0eb3f8f26139682e271deaef29d3517d58729022
[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 "send-utils.h"
37
38 /*
39  * This ignores symlinks with unreadable targets and subdirs that can't
40  * be read.  It's a best-effort to give a rough estimate of the size of
41  * a subdir.  It doesn't guarantee that prepopulating btrfs from this
42  * tree won't still run out of space.
43  */
44 static u64 global_total_size;
45 static u64 fs_block_size;
46
47 static u64 index_cnt = 2;
48
49 static int add_directory_items(struct btrfs_trans_handle *trans,
50                                struct btrfs_root *root, u64 objectid,
51                                ino_t parent_inum, const char *name,
52                                struct stat *st, int *dir_index_cnt)
53 {
54         int ret;
55         int name_len;
56         struct btrfs_key location;
57         u8 filetype = 0;
58
59         name_len = strlen(name);
60
61         location.objectid = objectid;
62         location.offset = 0;
63         location.type = BTRFS_INODE_ITEM_KEY;
64
65         if (S_ISDIR(st->st_mode))
66                 filetype = BTRFS_FT_DIR;
67         if (S_ISREG(st->st_mode))
68                 filetype = BTRFS_FT_REG_FILE;
69         if (S_ISLNK(st->st_mode))
70                 filetype = BTRFS_FT_SYMLINK;
71         if (S_ISSOCK(st->st_mode))
72                 filetype = BTRFS_FT_SOCK;
73         if (S_ISCHR(st->st_mode))
74                 filetype = BTRFS_FT_CHRDEV;
75         if (S_ISBLK(st->st_mode))
76                 filetype = BTRFS_FT_BLKDEV;
77         if (S_ISFIFO(st->st_mode))
78                 filetype = BTRFS_FT_FIFO;
79
80         ret = btrfs_insert_dir_item(trans, root, name, name_len,
81                                     parent_inum, &location,
82                                     filetype, index_cnt);
83         if (ret)
84                 return ret;
85         ret = btrfs_insert_inode_ref(trans, root, name, name_len,
86                                      objectid, parent_inum, index_cnt);
87         *dir_index_cnt = index_cnt;
88         index_cnt++;
89
90         return ret;
91 }
92
93 static int fill_inode_item(struct btrfs_trans_handle *trans,
94                            struct btrfs_root *root,
95                            struct btrfs_inode_item *dst, struct stat *src)
96 {
97         u64 blocks = 0;
98         u64 sectorsize = root->fs_info->sectorsize;
99
100         /*
101          * btrfs_inode_item has some reserved fields
102          * and represents on-disk inode entry, so
103          * zero everything to prevent information leak
104          */
105         memset(dst, 0, sizeof(*dst));
106
107         btrfs_set_stack_inode_generation(dst, trans->transid);
108         btrfs_set_stack_inode_size(dst, src->st_size);
109         btrfs_set_stack_inode_nbytes(dst, 0);
110         btrfs_set_stack_inode_block_group(dst, 0);
111         btrfs_set_stack_inode_nlink(dst, src->st_nlink);
112         btrfs_set_stack_inode_uid(dst, src->st_uid);
113         btrfs_set_stack_inode_gid(dst, src->st_gid);
114         btrfs_set_stack_inode_mode(dst, src->st_mode);
115         btrfs_set_stack_inode_rdev(dst, 0);
116         btrfs_set_stack_inode_flags(dst, 0);
117         btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
118         btrfs_set_stack_timespec_nsec(&dst->atime, 0);
119         btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
120         btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
121         btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
122         btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
123         btrfs_set_stack_timespec_sec(&dst->otime, 0);
124         btrfs_set_stack_timespec_nsec(&dst->otime, 0);
125
126         if (S_ISDIR(src->st_mode)) {
127                 btrfs_set_stack_inode_size(dst, 0);
128                 btrfs_set_stack_inode_nlink(dst, 1);
129         }
130         if (S_ISREG(src->st_mode)) {
131                 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
132                 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
133                         btrfs_set_stack_inode_nbytes(dst, src->st_size);
134                 else {
135                         blocks = src->st_size / sectorsize;
136                         if (src->st_size % sectorsize)
137                                 blocks += 1;
138                         blocks *= sectorsize;
139                         btrfs_set_stack_inode_nbytes(dst, blocks);
140                 }
141         }
142         if (S_ISLNK(src->st_mode))
143                 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
144
145         return 0;
146 }
147
148 static int directory_select(const struct direct *entry)
149 {
150         if (entry->d_name[0] == '.' &&
151                 (entry->d_name[1] == 0 ||
152                  (entry->d_name[1] == '.' && entry->d_name[2] == 0)))
153                 return 0;
154         return 1;
155 }
156
157 static void free_namelist(struct direct **files, int count)
158 {
159         int i;
160
161         if (count < 0)
162                 return;
163
164         for (i = 0; i < count; ++i)
165                 free(files[i]);
166         free(files);
167 }
168
169 static u64 calculate_dir_inode_size(const char *dirname)
170 {
171         int count, i;
172         struct direct **files, *cur_file;
173         u64 dir_inode_size = 0;
174
175         count = scandir(dirname, &files, directory_select, NULL);
176
177         for (i = 0; i < count; i++) {
178                 cur_file = files[i];
179                 dir_inode_size += strlen(cur_file->d_name);
180         }
181
182         free_namelist(files, count);
183
184         dir_inode_size *= 2;
185         return dir_inode_size;
186 }
187
188 static int add_inode_items(struct btrfs_trans_handle *trans,
189                            struct btrfs_root *root,
190                            struct stat *st, const char *name,
191                            u64 self_objectid,
192                            struct btrfs_inode_item *inode_ret)
193 {
194         int ret;
195         struct btrfs_inode_item btrfs_inode;
196         u64 objectid;
197         u64 inode_size = 0;
198
199         fill_inode_item(trans, root, &btrfs_inode, st);
200         objectid = self_objectid;
201
202         if (S_ISDIR(st->st_mode)) {
203                 inode_size = calculate_dir_inode_size(name);
204                 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
205         }
206
207         ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
208
209         *inode_ret = btrfs_inode;
210         return ret;
211 }
212
213 static int add_xattr_item(struct btrfs_trans_handle *trans,
214                           struct btrfs_root *root, u64 objectid,
215                           const char *file_name)
216 {
217         int ret;
218         int cur_name_len;
219         char xattr_list[XATTR_LIST_MAX];
220         char *cur_name;
221         char cur_value[XATTR_SIZE_MAX];
222         char delimiter = '\0';
223         char *next_location = xattr_list;
224
225         ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
226         if (ret < 0) {
227                 if (errno == ENOTSUP)
228                         return 0;
229                 error("getting a list of xattr failed for %s: %s", file_name,
230                                 strerror(errno));
231                 return ret;
232         }
233         if (ret == 0)
234                 return ret;
235
236         cur_name = strtok(xattr_list, &delimiter);
237         while (cur_name != NULL) {
238                 cur_name_len = strlen(cur_name);
239                 next_location += cur_name_len + 1;
240
241                 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
242                 if (ret < 0) {
243                         if (errno == ENOTSUP)
244                                 return 0;
245                         error("gettig a xattr value failed for %s attr %s: %s",
246                                 file_name, cur_name, strerror(errno));
247                         return ret;
248                 }
249
250                 ret = btrfs_insert_xattr_item(trans, root, cur_name,
251                                               cur_name_len, cur_value,
252                                               ret, objectid);
253                 if (ret) {
254                         error("inserting a xattr item failed for %s: %s",
255                                         file_name, strerror(-ret));
256                 }
257
258                 cur_name = strtok(next_location, &delimiter);
259         }
260
261         return ret;
262 }
263
264 static int add_symbolic_link(struct btrfs_trans_handle *trans,
265                              struct btrfs_root *root,
266                              u64 objectid, const char *path_name)
267 {
268         int ret;
269         char buf[PATH_MAX];
270
271         ret = readlink(path_name, buf, sizeof(buf));
272         if (ret <= 0) {
273                 error("readlink failed for %s: %s", path_name, strerror(errno));
274                 goto fail;
275         }
276         if (ret >= sizeof(buf)) {
277                 error("symlink too long for %s", path_name);
278                 ret = -1;
279                 goto fail;
280         }
281
282         buf[ret] = '\0'; /* readlink does not do it for us */
283         ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
284                                          buf, ret + 1);
285 fail:
286         return ret;
287 }
288
289 static int add_file_items(struct btrfs_trans_handle *trans,
290                           struct btrfs_root *root,
291                           struct btrfs_inode_item *btrfs_inode, u64 objectid,
292                           struct stat *st, const char *path_name)
293 {
294         int ret = -1;
295         ssize_t ret_read;
296         u64 bytes_read = 0;
297         struct btrfs_key key;
298         int blocks;
299         u32 sectorsize = root->fs_info->sectorsize;
300         u64 first_block = 0;
301         u64 file_pos = 0;
302         u64 cur_bytes;
303         u64 total_bytes;
304         struct extent_buffer *eb = NULL;
305         int fd;
306
307         if (st->st_size == 0)
308                 return 0;
309
310         fd = open(path_name, O_RDONLY);
311         if (fd == -1) {
312                 error("cannot open %s: %s", path_name, strerror(errno));
313                 return ret;
314         }
315
316         blocks = st->st_size / sectorsize;
317         if (st->st_size % sectorsize)
318                 blocks += 1;
319
320         if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
321                 char *buffer = malloc(st->st_size);
322
323                 if (!buffer) {
324                         ret = -ENOMEM;
325                         goto end;
326                 }
327
328                 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
329                 if (ret_read == -1) {
330                         error("cannot read %s at offset %llu length %llu: %s",
331                                 path_name, (unsigned long long)bytes_read,
332                                 (unsigned long long)st->st_size,
333                                 strerror(errno));
334                         free(buffer);
335                         goto end;
336                 }
337
338                 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
339                                                  buffer, st->st_size);
340                 free(buffer);
341                 goto end;
342         }
343
344         /* round up our st_size to the FS blocksize */
345         total_bytes = (u64)blocks * sectorsize;
346
347         /*
348          * do our IO in extent buffers so it can work
349          * against any raid type
350          */
351         eb = calloc(1, sizeof(*eb) + sectorsize);
352         if (!eb) {
353                 ret = -ENOMEM;
354                 goto end;
355         }
356
357 again:
358
359         /*
360          * keep our extent size at 1MB max, this makes it easier to work inside
361          * the tiny block groups created during mkfs
362          */
363         cur_bytes = min(total_bytes, (u64)SZ_1M);
364         ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
365                                    &key, 1);
366         if (ret)
367                 goto end;
368
369         first_block = key.objectid;
370         bytes_read = 0;
371
372         while (bytes_read < cur_bytes) {
373
374                 memset(eb->data, 0, sectorsize);
375
376                 ret_read = pread64(fd, eb->data, sectorsize, file_pos +
377                                    bytes_read);
378                 if (ret_read == -1) {
379                         error("cannot read %s at offset %llu length %llu: %s",
380                                 path_name,
381                                 (unsigned long long)file_pos + bytes_read,
382                                 (unsigned long long)sectorsize,
383                                 strerror(errno));
384                         goto end;
385                 }
386
387                 eb->start = first_block + bytes_read;
388                 eb->len = sectorsize;
389
390                 /*
391                  * we're doing the csum before we record the extent, but
392                  * that's ok
393                  */
394                 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
395                                 first_block + bytes_read + sectorsize,
396                                 first_block + bytes_read,
397                                 eb->data, sectorsize);
398                 if (ret)
399                         goto end;
400
401                 ret = write_and_map_eb(root->fs_info, eb);
402                 if (ret) {
403                         error("failed to write %s", path_name);
404                         goto end;
405                 }
406
407                 bytes_read += sectorsize;
408         }
409
410         if (bytes_read) {
411                 ret = btrfs_record_file_extent(trans, root, objectid,
412                                 btrfs_inode, file_pos, first_block, cur_bytes);
413                 if (ret)
414                         goto end;
415
416         }
417
418         file_pos += cur_bytes;
419         total_bytes -= cur_bytes;
420
421         if (total_bytes)
422                 goto again;
423
424 end:
425         free(eb);
426         close(fd);
427         return ret;
428 }
429
430 static int traverse_directory(struct btrfs_trans_handle *trans,
431                               struct btrfs_root *root, const char *dir_name,
432                               struct directory_name_entry *dir_head)
433 {
434         int ret = 0;
435
436         struct btrfs_inode_item cur_inode;
437         struct btrfs_inode_item *inode_item;
438         int count, i, dir_index_cnt;
439         struct direct **files;
440         struct stat st;
441         struct directory_name_entry *dir_entry, *parent_dir_entry;
442         struct direct *cur_file;
443         ino_t parent_inum, cur_inum;
444         ino_t highest_inum = 0;
445         const char *parent_dir_name;
446         char real_path[PATH_MAX];
447         struct btrfs_path path;
448         struct extent_buffer *leaf;
449         struct btrfs_key root_dir_key;
450         u64 root_dir_inode_size = 0;
451
452         /* Add list for source directory */
453         dir_entry = malloc(sizeof(struct directory_name_entry));
454         if (!dir_entry)
455                 return -ENOMEM;
456         dir_entry->dir_name = dir_name;
457         dir_entry->path = realpath(dir_name, real_path);
458         if (!dir_entry->path) {
459                 error("realpath  failed for %s: %s", dir_name, strerror(errno));
460                 ret = -1;
461                 goto fail_no_dir;
462         }
463
464         parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
465         dir_entry->inum = parent_inum;
466         list_add_tail(&dir_entry->list, &dir_head->list);
467
468         btrfs_init_path(&path);
469
470         root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
471         root_dir_key.offset = 0;
472         root_dir_key.type = BTRFS_INODE_ITEM_KEY;
473         ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
474         if (ret) {
475                 error("failed to lookup root dir: %d", ret);
476                 goto fail_no_dir;
477         }
478
479         leaf = path.nodes[0];
480         inode_item = btrfs_item_ptr(leaf, path.slots[0],
481                                     struct btrfs_inode_item);
482
483         root_dir_inode_size = calculate_dir_inode_size(dir_name);
484         btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
485         btrfs_mark_buffer_dirty(leaf);
486
487         btrfs_release_path(&path);
488
489         do {
490                 parent_dir_entry = list_entry(dir_head->list.next,
491                                               struct directory_name_entry,
492                                               list);
493                 list_del(&parent_dir_entry->list);
494
495                 parent_inum = parent_dir_entry->inum;
496                 parent_dir_name = parent_dir_entry->dir_name;
497                 if (chdir(parent_dir_entry->path)) {
498                         error("chdir failed for %s: %s",
499                                 parent_dir_name, strerror(errno));
500                         ret = -1;
501                         goto fail_no_files;
502                 }
503
504                 count = scandir(parent_dir_entry->path, &files,
505                                 directory_select, NULL);
506                 if (count == -1) {
507                         error("scandir failed for %s: %s",
508                                 parent_dir_name, strerror(errno));
509                         ret = -1;
510                         goto fail;
511                 }
512
513                 for (i = 0; i < count; i++) {
514                         cur_file = files[i];
515
516                         if (lstat(cur_file->d_name, &st) == -1) {
517                                 error("lstat failed for %s: %s",
518                                         cur_file->d_name, strerror(errno));
519                                 ret = -1;
520                                 goto fail;
521                         }
522
523                         cur_inum = st.st_ino;
524                         ret = add_directory_items(trans, root,
525                                                   cur_inum, parent_inum,
526                                                   cur_file->d_name,
527                                                   &st, &dir_index_cnt);
528                         if (ret) {
529                                 error("unable to add directory items for %s: %d",
530                                         cur_file->d_name, ret);
531                                 goto fail;
532                         }
533
534                         ret = add_inode_items(trans, root, &st,
535                                               cur_file->d_name, cur_inum,
536                                               &cur_inode);
537                         if (ret == -EEXIST) {
538                                 if (st.st_nlink <= 1) {
539                                         error(
540                         "item %s already exists but has wrong st_nlink %lu <= 1",
541                                                 cur_file->d_name,
542                                                 (unsigned long)st.st_nlink);
543                                         goto fail;
544                                 }
545                                 continue;
546                         }
547                         if (ret) {
548                                 error("unable to add inode items for %s: %d",
549                                         cur_file->d_name, ret);
550                                 goto fail;
551                         }
552
553                         ret = add_xattr_item(trans, root,
554                                              cur_inum, cur_file->d_name);
555                         if (ret) {
556                                 error("unable to add xattr items for %s: %d",
557                                         cur_file->d_name, ret);
558                                 if (ret != -ENOTSUP)
559                                         goto fail;
560                         }
561
562                         if (S_ISDIR(st.st_mode)) {
563                                 char tmp[PATH_MAX];
564
565                                 dir_entry = malloc(sizeof(*dir_entry));
566                                 if (!dir_entry) {
567                                         ret = -ENOMEM;
568                                         goto fail;
569                                 }
570                                 dir_entry->dir_name = cur_file->d_name;
571                                 if (path_cat_out(tmp, parent_dir_entry->path,
572                                                         cur_file->d_name)) {
573                                         error("invalid path: %s/%s",
574                                                         parent_dir_entry->path,
575                                                         cur_file->d_name);
576                                         ret = -EINVAL;
577                                         goto fail;
578                                 }
579                                 dir_entry->path = strdup(tmp);
580                                 if (!dir_entry->path) {
581                                         error("not enough memory to store path");
582                                         ret = -ENOMEM;
583                                         goto fail;
584                                 }
585                                 dir_entry->inum = cur_inum;
586                                 list_add_tail(&dir_entry->list,
587                                               &dir_head->list);
588                         } else if (S_ISREG(st.st_mode)) {
589                                 ret = add_file_items(trans, root, &cur_inode,
590                                                      cur_inum, &st,
591                                                      cur_file->d_name);
592                                 if (ret) {
593                                         error("unable to add file items for %s: %d",
594                                                 cur_file->d_name, ret);
595                                         goto fail;
596                                 }
597                         } else if (S_ISLNK(st.st_mode)) {
598                                 ret = add_symbolic_link(trans, root,
599                                                 cur_inum, cur_file->d_name);
600                                 if (ret) {
601                                         error("unable to add symlink for %s: %d",
602                                                 cur_file->d_name, ret);
603                                         goto fail;
604                                 }
605                         }
606                 }
607
608                 free_namelist(files, count);
609                 free(parent_dir_entry);
610
611                 index_cnt = 2;
612
613         } while (!list_empty(&dir_head->list));
614
615 out:
616         return !!ret;
617 fail:
618         free_namelist(files, count);
619 fail_no_files:
620         free(parent_dir_entry);
621         goto out;
622 fail_no_dir:
623         free(dir_entry);
624         goto out;
625 }
626
627 int btrfs_mkfs_fill_dir(const char *source_dir, struct btrfs_root *root,
628                         bool verbose)
629 {
630         int ret;
631         struct btrfs_trans_handle *trans;
632         struct stat root_st;
633         struct directory_name_entry dir_head;
634         struct directory_name_entry *dir_entry = NULL;
635
636         ret = lstat(source_dir, &root_st);
637         if (ret) {
638                 error("unable to lstat %s: %s", source_dir, strerror(errno));
639                 ret = -errno;
640                 goto out;
641         }
642
643         INIT_LIST_HEAD(&dir_head.list);
644
645         trans = btrfs_start_transaction(root, 1);
646         BUG_ON(IS_ERR(trans));
647         ret = traverse_directory(trans, root, source_dir, &dir_head);
648         if (ret) {
649                 error("unable to traverse directory %s: %d", source_dir, ret);
650                 goto fail;
651         }
652         ret = btrfs_commit_transaction(trans, root);
653         if (ret) {
654                 error("transaction commit failed: %d", ret);
655                 goto out;
656         }
657
658         if (verbose)
659                 printf("Making image is completed.\n");
660         return 0;
661 fail:
662         /*
663          * Since we don't have btrfs_abort_transaction() yet, uncommitted trans
664          * will trigger a BUG_ON().
665          *
666          * However before mkfs is fully finished, the magic number is invalid,
667          * so even we commit transaction here, the fs still can't be mounted.
668          *
669          * To do a graceful error out, here we commit transaction as a
670          * workaround.
671          * Since we have already hit some problem, the return value doesn't
672          * matter now.
673          */
674         btrfs_commit_transaction(trans, root);
675         while (!list_empty(&dir_head.list)) {
676                 dir_entry = list_entry(dir_head.list.next,
677                                        struct directory_name_entry, list);
678                 list_del(&dir_entry->list);
679                 free(dir_entry);
680         }
681 out:
682         return ret;
683 }
684
685 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
686                               int type)
687 {
688         if (type == FTW_F || type == FTW_D)
689                 global_total_size += round_up(st->st_size, fs_block_size);
690
691         return 0;
692 }
693
694 u64 btrfs_mkfs_size_dir(const char *dir_name, u64 sectorsize,
695                         u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
696 {
697         u64 dir_size = 0;
698         u64 total_size = 0;
699         int ret;
700         u64 default_chunk_size = SZ_8M;
701         u64 allocated_meta_size = SZ_8M;
702         u64 allocated_total_size = 20 * SZ_1M;  /* 20MB */
703         u64 num_of_meta_chunks = 0;
704         u64 num_of_data_chunks = 0;
705         u64 num_of_allocated_meta_chunks =
706                         allocated_meta_size / default_chunk_size;
707
708         global_total_size = 0;
709         fs_block_size = sectorsize;
710         ret = ftw(dir_name, ftw_add_entry_size, 10);
711         dir_size = global_total_size;
712         if (ret < 0) {
713                 error("ftw subdir walk of %s failed: %s", dir_name,
714                         strerror(errno));
715                 exit(1);
716         }
717
718         num_of_data_chunks = (dir_size + default_chunk_size - 1) /
719                 default_chunk_size;
720
721         num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
722         if (((dir_size / 2) % default_chunk_size) != 0)
723                 num_of_meta_chunks++;
724         if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
725                 num_of_meta_chunks = 0;
726         else
727                 num_of_meta_chunks -= num_of_allocated_meta_chunks;
728
729         total_size = allocated_total_size +
730                      (num_of_data_chunks * default_chunk_size) +
731                      (num_of_meta_chunks * default_chunk_size);
732
733         *num_of_meta_chunks_ret = num_of_meta_chunks;
734         *size_of_data_ret = num_of_data_chunks * default_chunk_size;
735         return total_size;
736 }
737
738 /*
739  * Get the end position of the last device extent for given @devid;
740  * @size_ret is exclsuive (means it should be aligned to sectorsize)
741  */
742 static int get_device_extent_end(struct btrfs_fs_info *fs_info,
743                                  u64 devid, u64 *size_ret)
744 {
745         struct btrfs_root *dev_root = fs_info->dev_root;
746         struct btrfs_key key;
747         struct btrfs_path path;
748         struct btrfs_dev_extent *de;
749         int ret;
750
751         key.objectid = devid;
752         key.type = BTRFS_DEV_EXTENT_KEY;
753         key.offset = (u64)-1;
754
755         btrfs_init_path(&path);
756         ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
757         /* Not really possible */
758         BUG_ON(ret == 0);
759
760         ret = btrfs_previous_item(dev_root, &path, devid, BTRFS_DEV_EXTENT_KEY);
761         if (ret < 0)
762                 goto out;
763
764         /* No dev_extent at all, not really possible for rootdir case */
765         if (ret > 0) {
766                 *size_ret = 0;
767                 ret = -EUCLEAN;
768                 goto out;
769         }
770
771         btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
772         de = btrfs_item_ptr(path.nodes[0], path.slots[0],
773                             struct btrfs_dev_extent);
774         *size_ret = key.offset + btrfs_dev_extent_length(path.nodes[0], de);
775 out:
776         btrfs_release_path(&path);
777
778         return ret;
779 }