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
24 #include <sys/ioctl.h>
25 #include <sys/mount.h>
27 #include <sys/types.h>
29 #include <uuid/uuid.h>
34 #include "kerncompat.h"
35 #include "radix-tree.h"
38 #include "transaction.h"
45 #define BLKGETSIZE64 0
46 static inline int ioctl(int fd, int define, u64 *size) { return 0; }
49 static u64 reference_root_table[6] = {
50 [1] = BTRFS_ROOT_TREE_OBJECTID,
51 [2] = BTRFS_EXTENT_TREE_OBJECTID,
52 [3] = BTRFS_CHUNK_TREE_OBJECTID,
53 [4] = BTRFS_DEV_TREE_OBJECTID,
54 [5] = BTRFS_FS_TREE_OBJECTID,
57 int make_btrfs(int fd, char *device_name,
58 u64 blocks[6], u64 num_bytes, u32 nodesize,
59 u32 leafsize, u32 sectorsize, u32 stripesize)
61 struct btrfs_super_block super;
62 struct extent_buffer *buf;
63 struct btrfs_root_item root_item;
64 struct btrfs_disk_key disk_key;
65 struct btrfs_extent_ref *extent_ref;
66 struct btrfs_extent_item *extent_item;
67 struct btrfs_inode_item *inode_item;
68 struct btrfs_chunk *chunk;
69 struct btrfs_dev_item *dev_item;
70 struct btrfs_dev_extent *dev_extent;
83 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
84 first_free &= ~((u64)sectorsize - 1);
86 num_bytes = (num_bytes / sectorsize) * sectorsize;
87 uuid_generate(super.fsid);
88 btrfs_set_super_bytenr(&super, blocks[0]);
89 btrfs_set_super_num_devices(&super, 1);
90 strncpy((char *)&super.magic, BTRFS_MAGIC, sizeof(super.magic));
91 btrfs_set_super_generation(&super, 1);
92 btrfs_set_super_root(&super, blocks[1]);
93 btrfs_set_super_chunk_root(&super, blocks[3]);
94 btrfs_set_super_total_bytes(&super, num_bytes);
95 btrfs_set_super_bytes_used(&super, first_free + 5 * leafsize);
96 btrfs_set_super_root_dir(&super, 0);
97 btrfs_set_super_sectorsize(&super, sectorsize);
98 btrfs_set_super_leafsize(&super, leafsize);
99 btrfs_set_super_nodesize(&super, nodesize);
100 btrfs_set_super_stripesize(&super, stripesize);
101 btrfs_set_super_root_level(&super, 0);
102 btrfs_set_super_chunk_root_level(&super, 0);
103 btrfs_set_super_sys_array_size(&super, 0);
105 buf = malloc(sizeof(*buf) + max(sectorsize, leafsize));
107 /* create the tree of root objects */
108 memset(buf->data, 0, leafsize);
109 btrfs_set_header_bytenr(buf, blocks[1]);
110 btrfs_set_header_nritems(buf, 3);
111 btrfs_set_header_generation(buf, 1);
112 btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
113 write_extent_buffer(buf, super.fsid, (unsigned long)
114 btrfs_header_fsid(buf), BTRFS_FSID_SIZE);
116 /* create the items for the root tree */
117 memset(&root_item, 0, sizeof(root_item));
118 inode_item = &root_item.inode;
119 btrfs_set_stack_inode_generation(inode_item, 1);
120 btrfs_set_stack_inode_size(inode_item, 3);
121 btrfs_set_stack_inode_nlink(inode_item, 1);
122 btrfs_set_stack_inode_nblocks(inode_item, 1);
123 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
124 btrfs_set_root_refs(&root_item, 1);
125 btrfs_set_root_used(&root_item, leafsize);
127 memset(&disk_key, 0, sizeof(disk_key));
128 btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
129 btrfs_set_disk_key_offset(&disk_key, 0);
132 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
133 btrfs_set_root_bytenr(&root_item, blocks[2]);
134 btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID);
135 btrfs_set_item_key(buf, &disk_key, nritems);
136 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
137 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
139 write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
140 nritems), sizeof(root_item));
143 itemoff = itemoff - sizeof(root_item);
144 btrfs_set_root_bytenr(&root_item, blocks[4]);
145 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
146 btrfs_set_item_key(buf, &disk_key, nritems);
147 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
148 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
150 write_extent_buffer(buf, &root_item,
151 btrfs_item_ptr_offset(buf, nritems),
155 itemoff = itemoff - sizeof(root_item);
156 btrfs_set_root_bytenr(&root_item, blocks[5]);
157 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
158 btrfs_set_item_key(buf, &disk_key, nritems);
159 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
160 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
162 write_extent_buffer(buf, &root_item,
163 btrfs_item_ptr_offset(buf, nritems),
168 ret = pwrite(fd, buf->data, leafsize, blocks[1]);
169 BUG_ON(ret != leafsize);
171 /* create the items for the extent tree */
173 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
174 sizeof(struct btrfs_extent_item);
175 btrfs_set_disk_key_objectid(&disk_key, 0);
176 btrfs_set_disk_key_offset(&disk_key, first_free);
177 btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_ITEM_KEY);
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),
181 sizeof(struct btrfs_extent_item));
182 extent_item = btrfs_item_ptr(buf, nritems, struct btrfs_extent_item);
183 btrfs_set_extent_refs(buf, extent_item, 1);
185 for (i = 1; i < 6; i++) {
186 BUG_ON(blocks[i] < first_free);
187 BUG_ON(blocks[i] < blocks[i - 1]);
189 /* create extent item */
190 itemoff = itemoff - sizeof(struct btrfs_extent_item);
191 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
192 btrfs_set_disk_key_offset(&disk_key, leafsize);
193 btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_ITEM_KEY);
194 btrfs_set_item_key(buf, &disk_key, nritems);
195 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
197 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
198 sizeof(struct btrfs_extent_item));
199 extent_item = btrfs_item_ptr(buf, nritems,
200 struct btrfs_extent_item);
201 btrfs_set_extent_refs(buf, extent_item, 1);
204 /* create extent ref */
205 ref_root = reference_root_table[i];
206 if (ref_root == BTRFS_FS_TREE_OBJECTID)
211 hash = btrfs_hash_extent_ref(ref_root, ref_gen, 0, 0);
212 itemoff = itemoff - sizeof(struct btrfs_extent_ref);
213 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
214 btrfs_set_disk_key_offset(&disk_key, hash);
215 btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_REF_KEY);
216 btrfs_set_item_key(buf, &disk_key, nritems);
217 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
219 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
220 sizeof(struct btrfs_extent_ref));
221 extent_ref = btrfs_item_ptr(buf, nritems,
222 struct btrfs_extent_ref);
223 btrfs_set_ref_root(buf, extent_ref, ref_root);
224 btrfs_set_ref_generation(buf, extent_ref, ref_gen);
225 btrfs_set_ref_objectid(buf, extent_ref, 0);
226 btrfs_set_ref_offset(buf, extent_ref, 0);
229 btrfs_set_header_bytenr(buf, blocks[2]);
230 btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
231 btrfs_set_header_nritems(buf, nritems);
232 ret = pwrite(fd, buf->data, leafsize, blocks[2]);
233 BUG_ON(ret != leafsize);
235 /* create the chunk tree */
237 item_size = btrfs_chunk_item_size(1);
238 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - item_size;
240 /* first we have chunk 0 */
241 btrfs_set_disk_key_objectid(&disk_key, 0);
242 btrfs_set_disk_key_offset(&disk_key, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
243 btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
244 btrfs_set_item_key(buf, &disk_key, nritems);
245 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
246 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), item_size);
248 chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
249 btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
250 btrfs_set_chunk_stripe_len(buf, chunk, 64 * 1024);
251 btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
252 btrfs_set_chunk_io_align(buf, chunk, sectorsize);
253 btrfs_set_chunk_io_width(buf, chunk, sectorsize);
254 btrfs_set_chunk_sector_size(buf, chunk, sectorsize);
255 btrfs_set_chunk_num_stripes(buf, chunk, 1);
256 btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
257 btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
259 /* copy the key for the chunk to the system array */
260 ptr = super.sys_chunk_array;
261 array_size = sizeof(disk_key);
263 memcpy(ptr, &disk_key, sizeof(disk_key));
264 ptr += sizeof(disk_key);
266 /* copy the chunk to the system array */
267 read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
268 array_size += item_size;
270 btrfs_set_super_sys_array_size(&super, array_size);
272 /* then device 1 (there is no device 0) */
274 item_size = sizeof(*dev_item);
275 itemoff = itemoff - item_size;
276 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
277 btrfs_set_disk_key_offset(&disk_key, 1);
278 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
279 btrfs_set_item_key(buf, &disk_key, nritems);
280 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
281 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), item_size);
283 dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
284 btrfs_set_device_id(buf, dev_item, 1);
285 btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
286 btrfs_set_device_bytes_used(buf, dev_item,
287 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
288 btrfs_set_device_io_align(buf, dev_item, sectorsize);
289 btrfs_set_device_io_width(buf, dev_item, sectorsize);
290 btrfs_set_device_sector_size(buf, dev_item, sectorsize);
291 btrfs_set_device_type(buf, dev_item, 0);
294 uuid_generate(super.dev_item.uuid);
296 write_extent_buffer(buf, super.dev_item.uuid,
297 (unsigned long)btrfs_device_uuid(dev_item),
298 BTRFS_DEV_UUID_SIZE);
299 read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
302 btrfs_set_header_bytenr(buf, blocks[3]);
303 btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
304 btrfs_set_header_nritems(buf, nritems);
305 ret = pwrite(fd, buf->data, leafsize, blocks[3]);
307 /* create the device tree */
309 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
310 sizeof(struct btrfs_dev_extent);
312 btrfs_set_disk_key_objectid(&disk_key, 1);
313 btrfs_set_disk_key_offset(&disk_key, 0);
314 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
315 btrfs_set_item_key(buf, &disk_key, nritems);
316 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
317 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
318 sizeof(struct btrfs_dev_extent));
319 dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
320 btrfs_set_dev_extent_owner(buf, dev_extent, 0);
321 btrfs_set_dev_extent_length(buf, dev_extent,
322 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
325 btrfs_set_header_bytenr(buf, blocks[4]);
326 btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
327 btrfs_set_header_nritems(buf, nritems);
328 ret = pwrite(fd, buf->data, leafsize, blocks[4]);
330 /* finally create the FS root */
331 btrfs_set_header_bytenr(buf, blocks[5]);
332 btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
333 btrfs_set_header_nritems(buf, 0);
334 ret = pwrite(fd, buf->data, leafsize, blocks[5]);
335 BUG_ON(ret != leafsize);
337 /* and write out the super block */
338 BUG_ON(sizeof(super) > sectorsize);
339 memset(buf->data, 0, sectorsize);
340 memcpy(buf->data, &super, sizeof(super));
341 ret = pwrite(fd, buf->data, sectorsize, blocks[0]);
342 BUG_ON(ret != sectorsize);
349 static u64 device_size(int fd, struct stat *st)
352 if (S_ISREG(st->st_mode)) {
355 if (!S_ISBLK(st->st_mode)) {
358 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
364 static int zero_blocks(int fd, off_t start, size_t len)
366 char *buf = malloc(len);
373 written = pwrite(fd, buf, len, start);
380 static int zero_dev_start(int fd)
383 size_t len = 2 * 1024 * 1024;
386 /* don't overwrite the disk labels on sparc */
390 return zero_blocks(fd, start, len);
393 static int zero_dev_end(int fd, u64 dev_size)
395 size_t len = 2 * 1024 * 1024;
396 off_t start = dev_size - len;
398 return zero_blocks(fd, start, len);
401 int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
402 struct btrfs_root *root, int fd, u64 block_count,
403 u32 io_width, u32 io_align, u32 sectorsize)
405 struct btrfs_super_block *disk_super;
406 struct btrfs_super_block *super = &root->fs_info->super_copy;
407 struct btrfs_device device;
408 struct btrfs_dev_item *dev_item;
414 buf = malloc(sectorsize);
415 BUG_ON(sizeof(*disk_super) > sectorsize);
416 memset(buf, 0, sectorsize);
418 disk_super = (struct btrfs_super_block *)buf;
419 dev_item = &disk_super->dev_item;
421 uuid_generate(device.uuid);
424 device.io_width = io_width;
425 device.io_align = io_align;
426 device.sector_size = sectorsize;
428 device.total_bytes = block_count;
429 device.bytes_used = 0;
431 ret = btrfs_add_device(trans, root, &device);
434 total_bytes = btrfs_super_total_bytes(super) + block_count;
435 btrfs_set_super_total_bytes(super, total_bytes);
437 num_devs = btrfs_super_num_devices(super) + 1;
438 btrfs_set_super_num_devices(super, num_devs);
440 memcpy(disk_super, super, sizeof(*disk_super));
442 printf("adding device id %llu\n", (unsigned long long)device.devid);
443 btrfs_set_stack_device_id(dev_item, device.devid);
444 btrfs_set_stack_device_type(dev_item, device.type);
445 btrfs_set_stack_device_io_align(dev_item, device.io_align);
446 btrfs_set_stack_device_io_width(dev_item, device.io_width);
447 btrfs_set_stack_device_sector_size(dev_item, device.sector_size);
448 btrfs_set_stack_device_total_bytes(dev_item, device.total_bytes);
449 btrfs_set_stack_device_bytes_used(dev_item, device.bytes_used);
450 memcpy(&dev_item->uuid, device.uuid, BTRFS_DEV_UUID_SIZE);
452 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
453 BUG_ON(ret != sectorsize);
459 int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret)
465 ret = fstat(fd, &st);
467 fprintf(stderr, "unable to stat %s\n", file);
471 block_count = device_size(fd, &st);
472 if (block_count == 0) {
473 fprintf(stderr, "unable to find %s size\n", file);
478 if (block_count < 256 * 1024 * 1024) {
479 fprintf(stderr, "device %s is too small\n", file);
482 ret = zero_dev_start(fd);
484 fprintf(stderr, "failed to zero device start %d\n", ret);
489 ret = zero_dev_end(fd, block_count);
491 fprintf(stderr, "failed to zero device end %d\n", ret);
495 *block_count_ret = block_count;
499 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
500 struct btrfs_root *root, u64 objectid)
503 struct btrfs_inode_item inode_item;
505 memset(&inode_item, 0, sizeof(inode_item));
506 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
507 btrfs_set_stack_inode_size(&inode_item, 0);
508 btrfs_set_stack_inode_nlink(&inode_item, 1);
509 btrfs_set_stack_inode_nblocks(&inode_item, 1);
510 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0555);
512 if (root->fs_info->tree_root == root)
513 btrfs_set_super_root_dir(&root->fs_info->super_copy, objectid);
515 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
519 ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid);
523 btrfs_set_root_dirid(&root->root_item, objectid);
530 * returns 1 if the device was mounted, < 0 on error or 0 if everything
531 * is safe to continue. TODO, this should also scan multi-device filesystems
533 int check_mounted(char *file)
543 if ((f = setmntent ("/proc/mounts", "r")) == NULL)
546 if (stat(file, &st_buf) < 0) {
549 if (S_ISBLK(st_buf.st_mode)) {
550 file_rdev = st_buf.st_rdev;
552 file_dev = st_buf.st_dev;
553 file_ino = st_buf.st_ino;
557 while ((mnt = getmntent (f)) != NULL) {
558 if (strcmp(file, mnt->mnt_fsname) == 0)
561 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
562 if (S_ISBLK(st_buf.st_mode)) {
563 if (file_rdev && (file_rdev == st_buf.st_rdev))
565 } else if (file_dev && ((file_dev == st_buf.st_dev) &&
566 (file_ino == st_buf.st_ino))) {
573 /* found an entry in mnt table */
582 struct list_head list;
586 int btrfs_register_one_device(char *fname)
588 struct btrfs_ioctl_vol_args args;
592 fd = open("/dev/btrfs-control", O_RDONLY);
595 strcpy(args.name, fname);
596 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
601 int btrfs_scan_one_dir(char *dirname, int run_ioctl)
604 struct dirent *dirent;
605 struct pending_dir *pending;
612 struct list_head pending_list;
613 struct btrfs_fs_devices *tmp_devices;
616 INIT_LIST_HEAD(&pending_list);
618 pending = malloc(sizeof(*pending));
621 strcpy(pending->name, dirname);
624 dirname_len = strlen(pending->name);
626 fullpath = malloc(pathlen);
627 dirname = pending->name;
633 dirp = opendir(dirname);
635 fprintf(stderr, "Unable to open /sys/block for scanning\n");
639 dirent = readdir(dirp);
642 if (dirent->d_name[0] == '.')
644 if (dirname_len + strlen(dirent->d_name) + 2 > pathlen) {
648 snprintf(fullpath, pathlen, "%s/%s", dirname, dirent->d_name);
649 ret = lstat(fullpath, &st);
651 fprintf(stderr, "failed to stat %s\n", fullpath);
654 if (S_ISLNK(st.st_mode))
656 if (S_ISDIR(st.st_mode)) {
657 struct pending_dir *next = malloc(sizeof(*next));
662 strcpy(next->name, fullpath);
663 list_add_tail(&next->list, &pending_list);
665 if (!S_ISBLK(st.st_mode)) {
668 fd = open(fullpath, O_RDONLY);
670 fprintf(stderr, "failed to read %s\n", fullpath);
673 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
675 BTRFS_SUPER_INFO_OFFSET);
676 if (ret == 0 && run_ioctl > 0) {
677 btrfs_register_one_device(fullpath);
681 if (!list_empty(&pending_list)) {
683 pending = list_entry(pending_list.next, struct pending_dir,
685 list_del(&pending->list);
696 int btrfs_scan_for_fsid(struct btrfs_fs_devices *fs_devices, u64 total_devs,
699 return btrfs_scan_one_dir("/dev", run_ioctls);