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