]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_ctldir.c
5bea0b6c94f576b80cc2468f806b40dd9265f2a2
[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 /*
372 * Gets the full dataset name that corresponds to the given snapshot name
373 * Example:
374 * zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
375 */
376 static int
377 zfsctl_snapshot_zpath(struct path *path, int len, char *zpath)
378 {
379 char *path_buffer, *path_ptr;
380 int path_len, error = 0;
381
382 path_buffer = kmem_alloc(len, KM_SLEEP);
383
384 path_ptr = d_path(path, path_buffer, len);
385 if (IS_ERR(path_ptr)) {
386 error = -PTR_ERR(path_ptr);
387 goto out;
388 }
389
390 path_len = path_buffer + len - 1 - path_ptr;
391 if (path_len > len) {
392 error = SET_ERROR(EFAULT);
393 goto out;
394 }
395
396 memcpy(zpath, path_ptr, path_len);
397 zpath[path_len] = '\0';
398 out:
399 kmem_free(path_buffer, len);
400
401 return (error);
402 }
403
404 /*
405 * Special case the handling of "..".
406 */
407 /* ARGSUSED */
408 int
409 zfsctl_root_lookup(struct inode *dip, char *name, struct inode **ipp,
410 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
411 {
412 zfs_sb_t *zsb = ITOZSB(dip);
413 int error = 0;
414
415 ZFS_ENTER(zsb);
416
417 if (strcmp(name, "..") == 0) {
418 *ipp = dip->i_sb->s_root->d_inode;
419 } else if (strcmp(name, ZFS_SNAPDIR_NAME) == 0) {
420 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIR,
421 &zpl_fops_snapdir, &zpl_ops_snapdir);
422 } else if (strcmp(name, ZFS_SHAREDIR_NAME) == 0) {
423 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SHARES,
424 &zpl_fops_shares, &zpl_ops_shares);
425 } else {
426 *ipp = NULL;
427 }
428
429 if (*ipp == NULL)
430 error = SET_ERROR(ENOENT);
431
432 ZFS_EXIT(zsb);
433
434 return (error);
435 }
436
437 /*
438 * Lookup entry point for the 'snapshot' directory. Try to open the
439 * snapshot if it exist, creating the pseudo filesystem inode as necessary.
440 * Perform a mount of the associated dataset on top of the inode.
441 */
442 /* ARGSUSED */
443 int
444 zfsctl_snapdir_lookup(struct inode *dip, char *name, struct inode **ipp,
445 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
446 {
447 zfs_sb_t *zsb = ITOZSB(dip);
448 uint64_t id;
449 int error;
450
451 ZFS_ENTER(zsb);
452
453 error = dmu_snapshot_lookup(zsb->z_os, name, &id);
454 if (error) {
455 ZFS_EXIT(zsb);
456 return (error);
457 }
458
459 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIRS - id,
460 &simple_dir_operations, &simple_dir_inode_operations);
461 if (*ipp) {
462 #ifdef HAVE_AUTOMOUNT
463 (*ipp)->i_flags |= S_AUTOMOUNT;
464 #endif /* HAVE_AUTOMOUNT */
465 } else {
466 error = SET_ERROR(ENOENT);
467 }
468
469 ZFS_EXIT(zsb);
470
471 return (error);
472 }
473
474 static void
475 zfsctl_rename_snap(zfs_sb_t *zsb, zfs_snapentry_t *sep, const char *name)
476 {
477 avl_index_t where;
478
479 ASSERT(MUTEX_HELD(&zsb->z_ctldir_lock));
480 ASSERT(sep != NULL);
481
482 /*
483 * Change the name in the AVL tree.
484 */
485 avl_remove(&zsb->z_ctldir_snaps, sep);
486 (void) strcpy(sep->se_name, name);
487 VERIFY(avl_find(&zsb->z_ctldir_snaps, sep, &where) == NULL);
488 avl_insert(&zsb->z_ctldir_snaps, sep, where);
489 }
490
491 /*
492 * Renaming a directory under '.zfs/snapshot' will automatically trigger
493 * a rename of the snapshot to the new given name. The rename is confined
494 * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere.
495 */
496 /*ARGSUSED*/
497 int
498 zfsctl_snapdir_rename(struct inode *sdip, char *snm,
499 struct inode *tdip, char *tnm, cred_t *cr, int flags)
500 {
501 zfs_sb_t *zsb = ITOZSB(sdip);
502 zfs_snapentry_t search, *sep;
503 avl_index_t where;
504 char *to, *from, *real, *fsname;
505 int error;
506
507 ZFS_ENTER(zsb);
508
509 to = kmem_alloc(MAXNAMELEN, KM_SLEEP);
510 from = kmem_alloc(MAXNAMELEN, KM_SLEEP);
511 real = kmem_alloc(MAXNAMELEN, KM_SLEEP);
512 fsname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
513
514 if (zsb->z_case == ZFS_CASE_INSENSITIVE) {
515 error = dmu_snapshot_realname(zsb->z_os, snm, real,
516 MAXNAMELEN, NULL);
517 if (error == 0) {
518 snm = real;
519 } else if (error != ENOTSUP) {
520 goto out;
521 }
522 }
523
524 dmu_objset_name(zsb->z_os, fsname);
525
526 error = zfsctl_snapshot_zname(sdip, snm, MAXNAMELEN, from);
527 if (error == 0)
528 error = zfsctl_snapshot_zname(tdip, tnm, MAXNAMELEN, to);
529 if (error == 0)
530 error = zfs_secpolicy_rename_perms(from, to, cr);
531 if (error != 0)
532 goto out;
533
534 /*
535 * Cannot move snapshots out of the snapdir.
536 */
537 if (sdip != tdip) {
538 error = SET_ERROR(EINVAL);
539 goto out;
540 }
541
542 /*
543 * No-op when names are identical.
544 */
545 if (strcmp(snm, tnm) == 0) {
546 error = 0;
547 goto out;
548 }
549
550 mutex_enter(&zsb->z_ctldir_lock);
551
552 error = dsl_dataset_rename_snapshot(fsname, snm, tnm, B_FALSE);
553 if (error)
554 goto out_unlock;
555
556 search.se_name = (char *)snm;
557 sep = avl_find(&zsb->z_ctldir_snaps, &search, &where);
558 if (sep)
559 zfsctl_rename_snap(zsb, sep, tnm);
560
561 out_unlock:
562 mutex_exit(&zsb->z_ctldir_lock);
563 out:
564 kmem_free(from, MAXNAMELEN);
565 kmem_free(to, MAXNAMELEN);
566 kmem_free(real, MAXNAMELEN);
567 kmem_free(fsname, MAXNAMELEN);
568
569 ZFS_EXIT(zsb);
570
571 return (error);
572 }
573
574 /*
575 * Removing a directory under '.zfs/snapshot' will automatically trigger
576 * the removal of the snapshot with the given name.
577 */
578 /* ARGSUSED */
579 int
580 zfsctl_snapdir_remove(struct inode *dip, char *name, cred_t *cr, int flags)
581 {
582 zfs_sb_t *zsb = ITOZSB(dip);
583 char *snapname, *real;
584 int error;
585
586 ZFS_ENTER(zsb);
587
588 snapname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
589 real = kmem_alloc(MAXNAMELEN, KM_SLEEP);
590
591 if (zsb->z_case == ZFS_CASE_INSENSITIVE) {
592 error = dmu_snapshot_realname(zsb->z_os, name, real,
593 MAXNAMELEN, NULL);
594 if (error == 0) {
595 name = real;
596 } else if (error != ENOTSUP) {
597 goto out;
598 }
599 }
600
601 error = zfsctl_snapshot_zname(dip, name, MAXNAMELEN, snapname);
602 if (error == 0)
603 error = zfs_secpolicy_destroy_perms(snapname, cr);
604 if (error != 0)
605 goto out;
606
607 error = zfsctl_unmount_snapshot(zsb, name, MNT_FORCE);
608 if ((error == 0) || (error == ENOENT))
609 error = dsl_destroy_snapshot(snapname, B_FALSE);
610 out:
611 kmem_free(snapname, MAXNAMELEN);
612 kmem_free(real, MAXNAMELEN);
613
614 ZFS_EXIT(zsb);
615
616 return (error);
617 }
618
619 /*
620 * Creating a directory under '.zfs/snapshot' will automatically trigger
621 * the creation of a new snapshot with the given name.
622 */
623 /* ARGSUSED */
624 int
625 zfsctl_snapdir_mkdir(struct inode *dip, char *dirname, vattr_t *vap,
626 struct inode **ipp, cred_t *cr, int flags)
627 {
628 zfs_sb_t *zsb = ITOZSB(dip);
629 char *dsname;
630 int error;
631
632 dsname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
633
634 if (snapshot_namecheck(dirname, NULL, NULL) != 0) {
635 error = SET_ERROR(EILSEQ);
636 goto out;
637 }
638
639 dmu_objset_name(zsb->z_os, dsname);
640
641 error = zfs_secpolicy_snapshot_perms(dsname, cr);
642 if (error != 0)
643 goto out;
644
645 if (error == 0) {
646 error = dmu_objset_snapshot_one(dsname, dirname);
647 if (error != 0)
648 goto out;
649
650 error = zfsctl_snapdir_lookup(dip, dirname, ipp,
651 0, cr, NULL, NULL);
652 }
653 out:
654 kmem_free(dsname, MAXNAMELEN);
655
656 return (error);
657 }
658
659 /*
660 * When a .zfs/snapshot/<snapshot> inode is evicted they must be removed
661 * from the snapshot list. This will normally happen as part of the auto
662 * unmount, however in the case of a manual snapshot unmount this will be
663 * the only notification we receive.
664 */
665 void
666 zfsctl_snapdir_inactive(struct inode *ip)
667 {
668 zfs_sb_t *zsb = ITOZSB(ip);
669 zfs_snapentry_t *sep, *next;
670
671 mutex_enter(&zsb->z_ctldir_lock);
672
673 sep = avl_first(&zsb->z_ctldir_snaps);
674 while (sep != NULL) {
675 next = AVL_NEXT(&zsb->z_ctldir_snaps, sep);
676
677 if (sep->se_inode == ip) {
678 avl_remove(&zsb->z_ctldir_snaps, sep);
679 taskq_cancel_id(zfs_expire_taskq, sep->se_taskqid);
680 zfsctl_sep_free(sep);
681 break;
682 }
683 sep = next;
684 }
685
686 mutex_exit(&zsb->z_ctldir_lock);
687 }
688
689 /*
690 * Attempt to unmount a snapshot by making a call to user space.
691 * There is no assurance that this can or will succeed, is just a
692 * best effort. In the case where it does fail, perhaps because
693 * it's in use, the unmount will fail harmlessly.
694 */
695 #define SET_UNMOUNT_CMD \
696 "exec 0</dev/null " \
697 " 1>/dev/null " \
698 " 2>/dev/null; " \
699 "umount -t zfs -n %s'%s'"
700
701 static int
702 __zfsctl_unmount_snapshot(zfs_snapentry_t *sep, int flags)
703 {
704 char *argv[] = { "/bin/sh", "-c", NULL, NULL };
705 char *envp[] = { NULL };
706 int error;
707
708 argv[2] = kmem_asprintf(SET_UNMOUNT_CMD,
709 flags & MNT_FORCE ? "-f " : "", sep->se_path);
710 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
711 strfree(argv[2]);
712
713 /*
714 * The umount system utility will return 256 on error. We must
715 * assume this error is because the file system is busy so it is
716 * converted to the more sensible EBUSY.
717 */
718 if (error)
719 error = SET_ERROR(EBUSY);
720
721 /*
722 * This was the result of a manual unmount, cancel the delayed work
723 * to prevent zfsctl_expire_snapshot() from attempting a unmount.
724 */
725 if ((error == 0) && !(flags & MNT_EXPIRE))
726 taskq_cancel_id(zfs_expire_taskq, sep->se_taskqid);
727
728
729 return (error);
730 }
731
732 int
733 zfsctl_unmount_snapshot(zfs_sb_t *zsb, char *name, int flags)
734 {
735 zfs_snapentry_t search;
736 zfs_snapentry_t *sep;
737 int error = 0;
738
739 mutex_enter(&zsb->z_ctldir_lock);
740
741 search.se_name = name;
742 sep = avl_find(&zsb->z_ctldir_snaps, &search, NULL);
743 if (sep) {
744 avl_remove(&zsb->z_ctldir_snaps, sep);
745 mutex_exit(&zsb->z_ctldir_lock);
746
747 error = __zfsctl_unmount_snapshot(sep, flags);
748
749 mutex_enter(&zsb->z_ctldir_lock);
750 if (error == EBUSY)
751 avl_add(&zsb->z_ctldir_snaps, sep);
752 else
753 zfsctl_sep_free(sep);
754 } else {
755 error = SET_ERROR(ENOENT);
756 }
757
758 mutex_exit(&zsb->z_ctldir_lock);
759 ASSERT3S(error, >=, 0);
760
761 return (error);
762 }
763
764 /*
765 * Traverse all mounted snapshots and attempt to unmount them. This
766 * is best effort, on failure EEXIST is returned and count will be set
767 * to the number of file snapshots which could not be unmounted.
768 */
769 int
770 zfsctl_unmount_snapshots(zfs_sb_t *zsb, int flags, int *count)
771 {
772 zfs_snapentry_t *sep, *next;
773 int error = 0;
774
775 *count = 0;
776
777 ASSERT(zsb->z_ctldir != NULL);
778 mutex_enter(&zsb->z_ctldir_lock);
779
780 sep = avl_first(&zsb->z_ctldir_snaps);
781 while (sep != NULL) {
782 next = AVL_NEXT(&zsb->z_ctldir_snaps, sep);
783 avl_remove(&zsb->z_ctldir_snaps, sep);
784 mutex_exit(&zsb->z_ctldir_lock);
785
786 error = __zfsctl_unmount_snapshot(sep, flags);
787
788 mutex_enter(&zsb->z_ctldir_lock);
789 if (error == EBUSY) {
790 avl_add(&zsb->z_ctldir_snaps, sep);
791 (*count)++;
792 } else {
793 zfsctl_sep_free(sep);
794 }
795
796 sep = next;
797 }
798
799 mutex_exit(&zsb->z_ctldir_lock);
800
801 return ((*count > 0) ? EEXIST : 0);
802 }
803
804 #define MOUNT_BUSY 0x80 /* Mount failed due to EBUSY (from mntent.h) */
805
806 #define SET_MOUNT_CMD \
807 "exec 0</dev/null " \
808 " 1>/dev/null " \
809 " 2>/dev/null; " \
810 "mount -t zfs -n '%s' '%s'"
811
812 int
813 zfsctl_mount_snapshot(struct path *path, int flags)
814 {
815 struct dentry *dentry = path->dentry;
816 struct inode *ip = dentry->d_inode;
817 zfs_sb_t *zsb = ITOZSB(ip);
818 char *full_name, *full_path;
819 zfs_snapentry_t *sep;
820 zfs_snapentry_t search;
821 char *argv[] = { "/bin/sh", "-c", NULL, NULL };
822 char *envp[] = { NULL };
823 int error;
824
825 ZFS_ENTER(zsb);
826
827 full_name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
828 full_path = kmem_zalloc(PATH_MAX, KM_SLEEP);
829
830 error = zfsctl_snapshot_zname(ip, dname(dentry), MAXNAMELEN, full_name);
831 if (error)
832 goto error;
833
834 error = zfsctl_snapshot_zpath(path, PATH_MAX, full_path);
835 if (error)
836 goto error;
837
838 /*
839 * Attempt to mount the snapshot from user space. Normally this
840 * would be done using the vfs_kern_mount() function, however that
841 * function is marked GPL-only and cannot be used. On error we
842 * careful to log the real error to the console and return EISDIR
843 * to safely abort the automount. This should be very rare.
844 *
845 * If the user mode helper happens to return EBUSY, a concurrent
846 * mount is already in progress in which case the error is ignored.
847 * Take note that if the program was executed successfully the return
848 * value from call_usermodehelper() will be (exitcode << 8 + signal).
849 */
850 argv[2] = kmem_asprintf(SET_MOUNT_CMD, full_name, full_path);
851 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
852 strfree(argv[2]);
853 if (error && !(error & MOUNT_BUSY << 8)) {
854 printk("ZFS: Unable to automount %s at %s: %d\n",
855 full_name, full_path, error);
856 error = SET_ERROR(EISDIR);
857 goto error;
858 }
859
860 error = 0;
861 mutex_enter(&zsb->z_ctldir_lock);
862
863 /*
864 * Ensure a previous entry does not exist, if it does safely remove
865 * it any cancel the outstanding expiration. This can occur when a
866 * snapshot is manually unmounted and then an automount is triggered.
867 */
868 search.se_name = full_name;
869 sep = avl_find(&zsb->z_ctldir_snaps, &search, NULL);
870 if (sep) {
871 avl_remove(&zsb->z_ctldir_snaps, sep);
872 taskq_cancel_id(zfs_expire_taskq, sep->se_taskqid);
873 zfsctl_sep_free(sep);
874 }
875
876 sep = zfsctl_sep_alloc();
877 sep->se_name = full_name;
878 sep->se_path = full_path;
879 sep->se_inode = ip;
880 avl_add(&zsb->z_ctldir_snaps, sep);
881
882 sep->se_taskqid = taskq_dispatch_delay(zfs_expire_taskq,
883 zfsctl_expire_snapshot, sep, TQ_SLEEP,
884 ddi_get_lbolt() + zfs_expire_snapshot * HZ);
885
886 mutex_exit(&zsb->z_ctldir_lock);
887 error:
888 if (error) {
889 kmem_free(full_name, MAXNAMELEN);
890 kmem_free(full_path, PATH_MAX);
891 }
892
893 ZFS_EXIT(zsb);
894
895 return (error);
896 }
897
898 /*
899 * Check if this super block has a matching objset id.
900 */
901 static int
902 zfsctl_test_super(struct super_block *sb, void *objsetidp)
903 {
904 zfs_sb_t *zsb = sb->s_fs_info;
905 uint64_t objsetid = *(uint64_t *)objsetidp;
906
907 return (dmu_objset_id(zsb->z_os) == objsetid);
908 }
909
910 /*
911 * Prevent a new super block from being allocated if an existing one
912 * could not be located. We only want to preform a lookup operation.
913 */
914 static int
915 zfsctl_set_super(struct super_block *sb, void *objsetidp)
916 {
917 return (-EEXIST);
918 }
919
920 int
921 zfsctl_lookup_objset(struct super_block *sb, uint64_t objsetid, zfs_sb_t **zsbp)
922 {
923 zfs_sb_t *zsb = sb->s_fs_info;
924 struct super_block *sbp;
925 zfs_snapentry_t *sep;
926 uint64_t id;
927 int error;
928
929 ASSERT(zsb->z_ctldir != NULL);
930
931 mutex_enter(&zsb->z_ctldir_lock);
932
933 /*
934 * Verify that the snapshot is mounted.
935 */
936 sep = avl_first(&zsb->z_ctldir_snaps);
937 while (sep != NULL) {
938 error = dmu_snapshot_lookup(zsb->z_os, sep->se_name, &id);
939 if (error)
940 goto out;
941
942 if (id == objsetid)
943 break;
944
945 sep = AVL_NEXT(&zsb->z_ctldir_snaps, sep);
946 }
947
948 if (sep != NULL) {
949 /*
950 * Lookup the mounted root rather than the covered mount
951 * point. This may fail if the snapshot has just been
952 * unmounted by an unrelated user space process. This
953 * race cannot occur to an expired mount point because
954 * we hold the zsb->z_ctldir_lock to prevent the race.
955 */
956 sbp = zpl_sget(&zpl_fs_type, zfsctl_test_super,
957 zfsctl_set_super, 0, &id);
958 if (IS_ERR(sbp)) {
959 error = -PTR_ERR(sbp);
960 } else {
961 *zsbp = sbp->s_fs_info;
962 deactivate_super(sbp);
963 }
964 } else {
965 error = SET_ERROR(EINVAL);
966 }
967 out:
968 mutex_exit(&zsb->z_ctldir_lock);
969 ASSERT3S(error, >=, 0);
970
971 return (error);
972 }
973
974 /* ARGSUSED */
975 int
976 zfsctl_shares_lookup(struct inode *dip, char *name, struct inode **ipp,
977 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
978 {
979 zfs_sb_t *zsb = ITOZSB(dip);
980 struct inode *ip;
981 znode_t *dzp;
982 int error;
983
984 ZFS_ENTER(zsb);
985
986 if (zsb->z_shares_dir == 0) {
987 ZFS_EXIT(zsb);
988 return (SET_ERROR(ENOTSUP));
989 }
990
991 error = zfs_zget(zsb, zsb->z_shares_dir, &dzp);
992 if (error) {
993 ZFS_EXIT(zsb);
994 return (error);
995 }
996
997 error = zfs_lookup(ZTOI(dzp), name, &ip, 0, cr, NULL, NULL);
998
999 iput(ZTOI(dzp));
1000 ZFS_EXIT(zsb);
1001
1002 return (error);
1003 }
1004
1005
1006 /*
1007 * Initialize the various pieces we'll need to create and manipulate .zfs
1008 * directories. Currently this is unused but available.
1009 */
1010 void
1011 zfsctl_init(void)
1012 {
1013 zfs_expire_taskq = taskq_create("z_unmount", 1, maxclsyspri,
1014 1, 8, TASKQ_PREPOPULATE);
1015 }
1016
1017 /*
1018 * Cleanup the various pieces we needed for .zfs directories. In particular
1019 * ensure the expiry timer is canceled safely.
1020 */
1021 void
1022 zfsctl_fini(void)
1023 {
1024 taskq_destroy(zfs_expire_taskq);
1025 }
1026
1027 module_param(zfs_expire_snapshot, int, 0644);
1028 MODULE_PARM_DESC(zfs_expire_snapshot, "Seconds to expire .zfs/snapshot");