Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 3 | |
| 4 | #include <linux/exportfs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/slab.h> |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 6 | #include <asm/unaligned.h> |
| 7 | |
| 8 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 9 | #include "mds_client.h" |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 10 | |
| 11 | /* |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 12 | * Basic fh |
| 13 | */ |
| 14 | struct ceph_nfs_fh { |
| 15 | u64 ino; |
| 16 | } __attribute__ ((packed)); |
| 17 | |
| 18 | /* |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 19 | * Larger fh that includes parent ino. |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 20 | */ |
| 21 | struct ceph_nfs_confh { |
| 22 | u64 ino, parent_ino; |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 23 | } __attribute__ ((packed)); |
| 24 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 25 | /* |
| 26 | * fh for snapped inode |
| 27 | */ |
| 28 | struct ceph_nfs_snapfh { |
| 29 | u64 ino; |
| 30 | u64 snapid; |
| 31 | u64 parent_ino; |
| 32 | u32 hash; |
| 33 | } __attribute__ ((packed)); |
| 34 | |
| 35 | static int ceph_encode_snapfh(struct inode *inode, u32 *rawfh, int *max_len, |
| 36 | struct inode *parent_inode) |
| 37 | { |
Krzysztof Wilczynski | 536cc33 | 2019-08-31 23:57:12 +0200 | [diff] [blame] | 38 | static const int snap_handle_length = |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 39 | sizeof(struct ceph_nfs_snapfh) >> 2; |
| 40 | struct ceph_nfs_snapfh *sfh = (void *)rawfh; |
| 41 | u64 snapid = ceph_snap(inode); |
| 42 | int ret; |
| 43 | bool no_parent = true; |
| 44 | |
| 45 | if (*max_len < snap_handle_length) { |
| 46 | *max_len = snap_handle_length; |
| 47 | ret = FILEID_INVALID; |
| 48 | goto out; |
| 49 | } |
| 50 | |
| 51 | ret = -EINVAL; |
| 52 | if (snapid != CEPH_SNAPDIR) { |
| 53 | struct inode *dir; |
| 54 | struct dentry *dentry = d_find_alias(inode); |
| 55 | if (!dentry) |
| 56 | goto out; |
| 57 | |
| 58 | rcu_read_lock(); |
| 59 | dir = d_inode_rcu(dentry->d_parent); |
| 60 | if (ceph_snap(dir) != CEPH_SNAPDIR) { |
| 61 | sfh->parent_ino = ceph_ino(dir); |
| 62 | sfh->hash = ceph_dentry_hash(dir, dentry); |
| 63 | no_parent = false; |
| 64 | } |
| 65 | rcu_read_unlock(); |
| 66 | dput(dentry); |
| 67 | } |
| 68 | |
| 69 | if (no_parent) { |
| 70 | if (!S_ISDIR(inode->i_mode)) |
| 71 | goto out; |
| 72 | sfh->parent_ino = sfh->ino; |
| 73 | sfh->hash = 0; |
| 74 | } |
| 75 | sfh->ino = ceph_ino(inode); |
| 76 | sfh->snapid = snapid; |
| 77 | |
| 78 | *max_len = snap_handle_length; |
| 79 | ret = FILEID_BTRFS_WITH_PARENT; |
| 80 | out: |
| 81 | dout("encode_snapfh %llx.%llx ret=%d\n", ceph_vinop(inode), ret); |
| 82 | return ret; |
| 83 | } |
| 84 | |
Sage Weil | c862868 | 2012-04-05 12:07:36 -0700 | [diff] [blame] | 85 | static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len, |
| 86 | struct inode *parent_inode) |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 87 | { |
Krzysztof Wilczynski | 536cc33 | 2019-08-31 23:57:12 +0200 | [diff] [blame] | 88 | static const int handle_length = |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 89 | sizeof(struct ceph_nfs_fh) >> 2; |
Krzysztof Wilczynski | 536cc33 | 2019-08-31 23:57:12 +0200 | [diff] [blame] | 90 | static const int connected_handle_length = |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 91 | sizeof(struct ceph_nfs_confh) >> 2; |
Aneesh Kumar K.V | 92923dc | 2010-10-05 16:03:41 +0530 | [diff] [blame] | 92 | int type; |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 93 | |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 94 | if (ceph_snap(inode) != CEPH_NOSNAP) |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 95 | return ceph_encode_snapfh(inode, rawfh, max_len, parent_inode); |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 96 | |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 97 | if (parent_inode && (*max_len < connected_handle_length)) { |
Aneesh Kumar K.V | 92923dc | 2010-10-05 16:03:41 +0530 | [diff] [blame] | 98 | *max_len = connected_handle_length; |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 99 | return FILEID_INVALID; |
| 100 | } else if (*max_len < handle_length) { |
Aneesh Kumar K.V | bba0cd0 | 2010-10-05 16:03:42 +0530 | [diff] [blame] | 101 | *max_len = handle_length; |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 102 | return FILEID_INVALID; |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 103 | } |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 104 | |
| 105 | if (parent_inode) { |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 106 | struct ceph_nfs_confh *cfh = (void *)rawfh; |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 107 | dout("encode_fh %llx with parent %llx\n", |
| 108 | ceph_ino(inode), ceph_ino(parent_inode)); |
| 109 | cfh->ino = ceph_ino(inode); |
| 110 | cfh->parent_ino = ceph_ino(parent_inode); |
| 111 | *max_len = connected_handle_length; |
| 112 | type = FILEID_INO32_GEN_PARENT; |
| 113 | } else { |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 114 | struct ceph_nfs_fh *fh = (void *)rawfh; |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 115 | dout("encode_fh %llx\n", ceph_ino(inode)); |
| 116 | fh->ino = ceph_ino(inode); |
| 117 | *max_len = handle_length; |
| 118 | type = FILEID_INO32_GEN; |
| 119 | } |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 120 | return type; |
| 121 | } |
| 122 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 123 | static struct inode *__lookup_inode(struct super_block *sb, u64 ino) |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 124 | { |
Sage Weil | 3c454cf | 2011-04-06 09:31:40 -0700 | [diff] [blame] | 125 | struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc; |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 126 | struct inode *inode; |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 127 | struct ceph_vino vino; |
| 128 | int err; |
| 129 | |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 130 | vino.ino = ino; |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 131 | vino.snap = CEPH_NOSNAP; |
| 132 | inode = ceph_find_inode(sb, vino); |
Sage Weil | 3c454cf | 2011-04-06 09:31:40 -0700 | [diff] [blame] | 133 | if (!inode) { |
| 134 | struct ceph_mds_request *req; |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 135 | int mask; |
Sage Weil | 3c454cf | 2011-04-06 09:31:40 -0700 | [diff] [blame] | 136 | |
| 137 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO, |
| 138 | USE_ANY_MDS); |
| 139 | if (IS_ERR(req)) |
| 140 | return ERR_CAST(req); |
| 141 | |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 142 | mask = CEPH_STAT_CAP_INODE; |
| 143 | if (ceph_security_xattr_wanted(d_inode(sb->s_root))) |
| 144 | mask |= CEPH_CAP_XATTR_SHARED; |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 145 | req->r_args.lookupino.mask = cpu_to_le32(mask); |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 146 | |
Sage Weil | 3c454cf | 2011-04-06 09:31:40 -0700 | [diff] [blame] | 147 | req->r_ino1 = vino; |
| 148 | req->r_num_caps = 1; |
| 149 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
Sage Weil | 45e3d3e | 2011-04-06 09:35:00 -0700 | [diff] [blame] | 150 | inode = req->r_target_inode; |
| 151 | if (inode) |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 152 | ihold(inode); |
Sage Weil | 3c454cf | 2011-04-06 09:31:40 -0700 | [diff] [blame] | 153 | ceph_mdsc_put_request(req); |
Sage Weil | 3c454cf | 2011-04-06 09:31:40 -0700 | [diff] [blame] | 154 | if (!inode) |
Luis Henriques | 3886274 | 2019-03-21 10:20:09 +0000 | [diff] [blame] | 155 | return err < 0 ? ERR_PTR(err) : ERR_PTR(-ESTALE); |
Sage Weil | 3c454cf | 2011-04-06 09:31:40 -0700 | [diff] [blame] | 156 | } |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 157 | return inode; |
| 158 | } |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 159 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 160 | struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino) |
| 161 | { |
| 162 | struct inode *inode = __lookup_inode(sb, ino); |
| 163 | if (IS_ERR(inode)) |
| 164 | return inode; |
| 165 | if (inode->i_nlink == 0) { |
| 166 | iput(inode); |
| 167 | return ERR_PTR(-ESTALE); |
| 168 | } |
Luis Henriques | 3886274 | 2019-03-21 10:20:09 +0000 | [diff] [blame] | 169 | return inode; |
| 170 | } |
| 171 | |
| 172 | static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino) |
| 173 | { |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 174 | struct inode *inode = __lookup_inode(sb, ino); |
Luis Henriques | 878dabb | 2020-05-18 18:47:26 +0100 | [diff] [blame] | 175 | int err; |
| 176 | |
Luis Henriques | 3886274 | 2019-03-21 10:20:09 +0000 | [diff] [blame] | 177 | if (IS_ERR(inode)) |
| 178 | return ERR_CAST(inode); |
Luis Henriques | 878dabb | 2020-05-18 18:47:26 +0100 | [diff] [blame] | 179 | /* We need LINK caps to reliably check i_nlink */ |
| 180 | err = ceph_do_getattr(inode, CEPH_CAP_LINK_SHARED, false); |
| 181 | if (err) |
| 182 | return ERR_PTR(err); |
| 183 | /* -ESTALE if inode as been unlinked and no file is open */ |
| 184 | if ((inode->i_nlink == 0) && (atomic_read(&inode->i_count) == 1)) { |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 185 | iput(inode); |
| 186 | return ERR_PTR(-ESTALE); |
| 187 | } |
Al Viro | ad5cb12 | 2016-10-28 22:05:13 -0400 | [diff] [blame] | 188 | return d_obtain_alias(inode); |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 191 | static struct dentry *__snapfh_to_dentry(struct super_block *sb, |
| 192 | struct ceph_nfs_snapfh *sfh, |
| 193 | bool want_parent) |
| 194 | { |
| 195 | struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc; |
| 196 | struct ceph_mds_request *req; |
| 197 | struct inode *inode; |
| 198 | struct ceph_vino vino; |
| 199 | int mask; |
| 200 | int err; |
| 201 | bool unlinked = false; |
| 202 | |
| 203 | if (want_parent) { |
| 204 | vino.ino = sfh->parent_ino; |
| 205 | if (sfh->snapid == CEPH_SNAPDIR) |
| 206 | vino.snap = CEPH_NOSNAP; |
| 207 | else if (sfh->ino == sfh->parent_ino) |
| 208 | vino.snap = CEPH_SNAPDIR; |
| 209 | else |
| 210 | vino.snap = sfh->snapid; |
| 211 | } else { |
| 212 | vino.ino = sfh->ino; |
| 213 | vino.snap = sfh->snapid; |
| 214 | } |
| 215 | inode = ceph_find_inode(sb, vino); |
| 216 | if (inode) |
| 217 | return d_obtain_alias(inode); |
| 218 | |
| 219 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO, |
| 220 | USE_ANY_MDS); |
| 221 | if (IS_ERR(req)) |
| 222 | return ERR_CAST(req); |
| 223 | |
| 224 | mask = CEPH_STAT_CAP_INODE; |
| 225 | if (ceph_security_xattr_wanted(d_inode(sb->s_root))) |
| 226 | mask |= CEPH_CAP_XATTR_SHARED; |
| 227 | req->r_args.lookupino.mask = cpu_to_le32(mask); |
| 228 | if (vino.snap < CEPH_NOSNAP) { |
| 229 | req->r_args.lookupino.snapid = cpu_to_le64(vino.snap); |
| 230 | if (!want_parent && sfh->ino != sfh->parent_ino) { |
| 231 | req->r_args.lookupino.parent = |
| 232 | cpu_to_le64(sfh->parent_ino); |
| 233 | req->r_args.lookupino.hash = |
| 234 | cpu_to_le32(sfh->hash); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | req->r_ino1 = vino; |
| 239 | req->r_num_caps = 1; |
| 240 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
| 241 | inode = req->r_target_inode; |
| 242 | if (inode) { |
| 243 | if (vino.snap == CEPH_SNAPDIR) { |
| 244 | if (inode->i_nlink == 0) |
| 245 | unlinked = true; |
| 246 | inode = ceph_get_snapdir(inode); |
| 247 | } else if (ceph_snap(inode) == vino.snap) { |
| 248 | ihold(inode); |
| 249 | } else { |
| 250 | /* mds does not support lookup snapped inode */ |
| 251 | err = -EOPNOTSUPP; |
| 252 | inode = NULL; |
| 253 | } |
| 254 | } |
| 255 | ceph_mdsc_put_request(req); |
| 256 | |
| 257 | if (want_parent) { |
| 258 | dout("snapfh_to_parent %llx.%llx\n err=%d\n", |
| 259 | vino.ino, vino.snap, err); |
| 260 | } else { |
| 261 | dout("snapfh_to_dentry %llx.%llx parent %llx hash %x err=%d", |
| 262 | vino.ino, vino.snap, sfh->parent_ino, sfh->hash, err); |
| 263 | } |
| 264 | if (!inode) |
| 265 | return ERR_PTR(-ESTALE); |
| 266 | /* see comments in ceph_get_parent() */ |
| 267 | return unlinked ? d_obtain_root(inode) : d_obtain_alias(inode); |
| 268 | } |
| 269 | |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 270 | /* |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 271 | * convert regular fh to dentry |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 272 | */ |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 273 | static struct dentry *ceph_fh_to_dentry(struct super_block *sb, |
| 274 | struct fid *fid, |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 275 | int fh_len, int fh_type) |
| 276 | { |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 277 | struct ceph_nfs_fh *fh = (void *)fid->raw; |
| 278 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 279 | if (fh_type == FILEID_BTRFS_WITH_PARENT) { |
| 280 | struct ceph_nfs_snapfh *sfh = (void *)fid->raw; |
| 281 | return __snapfh_to_dentry(sb, sfh, false); |
| 282 | } |
| 283 | |
Yan, Zheng | 4f32b42 | 2014-03-01 18:05:41 +0800 | [diff] [blame] | 284 | if (fh_type != FILEID_INO32_GEN && |
| 285 | fh_type != FILEID_INO32_GEN_PARENT) |
| 286 | return NULL; |
| 287 | if (fh_len < sizeof(*fh) / 4) |
| 288 | return NULL; |
| 289 | |
| 290 | dout("fh_to_dentry %llx\n", fh->ino); |
| 291 | return __fh_to_dentry(sb, fh->ino); |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 294 | static struct dentry *__get_parent(struct super_block *sb, |
| 295 | struct dentry *child, u64 ino) |
| 296 | { |
| 297 | struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc; |
| 298 | struct ceph_mds_request *req; |
| 299 | struct inode *inode; |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 300 | int mask; |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 301 | int err; |
| 302 | |
| 303 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPPARENT, |
| 304 | USE_ANY_MDS); |
| 305 | if (IS_ERR(req)) |
| 306 | return ERR_CAST(req); |
| 307 | |
| 308 | if (child) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 309 | req->r_inode = d_inode(child); |
| 310 | ihold(d_inode(child)); |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 311 | } else { |
| 312 | req->r_ino1 = (struct ceph_vino) { |
| 313 | .ino = ino, |
| 314 | .snap = CEPH_NOSNAP, |
| 315 | }; |
| 316 | } |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 317 | |
| 318 | mask = CEPH_STAT_CAP_INODE; |
| 319 | if (ceph_security_xattr_wanted(d_inode(sb->s_root))) |
| 320 | mask |= CEPH_CAP_XATTR_SHARED; |
| 321 | req->r_args.getattr.mask = cpu_to_le32(mask); |
| 322 | |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 323 | req->r_num_caps = 1; |
| 324 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
Qiujun Huang | c6d5029 | 2020-03-06 09:34:20 +0800 | [diff] [blame] | 325 | if (err) { |
| 326 | ceph_mdsc_put_request(req); |
| 327 | return ERR_PTR(err); |
| 328 | } |
| 329 | |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 330 | inode = req->r_target_inode; |
| 331 | if (inode) |
| 332 | ihold(inode); |
| 333 | ceph_mdsc_put_request(req); |
| 334 | if (!inode) |
| 335 | return ERR_PTR(-ENOENT); |
| 336 | |
Al Viro | ad5cb12 | 2016-10-28 22:05:13 -0400 | [diff] [blame] | 337 | return d_obtain_alias(inode); |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 338 | } |
| 339 | |
Fengguang Wu | e84be11 | 2014-04-09 14:11:31 +0800 | [diff] [blame] | 340 | static struct dentry *ceph_get_parent(struct dentry *child) |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 341 | { |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 342 | struct inode *inode = d_inode(child); |
| 343 | struct dentry *dn; |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 344 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 345 | if (ceph_snap(inode) != CEPH_NOSNAP) { |
| 346 | struct inode* dir; |
| 347 | bool unlinked = false; |
| 348 | /* do not support non-directory */ |
| 349 | if (!d_is_dir(child)) { |
| 350 | dn = ERR_PTR(-EINVAL); |
| 351 | goto out; |
| 352 | } |
| 353 | dir = __lookup_inode(inode->i_sb, ceph_ino(inode)); |
| 354 | if (IS_ERR(dir)) { |
| 355 | dn = ERR_CAST(dir); |
| 356 | goto out; |
| 357 | } |
| 358 | /* There can be multiple paths to access snapped inode. |
| 359 | * For simplicity, treat snapdir of head inode as parent */ |
| 360 | if (ceph_snap(inode) != CEPH_SNAPDIR) { |
| 361 | struct inode *snapdir = ceph_get_snapdir(dir); |
| 362 | if (dir->i_nlink == 0) |
| 363 | unlinked = true; |
| 364 | iput(dir); |
| 365 | if (IS_ERR(snapdir)) { |
| 366 | dn = ERR_CAST(snapdir); |
| 367 | goto out; |
| 368 | } |
| 369 | dir = snapdir; |
| 370 | } |
| 371 | /* If directory has already been deleted, futher get_parent |
| 372 | * will fail. Do not mark snapdir dentry as disconnected, |
| 373 | * this prevent exportfs from doing futher get_parent. */ |
| 374 | if (unlinked) |
| 375 | dn = d_obtain_root(dir); |
| 376 | else |
| 377 | dn = d_obtain_alias(dir); |
| 378 | } else { |
| 379 | dn = __get_parent(child->d_sb, child, 0); |
| 380 | } |
| 381 | out: |
| 382 | dout("get_parent %p ino %llx.%llx err=%ld\n", |
Hariprasad Kelam | 03af439 | 2019-05-25 14:45:59 +0530 | [diff] [blame] | 383 | child, ceph_vinop(inode), (long)PTR_ERR_OR_ZERO(dn)); |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 384 | return dn; |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 385 | } |
| 386 | |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 387 | /* |
Yan, Zheng | 8996f4f | 2014-03-01 23:09:05 +0800 | [diff] [blame] | 388 | * convert regular fh to parent |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 389 | */ |
| 390 | static struct dentry *ceph_fh_to_parent(struct super_block *sb, |
Yan, Zheng | 8996f4f | 2014-03-01 23:09:05 +0800 | [diff] [blame] | 391 | struct fid *fid, |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 392 | int fh_len, int fh_type) |
| 393 | { |
| 394 | struct ceph_nfs_confh *cfh = (void *)fid->raw; |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 395 | struct dentry *dentry; |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 396 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 397 | if (fh_type == FILEID_BTRFS_WITH_PARENT) { |
| 398 | struct ceph_nfs_snapfh *sfh = (void *)fid->raw; |
| 399 | return __snapfh_to_dentry(sb, sfh, true); |
| 400 | } |
| 401 | |
Yan, Zheng | 8996f4f | 2014-03-01 23:09:05 +0800 | [diff] [blame] | 402 | if (fh_type != FILEID_INO32_GEN_PARENT) |
| 403 | return NULL; |
Hugh Dickins | 35c2a7f | 2012-10-07 20:32:51 -0700 | [diff] [blame] | 404 | if (fh_len < sizeof(*cfh) / 4) |
Yan, Zheng | 8996f4f | 2014-03-01 23:09:05 +0800 | [diff] [blame] | 405 | return NULL; |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 406 | |
Yan, Zheng | 8996f4f | 2014-03-01 23:09:05 +0800 | [diff] [blame] | 407 | dout("fh_to_parent %llx\n", cfh->parent_ino); |
| 408 | dentry = __get_parent(sb, NULL, cfh->ino); |
Al Viro | b42b90d | 2016-06-24 23:49:03 -0400 | [diff] [blame] | 409 | if (unlikely(dentry == ERR_PTR(-ENOENT))) |
Yan, Zheng | 8996f4f | 2014-03-01 23:09:05 +0800 | [diff] [blame] | 410 | dentry = __fh_to_dentry(sb, cfh->parent_ino); |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 411 | return dentry; |
| 412 | } |
| 413 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 414 | static int __get_snap_name(struct dentry *parent, char *name, |
| 415 | struct dentry *child) |
| 416 | { |
| 417 | struct inode *inode = d_inode(child); |
| 418 | struct inode *dir = d_inode(parent); |
| 419 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
| 420 | struct ceph_mds_request *req = NULL; |
| 421 | char *last_name = NULL; |
| 422 | unsigned next_offset = 2; |
| 423 | int err = -EINVAL; |
| 424 | |
| 425 | if (ceph_ino(inode) != ceph_ino(dir)) |
| 426 | goto out; |
| 427 | if (ceph_snap(inode) == CEPH_SNAPDIR) { |
| 428 | if (ceph_snap(dir) == CEPH_NOSNAP) { |
| 429 | strcpy(name, fsc->mount_options->snapdir_name); |
| 430 | err = 0; |
| 431 | } |
| 432 | goto out; |
| 433 | } |
| 434 | if (ceph_snap(dir) != CEPH_SNAPDIR) |
| 435 | goto out; |
| 436 | |
| 437 | while (1) { |
| 438 | struct ceph_mds_reply_info_parsed *rinfo; |
| 439 | struct ceph_mds_reply_dir_entry *rde; |
| 440 | int i; |
| 441 | |
| 442 | req = ceph_mdsc_create_request(fsc->mdsc, CEPH_MDS_OP_LSSNAP, |
| 443 | USE_AUTH_MDS); |
| 444 | if (IS_ERR(req)) { |
| 445 | err = PTR_ERR(req); |
| 446 | req = NULL; |
| 447 | goto out; |
| 448 | } |
| 449 | err = ceph_alloc_readdir_reply_buffer(req, inode); |
| 450 | if (err) |
| 451 | goto out; |
| 452 | |
| 453 | req->r_direct_mode = USE_AUTH_MDS; |
| 454 | req->r_readdir_offset = next_offset; |
| 455 | req->r_args.readdir.flags = |
| 456 | cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS); |
| 457 | if (last_name) { |
| 458 | req->r_path2 = last_name; |
| 459 | last_name = NULL; |
| 460 | } |
| 461 | |
| 462 | req->r_inode = dir; |
| 463 | ihold(dir); |
| 464 | req->r_dentry = dget(parent); |
| 465 | |
| 466 | inode_lock(dir); |
| 467 | err = ceph_mdsc_do_request(fsc->mdsc, NULL, req); |
| 468 | inode_unlock(dir); |
| 469 | |
| 470 | if (err < 0) |
| 471 | goto out; |
| 472 | |
Ilya Dryomov | 0ed26f3 | 2019-07-29 11:33:08 +0200 | [diff] [blame] | 473 | rinfo = &req->r_reply_info; |
| 474 | for (i = 0; i < rinfo->dir_nr; i++) { |
| 475 | rde = rinfo->dir_entries + i; |
| 476 | BUG_ON(!rde->inode.in); |
| 477 | if (ceph_snap(inode) == |
| 478 | le64_to_cpu(rde->inode.in->snapid)) { |
| 479 | memcpy(name, rde->name, rde->name_len); |
| 480 | name[rde->name_len] = '\0'; |
| 481 | err = 0; |
| 482 | goto out; |
| 483 | } |
| 484 | } |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 485 | |
Ilya Dryomov | 0ed26f3 | 2019-07-29 11:33:08 +0200 | [diff] [blame] | 486 | if (rinfo->dir_end) |
| 487 | break; |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 488 | |
Ilya Dryomov | 0ed26f3 | 2019-07-29 11:33:08 +0200 | [diff] [blame] | 489 | BUG_ON(rinfo->dir_nr <= 0); |
| 490 | rde = rinfo->dir_entries + (rinfo->dir_nr - 1); |
| 491 | next_offset += rinfo->dir_nr; |
| 492 | last_name = kstrndup(rde->name, rde->name_len, GFP_KERNEL); |
| 493 | if (!last_name) { |
| 494 | err = -ENOMEM; |
| 495 | goto out; |
| 496 | } |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 497 | |
Ilya Dryomov | 0ed26f3 | 2019-07-29 11:33:08 +0200 | [diff] [blame] | 498 | ceph_mdsc_put_request(req); |
| 499 | req = NULL; |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 500 | } |
| 501 | err = -ENOENT; |
| 502 | out: |
| 503 | if (req) |
| 504 | ceph_mdsc_put_request(req); |
| 505 | kfree(last_name); |
| 506 | dout("get_snap_name %p ino %llx.%llx err=%d\n", |
| 507 | child, ceph_vinop(inode), err); |
| 508 | return err; |
| 509 | } |
| 510 | |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 511 | static int ceph_get_name(struct dentry *parent, char *name, |
| 512 | struct dentry *child) |
| 513 | { |
| 514 | struct ceph_mds_client *mdsc; |
| 515 | struct ceph_mds_request *req; |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 516 | struct inode *inode = d_inode(child); |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 517 | int err; |
| 518 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 519 | if (ceph_snap(inode) != CEPH_NOSNAP) |
| 520 | return __get_snap_name(parent, name, child); |
| 521 | |
| 522 | mdsc = ceph_inode_to_client(inode)->mdsc; |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 523 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPNAME, |
| 524 | USE_ANY_MDS); |
| 525 | if (IS_ERR(req)) |
| 526 | return PTR_ERR(req); |
| 527 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 528 | inode_lock(d_inode(parent)); |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 529 | |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 530 | req->r_inode = inode; |
| 531 | ihold(inode); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 532 | req->r_ino2 = ceph_vino(d_inode(parent)); |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 533 | req->r_parent = d_inode(parent); |
| 534 | set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 535 | req->r_num_caps = 2; |
| 536 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
| 537 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 538 | inode_unlock(d_inode(parent)); |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 539 | |
| 540 | if (!err) { |
| 541 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
| 542 | memcpy(name, rinfo->dname, rinfo->dname_len); |
| 543 | name[rinfo->dname_len] = 0; |
| 544 | dout("get_name %p ino %llx.%llx name %s\n", |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 545 | child, ceph_vinop(inode), name); |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 546 | } else { |
| 547 | dout("get_name %p ino %llx.%llx err %d\n", |
Yan, Zheng | 570df4e | 2017-11-15 17:39:40 +0800 | [diff] [blame] | 548 | child, ceph_vinop(inode), err); |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | ceph_mdsc_put_request(req); |
| 552 | return err; |
| 553 | } |
| 554 | |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 555 | const struct export_operations ceph_export_ops = { |
| 556 | .encode_fh = ceph_encode_fh, |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 557 | .fh_to_dentry = ceph_fh_to_dentry, |
| 558 | .fh_to_parent = ceph_fh_to_parent, |
Yan, Zheng | 9017c2e | 2014-03-01 22:11:45 +0800 | [diff] [blame] | 559 | .get_parent = ceph_get_parent, |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 560 | .get_name = ceph_get_name, |
Sage Weil | a8e63b7 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 561 | }; |