blob: f1828af09912b610005752f3f44cb2ae32276d90 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weila8e63b72009-10-06 11:31:13 -07002
3#include <linux/exportfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Sage Weila8e63b72009-10-06 11:31:13 -07005#include <asm/unaligned.h>
6
7#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07008#include "mds_client.h"
Sage Weila8e63b72009-10-06 11:31:13 -07009
10/*
11 * NFS export support
12 *
13 * NFS re-export of a ceph mount is, at present, only semireliable.
14 * The basic issue is that the Ceph architectures doesn't lend itself
15 * well to generating filehandles that will remain valid forever.
16 *
17 * So, we do our best. If you're lucky, your inode will be in the
18 * client's cache. If it's not, and you have a connectable fh, then
19 * the MDS server may be able to find it for you. Otherwise, you get
20 * ESTALE.
21 *
22 * There are ways to this more reliable, but in the non-connectable fh
23 * case, we won't every work perfectly, and in the connectable case,
24 * some changes are needed on the MDS side to work better.
25 */
26
27/*
28 * Basic fh
29 */
30struct ceph_nfs_fh {
31 u64 ino;
32} __attribute__ ((packed));
33
34/*
35 * Larger 'connectable' fh that includes parent ino and name hash.
36 * Use this whenever possible, as it works more reliably.
37 */
38struct ceph_nfs_confh {
39 u64 ino, parent_ino;
40 u32 parent_name_hash;
41} __attribute__ ((packed));
42
43static int ceph_encode_fh(struct dentry *dentry, u32 *rawfh, int *max_len,
44 int connectable)
45{
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053046 int type;
Sage Weila8e63b72009-10-06 11:31:13 -070047 struct ceph_nfs_fh *fh = (void *)rawfh;
48 struct ceph_nfs_confh *cfh = (void *)rawfh;
49 struct dentry *parent = dentry->d_parent;
50 struct inode *inode = dentry->d_inode;
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053051 int connected_handle_length = sizeof(*cfh)/4;
52 int handle_length = sizeof(*fh)/4;
Sage Weila8e63b72009-10-06 11:31:13 -070053
54 /* don't re-export snaps */
55 if (ceph_snap(inode) != CEPH_NOSNAP)
56 return -EINVAL;
57
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053058 if (*max_len >= connected_handle_length) {
Sage Weila8e63b72009-10-06 11:31:13 -070059 dout("encode_fh %p connectable\n", dentry);
60 cfh->ino = ceph_ino(dentry->d_inode);
61 cfh->parent_ino = ceph_ino(parent->d_inode);
Sage Weil6c0f3af2010-11-16 11:14:34 -080062 cfh->parent_name_hash = ceph_dentry_hash(parent);
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053063 *max_len = connected_handle_length;
Sage Weila8e63b72009-10-06 11:31:13 -070064 type = 2;
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053065 } else if (*max_len >= handle_length) {
Aneesh Kumar K.Vbba0cd02010-10-05 16:03:42 +053066 if (connectable) {
67 *max_len = connected_handle_length;
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053068 return 255;
Aneesh Kumar K.Vbba0cd02010-10-05 16:03:42 +053069 }
Sage Weila8e63b72009-10-06 11:31:13 -070070 dout("encode_fh %p\n", dentry);
71 fh->ino = ceph_ino(dentry->d_inode);
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053072 *max_len = handle_length;
Sage Weila8e63b72009-10-06 11:31:13 -070073 type = 1;
74 } else {
Aneesh Kumar K.Vbba0cd02010-10-05 16:03:42 +053075 *max_len = handle_length;
Aneesh Kumar K.V92923dc2010-10-05 16:03:41 +053076 return 255;
Sage Weila8e63b72009-10-06 11:31:13 -070077 }
78 return type;
79}
80
81/*
82 * convert regular fh to dentry
83 *
84 * FIXME: we should try harder by querying the mds for the ino.
85 */
86static struct dentry *__fh_to_dentry(struct super_block *sb,
87 struct ceph_nfs_fh *fh)
88{
Sage Weil3c454cf2011-04-06 09:31:40 -070089 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
Sage Weila8e63b72009-10-06 11:31:13 -070090 struct inode *inode;
91 struct dentry *dentry;
92 struct ceph_vino vino;
93 int err;
94
95 dout("__fh_to_dentry %llx\n", fh->ino);
96 vino.ino = fh->ino;
97 vino.snap = CEPH_NOSNAP;
98 inode = ceph_find_inode(sb, vino);
Sage Weil3c454cf2011-04-06 09:31:40 -070099 if (!inode) {
100 struct ceph_mds_request *req;
101
102 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
103 USE_ANY_MDS);
104 if (IS_ERR(req))
105 return ERR_CAST(req);
106
107 req->r_ino1 = vino;
108 req->r_num_caps = 1;
109 err = ceph_mdsc_do_request(mdsc, NULL, req);
110 ceph_mdsc_put_request(req);
111 inode = ceph_find_inode(sb, vino);
112 if (!inode)
113 return ERR_PTR(-ESTALE);
114 }
Sage Weila8e63b72009-10-06 11:31:13 -0700115
116 dentry = d_obtain_alias(inode);
Dan Carpenter0d509c92010-04-21 12:31:13 +0200117 if (IS_ERR(dentry)) {
Sage Weila8e63b72009-10-06 11:31:13 -0700118 pr_err("fh_to_dentry %llx -- inode %p but ENOMEM\n",
119 fh->ino, inode);
120 iput(inode);
Dan Carpenter0d509c92010-04-21 12:31:13 +0200121 return dentry;
Sage Weila8e63b72009-10-06 11:31:13 -0700122 }
123 err = ceph_init_dentry(dentry);
124
125 if (err < 0) {
126 iput(inode);
127 return ERR_PTR(err);
128 }
129 dout("__fh_to_dentry %llx %p dentry %p\n", fh->ino, inode, dentry);
130 return dentry;
131}
132
133/*
134 * convert connectable fh to dentry
135 */
136static struct dentry *__cfh_to_dentry(struct super_block *sb,
137 struct ceph_nfs_confh *cfh)
138{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700139 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
Sage Weila8e63b72009-10-06 11:31:13 -0700140 struct inode *inode;
141 struct dentry *dentry;
142 struct ceph_vino vino;
143 int err;
144
145 dout("__cfh_to_dentry %llx (%llx/%x)\n",
146 cfh->ino, cfh->parent_ino, cfh->parent_name_hash);
147
148 vino.ino = cfh->ino;
149 vino.snap = CEPH_NOSNAP;
150 inode = ceph_find_inode(sb, vino);
151 if (!inode) {
152 struct ceph_mds_request *req;
153
154 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPHASH,
155 USE_ANY_MDS);
156 if (IS_ERR(req))
Julia Lawall7e34bc52010-05-22 12:01:14 +0200157 return ERR_CAST(req);
Sage Weila8e63b72009-10-06 11:31:13 -0700158
159 req->r_ino1 = vino;
160 req->r_ino2.ino = cfh->parent_ino;
161 req->r_ino2.snap = CEPH_NOSNAP;
162 req->r_path2 = kmalloc(16, GFP_NOFS);
163 snprintf(req->r_path2, 16, "%d", cfh->parent_name_hash);
164 req->r_num_caps = 1;
165 err = ceph_mdsc_do_request(mdsc, NULL, req);
166 ceph_mdsc_put_request(req);
167 inode = ceph_find_inode(sb, vino);
168 if (!inode)
169 return ERR_PTR(err ? err : -ESTALE);
170 }
171
172 dentry = d_obtain_alias(inode);
Dan Carpenter0d509c92010-04-21 12:31:13 +0200173 if (IS_ERR(dentry)) {
Sage Weila8e63b72009-10-06 11:31:13 -0700174 pr_err("cfh_to_dentry %llx -- inode %p but ENOMEM\n",
175 cfh->ino, inode);
176 iput(inode);
Dan Carpenter0d509c92010-04-21 12:31:13 +0200177 return dentry;
Sage Weila8e63b72009-10-06 11:31:13 -0700178 }
179 err = ceph_init_dentry(dentry);
180 if (err < 0) {
181 iput(inode);
182 return ERR_PTR(err);
183 }
184 dout("__cfh_to_dentry %llx %p dentry %p\n", cfh->ino, inode, dentry);
185 return dentry;
186}
187
188static struct dentry *ceph_fh_to_dentry(struct super_block *sb, struct fid *fid,
189 int fh_len, int fh_type)
190{
191 if (fh_type == 1)
192 return __fh_to_dentry(sb, (struct ceph_nfs_fh *)fid->raw);
193 else
194 return __cfh_to_dentry(sb, (struct ceph_nfs_confh *)fid->raw);
195}
196
197/*
198 * get parent, if possible.
199 *
200 * FIXME: we could do better by querying the mds to discover the
201 * parent.
202 */
203static struct dentry *ceph_fh_to_parent(struct super_block *sb,
204 struct fid *fid,
205 int fh_len, int fh_type)
206{
207 struct ceph_nfs_confh *cfh = (void *)fid->raw;
208 struct ceph_vino vino;
209 struct inode *inode;
210 struct dentry *dentry;
211 int err;
212
213 if (fh_type == 1)
214 return ERR_PTR(-ESTALE);
215
216 pr_debug("fh_to_parent %llx/%d\n", cfh->parent_ino,
217 cfh->parent_name_hash);
218
219 vino.ino = cfh->ino;
220 vino.snap = CEPH_NOSNAP;
221 inode = ceph_find_inode(sb, vino);
222 if (!inode)
223 return ERR_PTR(-ESTALE);
224
225 dentry = d_obtain_alias(inode);
Dan Carpenter0d509c92010-04-21 12:31:13 +0200226 if (IS_ERR(dentry)) {
Sage Weila8e63b72009-10-06 11:31:13 -0700227 pr_err("fh_to_parent %llx -- inode %p but ENOMEM\n",
228 cfh->ino, inode);
229 iput(inode);
Dan Carpenter0d509c92010-04-21 12:31:13 +0200230 return dentry;
Sage Weila8e63b72009-10-06 11:31:13 -0700231 }
232 err = ceph_init_dentry(dentry);
233 if (err < 0) {
234 iput(inode);
235 return ERR_PTR(err);
236 }
237 dout("fh_to_parent %llx %p dentry %p\n", cfh->ino, inode, dentry);
238 return dentry;
239}
240
241const struct export_operations ceph_export_ops = {
242 .encode_fh = ceph_encode_fh,
243 .fh_to_dentry = ceph_fh_to_dentry,
244 .fh_to_parent = ceph_fh_to_parent,
245};