]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/zfs/zfs_ctldir.c
Account for .zfs ctldir inodes
[mirror_zfs-debian.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 zsb->z_nr_znodes++;
216 membar_producer();
217 mutex_exit(&zsb->z_znodes_lock);
218
219 unlock_new_inode(ip);
220
221 return (ip);
222 }
223
224 /*
225 * Lookup the inode with given id, it will be allocated if needed.
226 */
227 static struct inode *
228 zfsctl_inode_lookup(zfs_sb_t *zsb, unsigned long id,
229 const struct file_operations *fops, const struct inode_operations *ops)
230 {
231 struct inode *ip = NULL;
232
233 while (ip == NULL) {
234 ip = ilookup(zsb->z_sb, id);
235 if (ip)
236 break;
237
238 /* May fail due to concurrent zfsctl_inode_alloc() */
239 ip = zfsctl_inode_alloc(zsb, id, fops, ops);
240 }
241
242 return (ip);
243 }
244
245 /*
246 * Free zfsctl inode specific structures, currently there are none.
247 */
248 void
249 zfsctl_inode_destroy(struct inode *ip)
250 {
251 return;
252 }
253
254 /*
255 * An inode is being evicted from the cache.
256 */
257 void
258 zfsctl_inode_inactive(struct inode *ip)
259 {
260 if (zfsctl_is_snapdir(ip))
261 zfsctl_snapdir_inactive(ip);
262 }
263
264 /*
265 * Create the '.zfs' directory. This directory is cached as part of the VFS
266 * structure. This results in a hold on the zfs_sb_t. The code in zfs_umount()
267 * therefore checks against a vfs_count of 2 instead of 1. This reference
268 * is removed when the ctldir is destroyed in the unmount. All other entities
269 * under the '.zfs' directory are created dynamically as needed.
270 */
271 int
272 zfsctl_create(zfs_sb_t *zsb)
273 {
274 ASSERT(zsb->z_ctldir == NULL);
275
276 zsb->z_ctldir = zfsctl_inode_alloc(zsb, ZFSCTL_INO_ROOT,
277 &zpl_fops_root, &zpl_ops_root);
278 if (zsb->z_ctldir == NULL)
279 return (ENOENT);
280
281 return (0);
282 }
283
284 /*
285 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
286 */
287 void
288 zfsctl_destroy(zfs_sb_t *zsb)
289 {
290 iput(zsb->z_ctldir);
291 zsb->z_ctldir = NULL;
292 }
293
294 /*
295 * Given a root znode, retrieve the associated .zfs directory.
296 * Add a hold to the vnode and return it.
297 */
298 struct inode *
299 zfsctl_root(znode_t *zp)
300 {
301 ASSERT(zfs_has_ctldir(zp));
302 igrab(ZTOZSB(zp)->z_ctldir);
303 return (ZTOZSB(zp)->z_ctldir);
304 }
305
306 /*ARGSUSED*/
307 int
308 zfsctl_fid(struct inode *ip, fid_t *fidp)
309 {
310 znode_t *zp = ITOZ(ip);
311 zfs_sb_t *zsb = ITOZSB(ip);
312 uint64_t object = zp->z_id;
313 zfid_short_t *zfid;
314 int i;
315
316 ZFS_ENTER(zsb);
317
318 if (fidp->fid_len < SHORT_FID_LEN) {
319 fidp->fid_len = SHORT_FID_LEN;
320 ZFS_EXIT(zsb);
321 return (ENOSPC);
322 }
323
324 zfid = (zfid_short_t *)fidp;
325
326 zfid->zf_len = SHORT_FID_LEN;
327
328 for (i = 0; i < sizeof (zfid->zf_object); i++)
329 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
330
331 /* .zfs znodes always have a generation number of 0 */
332 for (i = 0; i < sizeof (zfid->zf_gen); i++)
333 zfid->zf_gen[i] = 0;
334
335 ZFS_EXIT(zsb);
336 return (0);
337 }
338
339 static int
340 zfsctl_snapshot_zname(struct inode *ip, const char *name, int len, char *zname)
341 {
342 objset_t *os = ITOZSB(ip)->z_os;
343
344 if (snapshot_namecheck(name, NULL, NULL) != 0)
345 return (EILSEQ);
346
347 dmu_objset_name(os, zname);
348 if ((strlen(zname) + 1 + strlen(name)) >= len)
349 return (ENAMETOOLONG);
350
351 (void) strcat(zname, "@");
352 (void) strcat(zname, name);
353
354 return (0);
355 }
356
357 static int
358 zfsctl_snapshot_zpath(struct path *path, int len, char *zpath)
359 {
360 char *path_buffer, *path_ptr;
361 int path_len, error = 0;
362
363 path_buffer = kmem_alloc(len, KM_SLEEP);
364
365 path_ptr = d_path(path, path_buffer, len);
366 if (IS_ERR(path_ptr)) {
367 error = -PTR_ERR(path_ptr);
368 goto out;
369 }
370
371 path_len = path_buffer + len - 1 - path_ptr;
372 if (path_len > len) {
373 error = EFAULT;
374 goto out;
375 }
376
377 memcpy(zpath, path_ptr, path_len);
378 zpath[path_len] = '\0';
379 out:
380 kmem_free(path_buffer, len);
381
382 return (error);
383 }
384
385 /*
386 * Special case the handling of "..".
387 */
388 /* ARGSUSED */
389 int
390 zfsctl_root_lookup(struct inode *dip, char *name, struct inode **ipp,
391 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
392 {
393 zfs_sb_t *zsb = ITOZSB(dip);
394 int error = 0;
395
396 ZFS_ENTER(zsb);
397
398 if (strcmp(name, "..") == 0) {
399 *ipp = dip->i_sb->s_root->d_inode;
400 } else if (strcmp(name, ZFS_SNAPDIR_NAME) == 0) {
401 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIR,
402 &zpl_fops_snapdir, &zpl_ops_snapdir);
403 } else if (strcmp(name, ZFS_SHAREDIR_NAME) == 0) {
404 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SHARES,
405 &zpl_fops_shares, &zpl_ops_shares);
406 } else {
407 *ipp = NULL;
408 }
409
410 if (*ipp == NULL)
411 error = ENOENT;
412
413 ZFS_EXIT(zsb);
414
415 return (error);
416 }
417
418 /*
419 * Lookup entry point for the 'snapshot' directory. Try to open the
420 * snapshot if it exist, creating the pseudo filesystem inode as necessary.
421 * Perform a mount of the associated dataset on top of the inode.
422 */
423 /* ARGSUSED */
424 int
425 zfsctl_snapdir_lookup(struct inode *dip, char *name, struct inode **ipp,
426 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
427 {
428 zfs_sb_t *zsb = ITOZSB(dip);
429 uint64_t id;
430 int error;
431
432 ZFS_ENTER(zsb);
433
434 error = dmu_snapshot_id(zsb->z_os, name, &id);
435 if (error) {
436 ZFS_EXIT(zsb);
437 return (error);
438 }
439
440 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIRS - id,
441 &simple_dir_operations, &simple_dir_inode_operations);
442 if (*ipp) {
443 #ifdef HAVE_AUTOMOUNT
444 (*ipp)->i_flags |= S_AUTOMOUNT;
445 #endif /* HAVE_AUTOMOUNT */
446 } else {
447 error = ENOENT;
448 }
449
450 ZFS_EXIT(zsb);
451
452 return (error);
453 }
454
455 static void
456 zfsctl_rename_snap(zfs_sb_t *zsb, zfs_snapentry_t *sep, const char *name)
457 {
458 avl_index_t where;
459
460 ASSERT(MUTEX_HELD(&zsb->z_ctldir_lock));
461 ASSERT(sep != NULL);
462
463 /*
464 * Change the name in the AVL tree.
465 */
466 avl_remove(&zsb->z_ctldir_snaps, sep);
467 (void) strcpy(sep->se_name, name);
468 VERIFY(avl_find(&zsb->z_ctldir_snaps, sep, &where) == NULL);
469 avl_insert(&zsb->z_ctldir_snaps, sep, where);
470 }
471
472 /*
473 * Renaming a directory under '.zfs/snapshot' will automatically trigger
474 * a rename of the snapshot to the new given name. The rename is confined
475 * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere.
476 */
477 /*ARGSUSED*/
478 int
479 zfsctl_snapdir_rename(struct inode *sdip, char *sname,
480 struct inode *tdip, char *tname, cred_t *cr, int flags)
481 {
482 zfs_sb_t *zsb = ITOZSB(sdip);
483 zfs_snapentry_t search, *sep;
484 avl_index_t where;
485 char *to, *from, *real;
486 int error;
487
488 ZFS_ENTER(zsb);
489
490 to = kmem_alloc(MAXNAMELEN, KM_SLEEP);
491 from = kmem_alloc(MAXNAMELEN, KM_SLEEP);
492 real = kmem_alloc(MAXNAMELEN, KM_SLEEP);
493
494 if (zsb->z_case == ZFS_CASE_INSENSITIVE) {
495 error = dmu_snapshot_realname(zsb->z_os, sname, real,
496 MAXNAMELEN, NULL);
497 if (error == 0) {
498 sname = real;
499 } else if (error != ENOTSUP) {
500 goto out;
501 }
502 }
503
504 error = zfsctl_snapshot_zname(sdip, sname, MAXNAMELEN, from);
505 if (!error)
506 error = zfsctl_snapshot_zname(tdip, tname, MAXNAMELEN, to);
507 if (!error)
508 error = zfs_secpolicy_rename_perms(from, to, cr);
509 if (error)
510 goto out;
511
512 /*
513 * Cannot move snapshots out of the snapdir.
514 */
515 if (sdip != tdip) {
516 error = EINVAL;
517 goto out;
518 }
519
520 /*
521 * No-op when names are identical.
522 */
523 if (strcmp(sname, tname) == 0) {
524 error = 0;
525 goto out;
526 }
527
528 mutex_enter(&zsb->z_ctldir_lock);
529
530 error = dmu_objset_rename(from, to, B_FALSE);
531 if (error)
532 goto out_unlock;
533
534 search.se_name = (char *)sname;
535 sep = avl_find(&zsb->z_ctldir_snaps, &search, &where);
536 if (sep)
537 zfsctl_rename_snap(zsb, sep, tname);
538
539 out_unlock:
540 mutex_exit(&zsb->z_ctldir_lock);
541 out:
542 kmem_free(from, MAXNAMELEN);
543 kmem_free(to, MAXNAMELEN);
544 kmem_free(real, MAXNAMELEN);
545
546 ZFS_EXIT(zsb);
547
548 return (error);
549 }
550
551 /*
552 * Removing a directory under '.zfs/snapshot' will automatically trigger
553 * the removal of the snapshot with the given name.
554 */
555 /* ARGSUSED */
556 int
557 zfsctl_snapdir_remove(struct inode *dip, char *name, cred_t *cr, int flags)
558 {
559 zfs_sb_t *zsb = ITOZSB(dip);
560 char *snapname, *real;
561 int error;
562
563 ZFS_ENTER(zsb);
564
565 snapname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
566 real = kmem_alloc(MAXNAMELEN, KM_SLEEP);
567
568 if (zsb->z_case == ZFS_CASE_INSENSITIVE) {
569 error = dmu_snapshot_realname(zsb->z_os, name, real,
570 MAXNAMELEN, NULL);
571 if (error == 0) {
572 name = real;
573 } else if (error != ENOTSUP) {
574 goto out;
575 }
576 }
577
578 error = zfsctl_snapshot_zname(dip, name, MAXNAMELEN, snapname);
579 if (!error)
580 error = zfs_secpolicy_destroy_perms(snapname, cr);
581 if (error)
582 goto out;
583
584 error = zfsctl_unmount_snapshot(zsb, name, MNT_FORCE);
585 if ((error == 0) || (error == ENOENT))
586 error = dmu_objset_destroy(snapname, B_FALSE);
587 out:
588 kmem_free(snapname, MAXNAMELEN);
589 kmem_free(real, MAXNAMELEN);
590
591 ZFS_EXIT(zsb);
592
593 return (error);
594 }
595
596 /*
597 * Creating a directory under '.zfs/snapshot' will automatically trigger
598 * the creation of a new snapshot with the given name.
599 */
600 /* ARGSUSED */
601 int
602 zfsctl_snapdir_mkdir(struct inode *dip, char *dirname, vattr_t *vap,
603 struct inode **ipp, cred_t *cr, int flags)
604 {
605 zfs_sb_t *zsb = ITOZSB(dip);
606 char *dsname;
607 int error;
608
609 dsname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
610
611 if (snapshot_namecheck(dirname, NULL, NULL) != 0) {
612 error = EILSEQ;
613 goto out;
614 }
615
616 dmu_objset_name(zsb->z_os, dsname);
617
618 error = zfs_secpolicy_snapshot_perms(dsname, cr);
619 if (error)
620 goto out;
621
622 if (error == 0) {
623 error = dmu_objset_snapshot(dsname, dirname,
624 NULL, NULL, B_FALSE, B_FALSE, -1);
625 if (error)
626 goto out;
627
628 error = zfsctl_snapdir_lookup(dip, dirname, ipp,
629 0, cr, NULL, NULL);
630 }
631 out:
632 kmem_free(dsname, MAXNAMELEN);
633
634 return (error);
635 }
636
637 /*
638 * When a .zfs/snapshot/<snapshot> inode is evicted they must be removed
639 * from the snapshot list. This will normally happen as part of the auto
640 * unmount, however in the case of a manual snapshot unmount this will be
641 * the only notification we receive.
642 */
643 void
644 zfsctl_snapdir_inactive(struct inode *ip)
645 {
646 zfs_sb_t *zsb = ITOZSB(ip);
647 zfs_snapentry_t *sep, *next;
648
649 mutex_enter(&zsb->z_ctldir_lock);
650
651 sep = avl_first(&zsb->z_ctldir_snaps);
652 while (sep != NULL) {
653 next = AVL_NEXT(&zsb->z_ctldir_snaps, sep);
654
655 if (sep->se_inode == ip) {
656 avl_remove(&zsb->z_ctldir_snaps, sep);
657 cancel_delayed_work_sync(&sep->se_work);
658 zfsctl_sep_free(sep);
659 break;
660 }
661 sep = next;
662 }
663
664 mutex_exit(&zsb->z_ctldir_lock);
665 }
666
667 /*
668 * Attempt to unmount a snapshot by making a call to user space.
669 * There is no assurance that this can or will succeed, is just a
670 * best effort. In the case where it does fail, perhaps because
671 * it's in use, the unmount will fail harmlessly.
672 */
673 #define SET_UNMOUNT_CMD \
674 "exec 0</dev/null " \
675 " 1>/dev/null " \
676 " 2>/dev/null; " \
677 "umount -t zfs -n %s%s"
678
679 static int
680 __zfsctl_unmount_snapshot(zfs_snapentry_t *sep, int flags)
681 {
682 char *argv[] = { "/bin/sh", "-c", NULL, NULL };
683 char *envp[] = { NULL };
684 int error;
685
686 argv[2] = kmem_asprintf(SET_UNMOUNT_CMD,
687 flags & MNT_FORCE ? "-f " : "", sep->se_path);
688 error = call_usermodehelper(argv[0], argv, envp, 1);
689 strfree(argv[2]);
690
691 /*
692 * The umount system utility will return 256 on error. We must
693 * assume this error is because the file system is busy so it is
694 * converted to the more sensible EBUSY.
695 */
696 if (error)
697 error = EBUSY;
698
699 /*
700 * This was the result of a manual unmount, cancel the delayed work
701 * to prevent zfsctl_expire_snapshot() from attempting a unmount.
702 */
703 if ((error == 0) && !(flags & MNT_EXPIRE))
704 cancel_delayed_work(&sep->se_work);
705
706 return (error);
707 }
708
709 int
710 zfsctl_unmount_snapshot(zfs_sb_t *zsb, char *name, int flags)
711 {
712 zfs_snapentry_t search;
713 zfs_snapentry_t *sep;
714 int error = 0;
715
716 mutex_enter(&zsb->z_ctldir_lock);
717
718 search.se_name = name;
719 sep = avl_find(&zsb->z_ctldir_snaps, &search, NULL);
720 if (sep) {
721 avl_remove(&zsb->z_ctldir_snaps, sep);
722 error = __zfsctl_unmount_snapshot(sep, flags);
723 if (error == EBUSY)
724 avl_add(&zsb->z_ctldir_snaps, sep);
725 else
726 zfsctl_sep_free(sep);
727 } else {
728 error = ENOENT;
729 }
730
731 mutex_exit(&zsb->z_ctldir_lock);
732 ASSERT3S(error, >=, 0);
733
734 return (error);
735 }
736
737 /*
738 * Traverse all mounted snapshots and attempt to unmount them. This
739 * is best effort, on failure EEXIST is returned and count will be set
740 * to the number of file snapshots which could not be unmounted.
741 */
742 int
743 zfsctl_unmount_snapshots(zfs_sb_t *zsb, int flags, int *count)
744 {
745 zfs_snapentry_t *sep, *next;
746 int error = 0;
747
748 *count = 0;
749
750 ASSERT(zsb->z_ctldir != NULL);
751 mutex_enter(&zsb->z_ctldir_lock);
752
753 sep = avl_first(&zsb->z_ctldir_snaps);
754 while (sep != NULL) {
755 next = AVL_NEXT(&zsb->z_ctldir_snaps, sep);
756 avl_remove(&zsb->z_ctldir_snaps, sep);
757 error = __zfsctl_unmount_snapshot(sep, flags);
758 if (error == EBUSY) {
759 avl_add(&zsb->z_ctldir_snaps, sep);
760 (*count)++;
761 } else {
762 zfsctl_sep_free(sep);
763 }
764
765 sep = next;
766 }
767
768 mutex_exit(&zsb->z_ctldir_lock);
769
770 return ((*count > 0) ? EEXIST : 0);
771 }
772
773 #define SET_MOUNT_CMD \
774 "exec 0</dev/null " \
775 " 1>/dev/null " \
776 " 2>/dev/null; " \
777 "mount -t zfs -n %s %s"
778
779 int
780 zfsctl_mount_snapshot(struct path *path, int flags)
781 {
782 struct dentry *dentry = path->dentry;
783 struct inode *ip = dentry->d_inode;
784 zfs_sb_t *zsb = ITOZSB(ip);
785 char *full_name, *full_path;
786 zfs_snapentry_t *sep;
787 zfs_snapentry_t search;
788 char *argv[] = { "/bin/sh", "-c", NULL, NULL };
789 char *envp[] = { NULL };
790 int error;
791
792 ZFS_ENTER(zsb);
793
794 full_name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
795 full_path = kmem_zalloc(PATH_MAX, KM_SLEEP);
796
797 error = zfsctl_snapshot_zname(ip, dname(dentry), MAXNAMELEN, full_name);
798 if (error)
799 goto error;
800
801 error = zfsctl_snapshot_zpath(path, PATH_MAX, full_path);
802 if (error)
803 goto error;
804
805 /*
806 * Attempt to mount the snapshot from user space. Normally this
807 * would be done using the vfs_kern_mount() function, however that
808 * function is marked GPL-only and cannot be used. On error we
809 * careful to log the real error to the console and return EISDIR
810 * to safely abort the automount. This should be very rare.
811 */
812 argv[2] = kmem_asprintf(SET_MOUNT_CMD, full_name, full_path);
813 error = call_usermodehelper(argv[0], argv, envp, 1);
814 strfree(argv[2]);
815 if (error) {
816 printk("ZFS: Unable to automount %s at %s: %d\n",
817 full_name, full_path, error);
818 error = EISDIR;
819 goto error;
820 }
821
822 mutex_enter(&zsb->z_ctldir_lock);
823
824 /*
825 * Ensure a previous entry does not exist, if it does safely remove
826 * it any cancel the outstanding expiration. This can occur when a
827 * snapshot is manually unmounted and then an automount is triggered.
828 */
829 search.se_name = full_name;
830 sep = avl_find(&zsb->z_ctldir_snaps, &search, NULL);
831 if (sep) {
832 avl_remove(&zsb->z_ctldir_snaps, sep);
833 cancel_delayed_work_sync(&sep->se_work);
834 zfsctl_sep_free(sep);
835 }
836
837 sep = zfsctl_sep_alloc();
838 sep->se_name = full_name;
839 sep->se_path = full_path;
840 sep->se_inode = ip;
841 avl_add(&zsb->z_ctldir_snaps, sep);
842
843 spl_init_delayed_work(&sep->se_work, zfsctl_expire_snapshot, sep);
844 schedule_delayed_work(&sep->se_work, zfs_expire_snapshot * HZ);
845
846 mutex_exit(&zsb->z_ctldir_lock);
847 error:
848 if (error) {
849 kmem_free(full_name, MAXNAMELEN);
850 kmem_free(full_path, PATH_MAX);
851 }
852
853 ZFS_EXIT(zsb);
854
855 return (error);
856 }
857
858 /*
859 * Check if this super block has a matching objset id.
860 */
861 static int
862 zfsctl_test_super(struct super_block *sb, void *objsetidp)
863 {
864 zfs_sb_t *zsb = sb->s_fs_info;
865 uint64_t objsetid = *(uint64_t *)objsetidp;
866
867 return (dmu_objset_id(zsb->z_os) == objsetid);
868 }
869
870 /*
871 * Prevent a new super block from being allocated if an existing one
872 * could not be located. We only want to preform a lookup operation.
873 */
874 static int
875 zfsctl_set_super(struct super_block *sb, void *objsetidp)
876 {
877 return (-EEXIST);
878 }
879
880 int
881 zfsctl_lookup_objset(struct super_block *sb, uint64_t objsetid, zfs_sb_t **zsbp)
882 {
883 zfs_sb_t *zsb = sb->s_fs_info;
884 struct super_block *sbp;
885 zfs_snapentry_t *sep;
886 uint64_t id;
887 int error;
888
889 ASSERT(zsb->z_ctldir != NULL);
890
891 mutex_enter(&zsb->z_ctldir_lock);
892
893 /*
894 * Verify that the snapshot is mounted.
895 */
896 sep = avl_first(&zsb->z_ctldir_snaps);
897 while (sep != NULL) {
898 error = dmu_snapshot_id(zsb->z_os, sep->se_name, &id);
899 if (error)
900 goto out;
901
902 if (id == objsetid)
903 break;
904
905 sep = AVL_NEXT(&zsb->z_ctldir_snaps, sep);
906 }
907
908 if (sep != NULL) {
909 /*
910 * Lookup the mounted root rather than the covered mount
911 * point. This may fail if the snapshot has just been
912 * unmounted by an unrelated user space process. This
913 * race cannot occur to an expired mount point because
914 * we hold the zsb->z_ctldir_lock to prevent the race.
915 */
916 sbp = sget(&zpl_fs_type, zfsctl_test_super,
917 zfsctl_set_super, &id);
918 if (IS_ERR(sbp)) {
919 error = -PTR_ERR(sbp);
920 } else {
921 *zsbp = sbp->s_fs_info;
922 deactivate_super(sbp);
923 }
924 } else {
925 error = EINVAL;
926 }
927 out:
928 mutex_exit(&zsb->z_ctldir_lock);
929 ASSERT3S(error, >=, 0);
930
931 return (error);
932 }
933
934 /* ARGSUSED */
935 int
936 zfsctl_shares_lookup(struct inode *dip, char *name, struct inode **ipp,
937 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
938 {
939 zfs_sb_t *zsb = ITOZSB(dip);
940 struct inode *ip;
941 znode_t *dzp;
942 int error;
943
944 ZFS_ENTER(zsb);
945
946 if (zsb->z_shares_dir == 0) {
947 ZFS_EXIT(zsb);
948 return (-ENOTSUP);
949 }
950
951 error = zfs_zget(zsb, zsb->z_shares_dir, &dzp);
952 if (error) {
953 ZFS_EXIT(zsb);
954 return (error);
955 }
956
957 error = zfs_lookup(ZTOI(dzp), name, &ip, 0, cr, NULL, NULL);
958
959 iput(ZTOI(dzp));
960 ZFS_EXIT(zsb);
961
962 return (error);
963 }
964
965
966 /*
967 * Initialize the various pieces we'll need to create and manipulate .zfs
968 * directories. Currently this is unused but available.
969 */
970 void
971 zfsctl_init(void)
972 {
973 }
974
975 /*
976 * Cleanup the various pieces we needed for .zfs directories. In particular
977 * ensure the expiry timer is canceled safely.
978 */
979 void
980 zfsctl_fini(void)
981 {
982 }
983
984 module_param(zfs_expire_snapshot, int, 0644);
985 MODULE_PARM_DESC(zfs_expire_snapshot, "Seconds to expire .zfs/snapshot");