1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/exportfs.h>
5 #include <linux/slab.h>
6 #include <asm/unaligned.h>
9 #include "mds_client.h"
16 } __attribute__ ((packed));
19 * Larger fh that includes parent ino.
21 struct ceph_nfs_confh {
23 } __attribute__ ((packed));
26 * fh for snapped inode
28 struct ceph_nfs_snapfh {
33 } __attribute__ ((packed));
35 static int ceph_encode_snapfh(struct inode *inode, u32 *rawfh, int *max_len,
36 struct inode *parent_inode)
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);
43 bool no_parent = true;
45 if (*max_len < snap_handle_length) {
46 *max_len = snap_handle_length;
52 if (snapid != CEPH_SNAPDIR) {
54 struct dentry *dentry = d_find_alias(inode);
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);
70 if (!S_ISDIR(inode->i_mode))
72 sfh->parent_ino = sfh->ino;
75 sfh->ino = ceph_ino(inode);
78 *max_len = snap_handle_length;
79 ret = FILEID_BTRFS_WITH_PARENT;
81 dout("encode_snapfh %llx.%llx ret=%d\n", ceph_vinop(inode), ret);
85 static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
86 struct inode *parent_inode)
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;
94 if (ceph_snap(inode) != CEPH_NOSNAP)
95 return ceph_encode_snapfh(inode, rawfh, max_len, parent_inode);
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;
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;
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;
123 static struct inode *__lookup_inode(struct super_block *sb, u64 ino)
125 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
127 struct ceph_vino vino;
131 vino.snap = CEPH_NOSNAP;
133 if (ceph_vino_is_reserved(vino))
134 return ERR_PTR(-ESTALE);
136 inode = ceph_find_inode(sb, vino);
138 struct ceph_mds_request *req;
141 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
144 return ERR_CAST(req);
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);
153 err = ceph_mdsc_do_request(mdsc, NULL, req);
154 inode = req->r_target_inode;
157 ceph_mdsc_put_request(req);
159 return err < 0 ? ERR_PTR(err) : ERR_PTR(-ESTALE);
161 if (ceph_inode_is_shutdown(inode)) {
163 return ERR_PTR(-ESTALE);
169 struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino)
171 struct inode *inode = __lookup_inode(sb, ino);
174 if (inode->i_nlink == 0) {
176 return ERR_PTR(-ESTALE);
181 static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)
183 struct inode *inode = __lookup_inode(sb, ino);
184 struct ceph_inode_info *ci = ceph_inode(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);
195 /* -ESTALE if inode as been unlinked and no file is open */
196 if ((inode->i_nlink == 0) && !__ceph_is_file_opened(ci)) {
198 return ERR_PTR(-ESTALE);
200 return d_obtain_alias(inode);
203 static struct dentry *__snapfh_to_dentry(struct super_block *sb,
204 struct ceph_nfs_snapfh *sfh,
207 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
208 struct ceph_mds_request *req;
210 struct ceph_vino vino;
213 bool unlinked = false;
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;
222 vino.snap = sfh->snapid;
225 vino.snap = sfh->snapid;
228 if (ceph_vino_is_reserved(vino))
229 return ERR_PTR(-ESTALE);
231 inode = ceph_find_inode(sb, vino);
233 if (ceph_inode_is_shutdown(inode)) {
235 return ERR_PTR(-ESTALE);
237 return d_obtain_alias(inode);
240 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
243 return ERR_CAST(req);
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);
261 err = ceph_mdsc_do_request(mdsc, NULL, req);
262 inode = req->r_target_inode;
264 if (vino.snap == CEPH_SNAPDIR) {
265 if (inode->i_nlink == 0)
267 inode = ceph_get_snapdir(inode);
268 } else if (ceph_snap(inode) == vino.snap) {
271 /* mds does not support lookup snapped inode */
272 inode = ERR_PTR(-EOPNOTSUPP);
275 inode = ERR_PTR(-ESTALE);
277 ceph_mdsc_put_request(req);
280 dout("snapfh_to_parent %llx.%llx\n err=%d\n",
281 vino.ino, vino.snap, err);
283 dout("snapfh_to_dentry %llx.%llx parent %llx hash %x err=%d",
284 vino.ino, vino.snap, sfh->parent_ino, sfh->hash, err);
287 return ERR_CAST(inode);
288 /* see comments in ceph_get_parent() */
289 return unlinked ? d_obtain_root(inode) : d_obtain_alias(inode);
293 * convert regular fh to dentry
295 static struct dentry *ceph_fh_to_dentry(struct super_block *sb,
297 int fh_len, int fh_type)
299 struct ceph_nfs_fh *fh = (void *)fid->raw;
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);
306 if (fh_type != FILEID_INO32_GEN &&
307 fh_type != FILEID_INO32_GEN_PARENT)
309 if (fh_len < sizeof(*fh) / 4)
312 dout("fh_to_dentry %llx\n", fh->ino);
313 return __fh_to_dentry(sb, fh->ino);
316 static struct dentry *__get_parent(struct super_block *sb,
317 struct dentry *child, u64 ino)
319 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
320 struct ceph_mds_request *req;
325 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPPARENT,
328 return ERR_CAST(req);
331 req->r_inode = d_inode(child);
332 ihold(d_inode(child));
334 req->r_ino1 = (struct ceph_vino) {
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);
346 err = ceph_mdsc_do_request(mdsc, NULL, req);
348 ceph_mdsc_put_request(req);
352 inode = req->r_target_inode;
355 ceph_mdsc_put_request(req);
357 return ERR_PTR(-ENOENT);
359 return d_obtain_alias(inode);
362 static struct dentry *ceph_get_parent(struct dentry *child)
364 struct inode *inode = d_inode(child);
367 if (ceph_snap(inode) != CEPH_NOSNAP) {
369 bool unlinked = false;
370 /* do not support non-directory */
371 if (!d_is_dir(child)) {
372 dn = ERR_PTR(-EINVAL);
375 dir = __lookup_inode(inode->i_sb, ceph_ino(inode));
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)
387 if (IS_ERR(snapdir)) {
388 dn = ERR_CAST(snapdir);
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. */
397 dn = d_obtain_root(dir);
399 dn = d_obtain_alias(dir);
401 dn = __get_parent(child->d_sb, child, 0);
404 dout("get_parent %p ino %llx.%llx err=%ld\n",
405 child, ceph_vinop(inode), (long)PTR_ERR_OR_ZERO(dn));
410 * convert regular fh to parent
412 static struct dentry *ceph_fh_to_parent(struct super_block *sb,
414 int fh_len, int fh_type)
416 struct ceph_nfs_confh *cfh = (void *)fid->raw;
417 struct dentry *dentry;
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);
424 if (fh_type != FILEID_INO32_GEN_PARENT)
426 if (fh_len < sizeof(*cfh) / 4)
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);
436 static int __get_snap_name(struct dentry *parent, char *name,
437 struct dentry *child)
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;
447 if (ceph_ino(inode) != ceph_ino(dir))
449 if (ceph_snap(inode) == CEPH_SNAPDIR) {
450 if (ceph_snap(dir) == CEPH_NOSNAP) {
451 strcpy(name, fsc->mount_options->snapdir_name);
456 if (ceph_snap(dir) != CEPH_SNAPDIR)
460 struct ceph_mds_reply_info_parsed *rinfo;
461 struct ceph_mds_reply_dir_entry *rde;
464 req = ceph_mdsc_create_request(fsc->mdsc, CEPH_MDS_OP_LSSNAP,
471 err = ceph_alloc_readdir_reply_buffer(req, inode);
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);
480 req->r_path2 = last_name;
486 req->r_dentry = dget(parent);
489 err = ceph_mdsc_do_request(fsc->mdsc, NULL, req);
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';
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);
520 ceph_mdsc_put_request(req);
526 ceph_mdsc_put_request(req);
528 dout("get_snap_name %p ino %llx.%llx err=%d\n",
529 child, ceph_vinop(inode), err);
533 static int ceph_get_name(struct dentry *parent, char *name,
534 struct dentry *child)
536 struct ceph_mds_client *mdsc;
537 struct ceph_mds_request *req;
538 struct inode *inode = d_inode(child);
541 if (ceph_snap(inode) != CEPH_NOSNAP)
542 return __get_snap_name(parent, name, child);
544 mdsc = ceph_inode_to_client(inode)->mdsc;
545 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPNAME,
550 inode_lock(d_inode(parent));
552 req->r_inode = 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);
559 err = ceph_mdsc_do_request(mdsc, NULL, req);
561 inode_unlock(d_inode(parent));
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);
570 dout("get_name %p ino %llx.%llx err %d\n",
571 child, ceph_vinop(inode), err);
574 ceph_mdsc_put_request(req);
578 const 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,