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