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"
17 } __attribute__ ((packed));
20 * Larger fh that includes parent ino.
22 struct ceph_nfs_confh {
24 } __attribute__ ((packed));
27 * fh for snapped inode
29 struct ceph_nfs_snapfh {
34 } __attribute__ ((packed));
36 static int ceph_encode_snapfh(struct inode *inode, u32 *rawfh, int *max_len,
37 struct inode *parent_inode)
39 static const int snap_handle_length =
40 sizeof(struct ceph_nfs_snapfh) >> 2;
41 struct ceph_nfs_snapfh *sfh = (void *)rawfh;
42 u64 snapid = ceph_snap(inode);
44 bool no_parent = true;
46 if (*max_len < snap_handle_length) {
47 *max_len = snap_handle_length;
53 if (snapid != CEPH_SNAPDIR) {
55 struct dentry *dentry = d_find_alias(inode);
60 dir = d_inode_rcu(dentry->d_parent);
61 if (ceph_snap(dir) != CEPH_SNAPDIR) {
62 sfh->parent_ino = ceph_ino(dir);
63 sfh->hash = ceph_dentry_hash(dir, dentry);
71 if (!S_ISDIR(inode->i_mode))
73 sfh->parent_ino = sfh->ino;
76 sfh->ino = ceph_ino(inode);
79 *max_len = snap_handle_length;
80 ret = FILEID_BTRFS_WITH_PARENT;
82 dout("encode_snapfh %llx.%llx ret=%d\n", ceph_vinop(inode), ret);
86 static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
87 struct inode *parent_inode)
89 static const int handle_length =
90 sizeof(struct ceph_nfs_fh) >> 2;
91 static const int connected_handle_length =
92 sizeof(struct ceph_nfs_confh) >> 2;
95 if (ceph_snap(inode) != CEPH_NOSNAP)
96 return ceph_encode_snapfh(inode, rawfh, max_len, parent_inode);
98 if (parent_inode && (*max_len < connected_handle_length)) {
99 *max_len = connected_handle_length;
100 return FILEID_INVALID;
101 } else if (*max_len < handle_length) {
102 *max_len = handle_length;
103 return FILEID_INVALID;
107 struct ceph_nfs_confh *cfh = (void *)rawfh;
108 dout("encode_fh %llx with parent %llx\n",
109 ceph_ino(inode), ceph_ino(parent_inode));
110 cfh->ino = ceph_ino(inode);
111 cfh->parent_ino = ceph_ino(parent_inode);
112 *max_len = connected_handle_length;
113 type = FILEID_INO32_GEN_PARENT;
115 struct ceph_nfs_fh *fh = (void *)rawfh;
116 dout("encode_fh %llx\n", ceph_ino(inode));
117 fh->ino = ceph_ino(inode);
118 *max_len = handle_length;
119 type = FILEID_INO32_GEN;
124 static struct inode *__lookup_inode(struct super_block *sb, u64 ino)
126 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
128 struct ceph_vino vino;
132 vino.snap = CEPH_NOSNAP;
134 if (ceph_vino_is_reserved(vino))
135 return ERR_PTR(-ESTALE);
137 inode = ceph_find_inode(sb, vino);
139 struct ceph_mds_request *req;
142 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
145 return ERR_CAST(req);
147 mask = CEPH_STAT_CAP_INODE;
148 if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
149 mask |= CEPH_CAP_XATTR_SHARED;
150 req->r_args.lookupino.mask = cpu_to_le32(mask);
154 err = ceph_mdsc_do_request(mdsc, NULL, req);
155 inode = req->r_target_inode;
158 ceph_mdsc_put_request(req);
160 return err < 0 ? ERR_PTR(err) : ERR_PTR(-ESTALE);
162 if (ceph_inode_is_shutdown(inode)) {
164 return ERR_PTR(-ESTALE);
170 struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino)
172 struct inode *inode = __lookup_inode(sb, ino);
175 if (inode->i_nlink == 0) {
177 return ERR_PTR(-ESTALE);
182 static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)
184 struct inode *inode = __lookup_inode(sb, ino);
185 struct ceph_inode_info *ci = ceph_inode(inode);
189 return ERR_CAST(inode);
190 /* We need LINK caps to reliably check i_nlink */
191 err = ceph_do_getattr(inode, CEPH_CAP_LINK_SHARED, false);
196 /* -ESTALE if inode as been unlinked and no file is open */
197 if ((inode->i_nlink == 0) && !__ceph_is_file_opened(ci)) {
199 return ERR_PTR(-ESTALE);
201 return d_obtain_alias(inode);
204 static struct dentry *__snapfh_to_dentry(struct super_block *sb,
205 struct ceph_nfs_snapfh *sfh,
208 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
209 struct ceph_mds_request *req;
211 struct ceph_vino vino;
214 bool unlinked = false;
217 vino.ino = sfh->parent_ino;
218 if (sfh->snapid == CEPH_SNAPDIR)
219 vino.snap = CEPH_NOSNAP;
220 else if (sfh->ino == sfh->parent_ino)
221 vino.snap = CEPH_SNAPDIR;
223 vino.snap = sfh->snapid;
226 vino.snap = sfh->snapid;
229 if (ceph_vino_is_reserved(vino))
230 return ERR_PTR(-ESTALE);
232 inode = ceph_find_inode(sb, vino);
234 if (ceph_inode_is_shutdown(inode)) {
236 return ERR_PTR(-ESTALE);
238 return d_obtain_alias(inode);
241 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
244 return ERR_CAST(req);
246 mask = CEPH_STAT_CAP_INODE;
247 if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
248 mask |= CEPH_CAP_XATTR_SHARED;
249 req->r_args.lookupino.mask = cpu_to_le32(mask);
250 if (vino.snap < CEPH_NOSNAP) {
251 req->r_args.lookupino.snapid = cpu_to_le64(vino.snap);
252 if (!want_parent && sfh->ino != sfh->parent_ino) {
253 req->r_args.lookupino.parent =
254 cpu_to_le64(sfh->parent_ino);
255 req->r_args.lookupino.hash =
256 cpu_to_le32(sfh->hash);
262 err = ceph_mdsc_do_request(mdsc, NULL, req);
263 inode = req->r_target_inode;
265 if (vino.snap == CEPH_SNAPDIR) {
266 if (inode->i_nlink == 0)
268 inode = ceph_get_snapdir(inode);
269 } else if (ceph_snap(inode) == vino.snap) {
272 /* mds does not support lookup snapped inode */
273 inode = ERR_PTR(-EOPNOTSUPP);
276 inode = ERR_PTR(-ESTALE);
278 ceph_mdsc_put_request(req);
281 dout("snapfh_to_parent %llx.%llx\n err=%d\n",
282 vino.ino, vino.snap, err);
284 dout("snapfh_to_dentry %llx.%llx parent %llx hash %x err=%d",
285 vino.ino, vino.snap, sfh->parent_ino, sfh->hash, err);
288 return ERR_CAST(inode);
289 /* see comments in ceph_get_parent() */
290 return unlinked ? d_obtain_root(inode) : d_obtain_alias(inode);
294 * convert regular fh to dentry
296 static struct dentry *ceph_fh_to_dentry(struct super_block *sb,
298 int fh_len, int fh_type)
300 struct ceph_nfs_fh *fh = (void *)fid->raw;
302 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
303 struct ceph_nfs_snapfh *sfh = (void *)fid->raw;
304 return __snapfh_to_dentry(sb, sfh, false);
307 if (fh_type != FILEID_INO32_GEN &&
308 fh_type != FILEID_INO32_GEN_PARENT)
310 if (fh_len < sizeof(*fh) / 4)
313 dout("fh_to_dentry %llx\n", fh->ino);
314 return __fh_to_dentry(sb, fh->ino);
317 static struct dentry *__get_parent(struct super_block *sb,
318 struct dentry *child, u64 ino)
320 struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
321 struct ceph_mds_request *req;
326 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPPARENT,
329 return ERR_CAST(req);
332 req->r_inode = d_inode(child);
333 ihold(d_inode(child));
335 req->r_ino1 = (struct ceph_vino) {
341 mask = CEPH_STAT_CAP_INODE;
342 if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
343 mask |= CEPH_CAP_XATTR_SHARED;
344 req->r_args.getattr.mask = cpu_to_le32(mask);
347 err = ceph_mdsc_do_request(mdsc, NULL, req);
349 ceph_mdsc_put_request(req);
353 inode = req->r_target_inode;
356 ceph_mdsc_put_request(req);
358 return ERR_PTR(-ENOENT);
360 return d_obtain_alias(inode);
363 static struct dentry *ceph_get_parent(struct dentry *child)
365 struct inode *inode = d_inode(child);
368 if (ceph_snap(inode) != CEPH_NOSNAP) {
370 bool unlinked = false;
371 /* do not support non-directory */
372 if (!d_is_dir(child)) {
373 dn = ERR_PTR(-EINVAL);
376 dir = __lookup_inode(inode->i_sb, ceph_ino(inode));
381 /* There can be multiple paths to access snapped inode.
382 * For simplicity, treat snapdir of head inode as parent */
383 if (ceph_snap(inode) != CEPH_SNAPDIR) {
384 struct inode *snapdir = ceph_get_snapdir(dir);
385 if (dir->i_nlink == 0)
388 if (IS_ERR(snapdir)) {
389 dn = ERR_CAST(snapdir);
394 /* If directory has already been deleted, futher get_parent
395 * will fail. Do not mark snapdir dentry as disconnected,
396 * this prevent exportfs from doing futher get_parent. */
398 dn = d_obtain_root(dir);
400 dn = d_obtain_alias(dir);
402 dn = __get_parent(child->d_sb, child, 0);
405 dout("get_parent %p ino %llx.%llx err=%ld\n",
406 child, ceph_vinop(inode), (long)PTR_ERR_OR_ZERO(dn));
411 * convert regular fh to parent
413 static struct dentry *ceph_fh_to_parent(struct super_block *sb,
415 int fh_len, int fh_type)
417 struct ceph_nfs_confh *cfh = (void *)fid->raw;
418 struct dentry *dentry;
420 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
421 struct ceph_nfs_snapfh *sfh = (void *)fid->raw;
422 return __snapfh_to_dentry(sb, sfh, true);
425 if (fh_type != FILEID_INO32_GEN_PARENT)
427 if (fh_len < sizeof(*cfh) / 4)
430 dout("fh_to_parent %llx\n", cfh->parent_ino);
431 dentry = __get_parent(sb, NULL, cfh->ino);
432 if (unlikely(dentry == ERR_PTR(-ENOENT)))
433 dentry = __fh_to_dentry(sb, cfh->parent_ino);
437 static int __get_snap_name(struct dentry *parent, char *name,
438 struct dentry *child)
440 struct inode *inode = d_inode(child);
441 struct inode *dir = d_inode(parent);
442 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
443 struct ceph_mds_request *req = NULL;
444 char *last_name = NULL;
445 unsigned next_offset = 2;
448 if (ceph_ino(inode) != ceph_ino(dir))
450 if (ceph_snap(inode) == CEPH_SNAPDIR) {
451 if (ceph_snap(dir) == CEPH_NOSNAP) {
452 strcpy(name, fsc->mount_options->snapdir_name);
457 if (ceph_snap(dir) != CEPH_SNAPDIR)
461 struct ceph_mds_reply_info_parsed *rinfo;
462 struct ceph_mds_reply_dir_entry *rde;
465 req = ceph_mdsc_create_request(fsc->mdsc, CEPH_MDS_OP_LSSNAP,
472 err = ceph_alloc_readdir_reply_buffer(req, inode);
476 req->r_direct_mode = USE_AUTH_MDS;
477 req->r_readdir_offset = next_offset;
478 req->r_args.readdir.flags =
479 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
481 req->r_path2 = last_name;
487 req->r_dentry = dget(parent);
490 err = ceph_mdsc_do_request(fsc->mdsc, NULL, req);
496 rinfo = &req->r_reply_info;
497 for (i = 0; i < rinfo->dir_nr; i++) {
498 rde = rinfo->dir_entries + i;
499 BUG_ON(!rde->inode.in);
500 if (ceph_snap(inode) ==
501 le64_to_cpu(rde->inode.in->snapid)) {
502 memcpy(name, rde->name, rde->name_len);
503 name[rde->name_len] = '\0';
512 BUG_ON(rinfo->dir_nr <= 0);
513 rde = rinfo->dir_entries + (rinfo->dir_nr - 1);
514 next_offset += rinfo->dir_nr;
515 last_name = kstrndup(rde->name, rde->name_len, GFP_KERNEL);
521 ceph_mdsc_put_request(req);
527 ceph_mdsc_put_request(req);
529 dout("get_snap_name %p ino %llx.%llx err=%d\n",
530 child, ceph_vinop(inode), err);
534 static int ceph_get_name(struct dentry *parent, char *name,
535 struct dentry *child)
537 struct ceph_mds_client *mdsc;
538 struct ceph_mds_request *req;
539 struct inode *dir = d_inode(parent);
540 struct inode *inode = d_inode(child);
541 struct ceph_mds_reply_info_parsed *rinfo;
544 if (ceph_snap(inode) != CEPH_NOSNAP)
545 return __get_snap_name(parent, name, child);
547 mdsc = ceph_inode_to_client(inode)->mdsc;
548 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPNAME,
554 req->r_inode = inode;
556 req->r_ino2 = ceph_vino(d_inode(parent));
559 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
561 err = ceph_mdsc_do_request(mdsc, NULL, req);
567 rinfo = &req->r_reply_info;
568 if (!IS_ENCRYPTED(dir)) {
569 memcpy(name, rinfo->dname, rinfo->dname_len);
570 name[rinfo->dname_len] = 0;
572 struct fscrypt_str oname = FSTR_INIT(NULL, 0);
573 struct ceph_fname fname = { .dir = dir,
574 .name = rinfo->dname,
575 .ctext = rinfo->altname,
576 .name_len = rinfo->dname_len,
577 .ctext_len = rinfo->altname_len };
579 err = ceph_fname_alloc_buffer(dir, &oname);
583 err = ceph_fname_to_usr(&fname, NULL, &oname, NULL);
585 memcpy(name, oname.name, oname.len);
588 ceph_fname_free_buffer(dir, &oname);
591 dout("get_name %p ino %llx.%llx err %d %s%s\n",
592 child, ceph_vinop(inode), err,
593 err ? "" : "name ", err ? "" : name);
594 ceph_mdsc_put_request(req);
598 const struct export_operations ceph_export_ops = {
599 .encode_fh = ceph_encode_fh,
600 .fh_to_dentry = ceph_fh_to_dentry,
601 .fh_to_parent = ceph_fh_to_parent,
602 .get_parent = ceph_get_parent,
603 .get_name = ceph_get_name,