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 829234SGeorge.Wilson@Sun.COM typedef enum { 839234SGeorge.Wilson@Sun.COM NO_NAME, 849234SGeorge.Wilson@Sun.COM POOL_NAME, 859234SGeorge.Wilson@Sun.COM DATASET_NAME 869234SGeorge.Wilson@Sun.COM } zfs_ioc_namecheck_t; 879234SGeorge.Wilson@Sun.COM 88789Sahrens typedef struct zfs_ioc_vec { 89789Sahrens zfs_ioc_func_t *zvec_func; 90789Sahrens zfs_secpolicy_func_t *zvec_secpolicy; 919234SGeorge.Wilson@Sun.COM zfs_ioc_namecheck_t zvec_namecheck; 924543Smarks boolean_t zvec_his_log; 939234SGeorge.Wilson@Sun.COM boolean_t zvec_pool_check; 94789Sahrens } zfs_ioc_vec_t; 95789Sahrens 969396SMatthew.Ahrens@Sun.COM /* This array is indexed by zfs_userquota_prop_t */ 979396SMatthew.Ahrens@Sun.COM static const char *userquota_perms[] = { 989396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERUSED, 999396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERQUOTA, 1009396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_GROUPUSED, 1019396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_GROUPQUOTA, 1029396SMatthew.Ahrens@Sun.COM }; 1039396SMatthew.Ahrens@Sun.COM 1049396SMatthew.Ahrens@Sun.COM static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 1058536SDavid.Pacheco@Sun.COM static void clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops); 1067184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 1077184Stimh boolean_t *); 1087184Stimh int zfs_set_prop_nvlist(const char *, nvlist_t *); 1097184Stimh 110789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 111789Sahrens void 112789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 113789Sahrens { 114789Sahrens const char *newfile; 115789Sahrens char buf[256]; 116789Sahrens va_list adx; 117789Sahrens 118789Sahrens /* 119789Sahrens * Get rid of annoying "../common/" prefix to filename. 120789Sahrens */ 121789Sahrens newfile = strrchr(file, '/'); 122789Sahrens if (newfile != NULL) { 123789Sahrens newfile = newfile + 1; /* Get rid of leading / */ 124789Sahrens } else { 125789Sahrens newfile = file; 126789Sahrens } 127789Sahrens 128789Sahrens va_start(adx, fmt); 129789Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, adx); 130789Sahrens va_end(adx); 131789Sahrens 132789Sahrens /* 133789Sahrens * To get this data, use the zfs-dprintf probe as so: 134789Sahrens * dtrace -q -n 'zfs-dprintf \ 135789Sahrens * /stringof(arg0) == "dbuf.c"/ \ 136789Sahrens * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 137789Sahrens * arg0 = file name 138789Sahrens * arg1 = function name 139789Sahrens * arg2 = line number 140789Sahrens * arg3 = message 141789Sahrens */ 142789Sahrens DTRACE_PROBE4(zfs__dprintf, 143789Sahrens char *, newfile, char *, func, int, line, char *, buf); 144789Sahrens } 145789Sahrens 1464543Smarks static void 1474715Sek110237 history_str_free(char *buf) 1484715Sek110237 { 1494715Sek110237 kmem_free(buf, HIS_MAX_RECORD_LEN); 1504715Sek110237 } 1514715Sek110237 1524715Sek110237 static char * 1534715Sek110237 history_str_get(zfs_cmd_t *zc) 1544715Sek110237 { 1554715Sek110237 char *buf; 1564715Sek110237 1574715Sek110237 if (zc->zc_history == NULL) 1584715Sek110237 return (NULL); 1594715Sek110237 1604715Sek110237 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 1614715Sek110237 if (copyinstr((void *)(uintptr_t)zc->zc_history, 1624715Sek110237 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 1634715Sek110237 history_str_free(buf); 1644715Sek110237 return (NULL); 1654715Sek110237 } 1664715Sek110237 1674715Sek110237 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 1684715Sek110237 1694715Sek110237 return (buf); 1704715Sek110237 } 1714715Sek110237 1725375Stimh /* 1737042Sgw25295 * Check to see if the named dataset is currently defined as bootable 1747042Sgw25295 */ 1757042Sgw25295 static boolean_t 1767042Sgw25295 zfs_is_bootfs(const char *name) 1777042Sgw25295 { 17810298SMatthew.Ahrens@Sun.COM objset_t *os; 17910298SMatthew.Ahrens@Sun.COM 18010298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(name, FTAG, &os) == 0) { 18110298SMatthew.Ahrens@Sun.COM boolean_t ret; 18210298SMatthew.Ahrens@Sun.COM ret = (dmu_objset_id(os) == dmu_objset_spa(os)->spa_bootfs); 18310298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 18410298SMatthew.Ahrens@Sun.COM return (ret); 1857042Sgw25295 } 18610298SMatthew.Ahrens@Sun.COM return (B_FALSE); 1877042Sgw25295 } 1887042Sgw25295 1897042Sgw25295 /* 1907184Stimh * zfs_earlier_version 1915375Stimh * 1925375Stimh * Return non-zero if the spa version is less than requested version. 1935375Stimh */ 1945331Samw static int 1957184Stimh zfs_earlier_version(const char *name, int version) 1965331Samw { 1975331Samw spa_t *spa; 1985331Samw 1995331Samw if (spa_open(name, &spa, FTAG) == 0) { 2005331Samw if (spa_version(spa) < version) { 2015331Samw spa_close(spa, FTAG); 2025331Samw return (1); 2035331Samw } 2045331Samw spa_close(spa, FTAG); 2055331Samw } 2065331Samw return (0); 2075331Samw } 2085331Samw 2095977Smarks /* 2106689Smaybee * zpl_earlier_version 2115977Smarks * 2126689Smaybee * Return TRUE if the ZPL version is less than requested version. 2135977Smarks */ 2146689Smaybee static boolean_t 2156689Smaybee zpl_earlier_version(const char *name, int version) 2165977Smarks { 2175977Smarks objset_t *os; 2186689Smaybee boolean_t rc = B_TRUE; 2195977Smarks 22010298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(name, FTAG, &os) == 0) { 2216689Smaybee uint64_t zplversion; 2226689Smaybee 22310298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 22410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 22510298SMatthew.Ahrens@Sun.COM return (B_TRUE); 22610298SMatthew.Ahrens@Sun.COM } 22710298SMatthew.Ahrens@Sun.COM /* XXX reading from non-owned objset */ 2286689Smaybee if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 2296689Smaybee rc = zplversion < version; 23010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 2315977Smarks } 2325977Smarks return (rc); 2335977Smarks } 2345977Smarks 2354715Sek110237 static void 2364543Smarks zfs_log_history(zfs_cmd_t *zc) 2374543Smarks { 2384543Smarks spa_t *spa; 2394603Sahrens char *buf; 2404543Smarks 2414715Sek110237 if ((buf = history_str_get(zc)) == NULL) 2424577Sahrens return; 2434577Sahrens 2444715Sek110237 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 2454715Sek110237 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 2464715Sek110237 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 2474715Sek110237 spa_close(spa, FTAG); 2484543Smarks } 2494715Sek110237 history_str_free(buf); 2504543Smarks } 2514543Smarks 252789Sahrens /* 253789Sahrens * Policy for top-level read operations (list pools). Requires no privileges, 254789Sahrens * and can be used in the local zone, as there is no associated dataset. 255789Sahrens */ 256789Sahrens /* ARGSUSED */ 257789Sahrens static int 2584543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 259789Sahrens { 260789Sahrens return (0); 261789Sahrens } 262789Sahrens 263789Sahrens /* 264789Sahrens * Policy for dataset read operations (list children, get statistics). Requires 265789Sahrens * no privileges, but must be visible in the local zone. 266789Sahrens */ 267789Sahrens /* ARGSUSED */ 268789Sahrens static int 2694543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 270789Sahrens { 271789Sahrens if (INGLOBALZONE(curproc) || 2724543Smarks zone_dataset_visible(zc->zc_name, NULL)) 273789Sahrens return (0); 274789Sahrens 275789Sahrens return (ENOENT); 276789Sahrens } 277789Sahrens 278789Sahrens static int 279789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr) 280789Sahrens { 281789Sahrens uint64_t zoned; 282789Sahrens int writable = 1; 283789Sahrens 284789Sahrens /* 285789Sahrens * The dataset must be visible by this zone -- check this first 286789Sahrens * so they don't see EPERM on something they shouldn't know about. 287789Sahrens */ 288789Sahrens if (!INGLOBALZONE(curproc) && 289789Sahrens !zone_dataset_visible(dataset, &writable)) 290789Sahrens return (ENOENT); 291789Sahrens 292789Sahrens if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 293789Sahrens return (ENOENT); 294789Sahrens 295789Sahrens if (INGLOBALZONE(curproc)) { 296789Sahrens /* 297789Sahrens * If the fs is zoned, only root can access it from the 298789Sahrens * global zone. 299789Sahrens */ 300789Sahrens if (secpolicy_zfs(cr) && zoned) 301789Sahrens return (EPERM); 302789Sahrens } else { 303789Sahrens /* 304789Sahrens * If we are in a local zone, the 'zoned' property must be set. 305789Sahrens */ 306789Sahrens if (!zoned) 307789Sahrens return (EPERM); 308789Sahrens 309789Sahrens /* must be writable by this zone */ 310789Sahrens if (!writable) 311789Sahrens return (EPERM); 312789Sahrens } 313789Sahrens return (0); 314789Sahrens } 315789Sahrens 316789Sahrens int 3174543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 318789Sahrens { 319789Sahrens int error; 320789Sahrens 3214543Smarks error = zfs_dozonecheck(name, cr); 3224543Smarks if (error == 0) { 3234543Smarks error = secpolicy_zfs(cr); 3244670Sahrens if (error) 3254543Smarks error = dsl_deleg_access(name, perm, cr); 3264543Smarks } 3274543Smarks return (error); 3284543Smarks } 3294543Smarks 3304543Smarks static int 3314543Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr) 3324543Smarks { 3334543Smarks /* 3344543Smarks * Check permissions for special properties. 3354543Smarks */ 3364543Smarks switch (prop) { 3374543Smarks case ZFS_PROP_ZONED: 3384543Smarks /* 3394543Smarks * Disallow setting of 'zoned' from within a local zone. 3404543Smarks */ 3414543Smarks if (!INGLOBALZONE(curproc)) 3424543Smarks return (EPERM); 3434543Smarks break; 344789Sahrens 3454543Smarks case ZFS_PROP_QUOTA: 3464543Smarks if (!INGLOBALZONE(curproc)) { 3474543Smarks uint64_t zoned; 3484543Smarks char setpoint[MAXNAMELEN]; 3494543Smarks /* 3504543Smarks * Unprivileged users are allowed to modify the 3514543Smarks * quota on things *under* (ie. contained by) 3524543Smarks * the thing they own. 3534543Smarks */ 3544543Smarks if (dsl_prop_get_integer(name, "zoned", &zoned, 3554543Smarks setpoint)) 3564543Smarks return (EPERM); 3574670Sahrens if (!zoned || strlen(name) <= strlen(setpoint)) 3584543Smarks return (EPERM); 3594543Smarks } 3604670Sahrens break; 3614543Smarks } 3624543Smarks 3634787Sahrens return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr)); 364789Sahrens } 365789Sahrens 3664543Smarks int 3674543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 3684543Smarks { 3694543Smarks int error; 3704543Smarks 3714543Smarks error = zfs_dozonecheck(zc->zc_name, cr); 3724543Smarks if (error) 3734543Smarks return (error); 3744543Smarks 3754543Smarks /* 3764543Smarks * permission to set permissions will be evaluated later in 3774543Smarks * dsl_deleg_can_allow() 3784543Smarks */ 3794543Smarks return (0); 3804543Smarks } 3814543Smarks 3824543Smarks int 3834543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 3844543Smarks { 38510588SEric.Taylor@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 38610588SEric.Taylor@Sun.COM ZFS_DELEG_PERM_ROLLBACK, cr)); 3874543Smarks } 3884543Smarks 3894543Smarks int 3904543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 3914543Smarks { 3924543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 3934543Smarks ZFS_DELEG_PERM_SEND, cr)); 3944543Smarks } 3954543Smarks 3968845Samw@Sun.COM static int 3978845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr) 3988845Samw@Sun.COM { 3998845Samw@Sun.COM vnode_t *vp; 4008845Samw@Sun.COM int error; 4018845Samw@Sun.COM 4028845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 4038845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 4048845Samw@Sun.COM return (error); 4058845Samw@Sun.COM 4068845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 4078845Samw@Sun.COM 4088845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 4098845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 4108845Samw@Sun.COM zc->zc_name) != 0)) { 4118845Samw@Sun.COM VN_RELE(vp); 4128845Samw@Sun.COM return (EPERM); 4138845Samw@Sun.COM } 4148845Samw@Sun.COM 4158845Samw@Sun.COM VN_RELE(vp); 4168845Samw@Sun.COM return (dsl_deleg_access(zc->zc_name, 4178845Samw@Sun.COM ZFS_DELEG_PERM_SHARE, cr)); 4188845Samw@Sun.COM } 4198845Samw@Sun.COM 4204543Smarks int 4214543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 4224543Smarks { 4234543Smarks if (!INGLOBALZONE(curproc)) 4244543Smarks return (EPERM); 4254543Smarks 4265367Sahrens if (secpolicy_nfs(cr) == 0) { 4274543Smarks return (0); 4284543Smarks } else { 4298845Samw@Sun.COM return (zfs_secpolicy_deleg_share(zc, cr)); 4308845Samw@Sun.COM } 4318845Samw@Sun.COM } 4328845Samw@Sun.COM 4338845Samw@Sun.COM int 4348845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr) 4358845Samw@Sun.COM { 4368845Samw@Sun.COM if (!INGLOBALZONE(curproc)) 4378845Samw@Sun.COM return (EPERM); 4388845Samw@Sun.COM 4398845Samw@Sun.COM if (secpolicy_smb(cr) == 0) { 4408845Samw@Sun.COM return (0); 4418845Samw@Sun.COM } else { 4428845Samw@Sun.COM return (zfs_secpolicy_deleg_share(zc, cr)); 4434543Smarks } 4444543Smarks } 4454543Smarks 446789Sahrens static int 4474543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize) 448789Sahrens { 449789Sahrens char *cp; 450789Sahrens 451789Sahrens /* 452789Sahrens * Remove the @bla or /bla from the end of the name to get the parent. 453789Sahrens */ 4544543Smarks (void) strncpy(parent, datasetname, parentsize); 4554543Smarks cp = strrchr(parent, '@'); 456789Sahrens if (cp != NULL) { 457789Sahrens cp[0] = '\0'; 458789Sahrens } else { 4594543Smarks cp = strrchr(parent, '/'); 460789Sahrens if (cp == NULL) 461789Sahrens return (ENOENT); 462789Sahrens cp[0] = '\0'; 463789Sahrens } 464789Sahrens 4654543Smarks return (0); 4664543Smarks } 4674543Smarks 4684543Smarks int 4694543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 4704543Smarks { 4714543Smarks int error; 4724543Smarks 4734543Smarks if ((error = zfs_secpolicy_write_perms(name, 4744543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 4754543Smarks return (error); 4764543Smarks 4774543Smarks return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 4784543Smarks } 4794543Smarks 4804543Smarks static int 4814543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 4824543Smarks { 4834543Smarks return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 4844543Smarks } 4854543Smarks 4864543Smarks /* 4874543Smarks * Must have sys_config privilege to check the iscsi permission 4884543Smarks */ 4894543Smarks /* ARGSUSED */ 4904543Smarks static int 4914543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr) 4924543Smarks { 4934543Smarks return (secpolicy_zfs(cr)); 4944543Smarks } 4954543Smarks 4964543Smarks int 4974543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 4984543Smarks { 4994543Smarks char parentname[MAXNAMELEN]; 5004543Smarks int error; 5014543Smarks 5024543Smarks if ((error = zfs_secpolicy_write_perms(from, 5034543Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0) 5044543Smarks return (error); 5054543Smarks 5064543Smarks if ((error = zfs_secpolicy_write_perms(from, 5074543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 5084543Smarks return (error); 5094543Smarks 5104543Smarks if ((error = zfs_get_parent(to, parentname, 5114543Smarks sizeof (parentname))) != 0) 5124543Smarks return (error); 5134543Smarks 5144543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 5154543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 5164543Smarks return (error); 5174543Smarks 5184543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 5194543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 5204543Smarks return (error); 5214543Smarks 5224543Smarks return (error); 5234543Smarks } 5244543Smarks 5254543Smarks static int 5264543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 5274543Smarks { 5284543Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 5294543Smarks } 5304543Smarks 5314543Smarks static int 5324543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 5334543Smarks { 5344543Smarks char parentname[MAXNAMELEN]; 5354543Smarks objset_t *clone; 5364543Smarks int error; 5374543Smarks 5384543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 5394543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 5404543Smarks if (error) 5414543Smarks return (error); 5424543Smarks 54310298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &clone); 5444543Smarks 5454543Smarks if (error == 0) { 5464543Smarks dsl_dataset_t *pclone = NULL; 5474543Smarks dsl_dir_t *dd; 54810298SMatthew.Ahrens@Sun.COM dd = clone->os_dsl_dataset->ds_dir; 5494543Smarks 5504543Smarks rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 5516689Smaybee error = dsl_dataset_hold_obj(dd->dd_pool, 5526689Smaybee dd->dd_phys->dd_origin_obj, FTAG, &pclone); 5534543Smarks rw_exit(&dd->dd_pool->dp_config_rwlock); 5544543Smarks if (error) { 55510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 5564543Smarks return (error); 5574543Smarks } 5584543Smarks 5594543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 5604543Smarks ZFS_DELEG_PERM_MOUNT, cr); 5614543Smarks 5624543Smarks dsl_dataset_name(pclone, parentname); 56310298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 5646689Smaybee dsl_dataset_rele(pclone, FTAG); 5654543Smarks if (error == 0) 5664543Smarks error = zfs_secpolicy_write_perms(parentname, 5674543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 5684543Smarks } 5694543Smarks return (error); 5704543Smarks } 5714543Smarks 5724543Smarks static int 5734543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 5744543Smarks { 5754543Smarks int error; 5764543Smarks 5774543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 5784543Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 5794543Smarks return (error); 5804543Smarks 5814543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 5824543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 5834543Smarks return (error); 5844543Smarks 5854543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 5864543Smarks ZFS_DELEG_PERM_CREATE, cr)); 5874543Smarks } 5884543Smarks 5894543Smarks int 5904543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 5914543Smarks { 59210588SEric.Taylor@Sun.COM return (zfs_secpolicy_write_perms(name, 59310588SEric.Taylor@Sun.COM ZFS_DELEG_PERM_SNAPSHOT, cr)); 5944543Smarks } 5954543Smarks 5964543Smarks static int 5974543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 5984543Smarks { 5994543Smarks 6004543Smarks return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 6014543Smarks } 6024543Smarks 6034543Smarks static int 6044543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 6054543Smarks { 6064543Smarks char parentname[MAXNAMELEN]; 6074543Smarks int error; 6084543Smarks 6094543Smarks if ((error = zfs_get_parent(zc->zc_name, parentname, 6104543Smarks sizeof (parentname))) != 0) 6114543Smarks return (error); 6124543Smarks 6134543Smarks if (zc->zc_value[0] != '\0') { 6144543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_value, 6154543Smarks ZFS_DELEG_PERM_CLONE, cr)) != 0) 6164543Smarks return (error); 6174543Smarks } 6184543Smarks 6194543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 6204543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 6214543Smarks return (error); 6224543Smarks 6234543Smarks error = zfs_secpolicy_write_perms(parentname, 6244543Smarks ZFS_DELEG_PERM_MOUNT, cr); 6254543Smarks 6264543Smarks return (error); 6274543Smarks } 6284543Smarks 6294543Smarks static int 6304543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 6314543Smarks { 6324543Smarks int error; 6334543Smarks 6344543Smarks error = secpolicy_fs_unmount(cr, NULL); 6354543Smarks if (error) { 6364543Smarks error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 6374543Smarks } 6384543Smarks return (error); 639789Sahrens } 640789Sahrens 641789Sahrens /* 642789Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 643789Sahrens * SYS_CONFIG privilege, which is not available in a local zone. 644789Sahrens */ 645789Sahrens /* ARGSUSED */ 646789Sahrens static int 6474543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 648789Sahrens { 649789Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0) 650789Sahrens return (EPERM); 651789Sahrens 652789Sahrens return (0); 653789Sahrens } 654789Sahrens 655789Sahrens /* 6561544Seschrock * Policy for fault injection. Requires all privileges. 6571544Seschrock */ 6581544Seschrock /* ARGSUSED */ 6591544Seschrock static int 6604543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 6611544Seschrock { 6621544Seschrock return (secpolicy_zinject(cr)); 6631544Seschrock } 6641544Seschrock 6654849Sahrens static int 6664849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 6674849Sahrens { 6684849Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 6694849Sahrens 6705094Slling if (prop == ZPROP_INVAL) { 6714849Sahrens if (!zfs_prop_user(zc->zc_value)) 6724849Sahrens return (EINVAL); 6734849Sahrens return (zfs_secpolicy_write_perms(zc->zc_name, 6744849Sahrens ZFS_DELEG_PERM_USERPROP, cr)); 6754849Sahrens } else { 6764849Sahrens if (!zfs_prop_inheritable(prop)) 6774849Sahrens return (EINVAL); 6784849Sahrens return (zfs_secpolicy_setprop(zc->zc_name, prop, cr)); 6794849Sahrens } 6804849Sahrens } 6814849Sahrens 6829396SMatthew.Ahrens@Sun.COM static int 6839396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr) 6849396SMatthew.Ahrens@Sun.COM { 6859396SMatthew.Ahrens@Sun.COM int err = zfs_secpolicy_read(zc, cr); 6869396SMatthew.Ahrens@Sun.COM if (err) 6879396SMatthew.Ahrens@Sun.COM return (err); 6889396SMatthew.Ahrens@Sun.COM 6899396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 6909396SMatthew.Ahrens@Sun.COM return (EINVAL); 6919396SMatthew.Ahrens@Sun.COM 6929396SMatthew.Ahrens@Sun.COM if (zc->zc_value[0] == 0) { 6939396SMatthew.Ahrens@Sun.COM /* 6949396SMatthew.Ahrens@Sun.COM * They are asking about a posix uid/gid. If it's 6959396SMatthew.Ahrens@Sun.COM * themself, allow it. 6969396SMatthew.Ahrens@Sun.COM */ 6979396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type == ZFS_PROP_USERUSED || 6989396SMatthew.Ahrens@Sun.COM zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 6999396SMatthew.Ahrens@Sun.COM if (zc->zc_guid == crgetuid(cr)) 7009396SMatthew.Ahrens@Sun.COM return (0); 7019396SMatthew.Ahrens@Sun.COM } else { 7029396SMatthew.Ahrens@Sun.COM if (groupmember(zc->zc_guid, cr)) 7039396SMatthew.Ahrens@Sun.COM return (0); 7049396SMatthew.Ahrens@Sun.COM } 7059396SMatthew.Ahrens@Sun.COM } 7069396SMatthew.Ahrens@Sun.COM 7079396SMatthew.Ahrens@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 7089396SMatthew.Ahrens@Sun.COM userquota_perms[zc->zc_objset_type], cr)); 7099396SMatthew.Ahrens@Sun.COM } 7109396SMatthew.Ahrens@Sun.COM 7119396SMatthew.Ahrens@Sun.COM static int 7129396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr) 7139396SMatthew.Ahrens@Sun.COM { 7149396SMatthew.Ahrens@Sun.COM int err = zfs_secpolicy_read(zc, cr); 7159396SMatthew.Ahrens@Sun.COM if (err) 7169396SMatthew.Ahrens@Sun.COM return (err); 7179396SMatthew.Ahrens@Sun.COM 7189396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 7199396SMatthew.Ahrens@Sun.COM return (EINVAL); 7209396SMatthew.Ahrens@Sun.COM 7219396SMatthew.Ahrens@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 7229396SMatthew.Ahrens@Sun.COM userquota_perms[zc->zc_objset_type], cr)); 7239396SMatthew.Ahrens@Sun.COM } 7249396SMatthew.Ahrens@Sun.COM 7259396SMatthew.Ahrens@Sun.COM static int 7269396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr) 7279396SMatthew.Ahrens@Sun.COM { 7289396SMatthew.Ahrens@Sun.COM return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, cr)); 7299396SMatthew.Ahrens@Sun.COM } 7309396SMatthew.Ahrens@Sun.COM 73110242Schris.kirby@sun.com static int 73210242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr) 73310242Schris.kirby@sun.com { 73410242Schris.kirby@sun.com return (zfs_secpolicy_write_perms(zc->zc_name, 73510242Schris.kirby@sun.com ZFS_DELEG_PERM_HOLD, cr)); 73610242Schris.kirby@sun.com } 73710242Schris.kirby@sun.com 73810242Schris.kirby@sun.com static int 73910242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr) 74010242Schris.kirby@sun.com { 74110242Schris.kirby@sun.com return (zfs_secpolicy_write_perms(zc->zc_name, 74210242Schris.kirby@sun.com ZFS_DELEG_PERM_RELEASE, cr)); 74310242Schris.kirby@sun.com } 74410242Schris.kirby@sun.com 7451544Seschrock /* 746789Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t. 747789Sahrens */ 748789Sahrens static int 7499643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 750789Sahrens { 751789Sahrens char *packed; 752789Sahrens int error; 7535094Slling nvlist_t *list = NULL; 754789Sahrens 755789Sahrens /* 7562676Seschrock * Read in and unpack the user-supplied nvlist. 757789Sahrens */ 7585094Slling if (size == 0) 759789Sahrens return (EINVAL); 760789Sahrens 761789Sahrens packed = kmem_alloc(size, KM_SLEEP); 762789Sahrens 7639643SEric.Taylor@Sun.COM if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 7649643SEric.Taylor@Sun.COM iflag)) != 0) { 765789Sahrens kmem_free(packed, size); 766789Sahrens return (error); 767789Sahrens } 768789Sahrens 7695094Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 770789Sahrens kmem_free(packed, size); 771789Sahrens return (error); 772789Sahrens } 773789Sahrens 774789Sahrens kmem_free(packed, size); 775789Sahrens 7765094Slling *nvp = list; 777789Sahrens return (0); 778789Sahrens } 779789Sahrens 780789Sahrens static int 7812676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 7822676Seschrock { 7832676Seschrock char *packed = NULL; 7842676Seschrock size_t size; 7852676Seschrock int error; 7862676Seschrock 7872676Seschrock VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 7882676Seschrock 7892676Seschrock if (size > zc->zc_nvlist_dst_size) { 7902676Seschrock error = ENOMEM; 7912676Seschrock } else { 7924611Smarks packed = kmem_alloc(size, KM_SLEEP); 7932676Seschrock VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 7942676Seschrock KM_SLEEP) == 0); 7959643SEric.Taylor@Sun.COM error = ddi_copyout(packed, 7969643SEric.Taylor@Sun.COM (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags); 7972676Seschrock kmem_free(packed, size); 7982676Seschrock } 7992676Seschrock 8002676Seschrock zc->zc_nvlist_dst_size = size; 8012676Seschrock return (error); 8022676Seschrock } 8032676Seschrock 8042676Seschrock static int 8059396SMatthew.Ahrens@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zvp) 8069396SMatthew.Ahrens@Sun.COM { 8079396SMatthew.Ahrens@Sun.COM objset_t *os; 8089396SMatthew.Ahrens@Sun.COM int error; 8099396SMatthew.Ahrens@Sun.COM 81010298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(dsname, FTAG, &os); 8119396SMatthew.Ahrens@Sun.COM if (error) 8129396SMatthew.Ahrens@Sun.COM return (error); 81310298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 81410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 81510298SMatthew.Ahrens@Sun.COM return (EINVAL); 81610298SMatthew.Ahrens@Sun.COM } 81710298SMatthew.Ahrens@Sun.COM 81810298SMatthew.Ahrens@Sun.COM mutex_enter(&os->os_user_ptr_lock); 8199396SMatthew.Ahrens@Sun.COM *zvp = dmu_objset_get_user(os); 8209396SMatthew.Ahrens@Sun.COM if (*zvp) { 8219396SMatthew.Ahrens@Sun.COM VFS_HOLD((*zvp)->z_vfs); 8229396SMatthew.Ahrens@Sun.COM } else { 8239396SMatthew.Ahrens@Sun.COM error = ESRCH; 8249396SMatthew.Ahrens@Sun.COM } 82510298SMatthew.Ahrens@Sun.COM mutex_exit(&os->os_user_ptr_lock); 82610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 8279396SMatthew.Ahrens@Sun.COM return (error); 8289396SMatthew.Ahrens@Sun.COM } 8299396SMatthew.Ahrens@Sun.COM 8309396SMatthew.Ahrens@Sun.COM /* 8319396SMatthew.Ahrens@Sun.COM * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 8329396SMatthew.Ahrens@Sun.COM * case its z_vfs will be NULL, and it will be opened as the owner. 8339396SMatthew.Ahrens@Sun.COM */ 8349396SMatthew.Ahrens@Sun.COM static int 83510298SMatthew.Ahrens@Sun.COM zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zvp) 8369396SMatthew.Ahrens@Sun.COM { 8379396SMatthew.Ahrens@Sun.COM int error = 0; 8389396SMatthew.Ahrens@Sun.COM 8399396SMatthew.Ahrens@Sun.COM if (getzfsvfs(name, zvp) != 0) 84010298SMatthew.Ahrens@Sun.COM error = zfsvfs_create(name, zvp); 8419396SMatthew.Ahrens@Sun.COM if (error == 0) { 8429396SMatthew.Ahrens@Sun.COM rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag); 8439396SMatthew.Ahrens@Sun.COM if ((*zvp)->z_unmounted) { 8449396SMatthew.Ahrens@Sun.COM /* 8459396SMatthew.Ahrens@Sun.COM * XXX we could probably try again, since the unmounting 8469396SMatthew.Ahrens@Sun.COM * thread should be just about to disassociate the 8479396SMatthew.Ahrens@Sun.COM * objset from the zfsvfs. 8489396SMatthew.Ahrens@Sun.COM */ 8499396SMatthew.Ahrens@Sun.COM rrw_exit(&(*zvp)->z_teardown_lock, tag); 8509396SMatthew.Ahrens@Sun.COM return (EBUSY); 8519396SMatthew.Ahrens@Sun.COM } 8529396SMatthew.Ahrens@Sun.COM } 8539396SMatthew.Ahrens@Sun.COM return (error); 8549396SMatthew.Ahrens@Sun.COM } 8559396SMatthew.Ahrens@Sun.COM 8569396SMatthew.Ahrens@Sun.COM static void 8579396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 8589396SMatthew.Ahrens@Sun.COM { 8599396SMatthew.Ahrens@Sun.COM rrw_exit(&zfsvfs->z_teardown_lock, tag); 8609396SMatthew.Ahrens@Sun.COM 8619396SMatthew.Ahrens@Sun.COM if (zfsvfs->z_vfs) { 8629396SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 8639396SMatthew.Ahrens@Sun.COM } else { 86410298SMatthew.Ahrens@Sun.COM dmu_objset_disown(zfsvfs->z_os, zfsvfs); 8659396SMatthew.Ahrens@Sun.COM zfsvfs_free(zfsvfs); 8669396SMatthew.Ahrens@Sun.COM } 8679396SMatthew.Ahrens@Sun.COM } 8689396SMatthew.Ahrens@Sun.COM 8699396SMatthew.Ahrens@Sun.COM static int 870789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc) 871789Sahrens { 872789Sahrens int error; 8735094Slling nvlist_t *config, *props = NULL; 8747184Stimh nvlist_t *rootprops = NULL; 8757184Stimh nvlist_t *zplprops = NULL; 8764715Sek110237 char *buf; 877789Sahrens 8785094Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 8799643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) 8804988Sek110237 return (error); 8814715Sek110237 8825094Slling if (zc->zc_nvlist_src_size != 0 && (error = 8839643SEric.Taylor@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 8849643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) { 8855094Slling nvlist_free(config); 8865094Slling return (error); 8875094Slling } 8885094Slling 8897184Stimh if (props) { 8907184Stimh nvlist_t *nvl = NULL; 8917184Stimh uint64_t version = SPA_VERSION; 8927184Stimh 8937184Stimh (void) nvlist_lookup_uint64(props, 8947184Stimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 8957184Stimh if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 8967184Stimh error = EINVAL; 8977184Stimh goto pool_props_bad; 8987184Stimh } 8997184Stimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 9007184Stimh if (nvl) { 9017184Stimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 9027184Stimh if (error != 0) { 9037184Stimh nvlist_free(config); 9047184Stimh nvlist_free(props); 9057184Stimh return (error); 9067184Stimh } 9077184Stimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 9087184Stimh } 9097184Stimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 9107184Stimh error = zfs_fill_zplprops_root(version, rootprops, 9117184Stimh zplprops, NULL); 9127184Stimh if (error) 9137184Stimh goto pool_props_bad; 9147184Stimh } 9157184Stimh 9164988Sek110237 buf = history_str_get(zc); 917789Sahrens 9187184Stimh error = spa_create(zc->zc_name, config, props, buf, zplprops); 9197184Stimh 9207184Stimh /* 9217184Stimh * Set the remaining root properties 9227184Stimh */ 9237184Stimh if (!error && 9247184Stimh (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0) 9257184Stimh (void) spa_destroy(zc->zc_name); 926789Sahrens 9274988Sek110237 if (buf != NULL) 9284988Sek110237 history_str_free(buf); 9295094Slling 9307184Stimh pool_props_bad: 9317184Stimh nvlist_free(rootprops); 9327184Stimh nvlist_free(zplprops); 933789Sahrens nvlist_free(config); 9347184Stimh nvlist_free(props); 9355094Slling 936789Sahrens return (error); 937789Sahrens } 938789Sahrens 939789Sahrens static int 940789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc) 941789Sahrens { 9424543Smarks int error; 9434543Smarks zfs_log_history(zc); 9444543Smarks error = spa_destroy(zc->zc_name); 94510588SEric.Taylor@Sun.COM if (error == 0) 94610588SEric.Taylor@Sun.COM zvol_remove_minors(zc->zc_name); 9474543Smarks return (error); 948789Sahrens } 949789Sahrens 950789Sahrens static int 951789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc) 952789Sahrens { 953789Sahrens int error; 9545094Slling nvlist_t *config, *props = NULL; 955789Sahrens uint64_t guid; 956789Sahrens 9575094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 9589643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) != 0) 959789Sahrens return (error); 960789Sahrens 9615094Slling if (zc->zc_nvlist_src_size != 0 && (error = 9629643SEric.Taylor@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 9639643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) { 9645094Slling nvlist_free(config); 9655094Slling return (error); 9665094Slling } 9675094Slling 968789Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 9691544Seschrock guid != zc->zc_guid) 970789Sahrens error = EINVAL; 9716643Seschrock else if (zc->zc_cookie) 9729425SEric.Schrock@Sun.COM error = spa_import_verbatim(zc->zc_name, config, 9736643Seschrock props); 974789Sahrens else 9755094Slling error = spa_import(zc->zc_name, config, props); 976789Sahrens 977789Sahrens nvlist_free(config); 978789Sahrens 9795094Slling if (props) 9805094Slling nvlist_free(props); 9815094Slling 982789Sahrens return (error); 983789Sahrens } 984789Sahrens 985789Sahrens static int 986789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc) 987789Sahrens { 9884543Smarks int error; 9897214Slling boolean_t force = (boolean_t)zc->zc_cookie; 9908211SGeorge.Wilson@Sun.COM boolean_t hardforce = (boolean_t)zc->zc_guid; 9917214Slling 9924543Smarks zfs_log_history(zc); 9938211SGeorge.Wilson@Sun.COM error = spa_export(zc->zc_name, NULL, force, hardforce); 99410588SEric.Taylor@Sun.COM if (error == 0) 99510588SEric.Taylor@Sun.COM zvol_remove_minors(zc->zc_name); 9964543Smarks return (error); 997789Sahrens } 998789Sahrens 999789Sahrens static int 1000789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 1001789Sahrens { 1002789Sahrens nvlist_t *configs; 1003789Sahrens int error; 1004789Sahrens 1005789Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1006789Sahrens return (EEXIST); 1007789Sahrens 10082676Seschrock error = put_nvlist(zc, configs); 1009789Sahrens 1010789Sahrens nvlist_free(configs); 1011789Sahrens 1012789Sahrens return (error); 1013789Sahrens } 1014789Sahrens 1015789Sahrens static int 1016789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 1017789Sahrens { 1018789Sahrens nvlist_t *config; 1019789Sahrens int error; 10201544Seschrock int ret = 0; 1021789Sahrens 10222676Seschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 10232676Seschrock sizeof (zc->zc_value)); 1024789Sahrens 1025789Sahrens if (config != NULL) { 10262676Seschrock ret = put_nvlist(zc, config); 1027789Sahrens nvlist_free(config); 10281544Seschrock 10291544Seschrock /* 10301544Seschrock * The config may be present even if 'error' is non-zero. 10311544Seschrock * In this case we return success, and preserve the real errno 10321544Seschrock * in 'zc_cookie'. 10331544Seschrock */ 10341544Seschrock zc->zc_cookie = error; 1035789Sahrens } else { 10361544Seschrock ret = error; 1037789Sahrens } 1038789Sahrens 10391544Seschrock return (ret); 1040789Sahrens } 1041789Sahrens 1042789Sahrens /* 1043789Sahrens * Try to import the given pool, returning pool stats as appropriate so that 1044789Sahrens * user land knows which devices are available and overall pool health. 1045789Sahrens */ 1046789Sahrens static int 1047789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1048789Sahrens { 1049789Sahrens nvlist_t *tryconfig, *config; 1050789Sahrens int error; 1051789Sahrens 10525094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 10539643SEric.Taylor@Sun.COM zc->zc_iflags, &tryconfig)) != 0) 1054789Sahrens return (error); 1055789Sahrens 1056789Sahrens config = spa_tryimport(tryconfig); 1057789Sahrens 1058789Sahrens nvlist_free(tryconfig); 1059789Sahrens 1060789Sahrens if (config == NULL) 1061789Sahrens return (EINVAL); 1062789Sahrens 10632676Seschrock error = put_nvlist(zc, config); 1064789Sahrens nvlist_free(config); 1065789Sahrens 1066789Sahrens return (error); 1067789Sahrens } 1068789Sahrens 1069789Sahrens static int 1070789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc) 1071789Sahrens { 1072789Sahrens spa_t *spa; 1073789Sahrens int error; 1074789Sahrens 10752926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 10762926Sek110237 return (error); 10772926Sek110237 10787046Sahrens error = spa_scrub(spa, zc->zc_cookie); 10792926Sek110237 10802926Sek110237 spa_close(spa, FTAG); 10812926Sek110237 1082789Sahrens return (error); 1083789Sahrens } 1084789Sahrens 1085789Sahrens static int 1086789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1087789Sahrens { 1088789Sahrens spa_t *spa; 1089789Sahrens int error; 1090789Sahrens 1091789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1092789Sahrens if (error == 0) { 1093789Sahrens spa_freeze(spa); 1094789Sahrens spa_close(spa, FTAG); 1095789Sahrens } 1096789Sahrens return (error); 1097789Sahrens } 1098789Sahrens 1099789Sahrens static int 11001760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 11011760Seschrock { 11021760Seschrock spa_t *spa; 11031760Seschrock int error; 11041760Seschrock 11052926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 11062926Sek110237 return (error); 11072926Sek110237 11085118Slling if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 11095118Slling spa_close(spa, FTAG); 11105118Slling return (EINVAL); 11115118Slling } 11125118Slling 11135094Slling spa_upgrade(spa, zc->zc_cookie); 11142926Sek110237 spa_close(spa, FTAG); 11152926Sek110237 11162926Sek110237 return (error); 11172926Sek110237 } 11182926Sek110237 11192926Sek110237 static int 11202926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 11212926Sek110237 { 11222926Sek110237 spa_t *spa; 11232926Sek110237 char *hist_buf; 11242926Sek110237 uint64_t size; 11252926Sek110237 int error; 11262926Sek110237 11272926Sek110237 if ((size = zc->zc_history_len) == 0) 11282926Sek110237 return (EINVAL); 11292926Sek110237 11302926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 11312926Sek110237 return (error); 11322926Sek110237 11334577Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 11343863Sek110237 spa_close(spa, FTAG); 11353863Sek110237 return (ENOTSUP); 11363863Sek110237 } 11373863Sek110237 11382926Sek110237 hist_buf = kmem_alloc(size, KM_SLEEP); 11392926Sek110237 if ((error = spa_history_get(spa, &zc->zc_history_offset, 11402926Sek110237 &zc->zc_history_len, hist_buf)) == 0) { 11419643SEric.Taylor@Sun.COM error = ddi_copyout(hist_buf, 11429643SEric.Taylor@Sun.COM (void *)(uintptr_t)zc->zc_history, 11439643SEric.Taylor@Sun.COM zc->zc_history_len, zc->zc_iflags); 11442926Sek110237 } 11452926Sek110237 11462926Sek110237 spa_close(spa, FTAG); 11472926Sek110237 kmem_free(hist_buf, size); 11482926Sek110237 return (error); 11492926Sek110237 } 11502926Sek110237 11512926Sek110237 static int 11523444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 11533444Sek110237 { 11543444Sek110237 int error; 11553444Sek110237 11563912Slling if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 11573444Sek110237 return (error); 11583444Sek110237 11593444Sek110237 return (0); 11603444Sek110237 } 11613444Sek110237 116210298SMatthew.Ahrens@Sun.COM /* 116310298SMatthew.Ahrens@Sun.COM * inputs: 116410298SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 116510298SMatthew.Ahrens@Sun.COM * zc_obj object to find 116610298SMatthew.Ahrens@Sun.COM * 116710298SMatthew.Ahrens@Sun.COM * outputs: 116810298SMatthew.Ahrens@Sun.COM * zc_value name of object 116910298SMatthew.Ahrens@Sun.COM */ 11703444Sek110237 static int 11713444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 11723444Sek110237 { 117310298SMatthew.Ahrens@Sun.COM objset_t *os; 11743444Sek110237 int error; 11753444Sek110237 117610298SMatthew.Ahrens@Sun.COM /* XXX reading from objset not owned */ 117710298SMatthew.Ahrens@Sun.COM if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 11783444Sek110237 return (error); 117910298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 118010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 118110298SMatthew.Ahrens@Sun.COM return (EINVAL); 118210298SMatthew.Ahrens@Sun.COM } 118310298SMatthew.Ahrens@Sun.COM error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 11843444Sek110237 sizeof (zc->zc_value)); 118510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 11863444Sek110237 11873444Sek110237 return (error); 11883444Sek110237 } 11893444Sek110237 11903444Sek110237 static int 1191789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1192789Sahrens { 1193789Sahrens spa_t *spa; 1194789Sahrens int error; 11956423Sgw25295 nvlist_t *config, **l2cache, **spares; 11966423Sgw25295 uint_t nl2cache = 0, nspares = 0; 1197789Sahrens 1198789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1199789Sahrens if (error != 0) 1200789Sahrens return (error); 1201789Sahrens 12025450Sbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 12039643SEric.Taylor@Sun.COM zc->zc_iflags, &config); 12045450Sbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 12055450Sbrendan &l2cache, &nl2cache); 12065450Sbrendan 12076423Sgw25295 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 12086423Sgw25295 &spares, &nspares); 12096423Sgw25295 12103912Slling /* 12113912Slling * A root pool with concatenated devices is not supported. 12126423Sgw25295 * Thus, can not add a device to a root pool. 12136423Sgw25295 * 12146423Sgw25295 * Intent log device can not be added to a rootpool because 12156423Sgw25295 * during mountroot, zil is replayed, a seperated log device 12166423Sgw25295 * can not be accessed during the mountroot time. 12176423Sgw25295 * 12186423Sgw25295 * l2cache and spare devices are ok to be added to a rootpool. 12193912Slling */ 12206423Sgw25295 if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) { 12213912Slling spa_close(spa, FTAG); 12223912Slling return (EDOM); 12233912Slling } 12243912Slling 12255450Sbrendan if (error == 0) { 1226789Sahrens error = spa_vdev_add(spa, config); 1227789Sahrens nvlist_free(config); 1228789Sahrens } 1229789Sahrens spa_close(spa, FTAG); 1230789Sahrens return (error); 1231789Sahrens } 1232789Sahrens 1233789Sahrens static int 1234789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1235789Sahrens { 12362082Seschrock spa_t *spa; 12372082Seschrock int error; 12382082Seschrock 12392082Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 12402082Seschrock if (error != 0) 12412082Seschrock return (error); 12422082Seschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 12432082Seschrock spa_close(spa, FTAG); 12442082Seschrock return (error); 1245789Sahrens } 1246789Sahrens 1247789Sahrens static int 12484451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1249789Sahrens { 125010588SEric.Taylor@Sun.COM boolean_t nslock; 1251789Sahrens spa_t *spa; 1252789Sahrens int error; 12534451Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1254789Sahrens 12552926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1256789Sahrens return (error); 125710588SEric.Taylor@Sun.COM nslock = spa_uses_zvols(spa); 125810588SEric.Taylor@Sun.COM if (nslock) 125910588SEric.Taylor@Sun.COM mutex_enter(&spa_namespace_lock); 12604451Seschrock switch (zc->zc_cookie) { 12614451Seschrock case VDEV_STATE_ONLINE: 12624451Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 12634451Seschrock break; 12644451Seschrock 12654451Seschrock case VDEV_STATE_OFFLINE: 12664451Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 12674451Seschrock break; 1268789Sahrens 12694451Seschrock case VDEV_STATE_FAULTED: 1270*10817SEric.Schrock@Sun.COM if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1271*10817SEric.Schrock@Sun.COM zc->zc_obj != VDEV_AUX_EXTERNAL) 1272*10817SEric.Schrock@Sun.COM zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1273*10817SEric.Schrock@Sun.COM 1274*10817SEric.Schrock@Sun.COM error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 12754451Seschrock break; 1276789Sahrens 12774451Seschrock case VDEV_STATE_DEGRADED: 1278*10817SEric.Schrock@Sun.COM if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1279*10817SEric.Schrock@Sun.COM zc->zc_obj != VDEV_AUX_EXTERNAL) 1280*10817SEric.Schrock@Sun.COM zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1281*10817SEric.Schrock@Sun.COM 1282*10817SEric.Schrock@Sun.COM error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 12834451Seschrock break; 12844451Seschrock 12854451Seschrock default: 12864451Seschrock error = EINVAL; 12874451Seschrock } 128810588SEric.Taylor@Sun.COM if (nslock) 128910588SEric.Taylor@Sun.COM mutex_exit(&spa_namespace_lock); 12904451Seschrock zc->zc_cookie = newstate; 1291789Sahrens spa_close(spa, FTAG); 1292789Sahrens return (error); 1293789Sahrens } 1294789Sahrens 1295789Sahrens static int 1296789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1297789Sahrens { 1298789Sahrens spa_t *spa; 1299789Sahrens int replacing = zc->zc_cookie; 1300789Sahrens nvlist_t *config; 1301789Sahrens int error; 1302789Sahrens 13032926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1304789Sahrens return (error); 1305789Sahrens 13065094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 13079643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) == 0) { 13081544Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1309789Sahrens nvlist_free(config); 1310789Sahrens } 1311789Sahrens 1312789Sahrens spa_close(spa, FTAG); 1313789Sahrens return (error); 1314789Sahrens } 1315789Sahrens 1316789Sahrens static int 1317789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1318789Sahrens { 1319789Sahrens spa_t *spa; 1320789Sahrens int error; 1321789Sahrens 13222926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1323789Sahrens return (error); 1324789Sahrens 13258241SJeff.Bonwick@Sun.COM error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1326789Sahrens 1327789Sahrens spa_close(spa, FTAG); 1328789Sahrens return (error); 1329789Sahrens } 1330789Sahrens 1331789Sahrens static int 13321354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 13331354Seschrock { 13341354Seschrock spa_t *spa; 13352676Seschrock char *path = zc->zc_value; 13361544Seschrock uint64_t guid = zc->zc_guid; 13371354Seschrock int error; 13381354Seschrock 13391354Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 13401354Seschrock if (error != 0) 13411354Seschrock return (error); 13421354Seschrock 13431354Seschrock error = spa_vdev_setpath(spa, guid, path); 13441354Seschrock spa_close(spa, FTAG); 13451354Seschrock return (error); 13461354Seschrock } 13471354Seschrock 13489425SEric.Schrock@Sun.COM static int 13499425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 13509425SEric.Schrock@Sun.COM { 13519425SEric.Schrock@Sun.COM spa_t *spa; 13529425SEric.Schrock@Sun.COM char *fru = zc->zc_value; 13539425SEric.Schrock@Sun.COM uint64_t guid = zc->zc_guid; 13549425SEric.Schrock@Sun.COM int error; 13559425SEric.Schrock@Sun.COM 13569425SEric.Schrock@Sun.COM error = spa_open(zc->zc_name, &spa, FTAG); 13579425SEric.Schrock@Sun.COM if (error != 0) 13589425SEric.Schrock@Sun.COM return (error); 13599425SEric.Schrock@Sun.COM 13609425SEric.Schrock@Sun.COM error = spa_vdev_setfru(spa, guid, fru); 13619425SEric.Schrock@Sun.COM spa_close(spa, FTAG); 13629425SEric.Schrock@Sun.COM return (error); 13639425SEric.Schrock@Sun.COM } 13649425SEric.Schrock@Sun.COM 13655367Sahrens /* 13665367Sahrens * inputs: 13675367Sahrens * zc_name name of filesystem 13685367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 13695367Sahrens * 13705367Sahrens * outputs: 13715367Sahrens * zc_objset_stats stats 13725367Sahrens * zc_nvlist_dst property nvlist 13735367Sahrens * zc_nvlist_dst_size size of property nvlist 13745367Sahrens */ 13751354Seschrock static int 1376789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc) 1377789Sahrens { 1378789Sahrens objset_t *os = NULL; 1379789Sahrens int error; 13801356Seschrock nvlist_t *nv; 1381789Sahrens 138210298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1383789Sahrens return (error); 1384789Sahrens 13852885Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1386789Sahrens 13872856Snd150628 if (zc->zc_nvlist_dst != 0 && 13886689Smaybee (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) { 13892885Sahrens dmu_objset_stats(os, nv); 13903087Sahrens /* 13915147Srm160521 * NB: zvol_get_stats() will read the objset contents, 13923087Sahrens * which we aren't supposed to do with a 13936689Smaybee * DS_MODE_USER hold, because it could be 13943087Sahrens * inconsistent. So this is a bit of a workaround... 139510298SMatthew.Ahrens@Sun.COM * XXX reading with out owning 13963087Sahrens */ 13974577Sahrens if (!zc->zc_objset_stats.dds_inconsistent) { 13984577Sahrens if (dmu_objset_type(os) == DMU_OST_ZVOL) 13994577Sahrens VERIFY(zvol_get_stats(os, nv) == 0); 14004577Sahrens } 14012676Seschrock error = put_nvlist(zc, nv); 14021356Seschrock nvlist_free(nv); 14031356Seschrock } 1404789Sahrens 140510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1406789Sahrens return (error); 1407789Sahrens } 1408789Sahrens 14095498Stimh static int 14105498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 14115498Stimh { 14125498Stimh uint64_t value; 14135498Stimh int error; 14145498Stimh 14155498Stimh /* 14165498Stimh * zfs_get_zplprop() will either find a value or give us 14175498Stimh * the default value (if there is one). 14185498Stimh */ 14195498Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 14205498Stimh return (error); 14215498Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 14225498Stimh return (0); 14235498Stimh } 14245498Stimh 14255498Stimh /* 14265498Stimh * inputs: 14275498Stimh * zc_name name of filesystem 14285498Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 14295498Stimh * 14305498Stimh * outputs: 14315498Stimh * zc_nvlist_dst zpl property nvlist 14325498Stimh * zc_nvlist_dst_size size of zpl property nvlist 14335498Stimh */ 14345498Stimh static int 14355498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 14365498Stimh { 14375498Stimh objset_t *os; 14385498Stimh int err; 14395498Stimh 144010298SMatthew.Ahrens@Sun.COM /* XXX reading without owning */ 144110298SMatthew.Ahrens@Sun.COM if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 14425498Stimh return (err); 14435498Stimh 14445498Stimh dmu_objset_fast_stat(os, &zc->zc_objset_stats); 14455498Stimh 14465498Stimh /* 14475498Stimh * NB: nvl_add_zplprop() will read the objset contents, 14486689Smaybee * which we aren't supposed to do with a DS_MODE_USER 14496689Smaybee * hold, because it could be inconsistent. 14505498Stimh */ 14515498Stimh if (zc->zc_nvlist_dst != NULL && 14525498Stimh !zc->zc_objset_stats.dds_inconsistent && 14535498Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 14545498Stimh nvlist_t *nv; 14555498Stimh 14565498Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 14575498Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 14585498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 14595498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 14605498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 14615498Stimh err = put_nvlist(zc, nv); 14625498Stimh nvlist_free(nv); 14635498Stimh } else { 14645498Stimh err = ENOENT; 14655498Stimh } 146610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 14675498Stimh return (err); 14685498Stimh } 14695498Stimh 14709396SMatthew.Ahrens@Sun.COM static boolean_t 14719396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name) 14729396SMatthew.Ahrens@Sun.COM { 14739396SMatthew.Ahrens@Sun.COM /* 14749396SMatthew.Ahrens@Sun.COM * Skip over datasets that are not visible in this zone, 14759396SMatthew.Ahrens@Sun.COM * internal datasets (which have a $ in their name), and 14769396SMatthew.Ahrens@Sun.COM * temporary datasets (which have a % in their name). 14779396SMatthew.Ahrens@Sun.COM */ 14789396SMatthew.Ahrens@Sun.COM if (strchr(name, '$') != NULL) 14799396SMatthew.Ahrens@Sun.COM return (B_TRUE); 14809396SMatthew.Ahrens@Sun.COM if (strchr(name, '%') != NULL) 14819396SMatthew.Ahrens@Sun.COM return (B_TRUE); 14829396SMatthew.Ahrens@Sun.COM if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) 14839396SMatthew.Ahrens@Sun.COM return (B_TRUE); 14849396SMatthew.Ahrens@Sun.COM return (B_FALSE); 14859396SMatthew.Ahrens@Sun.COM } 14869396SMatthew.Ahrens@Sun.COM 14875367Sahrens /* 14885367Sahrens * inputs: 14895367Sahrens * zc_name name of filesystem 14905367Sahrens * zc_cookie zap cursor 14915367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 14925367Sahrens * 14935367Sahrens * outputs: 14945367Sahrens * zc_name name of next filesystem 14959396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 14965367Sahrens * zc_objset_stats stats 14975367Sahrens * zc_nvlist_dst property nvlist 14985367Sahrens * zc_nvlist_dst_size size of property nvlist 14995367Sahrens */ 1500789Sahrens static int 1501789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1502789Sahrens { 1503885Sahrens objset_t *os; 1504789Sahrens int error; 1505789Sahrens char *p; 1506789Sahrens 150710298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 1508885Sahrens if (error == ENOENT) 1509885Sahrens error = ESRCH; 1510885Sahrens return (error); 1511789Sahrens } 1512789Sahrens 1513789Sahrens p = strrchr(zc->zc_name, '/'); 1514789Sahrens if (p == NULL || p[1] != '\0') 1515789Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1516789Sahrens p = zc->zc_name + strlen(zc->zc_name); 1517789Sahrens 15188697SRichard.Morris@Sun.COM /* 15198697SRichard.Morris@Sun.COM * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 15208697SRichard.Morris@Sun.COM * but is not declared void because its called by dmu_objset_find(). 15218697SRichard.Morris@Sun.COM */ 15228415SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) { 15238415SRichard.Morris@Sun.COM uint64_t cookie = 0; 15248415SRichard.Morris@Sun.COM int len = sizeof (zc->zc_name) - (p - zc->zc_name); 15258415SRichard.Morris@Sun.COM 15268415SRichard.Morris@Sun.COM while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) 15278697SRichard.Morris@Sun.COM (void) dmu_objset_prefetch(p, NULL); 15288415SRichard.Morris@Sun.COM } 15298415SRichard.Morris@Sun.COM 1530789Sahrens do { 1531885Sahrens error = dmu_dir_list_next(os, 1532885Sahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 1533885Sahrens NULL, &zc->zc_cookie); 1534789Sahrens if (error == ENOENT) 1535789Sahrens error = ESRCH; 153610588SEric.Taylor@Sun.COM } while (error == 0 && dataset_name_hidden(zc->zc_name) && 153710588SEric.Taylor@Sun.COM !(zc->zc_iflags & FKIOCTL)); 153810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1539789Sahrens 154010588SEric.Taylor@Sun.COM /* 154110588SEric.Taylor@Sun.COM * If it's an internal dataset (ie. with a '$' in its name), 154210588SEric.Taylor@Sun.COM * don't try to get stats for it, otherwise we'll return ENOENT. 154310588SEric.Taylor@Sun.COM */ 154410588SEric.Taylor@Sun.COM if (error == 0 && strchr(zc->zc_name, '$') == NULL) 1545885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1546789Sahrens return (error); 1547789Sahrens } 1548789Sahrens 15495367Sahrens /* 15505367Sahrens * inputs: 15515367Sahrens * zc_name name of filesystem 15525367Sahrens * zc_cookie zap cursor 15535367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 15545367Sahrens * 15555367Sahrens * outputs: 15565367Sahrens * zc_name name of next snapshot 15575367Sahrens * zc_objset_stats stats 15585367Sahrens * zc_nvlist_dst property nvlist 15595367Sahrens * zc_nvlist_dst_size size of property nvlist 15605367Sahrens */ 1561789Sahrens static int 1562789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1563789Sahrens { 1564885Sahrens objset_t *os; 1565789Sahrens int error; 1566789Sahrens 156710474SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) 156810474SRichard.Morris@Sun.COM (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 156910474SRichard.Morris@Sun.COM NULL, DS_FIND_SNAPSHOTS); 157010474SRichard.Morris@Sun.COM 157110298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 15726689Smaybee if (error) 15736689Smaybee return (error == ENOENT ? ESRCH : error); 1574789Sahrens 15751003Slling /* 15761003Slling * A dataset name of maximum length cannot have any snapshots, 15771003Slling * so exit immediately. 15781003Slling */ 15791003Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 158010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 15811003Slling return (ESRCH); 1582789Sahrens } 1583789Sahrens 1584885Sahrens error = dmu_snapshot_list_next(os, 1585885Sahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 15865663Sck153898 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 158710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1588885Sahrens if (error == 0) 1589885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 15906689Smaybee else if (error == ENOENT) 15916689Smaybee error = ESRCH; 1592789Sahrens 15935367Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 15946689Smaybee if (error) 15955367Sahrens *strchr(zc->zc_name, '@') = '\0'; 1596789Sahrens return (error); 1597789Sahrens } 1598789Sahrens 15996423Sgw25295 int 16004787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl) 1601789Sahrens { 16022676Seschrock nvpair_t *elem; 16038724SRichard.Morris@Sun.COM int error = 0; 16042676Seschrock uint64_t intval; 16052676Seschrock char *strval; 16068697SRichard.Morris@Sun.COM nvlist_t *genericnvl; 16079396SMatthew.Ahrens@Sun.COM boolean_t issnap = (strchr(name, '@') != NULL); 16082676Seschrock 16094543Smarks /* 16104543Smarks * First validate permission to set all of the properties 16114543Smarks */ 16122676Seschrock elem = NULL; 16132676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 16144670Sahrens const char *propname = nvpair_name(elem); 16154670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 16162676Seschrock 16175094Slling if (prop == ZPROP_INVAL) { 16182676Seschrock /* 16192676Seschrock * If this is a user-defined property, it must be a 16202676Seschrock * string, and there is no further validation to do. 16212676Seschrock */ 16229396SMatthew.Ahrens@Sun.COM if (zfs_prop_user(propname) && 16239396SMatthew.Ahrens@Sun.COM nvpair_type(elem) == DATA_TYPE_STRING) { 16249396SMatthew.Ahrens@Sun.COM if (error = zfs_secpolicy_write_perms(name, 16259396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERPROP, CRED())) 16269396SMatthew.Ahrens@Sun.COM return (error); 16279396SMatthew.Ahrens@Sun.COM continue; 16289396SMatthew.Ahrens@Sun.COM } 16299396SMatthew.Ahrens@Sun.COM 16309396SMatthew.Ahrens@Sun.COM if (!issnap && zfs_prop_userquota(propname) && 16319396SMatthew.Ahrens@Sun.COM nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) { 16329396SMatthew.Ahrens@Sun.COM const char *perm; 16339396SMatthew.Ahrens@Sun.COM const char *up = zfs_userquota_prop_prefixes 16349396SMatthew.Ahrens@Sun.COM [ZFS_PROP_USERQUOTA]; 16359396SMatthew.Ahrens@Sun.COM if (strncmp(propname, up, strlen(up)) == 0) 16369396SMatthew.Ahrens@Sun.COM perm = ZFS_DELEG_PERM_USERQUOTA; 16379396SMatthew.Ahrens@Sun.COM else 16389396SMatthew.Ahrens@Sun.COM perm = ZFS_DELEG_PERM_GROUPQUOTA; 16399396SMatthew.Ahrens@Sun.COM if (error = zfs_secpolicy_write_perms(name, 16409396SMatthew.Ahrens@Sun.COM perm, CRED())) 16419396SMatthew.Ahrens@Sun.COM return (error); 16429396SMatthew.Ahrens@Sun.COM continue; 16439396SMatthew.Ahrens@Sun.COM } 16449396SMatthew.Ahrens@Sun.COM 16459396SMatthew.Ahrens@Sun.COM return (EINVAL); 16462676Seschrock } 16472676Seschrock 16489396SMatthew.Ahrens@Sun.COM if (issnap) 16499396SMatthew.Ahrens@Sun.COM return (EINVAL); 16509396SMatthew.Ahrens@Sun.COM 16514787Sahrens if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 16524670Sahrens return (error); 16532676Seschrock 16544670Sahrens /* 16554670Sahrens * Check that this value is valid for this pool version 16564670Sahrens */ 16574670Sahrens switch (prop) { 16583886Sahl case ZFS_PROP_COMPRESSION: 16593886Sahl /* 16603886Sahl * If the user specified gzip compression, make sure 16613886Sahl * the SPA supports it. We ignore any errors here since 16623886Sahl * we'll catch them later. 16633886Sahl */ 16643886Sahl if (nvpair_type(elem) == DATA_TYPE_UINT64 && 16657042Sgw25295 nvpair_value_uint64(elem, &intval) == 0) { 16667042Sgw25295 if (intval >= ZIO_COMPRESS_GZIP_1 && 16677042Sgw25295 intval <= ZIO_COMPRESS_GZIP_9 && 16687184Stimh zfs_earlier_version(name, 16695331Samw SPA_VERSION_GZIP_COMPRESSION)) 16705331Samw return (ENOTSUP); 16717042Sgw25295 16727042Sgw25295 /* 16737042Sgw25295 * If this is a bootable dataset then 16747042Sgw25295 * verify that the compression algorithm 16757042Sgw25295 * is supported for booting. We must return 16767042Sgw25295 * something other than ENOTSUP since it 16777042Sgw25295 * implies a downrev pool version. 16787042Sgw25295 */ 16797042Sgw25295 if (zfs_is_bootfs(name) && 16807042Sgw25295 !BOOTFS_COMPRESS_VALID(intval)) 16817042Sgw25295 return (ERANGE); 16823886Sahl } 16833886Sahl break; 16844603Sahrens 16854603Sahrens case ZFS_PROP_COPIES: 16869396SMatthew.Ahrens@Sun.COM if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS)) 16875331Samw return (ENOTSUP); 16884603Sahrens break; 16895977Smarks 16905977Smarks case ZFS_PROP_SHARESMB: 16916689Smaybee if (zpl_earlier_version(name, ZPL_VERSION_FUID)) 16925977Smarks return (ENOTSUP); 16935977Smarks break; 16948053SMark.Shellenbaum@Sun.COM 16958053SMark.Shellenbaum@Sun.COM case ZFS_PROP_ACLINHERIT: 16968053SMark.Shellenbaum@Sun.COM if (nvpair_type(elem) == DATA_TYPE_UINT64 && 16978053SMark.Shellenbaum@Sun.COM nvpair_value_uint64(elem, &intval) == 0) 16988053SMark.Shellenbaum@Sun.COM if (intval == ZFS_ACL_PASSTHROUGH_X && 16998053SMark.Shellenbaum@Sun.COM zfs_earlier_version(name, 17008053SMark.Shellenbaum@Sun.COM SPA_VERSION_PASSTHROUGH_X)) 17018053SMark.Shellenbaum@Sun.COM return (ENOTSUP); 17024603Sahrens } 17034543Smarks } 17044543Smarks 17058697SRichard.Morris@Sun.COM VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 17064543Smarks elem = NULL; 17074543Smarks while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 17084670Sahrens const char *propname = nvpair_name(elem); 17094670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 17104543Smarks 17115094Slling if (prop == ZPROP_INVAL) { 17129396SMatthew.Ahrens@Sun.COM if (zfs_prop_userquota(propname)) { 17139396SMatthew.Ahrens@Sun.COM uint64_t *valary; 17149396SMatthew.Ahrens@Sun.COM unsigned int vallen; 17159396SMatthew.Ahrens@Sun.COM const char *domain; 17169396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t type; 17179396SMatthew.Ahrens@Sun.COM uint64_t rid; 17189396SMatthew.Ahrens@Sun.COM uint64_t quota; 17199396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 17209396SMatthew.Ahrens@Sun.COM 17219396SMatthew.Ahrens@Sun.COM VERIFY(nvpair_value_uint64_array(elem, 17229396SMatthew.Ahrens@Sun.COM &valary, &vallen) == 0); 17239396SMatthew.Ahrens@Sun.COM VERIFY(vallen == 3); 17249396SMatthew.Ahrens@Sun.COM type = valary[0]; 17259396SMatthew.Ahrens@Sun.COM rid = valary[1]; 17269396SMatthew.Ahrens@Sun.COM quota = valary[2]; 17279396SMatthew.Ahrens@Sun.COM domain = propname + 17289396SMatthew.Ahrens@Sun.COM strlen(zfs_userquota_prop_prefixes[type]); 17299396SMatthew.Ahrens@Sun.COM 173010298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(name, FTAG, &zfsvfs); 17319396SMatthew.Ahrens@Sun.COM if (error == 0) { 17329396SMatthew.Ahrens@Sun.COM error = zfs_set_userquota(zfsvfs, 17339396SMatthew.Ahrens@Sun.COM type, domain, rid, quota); 17349396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 17359396SMatthew.Ahrens@Sun.COM } 17369396SMatthew.Ahrens@Sun.COM if (error == 0) 17379396SMatthew.Ahrens@Sun.COM continue; 17389396SMatthew.Ahrens@Sun.COM else 17399396SMatthew.Ahrens@Sun.COM goto out; 17409396SMatthew.Ahrens@Sun.COM } else if (zfs_prop_user(propname)) { 17419396SMatthew.Ahrens@Sun.COM VERIFY(nvpair_value_string(elem, &strval) == 0); 17429396SMatthew.Ahrens@Sun.COM error = dsl_prop_set(name, propname, 1, 17439396SMatthew.Ahrens@Sun.COM strlen(strval) + 1, strval); 17449396SMatthew.Ahrens@Sun.COM if (error == 0) 17459396SMatthew.Ahrens@Sun.COM continue; 17469396SMatthew.Ahrens@Sun.COM else 17479396SMatthew.Ahrens@Sun.COM goto out; 17489396SMatthew.Ahrens@Sun.COM } 17494543Smarks } 17502676Seschrock 17512676Seschrock switch (prop) { 17522676Seschrock case ZFS_PROP_QUOTA: 17532676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17544577Sahrens (error = dsl_dir_set_quota(name, intval)) != 0) 17558697SRichard.Morris@Sun.COM goto out; 17562676Seschrock break; 17572676Seschrock 17585378Sck153898 case ZFS_PROP_REFQUOTA: 17595378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17605378Sck153898 (error = dsl_dataset_set_quota(name, intval)) != 0) 17618697SRichard.Morris@Sun.COM goto out; 17625378Sck153898 break; 17635378Sck153898 17642676Seschrock case ZFS_PROP_RESERVATION: 17652676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17662676Seschrock (error = dsl_dir_set_reservation(name, 17672676Seschrock intval)) != 0) 17688697SRichard.Morris@Sun.COM goto out; 17692676Seschrock break; 1770789Sahrens 17715378Sck153898 case ZFS_PROP_REFRESERVATION: 17725378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17735378Sck153898 (error = dsl_dataset_set_reservation(name, 17745378Sck153898 intval)) != 0) 17758697SRichard.Morris@Sun.COM goto out; 17765378Sck153898 break; 17775378Sck153898 17782676Seschrock case ZFS_PROP_VOLSIZE: 17792676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17804787Sahrens (error = zvol_set_volsize(name, 17814787Sahrens ddi_driver_major(zfs_dip), intval)) != 0) 17828697SRichard.Morris@Sun.COM goto out; 17832676Seschrock break; 17842676Seschrock 17854577Sahrens case ZFS_PROP_VERSION: 17869396SMatthew.Ahrens@Sun.COM { 17879396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 17889396SMatthew.Ahrens@Sun.COM 17899396SMatthew.Ahrens@Sun.COM if ((error = nvpair_value_uint64(elem, &intval)) != 0) 17909396SMatthew.Ahrens@Sun.COM goto out; 179110298SMatthew.Ahrens@Sun.COM if ((error = zfsvfs_hold(name, FTAG, &zfsvfs)) != 0) 17929396SMatthew.Ahrens@Sun.COM goto out; 17939396SMatthew.Ahrens@Sun.COM error = zfs_set_version(zfsvfs, intval); 17949396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 17959396SMatthew.Ahrens@Sun.COM 17969396SMatthew.Ahrens@Sun.COM if (error == 0 && intval >= ZPL_VERSION_USERSPACE) { 17979396SMatthew.Ahrens@Sun.COM zfs_cmd_t zc = { 0 }; 17989396SMatthew.Ahrens@Sun.COM (void) strcpy(zc.zc_name, name); 17999396SMatthew.Ahrens@Sun.COM (void) zfs_ioc_userspace_upgrade(&zc); 18009396SMatthew.Ahrens@Sun.COM } 18019396SMatthew.Ahrens@Sun.COM if (error) 18028697SRichard.Morris@Sun.COM goto out; 18032676Seschrock break; 18049396SMatthew.Ahrens@Sun.COM } 18052676Seschrock 18062676Seschrock default: 18072676Seschrock if (nvpair_type(elem) == DATA_TYPE_STRING) { 18082676Seschrock if (zfs_prop_get_type(prop) != 18098697SRichard.Morris@Sun.COM PROP_TYPE_STRING) { 18108697SRichard.Morris@Sun.COM error = EINVAL; 18118697SRichard.Morris@Sun.COM goto out; 18128697SRichard.Morris@Sun.COM } 18132676Seschrock } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 18142885Sahrens const char *unused; 18152885Sahrens 18162717Seschrock VERIFY(nvpair_value_uint64(elem, &intval) == 0); 18172676Seschrock 18182676Seschrock switch (zfs_prop_get_type(prop)) { 18194787Sahrens case PROP_TYPE_NUMBER: 18202676Seschrock break; 18214787Sahrens case PROP_TYPE_STRING: 18228697SRichard.Morris@Sun.COM error = EINVAL; 18238697SRichard.Morris@Sun.COM goto out; 18244787Sahrens case PROP_TYPE_INDEX: 18252717Seschrock if (zfs_prop_index_to_string(prop, 18268697SRichard.Morris@Sun.COM intval, &unused) != 0) { 18278697SRichard.Morris@Sun.COM error = EINVAL; 18288697SRichard.Morris@Sun.COM goto out; 18298697SRichard.Morris@Sun.COM } 18302676Seschrock break; 18312676Seschrock default: 18324577Sahrens cmn_err(CE_PANIC, 18334577Sahrens "unknown property type"); 18342676Seschrock break; 18352676Seschrock } 18362676Seschrock } else { 18378697SRichard.Morris@Sun.COM error = EINVAL; 18388697SRichard.Morris@Sun.COM goto out; 18392676Seschrock } 18408697SRichard.Morris@Sun.COM if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0) 18418697SRichard.Morris@Sun.COM goto out; 18422676Seschrock } 18432676Seschrock } 18442676Seschrock 18458697SRichard.Morris@Sun.COM if (nvlist_next_nvpair(genericnvl, NULL) != NULL) { 18468697SRichard.Morris@Sun.COM error = dsl_props_set(name, genericnvl); 18478697SRichard.Morris@Sun.COM } 18488697SRichard.Morris@Sun.COM out: 18498697SRichard.Morris@Sun.COM nvlist_free(genericnvl); 18508697SRichard.Morris@Sun.COM return (error); 1851789Sahrens } 1852789Sahrens 18535367Sahrens /* 18549355SMatthew.Ahrens@Sun.COM * Check that all the properties are valid user properties. 18559355SMatthew.Ahrens@Sun.COM */ 18569355SMatthew.Ahrens@Sun.COM static int 18579355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl) 18589355SMatthew.Ahrens@Sun.COM { 18599355SMatthew.Ahrens@Sun.COM nvpair_t *elem = NULL; 18609355SMatthew.Ahrens@Sun.COM int error = 0; 18619355SMatthew.Ahrens@Sun.COM 18629355SMatthew.Ahrens@Sun.COM while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 18639355SMatthew.Ahrens@Sun.COM const char *propname = nvpair_name(elem); 18649355SMatthew.Ahrens@Sun.COM char *valstr; 18659355SMatthew.Ahrens@Sun.COM 18669355SMatthew.Ahrens@Sun.COM if (!zfs_prop_user(propname) || 18679355SMatthew.Ahrens@Sun.COM nvpair_type(elem) != DATA_TYPE_STRING) 18689355SMatthew.Ahrens@Sun.COM return (EINVAL); 18699355SMatthew.Ahrens@Sun.COM 18709355SMatthew.Ahrens@Sun.COM if (error = zfs_secpolicy_write_perms(fsname, 18719355SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERPROP, CRED())) 18729355SMatthew.Ahrens@Sun.COM return (error); 18739355SMatthew.Ahrens@Sun.COM 18749355SMatthew.Ahrens@Sun.COM if (strlen(propname) >= ZAP_MAXNAMELEN) 18759355SMatthew.Ahrens@Sun.COM return (ENAMETOOLONG); 18769355SMatthew.Ahrens@Sun.COM 18779355SMatthew.Ahrens@Sun.COM VERIFY(nvpair_value_string(elem, &valstr) == 0); 18789355SMatthew.Ahrens@Sun.COM if (strlen(valstr) >= ZAP_MAXVALUELEN) 18799355SMatthew.Ahrens@Sun.COM return (E2BIG); 18809355SMatthew.Ahrens@Sun.COM } 18819355SMatthew.Ahrens@Sun.COM return (0); 18829355SMatthew.Ahrens@Sun.COM } 18839355SMatthew.Ahrens@Sun.COM 18849355SMatthew.Ahrens@Sun.COM /* 18855367Sahrens * inputs: 18865367Sahrens * zc_name name of filesystem 18878697SRichard.Morris@Sun.COM * zc_value name of property to set 18885367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 18897265Sahrens * zc_cookie clear existing local props? 18905367Sahrens * 18915367Sahrens * outputs: none 18925367Sahrens */ 1893789Sahrens static int 18942676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 1895789Sahrens { 18962676Seschrock nvlist_t *nvl; 18972676Seschrock int error; 1898789Sahrens 18995094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 19009643SEric.Taylor@Sun.COM zc->zc_iflags, &nvl)) != 0) 19012676Seschrock return (error); 19022676Seschrock 19037265Sahrens if (zc->zc_cookie) { 19047265Sahrens nvlist_t *origprops; 19057265Sahrens objset_t *os; 19067265Sahrens 190710298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) { 19087265Sahrens if (dsl_prop_get_all(os, &origprops, TRUE) == 0) { 19098536SDavid.Pacheco@Sun.COM clear_props(zc->zc_name, origprops, nvl); 19107265Sahrens nvlist_free(origprops); 19117265Sahrens } 191210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 19137265Sahrens } 19147265Sahrens 19157265Sahrens } 19167265Sahrens 19174787Sahrens error = zfs_set_prop_nvlist(zc->zc_name, nvl); 19184543Smarks 19192676Seschrock nvlist_free(nvl); 19202676Seschrock return (error); 1921789Sahrens } 1922789Sahrens 19235367Sahrens /* 19245367Sahrens * inputs: 19255367Sahrens * zc_name name of filesystem 19265367Sahrens * zc_value name of property to inherit 19275367Sahrens * 19285367Sahrens * outputs: none 19295367Sahrens */ 1930789Sahrens static int 19314849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 19324849Sahrens { 19334849Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 19344849Sahrens return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL)); 19354849Sahrens } 19364849Sahrens 19374849Sahrens static int 19384098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 19393912Slling { 19405094Slling nvlist_t *props; 19413912Slling spa_t *spa; 19425094Slling int error; 19438525SEric.Schrock@Sun.COM nvpair_t *elem; 19443912Slling 19455094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 19469643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) 19473912Slling return (error); 19483912Slling 19498525SEric.Schrock@Sun.COM /* 19508525SEric.Schrock@Sun.COM * If the only property is the configfile, then just do a spa_lookup() 19518525SEric.Schrock@Sun.COM * to handle the faulted case. 19528525SEric.Schrock@Sun.COM */ 19538525SEric.Schrock@Sun.COM elem = nvlist_next_nvpair(props, NULL); 19548525SEric.Schrock@Sun.COM if (elem != NULL && strcmp(nvpair_name(elem), 19558525SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 19568525SEric.Schrock@Sun.COM nvlist_next_nvpair(props, elem) == NULL) { 19578525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 19588525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) { 19598525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 19608525SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 19618525SEric.Schrock@Sun.COM } 19628525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 196310672SEric.Schrock@Sun.COM if (spa != NULL) { 196410672SEric.Schrock@Sun.COM nvlist_free(props); 19658525SEric.Schrock@Sun.COM return (0); 196610672SEric.Schrock@Sun.COM } 19678525SEric.Schrock@Sun.COM } 19688525SEric.Schrock@Sun.COM 19693912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 19705094Slling nvlist_free(props); 19713912Slling return (error); 19723912Slling } 19733912Slling 19745094Slling error = spa_prop_set(spa, props); 19753912Slling 19765094Slling nvlist_free(props); 19773912Slling spa_close(spa, FTAG); 19783912Slling 19793912Slling return (error); 19803912Slling } 19813912Slling 19823912Slling static int 19834098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 19843912Slling { 19853912Slling spa_t *spa; 19863912Slling int error; 19873912Slling nvlist_t *nvp = NULL; 19883912Slling 19898525SEric.Schrock@Sun.COM if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 19908525SEric.Schrock@Sun.COM /* 19918525SEric.Schrock@Sun.COM * If the pool is faulted, there may be properties we can still 19928525SEric.Schrock@Sun.COM * get (such as altroot and cachefile), so attempt to get them 19938525SEric.Schrock@Sun.COM * anyway. 19948525SEric.Schrock@Sun.COM */ 19958525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 19968525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) 19978525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 19988525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 19998525SEric.Schrock@Sun.COM } else { 20008525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 20018525SEric.Schrock@Sun.COM spa_close(spa, FTAG); 20028525SEric.Schrock@Sun.COM } 20033912Slling 20043912Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 20053912Slling error = put_nvlist(zc, nvp); 20063912Slling else 20073912Slling error = EFAULT; 20083912Slling 20098525SEric.Schrock@Sun.COM nvlist_free(nvp); 20103912Slling return (error); 20113912Slling } 20123912Slling 20133912Slling static int 20144543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 20154543Smarks { 20164543Smarks nvlist_t *nvp; 20174543Smarks int error; 20184543Smarks uint32_t uid; 20194543Smarks uint32_t gid; 20204543Smarks uint32_t *groups; 20214543Smarks uint_t group_cnt; 20224543Smarks cred_t *usercred; 20234543Smarks 20245094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 20259643SEric.Taylor@Sun.COM zc->zc_iflags, &nvp)) != 0) { 20264543Smarks return (error); 20274543Smarks } 20284543Smarks 20294543Smarks if ((error = nvlist_lookup_uint32(nvp, 20304543Smarks ZFS_DELEG_PERM_UID, &uid)) != 0) { 20314543Smarks nvlist_free(nvp); 20324543Smarks return (EPERM); 20334543Smarks } 20344543Smarks 20354543Smarks if ((error = nvlist_lookup_uint32(nvp, 20364543Smarks ZFS_DELEG_PERM_GID, &gid)) != 0) { 20374543Smarks nvlist_free(nvp); 20384543Smarks return (EPERM); 20394543Smarks } 20404543Smarks 20414543Smarks if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 20424543Smarks &groups, &group_cnt)) != 0) { 20434543Smarks nvlist_free(nvp); 20444543Smarks return (EPERM); 20454543Smarks } 20464543Smarks usercred = cralloc(); 20474543Smarks if ((crsetugid(usercred, uid, gid) != 0) || 20484543Smarks (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 20494543Smarks nvlist_free(nvp); 20504543Smarks crfree(usercred); 20514543Smarks return (EPERM); 20524543Smarks } 20534543Smarks nvlist_free(nvp); 20544543Smarks error = dsl_deleg_access(zc->zc_name, 20554787Sahrens zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred); 20564543Smarks crfree(usercred); 20574543Smarks return (error); 20584543Smarks } 20594543Smarks 20605367Sahrens /* 20615367Sahrens * inputs: 20625367Sahrens * zc_name name of filesystem 20635367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 20645367Sahrens * zc_perm_action allow/unallow flag 20655367Sahrens * 20665367Sahrens * outputs: none 20675367Sahrens */ 20684543Smarks static int 20694543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 20704543Smarks { 20714543Smarks int error; 20724543Smarks nvlist_t *fsaclnv = NULL; 20734543Smarks 20745094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 20759643SEric.Taylor@Sun.COM zc->zc_iflags, &fsaclnv)) != 0) 20764543Smarks return (error); 20774543Smarks 20784543Smarks /* 20794543Smarks * Verify nvlist is constructed correctly 20804543Smarks */ 20814543Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 20824543Smarks nvlist_free(fsaclnv); 20834543Smarks return (EINVAL); 20844543Smarks } 20854543Smarks 20864543Smarks /* 20874543Smarks * If we don't have PRIV_SYS_MOUNT, then validate 20884543Smarks * that user is allowed to hand out each permission in 20894543Smarks * the nvlist(s) 20904543Smarks */ 20914543Smarks 20924787Sahrens error = secpolicy_zfs(CRED()); 20934543Smarks if (error) { 20944787Sahrens if (zc->zc_perm_action == B_FALSE) { 20954787Sahrens error = dsl_deleg_can_allow(zc->zc_name, 20964787Sahrens fsaclnv, CRED()); 20974787Sahrens } else { 20984787Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 20994787Sahrens fsaclnv, CRED()); 21004787Sahrens } 21014543Smarks } 21024543Smarks 21034543Smarks if (error == 0) 21044543Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 21054543Smarks 21064543Smarks nvlist_free(fsaclnv); 21074543Smarks return (error); 21084543Smarks } 21094543Smarks 21105367Sahrens /* 21115367Sahrens * inputs: 21125367Sahrens * zc_name name of filesystem 21135367Sahrens * 21145367Sahrens * outputs: 21155367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 21165367Sahrens */ 21174543Smarks static int 21184543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 21194543Smarks { 21204543Smarks nvlist_t *nvp; 21214543Smarks int error; 21224543Smarks 21234543Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 21244543Smarks error = put_nvlist(zc, nvp); 21254543Smarks nvlist_free(nvp); 21264543Smarks } 21274543Smarks 21284543Smarks return (error); 21294543Smarks } 21304543Smarks 21315367Sahrens /* 2132789Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 2133789Sahrens * or NULL if no suitable entry is found. The caller of this routine 2134789Sahrens * is responsible for releasing the returned vfs pointer. 2135789Sahrens */ 2136789Sahrens static vfs_t * 2137789Sahrens zfs_get_vfs(const char *resource) 2138789Sahrens { 2139789Sahrens struct vfs *vfsp; 2140789Sahrens struct vfs *vfs_found = NULL; 2141789Sahrens 2142789Sahrens vfs_list_read_lock(); 2143789Sahrens vfsp = rootvfs; 2144789Sahrens do { 2145789Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2146789Sahrens VFS_HOLD(vfsp); 2147789Sahrens vfs_found = vfsp; 2148789Sahrens break; 2149789Sahrens } 2150789Sahrens vfsp = vfsp->vfs_next; 2151789Sahrens } while (vfsp != rootvfs); 2152789Sahrens vfs_list_unlock(); 2153789Sahrens return (vfs_found); 2154789Sahrens } 2155789Sahrens 21564543Smarks /* ARGSUSED */ 2157789Sahrens static void 21584543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2159789Sahrens { 21605331Samw zfs_creat_t *zct = arg; 21615498Stimh 21625498Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 21635331Samw } 21645331Samw 21655498Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 21665498Stimh 21675331Samw /* 21685498Stimh * inputs: 21697184Stimh * createprops list of properties requested by creator 21707184Stimh * default_zplver zpl version to use if unspecified in createprops 21717184Stimh * fuids_ok fuids allowed in this version of the spa? 21727184Stimh * os parent objset pointer (NULL if root fs) 21735331Samw * 21745498Stimh * outputs: 21755498Stimh * zplprops values for the zplprops we attach to the master node object 21767184Stimh * is_ci true if requested file system will be purely case-insensitive 21775331Samw * 21785498Stimh * Determine the settings for utf8only, normalization and 21795498Stimh * casesensitivity. Specific values may have been requested by the 21805498Stimh * creator and/or we can inherit values from the parent dataset. If 21815498Stimh * the file system is of too early a vintage, a creator can not 21825498Stimh * request settings for these properties, even if the requested 21835498Stimh * setting is the default value. We don't actually want to create dsl 21845498Stimh * properties for these, so remove them from the source nvlist after 21855498Stimh * processing. 21865331Samw */ 21875331Samw static int 21889396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 21897184Stimh boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops, 21907184Stimh boolean_t *is_ci) 21915331Samw { 21925498Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 21935498Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 21945498Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 21955498Stimh 21965498Stimh ASSERT(zplprops != NULL); 21975498Stimh 21985375Stimh /* 21995498Stimh * Pull out creator prop choices, if any. 22005375Stimh */ 22015498Stimh if (createprops) { 22025498Stimh (void) nvlist_lookup_uint64(createprops, 22037184Stimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 22047184Stimh (void) nvlist_lookup_uint64(createprops, 22055498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 22065498Stimh (void) nvlist_remove_all(createprops, 22075498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 22085498Stimh (void) nvlist_lookup_uint64(createprops, 22095498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 22105498Stimh (void) nvlist_remove_all(createprops, 22115498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 22125498Stimh (void) nvlist_lookup_uint64(createprops, 22135498Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 22145498Stimh (void) nvlist_remove_all(createprops, 22155498Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 22165331Samw } 22175331Samw 22185375Stimh /* 22197184Stimh * If the zpl version requested is whacky or the file system 22207184Stimh * or pool is version is too "young" to support normalization 22217184Stimh * and the creator tried to set a value for one of the props, 22227184Stimh * error out. 22235498Stimh */ 22247184Stimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 22257184Stimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 22267184Stimh (zplver < ZPL_VERSION_NORMALIZATION && 22275498Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 22287184Stimh sense != ZFS_PROP_UNDEFINED))) 22295498Stimh return (ENOTSUP); 22305498Stimh 22315498Stimh /* 22325498Stimh * Put the version in the zplprops 22335498Stimh */ 22345498Stimh VERIFY(nvlist_add_uint64(zplprops, 22355498Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 22365498Stimh 22375498Stimh if (norm == ZFS_PROP_UNDEFINED) 22385498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 22395498Stimh VERIFY(nvlist_add_uint64(zplprops, 22405498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 22415498Stimh 22425498Stimh /* 22435498Stimh * If we're normalizing, names must always be valid UTF-8 strings. 22445498Stimh */ 22455498Stimh if (norm) 22465498Stimh u8 = 1; 22475498Stimh if (u8 == ZFS_PROP_UNDEFINED) 22485498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 22495498Stimh VERIFY(nvlist_add_uint64(zplprops, 22505498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 22515498Stimh 22525498Stimh if (sense == ZFS_PROP_UNDEFINED) 22535498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 22545498Stimh VERIFY(nvlist_add_uint64(zplprops, 22555498Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 22565498Stimh 22576492Stimh if (is_ci) 22586492Stimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 22596492Stimh 22607184Stimh return (0); 22617184Stimh } 22627184Stimh 22637184Stimh static int 22647184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 22657184Stimh nvlist_t *zplprops, boolean_t *is_ci) 22667184Stimh { 22677184Stimh boolean_t fuids_ok = B_TRUE; 22687184Stimh uint64_t zplver = ZPL_VERSION; 22697184Stimh objset_t *os = NULL; 22707184Stimh char parentname[MAXNAMELEN]; 22717184Stimh char *cp; 22727184Stimh int error; 22737184Stimh 22747184Stimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 22757184Stimh cp = strrchr(parentname, '/'); 22767184Stimh ASSERT(cp != NULL); 22777184Stimh cp[0] = '\0'; 22787184Stimh 22799396SMatthew.Ahrens@Sun.COM if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE)) 22809396SMatthew.Ahrens@Sun.COM zplver = ZPL_VERSION_USERSPACE - 1; 22817184Stimh if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) { 22827184Stimh zplver = ZPL_VERSION_FUID - 1; 22837184Stimh fuids_ok = B_FALSE; 22847184Stimh } 22857184Stimh 22867184Stimh /* 22877184Stimh * Open parent object set so we can inherit zplprop values. 22887184Stimh */ 228910298SMatthew.Ahrens@Sun.COM if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 22907184Stimh return (error); 22917184Stimh 22927184Stimh error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops, 22937184Stimh zplprops, is_ci); 229410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 22957184Stimh return (error); 22967184Stimh } 22977184Stimh 22987184Stimh static int 22997184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 23007184Stimh nvlist_t *zplprops, boolean_t *is_ci) 23017184Stimh { 23027184Stimh boolean_t fuids_ok = B_TRUE; 23037184Stimh uint64_t zplver = ZPL_VERSION; 23047184Stimh int error; 23057184Stimh 23067184Stimh if (spa_vers < SPA_VERSION_FUID) { 23077184Stimh zplver = ZPL_VERSION_FUID - 1; 23087184Stimh fuids_ok = B_FALSE; 23097184Stimh } 23107184Stimh 23117184Stimh error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops, 23127184Stimh zplprops, is_ci); 23137184Stimh return (error); 2314789Sahrens } 2315789Sahrens 23165367Sahrens /* 23175367Sahrens * inputs: 23185367Sahrens * zc_objset_type type of objset to create (fs vs zvol) 23195367Sahrens * zc_name name of new objset 23205367Sahrens * zc_value name of snapshot to clone from (may be empty) 23215367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 23225367Sahrens * 23235498Stimh * outputs: none 23245367Sahrens */ 2325789Sahrens static int 2326789Sahrens zfs_ioc_create(zfs_cmd_t *zc) 2327789Sahrens { 2328789Sahrens objset_t *clone; 2329789Sahrens int error = 0; 23305331Samw zfs_creat_t zct; 23314543Smarks nvlist_t *nvprops = NULL; 23324543Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2333789Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2334789Sahrens 2335789Sahrens switch (type) { 2336789Sahrens 2337789Sahrens case DMU_OST_ZFS: 2338789Sahrens cbfunc = zfs_create_cb; 2339789Sahrens break; 2340789Sahrens 2341789Sahrens case DMU_OST_ZVOL: 2342789Sahrens cbfunc = zvol_create_cb; 2343789Sahrens break; 2344789Sahrens 2345789Sahrens default: 23462199Sahrens cbfunc = NULL; 23476423Sgw25295 break; 23482199Sahrens } 23495326Sek110237 if (strchr(zc->zc_name, '@') || 23505326Sek110237 strchr(zc->zc_name, '%')) 2351789Sahrens return (EINVAL); 2352789Sahrens 23532676Seschrock if (zc->zc_nvlist_src != NULL && 23545094Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 23559643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 23562676Seschrock return (error); 23572676Seschrock 23585498Stimh zct.zct_zplprops = NULL; 23595331Samw zct.zct_props = nvprops; 23605331Samw 23612676Seschrock if (zc->zc_value[0] != '\0') { 2362789Sahrens /* 2363789Sahrens * We're creating a clone of an existing snapshot. 2364789Sahrens */ 23652676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 23662676Seschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 23674543Smarks nvlist_free(nvprops); 2368789Sahrens return (EINVAL); 23692676Seschrock } 2370789Sahrens 237110298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_value, FTAG, &clone); 23722676Seschrock if (error) { 23734543Smarks nvlist_free(nvprops); 2374789Sahrens return (error); 23752676Seschrock } 23766492Stimh 237710272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0); 237810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 23795331Samw if (error) { 23805331Samw nvlist_free(nvprops); 23815331Samw return (error); 23825331Samw } 2383789Sahrens } else { 23846492Stimh boolean_t is_insensitive = B_FALSE; 23856492Stimh 23862676Seschrock if (cbfunc == NULL) { 23874543Smarks nvlist_free(nvprops); 23882199Sahrens return (EINVAL); 23892676Seschrock } 23902676Seschrock 2391789Sahrens if (type == DMU_OST_ZVOL) { 23922676Seschrock uint64_t volsize, volblocksize; 23932676Seschrock 23944543Smarks if (nvprops == NULL || 23954543Smarks nvlist_lookup_uint64(nvprops, 23962676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 23972676Seschrock &volsize) != 0) { 23984543Smarks nvlist_free(nvprops); 23992676Seschrock return (EINVAL); 24002676Seschrock } 24012676Seschrock 24024543Smarks if ((error = nvlist_lookup_uint64(nvprops, 24032676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 24042676Seschrock &volblocksize)) != 0 && error != ENOENT) { 24054543Smarks nvlist_free(nvprops); 24062676Seschrock return (EINVAL); 24072676Seschrock } 24081133Seschrock 24092676Seschrock if (error != 0) 24102676Seschrock volblocksize = zfs_prop_default_numeric( 24112676Seschrock ZFS_PROP_VOLBLOCKSIZE); 24122676Seschrock 24132676Seschrock if ((error = zvol_check_volblocksize( 24142676Seschrock volblocksize)) != 0 || 24152676Seschrock (error = zvol_check_volsize(volsize, 24162676Seschrock volblocksize)) != 0) { 24174543Smarks nvlist_free(nvprops); 2418789Sahrens return (error); 24192676Seschrock } 24204577Sahrens } else if (type == DMU_OST_ZFS) { 24215331Samw int error; 24225331Samw 24235498Stimh /* 24245331Samw * We have to have normalization and 24255331Samw * case-folding flags correct when we do the 24265331Samw * file system creation, so go figure them out 24275498Stimh * now. 24285331Samw */ 24295498Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 24305498Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 24315498Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 24327184Stimh zct.zct_zplprops, &is_insensitive); 24335331Samw if (error != 0) { 24345331Samw nvlist_free(nvprops); 24355498Stimh nvlist_free(zct.zct_zplprops); 24365331Samw return (error); 24374577Sahrens } 24382676Seschrock } 243910272SMatthew.Ahrens@Sun.COM error = dmu_objset_create(zc->zc_name, type, 24406492Stimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 24415498Stimh nvlist_free(zct.zct_zplprops); 2442789Sahrens } 24432676Seschrock 24442676Seschrock /* 24452676Seschrock * It would be nice to do this atomically. 24462676Seschrock */ 24472676Seschrock if (error == 0) { 24484787Sahrens if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0) 244910242Schris.kirby@sun.com (void) dmu_objset_destroy(zc->zc_name, B_FALSE); 24502676Seschrock } 24514543Smarks nvlist_free(nvprops); 2452789Sahrens return (error); 2453789Sahrens } 2454789Sahrens 24555367Sahrens /* 24565367Sahrens * inputs: 24575367Sahrens * zc_name name of filesystem 24585367Sahrens * zc_value short name of snapshot 24595367Sahrens * zc_cookie recursive flag 24609396SMatthew.Ahrens@Sun.COM * zc_nvlist_src[_size] property list 24615367Sahrens * 246210588SEric.Taylor@Sun.COM * outputs: 246310588SEric.Taylor@Sun.COM * zc_value short snapname (i.e. part after the '@') 24645367Sahrens */ 2465789Sahrens static int 24662199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 24672199Sahrens { 24687265Sahrens nvlist_t *nvprops = NULL; 24697265Sahrens int error; 24707265Sahrens boolean_t recursive = zc->zc_cookie; 24717265Sahrens 24722676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 24732199Sahrens return (EINVAL); 24747265Sahrens 24757265Sahrens if (zc->zc_nvlist_src != NULL && 24767265Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 24779643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 24787265Sahrens return (error); 24797265Sahrens 24809355SMatthew.Ahrens@Sun.COM error = zfs_check_userprops(zc->zc_name, nvprops); 24819355SMatthew.Ahrens@Sun.COM if (error) 24829355SMatthew.Ahrens@Sun.COM goto out; 24839355SMatthew.Ahrens@Sun.COM 24849355SMatthew.Ahrens@Sun.COM if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL && 24859355SMatthew.Ahrens@Sun.COM zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) { 24869355SMatthew.Ahrens@Sun.COM error = ENOTSUP; 24879355SMatthew.Ahrens@Sun.COM goto out; 24887265Sahrens } 24899355SMatthew.Ahrens@Sun.COM 24909355SMatthew.Ahrens@Sun.COM error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, 24919355SMatthew.Ahrens@Sun.COM nvprops, recursive); 24929355SMatthew.Ahrens@Sun.COM 24939355SMatthew.Ahrens@Sun.COM out: 24947265Sahrens nvlist_free(nvprops); 24957265Sahrens return (error); 24962199Sahrens } 24972199Sahrens 24984007Smmusante int 24992199Sahrens zfs_unmount_snap(char *name, void *arg) 2500789Sahrens { 25012417Sahrens vfs_t *vfsp = NULL; 25022199Sahrens 25036689Smaybee if (arg) { 25046689Smaybee char *snapname = arg; 25056689Smaybee int len = strlen(name) + strlen(snapname) + 2; 25066689Smaybee char *buf = kmem_alloc(len, KM_SLEEP); 25076689Smaybee 25086689Smaybee (void) strcpy(buf, name); 25096689Smaybee (void) strcat(buf, "@"); 25106689Smaybee (void) strcat(buf, snapname); 25116689Smaybee vfsp = zfs_get_vfs(buf); 25126689Smaybee kmem_free(buf, len); 25132417Sahrens } else if (strchr(name, '@')) { 25142199Sahrens vfsp = zfs_get_vfs(name); 25152199Sahrens } 25162199Sahrens 25172199Sahrens if (vfsp) { 25182199Sahrens /* 25192199Sahrens * Always force the unmount for snapshots. 25202199Sahrens */ 25212199Sahrens int flag = MS_FORCE; 2522789Sahrens int err; 2523789Sahrens 25242199Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 25252199Sahrens VFS_RELE(vfsp); 25262199Sahrens return (err); 25272199Sahrens } 25282199Sahrens VFS_RELE(vfsp); 25292199Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 25302199Sahrens return (err); 25312199Sahrens } 25322199Sahrens return (0); 25332199Sahrens } 25342199Sahrens 25355367Sahrens /* 25365367Sahrens * inputs: 253710242Schris.kirby@sun.com * zc_name name of filesystem 253810242Schris.kirby@sun.com * zc_value short name of snapshot 253910242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 25405367Sahrens * 25415367Sahrens * outputs: none 25425367Sahrens */ 25432199Sahrens static int 25442199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 25452199Sahrens { 25462199Sahrens int err; 2547789Sahrens 25482676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 25492199Sahrens return (EINVAL); 25502199Sahrens err = dmu_objset_find(zc->zc_name, 25512676Seschrock zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 25522199Sahrens if (err) 25532199Sahrens return (err); 255410242Schris.kirby@sun.com return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value, 255510242Schris.kirby@sun.com zc->zc_defer_destroy)); 25562199Sahrens } 25572199Sahrens 25585367Sahrens /* 25595367Sahrens * inputs: 25605367Sahrens * zc_name name of dataset to destroy 25615367Sahrens * zc_objset_type type of objset 256210242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 25635367Sahrens * 25645367Sahrens * outputs: none 25655367Sahrens */ 25662199Sahrens static int 25672199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 25682199Sahrens { 256910588SEric.Taylor@Sun.COM int err; 25702199Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 257110588SEric.Taylor@Sun.COM err = zfs_unmount_snap(zc->zc_name, NULL); 25722199Sahrens if (err) 25732199Sahrens return (err); 2574789Sahrens } 2575789Sahrens 257610588SEric.Taylor@Sun.COM err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy); 257710588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 257810693Schris.kirby@sun.com (void) zvol_remove_minor(zc->zc_name); 257910588SEric.Taylor@Sun.COM return (err); 2580789Sahrens } 2581789Sahrens 25825367Sahrens /* 25835367Sahrens * inputs: 25845446Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 25855367Sahrens * 25865367Sahrens * outputs: none 25875367Sahrens */ 2588789Sahrens static int 2589789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 2590789Sahrens { 259110272SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds, *clone; 25925446Sahrens int error; 259310272SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 259410272SMatthew.Ahrens@Sun.COM char *clone_name; 259510272SMatthew.Ahrens@Sun.COM 259610272SMatthew.Ahrens@Sun.COM error = dsl_dataset_hold(zc->zc_name, FTAG, &ds); 259710272SMatthew.Ahrens@Sun.COM if (error) 259810272SMatthew.Ahrens@Sun.COM return (error); 259910272SMatthew.Ahrens@Sun.COM 260010272SMatthew.Ahrens@Sun.COM /* must not be a snapshot */ 260110272SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds)) { 260210272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 260310272SMatthew.Ahrens@Sun.COM return (EINVAL); 260410272SMatthew.Ahrens@Sun.COM } 260510272SMatthew.Ahrens@Sun.COM 260610272SMatthew.Ahrens@Sun.COM /* must have a most recent snapshot */ 260710272SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 260810272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 260910272SMatthew.Ahrens@Sun.COM return (EINVAL); 261010272SMatthew.Ahrens@Sun.COM } 26115446Sahrens 26125446Sahrens /* 261310272SMatthew.Ahrens@Sun.COM * Create clone of most recent snapshot. 26145446Sahrens */ 261510272SMatthew.Ahrens@Sun.COM clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name); 261610272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT); 26175446Sahrens if (error) 261810272SMatthew.Ahrens@Sun.COM goto out; 261910272SMatthew.Ahrens@Sun.COM 262010298SMatthew.Ahrens@Sun.COM error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone); 262110272SMatthew.Ahrens@Sun.COM if (error) 262210272SMatthew.Ahrens@Sun.COM goto out; 262310272SMatthew.Ahrens@Sun.COM 262410272SMatthew.Ahrens@Sun.COM /* 262510272SMatthew.Ahrens@Sun.COM * Do clone swap. 262610272SMatthew.Ahrens@Sun.COM */ 26279396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 262810298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 26296083Sek110237 if (error == 0) { 26306083Sek110237 int resume_err; 26316083Sek110237 263210272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 263310272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, 263410272SMatthew.Ahrens@Sun.COM B_TRUE); 263510272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 263610272SMatthew.Ahrens@Sun.COM ds = NULL; 263710272SMatthew.Ahrens@Sun.COM } else { 263810272SMatthew.Ahrens@Sun.COM error = EBUSY; 263910272SMatthew.Ahrens@Sun.COM } 264010298SMatthew.Ahrens@Sun.COM resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); 26416083Sek110237 error = error ? error : resume_err; 26426083Sek110237 } 26435446Sahrens VFS_RELE(zfsvfs->z_vfs); 26445446Sahrens } else { 264510272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 264610272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, B_TRUE); 264710272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 264810272SMatthew.Ahrens@Sun.COM ds = NULL; 264910272SMatthew.Ahrens@Sun.COM } else { 265010272SMatthew.Ahrens@Sun.COM error = EBUSY; 265110272SMatthew.Ahrens@Sun.COM } 26525446Sahrens } 265310272SMatthew.Ahrens@Sun.COM 265410272SMatthew.Ahrens@Sun.COM /* 265510272SMatthew.Ahrens@Sun.COM * Destroy clone (which also closes it). 265610272SMatthew.Ahrens@Sun.COM */ 265710272SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(clone, FTAG, B_FALSE); 265810272SMatthew.Ahrens@Sun.COM 265910272SMatthew.Ahrens@Sun.COM out: 266010272SMatthew.Ahrens@Sun.COM strfree(clone_name); 266110272SMatthew.Ahrens@Sun.COM if (ds) 266210272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 26635446Sahrens return (error); 2664789Sahrens } 2665789Sahrens 26665367Sahrens /* 26675367Sahrens * inputs: 26685367Sahrens * zc_name old name of dataset 26695367Sahrens * zc_value new name of dataset 26705367Sahrens * zc_cookie recursive flag (only valid for snapshots) 26715367Sahrens * 26725367Sahrens * outputs: none 26735367Sahrens */ 2674789Sahrens static int 2675789Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 2676789Sahrens { 26774490Svb160487 boolean_t recursive = zc->zc_cookie & 1; 26784007Smmusante 26792676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 26805326Sek110237 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 26815326Sek110237 strchr(zc->zc_value, '%')) 2682789Sahrens return (EINVAL); 2683789Sahrens 26844007Smmusante /* 26854007Smmusante * Unmount snapshot unless we're doing a recursive rename, 26864007Smmusante * in which case the dataset code figures out which snapshots 26874007Smmusante * to unmount. 26884007Smmusante */ 26894007Smmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 2690789Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 26912199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 26922199Sahrens if (err) 26932199Sahrens return (err); 2694789Sahrens } 269510588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL) 269610588SEric.Taylor@Sun.COM (void) zvol_remove_minor(zc->zc_name); 26974007Smmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 2698789Sahrens } 2699789Sahrens 27006689Smaybee static void 27018536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops) 27026689Smaybee { 27036689Smaybee zfs_cmd_t *zc; 27046689Smaybee nvpair_t *prop; 27056689Smaybee 27066689Smaybee if (props == NULL) 27076689Smaybee return; 27086689Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 27096689Smaybee (void) strcpy(zc->zc_name, dataset); 27106689Smaybee for (prop = nvlist_next_nvpair(props, NULL); prop; 27116689Smaybee prop = nvlist_next_nvpair(props, prop)) { 27128536SDavid.Pacheco@Sun.COM if (newprops != NULL && 27138536SDavid.Pacheco@Sun.COM nvlist_exists(newprops, nvpair_name(prop))) 27148536SDavid.Pacheco@Sun.COM continue; 27156689Smaybee (void) strcpy(zc->zc_value, nvpair_name(prop)); 27166689Smaybee if (zfs_secpolicy_inherit(zc, CRED()) == 0) 27176689Smaybee (void) zfs_ioc_inherit_prop(zc); 27186689Smaybee } 27196689Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 27206689Smaybee } 27216689Smaybee 27225367Sahrens /* 27235367Sahrens * inputs: 27245367Sahrens * zc_name name of containing filesystem 27255367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 27265367Sahrens * zc_value name of snapshot to create 27275367Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 27285367Sahrens * zc_cookie file descriptor to recv from 27295367Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 27305367Sahrens * zc_guid force flag 27315367Sahrens * 27325367Sahrens * outputs: 27335367Sahrens * zc_cookie number of bytes read 27345367Sahrens */ 2735789Sahrens static int 27365367Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 2737789Sahrens { 2738789Sahrens file_t *fp; 27395326Sek110237 objset_t *os; 27405367Sahrens dmu_recv_cookie_t drc; 27415326Sek110237 boolean_t force = (boolean_t)zc->zc_guid; 2742789Sahrens int error, fd; 27435367Sahrens offset_t off; 27445367Sahrens nvlist_t *props = NULL; 27456689Smaybee nvlist_t *origprops = NULL; 27465367Sahrens objset_t *origin = NULL; 27475367Sahrens char *tosnap; 27485367Sahrens char tofs[ZFS_MAXNAMELEN]; 2749789Sahrens 27503265Sahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 27515326Sek110237 strchr(zc->zc_value, '@') == NULL || 27525326Sek110237 strchr(zc->zc_value, '%')) 27533265Sahrens return (EINVAL); 27543265Sahrens 27555367Sahrens (void) strcpy(tofs, zc->zc_value); 27565367Sahrens tosnap = strchr(tofs, '@'); 27575367Sahrens *tosnap = '\0'; 27585367Sahrens tosnap++; 27595367Sahrens 27605367Sahrens if (zc->zc_nvlist_src != NULL && 27615367Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 27629643SEric.Taylor@Sun.COM zc->zc_iflags, &props)) != 0) 27635367Sahrens return (error); 27645367Sahrens 2765789Sahrens fd = zc->zc_cookie; 2766789Sahrens fp = getf(fd); 27675367Sahrens if (fp == NULL) { 27685367Sahrens nvlist_free(props); 2769789Sahrens return (EBADF); 27705367Sahrens } 27715326Sek110237 277210298SMatthew.Ahrens@Sun.COM if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) { 27736689Smaybee /* 27746689Smaybee * If new properties are supplied, they are to completely 27756689Smaybee * replace the existing ones, so stash away the existing ones. 27766689Smaybee */ 277710298SMatthew.Ahrens@Sun.COM (void) dsl_prop_get_all(os, &origprops, B_TRUE); 277810298SMatthew.Ahrens@Sun.COM 277910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 27805326Sek110237 } 27815326Sek110237 27825367Sahrens if (zc->zc_string[0]) { 278310298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_string, FTAG, &origin); 27846689Smaybee if (error) 27856689Smaybee goto out; 27865367Sahrens } 27875367Sahrens 27885367Sahrens error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record, 278910204SMatthew.Ahrens@Sun.COM force, origin, &drc); 27905367Sahrens if (origin) 279110298SMatthew.Ahrens@Sun.COM dmu_objset_rele(origin, FTAG); 27926689Smaybee if (error) 27936689Smaybee goto out; 27945326Sek110237 27955326Sek110237 /* 27966689Smaybee * Reset properties. We do this before we receive the stream 27976689Smaybee * so that the properties are applied to the new data. 27985326Sek110237 */ 27995367Sahrens if (props) { 28008536SDavid.Pacheco@Sun.COM clear_props(tofs, origprops, props); 28016689Smaybee /* 28026689Smaybee * XXX - Note, this is all-or-nothing; should be best-effort. 28036689Smaybee */ 28046689Smaybee (void) zfs_set_prop_nvlist(tofs, props); 28055367Sahrens } 28065367Sahrens 28075367Sahrens off = fp->f_offset; 28085367Sahrens error = dmu_recv_stream(&drc, fp->f_vnode, &off); 28095367Sahrens 281010204SMatthew.Ahrens@Sun.COM if (error == 0) { 281110204SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs = NULL; 281210204SMatthew.Ahrens@Sun.COM 281310204SMatthew.Ahrens@Sun.COM if (getzfsvfs(tofs, &zfsvfs) == 0) { 281410204SMatthew.Ahrens@Sun.COM /* online recv */ 281510204SMatthew.Ahrens@Sun.COM int end_err; 281610298SMatthew.Ahrens@Sun.COM 281710298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 281810204SMatthew.Ahrens@Sun.COM /* 281910204SMatthew.Ahrens@Sun.COM * If the suspend fails, then the recv_end will 282010204SMatthew.Ahrens@Sun.COM * likely also fail, and clean up after itself. 282110204SMatthew.Ahrens@Sun.COM */ 282210204SMatthew.Ahrens@Sun.COM end_err = dmu_recv_end(&drc); 282310204SMatthew.Ahrens@Sun.COM if (error == 0) { 282410204SMatthew.Ahrens@Sun.COM int resume_err = 282510298SMatthew.Ahrens@Sun.COM zfs_resume_fs(zfsvfs, tofs); 282610204SMatthew.Ahrens@Sun.COM error = error ? error : resume_err; 282710204SMatthew.Ahrens@Sun.COM } 282810204SMatthew.Ahrens@Sun.COM error = error ? error : end_err; 282910204SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 283010204SMatthew.Ahrens@Sun.COM } else { 28316689Smaybee error = dmu_recv_end(&drc); 28325367Sahrens } 28336083Sek110237 } 28345367Sahrens 28355367Sahrens zc->zc_cookie = off - fp->f_offset; 28365367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 28375367Sahrens fp->f_offset = off; 28382885Sahrens 28396689Smaybee /* 28406689Smaybee * On error, restore the original props. 28416689Smaybee */ 28426689Smaybee if (error && props) { 28438536SDavid.Pacheco@Sun.COM clear_props(tofs, props, NULL); 28446689Smaybee (void) zfs_set_prop_nvlist(tofs, origprops); 28456689Smaybee } 28466689Smaybee out: 28476689Smaybee nvlist_free(props); 28486689Smaybee nvlist_free(origprops); 2849789Sahrens releasef(fd); 2850789Sahrens return (error); 2851789Sahrens } 2852789Sahrens 28535367Sahrens /* 28545367Sahrens * inputs: 28555367Sahrens * zc_name name of snapshot to send 28565367Sahrens * zc_value short name of incremental fromsnap (may be empty) 28575367Sahrens * zc_cookie file descriptor to send stream to 28585367Sahrens * zc_obj fromorigin flag (mutually exclusive with zc_value) 28595367Sahrens * 28605367Sahrens * outputs: none 28615367Sahrens */ 2862789Sahrens static int 28635367Sahrens zfs_ioc_send(zfs_cmd_t *zc) 2864789Sahrens { 2865789Sahrens objset_t *fromsnap = NULL; 2866789Sahrens objset_t *tosnap; 2867789Sahrens file_t *fp; 2868789Sahrens int error; 28695367Sahrens offset_t off; 2870789Sahrens 287110298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap); 2872789Sahrens if (error) 2873789Sahrens return (error); 2874789Sahrens 28752676Seschrock if (zc->zc_value[0] != '\0') { 28768012SEric.Taylor@Sun.COM char *buf; 28772885Sahrens char *cp; 28782885Sahrens 28798012SEric.Taylor@Sun.COM buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 28808012SEric.Taylor@Sun.COM (void) strncpy(buf, zc->zc_name, MAXPATHLEN); 28812885Sahrens cp = strchr(buf, '@'); 28822885Sahrens if (cp) 28832885Sahrens *(cp+1) = 0; 28848012SEric.Taylor@Sun.COM (void) strncat(buf, zc->zc_value, MAXPATHLEN); 288510298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(buf, FTAG, &fromsnap); 28868012SEric.Taylor@Sun.COM kmem_free(buf, MAXPATHLEN); 2887789Sahrens if (error) { 288810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 2889789Sahrens return (error); 2890789Sahrens } 2891789Sahrens } 2892789Sahrens 2893789Sahrens fp = getf(zc->zc_cookie); 2894789Sahrens if (fp == NULL) { 289510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 2896789Sahrens if (fromsnap) 289710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 2898789Sahrens return (EBADF); 2899789Sahrens } 2900789Sahrens 29015367Sahrens off = fp->f_offset; 29025367Sahrens error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 29035367Sahrens 29045367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 29055367Sahrens fp->f_offset = off; 2906789Sahrens releasef(zc->zc_cookie); 2907789Sahrens if (fromsnap) 290810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 290910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 2910789Sahrens return (error); 2911789Sahrens } 2912789Sahrens 29131544Seschrock static int 29141544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 29151544Seschrock { 29161544Seschrock int id, error; 29171544Seschrock 29181544Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 29191544Seschrock &zc->zc_inject_record); 29201544Seschrock 29211544Seschrock if (error == 0) 29221544Seschrock zc->zc_guid = (uint64_t)id; 29231544Seschrock 29241544Seschrock return (error); 29251544Seschrock } 29261544Seschrock 29271544Seschrock static int 29281544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 29291544Seschrock { 29301544Seschrock return (zio_clear_fault((int)zc->zc_guid)); 29311544Seschrock } 29321544Seschrock 29331544Seschrock static int 29341544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 29351544Seschrock { 29361544Seschrock int id = (int)zc->zc_guid; 29371544Seschrock int error; 29381544Seschrock 29391544Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 29401544Seschrock &zc->zc_inject_record); 29411544Seschrock 29421544Seschrock zc->zc_guid = id; 29431544Seschrock 29441544Seschrock return (error); 29451544Seschrock } 29461544Seschrock 29471544Seschrock static int 29481544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 29491544Seschrock { 29501544Seschrock spa_t *spa; 29511544Seschrock int error; 29522676Seschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 29531544Seschrock 29541544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 29551544Seschrock return (error); 29561544Seschrock 29572676Seschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 29581544Seschrock &count); 29591544Seschrock if (error == 0) 29602676Seschrock zc->zc_nvlist_dst_size = count; 29611544Seschrock else 29622676Seschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 29631544Seschrock 29641544Seschrock spa_close(spa, FTAG); 29651544Seschrock 29661544Seschrock return (error); 29671544Seschrock } 29681544Seschrock 29691544Seschrock static int 29701544Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 29711544Seschrock { 29721544Seschrock spa_t *spa; 29731544Seschrock vdev_t *vd; 29741544Seschrock int error; 29751544Seschrock 29767294Sperrin /* 29777294Sperrin * On zpool clear we also fix up missing slogs 29787294Sperrin */ 29797294Sperrin mutex_enter(&spa_namespace_lock); 29807294Sperrin spa = spa_lookup(zc->zc_name); 29817294Sperrin if (spa == NULL) { 29827294Sperrin mutex_exit(&spa_namespace_lock); 29837294Sperrin return (EIO); 29847294Sperrin } 29857294Sperrin if (spa->spa_log_state == SPA_LOG_MISSING) { 29867294Sperrin /* we need to let spa_open/spa_load clear the chains */ 29877294Sperrin spa->spa_log_state = SPA_LOG_CLEAR; 29887294Sperrin } 29897294Sperrin mutex_exit(&spa_namespace_lock); 29907294Sperrin 29911544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 29921544Seschrock return (error); 29931544Seschrock 299410685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 29951544Seschrock 29962676Seschrock if (zc->zc_guid == 0) { 29971544Seschrock vd = NULL; 29986643Seschrock } else { 29996643Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 30005450Sbrendan if (vd == NULL) { 30017754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, ENODEV); 30025450Sbrendan spa_close(spa, FTAG); 30035450Sbrendan return (ENODEV); 30045450Sbrendan } 30051544Seschrock } 30061544Seschrock 30077754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 30087754SJeff.Bonwick@Sun.COM 30097754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 30107754SJeff.Bonwick@Sun.COM 30117754SJeff.Bonwick@Sun.COM /* 30127754SJeff.Bonwick@Sun.COM * Resume any suspended I/Os. 30137754SJeff.Bonwick@Sun.COM */ 30149234SGeorge.Wilson@Sun.COM if (zio_resume(spa) != 0) 30159234SGeorge.Wilson@Sun.COM error = EIO; 30161544Seschrock 30171544Seschrock spa_close(spa, FTAG); 30181544Seschrock 30199234SGeorge.Wilson@Sun.COM return (error); 30201544Seschrock } 30211544Seschrock 30225367Sahrens /* 30235367Sahrens * inputs: 30245367Sahrens * zc_name name of filesystem 30255367Sahrens * zc_value name of origin snapshot 30265367Sahrens * 302710588SEric.Taylor@Sun.COM * outputs: 302810588SEric.Taylor@Sun.COM * zc_string name of conflicting snapshot, if there is one 30295367Sahrens */ 30301544Seschrock static int 30312082Seschrock zfs_ioc_promote(zfs_cmd_t *zc) 30322082Seschrock { 30332417Sahrens char *cp; 30342417Sahrens 30352417Sahrens /* 30362417Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 30372417Sahrens * it's easier. 30382417Sahrens */ 30392676Seschrock cp = strchr(zc->zc_value, '@'); 30402417Sahrens if (cp) 30412417Sahrens *cp = '\0'; 30422676Seschrock (void) dmu_objset_find(zc->zc_value, 30432417Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 304410588SEric.Taylor@Sun.COM return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 30452082Seschrock } 30462082Seschrock 30474543Smarks /* 30489396SMatthew.Ahrens@Sun.COM * Retrieve a single {user|group}{used|quota}@... property. 30499396SMatthew.Ahrens@Sun.COM * 30509396SMatthew.Ahrens@Sun.COM * inputs: 30519396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 30529396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 30539396SMatthew.Ahrens@Sun.COM * zc_value domain name (eg. "S-1-234-567-89") 30549396SMatthew.Ahrens@Sun.COM * zc_guid RID/UID/GID 30559396SMatthew.Ahrens@Sun.COM * 30569396SMatthew.Ahrens@Sun.COM * outputs: 30579396SMatthew.Ahrens@Sun.COM * zc_cookie property value 30589396SMatthew.Ahrens@Sun.COM */ 30599396SMatthew.Ahrens@Sun.COM static int 30609396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc) 30619396SMatthew.Ahrens@Sun.COM { 30629396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 30639396SMatthew.Ahrens@Sun.COM int error; 30649396SMatthew.Ahrens@Sun.COM 30659396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 30669396SMatthew.Ahrens@Sun.COM return (EINVAL); 30679396SMatthew.Ahrens@Sun.COM 306810298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 30699396SMatthew.Ahrens@Sun.COM if (error) 30709396SMatthew.Ahrens@Sun.COM return (error); 30719396SMatthew.Ahrens@Sun.COM 30729396SMatthew.Ahrens@Sun.COM error = zfs_userspace_one(zfsvfs, 30739396SMatthew.Ahrens@Sun.COM zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 30749396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 30759396SMatthew.Ahrens@Sun.COM 30769396SMatthew.Ahrens@Sun.COM return (error); 30779396SMatthew.Ahrens@Sun.COM } 30789396SMatthew.Ahrens@Sun.COM 30799396SMatthew.Ahrens@Sun.COM /* 30809396SMatthew.Ahrens@Sun.COM * inputs: 30819396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 30829396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 30839396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 30849396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 30859396SMatthew.Ahrens@Sun.COM * 30869396SMatthew.Ahrens@Sun.COM * outputs: 30879396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 30889396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 30899396SMatthew.Ahrens@Sun.COM */ 30909396SMatthew.Ahrens@Sun.COM static int 30919396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc) 30929396SMatthew.Ahrens@Sun.COM { 30939396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 30949396SMatthew.Ahrens@Sun.COM int error; 30959396SMatthew.Ahrens@Sun.COM 309610298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 30979396SMatthew.Ahrens@Sun.COM if (error) 30989396SMatthew.Ahrens@Sun.COM return (error); 30999396SMatthew.Ahrens@Sun.COM 31009396SMatthew.Ahrens@Sun.COM int bufsize = zc->zc_nvlist_dst_size; 31019396SMatthew.Ahrens@Sun.COM void *buf = kmem_alloc(bufsize, KM_SLEEP); 31029396SMatthew.Ahrens@Sun.COM 31039396SMatthew.Ahrens@Sun.COM error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 31049396SMatthew.Ahrens@Sun.COM buf, &zc->zc_nvlist_dst_size); 31059396SMatthew.Ahrens@Sun.COM 31069396SMatthew.Ahrens@Sun.COM if (error == 0) { 31079396SMatthew.Ahrens@Sun.COM error = xcopyout(buf, 31089396SMatthew.Ahrens@Sun.COM (void *)(uintptr_t)zc->zc_nvlist_dst, 31099396SMatthew.Ahrens@Sun.COM zc->zc_nvlist_dst_size); 31109396SMatthew.Ahrens@Sun.COM } 31119396SMatthew.Ahrens@Sun.COM kmem_free(buf, bufsize); 31129396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 31139396SMatthew.Ahrens@Sun.COM 31149396SMatthew.Ahrens@Sun.COM return (error); 31159396SMatthew.Ahrens@Sun.COM } 31169396SMatthew.Ahrens@Sun.COM 31179396SMatthew.Ahrens@Sun.COM /* 31189396SMatthew.Ahrens@Sun.COM * inputs: 31199396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 31209396SMatthew.Ahrens@Sun.COM * 31219396SMatthew.Ahrens@Sun.COM * outputs: 31229396SMatthew.Ahrens@Sun.COM * none 31239396SMatthew.Ahrens@Sun.COM */ 31249396SMatthew.Ahrens@Sun.COM static int 31259396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 31269396SMatthew.Ahrens@Sun.COM { 31279396SMatthew.Ahrens@Sun.COM objset_t *os; 31289396SMatthew.Ahrens@Sun.COM int error; 31299396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 31309396SMatthew.Ahrens@Sun.COM 31319396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 313210298SMatthew.Ahrens@Sun.COM if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 31339396SMatthew.Ahrens@Sun.COM /* 31349396SMatthew.Ahrens@Sun.COM * If userused is not enabled, it may be because the 31359396SMatthew.Ahrens@Sun.COM * objset needs to be closed & reopened (to grow the 31369396SMatthew.Ahrens@Sun.COM * objset_phys_t). Suspend/resume the fs will do that. 31379396SMatthew.Ahrens@Sun.COM */ 313810298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 313910298SMatthew.Ahrens@Sun.COM if (error == 0) 314010298SMatthew.Ahrens@Sun.COM error = zfs_resume_fs(zfsvfs, zc->zc_name); 31419396SMatthew.Ahrens@Sun.COM } 31429396SMatthew.Ahrens@Sun.COM if (error == 0) 31439396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 31449396SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 31459396SMatthew.Ahrens@Sun.COM } else { 314610298SMatthew.Ahrens@Sun.COM /* XXX kind of reading contents without owning */ 314710298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 31489396SMatthew.Ahrens@Sun.COM if (error) 31499396SMatthew.Ahrens@Sun.COM return (error); 31509396SMatthew.Ahrens@Sun.COM 31519396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(os); 315210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 31539396SMatthew.Ahrens@Sun.COM } 31549396SMatthew.Ahrens@Sun.COM 31559396SMatthew.Ahrens@Sun.COM return (error); 31569396SMatthew.Ahrens@Sun.COM } 31579396SMatthew.Ahrens@Sun.COM 31589396SMatthew.Ahrens@Sun.COM /* 31594543Smarks * We don't want to have a hard dependency 31604543Smarks * against some special symbols in sharefs 31615331Samw * nfs, and smbsrv. Determine them if needed when 31624543Smarks * the first file system is shared. 31635331Samw * Neither sharefs, nfs or smbsrv are unloadable modules. 31644543Smarks */ 31655331Samw int (*znfsexport_fs)(void *arg); 31664543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 31675331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 31685331Samw 31695331Samw int zfs_nfsshare_inited; 31705331Samw int zfs_smbshare_inited; 31715331Samw 31724543Smarks ddi_modhandle_t nfs_mod; 31734543Smarks ddi_modhandle_t sharefs_mod; 31745331Samw ddi_modhandle_t smbsrv_mod; 31754543Smarks kmutex_t zfs_share_lock; 31764543Smarks 31774543Smarks static int 31785331Samw zfs_init_sharefs() 31795331Samw { 31805331Samw int error; 31815331Samw 31825331Samw ASSERT(MUTEX_HELD(&zfs_share_lock)); 31835331Samw /* Both NFS and SMB shares also require sharetab support. */ 31845331Samw if (sharefs_mod == NULL && ((sharefs_mod = 31855331Samw ddi_modopen("fs/sharefs", 31865331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 31875331Samw return (ENOSYS); 31885331Samw } 31895331Samw if (zshare_fs == NULL && ((zshare_fs = 31905331Samw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 31915331Samw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 31925331Samw return (ENOSYS); 31935331Samw } 31945331Samw return (0); 31955331Samw } 31965331Samw 31975331Samw static int 31984543Smarks zfs_ioc_share(zfs_cmd_t *zc) 31994543Smarks { 32004543Smarks int error; 32014543Smarks int opcode; 32024543Smarks 32035331Samw switch (zc->zc_share.z_sharetype) { 32045331Samw case ZFS_SHARE_NFS: 32055331Samw case ZFS_UNSHARE_NFS: 32065331Samw if (zfs_nfsshare_inited == 0) { 32075331Samw mutex_enter(&zfs_share_lock); 32085331Samw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 32095331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 32105331Samw mutex_exit(&zfs_share_lock); 32115331Samw return (ENOSYS); 32125331Samw } 32135331Samw if (znfsexport_fs == NULL && 32145331Samw ((znfsexport_fs = (int (*)(void *)) 32155331Samw ddi_modsym(nfs_mod, 32165331Samw "nfs_export", &error)) == NULL)) { 32175331Samw mutex_exit(&zfs_share_lock); 32185331Samw return (ENOSYS); 32195331Samw } 32205331Samw error = zfs_init_sharefs(); 32215331Samw if (error) { 32225331Samw mutex_exit(&zfs_share_lock); 32235331Samw return (ENOSYS); 32245331Samw } 32255331Samw zfs_nfsshare_inited = 1; 32264543Smarks mutex_exit(&zfs_share_lock); 32274543Smarks } 32285331Samw break; 32295331Samw case ZFS_SHARE_SMB: 32305331Samw case ZFS_UNSHARE_SMB: 32315331Samw if (zfs_smbshare_inited == 0) { 32325331Samw mutex_enter(&zfs_share_lock); 32335331Samw if (smbsrv_mod == NULL && ((smbsrv_mod = 32345331Samw ddi_modopen("drv/smbsrv", 32355331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 32365331Samw mutex_exit(&zfs_share_lock); 32375331Samw return (ENOSYS); 32385331Samw } 32395331Samw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 32405331Samw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 32416139Sjb150015 "smb_server_share", &error)) == NULL)) { 32425331Samw mutex_exit(&zfs_share_lock); 32435331Samw return (ENOSYS); 32445331Samw } 32455331Samw error = zfs_init_sharefs(); 32465331Samw if (error) { 32475331Samw mutex_exit(&zfs_share_lock); 32485331Samw return (ENOSYS); 32495331Samw } 32505331Samw zfs_smbshare_inited = 1; 32514543Smarks mutex_exit(&zfs_share_lock); 32524543Smarks } 32535331Samw break; 32545331Samw default: 32555331Samw return (EINVAL); 32564543Smarks } 32574543Smarks 32585331Samw switch (zc->zc_share.z_sharetype) { 32595331Samw case ZFS_SHARE_NFS: 32605331Samw case ZFS_UNSHARE_NFS: 32615331Samw if (error = 32625331Samw znfsexport_fs((void *) 32635331Samw (uintptr_t)zc->zc_share.z_exportdata)) 32645331Samw return (error); 32655331Samw break; 32665331Samw case ZFS_SHARE_SMB: 32675331Samw case ZFS_UNSHARE_SMB: 32685331Samw if (error = zsmbexport_fs((void *) 32695331Samw (uintptr_t)zc->zc_share.z_exportdata, 32705331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 32718845Samw@Sun.COM B_TRUE: B_FALSE)) { 32725331Samw return (error); 32735331Samw } 32745331Samw break; 32755331Samw } 32765331Samw 32775331Samw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 32785331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 32794543Smarks SHAREFS_ADD : SHAREFS_REMOVE; 32804543Smarks 32815331Samw /* 32825331Samw * Add or remove share from sharetab 32835331Samw */ 32844543Smarks error = zshare_fs(opcode, 32854543Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 32864543Smarks zc->zc_share.z_sharemax); 32874543Smarks 32884543Smarks return (error); 32894543Smarks 32904543Smarks } 32914543Smarks 32928845Samw@Sun.COM ace_t full_access[] = { 32938845Samw@Sun.COM {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 32948845Samw@Sun.COM }; 32958845Samw@Sun.COM 32968845Samw@Sun.COM /* 32978845Samw@Sun.COM * Remove all ACL files in shares dir 32988845Samw@Sun.COM */ 32998845Samw@Sun.COM static int 33008845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp) 33018845Samw@Sun.COM { 33028845Samw@Sun.COM zap_cursor_t zc; 33038845Samw@Sun.COM zap_attribute_t zap; 33048845Samw@Sun.COM zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 33058845Samw@Sun.COM int error; 33068845Samw@Sun.COM 33078845Samw@Sun.COM for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 33088845Samw@Sun.COM (error = zap_cursor_retrieve(&zc, &zap)) == 0; 33098845Samw@Sun.COM zap_cursor_advance(&zc)) { 33108845Samw@Sun.COM if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 33118845Samw@Sun.COM NULL, 0)) != 0) 33128845Samw@Sun.COM break; 33138845Samw@Sun.COM } 33148845Samw@Sun.COM zap_cursor_fini(&zc); 33158845Samw@Sun.COM return (error); 33168845Samw@Sun.COM } 33178845Samw@Sun.COM 33188845Samw@Sun.COM static int 33198845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc) 33208845Samw@Sun.COM { 33218845Samw@Sun.COM vnode_t *vp; 33228845Samw@Sun.COM znode_t *dzp; 33238845Samw@Sun.COM vnode_t *resourcevp = NULL; 33248845Samw@Sun.COM znode_t *sharedir; 33258845Samw@Sun.COM zfsvfs_t *zfsvfs; 33268845Samw@Sun.COM nvlist_t *nvlist; 33278845Samw@Sun.COM char *src, *target; 33288845Samw@Sun.COM vattr_t vattr; 33298845Samw@Sun.COM vsecattr_t vsec; 33308845Samw@Sun.COM int error = 0; 33318845Samw@Sun.COM 33328845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 33338845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 33348845Samw@Sun.COM return (error); 33358845Samw@Sun.COM 33368845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 33378845Samw@Sun.COM 33388845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 33398845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 33408845Samw@Sun.COM zc->zc_name) != 0)) { 33418845Samw@Sun.COM VN_RELE(vp); 33428845Samw@Sun.COM return (EINVAL); 33438845Samw@Sun.COM } 33448845Samw@Sun.COM 33458845Samw@Sun.COM dzp = VTOZ(vp); 33468845Samw@Sun.COM zfsvfs = dzp->z_zfsvfs; 33478845Samw@Sun.COM ZFS_ENTER(zfsvfs); 33488845Samw@Sun.COM 33499030SMark.Shellenbaum@Sun.COM /* 33509030SMark.Shellenbaum@Sun.COM * Create share dir if its missing. 33519030SMark.Shellenbaum@Sun.COM */ 33529030SMark.Shellenbaum@Sun.COM mutex_enter(&zfsvfs->z_lock); 33539030SMark.Shellenbaum@Sun.COM if (zfsvfs->z_shares_dir == 0) { 33549030SMark.Shellenbaum@Sun.COM dmu_tx_t *tx; 33559030SMark.Shellenbaum@Sun.COM 33569030SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(zfsvfs->z_os); 33579030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 33589030SMark.Shellenbaum@Sun.COM ZFS_SHARES_DIR); 33599030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 33609030SMark.Shellenbaum@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 33619030SMark.Shellenbaum@Sun.COM if (error) { 33629030SMark.Shellenbaum@Sun.COM dmu_tx_abort(tx); 33639030SMark.Shellenbaum@Sun.COM } else { 33649030SMark.Shellenbaum@Sun.COM error = zfs_create_share_dir(zfsvfs, tx); 33659030SMark.Shellenbaum@Sun.COM dmu_tx_commit(tx); 33669030SMark.Shellenbaum@Sun.COM } 33679030SMark.Shellenbaum@Sun.COM if (error) { 33689030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 33699030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 33709030SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 33719030SMark.Shellenbaum@Sun.COM return (error); 33729030SMark.Shellenbaum@Sun.COM } 33739030SMark.Shellenbaum@Sun.COM } 33749030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 33759030SMark.Shellenbaum@Sun.COM 33769030SMark.Shellenbaum@Sun.COM ASSERT(zfsvfs->z_shares_dir); 33778845Samw@Sun.COM if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 33789030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 33798845Samw@Sun.COM ZFS_EXIT(zfsvfs); 33808845Samw@Sun.COM return (error); 33818845Samw@Sun.COM } 33828845Samw@Sun.COM 33838845Samw@Sun.COM switch (zc->zc_cookie) { 33848845Samw@Sun.COM case ZFS_SMB_ACL_ADD: 33858845Samw@Sun.COM vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 33868845Samw@Sun.COM vattr.va_type = VREG; 33878845Samw@Sun.COM vattr.va_mode = S_IFREG|0777; 33888845Samw@Sun.COM vattr.va_uid = 0; 33898845Samw@Sun.COM vattr.va_gid = 0; 33908845Samw@Sun.COM 33918845Samw@Sun.COM vsec.vsa_mask = VSA_ACE; 33928845Samw@Sun.COM vsec.vsa_aclentp = &full_access; 33938845Samw@Sun.COM vsec.vsa_aclentsz = sizeof (full_access); 33948845Samw@Sun.COM vsec.vsa_aclcnt = 1; 33958845Samw@Sun.COM 33968845Samw@Sun.COM error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 33978845Samw@Sun.COM &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 33988845Samw@Sun.COM if (resourcevp) 33998845Samw@Sun.COM VN_RELE(resourcevp); 34008845Samw@Sun.COM break; 34018845Samw@Sun.COM 34028845Samw@Sun.COM case ZFS_SMB_ACL_REMOVE: 34038845Samw@Sun.COM error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 34048845Samw@Sun.COM NULL, 0); 34058845Samw@Sun.COM break; 34068845Samw@Sun.COM 34078845Samw@Sun.COM case ZFS_SMB_ACL_RENAME: 34088845Samw@Sun.COM if ((error = get_nvlist(zc->zc_nvlist_src, 34099643SEric.Taylor@Sun.COM zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 34108845Samw@Sun.COM VN_RELE(vp); 34118845Samw@Sun.COM ZFS_EXIT(zfsvfs); 34128845Samw@Sun.COM return (error); 34138845Samw@Sun.COM } 34148845Samw@Sun.COM if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 34158845Samw@Sun.COM nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 34168845Samw@Sun.COM &target)) { 34178845Samw@Sun.COM VN_RELE(vp); 34189179SMark.Shellenbaum@Sun.COM VN_RELE(ZTOV(sharedir)); 34198845Samw@Sun.COM ZFS_EXIT(zfsvfs); 34208845Samw@Sun.COM return (error); 34218845Samw@Sun.COM } 34228845Samw@Sun.COM error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 34238845Samw@Sun.COM kcred, NULL, 0); 34248845Samw@Sun.COM nvlist_free(nvlist); 34258845Samw@Sun.COM break; 34268845Samw@Sun.COM 34278845Samw@Sun.COM case ZFS_SMB_ACL_PURGE: 34288845Samw@Sun.COM error = zfs_smb_acl_purge(sharedir); 34298845Samw@Sun.COM break; 34308845Samw@Sun.COM 34318845Samw@Sun.COM default: 34328845Samw@Sun.COM error = EINVAL; 34338845Samw@Sun.COM break; 34348845Samw@Sun.COM } 34358845Samw@Sun.COM 34368845Samw@Sun.COM VN_RELE(vp); 34378845Samw@Sun.COM VN_RELE(ZTOV(sharedir)); 34388845Samw@Sun.COM 34398845Samw@Sun.COM ZFS_EXIT(zfsvfs); 34408845Samw@Sun.COM 34418845Samw@Sun.COM return (error); 34428845Samw@Sun.COM } 34438845Samw@Sun.COM 34444543Smarks /* 344510242Schris.kirby@sun.com * inputs: 344610242Schris.kirby@sun.com * zc_name name of filesystem 344710242Schris.kirby@sun.com * zc_value short name of snap 344810242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 344910242Schris.kirby@sun.com * zc_cookie recursive flag 345010342Schris.kirby@sun.com * zc_temphold set if hold is temporary 345110242Schris.kirby@sun.com * 345210242Schris.kirby@sun.com * outputs: none 345310242Schris.kirby@sun.com */ 345410242Schris.kirby@sun.com static int 345510242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc) 345610242Schris.kirby@sun.com { 345710242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 345810242Schris.kirby@sun.com 345910242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 346010242Schris.kirby@sun.com return (EINVAL); 346110242Schris.kirby@sun.com 346210242Schris.kirby@sun.com return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value, 346310342Schris.kirby@sun.com zc->zc_string, recursive, zc->zc_temphold)); 346410242Schris.kirby@sun.com } 346510242Schris.kirby@sun.com 346610242Schris.kirby@sun.com /* 346710242Schris.kirby@sun.com * inputs: 346810242Schris.kirby@sun.com * zc_name name of dataset from which we're releasing a user reference 346910242Schris.kirby@sun.com * zc_value short name of snap 347010242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 347110242Schris.kirby@sun.com * zc_cookie recursive flag 347210242Schris.kirby@sun.com * 347310242Schris.kirby@sun.com * outputs: none 347410242Schris.kirby@sun.com */ 347510242Schris.kirby@sun.com static int 347610242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc) 347710242Schris.kirby@sun.com { 347810242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 347910242Schris.kirby@sun.com 348010242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 348110242Schris.kirby@sun.com return (EINVAL); 348210242Schris.kirby@sun.com 348310242Schris.kirby@sun.com return (dsl_dataset_user_release(zc->zc_name, zc->zc_value, 348410242Schris.kirby@sun.com zc->zc_string, recursive)); 348510242Schris.kirby@sun.com } 348610242Schris.kirby@sun.com 348710242Schris.kirby@sun.com /* 348810242Schris.kirby@sun.com * inputs: 348910242Schris.kirby@sun.com * zc_name name of filesystem 349010242Schris.kirby@sun.com * 349110242Schris.kirby@sun.com * outputs: 349210242Schris.kirby@sun.com * zc_nvlist_src{_size} nvlist of snapshot holds 349310242Schris.kirby@sun.com */ 349410242Schris.kirby@sun.com static int 349510242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc) 349610242Schris.kirby@sun.com { 349710242Schris.kirby@sun.com nvlist_t *nvp; 349810242Schris.kirby@sun.com int error; 349910242Schris.kirby@sun.com 350010242Schris.kirby@sun.com if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) { 350110242Schris.kirby@sun.com error = put_nvlist(zc, nvp); 350210242Schris.kirby@sun.com nvlist_free(nvp); 350310242Schris.kirby@sun.com } 350410242Schris.kirby@sun.com 350510242Schris.kirby@sun.com return (error); 350610242Schris.kirby@sun.com } 350710242Schris.kirby@sun.com 350810242Schris.kirby@sun.com /* 35094988Sek110237 * pool create, destroy, and export don't log the history as part of 35104988Sek110237 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 35114988Sek110237 * do the logging of those commands. 35124543Smarks */ 3513789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 35149234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35159234SGeorge.Wilson@Sun.COM B_FALSE }, 35169234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35179234SGeorge.Wilson@Sun.COM B_FALSE }, 35189234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35199234SGeorge.Wilson@Sun.COM B_FALSE }, 35209234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35219234SGeorge.Wilson@Sun.COM B_FALSE }, 35229234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE, 35239234SGeorge.Wilson@Sun.COM B_FALSE }, 35249234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE, 35259234SGeorge.Wilson@Sun.COM B_FALSE }, 35269234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE, 35279234SGeorge.Wilson@Sun.COM B_FALSE }, 35289234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35299234SGeorge.Wilson@Sun.COM B_TRUE }, 35309234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE, 35319234SGeorge.Wilson@Sun.COM B_FALSE }, 35329234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35339234SGeorge.Wilson@Sun.COM B_TRUE }, 35349234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35359234SGeorge.Wilson@Sun.COM B_FALSE }, 35369234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35379234SGeorge.Wilson@Sun.COM B_TRUE }, 35389234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35399234SGeorge.Wilson@Sun.COM B_TRUE }, 35409234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35419234SGeorge.Wilson@Sun.COM B_FALSE }, 35429234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35439234SGeorge.Wilson@Sun.COM B_TRUE }, 35449234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35459234SGeorge.Wilson@Sun.COM B_TRUE }, 35469234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35479234SGeorge.Wilson@Sun.COM B_TRUE }, 35489425SEric.Schrock@Sun.COM { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35499425SEric.Schrock@Sun.COM B_TRUE }, 35509234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35519234SGeorge.Wilson@Sun.COM B_FALSE }, 35529234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35539234SGeorge.Wilson@Sun.COM B_FALSE }, 35549234SGeorge.Wilson@Sun.COM { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35559234SGeorge.Wilson@Sun.COM B_FALSE }, 35569234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35579234SGeorge.Wilson@Sun.COM B_FALSE }, 35589234SGeorge.Wilson@Sun.COM { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE }, 35599234SGeorge.Wilson@Sun.COM { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE }, 35609234SGeorge.Wilson@Sun.COM { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 35619234SGeorge.Wilson@Sun.COM B_TRUE}, 35629234SGeorge.Wilson@Sun.COM { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE, 35639234SGeorge.Wilson@Sun.COM B_TRUE }, 35649234SGeorge.Wilson@Sun.COM { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE }, 35659234SGeorge.Wilson@Sun.COM { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE }, 35669234SGeorge.Wilson@Sun.COM { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE }, 35679234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 35689234SGeorge.Wilson@Sun.COM B_FALSE }, 35699234SGeorge.Wilson@Sun.COM { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 35709234SGeorge.Wilson@Sun.COM B_FALSE }, 35719234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE, 35729234SGeorge.Wilson@Sun.COM B_FALSE }, 35739234SGeorge.Wilson@Sun.COM { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE, 35749234SGeorge.Wilson@Sun.COM B_FALSE }, 35759234SGeorge.Wilson@Sun.COM { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE }, 35769234SGeorge.Wilson@Sun.COM { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE, 35779234SGeorge.Wilson@Sun.COM B_TRUE }, 35789234SGeorge.Wilson@Sun.COM { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 35799234SGeorge.Wilson@Sun.COM B_TRUE }, 35809234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE, 35819234SGeorge.Wilson@Sun.COM B_TRUE }, 35829234SGeorge.Wilson@Sun.COM { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35839234SGeorge.Wilson@Sun.COM B_FALSE }, 358410233SGeorge.Wilson@Sun.COM { zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE, 358510233SGeorge.Wilson@Sun.COM B_TRUE }, 35869234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35879234SGeorge.Wilson@Sun.COM B_TRUE }, 35889234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE, 35899234SGeorge.Wilson@Sun.COM B_FALSE }, 35909234SGeorge.Wilson@Sun.COM { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE, 35919234SGeorge.Wilson@Sun.COM B_TRUE }, 35929234SGeorge.Wilson@Sun.COM { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35939234SGeorge.Wilson@Sun.COM B_FALSE }, 35949234SGeorge.Wilson@Sun.COM { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE, 35959234SGeorge.Wilson@Sun.COM B_FALSE }, 35969234SGeorge.Wilson@Sun.COM { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE }, 35979234SGeorge.Wilson@Sun.COM { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE, 35989234SGeorge.Wilson@Sun.COM B_TRUE }, 35999234SGeorge.Wilson@Sun.COM { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE, 36009396SMatthew.Ahrens@Sun.COM B_FALSE }, 36019396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, 36029396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 36039396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, 36049396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 36059396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 36069396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_TRUE }, 360710242Schris.kirby@sun.com { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE }, 360810242Schris.kirby@sun.com { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE, 360910242Schris.kirby@sun.com B_TRUE }, 361010242Schris.kirby@sun.com { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 361110242Schris.kirby@sun.com B_TRUE } 3612789Sahrens }; 3613789Sahrens 36149234SGeorge.Wilson@Sun.COM int 36159234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type) 36169234SGeorge.Wilson@Sun.COM { 36179234SGeorge.Wilson@Sun.COM spa_t *spa; 36189234SGeorge.Wilson@Sun.COM int error; 36199234SGeorge.Wilson@Sun.COM 36209234SGeorge.Wilson@Sun.COM ASSERT(type == POOL_NAME || type == DATASET_NAME); 36219234SGeorge.Wilson@Sun.COM 36229396SMatthew.Ahrens@Sun.COM error = spa_open(name, &spa, FTAG); 36239234SGeorge.Wilson@Sun.COM if (error == 0) { 36249234SGeorge.Wilson@Sun.COM if (spa_suspended(spa)) 36259234SGeorge.Wilson@Sun.COM error = EAGAIN; 36269234SGeorge.Wilson@Sun.COM spa_close(spa, FTAG); 36279234SGeorge.Wilson@Sun.COM } 36289234SGeorge.Wilson@Sun.COM return (error); 36299234SGeorge.Wilson@Sun.COM } 36309234SGeorge.Wilson@Sun.COM 3631789Sahrens static int 3632789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 3633789Sahrens { 3634789Sahrens zfs_cmd_t *zc; 3635789Sahrens uint_t vec; 36362199Sahrens int error, rc; 3637789Sahrens 3638789Sahrens if (getminor(dev) != 0) 3639789Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 3640789Sahrens 3641789Sahrens vec = cmd - ZFS_IOC; 36424787Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 3643789Sahrens 3644789Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 3645789Sahrens return (EINVAL); 3646789Sahrens 3647789Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 3648789Sahrens 36499643SEric.Taylor@Sun.COM error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 3650789Sahrens 365110588SEric.Taylor@Sun.COM if ((error == 0) && !(flag & FKIOCTL)) 36524543Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 3653789Sahrens 3654789Sahrens /* 3655789Sahrens * Ensure that all pool/dataset names are valid before we pass down to 3656789Sahrens * the lower layers. 3657789Sahrens */ 3658789Sahrens if (error == 0) { 3659789Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 36609643SEric.Taylor@Sun.COM zc->zc_iflags = flag & FKIOCTL; 3661789Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 36624577Sahrens case POOL_NAME: 3663789Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 3664789Sahrens error = EINVAL; 36659234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 36669234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 36679234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 3668789Sahrens break; 3669789Sahrens 36704577Sahrens case DATASET_NAME: 3671789Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 3672789Sahrens error = EINVAL; 36739234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 36749234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 36759234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 3676789Sahrens break; 36772856Snd150628 36784577Sahrens case NO_NAME: 36792856Snd150628 break; 3680789Sahrens } 3681789Sahrens } 3682789Sahrens 3683789Sahrens if (error == 0) 3684789Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 3685789Sahrens 36869643SEric.Taylor@Sun.COM rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 36874543Smarks if (error == 0) { 36882199Sahrens error = rc; 36899396SMatthew.Ahrens@Sun.COM if (zfs_ioc_vec[vec].zvec_his_log) 36904543Smarks zfs_log_history(zc); 36914543Smarks } 3692789Sahrens 3693789Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 3694789Sahrens return (error); 3695789Sahrens } 3696789Sahrens 3697789Sahrens static int 3698789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 3699789Sahrens { 3700789Sahrens if (cmd != DDI_ATTACH) 3701789Sahrens return (DDI_FAILURE); 3702789Sahrens 3703789Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 3704789Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 3705789Sahrens return (DDI_FAILURE); 3706789Sahrens 3707789Sahrens zfs_dip = dip; 3708789Sahrens 3709789Sahrens ddi_report_dev(dip); 3710789Sahrens 3711789Sahrens return (DDI_SUCCESS); 3712789Sahrens } 3713789Sahrens 3714789Sahrens static int 3715789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3716789Sahrens { 3717789Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 3718789Sahrens return (DDI_FAILURE); 3719789Sahrens 3720789Sahrens if (cmd != DDI_DETACH) 3721789Sahrens return (DDI_FAILURE); 3722789Sahrens 3723789Sahrens zfs_dip = NULL; 3724789Sahrens 3725789Sahrens ddi_prop_remove_all(dip); 3726789Sahrens ddi_remove_minor_node(dip, NULL); 3727789Sahrens 3728789Sahrens return (DDI_SUCCESS); 3729789Sahrens } 3730789Sahrens 3731789Sahrens /*ARGSUSED*/ 3732789Sahrens static int 3733789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 3734789Sahrens { 3735789Sahrens switch (infocmd) { 3736789Sahrens case DDI_INFO_DEVT2DEVINFO: 3737789Sahrens *result = zfs_dip; 3738789Sahrens return (DDI_SUCCESS); 3739789Sahrens 3740789Sahrens case DDI_INFO_DEVT2INSTANCE: 3741849Sbonwick *result = (void *)0; 3742789Sahrens return (DDI_SUCCESS); 3743789Sahrens } 3744789Sahrens 3745789Sahrens return (DDI_FAILURE); 3746789Sahrens } 3747789Sahrens 3748789Sahrens /* 3749789Sahrens * OK, so this is a little weird. 3750789Sahrens * 3751789Sahrens * /dev/zfs is the control node, i.e. minor 0. 3752789Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 3753789Sahrens * 3754789Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 3755789Sahrens * so most of the standard driver entry points are in zvol.c. 3756789Sahrens */ 3757789Sahrens static struct cb_ops zfs_cb_ops = { 3758789Sahrens zvol_open, /* open */ 3759789Sahrens zvol_close, /* close */ 3760789Sahrens zvol_strategy, /* strategy */ 3761789Sahrens nodev, /* print */ 37626423Sgw25295 zvol_dump, /* dump */ 3763789Sahrens zvol_read, /* read */ 3764789Sahrens zvol_write, /* write */ 3765789Sahrens zfsdev_ioctl, /* ioctl */ 3766789Sahrens nodev, /* devmap */ 3767789Sahrens nodev, /* mmap */ 3768789Sahrens nodev, /* segmap */ 3769789Sahrens nochpoll, /* poll */ 3770789Sahrens ddi_prop_op, /* prop_op */ 3771789Sahrens NULL, /* streamtab */ 3772789Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 3773789Sahrens CB_REV, /* version */ 37743638Sbillm nodev, /* async read */ 37753638Sbillm nodev, /* async write */ 3776789Sahrens }; 3777789Sahrens 3778789Sahrens static struct dev_ops zfs_dev_ops = { 3779789Sahrens DEVO_REV, /* version */ 3780789Sahrens 0, /* refcnt */ 3781789Sahrens zfs_info, /* info */ 3782789Sahrens nulldev, /* identify */ 3783789Sahrens nulldev, /* probe */ 3784789Sahrens zfs_attach, /* attach */ 3785789Sahrens zfs_detach, /* detach */ 3786789Sahrens nodev, /* reset */ 3787789Sahrens &zfs_cb_ops, /* driver operations */ 37887656SSherry.Moore@Sun.COM NULL, /* no bus operations */ 37897656SSherry.Moore@Sun.COM NULL, /* power */ 37907656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */ 3791789Sahrens }; 3792789Sahrens 3793789Sahrens static struct modldrv zfs_modldrv = { 37947656SSherry.Moore@Sun.COM &mod_driverops, 37957656SSherry.Moore@Sun.COM "ZFS storage pool", 37967656SSherry.Moore@Sun.COM &zfs_dev_ops 3797789Sahrens }; 3798789Sahrens 3799789Sahrens static struct modlinkage modlinkage = { 3800789Sahrens MODREV_1, 3801789Sahrens (void *)&zfs_modlfs, 3802789Sahrens (void *)&zfs_modldrv, 3803789Sahrens NULL 3804789Sahrens }; 3805789Sahrens 38064720Sfr157268 38074720Sfr157268 uint_t zfs_fsyncer_key; 38085326Sek110237 extern uint_t rrw_tsd_key; 38094720Sfr157268 3810789Sahrens int 3811789Sahrens _init(void) 3812789Sahrens { 3813789Sahrens int error; 3814789Sahrens 3815849Sbonwick spa_init(FREAD | FWRITE); 3816849Sbonwick zfs_init(); 3817849Sbonwick zvol_init(); 3818849Sbonwick 3819849Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 3820849Sbonwick zvol_fini(); 3821849Sbonwick zfs_fini(); 3822849Sbonwick spa_fini(); 3823789Sahrens return (error); 3824849Sbonwick } 3825789Sahrens 38264720Sfr157268 tsd_create(&zfs_fsyncer_key, NULL); 38275326Sek110237 tsd_create(&rrw_tsd_key, NULL); 38284720Sfr157268 3829789Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 3830789Sahrens ASSERT(error == 0); 38314543Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 3832789Sahrens 3833789Sahrens return (0); 3834789Sahrens } 3835789Sahrens 3836789Sahrens int 3837789Sahrens _fini(void) 3838789Sahrens { 3839789Sahrens int error; 3840789Sahrens 38411544Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 3842789Sahrens return (EBUSY); 3843789Sahrens 3844789Sahrens if ((error = mod_remove(&modlinkage)) != 0) 3845789Sahrens return (error); 3846789Sahrens 3847789Sahrens zvol_fini(); 3848789Sahrens zfs_fini(); 3849789Sahrens spa_fini(); 38505331Samw if (zfs_nfsshare_inited) 38514543Smarks (void) ddi_modclose(nfs_mod); 38525331Samw if (zfs_smbshare_inited) 38535331Samw (void) ddi_modclose(smbsrv_mod); 38545331Samw if (zfs_nfsshare_inited || zfs_smbshare_inited) 38554543Smarks (void) ddi_modclose(sharefs_mod); 3856789Sahrens 38574720Sfr157268 tsd_destroy(&zfs_fsyncer_key); 3858789Sahrens ldi_ident_release(zfs_li); 3859789Sahrens zfs_li = NULL; 38604543Smarks mutex_destroy(&zfs_share_lock); 3861789Sahrens 3862789Sahrens return (error); 3863789Sahrens } 3864789Sahrens 3865789Sahrens int 3866789Sahrens _info(struct modinfo *modinfop) 3867789Sahrens { 3868789Sahrens return (mod_info(&modlinkage, modinfop)); 3869789Sahrens } 3870