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 = d_inode(child);
140		ihold(d_inode(child));
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(d_inode(child)) : ino,
168	     dentry, ceph_vinop(inode));
169	return dentry;
170}
171
172static struct dentry *ceph_get_parent(struct dentry *child)
173{
174	/* don't re-export snaps */
175	if (ceph_snap(d_inode(child)) != CEPH_NOSNAP)
176		return ERR_PTR(-EINVAL);
177
178	dout("get_parent %p ino %llx.%llx\n",
179	     child, ceph_vinop(d_inode(child)));
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(d_inode(child))->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(&d_inode(parent)->i_mutex);
219
220	req->r_inode = d_inode(child);
221	ihold(d_inode(child));
222	req->r_ino2 = ceph_vino(d_inode(parent));
223	req->r_locked_dir = d_inode(parent);
224	req->r_num_caps = 2;
225	err = ceph_mdsc_do_request(mdsc, NULL, req);
226
227	mutex_unlock(&d_inode(parent)->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(d_inode(child)), name);
235	} else {
236		dout("get_name %p ino %llx.%llx err %d\n",
237		     child, ceph_vinop(d_inode(child)), 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};
251