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