xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_onexit.c (revision 7ac89354)
1c99e4bdcSChris Kirby /*
2c99e4bdcSChris Kirby  * CDDL HEADER START
3c99e4bdcSChris Kirby  *
4c99e4bdcSChris Kirby  * The contents of this file are subject to the terms of the
5c99e4bdcSChris Kirby  * Common Development and Distribution License (the "License").
6c99e4bdcSChris Kirby  * You may not use this file except in compliance with the License.
7c99e4bdcSChris Kirby  *
8c99e4bdcSChris Kirby  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c99e4bdcSChris Kirby  * or http://www.opensolaris.org/os/licensing.
10c99e4bdcSChris Kirby  * See the License for the specific language governing permissions
11c99e4bdcSChris Kirby  * and limitations under the License.
12c99e4bdcSChris Kirby  *
13c99e4bdcSChris Kirby  * When distributing Covered Code, include this CDDL HEADER in each
14c99e4bdcSChris Kirby  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c99e4bdcSChris Kirby  * If applicable, add the following below this CDDL HEADER, with the
16c99e4bdcSChris Kirby  * fields enclosed by brackets "[]" replaced with your own identifying
17c99e4bdcSChris Kirby  * information: Portions Copyright [yyyy] [name of copyright owner]
18c99e4bdcSChris Kirby  *
19c99e4bdcSChris Kirby  * CDDL HEADER END
20c99e4bdcSChris Kirby  */
21c99e4bdcSChris Kirby /*
22c99e4bdcSChris Kirby  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23be6fd75aSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
24c99e4bdcSChris Kirby  */
25c99e4bdcSChris Kirby 
26c99e4bdcSChris Kirby #include <sys/types.h>
27c99e4bdcSChris Kirby #include <sys/param.h>
28c99e4bdcSChris Kirby #include <sys/errno.h>
29c99e4bdcSChris Kirby #include <sys/open.h>
30c99e4bdcSChris Kirby #include <sys/kmem.h>
31c99e4bdcSChris Kirby #include <sys/conf.h>
32c99e4bdcSChris Kirby #include <sys/ddi.h>
33c99e4bdcSChris Kirby #include <sys/sunddi.h>
34c99e4bdcSChris Kirby #include <sys/zfs_ioctl.h>
35c99e4bdcSChris Kirby #include <sys/mkdev.h>
36c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
37c99e4bdcSChris Kirby #include <sys/zvol.h>
38c99e4bdcSChris Kirby 
39c99e4bdcSChris Kirby /*
40c99e4bdcSChris Kirby  * ZFS kernel routines may add/delete callback routines to be invoked
41c99e4bdcSChris Kirby  * upon process exit (triggered via the close operation from the /dev/zfs
42c99e4bdcSChris Kirby  * driver).
43c99e4bdcSChris Kirby  *
44c99e4bdcSChris Kirby  * These cleanup callbacks are intended to allow for the accumulation
45c99e4bdcSChris Kirby  * of kernel state across multiple ioctls.  User processes participate
46c99e4bdcSChris Kirby  * by opening ZFS_DEV with O_EXCL. This causes the ZFS driver to do a
47c99e4bdcSChris Kirby  * clone-open, generating a unique minor number. The process then passes
48c99e4bdcSChris Kirby  * along that file descriptor to each ioctl that might have a cleanup operation.
49c99e4bdcSChris Kirby  *
50a7f53a56SChris Kirby  * Consumers of the onexit routines should call zfs_onexit_fd_hold() early
51a7f53a56SChris Kirby  * on to validate the given fd and add a reference to its file table entry.
52a7f53a56SChris Kirby  * This allows the consumer to do its work and then add a callback, knowing
53a7f53a56SChris Kirby  * that zfs_onexit_add_cb() won't fail with EBADF.  When finished, consumers
54a7f53a56SChris Kirby  * should call zfs_onexit_fd_rele().
55a7f53a56SChris Kirby  *
56c99e4bdcSChris Kirby  * A simple example is zfs_ioc_recv(), where we might create an AVL tree
57c99e4bdcSChris Kirby  * with dataset/GUID mappings and then reuse that tree on subsequent
58c99e4bdcSChris Kirby  * zfs_ioc_recv() calls.
59c99e4bdcSChris Kirby  *
60c99e4bdcSChris Kirby  * On the first zfs_ioc_recv() call, dmu_recv_stream() will kmem_alloc()
61c99e4bdcSChris Kirby  * the AVL tree and pass it along with a callback function to
62c99e4bdcSChris Kirby  * zfs_onexit_add_cb(). The zfs_onexit_add_cb() routine will register the
63c99e4bdcSChris Kirby  * callback and return an action handle.
64c99e4bdcSChris Kirby  *
65c99e4bdcSChris Kirby  * The action handle is then passed from user space to subsequent
66c99e4bdcSChris Kirby  * zfs_ioc_recv() calls, so that dmu_recv_stream() can fetch its AVL tree
67a7f53a56SChris Kirby  * by calling zfs_onexit_cb_data() with the device minor number and
68a7f53a56SChris Kirby  * action handle.
69c99e4bdcSChris Kirby  *
70c99e4bdcSChris Kirby  * If the user process exits abnormally, the callback is invoked implicitly
71c99e4bdcSChris Kirby  * as part of the driver close operation.  Once the user space process is
72c99e4bdcSChris Kirby  * finished with the accumulated kernel state, it can also just call close(2)
73c99e4bdcSChris Kirby  * on the cleanup fd to trigger the cleanup callback.
74c99e4bdcSChris Kirby  */
75c99e4bdcSChris Kirby 
76c99e4bdcSChris Kirby void
zfs_onexit_init(zfs_onexit_t ** zop)77c99e4bdcSChris Kirby zfs_onexit_init(zfs_onexit_t **zop)
78c99e4bdcSChris Kirby {
79c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
80c99e4bdcSChris Kirby 
81c99e4bdcSChris Kirby 	zo = *zop = kmem_zalloc(sizeof (zfs_onexit_t), KM_SLEEP);
82c99e4bdcSChris Kirby 	mutex_init(&zo->zo_lock, NULL, MUTEX_DEFAULT, NULL);
83c99e4bdcSChris Kirby 	list_create(&zo->zo_actions, sizeof (zfs_onexit_action_node_t),
84c99e4bdcSChris Kirby 	    offsetof(zfs_onexit_action_node_t, za_link));
85c99e4bdcSChris Kirby }
86c99e4bdcSChris Kirby 
87c99e4bdcSChris Kirby void
zfs_onexit_destroy(zfs_onexit_t * zo)88c99e4bdcSChris Kirby zfs_onexit_destroy(zfs_onexit_t *zo)
89c99e4bdcSChris Kirby {
90c99e4bdcSChris Kirby 	zfs_onexit_action_node_t *ap;
91c99e4bdcSChris Kirby 
92c99e4bdcSChris Kirby 	mutex_enter(&zo->zo_lock);
93c99e4bdcSChris Kirby 	while ((ap = list_head(&zo->zo_actions)) != NULL) {
94c99e4bdcSChris Kirby 		list_remove(&zo->zo_actions, ap);
95c99e4bdcSChris Kirby 		mutex_exit(&zo->zo_lock);
96c99e4bdcSChris Kirby 		ap->za_func(ap->za_data);
97c99e4bdcSChris Kirby 		kmem_free(ap, sizeof (zfs_onexit_action_node_t));
98c99e4bdcSChris Kirby 		mutex_enter(&zo->zo_lock);
99c99e4bdcSChris Kirby 	}
100c99e4bdcSChris Kirby 	mutex_exit(&zo->zo_lock);
101c99e4bdcSChris Kirby 
102c99e4bdcSChris Kirby 	list_destroy(&zo->zo_actions);
103c99e4bdcSChris Kirby 	mutex_destroy(&zo->zo_lock);
104c99e4bdcSChris Kirby 	kmem_free(zo, sizeof (zfs_onexit_t));
105c99e4bdcSChris Kirby }
106c99e4bdcSChris Kirby 
107c99e4bdcSChris Kirby static int
zfs_onexit_minor_to_state(minor_t minor,zfs_onexit_t ** zo)108a7f53a56SChris Kirby zfs_onexit_minor_to_state(minor_t minor, zfs_onexit_t **zo)
109a7f53a56SChris Kirby {
110a7f53a56SChris Kirby 	*zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
111a7f53a56SChris Kirby 	if (*zo == NULL)
112be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
113a7f53a56SChris Kirby 
114a7f53a56SChris Kirby 	return (0);
115a7f53a56SChris Kirby }
116a7f53a56SChris Kirby 
117a7f53a56SChris Kirby /*
118a7f53a56SChris Kirby  * Consumers might need to operate by minor number instead of fd, since
119a7f53a56SChris Kirby  * they might be running in another thread (e.g. txg_sync_thread). Callers
120a7f53a56SChris Kirby  * of this function must call zfs_onexit_fd_rele() when they're finished
121a7f53a56SChris Kirby  * using the minor number.
122a7f53a56SChris Kirby  */
123a7f53a56SChris Kirby int
zfs_onexit_fd_hold(int fd,minor_t * minorp)124a7f53a56SChris Kirby zfs_onexit_fd_hold(int fd, minor_t *minorp)
125c99e4bdcSChris Kirby {
126c99e4bdcSChris Kirby 	file_t *fp;
127a7f53a56SChris Kirby 	zfs_onexit_t *zo;
128*7ac89354SDon Brady 	int ret;
129c99e4bdcSChris Kirby 
130c99e4bdcSChris Kirby 	fp = getf(fd);
131c99e4bdcSChris Kirby 	if (fp == NULL)
132be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
133c99e4bdcSChris Kirby 
134a7f53a56SChris Kirby 	*minorp = getminor(fp->f_vnode->v_rdev);
135*7ac89354SDon Brady 	ret = zfs_onexit_minor_to_state(*minorp, &zo);
136*7ac89354SDon Brady 	if (ret != 0)
137*7ac89354SDon Brady 		releasef(fd);
138*7ac89354SDon Brady 
139*7ac89354SDon Brady 	return (ret);
140a7f53a56SChris Kirby }
141c99e4bdcSChris Kirby 
142a7f53a56SChris Kirby void
zfs_onexit_fd_rele(int fd)143a7f53a56SChris Kirby zfs_onexit_fd_rele(int fd)
144a7f53a56SChris Kirby {
145a7f53a56SChris Kirby 	releasef(fd);
146c99e4bdcSChris Kirby }
147c99e4bdcSChris Kirby 
148c99e4bdcSChris Kirby /*
149c99e4bdcSChris Kirby  * Add a callback to be invoked when the calling process exits.
150c99e4bdcSChris Kirby  */
151c99e4bdcSChris Kirby int
zfs_onexit_add_cb(minor_t minor,void (* func)(void *),void * data,uint64_t * action_handle)152a7f53a56SChris Kirby zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
153c99e4bdcSChris Kirby     uint64_t *action_handle)
154c99e4bdcSChris Kirby {
155c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
156c99e4bdcSChris Kirby 	zfs_onexit_action_node_t *ap;
157c99e4bdcSChris Kirby 	int error;
158c99e4bdcSChris Kirby 
159a7f53a56SChris Kirby 	error = zfs_onexit_minor_to_state(minor, &zo);
160c99e4bdcSChris Kirby 	if (error)
161c99e4bdcSChris Kirby 		return (error);
162c99e4bdcSChris Kirby 
163c99e4bdcSChris Kirby 	ap = kmem_alloc(sizeof (zfs_onexit_action_node_t), KM_SLEEP);
164c99e4bdcSChris Kirby 	list_link_init(&ap->za_link);
165c99e4bdcSChris Kirby 	ap->za_func = func;
166c99e4bdcSChris Kirby 	ap->za_data = data;
167c99e4bdcSChris Kirby 
168c99e4bdcSChris Kirby 	mutex_enter(&zo->zo_lock);
169c99e4bdcSChris Kirby 	list_insert_tail(&zo->zo_actions, ap);
170c99e4bdcSChris Kirby 	mutex_exit(&zo->zo_lock);
171a7f53a56SChris Kirby 	if (action_handle)
172a7f53a56SChris Kirby 		*action_handle = (uint64_t)(uintptr_t)ap;
173c99e4bdcSChris Kirby 
174c99e4bdcSChris Kirby 	return (0);
175c99e4bdcSChris Kirby }
176c99e4bdcSChris Kirby 
177c99e4bdcSChris Kirby static zfs_onexit_action_node_t *
zfs_onexit_find_cb(zfs_onexit_t * zo,uint64_t action_handle)178c99e4bdcSChris Kirby zfs_onexit_find_cb(zfs_onexit_t *zo, uint64_t action_handle)
179c99e4bdcSChris Kirby {
180c99e4bdcSChris Kirby 	zfs_onexit_action_node_t *match;
181c99e4bdcSChris Kirby 	zfs_onexit_action_node_t *ap;
182c99e4bdcSChris Kirby 	list_t *l;
183c99e4bdcSChris Kirby 
184c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zo->zo_lock));
185c99e4bdcSChris Kirby 
186c99e4bdcSChris Kirby 	match = (zfs_onexit_action_node_t *)(uintptr_t)action_handle;
187c99e4bdcSChris Kirby 	l = &zo->zo_actions;
188c99e4bdcSChris Kirby 	for (ap = list_head(l); ap != NULL; ap = list_next(l, ap)) {
189c99e4bdcSChris Kirby 		if (match == ap)
190c99e4bdcSChris Kirby 			break;
191c99e4bdcSChris Kirby 	}
192c99e4bdcSChris Kirby 	return (ap);
193c99e4bdcSChris Kirby }
194c99e4bdcSChris Kirby 
195c99e4bdcSChris Kirby /*
196c99e4bdcSChris Kirby  * Delete the callback, triggering it first if 'fire' is set.
197c99e4bdcSChris Kirby  */
198c99e4bdcSChris Kirby int
zfs_onexit_del_cb(minor_t minor,uint64_t action_handle,boolean_t fire)199a7f53a56SChris Kirby zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
200c99e4bdcSChris Kirby {
201c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
202c99e4bdcSChris Kirby 	zfs_onexit_action_node_t *ap;
203c99e4bdcSChris Kirby 	int error;
204c99e4bdcSChris Kirby 
205a7f53a56SChris Kirby 	error = zfs_onexit_minor_to_state(minor, &zo);
206c99e4bdcSChris Kirby 	if (error)
207c99e4bdcSChris Kirby 		return (error);
208c99e4bdcSChris Kirby 
209c99e4bdcSChris Kirby 	mutex_enter(&zo->zo_lock);
210c99e4bdcSChris Kirby 	ap = zfs_onexit_find_cb(zo, action_handle);
211c99e4bdcSChris Kirby 	if (ap != NULL) {
212c99e4bdcSChris Kirby 		list_remove(&zo->zo_actions, ap);
213c99e4bdcSChris Kirby 		mutex_exit(&zo->zo_lock);
214c99e4bdcSChris Kirby 		if (fire)
215c99e4bdcSChris Kirby 			ap->za_func(ap->za_data);
216c99e4bdcSChris Kirby 		kmem_free(ap, sizeof (zfs_onexit_action_node_t));
217c99e4bdcSChris Kirby 	} else {
218c99e4bdcSChris Kirby 		mutex_exit(&zo->zo_lock);
219be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
220c99e4bdcSChris Kirby 	}
221c99e4bdcSChris Kirby 
222c99e4bdcSChris Kirby 	return (error);
223c99e4bdcSChris Kirby }
224c99e4bdcSChris Kirby 
225c99e4bdcSChris Kirby /*
226c99e4bdcSChris Kirby  * Return the data associated with this callback.  This allows consumers
227c99e4bdcSChris Kirby  * of the cleanup-on-exit interfaces to stash kernel data across system
228c99e4bdcSChris Kirby  * calls, knowing that it will be cleaned up if the calling process exits.
229c99e4bdcSChris Kirby  */
230c99e4bdcSChris Kirby int
zfs_onexit_cb_data(minor_t minor,uint64_t action_handle,void ** data)231a7f53a56SChris Kirby zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
232c99e4bdcSChris Kirby {
233c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
234c99e4bdcSChris Kirby 	zfs_onexit_action_node_t *ap;
235c99e4bdcSChris Kirby 	int error;
236c99e4bdcSChris Kirby 
237c99e4bdcSChris Kirby 	*data = NULL;
238c99e4bdcSChris Kirby 
239a7f53a56SChris Kirby 	error = zfs_onexit_minor_to_state(minor, &zo);
240c99e4bdcSChris Kirby 	if (error)
241c99e4bdcSChris Kirby 		return (error);
242c99e4bdcSChris Kirby 
243c99e4bdcSChris Kirby 	mutex_enter(&zo->zo_lock);
244c99e4bdcSChris Kirby 	ap = zfs_onexit_find_cb(zo, action_handle);
245c99e4bdcSChris Kirby 	if (ap != NULL)
246c99e4bdcSChris Kirby 		*data = ap->za_data;
247c99e4bdcSChris Kirby 	else
248be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
249c99e4bdcSChris Kirby 	mutex_exit(&zo->zo_lock);
250c99e4bdcSChris Kirby 
251c99e4bdcSChris Kirby 	return (error);
252c99e4bdcSChris Kirby }
253