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 */ 21*ad135b5dSChristopher Siden 22fa9e4066Sahrens /* 233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 241df56adaSMartin Matuska * Portions Copyright 2011 Martin Matuska 25e9103aaeSGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 264263d13fSGeorge Wilson * Copyright (c) 2012 by Delphix. All rights reserved. 274e3c9f44SBill Pijewski * Copyright (c) 2012, Joyent, Inc. All rights reserved. 28e9103aaeSGarrett D'Amore */ 29fa9e4066Sahrens 30fa9e4066Sahrens #include <sys/types.h> 31fa9e4066Sahrens #include <sys/param.h> 32fa9e4066Sahrens #include <sys/errno.h> 33fa9e4066Sahrens #include <sys/uio.h> 34fa9e4066Sahrens #include <sys/buf.h> 35fa9e4066Sahrens #include <sys/modctl.h> 36fa9e4066Sahrens #include <sys/open.h> 37fa9e4066Sahrens #include <sys/file.h> 38fa9e4066Sahrens #include <sys/kmem.h> 39fa9e4066Sahrens #include <sys/conf.h> 40fa9e4066Sahrens #include <sys/cmn_err.h> 41fa9e4066Sahrens #include <sys/stat.h> 42fa9e4066Sahrens #include <sys/zfs_ioctl.h> 434201a95eSRic Aleshire #include <sys/zfs_vfsops.h> 44da6c28aaSamw #include <sys/zfs_znode.h> 45fa9e4066Sahrens #include <sys/zap.h> 46fa9e4066Sahrens #include <sys/spa.h> 47b1b8ab34Slling #include <sys/spa_impl.h> 48fa9e4066Sahrens #include <sys/vdev.h> 494201a95eSRic Aleshire #include <sys/priv_impl.h> 50fa9e4066Sahrens #include <sys/dmu.h> 51fa9e4066Sahrens #include <sys/dsl_dir.h> 52fa9e4066Sahrens #include <sys/dsl_dataset.h> 53fa9e4066Sahrens #include <sys/dsl_prop.h> 54ecd6cf80Smarks #include <sys/dsl_deleg.h> 55ecd6cf80Smarks #include <sys/dmu_objset.h> 564e3c9f44SBill Pijewski #include <sys/dmu_impl.h> 57fa9e4066Sahrens #include <sys/ddi.h> 58fa9e4066Sahrens #include <sys/sunddi.h> 59fa9e4066Sahrens #include <sys/sunldi.h> 60fa9e4066Sahrens #include <sys/policy.h> 61fa9e4066Sahrens #include <sys/zone.h> 62fa9e4066Sahrens #include <sys/nvpair.h> 63fa9e4066Sahrens #include <sys/pathname.h> 64fa9e4066Sahrens #include <sys/mount.h> 65fa9e4066Sahrens #include <sys/sdt.h> 66fa9e4066Sahrens #include <sys/fs/zfs.h> 67fa9e4066Sahrens #include <sys/zfs_ctldir.h> 68da6c28aaSamw #include <sys/zfs_dir.h> 69c99e4bdcSChris Kirby #include <sys/zfs_onexit.h> 70a2eea2e1Sahrens #include <sys/zvol.h> 713f9d6ad7SLin Ling #include <sys/dsl_scan.h> 72ecd6cf80Smarks #include <sharefs/share.h> 73f18faf3fSek #include <sys/dmu_objset.h> 74fa9e4066Sahrens 75fa9e4066Sahrens #include "zfs_namecheck.h" 76e9dbad6fSeschrock #include "zfs_prop.h" 77ecd6cf80Smarks #include "zfs_deleg.h" 780a586ceaSMark Shellenbaum #include "zfs_comutil.h" 79fa9e4066Sahrens 80fa9e4066Sahrens extern struct modlfs zfs_modlfs; 81fa9e4066Sahrens 82fa9e4066Sahrens extern void zfs_init(void); 83fa9e4066Sahrens extern void zfs_fini(void); 84fa9e4066Sahrens 85fa9e4066Sahrens ldi_ident_t zfs_li = NULL; 86fa9e4066Sahrens dev_info_t *zfs_dip; 87fa9e4066Sahrens 88fa9e4066Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *); 89ecd6cf80Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 90fa9e4066Sahrens 9154d692b7SGeorge Wilson typedef enum { 9254d692b7SGeorge Wilson NO_NAME, 9354d692b7SGeorge Wilson POOL_NAME, 9454d692b7SGeorge Wilson DATASET_NAME 9554d692b7SGeorge Wilson } zfs_ioc_namecheck_t; 9654d692b7SGeorge Wilson 97f9af39baSGeorge Wilson typedef enum { 98f9af39baSGeorge Wilson POOL_CHECK_NONE = 1 << 0, 99f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED = 1 << 1, 100f9af39baSGeorge Wilson POOL_CHECK_READONLY = 1 << 2 101f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t; 102f9af39baSGeorge Wilson 103fa9e4066Sahrens typedef struct zfs_ioc_vec { 104fa9e4066Sahrens zfs_ioc_func_t *zvec_func; 105fa9e4066Sahrens zfs_secpolicy_func_t *zvec_secpolicy; 10654d692b7SGeorge Wilson zfs_ioc_namecheck_t zvec_namecheck; 107ecd6cf80Smarks boolean_t zvec_his_log; 108f9af39baSGeorge Wilson zfs_ioc_poolcheck_t zvec_pool_check; 109fa9e4066Sahrens } zfs_ioc_vec_t; 110fa9e4066Sahrens 11114843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */ 11214843421SMatthew Ahrens static const char *userquota_perms[] = { 11314843421SMatthew Ahrens ZFS_DELEG_PERM_USERUSED, 11414843421SMatthew Ahrens ZFS_DELEG_PERM_USERQUOTA, 11514843421SMatthew Ahrens ZFS_DELEG_PERM_GROUPUSED, 11614843421SMatthew Ahrens ZFS_DELEG_PERM_GROUPQUOTA, 11714843421SMatthew Ahrens }; 11814843421SMatthew Ahrens 11914843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 12092241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property, 12192241e0bSTom Erickson cred_t *cr); 12292241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props, 12392241e0bSTom Erickson nvlist_t **errors); 1240a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 1250a48a24eStimh boolean_t *); 12692241e0bSTom Erickson int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **); 1270a48a24eStimh 128fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 129fa9e4066Sahrens void 130fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 131fa9e4066Sahrens { 132fa9e4066Sahrens const char *newfile; 1333f9d6ad7SLin Ling char buf[512]; 134fa9e4066Sahrens va_list adx; 135fa9e4066Sahrens 136fa9e4066Sahrens /* 137fa9e4066Sahrens * Get rid of annoying "../common/" prefix to filename. 138fa9e4066Sahrens */ 139fa9e4066Sahrens newfile = strrchr(file, '/'); 140fa9e4066Sahrens if (newfile != NULL) { 141fa9e4066Sahrens newfile = newfile + 1; /* Get rid of leading / */ 142fa9e4066Sahrens } else { 143fa9e4066Sahrens newfile = file; 144fa9e4066Sahrens } 145fa9e4066Sahrens 146fa9e4066Sahrens va_start(adx, fmt); 147fa9e4066Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, adx); 148fa9e4066Sahrens va_end(adx); 149fa9e4066Sahrens 150fa9e4066Sahrens /* 151fa9e4066Sahrens * To get this data, use the zfs-dprintf probe as so: 152fa9e4066Sahrens * dtrace -q -n 'zfs-dprintf \ 153fa9e4066Sahrens * /stringof(arg0) == "dbuf.c"/ \ 154fa9e4066Sahrens * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 155fa9e4066Sahrens * arg0 = file name 156fa9e4066Sahrens * arg1 = function name 157fa9e4066Sahrens * arg2 = line number 158fa9e4066Sahrens * arg3 = message 159fa9e4066Sahrens */ 160fa9e4066Sahrens DTRACE_PROBE4(zfs__dprintf, 161fa9e4066Sahrens char *, newfile, char *, func, int, line, char *, buf); 162fa9e4066Sahrens } 163fa9e4066Sahrens 164ecd6cf80Smarks static void 165228975ccSek history_str_free(char *buf) 166228975ccSek { 167228975ccSek kmem_free(buf, HIS_MAX_RECORD_LEN); 168228975ccSek } 169228975ccSek 170228975ccSek static char * 171228975ccSek history_str_get(zfs_cmd_t *zc) 172ecd6cf80Smarks { 17340feaa91Sahrens char *buf; 174ecd6cf80Smarks 175ecd6cf80Smarks if (zc->zc_history == NULL) 176228975ccSek return (NULL); 177e7437265Sahrens 178ecd6cf80Smarks buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 179ecd6cf80Smarks if (copyinstr((void *)(uintptr_t)zc->zc_history, 180ecd6cf80Smarks buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 181228975ccSek history_str_free(buf); 182228975ccSek return (NULL); 183ecd6cf80Smarks } 184ecd6cf80Smarks 185ecd6cf80Smarks buf[HIS_MAX_RECORD_LEN -1] = '\0'; 186ecd6cf80Smarks 187228975ccSek return (buf); 188228975ccSek } 189ecd6cf80Smarks 19015e6edf1Sgw /* 19115e6edf1Sgw * Check to see if the named dataset is currently defined as bootable 19215e6edf1Sgw */ 19315e6edf1Sgw static boolean_t 19415e6edf1Sgw zfs_is_bootfs(const char *name) 19515e6edf1Sgw { 196503ad85cSMatthew Ahrens objset_t *os; 19715e6edf1Sgw 198503ad85cSMatthew Ahrens if (dmu_objset_hold(name, FTAG, &os) == 0) { 199503ad85cSMatthew Ahrens boolean_t ret; 200b24ab676SJeff Bonwick ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os))); 201503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 202503ad85cSMatthew Ahrens return (ret); 20315e6edf1Sgw } 204503ad85cSMatthew Ahrens return (B_FALSE); 20515e6edf1Sgw } 20615e6edf1Sgw 207c2a93d44Stimh /* 2080a48a24eStimh * zfs_earlier_version 209c2a93d44Stimh * 210c2a93d44Stimh * Return non-zero if the spa version is less than requested version. 211c2a93d44Stimh */ 212da6c28aaSamw static int 2130a48a24eStimh zfs_earlier_version(const char *name, int version) 214da6c28aaSamw { 215da6c28aaSamw spa_t *spa; 216da6c28aaSamw 217da6c28aaSamw if (spa_open(name, &spa, FTAG) == 0) { 218da6c28aaSamw if (spa_version(spa) < version) { 219da6c28aaSamw spa_close(spa, FTAG); 220da6c28aaSamw return (1); 221da6c28aaSamw } 222da6c28aaSamw spa_close(spa, FTAG); 223da6c28aaSamw } 224da6c28aaSamw return (0); 225da6c28aaSamw } 226da6c28aaSamw 2279e6eda55Smarks /* 228745cd3c5Smaybee * zpl_earlier_version 2299e6eda55Smarks * 230745cd3c5Smaybee * Return TRUE if the ZPL version is less than requested version. 2319e6eda55Smarks */ 232745cd3c5Smaybee static boolean_t 233745cd3c5Smaybee zpl_earlier_version(const char *name, int version) 2349e6eda55Smarks { 2359e6eda55Smarks objset_t *os; 236745cd3c5Smaybee boolean_t rc = B_TRUE; 2379e6eda55Smarks 238503ad85cSMatthew Ahrens if (dmu_objset_hold(name, FTAG, &os) == 0) { 239745cd3c5Smaybee uint64_t zplversion; 2409e6eda55Smarks 241503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 242503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 243503ad85cSMatthew Ahrens return (B_TRUE); 244503ad85cSMatthew Ahrens } 245503ad85cSMatthew Ahrens /* XXX reading from non-owned objset */ 246745cd3c5Smaybee if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 247745cd3c5Smaybee rc = zplversion < version; 248503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2499e6eda55Smarks } 2509e6eda55Smarks return (rc); 2519e6eda55Smarks } 2529e6eda55Smarks 253228975ccSek static void 254228975ccSek zfs_log_history(zfs_cmd_t *zc) 255228975ccSek { 256228975ccSek spa_t *spa; 257228975ccSek char *buf; 258ecd6cf80Smarks 259228975ccSek if ((buf = history_str_get(zc)) == NULL) 260228975ccSek return; 261228975ccSek 262228975ccSek if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 263228975ccSek if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 264228975ccSek (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 265228975ccSek spa_close(spa, FTAG); 266228975ccSek } 267228975ccSek history_str_free(buf); 268ecd6cf80Smarks } 269ecd6cf80Smarks 270fa9e4066Sahrens /* 271fa9e4066Sahrens * Policy for top-level read operations (list pools). Requires no privileges, 272fa9e4066Sahrens * and can be used in the local zone, as there is no associated dataset. 273fa9e4066Sahrens */ 274fa9e4066Sahrens /* ARGSUSED */ 275fa9e4066Sahrens static int 276ecd6cf80Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 277fa9e4066Sahrens { 278fa9e4066Sahrens return (0); 279fa9e4066Sahrens } 280fa9e4066Sahrens 281fa9e4066Sahrens /* 282fa9e4066Sahrens * Policy for dataset read operations (list children, get statistics). Requires 283fa9e4066Sahrens * no privileges, but must be visible in the local zone. 284fa9e4066Sahrens */ 285fa9e4066Sahrens /* ARGSUSED */ 286fa9e4066Sahrens static int 287ecd6cf80Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 288fa9e4066Sahrens { 289fa9e4066Sahrens if (INGLOBALZONE(curproc) || 290ecd6cf80Smarks zone_dataset_visible(zc->zc_name, NULL)) 291fa9e4066Sahrens return (0); 292fa9e4066Sahrens 293fa9e4066Sahrens return (ENOENT); 294fa9e4066Sahrens } 295fa9e4066Sahrens 296fa9e4066Sahrens static int 297a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr) 298fa9e4066Sahrens { 299fa9e4066Sahrens int writable = 1; 300fa9e4066Sahrens 301fa9e4066Sahrens /* 302fa9e4066Sahrens * The dataset must be visible by this zone -- check this first 303fa9e4066Sahrens * so they don't see EPERM on something they shouldn't know about. 304fa9e4066Sahrens */ 305fa9e4066Sahrens if (!INGLOBALZONE(curproc) && 306fa9e4066Sahrens !zone_dataset_visible(dataset, &writable)) 307fa9e4066Sahrens return (ENOENT); 308fa9e4066Sahrens 309fa9e4066Sahrens if (INGLOBALZONE(curproc)) { 310fa9e4066Sahrens /* 311fa9e4066Sahrens * If the fs is zoned, only root can access it from the 312fa9e4066Sahrens * global zone. 313fa9e4066Sahrens */ 314fa9e4066Sahrens if (secpolicy_zfs(cr) && zoned) 315fa9e4066Sahrens return (EPERM); 316fa9e4066Sahrens } else { 317fa9e4066Sahrens /* 318fa9e4066Sahrens * If we are in a local zone, the 'zoned' property must be set. 319fa9e4066Sahrens */ 320fa9e4066Sahrens if (!zoned) 321fa9e4066Sahrens return (EPERM); 322fa9e4066Sahrens 323fa9e4066Sahrens /* must be writable by this zone */ 324fa9e4066Sahrens if (!writable) 325fa9e4066Sahrens return (EPERM); 326fa9e4066Sahrens } 327fa9e4066Sahrens return (0); 328fa9e4066Sahrens } 329fa9e4066Sahrens 330a7f53a56SChris Kirby static int 331a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr) 332a7f53a56SChris Kirby { 333a7f53a56SChris Kirby uint64_t zoned; 334a7f53a56SChris Kirby 335a7f53a56SChris Kirby if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 336a7f53a56SChris Kirby return (ENOENT); 337a7f53a56SChris Kirby 338a7f53a56SChris Kirby return (zfs_dozonecheck_impl(dataset, zoned, cr)); 339a7f53a56SChris Kirby } 340a7f53a56SChris Kirby 341a7f53a56SChris Kirby static int 342a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr) 343a7f53a56SChris Kirby { 344a7f53a56SChris Kirby uint64_t zoned; 345a7f53a56SChris Kirby 346a7f53a56SChris Kirby rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 347a7f53a56SChris Kirby if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) { 348a7f53a56SChris Kirby rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 349a7f53a56SChris Kirby return (ENOENT); 350a7f53a56SChris Kirby } 351a7f53a56SChris Kirby rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 352a7f53a56SChris Kirby 353a7f53a56SChris Kirby return (zfs_dozonecheck_impl(dataset, zoned, cr)); 354a7f53a56SChris Kirby } 355a7f53a56SChris Kirby 35619b94df9SMatthew Ahrens /* 35719b94df9SMatthew Ahrens * If name ends in a '@', then require recursive permissions. 35819b94df9SMatthew Ahrens */ 359fa9e4066Sahrens int 360ecd6cf80Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 361fa9e4066Sahrens { 362fa9e4066Sahrens int error; 36319b94df9SMatthew Ahrens boolean_t descendent = B_FALSE; 36419b94df9SMatthew Ahrens dsl_dataset_t *ds; 36519b94df9SMatthew Ahrens char *at; 36619b94df9SMatthew Ahrens 36719b94df9SMatthew Ahrens at = strchr(name, '@'); 36819b94df9SMatthew Ahrens if (at != NULL && at[1] == '\0') { 36919b94df9SMatthew Ahrens *at = '\0'; 37019b94df9SMatthew Ahrens descendent = B_TRUE; 37119b94df9SMatthew Ahrens } 37219b94df9SMatthew Ahrens 37319b94df9SMatthew Ahrens error = dsl_dataset_hold(name, FTAG, &ds); 37419b94df9SMatthew Ahrens if (at != NULL) 37519b94df9SMatthew Ahrens *at = '@'; 37619b94df9SMatthew Ahrens if (error != 0) 37719b94df9SMatthew Ahrens return (error); 378fa9e4066Sahrens 37919b94df9SMatthew Ahrens error = zfs_dozonecheck_ds(name, ds, cr); 380ecd6cf80Smarks if (error == 0) { 381ecd6cf80Smarks error = secpolicy_zfs(cr); 382db870a07Sahrens if (error) 38319b94df9SMatthew Ahrens error = dsl_deleg_access_impl(ds, descendent, perm, cr); 384ecd6cf80Smarks } 38519b94df9SMatthew Ahrens 38619b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 387ecd6cf80Smarks return (error); 388ecd6cf80Smarks } 389ecd6cf80Smarks 390a7f53a56SChris Kirby int 391a7f53a56SChris Kirby zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds, 392a7f53a56SChris Kirby const char *perm, cred_t *cr) 393a7f53a56SChris Kirby { 394a7f53a56SChris Kirby int error; 395a7f53a56SChris Kirby 396a7f53a56SChris Kirby error = zfs_dozonecheck_ds(name, ds, cr); 397a7f53a56SChris Kirby if (error == 0) { 398a7f53a56SChris Kirby error = secpolicy_zfs(cr); 399a7f53a56SChris Kirby if (error) 40019b94df9SMatthew Ahrens error = dsl_deleg_access_impl(ds, B_FALSE, perm, cr); 401a7f53a56SChris Kirby } 402a7f53a56SChris Kirby return (error); 403a7f53a56SChris Kirby } 404a7f53a56SChris Kirby 4054201a95eSRic Aleshire /* 4064201a95eSRic Aleshire * Policy for setting the security label property. 4074201a95eSRic Aleshire * 4084201a95eSRic Aleshire * Returns 0 for success, non-zero for access and other errors. 4094201a95eSRic Aleshire */ 4104201a95eSRic Aleshire static int 41192241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr) 4124201a95eSRic Aleshire { 4134201a95eSRic Aleshire char ds_hexsl[MAXNAMELEN]; 4144201a95eSRic Aleshire bslabel_t ds_sl, new_sl; 4154201a95eSRic Aleshire boolean_t new_default = FALSE; 4164201a95eSRic Aleshire uint64_t zoned; 4174201a95eSRic Aleshire int needed_priv = -1; 4184201a95eSRic Aleshire int error; 4194201a95eSRic Aleshire 4204201a95eSRic Aleshire /* First get the existing dataset label. */ 4214201a95eSRic Aleshire error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 4224201a95eSRic Aleshire 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 4234201a95eSRic Aleshire if (error) 4244201a95eSRic Aleshire return (EPERM); 4254201a95eSRic Aleshire 4264201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 4274201a95eSRic Aleshire new_default = TRUE; 4284201a95eSRic Aleshire 4294201a95eSRic Aleshire /* The label must be translatable */ 4304201a95eSRic Aleshire if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) 4314201a95eSRic Aleshire return (EINVAL); 4324201a95eSRic Aleshire 4334201a95eSRic Aleshire /* 4344201a95eSRic Aleshire * In a non-global zone, disallow attempts to set a label that 4354201a95eSRic Aleshire * doesn't match that of the zone; otherwise no other checks 4364201a95eSRic Aleshire * are needed. 4374201a95eSRic Aleshire */ 4384201a95eSRic Aleshire if (!INGLOBALZONE(curproc)) { 4394201a95eSRic Aleshire if (new_default || !blequal(&new_sl, CR_SL(CRED()))) 4404201a95eSRic Aleshire return (EPERM); 4414201a95eSRic Aleshire return (0); 4424201a95eSRic Aleshire } 4434201a95eSRic Aleshire 4444201a95eSRic Aleshire /* 4454201a95eSRic Aleshire * For global-zone datasets (i.e., those whose zoned property is 4464201a95eSRic Aleshire * "off", verify that the specified new label is valid for the 4474201a95eSRic Aleshire * global zone. 4484201a95eSRic Aleshire */ 4494201a95eSRic Aleshire if (dsl_prop_get_integer(name, 4504201a95eSRic Aleshire zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 4514201a95eSRic Aleshire return (EPERM); 4524201a95eSRic Aleshire if (!zoned) { 4534201a95eSRic Aleshire if (zfs_check_global_label(name, strval) != 0) 4544201a95eSRic Aleshire return (EPERM); 4554201a95eSRic Aleshire } 4564201a95eSRic Aleshire 4574201a95eSRic Aleshire /* 4584201a95eSRic Aleshire * If the existing dataset label is nondefault, check if the 4594201a95eSRic Aleshire * dataset is mounted (label cannot be changed while mounted). 4604201a95eSRic Aleshire * Get the zfsvfs; if there isn't one, then the dataset isn't 4614201a95eSRic Aleshire * mounted (or isn't a dataset, doesn't exist, ...). 4624201a95eSRic Aleshire */ 4634201a95eSRic Aleshire if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { 46492241e0bSTom Erickson objset_t *os; 46592241e0bSTom Erickson static char *setsl_tag = "setsl_tag"; 46692241e0bSTom Erickson 4674201a95eSRic Aleshire /* 4684201a95eSRic Aleshire * Try to own the dataset; abort if there is any error, 4694201a95eSRic Aleshire * (e.g., already mounted, in use, or other error). 4704201a95eSRic Aleshire */ 4714201a95eSRic Aleshire error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, 47292241e0bSTom Erickson setsl_tag, &os); 4734201a95eSRic Aleshire if (error) 4744201a95eSRic Aleshire return (EPERM); 4754201a95eSRic Aleshire 47692241e0bSTom Erickson dmu_objset_disown(os, setsl_tag); 47792241e0bSTom Erickson 4784201a95eSRic Aleshire if (new_default) { 4794201a95eSRic Aleshire needed_priv = PRIV_FILE_DOWNGRADE_SL; 4804201a95eSRic Aleshire goto out_check; 4814201a95eSRic Aleshire } 4824201a95eSRic Aleshire 4834201a95eSRic Aleshire if (hexstr_to_label(strval, &new_sl) != 0) 4844201a95eSRic Aleshire return (EPERM); 4854201a95eSRic Aleshire 4864201a95eSRic Aleshire if (blstrictdom(&ds_sl, &new_sl)) 4874201a95eSRic Aleshire needed_priv = PRIV_FILE_DOWNGRADE_SL; 4884201a95eSRic Aleshire else if (blstrictdom(&new_sl, &ds_sl)) 4894201a95eSRic Aleshire needed_priv = PRIV_FILE_UPGRADE_SL; 4904201a95eSRic Aleshire } else { 4914201a95eSRic Aleshire /* dataset currently has a default label */ 4924201a95eSRic Aleshire if (!new_default) 4934201a95eSRic Aleshire needed_priv = PRIV_FILE_UPGRADE_SL; 4944201a95eSRic Aleshire } 4954201a95eSRic Aleshire 4964201a95eSRic Aleshire out_check: 4974201a95eSRic Aleshire if (needed_priv != -1) 4984201a95eSRic Aleshire return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); 4994201a95eSRic Aleshire return (0); 5004201a95eSRic Aleshire } 5014201a95eSRic Aleshire 502ecd6cf80Smarks static int 50392241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, 50492241e0bSTom Erickson cred_t *cr) 505ecd6cf80Smarks { 50692241e0bSTom Erickson char *strval; 50792241e0bSTom Erickson 508ecd6cf80Smarks /* 509ecd6cf80Smarks * Check permissions for special properties. 510ecd6cf80Smarks */ 511ecd6cf80Smarks switch (prop) { 512ecd6cf80Smarks case ZFS_PROP_ZONED: 513ecd6cf80Smarks /* 514ecd6cf80Smarks * Disallow setting of 'zoned' from within a local zone. 515ecd6cf80Smarks */ 516ecd6cf80Smarks if (!INGLOBALZONE(curproc)) 517ecd6cf80Smarks return (EPERM); 518ecd6cf80Smarks break; 519ecd6cf80Smarks 520ecd6cf80Smarks case ZFS_PROP_QUOTA: 521ecd6cf80Smarks if (!INGLOBALZONE(curproc)) { 522ecd6cf80Smarks uint64_t zoned; 523ecd6cf80Smarks char setpoint[MAXNAMELEN]; 524ecd6cf80Smarks /* 525ecd6cf80Smarks * Unprivileged users are allowed to modify the 526ecd6cf80Smarks * quota on things *under* (ie. contained by) 527ecd6cf80Smarks * the thing they own. 528ecd6cf80Smarks */ 52992241e0bSTom Erickson if (dsl_prop_get_integer(dsname, "zoned", &zoned, 530ecd6cf80Smarks setpoint)) 531ecd6cf80Smarks return (EPERM); 53292241e0bSTom Erickson if (!zoned || strlen(dsname) <= strlen(setpoint)) 533ecd6cf80Smarks return (EPERM); 534ecd6cf80Smarks } 535db870a07Sahrens break; 5364201a95eSRic Aleshire 5374201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 5384201a95eSRic Aleshire if (!is_system_labeled()) 5394201a95eSRic Aleshire return (EPERM); 54092241e0bSTom Erickson 54192241e0bSTom Erickson if (nvpair_value_string(propval, &strval) == 0) { 54292241e0bSTom Erickson int err; 54392241e0bSTom Erickson 54492241e0bSTom Erickson err = zfs_set_slabel_policy(dsname, strval, CRED()); 54592241e0bSTom Erickson if (err != 0) 54692241e0bSTom Erickson return (err); 54792241e0bSTom Erickson } 5484201a95eSRic Aleshire break; 549ecd6cf80Smarks } 550ecd6cf80Smarks 55192241e0bSTom Erickson return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); 552ecd6cf80Smarks } 553ecd6cf80Smarks 554ecd6cf80Smarks int 555ecd6cf80Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 556ecd6cf80Smarks { 557ecd6cf80Smarks int error; 558ecd6cf80Smarks 559ecd6cf80Smarks error = zfs_dozonecheck(zc->zc_name, cr); 560ecd6cf80Smarks if (error) 561fa9e4066Sahrens return (error); 562fa9e4066Sahrens 563ecd6cf80Smarks /* 564ecd6cf80Smarks * permission to set permissions will be evaluated later in 565ecd6cf80Smarks * dsl_deleg_can_allow() 566ecd6cf80Smarks */ 567ecd6cf80Smarks return (0); 568ecd6cf80Smarks } 569ecd6cf80Smarks 570ecd6cf80Smarks int 571ecd6cf80Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 572ecd6cf80Smarks { 573681d9761SEric Taylor return (zfs_secpolicy_write_perms(zc->zc_name, 574681d9761SEric Taylor ZFS_DELEG_PERM_ROLLBACK, cr)); 575ecd6cf80Smarks } 576ecd6cf80Smarks 577ecd6cf80Smarks int 578ecd6cf80Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 579ecd6cf80Smarks { 580a7f53a56SChris Kirby spa_t *spa; 581a7f53a56SChris Kirby dsl_pool_t *dp; 582a7f53a56SChris Kirby dsl_dataset_t *ds; 583a7f53a56SChris Kirby char *cp; 584a7f53a56SChris Kirby int error; 585a7f53a56SChris Kirby 586a7f53a56SChris Kirby /* 587a7f53a56SChris Kirby * Generate the current snapshot name from the given objsetid, then 588a7f53a56SChris Kirby * use that name for the secpolicy/zone checks. 589a7f53a56SChris Kirby */ 590a7f53a56SChris Kirby cp = strchr(zc->zc_name, '@'); 591a7f53a56SChris Kirby if (cp == NULL) 592a7f53a56SChris Kirby return (EINVAL); 593a7f53a56SChris Kirby error = spa_open(zc->zc_name, &spa, FTAG); 594a7f53a56SChris Kirby if (error) 595a7f53a56SChris Kirby return (error); 596a7f53a56SChris Kirby 597a7f53a56SChris Kirby dp = spa_get_dsl(spa); 598a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 599a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 600a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 601a7f53a56SChris Kirby spa_close(spa, FTAG); 602a7f53a56SChris Kirby if (error) 603a7f53a56SChris Kirby return (error); 604a7f53a56SChris Kirby 605a7f53a56SChris Kirby dsl_dataset_name(ds, zc->zc_name); 606a7f53a56SChris Kirby 607a7f53a56SChris Kirby error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds, 608a7f53a56SChris Kirby ZFS_DELEG_PERM_SEND, cr); 609a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 610a7f53a56SChris Kirby 611a7f53a56SChris Kirby return (error); 612ecd6cf80Smarks } 613ecd6cf80Smarks 614743a77edSAlan Wright static int 615743a77edSAlan Wright zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr) 616743a77edSAlan Wright { 617743a77edSAlan Wright vnode_t *vp; 618743a77edSAlan Wright int error; 619743a77edSAlan Wright 620743a77edSAlan Wright if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 621743a77edSAlan Wright NO_FOLLOW, NULL, &vp)) != 0) 622743a77edSAlan Wright return (error); 623743a77edSAlan Wright 624743a77edSAlan Wright /* Now make sure mntpnt and dataset are ZFS */ 625743a77edSAlan Wright 626743a77edSAlan Wright if (vp->v_vfsp->vfs_fstype != zfsfstype || 627743a77edSAlan Wright (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 628743a77edSAlan Wright zc->zc_name) != 0)) { 629743a77edSAlan Wright VN_RELE(vp); 630743a77edSAlan Wright return (EPERM); 631743a77edSAlan Wright } 632743a77edSAlan Wright 633743a77edSAlan Wright VN_RELE(vp); 634743a77edSAlan Wright return (dsl_deleg_access(zc->zc_name, 635743a77edSAlan Wright ZFS_DELEG_PERM_SHARE, cr)); 636743a77edSAlan Wright } 637743a77edSAlan Wright 638ecd6cf80Smarks int 639ecd6cf80Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 640ecd6cf80Smarks { 641ecd6cf80Smarks if (!INGLOBALZONE(curproc)) 642ecd6cf80Smarks return (EPERM); 643ecd6cf80Smarks 6443cb34c60Sahrens if (secpolicy_nfs(cr) == 0) { 645ecd6cf80Smarks return (0); 646ecd6cf80Smarks } else { 647743a77edSAlan Wright return (zfs_secpolicy_deleg_share(zc, cr)); 648743a77edSAlan Wright } 649743a77edSAlan Wright } 650ecd6cf80Smarks 651743a77edSAlan Wright int 652743a77edSAlan Wright zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr) 653743a77edSAlan Wright { 654743a77edSAlan Wright if (!INGLOBALZONE(curproc)) 655743a77edSAlan Wright return (EPERM); 656ecd6cf80Smarks 657743a77edSAlan Wright if (secpolicy_smb(cr) == 0) { 658743a77edSAlan Wright return (0); 659743a77edSAlan Wright } else { 660743a77edSAlan Wright return (zfs_secpolicy_deleg_share(zc, cr)); 661ecd6cf80Smarks } 662fa9e4066Sahrens } 663fa9e4066Sahrens 664fa9e4066Sahrens static int 665ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize) 666fa9e4066Sahrens { 667fa9e4066Sahrens char *cp; 668fa9e4066Sahrens 669fa9e4066Sahrens /* 670fa9e4066Sahrens * Remove the @bla or /bla from the end of the name to get the parent. 671fa9e4066Sahrens */ 672ecd6cf80Smarks (void) strncpy(parent, datasetname, parentsize); 673ecd6cf80Smarks cp = strrchr(parent, '@'); 674fa9e4066Sahrens if (cp != NULL) { 675fa9e4066Sahrens cp[0] = '\0'; 676fa9e4066Sahrens } else { 677ecd6cf80Smarks cp = strrchr(parent, '/'); 678fa9e4066Sahrens if (cp == NULL) 679fa9e4066Sahrens return (ENOENT); 680fa9e4066Sahrens cp[0] = '\0'; 681ecd6cf80Smarks } 682ecd6cf80Smarks 683ecd6cf80Smarks return (0); 684ecd6cf80Smarks } 685ecd6cf80Smarks 686ecd6cf80Smarks int 687ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 688ecd6cf80Smarks { 689ecd6cf80Smarks int error; 690ecd6cf80Smarks 691ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(name, 692ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 693ecd6cf80Smarks return (error); 694ecd6cf80Smarks 695ecd6cf80Smarks return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 696ecd6cf80Smarks } 697ecd6cf80Smarks 698ecd6cf80Smarks static int 699ecd6cf80Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 700ecd6cf80Smarks { 701ecd6cf80Smarks return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 702ecd6cf80Smarks } 703ecd6cf80Smarks 704cbf6f6aaSWilliam Gorrell /* 705cbf6f6aaSWilliam Gorrell * Destroying snapshots with delegated permissions requires 706cbf6f6aaSWilliam Gorrell * descendent mount and destroy permissions. 707cbf6f6aaSWilliam Gorrell */ 708cbf6f6aaSWilliam Gorrell static int 70919b94df9SMatthew Ahrens zfs_secpolicy_destroy_recursive(zfs_cmd_t *zc, cred_t *cr) 710cbf6f6aaSWilliam Gorrell { 711cbf6f6aaSWilliam Gorrell int error; 712cbf6f6aaSWilliam Gorrell char *dsname; 713cbf6f6aaSWilliam Gorrell 71419b94df9SMatthew Ahrens dsname = kmem_asprintf("%s@", zc->zc_name); 715cbf6f6aaSWilliam Gorrell 716cbf6f6aaSWilliam Gorrell error = zfs_secpolicy_destroy_perms(dsname, cr); 717cbf6f6aaSWilliam Gorrell 718cbf6f6aaSWilliam Gorrell strfree(dsname); 719cbf6f6aaSWilliam Gorrell return (error); 720cbf6f6aaSWilliam Gorrell } 721cbf6f6aaSWilliam Gorrell 722ecd6cf80Smarks int 723ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 724ecd6cf80Smarks { 72592241e0bSTom Erickson char parentname[MAXNAMELEN]; 726ecd6cf80Smarks int error; 727ecd6cf80Smarks 728ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(from, 729ecd6cf80Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0) 730ecd6cf80Smarks return (error); 731ecd6cf80Smarks 732ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(from, 733ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 734ecd6cf80Smarks return (error); 735ecd6cf80Smarks 736ecd6cf80Smarks if ((error = zfs_get_parent(to, parentname, 737ecd6cf80Smarks sizeof (parentname))) != 0) 738ecd6cf80Smarks return (error); 739ecd6cf80Smarks 740ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname, 741ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 742ecd6cf80Smarks return (error); 743ecd6cf80Smarks 744ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname, 745ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 746ecd6cf80Smarks return (error); 747ecd6cf80Smarks 748ecd6cf80Smarks return (error); 749ecd6cf80Smarks } 750ecd6cf80Smarks 751ecd6cf80Smarks static int 752ecd6cf80Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 753ecd6cf80Smarks { 754ecd6cf80Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 755ecd6cf80Smarks } 756ecd6cf80Smarks 757ecd6cf80Smarks static int 758ecd6cf80Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 759ecd6cf80Smarks { 76092241e0bSTom Erickson char parentname[MAXNAMELEN]; 761ecd6cf80Smarks objset_t *clone; 762ecd6cf80Smarks int error; 763ecd6cf80Smarks 764ecd6cf80Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 765ecd6cf80Smarks ZFS_DELEG_PERM_PROMOTE, cr); 766ecd6cf80Smarks if (error) 767ecd6cf80Smarks return (error); 768ecd6cf80Smarks 769503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &clone); 770ecd6cf80Smarks 771ecd6cf80Smarks if (error == 0) { 772ecd6cf80Smarks dsl_dataset_t *pclone = NULL; 773ecd6cf80Smarks dsl_dir_t *dd; 774503ad85cSMatthew Ahrens dd = clone->os_dsl_dataset->ds_dir; 775ecd6cf80Smarks 776ecd6cf80Smarks rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 777745cd3c5Smaybee error = dsl_dataset_hold_obj(dd->dd_pool, 778745cd3c5Smaybee dd->dd_phys->dd_origin_obj, FTAG, &pclone); 779ecd6cf80Smarks rw_exit(&dd->dd_pool->dp_config_rwlock); 780ecd6cf80Smarks if (error) { 781503ad85cSMatthew Ahrens dmu_objset_rele(clone, FTAG); 782ecd6cf80Smarks return (error); 783ecd6cf80Smarks } 784ecd6cf80Smarks 785ecd6cf80Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 786ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr); 787ecd6cf80Smarks 788ecd6cf80Smarks dsl_dataset_name(pclone, parentname); 789503ad85cSMatthew Ahrens dmu_objset_rele(clone, FTAG); 790745cd3c5Smaybee dsl_dataset_rele(pclone, FTAG); 791ecd6cf80Smarks if (error == 0) 792ecd6cf80Smarks error = zfs_secpolicy_write_perms(parentname, 793ecd6cf80Smarks ZFS_DELEG_PERM_PROMOTE, cr); 794ecd6cf80Smarks } 795ecd6cf80Smarks return (error); 796ecd6cf80Smarks } 797ecd6cf80Smarks 798ecd6cf80Smarks static int 799ecd6cf80Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 800ecd6cf80Smarks { 801ecd6cf80Smarks int error; 802ecd6cf80Smarks 803ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 804ecd6cf80Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 805ecd6cf80Smarks return (error); 806ecd6cf80Smarks 807ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 808ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 809ecd6cf80Smarks return (error); 810ecd6cf80Smarks 811ecd6cf80Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 812ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)); 813ecd6cf80Smarks } 814ecd6cf80Smarks 815ecd6cf80Smarks int 816ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 817ecd6cf80Smarks { 818681d9761SEric Taylor return (zfs_secpolicy_write_perms(name, 819681d9761SEric Taylor ZFS_DELEG_PERM_SNAPSHOT, cr)); 820ecd6cf80Smarks } 821ecd6cf80Smarks 822ecd6cf80Smarks static int 823ecd6cf80Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 824ecd6cf80Smarks { 825ecd6cf80Smarks 826ecd6cf80Smarks return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 827ecd6cf80Smarks } 828ecd6cf80Smarks 829ecd6cf80Smarks static int 830ecd6cf80Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 831ecd6cf80Smarks { 83292241e0bSTom Erickson char parentname[MAXNAMELEN]; 83392241e0bSTom Erickson int error; 834ecd6cf80Smarks 835ecd6cf80Smarks if ((error = zfs_get_parent(zc->zc_name, parentname, 836ecd6cf80Smarks sizeof (parentname))) != 0) 837ecd6cf80Smarks return (error); 838fa9e4066Sahrens 839ecd6cf80Smarks if (zc->zc_value[0] != '\0') { 840ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_value, 841ecd6cf80Smarks ZFS_DELEG_PERM_CLONE, cr)) != 0) 842ecd6cf80Smarks return (error); 843fa9e4066Sahrens } 844fa9e4066Sahrens 845ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname, 846ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 847ecd6cf80Smarks return (error); 848ecd6cf80Smarks 849ecd6cf80Smarks error = zfs_secpolicy_write_perms(parentname, 850ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr); 851ecd6cf80Smarks 852ecd6cf80Smarks return (error); 853ecd6cf80Smarks } 854ecd6cf80Smarks 855ecd6cf80Smarks static int 856ecd6cf80Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 857ecd6cf80Smarks { 858ecd6cf80Smarks int error; 859ecd6cf80Smarks 860ecd6cf80Smarks error = secpolicy_fs_unmount(cr, NULL); 861ecd6cf80Smarks if (error) { 862ecd6cf80Smarks error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 863ecd6cf80Smarks } 864ecd6cf80Smarks return (error); 865fa9e4066Sahrens } 866fa9e4066Sahrens 867fa9e4066Sahrens /* 868fa9e4066Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 869fa9e4066Sahrens * SYS_CONFIG privilege, which is not available in a local zone. 870fa9e4066Sahrens */ 871fa9e4066Sahrens /* ARGSUSED */ 872fa9e4066Sahrens static int 873ecd6cf80Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 874fa9e4066Sahrens { 875fa9e4066Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0) 876fa9e4066Sahrens return (EPERM); 877fa9e4066Sahrens 878fa9e4066Sahrens return (0); 879fa9e4066Sahrens } 880fa9e4066Sahrens 88199d5e173STim Haley /* 88299d5e173STim Haley * Policy for object to name lookups. 88399d5e173STim Haley */ 88499d5e173STim Haley /* ARGSUSED */ 88599d5e173STim Haley static int 88699d5e173STim Haley zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr) 88799d5e173STim Haley { 88899d5e173STim Haley int error; 88999d5e173STim Haley 89099d5e173STim Haley if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0) 89199d5e173STim Haley return (0); 89299d5e173STim Haley 89399d5e173STim Haley error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr); 89499d5e173STim Haley return (error); 89599d5e173STim Haley } 89699d5e173STim Haley 897ea8dc4b6Seschrock /* 898ea8dc4b6Seschrock * Policy for fault injection. Requires all privileges. 899ea8dc4b6Seschrock */ 900ea8dc4b6Seschrock /* ARGSUSED */ 901ea8dc4b6Seschrock static int 902ecd6cf80Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 903ea8dc4b6Seschrock { 904ea8dc4b6Seschrock return (secpolicy_zinject(cr)); 905ea8dc4b6Seschrock } 906ea8dc4b6Seschrock 907e45ce728Sahrens static int 908e45ce728Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 909e45ce728Sahrens { 910e45ce728Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 911e45ce728Sahrens 912990b4856Slling if (prop == ZPROP_INVAL) { 913e45ce728Sahrens if (!zfs_prop_user(zc->zc_value)) 914e45ce728Sahrens return (EINVAL); 915e45ce728Sahrens return (zfs_secpolicy_write_perms(zc->zc_name, 916e45ce728Sahrens ZFS_DELEG_PERM_USERPROP, cr)); 917e45ce728Sahrens } else { 91892241e0bSTom Erickson return (zfs_secpolicy_setprop(zc->zc_name, prop, 91992241e0bSTom Erickson NULL, cr)); 920e45ce728Sahrens } 921e45ce728Sahrens } 922e45ce728Sahrens 92314843421SMatthew Ahrens static int 92414843421SMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr) 92514843421SMatthew Ahrens { 92614843421SMatthew Ahrens int err = zfs_secpolicy_read(zc, cr); 92714843421SMatthew Ahrens if (err) 92814843421SMatthew Ahrens return (err); 92914843421SMatthew Ahrens 93014843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 93114843421SMatthew Ahrens return (EINVAL); 93214843421SMatthew Ahrens 93314843421SMatthew Ahrens if (zc->zc_value[0] == 0) { 93414843421SMatthew Ahrens /* 93514843421SMatthew Ahrens * They are asking about a posix uid/gid. If it's 93614843421SMatthew Ahrens * themself, allow it. 93714843421SMatthew Ahrens */ 93814843421SMatthew Ahrens if (zc->zc_objset_type == ZFS_PROP_USERUSED || 93914843421SMatthew Ahrens zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 94014843421SMatthew Ahrens if (zc->zc_guid == crgetuid(cr)) 94114843421SMatthew Ahrens return (0); 94214843421SMatthew Ahrens } else { 94314843421SMatthew Ahrens if (groupmember(zc->zc_guid, cr)) 94414843421SMatthew Ahrens return (0); 94514843421SMatthew Ahrens } 94614843421SMatthew Ahrens } 94714843421SMatthew Ahrens 94814843421SMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name, 94914843421SMatthew Ahrens userquota_perms[zc->zc_objset_type], cr)); 95014843421SMatthew Ahrens } 95114843421SMatthew Ahrens 95214843421SMatthew Ahrens static int 95314843421SMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr) 95414843421SMatthew Ahrens { 95514843421SMatthew Ahrens int err = zfs_secpolicy_read(zc, cr); 95614843421SMatthew Ahrens if (err) 95714843421SMatthew Ahrens return (err); 95814843421SMatthew Ahrens 95914843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 96014843421SMatthew Ahrens return (EINVAL); 96114843421SMatthew Ahrens 96214843421SMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name, 96314843421SMatthew Ahrens userquota_perms[zc->zc_objset_type], cr)); 96414843421SMatthew Ahrens } 96514843421SMatthew Ahrens 96614843421SMatthew Ahrens static int 96714843421SMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr) 96814843421SMatthew Ahrens { 96992241e0bSTom Erickson return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 97092241e0bSTom Erickson NULL, cr)); 97114843421SMatthew Ahrens } 97214843421SMatthew Ahrens 973842727c2SChris Kirby static int 974842727c2SChris Kirby zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr) 975842727c2SChris Kirby { 976842727c2SChris Kirby return (zfs_secpolicy_write_perms(zc->zc_name, 977842727c2SChris Kirby ZFS_DELEG_PERM_HOLD, cr)); 978842727c2SChris Kirby } 979842727c2SChris Kirby 980842727c2SChris Kirby static int 981842727c2SChris Kirby zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr) 982842727c2SChris Kirby { 983842727c2SChris Kirby return (zfs_secpolicy_write_perms(zc->zc_name, 984842727c2SChris Kirby ZFS_DELEG_PERM_RELEASE, cr)); 985842727c2SChris Kirby } 986842727c2SChris Kirby 98799d5e173STim Haley /* 98899d5e173STim Haley * Policy for allowing temporary snapshots to be taken or released 98999d5e173STim Haley */ 99099d5e173STim Haley static int 99199d5e173STim Haley zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr) 99299d5e173STim Haley { 99399d5e173STim Haley /* 99499d5e173STim Haley * A temporary snapshot is the same as a snapshot, 99599d5e173STim Haley * hold, destroy and release all rolled into one. 99699d5e173STim Haley * Delegated diff alone is sufficient that we allow this. 99799d5e173STim Haley */ 99899d5e173STim Haley int error; 99999d5e173STim Haley 100099d5e173STim Haley if ((error = zfs_secpolicy_write_perms(zc->zc_name, 100199d5e173STim Haley ZFS_DELEG_PERM_DIFF, cr)) == 0) 100299d5e173STim Haley return (0); 100399d5e173STim Haley 100499d5e173STim Haley error = zfs_secpolicy_snapshot(zc, cr); 100599d5e173STim Haley if (!error) 100699d5e173STim Haley error = zfs_secpolicy_hold(zc, cr); 100799d5e173STim Haley if (!error) 100899d5e173STim Haley error = zfs_secpolicy_release(zc, cr); 100999d5e173STim Haley if (!error) 101099d5e173STim Haley error = zfs_secpolicy_destroy(zc, cr); 101199d5e173STim Haley return (error); 101299d5e173STim Haley } 101399d5e173STim Haley 1014fa9e4066Sahrens /* 1015fa9e4066Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t. 1016fa9e4066Sahrens */ 1017fa9e4066Sahrens static int 1018478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 1019fa9e4066Sahrens { 1020fa9e4066Sahrens char *packed; 1021fa9e4066Sahrens int error; 1022990b4856Slling nvlist_t *list = NULL; 1023fa9e4066Sahrens 1024fa9e4066Sahrens /* 1025e9dbad6fSeschrock * Read in and unpack the user-supplied nvlist. 1026fa9e4066Sahrens */ 1027990b4856Slling if (size == 0) 1028fa9e4066Sahrens return (EINVAL); 1029fa9e4066Sahrens 1030fa9e4066Sahrens packed = kmem_alloc(size, KM_SLEEP); 1031fa9e4066Sahrens 1032478ed9adSEric Taylor if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 1033478ed9adSEric Taylor iflag)) != 0) { 1034fa9e4066Sahrens kmem_free(packed, size); 1035fa9e4066Sahrens return (error); 1036fa9e4066Sahrens } 1037fa9e4066Sahrens 1038990b4856Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 1039fa9e4066Sahrens kmem_free(packed, size); 1040fa9e4066Sahrens return (error); 1041fa9e4066Sahrens } 1042fa9e4066Sahrens 1043fa9e4066Sahrens kmem_free(packed, size); 1044fa9e4066Sahrens 1045990b4856Slling *nvp = list; 1046fa9e4066Sahrens return (0); 1047fa9e4066Sahrens } 1048fa9e4066Sahrens 104992241e0bSTom Erickson static int 105092241e0bSTom Erickson fit_error_list(zfs_cmd_t *zc, nvlist_t **errors) 105192241e0bSTom Erickson { 105292241e0bSTom Erickson size_t size; 105392241e0bSTom Erickson 105492241e0bSTom Erickson VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 105592241e0bSTom Erickson 105692241e0bSTom Erickson if (size > zc->zc_nvlist_dst_size) { 105792241e0bSTom Erickson nvpair_t *more_errors; 105892241e0bSTom Erickson int n = 0; 105992241e0bSTom Erickson 106092241e0bSTom Erickson if (zc->zc_nvlist_dst_size < 1024) 106192241e0bSTom Erickson return (ENOMEM); 106292241e0bSTom Erickson 106392241e0bSTom Erickson VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0); 106492241e0bSTom Erickson more_errors = nvlist_prev_nvpair(*errors, NULL); 106592241e0bSTom Erickson 106692241e0bSTom Erickson do { 106792241e0bSTom Erickson nvpair_t *pair = nvlist_prev_nvpair(*errors, 106892241e0bSTom Erickson more_errors); 106992241e0bSTom Erickson VERIFY(nvlist_remove_nvpair(*errors, pair) == 0); 107092241e0bSTom Erickson n++; 107192241e0bSTom Erickson VERIFY(nvlist_size(*errors, &size, 107292241e0bSTom Erickson NV_ENCODE_NATIVE) == 0); 107392241e0bSTom Erickson } while (size > zc->zc_nvlist_dst_size); 107492241e0bSTom Erickson 107592241e0bSTom Erickson VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0); 107692241e0bSTom Erickson VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0); 107792241e0bSTom Erickson ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 107892241e0bSTom Erickson ASSERT(size <= zc->zc_nvlist_dst_size); 107992241e0bSTom Erickson } 108092241e0bSTom Erickson 108192241e0bSTom Erickson return (0); 108292241e0bSTom Erickson } 108392241e0bSTom Erickson 1084e9dbad6fSeschrock static int 1085e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 1086e9dbad6fSeschrock { 1087e9dbad6fSeschrock char *packed = NULL; 10886e27f868SSam Falkner int error = 0; 1089e9dbad6fSeschrock size_t size; 1090e9dbad6fSeschrock 1091e9dbad6fSeschrock VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 1092e9dbad6fSeschrock 1093e9dbad6fSeschrock if (size > zc->zc_nvlist_dst_size) { 1094e9dbad6fSeschrock error = ENOMEM; 1095e9dbad6fSeschrock } else { 1096da165920Smarks packed = kmem_alloc(size, KM_SLEEP); 1097e9dbad6fSeschrock VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 1098e9dbad6fSeschrock KM_SLEEP) == 0); 10996e27f868SSam Falkner if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 11006e27f868SSam Falkner size, zc->zc_iflags) != 0) 11016e27f868SSam Falkner error = EFAULT; 1102e9dbad6fSeschrock kmem_free(packed, size); 1103e9dbad6fSeschrock } 1104e9dbad6fSeschrock 1105e9dbad6fSeschrock zc->zc_nvlist_dst_size = size; 1106e9dbad6fSeschrock return (error); 1107e9dbad6fSeschrock } 1108e9dbad6fSeschrock 110914843421SMatthew Ahrens static int 1110af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp) 111114843421SMatthew Ahrens { 111214843421SMatthew Ahrens objset_t *os; 111314843421SMatthew Ahrens int error; 111414843421SMatthew Ahrens 1115503ad85cSMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os); 111614843421SMatthew Ahrens if (error) 111714843421SMatthew Ahrens return (error); 1118503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 1119503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1120503ad85cSMatthew Ahrens return (EINVAL); 1121503ad85cSMatthew Ahrens } 112214843421SMatthew Ahrens 1123503ad85cSMatthew Ahrens mutex_enter(&os->os_user_ptr_lock); 1124af4c679fSSean McEnroe *zfvp = dmu_objset_get_user(os); 1125af4c679fSSean McEnroe if (*zfvp) { 1126af4c679fSSean McEnroe VFS_HOLD((*zfvp)->z_vfs); 112714843421SMatthew Ahrens } else { 112814843421SMatthew Ahrens error = ESRCH; 112914843421SMatthew Ahrens } 1130503ad85cSMatthew Ahrens mutex_exit(&os->os_user_ptr_lock); 1131503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 113214843421SMatthew Ahrens return (error); 113314843421SMatthew Ahrens } 113414843421SMatthew Ahrens 113514843421SMatthew Ahrens /* 113614843421SMatthew Ahrens * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 113714843421SMatthew Ahrens * case its z_vfs will be NULL, and it will be opened as the owner. 1138*ad135b5dSChristopher Siden * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER, 1139*ad135b5dSChristopher Siden * which prevents all vnode ops from running. 114014843421SMatthew Ahrens */ 114114843421SMatthew Ahrens static int 11421412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer) 114314843421SMatthew Ahrens { 114414843421SMatthew Ahrens int error = 0; 114514843421SMatthew Ahrens 1146af4c679fSSean McEnroe if (getzfsvfs(name, zfvp) != 0) 1147af4c679fSSean McEnroe error = zfsvfs_create(name, zfvp); 114814843421SMatthew Ahrens if (error == 0) { 11491412a1a2SMark Shellenbaum rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER : 11501412a1a2SMark Shellenbaum RW_READER, tag); 1151af4c679fSSean McEnroe if ((*zfvp)->z_unmounted) { 115214843421SMatthew Ahrens /* 115314843421SMatthew Ahrens * XXX we could probably try again, since the unmounting 115414843421SMatthew Ahrens * thread should be just about to disassociate the 115514843421SMatthew Ahrens * objset from the zfsvfs. 115614843421SMatthew Ahrens */ 1157af4c679fSSean McEnroe rrw_exit(&(*zfvp)->z_teardown_lock, tag); 115814843421SMatthew Ahrens return (EBUSY); 115914843421SMatthew Ahrens } 116014843421SMatthew Ahrens } 116114843421SMatthew Ahrens return (error); 116214843421SMatthew Ahrens } 116314843421SMatthew Ahrens 116414843421SMatthew Ahrens static void 116514843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 116614843421SMatthew Ahrens { 116714843421SMatthew Ahrens rrw_exit(&zfsvfs->z_teardown_lock, tag); 116814843421SMatthew Ahrens 116914843421SMatthew Ahrens if (zfsvfs->z_vfs) { 117014843421SMatthew Ahrens VFS_RELE(zfsvfs->z_vfs); 117114843421SMatthew Ahrens } else { 1172503ad85cSMatthew Ahrens dmu_objset_disown(zfsvfs->z_os, zfsvfs); 117314843421SMatthew Ahrens zfsvfs_free(zfsvfs); 117414843421SMatthew Ahrens } 117514843421SMatthew Ahrens } 117614843421SMatthew Ahrens 1177fa9e4066Sahrens static int 1178fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc) 1179fa9e4066Sahrens { 1180fa9e4066Sahrens int error; 1181990b4856Slling nvlist_t *config, *props = NULL; 11820a48a24eStimh nvlist_t *rootprops = NULL; 11830a48a24eStimh nvlist_t *zplprops = NULL; 1184228975ccSek char *buf; 1185fa9e4066Sahrens 1186990b4856Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1187478ed9adSEric Taylor zc->zc_iflags, &config)) 1188fa9e4066Sahrens return (error); 11892a6b87f0Sek 1190990b4856Slling if (zc->zc_nvlist_src_size != 0 && (error = 1191478ed9adSEric Taylor get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1192478ed9adSEric Taylor zc->zc_iflags, &props))) { 1193990b4856Slling nvlist_free(config); 1194990b4856Slling return (error); 1195990b4856Slling } 1196990b4856Slling 11970a48a24eStimh if (props) { 11980a48a24eStimh nvlist_t *nvl = NULL; 11990a48a24eStimh uint64_t version = SPA_VERSION; 12000a48a24eStimh 12010a48a24eStimh (void) nvlist_lookup_uint64(props, 12020a48a24eStimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 1203*ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(version)) { 12040a48a24eStimh error = EINVAL; 12050a48a24eStimh goto pool_props_bad; 12060a48a24eStimh } 12070a48a24eStimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 12080a48a24eStimh if (nvl) { 12090a48a24eStimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 12100a48a24eStimh if (error != 0) { 12110a48a24eStimh nvlist_free(config); 12120a48a24eStimh nvlist_free(props); 12130a48a24eStimh return (error); 12140a48a24eStimh } 12150a48a24eStimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 12160a48a24eStimh } 12170a48a24eStimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 12180a48a24eStimh error = zfs_fill_zplprops_root(version, rootprops, 12190a48a24eStimh zplprops, NULL); 12200a48a24eStimh if (error) 12210a48a24eStimh goto pool_props_bad; 12220a48a24eStimh } 12230a48a24eStimh 12242a6b87f0Sek buf = history_str_get(zc); 1225fa9e4066Sahrens 12260a48a24eStimh error = spa_create(zc->zc_name, config, props, buf, zplprops); 12270a48a24eStimh 12280a48a24eStimh /* 12290a48a24eStimh * Set the remaining root properties 12300a48a24eStimh */ 123192241e0bSTom Erickson if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, 123292241e0bSTom Erickson ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) 12330a48a24eStimh (void) spa_destroy(zc->zc_name); 1234fa9e4066Sahrens 12352a6b87f0Sek if (buf != NULL) 12362a6b87f0Sek history_str_free(buf); 1237990b4856Slling 12380a48a24eStimh pool_props_bad: 12390a48a24eStimh nvlist_free(rootprops); 12400a48a24eStimh nvlist_free(zplprops); 1241fa9e4066Sahrens nvlist_free(config); 12420a48a24eStimh nvlist_free(props); 1243990b4856Slling 1244fa9e4066Sahrens return (error); 1245fa9e4066Sahrens } 1246fa9e4066Sahrens 1247fa9e4066Sahrens static int 1248fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1249fa9e4066Sahrens { 1250ecd6cf80Smarks int error; 1251ecd6cf80Smarks zfs_log_history(zc); 1252ecd6cf80Smarks error = spa_destroy(zc->zc_name); 1253681d9761SEric Taylor if (error == 0) 1254681d9761SEric Taylor zvol_remove_minors(zc->zc_name); 1255ecd6cf80Smarks return (error); 1256fa9e4066Sahrens } 1257fa9e4066Sahrens 1258fa9e4066Sahrens static int 1259fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc) 1260fa9e4066Sahrens { 1261990b4856Slling nvlist_t *config, *props = NULL; 1262fa9e4066Sahrens uint64_t guid; 1263468c413aSTim Haley int error; 1264fa9e4066Sahrens 1265990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1266478ed9adSEric Taylor zc->zc_iflags, &config)) != 0) 1267990b4856Slling return (error); 1268990b4856Slling 1269990b4856Slling if (zc->zc_nvlist_src_size != 0 && (error = 1270478ed9adSEric Taylor get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1271478ed9adSEric Taylor zc->zc_iflags, &props))) { 1272990b4856Slling nvlist_free(config); 1273fa9e4066Sahrens return (error); 1274990b4856Slling } 1275fa9e4066Sahrens 1276fa9e4066Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1277ea8dc4b6Seschrock guid != zc->zc_guid) 1278fa9e4066Sahrens error = EINVAL; 1279fa9e4066Sahrens else 12804b964adaSGeorge Wilson error = spa_import(zc->zc_name, config, props, zc->zc_cookie); 1281fa9e4066Sahrens 12824b964adaSGeorge Wilson if (zc->zc_nvlist_dst != 0) { 12834b964adaSGeorge Wilson int err; 12844b964adaSGeorge Wilson 12854b964adaSGeorge Wilson if ((err = put_nvlist(zc, config)) != 0) 12864b964adaSGeorge Wilson error = err; 12874b964adaSGeorge Wilson } 1288468c413aSTim Haley 1289fa9e4066Sahrens nvlist_free(config); 1290fa9e4066Sahrens 1291990b4856Slling if (props) 1292990b4856Slling nvlist_free(props); 1293990b4856Slling 1294fa9e4066Sahrens return (error); 1295fa9e4066Sahrens } 1296fa9e4066Sahrens 1297fa9e4066Sahrens static int 1298fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc) 1299fa9e4066Sahrens { 1300ecd6cf80Smarks int error; 130189a89ebfSlling boolean_t force = (boolean_t)zc->zc_cookie; 1302394ab0cbSGeorge Wilson boolean_t hardforce = (boolean_t)zc->zc_guid; 130389a89ebfSlling 1304ecd6cf80Smarks zfs_log_history(zc); 1305394ab0cbSGeorge Wilson error = spa_export(zc->zc_name, NULL, force, hardforce); 1306681d9761SEric Taylor if (error == 0) 1307681d9761SEric Taylor zvol_remove_minors(zc->zc_name); 1308ecd6cf80Smarks return (error); 1309fa9e4066Sahrens } 1310fa9e4066Sahrens 1311fa9e4066Sahrens static int 1312fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 1313fa9e4066Sahrens { 1314fa9e4066Sahrens nvlist_t *configs; 1315fa9e4066Sahrens int error; 1316fa9e4066Sahrens 1317fa9e4066Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1318fa9e4066Sahrens return (EEXIST); 1319fa9e4066Sahrens 1320e9dbad6fSeschrock error = put_nvlist(zc, configs); 1321fa9e4066Sahrens 1322fa9e4066Sahrens nvlist_free(configs); 1323fa9e4066Sahrens 1324fa9e4066Sahrens return (error); 1325fa9e4066Sahrens } 1326fa9e4066Sahrens 1327*ad135b5dSChristopher Siden /* 1328*ad135b5dSChristopher Siden * inputs: 1329*ad135b5dSChristopher Siden * zc_name name of the pool 1330*ad135b5dSChristopher Siden * 1331*ad135b5dSChristopher Siden * outputs: 1332*ad135b5dSChristopher Siden * zc_cookie real errno 1333*ad135b5dSChristopher Siden * zc_nvlist_dst config nvlist 1334*ad135b5dSChristopher Siden * zc_nvlist_dst_size size of config nvlist 1335*ad135b5dSChristopher Siden */ 1336fa9e4066Sahrens static int 1337fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 1338fa9e4066Sahrens { 1339fa9e4066Sahrens nvlist_t *config; 1340fa9e4066Sahrens int error; 1341ea8dc4b6Seschrock int ret = 0; 1342fa9e4066Sahrens 1343e9dbad6fSeschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 1344e9dbad6fSeschrock sizeof (zc->zc_value)); 1345fa9e4066Sahrens 1346fa9e4066Sahrens if (config != NULL) { 1347e9dbad6fSeschrock ret = put_nvlist(zc, config); 1348fa9e4066Sahrens nvlist_free(config); 1349ea8dc4b6Seschrock 1350ea8dc4b6Seschrock /* 1351ea8dc4b6Seschrock * The config may be present even if 'error' is non-zero. 1352ea8dc4b6Seschrock * In this case we return success, and preserve the real errno 1353ea8dc4b6Seschrock * in 'zc_cookie'. 1354ea8dc4b6Seschrock */ 1355ea8dc4b6Seschrock zc->zc_cookie = error; 1356fa9e4066Sahrens } else { 1357ea8dc4b6Seschrock ret = error; 1358fa9e4066Sahrens } 1359fa9e4066Sahrens 1360ea8dc4b6Seschrock return (ret); 1361fa9e4066Sahrens } 1362fa9e4066Sahrens 1363fa9e4066Sahrens /* 1364fa9e4066Sahrens * Try to import the given pool, returning pool stats as appropriate so that 1365fa9e4066Sahrens * user land knows which devices are available and overall pool health. 1366fa9e4066Sahrens */ 1367fa9e4066Sahrens static int 1368fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1369fa9e4066Sahrens { 1370fa9e4066Sahrens nvlist_t *tryconfig, *config; 1371fa9e4066Sahrens int error; 1372fa9e4066Sahrens 1373990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1374478ed9adSEric Taylor zc->zc_iflags, &tryconfig)) != 0) 1375fa9e4066Sahrens return (error); 1376fa9e4066Sahrens 1377fa9e4066Sahrens config = spa_tryimport(tryconfig); 1378fa9e4066Sahrens 1379fa9e4066Sahrens nvlist_free(tryconfig); 1380fa9e4066Sahrens 1381fa9e4066Sahrens if (config == NULL) 1382fa9e4066Sahrens return (EINVAL); 1383fa9e4066Sahrens 1384e9dbad6fSeschrock error = put_nvlist(zc, config); 1385fa9e4066Sahrens nvlist_free(config); 1386fa9e4066Sahrens 1387fa9e4066Sahrens return (error); 1388fa9e4066Sahrens } 1389fa9e4066Sahrens 13903f9d6ad7SLin Ling /* 13913f9d6ad7SLin Ling * inputs: 13923f9d6ad7SLin Ling * zc_name name of the pool 13933f9d6ad7SLin Ling * zc_cookie scan func (pool_scan_func_t) 13943f9d6ad7SLin Ling */ 1395fa9e4066Sahrens static int 13963f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc) 1397fa9e4066Sahrens { 1398fa9e4066Sahrens spa_t *spa; 1399fa9e4066Sahrens int error; 1400fa9e4066Sahrens 140106eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 140206eeb2adSek return (error); 140306eeb2adSek 14043f9d6ad7SLin Ling if (zc->zc_cookie == POOL_SCAN_NONE) 14053f9d6ad7SLin Ling error = spa_scan_stop(spa); 14063f9d6ad7SLin Ling else 14073f9d6ad7SLin Ling error = spa_scan(spa, zc->zc_cookie); 140806eeb2adSek 140906eeb2adSek spa_close(spa, FTAG); 141006eeb2adSek 1411fa9e4066Sahrens return (error); 1412fa9e4066Sahrens } 1413fa9e4066Sahrens 1414fa9e4066Sahrens static int 1415fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1416fa9e4066Sahrens { 1417fa9e4066Sahrens spa_t *spa; 1418fa9e4066Sahrens int error; 1419fa9e4066Sahrens 1420fa9e4066Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1421fa9e4066Sahrens if (error == 0) { 1422fa9e4066Sahrens spa_freeze(spa); 1423fa9e4066Sahrens spa_close(spa, FTAG); 1424fa9e4066Sahrens } 1425fa9e4066Sahrens return (error); 1426fa9e4066Sahrens } 1427fa9e4066Sahrens 1428eaca9bbdSeschrock static int 1429eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 1430eaca9bbdSeschrock { 1431eaca9bbdSeschrock spa_t *spa; 1432eaca9bbdSeschrock int error; 1433eaca9bbdSeschrock 143406eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 143506eeb2adSek return (error); 143606eeb2adSek 1437*ad135b5dSChristopher Siden if (zc->zc_cookie < spa_version(spa) || 1438*ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) { 1439558d2d50Slling spa_close(spa, FTAG); 1440558d2d50Slling return (EINVAL); 1441558d2d50Slling } 1442558d2d50Slling 1443990b4856Slling spa_upgrade(spa, zc->zc_cookie); 144406eeb2adSek spa_close(spa, FTAG); 144506eeb2adSek 144606eeb2adSek return (error); 144706eeb2adSek } 144806eeb2adSek 144906eeb2adSek static int 145006eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc) 145106eeb2adSek { 145206eeb2adSek spa_t *spa; 145306eeb2adSek char *hist_buf; 145406eeb2adSek uint64_t size; 145506eeb2adSek int error; 145606eeb2adSek 145706eeb2adSek if ((size = zc->zc_history_len) == 0) 145806eeb2adSek return (EINVAL); 145906eeb2adSek 146006eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 146106eeb2adSek return (error); 146206eeb2adSek 1463e7437265Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 1464d7306b64Sek spa_close(spa, FTAG); 1465d7306b64Sek return (ENOTSUP); 1466d7306b64Sek } 1467d7306b64Sek 146806eeb2adSek hist_buf = kmem_alloc(size, KM_SLEEP); 146906eeb2adSek if ((error = spa_history_get(spa, &zc->zc_history_offset, 147006eeb2adSek &zc->zc_history_len, hist_buf)) == 0) { 1471478ed9adSEric Taylor error = ddi_copyout(hist_buf, 1472478ed9adSEric Taylor (void *)(uintptr_t)zc->zc_history, 1473478ed9adSEric Taylor zc->zc_history_len, zc->zc_iflags); 147406eeb2adSek } 147506eeb2adSek 147606eeb2adSek spa_close(spa, FTAG); 147706eeb2adSek kmem_free(hist_buf, size); 147806eeb2adSek return (error); 147906eeb2adSek } 148006eeb2adSek 1481e9103aaeSGarrett D'Amore static int 1482e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc) 1483e9103aaeSGarrett D'Amore { 1484e9103aaeSGarrett D'Amore spa_t *spa; 1485e9103aaeSGarrett D'Amore int error; 1486e9103aaeSGarrett D'Amore 1487e9103aaeSGarrett D'Amore error = spa_open(zc->zc_name, &spa, FTAG); 1488e9103aaeSGarrett D'Amore if (error == 0) { 1489e9103aaeSGarrett D'Amore error = spa_change_guid(spa); 1490e9103aaeSGarrett D'Amore spa_close(spa, FTAG); 1491e9103aaeSGarrett D'Amore } 1492e9103aaeSGarrett D'Amore return (error); 1493e9103aaeSGarrett D'Amore } 1494e9103aaeSGarrett D'Amore 149555434c77Sek static int 149655434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 149755434c77Sek { 149855434c77Sek int error; 149955434c77Sek 1500b1b8ab34Slling if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 150155434c77Sek return (error); 150255434c77Sek 150355434c77Sek return (0); 150455434c77Sek } 150555434c77Sek 1506503ad85cSMatthew Ahrens /* 1507503ad85cSMatthew Ahrens * inputs: 1508503ad85cSMatthew Ahrens * zc_name name of filesystem 1509503ad85cSMatthew Ahrens * zc_obj object to find 1510503ad85cSMatthew Ahrens * 1511503ad85cSMatthew Ahrens * outputs: 1512503ad85cSMatthew Ahrens * zc_value name of object 1513503ad85cSMatthew Ahrens */ 151455434c77Sek static int 151555434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc) 151655434c77Sek { 1517503ad85cSMatthew Ahrens objset_t *os; 151855434c77Sek int error; 151955434c77Sek 1520503ad85cSMatthew Ahrens /* XXX reading from objset not owned */ 1521503ad85cSMatthew Ahrens if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 152255434c77Sek return (error); 1523503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 1524503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1525503ad85cSMatthew Ahrens return (EINVAL); 1526503ad85cSMatthew Ahrens } 1527503ad85cSMatthew Ahrens error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 152855434c77Sek sizeof (zc->zc_value)); 1529503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 153055434c77Sek 153155434c77Sek return (error); 153255434c77Sek } 153355434c77Sek 153499d5e173STim Haley /* 153599d5e173STim Haley * inputs: 153699d5e173STim Haley * zc_name name of filesystem 153799d5e173STim Haley * zc_obj object to find 153899d5e173STim Haley * 153999d5e173STim Haley * outputs: 154099d5e173STim Haley * zc_stat stats on object 154199d5e173STim Haley * zc_value path to object 154299d5e173STim Haley */ 154399d5e173STim Haley static int 154499d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc) 154599d5e173STim Haley { 154699d5e173STim Haley objset_t *os; 154799d5e173STim Haley int error; 154899d5e173STim Haley 154999d5e173STim Haley /* XXX reading from objset not owned */ 155099d5e173STim Haley if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 155199d5e173STim Haley return (error); 155299d5e173STim Haley if (dmu_objset_type(os) != DMU_OST_ZFS) { 155399d5e173STim Haley dmu_objset_rele(os, FTAG); 155499d5e173STim Haley return (EINVAL); 155599d5e173STim Haley } 155699d5e173STim Haley error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value, 155799d5e173STim Haley sizeof (zc->zc_value)); 155899d5e173STim Haley dmu_objset_rele(os, FTAG); 155999d5e173STim Haley 156099d5e173STim Haley return (error); 156199d5e173STim Haley } 156299d5e173STim Haley 1563fa9e4066Sahrens static int 1564fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1565fa9e4066Sahrens { 1566fa9e4066Sahrens spa_t *spa; 1567fa9e4066Sahrens int error; 1568e7cbe64fSgw nvlist_t *config, **l2cache, **spares; 1569e7cbe64fSgw uint_t nl2cache = 0, nspares = 0; 1570fa9e4066Sahrens 1571fa9e4066Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1572fa9e4066Sahrens if (error != 0) 1573fa9e4066Sahrens return (error); 1574fa9e4066Sahrens 1575fa94a07fSbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1576478ed9adSEric Taylor zc->zc_iflags, &config); 1577fa94a07fSbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 1578fa94a07fSbrendan &l2cache, &nl2cache); 1579fa94a07fSbrendan 1580e7cbe64fSgw (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 1581e7cbe64fSgw &spares, &nspares); 1582e7cbe64fSgw 1583b1b8ab34Slling /* 1584b1b8ab34Slling * A root pool with concatenated devices is not supported. 1585e7cbe64fSgw * Thus, can not add a device to a root pool. 1586e7cbe64fSgw * 1587e7cbe64fSgw * Intent log device can not be added to a rootpool because 1588e7cbe64fSgw * during mountroot, zil is replayed, a seperated log device 1589e7cbe64fSgw * can not be accessed during the mountroot time. 1590e7cbe64fSgw * 1591e7cbe64fSgw * l2cache and spare devices are ok to be added to a rootpool. 1592b1b8ab34Slling */ 1593b24ab676SJeff Bonwick if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) { 15941195e687SMark J Musante nvlist_free(config); 1595b1b8ab34Slling spa_close(spa, FTAG); 1596b1b8ab34Slling return (EDOM); 1597b1b8ab34Slling } 1598b1b8ab34Slling 1599fa94a07fSbrendan if (error == 0) { 1600fa9e4066Sahrens error = spa_vdev_add(spa, config); 1601fa9e4066Sahrens nvlist_free(config); 1602fa9e4066Sahrens } 1603fa9e4066Sahrens spa_close(spa, FTAG); 1604fa9e4066Sahrens return (error); 1605fa9e4066Sahrens } 1606fa9e4066Sahrens 16073f9d6ad7SLin Ling /* 16083f9d6ad7SLin Ling * inputs: 16093f9d6ad7SLin Ling * zc_name name of the pool 16103f9d6ad7SLin Ling * zc_nvlist_conf nvlist of devices to remove 16113f9d6ad7SLin Ling * zc_cookie to stop the remove? 16123f9d6ad7SLin Ling */ 1613fa9e4066Sahrens static int 1614fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1615fa9e4066Sahrens { 161699653d4eSeschrock spa_t *spa; 161799653d4eSeschrock int error; 161899653d4eSeschrock 161999653d4eSeschrock error = spa_open(zc->zc_name, &spa, FTAG); 162099653d4eSeschrock if (error != 0) 162199653d4eSeschrock return (error); 162299653d4eSeschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 162399653d4eSeschrock spa_close(spa, FTAG); 162499653d4eSeschrock return (error); 1625fa9e4066Sahrens } 1626fa9e4066Sahrens 1627fa9e4066Sahrens static int 16283d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1629fa9e4066Sahrens { 1630fa9e4066Sahrens spa_t *spa; 1631fa9e4066Sahrens int error; 16323d7072f8Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1633fa9e4066Sahrens 163406eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1635fa9e4066Sahrens return (error); 16363d7072f8Seschrock switch (zc->zc_cookie) { 16373d7072f8Seschrock case VDEV_STATE_ONLINE: 16383d7072f8Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 16393d7072f8Seschrock break; 1640fa9e4066Sahrens 16413d7072f8Seschrock case VDEV_STATE_OFFLINE: 16423d7072f8Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 16433d7072f8Seschrock break; 1644fa9e4066Sahrens 16453d7072f8Seschrock case VDEV_STATE_FAULTED: 1646069f55e2SEric Schrock if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1647069f55e2SEric Schrock zc->zc_obj != VDEV_AUX_EXTERNAL) 1648069f55e2SEric Schrock zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1649069f55e2SEric Schrock 1650069f55e2SEric Schrock error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 16513d7072f8Seschrock break; 16523d7072f8Seschrock 16533d7072f8Seschrock case VDEV_STATE_DEGRADED: 1654069f55e2SEric Schrock if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1655069f55e2SEric Schrock zc->zc_obj != VDEV_AUX_EXTERNAL) 1656069f55e2SEric Schrock zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1657069f55e2SEric Schrock 1658069f55e2SEric Schrock error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 16593d7072f8Seschrock break; 16603d7072f8Seschrock 16613d7072f8Seschrock default: 16623d7072f8Seschrock error = EINVAL; 16633d7072f8Seschrock } 16643d7072f8Seschrock zc->zc_cookie = newstate; 1665fa9e4066Sahrens spa_close(spa, FTAG); 1666fa9e4066Sahrens return (error); 1667fa9e4066Sahrens } 1668fa9e4066Sahrens 1669fa9e4066Sahrens static int 1670fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1671fa9e4066Sahrens { 1672fa9e4066Sahrens spa_t *spa; 1673fa9e4066Sahrens int replacing = zc->zc_cookie; 1674fa9e4066Sahrens nvlist_t *config; 1675fa9e4066Sahrens int error; 1676fa9e4066Sahrens 167706eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1678fa9e4066Sahrens return (error); 1679fa9e4066Sahrens 1680990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1681478ed9adSEric Taylor zc->zc_iflags, &config)) == 0) { 1682ea8dc4b6Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1683fa9e4066Sahrens nvlist_free(config); 1684fa9e4066Sahrens } 1685fa9e4066Sahrens 1686fa9e4066Sahrens spa_close(spa, FTAG); 1687fa9e4066Sahrens return (error); 1688fa9e4066Sahrens } 1689fa9e4066Sahrens 1690fa9e4066Sahrens static int 1691fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1692fa9e4066Sahrens { 1693fa9e4066Sahrens spa_t *spa; 1694fa9e4066Sahrens int error; 1695fa9e4066Sahrens 169606eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1697fa9e4066Sahrens return (error); 1698fa9e4066Sahrens 16998ad4d6ddSJeff Bonwick error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1700fa9e4066Sahrens 1701fa9e4066Sahrens spa_close(spa, FTAG); 1702fa9e4066Sahrens return (error); 1703fa9e4066Sahrens } 1704fa9e4066Sahrens 17051195e687SMark J Musante static int 17061195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc) 17071195e687SMark J Musante { 17081195e687SMark J Musante spa_t *spa; 17091195e687SMark J Musante nvlist_t *config, *props = NULL; 17101195e687SMark J Musante int error; 17111195e687SMark J Musante boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); 17121195e687SMark J Musante 17131195e687SMark J Musante if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 17141195e687SMark J Musante return (error); 17151195e687SMark J Musante 17161195e687SMark J Musante if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 17171195e687SMark J Musante zc->zc_iflags, &config)) { 17181195e687SMark J Musante spa_close(spa, FTAG); 17191195e687SMark J Musante return (error); 17201195e687SMark J Musante } 17211195e687SMark J Musante 17221195e687SMark J Musante if (zc->zc_nvlist_src_size != 0 && (error = 17231195e687SMark J Musante get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 17241195e687SMark J Musante zc->zc_iflags, &props))) { 17251195e687SMark J Musante spa_close(spa, FTAG); 17261195e687SMark J Musante nvlist_free(config); 17271195e687SMark J Musante return (error); 17281195e687SMark J Musante } 17291195e687SMark J Musante 17301195e687SMark J Musante error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); 17311195e687SMark J Musante 17321195e687SMark J Musante spa_close(spa, FTAG); 17331195e687SMark J Musante 17341195e687SMark J Musante nvlist_free(config); 17351195e687SMark J Musante nvlist_free(props); 17361195e687SMark J Musante 17371195e687SMark J Musante return (error); 17381195e687SMark J Musante } 17391195e687SMark J Musante 1740c67d9675Seschrock static int 1741c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 1742c67d9675Seschrock { 1743c67d9675Seschrock spa_t *spa; 1744e9dbad6fSeschrock char *path = zc->zc_value; 1745ea8dc4b6Seschrock uint64_t guid = zc->zc_guid; 1746c67d9675Seschrock int error; 1747c67d9675Seschrock 1748c67d9675Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 1749c67d9675Seschrock if (error != 0) 1750c67d9675Seschrock return (error); 1751c67d9675Seschrock 1752c67d9675Seschrock error = spa_vdev_setpath(spa, guid, path); 1753c67d9675Seschrock spa_close(spa, FTAG); 1754c67d9675Seschrock return (error); 1755c67d9675Seschrock } 1756c67d9675Seschrock 17576809eb4eSEric Schrock static int 17586809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 17596809eb4eSEric Schrock { 17606809eb4eSEric Schrock spa_t *spa; 17616809eb4eSEric Schrock char *fru = zc->zc_value; 17626809eb4eSEric Schrock uint64_t guid = zc->zc_guid; 17636809eb4eSEric Schrock int error; 17646809eb4eSEric Schrock 17656809eb4eSEric Schrock error = spa_open(zc->zc_name, &spa, FTAG); 17666809eb4eSEric Schrock if (error != 0) 17676809eb4eSEric Schrock return (error); 17686809eb4eSEric Schrock 17696809eb4eSEric Schrock error = spa_vdev_setfru(spa, guid, fru); 17706809eb4eSEric Schrock spa_close(spa, FTAG); 17716809eb4eSEric Schrock return (error); 17726809eb4eSEric Schrock } 17736809eb4eSEric Schrock 1774fa9e4066Sahrens static int 1775a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os) 1776fa9e4066Sahrens { 1777a7f53a56SChris Kirby int error = 0; 17787f7322feSeschrock nvlist_t *nv; 1779fa9e4066Sahrens 1780a2eea2e1Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1781fa9e4066Sahrens 17825ad82045Snd if (zc->zc_nvlist_dst != 0 && 178392241e0bSTom Erickson (error = dsl_prop_get_all(os, &nv)) == 0) { 1784a2eea2e1Sahrens dmu_objset_stats(os, nv); 1785432f72fdSahrens /* 1786bd00f61bSrm * NB: zvol_get_stats() will read the objset contents, 1787432f72fdSahrens * which we aren't supposed to do with a 1788745cd3c5Smaybee * DS_MODE_USER hold, because it could be 1789432f72fdSahrens * inconsistent. So this is a bit of a workaround... 1790503ad85cSMatthew Ahrens * XXX reading with out owning 1791432f72fdSahrens */ 179219b94df9SMatthew Ahrens if (!zc->zc_objset_stats.dds_inconsistent && 179319b94df9SMatthew Ahrens dmu_objset_type(os) == DMU_OST_ZVOL) { 179419b94df9SMatthew Ahrens error = zvol_get_stats(os, nv); 179519b94df9SMatthew Ahrens if (error == EIO) 179619b94df9SMatthew Ahrens return (error); 179719b94df9SMatthew Ahrens VERIFY3S(error, ==, 0); 1798e7437265Sahrens } 1799e9dbad6fSeschrock error = put_nvlist(zc, nv); 18007f7322feSeschrock nvlist_free(nv); 18017f7322feSeschrock } 1802fa9e4066Sahrens 1803a7f53a56SChris Kirby return (error); 1804a7f53a56SChris Kirby } 1805a7f53a56SChris Kirby 1806a7f53a56SChris Kirby /* 1807a7f53a56SChris Kirby * inputs: 1808a7f53a56SChris Kirby * zc_name name of filesystem 1809a7f53a56SChris Kirby * zc_nvlist_dst_size size of buffer for property nvlist 1810a7f53a56SChris Kirby * 1811a7f53a56SChris Kirby * outputs: 1812a7f53a56SChris Kirby * zc_objset_stats stats 1813a7f53a56SChris Kirby * zc_nvlist_dst property nvlist 1814a7f53a56SChris Kirby * zc_nvlist_dst_size size of property nvlist 1815a7f53a56SChris Kirby */ 1816a7f53a56SChris Kirby static int 1817a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc) 1818a7f53a56SChris Kirby { 1819a7f53a56SChris Kirby objset_t *os = NULL; 1820a7f53a56SChris Kirby int error; 1821a7f53a56SChris Kirby 1822a7f53a56SChris Kirby if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1823a7f53a56SChris Kirby return (error); 1824a7f53a56SChris Kirby 1825a7f53a56SChris Kirby error = zfs_ioc_objset_stats_impl(zc, os); 1826a7f53a56SChris Kirby 1827503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1828a7f53a56SChris Kirby 1829fa9e4066Sahrens return (error); 1830fa9e4066Sahrens } 1831fa9e4066Sahrens 183292241e0bSTom Erickson /* 183392241e0bSTom Erickson * inputs: 183492241e0bSTom Erickson * zc_name name of filesystem 183592241e0bSTom Erickson * zc_nvlist_dst_size size of buffer for property nvlist 183692241e0bSTom Erickson * 183792241e0bSTom Erickson * outputs: 183892241e0bSTom Erickson * zc_nvlist_dst received property nvlist 183992241e0bSTom Erickson * zc_nvlist_dst_size size of received property nvlist 184092241e0bSTom Erickson * 184192241e0bSTom Erickson * Gets received properties (distinct from local properties on or after 184292241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 184392241e0bSTom Erickson * local property values. 184492241e0bSTom Erickson */ 184592241e0bSTom Erickson static int 184692241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 184792241e0bSTom Erickson { 184892241e0bSTom Erickson objset_t *os = NULL; 184992241e0bSTom Erickson int error; 185092241e0bSTom Erickson nvlist_t *nv; 185192241e0bSTom Erickson 185292241e0bSTom Erickson if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 185392241e0bSTom Erickson return (error); 185492241e0bSTom Erickson 185592241e0bSTom Erickson /* 185692241e0bSTom Erickson * Without this check, we would return local property values if the 185792241e0bSTom Erickson * caller has not already received properties on or after 185892241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS. 185992241e0bSTom Erickson */ 186092241e0bSTom Erickson if (!dsl_prop_get_hasrecvd(os)) { 186192241e0bSTom Erickson dmu_objset_rele(os, FTAG); 186292241e0bSTom Erickson return (ENOTSUP); 186392241e0bSTom Erickson } 186492241e0bSTom Erickson 186592241e0bSTom Erickson if (zc->zc_nvlist_dst != 0 && 186692241e0bSTom Erickson (error = dsl_prop_get_received(os, &nv)) == 0) { 186792241e0bSTom Erickson error = put_nvlist(zc, nv); 186892241e0bSTom Erickson nvlist_free(nv); 186992241e0bSTom Erickson } 187092241e0bSTom Erickson 187192241e0bSTom Erickson dmu_objset_rele(os, FTAG); 187292241e0bSTom Erickson return (error); 187392241e0bSTom Erickson } 187492241e0bSTom Erickson 1875de8267e0Stimh static int 1876de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 1877de8267e0Stimh { 1878de8267e0Stimh uint64_t value; 1879de8267e0Stimh int error; 1880de8267e0Stimh 1881de8267e0Stimh /* 1882de8267e0Stimh * zfs_get_zplprop() will either find a value or give us 1883de8267e0Stimh * the default value (if there is one). 1884de8267e0Stimh */ 1885de8267e0Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 1886de8267e0Stimh return (error); 1887de8267e0Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 1888de8267e0Stimh return (0); 1889de8267e0Stimh } 1890de8267e0Stimh 18913cb34c60Sahrens /* 18923cb34c60Sahrens * inputs: 18933cb34c60Sahrens * zc_name name of filesystem 1894de8267e0Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 18953cb34c60Sahrens * 18963cb34c60Sahrens * outputs: 1897de8267e0Stimh * zc_nvlist_dst zpl property nvlist 1898de8267e0Stimh * zc_nvlist_dst_size size of zpl property nvlist 18993cb34c60Sahrens */ 1900bd00f61bSrm static int 1901de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 1902bd00f61bSrm { 1903de8267e0Stimh objset_t *os; 1904de8267e0Stimh int err; 1905bd00f61bSrm 1906503ad85cSMatthew Ahrens /* XXX reading without owning */ 1907503ad85cSMatthew Ahrens if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1908de8267e0Stimh return (err); 1909bd00f61bSrm 1910bd00f61bSrm dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1911bd00f61bSrm 1912bd00f61bSrm /* 1913de8267e0Stimh * NB: nvl_add_zplprop() will read the objset contents, 1914745cd3c5Smaybee * which we aren't supposed to do with a DS_MODE_USER 1915745cd3c5Smaybee * hold, because it could be inconsistent. 1916bd00f61bSrm */ 1917de8267e0Stimh if (zc->zc_nvlist_dst != NULL && 1918de8267e0Stimh !zc->zc_objset_stats.dds_inconsistent && 1919de8267e0Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 1920de8267e0Stimh nvlist_t *nv; 1921de8267e0Stimh 1922de8267e0Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1923de8267e0Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 1924de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 1925de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 1926de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 1927de8267e0Stimh err = put_nvlist(zc, nv); 1928de8267e0Stimh nvlist_free(nv); 1929de8267e0Stimh } else { 1930de8267e0Stimh err = ENOENT; 1931de8267e0Stimh } 1932503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1933de8267e0Stimh return (err); 1934bd00f61bSrm } 1935bd00f61bSrm 193614843421SMatthew Ahrens static boolean_t 193714843421SMatthew Ahrens dataset_name_hidden(const char *name) 193814843421SMatthew Ahrens { 193914843421SMatthew Ahrens /* 194014843421SMatthew Ahrens * Skip over datasets that are not visible in this zone, 194114843421SMatthew Ahrens * internal datasets (which have a $ in their name), and 194214843421SMatthew Ahrens * temporary datasets (which have a % in their name). 194314843421SMatthew Ahrens */ 194414843421SMatthew Ahrens if (strchr(name, '$') != NULL) 194514843421SMatthew Ahrens return (B_TRUE); 194614843421SMatthew Ahrens if (strchr(name, '%') != NULL) 194714843421SMatthew Ahrens return (B_TRUE); 194814843421SMatthew Ahrens if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) 194914843421SMatthew Ahrens return (B_TRUE); 195014843421SMatthew Ahrens return (B_FALSE); 195114843421SMatthew Ahrens } 195214843421SMatthew Ahrens 1953de8267e0Stimh /* 1954de8267e0Stimh * inputs: 1955de8267e0Stimh * zc_name name of filesystem 1956de8267e0Stimh * zc_cookie zap cursor 1957de8267e0Stimh * zc_nvlist_dst_size size of buffer for property nvlist 1958de8267e0Stimh * 1959de8267e0Stimh * outputs: 1960de8267e0Stimh * zc_name name of next filesystem 196114843421SMatthew Ahrens * zc_cookie zap cursor 1962de8267e0Stimh * zc_objset_stats stats 1963de8267e0Stimh * zc_nvlist_dst property nvlist 1964de8267e0Stimh * zc_nvlist_dst_size size of property nvlist 1965de8267e0Stimh */ 1966fa9e4066Sahrens static int 1967fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1968fa9e4066Sahrens { 196987e5029aSahrens objset_t *os; 1970fa9e4066Sahrens int error; 1971fa9e4066Sahrens char *p; 1972620252bcSChris Kirby size_t orig_len = strlen(zc->zc_name); 1973fa9e4066Sahrens 1974620252bcSChris Kirby top: 1975503ad85cSMatthew Ahrens if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 197687e5029aSahrens if (error == ENOENT) 197787e5029aSahrens error = ESRCH; 197887e5029aSahrens return (error); 1979fa9e4066Sahrens } 1980fa9e4066Sahrens 1981fa9e4066Sahrens p = strrchr(zc->zc_name, '/'); 1982fa9e4066Sahrens if (p == NULL || p[1] != '\0') 1983fa9e4066Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1984fa9e4066Sahrens p = zc->zc_name + strlen(zc->zc_name); 1985fa9e4066Sahrens 19865c0b6a79SRich Morris /* 19875c0b6a79SRich Morris * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 19885c0b6a79SRich Morris * but is not declared void because its called by dmu_objset_find(). 19895c0b6a79SRich Morris */ 19907f73c863SRich Morris if (zc->zc_cookie == 0) { 19917f73c863SRich Morris uint64_t cookie = 0; 19927f73c863SRich Morris int len = sizeof (zc->zc_name) - (p - zc->zc_name); 19937f73c863SRich Morris 19941df56adaSMartin Matuska while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) { 19951df56adaSMartin Matuska if (!dataset_name_hidden(zc->zc_name)) 19961df56adaSMartin Matuska (void) dmu_objset_prefetch(zc->zc_name, NULL); 19971df56adaSMartin Matuska } 19987f73c863SRich Morris } 19997f73c863SRich Morris 2000fa9e4066Sahrens do { 200187e5029aSahrens error = dmu_dir_list_next(os, 200287e5029aSahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 200387e5029aSahrens NULL, &zc->zc_cookie); 2004fa9e4066Sahrens if (error == ENOENT) 2005fa9e4066Sahrens error = ESRCH; 200619b94df9SMatthew Ahrens } while (error == 0 && dataset_name_hidden(zc->zc_name)); 2007503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2008fa9e4066Sahrens 2009681d9761SEric Taylor /* 2010681d9761SEric Taylor * If it's an internal dataset (ie. with a '$' in its name), 2011681d9761SEric Taylor * don't try to get stats for it, otherwise we'll return ENOENT. 2012681d9761SEric Taylor */ 2013620252bcSChris Kirby if (error == 0 && strchr(zc->zc_name, '$') == NULL) { 201487e5029aSahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 2015620252bcSChris Kirby if (error == ENOENT) { 2016620252bcSChris Kirby /* We lost a race with destroy, get the next one. */ 2017620252bcSChris Kirby zc->zc_name[orig_len] = '\0'; 2018620252bcSChris Kirby goto top; 2019620252bcSChris Kirby } 2020620252bcSChris Kirby } 2021fa9e4066Sahrens return (error); 2022fa9e4066Sahrens } 2023fa9e4066Sahrens 20243cb34c60Sahrens /* 20253cb34c60Sahrens * inputs: 20263cb34c60Sahrens * zc_name name of filesystem 20273cb34c60Sahrens * zc_cookie zap cursor 20283cb34c60Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 20293cb34c60Sahrens * 20303cb34c60Sahrens * outputs: 20313cb34c60Sahrens * zc_name name of next snapshot 20323cb34c60Sahrens * zc_objset_stats stats 20333cb34c60Sahrens * zc_nvlist_dst property nvlist 20343cb34c60Sahrens * zc_nvlist_dst_size size of property nvlist 20353cb34c60Sahrens */ 2036fa9e4066Sahrens static int 2037fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 2038fa9e4066Sahrens { 203987e5029aSahrens objset_t *os; 2040fa9e4066Sahrens int error; 2041fa9e4066Sahrens 2042620252bcSChris Kirby top: 20437cbf8b43SRich Morris if (zc->zc_cookie == 0) 20447cbf8b43SRich Morris (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 20457cbf8b43SRich Morris NULL, DS_FIND_SNAPSHOTS); 20467cbf8b43SRich Morris 2047503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os); 2048745cd3c5Smaybee if (error) 2049745cd3c5Smaybee return (error == ENOENT ? ESRCH : error); 2050fa9e4066Sahrens 2051b81d61a6Slling /* 2052b81d61a6Slling * A dataset name of maximum length cannot have any snapshots, 2053b81d61a6Slling * so exit immediately. 2054b81d61a6Slling */ 2055b81d61a6Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 2056503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2057b81d61a6Slling return (ESRCH); 2058fa9e4066Sahrens } 2059fa9e4066Sahrens 206087e5029aSahrens error = dmu_snapshot_list_next(os, 206187e5029aSahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 2062a7f53a56SChris Kirby zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie, 2063a7f53a56SChris Kirby NULL); 2064a7f53a56SChris Kirby 2065620252bcSChris Kirby if (error == 0) { 2066a7f53a56SChris Kirby dsl_dataset_t *ds; 2067a7f53a56SChris Kirby dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool; 2068a7f53a56SChris Kirby 2069a7f53a56SChris Kirby /* 2070a7f53a56SChris Kirby * Since we probably don't have a hold on this snapshot, 2071a7f53a56SChris Kirby * it's possible that the objsetid could have been destroyed 2072a7f53a56SChris Kirby * and reused for a new objset. It's OK if this happens during 2073a7f53a56SChris Kirby * a zfs send operation, since the new createtxg will be 2074a7f53a56SChris Kirby * beyond the range we're interested in. 2075a7f53a56SChris Kirby */ 2076a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 2077a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds); 2078a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 2079a7f53a56SChris Kirby if (error) { 2080a7f53a56SChris Kirby if (error == ENOENT) { 2081a7f53a56SChris Kirby /* Racing with destroy, get the next one. */ 2082a7f53a56SChris Kirby *strchr(zc->zc_name, '@') = '\0'; 2083a7f53a56SChris Kirby dmu_objset_rele(os, FTAG); 2084a7f53a56SChris Kirby goto top; 2085a7f53a56SChris Kirby } 2086a7f53a56SChris Kirby } else { 2087a7f53a56SChris Kirby objset_t *ossnap; 2088a7f53a56SChris Kirby 2089a7f53a56SChris Kirby error = dmu_objset_from_ds(ds, &ossnap); 2090a7f53a56SChris Kirby if (error == 0) 2091a7f53a56SChris Kirby error = zfs_ioc_objset_stats_impl(zc, ossnap); 2092a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 2093620252bcSChris Kirby } 2094620252bcSChris Kirby } else if (error == ENOENT) { 2095745cd3c5Smaybee error = ESRCH; 2096620252bcSChris Kirby } 2097fa9e4066Sahrens 2098a7f53a56SChris Kirby dmu_objset_rele(os, FTAG); 20993cb34c60Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 2100745cd3c5Smaybee if (error) 21013cb34c60Sahrens *strchr(zc->zc_name, '@') = '\0'; 2102fa9e4066Sahrens return (error); 2103fa9e4066Sahrens } 2104fa9e4066Sahrens 210592241e0bSTom Erickson static int 210692241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 2107fa9e4066Sahrens { 210892241e0bSTom Erickson const char *propname = nvpair_name(pair); 210992241e0bSTom Erickson uint64_t *valary; 211092241e0bSTom Erickson unsigned int vallen; 211192241e0bSTom Erickson const char *domain; 2112eeb85002STim Haley char *dash; 211392241e0bSTom Erickson zfs_userquota_prop_t type; 211492241e0bSTom Erickson uint64_t rid; 211592241e0bSTom Erickson uint64_t quota; 211692241e0bSTom Erickson zfsvfs_t *zfsvfs; 211792241e0bSTom Erickson int err; 211892241e0bSTom Erickson 211992241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 212092241e0bSTom Erickson nvlist_t *attrs; 212192241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2122eeb85002STim Haley if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2123eeb85002STim Haley &pair) != 0) 2124eeb85002STim Haley return (EINVAL); 212592241e0bSTom Erickson } 2126e9dbad6fSeschrock 2127ecd6cf80Smarks /* 2128eeb85002STim Haley * A correctly constructed propname is encoded as 212992241e0bSTom Erickson * userquota@<rid>-<domain>. 2130ecd6cf80Smarks */ 2131eeb85002STim Haley if ((dash = strchr(propname, '-')) == NULL || 2132eeb85002STim Haley nvpair_value_uint64_array(pair, &valary, &vallen) != 0 || 2133eeb85002STim Haley vallen != 3) 2134eeb85002STim Haley return (EINVAL); 2135eeb85002STim Haley 2136eeb85002STim Haley domain = dash + 1; 2137eeb85002STim Haley type = valary[0]; 2138eeb85002STim Haley rid = valary[1]; 2139eeb85002STim Haley quota = valary[2]; 2140e9dbad6fSeschrock 21411412a1a2SMark Shellenbaum err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE); 214292241e0bSTom Erickson if (err == 0) { 214392241e0bSTom Erickson err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 214492241e0bSTom Erickson zfsvfs_rele(zfsvfs, FTAG); 214592241e0bSTom Erickson } 2146e9dbad6fSeschrock 214792241e0bSTom Erickson return (err); 214892241e0bSTom Erickson } 214914843421SMatthew Ahrens 215092241e0bSTom Erickson /* 215192241e0bSTom Erickson * If the named property is one that has a special function to set its value, 215292241e0bSTom Erickson * return 0 on success and a positive error code on failure; otherwise if it is 215392241e0bSTom Erickson * not one of the special properties handled by this function, return -1. 215492241e0bSTom Erickson * 2155eeb85002STim Haley * XXX: It would be better for callers of the property interface if we handled 215692241e0bSTom Erickson * these special cases in dsl_prop.c (in the dsl layer). 215792241e0bSTom Erickson */ 215892241e0bSTom Erickson static int 215992241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source, 216092241e0bSTom Erickson nvpair_t *pair) 216192241e0bSTom Erickson { 216292241e0bSTom Erickson const char *propname = nvpair_name(pair); 216392241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 216492241e0bSTom Erickson uint64_t intval; 216592241e0bSTom Erickson int err; 2166fa9e4066Sahrens 216792241e0bSTom Erickson if (prop == ZPROP_INVAL) { 216892241e0bSTom Erickson if (zfs_prop_userquota(propname)) 216992241e0bSTom Erickson return (zfs_prop_set_userquota(dsname, pair)); 217092241e0bSTom Erickson return (-1); 217192241e0bSTom Erickson } 217214843421SMatthew Ahrens 217392241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 217492241e0bSTom Erickson nvlist_t *attrs; 217592241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 217692241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 217792241e0bSTom Erickson &pair) == 0); 217892241e0bSTom Erickson } 2179db870a07Sahrens 218092241e0bSTom Erickson if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) 218192241e0bSTom Erickson return (-1); 2182b24ab676SJeff Bonwick 218392241e0bSTom Erickson VERIFY(0 == nvpair_value_uint64(pair, &intval)); 218440feaa91Sahrens 218592241e0bSTom Erickson switch (prop) { 218692241e0bSTom Erickson case ZFS_PROP_QUOTA: 218792241e0bSTom Erickson err = dsl_dir_set_quota(dsname, source, intval); 218892241e0bSTom Erickson break; 218992241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 219092241e0bSTom Erickson err = dsl_dataset_set_quota(dsname, source, intval); 219192241e0bSTom Erickson break; 219292241e0bSTom Erickson case ZFS_PROP_RESERVATION: 219392241e0bSTom Erickson err = dsl_dir_set_reservation(dsname, source, intval); 219492241e0bSTom Erickson break; 219592241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 219692241e0bSTom Erickson err = dsl_dataset_set_reservation(dsname, source, intval); 219792241e0bSTom Erickson break; 219892241e0bSTom Erickson case ZFS_PROP_VOLSIZE: 219992241e0bSTom Erickson err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip), 220092241e0bSTom Erickson intval); 220192241e0bSTom Erickson break; 220292241e0bSTom Erickson case ZFS_PROP_VERSION: 220392241e0bSTom Erickson { 220492241e0bSTom Erickson zfsvfs_t *zfsvfs; 22059e6eda55Smarks 22061412a1a2SMark Shellenbaum if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0) 2207b24ab676SJeff Bonwick break; 2208b24ab676SJeff Bonwick 220992241e0bSTom Erickson err = zfs_set_version(zfsvfs, intval); 221092241e0bSTom Erickson zfsvfs_rele(zfsvfs, FTAG); 2211d0f3f37eSMark Shellenbaum 221292241e0bSTom Erickson if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 2213b16da2e2SGeorge Wilson zfs_cmd_t *zc; 2214b16da2e2SGeorge Wilson 2215b16da2e2SGeorge Wilson zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2216b16da2e2SGeorge Wilson (void) strcpy(zc->zc_name, dsname); 2217b16da2e2SGeorge Wilson (void) zfs_ioc_userspace_upgrade(zc); 2218b16da2e2SGeorge Wilson kmem_free(zc, sizeof (zfs_cmd_t)); 221940feaa91Sahrens } 222092241e0bSTom Erickson break; 2221ecd6cf80Smarks } 2222ecd6cf80Smarks 222392241e0bSTom Erickson default: 222492241e0bSTom Erickson err = -1; 222592241e0bSTom Erickson } 2226e9dbad6fSeschrock 222792241e0bSTom Erickson return (err); 222892241e0bSTom Erickson } 2229a9799022Sck 223092241e0bSTom Erickson /* 223192241e0bSTom Erickson * This function is best effort. If it fails to set any of the given properties, 223292241e0bSTom Erickson * it continues to set as many as it can and returns the first error 223392241e0bSTom Erickson * encountered. If the caller provides a non-NULL errlist, it also gives the 223492241e0bSTom Erickson * complete list of names of all the properties it failed to set along with the 223592241e0bSTom Erickson * corresponding error numbers. The caller is responsible for freeing the 223692241e0bSTom Erickson * returned errlist. 223792241e0bSTom Erickson * 223892241e0bSTom Erickson * If every property is set successfully, zero is returned and the list pointed 223992241e0bSTom Erickson * at by errlist is NULL. 224092241e0bSTom Erickson */ 224192241e0bSTom Erickson int 224292241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 224392241e0bSTom Erickson nvlist_t **errlist) 224492241e0bSTom Erickson { 224592241e0bSTom Erickson nvpair_t *pair; 224692241e0bSTom Erickson nvpair_t *propval; 224702e383d1STom Erickson int rv = 0; 224892241e0bSTom Erickson uint64_t intval; 224992241e0bSTom Erickson char *strval; 225092241e0bSTom Erickson nvlist_t *genericnvl; 225192241e0bSTom Erickson nvlist_t *errors; 225292241e0bSTom Erickson nvlist_t *retrynvl; 2253e9dbad6fSeschrock 225492241e0bSTom Erickson VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 225592241e0bSTom Erickson VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 225692241e0bSTom Erickson VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2257a9799022Sck 225892241e0bSTom Erickson retry: 225992241e0bSTom Erickson pair = NULL; 226092241e0bSTom Erickson while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 226192241e0bSTom Erickson const char *propname = nvpair_name(pair); 226292241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 2263cfa69fd2STom Erickson int err = 0; 2264e9dbad6fSeschrock 226592241e0bSTom Erickson /* decode the property value */ 226692241e0bSTom Erickson propval = pair; 226792241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 226892241e0bSTom Erickson nvlist_t *attrs; 226992241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2270eeb85002STim Haley if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2271eeb85002STim Haley &propval) != 0) 2272eeb85002STim Haley err = EINVAL; 227314843421SMatthew Ahrens } 2274e9dbad6fSeschrock 227592241e0bSTom Erickson /* Validate value type */ 2276eeb85002STim Haley if (err == 0 && prop == ZPROP_INVAL) { 227792241e0bSTom Erickson if (zfs_prop_user(propname)) { 227892241e0bSTom Erickson if (nvpair_type(propval) != DATA_TYPE_STRING) 227992241e0bSTom Erickson err = EINVAL; 228092241e0bSTom Erickson } else if (zfs_prop_userquota(propname)) { 228192241e0bSTom Erickson if (nvpair_type(propval) != 228292241e0bSTom Erickson DATA_TYPE_UINT64_ARRAY) 228392241e0bSTom Erickson err = EINVAL; 228419b94df9SMatthew Ahrens } else { 228519b94df9SMatthew Ahrens err = EINVAL; 22864201a95eSRic Aleshire } 2287eeb85002STim Haley } else if (err == 0) { 228892241e0bSTom Erickson if (nvpair_type(propval) == DATA_TYPE_STRING) { 228992241e0bSTom Erickson if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 229092241e0bSTom Erickson err = EINVAL; 229192241e0bSTom Erickson } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 2292a2eea2e1Sahrens const char *unused; 2293a2eea2e1Sahrens 229492241e0bSTom Erickson VERIFY(nvpair_value_uint64(propval, 229592241e0bSTom Erickson &intval) == 0); 2296e9dbad6fSeschrock 2297e9dbad6fSeschrock switch (zfs_prop_get_type(prop)) { 229891ebeef5Sahrens case PROP_TYPE_NUMBER: 2299e9dbad6fSeschrock break; 230091ebeef5Sahrens case PROP_TYPE_STRING: 230192241e0bSTom Erickson err = EINVAL; 230292241e0bSTom Erickson break; 230391ebeef5Sahrens case PROP_TYPE_INDEX: 2304acd76fe5Seschrock if (zfs_prop_index_to_string(prop, 230592241e0bSTom Erickson intval, &unused) != 0) 230692241e0bSTom Erickson err = EINVAL; 2307e9dbad6fSeschrock break; 2308e9dbad6fSeschrock default: 2309e7437265Sahrens cmn_err(CE_PANIC, 2310e7437265Sahrens "unknown property type"); 2311e9dbad6fSeschrock } 2312e9dbad6fSeschrock } else { 231392241e0bSTom Erickson err = EINVAL; 2314e9dbad6fSeschrock } 2315e9dbad6fSeschrock } 231692241e0bSTom Erickson 231792241e0bSTom Erickson /* Validate permissions */ 231892241e0bSTom Erickson if (err == 0) 231992241e0bSTom Erickson err = zfs_check_settable(dsname, pair, CRED()); 232092241e0bSTom Erickson 232192241e0bSTom Erickson if (err == 0) { 232292241e0bSTom Erickson err = zfs_prop_set_special(dsname, source, pair); 232392241e0bSTom Erickson if (err == -1) { 232492241e0bSTom Erickson /* 232592241e0bSTom Erickson * For better performance we build up a list of 232692241e0bSTom Erickson * properties to set in a single transaction. 232792241e0bSTom Erickson */ 232892241e0bSTom Erickson err = nvlist_add_nvpair(genericnvl, pair); 232992241e0bSTom Erickson } else if (err != 0 && nvl != retrynvl) { 233092241e0bSTom Erickson /* 233192241e0bSTom Erickson * This may be a spurious error caused by 233292241e0bSTom Erickson * receiving quota and reservation out of order. 233392241e0bSTom Erickson * Try again in a second pass. 233492241e0bSTom Erickson */ 233592241e0bSTom Erickson err = nvlist_add_nvpair(retrynvl, pair); 233692241e0bSTom Erickson } 233792241e0bSTom Erickson } 233892241e0bSTom Erickson 233992241e0bSTom Erickson if (err != 0) 234092241e0bSTom Erickson VERIFY(nvlist_add_int32(errors, propname, err) == 0); 2341e9dbad6fSeschrock } 2342e9dbad6fSeschrock 234392241e0bSTom Erickson if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 234492241e0bSTom Erickson nvl = retrynvl; 234592241e0bSTom Erickson goto retry; 234692241e0bSTom Erickson } 234792241e0bSTom Erickson 234892241e0bSTom Erickson if (!nvlist_empty(genericnvl) && 234992241e0bSTom Erickson dsl_props_set(dsname, source, genericnvl) != 0) { 235092241e0bSTom Erickson /* 235192241e0bSTom Erickson * If this fails, we still want to set as many properties as we 235292241e0bSTom Erickson * can, so try setting them individually. 235392241e0bSTom Erickson */ 235492241e0bSTom Erickson pair = NULL; 235592241e0bSTom Erickson while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 235692241e0bSTom Erickson const char *propname = nvpair_name(pair); 2357cfa69fd2STom Erickson int err = 0; 235892241e0bSTom Erickson 235992241e0bSTom Erickson propval = pair; 236092241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 236192241e0bSTom Erickson nvlist_t *attrs; 236292241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 236392241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 236492241e0bSTom Erickson &propval) == 0); 236592241e0bSTom Erickson } 236692241e0bSTom Erickson 236792241e0bSTom Erickson if (nvpair_type(propval) == DATA_TYPE_STRING) { 236892241e0bSTom Erickson VERIFY(nvpair_value_string(propval, 236992241e0bSTom Erickson &strval) == 0); 237092241e0bSTom Erickson err = dsl_prop_set(dsname, propname, source, 1, 237192241e0bSTom Erickson strlen(strval) + 1, strval); 237292241e0bSTom Erickson } else { 237392241e0bSTom Erickson VERIFY(nvpair_value_uint64(propval, 237492241e0bSTom Erickson &intval) == 0); 237592241e0bSTom Erickson err = dsl_prop_set(dsname, propname, source, 8, 237692241e0bSTom Erickson 1, &intval); 237792241e0bSTom Erickson } 237892241e0bSTom Erickson 237992241e0bSTom Erickson if (err != 0) { 238092241e0bSTom Erickson VERIFY(nvlist_add_int32(errors, propname, 238192241e0bSTom Erickson err) == 0); 238292241e0bSTom Erickson } 238392241e0bSTom Erickson } 23845c0b6a79SRich Morris } 23855c0b6a79SRich Morris nvlist_free(genericnvl); 238692241e0bSTom Erickson nvlist_free(retrynvl); 238792241e0bSTom Erickson 238892241e0bSTom Erickson if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 238992241e0bSTom Erickson nvlist_free(errors); 239092241e0bSTom Erickson errors = NULL; 239192241e0bSTom Erickson } else { 239292241e0bSTom Erickson VERIFY(nvpair_value_int32(pair, &rv) == 0); 239392241e0bSTom Erickson } 239492241e0bSTom Erickson 239592241e0bSTom Erickson if (errlist == NULL) 239692241e0bSTom Erickson nvlist_free(errors); 239792241e0bSTom Erickson else 239892241e0bSTom Erickson *errlist = errors; 239992241e0bSTom Erickson 240092241e0bSTom Erickson return (rv); 2401fa9e4066Sahrens } 2402fa9e4066Sahrens 2403ea2f5b9eSMatthew Ahrens /* 2404ea2f5b9eSMatthew Ahrens * Check that all the properties are valid user properties. 2405ea2f5b9eSMatthew Ahrens */ 2406ea2f5b9eSMatthew Ahrens static int 2407ea2f5b9eSMatthew Ahrens zfs_check_userprops(char *fsname, nvlist_t *nvl) 2408ea2f5b9eSMatthew Ahrens { 240992241e0bSTom Erickson nvpair_t *pair = NULL; 2410ea2f5b9eSMatthew Ahrens int error = 0; 2411ea2f5b9eSMatthew Ahrens 241292241e0bSTom Erickson while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 241392241e0bSTom Erickson const char *propname = nvpair_name(pair); 2414ea2f5b9eSMatthew Ahrens char *valstr; 2415ea2f5b9eSMatthew Ahrens 2416ea2f5b9eSMatthew Ahrens if (!zfs_prop_user(propname) || 241792241e0bSTom Erickson nvpair_type(pair) != DATA_TYPE_STRING) 2418ea2f5b9eSMatthew Ahrens return (EINVAL); 2419ea2f5b9eSMatthew Ahrens 2420ea2f5b9eSMatthew Ahrens if (error = zfs_secpolicy_write_perms(fsname, 2421ea2f5b9eSMatthew Ahrens ZFS_DELEG_PERM_USERPROP, CRED())) 2422ea2f5b9eSMatthew Ahrens return (error); 2423ea2f5b9eSMatthew Ahrens 2424ea2f5b9eSMatthew Ahrens if (strlen(propname) >= ZAP_MAXNAMELEN) 2425ea2f5b9eSMatthew Ahrens return (ENAMETOOLONG); 2426ea2f5b9eSMatthew Ahrens 242792241e0bSTom Erickson VERIFY(nvpair_value_string(pair, &valstr) == 0); 2428ea2f5b9eSMatthew Ahrens if (strlen(valstr) >= ZAP_MAXVALUELEN) 2429ea2f5b9eSMatthew Ahrens return (E2BIG); 2430ea2f5b9eSMatthew Ahrens } 2431ea2f5b9eSMatthew Ahrens return (0); 2432ea2f5b9eSMatthew Ahrens } 2433ea2f5b9eSMatthew Ahrens 243492241e0bSTom Erickson static void 243592241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 243692241e0bSTom Erickson { 243792241e0bSTom Erickson nvpair_t *pair; 243892241e0bSTom Erickson 243992241e0bSTom Erickson VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 244092241e0bSTom Erickson 244192241e0bSTom Erickson pair = NULL; 244292241e0bSTom Erickson while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 244392241e0bSTom Erickson if (nvlist_exists(skipped, nvpair_name(pair))) 244492241e0bSTom Erickson continue; 244592241e0bSTom Erickson 244692241e0bSTom Erickson VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 244792241e0bSTom Erickson } 244892241e0bSTom Erickson } 244992241e0bSTom Erickson 245092241e0bSTom Erickson static int 245192241e0bSTom Erickson clear_received_props(objset_t *os, const char *fs, nvlist_t *props, 245292241e0bSTom Erickson nvlist_t *skipped) 245392241e0bSTom Erickson { 245492241e0bSTom Erickson int err = 0; 245592241e0bSTom Erickson nvlist_t *cleared_props = NULL; 245692241e0bSTom Erickson props_skip(props, skipped, &cleared_props); 245792241e0bSTom Erickson if (!nvlist_empty(cleared_props)) { 245892241e0bSTom Erickson /* 245992241e0bSTom Erickson * Acts on local properties until the dataset has received 246092241e0bSTom Erickson * properties at least once on or after SPA_VERSION_RECVD_PROPS. 246192241e0bSTom Erickson */ 246292241e0bSTom Erickson zprop_source_t flags = (ZPROP_SRC_NONE | 246392241e0bSTom Erickson (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0)); 246492241e0bSTom Erickson err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL); 246592241e0bSTom Erickson } 246692241e0bSTom Erickson nvlist_free(cleared_props); 246792241e0bSTom Erickson return (err); 246892241e0bSTom Erickson } 246992241e0bSTom Erickson 24703cb34c60Sahrens /* 24713cb34c60Sahrens * inputs: 24723cb34c60Sahrens * zc_name name of filesystem 24735c0b6a79SRich Morris * zc_value name of property to set 24743cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 247592241e0bSTom Erickson * zc_cookie received properties flag 24763cb34c60Sahrens * 247792241e0bSTom Erickson * outputs: 247892241e0bSTom Erickson * zc_nvlist_dst{_size} error for each unapplied received property 24793cb34c60Sahrens */ 2480fa9e4066Sahrens static int 2481e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 2482fa9e4066Sahrens { 2483e9dbad6fSeschrock nvlist_t *nvl; 248492241e0bSTom Erickson boolean_t received = zc->zc_cookie; 248592241e0bSTom Erickson zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 248692241e0bSTom Erickson ZPROP_SRC_LOCAL); 248792241e0bSTom Erickson nvlist_t *errors = NULL; 2488e9dbad6fSeschrock int error; 2489e9dbad6fSeschrock 2490990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2491478ed9adSEric Taylor zc->zc_iflags, &nvl)) != 0) 2492e9dbad6fSeschrock return (error); 2493e9dbad6fSeschrock 249492241e0bSTom Erickson if (received) { 2495bb0ade09Sahrens nvlist_t *origprops; 2496bb0ade09Sahrens objset_t *os; 2497bb0ade09Sahrens 2498503ad85cSMatthew Ahrens if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) { 249992241e0bSTom Erickson if (dsl_prop_get_received(os, &origprops) == 0) { 250092241e0bSTom Erickson (void) clear_received_props(os, 250192241e0bSTom Erickson zc->zc_name, origprops, nvl); 2502bb0ade09Sahrens nvlist_free(origprops); 2503bb0ade09Sahrens } 250492241e0bSTom Erickson 250592241e0bSTom Erickson dsl_prop_set_hasrecvd(os); 2506503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2507bb0ade09Sahrens } 2508bb0ade09Sahrens } 2509bb0ade09Sahrens 251092241e0bSTom Erickson error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors); 251192241e0bSTom Erickson 251292241e0bSTom Erickson if (zc->zc_nvlist_dst != NULL && errors != NULL) { 251392241e0bSTom Erickson (void) put_nvlist(zc, errors); 251492241e0bSTom Erickson } 2515ecd6cf80Smarks 251692241e0bSTom Erickson nvlist_free(errors); 2517e9dbad6fSeschrock nvlist_free(nvl); 2518e9dbad6fSeschrock return (error); 2519fa9e4066Sahrens } 2520fa9e4066Sahrens 25213cb34c60Sahrens /* 25223cb34c60Sahrens * inputs: 25233cb34c60Sahrens * zc_name name of filesystem 25243cb34c60Sahrens * zc_value name of property to inherit 252592241e0bSTom Erickson * zc_cookie revert to received value if TRUE 25263cb34c60Sahrens * 25273cb34c60Sahrens * outputs: none 25283cb34c60Sahrens */ 2529e45ce728Sahrens static int 2530e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 2531e45ce728Sahrens { 253292241e0bSTom Erickson const char *propname = zc->zc_value; 253392241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 253492241e0bSTom Erickson boolean_t received = zc->zc_cookie; 253592241e0bSTom Erickson zprop_source_t source = (received 253692241e0bSTom Erickson ? ZPROP_SRC_NONE /* revert to received value, if any */ 253792241e0bSTom Erickson : ZPROP_SRC_INHERITED); /* explicitly inherit */ 253892241e0bSTom Erickson 253992241e0bSTom Erickson if (received) { 254092241e0bSTom Erickson nvlist_t *dummy; 254192241e0bSTom Erickson nvpair_t *pair; 254292241e0bSTom Erickson zprop_type_t type; 254392241e0bSTom Erickson int err; 254492241e0bSTom Erickson 254592241e0bSTom Erickson /* 254692241e0bSTom Erickson * zfs_prop_set_special() expects properties in the form of an 254792241e0bSTom Erickson * nvpair with type info. 254892241e0bSTom Erickson */ 254992241e0bSTom Erickson if (prop == ZPROP_INVAL) { 255092241e0bSTom Erickson if (!zfs_prop_user(propname)) 255192241e0bSTom Erickson return (EINVAL); 255292241e0bSTom Erickson 255392241e0bSTom Erickson type = PROP_TYPE_STRING; 2554a79992aaSTom Erickson } else if (prop == ZFS_PROP_VOLSIZE || 2555a79992aaSTom Erickson prop == ZFS_PROP_VERSION) { 2556a79992aaSTom Erickson return (EINVAL); 255792241e0bSTom Erickson } else { 255892241e0bSTom Erickson type = zfs_prop_get_type(prop); 255992241e0bSTom Erickson } 256092241e0bSTom Erickson 256192241e0bSTom Erickson VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0); 256292241e0bSTom Erickson 256392241e0bSTom Erickson switch (type) { 256492241e0bSTom Erickson case PROP_TYPE_STRING: 256592241e0bSTom Erickson VERIFY(0 == nvlist_add_string(dummy, propname, "")); 256692241e0bSTom Erickson break; 256792241e0bSTom Erickson case PROP_TYPE_NUMBER: 256892241e0bSTom Erickson case PROP_TYPE_INDEX: 256992241e0bSTom Erickson VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 257092241e0bSTom Erickson break; 257192241e0bSTom Erickson default: 257292241e0bSTom Erickson nvlist_free(dummy); 257392241e0bSTom Erickson return (EINVAL); 257492241e0bSTom Erickson } 257592241e0bSTom Erickson 257692241e0bSTom Erickson pair = nvlist_next_nvpair(dummy, NULL); 257792241e0bSTom Erickson err = zfs_prop_set_special(zc->zc_name, source, pair); 257892241e0bSTom Erickson nvlist_free(dummy); 257992241e0bSTom Erickson if (err != -1) 258092241e0bSTom Erickson return (err); /* special property already handled */ 258192241e0bSTom Erickson } else { 258292241e0bSTom Erickson /* 258392241e0bSTom Erickson * Only check this in the non-received case. We want to allow 258492241e0bSTom Erickson * 'inherit -S' to revert non-inheritable properties like quota 258592241e0bSTom Erickson * and reservation to the received or default values even though 258692241e0bSTom Erickson * they are not considered inheritable. 258792241e0bSTom Erickson */ 258892241e0bSTom Erickson if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 258992241e0bSTom Erickson return (EINVAL); 259092241e0bSTom Erickson } 259192241e0bSTom Erickson 2592e45ce728Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 259392241e0bSTom Erickson return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL)); 2594e45ce728Sahrens } 2595e45ce728Sahrens 2596b1b8ab34Slling static int 259711a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 2598b1b8ab34Slling { 2599990b4856Slling nvlist_t *props; 2600b1b8ab34Slling spa_t *spa; 2601990b4856Slling int error; 260292241e0bSTom Erickson nvpair_t *pair; 2603b1b8ab34Slling 260492241e0bSTom Erickson if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 260592241e0bSTom Erickson zc->zc_iflags, &props)) 2606b1b8ab34Slling return (error); 2607b1b8ab34Slling 2608379c004dSEric Schrock /* 2609379c004dSEric Schrock * If the only property is the configfile, then just do a spa_lookup() 2610379c004dSEric Schrock * to handle the faulted case. 2611379c004dSEric Schrock */ 261292241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL); 261392241e0bSTom Erickson if (pair != NULL && strcmp(nvpair_name(pair), 2614379c004dSEric Schrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 261592241e0bSTom Erickson nvlist_next_nvpair(props, pair) == NULL) { 2616379c004dSEric Schrock mutex_enter(&spa_namespace_lock); 2617379c004dSEric Schrock if ((spa = spa_lookup(zc->zc_name)) != NULL) { 2618379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 2619379c004dSEric Schrock spa_config_sync(spa, B_FALSE, B_TRUE); 2620379c004dSEric Schrock } 2621379c004dSEric Schrock mutex_exit(&spa_namespace_lock); 2622b693757aSEric Schrock if (spa != NULL) { 2623b693757aSEric Schrock nvlist_free(props); 2624379c004dSEric Schrock return (0); 2625b693757aSEric Schrock } 2626379c004dSEric Schrock } 2627379c004dSEric Schrock 2628b1b8ab34Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2629990b4856Slling nvlist_free(props); 2630b1b8ab34Slling return (error); 2631b1b8ab34Slling } 2632b1b8ab34Slling 2633990b4856Slling error = spa_prop_set(spa, props); 2634b1b8ab34Slling 2635990b4856Slling nvlist_free(props); 2636b1b8ab34Slling spa_close(spa, FTAG); 2637b1b8ab34Slling 2638b1b8ab34Slling return (error); 2639b1b8ab34Slling } 2640b1b8ab34Slling 2641b1b8ab34Slling static int 264211a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 2643b1b8ab34Slling { 2644b1b8ab34Slling spa_t *spa; 2645b1b8ab34Slling int error; 2646b1b8ab34Slling nvlist_t *nvp = NULL; 2647b1b8ab34Slling 2648379c004dSEric Schrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2649379c004dSEric Schrock /* 2650379c004dSEric Schrock * If the pool is faulted, there may be properties we can still 2651379c004dSEric Schrock * get (such as altroot and cachefile), so attempt to get them 2652379c004dSEric Schrock * anyway. 2653379c004dSEric Schrock */ 2654379c004dSEric Schrock mutex_enter(&spa_namespace_lock); 2655379c004dSEric Schrock if ((spa = spa_lookup(zc->zc_name)) != NULL) 2656379c004dSEric Schrock error = spa_prop_get(spa, &nvp); 2657379c004dSEric Schrock mutex_exit(&spa_namespace_lock); 2658379c004dSEric Schrock } else { 2659379c004dSEric Schrock error = spa_prop_get(spa, &nvp); 2660379c004dSEric Schrock spa_close(spa, FTAG); 2661379c004dSEric Schrock } 2662b1b8ab34Slling 2663b1b8ab34Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 2664b1b8ab34Slling error = put_nvlist(zc, nvp); 2665b1b8ab34Slling else 2666b1b8ab34Slling error = EFAULT; 2667b1b8ab34Slling 2668379c004dSEric Schrock nvlist_free(nvp); 2669b1b8ab34Slling return (error); 2670b1b8ab34Slling } 2671b1b8ab34Slling 26723cb34c60Sahrens /* 26733cb34c60Sahrens * inputs: 26743cb34c60Sahrens * zc_name name of filesystem 26753cb34c60Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 26763cb34c60Sahrens * zc_perm_action allow/unallow flag 26773cb34c60Sahrens * 26783cb34c60Sahrens * outputs: none 26793cb34c60Sahrens */ 2680ecd6cf80Smarks static int 2681ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 2682ecd6cf80Smarks { 2683ecd6cf80Smarks int error; 2684ecd6cf80Smarks nvlist_t *fsaclnv = NULL; 2685ecd6cf80Smarks 2686990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2687478ed9adSEric Taylor zc->zc_iflags, &fsaclnv)) != 0) 2688ecd6cf80Smarks return (error); 2689ecd6cf80Smarks 2690ecd6cf80Smarks /* 2691ecd6cf80Smarks * Verify nvlist is constructed correctly 2692ecd6cf80Smarks */ 2693ecd6cf80Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 2694ecd6cf80Smarks nvlist_free(fsaclnv); 2695ecd6cf80Smarks return (EINVAL); 2696ecd6cf80Smarks } 2697ecd6cf80Smarks 2698ecd6cf80Smarks /* 2699ecd6cf80Smarks * If we don't have PRIV_SYS_MOUNT, then validate 2700ecd6cf80Smarks * that user is allowed to hand out each permission in 2701ecd6cf80Smarks * the nvlist(s) 2702ecd6cf80Smarks */ 2703ecd6cf80Smarks 270491ebeef5Sahrens error = secpolicy_zfs(CRED()); 2705ecd6cf80Smarks if (error) { 270691ebeef5Sahrens if (zc->zc_perm_action == B_FALSE) { 270791ebeef5Sahrens error = dsl_deleg_can_allow(zc->zc_name, 270891ebeef5Sahrens fsaclnv, CRED()); 270991ebeef5Sahrens } else { 271091ebeef5Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 271191ebeef5Sahrens fsaclnv, CRED()); 271291ebeef5Sahrens } 2713ecd6cf80Smarks } 2714ecd6cf80Smarks 2715ecd6cf80Smarks if (error == 0) 2716ecd6cf80Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 2717ecd6cf80Smarks 2718ecd6cf80Smarks nvlist_free(fsaclnv); 2719ecd6cf80Smarks return (error); 2720ecd6cf80Smarks } 2721ecd6cf80Smarks 27223cb34c60Sahrens /* 27233cb34c60Sahrens * inputs: 27243cb34c60Sahrens * zc_name name of filesystem 27253cb34c60Sahrens * 27263cb34c60Sahrens * outputs: 27273cb34c60Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 27283cb34c60Sahrens */ 2729ecd6cf80Smarks static int 2730ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 2731ecd6cf80Smarks { 2732ecd6cf80Smarks nvlist_t *nvp; 2733ecd6cf80Smarks int error; 2734ecd6cf80Smarks 2735ecd6cf80Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 2736ecd6cf80Smarks error = put_nvlist(zc, nvp); 2737ecd6cf80Smarks nvlist_free(nvp); 2738ecd6cf80Smarks } 2739ecd6cf80Smarks 2740ecd6cf80Smarks return (error); 2741ecd6cf80Smarks } 2742ecd6cf80Smarks 2743fa9e4066Sahrens /* 2744fa9e4066Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 2745fa9e4066Sahrens * or NULL if no suitable entry is found. The caller of this routine 2746fa9e4066Sahrens * is responsible for releasing the returned vfs pointer. 2747fa9e4066Sahrens */ 2748fa9e4066Sahrens static vfs_t * 2749fa9e4066Sahrens zfs_get_vfs(const char *resource) 2750fa9e4066Sahrens { 2751fa9e4066Sahrens struct vfs *vfsp; 2752fa9e4066Sahrens struct vfs *vfs_found = NULL; 2753fa9e4066Sahrens 2754fa9e4066Sahrens vfs_list_read_lock(); 2755fa9e4066Sahrens vfsp = rootvfs; 2756fa9e4066Sahrens do { 2757fa9e4066Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2758fa9e4066Sahrens VFS_HOLD(vfsp); 2759fa9e4066Sahrens vfs_found = vfsp; 2760fa9e4066Sahrens break; 2761fa9e4066Sahrens } 2762fa9e4066Sahrens vfsp = vfsp->vfs_next; 2763fa9e4066Sahrens } while (vfsp != rootvfs); 2764fa9e4066Sahrens vfs_list_unlock(); 2765fa9e4066Sahrens return (vfs_found); 2766fa9e4066Sahrens } 2767fa9e4066Sahrens 2768ecd6cf80Smarks /* ARGSUSED */ 2769fa9e4066Sahrens static void 2770ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2771fa9e4066Sahrens { 2772da6c28aaSamw zfs_creat_t *zct = arg; 2773da6c28aaSamw 2774de8267e0Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 2775da6c28aaSamw } 2776da6c28aaSamw 2777de8267e0Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 2778da6c28aaSamw 2779da6c28aaSamw /* 2780de8267e0Stimh * inputs: 27810a48a24eStimh * createprops list of properties requested by creator 27820a48a24eStimh * default_zplver zpl version to use if unspecified in createprops 27830a48a24eStimh * fuids_ok fuids allowed in this version of the spa? 27840a48a24eStimh * os parent objset pointer (NULL if root fs) 2785de8267e0Stimh * 2786de8267e0Stimh * outputs: 2787de8267e0Stimh * zplprops values for the zplprops we attach to the master node object 27880a48a24eStimh * is_ci true if requested file system will be purely case-insensitive 2789da6c28aaSamw * 2790de8267e0Stimh * Determine the settings for utf8only, normalization and 2791de8267e0Stimh * casesensitivity. Specific values may have been requested by the 2792de8267e0Stimh * creator and/or we can inherit values from the parent dataset. If 2793de8267e0Stimh * the file system is of too early a vintage, a creator can not 2794de8267e0Stimh * request settings for these properties, even if the requested 2795de8267e0Stimh * setting is the default value. We don't actually want to create dsl 2796de8267e0Stimh * properties for these, so remove them from the source nvlist after 2797de8267e0Stimh * processing. 2798da6c28aaSamw */ 2799da6c28aaSamw static int 280014843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 28010a586ceaSMark Shellenbaum boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops, 28020a586ceaSMark Shellenbaum nvlist_t *zplprops, boolean_t *is_ci) 2803da6c28aaSamw { 2804de8267e0Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 2805de8267e0Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 2806de8267e0Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 2807da6c28aaSamw 2808de8267e0Stimh ASSERT(zplprops != NULL); 2809da6c28aaSamw 2810de8267e0Stimh /* 2811de8267e0Stimh * Pull out creator prop choices, if any. 2812de8267e0Stimh */ 2813de8267e0Stimh if (createprops) { 28140a48a24eStimh (void) nvlist_lookup_uint64(createprops, 28150a48a24eStimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 2816de8267e0Stimh (void) nvlist_lookup_uint64(createprops, 2817de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 2818de8267e0Stimh (void) nvlist_remove_all(createprops, 2819de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 2820de8267e0Stimh (void) nvlist_lookup_uint64(createprops, 2821de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 2822de8267e0Stimh (void) nvlist_remove_all(createprops, 2823de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 2824de8267e0Stimh (void) nvlist_lookup_uint64(createprops, 2825de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 2826de8267e0Stimh (void) nvlist_remove_all(createprops, 2827de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 2828de8267e0Stimh } 2829da6c28aaSamw 2830c2a93d44Stimh /* 28310a48a24eStimh * If the zpl version requested is whacky or the file system 28320a48a24eStimh * or pool is version is too "young" to support normalization 28330a48a24eStimh * and the creator tried to set a value for one of the props, 28340a48a24eStimh * error out. 2835c2a93d44Stimh */ 28360a48a24eStimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 28370a48a24eStimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 28380a586ceaSMark Shellenbaum (zplver >= ZPL_VERSION_SA && !sa_ok) || 28390a48a24eStimh (zplver < ZPL_VERSION_NORMALIZATION && 2840de8267e0Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 28410a48a24eStimh sense != ZFS_PROP_UNDEFINED))) 2842de8267e0Stimh return (ENOTSUP); 2843c2a93d44Stimh 2844de8267e0Stimh /* 2845de8267e0Stimh * Put the version in the zplprops 2846de8267e0Stimh */ 2847de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 2848de8267e0Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 2849da6c28aaSamw 2850de8267e0Stimh if (norm == ZFS_PROP_UNDEFINED) 2851de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 2852de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 2853de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 2854da6c28aaSamw 2855c2a93d44Stimh /* 2856de8267e0Stimh * If we're normalizing, names must always be valid UTF-8 strings. 2857c2a93d44Stimh */ 2858de8267e0Stimh if (norm) 2859de8267e0Stimh u8 = 1; 2860de8267e0Stimh if (u8 == ZFS_PROP_UNDEFINED) 2861de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 2862de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 2863de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 2864de8267e0Stimh 2865de8267e0Stimh if (sense == ZFS_PROP_UNDEFINED) 2866de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 2867de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 2868de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 2869c2a93d44Stimh 2870ab04eb8eStimh if (is_ci) 2871ab04eb8eStimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 2872ab04eb8eStimh 2873da6c28aaSamw return (0); 2874fa9e4066Sahrens } 2875fa9e4066Sahrens 28760a48a24eStimh static int 28770a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 28780a48a24eStimh nvlist_t *zplprops, boolean_t *is_ci) 28790a48a24eStimh { 28800a586ceaSMark Shellenbaum boolean_t fuids_ok, sa_ok; 28810a48a24eStimh uint64_t zplver = ZPL_VERSION; 28820a48a24eStimh objset_t *os = NULL; 28830a48a24eStimh char parentname[MAXNAMELEN]; 28840a48a24eStimh char *cp; 28850a586ceaSMark Shellenbaum spa_t *spa; 28860a586ceaSMark Shellenbaum uint64_t spa_vers; 28870a48a24eStimh int error; 28880a48a24eStimh 28890a48a24eStimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 28900a48a24eStimh cp = strrchr(parentname, '/'); 28910a48a24eStimh ASSERT(cp != NULL); 28920a48a24eStimh cp[0] = '\0'; 28930a48a24eStimh 28940a586ceaSMark Shellenbaum if ((error = spa_open(dataset, &spa, FTAG)) != 0) 28950a586ceaSMark Shellenbaum return (error); 28960a586ceaSMark Shellenbaum 28970a586ceaSMark Shellenbaum spa_vers = spa_version(spa); 28980a586ceaSMark Shellenbaum spa_close(spa, FTAG); 28990a586ceaSMark Shellenbaum 29000a586ceaSMark Shellenbaum zplver = zfs_zpl_version_map(spa_vers); 29010a586ceaSMark Shellenbaum fuids_ok = (zplver >= ZPL_VERSION_FUID); 29020a586ceaSMark Shellenbaum sa_ok = (zplver >= ZPL_VERSION_SA); 29030a48a24eStimh 29040a48a24eStimh /* 29050a48a24eStimh * Open parent object set so we can inherit zplprop values. 29060a48a24eStimh */ 2907503ad85cSMatthew Ahrens if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 29080a48a24eStimh return (error); 29090a48a24eStimh 29100a586ceaSMark Shellenbaum error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops, 29110a48a24eStimh zplprops, is_ci); 2912503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 29130a48a24eStimh return (error); 29140a48a24eStimh } 29150a48a24eStimh 29160a48a24eStimh static int 29170a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 29180a48a24eStimh nvlist_t *zplprops, boolean_t *is_ci) 29190a48a24eStimh { 29200a586ceaSMark Shellenbaum boolean_t fuids_ok; 29210a586ceaSMark Shellenbaum boolean_t sa_ok; 29220a48a24eStimh uint64_t zplver = ZPL_VERSION; 29230a48a24eStimh int error; 29240a48a24eStimh 29250a586ceaSMark Shellenbaum zplver = zfs_zpl_version_map(spa_vers); 29260a586ceaSMark Shellenbaum fuids_ok = (zplver >= ZPL_VERSION_FUID); 29270a586ceaSMark Shellenbaum sa_ok = (zplver >= ZPL_VERSION_SA); 29280a48a24eStimh 29290a586ceaSMark Shellenbaum error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok, 29300a586ceaSMark Shellenbaum createprops, zplprops, is_ci); 29310a48a24eStimh return (error); 29320a48a24eStimh } 29330a48a24eStimh 29343cb34c60Sahrens /* 29353cb34c60Sahrens * inputs: 29363cb34c60Sahrens * zc_objset_type type of objset to create (fs vs zvol) 29373cb34c60Sahrens * zc_name name of new objset 29383cb34c60Sahrens * zc_value name of snapshot to clone from (may be empty) 29393cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 29403cb34c60Sahrens * 2941de8267e0Stimh * outputs: none 29423cb34c60Sahrens */ 2943fa9e4066Sahrens static int 2944fa9e4066Sahrens zfs_ioc_create(zfs_cmd_t *zc) 2945fa9e4066Sahrens { 2946fa9e4066Sahrens objset_t *clone; 2947fa9e4066Sahrens int error = 0; 2948da6c28aaSamw zfs_creat_t zct; 2949ecd6cf80Smarks nvlist_t *nvprops = NULL; 2950ecd6cf80Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2951fa9e4066Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2952fa9e4066Sahrens 2953fa9e4066Sahrens switch (type) { 2954fa9e4066Sahrens 2955fa9e4066Sahrens case DMU_OST_ZFS: 2956fa9e4066Sahrens cbfunc = zfs_create_cb; 2957fa9e4066Sahrens break; 2958fa9e4066Sahrens 2959fa9e4066Sahrens case DMU_OST_ZVOL: 2960fa9e4066Sahrens cbfunc = zvol_create_cb; 2961fa9e4066Sahrens break; 2962fa9e4066Sahrens 2963fa9e4066Sahrens default: 29641d452cf5Sahrens cbfunc = NULL; 2965e7cbe64fSgw break; 2966fa9e4066Sahrens } 2967f18faf3fSek if (strchr(zc->zc_name, '@') || 2968f18faf3fSek strchr(zc->zc_name, '%')) 29691d452cf5Sahrens return (EINVAL); 2970fa9e4066Sahrens 2971e9dbad6fSeschrock if (zc->zc_nvlist_src != NULL && 2972990b4856Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2973478ed9adSEric Taylor zc->zc_iflags, &nvprops)) != 0) 2974e9dbad6fSeschrock return (error); 2975e9dbad6fSeschrock 2976de8267e0Stimh zct.zct_zplprops = NULL; 2977da6c28aaSamw zct.zct_props = nvprops; 2978da6c28aaSamw 2979e9dbad6fSeschrock if (zc->zc_value[0] != '\0') { 2980fa9e4066Sahrens /* 2981fa9e4066Sahrens * We're creating a clone of an existing snapshot. 2982fa9e4066Sahrens */ 2983e9dbad6fSeschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 2984e9dbad6fSeschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 2985ecd6cf80Smarks nvlist_free(nvprops); 2986fa9e4066Sahrens return (EINVAL); 2987e9dbad6fSeschrock } 2988fa9e4066Sahrens 2989503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_value, FTAG, &clone); 2990e9dbad6fSeschrock if (error) { 2991ecd6cf80Smarks nvlist_free(nvprops); 2992fa9e4066Sahrens return (error); 2993e9dbad6fSeschrock } 2994ab04eb8eStimh 2995ae46e4c7SMatthew Ahrens error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0); 2996503ad85cSMatthew Ahrens dmu_objset_rele(clone, FTAG); 2997da6c28aaSamw if (error) { 2998da6c28aaSamw nvlist_free(nvprops); 2999da6c28aaSamw return (error); 3000da6c28aaSamw } 3001fa9e4066Sahrens } else { 3002ab04eb8eStimh boolean_t is_insensitive = B_FALSE; 3003ab04eb8eStimh 3004e9dbad6fSeschrock if (cbfunc == NULL) { 3005ecd6cf80Smarks nvlist_free(nvprops); 30061d452cf5Sahrens return (EINVAL); 3007e9dbad6fSeschrock } 30085c5460e9Seschrock 3009e9dbad6fSeschrock if (type == DMU_OST_ZVOL) { 3010e9dbad6fSeschrock uint64_t volsize, volblocksize; 3011e9dbad6fSeschrock 3012ecd6cf80Smarks if (nvprops == NULL || 3013ecd6cf80Smarks nvlist_lookup_uint64(nvprops, 3014e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 3015e9dbad6fSeschrock &volsize) != 0) { 3016ecd6cf80Smarks nvlist_free(nvprops); 3017e9dbad6fSeschrock return (EINVAL); 3018e9dbad6fSeschrock } 3019e9dbad6fSeschrock 3020ecd6cf80Smarks if ((error = nvlist_lookup_uint64(nvprops, 3021e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3022e9dbad6fSeschrock &volblocksize)) != 0 && error != ENOENT) { 3023ecd6cf80Smarks nvlist_free(nvprops); 3024e9dbad6fSeschrock return (EINVAL); 3025e9dbad6fSeschrock } 3026e9dbad6fSeschrock 3027e9dbad6fSeschrock if (error != 0) 3028e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric( 3029e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 3030e9dbad6fSeschrock 3031e9dbad6fSeschrock if ((error = zvol_check_volblocksize( 3032e9dbad6fSeschrock volblocksize)) != 0 || 3033e9dbad6fSeschrock (error = zvol_check_volsize(volsize, 3034e9dbad6fSeschrock volblocksize)) != 0) { 3035ecd6cf80Smarks nvlist_free(nvprops); 30365c5460e9Seschrock return (error); 3037e9dbad6fSeschrock } 3038e7437265Sahrens } else if (type == DMU_OST_ZFS) { 3039da6c28aaSamw int error; 3040da6c28aaSamw 3041da6c28aaSamw /* 3042da6c28aaSamw * We have to have normalization and 3043da6c28aaSamw * case-folding flags correct when we do the 3044da6c28aaSamw * file system creation, so go figure them out 3045de8267e0Stimh * now. 3046da6c28aaSamw */ 3047de8267e0Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 3048de8267e0Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 3049de8267e0Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 30500a48a24eStimh zct.zct_zplprops, &is_insensitive); 3051da6c28aaSamw if (error != 0) { 3052da6c28aaSamw nvlist_free(nvprops); 3053de8267e0Stimh nvlist_free(zct.zct_zplprops); 3054da6c28aaSamw return (error); 3055da6c28aaSamw } 3056da6c28aaSamw } 3057ae46e4c7SMatthew Ahrens error = dmu_objset_create(zc->zc_name, type, 3058ab04eb8eStimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 3059de8267e0Stimh nvlist_free(zct.zct_zplprops); 3060fa9e4066Sahrens } 3061e9dbad6fSeschrock 3062e9dbad6fSeschrock /* 3063e9dbad6fSeschrock * It would be nice to do this atomically. 3064e9dbad6fSeschrock */ 3065e9dbad6fSeschrock if (error == 0) { 306692241e0bSTom Erickson error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL, 306792241e0bSTom Erickson nvprops, NULL); 306892241e0bSTom Erickson if (error != 0) 3069842727c2SChris Kirby (void) dmu_objset_destroy(zc->zc_name, B_FALSE); 3070e9dbad6fSeschrock } 3071ecd6cf80Smarks nvlist_free(nvprops); 3072fa9e4066Sahrens return (error); 3073fa9e4066Sahrens } 3074fa9e4066Sahrens 30753cb34c60Sahrens /* 30763cb34c60Sahrens * inputs: 30773cb34c60Sahrens * zc_name name of filesystem 30783cb34c60Sahrens * zc_value short name of snapshot 30793cb34c60Sahrens * zc_cookie recursive flag 308014843421SMatthew Ahrens * zc_nvlist_src[_size] property list 30813cb34c60Sahrens * 3082681d9761SEric Taylor * outputs: 3083681d9761SEric Taylor * zc_value short snapname (i.e. part after the '@') 30843cb34c60Sahrens */ 3085fa9e4066Sahrens static int 30861d452cf5Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 3087fa9e4066Sahrens { 3088bb0ade09Sahrens nvlist_t *nvprops = NULL; 3089bb0ade09Sahrens int error; 3090bb0ade09Sahrens boolean_t recursive = zc->zc_cookie; 3091bb0ade09Sahrens 3092e9dbad6fSeschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 30931d452cf5Sahrens return (EINVAL); 3094bb0ade09Sahrens 3095bb0ade09Sahrens if (zc->zc_nvlist_src != NULL && 3096bb0ade09Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 3097478ed9adSEric Taylor zc->zc_iflags, &nvprops)) != 0) 3098bb0ade09Sahrens return (error); 3099bb0ade09Sahrens 3100ea2f5b9eSMatthew Ahrens error = zfs_check_userprops(zc->zc_name, nvprops); 3101ea2f5b9eSMatthew Ahrens if (error) 3102ea2f5b9eSMatthew Ahrens goto out; 3103bb0ade09Sahrens 310492241e0bSTom Erickson if (!nvlist_empty(nvprops) && 3105ea2f5b9eSMatthew Ahrens zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) { 3106ea2f5b9eSMatthew Ahrens error = ENOTSUP; 3107ea2f5b9eSMatthew Ahrens goto out; 3108bb0ade09Sahrens } 3109ea2f5b9eSMatthew Ahrens 311099d5e173STim Haley error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL, 311199d5e173STim Haley nvprops, recursive, B_FALSE, -1); 3112ea2f5b9eSMatthew Ahrens 3113ea2f5b9eSMatthew Ahrens out: 3114bb0ade09Sahrens nvlist_free(nvprops); 3115bb0ade09Sahrens return (error); 31161d452cf5Sahrens } 3117fa9e4066Sahrens 3118cdf5b4caSmmusante int 3119fd136879SMatthew Ahrens zfs_unmount_snap(const char *name, void *arg) 31201d452cf5Sahrens { 31210b69c2f0Sahrens vfs_t *vfsp = NULL; 31221d452cf5Sahrens 3123745cd3c5Smaybee if (arg) { 3124745cd3c5Smaybee char *snapname = arg; 3125fd136879SMatthew Ahrens char *fullname = kmem_asprintf("%s@%s", name, snapname); 3126fd136879SMatthew Ahrens vfsp = zfs_get_vfs(fullname); 3127fd136879SMatthew Ahrens strfree(fullname); 31280b69c2f0Sahrens } else if (strchr(name, '@')) { 31291d452cf5Sahrens vfsp = zfs_get_vfs(name); 31301d452cf5Sahrens } 31311d452cf5Sahrens 31321d452cf5Sahrens if (vfsp) { 3133fa9e4066Sahrens /* 31341d452cf5Sahrens * Always force the unmount for snapshots. 3135fa9e4066Sahrens */ 31361d452cf5Sahrens int flag = MS_FORCE; 31371d452cf5Sahrens int err; 31381d452cf5Sahrens 31391d452cf5Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 3140fa9e4066Sahrens VFS_RELE(vfsp); 31411d452cf5Sahrens return (err); 3142fa9e4066Sahrens } 31431d452cf5Sahrens VFS_RELE(vfsp); 31441d452cf5Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 31451d452cf5Sahrens return (err); 31461d452cf5Sahrens } 31471d452cf5Sahrens return (0); 31481d452cf5Sahrens } 31491d452cf5Sahrens 31503cb34c60Sahrens /* 31513cb34c60Sahrens * inputs: 315219b94df9SMatthew Ahrens * zc_name name of filesystem, snaps must be under it 315319b94df9SMatthew Ahrens * zc_nvlist_src[_size] full names of snapshots to destroy 3154842727c2SChris Kirby * zc_defer_destroy mark for deferred destroy 31553cb34c60Sahrens * 315619b94df9SMatthew Ahrens * outputs: 315719b94df9SMatthew Ahrens * zc_name on failure, name of failed snapshot 31583cb34c60Sahrens */ 31591d452cf5Sahrens static int 316019b94df9SMatthew Ahrens zfs_ioc_destroy_snaps_nvl(zfs_cmd_t *zc) 31611d452cf5Sahrens { 316219b94df9SMatthew Ahrens int err, len; 316319b94df9SMatthew Ahrens nvlist_t *nvl; 316419b94df9SMatthew Ahrens nvpair_t *pair; 31651d452cf5Sahrens 316619b94df9SMatthew Ahrens if ((err = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 316719b94df9SMatthew Ahrens zc->zc_iflags, &nvl)) != 0) 31681d452cf5Sahrens return (err); 316919b94df9SMatthew Ahrens 317019b94df9SMatthew Ahrens len = strlen(zc->zc_name); 317119b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL; 317219b94df9SMatthew Ahrens pair = nvlist_next_nvpair(nvl, pair)) { 317319b94df9SMatthew Ahrens const char *name = nvpair_name(pair); 317419b94df9SMatthew Ahrens /* 317519b94df9SMatthew Ahrens * The snap name must be underneath the zc_name. This ensures 317619b94df9SMatthew Ahrens * that our permission checks were legitimate. 317719b94df9SMatthew Ahrens */ 317819b94df9SMatthew Ahrens if (strncmp(zc->zc_name, name, len) != 0 || 317919b94df9SMatthew Ahrens (name[len] != '@' && name[len] != '/')) { 318019b94df9SMatthew Ahrens nvlist_free(nvl); 318119b94df9SMatthew Ahrens return (EINVAL); 318219b94df9SMatthew Ahrens } 318319b94df9SMatthew Ahrens 318419b94df9SMatthew Ahrens (void) zfs_unmount_snap(name, NULL); 318519b94df9SMatthew Ahrens } 318619b94df9SMatthew Ahrens 318719b94df9SMatthew Ahrens err = dmu_snapshots_destroy_nvl(nvl, zc->zc_defer_destroy, 318819b94df9SMatthew Ahrens zc->zc_name); 318919b94df9SMatthew Ahrens nvlist_free(nvl); 319019b94df9SMatthew Ahrens return (err); 31911d452cf5Sahrens } 31921d452cf5Sahrens 31933cb34c60Sahrens /* 31943cb34c60Sahrens * inputs: 31953cb34c60Sahrens * zc_name name of dataset to destroy 31963cb34c60Sahrens * zc_objset_type type of objset 3197842727c2SChris Kirby * zc_defer_destroy mark for deferred destroy 31983cb34c60Sahrens * 31993cb34c60Sahrens * outputs: none 32003cb34c60Sahrens */ 32011d452cf5Sahrens static int 32021d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 32031d452cf5Sahrens { 3204681d9761SEric Taylor int err; 32051d452cf5Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 3206681d9761SEric Taylor err = zfs_unmount_snap(zc->zc_name, NULL); 32071d452cf5Sahrens if (err) 32081d452cf5Sahrens return (err); 3209fa9e4066Sahrens } 3210fa9e4066Sahrens 3211681d9761SEric Taylor err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy); 3212681d9761SEric Taylor if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 32135c987a37SChris Kirby (void) zvol_remove_minor(zc->zc_name); 3214681d9761SEric Taylor return (err); 3215fa9e4066Sahrens } 3216fa9e4066Sahrens 32173cb34c60Sahrens /* 32183cb34c60Sahrens * inputs: 32194ccbb6e7Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 32203cb34c60Sahrens * 32213cb34c60Sahrens * outputs: none 32223cb34c60Sahrens */ 3223fa9e4066Sahrens static int 3224fa9e4066Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 3225fa9e4066Sahrens { 3226ae46e4c7SMatthew Ahrens dsl_dataset_t *ds, *clone; 32274ccbb6e7Sahrens int error; 3228ae46e4c7SMatthew Ahrens zfsvfs_t *zfsvfs; 3229ae46e4c7SMatthew Ahrens char *clone_name; 3230ae46e4c7SMatthew Ahrens 3231ae46e4c7SMatthew Ahrens error = dsl_dataset_hold(zc->zc_name, FTAG, &ds); 3232ae46e4c7SMatthew Ahrens if (error) 3233ae46e4c7SMatthew Ahrens return (error); 3234ae46e4c7SMatthew Ahrens 3235ae46e4c7SMatthew Ahrens /* must not be a snapshot */ 3236ae46e4c7SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 3237ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3238ae46e4c7SMatthew Ahrens return (EINVAL); 3239ae46e4c7SMatthew Ahrens } 3240ae46e4c7SMatthew Ahrens 3241ae46e4c7SMatthew Ahrens /* must have a most recent snapshot */ 3242ae46e4c7SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 3243ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3244ae46e4c7SMatthew Ahrens return (EINVAL); 3245ae46e4c7SMatthew Ahrens } 32464ccbb6e7Sahrens 32474ccbb6e7Sahrens /* 3248ae46e4c7SMatthew Ahrens * Create clone of most recent snapshot. 32494ccbb6e7Sahrens */ 3250ae46e4c7SMatthew Ahrens clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name); 3251ae46e4c7SMatthew Ahrens error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT); 32524ccbb6e7Sahrens if (error) 3253ae46e4c7SMatthew Ahrens goto out; 32544ccbb6e7Sahrens 3255503ad85cSMatthew Ahrens error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone); 3256ae46e4c7SMatthew Ahrens if (error) 3257ae46e4c7SMatthew Ahrens goto out; 3258ae46e4c7SMatthew Ahrens 3259ae46e4c7SMatthew Ahrens /* 3260ae46e4c7SMatthew Ahrens * Do clone swap. 3261ae46e4c7SMatthew Ahrens */ 326214843421SMatthew Ahrens if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 3263503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs); 326447f263f4Sek if (error == 0) { 326547f263f4Sek int resume_err; 32664ccbb6e7Sahrens 3267ae46e4c7SMatthew Ahrens if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 3268ae46e4c7SMatthew Ahrens error = dsl_dataset_clone_swap(clone, ds, 3269ae46e4c7SMatthew Ahrens B_TRUE); 3270ae46e4c7SMatthew Ahrens dsl_dataset_disown(ds, FTAG); 3271ae46e4c7SMatthew Ahrens ds = NULL; 3272ae46e4c7SMatthew Ahrens } else { 3273ae46e4c7SMatthew Ahrens error = EBUSY; 3274ae46e4c7SMatthew Ahrens } 3275503ad85cSMatthew Ahrens resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); 327647f263f4Sek error = error ? error : resume_err; 327747f263f4Sek } 32784ccbb6e7Sahrens VFS_RELE(zfsvfs->z_vfs); 32794ccbb6e7Sahrens } else { 3280ae46e4c7SMatthew Ahrens if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 3281ae46e4c7SMatthew Ahrens error = dsl_dataset_clone_swap(clone, ds, B_TRUE); 3282ae46e4c7SMatthew Ahrens dsl_dataset_disown(ds, FTAG); 3283ae46e4c7SMatthew Ahrens ds = NULL; 3284ae46e4c7SMatthew Ahrens } else { 3285ae46e4c7SMatthew Ahrens error = EBUSY; 3286ae46e4c7SMatthew Ahrens } 32874ccbb6e7Sahrens } 32884ccbb6e7Sahrens 3289ae46e4c7SMatthew Ahrens /* 3290ae46e4c7SMatthew Ahrens * Destroy clone (which also closes it). 3291ae46e4c7SMatthew Ahrens */ 3292ae46e4c7SMatthew Ahrens (void) dsl_dataset_destroy(clone, FTAG, B_FALSE); 3293ae46e4c7SMatthew Ahrens 3294ae46e4c7SMatthew Ahrens out: 3295ae46e4c7SMatthew Ahrens strfree(clone_name); 3296ae46e4c7SMatthew Ahrens if (ds) 3297ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 32984ccbb6e7Sahrens return (error); 3299fa9e4066Sahrens } 3300fa9e4066Sahrens 33013cb34c60Sahrens /* 33023cb34c60Sahrens * inputs: 33033cb34c60Sahrens * zc_name old name of dataset 33043cb34c60Sahrens * zc_value new name of dataset 33053cb34c60Sahrens * zc_cookie recursive flag (only valid for snapshots) 33063cb34c60Sahrens * 33073cb34c60Sahrens * outputs: none 33083cb34c60Sahrens */ 3309fa9e4066Sahrens static int 3310fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 3311fa9e4066Sahrens { 33127f1f55eaSvb boolean_t recursive = zc->zc_cookie & 1; 3313cdf5b4caSmmusante 3314e9dbad6fSeschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 3315f18faf3fSek if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3316f18faf3fSek strchr(zc->zc_value, '%')) 3317fa9e4066Sahrens return (EINVAL); 3318fa9e4066Sahrens 3319cdf5b4caSmmusante /* 3320cdf5b4caSmmusante * Unmount snapshot unless we're doing a recursive rename, 3321cdf5b4caSmmusante * in which case the dataset code figures out which snapshots 3322cdf5b4caSmmusante * to unmount. 3323cdf5b4caSmmusante */ 3324cdf5b4caSmmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 3325fa9e4066Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 33261d452cf5Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 33271d452cf5Sahrens if (err) 33281d452cf5Sahrens return (err); 3329fa9e4066Sahrens } 3330681d9761SEric Taylor if (zc->zc_objset_type == DMU_OST_ZVOL) 3331681d9761SEric Taylor (void) zvol_remove_minor(zc->zc_name); 3332cdf5b4caSmmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 3333fa9e4066Sahrens } 3334fa9e4066Sahrens 333592241e0bSTom Erickson static int 333692241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 333792241e0bSTom Erickson { 333892241e0bSTom Erickson const char *propname = nvpair_name(pair); 333992241e0bSTom Erickson boolean_t issnap = (strchr(dsname, '@') != NULL); 334092241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 334192241e0bSTom Erickson uint64_t intval; 334292241e0bSTom Erickson int err; 334392241e0bSTom Erickson 334492241e0bSTom Erickson if (prop == ZPROP_INVAL) { 334592241e0bSTom Erickson if (zfs_prop_user(propname)) { 334692241e0bSTom Erickson if (err = zfs_secpolicy_write_perms(dsname, 334792241e0bSTom Erickson ZFS_DELEG_PERM_USERPROP, cr)) 334892241e0bSTom Erickson return (err); 334992241e0bSTom Erickson return (0); 335092241e0bSTom Erickson } 335192241e0bSTom Erickson 335292241e0bSTom Erickson if (!issnap && zfs_prop_userquota(propname)) { 335392241e0bSTom Erickson const char *perm = NULL; 335492241e0bSTom Erickson const char *uq_prefix = 335592241e0bSTom Erickson zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 335692241e0bSTom Erickson const char *gq_prefix = 335792241e0bSTom Erickson zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 335892241e0bSTom Erickson 335992241e0bSTom Erickson if (strncmp(propname, uq_prefix, 336092241e0bSTom Erickson strlen(uq_prefix)) == 0) { 336192241e0bSTom Erickson perm = ZFS_DELEG_PERM_USERQUOTA; 336292241e0bSTom Erickson } else if (strncmp(propname, gq_prefix, 336392241e0bSTom Erickson strlen(gq_prefix)) == 0) { 336492241e0bSTom Erickson perm = ZFS_DELEG_PERM_GROUPQUOTA; 336592241e0bSTom Erickson } else { 336692241e0bSTom Erickson /* USERUSED and GROUPUSED are read-only */ 336792241e0bSTom Erickson return (EINVAL); 336892241e0bSTom Erickson } 336992241e0bSTom Erickson 337092241e0bSTom Erickson if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 337192241e0bSTom Erickson return (err); 337292241e0bSTom Erickson return (0); 337392241e0bSTom Erickson } 337492241e0bSTom Erickson 337592241e0bSTom Erickson return (EINVAL); 337692241e0bSTom Erickson } 337792241e0bSTom Erickson 337892241e0bSTom Erickson if (issnap) 337992241e0bSTom Erickson return (EINVAL); 338092241e0bSTom Erickson 338192241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 338292241e0bSTom Erickson /* 338392241e0bSTom Erickson * dsl_prop_get_all_impl() returns properties in this 338492241e0bSTom Erickson * format. 338592241e0bSTom Erickson */ 338692241e0bSTom Erickson nvlist_t *attrs; 338792241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 338892241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 338992241e0bSTom Erickson &pair) == 0); 339092241e0bSTom Erickson } 339192241e0bSTom Erickson 339292241e0bSTom Erickson /* 339392241e0bSTom Erickson * Check that this value is valid for this pool version 339492241e0bSTom Erickson */ 339592241e0bSTom Erickson switch (prop) { 339692241e0bSTom Erickson case ZFS_PROP_COMPRESSION: 339792241e0bSTom Erickson /* 339892241e0bSTom Erickson * If the user specified gzip compression, make sure 339992241e0bSTom Erickson * the SPA supports it. We ignore any errors here since 340092241e0bSTom Erickson * we'll catch them later. 340192241e0bSTom Erickson */ 340292241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_UINT64 && 340392241e0bSTom Erickson nvpair_value_uint64(pair, &intval) == 0) { 340492241e0bSTom Erickson if (intval >= ZIO_COMPRESS_GZIP_1 && 340592241e0bSTom Erickson intval <= ZIO_COMPRESS_GZIP_9 && 340692241e0bSTom Erickson zfs_earlier_version(dsname, 340792241e0bSTom Erickson SPA_VERSION_GZIP_COMPRESSION)) { 340892241e0bSTom Erickson return (ENOTSUP); 340992241e0bSTom Erickson } 341092241e0bSTom Erickson 341192241e0bSTom Erickson if (intval == ZIO_COMPRESS_ZLE && 341292241e0bSTom Erickson zfs_earlier_version(dsname, 341392241e0bSTom Erickson SPA_VERSION_ZLE_COMPRESSION)) 341492241e0bSTom Erickson return (ENOTSUP); 341592241e0bSTom Erickson 341692241e0bSTom Erickson /* 341792241e0bSTom Erickson * If this is a bootable dataset then 341892241e0bSTom Erickson * verify that the compression algorithm 341992241e0bSTom Erickson * is supported for booting. We must return 342092241e0bSTom Erickson * something other than ENOTSUP since it 342192241e0bSTom Erickson * implies a downrev pool version. 342292241e0bSTom Erickson */ 342392241e0bSTom Erickson if (zfs_is_bootfs(dsname) && 342492241e0bSTom Erickson !BOOTFS_COMPRESS_VALID(intval)) { 342592241e0bSTom Erickson return (ERANGE); 342692241e0bSTom Erickson } 342792241e0bSTom Erickson } 342892241e0bSTom Erickson break; 342992241e0bSTom Erickson 343092241e0bSTom Erickson case ZFS_PROP_COPIES: 343192241e0bSTom Erickson if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 343292241e0bSTom Erickson return (ENOTSUP); 343392241e0bSTom Erickson break; 343492241e0bSTom Erickson 343592241e0bSTom Erickson case ZFS_PROP_DEDUP: 343692241e0bSTom Erickson if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 343792241e0bSTom Erickson return (ENOTSUP); 343892241e0bSTom Erickson break; 343992241e0bSTom Erickson 344092241e0bSTom Erickson case ZFS_PROP_SHARESMB: 344192241e0bSTom Erickson if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 344292241e0bSTom Erickson return (ENOTSUP); 344392241e0bSTom Erickson break; 344492241e0bSTom Erickson 344592241e0bSTom Erickson case ZFS_PROP_ACLINHERIT: 344692241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_UINT64 && 344792241e0bSTom Erickson nvpair_value_uint64(pair, &intval) == 0) { 344892241e0bSTom Erickson if (intval == ZFS_ACL_PASSTHROUGH_X && 344992241e0bSTom Erickson zfs_earlier_version(dsname, 345092241e0bSTom Erickson SPA_VERSION_PASSTHROUGH_X)) 345192241e0bSTom Erickson return (ENOTSUP); 345292241e0bSTom Erickson } 345392241e0bSTom Erickson break; 345492241e0bSTom Erickson } 345592241e0bSTom Erickson 345692241e0bSTom Erickson return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 345792241e0bSTom Erickson } 345892241e0bSTom Erickson 345992241e0bSTom Erickson /* 346092241e0bSTom Erickson * Removes properties from the given props list that fail permission checks 346192241e0bSTom Erickson * needed to clear them and to restore them in case of a receive error. For each 346292241e0bSTom Erickson * property, make sure we have both set and inherit permissions. 346392241e0bSTom Erickson * 346492241e0bSTom Erickson * Returns the first error encountered if any permission checks fail. If the 346592241e0bSTom Erickson * caller provides a non-NULL errlist, it also gives the complete list of names 346692241e0bSTom Erickson * of all the properties that failed a permission check along with the 346792241e0bSTom Erickson * corresponding error numbers. The caller is responsible for freeing the 346892241e0bSTom Erickson * returned errlist. 346992241e0bSTom Erickson * 347092241e0bSTom Erickson * If every property checks out successfully, zero is returned and the list 347192241e0bSTom Erickson * pointed at by errlist is NULL. 347292241e0bSTom Erickson */ 347392241e0bSTom Erickson static int 347492241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 3475745cd3c5Smaybee { 3476745cd3c5Smaybee zfs_cmd_t *zc; 347792241e0bSTom Erickson nvpair_t *pair, *next_pair; 347892241e0bSTom Erickson nvlist_t *errors; 347992241e0bSTom Erickson int err, rv = 0; 3480745cd3c5Smaybee 3481745cd3c5Smaybee if (props == NULL) 348292241e0bSTom Erickson return (0); 348392241e0bSTom Erickson 348492241e0bSTom Erickson VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 348592241e0bSTom Erickson 3486745cd3c5Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 3487745cd3c5Smaybee (void) strcpy(zc->zc_name, dataset); 348892241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL); 348992241e0bSTom Erickson while (pair != NULL) { 349092241e0bSTom Erickson next_pair = nvlist_next_nvpair(props, pair); 349192241e0bSTom Erickson 349292241e0bSTom Erickson (void) strcpy(zc->zc_value, nvpair_name(pair)); 349392241e0bSTom Erickson if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 349492241e0bSTom Erickson (err = zfs_secpolicy_inherit(zc, CRED())) != 0) { 349592241e0bSTom Erickson VERIFY(nvlist_remove_nvpair(props, pair) == 0); 349692241e0bSTom Erickson VERIFY(nvlist_add_int32(errors, 349792241e0bSTom Erickson zc->zc_value, err) == 0); 349892241e0bSTom Erickson } 349992241e0bSTom Erickson pair = next_pair; 3500745cd3c5Smaybee } 3501745cd3c5Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 350292241e0bSTom Erickson 350392241e0bSTom Erickson if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 350492241e0bSTom Erickson nvlist_free(errors); 350592241e0bSTom Erickson errors = NULL; 350692241e0bSTom Erickson } else { 350792241e0bSTom Erickson VERIFY(nvpair_value_int32(pair, &rv) == 0); 350892241e0bSTom Erickson } 350992241e0bSTom Erickson 351092241e0bSTom Erickson if (errlist == NULL) 351192241e0bSTom Erickson nvlist_free(errors); 351292241e0bSTom Erickson else 351392241e0bSTom Erickson *errlist = errors; 351492241e0bSTom Erickson 351592241e0bSTom Erickson return (rv); 351692241e0bSTom Erickson } 351792241e0bSTom Erickson 351892241e0bSTom Erickson static boolean_t 351992241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2) 352092241e0bSTom Erickson { 352192241e0bSTom Erickson if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 352292241e0bSTom Erickson /* dsl_prop_get_all_impl() format */ 352392241e0bSTom Erickson nvlist_t *attrs; 352492241e0bSTom Erickson VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 352592241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 352692241e0bSTom Erickson &p1) == 0); 352792241e0bSTom Erickson } 352892241e0bSTom Erickson 352992241e0bSTom Erickson if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 353092241e0bSTom Erickson nvlist_t *attrs; 353192241e0bSTom Erickson VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 353292241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 353392241e0bSTom Erickson &p2) == 0); 353492241e0bSTom Erickson } 353592241e0bSTom Erickson 353692241e0bSTom Erickson if (nvpair_type(p1) != nvpair_type(p2)) 353792241e0bSTom Erickson return (B_FALSE); 353892241e0bSTom Erickson 353992241e0bSTom Erickson if (nvpair_type(p1) == DATA_TYPE_STRING) { 354092241e0bSTom Erickson char *valstr1, *valstr2; 354192241e0bSTom Erickson 354292241e0bSTom Erickson VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 354392241e0bSTom Erickson VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 354492241e0bSTom Erickson return (strcmp(valstr1, valstr2) == 0); 354592241e0bSTom Erickson } else { 354692241e0bSTom Erickson uint64_t intval1, intval2; 354792241e0bSTom Erickson 354892241e0bSTom Erickson VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 354992241e0bSTom Erickson VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 355092241e0bSTom Erickson return (intval1 == intval2); 355192241e0bSTom Erickson } 3552745cd3c5Smaybee } 3553745cd3c5Smaybee 355492241e0bSTom Erickson /* 355592241e0bSTom Erickson * Remove properties from props if they are not going to change (as determined 355692241e0bSTom Erickson * by comparison with origprops). Remove them from origprops as well, since we 355792241e0bSTom Erickson * do not need to clear or restore properties that won't change. 355892241e0bSTom Erickson */ 355992241e0bSTom Erickson static void 356092241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops) 356192241e0bSTom Erickson { 356292241e0bSTom Erickson nvpair_t *pair, *next_pair; 356392241e0bSTom Erickson 356492241e0bSTom Erickson if (origprops == NULL) 356592241e0bSTom Erickson return; /* all props need to be received */ 356692241e0bSTom Erickson 356792241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL); 356892241e0bSTom Erickson while (pair != NULL) { 356992241e0bSTom Erickson const char *propname = nvpair_name(pair); 357092241e0bSTom Erickson nvpair_t *match; 357192241e0bSTom Erickson 357292241e0bSTom Erickson next_pair = nvlist_next_nvpair(props, pair); 357392241e0bSTom Erickson 357492241e0bSTom Erickson if ((nvlist_lookup_nvpair(origprops, propname, 357592241e0bSTom Erickson &match) != 0) || !propval_equals(pair, match)) 357692241e0bSTom Erickson goto next; /* need to set received value */ 357792241e0bSTom Erickson 357892241e0bSTom Erickson /* don't clear the existing received value */ 357992241e0bSTom Erickson (void) nvlist_remove_nvpair(origprops, match); 358092241e0bSTom Erickson /* don't bother receiving the property */ 358192241e0bSTom Erickson (void) nvlist_remove_nvpair(props, pair); 358292241e0bSTom Erickson next: 358392241e0bSTom Erickson pair = next_pair; 358492241e0bSTom Erickson } 358592241e0bSTom Erickson } 358692241e0bSTom Erickson 358792241e0bSTom Erickson #ifdef DEBUG 358892241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err; 358992241e0bSTom Erickson #endif 359092241e0bSTom Erickson 35913cb34c60Sahrens /* 35923cb34c60Sahrens * inputs: 35933cb34c60Sahrens * zc_name name of containing filesystem 35943cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 35953cb34c60Sahrens * zc_value name of snapshot to create 35963cb34c60Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 35973cb34c60Sahrens * zc_cookie file descriptor to recv from 35983cb34c60Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 35993cb34c60Sahrens * zc_guid force flag 3600c99e4bdcSChris Kirby * zc_cleanup_fd cleanup-on-exit file descriptor 3601c99e4bdcSChris Kirby * zc_action_handle handle for this guid/ds mapping (or zero on first call) 36023cb34c60Sahrens * 36033cb34c60Sahrens * outputs: 36043cb34c60Sahrens * zc_cookie number of bytes read 360592241e0bSTom Erickson * zc_nvlist_dst{_size} error for each unapplied received property 360692241e0bSTom Erickson * zc_obj zprop_errflags_t 3607c99e4bdcSChris Kirby * zc_action_handle handle for this guid/ds mapping 36083cb34c60Sahrens */ 3609fa9e4066Sahrens static int 36103cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 3611fa9e4066Sahrens { 3612fa9e4066Sahrens file_t *fp; 3613f18faf3fSek objset_t *os; 36143cb34c60Sahrens dmu_recv_cookie_t drc; 3615f18faf3fSek boolean_t force = (boolean_t)zc->zc_guid; 361692241e0bSTom Erickson int fd; 361792241e0bSTom Erickson int error = 0; 361892241e0bSTom Erickson int props_error = 0; 361992241e0bSTom Erickson nvlist_t *errors; 36203cb34c60Sahrens offset_t off; 362192241e0bSTom Erickson nvlist_t *props = NULL; /* sent properties */ 362292241e0bSTom Erickson nvlist_t *origprops = NULL; /* existing properties */ 36233cb34c60Sahrens objset_t *origin = NULL; 36243cb34c60Sahrens char *tosnap; 36253cb34c60Sahrens char tofs[ZFS_MAXNAMELEN]; 362692241e0bSTom Erickson boolean_t first_recvd_props = B_FALSE; 3627fa9e4066Sahrens 36283ccfa83cSahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3629f18faf3fSek strchr(zc->zc_value, '@') == NULL || 3630f18faf3fSek strchr(zc->zc_value, '%')) 36313ccfa83cSahrens return (EINVAL); 36323ccfa83cSahrens 36333cb34c60Sahrens (void) strcpy(tofs, zc->zc_value); 36343cb34c60Sahrens tosnap = strchr(tofs, '@'); 363592241e0bSTom Erickson *tosnap++ = '\0'; 36363cb34c60Sahrens 36373cb34c60Sahrens if (zc->zc_nvlist_src != NULL && 36383cb34c60Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 3639478ed9adSEric Taylor zc->zc_iflags, &props)) != 0) 36403cb34c60Sahrens return (error); 36413cb34c60Sahrens 3642fa9e4066Sahrens fd = zc->zc_cookie; 3643fa9e4066Sahrens fp = getf(fd); 36443cb34c60Sahrens if (fp == NULL) { 36453cb34c60Sahrens nvlist_free(props); 3646fa9e4066Sahrens return (EBADF); 36473cb34c60Sahrens } 3648f18faf3fSek 364992241e0bSTom Erickson VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 365092241e0bSTom Erickson 3651503ad85cSMatthew Ahrens if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) { 365292241e0bSTom Erickson if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) && 365392241e0bSTom Erickson !dsl_prop_get_hasrecvd(os)) { 365492241e0bSTom Erickson first_recvd_props = B_TRUE; 365592241e0bSTom Erickson } 365692241e0bSTom Erickson 3657745cd3c5Smaybee /* 365892241e0bSTom Erickson * If new received properties are supplied, they are to 365992241e0bSTom Erickson * completely replace the existing received properties, so stash 366092241e0bSTom Erickson * away the existing ones. 3661745cd3c5Smaybee */ 366292241e0bSTom Erickson if (dsl_prop_get_received(os, &origprops) == 0) { 366392241e0bSTom Erickson nvlist_t *errlist = NULL; 366492241e0bSTom Erickson /* 366592241e0bSTom Erickson * Don't bother writing a property if its value won't 366692241e0bSTom Erickson * change (and avoid the unnecessary security checks). 366792241e0bSTom Erickson * 366892241e0bSTom Erickson * The first receive after SPA_VERSION_RECVD_PROPS is a 366992241e0bSTom Erickson * special case where we blow away all local properties 367092241e0bSTom Erickson * regardless. 367192241e0bSTom Erickson */ 367292241e0bSTom Erickson if (!first_recvd_props) 367392241e0bSTom Erickson props_reduce(props, origprops); 367492241e0bSTom Erickson if (zfs_check_clearable(tofs, origprops, 367592241e0bSTom Erickson &errlist) != 0) 367692241e0bSTom Erickson (void) nvlist_merge(errors, errlist, 0); 367792241e0bSTom Erickson nvlist_free(errlist); 367892241e0bSTom Erickson } 3679745cd3c5Smaybee 3680503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 3681f18faf3fSek } 3682f18faf3fSek 36833cb34c60Sahrens if (zc->zc_string[0]) { 3684503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_string, FTAG, &origin); 3685745cd3c5Smaybee if (error) 3686745cd3c5Smaybee goto out; 36873cb34c60Sahrens } 36883cb34c60Sahrens 36899e69d7d0SLori Alt error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds, 36909e69d7d0SLori Alt &zc->zc_begin_record, force, origin, &drc); 36913cb34c60Sahrens if (origin) 3692503ad85cSMatthew Ahrens dmu_objset_rele(origin, FTAG); 3693745cd3c5Smaybee if (error) 3694745cd3c5Smaybee goto out; 3695f18faf3fSek 3696f18faf3fSek /* 369792241e0bSTom Erickson * Set properties before we receive the stream so that they are applied 369892241e0bSTom Erickson * to the new data. Note that we must call dmu_recv_stream() if 369992241e0bSTom Erickson * dmu_recv_begin() succeeds. 3700f18faf3fSek */ 37013cb34c60Sahrens if (props) { 370292241e0bSTom Erickson nvlist_t *errlist; 370392241e0bSTom Erickson 370492241e0bSTom Erickson if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) { 370592241e0bSTom Erickson if (drc.drc_newfs) { 370692241e0bSTom Erickson if (spa_version(os->os_spa) >= 370792241e0bSTom Erickson SPA_VERSION_RECVD_PROPS) 370892241e0bSTom Erickson first_recvd_props = B_TRUE; 370992241e0bSTom Erickson } else if (origprops != NULL) { 371092241e0bSTom Erickson if (clear_received_props(os, tofs, origprops, 371192241e0bSTom Erickson first_recvd_props ? NULL : props) != 0) 371292241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR; 371392241e0bSTom Erickson } else { 371492241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR; 371592241e0bSTom Erickson } 371692241e0bSTom Erickson dsl_prop_set_hasrecvd(os); 371792241e0bSTom Erickson } else if (!drc.drc_newfs) { 371892241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR; 371992241e0bSTom Erickson } 372092241e0bSTom Erickson 372192241e0bSTom Erickson (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 372292241e0bSTom Erickson props, &errlist); 372392241e0bSTom Erickson (void) nvlist_merge(errors, errlist, 0); 372492241e0bSTom Erickson nvlist_free(errlist); 372592241e0bSTom Erickson } 372692241e0bSTom Erickson 372792241e0bSTom Erickson if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) { 3728745cd3c5Smaybee /* 372992241e0bSTom Erickson * Caller made zc->zc_nvlist_dst less than the minimum expected 373092241e0bSTom Erickson * size or supplied an invalid address. 3731745cd3c5Smaybee */ 373292241e0bSTom Erickson props_error = EINVAL; 37333cb34c60Sahrens } 37343cb34c60Sahrens 37353cb34c60Sahrens off = fp->f_offset; 3736c99e4bdcSChris Kirby error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd, 3737c99e4bdcSChris Kirby &zc->zc_action_handle); 3738a2eea2e1Sahrens 3739f4b94bdeSMatthew Ahrens if (error == 0) { 3740f4b94bdeSMatthew Ahrens zfsvfs_t *zfsvfs = NULL; 3741745cd3c5Smaybee 3742f4b94bdeSMatthew Ahrens if (getzfsvfs(tofs, &zfsvfs) == 0) { 3743f4b94bdeSMatthew Ahrens /* online recv */ 3744f4b94bdeSMatthew Ahrens int end_err; 3745745cd3c5Smaybee 3746503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs); 3747f4b94bdeSMatthew Ahrens /* 3748f4b94bdeSMatthew Ahrens * If the suspend fails, then the recv_end will 3749f4b94bdeSMatthew Ahrens * likely also fail, and clean up after itself. 3750f4b94bdeSMatthew Ahrens */ 3751f4b94bdeSMatthew Ahrens end_err = dmu_recv_end(&drc); 37525c703fceSGeorge Wilson if (error == 0) 37535c703fceSGeorge Wilson error = zfs_resume_fs(zfsvfs, tofs); 3754f4b94bdeSMatthew Ahrens error = error ? error : end_err; 3755f4b94bdeSMatthew Ahrens VFS_RELE(zfsvfs->z_vfs); 3756745cd3c5Smaybee } else { 3757f4b94bdeSMatthew Ahrens error = dmu_recv_end(&drc); 37583cb34c60Sahrens } 375947f263f4Sek } 37603cb34c60Sahrens 37613cb34c60Sahrens zc->zc_cookie = off - fp->f_offset; 37623cb34c60Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 37633cb34c60Sahrens fp->f_offset = off; 3764a2eea2e1Sahrens 376592241e0bSTom Erickson #ifdef DEBUG 376692241e0bSTom Erickson if (zfs_ioc_recv_inject_err) { 376792241e0bSTom Erickson zfs_ioc_recv_inject_err = B_FALSE; 376892241e0bSTom Erickson error = 1; 376992241e0bSTom Erickson } 377092241e0bSTom Erickson #endif 3771745cd3c5Smaybee /* 3772745cd3c5Smaybee * On error, restore the original props. 3773745cd3c5Smaybee */ 3774745cd3c5Smaybee if (error && props) { 377592241e0bSTom Erickson if (dmu_objset_hold(tofs, FTAG, &os) == 0) { 377692241e0bSTom Erickson if (clear_received_props(os, tofs, props, NULL) != 0) { 377792241e0bSTom Erickson /* 377892241e0bSTom Erickson * We failed to clear the received properties. 377992241e0bSTom Erickson * Since we may have left a $recvd value on the 378092241e0bSTom Erickson * system, we can't clear the $hasrecvd flag. 378192241e0bSTom Erickson */ 378292241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 378392241e0bSTom Erickson } else if (first_recvd_props) { 378492241e0bSTom Erickson dsl_prop_unset_hasrecvd(os); 378592241e0bSTom Erickson } 378692241e0bSTom Erickson dmu_objset_rele(os, FTAG); 378792241e0bSTom Erickson } else if (!drc.drc_newfs) { 378892241e0bSTom Erickson /* We failed to clear the received properties. */ 378992241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 379092241e0bSTom Erickson } 379192241e0bSTom Erickson 379292241e0bSTom Erickson if (origprops == NULL && !drc.drc_newfs) { 379392241e0bSTom Erickson /* We failed to stash the original properties. */ 379492241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 379592241e0bSTom Erickson } 379692241e0bSTom Erickson 379792241e0bSTom Erickson /* 379892241e0bSTom Erickson * dsl_props_set() will not convert RECEIVED to LOCAL on or 379992241e0bSTom Erickson * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 380092241e0bSTom Erickson * explictly if we're restoring local properties cleared in the 380192241e0bSTom Erickson * first new-style receive. 380292241e0bSTom Erickson */ 380392241e0bSTom Erickson if (origprops != NULL && 380492241e0bSTom Erickson zfs_set_prop_nvlist(tofs, (first_recvd_props ? 380592241e0bSTom Erickson ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 380692241e0bSTom Erickson origprops, NULL) != 0) { 380792241e0bSTom Erickson /* 380892241e0bSTom Erickson * We stashed the original properties but failed to 380992241e0bSTom Erickson * restore them. 381092241e0bSTom Erickson */ 381192241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 381292241e0bSTom Erickson } 3813745cd3c5Smaybee } 3814745cd3c5Smaybee out: 3815745cd3c5Smaybee nvlist_free(props); 3816745cd3c5Smaybee nvlist_free(origprops); 381792241e0bSTom Erickson nvlist_free(errors); 3818fa9e4066Sahrens releasef(fd); 381992241e0bSTom Erickson 382092241e0bSTom Erickson if (error == 0) 382192241e0bSTom Erickson error = props_error; 382292241e0bSTom Erickson 3823fa9e4066Sahrens return (error); 3824fa9e4066Sahrens } 3825fa9e4066Sahrens 38263cb34c60Sahrens /* 38273cb34c60Sahrens * inputs: 38283cb34c60Sahrens * zc_name name of snapshot to send 38293cb34c60Sahrens * zc_cookie file descriptor to send stream to 3830a7f53a56SChris Kirby * zc_obj fromorigin flag (mutually exclusive with zc_fromobj) 3831a7f53a56SChris Kirby * zc_sendobj objsetid of snapshot to send 3832a7f53a56SChris Kirby * zc_fromobj objsetid of incremental fromsnap (may be zero) 383319b94df9SMatthew Ahrens * zc_guid if set, estimate size of stream only. zc_cookie is ignored. 383419b94df9SMatthew Ahrens * output size in zc_objset_type. 38353cb34c60Sahrens * 38363cb34c60Sahrens * outputs: none 38373cb34c60Sahrens */ 3838fa9e4066Sahrens static int 38393cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc) 3840fa9e4066Sahrens { 3841fa9e4066Sahrens objset_t *fromsnap = NULL; 3842fa9e4066Sahrens objset_t *tosnap; 3843fa9e4066Sahrens int error; 38443cb34c60Sahrens offset_t off; 3845a7f53a56SChris Kirby dsl_dataset_t *ds; 3846a7f53a56SChris Kirby dsl_dataset_t *dsfrom = NULL; 3847a7f53a56SChris Kirby spa_t *spa; 3848a7f53a56SChris Kirby dsl_pool_t *dp; 384919b94df9SMatthew Ahrens boolean_t estimate = (zc->zc_guid != 0); 3850fa9e4066Sahrens 3851a7f53a56SChris Kirby error = spa_open(zc->zc_name, &spa, FTAG); 3852fa9e4066Sahrens if (error) 3853fa9e4066Sahrens return (error); 3854fa9e4066Sahrens 3855a7f53a56SChris Kirby dp = spa_get_dsl(spa); 3856a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 3857a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 3858a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 3859a7f53a56SChris Kirby if (error) { 3860a7f53a56SChris Kirby spa_close(spa, FTAG); 3861a7f53a56SChris Kirby return (error); 3862a7f53a56SChris Kirby } 3863a7f53a56SChris Kirby 3864a7f53a56SChris Kirby error = dmu_objset_from_ds(ds, &tosnap); 3865a7f53a56SChris Kirby if (error) { 3866a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 3867a7f53a56SChris Kirby spa_close(spa, FTAG); 3868a7f53a56SChris Kirby return (error); 3869a7f53a56SChris Kirby } 3870a7f53a56SChris Kirby 3871a7f53a56SChris Kirby if (zc->zc_fromobj != 0) { 3872a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 3873a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom); 3874a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 3875a7f53a56SChris Kirby spa_close(spa, FTAG); 3876a7f53a56SChris Kirby if (error) { 3877a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 3878a7f53a56SChris Kirby return (error); 3879a7f53a56SChris Kirby } 3880a7f53a56SChris Kirby error = dmu_objset_from_ds(dsfrom, &fromsnap); 3881fa9e4066Sahrens if (error) { 3882a7f53a56SChris Kirby dsl_dataset_rele(dsfrom, FTAG); 3883a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 3884fa9e4066Sahrens return (error); 3885fa9e4066Sahrens } 3886a7f53a56SChris Kirby } else { 3887a7f53a56SChris Kirby spa_close(spa, FTAG); 3888fa9e4066Sahrens } 3889fa9e4066Sahrens 389019b94df9SMatthew Ahrens if (estimate) { 389119b94df9SMatthew Ahrens error = dmu_send_estimate(tosnap, fromsnap, zc->zc_obj, 389219b94df9SMatthew Ahrens &zc->zc_objset_type); 389319b94df9SMatthew Ahrens } else { 389419b94df9SMatthew Ahrens file_t *fp = getf(zc->zc_cookie); 389519b94df9SMatthew Ahrens if (fp == NULL) { 389619b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 389719b94df9SMatthew Ahrens if (dsfrom) 389819b94df9SMatthew Ahrens dsl_dataset_rele(dsfrom, FTAG); 389919b94df9SMatthew Ahrens return (EBADF); 390019b94df9SMatthew Ahrens } 3901fa9e4066Sahrens 390219b94df9SMatthew Ahrens off = fp->f_offset; 39034e3c9f44SBill Pijewski error = dmu_send(tosnap, fromsnap, zc->zc_obj, 39044e3c9f44SBill Pijewski zc->zc_cookie, fp->f_vnode, &off); 3905fa9e4066Sahrens 390619b94df9SMatthew Ahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 390719b94df9SMatthew Ahrens fp->f_offset = off; 390819b94df9SMatthew Ahrens releasef(zc->zc_cookie); 390919b94df9SMatthew Ahrens } 3910a7f53a56SChris Kirby if (dsfrom) 3911a7f53a56SChris Kirby dsl_dataset_rele(dsfrom, FTAG); 3912a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 3913fa9e4066Sahrens return (error); 3914fa9e4066Sahrens } 3915fa9e4066Sahrens 39164e3c9f44SBill Pijewski /* 39174e3c9f44SBill Pijewski * inputs: 39184e3c9f44SBill Pijewski * zc_name name of snapshot on which to report progress 39194e3c9f44SBill Pijewski * zc_cookie file descriptor of send stream 39204e3c9f44SBill Pijewski * 39214e3c9f44SBill Pijewski * outputs: 39224e3c9f44SBill Pijewski * zc_cookie number of bytes written in send stream thus far 39234e3c9f44SBill Pijewski */ 39244e3c9f44SBill Pijewski static int 39254e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc) 39264e3c9f44SBill Pijewski { 39274e3c9f44SBill Pijewski dsl_dataset_t *ds; 39284e3c9f44SBill Pijewski dmu_sendarg_t *dsp = NULL; 39294e3c9f44SBill Pijewski int error; 39304e3c9f44SBill Pijewski 39314e3c9f44SBill Pijewski if ((error = dsl_dataset_hold(zc->zc_name, FTAG, &ds)) != 0) 39324e3c9f44SBill Pijewski return (error); 39334e3c9f44SBill Pijewski 39344e3c9f44SBill Pijewski mutex_enter(&ds->ds_sendstream_lock); 39354e3c9f44SBill Pijewski 39364e3c9f44SBill Pijewski /* 39374e3c9f44SBill Pijewski * Iterate over all the send streams currently active on this dataset. 39384e3c9f44SBill Pijewski * If there's one which matches the specified file descriptor _and_ the 39394e3c9f44SBill Pijewski * stream was started by the current process, return the progress of 39404e3c9f44SBill Pijewski * that stream. 39414e3c9f44SBill Pijewski */ 39424e3c9f44SBill Pijewski for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL; 39434e3c9f44SBill Pijewski dsp = list_next(&ds->ds_sendstreams, dsp)) { 39444e3c9f44SBill Pijewski if (dsp->dsa_outfd == zc->zc_cookie && 39454e3c9f44SBill Pijewski dsp->dsa_proc == curproc) 39464e3c9f44SBill Pijewski break; 39474e3c9f44SBill Pijewski } 39484e3c9f44SBill Pijewski 39494e3c9f44SBill Pijewski if (dsp != NULL) 39504e3c9f44SBill Pijewski zc->zc_cookie = *(dsp->dsa_off); 39514e3c9f44SBill Pijewski else 39524e3c9f44SBill Pijewski error = ENOENT; 39534e3c9f44SBill Pijewski 39544e3c9f44SBill Pijewski mutex_exit(&ds->ds_sendstream_lock); 39554e3c9f44SBill Pijewski dsl_dataset_rele(ds, FTAG); 39564e3c9f44SBill Pijewski return (error); 39574e3c9f44SBill Pijewski } 39584e3c9f44SBill Pijewski 3959ea8dc4b6Seschrock static int 3960ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 3961ea8dc4b6Seschrock { 3962ea8dc4b6Seschrock int id, error; 3963ea8dc4b6Seschrock 3964ea8dc4b6Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 3965ea8dc4b6Seschrock &zc->zc_inject_record); 3966ea8dc4b6Seschrock 3967ea8dc4b6Seschrock if (error == 0) 3968ea8dc4b6Seschrock zc->zc_guid = (uint64_t)id; 3969ea8dc4b6Seschrock 3970ea8dc4b6Seschrock return (error); 3971ea8dc4b6Seschrock } 3972ea8dc4b6Seschrock 3973ea8dc4b6Seschrock static int 3974ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 3975ea8dc4b6Seschrock { 3976ea8dc4b6Seschrock return (zio_clear_fault((int)zc->zc_guid)); 3977ea8dc4b6Seschrock } 3978ea8dc4b6Seschrock 3979ea8dc4b6Seschrock static int 3980ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 3981ea8dc4b6Seschrock { 3982ea8dc4b6Seschrock int id = (int)zc->zc_guid; 3983ea8dc4b6Seschrock int error; 3984ea8dc4b6Seschrock 3985ea8dc4b6Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 3986ea8dc4b6Seschrock &zc->zc_inject_record); 3987ea8dc4b6Seschrock 3988ea8dc4b6Seschrock zc->zc_guid = id; 3989ea8dc4b6Seschrock 3990ea8dc4b6Seschrock return (error); 3991ea8dc4b6Seschrock } 3992ea8dc4b6Seschrock 3993ea8dc4b6Seschrock static int 3994ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 3995ea8dc4b6Seschrock { 3996ea8dc4b6Seschrock spa_t *spa; 3997ea8dc4b6Seschrock int error; 3998e9dbad6fSeschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 3999ea8dc4b6Seschrock 4000ea8dc4b6Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 4001ea8dc4b6Seschrock return (error); 4002ea8dc4b6Seschrock 4003e9dbad6fSeschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 4004ea8dc4b6Seschrock &count); 4005ea8dc4b6Seschrock if (error == 0) 4006e9dbad6fSeschrock zc->zc_nvlist_dst_size = count; 4007ea8dc4b6Seschrock else 4008e9dbad6fSeschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 4009ea8dc4b6Seschrock 4010ea8dc4b6Seschrock spa_close(spa, FTAG); 4011ea8dc4b6Seschrock 4012ea8dc4b6Seschrock return (error); 4013ea8dc4b6Seschrock } 4014ea8dc4b6Seschrock 4015ea8dc4b6Seschrock static int 4016ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 4017ea8dc4b6Seschrock { 4018ea8dc4b6Seschrock spa_t *spa; 4019ea8dc4b6Seschrock vdev_t *vd; 4020bb8b5132Sek int error; 4021ea8dc4b6Seschrock 4022b87f3af3Sperrin /* 4023b87f3af3Sperrin * On zpool clear we also fix up missing slogs 4024b87f3af3Sperrin */ 4025b87f3af3Sperrin mutex_enter(&spa_namespace_lock); 4026b87f3af3Sperrin spa = spa_lookup(zc->zc_name); 4027b87f3af3Sperrin if (spa == NULL) { 4028b87f3af3Sperrin mutex_exit(&spa_namespace_lock); 4029b87f3af3Sperrin return (EIO); 4030b87f3af3Sperrin } 4031b24ab676SJeff Bonwick if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 4032b87f3af3Sperrin /* we need to let spa_open/spa_load clear the chains */ 4033b24ab676SJeff Bonwick spa_set_log_state(spa, SPA_LOG_CLEAR); 4034b87f3af3Sperrin } 4035468c413aSTim Haley spa->spa_last_open_failed = 0; 4036b87f3af3Sperrin mutex_exit(&spa_namespace_lock); 4037b87f3af3Sperrin 4038c8ee1847SVictor Latushkin if (zc->zc_cookie & ZPOOL_NO_REWIND) { 4039468c413aSTim Haley error = spa_open(zc->zc_name, &spa, FTAG); 4040468c413aSTim Haley } else { 4041468c413aSTim Haley nvlist_t *policy; 4042468c413aSTim Haley nvlist_t *config = NULL; 4043468c413aSTim Haley 4044468c413aSTim Haley if (zc->zc_nvlist_src == NULL) 4045468c413aSTim Haley return (EINVAL); 4046468c413aSTim Haley 4047468c413aSTim Haley if ((error = get_nvlist(zc->zc_nvlist_src, 4048468c413aSTim Haley zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 4049468c413aSTim Haley error = spa_open_rewind(zc->zc_name, &spa, FTAG, 4050468c413aSTim Haley policy, &config); 4051468c413aSTim Haley if (config != NULL) { 40524b964adaSGeorge Wilson int err; 40534b964adaSGeorge Wilson 40544b964adaSGeorge Wilson if ((err = put_nvlist(zc, config)) != 0) 40554b964adaSGeorge Wilson error = err; 4056468c413aSTim Haley nvlist_free(config); 4057468c413aSTim Haley } 4058468c413aSTim Haley nvlist_free(policy); 4059468c413aSTim Haley } 4060468c413aSTim Haley } 4061468c413aSTim Haley 4062468c413aSTim Haley if (error) 4063ea8dc4b6Seschrock return (error); 4064ea8dc4b6Seschrock 40658f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 4066ea8dc4b6Seschrock 4067e9dbad6fSeschrock if (zc->zc_guid == 0) { 4068ea8dc4b6Seschrock vd = NULL; 4069c5904d13Seschrock } else { 4070c5904d13Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 4071fa94a07fSbrendan if (vd == NULL) { 4072e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, ENODEV); 4073fa94a07fSbrendan spa_close(spa, FTAG); 4074fa94a07fSbrendan return (ENODEV); 4075fa94a07fSbrendan } 4076ea8dc4b6Seschrock } 4077ea8dc4b6Seschrock 4078e14bb325SJeff Bonwick vdev_clear(spa, vd); 4079e14bb325SJeff Bonwick 4080e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 4081ea8dc4b6Seschrock 4082e14bb325SJeff Bonwick /* 4083e14bb325SJeff Bonwick * Resume any suspended I/Os. 4084e14bb325SJeff Bonwick */ 408554d692b7SGeorge Wilson if (zio_resume(spa) != 0) 408654d692b7SGeorge Wilson error = EIO; 4087ea8dc4b6Seschrock 4088ea8dc4b6Seschrock spa_close(spa, FTAG); 4089ea8dc4b6Seschrock 409054d692b7SGeorge Wilson return (error); 4091ea8dc4b6Seschrock } 4092ea8dc4b6Seschrock 40934263d13fSGeorge Wilson static int 40944263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc) 40954263d13fSGeorge Wilson { 40964263d13fSGeorge Wilson spa_t *spa; 40974263d13fSGeorge Wilson int error; 40984263d13fSGeorge Wilson 40994263d13fSGeorge Wilson error = spa_open(zc->zc_name, &spa, FTAG); 41004263d13fSGeorge Wilson if (error) 41014263d13fSGeorge Wilson return (error); 41024263d13fSGeorge Wilson 41034263d13fSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 41044263d13fSGeorge Wilson vdev_reopen(spa->spa_root_vdev); 41054263d13fSGeorge Wilson (void) spa_vdev_state_exit(spa, NULL, 0); 41064263d13fSGeorge Wilson spa_close(spa, FTAG); 41074263d13fSGeorge Wilson return (0); 41084263d13fSGeorge Wilson } 41093cb34c60Sahrens /* 41103cb34c60Sahrens * inputs: 41113cb34c60Sahrens * zc_name name of filesystem 41123cb34c60Sahrens * zc_value name of origin snapshot 41133cb34c60Sahrens * 4114681d9761SEric Taylor * outputs: 4115681d9761SEric Taylor * zc_string name of conflicting snapshot, if there is one 41163cb34c60Sahrens */ 411799653d4eSeschrock static int 411899653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc) 411999653d4eSeschrock { 41200b69c2f0Sahrens char *cp; 41210b69c2f0Sahrens 41220b69c2f0Sahrens /* 41230b69c2f0Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 41240b69c2f0Sahrens * it's easier. 41250b69c2f0Sahrens */ 4126e9dbad6fSeschrock cp = strchr(zc->zc_value, '@'); 41270b69c2f0Sahrens if (cp) 41280b69c2f0Sahrens *cp = '\0'; 4129e9dbad6fSeschrock (void) dmu_objset_find(zc->zc_value, 41300b69c2f0Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 4131681d9761SEric Taylor return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 413299653d4eSeschrock } 413399653d4eSeschrock 413414843421SMatthew Ahrens /* 413514843421SMatthew Ahrens * Retrieve a single {user|group}{used|quota}@... property. 413614843421SMatthew Ahrens * 413714843421SMatthew Ahrens * inputs: 413814843421SMatthew Ahrens * zc_name name of filesystem 413914843421SMatthew Ahrens * zc_objset_type zfs_userquota_prop_t 414014843421SMatthew Ahrens * zc_value domain name (eg. "S-1-234-567-89") 414114843421SMatthew Ahrens * zc_guid RID/UID/GID 414214843421SMatthew Ahrens * 414314843421SMatthew Ahrens * outputs: 414414843421SMatthew Ahrens * zc_cookie property value 414514843421SMatthew Ahrens */ 414614843421SMatthew Ahrens static int 414714843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc) 414814843421SMatthew Ahrens { 414914843421SMatthew Ahrens zfsvfs_t *zfsvfs; 415014843421SMatthew Ahrens int error; 415114843421SMatthew Ahrens 415214843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 415314843421SMatthew Ahrens return (EINVAL); 415414843421SMatthew Ahrens 41551412a1a2SMark Shellenbaum error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 415614843421SMatthew Ahrens if (error) 415714843421SMatthew Ahrens return (error); 415814843421SMatthew Ahrens 415914843421SMatthew Ahrens error = zfs_userspace_one(zfsvfs, 416014843421SMatthew Ahrens zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 416114843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG); 416214843421SMatthew Ahrens 416314843421SMatthew Ahrens return (error); 416414843421SMatthew Ahrens } 416514843421SMatthew Ahrens 416614843421SMatthew Ahrens /* 416714843421SMatthew Ahrens * inputs: 416814843421SMatthew Ahrens * zc_name name of filesystem 416914843421SMatthew Ahrens * zc_cookie zap cursor 417014843421SMatthew Ahrens * zc_objset_type zfs_userquota_prop_t 417114843421SMatthew Ahrens * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 417214843421SMatthew Ahrens * 417314843421SMatthew Ahrens * outputs: 417414843421SMatthew Ahrens * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 417514843421SMatthew Ahrens * zc_cookie zap cursor 417614843421SMatthew Ahrens */ 417714843421SMatthew Ahrens static int 417814843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc) 417914843421SMatthew Ahrens { 418014843421SMatthew Ahrens zfsvfs_t *zfsvfs; 4181eeb85002STim Haley int bufsize = zc->zc_nvlist_dst_size; 418214843421SMatthew Ahrens 4183eeb85002STim Haley if (bufsize <= 0) 4184eeb85002STim Haley return (ENOMEM); 4185eeb85002STim Haley 41861412a1a2SMark Shellenbaum int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 418714843421SMatthew Ahrens if (error) 418814843421SMatthew Ahrens return (error); 418914843421SMatthew Ahrens 419014843421SMatthew Ahrens void *buf = kmem_alloc(bufsize, KM_SLEEP); 419114843421SMatthew Ahrens 419214843421SMatthew Ahrens error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 419314843421SMatthew Ahrens buf, &zc->zc_nvlist_dst_size); 419414843421SMatthew Ahrens 419514843421SMatthew Ahrens if (error == 0) { 419614843421SMatthew Ahrens error = xcopyout(buf, 419714843421SMatthew Ahrens (void *)(uintptr_t)zc->zc_nvlist_dst, 419814843421SMatthew Ahrens zc->zc_nvlist_dst_size); 419914843421SMatthew Ahrens } 420014843421SMatthew Ahrens kmem_free(buf, bufsize); 420114843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG); 420214843421SMatthew Ahrens 420314843421SMatthew Ahrens return (error); 420414843421SMatthew Ahrens } 420514843421SMatthew Ahrens 420614843421SMatthew Ahrens /* 420714843421SMatthew Ahrens * inputs: 420814843421SMatthew Ahrens * zc_name name of filesystem 420914843421SMatthew Ahrens * 421014843421SMatthew Ahrens * outputs: 421114843421SMatthew Ahrens * none 421214843421SMatthew Ahrens */ 421314843421SMatthew Ahrens static int 421414843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 421514843421SMatthew Ahrens { 421614843421SMatthew Ahrens objset_t *os; 42171195e687SMark J Musante int error = 0; 421814843421SMatthew Ahrens zfsvfs_t *zfsvfs; 421914843421SMatthew Ahrens 422014843421SMatthew Ahrens if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 4221503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 422214843421SMatthew Ahrens /* 422314843421SMatthew Ahrens * If userused is not enabled, it may be because the 422414843421SMatthew Ahrens * objset needs to be closed & reopened (to grow the 422514843421SMatthew Ahrens * objset_phys_t). Suspend/resume the fs will do that. 422614843421SMatthew Ahrens */ 4227503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs); 4228503ad85cSMatthew Ahrens if (error == 0) 4229503ad85cSMatthew Ahrens error = zfs_resume_fs(zfsvfs, zc->zc_name); 423014843421SMatthew Ahrens } 423114843421SMatthew Ahrens if (error == 0) 423214843421SMatthew Ahrens error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 423314843421SMatthew Ahrens VFS_RELE(zfsvfs->z_vfs); 423414843421SMatthew Ahrens } else { 4235503ad85cSMatthew Ahrens /* XXX kind of reading contents without owning */ 4236503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os); 423714843421SMatthew Ahrens if (error) 423814843421SMatthew Ahrens return (error); 423914843421SMatthew Ahrens 424014843421SMatthew Ahrens error = dmu_objset_userspace_upgrade(os); 4241503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 424214843421SMatthew Ahrens } 424314843421SMatthew Ahrens 424414843421SMatthew Ahrens return (error); 424514843421SMatthew Ahrens } 424614843421SMatthew Ahrens 4247ecd6cf80Smarks /* 4248ecd6cf80Smarks * We don't want to have a hard dependency 4249ecd6cf80Smarks * against some special symbols in sharefs 4250da6c28aaSamw * nfs, and smbsrv. Determine them if needed when 4251ecd6cf80Smarks * the first file system is shared. 4252da6c28aaSamw * Neither sharefs, nfs or smbsrv are unloadable modules. 4253ecd6cf80Smarks */ 4254da6c28aaSamw int (*znfsexport_fs)(void *arg); 4255ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 4256da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 4257da6c28aaSamw 4258da6c28aaSamw int zfs_nfsshare_inited; 4259da6c28aaSamw int zfs_smbshare_inited; 4260ecd6cf80Smarks 4261ecd6cf80Smarks ddi_modhandle_t nfs_mod; 4262ecd6cf80Smarks ddi_modhandle_t sharefs_mod; 4263da6c28aaSamw ddi_modhandle_t smbsrv_mod; 4264ecd6cf80Smarks kmutex_t zfs_share_lock; 4265ecd6cf80Smarks 4266da6c28aaSamw static int 4267da6c28aaSamw zfs_init_sharefs() 4268da6c28aaSamw { 4269da6c28aaSamw int error; 4270da6c28aaSamw 4271da6c28aaSamw ASSERT(MUTEX_HELD(&zfs_share_lock)); 4272da6c28aaSamw /* Both NFS and SMB shares also require sharetab support. */ 4273da6c28aaSamw if (sharefs_mod == NULL && ((sharefs_mod = 4274da6c28aaSamw ddi_modopen("fs/sharefs", 4275da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) { 4276da6c28aaSamw return (ENOSYS); 4277da6c28aaSamw } 4278da6c28aaSamw if (zshare_fs == NULL && ((zshare_fs = 4279da6c28aaSamw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 4280da6c28aaSamw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 4281da6c28aaSamw return (ENOSYS); 4282da6c28aaSamw } 4283da6c28aaSamw return (0); 4284da6c28aaSamw } 4285da6c28aaSamw 4286ecd6cf80Smarks static int 4287ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc) 4288ecd6cf80Smarks { 4289ecd6cf80Smarks int error; 4290ecd6cf80Smarks int opcode; 4291ecd6cf80Smarks 4292da6c28aaSamw switch (zc->zc_share.z_sharetype) { 4293da6c28aaSamw case ZFS_SHARE_NFS: 4294da6c28aaSamw case ZFS_UNSHARE_NFS: 4295da6c28aaSamw if (zfs_nfsshare_inited == 0) { 4296da6c28aaSamw mutex_enter(&zfs_share_lock); 4297da6c28aaSamw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 4298da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) { 4299da6c28aaSamw mutex_exit(&zfs_share_lock); 4300da6c28aaSamw return (ENOSYS); 4301da6c28aaSamw } 4302da6c28aaSamw if (znfsexport_fs == NULL && 4303da6c28aaSamw ((znfsexport_fs = (int (*)(void *)) 4304da6c28aaSamw ddi_modsym(nfs_mod, 4305da6c28aaSamw "nfs_export", &error)) == NULL)) { 4306da6c28aaSamw mutex_exit(&zfs_share_lock); 4307da6c28aaSamw return (ENOSYS); 4308da6c28aaSamw } 4309da6c28aaSamw error = zfs_init_sharefs(); 4310da6c28aaSamw if (error) { 4311da6c28aaSamw mutex_exit(&zfs_share_lock); 4312da6c28aaSamw return (ENOSYS); 4313da6c28aaSamw } 4314da6c28aaSamw zfs_nfsshare_inited = 1; 4315ecd6cf80Smarks mutex_exit(&zfs_share_lock); 4316ecd6cf80Smarks } 4317da6c28aaSamw break; 4318da6c28aaSamw case ZFS_SHARE_SMB: 4319da6c28aaSamw case ZFS_UNSHARE_SMB: 4320da6c28aaSamw if (zfs_smbshare_inited == 0) { 4321da6c28aaSamw mutex_enter(&zfs_share_lock); 4322da6c28aaSamw if (smbsrv_mod == NULL && ((smbsrv_mod = 4323da6c28aaSamw ddi_modopen("drv/smbsrv", 4324da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) { 4325da6c28aaSamw mutex_exit(&zfs_share_lock); 4326da6c28aaSamw return (ENOSYS); 4327da6c28aaSamw } 4328da6c28aaSamw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 4329da6c28aaSamw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 4330faa1795aSjb "smb_server_share", &error)) == NULL)) { 4331da6c28aaSamw mutex_exit(&zfs_share_lock); 4332da6c28aaSamw return (ENOSYS); 4333da6c28aaSamw } 4334da6c28aaSamw error = zfs_init_sharefs(); 4335da6c28aaSamw if (error) { 4336da6c28aaSamw mutex_exit(&zfs_share_lock); 4337da6c28aaSamw return (ENOSYS); 4338da6c28aaSamw } 4339da6c28aaSamw zfs_smbshare_inited = 1; 4340ecd6cf80Smarks mutex_exit(&zfs_share_lock); 4341ecd6cf80Smarks } 4342da6c28aaSamw break; 4343da6c28aaSamw default: 4344da6c28aaSamw return (EINVAL); 4345da6c28aaSamw } 4346ecd6cf80Smarks 4347da6c28aaSamw switch (zc->zc_share.z_sharetype) { 4348da6c28aaSamw case ZFS_SHARE_NFS: 4349da6c28aaSamw case ZFS_UNSHARE_NFS: 4350da6c28aaSamw if (error = 4351da6c28aaSamw znfsexport_fs((void *) 4352da6c28aaSamw (uintptr_t)zc->zc_share.z_exportdata)) 4353da6c28aaSamw return (error); 4354da6c28aaSamw break; 4355da6c28aaSamw case ZFS_SHARE_SMB: 4356da6c28aaSamw case ZFS_UNSHARE_SMB: 4357da6c28aaSamw if (error = zsmbexport_fs((void *) 4358da6c28aaSamw (uintptr_t)zc->zc_share.z_exportdata, 4359da6c28aaSamw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 4360743a77edSAlan Wright B_TRUE: B_FALSE)) { 4361da6c28aaSamw return (error); 4362ecd6cf80Smarks } 4363da6c28aaSamw break; 4364ecd6cf80Smarks } 4365ecd6cf80Smarks 4366da6c28aaSamw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 4367da6c28aaSamw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 4368ecd6cf80Smarks SHAREFS_ADD : SHAREFS_REMOVE; 4369ecd6cf80Smarks 4370da6c28aaSamw /* 4371da6c28aaSamw * Add or remove share from sharetab 4372da6c28aaSamw */ 4373ecd6cf80Smarks error = zshare_fs(opcode, 4374ecd6cf80Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 4375ecd6cf80Smarks zc->zc_share.z_sharemax); 4376ecd6cf80Smarks 4377ecd6cf80Smarks return (error); 4378ecd6cf80Smarks 4379ecd6cf80Smarks } 4380ecd6cf80Smarks 4381743a77edSAlan Wright ace_t full_access[] = { 4382743a77edSAlan Wright {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 4383743a77edSAlan Wright }; 4384743a77edSAlan Wright 438599d5e173STim Haley /* 438699d5e173STim Haley * inputs: 438799d5e173STim Haley * zc_name name of containing filesystem 438899d5e173STim Haley * zc_obj object # beyond which we want next in-use object # 438999d5e173STim Haley * 439099d5e173STim Haley * outputs: 439199d5e173STim Haley * zc_obj next in-use object # 439299d5e173STim Haley */ 439399d5e173STim Haley static int 439499d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc) 439599d5e173STim Haley { 439699d5e173STim Haley objset_t *os = NULL; 439799d5e173STim Haley int error; 439899d5e173STim Haley 439999d5e173STim Haley error = dmu_objset_hold(zc->zc_name, FTAG, &os); 440099d5e173STim Haley if (error) 440199d5e173STim Haley return (error); 440299d5e173STim Haley 440399d5e173STim Haley error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 440499d5e173STim Haley os->os_dsl_dataset->ds_phys->ds_prev_snap_txg); 440599d5e173STim Haley 440699d5e173STim Haley dmu_objset_rele(os, FTAG); 440799d5e173STim Haley return (error); 440899d5e173STim Haley } 440999d5e173STim Haley 441099d5e173STim Haley /* 441199d5e173STim Haley * inputs: 441299d5e173STim Haley * zc_name name of filesystem 441399d5e173STim Haley * zc_value prefix name for snapshot 441499d5e173STim Haley * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 441599d5e173STim Haley * 441699d5e173STim Haley * outputs: 441799d5e173STim Haley */ 441899d5e173STim Haley static int 441999d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc) 442099d5e173STim Haley { 442199d5e173STim Haley char *snap_name; 442299d5e173STim Haley int error; 442399d5e173STim Haley 442499d5e173STim Haley snap_name = kmem_asprintf("%s-%016llx", zc->zc_value, 442599d5e173STim Haley (u_longlong_t)ddi_get_lbolt64()); 442699d5e173STim Haley 442799d5e173STim Haley if (strlen(snap_name) >= MAXNAMELEN) { 442899d5e173STim Haley strfree(snap_name); 442999d5e173STim Haley return (E2BIG); 443099d5e173STim Haley } 443199d5e173STim Haley 443299d5e173STim Haley error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name, 443399d5e173STim Haley NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd); 443499d5e173STim Haley if (error != 0) { 443599d5e173STim Haley strfree(snap_name); 443699d5e173STim Haley return (error); 443799d5e173STim Haley } 443899d5e173STim Haley 443999d5e173STim Haley (void) strcpy(zc->zc_value, snap_name); 444099d5e173STim Haley strfree(snap_name); 444199d5e173STim Haley return (0); 444299d5e173STim Haley } 444399d5e173STim Haley 444499d5e173STim Haley /* 444599d5e173STim Haley * inputs: 444699d5e173STim Haley * zc_name name of "to" snapshot 444799d5e173STim Haley * zc_value name of "from" snapshot 444899d5e173STim Haley * zc_cookie file descriptor to write diff data on 444999d5e173STim Haley * 445099d5e173STim Haley * outputs: 445199d5e173STim Haley * dmu_diff_record_t's to the file descriptor 445299d5e173STim Haley */ 445399d5e173STim Haley static int 445499d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc) 445599d5e173STim Haley { 445699d5e173STim Haley objset_t *fromsnap; 445799d5e173STim Haley objset_t *tosnap; 445899d5e173STim Haley file_t *fp; 445999d5e173STim Haley offset_t off; 446099d5e173STim Haley int error; 446199d5e173STim Haley 446299d5e173STim Haley error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap); 446399d5e173STim Haley if (error) 446499d5e173STim Haley return (error); 446599d5e173STim Haley 446699d5e173STim Haley error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap); 446799d5e173STim Haley if (error) { 446899d5e173STim Haley dmu_objset_rele(tosnap, FTAG); 446999d5e173STim Haley return (error); 447099d5e173STim Haley } 447199d5e173STim Haley 447299d5e173STim Haley fp = getf(zc->zc_cookie); 447399d5e173STim Haley if (fp == NULL) { 447499d5e173STim Haley dmu_objset_rele(fromsnap, FTAG); 447599d5e173STim Haley dmu_objset_rele(tosnap, FTAG); 447699d5e173STim Haley return (EBADF); 447799d5e173STim Haley } 447899d5e173STim Haley 447999d5e173STim Haley off = fp->f_offset; 448099d5e173STim Haley 448199d5e173STim Haley error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off); 448299d5e173STim Haley 448399d5e173STim Haley if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 448499d5e173STim Haley fp->f_offset = off; 448599d5e173STim Haley releasef(zc->zc_cookie); 448699d5e173STim Haley 448799d5e173STim Haley dmu_objset_rele(fromsnap, FTAG); 448899d5e173STim Haley dmu_objset_rele(tosnap, FTAG); 448999d5e173STim Haley return (error); 449099d5e173STim Haley } 449199d5e173STim Haley 4492743a77edSAlan Wright /* 4493743a77edSAlan Wright * Remove all ACL files in shares dir 4494743a77edSAlan Wright */ 4495743a77edSAlan Wright static int 4496743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp) 4497743a77edSAlan Wright { 4498743a77edSAlan Wright zap_cursor_t zc; 4499743a77edSAlan Wright zap_attribute_t zap; 4500743a77edSAlan Wright zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 4501743a77edSAlan Wright int error; 4502743a77edSAlan Wright 4503743a77edSAlan Wright for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 4504743a77edSAlan Wright (error = zap_cursor_retrieve(&zc, &zap)) == 0; 4505743a77edSAlan Wright zap_cursor_advance(&zc)) { 4506743a77edSAlan Wright if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 4507743a77edSAlan Wright NULL, 0)) != 0) 4508743a77edSAlan Wright break; 4509743a77edSAlan Wright } 4510743a77edSAlan Wright zap_cursor_fini(&zc); 4511743a77edSAlan Wright return (error); 4512743a77edSAlan Wright } 4513743a77edSAlan Wright 4514743a77edSAlan Wright static int 4515743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc) 4516743a77edSAlan Wright { 4517743a77edSAlan Wright vnode_t *vp; 4518743a77edSAlan Wright znode_t *dzp; 4519743a77edSAlan Wright vnode_t *resourcevp = NULL; 4520743a77edSAlan Wright znode_t *sharedir; 4521743a77edSAlan Wright zfsvfs_t *zfsvfs; 4522743a77edSAlan Wright nvlist_t *nvlist; 4523743a77edSAlan Wright char *src, *target; 4524743a77edSAlan Wright vattr_t vattr; 4525743a77edSAlan Wright vsecattr_t vsec; 4526743a77edSAlan Wright int error = 0; 4527743a77edSAlan Wright 4528743a77edSAlan Wright if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 4529743a77edSAlan Wright NO_FOLLOW, NULL, &vp)) != 0) 4530743a77edSAlan Wright return (error); 4531743a77edSAlan Wright 4532743a77edSAlan Wright /* Now make sure mntpnt and dataset are ZFS */ 4533743a77edSAlan Wright 4534743a77edSAlan Wright if (vp->v_vfsp->vfs_fstype != zfsfstype || 4535743a77edSAlan Wright (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 4536743a77edSAlan Wright zc->zc_name) != 0)) { 4537743a77edSAlan Wright VN_RELE(vp); 4538743a77edSAlan Wright return (EINVAL); 4539743a77edSAlan Wright } 4540743a77edSAlan Wright 4541743a77edSAlan Wright dzp = VTOZ(vp); 4542743a77edSAlan Wright zfsvfs = dzp->z_zfsvfs; 4543743a77edSAlan Wright ZFS_ENTER(zfsvfs); 4544743a77edSAlan Wright 45459e1320c0SMark Shellenbaum /* 45469e1320c0SMark Shellenbaum * Create share dir if its missing. 45479e1320c0SMark Shellenbaum */ 45489e1320c0SMark Shellenbaum mutex_enter(&zfsvfs->z_lock); 45499e1320c0SMark Shellenbaum if (zfsvfs->z_shares_dir == 0) { 45509e1320c0SMark Shellenbaum dmu_tx_t *tx; 45519e1320c0SMark Shellenbaum 45529e1320c0SMark Shellenbaum tx = dmu_tx_create(zfsvfs->z_os); 45539e1320c0SMark Shellenbaum dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 45549e1320c0SMark Shellenbaum ZFS_SHARES_DIR); 45559e1320c0SMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 45569e1320c0SMark Shellenbaum error = dmu_tx_assign(tx, TXG_WAIT); 45579e1320c0SMark Shellenbaum if (error) { 45589e1320c0SMark Shellenbaum dmu_tx_abort(tx); 45599e1320c0SMark Shellenbaum } else { 45609e1320c0SMark Shellenbaum error = zfs_create_share_dir(zfsvfs, tx); 45619e1320c0SMark Shellenbaum dmu_tx_commit(tx); 45629e1320c0SMark Shellenbaum } 45639e1320c0SMark Shellenbaum if (error) { 45649e1320c0SMark Shellenbaum mutex_exit(&zfsvfs->z_lock); 45659e1320c0SMark Shellenbaum VN_RELE(vp); 45669e1320c0SMark Shellenbaum ZFS_EXIT(zfsvfs); 45679e1320c0SMark Shellenbaum return (error); 45689e1320c0SMark Shellenbaum } 45699e1320c0SMark Shellenbaum } 45709e1320c0SMark Shellenbaum mutex_exit(&zfsvfs->z_lock); 45719e1320c0SMark Shellenbaum 45729e1320c0SMark Shellenbaum ASSERT(zfsvfs->z_shares_dir); 4573743a77edSAlan Wright if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 45749e1320c0SMark Shellenbaum VN_RELE(vp); 4575743a77edSAlan Wright ZFS_EXIT(zfsvfs); 4576743a77edSAlan Wright return (error); 4577743a77edSAlan Wright } 4578743a77edSAlan Wright 4579743a77edSAlan Wright switch (zc->zc_cookie) { 4580743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 4581743a77edSAlan Wright vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 4582743a77edSAlan Wright vattr.va_type = VREG; 4583743a77edSAlan Wright vattr.va_mode = S_IFREG|0777; 4584743a77edSAlan Wright vattr.va_uid = 0; 4585743a77edSAlan Wright vattr.va_gid = 0; 4586743a77edSAlan Wright 4587743a77edSAlan Wright vsec.vsa_mask = VSA_ACE; 4588743a77edSAlan Wright vsec.vsa_aclentp = &full_access; 4589743a77edSAlan Wright vsec.vsa_aclentsz = sizeof (full_access); 4590743a77edSAlan Wright vsec.vsa_aclcnt = 1; 4591743a77edSAlan Wright 4592743a77edSAlan Wright error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 4593743a77edSAlan Wright &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 4594743a77edSAlan Wright if (resourcevp) 4595743a77edSAlan Wright VN_RELE(resourcevp); 4596743a77edSAlan Wright break; 4597743a77edSAlan Wright 4598743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 4599743a77edSAlan Wright error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 4600743a77edSAlan Wright NULL, 0); 4601743a77edSAlan Wright break; 4602743a77edSAlan Wright 4603743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 4604743a77edSAlan Wright if ((error = get_nvlist(zc->zc_nvlist_src, 4605478ed9adSEric Taylor zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 4606743a77edSAlan Wright VN_RELE(vp); 4607743a77edSAlan Wright ZFS_EXIT(zfsvfs); 4608743a77edSAlan Wright return (error); 4609743a77edSAlan Wright } 4610743a77edSAlan Wright if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 4611743a77edSAlan Wright nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 4612743a77edSAlan Wright &target)) { 4613743a77edSAlan Wright VN_RELE(vp); 461489459e17SMark Shellenbaum VN_RELE(ZTOV(sharedir)); 4615743a77edSAlan Wright ZFS_EXIT(zfsvfs); 46161195e687SMark J Musante nvlist_free(nvlist); 4617743a77edSAlan Wright return (error); 4618743a77edSAlan Wright } 4619743a77edSAlan Wright error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 4620743a77edSAlan Wright kcred, NULL, 0); 4621743a77edSAlan Wright nvlist_free(nvlist); 4622743a77edSAlan Wright break; 4623743a77edSAlan Wright 4624743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 4625743a77edSAlan Wright error = zfs_smb_acl_purge(sharedir); 4626743a77edSAlan Wright break; 4627743a77edSAlan Wright 4628743a77edSAlan Wright default: 4629743a77edSAlan Wright error = EINVAL; 4630743a77edSAlan Wright break; 4631743a77edSAlan Wright } 4632743a77edSAlan Wright 4633743a77edSAlan Wright VN_RELE(vp); 4634743a77edSAlan Wright VN_RELE(ZTOV(sharedir)); 4635743a77edSAlan Wright 4636743a77edSAlan Wright ZFS_EXIT(zfsvfs); 4637743a77edSAlan Wright 4638743a77edSAlan Wright return (error); 4639743a77edSAlan Wright } 4640743a77edSAlan Wright 4641842727c2SChris Kirby /* 4642842727c2SChris Kirby * inputs: 4643c99e4bdcSChris Kirby * zc_name name of filesystem 4644c99e4bdcSChris Kirby * zc_value short name of snap 4645c99e4bdcSChris Kirby * zc_string user-supplied tag for this hold 4646c99e4bdcSChris Kirby * zc_cookie recursive flag 4647c99e4bdcSChris Kirby * zc_temphold set if hold is temporary 4648c99e4bdcSChris Kirby * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 4649a7f53a56SChris Kirby * zc_sendobj if non-zero, the objid for zc_name@zc_value 4650a7f53a56SChris Kirby * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg 4651842727c2SChris Kirby * 4652842727c2SChris Kirby * outputs: none 4653842727c2SChris Kirby */ 4654842727c2SChris Kirby static int 4655842727c2SChris Kirby zfs_ioc_hold(zfs_cmd_t *zc) 4656842727c2SChris Kirby { 4657842727c2SChris Kirby boolean_t recursive = zc->zc_cookie; 4658a7f53a56SChris Kirby spa_t *spa; 4659a7f53a56SChris Kirby dsl_pool_t *dp; 4660a7f53a56SChris Kirby dsl_dataset_t *ds; 4661a7f53a56SChris Kirby int error; 4662a7f53a56SChris Kirby minor_t minor = 0; 4663842727c2SChris Kirby 4664842727c2SChris Kirby if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 4665842727c2SChris Kirby return (EINVAL); 4666842727c2SChris Kirby 4667a7f53a56SChris Kirby if (zc->zc_sendobj == 0) { 4668a7f53a56SChris Kirby return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value, 4669a7f53a56SChris Kirby zc->zc_string, recursive, zc->zc_temphold, 4670a7f53a56SChris Kirby zc->zc_cleanup_fd)); 4671a7f53a56SChris Kirby } 4672a7f53a56SChris Kirby 4673a7f53a56SChris Kirby if (recursive) 4674a7f53a56SChris Kirby return (EINVAL); 4675a7f53a56SChris Kirby 4676a7f53a56SChris Kirby error = spa_open(zc->zc_name, &spa, FTAG); 4677a7f53a56SChris Kirby if (error) 4678a7f53a56SChris Kirby return (error); 4679a7f53a56SChris Kirby 4680a7f53a56SChris Kirby dp = spa_get_dsl(spa); 4681a7f53a56SChris Kirby rw_enter(&dp->dp_config_rwlock, RW_READER); 4682a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 4683a7f53a56SChris Kirby rw_exit(&dp->dp_config_rwlock); 4684a7f53a56SChris Kirby spa_close(spa, FTAG); 4685a7f53a56SChris Kirby if (error) 4686a7f53a56SChris Kirby return (error); 4687a7f53a56SChris Kirby 4688a7f53a56SChris Kirby /* 4689a7f53a56SChris Kirby * Until we have a hold on this snapshot, it's possible that 4690a7f53a56SChris Kirby * zc_sendobj could've been destroyed and reused as part 4691a7f53a56SChris Kirby * of a later txg. Make sure we're looking at the right object. 4692a7f53a56SChris Kirby */ 4693a7f53a56SChris Kirby if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) { 4694a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 4695a7f53a56SChris Kirby return (ENOENT); 4696a7f53a56SChris Kirby } 4697a7f53a56SChris Kirby 4698a7f53a56SChris Kirby if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) { 4699a7f53a56SChris Kirby error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor); 4700a7f53a56SChris Kirby if (error) { 4701a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 4702a7f53a56SChris Kirby return (error); 4703a7f53a56SChris Kirby } 4704a7f53a56SChris Kirby } 4705a7f53a56SChris Kirby 4706a7f53a56SChris Kirby error = dsl_dataset_user_hold_for_send(ds, zc->zc_string, 4707a7f53a56SChris Kirby zc->zc_temphold); 4708a7f53a56SChris Kirby if (minor != 0) { 4709a7f53a56SChris Kirby if (error == 0) { 4710a7f53a56SChris Kirby dsl_register_onexit_hold_cleanup(ds, zc->zc_string, 4711a7f53a56SChris Kirby minor); 4712a7f53a56SChris Kirby } 4713a7f53a56SChris Kirby zfs_onexit_fd_rele(zc->zc_cleanup_fd); 4714a7f53a56SChris Kirby } 4715a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 4716a7f53a56SChris Kirby 4717a7f53a56SChris Kirby return (error); 4718842727c2SChris Kirby } 4719842727c2SChris Kirby 4720842727c2SChris Kirby /* 4721842727c2SChris Kirby * inputs: 4722c99e4bdcSChris Kirby * zc_name name of dataset from which we're releasing a user hold 4723842727c2SChris Kirby * zc_value short name of snap 4724c99e4bdcSChris Kirby * zc_string user-supplied tag for this hold 4725842727c2SChris Kirby * zc_cookie recursive flag 4726842727c2SChris Kirby * 4727c99e4bdcSChris Kirby * outputs: none 4728842727c2SChris Kirby */ 4729842727c2SChris Kirby static int 4730842727c2SChris Kirby zfs_ioc_release(zfs_cmd_t *zc) 4731842727c2SChris Kirby { 4732842727c2SChris Kirby boolean_t recursive = zc->zc_cookie; 4733842727c2SChris Kirby 4734842727c2SChris Kirby if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 4735842727c2SChris Kirby return (EINVAL); 4736842727c2SChris Kirby 4737842727c2SChris Kirby return (dsl_dataset_user_release(zc->zc_name, zc->zc_value, 4738842727c2SChris Kirby zc->zc_string, recursive)); 4739842727c2SChris Kirby } 4740842727c2SChris Kirby 4741842727c2SChris Kirby /* 4742842727c2SChris Kirby * inputs: 4743842727c2SChris Kirby * zc_name name of filesystem 4744842727c2SChris Kirby * 4745842727c2SChris Kirby * outputs: 4746842727c2SChris Kirby * zc_nvlist_src{_size} nvlist of snapshot holds 4747842727c2SChris Kirby */ 4748842727c2SChris Kirby static int 4749842727c2SChris Kirby zfs_ioc_get_holds(zfs_cmd_t *zc) 4750842727c2SChris Kirby { 4751842727c2SChris Kirby nvlist_t *nvp; 4752842727c2SChris Kirby int error; 4753842727c2SChris Kirby 4754842727c2SChris Kirby if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) { 4755842727c2SChris Kirby error = put_nvlist(zc, nvp); 4756842727c2SChris Kirby nvlist_free(nvp); 4757842727c2SChris Kirby } 4758842727c2SChris Kirby 4759842727c2SChris Kirby return (error); 4760842727c2SChris Kirby } 4761842727c2SChris Kirby 476219b94df9SMatthew Ahrens /* 476319b94df9SMatthew Ahrens * inputs: 476419b94df9SMatthew Ahrens * zc_name name of new filesystem or snapshot 476519b94df9SMatthew Ahrens * zc_value full name of old snapshot 476619b94df9SMatthew Ahrens * 476719b94df9SMatthew Ahrens * outputs: 476819b94df9SMatthew Ahrens * zc_cookie space in bytes 476919b94df9SMatthew Ahrens * zc_objset_type compressed space in bytes 477019b94df9SMatthew Ahrens * zc_perm_action uncompressed space in bytes 477119b94df9SMatthew Ahrens */ 477219b94df9SMatthew Ahrens static int 477319b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc) 477419b94df9SMatthew Ahrens { 477519b94df9SMatthew Ahrens int error; 477619b94df9SMatthew Ahrens dsl_dataset_t *new, *old; 477719b94df9SMatthew Ahrens 477819b94df9SMatthew Ahrens error = dsl_dataset_hold(zc->zc_name, FTAG, &new); 477919b94df9SMatthew Ahrens if (error != 0) 478019b94df9SMatthew Ahrens return (error); 478119b94df9SMatthew Ahrens error = dsl_dataset_hold(zc->zc_value, FTAG, &old); 478219b94df9SMatthew Ahrens if (error != 0) { 478319b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 478419b94df9SMatthew Ahrens return (error); 478519b94df9SMatthew Ahrens } 478619b94df9SMatthew Ahrens 478719b94df9SMatthew Ahrens error = dsl_dataset_space_written(old, new, &zc->zc_cookie, 478819b94df9SMatthew Ahrens &zc->zc_objset_type, &zc->zc_perm_action); 478919b94df9SMatthew Ahrens dsl_dataset_rele(old, FTAG); 479019b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 479119b94df9SMatthew Ahrens return (error); 479219b94df9SMatthew Ahrens } 479319b94df9SMatthew Ahrens 479419b94df9SMatthew Ahrens /* 479519b94df9SMatthew Ahrens * inputs: 479619b94df9SMatthew Ahrens * zc_name full name of last snapshot 479719b94df9SMatthew Ahrens * zc_value full name of first snapshot 479819b94df9SMatthew Ahrens * 479919b94df9SMatthew Ahrens * outputs: 480019b94df9SMatthew Ahrens * zc_cookie space in bytes 480119b94df9SMatthew Ahrens * zc_objset_type compressed space in bytes 480219b94df9SMatthew Ahrens * zc_perm_action uncompressed space in bytes 480319b94df9SMatthew Ahrens */ 480419b94df9SMatthew Ahrens static int 480519b94df9SMatthew Ahrens zfs_ioc_space_snaps(zfs_cmd_t *zc) 480619b94df9SMatthew Ahrens { 480719b94df9SMatthew Ahrens int error; 480819b94df9SMatthew Ahrens dsl_dataset_t *new, *old; 480919b94df9SMatthew Ahrens 481019b94df9SMatthew Ahrens error = dsl_dataset_hold(zc->zc_name, FTAG, &new); 481119b94df9SMatthew Ahrens if (error != 0) 481219b94df9SMatthew Ahrens return (error); 481319b94df9SMatthew Ahrens error = dsl_dataset_hold(zc->zc_value, FTAG, &old); 481419b94df9SMatthew Ahrens if (error != 0) { 481519b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 481619b94df9SMatthew Ahrens return (error); 481719b94df9SMatthew Ahrens } 481819b94df9SMatthew Ahrens 481919b94df9SMatthew Ahrens error = dsl_dataset_space_wouldfree(old, new, &zc->zc_cookie, 482019b94df9SMatthew Ahrens &zc->zc_objset_type, &zc->zc_perm_action); 482119b94df9SMatthew Ahrens dsl_dataset_rele(old, FTAG); 482219b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 482319b94df9SMatthew Ahrens return (error); 482419b94df9SMatthew Ahrens } 482519b94df9SMatthew Ahrens 4826ecd6cf80Smarks /* 48272a6b87f0Sek * pool create, destroy, and export don't log the history as part of 48282a6b87f0Sek * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 48292a6b87f0Sek * do the logging of those commands. 4830ecd6cf80Smarks */ 4831fa9e4066Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 483254d692b7SGeorge Wilson { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4833f9af39baSGeorge Wilson POOL_CHECK_NONE }, 483454d692b7SGeorge Wilson { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4835f9af39baSGeorge Wilson POOL_CHECK_NONE }, 483654d692b7SGeorge Wilson { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4837f9af39baSGeorge Wilson POOL_CHECK_NONE }, 483854d692b7SGeorge Wilson { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4839f9af39baSGeorge Wilson POOL_CHECK_NONE }, 484054d692b7SGeorge Wilson { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE, 4841f9af39baSGeorge Wilson POOL_CHECK_NONE }, 484254d692b7SGeorge Wilson { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE, 4843f9af39baSGeorge Wilson POOL_CHECK_NONE }, 484454d692b7SGeorge Wilson { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE, 4845f9af39baSGeorge Wilson POOL_CHECK_NONE }, 48463f9d6ad7SLin Ling { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4847f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 484854d692b7SGeorge Wilson { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE, 4849f9af39baSGeorge Wilson POOL_CHECK_READONLY }, 485054d692b7SGeorge Wilson { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4851f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 485254d692b7SGeorge Wilson { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4853f9af39baSGeorge Wilson POOL_CHECK_NONE }, 485454d692b7SGeorge Wilson { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4855f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 485654d692b7SGeorge Wilson { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4857f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 485854d692b7SGeorge Wilson { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4859f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 486054d692b7SGeorge Wilson { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4861f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 486254d692b7SGeorge Wilson { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4863f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 486454d692b7SGeorge Wilson { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4865f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 48666809eb4eSEric Schrock { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4867f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 486854d692b7SGeorge Wilson { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4869f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 487054d692b7SGeorge Wilson { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4871f9af39baSGeorge Wilson POOL_CHECK_NONE }, 487254d692b7SGeorge Wilson { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4873f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 487454d692b7SGeorge Wilson { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4875f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 4876f9af39baSGeorge Wilson { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, 4877f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4878f9af39baSGeorge Wilson { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, 4879f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 488054d692b7SGeorge Wilson { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 4881f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 488254d692b7SGeorge Wilson { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE, 4883f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4884f9af39baSGeorge Wilson { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, 4885f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4886f9af39baSGeorge Wilson { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, 4887f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 488819b94df9SMatthew Ahrens { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_FALSE, 4889f9af39baSGeorge Wilson POOL_CHECK_NONE }, 489054d692b7SGeorge Wilson { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 4891f9af39baSGeorge Wilson POOL_CHECK_NONE }, 489254d692b7SGeorge Wilson { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 4893f9af39baSGeorge Wilson POOL_CHECK_NONE }, 489454d692b7SGeorge Wilson { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE, 4895f9af39baSGeorge Wilson POOL_CHECK_NONE }, 489654d692b7SGeorge Wilson { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE, 4897f9af39baSGeorge Wilson POOL_CHECK_NONE }, 4898f9af39baSGeorge Wilson { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4899f9af39baSGeorge Wilson POOL_CHECK_NONE }, 490054d692b7SGeorge Wilson { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE, 4901f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 490254d692b7SGeorge Wilson { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE, 4903f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 490499d5e173STim Haley { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE, 4905f9af39baSGeorge Wilson POOL_CHECK_NONE }, 490699d5e173STim Haley { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, 4907f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 490854d692b7SGeorge Wilson { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4909f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 491054d692b7SGeorge Wilson { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE, 4911f9af39baSGeorge Wilson POOL_CHECK_NONE }, 491254d692b7SGeorge Wilson { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE, 4913f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 491454d692b7SGeorge Wilson { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4915f9af39baSGeorge Wilson POOL_CHECK_NONE }, 4916f9af39baSGeorge Wilson { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, 4917f9af39baSGeorge Wilson POOL_CHECK_NONE }, 491854d692b7SGeorge Wilson { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE, 4919f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 492054d692b7SGeorge Wilson { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE, 4921f9af39baSGeorge Wilson POOL_CHECK_NONE }, 4922f9af39baSGeorge Wilson { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME, 4923f9af39baSGeorge Wilson B_FALSE, POOL_CHECK_NONE }, 4924f9af39baSGeorge Wilson { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME, 4925f9af39baSGeorge Wilson B_FALSE, POOL_CHECK_NONE }, 492614843421SMatthew Ahrens { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 4927f9af39baSGeorge Wilson DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4928f9af39baSGeorge Wilson { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, 4929f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4930842727c2SChris Kirby { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE, 4931f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4932842727c2SChris Kirby { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4933f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED }, 493492241e0bSTom Erickson { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4935f9af39baSGeorge Wilson POOL_CHECK_NONE }, 49361195e687SMark J Musante { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4937f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 493899d5e173STim Haley { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4939f9af39baSGeorge Wilson POOL_CHECK_NONE }, 4940f9af39baSGeorge Wilson { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, 4941f9af39baSGeorge Wilson POOL_CHECK_NONE }, 494299d5e173STim Haley { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME, 4943f9af39baSGeorge Wilson B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 494499d5e173STim Haley { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, 4945e9103aaeSGarrett D'Amore POOL_CHECK_SUSPENDED }, 494619b94df9SMatthew Ahrens { zfs_ioc_space_written, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 494719b94df9SMatthew Ahrens POOL_CHECK_SUSPENDED }, 494819b94df9SMatthew Ahrens { zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 494919b94df9SMatthew Ahrens POOL_CHECK_SUSPENDED }, 495019b94df9SMatthew Ahrens { zfs_ioc_destroy_snaps_nvl, zfs_secpolicy_destroy_recursive, 495119b94df9SMatthew Ahrens DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 4952e9103aaeSGarrett D'Amore { zfs_ioc_pool_reguid, zfs_secpolicy_config, POOL_NAME, B_TRUE, 49534263d13fSGeorge Wilson POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }, 49544263d13fSGeorge Wilson { zfs_ioc_pool_reopen, zfs_secpolicy_config, POOL_NAME, B_TRUE, 49554263d13fSGeorge Wilson POOL_CHECK_SUSPENDED }, 49564e3c9f44SBill Pijewski { zfs_ioc_send_progress, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 49574e3c9f44SBill Pijewski POOL_CHECK_NONE } 4958fa9e4066Sahrens }; 4959fa9e4066Sahrens 496054d692b7SGeorge Wilson int 4961f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type, 4962f9af39baSGeorge Wilson zfs_ioc_poolcheck_t check) 496354d692b7SGeorge Wilson { 496454d692b7SGeorge Wilson spa_t *spa; 496554d692b7SGeorge Wilson int error; 496654d692b7SGeorge Wilson 496754d692b7SGeorge Wilson ASSERT(type == POOL_NAME || type == DATASET_NAME); 496854d692b7SGeorge Wilson 4969f9af39baSGeorge Wilson if (check & POOL_CHECK_NONE) 4970f9af39baSGeorge Wilson return (0); 4971f9af39baSGeorge Wilson 497214843421SMatthew Ahrens error = spa_open(name, &spa, FTAG); 497354d692b7SGeorge Wilson if (error == 0) { 4974f9af39baSGeorge Wilson if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa)) 497554d692b7SGeorge Wilson error = EAGAIN; 4976f9af39baSGeorge Wilson else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa)) 4977f9af39baSGeorge Wilson error = EROFS; 497854d692b7SGeorge Wilson spa_close(spa, FTAG); 497954d692b7SGeorge Wilson } 498054d692b7SGeorge Wilson return (error); 498154d692b7SGeorge Wilson } 498254d692b7SGeorge Wilson 4983c99e4bdcSChris Kirby /* 4984c99e4bdcSChris Kirby * Find a free minor number. 4985c99e4bdcSChris Kirby */ 4986c99e4bdcSChris Kirby minor_t 4987c99e4bdcSChris Kirby zfsdev_minor_alloc(void) 4988c99e4bdcSChris Kirby { 4989c99e4bdcSChris Kirby static minor_t last_minor; 4990c99e4bdcSChris Kirby minor_t m; 4991c99e4bdcSChris Kirby 4992c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 4993c99e4bdcSChris Kirby 4994c99e4bdcSChris Kirby for (m = last_minor + 1; m != last_minor; m++) { 4995c99e4bdcSChris Kirby if (m > ZFSDEV_MAX_MINOR) 4996c99e4bdcSChris Kirby m = 1; 4997c99e4bdcSChris Kirby if (ddi_get_soft_state(zfsdev_state, m) == NULL) { 4998c99e4bdcSChris Kirby last_minor = m; 4999c99e4bdcSChris Kirby return (m); 5000c99e4bdcSChris Kirby } 5001c99e4bdcSChris Kirby } 5002c99e4bdcSChris Kirby 5003c99e4bdcSChris Kirby return (0); 5004c99e4bdcSChris Kirby } 5005c99e4bdcSChris Kirby 5006c99e4bdcSChris Kirby static int 5007c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp) 5008c99e4bdcSChris Kirby { 5009c99e4bdcSChris Kirby minor_t minor; 5010c99e4bdcSChris Kirby zfs_soft_state_t *zs; 5011c99e4bdcSChris Kirby 5012c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 5013c99e4bdcSChris Kirby ASSERT(getminor(*devp) == 0); 5014c99e4bdcSChris Kirby 5015c99e4bdcSChris Kirby minor = zfsdev_minor_alloc(); 5016c99e4bdcSChris Kirby if (minor == 0) 5017c99e4bdcSChris Kirby return (ENXIO); 5018c99e4bdcSChris Kirby 5019c99e4bdcSChris Kirby if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) 5020c99e4bdcSChris Kirby return (EAGAIN); 5021c99e4bdcSChris Kirby 5022c99e4bdcSChris Kirby *devp = makedevice(getemajor(*devp), minor); 5023c99e4bdcSChris Kirby 5024c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, minor); 5025c99e4bdcSChris Kirby zs->zss_type = ZSST_CTLDEV; 5026c99e4bdcSChris Kirby zfs_onexit_init((zfs_onexit_t **)&zs->zss_data); 5027c99e4bdcSChris Kirby 5028c99e4bdcSChris Kirby return (0); 5029c99e4bdcSChris Kirby } 5030c99e4bdcSChris Kirby 5031c99e4bdcSChris Kirby static void 5032c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor) 5033c99e4bdcSChris Kirby { 5034c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 5035c99e4bdcSChris Kirby 5036c99e4bdcSChris Kirby zfs_onexit_destroy(zo); 5037c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5038c99e4bdcSChris Kirby } 5039c99e4bdcSChris Kirby 5040c99e4bdcSChris Kirby void * 5041c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which) 5042c99e4bdcSChris Kirby { 5043c99e4bdcSChris Kirby zfs_soft_state_t *zp; 5044c99e4bdcSChris Kirby 5045c99e4bdcSChris Kirby zp = ddi_get_soft_state(zfsdev_state, minor); 5046c99e4bdcSChris Kirby if (zp == NULL || zp->zss_type != which) 5047c99e4bdcSChris Kirby return (NULL); 5048c99e4bdcSChris Kirby 5049c99e4bdcSChris Kirby return (zp->zss_data); 5050c99e4bdcSChris Kirby } 5051c99e4bdcSChris Kirby 5052c99e4bdcSChris Kirby static int 5053c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr) 5054c99e4bdcSChris Kirby { 5055c99e4bdcSChris Kirby int error = 0; 5056c99e4bdcSChris Kirby 5057c99e4bdcSChris Kirby if (getminor(*devp) != 0) 5058c99e4bdcSChris Kirby return (zvol_open(devp, flag, otyp, cr)); 5059c99e4bdcSChris Kirby 5060c99e4bdcSChris Kirby /* This is the control device. Allocate a new minor if requested. */ 5061c99e4bdcSChris Kirby if (flag & FEXCL) { 5062c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 5063c99e4bdcSChris Kirby error = zfs_ctldev_init(devp); 5064c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 5065c99e4bdcSChris Kirby } 5066c99e4bdcSChris Kirby 5067c99e4bdcSChris Kirby return (error); 5068c99e4bdcSChris Kirby } 5069c99e4bdcSChris Kirby 5070c99e4bdcSChris Kirby static int 5071c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr) 5072c99e4bdcSChris Kirby { 5073c99e4bdcSChris Kirby zfs_onexit_t *zo; 5074c99e4bdcSChris Kirby minor_t minor = getminor(dev); 5075c99e4bdcSChris Kirby 5076c99e4bdcSChris Kirby if (minor == 0) 5077c99e4bdcSChris Kirby return (0); 5078c99e4bdcSChris Kirby 5079c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 5080c99e4bdcSChris Kirby zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV); 5081c99e4bdcSChris Kirby if (zo == NULL) { 5082c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 5083c99e4bdcSChris Kirby return (zvol_close(dev, flag, otyp, cr)); 5084c99e4bdcSChris Kirby } 5085c99e4bdcSChris Kirby zfs_ctldev_destroy(zo, minor); 5086c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 5087c99e4bdcSChris Kirby 5088c99e4bdcSChris Kirby return (0); 5089c99e4bdcSChris Kirby } 5090c99e4bdcSChris Kirby 5091fa9e4066Sahrens static int 5092fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 5093fa9e4066Sahrens { 5094fa9e4066Sahrens zfs_cmd_t *zc; 5095fa9e4066Sahrens uint_t vec; 50961d452cf5Sahrens int error, rc; 5097c99e4bdcSChris Kirby minor_t minor = getminor(dev); 5098fa9e4066Sahrens 5099c99e4bdcSChris Kirby if (minor != 0 && 5100c99e4bdcSChris Kirby zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL) 5101fa9e4066Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 5102fa9e4066Sahrens 5103fa9e4066Sahrens vec = cmd - ZFS_IOC; 510491ebeef5Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 5105fa9e4066Sahrens 5106fa9e4066Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 5107fa9e4066Sahrens return (EINVAL); 5108fa9e4066Sahrens 5109fa9e4066Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 5110fa9e4066Sahrens 5111478ed9adSEric Taylor error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 51126e27f868SSam Falkner if (error != 0) 51136e27f868SSam Falkner error = EFAULT; 5114fa9e4066Sahrens 5115681d9761SEric Taylor if ((error == 0) && !(flag & FKIOCTL)) 5116ecd6cf80Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 5117fa9e4066Sahrens 5118fa9e4066Sahrens /* 5119fa9e4066Sahrens * Ensure that all pool/dataset names are valid before we pass down to 5120fa9e4066Sahrens * the lower layers. 5121fa9e4066Sahrens */ 5122fa9e4066Sahrens if (error == 0) { 5123fa9e4066Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 5124478ed9adSEric Taylor zc->zc_iflags = flag & FKIOCTL; 5125fa9e4066Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 5126e7437265Sahrens case POOL_NAME: 5127fa9e4066Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 5128fa9e4066Sahrens error = EINVAL; 5129f9af39baSGeorge Wilson error = pool_status_check(zc->zc_name, 5130f9af39baSGeorge Wilson zfs_ioc_vec[vec].zvec_namecheck, 5131f9af39baSGeorge Wilson zfs_ioc_vec[vec].zvec_pool_check); 5132fa9e4066Sahrens break; 5133fa9e4066Sahrens 5134e7437265Sahrens case DATASET_NAME: 5135fa9e4066Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 5136fa9e4066Sahrens error = EINVAL; 5137f9af39baSGeorge Wilson error = pool_status_check(zc->zc_name, 5138f9af39baSGeorge Wilson zfs_ioc_vec[vec].zvec_namecheck, 5139f9af39baSGeorge Wilson zfs_ioc_vec[vec].zvec_pool_check); 5140fa9e4066Sahrens break; 51415ad82045Snd 5142e7437265Sahrens case NO_NAME: 51435ad82045Snd break; 5144fa9e4066Sahrens } 5145fa9e4066Sahrens } 5146fa9e4066Sahrens 5147fa9e4066Sahrens if (error == 0) 5148fa9e4066Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 5149fa9e4066Sahrens 5150478ed9adSEric Taylor rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 5151ecd6cf80Smarks if (error == 0) { 51526e27f868SSam Falkner if (rc != 0) 51536e27f868SSam Falkner error = EFAULT; 515414843421SMatthew Ahrens if (zfs_ioc_vec[vec].zvec_his_log) 5155ecd6cf80Smarks zfs_log_history(zc); 5156ecd6cf80Smarks } 5157fa9e4066Sahrens 5158fa9e4066Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 5159fa9e4066Sahrens return (error); 5160fa9e4066Sahrens } 5161fa9e4066Sahrens 5162fa9e4066Sahrens static int 5163fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 5164fa9e4066Sahrens { 5165fa9e4066Sahrens if (cmd != DDI_ATTACH) 5166fa9e4066Sahrens return (DDI_FAILURE); 5167fa9e4066Sahrens 5168fa9e4066Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 5169fa9e4066Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 5170fa9e4066Sahrens return (DDI_FAILURE); 5171fa9e4066Sahrens 5172fa9e4066Sahrens zfs_dip = dip; 5173fa9e4066Sahrens 5174fa9e4066Sahrens ddi_report_dev(dip); 5175fa9e4066Sahrens 5176fa9e4066Sahrens return (DDI_SUCCESS); 5177fa9e4066Sahrens } 5178fa9e4066Sahrens 5179fa9e4066Sahrens static int 5180fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 5181fa9e4066Sahrens { 5182fa9e4066Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 5183fa9e4066Sahrens return (DDI_FAILURE); 5184fa9e4066Sahrens 5185fa9e4066Sahrens if (cmd != DDI_DETACH) 5186fa9e4066Sahrens return (DDI_FAILURE); 5187fa9e4066Sahrens 5188fa9e4066Sahrens zfs_dip = NULL; 5189fa9e4066Sahrens 5190fa9e4066Sahrens ddi_prop_remove_all(dip); 5191fa9e4066Sahrens ddi_remove_minor_node(dip, NULL); 5192fa9e4066Sahrens 5193fa9e4066Sahrens return (DDI_SUCCESS); 5194fa9e4066Sahrens } 5195fa9e4066Sahrens 5196fa9e4066Sahrens /*ARGSUSED*/ 5197fa9e4066Sahrens static int 5198fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 5199fa9e4066Sahrens { 5200fa9e4066Sahrens switch (infocmd) { 5201fa9e4066Sahrens case DDI_INFO_DEVT2DEVINFO: 5202fa9e4066Sahrens *result = zfs_dip; 5203fa9e4066Sahrens return (DDI_SUCCESS); 5204fa9e4066Sahrens 5205fa9e4066Sahrens case DDI_INFO_DEVT2INSTANCE: 5206a0965f35Sbonwick *result = (void *)0; 5207fa9e4066Sahrens return (DDI_SUCCESS); 5208fa9e4066Sahrens } 5209fa9e4066Sahrens 5210fa9e4066Sahrens return (DDI_FAILURE); 5211fa9e4066Sahrens } 5212fa9e4066Sahrens 5213fa9e4066Sahrens /* 5214fa9e4066Sahrens * OK, so this is a little weird. 5215fa9e4066Sahrens * 5216fa9e4066Sahrens * /dev/zfs is the control node, i.e. minor 0. 5217fa9e4066Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 5218fa9e4066Sahrens * 5219fa9e4066Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 5220fa9e4066Sahrens * so most of the standard driver entry points are in zvol.c. 5221fa9e4066Sahrens */ 5222fa9e4066Sahrens static struct cb_ops zfs_cb_ops = { 5223c99e4bdcSChris Kirby zfsdev_open, /* open */ 5224c99e4bdcSChris Kirby zfsdev_close, /* close */ 5225fa9e4066Sahrens zvol_strategy, /* strategy */ 5226fa9e4066Sahrens nodev, /* print */ 5227e7cbe64fSgw zvol_dump, /* dump */ 5228fa9e4066Sahrens zvol_read, /* read */ 5229fa9e4066Sahrens zvol_write, /* write */ 5230fa9e4066Sahrens zfsdev_ioctl, /* ioctl */ 5231fa9e4066Sahrens nodev, /* devmap */ 5232fa9e4066Sahrens nodev, /* mmap */ 5233fa9e4066Sahrens nodev, /* segmap */ 5234fa9e4066Sahrens nochpoll, /* poll */ 5235fa9e4066Sahrens ddi_prop_op, /* prop_op */ 5236fa9e4066Sahrens NULL, /* streamtab */ 5237fa9e4066Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 5238fa9e4066Sahrens CB_REV, /* version */ 5239feb08c6bSbillm nodev, /* async read */ 5240feb08c6bSbillm nodev, /* async write */ 5241fa9e4066Sahrens }; 5242fa9e4066Sahrens 5243fa9e4066Sahrens static struct dev_ops zfs_dev_ops = { 5244fa9e4066Sahrens DEVO_REV, /* version */ 5245fa9e4066Sahrens 0, /* refcnt */ 5246fa9e4066Sahrens zfs_info, /* info */ 5247fa9e4066Sahrens nulldev, /* identify */ 5248fa9e4066Sahrens nulldev, /* probe */ 5249fa9e4066Sahrens zfs_attach, /* attach */ 5250fa9e4066Sahrens zfs_detach, /* detach */ 5251fa9e4066Sahrens nodev, /* reset */ 5252fa9e4066Sahrens &zfs_cb_ops, /* driver operations */ 525319397407SSherry Moore NULL, /* no bus operations */ 525419397407SSherry Moore NULL, /* power */ 525519397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 5256fa9e4066Sahrens }; 5257fa9e4066Sahrens 5258fa9e4066Sahrens static struct modldrv zfs_modldrv = { 525919397407SSherry Moore &mod_driverops, 526019397407SSherry Moore "ZFS storage pool", 526119397407SSherry Moore &zfs_dev_ops 5262fa9e4066Sahrens }; 5263fa9e4066Sahrens 5264fa9e4066Sahrens static struct modlinkage modlinkage = { 5265fa9e4066Sahrens MODREV_1, 5266fa9e4066Sahrens (void *)&zfs_modlfs, 5267fa9e4066Sahrens (void *)&zfs_modldrv, 5268fa9e4066Sahrens NULL 5269fa9e4066Sahrens }; 5270fa9e4066Sahrens 5271ec533521Sfr 5272ec533521Sfr uint_t zfs_fsyncer_key; 5273f18faf3fSek extern uint_t rrw_tsd_key; 5274ec533521Sfr 5275fa9e4066Sahrens int 5276fa9e4066Sahrens _init(void) 5277fa9e4066Sahrens { 5278fa9e4066Sahrens int error; 5279fa9e4066Sahrens 5280a0965f35Sbonwick spa_init(FREAD | FWRITE); 5281a0965f35Sbonwick zfs_init(); 5282a0965f35Sbonwick zvol_init(); 5283a0965f35Sbonwick 5284a0965f35Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 5285a0965f35Sbonwick zvol_fini(); 5286a0965f35Sbonwick zfs_fini(); 5287a0965f35Sbonwick spa_fini(); 5288fa9e4066Sahrens return (error); 5289a0965f35Sbonwick } 5290fa9e4066Sahrens 5291ec533521Sfr tsd_create(&zfs_fsyncer_key, NULL); 5292f18faf3fSek tsd_create(&rrw_tsd_key, NULL); 5293ec533521Sfr 5294fa9e4066Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 5295fa9e4066Sahrens ASSERT(error == 0); 5296ecd6cf80Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 5297fa9e4066Sahrens 5298fa9e4066Sahrens return (0); 5299fa9e4066Sahrens } 5300fa9e4066Sahrens 5301fa9e4066Sahrens int 5302fa9e4066Sahrens _fini(void) 5303fa9e4066Sahrens { 5304fa9e4066Sahrens int error; 5305fa9e4066Sahrens 5306ea8dc4b6Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 5307fa9e4066Sahrens return (EBUSY); 5308fa9e4066Sahrens 5309fa9e4066Sahrens if ((error = mod_remove(&modlinkage)) != 0) 5310fa9e4066Sahrens return (error); 5311fa9e4066Sahrens 5312fa9e4066Sahrens zvol_fini(); 5313fa9e4066Sahrens zfs_fini(); 5314fa9e4066Sahrens spa_fini(); 5315da6c28aaSamw if (zfs_nfsshare_inited) 5316ecd6cf80Smarks (void) ddi_modclose(nfs_mod); 5317da6c28aaSamw if (zfs_smbshare_inited) 5318da6c28aaSamw (void) ddi_modclose(smbsrv_mod); 5319da6c28aaSamw if (zfs_nfsshare_inited || zfs_smbshare_inited) 5320ecd6cf80Smarks (void) ddi_modclose(sharefs_mod); 5321fa9e4066Sahrens 5322ec533521Sfr tsd_destroy(&zfs_fsyncer_key); 5323fa9e4066Sahrens ldi_ident_release(zfs_li); 5324fa9e4066Sahrens zfs_li = NULL; 5325ecd6cf80Smarks mutex_destroy(&zfs_share_lock); 5326fa9e4066Sahrens 5327fa9e4066Sahrens return (error); 5328fa9e4066Sahrens } 5329fa9e4066Sahrens 5330fa9e4066Sahrens int 5331fa9e4066Sahrens _info(struct modinfo *modinfop) 5332fa9e4066Sahrens { 5333fa9e4066Sahrens return (mod_info(&modlinkage, modinfop)); 5334fa9e4066Sahrens } 5335