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. 23*be6fd75aSMatthew 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 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 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 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) 112*be6fd75aSMatthew 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 124a7f53a56SChris Kirby zfs_onexit_fd_hold(int fd, minor_t *minorp) 125c99e4bdcSChris Kirby { 126c99e4bdcSChris Kirby file_t *fp; 127a7f53a56SChris Kirby zfs_onexit_t *zo; 128c99e4bdcSChris Kirby 129c99e4bdcSChris Kirby fp = getf(fd); 130c99e4bdcSChris Kirby if (fp == NULL) 131*be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 132c99e4bdcSChris Kirby 133a7f53a56SChris Kirby *minorp = getminor(fp->f_vnode->v_rdev); 134a7f53a56SChris Kirby return (zfs_onexit_minor_to_state(*minorp, &zo)); 135c99e4bdcSChris Kirby } 136c99e4bdcSChris Kirby 137a7f53a56SChris Kirby void 138a7f53a56SChris Kirby zfs_onexit_fd_rele(int fd) 139a7f53a56SChris Kirby { 140a7f53a56SChris Kirby releasef(fd); 141c99e4bdcSChris Kirby } 142c99e4bdcSChris Kirby 143c99e4bdcSChris Kirby /* 144c99e4bdcSChris Kirby * Add a callback to be invoked when the calling process exits. 145c99e4bdcSChris Kirby */ 146c99e4bdcSChris Kirby int 147a7f53a56SChris Kirby zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data, 148c99e4bdcSChris Kirby uint64_t *action_handle) 149c99e4bdcSChris Kirby { 150c99e4bdcSChris Kirby zfs_onexit_t *zo; 151c99e4bdcSChris Kirby zfs_onexit_action_node_t *ap; 152c99e4bdcSChris Kirby int error; 153c99e4bdcSChris Kirby 154a7f53a56SChris Kirby error = zfs_onexit_minor_to_state(minor, &zo); 155c99e4bdcSChris Kirby if (error) 156c99e4bdcSChris Kirby return (error); 157c99e4bdcSChris Kirby 158c99e4bdcSChris Kirby ap = kmem_alloc(sizeof (zfs_onexit_action_node_t), KM_SLEEP); 159c99e4bdcSChris Kirby list_link_init(&ap->za_link); 160c99e4bdcSChris Kirby ap->za_func = func; 161c99e4bdcSChris Kirby ap->za_data = data; 162c99e4bdcSChris Kirby 163c99e4bdcSChris Kirby mutex_enter(&zo->zo_lock); 164c99e4bdcSChris Kirby list_insert_tail(&zo->zo_actions, ap); 165c99e4bdcSChris Kirby mutex_exit(&zo->zo_lock); 166a7f53a56SChris Kirby if (action_handle) 167c99e4bdcSChris Kirby *action_handle = (uint64_t)(uintptr_t)ap; 168c99e4bdcSChris Kirby 169c99e4bdcSChris Kirby return (0); 170c99e4bdcSChris Kirby } 171c99e4bdcSChris Kirby 172c99e4bdcSChris Kirby static zfs_onexit_action_node_t * 173c99e4bdcSChris Kirby zfs_onexit_find_cb(zfs_onexit_t *zo, uint64_t action_handle) 174c99e4bdcSChris Kirby { 175c99e4bdcSChris Kirby zfs_onexit_action_node_t *match; 176c99e4bdcSChris Kirby zfs_onexit_action_node_t *ap; 177c99e4bdcSChris Kirby list_t *l; 178c99e4bdcSChris Kirby 179c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zo->zo_lock)); 180c99e4bdcSChris Kirby 181c99e4bdcSChris Kirby match = (zfs_onexit_action_node_t *)(uintptr_t)action_handle; 182c99e4bdcSChris Kirby l = &zo->zo_actions; 183c99e4bdcSChris Kirby for (ap = list_head(l); ap != NULL; ap = list_next(l, ap)) { 184c99e4bdcSChris Kirby if (match == ap) 185c99e4bdcSChris Kirby break; 186c99e4bdcSChris Kirby } 187c99e4bdcSChris Kirby return (ap); 188c99e4bdcSChris Kirby } 189c99e4bdcSChris Kirby 190c99e4bdcSChris Kirby /* 191c99e4bdcSChris Kirby * Delete the callback, triggering it first if 'fire' is set. 192c99e4bdcSChris Kirby */ 193c99e4bdcSChris Kirby int 194a7f53a56SChris Kirby zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire) 195c99e4bdcSChris Kirby { 196c99e4bdcSChris Kirby zfs_onexit_t *zo; 197c99e4bdcSChris Kirby zfs_onexit_action_node_t *ap; 198c99e4bdcSChris Kirby int error; 199c99e4bdcSChris Kirby 200a7f53a56SChris Kirby error = zfs_onexit_minor_to_state(minor, &zo); 201c99e4bdcSChris Kirby if (error) 202c99e4bdcSChris Kirby return (error); 203c99e4bdcSChris Kirby 204c99e4bdcSChris Kirby mutex_enter(&zo->zo_lock); 205c99e4bdcSChris Kirby ap = zfs_onexit_find_cb(zo, action_handle); 206c99e4bdcSChris Kirby if (ap != NULL) { 207c99e4bdcSChris Kirby list_remove(&zo->zo_actions, ap); 208c99e4bdcSChris Kirby mutex_exit(&zo->zo_lock); 209c99e4bdcSChris Kirby if (fire) 210c99e4bdcSChris Kirby ap->za_func(ap->za_data); 211c99e4bdcSChris Kirby kmem_free(ap, sizeof (zfs_onexit_action_node_t)); 212c99e4bdcSChris Kirby } else { 213c99e4bdcSChris Kirby mutex_exit(&zo->zo_lock); 214*be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 215c99e4bdcSChris Kirby } 216c99e4bdcSChris Kirby 217c99e4bdcSChris Kirby return (error); 218c99e4bdcSChris Kirby } 219c99e4bdcSChris Kirby 220c99e4bdcSChris Kirby /* 221c99e4bdcSChris Kirby * Return the data associated with this callback. This allows consumers 222c99e4bdcSChris Kirby * of the cleanup-on-exit interfaces to stash kernel data across system 223c99e4bdcSChris Kirby * calls, knowing that it will be cleaned up if the calling process exits. 224c99e4bdcSChris Kirby */ 225c99e4bdcSChris Kirby int 226a7f53a56SChris Kirby zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data) 227c99e4bdcSChris Kirby { 228c99e4bdcSChris Kirby zfs_onexit_t *zo; 229c99e4bdcSChris Kirby zfs_onexit_action_node_t *ap; 230c99e4bdcSChris Kirby int error; 231c99e4bdcSChris Kirby 232c99e4bdcSChris Kirby *data = NULL; 233c99e4bdcSChris Kirby 234a7f53a56SChris Kirby error = zfs_onexit_minor_to_state(minor, &zo); 235c99e4bdcSChris Kirby if (error) 236c99e4bdcSChris Kirby return (error); 237c99e4bdcSChris Kirby 238c99e4bdcSChris Kirby mutex_enter(&zo->zo_lock); 239c99e4bdcSChris Kirby ap = zfs_onexit_find_cb(zo, action_handle); 240c99e4bdcSChris Kirby if (ap != NULL) 241c99e4bdcSChris Kirby *data = ap->za_data; 242c99e4bdcSChris Kirby else 243*be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 244c99e4bdcSChris Kirby mutex_exit(&zo->zo_lock); 245c99e4bdcSChris Kirby 246c99e4bdcSChris Kirby return (error); 247c99e4bdcSChris Kirby } 248