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