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