1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 231df56adaSMartin Matuska * Portions Copyright 2011 Martin Matuska 24e9103aaeSGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 254263d13fSGeorge Wilson * Copyright (c) 2012 by Delphix. All rights reserved. 26*4e3c9f44SBill Pijewski * Copyright (c) 2012, Joyent, Inc. All rights reserved. 27e9103aaeSGarrett D'Amore */ 28fa9e4066Sahrens 29fa9e4066Sahrens #include <sys/types.h> 30fa9e4066Sahrens #include <sys/param.h> 31fa9e4066Sahrens #include <sys/errno.h> 32fa9e4066Sahrens #include <sys/uio.h> 33fa9e4066Sahrens #include <sys/buf.h> 34fa9e4066Sahrens #include <sys/modctl.h> 35fa9e4066Sahrens #include <sys/open.h> 36fa9e4066Sahrens #include <sys/file.h> 37fa9e4066Sahrens #include <sys/kmem.h> 38fa9e4066Sahrens #include <sys/conf.h> 39fa9e4066Sahrens #include <sys/cmn_err.h> 40fa9e4066Sahrens #include <sys/stat.h> 41fa9e4066Sahrens #include <sys/zfs_ioctl.h> 424201a95eSRic Aleshire #include <sys/zfs_vfsops.h> 43da6c28aaSamw #include <sys/zfs_znode.h> 44fa9e4066Sahrens #include <sys/zap.h> 45fa9e4066Sahrens #include <sys/spa.h> 46b1b8ab34Slling #include <sys/spa_impl.h> 47fa9e4066Sahrens #include <sys/vdev.h> 484201a95eSRic Aleshire #include <sys/priv_impl.h> 49fa9e4066Sahrens #include <sys/dmu.h> 50fa9e4066Sahrens #include <sys/dsl_dir.h> 51fa9e4066Sahrens #include <sys/dsl_dataset.h> 52fa9e4066Sahrens #include <sys/dsl_prop.h> 53ecd6cf80Smarks #include <sys/dsl_deleg.h> 54ecd6cf80Smarks #include <sys/dmu_objset.h> 55*4e3c9f44SBill Pijewski #include <sys/dmu_impl.h> 56fa9e4066Sahrens #include <sys/ddi.h> 57fa9e4066Sahrens #include <sys/sunddi.h> 58fa9e4066Sahrens #include <sys/sunldi.h> 59fa9e4066Sahrens #include <sys/policy.h> 60fa9e4066Sahrens #include <sys/zone.h> 61fa9e4066Sahrens #include <sys/nvpair.h> 62fa9e4066Sahrens #include <sys/pathname.h> 63fa9e4066Sahrens #include <sys/mount.h> 64fa9e4066Sahrens #include <sys/sdt.h> 65fa9e4066Sahrens #include <sys/fs/zfs.h> 66fa9e4066Sahrens #include <sys/zfs_ctldir.h> 67da6c28aaSamw #include <sys/zfs_dir.h> 68c99e4bdcSChris Kirby #include <sys/zfs_onexit.h> 69a2eea2e1Sahrens #include <sys/zvol.h> 703f9d6ad7SLin Ling #include <sys/dsl_scan.h> 71ecd6cf80Smarks #include <sharefs/share.h> 72f18faf3fSek #include <sys/dmu_objset.h> 73fa9e4066Sahrens 74fa9e4066Sahrens #include "zfs_namecheck.h" 75e9dbad6fSeschrock #include "zfs_prop.h" 76ecd6cf80Smarks #include "zfs_deleg.h" 770a586ceaSMark Shellenbaum #include "zfs_comutil.h" 78fa9e4066Sahrens 79fa9e4066Sahrens extern struct modlfs zfs_modlfs; 80fa9e4066Sahrens 81fa9e4066Sahrens extern void zfs_init(void); 82fa9e4066Sahrens extern void zfs_fini(void); 83fa9e4066Sahrens 84fa9e4066Sahrens ldi_ident_t zfs_li = NULL; 85fa9e4066Sahrens dev_info_t *zfs_dip; 86fa9e4066Sahrens 87fa9e4066Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *); 88ecd6cf80Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 89fa9e4066Sahrens 9054d692b7SGeorge Wilson typedef enum { 9154d692b7SGeorge Wilson NO_NAME, 9254d692b7SGeorge Wilson POOL_NAME, 9354d692b7SGeorge Wilson DATASET_NAME 9454d692b7SGeorge Wilson } zfs_ioc_namecheck_t; 9554d692b7SGeorge Wilson 96f9af39baSGeorge Wilson typedef enum { 97f9af39baSGeorge Wilson POOL_CHECK_NONE = 1 << 0, 98f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED = 1 << 1, 99f9af39baSGeorge Wilson POOL_CHECK_READONLY = 1 << 2 100f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t; 101f9af39baSGeorge Wilson 102fa9e4066Sahrens typedef struct zfs_ioc_vec { 103fa9e4066Sahrens zfs_ioc_func_t *zvec_func; 104fa9e4066Sahrens zfs_secpolicy_func_t *zvec_secpolicy; 10554d692b7SGeorge Wilson zfs_ioc_namecheck_t zvec_namecheck; 106ecd6cf80Smarks boolean_t zvec_his_log; 107f9af39baSGeorge Wilson zfs_ioc_poolcheck_t zvec_pool_check; 108fa9e4066Sahrens } zfs_ioc_vec_t; 109fa9e4066Sahrens 11014843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */ 11114843421SMatthew Ahrens static const char *userquota_perms[] = { 11214843421SMatthew Ahrens ZFS_DELEG_PERM_USERUSED, 11314843421SMatthew Ahrens ZFS_DELEG_PERM_USERQUOTA, 11414843421SMatthew Ahrens ZFS_DELEG_PERM_GROUPUSED, 11514843421SMatthew Ahrens ZFS_DELEG_PERM_GROUPQUOTA, 11614843421SMatthew Ahrens }; 11714843421SMatthew Ahrens 11814843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 11992241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property, 12092241e0bSTom Erickson cred_t *cr); 12192241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props, 12292241e0bSTom Erickson nvlist_t **errors); 1230a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 1240a48a24eStimh boolean_t *); 12592241e0bSTom Erickson int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **); 1260a48a24eStimh 127fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 128fa9e4066Sahrens void 129fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 130fa9e4066Sahrens { 131fa9e4066Sahrens const char *newfile; 1323f9d6ad7SLin Ling char buf[512]; 133fa9e4066Sahrens va_list adx; 134fa9e4066Sahrens 135fa9e4066Sahrens /* 136fa9e4066Sahrens * Get rid of annoying "../common/" prefix to filename. 137fa9e4066Sahrens */ 138fa9e4066Sahrens newfile = strrchr(file, '/'); 139fa9e4066Sahrens if (newfile != NULL) { 140fa9e4066Sahrens newfile = newfile + 1; /* Get rid of leading / */ 141fa9e4066Sahrens } else { 142fa9e4066Sahrens newfile = file; 143fa9e4066Sahrens } 144fa9e4066Sahrens 145fa9e4066Sahrens va_start(adx, fmt); 146fa9e4066Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, adx); 147fa9e4066Sahrens va_end(adx); 148fa9e4066Sahrens 149fa9e4066Sahrens /* 150fa9e4066Sahrens * To get this data, use the zfs-dprintf probe as so: 151fa9e4066Sahrens * dtrace -q -n 'zfs-dprintf \ 152fa9e4066Sahrens * /stringof(arg0) == "dbuf.c"/ \ 153fa9e4066Sahrens * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 154fa9e4066Sahrens * arg0 = file name 155fa9e4066Sahrens * arg1 = function name 156fa9e4066Sahrens * arg2 = line number 157fa9e4066Sahrens * arg3 = message 158fa9e4066Sahrens */ 159fa9e4066Sahrens DTRACE_PROBE4(zfs__dprintf, 160fa9e4066Sahrens char *, newfile, char *, func, int, line, char *, buf); 161fa9e4066Sahrens } 162fa9e4066Sahrens 163ecd6cf80Smarks static void 164228975ccSek history_str_free(char *buf) 165228975ccSek { 166228975ccSek kmem_free(buf, HIS_MAX_RECORD_LEN); 167228975ccSek } 168228975ccSek 169228975ccSek static char * 170228975ccSek history_str_get(zfs_cmd_t *zc) 171ecd6cf80Smarks { 17240feaa91Sahrens char *buf; 173ecd6cf80Smarks 174ecd6cf80Smarks if (zc->zc_history == NULL) 175228975ccSek return (NULL); 176e7437265Sahrens 177ecd6cf80Smarks buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 178ecd6cf80Smarks if (copyinstr((void *)(uintptr_t)zc->zc_history, 179ecd6cf80Smarks buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 180228975ccSek history_str_free(buf); 181228975ccSek return (NULL); 182ecd6cf80Smarks } 183ecd6cf80Smarks 184ecd6cf80Smarks buf[HIS_MAX_RECORD_LEN -1] = '\0'; 185ecd6cf80Smarks 186228975ccSek return (buf); 187228975ccSek } 188ecd6cf80Smarks 18915e6edf1Sgw /* 19015e6edf1Sgw * Check to see if the named dataset is currently defined as bootable 19115e6edf1Sgw */ 19215e6edf1Sgw static boolean_t 19315e6edf1Sgw zfs_is_bootfs(const char *name) 19415e6edf1Sgw { 195503ad85cSMatthew Ahrens objset_t *os; 19615e6edf1Sgw 197503ad85cSMatthew Ahrens if (dmu_objset_hold(name, FTAG, &os) == 0) { 198503ad85cSMatthew Ahrens boolean_t ret; 199b24ab676SJeff Bonwick ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os))); 200503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 201503ad85cSMatthew Ahrens return (ret); 20215e6edf1Sgw } 203503ad85cSMatthew Ahrens return (B_FALSE); 20415e6edf1Sgw } 20515e6edf1Sgw 206c2a93d44Stimh /* 2070a48a24eStimh * zfs_earlier_version 208c2a93d44Stimh * 209c2a93d44Stimh * Return non-zero if the spa version is less than requested version. 210c2a93d44Stimh */ 211da6c28aaSamw static int 2120a48a24eStimh zfs_earlier_version(const char *name, int version) 213da6c28aaSamw { 214da6c28aaSamw spa_t *spa; 215da6c28aaSamw 216da6c28aaSamw if (spa_open(name, &spa, FTAG) == 0) { 217da6c28aaSamw if (spa_version(spa) < version) { 218da6c28aaSamw spa_close(spa, FTAG); 219da6c28aaSamw return (1); 220da6c28aaSamw } 221da6c28aaSamw spa_close(spa, FTAG); 222da6c28aaSamw } 223da6c28aaSamw return (0); 224da6c28aaSamw } 225da6c28aaSamw 2269e6eda55Smarks /* 227745cd3c5Smaybee * zpl_earlier_version 2289e6eda55Smarks * 229745cd3c5Smaybee * Return TRUE if the ZPL version is less than requested version. 2309e6eda55Smarks */ 231745cd3c5Smaybee static boolean_t 232745cd3c5Smaybee zpl_earlier_version(const char *name, int version) 2339e6eda55Smarks { 2349e6eda55Smarks objset_t *os; 235745cd3c5Smaybee boolean_t rc = B_TRUE; 2369e6eda55Smarks 237503ad85cSMatthew Ahrens if (dmu_objset_hold(name, FTAG, &os) == 0) { 238745cd3c5Smaybee uint64_t zplversion; 2399e6eda55Smarks 240503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 241503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 242503ad85cSMatthew Ahrens return (B_TRUE); 243503ad85cSMatthew Ahrens } 244503ad85cSMatthew Ahrens /* XXX reading from non-owned objset */ 245745cd3c5Smaybee if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 246745cd3c5Smaybee rc = zplversion < version; 247503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2489e6eda55Smarks } 2499e6eda55Smarks return (rc); 2509e6eda55Smarks } 2519e6eda55Smarks 252228975ccSek static void 253228975ccSek zfs_log_history(zfs_cmd_t *zc) 254228975ccSek { 255228975ccSek spa_t *spa; 256228975ccSek char *buf; 257ecd6cf80Smarks 258228975ccSek if ((buf = history_str_get(zc)) == NULL) 259228975ccSek return; 260228975ccSek 261228975ccSek if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 262228975ccSek if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 263228975ccSek (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 264228975ccSek spa_close(spa, FTAG); 265228975ccSek } 266228975ccSek history_str_free(buf); 267ecd6cf80Smarks } 268ecd6cf80Smarks 269fa9e4066Sahrens /* 270fa9e4066Sahrens * Policy for top-level read operations (list pools). Requires no privileges, 271fa9e4066Sahrens * and can be used in the local zone, as there is no associated dataset. 272fa9e4066Sahrens */ 273fa9e4066Sahrens /* ARGSUSED */ 274fa9e4066Sahrens static int 275ecd6cf80Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 276fa9e4066Sahrens { 277fa9e4066Sahrens return (0); 278fa9e4066Sahrens } 279fa9e4066Sahrens 280fa9e4066Sahrens /* 281fa9e4066Sahrens * Policy for dataset read operations (list children, get statistics). Requires 282fa9e4066Sahrens * no privileges, but must be visible in the local zone. 283fa9e4066Sahrens */ 284fa9e4066Sahrens /* ARGSUSED */ 285fa9e4066Sahrens static int 286ecd6cf80Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 287fa9e4066Sahrens { 288fa9e4066Sahrens if (INGLOBALZONE(curproc) || 289ecd6cf80Smarks zone_dataset_visible(zc->zc_name, NULL)) 290fa9e4066Sahrens return (0); 291fa9e4066Sahrens 292fa9e4066Sahrens return (ENOENT); 293fa9e4066Sahrens } 294fa9e4066Sahrens 295fa9e4066Sahrens static int 296a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr) 297fa9e4066Sahrens { 298fa9e4066Sahrens int writable = 1; 299fa9e4066Sahrens 300fa9e4066Sahrens /* 301fa9e4066Sahrens * The dataset must be visible by this zone -- check this first 302fa9e4066Sahrens * so they don't see EPERM on something they shouldn't know about. 303fa9e4066Sahrens */ 304fa9e4066Sahrens if (!INGLOBALZONE(curproc) && 305fa9e4066Sahrens !zone_dataset_visible(dataset, &writable)) 306fa9e4066Sahrens return (ENOENT); 307fa9e4066Sahrens 308fa9e4066Sahrens if (INGLOBALZONE(curproc)) { 309fa9e4066Sahrens /* 310fa9e4066Sahrens * If the fs is zoned, only root can access it from the 311fa9e4066Sahrens * global zone. 312fa9e4066Sahrens */ 313fa9e4066Sahrens if (secpolicy_zfs(cr) && zoned) 314fa9e4066Sahrens return (EPERM); 315fa9e4066Sahrens } else { 316fa9e4066Sahrens /* 317fa9e4066Sahrens * If we are in a local zone, the 'zoned' property must be set. 318fa9e4066Sahrens */ 319fa9e4066Sahrens if (!zoned) 320fa9e4066Sahrens return (EPERM); 321fa9e4066Sahrens 322fa9e4066Sahrens /* must be writable by this zone */ 323fa9e4066Sahrens if (!writable) 324fa9e4066Sahrens return (EPERM); 325fa9e4066Sahrens } 326fa9e4066Sahrens return (0); 327fa9e4066Sahrens } 328fa9e4066Sahrens 329a7f53a56SChris Kirby static int 330a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr) 331a7f53a56SChris Kirby { 332a7f53a56SChris Kirby uint64_t zoned; 333a7f53a56SChris Kirby 334a7f53a56SChris Kirby if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 335a7f53a56SChris Kirby return (ENOENT); 336a7f53a56SChris Kirby 337a7f53a56SChris Kirby return (zfs_dozonecheck_impl(dataset, zoned, cr)); 338a7f53a56SChris Kirby } 339a7f53a56SChris Kirby 340a7f53a56SChris Kirby static int 341a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr) 342a7f53a56SChris Kirby { 343a7f53a56SChris Kirby uint64_t zoned; 344a7f53a56SChris Kirby 345a7f53a56SChris Kirby rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 346a7f53a56SChris Kirby if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) { 347a7f53a56SChris Kirby rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 348a7f53a56SChris Kirby return (ENOENT); 349a7f53a56SChris Kirby } 350a7f53a56SChris Kirby rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 351a7f53a56SChris Kirby 352a7f53a56SChris Kirby return (zfs_dozonecheck_impl(dataset, zoned, cr)); 353a7f53a56SChris Kirby } 354a7f53a56SChris Kirby 35519b94df9SMatthew Ahrens /* 35619b94df9SMatthew Ahrens * If name ends in a '@', then require recursive permissions. 35719b94df9SMatthew Ahrens */ 358fa9e4066Sahrens int 359ecd6cf80Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 360fa9e4066Sahrens { 361fa9e4066Sahrens int error; 36219b94df9SMatthew Ahrens boolean_t descendent = B_FALSE; 36319b94df9SMatthew Ahrens dsl_dataset_t *ds; 36419b94df9SMatthew Ahrens char *at; 36519b94df9SMatthew Ahrens 36619b94df9SMatthew Ahrens at = strchr(name, '@'); 36719b94df9SMatthew Ahrens if (at != NULL && at[1] == '\0') { 36819b94df9SMatthew Ahrens *at = '\0'; 36919b94df9SMatthew Ahrens descendent = B_TRUE; 37019b94df9SMatthew Ahrens } 37119b94df9SMatthew Ahrens 37219b94df9SMatthew Ahrens error = dsl_dataset_hold(name, FTAG, &ds); 37319b94df9SMatthew Ahrens if (at != NULL) 37419b94df9SMatthew Ahrens *at = '@'; 37519b94df9SMatthew Ahrens if (error != 0) 37619b94df9SMatthew Ahrens return (error); 377fa9e4066Sahrens 37819b94df9SMatthew Ahrens error = zfs_dozonecheck_ds(name, ds, cr); 379ecd6cf80Smarks if (error == 0) { 380ecd6cf80Smarks error = secpolicy_zfs(cr); 381db870a07Sahrens if (error) 38219b94df9SMatthew Ahrens error = dsl_deleg_access_impl(ds, descendent, perm, cr); 383ecd6cf80Smarks } 38419b94df9SMatthew Ahrens 38519b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 386ecd6cf80Smarks return (error); 387ecd6cf80Smarks } 388ecd6cf80Smarks 389a7f53a56SChris Kirby int 390a7f53a56SChris Kirby zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds, 391a7f53a56SChris Kirby const char *perm, cred_t *cr) 392a7f53a56SChris Kirby { 393a7f53a56SChris Kirby int error; 394a7f53a56SChris Kirby 395a7f53a56SChris Kirby error = zfs_dozonecheck_ds(name, ds, cr); 396a7f53a56SChris Kirby if (error == 0) { 397a7f53a56SChris Kirby error = secpolicy_zfs(cr); 398a7f53a56SChris Kirby if (error) 39919b94df9SMatthew Ahrens error = dsl_deleg_access_impl(ds, B_FALSE, perm, cr); 400a7f53a56SChris Kirby } 401a7f53a56SChris Kirby return (error); 402a7f53a56SChris Kirby } 403a7f53a56SChris Kirby 4044201a95eSRic Aleshire /* 4054201a95eSRic Aleshire * Policy for setting the security label property. 4064201a95eSRic Aleshire * 4074201a95eSRic Aleshire * Returns 0 for success, non-zero for access and other errors. 4084201a95eSRic Aleshire */ 4094201a95eSRic Aleshire static int 41092241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr) 4114201a95eSRic Aleshire { 4124201a95eSRic Aleshire char ds_hexsl[MAXNAMELEN]; 4134201a95eSRic Aleshire bslabel_t ds_sl, new_sl; 4144201a95eSRic Aleshire boolean_t new_default = FALSE; 4154201a95eSRic Aleshire uint64_t zoned; 4164201a95eSRic Aleshire int needed_priv = -1; 4174201a95eSRic Aleshire int error; 4184201a95eSRic Aleshire 4194201a95eSRic Aleshire /* First get the existing dataset label. */ 4204201a95eSRic Aleshire error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 4214201a95eSRic Aleshire 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 4224201a95eSRic Aleshire if (error) 4234201a95eSRic Aleshire return (EPERM); 4244201a95eSRic Aleshire 4254201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 4264201a95eSRic Aleshire new_default = TRUE; 4274201a95eSRic Aleshire 4284201a95eSRic Aleshire /* The label must be translatable */ 4294201a95eSRic Aleshire if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) 4304201a95eSRic Aleshire return (EINVAL); 4314201a95eSRic Aleshire 4324201a95eSRic Aleshire /* 4334201a95eSRic Aleshire * In a non-global zone, disallow attempts to set a label that 4344201a95eSRic Aleshire * doesn't match that of the zone; otherwise no other checks 4354201a95eSRic Aleshire * are needed. 4364201a95eSRic Aleshire */ 4374201a95eSRic Aleshire if (!INGLOBALZONE(curproc)) { 4384201a95eSRic Aleshire if (new_default || !blequal(&new_sl, CR_SL(CRED()))) 4394201a95eSRic Aleshire return (EPERM); 4404201a95eSRic Aleshire return (0); 4414201a95eSRic Aleshire } 4424201a95eSRic Aleshire 4434201a95eSRic Aleshire /* 4444201a95eSRic Aleshire * For global-zone datasets (i.e., those whose zoned property is 4454201a95eSRic Aleshire * "off", verify that the specified new label is valid for the 4464201a95eSRic Aleshire * global zone. 4474201a95eSRic Aleshire */ 4484201a95eSRic Aleshire if (dsl_prop_get_integer(name, 4494201a95eSRic Aleshire zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 4504201a95eSRic Aleshire return (EPERM); 4514201a95eSRic Aleshire if (!zoned) { 4524201a95eSRic Aleshire if (zfs_check_global_label(name, strval) != 0) 4534201a95eSRic Aleshire return (EPERM); 4544201a95eSRic Aleshire } 4554201a95eSRic Aleshire 4564201a95eSRic Aleshire /* 4574201a95eSRic Aleshire * If the existing dataset label is nondefault, check if the 4584201a95eSRic Aleshire * dataset is mounted (label cannot be changed while mounted). 4594201a95eSRic Aleshire * Get the zfsvfs; if there isn't one, then the dataset isn't 4604201a95eSRic Aleshire * mounted (or isn't a dataset, doesn't exist, ...). 4614201a95eSRic Aleshire */ 4624201a95eSRic Aleshire if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { 46392241e0bSTom Erickson objset_t *os; 46492241e0bSTom Erickson static char *setsl_tag = "setsl_tag"; 46592241e0bSTom Erickson 4664201a95eSRic Aleshire /* 4674201a95eSRic Aleshire * Try to own the dataset; abort if there is any error, 4684201a95eSRic Aleshire * (e.g., already mounted, in use, or other error). 4694201a95eSRic Aleshire */ 4704201a95eSRic Aleshire error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, 47192241e0bSTom Erickson setsl_tag, &os); 4724201a95eSRic Aleshire if (error) 4734201a95eSRic Aleshire return (EPERM); 4744201a95eSRic Aleshire 47592241e0bSTom Erickson dmu_objset_disown(os, setsl_tag); 47692241e0bSTom Erickson 4774201a95eSRic Aleshire if (new_default) { 4784201a95eSRic Aleshire needed_priv = PRIV_FILE_DOWNGRADE_SL; 4794201a95eSRic Aleshire goto out_check; 4804201a95eSRic Aleshire } 4814201a95eSRic Aleshire 4824201a95eSRic Aleshire if (hexstr_to_label(strval, &new_sl) != 0) 4834201a95eSRic Aleshire return (EPERM); 4844201a95eSRic Aleshire 4854201a95eSRic Aleshire if (blstrictdom(&ds_sl, &new_sl)) 4864201a95eSRic Aleshire needed_priv = PRIV_FILE_DOWNGRADE_SL; 4874201a95eSRic Aleshire else if (blstrictdom(&new_sl, &ds_sl)) 4884201a95eSRic Aleshire needed_priv = PRIV_FILE_UPGRADE_SL; 4894201a95eSRic Aleshire } else { 4904201a95eSRic Aleshire /* dataset currently has a default label */ 4914201a95eSRic Aleshire if (!new_default) 4924201a95eSRic Aleshire needed_priv = PRIV_FILE_UPGRADE_SL; 4934201a95eSRic Aleshire } 4944201a95eSRic Aleshire 4954201a95eSRic Aleshire out_check: 4964201a95eSRic Aleshire if (needed_priv != -1) 4974201a95eSRic Aleshire return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); 4984201a95eSRic Aleshire return (0); 4994201a95eSRic Aleshire } 5004201a95eSRic Aleshire 501ecd6cf80Smarks static int 50292241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, 50392241e0bSTom Erickson cred_t *cr) 504ecd6cf80Smarks { 50592241e0bSTom Erickson char *strval; 50692241e0bSTom Erickson 507ecd6cf80Smarks /* 508ecd6cf80Smarks * Check permissions for special properties. 509ecd6cf80Smarks */ 510ecd6cf80Smarks switch (prop) { 511ecd6cf80Smarks case ZFS_PROP_ZONED: 512ecd6cf80Smarks /* 513ecd6cf80Smarks * Disallow setting of 'zoned' from within a local zone. 514ecd6cf80Smarks */ 515ecd6cf80Smarks if (!INGLOBALZONE(curproc)) 516ecd6cf80Smarks return (EPERM); 517ecd6cf80Smarks break; 518ecd6cf80Smarks 519ecd6cf80Smarks case ZFS_PROP_QUOTA: 520ecd6cf80Smarks if (!INGLOBALZONE(curproc)) { 521ecd6cf80Smarks uint64_t zoned; 522ecd6cf80Smarks char setpoint[MAXNAMELEN]; 523ecd6cf80Smarks /* 524ecd6cf80Smarks * Unprivileged users are allowed to modify the 525ecd6cf80Smarks * quota on things *under* (ie. contained by) 526ecd6cf80Smarks * the thing they own. 527ecd6cf80Smarks */ 52892241e0bSTom Erickson if (dsl_prop_get_integer(dsname, "zoned", &zoned, 529ecd6cf80Smarks setpoint)) 530ecd6cf80Smarks return (EPERM); 53192241e0bSTom Erickson if (!zoned || strlen(dsname) <= strlen(setpoint)) 532ecd6cf80Smarks return (EPERM); 533ecd6cf80Smarks } 534db870a07Sahrens break; 5354201a95eSRic Aleshire 5364201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 5374201a95eSRic Aleshire if (!is_system_labeled()) 5384201a95eSRic Aleshire return (EPERM); 53992241e0bSTom Erickson 54092241e0bSTom Erickson if (nvpair_value_string(propval, &strval) == 0) { 54192241e0bSTom Erickson int err; 54292241e0bSTom Erickson 54392241e0bSTom Erickson err = zfs_set_slabel_policy(dsname, strval, CRED()); 54492241e0bSTom Erickson if (err != 0) 54592241e0bSTom Erickson return (err); 54692241e0bSTom Erickson } 5474201a95eSRic Aleshire break; 548ecd6cf80Smarks } 549ecd6cf80Smarks 55092241e0bSTom Erickson return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); 551ecd6cf80Smarks } 552ecd6cf80Smarks 553ecd6cf80Smarks int 554ecd6cf80Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 555ecd6cf80Smarks { 556ecd6cf80Smarks int error; 557ecd6cf80Smarks 558ecd6cf80Smarks error = zfs_dozonecheck(zc->zc_name, cr); 559ecd6cf80Smarks if (error) 560fa9e4066Sahrens return (error); 561fa9e4066Sahrens 562ecd6cf80Smarks /* 563ecd6cf80Smarks * permission to set permissions will be evaluated later in 564ecd6cf80Smarks * dsl_deleg_can_allow() 565ecd6cf80Smarks */ 566ecd6cf80Smarks return (0); 567ecd6cf80Smarks } 568ecd6cf80Smarks 569ecd6cf80Smarks int 570ecd6cf80Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 571ecd6cf80Smarks { 572681d9761SEric Taylor return (zfs_secpolicy_write_perms(zc->zc_name, 573681d9761SEric Taylor ZFS_DELEG_PERM_ROLLBACK, cr)); 574ecd6cf80Smarks } 575ecd6cf80Smarks 576ecd6cf80Smarks int 577ecd6cf80Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 578ecd6cf80Smarks { 579a7f53a56SChris Kirby spa_t *spa; 580a7f53a56SChris Kirby dsl_pool_t *dp; 581a7f53a56SChris Kirby dsl_dataset_t *ds; 582a7f53a56SChris Kirby char *cp; 583a7f53a56SChris Kirby int error; 584a7f53a56SChris Kirby 585a7f53a56SChris Kirby /* 586a7f53a56SChris Kirby * Generate the current snapshot name from the given objsetid, then 587a7f53a56SChris Kirby * use that name for the secpolicy/zone checks. 588a7f53a56SChris Kirby */ 589a7f53a56SChris Kirby cp = strchr(zc->zc_name, '@'); 590a7f53a56SChris Kirby if (cp == NULL) 591a7f53a56SChris Kirby return (EINVAL); 592a7f53a56SChris Kirby error = spa_open(zc->zc_name, &spa, FTAG); 593a7f53a56SChris Kirby if (error) 594a7f53a56SChris Kirby return (error); 595a7f53a56SChris Kirby 596a7f53a56SChris Kirby dp = spa_get_dsl(spa); 597a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 598a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 599a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 600a7f53a56SChris Kirby spa_close(spa, FTAG); 601a7f53a56SChris Kirby if (error) 602a7f53a56SChris Kirby return (error); 603a7f53a56SChris Kirby 604a7f53a56SChris Kirby dsl_dataset_name(ds, zc->zc_name); 605a7f53a56SChris Kirby 606a7f53a56SChris Kirby error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds, 607a7f53a56SChris Kirby ZFS_DELEG_PERM_SEND, cr); 608a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 609a7f53a56SChris Kirby 610a7f53a56SChris Kirby return (error); 611ecd6cf80Smarks } 612ecd6cf80Smarks 613743a77edSAlan Wright static int 614743a77edSAlan Wright zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr) 615743a77edSAlan Wright { 616743a77edSAlan Wright vnode_t *vp; 617743a77edSAlan Wright int error; 618743a77edSAlan Wright 619743a77edSAlan Wright if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 620743a77edSAlan Wright NO_FOLLOW, NULL, &vp)) != 0) 621743a77edSAlan Wright return (error); 622743a77edSAlan Wright 623743a77edSAlan Wright /* Now make sure mntpnt and dataset are ZFS */ 624743a77edSAlan Wright 625743a77edSAlan Wright if (vp->v_vfsp->vfs_fstype != zfsfstype || 626743a77edSAlan Wright (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 627743a77edSAlan Wright zc->zc_name) != 0)) { 628743a77edSAlan Wright VN_RELE(vp); 629743a77edSAlan Wright return (EPERM); 630743a77edSAlan Wright } 631743a77edSAlan Wright 632743a77edSAlan Wright VN_RELE(vp); 633743a77edSAlan Wright return (dsl_deleg_access(zc->zc_name, 634743a77edSAlan Wright ZFS_DELEG_PERM_SHARE, cr)); 635743a77edSAlan Wright } 636743a77edSAlan Wright 637ecd6cf80Smarks int 638ecd6cf80Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 639ecd6cf80Smarks { 640ecd6cf80Smarks if (!INGLOBALZONE(curproc)) 641ecd6cf80Smarks return (EPERM); 642ecd6cf80Smarks 6433cb34c60Sahrens if (secpolicy_nfs(cr) == 0) { 644ecd6cf80Smarks return (0); 645ecd6cf80Smarks } else { 646743a77edSAlan Wright return (zfs_secpolicy_deleg_share(zc, cr)); 647743a77edSAlan Wright } 648743a77edSAlan Wright } 649ecd6cf80Smarks 650743a77edSAlan Wright int 651743a77edSAlan Wright zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr) 652743a77edSAlan Wright { 653743a77edSAlan Wright if (!INGLOBALZONE(curproc)) 654743a77edSAlan Wright return (EPERM); 655ecd6cf80Smarks 656743a77edSAlan Wright if (secpolicy_smb(cr) == 0) { 657743a77edSAlan Wright return (0); 658743a77edSAlan Wright } else { 659743a77edSAlan Wright return (zfs_secpolicy_deleg_share(zc, cr)); 660ecd6cf80Smarks } 661fa9e4066Sahrens } 662fa9e4066Sahrens 663fa9e4066Sahrens static int 664ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize) 665fa9e4066Sahrens { 666fa9e4066Sahrens char *cp; 667fa9e4066Sahrens 668fa9e4066Sahrens /* 669fa9e4066Sahrens * Remove the @bla or /bla from the end of the name to get the parent. 670fa9e4066Sahrens */ 671ecd6cf80Smarks (void) strncpy(parent, datasetname, parentsize); 672ecd6cf80Smarks cp = strrchr(parent, '@'); 673fa9e4066Sahrens if (cp != NULL) { 674fa9e4066Sahrens cp[0] = '\0'; 675fa9e4066Sahrens } else { 676ecd6cf80Smarks cp = strrchr(parent, '/'); 677fa9e4066Sahrens if (cp == NULL) 678fa9e4066Sahrens return (ENOENT); 679fa9e4066Sahrens cp[0] = '\0'; 680ecd6cf80Smarks } 681ecd6cf80Smarks 682ecd6cf80Smarks return (0); 683ecd6cf80Smarks } 684ecd6cf80Smarks 685ecd6cf80Smarks int 686ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 687ecd6cf80Smarks { 688ecd6cf80Smarks int error; 689ecd6cf80Smarks 690ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(name, 691ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 692ecd6cf80Smarks return (error); 693ecd6cf80Smarks 694ecd6cf80Smarks return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 695ecd6cf80Smarks } 696ecd6cf80Smarks 697ecd6cf80Smarks static int 698ecd6cf80Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 699ecd6cf80Smarks { 700ecd6cf80Smarks return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 701ecd6cf80Smarks } 702ecd6cf80Smarks 703cbf6f6aaSWilliam Gorrell /* 704cbf6f6aaSWilliam Gorrell * Destroying snapshots with delegated permissions requires 705cbf6f6aaSWilliam Gorrell * descendent mount and destroy permissions. 706cbf6f6aaSWilliam Gorrell */ 707cbf6f6aaSWilliam Gorrell static int 70819b94df9SMatthew Ahrens zfs_secpolicy_destroy_recursive(zfs_cmd_t *zc, cred_t *cr) 709cbf6f6aaSWilliam Gorrell { 710cbf6f6aaSWilliam Gorrell int error; 711cbf6f6aaSWilliam Gorrell char *dsname; 712cbf6f6aaSWilliam Gorrell 71319b94df9SMatthew Ahrens dsname = kmem_asprintf("%s@", zc->zc_name); 714cbf6f6aaSWilliam Gorrell 715cbf6f6aaSWilliam Gorrell error = zfs_secpolicy_destroy_perms(dsname, cr); 716cbf6f6aaSWilliam Gorrell 717cbf6f6aaSWilliam Gorrell strfree(dsname); 718cbf6f6aaSWilliam Gorrell return (error); 719cbf6f6aaSWilliam Gorrell } 720cbf6f6aaSWilliam Gorrell 721ecd6cf80Smarks int 722ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 723ecd6cf80Smarks { 72492241e0bSTom Erickson char parentname[MAXNAMELEN]; 725ecd6cf80Smarks int error; 726ecd6cf80Smarks 727ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(from, 728ecd6cf80Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0) 729ecd6cf80Smarks return (error); 730ecd6cf80Smarks 731ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(from, 732ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 733ecd6cf80Smarks return (error); 734ecd6cf80Smarks 735ecd6cf80Smarks if ((error = zfs_get_parent(to, parentname, 736ecd6cf80Smarks sizeof (parentname))) != 0) 737ecd6cf80Smarks return (error); 738ecd6cf80Smarks 739ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname, 740ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 741ecd6cf80Smarks return (error); 742ecd6cf80Smarks 743ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname, 744ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 745ecd6cf80Smarks return (error); 746ecd6cf80Smarks 747ecd6cf80Smarks return (error); 748ecd6cf80Smarks } 749ecd6cf80Smarks 750ecd6cf80Smarks static int 751ecd6cf80Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 752ecd6cf80Smarks { 753ecd6cf80Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 754ecd6cf80Smarks } 755ecd6cf80Smarks 756ecd6cf80Smarks static int 757ecd6cf80Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 758ecd6cf80Smarks { 75992241e0bSTom Erickson char parentname[MAXNAMELEN]; 760ecd6cf80Smarks objset_t *clone; 761ecd6cf80Smarks int error; 762ecd6cf80Smarks 763ecd6cf80Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 764ecd6cf80Smarks ZFS_DELEG_PERM_PROMOTE, cr); 765ecd6cf80Smarks if (error) 766ecd6cf80Smarks return (error); 767ecd6cf80Smarks 768503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &clone); 769ecd6cf80Smarks 770ecd6cf80Smarks if (error == 0) { 771ecd6cf80Smarks dsl_dataset_t *pclone = NULL; 772ecd6cf80Smarks dsl_dir_t *dd; 773503ad85cSMatthew Ahrens dd = clone->os_dsl_dataset->ds_dir; 774ecd6cf80Smarks 775ecd6cf80Smarks rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 776745cd3c5Smaybee error = dsl_dataset_hold_obj(dd->dd_pool, 777745cd3c5Smaybee dd->dd_phys->dd_origin_obj, FTAG, &pclone); 778ecd6cf80Smarks rw_exit(&dd->dd_pool->dp_config_rwlock); 779ecd6cf80Smarks if (error) { 780503ad85cSMatthew Ahrens dmu_objset_rele(clone, FTAG); 781ecd6cf80Smarks return (error); 782ecd6cf80Smarks } 783ecd6cf80Smarks 784ecd6cf80Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 785ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr); 786ecd6cf80Smarks 787ecd6cf80Smarks dsl_dataset_name(pclone, parentname); 788503ad85cSMatthew Ahrens dmu_objset_rele(clone, FTAG); 789745cd3c5Smaybee dsl_dataset_rele(pclone, FTAG); 790ecd6cf80Smarks if (error == 0) 791ecd6cf80Smarks error = zfs_secpolicy_write_perms(parentname, 792ecd6cf80Smarks ZFS_DELEG_PERM_PROMOTE, cr); 793ecd6cf80Smarks } 794ecd6cf80Smarks return (error); 795ecd6cf80Smarks } 796ecd6cf80Smarks 797ecd6cf80Smarks static int 798ecd6cf80Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 799ecd6cf80Smarks { 800ecd6cf80Smarks int error; 801ecd6cf80Smarks 802ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 803ecd6cf80Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 804ecd6cf80Smarks return (error); 805ecd6cf80Smarks 806ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 807ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 808ecd6cf80Smarks return (error); 809ecd6cf80Smarks 810ecd6cf80Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 811ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)); 812ecd6cf80Smarks } 813ecd6cf80Smarks 814ecd6cf80Smarks int 815ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 816ecd6cf80Smarks { 817681d9761SEric Taylor return (zfs_secpolicy_write_perms(name, 818681d9761SEric Taylor ZFS_DELEG_PERM_SNAPSHOT, cr)); 819ecd6cf80Smarks } 820ecd6cf80Smarks 821ecd6cf80Smarks static int 822ecd6cf80Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 823ecd6cf80Smarks { 824ecd6cf80Smarks 825ecd6cf80Smarks return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 826ecd6cf80Smarks } 827ecd6cf80Smarks 828ecd6cf80Smarks static int 829ecd6cf80Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 830ecd6cf80Smarks { 83192241e0bSTom Erickson char parentname[MAXNAMELEN]; 83292241e0bSTom Erickson int error; 833ecd6cf80Smarks 834ecd6cf80Smarks if ((error = zfs_get_parent(zc->zc_name, parentname, 835ecd6cf80Smarks sizeof (parentname))) != 0) 836ecd6cf80Smarks return (error); 837fa9e4066Sahrens 838ecd6cf80Smarks if (zc->zc_value[0] != '\0') { 839ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_value, 840ecd6cf80Smarks ZFS_DELEG_PERM_CLONE, cr)) != 0) 841ecd6cf80Smarks return (error); 842fa9e4066Sahrens } 843fa9e4066Sahrens 844ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname, 845ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 846ecd6cf80Smarks return (error); 847ecd6cf80Smarks 848ecd6cf80Smarks error = zfs_secpolicy_write_perms(parentname, 849ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr); 850ecd6cf80Smarks 851ecd6cf80Smarks return (error); 852ecd6cf80Smarks } 853ecd6cf80Smarks 854ecd6cf80Smarks static int 855ecd6cf80Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 856ecd6cf80Smarks { 857ecd6cf80Smarks int error; 858ecd6cf80Smarks 859ecd6cf80Smarks error = secpolicy_fs_unmount(cr, NULL); 860ecd6cf80Smarks if (error) { 861ecd6cf80Smarks error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 862ecd6cf80Smarks } 863ecd6cf80Smarks return (error); 864fa9e4066Sahrens } 865fa9e4066Sahrens 866fa9e4066Sahrens /* 867fa9e4066Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 868fa9e4066Sahrens * SYS_CONFIG privilege, which is not available in a local zone. 869fa9e4066Sahrens */ 870fa9e4066Sahrens /* ARGSUSED */ 871fa9e4066Sahrens static int 872ecd6cf80Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 873fa9e4066Sahrens { 874fa9e4066Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0) 875fa9e4066Sahrens return (EPERM); 876fa9e4066Sahrens 877fa9e4066Sahrens return (0); 878fa9e4066Sahrens } 879fa9e4066Sahrens 88099d5e173STim Haley /* 88199d5e173STim Haley * Policy for object to name lookups. 88299d5e173STim Haley */ 88399d5e173STim Haley /* ARGSUSED */ 88499d5e173STim Haley static int 88599d5e173STim Haley zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr) 88699d5e173STim Haley { 88799d5e173STim Haley int error; 88899d5e173STim Haley 88999d5e173STim Haley if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0) 89099d5e173STim Haley return (0); 89199d5e173STim Haley 89299d5e173STim Haley error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr); 89399d5e173STim Haley return (error); 89499d5e173STim Haley } 89599d5e173STim Haley 896ea8dc4b6Seschrock /* 897ea8dc4b6Seschrock * Policy for fault injection. Requires all privileges. 898ea8dc4b6Seschrock */ 899ea8dc4b6Seschrock /* ARGSUSED */ 900ea8dc4b6Seschrock static int 901ecd6cf80Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 902ea8dc4b6Seschrock { 903ea8dc4b6Seschrock return (secpolicy_zinject(cr)); 904ea8dc4b6Seschrock } 905ea8dc4b6Seschrock 906e45ce728Sahrens static int 907e45ce728Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 908e45ce728Sahrens { 909e45ce728Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 910e45ce728Sahrens 911990b4856Slling if (prop == ZPROP_INVAL) { 912e45ce728Sahrens if (!zfs_prop_user(zc->zc_value)) 913e45ce728Sahrens return (EINVAL); 914e45ce728Sahrens return (zfs_secpolicy_write_perms(zc->zc_name, 915e45ce728Sahrens ZFS_DELEG_PERM_USERPROP, cr)); 916e45ce728Sahrens } else { 91792241e0bSTom Erickson return (zfs_secpolicy_setprop(zc->zc_name, prop, 91892241e0bSTom Erickson NULL, cr)); 919e45ce728Sahrens } 920e45ce728Sahrens } 921e45ce728Sahrens 92214843421SMatthew Ahrens static int 92314843421SMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr) 92414843421SMatthew Ahrens { 92514843421SMatthew Ahrens int err = zfs_secpolicy_read(zc, cr); 92614843421SMatthew Ahrens if (err) 92714843421SMatthew Ahrens return (err); 92814843421SMatthew Ahrens 92914843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 93014843421SMatthew Ahrens return (EINVAL); 93114843421SMatthew Ahrens 93214843421SMatthew Ahrens if (zc->zc_value[0] == 0) { 93314843421SMatthew Ahrens /* 93414843421SMatthew Ahrens * They are asking about a posix uid/gid. If it's 93514843421SMatthew Ahrens * themself, allow it. 93614843421SMatthew Ahrens */ 93714843421SMatthew Ahrens if (zc->zc_objset_type == ZFS_PROP_USERUSED || 93814843421SMatthew Ahrens zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 93914843421SMatthew Ahrens if (zc->zc_guid == crgetuid(cr)) 94014843421SMatthew Ahrens return (0); 94114843421SMatthew Ahrens } else { 94214843421SMatthew Ahrens if (groupmember(zc->zc_guid, cr)) 94314843421SMatthew Ahrens return (0); 94414843421SMatthew Ahrens } 94514843421SMatthew Ahrens } 94614843421SMatthew Ahrens 94714843421SMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name, 94814843421SMatthew Ahrens userquota_perms[zc->zc_objset_type], cr)); 94914843421SMatthew Ahrens } 95014843421SMatthew Ahrens 95114843421SMatthew Ahrens static int 95214843421SMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr) 95314843421SMatthew Ahrens { 95414843421SMatthew Ahrens int err = zfs_secpolicy_read(zc, cr); 95514843421SMatthew Ahrens if (err) 95614843421SMatthew Ahrens return (err); 95714843421SMatthew Ahrens 95814843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 95914843421SMatthew Ahrens return (EINVAL); 96014843421SMatthew Ahrens 96114843421SMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name, 96214843421SMatthew Ahrens userquota_perms[zc->zc_objset_type], cr)); 96314843421SMatthew Ahrens } 96414843421SMatthew Ahrens 96514843421SMatthew Ahrens static int 96614843421SMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr) 96714843421SMatthew Ahrens { 96892241e0bSTom Erickson return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 96992241e0bSTom Erickson NULL, cr)); 97014843421SMatthew Ahrens } 97114843421SMatthew Ahrens 972842727c2SChris Kirby static int 973842727c2SChris Kirby zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr) 974842727c2SChris Kirby { 975842727c2SChris Kirby return (zfs_secpolicy_write_perms(zc->zc_name, 976842727c2SChris Kirby ZFS_DELEG_PERM_HOLD, cr)); 977842727c2SChris Kirby } 978842727c2SChris Kirby 979842727c2SChris Kirby static int 980842727c2SChris Kirby zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr) 981842727c2SChris Kirby { 982842727c2SChris Kirby return (zfs_secpolicy_write_perms(zc->zc_name, 983842727c2SChris Kirby ZFS_DELEG_PERM_RELEASE, cr)); 984842727c2SChris Kirby } 985842727c2SChris Kirby 98699d5e173STim Haley /* 98799d5e173STim Haley * Policy for allowing temporary snapshots to be taken or released 98899d5e173STim Haley */ 98999d5e173STim Haley static int 99099d5e173STim Haley zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr) 99199d5e173STim Haley { 99299d5e173STim Haley /* 99399d5e173STim Haley * A temporary snapshot is the same as a snapshot, 99499d5e173STim Haley * hold, destroy and release all rolled into one. 99599d5e173STim Haley * Delegated diff alone is sufficient that we allow this. 99699d5e173STim Haley */ 99799d5e173STim Haley int error; 99899d5e173STim Haley 99999d5e173STim Haley if ((error = zfs_secpolicy_write_perms(zc->zc_name, 100099d5e173STim Haley ZFS_DELEG_PERM_DIFF, cr)) == 0) 100199d5e173STim Haley return (0); 100299d5e173STim Haley 100399d5e173STim Haley error = zfs_secpolicy_snapshot(zc, cr); 100499d5e173STim Haley if (!error) 100599d5e173STim Haley error = zfs_secpolicy_hold(zc, cr); 100699d5e173STim Haley if (!error) 100799d5e173STim Haley error = zfs_secpolicy_release(zc, cr); 100899d5e173STim Haley if (!error) 100999d5e173STim Haley error = zfs_secpolicy_destroy(zc, cr); 101099d5e173STim Haley return (error); 101199d5e173STim Haley } 101299d5e173STim Haley 1013fa9e4066Sahrens /* 1014fa9e4066Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t. 1015fa9e4066Sahrens */ 1016fa9e4066Sahrens static int 1017478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 1018fa9e4066Sahrens { 1019fa9e4066Sahrens char *packed; 1020fa9e4066Sahrens int error; 1021990b4856Slling nvlist_t *list = NULL; 1022fa9e4066Sahrens 1023fa9e4066Sahrens /* 1024e9dbad6fSeschrock * Read in and unpack the user-supplied nvlist. 1025fa9e4066Sahrens */ 1026990b4856Slling if (size == 0) 1027fa9e4066Sahrens return (EINVAL); 1028fa9e4066Sahrens 1029fa9e4066Sahrens packed = kmem_alloc(size, KM_SLEEP); 1030fa9e4066Sahrens 1031478ed9adSEric Taylor if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 1032478ed9adSEric Taylor iflag)) != 0) { 1033fa9e4066Sahrens kmem_free(packed, size); 1034fa9e4066Sahrens return (error); 1035fa9e4066Sahrens } 1036fa9e4066Sahrens 1037990b4856Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 1038fa9e4066Sahrens kmem_free(packed, size); 1039fa9e4066Sahrens return (error); 1040fa9e4066Sahrens } 1041fa9e4066Sahrens 1042fa9e4066Sahrens kmem_free(packed, size); 1043fa9e4066Sahrens 1044990b4856Slling *nvp = list; 1045fa9e4066Sahrens return (0); 1046fa9e4066Sahrens } 1047fa9e4066Sahrens 104892241e0bSTom Erickson static int 104992241e0bSTom Erickson fit_error_list(zfs_cmd_t *zc, nvlist_t **errors) 105092241e0bSTom Erickson { 105192241e0bSTom Erickson size_t size; 105292241e0bSTom Erickson 105392241e0bSTom Erickson VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 105492241e0bSTom Erickson 105592241e0bSTom Erickson if (size > zc->zc_nvlist_dst_size) { 105692241e0bSTom Erickson nvpair_t *more_errors; 105792241e0bSTom Erickson int n = 0; 105892241e0bSTom Erickson 105992241e0bSTom Erickson if (zc->zc_nvlist_dst_size < 1024) 106092241e0bSTom Erickson return (ENOMEM); 106192241e0bSTom Erickson 106292241e0bSTom Erickson VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0); 106392241e0bSTom Erickson more_errors = nvlist_prev_nvpair(*errors, NULL); 106492241e0bSTom Erickson 106592241e0bSTom Erickson do { 106692241e0bSTom Erickson nvpair_t *pair = nvlist_prev_nvpair(*errors, 106792241e0bSTom Erickson more_errors); 106892241e0bSTom Erickson VERIFY(nvlist_remove_nvpair(*errors, pair) == 0); 106992241e0bSTom Erickson n++; 107092241e0bSTom Erickson VERIFY(nvlist_size(*errors, &size, 107192241e0bSTom Erickson NV_ENCODE_NATIVE) == 0); 107292241e0bSTom Erickson } while (size > zc->zc_nvlist_dst_size); 107392241e0bSTom Erickson 107492241e0bSTom Erickson VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0); 107592241e0bSTom Erickson VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0); 107692241e0bSTom Erickson ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 107792241e0bSTom Erickson ASSERT(size <= zc->zc_nvlist_dst_size); 107892241e0bSTom Erickson } 107992241e0bSTom Erickson 108092241e0bSTom Erickson return (0); 108192241e0bSTom Erickson } 108292241e0bSTom Erickson 1083e9dbad6fSeschrock static int 1084e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 1085e9dbad6fSeschrock { 1086e9dbad6fSeschrock char *packed = NULL; 10876e27f868SSam Falkner int error = 0; 1088e9dbad6fSeschrock size_t size; 1089e9dbad6fSeschrock 1090e9dbad6fSeschrock VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 1091e9dbad6fSeschrock 1092e9dbad6fSeschrock if (size > zc->zc_nvlist_dst_size) { 1093e9dbad6fSeschrock error = ENOMEM; 1094e9dbad6fSeschrock } else { 1095da165920Smarks packed = kmem_alloc(size, KM_SLEEP); 1096e9dbad6fSeschrock VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 1097e9dbad6fSeschrock KM_SLEEP) == 0); 10986e27f868SSam Falkner if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 10996e27f868SSam Falkner size, zc->zc_iflags) != 0) 11006e27f868SSam Falkner error = EFAULT; 1101e9dbad6fSeschrock kmem_free(packed, size); 1102e9dbad6fSeschrock } 1103e9dbad6fSeschrock 1104e9dbad6fSeschrock zc->zc_nvlist_dst_size = size; 1105e9dbad6fSeschrock return (error); 1106e9dbad6fSeschrock } 1107e9dbad6fSeschrock 110814843421SMatthew Ahrens static int 1109af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp) 111014843421SMatthew Ahrens { 111114843421SMatthew Ahrens objset_t *os; 111214843421SMatthew Ahrens int error; 111314843421SMatthew Ahrens 1114503ad85cSMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os); 111514843421SMatthew Ahrens if (error) 111614843421SMatthew Ahrens return (error); 1117503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 1118503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1119503ad85cSMatthew Ahrens return (EINVAL); 1120503ad85cSMatthew Ahrens } 112114843421SMatthew Ahrens 1122503ad85cSMatthew Ahrens mutex_enter(&os->os_user_ptr_lock); 1123af4c679fSSean McEnroe *zfvp = dmu_objset_get_user(os); 1124af4c679fSSean McEnroe if (*zfvp) { 1125af4c679fSSean McEnroe VFS_HOLD((*zfvp)->z_vfs); 112614843421SMatthew Ahrens } else { 112714843421SMatthew Ahrens error = ESRCH; 112814843421SMatthew Ahrens } 1129503ad85cSMatthew Ahrens mutex_exit(&os->os_user_ptr_lock); 1130503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 113114843421SMatthew Ahrens return (error); 113214843421SMatthew Ahrens } 113314843421SMatthew Ahrens 113414843421SMatthew Ahrens /* 113514843421SMatthew Ahrens * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 113614843421SMatthew Ahrens * case its z_vfs will be NULL, and it will be opened as the owner. 113714843421SMatthew Ahrens */ 113814843421SMatthew Ahrens static int 11391412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer) 114014843421SMatthew Ahrens { 114114843421SMatthew Ahrens int error = 0; 114214843421SMatthew Ahrens 1143af4c679fSSean McEnroe if (getzfsvfs(name, zfvp) != 0) 1144af4c679fSSean McEnroe error = zfsvfs_create(name, zfvp); 114514843421SMatthew Ahrens if (error == 0) { 11461412a1a2SMark Shellenbaum rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER : 11471412a1a2SMark Shellenbaum RW_READER, tag); 1148af4c679fSSean McEnroe if ((*zfvp)->z_unmounted) { 114914843421SMatthew Ahrens /* 115014843421SMatthew Ahrens * XXX we could probably try again, since the unmounting 115114843421SMatthew Ahrens * thread should be just about to disassociate the 115214843421SMatthew Ahrens * objset from the zfsvfs. 115314843421SMatthew Ahrens */ 1154af4c679fSSean McEnroe rrw_exit(&(*zfvp)->z_teardown_lock, tag); 115514843421SMatthew Ahrens return (EBUSY); 115614843421SMatthew Ahrens } 115714843421SMatthew Ahrens } 115814843421SMatthew Ahrens return (error); 115914843421SMatthew Ahrens } 116014843421SMatthew Ahrens 116114843421SMatthew Ahrens static void 116214843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 116314843421SMatthew Ahrens { 116414843421SMatthew Ahrens rrw_exit(&zfsvfs->z_teardown_lock, tag); 116514843421SMatthew Ahrens 116614843421SMatthew Ahrens if (zfsvfs->z_vfs) { 116714843421SMatthew Ahrens VFS_RELE(zfsvfs->z_vfs); 116814843421SMatthew Ahrens } else { 1169503ad85cSMatthew Ahrens dmu_objset_disown(zfsvfs->z_os, zfsvfs); 117014843421SMatthew Ahrens zfsvfs_free(zfsvfs); 117114843421SMatthew Ahrens } 117214843421SMatthew Ahrens } 117314843421SMatthew Ahrens 1174fa9e4066Sahrens static int 1175fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc) 1176fa9e4066Sahrens { 1177fa9e4066Sahrens int error; 1178990b4856Slling nvlist_t *config, *props = NULL; 11790a48a24eStimh nvlist_t *rootprops = NULL; 11800a48a24eStimh nvlist_t *zplprops = NULL; 1181228975ccSek char *buf; 1182fa9e4066Sahrens 1183990b4856Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1184478ed9adSEric Taylor zc->zc_iflags, &config)) 1185fa9e4066Sahrens return (error); 11862a6b87f0Sek 1187990b4856Slling if (zc->zc_nvlist_src_size != 0 && (error = 1188478ed9adSEric Taylor get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1189478ed9adSEric Taylor zc->zc_iflags, &props))) { 1190990b4856Slling nvlist_free(config); 1191990b4856Slling return (error); 1192990b4856Slling } 1193990b4856Slling 11940a48a24eStimh if (props) { 11950a48a24eStimh nvlist_t *nvl = NULL; 11960a48a24eStimh uint64_t version = SPA_VERSION; 11970a48a24eStimh 11980a48a24eStimh (void) nvlist_lookup_uint64(props, 11990a48a24eStimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 12000a48a24eStimh if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 12010a48a24eStimh error = EINVAL; 12020a48a24eStimh goto pool_props_bad; 12030a48a24eStimh } 12040a48a24eStimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 12050a48a24eStimh if (nvl) { 12060a48a24eStimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 12070a48a24eStimh if (error != 0) { 12080a48a24eStimh nvlist_free(config); 12090a48a24eStimh nvlist_free(props); 12100a48a24eStimh return (error); 12110a48a24eStimh } 12120a48a24eStimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 12130a48a24eStimh } 12140a48a24eStimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 12150a48a24eStimh error = zfs_fill_zplprops_root(version, rootprops, 12160a48a24eStimh zplprops, NULL); 12170a48a24eStimh if (error) 12180a48a24eStimh goto pool_props_bad; 12190a48a24eStimh } 12200a48a24eStimh 12212a6b87f0Sek buf = history_str_get(zc); 1222fa9e4066Sahrens 12230a48a24eStimh error = spa_create(zc->zc_name, config, props, buf, zplprops); 12240a48a24eStimh 12250a48a24eStimh /* 12260a48a24eStimh * Set the remaining root properties 12270a48a24eStimh */ 122892241e0bSTom Erickson if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, 122992241e0bSTom Erickson ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) 12300a48a24eStimh (void) spa_destroy(zc->zc_name); 1231fa9e4066Sahrens 12322a6b87f0Sek if (buf != NULL) 12332a6b87f0Sek history_str_free(buf); 1234990b4856Slling 12350a48a24eStimh pool_props_bad: 12360a48a24eStimh nvlist_free(rootprops); 12370a48a24eStimh nvlist_free(zplprops); 1238fa9e4066Sahrens nvlist_free(config); 12390a48a24eStimh nvlist_free(props); 1240990b4856Slling 1241fa9e4066Sahrens return (error); 1242fa9e4066Sahrens } 1243fa9e4066Sahrens 1244fa9e4066Sahrens static int 1245fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1246fa9e4066Sahrens { 1247ecd6cf80Smarks int error; 1248ecd6cf80Smarks zfs_log_history(zc); 1249ecd6cf80Smarks error = spa_destroy(zc->zc_name); 1250681d9761SEric Taylor if (error == 0) 1251681d9761SEric Taylor zvol_remove_minors(zc->zc_name); 1252ecd6cf80Smarks return (error); 1253fa9e4066Sahrens } 1254fa9e4066Sahrens 1255fa9e4066Sahrens static int 1256fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc) 1257fa9e4066Sahrens { 1258990b4856Slling nvlist_t *config, *props = NULL; 1259fa9e4066Sahrens uint64_t guid; 1260468c413aSTim Haley int error; 1261fa9e4066Sahrens 1262990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1263478ed9adSEric Taylor zc->zc_iflags, &config)) != 0) 1264990b4856Slling return (error); 1265990b4856Slling 1266990b4856Slling if (zc->zc_nvlist_src_size != 0 && (error = 1267478ed9adSEric Taylor get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1268478ed9adSEric Taylor zc->zc_iflags, &props))) { 1269990b4856Slling nvlist_free(config); 1270fa9e4066Sahrens return (error); 1271990b4856Slling } 1272fa9e4066Sahrens 1273fa9e4066Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1274ea8dc4b6Seschrock guid != zc->zc_guid) 1275fa9e4066Sahrens error = EINVAL; 1276fa9e4066Sahrens else 12774b964adaSGeorge Wilson error = spa_import(zc->zc_name, config, props, zc->zc_cookie); 1278fa9e4066Sahrens 12794b964adaSGeorge Wilson if (zc->zc_nvlist_dst != 0) { 12804b964adaSGeorge Wilson int err; 12814b964adaSGeorge Wilson 12824b964adaSGeorge Wilson if ((err = put_nvlist(zc, config)) != 0) 12834b964adaSGeorge Wilson error = err; 12844b964adaSGeorge Wilson } 1285468c413aSTim Haley 1286fa9e4066Sahrens nvlist_free(config); 1287fa9e4066Sahrens 1288990b4856Slling if (props) 1289990b4856Slling nvlist_free(props); 1290990b4856Slling 1291fa9e4066Sahrens return (error); 1292fa9e4066Sahrens } 1293fa9e4066Sahrens 1294fa9e4066Sahrens static int 1295fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc) 1296fa9e4066Sahrens { 1297ecd6cf80Smarks int error; 129889a89ebfSlling boolean_t force = (boolean_t)zc->zc_cookie; 1299394ab0cbSGeorge Wilson boolean_t hardforce = (boolean_t)zc->zc_guid; 130089a89ebfSlling 1301ecd6cf80Smarks zfs_log_history(zc); 1302394ab0cbSGeorge Wilson error = spa_export(zc->zc_name, NULL, force, hardforce); 1303681d9761SEric Taylor if (error == 0) 1304681d9761SEric Taylor zvol_remove_minors(zc->zc_name); 1305ecd6cf80Smarks return (error); 1306fa9e4066Sahrens } 1307fa9e4066Sahrens 1308fa9e4066Sahrens static int 1309fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 1310fa9e4066Sahrens { 1311fa9e4066Sahrens nvlist_t *configs; 1312fa9e4066Sahrens int error; 1313fa9e4066Sahrens 1314fa9e4066Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1315fa9e4066Sahrens return (EEXIST); 1316fa9e4066Sahrens 1317e9dbad6fSeschrock error = put_nvlist(zc, configs); 1318fa9e4066Sahrens 1319fa9e4066Sahrens nvlist_free(configs); 1320fa9e4066Sahrens 1321fa9e4066Sahrens return (error); 1322fa9e4066Sahrens } 1323fa9e4066Sahrens 1324fa9e4066Sahrens static int 1325fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 1326fa9e4066Sahrens { 1327fa9e4066Sahrens nvlist_t *config; 1328fa9e4066Sahrens int error; 1329ea8dc4b6Seschrock int ret = 0; 1330fa9e4066Sahrens 1331e9dbad6fSeschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 1332e9dbad6fSeschrock sizeof (zc->zc_value)); 1333fa9e4066Sahrens 1334fa9e4066Sahrens if (config != NULL) { 1335e9dbad6fSeschrock ret = put_nvlist(zc, config); 1336fa9e4066Sahrens nvlist_free(config); 1337ea8dc4b6Seschrock 1338ea8dc4b6Seschrock /* 1339ea8dc4b6Seschrock * The config may be present even if 'error' is non-zero. 1340ea8dc4b6Seschrock * In this case we return success, and preserve the real errno 1341ea8dc4b6Seschrock * in 'zc_cookie'. 1342ea8dc4b6Seschrock */ 1343ea8dc4b6Seschrock zc->zc_cookie = error; 1344fa9e4066Sahrens } else { 1345ea8dc4b6Seschrock ret = error; 1346fa9e4066Sahrens } 1347fa9e4066Sahrens 1348ea8dc4b6Seschrock return (ret); 1349fa9e4066Sahrens } 1350fa9e4066Sahrens 1351fa9e4066Sahrens /* 1352fa9e4066Sahrens * Try to import the given pool, returning pool stats as appropriate so that 1353fa9e4066Sahrens * user land knows which devices are available and overall pool health. 1354fa9e4066Sahrens */ 1355fa9e4066Sahrens static int 1356fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1357fa9e4066Sahrens { 1358fa9e4066Sahrens nvlist_t *tryconfig, *config; 1359fa9e4066Sahrens int error; 1360fa9e4066Sahrens 1361990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1362478ed9adSEric Taylor zc->zc_iflags, &tryconfig)) != 0) 1363fa9e4066Sahrens return (error); 1364fa9e4066Sahrens 1365fa9e4066Sahrens config = spa_tryimport(tryconfig); 1366fa9e4066Sahrens 1367fa9e4066Sahrens nvlist_free(tryconfig); 1368fa9e4066Sahrens 1369fa9e4066Sahrens if (config == NULL) 1370fa9e4066Sahrens return (EINVAL); 1371fa9e4066Sahrens 1372e9dbad6fSeschrock error = put_nvlist(zc, config); 1373fa9e4066Sahrens nvlist_free(config); 1374fa9e4066Sahrens 1375fa9e4066Sahrens return (error); 1376fa9e4066Sahrens } 1377fa9e4066Sahrens 13783f9d6ad7SLin Ling /* 13793f9d6ad7SLin Ling * inputs: 13803f9d6ad7SLin Ling * zc_name name of the pool 13813f9d6ad7SLin Ling * zc_cookie scan func (pool_scan_func_t) 13823f9d6ad7SLin Ling */ 1383fa9e4066Sahrens static int 13843f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc) 1385fa9e4066Sahrens { 1386fa9e4066Sahrens spa_t *spa; 1387fa9e4066Sahrens int error; 1388fa9e4066Sahrens 138906eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 139006eeb2adSek return (error); 139106eeb2adSek 13923f9d6ad7SLin Ling if (zc->zc_cookie == POOL_SCAN_NONE) 13933f9d6ad7SLin Ling error = spa_scan_stop(spa); 13943f9d6ad7SLin Ling else 13953f9d6ad7SLin Ling error = spa_scan(spa, zc->zc_cookie); 139606eeb2adSek 139706eeb2adSek spa_close(spa, FTAG); 139806eeb2adSek 1399fa9e4066Sahrens return (error); 1400fa9e4066Sahrens } 1401fa9e4066Sahrens 1402fa9e4066Sahrens static int 1403fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1404fa9e4066Sahrens { 1405fa9e4066Sahrens spa_t *spa; 1406fa9e4066Sahrens int error; 1407fa9e4066Sahrens 1408fa9e4066Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1409fa9e4066Sahrens if (error == 0) { 1410fa9e4066Sahrens spa_freeze(spa); 1411fa9e4066Sahrens spa_close(spa, FTAG); 1412fa9e4066Sahrens } 1413fa9e4066Sahrens return (error); 1414fa9e4066Sahrens } 1415fa9e4066Sahrens 1416eaca9bbdSeschrock static int 1417eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 1418eaca9bbdSeschrock { 1419eaca9bbdSeschrock spa_t *spa; 1420eaca9bbdSeschrock int error; 1421eaca9bbdSeschrock 142206eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 142306eeb2adSek return (error); 142406eeb2adSek 1425558d2d50Slling if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 1426558d2d50Slling spa_close(spa, FTAG); 1427558d2d50Slling return (EINVAL); 1428558d2d50Slling } 1429558d2d50Slling 1430990b4856Slling spa_upgrade(spa, zc->zc_cookie); 143106eeb2adSek spa_close(spa, FTAG); 143206eeb2adSek 143306eeb2adSek return (error); 143406eeb2adSek } 143506eeb2adSek 143606eeb2adSek static int 143706eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc) 143806eeb2adSek { 143906eeb2adSek spa_t *spa; 144006eeb2adSek char *hist_buf; 144106eeb2adSek uint64_t size; 144206eeb2adSek int error; 144306eeb2adSek 144406eeb2adSek if ((size = zc->zc_history_len) == 0) 144506eeb2adSek return (EINVAL); 144606eeb2adSek 144706eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 144806eeb2adSek return (error); 144906eeb2adSek 1450e7437265Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 1451d7306b64Sek spa_close(spa, FTAG); 1452d7306b64Sek return (ENOTSUP); 1453d7306b64Sek } 1454d7306b64Sek 145506eeb2adSek hist_buf = kmem_alloc(size, KM_SLEEP); 145606eeb2adSek if ((error = spa_history_get(spa, &zc->zc_history_offset, 145706eeb2adSek &zc->zc_history_len, hist_buf)) == 0) { 1458478ed9adSEric Taylor error = ddi_copyout(hist_buf, 1459478ed9adSEric Taylor (void *)(uintptr_t)zc->zc_history, 1460478ed9adSEric Taylor zc->zc_history_len, zc->zc_iflags); 146106eeb2adSek } 146206eeb2adSek 146306eeb2adSek spa_close(spa, FTAG); 146406eeb2adSek kmem_free(hist_buf, size); 146506eeb2adSek return (error); 146606eeb2adSek } 146706eeb2adSek 1468e9103aaeSGarrett D'Amore static int 1469e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc) 1470e9103aaeSGarrett D'Amore { 1471e9103aaeSGarrett D'Amore spa_t *spa; 1472e9103aaeSGarrett D'Amore int error; 1473e9103aaeSGarrett D'Amore 1474e9103aaeSGarrett D'Amore error = spa_open(zc->zc_name, &spa, FTAG); 1475e9103aaeSGarrett D'Amore if (error == 0) { 1476e9103aaeSGarrett D'Amore error = spa_change_guid(spa); 1477e9103aaeSGarrett D'Amore spa_close(spa, FTAG); 1478e9103aaeSGarrett D'Amore } 1479e9103aaeSGarrett D'Amore return (error); 1480e9103aaeSGarrett D'Amore } 1481e9103aaeSGarrett D'Amore 148255434c77Sek static int 148355434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 148455434c77Sek { 148555434c77Sek int error; 148655434c77Sek 1487b1b8ab34Slling if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 148855434c77Sek return (error); 148955434c77Sek 149055434c77Sek return (0); 149155434c77Sek } 149255434c77Sek 1493503ad85cSMatthew Ahrens /* 1494503ad85cSMatthew Ahrens * inputs: 1495503ad85cSMatthew Ahrens * zc_name name of filesystem 1496503ad85cSMatthew Ahrens * zc_obj object to find 1497503ad85cSMatthew Ahrens * 1498503ad85cSMatthew Ahrens * outputs: 1499503ad85cSMatthew Ahrens * zc_value name of object 1500503ad85cSMatthew Ahrens */ 150155434c77Sek static int 150255434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc) 150355434c77Sek { 1504503ad85cSMatthew Ahrens objset_t *os; 150555434c77Sek int error; 150655434c77Sek 1507503ad85cSMatthew Ahrens /* XXX reading from objset not owned */ 1508503ad85cSMatthew Ahrens if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 150955434c77Sek return (error); 1510503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 1511503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1512503ad85cSMatthew Ahrens return (EINVAL); 1513503ad85cSMatthew Ahrens } 1514503ad85cSMatthew Ahrens error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 151555434c77Sek sizeof (zc->zc_value)); 1516503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 151755434c77Sek 151855434c77Sek return (error); 151955434c77Sek } 152055434c77Sek 152199d5e173STim Haley /* 152299d5e173STim Haley * inputs: 152399d5e173STim Haley * zc_name name of filesystem 152499d5e173STim Haley * zc_obj object to find 152599d5e173STim Haley * 152699d5e173STim Haley * outputs: 152799d5e173STim Haley * zc_stat stats on object 152899d5e173STim Haley * zc_value path to object 152999d5e173STim Haley */ 153099d5e173STim Haley static int 153199d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc) 153299d5e173STim Haley { 153399d5e173STim Haley objset_t *os; 153499d5e173STim Haley int error; 153599d5e173STim Haley 153699d5e173STim Haley /* XXX reading from objset not owned */ 153799d5e173STim Haley if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 153899d5e173STim Haley return (error); 153999d5e173STim Haley if (dmu_objset_type(os) != DMU_OST_ZFS) { 154099d5e173STim Haley dmu_objset_rele(os, FTAG); 154199d5e173STim Haley return (EINVAL); 154299d5e173STim Haley } 154399d5e173STim Haley error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value, 154499d5e173STim Haley sizeof (zc->zc_value)); 154599d5e173STim Haley dmu_objset_rele(os, FTAG); 154699d5e173STim Haley 154799d5e173STim Haley return (error); 154899d5e173STim Haley } 154999d5e173STim Haley 1550fa9e4066Sahrens static int 1551fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1552fa9e4066Sahrens { 1553fa9e4066Sahrens spa_t *spa; 1554fa9e4066Sahrens int error; 1555e7cbe64fSgw nvlist_t *config, **l2cache, **spares; 1556e7cbe64fSgw uint_t nl2cache = 0, nspares = 0; 1557fa9e4066Sahrens 1558fa9e4066Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1559fa9e4066Sahrens if (error != 0) 1560fa9e4066Sahrens return (error); 1561fa9e4066Sahrens 1562fa94a07fSbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1563478ed9adSEric Taylor zc->zc_iflags, &config); 1564fa94a07fSbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 1565fa94a07fSbrendan &l2cache, &nl2cache); 1566fa94a07fSbrendan 1567e7cbe64fSgw (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 1568e7cbe64fSgw &spares, &nspares); 1569e7cbe64fSgw 1570b1b8ab34Slling /* 1571b1b8ab34Slling * A root pool with concatenated devices is not supported. 1572e7cbe64fSgw * Thus, can not add a device to a root pool. 1573e7cbe64fSgw * 1574e7cbe64fSgw * Intent log device can not be added to a rootpool because 1575e7cbe64fSgw * during mountroot, zil is replayed, a seperated log device 1576e7cbe64fSgw * can not be accessed during the mountroot time. 1577e7cbe64fSgw * 1578e7cbe64fSgw * l2cache and spare devices are ok to be added to a rootpool. 1579b1b8ab34Slling */ 1580b24ab676SJeff Bonwick if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) { 15811195e687SMark J Musante nvlist_free(config); 1582b1b8ab34Slling spa_close(spa, FTAG); 1583b1b8ab34Slling return (EDOM); 1584b1b8ab34Slling } 1585b1b8ab34Slling 1586fa94a07fSbrendan if (error == 0) { 1587fa9e4066Sahrens error = spa_vdev_add(spa, config); 1588fa9e4066Sahrens nvlist_free(config); 1589fa9e4066Sahrens } 1590fa9e4066Sahrens spa_close(spa, FTAG); 1591fa9e4066Sahrens return (error); 1592fa9e4066Sahrens } 1593fa9e4066Sahrens 15943f9d6ad7SLin Ling /* 15953f9d6ad7SLin Ling * inputs: 15963f9d6ad7SLin Ling * zc_name name of the pool 15973f9d6ad7SLin Ling * zc_nvlist_conf nvlist of devices to remove 15983f9d6ad7SLin Ling * zc_cookie to stop the remove? 15993f9d6ad7SLin Ling */ 1600fa9e4066Sahrens static int 1601fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1602fa9e4066Sahrens { 160399653d4eSeschrock spa_t *spa; 160499653d4eSeschrock int error; 160599653d4eSeschrock 160699653d4eSeschrock error = spa_open(zc->zc_name, &spa, FTAG); 160799653d4eSeschrock if (error != 0) 160899653d4eSeschrock return (error); 160999653d4eSeschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 161099653d4eSeschrock spa_close(spa, FTAG); 161199653d4eSeschrock return (error); 1612fa9e4066Sahrens } 1613fa9e4066Sahrens 1614fa9e4066Sahrens static int 16153d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1616fa9e4066Sahrens { 1617fa9e4066Sahrens spa_t *spa; 1618fa9e4066Sahrens int error; 16193d7072f8Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1620fa9e4066Sahrens 162106eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1622fa9e4066Sahrens return (error); 16233d7072f8Seschrock switch (zc->zc_cookie) { 16243d7072f8Seschrock case VDEV_STATE_ONLINE: 16253d7072f8Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 16263d7072f8Seschrock break; 1627fa9e4066Sahrens 16283d7072f8Seschrock case VDEV_STATE_OFFLINE: 16293d7072f8Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 16303d7072f8Seschrock break; 1631fa9e4066Sahrens 16323d7072f8Seschrock case VDEV_STATE_FAULTED: 1633069f55e2SEric Schrock if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1634069f55e2SEric Schrock zc->zc_obj != VDEV_AUX_EXTERNAL) 1635069f55e2SEric Schrock zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1636069f55e2SEric Schrock 1637069f55e2SEric Schrock error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 16383d7072f8Seschrock break; 16393d7072f8Seschrock 16403d7072f8Seschrock case VDEV_STATE_DEGRADED: 1641069f55e2SEric Schrock if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1642069f55e2SEric Schrock zc->zc_obj != VDEV_AUX_EXTERNAL) 1643069f55e2SEric Schrock zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1644069f55e2SEric Schrock 1645069f55e2SEric Schrock error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 16463d7072f8Seschrock break; 16473d7072f8Seschrock 16483d7072f8Seschrock default: 16493d7072f8Seschrock error = EINVAL; 16503d7072f8Seschrock } 16513d7072f8Seschrock zc->zc_cookie = newstate; 1652fa9e4066Sahrens spa_close(spa, FTAG); 1653fa9e4066Sahrens return (error); 1654fa9e4066Sahrens } 1655fa9e4066Sahrens 1656fa9e4066Sahrens static int 1657fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1658fa9e4066Sahrens { 1659fa9e4066Sahrens spa_t *spa; 1660fa9e4066Sahrens int replacing = zc->zc_cookie; 1661fa9e4066Sahrens nvlist_t *config; 1662fa9e4066Sahrens int error; 1663fa9e4066Sahrens 166406eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1665fa9e4066Sahrens return (error); 1666fa9e4066Sahrens 1667990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1668478ed9adSEric Taylor zc->zc_iflags, &config)) == 0) { 1669ea8dc4b6Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1670fa9e4066Sahrens nvlist_free(config); 1671fa9e4066Sahrens } 1672fa9e4066Sahrens 1673fa9e4066Sahrens spa_close(spa, FTAG); 1674fa9e4066Sahrens return (error); 1675fa9e4066Sahrens } 1676fa9e4066Sahrens 1677fa9e4066Sahrens static int 1678fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1679fa9e4066Sahrens { 1680fa9e4066Sahrens spa_t *spa; 1681fa9e4066Sahrens int error; 1682fa9e4066Sahrens 168306eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1684fa9e4066Sahrens return (error); 1685fa9e4066Sahrens 16868ad4d6ddSJeff Bonwick error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1687fa9e4066Sahrens 1688fa9e4066Sahrens spa_close(spa, FTAG); 1689fa9e4066Sahrens return (error); 1690fa9e4066Sahrens } 1691fa9e4066Sahrens 16921195e687SMark J Musante static int 16931195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc) 16941195e687SMark J Musante { 16951195e687SMark J Musante spa_t *spa; 16961195e687SMark J Musante nvlist_t *config, *props = NULL; 16971195e687SMark J Musante int error; 16981195e687SMark J Musante boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); 16991195e687SMark J Musante 17001195e687SMark J Musante if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 17011195e687SMark J Musante return (error); 17021195e687SMark J Musante 17031195e687SMark J Musante if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 17041195e687SMark J Musante zc->zc_iflags, &config)) { 17051195e687SMark J Musante spa_close(spa, FTAG); 17061195e687SMark J Musante return (error); 17071195e687SMark J Musante } 17081195e687SMark J Musante 17091195e687SMark J Musante if (zc->zc_nvlist_src_size != 0 && (error = 17101195e687SMark J Musante get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 17111195e687SMark J Musante zc->zc_iflags, &props))) { 17121195e687SMark J Musante spa_close(spa, FTAG); 17131195e687SMark J Musante nvlist_free(config); 17141195e687SMark J Musante return (error); 17151195e687SMark J Musante } 17161195e687SMark J Musante 17171195e687SMark J Musante error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); 17181195e687SMark J Musante 17191195e687SMark J Musante spa_close(spa, FTAG); 17201195e687SMark J Musante 17211195e687SMark J Musante nvlist_free(config); 17221195e687SMark J Musante nvlist_free(props); 17231195e687SMark J Musante 17241195e687SMark J Musante return (error); 17251195e687SMark J Musante } 17261195e687SMark J Musante 1727c67d9675Seschrock static int 1728c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 1729c67d9675Seschrock { 1730c67d9675Seschrock spa_t *spa; 1731e9dbad6fSeschrock char *path = zc->zc_value; 1732ea8dc4b6Seschrock uint64_t guid = zc->zc_guid; 1733c67d9675Seschrock int error; 1734c67d9675Seschrock 1735c67d9675Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 1736c67d9675Seschrock if (error != 0) 1737c67d9675Seschrock return (error); 1738c67d9675Seschrock 1739c67d9675Seschrock error = spa_vdev_setpath(spa, guid, path); 1740c67d9675Seschrock spa_close(spa, FTAG); 1741c67d9675Seschrock return (error); 1742c67d9675Seschrock } 1743c67d9675Seschrock 17446809eb4eSEric Schrock static int 17456809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 17466809eb4eSEric Schrock { 17476809eb4eSEric Schrock spa_t *spa; 17486809eb4eSEric Schrock char *fru = zc->zc_value; 17496809eb4eSEric Schrock uint64_t guid = zc->zc_guid; 17506809eb4eSEric Schrock int error; 17516809eb4eSEric Schrock 17526809eb4eSEric Schrock error = spa_open(zc->zc_name, &spa, FTAG); 17536809eb4eSEric Schrock if (error != 0) 17546809eb4eSEric Schrock return (error); 17556809eb4eSEric Schrock 17566809eb4eSEric Schrock error = spa_vdev_setfru(spa, guid, fru); 17576809eb4eSEric Schrock spa_close(spa, FTAG); 17586809eb4eSEric Schrock return (error); 17596809eb4eSEric Schrock } 17606809eb4eSEric Schrock 1761fa9e4066Sahrens static int 1762a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os) 1763fa9e4066Sahrens { 1764a7f53a56SChris Kirby int error = 0; 17657f7322feSeschrock nvlist_t *nv; 1766fa9e4066Sahrens 1767a2eea2e1Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1768fa9e4066Sahrens 17695ad82045Snd if (zc->zc_nvlist_dst != 0 && 177092241e0bSTom Erickson (error = dsl_prop_get_all(os, &nv)) == 0) { 1771a2eea2e1Sahrens dmu_objset_stats(os, nv); 1772432f72fdSahrens /* 1773bd00f61bSrm * NB: zvol_get_stats() will read the objset contents, 1774432f72fdSahrens * which we aren't supposed to do with a 1775745cd3c5Smaybee * DS_MODE_USER hold, because it could be 1776432f72fdSahrens * inconsistent. So this is a bit of a workaround... 1777503ad85cSMatthew Ahrens * XXX reading with out owning 1778432f72fdSahrens */ 177919b94df9SMatthew Ahrens if (!zc->zc_objset_stats.dds_inconsistent && 178019b94df9SMatthew Ahrens dmu_objset_type(os) == DMU_OST_ZVOL) { 178119b94df9SMatthew Ahrens error = zvol_get_stats(os, nv); 178219b94df9SMatthew Ahrens if (error == EIO) 178319b94df9SMatthew Ahrens return (error); 178419b94df9SMatthew Ahrens VERIFY3S(error, ==, 0); 1785e7437265Sahrens } 1786e9dbad6fSeschrock error = put_nvlist(zc, nv); 17877f7322feSeschrock nvlist_free(nv); 17887f7322feSeschrock } 1789fa9e4066Sahrens 1790a7f53a56SChris Kirby return (error); 1791a7f53a56SChris Kirby } 1792a7f53a56SChris Kirby 1793a7f53a56SChris Kirby /* 1794a7f53a56SChris Kirby * inputs: 1795a7f53a56SChris Kirby * zc_name name of filesystem 1796a7f53a56SChris Kirby * zc_nvlist_dst_size size of buffer for property nvlist 1797a7f53a56SChris Kirby * 1798a7f53a56SChris Kirby * outputs: 1799a7f53a56SChris Kirby * zc_objset_stats stats 1800a7f53a56SChris Kirby * zc_nvlist_dst property nvlist 1801a7f53a56SChris Kirby * zc_nvlist_dst_size size of property nvlist 1802a7f53a56SChris Kirby */ 1803a7f53a56SChris Kirby static int 1804a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc) 1805a7f53a56SChris Kirby { 1806a7f53a56SChris Kirby objset_t *os = NULL; 1807a7f53a56SChris Kirby int error; 1808a7f53a56SChris Kirby 1809a7f53a56SChris Kirby if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1810a7f53a56SChris Kirby return (error); 1811a7f53a56SChris Kirby 1812a7f53a56SChris Kirby error = zfs_ioc_objset_stats_impl(zc, os); 1813a7f53a56SChris Kirby 1814503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1815a7f53a56SChris Kirby 1816fa9e4066Sahrens return (error); 1817fa9e4066Sahrens } 1818fa9e4066Sahrens 181992241e0bSTom Erickson /* 182092241e0bSTom Erickson * inputs: 182192241e0bSTom Erickson * zc_name name of filesystem 182292241e0bSTom Erickson * zc_nvlist_dst_size size of buffer for property nvlist 182392241e0bSTom Erickson * 182492241e0bSTom Erickson * outputs: 182592241e0bSTom Erickson * zc_nvlist_dst received property nvlist 182692241e0bSTom Erickson * zc_nvlist_dst_size size of received property nvlist 182792241e0bSTom Erickson * 182892241e0bSTom Erickson * Gets received properties (distinct from local properties on or after 182992241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 183092241e0bSTom Erickson * local property values. 183192241e0bSTom Erickson */ 183292241e0bSTom Erickson static int 183392241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 183492241e0bSTom Erickson { 183592241e0bSTom Erickson objset_t *os = NULL; 183692241e0bSTom Erickson int error; 183792241e0bSTom Erickson nvlist_t *nv; 183892241e0bSTom Erickson 183992241e0bSTom Erickson if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 184092241e0bSTom Erickson return (error); 184192241e0bSTom Erickson 184292241e0bSTom Erickson /* 184392241e0bSTom Erickson * Without this check, we would return local property values if the 184492241e0bSTom Erickson * caller has not already received properties on or after 184592241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS. 184692241e0bSTom Erickson */ 184792241e0bSTom Erickson if (!dsl_prop_get_hasrecvd(os)) { 184892241e0bSTom Erickson dmu_objset_rele(os, FTAG); 184992241e0bSTom Erickson return (ENOTSUP); 185092241e0bSTom Erickson } 185192241e0bSTom Erickson 185292241e0bSTom Erickson if (zc->zc_nvlist_dst != 0 && 185392241e0bSTom Erickson (error = dsl_prop_get_received(os, &nv)) == 0) { 185492241e0bSTom Erickson error = put_nvlist(zc, nv); 185592241e0bSTom Erickson nvlist_free(nv); 185692241e0bSTom Erickson } 185792241e0bSTom Erickson 185892241e0bSTom Erickson dmu_objset_rele(os, FTAG); 185992241e0bSTom Erickson return (error); 186092241e0bSTom Erickson } 186192241e0bSTom Erickson 1862de8267e0Stimh static int 1863de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 1864de8267e0Stimh { 1865de8267e0Stimh uint64_t value; 1866de8267e0Stimh int error; 1867de8267e0Stimh 1868de8267e0Stimh /* 1869de8267e0Stimh * zfs_get_zplprop() will either find a value or give us 1870de8267e0Stimh * the default value (if there is one). 1871de8267e0Stimh */ 1872de8267e0Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 1873de8267e0Stimh return (error); 1874de8267e0Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 1875de8267e0Stimh return (0); 1876de8267e0Stimh } 1877de8267e0Stimh 18783cb34c60Sahrens /* 18793cb34c60Sahrens * inputs: 18803cb34c60Sahrens * zc_name name of filesystem 1881de8267e0Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 18823cb34c60Sahrens * 18833cb34c60Sahrens * outputs: 1884de8267e0Stimh * zc_nvlist_dst zpl property nvlist 1885de8267e0Stimh * zc_nvlist_dst_size size of zpl property nvlist 18863cb34c60Sahrens */ 1887bd00f61bSrm static int 1888de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 1889bd00f61bSrm { 1890de8267e0Stimh objset_t *os; 1891de8267e0Stimh int err; 1892bd00f61bSrm 1893503ad85cSMatthew Ahrens /* XXX reading without owning */ 1894503ad85cSMatthew Ahrens if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1895de8267e0Stimh return (err); 1896bd00f61bSrm 1897bd00f61bSrm dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1898bd00f61bSrm 1899bd00f61bSrm /* 1900de8267e0Stimh * NB: nvl_add_zplprop() will read the objset contents, 1901745cd3c5Smaybee * which we aren't supposed to do with a DS_MODE_USER 1902745cd3c5Smaybee * hold, because it could be inconsistent. 1903bd00f61bSrm */ 1904de8267e0Stimh if (zc->zc_nvlist_dst != NULL && 1905de8267e0Stimh !zc->zc_objset_stats.dds_inconsistent && 1906de8267e0Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 1907de8267e0Stimh nvlist_t *nv; 1908de8267e0Stimh 1909de8267e0Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1910de8267e0Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 1911de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 1912de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 1913de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 1914de8267e0Stimh err = put_nvlist(zc, nv); 1915de8267e0Stimh nvlist_free(nv); 1916de8267e0Stimh } else { 1917de8267e0Stimh err = ENOENT; 1918de8267e0Stimh } 1919503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1920de8267e0Stimh return (err); 1921bd00f61bSrm } 1922bd00f61bSrm 192314843421SMatthew Ahrens static boolean_t 192414843421SMatthew Ahrens dataset_name_hidden(const char *name) 192514843421SMatthew Ahrens { 192614843421SMatthew Ahrens /* 192714843421SMatthew Ahrens * Skip over datasets that are not visible in this zone, 192814843421SMatthew Ahrens * internal datasets (which have a $ in their name), and 192914843421SMatthew Ahrens * temporary datasets (which have a % in their name). 193014843421SMatthew Ahrens */ 193114843421SMatthew Ahrens if (strchr(name, '$') != NULL) 193214843421SMatthew Ahrens return (B_TRUE); 193314843421SMatthew Ahrens if (strchr(name, '%') != NULL) 193414843421SMatthew Ahrens return (B_TRUE); 193514843421SMatthew Ahrens if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) 193614843421SMatthew Ahrens return (B_TRUE); 193714843421SMatthew Ahrens return (B_FALSE); 193814843421SMatthew Ahrens } 193914843421SMatthew Ahrens 1940de8267e0Stimh /* 1941de8267e0Stimh * inputs: 1942de8267e0Stimh * zc_name name of filesystem 1943de8267e0Stimh * zc_cookie zap cursor 1944de8267e0Stimh * zc_nvlist_dst_size size of buffer for property nvlist 1945de8267e0Stimh * 1946de8267e0Stimh * outputs: 1947de8267e0Stimh * zc_name name of next filesystem 194814843421SMatthew Ahrens * zc_cookie zap cursor 1949de8267e0Stimh * zc_objset_stats stats 1950de8267e0Stimh * zc_nvlist_dst property nvlist 1951de8267e0Stimh * zc_nvlist_dst_size size of property nvlist 1952de8267e0Stimh */ 1953fa9e4066Sahrens static int 1954fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1955fa9e4066Sahrens { 195687e5029aSahrens objset_t *os; 1957fa9e4066Sahrens int error; 1958fa9e4066Sahrens char *p; 1959620252bcSChris Kirby size_t orig_len = strlen(zc->zc_name); 1960fa9e4066Sahrens 1961620252bcSChris Kirby top: 1962503ad85cSMatthew Ahrens if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 196387e5029aSahrens if (error == ENOENT) 196487e5029aSahrens error = ESRCH; 196587e5029aSahrens return (error); 1966fa9e4066Sahrens } 1967fa9e4066Sahrens 1968fa9e4066Sahrens p = strrchr(zc->zc_name, '/'); 1969fa9e4066Sahrens if (p == NULL || p[1] != '\0') 1970fa9e4066Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1971fa9e4066Sahrens p = zc->zc_name + strlen(zc->zc_name); 1972fa9e4066Sahrens 19735c0b6a79SRich Morris /* 19745c0b6a79SRich Morris * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 19755c0b6a79SRich Morris * but is not declared void because its called by dmu_objset_find(). 19765c0b6a79SRich Morris */ 19777f73c863SRich Morris if (zc->zc_cookie == 0) { 19787f73c863SRich Morris uint64_t cookie = 0; 19797f73c863SRich Morris int len = sizeof (zc->zc_name) - (p - zc->zc_name); 19807f73c863SRich Morris 19811df56adaSMartin Matuska while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) { 19821df56adaSMartin Matuska if (!dataset_name_hidden(zc->zc_name)) 19831df56adaSMartin Matuska (void) dmu_objset_prefetch(zc->zc_name, NULL); 19841df56adaSMartin Matuska } 19857f73c863SRich Morris } 19867f73c863SRich Morris 1987fa9e4066Sahrens do { 198887e5029aSahrens error = dmu_dir_list_next(os, 198987e5029aSahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 199087e5029aSahrens NULL, &zc->zc_cookie); 1991fa9e4066Sahrens if (error == ENOENT) 1992fa9e4066Sahrens error = ESRCH; 199319b94df9SMatthew Ahrens } while (error == 0 && dataset_name_hidden(zc->zc_name)); 1994503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1995fa9e4066Sahrens 1996681d9761SEric Taylor /* 1997681d9761SEric Taylor * If it's an internal dataset (ie. with a '$' in its name), 1998681d9761SEric Taylor * don't try to get stats for it, otherwise we'll return ENOENT. 1999681d9761SEric Taylor */ 2000620252bcSChris Kirby if (error == 0 && strchr(zc->zc_name, '$') == NULL) { 200187e5029aSahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 2002620252bcSChris Kirby if (error == ENOENT) { 2003620252bcSChris Kirby /* We lost a race with destroy, get the next one. */ 2004620252bcSChris Kirby zc->zc_name[orig_len] = '\0'; 2005620252bcSChris Kirby goto top; 2006620252bcSChris Kirby } 2007620252bcSChris Kirby } 2008fa9e4066Sahrens return (error); 2009fa9e4066Sahrens } 2010fa9e4066Sahrens 20113cb34c60Sahrens /* 20123cb34c60Sahrens * inputs: 20133cb34c60Sahrens * zc_name name of filesystem 20143cb34c60Sahrens * zc_cookie zap cursor 20153cb34c60Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 20163cb34c60Sahrens * 20173cb34c60Sahrens * outputs: 20183cb34c60Sahrens * zc_name name of next snapshot 20193cb34c60Sahrens * zc_objset_stats stats 20203cb34c60Sahrens * zc_nvlist_dst property nvlist 20213cb34c60Sahrens * zc_nvlist_dst_size size of property nvlist 20223cb34c60Sahrens */ 2023fa9e4066Sahrens static int 2024fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 2025fa9e4066Sahrens { 202687e5029aSahrens objset_t *os; 2027fa9e4066Sahrens int error; 2028fa9e4066Sahrens 2029620252bcSChris Kirby top: 20307cbf8b43SRich Morris if (zc->zc_cookie == 0) 20317cbf8b43SRich Morris (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 20327cbf8b43SRich Morris NULL, DS_FIND_SNAPSHOTS); 20337cbf8b43SRich Morris 2034503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os); 2035745cd3c5Smaybee if (error) 2036745cd3c5Smaybee return (error == ENOENT ? ESRCH : error); 2037fa9e4066Sahrens 2038b81d61a6Slling /* 2039b81d61a6Slling * A dataset name of maximum length cannot have any snapshots, 2040b81d61a6Slling * so exit immediately. 2041b81d61a6Slling */ 2042b81d61a6Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 2043503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2044b81d61a6Slling return (ESRCH); 2045fa9e4066Sahrens } 2046fa9e4066Sahrens 204787e5029aSahrens error = dmu_snapshot_list_next(os, 204887e5029aSahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 2049a7f53a56SChris Kirby zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie, 2050a7f53a56SChris Kirby NULL); 2051a7f53a56SChris Kirby 2052620252bcSChris Kirby if (error == 0) { 2053a7f53a56SChris Kirby dsl_dataset_t *ds; 2054a7f53a56SChris Kirby dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool; 2055a7f53a56SChris Kirby 2056a7f53a56SChris Kirby /* 2057a7f53a56SChris Kirby * Since we probably don't have a hold on this snapshot, 2058a7f53a56SChris Kirby * it's possible that the objsetid could have been destroyed 2059a7f53a56SChris Kirby * and reused for a new objset. It's OK if this happens during 2060a7f53a56SChris Kirby * a zfs send operation, since the new createtxg will be 2061a7f53a56SChris Kirby * beyond the range we're interested in. 2062a7f53a56SChris Kirby */ 2063a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 2064a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds); 2065a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 2066a7f53a56SChris Kirby if (error) { 2067a7f53a56SChris Kirby if (error == ENOENT) { 2068a7f53a56SChris Kirby /* Racing with destroy, get the next one. */ 2069a7f53a56SChris Kirby *strchr(zc->zc_name, '@') = '\0'; 2070a7f53a56SChris Kirby dmu_objset_rele(os, FTAG); 2071a7f53a56SChris Kirby goto top; 2072a7f53a56SChris Kirby } 2073a7f53a56SChris Kirby } else { 2074a7f53a56SChris Kirby objset_t *ossnap; 2075a7f53a56SChris Kirby 2076a7f53a56SChris Kirby error = dmu_objset_from_ds(ds, &ossnap); 2077a7f53a56SChris Kirby if (error == 0) 2078a7f53a56SChris Kirby error = zfs_ioc_objset_stats_impl(zc, ossnap); 2079a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 2080620252bcSChris Kirby } 2081620252bcSChris Kirby } else if (error == ENOENT) { 2082745cd3c5Smaybee error = ESRCH; 2083620252bcSChris Kirby } 2084fa9e4066Sahrens 2085a7f53a56SChris Kirby dmu_objset_rele(os, FTAG); 20863cb34c60Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 2087745cd3c5Smaybee if (error) 20883cb34c60Sahrens *strchr(zc->zc_name, '@') = '\0'; 2089fa9e4066Sahrens return (error); 2090fa9e4066Sahrens } 2091fa9e4066Sahrens 209292241e0bSTom Erickson static int 209392241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 2094fa9e4066Sahrens { 209592241e0bSTom Erickson const char *propname = nvpair_name(pair); 209692241e0bSTom Erickson uint64_t *valary; 209792241e0bSTom Erickson unsigned int vallen; 209892241e0bSTom Erickson const char *domain; 2099eeb85002STim Haley char *dash; 210092241e0bSTom Erickson zfs_userquota_prop_t type; 210192241e0bSTom Erickson uint64_t rid; 210292241e0bSTom Erickson uint64_t quota; 210392241e0bSTom Erickson zfsvfs_t *zfsvfs; 210492241e0bSTom Erickson int err; 210592241e0bSTom Erickson 210692241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 210792241e0bSTom Erickson nvlist_t *attrs; 210892241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2109eeb85002STim Haley if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2110eeb85002STim Haley &pair) != 0) 2111eeb85002STim Haley return (EINVAL); 211292241e0bSTom Erickson } 2113e9dbad6fSeschrock 2114ecd6cf80Smarks /* 2115eeb85002STim Haley * A correctly constructed propname is encoded as 211692241e0bSTom Erickson * userquota@<rid>-<domain>. 2117ecd6cf80Smarks */ 2118eeb85002STim Haley if ((dash = strchr(propname, '-')) == NULL || 2119eeb85002STim Haley nvpair_value_uint64_array(pair, &valary, &vallen) != 0 || 2120eeb85002STim Haley vallen != 3) 2121eeb85002STim Haley return (EINVAL); 2122eeb85002STim Haley 2123eeb85002STim Haley domain = dash + 1; 2124eeb85002STim Haley type = valary[0]; 2125eeb85002STim Haley rid = valary[1]; 2126eeb85002STim Haley quota = valary[2]; 2127e9dbad6fSeschrock 21281412a1a2SMark Shellenbaum err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE); 212992241e0bSTom Erickson if (err == 0) { 213092241e0bSTom Erickson err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 213192241e0bSTom Erickson zfsvfs_rele(zfsvfs, FTAG); 213292241e0bSTom Erickson } 2133e9dbad6fSeschrock 213492241e0bSTom Erickson return (err); 213592241e0bSTom Erickson } 213614843421SMatthew Ahrens 213792241e0bSTom Erickson /* 213892241e0bSTom Erickson * If the named property is one that has a special function to set its value, 213992241e0bSTom Erickson * return 0 on success and a positive error code on failure; otherwise if it is 214092241e0bSTom Erickson * not one of the special properties handled by this function, return -1. 214192241e0bSTom Erickson * 2142eeb85002STim Haley * XXX: It would be better for callers of the property interface if we handled 214392241e0bSTom Erickson * these special cases in dsl_prop.c (in the dsl layer). 214492241e0bSTom Erickson */ 214592241e0bSTom Erickson static int 214692241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source, 214792241e0bSTom Erickson nvpair_t *pair) 214892241e0bSTom Erickson { 214992241e0bSTom Erickson const char *propname = nvpair_name(pair); 215092241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 215192241e0bSTom Erickson uint64_t intval; 215292241e0bSTom Erickson int err; 2153fa9e4066Sahrens 215492241e0bSTom Erickson if (prop == ZPROP_INVAL) { 215592241e0bSTom Erickson if (zfs_prop_userquota(propname)) 215692241e0bSTom Erickson return (zfs_prop_set_userquota(dsname, pair)); 215792241e0bSTom Erickson return (-1); 215892241e0bSTom Erickson } 215914843421SMatthew Ahrens 216092241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 216192241e0bSTom Erickson nvlist_t *attrs; 216292241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 216392241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 216492241e0bSTom Erickson &pair) == 0); 216592241e0bSTom Erickson } 2166db870a07Sahrens 216792241e0bSTom Erickson if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) 216892241e0bSTom Erickson return (-1); 2169b24ab676SJeff Bonwick 217092241e0bSTom Erickson VERIFY(0 == nvpair_value_uint64(pair, &intval)); 217140feaa91Sahrens 217292241e0bSTom Erickson switch (prop) { 217392241e0bSTom Erickson case ZFS_PROP_QUOTA: 217492241e0bSTom Erickson err = dsl_dir_set_quota(dsname, source, intval); 217592241e0bSTom Erickson break; 217692241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 217792241e0bSTom Erickson err = dsl_dataset_set_quota(dsname, source, intval); 217892241e0bSTom Erickson break; 217992241e0bSTom Erickson case ZFS_PROP_RESERVATION: 218092241e0bSTom Erickson err = dsl_dir_set_reservation(dsname, source, intval); 218192241e0bSTom Erickson break; 218292241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 218392241e0bSTom Erickson err = dsl_dataset_set_reservation(dsname, source, intval); 218492241e0bSTom Erickson break; 218592241e0bSTom Erickson case ZFS_PROP_VOLSIZE: 218692241e0bSTom Erickson err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip), 218792241e0bSTom Erickson intval); 218892241e0bSTom Erickson break; 218992241e0bSTom Erickson case ZFS_PROP_VERSION: 219092241e0bSTom Erickson { 219192241e0bSTom Erickson zfsvfs_t *zfsvfs; 21929e6eda55Smarks 21931412a1a2SMark Shellenbaum if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0) 2194b24ab676SJeff Bonwick break; 2195b24ab676SJeff Bonwick 219692241e0bSTom Erickson err = zfs_set_version(zfsvfs, intval); 219792241e0bSTom Erickson zfsvfs_rele(zfsvfs, FTAG); 2198d0f3f37eSMark Shellenbaum 219992241e0bSTom Erickson if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 2200b16da2e2SGeorge Wilson zfs_cmd_t *zc; 2201b16da2e2SGeorge Wilson 2202b16da2e2SGeorge Wilson zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2203b16da2e2SGeorge Wilson (void) strcpy(zc->zc_name, dsname); 2204b16da2e2SGeorge Wilson (void) zfs_ioc_userspace_upgrade(zc); 2205b16da2e2SGeorge Wilson kmem_free(zc, sizeof (zfs_cmd_t)); 220640feaa91Sahrens } 220792241e0bSTom Erickson break; 2208ecd6cf80Smarks } 2209ecd6cf80Smarks 221092241e0bSTom Erickson default: 221192241e0bSTom Erickson err = -1; 221292241e0bSTom Erickson } 2213e9dbad6fSeschrock 221492241e0bSTom Erickson return (err); 221592241e0bSTom Erickson } 2216a9799022Sck 221792241e0bSTom Erickson /* 221892241e0bSTom Erickson * This function is best effort. If it fails to set any of the given properties, 221992241e0bSTom Erickson * it continues to set as many as it can and returns the first error 222092241e0bSTom Erickson * encountered. If the caller provides a non-NULL errlist, it also gives the 222192241e0bSTom Erickson * complete list of names of all the properties it failed to set along with the 222292241e0bSTom Erickson * corresponding error numbers. The caller is responsible for freeing the 222392241e0bSTom Erickson * returned errlist. 222492241e0bSTom Erickson * 222592241e0bSTom Erickson * If every property is set successfully, zero is returned and the list pointed 222692241e0bSTom Erickson * at by errlist is NULL. 222792241e0bSTom Erickson */ 222892241e0bSTom Erickson int 222992241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 223092241e0bSTom Erickson nvlist_t **errlist) 223192241e0bSTom Erickson { 223292241e0bSTom Erickson nvpair_t *pair; 223392241e0bSTom Erickson nvpair_t *propval; 223402e383d1STom Erickson int rv = 0; 223592241e0bSTom Erickson uint64_t intval; 223692241e0bSTom Erickson char *strval; 223792241e0bSTom Erickson nvlist_t *genericnvl; 223892241e0bSTom Erickson nvlist_t *errors; 223992241e0bSTom Erickson nvlist_t *retrynvl; 2240e9dbad6fSeschrock 224192241e0bSTom Erickson VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 224292241e0bSTom Erickson VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 224392241e0bSTom Erickson VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2244a9799022Sck 224592241e0bSTom Erickson retry: 224692241e0bSTom Erickson pair = NULL; 224792241e0bSTom Erickson while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 224892241e0bSTom Erickson const char *propname = nvpair_name(pair); 224992241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 2250cfa69fd2STom Erickson int err = 0; 2251e9dbad6fSeschrock 225292241e0bSTom Erickson /* decode the property value */ 225392241e0bSTom Erickson propval = pair; 225492241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 225592241e0bSTom Erickson nvlist_t *attrs; 225692241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2257eeb85002STim Haley if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2258eeb85002STim Haley &propval) != 0) 2259eeb85002STim Haley err = EINVAL; 226014843421SMatthew Ahrens } 2261e9dbad6fSeschrock 226292241e0bSTom Erickson /* Validate value type */ 2263eeb85002STim Haley if (err == 0 && prop == ZPROP_INVAL) { 226492241e0bSTom Erickson if (zfs_prop_user(propname)) { 226592241e0bSTom Erickson if (nvpair_type(propval) != DATA_TYPE_STRING) 226692241e0bSTom Erickson err = EINVAL; 226792241e0bSTom Erickson } else if (zfs_prop_userquota(propname)) { 226892241e0bSTom Erickson if (nvpair_type(propval) != 226992241e0bSTom Erickson DATA_TYPE_UINT64_ARRAY) 227092241e0bSTom Erickson err = EINVAL; 227119b94df9SMatthew Ahrens } else { 227219b94df9SMatthew Ahrens err = EINVAL; 22734201a95eSRic Aleshire } 2274eeb85002STim Haley } else if (err == 0) { 227592241e0bSTom Erickson if (nvpair_type(propval) == DATA_TYPE_STRING) { 227692241e0bSTom Erickson if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 227792241e0bSTom Erickson err = EINVAL; 227892241e0bSTom Erickson } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 2279a2eea2e1Sahrens const char *unused; 2280a2eea2e1Sahrens 228192241e0bSTom Erickson VERIFY(nvpair_value_uint64(propval, 228292241e0bSTom Erickson &intval) == 0); 2283e9dbad6fSeschrock 2284e9dbad6fSeschrock switch (zfs_prop_get_type(prop)) { 228591ebeef5Sahrens case PROP_TYPE_NUMBER: 2286e9dbad6fSeschrock break; 228791ebeef5Sahrens case PROP_TYPE_STRING: 228892241e0bSTom Erickson err = EINVAL; 228992241e0bSTom Erickson break; 229091ebeef5Sahrens case PROP_TYPE_INDEX: 2291acd76fe5Seschrock if (zfs_prop_index_to_string(prop, 229292241e0bSTom Erickson intval, &unused) != 0) 229392241e0bSTom Erickson err = EINVAL; 2294e9dbad6fSeschrock break; 2295e9dbad6fSeschrock default: 2296e7437265Sahrens cmn_err(CE_PANIC, 2297e7437265Sahrens "unknown property type"); 2298e9dbad6fSeschrock } 2299e9dbad6fSeschrock } else { 230092241e0bSTom Erickson err = EINVAL; 2301e9dbad6fSeschrock } 2302e9dbad6fSeschrock } 230392241e0bSTom Erickson 230492241e0bSTom Erickson /* Validate permissions */ 230592241e0bSTom Erickson if (err == 0) 230692241e0bSTom Erickson err = zfs_check_settable(dsname, pair, CRED()); 230792241e0bSTom Erickson 230892241e0bSTom Erickson if (err == 0) { 230992241e0bSTom Erickson err = zfs_prop_set_special(dsname, source, pair); 231092241e0bSTom Erickson if (err == -1) { 231192241e0bSTom Erickson /* 231292241e0bSTom Erickson * For better performance we build up a list of 231392241e0bSTom Erickson * properties to set in a single transaction. 231492241e0bSTom Erickson */ 231592241e0bSTom Erickson err = nvlist_add_nvpair(genericnvl, pair); 231692241e0bSTom Erickson } else if (err != 0 && nvl != retrynvl) { 231792241e0bSTom Erickson /* 231892241e0bSTom Erickson * This may be a spurious error caused by 231992241e0bSTom Erickson * receiving quota and reservation out of order. 232092241e0bSTom Erickson * Try again in a second pass. 232192241e0bSTom Erickson */ 232292241e0bSTom Erickson err = nvlist_add_nvpair(retrynvl, pair); 232392241e0bSTom Erickson } 232492241e0bSTom Erickson } 232592241e0bSTom Erickson 232692241e0bSTom Erickson if (err != 0) 232792241e0bSTom Erickson VERIFY(nvlist_add_int32(errors, propname, err) == 0); 2328e9dbad6fSeschrock } 2329e9dbad6fSeschrock 233092241e0bSTom Erickson if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 233192241e0bSTom Erickson nvl = retrynvl; 233292241e0bSTom Erickson goto retry; 233392241e0bSTom Erickson } 233492241e0bSTom Erickson 233592241e0bSTom Erickson if (!nvlist_empty(genericnvl) && 233692241e0bSTom Erickson dsl_props_set(dsname, source, genericnvl) != 0) { 233792241e0bSTom Erickson /* 233892241e0bSTom Erickson * If this fails, we still want to set as many properties as we 233992241e0bSTom Erickson * can, so try setting them individually. 234092241e0bSTom Erickson */ 234192241e0bSTom Erickson pair = NULL; 234292241e0bSTom Erickson while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 234392241e0bSTom Erickson const char *propname = nvpair_name(pair); 2344cfa69fd2STom Erickson int err = 0; 234592241e0bSTom Erickson 234692241e0bSTom Erickson propval = pair; 234792241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 234892241e0bSTom Erickson nvlist_t *attrs; 234992241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 235092241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 235192241e0bSTom Erickson &propval) == 0); 235292241e0bSTom Erickson } 235392241e0bSTom Erickson 235492241e0bSTom Erickson if (nvpair_type(propval) == DATA_TYPE_STRING) { 235592241e0bSTom Erickson VERIFY(nvpair_value_string(propval, 235692241e0bSTom Erickson &strval) == 0); 235792241e0bSTom Erickson err = dsl_prop_set(dsname, propname, source, 1, 235892241e0bSTom Erickson strlen(strval) + 1, strval); 235992241e0bSTom Erickson } else { 236092241e0bSTom Erickson VERIFY(nvpair_value_uint64(propval, 236192241e0bSTom Erickson &intval) == 0); 236292241e0bSTom Erickson err = dsl_prop_set(dsname, propname, source, 8, 236392241e0bSTom Erickson 1, &intval); 236492241e0bSTom Erickson } 236592241e0bSTom Erickson 236692241e0bSTom Erickson if (err != 0) { 236792241e0bSTom Erickson VERIFY(nvlist_add_int32(errors, propname, 236892241e0bSTom Erickson err) == 0); 236992241e0bSTom Erickson } 237092241e0bSTom Erickson } 23715c0b6a79SRich Morris } 23725c0b6a79SRich Morris nvlist_free(genericnvl); 237392241e0bSTom Erickson nvlist_free(retrynvl); 237492241e0bSTom Erickson 237592241e0bSTom Erickson if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 237692241e0bSTom Erickson nvlist_free(errors); 237792241e0bSTom Erickson errors = NULL; 237892241e0bSTom Erickson } else { 237992241e0bSTom Erickson VERIFY(nvpair_value_int32(pair, &rv) == 0); 238092241e0bSTom Erickson } 238192241e0bSTom Erickson 238292241e0bSTom Erickson if (errlist == NULL) 238392241e0bSTom Erickson nvlist_free(errors); 238492241e0bSTom Erickson else 238592241e0bSTom Erickson *errlist = errors; 238692241e0bSTom Erickson 238792241e0bSTom Erickson return (rv); 2388fa9e4066Sahrens } 2389fa9e4066Sahrens 2390ea2f5b9eSMatthew Ahrens /* 2391ea2f5b9eSMatthew Ahrens * Check that all the properties are valid user properties. 2392ea2f5b9eSMatthew Ahrens */ 2393ea2f5b9eSMatthew Ahrens static int 2394ea2f5b9eSMatthew Ahrens zfs_check_userprops(char *fsname, nvlist_t *nvl) 2395ea2f5b9eSMatthew Ahrens { 239692241e0bSTom Erickson nvpair_t *pair = NULL; 2397ea2f5b9eSMatthew Ahrens int error = 0; 2398ea2f5b9eSMatthew Ahrens 239992241e0bSTom Erickson while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 240092241e0bSTom Erickson const char *propname = nvpair_name(pair); 2401ea2f5b9eSMatthew Ahrens char *valstr; 2402ea2f5b9eSMatthew Ahrens 2403ea2f5b9eSMatthew Ahrens if (!zfs_prop_user(propname) || 240492241e0bSTom Erickson nvpair_type(pair) != DATA_TYPE_STRING) 2405ea2f5b9eSMatthew Ahrens return (EINVAL); 2406ea2f5b9eSMatthew Ahrens 2407ea2f5b9eSMatthew Ahrens if (error = zfs_secpolicy_write_perms(fsname, 2408ea2f5b9eSMatthew Ahrens ZFS_DELEG_PERM_USERPROP, CRED())) 2409ea2f5b9eSMatthew Ahrens return (error); 2410ea2f5b9eSMatthew Ahrens 2411ea2f5b9eSMatthew Ahrens if (strlen(propname) >= ZAP_MAXNAMELEN) 2412ea2f5b9eSMatthew Ahrens return (ENAMETOOLONG); 2413ea2f5b9eSMatthew Ahrens 241492241e0bSTom Erickson VERIFY(nvpair_value_string(pair, &valstr) == 0); 2415ea2f5b9eSMatthew Ahrens if (strlen(valstr) >= ZAP_MAXVALUELEN) 2416ea2f5b9eSMatthew Ahrens return (E2BIG); 2417ea2f5b9eSMatthew Ahrens } 2418ea2f5b9eSMatthew Ahrens return (0); 2419ea2f5b9eSMatthew Ahrens } 2420ea2f5b9eSMatthew Ahrens 242192241e0bSTom Erickson static void 242292241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 242392241e0bSTom Erickson { 242492241e0bSTom Erickson nvpair_t *pair; 242592241e0bSTom Erickson 242692241e0bSTom Erickson VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 242792241e0bSTom Erickson 242892241e0bSTom Erickson pair = NULL; 242992241e0bSTom Erickson while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 243092241e0bSTom Erickson if (nvlist_exists(skipped, nvpair_name(pair))) 243192241e0bSTom Erickson continue; 243292241e0bSTom Erickson 243392241e0bSTom Erickson VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 243492241e0bSTom Erickson } 243592241e0bSTom Erickson } 243692241e0bSTom Erickson 243792241e0bSTom Erickson static int 243892241e0bSTom Erickson clear_received_props(objset_t *os, const char *fs, nvlist_t *props, 243992241e0bSTom Erickson nvlist_t *skipped) 244092241e0bSTom Erickson { 244192241e0bSTom Erickson int err = 0; 244292241e0bSTom Erickson nvlist_t *cleared_props = NULL; 244392241e0bSTom Erickson props_skip(props, skipped, &cleared_props); 244492241e0bSTom Erickson if (!nvlist_empty(cleared_props)) { 244592241e0bSTom Erickson /* 244692241e0bSTom Erickson * Acts on local properties until the dataset has received 244792241e0bSTom Erickson * properties at least once on or after SPA_VERSION_RECVD_PROPS. 244892241e0bSTom Erickson */ 244992241e0bSTom Erickson zprop_source_t flags = (ZPROP_SRC_NONE | 245092241e0bSTom Erickson (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0)); 245192241e0bSTom Erickson err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL); 245292241e0bSTom Erickson } 245392241e0bSTom Erickson nvlist_free(cleared_props); 245492241e0bSTom Erickson return (err); 245592241e0bSTom Erickson } 245692241e0bSTom Erickson 24573cb34c60Sahrens /* 24583cb34c60Sahrens * inputs: 24593cb34c60Sahrens * zc_name name of filesystem 24605c0b6a79SRich Morris * zc_value name of property to set 24613cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 246292241e0bSTom Erickson * zc_cookie received properties flag 24633cb34c60Sahrens * 246492241e0bSTom Erickson * outputs: 246592241e0bSTom Erickson * zc_nvlist_dst{_size} error for each unapplied received property 24663cb34c60Sahrens */ 2467fa9e4066Sahrens static int 2468e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 2469fa9e4066Sahrens { 2470e9dbad6fSeschrock nvlist_t *nvl; 247192241e0bSTom Erickson boolean_t received = zc->zc_cookie; 247292241e0bSTom Erickson zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 247392241e0bSTom Erickson ZPROP_SRC_LOCAL); 247492241e0bSTom Erickson nvlist_t *errors = NULL; 2475e9dbad6fSeschrock int error; 2476e9dbad6fSeschrock 2477990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2478478ed9adSEric Taylor zc->zc_iflags, &nvl)) != 0) 2479e9dbad6fSeschrock return (error); 2480e9dbad6fSeschrock 248192241e0bSTom Erickson if (received) { 2482bb0ade09Sahrens nvlist_t *origprops; 2483bb0ade09Sahrens objset_t *os; 2484bb0ade09Sahrens 2485503ad85cSMatthew Ahrens if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) { 248692241e0bSTom Erickson if (dsl_prop_get_received(os, &origprops) == 0) { 248792241e0bSTom Erickson (void) clear_received_props(os, 248892241e0bSTom Erickson zc->zc_name, origprops, nvl); 2489bb0ade09Sahrens nvlist_free(origprops); 2490bb0ade09Sahrens } 249192241e0bSTom Erickson 249292241e0bSTom Erickson dsl_prop_set_hasrecvd(os); 2493503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2494bb0ade09Sahrens } 2495bb0ade09Sahrens } 2496bb0ade09Sahrens 249792241e0bSTom Erickson error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors); 249892241e0bSTom Erickson 249992241e0bSTom Erickson if (zc->zc_nvlist_dst != NULL && errors != NULL) { 250092241e0bSTom Erickson (void) put_nvlist(zc, errors); 250192241e0bSTom Erickson } 2502ecd6cf80Smarks 250392241e0bSTom Erickson nvlist_free(errors); 2504e9dbad6fSeschrock nvlist_free(nvl); 2505e9dbad6fSeschrock return (error); 2506fa9e4066Sahrens } 2507fa9e4066Sahrens 25083cb34c60Sahrens /* 25093cb34c60Sahrens * inputs: 25103cb34c60Sahrens * zc_name name of filesystem 25113cb34c60Sahrens * zc_value name of property to inherit 251292241e0bSTom Erickson * zc_cookie revert to received value if TRUE 25133cb34c60Sahrens * 25143cb34c60Sahrens * outputs: none 25153cb34c60Sahrens */ 2516e45ce728Sahrens static int 2517e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 2518e45ce728Sahrens { 251992241e0bSTom Erickson const char *propname = zc->zc_value; 252092241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 252192241e0bSTom Erickson boolean_t received = zc->zc_cookie; 252292241e0bSTom Erickson zprop_source_t source = (received 252392241e0bSTom Erickson ? ZPROP_SRC_NONE /* revert to received value, if any */ 252492241e0bSTom Erickson : ZPROP_SRC_INHERITED); /* explicitly inherit */ 252592241e0bSTom Erickson 252692241e0bSTom Erickson if (received) { 252792241e0bSTom Erickson nvlist_t *dummy; 252892241e0bSTom Erickson nvpair_t *pair; 252992241e0bSTom Erickson zprop_type_t type; 253092241e0bSTom Erickson int err; 253192241e0bSTom Erickson 253292241e0bSTom Erickson /* 253392241e0bSTom Erickson * zfs_prop_set_special() expects properties in the form of an 253492241e0bSTom Erickson * nvpair with type info. 253592241e0bSTom Erickson */ 253692241e0bSTom Erickson if (prop == ZPROP_INVAL) { 253792241e0bSTom Erickson if (!zfs_prop_user(propname)) 253892241e0bSTom Erickson return (EINVAL); 253992241e0bSTom Erickson 254092241e0bSTom Erickson type = PROP_TYPE_STRING; 2541a79992aaSTom Erickson } else if (prop == ZFS_PROP_VOLSIZE || 2542a79992aaSTom Erickson prop == ZFS_PROP_VERSION) { 2543a79992aaSTom Erickson return (EINVAL); 254492241e0bSTom Erickson } else { 254592241e0bSTom Erickson type = zfs_prop_get_type(prop); 254692241e0bSTom Erickson } 254792241e0bSTom Erickson 254892241e0bSTom Erickson VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0); 254992241e0bSTom Erickson 255092241e0bSTom Erickson switch (type) { 255192241e0bSTom Erickson case PROP_TYPE_STRING: 255292241e0bSTom Erickson VERIFY(0 == nvlist_add_string(dummy, propname, "")); 255392241e0bSTom Erickson break; 255492241e0bSTom Erickson case PROP_TYPE_NUMBER: 255592241e0bSTom Erickson case PROP_TYPE_INDEX: 255692241e0bSTom Erickson VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 255792241e0bSTom Erickson break; 255892241e0bSTom Erickson default: 255992241e0bSTom Erickson nvlist_free(dummy); 256092241e0bSTom Erickson return (EINVAL); 256192241e0bSTom Erickson } 256292241e0bSTom Erickson 256392241e0bSTom Erickson pair = nvlist_next_nvpair(dummy, NULL); 256492241e0bSTom Erickson err = zfs_prop_set_special(zc->zc_name, source, pair); 256592241e0bSTom Erickson nvlist_free(dummy); 256692241e0bSTom Erickson if (err != -1) 256792241e0bSTom Erickson return (err); /* special property already handled */ 256892241e0bSTom Erickson } else { 256992241e0bSTom Erickson /* 257092241e0bSTom Erickson * Only check this in the non-received case. We want to allow 257192241e0bSTom Erickson * 'inherit -S' to revert non-inheritable properties like quota 257292241e0bSTom Erickson * and reservation to the received or default values even though 257392241e0bSTom Erickson * they are not considered inheritable. 257492241e0bSTom Erickson */ 257592241e0bSTom Erickson if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 257692241e0bSTom Erickson return (EINVAL); 257792241e0bSTom Erickson } 257892241e0bSTom Erickson 2579e45ce728Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 258092241e0bSTom Erickson return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL)); 2581e45ce728Sahrens } 2582e45ce728Sahrens 2583b1b8ab34Slling static int 258411a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 2585b1b8ab34Slling { 2586990b4856Slling nvlist_t *props; 2587b1b8ab34Slling spa_t *spa; 2588990b4856Slling int error; 258992241e0bSTom Erickson nvpair_t *pair; 2590b1b8ab34Slling 259192241e0bSTom Erickson if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 259292241e0bSTom Erickson zc->zc_iflags, &props)) 2593b1b8ab34Slling return (error); 2594b1b8ab34Slling 2595379c004dSEric Schrock /* 2596379c004dSEric Schrock * If the only property is the configfile, then just do a spa_lookup() 2597379c004dSEric Schrock * to handle the faulted case. 2598379c004dSEric Schrock */ 259992241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL); 260092241e0bSTom Erickson if (pair != NULL && strcmp(nvpair_name(pair), 2601379c004dSEric Schrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 260292241e0bSTom Erickson nvlist_next_nvpair(props, pair) == NULL) { 2603379c004dSEric Schrock mutex_enter(&spa_namespace_lock); 2604379c004dSEric Schrock if ((spa = spa_lookup(zc->zc_name)) != NULL) { 2605379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 2606379c004dSEric Schrock spa_config_sync(spa, B_FALSE, B_TRUE); 2607379c004dSEric Schrock } 2608379c004dSEric Schrock mutex_exit(&spa_namespace_lock); 2609b693757aSEric Schrock if (spa != NULL) { 2610b693757aSEric Schrock nvlist_free(props); 2611379c004dSEric Schrock return (0); 2612b693757aSEric Schrock } 2613379c004dSEric Schrock } 2614379c004dSEric Schrock 2615b1b8ab34Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2616990b4856Slling nvlist_free(props); 2617b1b8ab34Slling return (error); 2618b1b8ab34Slling } 2619b1b8ab34Slling 2620990b4856Slling error = spa_prop_set(spa, props); 2621b1b8ab34Slling 2622990b4856Slling nvlist_free(props); 2623b1b8ab34Slling spa_close(spa, FTAG); 2624b1b8ab34Slling 2625b1b8ab34Slling return (error); 2626b1b8ab34Slling } 2627b1b8ab34Slling 2628b1b8ab34Slling static int 262911a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 2630b1b8ab34Slling { 2631b1b8ab34Slling spa_t *spa; 2632b1b8ab34Slling int error; 2633b1b8ab34Slling nvlist_t *nvp = NULL; 2634b1b8ab34Slling 2635379c004dSEric Schrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2636379c004dSEric Schrock /* 2637379c004dSEric Schrock * If the pool is faulted, there may be properties we can still 2638379c004dSEric Schrock * get (such as altroot and cachefile), so attempt to get them 2639379c004dSEric Schrock * anyway. 2640379c004dSEric Schrock */ 2641379c004dSEric Schrock mutex_enter(&spa_namespace_lock); 2642379c004dSEric Schrock if ((spa = spa_lookup(zc->zc_name)) != NULL) 2643379c004dSEric Schrock error = spa_prop_get(spa, &nvp); 2644379c004dSEric Schrock mutex_exit(&spa_namespace_lock); 2645379c004dSEric Schrock } else { 2646379c004dSEric Schrock error = spa_prop_get(spa, &nvp); 2647379c004dSEric Schrock spa_close(spa, FTAG); 2648379c004dSEric Schrock } 2649b1b8ab34Slling 2650b1b8ab34Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 2651b1b8ab34Slling error = put_nvlist(zc, nvp); 2652b1b8ab34Slling else 2653b1b8ab34Slling error = EFAULT; 2654b1b8ab34Slling 2655379c004dSEric Schrock nvlist_free(nvp); 2656b1b8ab34Slling return (error); 2657b1b8ab34Slling } 2658b1b8ab34Slling 26593cb34c60Sahrens /* 26603cb34c60Sahrens * inputs: 26613cb34c60Sahrens * zc_name name of filesystem 26623cb34c60Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 26633cb34c60Sahrens * zc_perm_action allow/unallow flag 26643cb34c60Sahrens * 26653cb34c60Sahrens * outputs: none 26663cb34c60Sahrens */ 2667ecd6cf80Smarks static int 2668ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 2669ecd6cf80Smarks { 2670ecd6cf80Smarks int error; 2671ecd6cf80Smarks nvlist_t *fsaclnv = NULL; 2672ecd6cf80Smarks 2673990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2674478ed9adSEric Taylor zc->zc_iflags, &fsaclnv)) != 0) 2675ecd6cf80Smarks return (error); 2676ecd6cf80Smarks 2677ecd6cf80Smarks /* 2678ecd6cf80Smarks * Verify nvlist is constructed correctly 2679ecd6cf80Smarks */ 2680ecd6cf80Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 2681ecd6cf80Smarks nvlist_free(fsaclnv); 2682ecd6cf80Smarks return (EINVAL); 2683ecd6cf80Smarks } 2684ecd6cf80Smarks 2685ecd6cf80Smarks /* 2686ecd6cf80Smarks * If we don't have PRIV_SYS_MOUNT, then validate 2687ecd6cf80Smarks * that user is allowed to hand out each permission in 2688ecd6cf80Smarks * the nvlist(s) 2689ecd6cf80Smarks */ 2690ecd6cf80Smarks 269191ebeef5Sahrens error = secpolicy_zfs(CRED()); 2692ecd6cf80Smarks if (error) { 269391ebeef5Sahrens if (zc->zc_perm_action == B_FALSE) { 269491ebeef5Sahrens error = dsl_deleg_can_allow(zc->zc_name, 269591ebeef5Sahrens fsaclnv, CRED()); 269691ebeef5Sahrens } else { 269791ebeef5Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 269891ebeef5Sahrens fsaclnv, CRED()); 269991ebeef5Sahrens } 2700ecd6cf80Smarks } 2701ecd6cf80Smarks 2702ecd6cf80Smarks if (error == 0) 2703ecd6cf80Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 2704ecd6cf80Smarks 2705ecd6cf80Smarks nvlist_free(fsaclnv); 2706ecd6cf80Smarks return (error); 2707ecd6cf80Smarks } 2708ecd6cf80Smarks 27093cb34c60Sahrens /* 27103cb34c60Sahrens * inputs: 27113cb34c60Sahrens * zc_name name of filesystem 27123cb34c60Sahrens * 27133cb34c60Sahrens * outputs: 27143cb34c60Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 27153cb34c60Sahrens */ 2716ecd6cf80Smarks static int 2717ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 2718ecd6cf80Smarks { 2719ecd6cf80Smarks nvlist_t *nvp; 2720ecd6cf80Smarks int error; 2721ecd6cf80Smarks 2722ecd6cf80Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 2723ecd6cf80Smarks error = put_nvlist(zc, nvp); 2724ecd6cf80Smarks nvlist_free(nvp); 2725ecd6cf80Smarks } 2726ecd6cf80Smarks 2727ecd6cf80Smarks return (error); 2728ecd6cf80Smarks } 2729ecd6cf80Smarks 2730fa9e4066Sahrens /* 2731fa9e4066Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 2732fa9e4066Sahrens * or NULL if no suitable entry is found. The caller of this routine 2733fa9e4066Sahrens * is responsible for releasing the returned vfs pointer. 2734fa9e4066Sahrens */ 2735fa9e4066Sahrens static vfs_t * 2736fa9e4066Sahrens zfs_get_vfs(const char *resource) 2737fa9e4066Sahrens { 2738fa9e4066Sahrens struct vfs *vfsp; 2739fa9e4066Sahrens struct vfs *vfs_found = NULL; 2740fa9e4066Sahrens 2741fa9e4066Sahrens vfs_list_read_lock(); 2742fa9e4066Sahrens vfsp = rootvfs; 2743fa9e4066Sahrens do { 2744fa9e4066Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2745fa9e4066Sahrens VFS_HOLD(vfsp); 2746fa9e4066Sahrens vfs_found = vfsp; 2747fa9e4066Sahrens break; 2748fa9e4066Sahrens } 2749fa9e4066Sahrens vfsp = vfsp->vfs_next; 2750fa9e4066Sahrens } while (vfsp != rootvfs); 2751fa9e4066Sahrens vfs_list_unlock(); 2752fa9e4066Sahrens return (vfs_found); 2753fa9e4066Sahrens } 2754fa9e4066Sahrens 2755ecd6cf80Smarks /* ARGSUSED */ 2756fa9e4066Sahrens static void 2757ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2758fa9e4066Sahrens { 2759da6c28aaSamw zfs_creat_t *zct = arg; 2760da6c28aaSamw 2761de8267e0Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 2762da6c28aaSamw } 2763da6c28aaSamw 2764de8267e0Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 2765da6c28aaSamw 2766da6c28aaSamw /* 2767de8267e0Stimh * inputs: 27680a48a24eStimh * createprops list of properties requested by creator 27690a48a24eStimh * default_zplver zpl version to use if unspecified in createprops 27700a48a24eStimh * fuids_ok fuids allowed in this version of the spa? 27710a48a24eStimh * os parent objset pointer (NULL if root fs) 2772de8267e0Stimh * 2773de8267e0Stimh * outputs: 2774de8267e0Stimh * zplprops values for the zplprops we attach to the master node object 27750a48a24eStimh * is_ci true if requested file system will be purely case-insensitive 2776da6c28aaSamw * 2777de8267e0Stimh * Determine the settings for utf8only, normalization and 2778de8267e0Stimh * casesensitivity. Specific values may have been requested by the 2779de8267e0Stimh * creator and/or we can inherit values from the parent dataset. If 2780de8267e0Stimh * the file system is of too early a vintage, a creator can not 2781de8267e0Stimh * request settings for these properties, even if the requested 2782de8267e0Stimh * setting is the default value. We don't actually want to create dsl 2783de8267e0Stimh * properties for these, so remove them from the source nvlist after 2784de8267e0Stimh * processing. 2785da6c28aaSamw */ 2786da6c28aaSamw static int 278714843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 27880a586ceaSMark Shellenbaum boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops, 27890a586ceaSMark Shellenbaum nvlist_t *zplprops, boolean_t *is_ci) 2790da6c28aaSamw { 2791de8267e0Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 2792de8267e0Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 2793de8267e0Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 2794da6c28aaSamw 2795de8267e0Stimh ASSERT(zplprops != NULL); 2796da6c28aaSamw 2797de8267e0Stimh /* 2798de8267e0Stimh * Pull out creator prop choices, if any. 2799de8267e0Stimh */ 2800de8267e0Stimh if (createprops) { 28010a48a24eStimh (void) nvlist_lookup_uint64(createprops, 28020a48a24eStimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 2803de8267e0Stimh (void) nvlist_lookup_uint64(createprops, 2804de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 2805de8267e0Stimh (void) nvlist_remove_all(createprops, 2806de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 2807de8267e0Stimh (void) nvlist_lookup_uint64(createprops, 2808de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 2809de8267e0Stimh (void) nvlist_remove_all(createprops, 2810de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 2811de8267e0Stimh (void) nvlist_lookup_uint64(createprops, 2812de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 2813de8267e0Stimh (void) nvlist_remove_all(createprops, 2814de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 2815de8267e0Stimh } 2816da6c28aaSamw 2817c2a93d44Stimh /* 28180a48a24eStimh * If the zpl version requested is whacky or the file system 28190a48a24eStimh * or pool is version is too "young" to support normalization 28200a48a24eStimh * and the creator tried to set a value for one of the props, 28210a48a24eStimh * error out. 2822c2a93d44Stimh */ 28230a48a24eStimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 28240a48a24eStimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 28250a586ceaSMark Shellenbaum (zplver >= ZPL_VERSION_SA && !sa_ok) || 28260a48a24eStimh (zplver < ZPL_VERSION_NORMALIZATION && 2827de8267e0Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 28280a48a24eStimh sense != ZFS_PROP_UNDEFINED))) 2829de8267e0Stimh return (ENOTSUP); 2830c2a93d44Stimh 2831de8267e0Stimh /* 2832de8267e0Stimh * Put the version in the zplprops 2833de8267e0Stimh */ 2834de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 2835de8267e0Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 2836da6c28aaSamw 2837de8267e0Stimh if (norm == ZFS_PROP_UNDEFINED) 2838de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 2839de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 2840de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 2841da6c28aaSamw 2842c2a93d44Stimh /* 2843de8267e0Stimh * If we're normalizing, names must always be valid UTF-8 strings. 2844c2a93d44Stimh */ 2845de8267e0Stimh if (norm) 2846de8267e0Stimh u8 = 1; 2847de8267e0Stimh if (u8 == ZFS_PROP_UNDEFINED) 2848de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 2849de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 2850de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 2851de8267e0Stimh 2852de8267e0Stimh if (sense == ZFS_PROP_UNDEFINED) 2853de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 2854de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 2855de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 2856c2a93d44Stimh 2857ab04eb8eStimh if (is_ci) 2858ab04eb8eStimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 2859ab04eb8eStimh 2860da6c28aaSamw return (0); 2861fa9e4066Sahrens } 2862fa9e4066Sahrens 28630a48a24eStimh static int 28640a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 28650a48a24eStimh nvlist_t *zplprops, boolean_t *is_ci) 28660a48a24eStimh { 28670a586ceaSMark Shellenbaum boolean_t fuids_ok, sa_ok; 28680a48a24eStimh uint64_t zplver = ZPL_VERSION; 28690a48a24eStimh objset_t *os = NULL; 28700a48a24eStimh char parentname[MAXNAMELEN]; 28710a48a24eStimh char *cp; 28720a586ceaSMark Shellenbaum spa_t *spa; 28730a586ceaSMark Shellenbaum uint64_t spa_vers; 28740a48a24eStimh int error; 28750a48a24eStimh 28760a48a24eStimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 28770a48a24eStimh cp = strrchr(parentname, '/'); 28780a48a24eStimh ASSERT(cp != NULL); 28790a48a24eStimh cp[0] = '\0'; 28800a48a24eStimh 28810a586ceaSMark Shellenbaum if ((error = spa_open(dataset, &spa, FTAG)) != 0) 28820a586ceaSMark Shellenbaum return (error); 28830a586ceaSMark Shellenbaum 28840a586ceaSMark Shellenbaum spa_vers = spa_version(spa); 28850a586ceaSMark Shellenbaum spa_close(spa, FTAG); 28860a586ceaSMark Shellenbaum 28870a586ceaSMark Shellenbaum zplver = zfs_zpl_version_map(spa_vers); 28880a586ceaSMark Shellenbaum fuids_ok = (zplver >= ZPL_VERSION_FUID); 28890a586ceaSMark Shellenbaum sa_ok = (zplver >= ZPL_VERSION_SA); 28900a48a24eStimh 28910a48a24eStimh /* 28920a48a24eStimh * Open parent object set so we can inherit zplprop values. 28930a48a24eStimh */ 2894503ad85cSMatthew Ahrens if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 28950a48a24eStimh return (error); 28960a48a24eStimh 28970a586ceaSMark Shellenbaum error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops, 28980a48a24eStimh zplprops, is_ci); 2899503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 29000a48a24eStimh return (error); 29010a48a24eStimh } 29020a48a24eStimh 29030a48a24eStimh static int 29040a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 29050a48a24eStimh nvlist_t *zplprops, boolean_t *is_ci) 29060a48a24eStimh { 29070a586ceaSMark Shellenbaum boolean_t fuids_ok; 29080a586ceaSMark Shellenbaum boolean_t sa_ok; 29090a48a24eStimh uint64_t zplver = ZPL_VERSION; 29100a48a24eStimh int error; 29110a48a24eStimh 29120a586ceaSMark Shellenbaum zplver = zfs_zpl_version_map(spa_vers); 29130a586ceaSMark Shellenbaum fuids_ok = (zplver >= ZPL_VERSION_FUID); 29140a586ceaSMark Shellenbaum sa_ok = (zplver >= ZPL_VERSION_SA); 29150a48a24eStimh 29160a586ceaSMark Shellenbaum error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok, 29170a586ceaSMark Shellenbaum createprops, zplprops, is_ci); 29180a48a24eStimh return (error); 29190a48a24eStimh } 29200a48a24eStimh 29213cb34c60Sahrens /* 29223cb34c60Sahrens * inputs: 29233cb34c60Sahrens * zc_objset_type type of objset to create (fs vs zvol) 29243cb34c60Sahrens * zc_name name of new objset 29253cb34c60Sahrens * zc_value name of snapshot to clone from (may be empty) 29263cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 29273cb34c60Sahrens * 2928de8267e0Stimh * outputs: none 29293cb34c60Sahrens */ 2930fa9e4066Sahrens static int 2931fa9e4066Sahrens zfs_ioc_create(zfs_cmd_t *zc) 2932fa9e4066Sahrens { 2933fa9e4066Sahrens objset_t *clone; 2934fa9e4066Sahrens int error = 0; 2935da6c28aaSamw zfs_creat_t zct; 2936ecd6cf80Smarks nvlist_t *nvprops = NULL; 2937ecd6cf80Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2938fa9e4066Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2939fa9e4066Sahrens 2940fa9e4066Sahrens switch (type) { 2941fa9e4066Sahrens 2942fa9e4066Sahrens case DMU_OST_ZFS: 2943fa9e4066Sahrens cbfunc = zfs_create_cb; 2944fa9e4066Sahrens break; 2945fa9e4066Sahrens 2946fa9e4066Sahrens case DMU_OST_ZVOL: 2947fa9e4066Sahrens cbfunc = zvol_create_cb; 2948fa9e4066Sahrens break; 2949fa9e4066Sahrens 2950fa9e4066Sahrens default: 29511d452cf5Sahrens cbfunc = NULL; 2952e7cbe64fSgw break; 2953fa9e4066Sahrens } 2954f18faf3fSek if (strchr(zc->zc_name, '@') || 2955f18faf3fSek strchr(zc->zc_name, '%')) 29561d452cf5Sahrens return (EINVAL); 2957fa9e4066Sahrens 2958e9dbad6fSeschrock if (zc->zc_nvlist_src != NULL && 2959990b4856Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2960478ed9adSEric Taylor zc->zc_iflags, &nvprops)) != 0) 2961e9dbad6fSeschrock return (error); 2962e9dbad6fSeschrock 2963de8267e0Stimh zct.zct_zplprops = NULL; 2964da6c28aaSamw zct.zct_props = nvprops; 2965da6c28aaSamw 2966e9dbad6fSeschrock if (zc->zc_value[0] != '\0') { 2967fa9e4066Sahrens /* 2968fa9e4066Sahrens * We're creating a clone of an existing snapshot. 2969fa9e4066Sahrens */ 2970e9dbad6fSeschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 2971e9dbad6fSeschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 2972ecd6cf80Smarks nvlist_free(nvprops); 2973fa9e4066Sahrens return (EINVAL); 2974e9dbad6fSeschrock } 2975fa9e4066Sahrens 2976503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_value, FTAG, &clone); 2977e9dbad6fSeschrock if (error) { 2978ecd6cf80Smarks nvlist_free(nvprops); 2979fa9e4066Sahrens return (error); 2980e9dbad6fSeschrock } 2981ab04eb8eStimh 2982ae46e4c7SMatthew Ahrens error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0); 2983503ad85cSMatthew Ahrens dmu_objset_rele(clone, FTAG); 2984da6c28aaSamw if (error) { 2985da6c28aaSamw nvlist_free(nvprops); 2986da6c28aaSamw return (error); 2987da6c28aaSamw } 2988fa9e4066Sahrens } else { 2989ab04eb8eStimh boolean_t is_insensitive = B_FALSE; 2990ab04eb8eStimh 2991e9dbad6fSeschrock if (cbfunc == NULL) { 2992ecd6cf80Smarks nvlist_free(nvprops); 29931d452cf5Sahrens return (EINVAL); 2994e9dbad6fSeschrock } 29955c5460e9Seschrock 2996e9dbad6fSeschrock if (type == DMU_OST_ZVOL) { 2997e9dbad6fSeschrock uint64_t volsize, volblocksize; 2998e9dbad6fSeschrock 2999ecd6cf80Smarks if (nvprops == NULL || 3000ecd6cf80Smarks nvlist_lookup_uint64(nvprops, 3001e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 3002e9dbad6fSeschrock &volsize) != 0) { 3003ecd6cf80Smarks nvlist_free(nvprops); 3004e9dbad6fSeschrock return (EINVAL); 3005e9dbad6fSeschrock } 3006e9dbad6fSeschrock 3007ecd6cf80Smarks if ((error = nvlist_lookup_uint64(nvprops, 3008e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3009e9dbad6fSeschrock &volblocksize)) != 0 && error != ENOENT) { 3010ecd6cf80Smarks nvlist_free(nvprops); 3011e9dbad6fSeschrock return (EINVAL); 3012e9dbad6fSeschrock } 3013e9dbad6fSeschrock 3014e9dbad6fSeschrock if (error != 0) 3015e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric( 3016e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 3017e9dbad6fSeschrock 3018e9dbad6fSeschrock if ((error = zvol_check_volblocksize( 3019e9dbad6fSeschrock volblocksize)) != 0 || 3020e9dbad6fSeschrock (error = zvol_check_volsize(volsize, 3021e9dbad6fSeschrock volblocksize)) != 0) { 3022ecd6cf80Smarks nvlist_free(nvprops); 30235c5460e9Seschrock return (error); 3024e9dbad6fSeschrock } 3025e7437265Sahrens } else if (type == DMU_OST_ZFS) { 3026da6c28aaSamw int error; 3027da6c28aaSamw 3028da6c28aaSamw /* 3029da6c28aaSamw * We have to have normalization and 3030da6c28aaSamw * case-folding flags correct when we do the 3031da6c28aaSamw * file system creation, so go figure them out 3032de8267e0Stimh * now. 3033da6c28aaSamw */ 3034de8267e0Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 3035de8267e0Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 3036de8267e0Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 30370a48a24eStimh zct.zct_zplprops, &is_insensitive); 3038da6c28aaSamw if (error != 0) { 3039da6c28aaSamw nvlist_free(nvprops); 3040de8267e0Stimh nvlist_free(zct.zct_zplprops); 3041da6c28aaSamw return (error); 3042da6c28aaSamw } 3043da6c28aaSamw } 3044ae46e4c7SMatthew Ahrens error = dmu_objset_create(zc->zc_name, type, 3045ab04eb8eStimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 3046de8267e0Stimh nvlist_free(zct.zct_zplprops); 3047fa9e4066Sahrens } 3048e9dbad6fSeschrock 3049e9dbad6fSeschrock /* 3050e9dbad6fSeschrock * It would be nice to do this atomically. 3051e9dbad6fSeschrock */ 3052e9dbad6fSeschrock if (error == 0) { 305392241e0bSTom Erickson error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL, 305492241e0bSTom Erickson nvprops, NULL); 305592241e0bSTom Erickson if (error != 0) 3056842727c2SChris Kirby (void) dmu_objset_destroy(zc->zc_name, B_FALSE); 3057e9dbad6fSeschrock } 3058ecd6cf80Smarks nvlist_free(nvprops); 3059fa9e4066Sahrens return (error); 3060fa9e4066Sahrens } 3061fa9e4066Sahrens 30623cb34c60Sahrens /* 30633cb34c60Sahrens * inputs: 30643cb34c60Sahrens * zc_name name of filesystem 30653cb34c60Sahrens * zc_value short name of snapshot 30663cb34c60Sahrens * zc_cookie recursive flag 306714843421SMatthew Ahrens * zc_nvlist_src[_size] property list 30683cb34c60Sahrens * 3069681d9761SEric Taylor * outputs: 3070681d9761SEric Taylor * zc_value short snapname (i.e. part after the '@') 30713cb34c60Sahrens */ 3072fa9e4066Sahrens static int 30731d452cf5Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 3074fa9e4066Sahrens { 3075bb0ade09Sahrens nvlist_t *nvprops = NULL; 3076bb0ade09Sahrens int error; 3077bb0ade09Sahrens boolean_t recursive = zc->zc_cookie; 3078bb0ade09Sahrens 3079e9dbad6fSeschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 30801d452cf5Sahrens return (EINVAL); 3081bb0ade09Sahrens 3082bb0ade09Sahrens if (zc->zc_nvlist_src != NULL && 3083bb0ade09Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 3084478ed9adSEric Taylor zc->zc_iflags, &nvprops)) != 0) 3085bb0ade09Sahrens return (error); 3086bb0ade09Sahrens 3087ea2f5b9eSMatthew Ahrens error = zfs_check_userprops(zc->zc_name, nvprops); 3088ea2f5b9eSMatthew Ahrens if (error) 3089ea2f5b9eSMatthew Ahrens goto out; 3090bb0ade09Sahrens 309192241e0bSTom Erickson if (!nvlist_empty(nvprops) && 3092ea2f5b9eSMatthew Ahrens zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) { 3093ea2f5b9eSMatthew Ahrens error = ENOTSUP; 3094ea2f5b9eSMatthew Ahrens goto out; 3095bb0ade09Sahrens } 3096ea2f5b9eSMatthew Ahrens 309799d5e173STim Haley error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL, 309899d5e173STim Haley nvprops, recursive, B_FALSE, -1); 3099ea2f5b9eSMatthew Ahrens 3100ea2f5b9eSMatthew Ahrens out: 3101bb0ade09Sahrens nvlist_free(nvprops); 3102bb0ade09Sahrens return (error); 31031d452cf5Sahrens } 3104fa9e4066Sahrens 3105cdf5b4caSmmusante int 3106fd136879SMatthew Ahrens zfs_unmount_snap(const char *name, void *arg) 31071d452cf5Sahrens { 31080b69c2f0Sahrens vfs_t *vfsp = NULL; 31091d452cf5Sahrens 3110745cd3c5Smaybee if (arg) { 3111745cd3c5Smaybee char *snapname = arg; 3112fd136879SMatthew Ahrens char *fullname = kmem_asprintf("%s@%s", name, snapname); 3113fd136879SMatthew Ahrens vfsp = zfs_get_vfs(fullname); 3114fd136879SMatthew Ahrens strfree(fullname); 31150b69c2f0Sahrens } else if (strchr(name, '@')) { 31161d452cf5Sahrens vfsp = zfs_get_vfs(name); 31171d452cf5Sahrens } 31181d452cf5Sahrens 31191d452cf5Sahrens if (vfsp) { 3120fa9e4066Sahrens /* 31211d452cf5Sahrens * Always force the unmount for snapshots. 3122fa9e4066Sahrens */ 31231d452cf5Sahrens int flag = MS_FORCE; 31241d452cf5Sahrens int err; 31251d452cf5Sahrens 31261d452cf5Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 3127fa9e4066Sahrens VFS_RELE(vfsp); 31281d452cf5Sahrens return (err); 3129fa9e4066Sahrens } 31301d452cf5Sahrens VFS_RELE(vfsp); 31311d452cf5Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 31321d452cf5Sahrens return (err); 31331d452cf5Sahrens } 31341d452cf5Sahrens return (0); 31351d452cf5Sahrens } 31361d452cf5Sahrens 31373cb34c60Sahrens /* 31383cb34c60Sahrens * inputs: 313919b94df9SMatthew Ahrens * zc_name name of filesystem, snaps must be under it 314019b94df9SMatthew Ahrens * zc_nvlist_src[_size] full names of snapshots to destroy 3141842727c2SChris Kirby * zc_defer_destroy mark for deferred destroy 31423cb34c60Sahrens * 314319b94df9SMatthew Ahrens * outputs: 314419b94df9SMatthew Ahrens * zc_name on failure, name of failed snapshot 31453cb34c60Sahrens */ 31461d452cf5Sahrens static int 314719b94df9SMatthew Ahrens zfs_ioc_destroy_snaps_nvl(zfs_cmd_t *zc) 31481d452cf5Sahrens { 314919b94df9SMatthew Ahrens int err, len; 315019b94df9SMatthew Ahrens nvlist_t *nvl; 315119b94df9SMatthew Ahrens nvpair_t *pair; 31521d452cf5Sahrens 315319b94df9SMatthew Ahrens if ((err = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 315419b94df9SMatthew Ahrens zc->zc_iflags, &nvl)) != 0) 31551d452cf5Sahrens return (err); 315619b94df9SMatthew Ahrens 315719b94df9SMatthew Ahrens len = strlen(zc->zc_name); 315819b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL; 315919b94df9SMatthew Ahrens pair = nvlist_next_nvpair(nvl, pair)) { 316019b94df9SMatthew Ahrens const char *name = nvpair_name(pair); 316119b94df9SMatthew Ahrens /* 316219b94df9SMatthew Ahrens * The snap name must be underneath the zc_name. This ensures 316319b94df9SMatthew Ahrens * that our permission checks were legitimate. 316419b94df9SMatthew Ahrens */ 316519b94df9SMatthew Ahrens if (strncmp(zc->zc_name, name, len) != 0 || 316619b94df9SMatthew Ahrens (name[len] != '@' && name[len] != '/')) { 316719b94df9SMatthew Ahrens nvlist_free(nvl); 316819b94df9SMatthew Ahrens return (EINVAL); 316919b94df9SMatthew Ahrens } 317019b94df9SMatthew Ahrens 317119b94df9SMatthew Ahrens (void) zfs_unmount_snap(name, NULL); 317219b94df9SMatthew Ahrens } 317319b94df9SMatthew Ahrens 317419b94df9SMatthew Ahrens err = dmu_snapshots_destroy_nvl(nvl, zc->zc_defer_destroy, 317519b94df9SMatthew Ahrens zc->zc_name); 317619b94df9SMatthew Ahrens nvlist_free(nvl); 317719b94df9SMatthew Ahrens return (err); 31781d452cf5Sahrens } 31791d452cf5Sahrens 31803cb34c60Sahrens /* 31813cb34c60Sahrens * inputs: 31823cb34c60Sahrens * zc_name name of dataset to destroy 31833cb34c60Sahrens * zc_objset_type type of objset 3184842727c2SChris Kirby * zc_defer_destroy mark for deferred destroy 31853cb34c60Sahrens * 31863cb34c60Sahrens * outputs: none 31873cb34c60Sahrens */ 31881d452cf5Sahrens static int 31891d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 31901d452cf5Sahrens { 3191681d9761SEric Taylor int err; 31921d452cf5Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 3193681d9761SEric Taylor err = zfs_unmount_snap(zc->zc_name, NULL); 31941d452cf5Sahrens if (err) 31951d452cf5Sahrens return (err); 3196fa9e4066Sahrens } 3197fa9e4066Sahrens 3198681d9761SEric Taylor err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy); 3199681d9761SEric Taylor if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 32005c987a37SChris Kirby (void) zvol_remove_minor(zc->zc_name); 3201681d9761SEric Taylor return (err); 3202fa9e4066Sahrens } 3203fa9e4066Sahrens 32043cb34c60Sahrens /* 32053cb34c60Sahrens * inputs: 32064ccbb6e7Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 32073cb34c60Sahrens * 32083cb34c60Sahrens * outputs: none 32093cb34c60Sahrens */ 3210fa9e4066Sahrens static int 3211fa9e4066Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 3212fa9e4066Sahrens { 3213ae46e4c7SMatthew Ahrens dsl_dataset_t *ds, *clone; 32144ccbb6e7Sahrens int error; 3215ae46e4c7SMatthew Ahrens zfsvfs_t *zfsvfs; 3216ae46e4c7SMatthew Ahrens char *clone_name; 3217ae46e4c7SMatthew Ahrens 3218ae46e4c7SMatthew Ahrens error = dsl_dataset_hold(zc->zc_name, FTAG, &ds); 3219ae46e4c7SMatthew Ahrens if (error) 3220ae46e4c7SMatthew Ahrens return (error); 3221ae46e4c7SMatthew Ahrens 3222ae46e4c7SMatthew Ahrens /* must not be a snapshot */ 3223ae46e4c7SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 3224ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3225ae46e4c7SMatthew Ahrens return (EINVAL); 3226ae46e4c7SMatthew Ahrens } 3227ae46e4c7SMatthew Ahrens 3228ae46e4c7SMatthew Ahrens /* must have a most recent snapshot */ 3229ae46e4c7SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 3230ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3231ae46e4c7SMatthew Ahrens return (EINVAL); 3232ae46e4c7SMatthew Ahrens } 32334ccbb6e7Sahrens 32344ccbb6e7Sahrens /* 3235ae46e4c7SMatthew Ahrens * Create clone of most recent snapshot. 32364ccbb6e7Sahrens */ 3237ae46e4c7SMatthew Ahrens clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name); 3238ae46e4c7SMatthew Ahrens error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT); 32394ccbb6e7Sahrens if (error) 3240ae46e4c7SMatthew Ahrens goto out; 32414ccbb6e7Sahrens 3242503ad85cSMatthew Ahrens error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone); 3243ae46e4c7SMatthew Ahrens if (error) 3244ae46e4c7SMatthew Ahrens goto out; 3245ae46e4c7SMatthew Ahrens 3246ae46e4c7SMatthew Ahrens /* 3247ae46e4c7SMatthew Ahrens * Do clone swap. 3248ae46e4c7SMatthew Ahrens */ 324914843421SMatthew Ahrens if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 3250503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs); 325147f263f4Sek if (error == 0) { 325247f263f4Sek int resume_err; 32534ccbb6e7Sahrens 3254ae46e4c7SMatthew Ahrens if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 3255ae46e4c7SMatthew Ahrens error = dsl_dataset_clone_swap(clone, ds, 3256ae46e4c7SMatthew Ahrens B_TRUE); 3257ae46e4c7SMatthew Ahrens dsl_dataset_disown(ds, FTAG); 3258ae46e4c7SMatthew Ahrens ds = NULL; 3259ae46e4c7SMatthew Ahrens } else { 3260ae46e4c7SMatthew Ahrens error = EBUSY; 3261ae46e4c7SMatthew Ahrens } 3262503ad85cSMatthew Ahrens resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); 326347f263f4Sek error = error ? error : resume_err; 326447f263f4Sek } 32654ccbb6e7Sahrens VFS_RELE(zfsvfs->z_vfs); 32664ccbb6e7Sahrens } else { 3267ae46e4c7SMatthew Ahrens if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 3268ae46e4c7SMatthew Ahrens error = dsl_dataset_clone_swap(clone, ds, B_TRUE); 3269ae46e4c7SMatthew Ahrens dsl_dataset_disown(ds, FTAG); 3270ae46e4c7SMatthew Ahrens ds = NULL; 3271ae46e4c7SMatthew Ahrens } else { 3272ae46e4c7SMatthew Ahrens error = EBUSY; 3273ae46e4c7SMatthew Ahrens } 32744ccbb6e7Sahrens } 32754ccbb6e7Sahrens 3276ae46e4c7SMatthew Ahrens /* 3277ae46e4c7SMatthew Ahrens * Destroy clone (which also closes it). 3278ae46e4c7SMatthew Ahrens */ 3279ae46e4c7SMatthew Ahrens (void) dsl_dataset_destroy(clone, FTAG, B_FALSE); 3280ae46e4c7SMatthew Ahrens 3281ae46e4c7SMatthew Ahrens out: 3282ae46e4c7SMatthew Ahrens strfree(clone_name); 3283ae46e4c7SMatthew Ahrens if (ds) 3284ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 32854ccbb6e7Sahrens return (error); 3286fa9e4066Sahrens } 3287fa9e4066Sahrens 32883cb34c60Sahrens /* 32893cb34c60Sahrens * inputs: 32903cb34c60Sahrens * zc_name old name of dataset 32913cb34c60Sahrens * zc_value new name of dataset 32923cb34c60Sahrens * zc_cookie recursive flag (only valid for snapshots) 32933cb34c60Sahrens * 32943cb34c60Sahrens * outputs: none 32953cb34c60Sahrens */ 3296fa9e4066Sahrens static int 3297fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 3298fa9e4066Sahrens { 32997f1f55eaSvb boolean_t recursive = zc->zc_cookie & 1; 3300cdf5b4caSmmusante 3301e9dbad6fSeschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 3302f18faf3fSek if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3303f18faf3fSek strchr(zc->zc_value, '%')) 3304fa9e4066Sahrens return (EINVAL); 3305fa9e4066Sahrens 3306cdf5b4caSmmusante /* 3307cdf5b4caSmmusante * Unmount snapshot unless we're doing a recursive rename, 3308cdf5b4caSmmusante * in which case the dataset code figures out which snapshots 3309cdf5b4caSmmusante * to unmount. 3310cdf5b4caSmmusante */ 3311cdf5b4caSmmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 3312fa9e4066Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 33131d452cf5Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 33141d452cf5Sahrens if (err) 33151d452cf5Sahrens return (err); 3316fa9e4066Sahrens } 3317681d9761SEric Taylor if (zc->zc_objset_type == DMU_OST_ZVOL) 3318681d9761SEric Taylor (void) zvol_remove_minor(zc->zc_name); 3319cdf5b4caSmmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 3320fa9e4066Sahrens } 3321fa9e4066Sahrens 332292241e0bSTom Erickson static int 332392241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 332492241e0bSTom Erickson { 332592241e0bSTom Erickson const char *propname = nvpair_name(pair); 332692241e0bSTom Erickson boolean_t issnap = (strchr(dsname, '@') != NULL); 332792241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 332892241e0bSTom Erickson uint64_t intval; 332992241e0bSTom Erickson int err; 333092241e0bSTom Erickson 333192241e0bSTom Erickson if (prop == ZPROP_INVAL) { 333292241e0bSTom Erickson if (zfs_prop_user(propname)) { 333392241e0bSTom Erickson if (err = zfs_secpolicy_write_perms(dsname, 333492241e0bSTom Erickson ZFS_DELEG_PERM_USERPROP, cr)) 333592241e0bSTom Erickson return (err); 333692241e0bSTom Erickson return (0); 333792241e0bSTom Erickson } 333892241e0bSTom Erickson 333992241e0bSTom Erickson if (!issnap && zfs_prop_userquota(propname)) { 334092241e0bSTom Erickson const char *perm = NULL; 334192241e0bSTom Erickson const char *uq_prefix = 334292241e0bSTom Erickson zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 334392241e0bSTom Erickson const char *gq_prefix = 334492241e0bSTom Erickson zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 334592241e0bSTom Erickson 334692241e0bSTom Erickson if (strncmp(propname, uq_prefix, 334792241e0bSTom Erickson strlen(uq_prefix)) == 0) { 334892241e0bSTom Erickson perm = ZFS_DELEG_PERM_USERQUOTA; 334992241e0bSTom Erickson } else if (strncmp(propname, gq_prefix, 335092241e0bSTom Erickson strlen(gq_prefix)) == 0) { 335192241e0bSTom Erickson perm = ZFS_DELEG_PERM_GROUPQUOTA; 335292241e0bSTom Erickson } else { 335392241e0bSTom Erickson /* USERUSED and GROUPUSED are read-only */ 335492241e0bSTom Erickson return (EINVAL); 335592241e0bSTom Erickson } 335692241e0bSTom Erickson 335792241e0bSTom Erickson if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 335892241e0bSTom Erickson return (err); 335992241e0bSTom Erickson return (0); 336092241e0bSTom Erickson } 336192241e0bSTom Erickson 336292241e0bSTom Erickson return (EINVAL); 336392241e0bSTom Erickson } 336492241e0bSTom Erickson 336592241e0bSTom Erickson if (issnap) 336692241e0bSTom Erickson return (EINVAL); 336792241e0bSTom Erickson 336892241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 336992241e0bSTom Erickson /* 337092241e0bSTom Erickson * dsl_prop_get_all_impl() returns properties in this 337192241e0bSTom Erickson * format. 337292241e0bSTom Erickson */ 337392241e0bSTom Erickson nvlist_t *attrs; 337492241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 337592241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 337692241e0bSTom Erickson &pair) == 0); 337792241e0bSTom Erickson } 337892241e0bSTom Erickson 337992241e0bSTom Erickson /* 338092241e0bSTom Erickson * Check that this value is valid for this pool version 338192241e0bSTom Erickson */ 338292241e0bSTom Erickson switch (prop) { 338392241e0bSTom Erickson case ZFS_PROP_COMPRESSION: 338492241e0bSTom Erickson /* 338592241e0bSTom Erickson * If the user specified gzip compression, make sure 338692241e0bSTom Erickson * the SPA supports it. We ignore any errors here since 338792241e0bSTom Erickson * we'll catch them later. 338892241e0bSTom Erickson */ 338992241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_UINT64 && 339092241e0bSTom Erickson nvpair_value_uint64(pair, &intval) == 0) { 339192241e0bSTom Erickson if (intval >= ZIO_COMPRESS_GZIP_1 && 339292241e0bSTom Erickson intval <= ZIO_COMPRESS_GZIP_9 && 339392241e0bSTom Erickson zfs_earlier_version(dsname, 339492241e0bSTom Erickson SPA_VERSION_GZIP_COMPRESSION)) { 339592241e0bSTom Erickson return (ENOTSUP); 339692241e0bSTom Erickson } 339792241e0bSTom Erickson 339892241e0bSTom Erickson if (intval == ZIO_COMPRESS_ZLE && 339992241e0bSTom Erickson zfs_earlier_version(dsname, 340092241e0bSTom Erickson SPA_VERSION_ZLE_COMPRESSION)) 340192241e0bSTom Erickson return (ENOTSUP); 340292241e0bSTom Erickson 340392241e0bSTom Erickson /* 340492241e0bSTom Erickson * If this is a bootable dataset then 340592241e0bSTom Erickson * verify that the compression algorithm 340692241e0bSTom Erickson * is supported for booting. We must return 340792241e0bSTom Erickson * something other than ENOTSUP since it 340892241e0bSTom Erickson * implies a downrev pool version. 340992241e0bSTom Erickson */ 341092241e0bSTom Erickson if (zfs_is_bootfs(dsname) && 341192241e0bSTom Erickson !BOOTFS_COMPRESS_VALID(intval)) { 341292241e0bSTom Erickson return (ERANGE); 341392241e0bSTom Erickson } 341492241e0bSTom Erickson } 341592241e0bSTom Erickson break; 341692241e0bSTom Erickson 341792241e0bSTom Erickson case ZFS_PROP_COPIES: 341892241e0bSTom Erickson if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 341992241e0bSTom Erickson return (ENOTSUP); 342092241e0bSTom Erickson break; 342192241e0bSTom Erickson 342292241e0bSTom Erickson case ZFS_PROP_DEDUP: 342392241e0bSTom Erickson if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 342492241e0bSTom Erickson return (ENOTSUP); 342592241e0bSTom Erickson break; 342692241e0bSTom Erickson 342792241e0bSTom Erickson case ZFS_PROP_SHARESMB: 342892241e0bSTom Erickson if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 342992241e0bSTom Erickson return (ENOTSUP); 343092241e0bSTom Erickson break; 343192241e0bSTom Erickson 343292241e0bSTom Erickson case ZFS_PROP_ACLINHERIT: 343392241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_UINT64 && 343492241e0bSTom Erickson nvpair_value_uint64(pair, &intval) == 0) { 343592241e0bSTom Erickson if (intval == ZFS_ACL_PASSTHROUGH_X && 343692241e0bSTom Erickson zfs_earlier_version(dsname, 343792241e0bSTom Erickson SPA_VERSION_PASSTHROUGH_X)) 343892241e0bSTom Erickson return (ENOTSUP); 343992241e0bSTom Erickson } 344092241e0bSTom Erickson break; 344192241e0bSTom Erickson } 344292241e0bSTom Erickson 344392241e0bSTom Erickson return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 344492241e0bSTom Erickson } 344592241e0bSTom Erickson 344692241e0bSTom Erickson /* 344792241e0bSTom Erickson * Removes properties from the given props list that fail permission checks 344892241e0bSTom Erickson * needed to clear them and to restore them in case of a receive error. For each 344992241e0bSTom Erickson * property, make sure we have both set and inherit permissions. 345092241e0bSTom Erickson * 345192241e0bSTom Erickson * Returns the first error encountered if any permission checks fail. If the 345292241e0bSTom Erickson * caller provides a non-NULL errlist, it also gives the complete list of names 345392241e0bSTom Erickson * of all the properties that failed a permission check along with the 345492241e0bSTom Erickson * corresponding error numbers. The caller is responsible for freeing the 345592241e0bSTom Erickson * returned errlist. 345692241e0bSTom Erickson * 345792241e0bSTom Erickson * If every property checks out successfully, zero is returned and the list 345892241e0bSTom Erickson * pointed at by errlist is NULL. 345992241e0bSTom Erickson */ 346092241e0bSTom Erickson static int 346192241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 3462745cd3c5Smaybee { 3463745cd3c5Smaybee zfs_cmd_t *zc; 346492241e0bSTom Erickson nvpair_t *pair, *next_pair; 346592241e0bSTom Erickson nvlist_t *errors; 346692241e0bSTom Erickson int err, rv = 0; 3467745cd3c5Smaybee 3468745cd3c5Smaybee if (props == NULL) 346992241e0bSTom Erickson return (0); 347092241e0bSTom Erickson 347192241e0bSTom Erickson VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 347292241e0bSTom Erickson 3473745cd3c5Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 3474745cd3c5Smaybee (void) strcpy(zc->zc_name, dataset); 347592241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL); 347692241e0bSTom Erickson while (pair != NULL) { 347792241e0bSTom Erickson next_pair = nvlist_next_nvpair(props, pair); 347892241e0bSTom Erickson 347992241e0bSTom Erickson (void) strcpy(zc->zc_value, nvpair_name(pair)); 348092241e0bSTom Erickson if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 348192241e0bSTom Erickson (err = zfs_secpolicy_inherit(zc, CRED())) != 0) { 348292241e0bSTom Erickson VERIFY(nvlist_remove_nvpair(props, pair) == 0); 348392241e0bSTom Erickson VERIFY(nvlist_add_int32(errors, 348492241e0bSTom Erickson zc->zc_value, err) == 0); 348592241e0bSTom Erickson } 348692241e0bSTom Erickson pair = next_pair; 3487745cd3c5Smaybee } 3488745cd3c5Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 348992241e0bSTom Erickson 349092241e0bSTom Erickson if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 349192241e0bSTom Erickson nvlist_free(errors); 349292241e0bSTom Erickson errors = NULL; 349392241e0bSTom Erickson } else { 349492241e0bSTom Erickson VERIFY(nvpair_value_int32(pair, &rv) == 0); 349592241e0bSTom Erickson } 349692241e0bSTom Erickson 349792241e0bSTom Erickson if (errlist == NULL) 349892241e0bSTom Erickson nvlist_free(errors); 349992241e0bSTom Erickson else 350092241e0bSTom Erickson *errlist = errors; 350192241e0bSTom Erickson 350292241e0bSTom Erickson return (rv); 350392241e0bSTom Erickson } 350492241e0bSTom Erickson 350592241e0bSTom Erickson static boolean_t 350692241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2) 350792241e0bSTom Erickson { 350892241e0bSTom Erickson if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 350992241e0bSTom Erickson /* dsl_prop_get_all_impl() format */ 351092241e0bSTom Erickson nvlist_t *attrs; 351192241e0bSTom Erickson VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 351292241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 351392241e0bSTom Erickson &p1) == 0); 351492241e0bSTom Erickson } 351592241e0bSTom Erickson 351692241e0bSTom Erickson if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 351792241e0bSTom Erickson nvlist_t *attrs; 351892241e0bSTom Erickson VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 351992241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 352092241e0bSTom Erickson &p2) == 0); 352192241e0bSTom Erickson } 352292241e0bSTom Erickson 352392241e0bSTom Erickson if (nvpair_type(p1) != nvpair_type(p2)) 352492241e0bSTom Erickson return (B_FALSE); 352592241e0bSTom Erickson 352692241e0bSTom Erickson if (nvpair_type(p1) == DATA_TYPE_STRING) { 352792241e0bSTom Erickson char *valstr1, *valstr2; 352892241e0bSTom Erickson 352992241e0bSTom Erickson VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 353092241e0bSTom Erickson VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 353192241e0bSTom Erickson return (strcmp(valstr1, valstr2) == 0); 353292241e0bSTom Erickson } else { 353392241e0bSTom Erickson uint64_t intval1, intval2; 353492241e0bSTom Erickson 353592241e0bSTom Erickson VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 353692241e0bSTom Erickson VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 353792241e0bSTom Erickson return (intval1 == intval2); 353892241e0bSTom Erickson } 3539745cd3c5Smaybee } 3540745cd3c5Smaybee 354192241e0bSTom Erickson /* 354292241e0bSTom Erickson * Remove properties from props if they are not going to change (as determined 354392241e0bSTom Erickson * by comparison with origprops). Remove them from origprops as well, since we 354492241e0bSTom Erickson * do not need to clear or restore properties that won't change. 354592241e0bSTom Erickson */ 354692241e0bSTom Erickson static void 354792241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops) 354892241e0bSTom Erickson { 354992241e0bSTom Erickson nvpair_t *pair, *next_pair; 355092241e0bSTom Erickson 355192241e0bSTom Erickson if (origprops == NULL) 355292241e0bSTom Erickson return; /* all props need to be received */ 355392241e0bSTom Erickson 355492241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL); 355592241e0bSTom Erickson while (pair != NULL) { 355692241e0bSTom Erickson const char *propname = nvpair_name(pair); 355792241e0bSTom Erickson nvpair_t *match; 355892241e0bSTom Erickson 355992241e0bSTom Erickson next_pair = nvlist_next_nvpair(props, pair); 356092241e0bSTom Erickson 356192241e0bSTom Erickson if ((nvlist_lookup_nvpair(origprops, propname, 356292241e0bSTom Erickson &match) != 0) || !propval_equals(pair, match)) 356392241e0bSTom Erickson goto next; /* need to set received value */ 356492241e0bSTom Erickson 356592241e0bSTom Erickson /* don't clear the existing received value */ 356692241e0bSTom Erickson (void) nvlist_remove_nvpair(origprops, match); 356792241e0bSTom Erickson /* don't bother receiving the property */ 356892241e0bSTom Erickson (void) nvlist_remove_nvpair(props, pair); 356992241e0bSTom Erickson next: 357092241e0bSTom Erickson pair = next_pair; 357192241e0bSTom Erickson } 357292241e0bSTom Erickson } 357392241e0bSTom Erickson 357492241e0bSTom Erickson #ifdef DEBUG 357592241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err; 357692241e0bSTom Erickson #endif 357792241e0bSTom Erickson 35783cb34c60Sahrens /* 35793cb34c60Sahrens * inputs: 35803cb34c60Sahrens * zc_name name of containing filesystem 35813cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 35823cb34c60Sahrens * zc_value name of snapshot to create 35833cb34c60Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 35843cb34c60Sahrens * zc_cookie file descriptor to recv from 35853cb34c60Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 35863cb34c60Sahrens * zc_guid force flag 3587c99e4bdcSChris Kirby * zc_cleanup_fd cleanup-on-exit file descriptor 3588c99e4bdcSChris Kirby * zc_action_handle handle for this guid/ds mapping (or zero on first call) 35893cb34c60Sahrens * 35903cb34c60Sahrens * outputs: 35913cb34c60Sahrens * zc_cookie number of bytes read 359292241e0bSTom Erickson * zc_nvlist_dst{_size} error for each unapplied received property 359392241e0bSTom Erickson * zc_obj zprop_errflags_t 3594c99e4bdcSChris Kirby * zc_action_handle handle for this guid/ds mapping 35953cb34c60Sahrens */ 3596fa9e4066Sahrens static int 35973cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 3598fa9e4066Sahrens { 3599fa9e4066Sahrens file_t *fp; 3600f18faf3fSek objset_t *os; 36013cb34c60Sahrens dmu_recv_cookie_t drc; 3602f18faf3fSek boolean_t force = (boolean_t)zc->zc_guid; 360392241e0bSTom Erickson int fd; 360492241e0bSTom Erickson int error = 0; 360592241e0bSTom Erickson int props_error = 0; 360692241e0bSTom Erickson nvlist_t *errors; 36073cb34c60Sahrens offset_t off; 360892241e0bSTom Erickson nvlist_t *props = NULL; /* sent properties */ 360992241e0bSTom Erickson nvlist_t *origprops = NULL; /* existing properties */ 36103cb34c60Sahrens objset_t *origin = NULL; 36113cb34c60Sahrens char *tosnap; 36123cb34c60Sahrens char tofs[ZFS_MAXNAMELEN]; 361392241e0bSTom Erickson boolean_t first_recvd_props = B_FALSE; 3614fa9e4066Sahrens 36153ccfa83cSahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3616f18faf3fSek strchr(zc->zc_value, '@') == NULL || 3617f18faf3fSek strchr(zc->zc_value, '%')) 36183ccfa83cSahrens return (EINVAL); 36193ccfa83cSahrens 36203cb34c60Sahrens (void) strcpy(tofs, zc->zc_value); 36213cb34c60Sahrens tosnap = strchr(tofs, '@'); 362292241e0bSTom Erickson *tosnap++ = '\0'; 36233cb34c60Sahrens 36243cb34c60Sahrens if (zc->zc_nvlist_src != NULL && 36253cb34c60Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 3626478ed9adSEric Taylor zc->zc_iflags, &props)) != 0) 36273cb34c60Sahrens return (error); 36283cb34c60Sahrens 3629fa9e4066Sahrens fd = zc->zc_cookie; 3630fa9e4066Sahrens fp = getf(fd); 36313cb34c60Sahrens if (fp == NULL) { 36323cb34c60Sahrens nvlist_free(props); 3633fa9e4066Sahrens return (EBADF); 36343cb34c60Sahrens } 3635f18faf3fSek 363692241e0bSTom Erickson VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 363792241e0bSTom Erickson 3638503ad85cSMatthew Ahrens if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) { 363992241e0bSTom Erickson if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) && 364092241e0bSTom Erickson !dsl_prop_get_hasrecvd(os)) { 364192241e0bSTom Erickson first_recvd_props = B_TRUE; 364292241e0bSTom Erickson } 364392241e0bSTom Erickson 3644745cd3c5Smaybee /* 364592241e0bSTom Erickson * If new received properties are supplied, they are to 364692241e0bSTom Erickson * completely replace the existing received properties, so stash 364792241e0bSTom Erickson * away the existing ones. 3648745cd3c5Smaybee */ 364992241e0bSTom Erickson if (dsl_prop_get_received(os, &origprops) == 0) { 365092241e0bSTom Erickson nvlist_t *errlist = NULL; 365192241e0bSTom Erickson /* 365292241e0bSTom Erickson * Don't bother writing a property if its value won't 365392241e0bSTom Erickson * change (and avoid the unnecessary security checks). 365492241e0bSTom Erickson * 365592241e0bSTom Erickson * The first receive after SPA_VERSION_RECVD_PROPS is a 365692241e0bSTom Erickson * special case where we blow away all local properties 365792241e0bSTom Erickson * regardless. 365892241e0bSTom Erickson */ 365992241e0bSTom Erickson if (!first_recvd_props) 366092241e0bSTom Erickson props_reduce(props, origprops); 366192241e0bSTom Erickson if (zfs_check_clearable(tofs, origprops, 366292241e0bSTom Erickson &errlist) != 0) 366392241e0bSTom Erickson (void) nvlist_merge(errors, errlist, 0); 366492241e0bSTom Erickson nvlist_free(errlist); 366592241e0bSTom Erickson } 3666745cd3c5Smaybee 3667503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 3668f18faf3fSek } 3669f18faf3fSek 36703cb34c60Sahrens if (zc->zc_string[0]) { 3671503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_string, FTAG, &origin); 3672745cd3c5Smaybee if (error) 3673745cd3c5Smaybee goto out; 36743cb34c60Sahrens } 36753cb34c60Sahrens 36769e69d7d0SLori Alt error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds, 36779e69d7d0SLori Alt &zc->zc_begin_record, force, origin, &drc); 36783cb34c60Sahrens if (origin) 3679503ad85cSMatthew Ahrens dmu_objset_rele(origin, FTAG); 3680745cd3c5Smaybee if (error) 3681745cd3c5Smaybee goto out; 3682f18faf3fSek 3683f18faf3fSek /* 368492241e0bSTom Erickson * Set properties before we receive the stream so that they are applied 368592241e0bSTom Erickson * to the new data. Note that we must call dmu_recv_stream() if 368692241e0bSTom Erickson * dmu_recv_begin() succeeds. 3687f18faf3fSek */ 36883cb34c60Sahrens if (props) { 368992241e0bSTom Erickson nvlist_t *errlist; 369092241e0bSTom Erickson 369192241e0bSTom Erickson if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) { 369292241e0bSTom Erickson if (drc.drc_newfs) { 369392241e0bSTom Erickson if (spa_version(os->os_spa) >= 369492241e0bSTom Erickson SPA_VERSION_RECVD_PROPS) 369592241e0bSTom Erickson first_recvd_props = B_TRUE; 369692241e0bSTom Erickson } else if (origprops != NULL) { 369792241e0bSTom Erickson if (clear_received_props(os, tofs, origprops, 369892241e0bSTom Erickson first_recvd_props ? NULL : props) != 0) 369992241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR; 370092241e0bSTom Erickson } else { 370192241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR; 370292241e0bSTom Erickson } 370392241e0bSTom Erickson dsl_prop_set_hasrecvd(os); 370492241e0bSTom Erickson } else if (!drc.drc_newfs) { 370592241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR; 370692241e0bSTom Erickson } 370792241e0bSTom Erickson 370892241e0bSTom Erickson (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 370992241e0bSTom Erickson props, &errlist); 371092241e0bSTom Erickson (void) nvlist_merge(errors, errlist, 0); 371192241e0bSTom Erickson nvlist_free(errlist); 371292241e0bSTom Erickson } 371392241e0bSTom Erickson 371492241e0bSTom Erickson if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) { 3715745cd3c5Smaybee /* 371692241e0bSTom Erickson * Caller made zc->zc_nvlist_dst less than the minimum expected 371792241e0bSTom Erickson * size or supplied an invalid address. 3718745cd3c5Smaybee */ 371992241e0bSTom Erickson props_error = EINVAL; 37203cb34c60Sahrens } 37213cb34c60Sahrens 37223cb34c60Sahrens off = fp->f_offset; 3723c99e4bdcSChris Kirby error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd, 3724c99e4bdcSChris Kirby &zc->zc_action_handle); 3725a2eea2e1Sahrens 3726f4b94bdeSMatthew Ahrens if (error == 0) { 3727f4b94bdeSMatthew Ahrens zfsvfs_t *zfsvfs = NULL; 3728745cd3c5Smaybee 3729f4b94bdeSMatthew Ahrens if (getzfsvfs(tofs, &zfsvfs) == 0) { 3730f4b94bdeSMatthew Ahrens /* online recv */ 3731f4b94bdeSMatthew Ahrens int end_err; 3732745cd3c5Smaybee 3733503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs); 3734f4b94bdeSMatthew Ahrens /* 3735f4b94bdeSMatthew Ahrens * If the suspend fails, then the recv_end will 3736f4b94bdeSMatthew Ahrens * likely also fail, and clean up after itself. 3737f4b94bdeSMatthew Ahrens */ 3738f4b94bdeSMatthew Ahrens end_err = dmu_recv_end(&drc); 37395c703fceSGeorge Wilson if (error == 0) 37405c703fceSGeorge Wilson error = zfs_resume_fs(zfsvfs, tofs); 3741f4b94bdeSMatthew Ahrens error = error ? error : end_err; 3742f4b94bdeSMatthew Ahrens VFS_RELE(zfsvfs->z_vfs); 3743745cd3c5Smaybee } else { 3744f4b94bdeSMatthew Ahrens error = dmu_recv_end(&drc); 37453cb34c60Sahrens } 374647f263f4Sek } 37473cb34c60Sahrens 37483cb34c60Sahrens zc->zc_cookie = off - fp->f_offset; 37493cb34c60Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 37503cb34c60Sahrens fp->f_offset = off; 3751a2eea2e1Sahrens 375292241e0bSTom Erickson #ifdef DEBUG 375392241e0bSTom Erickson if (zfs_ioc_recv_inject_err) { 375492241e0bSTom Erickson zfs_ioc_recv_inject_err = B_FALSE; 375592241e0bSTom Erickson error = 1; 375692241e0bSTom Erickson } 375792241e0bSTom Erickson #endif 3758745cd3c5Smaybee /* 3759745cd3c5Smaybee * On error, restore the original props. 3760745cd3c5Smaybee */ 3761745cd3c5Smaybee if (error && props) { 376292241e0bSTom Erickson if (dmu_objset_hold(tofs, FTAG, &os) == 0) { 376392241e0bSTom Erickson if (clear_received_props(os, tofs, props, NULL) != 0) { 376492241e0bSTom Erickson /* 376592241e0bSTom Erickson * We failed to clear the received properties. 376692241e0bSTom Erickson * Since we may have left a $recvd value on the 376792241e0bSTom Erickson * system, we can't clear the $hasrecvd flag. 376892241e0bSTom Erickson */ 376992241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 377092241e0bSTom Erickson } else if (first_recvd_props) { 377192241e0bSTom Erickson dsl_prop_unset_hasrecvd(os); 377292241e0bSTom Erickson } 377392241e0bSTom Erickson dmu_objset_rele(os, FTAG); 377492241e0bSTom Erickson } else if (!drc.drc_newfs) { 377592241e0bSTom Erickson /* We failed to clear the received properties. */ 377692241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 377792241e0bSTom Erickson } 377892241e0bSTom Erickson 377992241e0bSTom Erickson if (origprops == NULL && !drc.drc_newfs) { 378092241e0bSTom Erickson /* We failed to stash the original properties. */ 378192241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 378292241e0bSTom Erickson } 378392241e0bSTom Erickson 378492241e0bSTom Erickson /* 378592241e0bSTom Erickson * dsl_props_set() will not convert RECEIVED to LOCAL on or 378692241e0bSTom Erickson * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 378792241e0bSTom Erickson * explictly if we're restoring local properties cleared in the 378892241e0bSTom Erickson * first new-style receive. 378992241e0bSTom Erickson */ 379092241e0bSTom Erickson if (origprops != NULL && 379192241e0bSTom Erickson zfs_set_prop_nvlist(tofs, (first_recvd_props ? 379292241e0bSTom Erickson ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 379392241e0bSTom Erickson origprops, NULL) != 0) { 379492241e0bSTom Erickson /* 379592241e0bSTom Erickson * We stashed the original properties but failed to 379692241e0bSTom Erickson * restore them. 379792241e0bSTom Erickson */ 379892241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 379992241e0bSTom Erickson } 3800745cd3c5Smaybee } 3801745cd3c5Smaybee out: 3802745cd3c5Smaybee nvlist_free(props); 3803745cd3c5Smaybee nvlist_free(origprops); 380492241e0bSTom Erickson nvlist_free(errors); 3805fa9e4066Sahrens releasef(fd); 380692241e0bSTom Erickson 380792241e0bSTom Erickson if (error == 0) 380892241e0bSTom Erickson error = props_error; 380992241e0bSTom Erickson 3810fa9e4066Sahrens return (error); 3811fa9e4066Sahrens } 3812fa9e4066Sahrens 38133cb34c60Sahrens /* 38143cb34c60Sahrens * inputs: 38153cb34c60Sahrens * zc_name name of snapshot to send 38163cb34c60Sahrens * zc_cookie file descriptor to send stream to 3817a7f53a56SChris Kirby * zc_obj fromorigin flag (mutually exclusive with zc_fromobj) 3818a7f53a56SChris Kirby * zc_sendobj objsetid of snapshot to send 3819a7f53a56SChris Kirby * zc_fromobj objsetid of incremental fromsnap (may be zero) 382019b94df9SMatthew Ahrens * zc_guid if set, estimate size of stream only. zc_cookie is ignored. 382119b94df9SMatthew Ahrens * output size in zc_objset_type. 38223cb34c60Sahrens * 38233cb34c60Sahrens * outputs: none 38243cb34c60Sahrens */ 3825fa9e4066Sahrens static int 38263cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc) 3827fa9e4066Sahrens { 3828fa9e4066Sahrens objset_t *fromsnap = NULL; 3829fa9e4066Sahrens objset_t *tosnap; 3830fa9e4066Sahrens int error; 38313cb34c60Sahrens offset_t off; 3832a7f53a56SChris Kirby dsl_dataset_t *ds; 3833a7f53a56SChris Kirby dsl_dataset_t *dsfrom = NULL; 3834a7f53a56SChris Kirby spa_t *spa; 3835a7f53a56SChris Kirby dsl_pool_t *dp; 383619b94df9SMatthew Ahrens boolean_t estimate = (zc->zc_guid != 0); 3837fa9e4066Sahrens 3838a7f53a56SChris Kirby error = spa_open(zc->zc_name, &spa, FTAG); 3839fa9e4066Sahrens if (error) 3840fa9e4066Sahrens return (error); 3841fa9e4066Sahrens 3842a7f53a56SChris Kirby dp = spa_get_dsl(spa); 3843a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 3844a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 3845a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 3846a7f53a56SChris Kirby if (error) { 3847a7f53a56SChris Kirby spa_close(spa, FTAG); 3848a7f53a56SChris Kirby return (error); 3849a7f53a56SChris Kirby } 3850a7f53a56SChris Kirby 3851a7f53a56SChris Kirby error = dmu_objset_from_ds(ds, &tosnap); 3852a7f53a56SChris Kirby if (error) { 3853a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 3854a7f53a56SChris Kirby spa_close(spa, FTAG); 3855a7f53a56SChris Kirby return (error); 3856a7f53a56SChris Kirby } 3857a7f53a56SChris Kirby 3858a7f53a56SChris Kirby if (zc->zc_fromobj != 0) { 3859a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 3860a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom); 3861a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 3862a7f53a56SChris Kirby spa_close(spa, FTAG); 3863a7f53a56SChris Kirby if (error) { 3864a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 3865a7f53a56SChris Kirby return (error); 3866a7f53a56SChris Kirby } 3867a7f53a56SChris Kirby error = dmu_objset_from_ds(dsfrom, &fromsnap); 3868fa9e4066Sahrens if (error) { 3869a7f53a56SChris Kirby dsl_dataset_rele(dsfrom, FTAG); 3870a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 3871fa9e4066Sahrens return (error); 3872fa9e4066Sahrens } 3873a7f53a56SChris Kirby } else { 3874a7f53a56SChris Kirby spa_close(spa, FTAG); 3875fa9e4066Sahrens } 3876fa9e4066Sahrens 387719b94df9SMatthew Ahrens if (estimate) { 387819b94df9SMatthew Ahrens error = dmu_send_estimate(tosnap, fromsnap, zc->zc_obj, 387919b94df9SMatthew Ahrens &zc->zc_objset_type); 388019b94df9SMatthew Ahrens } else { 388119b94df9SMatthew Ahrens file_t *fp = getf(zc->zc_cookie); 388219b94df9SMatthew Ahrens if (fp == NULL) { 388319b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 388419b94df9SMatthew Ahrens if (dsfrom) 388519b94df9SMatthew Ahrens dsl_dataset_rele(dsfrom, FTAG); 388619b94df9SMatthew Ahrens return (EBADF); 388719b94df9SMatthew Ahrens } 3888fa9e4066Sahrens 388919b94df9SMatthew Ahrens off = fp->f_offset; 3890*4e3c9f44SBill Pijewski error = dmu_send(tosnap, fromsnap, zc->zc_obj, 3891*4e3c9f44SBill Pijewski zc->zc_cookie, fp->f_vnode, &off); 3892fa9e4066Sahrens 389319b94df9SMatthew Ahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 389419b94df9SMatthew Ahrens fp->f_offset = off; 389519b94df9SMatthew Ahrens releasef(zc->zc_cookie); 389619b94df9SMatthew Ahrens } 3897a7f53a56SChris Kirby if (dsfrom) 3898a7f53a56SChris Kirby dsl_dataset_rele(dsfrom, FTAG); 3899a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 3900fa9e4066Sahrens return (error); 3901fa9e4066Sahrens } 3902fa9e4066Sahrens 3903*4e3c9f44SBill Pijewski /* 3904*4e3c9f44SBill Pijewski * inputs: 3905*4e3c9f44SBill Pijewski * zc_name name of snapshot on which to report progress 3906*4e3c9f44SBill Pijewski * zc_cookie file descriptor of send stream 3907*4e3c9f44SBill Pijewski * 3908*4e3c9f44SBill Pijewski * outputs: 3909*4e3c9f44SBill Pijewski * zc_cookie number of bytes written in send stream thus far 3910*4e3c9f44SBill Pijewski */ 3911*4e3c9f44SBill Pijewski static int 3912*4e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc) 3913*4e3c9f44SBill Pijewski { 3914*4e3c9f44SBill Pijewski dsl_dataset_t *ds; 3915*4e3c9f44SBill Pijewski dmu_sendarg_t *dsp = NULL; 3916*4e3c9f44SBill Pijewski int error; 3917*4e3c9f44SBill Pijewski 3918*4e3c9f44SBill Pijewski if ((error = dsl_dataset_hold(zc->zc_name, FTAG, &ds)) != 0) 3919*4e3c9f44SBill Pijewski return (error); 3920*4e3c9f44SBill Pijewski 3921*4e3c9f44SBill Pijewski mutex_enter(&ds->ds_sendstream_lock); 3922*4e3c9f44SBill Pijewski 3923*4e3c9f44SBill Pijewski /* 3924*4e3c9f44SBill Pijewski * Iterate over all the send streams currently active on this dataset. 3925*4e3c9f44SBill Pijewski * If there's one which matches the specified file descriptor _and_ the 3926*4e3c9f44SBill Pijewski * stream was started by the current process, return the progress of 3927*4e3c9f44SBill Pijewski * that stream. 3928*4e3c9f44SBill Pijewski */ 3929*4e3c9f44SBill Pijewski for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL; 3930*4e3c9f44SBill Pijewski dsp = list_next(&ds->ds_sendstreams, dsp)) { 3931*4e3c9f44SBill Pijewski if (dsp->dsa_outfd == zc->zc_cookie && 3932*4e3c9f44SBill Pijewski dsp->dsa_proc == curproc) 3933*4e3c9f44SBill Pijewski break; 3934*4e3c9f44SBill Pijewski } 3935*4e3c9f44SBill Pijewski 3936*4e3c9f44SBill Pijewski if (dsp != NULL) 3937*4e3c9f44SBill Pijewski zc->zc_cookie = *(dsp->dsa_off); 3938*4e3c9f44SBill Pijewski else 3939*4e3c9f44SBill Pijewski error = ENOENT; 3940*4e3c9f44SBill Pijewski 3941*4e3c9f44SBill Pijewski mutex_exit(&ds->ds_sendstream_lock); 3942*4e3c9f44SBill Pijewski dsl_dataset_rele(ds, FTAG); 3943*4e3c9f44SBill Pijewski return (error); 3944*4e3c9f44SBill Pijewski } 3945*4e3c9f44SBill Pijewski 3946ea8dc4b6Seschrock static int 3947ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 3948ea8dc4b6Seschrock { 3949ea8dc4b6Seschrock int id, error; 3950ea8dc4b6Seschrock 3951ea8dc4b6Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 3952ea8dc4b6Seschrock &zc->zc_inject_record); 3953ea8dc4b6Seschrock 3954ea8dc4b6Seschrock if (error == 0) 3955ea8dc4b6Seschrock zc->zc_guid = (uint64_t)id; 3956ea8dc4b6Seschrock 3957ea8dc4b6Seschrock return (error); 3958ea8dc4b6Seschrock } 3959ea8dc4b6Seschrock 3960ea8dc4b6Seschrock static int 3961ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 3962ea8dc4b6Seschrock { 3963ea8dc4b6Seschrock return (zio_clear_fault((int)zc->zc_guid)); 3964ea8dc4b6Seschrock } 3965ea8dc4b6Seschrock 3966ea8dc4b6Seschrock static int 3967ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 3968ea8dc4b6Seschrock { 3969ea8dc4b6Seschrock int id = (int)zc->zc_guid; 3970ea8dc4b6Seschrock int error; 3971ea8dc4b6Seschrock 3972ea8dc4b6Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 3973ea8dc4b6Seschrock &zc->zc_inject_record); 3974ea8dc4b6Seschrock 3975ea8dc4b6Seschrock zc->zc_guid = id; 3976ea8dc4b6Seschrock 3977ea8dc4b6Seschrock return (error); 3978ea8dc4b6Seschrock } 3979ea8dc4b6Seschrock 3980ea8dc4b6Seschrock static int 3981ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 3982ea8dc4b6Seschrock { 3983ea8dc4b6Seschrock spa_t *spa; 3984ea8dc4b6Seschrock int error; 3985e9dbad6fSeschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 3986ea8dc4b6Seschrock 3987ea8dc4b6Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 3988ea8dc4b6Seschrock return (error); 3989ea8dc4b6Seschrock 3990e9dbad6fSeschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 3991ea8dc4b6Seschrock &count); 3992ea8dc4b6Seschrock if (error == 0) 3993e9dbad6fSeschrock zc->zc_nvlist_dst_size = count; 3994ea8dc4b6Seschrock else 3995e9dbad6fSeschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 3996ea8dc4b6Seschrock 3997ea8dc4b6Seschrock spa_close(spa, FTAG); 3998ea8dc4b6Seschrock 3999ea8dc4b6Seschrock return (error); 4000ea8dc4b6Seschrock } 4001ea8dc4b6Seschrock 4002ea8dc4b6Seschrock static int 4003ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 4004ea8dc4b6Seschrock { 4005ea8dc4b6Seschrock spa_t *spa; 4006ea8dc4b6Seschrock vdev_t *vd; 4007bb8b5132Sek int error; 4008ea8dc4b6Seschrock 4009b87f3af3Sperrin /* 4010b87f3af3Sperrin * On zpool clear we also fix up missing slogs 4011b87f3af3Sperrin */ 4012b87f3af3Sperrin mutex_enter(&spa_namespace_lock); 4013b87f3af3Sperrin spa = spa_lookup(zc->zc_name); 4014b87f3af3Sperrin if (spa == NULL) { 4015b87f3af3Sperrin mutex_exit(&spa_namespace_lock); 4016b87f3af3Sperrin return (EIO); 4017b87f3af3Sperrin } 4018b24ab676SJeff Bonwick if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 4019b87f3af3Sperrin /* we need to let spa_open/spa_load clear the chains */ 4020b24ab676SJeff Bonwick spa_set_log_state(spa, SPA_LOG_CLEAR); 4021b87f3af3Sperrin } 4022468c413aSTim Haley spa->spa_last_open_failed = 0; 4023b87f3af3Sperrin mutex_exit(&spa_namespace_lock); 4024b87f3af3Sperrin 4025c8ee1847SVictor Latushkin if (zc->zc_cookie & ZPOOL_NO_REWIND) { 4026468c413aSTim Haley error = spa_open(zc->zc_name, &spa, FTAG); 4027468c413aSTim Haley } else { 4028468c413aSTim Haley nvlist_t *policy; 4029468c413aSTim Haley nvlist_t *config = NULL; 4030468c413aSTim Haley 4031468c413aSTim Haley if (zc->zc_nvlist_src == NULL) 4032468c413aSTim Haley return (EINVAL); 4033468c413aSTim Haley 4034468c413aSTim Haley if ((error = get_nvlist(zc->zc_nvlist_src, 4035468c413aSTim Haley zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 4036468c413aSTim Haley error = spa_open_rewind(zc->zc_name, &spa, FTAG, 4037468c413aSTim Haley policy, &config); 4038468c413aSTim Haley if (config != NULL) { 40394b964adaSGeorge Wilson int err; 40404b964adaSGeorge Wilson 40414b964adaSGeorge Wilson if ((err = put_nvlist(zc, config)) != 0) 40424b964adaSGeorge Wilson error = err; 4043468c413aSTim Haley nvlist_free(config); 4044468c413aSTim Haley } 4045468c413aSTim Haley nvlist_free(policy); 4046468c413aSTim Haley } 4047468c413aSTim Haley } 4048468c413aSTim Haley 4049468c413aSTim Haley if (error) 4050ea8dc4b6Seschrock return (error); 4051ea8dc4b6Seschrock 40528f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 4053ea8dc4b6Seschrock 4054e9dbad6fSeschrock if (zc->zc_guid == 0) { 4055ea8dc4b6Seschrock vd = NULL; 4056c5904d13Seschrock } else { 4057c5904d13Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 4058fa94a07fSbrendan if (vd == NULL) { 4059e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, ENODEV); 4060fa94a07fSbrendan spa_close(spa, FTAG); 4061fa94a07fSbrendan return (ENODEV); 4062fa94a07fSbrendan } 4063ea8dc4b6Seschrock } 4064ea8dc4b6Seschrock 4065e14bb325SJeff Bonwick vdev_clear(spa, vd); 4066e14bb325SJeff Bonwick 4067e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 4068ea8dc4b6Seschrock 4069e14bb325SJeff Bonwick /* 4070e14bb325SJeff Bonwick * Resume any suspended I/Os. 4071e14bb325SJeff Bonwick */ 407254d692b7SGeorge Wilson if (zio_resume(spa) != 0) 407354d692b7SGeorge Wilson error = EIO; 4074ea8dc4b6Seschrock 4075ea8dc4b6Seschrock spa_close(spa, FTAG); 4076ea8dc4b6Seschrock 407754d692b7SGeorge Wilson return (error); 4078ea8dc4b6Seschrock } 4079ea8dc4b6Seschrock 40804263d13fSGeorge Wilson static int 40814263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc) 40824263d13fSGeorge Wilson { 40834263d13fSGeorge Wilson spa_t *spa; 40844263d13fSGeorge Wilson int error; 40854263d13fSGeorge Wilson 40864263d13fSGeorge Wilson error = spa_open(zc->zc_name, &spa, FTAG); 40874263d13fSGeorge Wilson if (error) 40884263d13fSGeorge Wilson return (error); 40894263d13fSGeorge Wilson 40904263d13fSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 40914263d13fSGeorge Wilson vdev_reopen(spa->spa_root_vdev); 40924263d13fSGeorge Wilson (void) spa_vdev_state_exit(spa, NULL, 0); 40934263d13fSGeorge Wilson spa_close(spa, FTAG); 40944263d13fSGeorge Wilson return (0); 40954263d13fSGeorge Wilson } 40963cb34c60Sahrens /* 40973cb34c60Sahrens * inputs: 40983cb34c60Sahrens * zc_name name of filesystem 40993cb34c60Sahrens * zc_value name of origin snapshot 41003cb34c60Sahrens * 4101681d9761SEric Taylor * outputs: 4102681d9761SEric Taylor * zc_string name of conflicting snapshot, if there is one 41033cb34c60Sahrens */ 410499653d4eSeschrock static int 410599653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc) 410699653d4eSeschrock { 41070b69c2f0Sahrens char *cp; 41080b69c2f0Sahrens 41090b69c2f0Sahrens /* 41100b69c2f0Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 41110b69c2f0Sahrens * it's easier. 41120b69c2f0Sahrens */ 4113e9dbad6fSeschrock cp = strchr(zc->zc_value, '@'); 41140b69c2f0Sahrens if (cp) 41150b69c2f0Sahrens *cp = '\0'; 4116e9dbad6fSeschrock (void) dmu_objset_find(zc->zc_value, 41170b69c2f0Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 4118681d9761SEric Taylor return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 411999653d4eSeschrock } 412099653d4eSeschrock 412114843421SMatthew Ahrens /* 412214843421SMatthew Ahrens * Retrieve a single {user|group}{used|quota}@... property. 412314843421SMatthew Ahrens * 412414843421SMatthew Ahrens * inputs: 412514843421SMatthew Ahrens * zc_name name of filesystem 412614843421SMatthew Ahrens * zc_objset_type zfs_userquota_prop_t 412714843421SMatthew Ahrens * zc_value domain name (eg. "S-1-234-567-89") 412814843421SMatthew Ahrens * zc_guid RID/UID/GID 412914843421SMatthew Ahrens * 413014843421SMatthew Ahrens * outputs: 413114843421SMatthew Ahrens * zc_cookie property value 413214843421SMatthew Ahrens */ 413314843421SMatthew Ahrens static int 413414843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc) 413514843421SMatthew Ahrens { 413614843421SMatthew Ahrens zfsvfs_t *zfsvfs; 413714843421SMatthew Ahrens int error; 413814843421SMatthew Ahrens 413914843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 414014843421SMatthew Ahrens return (EINVAL); 414114843421SMatthew Ahrens 41421412a1a2SMark Shellenbaum error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 414314843421SMatthew Ahrens if (error) 414414843421SMatthew Ahrens return (error); 414514843421SMatthew Ahrens 414614843421SMatthew Ahrens error = zfs_userspace_one(zfsvfs, 414714843421SMatthew Ahrens zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 414814843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG); 414914843421SMatthew Ahrens 415014843421SMatthew Ahrens return (error); 415114843421SMatthew Ahrens } 415214843421SMatthew Ahrens 415314843421SMatthew Ahrens /* 415414843421SMatthew Ahrens * inputs: 415514843421SMatthew Ahrens * zc_name name of filesystem 415614843421SMatthew Ahrens * zc_cookie zap cursor 415714843421SMatthew Ahrens * zc_objset_type zfs_userquota_prop_t 415814843421SMatthew Ahrens * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 415914843421SMatthew Ahrens * 416014843421SMatthew Ahrens * outputs: 416114843421SMatthew Ahrens * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 416214843421SMatthew Ahrens * zc_cookie zap cursor 416314843421SMatthew Ahrens */ 416414843421SMatthew Ahrens static int 416514843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc) 416614843421SMatthew Ahrens { 416714843421SMatthew Ahrens zfsvfs_t *zfsvfs; 4168eeb85002STim Haley int bufsize = zc->zc_nvlist_dst_size; 416914843421SMatthew Ahrens 4170eeb85002STim Haley if (bufsize <= 0) 4171eeb85002STim Haley return (ENOMEM); 4172eeb85002STim Haley 41731412a1a2SMark Shellenbaum int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 417414843421SMatthew Ahrens if (error) 417514843421SMatthew Ahrens return (error); 417614843421SMatthew Ahrens 417714843421SMatthew Ahrens void *buf = kmem_alloc(bufsize, KM_SLEEP); 417814843421SMatthew Ahrens 417914843421SMatthew Ahrens error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 418014843421SMatthew Ahrens buf, &zc->zc_nvlist_dst_size); 418114843421SMatthew Ahrens 418214843421SMatthew Ahrens if (error == 0) { 418314843421SMatthew Ahrens error = xcopyout(buf, 418414843421SMatthew Ahrens (void *)(uintptr_t)zc->zc_nvlist_dst, 418514843421SMatthew Ahrens zc->zc_nvlist_dst_size); 418614843421SMatthew Ahrens } 418714843421SMatthew Ahrens kmem_free(buf, bufsize); 418814843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG); 418914843421SMatthew Ahrens 419014843421SMatthew Ahrens return (error); 419114843421SMatthew Ahrens } 419214843421SMatthew Ahrens 419314843421SMatthew Ahrens /* 419414843421SMatthew Ahrens * inputs: 419514843421SMatthew Ahrens * zc_name name of filesystem 419614843421SMatthew Ahrens * 419714843421SMatthew Ahrens * outputs: 419814843421SMatthew Ahrens * none 419914843421SMatthew Ahrens */ 420014843421SMatthew Ahrens static int 420114843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 420214843421SMatthew Ahrens { 420314843421SMatthew Ahrens objset_t *os; 42041195e687SMark J Musante int error = 0; 420514843421SMatthew Ahrens zfsvfs_t *zfsvfs; 420614843421SMatthew Ahrens 420714843421SMatthew Ahrens if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 4208503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 420914843421SMatthew Ahrens /* 421014843421SMatthew Ahrens * If userused is not enabled, it may be because the 421114843421SMatthew Ahrens * objset needs to be closed & reopened (to grow the 421214843421SMatthew Ahrens * objset_phys_t). Suspend/resume the fs will do that. 421314843421SMatthew Ahrens */ 4214503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs); 4215503ad85cSMatthew Ahrens if (error == 0) 4216503ad85cSMatthew Ahrens error = zfs_resume_fs(zfsvfs, zc->zc_name); 421714843421SMatthew Ahrens } 421814843421SMatthew Ahrens if (error == 0) 421914843421SMatthew Ahrens error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 422014843421SMatthew Ahrens VFS_RELE(zfsvfs->z_vfs); 422114843421SMatthew Ahrens } else { 4222503ad85cSMatthew Ahrens /* XXX kind of reading contents without owning */ 4223503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os); 422414843421SMatthew Ahrens if (error) 422514843421SMatthew Ahrens return (error); 422614843421SMatthew Ahrens 422714843421SMatthew Ahrens error = dmu_objset_userspace_upgrade(os); 4228503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 422914843421SMatthew Ahrens } 423014843421SMatthew Ahrens 423114843421SMatthew Ahrens return (error); 423214843421SMatthew Ahrens } 423314843421SMatthew Ahrens 4234ecd6cf80Smarks /* 4235ecd6cf80Smarks * We don't want to have a hard dependency 4236ecd6cf80Smarks * against some special symbols in sharefs 4237da6c28aaSamw * nfs, and smbsrv. Determine them if needed when 4238ecd6cf80Smarks * the first file system is shared. 4239da6c28aaSamw * Neither sharefs, nfs or smbsrv are unloadable modules. 4240ecd6cf80Smarks */ 4241da6c28aaSamw int (*znfsexport_fs)(void *arg); 4242ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 4243da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 4244da6c28aaSamw 4245da6c28aaSamw int zfs_nfsshare_inited; 4246da6c28aaSamw int zfs_smbshare_inited; 4247ecd6cf80Smarks 4248ecd6cf80Smarks ddi_modhandle_t nfs_mod; 4249ecd6cf80Smarks ddi_modhandle_t sharefs_mod; 4250da6c28aaSamw ddi_modhandle_t smbsrv_mod; 4251ecd6cf80Smarks kmutex_t zfs_share_lock; 4252ecd6cf80Smarks 4253da6c28aaSamw static int 4254da6c28aaSamw zfs_init_sharefs() 4255da6c28aaSamw { 4256da6c28aaSamw int error; 4257da6c28aaSamw 4258da6c28aaSamw ASSERT(MUTEX_HELD(&zfs_share_lock)); 4259da6c28aaSamw /* Both NFS and SMB shares also require sharetab support. */ 4260da6c28aaSamw if (sharefs_mod == NULL && ((sharefs_mod = 4261da6c28aaSamw ddi_modopen("fs/sharefs", 4262da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) { 4263da6c28aaSamw return (ENOSYS); 4264da6c28aaSamw } 4265da6c28aaSamw if (zshare_fs == NULL && ((zshare_fs = 4266da6c28aaSamw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 4267da6c28aaSamw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 4268da6c28aaSamw return (ENOSYS); 4269da6c28aaSamw } 4270da6c28aaSamw return (0); 4271da6c28aaSamw } 4272da6c28aaSamw 4273ecd6cf80Smarks static int 4274ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc) 4275ecd6cf80Smarks { 4276ecd6cf80Smarks int error; 4277ecd6cf80Smarks int opcode; 4278ecd6cf80Smarks 4279da6c28aaSamw switch (zc->zc_share.z_sharetype) { 4280da6c28aaSamw case ZFS_SHARE_NFS: 4281da6c28aaSamw case ZFS_UNSHARE_NFS: 4282da6c28aaSamw if (zfs_nfsshare_inited == 0) { 4283da6c28aaSamw mutex_enter(&zfs_share_lock); 4284da6c28aaSamw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 4285da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) { 4286da6c28aaSamw mutex_exit(&zfs_share_lock); 4287da6c28aaSamw return (ENOSYS); 4288da6c28aaSamw } 4289da6c28aaSamw if (znfsexport_fs == NULL && 4290da6c28aaSamw ((znfsexport_fs = (int (*)(void *)) 4291da6c28aaSamw ddi_modsym(nfs_mod, 4292da6c28aaSamw "nfs_export", &error)) == NULL)) { 4293da6c28aaSamw mutex_exit(&zfs_share_lock); 4294da6c28aaSamw return (ENOSYS); 4295da6c28aaSamw } 4296da6c28aaSamw error = zfs_init_sharefs(); 4297da6c28aaSamw if (error) { 4298da6c28aaSamw mutex_exit(&zfs_share_lock); 4299da6c28aaSamw return (ENOSYS); 4300da6c28aaSamw } 4301da6c28aaSamw zfs_nfsshare_inited = 1; 4302ecd6cf80Smarks mutex_exit(&zfs_share_lock); 4303ecd6cf80Smarks } 4304da6c28aaSamw break; 4305da6c28aaSamw case ZFS_SHARE_SMB: 4306da6c28aaSamw case ZFS_UNSHARE_SMB: 4307da6c28aaSamw if (zfs_smbshare_inited == 0) { 4308da6c28aaSamw mutex_enter(&zfs_share_lock); 4309da6c28aaSamw if (smbsrv_mod == NULL && ((smbsrv_mod = 4310da6c28aaSamw ddi_modopen("drv/smbsrv", 4311da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) { 4312da6c28aaSamw mutex_exit(&zfs_share_lock); 4313da6c28aaSamw return (ENOSYS); 4314da6c28aaSamw } 4315da6c28aaSamw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 4316da6c28aaSamw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 4317faa1795aSjb "smb_server_share", &error)) == NULL)) { 4318da6c28aaSamw mutex_exit(&zfs_share_lock); 4319da6c28aaSamw return (ENOSYS); 4320da6c28aaSamw } 4321da6c28aaSamw error = zfs_init_sharefs(); 4322da6c28aaSamw if (error) { 4323da6c28aaSamw mutex_exit(&zfs_share_lock); 4324da6c28aaSamw return (ENOSYS); 4325da6c28aaSamw } 4326da6c28aaSamw zfs_smbshare_inited = 1; 4327ecd6cf80Smarks mutex_exit(&zfs_share_lock); 4328ecd6cf80Smarks } 4329da6c28aaSamw break; 4330da6c28aaSamw default: 4331da6c28aaSamw return (EINVAL); 4332da6c28aaSamw } 4333ecd6cf80Smarks 4334da6c28aaSamw switch (zc->zc_share.z_sharetype) { 4335da6c28aaSamw case ZFS_SHARE_NFS: 4336da6c28aaSamw case ZFS_UNSHARE_NFS: 4337da6c28aaSamw if (error = 4338da6c28aaSamw znfsexport_fs((void *) 4339da6c28aaSamw (uintptr_t)zc->zc_share.z_exportdata)) 4340da6c28aaSamw return (error); 4341da6c28aaSamw break; 4342da6c28aaSamw case ZFS_SHARE_SMB: 4343da6c28aaSamw case ZFS_UNSHARE_SMB: 4344da6c28aaSamw if (error = zsmbexport_fs((void *) 4345da6c28aaSamw (uintptr_t)zc->zc_share.z_exportdata, 4346da6c28aaSamw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 4347743a77edSAlan Wright B_TRUE: B_FALSE)) { 4348da6c28aaSamw return (error); 4349ecd6cf80Smarks } 4350da6c28aaSamw break; 4351ecd6cf80Smarks } 4352ecd6cf80Smarks 4353da6c28aaSamw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 4354da6c28aaSamw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 4355ecd6cf80Smarks SHAREFS_ADD : SHAREFS_REMOVE; 4356ecd6cf80Smarks 4357da6c28aaSamw /* 4358da6c28aaSamw * Add or remove share from sharetab 4359da6c28aaSamw */ 4360ecd6cf80Smarks error = zshare_fs(opcode, 4361ecd6cf80Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 4362ecd6cf80Smarks zc->zc_share.z_sharemax); 4363ecd6cf80Smarks 4364ecd6cf80Smarks return (error); 4365ecd6cf80Smarks 4366ecd6cf80Smarks } 4367ecd6cf80Smarks 4368743a77edSAlan Wright ace_t full_access[] = { 4369743a77edSAlan Wright {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 4370743a77edSAlan Wright }; 4371743a77edSAlan Wright 437299d5e173STim Haley /* 437399d5e173STim Haley * inputs: 437499d5e173STim Haley * zc_name name of containing filesystem 437599d5e173STim Haley * zc_obj object # beyond which we want next in-use object # 437699d5e173STim Haley * 437799d5e173STim Haley * outputs: 437899d5e173STim Haley * zc_obj next in-use object # 437999d5e173STim Haley */ 438099d5e173STim Haley static int 438199d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc) 438299d5e173STim Haley { 438399d5e173STim Haley objset_t *os = NULL; 438499d5e173STim Haley int error; 438599d5e173STim Haley 438699d5e173STim Haley error = dmu_objset_hold(zc->zc_name, FTAG, &os); 438799d5e173STim Haley if (error) 438899d5e173STim Haley return (error); 438999d5e173STim Haley 439099d5e173STim Haley error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 439199d5e173STim Haley os->os_dsl_dataset->ds_phys->ds_prev_snap_txg); 439299d5e173STim Haley 439399d5e173STim Haley dmu_objset_rele(os, FTAG); 439499d5e173STim Haley return (error); 439599d5e173STim Haley } 439699d5e173STim Haley 439799d5e173STim Haley /* 439899d5e173STim Haley * inputs: 439999d5e173STim Haley * zc_name name of filesystem 440099d5e173STim Haley * zc_value prefix name for snapshot 440199d5e173STim Haley * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 440299d5e173STim Haley * 440399d5e173STim Haley * outputs: 440499d5e173STim Haley */ 440599d5e173STim Haley static int 440699d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc) 440799d5e173STim Haley { 440899d5e173STim Haley char *snap_name; 440999d5e173STim Haley int error; 441099d5e173STim Haley 441199d5e173STim Haley snap_name = kmem_asprintf("%s-%016llx", zc->zc_value, 441299d5e173STim Haley (u_longlong_t)ddi_get_lbolt64()); 441399d5e173STim Haley 441499d5e173STim Haley if (strlen(snap_name) >= MAXNAMELEN) { 441599d5e173STim Haley strfree(snap_name); 441699d5e173STim Haley return (E2BIG); 441799d5e173STim Haley } 441899d5e173STim Haley 441999d5e173STim Haley error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name, 442099d5e173STim Haley NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd); 442199d5e173STim Haley if (error != 0) { 442299d5e173STim Haley strfree(snap_name); 442399d5e173STim Haley return (error); 442499d5e173STim Haley } 442599d5e173STim Haley 442699d5e173STim Haley (void) strcpy(zc->zc_value, snap_name); 442799d5e173STim Haley strfree(snap_name); 442899d5e173STim Haley return (0); 442999d5e173STim Haley } 443099d5e173STim Haley 443199d5e173STim Haley /* 443299d5e173STim Haley * inputs: 443399d5e173STim Haley * zc_name name of "to" snapshot 443499d5e173STim Haley * zc_value name of "from" snapshot 443599d5e173STim Haley * zc_cookie file descriptor to write diff data on 443699d5e173STim Haley * 443799d5e173STim Haley * outputs: 443899d5e173STim Haley * dmu_diff_record_t's to the file descriptor 443999d5e173STim Haley */ 444099d5e173STim Haley static int 444199d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc) 444299d5e173STim Haley { 444399d5e173STim Haley objset_t *fromsnap; 444499d5e173STim Haley objset_t *tosnap; 444599d5e173STim Haley file_t *fp; 444699d5e173STim Haley offset_t off; 444799d5e173STim Haley int error; 444899d5e173STim Haley 444999d5e173STim Haley error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap); 445099d5e173STim Haley if (error) 445199d5e173STim Haley return (error); 445299d5e173STim Haley 445399d5e173STim Haley error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap); 445499d5e173STim Haley if (error) { 445599d5e173STim Haley dmu_objset_rele(tosnap, FTAG); 445699d5e173STim Haley return (error); 445799d5e173STim Haley } 445899d5e173STim Haley 445999d5e173STim Haley fp = getf(zc->zc_cookie); 446099d5e173STim Haley if (fp == NULL) { 446199d5e173STim Haley dmu_objset_rele(fromsnap, FTAG); 446299d5e173STim Haley dmu_objset_rele(tosnap, FTAG); 446399d5e173STim Haley return (EBADF); 446499d5e173STim Haley } 446599d5e173STim Haley 446699d5e173STim Haley off = fp->f_offset; 446799d5e173STim Haley 446899d5e173STim Haley error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off); 446999d5e173STim Haley 447099d5e173STim Haley if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 447199d5e173STim Haley fp->f_offset = off; 447299d5e173STim Haley releasef(zc->zc_cookie); 447399d5e173STim Haley 447499d5e173STim Haley dmu_objset_rele(fromsnap, FTAG); 447599d5e173STim Haley dmu_objset_rele(tosnap, FTAG); 447699d5e173STim Haley return (error); 447799d5e173STim Haley } 447899d5e173STim Haley 4479743a77edSAlan Wright /* 4480743a77edSAlan Wright * Remove all ACL files in shares dir 4481743a77edSAlan Wright */ 4482743a77edSAlan Wright static int 4483743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp) 4484743a77edSAlan Wright { 4485743a77edSAlan Wright zap_cursor_t zc; 4486743a77edSAlan Wright zap_attribute_t zap; 4487743a77edSAlan Wright zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 4488743a77edSAlan Wright int error; 4489743a77edSAlan Wright 4490743a77edSAlan Wright for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 4491743a77edSAlan Wright (error = zap_cursor_retrieve(&zc, &zap)) == 0; 4492743a77edSAlan Wright zap_cursor_advance(&zc)) { 4493743a77edSAlan Wright if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 4494743a77edSAlan Wright NULL, 0)) != 0) 4495743a77edSAlan Wright break; 4496743a77edSAlan Wright } 4497743a77edSAlan Wright zap_cursor_fini(&zc); 4498743a77edSAlan Wright return (error); 4499743a77edSAlan Wright } 4500743a77edSAlan Wright 4501743a77edSAlan Wright static int 4502743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc) 4503743a77edSAlan Wright { 4504743a77edSAlan Wright vnode_t *vp; 4505743a77edSAlan Wright znode_t *dzp; 4506743a77edSAlan Wright vnode_t *resourcevp = NULL; 4507743a77edSAlan Wright znode_t *sharedir; 4508743a77edSAlan Wright zfsvfs_t *zfsvfs; 4509743a77edSAlan Wright nvlist_t *nvlist; 4510743a77edSAlan Wright char *src, *target; 4511743a77edSAlan Wright vattr_t vattr; 4512743a77edSAlan Wright vsecattr_t vsec; 4513743a77edSAlan Wright int error = 0; 4514743a77edSAlan Wright 4515743a77edSAlan Wright if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 4516743a77edSAlan Wright NO_FOLLOW, NULL, &vp)) != 0) 4517743a77edSAlan Wright return (error); 4518743a77edSAlan Wright 4519743a77edSAlan Wright /* Now make sure mntpnt and dataset are ZFS */ 4520743a77edSAlan Wright 4521743a77edSAlan Wright if (vp->v_vfsp->vfs_fstype != zfsfstype || 4522743a77edSAlan Wright (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 4523743a77edSAlan Wright zc->zc_name) != 0)) { 4524743a77edSAlan Wright VN_RELE(vp); 4525743a77edSAlan Wright return (EINVAL); 4526743a77edSAlan Wright } 4527743a77edSAlan Wright 4528743a77edSAlan Wright dzp = VTOZ(vp); 4529743a77edSAlan Wright zfsvfs = dzp->z_zfsvfs; 4530743a77edSAlan Wright ZFS_ENTER(zfsvfs); 4531743a77edSAlan Wright 45329e1320c0SMark Shellenbaum /* 45339e1320c0SMark Shellenbaum * Create share dir if its missing. 45349e1320c0SMark Shellenbaum */ 45359e1320c0SMark Shellenbaum mutex_enter(&zfsvfs->z_lock); 45369e1320c0SMark Shellenbaum if (zfsvfs->z_shares_dir == 0) { 45379e1320c0SMark Shellenbaum dmu_tx_t *tx; 45389e1320c0SMark Shellenbaum 45399e1320c0SMark Shellenbaum tx = dmu_tx_create(zfsvfs->z_os); 45409e1320c0SMark Shellenbaum dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 45419e1320c0SMark Shellenbaum ZFS_SHARES_DIR); 45429e1320c0SMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 45439e1320c0SMark Shellenbaum error = dmu_tx_assign(tx, TXG_WAIT); 45449e1320c0SMark Shellenbaum if (error) { 45459e1320c0SMark Shellenbaum dmu_tx_abort(tx); 45469e1320c0SMark Shellenbaum } else { 45479e1320c0SMark Shellenbaum error = zfs_create_share_dir(zfsvfs, tx); 45489e1320c0SMark Shellenbaum dmu_tx_commit(tx); 45499e1320c0SMark Shellenbaum } 45509e1320c0SMark Shellenbaum if (error) { 45519e1320c0SMark Shellenbaum mutex_exit(&zfsvfs->z_lock); 45529e1320c0SMark Shellenbaum VN_RELE(vp); 45539e1320c0SMark Shellenbaum ZFS_EXIT(zfsvfs); 45549e1320c0SMark Shellenbaum return (error); 45559e1320c0SMark Shellenbaum } 45569e1320c0SMark Shellenbaum } 45579e1320c0SMark Shellenbaum mutex_exit(&zfsvfs->z_lock); 45589e1320c0SMark Shellenbaum 45599e1320c0SMark Shellenbaum ASSERT(zfsvfs->z_shares_dir); 4560743a77edSAlan Wright if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 45619e1320c0SMark Shellenbaum VN_RELE(vp); 4562743a77edSAlan Wright ZFS_EXIT(zfsvfs); 4563743a77edSAlan Wright return (error); 4564743a77edSAlan Wright } 4565743a77edSAlan Wright 4566743a77edSAlan Wright switch (zc->zc_cookie) { 4567743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 4568743a77edSAlan Wright vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 4569743a77edSAlan Wright vattr.va_type = VREG; 4570743a77edSAlan Wright vattr.va_mode = S_IFREG|0777; 4571743a77edSAlan Wright vattr.va_uid = 0; 4572743a77edSAlan Wright vattr.va_gid = 0; 4573743a77edSAlan Wright 4574743a77edSAlan Wright vsec.vsa_mask = VSA_ACE; 4575743a77edSAlan Wright vsec.vsa_aclentp = &full_access; 4576743a77edSAlan Wright vsec.vsa_aclentsz = sizeof (full_access); 4577743a77edSAlan Wright vsec.vsa_aclcnt = 1; 4578743a77edSAlan Wright 4579743a77edSAlan Wright error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 4580743a77edSAlan Wright &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 4581743a77edSAlan Wright if (resourcevp) 4582743a77edSAlan Wright VN_RELE(resourcevp); 4583743a77edSAlan Wright break; 4584743a77edSAlan Wright 4585743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 4586743a77edSAlan Wright error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 4587743a77edSAlan Wright NULL, 0); 4588743a77edSAlan Wright break; 4589743a77edSAlan Wright 4590743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 4591743a77edSAlan Wright if ((error = get_nvlist(zc->zc_nvlist_src, 4592478ed9adSEric Taylor zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 4593743a77edSAlan Wright VN_RELE(vp); 4594743a77edSAlan Wright ZFS_EXIT(zfsvfs); 4595743a77edSAlan Wright return (error); 4596743a77edSAlan Wright } 4597743a77edSAlan Wright if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 4598743a77edSAlan Wright nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 4599743a77edSAlan Wright &target)) { 4600743a77edSAlan Wright VN_RELE(vp); 460189459e17SMark Shellenbaum VN_RELE(ZTOV(sharedir)); 4602743a77edSAlan Wright ZFS_EXIT(zfsvfs); 46031195e687SMark J Musante nvlist_free(nvlist); 4604743a77edSAlan Wright return (error); 4605743a77edSAlan Wright } 4606743a77edSAlan Wright error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 4607743a77edSAlan Wright kcred, NULL, 0); 4608743a77edSAlan Wright nvlist_free(nvlist); 4609743a77edSAlan Wright break; 4610743a77edSAlan Wright 4611743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 4612743a77edSAlan Wright error = zfs_smb_acl_purge(sharedir); 4613743a77edSAlan Wright break; 4614743a77edSAlan Wright 4615743a77edSAlan Wright default: 4616743a77edSAlan Wright error = EINVAL; 4617743a77edSAlan Wright break; 4618743a77edSAlan Wright } 4619743a77edSAlan Wright 4620743a77edSAlan Wright VN_RELE(vp); 4621743a77edSAlan Wright VN_RELE(ZTOV(sharedir)); 4622743a77edSAlan Wright 4623743a77edSAlan Wright ZFS_EXIT(zfsvfs); 4624743a77edSAlan Wright 4625743a77edSAlan Wright return (error); 4626743a77edSAlan Wright } 4627743a77edSAlan Wright 4628842727c2SChris Kirby /* 4629842727c2SChris Kirby * inputs: 4630c99e4bdcSChris Kirby * zc_name name of filesystem 4631c99e4bdcSChris Kirby * zc_value short name of snap 4632c99e4bdcSChris Kirby * zc_string user-supplied tag for this hold 4633c99e4bdcSChris Kirby * zc_cookie recursive flag 4634c99e4bdcSChris Kirby * zc_temphold set if hold is temporary 4635c99e4bdcSChris Kirby * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 4636a7f53a56SChris Kirby * zc_sendobj if non-zero, the objid for zc_name@zc_value 4637a7f53a56SChris Kirby * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg 4638842727c2SChris Kirby * 4639842727c2SChris Kirby * outputs: none 4640842727c2SChris Kirby */ 4641842727c2SChris Kirby static int 4642842727c2SChris Kirby zfs_ioc_hold(zfs_cmd_t *zc) 4643842727c2SChris Kirby { 4644842727c2SChris Kirby boolean_t recursive = zc->zc_cookie; 4645a7f53a56SChris Kirby spa_t *spa; 4646a7f53a56SChris Kirby dsl_pool_t *dp; 4647a7f53a56SChris Kirby dsl_dataset_t *ds; 4648a7f53a56SChris Kirby int error; 4649a7f53a56SChris Kirby minor_t minor = 0; 4650842727c2SChris Kirby 4651842727c2SChris Kirby if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 4652842727c2SChris Kirby return (EINVAL); 4653842727c2SChris Kirby 4654a7f53a56SChris Kirby if (zc->zc_sendobj == 0) { 4655a7f53a56SChris Kirby return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value, 4656a7f53a56SChris Kirby zc->zc_string, recursive, zc->zc_temphold, 4657a7f53a56SChris Kirby zc->zc_cleanup_fd)); 4658a7f53a56SChris Kirby } 4659a7f53a56SChris Kirby 4660a7f53a56SChris Kirby if (recursive) 4661a7f53a56SChris Kirby return (EINVAL); 4662a7f53a56SChris Kirby 4663a7f53a56SChris Kirby error = spa_open(zc->zc_name, &spa, FTAG); 4664a7f53a56SChris Kirby if (error) 4665a7f53a56SChris Kirby return (error); 4666a7f53a56SChris Kirby 4667a7f53a56SChris Kirby dp = spa_get_dsl(spa); 4668a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 4669a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 4670a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 4671a7f53a56SChris Kirby spa_close(spa, FTAG); 4672a7f53a56SChris Kirby if (error) 4673a7f53a56SChris Kirby return (error); 4674a7f53a56SChris Kirby 4675a7f53a56SChris Kirby /* 4676a7f53a56SChris Kirby * Until we have a hold on this snapshot, it's possible that 4677a7f53a56SChris Kirby * zc_sendobj could've been destroyed and reused as part 4678a7f53a56SChris Kirby * of a later txg. Make sure we're looking at the right object. 4679a7f53a56SChris Kirby */ 4680a7f53a56SChris Kirby if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) { 4681a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 4682a7f53a56SChris Kirby return (ENOENT); 4683a7f53a56SChris Kirby } 4684a7f53a56SChris Kirby 4685a7f53a56SChris Kirby if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) { 4686a7f53a56SChris Kirby error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor); 4687a7f53a56SChris Kirby if (error) { 4688a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 4689a7f53a56SChris Kirby return (error); 4690a7f53a56SChris Kirby } 4691a7f53a56SChris Kirby } 4692a7f53a56SChris Kirby 4693a7f53a56SChris Kirby error = dsl_dataset_user_hold_for_send(ds, zc->zc_string, 4694a7f53a56SChris Kirby zc->zc_temphold); 4695a7f53a56SChris Kirby if (minor != 0) { 4696a7f53a56SChris Kirby if (error == 0) { 4697a7f53a56SChris Kirby dsl_register_onexit_hold_cleanup(ds, zc->zc_string, 4698a7f53a56SChris Kirby minor); 4699a7f53a56SChris Kirby } 4700a7f53a56SChris Kirby zfs_onexit_fd_rele(zc->zc_cleanup_fd); 4701a7f53a56SChris Kirby } 4702a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 4703a7f53a56SChris Kirby 4704a7f53a56SChris Kirby return (error); 4705842727c2SChris Kirby } 4706842727c2SChris Kirby 4707842727c2SChris Kirby /* 4708842727c2SChris Kirby * inputs: 4709c99e4bdcSChris Kirby * zc_name name of dataset from which we're releasing a user hold 4710842727c2SChris Kirby * zc_value short name of snap 4711c99e4bdcSChris Kirby * zc_string user-supplied tag for this hold 4712842727c2SChris Kirby * zc_cookie recursive flag 4713842727c2SChris Kirby * 4714c99e4bdcSChris Kirby * outputs: none 4715842727c2SChris Kirby */ 4716842727c2SChris Kirby static int 4717842727c2SChris Kirby zfs_ioc_release(zfs_cmd_t *zc) 4718842727c2SChris Kirby { 4719842727c2SChris Kirby boolean_t recursive = zc->zc_cookie; 4720842727c2SChris Kirby 4721842727c2SChris Kirby if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 4722842727c2SChris Kirby return (EINVAL); 4723842727c2SChris Kirby 4724842727c2SChris Kirby return (dsl_dataset_user_release(zc->zc_name, zc->zc_value, 4725842727c2SChris Kirby zc->zc_string, recursive)); 4726842727c2SChris Kirby } 4727842727c2SChris Kirby 4728842727c2SChris Kirby /* 4729842727c2SChris Kirby * inputs: 4730842727c2SChris Kirby * zc_name name of filesystem 4731842727c2SChris Kirby * 4732842727c2SChris Kirby * outputs: 4733842727c2SChris Kirby * zc_nvlist_src{_size} nvlist of snapshot holds 4734842727c2SChris Kirby */ 4735842727c2SChris Kirby static int 4736842727c2SChris Kirby zfs_ioc_get_holds(zfs_cmd_t *zc) 4737842727c2SChris Kirby { 4738842727c2SChris Kirby nvlist_t *nvp; 4739842727c2SChris Kirby int error; 4740842727c2SChris Kirby 4741842727c2SChris Kirby if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) { 4742842727c2SChris Kirby error = put_nvlist(zc, nvp); 4743842727c2SChris Kirby nvlist_free(nvp); 4744842727c2SChris Kirby } 4745842727c2SChris Kirby 4746842727c2SChris Kirby return (error); 4747842727c2SChris Kirby } 4748842727c2SChris Kirby 474919b94df9SMatthew Ahrens /* 475019b94df9SMatthew Ahrens * inputs: 475119b94df9SMatthew Ahrens * zc_name name of new filesystem or snapshot 475219b94df9SMatthew Ahrens * zc_value full name of old snapshot 475319b94df9SMatthew Ahrens * 475419b94df9SMatthew Ahrens * outputs: 475519b94df9SMatthew Ahrens * zc_cookie space in bytes 475619b94df9SMatthew Ahrens * zc_objset_type compressed space in bytes 475719b94df9SMatthew Ahrens * zc_perm_action uncompressed space in bytes 475819b94df9SMatthew Ahrens */ 475919b94df9SMatthew Ahrens static int 476019b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc) 476119b94df9SMatthew Ahrens { 476219b94df9SMatthew Ahrens int error; 476319b94df9SMatthew Ahrens dsl_dataset_t *new, *old; 476419b94df9SMatthew Ahrens 476519b94df9SMatthew Ahrens error = dsl_dataset_hold(zc->zc_name, FTAG, &new); 476619b94df9SMatthew Ahrens if (error != 0) 476719b94df9SMatthew Ahrens return (error); 476819b94df9SMatthew Ahrens error = dsl_dataset_hold(zc->zc_value, FTAG, &old); 476919b94df9SMatthew Ahrens if (error != 0) { 477019b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 477119b94df9SMatthew Ahrens return (error); 477219b94df9SMatthew Ahrens } 477319b94df9SMatthew Ahrens 477419b94df9SMatthew Ahrens error = dsl_dataset_space_written(old, new, &zc->zc_cookie, 477519b94df9SMatthew Ahrens &zc->zc_objset_type, &zc->zc_perm_action); 477619b94df9SMatthew Ahrens dsl_dataset_rele(old, FTAG); 477719b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 477819b94df9SMatthew Ahrens return (error); 477919b94df9SMatthew Ahrens } 478019b94df9SMatthew Ahrens 478119b94df9SMatthew Ahrens /* 478219b94df9SMatthew Ahrens * inputs: 478319b94df9SMatthew Ahrens * zc_name full name of last snapshot 478419b94df9SMatthew Ahrens * zc_value full name of first snapshot 478519b94df9SMatthew Ahrens * 478619b94df9SMatthew Ahrens * outputs: 478719b94df9SMatthew Ahrens * zc_cookie space in bytes 478819b94df9SMatthew Ahrens * zc_objset_type compressed space in bytes 478919b94df9SMatthew Ahrens * zc_perm_action uncompressed space in bytes 479019b94df9SMatthew Ahrens */ 479119b94df9SMatthew Ahrens static int 479219b94df9SMatthew Ahrens zfs_ioc_space_snaps(zfs_cmd_t *zc) 479319b94df9SMatthew Ahrens { 479419b94df9SMatthew Ahrens int error; 479519b94df9SMatthew Ahrens dsl_dataset_t *new, *old; 479619b94df9SMatthew Ahrens 479719b94df9SMatthew Ahrens error = dsl_dataset_hold(zc->zc_name, FTAG, &new); 479819b94df9SMatthew Ahrens if (error != 0) 479919b94df9SMatthew Ahrens return (error); 480019b94df9SMatthew Ahrens error = dsl_dataset_hold(zc->zc_value, FTAG, &old); 480119b94df9SMatthew Ahrens if (error != 0) { 480219b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 480319b94df9SMatthew Ahrens return (error); 480419b94df9SMatthew Ahrens } 480519b94df9SMatthew Ahrens 480619b94df9SMatthew Ahrens error = dsl_dataset_space_wouldfree(old, new, &zc->zc_cookie, 480719b94df9SMatthew Ahrens &zc->zc_objset_type, &zc->zc_perm_action); 480819b94df9SMatthew Ahrens dsl_dataset_rele(old, FTAG); 480919b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 481019b94df9SMatthew Ahrens return (error); 481119b94df9SMatthew Ahrens } 481219b94df9SMatthew Ahrens 4813ecd6cf80Smarks /* 48142a6b87f0Sek * pool create, destroy, and export don't log the history as part of 48152a6b87f0Sek * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 48162a6b87f0Sek * do the logging of those commands. 4817ecd6cf80Smarks */ 4818fa9e4066Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 481954d692b7SGeorge Wilson { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4820f9af39baSGeorge Wilson POOL_CHECK_NONE }, 482154d692b7SGeorge Wilson { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4822f9af39baSGeorge Wilson POOL_CHECK_NONE }, 482354d692b7SGeorge Wilson { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4824f9af39baSGeorge Wilson POOL_CHECK_NONE }, 482554d692b7SGeorge Wilson { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4826f9af39baSGeorge Wilson POOL_CHECK_NONE }, 482754d692b7SGeorge Wilson { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE, 4828f9af39baSGeorge Wilson POOL_CHECK_NONE }, 482954d692b7SGeorge Wilson { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE, 4830f9af39baSGeorge Wilson POOL_CHECK_NONE }, 483154d692b7SGeorge Wilson { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE, 4832f9af39baSGeorge Wilson POOL_CHECK_NONE }, 48333f9d6ad7SLin Ling { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4834f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 483554d692b7SGeorge Wilson { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE, 4836f9af39baSGeorge Wilson POOL_CHECK_READONLY }, 483754d692b7SGeorge Wilson { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4838f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 483954d692b7SGeorge Wilson { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4840f9af39baSGeorge Wilson POOL_CHECK_NONE }, 484154d692b7SGeorge Wilson { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4842f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 484354d692b7SGeorge Wilson { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4844f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 484554d692b7SGeorge Wilson { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4846f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 484754d692b7SGeorge Wilson { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4848f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 484954d692b7SGeorge Wilson { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4850f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 485154d692b7SGeorge Wilson { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4852f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 48536809eb4eSEric Schrock { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4854f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 485554d692b7SGeorge Wilson { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4856f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 485754d692b7SGeorge Wilson { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4858f9af39baSGeorge Wilson POOL_CHECK_NONE }, 485954d692b7SGeorge Wilson { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4860f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 486154d692b7SGeorge Wilson { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4862f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 4863f9af39baSGeorge Wilson { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, 4864f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4865f9af39baSGeorge Wilson { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, 4866f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 486754d692b7SGeorge Wilson { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 4868f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 486954d692b7SGeorge Wilson { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE, 4870f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4871f9af39baSGeorge Wilson { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, 4872f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4873f9af39baSGeorge Wilson { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, 4874f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 487519b94df9SMatthew Ahrens { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_FALSE, 4876f9af39baSGeorge Wilson POOL_CHECK_NONE }, 487754d692b7SGeorge Wilson { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 4878f9af39baSGeorge Wilson POOL_CHECK_NONE }, 487954d692b7SGeorge Wilson { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 4880f9af39baSGeorge Wilson POOL_CHECK_NONE }, 488154d692b7SGeorge Wilson { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE, 4882f9af39baSGeorge Wilson POOL_CHECK_NONE }, 488354d692b7SGeorge Wilson { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE, 4884f9af39baSGeorge Wilson POOL_CHECK_NONE }, 4885f9af39baSGeorge Wilson { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4886f9af39baSGeorge Wilson POOL_CHECK_NONE }, 488754d692b7SGeorge Wilson { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE, 4888f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 488954d692b7SGeorge Wilson { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE, 4890f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 489199d5e173STim Haley { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE, 4892f9af39baSGeorge Wilson POOL_CHECK_NONE }, 489399d5e173STim Haley { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, 4894f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 489554d692b7SGeorge Wilson { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4896f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 489754d692b7SGeorge Wilson { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE, 4898f9af39baSGeorge Wilson POOL_CHECK_NONE }, 489954d692b7SGeorge Wilson { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE, 4900f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 490154d692b7SGeorge Wilson { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4902f9af39baSGeorge Wilson POOL_CHECK_NONE }, 4903f9af39baSGeorge Wilson { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, 4904f9af39baSGeorge Wilson POOL_CHECK_NONE }, 490554d692b7SGeorge Wilson { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE, 4906f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 490754d692b7SGeorge Wilson { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE, 4908f9af39baSGeorge Wilson POOL_CHECK_NONE }, 4909f9af39baSGeorge Wilson { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME, 4910f9af39baSGeorge Wilson B_FALSE, POOL_CHECK_NONE }, 4911f9af39baSGeorge Wilson { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME, 4912f9af39baSGeorge Wilson B_FALSE, POOL_CHECK_NONE }, 491314843421SMatthew Ahrens { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 4914f9af39baSGeorge Wilson DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4915f9af39baSGeorge Wilson { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, 4916f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4917842727c2SChris Kirby { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE, 4918f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4919842727c2SChris Kirby { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4920f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 492192241e0bSTom Erickson { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4922f9af39baSGeorge Wilson POOL_CHECK_NONE }, 49231195e687SMark J Musante { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4924f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 492599d5e173STim Haley { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4926f9af39baSGeorge Wilson POOL_CHECK_NONE }, 4927f9af39baSGeorge Wilson { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, 4928f9af39baSGeorge Wilson POOL_CHECK_NONE }, 492999d5e173STim Haley { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME, 4930f9af39baSGeorge Wilson B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 493199d5e173STim Haley { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, 4932e9103aaeSGarrett D'Amore POOL_CHECK_SUSPENDED }, 493319b94df9SMatthew Ahrens { zfs_ioc_space_written, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 493419b94df9SMatthew Ahrens POOL_CHECK_SUSPENDED }, 493519b94df9SMatthew Ahrens { zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 493619b94df9SMatthew Ahrens POOL_CHECK_SUSPENDED }, 493719b94df9SMatthew Ahrens { zfs_ioc_destroy_snaps_nvl, zfs_secpolicy_destroy_recursive, 493819b94df9SMatthew Ahrens DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4939e9103aaeSGarrett D'Amore { zfs_ioc_pool_reguid, zfs_secpolicy_config, POOL_NAME, B_TRUE, 49404263d13fSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 49414263d13fSGeorge Wilson { zfs_ioc_pool_reopen, zfs_secpolicy_config, POOL_NAME, B_TRUE, 49424263d13fSGeorge Wilson POOL_CHECK_SUSPENDED }, 4943*4e3c9f44SBill Pijewski { zfs_ioc_send_progress, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4944*4e3c9f44SBill Pijewski POOL_CHECK_NONE } 4945fa9e4066Sahrens }; 4946fa9e4066Sahrens 494754d692b7SGeorge Wilson int 4948f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type, 4949f9af39baSGeorge Wilson zfs_ioc_poolcheck_t check) 495054d692b7SGeorge Wilson { 495154d692b7SGeorge Wilson spa_t *spa; 495254d692b7SGeorge Wilson int error; 495354d692b7SGeorge Wilson 495454d692b7SGeorge Wilson ASSERT(type == POOL_NAME || type == DATASET_NAME); 495554d692b7SGeorge Wilson 4956f9af39baSGeorge Wilson if (check & POOL_CHECK_NONE) 4957f9af39baSGeorge Wilson return (0); 4958f9af39baSGeorge Wilson 495914843421SMatthew Ahrens error = spa_open(name, &spa, FTAG); 496054d692b7SGeorge Wilson if (error == 0) { 4961f9af39baSGeorge Wilson if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa)) 496254d692b7SGeorge Wilson error = EAGAIN; 4963f9af39baSGeorge Wilson else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa)) 4964f9af39baSGeorge Wilson error = EROFS; 496554d692b7SGeorge Wilson spa_close(spa, FTAG); 496654d692b7SGeorge Wilson } 496754d692b7SGeorge Wilson return (error); 496854d692b7SGeorge Wilson } 496954d692b7SGeorge Wilson 4970c99e4bdcSChris Kirby /* 4971c99e4bdcSChris Kirby * Find a free minor number. 4972c99e4bdcSChris Kirby */ 4973c99e4bdcSChris Kirby minor_t 4974c99e4bdcSChris Kirby zfsdev_minor_alloc(void) 4975c99e4bdcSChris Kirby { 4976c99e4bdcSChris Kirby static minor_t last_minor; 4977c99e4bdcSChris Kirby minor_t m; 4978c99e4bdcSChris Kirby 4979c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 4980c99e4bdcSChris Kirby 4981c99e4bdcSChris Kirby for (m = last_minor + 1; m != last_minor; m++) { 4982c99e4bdcSChris Kirby if (m > ZFSDEV_MAX_MINOR) 4983c99e4bdcSChris Kirby m = 1; 4984c99e4bdcSChris Kirby if (ddi_get_soft_state(zfsdev_state, m) == NULL) { 4985c99e4bdcSChris Kirby last_minor = m; 4986c99e4bdcSChris Kirby return (m); 4987c99e4bdcSChris Kirby } 4988c99e4bdcSChris Kirby } 4989c99e4bdcSChris Kirby 4990c99e4bdcSChris Kirby return (0); 4991c99e4bdcSChris Kirby } 4992c99e4bdcSChris Kirby 4993c99e4bdcSChris Kirby static int 4994c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp) 4995c99e4bdcSChris Kirby { 4996c99e4bdcSChris Kirby minor_t minor; 4997c99e4bdcSChris Kirby zfs_soft_state_t *zs; 4998c99e4bdcSChris Kirby 4999c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 5000c99e4bdcSChris Kirby ASSERT(getminor(*devp) == 0); 5001c99e4bdcSChris Kirby 5002c99e4bdcSChris Kirby minor = zfsdev_minor_alloc(); 5003c99e4bdcSChris Kirby if (minor == 0) 5004c99e4bdcSChris Kirby return (ENXIO); 5005c99e4bdcSChris Kirby 5006c99e4bdcSChris Kirby if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) 5007c99e4bdcSChris Kirby return (EAGAIN); 5008c99e4bdcSChris Kirby 5009c99e4bdcSChris Kirby *devp = makedevice(getemajor(*devp), minor); 5010c99e4bdcSChris Kirby 5011c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, minor); 5012c99e4bdcSChris Kirby zs->zss_type = ZSST_CTLDEV; 5013c99e4bdcSChris Kirby zfs_onexit_init((zfs_onexit_t **)&zs->zss_data); 5014c99e4bdcSChris Kirby 5015c99e4bdcSChris Kirby return (0); 5016c99e4bdcSChris Kirby } 5017c99e4bdcSChris Kirby 5018c99e4bdcSChris Kirby static void 5019c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor) 5020c99e4bdcSChris Kirby { 5021c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 5022c99e4bdcSChris Kirby 5023c99e4bdcSChris Kirby zfs_onexit_destroy(zo); 5024c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5025c99e4bdcSChris Kirby } 5026c99e4bdcSChris Kirby 5027c99e4bdcSChris Kirby void * 5028c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which) 5029c99e4bdcSChris Kirby { 5030c99e4bdcSChris Kirby zfs_soft_state_t *zp; 5031c99e4bdcSChris Kirby 5032c99e4bdcSChris Kirby zp = ddi_get_soft_state(zfsdev_state, minor); 5033c99e4bdcSChris Kirby if (zp == NULL || zp->zss_type != which) 5034c99e4bdcSChris Kirby return (NULL); 5035c99e4bdcSChris Kirby 5036c99e4bdcSChris Kirby return (zp->zss_data); 5037c99e4bdcSChris Kirby } 5038c99e4bdcSChris Kirby 5039c99e4bdcSChris Kirby static int 5040c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr) 5041c99e4bdcSChris Kirby { 5042c99e4bdcSChris Kirby int error = 0; 5043c99e4bdcSChris Kirby 5044c99e4bdcSChris Kirby if (getminor(*devp) != 0) 5045c99e4bdcSChris Kirby return (zvol_open(devp, flag, otyp, cr)); 5046c99e4bdcSChris Kirby 5047c99e4bdcSChris Kirby /* This is the control device. Allocate a new minor if requested. */ 5048c99e4bdcSChris Kirby if (flag & FEXCL) { 5049c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 5050c99e4bdcSChris Kirby error = zfs_ctldev_init(devp); 5051c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 5052c99e4bdcSChris Kirby } 5053c99e4bdcSChris Kirby 5054c99e4bdcSChris Kirby return (error); 5055c99e4bdcSChris Kirby } 5056c99e4bdcSChris Kirby 5057c99e4bdcSChris Kirby static int 5058c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr) 5059c99e4bdcSChris Kirby { 5060c99e4bdcSChris Kirby zfs_onexit_t *zo; 5061c99e4bdcSChris Kirby minor_t minor = getminor(dev); 5062c99e4bdcSChris Kirby 5063c99e4bdcSChris Kirby if (minor == 0) 5064c99e4bdcSChris Kirby return (0); 5065c99e4bdcSChris Kirby 5066c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 5067c99e4bdcSChris Kirby zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV); 5068c99e4bdcSChris Kirby if (zo == NULL) { 5069c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 5070c99e4bdcSChris Kirby return (zvol_close(dev, flag, otyp, cr)); 5071c99e4bdcSChris Kirby } 5072c99e4bdcSChris Kirby zfs_ctldev_destroy(zo, minor); 5073c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 5074c99e4bdcSChris Kirby 5075c99e4bdcSChris Kirby return (0); 5076c99e4bdcSChris Kirby } 5077c99e4bdcSChris Kirby 5078fa9e4066Sahrens static int 5079fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 5080fa9e4066Sahrens { 5081fa9e4066Sahrens zfs_cmd_t *zc; 5082fa9e4066Sahrens uint_t vec; 50831d452cf5Sahrens int error, rc; 5084c99e4bdcSChris Kirby minor_t minor = getminor(dev); 5085fa9e4066Sahrens 5086c99e4bdcSChris Kirby if (minor != 0 && 5087c99e4bdcSChris Kirby zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL) 5088fa9e4066Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 5089fa9e4066Sahrens 5090fa9e4066Sahrens vec = cmd - ZFS_IOC; 509191ebeef5Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 5092fa9e4066Sahrens 5093fa9e4066Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 5094fa9e4066Sahrens return (EINVAL); 5095fa9e4066Sahrens 5096fa9e4066Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 5097fa9e4066Sahrens 5098478ed9adSEric Taylor error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 50996e27f868SSam Falkner if (error != 0) 51006e27f868SSam Falkner error = EFAULT; 5101fa9e4066Sahrens 5102681d9761SEric Taylor if ((error == 0) && !(flag & FKIOCTL)) 5103ecd6cf80Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 5104fa9e4066Sahrens 5105fa9e4066Sahrens /* 5106fa9e4066Sahrens * Ensure that all pool/dataset names are valid before we pass down to 5107fa9e4066Sahrens * the lower layers. 5108fa9e4066Sahrens */ 5109fa9e4066Sahrens if (error == 0) { 5110fa9e4066Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 5111478ed9adSEric Taylor zc->zc_iflags = flag & FKIOCTL; 5112fa9e4066Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 5113e7437265Sahrens case POOL_NAME: 5114fa9e4066Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 5115fa9e4066Sahrens error = EINVAL; 5116f9af39baSGeorge Wilson error = pool_status_check(zc->zc_name, 5117f9af39baSGeorge Wilson zfs_ioc_vec[vec].zvec_namecheck, 5118f9af39baSGeorge Wilson zfs_ioc_vec[vec].zvec_pool_check); 5119fa9e4066Sahrens break; 5120fa9e4066Sahrens 5121e7437265Sahrens case DATASET_NAME: 5122fa9e4066Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 5123fa9e4066Sahrens error = EINVAL; 5124f9af39baSGeorge Wilson error = pool_status_check(zc->zc_name, 5125f9af39baSGeorge Wilson zfs_ioc_vec[vec].zvec_namecheck, 5126f9af39baSGeorge Wilson zfs_ioc_vec[vec].zvec_pool_check); 5127fa9e4066Sahrens break; 51285ad82045Snd 5129e7437265Sahrens case NO_NAME: 51305ad82045Snd break; 5131fa9e4066Sahrens } 5132fa9e4066Sahrens } 5133fa9e4066Sahrens 5134fa9e4066Sahrens if (error == 0) 5135fa9e4066Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 5136fa9e4066Sahrens 5137478ed9adSEric Taylor rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 5138ecd6cf80Smarks if (error == 0) { 51396e27f868SSam Falkner if (rc != 0) 51406e27f868SSam Falkner error = EFAULT; 514114843421SMatthew Ahrens if (zfs_ioc_vec[vec].zvec_his_log) 5142ecd6cf80Smarks zfs_log_history(zc); 5143ecd6cf80Smarks } 5144fa9e4066Sahrens 5145fa9e4066Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 5146fa9e4066Sahrens return (error); 5147fa9e4066Sahrens } 5148fa9e4066Sahrens 5149fa9e4066Sahrens static int 5150fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 5151fa9e4066Sahrens { 5152fa9e4066Sahrens if (cmd != DDI_ATTACH) 5153fa9e4066Sahrens return (DDI_FAILURE); 5154fa9e4066Sahrens 5155fa9e4066Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 5156fa9e4066Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 5157fa9e4066Sahrens return (DDI_FAILURE); 5158fa9e4066Sahrens 5159fa9e4066Sahrens zfs_dip = dip; 5160fa9e4066Sahrens 5161fa9e4066Sahrens ddi_report_dev(dip); 5162fa9e4066Sahrens 5163fa9e4066Sahrens return (DDI_SUCCESS); 5164fa9e4066Sahrens } 5165fa9e4066Sahrens 5166fa9e4066Sahrens static int 5167fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 5168fa9e4066Sahrens { 5169fa9e4066Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 5170fa9e4066Sahrens return (DDI_FAILURE); 5171fa9e4066Sahrens 5172fa9e4066Sahrens if (cmd != DDI_DETACH) 5173fa9e4066Sahrens return (DDI_FAILURE); 5174fa9e4066Sahrens 5175fa9e4066Sahrens zfs_dip = NULL; 5176fa9e4066Sahrens 5177fa9e4066Sahrens ddi_prop_remove_all(dip); 5178fa9e4066Sahrens ddi_remove_minor_node(dip, NULL); 5179fa9e4066Sahrens 5180fa9e4066Sahrens return (DDI_SUCCESS); 5181fa9e4066Sahrens } 5182fa9e4066Sahrens 5183fa9e4066Sahrens /*ARGSUSED*/ 5184fa9e4066Sahrens static int 5185fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 5186fa9e4066Sahrens { 5187fa9e4066Sahrens switch (infocmd) { 5188fa9e4066Sahrens case DDI_INFO_DEVT2DEVINFO: 5189fa9e4066Sahrens *result = zfs_dip; 5190fa9e4066Sahrens return (DDI_SUCCESS); 5191fa9e4066Sahrens 5192fa9e4066Sahrens case DDI_INFO_DEVT2INSTANCE: 5193a0965f35Sbonwick *result = (void *)0; 5194fa9e4066Sahrens return (DDI_SUCCESS); 5195fa9e4066Sahrens } 5196fa9e4066Sahrens 5197fa9e4066Sahrens return (DDI_FAILURE); 5198fa9e4066Sahrens } 5199fa9e4066Sahrens 5200fa9e4066Sahrens /* 5201fa9e4066Sahrens * OK, so this is a little weird. 5202fa9e4066Sahrens * 5203fa9e4066Sahrens * /dev/zfs is the control node, i.e. minor 0. 5204fa9e4066Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 5205fa9e4066Sahrens * 5206fa9e4066Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 5207fa9e4066Sahrens * so most of the standard driver entry points are in zvol.c. 5208fa9e4066Sahrens */ 5209fa9e4066Sahrens static struct cb_ops zfs_cb_ops = { 5210c99e4bdcSChris Kirby zfsdev_open, /* open */ 5211c99e4bdcSChris Kirby zfsdev_close, /* close */ 5212fa9e4066Sahrens zvol_strategy, /* strategy */ 5213fa9e4066Sahrens nodev, /* print */ 5214e7cbe64fSgw zvol_dump, /* dump */ 5215fa9e4066Sahrens zvol_read, /* read */ 5216fa9e4066Sahrens zvol_write, /* write */ 5217fa9e4066Sahrens zfsdev_ioctl, /* ioctl */ 5218fa9e4066Sahrens nodev, /* devmap */ 5219fa9e4066Sahrens nodev, /* mmap */ 5220fa9e4066Sahrens nodev, /* segmap */ 5221fa9e4066Sahrens nochpoll, /* poll */ 5222fa9e4066Sahrens ddi_prop_op, /* prop_op */ 5223fa9e4066Sahrens NULL, /* streamtab */ 5224fa9e4066Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 5225fa9e4066Sahrens CB_REV, /* version */ 5226feb08c6bSbillm nodev, /* async read */ 5227feb08c6bSbillm nodev, /* async write */ 5228fa9e4066Sahrens }; 5229fa9e4066Sahrens 5230fa9e4066Sahrens static struct dev_ops zfs_dev_ops = { 5231fa9e4066Sahrens DEVO_REV, /* version */ 5232fa9e4066Sahrens 0, /* refcnt */ 5233fa9e4066Sahrens zfs_info, /* info */ 5234fa9e4066Sahrens nulldev, /* identify */ 5235fa9e4066Sahrens nulldev, /* probe */ 5236fa9e4066Sahrens zfs_attach, /* attach */ 5237fa9e4066Sahrens zfs_detach, /* detach */ 5238fa9e4066Sahrens nodev, /* reset */ 5239fa9e4066Sahrens &zfs_cb_ops, /* driver operations */ 524019397407SSherry Moore NULL, /* no bus operations */ 524119397407SSherry Moore NULL, /* power */ 524219397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 5243fa9e4066Sahrens }; 5244fa9e4066Sahrens 5245fa9e4066Sahrens static struct modldrv zfs_modldrv = { 524619397407SSherry Moore &mod_driverops, 524719397407SSherry Moore "ZFS storage pool", 524819397407SSherry Moore &zfs_dev_ops 5249fa9e4066Sahrens }; 5250fa9e4066Sahrens 5251fa9e4066Sahrens static struct modlinkage modlinkage = { 5252fa9e4066Sahrens MODREV_1, 5253fa9e4066Sahrens (void *)&zfs_modlfs, 5254fa9e4066Sahrens (void *)&zfs_modldrv, 5255fa9e4066Sahrens NULL 5256fa9e4066Sahrens }; 5257fa9e4066Sahrens 5258ec533521Sfr 5259ec533521Sfr uint_t zfs_fsyncer_key; 5260f18faf3fSek extern uint_t rrw_tsd_key; 5261ec533521Sfr 5262fa9e4066Sahrens int 5263fa9e4066Sahrens _init(void) 5264fa9e4066Sahrens { 5265fa9e4066Sahrens int error; 5266fa9e4066Sahrens 5267a0965f35Sbonwick spa_init(FREAD | FWRITE); 5268a0965f35Sbonwick zfs_init(); 5269a0965f35Sbonwick zvol_init(); 5270a0965f35Sbonwick 5271a0965f35Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 5272a0965f35Sbonwick zvol_fini(); 5273a0965f35Sbonwick zfs_fini(); 5274a0965f35Sbonwick spa_fini(); 5275fa9e4066Sahrens return (error); 5276a0965f35Sbonwick } 5277fa9e4066Sahrens 5278ec533521Sfr tsd_create(&zfs_fsyncer_key, NULL); 5279f18faf3fSek tsd_create(&rrw_tsd_key, NULL); 5280ec533521Sfr 5281fa9e4066Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 5282fa9e4066Sahrens ASSERT(error == 0); 5283ecd6cf80Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 5284fa9e4066Sahrens 5285fa9e4066Sahrens return (0); 5286fa9e4066Sahrens } 5287fa9e4066Sahrens 5288fa9e4066Sahrens int 5289fa9e4066Sahrens _fini(void) 5290fa9e4066Sahrens { 5291fa9e4066Sahrens int error; 5292fa9e4066Sahrens 5293ea8dc4b6Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 5294fa9e4066Sahrens return (EBUSY); 5295fa9e4066Sahrens 5296fa9e4066Sahrens if ((error = mod_remove(&modlinkage)) != 0) 5297fa9e4066Sahrens return (error); 5298fa9e4066Sahrens 5299fa9e4066Sahrens zvol_fini(); 5300fa9e4066Sahrens zfs_fini(); 5301fa9e4066Sahrens spa_fini(); 5302da6c28aaSamw if (zfs_nfsshare_inited) 5303ecd6cf80Smarks (void) ddi_modclose(nfs_mod); 5304da6c28aaSamw if (zfs_smbshare_inited) 5305da6c28aaSamw (void) ddi_modclose(smbsrv_mod); 5306da6c28aaSamw if (zfs_nfsshare_inited || zfs_smbshare_inited) 5307ecd6cf80Smarks (void) ddi_modclose(sharefs_mod); 5308fa9e4066Sahrens 5309ec533521Sfr tsd_destroy(&zfs_fsyncer_key); 5310fa9e4066Sahrens ldi_ident_release(zfs_li); 5311fa9e4066Sahrens zfs_li = NULL; 5312ecd6cf80Smarks mutex_destroy(&zfs_share_lock); 5313fa9e4066Sahrens 5314fa9e4066Sahrens return (error); 5315fa9e4066Sahrens } 5316fa9e4066Sahrens 5317fa9e4066Sahrens int 5318fa9e4066Sahrens _info(struct modinfo *modinfop) 5319fa9e4066Sahrens { 5320fa9e4066Sahrens return (mod_info(&modlinkage, modinfop)); 5321fa9e4066Sahrens } 5322