Add .zfs control directory
[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  *
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
25  * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
26  * LLNL-CODE-403049.
27  * Rewritten for Linux by:
28  *   Rohan Puri <rohan.puri15@gmail.com>
29  *   Brian Behlendorf <behlendorf1@llnl.gov>
30  */
31
32 /*
33  * ZFS control directory (a.k.a. ".zfs")
34  *
35  * This directory provides a common location for all ZFS meta-objects.
36  * Currently, this is only the 'snapshot' and 'shares' directory, but this may
37  * expand in the future.  The elements are built dynamically, as the hierarchy
38  * does not actually exist on disk.
39  *
40  * For 'snapshot', we don't want to have all snapshots always mounted, because
41  * this would take up a huge amount of space in /etc/mnttab.  We have three
42  * types of objects:
43  *
44  *      ctldir ------> snapshotdir -------> snapshot
45  *                                             |
46  *                                             |
47  *                                             V
48  *                                         mounted fs
49  *
50  * The 'snapshot' node contains just enough information to lookup '..' and act
51  * as a mountpoint for the snapshot.  Whenever we lookup a specific snapshot, we
52  * perform an automount of the underlying filesystem and return the
53  * corresponding inode.
54  *
55  * All mounts are handled automatically by an user mode helper which invokes
56  * the mount mount procedure.  Unmounts are handled by allowing the mount
57  * point to expire so the kernel may automatically unmount it.
58  *
59  * The '.zfs', '.zfs/snapshot', and all directories created under
60  * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') all share the same
61  * share the same zfs_sb_t as the head filesystem (what '.zfs' lives under).
62  *
63  * File systems mounted on top of the '.zfs/snapshot/<snapname>' paths
64  * (ie: snapshots) are complete ZFS filesystems and have their own unique
65  * zfs_sb_t.  However, the fsid reported by these mounts will be the same
66  * as that used by the parent zfs_sb_t to make NFS happy.
67  */
68
69 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <sys/systm.h>
73 #include <sys/sysmacros.h>
74 #include <sys/pathname.h>
75 #include <sys/vfs.h>
76 #include <sys/vfs_opreg.h>
77 #include <sys/zfs_ctldir.h>
78 #include <sys/zfs_ioctl.h>
79 #include <sys/zfs_vfsops.h>
80 #include <sys/zfs_vnops.h>
81 #include <sys/stat.h>
82 #include <sys/dmu.h>
83 #include <sys/dsl_deleg.h>
84 #include <sys/mount.h>
85 #include <sys/zpl.h>
86 #include "zfs_namecheck.h"
87
88 /*
89  * Control Directory Tunables (.zfs)
90  */
91 int zfs_expire_snapshot = ZFSCTL_EXPIRE_SNAPSHOT;
92
93 static zfs_snapentry_t *
94 zfsctl_sep_alloc(void)
95 {
96         return kmem_zalloc(sizeof (zfs_snapentry_t), KM_SLEEP);
97 }
98
99 void
100 zfsctl_sep_free(zfs_snapentry_t *sep)
101 {
102         kmem_free(sep->se_name, MAXNAMELEN);
103         kmem_free(sep->se_path, PATH_MAX);
104         kmem_free(sep, sizeof (zfs_snapentry_t));
105 }
106
107 /*
108  * Attempt to expire an automounted snapshot, unmounts are attempted every
109  * 'zfs_expire_snapshot' seconds until they succeed.  The work request is
110  * responsible for rescheduling itself and freeing the zfs_expire_snapshot_t.
111  */
112 static void
113 zfsctl_expire_snapshot(void *data)
114 {
115         zfs_snapentry_t *sep;
116         zfs_sb_t *zsb;
117         int error;
118
119         sep = spl_get_work_data(data, zfs_snapentry_t, se_work.work);
120         zsb = ITOZSB(sep->se_inode);
121
122         error = zfsctl_unmount_snapshot(zsb, sep->se_name, MNT_EXPIRE);
123         if (error == EBUSY)
124                 schedule_delayed_work(&sep->se_work, zfs_expire_snapshot * HZ);
125 }
126
127 int
128 snapentry_compare(const void *a, const void *b)
129 {
130         const zfs_snapentry_t *sa = a;
131         const zfs_snapentry_t *sb = b;
132         int ret = strcmp(sa->se_name, sb->se_name);
133
134         if (ret < 0)
135                 return (-1);
136         else if (ret > 0)
137                 return (1);
138         else
139                 return (0);
140 }
141
142 boolean_t
143 zfsctl_is_node(struct inode *ip)
144 {
145         return (ITOZ(ip)->z_is_ctldir);
146 }
147
148 boolean_t
149 zfsctl_is_snapdir(struct inode *ip)
150 {
151         return (zfsctl_is_node(ip) && (ip->i_ino <= ZFSCTL_INO_SNAPDIRS));
152 }
153
154 /*
155  * Allocate a new inode with the passed id and ops.
156  */
157 static struct inode *
158 zfsctl_inode_alloc(zfs_sb_t *zsb, uint64_t id,
159     const struct file_operations *fops, const struct inode_operations *ops)
160 {
161         struct timespec now = current_fs_time(zsb->z_sb);
162         struct inode *ip;
163         znode_t *zp;
164
165         ip = new_inode(zsb->z_sb);
166         if (ip == NULL)
167                 return (NULL);
168
169         zp = ITOZ(ip);
170         ASSERT3P(zp->z_dirlocks, ==, NULL);
171         ASSERT3P(zp->z_acl_cached, ==, NULL);
172         ASSERT3P(zp->z_xattr_cached, ==, NULL);
173         zp->z_id = id;
174         zp->z_unlinked = 0;
175         zp->z_atime_dirty = 0;
176         zp->z_zn_prefetch = 0;
177         zp->z_moved = 0;
178         zp->z_sa_hdl = NULL;
179         zp->z_blksz = 0;
180         zp->z_seq = 0;
181         zp->z_mapcnt = 0;
182         zp->z_gen = 0;
183         zp->z_size = 0;
184         zp->z_atime[0] = 0;
185         zp->z_atime[1] = 0;
186         zp->z_links = 0;
187         zp->z_pflags = 0;
188         zp->z_uid = 0;
189         zp->z_gid = 0;
190         zp->z_mode = 0;
191         zp->z_sync_cnt = 0;
192         zp->z_is_zvol = B_FALSE;
193         zp->z_is_mapped = B_FALSE;
194         zp->z_is_ctldir = B_TRUE;
195         zp->z_is_sa = B_FALSE;
196         ip->i_ino = id;
197         ip->i_mode = (S_IFDIR | S_IRUGO | S_IXUGO);
198         ip->i_uid = 0;
199         ip->i_gid = 0;
200         ip->i_blkbits = SPA_MINBLOCKSHIFT;
201         ip->i_atime = now;
202         ip->i_mtime = now;
203         ip->i_ctime = now;
204         ip->i_fop = fops;
205         ip->i_op = ops;
206
207         if (insert_inode_locked(ip)) {
208                 unlock_new_inode(ip);
209                 iput(ip);
210                 return (NULL);
211         }
212
213         mutex_enter(&zsb->z_znodes_lock);
214         list_insert_tail(&zsb->z_all_znodes, zp);
215         membar_producer();
216         mutex_exit(&zsb->z_znodes_lock);
217
218         unlock_new_inode(ip);
219
220         return (ip);
221 }
222
223 /*
224  * Lookup the inode with given id, it will be allocated if needed.
225  */
226 static struct inode *
227 zfsctl_inode_lookup(zfs_sb_t *zsb, unsigned long id,
228     const struct file_operations *fops, const struct inode_operations *ops)
229 {
230         struct inode *ip = NULL;
231
232         while (ip == NULL) {
233                 ip = ilookup(zsb->z_sb, id);
234                 if (ip)
235                         break;
236
237                 /* May fail due to concurrent zfsctl_inode_alloc() */
238                 ip = zfsctl_inode_alloc(zsb, id, fops, ops);
239         }
240
241         return (ip);
242 }
243
244 /*
245  * Free zfsctl inode specific structures, currently there are none.
246  */
247 void
248 zfsctl_inode_destroy(struct inode *ip)
249 {
250         return;
251 }
252
253 /*
254  * An inode is being evicted from the cache.
255  */
256 void
257 zfsctl_inode_inactive(struct inode *ip)
258 {
259         if (zfsctl_is_snapdir(ip))
260                 zfsctl_snapdir_inactive(ip);
261 }
262
263 /*
264  * Create the '.zfs' directory.  This directory is cached as part of the VFS
265  * structure.  This results in a hold on the zfs_sb_t.  The code in zfs_umount()
266  * therefore checks against a vfs_count of 2 instead of 1.  This reference
267  * is removed when the ctldir is destroyed in the unmount.  All other entities
268  * under the '.zfs' directory are created dynamically as needed.
269  */
270 int
271 zfsctl_create(zfs_sb_t *zsb)
272 {
273         ASSERT(zsb->z_ctldir == NULL);
274
275         zsb->z_ctldir = zfsctl_inode_alloc(zsb, ZFSCTL_INO_ROOT,
276             &zpl_fops_root, &zpl_ops_root);
277         if (zsb->z_ctldir == NULL)
278                 return (ENOENT);
279
280         return (0);
281 }
282
283 /*
284  * Destroy the '.zfs' directory.  Only called when the filesystem is unmounted.
285  */
286 void
287 zfsctl_destroy(zfs_sb_t *zsb)
288 {
289         iput(zsb->z_ctldir);
290         zsb->z_ctldir = NULL;
291 }
292
293 /*
294  * Given a root znode, retrieve the associated .zfs directory.
295  * Add a hold to the vnode and return it.
296  */
297 struct inode *
298 zfsctl_root(znode_t *zp)
299 {
300         ASSERT(zfs_has_ctldir(zp));
301         igrab(ZTOZSB(zp)->z_ctldir);
302         return (ZTOZSB(zp)->z_ctldir);
303 }
304
305 /*ARGSUSED*/
306 int
307 zfsctl_fid(struct inode *ip, fid_t *fidp)
308 {
309         znode_t         *zp = ITOZ(ip);
310         zfs_sb_t        *zsb = ITOZSB(ip);
311         uint64_t        object = zp->z_id;
312         zfid_short_t    *zfid;
313         int             i;
314
315         ZFS_ENTER(zsb);
316
317         if (fidp->fid_len < SHORT_FID_LEN) {
318                 fidp->fid_len = SHORT_FID_LEN;
319                 ZFS_EXIT(zsb);
320                 return (ENOSPC);
321         }
322
323         zfid = (zfid_short_t *)fidp;
324
325         zfid->zf_len = SHORT_FID_LEN;
326
327         for (i = 0; i < sizeof (zfid->zf_object); i++)
328                 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
329
330         /* .zfs znodes always have a generation number of 0 */
331         for (i = 0; i < sizeof (zfid->zf_gen); i++)
332                 zfid->zf_gen[i] = 0;
333
334         ZFS_EXIT(zsb);
335         return (0);
336 }
337
338 static int
339 zfsctl_snapshot_zname(struct inode *ip, const char *name, int len, char *zname)
340 {
341         objset_t *os = ITOZSB(ip)->z_os;
342
343         if (snapshot_namecheck(name, NULL, NULL) != 0)
344                 return (EILSEQ);
345
346         dmu_objset_name(os, zname);
347         if ((strlen(zname) + 1 + strlen(name)) >= len)
348                 return (ENAMETOOLONG);
349
350         (void) strcat(zname, "@");
351         (void) strcat(zname, name);
352
353         return (0);
354 }
355
356 static int
357 zfsctl_snapshot_zpath(struct path *path, int len, char *zpath)
358 {
359         char *path_buffer, *path_ptr;
360         int path_len, error = 0;
361
362         path_buffer = kmem_alloc(len, KM_SLEEP);
363
364         path_ptr = d_path(path, path_buffer, len);
365         if (IS_ERR(path_ptr)) {
366                 error = -PTR_ERR(path_ptr);
367                 goto out;
368         }
369
370         path_len = path_buffer + len - 1 - path_ptr;
371         if (path_len > len) {
372                 error = EFAULT;
373                 goto out;
374         }
375
376         memcpy(zpath, path_ptr, path_len);
377         zpath[path_len] = '\0';
378 out:
379         kmem_free(path_buffer, len);
380
381         return (error);
382 }
383
384 /*
385  * Special case the handling of "..".
386  */
387 /* ARGSUSED */
388 int
389 zfsctl_root_lookup(struct inode *dip, char *name, struct inode **ipp,
390     int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
391 {
392         zfs_sb_t *zsb = ITOZSB(dip);
393         int error = 0;
394
395         ZFS_ENTER(zsb);
396
397         if (strcmp(name, "..") == 0) {
398                 *ipp = dip->i_sb->s_root->d_inode;
399         } else if (strcmp(name, ZFS_SNAPDIR_NAME) == 0) {
400                 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIR,
401                     &zpl_fops_snapdir, &zpl_ops_snapdir);
402         } else if (strcmp(name, ZFS_SHAREDIR_NAME) == 0) {
403                 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SHARES,
404                     &zpl_fops_shares, &zpl_ops_shares);
405         } else {
406                 *ipp = NULL;
407         }
408
409         if (*ipp == NULL)
410                 error = ENOENT;
411
412         ZFS_EXIT(zsb);
413
414         return (error);
415 }
416
417 /*
418  * Lookup entry point for the 'snapshot' directory.  Try to open the
419  * snapshot if it exist, creating the pseudo filesystem inode as necessary.
420  * Perform a mount of the associated dataset on top of the inode.
421  */
422 /* ARGSUSED */
423 int
424 zfsctl_snapdir_lookup(struct inode *dip, char *name, struct inode **ipp,
425     int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
426 {
427         zfs_sb_t *zsb = ITOZSB(dip);
428         uint64_t id;
429         int error;
430
431         ZFS_ENTER(zsb);
432
433         error = dmu_snapshot_id(zsb->z_os, name, &id);
434         if (error) {
435                 ZFS_EXIT(zsb);
436                 return (error);
437         }
438
439         *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIRS - id,
440             &simple_dir_operations, &simple_dir_inode_operations);
441         if (*ipp) {
442 #ifdef HAVE_AUTOMOUNT
443                 (*ipp)->i_flags |= S_AUTOMOUNT;
444 #endif /* HAVE_AUTOMOUNT */
445         } else {
446                 error = ENOENT;
447         }
448
449         ZFS_EXIT(zsb);
450
451         return (error);
452 }
453
454 static void
455 zfsctl_rename_snap(zfs_sb_t *zsb, zfs_snapentry_t *sep, const char *name)
456 {
457         avl_index_t where;
458
459         ASSERT(MUTEX_HELD(&zsb->z_ctldir_lock));
460         ASSERT(sep != NULL);
461
462         /*
463          * Change the name in the AVL tree.
464          */
465         avl_remove(&zsb->z_ctldir_snaps, sep);
466         (void) strcpy(sep->se_name, name);
467         VERIFY(avl_find(&zsb->z_ctldir_snaps, sep, &where) == NULL);
468         avl_insert(&zsb->z_ctldir_snaps, sep, where);
469 }
470
471 /*
472  * Renaming a directory under '.zfs/snapshot' will automatically trigger
473  * a rename of the snapshot to the new given name.  The rename is confined
474  * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere.
475  */
476 /*ARGSUSED*/
477 int
478 zfsctl_snapdir_rename(struct inode *sdip, char *sname,
479     struct inode *tdip, char *tname, cred_t *cr, int flags)
480 {
481         zfs_sb_t *zsb = ITOZSB(sdip);
482         zfs_snapentry_t search, *sep;
483         avl_index_t where;
484         char *to, *from, *real;
485         int error;
486
487         ZFS_ENTER(zsb);
488
489         to = kmem_alloc(MAXNAMELEN, KM_SLEEP);
490         from = kmem_alloc(MAXNAMELEN, KM_SLEEP);
491         real = kmem_alloc(MAXNAMELEN, KM_SLEEP);
492
493         if (zsb->z_case == ZFS_CASE_INSENSITIVE) {
494                 error = dmu_snapshot_realname(zsb->z_os, sname, real,
495                     MAXNAMELEN, NULL);
496                 if (error == 0) {
497                         sname = real;
498                 } else if (error != ENOTSUP) {
499                         goto out;
500                 }
501         }
502
503         error = zfsctl_snapshot_zname(sdip, sname, MAXNAMELEN, from);
504         if (!error)
505                 error = zfsctl_snapshot_zname(tdip, tname, MAXNAMELEN, to);
506         if (!error)
507                 error = zfs_secpolicy_rename_perms(from, to, cr);
508         if (error)
509                 goto out;
510
511         /*
512          * Cannot move snapshots out of the snapdir.
513          */
514         if (sdip != tdip) {
515                 error = EINVAL;
516                 goto out;
517         }
518
519         /*
520          * No-op when names are identical.
521          */
522         if (strcmp(sname, tname) == 0) {
523                 error = 0;
524                 goto out;
525         }
526
527         mutex_enter(&zsb->z_ctldir_lock);
528
529         error = dmu_objset_rename(from, to, B_FALSE);
530         if (error)
531                 goto out_unlock;
532
533         search.se_name = (char *)sname;
534         sep = avl_find(&zsb->z_ctldir_snaps, &search, &where);
535         if (sep)
536                 zfsctl_rename_snap(zsb, sep, tname);
537
538 out_unlock:
539         mutex_exit(&zsb->z_ctldir_lock);
540 out:
541         kmem_free(from, MAXNAMELEN);
542         kmem_free(to, MAXNAMELEN);
543         kmem_free(real, MAXNAMELEN);
544
545         ZFS_EXIT(zsb);
546
547         return (error);
548 }
549
550 /*
551  * Removing a directory under '.zfs/snapshot' will automatically trigger
552  * the removal of the snapshot with the given name.
553  */
554 /* ARGSUSED */
555 int
556 zfsctl_snapdir_remove(struct inode *dip, char *name, cred_t *cr, int flags)
557 {
558         zfs_sb_t *zsb = ITOZSB(dip);
559         char *snapname, *real;
560         int error;
561
562         ZFS_ENTER(zsb);
563
564         snapname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
565         real = kmem_alloc(MAXNAMELEN, KM_SLEEP);
566
567         if (zsb->z_case == ZFS_CASE_INSENSITIVE) {
568                 error = dmu_snapshot_realname(zsb->z_os, name, real,
569                     MAXNAMELEN, NULL);
570                 if (error == 0) {
571                         name = real;
572                 } else if (error != ENOTSUP) {
573                         goto out;
574                 }
575         }
576
577         error = zfsctl_snapshot_zname(dip, name, MAXNAMELEN, snapname);
578         if (!error)
579                 error = zfs_secpolicy_destroy_perms(snapname, cr);
580         if (error)
581                 goto out;
582
583         error = zfsctl_unmount_snapshot(zsb, name, MNT_FORCE);
584         if ((error == 0) || (error == ENOENT))
585                 error = dmu_objset_destroy(snapname, B_FALSE);
586 out:
587         kmem_free(snapname, MAXNAMELEN);
588         kmem_free(real, MAXNAMELEN);
589
590         ZFS_EXIT(zsb);
591
592         return (error);
593 }
594
595 /*
596  * Creating a directory under '.zfs/snapshot' will automatically trigger
597  * the creation of a new snapshot with the given name.
598  */
599 /* ARGSUSED */
600 int
601 zfsctl_snapdir_mkdir(struct inode *dip, char *dirname, vattr_t *vap,
602         struct inode **ipp, cred_t *cr, int flags)
603 {
604         zfs_sb_t *zsb = ITOZSB(dip);
605         char *dsname;
606         int error;
607
608         dsname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
609
610         if (snapshot_namecheck(dirname, NULL, NULL) != 0) {
611                 error = EILSEQ;
612                 goto out;
613         }
614
615         dmu_objset_name(zsb->z_os, dsname);
616
617         error = zfs_secpolicy_snapshot_perms(dsname, cr);
618         if (error)
619                 goto out;
620
621         if (error == 0) {
622                 error = dmu_objset_snapshot(dsname, dirname,
623                     NULL, NULL, B_FALSE, B_FALSE, -1);
624                 if (error)
625                         goto out;
626
627                 error = zfsctl_snapdir_lookup(dip, dirname, ipp,
628                     0, cr, NULL, NULL);
629         }
630 out:
631         kmem_free(dsname, MAXNAMELEN);
632
633         return (error);
634 }
635
636 /*
637  * When a .zfs/snapshot/<snapshot> inode is evicted they must be removed
638  * from the snapshot list.  This will normally happen as part of the auto
639  * unmount, however in the case of a manual snapshot unmount this will be
640  * the only notification we receive.
641  */
642 void
643 zfsctl_snapdir_inactive(struct inode *ip)
644 {
645         zfs_sb_t *zsb = ITOZSB(ip);
646         zfs_snapentry_t *sep, *next;
647
648         mutex_enter(&zsb->z_ctldir_lock);
649
650         sep = avl_first(&zsb->z_ctldir_snaps);
651         while (sep != NULL) {
652                 next = AVL_NEXT(&zsb->z_ctldir_snaps, sep);
653
654                 if (sep->se_inode == ip) {
655                         avl_remove(&zsb->z_ctldir_snaps, sep);
656                         cancel_delayed_work_sync(&sep->se_work);
657                         zfsctl_sep_free(sep);
658                         break;
659                 }
660                 sep = next;
661         }
662
663         mutex_exit(&zsb->z_ctldir_lock);
664 }
665
666 /*
667  * Attempt to unmount a snapshot by making a call to user space.
668  * There is no assurance that this can or will succeed, is just a
669  * best effort.  In the case where it does fail, perhaps because
670  * it's in use, the unmount will fail harmlessly.
671  */
672 #define SET_UNMOUNT_CMD \
673         "exec 0</dev/null " \
674         "     1>/dev/null " \
675         "     2>/dev/null; " \
676         "umount -t zfs -n %s%s"
677
678 static int
679 __zfsctl_unmount_snapshot(zfs_snapentry_t *sep, int flags)
680 {
681         char *argv[] = { "/bin/sh", "-c", NULL, NULL };
682         char *envp[] = { NULL };
683         int error;
684
685         argv[2] = kmem_asprintf(SET_UNMOUNT_CMD,
686             flags & MNT_FORCE ? "-f " : "", sep->se_path);
687         error = call_usermodehelper(argv[0], argv, envp, 1);
688         strfree(argv[2]);
689
690         /*
691          * The umount system utility will return 256 on error.  We must
692          * assume this error is because the file system is busy so it is
693          * converted to the more sensible EBUSY.
694          */
695         if (error)
696                 error = EBUSY;
697
698         /*
699          * This was the result of a manual unmount, cancel the delayed work
700          * to prevent zfsctl_expire_snapshot() from attempting a unmount.
701          */
702         if ((error == 0) && !(flags & MNT_EXPIRE))
703                 cancel_delayed_work(&sep->se_work);
704
705         return (error);
706 }
707
708 int
709 zfsctl_unmount_snapshot(zfs_sb_t *zsb, char *name, int flags)
710 {
711         zfs_snapentry_t search;
712         zfs_snapentry_t *sep;
713         int error = 0;
714
715         mutex_enter(&zsb->z_ctldir_lock);
716
717         search.se_name = name;
718         sep = avl_find(&zsb->z_ctldir_snaps, &search, NULL);
719         if (sep) {
720                 avl_remove(&zsb->z_ctldir_snaps, sep);
721                 error = __zfsctl_unmount_snapshot(sep, flags);
722                 if (error == EBUSY)
723                         avl_add(&zsb->z_ctldir_snaps, sep);
724                 else
725                         zfsctl_sep_free(sep);
726         } else {
727                 error = ENOENT;
728         }
729
730         mutex_exit(&zsb->z_ctldir_lock);
731         ASSERT3S(error, >=, 0);
732
733         return (error);
734 }
735
736 /*
737  * Traverse all mounted snapshots and attempt to unmount them.  This
738  * is best effort, on failure EEXIST is returned and count will be set
739  * to the number of file snapshots which could not be unmounted.
740  */
741 int
742 zfsctl_unmount_snapshots(zfs_sb_t *zsb, int flags, int *count)
743 {
744         zfs_snapentry_t *sep, *next;
745         int error = 0;
746
747         *count = 0;
748
749         ASSERT(zsb->z_ctldir != NULL);
750         mutex_enter(&zsb->z_ctldir_lock);
751
752         sep = avl_first(&zsb->z_ctldir_snaps);
753         while (sep != NULL) {
754                 next = AVL_NEXT(&zsb->z_ctldir_snaps, sep);
755                 avl_remove(&zsb->z_ctldir_snaps, sep);
756                 error = __zfsctl_unmount_snapshot(sep, flags);
757                 if (error == EBUSY) {
758                         avl_add(&zsb->z_ctldir_snaps, sep);
759                         (*count)++;
760                 } else {
761                         zfsctl_sep_free(sep);
762                 }
763
764                 sep = next;
765         }
766
767         mutex_exit(&zsb->z_ctldir_lock);
768
769         return ((*count > 0) ? EEXIST : 0);
770 }
771
772 #define SET_MOUNT_CMD \
773         "exec 0</dev/null " \
774         "     1>/dev/null " \
775         "     2>/dev/null; " \
776         "mount -t zfs -n %s %s"
777
778 int
779 zfsctl_mount_snapshot(struct path *path, int flags)
780 {
781         struct dentry *dentry = path->dentry;
782         struct inode *ip = dentry->d_inode;
783         zfs_sb_t *zsb = ITOZSB(ip);
784         char *full_name, *full_path;
785         zfs_snapentry_t *sep;
786         zfs_snapentry_t search;
787         char *argv[] = { "/bin/sh", "-c", NULL, NULL };
788         char *envp[] = { NULL };
789         int error;
790
791         ZFS_ENTER(zsb);
792
793         full_name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
794         full_path = kmem_zalloc(PATH_MAX, KM_SLEEP);
795
796         error = zfsctl_snapshot_zname(ip, dname(dentry), MAXNAMELEN, full_name);
797         if (error)
798                 goto error;
799
800         error = zfsctl_snapshot_zpath(path, PATH_MAX, full_path);
801         if (error)
802                 goto error;
803
804         /*
805          * Attempt to mount the snapshot from user space.  Normally this
806          * would be done using the vfs_kern_mount() function, however that
807          * function is marked GPL-only and cannot be used.  On error we
808          * careful to log the real error to the console and return EISDIR
809          * to safely abort the automount.  This should be very rare.
810          */
811         argv[2] = kmem_asprintf(SET_MOUNT_CMD, full_name, full_path);
812         error = call_usermodehelper(argv[0], argv, envp, 1);
813         strfree(argv[2]);
814         if (error) {
815                 printk("ZFS: Unable to automount %s at %s: %d\n",
816                     full_name, full_path, error);
817                 error = EISDIR;
818                 goto error;
819         }
820
821         mutex_enter(&zsb->z_ctldir_lock);
822
823         /*
824          * Ensure a previous entry does not exist, if it does safely remove
825          * it any cancel the outstanding expiration.  This can occur when a
826          * snapshot is manually unmounted and then an automount is triggered.
827          */
828         search.se_name = full_name;
829         sep = avl_find(&zsb->z_ctldir_snaps, &search, NULL);
830         if (sep) {
831                 avl_remove(&zsb->z_ctldir_snaps, sep);
832                 cancel_delayed_work_sync(&sep->se_work);
833                 zfsctl_sep_free(sep);
834         }
835
836         sep = zfsctl_sep_alloc();
837         sep->se_name = full_name;
838         sep->se_path = full_path;
839         sep->se_inode = ip;
840         avl_add(&zsb->z_ctldir_snaps, sep);
841
842         spl_init_delayed_work(&sep->se_work, zfsctl_expire_snapshot, sep);
843         schedule_delayed_work(&sep->se_work, zfs_expire_snapshot * HZ);
844
845         mutex_exit(&zsb->z_ctldir_lock);
846 error:
847         if (error) {
848                 kmem_free(full_name, MAXNAMELEN);
849                 kmem_free(full_path, PATH_MAX);
850         }
851
852         ZFS_EXIT(zsb);
853
854         return (error);
855 }
856
857 /*
858  * Check if this super block has a matching objset id.
859  */
860 static int
861 zfsctl_test_super(struct super_block *sb, void *objsetidp)
862 {
863         zfs_sb_t *zsb = sb->s_fs_info;
864         uint64_t objsetid = *(uint64_t *)objsetidp;
865
866         return (dmu_objset_id(zsb->z_os) == objsetid);
867 }
868
869 /*
870  * Prevent a new super block from being allocated if an existing one
871  * could not be located.  We only want to preform a lookup operation.
872  */
873 static int
874 zfsctl_set_super(struct super_block *sb, void *objsetidp)
875 {
876         return (-EEXIST);
877 }
878
879 int
880 zfsctl_lookup_objset(struct super_block *sb, uint64_t objsetid, zfs_sb_t **zsbp)
881 {
882         zfs_sb_t *zsb = sb->s_fs_info;
883         struct super_block *sbp;
884         zfs_snapentry_t *sep;
885         uint64_t id;
886         int error;
887
888         ASSERT(zsb->z_ctldir != NULL);
889
890         mutex_enter(&zsb->z_ctldir_lock);
891
892         /*
893          * Verify that the snapshot is mounted.
894          */
895         sep = avl_first(&zsb->z_ctldir_snaps);
896         while (sep != NULL) {
897                 error = dmu_snapshot_id(zsb->z_os, sep->se_name, &id);
898                 if (error)
899                         goto out;
900
901                 if (id == objsetid)
902                         break;
903
904                 sep = AVL_NEXT(&zsb->z_ctldir_snaps, sep);
905         }
906
907         if (sep != NULL) {
908                 /*
909                  * Lookup the mounted root rather than the covered mount
910                  * point.  This may fail if the snapshot has just been
911                  * unmounted by an unrelated user space process.  This
912                  * race cannot occur to an expired mount point because
913                  * we hold the zsb->z_ctldir_lock to prevent the race.
914                  */
915                 sbp = sget(&zpl_fs_type, zfsctl_test_super,
916                     zfsctl_set_super, &id);
917                 if (IS_ERR(sbp)) {
918                         error = -PTR_ERR(sbp);
919                 } else {
920                         *zsbp = sbp->s_fs_info;
921                         deactivate_super(sbp);
922                 }
923         } else {
924                 error = EINVAL;
925         }
926 out:
927         mutex_exit(&zsb->z_ctldir_lock);
928         ASSERT3S(error, >=, 0);
929
930         return (error);
931 }
932
933 /* ARGSUSED */
934 int
935 zfsctl_shares_lookup(struct inode *dip, char *name, struct inode **ipp,
936     int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
937 {
938         zfs_sb_t *zsb = ITOZSB(dip);
939         struct inode *ip;
940         znode_t *dzp;
941         int error;
942
943         ZFS_ENTER(zsb);
944
945         if (zsb->z_shares_dir == 0) {
946                 ZFS_EXIT(zsb);
947                 return (-ENOTSUP);
948         }
949
950         error = zfs_zget(zsb, zsb->z_shares_dir, &dzp);
951         if (error) {
952                 ZFS_EXIT(zsb);
953                 return (error);
954         }
955
956         error = zfs_lookup(ZTOI(dzp), name, &ip, 0, cr, NULL, NULL);
957
958         iput(ZTOI(dzp));
959         ZFS_EXIT(zsb);
960
961         return (error);
962 }
963
964
965 /*
966  * Initialize the various pieces we'll need to create and manipulate .zfs
967  * directories.  Currently this is unused but available.
968  */
969 void
970 zfsctl_init(void)
971 {
972 }
973
974 /*
975  * Cleanup the various pieces we needed for .zfs directories.  In particular
976  * ensure the expiry timer is canceled safely.
977  */
978 void
979 zfsctl_fini(void)
980 {
981 }
982
983 module_param(zfs_expire_snapshot, int, 0644);
984 MODULE_PARM_DESC(zfs_expire_snapshot, "Seconds to expire .zfs/snapshot");