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 957184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 967184Stimh boolean_t *); 977184Stimh int zfs_set_prop_nvlist(const char *, nvlist_t *); 987184Stimh 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 /* 1867184Stimh * zfs_earlier_version 1875375Stimh * 1885375Stimh * Return non-zero if the spa version is less than requested version. 1895375Stimh */ 1905331Samw static int 1917184Stimh 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; 7487184Stimh nvlist_t *rootprops = NULL; 7497184Stimh 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 7627184Stimh if (props) { 7637184Stimh nvlist_t *nvl = NULL; 7647184Stimh uint64_t version = SPA_VERSION; 7657184Stimh 7667184Stimh (void) nvlist_lookup_uint64(props, 7677184Stimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 7687184Stimh if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 7697184Stimh error = EINVAL; 7707184Stimh goto pool_props_bad; 7717184Stimh } 7727184Stimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 7737184Stimh if (nvl) { 7747184Stimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 7757184Stimh if (error != 0) { 7767184Stimh nvlist_free(config); 7777184Stimh nvlist_free(props); 7787184Stimh return (error); 7797184Stimh } 7807184Stimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 7817184Stimh } 7827184Stimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 7837184Stimh error = zfs_fill_zplprops_root(version, rootprops, 7847184Stimh zplprops, NULL); 7857184Stimh if (error) 7867184Stimh goto pool_props_bad; 7877184Stimh } 7887184Stimh 7894988Sek110237 buf = history_str_get(zc); 790789Sahrens 7917184Stimh error = spa_create(zc->zc_name, config, props, buf, zplprops); 7927184Stimh 7937184Stimh /* 7947184Stimh * Set the remaining root properties 7957184Stimh */ 7967184Stimh if (!error && 7977184Stimh (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0) 7987184Stimh (void) spa_destroy(zc->zc_name); 799789Sahrens 8004988Sek110237 if (buf != NULL) 8014988Sek110237 history_str_free(buf); 8025094Slling 8037184Stimh pool_props_bad: 8047184Stimh nvlist_free(rootprops); 8057184Stimh nvlist_free(zplprops); 806789Sahrens nvlist_free(config); 8077184Stimh 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; 859*7214Slling boolean_t force = (boolean_t)zc->zc_cookie; 860*7214Slling 8614543Smarks zfs_log_history(zc); 862*7214Slling error = spa_export(zc->zc_name, NULL, force); 8634543Smarks return (error); 864789Sahrens } 865789Sahrens 866789Sahrens static int 867789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 868789Sahrens { 869789Sahrens nvlist_t *configs; 870789Sahrens int error; 871789Sahrens 872789Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 873789Sahrens return (EEXIST); 874789Sahrens 8752676Seschrock error = put_nvlist(zc, configs); 876789Sahrens 877789Sahrens nvlist_free(configs); 878789Sahrens 879789Sahrens return (error); 880789Sahrens } 881789Sahrens 882789Sahrens static int 883789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 884789Sahrens { 885789Sahrens nvlist_t *config; 886789Sahrens int error; 8871544Seschrock int ret = 0; 888789Sahrens 8892676Seschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 8902676Seschrock sizeof (zc->zc_value)); 891789Sahrens 892789Sahrens if (config != NULL) { 8932676Seschrock ret = put_nvlist(zc, config); 894789Sahrens nvlist_free(config); 8951544Seschrock 8961544Seschrock /* 8971544Seschrock * The config may be present even if 'error' is non-zero. 8981544Seschrock * In this case we return success, and preserve the real errno 8991544Seschrock * in 'zc_cookie'. 9001544Seschrock */ 9011544Seschrock zc->zc_cookie = error; 902789Sahrens } else { 9031544Seschrock ret = error; 904789Sahrens } 905789Sahrens 9061544Seschrock return (ret); 907789Sahrens } 908789Sahrens 909789Sahrens /* 910789Sahrens * Try to import the given pool, returning pool stats as appropriate so that 911789Sahrens * user land knows which devices are available and overall pool health. 912789Sahrens */ 913789Sahrens static int 914789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 915789Sahrens { 916789Sahrens nvlist_t *tryconfig, *config; 917789Sahrens int error; 918789Sahrens 9195094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 9205094Slling &tryconfig)) != 0) 921789Sahrens return (error); 922789Sahrens 923789Sahrens config = spa_tryimport(tryconfig); 924789Sahrens 925789Sahrens nvlist_free(tryconfig); 926789Sahrens 927789Sahrens if (config == NULL) 928789Sahrens return (EINVAL); 929789Sahrens 9302676Seschrock error = put_nvlist(zc, config); 931789Sahrens nvlist_free(config); 932789Sahrens 933789Sahrens return (error); 934789Sahrens } 935789Sahrens 936789Sahrens static int 937789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc) 938789Sahrens { 939789Sahrens spa_t *spa; 940789Sahrens int error; 941789Sahrens 9422926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 9432926Sek110237 return (error); 9442926Sek110237 9457046Sahrens error = spa_scrub(spa, zc->zc_cookie); 9462926Sek110237 9472926Sek110237 spa_close(spa, FTAG); 9482926Sek110237 949789Sahrens return (error); 950789Sahrens } 951789Sahrens 952789Sahrens static int 953789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 954789Sahrens { 955789Sahrens spa_t *spa; 956789Sahrens int error; 957789Sahrens 958789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 959789Sahrens if (error == 0) { 960789Sahrens spa_freeze(spa); 961789Sahrens spa_close(spa, FTAG); 962789Sahrens } 963789Sahrens return (error); 964789Sahrens } 965789Sahrens 966789Sahrens static int 9671760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 9681760Seschrock { 9691760Seschrock spa_t *spa; 9701760Seschrock int error; 9711760Seschrock 9722926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 9732926Sek110237 return (error); 9742926Sek110237 9755118Slling if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 9765118Slling spa_close(spa, FTAG); 9775118Slling return (EINVAL); 9785118Slling } 9795118Slling 9805094Slling spa_upgrade(spa, zc->zc_cookie); 9812926Sek110237 spa_close(spa, FTAG); 9822926Sek110237 9832926Sek110237 return (error); 9842926Sek110237 } 9852926Sek110237 9862926Sek110237 static int 9872926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 9882926Sek110237 { 9892926Sek110237 spa_t *spa; 9902926Sek110237 char *hist_buf; 9912926Sek110237 uint64_t size; 9922926Sek110237 int error; 9932926Sek110237 9942926Sek110237 if ((size = zc->zc_history_len) == 0) 9952926Sek110237 return (EINVAL); 9962926Sek110237 9972926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 9982926Sek110237 return (error); 9992926Sek110237 10004577Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 10013863Sek110237 spa_close(spa, FTAG); 10023863Sek110237 return (ENOTSUP); 10033863Sek110237 } 10043863Sek110237 10052926Sek110237 hist_buf = kmem_alloc(size, KM_SLEEP); 10062926Sek110237 if ((error = spa_history_get(spa, &zc->zc_history_offset, 10072926Sek110237 &zc->zc_history_len, hist_buf)) == 0) { 10084543Smarks error = xcopyout(hist_buf, 10094543Smarks (char *)(uintptr_t)zc->zc_history, 10102926Sek110237 zc->zc_history_len); 10112926Sek110237 } 10122926Sek110237 10132926Sek110237 spa_close(spa, FTAG); 10142926Sek110237 kmem_free(hist_buf, size); 10152926Sek110237 return (error); 10162926Sek110237 } 10172926Sek110237 10182926Sek110237 static int 10193444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 10203444Sek110237 { 10213444Sek110237 int error; 10223444Sek110237 10233912Slling if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 10243444Sek110237 return (error); 10253444Sek110237 10263444Sek110237 return (0); 10273444Sek110237 } 10283444Sek110237 10293444Sek110237 static int 10303444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 10313444Sek110237 { 10323444Sek110237 objset_t *osp; 10333444Sek110237 int error; 10343444Sek110237 10353444Sek110237 if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS, 10366689Smaybee DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0) 10373444Sek110237 return (error); 10383444Sek110237 error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value, 10393444Sek110237 sizeof (zc->zc_value)); 10403444Sek110237 dmu_objset_close(osp); 10413444Sek110237 10423444Sek110237 return (error); 10433444Sek110237 } 10443444Sek110237 10453444Sek110237 static int 1046789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1047789Sahrens { 1048789Sahrens spa_t *spa; 1049789Sahrens int error; 10506423Sgw25295 nvlist_t *config, **l2cache, **spares; 10516423Sgw25295 uint_t nl2cache = 0, nspares = 0; 1052789Sahrens 1053789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1054789Sahrens if (error != 0) 1055789Sahrens return (error); 1056789Sahrens 10575450Sbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 10585450Sbrendan &config); 10595450Sbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 10605450Sbrendan &l2cache, &nl2cache); 10615450Sbrendan 10626423Sgw25295 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 10636423Sgw25295 &spares, &nspares); 10646423Sgw25295 10653912Slling /* 10663912Slling * A root pool with concatenated devices is not supported. 10676423Sgw25295 * Thus, can not add a device to a root pool. 10686423Sgw25295 * 10696423Sgw25295 * Intent log device can not be added to a rootpool because 10706423Sgw25295 * during mountroot, zil is replayed, a seperated log device 10716423Sgw25295 * can not be accessed during the mountroot time. 10726423Sgw25295 * 10736423Sgw25295 * l2cache and spare devices are ok to be added to a rootpool. 10743912Slling */ 10756423Sgw25295 if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) { 10763912Slling spa_close(spa, FTAG); 10773912Slling return (EDOM); 10783912Slling } 10793912Slling 10805450Sbrendan if (error == 0) { 1081789Sahrens error = spa_vdev_add(spa, config); 1082789Sahrens nvlist_free(config); 1083789Sahrens } 1084789Sahrens spa_close(spa, FTAG); 1085789Sahrens return (error); 1086789Sahrens } 1087789Sahrens 1088789Sahrens static int 1089789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1090789Sahrens { 10912082Seschrock spa_t *spa; 10922082Seschrock int error; 10932082Seschrock 10942082Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 10952082Seschrock if (error != 0) 10962082Seschrock return (error); 10972082Seschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 10982082Seschrock spa_close(spa, FTAG); 10992082Seschrock return (error); 1100789Sahrens } 1101789Sahrens 1102789Sahrens static int 11034451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1104789Sahrens { 1105789Sahrens spa_t *spa; 1106789Sahrens int error; 11074451Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1108789Sahrens 11092926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1110789Sahrens return (error); 11114451Seschrock switch (zc->zc_cookie) { 11124451Seschrock case VDEV_STATE_ONLINE: 11134451Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 11144451Seschrock break; 11154451Seschrock 11164451Seschrock case VDEV_STATE_OFFLINE: 11174451Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 11184451Seschrock break; 1119789Sahrens 11204451Seschrock case VDEV_STATE_FAULTED: 11214451Seschrock error = vdev_fault(spa, zc->zc_guid); 11224451Seschrock break; 1123789Sahrens 11244451Seschrock case VDEV_STATE_DEGRADED: 11254451Seschrock error = vdev_degrade(spa, zc->zc_guid); 11264451Seschrock break; 11274451Seschrock 11284451Seschrock default: 11294451Seschrock error = EINVAL; 11304451Seschrock } 11314451Seschrock zc->zc_cookie = newstate; 1132789Sahrens spa_close(spa, FTAG); 1133789Sahrens return (error); 1134789Sahrens } 1135789Sahrens 1136789Sahrens static int 1137789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1138789Sahrens { 1139789Sahrens spa_t *spa; 1140789Sahrens int replacing = zc->zc_cookie; 1141789Sahrens nvlist_t *config; 1142789Sahrens int error; 1143789Sahrens 11442926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1145789Sahrens return (error); 1146789Sahrens 11475094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 11485094Slling &config)) == 0) { 11491544Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1150789Sahrens nvlist_free(config); 1151789Sahrens } 1152789Sahrens 1153789Sahrens spa_close(spa, FTAG); 1154789Sahrens return (error); 1155789Sahrens } 1156789Sahrens 1157789Sahrens static int 1158789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1159789Sahrens { 1160789Sahrens spa_t *spa; 1161789Sahrens int error; 1162789Sahrens 11632926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1164789Sahrens return (error); 1165789Sahrens 11661544Seschrock error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE); 1167789Sahrens 1168789Sahrens spa_close(spa, FTAG); 1169789Sahrens return (error); 1170789Sahrens } 1171789Sahrens 1172789Sahrens static int 11731354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 11741354Seschrock { 11751354Seschrock spa_t *spa; 11762676Seschrock char *path = zc->zc_value; 11771544Seschrock uint64_t guid = zc->zc_guid; 11781354Seschrock int error; 11791354Seschrock 11801354Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 11811354Seschrock if (error != 0) 11821354Seschrock return (error); 11831354Seschrock 11841354Seschrock error = spa_vdev_setpath(spa, guid, path); 11851354Seschrock spa_close(spa, FTAG); 11861354Seschrock return (error); 11871354Seschrock } 11881354Seschrock 11895367Sahrens /* 11905367Sahrens * inputs: 11915367Sahrens * zc_name name of filesystem 11925367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 11935367Sahrens * 11945367Sahrens * outputs: 11955367Sahrens * zc_objset_stats stats 11965367Sahrens * zc_nvlist_dst property nvlist 11975367Sahrens * zc_nvlist_dst_size size of property nvlist 11985367Sahrens */ 11991354Seschrock static int 1200789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc) 1201789Sahrens { 1202789Sahrens objset_t *os = NULL; 1203789Sahrens int error; 12041356Seschrock nvlist_t *nv; 1205789Sahrens 12066689Smaybee if (error = dmu_objset_open(zc->zc_name, 12076689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) 1208789Sahrens return (error); 1209789Sahrens 12102885Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1211789Sahrens 12122856Snd150628 if (zc->zc_nvlist_dst != 0 && 12136689Smaybee (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) { 12142885Sahrens dmu_objset_stats(os, nv); 12153087Sahrens /* 12165147Srm160521 * NB: zvol_get_stats() will read the objset contents, 12173087Sahrens * which we aren't supposed to do with a 12186689Smaybee * DS_MODE_USER hold, because it could be 12193087Sahrens * inconsistent. So this is a bit of a workaround... 12203087Sahrens */ 12214577Sahrens if (!zc->zc_objset_stats.dds_inconsistent) { 12224577Sahrens if (dmu_objset_type(os) == DMU_OST_ZVOL) 12234577Sahrens VERIFY(zvol_get_stats(os, nv) == 0); 12244577Sahrens } 12252676Seschrock error = put_nvlist(zc, nv); 12261356Seschrock nvlist_free(nv); 12271356Seschrock } 1228789Sahrens 1229789Sahrens dmu_objset_close(os); 1230789Sahrens return (error); 1231789Sahrens } 1232789Sahrens 12335498Stimh static int 12345498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 12355498Stimh { 12365498Stimh uint64_t value; 12375498Stimh int error; 12385498Stimh 12395498Stimh /* 12405498Stimh * zfs_get_zplprop() will either find a value or give us 12415498Stimh * the default value (if there is one). 12425498Stimh */ 12435498Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 12445498Stimh return (error); 12455498Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 12465498Stimh return (0); 12475498Stimh } 12485498Stimh 12495498Stimh /* 12505498Stimh * inputs: 12515498Stimh * zc_name name of filesystem 12525498Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 12535498Stimh * 12545498Stimh * outputs: 12555498Stimh * zc_nvlist_dst zpl property nvlist 12565498Stimh * zc_nvlist_dst_size size of zpl property nvlist 12575498Stimh */ 12585498Stimh static int 12595498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 12605498Stimh { 12615498Stimh objset_t *os; 12625498Stimh int err; 12635498Stimh 12646689Smaybee if (err = dmu_objset_open(zc->zc_name, 12656689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) 12665498Stimh return (err); 12675498Stimh 12685498Stimh dmu_objset_fast_stat(os, &zc->zc_objset_stats); 12695498Stimh 12705498Stimh /* 12715498Stimh * NB: nvl_add_zplprop() will read the objset contents, 12726689Smaybee * which we aren't supposed to do with a DS_MODE_USER 12736689Smaybee * hold, because it could be inconsistent. 12745498Stimh */ 12755498Stimh if (zc->zc_nvlist_dst != NULL && 12765498Stimh !zc->zc_objset_stats.dds_inconsistent && 12775498Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 12785498Stimh nvlist_t *nv; 12795498Stimh 12805498Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 12815498Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 12825498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 12835498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 12845498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 12855498Stimh err = put_nvlist(zc, nv); 12865498Stimh nvlist_free(nv); 12875498Stimh } else { 12885498Stimh err = ENOENT; 12895498Stimh } 12905498Stimh dmu_objset_close(os); 12915498Stimh return (err); 12925498Stimh } 12935498Stimh 12945367Sahrens /* 12955367Sahrens * inputs: 12965367Sahrens * zc_name name of filesystem 12975367Sahrens * zc_cookie zap cursor 12985367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 12995367Sahrens * 13005367Sahrens * outputs: 13015367Sahrens * zc_name name of next filesystem 13025367Sahrens * zc_objset_stats stats 13035367Sahrens * zc_nvlist_dst property nvlist 13045367Sahrens * zc_nvlist_dst_size size of property nvlist 13055367Sahrens */ 1306789Sahrens static int 1307789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1308789Sahrens { 1309885Sahrens objset_t *os; 1310789Sahrens int error; 1311789Sahrens char *p; 1312789Sahrens 13136689Smaybee if (error = dmu_objset_open(zc->zc_name, 13146689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) { 1315885Sahrens if (error == ENOENT) 1316885Sahrens error = ESRCH; 1317885Sahrens return (error); 1318789Sahrens } 1319789Sahrens 1320789Sahrens p = strrchr(zc->zc_name, '/'); 1321789Sahrens if (p == NULL || p[1] != '\0') 1322789Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1323789Sahrens p = zc->zc_name + strlen(zc->zc_name); 1324789Sahrens 1325789Sahrens do { 1326885Sahrens error = dmu_dir_list_next(os, 1327885Sahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 1328885Sahrens NULL, &zc->zc_cookie); 1329789Sahrens if (error == ENOENT) 1330789Sahrens error = ESRCH; 1331885Sahrens } while (error == 0 && !INGLOBALZONE(curproc) && 1332789Sahrens !zone_dataset_visible(zc->zc_name, NULL)); 13336689Smaybee dmu_objset_close(os); 1334789Sahrens 1335885Sahrens /* 1336885Sahrens * If it's a hidden dataset (ie. with a '$' in its name), don't 1337885Sahrens * try to get stats for it. Userland will skip over it. 1338885Sahrens */ 1339885Sahrens if (error == 0 && strchr(zc->zc_name, '$') == NULL) 1340885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1341789Sahrens 1342789Sahrens return (error); 1343789Sahrens } 1344789Sahrens 13455367Sahrens /* 13465367Sahrens * inputs: 13475367Sahrens * zc_name name of filesystem 13485367Sahrens * zc_cookie zap cursor 13495367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 13505367Sahrens * 13515367Sahrens * outputs: 13525367Sahrens * zc_name name of next snapshot 13535367Sahrens * zc_objset_stats stats 13545367Sahrens * zc_nvlist_dst property nvlist 13555367Sahrens * zc_nvlist_dst_size size of property nvlist 13565367Sahrens */ 1357789Sahrens static int 1358789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1359789Sahrens { 1360885Sahrens objset_t *os; 1361789Sahrens int error; 1362789Sahrens 13636689Smaybee error = dmu_objset_open(zc->zc_name, 13646689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os); 13656689Smaybee if (error) 13666689Smaybee return (error == ENOENT ? ESRCH : error); 1367789Sahrens 13681003Slling /* 13691003Slling * A dataset name of maximum length cannot have any snapshots, 13701003Slling * so exit immediately. 13711003Slling */ 13721003Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 1373885Sahrens dmu_objset_close(os); 13741003Slling return (ESRCH); 1375789Sahrens } 1376789Sahrens 1377885Sahrens error = dmu_snapshot_list_next(os, 1378885Sahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 13795663Sck153898 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 13806689Smaybee dmu_objset_close(os); 1381885Sahrens if (error == 0) 1382885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 13836689Smaybee else if (error == ENOENT) 13846689Smaybee error = ESRCH; 1385789Sahrens 13865367Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 13876689Smaybee if (error) 13885367Sahrens *strchr(zc->zc_name, '@') = '\0'; 1389789Sahrens return (error); 1390789Sahrens } 1391789Sahrens 13926423Sgw25295 int 13934787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl) 1394789Sahrens { 13952676Seschrock nvpair_t *elem; 13962676Seschrock int error; 13972676Seschrock uint64_t intval; 13982676Seschrock char *strval; 13992676Seschrock 14004543Smarks /* 14014543Smarks * First validate permission to set all of the properties 14024543Smarks */ 14032676Seschrock elem = NULL; 14042676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 14054670Sahrens const char *propname = nvpair_name(elem); 14064670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 14072676Seschrock 14085094Slling if (prop == ZPROP_INVAL) { 14092676Seschrock /* 14102676Seschrock * If this is a user-defined property, it must be a 14112676Seschrock * string, and there is no further validation to do. 14122676Seschrock */ 14132676Seschrock if (!zfs_prop_user(propname) || 14142676Seschrock nvpair_type(elem) != DATA_TYPE_STRING) 14152676Seschrock return (EINVAL); 14162676Seschrock 14175331Samw if (error = zfs_secpolicy_write_perms(name, 14185331Samw ZFS_DELEG_PERM_USERPROP, CRED())) 14194670Sahrens return (error); 14204543Smarks continue; 14212676Seschrock } 14222676Seschrock 14234787Sahrens if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 14244670Sahrens return (error); 14252676Seschrock 14264670Sahrens /* 14274670Sahrens * Check that this value is valid for this pool version 14284670Sahrens */ 14294670Sahrens switch (prop) { 14303886Sahl case ZFS_PROP_COMPRESSION: 14313886Sahl /* 14323886Sahl * If the user specified gzip compression, make sure 14333886Sahl * the SPA supports it. We ignore any errors here since 14343886Sahl * we'll catch them later. 14353886Sahl */ 14363886Sahl if (nvpair_type(elem) == DATA_TYPE_UINT64 && 14377042Sgw25295 nvpair_value_uint64(elem, &intval) == 0) { 14387042Sgw25295 if (intval >= ZIO_COMPRESS_GZIP_1 && 14397042Sgw25295 intval <= ZIO_COMPRESS_GZIP_9 && 14407184Stimh zfs_earlier_version(name, 14415331Samw SPA_VERSION_GZIP_COMPRESSION)) 14425331Samw return (ENOTSUP); 14437042Sgw25295 14447042Sgw25295 /* 14457042Sgw25295 * If this is a bootable dataset then 14467042Sgw25295 * verify that the compression algorithm 14477042Sgw25295 * is supported for booting. We must return 14487042Sgw25295 * something other than ENOTSUP since it 14497042Sgw25295 * implies a downrev pool version. 14507042Sgw25295 */ 14517042Sgw25295 if (zfs_is_bootfs(name) && 14527042Sgw25295 !BOOTFS_COMPRESS_VALID(intval)) 14537042Sgw25295 return (ERANGE); 14543886Sahl } 14553886Sahl break; 14564603Sahrens 14574603Sahrens case ZFS_PROP_COPIES: 14587184Stimh if (zfs_earlier_version(name, 14597184Stimh SPA_VERSION_DITTO_BLOCKS)) 14605331Samw return (ENOTSUP); 14614603Sahrens break; 14625977Smarks 14635977Smarks case ZFS_PROP_SHARESMB: 14646689Smaybee if (zpl_earlier_version(name, ZPL_VERSION_FUID)) 14655977Smarks return (ENOTSUP); 14665977Smarks break; 14674603Sahrens } 14685331Samw if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 14695331Samw return (error); 14704543Smarks } 14714543Smarks 14724543Smarks elem = NULL; 14734543Smarks while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 14744670Sahrens const char *propname = nvpair_name(elem); 14754670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 14764543Smarks 14775094Slling if (prop == ZPROP_INVAL) { 14784543Smarks VERIFY(nvpair_value_string(elem, &strval) == 0); 14794543Smarks error = dsl_prop_set(name, propname, 1, 14804543Smarks strlen(strval) + 1, strval); 14814543Smarks if (error == 0) 14824543Smarks continue; 14834543Smarks else 14844543Smarks return (error); 14854543Smarks } 14862676Seschrock 14872676Seschrock switch (prop) { 14882676Seschrock case ZFS_PROP_QUOTA: 14892676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 14904577Sahrens (error = dsl_dir_set_quota(name, intval)) != 0) 14912676Seschrock return (error); 14922676Seschrock break; 14932676Seschrock 14945378Sck153898 case ZFS_PROP_REFQUOTA: 14955378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 14965378Sck153898 (error = dsl_dataset_set_quota(name, intval)) != 0) 14975378Sck153898 return (error); 14985378Sck153898 break; 14995378Sck153898 15002676Seschrock case ZFS_PROP_RESERVATION: 15012676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15022676Seschrock (error = dsl_dir_set_reservation(name, 15032676Seschrock intval)) != 0) 15042676Seschrock return (error); 15052676Seschrock break; 1506789Sahrens 15075378Sck153898 case ZFS_PROP_REFRESERVATION: 15085378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15095378Sck153898 (error = dsl_dataset_set_reservation(name, 15105378Sck153898 intval)) != 0) 15115378Sck153898 return (error); 15125378Sck153898 break; 15135378Sck153898 15142676Seschrock case ZFS_PROP_VOLSIZE: 15152676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15164787Sahrens (error = zvol_set_volsize(name, 15174787Sahrens ddi_driver_major(zfs_dip), intval)) != 0) 15182676Seschrock return (error); 15192676Seschrock break; 15202676Seschrock 15212676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 15222676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15234577Sahrens (error = zvol_set_volblocksize(name, intval)) != 0) 15244577Sahrens return (error); 15254577Sahrens break; 15264577Sahrens 15274577Sahrens case ZFS_PROP_VERSION: 15284577Sahrens if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15294577Sahrens (error = zfs_set_version(name, intval)) != 0) 15302676Seschrock return (error); 15312676Seschrock break; 15322676Seschrock 15332676Seschrock default: 15342676Seschrock if (nvpair_type(elem) == DATA_TYPE_STRING) { 15352676Seschrock if (zfs_prop_get_type(prop) != 15364787Sahrens PROP_TYPE_STRING) 15372676Seschrock return (EINVAL); 15382717Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 15392717Seschrock if ((error = dsl_prop_set(name, 15402676Seschrock nvpair_name(elem), 1, strlen(strval) + 1, 15412717Seschrock strval)) != 0) 15422717Seschrock return (error); 15432676Seschrock } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 15442885Sahrens const char *unused; 15452885Sahrens 15462717Seschrock VERIFY(nvpair_value_uint64(elem, &intval) == 0); 15472676Seschrock 15482676Seschrock switch (zfs_prop_get_type(prop)) { 15494787Sahrens case PROP_TYPE_NUMBER: 15502676Seschrock break; 15514787Sahrens case PROP_TYPE_STRING: 15522717Seschrock return (EINVAL); 15534787Sahrens case PROP_TYPE_INDEX: 15542717Seschrock if (zfs_prop_index_to_string(prop, 15552717Seschrock intval, &unused) != 0) 15562717Seschrock return (EINVAL); 15572676Seschrock break; 15582676Seschrock default: 15594577Sahrens cmn_err(CE_PANIC, 15604577Sahrens "unknown property type"); 15612676Seschrock break; 15622676Seschrock } 15632676Seschrock 15642717Seschrock if ((error = dsl_prop_set(name, propname, 15652717Seschrock 8, 1, &intval)) != 0) 15662717Seschrock return (error); 15672676Seschrock } else { 15682676Seschrock return (EINVAL); 15692676Seschrock } 15702676Seschrock break; 15712676Seschrock } 15722676Seschrock } 15732676Seschrock 15742676Seschrock return (0); 1575789Sahrens } 1576789Sahrens 15775367Sahrens /* 15785367Sahrens * inputs: 15795367Sahrens * zc_name name of filesystem 15805367Sahrens * zc_value name of property to inherit 15815367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 15825367Sahrens * 15835367Sahrens * outputs: none 15845367Sahrens */ 1585789Sahrens static int 15862676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 1587789Sahrens { 15882676Seschrock nvlist_t *nvl; 15892676Seschrock int error; 1590789Sahrens 15915094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 15925094Slling &nvl)) != 0) 15932676Seschrock return (error); 15942676Seschrock 15954787Sahrens error = zfs_set_prop_nvlist(zc->zc_name, nvl); 15964543Smarks 15972676Seschrock nvlist_free(nvl); 15982676Seschrock return (error); 1599789Sahrens } 1600789Sahrens 16015367Sahrens /* 16025367Sahrens * inputs: 16035367Sahrens * zc_name name of filesystem 16045367Sahrens * zc_value name of property to inherit 16055367Sahrens * 16065367Sahrens * outputs: none 16075367Sahrens */ 1608789Sahrens static int 16094849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 16104849Sahrens { 16114849Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 16124849Sahrens return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL)); 16134849Sahrens } 16144849Sahrens 16154849Sahrens static int 16164098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 16173912Slling { 16185094Slling nvlist_t *props; 16193912Slling spa_t *spa; 16205094Slling int error; 16213912Slling 16225094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 16235094Slling &props))) 16243912Slling return (error); 16253912Slling 16263912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 16275094Slling nvlist_free(props); 16283912Slling return (error); 16293912Slling } 16303912Slling 16315094Slling error = spa_prop_set(spa, props); 16323912Slling 16335094Slling nvlist_free(props); 16343912Slling spa_close(spa, FTAG); 16353912Slling 16363912Slling return (error); 16373912Slling } 16383912Slling 16393912Slling static int 16404098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 16413912Slling { 16423912Slling spa_t *spa; 16433912Slling int error; 16443912Slling nvlist_t *nvp = NULL; 16453912Slling 16463912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 16473912Slling return (error); 16483912Slling 16495094Slling error = spa_prop_get(spa, &nvp); 16503912Slling 16513912Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 16523912Slling error = put_nvlist(zc, nvp); 16533912Slling else 16543912Slling error = EFAULT; 16553912Slling 16563912Slling spa_close(spa, FTAG); 16573912Slling 16583912Slling if (nvp) 16593912Slling nvlist_free(nvp); 16603912Slling return (error); 16613912Slling } 16623912Slling 16633912Slling static int 16644543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 16654543Smarks { 16664543Smarks nvlist_t *nvp; 16674543Smarks int error; 16684543Smarks uint32_t uid; 16694543Smarks uint32_t gid; 16704543Smarks uint32_t *groups; 16714543Smarks uint_t group_cnt; 16724543Smarks cred_t *usercred; 16734543Smarks 16745094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 16755094Slling &nvp)) != 0) { 16764543Smarks return (error); 16774543Smarks } 16784543Smarks 16794543Smarks if ((error = nvlist_lookup_uint32(nvp, 16804543Smarks ZFS_DELEG_PERM_UID, &uid)) != 0) { 16814543Smarks nvlist_free(nvp); 16824543Smarks return (EPERM); 16834543Smarks } 16844543Smarks 16854543Smarks if ((error = nvlist_lookup_uint32(nvp, 16864543Smarks ZFS_DELEG_PERM_GID, &gid)) != 0) { 16874543Smarks nvlist_free(nvp); 16884543Smarks return (EPERM); 16894543Smarks } 16904543Smarks 16914543Smarks if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 16924543Smarks &groups, &group_cnt)) != 0) { 16934543Smarks nvlist_free(nvp); 16944543Smarks return (EPERM); 16954543Smarks } 16964543Smarks usercred = cralloc(); 16974543Smarks if ((crsetugid(usercred, uid, gid) != 0) || 16984543Smarks (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 16994543Smarks nvlist_free(nvp); 17004543Smarks crfree(usercred); 17014543Smarks return (EPERM); 17024543Smarks } 17034543Smarks nvlist_free(nvp); 17044543Smarks error = dsl_deleg_access(zc->zc_name, 17054787Sahrens zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred); 17064543Smarks crfree(usercred); 17074543Smarks return (error); 17084543Smarks } 17094543Smarks 17105367Sahrens /* 17115367Sahrens * inputs: 17125367Sahrens * zc_name name of filesystem 17135367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 17145367Sahrens * zc_perm_action allow/unallow flag 17155367Sahrens * 17165367Sahrens * outputs: none 17175367Sahrens */ 17184543Smarks static int 17194543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 17204543Smarks { 17214543Smarks int error; 17224543Smarks nvlist_t *fsaclnv = NULL; 17234543Smarks 17245094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 17255094Slling &fsaclnv)) != 0) 17264543Smarks return (error); 17274543Smarks 17284543Smarks /* 17294543Smarks * Verify nvlist is constructed correctly 17304543Smarks */ 17314543Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 17324543Smarks nvlist_free(fsaclnv); 17334543Smarks return (EINVAL); 17344543Smarks } 17354543Smarks 17364543Smarks /* 17374543Smarks * If we don't have PRIV_SYS_MOUNT, then validate 17384543Smarks * that user is allowed to hand out each permission in 17394543Smarks * the nvlist(s) 17404543Smarks */ 17414543Smarks 17424787Sahrens error = secpolicy_zfs(CRED()); 17434543Smarks if (error) { 17444787Sahrens if (zc->zc_perm_action == B_FALSE) { 17454787Sahrens error = dsl_deleg_can_allow(zc->zc_name, 17464787Sahrens fsaclnv, CRED()); 17474787Sahrens } else { 17484787Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 17494787Sahrens fsaclnv, CRED()); 17504787Sahrens } 17514543Smarks } 17524543Smarks 17534543Smarks if (error == 0) 17544543Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 17554543Smarks 17564543Smarks nvlist_free(fsaclnv); 17574543Smarks return (error); 17584543Smarks } 17594543Smarks 17605367Sahrens /* 17615367Sahrens * inputs: 17625367Sahrens * zc_name name of filesystem 17635367Sahrens * 17645367Sahrens * outputs: 17655367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 17665367Sahrens */ 17674543Smarks static int 17684543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 17694543Smarks { 17704543Smarks nvlist_t *nvp; 17714543Smarks int error; 17724543Smarks 17734543Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 17744543Smarks error = put_nvlist(zc, nvp); 17754543Smarks nvlist_free(nvp); 17764543Smarks } 17774543Smarks 17784543Smarks return (error); 17794543Smarks } 17804543Smarks 17815367Sahrens /* 17825367Sahrens * inputs: 17835367Sahrens * zc_name name of volume 17845367Sahrens * 17855367Sahrens * outputs: none 17865367Sahrens */ 17874543Smarks static int 1788789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc) 1789789Sahrens { 17904787Sahrens return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip))); 1791789Sahrens } 1792789Sahrens 17935367Sahrens /* 17945367Sahrens * inputs: 17955367Sahrens * zc_name name of volume 17965367Sahrens * 17975367Sahrens * outputs: none 17985367Sahrens */ 1799789Sahrens static int 1800789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc) 1801789Sahrens { 18022676Seschrock return (zvol_remove_minor(zc->zc_name)); 1803789Sahrens } 1804789Sahrens 1805789Sahrens /* 1806789Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 1807789Sahrens * or NULL if no suitable entry is found. The caller of this routine 1808789Sahrens * is responsible for releasing the returned vfs pointer. 1809789Sahrens */ 1810789Sahrens static vfs_t * 1811789Sahrens zfs_get_vfs(const char *resource) 1812789Sahrens { 1813789Sahrens struct vfs *vfsp; 1814789Sahrens struct vfs *vfs_found = NULL; 1815789Sahrens 1816789Sahrens vfs_list_read_lock(); 1817789Sahrens vfsp = rootvfs; 1818789Sahrens do { 1819789Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 1820789Sahrens VFS_HOLD(vfsp); 1821789Sahrens vfs_found = vfsp; 1822789Sahrens break; 1823789Sahrens } 1824789Sahrens vfsp = vfsp->vfs_next; 1825789Sahrens } while (vfsp != rootvfs); 1826789Sahrens vfs_list_unlock(); 1827789Sahrens return (vfs_found); 1828789Sahrens } 1829789Sahrens 18304543Smarks /* ARGSUSED */ 1831789Sahrens static void 18324543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 1833789Sahrens { 18345331Samw zfs_creat_t *zct = arg; 18355498Stimh 18365498Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 18375331Samw } 18385331Samw 18395498Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 18405498Stimh 18415331Samw /* 18425498Stimh * inputs: 18437184Stimh * createprops list of properties requested by creator 18447184Stimh * default_zplver zpl version to use if unspecified in createprops 18457184Stimh * fuids_ok fuids allowed in this version of the spa? 18467184Stimh * os parent objset pointer (NULL if root fs) 18475331Samw * 18485498Stimh * outputs: 18495498Stimh * zplprops values for the zplprops we attach to the master node object 18507184Stimh * is_ci true if requested file system will be purely case-insensitive 18515331Samw * 18525498Stimh * Determine the settings for utf8only, normalization and 18535498Stimh * casesensitivity. Specific values may have been requested by the 18545498Stimh * creator and/or we can inherit values from the parent dataset. If 18555498Stimh * the file system is of too early a vintage, a creator can not 18565498Stimh * request settings for these properties, even if the requested 18575498Stimh * setting is the default value. We don't actually want to create dsl 18585498Stimh * properties for these, so remove them from the source nvlist after 18595498Stimh * processing. 18605331Samw */ 18615331Samw static int 18627184Stimh zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver, 18637184Stimh boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops, 18647184Stimh boolean_t *is_ci) 18655331Samw { 18667184Stimh uint64_t zplver = default_zplver; 18675498Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 18685498Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 18695498Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 18705498Stimh 18715498Stimh ASSERT(zplprops != NULL); 18725498Stimh 18735375Stimh /* 18745498Stimh * Pull out creator prop choices, if any. 18755375Stimh */ 18765498Stimh if (createprops) { 18775498Stimh (void) nvlist_lookup_uint64(createprops, 18787184Stimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 18797184Stimh (void) nvlist_lookup_uint64(createprops, 18805498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 18815498Stimh (void) nvlist_remove_all(createprops, 18825498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 18835498Stimh (void) nvlist_lookup_uint64(createprops, 18845498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 18855498Stimh (void) nvlist_remove_all(createprops, 18865498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 18875498Stimh (void) nvlist_lookup_uint64(createprops, 18885498Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 18895498Stimh (void) nvlist_remove_all(createprops, 18905498Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 18915331Samw } 18925331Samw 18935375Stimh /* 18947184Stimh * If the zpl version requested is whacky or the file system 18957184Stimh * or pool is version is too "young" to support normalization 18967184Stimh * and the creator tried to set a value for one of the props, 18977184Stimh * error out. 18985498Stimh */ 18997184Stimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 19007184Stimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 19017184Stimh (zplver < ZPL_VERSION_NORMALIZATION && 19025498Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 19037184Stimh sense != ZFS_PROP_UNDEFINED))) 19045498Stimh return (ENOTSUP); 19055498Stimh 19065498Stimh /* 19075498Stimh * Put the version in the zplprops 19085498Stimh */ 19095498Stimh VERIFY(nvlist_add_uint64(zplprops, 19105498Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 19115498Stimh 19125498Stimh if (norm == ZFS_PROP_UNDEFINED) 19135498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 19145498Stimh VERIFY(nvlist_add_uint64(zplprops, 19155498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 19165498Stimh 19175498Stimh /* 19185498Stimh * If we're normalizing, names must always be valid UTF-8 strings. 19195498Stimh */ 19205498Stimh if (norm) 19215498Stimh u8 = 1; 19225498Stimh if (u8 == ZFS_PROP_UNDEFINED) 19235498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 19245498Stimh VERIFY(nvlist_add_uint64(zplprops, 19255498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 19265498Stimh 19275498Stimh if (sense == ZFS_PROP_UNDEFINED) 19285498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 19295498Stimh VERIFY(nvlist_add_uint64(zplprops, 19305498Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 19315498Stimh 19326492Stimh if (is_ci) 19336492Stimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 19346492Stimh 19357184Stimh return (0); 19367184Stimh } 19377184Stimh 19387184Stimh static int 19397184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 19407184Stimh nvlist_t *zplprops, boolean_t *is_ci) 19417184Stimh { 19427184Stimh boolean_t fuids_ok = B_TRUE; 19437184Stimh uint64_t zplver = ZPL_VERSION; 19447184Stimh objset_t *os = NULL; 19457184Stimh char parentname[MAXNAMELEN]; 19467184Stimh char *cp; 19477184Stimh int error; 19487184Stimh 19497184Stimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 19507184Stimh cp = strrchr(parentname, '/'); 19517184Stimh ASSERT(cp != NULL); 19527184Stimh cp[0] = '\0'; 19537184Stimh 19547184Stimh if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) { 19557184Stimh zplver = ZPL_VERSION_FUID - 1; 19567184Stimh fuids_ok = B_FALSE; 19577184Stimh } 19587184Stimh 19597184Stimh /* 19607184Stimh * Open parent object set so we can inherit zplprop values. 19617184Stimh */ 19627184Stimh if ((error = dmu_objset_open(parentname, DMU_OST_ANY, 19637184Stimh DS_MODE_USER | DS_MODE_READONLY, &os)) != 0) 19647184Stimh return (error); 19657184Stimh 19667184Stimh error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops, 19677184Stimh zplprops, is_ci); 19685498Stimh dmu_objset_close(os); 19697184Stimh return (error); 19707184Stimh } 19717184Stimh 19727184Stimh static int 19737184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 19747184Stimh nvlist_t *zplprops, boolean_t *is_ci) 19757184Stimh { 19767184Stimh boolean_t fuids_ok = B_TRUE; 19777184Stimh uint64_t zplver = ZPL_VERSION; 19787184Stimh int error; 19797184Stimh 19807184Stimh if (spa_vers < SPA_VERSION_FUID) { 19817184Stimh zplver = ZPL_VERSION_FUID - 1; 19827184Stimh fuids_ok = B_FALSE; 19837184Stimh } 19847184Stimh 19857184Stimh error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops, 19867184Stimh zplprops, is_ci); 19877184Stimh return (error); 1988789Sahrens } 1989789Sahrens 19905367Sahrens /* 19915367Sahrens * inputs: 19925367Sahrens * zc_objset_type type of objset to create (fs vs zvol) 19935367Sahrens * zc_name name of new objset 19945367Sahrens * zc_value name of snapshot to clone from (may be empty) 19955367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 19965367Sahrens * 19975498Stimh * outputs: none 19985367Sahrens */ 1999789Sahrens static int 2000789Sahrens zfs_ioc_create(zfs_cmd_t *zc) 2001789Sahrens { 2002789Sahrens objset_t *clone; 2003789Sahrens int error = 0; 20045331Samw zfs_creat_t zct; 20054543Smarks nvlist_t *nvprops = NULL; 20064543Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2007789Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2008789Sahrens 2009789Sahrens switch (type) { 2010789Sahrens 2011789Sahrens case DMU_OST_ZFS: 2012789Sahrens cbfunc = zfs_create_cb; 2013789Sahrens break; 2014789Sahrens 2015789Sahrens case DMU_OST_ZVOL: 2016789Sahrens cbfunc = zvol_create_cb; 2017789Sahrens break; 2018789Sahrens 2019789Sahrens default: 20202199Sahrens cbfunc = NULL; 20216423Sgw25295 break; 20222199Sahrens } 20235326Sek110237 if (strchr(zc->zc_name, '@') || 20245326Sek110237 strchr(zc->zc_name, '%')) 2025789Sahrens return (EINVAL); 2026789Sahrens 20272676Seschrock if (zc->zc_nvlist_src != NULL && 20285094Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 20295094Slling &nvprops)) != 0) 20302676Seschrock return (error); 20312676Seschrock 20325498Stimh zct.zct_zplprops = NULL; 20335331Samw zct.zct_props = nvprops; 20345331Samw 20352676Seschrock if (zc->zc_value[0] != '\0') { 2036789Sahrens /* 2037789Sahrens * We're creating a clone of an existing snapshot. 2038789Sahrens */ 20392676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 20402676Seschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 20414543Smarks nvlist_free(nvprops); 2042789Sahrens return (EINVAL); 20432676Seschrock } 2044789Sahrens 20452676Seschrock error = dmu_objset_open(zc->zc_value, type, 20466689Smaybee DS_MODE_USER | DS_MODE_READONLY, &clone); 20472676Seschrock if (error) { 20484543Smarks nvlist_free(nvprops); 2049789Sahrens return (error); 20502676Seschrock } 20516492Stimh 20526492Stimh error = dmu_objset_create(zc->zc_name, type, clone, 0, 20536492Stimh NULL, NULL); 20545331Samw if (error) { 20555331Samw dmu_objset_close(clone); 20565331Samw nvlist_free(nvprops); 20575331Samw return (error); 20585331Samw } 2059789Sahrens dmu_objset_close(clone); 2060789Sahrens } else { 20616492Stimh boolean_t is_insensitive = B_FALSE; 20626492Stimh 20632676Seschrock if (cbfunc == NULL) { 20644543Smarks nvlist_free(nvprops); 20652199Sahrens return (EINVAL); 20662676Seschrock } 20672676Seschrock 2068789Sahrens if (type == DMU_OST_ZVOL) { 20692676Seschrock uint64_t volsize, volblocksize; 20702676Seschrock 20714543Smarks if (nvprops == NULL || 20724543Smarks nvlist_lookup_uint64(nvprops, 20732676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 20742676Seschrock &volsize) != 0) { 20754543Smarks nvlist_free(nvprops); 20762676Seschrock return (EINVAL); 20772676Seschrock } 20782676Seschrock 20794543Smarks if ((error = nvlist_lookup_uint64(nvprops, 20802676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 20812676Seschrock &volblocksize)) != 0 && error != ENOENT) { 20824543Smarks nvlist_free(nvprops); 20832676Seschrock return (EINVAL); 20842676Seschrock } 20851133Seschrock 20862676Seschrock if (error != 0) 20872676Seschrock volblocksize = zfs_prop_default_numeric( 20882676Seschrock ZFS_PROP_VOLBLOCKSIZE); 20892676Seschrock 20902676Seschrock if ((error = zvol_check_volblocksize( 20912676Seschrock volblocksize)) != 0 || 20922676Seschrock (error = zvol_check_volsize(volsize, 20932676Seschrock volblocksize)) != 0) { 20944543Smarks nvlist_free(nvprops); 2095789Sahrens return (error); 20962676Seschrock } 20974577Sahrens } else if (type == DMU_OST_ZFS) { 20985331Samw int error; 20995331Samw 21005498Stimh /* 21015331Samw * We have to have normalization and 21025331Samw * case-folding flags correct when we do the 21035331Samw * file system creation, so go figure them out 21045498Stimh * now. 21055331Samw */ 21065498Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 21075498Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 21085498Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 21097184Stimh zct.zct_zplprops, &is_insensitive); 21105331Samw if (error != 0) { 21115331Samw nvlist_free(nvprops); 21125498Stimh nvlist_free(zct.zct_zplprops); 21135331Samw return (error); 21144577Sahrens } 21152676Seschrock } 21166492Stimh error = dmu_objset_create(zc->zc_name, type, NULL, 21176492Stimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 21185498Stimh nvlist_free(zct.zct_zplprops); 2119789Sahrens } 21202676Seschrock 21212676Seschrock /* 21222676Seschrock * It would be nice to do this atomically. 21232676Seschrock */ 21242676Seschrock if (error == 0) { 21254787Sahrens if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0) 21262676Seschrock (void) dmu_objset_destroy(zc->zc_name); 21272676Seschrock } 21284543Smarks nvlist_free(nvprops); 2129789Sahrens return (error); 2130789Sahrens } 2131789Sahrens 21325367Sahrens /* 21335367Sahrens * inputs: 21345367Sahrens * zc_name name of filesystem 21355367Sahrens * zc_value short name of snapshot 21365367Sahrens * zc_cookie recursive flag 21375367Sahrens * 21385367Sahrens * outputs: none 21395367Sahrens */ 2140789Sahrens static int 21412199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 21422199Sahrens { 21432676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 21442199Sahrens return (EINVAL); 21452199Sahrens return (dmu_objset_snapshot(zc->zc_name, 21462676Seschrock zc->zc_value, zc->zc_cookie)); 21472199Sahrens } 21482199Sahrens 21494007Smmusante int 21502199Sahrens zfs_unmount_snap(char *name, void *arg) 2151789Sahrens { 21522417Sahrens vfs_t *vfsp = NULL; 21532199Sahrens 21546689Smaybee if (arg) { 21556689Smaybee char *snapname = arg; 21566689Smaybee int len = strlen(name) + strlen(snapname) + 2; 21576689Smaybee char *buf = kmem_alloc(len, KM_SLEEP); 21586689Smaybee 21596689Smaybee (void) strcpy(buf, name); 21606689Smaybee (void) strcat(buf, "@"); 21616689Smaybee (void) strcat(buf, snapname); 21626689Smaybee vfsp = zfs_get_vfs(buf); 21636689Smaybee kmem_free(buf, len); 21642417Sahrens } else if (strchr(name, '@')) { 21652199Sahrens vfsp = zfs_get_vfs(name); 21662199Sahrens } 21672199Sahrens 21682199Sahrens if (vfsp) { 21692199Sahrens /* 21702199Sahrens * Always force the unmount for snapshots. 21712199Sahrens */ 21722199Sahrens int flag = MS_FORCE; 2173789Sahrens int err; 2174789Sahrens 21752199Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 21762199Sahrens VFS_RELE(vfsp); 21772199Sahrens return (err); 21782199Sahrens } 21792199Sahrens VFS_RELE(vfsp); 21802199Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 21812199Sahrens return (err); 21822199Sahrens } 21832199Sahrens return (0); 21842199Sahrens } 21852199Sahrens 21865367Sahrens /* 21875367Sahrens * inputs: 21885367Sahrens * zc_name name of filesystem 21895367Sahrens * zc_value short name of snapshot 21905367Sahrens * 21915367Sahrens * outputs: none 21925367Sahrens */ 21932199Sahrens static int 21942199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 21952199Sahrens { 21962199Sahrens int err; 2197789Sahrens 21982676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 21992199Sahrens return (EINVAL); 22002199Sahrens err = dmu_objset_find(zc->zc_name, 22012676Seschrock zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 22022199Sahrens if (err) 22032199Sahrens return (err); 22042676Seschrock return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value)); 22052199Sahrens } 22062199Sahrens 22075367Sahrens /* 22085367Sahrens * inputs: 22095367Sahrens * zc_name name of dataset to destroy 22105367Sahrens * zc_objset_type type of objset 22115367Sahrens * 22125367Sahrens * outputs: none 22135367Sahrens */ 22142199Sahrens static int 22152199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 22162199Sahrens { 22172199Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 22182199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 22192199Sahrens if (err) 22202199Sahrens return (err); 2221789Sahrens } 2222789Sahrens 2223789Sahrens return (dmu_objset_destroy(zc->zc_name)); 2224789Sahrens } 2225789Sahrens 22265367Sahrens /* 22275367Sahrens * inputs: 22285446Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 22295367Sahrens * 22305367Sahrens * outputs: none 22315367Sahrens */ 2232789Sahrens static int 2233789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 2234789Sahrens { 22355446Sahrens objset_t *os; 22365446Sahrens int error; 22375446Sahrens zfsvfs_t *zfsvfs = NULL; 22385446Sahrens 22395446Sahrens /* 22405446Sahrens * Get the zfsvfs for the receiving objset. There 22415446Sahrens * won't be one if we're operating on a zvol, if the 22425446Sahrens * objset doesn't exist yet, or is not mounted. 22435446Sahrens */ 22446689Smaybee error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os); 22455446Sahrens if (error) 22465446Sahrens return (error); 22475446Sahrens 22485446Sahrens if (dmu_objset_type(os) == DMU_OST_ZFS) { 22495446Sahrens mutex_enter(&os->os->os_user_ptr_lock); 22505446Sahrens zfsvfs = dmu_objset_get_user(os); 22515446Sahrens if (zfsvfs != NULL) 22525446Sahrens VFS_HOLD(zfsvfs->z_vfs); 22535446Sahrens mutex_exit(&os->os->os_user_ptr_lock); 22545446Sahrens } 22555446Sahrens 22565446Sahrens if (zfsvfs != NULL) { 22575446Sahrens char osname[MAXNAMELEN]; 22585446Sahrens int mode; 22595446Sahrens 22606083Sek110237 error = zfs_suspend_fs(zfsvfs, osname, &mode); 22616083Sek110237 if (error == 0) { 22626083Sek110237 int resume_err; 22636083Sek110237 22646083Sek110237 ASSERT(strcmp(osname, zc->zc_name) == 0); 22656083Sek110237 error = dmu_objset_rollback(os); 22666083Sek110237 resume_err = zfs_resume_fs(zfsvfs, osname, mode); 22676083Sek110237 error = error ? error : resume_err; 22686083Sek110237 } else { 22696083Sek110237 dmu_objset_close(os); 22706083Sek110237 } 22715446Sahrens VFS_RELE(zfsvfs->z_vfs); 22725446Sahrens } else { 22735446Sahrens error = dmu_objset_rollback(os); 22745446Sahrens } 22756689Smaybee /* Note, the dmu_objset_rollback() releases the objset for us. */ 22765446Sahrens 22775446Sahrens return (error); 2278789Sahrens } 2279789Sahrens 22805367Sahrens /* 22815367Sahrens * inputs: 22825367Sahrens * zc_name old name of dataset 22835367Sahrens * zc_value new name of dataset 22845367Sahrens * zc_cookie recursive flag (only valid for snapshots) 22855367Sahrens * 22865367Sahrens * outputs: none 22875367Sahrens */ 2288789Sahrens static int 2289789Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 2290789Sahrens { 22914490Svb160487 boolean_t recursive = zc->zc_cookie & 1; 22924007Smmusante 22932676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 22945326Sek110237 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 22955326Sek110237 strchr(zc->zc_value, '%')) 2296789Sahrens return (EINVAL); 2297789Sahrens 22984007Smmusante /* 22994007Smmusante * Unmount snapshot unless we're doing a recursive rename, 23004007Smmusante * in which case the dataset code figures out which snapshots 23014007Smmusante * to unmount. 23024007Smmusante */ 23034007Smmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 2304789Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 23052199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 23062199Sahrens if (err) 23072199Sahrens return (err); 2308789Sahrens } 23094007Smmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 2310789Sahrens } 2311789Sahrens 23126689Smaybee static void 23136689Smaybee clear_props(char *dataset, nvlist_t *props) 23146689Smaybee { 23156689Smaybee zfs_cmd_t *zc; 23166689Smaybee nvpair_t *prop; 23176689Smaybee 23186689Smaybee if (props == NULL) 23196689Smaybee return; 23206689Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 23216689Smaybee (void) strcpy(zc->zc_name, dataset); 23226689Smaybee for (prop = nvlist_next_nvpair(props, NULL); prop; 23236689Smaybee prop = nvlist_next_nvpair(props, prop)) { 23246689Smaybee (void) strcpy(zc->zc_value, nvpair_name(prop)); 23256689Smaybee if (zfs_secpolicy_inherit(zc, CRED()) == 0) 23266689Smaybee (void) zfs_ioc_inherit_prop(zc); 23276689Smaybee } 23286689Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 23296689Smaybee } 23306689Smaybee 23315367Sahrens /* 23325367Sahrens * inputs: 23335367Sahrens * zc_name name of containing filesystem 23345367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 23355367Sahrens * zc_value name of snapshot to create 23365367Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 23375367Sahrens * zc_cookie file descriptor to recv from 23385367Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 23395367Sahrens * zc_guid force flag 23405367Sahrens * 23415367Sahrens * outputs: 23425367Sahrens * zc_cookie number of bytes read 23435367Sahrens */ 2344789Sahrens static int 23455367Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 2346789Sahrens { 2347789Sahrens file_t *fp; 23485326Sek110237 objset_t *os; 23495367Sahrens dmu_recv_cookie_t drc; 23505326Sek110237 zfsvfs_t *zfsvfs = NULL; 23515326Sek110237 boolean_t force = (boolean_t)zc->zc_guid; 2352789Sahrens int error, fd; 23535367Sahrens offset_t off; 23545367Sahrens nvlist_t *props = NULL; 23556689Smaybee nvlist_t *origprops = NULL; 23565367Sahrens objset_t *origin = NULL; 23575367Sahrens char *tosnap; 23585367Sahrens char tofs[ZFS_MAXNAMELEN]; 2359789Sahrens 23603265Sahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 23615326Sek110237 strchr(zc->zc_value, '@') == NULL || 23625326Sek110237 strchr(zc->zc_value, '%')) 23633265Sahrens return (EINVAL); 23643265Sahrens 23655367Sahrens (void) strcpy(tofs, zc->zc_value); 23665367Sahrens tosnap = strchr(tofs, '@'); 23675367Sahrens *tosnap = '\0'; 23685367Sahrens tosnap++; 23695367Sahrens 23705367Sahrens if (zc->zc_nvlist_src != NULL && 23715367Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 23725367Sahrens &props)) != 0) 23735367Sahrens return (error); 23745367Sahrens 2375789Sahrens fd = zc->zc_cookie; 2376789Sahrens fp = getf(fd); 23775367Sahrens if (fp == NULL) { 23785367Sahrens nvlist_free(props); 2379789Sahrens return (EBADF); 23805367Sahrens } 23815326Sek110237 23826689Smaybee if (dmu_objset_open(tofs, DMU_OST_ANY, 23836689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 23846689Smaybee /* 23856689Smaybee * Try to get the zfsvfs for the receiving objset. 23866689Smaybee * There won't be one if we're operating on a zvol, 23876689Smaybee * if the objset doesn't exist yet, or is not mounted. 23886689Smaybee */ 23895446Sahrens mutex_enter(&os->os->os_user_ptr_lock); 23906689Smaybee if (zfsvfs = dmu_objset_get_user(os)) { 23916083Sek110237 if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) { 23926689Smaybee mutex_exit(&os->os->os_user_ptr_lock); 23936083Sek110237 dmu_objset_close(os); 23946689Smaybee zfsvfs = NULL; 23956689Smaybee error = EBUSY; 23966689Smaybee goto out; 23976083Sek110237 } 23986689Smaybee VFS_HOLD(zfsvfs->z_vfs); 23996083Sek110237 } 24006689Smaybee mutex_exit(&os->os->os_user_ptr_lock); 24016689Smaybee 24026689Smaybee /* 24036689Smaybee * If new properties are supplied, they are to completely 24046689Smaybee * replace the existing ones, so stash away the existing ones. 24056689Smaybee */ 24066689Smaybee if (props) 24076689Smaybee (void) dsl_prop_get_all(os, &origprops, TRUE); 24086689Smaybee 24095326Sek110237 dmu_objset_close(os); 24105326Sek110237 } 24115326Sek110237 24125367Sahrens if (zc->zc_string[0]) { 24135367Sahrens error = dmu_objset_open(zc->zc_string, DMU_OST_ANY, 24146689Smaybee DS_MODE_USER | DS_MODE_READONLY, &origin); 24156689Smaybee if (error) 24166689Smaybee goto out; 24175367Sahrens } 24185367Sahrens 24195367Sahrens error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record, 24205367Sahrens force, origin, zfsvfs != NULL, &drc); 24215367Sahrens if (origin) 24225367Sahrens dmu_objset_close(origin); 24236689Smaybee if (error) 24246689Smaybee goto out; 24255326Sek110237 24265326Sek110237 /* 24276689Smaybee * Reset properties. We do this before we receive the stream 24286689Smaybee * so that the properties are applied to the new data. 24295326Sek110237 */ 24305367Sahrens if (props) { 24316689Smaybee clear_props(tofs, origprops); 24326689Smaybee /* 24336689Smaybee * XXX - Note, this is all-or-nothing; should be best-effort. 24346689Smaybee */ 24356689Smaybee (void) zfs_set_prop_nvlist(tofs, props); 24365367Sahrens } 24375367Sahrens 24385367Sahrens off = fp->f_offset; 24395367Sahrens error = dmu_recv_stream(&drc, fp->f_vnode, &off); 24405367Sahrens 24416689Smaybee if (error == 0 && zfsvfs) { 24426689Smaybee char osname[MAXNAMELEN]; 24436689Smaybee int mode; 24446689Smaybee 24456689Smaybee /* online recv */ 24466689Smaybee error = zfs_suspend_fs(zfsvfs, osname, &mode); 24476689Smaybee if (error == 0) { 24486689Smaybee int resume_err; 24496689Smaybee 24506689Smaybee error = dmu_recv_end(&drc); 24516689Smaybee resume_err = zfs_resume_fs(zfsvfs, osname, mode); 24526689Smaybee error = error ? error : resume_err; 24535367Sahrens } else { 24546689Smaybee dmu_recv_abort_cleanup(&drc); 24555367Sahrens } 24566689Smaybee } else if (error == 0) { 24576689Smaybee error = dmu_recv_end(&drc); 24586083Sek110237 } 24595367Sahrens 24605367Sahrens zc->zc_cookie = off - fp->f_offset; 24615367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 24625367Sahrens fp->f_offset = off; 24632885Sahrens 24646689Smaybee /* 24656689Smaybee * On error, restore the original props. 24666689Smaybee */ 24676689Smaybee if (error && props) { 24686689Smaybee clear_props(tofs, props); 24696689Smaybee (void) zfs_set_prop_nvlist(tofs, origprops); 24706689Smaybee } 24716689Smaybee out: 24726689Smaybee if (zfsvfs) { 24736689Smaybee mutex_exit(&zfsvfs->z_online_recv_lock); 24746689Smaybee VFS_RELE(zfsvfs->z_vfs); 24756689Smaybee } 24766689Smaybee nvlist_free(props); 24776689Smaybee nvlist_free(origprops); 2478789Sahrens releasef(fd); 2479789Sahrens return (error); 2480789Sahrens } 2481789Sahrens 24825367Sahrens /* 24835367Sahrens * inputs: 24845367Sahrens * zc_name name of snapshot to send 24855367Sahrens * zc_value short name of incremental fromsnap (may be empty) 24865367Sahrens * zc_cookie file descriptor to send stream to 24875367Sahrens * zc_obj fromorigin flag (mutually exclusive with zc_value) 24885367Sahrens * 24895367Sahrens * outputs: none 24905367Sahrens */ 2491789Sahrens static int 24925367Sahrens zfs_ioc_send(zfs_cmd_t *zc) 2493789Sahrens { 2494789Sahrens objset_t *fromsnap = NULL; 2495789Sahrens objset_t *tosnap; 2496789Sahrens file_t *fp; 2497789Sahrens int error; 24985367Sahrens offset_t off; 2499789Sahrens 2500789Sahrens error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 25016689Smaybee DS_MODE_USER | DS_MODE_READONLY, &tosnap); 2502789Sahrens if (error) 2503789Sahrens return (error); 2504789Sahrens 25052676Seschrock if (zc->zc_value[0] != '\0') { 25062885Sahrens char buf[MAXPATHLEN]; 25072885Sahrens char *cp; 25082885Sahrens 25092885Sahrens (void) strncpy(buf, zc->zc_name, sizeof (buf)); 25102885Sahrens cp = strchr(buf, '@'); 25112885Sahrens if (cp) 25122885Sahrens *(cp+1) = 0; 25132885Sahrens (void) strncat(buf, zc->zc_value, sizeof (buf)); 25142885Sahrens error = dmu_objset_open(buf, DMU_OST_ANY, 25156689Smaybee DS_MODE_USER | DS_MODE_READONLY, &fromsnap); 2516789Sahrens if (error) { 2517789Sahrens dmu_objset_close(tosnap); 2518789Sahrens return (error); 2519789Sahrens } 2520789Sahrens } 2521789Sahrens 2522789Sahrens fp = getf(zc->zc_cookie); 2523789Sahrens if (fp == NULL) { 2524789Sahrens dmu_objset_close(tosnap); 2525789Sahrens if (fromsnap) 2526789Sahrens dmu_objset_close(fromsnap); 2527789Sahrens return (EBADF); 2528789Sahrens } 2529789Sahrens 25305367Sahrens off = fp->f_offset; 25315367Sahrens error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 25325367Sahrens 25335367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 25345367Sahrens fp->f_offset = off; 2535789Sahrens releasef(zc->zc_cookie); 2536789Sahrens if (fromsnap) 2537789Sahrens dmu_objset_close(fromsnap); 2538789Sahrens dmu_objset_close(tosnap); 2539789Sahrens return (error); 2540789Sahrens } 2541789Sahrens 25421544Seschrock static int 25431544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 25441544Seschrock { 25451544Seschrock int id, error; 25461544Seschrock 25471544Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 25481544Seschrock &zc->zc_inject_record); 25491544Seschrock 25501544Seschrock if (error == 0) 25511544Seschrock zc->zc_guid = (uint64_t)id; 25521544Seschrock 25531544Seschrock return (error); 25541544Seschrock } 25551544Seschrock 25561544Seschrock static int 25571544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 25581544Seschrock { 25591544Seschrock return (zio_clear_fault((int)zc->zc_guid)); 25601544Seschrock } 25611544Seschrock 25621544Seschrock static int 25631544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 25641544Seschrock { 25651544Seschrock int id = (int)zc->zc_guid; 25661544Seschrock int error; 25671544Seschrock 25681544Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 25691544Seschrock &zc->zc_inject_record); 25701544Seschrock 25711544Seschrock zc->zc_guid = id; 25721544Seschrock 25731544Seschrock return (error); 25741544Seschrock } 25751544Seschrock 25761544Seschrock static int 25771544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 25781544Seschrock { 25791544Seschrock spa_t *spa; 25801544Seschrock int error; 25812676Seschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 25821544Seschrock 25831544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 25841544Seschrock return (error); 25851544Seschrock 25862676Seschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 25871544Seschrock &count); 25881544Seschrock if (error == 0) 25892676Seschrock zc->zc_nvlist_dst_size = count; 25901544Seschrock else 25912676Seschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 25921544Seschrock 25931544Seschrock spa_close(spa, FTAG); 25941544Seschrock 25951544Seschrock return (error); 25961544Seschrock } 25971544Seschrock 25981544Seschrock static int 25991544Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 26001544Seschrock { 26011544Seschrock spa_t *spa; 26021544Seschrock vdev_t *vd; 26034808Sek110237 uint64_t txg; 26041544Seschrock int error; 26051544Seschrock 26061544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 26071544Seschrock return (error); 26081544Seschrock 26095329Sgw25295 /* 26105329Sgw25295 * Try to resume any I/Os which may have been suspended 26115329Sgw25295 * as a result of a complete pool failure. 26125329Sgw25295 */ 26135329Sgw25295 if (!list_is_empty(&spa->spa_zio_list)) { 26145329Sgw25295 if (zio_vdev_resume_io(spa) != 0) { 26155329Sgw25295 spa_close(spa, FTAG); 26165329Sgw25295 return (EIO); 26175329Sgw25295 } 26185329Sgw25295 } 26195329Sgw25295 26204451Seschrock txg = spa_vdev_enter(spa); 26211544Seschrock 26222676Seschrock if (zc->zc_guid == 0) { 26231544Seschrock vd = NULL; 26246643Seschrock } else { 26256643Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 26265450Sbrendan if (vd == NULL) { 26275450Sbrendan (void) spa_vdev_exit(spa, NULL, txg, ENODEV); 26285450Sbrendan spa_close(spa, FTAG); 26295450Sbrendan return (ENODEV); 26305450Sbrendan } 26311544Seschrock } 26321544Seschrock 26335329Sgw25295 vdev_clear(spa, vd, B_TRUE); 26341544Seschrock 26354451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 26361544Seschrock 26371544Seschrock spa_close(spa, FTAG); 26381544Seschrock 26391544Seschrock return (0); 26401544Seschrock } 26411544Seschrock 26425367Sahrens /* 26435367Sahrens * inputs: 26445367Sahrens * zc_name name of filesystem 26455367Sahrens * zc_value name of origin snapshot 26465367Sahrens * 26475367Sahrens * outputs: none 26485367Sahrens */ 26491544Seschrock static int 26502082Seschrock zfs_ioc_promote(zfs_cmd_t *zc) 26512082Seschrock { 26522417Sahrens char *cp; 26532417Sahrens 26542417Sahrens /* 26552417Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 26562417Sahrens * it's easier. 26572417Sahrens */ 26582676Seschrock cp = strchr(zc->zc_value, '@'); 26592417Sahrens if (cp) 26602417Sahrens *cp = '\0'; 26612676Seschrock (void) dmu_objset_find(zc->zc_value, 26622417Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 26632082Seschrock return (dsl_dataset_promote(zc->zc_name)); 26642082Seschrock } 26652082Seschrock 26664543Smarks /* 26674543Smarks * We don't want to have a hard dependency 26684543Smarks * against some special symbols in sharefs 26695331Samw * nfs, and smbsrv. Determine them if needed when 26704543Smarks * the first file system is shared. 26715331Samw * Neither sharefs, nfs or smbsrv are unloadable modules. 26724543Smarks */ 26735331Samw int (*znfsexport_fs)(void *arg); 26744543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 26755331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 26765331Samw 26775331Samw int zfs_nfsshare_inited; 26785331Samw int zfs_smbshare_inited; 26795331Samw 26804543Smarks ddi_modhandle_t nfs_mod; 26814543Smarks ddi_modhandle_t sharefs_mod; 26825331Samw ddi_modhandle_t smbsrv_mod; 26834543Smarks kmutex_t zfs_share_lock; 26844543Smarks 26854543Smarks static int 26865331Samw zfs_init_sharefs() 26875331Samw { 26885331Samw int error; 26895331Samw 26905331Samw ASSERT(MUTEX_HELD(&zfs_share_lock)); 26915331Samw /* Both NFS and SMB shares also require sharetab support. */ 26925331Samw if (sharefs_mod == NULL && ((sharefs_mod = 26935331Samw ddi_modopen("fs/sharefs", 26945331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 26955331Samw return (ENOSYS); 26965331Samw } 26975331Samw if (zshare_fs == NULL && ((zshare_fs = 26985331Samw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 26995331Samw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 27005331Samw return (ENOSYS); 27015331Samw } 27025331Samw return (0); 27035331Samw } 27045331Samw 27055331Samw static int 27064543Smarks zfs_ioc_share(zfs_cmd_t *zc) 27074543Smarks { 27084543Smarks int error; 27094543Smarks int opcode; 27104543Smarks 27115331Samw switch (zc->zc_share.z_sharetype) { 27125331Samw case ZFS_SHARE_NFS: 27135331Samw case ZFS_UNSHARE_NFS: 27145331Samw if (zfs_nfsshare_inited == 0) { 27155331Samw mutex_enter(&zfs_share_lock); 27165331Samw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 27175331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 27185331Samw mutex_exit(&zfs_share_lock); 27195331Samw return (ENOSYS); 27205331Samw } 27215331Samw if (znfsexport_fs == NULL && 27225331Samw ((znfsexport_fs = (int (*)(void *)) 27235331Samw ddi_modsym(nfs_mod, 27245331Samw "nfs_export", &error)) == NULL)) { 27255331Samw mutex_exit(&zfs_share_lock); 27265331Samw return (ENOSYS); 27275331Samw } 27285331Samw error = zfs_init_sharefs(); 27295331Samw if (error) { 27305331Samw mutex_exit(&zfs_share_lock); 27315331Samw return (ENOSYS); 27325331Samw } 27335331Samw zfs_nfsshare_inited = 1; 27344543Smarks mutex_exit(&zfs_share_lock); 27354543Smarks } 27365331Samw break; 27375331Samw case ZFS_SHARE_SMB: 27385331Samw case ZFS_UNSHARE_SMB: 27395331Samw if (zfs_smbshare_inited == 0) { 27405331Samw mutex_enter(&zfs_share_lock); 27415331Samw if (smbsrv_mod == NULL && ((smbsrv_mod = 27425331Samw ddi_modopen("drv/smbsrv", 27435331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 27445331Samw mutex_exit(&zfs_share_lock); 27455331Samw return (ENOSYS); 27465331Samw } 27475331Samw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 27485331Samw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 27496139Sjb150015 "smb_server_share", &error)) == NULL)) { 27505331Samw mutex_exit(&zfs_share_lock); 27515331Samw return (ENOSYS); 27525331Samw } 27535331Samw error = zfs_init_sharefs(); 27545331Samw if (error) { 27555331Samw mutex_exit(&zfs_share_lock); 27565331Samw return (ENOSYS); 27575331Samw } 27585331Samw zfs_smbshare_inited = 1; 27594543Smarks mutex_exit(&zfs_share_lock); 27604543Smarks } 27615331Samw break; 27625331Samw default: 27635331Samw return (EINVAL); 27644543Smarks } 27654543Smarks 27665331Samw switch (zc->zc_share.z_sharetype) { 27675331Samw case ZFS_SHARE_NFS: 27685331Samw case ZFS_UNSHARE_NFS: 27695331Samw if (error = 27705331Samw znfsexport_fs((void *) 27715331Samw (uintptr_t)zc->zc_share.z_exportdata)) 27725331Samw return (error); 27735331Samw break; 27745331Samw case ZFS_SHARE_SMB: 27755331Samw case ZFS_UNSHARE_SMB: 27765331Samw if (error = zsmbexport_fs((void *) 27775331Samw (uintptr_t)zc->zc_share.z_exportdata, 27785331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 27795331Samw B_TRUE : B_FALSE)) { 27805331Samw return (error); 27815331Samw } 27825331Samw break; 27835331Samw } 27845331Samw 27855331Samw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 27865331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 27874543Smarks SHAREFS_ADD : SHAREFS_REMOVE; 27884543Smarks 27895331Samw /* 27905331Samw * Add or remove share from sharetab 27915331Samw */ 27924543Smarks error = zshare_fs(opcode, 27934543Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 27944543Smarks zc->zc_share.z_sharemax); 27954543Smarks 27964543Smarks return (error); 27974543Smarks 27984543Smarks } 27994543Smarks 28004543Smarks /* 28014988Sek110237 * pool create, destroy, and export don't log the history as part of 28024988Sek110237 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 28034988Sek110237 * do the logging of those commands. 28044543Smarks */ 2805789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 28064715Sek110237 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28074577Sahrens { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28084577Sahrens { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28094577Sahrens { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28104577Sahrens { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE }, 28114577Sahrens { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 28124577Sahrens { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE }, 28134577Sahrens { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28144577Sahrens { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE }, 28154577Sahrens { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28164577Sahrens { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28174577Sahrens { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28184577Sahrens { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28194577Sahrens { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28204577Sahrens { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28214577Sahrens { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28224577Sahrens { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28234577Sahrens { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 28245498Stimh { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 28254543Smarks { zfs_ioc_dataset_list_next, zfs_secpolicy_read, 28264577Sahrens DATASET_NAME, B_FALSE }, 28274543Smarks { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, 28284577Sahrens DATASET_NAME, B_FALSE }, 28294577Sahrens { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE }, 28304577Sahrens { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 28314577Sahrens { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 28324577Sahrens { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE }, 28334577Sahrens { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 28344577Sahrens { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE }, 28354577Sahrens { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE }, 28365367Sahrens { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE }, 28375367Sahrens { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE }, 28384577Sahrens { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 28394577Sahrens { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 28404577Sahrens { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 28414577Sahrens { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE }, 28424577Sahrens { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28434577Sahrens { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE }, 28444577Sahrens { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 28454577Sahrens { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE }, 28464577Sahrens { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 28474577Sahrens { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE }, 28484577Sahrens { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 28494577Sahrens { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 28504577Sahrens { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE }, 28514577Sahrens { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 28524543Smarks { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, 28534577Sahrens DATASET_NAME, B_FALSE }, 28544849Sahrens { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE }, 28554849Sahrens { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE }, 2856789Sahrens }; 2857789Sahrens 2858789Sahrens static int 2859789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 2860789Sahrens { 2861789Sahrens zfs_cmd_t *zc; 2862789Sahrens uint_t vec; 28632199Sahrens int error, rc; 2864789Sahrens 2865789Sahrens if (getminor(dev) != 0) 2866789Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 2867789Sahrens 2868789Sahrens vec = cmd - ZFS_IOC; 28694787Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 2870789Sahrens 2871789Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 2872789Sahrens return (EINVAL); 2873789Sahrens 2874789Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2875789Sahrens 2876789Sahrens error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t)); 2877789Sahrens 28784787Sahrens if (error == 0) 28794543Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 2880789Sahrens 2881789Sahrens /* 2882789Sahrens * Ensure that all pool/dataset names are valid before we pass down to 2883789Sahrens * the lower layers. 2884789Sahrens */ 2885789Sahrens if (error == 0) { 2886789Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 2887789Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 28884577Sahrens case POOL_NAME: 2889789Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 2890789Sahrens error = EINVAL; 2891789Sahrens break; 2892789Sahrens 28934577Sahrens case DATASET_NAME: 2894789Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 2895789Sahrens error = EINVAL; 2896789Sahrens break; 28972856Snd150628 28984577Sahrens case NO_NAME: 28992856Snd150628 break; 2900789Sahrens } 2901789Sahrens } 2902789Sahrens 2903789Sahrens if (error == 0) 2904789Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 2905789Sahrens 29062199Sahrens rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t)); 29074543Smarks if (error == 0) { 29082199Sahrens error = rc; 29094543Smarks if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE) 29104543Smarks zfs_log_history(zc); 29114543Smarks } 2912789Sahrens 2913789Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 2914789Sahrens return (error); 2915789Sahrens } 2916789Sahrens 2917789Sahrens static int 2918789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2919789Sahrens { 2920789Sahrens if (cmd != DDI_ATTACH) 2921789Sahrens return (DDI_FAILURE); 2922789Sahrens 2923789Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 2924789Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 2925789Sahrens return (DDI_FAILURE); 2926789Sahrens 2927789Sahrens zfs_dip = dip; 2928789Sahrens 2929789Sahrens ddi_report_dev(dip); 2930789Sahrens 2931789Sahrens return (DDI_SUCCESS); 2932789Sahrens } 2933789Sahrens 2934789Sahrens static int 2935789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2936789Sahrens { 2937789Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 2938789Sahrens return (DDI_FAILURE); 2939789Sahrens 2940789Sahrens if (cmd != DDI_DETACH) 2941789Sahrens return (DDI_FAILURE); 2942789Sahrens 2943789Sahrens zfs_dip = NULL; 2944789Sahrens 2945789Sahrens ddi_prop_remove_all(dip); 2946789Sahrens ddi_remove_minor_node(dip, NULL); 2947789Sahrens 2948789Sahrens return (DDI_SUCCESS); 2949789Sahrens } 2950789Sahrens 2951789Sahrens /*ARGSUSED*/ 2952789Sahrens static int 2953789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2954789Sahrens { 2955789Sahrens switch (infocmd) { 2956789Sahrens case DDI_INFO_DEVT2DEVINFO: 2957789Sahrens *result = zfs_dip; 2958789Sahrens return (DDI_SUCCESS); 2959789Sahrens 2960789Sahrens case DDI_INFO_DEVT2INSTANCE: 2961849Sbonwick *result = (void *)0; 2962789Sahrens return (DDI_SUCCESS); 2963789Sahrens } 2964789Sahrens 2965789Sahrens return (DDI_FAILURE); 2966789Sahrens } 2967789Sahrens 2968789Sahrens /* 2969789Sahrens * OK, so this is a little weird. 2970789Sahrens * 2971789Sahrens * /dev/zfs is the control node, i.e. minor 0. 2972789Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 2973789Sahrens * 2974789Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 2975789Sahrens * so most of the standard driver entry points are in zvol.c. 2976789Sahrens */ 2977789Sahrens static struct cb_ops zfs_cb_ops = { 2978789Sahrens zvol_open, /* open */ 2979789Sahrens zvol_close, /* close */ 2980789Sahrens zvol_strategy, /* strategy */ 2981789Sahrens nodev, /* print */ 29826423Sgw25295 zvol_dump, /* dump */ 2983789Sahrens zvol_read, /* read */ 2984789Sahrens zvol_write, /* write */ 2985789Sahrens zfsdev_ioctl, /* ioctl */ 2986789Sahrens nodev, /* devmap */ 2987789Sahrens nodev, /* mmap */ 2988789Sahrens nodev, /* segmap */ 2989789Sahrens nochpoll, /* poll */ 2990789Sahrens ddi_prop_op, /* prop_op */ 2991789Sahrens NULL, /* streamtab */ 2992789Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 2993789Sahrens CB_REV, /* version */ 29943638Sbillm nodev, /* async read */ 29953638Sbillm nodev, /* async write */ 2996789Sahrens }; 2997789Sahrens 2998789Sahrens static struct dev_ops zfs_dev_ops = { 2999789Sahrens DEVO_REV, /* version */ 3000789Sahrens 0, /* refcnt */ 3001789Sahrens zfs_info, /* info */ 3002789Sahrens nulldev, /* identify */ 3003789Sahrens nulldev, /* probe */ 3004789Sahrens zfs_attach, /* attach */ 3005789Sahrens zfs_detach, /* detach */ 3006789Sahrens nodev, /* reset */ 3007789Sahrens &zfs_cb_ops, /* driver operations */ 3008789Sahrens NULL /* no bus operations */ 3009789Sahrens }; 3010789Sahrens 3011789Sahrens static struct modldrv zfs_modldrv = { 30124577Sahrens &mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING, 30132676Seschrock &zfs_dev_ops 3014789Sahrens }; 3015789Sahrens 3016789Sahrens static struct modlinkage modlinkage = { 3017789Sahrens MODREV_1, 3018789Sahrens (void *)&zfs_modlfs, 3019789Sahrens (void *)&zfs_modldrv, 3020789Sahrens NULL 3021789Sahrens }; 3022789Sahrens 30234720Sfr157268 30244720Sfr157268 uint_t zfs_fsyncer_key; 30255326Sek110237 extern uint_t rrw_tsd_key; 30264720Sfr157268 3027789Sahrens int 3028789Sahrens _init(void) 3029789Sahrens { 3030789Sahrens int error; 3031789Sahrens 3032849Sbonwick spa_init(FREAD | FWRITE); 3033849Sbonwick zfs_init(); 3034849Sbonwick zvol_init(); 3035849Sbonwick 3036849Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 3037849Sbonwick zvol_fini(); 3038849Sbonwick zfs_fini(); 3039849Sbonwick spa_fini(); 3040789Sahrens return (error); 3041849Sbonwick } 3042789Sahrens 30434720Sfr157268 tsd_create(&zfs_fsyncer_key, NULL); 30445326Sek110237 tsd_create(&rrw_tsd_key, NULL); 30454720Sfr157268 3046789Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 3047789Sahrens ASSERT(error == 0); 30484543Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 3049789Sahrens 3050789Sahrens return (0); 3051789Sahrens } 3052789Sahrens 3053789Sahrens int 3054789Sahrens _fini(void) 3055789Sahrens { 3056789Sahrens int error; 3057789Sahrens 30581544Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 3059789Sahrens return (EBUSY); 3060789Sahrens 3061789Sahrens if ((error = mod_remove(&modlinkage)) != 0) 3062789Sahrens return (error); 3063789Sahrens 3064789Sahrens zvol_fini(); 3065789Sahrens zfs_fini(); 3066789Sahrens spa_fini(); 30675331Samw if (zfs_nfsshare_inited) 30684543Smarks (void) ddi_modclose(nfs_mod); 30695331Samw if (zfs_smbshare_inited) 30705331Samw (void) ddi_modclose(smbsrv_mod); 30715331Samw if (zfs_nfsshare_inited || zfs_smbshare_inited) 30724543Smarks (void) ddi_modclose(sharefs_mod); 3073789Sahrens 30744720Sfr157268 tsd_destroy(&zfs_fsyncer_key); 3075789Sahrens ldi_ident_release(zfs_li); 3076789Sahrens zfs_li = NULL; 30774543Smarks mutex_destroy(&zfs_share_lock); 3078789Sahrens 3079789Sahrens return (error); 3080789Sahrens } 3081789Sahrens 3082789Sahrens int 3083789Sahrens _info(struct modinfo *modinfop) 3084789Sahrens { 3085789Sahrens return (mod_info(&modlinkage, modinfop)); 3086789Sahrens } 3087