2 * Copyright (C) 2007 Oracle. All rights reserved.
3 * Copyright (C) 2008 Morey Roof. All rights reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 021110-1307, USA.
20 #define _XOPEN_SOURCE 700
21 #define __USE_XOPEN2K8
22 #define __XOPEN2K8 /* due to an error in dirent.h, to get dirfd() */
23 #define _GNU_SOURCE /* O_NOATIME */
27 #include <sys/ioctl.h>
28 #include <sys/mount.h>
29 #include <sys/types.h>
31 #include <uuid/uuid.h>
36 #include <linux/loop.h>
37 #include <linux/major.h>
38 #include <linux/kdev_t.h>
40 #include <blkid/blkid.h>
41 #include "kerncompat.h"
42 #include "radix-tree.h"
45 #include "transaction.h"
52 #define BLKDISCARD _IO(0x12,119)
56 * Discard the given range in one go
58 static int discard_range(int fd, u64 start, u64 len)
60 u64 range[2] = { start, len };
62 if (ioctl(fd, BLKDISCARD, &range) < 0)
68 * Discard blocks in the given range in 1G chunks, the process is interruptible
70 static int discard_blocks(int fd, u64 start, u64 len)
74 u64 chunk_size = min_t(u64, len, 1*1024*1024*1024);
77 ret = discard_range(fd, start, chunk_size);
87 static u64 reference_root_table[] = {
88 [1] = BTRFS_ROOT_TREE_OBJECTID,
89 [2] = BTRFS_EXTENT_TREE_OBJECTID,
90 [3] = BTRFS_CHUNK_TREE_OBJECTID,
91 [4] = BTRFS_DEV_TREE_OBJECTID,
92 [5] = BTRFS_FS_TREE_OBJECTID,
93 [6] = BTRFS_CSUM_TREE_OBJECTID,
96 int test_uuid_unique(char *fs_uuid)
99 blkid_dev_iterate iter = NULL;
100 blkid_dev dev = NULL;
101 blkid_cache cache = NULL;
103 if (blkid_get_cache(&cache, 0) < 0) {
104 printf("ERROR: lblkid cache get failed\n");
107 blkid_probe_all(cache);
108 iter = blkid_dev_iterate_begin(cache);
109 blkid_dev_set_search(iter, "UUID", fs_uuid);
111 while (blkid_dev_next(iter, &dev) == 0) {
112 dev = blkid_verify(cache, dev);
119 blkid_dev_iterate_end(iter);
120 blkid_put_cache(cache);
125 int make_btrfs(int fd, const char *device, const char *label, char *fs_uuid,
126 u64 blocks[7], u64 num_bytes, u32 nodesize,
127 u32 leafsize, u32 sectorsize, u32 stripesize, u64 features)
129 struct btrfs_super_block super;
130 struct extent_buffer *buf = NULL;
131 struct btrfs_root_item root_item;
132 struct btrfs_disk_key disk_key;
133 struct btrfs_extent_item *extent_item;
134 struct btrfs_inode_item *inode_item;
135 struct btrfs_chunk *chunk;
136 struct btrfs_dev_item *dev_item;
137 struct btrfs_dev_extent *dev_extent;
138 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
148 int skinny_metadata = !!(features &
149 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
151 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
152 first_free &= ~((u64)sectorsize - 1);
154 memset(&super, 0, sizeof(super));
156 num_bytes = (num_bytes / sectorsize) * sectorsize;
158 if (uuid_parse(fs_uuid, super.fsid) != 0) {
159 fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
163 if (!test_uuid_unique(fs_uuid)) {
164 fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
169 uuid_generate(super.fsid);
171 uuid_generate(super.dev_item.uuid);
172 uuid_generate(chunk_tree_uuid);
174 btrfs_set_super_bytenr(&super, blocks[0]);
175 btrfs_set_super_num_devices(&super, 1);
176 btrfs_set_super_magic(&super, BTRFS_MAGIC);
177 btrfs_set_super_generation(&super, 1);
178 btrfs_set_super_root(&super, blocks[1]);
179 btrfs_set_super_chunk_root(&super, blocks[3]);
180 btrfs_set_super_total_bytes(&super, num_bytes);
181 btrfs_set_super_bytes_used(&super, 6 * leafsize);
182 btrfs_set_super_sectorsize(&super, sectorsize);
183 btrfs_set_super_leafsize(&super, leafsize);
184 btrfs_set_super_nodesize(&super, nodesize);
185 btrfs_set_super_stripesize(&super, stripesize);
186 btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
187 btrfs_set_super_chunk_root_generation(&super, 1);
188 btrfs_set_super_cache_generation(&super, -1);
189 btrfs_set_super_incompat_flags(&super, features);
191 strncpy(super.label, label, BTRFS_LABEL_SIZE - 1);
193 buf = malloc(sizeof(*buf) + max(sectorsize, leafsize));
195 /* create the tree of root objects */
196 memset(buf->data, 0, leafsize);
198 btrfs_set_header_bytenr(buf, blocks[1]);
199 btrfs_set_header_nritems(buf, 4);
200 btrfs_set_header_generation(buf, 1);
201 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
202 btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
203 write_extent_buffer(buf, super.fsid, btrfs_header_fsid(),
206 write_extent_buffer(buf, chunk_tree_uuid,
207 btrfs_header_chunk_tree_uuid(buf),
210 /* create the items for the root tree */
211 memset(&root_item, 0, sizeof(root_item));
212 inode_item = &root_item.inode;
213 btrfs_set_stack_inode_generation(inode_item, 1);
214 btrfs_set_stack_inode_size(inode_item, 3);
215 btrfs_set_stack_inode_nlink(inode_item, 1);
216 btrfs_set_stack_inode_nbytes(inode_item, leafsize);
217 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
218 btrfs_set_root_refs(&root_item, 1);
219 btrfs_set_root_used(&root_item, leafsize);
220 btrfs_set_root_generation(&root_item, 1);
222 memset(&disk_key, 0, sizeof(disk_key));
223 btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
224 btrfs_set_disk_key_offset(&disk_key, 0);
227 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
228 btrfs_set_root_bytenr(&root_item, blocks[2]);
229 btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID);
230 btrfs_set_item_key(buf, &disk_key, nritems);
231 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
232 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
234 write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
235 nritems), sizeof(root_item));
238 itemoff = itemoff - sizeof(root_item);
239 btrfs_set_root_bytenr(&root_item, blocks[4]);
240 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
241 btrfs_set_item_key(buf, &disk_key, nritems);
242 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
243 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
245 write_extent_buffer(buf, &root_item,
246 btrfs_item_ptr_offset(buf, nritems),
250 itemoff = itemoff - sizeof(root_item);
251 btrfs_set_root_bytenr(&root_item, blocks[5]);
252 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
253 btrfs_set_item_key(buf, &disk_key, nritems);
254 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
255 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
257 write_extent_buffer(buf, &root_item,
258 btrfs_item_ptr_offset(buf, nritems),
262 itemoff = itemoff - sizeof(root_item);
263 btrfs_set_root_bytenr(&root_item, blocks[6]);
264 btrfs_set_disk_key_objectid(&disk_key, BTRFS_CSUM_TREE_OBJECTID);
265 btrfs_set_item_key(buf, &disk_key, nritems);
266 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
267 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
269 write_extent_buffer(buf, &root_item,
270 btrfs_item_ptr_offset(buf, nritems),
275 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
276 ret = pwrite(fd, buf->data, leafsize, blocks[1]);
277 if (ret != leafsize) {
278 ret = (ret < 0 ? -errno : -EIO);
282 /* create the items for the extent tree */
283 memset(buf->data+sizeof(struct btrfs_header), 0,
284 leafsize-sizeof(struct btrfs_header));
286 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize);
287 for (i = 1; i < 7; i++) {
288 item_size = sizeof(struct btrfs_extent_item);
289 if (!skinny_metadata)
290 item_size += sizeof(struct btrfs_tree_block_info);
292 BUG_ON(blocks[i] < first_free);
293 BUG_ON(blocks[i] < blocks[i - 1]);
295 /* create extent item */
296 itemoff -= item_size;
297 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
298 if (skinny_metadata) {
299 btrfs_set_disk_key_type(&disk_key,
300 BTRFS_METADATA_ITEM_KEY);
301 btrfs_set_disk_key_offset(&disk_key, 0);
303 btrfs_set_disk_key_type(&disk_key,
304 BTRFS_EXTENT_ITEM_KEY);
305 btrfs_set_disk_key_offset(&disk_key, leafsize);
307 btrfs_set_item_key(buf, &disk_key, nritems);
308 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
310 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
312 extent_item = btrfs_item_ptr(buf, nritems,
313 struct btrfs_extent_item);
314 btrfs_set_extent_refs(buf, extent_item, 1);
315 btrfs_set_extent_generation(buf, extent_item, 1);
316 btrfs_set_extent_flags(buf, extent_item,
317 BTRFS_EXTENT_FLAG_TREE_BLOCK);
320 /* create extent ref */
321 ref_root = reference_root_table[i];
322 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
323 btrfs_set_disk_key_offset(&disk_key, ref_root);
324 btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
325 btrfs_set_item_key(buf, &disk_key, nritems);
326 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
328 btrfs_set_item_size(buf, btrfs_item_nr(nritems), 0);
331 btrfs_set_header_bytenr(buf, blocks[2]);
332 btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
333 btrfs_set_header_nritems(buf, nritems);
334 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
335 ret = pwrite(fd, buf->data, leafsize, blocks[2]);
336 if (ret != leafsize) {
337 ret = (ret < 0 ? -errno : -EIO);
341 /* create the chunk tree */
342 memset(buf->data+sizeof(struct btrfs_header), 0,
343 leafsize-sizeof(struct btrfs_header));
345 item_size = sizeof(*dev_item);
346 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - item_size;
348 /* first device 1 (there is no device 0) */
349 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
350 btrfs_set_disk_key_offset(&disk_key, 1);
351 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
352 btrfs_set_item_key(buf, &disk_key, nritems);
353 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
354 btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
356 dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
357 btrfs_set_device_id(buf, dev_item, 1);
358 btrfs_set_device_generation(buf, dev_item, 0);
359 btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
360 btrfs_set_device_bytes_used(buf, dev_item,
361 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
362 btrfs_set_device_io_align(buf, dev_item, sectorsize);
363 btrfs_set_device_io_width(buf, dev_item, sectorsize);
364 btrfs_set_device_sector_size(buf, dev_item, sectorsize);
365 btrfs_set_device_type(buf, dev_item, 0);
367 write_extent_buffer(buf, super.dev_item.uuid,
368 (unsigned long)btrfs_device_uuid(dev_item),
370 write_extent_buffer(buf, super.fsid,
371 (unsigned long)btrfs_device_fsid(dev_item),
373 read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
377 item_size = btrfs_chunk_item_size(1);
378 itemoff = itemoff - item_size;
380 /* then we have chunk 0 */
381 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
382 btrfs_set_disk_key_offset(&disk_key, 0);
383 btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
384 btrfs_set_item_key(buf, &disk_key, nritems);
385 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
386 btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
388 chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
389 btrfs_set_chunk_length(buf, chunk, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
390 btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
391 btrfs_set_chunk_stripe_len(buf, chunk, 64 * 1024);
392 btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
393 btrfs_set_chunk_io_align(buf, chunk, sectorsize);
394 btrfs_set_chunk_io_width(buf, chunk, sectorsize);
395 btrfs_set_chunk_sector_size(buf, chunk, sectorsize);
396 btrfs_set_chunk_num_stripes(buf, chunk, 1);
397 btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
398 btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
401 write_extent_buffer(buf, super.dev_item.uuid,
402 (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
405 /* copy the key for the chunk to the system array */
406 ptr = super.sys_chunk_array;
407 array_size = sizeof(disk_key);
409 memcpy(ptr, &disk_key, sizeof(disk_key));
410 ptr += sizeof(disk_key);
412 /* copy the chunk to the system array */
413 read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
414 array_size += item_size;
416 btrfs_set_super_sys_array_size(&super, array_size);
418 btrfs_set_header_bytenr(buf, blocks[3]);
419 btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
420 btrfs_set_header_nritems(buf, nritems);
421 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
422 ret = pwrite(fd, buf->data, leafsize, blocks[3]);
423 if (ret != leafsize) {
424 ret = (ret < 0 ? -errno : -EIO);
428 /* create the device tree */
429 memset(buf->data+sizeof(struct btrfs_header), 0,
430 leafsize-sizeof(struct btrfs_header));
432 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
433 sizeof(struct btrfs_dev_extent);
435 btrfs_set_disk_key_objectid(&disk_key, 1);
436 btrfs_set_disk_key_offset(&disk_key, 0);
437 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
438 btrfs_set_item_key(buf, &disk_key, nritems);
439 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
440 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
441 sizeof(struct btrfs_dev_extent));
442 dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
443 btrfs_set_dev_extent_chunk_tree(buf, dev_extent,
444 BTRFS_CHUNK_TREE_OBJECTID);
445 btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
446 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
447 btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
449 write_extent_buffer(buf, chunk_tree_uuid,
450 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
453 btrfs_set_dev_extent_length(buf, dev_extent,
454 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
457 btrfs_set_header_bytenr(buf, blocks[4]);
458 btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
459 btrfs_set_header_nritems(buf, nritems);
460 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
461 ret = pwrite(fd, buf->data, leafsize, blocks[4]);
462 if (ret != leafsize) {
463 ret = (ret < 0 ? -errno : -EIO);
467 /* create the FS root */
468 memset(buf->data+sizeof(struct btrfs_header), 0,
469 leafsize-sizeof(struct btrfs_header));
470 btrfs_set_header_bytenr(buf, blocks[5]);
471 btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
472 btrfs_set_header_nritems(buf, 0);
473 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
474 ret = pwrite(fd, buf->data, leafsize, blocks[5]);
475 if (ret != leafsize) {
476 ret = (ret < 0 ? -errno : -EIO);
479 /* finally create the csum root */
480 memset(buf->data+sizeof(struct btrfs_header), 0,
481 leafsize-sizeof(struct btrfs_header));
482 btrfs_set_header_bytenr(buf, blocks[6]);
483 btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
484 btrfs_set_header_nritems(buf, 0);
485 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
486 ret = pwrite(fd, buf->data, leafsize, blocks[6]);
487 if (ret != leafsize) {
488 ret = (ret < 0 ? -errno : -EIO);
492 /* and write out the super block */
493 BUG_ON(sizeof(super) > sectorsize);
494 memset(buf->data, 0, sectorsize);
495 memcpy(buf->data, &super, sizeof(super));
496 buf->len = sectorsize;
497 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
498 ret = pwrite(fd, buf->data, sectorsize, blocks[0]);
499 if (ret != sectorsize) {
500 ret = (ret < 0 ? -errno : -EIO);
511 u64 btrfs_device_size(int fd, struct stat *st)
514 if (S_ISREG(st->st_mode)) {
517 if (!S_ISBLK(st->st_mode)) {
520 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
526 static int zero_blocks(int fd, off_t start, size_t len)
528 char *buf = malloc(len);
535 written = pwrite(fd, buf, len, start);
542 static int zero_dev_start(int fd)
545 size_t len = 2 * 1024 * 1024;
548 /* don't overwrite the disk labels on sparc */
552 return zero_blocks(fd, start, len);
555 static int zero_dev_end(int fd, u64 dev_size)
557 size_t len = 2 * 1024 * 1024;
558 off_t start = dev_size - len;
560 return zero_blocks(fd, start, len);
563 int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root, int fd, char *path,
565 u64 block_count, u32 io_width, u32 io_align,
568 struct btrfs_super_block *disk_super;
569 struct btrfs_super_block *super = root->fs_info->super_copy;
570 struct btrfs_device *device;
571 struct btrfs_dev_item *dev_item;
577 device = kzalloc(sizeof(*device), GFP_NOFS);
580 buf = kmalloc(sectorsize, GFP_NOFS);
585 BUG_ON(sizeof(*disk_super) > sectorsize);
586 memset(buf, 0, sectorsize);
588 disk_super = (struct btrfs_super_block *)buf;
589 dev_item = &disk_super->dev_item;
591 uuid_generate(device->uuid);
594 device->io_width = io_width;
595 device->io_align = io_align;
596 device->sector_size = sectorsize;
598 device->writeable = 1;
599 device->total_bytes = block_count;
600 device->bytes_used = 0;
601 device->total_ios = 0;
602 device->dev_root = root->fs_info->dev_root;
604 ret = btrfs_add_device(trans, root, device);
607 total_bytes = btrfs_super_total_bytes(super) + block_count;
608 btrfs_set_super_total_bytes(super, total_bytes);
610 num_devs = btrfs_super_num_devices(super) + 1;
611 btrfs_set_super_num_devices(super, num_devs);
613 memcpy(disk_super, super, sizeof(*disk_super));
615 printf("adding device %s id %llu\n", path,
616 (unsigned long long)device->devid);
618 btrfs_set_super_bytenr(disk_super, BTRFS_SUPER_INFO_OFFSET);
619 btrfs_set_stack_device_id(dev_item, device->devid);
620 btrfs_set_stack_device_type(dev_item, device->type);
621 btrfs_set_stack_device_io_align(dev_item, device->io_align);
622 btrfs_set_stack_device_io_width(dev_item, device->io_width);
623 btrfs_set_stack_device_sector_size(dev_item, device->sector_size);
624 btrfs_set_stack_device_total_bytes(dev_item, device->total_bytes);
625 btrfs_set_stack_device_bytes_used(dev_item, device->bytes_used);
626 memcpy(&dev_item->uuid, device->uuid, BTRFS_UUID_SIZE);
628 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
629 BUG_ON(ret != sectorsize);
632 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
633 device->fs_devices = root->fs_info->fs_devices;
637 int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
638 u64 max_block_count, int *mixed, int discard)
645 ret = fstat(fd, &st);
647 fprintf(stderr, "unable to stat %s\n", file);
651 block_count = btrfs_device_size(fd, &st);
652 if (block_count == 0) {
653 fprintf(stderr, "unable to find %s size\n", file);
657 block_count = min(block_count, max_block_count);
659 if (block_count < 1024 * 1024 * 1024 && !(*mixed)) {
660 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
666 * We intentionally ignore errors from the discard ioctl. It
667 * is not necessary for the mkfs functionality but just an
670 if (discard_range(fd, 0, 0) == 0) {
671 fprintf(stderr, "Performing full device TRIM (%s) ...\n",
672 pretty_size(block_count));
673 discard_blocks(fd, 0, block_count);
677 ret = zero_dev_start(fd);
681 for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
682 bytenr = btrfs_sb_offset(i);
683 if (bytenr >= block_count)
685 ret = zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
691 ret = zero_dev_end(fd, block_count);
695 *block_count_ret = block_count;
699 fprintf(stderr, "ERROR: failed to zero device '%s' - %s\n",
700 file, strerror(-ret));
702 } else if (ret > 0) {
703 fprintf(stderr, "ERROR: failed to zero device '%s' - %d\n",
710 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
711 struct btrfs_root *root, u64 objectid)
714 struct btrfs_inode_item inode_item;
715 time_t now = time(NULL);
717 memset(&inode_item, 0, sizeof(inode_item));
718 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
719 btrfs_set_stack_inode_size(&inode_item, 0);
720 btrfs_set_stack_inode_nlink(&inode_item, 1);
721 btrfs_set_stack_inode_nbytes(&inode_item, root->leafsize);
722 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
723 btrfs_set_stack_timespec_sec(&inode_item.atime, now);
724 btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
725 btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
726 btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
727 btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
728 btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
729 btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
730 btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
732 if (root->fs_info->tree_root == root)
733 btrfs_set_super_root_dir(root->fs_info->super_copy, objectid);
735 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
739 ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0);
743 btrfs_set_root_dirid(&root->root_item, objectid);
750 * checks if a path is a block device node
751 * Returns negative errno on failure, otherwise
752 * returns 1 for blockdev, 0 for not-blockdev
754 int is_block_device(const char *path)
758 if (stat(path, &statbuf) < 0)
761 return S_ISBLK(statbuf.st_mode);
765 * check if given path is a mount point
766 * return 1 if yes. 0 if no. -1 for error
768 int is_mount_point(const char *path)
774 f = setmntent("/proc/self/mounts", "r");
778 while ((mnt = getmntent(f)) != NULL) {
779 if (strcmp(mnt->mnt_dir, path))
789 * Find the mount point for a mounted device.
790 * On success, returns 0 with mountpoint in *mp.
791 * On failure, returns -errno (not mounted yields -EINVAL)
792 * Is noisy on failures, expects to be given a mounted device.
794 int get_btrfs_mount(const char *dev, char *mp, size_t mp_size)
799 ret = is_block_device(dev);
802 fprintf(stderr, "%s is not a block device\n", dev);
805 fprintf(stderr, "Could not check %s: %s\n",
806 dev, strerror(-ret));
811 fd = open(dev, O_RDONLY);
814 fprintf(stderr, "Could not open %s: %s\n", dev, strerror(errno));
818 ret = check_mounted_where(fd, dev, mp, mp_size, NULL);
821 } else { /* mounted, all good */
831 * Given a pathname, return a filehandle to:
832 * the original pathname or,
833 * if the pathname is a mounted btrfs device, to its mountpoint.
835 * On error, return -1, errno should be set.
837 int open_path_or_dev_mnt(const char *path, DIR **dirstream)
839 char mp[BTRFS_PATH_NAME_MAX + 1];
842 if (is_block_device(path)) {
845 ret = get_btrfs_mount(path, mp, sizeof(mp));
847 /* not a mounted btrfs dev */
851 fdmnt = open_file_or_dir(mp, dirstream);
853 fdmnt = open_file_or_dir(path, dirstream);
859 /* checks if a device is a loop device */
860 static int is_loop_device (const char* device) {
863 if(stat(device, &statbuf) < 0)
866 return (S_ISBLK(statbuf.st_mode) &&
867 MAJOR(statbuf.st_rdev) == LOOP_MAJOR);
871 /* Takes a loop device path (e.g. /dev/loop0) and returns
872 * the associated file (e.g. /images/my_btrfs.img) */
873 static int resolve_loop_device(const char* loop_dev, char* loop_file,
880 char real_loop_dev[PATH_MAX];
882 if (!realpath(loop_dev, real_loop_dev))
884 snprintf(p, PATH_MAX, "/sys/block/%s/loop/backing_file", strrchr(real_loop_dev, '/'));
885 if (!(f = fopen(p, "r")))
888 snprintf(fmt, 20, "%%%i[^\n]", max_len-1);
889 ret = fscanf(f, fmt, loop_file);
897 /* Checks whether a and b are identical or device
898 * files associated with the same block device
900 static int is_same_blk_file(const char* a, const char* b)
902 struct stat st_buf_a, st_buf_b;
903 char real_a[PATH_MAX];
904 char real_b[PATH_MAX];
906 if(!realpath(a, real_a))
909 if (!realpath(b, real_b))
912 /* Identical path? */
913 if(strcmp(real_a, real_b) == 0)
916 if(stat(a, &st_buf_a) < 0 ||
917 stat(b, &st_buf_b) < 0)
924 /* Same blockdevice? */
925 if(S_ISBLK(st_buf_a.st_mode) &&
926 S_ISBLK(st_buf_b.st_mode) &&
927 st_buf_a.st_rdev == st_buf_b.st_rdev)
933 if (st_buf_a.st_dev == st_buf_b.st_dev &&
934 st_buf_a.st_ino == st_buf_b.st_ino)
942 /* checks if a and b are identical or device
943 * files associated with the same block device or
944 * if one file is a loop device that uses the other
947 static int is_same_loop_file(const char* a, const char* b)
949 char res_a[PATH_MAX];
950 char res_b[PATH_MAX];
951 const char* final_a = NULL;
952 const char* final_b = NULL;
955 /* Resolve a if it is a loop device */
956 if((ret = is_loop_device(a)) < 0) {
961 ret = resolve_loop_device(a, res_a, sizeof(res_a));
972 /* Resolve b if it is a loop device */
973 if ((ret = is_loop_device(b)) < 0) {
978 ret = resolve_loop_device(b, res_b, sizeof(res_b));
989 return is_same_blk_file(final_a, final_b);
992 /* Checks if a file exists and is a block or regular file*/
993 static int is_existing_blk_or_reg_file(const char* filename)
997 if(stat(filename, &st_buf) < 0) {
1004 return (S_ISBLK(st_buf.st_mode) || S_ISREG(st_buf.st_mode));
1007 /* Checks if a file is used (directly or indirectly via a loop device)
1008 * by a device in fs_devices
1010 static int blk_file_in_dev_list(struct btrfs_fs_devices* fs_devices,
1014 struct list_head *head;
1015 struct list_head *cur;
1016 struct btrfs_device *device;
1018 head = &fs_devices->devices;
1019 list_for_each(cur, head) {
1020 device = list_entry(cur, struct btrfs_device, dev_list);
1022 if((ret = is_same_loop_file(device->name, file)))
1030 * returns 1 if the device was mounted, < 0 on error or 0 if everything
1031 * is safe to continue.
1033 int check_mounted(const char* file)
1038 fd = open(file, O_RDONLY);
1040 fprintf (stderr, "check_mounted(): Could not open %s\n", file);
1044 ret = check_mounted_where(fd, file, NULL, 0, NULL);
1050 int check_mounted_where(int fd, const char *file, char *where, int size,
1051 struct btrfs_fs_devices **fs_dev_ret)
1056 struct btrfs_fs_devices *fs_devices_mnt = NULL;
1060 /* scan the initial device */
1061 ret = btrfs_scan_one_device(fd, file, &fs_devices_mnt,
1062 &total_devs, BTRFS_SUPER_INFO_OFFSET);
1063 is_btrfs = (ret >= 0);
1065 /* scan other devices */
1066 if (is_btrfs && total_devs > 1) {
1067 if ((ret = btrfs_scan_for_fsid(!BTRFS_UPDATE_KERNEL)))
1071 /* iterate over the list of currently mountes filesystems */
1072 if ((f = setmntent ("/proc/self/mounts", "r")) == NULL)
1075 while ((mnt = getmntent (f)) != NULL) {
1077 if(strcmp(mnt->mnt_type, "btrfs") != 0)
1080 ret = blk_file_in_dev_list(fs_devices_mnt, mnt->mnt_fsname);
1082 /* ignore entries in the mount table that are not
1083 associated with a file*/
1084 if((ret = is_existing_blk_or_reg_file(mnt->mnt_fsname)) < 0)
1085 goto out_mntloop_err;
1089 ret = is_same_loop_file(file, mnt->mnt_fsname);
1093 goto out_mntloop_err;
1098 /* Did we find an entry in mnt table? */
1099 if (mnt && size && where) {
1100 strncpy(where, mnt->mnt_dir, size);
1104 *fs_dev_ret = fs_devices_mnt;
1106 ret = (mnt != NULL);
1114 struct pending_dir {
1115 struct list_head list;
1116 char name[PATH_MAX];
1119 void btrfs_register_one_device(char *fname)
1121 struct btrfs_ioctl_vol_args args;
1126 fd = open("/dev/btrfs-control", O_RDONLY);
1128 fprintf(stderr, "failed to open /dev/btrfs-control "
1129 "skipping device registration: %s\n",
1133 strncpy(args.name, fname, BTRFS_PATH_NAME_MAX);
1134 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
1135 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
1138 fprintf(stderr, "ERROR: device scan failed '%s' - %s\n",
1139 fname, strerror(e));
1144 int btrfs_scan_one_dir(char *dirname, int run_ioctl)
1147 struct dirent *dirent;
1148 struct pending_dir *pending;
1154 struct list_head pending_list;
1155 struct btrfs_fs_devices *tmp_devices;
1158 INIT_LIST_HEAD(&pending_list);
1160 pending = malloc(sizeof(*pending));
1163 strcpy(pending->name, dirname);
1166 dirname_len = strlen(pending->name);
1167 fullpath = malloc(PATH_MAX);
1168 dirname = pending->name;
1174 dirp = opendir(dirname);
1176 fprintf(stderr, "Unable to open %s for scanning\n", dirname);
1181 dirent = readdir(dirp);
1184 if (dirent->d_name[0] == '.')
1186 if (dirname_len + strlen(dirent->d_name) + 2 > PATH_MAX) {
1190 snprintf(fullpath, PATH_MAX, "%s/%s", dirname, dirent->d_name);
1191 ret = lstat(fullpath, &st);
1193 fprintf(stderr, "failed to stat %s\n", fullpath);
1196 if (S_ISLNK(st.st_mode))
1198 if (S_ISDIR(st.st_mode)) {
1199 struct pending_dir *next = malloc(sizeof(*next));
1204 strcpy(next->name, fullpath);
1205 list_add_tail(&next->list, &pending_list);
1207 if (!S_ISBLK(st.st_mode)) {
1210 fd = open(fullpath, O_RDONLY);
1212 /* ignore the following errors:
1213 ENXIO (device don't exists)
1214 ENOMEDIUM (No medium found ->
1215 like a cd tray empty)
1217 if(errno != ENXIO && errno != ENOMEDIUM)
1218 fprintf(stderr, "failed to read %s: %s\n",
1219 fullpath, strerror(errno));
1222 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1224 BTRFS_SUPER_INFO_OFFSET);
1225 if (ret == 0 && run_ioctl > 0) {
1226 btrfs_register_one_device(fullpath);
1230 if (!list_empty(&pending_list)) {
1232 pending = list_entry(pending_list.next, struct pending_dir,
1235 list_del(&pending->list);
1244 while (!list_empty(&pending_list)) {
1245 pending = list_entry(pending_list.next, struct pending_dir,
1247 list_del(&pending->list);
1255 int btrfs_scan_for_fsid(int run_ioctls)
1259 ret = scan_for_btrfs(BTRFS_SCAN_PROC, run_ioctls);
1261 ret = scan_for_btrfs(BTRFS_SCAN_DEV, run_ioctls);
1265 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
1268 struct btrfs_super_block *disk_super;
1272 buf = malloc(BTRFS_SUPER_INFO_SIZE);
1277 ret = pread(fd, buf, BTRFS_SUPER_INFO_SIZE, super_offset);
1278 if (ret != BTRFS_SUPER_INFO_SIZE)
1282 disk_super = (struct btrfs_super_block *)buf;
1283 if (btrfs_super_magic(disk_super) != BTRFS_MAGIC)
1286 if (!memcmp(disk_super->fsid, root->fs_info->super_copy->fsid,
1295 static char *size_strs[] = { "", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
1296 int pretty_size_snprintf(u64 size, char *str, size_t str_bytes)
1308 u64 last_size = size;
1310 while(size >= 1024){
1316 if (num_divs >= ARRAY_SIZE(size_strs)) {
1320 fraction = (float)last_size / 1024;
1322 return snprintf(str, str_bytes, "%.2f%s", fraction,
1323 size_strs[num_divs]);
1327 * __strncpy__null - strncpy with null termination
1328 * @dest: the target array
1329 * @src: the source string
1330 * @n: maximum bytes to copy (size of *dest)
1332 * Like strncpy, but ensures destination is null-terminated.
1334 * Copies the string pointed to by src, including the terminating null
1335 * byte ('\0'), to the buffer pointed to by dest, up to a maximum
1336 * of n bytes. Then ensure that dest is null-terminated.
1338 char *__strncpy__null(char *dest, const char *src, size_t n)
1340 strncpy(dest, src, n);
1347 * Checks to make sure that the label matches our requirements.
1349 0 if everything is safe and usable
1350 -1 if the label is too long
1352 static int check_label(const char *input)
1354 int len = strlen(input);
1356 if (len > BTRFS_LABEL_SIZE - 1) {
1357 fprintf(stderr, "ERROR: Label %s is too long (max %d)\n",
1358 input, BTRFS_LABEL_SIZE - 1);
1365 static int set_label_unmounted(const char *dev, const char *label)
1367 struct btrfs_trans_handle *trans;
1368 struct btrfs_root *root;
1371 ret = check_mounted(dev);
1373 fprintf(stderr, "FATAL: error checking %s mount status\n", dev);
1377 fprintf(stderr, "ERROR: dev %s is mounted, use mount point\n",
1382 /* Open the super_block at the default location
1383 * and as read-write.
1385 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1386 if (!root) /* errors are printed by open_ctree() */
1389 trans = btrfs_start_transaction(root, 1);
1390 snprintf(root->fs_info->super_copy->label, BTRFS_LABEL_SIZE, "%s",
1392 btrfs_commit_transaction(trans, root);
1394 /* Now we close it since we are done. */
1399 static int set_label_mounted(const char *mount_path, const char *label)
1403 fd = open(mount_path, O_RDONLY | O_NOATIME);
1405 fprintf(stderr, "ERROR: unable to access '%s'\n", mount_path);
1409 if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) {
1410 fprintf(stderr, "ERROR: unable to set label %s\n",
1420 static int get_label_unmounted(const char *dev, char *label)
1422 struct btrfs_root *root;
1425 ret = check_mounted(dev);
1427 fprintf(stderr, "FATAL: error checking %s mount status\n", dev);
1431 fprintf(stderr, "ERROR: dev %s is mounted, use mount point\n",
1436 /* Open the super_block at the default location
1439 root = open_ctree(dev, 0, 0);
1443 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
1445 /* Now we close it since we are done. */
1451 * If a partition is mounted, try to get the filesystem label via its
1452 * mounted path rather than device. Return the corresponding error
1453 * the user specified the device path.
1455 int get_label_mounted(const char *mount_path, char *labelp)
1457 char label[BTRFS_LABEL_SIZE];
1460 fd = open(mount_path, O_RDONLY | O_NOATIME);
1462 fprintf(stderr, "ERROR: unable to access '%s'\n", mount_path);
1466 memset(label, '\0', sizeof(label));
1467 if (ioctl(fd, BTRFS_IOC_GET_FSLABEL, label) < 0) {
1468 fprintf(stderr, "ERROR: unable get label %s\n", strerror(errno));
1473 strncpy(labelp, label, sizeof(label));
1478 int get_label(const char *btrfs_dev, char *label)
1482 if (is_existing_blk_or_reg_file(btrfs_dev))
1483 ret = get_label_unmounted(btrfs_dev, label);
1485 ret = get_label_mounted(btrfs_dev, label);
1490 int set_label(const char *btrfs_dev, const char *label)
1492 if (check_label(label))
1495 return is_existing_blk_or_reg_file(btrfs_dev) ?
1496 set_label_unmounted(btrfs_dev, label) :
1497 set_label_mounted(btrfs_dev, label);
1500 int btrfs_scan_block_devices(int run_ioctl)
1506 struct btrfs_fs_devices *tmp_devices;
1508 FILE *proc_partitions;
1516 proc_partitions = fopen("/proc/partitions","r");
1517 if (!proc_partitions) {
1518 fprintf(stderr, "Unable to open '/proc/partitions' for scanning\n");
1521 /* skip the header */
1522 for (i = 0; i < 2; i++)
1523 if (!fgets(buf, 1023, proc_partitions)) {
1525 "Unable to read '/proc/partitions' for scanning\n");
1526 fclose(proc_partitions);
1530 strcpy(fullpath,"/dev/");
1531 while(fgets(buf, 1023, proc_partitions)) {
1532 i = sscanf(buf," %*d %*d %*d %99s", fullpath+5);
1535 * multipath and MD devices may register as a btrfs filesystem
1536 * both through the original block device and through
1537 * the special (/dev/mapper or /dev/mdX) entry.
1538 * This scans the special entries last
1540 special = strncmp(fullpath, "/dev/dm-", strlen("/dev/dm-")) == 0;
1542 special = strncmp(fullpath, "/dev/md", strlen("/dev/md")) == 0;
1544 if (scans == 0 && special)
1546 if (scans > 0 && !special)
1549 ret = lstat(fullpath, &st);
1551 fprintf(stderr, "failed to stat %s\n", fullpath);
1554 if (!S_ISBLK(st.st_mode)) {
1558 fd = open(fullpath, O_RDONLY);
1560 if (errno != ENOMEDIUM)
1561 fprintf(stderr, "failed to open %s: %s\n",
1562 fullpath, strerror(errno));
1565 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1567 BTRFS_SUPER_INFO_OFFSET);
1568 if (ret == 0 && run_ioctl > 0) {
1569 btrfs_register_one_device(fullpath);
1574 fclose(proc_partitions);
1583 u64 parse_size(char *s)
1589 for (i = 0; s && s[i] && isdigit(s[i]); i++) ;
1591 fprintf(stderr, "ERROR: size value is empty\n");
1619 fprintf(stderr, "ERROR: Unknown size descriptor "
1624 if (s[i] && s[i+1]) {
1625 fprintf(stderr, "ERROR: Illegal suffix contains "
1626 "character '%c' in wrong position\n",
1630 return strtoull(s, NULL, 10) * mult;
1633 int open_file_or_dir3(const char *fname, DIR **dirstream, int open_flags)
1639 ret = stat(fname, &st);
1643 if (S_ISDIR(st.st_mode)) {
1644 *dirstream = opendir(fname);
1647 fd = dirfd(*dirstream);
1648 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
1649 fd = open(fname, open_flags);
1652 * we set this on purpose, in case the caller output
1653 * strerror(errno) as success
1661 closedir(*dirstream);
1666 int open_file_or_dir(const char *fname, DIR **dirstream)
1668 return open_file_or_dir3(fname, dirstream, O_RDWR);
1671 void close_file_or_dir(int fd, DIR *dirstream)
1674 closedir(dirstream);
1679 int get_device_info(int fd, u64 devid,
1680 struct btrfs_ioctl_dev_info_args *di_args)
1684 di_args->devid = devid;
1685 memset(&di_args->uuid, '\0', sizeof(di_args->uuid));
1687 ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
1688 return ret ? -errno : 0;
1692 * For a given path, fill in the ioctl fs_ and info_ args.
1693 * If the path is a btrfs mountpoint, fill info for all devices.
1694 * If the path is a btrfs device, fill in only that device.
1696 * The path provided must be either on a mounted btrfs fs,
1697 * or be a mounted btrfs device.
1699 * Returns 0 on success, or a negative errno.
1701 int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
1702 struct btrfs_ioctl_dev_info_args **di_ret)
1708 struct btrfs_fs_devices *fs_devices_mnt = NULL;
1709 struct btrfs_ioctl_dev_info_args *di_args;
1710 char mp[BTRFS_PATH_NAME_MAX + 1];
1711 DIR *dirstream = NULL;
1713 memset(fi_args, 0, sizeof(*fi_args));
1715 if (is_block_device(path)) {
1716 struct btrfs_super_block *disk_super;
1717 char buf[BTRFS_SUPER_INFO_SIZE];
1720 /* Ensure it's mounted, then set path to the mountpoint */
1721 fd = open(path, O_RDONLY);
1724 fprintf(stderr, "Couldn't open %s: %s\n",
1725 path, strerror(errno));
1728 ret = check_mounted_where(fd, path, mp, sizeof(mp),
1737 /* Only fill in this one device */
1738 fi_args->num_devices = 1;
1740 disk_super = (struct btrfs_super_block *)buf;
1741 ret = btrfs_read_dev_super(fd, disk_super, BTRFS_SUPER_INFO_OFFSET);
1746 devid = btrfs_stack_device_id(&disk_super->dev_item);
1748 fi_args->max_id = devid;
1751 memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE);
1755 /* at this point path must not be for a block device */
1756 fd = open_file_or_dir(path, &dirstream);
1762 /* fill in fi_args if not just a single device */
1763 if (fi_args->num_devices != 1) {
1764 ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args);
1771 if (!fi_args->num_devices)
1774 di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args));
1780 for (; i <= fi_args->max_id; ++i) {
1781 BUG_ON(ndevs >= fi_args->num_devices);
1782 ret = get_device_info(fd, i, &di_args[ndevs]);
1791 * only when the only dev we wanted to find is not there then
1792 * let any error be returned
1794 if (fi_args->num_devices != 1) {
1800 close_file_or_dir(fd, dirstream);
1804 #define isoctal(c) (((c) & ~7) == '0')
1806 static inline void translate(char *f, char *t)
1808 while (*f != '\0') {
1810 isoctal(f[1]) && isoctal(f[2]) && isoctal(f[3])) {
1811 *t++ = 64*(f[1] & 7) + 8*(f[2] & 7) + (f[3] & 7);
1821 * Checks if the swap device.
1822 * Returns 1 if swap device, < 0 on error or 0 if not swap device.
1824 static int is_swap_device(const char *file)
1835 if (stat(file, &st_buf) < 0)
1837 if (S_ISBLK(st_buf.st_mode))
1838 dev = st_buf.st_rdev;
1839 else if (S_ISREG(st_buf.st_mode)) {
1840 dev = st_buf.st_dev;
1841 ino = st_buf.st_ino;
1845 if ((f = fopen("/proc/swaps", "r")) == NULL)
1848 /* skip the first line */
1849 if (fgets(tmp, sizeof(tmp), f) == NULL)
1852 while (fgets(tmp, sizeof(tmp), f) != NULL) {
1853 if ((cp = strchr(tmp, ' ')) != NULL)
1855 if ((cp = strchr(tmp, '\t')) != NULL)
1857 translate(tmp, buf);
1858 if (stat(buf, &st_buf) != 0)
1860 if (S_ISBLK(st_buf.st_mode)) {
1861 if (dev == st_buf.st_rdev) {
1865 } else if (S_ISREG(st_buf.st_mode)) {
1866 if (dev == st_buf.st_dev && ino == st_buf.st_ino) {
1880 * Check for existing filesystem or partition table on device.
1882 * 1 for existing fs or partition
1883 * 0 for nothing found
1884 * -1 for internal error
1891 blkid_probe pr = NULL;
1895 if (!device || !*device)
1898 ret = -1; /* will reset on success of all setup calls */
1900 pr = blkid_new_probe_from_filename(device);
1904 size = blkid_probe_get_size(pr);
1908 /* nothing to overwrite on a 0-length device */
1914 ret = blkid_probe_enable_partitions(pr, 1);
1918 ret = blkid_do_fullprobe(pr);
1923 * Blkid returns 1 for nothing found and 0 when it finds a signature,
1924 * but we want the exact opposite, so reverse the return value here.
1926 * In addition print some useful diagnostics about what actually is
1934 if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
1936 "%s appears to contain an existing "
1937 "filesystem (%s).\n", device, type);
1938 } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
1940 "%s appears to contain a partition "
1941 "table (%s).\n", device, type);
1944 "%s appears to contain something weird "
1945 "according to blkid\n", device);
1951 blkid_free_probe(pr);
1954 "probe of %s failed, cannot detect "
1955 "existing filesystem.\n", device);
1959 int test_num_disk_vs_raid(u64 metadata_profile, u64 data_profile,
1960 u64 dev_cnt, int mixed, char *estr)
1968 allowed |= BTRFS_BLOCK_GROUP_RAID10;
1970 allowed |= BTRFS_BLOCK_GROUP_RAID6;
1972 allowed |= BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1973 BTRFS_BLOCK_GROUP_RAID5;
1976 allowed |= BTRFS_BLOCK_GROUP_DUP;
1979 if (metadata_profile & ~allowed) {
1980 snprintf(estr, sz, "unable to create FS with metadata "
1981 "profile %llu (have %llu devices)\n",
1982 metadata_profile, dev_cnt);
1985 if (data_profile & ~allowed) {
1986 snprintf(estr, sz, "unable to create FS with data "
1987 "profile %llu (have %llu devices)\n",
1988 metadata_profile, dev_cnt);
1992 if (!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP)) {
1994 "dup for data is allowed only in mixed mode");
2000 /* Check if disk is suitable for btrfs
2002 * 1: something is wrong, estr provides the error
2005 int test_dev_for_mkfs(char *file, int force_overwrite, char *estr)
2011 ret = is_swap_device(file);
2013 snprintf(estr, sz, "error checking %s status: %s\n", file,
2018 snprintf(estr, sz, "%s is a swap device\n", file);
2021 if (!force_overwrite) {
2022 if (check_overwrite(file)) {
2023 snprintf(estr, sz, "Use the -f option to force overwrite.\n");
2027 ret = check_mounted(file);
2029 snprintf(estr, sz, "error checking %s mount status\n",
2034 snprintf(estr, sz, "%s is mounted\n", file);
2037 /* check if the device is busy */
2038 fd = open(file, O_RDWR|O_EXCL);
2040 snprintf(estr, sz, "unable to open %s: %s\n", file,
2044 if (fstat(fd, &st)) {
2045 snprintf(estr, sz, "unable to stat %s: %s\n", file,
2050 if (!S_ISBLK(st.st_mode)) {
2051 fprintf(stderr, "'%s' is not a block device\n", file);
2059 int btrfs_scan_lblkid(int update_kernel)
2064 struct btrfs_fs_devices *tmp_devices;
2065 blkid_dev_iterate iter = NULL;
2066 blkid_dev dev = NULL;
2067 blkid_cache cache = NULL;
2068 char path[PATH_MAX];
2070 if (blkid_get_cache(&cache, 0) < 0) {
2071 printf("ERROR: lblkid cache get failed\n");
2074 blkid_probe_all(cache);
2075 iter = blkid_dev_iterate_begin(cache);
2076 blkid_dev_set_search(iter, "TYPE", "btrfs");
2077 while (blkid_dev_next(iter, &dev) == 0) {
2078 dev = blkid_verify(cache, dev);
2081 /* if we are here its definitely a btrfs disk*/
2082 strncpy(path, blkid_dev_devname(dev), PATH_MAX);
2084 fd = open(path, O_RDONLY);
2086 printf("ERROR: could not open %s\n", path);
2089 ret = btrfs_scan_one_device(fd, path, &tmp_devices,
2090 &num_devices, BTRFS_SUPER_INFO_OFFSET);
2092 printf("ERROR: could not scan %s\n", path);
2099 btrfs_register_one_device(path);
2101 blkid_dev_iterate_end(iter);
2102 blkid_put_cache(cache);
2107 * scans devs for the btrfs
2109 int scan_for_btrfs(int where, int update_kernel)
2114 case BTRFS_SCAN_PROC:
2115 ret = btrfs_scan_block_devices(update_kernel);
2117 case BTRFS_SCAN_DEV:
2118 ret = btrfs_scan_one_dir("/dev", update_kernel);
2120 case BTRFS_SCAN_LBLKID:
2121 ret = btrfs_scan_lblkid(update_kernel);
2127 int is_vol_small(char *file)
2134 fd = open(file, O_RDONLY);
2137 if (fstat(fd, &st) < 0) {
2142 size = btrfs_device_size(fd, &st);
2147 if (size < 1024 * 1024 * 1024) {
2157 * This reads a line from the stdin and only returns non-zero if the
2158 * first whitespace delimited token is a case insensitive match with yes
2161 int ask_user(char *question)
2163 char buf[30] = {0,};
2164 char *saveptr = NULL;
2167 printf("%s [y/N]: ", question);
2169 return fgets(buf, sizeof(buf) - 1, stdin) &&
2170 (answer = strtok_r(buf, " \t\n\r", &saveptr)) &&
2171 (!strcasecmp(answer, "yes") || !strcasecmp(answer, "y"));
2176 * - file or directory return the containing tree root id
2177 * - subvolume return it's own tree id
2178 * - BTRFS_EMPTY_SUBVOL_DIR_OBJECTID (directory with ino == 2) the result is
2179 * undefined and function returns -1
2181 int lookup_ino_rootid(int fd, u64 *rootid)
2183 struct btrfs_ioctl_ino_lookup_args args;
2187 memset(&args, 0, sizeof(args));
2189 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
2191 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
2194 fprintf(stderr, "ERROR: Failed to lookup root id - %s\n",
2199 *rootid = args.treeid;
2204 int find_mount_root(const char *path, char **mount_root)
2211 int longest_matchlen = 0;
2212 char *longest_match = NULL;
2214 fd = open(path, O_RDONLY | O_NOATIME);
2219 mnttab = setmntent("/proc/self/mounts", "r");
2223 while ((ent = getmntent(mnttab))) {
2224 len = strlen(ent->mnt_dir);
2225 if (strncmp(ent->mnt_dir, path, len) == 0) {
2227 if (longest_matchlen < len) {
2228 free(longest_match);
2229 longest_matchlen = len;
2230 longest_match = strdup(ent->mnt_dir);
2236 if (!longest_match) {
2238 "ERROR: Failed to find mount root for path %s.\n",
2244 *mount_root = realpath(longest_match, NULL);
2248 free(longest_match);