|
| 1 | +/* |
| 2 | + * This program is free software; you can redistribute it and/or |
| 3 | + * modify it under the terms of the GNU General Public |
| 4 | + * License v2 as published by the Free Software Foundation. |
| 5 | + * |
| 6 | + * This program is distributed in the hope that it will be useful, |
| 7 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 9 | + * General Public License for more details. |
| 10 | + * |
| 11 | + * You should have received a copy of the GNU General Public |
| 12 | + * License along with this program; if not, write to the |
| 13 | + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 14 | + * Boston, MA 021110-1307, USA. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <time.h> |
| 18 | +#include "common/root-tree-utils.h" |
| 19 | +#include "common/messages.h" |
| 20 | +#include "kernel-shared/disk-io.h" |
| 21 | + |
| 22 | +int btrfs_make_root_dir(struct btrfs_trans_handle *trans, |
| 23 | + struct btrfs_root *root, u64 objectid) |
| 24 | +{ |
| 25 | + int ret; |
| 26 | + struct btrfs_inode_item inode_item; |
| 27 | + time_t now = time(NULL); |
| 28 | + |
| 29 | + memset(&inode_item, 0, sizeof(inode_item)); |
| 30 | + btrfs_set_stack_inode_generation(&inode_item, trans->transid); |
| 31 | + btrfs_set_stack_inode_size(&inode_item, 0); |
| 32 | + btrfs_set_stack_inode_nlink(&inode_item, 1); |
| 33 | + btrfs_set_stack_inode_nbytes(&inode_item, root->fs_info->nodesize); |
| 34 | + btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755); |
| 35 | + btrfs_set_stack_timespec_sec(&inode_item.atime, now); |
| 36 | + btrfs_set_stack_timespec_nsec(&inode_item.atime, 0); |
| 37 | + btrfs_set_stack_timespec_sec(&inode_item.ctime, now); |
| 38 | + btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0); |
| 39 | + btrfs_set_stack_timespec_sec(&inode_item.mtime, now); |
| 40 | + btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0); |
| 41 | + btrfs_set_stack_timespec_sec(&inode_item.otime, now); |
| 42 | + btrfs_set_stack_timespec_nsec(&inode_item.otime, 0); |
| 43 | + |
| 44 | + if (root->fs_info->tree_root == root) |
| 45 | + btrfs_set_super_root_dir(root->fs_info->super_copy, objectid); |
| 46 | + |
| 47 | + ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
| 48 | + if (ret) |
| 49 | + goto error; |
| 50 | + |
| 51 | + ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0); |
| 52 | + if (ret) |
| 53 | + goto error; |
| 54 | + |
| 55 | + btrfs_set_root_dirid(&root->root_item, objectid); |
| 56 | + ret = 0; |
| 57 | +error: |
| 58 | + return ret; |
| 59 | +} |
| 60 | + |
| 61 | +/* |
| 62 | + * Create a subvolume and initialize its content with the top inode. |
| 63 | + * |
| 64 | + * The created tree root would have its root_ref as 1. |
| 65 | + * Thus for subvolumes caller needs to properly add ROOT_BACKREF items. |
| 66 | + */ |
| 67 | +int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid) |
| 68 | +{ |
| 69 | + struct btrfs_fs_info *fs_info = trans->fs_info; |
| 70 | + struct btrfs_root *root; |
| 71 | + struct btrfs_key key = { |
| 72 | + .objectid = objectid, |
| 73 | + .type = BTRFS_ROOT_ITEM_KEY, |
| 74 | + }; |
| 75 | + int ret; |
| 76 | + |
| 77 | + /* FSTREE is different and can not be created by this function. */ |
| 78 | + UASSERT(objectid != BTRFS_FS_TREE_OBJECTID); |
| 79 | + UASSERT(is_fstree(objectid) || objectid == BTRFS_DATA_RELOC_TREE_OBJECTID); |
| 80 | + |
| 81 | + root = btrfs_create_tree(trans, &key); |
| 82 | + if (IS_ERR(root)) { |
| 83 | + ret = PTR_ERR(root); |
| 84 | + goto error; |
| 85 | + } |
| 86 | + /* |
| 87 | + * Free it for now, and re-read it from disk to setup cache and |
| 88 | + * tracking. |
| 89 | + */ |
| 90 | + btrfs_free_fs_root(root); |
| 91 | + root = btrfs_read_fs_root(fs_info, &key); |
| 92 | + if (IS_ERR(root)) { |
| 93 | + ret = PTR_ERR(root); |
| 94 | + goto error; |
| 95 | + } |
| 96 | + ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID); |
| 97 | + if (ret < 0) |
| 98 | + goto error; |
| 99 | + ret = btrfs_update_root(trans, fs_info->tree_root, &root->root_key, |
| 100 | + &root->root_item); |
| 101 | + if (ret < 0) |
| 102 | + goto error; |
| 103 | + return 0; |
| 104 | +error: |
| 105 | + btrfs_abort_transaction(trans, ret); |
| 106 | + return ret; |
| 107 | +} |
0 commit comments