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 /* 228525SEric.Schrock@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/types.h> 27789Sahrens #include <sys/param.h> 28789Sahrens #include <sys/errno.h> 29789Sahrens #include <sys/uio.h> 30789Sahrens #include <sys/buf.h> 31789Sahrens #include <sys/modctl.h> 32789Sahrens #include <sys/open.h> 33789Sahrens #include <sys/file.h> 34789Sahrens #include <sys/kmem.h> 35789Sahrens #include <sys/conf.h> 36789Sahrens #include <sys/cmn_err.h> 37789Sahrens #include <sys/stat.h> 38789Sahrens #include <sys/zfs_ioctl.h> 395331Samw #include <sys/zfs_znode.h> 40789Sahrens #include <sys/zap.h> 41789Sahrens #include <sys/spa.h> 423912Slling #include <sys/spa_impl.h> 43789Sahrens #include <sys/vdev.h> 443912Slling #include <sys/vdev_impl.h> 45789Sahrens #include <sys/dmu.h> 46789Sahrens #include <sys/dsl_dir.h> 47789Sahrens #include <sys/dsl_dataset.h> 48789Sahrens #include <sys/dsl_prop.h> 494543Smarks #include <sys/dsl_deleg.h> 504543Smarks #include <sys/dmu_objset.h> 51789Sahrens #include <sys/ddi.h> 52789Sahrens #include <sys/sunddi.h> 53789Sahrens #include <sys/sunldi.h> 54789Sahrens #include <sys/policy.h> 55789Sahrens #include <sys/zone.h> 56789Sahrens #include <sys/nvpair.h> 57789Sahrens #include <sys/pathname.h> 58789Sahrens #include <sys/mount.h> 59789Sahrens #include <sys/sdt.h> 60789Sahrens #include <sys/fs/zfs.h> 61789Sahrens #include <sys/zfs_ctldir.h> 625331Samw #include <sys/zfs_dir.h> 632885Sahrens #include <sys/zvol.h> 644543Smarks #include <sharefs/share.h> 655326Sek110237 #include <sys/dmu_objset.h> 66789Sahrens 67789Sahrens #include "zfs_namecheck.h" 682676Seschrock #include "zfs_prop.h" 694543Smarks #include "zfs_deleg.h" 70789Sahrens 71789Sahrens extern struct modlfs zfs_modlfs; 72789Sahrens 73789Sahrens extern void zfs_init(void); 74789Sahrens extern void zfs_fini(void); 75789Sahrens 76789Sahrens ldi_ident_t zfs_li = NULL; 77789Sahrens dev_info_t *zfs_dip; 78789Sahrens 79789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *); 804543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 81789Sahrens 82789Sahrens typedef struct zfs_ioc_vec { 83789Sahrens zfs_ioc_func_t *zvec_func; 84789Sahrens zfs_secpolicy_func_t *zvec_secpolicy; 85789Sahrens enum { 864577Sahrens NO_NAME, 874577Sahrens POOL_NAME, 884577Sahrens DATASET_NAME 894543Smarks } zvec_namecheck; 904543Smarks boolean_t zvec_his_log; 91789Sahrens } zfs_ioc_vec_t; 92789Sahrens 938536SDavid.Pacheco@Sun.COM static void clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops); 947184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 957184Stimh boolean_t *); 967184Stimh int zfs_set_prop_nvlist(const char *, nvlist_t *); 977184Stimh 98789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 99789Sahrens void 100789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 101789Sahrens { 102789Sahrens const char *newfile; 103789Sahrens char buf[256]; 104789Sahrens va_list adx; 105789Sahrens 106789Sahrens /* 107789Sahrens * Get rid of annoying "../common/" prefix to filename. 108789Sahrens */ 109789Sahrens newfile = strrchr(file, '/'); 110789Sahrens if (newfile != NULL) { 111789Sahrens newfile = newfile + 1; /* Get rid of leading / */ 112789Sahrens } else { 113789Sahrens newfile = file; 114789Sahrens } 115789Sahrens 116789Sahrens va_start(adx, fmt); 117789Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, adx); 118789Sahrens va_end(adx); 119789Sahrens 120789Sahrens /* 121789Sahrens * To get this data, use the zfs-dprintf probe as so: 122789Sahrens * dtrace -q -n 'zfs-dprintf \ 123789Sahrens * /stringof(arg0) == "dbuf.c"/ \ 124789Sahrens * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 125789Sahrens * arg0 = file name 126789Sahrens * arg1 = function name 127789Sahrens * arg2 = line number 128789Sahrens * arg3 = message 129789Sahrens */ 130789Sahrens DTRACE_PROBE4(zfs__dprintf, 131789Sahrens char *, newfile, char *, func, int, line, char *, buf); 132789Sahrens } 133789Sahrens 1344543Smarks static void 1354715Sek110237 history_str_free(char *buf) 1364715Sek110237 { 1374715Sek110237 kmem_free(buf, HIS_MAX_RECORD_LEN); 1384715Sek110237 } 1394715Sek110237 1404715Sek110237 static char * 1414715Sek110237 history_str_get(zfs_cmd_t *zc) 1424715Sek110237 { 1434715Sek110237 char *buf; 1444715Sek110237 1454715Sek110237 if (zc->zc_history == NULL) 1464715Sek110237 return (NULL); 1474715Sek110237 1484715Sek110237 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 1494715Sek110237 if (copyinstr((void *)(uintptr_t)zc->zc_history, 1504715Sek110237 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 1514715Sek110237 history_str_free(buf); 1524715Sek110237 return (NULL); 1534715Sek110237 } 1544715Sek110237 1554715Sek110237 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 1564715Sek110237 1574715Sek110237 return (buf); 1584715Sek110237 } 1594715Sek110237 1605375Stimh /* 1617042Sgw25295 * Check to see if the named dataset is currently defined as bootable 1627042Sgw25295 */ 1637042Sgw25295 static boolean_t 1647042Sgw25295 zfs_is_bootfs(const char *name) 1657042Sgw25295 { 1667042Sgw25295 spa_t *spa; 1677042Sgw25295 boolean_t ret = B_FALSE; 1687042Sgw25295 1697042Sgw25295 if (spa_open(name, &spa, FTAG) == 0) { 1707042Sgw25295 if (spa->spa_bootfs) { 1717042Sgw25295 objset_t *os; 1727042Sgw25295 1737042Sgw25295 if (dmu_objset_open(name, DMU_OST_ZFS, 1747042Sgw25295 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 1757042Sgw25295 ret = (dmu_objset_id(os) == spa->spa_bootfs); 1767042Sgw25295 dmu_objset_close(os); 1777042Sgw25295 } 1787042Sgw25295 } 1797042Sgw25295 spa_close(spa, FTAG); 1807042Sgw25295 } 1817042Sgw25295 return (ret); 1827042Sgw25295 } 1837042Sgw25295 1847042Sgw25295 /* 1857184Stimh * zfs_earlier_version 1865375Stimh * 1875375Stimh * Return non-zero if the spa version is less than requested version. 1885375Stimh */ 1895331Samw static int 1907184Stimh zfs_earlier_version(const char *name, int version) 1915331Samw { 1925331Samw spa_t *spa; 1935331Samw 1945331Samw if (spa_open(name, &spa, FTAG) == 0) { 1955331Samw if (spa_version(spa) < version) { 1965331Samw spa_close(spa, FTAG); 1975331Samw return (1); 1985331Samw } 1995331Samw spa_close(spa, FTAG); 2005331Samw } 2015331Samw return (0); 2025331Samw } 2035331Samw 2045977Smarks /* 2056689Smaybee * zpl_earlier_version 2065977Smarks * 2076689Smaybee * Return TRUE if the ZPL version is less than requested version. 2085977Smarks */ 2096689Smaybee static boolean_t 2106689Smaybee zpl_earlier_version(const char *name, int version) 2115977Smarks { 2125977Smarks objset_t *os; 2136689Smaybee boolean_t rc = B_TRUE; 2145977Smarks 2155977Smarks if (dmu_objset_open(name, DMU_OST_ANY, 2166689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 2176689Smaybee uint64_t zplversion; 2186689Smaybee 2196689Smaybee if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 2206689Smaybee rc = zplversion < version; 2215977Smarks dmu_objset_close(os); 2225977Smarks } 2235977Smarks return (rc); 2245977Smarks } 2255977Smarks 2264715Sek110237 static void 2274543Smarks zfs_log_history(zfs_cmd_t *zc) 2284543Smarks { 2294543Smarks spa_t *spa; 2304603Sahrens char *buf; 2314543Smarks 2324715Sek110237 if ((buf = history_str_get(zc)) == NULL) 2334577Sahrens return; 2344577Sahrens 2354715Sek110237 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 2364715Sek110237 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 2374715Sek110237 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 2384715Sek110237 spa_close(spa, FTAG); 2394543Smarks } 2404715Sek110237 history_str_free(buf); 2414543Smarks } 2424543Smarks 243789Sahrens /* 244789Sahrens * Policy for top-level read operations (list pools). Requires no privileges, 245789Sahrens * and can be used in the local zone, as there is no associated dataset. 246789Sahrens */ 247789Sahrens /* ARGSUSED */ 248789Sahrens static int 2494543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 250789Sahrens { 251789Sahrens return (0); 252789Sahrens } 253789Sahrens 254789Sahrens /* 255789Sahrens * Policy for dataset read operations (list children, get statistics). Requires 256789Sahrens * no privileges, but must be visible in the local zone. 257789Sahrens */ 258789Sahrens /* ARGSUSED */ 259789Sahrens static int 2604543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 261789Sahrens { 262789Sahrens if (INGLOBALZONE(curproc) || 2634543Smarks zone_dataset_visible(zc->zc_name, NULL)) 264789Sahrens return (0); 265789Sahrens 266789Sahrens return (ENOENT); 267789Sahrens } 268789Sahrens 269789Sahrens static int 270789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr) 271789Sahrens { 272789Sahrens uint64_t zoned; 273789Sahrens int writable = 1; 274789Sahrens 275789Sahrens /* 276789Sahrens * The dataset must be visible by this zone -- check this first 277789Sahrens * so they don't see EPERM on something they shouldn't know about. 278789Sahrens */ 279789Sahrens if (!INGLOBALZONE(curproc) && 280789Sahrens !zone_dataset_visible(dataset, &writable)) 281789Sahrens return (ENOENT); 282789Sahrens 283789Sahrens if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 284789Sahrens return (ENOENT); 285789Sahrens 286789Sahrens if (INGLOBALZONE(curproc)) { 287789Sahrens /* 288789Sahrens * If the fs is zoned, only root can access it from the 289789Sahrens * global zone. 290789Sahrens */ 291789Sahrens if (secpolicy_zfs(cr) && zoned) 292789Sahrens return (EPERM); 293789Sahrens } else { 294789Sahrens /* 295789Sahrens * If we are in a local zone, the 'zoned' property must be set. 296789Sahrens */ 297789Sahrens if (!zoned) 298789Sahrens return (EPERM); 299789Sahrens 300789Sahrens /* must be writable by this zone */ 301789Sahrens if (!writable) 302789Sahrens return (EPERM); 303789Sahrens } 304789Sahrens return (0); 305789Sahrens } 306789Sahrens 307789Sahrens int 3084543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 309789Sahrens { 310789Sahrens int error; 311789Sahrens 3124543Smarks error = zfs_dozonecheck(name, cr); 3134543Smarks if (error == 0) { 3144543Smarks error = secpolicy_zfs(cr); 3154670Sahrens if (error) 3164543Smarks error = dsl_deleg_access(name, perm, cr); 3174543Smarks } 3184543Smarks return (error); 3194543Smarks } 3204543Smarks 3214543Smarks static int 3224543Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr) 3234543Smarks { 3244543Smarks /* 3254543Smarks * Check permissions for special properties. 3264543Smarks */ 3274543Smarks switch (prop) { 3284543Smarks case ZFS_PROP_ZONED: 3294543Smarks /* 3304543Smarks * Disallow setting of 'zoned' from within a local zone. 3314543Smarks */ 3324543Smarks if (!INGLOBALZONE(curproc)) 3334543Smarks return (EPERM); 3344543Smarks break; 335789Sahrens 3364543Smarks case ZFS_PROP_QUOTA: 3374543Smarks if (!INGLOBALZONE(curproc)) { 3384543Smarks uint64_t zoned; 3394543Smarks char setpoint[MAXNAMELEN]; 3404543Smarks /* 3414543Smarks * Unprivileged users are allowed to modify the 3424543Smarks * quota on things *under* (ie. contained by) 3434543Smarks * the thing they own. 3444543Smarks */ 3454543Smarks if (dsl_prop_get_integer(name, "zoned", &zoned, 3464543Smarks setpoint)) 3474543Smarks return (EPERM); 3484670Sahrens if (!zoned || strlen(name) <= strlen(setpoint)) 3494543Smarks return (EPERM); 3504543Smarks } 3514670Sahrens break; 3524543Smarks } 3534543Smarks 3544787Sahrens return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr)); 355789Sahrens } 356789Sahrens 3574543Smarks int 3584543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 3594543Smarks { 3604543Smarks int error; 3614543Smarks 3624543Smarks error = zfs_dozonecheck(zc->zc_name, cr); 3634543Smarks if (error) 3644543Smarks return (error); 3654543Smarks 3664543Smarks /* 3674543Smarks * permission to set permissions will be evaluated later in 3684543Smarks * dsl_deleg_can_allow() 3694543Smarks */ 3704543Smarks return (0); 3714543Smarks } 3724543Smarks 3734543Smarks int 3744543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 3754543Smarks { 3764543Smarks int error; 3774543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 3784543Smarks ZFS_DELEG_PERM_ROLLBACK, cr); 3794543Smarks if (error == 0) 3804543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 3814543Smarks ZFS_DELEG_PERM_MOUNT, cr); 3824543Smarks return (error); 3834543Smarks } 3844543Smarks 3854543Smarks int 3864543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 3874543Smarks { 3884543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 3894543Smarks ZFS_DELEG_PERM_SEND, cr)); 3904543Smarks } 3914543Smarks 392*8845Samw@Sun.COM static int 393*8845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr) 394*8845Samw@Sun.COM { 395*8845Samw@Sun.COM vnode_t *vp; 396*8845Samw@Sun.COM int error; 397*8845Samw@Sun.COM 398*8845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 399*8845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 400*8845Samw@Sun.COM return (error); 401*8845Samw@Sun.COM 402*8845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 403*8845Samw@Sun.COM 404*8845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 405*8845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 406*8845Samw@Sun.COM zc->zc_name) != 0)) { 407*8845Samw@Sun.COM VN_RELE(vp); 408*8845Samw@Sun.COM return (EPERM); 409*8845Samw@Sun.COM } 410*8845Samw@Sun.COM 411*8845Samw@Sun.COM VN_RELE(vp); 412*8845Samw@Sun.COM return (dsl_deleg_access(zc->zc_name, 413*8845Samw@Sun.COM ZFS_DELEG_PERM_SHARE, cr)); 414*8845Samw@Sun.COM } 415*8845Samw@Sun.COM 4164543Smarks int 4174543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 4184543Smarks { 4194543Smarks if (!INGLOBALZONE(curproc)) 4204543Smarks return (EPERM); 4214543Smarks 4225367Sahrens if (secpolicy_nfs(cr) == 0) { 4234543Smarks return (0); 4244543Smarks } else { 425*8845Samw@Sun.COM return (zfs_secpolicy_deleg_share(zc, cr)); 426*8845Samw@Sun.COM } 427*8845Samw@Sun.COM } 428*8845Samw@Sun.COM 429*8845Samw@Sun.COM int 430*8845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr) 431*8845Samw@Sun.COM { 432*8845Samw@Sun.COM if (!INGLOBALZONE(curproc)) 433*8845Samw@Sun.COM return (EPERM); 434*8845Samw@Sun.COM 435*8845Samw@Sun.COM if (secpolicy_smb(cr) == 0) { 436*8845Samw@Sun.COM return (0); 437*8845Samw@Sun.COM } else { 438*8845Samw@Sun.COM return (zfs_secpolicy_deleg_share(zc, cr)); 4394543Smarks } 4404543Smarks } 4414543Smarks 442789Sahrens static int 4434543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize) 444789Sahrens { 445789Sahrens char *cp; 446789Sahrens 447789Sahrens /* 448789Sahrens * Remove the @bla or /bla from the end of the name to get the parent. 449789Sahrens */ 4504543Smarks (void) strncpy(parent, datasetname, parentsize); 4514543Smarks cp = strrchr(parent, '@'); 452789Sahrens if (cp != NULL) { 453789Sahrens cp[0] = '\0'; 454789Sahrens } else { 4554543Smarks cp = strrchr(parent, '/'); 456789Sahrens if (cp == NULL) 457789Sahrens return (ENOENT); 458789Sahrens cp[0] = '\0'; 459789Sahrens } 460789Sahrens 4614543Smarks return (0); 4624543Smarks } 4634543Smarks 4644543Smarks int 4654543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 4664543Smarks { 4674543Smarks int error; 4684543Smarks 4694543Smarks if ((error = zfs_secpolicy_write_perms(name, 4704543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 4714543Smarks return (error); 4724543Smarks 4734543Smarks return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 4744543Smarks } 4754543Smarks 4764543Smarks static int 4774543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 4784543Smarks { 4794543Smarks return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 4804543Smarks } 4814543Smarks 4824543Smarks /* 4834543Smarks * Must have sys_config privilege to check the iscsi permission 4844543Smarks */ 4854543Smarks /* ARGSUSED */ 4864543Smarks static int 4874543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr) 4884543Smarks { 4894543Smarks return (secpolicy_zfs(cr)); 4904543Smarks } 4914543Smarks 4924543Smarks int 4934543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 4944543Smarks { 4954543Smarks char parentname[MAXNAMELEN]; 4964543Smarks int error; 4974543Smarks 4984543Smarks if ((error = zfs_secpolicy_write_perms(from, 4994543Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0) 5004543Smarks return (error); 5014543Smarks 5024543Smarks if ((error = zfs_secpolicy_write_perms(from, 5034543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 5044543Smarks return (error); 5054543Smarks 5064543Smarks if ((error = zfs_get_parent(to, parentname, 5074543Smarks sizeof (parentname))) != 0) 5084543Smarks return (error); 5094543Smarks 5104543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 5114543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 5124543Smarks return (error); 5134543Smarks 5144543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 5154543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 5164543Smarks return (error); 5174543Smarks 5184543Smarks return (error); 5194543Smarks } 5204543Smarks 5214543Smarks static int 5224543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 5234543Smarks { 5244543Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 5254543Smarks } 5264543Smarks 5274543Smarks static int 5284543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 5294543Smarks { 5304543Smarks char parentname[MAXNAMELEN]; 5314543Smarks objset_t *clone; 5324543Smarks int error; 5334543Smarks 5344543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 5354543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 5364543Smarks if (error) 5374543Smarks return (error); 5384543Smarks 5394543Smarks error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 5406689Smaybee DS_MODE_USER | DS_MODE_READONLY, &clone); 5414543Smarks 5424543Smarks if (error == 0) { 5434543Smarks dsl_dataset_t *pclone = NULL; 5444543Smarks dsl_dir_t *dd; 5454543Smarks dd = clone->os->os_dsl_dataset->ds_dir; 5464543Smarks 5474543Smarks rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 5486689Smaybee error = dsl_dataset_hold_obj(dd->dd_pool, 5496689Smaybee dd->dd_phys->dd_origin_obj, FTAG, &pclone); 5504543Smarks rw_exit(&dd->dd_pool->dp_config_rwlock); 5514543Smarks if (error) { 5524543Smarks dmu_objset_close(clone); 5534543Smarks return (error); 5544543Smarks } 5554543Smarks 5564543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 5574543Smarks ZFS_DELEG_PERM_MOUNT, cr); 5584543Smarks 5594543Smarks dsl_dataset_name(pclone, parentname); 5604543Smarks dmu_objset_close(clone); 5616689Smaybee dsl_dataset_rele(pclone, FTAG); 5624543Smarks if (error == 0) 5634543Smarks error = zfs_secpolicy_write_perms(parentname, 5644543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 5654543Smarks } 5664543Smarks return (error); 5674543Smarks } 5684543Smarks 5694543Smarks static int 5704543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 5714543Smarks { 5724543Smarks int error; 5734543Smarks 5744543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 5754543Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 5764543Smarks return (error); 5774543Smarks 5784543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 5794543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 5804543Smarks return (error); 5814543Smarks 5824543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 5834543Smarks ZFS_DELEG_PERM_CREATE, cr)); 5844543Smarks } 5854543Smarks 5864543Smarks int 5874543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 5884543Smarks { 5894543Smarks int error; 5904543Smarks 5914543Smarks if ((error = zfs_secpolicy_write_perms(name, 5924543Smarks ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0) 5934543Smarks return (error); 5944543Smarks 5954543Smarks error = zfs_secpolicy_write_perms(name, 5964543Smarks ZFS_DELEG_PERM_MOUNT, cr); 5974543Smarks 5984543Smarks return (error); 5994543Smarks } 6004543Smarks 6014543Smarks static int 6024543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 6034543Smarks { 6044543Smarks 6054543Smarks return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 6064543Smarks } 6074543Smarks 6084543Smarks static int 6094543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 6104543Smarks { 6114543Smarks char parentname[MAXNAMELEN]; 6124543Smarks int error; 6134543Smarks 6144543Smarks if ((error = zfs_get_parent(zc->zc_name, parentname, 6154543Smarks sizeof (parentname))) != 0) 6164543Smarks return (error); 6174543Smarks 6184543Smarks if (zc->zc_value[0] != '\0') { 6194543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_value, 6204543Smarks ZFS_DELEG_PERM_CLONE, cr)) != 0) 6214543Smarks return (error); 6224543Smarks } 6234543Smarks 6244543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 6254543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 6264543Smarks return (error); 6274543Smarks 6284543Smarks error = zfs_secpolicy_write_perms(parentname, 6294543Smarks ZFS_DELEG_PERM_MOUNT, cr); 6304543Smarks 6314543Smarks return (error); 6324543Smarks } 6334543Smarks 6344543Smarks static int 6354543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 6364543Smarks { 6374543Smarks int error; 6384543Smarks 6394543Smarks error = secpolicy_fs_unmount(cr, NULL); 6404543Smarks if (error) { 6414543Smarks error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 6424543Smarks } 6434543Smarks return (error); 644789Sahrens } 645789Sahrens 646789Sahrens /* 647789Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 648789Sahrens * SYS_CONFIG privilege, which is not available in a local zone. 649789Sahrens */ 650789Sahrens /* ARGSUSED */ 651789Sahrens static int 6524543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 653789Sahrens { 654789Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0) 655789Sahrens return (EPERM); 656789Sahrens 657789Sahrens return (0); 658789Sahrens } 659789Sahrens 660789Sahrens /* 6614543Smarks * Just like zfs_secpolicy_config, except that we will check for 6624543Smarks * mount permission on the dataset for permission to create/remove 6634543Smarks * the minor nodes. 6644543Smarks */ 6654543Smarks static int 6664543Smarks zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr) 6674543Smarks { 6684543Smarks if (secpolicy_sys_config(cr, B_FALSE) != 0) { 6694543Smarks return (dsl_deleg_access(zc->zc_name, 6704543Smarks ZFS_DELEG_PERM_MOUNT, cr)); 6714543Smarks } 6724543Smarks 6734543Smarks return (0); 6744543Smarks } 6754543Smarks 6764543Smarks /* 6771544Seschrock * Policy for fault injection. Requires all privileges. 6781544Seschrock */ 6791544Seschrock /* ARGSUSED */ 6801544Seschrock static int 6814543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 6821544Seschrock { 6831544Seschrock return (secpolicy_zinject(cr)); 6841544Seschrock } 6851544Seschrock 6864849Sahrens static int 6874849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 6884849Sahrens { 6894849Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 6904849Sahrens 6915094Slling if (prop == ZPROP_INVAL) { 6924849Sahrens if (!zfs_prop_user(zc->zc_value)) 6934849Sahrens return (EINVAL); 6944849Sahrens return (zfs_secpolicy_write_perms(zc->zc_name, 6954849Sahrens ZFS_DELEG_PERM_USERPROP, cr)); 6964849Sahrens } else { 6974849Sahrens if (!zfs_prop_inheritable(prop)) 6984849Sahrens return (EINVAL); 6994849Sahrens return (zfs_secpolicy_setprop(zc->zc_name, prop, cr)); 7004849Sahrens } 7014849Sahrens } 7024849Sahrens 7031544Seschrock /* 704789Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t. 705789Sahrens */ 706789Sahrens static int 7075094Slling get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp) 708789Sahrens { 709789Sahrens char *packed; 710789Sahrens int error; 7115094Slling nvlist_t *list = NULL; 712789Sahrens 713789Sahrens /* 7142676Seschrock * Read in and unpack the user-supplied nvlist. 715789Sahrens */ 7165094Slling if (size == 0) 717789Sahrens return (EINVAL); 718789Sahrens 719789Sahrens packed = kmem_alloc(size, KM_SLEEP); 720789Sahrens 7215094Slling if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) { 722789Sahrens kmem_free(packed, size); 723789Sahrens return (error); 724789Sahrens } 725789Sahrens 7265094Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 727789Sahrens kmem_free(packed, size); 728789Sahrens return (error); 729789Sahrens } 730789Sahrens 731789Sahrens kmem_free(packed, size); 732789Sahrens 7335094Slling *nvp = list; 734789Sahrens return (0); 735789Sahrens } 736789Sahrens 737789Sahrens static int 7382676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 7392676Seschrock { 7402676Seschrock char *packed = NULL; 7412676Seschrock size_t size; 7422676Seschrock int error; 7432676Seschrock 7442676Seschrock VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 7452676Seschrock 7462676Seschrock if (size > zc->zc_nvlist_dst_size) { 7472676Seschrock error = ENOMEM; 7482676Seschrock } else { 7494611Smarks packed = kmem_alloc(size, KM_SLEEP); 7502676Seschrock VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 7512676Seschrock KM_SLEEP) == 0); 7522676Seschrock error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 7532676Seschrock size); 7542676Seschrock kmem_free(packed, size); 7552676Seschrock } 7562676Seschrock 7572676Seschrock zc->zc_nvlist_dst_size = size; 7582676Seschrock return (error); 7592676Seschrock } 7602676Seschrock 7612676Seschrock static int 762789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc) 763789Sahrens { 764789Sahrens int error; 7655094Slling nvlist_t *config, *props = NULL; 7667184Stimh nvlist_t *rootprops = NULL; 7677184Stimh nvlist_t *zplprops = NULL; 7684715Sek110237 char *buf; 769789Sahrens 7705094Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 7715094Slling &config)) 7724988Sek110237 return (error); 7734715Sek110237 7745094Slling if (zc->zc_nvlist_src_size != 0 && (error = 7755094Slling get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) { 7765094Slling nvlist_free(config); 7775094Slling return (error); 7785094Slling } 7795094Slling 7807184Stimh if (props) { 7817184Stimh nvlist_t *nvl = NULL; 7827184Stimh uint64_t version = SPA_VERSION; 7837184Stimh 7847184Stimh (void) nvlist_lookup_uint64(props, 7857184Stimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 7867184Stimh if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 7877184Stimh error = EINVAL; 7887184Stimh goto pool_props_bad; 7897184Stimh } 7907184Stimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 7917184Stimh if (nvl) { 7927184Stimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 7937184Stimh if (error != 0) { 7947184Stimh nvlist_free(config); 7957184Stimh nvlist_free(props); 7967184Stimh return (error); 7977184Stimh } 7987184Stimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 7997184Stimh } 8007184Stimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 8017184Stimh error = zfs_fill_zplprops_root(version, rootprops, 8027184Stimh zplprops, NULL); 8037184Stimh if (error) 8047184Stimh goto pool_props_bad; 8057184Stimh } 8067184Stimh 8074988Sek110237 buf = history_str_get(zc); 808789Sahrens 8097184Stimh error = spa_create(zc->zc_name, config, props, buf, zplprops); 8107184Stimh 8117184Stimh /* 8127184Stimh * Set the remaining root properties 8137184Stimh */ 8147184Stimh if (!error && 8157184Stimh (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0) 8167184Stimh (void) spa_destroy(zc->zc_name); 817789Sahrens 8184988Sek110237 if (buf != NULL) 8194988Sek110237 history_str_free(buf); 8205094Slling 8217184Stimh pool_props_bad: 8227184Stimh nvlist_free(rootprops); 8237184Stimh nvlist_free(zplprops); 824789Sahrens nvlist_free(config); 8257184Stimh nvlist_free(props); 8265094Slling 827789Sahrens return (error); 828789Sahrens } 829789Sahrens 830789Sahrens static int 831789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc) 832789Sahrens { 8334543Smarks int error; 8344543Smarks zfs_log_history(zc); 8354543Smarks error = spa_destroy(zc->zc_name); 8364543Smarks return (error); 837789Sahrens } 838789Sahrens 839789Sahrens static int 840789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc) 841789Sahrens { 842789Sahrens int error; 8435094Slling nvlist_t *config, *props = NULL; 844789Sahrens uint64_t guid; 845789Sahrens 8465094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 8475094Slling &config)) != 0) 848789Sahrens return (error); 849789Sahrens 8505094Slling if (zc->zc_nvlist_src_size != 0 && (error = 8515094Slling get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) { 8525094Slling nvlist_free(config); 8535094Slling return (error); 8545094Slling } 8555094Slling 856789Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 8571544Seschrock guid != zc->zc_guid) 858789Sahrens error = EINVAL; 8596643Seschrock else if (zc->zc_cookie) 8606643Seschrock error = spa_import_faulted(zc->zc_name, config, 8616643Seschrock props); 862789Sahrens else 8635094Slling error = spa_import(zc->zc_name, config, props); 864789Sahrens 865789Sahrens nvlist_free(config); 866789Sahrens 8675094Slling if (props) 8685094Slling nvlist_free(props); 8695094Slling 870789Sahrens return (error); 871789Sahrens } 872789Sahrens 873789Sahrens static int 874789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc) 875789Sahrens { 8764543Smarks int error; 8777214Slling boolean_t force = (boolean_t)zc->zc_cookie; 8788211SGeorge.Wilson@Sun.COM boolean_t hardforce = (boolean_t)zc->zc_guid; 8797214Slling 8804543Smarks zfs_log_history(zc); 8818211SGeorge.Wilson@Sun.COM error = spa_export(zc->zc_name, NULL, force, hardforce); 8824543Smarks return (error); 883789Sahrens } 884789Sahrens 885789Sahrens static int 886789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 887789Sahrens { 888789Sahrens nvlist_t *configs; 889789Sahrens int error; 890789Sahrens 891789Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 892789Sahrens return (EEXIST); 893789Sahrens 8942676Seschrock error = put_nvlist(zc, configs); 895789Sahrens 896789Sahrens nvlist_free(configs); 897789Sahrens 898789Sahrens return (error); 899789Sahrens } 900789Sahrens 901789Sahrens static int 902789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 903789Sahrens { 904789Sahrens nvlist_t *config; 905789Sahrens int error; 9061544Seschrock int ret = 0; 907789Sahrens 9082676Seschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 9092676Seschrock sizeof (zc->zc_value)); 910789Sahrens 911789Sahrens if (config != NULL) { 9122676Seschrock ret = put_nvlist(zc, config); 913789Sahrens nvlist_free(config); 9141544Seschrock 9151544Seschrock /* 9161544Seschrock * The config may be present even if 'error' is non-zero. 9171544Seschrock * In this case we return success, and preserve the real errno 9181544Seschrock * in 'zc_cookie'. 9191544Seschrock */ 9201544Seschrock zc->zc_cookie = error; 921789Sahrens } else { 9221544Seschrock ret = error; 923789Sahrens } 924789Sahrens 9251544Seschrock return (ret); 926789Sahrens } 927789Sahrens 928789Sahrens /* 929789Sahrens * Try to import the given pool, returning pool stats as appropriate so that 930789Sahrens * user land knows which devices are available and overall pool health. 931789Sahrens */ 932789Sahrens static int 933789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 934789Sahrens { 935789Sahrens nvlist_t *tryconfig, *config; 936789Sahrens int error; 937789Sahrens 9385094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 9395094Slling &tryconfig)) != 0) 940789Sahrens return (error); 941789Sahrens 942789Sahrens config = spa_tryimport(tryconfig); 943789Sahrens 944789Sahrens nvlist_free(tryconfig); 945789Sahrens 946789Sahrens if (config == NULL) 947789Sahrens return (EINVAL); 948789Sahrens 9492676Seschrock error = put_nvlist(zc, config); 950789Sahrens nvlist_free(config); 951789Sahrens 952789Sahrens return (error); 953789Sahrens } 954789Sahrens 955789Sahrens static int 956789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc) 957789Sahrens { 958789Sahrens spa_t *spa; 959789Sahrens int error; 960789Sahrens 9612926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 9622926Sek110237 return (error); 9632926Sek110237 9647046Sahrens error = spa_scrub(spa, zc->zc_cookie); 9652926Sek110237 9662926Sek110237 spa_close(spa, FTAG); 9672926Sek110237 968789Sahrens return (error); 969789Sahrens } 970789Sahrens 971789Sahrens static int 972789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 973789Sahrens { 974789Sahrens spa_t *spa; 975789Sahrens int error; 976789Sahrens 977789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 978789Sahrens if (error == 0) { 979789Sahrens spa_freeze(spa); 980789Sahrens spa_close(spa, FTAG); 981789Sahrens } 982789Sahrens return (error); 983789Sahrens } 984789Sahrens 985789Sahrens static int 9861760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 9871760Seschrock { 9881760Seschrock spa_t *spa; 9891760Seschrock int error; 9901760Seschrock 9912926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 9922926Sek110237 return (error); 9932926Sek110237 9945118Slling if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 9955118Slling spa_close(spa, FTAG); 9965118Slling return (EINVAL); 9975118Slling } 9985118Slling 9995094Slling spa_upgrade(spa, zc->zc_cookie); 10002926Sek110237 spa_close(spa, FTAG); 10012926Sek110237 10022926Sek110237 return (error); 10032926Sek110237 } 10042926Sek110237 10052926Sek110237 static int 10062926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 10072926Sek110237 { 10082926Sek110237 spa_t *spa; 10092926Sek110237 char *hist_buf; 10102926Sek110237 uint64_t size; 10112926Sek110237 int error; 10122926Sek110237 10132926Sek110237 if ((size = zc->zc_history_len) == 0) 10142926Sek110237 return (EINVAL); 10152926Sek110237 10162926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 10172926Sek110237 return (error); 10182926Sek110237 10194577Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 10203863Sek110237 spa_close(spa, FTAG); 10213863Sek110237 return (ENOTSUP); 10223863Sek110237 } 10233863Sek110237 10242926Sek110237 hist_buf = kmem_alloc(size, KM_SLEEP); 10252926Sek110237 if ((error = spa_history_get(spa, &zc->zc_history_offset, 10262926Sek110237 &zc->zc_history_len, hist_buf)) == 0) { 10274543Smarks error = xcopyout(hist_buf, 10284543Smarks (char *)(uintptr_t)zc->zc_history, 10292926Sek110237 zc->zc_history_len); 10302926Sek110237 } 10312926Sek110237 10322926Sek110237 spa_close(spa, FTAG); 10332926Sek110237 kmem_free(hist_buf, size); 10342926Sek110237 return (error); 10352926Sek110237 } 10362926Sek110237 10372926Sek110237 static int 10383444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 10393444Sek110237 { 10403444Sek110237 int error; 10413444Sek110237 10423912Slling if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 10433444Sek110237 return (error); 10443444Sek110237 10453444Sek110237 return (0); 10463444Sek110237 } 10473444Sek110237 10483444Sek110237 static int 10493444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 10503444Sek110237 { 10513444Sek110237 objset_t *osp; 10523444Sek110237 int error; 10533444Sek110237 10543444Sek110237 if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS, 10556689Smaybee DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0) 10563444Sek110237 return (error); 10573444Sek110237 error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value, 10583444Sek110237 sizeof (zc->zc_value)); 10593444Sek110237 dmu_objset_close(osp); 10603444Sek110237 10613444Sek110237 return (error); 10623444Sek110237 } 10633444Sek110237 10643444Sek110237 static int 1065789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1066789Sahrens { 1067789Sahrens spa_t *spa; 1068789Sahrens int error; 10696423Sgw25295 nvlist_t *config, **l2cache, **spares; 10706423Sgw25295 uint_t nl2cache = 0, nspares = 0; 1071789Sahrens 1072789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1073789Sahrens if (error != 0) 1074789Sahrens return (error); 1075789Sahrens 10765450Sbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 10775450Sbrendan &config); 10785450Sbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 10795450Sbrendan &l2cache, &nl2cache); 10805450Sbrendan 10816423Sgw25295 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 10826423Sgw25295 &spares, &nspares); 10836423Sgw25295 10843912Slling /* 10853912Slling * A root pool with concatenated devices is not supported. 10866423Sgw25295 * Thus, can not add a device to a root pool. 10876423Sgw25295 * 10886423Sgw25295 * Intent log device can not be added to a rootpool because 10896423Sgw25295 * during mountroot, zil is replayed, a seperated log device 10906423Sgw25295 * can not be accessed during the mountroot time. 10916423Sgw25295 * 10926423Sgw25295 * l2cache and spare devices are ok to be added to a rootpool. 10933912Slling */ 10946423Sgw25295 if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) { 10953912Slling spa_close(spa, FTAG); 10963912Slling return (EDOM); 10973912Slling } 10983912Slling 10995450Sbrendan if (error == 0) { 1100789Sahrens error = spa_vdev_add(spa, config); 1101789Sahrens nvlist_free(config); 1102789Sahrens } 1103789Sahrens spa_close(spa, FTAG); 1104789Sahrens return (error); 1105789Sahrens } 1106789Sahrens 1107789Sahrens static int 1108789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1109789Sahrens { 11102082Seschrock spa_t *spa; 11112082Seschrock int error; 11122082Seschrock 11132082Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 11142082Seschrock if (error != 0) 11152082Seschrock return (error); 11162082Seschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 11172082Seschrock spa_close(spa, FTAG); 11182082Seschrock return (error); 1119789Sahrens } 1120789Sahrens 1121789Sahrens static int 11224451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1123789Sahrens { 1124789Sahrens spa_t *spa; 1125789Sahrens int error; 11264451Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1127789Sahrens 11282926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1129789Sahrens return (error); 11304451Seschrock switch (zc->zc_cookie) { 11314451Seschrock case VDEV_STATE_ONLINE: 11324451Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 11334451Seschrock break; 11344451Seschrock 11354451Seschrock case VDEV_STATE_OFFLINE: 11364451Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 11374451Seschrock break; 1138789Sahrens 11394451Seschrock case VDEV_STATE_FAULTED: 11404451Seschrock error = vdev_fault(spa, zc->zc_guid); 11414451Seschrock break; 1142789Sahrens 11434451Seschrock case VDEV_STATE_DEGRADED: 11444451Seschrock error = vdev_degrade(spa, zc->zc_guid); 11454451Seschrock break; 11464451Seschrock 11474451Seschrock default: 11484451Seschrock error = EINVAL; 11494451Seschrock } 11504451Seschrock zc->zc_cookie = newstate; 1151789Sahrens spa_close(spa, FTAG); 1152789Sahrens return (error); 1153789Sahrens } 1154789Sahrens 1155789Sahrens static int 1156789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1157789Sahrens { 1158789Sahrens spa_t *spa; 1159789Sahrens int replacing = zc->zc_cookie; 1160789Sahrens nvlist_t *config; 1161789Sahrens int error; 1162789Sahrens 11632926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1164789Sahrens return (error); 1165789Sahrens 11665094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 11675094Slling &config)) == 0) { 11681544Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1169789Sahrens nvlist_free(config); 1170789Sahrens } 1171789Sahrens 1172789Sahrens spa_close(spa, FTAG); 1173789Sahrens return (error); 1174789Sahrens } 1175789Sahrens 1176789Sahrens static int 1177789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1178789Sahrens { 1179789Sahrens spa_t *spa; 1180789Sahrens int error; 1181789Sahrens 11822926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1183789Sahrens return (error); 1184789Sahrens 11858241SJeff.Bonwick@Sun.COM error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1186789Sahrens 1187789Sahrens spa_close(spa, FTAG); 1188789Sahrens return (error); 1189789Sahrens } 1190789Sahrens 1191789Sahrens static int 11921354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 11931354Seschrock { 11941354Seschrock spa_t *spa; 11952676Seschrock char *path = zc->zc_value; 11961544Seschrock uint64_t guid = zc->zc_guid; 11971354Seschrock int error; 11981354Seschrock 11991354Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 12001354Seschrock if (error != 0) 12011354Seschrock return (error); 12021354Seschrock 12031354Seschrock error = spa_vdev_setpath(spa, guid, path); 12041354Seschrock spa_close(spa, FTAG); 12051354Seschrock return (error); 12061354Seschrock } 12071354Seschrock 12085367Sahrens /* 12095367Sahrens * inputs: 12105367Sahrens * zc_name name of filesystem 12115367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 12125367Sahrens * 12135367Sahrens * outputs: 12145367Sahrens * zc_objset_stats stats 12155367Sahrens * zc_nvlist_dst property nvlist 12165367Sahrens * zc_nvlist_dst_size size of property nvlist 12175367Sahrens */ 12181354Seschrock static int 1219789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc) 1220789Sahrens { 1221789Sahrens objset_t *os = NULL; 1222789Sahrens int error; 12231356Seschrock nvlist_t *nv; 1224789Sahrens 12256689Smaybee if (error = dmu_objset_open(zc->zc_name, 12266689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) 1227789Sahrens return (error); 1228789Sahrens 12292885Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1230789Sahrens 12312856Snd150628 if (zc->zc_nvlist_dst != 0 && 12326689Smaybee (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) { 12332885Sahrens dmu_objset_stats(os, nv); 12343087Sahrens /* 12355147Srm160521 * NB: zvol_get_stats() will read the objset contents, 12363087Sahrens * which we aren't supposed to do with a 12376689Smaybee * DS_MODE_USER hold, because it could be 12383087Sahrens * inconsistent. So this is a bit of a workaround... 12393087Sahrens */ 12404577Sahrens if (!zc->zc_objset_stats.dds_inconsistent) { 12414577Sahrens if (dmu_objset_type(os) == DMU_OST_ZVOL) 12424577Sahrens VERIFY(zvol_get_stats(os, nv) == 0); 12434577Sahrens } 12442676Seschrock error = put_nvlist(zc, nv); 12451356Seschrock nvlist_free(nv); 12461356Seschrock } 1247789Sahrens 1248789Sahrens dmu_objset_close(os); 1249789Sahrens return (error); 1250789Sahrens } 1251789Sahrens 12525498Stimh static int 12535498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 12545498Stimh { 12555498Stimh uint64_t value; 12565498Stimh int error; 12575498Stimh 12585498Stimh /* 12595498Stimh * zfs_get_zplprop() will either find a value or give us 12605498Stimh * the default value (if there is one). 12615498Stimh */ 12625498Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 12635498Stimh return (error); 12645498Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 12655498Stimh return (0); 12665498Stimh } 12675498Stimh 12685498Stimh /* 12695498Stimh * inputs: 12705498Stimh * zc_name name of filesystem 12715498Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 12725498Stimh * 12735498Stimh * outputs: 12745498Stimh * zc_nvlist_dst zpl property nvlist 12755498Stimh * zc_nvlist_dst_size size of zpl property nvlist 12765498Stimh */ 12775498Stimh static int 12785498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 12795498Stimh { 12805498Stimh objset_t *os; 12815498Stimh int err; 12825498Stimh 12836689Smaybee if (err = dmu_objset_open(zc->zc_name, 12846689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) 12855498Stimh return (err); 12865498Stimh 12875498Stimh dmu_objset_fast_stat(os, &zc->zc_objset_stats); 12885498Stimh 12895498Stimh /* 12905498Stimh * NB: nvl_add_zplprop() will read the objset contents, 12916689Smaybee * which we aren't supposed to do with a DS_MODE_USER 12926689Smaybee * hold, because it could be inconsistent. 12935498Stimh */ 12945498Stimh if (zc->zc_nvlist_dst != NULL && 12955498Stimh !zc->zc_objset_stats.dds_inconsistent && 12965498Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 12975498Stimh nvlist_t *nv; 12985498Stimh 12995498Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 13005498Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 13015498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 13025498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 13035498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 13045498Stimh err = put_nvlist(zc, nv); 13055498Stimh nvlist_free(nv); 13065498Stimh } else { 13075498Stimh err = ENOENT; 13085498Stimh } 13095498Stimh dmu_objset_close(os); 13105498Stimh return (err); 13115498Stimh } 13125498Stimh 13135367Sahrens /* 13145367Sahrens * inputs: 13155367Sahrens * zc_name name of filesystem 13165367Sahrens * zc_cookie zap cursor 13175367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 13185367Sahrens * 13195367Sahrens * outputs: 13205367Sahrens * zc_name name of next filesystem 13215367Sahrens * zc_objset_stats stats 13225367Sahrens * zc_nvlist_dst property nvlist 13235367Sahrens * zc_nvlist_dst_size size of property nvlist 13245367Sahrens */ 1325789Sahrens static int 1326789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1327789Sahrens { 1328885Sahrens objset_t *os; 1329789Sahrens int error; 1330789Sahrens char *p; 1331789Sahrens 13326689Smaybee if (error = dmu_objset_open(zc->zc_name, 13336689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) { 1334885Sahrens if (error == ENOENT) 1335885Sahrens error = ESRCH; 1336885Sahrens return (error); 1337789Sahrens } 1338789Sahrens 1339789Sahrens p = strrchr(zc->zc_name, '/'); 1340789Sahrens if (p == NULL || p[1] != '\0') 1341789Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1342789Sahrens p = zc->zc_name + strlen(zc->zc_name); 1343789Sahrens 13448697SRichard.Morris@Sun.COM /* 13458697SRichard.Morris@Sun.COM * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 13468697SRichard.Morris@Sun.COM * but is not declared void because its called by dmu_objset_find(). 13478697SRichard.Morris@Sun.COM */ 13488415SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) { 13498415SRichard.Morris@Sun.COM uint64_t cookie = 0; 13508415SRichard.Morris@Sun.COM int len = sizeof (zc->zc_name) - (p - zc->zc_name); 13518415SRichard.Morris@Sun.COM 13528415SRichard.Morris@Sun.COM while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) 13538697SRichard.Morris@Sun.COM (void) dmu_objset_prefetch(p, NULL); 13548415SRichard.Morris@Sun.COM } 13558415SRichard.Morris@Sun.COM 1356789Sahrens do { 1357885Sahrens error = dmu_dir_list_next(os, 1358885Sahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 1359885Sahrens NULL, &zc->zc_cookie); 1360789Sahrens if (error == ENOENT) 1361789Sahrens error = ESRCH; 1362885Sahrens } while (error == 0 && !INGLOBALZONE(curproc) && 1363789Sahrens !zone_dataset_visible(zc->zc_name, NULL)); 13646689Smaybee dmu_objset_close(os); 1365789Sahrens 1366885Sahrens /* 1367885Sahrens * If it's a hidden dataset (ie. with a '$' in its name), don't 1368885Sahrens * try to get stats for it. Userland will skip over it. 1369885Sahrens */ 1370885Sahrens if (error == 0 && strchr(zc->zc_name, '$') == NULL) 1371885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1372789Sahrens 1373789Sahrens return (error); 1374789Sahrens } 1375789Sahrens 13765367Sahrens /* 13775367Sahrens * inputs: 13785367Sahrens * zc_name name of filesystem 13795367Sahrens * zc_cookie zap cursor 13805367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 13815367Sahrens * 13825367Sahrens * outputs: 13835367Sahrens * zc_name name of next snapshot 13845367Sahrens * zc_objset_stats stats 13855367Sahrens * zc_nvlist_dst property nvlist 13865367Sahrens * zc_nvlist_dst_size size of property nvlist 13875367Sahrens */ 1388789Sahrens static int 1389789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1390789Sahrens { 1391885Sahrens objset_t *os; 1392789Sahrens int error; 1393789Sahrens 13946689Smaybee error = dmu_objset_open(zc->zc_name, 13956689Smaybee DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os); 13966689Smaybee if (error) 13976689Smaybee return (error == ENOENT ? ESRCH : error); 1398789Sahrens 13998415SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) 14008697SRichard.Morris@Sun.COM (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 14018415SRichard.Morris@Sun.COM NULL, DS_FIND_SNAPSHOTS); 14021003Slling /* 14031003Slling * A dataset name of maximum length cannot have any snapshots, 14041003Slling * so exit immediately. 14051003Slling */ 14061003Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 1407885Sahrens dmu_objset_close(os); 14081003Slling return (ESRCH); 1409789Sahrens } 1410789Sahrens 1411885Sahrens error = dmu_snapshot_list_next(os, 1412885Sahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 14135663Sck153898 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 14146689Smaybee dmu_objset_close(os); 1415885Sahrens if (error == 0) 1416885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 14176689Smaybee else if (error == ENOENT) 14186689Smaybee error = ESRCH; 1419789Sahrens 14205367Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 14216689Smaybee if (error) 14225367Sahrens *strchr(zc->zc_name, '@') = '\0'; 1423789Sahrens return (error); 1424789Sahrens } 1425789Sahrens 14266423Sgw25295 int 14274787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl) 1428789Sahrens { 14292676Seschrock nvpair_t *elem; 14308724SRichard.Morris@Sun.COM int error = 0; 14312676Seschrock uint64_t intval; 14322676Seschrock char *strval; 14338697SRichard.Morris@Sun.COM nvlist_t *genericnvl; 14342676Seschrock 14354543Smarks /* 14364543Smarks * First validate permission to set all of the properties 14374543Smarks */ 14382676Seschrock elem = NULL; 14392676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 14404670Sahrens const char *propname = nvpair_name(elem); 14414670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 14422676Seschrock 14435094Slling if (prop == ZPROP_INVAL) { 14442676Seschrock /* 14452676Seschrock * If this is a user-defined property, it must be a 14462676Seschrock * string, and there is no further validation to do. 14472676Seschrock */ 14482676Seschrock if (!zfs_prop_user(propname) || 14492676Seschrock nvpair_type(elem) != DATA_TYPE_STRING) 14502676Seschrock return (EINVAL); 14512676Seschrock 14525331Samw if (error = zfs_secpolicy_write_perms(name, 14535331Samw ZFS_DELEG_PERM_USERPROP, CRED())) 14544670Sahrens return (error); 14554543Smarks continue; 14562676Seschrock } 14572676Seschrock 14584787Sahrens if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 14594670Sahrens return (error); 14602676Seschrock 14614670Sahrens /* 14624670Sahrens * Check that this value is valid for this pool version 14634670Sahrens */ 14644670Sahrens switch (prop) { 14653886Sahl case ZFS_PROP_COMPRESSION: 14663886Sahl /* 14673886Sahl * If the user specified gzip compression, make sure 14683886Sahl * the SPA supports it. We ignore any errors here since 14693886Sahl * we'll catch them later. 14703886Sahl */ 14713886Sahl if (nvpair_type(elem) == DATA_TYPE_UINT64 && 14727042Sgw25295 nvpair_value_uint64(elem, &intval) == 0) { 14737042Sgw25295 if (intval >= ZIO_COMPRESS_GZIP_1 && 14747042Sgw25295 intval <= ZIO_COMPRESS_GZIP_9 && 14757184Stimh zfs_earlier_version(name, 14765331Samw SPA_VERSION_GZIP_COMPRESSION)) 14775331Samw return (ENOTSUP); 14787042Sgw25295 14797042Sgw25295 /* 14807042Sgw25295 * If this is a bootable dataset then 14817042Sgw25295 * verify that the compression algorithm 14827042Sgw25295 * is supported for booting. We must return 14837042Sgw25295 * something other than ENOTSUP since it 14847042Sgw25295 * implies a downrev pool version. 14857042Sgw25295 */ 14867042Sgw25295 if (zfs_is_bootfs(name) && 14877042Sgw25295 !BOOTFS_COMPRESS_VALID(intval)) 14887042Sgw25295 return (ERANGE); 14893886Sahl } 14903886Sahl break; 14914603Sahrens 14924603Sahrens case ZFS_PROP_COPIES: 14937184Stimh if (zfs_earlier_version(name, 14947184Stimh SPA_VERSION_DITTO_BLOCKS)) 14955331Samw return (ENOTSUP); 14964603Sahrens break; 14975977Smarks 14985977Smarks case ZFS_PROP_SHARESMB: 14996689Smaybee if (zpl_earlier_version(name, ZPL_VERSION_FUID)) 15005977Smarks return (ENOTSUP); 15015977Smarks break; 15028053SMark.Shellenbaum@Sun.COM 15038053SMark.Shellenbaum@Sun.COM case ZFS_PROP_ACLINHERIT: 15048053SMark.Shellenbaum@Sun.COM if (nvpair_type(elem) == DATA_TYPE_UINT64 && 15058053SMark.Shellenbaum@Sun.COM nvpair_value_uint64(elem, &intval) == 0) 15068053SMark.Shellenbaum@Sun.COM if (intval == ZFS_ACL_PASSTHROUGH_X && 15078053SMark.Shellenbaum@Sun.COM zfs_earlier_version(name, 15088053SMark.Shellenbaum@Sun.COM SPA_VERSION_PASSTHROUGH_X)) 15098053SMark.Shellenbaum@Sun.COM return (ENOTSUP); 15104603Sahrens } 15114543Smarks } 15124543Smarks 15138697SRichard.Morris@Sun.COM VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 15144543Smarks elem = NULL; 15154543Smarks while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 15164670Sahrens const char *propname = nvpair_name(elem); 15174670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 15184543Smarks 15195094Slling if (prop == ZPROP_INVAL) { 15204543Smarks VERIFY(nvpair_value_string(elem, &strval) == 0); 15214543Smarks error = dsl_prop_set(name, propname, 1, 15224543Smarks strlen(strval) + 1, strval); 15234543Smarks if (error == 0) 15244543Smarks continue; 15254543Smarks else 15268697SRichard.Morris@Sun.COM goto out; 15274543Smarks } 15282676Seschrock 15292676Seschrock switch (prop) { 15302676Seschrock case ZFS_PROP_QUOTA: 15312676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15324577Sahrens (error = dsl_dir_set_quota(name, intval)) != 0) 15338697SRichard.Morris@Sun.COM goto out; 15342676Seschrock break; 15352676Seschrock 15365378Sck153898 case ZFS_PROP_REFQUOTA: 15375378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15385378Sck153898 (error = dsl_dataset_set_quota(name, intval)) != 0) 15398697SRichard.Morris@Sun.COM goto out; 15405378Sck153898 break; 15415378Sck153898 15422676Seschrock case ZFS_PROP_RESERVATION: 15432676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15442676Seschrock (error = dsl_dir_set_reservation(name, 15452676Seschrock intval)) != 0) 15468697SRichard.Morris@Sun.COM goto out; 15472676Seschrock break; 1548789Sahrens 15495378Sck153898 case ZFS_PROP_REFRESERVATION: 15505378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15515378Sck153898 (error = dsl_dataset_set_reservation(name, 15525378Sck153898 intval)) != 0) 15538697SRichard.Morris@Sun.COM goto out; 15545378Sck153898 break; 15555378Sck153898 15562676Seschrock case ZFS_PROP_VOLSIZE: 15572676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15584787Sahrens (error = zvol_set_volsize(name, 15594787Sahrens ddi_driver_major(zfs_dip), intval)) != 0) 15608697SRichard.Morris@Sun.COM goto out; 15612676Seschrock break; 15622676Seschrock 15632676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 15642676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15654577Sahrens (error = zvol_set_volblocksize(name, intval)) != 0) 15668697SRichard.Morris@Sun.COM goto out; 15674577Sahrens break; 15684577Sahrens 15694577Sahrens case ZFS_PROP_VERSION: 15704577Sahrens if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 15714577Sahrens (error = zfs_set_version(name, intval)) != 0) 15728697SRichard.Morris@Sun.COM goto out; 15732676Seschrock break; 15742676Seschrock 15752676Seschrock default: 15762676Seschrock if (nvpair_type(elem) == DATA_TYPE_STRING) { 15772676Seschrock if (zfs_prop_get_type(prop) != 15788697SRichard.Morris@Sun.COM PROP_TYPE_STRING) { 15798697SRichard.Morris@Sun.COM error = EINVAL; 15808697SRichard.Morris@Sun.COM goto out; 15818697SRichard.Morris@Sun.COM } 15822676Seschrock } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 15832885Sahrens const char *unused; 15842885Sahrens 15852717Seschrock VERIFY(nvpair_value_uint64(elem, &intval) == 0); 15862676Seschrock 15872676Seschrock switch (zfs_prop_get_type(prop)) { 15884787Sahrens case PROP_TYPE_NUMBER: 15892676Seschrock break; 15904787Sahrens case PROP_TYPE_STRING: 15918697SRichard.Morris@Sun.COM error = EINVAL; 15928697SRichard.Morris@Sun.COM goto out; 15934787Sahrens case PROP_TYPE_INDEX: 15942717Seschrock if (zfs_prop_index_to_string(prop, 15958697SRichard.Morris@Sun.COM intval, &unused) != 0) { 15968697SRichard.Morris@Sun.COM error = EINVAL; 15978697SRichard.Morris@Sun.COM goto out; 15988697SRichard.Morris@Sun.COM } 15992676Seschrock break; 16002676Seschrock default: 16014577Sahrens cmn_err(CE_PANIC, 16024577Sahrens "unknown property type"); 16032676Seschrock break; 16042676Seschrock } 16052676Seschrock } else { 16068697SRichard.Morris@Sun.COM error = EINVAL; 16078697SRichard.Morris@Sun.COM goto out; 16082676Seschrock } 16098697SRichard.Morris@Sun.COM if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0) 16108697SRichard.Morris@Sun.COM goto out; 16112676Seschrock } 16122676Seschrock } 16132676Seschrock 16148697SRichard.Morris@Sun.COM if (nvlist_next_nvpair(genericnvl, NULL) != NULL) { 16158697SRichard.Morris@Sun.COM error = dsl_props_set(name, genericnvl); 16168697SRichard.Morris@Sun.COM } 16178697SRichard.Morris@Sun.COM out: 16188697SRichard.Morris@Sun.COM nvlist_free(genericnvl); 16198697SRichard.Morris@Sun.COM return (error); 1620789Sahrens } 1621789Sahrens 16225367Sahrens /* 16235367Sahrens * inputs: 16245367Sahrens * zc_name name of filesystem 16258697SRichard.Morris@Sun.COM * zc_value name of property to set 16265367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 16277265Sahrens * zc_cookie clear existing local props? 16285367Sahrens * 16295367Sahrens * outputs: none 16305367Sahrens */ 1631789Sahrens static int 16322676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 1633789Sahrens { 16342676Seschrock nvlist_t *nvl; 16352676Seschrock int error; 1636789Sahrens 16375094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 16385094Slling &nvl)) != 0) 16392676Seschrock return (error); 16402676Seschrock 16417265Sahrens if (zc->zc_cookie) { 16427265Sahrens nvlist_t *origprops; 16437265Sahrens objset_t *os; 16447265Sahrens 16457265Sahrens if (dmu_objset_open(zc->zc_name, DMU_OST_ANY, 16467265Sahrens DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 16477265Sahrens if (dsl_prop_get_all(os, &origprops, TRUE) == 0) { 16488536SDavid.Pacheco@Sun.COM clear_props(zc->zc_name, origprops, nvl); 16497265Sahrens nvlist_free(origprops); 16507265Sahrens } 16517265Sahrens dmu_objset_close(os); 16527265Sahrens } 16537265Sahrens 16547265Sahrens } 16557265Sahrens 16564787Sahrens error = zfs_set_prop_nvlist(zc->zc_name, nvl); 16574543Smarks 16582676Seschrock nvlist_free(nvl); 16592676Seschrock return (error); 1660789Sahrens } 1661789Sahrens 16625367Sahrens /* 16635367Sahrens * inputs: 16645367Sahrens * zc_name name of filesystem 16655367Sahrens * zc_value name of property to inherit 16665367Sahrens * 16675367Sahrens * outputs: none 16685367Sahrens */ 1669789Sahrens static int 16704849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 16714849Sahrens { 16724849Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 16734849Sahrens return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL)); 16744849Sahrens } 16754849Sahrens 16764849Sahrens static int 16774098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 16783912Slling { 16795094Slling nvlist_t *props; 16803912Slling spa_t *spa; 16815094Slling int error; 16828525SEric.Schrock@Sun.COM nvpair_t *elem; 16833912Slling 16845094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 16855094Slling &props))) 16863912Slling return (error); 16873912Slling 16888525SEric.Schrock@Sun.COM /* 16898525SEric.Schrock@Sun.COM * If the only property is the configfile, then just do a spa_lookup() 16908525SEric.Schrock@Sun.COM * to handle the faulted case. 16918525SEric.Schrock@Sun.COM */ 16928525SEric.Schrock@Sun.COM elem = nvlist_next_nvpair(props, NULL); 16938525SEric.Schrock@Sun.COM if (elem != NULL && strcmp(nvpair_name(elem), 16948525SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 16958525SEric.Schrock@Sun.COM nvlist_next_nvpair(props, elem) == NULL) { 16968525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 16978525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) { 16988525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 16998525SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 17008525SEric.Schrock@Sun.COM } 17018525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 17028525SEric.Schrock@Sun.COM if (spa != NULL) 17038525SEric.Schrock@Sun.COM return (0); 17048525SEric.Schrock@Sun.COM } 17058525SEric.Schrock@Sun.COM 17063912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 17075094Slling nvlist_free(props); 17083912Slling return (error); 17093912Slling } 17103912Slling 17115094Slling error = spa_prop_set(spa, props); 17123912Slling 17135094Slling nvlist_free(props); 17143912Slling spa_close(spa, FTAG); 17153912Slling 17163912Slling return (error); 17173912Slling } 17183912Slling 17193912Slling static int 17204098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 17213912Slling { 17223912Slling spa_t *spa; 17233912Slling int error; 17243912Slling nvlist_t *nvp = NULL; 17253912Slling 17268525SEric.Schrock@Sun.COM if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 17278525SEric.Schrock@Sun.COM /* 17288525SEric.Schrock@Sun.COM * If the pool is faulted, there may be properties we can still 17298525SEric.Schrock@Sun.COM * get (such as altroot and cachefile), so attempt to get them 17308525SEric.Schrock@Sun.COM * anyway. 17318525SEric.Schrock@Sun.COM */ 17328525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 17338525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) 17348525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 17358525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 17368525SEric.Schrock@Sun.COM } else { 17378525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 17388525SEric.Schrock@Sun.COM spa_close(spa, FTAG); 17398525SEric.Schrock@Sun.COM } 17403912Slling 17413912Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 17423912Slling error = put_nvlist(zc, nvp); 17433912Slling else 17443912Slling error = EFAULT; 17453912Slling 17468525SEric.Schrock@Sun.COM nvlist_free(nvp); 17473912Slling return (error); 17483912Slling } 17493912Slling 17503912Slling static int 17514543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 17524543Smarks { 17534543Smarks nvlist_t *nvp; 17544543Smarks int error; 17554543Smarks uint32_t uid; 17564543Smarks uint32_t gid; 17574543Smarks uint32_t *groups; 17584543Smarks uint_t group_cnt; 17594543Smarks cred_t *usercred; 17604543Smarks 17615094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 17625094Slling &nvp)) != 0) { 17634543Smarks return (error); 17644543Smarks } 17654543Smarks 17664543Smarks if ((error = nvlist_lookup_uint32(nvp, 17674543Smarks ZFS_DELEG_PERM_UID, &uid)) != 0) { 17684543Smarks nvlist_free(nvp); 17694543Smarks return (EPERM); 17704543Smarks } 17714543Smarks 17724543Smarks if ((error = nvlist_lookup_uint32(nvp, 17734543Smarks ZFS_DELEG_PERM_GID, &gid)) != 0) { 17744543Smarks nvlist_free(nvp); 17754543Smarks return (EPERM); 17764543Smarks } 17774543Smarks 17784543Smarks if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 17794543Smarks &groups, &group_cnt)) != 0) { 17804543Smarks nvlist_free(nvp); 17814543Smarks return (EPERM); 17824543Smarks } 17834543Smarks usercred = cralloc(); 17844543Smarks if ((crsetugid(usercred, uid, gid) != 0) || 17854543Smarks (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 17864543Smarks nvlist_free(nvp); 17874543Smarks crfree(usercred); 17884543Smarks return (EPERM); 17894543Smarks } 17904543Smarks nvlist_free(nvp); 17914543Smarks error = dsl_deleg_access(zc->zc_name, 17924787Sahrens zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred); 17934543Smarks crfree(usercred); 17944543Smarks return (error); 17954543Smarks } 17964543Smarks 17975367Sahrens /* 17985367Sahrens * inputs: 17995367Sahrens * zc_name name of filesystem 18005367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 18015367Sahrens * zc_perm_action allow/unallow flag 18025367Sahrens * 18035367Sahrens * outputs: none 18045367Sahrens */ 18054543Smarks static int 18064543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 18074543Smarks { 18084543Smarks int error; 18094543Smarks nvlist_t *fsaclnv = NULL; 18104543Smarks 18115094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 18125094Slling &fsaclnv)) != 0) 18134543Smarks return (error); 18144543Smarks 18154543Smarks /* 18164543Smarks * Verify nvlist is constructed correctly 18174543Smarks */ 18184543Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 18194543Smarks nvlist_free(fsaclnv); 18204543Smarks return (EINVAL); 18214543Smarks } 18224543Smarks 18234543Smarks /* 18244543Smarks * If we don't have PRIV_SYS_MOUNT, then validate 18254543Smarks * that user is allowed to hand out each permission in 18264543Smarks * the nvlist(s) 18274543Smarks */ 18284543Smarks 18294787Sahrens error = secpolicy_zfs(CRED()); 18304543Smarks if (error) { 18314787Sahrens if (zc->zc_perm_action == B_FALSE) { 18324787Sahrens error = dsl_deleg_can_allow(zc->zc_name, 18334787Sahrens fsaclnv, CRED()); 18344787Sahrens } else { 18354787Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 18364787Sahrens fsaclnv, CRED()); 18374787Sahrens } 18384543Smarks } 18394543Smarks 18404543Smarks if (error == 0) 18414543Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 18424543Smarks 18434543Smarks nvlist_free(fsaclnv); 18444543Smarks return (error); 18454543Smarks } 18464543Smarks 18475367Sahrens /* 18485367Sahrens * inputs: 18495367Sahrens * zc_name name of filesystem 18505367Sahrens * 18515367Sahrens * outputs: 18525367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 18535367Sahrens */ 18544543Smarks static int 18554543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 18564543Smarks { 18574543Smarks nvlist_t *nvp; 18584543Smarks int error; 18594543Smarks 18604543Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 18614543Smarks error = put_nvlist(zc, nvp); 18624543Smarks nvlist_free(nvp); 18634543Smarks } 18644543Smarks 18654543Smarks return (error); 18664543Smarks } 18674543Smarks 18685367Sahrens /* 18695367Sahrens * inputs: 18705367Sahrens * zc_name name of volume 18715367Sahrens * 18725367Sahrens * outputs: none 18735367Sahrens */ 18744543Smarks static int 1875789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc) 1876789Sahrens { 18774787Sahrens return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip))); 1878789Sahrens } 1879789Sahrens 18805367Sahrens /* 18815367Sahrens * inputs: 18825367Sahrens * zc_name name of volume 18835367Sahrens * 18845367Sahrens * outputs: none 18855367Sahrens */ 1886789Sahrens static int 1887789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc) 1888789Sahrens { 18892676Seschrock return (zvol_remove_minor(zc->zc_name)); 1890789Sahrens } 1891789Sahrens 1892789Sahrens /* 1893789Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 1894789Sahrens * or NULL if no suitable entry is found. The caller of this routine 1895789Sahrens * is responsible for releasing the returned vfs pointer. 1896789Sahrens */ 1897789Sahrens static vfs_t * 1898789Sahrens zfs_get_vfs(const char *resource) 1899789Sahrens { 1900789Sahrens struct vfs *vfsp; 1901789Sahrens struct vfs *vfs_found = NULL; 1902789Sahrens 1903789Sahrens vfs_list_read_lock(); 1904789Sahrens vfsp = rootvfs; 1905789Sahrens do { 1906789Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 1907789Sahrens VFS_HOLD(vfsp); 1908789Sahrens vfs_found = vfsp; 1909789Sahrens break; 1910789Sahrens } 1911789Sahrens vfsp = vfsp->vfs_next; 1912789Sahrens } while (vfsp != rootvfs); 1913789Sahrens vfs_list_unlock(); 1914789Sahrens return (vfs_found); 1915789Sahrens } 1916789Sahrens 19174543Smarks /* ARGSUSED */ 1918789Sahrens static void 19194543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 1920789Sahrens { 19215331Samw zfs_creat_t *zct = arg; 19225498Stimh 19235498Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 19245331Samw } 19255331Samw 19265498Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 19275498Stimh 19285331Samw /* 19295498Stimh * inputs: 19307184Stimh * createprops list of properties requested by creator 19317184Stimh * default_zplver zpl version to use if unspecified in createprops 19327184Stimh * fuids_ok fuids allowed in this version of the spa? 19337184Stimh * os parent objset pointer (NULL if root fs) 19345331Samw * 19355498Stimh * outputs: 19365498Stimh * zplprops values for the zplprops we attach to the master node object 19377184Stimh * is_ci true if requested file system will be purely case-insensitive 19385331Samw * 19395498Stimh * Determine the settings for utf8only, normalization and 19405498Stimh * casesensitivity. Specific values may have been requested by the 19415498Stimh * creator and/or we can inherit values from the parent dataset. If 19425498Stimh * the file system is of too early a vintage, a creator can not 19435498Stimh * request settings for these properties, even if the requested 19445498Stimh * setting is the default value. We don't actually want to create dsl 19455498Stimh * properties for these, so remove them from the source nvlist after 19465498Stimh * processing. 19475331Samw */ 19485331Samw static int 19497184Stimh zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver, 19507184Stimh boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops, 19517184Stimh boolean_t *is_ci) 19525331Samw { 19537184Stimh uint64_t zplver = default_zplver; 19545498Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 19555498Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 19565498Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 19575498Stimh 19585498Stimh ASSERT(zplprops != NULL); 19595498Stimh 19605375Stimh /* 19615498Stimh * Pull out creator prop choices, if any. 19625375Stimh */ 19635498Stimh if (createprops) { 19645498Stimh (void) nvlist_lookup_uint64(createprops, 19657184Stimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 19667184Stimh (void) nvlist_lookup_uint64(createprops, 19675498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 19685498Stimh (void) nvlist_remove_all(createprops, 19695498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 19705498Stimh (void) nvlist_lookup_uint64(createprops, 19715498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 19725498Stimh (void) nvlist_remove_all(createprops, 19735498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 19745498Stimh (void) nvlist_lookup_uint64(createprops, 19755498Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 19765498Stimh (void) nvlist_remove_all(createprops, 19775498Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 19785331Samw } 19795331Samw 19805375Stimh /* 19817184Stimh * If the zpl version requested is whacky or the file system 19827184Stimh * or pool is version is too "young" to support normalization 19837184Stimh * and the creator tried to set a value for one of the props, 19847184Stimh * error out. 19855498Stimh */ 19867184Stimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 19877184Stimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 19887184Stimh (zplver < ZPL_VERSION_NORMALIZATION && 19895498Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 19907184Stimh sense != ZFS_PROP_UNDEFINED))) 19915498Stimh return (ENOTSUP); 19925498Stimh 19935498Stimh /* 19945498Stimh * Put the version in the zplprops 19955498Stimh */ 19965498Stimh VERIFY(nvlist_add_uint64(zplprops, 19975498Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 19985498Stimh 19995498Stimh if (norm == ZFS_PROP_UNDEFINED) 20005498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 20015498Stimh VERIFY(nvlist_add_uint64(zplprops, 20025498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 20035498Stimh 20045498Stimh /* 20055498Stimh * If we're normalizing, names must always be valid UTF-8 strings. 20065498Stimh */ 20075498Stimh if (norm) 20085498Stimh u8 = 1; 20095498Stimh if (u8 == ZFS_PROP_UNDEFINED) 20105498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 20115498Stimh VERIFY(nvlist_add_uint64(zplprops, 20125498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 20135498Stimh 20145498Stimh if (sense == ZFS_PROP_UNDEFINED) 20155498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 20165498Stimh VERIFY(nvlist_add_uint64(zplprops, 20175498Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 20185498Stimh 20196492Stimh if (is_ci) 20206492Stimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 20216492Stimh 20227184Stimh return (0); 20237184Stimh } 20247184Stimh 20257184Stimh static int 20267184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 20277184Stimh nvlist_t *zplprops, boolean_t *is_ci) 20287184Stimh { 20297184Stimh boolean_t fuids_ok = B_TRUE; 20307184Stimh uint64_t zplver = ZPL_VERSION; 20317184Stimh objset_t *os = NULL; 20327184Stimh char parentname[MAXNAMELEN]; 20337184Stimh char *cp; 20347184Stimh int error; 20357184Stimh 20367184Stimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 20377184Stimh cp = strrchr(parentname, '/'); 20387184Stimh ASSERT(cp != NULL); 20397184Stimh cp[0] = '\0'; 20407184Stimh 20417184Stimh if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) { 20427184Stimh zplver = ZPL_VERSION_FUID - 1; 20437184Stimh fuids_ok = B_FALSE; 20447184Stimh } 20457184Stimh 20467184Stimh /* 20477184Stimh * Open parent object set so we can inherit zplprop values. 20487184Stimh */ 20497184Stimh if ((error = dmu_objset_open(parentname, DMU_OST_ANY, 20507184Stimh DS_MODE_USER | DS_MODE_READONLY, &os)) != 0) 20517184Stimh return (error); 20527184Stimh 20537184Stimh error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops, 20547184Stimh zplprops, is_ci); 20555498Stimh dmu_objset_close(os); 20567184Stimh return (error); 20577184Stimh } 20587184Stimh 20597184Stimh static int 20607184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 20617184Stimh nvlist_t *zplprops, boolean_t *is_ci) 20627184Stimh { 20637184Stimh boolean_t fuids_ok = B_TRUE; 20647184Stimh uint64_t zplver = ZPL_VERSION; 20657184Stimh int error; 20667184Stimh 20677184Stimh if (spa_vers < SPA_VERSION_FUID) { 20687184Stimh zplver = ZPL_VERSION_FUID - 1; 20697184Stimh fuids_ok = B_FALSE; 20707184Stimh } 20717184Stimh 20727184Stimh error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops, 20737184Stimh zplprops, is_ci); 20747184Stimh return (error); 2075789Sahrens } 2076789Sahrens 20775367Sahrens /* 20785367Sahrens * inputs: 20795367Sahrens * zc_objset_type type of objset to create (fs vs zvol) 20805367Sahrens * zc_name name of new objset 20815367Sahrens * zc_value name of snapshot to clone from (may be empty) 20825367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 20835367Sahrens * 20845498Stimh * outputs: none 20855367Sahrens */ 2086789Sahrens static int 2087789Sahrens zfs_ioc_create(zfs_cmd_t *zc) 2088789Sahrens { 2089789Sahrens objset_t *clone; 2090789Sahrens int error = 0; 20915331Samw zfs_creat_t zct; 20924543Smarks nvlist_t *nvprops = NULL; 20934543Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2094789Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2095789Sahrens 2096789Sahrens switch (type) { 2097789Sahrens 2098789Sahrens case DMU_OST_ZFS: 2099789Sahrens cbfunc = zfs_create_cb; 2100789Sahrens break; 2101789Sahrens 2102789Sahrens case DMU_OST_ZVOL: 2103789Sahrens cbfunc = zvol_create_cb; 2104789Sahrens break; 2105789Sahrens 2106789Sahrens default: 21072199Sahrens cbfunc = NULL; 21086423Sgw25295 break; 21092199Sahrens } 21105326Sek110237 if (strchr(zc->zc_name, '@') || 21115326Sek110237 strchr(zc->zc_name, '%')) 2112789Sahrens return (EINVAL); 2113789Sahrens 21142676Seschrock if (zc->zc_nvlist_src != NULL && 21155094Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 21165094Slling &nvprops)) != 0) 21172676Seschrock return (error); 21182676Seschrock 21195498Stimh zct.zct_zplprops = NULL; 21205331Samw zct.zct_props = nvprops; 21215331Samw 21222676Seschrock if (zc->zc_value[0] != '\0') { 2123789Sahrens /* 2124789Sahrens * We're creating a clone of an existing snapshot. 2125789Sahrens */ 21262676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 21272676Seschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 21284543Smarks nvlist_free(nvprops); 2129789Sahrens return (EINVAL); 21302676Seschrock } 2131789Sahrens 21322676Seschrock error = dmu_objset_open(zc->zc_value, type, 21336689Smaybee DS_MODE_USER | DS_MODE_READONLY, &clone); 21342676Seschrock if (error) { 21354543Smarks nvlist_free(nvprops); 2136789Sahrens return (error); 21372676Seschrock } 21386492Stimh 21396492Stimh error = dmu_objset_create(zc->zc_name, type, clone, 0, 21406492Stimh NULL, NULL); 21415331Samw if (error) { 21425331Samw dmu_objset_close(clone); 21435331Samw nvlist_free(nvprops); 21445331Samw return (error); 21455331Samw } 2146789Sahrens dmu_objset_close(clone); 2147789Sahrens } else { 21486492Stimh boolean_t is_insensitive = B_FALSE; 21496492Stimh 21502676Seschrock if (cbfunc == NULL) { 21514543Smarks nvlist_free(nvprops); 21522199Sahrens return (EINVAL); 21532676Seschrock } 21542676Seschrock 2155789Sahrens if (type == DMU_OST_ZVOL) { 21562676Seschrock uint64_t volsize, volblocksize; 21572676Seschrock 21584543Smarks if (nvprops == NULL || 21594543Smarks nvlist_lookup_uint64(nvprops, 21602676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 21612676Seschrock &volsize) != 0) { 21624543Smarks nvlist_free(nvprops); 21632676Seschrock return (EINVAL); 21642676Seschrock } 21652676Seschrock 21664543Smarks if ((error = nvlist_lookup_uint64(nvprops, 21672676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 21682676Seschrock &volblocksize)) != 0 && error != ENOENT) { 21694543Smarks nvlist_free(nvprops); 21702676Seschrock return (EINVAL); 21712676Seschrock } 21721133Seschrock 21732676Seschrock if (error != 0) 21742676Seschrock volblocksize = zfs_prop_default_numeric( 21752676Seschrock ZFS_PROP_VOLBLOCKSIZE); 21762676Seschrock 21772676Seschrock if ((error = zvol_check_volblocksize( 21782676Seschrock volblocksize)) != 0 || 21792676Seschrock (error = zvol_check_volsize(volsize, 21802676Seschrock volblocksize)) != 0) { 21814543Smarks nvlist_free(nvprops); 2182789Sahrens return (error); 21832676Seschrock } 21844577Sahrens } else if (type == DMU_OST_ZFS) { 21855331Samw int error; 21865331Samw 21875498Stimh /* 21885331Samw * We have to have normalization and 21895331Samw * case-folding flags correct when we do the 21905331Samw * file system creation, so go figure them out 21915498Stimh * now. 21925331Samw */ 21935498Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 21945498Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 21955498Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 21967184Stimh zct.zct_zplprops, &is_insensitive); 21975331Samw if (error != 0) { 21985331Samw nvlist_free(nvprops); 21995498Stimh nvlist_free(zct.zct_zplprops); 22005331Samw return (error); 22014577Sahrens } 22022676Seschrock } 22036492Stimh error = dmu_objset_create(zc->zc_name, type, NULL, 22046492Stimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 22055498Stimh nvlist_free(zct.zct_zplprops); 2206789Sahrens } 22072676Seschrock 22082676Seschrock /* 22092676Seschrock * It would be nice to do this atomically. 22102676Seschrock */ 22112676Seschrock if (error == 0) { 22124787Sahrens if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0) 22132676Seschrock (void) dmu_objset_destroy(zc->zc_name); 22142676Seschrock } 22154543Smarks nvlist_free(nvprops); 2216789Sahrens return (error); 2217789Sahrens } 2218789Sahrens 22197265Sahrens struct snap_prop_arg { 22207265Sahrens nvlist_t *nvprops; 22217265Sahrens const char *snapname; 22227265Sahrens }; 22237265Sahrens 22247265Sahrens static int 22257265Sahrens set_snap_props(char *name, void *arg) 22267265Sahrens { 22277265Sahrens struct snap_prop_arg *snpa = arg; 22287265Sahrens int len = strlen(name) + strlen(snpa->snapname) + 2; 22297265Sahrens char *buf = kmem_alloc(len, KM_SLEEP); 22307265Sahrens int err; 22317265Sahrens 22327265Sahrens (void) snprintf(buf, len, "%s@%s", name, snpa->snapname); 22337265Sahrens err = zfs_set_prop_nvlist(buf, snpa->nvprops); 22347265Sahrens if (err) 22357265Sahrens (void) dmu_objset_destroy(buf); 22367265Sahrens kmem_free(buf, len); 22377265Sahrens return (err); 22387265Sahrens } 22397265Sahrens 22405367Sahrens /* 22415367Sahrens * inputs: 22425367Sahrens * zc_name name of filesystem 22435367Sahrens * zc_value short name of snapshot 22445367Sahrens * zc_cookie recursive flag 22455367Sahrens * 22465367Sahrens * outputs: none 22475367Sahrens */ 2248789Sahrens static int 22492199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 22502199Sahrens { 22517265Sahrens nvlist_t *nvprops = NULL; 22527265Sahrens int error; 22537265Sahrens boolean_t recursive = zc->zc_cookie; 22547265Sahrens 22552676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 22562199Sahrens return (EINVAL); 22577265Sahrens 22587265Sahrens if (zc->zc_nvlist_src != NULL && 22597265Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 22607265Sahrens &nvprops)) != 0) 22617265Sahrens return (error); 22627265Sahrens 22637265Sahrens error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, recursive); 22647265Sahrens 22657265Sahrens /* 22667265Sahrens * It would be nice to do this atomically. 22677265Sahrens */ 22687265Sahrens if (error == 0) { 22697265Sahrens struct snap_prop_arg snpa; 22707265Sahrens snpa.nvprops = nvprops; 22717265Sahrens snpa.snapname = zc->zc_value; 22727265Sahrens if (recursive) { 22737265Sahrens error = dmu_objset_find(zc->zc_name, 22747265Sahrens set_snap_props, &snpa, DS_FIND_CHILDREN); 22757265Sahrens if (error) { 22767265Sahrens (void) dmu_snapshots_destroy(zc->zc_name, 22777265Sahrens zc->zc_value); 22787265Sahrens } 22797265Sahrens } else { 22807265Sahrens error = set_snap_props(zc->zc_name, &snpa); 22817265Sahrens } 22827265Sahrens } 22837265Sahrens nvlist_free(nvprops); 22847265Sahrens return (error); 22852199Sahrens } 22862199Sahrens 22874007Smmusante int 22882199Sahrens zfs_unmount_snap(char *name, void *arg) 2289789Sahrens { 22902417Sahrens vfs_t *vfsp = NULL; 22912199Sahrens 22926689Smaybee if (arg) { 22936689Smaybee char *snapname = arg; 22946689Smaybee int len = strlen(name) + strlen(snapname) + 2; 22956689Smaybee char *buf = kmem_alloc(len, KM_SLEEP); 22966689Smaybee 22976689Smaybee (void) strcpy(buf, name); 22986689Smaybee (void) strcat(buf, "@"); 22996689Smaybee (void) strcat(buf, snapname); 23006689Smaybee vfsp = zfs_get_vfs(buf); 23016689Smaybee kmem_free(buf, len); 23022417Sahrens } else if (strchr(name, '@')) { 23032199Sahrens vfsp = zfs_get_vfs(name); 23042199Sahrens } 23052199Sahrens 23062199Sahrens if (vfsp) { 23072199Sahrens /* 23082199Sahrens * Always force the unmount for snapshots. 23092199Sahrens */ 23102199Sahrens int flag = MS_FORCE; 2311789Sahrens int err; 2312789Sahrens 23132199Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 23142199Sahrens VFS_RELE(vfsp); 23152199Sahrens return (err); 23162199Sahrens } 23172199Sahrens VFS_RELE(vfsp); 23182199Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 23192199Sahrens return (err); 23202199Sahrens } 23212199Sahrens return (0); 23222199Sahrens } 23232199Sahrens 23245367Sahrens /* 23255367Sahrens * inputs: 23265367Sahrens * zc_name name of filesystem 23275367Sahrens * zc_value short name of snapshot 23285367Sahrens * 23295367Sahrens * outputs: none 23305367Sahrens */ 23312199Sahrens static int 23322199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 23332199Sahrens { 23342199Sahrens int err; 2335789Sahrens 23362676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 23372199Sahrens return (EINVAL); 23382199Sahrens err = dmu_objset_find(zc->zc_name, 23392676Seschrock zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 23402199Sahrens if (err) 23412199Sahrens return (err); 23422676Seschrock return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value)); 23432199Sahrens } 23442199Sahrens 23455367Sahrens /* 23465367Sahrens * inputs: 23475367Sahrens * zc_name name of dataset to destroy 23485367Sahrens * zc_objset_type type of objset 23495367Sahrens * 23505367Sahrens * outputs: none 23515367Sahrens */ 23522199Sahrens static int 23532199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 23542199Sahrens { 23552199Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 23562199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 23572199Sahrens if (err) 23582199Sahrens return (err); 2359789Sahrens } 2360789Sahrens 2361789Sahrens return (dmu_objset_destroy(zc->zc_name)); 2362789Sahrens } 2363789Sahrens 23645367Sahrens /* 23655367Sahrens * inputs: 23665446Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 23675367Sahrens * 23685367Sahrens * outputs: none 23695367Sahrens */ 2370789Sahrens static int 2371789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 2372789Sahrens { 23735446Sahrens objset_t *os; 23745446Sahrens int error; 23755446Sahrens zfsvfs_t *zfsvfs = NULL; 23765446Sahrens 23775446Sahrens /* 23785446Sahrens * Get the zfsvfs for the receiving objset. There 23795446Sahrens * won't be one if we're operating on a zvol, if the 23805446Sahrens * objset doesn't exist yet, or is not mounted. 23815446Sahrens */ 23826689Smaybee error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os); 23835446Sahrens if (error) 23845446Sahrens return (error); 23855446Sahrens 23865446Sahrens if (dmu_objset_type(os) == DMU_OST_ZFS) { 23875446Sahrens mutex_enter(&os->os->os_user_ptr_lock); 23885446Sahrens zfsvfs = dmu_objset_get_user(os); 23895446Sahrens if (zfsvfs != NULL) 23905446Sahrens VFS_HOLD(zfsvfs->z_vfs); 23915446Sahrens mutex_exit(&os->os->os_user_ptr_lock); 23925446Sahrens } 23935446Sahrens 23945446Sahrens if (zfsvfs != NULL) { 23958012SEric.Taylor@Sun.COM char *osname; 23965446Sahrens int mode; 23975446Sahrens 23988012SEric.Taylor@Sun.COM osname = kmem_alloc(MAXNAMELEN, KM_SLEEP); 23996083Sek110237 error = zfs_suspend_fs(zfsvfs, osname, &mode); 24006083Sek110237 if (error == 0) { 24016083Sek110237 int resume_err; 24026083Sek110237 24036083Sek110237 ASSERT(strcmp(osname, zc->zc_name) == 0); 24046083Sek110237 error = dmu_objset_rollback(os); 24056083Sek110237 resume_err = zfs_resume_fs(zfsvfs, osname, mode); 24066083Sek110237 error = error ? error : resume_err; 24076083Sek110237 } else { 24086083Sek110237 dmu_objset_close(os); 24096083Sek110237 } 24108012SEric.Taylor@Sun.COM kmem_free(osname, MAXNAMELEN); 24115446Sahrens VFS_RELE(zfsvfs->z_vfs); 24125446Sahrens } else { 24135446Sahrens error = dmu_objset_rollback(os); 24145446Sahrens } 24156689Smaybee /* Note, the dmu_objset_rollback() releases the objset for us. */ 24165446Sahrens 24175446Sahrens return (error); 2418789Sahrens } 2419789Sahrens 24205367Sahrens /* 24215367Sahrens * inputs: 24225367Sahrens * zc_name old name of dataset 24235367Sahrens * zc_value new name of dataset 24245367Sahrens * zc_cookie recursive flag (only valid for snapshots) 24255367Sahrens * 24265367Sahrens * outputs: none 24275367Sahrens */ 2428789Sahrens static int 2429789Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 2430789Sahrens { 24314490Svb160487 boolean_t recursive = zc->zc_cookie & 1; 24324007Smmusante 24332676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 24345326Sek110237 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 24355326Sek110237 strchr(zc->zc_value, '%')) 2436789Sahrens return (EINVAL); 2437789Sahrens 24384007Smmusante /* 24394007Smmusante * Unmount snapshot unless we're doing a recursive rename, 24404007Smmusante * in which case the dataset code figures out which snapshots 24414007Smmusante * to unmount. 24424007Smmusante */ 24434007Smmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 2444789Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 24452199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 24462199Sahrens if (err) 24472199Sahrens return (err); 2448789Sahrens } 24494007Smmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 2450789Sahrens } 2451789Sahrens 24526689Smaybee static void 24538536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops) 24546689Smaybee { 24556689Smaybee zfs_cmd_t *zc; 24566689Smaybee nvpair_t *prop; 24576689Smaybee 24586689Smaybee if (props == NULL) 24596689Smaybee return; 24606689Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 24616689Smaybee (void) strcpy(zc->zc_name, dataset); 24626689Smaybee for (prop = nvlist_next_nvpair(props, NULL); prop; 24636689Smaybee prop = nvlist_next_nvpair(props, prop)) { 24648536SDavid.Pacheco@Sun.COM if (newprops != NULL && 24658536SDavid.Pacheco@Sun.COM nvlist_exists(newprops, nvpair_name(prop))) 24668536SDavid.Pacheco@Sun.COM continue; 24676689Smaybee (void) strcpy(zc->zc_value, nvpair_name(prop)); 24686689Smaybee if (zfs_secpolicy_inherit(zc, CRED()) == 0) 24696689Smaybee (void) zfs_ioc_inherit_prop(zc); 24706689Smaybee } 24716689Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 24726689Smaybee } 24736689Smaybee 24745367Sahrens /* 24755367Sahrens * inputs: 24765367Sahrens * zc_name name of containing filesystem 24775367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 24785367Sahrens * zc_value name of snapshot to create 24795367Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 24805367Sahrens * zc_cookie file descriptor to recv from 24815367Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 24825367Sahrens * zc_guid force flag 24835367Sahrens * 24845367Sahrens * outputs: 24855367Sahrens * zc_cookie number of bytes read 24865367Sahrens */ 2487789Sahrens static int 24885367Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 2489789Sahrens { 2490789Sahrens file_t *fp; 24915326Sek110237 objset_t *os; 24925367Sahrens dmu_recv_cookie_t drc; 24935326Sek110237 zfsvfs_t *zfsvfs = NULL; 24945326Sek110237 boolean_t force = (boolean_t)zc->zc_guid; 2495789Sahrens int error, fd; 24965367Sahrens offset_t off; 24975367Sahrens nvlist_t *props = NULL; 24986689Smaybee nvlist_t *origprops = NULL; 24995367Sahrens objset_t *origin = NULL; 25005367Sahrens char *tosnap; 25015367Sahrens char tofs[ZFS_MAXNAMELEN]; 2502789Sahrens 25033265Sahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 25045326Sek110237 strchr(zc->zc_value, '@') == NULL || 25055326Sek110237 strchr(zc->zc_value, '%')) 25063265Sahrens return (EINVAL); 25073265Sahrens 25085367Sahrens (void) strcpy(tofs, zc->zc_value); 25095367Sahrens tosnap = strchr(tofs, '@'); 25105367Sahrens *tosnap = '\0'; 25115367Sahrens tosnap++; 25125367Sahrens 25135367Sahrens if (zc->zc_nvlist_src != NULL && 25145367Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 25155367Sahrens &props)) != 0) 25165367Sahrens return (error); 25175367Sahrens 2518789Sahrens fd = zc->zc_cookie; 2519789Sahrens fp = getf(fd); 25205367Sahrens if (fp == NULL) { 25215367Sahrens nvlist_free(props); 2522789Sahrens return (EBADF); 25235367Sahrens } 25245326Sek110237 25256689Smaybee if (dmu_objset_open(tofs, DMU_OST_ANY, 25266689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 25276689Smaybee /* 25286689Smaybee * Try to get the zfsvfs for the receiving objset. 25296689Smaybee * There won't be one if we're operating on a zvol, 25306689Smaybee * if the objset doesn't exist yet, or is not mounted. 25316689Smaybee */ 25325446Sahrens mutex_enter(&os->os->os_user_ptr_lock); 25336689Smaybee if (zfsvfs = dmu_objset_get_user(os)) { 25346083Sek110237 if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) { 25356689Smaybee mutex_exit(&os->os->os_user_ptr_lock); 25366083Sek110237 dmu_objset_close(os); 25376689Smaybee zfsvfs = NULL; 25386689Smaybee error = EBUSY; 25396689Smaybee goto out; 25406083Sek110237 } 25416689Smaybee VFS_HOLD(zfsvfs->z_vfs); 25426083Sek110237 } 25436689Smaybee mutex_exit(&os->os->os_user_ptr_lock); 25446689Smaybee 25456689Smaybee /* 25466689Smaybee * If new properties are supplied, they are to completely 25476689Smaybee * replace the existing ones, so stash away the existing ones. 25486689Smaybee */ 25496689Smaybee if (props) 25506689Smaybee (void) dsl_prop_get_all(os, &origprops, TRUE); 25516689Smaybee 25525326Sek110237 dmu_objset_close(os); 25535326Sek110237 } 25545326Sek110237 25555367Sahrens if (zc->zc_string[0]) { 25565367Sahrens error = dmu_objset_open(zc->zc_string, DMU_OST_ANY, 25576689Smaybee DS_MODE_USER | DS_MODE_READONLY, &origin); 25586689Smaybee if (error) 25596689Smaybee goto out; 25605367Sahrens } 25615367Sahrens 25625367Sahrens error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record, 25635367Sahrens force, origin, zfsvfs != NULL, &drc); 25645367Sahrens if (origin) 25655367Sahrens dmu_objset_close(origin); 25666689Smaybee if (error) 25676689Smaybee goto out; 25685326Sek110237 25695326Sek110237 /* 25706689Smaybee * Reset properties. We do this before we receive the stream 25716689Smaybee * so that the properties are applied to the new data. 25725326Sek110237 */ 25735367Sahrens if (props) { 25748536SDavid.Pacheco@Sun.COM clear_props(tofs, origprops, props); 25756689Smaybee /* 25766689Smaybee * XXX - Note, this is all-or-nothing; should be best-effort. 25776689Smaybee */ 25786689Smaybee (void) zfs_set_prop_nvlist(tofs, props); 25795367Sahrens } 25805367Sahrens 25815367Sahrens off = fp->f_offset; 25825367Sahrens error = dmu_recv_stream(&drc, fp->f_vnode, &off); 25835367Sahrens 25846689Smaybee if (error == 0 && zfsvfs) { 25858012SEric.Taylor@Sun.COM char *osname; 25866689Smaybee int mode; 25876689Smaybee 25886689Smaybee /* online recv */ 25898012SEric.Taylor@Sun.COM osname = kmem_alloc(MAXNAMELEN, KM_SLEEP); 25906689Smaybee error = zfs_suspend_fs(zfsvfs, osname, &mode); 25916689Smaybee if (error == 0) { 25926689Smaybee int resume_err; 25936689Smaybee 25946689Smaybee error = dmu_recv_end(&drc); 25956689Smaybee resume_err = zfs_resume_fs(zfsvfs, osname, mode); 25966689Smaybee error = error ? error : resume_err; 25975367Sahrens } else { 25986689Smaybee dmu_recv_abort_cleanup(&drc); 25995367Sahrens } 26008012SEric.Taylor@Sun.COM kmem_free(osname, MAXNAMELEN); 26016689Smaybee } else if (error == 0) { 26026689Smaybee error = dmu_recv_end(&drc); 26036083Sek110237 } 26045367Sahrens 26055367Sahrens zc->zc_cookie = off - fp->f_offset; 26065367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 26075367Sahrens fp->f_offset = off; 26082885Sahrens 26096689Smaybee /* 26106689Smaybee * On error, restore the original props. 26116689Smaybee */ 26126689Smaybee if (error && props) { 26138536SDavid.Pacheco@Sun.COM clear_props(tofs, props, NULL); 26146689Smaybee (void) zfs_set_prop_nvlist(tofs, origprops); 26156689Smaybee } 26166689Smaybee out: 26176689Smaybee if (zfsvfs) { 26186689Smaybee mutex_exit(&zfsvfs->z_online_recv_lock); 26196689Smaybee VFS_RELE(zfsvfs->z_vfs); 26206689Smaybee } 26216689Smaybee nvlist_free(props); 26226689Smaybee nvlist_free(origprops); 2623789Sahrens releasef(fd); 2624789Sahrens return (error); 2625789Sahrens } 2626789Sahrens 26275367Sahrens /* 26285367Sahrens * inputs: 26295367Sahrens * zc_name name of snapshot to send 26305367Sahrens * zc_value short name of incremental fromsnap (may be empty) 26315367Sahrens * zc_cookie file descriptor to send stream to 26325367Sahrens * zc_obj fromorigin flag (mutually exclusive with zc_value) 26335367Sahrens * 26345367Sahrens * outputs: none 26355367Sahrens */ 2636789Sahrens static int 26375367Sahrens zfs_ioc_send(zfs_cmd_t *zc) 2638789Sahrens { 2639789Sahrens objset_t *fromsnap = NULL; 2640789Sahrens objset_t *tosnap; 2641789Sahrens file_t *fp; 2642789Sahrens int error; 26435367Sahrens offset_t off; 2644789Sahrens 2645789Sahrens error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 26466689Smaybee DS_MODE_USER | DS_MODE_READONLY, &tosnap); 2647789Sahrens if (error) 2648789Sahrens return (error); 2649789Sahrens 26502676Seschrock if (zc->zc_value[0] != '\0') { 26518012SEric.Taylor@Sun.COM char *buf; 26522885Sahrens char *cp; 26532885Sahrens 26548012SEric.Taylor@Sun.COM buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 26558012SEric.Taylor@Sun.COM (void) strncpy(buf, zc->zc_name, MAXPATHLEN); 26562885Sahrens cp = strchr(buf, '@'); 26572885Sahrens if (cp) 26582885Sahrens *(cp+1) = 0; 26598012SEric.Taylor@Sun.COM (void) strncat(buf, zc->zc_value, MAXPATHLEN); 26602885Sahrens error = dmu_objset_open(buf, DMU_OST_ANY, 26616689Smaybee DS_MODE_USER | DS_MODE_READONLY, &fromsnap); 26628012SEric.Taylor@Sun.COM kmem_free(buf, MAXPATHLEN); 2663789Sahrens if (error) { 2664789Sahrens dmu_objset_close(tosnap); 2665789Sahrens return (error); 2666789Sahrens } 2667789Sahrens } 2668789Sahrens 2669789Sahrens fp = getf(zc->zc_cookie); 2670789Sahrens if (fp == NULL) { 2671789Sahrens dmu_objset_close(tosnap); 2672789Sahrens if (fromsnap) 2673789Sahrens dmu_objset_close(fromsnap); 2674789Sahrens return (EBADF); 2675789Sahrens } 2676789Sahrens 26775367Sahrens off = fp->f_offset; 26785367Sahrens error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 26795367Sahrens 26805367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 26815367Sahrens fp->f_offset = off; 2682789Sahrens releasef(zc->zc_cookie); 2683789Sahrens if (fromsnap) 2684789Sahrens dmu_objset_close(fromsnap); 2685789Sahrens dmu_objset_close(tosnap); 2686789Sahrens return (error); 2687789Sahrens } 2688789Sahrens 26891544Seschrock static int 26901544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 26911544Seschrock { 26921544Seschrock int id, error; 26931544Seschrock 26941544Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 26951544Seschrock &zc->zc_inject_record); 26961544Seschrock 26971544Seschrock if (error == 0) 26981544Seschrock zc->zc_guid = (uint64_t)id; 26991544Seschrock 27001544Seschrock return (error); 27011544Seschrock } 27021544Seschrock 27031544Seschrock static int 27041544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 27051544Seschrock { 27061544Seschrock return (zio_clear_fault((int)zc->zc_guid)); 27071544Seschrock } 27081544Seschrock 27091544Seschrock static int 27101544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 27111544Seschrock { 27121544Seschrock int id = (int)zc->zc_guid; 27131544Seschrock int error; 27141544Seschrock 27151544Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 27161544Seschrock &zc->zc_inject_record); 27171544Seschrock 27181544Seschrock zc->zc_guid = id; 27191544Seschrock 27201544Seschrock return (error); 27211544Seschrock } 27221544Seschrock 27231544Seschrock static int 27241544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 27251544Seschrock { 27261544Seschrock spa_t *spa; 27271544Seschrock int error; 27282676Seschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 27291544Seschrock 27301544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 27311544Seschrock return (error); 27321544Seschrock 27332676Seschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 27341544Seschrock &count); 27351544Seschrock if (error == 0) 27362676Seschrock zc->zc_nvlist_dst_size = count; 27371544Seschrock else 27382676Seschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 27391544Seschrock 27401544Seschrock spa_close(spa, FTAG); 27411544Seschrock 27421544Seschrock return (error); 27431544Seschrock } 27441544Seschrock 27451544Seschrock static int 27461544Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 27471544Seschrock { 27481544Seschrock spa_t *spa; 27491544Seschrock vdev_t *vd; 27501544Seschrock int error; 27511544Seschrock 27527294Sperrin /* 27537294Sperrin * On zpool clear we also fix up missing slogs 27547294Sperrin */ 27557294Sperrin mutex_enter(&spa_namespace_lock); 27567294Sperrin spa = spa_lookup(zc->zc_name); 27577294Sperrin if (spa == NULL) { 27587294Sperrin mutex_exit(&spa_namespace_lock); 27597294Sperrin return (EIO); 27607294Sperrin } 27617294Sperrin if (spa->spa_log_state == SPA_LOG_MISSING) { 27627294Sperrin /* we need to let spa_open/spa_load clear the chains */ 27637294Sperrin spa->spa_log_state = SPA_LOG_CLEAR; 27647294Sperrin } 27657294Sperrin mutex_exit(&spa_namespace_lock); 27667294Sperrin 27671544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 27681544Seschrock return (error); 27691544Seschrock 27707754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 27711544Seschrock 27722676Seschrock if (zc->zc_guid == 0) { 27731544Seschrock vd = NULL; 27746643Seschrock } else { 27756643Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 27765450Sbrendan if (vd == NULL) { 27777754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, ENODEV); 27785450Sbrendan spa_close(spa, FTAG); 27795450Sbrendan return (ENODEV); 27805450Sbrendan } 27811544Seschrock } 27821544Seschrock 27837754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 27847754SJeff.Bonwick@Sun.COM 27857754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 27867754SJeff.Bonwick@Sun.COM 27877754SJeff.Bonwick@Sun.COM /* 27887754SJeff.Bonwick@Sun.COM * Resume any suspended I/Os. 27897754SJeff.Bonwick@Sun.COM */ 27907754SJeff.Bonwick@Sun.COM zio_resume(spa); 27911544Seschrock 27921544Seschrock spa_close(spa, FTAG); 27931544Seschrock 27941544Seschrock return (0); 27951544Seschrock } 27961544Seschrock 27975367Sahrens /* 27985367Sahrens * inputs: 27995367Sahrens * zc_name name of filesystem 28005367Sahrens * zc_value name of origin snapshot 28015367Sahrens * 28025367Sahrens * outputs: none 28035367Sahrens */ 28041544Seschrock static int 28052082Seschrock zfs_ioc_promote(zfs_cmd_t *zc) 28062082Seschrock { 28072417Sahrens char *cp; 28082417Sahrens 28092417Sahrens /* 28102417Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 28112417Sahrens * it's easier. 28122417Sahrens */ 28132676Seschrock cp = strchr(zc->zc_value, '@'); 28142417Sahrens if (cp) 28152417Sahrens *cp = '\0'; 28162676Seschrock (void) dmu_objset_find(zc->zc_value, 28172417Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 28182082Seschrock return (dsl_dataset_promote(zc->zc_name)); 28192082Seschrock } 28202082Seschrock 28214543Smarks /* 28224543Smarks * We don't want to have a hard dependency 28234543Smarks * against some special symbols in sharefs 28245331Samw * nfs, and smbsrv. Determine them if needed when 28254543Smarks * the first file system is shared. 28265331Samw * Neither sharefs, nfs or smbsrv are unloadable modules. 28274543Smarks */ 28285331Samw int (*znfsexport_fs)(void *arg); 28294543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 28305331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 28315331Samw 28325331Samw int zfs_nfsshare_inited; 28335331Samw int zfs_smbshare_inited; 28345331Samw 28354543Smarks ddi_modhandle_t nfs_mod; 28364543Smarks ddi_modhandle_t sharefs_mod; 28375331Samw ddi_modhandle_t smbsrv_mod; 28384543Smarks kmutex_t zfs_share_lock; 28394543Smarks 28404543Smarks static int 28415331Samw zfs_init_sharefs() 28425331Samw { 28435331Samw int error; 28445331Samw 28455331Samw ASSERT(MUTEX_HELD(&zfs_share_lock)); 28465331Samw /* Both NFS and SMB shares also require sharetab support. */ 28475331Samw if (sharefs_mod == NULL && ((sharefs_mod = 28485331Samw ddi_modopen("fs/sharefs", 28495331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 28505331Samw return (ENOSYS); 28515331Samw } 28525331Samw if (zshare_fs == NULL && ((zshare_fs = 28535331Samw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 28545331Samw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 28555331Samw return (ENOSYS); 28565331Samw } 28575331Samw return (0); 28585331Samw } 28595331Samw 28605331Samw static int 28614543Smarks zfs_ioc_share(zfs_cmd_t *zc) 28624543Smarks { 28634543Smarks int error; 28644543Smarks int opcode; 28654543Smarks 28665331Samw switch (zc->zc_share.z_sharetype) { 28675331Samw case ZFS_SHARE_NFS: 28685331Samw case ZFS_UNSHARE_NFS: 28695331Samw if (zfs_nfsshare_inited == 0) { 28705331Samw mutex_enter(&zfs_share_lock); 28715331Samw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 28725331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 28735331Samw mutex_exit(&zfs_share_lock); 28745331Samw return (ENOSYS); 28755331Samw } 28765331Samw if (znfsexport_fs == NULL && 28775331Samw ((znfsexport_fs = (int (*)(void *)) 28785331Samw ddi_modsym(nfs_mod, 28795331Samw "nfs_export", &error)) == NULL)) { 28805331Samw mutex_exit(&zfs_share_lock); 28815331Samw return (ENOSYS); 28825331Samw } 28835331Samw error = zfs_init_sharefs(); 28845331Samw if (error) { 28855331Samw mutex_exit(&zfs_share_lock); 28865331Samw return (ENOSYS); 28875331Samw } 28885331Samw zfs_nfsshare_inited = 1; 28894543Smarks mutex_exit(&zfs_share_lock); 28904543Smarks } 28915331Samw break; 28925331Samw case ZFS_SHARE_SMB: 28935331Samw case ZFS_UNSHARE_SMB: 28945331Samw if (zfs_smbshare_inited == 0) { 28955331Samw mutex_enter(&zfs_share_lock); 28965331Samw if (smbsrv_mod == NULL && ((smbsrv_mod = 28975331Samw ddi_modopen("drv/smbsrv", 28985331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 28995331Samw mutex_exit(&zfs_share_lock); 29005331Samw return (ENOSYS); 29015331Samw } 29025331Samw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 29035331Samw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 29046139Sjb150015 "smb_server_share", &error)) == NULL)) { 29055331Samw mutex_exit(&zfs_share_lock); 29065331Samw return (ENOSYS); 29075331Samw } 29085331Samw error = zfs_init_sharefs(); 29095331Samw if (error) { 29105331Samw mutex_exit(&zfs_share_lock); 29115331Samw return (ENOSYS); 29125331Samw } 29135331Samw zfs_smbshare_inited = 1; 29144543Smarks mutex_exit(&zfs_share_lock); 29154543Smarks } 29165331Samw break; 29175331Samw default: 29185331Samw return (EINVAL); 29194543Smarks } 29204543Smarks 29215331Samw switch (zc->zc_share.z_sharetype) { 29225331Samw case ZFS_SHARE_NFS: 29235331Samw case ZFS_UNSHARE_NFS: 29245331Samw if (error = 29255331Samw znfsexport_fs((void *) 29265331Samw (uintptr_t)zc->zc_share.z_exportdata)) 29275331Samw return (error); 29285331Samw break; 29295331Samw case ZFS_SHARE_SMB: 29305331Samw case ZFS_UNSHARE_SMB: 29315331Samw if (error = zsmbexport_fs((void *) 29325331Samw (uintptr_t)zc->zc_share.z_exportdata, 29335331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 2934*8845Samw@Sun.COM B_TRUE: B_FALSE)) { 29355331Samw return (error); 29365331Samw } 29375331Samw break; 29385331Samw } 29395331Samw 29405331Samw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 29415331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 29424543Smarks SHAREFS_ADD : SHAREFS_REMOVE; 29434543Smarks 29445331Samw /* 29455331Samw * Add or remove share from sharetab 29465331Samw */ 29474543Smarks error = zshare_fs(opcode, 29484543Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 29494543Smarks zc->zc_share.z_sharemax); 29504543Smarks 29514543Smarks return (error); 29524543Smarks 29534543Smarks } 29544543Smarks 2955*8845Samw@Sun.COM ace_t full_access[] = { 2956*8845Samw@Sun.COM {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 2957*8845Samw@Sun.COM }; 2958*8845Samw@Sun.COM 2959*8845Samw@Sun.COM /* 2960*8845Samw@Sun.COM * Remove all ACL files in shares dir 2961*8845Samw@Sun.COM */ 2962*8845Samw@Sun.COM static int 2963*8845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp) 2964*8845Samw@Sun.COM { 2965*8845Samw@Sun.COM zap_cursor_t zc; 2966*8845Samw@Sun.COM zap_attribute_t zap; 2967*8845Samw@Sun.COM zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2968*8845Samw@Sun.COM int error; 2969*8845Samw@Sun.COM 2970*8845Samw@Sun.COM for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 2971*8845Samw@Sun.COM (error = zap_cursor_retrieve(&zc, &zap)) == 0; 2972*8845Samw@Sun.COM zap_cursor_advance(&zc)) { 2973*8845Samw@Sun.COM if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 2974*8845Samw@Sun.COM NULL, 0)) != 0) 2975*8845Samw@Sun.COM break; 2976*8845Samw@Sun.COM } 2977*8845Samw@Sun.COM zap_cursor_fini(&zc); 2978*8845Samw@Sun.COM return (error); 2979*8845Samw@Sun.COM } 2980*8845Samw@Sun.COM 2981*8845Samw@Sun.COM static int 2982*8845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc) 2983*8845Samw@Sun.COM { 2984*8845Samw@Sun.COM vnode_t *vp; 2985*8845Samw@Sun.COM znode_t *dzp; 2986*8845Samw@Sun.COM vnode_t *resourcevp = NULL; 2987*8845Samw@Sun.COM znode_t *sharedir; 2988*8845Samw@Sun.COM zfsvfs_t *zfsvfs; 2989*8845Samw@Sun.COM nvlist_t *nvlist; 2990*8845Samw@Sun.COM char *src, *target; 2991*8845Samw@Sun.COM vattr_t vattr; 2992*8845Samw@Sun.COM vsecattr_t vsec; 2993*8845Samw@Sun.COM int error = 0; 2994*8845Samw@Sun.COM 2995*8845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 2996*8845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 2997*8845Samw@Sun.COM return (error); 2998*8845Samw@Sun.COM 2999*8845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 3000*8845Samw@Sun.COM 3001*8845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 3002*8845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 3003*8845Samw@Sun.COM zc->zc_name) != 0)) { 3004*8845Samw@Sun.COM VN_RELE(vp); 3005*8845Samw@Sun.COM return (EINVAL); 3006*8845Samw@Sun.COM } 3007*8845Samw@Sun.COM 3008*8845Samw@Sun.COM dzp = VTOZ(vp); 3009*8845Samw@Sun.COM zfsvfs = dzp->z_zfsvfs; 3010*8845Samw@Sun.COM 3011*8845Samw@Sun.COM ZFS_ENTER(zfsvfs); 3012*8845Samw@Sun.COM 3013*8845Samw@Sun.COM if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 3014*8845Samw@Sun.COM ZFS_EXIT(zfsvfs); 3015*8845Samw@Sun.COM return (error); 3016*8845Samw@Sun.COM } 3017*8845Samw@Sun.COM 3018*8845Samw@Sun.COM switch (zc->zc_cookie) { 3019*8845Samw@Sun.COM case ZFS_SMB_ACL_ADD: 3020*8845Samw@Sun.COM vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 3021*8845Samw@Sun.COM vattr.va_type = VREG; 3022*8845Samw@Sun.COM vattr.va_mode = S_IFREG|0777; 3023*8845Samw@Sun.COM vattr.va_uid = 0; 3024*8845Samw@Sun.COM vattr.va_gid = 0; 3025*8845Samw@Sun.COM 3026*8845Samw@Sun.COM vsec.vsa_mask = VSA_ACE; 3027*8845Samw@Sun.COM vsec.vsa_aclentp = &full_access; 3028*8845Samw@Sun.COM vsec.vsa_aclentsz = sizeof (full_access); 3029*8845Samw@Sun.COM vsec.vsa_aclcnt = 1; 3030*8845Samw@Sun.COM 3031*8845Samw@Sun.COM error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 3032*8845Samw@Sun.COM &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 3033*8845Samw@Sun.COM if (resourcevp) 3034*8845Samw@Sun.COM VN_RELE(resourcevp); 3035*8845Samw@Sun.COM break; 3036*8845Samw@Sun.COM 3037*8845Samw@Sun.COM case ZFS_SMB_ACL_REMOVE: 3038*8845Samw@Sun.COM error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 3039*8845Samw@Sun.COM NULL, 0); 3040*8845Samw@Sun.COM break; 3041*8845Samw@Sun.COM 3042*8845Samw@Sun.COM case ZFS_SMB_ACL_RENAME: 3043*8845Samw@Sun.COM if ((error = get_nvlist(zc->zc_nvlist_src, 3044*8845Samw@Sun.COM zc->zc_nvlist_src_size, &nvlist)) != 0) { 3045*8845Samw@Sun.COM VN_RELE(vp); 3046*8845Samw@Sun.COM ZFS_EXIT(zfsvfs); 3047*8845Samw@Sun.COM return (error); 3048*8845Samw@Sun.COM } 3049*8845Samw@Sun.COM if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 3050*8845Samw@Sun.COM nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 3051*8845Samw@Sun.COM &target)) { 3052*8845Samw@Sun.COM VN_RELE(vp); 3053*8845Samw@Sun.COM ZFS_EXIT(zfsvfs); 3054*8845Samw@Sun.COM return (error); 3055*8845Samw@Sun.COM } 3056*8845Samw@Sun.COM error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 3057*8845Samw@Sun.COM kcred, NULL, 0); 3058*8845Samw@Sun.COM nvlist_free(nvlist); 3059*8845Samw@Sun.COM break; 3060*8845Samw@Sun.COM 3061*8845Samw@Sun.COM case ZFS_SMB_ACL_PURGE: 3062*8845Samw@Sun.COM error = zfs_smb_acl_purge(sharedir); 3063*8845Samw@Sun.COM break; 3064*8845Samw@Sun.COM 3065*8845Samw@Sun.COM default: 3066*8845Samw@Sun.COM error = EINVAL; 3067*8845Samw@Sun.COM break; 3068*8845Samw@Sun.COM } 3069*8845Samw@Sun.COM 3070*8845Samw@Sun.COM VN_RELE(vp); 3071*8845Samw@Sun.COM VN_RELE(ZTOV(sharedir)); 3072*8845Samw@Sun.COM 3073*8845Samw@Sun.COM ZFS_EXIT(zfsvfs); 3074*8845Samw@Sun.COM 3075*8845Samw@Sun.COM return (error); 3076*8845Samw@Sun.COM } 3077*8845Samw@Sun.COM 30784543Smarks /* 30794988Sek110237 * pool create, destroy, and export don't log the history as part of 30804988Sek110237 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 30814988Sek110237 * do the logging of those commands. 30824543Smarks */ 3083789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 30844715Sek110237 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 30854577Sahrens { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 30864577Sahrens { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 30874577Sahrens { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 30884577Sahrens { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE }, 30894577Sahrens { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 30904577Sahrens { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE }, 30914577Sahrens { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 30924577Sahrens { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE }, 30934577Sahrens { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 30944577Sahrens { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 30954577Sahrens { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 30964577Sahrens { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 30974577Sahrens { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 30984577Sahrens { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 30994577Sahrens { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 31004577Sahrens { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 31014577Sahrens { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 31025498Stimh { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 31034543Smarks { zfs_ioc_dataset_list_next, zfs_secpolicy_read, 31044577Sahrens DATASET_NAME, B_FALSE }, 31054543Smarks { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, 31064577Sahrens DATASET_NAME, B_FALSE }, 31074577Sahrens { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE }, 31084577Sahrens { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 31094577Sahrens { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 31104577Sahrens { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE }, 31114577Sahrens { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 31124577Sahrens { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE }, 31134577Sahrens { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE }, 31145367Sahrens { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE }, 31155367Sahrens { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE }, 31164577Sahrens { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 31174577Sahrens { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 31184577Sahrens { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 31194577Sahrens { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE }, 31204577Sahrens { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 31214577Sahrens { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE }, 31224577Sahrens { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 31234577Sahrens { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE }, 31244577Sahrens { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 31254577Sahrens { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE }, 31264577Sahrens { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 31274577Sahrens { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 31284577Sahrens { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE }, 31294577Sahrens { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 31304543Smarks { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, 31314577Sahrens DATASET_NAME, B_FALSE }, 31324849Sahrens { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE }, 31334849Sahrens { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE }, 3134*8845Samw@Sun.COM { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE } 3135789Sahrens }; 3136789Sahrens 3137789Sahrens static int 3138789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 3139789Sahrens { 3140789Sahrens zfs_cmd_t *zc; 3141789Sahrens uint_t vec; 31422199Sahrens int error, rc; 3143789Sahrens 3144789Sahrens if (getminor(dev) != 0) 3145789Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 3146789Sahrens 3147789Sahrens vec = cmd - ZFS_IOC; 31484787Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 3149789Sahrens 3150789Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 3151789Sahrens return (EINVAL); 3152789Sahrens 3153789Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 3154789Sahrens 3155789Sahrens error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t)); 3156789Sahrens 31574787Sahrens if (error == 0) 31584543Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 3159789Sahrens 3160789Sahrens /* 3161789Sahrens * Ensure that all pool/dataset names are valid before we pass down to 3162789Sahrens * the lower layers. 3163789Sahrens */ 3164789Sahrens if (error == 0) { 3165789Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 3166789Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 31674577Sahrens case POOL_NAME: 3168789Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 3169789Sahrens error = EINVAL; 3170789Sahrens break; 3171789Sahrens 31724577Sahrens case DATASET_NAME: 3173789Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 3174789Sahrens error = EINVAL; 3175789Sahrens break; 31762856Snd150628 31774577Sahrens case NO_NAME: 31782856Snd150628 break; 3179789Sahrens } 3180789Sahrens } 3181789Sahrens 3182789Sahrens if (error == 0) 3183789Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 3184789Sahrens 31852199Sahrens rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t)); 31864543Smarks if (error == 0) { 31872199Sahrens error = rc; 31884543Smarks if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE) 31894543Smarks zfs_log_history(zc); 31904543Smarks } 3191789Sahrens 3192789Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 3193789Sahrens return (error); 3194789Sahrens } 3195789Sahrens 3196789Sahrens static int 3197789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 3198789Sahrens { 3199789Sahrens if (cmd != DDI_ATTACH) 3200789Sahrens return (DDI_FAILURE); 3201789Sahrens 3202789Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 3203789Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 3204789Sahrens return (DDI_FAILURE); 3205789Sahrens 3206789Sahrens zfs_dip = dip; 3207789Sahrens 3208789Sahrens ddi_report_dev(dip); 3209789Sahrens 3210789Sahrens return (DDI_SUCCESS); 3211789Sahrens } 3212789Sahrens 3213789Sahrens static int 3214789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3215789Sahrens { 3216789Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 3217789Sahrens return (DDI_FAILURE); 3218789Sahrens 3219789Sahrens if (cmd != DDI_DETACH) 3220789Sahrens return (DDI_FAILURE); 3221789Sahrens 3222789Sahrens zfs_dip = NULL; 3223789Sahrens 3224789Sahrens ddi_prop_remove_all(dip); 3225789Sahrens ddi_remove_minor_node(dip, NULL); 3226789Sahrens 3227789Sahrens return (DDI_SUCCESS); 3228789Sahrens } 3229789Sahrens 3230789Sahrens /*ARGSUSED*/ 3231789Sahrens static int 3232789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 3233789Sahrens { 3234789Sahrens switch (infocmd) { 3235789Sahrens case DDI_INFO_DEVT2DEVINFO: 3236789Sahrens *result = zfs_dip; 3237789Sahrens return (DDI_SUCCESS); 3238789Sahrens 3239789Sahrens case DDI_INFO_DEVT2INSTANCE: 3240849Sbonwick *result = (void *)0; 3241789Sahrens return (DDI_SUCCESS); 3242789Sahrens } 3243789Sahrens 3244789Sahrens return (DDI_FAILURE); 3245789Sahrens } 3246789Sahrens 3247789Sahrens /* 3248789Sahrens * OK, so this is a little weird. 3249789Sahrens * 3250789Sahrens * /dev/zfs is the control node, i.e. minor 0. 3251789Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 3252789Sahrens * 3253789Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 3254789Sahrens * so most of the standard driver entry points are in zvol.c. 3255789Sahrens */ 3256789Sahrens static struct cb_ops zfs_cb_ops = { 3257789Sahrens zvol_open, /* open */ 3258789Sahrens zvol_close, /* close */ 3259789Sahrens zvol_strategy, /* strategy */ 3260789Sahrens nodev, /* print */ 32616423Sgw25295 zvol_dump, /* dump */ 3262789Sahrens zvol_read, /* read */ 3263789Sahrens zvol_write, /* write */ 3264789Sahrens zfsdev_ioctl, /* ioctl */ 3265789Sahrens nodev, /* devmap */ 3266789Sahrens nodev, /* mmap */ 3267789Sahrens nodev, /* segmap */ 3268789Sahrens nochpoll, /* poll */ 3269789Sahrens ddi_prop_op, /* prop_op */ 3270789Sahrens NULL, /* streamtab */ 3271789Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 3272789Sahrens CB_REV, /* version */ 32733638Sbillm nodev, /* async read */ 32743638Sbillm nodev, /* async write */ 3275789Sahrens }; 3276789Sahrens 3277789Sahrens static struct dev_ops zfs_dev_ops = { 3278789Sahrens DEVO_REV, /* version */ 3279789Sahrens 0, /* refcnt */ 3280789Sahrens zfs_info, /* info */ 3281789Sahrens nulldev, /* identify */ 3282789Sahrens nulldev, /* probe */ 3283789Sahrens zfs_attach, /* attach */ 3284789Sahrens zfs_detach, /* detach */ 3285789Sahrens nodev, /* reset */ 3286789Sahrens &zfs_cb_ops, /* driver operations */ 32877656SSherry.Moore@Sun.COM NULL, /* no bus operations */ 32887656SSherry.Moore@Sun.COM NULL, /* power */ 32897656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */ 3290789Sahrens }; 3291789Sahrens 3292789Sahrens static struct modldrv zfs_modldrv = { 32937656SSherry.Moore@Sun.COM &mod_driverops, 32947656SSherry.Moore@Sun.COM "ZFS storage pool", 32957656SSherry.Moore@Sun.COM &zfs_dev_ops 3296789Sahrens }; 3297789Sahrens 3298789Sahrens static struct modlinkage modlinkage = { 3299789Sahrens MODREV_1, 3300789Sahrens (void *)&zfs_modlfs, 3301789Sahrens (void *)&zfs_modldrv, 3302789Sahrens NULL 3303789Sahrens }; 3304789Sahrens 33054720Sfr157268 33064720Sfr157268 uint_t zfs_fsyncer_key; 33075326Sek110237 extern uint_t rrw_tsd_key; 33084720Sfr157268 3309789Sahrens int 3310789Sahrens _init(void) 3311789Sahrens { 3312789Sahrens int error; 3313789Sahrens 3314849Sbonwick spa_init(FREAD | FWRITE); 3315849Sbonwick zfs_init(); 3316849Sbonwick zvol_init(); 3317849Sbonwick 3318849Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 3319849Sbonwick zvol_fini(); 3320849Sbonwick zfs_fini(); 3321849Sbonwick spa_fini(); 3322789Sahrens return (error); 3323849Sbonwick } 3324789Sahrens 33254720Sfr157268 tsd_create(&zfs_fsyncer_key, NULL); 33265326Sek110237 tsd_create(&rrw_tsd_key, NULL); 33274720Sfr157268 3328789Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 3329789Sahrens ASSERT(error == 0); 33304543Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 3331789Sahrens 3332789Sahrens return (0); 3333789Sahrens } 3334789Sahrens 3335789Sahrens int 3336789Sahrens _fini(void) 3337789Sahrens { 3338789Sahrens int error; 3339789Sahrens 33401544Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 3341789Sahrens return (EBUSY); 3342789Sahrens 3343789Sahrens if ((error = mod_remove(&modlinkage)) != 0) 3344789Sahrens return (error); 3345789Sahrens 3346789Sahrens zvol_fini(); 3347789Sahrens zfs_fini(); 3348789Sahrens spa_fini(); 33495331Samw if (zfs_nfsshare_inited) 33504543Smarks (void) ddi_modclose(nfs_mod); 33515331Samw if (zfs_smbshare_inited) 33525331Samw (void) ddi_modclose(smbsrv_mod); 33535331Samw if (zfs_nfsshare_inited || zfs_smbshare_inited) 33544543Smarks (void) ddi_modclose(sharefs_mod); 3355789Sahrens 33564720Sfr157268 tsd_destroy(&zfs_fsyncer_key); 3357789Sahrens ldi_ident_release(zfs_li); 3358789Sahrens zfs_li = NULL; 33594543Smarks mutex_destroy(&zfs_share_lock); 3360789Sahrens 3361789Sahrens return (error); 3362789Sahrens } 3363789Sahrens 3364789Sahrens int 3365789Sahrens _info(struct modinfo *modinfop) 3366789Sahrens { 3367789Sahrens return (mod_info(&modlinkage, modinfop)); 3368789Sahrens } 3369