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