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