btrfs-progs: mkfs: avoid positive return value from cleanup_temp_chunks
[platform/upstream/btrfs-progs.git] / mkfs / common.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include <unistd.h>
18 #include <uuid/uuid.h>
19 #include <blkid/blkid.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include "ctree.h"
23 #include "disk-io.h"
24 #include "volumes.h"
25 #include "utils.h"
26 #include "mkfs/common.h"
27
28 static u64 reference_root_table[] = {
29         [1] =   BTRFS_ROOT_TREE_OBJECTID,
30         [2] =   BTRFS_EXTENT_TREE_OBJECTID,
31         [3] =   BTRFS_CHUNK_TREE_OBJECTID,
32         [4] =   BTRFS_DEV_TREE_OBJECTID,
33         [5] =   BTRFS_FS_TREE_OBJECTID,
34         [6] =   BTRFS_CSUM_TREE_OBJECTID,
35 };
36
37 static int btrfs_create_tree_root(int fd, struct btrfs_mkfs_config *cfg,
38                         struct extent_buffer *buf)
39 {
40         struct btrfs_root_item root_item;
41         struct btrfs_inode_item *inode_item;
42         struct btrfs_disk_key disk_key;
43         u32 nritems = 0;
44         u32 itemoff;
45         int ret = 0;
46         int blk;
47
48         memset(buf->data + sizeof(struct btrfs_header), 0,
49                 cfg->nodesize - sizeof(struct btrfs_header));
50         memset(&root_item, 0, sizeof(root_item));
51         memset(&disk_key, 0, sizeof(disk_key));
52
53         /* create the items for the root tree */
54         inode_item = &root_item.inode;
55         btrfs_set_stack_inode_generation(inode_item, 1);
56         btrfs_set_stack_inode_size(inode_item, 3);
57         btrfs_set_stack_inode_nlink(inode_item, 1);
58         btrfs_set_stack_inode_nbytes(inode_item, cfg->nodesize);
59         btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
60         btrfs_set_root_refs(&root_item, 1);
61         btrfs_set_root_used(&root_item, cfg->nodesize);
62         btrfs_set_root_generation(&root_item, 1);
63
64         btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
65         btrfs_set_disk_key_offset(&disk_key, 0);
66         itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - sizeof(root_item);
67
68         for (blk = 0; blk < MKFS_BLOCK_COUNT; blk++) {
69                 if (blk == MKFS_SUPER_BLOCK || blk == MKFS_ROOT_TREE
70                     || blk == MKFS_CHUNK_TREE)
71                         continue;
72
73                 btrfs_set_root_bytenr(&root_item, cfg->blocks[blk]);
74                 btrfs_set_disk_key_objectid(&disk_key,
75                         reference_root_table[blk]);
76                 btrfs_set_item_key(buf, &disk_key, nritems);
77                 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
78                 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
79                                 sizeof(root_item));
80                 write_extent_buffer(buf, &root_item,
81                         btrfs_item_ptr_offset(buf, nritems),
82                         sizeof(root_item));
83                 nritems++;
84                 itemoff -= sizeof(root_item);
85         }
86
87         /* generate checksum */
88         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
89
90         /* write back root tree */
91         ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_ROOT_TREE]);
92         if (ret != cfg->nodesize)
93                 return (ret < 0 ? -errno : -EIO);
94
95         return ret;
96 }
97
98 /*
99  * @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
100  *
101  * The superblock signature is not valid, denotes a partially created
102  * filesystem, needs to be finalized.
103  */
104 int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
105 {
106         struct btrfs_super_block super;
107         struct extent_buffer *buf;
108         struct btrfs_disk_key disk_key;
109         struct btrfs_extent_item *extent_item;
110         struct btrfs_chunk *chunk;
111         struct btrfs_dev_item *dev_item;
112         struct btrfs_dev_extent *dev_extent;
113         u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
114         u8 *ptr;
115         int i;
116         int ret;
117         u32 itemoff;
118         u32 nritems = 0;
119         u64 first_free;
120         u64 ref_root;
121         u32 array_size;
122         u32 item_size;
123         int skinny_metadata = !!(cfg->features &
124                                  BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
125         u64 num_bytes;
126
127         buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
128         if (!buf)
129                 return -ENOMEM;
130
131         first_free = BTRFS_SUPER_INFO_OFFSET + cfg->sectorsize * 2 - 1;
132         first_free &= ~((u64)cfg->sectorsize - 1);
133
134         memset(&super, 0, sizeof(super));
135
136         num_bytes = (cfg->num_bytes / cfg->sectorsize) * cfg->sectorsize;
137         if (*cfg->fs_uuid) {
138                 if (uuid_parse(cfg->fs_uuid, super.fsid) != 0) {
139                         error("cannot not parse UUID: %s", cfg->fs_uuid);
140                         ret = -EINVAL;
141                         goto out;
142                 }
143                 if (!test_uuid_unique(cfg->fs_uuid)) {
144                         error("non-unique UUID: %s", cfg->fs_uuid);
145                         ret = -EBUSY;
146                         goto out;
147                 }
148         } else {
149                 uuid_generate(super.fsid);
150                 uuid_unparse(super.fsid, cfg->fs_uuid);
151         }
152         uuid_generate(super.dev_item.uuid);
153         uuid_generate(chunk_tree_uuid);
154
155         cfg->blocks[MKFS_SUPER_BLOCK] = BTRFS_SUPER_INFO_OFFSET;
156         for (i = 1; i < MKFS_BLOCK_COUNT; i++) {
157                 cfg->blocks[i] = BTRFS_SUPER_INFO_OFFSET + SZ_1M +
158                         cfg->nodesize * i;
159         }
160
161         btrfs_set_super_bytenr(&super, cfg->blocks[MKFS_SUPER_BLOCK]);
162         btrfs_set_super_num_devices(&super, 1);
163         btrfs_set_super_magic(&super, BTRFS_MAGIC_PARTIAL);
164         btrfs_set_super_generation(&super, 1);
165         btrfs_set_super_root(&super, cfg->blocks[MKFS_ROOT_TREE]);
166         btrfs_set_super_chunk_root(&super, cfg->blocks[MKFS_CHUNK_TREE]);
167         btrfs_set_super_total_bytes(&super, num_bytes);
168         btrfs_set_super_bytes_used(&super, 6 * cfg->nodesize);
169         btrfs_set_super_sectorsize(&super, cfg->sectorsize);
170         super.__unused_leafsize = cpu_to_le32(cfg->nodesize);
171         btrfs_set_super_nodesize(&super, cfg->nodesize);
172         btrfs_set_super_stripesize(&super, cfg->stripesize);
173         btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
174         btrfs_set_super_chunk_root_generation(&super, 1);
175         btrfs_set_super_cache_generation(&super, -1);
176         btrfs_set_super_incompat_flags(&super, cfg->features);
177         if (cfg->label)
178                 __strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE - 1);
179
180         /* create the tree of root objects */
181         memset(buf->data, 0, cfg->nodesize);
182         buf->len = cfg->nodesize;
183         btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_ROOT_TREE]);
184         btrfs_set_header_nritems(buf, 4);
185         btrfs_set_header_generation(buf, 1);
186         btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
187         btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
188         write_extent_buffer(buf, super.fsid, btrfs_header_fsid(),
189                             BTRFS_FSID_SIZE);
190
191         write_extent_buffer(buf, chunk_tree_uuid,
192                             btrfs_header_chunk_tree_uuid(buf),
193                             BTRFS_UUID_SIZE);
194
195         ret = btrfs_create_tree_root(fd, cfg, buf);
196         if (ret < 0)
197                 goto out;
198
199         /* create the items for the extent tree */
200         memset(buf->data + sizeof(struct btrfs_header), 0,
201                 cfg->nodesize - sizeof(struct btrfs_header));
202         nritems = 0;
203         itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize);
204         for (i = 1; i < MKFS_BLOCK_COUNT; i++) {
205                 item_size = sizeof(struct btrfs_extent_item);
206                 if (!skinny_metadata)
207                         item_size += sizeof(struct btrfs_tree_block_info);
208
209                 if (cfg->blocks[i] < first_free) {
210                         error("block[%d] below first free: %llu < %llu",
211                                         i, (unsigned long long)cfg->blocks[i],
212                                         (unsigned long long)first_free);
213                         ret = -EINVAL;
214                         goto out;
215                 }
216                 if (cfg->blocks[i] < cfg->blocks[i - 1]) {
217                         error("blocks %d and %d in reverse order: %llu < %llu",
218                                 i, i - 1,
219                                 (unsigned long long)cfg->blocks[i],
220                                 (unsigned long long)cfg->blocks[i - 1]);
221                         ret = -EINVAL;
222                         goto out;
223                 }
224
225                 /* create extent item */
226                 itemoff -= item_size;
227                 btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[i]);
228                 if (skinny_metadata) {
229                         btrfs_set_disk_key_type(&disk_key,
230                                                 BTRFS_METADATA_ITEM_KEY);
231                         btrfs_set_disk_key_offset(&disk_key, 0);
232                 } else {
233                         btrfs_set_disk_key_type(&disk_key,
234                                                 BTRFS_EXTENT_ITEM_KEY);
235                         btrfs_set_disk_key_offset(&disk_key, cfg->nodesize);
236                 }
237                 btrfs_set_item_key(buf, &disk_key, nritems);
238                 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
239                                       itemoff);
240                 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
241                                     item_size);
242                 extent_item = btrfs_item_ptr(buf, nritems,
243                                              struct btrfs_extent_item);
244                 btrfs_set_extent_refs(buf, extent_item, 1);
245                 btrfs_set_extent_generation(buf, extent_item, 1);
246                 btrfs_set_extent_flags(buf, extent_item,
247                                        BTRFS_EXTENT_FLAG_TREE_BLOCK);
248                 nritems++;
249
250                 /* create extent ref */
251                 ref_root = reference_root_table[i];
252                 btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[i]);
253                 btrfs_set_disk_key_offset(&disk_key, ref_root);
254                 btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
255                 btrfs_set_item_key(buf, &disk_key, nritems);
256                 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
257                                       itemoff);
258                 btrfs_set_item_size(buf, btrfs_item_nr(nritems), 0);
259                 nritems++;
260         }
261         btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_EXTENT_TREE]);
262         btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
263         btrfs_set_header_nritems(buf, nritems);
264         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
265         ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_EXTENT_TREE]);
266         if (ret != cfg->nodesize) {
267                 ret = (ret < 0 ? -errno : -EIO);
268                 goto out;
269         }
270
271         /* create the chunk tree */
272         memset(buf->data + sizeof(struct btrfs_header), 0,
273                 cfg->nodesize - sizeof(struct btrfs_header));
274         nritems = 0;
275         item_size = sizeof(*dev_item);
276         itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - item_size;
277
278         /* first device 1 (there is no device 0) */
279         btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
280         btrfs_set_disk_key_offset(&disk_key, 1);
281         btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
282         btrfs_set_item_key(buf, &disk_key, nritems);
283         btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
284         btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
285
286         dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
287         btrfs_set_device_id(buf, dev_item, 1);
288         btrfs_set_device_generation(buf, dev_item, 0);
289         btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
290         btrfs_set_device_bytes_used(buf, dev_item,
291                                     BTRFS_MKFS_SYSTEM_GROUP_SIZE);
292         btrfs_set_device_io_align(buf, dev_item, cfg->sectorsize);
293         btrfs_set_device_io_width(buf, dev_item, cfg->sectorsize);
294         btrfs_set_device_sector_size(buf, dev_item, cfg->sectorsize);
295         btrfs_set_device_type(buf, dev_item, 0);
296
297         write_extent_buffer(buf, super.dev_item.uuid,
298                             (unsigned long)btrfs_device_uuid(dev_item),
299                             BTRFS_UUID_SIZE);
300         write_extent_buffer(buf, super.fsid,
301                             (unsigned long)btrfs_device_fsid(dev_item),
302                             BTRFS_UUID_SIZE);
303         read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
304                            sizeof(*dev_item));
305
306         nritems++;
307         item_size = btrfs_chunk_item_size(1);
308         itemoff = itemoff - item_size;
309
310         /* then we have chunk 0 */
311         btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
312         btrfs_set_disk_key_offset(&disk_key, 0);
313         btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
314         btrfs_set_item_key(buf, &disk_key, nritems);
315         btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
316         btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
317
318         chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
319         btrfs_set_chunk_length(buf, chunk, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
320         btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
321         btrfs_set_chunk_stripe_len(buf, chunk, BTRFS_STRIPE_LEN);
322         btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
323         btrfs_set_chunk_io_align(buf, chunk, cfg->sectorsize);
324         btrfs_set_chunk_io_width(buf, chunk, cfg->sectorsize);
325         btrfs_set_chunk_sector_size(buf, chunk, cfg->sectorsize);
326         btrfs_set_chunk_num_stripes(buf, chunk, 1);
327         btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
328         btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
329         nritems++;
330
331         write_extent_buffer(buf, super.dev_item.uuid,
332                             (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
333                             BTRFS_UUID_SIZE);
334
335         /* copy the key for the chunk to the system array */
336         ptr = super.sys_chunk_array;
337         array_size = sizeof(disk_key);
338
339         memcpy(ptr, &disk_key, sizeof(disk_key));
340         ptr += sizeof(disk_key);
341
342         /* copy the chunk to the system array */
343         read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
344         array_size += item_size;
345         ptr += item_size;
346         btrfs_set_super_sys_array_size(&super, array_size);
347
348         btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CHUNK_TREE]);
349         btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
350         btrfs_set_header_nritems(buf, nritems);
351         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
352         ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CHUNK_TREE]);
353         if (ret != cfg->nodesize) {
354                 ret = (ret < 0 ? -errno : -EIO);
355                 goto out;
356         }
357
358         /* create the device tree */
359         memset(buf->data + sizeof(struct btrfs_header), 0,
360                 cfg->nodesize - sizeof(struct btrfs_header));
361         nritems = 0;
362         itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) -
363                 sizeof(struct btrfs_dev_extent);
364
365         btrfs_set_disk_key_objectid(&disk_key, 1);
366         btrfs_set_disk_key_offset(&disk_key, 0);
367         btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
368         btrfs_set_item_key(buf, &disk_key, nritems);
369         btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
370         btrfs_set_item_size(buf, btrfs_item_nr(nritems),
371                             sizeof(struct btrfs_dev_extent));
372         dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
373         btrfs_set_dev_extent_chunk_tree(buf, dev_extent,
374                                         BTRFS_CHUNK_TREE_OBJECTID);
375         btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
376                                         BTRFS_FIRST_CHUNK_TREE_OBJECTID);
377         btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
378
379         write_extent_buffer(buf, chunk_tree_uuid,
380                     (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
381                     BTRFS_UUID_SIZE);
382
383         btrfs_set_dev_extent_length(buf, dev_extent,
384                                     BTRFS_MKFS_SYSTEM_GROUP_SIZE);
385         nritems++;
386
387         btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_DEV_TREE]);
388         btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
389         btrfs_set_header_nritems(buf, nritems);
390         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
391         ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_DEV_TREE]);
392         if (ret != cfg->nodesize) {
393                 ret = (ret < 0 ? -errno : -EIO);
394                 goto out;
395         }
396
397         /* create the FS root */
398         memset(buf->data + sizeof(struct btrfs_header), 0,
399                 cfg->nodesize - sizeof(struct btrfs_header));
400         btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_FS_TREE]);
401         btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
402         btrfs_set_header_nritems(buf, 0);
403         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
404         ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_FS_TREE]);
405         if (ret != cfg->nodesize) {
406                 ret = (ret < 0 ? -errno : -EIO);
407                 goto out;
408         }
409         /* finally create the csum root */
410         memset(buf->data + sizeof(struct btrfs_header), 0,
411                 cfg->nodesize - sizeof(struct btrfs_header));
412         btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CSUM_TREE]);
413         btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
414         btrfs_set_header_nritems(buf, 0);
415         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
416         ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CSUM_TREE]);
417         if (ret != cfg->nodesize) {
418                 ret = (ret < 0 ? -errno : -EIO);
419                 goto out;
420         }
421
422         /* and write out the super block */
423         memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
424         memcpy(buf->data, &super, sizeof(super));
425         buf->len = BTRFS_SUPER_INFO_SIZE;
426         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
427         ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
428                         cfg->blocks[MKFS_SUPER_BLOCK]);
429         if (ret != BTRFS_SUPER_INFO_SIZE) {
430                 ret = (ret < 0 ? -errno : -EIO);
431                 goto out;
432         }
433
434         ret = 0;
435
436 out:
437         free(buf);
438         return ret;
439 }
440
441 u64 btrfs_min_dev_size(u32 nodesize)
442 {
443         return 2 * (BTRFS_MKFS_SYSTEM_GROUP_SIZE +
444                     btrfs_min_global_blk_rsv_size(nodesize));
445 }
446
447 /*
448  * Btrfs minimum size calculation is complicated, it should include at least:
449  * 1. system group size
450  * 2. minimum global block reserve
451  * 3. metadata used at mkfs
452  * 4. space reservation to create uuid for first mount.
453  * Also, raid factor should also be taken into consideration.
454  * To avoid the overkill calculation, (system group + global block rsv) * 2
455  * for *EACH* device should be good enough.
456  */
457 u64 btrfs_min_global_blk_rsv_size(u32 nodesize)
458 {
459         return (u64)nodesize << 10;
460 }
461
462 #define isoctal(c)      (((c) & ~7) == '0')
463
464 static inline void translate(char *f, char *t)
465 {
466         while (*f != '\0') {
467                 if (*f == '\\' &&
468                     isoctal(f[1]) && isoctal(f[2]) && isoctal(f[3])) {
469                         *t++ = 64*(f[1] & 7) + 8*(f[2] & 7) + (f[3] & 7);
470                         f += 4;
471                 } else
472                         *t++ = *f++;
473         }
474         *t = '\0';
475         return;
476 }
477
478 /*
479  * Checks if the swap device.
480  * Returns 1 if swap device, < 0 on error or 0 if not swap device.
481  */
482 static int is_swap_device(const char *file)
483 {
484         FILE    *f;
485         struct stat     st_buf;
486         dev_t   dev;
487         ino_t   ino = 0;
488         char    tmp[PATH_MAX];
489         char    buf[PATH_MAX];
490         char    *cp;
491         int     ret = 0;
492
493         if (stat(file, &st_buf) < 0)
494                 return -errno;
495         if (S_ISBLK(st_buf.st_mode))
496                 dev = st_buf.st_rdev;
497         else if (S_ISREG(st_buf.st_mode)) {
498                 dev = st_buf.st_dev;
499                 ino = st_buf.st_ino;
500         } else
501                 return 0;
502
503         if ((f = fopen("/proc/swaps", "r")) == NULL)
504                 return 0;
505
506         /* skip the first line */
507         if (fgets(tmp, sizeof(tmp), f) == NULL)
508                 goto out;
509
510         while (fgets(tmp, sizeof(tmp), f) != NULL) {
511                 if ((cp = strchr(tmp, ' ')) != NULL)
512                         *cp = '\0';
513                 if ((cp = strchr(tmp, '\t')) != NULL)
514                         *cp = '\0';
515                 translate(tmp, buf);
516                 if (stat(buf, &st_buf) != 0)
517                         continue;
518                 if (S_ISBLK(st_buf.st_mode)) {
519                         if (dev == st_buf.st_rdev) {
520                                 ret = 1;
521                                 break;
522                         }
523                 } else if (S_ISREG(st_buf.st_mode)) {
524                         if (dev == st_buf.st_dev && ino == st_buf.st_ino) {
525                                 ret = 1;
526                                 break;
527                         }
528                 }
529         }
530
531 out:
532         fclose(f);
533
534         return ret;
535 }
536
537 /*
538  * Check for existing filesystem or partition table on device.
539  * Returns:
540  *       1 for existing fs or partition
541  *       0 for nothing found
542  *      -1 for internal error
543  */
544 static int check_overwrite(const char *device)
545 {
546         const char      *type;
547         blkid_probe     pr = NULL;
548         int             ret;
549         blkid_loff_t    size;
550
551         if (!device || !*device)
552                 return 0;
553
554         ret = -1; /* will reset on success of all setup calls */
555
556         pr = blkid_new_probe_from_filename(device);
557         if (!pr)
558                 goto out;
559
560         size = blkid_probe_get_size(pr);
561         if (size < 0)
562                 goto out;
563
564         /* nothing to overwrite on a 0-length device */
565         if (size == 0) {
566                 ret = 0;
567                 goto out;
568         }
569
570         ret = blkid_probe_enable_partitions(pr, 1);
571         if (ret < 0)
572                 goto out;
573
574         ret = blkid_do_fullprobe(pr);
575         if (ret < 0)
576                 goto out;
577
578         /*
579          * Blkid returns 1 for nothing found and 0 when it finds a signature,
580          * but we want the exact opposite, so reverse the return value here.
581          *
582          * In addition print some useful diagnostics about what actually is
583          * on the device.
584          */
585         if (ret) {
586                 ret = 0;
587                 goto out;
588         }
589
590         if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
591                 fprintf(stderr,
592                         "%s appears to contain an existing "
593                         "filesystem (%s).\n", device, type);
594         } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
595                 fprintf(stderr,
596                         "%s appears to contain a partition "
597                         "table (%s).\n", device, type);
598         } else {
599                 fprintf(stderr,
600                         "%s appears to contain something weird "
601                         "according to blkid\n", device);
602         }
603         ret = 1;
604
605 out:
606         if (pr)
607                 blkid_free_probe(pr);
608         if (ret == -1)
609                 fprintf(stderr,
610                         "probe of %s failed, cannot detect "
611                           "existing filesystem.\n", device);
612         return ret;
613 }
614
615 /*
616  * Check if a device is suitable for btrfs
617  * returns:
618  *  1: something is wrong, an error is printed
619  *  0: all is fine
620  */
621 int test_dev_for_mkfs(const char *file, int force_overwrite)
622 {
623         int ret, fd;
624         struct stat st;
625
626         ret = is_swap_device(file);
627         if (ret < 0) {
628                 error("checking status of %s: %s", file, strerror(-ret));
629                 return 1;
630         }
631         if (ret == 1) {
632                 error("%s is a swap device", file);
633                 return 1;
634         }
635         if (!force_overwrite) {
636                 if (check_overwrite(file)) {
637                         error("use the -f option to force overwrite of %s",
638                                         file);
639                         return 1;
640                 }
641         }
642         ret = check_mounted(file);
643         if (ret < 0) {
644                 error("cannot check mount status of %s: %s", file,
645                                 strerror(-ret));
646                 return 1;
647         }
648         if (ret == 1) {
649                 error("%s is mounted", file);
650                 return 1;
651         }
652         /* check if the device is busy */
653         fd = open(file, O_RDWR|O_EXCL);
654         if (fd < 0) {
655                 error("unable to open %s: %s", file, strerror(errno));
656                 return 1;
657         }
658         if (fstat(fd, &st)) {
659                 error("unable to stat %s: %s", file, strerror(errno));
660                 close(fd);
661                 return 1;
662         }
663         if (!S_ISBLK(st.st_mode)) {
664                 error("%s is not a block device", file);
665                 close(fd);
666                 return 1;
667         }
668         close(fd);
669         return 0;
670 }
671
672 int is_vol_small(const char *file)
673 {
674         int fd = -1;
675         int e;
676         struct stat st;
677         u64 size;
678
679         fd = open(file, O_RDONLY);
680         if (fd < 0)
681                 return -errno;
682         if (fstat(fd, &st) < 0) {
683                 e = -errno;
684                 close(fd);
685                 return e;
686         }
687         size = btrfs_device_size(fd, &st);
688         if (size == 0) {
689                 close(fd);
690                 return -1;
691         }
692         if (size < BTRFS_MKFS_SMALL_VOLUME_SIZE) {
693                 close(fd);
694                 return 1;
695         } else {
696                 close(fd);
697                 return 0;
698         }
699 }
700
701 int test_minimum_size(const char *file, u32 nodesize)
702 {
703         int fd;
704         struct stat statbuf;
705
706         fd = open(file, O_RDONLY);
707         if (fd < 0)
708                 return -errno;
709         if (stat(file, &statbuf) < 0) {
710                 close(fd);
711                 return -errno;
712         }
713         if (btrfs_device_size(fd, &statbuf) < btrfs_min_dev_size(nodesize)) {
714                 close(fd);
715                 return 1;
716         }
717         close(fd);
718         return 0;
719 }
720
721