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