xref: /freebsd/sys/contrib/openzfs/module/os/linux/zfs/zpl_ctldir.c (revision b5daf675efc746611c7cfcd1fa474b8905064c4b)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
24  * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
25  * LLNL-CODE-403049.
26  * Rewritten for Linux by:
27  *   Rohan Puri <rohan.puri15@gmail.com>
28  *   Brian Behlendorf <behlendorf1@llnl.gov>
29  */
30 
31 #include <sys/zfs_znode.h>
32 #include <sys/zfs_vfsops.h>
33 #include <sys/zfs_vnops.h>
34 #include <sys/zfs_ctldir.h>
35 #include <sys/zpl.h>
36 #include <sys/dmu.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/zap.h>
39 
40 /*
41  * Common open routine.  Disallow any write access.
42  */
43 static int
44 zpl_common_open(struct inode *ip, struct file *filp)
45 {
46 	if (blk_mode_is_open_write(filp->f_mode))
47 		return (-EACCES);
48 
49 	return (generic_file_open(ip, filp));
50 }
51 
52 /*
53  * Get root directory contents.
54  */
55 static int
56 zpl_root_iterate(struct file *filp, struct dir_context *ctx)
57 {
58 	zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
59 	int error = 0;
60 
61 	if (zfsvfs->z_show_ctldir == ZFS_SNAPDIR_DISABLED) {
62 		return (SET_ERROR(ENOENT));
63 	}
64 
65 	if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
66 		return (error);
67 
68 	if (!dir_emit_dots(filp, ctx))
69 		goto out;
70 
71 	if (ctx->pos == 2) {
72 		if (!dir_emit(ctx, ZFS_SNAPDIR_NAME,
73 		    strlen(ZFS_SNAPDIR_NAME), ZFSCTL_INO_SNAPDIR, DT_DIR))
74 			goto out;
75 
76 		ctx->pos++;
77 	}
78 
79 	if (ctx->pos == 3) {
80 		if (!dir_emit(ctx, ZFS_SHAREDIR_NAME,
81 		    strlen(ZFS_SHAREDIR_NAME), ZFSCTL_INO_SHARES, DT_DIR))
82 			goto out;
83 
84 		ctx->pos++;
85 	}
86 out:
87 	zpl_exit(zfsvfs, FTAG);
88 
89 	return (error);
90 }
91 
92 /*
93  * Get root directory attributes.
94  */
95 static int
96 #ifdef HAVE_IDMAP_IOPS_GETATTR
97 zpl_root_getattr_impl(struct mnt_idmap *user_ns,
98     const struct path *path, struct kstat *stat, u32 request_mask,
99     unsigned int query_flags)
100 #elif defined(HAVE_USERNS_IOPS_GETATTR)
101 zpl_root_getattr_impl(struct user_namespace *user_ns,
102     const struct path *path, struct kstat *stat, u32 request_mask,
103     unsigned int query_flags)
104 #else
105 zpl_root_getattr_impl(const struct path *path, struct kstat *stat,
106     u32 request_mask, unsigned int query_flags)
107 #endif
108 {
109 	(void) request_mask, (void) query_flags;
110 	struct inode *ip = path->dentry->d_inode;
111 
112 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
113 #ifdef HAVE_GENERIC_FILLATTR_USERNS
114 	generic_fillattr(user_ns, ip, stat);
115 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
116 	generic_fillattr(user_ns, ip, stat);
117 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
118 	generic_fillattr(user_ns, request_mask, ip, stat);
119 #else
120 	(void) user_ns;
121 #endif
122 #else
123 	generic_fillattr(ip, stat);
124 #endif
125 	stat->atime = current_time(ip);
126 
127 	return (0);
128 }
129 ZPL_GETATTR_WRAPPER(zpl_root_getattr);
130 
131 static struct dentry *
132 zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
133 {
134 	cred_t *cr = CRED();
135 	struct inode *ip;
136 	int error;
137 
138 	crhold(cr);
139 	error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL);
140 	ASSERT3S(error, <=, 0);
141 	crfree(cr);
142 
143 	if (error) {
144 		if (error == -ENOENT)
145 			return (d_splice_alias(NULL, dentry));
146 		else
147 			return (ERR_PTR(error));
148 	}
149 
150 	return (d_splice_alias(ip, dentry));
151 }
152 
153 /*
154  * The '.zfs' control directory file and inode operations.
155  */
156 const struct file_operations zpl_fops_root = {
157 	.open		= zpl_common_open,
158 	.llseek		= generic_file_llseek,
159 	.read		= generic_read_dir,
160 	.iterate_shared	= zpl_root_iterate,
161 };
162 
163 const struct inode_operations zpl_ops_root = {
164 	.lookup		= zpl_root_lookup,
165 	.getattr	= zpl_root_getattr,
166 };
167 
168 static struct vfsmount *
169 zpl_snapdir_automount(struct path *path)
170 {
171 	int error;
172 
173 	error = -zfsctl_snapshot_mount(path, 0);
174 	if (error)
175 		return (ERR_PTR(error));
176 
177 	/*
178 	 * Rather than returning the new vfsmount for the snapshot we must
179 	 * return NULL to indicate a mount collision.  This is done because
180 	 * the user space mount calls do_add_mount() which adds the vfsmount
181 	 * to the name space.  If we returned the new mount here it would be
182 	 * added again to the vfsmount list resulting in list corruption.
183 	 */
184 	return (NULL);
185 }
186 
187 /*
188  * Negative dentries must always be revalidated so newly created snapshots
189  * can be detected and automounted.  Normal dentries should be kept because
190  * as of the 3.18 kernel revaliding the mountpoint dentry will result in
191  * the snapshot being immediately unmounted.
192  */
193 #ifdef HAVE_D_REVALIDATE_4ARGS
194 static int
195 zpl_snapdir_revalidate(struct inode *dir, const struct qstr *name,
196     struct dentry *dentry, unsigned int flags)
197 #else
198 static int
199 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags)
200 #endif
201 {
202 	return (!!dentry->d_inode);
203 }
204 
205 static dentry_operations_t zpl_dops_snapdirs = {
206 /*
207  * Auto mounting of snapshots is only supported for 2.6.37 and
208  * newer kernels.  Prior to this kernel the ops->follow_link()
209  * callback was used as a hack to trigger the mount.  The
210  * resulting vfsmount was then explicitly grafted in to the
211  * name space.  While it might be possible to add compatibility
212  * code to accomplish this it would require considerable care.
213  */
214 	.d_automount	= zpl_snapdir_automount,
215 	.d_revalidate	= zpl_snapdir_revalidate,
216 };
217 
218 static struct dentry *
219 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
220     unsigned int flags)
221 {
222 	fstrans_cookie_t cookie;
223 	cred_t *cr = CRED();
224 	struct inode *ip = NULL;
225 	int error;
226 
227 	crhold(cr);
228 	cookie = spl_fstrans_mark();
229 	error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,
230 	    0, cr, NULL, NULL);
231 	ASSERT3S(error, <=, 0);
232 	spl_fstrans_unmark(cookie);
233 	crfree(cr);
234 
235 	if (error && error != -ENOENT)
236 		return (ERR_PTR(error));
237 
238 	ASSERT(error == 0 || ip == NULL);
239 	d_clear_d_op(dentry);
240 	d_set_d_op(dentry, &zpl_dops_snapdirs);
241 	dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
242 
243 	return (d_splice_alias(ip, dentry));
244 }
245 
246 static int
247 zpl_snapdir_iterate(struct file *filp, struct dir_context *ctx)
248 {
249 	zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
250 	fstrans_cookie_t cookie;
251 	char snapname[MAXNAMELEN];
252 	boolean_t case_conflict;
253 	uint64_t id, pos;
254 	int error = 0;
255 
256 	if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
257 		return (error);
258 	cookie = spl_fstrans_mark();
259 
260 	if (!dir_emit_dots(filp, ctx))
261 		goto out;
262 
263 	/* Start the position at 0 if it already emitted . and .. */
264 	pos = (ctx->pos == 2 ? 0 : ctx->pos);
265 	while (error == 0) {
266 		dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
267 		error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN,
268 		    snapname, &id, &pos, &case_conflict);
269 		dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
270 		if (error)
271 			goto out;
272 
273 		if (!dir_emit(ctx, snapname, strlen(snapname),
274 		    ZFSCTL_INO_SHARES - id, DT_DIR))
275 			goto out;
276 
277 		ctx->pos = pos;
278 	}
279 out:
280 	spl_fstrans_unmark(cookie);
281 	zpl_exit(zfsvfs, FTAG);
282 
283 	if (error == -ENOENT)
284 		return (0);
285 
286 	return (error);
287 }
288 
289 static int
290 #ifdef HAVE_IOPS_RENAME_USERNS
291 zpl_snapdir_rename2(struct user_namespace *user_ns, struct inode *sdip,
292     struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
293     unsigned int flags)
294 #elif defined(HAVE_IOPS_RENAME_IDMAP)
295 zpl_snapdir_rename2(struct mnt_idmap *user_ns, struct inode *sdip,
296     struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
297     unsigned int flags)
298 #else
299 zpl_snapdir_rename2(struct inode *sdip, struct dentry *sdentry,
300     struct inode *tdip, struct dentry *tdentry, unsigned int flags)
301 #endif
302 {
303 	cred_t *cr = CRED();
304 	int error;
305 
306 	/* We probably don't want to support renameat2(2) in ctldir */
307 	if (flags)
308 		return (-EINVAL);
309 
310 	crhold(cr);
311 	error = -zfsctl_snapdir_rename(sdip, dname(sdentry),
312 	    tdip, dname(tdentry), cr, 0);
313 	ASSERT3S(error, <=, 0);
314 	crfree(cr);
315 
316 	return (error);
317 }
318 
319 #if (!defined(HAVE_RENAME_WANTS_FLAGS) && \
320 	!defined(HAVE_IOPS_RENAME_USERNS) && \
321 	!defined(HAVE_IOPS_RENAME_IDMAP))
322 static int
323 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,
324     struct inode *tdip, struct dentry *tdentry)
325 {
326 	return (zpl_snapdir_rename2(sdip, sdentry, tdip, tdentry, 0));
327 }
328 #endif
329 
330 static int
331 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
332 {
333 	cred_t *cr = CRED();
334 	int error;
335 
336 	crhold(cr);
337 	error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);
338 	ASSERT3S(error, <=, 0);
339 	crfree(cr);
340 
341 	return (error);
342 }
343 
344 #if defined(HAVE_IOPS_MKDIR_USERNS)
345 static int
346 zpl_snapdir_mkdir(struct user_namespace *user_ns, struct inode *dip,
347     struct dentry *dentry, umode_t mode)
348 #elif defined(HAVE_IOPS_MKDIR_IDMAP)
349 static int
350 zpl_snapdir_mkdir(struct mnt_idmap *user_ns, struct inode *dip,
351     struct dentry *dentry, umode_t mode)
352 #elif defined(HAVE_IOPS_MKDIR_DENTRY)
353 static struct dentry *
354 zpl_snapdir_mkdir(struct mnt_idmap *user_ns, struct inode *dip,
355     struct dentry *dentry, umode_t mode)
356 #else
357 static int
358 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
359 #endif
360 {
361 	cred_t *cr = CRED();
362 	vattr_t *vap;
363 	struct inode *ip;
364 	int error;
365 
366 	crhold(cr);
367 	vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
368 #if (defined(HAVE_IOPS_MKDIR_USERNS) || defined(HAVE_IOPS_MKDIR_IDMAP))
369 	zpl_vap_init(vap, dip, mode | S_IFDIR, cr, user_ns);
370 #else
371 	zpl_vap_init(vap, dip, mode | S_IFDIR, cr, zfs_init_idmap);
372 #endif
373 
374 	error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
375 	if (error == 0) {
376 		d_clear_d_op(dentry);
377 		d_set_d_op(dentry, &zpl_dops_snapdirs);
378 		d_instantiate(dentry, ip);
379 	}
380 
381 	kmem_free(vap, sizeof (vattr_t));
382 	ASSERT3S(error, <=, 0);
383 	crfree(cr);
384 
385 #if defined(HAVE_IOPS_MKDIR_DENTRY)
386 	return (ERR_PTR(error));
387 #else
388 	return (error);
389 #endif
390 }
391 
392 /*
393  * Get snapshot directory attributes.
394  */
395 static int
396 #ifdef HAVE_IDMAP_IOPS_GETATTR
397 zpl_snapdir_getattr_impl(struct mnt_idmap *user_ns,
398     const struct path *path, struct kstat *stat, u32 request_mask,
399     unsigned int query_flags)
400 #elif defined(HAVE_USERNS_IOPS_GETATTR)
401 zpl_snapdir_getattr_impl(struct user_namespace *user_ns,
402     const struct path *path, struct kstat *stat, u32 request_mask,
403     unsigned int query_flags)
404 #else
405 zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat,
406     u32 request_mask, unsigned int query_flags)
407 #endif
408 {
409 	(void) request_mask, (void) query_flags;
410 	struct inode *ip = path->dentry->d_inode;
411 	zfsvfs_t *zfsvfs = ITOZSB(ip);
412 	int error;
413 
414 	if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
415 		return (error);
416 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
417 #ifdef HAVE_GENERIC_FILLATTR_USERNS
418 	generic_fillattr(user_ns, ip, stat);
419 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
420 	generic_fillattr(user_ns, ip, stat);
421 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
422 	generic_fillattr(user_ns, request_mask, ip, stat);
423 #else
424 	(void) user_ns;
425 #endif
426 #else
427 	generic_fillattr(ip, stat);
428 #endif
429 
430 	stat->nlink = stat->size = 2;
431 
432 	dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);
433 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {
434 		uint64_t snap_count;
435 		int err = zap_count(
436 		    dmu_objset_pool(ds->ds_objset)->dp_meta_objset,
437 		    dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);
438 		if (err != 0) {
439 			zpl_exit(zfsvfs, FTAG);
440 			return (-err);
441 		}
442 		stat->nlink += snap_count;
443 	}
444 
445 	stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
446 	stat->atime = current_time(ip);
447 	zpl_exit(zfsvfs, FTAG);
448 
449 	return (0);
450 }
451 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr);
452 
453 /*
454  * The '.zfs/snapshot' directory file operations.  These mainly control
455  * generating the list of available snapshots when doing an 'ls' in the
456  * directory.  See zpl_snapdir_readdir().
457  */
458 const struct file_operations zpl_fops_snapdir = {
459 	.open		= zpl_common_open,
460 	.llseek		= generic_file_llseek,
461 	.read		= generic_read_dir,
462 	.iterate_shared	= zpl_snapdir_iterate,
463 
464 };
465 
466 /*
467  * The '.zfs/snapshot' directory inode operations.  These mainly control
468  * creating an inode for a snapshot directory and initializing the needed
469  * infrastructure to automount the snapshot.  See zpl_snapdir_lookup().
470  */
471 const struct inode_operations zpl_ops_snapdir = {
472 	.lookup		= zpl_snapdir_lookup,
473 	.getattr	= zpl_snapdir_getattr,
474 #if (defined(HAVE_RENAME_WANTS_FLAGS) || \
475 	defined(HAVE_IOPS_RENAME_USERNS) || \
476 	defined(HAVE_IOPS_RENAME_IDMAP))
477 	.rename		= zpl_snapdir_rename2,
478 #else
479 	.rename		= zpl_snapdir_rename,
480 #endif
481 	.rmdir		= zpl_snapdir_rmdir,
482 	.mkdir		= zpl_snapdir_mkdir,
483 };
484 
485 static struct dentry *
486 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
487     unsigned int flags)
488 {
489 	fstrans_cookie_t cookie;
490 	cred_t *cr = CRED();
491 	struct inode *ip = NULL;
492 	int error;
493 
494 	crhold(cr);
495 	cookie = spl_fstrans_mark();
496 	error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
497 	    0, cr, NULL, NULL);
498 	ASSERT3S(error, <=, 0);
499 	spl_fstrans_unmark(cookie);
500 	crfree(cr);
501 
502 	if (error) {
503 		if (error == -ENOENT)
504 			return (d_splice_alias(NULL, dentry));
505 		else
506 			return (ERR_PTR(error));
507 	}
508 
509 	return (d_splice_alias(ip, dentry));
510 }
511 
512 static int
513 zpl_shares_iterate(struct file *filp, struct dir_context *ctx)
514 {
515 	fstrans_cookie_t cookie;
516 	cred_t *cr = CRED();
517 	zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
518 	znode_t *dzp;
519 	int error = 0;
520 
521 	if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
522 		return (error);
523 	cookie = spl_fstrans_mark();
524 
525 	if (zfsvfs->z_shares_dir == 0) {
526 		dir_emit_dots(filp, ctx);
527 		goto out;
528 	}
529 
530 	error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
531 	if (error)
532 		goto out;
533 
534 	crhold(cr);
535 	error = -zfs_readdir(ZTOI(dzp), ctx, cr);
536 	crfree(cr);
537 
538 	iput(ZTOI(dzp));
539 out:
540 	spl_fstrans_unmark(cookie);
541 	zpl_exit(zfsvfs, FTAG);
542 	ASSERT3S(error, <=, 0);
543 
544 	return (error);
545 }
546 
547 static int
548 #ifdef HAVE_USERNS_IOPS_GETATTR
549 zpl_shares_getattr_impl(struct user_namespace *user_ns,
550     const struct path *path, struct kstat *stat, u32 request_mask,
551     unsigned int query_flags)
552 #elif defined(HAVE_IDMAP_IOPS_GETATTR)
553 zpl_shares_getattr_impl(struct mnt_idmap *user_ns,
554     const struct path *path, struct kstat *stat, u32 request_mask,
555     unsigned int query_flags)
556 #else
557 zpl_shares_getattr_impl(const struct path *path, struct kstat *stat,
558     u32 request_mask, unsigned int query_flags)
559 #endif
560 {
561 	(void) request_mask, (void) query_flags;
562 	struct inode *ip = path->dentry->d_inode;
563 	zfsvfs_t *zfsvfs = ITOZSB(ip);
564 	znode_t *dzp;
565 	int error;
566 
567 	if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
568 		return (error);
569 
570 	if (zfsvfs->z_shares_dir == 0) {
571 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
572 #ifdef HAVE_GENERIC_FILLATTR_USERNS
573 		generic_fillattr(user_ns, path->dentry->d_inode, stat);
574 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
575 		generic_fillattr(user_ns, path->dentry->d_inode, stat);
576 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
577 	generic_fillattr(user_ns, request_mask, ip, stat);
578 #else
579 		(void) user_ns;
580 #endif
581 #else
582 		generic_fillattr(path->dentry->d_inode, stat);
583 #endif
584 		stat->nlink = stat->size = 2;
585 		stat->atime = current_time(ip);
586 		zpl_exit(zfsvfs, FTAG);
587 		return (0);
588 	}
589 
590 	error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
591 	if (error == 0) {
592 #ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
593 		error = -zfs_getattr_fast(user_ns, request_mask, ZTOI(dzp),
594 		    stat);
595 #elif (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
596 		error = -zfs_getattr_fast(user_ns, ZTOI(dzp), stat);
597 #else
598 		error = -zfs_getattr_fast(kcred->user_ns, ZTOI(dzp), stat);
599 #endif
600 		iput(ZTOI(dzp));
601 	}
602 
603 	zpl_exit(zfsvfs, FTAG);
604 	ASSERT3S(error, <=, 0);
605 
606 	return (error);
607 }
608 ZPL_GETATTR_WRAPPER(zpl_shares_getattr);
609 
610 /*
611  * The '.zfs/shares' directory file operations.
612  */
613 const struct file_operations zpl_fops_shares = {
614 	.open		= zpl_common_open,
615 	.llseek		= generic_file_llseek,
616 	.read		= generic_read_dir,
617 	.iterate_shared	= zpl_shares_iterate,
618 };
619 
620 /*
621  * The '.zfs/shares' directory inode operations.
622  */
623 const struct inode_operations zpl_ops_shares = {
624 	.lookup		= zpl_shares_lookup,
625 	.getattr	= zpl_shares_getattr,
626 };
627