xref: /linux/fs/ceph/export.c (revision d623704bb23901a25bf6d6a40aa16b43a17622eb)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 
4 #include <linux/exportfs.h>
5 #include <linux/slab.h>
6 #include <asm/unaligned.h>
7 
8 #include "super.h"
9 #include "mds_client.h"
10 #include "crypto.h"
11 
12 /*
13  * Basic fh
14  */
15 struct ceph_nfs_fh {
16 	u64 ino;
17 } __attribute__ ((packed));
18 
19 /*
20  * Larger fh that includes parent ino.
21  */
22 struct ceph_nfs_confh {
23 	u64 ino, parent_ino;
24 } __attribute__ ((packed));
25 
26 /*
27  * fh for snapped inode
28  */
29 struct ceph_nfs_snapfh {
30 	u64 ino;
31 	u64 snapid;
32 	u64 parent_ino;
33 	u32 hash;
34 } __attribute__ ((packed));
35 
36 static int ceph_encode_snapfh(struct inode *inode, u32 *rawfh, int *max_len,
37 			      struct inode *parent_inode)
38 {
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);
44 	int ret;
45 	bool no_parent = true;
46 
47 	if (*max_len < snap_handle_length) {
48 		*max_len = snap_handle_length;
49 		ret = FILEID_INVALID;
50 		goto out;
51 	}
52 
53 	ret =  -EINVAL;
54 	if (snapid != CEPH_SNAPDIR) {
55 		struct inode *dir;
56 		struct dentry *dentry = d_find_alias(inode);
57 		if (!dentry)
58 			goto out;
59 
60 		rcu_read_lock();
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);
65 			no_parent = false;
66 		}
67 		rcu_read_unlock();
68 		dput(dentry);
69 	}
70 
71 	if (no_parent) {
72 		if (!S_ISDIR(inode->i_mode))
73 			goto out;
74 		sfh->parent_ino = sfh->ino;
75 		sfh->hash = 0;
76 	}
77 	sfh->ino = ceph_ino(inode);
78 	sfh->snapid = snapid;
79 
80 	*max_len = snap_handle_length;
81 	ret = FILEID_BTRFS_WITH_PARENT;
82 out:
83 	doutc(cl, "%p %llx.%llx ret=%d\n", inode, ceph_vinop(inode), ret);
84 	return ret;
85 }
86 
87 static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
88 			  struct inode *parent_inode)
89 {
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;
95 	int type;
96 
97 	if (ceph_snap(inode) != CEPH_NOSNAP)
98 		return ceph_encode_snapfh(inode, rawfh, max_len, parent_inode);
99 
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;
106 	}
107 
108 	if (parent_inode) {
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;
116 	} else {
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;
122 	}
123 	return type;
124 }
125 
126 static struct inode *__lookup_inode(struct super_block *sb, u64 ino)
127 {
128 	struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(sb)->mdsc;
129 	struct inode *inode;
130 	struct ceph_vino vino;
131 	int err;
132 
133 	vino.ino = ino;
134 	vino.snap = CEPH_NOSNAP;
135 
136 	if (ceph_vino_is_reserved(vino))
137 		return ERR_PTR(-ESTALE);
138 
139 	inode = ceph_find_inode(sb, vino);
140 	if (!inode) {
141 		struct ceph_mds_request *req;
142 		int mask;
143 
144 		req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
145 					       USE_ANY_MDS);
146 		if (IS_ERR(req))
147 			return ERR_CAST(req);
148 
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);
153 
154 		req->r_ino1 = vino;
155 		req->r_num_caps = 1;
156 		err = ceph_mdsc_do_request(mdsc, NULL, req);
157 		inode = req->r_target_inode;
158 		if (inode)
159 			ihold(inode);
160 		ceph_mdsc_put_request(req);
161 		if (!inode)
162 			return err < 0 ? ERR_PTR(err) : ERR_PTR(-ESTALE);
163 	} else {
164 		if (ceph_inode_is_shutdown(inode)) {
165 			iput(inode);
166 			return ERR_PTR(-ESTALE);
167 		}
168 	}
169 	return inode;
170 }
171 
172 struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino)
173 {
174 	struct inode *inode = __lookup_inode(sb, ino);
175 	if (IS_ERR(inode))
176 		return inode;
177 	if (inode->i_nlink == 0) {
178 		iput(inode);
179 		return ERR_PTR(-ESTALE);
180 	}
181 	return inode;
182 }
183 
184 static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)
185 {
186 	struct inode *inode = __lookup_inode(sb, ino);
187 	struct ceph_inode_info *ci = ceph_inode(inode);
188 	int err;
189 
190 	if (IS_ERR(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);
194 	if (err) {
195 		iput(inode);
196 		return ERR_PTR(err);
197 	}
198 	/* -ESTALE if inode as been unlinked and no file is open */
199 	if ((inode->i_nlink == 0) && !__ceph_is_file_opened(ci)) {
200 		iput(inode);
201 		return ERR_PTR(-ESTALE);
202 	}
203 	return d_obtain_alias(inode);
204 }
205 
206 static struct dentry *__snapfh_to_dentry(struct super_block *sb,
207 					  struct ceph_nfs_snapfh *sfh,
208 					  bool want_parent)
209 {
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;
213 	struct inode *inode;
214 	struct ceph_vino vino;
215 	int mask;
216 	int err;
217 	bool unlinked = false;
218 
219 	if (want_parent) {
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;
225 		else
226 			vino.snap = sfh->snapid;
227 	} else {
228 		vino.ino = sfh->ino;
229 		vino.snap = sfh->snapid;
230 	}
231 
232 	if (ceph_vino_is_reserved(vino))
233 		return ERR_PTR(-ESTALE);
234 
235 	inode = ceph_find_inode(sb, vino);
236 	if (inode) {
237 		if (ceph_inode_is_shutdown(inode)) {
238 			iput(inode);
239 			return ERR_PTR(-ESTALE);
240 		}
241 		return d_obtain_alias(inode);
242 	}
243 
244 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
245 				       USE_ANY_MDS);
246 	if (IS_ERR(req))
247 		return ERR_CAST(req);
248 
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);
260 		}
261 	}
262 
263 	req->r_ino1 = vino;
264 	req->r_num_caps = 1;
265 	err = ceph_mdsc_do_request(mdsc, NULL, req);
266 	inode = req->r_target_inode;
267 	if (inode) {
268 		if (vino.snap == CEPH_SNAPDIR) {
269 			if (inode->i_nlink == 0)
270 				unlinked = true;
271 			inode = ceph_get_snapdir(inode);
272 		} else if (ceph_snap(inode) == vino.snap) {
273 			ihold(inode);
274 		} else {
275 			/* mds does not support lookup snapped inode */
276 			inode = ERR_PTR(-EOPNOTSUPP);
277 		}
278 	} else {
279 		inode = ERR_PTR(-ESTALE);
280 	}
281 	ceph_mdsc_put_request(req);
282 
283 	if (want_parent) {
284 		doutc(cl, "%llx.%llx\n err=%d\n", vino.ino, vino.snap, err);
285 	} else {
286 		doutc(cl, "%llx.%llx parent %llx hash %x err=%d", vino.ino,
287 		      vino.snap, sfh->parent_ino, sfh->hash, err);
288 	}
289 	if (IS_ERR(inode))
290 		return ERR_CAST(inode);
291 	/* see comments in ceph_get_parent() */
292 	return unlinked ? d_obtain_root(inode) : d_obtain_alias(inode);
293 }
294 
295 /*
296  * convert regular fh to dentry
297  */
298 static struct dentry *ceph_fh_to_dentry(struct super_block *sb,
299 					struct fid *fid,
300 					int fh_len, int fh_type)
301 {
302 	struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb);
303 	struct ceph_nfs_fh *fh = (void *)fid->raw;
304 
305 	if (fh_type == FILEID_BTRFS_WITH_PARENT) {
306 		struct ceph_nfs_snapfh *sfh = (void *)fid->raw;
307 		return __snapfh_to_dentry(sb, sfh, false);
308 	}
309 
310 	if (fh_type != FILEID_INO32_GEN  &&
311 	    fh_type != FILEID_INO32_GEN_PARENT)
312 		return NULL;
313 	if (fh_len < sizeof(*fh) / 4)
314 		return NULL;
315 
316 	doutc(fsc->client, "%llx\n", fh->ino);
317 	return __fh_to_dentry(sb, fh->ino);
318 }
319 
320 static struct dentry *__get_parent(struct super_block *sb,
321 				   struct dentry *child, u64 ino)
322 {
323 	struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(sb)->mdsc;
324 	struct ceph_mds_request *req;
325 	struct inode *inode;
326 	int mask;
327 	int err;
328 
329 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPPARENT,
330 				       USE_ANY_MDS);
331 	if (IS_ERR(req))
332 		return ERR_CAST(req);
333 
334 	if (child) {
335 		req->r_inode = d_inode(child);
336 		ihold(d_inode(child));
337 	} else {
338 		req->r_ino1 = (struct ceph_vino) {
339 			.ino = ino,
340 			.snap = CEPH_NOSNAP,
341 		};
342 	}
343 
344 	mask = CEPH_STAT_CAP_INODE;
345 	if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
346 		mask |= CEPH_CAP_XATTR_SHARED;
347 	req->r_args.getattr.mask = cpu_to_le32(mask);
348 
349 	req->r_num_caps = 1;
350 	err = ceph_mdsc_do_request(mdsc, NULL, req);
351 	if (err) {
352 		ceph_mdsc_put_request(req);
353 		return ERR_PTR(err);
354 	}
355 
356 	inode = req->r_target_inode;
357 	if (inode)
358 		ihold(inode);
359 	ceph_mdsc_put_request(req);
360 	if (!inode)
361 		return ERR_PTR(-ENOENT);
362 
363 	return d_obtain_alias(inode);
364 }
365 
366 static struct dentry *ceph_get_parent(struct dentry *child)
367 {
368 	struct inode *inode = d_inode(child);
369 	struct ceph_client *cl = ceph_inode_to_client(inode);
370 	struct dentry *dn;
371 
372 	if (ceph_snap(inode) != CEPH_NOSNAP) {
373 		struct inode* dir;
374 		bool unlinked = false;
375 		/* do not support non-directory */
376 		if (!d_is_dir(child)) {
377 			dn = ERR_PTR(-EINVAL);
378 			goto out;
379 		}
380 		dir = __lookup_inode(inode->i_sb, ceph_ino(inode));
381 		if (IS_ERR(dir)) {
382 			dn = ERR_CAST(dir);
383 			goto out;
384 		}
385 		/* There can be multiple paths to access snapped inode.
386 		 * For simplicity, treat snapdir of head inode as parent */
387 		if (ceph_snap(inode) != CEPH_SNAPDIR) {
388 			struct inode *snapdir = ceph_get_snapdir(dir);
389 			if (dir->i_nlink == 0)
390 				unlinked = true;
391 			iput(dir);
392 			if (IS_ERR(snapdir)) {
393 				dn = ERR_CAST(snapdir);
394 				goto out;
395 			}
396 			dir = snapdir;
397 		}
398 		/* If directory has already been deleted, futher get_parent
399 		 * will fail. Do not mark snapdir dentry as disconnected,
400 		 * this prevent exportfs from doing futher get_parent. */
401 		if (unlinked)
402 			dn = d_obtain_root(dir);
403 		else
404 			dn = d_obtain_alias(dir);
405 	} else {
406 		dn = __get_parent(child->d_sb, child, 0);
407 	}
408 out:
409 	doutc(cl, "child %p %p %llx.%llx err=%ld\n", child, inode,
410 	      ceph_vinop(inode), (long)PTR_ERR_OR_ZERO(dn));
411 	return dn;
412 }
413 
414 /*
415  * convert regular fh to parent
416  */
417 static struct dentry *ceph_fh_to_parent(struct super_block *sb,
418 					struct fid *fid,
419 					int fh_len, int fh_type)
420 {
421 	struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb);
422 	struct ceph_nfs_confh *cfh = (void *)fid->raw;
423 	struct dentry *dentry;
424 
425 	if (fh_type == FILEID_BTRFS_WITH_PARENT) {
426 		struct ceph_nfs_snapfh *sfh = (void *)fid->raw;
427 		return __snapfh_to_dentry(sb, sfh, true);
428 	}
429 
430 	if (fh_type != FILEID_INO32_GEN_PARENT)
431 		return NULL;
432 	if (fh_len < sizeof(*cfh) / 4)
433 		return NULL;
434 
435 	doutc(fsc->client, "%llx\n", cfh->parent_ino);
436 	dentry = __get_parent(sb, NULL, cfh->ino);
437 	if (unlikely(dentry == ERR_PTR(-ENOENT)))
438 		dentry = __fh_to_dentry(sb, cfh->parent_ino);
439 	return dentry;
440 }
441 
442 static int __get_snap_name(struct dentry *parent, char *name,
443 			   struct dentry *child)
444 {
445 	struct inode *inode = d_inode(child);
446 	struct inode *dir = d_inode(parent);
447 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
448 	struct ceph_mds_request *req = NULL;
449 	char *last_name = NULL;
450 	unsigned next_offset = 2;
451 	int err = -EINVAL;
452 
453 	if (ceph_ino(inode) != ceph_ino(dir))
454 		goto out;
455 	if (ceph_snap(inode) == CEPH_SNAPDIR) {
456 		if (ceph_snap(dir) == CEPH_NOSNAP) {
457 			strcpy(name, fsc->mount_options->snapdir_name);
458 			err = 0;
459 		}
460 		goto out;
461 	}
462 	if (ceph_snap(dir) != CEPH_SNAPDIR)
463 		goto out;
464 
465 	while (1) {
466 		struct ceph_mds_reply_info_parsed *rinfo;
467 		struct ceph_mds_reply_dir_entry *rde;
468 		int i;
469 
470 		req = ceph_mdsc_create_request(fsc->mdsc, CEPH_MDS_OP_LSSNAP,
471 					       USE_AUTH_MDS);
472 		if (IS_ERR(req)) {
473 			err = PTR_ERR(req);
474 			req = NULL;
475 			goto out;
476 		}
477 		err = ceph_alloc_readdir_reply_buffer(req, inode);
478 		if (err)
479 			goto out;
480 
481 		req->r_direct_mode = USE_AUTH_MDS;
482 		req->r_readdir_offset = next_offset;
483 		req->r_args.readdir.flags =
484 				cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
485 		if (last_name) {
486 			req->r_path2 = last_name;
487 			last_name = NULL;
488 		}
489 
490 		req->r_inode = dir;
491 		ihold(dir);
492 		req->r_dentry = dget(parent);
493 
494 		inode_lock(dir);
495 		err = ceph_mdsc_do_request(fsc->mdsc, NULL, req);
496 		inode_unlock(dir);
497 
498 		if (err < 0)
499 			goto out;
500 
501 		rinfo = &req->r_reply_info;
502 		for (i = 0; i < rinfo->dir_nr; i++) {
503 			rde = rinfo->dir_entries + i;
504 			BUG_ON(!rde->inode.in);
505 			if (ceph_snap(inode) ==
506 			    le64_to_cpu(rde->inode.in->snapid)) {
507 				memcpy(name, rde->name, rde->name_len);
508 				name[rde->name_len] = '\0';
509 				err = 0;
510 				goto out;
511 			}
512 		}
513 
514 		if (rinfo->dir_end)
515 			break;
516 
517 		BUG_ON(rinfo->dir_nr <= 0);
518 		rde = rinfo->dir_entries + (rinfo->dir_nr - 1);
519 		next_offset += rinfo->dir_nr;
520 		last_name = kstrndup(rde->name, rde->name_len, GFP_KERNEL);
521 		if (!last_name) {
522 			err = -ENOMEM;
523 			goto out;
524 		}
525 
526 		ceph_mdsc_put_request(req);
527 		req = NULL;
528 	}
529 	err = -ENOENT;
530 out:
531 	if (req)
532 		ceph_mdsc_put_request(req);
533 	kfree(last_name);
534 	doutc(fsc->client, "child dentry %p %p %llx.%llx err=%d\n", child,
535 	      inode, ceph_vinop(inode), err);
536 	return err;
537 }
538 
539 static int ceph_get_name(struct dentry *parent, char *name,
540 			 struct dentry *child)
541 {
542 	struct ceph_mds_client *mdsc;
543 	struct ceph_mds_request *req;
544 	struct inode *dir = d_inode(parent);
545 	struct inode *inode = d_inode(child);
546 	struct ceph_mds_reply_info_parsed *rinfo;
547 	int err;
548 
549 	if (ceph_snap(inode) != CEPH_NOSNAP)
550 		return __get_snap_name(parent, name, child);
551 
552 	mdsc = ceph_inode_to_fs_client(inode)->mdsc;
553 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPNAME,
554 				       USE_ANY_MDS);
555 	if (IS_ERR(req))
556 		return PTR_ERR(req);
557 
558 	inode_lock(dir);
559 	req->r_inode = inode;
560 	ihold(inode);
561 	req->r_ino2 = ceph_vino(d_inode(parent));
562 	req->r_parent = dir;
563 	ihold(dir);
564 	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
565 	req->r_num_caps = 2;
566 	err = ceph_mdsc_do_request(mdsc, NULL, req);
567 	inode_unlock(dir);
568 
569 	if (err)
570 		goto out;
571 
572 	rinfo = &req->r_reply_info;
573 	if (!IS_ENCRYPTED(dir)) {
574 		memcpy(name, rinfo->dname, rinfo->dname_len);
575 		name[rinfo->dname_len] = 0;
576 	} else {
577 		struct fscrypt_str oname = FSTR_INIT(NULL, 0);
578 		struct ceph_fname fname = { .dir	= dir,
579 					    .name	= rinfo->dname,
580 					    .ctext	= rinfo->altname,
581 					    .name_len	= rinfo->dname_len,
582 					    .ctext_len	= rinfo->altname_len };
583 
584 		err = ceph_fname_alloc_buffer(dir, &oname);
585 		if (err < 0)
586 			goto out;
587 
588 		err = ceph_fname_to_usr(&fname, NULL, &oname, NULL);
589 		if (!err) {
590 			memcpy(name, oname.name, oname.len);
591 			name[oname.len] = 0;
592 		}
593 		ceph_fname_free_buffer(dir, &oname);
594 	}
595 out:
596 	doutc(mdsc->fsc->client, "child dentry %p %p %llx.%llx err %d %s%s\n",
597 	      child, inode, ceph_vinop(inode), err, err ? "" : "name ",
598 	      err ? "" : name);
599 	ceph_mdsc_put_request(req);
600 	return err;
601 }
602 
603 const struct export_operations ceph_export_ops = {
604 	.encode_fh = ceph_encode_fh,
605 	.fh_to_dentry = ceph_fh_to_dentry,
606 	.fh_to_parent = ceph_fh_to_parent,
607 	.get_parent = ceph_get_parent,
608 	.get_name = ceph_get_name,
609 };
610