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