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 if (!btrfs_grab_fs_root(root)) { 86 err = -ENOENT; 87 goto fail; 88 } 89 90 key.objectid = objectid; 91 key.type = BTRFS_INODE_ITEM_KEY; 92 key.offset = 0; 93 94 inode = btrfs_iget(sb, &key, root); 95 btrfs_put_fs_root(root); 96 if (IS_ERR(inode)) { 97 err = PTR_ERR(inode); 98 goto fail; 99 } 100 101 srcu_read_unlock(&fs_info->subvol_srcu, index); 102 103 if (check_generation && generation != inode->i_generation) { 104 iput(inode); 105 return ERR_PTR(-ESTALE); 106 } 107 108 return d_obtain_alias(inode); 109 fail: 110 srcu_read_unlock(&fs_info->subvol_srcu, index); 111 return ERR_PTR(err); 112 } 113 114 static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh, 115 int fh_len, int fh_type) 116 { 117 struct btrfs_fid *fid = (struct btrfs_fid *) fh; 118 u64 objectid, root_objectid; 119 u32 generation; 120 121 if (fh_type == FILEID_BTRFS_WITH_PARENT) { 122 if (fh_len < BTRFS_FID_SIZE_CONNECTABLE) 123 return NULL; 124 root_objectid = fid->root_objectid; 125 } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) { 126 if (fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT) 127 return NULL; 128 root_objectid = fid->parent_root_objectid; 129 } else 130 return NULL; 131 132 objectid = fid->parent_objectid; 133 generation = fid->parent_gen; 134 135 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1); 136 } 137 138 static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh, 139 int fh_len, int fh_type) 140 { 141 struct btrfs_fid *fid = (struct btrfs_fid *) fh; 142 u64 objectid, root_objectid; 143 u32 generation; 144 145 if ((fh_type != FILEID_BTRFS_WITH_PARENT || 146 fh_len < BTRFS_FID_SIZE_CONNECTABLE) && 147 (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT || 148 fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT) && 149 (fh_type != FILEID_BTRFS_WITHOUT_PARENT || 150 fh_len < BTRFS_FID_SIZE_NON_CONNECTABLE)) 151 return NULL; 152 153 objectid = fid->objectid; 154 root_objectid = fid->root_objectid; 155 generation = fid->gen; 156 157 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1); 158 } 159 160 static struct dentry *btrfs_get_parent(struct dentry *child) 161 { 162 struct inode *dir = d_inode(child); 163 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 164 struct btrfs_root *root = BTRFS_I(dir)->root; 165 struct btrfs_path *path; 166 struct extent_buffer *leaf; 167 struct btrfs_root_ref *ref; 168 struct btrfs_key key; 169 struct btrfs_key found_key; 170 int ret; 171 172 path = btrfs_alloc_path(); 173 if (!path) 174 return ERR_PTR(-ENOMEM); 175 176 if (btrfs_ino(BTRFS_I(dir)) == BTRFS_FIRST_FREE_OBJECTID) { 177 key.objectid = root->root_key.objectid; 178 key.type = BTRFS_ROOT_BACKREF_KEY; 179 key.offset = (u64)-1; 180 root = fs_info->tree_root; 181 } else { 182 key.objectid = btrfs_ino(BTRFS_I(dir)); 183 key.type = BTRFS_INODE_REF_KEY; 184 key.offset = (u64)-1; 185 } 186 187 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 188 if (ret < 0) 189 goto fail; 190 191 BUG_ON(ret == 0); /* Key with offset of -1 found */ 192 if (path->slots[0] == 0) { 193 ret = -ENOENT; 194 goto fail; 195 } 196 197 path->slots[0]--; 198 leaf = path->nodes[0]; 199 200 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 201 if (found_key.objectid != key.objectid || found_key.type != key.type) { 202 ret = -ENOENT; 203 goto fail; 204 } 205 206 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) { 207 ref = btrfs_item_ptr(leaf, path->slots[0], 208 struct btrfs_root_ref); 209 key.objectid = btrfs_root_ref_dirid(leaf, ref); 210 } else { 211 key.objectid = found_key.offset; 212 } 213 btrfs_free_path(path); 214 215 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) { 216 return btrfs_get_dentry(fs_info->sb, key.objectid, 217 found_key.offset, 0, 0); 218 } 219 220 key.type = BTRFS_INODE_ITEM_KEY; 221 key.offset = 0; 222 return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root)); 223 fail: 224 btrfs_free_path(path); 225 return ERR_PTR(ret); 226 } 227 228 static int btrfs_get_name(struct dentry *parent, char *name, 229 struct dentry *child) 230 { 231 struct inode *inode = d_inode(child); 232 struct inode *dir = d_inode(parent); 233 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 234 struct btrfs_path *path; 235 struct btrfs_root *root = BTRFS_I(dir)->root; 236 struct btrfs_inode_ref *iref; 237 struct btrfs_root_ref *rref; 238 struct extent_buffer *leaf; 239 unsigned long name_ptr; 240 struct btrfs_key key; 241 int name_len; 242 int ret; 243 u64 ino; 244 245 if (!S_ISDIR(dir->i_mode)) 246 return -EINVAL; 247 248 ino = btrfs_ino(BTRFS_I(inode)); 249 250 path = btrfs_alloc_path(); 251 if (!path) 252 return -ENOMEM; 253 path->leave_spinning = 1; 254 255 if (ino == BTRFS_FIRST_FREE_OBJECTID) { 256 key.objectid = BTRFS_I(inode)->root->root_key.objectid; 257 key.type = BTRFS_ROOT_BACKREF_KEY; 258 key.offset = (u64)-1; 259 root = fs_info->tree_root; 260 } else { 261 key.objectid = ino; 262 key.offset = btrfs_ino(BTRFS_I(dir)); 263 key.type = BTRFS_INODE_REF_KEY; 264 } 265 266 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 267 if (ret < 0) { 268 btrfs_free_path(path); 269 return ret; 270 } else if (ret > 0) { 271 if (ino == BTRFS_FIRST_FREE_OBJECTID) { 272 path->slots[0]--; 273 } else { 274 btrfs_free_path(path); 275 return -ENOENT; 276 } 277 } 278 leaf = path->nodes[0]; 279 280 if (ino == BTRFS_FIRST_FREE_OBJECTID) { 281 rref = btrfs_item_ptr(leaf, path->slots[0], 282 struct btrfs_root_ref); 283 name_ptr = (unsigned long)(rref + 1); 284 name_len = btrfs_root_ref_name_len(leaf, rref); 285 } else { 286 iref = btrfs_item_ptr(leaf, path->slots[0], 287 struct btrfs_inode_ref); 288 name_ptr = (unsigned long)(iref + 1); 289 name_len = btrfs_inode_ref_name_len(leaf, iref); 290 } 291 292 read_extent_buffer(leaf, name, name_ptr, name_len); 293 btrfs_free_path(path); 294 295 /* 296 * have to add the null termination to make sure that reconnect_path 297 * gets the right len for strlen 298 */ 299 name[name_len] = '\0'; 300 301 return 0; 302 } 303 304 const struct export_operations btrfs_export_ops = { 305 .encode_fh = btrfs_encode_fh, 306 .fh_to_dentry = btrfs_fh_to_dentry, 307 .fh_to_parent = btrfs_fh_to_parent, 308 .get_parent = btrfs_get_parent, 309 .get_name = btrfs_get_name, 310 }; 311