1 /* 2 * Copyright (c) 2020 iXsystems, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/types.h> 32 #include <sys/errno.h> 33 #include <sys/nvpair.h> 34 #include <sys/spa_impl.h> 35 #include <sys/vdev_os.h> 36 #include <sys/zfs_vfsops.h> 37 #include <sys/zone.h> 38 #include <vm/vm_pageout.h> 39 40 #include <sys/zfs_ioctl_impl.h> 41 42 #if __FreeBSD_version < 1201517 43 #define vm_page_max_user_wired vm_page_max_wired 44 #endif 45 46 int 47 zfs_vfs_ref(zfsvfs_t **zfvp) 48 { 49 int error = 0; 50 51 if (*zfvp == NULL) 52 return (SET_ERROR(ESRCH)); 53 54 error = vfs_busy((*zfvp)->z_vfs, 0); 55 if (error != 0) { 56 *zfvp = NULL; 57 error = SET_ERROR(ESRCH); 58 } 59 return (error); 60 } 61 62 boolean_t 63 zfs_vfs_held(zfsvfs_t *zfsvfs) 64 { 65 return (zfsvfs->z_vfs != NULL); 66 } 67 68 void 69 zfs_vfs_rele(zfsvfs_t *zfsvfs) 70 { 71 vfs_unbusy(zfsvfs->z_vfs); 72 } 73 74 static const zfs_ioc_key_t zfs_keys_nextboot[] = { 75 {"command", DATA_TYPE_STRING, 0}, 76 { ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64, 0}, 77 { ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64, 0} 78 }; 79 80 static int 81 zfs_ioc_jail(zfs_cmd_t *zc) 82 { 83 84 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name, 85 (int)zc->zc_zoneid)); 86 } 87 88 static int 89 zfs_ioc_unjail(zfs_cmd_t *zc) 90 { 91 92 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name, 93 (int)zc->zc_zoneid)); 94 } 95 96 static int 97 zfs_ioc_nextboot(const char *unused, nvlist_t *innvl, nvlist_t *outnvl) 98 { 99 char name[MAXNAMELEN]; 100 spa_t *spa; 101 vdev_t *vd; 102 const char *command; 103 uint64_t pool_guid; 104 uint64_t vdev_guid; 105 int error; 106 107 if (nvlist_lookup_uint64(innvl, 108 ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0) 109 return (EINVAL); 110 if (nvlist_lookup_uint64(innvl, 111 ZPOOL_CONFIG_GUID, &vdev_guid) != 0) 112 return (EINVAL); 113 if (nvlist_lookup_string(innvl, 114 "command", &command) != 0) 115 return (EINVAL); 116 117 mutex_enter(&spa_namespace_lock); 118 spa = spa_by_guid(pool_guid, vdev_guid); 119 if (spa != NULL) 120 strcpy(name, spa_name(spa)); 121 mutex_exit(&spa_namespace_lock); 122 if (spa == NULL) 123 return (ENOENT); 124 125 if ((error = spa_open(name, &spa, FTAG)) != 0) 126 return (error); 127 spa_vdev_state_enter(spa, SCL_ALL); 128 vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE); 129 if (vd == NULL) { 130 (void) spa_vdev_state_exit(spa, NULL, ENXIO); 131 spa_close(spa, FTAG); 132 return (ENODEV); 133 } 134 error = vdev_label_write_pad2(vd, command, strlen(command)); 135 (void) spa_vdev_state_exit(spa, NULL, 0); 136 txg_wait_synced(spa->spa_dsl_pool, 0); 137 spa_close(spa, FTAG); 138 return (error); 139 } 140 141 /* Update the VFS's cache of mountpoint properties */ 142 void 143 zfs_ioctl_update_mount_cache(const char *dsname) 144 { 145 zfsvfs_t *zfsvfs; 146 147 if (getzfsvfs(dsname, &zfsvfs) == 0) { 148 struct mount *mp = zfsvfs->z_vfs; 149 VFS_STATFS(mp, &mp->mnt_stat); 150 zfs_vfs_rele(zfsvfs); 151 } 152 /* 153 * Ignore errors; we can't do anything useful if either getzfsvfs or 154 * VFS_STATFS fails. 155 */ 156 } 157 158 uint64_t 159 zfs_max_nvlist_src_size_os(void) 160 { 161 if (zfs_max_nvlist_src_size != 0) 162 return (zfs_max_nvlist_src_size); 163 164 return (ptob(vm_page_max_user_wired) / 4); 165 } 166 167 void 168 zfs_ioctl_init_os(void) 169 { 170 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail, 171 zfs_secpolicy_config, POOL_CHECK_NONE); 172 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail, 173 zfs_secpolicy_config, POOL_CHECK_NONE); 174 zfs_ioctl_register("fbsd_nextboot", ZFS_IOC_NEXTBOOT, 175 zfs_ioc_nextboot, zfs_secpolicy_config, NO_NAME, 176 POOL_CHECK_NONE, B_FALSE, B_FALSE, zfs_keys_nextboot, 3); 177 178 } 179