Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/fs.h>
0004 #include <linux/types.h>
0005 #include "ctree.h"
0006 #include "disk-io.h"
0007 #include "btrfs_inode.h"
0008 #include "print-tree.h"
0009 #include "export.h"
0010 
0011 #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
0012                          parent_objectid) / 4)
0013 #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \
0014                          parent_root_objectid) / 4)
0015 #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4)
0016 
0017 static int btrfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
0018                struct inode *parent)
0019 {
0020     struct btrfs_fid *fid = (struct btrfs_fid *)fh;
0021     int len = *max_len;
0022     int type;
0023 
0024     if (parent && (len < BTRFS_FID_SIZE_CONNECTABLE)) {
0025         *max_len = BTRFS_FID_SIZE_CONNECTABLE;
0026         return FILEID_INVALID;
0027     } else if (len < BTRFS_FID_SIZE_NON_CONNECTABLE) {
0028         *max_len = BTRFS_FID_SIZE_NON_CONNECTABLE;
0029         return FILEID_INVALID;
0030     }
0031 
0032     len  = BTRFS_FID_SIZE_NON_CONNECTABLE;
0033     type = FILEID_BTRFS_WITHOUT_PARENT;
0034 
0035     fid->objectid = btrfs_ino(BTRFS_I(inode));
0036     fid->root_objectid = BTRFS_I(inode)->root->root_key.objectid;
0037     fid->gen = inode->i_generation;
0038 
0039     if (parent) {
0040         u64 parent_root_id;
0041 
0042         fid->parent_objectid = BTRFS_I(parent)->location.objectid;
0043         fid->parent_gen = parent->i_generation;
0044         parent_root_id = BTRFS_I(parent)->root->root_key.objectid;
0045 
0046         if (parent_root_id != fid->root_objectid) {
0047             fid->parent_root_objectid = parent_root_id;
0048             len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
0049             type = FILEID_BTRFS_WITH_PARENT_ROOT;
0050         } else {
0051             len = BTRFS_FID_SIZE_CONNECTABLE;
0052             type = FILEID_BTRFS_WITH_PARENT;
0053         }
0054     }
0055 
0056     *max_len = len;
0057     return type;
0058 }
0059 
0060 struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
0061                 u64 root_objectid, u32 generation,
0062                 int check_generation)
0063 {
0064     struct btrfs_fs_info *fs_info = btrfs_sb(sb);
0065     struct btrfs_root *root;
0066     struct inode *inode;
0067 
0068     if (objectid < BTRFS_FIRST_FREE_OBJECTID)
0069         return ERR_PTR(-ESTALE);
0070 
0071     root = btrfs_get_fs_root(fs_info, root_objectid, true);
0072     if (IS_ERR(root))
0073         return ERR_CAST(root);
0074 
0075     inode = btrfs_iget(sb, objectid, root);
0076     btrfs_put_root(root);
0077     if (IS_ERR(inode))
0078         return ERR_CAST(inode);
0079 
0080     if (check_generation && generation != inode->i_generation) {
0081         iput(inode);
0082         return ERR_PTR(-ESTALE);
0083     }
0084 
0085     return d_obtain_alias(inode);
0086 }
0087 
0088 static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
0089                      int fh_len, int fh_type)
0090 {
0091     struct btrfs_fid *fid = (struct btrfs_fid *) fh;
0092     u64 objectid, root_objectid;
0093     u32 generation;
0094 
0095     if (fh_type == FILEID_BTRFS_WITH_PARENT) {
0096         if (fh_len <  BTRFS_FID_SIZE_CONNECTABLE)
0097             return NULL;
0098         root_objectid = fid->root_objectid;
0099     } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
0100         if (fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT)
0101             return NULL;
0102         root_objectid = fid->parent_root_objectid;
0103     } else
0104         return NULL;
0105 
0106     objectid = fid->parent_objectid;
0107     generation = fid->parent_gen;
0108 
0109     return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
0110 }
0111 
0112 static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
0113                      int fh_len, int fh_type)
0114 {
0115     struct btrfs_fid *fid = (struct btrfs_fid *) fh;
0116     u64 objectid, root_objectid;
0117     u32 generation;
0118 
0119     if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
0120          fh_len < BTRFS_FID_SIZE_CONNECTABLE) &&
0121         (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
0122          fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
0123         (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
0124          fh_len < BTRFS_FID_SIZE_NON_CONNECTABLE))
0125         return NULL;
0126 
0127     objectid = fid->objectid;
0128     root_objectid = fid->root_objectid;
0129     generation = fid->gen;
0130 
0131     return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
0132 }
0133 
0134 struct dentry *btrfs_get_parent(struct dentry *child)
0135 {
0136     struct inode *dir = d_inode(child);
0137     struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
0138     struct btrfs_root *root = BTRFS_I(dir)->root;
0139     struct btrfs_path *path;
0140     struct extent_buffer *leaf;
0141     struct btrfs_root_ref *ref;
0142     struct btrfs_key key;
0143     struct btrfs_key found_key;
0144     int ret;
0145 
0146     path = btrfs_alloc_path();
0147     if (!path)
0148         return ERR_PTR(-ENOMEM);
0149 
0150     if (btrfs_ino(BTRFS_I(dir)) == BTRFS_FIRST_FREE_OBJECTID) {
0151         key.objectid = root->root_key.objectid;
0152         key.type = BTRFS_ROOT_BACKREF_KEY;
0153         key.offset = (u64)-1;
0154         root = fs_info->tree_root;
0155     } else {
0156         key.objectid = btrfs_ino(BTRFS_I(dir));
0157         key.type = BTRFS_INODE_REF_KEY;
0158         key.offset = (u64)-1;
0159     }
0160 
0161     ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
0162     if (ret < 0)
0163         goto fail;
0164 
0165     BUG_ON(ret == 0); /* Key with offset of -1 found */
0166     if (path->slots[0] == 0) {
0167         ret = -ENOENT;
0168         goto fail;
0169     }
0170 
0171     path->slots[0]--;
0172     leaf = path->nodes[0];
0173 
0174     btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
0175     if (found_key.objectid != key.objectid || found_key.type != key.type) {
0176         ret = -ENOENT;
0177         goto fail;
0178     }
0179 
0180     if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
0181         ref = btrfs_item_ptr(leaf, path->slots[0],
0182                      struct btrfs_root_ref);
0183         key.objectid = btrfs_root_ref_dirid(leaf, ref);
0184     } else {
0185         key.objectid = found_key.offset;
0186     }
0187     btrfs_free_path(path);
0188 
0189     if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
0190         return btrfs_get_dentry(fs_info->sb, key.objectid,
0191                     found_key.offset, 0, 0);
0192     }
0193 
0194     return d_obtain_alias(btrfs_iget(fs_info->sb, key.objectid, root));
0195 fail:
0196     btrfs_free_path(path);
0197     return ERR_PTR(ret);
0198 }
0199 
0200 static int btrfs_get_name(struct dentry *parent, char *name,
0201               struct dentry *child)
0202 {
0203     struct inode *inode = d_inode(child);
0204     struct inode *dir = d_inode(parent);
0205     struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
0206     struct btrfs_path *path;
0207     struct btrfs_root *root = BTRFS_I(dir)->root;
0208     struct btrfs_inode_ref *iref;
0209     struct btrfs_root_ref *rref;
0210     struct extent_buffer *leaf;
0211     unsigned long name_ptr;
0212     struct btrfs_key key;
0213     int name_len;
0214     int ret;
0215     u64 ino;
0216 
0217     if (!S_ISDIR(dir->i_mode))
0218         return -EINVAL;
0219 
0220     ino = btrfs_ino(BTRFS_I(inode));
0221 
0222     path = btrfs_alloc_path();
0223     if (!path)
0224         return -ENOMEM;
0225 
0226     if (ino == BTRFS_FIRST_FREE_OBJECTID) {
0227         key.objectid = BTRFS_I(inode)->root->root_key.objectid;
0228         key.type = BTRFS_ROOT_BACKREF_KEY;
0229         key.offset = (u64)-1;
0230         root = fs_info->tree_root;
0231     } else {
0232         key.objectid = ino;
0233         key.offset = btrfs_ino(BTRFS_I(dir));
0234         key.type = BTRFS_INODE_REF_KEY;
0235     }
0236 
0237     ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
0238     if (ret < 0) {
0239         btrfs_free_path(path);
0240         return ret;
0241     } else if (ret > 0) {
0242         if (ino == BTRFS_FIRST_FREE_OBJECTID) {
0243             path->slots[0]--;
0244         } else {
0245             btrfs_free_path(path);
0246             return -ENOENT;
0247         }
0248     }
0249     leaf = path->nodes[0];
0250 
0251     if (ino == BTRFS_FIRST_FREE_OBJECTID) {
0252         rref = btrfs_item_ptr(leaf, path->slots[0],
0253                      struct btrfs_root_ref);
0254         name_ptr = (unsigned long)(rref + 1);
0255         name_len = btrfs_root_ref_name_len(leaf, rref);
0256     } else {
0257         iref = btrfs_item_ptr(leaf, path->slots[0],
0258                       struct btrfs_inode_ref);
0259         name_ptr = (unsigned long)(iref + 1);
0260         name_len = btrfs_inode_ref_name_len(leaf, iref);
0261     }
0262 
0263     read_extent_buffer(leaf, name, name_ptr, name_len);
0264     btrfs_free_path(path);
0265 
0266     /*
0267      * have to add the null termination to make sure that reconnect_path
0268      * gets the right len for strlen
0269      */
0270     name[name_len] = '\0';
0271 
0272     return 0;
0273 }
0274 
0275 const struct export_operations btrfs_export_ops = {
0276     .encode_fh  = btrfs_encode_fh,
0277     .fh_to_dentry   = btrfs_fh_to_dentry,
0278     .fh_to_parent   = btrfs_fh_to_parent,
0279     .get_parent = btrfs_get_parent,
0280     .get_name   = btrfs_get_name,
0281 };