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 700
20 #define __USE_XOPEN2K8
21 #define __XOPEN2K8 /* due to an error in dirent.h, to get dirfd() */
26 #include <sys/ioctl.h>
27 #include <sys/mount.h>
29 #include <sys/types.h>
31 #include <uuid/uuid.h>
37 #include <linux/loop.h>
38 #include <linux/major.h>
39 #include <linux/kdev_t.h>
41 #include "kerncompat.h"
42 #include "radix-tree.h"
45 #include "transaction.h"
52 #define BLKGETSIZE64 0
53 static inline int ioctl(int fd, int define, u64 *size) { return 0; }
57 #define BLKDISCARD _IO(0x12,119)
61 discard_blocks(int fd, u64 start, u64 len)
63 u64 range[2] = { start, len };
65 if (ioctl(fd, BLKDISCARD, &range) < 0)
70 static u64 reference_root_table[] = {
71 [1] = BTRFS_ROOT_TREE_OBJECTID,
72 [2] = BTRFS_EXTENT_TREE_OBJECTID,
73 [3] = BTRFS_CHUNK_TREE_OBJECTID,
74 [4] = BTRFS_DEV_TREE_OBJECTID,
75 [5] = BTRFS_FS_TREE_OBJECTID,
76 [6] = BTRFS_CSUM_TREE_OBJECTID,
79 int make_btrfs(int fd, const char *device, const char *label,
80 u64 blocks[7], u64 num_bytes, u32 nodesize,
81 u32 leafsize, u32 sectorsize, u32 stripesize)
83 struct btrfs_super_block super;
84 struct extent_buffer *buf;
85 struct btrfs_root_item root_item;
86 struct btrfs_disk_key disk_key;
87 struct btrfs_extent_item *extent_item;
88 struct btrfs_inode_item *inode_item;
89 struct btrfs_chunk *chunk;
90 struct btrfs_dev_item *dev_item;
91 struct btrfs_dev_extent *dev_extent;
92 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
103 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
104 first_free &= ~((u64)sectorsize - 1);
106 memset(&super, 0, sizeof(super));
108 num_bytes = (num_bytes / sectorsize) * sectorsize;
109 uuid_generate(super.fsid);
110 uuid_generate(super.dev_item.uuid);
111 uuid_generate(chunk_tree_uuid);
113 btrfs_set_super_bytenr(&super, blocks[0]);
114 btrfs_set_super_num_devices(&super, 1);
115 super.magic = cpu_to_le64(BTRFS_MAGIC);
116 btrfs_set_super_generation(&super, 1);
117 btrfs_set_super_root(&super, blocks[1]);
118 btrfs_set_super_chunk_root(&super, blocks[3]);
119 btrfs_set_super_total_bytes(&super, num_bytes);
120 btrfs_set_super_bytes_used(&super, 6 * leafsize);
121 btrfs_set_super_sectorsize(&super, sectorsize);
122 btrfs_set_super_leafsize(&super, leafsize);
123 btrfs_set_super_nodesize(&super, nodesize);
124 btrfs_set_super_stripesize(&super, stripesize);
125 btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
126 btrfs_set_super_chunk_root_generation(&super, 1);
127 btrfs_set_super_cache_generation(&super, -1);
129 strncpy(super.label, label, BTRFS_LABEL_SIZE - 1);
131 buf = malloc(sizeof(*buf) + max(sectorsize, leafsize));
133 /* create the tree of root objects */
134 memset(buf->data, 0, leafsize);
136 btrfs_set_header_bytenr(buf, blocks[1]);
137 btrfs_set_header_nritems(buf, 4);
138 btrfs_set_header_generation(buf, 1);
139 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
140 btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
141 write_extent_buffer(buf, super.fsid, (unsigned long)
142 btrfs_header_fsid(buf), BTRFS_FSID_SIZE);
144 write_extent_buffer(buf, chunk_tree_uuid, (unsigned long)
145 btrfs_header_chunk_tree_uuid(buf),
148 /* create the items for the root tree */
149 memset(&root_item, 0, sizeof(root_item));
150 inode_item = &root_item.inode;
151 btrfs_set_stack_inode_generation(inode_item, 1);
152 btrfs_set_stack_inode_size(inode_item, 3);
153 btrfs_set_stack_inode_nlink(inode_item, 1);
154 btrfs_set_stack_inode_nbytes(inode_item, leafsize);
155 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
156 btrfs_set_root_refs(&root_item, 1);
157 btrfs_set_root_used(&root_item, leafsize);
158 btrfs_set_root_generation(&root_item, 1);
160 memset(&disk_key, 0, sizeof(disk_key));
161 btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
162 btrfs_set_disk_key_offset(&disk_key, 0);
165 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
166 btrfs_set_root_bytenr(&root_item, blocks[2]);
167 btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID);
168 btrfs_set_item_key(buf, &disk_key, nritems);
169 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
170 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
172 write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
173 nritems), sizeof(root_item));
176 itemoff = itemoff - sizeof(root_item);
177 btrfs_set_root_bytenr(&root_item, blocks[4]);
178 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
179 btrfs_set_item_key(buf, &disk_key, nritems);
180 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
181 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
183 write_extent_buffer(buf, &root_item,
184 btrfs_item_ptr_offset(buf, nritems),
188 itemoff = itemoff - sizeof(root_item);
189 btrfs_set_root_bytenr(&root_item, blocks[5]);
190 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
191 btrfs_set_item_key(buf, &disk_key, nritems);
192 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
193 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
195 write_extent_buffer(buf, &root_item,
196 btrfs_item_ptr_offset(buf, nritems),
200 itemoff = itemoff - sizeof(root_item);
201 btrfs_set_root_bytenr(&root_item, blocks[6]);
202 btrfs_set_disk_key_objectid(&disk_key, BTRFS_CSUM_TREE_OBJECTID);
203 btrfs_set_item_key(buf, &disk_key, nritems);
204 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
205 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
207 write_extent_buffer(buf, &root_item,
208 btrfs_item_ptr_offset(buf, nritems),
213 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
214 ret = pwrite(fd, buf->data, leafsize, blocks[1]);
215 BUG_ON(ret != leafsize);
217 /* create the items for the extent tree */
218 memset(buf->data+sizeof(struct btrfs_header), 0,
219 leafsize-sizeof(struct btrfs_header));
221 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize);
222 for (i = 1; i < 7; i++) {
223 BUG_ON(blocks[i] < first_free);
224 BUG_ON(blocks[i] < blocks[i - 1]);
226 /* create extent item */
227 itemoff -= sizeof(struct btrfs_extent_item) +
228 sizeof(struct btrfs_tree_block_info);
229 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
230 btrfs_set_disk_key_offset(&disk_key, leafsize);
231 btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_ITEM_KEY);
232 btrfs_set_item_key(buf, &disk_key, nritems);
233 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
235 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
236 sizeof(struct btrfs_extent_item) +
237 sizeof(struct btrfs_tree_block_info));
238 extent_item = btrfs_item_ptr(buf, nritems,
239 struct btrfs_extent_item);
240 btrfs_set_extent_refs(buf, extent_item, 1);
241 btrfs_set_extent_generation(buf, extent_item, 1);
242 btrfs_set_extent_flags(buf, extent_item,
243 BTRFS_EXTENT_FLAG_TREE_BLOCK);
246 /* create extent ref */
247 ref_root = reference_root_table[i];
248 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
249 btrfs_set_disk_key_offset(&disk_key, ref_root);
250 btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
251 btrfs_set_item_key(buf, &disk_key, nritems);
252 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
254 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), 0);
257 btrfs_set_header_bytenr(buf, blocks[2]);
258 btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
259 btrfs_set_header_nritems(buf, nritems);
260 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
261 ret = pwrite(fd, buf->data, leafsize, blocks[2]);
262 BUG_ON(ret != leafsize);
264 /* create the chunk tree */
265 memset(buf->data+sizeof(struct btrfs_header), 0,
266 leafsize-sizeof(struct btrfs_header));
268 item_size = sizeof(*dev_item);
269 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - item_size;
271 /* first device 1 (there is no device 0) */
272 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
273 btrfs_set_disk_key_offset(&disk_key, 1);
274 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
275 btrfs_set_item_key(buf, &disk_key, nritems);
276 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
277 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), item_size);
279 dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
280 btrfs_set_device_id(buf, dev_item, 1);
281 btrfs_set_device_generation(buf, dev_item, 0);
282 btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
283 btrfs_set_device_bytes_used(buf, dev_item,
284 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
285 btrfs_set_device_io_align(buf, dev_item, sectorsize);
286 btrfs_set_device_io_width(buf, dev_item, sectorsize);
287 btrfs_set_device_sector_size(buf, dev_item, sectorsize);
288 btrfs_set_device_type(buf, dev_item, 0);
290 write_extent_buffer(buf, super.dev_item.uuid,
291 (unsigned long)btrfs_device_uuid(dev_item),
293 write_extent_buffer(buf, super.fsid,
294 (unsigned long)btrfs_device_fsid(dev_item),
296 read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
300 item_size = btrfs_chunk_item_size(1);
301 itemoff = itemoff - item_size;
303 /* then we have chunk 0 */
304 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
305 btrfs_set_disk_key_offset(&disk_key, 0);
306 btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
307 btrfs_set_item_key(buf, &disk_key, nritems);
308 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
309 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), item_size);
311 chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
312 btrfs_set_chunk_length(buf, chunk, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
313 btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
314 btrfs_set_chunk_stripe_len(buf, chunk, 64 * 1024);
315 btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
316 btrfs_set_chunk_io_align(buf, chunk, sectorsize);
317 btrfs_set_chunk_io_width(buf, chunk, sectorsize);
318 btrfs_set_chunk_sector_size(buf, chunk, sectorsize);
319 btrfs_set_chunk_num_stripes(buf, chunk, 1);
320 btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
321 btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
324 write_extent_buffer(buf, super.dev_item.uuid,
325 (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
328 /* copy the key for the chunk to the system array */
329 ptr = super.sys_chunk_array;
330 array_size = sizeof(disk_key);
332 memcpy(ptr, &disk_key, sizeof(disk_key));
333 ptr += sizeof(disk_key);
335 /* copy the chunk to the system array */
336 read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
337 array_size += item_size;
339 btrfs_set_super_sys_array_size(&super, array_size);
341 btrfs_set_header_bytenr(buf, blocks[3]);
342 btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
343 btrfs_set_header_nritems(buf, nritems);
344 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
345 ret = pwrite(fd, buf->data, leafsize, blocks[3]);
347 /* create the device tree */
348 memset(buf->data+sizeof(struct btrfs_header), 0,
349 leafsize-sizeof(struct btrfs_header));
351 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
352 sizeof(struct btrfs_dev_extent);
354 btrfs_set_disk_key_objectid(&disk_key, 1);
355 btrfs_set_disk_key_offset(&disk_key, 0);
356 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
357 btrfs_set_item_key(buf, &disk_key, nritems);
358 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
359 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
360 sizeof(struct btrfs_dev_extent));
361 dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
362 btrfs_set_dev_extent_chunk_tree(buf, dev_extent,
363 BTRFS_CHUNK_TREE_OBJECTID);
364 btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
365 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
366 btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
368 write_extent_buffer(buf, chunk_tree_uuid,
369 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
372 btrfs_set_dev_extent_length(buf, dev_extent,
373 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
376 btrfs_set_header_bytenr(buf, blocks[4]);
377 btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
378 btrfs_set_header_nritems(buf, nritems);
379 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
380 ret = pwrite(fd, buf->data, leafsize, blocks[4]);
382 /* create the FS root */
383 memset(buf->data+sizeof(struct btrfs_header), 0,
384 leafsize-sizeof(struct btrfs_header));
385 btrfs_set_header_bytenr(buf, blocks[5]);
386 btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
387 btrfs_set_header_nritems(buf, 0);
388 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
389 ret = pwrite(fd, buf->data, leafsize, blocks[5]);
390 BUG_ON(ret != leafsize);
392 /* finally create the csum root */
393 memset(buf->data+sizeof(struct btrfs_header), 0,
394 leafsize-sizeof(struct btrfs_header));
395 btrfs_set_header_bytenr(buf, blocks[6]);
396 btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
397 btrfs_set_header_nritems(buf, 0);
398 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
399 ret = pwrite(fd, buf->data, leafsize, blocks[6]);
400 BUG_ON(ret != leafsize);
402 /* and write out the super block */
403 BUG_ON(sizeof(super) > sectorsize);
404 memset(buf->data, 0, sectorsize);
405 memcpy(buf->data, &super, sizeof(super));
406 buf->len = sectorsize;
407 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
408 ret = pwrite(fd, buf->data, sectorsize, blocks[0]);
409 BUG_ON(ret != sectorsize);
416 static u64 device_size(int fd, struct stat *st)
419 if (S_ISREG(st->st_mode)) {
422 if (!S_ISBLK(st->st_mode)) {
425 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
431 static int zero_blocks(int fd, off_t start, size_t len)
433 char *buf = malloc(len);
440 written = pwrite(fd, buf, len, start);
447 static int zero_dev_start(int fd)
450 size_t len = 2 * 1024 * 1024;
453 /* don't overwrite the disk labels on sparc */
457 return zero_blocks(fd, start, len);
460 static int zero_dev_end(int fd, u64 dev_size)
462 size_t len = 2 * 1024 * 1024;
463 off_t start = dev_size - len;
465 return zero_blocks(fd, start, len);
468 int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
469 struct btrfs_root *root, int fd, char *path,
470 u64 block_count, u32 io_width, u32 io_align,
473 struct btrfs_super_block *disk_super;
474 struct btrfs_super_block *super = &root->fs_info->super_copy;
475 struct btrfs_device *device;
476 struct btrfs_dev_item *dev_item;
482 device = kmalloc(sizeof(*device), GFP_NOFS);
485 buf = kmalloc(sectorsize, GFP_NOFS);
490 BUG_ON(sizeof(*disk_super) > sectorsize);
491 memset(buf, 0, sectorsize);
493 disk_super = (struct btrfs_super_block *)buf;
494 dev_item = &disk_super->dev_item;
496 uuid_generate(device->uuid);
499 device->io_width = io_width;
500 device->io_align = io_align;
501 device->sector_size = sectorsize;
503 device->writeable = 1;
504 device->total_bytes = block_count;
505 device->bytes_used = 0;
506 device->total_ios = 0;
507 device->dev_root = root->fs_info->dev_root;
509 ret = btrfs_add_device(trans, root, device);
512 total_bytes = btrfs_super_total_bytes(super) + block_count;
513 btrfs_set_super_total_bytes(super, total_bytes);
515 num_devs = btrfs_super_num_devices(super) + 1;
516 btrfs_set_super_num_devices(super, num_devs);
518 memcpy(disk_super, super, sizeof(*disk_super));
520 printf("adding device %s id %llu\n", path,
521 (unsigned long long)device->devid);
523 btrfs_set_super_bytenr(disk_super, BTRFS_SUPER_INFO_OFFSET);
524 btrfs_set_stack_device_id(dev_item, device->devid);
525 btrfs_set_stack_device_type(dev_item, device->type);
526 btrfs_set_stack_device_io_align(dev_item, device->io_align);
527 btrfs_set_stack_device_io_width(dev_item, device->io_width);
528 btrfs_set_stack_device_sector_size(dev_item, device->sector_size);
529 btrfs_set_stack_device_total_bytes(dev_item, device->total_bytes);
530 btrfs_set_stack_device_bytes_used(dev_item, device->bytes_used);
531 memcpy(&dev_item->uuid, device->uuid, BTRFS_UUID_SIZE);
533 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
534 BUG_ON(ret != sectorsize);
537 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
538 device->fs_devices = root->fs_info->fs_devices;
542 int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
543 u64 max_block_count, int *mixed, int nodiscard)
550 ret = fstat(fd, &st);
552 fprintf(stderr, "unable to stat %s\n", file);
556 block_count = device_size(fd, &st);
557 if (block_count == 0) {
558 fprintf(stderr, "unable to find %s size\n", file);
562 block_count = min(block_count, max_block_count);
565 if (block_count < 1024 * 1024 * 1024 && !(*mixed)) {
566 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
572 * We intentionally ignore errors from the discard ioctl. It is
573 * not necessary for the mkfs functionality but just an optimization.
575 discard_blocks(fd, 0, block_count);
578 ret = zero_dev_start(fd);
580 fprintf(stderr, "failed to zero device start %d\n", ret);
584 for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
585 bytenr = btrfs_sb_offset(i);
586 if (bytenr >= block_count)
588 zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
592 ret = zero_dev_end(fd, block_count);
594 fprintf(stderr, "failed to zero device end %d\n", ret);
598 *block_count_ret = block_count;
602 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
603 struct btrfs_root *root, u64 objectid)
606 struct btrfs_inode_item inode_item;
607 time_t now = time(NULL);
609 memset(&inode_item, 0, sizeof(inode_item));
610 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
611 btrfs_set_stack_inode_size(&inode_item, 0);
612 btrfs_set_stack_inode_nlink(&inode_item, 1);
613 btrfs_set_stack_inode_nbytes(&inode_item, root->leafsize);
614 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
615 btrfs_set_stack_timespec_sec(&inode_item.atime, now);
616 btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
617 btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
618 btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
619 btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
620 btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
621 btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
622 btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
624 if (root->fs_info->tree_root == root)
625 btrfs_set_super_root_dir(&root->fs_info->super_copy, objectid);
627 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
631 ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0);
635 btrfs_set_root_dirid(&root->root_item, objectid);
641 /* checks if a device is a loop device */
642 int is_loop_device (const char* device) {
645 if(stat(device, &statbuf) < 0)
648 return (S_ISBLK(statbuf.st_mode) &&
649 MAJOR(statbuf.st_rdev) == LOOP_MAJOR);
653 /* Takes a loop device path (e.g. /dev/loop0) and returns
654 * the associated file (e.g. /images/my_btrfs.img) */
655 int resolve_loop_device(const char* loop_dev, char* loop_file, int max_len)
661 char real_loop_dev[PATH_MAX];
663 if (!realpath(loop_dev, real_loop_dev))
665 snprintf(p, PATH_MAX, "/sys/block/%s/loop/backing_file", strrchr(real_loop_dev, '/'));
666 if (!(f = fopen(p, "r")))
669 snprintf(fmt, 20, "%%%i[^\n]", max_len-1);
670 ret = fscanf(f, fmt, loop_file);
678 /* Checks whether a and b are identical or device
679 * files associated with the same block device
681 int is_same_blk_file(const char* a, const char* b)
683 struct stat st_buf_a, st_buf_b;
684 char real_a[PATH_MAX];
685 char real_b[PATH_MAX];
687 if(!realpath(a, real_a) ||
688 !realpath(b, real_b))
693 /* Identical path? */
694 if(strcmp(real_a, real_b) == 0)
697 if(stat(a, &st_buf_a) < 0 ||
698 stat(b, &st_buf_b) < 0)
705 /* Same blockdevice? */
706 if(S_ISBLK(st_buf_a.st_mode) &&
707 S_ISBLK(st_buf_b.st_mode) &&
708 st_buf_a.st_rdev == st_buf_b.st_rdev)
714 if (st_buf_a.st_dev == st_buf_b.st_dev &&
715 st_buf_a.st_ino == st_buf_b.st_ino)
723 /* checks if a and b are identical or device
724 * files associated with the same block device or
725 * if one file is a loop device that uses the other
728 int is_same_loop_file(const char* a, const char* b)
730 char res_a[PATH_MAX];
731 char res_b[PATH_MAX];
736 /* Resolve a if it is a loop device */
737 if((ret = is_loop_device(a)) < 0) {
742 if ((ret = resolve_loop_device(a, res_a, sizeof(res_a))) < 0)
750 /* Resolve b if it is a loop device */
751 if ((ret = is_loop_device(b)) < 0) {
756 if((ret = resolve_loop_device(b, res_b, sizeof(res_b))) < 0)
764 return is_same_blk_file(final_a, final_b);
767 /* Checks if a file exists and is a block or regular file*/
768 int is_existing_blk_or_reg_file(const char* filename)
772 if(stat(filename, &st_buf) < 0) {
779 return (S_ISBLK(st_buf.st_mode) || S_ISREG(st_buf.st_mode));
782 /* Checks if a file is used (directly or indirectly via a loop device)
783 * by a device in fs_devices
785 int blk_file_in_dev_list(struct btrfs_fs_devices* fs_devices, const char* file)
788 struct list_head *head;
789 struct list_head *cur;
790 struct btrfs_device *device;
792 head = &fs_devices->devices;
793 list_for_each(cur, head) {
794 device = list_entry(cur, struct btrfs_device, dev_list);
796 if((ret = is_same_loop_file(device->name, file)))
804 * returns 1 if the device was mounted, < 0 on error or 0 if everything
805 * is safe to continue.
807 int check_mounted(const char* file)
812 fd = open(file, O_RDONLY);
814 fprintf (stderr, "check_mounted(): Could not open %s\n", file);
818 ret = check_mounted_where(fd, file, NULL, 0, NULL);
824 int check_mounted_where(int fd, const char *file, char *where, int size,
825 struct btrfs_fs_devices **fs_dev_ret)
830 struct btrfs_fs_devices *fs_devices_mnt = NULL;
834 /* scan the initial device */
835 ret = btrfs_scan_one_device(fd, file, &fs_devices_mnt,
836 &total_devs, BTRFS_SUPER_INFO_OFFSET);
837 is_btrfs = (ret >= 0);
839 /* scan other devices */
840 if (is_btrfs && total_devs > 1) {
841 if((ret = btrfs_scan_for_fsid(fs_devices_mnt, total_devs, 1)))
845 /* iterate over the list of currently mountes filesystems */
846 if ((f = setmntent ("/proc/mounts", "r")) == NULL)
849 while ((mnt = getmntent (f)) != NULL) {
851 if(strcmp(mnt->mnt_type, "btrfs") != 0)
854 ret = blk_file_in_dev_list(fs_devices_mnt, mnt->mnt_fsname);
856 /* ignore entries in the mount table that are not
857 associated with a file*/
858 if((ret = is_existing_blk_or_reg_file(mnt->mnt_fsname)) < 0)
859 goto out_mntloop_err;
863 ret = is_same_loop_file(file, mnt->mnt_fsname);
867 goto out_mntloop_err;
872 /* Did we find an entry in mnt table? */
873 if (mnt && size && where) {
874 strncpy(where, mnt->mnt_dir, size);
878 *fs_dev_ret = fs_devices_mnt;
888 /* Gets the mount point of btrfs filesystem that is using the specified device.
889 * Returns 0 is everything is good, <0 if we have an error.
890 * TODO: Fix this fucntion and check_mounted to work with multiple drive BTRFS
893 int get_mountpt(char *dev, char *mntpt, size_t size)
899 f = setmntent("/proc/mounts", "r");
903 while ((mnt = getmntent(f)) != NULL )
905 if (strcmp(dev, mnt->mnt_fsname) == 0)
907 strncpy(mntpt, mnt->mnt_dir, size);
916 /* We didn't find an entry so lets report an error */
924 struct list_head list;
928 void btrfs_register_one_device(char *fname)
930 struct btrfs_ioctl_vol_args args;
935 fd = open("/dev/btrfs-control", O_RDONLY);
937 fprintf(stderr, "failed to open /dev/btrfs-control "
938 "skipping device registration\n");
941 strncpy(args.name, fname, BTRFS_PATH_NAME_MAX);
942 args.name[BTRFS_PATH_NAME_MAX-1] = 0;
943 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
946 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
952 int btrfs_scan_one_dir(char *dirname, int run_ioctl)
955 struct dirent *dirent;
956 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);
975 fullpath = malloc(PATH_MAX);
976 dirname = pending->name;
982 dirp = opendir(dirname);
984 fprintf(stderr, "Unable to open %s for scanning\n", dirname);
989 dirent = readdir(dirp);
992 if (dirent->d_name[0] == '.')
994 if (dirname_len + strlen(dirent->d_name) + 2 > PATH_MAX) {
998 snprintf(fullpath, PATH_MAX, "%s/%s", dirname, dirent->d_name);
999 ret = lstat(fullpath, &st);
1001 fprintf(stderr, "failed to stat %s\n", fullpath);
1004 if (S_ISLNK(st.st_mode))
1006 if (S_ISDIR(st.st_mode)) {
1007 struct pending_dir *next = malloc(sizeof(*next));
1012 strcpy(next->name, fullpath);
1013 list_add_tail(&next->list, &pending_list);
1015 if (!S_ISBLK(st.st_mode)) {
1018 fd = open(fullpath, O_RDONLY);
1020 /* ignore the following errors:
1021 ENXIO (device don't exists)
1022 ENOMEDIUM (No medium found ->
1023 like a cd tray empty)
1025 if(errno != ENXIO && errno != ENOMEDIUM)
1026 fprintf(stderr, "failed to read %s: %s\n",
1027 fullpath, strerror(errno));
1030 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1032 BTRFS_SUPER_INFO_OFFSET);
1033 if (ret == 0 && run_ioctl > 0) {
1034 btrfs_register_one_device(fullpath);
1038 if (!list_empty(&pending_list)) {
1040 pending = list_entry(pending_list.next, struct pending_dir,
1043 list_del(&pending->list);
1057 int btrfs_scan_for_fsid(struct btrfs_fs_devices *fs_devices, u64 total_devs,
1062 ret = btrfs_scan_block_devices(run_ioctls);
1064 ret = btrfs_scan_one_dir("/dev", run_ioctls);
1068 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
1071 struct btrfs_super_block *disk_super;
1075 buf = malloc(BTRFS_SUPER_INFO_SIZE);
1080 ret = pread(fd, buf, BTRFS_SUPER_INFO_SIZE, super_offset);
1081 if (ret != BTRFS_SUPER_INFO_SIZE)
1085 disk_super = (struct btrfs_super_block *)buf;
1086 if (disk_super->magic != cpu_to_le64(BTRFS_MAGIC))
1089 if (!memcmp(disk_super->fsid, root->fs_info->super_copy.fsid,
1098 static char *size_strs[] = { "", "KB", "MB", "GB", "TB",
1099 "PB", "EB", "ZB", "YB"};
1100 char *pretty_sizes(u64 size)
1103 int pretty_len = 16;
1111 u64 last_size = size;
1113 while(size >= 1024){
1119 if (num_divs >= ARRAY_SIZE(size_strs))
1121 fraction = (float)last_size / 1024;
1123 pretty = malloc(pretty_len);
1124 snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]);
1129 * __strncpy__null - strncpy with null termination
1130 * @dest: the target array
1131 * @src: the source string
1132 * @n: maximum bytes to copy (size of *dest)
1134 * Like strncpy, but ensures destination is null-terminated.
1136 * Copies the string pointed to by src, including the terminating null
1137 * byte ('\0'), to the buffer pointed to by dest, up to a maximum
1138 * of n bytes. Then ensure that dest is null-terminated.
1140 char *__strncpy__null(char *dest, const char *src, size_t n)
1142 strncpy(dest, src, n);
1149 * Checks to make sure that the label matches our requirements.
1151 0 if everything is safe and usable
1152 -1 if the label is too long
1153 -2 if the label contains an invalid character
1155 int check_label(char *input)
1158 int len = strlen(input);
1160 if (len > BTRFS_LABEL_SIZE) {
1164 for (i = 0; i < len; i++) {
1165 if (input[i] == '/' || input[i] == '\\') {
1173 int btrfs_scan_block_devices(int run_ioctl)
1179 struct btrfs_fs_devices *tmp_devices;
1181 FILE *proc_partitions;
1189 proc_partitions = fopen("/proc/partitions","r");
1190 if (!proc_partitions) {
1191 fprintf(stderr, "Unable to open '/proc/partitions' for scanning\n");
1194 /* skip the header */
1195 for(i=0; i < 2 ; i++)
1196 if(!fgets(buf, 1023, proc_partitions)){
1197 fprintf(stderr, "Unable to read '/proc/partitions' for scanning\n");
1198 fclose(proc_partitions);
1202 strcpy(fullpath,"/dev/");
1203 while(fgets(buf, 1023, proc_partitions)) {
1204 i = sscanf(buf," %*d %*d %*d %99s", fullpath+5);
1207 * multipath and MD devices may register as a btrfs filesystem
1208 * both through the original block device and through
1209 * the special (/dev/mapper or /dev/mdX) entry.
1210 * This scans the special entries last
1212 special = strncmp(fullpath, "/dev/dm-", strlen("/dev/dm-")) == 0;
1214 special = strncmp(fullpath, "/dev/md", strlen("/dev/md")) == 0;
1216 if (scans == 0 && special)
1218 if (scans > 0 && !special)
1221 ret = lstat(fullpath, &st);
1223 fprintf(stderr, "failed to stat %s\n", fullpath);
1226 if (!S_ISBLK(st.st_mode)) {
1230 fd = open(fullpath, O_RDONLY);
1232 fprintf(stderr, "failed to open %s: %s\n",
1233 fullpath, strerror(errno));
1236 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1238 BTRFS_SUPER_INFO_OFFSET);
1239 if (ret == 0 && run_ioctl > 0) {
1240 btrfs_register_one_device(fullpath);
1245 fclose(proc_partitions);
1254 u64 parse_size(char *s)
1260 for (i=0 ; s[i] && isdigit(s[i]) ; i++) ;
1262 fprintf(stderr, "ERROR: size value is empty\n");
1284 fprintf(stderr, "ERROR: Unknown size descriptor "
1289 if (s[i] && s[i+1]) {
1290 fprintf(stderr, "ERROR: Illegal suffix contains "
1291 "character '%c' in wrong position\n",
1295 return strtoull(s, NULL, 10) * mult;
1298 int open_file_or_dir(const char *fname)
1305 ret = stat(fname, &st);
1309 if (S_ISDIR(st.st_mode)) {
1310 dirstream = opendir(fname);
1314 fd = dirfd(dirstream);
1316 fd = open(fname, O_RDWR);
1324 int get_device_info(int fd, u64 devid,
1325 struct btrfs_ioctl_dev_info_args *di_args)
1329 di_args->devid = devid;
1330 memset(&di_args->uuid, '\0', sizeof(di_args->uuid));
1332 ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
1333 return ret ? -errno : 0;
1336 int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args,
1337 struct btrfs_ioctl_dev_info_args **di_ret)
1342 struct btrfs_fs_devices *fs_devices_mnt = NULL;
1343 struct btrfs_ioctl_dev_info_args *di_args;
1344 char mp[BTRFS_PATH_NAME_MAX + 1];
1346 memset(fi_args, 0, sizeof(*fi_args));
1348 ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args);
1349 if (ret && (errno == EINVAL || errno == ENOTTY)) {
1350 /* path is not a mounted btrfs. Try if it's a device */
1351 ret = check_mounted_where(fd, path, mp, sizeof(mp),
1357 fi_args->num_devices = 1;
1358 fi_args->max_id = fs_devices_mnt->latest_devid;
1359 i = fs_devices_mnt->latest_devid;
1360 memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE);
1362 fd = open_file_or_dir(mp);
1369 if (!fi_args->num_devices)
1372 di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args));
1376 for (; i <= fi_args->max_id; ++i) {
1377 BUG_ON(ndevs >= fi_args->num_devices);
1378 ret = get_device_info(fd, i, &di_args[ndevs]);