btrfs-progs: use better name for nodiscard variable and flip the logic
[platform/upstream/btrfs-progs.git] / mkfs.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE
21
22 #include "kerncompat.h"
23
24 #include <sys/ioctl.h>
25 #include <sys/mount.h>
26 #include "ioctl.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/dir.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <getopt.h>
35 #include <uuid/uuid.h>
36 #include <ctype.h>
37 #include <attr/xattr.h>
38 #include <blkid/blkid.h>
39 #include <ftw.h>
40 #include "ctree.h"
41 #include "disk-io.h"
42 #include "volumes.h"
43 #include "transaction.h"
44 #include "utils.h"
45 #include "version.h"
46
47 static u64 index_cnt = 2;
48
49 struct directory_name_entry {
50         char *dir_name;
51         char *path;
52         ino_t inum;
53         struct list_head list;
54 };
55
56 static int make_root_dir(struct btrfs_root *root, int mixed)
57 {
58         struct btrfs_trans_handle *trans;
59         struct btrfs_key location;
60         u64 bytes_used;
61         u64 chunk_start = 0;
62         u64 chunk_size = 0;
63         int ret;
64
65         trans = btrfs_start_transaction(root, 1);
66         bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
67
68         root->fs_info->system_allocs = 1;
69         ret = btrfs_make_block_group(trans, root, bytes_used,
70                                      BTRFS_BLOCK_GROUP_SYSTEM,
71                                      BTRFS_FIRST_CHUNK_TREE_OBJECTID,
72                                      0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
73         BUG_ON(ret);
74
75         if (mixed) {
76                 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
77                                         &chunk_start, &chunk_size,
78                                         BTRFS_BLOCK_GROUP_METADATA |
79                                         BTRFS_BLOCK_GROUP_DATA);
80                 if (ret == -ENOSPC) {
81                         fprintf(stderr,
82                                 "no space to alloc data/metadata chunk\n");
83                         goto err;
84                 }
85                 BUG_ON(ret);
86                 ret = btrfs_make_block_group(trans, root, 0,
87                                              BTRFS_BLOCK_GROUP_METADATA |
88                                              BTRFS_BLOCK_GROUP_DATA,
89                                              BTRFS_FIRST_CHUNK_TREE_OBJECTID,
90                                              chunk_start, chunk_size);
91                 BUG_ON(ret);
92                 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
93         } else {
94                 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
95                                         &chunk_start, &chunk_size,
96                                         BTRFS_BLOCK_GROUP_METADATA);
97                 if (ret == -ENOSPC) {
98                         fprintf(stderr, "no space to alloc metadata chunk\n");
99                         goto err;
100                 }
101                 BUG_ON(ret);
102                 ret = btrfs_make_block_group(trans, root, 0,
103                                              BTRFS_BLOCK_GROUP_METADATA,
104                                              BTRFS_FIRST_CHUNK_TREE_OBJECTID,
105                                              chunk_start, chunk_size);
106                 BUG_ON(ret);
107         }
108
109         root->fs_info->system_allocs = 0;
110         btrfs_commit_transaction(trans, root);
111         trans = btrfs_start_transaction(root, 1);
112         BUG_ON(!trans);
113
114         if (!mixed) {
115                 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
116                                         &chunk_start, &chunk_size,
117                                         BTRFS_BLOCK_GROUP_DATA);
118                 if (ret == -ENOSPC) {
119                         fprintf(stderr, "no space to alloc data chunk\n");
120                         goto err;
121                 }
122                 BUG_ON(ret);
123                 ret = btrfs_make_block_group(trans, root, 0,
124                                              BTRFS_BLOCK_GROUP_DATA,
125                                              BTRFS_FIRST_CHUNK_TREE_OBJECTID,
126                                              chunk_start, chunk_size);
127                 BUG_ON(ret);
128         }
129
130         ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
131                               BTRFS_ROOT_TREE_DIR_OBJECTID);
132         if (ret)
133                 goto err;
134         ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
135         if (ret)
136                 goto err;
137         memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
138         location.offset = (u64)-1;
139         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
140                         "default", 7,
141                         btrfs_super_root_dir(root->fs_info->super_copy),
142                         &location, BTRFS_FT_DIR, 0);
143         if (ret)
144                 goto err;
145
146         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
147                              "default", 7, location.objectid,
148                              BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
149         if (ret)
150                 goto err;
151
152         btrfs_commit_transaction(trans, root);
153 err:
154         return ret;
155 }
156
157 static void __recow_root(struct btrfs_trans_handle *trans,
158                          struct btrfs_root *root)
159 {
160         int ret;
161         struct extent_buffer *tmp;
162
163         if (trans->transid != btrfs_root_generation(&root->root_item)) {
164                 ret = __btrfs_cow_block(trans, root, root->node,
165                                         NULL, 0, &tmp, 0, 0);
166                 BUG_ON(ret);
167                 free_extent_buffer(tmp);
168         }
169 }
170
171 static void recow_roots(struct btrfs_trans_handle *trans,
172                        struct btrfs_root *root)
173 {
174         struct btrfs_fs_info *info = root->fs_info;
175
176         __recow_root(trans, info->fs_root);
177         __recow_root(trans, info->tree_root);
178         __recow_root(trans, info->extent_root);
179         __recow_root(trans, info->chunk_root);
180         __recow_root(trans, info->dev_root);
181         __recow_root(trans, info->csum_root);
182 }
183
184 static int create_one_raid_group(struct btrfs_trans_handle *trans,
185                               struct btrfs_root *root, u64 type)
186 {
187         u64 chunk_start;
188         u64 chunk_size;
189         int ret;
190
191         ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
192                                 &chunk_start, &chunk_size, type);
193         if (ret == -ENOSPC) {
194                 fprintf(stderr, "not enough free space\n");
195                 exit(1);
196         }
197         BUG_ON(ret);
198         ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
199                                      type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
200                                      chunk_start, chunk_size);
201         BUG_ON(ret);
202         return ret;
203 }
204
205 static int create_raid_groups(struct btrfs_trans_handle *trans,
206                               struct btrfs_root *root, u64 data_profile,
207                               int data_profile_opt, u64 metadata_profile,
208                               int metadata_profile_opt, int mixed, int ssd)
209 {
210         u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
211         int ret;
212
213         if (metadata_profile) {
214                 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
215
216                 ret = create_one_raid_group(trans, root,
217                                             BTRFS_BLOCK_GROUP_SYSTEM |
218                                             metadata_profile);
219                 BUG_ON(ret);
220
221                 if (mixed)
222                         meta_flags |= BTRFS_BLOCK_GROUP_DATA;
223
224                 ret = create_one_raid_group(trans, root, meta_flags |
225                                             metadata_profile);
226                 BUG_ON(ret);
227
228         }
229         if (!mixed && num_devices > 1 && data_profile) {
230                 ret = create_one_raid_group(trans, root,
231                                             BTRFS_BLOCK_GROUP_DATA |
232                                             data_profile);
233                 BUG_ON(ret);
234         }
235         recow_roots(trans, root);
236
237         return 0;
238 }
239
240 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
241                                   struct btrfs_root *root)
242 {
243         struct btrfs_key location;
244         struct btrfs_root_item root_item;
245         struct extent_buffer *tmp;
246         u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
247         int ret;
248
249         ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
250         BUG_ON(ret);
251
252         memcpy(&root_item, &root->root_item, sizeof(root_item));
253         btrfs_set_root_bytenr(&root_item, tmp->start);
254         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
255         btrfs_set_root_generation(&root_item, trans->transid);
256         free_extent_buffer(tmp);
257
258         location.objectid = objectid;
259         location.type = BTRFS_ROOT_ITEM_KEY;
260         location.offset = 0;
261         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
262                                 &location, &root_item);
263         BUG_ON(ret);
264         return 0;
265 }
266
267
268 static void print_usage(void) __attribute__((noreturn));
269 static void print_usage(void)
270 {
271         fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
272         fprintf(stderr, "options:\n");
273         fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
274         fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
275         fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
276         fprintf(stderr, "\t -f --force force overwrite of existing filesystem\n");
277         fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
278         fprintf(stderr, "\t -L --label set a label\n");
279         fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
280         fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
281         fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
282         fprintf(stderr, "\t -s --sectorsize min block allocation (may not mountable by current kernel)\n");
283         fprintf(stderr, "\t -r --rootdir the source directory\n");
284         fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
285         fprintf(stderr, "\t -O --features comma separated list of filesystem features\n");
286         fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
287         fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
288         exit(1);
289 }
290
291 static void print_version(void) __attribute__((noreturn));
292 static void print_version(void)
293 {
294         fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
295         exit(0);
296 }
297
298 static u64 parse_profile(char *s)
299 {
300         if (strcmp(s, "raid0") == 0) {
301                 return BTRFS_BLOCK_GROUP_RAID0;
302         } else if (strcmp(s, "raid1") == 0) {
303                 return BTRFS_BLOCK_GROUP_RAID1;
304         } else if (strcmp(s, "raid5") == 0) {
305                 return BTRFS_BLOCK_GROUP_RAID5;
306         } else if (strcmp(s, "raid6") == 0) {
307                 return BTRFS_BLOCK_GROUP_RAID6;
308         } else if (strcmp(s, "raid10") == 0) {
309                 return BTRFS_BLOCK_GROUP_RAID10;
310         } else if (strcmp(s, "dup") == 0) {
311                 return BTRFS_BLOCK_GROUP_DUP;
312         } else if (strcmp(s, "single") == 0) {
313                 return 0;
314         } else {
315                 fprintf(stderr, "Unknown profile %s\n", s);
316                 print_usage();
317         }
318         /* not reached */
319         return 0;
320 }
321
322 static char *parse_label(char *input)
323 {
324         int len = strlen(input);
325
326         if (len >= BTRFS_LABEL_SIZE) {
327                 fprintf(stderr, "Label %s is too long (max %d)\n", input,
328                         BTRFS_LABEL_SIZE - 1);
329                 exit(1);
330         }
331         return strdup(input);
332 }
333
334 static struct option long_options[] = {
335         { "alloc-start", 1, NULL, 'A'},
336         { "byte-count", 1, NULL, 'b' },
337         { "force", 0, NULL, 'f' },
338         { "leafsize", 1, NULL, 'l' },
339         { "label", 1, NULL, 'L'},
340         { "metadata", 1, NULL, 'm' },
341         { "mixed", 0, NULL, 'M' },
342         { "nodesize", 1, NULL, 'n' },
343         { "sectorsize", 1, NULL, 's' },
344         { "data", 1, NULL, 'd' },
345         { "version", 0, NULL, 'V' },
346         { "rootdir", 1, NULL, 'r' },
347         { "nodiscard", 0, NULL, 'K' },
348         { "features", 0, NULL, 'O' },
349         { NULL, 0, NULL, 0}
350 };
351
352 static int add_directory_items(struct btrfs_trans_handle *trans,
353                                struct btrfs_root *root, u64 objectid,
354                                ino_t parent_inum, const char *name,
355                                struct stat *st, int *dir_index_cnt)
356 {
357         int ret;
358         int name_len;
359         struct btrfs_key location;
360         u8 filetype = 0;
361
362         name_len = strlen(name);
363
364         location.objectid = objectid;
365         location.offset = 0;
366         btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
367
368         if (S_ISDIR(st->st_mode))
369                 filetype = BTRFS_FT_DIR;
370         if (S_ISREG(st->st_mode))
371                 filetype = BTRFS_FT_REG_FILE;
372         if (S_ISLNK(st->st_mode))
373                 filetype = BTRFS_FT_SYMLINK;
374
375         ret = btrfs_insert_dir_item(trans, root, name, name_len,
376                                     parent_inum, &location,
377                                     filetype, index_cnt);
378
379         *dir_index_cnt = index_cnt;
380         index_cnt++;
381
382         return ret;
383 }
384
385 static int fill_inode_item(struct btrfs_trans_handle *trans,
386                            struct btrfs_root *root,
387                            struct btrfs_inode_item *dst, struct stat *src)
388 {
389         u64 blocks = 0;
390         u64 sectorsize = root->sectorsize;
391
392         /*
393          * btrfs_inode_item has some reserved fields
394          * and represents on-disk inode entry, so
395          * zero everything to prevent information leak
396          */
397         memset(dst, 0, sizeof (*dst));
398
399         btrfs_set_stack_inode_generation(dst, trans->transid);
400         btrfs_set_stack_inode_size(dst, src->st_size);
401         btrfs_set_stack_inode_nbytes(dst, 0);
402         btrfs_set_stack_inode_block_group(dst, 0);
403         btrfs_set_stack_inode_nlink(dst, src->st_nlink);
404         btrfs_set_stack_inode_uid(dst, src->st_uid);
405         btrfs_set_stack_inode_gid(dst, src->st_gid);
406         btrfs_set_stack_inode_mode(dst, src->st_mode);
407         btrfs_set_stack_inode_rdev(dst, 0);
408         btrfs_set_stack_inode_flags(dst, 0);
409         btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
410         btrfs_set_stack_timespec_nsec(&dst->atime, 0);
411         btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
412         btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
413         btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
414         btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
415         btrfs_set_stack_timespec_sec(&dst->otime, 0);
416         btrfs_set_stack_timespec_nsec(&dst->otime, 0);
417
418         if (S_ISDIR(src->st_mode)) {
419                 btrfs_set_stack_inode_size(dst, 0);
420                 btrfs_set_stack_inode_nlink(dst, 1);
421         }
422         if (S_ISREG(src->st_mode)) {
423                 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
424                 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
425                         btrfs_set_stack_inode_nbytes(dst, src->st_size);
426                 else {
427                         blocks = src->st_size / sectorsize;
428                         if (src->st_size % sectorsize)
429                                 blocks += 1;
430                         blocks *= sectorsize;
431                         btrfs_set_stack_inode_nbytes(dst, blocks);
432                 }
433         }
434         if (S_ISLNK(src->st_mode))
435                 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
436
437         return 0;
438 }
439
440 static int directory_select(const struct direct *entry)
441 {
442         if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
443                 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
444                 return 0;
445         else
446                 return 1;
447 }
448
449 static void free_namelist(struct direct **files, int count)
450 {
451         int i;
452
453         if (count < 0)
454                 return;
455
456         for (i = 0; i < count; ++i)
457                 free(files[i]);
458         free(files);
459 }
460
461 static u64 calculate_dir_inode_size(char *dirname)
462 {
463         int count, i;
464         struct direct **files, *cur_file;
465         u64 dir_inode_size = 0;
466
467         count = scandir(dirname, &files, directory_select, NULL);
468
469         for (i = 0; i < count; i++) {
470                 cur_file = files[i];
471                 dir_inode_size += strlen(cur_file->d_name);
472         }
473
474         free_namelist(files, count);
475
476         dir_inode_size *= 2;
477         return dir_inode_size;
478 }
479
480 static int add_inode_items(struct btrfs_trans_handle *trans,
481                            struct btrfs_root *root,
482                            struct stat *st, char *name,
483                            u64 self_objectid, ino_t parent_inum,
484                            int dir_index_cnt, struct btrfs_inode_item *inode_ret)
485 {
486         int ret;
487         struct btrfs_key inode_key;
488         struct btrfs_inode_item btrfs_inode;
489         u64 objectid;
490         u64 inode_size = 0;
491         int name_len;
492
493         name_len = strlen(name);
494         fill_inode_item(trans, root, &btrfs_inode, st);
495         objectid = self_objectid;
496
497         if (S_ISDIR(st->st_mode)) {
498                 inode_size = calculate_dir_inode_size(name);
499                 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
500         }
501
502         inode_key.objectid = objectid;
503         inode_key.offset = 0;
504         btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
505
506         ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
507         if (ret)
508                 goto fail;
509
510         ret = btrfs_insert_inode_ref(trans, root, name, name_len,
511                                      objectid, parent_inum, dir_index_cnt);
512         if (ret)
513                 goto fail;
514
515         *inode_ret = btrfs_inode;
516 fail:
517         return ret;
518 }
519
520 static int add_xattr_item(struct btrfs_trans_handle *trans,
521                           struct btrfs_root *root, u64 objectid,
522                           const char *file_name)
523 {
524         int ret;
525         int cur_name_len;
526         char xattr_list[XATTR_LIST_MAX];
527         char *cur_name;
528         char cur_value[XATTR_SIZE_MAX];
529         char delimiter = '\0';
530         char *next_location = xattr_list;
531
532         ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
533         if (ret < 0) {
534                 if(errno == ENOTSUP)
535                         return 0;
536                 fprintf(stderr, "get a list of xattr failed for %s\n",
537                         file_name);
538                 return ret;
539         }
540         if (ret == 0)
541                 return ret;
542
543         cur_name = strtok(xattr_list, &delimiter);
544         while (cur_name != NULL) {
545                 cur_name_len = strlen(cur_name);
546                 next_location += cur_name_len + 1;
547
548                 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
549                 if (ret < 0) {
550                         if(errno == ENOTSUP)
551                                 return 0;
552                         fprintf(stderr, "get a xattr value failed for %s attr %s\n",
553                                 file_name, cur_name);
554                         return ret;
555                 }
556
557                 ret = btrfs_insert_xattr_item(trans, root, cur_name,
558                                               cur_name_len, cur_value,
559                                               ret, objectid);
560                 if (ret) {
561                         fprintf(stderr, "insert a xattr item failed for %s\n",
562                                 file_name);
563                 }
564
565                 cur_name = strtok(next_location, &delimiter);
566         }
567
568         return ret;
569 }
570 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
571                                u64 hint_byte, struct btrfs_key *ins)
572 {
573         u64 start;
574         u64 end;
575         u64 last = hint_byte;
576         int ret;
577         int wrapped = 0;
578         struct btrfs_block_group_cache *cache;
579
580         while (1) {
581                 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
582                                             last, &start, &end, EXTENT_DIRTY);
583                 if (ret) {
584                         if (wrapped++ == 0) {
585                                 last = 0;
586                                 continue;
587                         } else {
588                                 goto fail;
589                         }
590                 }
591
592                 start = max(last, start);
593                 last = end + 1;
594                 if (last - start < num_bytes)
595                         continue;
596
597                 last = start + num_bytes;
598                 if (test_range_bit(&root->fs_info->pinned_extents,
599                                    start, last - 1, EXTENT_DIRTY, 0))
600                         continue;
601
602                 cache = btrfs_lookup_block_group(root->fs_info, start);
603                 BUG_ON(!cache);
604                 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
605                     last > cache->key.objectid + cache->key.offset) {
606                         last = cache->key.objectid + cache->key.offset;
607                         continue;
608                 }
609
610                 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
611                             BTRFS_BLOCK_GROUP_METADATA)) {
612                         last = cache->key.objectid + cache->key.offset;
613                         continue;
614                 }
615
616                 clear_extent_dirty(&root->fs_info->free_space_cache,
617                                    start, start + num_bytes - 1, 0);
618
619                 ins->objectid = start;
620                 ins->offset = num_bytes;
621                 ins->type = BTRFS_EXTENT_ITEM_KEY;
622                 return 0;
623         }
624 fail:
625         fprintf(stderr, "not enough free space\n");
626         return -ENOSPC;
627 }
628
629 static int record_file_extent(struct btrfs_trans_handle *trans,
630                               struct btrfs_root *root, u64 objectid,
631                               struct btrfs_inode_item *inode,
632                               u64 file_pos, u64 disk_bytenr,
633                               u64 num_bytes)
634 {
635         int ret;
636         struct btrfs_fs_info *info = root->fs_info;
637         struct btrfs_root *extent_root = info->extent_root;
638         struct extent_buffer *leaf;
639         struct btrfs_file_extent_item *fi;
640         struct btrfs_key ins_key;
641         struct btrfs_path path;
642         struct btrfs_extent_item *ei;
643
644         btrfs_init_path(&path);
645
646         ins_key.objectid = objectid;
647         ins_key.offset = 0;
648         btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
649         ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
650                                       sizeof(*fi));
651         if (ret)
652                 goto fail;
653         leaf = path.nodes[0];
654         fi = btrfs_item_ptr(leaf, path.slots[0],
655                             struct btrfs_file_extent_item);
656         btrfs_set_file_extent_generation(leaf, fi, trans->transid);
657         btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
658         btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
659         btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
660         btrfs_set_file_extent_offset(leaf, fi, 0);
661         btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
662         btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
663         btrfs_set_file_extent_compression(leaf, fi, 0);
664         btrfs_set_file_extent_encryption(leaf, fi, 0);
665         btrfs_set_file_extent_other_encoding(leaf, fi, 0);
666         btrfs_mark_buffer_dirty(leaf);
667
668         btrfs_release_path(&path);
669
670         ins_key.objectid = disk_bytenr;
671         ins_key.offset = num_bytes;
672         ins_key.type = BTRFS_EXTENT_ITEM_KEY;
673
674         ret = btrfs_insert_empty_item(trans, extent_root, &path,
675                                 &ins_key, sizeof(*ei));
676         if (ret == 0) {
677                 leaf = path.nodes[0];
678                 ei = btrfs_item_ptr(leaf, path.slots[0],
679                                     struct btrfs_extent_item);
680
681                 btrfs_set_extent_refs(leaf, ei, 0);
682                 btrfs_set_extent_generation(leaf, ei, trans->transid);
683                 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
684
685                 btrfs_mark_buffer_dirty(leaf);
686                 ret = btrfs_update_block_group(trans, root, disk_bytenr,
687                                                num_bytes, 1, 0);
688                 if (ret)
689                         goto fail;
690         } else if (ret != -EEXIST) {
691                 goto fail;
692         }
693
694         ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
695                                    root->root_key.objectid,
696                                    objectid, 0);
697 fail:
698         btrfs_release_path(&path);
699         return ret;
700 }
701
702 static int add_symbolic_link(struct btrfs_trans_handle *trans,
703                              struct btrfs_root *root,
704                              u64 objectid, const char *path_name)
705 {
706         int ret;
707         u64 sectorsize = root->sectorsize;
708         char *buf = malloc(sectorsize);
709
710         ret = readlink(path_name, buf, sectorsize);
711         if (ret <= 0) {
712                 fprintf(stderr, "readlink failed for %s\n", path_name);
713                 goto fail;
714         }
715         if (ret >= sectorsize) {
716                 fprintf(stderr, "symlink too long for %s", path_name);
717                 ret = -1;
718                 goto fail;
719         }
720
721         buf[ret] = '\0'; /* readlink does not do it for us */
722         ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
723                                          buf, ret + 1);
724 fail:
725         free(buf);
726         return ret;
727 }
728
729 static int add_file_items(struct btrfs_trans_handle *trans,
730                           struct btrfs_root *root,
731                           struct btrfs_inode_item *btrfs_inode, u64 objectid,
732                           ino_t parent_inum, struct stat *st,
733                           const char *path_name, int out_fd)
734 {
735         int ret = -1;
736         ssize_t ret_read;
737         u64 bytes_read = 0;
738         char *buffer = NULL;
739         struct btrfs_key key;
740         int blocks;
741         u32 sectorsize = root->sectorsize;
742         u64 first_block = 0;
743         u64 num_blocks = 0;
744         int fd;
745
746         fd = open(path_name, O_RDONLY);
747         if (fd == -1) {
748                 fprintf(stderr, "%s open failed\n", path_name);
749                 return ret;
750         }
751
752         blocks = st->st_size / sectorsize;
753         if (st->st_size % sectorsize)
754                 blocks += 1;
755
756         if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
757                 buffer = malloc(st->st_size);
758                 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
759                 if (ret_read == -1) {
760                         fprintf(stderr, "%s read failed\n", path_name);
761                         goto end;
762                 }
763
764                 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
765                                                  buffer, st->st_size);
766                 goto end;
767         }
768
769         ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
770         if (ret)
771                 goto end;
772
773         first_block = key.objectid;
774         bytes_read = 0;
775         buffer = malloc(sectorsize);
776
777         do {
778                 memset(buffer, 0, sectorsize);
779                 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
780                 if (ret_read == -1) {
781                         fprintf(stderr, "%s read failed\n", path_name);
782                         goto end;
783                 }
784
785                 ret = pwrite64(out_fd, buffer, sectorsize,
786                                first_block + bytes_read);
787                 if (ret != sectorsize) {
788                         fprintf(stderr, "output file write failed\n");
789                         goto end;
790                 }
791
792                 /* checksum for file data */
793                 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
794                                 first_block + (blocks * sectorsize),
795                                 first_block + bytes_read,
796                                 buffer, sectorsize);
797                 if (ret) {
798                         fprintf(stderr, "%s checksum failed\n", path_name);
799                         goto end;
800                 }
801
802                 bytes_read += ret_read;
803                 num_blocks++;
804         } while (ret_read == sectorsize);
805
806         if (num_blocks > 0) {
807                 ret = record_file_extent(trans, root, objectid, btrfs_inode,
808                                          first_block, first_block,
809                                          blocks * sectorsize);
810                 if (ret)
811                         goto end;
812         }
813
814 end:
815         if (buffer)
816                 free(buffer);
817         close(fd);
818         return ret;
819 }
820
821 static char *make_path(char *dir, char *name)
822 {
823         char *path;
824
825         path = malloc(strlen(dir) + strlen(name) + 2);
826         if (!path)
827                 return NULL;
828         strcpy(path, dir);
829         if (dir[strlen(dir) - 1] != '/')
830                 strcat(path, "/");
831         strcat(path, name);
832         return path;
833 }
834
835 static int traverse_directory(struct btrfs_trans_handle *trans,
836                               struct btrfs_root *root, char *dir_name,
837                               struct directory_name_entry *dir_head, int out_fd)
838 {
839         int ret = 0;
840
841         struct btrfs_inode_item cur_inode;
842         struct btrfs_inode_item *inode_item;
843         int count, i, dir_index_cnt;
844         struct direct **files;
845         struct stat st;
846         struct directory_name_entry *dir_entry, *parent_dir_entry;
847         struct direct *cur_file;
848         ino_t parent_inum, cur_inum;
849         ino_t highest_inum = 0;
850         char *parent_dir_name;
851         struct btrfs_path path;
852         struct extent_buffer *leaf;
853         struct btrfs_key root_dir_key;
854         u64 root_dir_inode_size = 0;
855
856         /* Add list for source directory */
857         dir_entry = malloc(sizeof(struct directory_name_entry));
858         dir_entry->dir_name = dir_name;
859         dir_entry->path = strdup(dir_name);
860
861         parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
862         dir_entry->inum = parent_inum;
863         list_add_tail(&dir_entry->list, &dir_head->list);
864
865         btrfs_init_path(&path);
866
867         root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
868         root_dir_key.offset = 0;
869         btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
870         ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
871         if (ret) {
872                 fprintf(stderr, "root dir lookup error\n");
873                 return -1;
874         }
875
876         leaf = path.nodes[0];
877         inode_item = btrfs_item_ptr(leaf, path.slots[0],
878                                     struct btrfs_inode_item);
879
880         root_dir_inode_size = calculate_dir_inode_size(dir_name);
881         btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
882         btrfs_mark_buffer_dirty(leaf);
883
884         btrfs_release_path(&path);
885
886         do {
887                 parent_dir_entry = list_entry(dir_head->list.next,
888                                               struct directory_name_entry,
889                                               list);
890                 list_del(&parent_dir_entry->list);
891
892                 parent_inum = parent_dir_entry->inum;
893                 parent_dir_name = parent_dir_entry->dir_name;
894                 if (chdir(parent_dir_entry->path)) {
895                         fprintf(stderr, "chdir error for %s\n",
896                                 parent_dir_name);
897                         goto fail_no_files;
898                 }
899
900                 count = scandir(parent_dir_entry->path, &files,
901                                 directory_select, NULL);
902                 if (count == -1)
903                 {
904                         fprintf(stderr, "scandir for %s failed: %s\n",
905                                 parent_dir_name, strerror (errno));
906                         goto fail;
907                 }
908
909                 for (i = 0; i < count; i++) {
910                         cur_file = files[i];
911
912                         if (lstat(cur_file->d_name, &st) == -1) {
913                                 fprintf(stderr, "lstat failed for file %s\n",
914                                         cur_file->d_name);
915                                 goto fail;
916                         }
917
918                         cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
919                         ret = add_directory_items(trans, root,
920                                                   cur_inum, parent_inum,
921                                                   cur_file->d_name,
922                                                   &st, &dir_index_cnt);
923                         if (ret) {
924                                 fprintf(stderr, "add_directory_items failed\n");
925                                 goto fail;
926                         }
927
928                         ret = add_inode_items(trans, root, &st,
929                                               cur_file->d_name, cur_inum,
930                                               parent_inum, dir_index_cnt,
931                                               &cur_inode);
932                         if (ret) {
933                                 fprintf(stderr, "add_inode_items failed\n");
934                                 goto fail;
935                         }
936
937                         ret = add_xattr_item(trans, root,
938                                              cur_inum, cur_file->d_name);
939                         if (ret) {
940                                 fprintf(stderr, "add_xattr_item failed\n");
941                                 if(ret != -ENOTSUP)
942                                         goto fail;
943                         }
944
945                         if (S_ISDIR(st.st_mode)) {
946                                 dir_entry = malloc(sizeof(struct directory_name_entry));
947                                 dir_entry->dir_name = cur_file->d_name;
948                                 dir_entry->path = make_path(parent_dir_entry->path,
949                                                             cur_file->d_name);
950                                 dir_entry->inum = cur_inum;
951                                 list_add_tail(&dir_entry->list, &dir_head->list);
952                         } else if (S_ISREG(st.st_mode)) {
953                                 ret = add_file_items(trans, root, &cur_inode,
954                                                      cur_inum, parent_inum, &st,
955                                                      cur_file->d_name, out_fd);
956                                 if (ret) {
957                                         fprintf(stderr, "add_file_items failed\n");
958                                         goto fail;
959                                 }
960                         } else if (S_ISLNK(st.st_mode)) {
961                                 ret = add_symbolic_link(trans, root,
962                                                         cur_inum, cur_file->d_name);
963                                 if (ret) {
964                                         fprintf(stderr, "add_symbolic_link failed\n");
965                                         goto fail;
966                                 }
967                         }
968                 }
969
970                 free_namelist(files, count);
971                 free(parent_dir_entry->path);
972                 free(parent_dir_entry);
973
974                 index_cnt = 2;
975
976         } while (!list_empty(&dir_head->list));
977
978         return 0;
979 fail:
980         free_namelist(files, count);
981 fail_no_files:
982         free(parent_dir_entry->path);
983         free(parent_dir_entry);
984         return -1;
985 }
986
987 static int open_target(char *output_name)
988 {
989         int output_fd;
990         output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
991                          S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
992
993         return output_fd;
994 }
995
996 static int create_chunks(struct btrfs_trans_handle *trans,
997                          struct btrfs_root *root, u64 num_of_meta_chunks,
998                          u64 size_of_data)
999 {
1000         u64 chunk_start;
1001         u64 chunk_size;
1002         u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1003         u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1004         u64 minimum_data_chunk_size = 8 * 1024 * 1024;
1005         u64 i;
1006         int ret;
1007
1008         for (i = 0; i < num_of_meta_chunks; i++) {
1009                 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1010                                         &chunk_start, &chunk_size, meta_type);
1011                 BUG_ON(ret);
1012                 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1013                                              meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1014                                              chunk_start, chunk_size);
1015                 BUG_ON(ret);
1016                 set_extent_dirty(&root->fs_info->free_space_cache,
1017                                  chunk_start, chunk_start + chunk_size - 1, 0);
1018         }
1019
1020         if (size_of_data < minimum_data_chunk_size)
1021                 size_of_data = minimum_data_chunk_size;
1022         ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1023                                      &chunk_start, size_of_data, data_type);
1024         BUG_ON(ret);
1025         ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1026                                      data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1027                                      chunk_start, size_of_data);
1028         BUG_ON(ret);
1029         set_extent_dirty(&root->fs_info->free_space_cache,
1030                          chunk_start, chunk_start + size_of_data - 1, 0);
1031         return ret;
1032 }
1033
1034 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1035 {
1036         int ret;
1037         struct btrfs_trans_handle *trans;
1038
1039         struct stat root_st;
1040
1041         struct directory_name_entry dir_head;
1042
1043         struct directory_name_entry *dir_entry = NULL;
1044
1045         ret = lstat(source_dir, &root_st);
1046         if (ret) {
1047                 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1048                 goto fail;
1049         }
1050
1051         INIT_LIST_HEAD(&dir_head.list);
1052
1053         trans = btrfs_start_transaction(root, 1);
1054         ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1055         if (ret) {
1056                 fprintf(stderr, "unable to traverse_directory\n");
1057                 goto fail;
1058         }
1059         btrfs_commit_transaction(trans, root);
1060
1061         printf("Making image is completed.\n");
1062         return 0;
1063 fail:
1064         while (!list_empty(&dir_head.list)) {
1065                 dir_entry = list_entry(dir_head.list.next,
1066                                        struct directory_name_entry, list);
1067                 list_del(&dir_entry->list);
1068                 free(dir_entry);
1069         }
1070         fprintf(stderr, "Making image is aborted.\n");
1071         return -1;
1072 }
1073
1074 /*
1075  * This ignores symlinks with unreadable targets and subdirs that can't
1076  * be read.  It's a best-effort to give a rough estimate of the size of
1077  * a subdir.  It doesn't guarantee that prepopulating btrfs from this
1078  * tree won't still run out of space. 
1079  *
1080  * The rounding up to 4096 is questionable.  Previous code used du -B 4096.
1081  */
1082 static u64 global_total_size;
1083 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1084                               int type)
1085 {
1086         if (type == FTW_F || type == FTW_D)
1087                 global_total_size += round_up(st->st_size, 4096);
1088
1089         return 0;
1090 }
1091
1092 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1093                           u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1094 {
1095         u64 dir_size = 0;
1096         u64 total_size = 0;
1097         int ret;
1098         u64 default_chunk_size = 8 * 1024 * 1024;       /* 8MB */
1099         u64 allocated_meta_size = 8 * 1024 * 1024;      /* 8MB */
1100         u64 allocated_total_size = 20 * 1024 * 1024;    /* 20MB */
1101         u64 num_of_meta_chunks = 0;
1102         u64 num_of_allocated_meta_chunks =
1103                         allocated_meta_size / default_chunk_size;
1104
1105         global_total_size = 0;
1106         ret = ftw(dir_name, ftw_add_entry_size, 10);
1107         dir_size = global_total_size;
1108         if (ret < 0) {
1109                 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1110                         dir_name, strerror(errno));
1111                 exit(1);
1112         }
1113
1114         num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1115         if (((dir_size / 2) % default_chunk_size) != 0)
1116                 num_of_meta_chunks++;
1117         if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1118                 num_of_meta_chunks = 0;
1119         else
1120                 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1121
1122         total_size = allocated_total_size + dir_size +
1123                      (num_of_meta_chunks * default_chunk_size);
1124
1125         *num_of_meta_chunks_ret = num_of_meta_chunks;
1126
1127         return total_size;
1128 }
1129
1130 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1131 {
1132         int len = sectorsize;
1133         int loop_num = size / sectorsize;
1134         u64 location = 0;
1135         char *buf = malloc(len);
1136         int ret = 0, i;
1137         ssize_t written;
1138
1139         if (!buf)
1140                 return -ENOMEM;
1141         memset(buf, 0, len);
1142         for (i = 0; i < loop_num; i++) {
1143                 written = pwrite64(out_fd, buf, len, location);
1144                 if (written != len)
1145                         ret = -EIO;
1146                 location += sectorsize;
1147         }
1148         free(buf);
1149         return ret;
1150 }
1151
1152 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1153 {
1154         if (size < sectorsize) {
1155                 fprintf(stderr,
1156                         "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1157                         size, sectorsize);
1158                 return -1;
1159         } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1160                 fprintf(stderr,
1161                         "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1162                         size, BTRFS_MAX_METADATA_BLOCKSIZE);
1163                 return -1;
1164         } else if (size & (sectorsize - 1)) {
1165                 fprintf(stderr,
1166                         "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1167                         size, sectorsize);
1168                 return -1;
1169         }
1170         return 0;
1171 }
1172
1173 static int is_ssd(const char *file)
1174 {
1175         blkid_probe probe;
1176         char wholedisk[32];
1177         char sysfs_path[PATH_MAX];
1178         dev_t devno;
1179         int fd;
1180         char rotational;
1181         int ret;
1182
1183         probe = blkid_new_probe_from_filename(file);
1184         if (!probe)
1185                 return 0;
1186
1187         /* Device number of this disk (possibly a partition) */
1188         devno = blkid_probe_get_devno(probe);
1189         if (!devno) {
1190                 blkid_free_probe(probe);
1191                 return 0;
1192         }
1193
1194         /* Get whole disk name (not full path) for this devno */
1195         ret = blkid_devno_to_wholedisk(devno,
1196                         wholedisk, sizeof(wholedisk), NULL);
1197         if (ret) {
1198                 blkid_free_probe(probe);
1199                 return 0;
1200         }
1201
1202         snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1203                  wholedisk);
1204
1205         blkid_free_probe(probe);
1206
1207         fd = open(sysfs_path, O_RDONLY);
1208         if (fd < 0) {
1209                 return 0;
1210         }
1211
1212         if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1213                 close(fd);
1214                 return 0;
1215         }
1216         close(fd);
1217
1218         return !atoi((const char *)&rotational);
1219 }
1220
1221 #define BTRFS_FEATURE_LIST_ALL          (1ULL << 63)
1222
1223 static const struct btrfs_fs_feature {
1224         const char *name;
1225         u64 flag;
1226         const char *desc;
1227 } mkfs_features[] = {
1228         { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1229                 "mixed data and metadata block groups" },
1230         { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1231                 "increased hardlink limit per file to 65536" },
1232         { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1233                 "raid56 extended format" },
1234         { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1235                 "reduced-size metadata extent refs" },
1236         /* Keep this one last */
1237         { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1238 };
1239
1240 static void list_all_fs_features(void)
1241 {
1242         int i;
1243
1244         fprintf(stderr, "Filesystem features available at mkfs time:\n");
1245         for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1246                 fprintf(stderr, "%-20s- %s (0x%llx)\n",
1247                                 mkfs_features[i].name,
1248                                 mkfs_features[i].desc,
1249                                 mkfs_features[i].flag);
1250         }
1251 }
1252
1253 static int parse_one_fs_feature(const char *name, u64 *flags)
1254 {
1255         int i;
1256         int found = 0;
1257
1258         for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1259                 if (!strcmp(mkfs_features[i].name, name)) {
1260                         *flags |= mkfs_features[i].flag;
1261                         found = 1;
1262                 }
1263         }
1264
1265         return !found;
1266 }
1267
1268 static void process_fs_features(u64 flags)
1269 {
1270         int i;
1271
1272         for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1273                 if (flags & mkfs_features[i].flag) {
1274                         fprintf(stderr,
1275                                 "Turning ON incompat feature '%s': %s\n",
1276                                 mkfs_features[i].name,
1277                                 mkfs_features[i].desc);
1278                 }
1279         }
1280 }
1281
1282
1283 /*
1284  * Return NULL if all features were parsed fine, otherwise return the name of
1285  * the first unparsed.
1286  */
1287 static char* parse_fs_features(char *namelist, u64 *flags)
1288 {
1289         char *this_char;
1290         char *save_ptr = NULL; /* Satisfy static checkers */
1291
1292         for (this_char = strtok_r(namelist, ",", &save_ptr);
1293              this_char != NULL;
1294              this_char = strtok_r(NULL, ",", &save_ptr)) {
1295                 if (parse_one_fs_feature(this_char, flags))
1296                         return this_char;
1297         }
1298
1299         return NULL;
1300 }
1301
1302 int main(int ac, char **av)
1303 {
1304         char *file;
1305         struct btrfs_root *root;
1306         struct btrfs_trans_handle *trans;
1307         char *label = NULL;
1308         char *first_file;
1309         u64 block_count = 0;
1310         u64 dev_block_count = 0;
1311         u64 blocks[7];
1312         u64 alloc_start = 0;
1313         u64 metadata_profile = 0;
1314         u64 data_profile = 0;
1315         u32 leafsize = sysconf(_SC_PAGESIZE);
1316         u32 sectorsize = 4096;
1317         u32 nodesize = leafsize;
1318         u32 stripesize = 4096;
1319         int zero_end = 1;
1320         int option_index = 0;
1321         int fd;
1322         int ret;
1323         int i;
1324         int mixed = 0;
1325         int data_profile_opt = 0;
1326         int metadata_profile_opt = 0;
1327         int discard = 1;
1328         int ssd = 0;
1329         int force_overwrite = 0;
1330
1331         char *source_dir = NULL;
1332         int source_dir_set = 0;
1333         u64 num_of_meta_chunks = 0;
1334         u64 size_of_data = 0;
1335         u64 source_dir_size = 0;
1336         int dev_cnt = 0;
1337         int saved_optind;
1338         char estr[100];
1339         u64 features = 0;
1340
1341         while(1) {
1342                 int c;
1343                 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK",
1344                                 long_options, &option_index);
1345                 if (c < 0)
1346                         break;
1347                 switch(c) {
1348                         case 'A':
1349                                 alloc_start = parse_size(optarg);
1350                                 break;
1351                         case 'f':
1352                                 force_overwrite = 1;
1353                                 break;
1354                         case 'd':
1355                                 data_profile = parse_profile(optarg);
1356                                 data_profile_opt = 1;
1357                                 break;
1358                         case 'l':
1359                         case 'n':
1360                                 nodesize = parse_size(optarg);
1361                                 leafsize = parse_size(optarg);
1362                                 break;
1363                         case 'L':
1364                                 label = parse_label(optarg);
1365                                 break;
1366                         case 'm':
1367                                 metadata_profile = parse_profile(optarg);
1368                                 metadata_profile_opt = 1;
1369                                 break;
1370                         case 'M':
1371                                 mixed = 1;
1372                                 break;
1373                         case 'O': {
1374                                 char *orig = strdup(optarg);
1375                                 char *tmp = orig;
1376
1377                                 tmp = parse_fs_features(tmp, &features);
1378                                 if (tmp) {
1379                                         fprintf(stderr,
1380                                                 "Unrecognized filesystem feature '%s'\n",
1381                                                         tmp);
1382                                         free(orig);
1383                                         exit(1);
1384                                 }
1385                                 free(orig);
1386                                 if (features & BTRFS_FEATURE_LIST_ALL) {
1387                                         list_all_fs_features();
1388                                         exit(0);
1389                                 }
1390                                 break;
1391                                 }
1392                         case 's':
1393                                 sectorsize = parse_size(optarg);
1394                                 break;
1395                         case 'b':
1396                                 block_count = parse_size(optarg);
1397                                 if (block_count <= 1024*1024*1024) {
1398                                         printf("SMALL VOLUME: forcing mixed "
1399                                                "metadata/data groups\n");
1400                                         mixed = 1;
1401                                 }
1402                                 zero_end = 0;
1403                                 break;
1404                         case 'V':
1405                                 print_version();
1406                                 break;
1407                         case 'r':
1408                                 source_dir = optarg;
1409                                 source_dir_set = 1;
1410                                 break;
1411                         case 'K':
1412                                 discard = 0;
1413                                 break;
1414                         default:
1415                                 print_usage();
1416                 }
1417         }
1418         sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1419         if (check_leaf_or_node_size(leafsize, sectorsize))
1420                 exit(1);
1421         if (check_leaf_or_node_size(nodesize, sectorsize))
1422                 exit(1);
1423         saved_optind = optind;
1424         dev_cnt = ac - optind;
1425         if (dev_cnt == 0)
1426                 print_usage();
1427
1428         if (source_dir_set && dev_cnt > 1) {
1429                 fprintf(stderr,
1430                         "The -r option is limited to a single device\n");
1431                 exit(1);
1432         }
1433         while (dev_cnt-- > 0) {
1434                 file = av[optind++];
1435                 if (is_block_device(file))
1436                         if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1437                                 fprintf(stderr, "Error: %s", estr);
1438                                 exit(1);
1439                         }
1440         }
1441
1442         optind = saved_optind;
1443         dev_cnt = ac - optind;
1444
1445         file = av[optind++];
1446         ssd = is_ssd(file);
1447
1448         if (is_vol_small(file)) {
1449                 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1450                 mixed = 1;
1451                 if (metadata_profile != data_profile) {
1452                         if (metadata_profile_opt || data_profile_opt) {
1453                                 fprintf(stderr,
1454         "With mixed block groups data and metadata profiles must be the same\n");
1455                                 exit(1);
1456                         }
1457                 }
1458         }
1459         /*
1460         * Set default profiles according to number of added devices.
1461         * For mixed groups defaults are single/single.
1462         */
1463         if (!mixed) {
1464                 if (!metadata_profile_opt) {
1465                         if (dev_cnt == 1 && ssd)
1466                                 printf("Detected a SSD, turning off metadata "
1467                                 "duplication.  Mkfs with -m dup if you want to "
1468                                 "force metadata duplication.\n");
1469
1470                         metadata_profile = (dev_cnt > 1) ?
1471                                         BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1472                                         0: BTRFS_BLOCK_GROUP_DUP;
1473                 }
1474                 if (!data_profile_opt) {
1475                         data_profile = (dev_cnt > 1) ?
1476                                 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1477                 }
1478         } else {
1479                 metadata_profile = 0;
1480                 data_profile = 0;
1481         }
1482
1483         ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1484                         dev_cnt, mixed, estr);
1485         if (ret) {
1486                 fprintf(stderr, "Error: %s\n", estr);
1487                 exit(1);
1488         }
1489
1490         /* if we are here that means all devs are good to btrfsify */
1491         printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1492         printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1493
1494         dev_cnt--;
1495
1496         if (!source_dir_set) {
1497                 /*
1498                  * open without O_EXCL so that the problem should not
1499                  * occur by the following processing.
1500                  * (btrfs_register_one_device() fails if O_EXCL is on)
1501                  */
1502                 fd = open(file, O_RDWR);
1503                 if (fd < 0) {
1504                         fprintf(stderr, "unable to open %s: %s\n", file,
1505                                 strerror(errno));
1506                         exit(1);
1507                 }
1508                 first_file = file;
1509                 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1510                                            block_count, &mixed, discard);
1511                 if (block_count && block_count > dev_block_count) {
1512                         fprintf(stderr, "%s is smaller than requested size\n", file);
1513                         exit(1);
1514                 }
1515         } else {
1516                 fd = open_target(file);
1517                 if (fd < 0) {
1518                         fprintf(stderr, "unable to open the %s\n", file);
1519                         exit(1);
1520                 }
1521
1522                 first_file = file;
1523                 source_dir_size = size_sourcedir(source_dir, sectorsize,
1524                                              &num_of_meta_chunks, &size_of_data);
1525                 if(block_count < source_dir_size)
1526                         block_count = source_dir_size;
1527                 ret = zero_output_file(fd, block_count, sectorsize);
1528                 if (ret) {
1529                         fprintf(stderr, "unable to zero the output file\n");
1530                         exit(1);
1531                 }
1532                 /* our "device" is the new image file */
1533                 dev_block_count = block_count;
1534         }
1535
1536         /* To create the first block group and chunk 0 in make_btrfs */
1537         if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1538                 fprintf(stderr, "device is too small to make filesystem\n");
1539                 exit(1);
1540         }
1541
1542         blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1543         for (i = 1; i < 7; i++) {
1544                 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1545                         leafsize * i;
1546         }
1547
1548         /*
1549          * FS features that can be set by other means than -O
1550          * just set the bit here
1551          */
1552         if (mixed)
1553                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1554
1555         if ((data_profile | metadata_profile) &
1556             (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1557                 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1558         }
1559
1560         process_fs_features(features);
1561
1562         ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1563                          nodesize, leafsize,
1564                          sectorsize, stripesize, features);
1565         if (ret) {
1566                 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1567                 exit(1);
1568         }
1569
1570         root = open_ctree(file, 0, O_RDWR);
1571         if (!root) {
1572                 fprintf(stderr, "Open ctree failed\n");
1573                 close(fd);
1574                 exit(1);
1575         }
1576         root->fs_info->alloc_start = alloc_start;
1577
1578         ret = make_root_dir(root, mixed);
1579         if (ret) {
1580                 fprintf(stderr, "failed to setup the root directory\n");
1581                 exit(1);
1582         }
1583
1584         trans = btrfs_start_transaction(root, 1);
1585
1586         if (dev_cnt == 0)
1587                 goto raid_groups;
1588
1589         btrfs_register_one_device(file);
1590
1591         zero_end = 1;
1592         while (dev_cnt-- > 0) {
1593                 int old_mixed = mixed;
1594
1595                 file = av[optind++];
1596
1597                 /*
1598                  * open without O_EXCL so that the problem should not
1599                  * occur by the following processing.
1600                  * (btrfs_register_one_device() fails if O_EXCL is on)
1601                  */
1602                 fd = open(file, O_RDWR);
1603                 if (fd < 0) {
1604                         fprintf(stderr, "unable to open %s: %s\n", file,
1605                                 strerror(errno));
1606                         exit(1);
1607                 }
1608                 ret = btrfs_device_already_in_root(root, fd,
1609                                                    BTRFS_SUPER_INFO_OFFSET);
1610                 if (ret) {
1611                         fprintf(stderr, "skipping duplicate device %s in FS\n",
1612                                 file);
1613                         close(fd);
1614                         continue;
1615                 }
1616                 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1617                                            block_count, &mixed, discard);
1618                 mixed = old_mixed;
1619                 BUG_ON(ret);
1620
1621                 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1622                                         sectorsize, sectorsize, sectorsize);
1623                 BUG_ON(ret);
1624                 btrfs_register_one_device(file);
1625         }
1626
1627 raid_groups:
1628         if (!source_dir_set) {
1629                 ret = create_raid_groups(trans, root, data_profile,
1630                                  data_profile_opt, metadata_profile,
1631                                  metadata_profile_opt, mixed, ssd);
1632                 BUG_ON(ret);
1633         }
1634
1635         ret = create_data_reloc_tree(trans, root);
1636         BUG_ON(ret);
1637
1638         printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1639             "sectorsize %u size %s\n",
1640             label, first_file, nodesize, leafsize, sectorsize,
1641             pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1642
1643         printf("%s\n", BTRFS_BUILD_VERSION);
1644         btrfs_commit_transaction(trans, root);
1645
1646         if (source_dir_set) {
1647                 trans = btrfs_start_transaction(root, 1);
1648                 ret = create_chunks(trans, root,
1649                                     num_of_meta_chunks, size_of_data);
1650                 BUG_ON(ret);
1651                 btrfs_commit_transaction(trans, root);
1652
1653                 ret = make_image(source_dir, root, fd);
1654                 BUG_ON(ret);
1655         }
1656
1657         ret = close_ctree(root);
1658         BUG_ON(ret);
1659         free(label);
1660         return 0;
1661 }