blob: 65540a4429b26aa68063ef914700d5d997ad3d00 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weila8e63b72009-10-06 11:31:13 -07003
4#include <linux/exportfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Sage Weila8e63b72009-10-06 11:31:13 -07006#include <asm/unaligned.h>
7
8#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07009#include "mds_client.h"
Sage Weila8e63b72009-10-06 11:31:13 -070010
11/*
Sage Weila8e63b72009-10-06 11:31:13 -070012 * Basic fh
13 */
14struct ceph_nfs_fh {
15 u64 ino;
16} __attribute__ ((packed));
17
18/*
Yan, Zheng4f32b422014-03-01 18:05:41 +080019 * Larger fh that includes parent ino.
Sage Weila8e63b72009-10-06 11:31:13 -070020 */
21struct ceph_nfs_confh {
22 u64 ino, parent_ino;
Sage Weila8e63b72009-10-06 11:31:13 -070023} __attribute__ ((packed));
24
Yan, Zheng570df4e2017-11-15 17:39:40 +080025/*
26 * fh for snapped inode
27 */
28struct ceph_nfs_snapfh {
29 u64 ino;
30 u64 snapid;
31 u64 parent_ino;
32 u32 hash;
33} __attribute__ ((packed));
34
35static int ceph_encode_snapfh(struct inode *inode, u32 *rawfh, int *max_len,
36 struct inode *parent_inode)
37{
Krzysztof Wilczynski536cc332019-08-31 23:57:12 +020038 static const int snap_handle_length =
Yan, Zheng570df4e2017-11-15 17:39:40 +080039 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;
80out:
81 dout("encode_snapfh %llx.%llx ret=%d\n", ceph_vinop(inode), ret);
82 return ret;
83}
84
Sage Weilc8628682012-04-05 12:07:36 -070085static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
86 struct inode *parent_inode)
Sage Weila8e63b72009-10-06 11:31:13 -070087{
Krzysztof Wilczynski536cc332019-08-31 23:57:12 +020088 static const int handle_length =
Yan, Zheng570df4e2017-11-15 17:39:40 +080089 sizeof(struct ceph_nfs_fh) >> 2;
Krzysztof Wilczynski536cc332019-08-31 23:57:12 +020090 static const int connected_handle_length =
Yan, Zheng570df4e2017-11-15 17:39:40 +080091 sizeof(struct ceph_nfs_confh) >> 2;
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053092 int type;
Sage Weila8e63b72009-10-06 11:31:13 -070093
Sage Weila8e63b72009-10-06 11:31:13 -070094 if (ceph_snap(inode) != CEPH_NOSNAP)
Yan, Zheng570df4e2017-11-15 17:39:40 +080095 return ceph_encode_snapfh(inode, rawfh, max_len, parent_inode);
Sage Weila8e63b72009-10-06 11:31:13 -070096
Yan, Zheng4f32b422014-03-01 18:05:41 +080097 if (parent_inode && (*max_len < connected_handle_length)) {
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053098 *max_len = connected_handle_length;
Yan, Zheng4f32b422014-03-01 18:05:41 +080099 return FILEID_INVALID;
100 } else if (*max_len < handle_length) {
Aneesh Kumar K.Vbba0cd02010-10-05 16:03:42 +0530101 *max_len = handle_length;
Yan, Zheng4f32b422014-03-01 18:05:41 +0800102 return FILEID_INVALID;
Sage Weila8e63b72009-10-06 11:31:13 -0700103 }
Yan, Zheng4f32b422014-03-01 18:05:41 +0800104
105 if (parent_inode) {
Yan, Zheng570df4e2017-11-15 17:39:40 +0800106 struct ceph_nfs_confh *cfh = (void *)rawfh;
Yan, Zheng4f32b422014-03-01 18:05:41 +0800107 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, Zheng570df4e2017-11-15 17:39:40 +0800114 struct ceph_nfs_fh *fh = (void *)rawfh;
Yan, Zheng4f32b422014-03-01 18:05:41 +0800115 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 Weila8e63b72009-10-06 11:31:13 -0700120 return type;
121}
122
Yan, Zheng570df4e2017-11-15 17:39:40 +0800123static struct inode *__lookup_inode(struct super_block *sb, u64 ino)
Sage Weila8e63b72009-10-06 11:31:13 -0700124{
Sage Weil3c454cf2011-04-06 09:31:40 -0700125 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
Sage Weila8e63b72009-10-06 11:31:13 -0700126 struct inode *inode;
Sage Weila8e63b72009-10-06 11:31:13 -0700127 struct ceph_vino vino;
128 int err;
129
Yan, Zheng4f32b422014-03-01 18:05:41 +0800130 vino.ino = ino;
Sage Weila8e63b72009-10-06 11:31:13 -0700131 vino.snap = CEPH_NOSNAP;
Jeff Laytond4f6b312021-04-01 13:55:11 -0400132
133 if (ceph_vino_is_reserved(vino))
134 return ERR_PTR(-ESTALE);
135
Sage Weila8e63b72009-10-06 11:31:13 -0700136 inode = ceph_find_inode(sb, vino);
Sage Weil3c454cf2011-04-06 09:31:40 -0700137 if (!inode) {
138 struct ceph_mds_request *req;
Yan, Zheng315f2402016-03-07 10:34:50 +0800139 int mask;
Sage Weil3c454cf2011-04-06 09:31:40 -0700140
141 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
142 USE_ANY_MDS);
143 if (IS_ERR(req))
144 return ERR_CAST(req);
145
Yan, Zheng315f2402016-03-07 10:34:50 +0800146 mask = CEPH_STAT_CAP_INODE;
147 if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
148 mask |= CEPH_CAP_XATTR_SHARED;
Yan, Zheng570df4e2017-11-15 17:39:40 +0800149 req->r_args.lookupino.mask = cpu_to_le32(mask);
Yan, Zheng315f2402016-03-07 10:34:50 +0800150
Sage Weil3c454cf2011-04-06 09:31:40 -0700151 req->r_ino1 = vino;
152 req->r_num_caps = 1;
153 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil45e3d3e2011-04-06 09:35:00 -0700154 inode = req->r_target_inode;
155 if (inode)
Sage Weil70b666c2011-05-27 09:24:26 -0700156 ihold(inode);
Sage Weil3c454cf2011-04-06 09:31:40 -0700157 ceph_mdsc_put_request(req);
Sage Weil3c454cf2011-04-06 09:31:40 -0700158 if (!inode)
Luis Henriques38862742019-03-21 10:20:09 +0000159 return err < 0 ? ERR_PTR(err) : ERR_PTR(-ESTALE);
Sage Weil3c454cf2011-04-06 09:31:40 -0700160 }
Yan, Zheng570df4e2017-11-15 17:39:40 +0800161 return inode;
162}
Sage Weila8e63b72009-10-06 11:31:13 -0700163
Yan, Zheng570df4e2017-11-15 17:39:40 +0800164struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino)
165{
166 struct inode *inode = __lookup_inode(sb, ino);
167 if (IS_ERR(inode))
168 return inode;
169 if (inode->i_nlink == 0) {
170 iput(inode);
171 return ERR_PTR(-ESTALE);
172 }
Luis Henriques38862742019-03-21 10:20:09 +0000173 return inode;
174}
175
176static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)
177{
Yan, Zheng570df4e2017-11-15 17:39:40 +0800178 struct inode *inode = __lookup_inode(sb, ino);
Luis Henriques878dabb2020-05-18 18:47:26 +0100179 int err;
180
Luis Henriques38862742019-03-21 10:20:09 +0000181 if (IS_ERR(inode))
182 return ERR_CAST(inode);
Luis Henriques878dabb2020-05-18 18:47:26 +0100183 /* We need LINK caps to reliably check i_nlink */
184 err = ceph_do_getattr(inode, CEPH_CAP_LINK_SHARED, false);
Jeff Layton1775c7d2021-03-26 09:21:53 -0400185 if (err) {
186 iput(inode);
Luis Henriques878dabb2020-05-18 18:47:26 +0100187 return ERR_PTR(err);
Jeff Layton1775c7d2021-03-26 09:21:53 -0400188 }
Luis Henriques878dabb2020-05-18 18:47:26 +0100189 /* -ESTALE if inode as been unlinked and no file is open */
190 if ((inode->i_nlink == 0) && (atomic_read(&inode->i_count) == 1)) {
Yan, Zheng570df4e2017-11-15 17:39:40 +0800191 iput(inode);
192 return ERR_PTR(-ESTALE);
193 }
Al Viroad5cb122016-10-28 22:05:13 -0400194 return d_obtain_alias(inode);
Sage Weila8e63b72009-10-06 11:31:13 -0700195}
196
Yan, Zheng570df4e2017-11-15 17:39:40 +0800197static struct dentry *__snapfh_to_dentry(struct super_block *sb,
198 struct ceph_nfs_snapfh *sfh,
199 bool want_parent)
200{
201 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
202 struct ceph_mds_request *req;
203 struct inode *inode;
204 struct ceph_vino vino;
205 int mask;
206 int err;
207 bool unlinked = false;
208
209 if (want_parent) {
210 vino.ino = sfh->parent_ino;
211 if (sfh->snapid == CEPH_SNAPDIR)
212 vino.snap = CEPH_NOSNAP;
213 else if (sfh->ino == sfh->parent_ino)
214 vino.snap = CEPH_SNAPDIR;
215 else
216 vino.snap = sfh->snapid;
217 } else {
218 vino.ino = sfh->ino;
219 vino.snap = sfh->snapid;
220 }
Jeff Laytond4f6b312021-04-01 13:55:11 -0400221
222 if (ceph_vino_is_reserved(vino))
223 return ERR_PTR(-ESTALE);
224
Yan, Zheng570df4e2017-11-15 17:39:40 +0800225 inode = ceph_find_inode(sb, vino);
226 if (inode)
227 return d_obtain_alias(inode);
228
229 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
230 USE_ANY_MDS);
231 if (IS_ERR(req))
232 return ERR_CAST(req);
233
234 mask = CEPH_STAT_CAP_INODE;
235 if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
236 mask |= CEPH_CAP_XATTR_SHARED;
237 req->r_args.lookupino.mask = cpu_to_le32(mask);
238 if (vino.snap < CEPH_NOSNAP) {
239 req->r_args.lookupino.snapid = cpu_to_le64(vino.snap);
240 if (!want_parent && sfh->ino != sfh->parent_ino) {
241 req->r_args.lookupino.parent =
242 cpu_to_le64(sfh->parent_ino);
243 req->r_args.lookupino.hash =
244 cpu_to_le32(sfh->hash);
245 }
246 }
247
248 req->r_ino1 = vino;
249 req->r_num_caps = 1;
250 err = ceph_mdsc_do_request(mdsc, NULL, req);
251 inode = req->r_target_inode;
252 if (inode) {
253 if (vino.snap == CEPH_SNAPDIR) {
254 if (inode->i_nlink == 0)
255 unlinked = true;
256 inode = ceph_get_snapdir(inode);
257 } else if (ceph_snap(inode) == vino.snap) {
258 ihold(inode);
259 } else {
260 /* mds does not support lookup snapped inode */
Jeff Layton3e10a152021-02-25 15:04:15 -0500261 inode = ERR_PTR(-EOPNOTSUPP);
Yan, Zheng570df4e2017-11-15 17:39:40 +0800262 }
Jeff Layton3e10a152021-02-25 15:04:15 -0500263 } else {
264 inode = ERR_PTR(-ESTALE);
Yan, Zheng570df4e2017-11-15 17:39:40 +0800265 }
266 ceph_mdsc_put_request(req);
267
268 if (want_parent) {
269 dout("snapfh_to_parent %llx.%llx\n err=%d\n",
270 vino.ino, vino.snap, err);
271 } else {
272 dout("snapfh_to_dentry %llx.%llx parent %llx hash %x err=%d",
273 vino.ino, vino.snap, sfh->parent_ino, sfh->hash, err);
274 }
Jeff Layton3e10a152021-02-25 15:04:15 -0500275 if (IS_ERR(inode))
276 return ERR_CAST(inode);
Yan, Zheng570df4e2017-11-15 17:39:40 +0800277 /* see comments in ceph_get_parent() */
278 return unlinked ? d_obtain_root(inode) : d_obtain_alias(inode);
279}
280
Sage Weila8e63b72009-10-06 11:31:13 -0700281/*
Yan, Zheng4f32b422014-03-01 18:05:41 +0800282 * convert regular fh to dentry
Sage Weila8e63b72009-10-06 11:31:13 -0700283 */
Yan, Zheng4f32b422014-03-01 18:05:41 +0800284static struct dentry *ceph_fh_to_dentry(struct super_block *sb,
285 struct fid *fid,
Sage Weila8e63b72009-10-06 11:31:13 -0700286 int fh_len, int fh_type)
287{
Yan, Zheng4f32b422014-03-01 18:05:41 +0800288 struct ceph_nfs_fh *fh = (void *)fid->raw;
289
Yan, Zheng570df4e2017-11-15 17:39:40 +0800290 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
291 struct ceph_nfs_snapfh *sfh = (void *)fid->raw;
292 return __snapfh_to_dentry(sb, sfh, false);
293 }
294
Yan, Zheng4f32b422014-03-01 18:05:41 +0800295 if (fh_type != FILEID_INO32_GEN &&
296 fh_type != FILEID_INO32_GEN_PARENT)
297 return NULL;
298 if (fh_len < sizeof(*fh) / 4)
299 return NULL;
300
301 dout("fh_to_dentry %llx\n", fh->ino);
302 return __fh_to_dentry(sb, fh->ino);
Sage Weila8e63b72009-10-06 11:31:13 -0700303}
304
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800305static struct dentry *__get_parent(struct super_block *sb,
306 struct dentry *child, u64 ino)
307{
308 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
309 struct ceph_mds_request *req;
310 struct inode *inode;
Yan, Zheng315f2402016-03-07 10:34:50 +0800311 int mask;
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800312 int err;
313
314 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPPARENT,
315 USE_ANY_MDS);
316 if (IS_ERR(req))
317 return ERR_CAST(req);
318
319 if (child) {
David Howells2b0143b2015-03-17 22:25:59 +0000320 req->r_inode = d_inode(child);
321 ihold(d_inode(child));
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800322 } else {
323 req->r_ino1 = (struct ceph_vino) {
324 .ino = ino,
325 .snap = CEPH_NOSNAP,
326 };
327 }
Yan, Zheng315f2402016-03-07 10:34:50 +0800328
329 mask = CEPH_STAT_CAP_INODE;
330 if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
331 mask |= CEPH_CAP_XATTR_SHARED;
332 req->r_args.getattr.mask = cpu_to_le32(mask);
333
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800334 req->r_num_caps = 1;
335 err = ceph_mdsc_do_request(mdsc, NULL, req);
Qiujun Huangc6d50292020-03-06 09:34:20 +0800336 if (err) {
337 ceph_mdsc_put_request(req);
338 return ERR_PTR(err);
339 }
340
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800341 inode = req->r_target_inode;
342 if (inode)
343 ihold(inode);
344 ceph_mdsc_put_request(req);
345 if (!inode)
346 return ERR_PTR(-ENOENT);
347
Al Viroad5cb122016-10-28 22:05:13 -0400348 return d_obtain_alias(inode);
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800349}
350
Fengguang Wue84be112014-04-09 14:11:31 +0800351static struct dentry *ceph_get_parent(struct dentry *child)
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800352{
Yan, Zheng570df4e2017-11-15 17:39:40 +0800353 struct inode *inode = d_inode(child);
354 struct dentry *dn;
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800355
Yan, Zheng570df4e2017-11-15 17:39:40 +0800356 if (ceph_snap(inode) != CEPH_NOSNAP) {
357 struct inode* dir;
358 bool unlinked = false;
359 /* do not support non-directory */
360 if (!d_is_dir(child)) {
361 dn = ERR_PTR(-EINVAL);
362 goto out;
363 }
364 dir = __lookup_inode(inode->i_sb, ceph_ino(inode));
365 if (IS_ERR(dir)) {
366 dn = ERR_CAST(dir);
367 goto out;
368 }
369 /* There can be multiple paths to access snapped inode.
370 * For simplicity, treat snapdir of head inode as parent */
371 if (ceph_snap(inode) != CEPH_SNAPDIR) {
372 struct inode *snapdir = ceph_get_snapdir(dir);
373 if (dir->i_nlink == 0)
374 unlinked = true;
375 iput(dir);
376 if (IS_ERR(snapdir)) {
377 dn = ERR_CAST(snapdir);
378 goto out;
379 }
380 dir = snapdir;
381 }
382 /* If directory has already been deleted, futher get_parent
383 * will fail. Do not mark snapdir dentry as disconnected,
384 * this prevent exportfs from doing futher get_parent. */
385 if (unlinked)
386 dn = d_obtain_root(dir);
387 else
388 dn = d_obtain_alias(dir);
389 } else {
390 dn = __get_parent(child->d_sb, child, 0);
391 }
392out:
393 dout("get_parent %p ino %llx.%llx err=%ld\n",
Hariprasad Kelam03af4392019-05-25 14:45:59 +0530394 child, ceph_vinop(inode), (long)PTR_ERR_OR_ZERO(dn));
Yan, Zheng570df4e2017-11-15 17:39:40 +0800395 return dn;
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800396}
397
Sage Weila8e63b72009-10-06 11:31:13 -0700398/*
Yan, Zheng8996f4f2014-03-01 23:09:05 +0800399 * convert regular fh to parent
Sage Weila8e63b72009-10-06 11:31:13 -0700400 */
401static struct dentry *ceph_fh_to_parent(struct super_block *sb,
Yan, Zheng8996f4f2014-03-01 23:09:05 +0800402 struct fid *fid,
Sage Weila8e63b72009-10-06 11:31:13 -0700403 int fh_len, int fh_type)
404{
405 struct ceph_nfs_confh *cfh = (void *)fid->raw;
Sage Weila8e63b72009-10-06 11:31:13 -0700406 struct dentry *dentry;
Sage Weila8e63b72009-10-06 11:31:13 -0700407
Yan, Zheng570df4e2017-11-15 17:39:40 +0800408 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
409 struct ceph_nfs_snapfh *sfh = (void *)fid->raw;
410 return __snapfh_to_dentry(sb, sfh, true);
411 }
412
Yan, Zheng8996f4f2014-03-01 23:09:05 +0800413 if (fh_type != FILEID_INO32_GEN_PARENT)
414 return NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -0700415 if (fh_len < sizeof(*cfh) / 4)
Yan, Zheng8996f4f2014-03-01 23:09:05 +0800416 return NULL;
Sage Weila8e63b72009-10-06 11:31:13 -0700417
Yan, Zheng8996f4f2014-03-01 23:09:05 +0800418 dout("fh_to_parent %llx\n", cfh->parent_ino);
419 dentry = __get_parent(sb, NULL, cfh->ino);
Al Virob42b90d2016-06-24 23:49:03 -0400420 if (unlikely(dentry == ERR_PTR(-ENOENT)))
Yan, Zheng8996f4f2014-03-01 23:09:05 +0800421 dentry = __fh_to_dentry(sb, cfh->parent_ino);
Sage Weila8e63b72009-10-06 11:31:13 -0700422 return dentry;
423}
424
Yan, Zheng570df4e2017-11-15 17:39:40 +0800425static int __get_snap_name(struct dentry *parent, char *name,
426 struct dentry *child)
427{
428 struct inode *inode = d_inode(child);
429 struct inode *dir = d_inode(parent);
430 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
431 struct ceph_mds_request *req = NULL;
432 char *last_name = NULL;
433 unsigned next_offset = 2;
434 int err = -EINVAL;
435
436 if (ceph_ino(inode) != ceph_ino(dir))
437 goto out;
438 if (ceph_snap(inode) == CEPH_SNAPDIR) {
439 if (ceph_snap(dir) == CEPH_NOSNAP) {
440 strcpy(name, fsc->mount_options->snapdir_name);
441 err = 0;
442 }
443 goto out;
444 }
445 if (ceph_snap(dir) != CEPH_SNAPDIR)
446 goto out;
447
448 while (1) {
449 struct ceph_mds_reply_info_parsed *rinfo;
450 struct ceph_mds_reply_dir_entry *rde;
451 int i;
452
453 req = ceph_mdsc_create_request(fsc->mdsc, CEPH_MDS_OP_LSSNAP,
454 USE_AUTH_MDS);
455 if (IS_ERR(req)) {
456 err = PTR_ERR(req);
457 req = NULL;
458 goto out;
459 }
460 err = ceph_alloc_readdir_reply_buffer(req, inode);
461 if (err)
462 goto out;
463
464 req->r_direct_mode = USE_AUTH_MDS;
465 req->r_readdir_offset = next_offset;
466 req->r_args.readdir.flags =
467 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
468 if (last_name) {
469 req->r_path2 = last_name;
470 last_name = NULL;
471 }
472
473 req->r_inode = dir;
474 ihold(dir);
475 req->r_dentry = dget(parent);
476
477 inode_lock(dir);
478 err = ceph_mdsc_do_request(fsc->mdsc, NULL, req);
479 inode_unlock(dir);
480
481 if (err < 0)
482 goto out;
483
Ilya Dryomov0ed26f32019-07-29 11:33:08 +0200484 rinfo = &req->r_reply_info;
485 for (i = 0; i < rinfo->dir_nr; i++) {
486 rde = rinfo->dir_entries + i;
487 BUG_ON(!rde->inode.in);
488 if (ceph_snap(inode) ==
489 le64_to_cpu(rde->inode.in->snapid)) {
490 memcpy(name, rde->name, rde->name_len);
491 name[rde->name_len] = '\0';
492 err = 0;
493 goto out;
494 }
495 }
Yan, Zheng570df4e2017-11-15 17:39:40 +0800496
Ilya Dryomov0ed26f32019-07-29 11:33:08 +0200497 if (rinfo->dir_end)
498 break;
Yan, Zheng570df4e2017-11-15 17:39:40 +0800499
Ilya Dryomov0ed26f32019-07-29 11:33:08 +0200500 BUG_ON(rinfo->dir_nr <= 0);
501 rde = rinfo->dir_entries + (rinfo->dir_nr - 1);
502 next_offset += rinfo->dir_nr;
503 last_name = kstrndup(rde->name, rde->name_len, GFP_KERNEL);
504 if (!last_name) {
505 err = -ENOMEM;
506 goto out;
507 }
Yan, Zheng570df4e2017-11-15 17:39:40 +0800508
Ilya Dryomov0ed26f32019-07-29 11:33:08 +0200509 ceph_mdsc_put_request(req);
510 req = NULL;
Yan, Zheng570df4e2017-11-15 17:39:40 +0800511 }
512 err = -ENOENT;
513out:
514 if (req)
515 ceph_mdsc_put_request(req);
516 kfree(last_name);
517 dout("get_snap_name %p ino %llx.%llx err=%d\n",
518 child, ceph_vinop(inode), err);
519 return err;
520}
521
Yan, Zheng19913b42014-03-06 16:40:32 +0800522static int ceph_get_name(struct dentry *parent, char *name,
523 struct dentry *child)
524{
525 struct ceph_mds_client *mdsc;
526 struct ceph_mds_request *req;
Yan, Zheng570df4e2017-11-15 17:39:40 +0800527 struct inode *inode = d_inode(child);
Yan, Zheng19913b42014-03-06 16:40:32 +0800528 int err;
529
Yan, Zheng570df4e2017-11-15 17:39:40 +0800530 if (ceph_snap(inode) != CEPH_NOSNAP)
531 return __get_snap_name(parent, name, child);
532
533 mdsc = ceph_inode_to_client(inode)->mdsc;
Yan, Zheng19913b42014-03-06 16:40:32 +0800534 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPNAME,
535 USE_ANY_MDS);
536 if (IS_ERR(req))
537 return PTR_ERR(req);
538
Al Viro59551022016-01-22 15:40:57 -0500539 inode_lock(d_inode(parent));
Yan, Zheng19913b42014-03-06 16:40:32 +0800540
Yan, Zheng570df4e2017-11-15 17:39:40 +0800541 req->r_inode = inode;
542 ihold(inode);
David Howells2b0143b2015-03-17 22:25:59 +0000543 req->r_ino2 = ceph_vino(d_inode(parent));
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500544 req->r_parent = d_inode(parent);
545 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Yan, Zheng19913b42014-03-06 16:40:32 +0800546 req->r_num_caps = 2;
547 err = ceph_mdsc_do_request(mdsc, NULL, req);
548
Al Viro59551022016-01-22 15:40:57 -0500549 inode_unlock(d_inode(parent));
Yan, Zheng19913b42014-03-06 16:40:32 +0800550
551 if (!err) {
552 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
553 memcpy(name, rinfo->dname, rinfo->dname_len);
554 name[rinfo->dname_len] = 0;
555 dout("get_name %p ino %llx.%llx name %s\n",
Yan, Zheng570df4e2017-11-15 17:39:40 +0800556 child, ceph_vinop(inode), name);
Yan, Zheng19913b42014-03-06 16:40:32 +0800557 } else {
558 dout("get_name %p ino %llx.%llx err %d\n",
Yan, Zheng570df4e2017-11-15 17:39:40 +0800559 child, ceph_vinop(inode), err);
Yan, Zheng19913b42014-03-06 16:40:32 +0800560 }
561
562 ceph_mdsc_put_request(req);
563 return err;
564}
565
Sage Weila8e63b72009-10-06 11:31:13 -0700566const struct export_operations ceph_export_ops = {
567 .encode_fh = ceph_encode_fh,
Sage Weila8e63b72009-10-06 11:31:13 -0700568 .fh_to_dentry = ceph_fh_to_dentry,
569 .fh_to_parent = ceph_fh_to_parent,
Yan, Zheng9017c2e2014-03-01 22:11:45 +0800570 .get_parent = ceph_get_parent,
Yan, Zheng19913b42014-03-06 16:40:32 +0800571 .get_name = ceph_get_name,
Sage Weila8e63b72009-10-06 11:31:13 -0700572};