1 #include "ceph_debug.h"
3 #include <linux/exportfs.h>
4 #include <linux/slab.h>
5 #include <asm/unaligned.h>
12 * NFS re-export of a ceph mount is, at present, only semireliable.
13 * The basic issue is that the Ceph architectures doesn't lend itself
14 * well to generating filehandles that will remain valid forever.
16 * So, we do our best. If you're lucky, your inode will be in the
17 * client's cache. If it's not, and you have a connectable fh, then
18 * the MDS server may be able to find it for you. Otherwise, you get
21 * There are ways to this more reliable, but in the non-connectable fh
22 * case, we won't every work perfectly, and in the connectable case,
23 * some changes are needed on the MDS side to work better.
31 } __attribute__ ((packed
));
34 * Larger 'connectable' fh that includes parent ino and name hash.
35 * Use this whenever possible, as it works more reliably.
37 struct ceph_nfs_confh
{
40 } __attribute__ ((packed
));
42 static int ceph_encode_fh(struct dentry
*dentry
, u32
*rawfh
, int *max_len
,
46 struct ceph_nfs_fh
*fh
= (void *)rawfh
;
47 struct ceph_nfs_confh
*cfh
= (void *)rawfh
;
48 struct dentry
*parent
= dentry
->d_parent
;
49 struct inode
*inode
= dentry
->d_inode
;
50 int connected_handle_length
= sizeof(*cfh
)/4;
51 int handle_length
= sizeof(*fh
)/4;
53 /* don't re-export snaps */
54 if (ceph_snap(inode
) != CEPH_NOSNAP
)
57 if (*max_len
>= connected_handle_length
) {
58 dout("encode_fh %p connectable\n", dentry
);
59 cfh
->ino
= ceph_ino(dentry
->d_inode
);
60 cfh
->parent_ino
= ceph_ino(parent
->d_inode
);
61 cfh
->parent_name_hash
= parent
->d_name
.hash
;
62 *max_len
= connected_handle_length
;
64 } else if (*max_len
>= handle_length
) {
66 *max_len
= connected_handle_length
;
69 dout("encode_fh %p\n", dentry
);
70 fh
->ino
= ceph_ino(dentry
->d_inode
);
71 *max_len
= handle_length
;
74 *max_len
= handle_length
;
80 static struct dentry
*__fh_to_dentry(struct super_block
*sb
,
81 struct ceph_nfs_fh
*fh
)
84 struct dentry
*dentry
;
85 struct ceph_vino vino
;
88 dout("__fh_to_dentry %llx\n", fh
->ino
);
90 vino
.snap
= CEPH_NOSNAP
;
91 inode
= ceph_find_inode(sb
, vino
);
93 return ERR_PTR(-ESTALE
);
95 dentry
= d_obtain_alias(inode
);
97 pr_err("fh_to_dentry %llx -- inode %p but ENOMEM\n",
102 err
= ceph_init_dentry(dentry
);
108 dout("__fh_to_dentry %llx %p dentry %p\n", fh
->ino
, inode
, dentry
);
113 * convert connectable fh to dentry
115 static struct dentry
*__cfh_to_dentry(struct super_block
*sb
,
116 struct ceph_nfs_confh
*cfh
)
118 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(sb
)->mdsc
;
120 struct dentry
*dentry
;
121 struct ceph_vino vino
;
124 dout("__cfh_to_dentry %llx (%llx/%x)\n",
125 cfh
->ino
, cfh
->parent_ino
, cfh
->parent_name_hash
);
128 vino
.snap
= CEPH_NOSNAP
;
129 inode
= ceph_find_inode(sb
, vino
);
131 struct ceph_mds_request
*req
;
133 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_LOOKUPHASH
,
136 return ERR_CAST(req
);
139 req
->r_ino2
.ino
= cfh
->parent_ino
;
140 req
->r_ino2
.snap
= CEPH_NOSNAP
;
141 req
->r_path2
= kmalloc(16, GFP_NOFS
);
142 snprintf(req
->r_path2
, 16, "%d", cfh
->parent_name_hash
);
144 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
145 ceph_mdsc_put_request(req
);
146 inode
= ceph_find_inode(sb
, vino
);
148 return ERR_PTR(err
? err
: -ESTALE
);
151 dentry
= d_obtain_alias(inode
);
152 if (IS_ERR(dentry
)) {
153 pr_err("cfh_to_dentry %llx -- inode %p but ENOMEM\n",
158 err
= ceph_init_dentry(dentry
);
163 dout("__cfh_to_dentry %llx %p dentry %p\n", cfh
->ino
, inode
, dentry
);
167 static struct dentry
*ceph_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
168 int fh_len
, int fh_type
)
171 return __fh_to_dentry(sb
, (struct ceph_nfs_fh
*)fid
->raw
);
173 return __cfh_to_dentry(sb
, (struct ceph_nfs_confh
*)fid
->raw
);
176 static struct dentry
*ceph_fh_to_parent(struct super_block
*sb
,
178 int fh_len
, int fh_type
)
180 struct ceph_nfs_confh
*cfh
= (void *)fid
->raw
;
181 struct ceph_vino vino
;
183 struct dentry
*dentry
;
187 return ERR_PTR(-ESTALE
);
189 pr_debug("fh_to_parent %llx/%d\n", cfh
->parent_ino
,
190 cfh
->parent_name_hash
);
193 vino
.snap
= CEPH_NOSNAP
;
194 inode
= ceph_find_inode(sb
, vino
);
196 return ERR_PTR(-ESTALE
);
198 dentry
= d_obtain_alias(inode
);
199 if (IS_ERR(dentry
)) {
200 pr_err("fh_to_parent %llx -- inode %p but ENOMEM\n",
205 err
= ceph_init_dentry(dentry
);
210 dout("fh_to_parent %llx %p dentry %p\n", cfh
->ino
, inode
, dentry
);
214 const struct export_operations ceph_export_ops
= {
215 .encode_fh
= ceph_encode_fh
,
216 .fh_to_dentry
= ceph_fh_to_dentry
,
217 .fh_to_parent
= ceph_fh_to_parent
,