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