Linux Audio

Check our new training course

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