1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
14 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
15 * LLNL-CODE-403049.
16 * Rewritten for Linux by:
17 * Rohan Puri <rohan.puri15@gmail.com>
18 * Brian Behlendorf <behlendorf1@llnl.gov>
19 * Copyright (c) 2026, TrueNAS.
20 */
21
22 #include <sys/zfs_znode.h>
23 #include <sys/zfs_vfsops.h>
24 #include <sys/zfs_vnops.h>
25 #include <sys/zfs_ctldir.h>
26 #include <sys/zpl.h>
27 #include <sys/dmu.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/zap.h>
30 #include <linux/version.h>
31
32 /*
33 * Common open routine. Disallow any write access.
34 */
35 static int
zpl_common_open(struct inode * ip,struct file * filp)36 zpl_common_open(struct inode *ip, struct file *filp)
37 {
38 if (blk_mode_is_open_write(filp->f_mode))
39 return (-EACCES);
40
41 return (generic_file_open(ip, filp));
42 }
43
44 /*
45 * Get root directory contents.
46 */
47 static int
zpl_root_iterate(struct file * filp,struct dir_context * ctx)48 zpl_root_iterate(struct file *filp, struct dir_context *ctx)
49 {
50 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
51 int error = 0;
52
53 if (zfsvfs->z_show_ctldir == ZFS_SNAPDIR_DISABLED) {
54 return (SET_ERROR(ENOENT));
55 }
56
57 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
58 return (error);
59
60 if (!dir_emit_dots(filp, ctx))
61 goto out;
62
63 if (ctx->pos == 2) {
64 if (!dir_emit(ctx, ZFS_SNAPDIR_NAME,
65 strlen(ZFS_SNAPDIR_NAME), ZFSCTL_INO_SNAPDIR, DT_DIR))
66 goto out;
67
68 ctx->pos++;
69 }
70
71 if (ctx->pos == 3) {
72 if (!dir_emit(ctx, ZFS_SHAREDIR_NAME,
73 strlen(ZFS_SHAREDIR_NAME), ZFSCTL_INO_SHARES, DT_DIR))
74 goto out;
75
76 ctx->pos++;
77 }
78 out:
79 zpl_exit(zfsvfs, FTAG);
80
81 return (error);
82 }
83
84 /*
85 * Get root directory attributes.
86 */
87 ZPL_IDMAP_IOP_DEFINE(int, zpl_root_getattr, 4,
88 const struct path *, path, struct kstat *, stat, u32, request_mask,
89 unsigned int, query_flags)
90 {
91 (void) request_mask, (void) query_flags;
92 struct inode *ip = path->dentry->d_inode;
93 zpl_generic_fillattr(idmap, request_mask, ip, stat);
94 stat->atime = current_time(ip);
95
96 return (0);
97 }
98
99 static struct dentry *
zpl_root_lookup(struct inode * dip,struct dentry * dentry,unsigned int flags)100 zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
101 {
102 cred_t *cr = CRED();
103 struct inode *ip;
104 int error;
105
106 crhold(cr);
107 error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL);
108 ASSERT3S(error, <=, 0);
109 crfree(cr);
110
111 if (error) {
112 if (error == -ENOENT)
113 return (d_splice_alias(NULL, dentry));
114 else
115 return (ERR_PTR(error));
116 }
117
118 return (d_splice_alias(ip, dentry));
119 }
120
121 /*
122 * The '.zfs' control directory file and inode operations.
123 */
124 const struct file_operations zpl_fops_root = {
125 .open = zpl_common_open,
126 .llseek = generic_file_llseek,
127 .read = generic_read_dir,
128 .iterate_shared = zpl_root_iterate,
129 };
130
131 const struct inode_operations zpl_ops_root = {
132 .lookup = zpl_root_lookup,
133 .getattr = zpl_root_getattr,
134 };
135
136 /*
137 * Snapdir control nodes. The snapdir system is described in full at the top of
138 * zfs_ctldir.c.
139 *
140 * This file has the dentry operations that manage the kernel's path traversal
141 * into the snapshot mounts, and coordinate between the d_manage and
142 * d_automount handlers to get a snapshot online safely.
143 */
144
145 /*
146 * Lookups (zpl_snapdir_lookup() or zpl_snapdir_revalidate()) with flags
147 * matching this criteria will result in an automount being triggered when
148 * walking a snapshot control dentry. They should roughly match the criteria
149 * laid out in the kernel's follow_automount() function. Read the grand theory
150 * for more on why this is necessary.
151 */
152 static inline bool
lookup_want_automount(unsigned int flags)153 lookup_want_automount(unsigned int flags)
154 {
155 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | LOOKUP_OPEN |
156 LOOKUP_CREATE | LOOKUP_AUTOMOUNT)))
157 return (false);
158
159 #ifdef LOOKUP_NO_XDEV
160 /* LOOKUP_NO_XDEV added in 5.6 to support openat2(RESOLVE_NO_XDEV). */
161 if (flags & LOOKUP_NO_XDEV)
162 return (false);
163 #endif
164
165 return (true);
166 }
167
168 static int
zpl_snapdir_manage(const struct path * path,bool rcu_walk)169 zpl_snapdir_manage(const struct path *path, bool rcu_walk)
170 {
171 struct dentry *dentry = path->dentry;
172 zfs_snapentry_t *se = dentry->d_fsdata;
173
174 if (rcu_walk) {
175 /*
176 * In RCU-walk mode, we are under the RCU lock and must not
177 * block, and should not slow down if we can help it (eg take a
178 * spinlock).
179 *
180 * Note that this is the only place it's safe to access flag
181 * bits without se_mtx held, and that's only because of this
182 * unique context. SE_BUSY will only ever change under lock,
183 * as the last thing before the lock is released.
184 *
185 * If we catch SE_BUSY just before it's cleared, then we fall
186 * back to REF-walk when we didn't need to, but that is always
187 * safe.
188 *
189 * If we catch it just before it's set, then we will proceed,
190 * however RCU-walk cannot trigger automount. If there's no
191 * mount there, we will simply return as such; if there is
192 * a mount, we will walk into it. In the worst cases, we miss
193 * the the mount happening and can't walk into it, or we enter
194 * a mount and prevent it being unmounted. Both these are
195 * safe and defensible behaviour for a walk racing a mount or
196 * unmount.
197 */
198 if (SE_TEST(se, SE_BUSY))
199 return (-ECHILD);
200
201 /* dentry is stable, walk may proceed. */
202 return (0);
203 }
204
205 /*
206 * REF-walk. Unlocked, and may block. Valid returns are:
207 * 0 proceed, enter existing mount or start automount
208 * EISDIR don't automount, caller is not entering
209 * ESTALE restart the path walk (dentry invalidated, raced)
210 */
211
212 mutex_enter(&se->se_mtx);
213 if (se->se_mount_task == current) {
214 /*
215 * Caller is our own task, reentering through follow_down.
216 * Allow it to proceed.
217 */
218 mutex_exit(&se->se_mtx);
219 return (0);
220 }
221
222 /*
223 * Localise the mount intent before the busy wait. At this point we
224 * know we definitely want to mount, but we might sleep on BUSY below,
225 * and then another mount completing under a different parent will
226 * clear the bit while we wait.
227 *
228 * Note that there is a tiny gap here: if this thread is preempted
229 * between d_revalidate() (which set the flag) and this read then
230 * another walk could clear it. However, that's a few non-sleeping
231 * instructions in the pathwalk fast-path and so vanishingly unlikely.
232 */
233 bool want_mount = SE_TEST(se, SE_WANT_MOUNT);
234
235 /*
236 * Wait for any mount/unmount to complete. This includes the mount
237 * task that we just let through.
238 */
239 while (SE_TEST(se, SE_BUSY))
240 cv_wait(&se->se_cv, &se->se_mtx);
241
242 /*
243 * There can't be a mount task any longer, because otherwise we'd
244 * be busy above.
245 */
246 ASSERT0P(se->se_mount_task);
247
248 if (d_unhashed(dentry)) {
249 /*
250 * dentry was invalidated while we were sleeping. That might be
251 * expiry, but could also be the underlying dataset being
252 * unmounted. Force pathwalk to start again from the beginning.
253 */
254 mutex_exit(&se->se_mtx);
255 return (-ESTALE);
256 }
257
258 if (!dentry->d_inode) {
259 /*
260 * dentry has no inode. Can happen when the snapshot is
261 * destroyed by rmdir'ing the snapdir, but the dentry hasn't
262 * been unhashed or invalidated yet. Returning EISDIR
263 * disables automount for this walk.
264 */
265 mutex_exit(&se->se_mtx);
266 return (-EISDIR);
267 }
268
269 /*
270 * Check for an existing mount. We have to use follow_down_one() (ie
271 * lookup_mnt()) here because mounts are keyed on path (parent mount +
272 * dentry), so eg d_mountpoint() would find _any_ mount, not
273 * necessarily _this_ mount.
274 */
275 struct path check_path = *path;
276 path_get(&check_path);
277 bool mounted = follow_down_one(&check_path);
278 path_put(&check_path);
279
280 if (mounted) {
281 /*
282 * Something mounted here, let the VFS have it and hope its
283 * the right thing!
284 */
285 SE_CLEAR(se, SE_WANT_MOUNT);
286 mutex_exit(&se->se_mtx);
287 return (0);
288 }
289
290 /* Nothing mounted under this parent, continue. */
291
292 if (!want_mount) {
293 /*
294 * Whatever triggered this walk is not looking for anything
295 * "inside" the mount, so tell the VFS not to attempt
296 * automount.
297 */
298 mutex_exit(&se->se_mtx);
299 return (-EISDIR);
300 }
301
302 /* We are the mount task now. */
303 se->se_mount_task = current;
304 SE_SET(se, SE_BUSY);
305 mutex_exit(&se->se_mtx);
306
307 struct path am_path = *path;
308 path_get(&am_path);
309 int err = zpl_follow_down(&am_path, LOOKUP_AUTOMOUNT);
310 if (err) {
311 /* Mount failed or some other internal error. */
312 path_put(&am_path);
313 err = -ESTALE;
314 goto out;
315 }
316
317 if (am_path.dentry != am_path.mnt->mnt_root ||
318 am_path.mnt->mnt_sb->s_type != &zpl_fs_type) {
319 /*
320 * follow_down() succeeded but ended up somewhere not the root
321 * of a ZFS filesystem, nothing more we can do.
322 */
323 path_put(&am_path);
324 err = -ESTALE;
325 goto out;
326 }
327
328 zfsvfs_t *zfsvfs = am_path.mnt->mnt_sb->s_fs_info;
329 if (dmu_objset_spa(zfsvfs->z_os) != se->se_spa ||
330 dmu_objset_id(zfsvfs->z_os) != se->se_objsetid) {
331 /*
332 * follow_down() ended up in a ZFS filesystem, but not the
333 * snapshot we expected. Again, nothing more we can do.
334 */
335 path_put(&am_path);
336 err = -ESTALE;
337 goto out;
338 }
339
340 /* Finalise and publish the mount. */
341 zfsctl_snapshot_finish_mount(se, am_path.mnt);
342
343 path_put(&am_path);
344
345 out:
346 mutex_enter(&se->se_mtx);
347 ASSERT3P(se->se_mount_task, ==, current);
348 ASSERT(SE_TEST(se, SE_BUSY));
349
350 if (err == 0)
351 /* Something mounted, hopefully the thing we wanted! */
352 SE_CLEAR(se, SE_WANT_MOUNT);
353
354 se->se_mount_task = NULL;
355 SE_CLEAR(se, SE_BUSY);
356 cv_broadcast(&se->se_cv);
357 mutex_exit(&se->se_mtx);
358
359 return (err);
360 }
361
362 static struct vfsmount *
zpl_snapdir_automount(struct path * path)363 zpl_snapdir_automount(struct path *path)
364 {
365 struct dentry *dentry = path->dentry;
366 zfs_snapentry_t *se = dentry->d_fsdata;
367
368 /*
369 * Only the mounting task from zpl_snapdir_manage() can make it here.
370 * However, before 5.5 it would enter twice, as the mount loop (in
371 * follow_managed(), which is now __traverse_mounts()) would not reload
372 * the dentry flags, so would not "see" the automount. This was ok
373 * because nothing does the d_manage() double-entry thing we're doing,
374 * and the filesystem itself would just return the mount it had already
375 * created.
376 *
377 * So if anything arrives here when se_mount_task is NULL, we simply
378 * return NULL here. On those older kernels, that should cause the path
379 * to be reevaluated and enter the mount.
380 */
381 if (se->se_mount_task == NULL)
382 return (NULL);
383
384 /*
385 * This has better be the mounting task, or something very strange
386 * has happened.
387 */
388 ASSERT3P(se->se_mount_task, ==, current);
389 ASSERT(SE_TEST(se, SE_BUSY));
390 ASSERT3P(dentry->d_inode, !=, NULL);
391
392 struct vfsmount *mnt = NULL;
393 int error = -zfsctl_snapshot_mount(path, &mnt);
394
395 if (error)
396 return (ERR_PTR(error));
397
398 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 16, 0)
399 /*
400 * Before torvalds/linux@006ff7498fe89 (6.16), the kernel's internal
401 * expiry machinery could expire any mount that had only a single
402 * reference, reasoning that that reference must be the mountpoint
403 * itself, and thus there are no users.
404 *
405 * To work around this, filesystems implementing d_automount were
406 * expected to return a mount with two refs, the extra to cause expiry
407 * to assume the mount is in use and skip it. finish_automount()
408 * enforces this by calling BUG() if the count is <2, and will release
409 * the extra once the graft is completed.
410 *
411 * 6.16 changes this to simply have the expiry task ignore mounts that
412 * aren't mounted yet, making life much easier for filesystems.
413 *
414 * There's no test that can really detect this, so we go for a simple
415 * version check, and take an extra reference on older kernels.
416 *
417 * This should be safe for RHEL too; at time of writing, EL8.10 (4.18+)
418 * and EL9.8 (5.14) kernels have not backported this change.
419 */
420 mntget(mnt);
421 #endif
422
423 return (mnt);
424 }
425
426 /*
427 * Kernel will revalidate when the dentry may have changed state during
428 * RCU-walk (eg when the dentry is reused on splice, see zpl_snapdir_lookup()).
429 * If we have an inode, then update the flags and declare it good.
430 *
431 * For negative dentries, no revalidation necessary - they're either brand-new
432 * and not yet live (eg between lookup->mkdir) or they've been invalidated
433 * because the mount is dead and we want a new one on next lookup.
434 */
435 #ifdef HAVE_D_REVALIDATE_4ARGS
436 static int
zpl_snapdir_revalidate(struct inode * dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)437 zpl_snapdir_revalidate(struct inode *dir, const struct qstr *name,
438 struct dentry *dentry, unsigned int flags)
439 #else
440 static int
441 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags)
442 #endif
443 {
444 zfs_snapentry_t *se = dentry->d_fsdata;
445
446 if (dentry->d_inode) {
447 if (lookup_want_automount(flags))
448 SE_SET(se, SE_WANT_MOUNT);
449 atomic_store_64(&se->se_atime, jiffies);
450 return (1);
451 }
452
453 return (0);
454 }
455
456 /* Kernel is done with the dentry, tear down the snapentry too. */
457 static void
zpl_snapdir_release(struct dentry * dentry)458 zpl_snapdir_release(struct dentry *dentry)
459 {
460 spin_lock(&dentry->d_lock);
461 zfs_snapentry_t *se = dentry->d_fsdata;
462 dentry->d_fsdata = NULL;
463 spin_unlock(&dentry->d_lock);
464
465 /*
466 * Release can be called more than once if part of the release was
467 * deferred, so we might have already cleaned up. Do nothing if so.
468 */
469 if (se == NULL)
470 return;
471
472 zfsctl_snapshot_timer_clear(se);
473
474 mutex_destroy(&se->se_mtx);
475 cv_destroy(&se->se_cv);
476
477 kmem_free(se, sizeof (zfs_snapentry_t));
478 }
479
480 static const struct dentry_operations zpl_dops_snapdirs = {
481 .d_manage = zpl_snapdir_manage,
482 .d_automount = zpl_snapdir_automount,
483 .d_revalidate = zpl_snapdir_revalidate,
484 .d_release = zpl_snapdir_release,
485 };
486
487 /*
488 * Snapdir control dentries need a zfs_snapentry_t to track a possible mount
489 * and custom dentry operations to coordinate access against management of
490 * the mount. This sets all that up on the given dentry.
491 */
492 static void
zpl_snapdir_init_snapentry(struct dentry * dentry)493 zpl_snapdir_init_snapentry(struct dentry *dentry)
494 {
495 /*
496 * The snapentry starts off as an empty stub. It will be filled in
497 * later if/when the mount actually happens.
498 */
499 zfs_snapentry_t *se = kmem_zalloc(sizeof (zfs_snapentry_t), KM_SLEEP);
500
501 se->se_taskqid = TASKQID_INVALID;
502 mutex_init(&se->se_mtx, NULL, MUTEX_DEFAULT, NULL);
503 cv_init(&se->se_cv, NULL, CV_DEFAULT, NULL);
504 se->se_flags = 0;
505
506 /*
507 * We have to take the lock here as the dentry might be about to be
508 * reused and so on a cleanup list or similar.
509 */
510 spin_lock(&dentry->d_lock);
511 ASSERT0P(dentry->d_fsdata);
512
513 se->se_dentry = dentry;
514 dentry->d_fsdata = se;
515
516 /*
517 * Full set of "op" flags. The dentry may have other flags tracking
518 * its state, so we want to mask these off instead of setting it to
519 * NULL.
520 */
521 static const unsigned int op_flags =
522 DCACHE_OP_HASH | DCACHE_OP_COMPARE |
523 DCACHE_OP_REVALIDATE | DCACHE_OP_DELETE |
524 DCACHE_OP_PRUNE | DCACHE_OP_WEAK_REVALIDATE | DCACHE_OP_REAL;
525
526 #ifdef HAVE_D_SET_D_OP
527 /*
528 * d_set_d_op() will set the DCACHE_OP_ flags according to what it
529 * finds in the passed dentry_operations, so we don't have to.
530 *
531 * We clear the flags and the old op table before calling d_set_d_op()
532 * because issues a warning when the dentry operations table is already
533 * set.
534 */
535 dentry->d_op = NULL;
536 dentry->d_flags &= ~op_flags;
537 d_set_d_op(dentry, &zpl_dops_snapdirs);
538 dentry->d_flags |= DCACHE_MANAGE_TRANSIT | DCACHE_NEED_AUTOMOUNT;
539 #else
540 /*
541 * Since 6.17 there's no exported way to modify dentry ops, so we have
542 * to reach in and do it ourselves. We have the lock, so this should
543 * be safe.
544 *
545 * Note that the DCACHE_OP_ flags must match the associated ops in
546 * zpl_dops_snapdirs.
547 */
548 dentry->d_op = &zpl_dops_snapdirs;
549 dentry->d_flags &= ~op_flags;
550 dentry->d_flags |= DCACHE_OP_REVALIDATE |
551 DCACHE_MANAGE_TRANSIT | DCACHE_NEED_AUTOMOUNT;
552 #endif
553
554 se->se_dentry = dentry;
555
556 spin_unlock(&dentry->d_lock);
557 }
558
559 static struct dentry *
zpl_snapdir_lookup(struct inode * dip,struct dentry * dentry,unsigned int flags)560 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
561 unsigned int flags)
562 {
563 fstrans_cookie_t cookie;
564 cred_t *cr = CRED();
565 struct inode *ip = NULL;
566 int error;
567
568 crhold(cr);
569 cookie = spl_fstrans_mark();
570 error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,
571 0, cr, NULL, NULL);
572 ASSERT3S(error, <=, 0);
573 spl_fstrans_unmark(cookie);
574 crfree(cr);
575
576 if (error && error != -ENOENT)
577 return (ERR_PTR(error));
578
579 ASSERT(error == 0 || ip == NULL);
580
581 zpl_snapdir_init_snapentry(dentry);
582 zfs_snapentry_t *se = dentry->d_fsdata;
583
584 if (lookup_want_automount(flags))
585 SE_SET(se, SE_WANT_MOUNT);
586
587 struct dentry *old = d_splice_alias(ip, dentry);
588 if (old == NULL || IS_ERR(old))
589 return (old);
590
591 /*
592 * Previous dentry was invalidated and waiting to be cleaned up, so
593 * d_splice_alias() has re-lifed it and thrown our new one away. So we
594 * have to get it back into shape.
595 *
596 * The dentry is already published and an RCU-walk lookup may already
597 * be in progress, however it is also on a wait list until this call
598 * returns, and REF-walk can't happen until that list is cleared. So
599 * we're safe to make adjustments here provided the RCU-walk won't
600 * see them. d_revalidate()->zpl_snapdir_revalidate() will be called
601 * on our dentries on that list before the RCU-walk, which will
602 * correctly set SE_WANT_MOUNT.
603 *
604 * However, there may not be a walk in progress, and so the VFS will
605 * trust the dentry returned here. So we also need to correctly set
606 * SE_WANT_MOUNT here too.
607 *
608 * Any other state from the previous version will be cleaned up
609 * before actually repopulating it fully in
610 * zpl_snapdir_automount()->zfsctl_snapshot_mount().
611 */
612 se = old->d_fsdata;
613 if (lookup_want_automount(flags))
614 SE_SET(se, SE_WANT_MOUNT);
615
616 return (old);
617 }
618
619 static int
zpl_snapdir_iterate(struct file * filp,struct dir_context * ctx)620 zpl_snapdir_iterate(struct file *filp, struct dir_context *ctx)
621 {
622 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
623 fstrans_cookie_t cookie;
624 char snapname[MAXNAMELEN];
625 boolean_t case_conflict;
626 uint64_t id, pos;
627 int error = 0;
628
629 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
630 return (error);
631 cookie = spl_fstrans_mark();
632
633 if (!dir_emit_dots(filp, ctx))
634 goto out;
635
636 /* Start the position at 0 if it already emitted . and .. */
637 pos = (ctx->pos == 2 ? 0 : ctx->pos);
638 while (error == 0) {
639 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
640 error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN,
641 snapname, &id, &pos, &case_conflict);
642 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
643 if (error)
644 goto out;
645
646 if (!dir_emit(ctx, snapname, strlen(snapname),
647 ZFSCTL_INO_SHARES - id, DT_DIR))
648 goto out;
649
650 ctx->pos = pos;
651 }
652 out:
653 spl_fstrans_unmark(cookie);
654 zpl_exit(zfsvfs, FTAG);
655
656 if (error == -ENOENT)
657 return (0);
658
659 return (error);
660 }
661
662 ZPL_IDMAP_IOP_DEFINE(int, zpl_snapdir_rename, 5,
663 struct inode *, sdip, struct dentry *, sdentry,
664 struct inode *, tdip, struct dentry *, tdentry, unsigned int, flags)
665 {
666 cred_t *cr = CRED();
667 int error;
668
669 /* We probably don't want to support renameat2(2) in ctldir */
670 if (flags)
671 return (-EINVAL);
672
673 crhold(cr);
674 error = -zfsctl_snapdir_rename(sdip, sdentry, tdip, tdentry, cr);
675 ASSERT3S(error, <=, 0);
676 crfree(cr);
677
678 return (error);
679 }
680
681 static int
zpl_snapdir_rmdir(struct inode * dip,struct dentry * dentry)682 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
683 {
684 cred_t *cr = CRED();
685 int error;
686
687 crhold(cr);
688 error = -zfsctl_snapdir_remove(dip, dentry, cr);
689 ASSERT3S(error, <=, 0);
690 crfree(cr);
691
692 return (error);
693 }
694
695 #if defined(HAVE_MKDIR_DENTRY_RETURN)
696 ZPL_IDMAP_IOP_DEFINE(struct dentry *, zpl_snapdir_mkdir, 3,
697 struct inode *, dip, struct dentry *, dentry, umode_t, mode)
698 #else
699 ZPL_IDMAP_IOP_DEFINE(int, zpl_snapdir_mkdir, 3,
700 struct inode *, dip, struct dentry *, dentry, umode_t, mode)
701 #endif
702 {
703 cred_t *cr = CRED();
704 vattr_t *vap;
705 struct inode *ip;
706 int error;
707
708 crhold(cr);
709 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
710 zpl_vap_init(vap, dip, mode | S_IFDIR, cr, idmap);
711
712 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
713 if (error == 0)
714 d_instantiate(dentry, ip);
715
716 kmem_free(vap, sizeof (vattr_t));
717 ASSERT3S(error, <=, 0);
718 crfree(cr);
719
720 #if defined(HAVE_MKDIR_DENTRY_RETURN)
721 return (ERR_PTR(error));
722 #else
723 return (error);
724 #endif
725 }
726
727 /*
728 * Get snapshot directory attributes.
729 */
730 ZPL_IDMAP_IOP_DEFINE(int, zpl_snapdir_getattr, 4,
731 const struct path *, path, struct kstat *, stat, u32, request_mask,
732 unsigned int, query_flags)
733 {
734 (void) request_mask, (void) query_flags;
735 struct inode *ip = path->dentry->d_inode;
736 zfsvfs_t *zfsvfs = ITOZSB(ip);
737 int error;
738
739 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
740 return (error);
741
742 zpl_generic_fillattr(idmap, request_mask, ip, stat);
743 stat->nlink = stat->size = 2;
744
745 dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);
746 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {
747 uint64_t snap_count;
748 int err = zap_count(
749 dmu_objset_pool(ds->ds_objset)->dp_meta_objset,
750 dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);
751 if (err != 0) {
752 zpl_exit(zfsvfs, FTAG);
753 return (-err);
754 }
755 stat->nlink += snap_count;
756 }
757
758 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
759 stat->atime = current_time(ip);
760 zpl_exit(zfsvfs, FTAG);
761
762 return (0);
763 }
764
765 /*
766 * The '.zfs/snapshot' directory file operations. These mainly control
767 * generating the list of available snapshots when doing an 'ls' in the
768 * directory. See zpl_snapdir_readdir().
769 */
770 const struct file_operations zpl_fops_snapdir = {
771 .open = zpl_common_open,
772 .llseek = generic_file_llseek,
773 .read = generic_read_dir,
774 .iterate_shared = zpl_snapdir_iterate,
775
776 };
777
778 /*
779 * The '.zfs/snapshot' directory inode operations. These mainly control
780 * creating an inode for a snapshot directory and initializing the needed
781 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
782 */
783 const struct inode_operations zpl_ops_snapdir = {
784 .lookup = zpl_snapdir_lookup,
785 .getattr = zpl_snapdir_getattr,
786 .rename = zpl_snapdir_rename,
787 .rmdir = zpl_snapdir_rmdir,
788 .mkdir = zpl_snapdir_mkdir,
789 };
790
791 static struct dentry *
zpl_shares_lookup(struct inode * dip,struct dentry * dentry,unsigned int flags)792 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
793 unsigned int flags)
794 {
795 fstrans_cookie_t cookie;
796 cred_t *cr = CRED();
797 struct inode *ip = NULL;
798 int error;
799
800 crhold(cr);
801 cookie = spl_fstrans_mark();
802 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
803 0, cr, NULL, NULL);
804 ASSERT3S(error, <=, 0);
805 spl_fstrans_unmark(cookie);
806 crfree(cr);
807
808 if (error) {
809 if (error == -ENOENT)
810 return (d_splice_alias(NULL, dentry));
811 else
812 return (ERR_PTR(error));
813 }
814
815 return (d_splice_alias(ip, dentry));
816 }
817
818 static int
zpl_shares_iterate(struct file * filp,struct dir_context * ctx)819 zpl_shares_iterate(struct file *filp, struct dir_context *ctx)
820 {
821 fstrans_cookie_t cookie;
822 cred_t *cr = CRED();
823 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
824 znode_t *dzp;
825 int error = 0;
826
827 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
828 return (error);
829 cookie = spl_fstrans_mark();
830
831 if (zfsvfs->z_shares_dir == 0) {
832 dir_emit_dots(filp, ctx);
833 goto out;
834 }
835
836 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
837 if (error)
838 goto out;
839
840 crhold(cr);
841 error = -zfs_readdir(ZTOI(dzp), ctx, cr);
842 crfree(cr);
843
844 iput(ZTOI(dzp));
845 out:
846 spl_fstrans_unmark(cookie);
847 zpl_exit(zfsvfs, FTAG);
848 ASSERT3S(error, <=, 0);
849
850 return (error);
851 }
852
853 ZPL_IDMAP_IOP_DEFINE(int, zpl_shares_getattr, 4,
854 const struct path *, path, struct kstat *, stat, u32, request_mask,
855 unsigned int, query_flags)
856 {
857 (void) request_mask, (void) query_flags;
858 struct inode *ip = path->dentry->d_inode;
859 zfsvfs_t *zfsvfs = ITOZSB(ip);
860 znode_t *dzp;
861 int error;
862
863 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
864 return (error);
865
866 if (zfsvfs->z_shares_dir == 0) {
867 zpl_generic_fillattr(idmap, request_mask, ip, stat);
868 stat->nlink = stat->size = 2;
869 stat->atime = current_time(ip);
870 zpl_exit(zfsvfs, FTAG);
871 return (0);
872 }
873
874 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
875 if (error == 0) {
876 error = -zfs_getattr_fast(idmap, request_mask, ZTOI(dzp),
877 stat);
878 iput(ZTOI(dzp));
879 }
880
881 zpl_exit(zfsvfs, FTAG);
882 ASSERT3S(error, <=, 0);
883
884 return (error);
885 }
886
887 /*
888 * The '.zfs/shares' directory file operations.
889 */
890 const struct file_operations zpl_fops_shares = {
891 .open = zpl_common_open,
892 .llseek = generic_file_llseek,
893 .read = generic_read_dir,
894 .iterate_shared = zpl_shares_iterate,
895 };
896
897 /*
898 * The '.zfs/shares' directory inode operations.
899 */
900 const struct inode_operations zpl_ops_shares = {
901 .lookup = zpl_shares_lookup,
902 .getattr = zpl_shares_getattr,
903 };
904