1 #include "ceph_debug.h"
3 #include <linux/exportfs.h>
4 #include <asm/unaligned.h>
11 * NFS re-export of a ceph mount is, at present, only semireliable.
12 * The basic issue is that the Ceph architectures doesn't lend itself
13 * well to generating filehandles that will remain valid forever.
15 * So, we do our best. If you're lucky, your inode will be in the
16 * client's cache. If it's not, and you have a connectable fh, then
17 * the MDS server may be able to find it for you. Otherwise, you get
20 * There are ways to this more reliable, but in the non-connectable fh
21 * case, we won't every work perfectly, and in the connectable case,
22 * some changes are needed on the MDS side to work better.
30 } __attribute__ ((packed));
33 * Larger 'connectable' fh that includes parent ino and name hash.
34 * Use this whenever possible, as it works more reliably.
36 struct ceph_nfs_confh {
39 } __attribute__ ((packed));
41 static int ceph_encode_fh(struct dentry *dentry, u32 *rawfh, int *max_len,
44 struct ceph_nfs_fh *fh = (void *)rawfh;
45 struct ceph_nfs_confh *cfh = (void *)rawfh;
46 struct dentry *parent = dentry->d_parent;
47 struct inode *inode = dentry->d_inode;
50 /* don't re-export snaps */
51 if (ceph_snap(inode) != CEPH_NOSNAP)
54 if (*max_len >= sizeof(*cfh)) {
55 dout("encode_fh %p connectable\n", dentry);
56 cfh->ino = ceph_ino(dentry->d_inode);
57 cfh->parent_ino = ceph_ino(parent->d_inode);
58 cfh->parent_name_hash = parent->d_name.hash;
59 *max_len = sizeof(*cfh);
61 } else if (*max_len > sizeof(*fh)) {
64 dout("encode_fh %p\n", dentry);
65 fh->ino = ceph_ino(dentry->d_inode);
66 *max_len = sizeof(*fh);
75 * convert regular fh to dentry
77 * FIXME: we should try harder by querying the mds for the ino.
79 static struct dentry *__fh_to_dentry(struct super_block *sb,
80 struct ceph_nfs_fh *fh)
83 struct dentry *dentry;
84 struct ceph_vino vino;
87 dout("__fh_to_dentry %llx\n", fh->ino);
89 vino.snap = CEPH_NOSNAP;
90 inode = ceph_find_inode(sb, vino);
92 return ERR_PTR(-ESTALE);
94 dentry = d_obtain_alias(inode);
96 pr_err("fh_to_dentry %llx -- inode %p but ENOMEM\n",
99 return ERR_PTR(-ENOMEM);
101 err = ceph_init_dentry(dentry);
107 dout("__fh_to_dentry %llx %p dentry %p\n", fh->ino, inode, dentry);
112 * convert connectable fh to dentry
114 static struct dentry *__cfh_to_dentry(struct super_block *sb,
115 struct ceph_nfs_confh *cfh)
117 struct ceph_mds_client *mdsc = &ceph_client(sb)->mdsc;
119 struct dentry *dentry;
120 struct ceph_vino vino;
123 dout("__cfh_to_dentry %llx (%llx/%x)\n",
124 cfh->ino, cfh->parent_ino, cfh->parent_name_hash);
127 vino.snap = CEPH_NOSNAP;
128 inode = ceph_find_inode(sb, vino);
130 struct ceph_mds_request *req;
132 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPHASH,
135 return ERR_PTR(PTR_ERR(req));
138 req->r_ino2.ino = cfh->parent_ino;
139 req->r_ino2.snap = CEPH_NOSNAP;
140 req->r_path2 = kmalloc(16, GFP_NOFS);
141 snprintf(req->r_path2, 16, "%d", cfh->parent_name_hash);
143 err = ceph_mdsc_do_request(mdsc, NULL, req);
144 ceph_mdsc_put_request(req);
145 inode = ceph_find_inode(sb, vino);
147 return ERR_PTR(err ? err : -ESTALE);
150 dentry = d_obtain_alias(inode);
152 pr_err("cfh_to_dentry %llx -- inode %p but ENOMEM\n",
155 return ERR_PTR(-ENOMEM);
157 err = ceph_init_dentry(dentry);
162 dout("__cfh_to_dentry %llx %p dentry %p\n", cfh->ino, inode, dentry);
166 static struct dentry *ceph_fh_to_dentry(struct super_block *sb, struct fid *fid,
167 int fh_len, int fh_type)
170 return __fh_to_dentry(sb, (struct ceph_nfs_fh *)fid->raw);
172 return __cfh_to_dentry(sb, (struct ceph_nfs_confh *)fid->raw);
176 * get parent, if possible.
178 * FIXME: we could do better by querying the mds to discover the
181 static struct dentry *ceph_fh_to_parent(struct super_block *sb,
183 int fh_len, int fh_type)
185 struct ceph_nfs_confh *cfh = (void *)fid->raw;
186 struct ceph_vino vino;
188 struct dentry *dentry;
192 return ERR_PTR(-ESTALE);
194 pr_debug("fh_to_parent %llx/%d\n", cfh->parent_ino,
195 cfh->parent_name_hash);
198 vino.snap = CEPH_NOSNAP;
199 inode = ceph_find_inode(sb, vino);
201 return ERR_PTR(-ESTALE);
203 dentry = d_obtain_alias(inode);
205 pr_err("fh_to_parent %llx -- inode %p but ENOMEM\n",
208 return ERR_PTR(-ENOMEM);
210 err = ceph_init_dentry(dentry);
215 dout("fh_to_parent %llx %p dentry %p\n", cfh->ino, inode, dentry);
219 const struct export_operations ceph_export_ops = {
220 .encode_fh = ceph_encode_fh,
221 .fh_to_dentry = ceph_fh_to_dentry,
222 .fh_to_parent = ceph_fh_to_parent,