c2b8b75e6f003b50762ff5337b4526ee435899fa
[zfs.git] / module / zfs / zfs_ctldir.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24
25 /*
26  * ZFS control directory (a.k.a. ".zfs")
27  *
28  * This directory provides a common location for all ZFS meta-objects.
29  * Currently, this is only the 'snapshot' directory, but this may expand in the
30  * future.  The elements are built using the GFS primitives, as the hierarchy
31  * does not actually exist on disk.
32  *
33  * For 'snapshot', we don't want to have all snapshots always mounted, because
34  * this would take up a huge amount of space in /etc/mtab.  We have three
35  * types of objects:
36  *
37  *      ctldir ------> snapshotdir -------> snapshot
38  *                                             |
39  *                                             |
40  *                                             V
41  *                                         mounted fs
42  *
43  * The 'snapshot' node contains just enough information to lookup '..' and act
44  * as a mountpoint for the snapshot.  Whenever we lookup a specific snapshot, we
45  * perform an automount of the underlying filesystem and return the
46  * corresponding vnode.
47  *
48  * All mounts are handled automatically by the kernel, but unmounts are
49  * (currently) handled from user land.  The main reason is that there is no
50  * reliable way to auto-unmount the filesystem when it's "no longer in use".
51  * When the user unmounts a filesystem, we call zfsctl_unmount(), which
52  * unmounts any snapshots within the snapshot directory.
53  *
54  * The '.zfs', '.zfs/snapshot', and all directories created under
55  * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
56  * share the same vfs_t as the head filesystem (what '.zfs' lives under).
57  *
58  * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
59  * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
60  * However, vnodes within these mounted on file systems have their v_vfsp
61  * fields set to the head filesystem to make NFS happy (see
62  * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
63  * so that it cannot be freed until all snapshots have been unmounted.
64  */
65
66 #ifdef HAVE_ZPL
67
68 #include <fs/fs_subr.h>
69 #include <sys/zfs_ctldir.h>
70 #include <sys/zfs_ioctl.h>
71 #include <sys/zfs_vfsops.h>
72 #include <sys/vfs_opreg.h>
73 #include <sys/gfs.h>
74 #include <sys/stat.h>
75 #include <sys/dmu.h>
76 #include <sys/dsl_deleg.h>
77 #include <sys/mount.h>
78 #include <sys/sunddi.h>
79
80 #include "zfs_namecheck.h"
81
82 typedef struct zfsctl_node {
83         gfs_dir_t       zc_gfs_private;
84         uint64_t        zc_id;
85         timestruc_t     zc_cmtime;      /* ctime and mtime, always the same */
86 } zfsctl_node_t;
87
88 typedef struct zfsctl_snapdir {
89         zfsctl_node_t   sd_node;
90         kmutex_t        sd_lock;
91         avl_tree_t      sd_snaps;
92 } zfsctl_snapdir_t;
93
94 typedef struct {
95         char            *se_name;
96         vnode_t         *se_root;
97         avl_node_t      se_node;
98 } zfs_snapentry_t;
99
100 static int
101 snapentry_compare(const void *a, const void *b)
102 {
103         const zfs_snapentry_t *sa = a;
104         const zfs_snapentry_t *sb = b;
105         int ret = strcmp(sa->se_name, sb->se_name);
106
107         if (ret < 0)
108                 return (-1);
109         else if (ret > 0)
110                 return (1);
111         else
112                 return (0);
113 }
114
115 vnodeops_t *zfsctl_ops_root;
116 vnodeops_t *zfsctl_ops_snapdir;
117 vnodeops_t *zfsctl_ops_snapshot;
118 vnodeops_t *zfsctl_ops_shares;
119 vnodeops_t *zfsctl_ops_shares_dir;
120
121 static const fs_operation_def_t zfsctl_tops_root[];
122 static const fs_operation_def_t zfsctl_tops_snapdir[];
123 static const fs_operation_def_t zfsctl_tops_snapshot[];
124 static const fs_operation_def_t zfsctl_tops_shares[];
125
126 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
127 static vnode_t *zfsctl_mknode_shares(vnode_t *);
128 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
129 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
130
131 /*
132  * Root directory elements.  We only have two entries
133  * snapshot and shares.
134  */
135 static gfs_dirent_t zfsctl_root_entries[] = {
136         { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
137         { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
138         { NULL }
139 };
140
141 /* include . and .. in the calculation */
142 #define NROOT_ENTRIES   ((sizeof (zfsctl_root_entries) / \
143     sizeof (gfs_dirent_t)) + 1)
144
145
146 /*
147  * Initialize the various GFS pieces we'll need to create and manipulate .zfs
148  * directories.  This is called from the ZFS init routine, and initializes the
149  * vnode ops vectors that we'll be using.
150  */
151 void
152 zfsctl_init(void)
153 {
154 }
155
156 void
157 zfsctl_fini(void)
158 {
159         /*
160          * Remove vfsctl vnode ops
161          */
162         if (zfsctl_ops_root)
163                 vn_freevnodeops(zfsctl_ops_root);
164         if (zfsctl_ops_snapdir)
165                 vn_freevnodeops(zfsctl_ops_snapdir);
166         if (zfsctl_ops_snapshot)
167                 vn_freevnodeops(zfsctl_ops_snapshot);
168         if (zfsctl_ops_shares)
169                 vn_freevnodeops(zfsctl_ops_shares);
170         if (zfsctl_ops_shares_dir)
171                 vn_freevnodeops(zfsctl_ops_shares_dir);
172
173         zfsctl_ops_root = NULL;
174         zfsctl_ops_snapdir = NULL;
175         zfsctl_ops_snapshot = NULL;
176         zfsctl_ops_shares = NULL;
177         zfsctl_ops_shares_dir = NULL;
178 }
179
180 boolean_t
181 zfsctl_is_node(vnode_t *vp)
182 {
183         return (vn_matchops(vp, zfsctl_ops_root) ||
184             vn_matchops(vp, zfsctl_ops_snapdir) ||
185             vn_matchops(vp, zfsctl_ops_snapshot) ||
186             vn_matchops(vp, zfsctl_ops_shares) ||
187             vn_matchops(vp, zfsctl_ops_shares_dir));
188
189 }
190
191 /*
192  * Return the inode number associated with the 'snapshot' or
193  * 'shares' directory.
194  */
195 /* ARGSUSED */
196 static ino64_t
197 zfsctl_root_inode_cb(vnode_t *vp, int index)
198 {
199         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
200
201         ASSERT(index <= 2);
202
203         if (index == 0)
204                 return (ZFSCTL_INO_SNAPDIR);
205
206         return (zfsvfs->z_shares_dir);
207 }
208
209 /*
210  * Create the '.zfs' directory.  This directory is cached as part of the VFS
211  * structure.  This results in a hold on the vfs_t.  The code in zfs_umount()
212  * therefore checks against a vfs_count of 2 instead of 1.  This reference
213  * is removed when the ctldir is destroyed in the unmount.
214  */
215 void
216 zfsctl_create(zfsvfs_t *zfsvfs)
217 {
218         vnode_t *vp, *rvp;
219         zfsctl_node_t *zcp;
220         uint64_t crtime[2];
221
222         ASSERT(zfsvfs->z_ctldir == NULL);
223
224         vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
225             zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
226             zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
227         zcp = vp->v_data;
228         zcp->zc_id = ZFSCTL_INO_ROOT;
229
230         VERIFY(VFS_ROOT(zfsvfs->z_vfs, &rvp) == 0);
231         VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
232             &crtime, sizeof (crtime)));
233         ZFS_TIME_DECODE(&zcp->zc_cmtime, crtime);
234         VN_RELE(rvp);
235
236         /*
237          * We're only faking the fact that we have a root of a filesystem for
238          * the sake of the GFS interfaces.  Undo the flag manipulation it did
239          * for us.
240          */
241         vp->v_flag &= ~(VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT);
242
243         zfsvfs->z_ctldir = vp;
244 }
245
246 /*
247  * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
248  * There might still be more references if we were force unmounted, but only
249  * new zfs_inactive() calls can occur and they don't reference .zfs
250  */
251 void
252 zfsctl_destroy(zfsvfs_t *zfsvfs)
253 {
254         VN_RELE(zfsvfs->z_ctldir);
255         zfsvfs->z_ctldir = NULL;
256 }
257
258 /*
259  * Given a root znode, retrieve the associated .zfs directory.
260  * Add a hold to the vnode and return it.
261  */
262 vnode_t *
263 zfsctl_root(znode_t *zp)
264 {
265         ASSERT(zfs_has_ctldir(zp));
266         VN_HOLD(zp->z_zfsvfs->z_ctldir);
267         return (zp->z_zfsvfs->z_ctldir);
268 }
269
270 /*
271  * Common open routine.  Disallow any write access.
272  */
273 /* ARGSUSED */
274 static int
275 zfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
276 {
277         if (flags & FWRITE)
278                 return (EACCES);
279
280         return (0);
281 }
282
283 /*
284  * Common close routine.  Nothing to do here.
285  */
286 /* ARGSUSED */
287 static int
288 zfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off,
289     cred_t *cr, caller_context_t *ct)
290 {
291         return (0);
292 }
293
294 /*
295  * Common access routine.  Disallow writes.
296  */
297 /* ARGSUSED */
298 static int
299 zfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr,
300     caller_context_t *ct)
301 {
302         if (flags & V_ACE_MASK) {
303                 if (mode & ACE_ALL_WRITE_PERMS)
304                         return (EACCES);
305         } else {
306                 if (mode & VWRITE)
307                         return (EACCES);
308         }
309
310         return (0);
311 }
312
313 /*
314  * Common getattr function.  Fill in basic information.
315  */
316 static void
317 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
318 {
319         timestruc_t     now;
320
321         vap->va_uid = 0;
322         vap->va_gid = 0;
323         vap->va_rdev = 0;
324         /*
325          * We are a purely virtual object, so we have no
326          * blocksize or allocated blocks.
327          */
328         vap->va_blksize = 0;
329         vap->va_nblocks = 0;
330         vap->va_seq = 0;
331         vap->va_fsid = vp->v_vfsp->vfs_dev;
332         vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
333             S_IROTH | S_IXOTH;
334         vap->va_type = VDIR;
335         /*
336          * We live in the now (for atime).
337          */
338         gethrestime(&now);
339         vap->va_atime = now;
340 }
341
342 /*ARGSUSED*/
343 static int
344 zfsctl_common_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
345 {
346         zfsvfs_t        *zfsvfs = vp->v_vfsp->vfs_data;
347         zfsctl_node_t   *zcp = vp->v_data;
348         uint64_t        object = zcp->zc_id;
349         zfid_short_t    *zfid;
350         int             i;
351
352         ZFS_ENTER(zfsvfs);
353
354         if (fidp->fid_len < SHORT_FID_LEN) {
355                 fidp->fid_len = SHORT_FID_LEN;
356                 ZFS_EXIT(zfsvfs);
357                 return (ENOSPC);
358         }
359
360         zfid = (zfid_short_t *)fidp;
361
362         zfid->zf_len = SHORT_FID_LEN;
363
364         for (i = 0; i < sizeof (zfid->zf_object); i++)
365                 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
366
367         /* .zfs znodes always have a generation number of 0 */
368         for (i = 0; i < sizeof (zfid->zf_gen); i++)
369                 zfid->zf_gen[i] = 0;
370
371         ZFS_EXIT(zfsvfs);
372         return (0);
373 }
374
375
376 /*ARGSUSED*/
377 static int
378 zfsctl_shares_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
379 {
380         zfsvfs_t        *zfsvfs = vp->v_vfsp->vfs_data;
381         znode_t         *dzp;
382         int             error;
383
384         ZFS_ENTER(zfsvfs);
385
386         if (zfsvfs->z_shares_dir == 0) {
387                 ZFS_EXIT(zfsvfs);
388                 return (ENOTSUP);
389         }
390
391         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
392                 error = VOP_FID(ZTOV(dzp), fidp, ct);
393                 VN_RELE(ZTOV(dzp));
394         }
395
396         ZFS_EXIT(zfsvfs);
397         return (error);
398 }
399 /*
400  * .zfs inode namespace
401  *
402  * We need to generate unique inode numbers for all files and directories
403  * within the .zfs pseudo-filesystem.  We use the following scheme:
404  *
405  *      ENTRY                   ZFSCTL_INODE
406  *      .zfs                    1
407  *      .zfs/snapshot           2
408  *      .zfs/snapshot/<snap>    objectid(snap)
409  */
410
411 #define ZFSCTL_INO_SNAP(id)     (id)
412
413 /*
414  * Get root directory attributes.
415  */
416 /* ARGSUSED */
417 static int
418 zfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
419     caller_context_t *ct)
420 {
421         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
422         zfsctl_node_t *zcp = vp->v_data;
423
424         ZFS_ENTER(zfsvfs);
425         vap->va_nodeid = ZFSCTL_INO_ROOT;
426         vap->va_nlink = vap->va_size = NROOT_ENTRIES;
427         vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
428
429         zfsctl_common_getattr(vp, vap);
430         ZFS_EXIT(zfsvfs);
431
432         return (0);
433 }
434
435 /*
436  * Special case the handling of "..".
437  */
438 /* ARGSUSED */
439 int
440 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
441     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
442     int *direntflags, pathname_t *realpnp)
443 {
444         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
445         int err;
446
447         /*
448          * No extended attributes allowed under .zfs
449          */
450         if (flags & LOOKUP_XATTR)
451                 return (EINVAL);
452
453         ZFS_ENTER(zfsvfs);
454
455         if (strcmp(nm, "..") == 0) {
456                 err = VFS_ROOT(dvp->v_vfsp, vpp);
457         } else {
458                 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
459                     cr, ct, direntflags, realpnp);
460         }
461
462         ZFS_EXIT(zfsvfs);
463
464         return (err);
465 }
466
467 static int
468 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
469     caller_context_t *ct)
470 {
471         /*
472          * We only care about ACL_ENABLED so that libsec can
473          * display ACL correctly and not default to POSIX draft.
474          */
475         if (cmd == _PC_ACL_ENABLED) {
476                 *valp = _ACL_ACE_ENABLED;
477                 return (0);
478         }
479
480         return (fs_pathconf(vp, cmd, valp, cr, ct));
481 }
482
483 static const fs_operation_def_t zfsctl_tops_root[] = {
484         { VOPNAME_OPEN,         { .vop_open = zfsctl_common_open }      },
485         { VOPNAME_CLOSE,        { .vop_close = zfsctl_common_close }    },
486         { VOPNAME_IOCTL,        { .error = fs_inval }                   },
487         { VOPNAME_GETATTR,      { .vop_getattr = zfsctl_root_getattr }  },
488         { VOPNAME_ACCESS,       { .vop_access = zfsctl_common_access }  },
489         { VOPNAME_READDIR,      { .vop_readdir = gfs_vop_readdir }      },
490         { VOPNAME_LOOKUP,       { .vop_lookup = zfsctl_root_lookup }    },
491         { VOPNAME_SEEK,         { .vop_seek = fs_seek }                 },
492         { VOPNAME_INACTIVE,     { .vop_inactive = gfs_vop_inactive }    },
493         { VOPNAME_PATHCONF,     { .vop_pathconf = zfsctl_pathconf }     },
494         { VOPNAME_FID,          { .vop_fid = zfsctl_common_fid  }       },
495         { NULL }
496 };
497
498 static int
499 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
500 {
501         objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
502
503         if (snapshot_namecheck(name, NULL, NULL) != 0)
504                 return (EILSEQ);
505         dmu_objset_name(os, zname);
506         if (strlen(zname) + 1 + strlen(name) >= len)
507                 return (ENAMETOOLONG);
508         (void) strcat(zname, "@");
509         (void) strcat(zname, name);
510         return (0);
511 }
512
513 static int
514 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
515 {
516         vnode_t *svp = sep->se_root;
517         int error;
518
519         ASSERT(vn_ismntpt(svp));
520
521         /* this will be dropped by dounmount() */
522         if ((error = vn_vfswlock(svp)) != 0)
523                 return (error);
524
525         VN_HOLD(svp);
526         error = dounmount(vn_mountedvfs(svp), fflags, cr);
527         if (error) {
528                 VN_RELE(svp);
529                 return (error);
530         }
531
532         /*
533          * We can't use VN_RELE(), as that will try to invoke
534          * zfsctl_snapdir_inactive(), which would cause us to destroy
535          * the sd_lock mutex held by our caller.
536          */
537         ASSERT(svp->v_count == 1);
538         gfs_vop_inactive(svp, cr, NULL);
539
540         kmem_free(sep->se_name, strlen(sep->se_name) + 1);
541         kmem_free(sep, sizeof (zfs_snapentry_t));
542
543         return (0);
544 }
545
546 static void
547 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
548 {
549         avl_index_t where;
550         vfs_t *vfsp;
551         refstr_t *pathref;
552         char newpath[MAXNAMELEN];
553         char *tail;
554
555         ASSERT(MUTEX_HELD(&sdp->sd_lock));
556         ASSERT(sep != NULL);
557
558         vfsp = vn_mountedvfs(sep->se_root);
559         ASSERT(vfsp != NULL);
560
561         vfs_lock_wait(vfsp);
562
563         /*
564          * Change the name in the AVL tree.
565          */
566         avl_remove(&sdp->sd_snaps, sep);
567         kmem_free(sep->se_name, strlen(sep->se_name) + 1);
568         sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
569         (void) strcpy(sep->se_name, nm);
570         VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
571         avl_insert(&sdp->sd_snaps, sep, where);
572
573         /*
574          * Change the current mountpoint info:
575          *      - update the tail of the mntpoint path
576          *      - update the tail of the resource path
577          */
578         pathref = vfs_getmntpoint(vfsp);
579         (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
580         VERIFY((tail = strrchr(newpath, '/')) != NULL);
581         *(tail+1) = '\0';
582         ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
583         (void) strcat(newpath, nm);
584         refstr_rele(pathref);
585         vfs_setmntpoint(vfsp, newpath, 0);
586
587         pathref = vfs_getresource(vfsp);
588         (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
589         VERIFY((tail = strrchr(newpath, '@')) != NULL);
590         *(tail+1) = '\0';
591         ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
592         (void) strcat(newpath, nm);
593         refstr_rele(pathref);
594         vfs_setresource(vfsp, newpath, 0);
595
596         vfs_unlock(vfsp);
597 }
598
599 /*ARGSUSED*/
600 static int
601 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
602     cred_t *cr, caller_context_t *ct, int flags)
603 {
604         zfsctl_snapdir_t *sdp = sdvp->v_data;
605         zfs_snapentry_t search, *sep;
606         zfsvfs_t *zfsvfs;
607         avl_index_t where;
608         char from[MAXNAMELEN], to[MAXNAMELEN];
609         char real[MAXNAMELEN];
610         int err;
611
612         zfsvfs = sdvp->v_vfsp->vfs_data;
613         ZFS_ENTER(zfsvfs);
614
615         if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
616                 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
617                     MAXNAMELEN, NULL);
618                 if (err == 0) {
619                         snm = real;
620                 } else if (err != ENOTSUP) {
621                         ZFS_EXIT(zfsvfs);
622                         return (err);
623                 }
624         }
625
626         ZFS_EXIT(zfsvfs);
627
628         err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
629         if (!err)
630                 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
631         if (!err)
632                 err = zfs_secpolicy_rename_perms(from, to, cr);
633         if (err)
634                 return (err);
635
636         /*
637          * Cannot move snapshots out of the snapdir.
638          */
639         if (sdvp != tdvp)
640                 return (EINVAL);
641
642         if (strcmp(snm, tnm) == 0)
643                 return (0);
644
645         mutex_enter(&sdp->sd_lock);
646
647         search.se_name = (char *)snm;
648         if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
649                 mutex_exit(&sdp->sd_lock);
650                 return (ENOENT);
651         }
652
653         err = dmu_objset_rename(from, to, B_FALSE);
654         if (err == 0)
655                 zfsctl_rename_snap(sdp, sep, tnm);
656
657         mutex_exit(&sdp->sd_lock);
658
659         return (err);
660 }
661
662 /* ARGSUSED */
663 static int
664 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
665     caller_context_t *ct, int flags)
666 {
667         zfsctl_snapdir_t *sdp = dvp->v_data;
668         zfs_snapentry_t *sep;
669         zfs_snapentry_t search;
670         zfsvfs_t *zfsvfs;
671         char snapname[MAXNAMELEN];
672         char real[MAXNAMELEN];
673         int err;
674
675         zfsvfs = dvp->v_vfsp->vfs_data;
676         ZFS_ENTER(zfsvfs);
677
678         if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
679
680                 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
681                     MAXNAMELEN, NULL);
682                 if (err == 0) {
683                         name = real;
684                 } else if (err != ENOTSUP) {
685                         ZFS_EXIT(zfsvfs);
686                         return (err);
687                 }
688         }
689
690         ZFS_EXIT(zfsvfs);
691
692         err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
693         if (!err)
694                 err = zfs_secpolicy_destroy_perms(snapname, cr);
695         if (err)
696                 return (err);
697
698         mutex_enter(&sdp->sd_lock);
699
700         search.se_name = name;
701         sep = avl_find(&sdp->sd_snaps, &search, NULL);
702         if (sep) {
703                 avl_remove(&sdp->sd_snaps, sep);
704                 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
705                 if (err)
706                         avl_add(&sdp->sd_snaps, sep);
707                 else
708                         err = dmu_objset_destroy(snapname, B_FALSE);
709         } else {
710                 err = ENOENT;
711         }
712
713         mutex_exit(&sdp->sd_lock);
714
715         return (err);
716 }
717
718 /*
719  * This creates a snapshot under '.zfs/snapshot'.
720  */
721 /* ARGSUSED */
722 static int
723 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t  **vpp,
724     cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
725 {
726         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
727         char name[MAXNAMELEN];
728         int err;
729         static enum symfollow follow = NO_FOLLOW;
730         static enum uio_seg seg = UIO_SYSSPACE;
731
732         if (snapshot_namecheck(dirname, NULL, NULL) != 0)
733                 return (EILSEQ);
734
735         dmu_objset_name(zfsvfs->z_os, name);
736
737         *vpp = NULL;
738
739         err = zfs_secpolicy_snapshot_perms(name, cr);
740         if (err)
741                 return (err);
742
743         if (err == 0) {
744                 err = dmu_objset_snapshot(name, dirname, NULL, NULL,
745                     B_FALSE, B_FALSE, -1);
746                 if (err)
747                         return (err);
748                 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
749         }
750
751         return (err);
752 }
753
754 /*
755  * Lookup entry point for the 'snapshot' directory.  Try to open the
756  * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
757  * Perform a mount of the associated dataset on top of the vnode.
758  */
759 /* ARGSUSED */
760 static int
761 zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
762     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
763     int *direntflags, pathname_t *realpnp)
764 {
765         zfsctl_snapdir_t *sdp = dvp->v_data;
766         objset_t *snap;
767         char snapname[MAXNAMELEN];
768         char real[MAXNAMELEN];
769         char *mountpoint;
770         zfs_snapentry_t *sep, search;
771         struct mounta margs;
772         vfs_t *vfsp;
773         size_t mountpoint_len;
774         avl_index_t where;
775         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
776         int err;
777
778         /*
779          * No extended attributes allowed under .zfs
780          */
781         if (flags & LOOKUP_XATTR)
782                 return (EINVAL);
783
784         ASSERT(dvp->v_type == VDIR);
785
786         /*
787          * If we get a recursive call, that means we got called
788          * from the domount() code while it was trying to look up the
789          * spec (which looks like a local path for zfs).  We need to
790          * add some flag to domount() to tell it not to do this lookup.
791          */
792         if (MUTEX_HELD(&sdp->sd_lock))
793                 return (ENOENT);
794
795         ZFS_ENTER(zfsvfs);
796
797         if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
798                 ZFS_EXIT(zfsvfs);
799                 return (0);
800         }
801
802         if (flags & FIGNORECASE) {
803                 boolean_t conflict = B_FALSE;
804
805                 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
806                     MAXNAMELEN, &conflict);
807                 if (err == 0) {
808                         nm = real;
809                 } else if (err != ENOTSUP) {
810                         ZFS_EXIT(zfsvfs);
811                         return (err);
812                 }
813                 if (realpnp)
814                         (void) strlcpy(realpnp->pn_buf, nm,
815                             realpnp->pn_bufsize);
816                 if (conflict && direntflags)
817                         *direntflags = ED_CASE_CONFLICT;
818         }
819
820         mutex_enter(&sdp->sd_lock);
821         search.se_name = (char *)nm;
822         if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
823                 *vpp = sep->se_root;
824                 VN_HOLD(*vpp);
825                 err = traverse(vpp);
826                 if (err) {
827                         VN_RELE(*vpp);
828                         *vpp = NULL;
829                 } else if (*vpp == sep->se_root) {
830                         /*
831                          * The snapshot was unmounted behind our backs,
832                          * try to remount it.
833                          */
834                         goto domount;
835                 } else {
836                         /*
837                          * VROOT was set during the traverse call.  We need
838                          * to clear it since we're pretending to be part
839                          * of our parent's vfs.
840                          */
841                         (*vpp)->v_flag &= ~VROOT;
842                 }
843                 mutex_exit(&sdp->sd_lock);
844                 ZFS_EXIT(zfsvfs);
845                 return (err);
846         }
847
848         /*
849          * The requested snapshot is not currently mounted, look it up.
850          */
851         err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
852         if (err) {
853                 mutex_exit(&sdp->sd_lock);
854                 ZFS_EXIT(zfsvfs);
855                 /*
856                  * handle "ls *" or "?" in a graceful manner,
857                  * forcing EILSEQ to ENOENT.
858                  * Since shell ultimately passes "*" or "?" as name to lookup
859                  */
860                 return (err == EILSEQ ? ENOENT : err);
861         }
862         if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
863                 mutex_exit(&sdp->sd_lock);
864                 ZFS_EXIT(zfsvfs);
865                 return (ENOENT);
866         }
867
868         sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
869         sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
870         (void) strcpy(sep->se_name, nm);
871         *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
872         avl_insert(&sdp->sd_snaps, sep, where);
873
874         dmu_objset_rele(snap, FTAG);
875 domount:
876         mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) +
877             strlen("/.zfs/snapshot/") + strlen(nm) + 1;
878         mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
879         (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
880             refstr_value(dvp->v_vfsp->vfs_mntpt), nm);
881
882         margs.spec = snapname;
883         margs.dir = mountpoint;
884         margs.flags = MS_SYSSPACE | MS_NOMNTTAB;
885         margs.fstype = "zfs";
886         margs.dataptr = NULL;
887         margs.datalen = 0;
888         margs.optptr = NULL;
889         margs.optlen = 0;
890
891         err = domount("zfs", &margs, *vpp, kcred, &vfsp);
892         kmem_free(mountpoint, mountpoint_len);
893
894         if (err == 0) {
895                 /*
896                  * Return the mounted root rather than the covered mount point.
897                  * Takes the GFS vnode at .zfs/snapshot/<snapname> and returns
898                  * the ZFS vnode mounted on top of the GFS node.  This ZFS
899                  * vnode is the root of the newly created vfsp.
900                  */
901                 VFS_RELE(vfsp);
902                 err = traverse(vpp);
903         }
904
905         if (err == 0) {
906                 /*
907                  * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
908                  *
909                  * This is where we lie about our v_vfsp in order to
910                  * make .zfs/snapshot/<snapname> accessible over NFS
911                  * without requiring manual mounts of <snapname>.
912                  */
913                 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
914                 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
915                 (*vpp)->v_vfsp = zfsvfs->z_vfs;
916                 (*vpp)->v_flag &= ~VROOT;
917         }
918         mutex_exit(&sdp->sd_lock);
919         ZFS_EXIT(zfsvfs);
920
921         /*
922          * If we had an error, drop our hold on the vnode and
923          * zfsctl_snapshot_inactive() will clean up.
924          */
925         if (err) {
926                 VN_RELE(*vpp);
927                 *vpp = NULL;
928         }
929         return (err);
930 }
931
932 /* ARGSUSED */
933 static int
934 zfsctl_shares_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
935     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
936     int *direntflags, pathname_t *realpnp)
937 {
938         zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
939         znode_t *dzp;
940         int error;
941
942         ZFS_ENTER(zfsvfs);
943
944         if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
945                 ZFS_EXIT(zfsvfs);
946                 return (0);
947         }
948
949         if (zfsvfs->z_shares_dir == 0) {
950                 ZFS_EXIT(zfsvfs);
951                 return (ENOTSUP);
952         }
953         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
954                 error = VOP_LOOKUP(ZTOV(dzp), nm, vpp, pnp,
955                     flags, rdir, cr, ct, direntflags, realpnp);
956
957         VN_RELE(ZTOV(dzp));
958         ZFS_EXIT(zfsvfs);
959
960         return (error);
961 }
962
963 /* ARGSUSED */
964 static int
965 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
966     offset_t *offp, offset_t *nextp, void *data, int flags)
967 {
968         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
969         char snapname[MAXNAMELEN];
970         uint64_t id, cookie;
971         boolean_t case_conflict;
972         int error;
973
974         ZFS_ENTER(zfsvfs);
975
976         cookie = *offp;
977         error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
978             &cookie, &case_conflict);
979         if (error) {
980                 ZFS_EXIT(zfsvfs);
981                 if (error == ENOENT) {
982                         *eofp = 1;
983                         return (0);
984                 }
985                 return (error);
986         }
987
988         if (flags & V_RDDIR_ENTFLAGS) {
989                 edirent_t *eodp = dp;
990
991                 (void) strcpy(eodp->ed_name, snapname);
992                 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
993                 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
994         } else {
995                 struct dirent64 *odp = dp;
996
997                 (void) strcpy(odp->d_name, snapname);
998                 odp->d_ino = ZFSCTL_INO_SNAP(id);
999         }
1000         *nextp = cookie;
1001
1002         ZFS_EXIT(zfsvfs);
1003
1004         return (0);
1005 }
1006
1007 /* ARGSUSED */
1008 static int
1009 zfsctl_shares_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
1010     caller_context_t *ct, int flags)
1011 {
1012         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1013         znode_t *dzp;
1014         int error;
1015
1016         ZFS_ENTER(zfsvfs);
1017
1018         if (zfsvfs->z_shares_dir == 0) {
1019                 ZFS_EXIT(zfsvfs);
1020                 return (ENOTSUP);
1021         }
1022         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1023                 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ct, flags);
1024                 VN_RELE(ZTOV(dzp));
1025         } else {
1026                 *eofp = 1;
1027                 error = ENOENT;
1028         }
1029
1030         ZFS_EXIT(zfsvfs);
1031         return (error);
1032 }
1033
1034 /*
1035  * pvp is the '.zfs' directory (zfsctl_node_t).
1036  * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1037  *
1038  * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1039  * when a lookup is performed on .zfs for "snapshot".
1040  */
1041 vnode_t *
1042 zfsctl_mknode_snapdir(vnode_t *pvp)
1043 {
1044         vnode_t *vp;
1045         zfsctl_snapdir_t *sdp;
1046
1047         vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp,
1048             zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1049             zfsctl_snapdir_readdir_cb, NULL);
1050         sdp = vp->v_data;
1051         sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1052         sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1053         mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1054         avl_create(&sdp->sd_snaps, snapentry_compare,
1055             sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1056         return (vp);
1057 }
1058
1059 vnode_t *
1060 zfsctl_mknode_shares(vnode_t *pvp)
1061 {
1062         vnode_t *vp;
1063         zfsctl_node_t *sdp;
1064
1065         vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1066             zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1067             NULL, NULL);
1068         sdp = vp->v_data;
1069         sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1070         return (vp);
1071
1072 }
1073
1074 /* ARGSUSED */
1075 static int
1076 zfsctl_shares_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1077     caller_context_t *ct)
1078 {
1079         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1080         znode_t *dzp;
1081         int error;
1082
1083         ZFS_ENTER(zfsvfs);
1084         if (zfsvfs->z_shares_dir == 0) {
1085                 ZFS_EXIT(zfsvfs);
1086                 return (ENOTSUP);
1087         }
1088         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1089                 error = VOP_GETATTR(ZTOV(dzp), vap, flags, cr, ct);
1090                 VN_RELE(ZTOV(dzp));
1091         }
1092         ZFS_EXIT(zfsvfs);
1093         return (error);
1094
1095
1096 }
1097
1098 /* ARGSUSED */
1099 static int
1100 zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1101     caller_context_t *ct)
1102 {
1103         zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1104         zfsctl_snapdir_t *sdp = vp->v_data;
1105
1106         ZFS_ENTER(zfsvfs);
1107         zfsctl_common_getattr(vp, vap);
1108         vap->va_nodeid = gfs_file_inode(vp);
1109         vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1110         vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1111         ZFS_EXIT(zfsvfs);
1112
1113         return (0);
1114 }
1115
1116 /* ARGSUSED */
1117 static void
1118 zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1119 {
1120         zfsctl_snapdir_t *sdp = vp->v_data;
1121         void *private;
1122
1123         private = gfs_dir_inactive(vp);
1124         if (private != NULL) {
1125                 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1126                 mutex_destroy(&sdp->sd_lock);
1127                 avl_destroy(&sdp->sd_snaps);
1128                 kmem_free(private, sizeof (zfsctl_snapdir_t));
1129         }
1130 }
1131
1132 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1133         { VOPNAME_OPEN,         { .vop_open = zfsctl_common_open }      },
1134         { VOPNAME_CLOSE,        { .vop_close = zfsctl_common_close }    },
1135         { VOPNAME_IOCTL,        { .error = fs_inval }                   },
1136         { VOPNAME_GETATTR,      { .vop_getattr = zfsctl_snapdir_getattr } },
1137         { VOPNAME_ACCESS,       { .vop_access = zfsctl_common_access }  },
1138         { VOPNAME_RENAME,       { .vop_rename = zfsctl_snapdir_rename } },
1139         { VOPNAME_RMDIR,        { .vop_rmdir = zfsctl_snapdir_remove }  },
1140         { VOPNAME_MKDIR,        { .vop_mkdir = zfsctl_snapdir_mkdir }   },
1141         { VOPNAME_READDIR,      { .vop_readdir = gfs_vop_readdir }      },
1142         { VOPNAME_LOOKUP,       { .vop_lookup = zfsctl_snapdir_lookup } },
1143         { VOPNAME_SEEK,         { .vop_seek = fs_seek }                 },
1144         { VOPNAME_INACTIVE,     { .vop_inactive = zfsctl_snapdir_inactive } },
1145         { VOPNAME_FID,          { .vop_fid = zfsctl_common_fid }        },
1146         { NULL }
1147 };
1148
1149 static const fs_operation_def_t zfsctl_tops_shares[] = {
1150         { VOPNAME_OPEN,         { .vop_open = zfsctl_common_open }      },
1151         { VOPNAME_CLOSE,        { .vop_close = zfsctl_common_close }    },
1152         { VOPNAME_IOCTL,        { .error = fs_inval }                   },
1153         { VOPNAME_GETATTR,      { .vop_getattr = zfsctl_shares_getattr } },
1154         { VOPNAME_ACCESS,       { .vop_access = zfsctl_common_access }  },
1155         { VOPNAME_READDIR,      { .vop_readdir = zfsctl_shares_readdir } },
1156         { VOPNAME_LOOKUP,       { .vop_lookup = zfsctl_shares_lookup }  },
1157         { VOPNAME_SEEK,         { .vop_seek = fs_seek }                 },
1158         { VOPNAME_INACTIVE,     { .vop_inactive = gfs_vop_inactive } },
1159         { VOPNAME_FID,          { .vop_fid = zfsctl_shares_fid } },
1160         { NULL }
1161 };
1162
1163 /*
1164  * pvp is the GFS vnode '.zfs/snapshot'.
1165  *
1166  * This creates a GFS node under '.zfs/snapshot' representing each
1167  * snapshot.  This newly created GFS node is what we mount snapshot
1168  * vfs_t's ontop of.
1169  */
1170 static vnode_t *
1171 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1172 {
1173         vnode_t *vp;
1174         zfsctl_node_t *zcp;
1175
1176         vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1177             zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1178         zcp = vp->v_data;
1179         zcp->zc_id = objset;
1180
1181         return (vp);
1182 }
1183
1184 static void
1185 zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1186 {
1187         zfsctl_snapdir_t *sdp;
1188         zfs_snapentry_t *sep, *next;
1189         vnode_t *dvp;
1190
1191         VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1192         sdp = dvp->v_data;
1193
1194         mutex_enter(&sdp->sd_lock);
1195
1196         if (vp->v_count > 1) {
1197                 mutex_exit(&sdp->sd_lock);
1198                 return;
1199         }
1200         ASSERT(!vn_ismntpt(vp));
1201
1202         sep = avl_first(&sdp->sd_snaps);
1203         while (sep != NULL) {
1204                 next = AVL_NEXT(&sdp->sd_snaps, sep);
1205
1206                 if (sep->se_root == vp) {
1207                         avl_remove(&sdp->sd_snaps, sep);
1208                         kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1209                         kmem_free(sep, sizeof (zfs_snapentry_t));
1210                         break;
1211                 }
1212                 sep = next;
1213         }
1214         ASSERT(sep != NULL);
1215
1216         mutex_exit(&sdp->sd_lock);
1217         VN_RELE(dvp);
1218
1219         /*
1220          * Dispose of the vnode for the snapshot mount point.
1221          * This is safe to do because once this entry has been removed
1222          * from the AVL tree, it can't be found again, so cannot become
1223          * "active".  If we lookup the same name again we will end up
1224          * creating a new vnode.
1225          */
1226         gfs_vop_inactive(vp, cr, ct);
1227 }
1228
1229
1230 /*
1231  * These VP's should never see the light of day.  They should always
1232  * be covered.
1233  */
1234 static const fs_operation_def_t zfsctl_tops_snapshot[] = {
1235         VOPNAME_INACTIVE, { .vop_inactive =  zfsctl_snapshot_inactive },
1236         NULL, NULL
1237 };
1238
1239 int
1240 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1241 {
1242         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1243         vnode_t *dvp, *vp;
1244         zfsctl_snapdir_t *sdp;
1245         zfsctl_node_t *zcp;
1246         zfs_snapentry_t *sep;
1247         int error;
1248
1249         ASSERT(zfsvfs->z_ctldir != NULL);
1250         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1251             NULL, 0, NULL, kcred, NULL, NULL, NULL);
1252         if (error != 0)
1253                 return (error);
1254         sdp = dvp->v_data;
1255
1256         mutex_enter(&sdp->sd_lock);
1257         sep = avl_first(&sdp->sd_snaps);
1258         while (sep != NULL) {
1259                 vp = sep->se_root;
1260                 zcp = vp->v_data;
1261                 if (zcp->zc_id == objsetid)
1262                         break;
1263
1264                 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1265         }
1266
1267         if (sep != NULL) {
1268                 VN_HOLD(vp);
1269                 /*
1270                  * Return the mounted root rather than the covered mount point.
1271                  * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1272                  * and returns the ZFS vnode mounted on top of the GFS node.
1273                  * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1274                  */
1275                 error = traverse(&vp);
1276                 if (error == 0) {
1277                         if (vp == sep->se_root)
1278                                 error = EINVAL;
1279                         else
1280                                 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1281                 }
1282                 mutex_exit(&sdp->sd_lock);
1283                 VN_RELE(vp);
1284         } else {
1285                 error = EINVAL;
1286                 mutex_exit(&sdp->sd_lock);
1287         }
1288
1289         VN_RELE(dvp);
1290
1291         return (error);
1292 }
1293
1294 /*
1295  * Unmount any snapshots for the given filesystem.  This is called from
1296  * zfs_umount() - if we have a ctldir, then go through and unmount all the
1297  * snapshots.
1298  */
1299 int
1300 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1301 {
1302         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1303         vnode_t *dvp;
1304         zfsctl_snapdir_t *sdp;
1305         zfs_snapentry_t *sep, *next;
1306         int error;
1307
1308         ASSERT(zfsvfs->z_ctldir != NULL);
1309         error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1310             NULL, 0, NULL, cr, NULL, NULL, NULL);
1311         if (error != 0)
1312                 return (error);
1313         sdp = dvp->v_data;
1314
1315         mutex_enter(&sdp->sd_lock);
1316
1317         sep = avl_first(&sdp->sd_snaps);
1318         while (sep != NULL) {
1319                 next = AVL_NEXT(&sdp->sd_snaps, sep);
1320
1321                 /*
1322                  * If this snapshot is not mounted, then it must
1323                  * have just been unmounted by somebody else, and
1324                  * will be cleaned up by zfsctl_snapdir_inactive().
1325                  */
1326                 if (vn_ismntpt(sep->se_root)) {
1327                         avl_remove(&sdp->sd_snaps, sep);
1328                         error = zfsctl_unmount_snap(sep, fflags, cr);
1329                         if (error) {
1330                                 avl_add(&sdp->sd_snaps, sep);
1331                                 break;
1332                         }
1333                 }
1334                 sep = next;
1335         }
1336
1337         mutex_exit(&sdp->sd_lock);
1338         VN_RELE(dvp);
1339
1340         return (error);
1341 }
1342 #endif /* HAVE_ZPL */