1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/fs.h> 4 #include <linux/types.h> 5 #include "ctree.h" 6 #include "disk-io.h" 7 #include "btrfs_inode.h" 8 #include "print-tree.h" 9 #include "export.h" 10 11 #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \ 12 parent_objectid) / 4) 13 #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \ 14 parent_root_objectid) / 4) 15 #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4) 16 17 static int btrfs_encode_fh(struct inode *inode, u32 *fh, int *max_len, 18 struct inode *parent) 19 { 20 struct btrfs_fid *fid = (struct btrfs_fid *)fh; 21 int len = *max_len; 22 int type; 23 24 if (parent && (len < BTRFS_FID_SIZE_CONNECTABLE)) { 25 *max_len = BTRFS_FID_SIZE_CONNECTABLE; 26 return FILEID_INVALID; 27 } else if (len < BTRFS_FID_SIZE_NON_CONNECTABLE) { 28 *max_len = BTRFS_FID_SIZE_NON_CONNECTABLE; 29 return FILEID_INVALID; 30 } 31 32 len = BTRFS_FID_SIZE_NON_CONNECTABLE; 33 type = FILEID_BTRFS_WITHOUT_PARENT; 34 35 fid->objectid = btrfs_ino(BTRFS_I(inode)); 36 fid->root_objectid = BTRFS_I(inode)->root->root_key.objectid; 37 fid->gen = inode->i_generation; 38 39 if (parent) { 40 u64 parent_root_id; 41 42 fid->parent_objectid = BTRFS_I(parent)->location.objectid; 43 fid->parent_gen = parent->i_generation; 44 parent_root_id = BTRFS_I(parent)->root->root_key.objectid; 45 46 if (parent_root_id != fid->root_objectid) { 47 fid->parent_root_objectid = parent_root_id; 48 len = BTRFS_FID_SIZE_CONNECTABLE_ROOT; 49 type = FILEID_BTRFS_WITH_PARENT_ROOT; 50 } else { 51 len = BTRFS_FID_SIZE_CONNECTABLE; 52 type = FILEID_BTRFS_WITH_PARENT; 53 } 54 } 55 56 *max_len = len; 57 return type; 58 } 59 60 static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid, 61 u64 root_objectid, u32 generation, 62 int check_generation) 63 { 64 struct btrfs_fs_info *fs_info = btrfs_sb(sb); 65 struct btrfs_root *root; 66 struct inode *inode; 67 struct btrfs_key key; 68 int index; 69 int err = 0; 70 71 if (objectid < BTRFS_FIRST_FREE_OBJECTID) 72 return ERR_PTR(-ESTALE); 73 74 key.objectid = root_objectid; 75 key.type = BTRFS_ROOT_ITEM_KEY; 76 key.offset = (u64)-1; 77 78 index = srcu_read_lock(&fs_info->subvol_srcu); 79 80 root = btrfs_get_fs_root(fs_info, &key, true); 81 if (IS_ERR(root)) { 82 err = PTR_ERR(root); 83 goto fail; 84 } 85 86 key.objectid = objectid; 87 key.type = BTRFS_INODE_ITEM_KEY; 88 key.offset = 0; 89 90 inode = btrfs_iget(sb, &key, root); 91 btrfs_put_root(root); 92 if (IS_ERR(inode)) { 93 err = PTR_ERR(inode); 94 goto fail; 95 } 96 97 srcu_read_unlock(&fs_info->subvol_srcu, index); 98 99 if (check_generation && generation != inode->i_generation) { 100 iput(inode); 101 return ERR_PTR(-ESTALE); 102 } 103 104 return d_obtain_alias(inode); 105 fail: 106 srcu_read_unlock(&fs_info->subvol_srcu, index); 107 return ERR_PTR(err); 108 } 109 110 static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh, 111 int fh_len, int fh_type) 112 { 113 struct btrfs_fid *fid = (struct btrfs_fid *) fh; 114 u64 objectid, root_objectid; 115 u32 generation; 116 117 if (fh_type == FILEID_BTRFS_WITH_PARENT) { 118 if (fh_len < BTRFS_FID_SIZE_CONNECTABLE) 119 return NULL; 120 root_objectid = fid->root_objectid; 121 } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) { 122 if (fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT) 123 return NULL; 124 root_objectid = fid->parent_root_objectid; 125 } else 126 return NULL; 127 128 objectid = fid->parent_objectid; 129 generation = fid->parent_gen; 130 131 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1); 132 } 133 134 static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh, 135 int fh_len, int fh_type) 136 { 137 struct btrfs_fid *fid = (struct btrfs_fid *) fh; 138 u64 objectid, root_objectid; 139 u32 generation; 140 141 if ((fh_type != FILEID_BTRFS_WITH_PARENT || 142 fh_len < BTRFS_FID_SIZE_CONNECTABLE) && 143 (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT || 144 fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT) && 145 (fh_type != FILEID_BTRFS_WITHOUT_PARENT || 146 fh_len < BTRFS_FID_SIZE_NON_CONNECTABLE)) 147 return NULL; 148 149 objectid = fid->objectid; 150 root_objectid = fid->root_objectid; 151 generation = fid->gen; 152 153 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1); 154 } 155 156 static struct dentry *btrfs_get_parent(struct dentry *child) 157 { 158 struct inode *dir = d_inode(child); 159 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 160 struct btrfs_root *root = BTRFS_I(dir)->root; 161 struct btrfs_path *path; 162 struct extent_buffer *leaf; 163 struct btrfs_root_ref *ref; 164 struct btrfs_key key; 165 struct btrfs_key found_key; 166 int ret; 167 168 path = btrfs_alloc_path(); 169 if (!path) 170 return ERR_PTR(-ENOMEM); 171 172 if (btrfs_ino(BTRFS_I(dir)) == BTRFS_FIRST_FREE_OBJECTID) { 173 key.objectid = root->root_key.objectid; 174 key.type = BTRFS_ROOT_BACKREF_KEY; 175 key.offset = (u64)-1; 176 root = fs_info->tree_root; 177 } else { 178 key.objectid = btrfs_ino(BTRFS_I(dir)); 179 key.type = BTRFS_INODE_REF_KEY; 180 key.offset = (u64)-1; 181 } 182 183 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 184 if (ret < 0) 185 goto fail; 186 187 BUG_ON(ret == 0); /* Key with offset of -1 found */ 188 if (path->slots[0] == 0) { 189 ret = -ENOENT; 190 goto fail; 191 } 192 193 path->slots[0]--; 194 leaf = path->nodes[0]; 195 196 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 197 if (found_key.objectid != key.objectid || found_key.type != key.type) { 198 ret = -ENOENT; 199 goto fail; 200 } 201 202 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) { 203 ref = btrfs_item_ptr(leaf, path->slots[0], 204 struct btrfs_root_ref); 205 key.objectid = btrfs_root_ref_dirid(leaf, ref); 206 } else { 207 key.objectid = found_key.offset; 208 } 209 btrfs_free_path(path); 210 211 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) { 212 return btrfs_get_dentry(fs_info->sb, key.objectid, 213 found_key.offset, 0, 0); 214 } 215 216 key.type = BTRFS_INODE_ITEM_KEY; 217 key.offset = 0; 218 return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root)); 219 fail: 220 btrfs_free_path(path); 221 return ERR_PTR(ret); 222 } 223 224 static int btrfs_get_name(struct dentry *parent, char *name, 225 struct dentry *child) 226 { 227 struct inode *inode = d_inode(child); 228 struct inode *dir = d_inode(parent); 229 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 230 struct btrfs_path *path; 231 struct btrfs_root *root = BTRFS_I(dir)->root; 232 struct btrfs_inode_ref *iref; 233 struct btrfs_root_ref *rref; 234 struct extent_buffer *leaf; 235 unsigned long name_ptr; 236 struct btrfs_key key; 237 int name_len; 238 int ret; 239 u64 ino; 240 241 if (!S_ISDIR(dir->i_mode)) 242 return -EINVAL; 243 244 ino = btrfs_ino(BTRFS_I(inode)); 245 246 path = btrfs_alloc_path(); 247 if (!path) 248 return -ENOMEM; 249 path->leave_spinning = 1; 250 251 if (ino == BTRFS_FIRST_FREE_OBJECTID) { 252 key.objectid = BTRFS_I(inode)->root->root_key.objectid; 253 key.type = BTRFS_ROOT_BACKREF_KEY; 254 key.offset = (u64)-1; 255 root = fs_info->tree_root; 256 } else { 257 key.objectid = ino; 258 key.offset = btrfs_ino(BTRFS_I(dir)); 259 key.type = BTRFS_INODE_REF_KEY; 260 } 261 262 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 263 if (ret < 0) { 264 btrfs_free_path(path); 265 return ret; 266 } else if (ret > 0) { 267 if (ino == BTRFS_FIRST_FREE_OBJECTID) { 268 path->slots[0]--; 269 } else { 270 btrfs_free_path(path); 271 return -ENOENT; 272 } 273 } 274 leaf = path->nodes[0]; 275 276 if (ino == BTRFS_FIRST_FREE_OBJECTID) { 277 rref = btrfs_item_ptr(leaf, path->slots[0], 278 struct btrfs_root_ref); 279 name_ptr = (unsigned long)(rref + 1); 280 name_len = btrfs_root_ref_name_len(leaf, rref); 281 } else { 282 iref = btrfs_item_ptr(leaf, path->slots[0], 283 struct btrfs_inode_ref); 284 name_ptr = (unsigned long)(iref + 1); 285 name_len = btrfs_inode_ref_name_len(leaf, iref); 286 } 287 288 read_extent_buffer(leaf, name, name_ptr, name_len); 289 btrfs_free_path(path); 290 291 /* 292 * have to add the null termination to make sure that reconnect_path 293 * gets the right len for strlen 294 */ 295 name[name_len] = '\0'; 296 297 return 0; 298 } 299 300 const struct export_operations btrfs_export_ops = { 301 .encode_fh = btrfs_encode_fh, 302 .fh_to_dentry = btrfs_fh_to_dentry, 303 .fh_to_parent = btrfs_fh_to_parent, 304 .get_parent = btrfs_get_parent, 305 .get_name = btrfs_get_name, 306 }; 307