]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_ctldir.c
Simplify spa_sync by breaking it up to smaller functions
[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 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
32 * Copyright (c) 2018 George Melikov. All Rights Reserved.
33 */
34
35 /*
36 * ZFS control directory (a.k.a. ".zfs")
37 *
38 * This directory provides a common location for all ZFS meta-objects.
39 * Currently, this is only the 'snapshot' and 'shares' directory, but this may
40 * expand in the future. The elements are built dynamically, as the hierarchy
41 * does not actually exist on disk.
42 *
43 * For 'snapshot', we don't want to have all snapshots always mounted, because
44 * this would take up a huge amount of space in /etc/mnttab. We have three
45 * types of objects:
46 *
47 * ctldir ------> snapshotdir -------> snapshot
48 * |
49 * |
50 * V
51 * mounted fs
52 *
53 * The 'snapshot' node contains just enough information to lookup '..' and act
54 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
55 * perform an automount of the underlying filesystem and return the
56 * corresponding inode.
57 *
58 * All mounts are handled automatically by an user mode helper which invokes
59 * the mount mount procedure. Unmounts are handled by allowing the mount
60 * point to expire so the kernel may automatically unmount it.
61 *
62 * The '.zfs', '.zfs/snapshot', and all directories created under
63 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') all share the same
64 * share the same zfsvfs_t as the head filesystem (what '.zfs' lives under).
65 *
66 * File systems mounted on top of the '.zfs/snapshot/<snapname>' paths
67 * (ie: snapshots) are complete ZFS filesystems and have their own unique
68 * zfsvfs_t. However, the fsid reported by these mounts will be the same
69 * as that used by the parent zfsvfs_t to make NFS happy.
70 */
71
72 #include <sys/types.h>
73 #include <sys/param.h>
74 #include <sys/time.h>
75 #include <sys/sysmacros.h>
76 #include <sys/pathname.h>
77 #include <sys/vfs.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/dmu_objset.h>
85 #include <sys/dsl_destroy.h>
86 #include <sys/dsl_deleg.h>
87 #include <sys/zpl.h>
88 #include "zfs_namecheck.h"
89
90 /*
91 * Two AVL trees are maintained which contain all currently automounted
92 * snapshots. Every automounted snapshots maps to a single zfs_snapentry_t
93 * entry which MUST:
94 *
95 * - be attached to both trees, and
96 * - be unique, no duplicate entries are allowed.
97 *
98 * The zfs_snapshots_by_name tree is indexed by the full dataset name
99 * while the zfs_snapshots_by_objsetid tree is indexed by the unique
100 * objsetid. This allows for fast lookups either by name or objsetid.
101 */
102 static avl_tree_t zfs_snapshots_by_name;
103 static avl_tree_t zfs_snapshots_by_objsetid;
104 static krwlock_t zfs_snapshot_lock;
105
106 /*
107 * Control Directory Tunables (.zfs)
108 */
109 int zfs_expire_snapshot = ZFSCTL_EXPIRE_SNAPSHOT;
110 int zfs_admin_snapshot = 0;
111
112 typedef struct {
113 char *se_name; /* full snapshot name */
114 char *se_path; /* full mount path */
115 spa_t *se_spa; /* pool spa */
116 uint64_t se_objsetid; /* snapshot objset id */
117 struct dentry *se_root_dentry; /* snapshot root dentry */
118 taskqid_t se_taskqid; /* scheduled unmount taskqid */
119 avl_node_t se_node_name; /* zfs_snapshots_by_name link */
120 avl_node_t se_node_objsetid; /* zfs_snapshots_by_objsetid link */
121 zfs_refcount_t se_refcount; /* reference count */
122 } zfs_snapentry_t;
123
124 static void zfsctl_snapshot_unmount_delay_impl(zfs_snapentry_t *se, int delay);
125
126 /*
127 * Allocate a new zfs_snapentry_t being careful to make a copy of the
128 * the snapshot name and provided mount point. No reference is taken.
129 */
130 static zfs_snapentry_t *
131 zfsctl_snapshot_alloc(char *full_name, char *full_path, spa_t *spa,
132 uint64_t objsetid, struct dentry *root_dentry)
133 {
134 zfs_snapentry_t *se;
135
136 se = kmem_zalloc(sizeof (zfs_snapentry_t), KM_SLEEP);
137
138 se->se_name = strdup(full_name);
139 se->se_path = strdup(full_path);
140 se->se_spa = spa;
141 se->se_objsetid = objsetid;
142 se->se_root_dentry = root_dentry;
143 se->se_taskqid = TASKQID_INVALID;
144
145 zfs_refcount_create(&se->se_refcount);
146
147 return (se);
148 }
149
150 /*
151 * Free a zfs_snapentry_t the called must ensure there are no active
152 * references.
153 */
154 static void
155 zfsctl_snapshot_free(zfs_snapentry_t *se)
156 {
157 zfs_refcount_destroy(&se->se_refcount);
158 strfree(se->se_name);
159 strfree(se->se_path);
160
161 kmem_free(se, sizeof (zfs_snapentry_t));
162 }
163
164 /*
165 * Hold a reference on the zfs_snapentry_t.
166 */
167 static void
168 zfsctl_snapshot_hold(zfs_snapentry_t *se)
169 {
170 zfs_refcount_add(&se->se_refcount, NULL);
171 }
172
173 /*
174 * Release a reference on the zfs_snapentry_t. When the number of
175 * references drops to zero the structure will be freed.
176 */
177 static void
178 zfsctl_snapshot_rele(zfs_snapentry_t *se)
179 {
180 if (zfs_refcount_remove(&se->se_refcount, NULL) == 0)
181 zfsctl_snapshot_free(se);
182 }
183
184 /*
185 * Add a zfs_snapentry_t to both the zfs_snapshots_by_name and
186 * zfs_snapshots_by_objsetid trees. While the zfs_snapentry_t is part
187 * of the trees a reference is held.
188 */
189 static void
190 zfsctl_snapshot_add(zfs_snapentry_t *se)
191 {
192 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock));
193 zfs_refcount_add(&se->se_refcount, NULL);
194 avl_add(&zfs_snapshots_by_name, se);
195 avl_add(&zfs_snapshots_by_objsetid, se);
196 }
197
198 /*
199 * Remove a zfs_snapentry_t from both the zfs_snapshots_by_name and
200 * zfs_snapshots_by_objsetid trees. Upon removal a reference is dropped,
201 * this can result in the structure being freed if that was the last
202 * remaining reference.
203 */
204 static void
205 zfsctl_snapshot_remove(zfs_snapentry_t *se)
206 {
207 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock));
208 avl_remove(&zfs_snapshots_by_name, se);
209 avl_remove(&zfs_snapshots_by_objsetid, se);
210 zfsctl_snapshot_rele(se);
211 }
212
213 /*
214 * Snapshot name comparison function for the zfs_snapshots_by_name.
215 */
216 static int
217 snapentry_compare_by_name(const void *a, const void *b)
218 {
219 const zfs_snapentry_t *se_a = a;
220 const zfs_snapentry_t *se_b = b;
221 int ret;
222
223 ret = strcmp(se_a->se_name, se_b->se_name);
224
225 if (ret < 0)
226 return (-1);
227 else if (ret > 0)
228 return (1);
229 else
230 return (0);
231 }
232
233 /*
234 * Snapshot name comparison function for the zfs_snapshots_by_objsetid.
235 */
236 static int
237 snapentry_compare_by_objsetid(const void *a, const void *b)
238 {
239 const zfs_snapentry_t *se_a = a;
240 const zfs_snapentry_t *se_b = b;
241
242 if (se_a->se_spa != se_b->se_spa)
243 return ((ulong_t)se_a->se_spa < (ulong_t)se_b->se_spa ? -1 : 1);
244
245 if (se_a->se_objsetid < se_b->se_objsetid)
246 return (-1);
247 else if (se_a->se_objsetid > se_b->se_objsetid)
248 return (1);
249 else
250 return (0);
251 }
252
253 /*
254 * Find a zfs_snapentry_t in zfs_snapshots_by_name. If the snapname
255 * is found a pointer to the zfs_snapentry_t is returned and a reference
256 * taken on the structure. The caller is responsible for dropping the
257 * reference with zfsctl_snapshot_rele(). If the snapname is not found
258 * NULL will be returned.
259 */
260 static zfs_snapentry_t *
261 zfsctl_snapshot_find_by_name(char *snapname)
262 {
263 zfs_snapentry_t *se, search;
264
265 ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock));
266
267 search.se_name = snapname;
268 se = avl_find(&zfs_snapshots_by_name, &search, NULL);
269 if (se)
270 zfs_refcount_add(&se->se_refcount, NULL);
271
272 return (se);
273 }
274
275 /*
276 * Find a zfs_snapentry_t in zfs_snapshots_by_objsetid given the objset id
277 * rather than the snapname. In all other respects it behaves the same
278 * as zfsctl_snapshot_find_by_name().
279 */
280 static zfs_snapentry_t *
281 zfsctl_snapshot_find_by_objsetid(spa_t *spa, uint64_t objsetid)
282 {
283 zfs_snapentry_t *se, search;
284
285 ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock));
286
287 search.se_spa = spa;
288 search.se_objsetid = objsetid;
289 se = avl_find(&zfs_snapshots_by_objsetid, &search, NULL);
290 if (se)
291 zfs_refcount_add(&se->se_refcount, NULL);
292
293 return (se);
294 }
295
296 /*
297 * Rename a zfs_snapentry_t in the zfs_snapshots_by_name. The structure is
298 * removed, renamed, and added back to the new correct location in the tree.
299 */
300 static int
301 zfsctl_snapshot_rename(char *old_snapname, char *new_snapname)
302 {
303 zfs_snapentry_t *se;
304
305 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock));
306
307 se = zfsctl_snapshot_find_by_name(old_snapname);
308 if (se == NULL)
309 return (SET_ERROR(ENOENT));
310
311 zfsctl_snapshot_remove(se);
312 strfree(se->se_name);
313 se->se_name = strdup(new_snapname);
314 zfsctl_snapshot_add(se);
315 zfsctl_snapshot_rele(se);
316
317 return (0);
318 }
319
320 /*
321 * Delayed task responsible for unmounting an expired automounted snapshot.
322 */
323 static void
324 snapentry_expire(void *data)
325 {
326 zfs_snapentry_t *se = (zfs_snapentry_t *)data;
327 spa_t *spa = se->se_spa;
328 uint64_t objsetid = se->se_objsetid;
329
330 if (zfs_expire_snapshot <= 0) {
331 zfsctl_snapshot_rele(se);
332 return;
333 }
334
335 se->se_taskqid = TASKQID_INVALID;
336 (void) zfsctl_snapshot_unmount(se->se_name, MNT_EXPIRE);
337 zfsctl_snapshot_rele(se);
338
339 /*
340 * Reschedule the unmount if the zfs_snapentry_t wasn't removed.
341 * This can occur when the snapshot is busy.
342 */
343 rw_enter(&zfs_snapshot_lock, RW_READER);
344 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) {
345 zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot);
346 zfsctl_snapshot_rele(se);
347 }
348 rw_exit(&zfs_snapshot_lock);
349 }
350
351 /*
352 * Cancel an automatic unmount of a snapname. This callback is responsible
353 * for dropping the reference on the zfs_snapentry_t which was taken when
354 * during dispatch.
355 */
356 static void
357 zfsctl_snapshot_unmount_cancel(zfs_snapentry_t *se)
358 {
359 if (taskq_cancel_id(system_delay_taskq, se->se_taskqid) == 0) {
360 se->se_taskqid = TASKQID_INVALID;
361 zfsctl_snapshot_rele(se);
362 }
363 }
364
365 /*
366 * Dispatch the unmount task for delayed handling with a hold protecting it.
367 */
368 static void
369 zfsctl_snapshot_unmount_delay_impl(zfs_snapentry_t *se, int delay)
370 {
371 ASSERT3S(se->se_taskqid, ==, TASKQID_INVALID);
372
373 if (delay <= 0)
374 return;
375
376 zfsctl_snapshot_hold(se);
377 se->se_taskqid = taskq_dispatch_delay(system_delay_taskq,
378 snapentry_expire, se, TQ_SLEEP, ddi_get_lbolt() + delay * HZ);
379 }
380
381 /*
382 * Schedule an automatic unmount of objset id to occur in delay seconds from
383 * now. Any previous delayed unmount will be cancelled in favor of the
384 * updated deadline. A reference is taken by zfsctl_snapshot_find_by_name()
385 * and held until the outstanding task is handled or cancelled.
386 */
387 int
388 zfsctl_snapshot_unmount_delay(spa_t *spa, uint64_t objsetid, int delay)
389 {
390 zfs_snapentry_t *se;
391 int error = ENOENT;
392
393 rw_enter(&zfs_snapshot_lock, RW_READER);
394 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) {
395 zfsctl_snapshot_unmount_cancel(se);
396 zfsctl_snapshot_unmount_delay_impl(se, delay);
397 zfsctl_snapshot_rele(se);
398 error = 0;
399 }
400 rw_exit(&zfs_snapshot_lock);
401
402 return (error);
403 }
404
405 /*
406 * Check if snapname is currently mounted. Returned non-zero when mounted
407 * and zero when unmounted.
408 */
409 static boolean_t
410 zfsctl_snapshot_ismounted(char *snapname)
411 {
412 zfs_snapentry_t *se;
413 boolean_t ismounted = B_FALSE;
414
415 rw_enter(&zfs_snapshot_lock, RW_READER);
416 if ((se = zfsctl_snapshot_find_by_name(snapname)) != NULL) {
417 zfsctl_snapshot_rele(se);
418 ismounted = B_TRUE;
419 }
420 rw_exit(&zfs_snapshot_lock);
421
422 return (ismounted);
423 }
424
425 /*
426 * Check if the given inode is a part of the virtual .zfs directory.
427 */
428 boolean_t
429 zfsctl_is_node(struct inode *ip)
430 {
431 return (ITOZ(ip)->z_is_ctldir);
432 }
433
434 /*
435 * Check if the given inode is a .zfs/snapshots/snapname directory.
436 */
437 boolean_t
438 zfsctl_is_snapdir(struct inode *ip)
439 {
440 return (zfsctl_is_node(ip) && (ip->i_ino <= ZFSCTL_INO_SNAPDIRS));
441 }
442
443 /*
444 * Allocate a new inode with the passed id and ops.
445 */
446 static struct inode *
447 zfsctl_inode_alloc(zfsvfs_t *zfsvfs, uint64_t id,
448 const struct file_operations *fops, const struct inode_operations *ops)
449 {
450 inode_timespec_t now;
451 struct inode *ip;
452 znode_t *zp;
453
454 ip = new_inode(zfsvfs->z_sb);
455 if (ip == NULL)
456 return (NULL);
457
458 now = current_time(ip);
459 zp = ITOZ(ip);
460 ASSERT3P(zp->z_dirlocks, ==, NULL);
461 ASSERT3P(zp->z_acl_cached, ==, NULL);
462 ASSERT3P(zp->z_xattr_cached, ==, NULL);
463 zp->z_id = id;
464 zp->z_unlinked = 0;
465 zp->z_atime_dirty = 0;
466 zp->z_zn_prefetch = 0;
467 zp->z_moved = 0;
468 zp->z_sa_hdl = NULL;
469 zp->z_blksz = 0;
470 zp->z_seq = 0;
471 zp->z_mapcnt = 0;
472 zp->z_size = 0;
473 zp->z_pflags = 0;
474 zp->z_mode = 0;
475 zp->z_sync_cnt = 0;
476 zp->z_is_mapped = B_FALSE;
477 zp->z_is_ctldir = B_TRUE;
478 zp->z_is_sa = B_FALSE;
479 zp->z_is_stale = B_FALSE;
480 ip->i_generation = 0;
481 ip->i_ino = id;
482 ip->i_mode = (S_IFDIR | S_IRWXUGO);
483 ip->i_uid = SUID_TO_KUID(0);
484 ip->i_gid = SGID_TO_KGID(0);
485 ip->i_blkbits = SPA_MINBLOCKSHIFT;
486 ip->i_atime = now;
487 ip->i_mtime = now;
488 ip->i_ctime = now;
489 ip->i_fop = fops;
490 ip->i_op = ops;
491 #if defined(IOP_XATTR)
492 ip->i_opflags &= ~IOP_XATTR;
493 #endif
494
495 if (insert_inode_locked(ip)) {
496 unlock_new_inode(ip);
497 iput(ip);
498 return (NULL);
499 }
500
501 mutex_enter(&zfsvfs->z_znodes_lock);
502 list_insert_tail(&zfsvfs->z_all_znodes, zp);
503 zfsvfs->z_nr_znodes++;
504 membar_producer();
505 mutex_exit(&zfsvfs->z_znodes_lock);
506
507 unlock_new_inode(ip);
508
509 return (ip);
510 }
511
512 /*
513 * Lookup the inode with given id, it will be allocated if needed.
514 */
515 static struct inode *
516 zfsctl_inode_lookup(zfsvfs_t *zfsvfs, uint64_t id,
517 const struct file_operations *fops, const struct inode_operations *ops)
518 {
519 struct inode *ip = NULL;
520
521 while (ip == NULL) {
522 ip = ilookup(zfsvfs->z_sb, (unsigned long)id);
523 if (ip)
524 break;
525
526 /* May fail due to concurrent zfsctl_inode_alloc() */
527 ip = zfsctl_inode_alloc(zfsvfs, id, fops, ops);
528 }
529
530 return (ip);
531 }
532
533 /*
534 * Create the '.zfs' directory. This directory is cached as part of the VFS
535 * structure. This results in a hold on the zfsvfs_t. The code in zfs_umount()
536 * therefore checks against a vfs_count of 2 instead of 1. This reference
537 * is removed when the ctldir is destroyed in the unmount. All other entities
538 * under the '.zfs' directory are created dynamically as needed.
539 *
540 * Because the dynamically created '.zfs' directory entries assume the use
541 * of 64-bit inode numbers this support must be disabled on 32-bit systems.
542 */
543 int
544 zfsctl_create(zfsvfs_t *zfsvfs)
545 {
546 ASSERT(zfsvfs->z_ctldir == NULL);
547
548 zfsvfs->z_ctldir = zfsctl_inode_alloc(zfsvfs, ZFSCTL_INO_ROOT,
549 &zpl_fops_root, &zpl_ops_root);
550 if (zfsvfs->z_ctldir == NULL)
551 return (SET_ERROR(ENOENT));
552
553 return (0);
554 }
555
556 /*
557 * Destroy the '.zfs' directory or remove a snapshot from zfs_snapshots_by_name.
558 * Only called when the filesystem is unmounted.
559 */
560 void
561 zfsctl_destroy(zfsvfs_t *zfsvfs)
562 {
563 if (zfsvfs->z_issnap) {
564 zfs_snapentry_t *se;
565 spa_t *spa = zfsvfs->z_os->os_spa;
566 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
567
568 rw_enter(&zfs_snapshot_lock, RW_WRITER);
569 se = zfsctl_snapshot_find_by_objsetid(spa, objsetid);
570 if (se != NULL)
571 zfsctl_snapshot_remove(se);
572 rw_exit(&zfs_snapshot_lock);
573 if (se != NULL) {
574 zfsctl_snapshot_unmount_cancel(se);
575 zfsctl_snapshot_rele(se);
576 }
577 } else if (zfsvfs->z_ctldir) {
578 iput(zfsvfs->z_ctldir);
579 zfsvfs->z_ctldir = NULL;
580 }
581 }
582
583 /*
584 * Given a root znode, retrieve the associated .zfs directory.
585 * Add a hold to the vnode and return it.
586 */
587 struct inode *
588 zfsctl_root(znode_t *zp)
589 {
590 ASSERT(zfs_has_ctldir(zp));
591 igrab(ZTOZSB(zp)->z_ctldir);
592 return (ZTOZSB(zp)->z_ctldir);
593 }
594
595 /*
596 * Generate a long fid to indicate a snapdir. We encode whether snapdir is
597 * already monunted in gen field. We do this because nfsd lookup will not
598 * trigger automount. Next time the nfsd does fh_to_dentry, we will notice
599 * this and do automount and return ESTALE to force nfsd revalidate and follow
600 * mount.
601 */
602 static int
603 zfsctl_snapdir_fid(struct inode *ip, fid_t *fidp)
604 {
605 zfid_short_t *zfid = (zfid_short_t *)fidp;
606 zfid_long_t *zlfid = (zfid_long_t *)fidp;
607 uint32_t gen = 0;
608 uint64_t object;
609 uint64_t objsetid;
610 int i;
611 struct dentry *dentry;
612
613 if (fidp->fid_len < LONG_FID_LEN) {
614 fidp->fid_len = LONG_FID_LEN;
615 return (SET_ERROR(ENOSPC));
616 }
617
618 object = ip->i_ino;
619 objsetid = ZFSCTL_INO_SNAPDIRS - ip->i_ino;
620 zfid->zf_len = LONG_FID_LEN;
621
622 dentry = d_obtain_alias(igrab(ip));
623 if (!IS_ERR(dentry)) {
624 gen = !!d_mountpoint(dentry);
625 dput(dentry);
626 }
627
628 for (i = 0; i < sizeof (zfid->zf_object); i++)
629 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
630
631 for (i = 0; i < sizeof (zfid->zf_gen); i++)
632 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
633
634 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
635 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
636
637 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
638 zlfid->zf_setgen[i] = 0;
639
640 return (0);
641 }
642
643 /*
644 * Generate an appropriate fid for an entry in the .zfs directory.
645 */
646 int
647 zfsctl_fid(struct inode *ip, fid_t *fidp)
648 {
649 znode_t *zp = ITOZ(ip);
650 zfsvfs_t *zfsvfs = ITOZSB(ip);
651 uint64_t object = zp->z_id;
652 zfid_short_t *zfid;
653 int i;
654
655 ZFS_ENTER(zfsvfs);
656
657 if (zfsctl_is_snapdir(ip)) {
658 ZFS_EXIT(zfsvfs);
659 return (zfsctl_snapdir_fid(ip, fidp));
660 }
661
662 if (fidp->fid_len < SHORT_FID_LEN) {
663 fidp->fid_len = SHORT_FID_LEN;
664 ZFS_EXIT(zfsvfs);
665 return (SET_ERROR(ENOSPC));
666 }
667
668 zfid = (zfid_short_t *)fidp;
669
670 zfid->zf_len = SHORT_FID_LEN;
671
672 for (i = 0; i < sizeof (zfid->zf_object); i++)
673 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
674
675 /* .zfs znodes always have a generation number of 0 */
676 for (i = 0; i < sizeof (zfid->zf_gen); i++)
677 zfid->zf_gen[i] = 0;
678
679 ZFS_EXIT(zfsvfs);
680 return (0);
681 }
682
683 /*
684 * Construct a full dataset name in full_name: "pool/dataset@snap_name"
685 */
686 static int
687 zfsctl_snapshot_name(zfsvfs_t *zfsvfs, const char *snap_name, int len,
688 char *full_name)
689 {
690 objset_t *os = zfsvfs->z_os;
691
692 if (zfs_component_namecheck(snap_name, NULL, NULL) != 0)
693 return (SET_ERROR(EILSEQ));
694
695 dmu_objset_name(os, full_name);
696 if ((strlen(full_name) + 1 + strlen(snap_name)) >= len)
697 return (SET_ERROR(ENAMETOOLONG));
698
699 (void) strcat(full_name, "@");
700 (void) strcat(full_name, snap_name);
701
702 return (0);
703 }
704
705 /*
706 * Returns full path in full_path: "/pool/dataset/.zfs/snapshot/snap_name/"
707 */
708 static int
709 zfsctl_snapshot_path(struct path *path, int len, char *full_path)
710 {
711 char *path_buffer, *path_ptr;
712 int path_len, error = 0;
713
714 path_buffer = kmem_alloc(len, KM_SLEEP);
715
716 path_ptr = d_path(path, path_buffer, len);
717 if (IS_ERR(path_ptr)) {
718 error = -PTR_ERR(path_ptr);
719 goto out;
720 }
721
722 path_len = path_buffer + len - 1 - path_ptr;
723 if (path_len > len) {
724 error = SET_ERROR(EFAULT);
725 goto out;
726 }
727
728 memcpy(full_path, path_ptr, path_len);
729 full_path[path_len] = '\0';
730 out:
731 kmem_free(path_buffer, len);
732
733 return (error);
734 }
735
736 /*
737 * Returns full path in full_path: "/pool/dataset/.zfs/snapshot/snap_name/"
738 */
739 static int
740 zfsctl_snapshot_path_objset(zfsvfs_t *zfsvfs, uint64_t objsetid,
741 int path_len, char *full_path)
742 {
743 objset_t *os = zfsvfs->z_os;
744 fstrans_cookie_t cookie;
745 char *snapname;
746 boolean_t case_conflict;
747 uint64_t id, pos = 0;
748 int error = 0;
749
750 if (zfsvfs->z_vfs->vfs_mntpoint == NULL)
751 return (SET_ERROR(ENOENT));
752
753 cookie = spl_fstrans_mark();
754 snapname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
755
756 while (error == 0) {
757 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
758 error = dmu_snapshot_list_next(zfsvfs->z_os,
759 ZFS_MAX_DATASET_NAME_LEN, snapname, &id, &pos,
760 &case_conflict);
761 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
762 if (error)
763 goto out;
764
765 if (id == objsetid)
766 break;
767 }
768
769 memset(full_path, 0, path_len);
770 snprintf(full_path, path_len - 1, "%s/.zfs/snapshot/%s",
771 zfsvfs->z_vfs->vfs_mntpoint, snapname);
772 out:
773 kmem_free(snapname, ZFS_MAX_DATASET_NAME_LEN);
774 spl_fstrans_unmark(cookie);
775
776 return (error);
777 }
778
779 /*
780 * Special case the handling of "..".
781 */
782 int
783 zfsctl_root_lookup(struct inode *dip, char *name, struct inode **ipp,
784 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
785 {
786 zfsvfs_t *zfsvfs = ITOZSB(dip);
787 int error = 0;
788
789 ZFS_ENTER(zfsvfs);
790
791 if (strcmp(name, "..") == 0) {
792 *ipp = dip->i_sb->s_root->d_inode;
793 } else if (strcmp(name, ZFS_SNAPDIR_NAME) == 0) {
794 *ipp = zfsctl_inode_lookup(zfsvfs, ZFSCTL_INO_SNAPDIR,
795 &zpl_fops_snapdir, &zpl_ops_snapdir);
796 } else if (strcmp(name, ZFS_SHAREDIR_NAME) == 0) {
797 *ipp = zfsctl_inode_lookup(zfsvfs, ZFSCTL_INO_SHARES,
798 &zpl_fops_shares, &zpl_ops_shares);
799 } else {
800 *ipp = NULL;
801 }
802
803 if (*ipp == NULL)
804 error = SET_ERROR(ENOENT);
805
806 ZFS_EXIT(zfsvfs);
807
808 return (error);
809 }
810
811 /*
812 * Lookup entry point for the 'snapshot' directory. Try to open the
813 * snapshot if it exist, creating the pseudo filesystem inode as necessary.
814 * Perform a mount of the associated dataset on top of the inode.
815 */
816 int
817 zfsctl_snapdir_lookup(struct inode *dip, char *name, struct inode **ipp,
818 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
819 {
820 zfsvfs_t *zfsvfs = ITOZSB(dip);
821 uint64_t id;
822 int error;
823
824 ZFS_ENTER(zfsvfs);
825
826 error = dmu_snapshot_lookup(zfsvfs->z_os, name, &id);
827 if (error) {
828 ZFS_EXIT(zfsvfs);
829 return (error);
830 }
831
832 *ipp = zfsctl_inode_lookup(zfsvfs, ZFSCTL_INO_SNAPDIRS - id,
833 &simple_dir_operations, &simple_dir_inode_operations);
834 if (*ipp == NULL)
835 error = SET_ERROR(ENOENT);
836
837 ZFS_EXIT(zfsvfs);
838
839 return (error);
840 }
841
842 /*
843 * Renaming a directory under '.zfs/snapshot' will automatically trigger
844 * a rename of the snapshot to the new given name. The rename is confined
845 * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere.
846 */
847 int
848 zfsctl_snapdir_rename(struct inode *sdip, char *snm,
849 struct inode *tdip, char *tnm, cred_t *cr, int flags)
850 {
851 zfsvfs_t *zfsvfs = ITOZSB(sdip);
852 char *to, *from, *real, *fsname;
853 int error;
854
855 if (!zfs_admin_snapshot)
856 return (SET_ERROR(EACCES));
857
858 ZFS_ENTER(zfsvfs);
859
860 to = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
861 from = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
862 real = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
863 fsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
864
865 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
866 error = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
867 ZFS_MAX_DATASET_NAME_LEN, NULL);
868 if (error == 0) {
869 snm = real;
870 } else if (error != ENOTSUP) {
871 goto out;
872 }
873 }
874
875 dmu_objset_name(zfsvfs->z_os, fsname);
876
877 error = zfsctl_snapshot_name(ITOZSB(sdip), snm,
878 ZFS_MAX_DATASET_NAME_LEN, from);
879 if (error == 0)
880 error = zfsctl_snapshot_name(ITOZSB(tdip), tnm,
881 ZFS_MAX_DATASET_NAME_LEN, to);
882 if (error == 0)
883 error = zfs_secpolicy_rename_perms(from, to, cr);
884 if (error != 0)
885 goto out;
886
887 /*
888 * Cannot move snapshots out of the snapdir.
889 */
890 if (sdip != tdip) {
891 error = SET_ERROR(EINVAL);
892 goto out;
893 }
894
895 /*
896 * No-op when names are identical.
897 */
898 if (strcmp(snm, tnm) == 0) {
899 error = 0;
900 goto out;
901 }
902
903 rw_enter(&zfs_snapshot_lock, RW_WRITER);
904
905 error = dsl_dataset_rename_snapshot(fsname, snm, tnm, B_FALSE);
906 if (error == 0)
907 (void) zfsctl_snapshot_rename(snm, tnm);
908
909 rw_exit(&zfs_snapshot_lock);
910 out:
911 kmem_free(from, ZFS_MAX_DATASET_NAME_LEN);
912 kmem_free(to, ZFS_MAX_DATASET_NAME_LEN);
913 kmem_free(real, ZFS_MAX_DATASET_NAME_LEN);
914 kmem_free(fsname, ZFS_MAX_DATASET_NAME_LEN);
915
916 ZFS_EXIT(zfsvfs);
917
918 return (error);
919 }
920
921 /*
922 * Removing a directory under '.zfs/snapshot' will automatically trigger
923 * the removal of the snapshot with the given name.
924 */
925 int
926 zfsctl_snapdir_remove(struct inode *dip, char *name, cred_t *cr, int flags)
927 {
928 zfsvfs_t *zfsvfs = ITOZSB(dip);
929 char *snapname, *real;
930 int error;
931
932 if (!zfs_admin_snapshot)
933 return (SET_ERROR(EACCES));
934
935 ZFS_ENTER(zfsvfs);
936
937 snapname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
938 real = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
939
940 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
941 error = dmu_snapshot_realname(zfsvfs->z_os, name, real,
942 ZFS_MAX_DATASET_NAME_LEN, NULL);
943 if (error == 0) {
944 name = real;
945 } else if (error != ENOTSUP) {
946 goto out;
947 }
948 }
949
950 error = zfsctl_snapshot_name(ITOZSB(dip), name,
951 ZFS_MAX_DATASET_NAME_LEN, snapname);
952 if (error == 0)
953 error = zfs_secpolicy_destroy_perms(snapname, cr);
954 if (error != 0)
955 goto out;
956
957 error = zfsctl_snapshot_unmount(snapname, MNT_FORCE);
958 if ((error == 0) || (error == ENOENT))
959 error = dsl_destroy_snapshot(snapname, B_FALSE);
960 out:
961 kmem_free(snapname, ZFS_MAX_DATASET_NAME_LEN);
962 kmem_free(real, ZFS_MAX_DATASET_NAME_LEN);
963
964 ZFS_EXIT(zfsvfs);
965
966 return (error);
967 }
968
969 /*
970 * Creating a directory under '.zfs/snapshot' will automatically trigger
971 * the creation of a new snapshot with the given name.
972 */
973 int
974 zfsctl_snapdir_mkdir(struct inode *dip, char *dirname, vattr_t *vap,
975 struct inode **ipp, cred_t *cr, int flags)
976 {
977 zfsvfs_t *zfsvfs = ITOZSB(dip);
978 char *dsname;
979 int error;
980
981 if (!zfs_admin_snapshot)
982 return (SET_ERROR(EACCES));
983
984 dsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
985
986 if (zfs_component_namecheck(dirname, NULL, NULL) != 0) {
987 error = SET_ERROR(EILSEQ);
988 goto out;
989 }
990
991 dmu_objset_name(zfsvfs->z_os, dsname);
992
993 error = zfs_secpolicy_snapshot_perms(dsname, cr);
994 if (error != 0)
995 goto out;
996
997 if (error == 0) {
998 error = dmu_objset_snapshot_one(dsname, dirname);
999 if (error != 0)
1000 goto out;
1001
1002 error = zfsctl_snapdir_lookup(dip, dirname, ipp,
1003 0, cr, NULL, NULL);
1004 }
1005 out:
1006 kmem_free(dsname, ZFS_MAX_DATASET_NAME_LEN);
1007
1008 return (error);
1009 }
1010
1011 /*
1012 * Attempt to unmount a snapshot by making a call to user space.
1013 * There is no assurance that this can or will succeed, is just a
1014 * best effort. In the case where it does fail, perhaps because
1015 * it's in use, the unmount will fail harmlessly.
1016 */
1017 int
1018 zfsctl_snapshot_unmount(char *snapname, int flags)
1019 {
1020 char *argv[] = { "/usr/bin/env", "umount", "-t", "zfs", "-n", NULL,
1021 NULL };
1022 char *envp[] = { NULL };
1023 zfs_snapentry_t *se;
1024 int error;
1025
1026 rw_enter(&zfs_snapshot_lock, RW_READER);
1027 if ((se = zfsctl_snapshot_find_by_name(snapname)) == NULL) {
1028 rw_exit(&zfs_snapshot_lock);
1029 return (SET_ERROR(ENOENT));
1030 }
1031 rw_exit(&zfs_snapshot_lock);
1032
1033 if (flags & MNT_FORCE)
1034 argv[4] = "-fn";
1035 argv[5] = se->se_path;
1036 dprintf("unmount; path=%s\n", se->se_path);
1037 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1038 zfsctl_snapshot_rele(se);
1039
1040
1041 /*
1042 * The umount system utility will return 256 on error. We must
1043 * assume this error is because the file system is busy so it is
1044 * converted to the more sensible EBUSY.
1045 */
1046 if (error)
1047 error = SET_ERROR(EBUSY);
1048
1049 return (error);
1050 }
1051
1052 #define MOUNT_BUSY 0x80 /* Mount failed due to EBUSY (from mntent.h) */
1053
1054 int
1055 zfsctl_snapshot_mount(struct path *path, int flags)
1056 {
1057 struct dentry *dentry = path->dentry;
1058 struct inode *ip = dentry->d_inode;
1059 zfsvfs_t *zfsvfs;
1060 zfsvfs_t *snap_zfsvfs;
1061 zfs_snapentry_t *se;
1062 char *full_name, *full_path;
1063 char *argv[] = { "/usr/bin/env", "mount", "-t", "zfs", "-n", NULL, NULL,
1064 NULL };
1065 char *envp[] = { NULL };
1066 int error;
1067 struct path spath;
1068
1069 if (ip == NULL)
1070 return (SET_ERROR(EISDIR));
1071
1072 zfsvfs = ITOZSB(ip);
1073 ZFS_ENTER(zfsvfs);
1074
1075 full_name = kmem_zalloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
1076 full_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1077
1078 error = zfsctl_snapshot_name(zfsvfs, dname(dentry),
1079 ZFS_MAX_DATASET_NAME_LEN, full_name);
1080 if (error)
1081 goto error;
1082
1083 error = zfsctl_snapshot_path(path, MAXPATHLEN, full_path);
1084 if (error)
1085 goto error;
1086
1087 /*
1088 * Multiple concurrent automounts of a snapshot are never allowed.
1089 * The snapshot may be manually mounted as many times as desired.
1090 */
1091 if (zfsctl_snapshot_ismounted(full_name)) {
1092 error = 0;
1093 goto error;
1094 }
1095
1096 /*
1097 * Attempt to mount the snapshot from user space. Normally this
1098 * would be done using the vfs_kern_mount() function, however that
1099 * function is marked GPL-only and cannot be used. On error we
1100 * careful to log the real error to the console and return EISDIR
1101 * to safely abort the automount. This should be very rare.
1102 *
1103 * If the user mode helper happens to return EBUSY, a concurrent
1104 * mount is already in progress in which case the error is ignored.
1105 * Take note that if the program was executed successfully the return
1106 * value from call_usermodehelper() will be (exitcode << 8 + signal).
1107 */
1108 dprintf("mount; name=%s path=%s\n", full_name, full_path);
1109 argv[5] = full_name;
1110 argv[6] = full_path;
1111 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1112 if (error) {
1113 if (!(error & MOUNT_BUSY << 8)) {
1114 cmn_err(CE_WARN, "Unable to automount %s/%s: %d",
1115 full_path, full_name, error);
1116 error = SET_ERROR(EISDIR);
1117 } else {
1118 /*
1119 * EBUSY, this could mean a concurrent mount, or the
1120 * snapshot has already been mounted at completely
1121 * different place. We return 0 so VFS will retry. For
1122 * the latter case the VFS will retry several times
1123 * and return ELOOP, which is probably not a very good
1124 * behavior.
1125 */
1126 error = 0;
1127 }
1128 goto error;
1129 }
1130
1131 /*
1132 * Follow down in to the mounted snapshot and set MNT_SHRINKABLE
1133 * to identify this as an automounted filesystem.
1134 */
1135 spath = *path;
1136 path_get(&spath);
1137 if (zpl_follow_down_one(&spath)) {
1138 snap_zfsvfs = ITOZSB(spath.dentry->d_inode);
1139 snap_zfsvfs->z_parent = zfsvfs;
1140 dentry = spath.dentry;
1141 spath.mnt->mnt_flags |= MNT_SHRINKABLE;
1142
1143 rw_enter(&zfs_snapshot_lock, RW_WRITER);
1144 se = zfsctl_snapshot_alloc(full_name, full_path,
1145 snap_zfsvfs->z_os->os_spa, dmu_objset_id(snap_zfsvfs->z_os),
1146 dentry);
1147 zfsctl_snapshot_add(se);
1148 zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot);
1149 rw_exit(&zfs_snapshot_lock);
1150 }
1151 path_put(&spath);
1152 error:
1153 kmem_free(full_name, ZFS_MAX_DATASET_NAME_LEN);
1154 kmem_free(full_path, MAXPATHLEN);
1155
1156 ZFS_EXIT(zfsvfs);
1157
1158 return (error);
1159 }
1160
1161 /*
1162 * Get the snapdir inode from fid
1163 */
1164 int
1165 zfsctl_snapdir_vget(struct super_block *sb, uint64_t objsetid, int gen,
1166 struct inode **ipp)
1167 {
1168 int error;
1169 struct path path;
1170 char *mnt;
1171 struct dentry *dentry;
1172
1173 mnt = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1174
1175 error = zfsctl_snapshot_path_objset(sb->s_fs_info, objsetid,
1176 MAXPATHLEN, mnt);
1177 if (error)
1178 goto out;
1179
1180 /* Trigger automount */
1181 error = -kern_path(mnt, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &path);
1182 if (error)
1183 goto out;
1184
1185 path_put(&path);
1186 /*
1187 * Get the snapdir inode. Note, we don't want to use the above
1188 * path because it contains the root of the snapshot rather
1189 * than the snapdir.
1190 */
1191 *ipp = ilookup(sb, ZFSCTL_INO_SNAPDIRS - objsetid);
1192 if (*ipp == NULL) {
1193 error = SET_ERROR(ENOENT);
1194 goto out;
1195 }
1196
1197 /* check gen, see zfsctl_snapdir_fid */
1198 dentry = d_obtain_alias(igrab(*ipp));
1199 if (gen != (!IS_ERR(dentry) && d_mountpoint(dentry))) {
1200 iput(*ipp);
1201 *ipp = NULL;
1202 error = SET_ERROR(ENOENT);
1203 }
1204 if (!IS_ERR(dentry))
1205 dput(dentry);
1206 out:
1207 kmem_free(mnt, MAXPATHLEN);
1208 return (error);
1209 }
1210
1211 int
1212 zfsctl_shares_lookup(struct inode *dip, char *name, struct inode **ipp,
1213 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
1214 {
1215 zfsvfs_t *zfsvfs = ITOZSB(dip);
1216 struct inode *ip;
1217 znode_t *dzp;
1218 int error;
1219
1220 ZFS_ENTER(zfsvfs);
1221
1222 if (zfsvfs->z_shares_dir == 0) {
1223 ZFS_EXIT(zfsvfs);
1224 return (SET_ERROR(ENOTSUP));
1225 }
1226
1227 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1228 error = zfs_lookup(ZTOI(dzp), name, &ip, 0, cr, NULL, NULL);
1229 iput(ZTOI(dzp));
1230 }
1231
1232 ZFS_EXIT(zfsvfs);
1233
1234 return (error);
1235 }
1236
1237 /*
1238 * Initialize the various pieces we'll need to create and manipulate .zfs
1239 * directories. Currently this is unused but available.
1240 */
1241 void
1242 zfsctl_init(void)
1243 {
1244 avl_create(&zfs_snapshots_by_name, snapentry_compare_by_name,
1245 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t,
1246 se_node_name));
1247 avl_create(&zfs_snapshots_by_objsetid, snapentry_compare_by_objsetid,
1248 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t,
1249 se_node_objsetid));
1250 rw_init(&zfs_snapshot_lock, NULL, RW_DEFAULT, NULL);
1251 }
1252
1253 /*
1254 * Cleanup the various pieces we needed for .zfs directories. In particular
1255 * ensure the expiry timer is canceled safely.
1256 */
1257 void
1258 zfsctl_fini(void)
1259 {
1260 avl_destroy(&zfs_snapshots_by_name);
1261 avl_destroy(&zfs_snapshots_by_objsetid);
1262 rw_destroy(&zfs_snapshot_lock);
1263 }
1264
1265 module_param(zfs_admin_snapshot, int, 0644);
1266 MODULE_PARM_DESC(zfs_admin_snapshot, "Enable mkdir/rmdir/mv in .zfs/snapshot");
1267
1268 module_param(zfs_expire_snapshot, int, 0644);
1269 MODULE_PARM_DESC(zfs_expire_snapshot, "Seconds to expire .zfs/snapshot");