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