btrfs-progs: print-tree: Enhance warning on tree block level mismatch and error handling
[platform/upstream/btrfs-progs.git] / mkfs / rootdir.c
1 /*
2  * Copyright (C) 2017 SUSE.  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.
15  */
16
17 #include "kerncompat.h"
18 #include "androidcompat.h"
19
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <sys/xattr.h>
23 #include <linux/limits.h>
24 #include <dirent.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <ftw.h>
28 #include "ctree.h"
29 #include "volumes.h"
30 #include "internal.h"
31 #include "disk-io.h"
32 #include "messages.h"
33 #include "transaction.h"
34 #include "utils.h"
35 #include "mkfs/rootdir.h"
36 #include "mkfs/common.h"
37 #include "send-utils.h"
38
39 static u32 fs_block_size;
40
41 static u64 index_cnt = 2;
42
43 /*
44  * Size estimate will be done using the following data:
45  * 1) Number of inodes
46  *    Since we will later shrink the fs, over-estimate is completely fine here
47  *    as long as our estimate ensures we can populate the image without ENOSPC.
48  *    So we only record how many inodes there are, and account the maximum
49  *    space for each inode.
50  *
51  * 2) Data space for each (regular) inode
52  *    To estimate data chunk size.
53  *    Don't care if it can fit as an inline extent.
54  *    Always round them up to sectorsize.
55  */
56 static u64 ftw_meta_nr_inode;
57 static u64 ftw_data_size;
58
59 static int add_directory_items(struct btrfs_trans_handle *trans,
60                                struct btrfs_root *root, u64 objectid,
61                                ino_t parent_inum, const char *name,
62                                struct stat *st, int *dir_index_cnt)
63 {
64         int ret;
65         int name_len;
66         struct btrfs_key location;
67         u8 filetype = 0;
68
69         name_len = strlen(name);
70
71         location.objectid = objectid;
72         location.offset = 0;
73         location.type = BTRFS_INODE_ITEM_KEY;
74
75         if (S_ISDIR(st->st_mode))
76                 filetype = BTRFS_FT_DIR;
77         if (S_ISREG(st->st_mode))
78                 filetype = BTRFS_FT_REG_FILE;
79         if (S_ISLNK(st->st_mode))
80                 filetype = BTRFS_FT_SYMLINK;
81         if (S_ISSOCK(st->st_mode))
82                 filetype = BTRFS_FT_SOCK;
83         if (S_ISCHR(st->st_mode))
84                 filetype = BTRFS_FT_CHRDEV;
85         if (S_ISBLK(st->st_mode))
86                 filetype = BTRFS_FT_BLKDEV;
87         if (S_ISFIFO(st->st_mode))
88                 filetype = BTRFS_FT_FIFO;
89
90         ret = btrfs_insert_dir_item(trans, root, name, name_len,
91                                     parent_inum, &location,
92                                     filetype, index_cnt);
93         if (ret)
94                 return ret;
95         ret = btrfs_insert_inode_ref(trans, root, name, name_len,
96                                      objectid, parent_inum, index_cnt);
97         *dir_index_cnt = index_cnt;
98         index_cnt++;
99
100         return ret;
101 }
102
103 static int fill_inode_item(struct btrfs_trans_handle *trans,
104                            struct btrfs_root *root,
105                            struct btrfs_inode_item *dst, struct stat *src)
106 {
107         u64 blocks = 0;
108         u64 sectorsize = root->fs_info->sectorsize;
109
110         /*
111          * btrfs_inode_item has some reserved fields
112          * and represents on-disk inode entry, so
113          * zero everything to prevent information leak
114          */
115         memset(dst, 0, sizeof(*dst));
116
117         btrfs_set_stack_inode_generation(dst, trans->transid);
118         btrfs_set_stack_inode_size(dst, src->st_size);
119         btrfs_set_stack_inode_nbytes(dst, 0);
120         btrfs_set_stack_inode_block_group(dst, 0);
121         btrfs_set_stack_inode_nlink(dst, src->st_nlink);
122         btrfs_set_stack_inode_uid(dst, src->st_uid);
123         btrfs_set_stack_inode_gid(dst, src->st_gid);
124         btrfs_set_stack_inode_mode(dst, src->st_mode);
125         btrfs_set_stack_inode_rdev(dst, 0);
126         btrfs_set_stack_inode_flags(dst, 0);
127         btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
128         btrfs_set_stack_timespec_nsec(&dst->atime, 0);
129         btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
130         btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
131         btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
132         btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
133         btrfs_set_stack_timespec_sec(&dst->otime, 0);
134         btrfs_set_stack_timespec_nsec(&dst->otime, 0);
135
136         if (S_ISDIR(src->st_mode)) {
137                 btrfs_set_stack_inode_size(dst, 0);
138                 btrfs_set_stack_inode_nlink(dst, 1);
139         }
140         if (S_ISREG(src->st_mode)) {
141                 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
142                 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info))
143                         btrfs_set_stack_inode_nbytes(dst, src->st_size);
144                 else {
145                         blocks = src->st_size / sectorsize;
146                         if (src->st_size % sectorsize)
147                                 blocks += 1;
148                         blocks *= sectorsize;
149                         btrfs_set_stack_inode_nbytes(dst, blocks);
150                 }
151         }
152         if (S_ISLNK(src->st_mode))
153                 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
154
155         return 0;
156 }
157
158 static int directory_select(const struct direct *entry)
159 {
160         if (entry->d_name[0] == '.' &&
161                 (entry->d_name[1] == 0 ||
162                  (entry->d_name[1] == '.' && entry->d_name[2] == 0)))
163                 return 0;
164         return 1;
165 }
166
167 static void free_namelist(struct direct **files, int count)
168 {
169         int i;
170
171         if (count < 0)
172                 return;
173
174         for (i = 0; i < count; ++i)
175                 free(files[i]);
176         free(files);
177 }
178
179 static u64 calculate_dir_inode_size(const char *dirname)
180 {
181         int count, i;
182         struct direct **files, *cur_file;
183         u64 dir_inode_size = 0;
184
185         count = scandir(dirname, &files, directory_select, NULL);
186
187         for (i = 0; i < count; i++) {
188                 cur_file = files[i];
189                 dir_inode_size += strlen(cur_file->d_name);
190         }
191
192         free_namelist(files, count);
193
194         dir_inode_size *= 2;
195         return dir_inode_size;
196 }
197
198 static int add_inode_items(struct btrfs_trans_handle *trans,
199                            struct btrfs_root *root,
200                            struct stat *st, const char *name,
201                            u64 self_objectid,
202                            struct btrfs_inode_item *inode_ret)
203 {
204         int ret;
205         struct btrfs_inode_item btrfs_inode;
206         u64 objectid;
207         u64 inode_size = 0;
208
209         fill_inode_item(trans, root, &btrfs_inode, st);
210         objectid = self_objectid;
211
212         if (S_ISDIR(st->st_mode)) {
213                 inode_size = calculate_dir_inode_size(name);
214                 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
215         }
216
217         ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
218
219         *inode_ret = btrfs_inode;
220         return ret;
221 }
222
223 static int add_xattr_item(struct btrfs_trans_handle *trans,
224                           struct btrfs_root *root, u64 objectid,
225                           const char *file_name)
226 {
227         int ret;
228         int cur_name_len;
229         char xattr_list[XATTR_LIST_MAX];
230         char *cur_name;
231         char cur_value[XATTR_SIZE_MAX];
232         char delimiter = '\0';
233         char *next_location = xattr_list;
234
235         ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
236         if (ret < 0) {
237                 if (errno == ENOTSUP)
238                         return 0;
239                 error("getting a list of xattr failed for %s: %s", file_name,
240                                 strerror(errno));
241                 return ret;
242         }
243         if (ret == 0)
244                 return ret;
245
246         cur_name = strtok(xattr_list, &delimiter);
247         while (cur_name != NULL) {
248                 cur_name_len = strlen(cur_name);
249                 next_location += cur_name_len + 1;
250
251                 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
252                 if (ret < 0) {
253                         if (errno == ENOTSUP)
254                                 return 0;
255                         error("gettig a xattr value failed for %s attr %s: %s",
256                                 file_name, cur_name, strerror(errno));
257                         return ret;
258                 }
259
260                 ret = btrfs_insert_xattr_item(trans, root, cur_name,
261                                               cur_name_len, cur_value,
262                                               ret, objectid);
263                 if (ret) {
264                         error("inserting a xattr item failed for %s: %s",
265                                         file_name, strerror(-ret));
266                 }
267
268                 cur_name = strtok(next_location, &delimiter);
269         }
270
271         return ret;
272 }
273
274 static int add_symbolic_link(struct btrfs_trans_handle *trans,
275                              struct btrfs_root *root,
276                              u64 objectid, const char *path_name)
277 {
278         int ret;
279         char buf[PATH_MAX];
280
281         ret = readlink(path_name, buf, sizeof(buf));
282         if (ret <= 0) {
283                 error("readlink failed for %s: %s", path_name, strerror(errno));
284                 goto fail;
285         }
286         if (ret >= sizeof(buf)) {
287                 error("symlink too long for %s", path_name);
288                 ret = -1;
289                 goto fail;
290         }
291
292         buf[ret] = '\0'; /* readlink does not do it for us */
293         ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
294                                          buf, ret + 1);
295 fail:
296         return ret;
297 }
298
299 static int add_file_items(struct btrfs_trans_handle *trans,
300                           struct btrfs_root *root,
301                           struct btrfs_inode_item *btrfs_inode, u64 objectid,
302                           struct stat *st, const char *path_name)
303 {
304         int ret = -1;
305         ssize_t ret_read;
306         u64 bytes_read = 0;
307         struct btrfs_key key;
308         int blocks;
309         u32 sectorsize = root->fs_info->sectorsize;
310         u64 first_block = 0;
311         u64 file_pos = 0;
312         u64 cur_bytes;
313         u64 total_bytes;
314         struct extent_buffer *eb = NULL;
315         int fd;
316
317         if (st->st_size == 0)
318                 return 0;
319
320         fd = open(path_name, O_RDONLY);
321         if (fd == -1) {
322                 error("cannot open %s: %s", path_name, strerror(errno));
323                 return ret;
324         }
325
326         blocks = st->st_size / sectorsize;
327         if (st->st_size % sectorsize)
328                 blocks += 1;
329
330         if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info)) {
331                 char *buffer = malloc(st->st_size);
332
333                 if (!buffer) {
334                         ret = -ENOMEM;
335                         goto end;
336                 }
337
338                 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
339                 if (ret_read == -1) {
340                         error("cannot read %s at offset %llu length %llu: %s",
341                                 path_name, (unsigned long long)bytes_read,
342                                 (unsigned long long)st->st_size,
343                                 strerror(errno));
344                         free(buffer);
345                         goto end;
346                 }
347
348                 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
349                                                  buffer, st->st_size);
350                 free(buffer);
351                 goto end;
352         }
353
354         /* round up our st_size to the FS blocksize */
355         total_bytes = (u64)blocks * sectorsize;
356
357         /*
358          * do our IO in extent buffers so it can work
359          * against any raid type
360          */
361         eb = calloc(1, sizeof(*eb) + sectorsize);
362         if (!eb) {
363                 ret = -ENOMEM;
364                 goto end;
365         }
366
367 again:
368
369         /*
370          * keep our extent size at 1MB max, this makes it easier to work inside
371          * the tiny block groups created during mkfs
372          */
373         cur_bytes = min(total_bytes, (u64)SZ_1M);
374         ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
375                                    &key, 1);
376         if (ret)
377                 goto end;
378
379         first_block = key.objectid;
380         bytes_read = 0;
381
382         while (bytes_read < cur_bytes) {
383
384                 memset(eb->data, 0, sectorsize);
385
386                 ret_read = pread64(fd, eb->data, sectorsize, file_pos +
387                                    bytes_read);
388                 if (ret_read == -1) {
389                         error("cannot read %s at offset %llu length %llu: %s",
390                                 path_name,
391                                 (unsigned long long)file_pos + bytes_read,
392                                 (unsigned long long)sectorsize,
393                                 strerror(errno));
394                         goto end;
395                 }
396
397                 eb->start = first_block + bytes_read;
398                 eb->len = sectorsize;
399
400                 /*
401                  * we're doing the csum before we record the extent, but
402                  * that's ok
403                  */
404                 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
405                                 first_block + bytes_read + sectorsize,
406                                 first_block + bytes_read,
407                                 eb->data, sectorsize);
408                 if (ret)
409                         goto end;
410
411                 ret = write_and_map_eb(root->fs_info, eb);
412                 if (ret) {
413                         error("failed to write %s", path_name);
414                         goto end;
415                 }
416
417                 bytes_read += sectorsize;
418         }
419
420         if (bytes_read) {
421                 ret = btrfs_record_file_extent(trans, root, objectid,
422                                 btrfs_inode, file_pos, first_block, cur_bytes);
423                 if (ret)
424                         goto end;
425
426         }
427
428         file_pos += cur_bytes;
429         total_bytes -= cur_bytes;
430
431         if (total_bytes)
432                 goto again;
433
434 end:
435         free(eb);
436         close(fd);
437         return ret;
438 }
439
440 static int traverse_directory(struct btrfs_trans_handle *trans,
441                               struct btrfs_root *root, const char *dir_name,
442                               struct directory_name_entry *dir_head)
443 {
444         int ret = 0;
445
446         struct btrfs_inode_item cur_inode;
447         struct btrfs_inode_item *inode_item;
448         int count, i, dir_index_cnt;
449         struct direct **files;
450         struct stat st;
451         struct directory_name_entry *dir_entry, *parent_dir_entry;
452         struct direct *cur_file;
453         ino_t parent_inum, cur_inum;
454         ino_t highest_inum = 0;
455         const char *parent_dir_name;
456         struct btrfs_path path;
457         struct extent_buffer *leaf;
458         struct btrfs_key root_dir_key;
459         u64 root_dir_inode_size = 0;
460
461         /* Add list for source directory */
462         dir_entry = malloc(sizeof(struct directory_name_entry));
463         if (!dir_entry)
464                 return -ENOMEM;
465         dir_entry->dir_name = dir_name;
466         dir_entry->path = realpath(dir_name, NULL);
467         if (!dir_entry->path) {
468                 error("realpath  failed for %s: %s", dir_name, strerror(errno));
469                 ret = -1;
470                 goto fail_no_dir;
471         }
472
473         parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
474         dir_entry->inum = parent_inum;
475         list_add_tail(&dir_entry->list, &dir_head->list);
476
477         btrfs_init_path(&path);
478
479         root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
480         root_dir_key.offset = 0;
481         root_dir_key.type = BTRFS_INODE_ITEM_KEY;
482         ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
483         if (ret) {
484                 error("failed to lookup root dir: %d", ret);
485                 goto fail_no_dir;
486         }
487
488         leaf = path.nodes[0];
489         inode_item = btrfs_item_ptr(leaf, path.slots[0],
490                                     struct btrfs_inode_item);
491
492         root_dir_inode_size = calculate_dir_inode_size(dir_name);
493         btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
494         btrfs_mark_buffer_dirty(leaf);
495
496         btrfs_release_path(&path);
497
498         do {
499                 parent_dir_entry = list_entry(dir_head->list.next,
500                                               struct directory_name_entry,
501                                               list);
502                 list_del(&parent_dir_entry->list);
503
504                 parent_inum = parent_dir_entry->inum;
505                 parent_dir_name = parent_dir_entry->dir_name;
506                 if (chdir(parent_dir_entry->path)) {
507                         error("chdir failed for %s: %s",
508                                 parent_dir_name, strerror(errno));
509                         ret = -1;
510                         goto fail_no_files;
511                 }
512
513                 count = scandir(parent_dir_entry->path, &files,
514                                 directory_select, NULL);
515                 if (count == -1) {
516                         error("scandir failed for %s: %s",
517                                 parent_dir_name, strerror(errno));
518                         ret = -1;
519                         goto fail;
520                 }
521
522                 for (i = 0; i < count; i++) {
523                         cur_file = files[i];
524
525                         if (lstat(cur_file->d_name, &st) == -1) {
526                                 error("lstat failed for %s: %s",
527                                         cur_file->d_name, strerror(errno));
528                                 ret = -1;
529                                 goto fail;
530                         }
531
532                         cur_inum = st.st_ino;
533                         ret = add_directory_items(trans, root,
534                                                   cur_inum, parent_inum,
535                                                   cur_file->d_name,
536                                                   &st, &dir_index_cnt);
537                         if (ret) {
538                                 error("unable to add directory items for %s: %d",
539                                         cur_file->d_name, ret);
540                                 goto fail;
541                         }
542
543                         ret = add_inode_items(trans, root, &st,
544                                               cur_file->d_name, cur_inum,
545                                               &cur_inode);
546                         if (ret == -EEXIST) {
547                                 if (st.st_nlink <= 1) {
548                                         error(
549                         "item %s already exists but has wrong st_nlink %lu <= 1",
550                                                 cur_file->d_name,
551                                                 (unsigned long)st.st_nlink);
552                                         goto fail;
553                                 }
554                                 continue;
555                         }
556                         if (ret) {
557                                 error("unable to add inode items for %s: %d",
558                                         cur_file->d_name, ret);
559                                 goto fail;
560                         }
561
562                         ret = add_xattr_item(trans, root,
563                                              cur_inum, cur_file->d_name);
564                         if (ret) {
565                                 error("unable to add xattr items for %s: %d",
566                                         cur_file->d_name, ret);
567                                 if (ret != -ENOTSUP)
568                                         goto fail;
569                         }
570
571                         if (S_ISDIR(st.st_mode)) {
572                                 char tmp[PATH_MAX];
573
574                                 dir_entry = malloc(sizeof(*dir_entry));
575                                 if (!dir_entry) {
576                                         ret = -ENOMEM;
577                                         goto fail;
578                                 }
579                                 dir_entry->dir_name = cur_file->d_name;
580                                 if (path_cat_out(tmp, parent_dir_entry->path,
581                                                         cur_file->d_name)) {
582                                         error("invalid path: %s/%s",
583                                                         parent_dir_entry->path,
584                                                         cur_file->d_name);
585                                         ret = -EINVAL;
586                                         goto fail;
587                                 }
588                                 dir_entry->path = strdup(tmp);
589                                 if (!dir_entry->path) {
590                                         error("not enough memory to store path");
591                                         ret = -ENOMEM;
592                                         goto fail;
593                                 }
594                                 dir_entry->inum = cur_inum;
595                                 list_add_tail(&dir_entry->list,
596                                               &dir_head->list);
597                         } else if (S_ISREG(st.st_mode)) {
598                                 ret = add_file_items(trans, root, &cur_inode,
599                                                      cur_inum, &st,
600                                                      cur_file->d_name);
601                                 if (ret) {
602                                         error("unable to add file items for %s: %d",
603                                                 cur_file->d_name, ret);
604                                         goto fail;
605                                 }
606                         } else if (S_ISLNK(st.st_mode)) {
607                                 ret = add_symbolic_link(trans, root,
608                                                 cur_inum, cur_file->d_name);
609                                 if (ret) {
610                                         error("unable to add symlink for %s: %d",
611                                                 cur_file->d_name, ret);
612                                         goto fail;
613                                 }
614                         }
615                 }
616
617                 free_namelist(files, count);
618                 free(parent_dir_entry->path);
619                 free(parent_dir_entry);
620
621                 index_cnt = 2;
622
623         } while (!list_empty(&dir_head->list));
624
625 out:
626         return !!ret;
627 fail:
628         free_namelist(files, count);
629 fail_no_files:
630         free(parent_dir_entry);
631         goto out;
632 fail_no_dir:
633         free(dir_entry);
634         goto out;
635 }
636
637 int btrfs_mkfs_fill_dir(const char *source_dir, struct btrfs_root *root,
638                         bool verbose)
639 {
640         int ret;
641         struct btrfs_trans_handle *trans;
642         struct stat root_st;
643         struct directory_name_entry dir_head;
644         struct directory_name_entry *dir_entry = NULL;
645
646         ret = lstat(source_dir, &root_st);
647         if (ret) {
648                 error("unable to lstat %s: %s", source_dir, strerror(errno));
649                 ret = -errno;
650                 goto out;
651         }
652
653         INIT_LIST_HEAD(&dir_head.list);
654
655         trans = btrfs_start_transaction(root, 1);
656         BUG_ON(IS_ERR(trans));
657         ret = traverse_directory(trans, root, source_dir, &dir_head);
658         if (ret) {
659                 error("unable to traverse directory %s: %d", source_dir, ret);
660                 goto fail;
661         }
662         ret = btrfs_commit_transaction(trans, root);
663         if (ret) {
664                 error("transaction commit failed: %d", ret);
665                 goto out;
666         }
667
668         if (verbose)
669                 printf("Making image is completed.\n");
670         return 0;
671 fail:
672         /*
673          * Since we don't have btrfs_abort_transaction() yet, uncommitted trans
674          * will trigger a BUG_ON().
675          *
676          * However before mkfs is fully finished, the magic number is invalid,
677          * so even we commit transaction here, the fs still can't be mounted.
678          *
679          * To do a graceful error out, here we commit transaction as a
680          * workaround.
681          * Since we have already hit some problem, the return value doesn't
682          * matter now.
683          */
684         btrfs_commit_transaction(trans, root);
685         while (!list_empty(&dir_head.list)) {
686                 dir_entry = list_entry(dir_head.list.next,
687                                        struct directory_name_entry, list);
688                 list_del(&dir_entry->list);
689                 free(dir_entry->path);
690                 free(dir_entry);
691         }
692 out:
693         return ret;
694 }
695
696 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
697                               int type)
698 {
699         /*
700          * Failed to read the directory, mostly due to EPERM.  Abort ASAP, so
701          * we don't need to populate the fs.
702          */
703         if (type == FTW_DNR || type == FTW_NS)
704                 return -EPERM;
705
706         if (S_ISREG(st->st_mode))
707                 ftw_data_size += round_up(st->st_size, fs_block_size);
708         ftw_meta_nr_inode++;
709
710         return 0;
711 }
712
713 u64 btrfs_mkfs_size_dir(const char *dir_name, u32 sectorsize, u64 min_dev_size,
714                         u64 meta_profile, u64 data_profile)
715 {
716         u64 total_size = 0;
717         int ret;
718
719         u64 meta_size = 0;              /* Based on @ftw_meta_nr_inode */
720         u64 meta_chunk_size = 0;        /* Based on @meta_size */
721         u64 data_chunk_size = 0;        /* Based on @ftw_data_size */
722
723         u64 meta_threshold = SZ_8M;
724         u64 data_threshold = SZ_8M;
725
726         float data_multipler = 1;
727         float meta_multipler = 1;
728
729         fs_block_size = sectorsize;
730         ftw_data_size = 0;
731         ftw_meta_nr_inode = 0;
732         ret = ftw(dir_name, ftw_add_entry_size, 10);
733         if (ret < 0) {
734                 error("ftw subdir walk of %s failed: %s", dir_name,
735                         strerror(errno));
736                 exit(1);
737         }
738
739
740         /*
741          * Maximum metadata useage for every inode, which will be PATH_MAX
742          * for the following items:
743          * 1) DIR_ITEM
744          * 2) DIR_INDEX
745          * 3) INODE_REF
746          *
747          * Plus possible inline extent size, which is sectorsize.
748          *
749          * And finally, allow metadata usage to increase with data size.
750          * Follow the old kernel 8:1 data:meta ratio.
751          * This is especially important for --rootdir, as the file extent size
752          * upper limit is 1M, instead of 128M in kernel.
753          * This can bump meta usage easily.
754          */
755         meta_size = ftw_meta_nr_inode * (PATH_MAX * 3 + sectorsize) +
756                     ftw_data_size / 8;
757
758         /* Minimal chunk size from btrfs_alloc_chunk(). */
759         if (meta_profile & BTRFS_BLOCK_GROUP_DUP) {
760                 meta_threshold = SZ_32M;
761                 meta_multipler = 2;
762         }
763         if (data_profile & BTRFS_BLOCK_GROUP_DUP) {
764                 data_threshold = SZ_64M;
765                 data_multipler = 2;
766         }
767
768         /*
769          * Only when the usage is larger than the minimal chunk size (threshold)
770          * we need to allocate new chunk, or the initial chunk in the image is
771          * large enough.
772          */
773         if (meta_size > meta_threshold)
774                 meta_chunk_size = (round_up(meta_size, meta_threshold) -
775                                    meta_threshold) * meta_multipler;
776         if (ftw_data_size > data_threshold)
777                 data_chunk_size = (round_up(ftw_data_size, data_threshold) -
778                                    data_threshold) * data_multipler;
779
780         total_size = data_chunk_size + meta_chunk_size + min_dev_size;
781         return total_size;
782 }
783
784 /*
785  * Get the end position of the last device extent for given @devid;
786  * @size_ret is exclsuive (means it should be aligned to sectorsize)
787  */
788 static int get_device_extent_end(struct btrfs_fs_info *fs_info,
789                                  u64 devid, u64 *size_ret)
790 {
791         struct btrfs_root *dev_root = fs_info->dev_root;
792         struct btrfs_key key;
793         struct btrfs_path path;
794         struct btrfs_dev_extent *de;
795         int ret;
796
797         key.objectid = devid;
798         key.type = BTRFS_DEV_EXTENT_KEY;
799         key.offset = (u64)-1;
800
801         btrfs_init_path(&path);
802         ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
803         /* Not really possible */
804         BUG_ON(ret == 0);
805
806         ret = btrfs_previous_item(dev_root, &path, devid, BTRFS_DEV_EXTENT_KEY);
807         if (ret < 0)
808                 goto out;
809
810         /* No dev_extent at all, not really possible for rootdir case */
811         if (ret > 0) {
812                 *size_ret = 0;
813                 ret = -EUCLEAN;
814                 goto out;
815         }
816
817         btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
818         de = btrfs_item_ptr(path.nodes[0], path.slots[0],
819                             struct btrfs_dev_extent);
820         *size_ret = key.offset + btrfs_dev_extent_length(path.nodes[0], de);
821 out:
822         btrfs_release_path(&path);
823
824         return ret;
825 }
826
827 /*
828  * Set device size to @new_size.
829  *
830  * Only used for --rootdir option.
831  * We will need to reset the following values:
832  * 1) dev item in chunk tree
833  * 2) super->dev_item
834  * 3) super->total_bytes
835  */
836 static int set_device_size(struct btrfs_fs_info *fs_info,
837                            struct btrfs_device *device, u64 new_size)
838 {
839         struct btrfs_root *chunk_root = fs_info->chunk_root;
840         struct btrfs_trans_handle *trans;
841         struct btrfs_dev_item *di;
842         struct btrfs_path path;
843         struct btrfs_key key;
844         int ret;
845
846         /*
847          * Update in-meory device->total_bytes, so that at trans commit time,
848          * super->dev_item will also get updated
849          */
850         device->total_bytes = new_size;
851         btrfs_init_path(&path);
852
853         /* Update device item in chunk tree */
854         trans = btrfs_start_transaction(chunk_root, 1);
855         if (IS_ERR(trans)) {
856                 ret = PTR_ERR(trans);
857                 error("failed to start transaction: %d (%s)", ret,
858                         strerror(-ret));
859                 return ret;
860         }
861         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
862         key.type = BTRFS_DEV_ITEM_KEY;
863         key.offset = device->devid;
864
865         ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
866         if (ret < 0)
867                 goto err;
868         if (ret > 0)
869                 ret = -ENOENT;
870         di = btrfs_item_ptr(path.nodes[0], path.slots[0],
871                             struct btrfs_dev_item);
872         btrfs_set_device_total_bytes(path.nodes[0], di, new_size);
873         btrfs_mark_buffer_dirty(path.nodes[0]);
874
875         /*
876          * Update super->total_bytes, since it's only used for --rootdir,
877          * there is only one device, just use the @new_size.
878          */
879         btrfs_set_super_total_bytes(fs_info->super_copy, new_size);
880
881         /*
882          * Commit transaction to reflect the updated super->total_bytes and
883          * super->dev_item
884          */
885         ret = btrfs_commit_transaction(trans, chunk_root);
886         if (ret < 0)
887                 error("failed to commit current transaction: %d (%s)",
888                         ret, strerror(-ret));
889         btrfs_release_path(&path);
890         return ret;
891
892 err:
893         btrfs_release_path(&path);
894         /*
895          * Committing the transaction here won't cause problems since the fs
896          * still has an invalid magic number, and something wrong already
897          * happened, we don't care the return value anyway.
898          */
899         btrfs_commit_transaction(trans, chunk_root);
900         return ret;
901 }
902
903 int btrfs_mkfs_shrink_fs(struct btrfs_fs_info *fs_info, u64 *new_size_ret,
904                          bool shrink_file_size)
905 {
906         u64 new_size;
907         struct btrfs_device *device;
908         struct list_head *cur;
909         struct stat64 file_stat;
910         int nr_devs = 0;
911         int ret;
912
913         list_for_each(cur, &fs_info->fs_devices->devices)
914                 nr_devs++;
915
916         if (nr_devs > 1) {
917                 error("cannot shrink fs with more than 1 device");
918                 return -ENOTTY;
919         }
920
921         ret = get_device_extent_end(fs_info, 1, &new_size);
922         if (ret < 0) {
923                 error("failed to get minimal device size: %d (%s)",
924                         ret, strerror(-ret));
925                 return ret;
926         }
927
928         BUG_ON(!IS_ALIGNED(new_size, fs_info->sectorsize));
929
930         device = list_entry(fs_info->fs_devices->devices.next,
931                            struct btrfs_device, dev_list);
932         ret = set_device_size(fs_info, device, new_size);
933         if (ret < 0)
934                 return ret;
935         if (new_size_ret)
936                 *new_size_ret = new_size;
937
938         if (shrink_file_size) {
939                 ret = fstat64(device->fd, &file_stat);
940                 if (ret < 0) {
941                         error("failed to stat devid %llu: %s", device->devid,
942                                 strerror(errno));
943                         return ret;
944                 }
945                 if (!S_ISREG(file_stat.st_mode))
946                         return ret;
947                 ret = ftruncate64(device->fd, new_size);
948                 if (ret < 0) {
949                         error("failed to truncate device file of devid %llu: %s",
950                                 device->devid, strerror(errno));
951                         return ret;
952                 }
953         }
954         return ret;
955 }