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>
33 #include "transaction.h"
35 #include "mkfs/rootdir.h"
36 #include "send-utils.h"
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.
44 static u64 global_total_size;
45 static u64 fs_block_size;
47 static u64 index_cnt = 2;
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)
56 struct btrfs_key location;
59 name_len = strlen(name);
61 location.objectid = objectid;
63 location.type = BTRFS_INODE_ITEM_KEY;
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;
80 ret = btrfs_insert_dir_item(trans, root, name, name_len,
81 parent_inum, &location,
85 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
86 objectid, parent_inum, index_cnt);
87 *dir_index_cnt = index_cnt;
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)
98 u64 sectorsize = root->fs_info->sectorsize;
101 * btrfs_inode_item has some reserved fields
102 * and represents on-disk inode entry, so
103 * zero everything to prevent information leak
105 memset(dst, 0, sizeof(*dst));
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);
126 if (S_ISDIR(src->st_mode)) {
127 btrfs_set_stack_inode_size(dst, 0);
128 btrfs_set_stack_inode_nlink(dst, 1);
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);
135 blocks = src->st_size / sectorsize;
136 if (src->st_size % sectorsize)
138 blocks *= sectorsize;
139 btrfs_set_stack_inode_nbytes(dst, blocks);
142 if (S_ISLNK(src->st_mode))
143 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
148 static int directory_select(const struct direct *entry)
150 if (entry->d_name[0] == '.' &&
151 (entry->d_name[1] == 0 ||
152 (entry->d_name[1] == '.' && entry->d_name[2] == 0)))
157 static void free_namelist(struct direct **files, int count)
164 for (i = 0; i < count; ++i)
169 static u64 calculate_dir_inode_size(const char *dirname)
172 struct direct **files, *cur_file;
173 u64 dir_inode_size = 0;
175 count = scandir(dirname, &files, directory_select, NULL);
177 for (i = 0; i < count; i++) {
179 dir_inode_size += strlen(cur_file->d_name);
182 free_namelist(files, count);
185 return dir_inode_size;
188 static int add_inode_items(struct btrfs_trans_handle *trans,
189 struct btrfs_root *root,
190 struct stat *st, const char *name,
192 struct btrfs_inode_item *inode_ret)
195 struct btrfs_inode_item btrfs_inode;
199 fill_inode_item(trans, root, &btrfs_inode, st);
200 objectid = self_objectid;
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);
207 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
209 *inode_ret = btrfs_inode;
213 static int add_xattr_item(struct btrfs_trans_handle *trans,
214 struct btrfs_root *root, u64 objectid,
215 const char *file_name)
219 char xattr_list[XATTR_LIST_MAX];
221 char cur_value[XATTR_SIZE_MAX];
222 char delimiter = '\0';
223 char *next_location = xattr_list;
225 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
227 if (errno == ENOTSUP)
229 error("getting a list of xattr failed for %s: %s", file_name,
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;
241 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
243 if (errno == ENOTSUP)
245 error("gettig a xattr value failed for %s attr %s: %s",
246 file_name, cur_name, strerror(errno));
250 ret = btrfs_insert_xattr_item(trans, root, cur_name,
251 cur_name_len, cur_value,
254 error("inserting a xattr item failed for %s: %s",
255 file_name, strerror(-ret));
258 cur_name = strtok(next_location, &delimiter);
264 static int add_symbolic_link(struct btrfs_trans_handle *trans,
265 struct btrfs_root *root,
266 u64 objectid, const char *path_name)
271 ret = readlink(path_name, buf, sizeof(buf));
273 error("readlink failed for %s: %s", path_name, strerror(errno));
276 if (ret >= sizeof(buf)) {
277 error("symlink too long for %s", path_name);
282 buf[ret] = '\0'; /* readlink does not do it for us */
283 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
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)
297 struct btrfs_key key;
299 u32 sectorsize = root->fs_info->sectorsize;
304 struct extent_buffer *eb = NULL;
307 if (st->st_size == 0)
310 fd = open(path_name, O_RDONLY);
312 error("cannot open %s: %s", path_name, strerror(errno));
316 blocks = st->st_size / sectorsize;
317 if (st->st_size % sectorsize)
320 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
321 char *buffer = malloc(st->st_size);
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,
338 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
339 buffer, st->st_size);
344 /* round up our st_size to the FS blocksize */
345 total_bytes = (u64)blocks * sectorsize;
348 * do our IO in extent buffers so it can work
349 * against any raid type
351 eb = calloc(1, sizeof(*eb) + sectorsize);
360 * keep our extent size at 1MB max, this makes it easier to work inside
361 * the tiny block groups created during mkfs
363 cur_bytes = min(total_bytes, (u64)SZ_1M);
364 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
369 first_block = key.objectid;
372 while (bytes_read < cur_bytes) {
374 memset(eb->data, 0, sectorsize);
376 ret_read = pread64(fd, eb->data, sectorsize, file_pos +
378 if (ret_read == -1) {
379 error("cannot read %s at offset %llu length %llu: %s",
381 (unsigned long long)file_pos + bytes_read,
382 (unsigned long long)sectorsize,
387 eb->start = first_block + bytes_read;
388 eb->len = sectorsize;
391 * we're doing the csum before we record the extent, but
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);
401 ret = write_and_map_eb(root->fs_info, eb);
403 error("failed to write %s", path_name);
407 bytes_read += sectorsize;
411 ret = btrfs_record_file_extent(trans, root, objectid,
412 btrfs_inode, file_pos, first_block, cur_bytes);
418 file_pos += cur_bytes;
419 total_bytes -= cur_bytes;
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)
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;
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;
452 /* Add list for source directory */
453 dir_entry = malloc(sizeof(struct directory_name_entry));
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));
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);
468 btrfs_init_path(&path);
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);
475 error("failed to lookup root dir: %d", ret);
479 leaf = path.nodes[0];
480 inode_item = btrfs_item_ptr(leaf, path.slots[0],
481 struct btrfs_inode_item);
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);
487 btrfs_release_path(&path);
490 parent_dir_entry = list_entry(dir_head->list.next,
491 struct directory_name_entry,
493 list_del(&parent_dir_entry->list);
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));
504 count = scandir(parent_dir_entry->path, &files,
505 directory_select, NULL);
507 error("scandir failed for %s: %s",
508 parent_dir_name, strerror(errno));
513 for (i = 0; i < count; i++) {
516 if (lstat(cur_file->d_name, &st) == -1) {
517 error("lstat failed for %s: %s",
518 cur_file->d_name, strerror(errno));
523 cur_inum = st.st_ino;
524 ret = add_directory_items(trans, root,
525 cur_inum, parent_inum,
527 &st, &dir_index_cnt);
529 error("unable to add directory items for %s: %d",
530 cur_file->d_name, ret);
534 ret = add_inode_items(trans, root, &st,
535 cur_file->d_name, cur_inum,
537 if (ret == -EEXIST) {
538 if (st.st_nlink <= 1) {
540 "item %s already exists but has wrong st_nlink %lu <= 1",
542 (unsigned long)st.st_nlink);
548 error("unable to add inode items for %s: %d",
549 cur_file->d_name, ret);
553 ret = add_xattr_item(trans, root,
554 cur_inum, cur_file->d_name);
556 error("unable to add xattr items for %s: %d",
557 cur_file->d_name, ret);
562 if (S_ISDIR(st.st_mode)) {
565 dir_entry = malloc(sizeof(*dir_entry));
570 dir_entry->dir_name = cur_file->d_name;
571 if (path_cat_out(tmp, parent_dir_entry->path,
573 error("invalid path: %s/%s",
574 parent_dir_entry->path,
579 dir_entry->path = strdup(tmp);
580 if (!dir_entry->path) {
581 error("not enough memory to store path");
585 dir_entry->inum = cur_inum;
586 list_add_tail(&dir_entry->list,
588 } else if (S_ISREG(st.st_mode)) {
589 ret = add_file_items(trans, root, &cur_inode,
593 error("unable to add file items for %s: %d",
594 cur_file->d_name, ret);
597 } else if (S_ISLNK(st.st_mode)) {
598 ret = add_symbolic_link(trans, root,
599 cur_inum, cur_file->d_name);
601 error("unable to add symlink for %s: %d",
602 cur_file->d_name, ret);
608 free_namelist(files, count);
609 free(parent_dir_entry);
613 } while (!list_empty(&dir_head->list));
618 free_namelist(files, count);
620 free(parent_dir_entry);
627 int btrfs_mkfs_fill_dir(const char *source_dir, struct btrfs_root *root,
631 struct btrfs_trans_handle *trans;
633 struct directory_name_entry dir_head;
634 struct directory_name_entry *dir_entry = NULL;
636 ret = lstat(source_dir, &root_st);
638 error("unable to lstat %s: %s", source_dir, strerror(errno));
643 INIT_LIST_HEAD(&dir_head.list);
645 trans = btrfs_start_transaction(root, 1);
646 BUG_ON(IS_ERR(trans));
647 ret = traverse_directory(trans, root, source_dir, &dir_head);
649 error("unable to traverse directory %s: %d", source_dir, ret);
652 ret = btrfs_commit_transaction(trans, root);
654 error("transaction commit failed: %d", ret);
659 printf("Making image is completed.\n");
663 * Since we don't have btrfs_abort_transaction() yet, uncommitted trans
664 * will trigger a BUG_ON().
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.
669 * To do a graceful error out, here we commit transaction as a
671 * Since we have already hit some problem, the return value doesn't
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);
685 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
688 if (type == FTW_F || type == FTW_D)
689 global_total_size += round_up(st->st_size, fs_block_size);
694 u64 btrfs_mkfs_size_dir(const char *dir_name, u64 sectorsize,
695 u64 *num_of_meta_chunks_ret, u64 *size_of_data_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;
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;
713 error("ftw subdir walk of %s failed: %s", dir_name,
718 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
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;
727 num_of_meta_chunks -= num_of_allocated_meta_chunks;
729 total_size = allocated_total_size +
730 (num_of_data_chunks * default_chunk_size) +
731 (num_of_meta_chunks * default_chunk_size);
733 *num_of_meta_chunks_ret = num_of_meta_chunks;
734 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
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)
742 static int get_device_extent_end(struct btrfs_fs_info *fs_info,
743 u64 devid, u64 *size_ret)
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;
751 key.objectid = devid;
752 key.type = BTRFS_DEV_EXTENT_KEY;
753 key.offset = (u64)-1;
755 btrfs_init_path(&path);
756 ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
757 /* Not really possible */
760 ret = btrfs_previous_item(dev_root, &path, devid, BTRFS_DEV_EXTENT_KEY);
764 /* No dev_extent at all, not really possible for rootdir case */
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);
776 btrfs_release_path(&path);