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
, const char *device
, const char *label
,
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
;
71 u8 chunk_tree_uuid
[BTRFS_UUID_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
, first_free
+ 5 * 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_chunk_root_generation(&super
, 1);
106 strcpy(super
.label
, label
);
108 buf
= malloc(sizeof(*buf
) + max(sectorsize
, leafsize
));
110 /* create the tree of root objects */
111 memset(buf
->data
, 0, leafsize
);
113 btrfs_set_header_bytenr(buf
, blocks
[1]);
114 btrfs_set_header_nritems(buf
, 3);
115 btrfs_set_header_generation(buf
, 1);
116 btrfs_set_header_owner(buf
, BTRFS_ROOT_TREE_OBJECTID
);
117 write_extent_buffer(buf
, super
.fsid
, (unsigned long)
118 btrfs_header_fsid(buf
), BTRFS_FSID_SIZE
);
120 write_extent_buffer(buf
, chunk_tree_uuid
, (unsigned long)
121 btrfs_header_chunk_tree_uuid(buf
),
124 /* create the items for the root tree */
125 memset(&root_item
, 0, sizeof(root_item
));
126 inode_item
= &root_item
.inode
;
127 btrfs_set_stack_inode_generation(inode_item
, 1);
128 btrfs_set_stack_inode_size(inode_item
, 3);
129 btrfs_set_stack_inode_nlink(inode_item
, 1);
130 btrfs_set_stack_inode_nbytes(inode_item
, leafsize
);
131 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
132 btrfs_set_root_refs(&root_item
, 1);
133 btrfs_set_root_used(&root_item
, leafsize
);
134 btrfs_set_root_generation(&root_item
, 1);
136 memset(&disk_key
, 0, sizeof(disk_key
));
137 btrfs_set_disk_key_type(&disk_key
, BTRFS_ROOT_ITEM_KEY
);
138 btrfs_set_disk_key_offset(&disk_key
, 0);
141 itemoff
= __BTRFS_LEAF_DATA_SIZE(leafsize
) - sizeof(root_item
);
142 btrfs_set_root_bytenr(&root_item
, blocks
[2]);
143 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_EXTENT_TREE_OBJECTID
);
144 btrfs_set_item_key(buf
, &disk_key
, nritems
);
145 btrfs_set_item_offset(buf
, btrfs_item_nr(buf
, nritems
), itemoff
);
146 btrfs_set_item_size(buf
, btrfs_item_nr(buf
, nritems
),
148 write_extent_buffer(buf
, &root_item
, btrfs_item_ptr_offset(buf
,
149 nritems
), sizeof(root_item
));
152 itemoff
= itemoff
- sizeof(root_item
);
153 btrfs_set_root_bytenr(&root_item
, blocks
[4]);
154 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_DEV_TREE_OBJECTID
);
155 btrfs_set_item_key(buf
, &disk_key
, nritems
);
156 btrfs_set_item_offset(buf
, btrfs_item_nr(buf
, nritems
), itemoff
);
157 btrfs_set_item_size(buf
, btrfs_item_nr(buf
, nritems
),
159 write_extent_buffer(buf
, &root_item
,
160 btrfs_item_ptr_offset(buf
, nritems
),
164 itemoff
= itemoff
- sizeof(root_item
);
165 btrfs_set_root_bytenr(&root_item
, blocks
[5]);
166 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_FS_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
,
172 btrfs_item_ptr_offset(buf
, nritems
),
177 csum_tree_block(NULL
, buf
, 0);
178 ret
= pwrite(fd
, buf
->data
, leafsize
, blocks
[1]);
179 BUG_ON(ret
!= leafsize
);
181 /* create the items for the extent tree */
183 itemoff
= __BTRFS_LEAF_DATA_SIZE(leafsize
) -
184 sizeof(struct btrfs_extent_item
);
185 btrfs_set_disk_key_objectid(&disk_key
, 0);
186 btrfs_set_disk_key_offset(&disk_key
, first_free
);
187 btrfs_set_disk_key_type(&disk_key
, BTRFS_EXTENT_ITEM_KEY
);
188 btrfs_set_item_key(buf
, &disk_key
, nritems
);
189 btrfs_set_item_offset(buf
, btrfs_item_nr(buf
, nritems
), itemoff
);
190 btrfs_set_item_size(buf
, btrfs_item_nr(buf
, nritems
),
191 sizeof(struct btrfs_extent_item
));
192 extent_item
= btrfs_item_ptr(buf
, nritems
, struct btrfs_extent_item
);
193 btrfs_set_extent_refs(buf
, extent_item
, 1);
195 for (i
= 1; i
< 6; i
++) {
196 BUG_ON(blocks
[i
] < first_free
);
197 BUG_ON(blocks
[i
] < blocks
[i
- 1]);
199 /* create extent item */
200 itemoff
= itemoff
- sizeof(struct btrfs_extent_item
);
201 btrfs_set_disk_key_objectid(&disk_key
, blocks
[i
]);
202 btrfs_set_disk_key_offset(&disk_key
, leafsize
);
203 btrfs_set_disk_key_type(&disk_key
, BTRFS_EXTENT_ITEM_KEY
);
204 btrfs_set_item_key(buf
, &disk_key
, nritems
);
205 btrfs_set_item_offset(buf
, btrfs_item_nr(buf
, nritems
),
207 btrfs_set_item_size(buf
, btrfs_item_nr(buf
, nritems
),
208 sizeof(struct btrfs_extent_item
));
209 extent_item
= btrfs_item_ptr(buf
, nritems
,
210 struct btrfs_extent_item
);
211 btrfs_set_extent_refs(buf
, extent_item
, 1);
214 /* create extent ref */
215 ref_root
= reference_root_table
[i
];
216 itemoff
= itemoff
- sizeof(struct btrfs_extent_ref
);
217 btrfs_set_disk_key_objectid(&disk_key
, blocks
[i
]);
218 btrfs_set_disk_key_offset(&disk_key
, blocks
[i
]);
219 btrfs_set_disk_key_type(&disk_key
, BTRFS_EXTENT_REF_KEY
);
220 btrfs_set_item_key(buf
, &disk_key
, nritems
);
221 btrfs_set_item_offset(buf
, btrfs_item_nr(buf
, nritems
),
223 btrfs_set_item_size(buf
, btrfs_item_nr(buf
, nritems
),
224 sizeof(struct btrfs_extent_ref
));
225 extent_ref
= btrfs_item_ptr(buf
, nritems
,
226 struct btrfs_extent_ref
);
227 btrfs_set_ref_root(buf
, extent_ref
, ref_root
);
228 btrfs_set_ref_generation(buf
, extent_ref
, 1);
229 btrfs_set_ref_objectid(buf
, extent_ref
, 0);
230 btrfs_set_ref_num_refs(buf
, extent_ref
, 1);
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(NULL
, buf
, 0);
237 ret
= pwrite(fd
, buf
->data
, leafsize
, blocks
[2]);
238 BUG_ON(ret
!= leafsize
);
240 /* create the chunk tree */
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_total_bytes(buf
, dev_item
, num_bytes
);
256 btrfs_set_device_bytes_used(buf
, dev_item
,
257 BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
258 btrfs_set_device_io_align(buf
, dev_item
, sectorsize
);
259 btrfs_set_device_io_width(buf
, dev_item
, sectorsize
);
260 btrfs_set_device_sector_size(buf
, dev_item
, sectorsize
);
261 btrfs_set_device_type(buf
, dev_item
, 0);
263 write_extent_buffer(buf
, super
.dev_item
.uuid
,
264 (unsigned long)btrfs_device_uuid(dev_item
),
266 read_extent_buffer(buf
, &super
.dev_item
, (unsigned long)dev_item
,
270 item_size
= btrfs_chunk_item_size(1);
271 itemoff
= itemoff
- item_size
;
273 /* then we have chunk 0 */
274 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
275 btrfs_set_disk_key_offset(&disk_key
, 0);
276 btrfs_set_disk_key_type(&disk_key
, BTRFS_CHUNK_ITEM_KEY
);
277 btrfs_set_item_key(buf
, &disk_key
, nritems
);
278 btrfs_set_item_offset(buf
, btrfs_item_nr(buf
, nritems
), itemoff
);
279 btrfs_set_item_size(buf
, btrfs_item_nr(buf
, nritems
), item_size
);
281 chunk
= btrfs_item_ptr(buf
, nritems
, struct btrfs_chunk
);
282 btrfs_set_chunk_length(buf
, chunk
, BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
283 btrfs_set_chunk_owner(buf
, chunk
, BTRFS_EXTENT_TREE_OBJECTID
);
284 btrfs_set_chunk_stripe_len(buf
, chunk
, 64 * 1024);
285 btrfs_set_chunk_type(buf
, chunk
, BTRFS_BLOCK_GROUP_SYSTEM
);
286 btrfs_set_chunk_io_align(buf
, chunk
, sectorsize
);
287 btrfs_set_chunk_io_width(buf
, chunk
, sectorsize
);
288 btrfs_set_chunk_sector_size(buf
, chunk
, sectorsize
);
289 btrfs_set_chunk_num_stripes(buf
, chunk
, 1);
290 btrfs_set_stripe_devid_nr(buf
, chunk
, 0, 1);
291 btrfs_set_stripe_offset_nr(buf
, chunk
, 0, 0);
294 write_extent_buffer(buf
, super
.dev_item
.uuid
,
295 (unsigned long)btrfs_stripe_dev_uuid(&chunk
->stripe
),
298 /* copy the key for the chunk to the system array */
299 ptr
= super
.sys_chunk_array
;
300 array_size
= sizeof(disk_key
);
302 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
303 ptr
+= sizeof(disk_key
);
305 /* copy the chunk to the system array */
306 read_extent_buffer(buf
, ptr
, (unsigned long)chunk
, item_size
);
307 array_size
+= item_size
;
309 btrfs_set_super_sys_array_size(&super
, array_size
);
311 btrfs_set_header_bytenr(buf
, blocks
[3]);
312 btrfs_set_header_owner(buf
, BTRFS_CHUNK_TREE_OBJECTID
);
313 btrfs_set_header_nritems(buf
, nritems
);
314 csum_tree_block(NULL
, buf
, 0);
315 ret
= pwrite(fd
, buf
->data
, leafsize
, blocks
[3]);
317 /* create the device tree */
319 itemoff
= __BTRFS_LEAF_DATA_SIZE(leafsize
) -
320 sizeof(struct btrfs_dev_extent
);
322 btrfs_set_disk_key_objectid(&disk_key
, 1);
323 btrfs_set_disk_key_offset(&disk_key
, 0);
324 btrfs_set_disk_key_type(&disk_key
, BTRFS_DEV_EXTENT_KEY
);
325 btrfs_set_item_key(buf
, &disk_key
, nritems
);
326 btrfs_set_item_offset(buf
, btrfs_item_nr(buf
, nritems
), itemoff
);
327 btrfs_set_item_size(buf
, btrfs_item_nr(buf
, nritems
),
328 sizeof(struct btrfs_dev_extent
));
329 dev_extent
= btrfs_item_ptr(buf
, nritems
, struct btrfs_dev_extent
);
330 btrfs_set_dev_extent_chunk_tree(buf
, dev_extent
,
331 BTRFS_CHUNK_TREE_OBJECTID
);
332 btrfs_set_dev_extent_chunk_objectid(buf
, dev_extent
,
333 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
334 btrfs_set_dev_extent_chunk_offset(buf
, dev_extent
, 0);
336 write_extent_buffer(buf
, chunk_tree_uuid
,
337 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent
),
340 btrfs_set_dev_extent_length(buf
, dev_extent
,
341 BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
344 btrfs_set_header_bytenr(buf
, blocks
[4]);
345 btrfs_set_header_owner(buf
, BTRFS_DEV_TREE_OBJECTID
);
346 btrfs_set_header_nritems(buf
, nritems
);
347 csum_tree_block(NULL
, buf
, 0);
348 ret
= pwrite(fd
, buf
->data
, leafsize
, blocks
[4]);
350 /* finally create the FS root */
351 btrfs_set_header_bytenr(buf
, blocks
[5]);
352 btrfs_set_header_owner(buf
, BTRFS_FS_TREE_OBJECTID
);
353 btrfs_set_header_nritems(buf
, 0);
354 csum_tree_block(NULL
, buf
, 0);
355 ret
= pwrite(fd
, buf
->data
, leafsize
, blocks
[5]);
356 BUG_ON(ret
!= leafsize
);
358 /* and write out the super block */
359 BUG_ON(sizeof(super
) > sectorsize
);
360 memset(buf
->data
, 0, sectorsize
);
361 memcpy(buf
->data
, &super
, sizeof(super
));
362 buf
->len
= sectorsize
;
363 csum_tree_block(NULL
, buf
, 0);
364 ret
= pwrite(fd
, buf
->data
, sectorsize
, blocks
[0]);
365 BUG_ON(ret
!= sectorsize
);
372 static u64
device_size(int fd
, struct stat
*st
)
375 if (S_ISREG(st
->st_mode
)) {
378 if (!S_ISBLK(st
->st_mode
)) {
381 if (ioctl(fd
, BLKGETSIZE64
, &size
) >= 0) {
387 static int zero_blocks(int fd
, off_t start
, size_t len
)
389 char *buf
= malloc(len
);
396 written
= pwrite(fd
, buf
, len
, start
);
403 static int zero_dev_start(int fd
)
406 size_t len
= 2 * 1024 * 1024;
409 /* don't overwrite the disk labels on sparc */
413 return zero_blocks(fd
, start
, len
);
416 static int zero_dev_end(int fd
, u64 dev_size
)
418 size_t len
= 2 * 1024 * 1024;
419 off_t start
= dev_size
- len
;
421 return zero_blocks(fd
, start
, len
);
424 int btrfs_add_to_fsid(struct btrfs_trans_handle
*trans
,
425 struct btrfs_root
*root
, int fd
, char *path
,
426 u64 block_count
, u32 io_width
, u32 io_align
,
429 struct btrfs_super_block
*disk_super
;
430 struct btrfs_super_block
*super
= &root
->fs_info
->super_copy
;
431 struct btrfs_device
*device
;
432 struct btrfs_dev_item
*dev_item
;
438 device
= kmalloc(sizeof(*device
), GFP_NOFS
);
441 buf
= kmalloc(sectorsize
, GFP_NOFS
);
446 BUG_ON(sizeof(*disk_super
) > sectorsize
);
447 memset(buf
, 0, sectorsize
);
449 disk_super
= (struct btrfs_super_block
*)buf
;
450 dev_item
= &disk_super
->dev_item
;
452 uuid_generate(device
->uuid
);
455 device
->io_width
= io_width
;
456 device
->io_align
= io_align
;
457 device
->sector_size
= sectorsize
;
459 device
->total_bytes
= block_count
;
460 device
->bytes_used
= 0;
461 device
->total_ios
= 0;
462 device
->dev_root
= root
->fs_info
->dev_root
;
464 ret
= btrfs_add_device(trans
, root
, device
);
467 total_bytes
= btrfs_super_total_bytes(super
) + block_count
;
468 btrfs_set_super_total_bytes(super
, total_bytes
);
470 num_devs
= btrfs_super_num_devices(super
) + 1;
471 btrfs_set_super_num_devices(super
, num_devs
);
473 memcpy(disk_super
, super
, sizeof(*disk_super
));
475 printf("adding device %s id %llu\n", path
,
476 (unsigned long long)device
->devid
);
478 btrfs_set_stack_device_id(dev_item
, device
->devid
);
479 btrfs_set_stack_device_type(dev_item
, device
->type
);
480 btrfs_set_stack_device_io_align(dev_item
, device
->io_align
);
481 btrfs_set_stack_device_io_width(dev_item
, device
->io_width
);
482 btrfs_set_stack_device_sector_size(dev_item
, device
->sector_size
);
483 btrfs_set_stack_device_total_bytes(dev_item
, device
->total_bytes
);
484 btrfs_set_stack_device_bytes_used(dev_item
, device
->bytes_used
);
485 memcpy(&dev_item
->uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
487 ret
= pwrite(fd
, buf
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
488 BUG_ON(ret
!= sectorsize
);
491 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
492 ret
= btrfs_bootstrap_super_map(&root
->fs_info
->mapping_tree
,
493 root
->fs_info
->fs_devices
);
498 int btrfs_prepare_device(int fd
, char *file
, int zero_end
, u64
*block_count_ret
)
504 ret
= fstat(fd
, &st
);
506 fprintf(stderr
, "unable to stat %s\n", file
);
510 block_count
= device_size(fd
, &st
);
511 if (block_count
== 0) {
512 fprintf(stderr
, "unable to find %s size\n", file
);
517 if (block_count
< 256 * 1024 * 1024) {
518 fprintf(stderr
, "device %s is too small\n", file
);
521 ret
= zero_dev_start(fd
);
523 fprintf(stderr
, "failed to zero device start %d\n", ret
);
528 ret
= zero_dev_end(fd
, block_count
);
530 fprintf(stderr
, "failed to zero device end %d\n", ret
);
534 *block_count_ret
= block_count
;
538 int btrfs_make_root_dir(struct btrfs_trans_handle
*trans
,
539 struct btrfs_root
*root
, u64 objectid
)
542 struct btrfs_inode_item inode_item
;
544 memset(&inode_item
, 0, sizeof(inode_item
));
545 btrfs_set_stack_inode_generation(&inode_item
, trans
->transid
);
546 btrfs_set_stack_inode_size(&inode_item
, 0);
547 btrfs_set_stack_inode_nlink(&inode_item
, 1);
548 btrfs_set_stack_inode_nbytes(&inode_item
, root
->leafsize
);
549 btrfs_set_stack_inode_mode(&inode_item
, S_IFDIR
| 0555);
551 if (root
->fs_info
->tree_root
== root
)
552 btrfs_set_super_root_dir(&root
->fs_info
->super_copy
, objectid
);
554 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
558 ret
= btrfs_insert_inode_ref(trans
, root
, "..", 2, objectid
, objectid
, 0);
562 btrfs_set_root_dirid(&root
->root_item
, objectid
);
569 * returns 1 if the device was mounted, < 0 on error or 0 if everything
570 * is safe to continue. TODO, this should also scan multi-device filesystems
572 int check_mounted(char *file
)
582 if ((f
= setmntent ("/proc/mounts", "r")) == NULL
)
585 if (stat(file
, &st_buf
) < 0) {
588 if (S_ISBLK(st_buf
.st_mode
)) {
589 file_rdev
= st_buf
.st_rdev
;
591 file_dev
= st_buf
.st_dev
;
592 file_ino
= st_buf
.st_ino
;
596 while ((mnt
= getmntent (f
)) != NULL
) {
597 if (strcmp(file
, mnt
->mnt_fsname
) == 0)
600 if (stat(mnt
->mnt_fsname
, &st_buf
) == 0) {
601 if (S_ISBLK(st_buf
.st_mode
)) {
602 if (file_rdev
&& (file_rdev
== st_buf
.st_rdev
))
604 } else if (file_dev
&& ((file_dev
== st_buf
.st_dev
) &&
605 (file_ino
== st_buf
.st_ino
))) {
612 /* found an entry in mnt table */
621 struct list_head list
;
625 int btrfs_register_one_device(char *fname
)
627 struct btrfs_ioctl_vol_args args
;
631 fd
= open("/dev/btrfs-control", O_RDONLY
);
634 strcpy(args
.name
, fname
);
635 ret
= ioctl(fd
, BTRFS_IOC_SCAN_DEV
, &args
);
640 int btrfs_scan_one_dir(char *dirname
, int run_ioctl
)
643 struct dirent
*dirent
;
644 struct pending_dir
*pending
;
651 struct list_head pending_list
;
652 struct btrfs_fs_devices
*tmp_devices
;
655 INIT_LIST_HEAD(&pending_list
);
657 pending
= malloc(sizeof(*pending
));
660 strcpy(pending
->name
, dirname
);
663 dirname_len
= strlen(pending
->name
);
665 fullpath
= malloc(pathlen
);
666 dirname
= pending
->name
;
672 dirp
= opendir(dirname
);
674 fprintf(stderr
, "Unable to open /sys/block for scanning\n");
678 dirent
= readdir(dirp
);
681 if (dirent
->d_name
[0] == '.')
683 if (dirname_len
+ strlen(dirent
->d_name
) + 2 > pathlen
) {
687 snprintf(fullpath
, pathlen
, "%s/%s", dirname
, dirent
->d_name
);
688 ret
= lstat(fullpath
, &st
);
690 fprintf(stderr
, "failed to stat %s\n", fullpath
);
693 if (S_ISLNK(st
.st_mode
))
695 if (S_ISDIR(st
.st_mode
)) {
696 struct pending_dir
*next
= malloc(sizeof(*next
));
701 strcpy(next
->name
, fullpath
);
702 list_add_tail(&next
->list
, &pending_list
);
704 if (!S_ISBLK(st
.st_mode
)) {
707 fd
= open(fullpath
, O_RDONLY
);
709 fprintf(stderr
, "failed to read %s\n", fullpath
);
712 ret
= btrfs_scan_one_device(fd
, fullpath
, &tmp_devices
,
714 BTRFS_SUPER_INFO_OFFSET
);
715 if (ret
== 0 && run_ioctl
> 0) {
716 btrfs_register_one_device(fullpath
);
720 if (!list_empty(&pending_list
)) {
722 pending
= list_entry(pending_list
.next
, struct pending_dir
,
724 list_del(&pending
->list
);
736 int btrfs_scan_for_fsid(struct btrfs_fs_devices
*fs_devices
, u64 total_devs
,
739 return btrfs_scan_one_dir("/dev", run_ioctls
);
742 int btrfs_device_already_in_root(struct btrfs_root
*root
, int fd
,
745 struct btrfs_super_block
*disk_super
;
749 buf
= malloc(BTRFS_SUPER_INFO_SIZE
);
754 ret
= pread(fd
, buf
, BTRFS_SUPER_INFO_SIZE
, super_offset
);
755 if (ret
!= BTRFS_SUPER_INFO_SIZE
)
759 disk_super
= (struct btrfs_super_block
*)buf
;
760 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
761 sizeof(disk_super
->magic
)))
764 if (!memcmp(disk_super
->fsid
, root
->fs_info
->super_copy
.fsid
,
773 static char *size_strs
[] = { "", "KB", "MB", "GB", "TB",
774 "PB", "EB", "ZB", "YB"};
775 char *pretty_sizes(u64 size
)
778 u64 last_size
= size
;
779 u64 fract_size
= size
;
784 fract_size
= last_size
;
791 if (num_divs
> ARRAY_SIZE(size_strs
))
794 fraction
= (float)fract_size
/ 1024;
796 sprintf(pretty
, "%.2f%s", fraction
, size_strs
[num_divs
-1]);