btrfs-progs: readme: fix link to issue tracker on github.
[btrfs-progs-unstable/devel.git] / mkfs / common.c
blob1e6a32419b686b0290ad8a28d4106adb78de8227
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
17 #include <unistd.h>
18 #include <uuid/uuid.h>
19 #include <blkid/blkid.h>
20 #include <fcntl.h>
21 #include "ctree.h"
22 #include "disk-io.h"
23 #include "volumes.h"
24 #include "utils.h"
25 #include "mkfs/common.h"
27 static u64 reference_root_table[] = {
28 [1] = BTRFS_ROOT_TREE_OBJECTID,
29 [2] = BTRFS_EXTENT_TREE_OBJECTID,
30 [3] = BTRFS_CHUNK_TREE_OBJECTID,
31 [4] = BTRFS_DEV_TREE_OBJECTID,
32 [5] = BTRFS_FS_TREE_OBJECTID,
33 [6] = BTRFS_CSUM_TREE_OBJECTID,
37 * @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
39 * The superblock signature is not valid, denotes a partially created
40 * filesystem, needs to be finalized.
42 int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
44 struct btrfs_super_block super;
45 struct extent_buffer *buf;
46 struct btrfs_root_item root_item;
47 struct btrfs_disk_key disk_key;
48 struct btrfs_extent_item *extent_item;
49 struct btrfs_inode_item *inode_item;
50 struct btrfs_chunk *chunk;
51 struct btrfs_dev_item *dev_item;
52 struct btrfs_dev_extent *dev_extent;
53 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
54 u8 *ptr;
55 int i;
56 int ret;
57 u32 itemoff;
58 u32 nritems = 0;
59 u64 first_free;
60 u64 ref_root;
61 u32 array_size;
62 u32 item_size;
63 int skinny_metadata = !!(cfg->features &
64 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
65 u64 num_bytes;
67 buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
68 if (!buf)
69 return -ENOMEM;
71 first_free = BTRFS_SUPER_INFO_OFFSET + cfg->sectorsize * 2 - 1;
72 first_free &= ~((u64)cfg->sectorsize - 1);
74 memset(&super, 0, sizeof(super));
76 num_bytes = (cfg->num_bytes / cfg->sectorsize) * cfg->sectorsize;
77 if (*cfg->fs_uuid) {
78 if (uuid_parse(cfg->fs_uuid, super.fsid) != 0) {
79 error("cannot not parse UUID: %s", cfg->fs_uuid);
80 ret = -EINVAL;
81 goto out;
83 if (!test_uuid_unique(cfg->fs_uuid)) {
84 error("non-unique UUID: %s", cfg->fs_uuid);
85 ret = -EBUSY;
86 goto out;
88 } else {
89 uuid_generate(super.fsid);
90 uuid_unparse(super.fsid, cfg->fs_uuid);
92 uuid_generate(super.dev_item.uuid);
93 uuid_generate(chunk_tree_uuid);
95 cfg->blocks[0] = BTRFS_SUPER_INFO_OFFSET;
96 for (i = 1; i < 7; i++) {
97 cfg->blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
98 cfg->nodesize * i;
101 btrfs_set_super_bytenr(&super, cfg->blocks[0]);
102 btrfs_set_super_num_devices(&super, 1);
103 btrfs_set_super_magic(&super, BTRFS_MAGIC_PARTIAL);
104 btrfs_set_super_generation(&super, 1);
105 btrfs_set_super_root(&super, cfg->blocks[1]);
106 btrfs_set_super_chunk_root(&super, cfg->blocks[3]);
107 btrfs_set_super_total_bytes(&super, num_bytes);
108 btrfs_set_super_bytes_used(&super, 6 * cfg->nodesize);
109 btrfs_set_super_sectorsize(&super, cfg->sectorsize);
110 btrfs_set_super_leafsize(&super, cfg->nodesize);
111 btrfs_set_super_nodesize(&super, cfg->nodesize);
112 btrfs_set_super_stripesize(&super, cfg->stripesize);
113 btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
114 btrfs_set_super_chunk_root_generation(&super, 1);
115 btrfs_set_super_cache_generation(&super, -1);
116 btrfs_set_super_incompat_flags(&super, cfg->features);
117 if (cfg->label)
118 __strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE - 1);
120 /* create the tree of root objects */
121 memset(buf->data, 0, cfg->nodesize);
122 buf->len = cfg->nodesize;
123 btrfs_set_header_bytenr(buf, cfg->blocks[1]);
124 btrfs_set_header_nritems(buf, 4);
125 btrfs_set_header_generation(buf, 1);
126 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
127 btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
128 write_extent_buffer(buf, super.fsid, btrfs_header_fsid(),
129 BTRFS_FSID_SIZE);
131 write_extent_buffer(buf, chunk_tree_uuid,
132 btrfs_header_chunk_tree_uuid(buf),
133 BTRFS_UUID_SIZE);
135 /* create the items for the root tree */
136 memset(&root_item, 0, sizeof(root_item));
137 inode_item = &root_item.inode;
138 btrfs_set_stack_inode_generation(inode_item, 1);
139 btrfs_set_stack_inode_size(inode_item, 3);
140 btrfs_set_stack_inode_nlink(inode_item, 1);
141 btrfs_set_stack_inode_nbytes(inode_item, cfg->nodesize);
142 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
143 btrfs_set_root_refs(&root_item, 1);
144 btrfs_set_root_used(&root_item, cfg->nodesize);
145 btrfs_set_root_generation(&root_item, 1);
147 memset(&disk_key, 0, sizeof(disk_key));
148 btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
149 btrfs_set_disk_key_offset(&disk_key, 0);
150 nritems = 0;
152 itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - sizeof(root_item);
153 btrfs_set_root_bytenr(&root_item, cfg->blocks[2]);
154 btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID);
155 btrfs_set_item_key(buf, &disk_key, nritems);
156 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
157 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
158 sizeof(root_item));
159 write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
160 nritems), sizeof(root_item));
161 nritems++;
163 itemoff = itemoff - sizeof(root_item);
164 btrfs_set_root_bytenr(&root_item, cfg->blocks[4]);
165 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
166 btrfs_set_item_key(buf, &disk_key, nritems);
167 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
168 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
169 sizeof(root_item));
170 write_extent_buffer(buf, &root_item,
171 btrfs_item_ptr_offset(buf, nritems),
172 sizeof(root_item));
173 nritems++;
175 itemoff = itemoff - sizeof(root_item);
176 btrfs_set_root_bytenr(&root_item, cfg->blocks[5]);
177 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
178 btrfs_set_item_key(buf, &disk_key, nritems);
179 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
180 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
181 sizeof(root_item));
182 write_extent_buffer(buf, &root_item,
183 btrfs_item_ptr_offset(buf, nritems),
184 sizeof(root_item));
185 nritems++;
187 itemoff = itemoff - sizeof(root_item);
188 btrfs_set_root_bytenr(&root_item, cfg->blocks[6]);
189 btrfs_set_disk_key_objectid(&disk_key, BTRFS_CSUM_TREE_OBJECTID);
190 btrfs_set_item_key(buf, &disk_key, nritems);
191 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
192 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
193 sizeof(root_item));
194 write_extent_buffer(buf, &root_item,
195 btrfs_item_ptr_offset(buf, nritems),
196 sizeof(root_item));
197 nritems++;
200 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
201 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[1]);
202 if (ret != cfg->nodesize) {
203 ret = (ret < 0 ? -errno : -EIO);
204 goto out;
207 /* create the items for the extent tree */
208 memset(buf->data + sizeof(struct btrfs_header), 0,
209 cfg->nodesize - sizeof(struct btrfs_header));
210 nritems = 0;
211 itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize);
212 for (i = 1; i < 7; i++) {
213 item_size = sizeof(struct btrfs_extent_item);
214 if (!skinny_metadata)
215 item_size += sizeof(struct btrfs_tree_block_info);
217 if (cfg->blocks[i] < first_free) {
218 error("block[%d] below first free: %llu < %llu",
219 i, (unsigned long long)cfg->blocks[i],
220 (unsigned long long)first_free);
221 ret = -EINVAL;
222 goto out;
224 if (cfg->blocks[i] < cfg->blocks[i - 1]) {
225 error("blocks %d and %d in reverse order: %llu < %llu",
226 i, i - 1,
227 (unsigned long long)cfg->blocks[i],
228 (unsigned long long)cfg->blocks[i - 1]);
229 ret = -EINVAL;
230 goto out;
233 /* create extent item */
234 itemoff -= item_size;
235 btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[i]);
236 if (skinny_metadata) {
237 btrfs_set_disk_key_type(&disk_key,
238 BTRFS_METADATA_ITEM_KEY);
239 btrfs_set_disk_key_offset(&disk_key, 0);
240 } else {
241 btrfs_set_disk_key_type(&disk_key,
242 BTRFS_EXTENT_ITEM_KEY);
243 btrfs_set_disk_key_offset(&disk_key, cfg->nodesize);
245 btrfs_set_item_key(buf, &disk_key, nritems);
246 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
247 itemoff);
248 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
249 item_size);
250 extent_item = btrfs_item_ptr(buf, nritems,
251 struct btrfs_extent_item);
252 btrfs_set_extent_refs(buf, extent_item, 1);
253 btrfs_set_extent_generation(buf, extent_item, 1);
254 btrfs_set_extent_flags(buf, extent_item,
255 BTRFS_EXTENT_FLAG_TREE_BLOCK);
256 nritems++;
258 /* create extent ref */
259 ref_root = reference_root_table[i];
260 btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[i]);
261 btrfs_set_disk_key_offset(&disk_key, ref_root);
262 btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
263 btrfs_set_item_key(buf, &disk_key, nritems);
264 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
265 itemoff);
266 btrfs_set_item_size(buf, btrfs_item_nr(nritems), 0);
267 nritems++;
269 btrfs_set_header_bytenr(buf, cfg->blocks[2]);
270 btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
271 btrfs_set_header_nritems(buf, nritems);
272 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
273 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[2]);
274 if (ret != cfg->nodesize) {
275 ret = (ret < 0 ? -errno : -EIO);
276 goto out;
279 /* create the chunk tree */
280 memset(buf->data + sizeof(struct btrfs_header), 0,
281 cfg->nodesize - sizeof(struct btrfs_header));
282 nritems = 0;
283 item_size = sizeof(*dev_item);
284 itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - item_size;
286 /* first device 1 (there is no device 0) */
287 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
288 btrfs_set_disk_key_offset(&disk_key, 1);
289 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
290 btrfs_set_item_key(buf, &disk_key, nritems);
291 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
292 btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
294 dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
295 btrfs_set_device_id(buf, dev_item, 1);
296 btrfs_set_device_generation(buf, dev_item, 0);
297 btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
298 btrfs_set_device_bytes_used(buf, dev_item,
299 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
300 btrfs_set_device_io_align(buf, dev_item, cfg->sectorsize);
301 btrfs_set_device_io_width(buf, dev_item, cfg->sectorsize);
302 btrfs_set_device_sector_size(buf, dev_item, cfg->sectorsize);
303 btrfs_set_device_type(buf, dev_item, 0);
305 write_extent_buffer(buf, super.dev_item.uuid,
306 (unsigned long)btrfs_device_uuid(dev_item),
307 BTRFS_UUID_SIZE);
308 write_extent_buffer(buf, super.fsid,
309 (unsigned long)btrfs_device_fsid(dev_item),
310 BTRFS_UUID_SIZE);
311 read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
312 sizeof(*dev_item));
314 nritems++;
315 item_size = btrfs_chunk_item_size(1);
316 itemoff = itemoff - item_size;
318 /* then we have chunk 0 */
319 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
320 btrfs_set_disk_key_offset(&disk_key, 0);
321 btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
322 btrfs_set_item_key(buf, &disk_key, nritems);
323 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
324 btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
326 chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
327 btrfs_set_chunk_length(buf, chunk, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
328 btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
329 btrfs_set_chunk_stripe_len(buf, chunk, BTRFS_STRIPE_LEN);
330 btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
331 btrfs_set_chunk_io_align(buf, chunk, cfg->sectorsize);
332 btrfs_set_chunk_io_width(buf, chunk, cfg->sectorsize);
333 btrfs_set_chunk_sector_size(buf, chunk, cfg->sectorsize);
334 btrfs_set_chunk_num_stripes(buf, chunk, 1);
335 btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
336 btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
337 nritems++;
339 write_extent_buffer(buf, super.dev_item.uuid,
340 (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
341 BTRFS_UUID_SIZE);
343 /* copy the key for the chunk to the system array */
344 ptr = super.sys_chunk_array;
345 array_size = sizeof(disk_key);
347 memcpy(ptr, &disk_key, sizeof(disk_key));
348 ptr += sizeof(disk_key);
350 /* copy the chunk to the system array */
351 read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
352 array_size += item_size;
353 ptr += item_size;
354 btrfs_set_super_sys_array_size(&super, array_size);
356 btrfs_set_header_bytenr(buf, cfg->blocks[3]);
357 btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
358 btrfs_set_header_nritems(buf, nritems);
359 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
360 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[3]);
361 if (ret != cfg->nodesize) {
362 ret = (ret < 0 ? -errno : -EIO);
363 goto out;
366 /* create the device tree */
367 memset(buf->data + sizeof(struct btrfs_header), 0,
368 cfg->nodesize - sizeof(struct btrfs_header));
369 nritems = 0;
370 itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) -
371 sizeof(struct btrfs_dev_extent);
373 btrfs_set_disk_key_objectid(&disk_key, 1);
374 btrfs_set_disk_key_offset(&disk_key, 0);
375 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
376 btrfs_set_item_key(buf, &disk_key, nritems);
377 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
378 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
379 sizeof(struct btrfs_dev_extent));
380 dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
381 btrfs_set_dev_extent_chunk_tree(buf, dev_extent,
382 BTRFS_CHUNK_TREE_OBJECTID);
383 btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
384 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
385 btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
387 write_extent_buffer(buf, chunk_tree_uuid,
388 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
389 BTRFS_UUID_SIZE);
391 btrfs_set_dev_extent_length(buf, dev_extent,
392 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
393 nritems++;
395 btrfs_set_header_bytenr(buf, cfg->blocks[4]);
396 btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
397 btrfs_set_header_nritems(buf, nritems);
398 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
399 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[4]);
400 if (ret != cfg->nodesize) {
401 ret = (ret < 0 ? -errno : -EIO);
402 goto out;
405 /* create the FS root */
406 memset(buf->data + sizeof(struct btrfs_header), 0,
407 cfg->nodesize - sizeof(struct btrfs_header));
408 btrfs_set_header_bytenr(buf, cfg->blocks[5]);
409 btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
410 btrfs_set_header_nritems(buf, 0);
411 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
412 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[5]);
413 if (ret != cfg->nodesize) {
414 ret = (ret < 0 ? -errno : -EIO);
415 goto out;
417 /* finally create the csum root */
418 memset(buf->data + sizeof(struct btrfs_header), 0,
419 cfg->nodesize - sizeof(struct btrfs_header));
420 btrfs_set_header_bytenr(buf, cfg->blocks[6]);
421 btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
422 btrfs_set_header_nritems(buf, 0);
423 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
424 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[6]);
425 if (ret != cfg->nodesize) {
426 ret = (ret < 0 ? -errno : -EIO);
427 goto out;
430 /* and write out the super block */
431 memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
432 memcpy(buf->data, &super, sizeof(super));
433 buf->len = BTRFS_SUPER_INFO_SIZE;
434 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
435 ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE, cfg->blocks[0]);
436 if (ret != BTRFS_SUPER_INFO_SIZE) {
437 ret = (ret < 0 ? -errno : -EIO);
438 goto out;
441 ret = 0;
443 out:
444 free(buf);
445 return ret;
448 u64 btrfs_min_dev_size(u32 nodesize)
450 return 2 * (BTRFS_MKFS_SYSTEM_GROUP_SIZE +
451 btrfs_min_global_blk_rsv_size(nodesize));
455 * Btrfs minimum size calculation is complicated, it should include at least:
456 * 1. system group size
457 * 2. minimum global block reserve
458 * 3. metadata used at mkfs
459 * 4. space reservation to create uuid for first mount.
460 * Also, raid factor should also be taken into consideration.
461 * To avoid the overkill calculation, (system group + global block rsv) * 2
462 * for *EACH* device should be good enough.
464 u64 btrfs_min_global_blk_rsv_size(u32 nodesize)
466 return (u64)nodesize << 10;
469 #define isoctal(c) (((c) & ~7) == '0')
471 static inline void translate(char *f, char *t)
473 while (*f != '\0') {
474 if (*f == '\\' &&
475 isoctal(f[1]) && isoctal(f[2]) && isoctal(f[3])) {
476 *t++ = 64*(f[1] & 7) + 8*(f[2] & 7) + (f[3] & 7);
477 f += 4;
478 } else
479 *t++ = *f++;
481 *t = '\0';
482 return;
486 * Checks if the swap device.
487 * Returns 1 if swap device, < 0 on error or 0 if not swap device.
489 static int is_swap_device(const char *file)
491 FILE *f;
492 struct stat st_buf;
493 dev_t dev;
494 ino_t ino = 0;
495 char tmp[PATH_MAX];
496 char buf[PATH_MAX];
497 char *cp;
498 int ret = 0;
500 if (stat(file, &st_buf) < 0)
501 return -errno;
502 if (S_ISBLK(st_buf.st_mode))
503 dev = st_buf.st_rdev;
504 else if (S_ISREG(st_buf.st_mode)) {
505 dev = st_buf.st_dev;
506 ino = st_buf.st_ino;
507 } else
508 return 0;
510 if ((f = fopen("/proc/swaps", "r")) == NULL)
511 return 0;
513 /* skip the first line */
514 if (fgets(tmp, sizeof(tmp), f) == NULL)
515 goto out;
517 while (fgets(tmp, sizeof(tmp), f) != NULL) {
518 if ((cp = strchr(tmp, ' ')) != NULL)
519 *cp = '\0';
520 if ((cp = strchr(tmp, '\t')) != NULL)
521 *cp = '\0';
522 translate(tmp, buf);
523 if (stat(buf, &st_buf) != 0)
524 continue;
525 if (S_ISBLK(st_buf.st_mode)) {
526 if (dev == st_buf.st_rdev) {
527 ret = 1;
528 break;
530 } else if (S_ISREG(st_buf.st_mode)) {
531 if (dev == st_buf.st_dev && ino == st_buf.st_ino) {
532 ret = 1;
533 break;
538 out:
539 fclose(f);
541 return ret;
545 * Check for existing filesystem or partition table on device.
546 * Returns:
547 * 1 for existing fs or partition
548 * 0 for nothing found
549 * -1 for internal error
551 static int check_overwrite(const char *device)
553 const char *type;
554 blkid_probe pr = NULL;
555 int ret;
556 blkid_loff_t size;
558 if (!device || !*device)
559 return 0;
561 ret = -1; /* will reset on success of all setup calls */
563 pr = blkid_new_probe_from_filename(device);
564 if (!pr)
565 goto out;
567 size = blkid_probe_get_size(pr);
568 if (size < 0)
569 goto out;
571 /* nothing to overwrite on a 0-length device */
572 if (size == 0) {
573 ret = 0;
574 goto out;
577 ret = blkid_probe_enable_partitions(pr, 1);
578 if (ret < 0)
579 goto out;
581 ret = blkid_do_fullprobe(pr);
582 if (ret < 0)
583 goto out;
586 * Blkid returns 1 for nothing found and 0 when it finds a signature,
587 * but we want the exact opposite, so reverse the return value here.
589 * In addition print some useful diagnostics about what actually is
590 * on the device.
592 if (ret) {
593 ret = 0;
594 goto out;
597 if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
598 fprintf(stderr,
599 "%s appears to contain an existing "
600 "filesystem (%s).\n", device, type);
601 } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
602 fprintf(stderr,
603 "%s appears to contain a partition "
604 "table (%s).\n", device, type);
605 } else {
606 fprintf(stderr,
607 "%s appears to contain something weird "
608 "according to blkid\n", device);
610 ret = 1;
612 out:
613 if (pr)
614 blkid_free_probe(pr);
615 if (ret == -1)
616 fprintf(stderr,
617 "probe of %s failed, cannot detect "
618 "existing filesystem.\n", device);
619 return ret;
623 * Check if a device is suitable for btrfs
624 * returns:
625 * 1: something is wrong, an error is printed
626 * 0: all is fine
628 int test_dev_for_mkfs(const char *file, int force_overwrite)
630 int ret, fd;
631 struct stat st;
633 ret = is_swap_device(file);
634 if (ret < 0) {
635 error("checking status of %s: %s", file, strerror(-ret));
636 return 1;
638 if (ret == 1) {
639 error("%s is a swap device", file);
640 return 1;
642 if (!force_overwrite) {
643 if (check_overwrite(file)) {
644 error("use the -f option to force overwrite of %s",
645 file);
646 return 1;
649 ret = check_mounted(file);
650 if (ret < 0) {
651 error("cannot check mount status of %s: %s", file,
652 strerror(-ret));
653 return 1;
655 if (ret == 1) {
656 error("%s is mounted", file);
657 return 1;
659 /* check if the device is busy */
660 fd = open(file, O_RDWR|O_EXCL);
661 if (fd < 0) {
662 error("unable to open %s: %s", file, strerror(errno));
663 return 1;
665 if (fstat(fd, &st)) {
666 error("unable to stat %s: %s", file, strerror(errno));
667 close(fd);
668 return 1;
670 if (!S_ISBLK(st.st_mode)) {
671 error("%s is not a block device", file);
672 close(fd);
673 return 1;
675 close(fd);
676 return 0;
679 int is_vol_small(const char *file)
681 int fd = -1;
682 int e;
683 struct stat st;
684 u64 size;
686 fd = open(file, O_RDONLY);
687 if (fd < 0)
688 return -errno;
689 if (fstat(fd, &st) < 0) {
690 e = -errno;
691 close(fd);
692 return e;
694 size = btrfs_device_size(fd, &st);
695 if (size == 0) {
696 close(fd);
697 return -1;
699 if (size < BTRFS_MKFS_SMALL_VOLUME_SIZE) {
700 close(fd);
701 return 1;
702 } else {
703 close(fd);
704 return 0;
708 int test_minimum_size(const char *file, u32 nodesize)
710 int fd;
711 struct stat statbuf;
713 fd = open(file, O_RDONLY);
714 if (fd < 0)
715 return -errno;
716 if (stat(file, &statbuf) < 0) {
717 close(fd);
718 return -errno;
720 if (btrfs_device_size(fd, &statbuf) < btrfs_min_dev_size(nodesize)) {
721 close(fd);
722 return 1;
724 close(fd);
725 return 0;