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