Add btrfs-list for listing subvolumes
[btrfs-progs-unstable/devel.git] / utils.c
blob2f4c6e1480e36e33d2a50d3679252bf6c8316557
1 /*
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
20 #define __USE_XOPEN2K
21 #include <stdio.h>
22 #include <stdlib.h>
23 #ifndef __CHECKER__
24 #include <sys/ioctl.h>
25 #include <sys/mount.h>
26 #endif
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <uuid/uuid.h>
30 #include <dirent.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <mntent.h>
34 #include "kerncompat.h"
35 #include "radix-tree.h"
36 #include "ctree.h"
37 #include "disk-io.h"
38 #include "transaction.h"
39 #include "crc32c.h"
40 #include "utils.h"
41 #include "volumes.h"
42 #include "ioctl.h"
44 #ifdef __CHECKER__
45 #define BLKGETSIZE64 0
46 static inline int ioctl(int fd, int define, u64 *size) { return 0; }
47 #endif
49 static u64 reference_root_table[] = {
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,
55 [6] = BTRFS_CSUM_TREE_OBJECTID,
58 int make_btrfs(int fd, const char *device, const char *label,
59 u64 blocks[7], u64 num_bytes, u32 nodesize,
60 u32 leafsize, u32 sectorsize, u32 stripesize)
62 struct btrfs_super_block super;
63 struct extent_buffer *buf;
64 struct btrfs_root_item root_item;
65 struct btrfs_disk_key disk_key;
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;
71 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
72 u8 *ptr;
73 int i;
74 int ret;
75 u32 itemoff;
76 u32 nritems = 0;
77 u64 first_free;
78 u64 ref_root;
79 u32 array_size;
80 u32 item_size;
82 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
83 first_free &= ~((u64)sectorsize - 1);
85 memset(&super, 0, sizeof(super));
87 num_bytes = (num_bytes / sectorsize) * sectorsize;
88 uuid_generate(super.fsid);
89 uuid_generate(super.dev_item.uuid);
90 uuid_generate(chunk_tree_uuid);
92 btrfs_set_super_bytenr(&super, blocks[0]);
93 btrfs_set_super_num_devices(&super, 1);
94 strncpy((char *)&super.magic, BTRFS_MAGIC, sizeof(super.magic));
95 btrfs_set_super_generation(&super, 1);
96 btrfs_set_super_root(&super, blocks[1]);
97 btrfs_set_super_chunk_root(&super, blocks[3]);
98 btrfs_set_super_total_bytes(&super, num_bytes);
99 btrfs_set_super_bytes_used(&super, 6 * leafsize);
100 btrfs_set_super_sectorsize(&super, sectorsize);
101 btrfs_set_super_leafsize(&super, leafsize);
102 btrfs_set_super_nodesize(&super, nodesize);
103 btrfs_set_super_stripesize(&super, stripesize);
104 btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
105 btrfs_set_super_chunk_root_generation(&super, 1);
106 if (label)
107 strcpy(super.label, label);
109 buf = malloc(sizeof(*buf) + max(sectorsize, leafsize));
111 /* create the tree of root objects */
112 memset(buf->data, 0, leafsize);
113 buf->len = leafsize;
114 btrfs_set_header_bytenr(buf, blocks[1]);
115 btrfs_set_header_nritems(buf, 4);
116 btrfs_set_header_generation(buf, 1);
117 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
118 btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
119 write_extent_buffer(buf, super.fsid, (unsigned long)
120 btrfs_header_fsid(buf), BTRFS_FSID_SIZE);
122 write_extent_buffer(buf, chunk_tree_uuid, (unsigned long)
123 btrfs_header_chunk_tree_uuid(buf),
124 BTRFS_UUID_SIZE);
126 /* create the items for the root tree */
127 memset(&root_item, 0, sizeof(root_item));
128 inode_item = &root_item.inode;
129 btrfs_set_stack_inode_generation(inode_item, 1);
130 btrfs_set_stack_inode_size(inode_item, 3);
131 btrfs_set_stack_inode_nlink(inode_item, 1);
132 btrfs_set_stack_inode_nbytes(inode_item, leafsize);
133 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
134 btrfs_set_root_refs(&root_item, 1);
135 btrfs_set_root_used(&root_item, leafsize);
136 btrfs_set_root_generation(&root_item, 1);
138 memset(&disk_key, 0, sizeof(disk_key));
139 btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
140 btrfs_set_disk_key_offset(&disk_key, 0);
141 nritems = 0;
143 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
144 btrfs_set_root_bytenr(&root_item, blocks[2]);
145 btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_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),
149 sizeof(root_item));
150 write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
151 nritems), sizeof(root_item));
152 nritems++;
154 itemoff = itemoff - sizeof(root_item);
155 btrfs_set_root_bytenr(&root_item, blocks[4]);
156 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
157 btrfs_set_item_key(buf, &disk_key, nritems);
158 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
159 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
160 sizeof(root_item));
161 write_extent_buffer(buf, &root_item,
162 btrfs_item_ptr_offset(buf, nritems),
163 sizeof(root_item));
164 nritems++;
166 itemoff = itemoff - sizeof(root_item);
167 btrfs_set_root_bytenr(&root_item, blocks[5]);
168 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
169 btrfs_set_item_key(buf, &disk_key, nritems);
170 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
171 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
172 sizeof(root_item));
173 write_extent_buffer(buf, &root_item,
174 btrfs_item_ptr_offset(buf, nritems),
175 sizeof(root_item));
176 nritems++;
178 itemoff = itemoff - sizeof(root_item);
179 btrfs_set_root_bytenr(&root_item, blocks[6]);
180 btrfs_set_disk_key_objectid(&disk_key, BTRFS_CSUM_TREE_OBJECTID);
181 btrfs_set_item_key(buf, &disk_key, nritems);
182 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
183 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
184 sizeof(root_item));
185 write_extent_buffer(buf, &root_item,
186 btrfs_item_ptr_offset(buf, nritems),
187 sizeof(root_item));
188 nritems++;
191 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
192 ret = pwrite(fd, buf->data, leafsize, blocks[1]);
193 BUG_ON(ret != leafsize);
195 /* create the items for the extent tree */
196 nritems = 0;
197 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize);
198 for (i = 1; i < 7; i++) {
199 BUG_ON(blocks[i] < first_free);
200 BUG_ON(blocks[i] < blocks[i - 1]);
202 /* create extent item */
203 itemoff -= sizeof(struct btrfs_extent_item) +
204 sizeof(struct btrfs_tree_block_info);
205 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
206 btrfs_set_disk_key_offset(&disk_key, leafsize);
207 btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_ITEM_KEY);
208 btrfs_set_item_key(buf, &disk_key, nritems);
209 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
210 itemoff);
211 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
212 sizeof(struct btrfs_extent_item) +
213 sizeof(struct btrfs_tree_block_info));
214 extent_item = btrfs_item_ptr(buf, nritems,
215 struct btrfs_extent_item);
216 btrfs_set_extent_refs(buf, extent_item, 1);
217 btrfs_set_extent_generation(buf, extent_item, 1);
218 btrfs_set_extent_flags(buf, extent_item,
219 BTRFS_EXTENT_FLAG_TREE_BLOCK);
220 nritems++;
222 /* create extent ref */
223 ref_root = reference_root_table[i];
224 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
225 btrfs_set_disk_key_offset(&disk_key, ref_root);
226 btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
227 btrfs_set_item_key(buf, &disk_key, nritems);
228 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
229 itemoff);
230 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), 0);
231 nritems++;
233 btrfs_set_header_bytenr(buf, blocks[2]);
234 btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
235 btrfs_set_header_nritems(buf, nritems);
236 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
237 ret = pwrite(fd, buf->data, leafsize, blocks[2]);
238 BUG_ON(ret != leafsize);
240 /* create the chunk tree */
241 nritems = 0;
242 item_size = sizeof(*dev_item);
243 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - item_size;
245 /* first device 1 (there is no device 0) */
246 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
247 btrfs_set_disk_key_offset(&disk_key, 1);
248 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
249 btrfs_set_item_key(buf, &disk_key, nritems);
250 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
251 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), item_size);
253 dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
254 btrfs_set_device_id(buf, dev_item, 1);
255 btrfs_set_device_generation(buf, dev_item, 0);
256 btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
257 btrfs_set_device_bytes_used(buf, dev_item,
258 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
259 btrfs_set_device_io_align(buf, dev_item, sectorsize);
260 btrfs_set_device_io_width(buf, dev_item, sectorsize);
261 btrfs_set_device_sector_size(buf, dev_item, sectorsize);
262 btrfs_set_device_type(buf, dev_item, 0);
264 write_extent_buffer(buf, super.dev_item.uuid,
265 (unsigned long)btrfs_device_uuid(dev_item),
266 BTRFS_UUID_SIZE);
267 write_extent_buffer(buf, super.fsid,
268 (unsigned long)btrfs_device_fsid(dev_item),
269 BTRFS_UUID_SIZE);
270 read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
271 sizeof(*dev_item));
273 nritems++;
274 item_size = btrfs_chunk_item_size(1);
275 itemoff = itemoff - item_size;
277 /* then we have chunk 0 */
278 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
279 btrfs_set_disk_key_offset(&disk_key, 0);
280 btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
281 btrfs_set_item_key(buf, &disk_key, nritems);
282 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
283 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), item_size);
285 chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
286 btrfs_set_chunk_length(buf, chunk, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
287 btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
288 btrfs_set_chunk_stripe_len(buf, chunk, 64 * 1024);
289 btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
290 btrfs_set_chunk_io_align(buf, chunk, sectorsize);
291 btrfs_set_chunk_io_width(buf, chunk, sectorsize);
292 btrfs_set_chunk_sector_size(buf, chunk, sectorsize);
293 btrfs_set_chunk_num_stripes(buf, chunk, 1);
294 btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
295 btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
296 nritems++;
298 write_extent_buffer(buf, super.dev_item.uuid,
299 (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
300 BTRFS_UUID_SIZE);
302 /* copy the key for the chunk to the system array */
303 ptr = super.sys_chunk_array;
304 array_size = sizeof(disk_key);
306 memcpy(ptr, &disk_key, sizeof(disk_key));
307 ptr += sizeof(disk_key);
309 /* copy the chunk to the system array */
310 read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
311 array_size += item_size;
312 ptr += item_size;
313 btrfs_set_super_sys_array_size(&super, array_size);
315 btrfs_set_header_bytenr(buf, blocks[3]);
316 btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
317 btrfs_set_header_nritems(buf, nritems);
318 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
319 ret = pwrite(fd, buf->data, leafsize, blocks[3]);
321 /* create the device tree */
322 nritems = 0;
323 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
324 sizeof(struct btrfs_dev_extent);
326 btrfs_set_disk_key_objectid(&disk_key, 1);
327 btrfs_set_disk_key_offset(&disk_key, 0);
328 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
329 btrfs_set_item_key(buf, &disk_key, nritems);
330 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
331 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
332 sizeof(struct btrfs_dev_extent));
333 dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
334 btrfs_set_dev_extent_chunk_tree(buf, dev_extent,
335 BTRFS_CHUNK_TREE_OBJECTID);
336 btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
337 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
338 btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
340 write_extent_buffer(buf, chunk_tree_uuid,
341 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
342 BTRFS_UUID_SIZE);
344 btrfs_set_dev_extent_length(buf, dev_extent,
345 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
346 nritems++;
348 btrfs_set_header_bytenr(buf, blocks[4]);
349 btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
350 btrfs_set_header_nritems(buf, nritems);
351 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
352 ret = pwrite(fd, buf->data, leafsize, blocks[4]);
354 /* create the FS root */
355 btrfs_set_header_bytenr(buf, blocks[5]);
356 btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
357 btrfs_set_header_nritems(buf, 0);
358 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
359 ret = pwrite(fd, buf->data, leafsize, blocks[5]);
360 BUG_ON(ret != leafsize);
362 /* finally create the csum root */
363 btrfs_set_header_bytenr(buf, blocks[6]);
364 btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
365 btrfs_set_header_nritems(buf, 0);
366 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
367 ret = pwrite(fd, buf->data, leafsize, blocks[6]);
368 BUG_ON(ret != leafsize);
370 /* and write out the super block */
371 BUG_ON(sizeof(super) > sectorsize);
372 memset(buf->data, 0, sectorsize);
373 memcpy(buf->data, &super, sizeof(super));
374 buf->len = sectorsize;
375 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
376 ret = pwrite(fd, buf->data, sectorsize, blocks[0]);
377 BUG_ON(ret != sectorsize);
380 free(buf);
381 return 0;
384 static u64 device_size(int fd, struct stat *st)
386 u64 size;
387 if (S_ISREG(st->st_mode)) {
388 return st->st_size;
390 if (!S_ISBLK(st->st_mode)) {
391 return 0;
393 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
394 return size;
396 return 0;
399 static int zero_blocks(int fd, off_t start, size_t len)
401 char *buf = malloc(len);
402 int ret = 0;
403 ssize_t written;
405 if (!buf)
406 return -ENOMEM;
407 memset(buf, 0, len);
408 written = pwrite(fd, buf, len, start);
409 if (written != len)
410 ret = -EIO;
411 free(buf);
412 return ret;
415 static int zero_dev_start(int fd)
417 off_t start = 0;
418 size_t len = 2 * 1024 * 1024;
420 #ifdef __sparc__
421 /* don't overwrite the disk labels on sparc */
422 start = 1024;
423 len -= 1024;
424 #endif
425 return zero_blocks(fd, start, len);
428 static int zero_dev_end(int fd, u64 dev_size)
430 size_t len = 2 * 1024 * 1024;
431 off_t start = dev_size - len;
433 return zero_blocks(fd, start, len);
436 int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
437 struct btrfs_root *root, int fd, char *path,
438 u64 block_count, u32 io_width, u32 io_align,
439 u32 sectorsize)
441 struct btrfs_super_block *disk_super;
442 struct btrfs_super_block *super = &root->fs_info->super_copy;
443 struct btrfs_device *device;
444 struct btrfs_dev_item *dev_item;
445 char *buf;
446 u64 total_bytes;
447 u64 num_devs;
448 int ret;
450 device = kmalloc(sizeof(*device), GFP_NOFS);
451 if (!device)
452 return -ENOMEM;
453 buf = kmalloc(sectorsize, GFP_NOFS);
454 if (!buf) {
455 kfree(device);
456 return -ENOMEM;
458 BUG_ON(sizeof(*disk_super) > sectorsize);
459 memset(buf, 0, sectorsize);
461 disk_super = (struct btrfs_super_block *)buf;
462 dev_item = &disk_super->dev_item;
464 uuid_generate(device->uuid);
465 device->devid = 0;
466 device->type = 0;
467 device->io_width = io_width;
468 device->io_align = io_align;
469 device->sector_size = sectorsize;
470 device->fd = fd;
471 device->writeable = 1;
472 device->total_bytes = block_count;
473 device->bytes_used = 0;
474 device->total_ios = 0;
475 device->dev_root = root->fs_info->dev_root;
477 ret = btrfs_add_device(trans, root, device);
478 BUG_ON(ret);
480 total_bytes = btrfs_super_total_bytes(super) + block_count;
481 btrfs_set_super_total_bytes(super, total_bytes);
483 num_devs = btrfs_super_num_devices(super) + 1;
484 btrfs_set_super_num_devices(super, num_devs);
486 memcpy(disk_super, super, sizeof(*disk_super));
488 printf("adding device %s id %llu\n", path,
489 (unsigned long long)device->devid);
491 btrfs_set_super_bytenr(disk_super, BTRFS_SUPER_INFO_OFFSET);
492 btrfs_set_stack_device_id(dev_item, device->devid);
493 btrfs_set_stack_device_type(dev_item, device->type);
494 btrfs_set_stack_device_io_align(dev_item, device->io_align);
495 btrfs_set_stack_device_io_width(dev_item, device->io_width);
496 btrfs_set_stack_device_sector_size(dev_item, device->sector_size);
497 btrfs_set_stack_device_total_bytes(dev_item, device->total_bytes);
498 btrfs_set_stack_device_bytes_used(dev_item, device->bytes_used);
499 memcpy(&dev_item->uuid, device->uuid, BTRFS_UUID_SIZE);
501 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
502 BUG_ON(ret != sectorsize);
504 kfree(buf);
505 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
506 device->fs_devices = root->fs_info->fs_devices;
507 return 0;
510 int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret)
512 u64 block_count;
513 u64 bytenr;
514 struct stat st;
515 int i, ret;
517 ret = fstat(fd, &st);
518 if (ret < 0) {
519 fprintf(stderr, "unable to stat %s\n", file);
520 exit(1);
523 block_count = device_size(fd, &st);
524 if (block_count == 0) {
525 fprintf(stderr, "unable to find %s size\n", file);
526 exit(1);
528 zero_end = 1;
530 if (block_count < 256 * 1024 * 1024) {
531 fprintf(stderr, "device %s is too small "
532 "(must be at least 256 MB)\n", file);
533 exit(1);
535 ret = zero_dev_start(fd);
536 if (ret) {
537 fprintf(stderr, "failed to zero device start %d\n", ret);
538 exit(1);
541 for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
542 bytenr = btrfs_sb_offset(i);
543 if (bytenr >= block_count)
544 break;
545 zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
548 if (zero_end) {
549 ret = zero_dev_end(fd, block_count);
550 if (ret) {
551 fprintf(stderr, "failed to zero device end %d\n", ret);
552 exit(1);
555 *block_count_ret = block_count;
556 return 0;
559 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
560 struct btrfs_root *root, u64 objectid)
562 int ret;
563 struct btrfs_inode_item inode_item;
565 memset(&inode_item, 0, sizeof(inode_item));
566 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
567 btrfs_set_stack_inode_size(&inode_item, 0);
568 btrfs_set_stack_inode_nlink(&inode_item, 1);
569 btrfs_set_stack_inode_nbytes(&inode_item, root->leafsize);
570 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0555);
572 if (root->fs_info->tree_root == root)
573 btrfs_set_super_root_dir(&root->fs_info->super_copy, objectid);
575 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
576 if (ret)
577 goto error;
579 ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0);
580 if (ret)
581 goto error;
583 btrfs_set_root_dirid(&root->root_item, objectid);
584 ret = 0;
585 error:
586 return ret;
590 * returns 1 if the device was mounted, < 0 on error or 0 if everything
591 * is safe to continue. TODO, this should also scan multi-device filesystems
593 int check_mounted(char *file)
595 struct mntent *mnt;
596 struct stat st_buf;
597 dev_t file_dev = 0;
598 dev_t file_rdev = 0;
599 ino_t file_ino = 0;
600 FILE *f;
601 int ret = 0;
603 if ((f = setmntent ("/proc/mounts", "r")) == NULL)
604 return -errno;
606 if (stat(file, &st_buf) < 0) {
607 return -errno;
608 } else {
609 if (S_ISBLK(st_buf.st_mode)) {
610 file_rdev = st_buf.st_rdev;
611 } else {
612 file_dev = st_buf.st_dev;
613 file_ino = st_buf.st_ino;
617 while ((mnt = getmntent (f)) != NULL) {
618 if (strcmp(file, mnt->mnt_fsname) == 0)
619 break;
621 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
622 if (S_ISBLK(st_buf.st_mode)) {
623 if (file_rdev && (file_rdev == st_buf.st_rdev))
624 break;
625 } else if (file_dev && ((file_dev == st_buf.st_dev) &&
626 (file_ino == st_buf.st_ino))) {
627 break;
632 if (mnt) {
633 /* found an entry in mnt table */
634 ret = 1;
637 endmntent (f);
638 return ret;
641 struct pending_dir {
642 struct list_head list;
643 char name[256];
646 void btrfs_register_one_device(char *fname)
648 struct btrfs_ioctl_vol_args args;
649 int fd;
650 int ret;
652 fd = open("/dev/btrfs-control", O_RDONLY);
653 if (fd < 0) {
654 fprintf(stderr, "failed to open /dev/btrfs-control "
655 "skipping device registration\n");
656 return;
658 strcpy(args.name, fname);
659 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
660 close(fd);
663 int btrfs_scan_one_dir(char *dirname, int run_ioctl)
665 DIR *dirp = NULL;
666 struct dirent *dirent;
667 struct pending_dir *pending;
668 struct stat st;
669 int ret;
670 int fd;
671 int dirname_len;
672 int pathlen;
673 char *fullpath;
674 struct list_head pending_list;
675 struct btrfs_fs_devices *tmp_devices;
676 u64 num_devices;
678 INIT_LIST_HEAD(&pending_list);
680 pending = malloc(sizeof(*pending));
681 if (!pending)
682 return -ENOMEM;
683 strcpy(pending->name, dirname);
685 again:
686 dirname_len = strlen(pending->name);
687 pathlen = 1024;
688 fullpath = malloc(pathlen);
689 dirname = pending->name;
691 if (!fullpath) {
692 ret = -ENOMEM;
693 goto fail;
695 dirp = opendir(dirname);
696 if (!dirp) {
697 fprintf(stderr, "Unable to open /sys/block for scanning\n");
698 return -ENOENT;
700 while(1) {
701 dirent = readdir(dirp);
702 if (!dirent)
703 break;
704 if (dirent->d_name[0] == '.')
705 continue;
706 if (dirname_len + strlen(dirent->d_name) + 2 > pathlen) {
707 ret = -EFAULT;
708 goto fail;
710 snprintf(fullpath, pathlen, "%s/%s", dirname, dirent->d_name);
711 ret = lstat(fullpath, &st);
712 if (ret < 0) {
713 fprintf(stderr, "failed to stat %s\n", fullpath);
714 continue;
716 if (S_ISLNK(st.st_mode))
717 continue;
718 if (S_ISDIR(st.st_mode)) {
719 struct pending_dir *next = malloc(sizeof(*next));
720 if (!next) {
721 ret = -ENOMEM;
722 goto fail;
724 strcpy(next->name, fullpath);
725 list_add_tail(&next->list, &pending_list);
727 if (!S_ISBLK(st.st_mode)) {
728 continue;
730 fd = open(fullpath, O_RDONLY);
731 if (fd < 0) {
732 fprintf(stderr, "failed to read %s\n", fullpath);
733 continue;
735 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
736 &num_devices,
737 BTRFS_SUPER_INFO_OFFSET);
738 if (ret == 0 && run_ioctl > 0) {
739 btrfs_register_one_device(fullpath);
741 close(fd);
743 if (!list_empty(&pending_list)) {
744 free(pending);
745 pending = list_entry(pending_list.next, struct pending_dir,
746 list);
747 list_del(&pending->list);
748 closedir(dirp);
749 goto again;
751 ret = 0;
752 fail:
753 free(pending);
754 if (dirp)
755 closedir(dirp);
756 return ret;
759 int btrfs_scan_for_fsid(struct btrfs_fs_devices *fs_devices, u64 total_devs,
760 int run_ioctls)
762 return btrfs_scan_one_dir("/dev", run_ioctls);
765 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
766 int super_offset)
768 struct btrfs_super_block *disk_super;
769 char *buf;
770 int ret = 0;
772 buf = malloc(BTRFS_SUPER_INFO_SIZE);
773 if (!buf) {
774 ret = -ENOMEM;
775 goto out;
777 ret = pread(fd, buf, BTRFS_SUPER_INFO_SIZE, super_offset);
778 if (ret != BTRFS_SUPER_INFO_SIZE)
779 goto brelse;
781 ret = 0;
782 disk_super = (struct btrfs_super_block *)buf;
783 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
784 sizeof(disk_super->magic)))
785 goto brelse;
787 if (!memcmp(disk_super->fsid, root->fs_info->super_copy.fsid,
788 BTRFS_FSID_SIZE))
789 ret = 1;
790 brelse:
791 free(buf);
792 out:
793 return ret;
796 static char *size_strs[] = { "", "KB", "MB", "GB", "TB",
797 "PB", "EB", "ZB", "YB"};
798 char *pretty_sizes(u64 size)
800 int num_divs = 0;
801 u64 last_size = size;
802 u64 fract_size = size;
803 float fraction;
804 char *pretty;
806 while(size > 0) {
807 fract_size = last_size;
808 last_size = size;
809 size /= 1024;
810 num_divs++;
812 if (num_divs == 0)
813 num_divs = 1;
814 if (num_divs > ARRAY_SIZE(size_strs))
815 return NULL;
817 fraction = (float)fract_size / 1024;
818 pretty = malloc(16);
819 sprintf(pretty, "%.2f%s", fraction, size_strs[num_divs-1]);
820 return pretty;