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 <linux/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 struct ceph_client *cl = ceph_inode_to_client(inode);
40 static const int snap_handle_length =
41 sizeof(struct ceph_nfs_snapfh) >> 2;
42 struct ceph_nfs_snapfh *sfh = (void *)rawfh;
43 u64 snapid = ceph_snap(inode);
45 bool no_parent = true;
47 if (*max_len < snap_handle_length) {
48 *max_len = snap_handle_length;
54 if (snapid != CEPH_SNAPDIR) {
56 struct dentry *dentry = d_find_alias(inode);
61 dir = d_inode_rcu(dentry->d_parent);
62 if (ceph_snap(dir) != CEPH_SNAPDIR) {
63 sfh->parent_ino = ceph_ino(dir);
64 sfh->hash = ceph_dentry_hash(dir, dentry);
72 if (!S_ISDIR(inode->i_mode))
74 sfh->parent_ino = sfh->ino;
77 sfh->ino = ceph_ino(inode);
80 *max_len = snap_handle_length;
81 ret = FILEID_BTRFS_WITH_PARENT;
83 doutc(cl, "%p %llx.%llx ret=%d\n", inode, ceph_vinop(inode), ret);
87 static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
88 struct inode *parent_inode)
90 struct ceph_client *cl = ceph_inode_to_client(inode);
91 static const int handle_length =
92 sizeof(struct ceph_nfs_fh) >> 2;
93 static const int connected_handle_length =
94 sizeof(struct ceph_nfs_confh) >> 2;
97 if (ceph_snap(inode) != CEPH_NOSNAP)
98 return ceph_encode_snapfh(inode, rawfh, max_len, parent_inode);
100 if (parent_inode && (*max_len < connected_handle_length)) {
101 *max_len = connected_handle_length;
102 return FILEID_INVALID;
103 } else if (*max_len < handle_length) {
104 *max_len = handle_length;
105 return FILEID_INVALID;
109 struct ceph_nfs_confh *cfh = (void *)rawfh;
110 doutc(cl, "%p %llx.%llx with parent %p %llx.%llx\n", inode,
111 ceph_vinop(inode), parent_inode, ceph_vinop(parent_inode));
112 cfh->ino = ceph_ino(inode);
113 cfh->parent_ino = ceph_ino(parent_inode);
114 *max_len = connected_handle_length;
115 type = FILEID_INO32_GEN_PARENT;
117 struct ceph_nfs_fh *fh = (void *)rawfh;
118 doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
119 fh->ino = ceph_ino(inode);
120 *max_len = handle_length;
121 type = FILEID_INO32_GEN;
126 static struct inode *__lookup_inode(struct super_block *sb, u64 ino)
128 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(sb)->mdsc;
130 struct ceph_vino vino;
134 vino.snap = CEPH_NOSNAP;
136 if (ceph_vino_is_reserved(vino))
137 return ERR_PTR(-ESTALE);
139 inode = ceph_find_inode(sb, vino);
141 struct ceph_mds_request *req;
144 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
147 return ERR_CAST(req);
149 mask = CEPH_STAT_CAP_INODE;
150 if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
151 mask |= CEPH_CAP_XATTR_SHARED;
152 req->r_args.lookupino.mask = cpu_to_le32(mask);
156 err = ceph_mdsc_do_request(mdsc, NULL, req);
157 inode = req->r_target_inode;
160 ceph_mdsc_put_request(req);
162 return err < 0 ? ERR_PTR(err) : ERR_PTR(-ESTALE);
164 if (ceph_inode_is_shutdown(inode)) {
166 return ERR_PTR(-ESTALE);
172 struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino)
174 struct inode *inode = __lookup_inode(sb, ino);
177 if (inode->i_nlink == 0) {
179 return ERR_PTR(-ESTALE);
184 static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)
186 struct inode *inode = __lookup_inode(sb, ino);
187 struct ceph_inode_info *ci = ceph_inode(inode);
191 return ERR_CAST(inode);
192 /* We need LINK caps to reliably check i_nlink */
193 err = ceph_do_getattr(inode, CEPH_CAP_LINK_SHARED, false);
198 /* -ESTALE if inode as been unlinked and no file is open */
199 if ((inode->i_nlink == 0) && !__ceph_is_file_opened(ci)) {
201 return ERR_PTR(-ESTALE);
203 return d_obtain_alias(inode);
206 static struct dentry *__snapfh_to_dentry(struct super_block *sb,
207 struct ceph_nfs_snapfh *sfh,
210 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(sb)->mdsc;
211 struct ceph_client *cl = mdsc->fsc->client;
212 struct ceph_mds_request *req;
214 struct ceph_vino vino;
217 bool unlinked = false;
220 vino.ino = sfh->parent_ino;
221 if (sfh->snapid == CEPH_SNAPDIR)
222 vino.snap = CEPH_NOSNAP;
223 else if (sfh->ino == sfh->parent_ino)
224 vino.snap = CEPH_SNAPDIR;
226 vino.snap = sfh->snapid;
229 vino.snap = sfh->snapid;
232 if (ceph_vino_is_reserved(vino))
233 return ERR_PTR(-ESTALE);
235 inode = ceph_find_inode(sb, vino);
237 if (ceph_inode_is_shutdown(inode)) {
239 return ERR_PTR(-ESTALE);
241 return d_obtain_alias(inode);
244 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
247 return ERR_CAST(req);
249 mask = CEPH_STAT_CAP_INODE;
250 if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
251 mask |= CEPH_CAP_XATTR_SHARED;
252 req->r_args.lookupino.mask = cpu_to_le32(mask);
253 if (vino.snap < CEPH_NOSNAP) {
254 req->r_args.lookupino.snapid = cpu_to_le64(vino.snap);
255 if (!want_parent && sfh->ino != sfh->parent_ino) {
256 req->r_args.lookupino.parent =
257 cpu_to_le64(sfh->parent_ino);
258 req->r_args.lookupino.hash =
259 cpu_to_le32(sfh->hash);
265 err = ceph_mdsc_do_request(mdsc, NULL, req);
266 inode = req->r_target_inode;
268 if (vino.snap == CEPH_SNAPDIR) {
269 if (inode->i_nlink == 0)
271 inode = ceph_get_snapdir(inode);
272 } else if (ceph_snap(inode) == vino.snap) {
275 /* mds does not support lookup snapped inode */
276 inode = ERR_PTR(-EOPNOTSUPP);
279 inode = ERR_PTR(-ESTALE);
281 ceph_mdsc_put_request(req);
284 doutc(cl, "%llx.%llx\n err=%d\n", vino.ino, vino.snap, err);
286 doutc(cl, "%llx.%llx parent %llx hash %x err=%d", vino.ino,
287 vino.snap, sfh->parent_ino, sfh->hash, err);
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_fs_client *fsc = ceph_sb_to_fs_client(sb);
301 struct ceph_nfs_fh *fh = (void *)fid->raw;
303 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
304 struct ceph_nfs_snapfh *sfh = (void *)fid->raw;
305 return __snapfh_to_dentry(sb, sfh, false);
308 if (fh_type != FILEID_INO32_GEN &&
309 fh_type != FILEID_INO32_GEN_PARENT)
311 if (fh_len < sizeof(*fh) / 4)
314 doutc(fsc->client, "%llx\n", fh->ino);
315 return __fh_to_dentry(sb, fh->ino);
318 static struct dentry *__get_parent(struct super_block *sb,
319 struct dentry *child, u64 ino)
321 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(sb)->mdsc;
322 struct ceph_mds_request *req;
327 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPPARENT,
330 return ERR_CAST(req);
333 req->r_inode = d_inode(child);
334 ihold(d_inode(child));
336 req->r_ino1 = (struct ceph_vino) {
342 mask = CEPH_STAT_CAP_INODE;
343 if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
344 mask |= CEPH_CAP_XATTR_SHARED;
345 req->r_args.getattr.mask = cpu_to_le32(mask);
348 err = ceph_mdsc_do_request(mdsc, NULL, req);
350 ceph_mdsc_put_request(req);
354 inode = req->r_target_inode;
357 ceph_mdsc_put_request(req);
359 return ERR_PTR(-ENOENT);
361 return d_obtain_alias(inode);
364 static struct dentry *ceph_get_parent(struct dentry *child)
366 struct inode *inode = d_inode(child);
367 struct ceph_client *cl = ceph_inode_to_client(inode);
370 if (ceph_snap(inode) != CEPH_NOSNAP) {
372 bool unlinked = false;
373 /* do not support non-directory */
374 if (!d_is_dir(child)) {
375 dn = ERR_PTR(-EINVAL);
378 dir = __lookup_inode(inode->i_sb, ceph_ino(inode));
383 /* There can be multiple paths to access snapped inode.
384 * For simplicity, treat snapdir of head inode as parent */
385 if (ceph_snap(inode) != CEPH_SNAPDIR) {
386 struct inode *snapdir = ceph_get_snapdir(dir);
387 if (dir->i_nlink == 0)
390 if (IS_ERR(snapdir)) {
391 dn = ERR_CAST(snapdir);
396 /* If directory has already been deleted, further get_parent
397 * will fail. Do not mark snapdir dentry as disconnected,
398 * this prevents exportfs from doing further get_parent. */
400 dn = d_obtain_root(dir);
402 dn = d_obtain_alias(dir);
404 dn = __get_parent(child->d_sb, child, 0);
407 doutc(cl, "child %p %p %llx.%llx err=%ld\n", child, inode,
408 ceph_vinop(inode), (long)PTR_ERR_OR_ZERO(dn));
413 * convert regular fh to parent
415 static struct dentry *ceph_fh_to_parent(struct super_block *sb,
417 int fh_len, int fh_type)
419 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb);
420 struct ceph_nfs_confh *cfh = (void *)fid->raw;
421 struct dentry *dentry;
423 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
424 struct ceph_nfs_snapfh *sfh = (void *)fid->raw;
425 return __snapfh_to_dentry(sb, sfh, true);
428 if (fh_type != FILEID_INO32_GEN_PARENT)
430 if (fh_len < sizeof(*cfh) / 4)
433 doutc(fsc->client, "%llx\n", cfh->parent_ino);
434 dentry = __get_parent(sb, NULL, cfh->ino);
435 if (unlikely(dentry == ERR_PTR(-ENOENT)))
436 dentry = __fh_to_dentry(sb, cfh->parent_ino);
440 static int __get_snap_name(struct dentry *parent, char *name,
441 struct dentry *child)
443 struct inode *inode = d_inode(child);
444 struct inode *dir = d_inode(parent);
445 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
446 struct ceph_mds_request *req = NULL;
447 char *last_name = NULL;
448 unsigned next_offset = 2;
451 if (ceph_ino(inode) != ceph_ino(dir))
453 if (ceph_snap(inode) == CEPH_SNAPDIR) {
454 if (ceph_snap(dir) == CEPH_NOSNAP) {
456 * .get_name() from struct export_operations
457 * assumes that its 'name' parameter is pointing
458 * to a NAME_MAX+1 sized buffer
460 strscpy(name, fsc->mount_options->snapdir_name,
466 if (ceph_snap(dir) != CEPH_SNAPDIR)
470 struct ceph_mds_reply_info_parsed *rinfo;
471 struct ceph_mds_reply_dir_entry *rde;
474 req = ceph_mdsc_create_request(fsc->mdsc, CEPH_MDS_OP_LSSNAP,
481 err = ceph_alloc_readdir_reply_buffer(req, inode);
485 req->r_direct_mode = USE_AUTH_MDS;
486 req->r_readdir_offset = next_offset;
487 req->r_args.readdir.flags =
488 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
490 req->r_path2 = last_name;
496 req->r_dentry = dget(parent);
499 err = ceph_mdsc_do_request(fsc->mdsc, NULL, req);
505 rinfo = &req->r_reply_info;
506 for (i = 0; i < rinfo->dir_nr; i++) {
507 rde = rinfo->dir_entries + i;
508 BUG_ON(!rde->inode.in);
509 if (ceph_snap(inode) ==
510 le64_to_cpu(rde->inode.in->snapid)) {
511 memcpy(name, rde->name, rde->name_len);
512 name[rde->name_len] = '\0';
521 BUG_ON(rinfo->dir_nr <= 0);
522 rde = rinfo->dir_entries + (rinfo->dir_nr - 1);
523 next_offset += rinfo->dir_nr;
524 last_name = kstrndup(rde->name, rde->name_len, GFP_KERNEL);
530 ceph_mdsc_put_request(req);
536 ceph_mdsc_put_request(req);
538 doutc(fsc->client, "child dentry %p %p %llx.%llx err=%d\n", child,
539 inode, ceph_vinop(inode), err);
543 static int ceph_get_name(struct dentry *parent, char *name,
544 struct dentry *child)
546 struct ceph_mds_client *mdsc;
547 struct ceph_mds_request *req;
548 struct inode *dir = d_inode(parent);
549 struct inode *inode = d_inode(child);
550 struct ceph_mds_reply_info_parsed *rinfo;
553 if (ceph_snap(inode) != CEPH_NOSNAP)
554 return __get_snap_name(parent, name, child);
556 mdsc = ceph_inode_to_fs_client(inode)->mdsc;
557 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPNAME,
563 req->r_inode = inode;
565 req->r_ino2 = ceph_vino(d_inode(parent));
568 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
570 err = ceph_mdsc_do_request(mdsc, NULL, req);
576 rinfo = &req->r_reply_info;
577 if (!IS_ENCRYPTED(dir)) {
578 memcpy(name, rinfo->dname, rinfo->dname_len);
579 name[rinfo->dname_len] = 0;
581 struct fscrypt_str oname = FSTR_INIT(NULL, 0);
582 struct ceph_fname fname = { .dir = dir,
583 .name = rinfo->dname,
584 .ctext = rinfo->altname,
585 .name_len = rinfo->dname_len,
586 .ctext_len = rinfo->altname_len };
588 err = ceph_fname_alloc_buffer(dir, &oname);
592 err = ceph_fname_to_usr(&fname, NULL, &oname, NULL);
594 memcpy(name, oname.name, oname.len);
597 ceph_fname_free_buffer(dir, &oname);
600 doutc(mdsc->fsc->client, "child dentry %p %p %llx.%llx err %d %s%s\n",
601 child, inode, ceph_vinop(inode), err, err ? "" : "name ",
603 ceph_mdsc_put_request(req);
607 const struct export_operations ceph_export_ops = {
608 .encode_fh = ceph_encode_fh,
609 .fh_to_dentry = ceph_fh_to_dentry,
610 .fh_to_parent = ceph_fh_to_parent,
611 .get_parent = ceph_get_parent,
612 .get_name = ceph_get_name,