2 * Copyright (C) 2017 SUSE. All rights reserved.
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.
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.
13 * You should have received a copy of the GNU General Public
14 * License along with this program.
17 #include "kerncompat.h"
18 #include "androidcompat.h"
21 #include <sys/types.h>
22 #include <sys/xattr.h>
23 #include <linux/limits.h>
32 #include "transaction.h"
34 #include "mkfs/rootdir.h"
35 #include "send-utils.h"
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.
43 static u64 global_total_size;
44 static u64 fs_block_size;
46 static u64 index_cnt = 2;
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)
55 struct btrfs_key location;
58 name_len = strlen(name);
60 location.objectid = objectid;
62 location.type = BTRFS_INODE_ITEM_KEY;
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;
79 ret = btrfs_insert_dir_item(trans, root, name, name_len,
80 parent_inum, &location,
84 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
85 objectid, parent_inum, index_cnt);
86 *dir_index_cnt = index_cnt;
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)
97 u64 sectorsize = root->fs_info->sectorsize;
100 * btrfs_inode_item has some reserved fields
101 * and represents on-disk inode entry, so
102 * zero everything to prevent information leak
104 memset(dst, 0, sizeof(*dst));
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);
125 if (S_ISDIR(src->st_mode)) {
126 btrfs_set_stack_inode_size(dst, 0);
127 btrfs_set_stack_inode_nlink(dst, 1);
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);
134 blocks = src->st_size / sectorsize;
135 if (src->st_size % sectorsize)
137 blocks *= sectorsize;
138 btrfs_set_stack_inode_nbytes(dst, blocks);
141 if (S_ISLNK(src->st_mode))
142 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
147 static int directory_select(const struct direct *entry)
149 if (entry->d_name[0] == '.' &&
150 (entry->d_name[1] == 0 ||
151 (entry->d_name[1] == '.' && entry->d_name[2] == 0)))
156 static void free_namelist(struct direct **files, int count)
163 for (i = 0; i < count; ++i)
168 static u64 calculate_dir_inode_size(const char *dirname)
171 struct direct **files, *cur_file;
172 u64 dir_inode_size = 0;
174 count = scandir(dirname, &files, directory_select, NULL);
176 for (i = 0; i < count; i++) {
178 dir_inode_size += strlen(cur_file->d_name);
181 free_namelist(files, count);
184 return dir_inode_size;
187 static int add_inode_items(struct btrfs_trans_handle *trans,
188 struct btrfs_root *root,
189 struct stat *st, const char *name,
191 struct btrfs_inode_item *inode_ret)
194 struct btrfs_inode_item btrfs_inode;
198 fill_inode_item(trans, root, &btrfs_inode, st);
199 objectid = self_objectid;
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);
206 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
208 *inode_ret = btrfs_inode;
212 static int add_xattr_item(struct btrfs_trans_handle *trans,
213 struct btrfs_root *root, u64 objectid,
214 const char *file_name)
218 char xattr_list[XATTR_LIST_MAX];
220 char cur_value[XATTR_SIZE_MAX];
221 char delimiter = '\0';
222 char *next_location = xattr_list;
224 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
226 if (errno == ENOTSUP)
228 error("getting a list of xattr failed for %s: %s", file_name,
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;
240 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
242 if (errno == ENOTSUP)
244 error("gettig a xattr value failed for %s attr %s: %s",
245 file_name, cur_name, strerror(errno));
249 ret = btrfs_insert_xattr_item(trans, root, cur_name,
250 cur_name_len, cur_value,
253 error("inserting a xattr item failed for %s: %s",
254 file_name, strerror(-ret));
257 cur_name = strtok(next_location, &delimiter);
263 static int add_symbolic_link(struct btrfs_trans_handle *trans,
264 struct btrfs_root *root,
265 u64 objectid, const char *path_name)
270 ret = readlink(path_name, buf, sizeof(buf));
272 error("readlink failed for %s: %s", path_name, strerror(errno));
275 if (ret >= sizeof(buf)) {
276 error("symlink too long for %s", path_name);
281 buf[ret] = '\0'; /* readlink does not do it for us */
282 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
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)
296 struct btrfs_key key;
298 u32 sectorsize = root->fs_info->sectorsize;
303 struct extent_buffer *eb = NULL;
306 if (st->st_size == 0)
309 fd = open(path_name, O_RDONLY);
311 error("cannot open %s: %s", path_name, strerror(errno));
315 blocks = st->st_size / sectorsize;
316 if (st->st_size % sectorsize)
319 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
320 char *buffer = malloc(st->st_size);
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,
337 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
338 buffer, st->st_size);
343 /* round up our st_size to the FS blocksize */
344 total_bytes = (u64)blocks * sectorsize;
347 * do our IO in extent buffers so it can work
348 * against any raid type
350 eb = calloc(1, sizeof(*eb) + sectorsize);
359 * keep our extent size at 1MB max, this makes it easier to work inside
360 * the tiny block groups created during mkfs
362 cur_bytes = min(total_bytes, (u64)SZ_1M);
363 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
368 first_block = key.objectid;
371 while (bytes_read < cur_bytes) {
373 memset(eb->data, 0, sectorsize);
375 ret_read = pread64(fd, eb->data, sectorsize, file_pos +
377 if (ret_read == -1) {
378 error("cannot read %s at offset %llu length %llu: %s",
380 (unsigned long long)file_pos + bytes_read,
381 (unsigned long long)sectorsize,
386 eb->start = first_block + bytes_read;
387 eb->len = sectorsize;
390 * we're doing the csum before we record the extent, but
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);
400 ret = write_and_map_eb(root->fs_info, eb);
402 error("failed to write %s", path_name);
406 bytes_read += sectorsize;
410 ret = btrfs_record_file_extent(trans, root, objectid,
411 btrfs_inode, file_pos, first_block, cur_bytes);
417 file_pos += cur_bytes;
418 total_bytes -= cur_bytes;
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)
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;
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;
451 /* Add list for source directory */
452 dir_entry = malloc(sizeof(struct directory_name_entry));
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));
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);
467 btrfs_init_path(&path);
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);
474 error("failed to lookup root dir: %d", ret);
478 leaf = path.nodes[0];
479 inode_item = btrfs_item_ptr(leaf, path.slots[0],
480 struct btrfs_inode_item);
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);
486 btrfs_release_path(&path);
489 parent_dir_entry = list_entry(dir_head->list.next,
490 struct directory_name_entry,
492 list_del(&parent_dir_entry->list);
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));
503 count = scandir(parent_dir_entry->path, &files,
504 directory_select, NULL);
506 error("scandir failed for %s: %s",
507 parent_dir_name, strerror(errno));
512 for (i = 0; i < count; i++) {
515 if (lstat(cur_file->d_name, &st) == -1) {
516 error("lstat failed for %s: %s",
517 cur_file->d_name, strerror(errno));
522 cur_inum = st.st_ino;
523 ret = add_directory_items(trans, root,
524 cur_inum, parent_inum,
526 &st, &dir_index_cnt);
528 error("unable to add directory items for %s: %d",
529 cur_file->d_name, ret);
533 ret = add_inode_items(trans, root, &st,
534 cur_file->d_name, cur_inum,
536 if (ret == -EEXIST) {
537 if (st.st_nlink <= 1) {
539 "item %s already exists but has wrong st_nlink %lu <= 1",
541 (unsigned long)st.st_nlink);
547 error("unable to add inode items for %s: %d",
548 cur_file->d_name, ret);
552 ret = add_xattr_item(trans, root,
553 cur_inum, cur_file->d_name);
555 error("unable to add xattr items for %s: %d",
556 cur_file->d_name, ret);
561 if (S_ISDIR(st.st_mode)) {
564 dir_entry = malloc(sizeof(*dir_entry));
569 dir_entry->dir_name = cur_file->d_name;
570 if (path_cat_out(tmp, parent_dir_entry->path,
572 error("invalid path: %s/%s",
573 parent_dir_entry->path,
578 dir_entry->path = strdup(tmp);
579 if (!dir_entry->path) {
580 error("not enough memory to store path");
584 dir_entry->inum = cur_inum;
585 list_add_tail(&dir_entry->list,
587 } else if (S_ISREG(st.st_mode)) {
588 ret = add_file_items(trans, root, &cur_inode,
592 error("unable to add file items for %s: %d",
593 cur_file->d_name, ret);
596 } else if (S_ISLNK(st.st_mode)) {
597 ret = add_symbolic_link(trans, root,
598 cur_inum, cur_file->d_name);
600 error("unable to add symlink for %s: %d",
601 cur_file->d_name, ret);
607 free_namelist(files, count);
608 free(parent_dir_entry);
612 } while (!list_empty(&dir_head->list));
617 free_namelist(files, count);
619 free(parent_dir_entry);
626 int btrfs_mkfs_fill_dir(const char *source_dir, struct btrfs_root *root,
630 struct btrfs_trans_handle *trans;
632 struct directory_name_entry dir_head;
633 struct directory_name_entry *dir_entry = NULL;
635 ret = lstat(source_dir, &root_st);
637 error("unable to lstat %s: %s", source_dir, strerror(errno));
642 INIT_LIST_HEAD(&dir_head.list);
644 trans = btrfs_start_transaction(root, 1);
645 BUG_ON(IS_ERR(trans));
646 ret = traverse_directory(trans, root, source_dir, &dir_head);
648 error("unable to traverse directory %s: %d", source_dir, ret);
651 ret = btrfs_commit_transaction(trans, root);
653 error("transaction commit failed: %d", ret);
658 printf("Making image is completed.\n");
662 * Since we don't have btrfs_abort_transaction() yet, uncommitted trans
663 * will trigger a BUG_ON().
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.
668 * To do a graceful error out, here we commit transaction as a
670 * Since we have already hit some problem, the return value doesn't
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);
684 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
687 if (type == FTW_F || type == FTW_D)
688 global_total_size += round_up(st->st_size, fs_block_size);
693 u64 btrfs_mkfs_size_dir(const char *dir_name, u64 sectorsize,
694 u64 *num_of_meta_chunks_ret, u64 *size_of_data_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;
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;
712 error("ftw subdir walk of %s failed: %s", dir_name,
717 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
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;
726 num_of_meta_chunks -= num_of_allocated_meta_chunks;
728 total_size = allocated_total_size +
729 (num_of_data_chunks * default_chunk_size) +
730 (num_of_meta_chunks * default_chunk_size);
732 *num_of_meta_chunks_ret = num_of_meta_chunks;
733 *size_of_data_ret = num_of_data_chunks * default_chunk_size;