2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #define _XOPEN_SOURCE 600
25 #include <sys/ioctl.h>
26 #include <sys/mount.h>
28 #include <sys/types.h>
30 #include <uuid/uuid.h>
36 #include <linux/loop.h>
37 #include <linux/major.h>
38 #include <linux/kdev_t.h>
40 #include "kerncompat.h"
41 #include "radix-tree.h"
44 #include "transaction.h"
51 #define BLKGETSIZE64 0
52 static inline int ioctl(int fd, int define, u64 *size) { return 0; }
56 #define BLKDISCARD _IO(0x12,119)
60 discard_blocks(int fd, u64 start, u64 len)
62 u64 range[2] = { start, len };
64 if (ioctl(fd, BLKDISCARD, &range) < 0)
69 static u64 reference_root_table[] = {
70 [1] = BTRFS_ROOT_TREE_OBJECTID,
71 [2] = BTRFS_EXTENT_TREE_OBJECTID,
72 [3] = BTRFS_CHUNK_TREE_OBJECTID,
73 [4] = BTRFS_DEV_TREE_OBJECTID,
74 [5] = BTRFS_FS_TREE_OBJECTID,
75 [6] = BTRFS_CSUM_TREE_OBJECTID,
78 int make_btrfs(int fd, const char *device, const char *label,
79 u64 blocks[7], u64 num_bytes, u32 nodesize,
80 u32 leafsize, u32 sectorsize, u32 stripesize)
82 struct btrfs_super_block super;
83 struct extent_buffer *buf;
84 struct btrfs_root_item root_item;
85 struct btrfs_disk_key disk_key;
86 struct btrfs_extent_item *extent_item;
87 struct btrfs_inode_item *inode_item;
88 struct btrfs_chunk *chunk;
89 struct btrfs_dev_item *dev_item;
90 struct btrfs_dev_extent *dev_extent;
91 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
102 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
103 first_free &= ~((u64)sectorsize - 1);
105 memset(&super, 0, sizeof(super));
107 num_bytes = (num_bytes / sectorsize) * sectorsize;
108 uuid_generate(super.fsid);
109 uuid_generate(super.dev_item.uuid);
110 uuid_generate(chunk_tree_uuid);
112 btrfs_set_super_bytenr(&super, blocks[0]);
113 btrfs_set_super_num_devices(&super, 1);
114 strncpy((char *)&super.magic, BTRFS_MAGIC, sizeof(super.magic));
115 btrfs_set_super_generation(&super, 1);
116 btrfs_set_super_root(&super, blocks[1]);
117 btrfs_set_super_chunk_root(&super, blocks[3]);
118 btrfs_set_super_total_bytes(&super, num_bytes);
119 btrfs_set_super_bytes_used(&super, 6 * leafsize);
120 btrfs_set_super_sectorsize(&super, sectorsize);
121 btrfs_set_super_leafsize(&super, leafsize);
122 btrfs_set_super_nodesize(&super, nodesize);
123 btrfs_set_super_stripesize(&super, stripesize);
124 btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
125 btrfs_set_super_chunk_root_generation(&super, 1);
126 btrfs_set_super_cache_generation(&super, -1);
128 strncpy(super.label, label, BTRFS_LABEL_SIZE - 1);
130 buf = malloc(sizeof(*buf) + max(sectorsize, leafsize));
132 /* create the tree of root objects */
133 memset(buf->data, 0, leafsize);
135 btrfs_set_header_bytenr(buf, blocks[1]);
136 btrfs_set_header_nritems(buf, 4);
137 btrfs_set_header_generation(buf, 1);
138 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
139 btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
140 write_extent_buffer(buf, super.fsid, (unsigned long)
141 btrfs_header_fsid(buf), BTRFS_FSID_SIZE);
143 write_extent_buffer(buf, chunk_tree_uuid, (unsigned long)
144 btrfs_header_chunk_tree_uuid(buf),
147 /* create the items for the root tree */
148 memset(&root_item, 0, sizeof(root_item));
149 inode_item = &root_item.inode;
150 btrfs_set_stack_inode_generation(inode_item, 1);
151 btrfs_set_stack_inode_size(inode_item, 3);
152 btrfs_set_stack_inode_nlink(inode_item, 1);
153 btrfs_set_stack_inode_nbytes(inode_item, leafsize);
154 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
155 btrfs_set_root_refs(&root_item, 1);
156 btrfs_set_root_used(&root_item, leafsize);
157 btrfs_set_root_generation(&root_item, 1);
159 memset(&disk_key, 0, sizeof(disk_key));
160 btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
161 btrfs_set_disk_key_offset(&disk_key, 0);
164 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
165 btrfs_set_root_bytenr(&root_item, blocks[2]);
166 btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID);
167 btrfs_set_item_key(buf, &disk_key, nritems);
168 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
169 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
171 write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
172 nritems), sizeof(root_item));
175 itemoff = itemoff - sizeof(root_item);
176 btrfs_set_root_bytenr(&root_item, blocks[4]);
177 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
178 btrfs_set_item_key(buf, &disk_key, nritems);
179 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
180 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
182 write_extent_buffer(buf, &root_item,
183 btrfs_item_ptr_offset(buf, nritems),
187 itemoff = itemoff - sizeof(root_item);
188 btrfs_set_root_bytenr(&root_item, blocks[5]);
189 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
190 btrfs_set_item_key(buf, &disk_key, nritems);
191 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
192 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
194 write_extent_buffer(buf, &root_item,
195 btrfs_item_ptr_offset(buf, nritems),
199 itemoff = itemoff - sizeof(root_item);
200 btrfs_set_root_bytenr(&root_item, blocks[6]);
201 btrfs_set_disk_key_objectid(&disk_key, BTRFS_CSUM_TREE_OBJECTID);
202 btrfs_set_item_key(buf, &disk_key, nritems);
203 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
204 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
206 write_extent_buffer(buf, &root_item,
207 btrfs_item_ptr_offset(buf, nritems),
212 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
213 ret = pwrite(fd, buf->data, leafsize, blocks[1]);
214 BUG_ON(ret != leafsize);
216 /* create the items for the extent tree */
217 memset(buf->data+sizeof(struct btrfs_header), 0,
218 leafsize-sizeof(struct btrfs_header));
220 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize);
221 for (i = 1; i < 7; i++) {
222 BUG_ON(blocks[i] < first_free);
223 BUG_ON(blocks[i] < blocks[i - 1]);
225 /* create extent item */
226 itemoff -= sizeof(struct btrfs_extent_item) +
227 sizeof(struct btrfs_tree_block_info);
228 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
229 btrfs_set_disk_key_offset(&disk_key, leafsize);
230 btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_ITEM_KEY);
231 btrfs_set_item_key(buf, &disk_key, nritems);
232 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
234 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
235 sizeof(struct btrfs_extent_item) +
236 sizeof(struct btrfs_tree_block_info));
237 extent_item = btrfs_item_ptr(buf, nritems,
238 struct btrfs_extent_item);
239 btrfs_set_extent_refs(buf, extent_item, 1);
240 btrfs_set_extent_generation(buf, extent_item, 1);
241 btrfs_set_extent_flags(buf, extent_item,
242 BTRFS_EXTENT_FLAG_TREE_BLOCK);
245 /* create extent ref */
246 ref_root = reference_root_table[i];
247 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
248 btrfs_set_disk_key_offset(&disk_key, ref_root);
249 btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
250 btrfs_set_item_key(buf, &disk_key, nritems);
251 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
253 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), 0);
256 btrfs_set_header_bytenr(buf, blocks[2]);
257 btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
258 btrfs_set_header_nritems(buf, nritems);
259 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
260 ret = pwrite(fd, buf->data, leafsize, blocks[2]);
261 BUG_ON(ret != leafsize);
263 /* create the chunk tree */
264 memset(buf->data+sizeof(struct btrfs_header), 0,
265 leafsize-sizeof(struct btrfs_header));
267 item_size = sizeof(*dev_item);
268 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - item_size;
270 /* first device 1 (there is no device 0) */
271 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
272 btrfs_set_disk_key_offset(&disk_key, 1);
273 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
274 btrfs_set_item_key(buf, &disk_key, nritems);
275 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
276 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), item_size);
278 dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
279 btrfs_set_device_id(buf, dev_item, 1);
280 btrfs_set_device_generation(buf, dev_item, 0);
281 btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
282 btrfs_set_device_bytes_used(buf, dev_item,
283 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
284 btrfs_set_device_io_align(buf, dev_item, sectorsize);
285 btrfs_set_device_io_width(buf, dev_item, sectorsize);
286 btrfs_set_device_sector_size(buf, dev_item, sectorsize);
287 btrfs_set_device_type(buf, dev_item, 0);
289 write_extent_buffer(buf, super.dev_item.uuid,
290 (unsigned long)btrfs_device_uuid(dev_item),
292 write_extent_buffer(buf, super.fsid,
293 (unsigned long)btrfs_device_fsid(dev_item),
295 read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
299 item_size = btrfs_chunk_item_size(1);
300 itemoff = itemoff - item_size;
302 /* then we have chunk 0 */
303 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
304 btrfs_set_disk_key_offset(&disk_key, 0);
305 btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
306 btrfs_set_item_key(buf, &disk_key, nritems);
307 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
308 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), item_size);
310 chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
311 btrfs_set_chunk_length(buf, chunk, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
312 btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
313 btrfs_set_chunk_stripe_len(buf, chunk, 64 * 1024);
314 btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
315 btrfs_set_chunk_io_align(buf, chunk, sectorsize);
316 btrfs_set_chunk_io_width(buf, chunk, sectorsize);
317 btrfs_set_chunk_sector_size(buf, chunk, sectorsize);
318 btrfs_set_chunk_num_stripes(buf, chunk, 1);
319 btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
320 btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
323 write_extent_buffer(buf, super.dev_item.uuid,
324 (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
327 /* copy the key for the chunk to the system array */
328 ptr = super.sys_chunk_array;
329 array_size = sizeof(disk_key);
331 memcpy(ptr, &disk_key, sizeof(disk_key));
332 ptr += sizeof(disk_key);
334 /* copy the chunk to the system array */
335 read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
336 array_size += item_size;
338 btrfs_set_super_sys_array_size(&super, array_size);
340 btrfs_set_header_bytenr(buf, blocks[3]);
341 btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
342 btrfs_set_header_nritems(buf, nritems);
343 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
344 ret = pwrite(fd, buf->data, leafsize, blocks[3]);
346 /* create the device tree */
347 memset(buf->data+sizeof(struct btrfs_header), 0,
348 leafsize-sizeof(struct btrfs_header));
350 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
351 sizeof(struct btrfs_dev_extent);
353 btrfs_set_disk_key_objectid(&disk_key, 1);
354 btrfs_set_disk_key_offset(&disk_key, 0);
355 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
356 btrfs_set_item_key(buf, &disk_key, nritems);
357 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
358 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
359 sizeof(struct btrfs_dev_extent));
360 dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
361 btrfs_set_dev_extent_chunk_tree(buf, dev_extent,
362 BTRFS_CHUNK_TREE_OBJECTID);
363 btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
364 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
365 btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
367 write_extent_buffer(buf, chunk_tree_uuid,
368 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
371 btrfs_set_dev_extent_length(buf, dev_extent,
372 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
375 btrfs_set_header_bytenr(buf, blocks[4]);
376 btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
377 btrfs_set_header_nritems(buf, nritems);
378 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
379 ret = pwrite(fd, buf->data, leafsize, blocks[4]);
381 /* create the FS root */
382 memset(buf->data+sizeof(struct btrfs_header), 0,
383 leafsize-sizeof(struct btrfs_header));
384 btrfs_set_header_bytenr(buf, blocks[5]);
385 btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
386 btrfs_set_header_nritems(buf, 0);
387 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
388 ret = pwrite(fd, buf->data, leafsize, blocks[5]);
389 BUG_ON(ret != leafsize);
391 /* finally create the csum root */
392 memset(buf->data+sizeof(struct btrfs_header), 0,
393 leafsize-sizeof(struct btrfs_header));
394 btrfs_set_header_bytenr(buf, blocks[6]);
395 btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
396 btrfs_set_header_nritems(buf, 0);
397 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
398 ret = pwrite(fd, buf->data, leafsize, blocks[6]);
399 BUG_ON(ret != leafsize);
401 /* and write out the super block */
402 BUG_ON(sizeof(super) > sectorsize);
403 memset(buf->data, 0, sectorsize);
404 memcpy(buf->data, &super, sizeof(super));
405 buf->len = sectorsize;
406 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
407 ret = pwrite(fd, buf->data, sectorsize, blocks[0]);
408 BUG_ON(ret != sectorsize);
415 static u64 device_size(int fd, struct stat *st)
418 if (S_ISREG(st->st_mode)) {
421 if (!S_ISBLK(st->st_mode)) {
424 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
430 static int zero_blocks(int fd, off_t start, size_t len)
432 char *buf = malloc(len);
439 written = pwrite(fd, buf, len, start);
446 static int zero_dev_start(int fd)
449 size_t len = 2 * 1024 * 1024;
452 /* don't overwrite the disk labels on sparc */
456 return zero_blocks(fd, start, len);
459 static int zero_dev_end(int fd, u64 dev_size)
461 size_t len = 2 * 1024 * 1024;
462 off_t start = dev_size - len;
464 return zero_blocks(fd, start, len);
467 int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
468 struct btrfs_root *root, int fd, char *path,
469 u64 block_count, u32 io_width, u32 io_align,
472 struct btrfs_super_block *disk_super;
473 struct btrfs_super_block *super = &root->fs_info->super_copy;
474 struct btrfs_device *device;
475 struct btrfs_dev_item *dev_item;
481 device = kmalloc(sizeof(*device), GFP_NOFS);
484 buf = kmalloc(sectorsize, GFP_NOFS);
489 BUG_ON(sizeof(*disk_super) > sectorsize);
490 memset(buf, 0, sectorsize);
492 disk_super = (struct btrfs_super_block *)buf;
493 dev_item = &disk_super->dev_item;
495 uuid_generate(device->uuid);
498 device->io_width = io_width;
499 device->io_align = io_align;
500 device->sector_size = sectorsize;
502 device->writeable = 1;
503 device->total_bytes = block_count;
504 device->bytes_used = 0;
505 device->total_ios = 0;
506 device->dev_root = root->fs_info->dev_root;
508 ret = btrfs_add_device(trans, root, device);
511 total_bytes = btrfs_super_total_bytes(super) + block_count;
512 btrfs_set_super_total_bytes(super, total_bytes);
514 num_devs = btrfs_super_num_devices(super) + 1;
515 btrfs_set_super_num_devices(super, num_devs);
517 memcpy(disk_super, super, sizeof(*disk_super));
519 printf("adding device %s id %llu\n", path,
520 (unsigned long long)device->devid);
522 btrfs_set_super_bytenr(disk_super, BTRFS_SUPER_INFO_OFFSET);
523 btrfs_set_stack_device_id(dev_item, device->devid);
524 btrfs_set_stack_device_type(dev_item, device->type);
525 btrfs_set_stack_device_io_align(dev_item, device->io_align);
526 btrfs_set_stack_device_io_width(dev_item, device->io_width);
527 btrfs_set_stack_device_sector_size(dev_item, device->sector_size);
528 btrfs_set_stack_device_total_bytes(dev_item, device->total_bytes);
529 btrfs_set_stack_device_bytes_used(dev_item, device->bytes_used);
530 memcpy(&dev_item->uuid, device->uuid, BTRFS_UUID_SIZE);
532 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
533 BUG_ON(ret != sectorsize);
536 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
537 device->fs_devices = root->fs_info->fs_devices;
541 int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
542 u64 max_block_count, int *mixed, int nodiscard)
549 ret = fstat(fd, &st);
551 fprintf(stderr, "unable to stat %s\n", file);
555 block_count = device_size(fd, &st);
556 if (block_count == 0) {
557 fprintf(stderr, "unable to find %s size\n", file);
561 block_count = min(block_count, max_block_count);
564 if (block_count < 1024 * 1024 * 1024 && !(*mixed)) {
565 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
571 * We intentionally ignore errors from the discard ioctl. It is
572 * not necessary for the mkfs functionality but just an optimization.
574 discard_blocks(fd, 0, block_count);
577 ret = zero_dev_start(fd);
579 fprintf(stderr, "failed to zero device start %d\n", ret);
583 for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
584 bytenr = btrfs_sb_offset(i);
585 if (bytenr >= block_count)
587 zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
591 ret = zero_dev_end(fd, block_count);
593 fprintf(stderr, "failed to zero device end %d\n", ret);
597 *block_count_ret = block_count;
601 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
602 struct btrfs_root *root, u64 objectid)
605 struct btrfs_inode_item inode_item;
606 time_t now = time(NULL);
608 memset(&inode_item, 0, sizeof(inode_item));
609 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
610 btrfs_set_stack_inode_size(&inode_item, 0);
611 btrfs_set_stack_inode_nlink(&inode_item, 1);
612 btrfs_set_stack_inode_nbytes(&inode_item, root->leafsize);
613 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
614 btrfs_set_stack_timespec_sec(&inode_item.atime, now);
615 btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
616 btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
617 btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
618 btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
619 btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
620 btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
621 btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
623 if (root->fs_info->tree_root == root)
624 btrfs_set_super_root_dir(&root->fs_info->super_copy, objectid);
626 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
630 ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0);
634 btrfs_set_root_dirid(&root->root_item, objectid);
640 /* checks if a device is a loop device */
641 int is_loop_device (const char* device) {
644 if(stat(device, &statbuf) < 0)
647 return (S_ISBLK(statbuf.st_mode) &&
648 MAJOR(statbuf.st_rdev) == LOOP_MAJOR);
652 /* Takes a loop device path (e.g. /dev/loop0) and returns
653 * the associated file (e.g. /images/my_btrfs.img) */
654 int resolve_loop_device(const char* loop_dev, char* loop_file, int max_len)
660 char real_loop_dev[PATH_MAX];
662 if (!realpath(loop_dev, real_loop_dev))
664 snprintf(p, PATH_MAX, "/sys/block/%s/loop/backing_file", strrchr(real_loop_dev, '/'));
665 if (!(f = fopen(p, "r")))
668 snprintf(fmt, 20, "%%%i[^\n]", max_len-1);
669 ret = fscanf(f, fmt, loop_file);
677 /* Checks whether a and b are identical or device
678 * files associated with the same block device
680 int is_same_blk_file(const char* a, const char* b)
682 struct stat st_buf_a, st_buf_b;
683 char real_a[PATH_MAX];
684 char real_b[PATH_MAX];
686 if(!realpath(a, real_a) ||
687 !realpath(b, real_b))
692 /* Identical path? */
693 if(strcmp(real_a, real_b) == 0)
696 if(stat(a, &st_buf_a) < 0 ||
697 stat(b, &st_buf_b) < 0)
704 /* Same blockdevice? */
705 if(S_ISBLK(st_buf_a.st_mode) &&
706 S_ISBLK(st_buf_b.st_mode) &&
707 st_buf_a.st_rdev == st_buf_b.st_rdev)
713 if (st_buf_a.st_dev == st_buf_b.st_dev &&
714 st_buf_a.st_ino == st_buf_b.st_ino)
722 /* checks if a and b are identical or device
723 * files associated with the same block device or
724 * if one file is a loop device that uses the other
727 int is_same_loop_file(const char* a, const char* b)
729 char res_a[PATH_MAX];
730 char res_b[PATH_MAX];
735 /* Resolve a if it is a loop device */
736 if((ret = is_loop_device(a)) < 0) {
741 if ((ret = resolve_loop_device(a, res_a, sizeof(res_a))) < 0)
749 /* Resolve b if it is a loop device */
750 if ((ret = is_loop_device(b)) < 0) {
755 if((ret = resolve_loop_device(b, res_b, sizeof(res_b))) < 0)
763 return is_same_blk_file(final_a, final_b);
766 /* Checks if a file exists and is a block or regular file*/
767 int is_existing_blk_or_reg_file(const char* filename)
771 if(stat(filename, &st_buf) < 0) {
778 return (S_ISBLK(st_buf.st_mode) || S_ISREG(st_buf.st_mode));
781 /* Checks if a file is used (directly or indirectly via a loop device)
782 * by a device in fs_devices
784 int blk_file_in_dev_list(struct btrfs_fs_devices* fs_devices, const char* file)
787 struct list_head *head;
788 struct list_head *cur;
789 struct btrfs_device *device;
791 head = &fs_devices->devices;
792 list_for_each(cur, head) {
793 device = list_entry(cur, struct btrfs_device, dev_list);
795 if((ret = is_same_loop_file(device->name, file)))
803 * returns 1 if the device was mounted, < 0 on error or 0 if everything
804 * is safe to continue.
806 int check_mounted(const char* file)
811 fd = open(file, O_RDONLY);
813 fprintf (stderr, "check_mounted(): Could not open %s\n", file);
817 ret = check_mounted_where(fd, file, NULL, 0, NULL);
823 int check_mounted_where(int fd, const char *file, char *where, int size,
824 struct btrfs_fs_devices **fs_dev_ret)
829 struct btrfs_fs_devices *fs_devices_mnt = NULL;
833 /* scan the initial device */
834 ret = btrfs_scan_one_device(fd, file, &fs_devices_mnt,
835 &total_devs, BTRFS_SUPER_INFO_OFFSET);
836 is_btrfs = (ret >= 0);
838 /* scan other devices */
839 if (is_btrfs && total_devs > 1) {
840 if((ret = btrfs_scan_for_fsid(fs_devices_mnt, total_devs, 1)))
844 /* iterate over the list of currently mountes filesystems */
845 if ((f = setmntent ("/proc/mounts", "r")) == NULL)
848 while ((mnt = getmntent (f)) != NULL) {
850 if(strcmp(mnt->mnt_type, "btrfs") != 0)
853 ret = blk_file_in_dev_list(fs_devices_mnt, mnt->mnt_fsname);
855 /* ignore entries in the mount table that are not
856 associated with a file*/
857 if((ret = is_existing_blk_or_reg_file(mnt->mnt_fsname)) < 0)
858 goto out_mntloop_err;
862 ret = is_same_loop_file(file, mnt->mnt_fsname);
866 goto out_mntloop_err;
871 /* Did we find an entry in mnt table? */
872 if (mnt && size && where) {
873 strncpy(where, mnt->mnt_dir, size);
877 *fs_dev_ret = fs_devices_mnt;
887 /* Gets the mount point of btrfs filesystem that is using the specified device.
888 * Returns 0 is everything is good, <0 if we have an error.
889 * TODO: Fix this fucntion and check_mounted to work with multiple drive BTRFS
892 int get_mountpt(char *dev, char *mntpt, size_t size)
898 f = setmntent("/proc/mounts", "r");
902 while ((mnt = getmntent(f)) != NULL )
904 if (strcmp(dev, mnt->mnt_fsname) == 0)
906 strncpy(mntpt, mnt->mnt_dir, size);
915 /* We didn't find an entry so lets report an error */
923 struct list_head list;
927 void btrfs_register_one_device(char *fname)
929 struct btrfs_ioctl_vol_args args;
934 fd = open("/dev/btrfs-control", O_RDONLY);
936 fprintf(stderr, "failed to open /dev/btrfs-control "
937 "skipping device registration\n");
940 strncpy(args.name, fname, BTRFS_PATH_NAME_MAX);
941 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
942 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
945 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
951 int btrfs_scan_one_dir(char *dirname, int run_ioctl)
954 struct dirent *dirent;
955 struct pending_dir *pending;
962 struct list_head pending_list;
963 struct btrfs_fs_devices *tmp_devices;
966 INIT_LIST_HEAD(&pending_list);
968 pending = malloc(sizeof(*pending));
971 strcpy(pending->name, dirname);
974 dirname_len = strlen(pending->name);
976 fullpath = malloc(pathlen);
977 dirname = pending->name;
983 dirp = opendir(dirname);
985 fprintf(stderr, "Unable to open %s for scanning\n", dirname);
990 dirent = readdir(dirp);
993 if (dirent->d_name[0] == '.')
995 if (dirname_len + strlen(dirent->d_name) + 2 > pathlen) {
999 snprintf(fullpath, pathlen, "%s/%s", dirname, dirent->d_name);
1000 ret = lstat(fullpath, &st);
1002 fprintf(stderr, "failed to stat %s\n", fullpath);
1005 if (S_ISLNK(st.st_mode))
1007 if (S_ISDIR(st.st_mode)) {
1008 struct pending_dir *next = malloc(sizeof(*next));
1013 strcpy(next->name, fullpath);
1014 list_add_tail(&next->list, &pending_list);
1016 if (!S_ISBLK(st.st_mode)) {
1019 fd = open(fullpath, O_RDONLY);
1021 /* ignore the following errors:
1022 ENXIO (device don't exists)
1023 ENOMEDIUM (No medium found ->
1024 like a cd tray empty)
1026 if(errno != ENXIO && errno != ENOMEDIUM)
1027 fprintf(stderr, "failed to read %s: %s\n",
1028 fullpath, strerror(errno));
1031 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1033 BTRFS_SUPER_INFO_OFFSET);
1034 if (ret == 0 && run_ioctl > 0) {
1035 btrfs_register_one_device(fullpath);
1039 if (!list_empty(&pending_list)) {
1041 pending = list_entry(pending_list.next, struct pending_dir,
1044 list_del(&pending->list);
1058 int btrfs_scan_for_fsid(struct btrfs_fs_devices *fs_devices, u64 total_devs,
1063 ret = btrfs_scan_block_devices(run_ioctls);
1065 ret = btrfs_scan_one_dir("/dev", run_ioctls);
1069 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
1072 struct btrfs_super_block *disk_super;
1076 buf = malloc(BTRFS_SUPER_INFO_SIZE);
1081 ret = pread(fd, buf, BTRFS_SUPER_INFO_SIZE, super_offset);
1082 if (ret != BTRFS_SUPER_INFO_SIZE)
1086 disk_super = (struct btrfs_super_block *)buf;
1087 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1088 sizeof(disk_super->magic)))
1091 if (!memcmp(disk_super->fsid, root->fs_info->super_copy.fsid,
1100 static char *size_strs[] = { "", "KB", "MB", "GB", "TB",
1101 "PB", "EB", "ZB", "YB"};
1102 char *pretty_sizes(u64 size)
1105 int pretty_len = 16;
1113 u64 last_size = size;
1115 while(size >= 1024){
1121 if (num_divs > ARRAY_SIZE(size_strs))
1123 fraction = (float)last_size / 1024;
1125 pretty = malloc(pretty_len);
1126 snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]);
1131 * Checks to make sure that the label matches our requirements.
1133 0 if everything is safe and usable
1134 -1 if the label is too long
1135 -2 if the label contains an invalid character
1137 int check_label(char *input)
1140 int len = strlen(input);
1142 if (len > BTRFS_LABEL_SIZE) {
1146 for (i = 0; i < len; i++) {
1147 if (input[i] == '/' || input[i] == '\\') {
1155 int btrfs_scan_block_devices(int run_ioctl)
1161 struct btrfs_fs_devices *tmp_devices;
1163 FILE *proc_partitions;
1171 proc_partitions = fopen("/proc/partitions","r");
1172 if (!proc_partitions) {
1173 fprintf(stderr, "Unable to open '/proc/partitions' for scanning\n");
1176 /* skip the header */
1177 for(i=0; i < 2 ; i++)
1178 if(!fgets(buf, 1023, proc_partitions)){
1179 fprintf(stderr, "Unable to read '/proc/partitions' for scanning\n");
1180 fclose(proc_partitions);
1184 strcpy(fullpath,"/dev/");
1185 while(fgets(buf, 1023, proc_partitions)) {
1186 i = sscanf(buf," %*d %*d %*d %99s", fullpath+5);
1189 * multipath and MD devices may register as a btrfs filesystem
1190 * both through the original block device and through
1191 * the special (/dev/mapper or /dev/mdX) entry.
1192 * This scans the special entries last
1194 special = strncmp(fullpath, "/dev/dm-", strlen("/dev/dm-")) == 0;
1196 special = strncmp(fullpath, "/dev/md", strlen("/dev/md")) == 0;
1198 if (scans == 0 && special)
1200 if (scans > 0 && !special)
1203 ret = lstat(fullpath, &st);
1205 fprintf(stderr, "failed to stat %s\n", fullpath);
1208 if (!S_ISBLK(st.st_mode)) {
1212 fd = open(fullpath, O_RDONLY);
1214 fprintf(stderr, "failed to read %s\n", fullpath);
1217 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1219 BTRFS_SUPER_INFO_OFFSET);
1220 if (ret == 0 && run_ioctl > 0) {
1221 btrfs_register_one_device(fullpath);
1226 fclose(proc_partitions);
1235 u64 parse_size(char *s)
1241 for (i=0 ; s[i] && isdigit(s[i]) ; i++) ;
1243 fprintf(stderr, "ERROR: size value is empty\n");
1265 fprintf(stderr, "ERROR: Unknown size descriptor "
1270 if (s[i] && s[i+1]) {
1271 fprintf(stderr, "ERROR: Illegal suffix contains "
1272 "character '%c' in wrong position\n",
1276 return strtoull(s, NULL, 10) * mult;