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