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