1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 225977Smarks * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/types.h> 29789Sahrens #include <sys/param.h> 30789Sahrens #include <sys/errno.h> 31789Sahrens #include <sys/uio.h> 32789Sahrens #include <sys/buf.h> 33789Sahrens #include <sys/modctl.h> 34789Sahrens #include <sys/open.h> 35789Sahrens #include <sys/file.h> 36789Sahrens #include <sys/kmem.h> 37789Sahrens #include <sys/conf.h> 38789Sahrens #include <sys/cmn_err.h> 39789Sahrens #include <sys/stat.h> 40789Sahrens #include <sys/zfs_ioctl.h> 415331Samw #include <sys/zfs_znode.h> 42789Sahrens #include <sys/zap.h> 43789Sahrens #include <sys/spa.h> 443912Slling #include <sys/spa_impl.h> 45789Sahrens #include <sys/vdev.h> 463912Slling #include <sys/vdev_impl.h> 47789Sahrens #include <sys/dmu.h> 48789Sahrens #include <sys/dsl_dir.h> 49789Sahrens #include <sys/dsl_dataset.h> 50789Sahrens #include <sys/dsl_prop.h> 514543Smarks #include <sys/dsl_deleg.h> 524543Smarks #include <sys/dmu_objset.h> 53789Sahrens #include <sys/ddi.h> 54789Sahrens #include <sys/sunddi.h> 55789Sahrens #include <sys/sunldi.h> 56789Sahrens #include <sys/policy.h> 57789Sahrens #include <sys/zone.h> 58789Sahrens #include <sys/nvpair.h> 59789Sahrens #include <sys/pathname.h> 60789Sahrens #include <sys/mount.h> 61789Sahrens #include <sys/sdt.h> 62789Sahrens #include <sys/fs/zfs.h> 63789Sahrens #include <sys/zfs_ctldir.h> 645331Samw #include <sys/zfs_dir.h> 652885Sahrens #include <sys/zvol.h> 664543Smarks #include <sharefs/share.h> 675326Sek110237 #include <sys/dmu_objset.h> 68789Sahrens 69789Sahrens #include "zfs_namecheck.h" 702676Seschrock #include "zfs_prop.h" 714543Smarks #include "zfs_deleg.h" 72789Sahrens 73789Sahrens extern struct modlfs zfs_modlfs; 74789Sahrens 75789Sahrens extern void zfs_init(void); 76789Sahrens extern void zfs_fini(void); 77789Sahrens 78789Sahrens ldi_ident_t zfs_li = NULL; 79789Sahrens dev_info_t *zfs_dip; 80789Sahrens 81789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *); 824543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 83789Sahrens 84789Sahrens typedef struct zfs_ioc_vec { 85789Sahrens zfs_ioc_func_t *zvec_func; 86789Sahrens zfs_secpolicy_func_t *zvec_secpolicy; 87789Sahrens enum { 884577Sahrens NO_NAME, 894577Sahrens POOL_NAME, 904577Sahrens DATASET_NAME 914543Smarks } zvec_namecheck; 924543Smarks boolean_t zvec_his_log; 93789Sahrens } zfs_ioc_vec_t; 94789Sahrens 95*7184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 96*7184Stimh boolean_t *); 97*7184Stimh int zfs_set_prop_nvlist(const char *, nvlist_t *); 98*7184Stimh 99789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 100789Sahrens void 101789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 102789Sahrens { 103789Sahrens const char *newfile; 104789Sahrens char buf[256]; 105789Sahrens va_list adx; 106789Sahrens 107789Sahrens /* 108789Sahrens * Get rid of annoying "../common/" prefix to filename. 109789Sahrens */ 110789Sahrens newfile = strrchr(file, '/'); 111789Sahrens if (newfile != NULL) { 112789Sahrens newfile = newfile + 1; /* Get rid of leading / */ 113789Sahrens } else { 114789Sahrens newfile = file; 115789Sahrens } 116789Sahrens 117789Sahrens va_start(adx, fmt); 118789Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, adx); 119789Sahrens va_end(adx); 120789Sahrens 121789Sahrens /* 122789Sahrens * To get this data, use the zfs-dprintf probe as so: 123789Sahrens * dtrace -q -n 'zfs-dprintf \ 124789Sahrens * /stringof(arg0) == "dbuf.c"/ \ 125789Sahrens * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 126789Sahrens * arg0 = file name 127789Sahrens * arg1 = function name 128789Sahrens * arg2 = line number 129789Sahrens * arg3 = message 130789Sahrens */ 131789Sahrens DTRACE_PROBE4(zfs__dprintf, 132789Sahrens char *, newfile, char *, func, int, line, char *, buf); 133789Sahrens } 134789Sahrens 1354543Smarks static void 1364715Sek110237 history_str_free(char *buf) 1374715Sek110237 { 1384715Sek110237 kmem_free(buf, HIS_MAX_RECORD_LEN); 1394715Sek110237 } 1404715Sek110237 1414715Sek110237 static char * 1424715Sek110237 history_str_get(zfs_cmd_t *zc) 1434715Sek110237 { 1444715Sek110237 char *buf; 1454715Sek110237 1464715Sek110237 if (zc->zc_history == NULL) 1474715Sek110237 return (NULL); 1484715Sek110237 1494715Sek110237 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 1504715Sek110237 if (copyinstr((void *)(uintptr_t)zc->zc_history, 1514715Sek110237 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 1524715Sek110237 history_str_free(buf); 1534715Sek110237 return (NULL); 1544715Sek110237 } 1554715Sek110237 1564715Sek110237 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 1574715Sek110237 1584715Sek110237 return (buf); 1594715Sek110237 } 1604715Sek110237 1615375Stimh /* 1627042Sgw25295 * Check to see if the named dataset is currently defined as bootable 1637042Sgw25295 */ 1647042Sgw25295 static boolean_t 1657042Sgw25295 zfs_is_bootfs(const char *name) 1667042Sgw25295 { 1677042Sgw25295 spa_t *spa; 1687042Sgw25295 boolean_t ret = B_FALSE; 1697042Sgw25295 1707042Sgw25295 if (spa_open(name, &spa, FTAG) == 0) { 1717042Sgw25295 if (spa->spa_bootfs) { 1727042Sgw25295 objset_t *os; 1737042Sgw25295 1747042Sgw25295 if (dmu_objset_open(name, DMU_OST_ZFS, 1757042Sgw25295 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 1767042Sgw25295 ret = (dmu_objset_id(os) == spa->spa_bootfs); 1777042Sgw25295 dmu_objset_close(os); 1787042Sgw25295 } 1797042Sgw25295 } 1807042Sgw25295 spa_close(spa, FTAG); 1817042Sgw25295 } 1827042Sgw25295 return (ret); 1837042Sgw25295 } 1847042Sgw25295 1857042Sgw25295 /* 186*7184Stimh * zfs_earlier_version 1875375Stimh * 1885375Stimh * Return non-zero if the spa version is less than requested version. 1895375Stimh */ 1905331Samw static int 191*7184Stimh zfs_earlier_version(const char *name, int version) 1925331Samw { 1935331Samw spa_t *spa; 1945331Samw 1955331Samw if (spa_open(name, &spa, FTAG) == 0) { 1965331Samw if (spa_version(spa) < version) { 1975331Samw spa_close(spa, FTAG); 1985331Samw return (1); 1995331Samw } 2005331Samw spa_close(spa, FTAG); 2015331Samw } 2025331Samw return (0); 2035331Samw } 2045331Samw 2055977Smarks /* 2066689Smaybee * zpl_earlier_version 2075977Smarks * 2086689Smaybee * Return TRUE if the ZPL version is less than requested version. 2095977Smarks */ 2106689Smaybee static boolean_t 2116689Smaybee zpl_earlier_version(const char *name, int version) 2125977Smarks { 2135977Smarks objset_t *os; 2146689Smaybee boolean_t rc = B_TRUE; 2155977Smarks 2165977Smarks if (dmu_objset_open(name, DMU_OST_ANY, 2176689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 2186689Smaybee uint64_t zplversion; 2196689Smaybee 2206689Smaybee if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 2216689Smaybee rc = zplversion < version; 2225977Smarks dmu_objset_close(os); 2235977Smarks } 2245977Smarks return (rc); 2255977Smarks } 2265977Smarks 2274715Sek110237 static void 2284543Smarks zfs_log_history(zfs_cmd_t *zc) 2294543Smarks { 2304543Smarks spa_t *spa; 2314603Sahrens char *buf; 2324543Smarks 2334715Sek110237 if ((buf = history_str_get(zc)) == NULL) 2344577Sahrens return; 2354577Sahrens 2364715Sek110237 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 2374715Sek110237 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 2384715Sek110237 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 2394715Sek110237 spa_close(spa, FTAG); 2404543Smarks } 2414715Sek110237 history_str_free(buf); 2424543Smarks } 2434543Smarks 244789Sahrens /* 245789Sahrens * Policy for top-level read operations (list pools). Requires no privileges, 246789Sahrens * and can be used in the local zone, as there is no associated dataset. 247789Sahrens */ 248789Sahrens /* ARGSUSED */ 249789Sahrens static int 2504543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 251789Sahrens { 252789Sahrens return (0); 253789Sahrens } 254789Sahrens 255789Sahrens /* 256789Sahrens * Policy for dataset read operations (list children, get statistics). Requires 257789Sahrens * no privileges, but must be visible in the local zone. 258789Sahrens */ 259789Sahrens /* ARGSUSED */ 260789Sahrens static int 2614543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 262789Sahrens { 263789Sahrens if (INGLOBALZONE(curproc) || 2644543Smarks zone_dataset_visible(zc->zc_name, NULL)) 265789Sahrens return (0); 266789Sahrens 267789Sahrens return (ENOENT); 268789Sahrens } 269789Sahrens 270789Sahrens static int 271789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr) 272789Sahrens { 273789Sahrens uint64_t zoned; 274789Sahrens int writable = 1; 275789Sahrens 276789Sahrens /* 277789Sahrens * The dataset must be visible by this zone -- check this first 278789Sahrens * so they don't see EPERM on something they shouldn't know about. 279789Sahrens */ 280789Sahrens if (!INGLOBALZONE(curproc) && 281789Sahrens !zone_dataset_visible(dataset, &writable)) 282789Sahrens return (ENOENT); 283789Sahrens 284789Sahrens if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 285789Sahrens return (ENOENT); 286789Sahrens 287789Sahrens if (INGLOBALZONE(curproc)) { 288789Sahrens /* 289789Sahrens * If the fs is zoned, only root can access it from the 290789Sahrens * global zone. 291789Sahrens */ 292789Sahrens if (secpolicy_zfs(cr) && zoned) 293789Sahrens return (EPERM); 294789Sahrens } else { 295789Sahrens /* 296789Sahrens * If we are in a local zone, the 'zoned' property must be set. 297789Sahrens */ 298789Sahrens if (!zoned) 299789Sahrens return (EPERM); 300789Sahrens 301789Sahrens /* must be writable by this zone */ 302789Sahrens if (!writable) 303789Sahrens return (EPERM); 304789Sahrens } 305789Sahrens return (0); 306789Sahrens } 307789Sahrens 308789Sahrens int 3094543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 310789Sahrens { 311789Sahrens int error; 312789Sahrens 3134543Smarks error = zfs_dozonecheck(name, cr); 3144543Smarks if (error == 0) { 3154543Smarks error = secpolicy_zfs(cr); 3164670Sahrens if (error) 3174543Smarks error = dsl_deleg_access(name, perm, cr); 3184543Smarks } 3194543Smarks return (error); 3204543Smarks } 3214543Smarks 3224543Smarks static int 3234543Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr) 3244543Smarks { 3254543Smarks /* 3264543Smarks * Check permissions for special properties. 3274543Smarks */ 3284543Smarks switch (prop) { 3294543Smarks case ZFS_PROP_ZONED: 3304543Smarks /* 3314543Smarks * Disallow setting of 'zoned' from within a local zone. 3324543Smarks */ 3334543Smarks if (!INGLOBALZONE(curproc)) 3344543Smarks return (EPERM); 3354543Smarks break; 336789Sahrens 3374543Smarks case ZFS_PROP_QUOTA: 3384543Smarks if (!INGLOBALZONE(curproc)) { 3394543Smarks uint64_t zoned; 3404543Smarks char setpoint[MAXNAMELEN]; 3414543Smarks /* 3424543Smarks * Unprivileged users are allowed to modify the 3434543Smarks * quota on things *under* (ie. contained by) 3444543Smarks * the thing they own. 3454543Smarks */ 3464543Smarks if (dsl_prop_get_integer(name, "zoned", &zoned, 3474543Smarks setpoint)) 3484543Smarks return (EPERM); 3494670Sahrens if (!zoned || strlen(name) <= strlen(setpoint)) 3504543Smarks return (EPERM); 3514543Smarks } 3524670Sahrens break; 3534543Smarks } 3544543Smarks 3554787Sahrens return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr)); 356789Sahrens } 357789Sahrens 3584543Smarks int 3594543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 3604543Smarks { 3614543Smarks int error; 3624543Smarks 3634543Smarks error = zfs_dozonecheck(zc->zc_name, cr); 3644543Smarks if (error) 3654543Smarks return (error); 3664543Smarks 3674543Smarks /* 3684543Smarks * permission to set permissions will be evaluated later in 3694543Smarks * dsl_deleg_can_allow() 3704543Smarks */ 3714543Smarks return (0); 3724543Smarks } 3734543Smarks 3744543Smarks int 3754543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 3764543Smarks { 3774543Smarks int error; 3784543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 3794543Smarks ZFS_DELEG_PERM_ROLLBACK, cr); 3804543Smarks if (error == 0) 3814543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 3824543Smarks ZFS_DELEG_PERM_MOUNT, cr); 3834543Smarks return (error); 3844543Smarks } 3854543Smarks 3864543Smarks int 3874543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 3884543Smarks { 3894543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 3904543Smarks ZFS_DELEG_PERM_SEND, cr)); 3914543Smarks } 3924543Smarks 3934543Smarks int 3944543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 3954543Smarks { 3964543Smarks if (!INGLOBALZONE(curproc)) 3974543Smarks return (EPERM); 3984543Smarks 3995367Sahrens if (secpolicy_nfs(cr) == 0) { 4004543Smarks return (0); 4014543Smarks } else { 4024543Smarks vnode_t *vp; 4034543Smarks int error; 4044543Smarks 4054543Smarks if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 4064543Smarks NO_FOLLOW, NULL, &vp)) != 0) 4074543Smarks return (error); 4084543Smarks 4094543Smarks /* Now make sure mntpnt and dataset are ZFS */ 4104543Smarks 4114543Smarks if (vp->v_vfsp->vfs_fstype != zfsfstype || 4124543Smarks (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 4134543Smarks zc->zc_name) != 0)) { 4144543Smarks VN_RELE(vp); 4154543Smarks return (EPERM); 4164543Smarks } 4174543Smarks 4184543Smarks VN_RELE(vp); 4194543Smarks return (dsl_deleg_access(zc->zc_name, 4204543Smarks ZFS_DELEG_PERM_SHARE, cr)); 4214543Smarks } 4224543Smarks } 4234543Smarks 424789Sahrens static int 4254543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize) 426789Sahrens { 427789Sahrens char *cp; 428789Sahrens 429789Sahrens /* 430789Sahrens * Remove the @bla or /bla from the end of the name to get the parent. 431789Sahrens */ 4324543Smarks (void) strncpy(parent, datasetname, parentsize); 4334543Smarks cp = strrchr(parent, '@'); 434789Sahrens if (cp != NULL) { 435789Sahrens cp[0] = '\0'; 436789Sahrens } else { 4374543Smarks cp = strrchr(parent, '/'); 438789Sahrens if (cp == NULL) 439789Sahrens return (ENOENT); 440789Sahrens cp[0] = '\0'; 441789Sahrens } 442789Sahrens 4434543Smarks return (0); 4444543Smarks } 4454543Smarks 4464543Smarks int 4474543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 4484543Smarks { 4494543Smarks int error; 4504543Smarks 4514543Smarks if ((error = zfs_secpolicy_write_perms(name, 4524543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 4534543Smarks return (error); 4544543Smarks 4554543Smarks return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 4564543Smarks } 4574543Smarks 4584543Smarks static int 4594543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 4604543Smarks { 4614543Smarks return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 4624543Smarks } 4634543Smarks 4644543Smarks /* 4654543Smarks * Must have sys_config privilege to check the iscsi permission 4664543Smarks */ 4674543Smarks /* ARGSUSED */ 4684543Smarks static int 4694543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr) 4704543Smarks { 4714543Smarks return (secpolicy_zfs(cr)); 4724543Smarks } 4734543Smarks 4744543Smarks int 4754543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 4764543Smarks { 4774543Smarks char parentname[MAXNAMELEN]; 4784543Smarks int error; 4794543Smarks 4804543Smarks if ((error = zfs_secpolicy_write_perms(from, 4814543Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0) 4824543Smarks return (error); 4834543Smarks 4844543Smarks if ((error = zfs_secpolicy_write_perms(from, 4854543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 4864543Smarks return (error); 4874543Smarks 4884543Smarks if ((error = zfs_get_parent(to, parentname, 4894543Smarks sizeof (parentname))) != 0) 4904543Smarks return (error); 4914543Smarks 4924543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 4934543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 4944543Smarks return (error); 4954543Smarks 4964543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 4974543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 4984543Smarks return (error); 4994543Smarks 5004543Smarks return (error); 5014543Smarks } 5024543Smarks 5034543Smarks static int 5044543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 5054543Smarks { 5064543Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 5074543Smarks } 5084543Smarks 5094543Smarks static int 5104543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 5114543Smarks { 5124543Smarks char parentname[MAXNAMELEN]; 5134543Smarks objset_t *clone; 5144543Smarks int error; 5154543Smarks 5164543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 5174543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 5184543Smarks if (error) 5194543Smarks return (error); 5204543Smarks 5214543Smarks error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 5226689Smaybee DS_MODE_USER | DS_MODE_READONLY, &clone); 5234543Smarks 5244543Smarks if (error == 0) { 5254543Smarks dsl_dataset_t *pclone = NULL; 5264543Smarks dsl_dir_t *dd; 5274543Smarks dd = clone->os->os_dsl_dataset->ds_dir; 5284543Smarks 5294543Smarks rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 5306689Smaybee error = dsl_dataset_hold_obj(dd->dd_pool, 5316689Smaybee dd->dd_phys->dd_origin_obj, FTAG, &pclone); 5324543Smarks rw_exit(&dd->dd_pool->dp_config_rwlock); 5334543Smarks if (error) { 5344543Smarks dmu_objset_close(clone); 5354543Smarks return (error); 5364543Smarks } 5374543Smarks 5384543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 5394543Smarks ZFS_DELEG_PERM_MOUNT, cr); 5404543Smarks 5414543Smarks dsl_dataset_name(pclone, parentname); 5424543Smarks dmu_objset_close(clone); 5436689Smaybee dsl_dataset_rele(pclone, FTAG); 5444543Smarks if (error == 0) 5454543Smarks error = zfs_secpolicy_write_perms(parentname, 5464543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 5474543Smarks } 5484543Smarks return (error); 5494543Smarks } 5504543Smarks 5514543Smarks static int 5524543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 5534543Smarks { 5544543Smarks int error; 5554543Smarks 5564543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 5574543Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 5584543Smarks return (error); 5594543Smarks 5604543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 5614543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 5624543Smarks return (error); 5634543Smarks 5644543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 5654543Smarks ZFS_DELEG_PERM_CREATE, cr)); 5664543Smarks } 5674543Smarks 5684543Smarks int 5694543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 5704543Smarks { 5714543Smarks int error; 5724543Smarks 5734543Smarks if ((error = zfs_secpolicy_write_perms(name, 5744543Smarks ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0) 5754543Smarks return (error); 5764543Smarks 5774543Smarks error = zfs_secpolicy_write_perms(name, 5784543Smarks ZFS_DELEG_PERM_MOUNT, cr); 5794543Smarks 5804543Smarks return (error); 5814543Smarks } 5824543Smarks 5834543Smarks static int 5844543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 5854543Smarks { 5864543Smarks 5874543Smarks return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 5884543Smarks } 5894543Smarks 5904543Smarks static int 5914543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 5924543Smarks { 5934543Smarks char parentname[MAXNAMELEN]; 5944543Smarks int error; 5954543Smarks 5964543Smarks if ((error = zfs_get_parent(zc->zc_name, parentname, 5974543Smarks sizeof (parentname))) != 0) 5984543Smarks return (error); 5994543Smarks 6004543Smarks if (zc->zc_value[0] != '\0') { 6014543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_value, 6024543Smarks ZFS_DELEG_PERM_CLONE, cr)) != 0) 6034543Smarks return (error); 6044543Smarks } 6054543Smarks 6064543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 6074543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 6084543Smarks return (error); 6094543Smarks 6104543Smarks error = zfs_secpolicy_write_perms(parentname, 6114543Smarks ZFS_DELEG_PERM_MOUNT, cr); 6124543Smarks 6134543Smarks return (error); 6144543Smarks } 6154543Smarks 6164543Smarks static int 6174543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 6184543Smarks { 6194543Smarks int error; 6204543Smarks 6214543Smarks error = secpolicy_fs_unmount(cr, NULL); 6224543Smarks if (error) { 6234543Smarks error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 6244543Smarks } 6254543Smarks return (error); 626789Sahrens } 627789Sahrens 628789Sahrens /* 629789Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 630789Sahrens * SYS_CONFIG privilege, which is not available in a local zone. 631789Sahrens */ 632789Sahrens /* ARGSUSED */ 633789Sahrens static int 6344543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 635789Sahrens { 636789Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0) 637789Sahrens return (EPERM); 638789Sahrens 639789Sahrens return (0); 640789Sahrens } 641789Sahrens 642789Sahrens /* 6434543Smarks * Just like zfs_secpolicy_config, except that we will check for 6444543Smarks * mount permission on the dataset for permission to create/remove 6454543Smarks * the minor nodes. 6464543Smarks */ 6474543Smarks static int 6484543Smarks zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr) 6494543Smarks { 6504543Smarks if (secpolicy_sys_config(cr, B_FALSE) != 0) { 6514543Smarks return (dsl_deleg_access(zc->zc_name, 6524543Smarks ZFS_DELEG_PERM_MOUNT, cr)); 6534543Smarks } 6544543Smarks 6554543Smarks return (0); 6564543Smarks } 6574543Smarks 6584543Smarks /* 6591544Seschrock * Policy for fault injection. Requires all privileges. 6601544Seschrock */ 6611544Seschrock /* ARGSUSED */ 6621544Seschrock static int 6634543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 6641544Seschrock { 6651544Seschrock return (secpolicy_zinject(cr)); 6661544Seschrock } 6671544Seschrock 6684849Sahrens static int 6694849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 6704849Sahrens { 6714849Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 6724849Sahrens 6735094Slling if (prop == ZPROP_INVAL) { 6744849Sahrens if (!zfs_prop_user(zc->zc_value)) 6754849Sahrens return (EINVAL); 6764849Sahrens return (zfs_secpolicy_write_perms(zc->zc_name, 6774849Sahrens ZFS_DELEG_PERM_USERPROP, cr)); 6784849Sahrens } else { 6794849Sahrens if (!zfs_prop_inheritable(prop)) 6804849Sahrens return (EINVAL); 6814849Sahrens return (zfs_secpolicy_setprop(zc->zc_name, prop, cr)); 6824849Sahrens } 6834849Sahrens } 6844849Sahrens 6851544Seschrock /* 686789Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t. 687789Sahrens */ 688789Sahrens static int 6895094Slling get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp) 690789Sahrens { 691789Sahrens char *packed; 692789Sahrens int error; 6935094Slling nvlist_t *list = NULL; 694789Sahrens 695789Sahrens /* 6962676Seschrock * Read in and unpack the user-supplied nvlist. 697789Sahrens */ 6985094Slling if (size == 0) 699789Sahrens return (EINVAL); 700789Sahrens 701789Sahrens packed = kmem_alloc(size, KM_SLEEP); 702789Sahrens 7035094Slling if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) { 704789Sahrens kmem_free(packed, size); 705789Sahrens return (error); 706789Sahrens } 707789Sahrens 7085094Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 709789Sahrens kmem_free(packed, size); 710789Sahrens return (error); 711789Sahrens } 712789Sahrens 713789Sahrens kmem_free(packed, size); 714789Sahrens 7155094Slling *nvp = list; 716789Sahrens return (0); 717789Sahrens } 718789Sahrens 719789Sahrens static int 7202676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 7212676Seschrock { 7222676Seschrock char *packed = NULL; 7232676Seschrock size_t size; 7242676Seschrock int error; 7252676Seschrock 7262676Seschrock VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 7272676Seschrock 7282676Seschrock if (size > zc->zc_nvlist_dst_size) { 7292676Seschrock error = ENOMEM; 7302676Seschrock } else { 7314611Smarks packed = kmem_alloc(size, KM_SLEEP); 7322676Seschrock VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 7332676Seschrock KM_SLEEP) == 0); 7342676Seschrock error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 7352676Seschrock size); 7362676Seschrock kmem_free(packed, size); 7372676Seschrock } 7382676Seschrock 7392676Seschrock zc->zc_nvlist_dst_size = size; 7402676Seschrock return (error); 7412676Seschrock } 7422676Seschrock 7432676Seschrock static int 744789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc) 745789Sahrens { 746789Sahrens int error; 7475094Slling nvlist_t *config, *props = NULL; 748*7184Stimh nvlist_t *rootprops = NULL; 749*7184Stimh nvlist_t *zplprops = NULL; 7504715Sek110237 char *buf; 751789Sahrens 7525094Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 7535094Slling &config)) 7544988Sek110237 return (error); 7554715Sek110237 7565094Slling if (zc->zc_nvlist_src_size != 0 && (error = 7575094Slling get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) { 7585094Slling nvlist_free(config); 7595094Slling return (error); 7605094Slling } 7615094Slling 762*7184Stimh if (props) { 763*7184Stimh nvlist_t *nvl = NULL; 764*7184Stimh uint64_t version = SPA_VERSION; 765*7184Stimh 766*7184Stimh (void) nvlist_lookup_uint64(props, 767*7184Stimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 768*7184Stimh if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 769*7184Stimh error = EINVAL; 770*7184Stimh goto pool_props_bad; 771*7184Stimh } 772*7184Stimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 773*7184Stimh if (nvl) { 774*7184Stimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 775*7184Stimh if (error != 0) { 776*7184Stimh nvlist_free(config); 777*7184Stimh nvlist_free(props); 778*7184Stimh return (error); 779*7184Stimh } 780*7184Stimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 781*7184Stimh } 782*7184Stimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 783*7184Stimh error = zfs_fill_zplprops_root(version, rootprops, 784*7184Stimh zplprops, NULL); 785*7184Stimh if (error) 786*7184Stimh goto pool_props_bad; 787*7184Stimh } 788*7184Stimh 7894988Sek110237 buf = history_str_get(zc); 790789Sahrens 791*7184Stimh error = spa_create(zc->zc_name, config, props, buf, zplprops); 792*7184Stimh 793*7184Stimh /* 794*7184Stimh * Set the remaining root properties 795*7184Stimh */ 796*7184Stimh if (!error && 797*7184Stimh (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0) 798*7184Stimh (void) spa_destroy(zc->zc_name); 799789Sahrens 8004988Sek110237 if (buf != NULL) 8014988Sek110237 history_str_free(buf); 8025094Slling 803*7184Stimh pool_props_bad: 804*7184Stimh nvlist_free(rootprops); 805*7184Stimh nvlist_free(zplprops); 806789Sahrens nvlist_free(config); 807*7184Stimh nvlist_free(props); 8085094Slling 809789Sahrens return (error); 810789Sahrens } 811789Sahrens 812789Sahrens static int 813789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc) 814789Sahrens { 8154543Smarks int error; 8164543Smarks zfs_log_history(zc); 8174543Smarks error = spa_destroy(zc->zc_name); 8184543Smarks return (error); 819789Sahrens } 820789Sahrens 821789Sahrens static int 822789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc) 823789Sahrens { 824789Sahrens int error; 8255094Slling nvlist_t *config, *props = NULL; 826789Sahrens uint64_t guid; 827789Sahrens 8285094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 8295094Slling &config)) != 0) 830789Sahrens return (error); 831789Sahrens 8325094Slling if (zc->zc_nvlist_src_size != 0 && (error = 8335094Slling get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) { 8345094Slling nvlist_free(config); 8355094Slling return (error); 8365094Slling } 8375094Slling 838789Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 8391544Seschrock guid != zc->zc_guid) 840789Sahrens error = EINVAL; 8416643Seschrock else if (zc->zc_cookie) 8426643Seschrock error = spa_import_faulted(zc->zc_name, config, 8436643Seschrock props); 844789Sahrens else 8455094Slling error = spa_import(zc->zc_name, config, props); 846789Sahrens 847789Sahrens nvlist_free(config); 848789Sahrens 8495094Slling if (props) 8505094Slling nvlist_free(props); 8515094Slling 852789Sahrens return (error); 853789Sahrens } 854789Sahrens 855789Sahrens static int 856789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc) 857789Sahrens { 8584543Smarks int error; 8594543Smarks zfs_log_history(zc); 8604543Smarks error = spa_export(zc->zc_name, NULL); 8614543Smarks return (error); 862789Sahrens } 863789Sahrens 864789Sahrens static int 865789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 866789Sahrens { 867789Sahrens nvlist_t *configs; 868789Sahrens int error; 869789Sahrens 870789Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 871789Sahrens return (EEXIST); 872789Sahrens 8732676Seschrock error = put_nvlist(zc, configs); 874789Sahrens 875789Sahrens nvlist_free(configs); 876789Sahrens 877789Sahrens return (error); 878789Sahrens } 879789Sahrens 880789Sahrens static int 881789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 882789Sahrens { 883789Sahrens nvlist_t *config; 884789Sahrens int error; 8851544Seschrock int ret = 0; 886789Sahrens 8872676Seschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 8882676Seschrock sizeof (zc->zc_value)); 889789Sahrens 890789Sahrens if (config != NULL) { 8912676Seschrock ret = put_nvlist(zc, config); 892789Sahrens nvlist_free(config); 8931544Seschrock 8941544Seschrock /* 8951544Seschrock * The config may be present even if 'error' is non-zero. 8961544Seschrock * In this case we return success, and preserve the real errno 8971544Seschrock * in 'zc_cookie'. 8981544Seschrock */ 8991544Seschrock zc->zc_cookie = error; 900789Sahrens } else { 9011544Seschrock ret = error; 902789Sahrens } 903789Sahrens 9041544Seschrock return (ret); 905789Sahrens } 906789Sahrens 907789Sahrens /* 908789Sahrens * Try to import the given pool, returning pool stats as appropriate so that 909789Sahrens * user land knows which devices are available and overall pool health. 910789Sahrens */ 911789Sahrens static int 912789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 913789Sahrens { 914789Sahrens nvlist_t *tryconfig, *config; 915789Sahrens int error; 916789Sahrens 9175094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 9185094Slling &tryconfig)) != 0) 919789Sahrens return (error); 920789Sahrens 921789Sahrens config = spa_tryimport(tryconfig); 922789Sahrens 923789Sahrens nvlist_free(tryconfig); 924789Sahrens 925789Sahrens if (config == NULL) 926789Sahrens return (EINVAL); 927789Sahrens 9282676Seschrock error = put_nvlist(zc, config); 929789Sahrens nvlist_free(config); 930789Sahrens 931789Sahrens return (error); 932789Sahrens } 933789Sahrens 934789Sahrens static int 935789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc) 936789Sahrens { 937789Sahrens spa_t *spa; 938789Sahrens int error; 939789Sahrens 9402926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 9412926Sek110237 return (error); 9422926Sek110237 9437046Sahrens error = spa_scrub(spa, zc->zc_cookie); 9442926Sek110237 9452926Sek110237 spa_close(spa, FTAG); 9462926Sek110237 947789Sahrens return (error); 948789Sahrens } 949789Sahrens 950789Sahrens static int 951789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 952789Sahrens { 953789Sahrens spa_t *spa; 954789Sahrens int error; 955789Sahrens 956789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 957789Sahrens if (error == 0) { 958789Sahrens spa_freeze(spa); 959789Sahrens spa_close(spa, FTAG); 960789Sahrens } 961789Sahrens return (error); 962789Sahrens } 963789Sahrens 964789Sahrens static int 9651760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 9661760Seschrock { 9671760Seschrock spa_t *spa; 9681760Seschrock int error; 9691760Seschrock 9702926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 9712926Sek110237 return (error); 9722926Sek110237 9735118Slling if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 9745118Slling spa_close(spa, FTAG); 9755118Slling return (EINVAL); 9765118Slling } 9775118Slling 9785094Slling spa_upgrade(spa, zc->zc_cookie); 9792926Sek110237 spa_close(spa, FTAG); 9802926Sek110237 9812926Sek110237 return (error); 9822926Sek110237 } 9832926Sek110237 9842926Sek110237 static int 9852926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 9862926Sek110237 { 9872926Sek110237 spa_t *spa; 9882926Sek110237 char *hist_buf; 9892926Sek110237 uint64_t size; 9902926Sek110237 int error; 9912926Sek110237 9922926Sek110237 if ((size = zc->zc_history_len) == 0) 9932926Sek110237 return (EINVAL); 9942926Sek110237 9952926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 9962926Sek110237 return (error); 9972926Sek110237 9984577Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 9993863Sek110237 spa_close(spa, FTAG); 10003863Sek110237 return (ENOTSUP); 10013863Sek110237 } 10023863Sek110237 10032926Sek110237 hist_buf = kmem_alloc(size, KM_SLEEP); 10042926Sek110237 if ((error = spa_history_get(spa, &zc->zc_history_offset, 10052926Sek110237 &zc->zc_history_len, hist_buf)) == 0) { 10064543Smarks error = xcopyout(hist_buf, 10074543Smarks (char *)(uintptr_t)zc->zc_history, 10082926Sek110237 zc->zc_history_len); 10092926Sek110237 } 10102926Sek110237 10112926Sek110237 spa_close(spa, FTAG); 10122926Sek110237 kmem_free(hist_buf, size); 10132926Sek110237 return (error); 10142926Sek110237 } 10152926Sek110237 10162926Sek110237 static int 10173444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 10183444Sek110237 { 10193444Sek110237 int error; 10203444Sek110237 10213912Slling if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 10223444Sek110237 return (error); 10233444Sek110237 10243444Sek110237 return (0); 10253444Sek110237 } 10263444Sek110237 10273444Sek110237 static int 10283444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 10293444Sek110237 { 10303444Sek110237 objset_t *osp; 10313444Sek110237 int error; 10323444Sek110237 10333444Sek110237 if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS, 10346689Smaybee DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0) 10353444Sek110237 return (error); 10363444Sek110237 error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value, 10373444Sek110237 sizeof (zc->zc_value)); 10383444Sek110237 dmu_objset_close(osp); 10393444Sek110237 10403444Sek110237 return (error); 10413444Sek110237 } 10423444Sek110237 10433444Sek110237 static int 1044789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1045789Sahrens { 1046789Sahrens spa_t *spa; 1047789Sahrens int error; 10486423Sgw25295 nvlist_t *config, **l2cache, **spares; 10496423Sgw25295 uint_t nl2cache = 0, nspares = 0; 1050789Sahrens 1051789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1052789Sahrens if (error != 0) 1053789Sahrens return (error); 1054789Sahrens 10555450Sbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 10565450Sbrendan &config); 10575450Sbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 10585450Sbrendan &l2cache, &nl2cache); 10595450Sbrendan 10606423Sgw25295 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 10616423Sgw25295 &spares, &nspares); 10626423Sgw25295 10633912Slling /* 10643912Slling * A root pool with concatenated devices is not supported. 10656423Sgw25295 * Thus, can not add a device to a root pool. 10666423Sgw25295 * 10676423Sgw25295 * Intent log device can not be added to a rootpool because 10686423Sgw25295 * during mountroot, zil is replayed, a seperated log device 10696423Sgw25295 * can not be accessed during the mountroot time. 10706423Sgw25295 * 10716423Sgw25295 * l2cache and spare devices are ok to be added to a rootpool. 10723912Slling */ 10736423Sgw25295 if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) { 10743912Slling spa_close(spa, FTAG); 10753912Slling return (EDOM); 10763912Slling } 10773912Slling 10785450Sbrendan if (error == 0) { 1079789Sahrens error = spa_vdev_add(spa, config); 1080789Sahrens nvlist_free(config); 1081789Sahrens } 1082789Sahrens spa_close(spa, FTAG); 1083789Sahrens return (error); 1084789Sahrens } 1085789Sahrens 1086789Sahrens static int 1087789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1088789Sahrens { 10892082Seschrock spa_t *spa; 10902082Seschrock int error; 10912082Seschrock 10922082Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 10932082Seschrock if (error != 0) 10942082Seschrock return (error); 10952082Seschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 10962082Seschrock spa_close(spa, FTAG); 10972082Seschrock return (error); 1098789Sahrens } 1099789Sahrens 1100789Sahrens static int 11014451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1102789Sahrens { 1103789Sahrens spa_t *spa; 1104789Sahrens int error; 11054451Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1106789Sahrens 11072926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1108789Sahrens return (error); 11094451Seschrock switch (zc->zc_cookie) { 11104451Seschrock case VDEV_STATE_ONLINE: 11114451Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 11124451Seschrock break; 11134451Seschrock 11144451Seschrock case VDEV_STATE_OFFLINE: 11154451Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 11164451Seschrock break; 1117789Sahrens 11184451Seschrock case VDEV_STATE_FAULTED: 11194451Seschrock error = vdev_fault(spa, zc->zc_guid); 11204451Seschrock break; 1121789Sahrens 11224451Seschrock case VDEV_STATE_DEGRADED: 11234451Seschrock error = vdev_degrade(spa, zc->zc_guid); 11244451Seschrock break; 11254451Seschrock 11264451Seschrock default: 11274451Seschrock error = EINVAL; 11284451Seschrock } 11294451Seschrock zc->zc_cookie = newstate; 1130789Sahrens spa_close(spa, FTAG); 1131789Sahrens return (error); 1132789Sahrens } 1133789Sahrens 1134789Sahrens static int 1135789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1136789Sahrens { 1137789Sahrens spa_t *spa; 1138789Sahrens int replacing = zc->zc_cookie; 1139789Sahrens nvlist_t *config; 1140789Sahrens int error; 1141789Sahrens 11422926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1143789Sahrens return (error); 1144789Sahrens 11455094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 11465094Slling &config)) == 0) { 11471544Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1148789Sahrens nvlist_free(config); 1149789Sahrens } 1150789Sahrens 1151789Sahrens spa_close(spa, FTAG); 1152789Sahrens return (error); 1153789Sahrens } 1154789Sahrens 1155789Sahrens static int 1156789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1157789Sahrens { 1158789Sahrens spa_t *spa; 1159789Sahrens int error; 1160789Sahrens 11612926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1162789Sahrens return (error); 1163789Sahrens 11641544Seschrock error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE); 1165789Sahrens 1166789Sahrens spa_close(spa, FTAG); 1167789Sahrens return (error); 1168789Sahrens } 1169789Sahrens 1170789Sahrens static int 11711354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 11721354Seschrock { 11731354Seschrock spa_t *spa; 11742676Seschrock char *path = zc->zc_value; 11751544Seschrock uint64_t guid = zc->zc_guid; 11761354Seschrock int error; 11771354Seschrock 11781354Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 11791354Seschrock if (error != 0) 11801354Seschrock return (error); 11811354Seschrock 11821354Seschrock error = spa_vdev_setpath(spa, guid, path); 11831354Seschrock spa_close(spa, FTAG); 11841354Seschrock return (error); 11851354Seschrock } 11861354Seschrock 11875367Sahrens /* 11885367Sahrens * inputs: 11895367Sahrens * zc_name name of filesystem 11905367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 11915367Sahrens * 11925367Sahrens * outputs: 11935367Sahrens * zc_objset_stats stats 11945367Sahrens * zc_nvlist_dst property nvlist 11955367Sahrens * zc_nvlist_dst_size size of property nvlist 11965367Sahrens */ 11971354Seschrock static int 1198789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc) 1199789Sahrens { 1200789Sahrens objset_t *os = NULL; 1201789Sahrens int error; 12021356Seschrock nvlist_t *nv; 1203789Sahrens 12046689Smaybee if (error = dmu_objset_open(zc->zc_name, 12056689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) 1206789Sahrens return (error); 1207789Sahrens 12082885Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1209789Sahrens 12102856Snd150628 if (zc->zc_nvlist_dst != 0 && 12116689Smaybee (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) { 12122885Sahrens dmu_objset_stats(os, nv); 12133087Sahrens /* 12145147Srm160521 * NB: zvol_get_stats() will read the objset contents, 12153087Sahrens * which we aren't supposed to do with a 12166689Smaybee * DS_MODE_USER hold, because it could be 12173087Sahrens * inconsistent. So this is a bit of a workaround... 12183087Sahrens */ 12194577Sahrens if (!zc->zc_objset_stats.dds_inconsistent) { 12204577Sahrens if (dmu_objset_type(os) == DMU_OST_ZVOL) 12214577Sahrens VERIFY(zvol_get_stats(os, nv) == 0); 12224577Sahrens } 12232676Seschrock error = put_nvlist(zc, nv); 12241356Seschrock nvlist_free(nv); 12251356Seschrock } 1226789Sahrens 1227789Sahrens dmu_objset_close(os); 1228789Sahrens return (error); 1229789Sahrens } 1230789Sahrens 12315498Stimh static int 12325498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 12335498Stimh { 12345498Stimh uint64_t value; 12355498Stimh int error; 12365498Stimh 12375498Stimh /* 12385498Stimh * zfs_get_zplprop() will either find a value or give us 12395498Stimh * the default value (if there is one). 12405498Stimh */ 12415498Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 12425498Stimh return (error); 12435498Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 12445498Stimh return (0); 12455498Stimh } 12465498Stimh 12475498Stimh /* 12485498Stimh * inputs: 12495498Stimh * zc_name name of filesystem 12505498Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 12515498Stimh * 12525498Stimh * outputs: 12535498Stimh * zc_nvlist_dst zpl property nvlist 12545498Stimh * zc_nvlist_dst_size size of zpl property nvlist 12555498Stimh */ 12565498Stimh static int 12575498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 12585498Stimh { 12595498Stimh objset_t *os; 12605498Stimh int err; 12615498Stimh 12626689Smaybee if (err = dmu_objset_open(zc->zc_name, 12636689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) 12645498Stimh return (err); 12655498Stimh 12665498Stimh dmu_objset_fast_stat(os, &zc->zc_objset_stats); 12675498Stimh 12685498Stimh /* 12695498Stimh * NB: nvl_add_zplprop() will read the objset contents, 12706689Smaybee * which we aren't supposed to do with a DS_MODE_USER 12716689Smaybee * hold, because it could be inconsistent. 12725498Stimh */ 12735498Stimh if (zc->zc_nvlist_dst != NULL && 12745498Stimh !zc->zc_objset_stats.dds_inconsistent && 12755498Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 12765498Stimh nvlist_t *nv; 12775498Stimh 12785498Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 12795498Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 12805498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 12815498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 12825498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 12835498Stimh err = put_nvlist(zc, nv); 12845498Stimh nvlist_free(nv); 12855498Stimh } else { 12865498Stimh err = ENOENT; 12875498Stimh } 12885498Stimh dmu_objset_close(os); 12895498Stimh return (err); 12905498Stimh } 12915498Stimh 12925367Sahrens /* 12935367Sahrens * inputs: 12945367Sahrens * zc_name name of filesystem 12955367Sahrens * zc_cookie zap cursor 12965367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 12975367Sahrens * 12985367Sahrens * outputs: 12995367Sahrens * zc_name name of next filesystem 13005367Sahrens * zc_objset_stats stats 13015367Sahrens * zc_nvlist_dst property nvlist 13025367Sahrens * zc_nvlist_dst_size size of property nvlist 13035367Sahrens */ 1304789Sahrens static int 1305789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1306789Sahrens { 1307885Sahrens objset_t *os; 1308789Sahrens int error; 1309789Sahrens char *p; 1310789Sahrens 13116689Smaybee if (error = dmu_objset_open(zc->zc_name, 13126689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) { 1313885Sahrens if (error == ENOENT) 1314885Sahrens error = ESRCH; 1315885Sahrens return (error); 1316789Sahrens } 1317789Sahrens 1318789Sahrens p = strrchr(zc->zc_name, '/'); 1319789Sahrens if (p == NULL || p[1] != '\0') 1320789Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1321789Sahrens p = zc->zc_name + strlen(zc->zc_name); 1322789Sahrens 1323789Sahrens do { 1324885Sahrens error = dmu_dir_list_next(os, 1325885Sahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 1326885Sahrens NULL, &zc->zc_cookie); 1327789Sahrens if (error == ENOENT) 1328789Sahrens error = ESRCH; 1329885Sahrens } while (error == 0 && !INGLOBALZONE(curproc) && 1330789Sahrens !zone_dataset_visible(zc->zc_name, NULL)); 13316689Smaybee dmu_objset_close(os); 1332789Sahrens 1333885Sahrens /* 1334885Sahrens * If it's a hidden dataset (ie. with a '$' in its name), don't 1335885Sahrens * try to get stats for it. Userland will skip over it. 1336885Sahrens */ 1337885Sahrens if (error == 0 && strchr(zc->zc_name, '$') == NULL) 1338885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1339789Sahrens 1340789Sahrens return (error); 1341789Sahrens } 1342789Sahrens 13435367Sahrens /* 13445367Sahrens * inputs: 13455367Sahrens * zc_name name of filesystem 13465367Sahrens * zc_cookie zap cursor 13475367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 13485367Sahrens * 13495367Sahrens * outputs: 13505367Sahrens * zc_name name of next snapshot 13515367Sahrens * zc_objset_stats stats 13525367Sahrens * zc_nvlist_dst property nvlist 13535367Sahrens * zc_nvlist_dst_size size of property nvlist 13545367Sahrens */ 1355789Sahrens static int 1356789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1357789Sahrens { 1358885Sahrens objset_t *os; 1359789Sahrens int error; 1360789Sahrens 13616689Smaybee error = dmu_objset_open(zc->zc_name, 13626689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os); 13636689Smaybee if (error) 13646689Smaybee return (error == ENOENT ? ESRCH : error); 1365789Sahrens 13661003Slling /* 13671003Slling * A dataset name of maximum length cannot have any snapshots, 13681003Slling * so exit immediately. 13691003Slling */ 13701003Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 1371885Sahrens dmu_objset_close(os); 13721003Slling return (ESRCH); 1373789Sahrens } 1374789Sahrens 1375885Sahrens error = dmu_snapshot_list_next(os, 1376885Sahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 13775663Sck153898 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 13786689Smaybee dmu_objset_close(os); 1379885Sahrens if (error == 0) 1380885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 13816689Smaybee else if (error == ENOENT) 13826689Smaybee error = ESRCH; 1383789Sahrens 13845367Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 13856689Smaybee if (error) 13865367Sahrens *strchr(zc->zc_name, '@') = '\0'; 1387789Sahrens return (error); 1388789Sahrens } 1389789Sahrens 13906423Sgw25295 int 13914787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl) 1392789Sahrens { 13932676Seschrock nvpair_t *elem; 13942676Seschrock int error; 13952676Seschrock uint64_t intval; 13962676Seschrock char *strval; 13972676Seschrock 13984543Smarks /* 13994543Smarks * First validate permission to set all of the properties 14004543Smarks */ 14012676Seschrock elem = NULL; 14022676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 14034670Sahrens const char *propname = nvpair_name(elem); 14044670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 14052676Seschrock 14065094Slling if (prop == ZPROP_INVAL) { 14072676Seschrock /* 14082676Seschrock * If this is a user-defined property, it must be a 14092676Seschrock * string, and there is no further validation to do. 14102676Seschrock */ 14112676Seschrock if (!zfs_prop_user(propname) || 14122676Seschrock nvpair_type(elem) != DATA_TYPE_STRING) 14132676Seschrock return (EINVAL); 14142676Seschrock 14155331Samw if (error = zfs_secpolicy_write_perms(name, 14165331Samw ZFS_DELEG_PERM_USERPROP, CRED())) 14174670Sahrens return (error); 14184543Smarks continue; 14192676Seschrock } 14202676Seschrock 14214787Sahrens if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 14224670Sahrens return (error); 14232676Seschrock 14244670Sahrens /* 14254670Sahrens * Check that this value is valid for this pool version 14264670Sahrens */ 14274670Sahrens switch (prop) { 14283886Sahl case ZFS_PROP_COMPRESSION: 14293886Sahl /* 14303886Sahl * If the user specified gzip compression, make sure 14313886Sahl * the SPA supports it. We ignore any errors here since 14323886Sahl * we'll catch them later. 14333886Sahl */ 14343886Sahl if (nvpair_type(elem) == DATA_TYPE_UINT64 && 14357042Sgw25295 nvpair_value_uint64(elem, &intval) == 0) { 14367042Sgw25295 if (intval >= ZIO_COMPRESS_GZIP_1 && 14377042Sgw25295 intval <= ZIO_COMPRESS_GZIP_9 && 1438*7184Stimh zfs_earlier_version(name, 14395331Samw SPA_VERSION_GZIP_COMPRESSION)) 14405331Samw return (ENOTSUP); 14417042Sgw25295 14427042Sgw25295 /* 14437042Sgw25295 * If this is a bootable dataset then 14447042Sgw25295 * verify that the compression algorithm 14457042Sgw25295 * is supported for booting. We must return 14467042Sgw25295 * something other than ENOTSUP since it 14477042Sgw25295 * implies a downrev pool version. 14487042Sgw25295 */ 14497042Sgw25295 if (zfs_is_bootfs(name) && 14507042Sgw25295 !BOOTFS_COMPRESS_VALID(intval)) 14517042Sgw25295 return (ERANGE); 14523886Sahl } 14533886Sahl break; 14544603Sahrens 14554603Sahrens case ZFS_PROP_COPIES: 1456*7184Stimh if (zfs_earlier_version(name, 1457*7184Stimh SPA_VERSION_DITTO_BLOCKS)) 14585331Samw return (ENOTSUP); 14594603Sahrens break; 14605977Smarks 14615977Smarks case ZFS_PROP_SHARESMB: 14626689Smaybee if (zpl_earlier_version(name, ZPL_VERSION_FUID)) 14635977Smarks return (ENOTSUP); 14645977Smarks break; 14654603Sahrens } 14665331Samw if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 14675331Samw return (error); 14684543Smarks } 14694543Smarks 14704543Smarks elem = NULL; 14714543Smarks while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 14724670Sahrens const char *propname = nvpair_name(elem); 14734670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 14744543Smarks 14755094Slling if (prop == ZPROP_INVAL) { 14764543Smarks VERIFY(nvpair_value_string(elem, &strval) == 0); 14774543Smarks error = dsl_prop_set(name, propname, 1, 14784543Smarks strlen(strval) + 1, strval); 14794543Smarks if (error == 0) 14804543Smarks continue; 14814543Smarks else 14824543Smarks return (error); 14834543Smarks } 14842676Seschrock 14852676Seschrock switch (prop) { 14862676Seschrock case ZFS_PROP_QUOTA: 14872676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 14884577Sahrens (error = dsl_dir_set_quota(name, intval)) != 0) 14892676Seschrock return (error); 14902676Seschrock break; 14912676Seschrock 14925378Sck153898 case ZFS_PROP_REFQUOTA: 14935378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 14945378Sck153898 (error = dsl_dataset_set_quota(name, intval)) != 0) 14955378Sck153898 return (error); 14965378Sck153898 break; 14975378Sck153898 14982676Seschrock case ZFS_PROP_RESERVATION: 14992676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15002676Seschrock (error = dsl_dir_set_reservation(name, 15012676Seschrock intval)) != 0) 15022676Seschrock return (error); 15032676Seschrock break; 1504789Sahrens 15055378Sck153898 case ZFS_PROP_REFRESERVATION: 15065378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15075378Sck153898 (error = dsl_dataset_set_reservation(name, 15085378Sck153898 intval)) != 0) 15095378Sck153898 return (error); 15105378Sck153898 break; 15115378Sck153898 15122676Seschrock case ZFS_PROP_VOLSIZE: 15132676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15144787Sahrens (error = zvol_set_volsize(name, 15154787Sahrens ddi_driver_major(zfs_dip), intval)) != 0) 15162676Seschrock return (error); 15172676Seschrock break; 15182676Seschrock 15192676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 15202676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15214577Sahrens (error = zvol_set_volblocksize(name, intval)) != 0) 15224577Sahrens return (error); 15234577Sahrens break; 15244577Sahrens 15254577Sahrens case ZFS_PROP_VERSION: 15264577Sahrens if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15274577Sahrens (error = zfs_set_version(name, intval)) != 0) 15282676Seschrock return (error); 15292676Seschrock break; 15302676Seschrock 15312676Seschrock default: 15322676Seschrock if (nvpair_type(elem) == DATA_TYPE_STRING) { 15332676Seschrock if (zfs_prop_get_type(prop) != 15344787Sahrens PROP_TYPE_STRING) 15352676Seschrock return (EINVAL); 15362717Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 15372717Seschrock if ((error = dsl_prop_set(name, 15382676Seschrock nvpair_name(elem), 1, strlen(strval) + 1, 15392717Seschrock strval)) != 0) 15402717Seschrock return (error); 15412676Seschrock } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 15422885Sahrens const char *unused; 15432885Sahrens 15442717Seschrock VERIFY(nvpair_value_uint64(elem, &intval) == 0); 15452676Seschrock 15462676Seschrock switch (zfs_prop_get_type(prop)) { 15474787Sahrens case PROP_TYPE_NUMBER: 15482676Seschrock break; 15494787Sahrens case PROP_TYPE_STRING: 15502717Seschrock return (EINVAL); 15514787Sahrens case PROP_TYPE_INDEX: 15522717Seschrock if (zfs_prop_index_to_string(prop, 15532717Seschrock intval, &unused) != 0) 15542717Seschrock return (EINVAL); 15552676Seschrock break; 15562676Seschrock default: 15574577Sahrens cmn_err(CE_PANIC, 15584577Sahrens "unknown property type"); 15592676Seschrock break; 15602676Seschrock } 15612676Seschrock 15622717Seschrock if ((error = dsl_prop_set(name, propname, 15632717Seschrock 8, 1, &intval)) != 0) 15642717Seschrock return (error); 15652676Seschrock } else { 15662676Seschrock return (EINVAL); 15672676Seschrock } 15682676Seschrock break; 15692676Seschrock } 15702676Seschrock } 15712676Seschrock 15722676Seschrock return (0); 1573789Sahrens } 1574789Sahrens 15755367Sahrens /* 15765367Sahrens * inputs: 15775367Sahrens * zc_name name of filesystem 15785367Sahrens * zc_value name of property to inherit 15795367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 15805367Sahrens * 15815367Sahrens * outputs: none 15825367Sahrens */ 1583789Sahrens static int 15842676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 1585789Sahrens { 15862676Seschrock nvlist_t *nvl; 15872676Seschrock int error; 1588789Sahrens 15895094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 15905094Slling &nvl)) != 0) 15912676Seschrock return (error); 15922676Seschrock 15934787Sahrens error = zfs_set_prop_nvlist(zc->zc_name, nvl); 15944543Smarks 15952676Seschrock nvlist_free(nvl); 15962676Seschrock return (error); 1597789Sahrens } 1598789Sahrens 15995367Sahrens /* 16005367Sahrens * inputs: 16015367Sahrens * zc_name name of filesystem 16025367Sahrens * zc_value name of property to inherit 16035367Sahrens * 16045367Sahrens * outputs: none 16055367Sahrens */ 1606789Sahrens static int 16074849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 16084849Sahrens { 16094849Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 16104849Sahrens return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL)); 16114849Sahrens } 16124849Sahrens 16134849Sahrens static int 16144098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 16153912Slling { 16165094Slling nvlist_t *props; 16173912Slling spa_t *spa; 16185094Slling int error; 16193912Slling 16205094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 16215094Slling &props))) 16223912Slling return (error); 16233912Slling 16243912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 16255094Slling nvlist_free(props); 16263912Slling return (error); 16273912Slling } 16283912Slling 16295094Slling error = spa_prop_set(spa, props); 16303912Slling 16315094Slling nvlist_free(props); 16323912Slling spa_close(spa, FTAG); 16333912Slling 16343912Slling return (error); 16353912Slling } 16363912Slling 16373912Slling static int 16384098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 16393912Slling { 16403912Slling spa_t *spa; 16413912Slling int error; 16423912Slling nvlist_t *nvp = NULL; 16433912Slling 16443912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 16453912Slling return (error); 16463912Slling 16475094Slling error = spa_prop_get(spa, &nvp); 16483912Slling 16493912Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 16503912Slling error = put_nvlist(zc, nvp); 16513912Slling else 16523912Slling error = EFAULT; 16533912Slling 16543912Slling spa_close(spa, FTAG); 16553912Slling 16563912Slling if (nvp) 16573912Slling nvlist_free(nvp); 16583912Slling return (error); 16593912Slling } 16603912Slling 16613912Slling static int 16624543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 16634543Smarks { 16644543Smarks nvlist_t *nvp; 16654543Smarks int error; 16664543Smarks uint32_t uid; 16674543Smarks uint32_t gid; 16684543Smarks uint32_t *groups; 16694543Smarks uint_t group_cnt; 16704543Smarks cred_t *usercred; 16714543Smarks 16725094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 16735094Slling &nvp)) != 0) { 16744543Smarks return (error); 16754543Smarks } 16764543Smarks 16774543Smarks if ((error = nvlist_lookup_uint32(nvp, 16784543Smarks ZFS_DELEG_PERM_UID, &uid)) != 0) { 16794543Smarks nvlist_free(nvp); 16804543Smarks return (EPERM); 16814543Smarks } 16824543Smarks 16834543Smarks if ((error = nvlist_lookup_uint32(nvp, 16844543Smarks ZFS_DELEG_PERM_GID, &gid)) != 0) { 16854543Smarks nvlist_free(nvp); 16864543Smarks return (EPERM); 16874543Smarks } 16884543Smarks 16894543Smarks if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 16904543Smarks &groups, &group_cnt)) != 0) { 16914543Smarks nvlist_free(nvp); 16924543Smarks return (EPERM); 16934543Smarks } 16944543Smarks usercred = cralloc(); 16954543Smarks if ((crsetugid(usercred, uid, gid) != 0) || 16964543Smarks (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 16974543Smarks nvlist_free(nvp); 16984543Smarks crfree(usercred); 16994543Smarks return (EPERM); 17004543Smarks } 17014543Smarks nvlist_free(nvp); 17024543Smarks error = dsl_deleg_access(zc->zc_name, 17034787Sahrens zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred); 17044543Smarks crfree(usercred); 17054543Smarks return (error); 17064543Smarks } 17074543Smarks 17085367Sahrens /* 17095367Sahrens * inputs: 17105367Sahrens * zc_name name of filesystem 17115367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 17125367Sahrens * zc_perm_action allow/unallow flag 17135367Sahrens * 17145367Sahrens * outputs: none 17155367Sahrens */ 17164543Smarks static int 17174543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 17184543Smarks { 17194543Smarks int error; 17204543Smarks nvlist_t *fsaclnv = NULL; 17214543Smarks 17225094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 17235094Slling &fsaclnv)) != 0) 17244543Smarks return (error); 17254543Smarks 17264543Smarks /* 17274543Smarks * Verify nvlist is constructed correctly 17284543Smarks */ 17294543Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 17304543Smarks nvlist_free(fsaclnv); 17314543Smarks return (EINVAL); 17324543Smarks } 17334543Smarks 17344543Smarks /* 17354543Smarks * If we don't have PRIV_SYS_MOUNT, then validate 17364543Smarks * that user is allowed to hand out each permission in 17374543Smarks * the nvlist(s) 17384543Smarks */ 17394543Smarks 17404787Sahrens error = secpolicy_zfs(CRED()); 17414543Smarks if (error) { 17424787Sahrens if (zc->zc_perm_action == B_FALSE) { 17434787Sahrens error = dsl_deleg_can_allow(zc->zc_name, 17444787Sahrens fsaclnv, CRED()); 17454787Sahrens } else { 17464787Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 17474787Sahrens fsaclnv, CRED()); 17484787Sahrens } 17494543Smarks } 17504543Smarks 17514543Smarks if (error == 0) 17524543Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 17534543Smarks 17544543Smarks nvlist_free(fsaclnv); 17554543Smarks return (error); 17564543Smarks } 17574543Smarks 17585367Sahrens /* 17595367Sahrens * inputs: 17605367Sahrens * zc_name name of filesystem 17615367Sahrens * 17625367Sahrens * outputs: 17635367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 17645367Sahrens */ 17654543Smarks static int 17664543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 17674543Smarks { 17684543Smarks nvlist_t *nvp; 17694543Smarks int error; 17704543Smarks 17714543Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 17724543Smarks error = put_nvlist(zc, nvp); 17734543Smarks nvlist_free(nvp); 17744543Smarks } 17754543Smarks 17764543Smarks return (error); 17774543Smarks } 17784543Smarks 17795367Sahrens /* 17805367Sahrens * inputs: 17815367Sahrens * zc_name name of volume 17825367Sahrens * 17835367Sahrens * outputs: none 17845367Sahrens */ 17854543Smarks static int 1786789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc) 1787789Sahrens { 17884787Sahrens return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip))); 1789789Sahrens } 1790789Sahrens 17915367Sahrens /* 17925367Sahrens * inputs: 17935367Sahrens * zc_name name of volume 17945367Sahrens * 17955367Sahrens * outputs: none 17965367Sahrens */ 1797789Sahrens static int 1798789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc) 1799789Sahrens { 18002676Seschrock return (zvol_remove_minor(zc->zc_name)); 1801789Sahrens } 1802789Sahrens 1803789Sahrens /* 1804789Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 1805789Sahrens * or NULL if no suitable entry is found. The caller of this routine 1806789Sahrens * is responsible for releasing the returned vfs pointer. 1807789Sahrens */ 1808789Sahrens static vfs_t * 1809789Sahrens zfs_get_vfs(const char *resource) 1810789Sahrens { 1811789Sahrens struct vfs *vfsp; 1812789Sahrens struct vfs *vfs_found = NULL; 1813789Sahrens 1814789Sahrens vfs_list_read_lock(); 1815789Sahrens vfsp = rootvfs; 1816789Sahrens do { 1817789Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 1818789Sahrens VFS_HOLD(vfsp); 1819789Sahrens vfs_found = vfsp; 1820789Sahrens break; 1821789Sahrens } 1822789Sahrens vfsp = vfsp->vfs_next; 1823789Sahrens } while (vfsp != rootvfs); 1824789Sahrens vfs_list_unlock(); 1825789Sahrens return (vfs_found); 1826789Sahrens } 1827789Sahrens 18284543Smarks /* ARGSUSED */ 1829789Sahrens static void 18304543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 1831789Sahrens { 18325331Samw zfs_creat_t *zct = arg; 18335498Stimh 18345498Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 18355331Samw } 18365331Samw 18375498Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 18385498Stimh 18395331Samw /* 18405498Stimh * inputs: 1841*7184Stimh * createprops list of properties requested by creator 1842*7184Stimh * default_zplver zpl version to use if unspecified in createprops 1843*7184Stimh * fuids_ok fuids allowed in this version of the spa? 1844*7184Stimh * os parent objset pointer (NULL if root fs) 18455331Samw * 18465498Stimh * outputs: 18475498Stimh * zplprops values for the zplprops we attach to the master node object 1848*7184Stimh * is_ci true if requested file system will be purely case-insensitive 18495331Samw * 18505498Stimh * Determine the settings for utf8only, normalization and 18515498Stimh * casesensitivity. Specific values may have been requested by the 18525498Stimh * creator and/or we can inherit values from the parent dataset. If 18535498Stimh * the file system is of too early a vintage, a creator can not 18545498Stimh * request settings for these properties, even if the requested 18555498Stimh * setting is the default value. We don't actually want to create dsl 18565498Stimh * properties for these, so remove them from the source nvlist after 18575498Stimh * processing. 18585331Samw */ 18595331Samw static int 1860*7184Stimh zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver, 1861*7184Stimh boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops, 1862*7184Stimh boolean_t *is_ci) 18635331Samw { 1864*7184Stimh uint64_t zplver = default_zplver; 18655498Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 18665498Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 18675498Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 18685498Stimh 18695498Stimh ASSERT(zplprops != NULL); 18705498Stimh 18715375Stimh /* 18725498Stimh * Pull out creator prop choices, if any. 18735375Stimh */ 18745498Stimh if (createprops) { 18755498Stimh (void) nvlist_lookup_uint64(createprops, 1876*7184Stimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 1877*7184Stimh (void) nvlist_lookup_uint64(createprops, 18785498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 18795498Stimh (void) nvlist_remove_all(createprops, 18805498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 18815498Stimh (void) nvlist_lookup_uint64(createprops, 18825498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 18835498Stimh (void) nvlist_remove_all(createprops, 18845498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 18855498Stimh (void) nvlist_lookup_uint64(createprops, 18865498Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 18875498Stimh (void) nvlist_remove_all(createprops, 18885498Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 18895331Samw } 18905331Samw 18915375Stimh /* 1892*7184Stimh * If the zpl version requested is whacky or the file system 1893*7184Stimh * or pool is version is too "young" to support normalization 1894*7184Stimh * and the creator tried to set a value for one of the props, 1895*7184Stimh * error out. 18965498Stimh */ 1897*7184Stimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 1898*7184Stimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 1899*7184Stimh (zplver < ZPL_VERSION_NORMALIZATION && 19005498Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 1901*7184Stimh sense != ZFS_PROP_UNDEFINED))) 19025498Stimh return (ENOTSUP); 19035498Stimh 19045498Stimh /* 19055498Stimh * Put the version in the zplprops 19065498Stimh */ 19075498Stimh VERIFY(nvlist_add_uint64(zplprops, 19085498Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 19095498Stimh 19105498Stimh if (norm == ZFS_PROP_UNDEFINED) 19115498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 19125498Stimh VERIFY(nvlist_add_uint64(zplprops, 19135498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 19145498Stimh 19155498Stimh /* 19165498Stimh * If we're normalizing, names must always be valid UTF-8 strings. 19175498Stimh */ 19185498Stimh if (norm) 19195498Stimh u8 = 1; 19205498Stimh if (u8 == ZFS_PROP_UNDEFINED) 19215498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 19225498Stimh VERIFY(nvlist_add_uint64(zplprops, 19235498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 19245498Stimh 19255498Stimh if (sense == ZFS_PROP_UNDEFINED) 19265498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 19275498Stimh VERIFY(nvlist_add_uint64(zplprops, 19285498Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 19295498Stimh 19306492Stimh if (is_ci) 19316492Stimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 19326492Stimh 1933*7184Stimh return (0); 1934*7184Stimh } 1935*7184Stimh 1936*7184Stimh static int 1937*7184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 1938*7184Stimh nvlist_t *zplprops, boolean_t *is_ci) 1939*7184Stimh { 1940*7184Stimh boolean_t fuids_ok = B_TRUE; 1941*7184Stimh uint64_t zplver = ZPL_VERSION; 1942*7184Stimh objset_t *os = NULL; 1943*7184Stimh char parentname[MAXNAMELEN]; 1944*7184Stimh char *cp; 1945*7184Stimh int error; 1946*7184Stimh 1947*7184Stimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 1948*7184Stimh cp = strrchr(parentname, '/'); 1949*7184Stimh ASSERT(cp != NULL); 1950*7184Stimh cp[0] = '\0'; 1951*7184Stimh 1952*7184Stimh if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) { 1953*7184Stimh zplver = ZPL_VERSION_FUID - 1; 1954*7184Stimh fuids_ok = B_FALSE; 1955*7184Stimh } 1956*7184Stimh 1957*7184Stimh /* 1958*7184Stimh * Open parent object set so we can inherit zplprop values. 1959*7184Stimh */ 1960*7184Stimh if ((error = dmu_objset_open(parentname, DMU_OST_ANY, 1961*7184Stimh DS_MODE_USER | DS_MODE_READONLY, &os)) != 0) 1962*7184Stimh return (error); 1963*7184Stimh 1964*7184Stimh error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops, 1965*7184Stimh zplprops, is_ci); 19665498Stimh dmu_objset_close(os); 1967*7184Stimh return (error); 1968*7184Stimh } 1969*7184Stimh 1970*7184Stimh static int 1971*7184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 1972*7184Stimh nvlist_t *zplprops, boolean_t *is_ci) 1973*7184Stimh { 1974*7184Stimh boolean_t fuids_ok = B_TRUE; 1975*7184Stimh uint64_t zplver = ZPL_VERSION; 1976*7184Stimh int error; 1977*7184Stimh 1978*7184Stimh if (spa_vers < SPA_VERSION_FUID) { 1979*7184Stimh zplver = ZPL_VERSION_FUID - 1; 1980*7184Stimh fuids_ok = B_FALSE; 1981*7184Stimh } 1982*7184Stimh 1983*7184Stimh error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops, 1984*7184Stimh zplprops, is_ci); 1985*7184Stimh return (error); 1986789Sahrens } 1987789Sahrens 19885367Sahrens /* 19895367Sahrens * inputs: 19905367Sahrens * zc_objset_type type of objset to create (fs vs zvol) 19915367Sahrens * zc_name name of new objset 19925367Sahrens * zc_value name of snapshot to clone from (may be empty) 19935367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 19945367Sahrens * 19955498Stimh * outputs: none 19965367Sahrens */ 1997789Sahrens static int 1998789Sahrens zfs_ioc_create(zfs_cmd_t *zc) 1999789Sahrens { 2000789Sahrens objset_t *clone; 2001789Sahrens int error = 0; 20025331Samw zfs_creat_t zct; 20034543Smarks nvlist_t *nvprops = NULL; 20044543Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2005789Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2006789Sahrens 2007789Sahrens switch (type) { 2008789Sahrens 2009789Sahrens case DMU_OST_ZFS: 2010789Sahrens cbfunc = zfs_create_cb; 2011789Sahrens break; 2012789Sahrens 2013789Sahrens case DMU_OST_ZVOL: 2014789Sahrens cbfunc = zvol_create_cb; 2015789Sahrens break; 2016789Sahrens 2017789Sahrens default: 20182199Sahrens cbfunc = NULL; 20196423Sgw25295 break; 20202199Sahrens } 20215326Sek110237 if (strchr(zc->zc_name, '@') || 20225326Sek110237 strchr(zc->zc_name, '%')) 2023789Sahrens return (EINVAL); 2024789Sahrens 20252676Seschrock if (zc->zc_nvlist_src != NULL && 20265094Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 20275094Slling &nvprops)) != 0) 20282676Seschrock return (error); 20292676Seschrock 20305498Stimh zct.zct_zplprops = NULL; 20315331Samw zct.zct_props = nvprops; 20325331Samw 20332676Seschrock if (zc->zc_value[0] != '\0') { 2034789Sahrens /* 2035789Sahrens * We're creating a clone of an existing snapshot. 2036789Sahrens */ 20372676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 20382676Seschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 20394543Smarks nvlist_free(nvprops); 2040789Sahrens return (EINVAL); 20412676Seschrock } 2042789Sahrens 20432676Seschrock error = dmu_objset_open(zc->zc_value, type, 20446689Smaybee DS_MODE_USER | DS_MODE_READONLY, &clone); 20452676Seschrock if (error) { 20464543Smarks nvlist_free(nvprops); 2047789Sahrens return (error); 20482676Seschrock } 20496492Stimh 20506492Stimh error = dmu_objset_create(zc->zc_name, type, clone, 0, 20516492Stimh NULL, NULL); 20525331Samw if (error) { 20535331Samw dmu_objset_close(clone); 20545331Samw nvlist_free(nvprops); 20555331Samw return (error); 20565331Samw } 2057789Sahrens dmu_objset_close(clone); 2058789Sahrens } else { 20596492Stimh boolean_t is_insensitive = B_FALSE; 20606492Stimh 20612676Seschrock if (cbfunc == NULL) { 20624543Smarks nvlist_free(nvprops); 20632199Sahrens return (EINVAL); 20642676Seschrock } 20652676Seschrock 2066789Sahrens if (type == DMU_OST_ZVOL) { 20672676Seschrock uint64_t volsize, volblocksize; 20682676Seschrock 20694543Smarks if (nvprops == NULL || 20704543Smarks nvlist_lookup_uint64(nvprops, 20712676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 20722676Seschrock &volsize) != 0) { 20734543Smarks nvlist_free(nvprops); 20742676Seschrock return (EINVAL); 20752676Seschrock } 20762676Seschrock 20774543Smarks if ((error = nvlist_lookup_uint64(nvprops, 20782676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 20792676Seschrock &volblocksize)) != 0 && error != ENOENT) { 20804543Smarks nvlist_free(nvprops); 20812676Seschrock return (EINVAL); 20822676Seschrock } 20831133Seschrock 20842676Seschrock if (error != 0) 20852676Seschrock volblocksize = zfs_prop_default_numeric( 20862676Seschrock ZFS_PROP_VOLBLOCKSIZE); 20872676Seschrock 20882676Seschrock if ((error = zvol_check_volblocksize( 20892676Seschrock volblocksize)) != 0 || 20902676Seschrock (error = zvol_check_volsize(volsize, 20912676Seschrock volblocksize)) != 0) { 20924543Smarks nvlist_free(nvprops); 2093789Sahrens return (error); 20942676Seschrock } 20954577Sahrens } else if (type == DMU_OST_ZFS) { 20965331Samw int error; 20975331Samw 20985498Stimh /* 20995331Samw * We have to have normalization and 21005331Samw * case-folding flags correct when we do the 21015331Samw * file system creation, so go figure them out 21025498Stimh * now. 21035331Samw */ 21045498Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 21055498Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 21065498Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 2107*7184Stimh zct.zct_zplprops, &is_insensitive); 21085331Samw if (error != 0) { 21095331Samw nvlist_free(nvprops); 21105498Stimh nvlist_free(zct.zct_zplprops); 21115331Samw return (error); 21124577Sahrens } 21132676Seschrock } 21146492Stimh error = dmu_objset_create(zc->zc_name, type, NULL, 21156492Stimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 21165498Stimh nvlist_free(zct.zct_zplprops); 2117789Sahrens } 21182676Seschrock 21192676Seschrock /* 21202676Seschrock * It would be nice to do this atomically. 21212676Seschrock */ 21222676Seschrock if (error == 0) { 21234787Sahrens if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0) 21242676Seschrock (void) dmu_objset_destroy(zc->zc_name); 21252676Seschrock } 21264543Smarks nvlist_free(nvprops); 2127789Sahrens return (error); 2128789Sahrens } 2129789Sahrens 21305367Sahrens /* 21315367Sahrens * inputs: 21325367Sahrens * zc_name name of filesystem 21335367Sahrens * zc_value short name of snapshot 21345367Sahrens * zc_cookie recursive flag 21355367Sahrens * 21365367Sahrens * outputs: none 21375367Sahrens */ 2138789Sahrens static int 21392199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 21402199Sahrens { 21412676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 21422199Sahrens return (EINVAL); 21432199Sahrens return (dmu_objset_snapshot(zc->zc_name, 21442676Seschrock zc->zc_value, zc->zc_cookie)); 21452199Sahrens } 21462199Sahrens 21474007Smmusante int 21482199Sahrens zfs_unmount_snap(char *name, void *arg) 2149789Sahrens { 21502417Sahrens vfs_t *vfsp = NULL; 21512199Sahrens 21526689Smaybee if (arg) { 21536689Smaybee char *snapname = arg; 21546689Smaybee int len = strlen(name) + strlen(snapname) + 2; 21556689Smaybee char *buf = kmem_alloc(len, KM_SLEEP); 21566689Smaybee 21576689Smaybee (void) strcpy(buf, name); 21586689Smaybee (void) strcat(buf, "@"); 21596689Smaybee (void) strcat(buf, snapname); 21606689Smaybee vfsp = zfs_get_vfs(buf); 21616689Smaybee kmem_free(buf, len); 21622417Sahrens } else if (strchr(name, '@')) { 21632199Sahrens vfsp = zfs_get_vfs(name); 21642199Sahrens } 21652199Sahrens 21662199Sahrens if (vfsp) { 21672199Sahrens /* 21682199Sahrens * Always force the unmount for snapshots. 21692199Sahrens */ 21702199Sahrens int flag = MS_FORCE; 2171789Sahrens int err; 2172789Sahrens 21732199Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 21742199Sahrens VFS_RELE(vfsp); 21752199Sahrens return (err); 21762199Sahrens } 21772199Sahrens VFS_RELE(vfsp); 21782199Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 21792199Sahrens return (err); 21802199Sahrens } 21812199Sahrens return (0); 21822199Sahrens } 21832199Sahrens 21845367Sahrens /* 21855367Sahrens * inputs: 21865367Sahrens * zc_name name of filesystem 21875367Sahrens * zc_value short name of snapshot 21885367Sahrens * 21895367Sahrens * outputs: none 21905367Sahrens */ 21912199Sahrens static int 21922199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 21932199Sahrens { 21942199Sahrens int err; 2195789Sahrens 21962676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 21972199Sahrens return (EINVAL); 21982199Sahrens err = dmu_objset_find(zc->zc_name, 21992676Seschrock zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 22002199Sahrens if (err) 22012199Sahrens return (err); 22022676Seschrock return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value)); 22032199Sahrens } 22042199Sahrens 22055367Sahrens /* 22065367Sahrens * inputs: 22075367Sahrens * zc_name name of dataset to destroy 22085367Sahrens * zc_objset_type type of objset 22095367Sahrens * 22105367Sahrens * outputs: none 22115367Sahrens */ 22122199Sahrens static int 22132199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 22142199Sahrens { 22152199Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 22162199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 22172199Sahrens if (err) 22182199Sahrens return (err); 2219789Sahrens } 2220789Sahrens 2221789Sahrens return (dmu_objset_destroy(zc->zc_name)); 2222789Sahrens } 2223789Sahrens 22245367Sahrens /* 22255367Sahrens * inputs: 22265446Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 22275367Sahrens * 22285367Sahrens * outputs: none 22295367Sahrens */ 2230789Sahrens static int 2231789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 2232789Sahrens { 22335446Sahrens objset_t *os; 22345446Sahrens int error; 22355446Sahrens zfsvfs_t *zfsvfs = NULL; 22365446Sahrens 22375446Sahrens /* 22385446Sahrens * Get the zfsvfs for the receiving objset. There 22395446Sahrens * won't be one if we're operating on a zvol, if the 22405446Sahrens * objset doesn't exist yet, or is not mounted. 22415446Sahrens */ 22426689Smaybee error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os); 22435446Sahrens if (error) 22445446Sahrens return (error); 22455446Sahrens 22465446Sahrens if (dmu_objset_type(os) == DMU_OST_ZFS) { 22475446Sahrens mutex_enter(&os->os->os_user_ptr_lock); 22485446Sahrens zfsvfs = dmu_objset_get_user(os); 22495446Sahrens if (zfsvfs != NULL) 22505446Sahrens VFS_HOLD(zfsvfs->z_vfs); 22515446Sahrens mutex_exit(&os->os->os_user_ptr_lock); 22525446Sahrens } 22535446Sahrens 22545446Sahrens if (zfsvfs != NULL) { 22555446Sahrens char osname[MAXNAMELEN]; 22565446Sahrens int mode; 22575446Sahrens 22586083Sek110237 error = zfs_suspend_fs(zfsvfs, osname, &mode); 22596083Sek110237 if (error == 0) { 22606083Sek110237 int resume_err; 22616083Sek110237 22626083Sek110237 ASSERT(strcmp(osname, zc->zc_name) == 0); 22636083Sek110237 error = dmu_objset_rollback(os); 22646083Sek110237 resume_err = zfs_resume_fs(zfsvfs, osname, mode); 22656083Sek110237 error = error ? error : resume_err; 22666083Sek110237 } else { 22676083Sek110237 dmu_objset_close(os); 22686083Sek110237 } 22695446Sahrens VFS_RELE(zfsvfs->z_vfs); 22705446Sahrens } else { 22715446Sahrens error = dmu_objset_rollback(os); 22725446Sahrens } 22736689Smaybee /* Note, the dmu_objset_rollback() releases the objset for us. */ 22745446Sahrens 22755446Sahrens return (error); 2276789Sahrens } 2277789Sahrens 22785367Sahrens /* 22795367Sahrens * inputs: 22805367Sahrens * zc_name old name of dataset 22815367Sahrens * zc_value new name of dataset 22825367Sahrens * zc_cookie recursive flag (only valid for snapshots) 22835367Sahrens * 22845367Sahrens * outputs: none 22855367Sahrens */ 2286789Sahrens static int 2287789Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 2288789Sahrens { 22894490Svb160487 boolean_t recursive = zc->zc_cookie & 1; 22904007Smmusante 22912676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 22925326Sek110237 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 22935326Sek110237 strchr(zc->zc_value, '%')) 2294789Sahrens return (EINVAL); 2295789Sahrens 22964007Smmusante /* 22974007Smmusante * Unmount snapshot unless we're doing a recursive rename, 22984007Smmusante * in which case the dataset code figures out which snapshots 22994007Smmusante * to unmount. 23004007Smmusante */ 23014007Smmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 2302789Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 23032199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 23042199Sahrens if (err) 23052199Sahrens return (err); 2306789Sahrens } 23074007Smmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 2308789Sahrens } 2309789Sahrens 23106689Smaybee static void 23116689Smaybee clear_props(char *dataset, nvlist_t *props) 23126689Smaybee { 23136689Smaybee zfs_cmd_t *zc; 23146689Smaybee nvpair_t *prop; 23156689Smaybee 23166689Smaybee if (props == NULL) 23176689Smaybee return; 23186689Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 23196689Smaybee (void) strcpy(zc->zc_name, dataset); 23206689Smaybee for (prop = nvlist_next_nvpair(props, NULL); prop; 23216689Smaybee prop = nvlist_next_nvpair(props, prop)) { 23226689Smaybee (void) strcpy(zc->zc_value, nvpair_name(prop)); 23236689Smaybee if (zfs_secpolicy_inherit(zc, CRED()) == 0) 23246689Smaybee (void) zfs_ioc_inherit_prop(zc); 23256689Smaybee } 23266689Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 23276689Smaybee } 23286689Smaybee 23295367Sahrens /* 23305367Sahrens * inputs: 23315367Sahrens * zc_name name of containing filesystem 23325367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 23335367Sahrens * zc_value name of snapshot to create 23345367Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 23355367Sahrens * zc_cookie file descriptor to recv from 23365367Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 23375367Sahrens * zc_guid force flag 23385367Sahrens * 23395367Sahrens * outputs: 23405367Sahrens * zc_cookie number of bytes read 23415367Sahrens */ 2342789Sahrens static int 23435367Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 2344789Sahrens { 2345789Sahrens file_t *fp; 23465326Sek110237 objset_t *os; 23475367Sahrens dmu_recv_cookie_t drc; 23485326Sek110237 zfsvfs_t *zfsvfs = NULL; 23495326Sek110237 boolean_t force = (boolean_t)zc->zc_guid; 2350789Sahrens int error, fd; 23515367Sahrens offset_t off; 23525367Sahrens nvlist_t *props = NULL; 23536689Smaybee nvlist_t *origprops = NULL; 23545367Sahrens objset_t *origin = NULL; 23555367Sahrens char *tosnap; 23565367Sahrens char tofs[ZFS_MAXNAMELEN]; 2357789Sahrens 23583265Sahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 23595326Sek110237 strchr(zc->zc_value, '@') == NULL || 23605326Sek110237 strchr(zc->zc_value, '%')) 23613265Sahrens return (EINVAL); 23623265Sahrens 23635367Sahrens (void) strcpy(tofs, zc->zc_value); 23645367Sahrens tosnap = strchr(tofs, '@'); 23655367Sahrens *tosnap = '\0'; 23665367Sahrens tosnap++; 23675367Sahrens 23685367Sahrens if (zc->zc_nvlist_src != NULL && 23695367Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 23705367Sahrens &props)) != 0) 23715367Sahrens return (error); 23725367Sahrens 2373789Sahrens fd = zc->zc_cookie; 2374789Sahrens fp = getf(fd); 23755367Sahrens if (fp == NULL) { 23765367Sahrens nvlist_free(props); 2377789Sahrens return (EBADF); 23785367Sahrens } 23795326Sek110237 23806689Smaybee if (dmu_objset_open(tofs, DMU_OST_ANY, 23816689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 23826689Smaybee /* 23836689Smaybee * Try to get the zfsvfs for the receiving objset. 23846689Smaybee * There won't be one if we're operating on a zvol, 23856689Smaybee * if the objset doesn't exist yet, or is not mounted. 23866689Smaybee */ 23875446Sahrens mutex_enter(&os->os->os_user_ptr_lock); 23886689Smaybee if (zfsvfs = dmu_objset_get_user(os)) { 23896083Sek110237 if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) { 23906689Smaybee mutex_exit(&os->os->os_user_ptr_lock); 23916083Sek110237 dmu_objset_close(os); 23926689Smaybee zfsvfs = NULL; 23936689Smaybee error = EBUSY; 23946689Smaybee goto out; 23956083Sek110237 } 23966689Smaybee VFS_HOLD(zfsvfs->z_vfs); 23976083Sek110237 } 23986689Smaybee mutex_exit(&os->os->os_user_ptr_lock); 23996689Smaybee 24006689Smaybee /* 24016689Smaybee * If new properties are supplied, they are to completely 24026689Smaybee * replace the existing ones, so stash away the existing ones. 24036689Smaybee */ 24046689Smaybee if (props) 24056689Smaybee (void) dsl_prop_get_all(os, &origprops, TRUE); 24066689Smaybee 24075326Sek110237 dmu_objset_close(os); 24085326Sek110237 } 24095326Sek110237 24105367Sahrens if (zc->zc_string[0]) { 24115367Sahrens error = dmu_objset_open(zc->zc_string, DMU_OST_ANY, 24126689Smaybee DS_MODE_USER | DS_MODE_READONLY, &origin); 24136689Smaybee if (error) 24146689Smaybee goto out; 24155367Sahrens } 24165367Sahrens 24175367Sahrens error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record, 24185367Sahrens force, origin, zfsvfs != NULL, &drc); 24195367Sahrens if (origin) 24205367Sahrens dmu_objset_close(origin); 24216689Smaybee if (error) 24226689Smaybee goto out; 24235326Sek110237 24245326Sek110237 /* 24256689Smaybee * Reset properties. We do this before we receive the stream 24266689Smaybee * so that the properties are applied to the new data. 24275326Sek110237 */ 24285367Sahrens if (props) { 24296689Smaybee clear_props(tofs, origprops); 24306689Smaybee /* 24316689Smaybee * XXX - Note, this is all-or-nothing; should be best-effort. 24326689Smaybee */ 24336689Smaybee (void) zfs_set_prop_nvlist(tofs, props); 24345367Sahrens } 24355367Sahrens 24365367Sahrens off = fp->f_offset; 24375367Sahrens error = dmu_recv_stream(&drc, fp->f_vnode, &off); 24385367Sahrens 24396689Smaybee if (error == 0 && zfsvfs) { 24406689Smaybee char osname[MAXNAMELEN]; 24416689Smaybee int mode; 24426689Smaybee 24436689Smaybee /* online recv */ 24446689Smaybee error = zfs_suspend_fs(zfsvfs, osname, &mode); 24456689Smaybee if (error == 0) { 24466689Smaybee int resume_err; 24476689Smaybee 24486689Smaybee error = dmu_recv_end(&drc); 24496689Smaybee resume_err = zfs_resume_fs(zfsvfs, osname, mode); 24506689Smaybee error = error ? error : resume_err; 24515367Sahrens } else { 24526689Smaybee dmu_recv_abort_cleanup(&drc); 24535367Sahrens } 24546689Smaybee } else if (error == 0) { 24556689Smaybee error = dmu_recv_end(&drc); 24566083Sek110237 } 24575367Sahrens 24585367Sahrens zc->zc_cookie = off - fp->f_offset; 24595367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 24605367Sahrens fp->f_offset = off; 24612885Sahrens 24626689Smaybee /* 24636689Smaybee * On error, restore the original props. 24646689Smaybee */ 24656689Smaybee if (error && props) { 24666689Smaybee clear_props(tofs, props); 24676689Smaybee (void) zfs_set_prop_nvlist(tofs, origprops); 24686689Smaybee } 24696689Smaybee out: 24706689Smaybee if (zfsvfs) { 24716689Smaybee mutex_exit(&zfsvfs->z_online_recv_lock); 24726689Smaybee VFS_RELE(zfsvfs->z_vfs); 24736689Smaybee } 24746689Smaybee nvlist_free(props); 24756689Smaybee nvlist_free(origprops); 2476789Sahrens releasef(fd); 2477789Sahrens return (error); 2478789Sahrens } 2479789Sahrens 24805367Sahrens /* 24815367Sahrens * inputs: 24825367Sahrens * zc_name name of snapshot to send 24835367Sahrens * zc_value short name of incremental fromsnap (may be empty) 24845367Sahrens * zc_cookie file descriptor to send stream to 24855367Sahrens * zc_obj fromorigin flag (mutually exclusive with zc_value) 24865367Sahrens * 24875367Sahrens * outputs: none 24885367Sahrens */ 2489789Sahrens static int 24905367Sahrens zfs_ioc_send(zfs_cmd_t *zc) 2491789Sahrens { 2492789Sahrens objset_t *fromsnap = NULL; 2493789Sahrens objset_t *tosnap; 2494789Sahrens file_t *fp; 2495789Sahrens int error; 24965367Sahrens offset_t off; 2497789Sahrens 2498789Sahrens error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 24996689Smaybee DS_MODE_USER | DS_MODE_READONLY, &tosnap); 2500789Sahrens if (error) 2501789Sahrens return (error); 2502789Sahrens 25032676Seschrock if (zc->zc_value[0] != '\0') { 25042885Sahrens char buf[MAXPATHLEN]; 25052885Sahrens char *cp; 25062885Sahrens 25072885Sahrens (void) strncpy(buf, zc->zc_name, sizeof (buf)); 25082885Sahrens cp = strchr(buf, '@'); 25092885Sahrens if (cp) 25102885Sahrens *(cp+1) = 0; 25112885Sahrens (void) strncat(buf, zc->zc_value, sizeof (buf)); 25122885Sahrens error = dmu_objset_open(buf, DMU_OST_ANY, 25136689Smaybee DS_MODE_USER | DS_MODE_READONLY, &fromsnap); 2514789Sahrens if (error) { 2515789Sahrens dmu_objset_close(tosnap); 2516789Sahrens return (error); 2517789Sahrens } 2518789Sahrens } 2519789Sahrens 2520789Sahrens fp = getf(zc->zc_cookie); 2521789Sahrens if (fp == NULL) { 2522789Sahrens dmu_objset_close(tosnap); 2523789Sahrens if (fromsnap) 2524789Sahrens dmu_objset_close(fromsnap); 2525789Sahrens return (EBADF); 2526789Sahrens } 2527789Sahrens 25285367Sahrens off = fp->f_offset; 25295367Sahrens error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 25305367Sahrens 25315367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 25325367Sahrens fp->f_offset = off; 2533789Sahrens releasef(zc->zc_cookie); 2534789Sahrens if (fromsnap) 2535789Sahrens dmu_objset_close(fromsnap); 2536789Sahrens dmu_objset_close(tosnap); 2537789Sahrens return (error); 2538789Sahrens } 2539789Sahrens 25401544Seschrock static int 25411544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 25421544Seschrock { 25431544Seschrock int id, error; 25441544Seschrock 25451544Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 25461544Seschrock &zc->zc_inject_record); 25471544Seschrock 25481544Seschrock if (error == 0) 25491544Seschrock zc->zc_guid = (uint64_t)id; 25501544Seschrock 25511544Seschrock return (error); 25521544Seschrock } 25531544Seschrock 25541544Seschrock static int 25551544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 25561544Seschrock { 25571544Seschrock return (zio_clear_fault((int)zc->zc_guid)); 25581544Seschrock } 25591544Seschrock 25601544Seschrock static int 25611544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 25621544Seschrock { 25631544Seschrock int id = (int)zc->zc_guid; 25641544Seschrock int error; 25651544Seschrock 25661544Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 25671544Seschrock &zc->zc_inject_record); 25681544Seschrock 25691544Seschrock zc->zc_guid = id; 25701544Seschrock 25711544Seschrock return (error); 25721544Seschrock } 25731544Seschrock 25741544Seschrock static int 25751544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 25761544Seschrock { 25771544Seschrock spa_t *spa; 25781544Seschrock int error; 25792676Seschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 25801544Seschrock 25811544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 25821544Seschrock return (error); 25831544Seschrock 25842676Seschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 25851544Seschrock &count); 25861544Seschrock if (error == 0) 25872676Seschrock zc->zc_nvlist_dst_size = count; 25881544Seschrock else 25892676Seschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 25901544Seschrock 25911544Seschrock spa_close(spa, FTAG); 25921544Seschrock 25931544Seschrock return (error); 25941544Seschrock } 25951544Seschrock 25961544Seschrock static int 25971544Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 25981544Seschrock { 25991544Seschrock spa_t *spa; 26001544Seschrock vdev_t *vd; 26014808Sek110237 uint64_t txg; 26021544Seschrock int error; 26031544Seschrock 26041544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 26051544Seschrock return (error); 26061544Seschrock 26075329Sgw25295 /* 26085329Sgw25295 * Try to resume any I/Os which may have been suspended 26095329Sgw25295 * as a result of a complete pool failure. 26105329Sgw25295 */ 26115329Sgw25295 if (!list_is_empty(&spa->spa_zio_list)) { 26125329Sgw25295 if (zio_vdev_resume_io(spa) != 0) { 26135329Sgw25295 spa_close(spa, FTAG); 26145329Sgw25295 return (EIO); 26155329Sgw25295 } 26165329Sgw25295 } 26175329Sgw25295 26184451Seschrock txg = spa_vdev_enter(spa); 26191544Seschrock 26202676Seschrock if (zc->zc_guid == 0) { 26211544Seschrock vd = NULL; 26226643Seschrock } else { 26236643Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 26245450Sbrendan if (vd == NULL) { 26255450Sbrendan (void) spa_vdev_exit(spa, NULL, txg, ENODEV); 26265450Sbrendan spa_close(spa, FTAG); 26275450Sbrendan return (ENODEV); 26285450Sbrendan } 26291544Seschrock } 26301544Seschrock 26315329Sgw25295 vdev_clear(spa, vd, B_TRUE); 26321544Seschrock 26334451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 26341544Seschrock 26351544Seschrock spa_close(spa, FTAG); 26361544Seschrock 26371544Seschrock return (0); 26381544Seschrock } 26391544Seschrock 26405367Sahrens /* 26415367Sahrens * inputs: 26425367Sahrens * zc_name name of filesystem 26435367Sahrens * zc_value name of origin snapshot 26445367Sahrens * 26455367Sahrens * outputs: none 26465367Sahrens */ 26471544Seschrock static int 26482082Seschrock zfs_ioc_promote(zfs_cmd_t *zc) 26492082Seschrock { 26502417Sahrens char *cp; 26512417Sahrens 26522417Sahrens /* 26532417Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 26542417Sahrens * it's easier. 26552417Sahrens */ 26562676Seschrock cp = strchr(zc->zc_value, '@'); 26572417Sahrens if (cp) 26582417Sahrens *cp = '\0'; 26592676Seschrock (void) dmu_objset_find(zc->zc_value, 26602417Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 26612082Seschrock return (dsl_dataset_promote(zc->zc_name)); 26622082Seschrock } 26632082Seschrock 26644543Smarks /* 26654543Smarks * We don't want to have a hard dependency 26664543Smarks * against some special symbols in sharefs 26675331Samw * nfs, and smbsrv. Determine them if needed when 26684543Smarks * the first file system is shared. 26695331Samw * Neither sharefs, nfs or smbsrv are unloadable modules. 26704543Smarks */ 26715331Samw int (*znfsexport_fs)(void *arg); 26724543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 26735331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 26745331Samw 26755331Samw int zfs_nfsshare_inited; 26765331Samw int zfs_smbshare_inited; 26775331Samw 26784543Smarks ddi_modhandle_t nfs_mod; 26794543Smarks ddi_modhandle_t sharefs_mod; 26805331Samw ddi_modhandle_t smbsrv_mod; 26814543Smarks kmutex_t zfs_share_lock; 26824543Smarks 26834543Smarks static int 26845331Samw zfs_init_sharefs() 26855331Samw { 26865331Samw int error; 26875331Samw 26885331Samw ASSERT(MUTEX_HELD(&zfs_share_lock)); 26895331Samw /* Both NFS and SMB shares also require sharetab support. */ 26905331Samw if (sharefs_mod == NULL && ((sharefs_mod = 26915331Samw ddi_modopen("fs/sharefs", 26925331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 26935331Samw return (ENOSYS); 26945331Samw } 26955331Samw if (zshare_fs == NULL && ((zshare_fs = 26965331Samw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 26975331Samw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 26985331Samw return (ENOSYS); 26995331Samw } 27005331Samw return (0); 27015331Samw } 27025331Samw 27035331Samw static int 27044543Smarks zfs_ioc_share(zfs_cmd_t *zc) 27054543Smarks { 27064543Smarks int error; 27074543Smarks int opcode; 27084543Smarks 27095331Samw switch (zc->zc_share.z_sharetype) { 27105331Samw case ZFS_SHARE_NFS: 27115331Samw case ZFS_UNSHARE_NFS: 27125331Samw if (zfs_nfsshare_inited == 0) { 27135331Samw mutex_enter(&zfs_share_lock); 27145331Samw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 27155331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 27165331Samw mutex_exit(&zfs_share_lock); 27175331Samw return (ENOSYS); 27185331Samw } 27195331Samw if (znfsexport_fs == NULL && 27205331Samw ((znfsexport_fs = (int (*)(void *)) 27215331Samw ddi_modsym(nfs_mod, 27225331Samw "nfs_export", &error)) == NULL)) { 27235331Samw mutex_exit(&zfs_share_lock); 27245331Samw return (ENOSYS); 27255331Samw } 27265331Samw error = zfs_init_sharefs(); 27275331Samw if (error) { 27285331Samw mutex_exit(&zfs_share_lock); 27295331Samw return (ENOSYS); 27305331Samw } 27315331Samw zfs_nfsshare_inited = 1; 27324543Smarks mutex_exit(&zfs_share_lock); 27334543Smarks } 27345331Samw break; 27355331Samw case ZFS_SHARE_SMB: 27365331Samw case ZFS_UNSHARE_SMB: 27375331Samw if (zfs_smbshare_inited == 0) { 27385331Samw mutex_enter(&zfs_share_lock); 27395331Samw if (smbsrv_mod == NULL && ((smbsrv_mod = 27405331Samw ddi_modopen("drv/smbsrv", 27415331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 27425331Samw mutex_exit(&zfs_share_lock); 27435331Samw return (ENOSYS); 27445331Samw } 27455331Samw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 27465331Samw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 27476139Sjb150015 "smb_server_share", &error)) == NULL)) { 27485331Samw mutex_exit(&zfs_share_lock); 27495331Samw return (ENOSYS); 27505331Samw } 27515331Samw error = zfs_init_sharefs(); 27525331Samw if (error) { 27535331Samw mutex_exit(&zfs_share_lock); 27545331Samw return (ENOSYS); 27555331Samw } 27565331Samw zfs_smbshare_inited = 1; 27574543Smarks mutex_exit(&zfs_share_lock); 27584543Smarks } 27595331Samw break; 27605331Samw default: 27615331Samw return (EINVAL); 27624543Smarks } 27634543Smarks 27645331Samw switch (zc->zc_share.z_sharetype) { 27655331Samw case ZFS_SHARE_NFS: 27665331Samw case ZFS_UNSHARE_NFS: 27675331Samw if (error = 27685331Samw znfsexport_fs((void *) 27695331Samw (uintptr_t)zc->zc_share.z_exportdata)) 27705331Samw return (error); 27715331Samw break; 27725331Samw case ZFS_SHARE_SMB: 27735331Samw case ZFS_UNSHARE_SMB: 27745331Samw if (error = zsmbexport_fs((void *) 27755331Samw (uintptr_t)zc->zc_share.z_exportdata, 27765331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 27775331Samw B_TRUE : B_FALSE)) { 27785331Samw return (error); 27795331Samw } 27805331Samw break; 27815331Samw } 27825331Samw 27835331Samw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 27845331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 27854543Smarks SHAREFS_ADD : SHAREFS_REMOVE; 27864543Smarks 27875331Samw /* 27885331Samw * Add or remove share from sharetab 27895331Samw */ 27904543Smarks error = zshare_fs(opcode, 27914543Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 27924543Smarks zc->zc_share.z_sharemax); 27934543Smarks 27944543Smarks return (error); 27954543Smarks 27964543Smarks } 27974543Smarks 27984543Smarks /* 27994988Sek110237 * pool create, destroy, and export don't log the history as part of 28004988Sek110237 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 28014988Sek110237 * do the logging of those commands. 28024543Smarks */ 2803789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 28044715Sek110237 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28054577Sahrens { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28064577Sahrens { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28074577Sahrens { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28084577Sahrens { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE }, 28094577Sahrens { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 28104577Sahrens { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE }, 28114577Sahrens { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28124577Sahrens { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE }, 28134577Sahrens { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28144577Sahrens { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28154577Sahrens { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28164577Sahrens { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28174577Sahrens { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28184577Sahrens { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28194577Sahrens { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28204577Sahrens { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28214577Sahrens { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 28225498Stimh { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 28234543Smarks { zfs_ioc_dataset_list_next, zfs_secpolicy_read, 28244577Sahrens DATASET_NAME, B_FALSE }, 28254543Smarks { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, 28264577Sahrens DATASET_NAME, B_FALSE }, 28274577Sahrens { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE }, 28284577Sahrens { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 28294577Sahrens { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 28304577Sahrens { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE }, 28314577Sahrens { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 28324577Sahrens { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE }, 28334577Sahrens { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE }, 28345367Sahrens { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE }, 28355367Sahrens { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE }, 28364577Sahrens { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 28374577Sahrens { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 28384577Sahrens { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 28394577Sahrens { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE }, 28404577Sahrens { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28414577Sahrens { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE }, 28424577Sahrens { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 28434577Sahrens { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE }, 28444577Sahrens { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28454577Sahrens { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE }, 28464577Sahrens { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28474577Sahrens { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 28484577Sahrens { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE }, 28494577Sahrens { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 28504543Smarks { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, 28514577Sahrens DATASET_NAME, B_FALSE }, 28524849Sahrens { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE }, 28534849Sahrens { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE }, 2854789Sahrens }; 2855789Sahrens 2856789Sahrens static int 2857789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 2858789Sahrens { 2859789Sahrens zfs_cmd_t *zc; 2860789Sahrens uint_t vec; 28612199Sahrens int error, rc; 2862789Sahrens 2863789Sahrens if (getminor(dev) != 0) 2864789Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 2865789Sahrens 2866789Sahrens vec = cmd - ZFS_IOC; 28674787Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 2868789Sahrens 2869789Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 2870789Sahrens return (EINVAL); 2871789Sahrens 2872789Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2873789Sahrens 2874789Sahrens error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t)); 2875789Sahrens 28764787Sahrens if (error == 0) 28774543Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 2878789Sahrens 2879789Sahrens /* 2880789Sahrens * Ensure that all pool/dataset names are valid before we pass down to 2881789Sahrens * the lower layers. 2882789Sahrens */ 2883789Sahrens if (error == 0) { 2884789Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 2885789Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 28864577Sahrens case POOL_NAME: 2887789Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 2888789Sahrens error = EINVAL; 2889789Sahrens break; 2890789Sahrens 28914577Sahrens case DATASET_NAME: 2892789Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 2893789Sahrens error = EINVAL; 2894789Sahrens break; 28952856Snd150628 28964577Sahrens case NO_NAME: 28972856Snd150628 break; 2898789Sahrens } 2899789Sahrens } 2900789Sahrens 2901789Sahrens if (error == 0) 2902789Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 2903789Sahrens 29042199Sahrens rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t)); 29054543Smarks if (error == 0) { 29062199Sahrens error = rc; 29074543Smarks if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE) 29084543Smarks zfs_log_history(zc); 29094543Smarks } 2910789Sahrens 2911789Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 2912789Sahrens return (error); 2913789Sahrens } 2914789Sahrens 2915789Sahrens static int 2916789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2917789Sahrens { 2918789Sahrens if (cmd != DDI_ATTACH) 2919789Sahrens return (DDI_FAILURE); 2920789Sahrens 2921789Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 2922789Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 2923789Sahrens return (DDI_FAILURE); 2924789Sahrens 2925789Sahrens zfs_dip = dip; 2926789Sahrens 2927789Sahrens ddi_report_dev(dip); 2928789Sahrens 2929789Sahrens return (DDI_SUCCESS); 2930789Sahrens } 2931789Sahrens 2932789Sahrens static int 2933789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2934789Sahrens { 2935789Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 2936789Sahrens return (DDI_FAILURE); 2937789Sahrens 2938789Sahrens if (cmd != DDI_DETACH) 2939789Sahrens return (DDI_FAILURE); 2940789Sahrens 2941789Sahrens zfs_dip = NULL; 2942789Sahrens 2943789Sahrens ddi_prop_remove_all(dip); 2944789Sahrens ddi_remove_minor_node(dip, NULL); 2945789Sahrens 2946789Sahrens return (DDI_SUCCESS); 2947789Sahrens } 2948789Sahrens 2949789Sahrens /*ARGSUSED*/ 2950789Sahrens static int 2951789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2952789Sahrens { 2953789Sahrens switch (infocmd) { 2954789Sahrens case DDI_INFO_DEVT2DEVINFO: 2955789Sahrens *result = zfs_dip; 2956789Sahrens return (DDI_SUCCESS); 2957789Sahrens 2958789Sahrens case DDI_INFO_DEVT2INSTANCE: 2959849Sbonwick *result = (void *)0; 2960789Sahrens return (DDI_SUCCESS); 2961789Sahrens } 2962789Sahrens 2963789Sahrens return (DDI_FAILURE); 2964789Sahrens } 2965789Sahrens 2966789Sahrens /* 2967789Sahrens * OK, so this is a little weird. 2968789Sahrens * 2969789Sahrens * /dev/zfs is the control node, i.e. minor 0. 2970789Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 2971789Sahrens * 2972789Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 2973789Sahrens * so most of the standard driver entry points are in zvol.c. 2974789Sahrens */ 2975789Sahrens static struct cb_ops zfs_cb_ops = { 2976789Sahrens zvol_open, /* open */ 2977789Sahrens zvol_close, /* close */ 2978789Sahrens zvol_strategy, /* strategy */ 2979789Sahrens nodev, /* print */ 29806423Sgw25295 zvol_dump, /* dump */ 2981789Sahrens zvol_read, /* read */ 2982789Sahrens zvol_write, /* write */ 2983789Sahrens zfsdev_ioctl, /* ioctl */ 2984789Sahrens nodev, /* devmap */ 2985789Sahrens nodev, /* mmap */ 2986789Sahrens nodev, /* segmap */ 2987789Sahrens nochpoll, /* poll */ 2988789Sahrens ddi_prop_op, /* prop_op */ 2989789Sahrens NULL, /* streamtab */ 2990789Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 2991789Sahrens CB_REV, /* version */ 29923638Sbillm nodev, /* async read */ 29933638Sbillm nodev, /* async write */ 2994789Sahrens }; 2995789Sahrens 2996789Sahrens static struct dev_ops zfs_dev_ops = { 2997789Sahrens DEVO_REV, /* version */ 2998789Sahrens 0, /* refcnt */ 2999789Sahrens zfs_info, /* info */ 3000789Sahrens nulldev, /* identify */ 3001789Sahrens nulldev, /* probe */ 3002789Sahrens zfs_attach, /* attach */ 3003789Sahrens zfs_detach, /* detach */ 3004789Sahrens nodev, /* reset */ 3005789Sahrens &zfs_cb_ops, /* driver operations */ 3006789Sahrens NULL /* no bus operations */ 3007789Sahrens }; 3008789Sahrens 3009789Sahrens static struct modldrv zfs_modldrv = { 30104577Sahrens &mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING, 30112676Seschrock &zfs_dev_ops 3012789Sahrens }; 3013789Sahrens 3014789Sahrens static struct modlinkage modlinkage = { 3015789Sahrens MODREV_1, 3016789Sahrens (void *)&zfs_modlfs, 3017789Sahrens (void *)&zfs_modldrv, 3018789Sahrens NULL 3019789Sahrens }; 3020789Sahrens 30214720Sfr157268 30224720Sfr157268 uint_t zfs_fsyncer_key; 30235326Sek110237 extern uint_t rrw_tsd_key; 30244720Sfr157268 3025789Sahrens int 3026789Sahrens _init(void) 3027789Sahrens { 3028789Sahrens int error; 3029789Sahrens 3030849Sbonwick spa_init(FREAD | FWRITE); 3031849Sbonwick zfs_init(); 3032849Sbonwick zvol_init(); 3033849Sbonwick 3034849Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 3035849Sbonwick zvol_fini(); 3036849Sbonwick zfs_fini(); 3037849Sbonwick spa_fini(); 3038789Sahrens return (error); 3039849Sbonwick } 3040789Sahrens 30414720Sfr157268 tsd_create(&zfs_fsyncer_key, NULL); 30425326Sek110237 tsd_create(&rrw_tsd_key, NULL); 30434720Sfr157268 3044789Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 3045789Sahrens ASSERT(error == 0); 30464543Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 3047789Sahrens 3048789Sahrens return (0); 3049789Sahrens } 3050789Sahrens 3051789Sahrens int 3052789Sahrens _fini(void) 3053789Sahrens { 3054789Sahrens int error; 3055789Sahrens 30561544Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 3057789Sahrens return (EBUSY); 3058789Sahrens 3059789Sahrens if ((error = mod_remove(&modlinkage)) != 0) 3060789Sahrens return (error); 3061789Sahrens 3062789Sahrens zvol_fini(); 3063789Sahrens zfs_fini(); 3064789Sahrens spa_fini(); 30655331Samw if (zfs_nfsshare_inited) 30664543Smarks (void) ddi_modclose(nfs_mod); 30675331Samw if (zfs_smbshare_inited) 30685331Samw (void) ddi_modclose(smbsrv_mod); 30695331Samw if (zfs_nfsshare_inited || zfs_smbshare_inited) 30704543Smarks (void) ddi_modclose(sharefs_mod); 3071789Sahrens 30724720Sfr157268 tsd_destroy(&zfs_fsyncer_key); 3073789Sahrens ldi_ident_release(zfs_li); 3074789Sahrens zfs_li = NULL; 30754543Smarks mutex_destroy(&zfs_share_lock); 3076789Sahrens 3077789Sahrens return (error); 3078789Sahrens } 3079789Sahrens 3080789Sahrens int 3081789Sahrens _info(struct modinfo *modinfop) 3082789Sahrens { 3083789Sahrens return (mod_info(&modlinkage, modinfop)); 3084789Sahrens } 3085