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