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_blocks(int fd, u64 start, u64 len)
58 u64 range[2] = { start, len };
60 if (ioctl(fd, BLKDISCARD, &range) < 0)
65 static u64 reference_root_table[] = {
66 [1] = BTRFS_ROOT_TREE_OBJECTID,
67 [2] = BTRFS_EXTENT_TREE_OBJECTID,
68 [3] = BTRFS_CHUNK_TREE_OBJECTID,
69 [4] = BTRFS_DEV_TREE_OBJECTID,
70 [5] = BTRFS_FS_TREE_OBJECTID,
71 [6] = BTRFS_CSUM_TREE_OBJECTID,
74 int make_btrfs(int fd, const char *device, const char *label,
75 u64 blocks[7], u64 num_bytes, u32 nodesize,
76 u32 leafsize, u32 sectorsize, u32 stripesize, u64 features)
78 struct btrfs_super_block super;
79 struct extent_buffer *buf;
80 struct btrfs_root_item root_item;
81 struct btrfs_disk_key disk_key;
82 struct btrfs_extent_item *extent_item;
83 struct btrfs_inode_item *inode_item;
84 struct btrfs_chunk *chunk;
85 struct btrfs_dev_item *dev_item;
86 struct btrfs_dev_extent *dev_extent;
87 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
97 int skinny_metadata = !!(features &
98 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
100 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
101 first_free &= ~((u64)sectorsize - 1);
103 memset(&super, 0, sizeof(super));
105 num_bytes = (num_bytes / sectorsize) * sectorsize;
106 uuid_generate(super.fsid);
107 uuid_generate(super.dev_item.uuid);
108 uuid_generate(chunk_tree_uuid);
110 btrfs_set_super_bytenr(&super, blocks[0]);
111 btrfs_set_super_num_devices(&super, 1);
112 btrfs_set_super_magic(&super, BTRFS_MAGIC);
113 btrfs_set_super_generation(&super, 1);
114 btrfs_set_super_root(&super, blocks[1]);
115 btrfs_set_super_chunk_root(&super, blocks[3]);
116 btrfs_set_super_total_bytes(&super, num_bytes);
117 btrfs_set_super_bytes_used(&super, 6 * leafsize);
118 btrfs_set_super_sectorsize(&super, sectorsize);
119 btrfs_set_super_leafsize(&super, leafsize);
120 btrfs_set_super_nodesize(&super, nodesize);
121 btrfs_set_super_stripesize(&super, stripesize);
122 btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
123 btrfs_set_super_chunk_root_generation(&super, 1);
124 btrfs_set_super_cache_generation(&super, -1);
125 btrfs_set_super_incompat_flags(&super, features);
127 strncpy(super.label, label, BTRFS_LABEL_SIZE - 1);
129 buf = malloc(sizeof(*buf) + max(sectorsize, leafsize));
131 /* create the tree of root objects */
132 memset(buf->data, 0, leafsize);
134 btrfs_set_header_bytenr(buf, blocks[1]);
135 btrfs_set_header_nritems(buf, 4);
136 btrfs_set_header_generation(buf, 1);
137 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
138 btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
139 write_extent_buffer(buf, super.fsid, btrfs_header_fsid(),
142 write_extent_buffer(buf, chunk_tree_uuid,
143 btrfs_header_chunk_tree_uuid(buf),
146 /* create the items for the root tree */
147 memset(&root_item, 0, sizeof(root_item));
148 inode_item = &root_item.inode;
149 btrfs_set_stack_inode_generation(inode_item, 1);
150 btrfs_set_stack_inode_size(inode_item, 3);
151 btrfs_set_stack_inode_nlink(inode_item, 1);
152 btrfs_set_stack_inode_nbytes(inode_item, leafsize);
153 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
154 btrfs_set_root_refs(&root_item, 1);
155 btrfs_set_root_used(&root_item, leafsize);
156 btrfs_set_root_generation(&root_item, 1);
158 memset(&disk_key, 0, sizeof(disk_key));
159 btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
160 btrfs_set_disk_key_offset(&disk_key, 0);
163 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
164 btrfs_set_root_bytenr(&root_item, blocks[2]);
165 btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID);
166 btrfs_set_item_key(buf, &disk_key, nritems);
167 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
168 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
170 write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
171 nritems), sizeof(root_item));
174 itemoff = itemoff - sizeof(root_item);
175 btrfs_set_root_bytenr(&root_item, blocks[4]);
176 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
177 btrfs_set_item_key(buf, &disk_key, nritems);
178 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
179 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
181 write_extent_buffer(buf, &root_item,
182 btrfs_item_ptr_offset(buf, nritems),
186 itemoff = itemoff - sizeof(root_item);
187 btrfs_set_root_bytenr(&root_item, blocks[5]);
188 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
189 btrfs_set_item_key(buf, &disk_key, nritems);
190 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
191 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
193 write_extent_buffer(buf, &root_item,
194 btrfs_item_ptr_offset(buf, nritems),
198 itemoff = itemoff - sizeof(root_item);
199 btrfs_set_root_bytenr(&root_item, blocks[6]);
200 btrfs_set_disk_key_objectid(&disk_key, BTRFS_CSUM_TREE_OBJECTID);
201 btrfs_set_item_key(buf, &disk_key, nritems);
202 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
203 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
205 write_extent_buffer(buf, &root_item,
206 btrfs_item_ptr_offset(buf, nritems),
211 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
212 ret = pwrite(fd, buf->data, leafsize, blocks[1]);
213 if (ret != leafsize) {
214 ret = (ret < 0 ? -errno : -EIO);
218 /* create the items for the extent tree */
219 memset(buf->data+sizeof(struct btrfs_header), 0,
220 leafsize-sizeof(struct btrfs_header));
222 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize);
223 for (i = 1; i < 7; i++) {
224 item_size = sizeof(struct btrfs_extent_item);
225 if (!skinny_metadata)
226 item_size += sizeof(struct btrfs_tree_block_info);
228 BUG_ON(blocks[i] < first_free);
229 BUG_ON(blocks[i] < blocks[i - 1]);
231 /* create extent item */
232 itemoff -= item_size;
233 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
234 if (skinny_metadata) {
235 btrfs_set_disk_key_type(&disk_key,
236 BTRFS_METADATA_ITEM_KEY);
237 btrfs_set_disk_key_offset(&disk_key, 0);
239 btrfs_set_disk_key_type(&disk_key,
240 BTRFS_EXTENT_ITEM_KEY);
241 btrfs_set_disk_key_offset(&disk_key, leafsize);
243 btrfs_set_item_key(buf, &disk_key, nritems);
244 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
246 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
248 extent_item = btrfs_item_ptr(buf, nritems,
249 struct btrfs_extent_item);
250 btrfs_set_extent_refs(buf, extent_item, 1);
251 btrfs_set_extent_generation(buf, extent_item, 1);
252 btrfs_set_extent_flags(buf, extent_item,
253 BTRFS_EXTENT_FLAG_TREE_BLOCK);
256 /* create extent ref */
257 ref_root = reference_root_table[i];
258 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
259 btrfs_set_disk_key_offset(&disk_key, ref_root);
260 btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
261 btrfs_set_item_key(buf, &disk_key, nritems);
262 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
264 btrfs_set_item_size(buf, btrfs_item_nr(nritems), 0);
267 btrfs_set_header_bytenr(buf, blocks[2]);
268 btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
269 btrfs_set_header_nritems(buf, nritems);
270 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
271 ret = pwrite(fd, buf->data, leafsize, blocks[2]);
272 if (ret != leafsize) {
273 ret = (ret < 0 ? -errno : -EIO);
277 /* create the chunk tree */
278 memset(buf->data+sizeof(struct btrfs_header), 0,
279 leafsize-sizeof(struct btrfs_header));
281 item_size = sizeof(*dev_item);
282 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - item_size;
284 /* first device 1 (there is no device 0) */
285 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
286 btrfs_set_disk_key_offset(&disk_key, 1);
287 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
288 btrfs_set_item_key(buf, &disk_key, nritems);
289 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
290 btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
292 dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
293 btrfs_set_device_id(buf, dev_item, 1);
294 btrfs_set_device_generation(buf, dev_item, 0);
295 btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
296 btrfs_set_device_bytes_used(buf, dev_item,
297 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
298 btrfs_set_device_io_align(buf, dev_item, sectorsize);
299 btrfs_set_device_io_width(buf, dev_item, sectorsize);
300 btrfs_set_device_sector_size(buf, dev_item, sectorsize);
301 btrfs_set_device_type(buf, dev_item, 0);
303 write_extent_buffer(buf, super.dev_item.uuid,
304 (unsigned long)btrfs_device_uuid(dev_item),
306 write_extent_buffer(buf, super.fsid,
307 (unsigned long)btrfs_device_fsid(dev_item),
309 read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
313 item_size = btrfs_chunk_item_size(1);
314 itemoff = itemoff - item_size;
316 /* then we have chunk 0 */
317 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
318 btrfs_set_disk_key_offset(&disk_key, 0);
319 btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
320 btrfs_set_item_key(buf, &disk_key, nritems);
321 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
322 btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
324 chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
325 btrfs_set_chunk_length(buf, chunk, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
326 btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
327 btrfs_set_chunk_stripe_len(buf, chunk, 64 * 1024);
328 btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
329 btrfs_set_chunk_io_align(buf, chunk, sectorsize);
330 btrfs_set_chunk_io_width(buf, chunk, sectorsize);
331 btrfs_set_chunk_sector_size(buf, chunk, sectorsize);
332 btrfs_set_chunk_num_stripes(buf, chunk, 1);
333 btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
334 btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
337 write_extent_buffer(buf, super.dev_item.uuid,
338 (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
341 /* copy the key for the chunk to the system array */
342 ptr = super.sys_chunk_array;
343 array_size = sizeof(disk_key);
345 memcpy(ptr, &disk_key, sizeof(disk_key));
346 ptr += sizeof(disk_key);
348 /* copy the chunk to the system array */
349 read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
350 array_size += item_size;
352 btrfs_set_super_sys_array_size(&super, array_size);
354 btrfs_set_header_bytenr(buf, blocks[3]);
355 btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
356 btrfs_set_header_nritems(buf, nritems);
357 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
358 ret = pwrite(fd, buf->data, leafsize, blocks[3]);
359 if (ret != leafsize) {
360 ret = (ret < 0 ? -errno : -EIO);
364 /* create the device tree */
365 memset(buf->data+sizeof(struct btrfs_header), 0,
366 leafsize-sizeof(struct btrfs_header));
368 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
369 sizeof(struct btrfs_dev_extent);
371 btrfs_set_disk_key_objectid(&disk_key, 1);
372 btrfs_set_disk_key_offset(&disk_key, 0);
373 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
374 btrfs_set_item_key(buf, &disk_key, nritems);
375 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
376 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
377 sizeof(struct btrfs_dev_extent));
378 dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
379 btrfs_set_dev_extent_chunk_tree(buf, dev_extent,
380 BTRFS_CHUNK_TREE_OBJECTID);
381 btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
382 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
383 btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
385 write_extent_buffer(buf, chunk_tree_uuid,
386 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
389 btrfs_set_dev_extent_length(buf, dev_extent,
390 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
393 btrfs_set_header_bytenr(buf, blocks[4]);
394 btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
395 btrfs_set_header_nritems(buf, nritems);
396 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
397 ret = pwrite(fd, buf->data, leafsize, blocks[4]);
398 if (ret != leafsize) {
399 ret = (ret < 0 ? -errno : -EIO);
403 /* create the FS root */
404 memset(buf->data+sizeof(struct btrfs_header), 0,
405 leafsize-sizeof(struct btrfs_header));
406 btrfs_set_header_bytenr(buf, blocks[5]);
407 btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
408 btrfs_set_header_nritems(buf, 0);
409 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
410 ret = pwrite(fd, buf->data, leafsize, blocks[5]);
411 if (ret != leafsize) {
412 ret = (ret < 0 ? -errno : -EIO);
415 /* finally create the csum root */
416 memset(buf->data+sizeof(struct btrfs_header), 0,
417 leafsize-sizeof(struct btrfs_header));
418 btrfs_set_header_bytenr(buf, blocks[6]);
419 btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
420 btrfs_set_header_nritems(buf, 0);
421 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
422 ret = pwrite(fd, buf->data, leafsize, blocks[6]);
423 if (ret != leafsize) {
424 ret = (ret < 0 ? -errno : -EIO);
428 /* and write out the super block */
429 BUG_ON(sizeof(super) > sectorsize);
430 memset(buf->data, 0, sectorsize);
431 memcpy(buf->data, &super, sizeof(super));
432 buf->len = sectorsize;
433 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
434 ret = pwrite(fd, buf->data, sectorsize, blocks[0]);
435 if (ret != sectorsize) {
436 ret = (ret < 0 ? -errno : -EIO);
447 u64 btrfs_device_size(int fd, struct stat *st)
450 if (S_ISREG(st->st_mode)) {
453 if (!S_ISBLK(st->st_mode)) {
456 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
462 static int zero_blocks(int fd, off_t start, size_t len)
464 char *buf = malloc(len);
471 written = pwrite(fd, buf, len, start);
478 static int zero_dev_start(int fd)
481 size_t len = 2 * 1024 * 1024;
484 /* don't overwrite the disk labels on sparc */
488 return zero_blocks(fd, start, len);
491 static int zero_dev_end(int fd, u64 dev_size)
493 size_t len = 2 * 1024 * 1024;
494 off_t start = dev_size - len;
496 return zero_blocks(fd, start, len);
499 int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
500 struct btrfs_root *root, int fd, char *path,
501 u64 block_count, u32 io_width, u32 io_align,
504 struct btrfs_super_block *disk_super;
505 struct btrfs_super_block *super = root->fs_info->super_copy;
506 struct btrfs_device *device;
507 struct btrfs_dev_item *dev_item;
513 device = kzalloc(sizeof(*device), GFP_NOFS);
516 buf = kmalloc(sectorsize, GFP_NOFS);
521 BUG_ON(sizeof(*disk_super) > sectorsize);
522 memset(buf, 0, sectorsize);
524 disk_super = (struct btrfs_super_block *)buf;
525 dev_item = &disk_super->dev_item;
527 uuid_generate(device->uuid);
530 device->io_width = io_width;
531 device->io_align = io_align;
532 device->sector_size = sectorsize;
534 device->writeable = 1;
535 device->total_bytes = block_count;
536 device->bytes_used = 0;
537 device->total_ios = 0;
538 device->dev_root = root->fs_info->dev_root;
540 ret = btrfs_add_device(trans, root, device);
543 total_bytes = btrfs_super_total_bytes(super) + block_count;
544 btrfs_set_super_total_bytes(super, total_bytes);
546 num_devs = btrfs_super_num_devices(super) + 1;
547 btrfs_set_super_num_devices(super, num_devs);
549 memcpy(disk_super, super, sizeof(*disk_super));
551 printf("adding device %s id %llu\n", path,
552 (unsigned long long)device->devid);
554 btrfs_set_super_bytenr(disk_super, BTRFS_SUPER_INFO_OFFSET);
555 btrfs_set_stack_device_id(dev_item, device->devid);
556 btrfs_set_stack_device_type(dev_item, device->type);
557 btrfs_set_stack_device_io_align(dev_item, device->io_align);
558 btrfs_set_stack_device_io_width(dev_item, device->io_width);
559 btrfs_set_stack_device_sector_size(dev_item, device->sector_size);
560 btrfs_set_stack_device_total_bytes(dev_item, device->total_bytes);
561 btrfs_set_stack_device_bytes_used(dev_item, device->bytes_used);
562 memcpy(&dev_item->uuid, device->uuid, BTRFS_UUID_SIZE);
564 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
565 BUG_ON(ret != sectorsize);
568 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
569 device->fs_devices = root->fs_info->fs_devices;
573 int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
574 u64 max_block_count, int *mixed, int discard)
581 ret = fstat(fd, &st);
583 fprintf(stderr, "unable to stat %s\n", file);
587 block_count = btrfs_device_size(fd, &st);
588 if (block_count == 0) {
589 fprintf(stderr, "unable to find %s size\n", file);
593 block_count = min(block_count, max_block_count);
596 if (block_count < 1024 * 1024 * 1024 && !(*mixed)) {
597 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
603 * We intentionally ignore errors from the discard ioctl. It
604 * is not necessary for the mkfs functionality but just an
607 if (discard_blocks(fd, 0, 0) == 0) {
608 fprintf(stderr, "Performing full device TRIM (%s) ...\n",
609 pretty_size(block_count));
610 discard_blocks(fd, 0, block_count);
614 ret = zero_dev_start(fd);
618 for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
619 bytenr = btrfs_sb_offset(i);
620 if (bytenr >= block_count)
622 ret = zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
628 ret = zero_dev_end(fd, block_count);
632 *block_count_ret = block_count;
636 fprintf(stderr, "ERROR: failed to zero device '%s' - %s\n",
637 file, strerror(-ret));
639 } else if (ret > 0) {
640 fprintf(stderr, "ERROR: failed to zero device '%s' - %d\n",
647 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
648 struct btrfs_root *root, u64 objectid)
651 struct btrfs_inode_item inode_item;
652 time_t now = time(NULL);
654 memset(&inode_item, 0, sizeof(inode_item));
655 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
656 btrfs_set_stack_inode_size(&inode_item, 0);
657 btrfs_set_stack_inode_nlink(&inode_item, 1);
658 btrfs_set_stack_inode_nbytes(&inode_item, root->leafsize);
659 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
660 btrfs_set_stack_timespec_sec(&inode_item.atime, now);
661 btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
662 btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
663 btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
664 btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
665 btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
666 btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
667 btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
669 if (root->fs_info->tree_root == root)
670 btrfs_set_super_root_dir(root->fs_info->super_copy, objectid);
672 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
676 ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0);
680 btrfs_set_root_dirid(&root->root_item, objectid);
687 * checks if a path is a block device node
688 * Returns negative errno on failure, otherwise
689 * returns 1 for blockdev, 0 for not-blockdev
691 int is_block_device(const char *path)
695 if (stat(path, &statbuf) < 0)
698 return S_ISBLK(statbuf.st_mode);
702 * check if given path is a mount point
703 * return 1 if yes. 0 if no. -1 for error
705 int is_mount_point(const char *path)
711 f = setmntent("/proc/self/mounts", "r");
715 while ((mnt = getmntent(f)) != NULL) {
716 if (strcmp(mnt->mnt_dir, path))
726 * Find the mount point for a mounted device.
727 * On success, returns 0 with mountpoint in *mp.
728 * On failure, returns -errno (not mounted yields -EINVAL)
729 * Is noisy on failures, expects to be given a mounted device.
731 int get_btrfs_mount(const char *dev, char *mp, size_t mp_size)
736 ret = is_block_device(dev);
739 fprintf(stderr, "%s is not a block device\n", dev);
742 fprintf(stderr, "Could not check %s: %s\n",
743 dev, strerror(-ret));
748 fd = open(dev, O_RDONLY);
751 fprintf(stderr, "Could not open %s: %s\n", dev, strerror(errno));
755 ret = check_mounted_where(fd, dev, mp, mp_size, NULL);
758 } else { /* mounted, all good */
768 * Given a pathname, return a filehandle to:
769 * the original pathname or,
770 * if the pathname is a mounted btrfs device, to its mountpoint.
772 * On error, return -1, errno should be set.
774 int open_path_or_dev_mnt(const char *path, DIR **dirstream)
776 char mp[BTRFS_PATH_NAME_MAX + 1];
779 if (is_block_device(path)) {
782 ret = get_btrfs_mount(path, mp, sizeof(mp));
784 /* not a mounted btrfs dev */
788 fdmnt = open_file_or_dir(mp, dirstream);
790 fdmnt = open_file_or_dir(path, dirstream);
796 /* checks if a device is a loop device */
797 static int is_loop_device (const char* device) {
800 if(stat(device, &statbuf) < 0)
803 return (S_ISBLK(statbuf.st_mode) &&
804 MAJOR(statbuf.st_rdev) == LOOP_MAJOR);
808 /* Takes a loop device path (e.g. /dev/loop0) and returns
809 * the associated file (e.g. /images/my_btrfs.img) */
810 static int resolve_loop_device(const char* loop_dev, char* loop_file,
817 char real_loop_dev[PATH_MAX];
819 if (!realpath(loop_dev, real_loop_dev))
821 snprintf(p, PATH_MAX, "/sys/block/%s/loop/backing_file", strrchr(real_loop_dev, '/'));
822 if (!(f = fopen(p, "r")))
825 snprintf(fmt, 20, "%%%i[^\n]", max_len-1);
826 ret = fscanf(f, fmt, loop_file);
834 /* Checks whether a and b are identical or device
835 * files associated with the same block device
837 static int is_same_blk_file(const char* a, const char* b)
839 struct stat st_buf_a, st_buf_b;
840 char real_a[PATH_MAX];
841 char real_b[PATH_MAX];
843 if(!realpath(a, real_a))
846 if (!realpath(b, real_b))
849 /* Identical path? */
850 if(strcmp(real_a, real_b) == 0)
853 if(stat(a, &st_buf_a) < 0 ||
854 stat(b, &st_buf_b) < 0)
861 /* Same blockdevice? */
862 if(S_ISBLK(st_buf_a.st_mode) &&
863 S_ISBLK(st_buf_b.st_mode) &&
864 st_buf_a.st_rdev == st_buf_b.st_rdev)
870 if (st_buf_a.st_dev == st_buf_b.st_dev &&
871 st_buf_a.st_ino == st_buf_b.st_ino)
879 /* checks if a and b are identical or device
880 * files associated with the same block device or
881 * if one file is a loop device that uses the other
884 static int is_same_loop_file(const char* a, const char* b)
886 char res_a[PATH_MAX];
887 char res_b[PATH_MAX];
888 const char* final_a = NULL;
889 const char* final_b = NULL;
892 /* Resolve a if it is a loop device */
893 if((ret = is_loop_device(a)) < 0) {
898 ret = resolve_loop_device(a, res_a, sizeof(res_a));
909 /* Resolve b if it is a loop device */
910 if ((ret = is_loop_device(b)) < 0) {
915 ret = resolve_loop_device(b, res_b, sizeof(res_b));
926 return is_same_blk_file(final_a, final_b);
929 /* Checks if a file exists and is a block or regular file*/
930 static int is_existing_blk_or_reg_file(const char* filename)
934 if(stat(filename, &st_buf) < 0) {
941 return (S_ISBLK(st_buf.st_mode) || S_ISREG(st_buf.st_mode));
944 /* Checks if a file is used (directly or indirectly via a loop device)
945 * by a device in fs_devices
947 static int blk_file_in_dev_list(struct btrfs_fs_devices* fs_devices,
951 struct list_head *head;
952 struct list_head *cur;
953 struct btrfs_device *device;
955 head = &fs_devices->devices;
956 list_for_each(cur, head) {
957 device = list_entry(cur, struct btrfs_device, dev_list);
959 if((ret = is_same_loop_file(device->name, file)))
967 * returns 1 if the device was mounted, < 0 on error or 0 if everything
968 * is safe to continue.
970 int check_mounted(const char* file)
975 fd = open(file, O_RDONLY);
977 fprintf (stderr, "check_mounted(): Could not open %s\n", file);
981 ret = check_mounted_where(fd, file, NULL, 0, NULL);
987 int check_mounted_where(int fd, const char *file, char *where, int size,
988 struct btrfs_fs_devices **fs_dev_ret)
993 struct btrfs_fs_devices *fs_devices_mnt = NULL;
997 /* scan the initial device */
998 ret = btrfs_scan_one_device(fd, file, &fs_devices_mnt,
999 &total_devs, BTRFS_SUPER_INFO_OFFSET);
1000 is_btrfs = (ret >= 0);
1002 /* scan other devices */
1003 if (is_btrfs && total_devs > 1) {
1004 if ((ret = btrfs_scan_for_fsid(!BTRFS_UPDATE_KERNEL)))
1008 /* iterate over the list of currently mountes filesystems */
1009 if ((f = setmntent ("/proc/self/mounts", "r")) == NULL)
1012 while ((mnt = getmntent (f)) != NULL) {
1014 if(strcmp(mnt->mnt_type, "btrfs") != 0)
1017 ret = blk_file_in_dev_list(fs_devices_mnt, mnt->mnt_fsname);
1019 /* ignore entries in the mount table that are not
1020 associated with a file*/
1021 if((ret = is_existing_blk_or_reg_file(mnt->mnt_fsname)) < 0)
1022 goto out_mntloop_err;
1026 ret = is_same_loop_file(file, mnt->mnt_fsname);
1030 goto out_mntloop_err;
1035 /* Did we find an entry in mnt table? */
1036 if (mnt && size && where) {
1037 strncpy(where, mnt->mnt_dir, size);
1041 *fs_dev_ret = fs_devices_mnt;
1043 ret = (mnt != NULL);
1051 struct pending_dir {
1052 struct list_head list;
1053 char name[PATH_MAX];
1056 void btrfs_register_one_device(char *fname)
1058 struct btrfs_ioctl_vol_args args;
1063 fd = open("/dev/btrfs-control", O_RDONLY);
1065 fprintf(stderr, "failed to open /dev/btrfs-control "
1066 "skipping device registration: %s\n",
1070 strncpy(args.name, fname, BTRFS_PATH_NAME_MAX);
1071 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
1072 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
1075 fprintf(stderr, "ERROR: device scan failed '%s' - %s\n",
1076 fname, strerror(e));
1081 int btrfs_scan_one_dir(char *dirname, int run_ioctl)
1084 struct dirent *dirent;
1085 struct pending_dir *pending;
1091 struct list_head pending_list;
1092 struct btrfs_fs_devices *tmp_devices;
1095 INIT_LIST_HEAD(&pending_list);
1097 pending = malloc(sizeof(*pending));
1100 strcpy(pending->name, dirname);
1103 dirname_len = strlen(pending->name);
1104 fullpath = malloc(PATH_MAX);
1105 dirname = pending->name;
1111 dirp = opendir(dirname);
1113 fprintf(stderr, "Unable to open %s for scanning\n", dirname);
1118 dirent = readdir(dirp);
1121 if (dirent->d_name[0] == '.')
1123 if (dirname_len + strlen(dirent->d_name) + 2 > PATH_MAX) {
1127 snprintf(fullpath, PATH_MAX, "%s/%s", dirname, dirent->d_name);
1128 ret = lstat(fullpath, &st);
1130 fprintf(stderr, "failed to stat %s\n", fullpath);
1133 if (S_ISLNK(st.st_mode))
1135 if (S_ISDIR(st.st_mode)) {
1136 struct pending_dir *next = malloc(sizeof(*next));
1141 strcpy(next->name, fullpath);
1142 list_add_tail(&next->list, &pending_list);
1144 if (!S_ISBLK(st.st_mode)) {
1147 fd = open(fullpath, O_RDONLY);
1149 /* ignore the following errors:
1150 ENXIO (device don't exists)
1151 ENOMEDIUM (No medium found ->
1152 like a cd tray empty)
1154 if(errno != ENXIO && errno != ENOMEDIUM)
1155 fprintf(stderr, "failed to read %s: %s\n",
1156 fullpath, strerror(errno));
1159 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1161 BTRFS_SUPER_INFO_OFFSET);
1162 if (ret == 0 && run_ioctl > 0) {
1163 btrfs_register_one_device(fullpath);
1167 if (!list_empty(&pending_list)) {
1169 pending = list_entry(pending_list.next, struct pending_dir,
1172 list_del(&pending->list);
1181 while (!list_empty(&pending_list)) {
1182 pending = list_entry(pending_list.next, struct pending_dir,
1184 list_del(&pending->list);
1192 int btrfs_scan_for_fsid(int run_ioctls)
1196 ret = scan_for_btrfs(BTRFS_SCAN_PROC, run_ioctls);
1198 ret = scan_for_btrfs(BTRFS_SCAN_DEV, run_ioctls);
1202 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
1205 struct btrfs_super_block *disk_super;
1209 buf = malloc(BTRFS_SUPER_INFO_SIZE);
1214 ret = pread(fd, buf, BTRFS_SUPER_INFO_SIZE, super_offset);
1215 if (ret != BTRFS_SUPER_INFO_SIZE)
1219 disk_super = (struct btrfs_super_block *)buf;
1220 if (btrfs_super_magic(disk_super) != BTRFS_MAGIC)
1223 if (!memcmp(disk_super->fsid, root->fs_info->super_copy->fsid,
1232 static char *size_strs[] = { "", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
1233 int pretty_size_snprintf(u64 size, char *str, size_t str_bytes)
1245 u64 last_size = size;
1247 while(size >= 1024){
1253 if (num_divs >= ARRAY_SIZE(size_strs)) {
1257 fraction = (float)last_size / 1024;
1259 return snprintf(str, str_bytes, "%.2f%s", fraction,
1260 size_strs[num_divs]);
1264 * __strncpy__null - strncpy with null termination
1265 * @dest: the target array
1266 * @src: the source string
1267 * @n: maximum bytes to copy (size of *dest)
1269 * Like strncpy, but ensures destination is null-terminated.
1271 * Copies the string pointed to by src, including the terminating null
1272 * byte ('\0'), to the buffer pointed to by dest, up to a maximum
1273 * of n bytes. Then ensure that dest is null-terminated.
1275 char *__strncpy__null(char *dest, const char *src, size_t n)
1277 strncpy(dest, src, n);
1284 * Checks to make sure that the label matches our requirements.
1286 0 if everything is safe and usable
1287 -1 if the label is too long
1289 static int check_label(const char *input)
1291 int len = strlen(input);
1293 if (len > BTRFS_LABEL_SIZE - 1) {
1294 fprintf(stderr, "ERROR: Label %s is too long (max %d)\n",
1295 input, BTRFS_LABEL_SIZE - 1);
1302 static int set_label_unmounted(const char *dev, const char *label)
1304 struct btrfs_trans_handle *trans;
1305 struct btrfs_root *root;
1308 ret = check_mounted(dev);
1310 fprintf(stderr, "FATAL: error checking %s mount status\n", dev);
1314 fprintf(stderr, "ERROR: dev %s is mounted, use mount point\n",
1319 /* Open the super_block at the default location
1320 * and as read-write.
1322 root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
1323 if (!root) /* errors are printed by open_ctree() */
1326 trans = btrfs_start_transaction(root, 1);
1327 snprintf(root->fs_info->super_copy->label, BTRFS_LABEL_SIZE, "%s",
1329 btrfs_commit_transaction(trans, root);
1331 /* Now we close it since we are done. */
1336 static int set_label_mounted(const char *mount_path, const char *label)
1340 fd = open(mount_path, O_RDONLY | O_NOATIME);
1342 fprintf(stderr, "ERROR: unable access to '%s'\n", mount_path);
1346 if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) {
1347 fprintf(stderr, "ERROR: unable to set label %s\n",
1357 static int get_label_unmounted(const char *dev, char *label)
1359 struct btrfs_root *root;
1362 ret = check_mounted(dev);
1364 fprintf(stderr, "FATAL: error checking %s mount status\n", dev);
1368 fprintf(stderr, "ERROR: dev %s is mounted, use mount point\n",
1373 /* Open the super_block at the default location
1376 root = open_ctree(dev, 0, 0);
1380 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
1382 /* Now we close it since we are done. */
1388 * If a partition is mounted, try to get the filesystem label via its
1389 * mounted path rather than device. Return the corresponding error
1390 * the user specified the device path.
1392 int get_label_mounted(const char *mount_path, char *labelp)
1394 char label[BTRFS_LABEL_SIZE];
1397 fd = open(mount_path, O_RDONLY | O_NOATIME);
1399 fprintf(stderr, "ERROR: unable access to '%s'\n", mount_path);
1403 memset(label, '\0', sizeof(label));
1404 if (ioctl(fd, BTRFS_IOC_GET_FSLABEL, label) < 0) {
1405 fprintf(stderr, "ERROR: unable get label %s\n", strerror(errno));
1410 strncpy(labelp, label, sizeof(label));
1415 int get_label(const char *btrfs_dev, char *label)
1419 if (is_existing_blk_or_reg_file(btrfs_dev))
1420 ret = get_label_unmounted(btrfs_dev, label);
1422 ret = get_label_mounted(btrfs_dev, label);
1427 int set_label(const char *btrfs_dev, const char *label)
1429 if (check_label(label))
1432 return is_existing_blk_or_reg_file(btrfs_dev) ?
1433 set_label_unmounted(btrfs_dev, label) :
1434 set_label_mounted(btrfs_dev, label);
1437 int btrfs_scan_block_devices(int run_ioctl)
1443 struct btrfs_fs_devices *tmp_devices;
1445 FILE *proc_partitions;
1453 proc_partitions = fopen("/proc/partitions","r");
1454 if (!proc_partitions) {
1455 fprintf(stderr, "Unable to open '/proc/partitions' for scanning\n");
1458 /* skip the header */
1459 for (i = 0; i < 2; i++)
1460 if (!fgets(buf, 1023, proc_partitions)) {
1462 "Unable to read '/proc/partitions' for scanning\n");
1463 fclose(proc_partitions);
1467 strcpy(fullpath,"/dev/");
1468 while(fgets(buf, 1023, proc_partitions)) {
1469 i = sscanf(buf," %*d %*d %*d %99s", fullpath+5);
1472 * multipath and MD devices may register as a btrfs filesystem
1473 * both through the original block device and through
1474 * the special (/dev/mapper or /dev/mdX) entry.
1475 * This scans the special entries last
1477 special = strncmp(fullpath, "/dev/dm-", strlen("/dev/dm-")) == 0;
1479 special = strncmp(fullpath, "/dev/md", strlen("/dev/md")) == 0;
1481 if (scans == 0 && special)
1483 if (scans > 0 && !special)
1486 ret = lstat(fullpath, &st);
1488 fprintf(stderr, "failed to stat %s\n", fullpath);
1491 if (!S_ISBLK(st.st_mode)) {
1495 fd = open(fullpath, O_RDONLY);
1497 if (errno != ENOMEDIUM)
1498 fprintf(stderr, "failed to open %s: %s\n",
1499 fullpath, strerror(errno));
1502 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1504 BTRFS_SUPER_INFO_OFFSET);
1505 if (ret == 0 && run_ioctl > 0) {
1506 btrfs_register_one_device(fullpath);
1511 fclose(proc_partitions);
1520 u64 parse_size(char *s)
1526 for (i = 0; s && s[i] && isdigit(s[i]); i++) ;
1528 fprintf(stderr, "ERROR: size value is empty\n");
1556 fprintf(stderr, "ERROR: Unknown size descriptor "
1561 if (s[i] && s[i+1]) {
1562 fprintf(stderr, "ERROR: Illegal suffix contains "
1563 "character '%c' in wrong position\n",
1567 return strtoull(s, NULL, 10) * mult;
1570 int open_file_or_dir(const char *fname, DIR **dirstream)
1576 ret = stat(fname, &st);
1580 if (S_ISDIR(st.st_mode)) {
1581 *dirstream = opendir(fname);
1584 fd = dirfd(*dirstream);
1585 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
1586 fd = open(fname, O_RDWR);
1589 * we set this on purpose, in case the caller output
1590 * strerror(errno) as success
1598 closedir(*dirstream);
1603 void close_file_or_dir(int fd, DIR *dirstream)
1606 closedir(dirstream);
1611 static int get_device_info(int fd, u64 devid,
1612 struct btrfs_ioctl_dev_info_args *di_args)
1616 di_args->devid = devid;
1617 memset(&di_args->uuid, '\0', sizeof(di_args->uuid));
1619 ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
1620 return ret ? -errno : 0;
1624 * For a given path, fill in the ioctl fs_ and info_ args.
1625 * If the path is a btrfs mountpoint, fill info for all devices.
1626 * If the path is a btrfs device, fill in only that device.
1628 * The path provided must be either on a mounted btrfs fs,
1629 * or be a mounted btrfs device.
1631 * Returns 0 on success, or a negative errno.
1633 int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
1634 struct btrfs_ioctl_dev_info_args **di_ret)
1640 struct btrfs_fs_devices *fs_devices_mnt = NULL;
1641 struct btrfs_ioctl_dev_info_args *di_args;
1642 char mp[BTRFS_PATH_NAME_MAX + 1];
1643 DIR *dirstream = NULL;
1645 memset(fi_args, 0, sizeof(*fi_args));
1647 if (is_block_device(path)) {
1648 /* Ensure it's mounted, then set path to the mountpoint */
1649 fd = open(path, O_RDONLY);
1652 fprintf(stderr, "Couldn't open %s: %s\n",
1653 path, strerror(errno));
1656 ret = check_mounted_where(fd, path, mp, sizeof(mp),
1665 /* Only fill in this one device */
1666 fi_args->num_devices = 1;
1667 fi_args->max_id = fs_devices_mnt->latest_devid;
1668 i = fs_devices_mnt->latest_devid;
1669 memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE);
1673 /* at this point path must not be for a block device */
1674 fd = open_file_or_dir(path, &dirstream);
1680 /* fill in fi_args if not just a single device */
1681 if (fi_args->num_devices != 1) {
1682 ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args);
1689 if (!fi_args->num_devices)
1692 di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args));
1698 for (; i <= fi_args->max_id; ++i) {
1699 BUG_ON(ndevs >= fi_args->num_devices);
1700 ret = get_device_info(fd, i, &di_args[ndevs]);
1711 close_file_or_dir(fd, dirstream);
1715 #define isoctal(c) (((c) & ~7) == '0')
1717 static inline void translate(char *f, char *t)
1719 while (*f != '\0') {
1721 isoctal(f[1]) && isoctal(f[2]) && isoctal(f[3])) {
1722 *t++ = 64*(f[1] & 7) + 8*(f[2] & 7) + (f[3] & 7);
1732 * Checks if the swap device.
1733 * Returns 1 if swap device, < 0 on error or 0 if not swap device.
1735 static int is_swap_device(const char *file)
1746 if (stat(file, &st_buf) < 0)
1748 if (S_ISBLK(st_buf.st_mode))
1749 dev = st_buf.st_rdev;
1750 else if (S_ISREG(st_buf.st_mode)) {
1751 dev = st_buf.st_dev;
1752 ino = st_buf.st_ino;
1756 if ((f = fopen("/proc/swaps", "r")) == NULL)
1759 /* skip the first line */
1760 if (fgets(tmp, sizeof(tmp), f) == NULL)
1763 while (fgets(tmp, sizeof(tmp), f) != NULL) {
1764 if ((cp = strchr(tmp, ' ')) != NULL)
1766 if ((cp = strchr(tmp, '\t')) != NULL)
1768 translate(tmp, buf);
1769 if (stat(buf, &st_buf) != 0)
1771 if (S_ISBLK(st_buf.st_mode)) {
1772 if (dev == st_buf.st_rdev) {
1776 } else if (S_ISREG(st_buf.st_mode)) {
1777 if (dev == st_buf.st_dev && ino == st_buf.st_ino) {
1791 * Check for existing filesystem or partition table on device.
1793 * 1 for existing fs or partition
1794 * 0 for nothing found
1795 * -1 for internal error
1802 blkid_probe pr = NULL;
1806 if (!device || !*device)
1809 ret = -1; /* will reset on success of all setup calls */
1811 pr = blkid_new_probe_from_filename(device);
1815 size = blkid_probe_get_size(pr);
1819 /* nothing to overwrite on a 0-length device */
1825 ret = blkid_probe_enable_partitions(pr, 1);
1829 ret = blkid_do_fullprobe(pr);
1834 * Blkid returns 1 for nothing found and 0 when it finds a signature,
1835 * but we want the exact opposite, so reverse the return value here.
1837 * In addition print some useful diagnostics about what actually is
1845 if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
1847 "%s appears to contain an existing "
1848 "filesystem (%s).\n", device, type);
1849 } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
1851 "%s appears to contain a partition "
1852 "table (%s).\n", device, type);
1855 "%s appears to contain something weird "
1856 "according to blkid\n", device);
1862 blkid_free_probe(pr);
1865 "probe of %s failed, cannot detect "
1866 "existing filesystem.\n", device);
1870 int test_num_disk_vs_raid(u64 metadata_profile, u64 data_profile,
1871 u64 dev_cnt, int mixed, char *estr)
1879 allowed |= BTRFS_BLOCK_GROUP_RAID10;
1881 allowed |= BTRFS_BLOCK_GROUP_RAID6;
1883 allowed |= BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1884 BTRFS_BLOCK_GROUP_RAID5;
1887 allowed |= BTRFS_BLOCK_GROUP_DUP;
1890 if (metadata_profile & ~allowed) {
1891 snprintf(estr, sz, "unable to create FS with metadata "
1892 "profile %llu (have %llu devices)\n",
1893 metadata_profile, dev_cnt);
1896 if (data_profile & ~allowed) {
1897 snprintf(estr, sz, "unable to create FS with data "
1898 "profile %llu (have %llu devices)\n",
1899 metadata_profile, dev_cnt);
1903 if (!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP)) {
1905 "dup for data is allowed only in mixed mode");
1911 /* Check if disk is suitable for btrfs
1913 * 1: something is wrong, estr provides the error
1916 int test_dev_for_mkfs(char *file, int force_overwrite, char *estr)
1922 ret = is_swap_device(file);
1924 snprintf(estr, sz, "error checking %s status: %s\n", file,
1929 snprintf(estr, sz, "%s is a swap device\n", file);
1932 if (!force_overwrite) {
1933 if (check_overwrite(file)) {
1934 snprintf(estr, sz, "Use the -f option to force overwrite.\n");
1938 ret = check_mounted(file);
1940 snprintf(estr, sz, "error checking %s mount status\n",
1945 snprintf(estr, sz, "%s is mounted\n", file);
1948 /* check if the device is busy */
1949 fd = open(file, O_RDWR|O_EXCL);
1951 snprintf(estr, sz, "unable to open %s: %s\n", file,
1955 if (fstat(fd, &st)) {
1956 snprintf(estr, sz, "unable to stat %s: %s\n", file,
1961 if (!S_ISBLK(st.st_mode)) {
1962 fprintf(stderr, "'%s' is not a block device\n", file);
1970 int btrfs_scan_lblkid(int update_kernel)
1975 struct btrfs_fs_devices *tmp_devices;
1976 blkid_dev_iterate iter = NULL;
1977 blkid_dev dev = NULL;
1978 blkid_cache cache = NULL;
1979 char path[PATH_MAX];
1981 if (blkid_get_cache(&cache, 0) < 0) {
1982 printf("ERROR: lblkid cache get failed\n");
1985 blkid_probe_all(cache);
1986 iter = blkid_dev_iterate_begin(cache);
1987 blkid_dev_set_search(iter, "TYPE", "btrfs");
1988 while (blkid_dev_next(iter, &dev) == 0) {
1989 dev = blkid_verify(cache, dev);
1992 /* if we are here its definitely a btrfs disk*/
1993 strncpy(path, blkid_dev_devname(dev), PATH_MAX);
1995 fd = open(path, O_RDONLY);
1997 printf("ERROR: could not open %s\n", path);
2000 ret = btrfs_scan_one_device(fd, path, &tmp_devices,
2001 &num_devices, BTRFS_SUPER_INFO_OFFSET);
2003 printf("ERROR: could not scan %s\n", path);
2010 btrfs_register_one_device(path);
2012 blkid_dev_iterate_end(iter);
2017 * scans devs for the btrfs
2019 int scan_for_btrfs(int where, int update_kernel)
2024 case BTRFS_SCAN_PROC:
2025 ret = btrfs_scan_block_devices(update_kernel);
2027 case BTRFS_SCAN_DEV:
2028 ret = btrfs_scan_one_dir("/dev", update_kernel);
2030 case BTRFS_SCAN_LBLKID:
2031 ret = btrfs_scan_lblkid(update_kernel);
2037 int is_vol_small(char *file)
2044 fd = open(file, O_RDONLY);
2047 if (fstat(fd, &st) < 0) {
2052 size = btrfs_device_size(fd, &st);
2057 if (size < 1024 * 1024 * 1024) {
2067 * This reads a line from the stdin and only returns non-zero if the
2068 * first whitespace delimited token is a case insensitive match with yes
2071 int ask_user(char *question)
2073 char buf[30] = {0,};
2074 char *saveptr = NULL;
2077 printf("%s [y/N]: ", question);
2079 return fgets(buf, sizeof(buf) - 1, stdin) &&
2080 (answer = strtok_r(buf, " \t\n\r", &saveptr)) &&
2081 (!strcasecmp(answer, "yes") || !strcasecmp(answer, "y"));
2086 * - file or directory return the containing tree root id
2087 * - subvolume return it's own tree id
2088 * - BTRFS_EMPTY_SUBVOL_DIR_OBJECTID (directory with ino == 2) the result is
2089 * undefined and function returns -1
2091 int lookup_ino_rootid(int fd, u64 *rootid)
2093 struct btrfs_ioctl_ino_lookup_args args;
2097 memset(&args, 0, sizeof(args));
2099 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
2101 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
2104 fprintf(stderr, "ERROR: Failed to lookup root id - %s\n",
2109 *rootid = args.treeid;