btrfs-progs: mkfs: Replace number with enum
[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[MKFS_SUPER_BLOCK] = BTRFS_SUPER_INFO_OFFSET;
97         for (i = 1; i < MKFS_BLOCK_COUNT; 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[MKFS_SUPER_BLOCK]);
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[MKFS_ROOT_TREE]);
107         btrfs_set_super_chunk_root(&super, cfg->blocks[MKFS_CHUNK_TREE]);
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[MKFS_ROOT_TREE]);
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[MKFS_EXTENT_TREE]);
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[MKFS_DEV_TREE]);
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[MKFS_FS_TREE]);
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[MKFS_CSUM_TREE]);
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[MKFS_ROOT_TREE]);
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 < MKFS_BLOCK_COUNT; 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[MKFS_EXTENT_TREE]);
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[MKFS_EXTENT_TREE]);
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[MKFS_CHUNK_TREE]);
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[MKFS_CHUNK_TREE]);
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[MKFS_DEV_TREE]);
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[MKFS_DEV_TREE]);
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[MKFS_FS_TREE]);
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[MKFS_FS_TREE]);
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[MKFS_CSUM_TREE]);
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[MKFS_CSUM_TREE]);
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,
437                         cfg->blocks[MKFS_SUPER_BLOCK]);
438         if (ret != BTRFS_SUPER_INFO_SIZE) {
439                 ret = (ret < 0 ? -errno : -EIO);
440                 goto out;
441         }
442
443         ret = 0;
444
445 out:
446         free(buf);
447         return ret;
448 }
449
450 u64 btrfs_min_dev_size(u32 nodesize)
451 {
452         return 2 * (BTRFS_MKFS_SYSTEM_GROUP_SIZE +
453                     btrfs_min_global_blk_rsv_size(nodesize));
454 }
455
456 /*
457  * Btrfs minimum size calculation is complicated, it should include at least:
458  * 1. system group size
459  * 2. minimum global block reserve
460  * 3. metadata used at mkfs
461  * 4. space reservation to create uuid for first mount.
462  * Also, raid factor should also be taken into consideration.
463  * To avoid the overkill calculation, (system group + global block rsv) * 2
464  * for *EACH* device should be good enough.
465  */
466 u64 btrfs_min_global_blk_rsv_size(u32 nodesize)
467 {
468         return (u64)nodesize << 10;
469 }
470
471 #define isoctal(c)      (((c) & ~7) == '0')
472
473 static inline void translate(char *f, char *t)
474 {
475         while (*f != '\0') {
476                 if (*f == '\\' &&
477                     isoctal(f[1]) && isoctal(f[2]) && isoctal(f[3])) {
478                         *t++ = 64*(f[1] & 7) + 8*(f[2] & 7) + (f[3] & 7);
479                         f += 4;
480                 } else
481                         *t++ = *f++;
482         }
483         *t = '\0';
484         return;
485 }
486
487 /*
488  * Checks if the swap device.
489  * Returns 1 if swap device, < 0 on error or 0 if not swap device.
490  */
491 static int is_swap_device(const char *file)
492 {
493         FILE    *f;
494         struct stat     st_buf;
495         dev_t   dev;
496         ino_t   ino = 0;
497         char    tmp[PATH_MAX];
498         char    buf[PATH_MAX];
499         char    *cp;
500         int     ret = 0;
501
502         if (stat(file, &st_buf) < 0)
503                 return -errno;
504         if (S_ISBLK(st_buf.st_mode))
505                 dev = st_buf.st_rdev;
506         else if (S_ISREG(st_buf.st_mode)) {
507                 dev = st_buf.st_dev;
508                 ino = st_buf.st_ino;
509         } else
510                 return 0;
511
512         if ((f = fopen("/proc/swaps", "r")) == NULL)
513                 return 0;
514
515         /* skip the first line */
516         if (fgets(tmp, sizeof(tmp), f) == NULL)
517                 goto out;
518
519         while (fgets(tmp, sizeof(tmp), f) != NULL) {
520                 if ((cp = strchr(tmp, ' ')) != NULL)
521                         *cp = '\0';
522                 if ((cp = strchr(tmp, '\t')) != NULL)
523                         *cp = '\0';
524                 translate(tmp, buf);
525                 if (stat(buf, &st_buf) != 0)
526                         continue;
527                 if (S_ISBLK(st_buf.st_mode)) {
528                         if (dev == st_buf.st_rdev) {
529                                 ret = 1;
530                                 break;
531                         }
532                 } else if (S_ISREG(st_buf.st_mode)) {
533                         if (dev == st_buf.st_dev && ino == st_buf.st_ino) {
534                                 ret = 1;
535                                 break;
536                         }
537                 }
538         }
539
540 out:
541         fclose(f);
542
543         return ret;
544 }
545
546 /*
547  * Check for existing filesystem or partition table on device.
548  * Returns:
549  *       1 for existing fs or partition
550  *       0 for nothing found
551  *      -1 for internal error
552  */
553 static int check_overwrite(const char *device)
554 {
555         const char      *type;
556         blkid_probe     pr = NULL;
557         int             ret;
558         blkid_loff_t    size;
559
560         if (!device || !*device)
561                 return 0;
562
563         ret = -1; /* will reset on success of all setup calls */
564
565         pr = blkid_new_probe_from_filename(device);
566         if (!pr)
567                 goto out;
568
569         size = blkid_probe_get_size(pr);
570         if (size < 0)
571                 goto out;
572
573         /* nothing to overwrite on a 0-length device */
574         if (size == 0) {
575                 ret = 0;
576                 goto out;
577         }
578
579         ret = blkid_probe_enable_partitions(pr, 1);
580         if (ret < 0)
581                 goto out;
582
583         ret = blkid_do_fullprobe(pr);
584         if (ret < 0)
585                 goto out;
586
587         /*
588          * Blkid returns 1 for nothing found and 0 when it finds a signature,
589          * but we want the exact opposite, so reverse the return value here.
590          *
591          * In addition print some useful diagnostics about what actually is
592          * on the device.
593          */
594         if (ret) {
595                 ret = 0;
596                 goto out;
597         }
598
599         if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
600                 fprintf(stderr,
601                         "%s appears to contain an existing "
602                         "filesystem (%s).\n", device, type);
603         } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
604                 fprintf(stderr,
605                         "%s appears to contain a partition "
606                         "table (%s).\n", device, type);
607         } else {
608                 fprintf(stderr,
609                         "%s appears to contain something weird "
610                         "according to blkid\n", device);
611         }
612         ret = 1;
613
614 out:
615         if (pr)
616                 blkid_free_probe(pr);
617         if (ret == -1)
618                 fprintf(stderr,
619                         "probe of %s failed, cannot detect "
620                           "existing filesystem.\n", device);
621         return ret;
622 }
623
624 /*
625  * Check if a device is suitable for btrfs
626  * returns:
627  *  1: something is wrong, an error is printed
628  *  0: all is fine
629  */
630 int test_dev_for_mkfs(const char *file, int force_overwrite)
631 {
632         int ret, fd;
633         struct stat st;
634
635         ret = is_swap_device(file);
636         if (ret < 0) {
637                 error("checking status of %s: %s", file, strerror(-ret));
638                 return 1;
639         }
640         if (ret == 1) {
641                 error("%s is a swap device", file);
642                 return 1;
643         }
644         if (!force_overwrite) {
645                 if (check_overwrite(file)) {
646                         error("use the -f option to force overwrite of %s",
647                                         file);
648                         return 1;
649                 }
650         }
651         ret = check_mounted(file);
652         if (ret < 0) {
653                 error("cannot check mount status of %s: %s", file,
654                                 strerror(-ret));
655                 return 1;
656         }
657         if (ret == 1) {
658                 error("%s is mounted", file);
659                 return 1;
660         }
661         /* check if the device is busy */
662         fd = open(file, O_RDWR|O_EXCL);
663         if (fd < 0) {
664                 error("unable to open %s: %s", file, strerror(errno));
665                 return 1;
666         }
667         if (fstat(fd, &st)) {
668                 error("unable to stat %s: %s", file, strerror(errno));
669                 close(fd);
670                 return 1;
671         }
672         if (!S_ISBLK(st.st_mode)) {
673                 error("%s is not a block device", file);
674                 close(fd);
675                 return 1;
676         }
677         close(fd);
678         return 0;
679 }
680
681 int is_vol_small(const char *file)
682 {
683         int fd = -1;
684         int e;
685         struct stat st;
686         u64 size;
687
688         fd = open(file, O_RDONLY);
689         if (fd < 0)
690                 return -errno;
691         if (fstat(fd, &st) < 0) {
692                 e = -errno;
693                 close(fd);
694                 return e;
695         }
696         size = btrfs_device_size(fd, &st);
697         if (size == 0) {
698                 close(fd);
699                 return -1;
700         }
701         if (size < BTRFS_MKFS_SMALL_VOLUME_SIZE) {
702                 close(fd);
703                 return 1;
704         } else {
705                 close(fd);
706                 return 0;
707         }
708 }
709
710 int test_minimum_size(const char *file, u32 nodesize)
711 {
712         int fd;
713         struct stat statbuf;
714
715         fd = open(file, O_RDONLY);
716         if (fd < 0)
717                 return -errno;
718         if (stat(file, &statbuf) < 0) {
719                 close(fd);
720                 return -errno;
721         }
722         if (btrfs_device_size(fd, &statbuf) < btrfs_min_dev_size(nodesize)) {
723                 close(fd);
724                 return 1;
725         }
726         close(fd);
727         return 0;
728 }
729
730