c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * CDDL HEADER START
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * The contents of this file are subject to the terms of the
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * Common Development and Distribution License (the "License").
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * You may not use this file except in compliance with the License.
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * See the License for the specific language governing permissions
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * and limitations under the License.
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * When distributing Covered Code, include this CDDL HEADER in each
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * If applicable, add the following below this CDDL HEADER, with the
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * fields enclosed by brackets "[]" replaced with your own identifying
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * information: Portions Copyright [yyyy] [name of copyright owner]
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * CDDL HEADER END
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * ZFS kernel routines may add/delete callback routines to be invoked
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * upon process exit (triggered via the close operation from the /dev/zfs
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * These cleanup callbacks are intended to allow for the accumulation
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * of kernel state across multiple ioctls. User processes participate
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * by opening ZFS_DEV with O_EXCL. This causes the ZFS driver to do a
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * clone-open, generating a unique minor number. The process then passes
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * along that file descriptor to each ioctl that might have a cleanup operation.
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * Consumers of the onexit routines should call zfs_onexit_fd_hold() early
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * on to validate the given fd and add a reference to its file table entry.
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * This allows the consumer to do its work and then add a callback, knowing
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * that zfs_onexit_add_cb() won't fail with EBADF. When finished, consumers
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * should call zfs_onexit_fd_rele().
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * A simple example is zfs_ioc_recv(), where we might create an AVL tree
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * with dataset/GUID mappings and then reuse that tree on subsequent
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * zfs_ioc_recv() calls.
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * On the first zfs_ioc_recv() call, dmu_recv_stream() will kmem_alloc()
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * the AVL tree and pass it along with a callback function to
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * zfs_onexit_add_cb(). The zfs_onexit_add_cb() routine will register the
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * callback and return an action handle.
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * The action handle is then passed from user space to subsequent
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * zfs_ioc_recv() calls, so that dmu_recv_stream() can fetch its AVL tree
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * by calling zfs_onexit_cb_data() with the device minor number and
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * action handle.
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * If the user process exits abnormally, the callback is invoked implicitly
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * as part of the driver close operation. Once the user space process is
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * finished with the accumulated kernel state, it can also just call close(2)
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * on the cleanup fd to trigger the cleanup callback.
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby zo = *zop = kmem_zalloc(sizeof (zfs_onexit_t), KM_SLEEP);
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby mutex_init(&zo->zo_lock, NULL, MUTEX_DEFAULT, NULL);
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby list_create(&zo->zo_actions, sizeof (zfs_onexit_action_node_t),
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby while ((ap = list_head(&zo->zo_actions)) != NULL) {
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby kmem_free(ap, sizeof (zfs_onexit_action_node_t));
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirbyzfs_onexit_minor_to_state(minor_t minor, zfs_onexit_t **zo)
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby *zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * Consumers might need to operate by minor number instead of fd, since
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * they might be running in another thread (e.g. txg_sync_thread). Callers
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * of this function must call zfs_onexit_fd_rele() when they're finished
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * using the minor number.
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby return (zfs_onexit_minor_to_state(*minorp, &zo));
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * Add a callback to be invoked when the calling process exits.
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirbyzfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby ap = kmem_alloc(sizeof (zfs_onexit_action_node_t), KM_SLEEP);
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirbyzfs_onexit_find_cb(zfs_onexit_t *zo, uint64_t action_handle)
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby match = (zfs_onexit_action_node_t *)(uintptr_t)action_handle;
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby for (ap = list_head(l); ap != NULL; ap = list_next(l, ap)) {
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * Delete the callback, triggering it first if 'fire' is set.
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirbyzfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby kmem_free(ap, sizeof (zfs_onexit_action_node_t));
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * Return the data associated with this callback. This allows consumers
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * of the cleanup-on-exit interfaces to stash kernel data across system
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby * calls, knowing that it will be cleaned up if the calling process exits.