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 { 385*10588SEric.Taylor@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 386*10588SEric.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 { 592*10588SEric.Taylor@Sun.COM return (zfs_secpolicy_write_perms(name, 593*10588SEric.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); 945*10588SEric.Taylor@Sun.COM if (error == 0) 946*10588SEric.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); 994*10588SEric.Taylor@Sun.COM if (error == 0) 995*10588SEric.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 { 1250*10588SEric.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); 1257*10588SEric.Taylor@Sun.COM nslock = spa_uses_zvols(spa); 1258*10588SEric.Taylor@Sun.COM if (nslock) 1259*10588SEric.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: 12704451Seschrock error = vdev_fault(spa, zc->zc_guid); 12714451Seschrock break; 1272789Sahrens 12734451Seschrock case VDEV_STATE_DEGRADED: 12744451Seschrock error = vdev_degrade(spa, zc->zc_guid); 12754451Seschrock break; 12764451Seschrock 12774451Seschrock default: 12784451Seschrock error = EINVAL; 12794451Seschrock } 1280*10588SEric.Taylor@Sun.COM if (nslock) 1281*10588SEric.Taylor@Sun.COM mutex_exit(&spa_namespace_lock); 12824451Seschrock zc->zc_cookie = newstate; 1283789Sahrens spa_close(spa, FTAG); 1284789Sahrens return (error); 1285789Sahrens } 1286789Sahrens 1287789Sahrens static int 1288789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1289789Sahrens { 1290789Sahrens spa_t *spa; 1291789Sahrens int replacing = zc->zc_cookie; 1292789Sahrens nvlist_t *config; 1293789Sahrens int error; 1294789Sahrens 12952926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1296789Sahrens return (error); 1297789Sahrens 12985094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 12999643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) == 0) { 13001544Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1301789Sahrens nvlist_free(config); 1302789Sahrens } 1303789Sahrens 1304789Sahrens spa_close(spa, FTAG); 1305789Sahrens return (error); 1306789Sahrens } 1307789Sahrens 1308789Sahrens static int 1309789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1310789Sahrens { 1311789Sahrens spa_t *spa; 1312789Sahrens int error; 1313789Sahrens 13142926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1315789Sahrens return (error); 1316789Sahrens 13178241SJeff.Bonwick@Sun.COM error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1318789Sahrens 1319789Sahrens spa_close(spa, FTAG); 1320789Sahrens return (error); 1321789Sahrens } 1322789Sahrens 1323789Sahrens static int 13241354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 13251354Seschrock { 13261354Seschrock spa_t *spa; 13272676Seschrock char *path = zc->zc_value; 13281544Seschrock uint64_t guid = zc->zc_guid; 13291354Seschrock int error; 13301354Seschrock 13311354Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 13321354Seschrock if (error != 0) 13331354Seschrock return (error); 13341354Seschrock 13351354Seschrock error = spa_vdev_setpath(spa, guid, path); 13361354Seschrock spa_close(spa, FTAG); 13371354Seschrock return (error); 13381354Seschrock } 13391354Seschrock 13409425SEric.Schrock@Sun.COM static int 13419425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 13429425SEric.Schrock@Sun.COM { 13439425SEric.Schrock@Sun.COM spa_t *spa; 13449425SEric.Schrock@Sun.COM char *fru = zc->zc_value; 13459425SEric.Schrock@Sun.COM uint64_t guid = zc->zc_guid; 13469425SEric.Schrock@Sun.COM int error; 13479425SEric.Schrock@Sun.COM 13489425SEric.Schrock@Sun.COM error = spa_open(zc->zc_name, &spa, FTAG); 13499425SEric.Schrock@Sun.COM if (error != 0) 13509425SEric.Schrock@Sun.COM return (error); 13519425SEric.Schrock@Sun.COM 13529425SEric.Schrock@Sun.COM error = spa_vdev_setfru(spa, guid, fru); 13539425SEric.Schrock@Sun.COM spa_close(spa, FTAG); 13549425SEric.Schrock@Sun.COM return (error); 13559425SEric.Schrock@Sun.COM } 13569425SEric.Schrock@Sun.COM 13575367Sahrens /* 13585367Sahrens * inputs: 13595367Sahrens * zc_name name of filesystem 13605367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 13615367Sahrens * 13625367Sahrens * outputs: 13635367Sahrens * zc_objset_stats stats 13645367Sahrens * zc_nvlist_dst property nvlist 13655367Sahrens * zc_nvlist_dst_size size of property nvlist 13665367Sahrens */ 13671354Seschrock static int 1368789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc) 1369789Sahrens { 1370789Sahrens objset_t *os = NULL; 1371789Sahrens int error; 13721356Seschrock nvlist_t *nv; 1373789Sahrens 137410298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1375789Sahrens return (error); 1376789Sahrens 13772885Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1378789Sahrens 13792856Snd150628 if (zc->zc_nvlist_dst != 0 && 13806689Smaybee (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) { 13812885Sahrens dmu_objset_stats(os, nv); 13823087Sahrens /* 13835147Srm160521 * NB: zvol_get_stats() will read the objset contents, 13843087Sahrens * which we aren't supposed to do with a 13856689Smaybee * DS_MODE_USER hold, because it could be 13863087Sahrens * inconsistent. So this is a bit of a workaround... 138710298SMatthew.Ahrens@Sun.COM * XXX reading with out owning 13883087Sahrens */ 13894577Sahrens if (!zc->zc_objset_stats.dds_inconsistent) { 13904577Sahrens if (dmu_objset_type(os) == DMU_OST_ZVOL) 13914577Sahrens VERIFY(zvol_get_stats(os, nv) == 0); 13924577Sahrens } 13932676Seschrock error = put_nvlist(zc, nv); 13941356Seschrock nvlist_free(nv); 13951356Seschrock } 1396789Sahrens 139710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1398789Sahrens return (error); 1399789Sahrens } 1400789Sahrens 14015498Stimh static int 14025498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 14035498Stimh { 14045498Stimh uint64_t value; 14055498Stimh int error; 14065498Stimh 14075498Stimh /* 14085498Stimh * zfs_get_zplprop() will either find a value or give us 14095498Stimh * the default value (if there is one). 14105498Stimh */ 14115498Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 14125498Stimh return (error); 14135498Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 14145498Stimh return (0); 14155498Stimh } 14165498Stimh 14175498Stimh /* 14185498Stimh * inputs: 14195498Stimh * zc_name name of filesystem 14205498Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 14215498Stimh * 14225498Stimh * outputs: 14235498Stimh * zc_nvlist_dst zpl property nvlist 14245498Stimh * zc_nvlist_dst_size size of zpl property nvlist 14255498Stimh */ 14265498Stimh static int 14275498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 14285498Stimh { 14295498Stimh objset_t *os; 14305498Stimh int err; 14315498Stimh 143210298SMatthew.Ahrens@Sun.COM /* XXX reading without owning */ 143310298SMatthew.Ahrens@Sun.COM if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 14345498Stimh return (err); 14355498Stimh 14365498Stimh dmu_objset_fast_stat(os, &zc->zc_objset_stats); 14375498Stimh 14385498Stimh /* 14395498Stimh * NB: nvl_add_zplprop() will read the objset contents, 14406689Smaybee * which we aren't supposed to do with a DS_MODE_USER 14416689Smaybee * hold, because it could be inconsistent. 14425498Stimh */ 14435498Stimh if (zc->zc_nvlist_dst != NULL && 14445498Stimh !zc->zc_objset_stats.dds_inconsistent && 14455498Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 14465498Stimh nvlist_t *nv; 14475498Stimh 14485498Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 14495498Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 14505498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 14515498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 14525498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 14535498Stimh err = put_nvlist(zc, nv); 14545498Stimh nvlist_free(nv); 14555498Stimh } else { 14565498Stimh err = ENOENT; 14575498Stimh } 145810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 14595498Stimh return (err); 14605498Stimh } 14615498Stimh 14629396SMatthew.Ahrens@Sun.COM static boolean_t 14639396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name) 14649396SMatthew.Ahrens@Sun.COM { 14659396SMatthew.Ahrens@Sun.COM /* 14669396SMatthew.Ahrens@Sun.COM * Skip over datasets that are not visible in this zone, 14679396SMatthew.Ahrens@Sun.COM * internal datasets (which have a $ in their name), and 14689396SMatthew.Ahrens@Sun.COM * temporary datasets (which have a % in their name). 14699396SMatthew.Ahrens@Sun.COM */ 14709396SMatthew.Ahrens@Sun.COM if (strchr(name, '$') != NULL) 14719396SMatthew.Ahrens@Sun.COM return (B_TRUE); 14729396SMatthew.Ahrens@Sun.COM if (strchr(name, '%') != NULL) 14739396SMatthew.Ahrens@Sun.COM return (B_TRUE); 14749396SMatthew.Ahrens@Sun.COM if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) 14759396SMatthew.Ahrens@Sun.COM return (B_TRUE); 14769396SMatthew.Ahrens@Sun.COM return (B_FALSE); 14779396SMatthew.Ahrens@Sun.COM } 14789396SMatthew.Ahrens@Sun.COM 14795367Sahrens /* 14805367Sahrens * inputs: 14815367Sahrens * zc_name name of filesystem 14825367Sahrens * zc_cookie zap cursor 14835367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 14845367Sahrens * 14855367Sahrens * outputs: 14865367Sahrens * zc_name name of next filesystem 14879396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 14885367Sahrens * zc_objset_stats stats 14895367Sahrens * zc_nvlist_dst property nvlist 14905367Sahrens * zc_nvlist_dst_size size of property nvlist 14915367Sahrens */ 1492789Sahrens static int 1493789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1494789Sahrens { 1495885Sahrens objset_t *os; 1496789Sahrens int error; 1497789Sahrens char *p; 1498789Sahrens 149910298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 1500885Sahrens if (error == ENOENT) 1501885Sahrens error = ESRCH; 1502885Sahrens return (error); 1503789Sahrens } 1504789Sahrens 1505789Sahrens p = strrchr(zc->zc_name, '/'); 1506789Sahrens if (p == NULL || p[1] != '\0') 1507789Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1508789Sahrens p = zc->zc_name + strlen(zc->zc_name); 1509789Sahrens 15108697SRichard.Morris@Sun.COM /* 15118697SRichard.Morris@Sun.COM * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 15128697SRichard.Morris@Sun.COM * but is not declared void because its called by dmu_objset_find(). 15138697SRichard.Morris@Sun.COM */ 15148415SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) { 15158415SRichard.Morris@Sun.COM uint64_t cookie = 0; 15168415SRichard.Morris@Sun.COM int len = sizeof (zc->zc_name) - (p - zc->zc_name); 15178415SRichard.Morris@Sun.COM 15188415SRichard.Morris@Sun.COM while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) 15198697SRichard.Morris@Sun.COM (void) dmu_objset_prefetch(p, NULL); 15208415SRichard.Morris@Sun.COM } 15218415SRichard.Morris@Sun.COM 1522789Sahrens do { 1523885Sahrens error = dmu_dir_list_next(os, 1524885Sahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 1525885Sahrens NULL, &zc->zc_cookie); 1526789Sahrens if (error == ENOENT) 1527789Sahrens error = ESRCH; 1528*10588SEric.Taylor@Sun.COM } while (error == 0 && dataset_name_hidden(zc->zc_name) && 1529*10588SEric.Taylor@Sun.COM !(zc->zc_iflags & FKIOCTL)); 153010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1531789Sahrens 1532*10588SEric.Taylor@Sun.COM /* 1533*10588SEric.Taylor@Sun.COM * If it's an internal dataset (ie. with a '$' in its name), 1534*10588SEric.Taylor@Sun.COM * don't try to get stats for it, otherwise we'll return ENOENT. 1535*10588SEric.Taylor@Sun.COM */ 1536*10588SEric.Taylor@Sun.COM if (error == 0 && strchr(zc->zc_name, '$') == NULL) 1537885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1538789Sahrens return (error); 1539789Sahrens } 1540789Sahrens 15415367Sahrens /* 15425367Sahrens * inputs: 15435367Sahrens * zc_name name of filesystem 15445367Sahrens * zc_cookie zap cursor 15455367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 15465367Sahrens * 15475367Sahrens * outputs: 15485367Sahrens * zc_name name of next snapshot 15495367Sahrens * zc_objset_stats stats 15505367Sahrens * zc_nvlist_dst property nvlist 15515367Sahrens * zc_nvlist_dst_size size of property nvlist 15525367Sahrens */ 1553789Sahrens static int 1554789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1555789Sahrens { 1556885Sahrens objset_t *os; 1557789Sahrens int error; 1558789Sahrens 155910474SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) 156010474SRichard.Morris@Sun.COM (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 156110474SRichard.Morris@Sun.COM NULL, DS_FIND_SNAPSHOTS); 156210474SRichard.Morris@Sun.COM 156310298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 15646689Smaybee if (error) 15656689Smaybee return (error == ENOENT ? ESRCH : error); 1566789Sahrens 15671003Slling /* 15681003Slling * A dataset name of maximum length cannot have any snapshots, 15691003Slling * so exit immediately. 15701003Slling */ 15711003Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 157210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 15731003Slling return (ESRCH); 1574789Sahrens } 1575789Sahrens 1576885Sahrens error = dmu_snapshot_list_next(os, 1577885Sahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 15785663Sck153898 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 157910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1580885Sahrens if (error == 0) 1581885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 15826689Smaybee else if (error == ENOENT) 15836689Smaybee error = ESRCH; 1584789Sahrens 15855367Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 15866689Smaybee if (error) 15875367Sahrens *strchr(zc->zc_name, '@') = '\0'; 1588789Sahrens return (error); 1589789Sahrens } 1590789Sahrens 15916423Sgw25295 int 15924787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl) 1593789Sahrens { 15942676Seschrock nvpair_t *elem; 15958724SRichard.Morris@Sun.COM int error = 0; 15962676Seschrock uint64_t intval; 15972676Seschrock char *strval; 15988697SRichard.Morris@Sun.COM nvlist_t *genericnvl; 15999396SMatthew.Ahrens@Sun.COM boolean_t issnap = (strchr(name, '@') != NULL); 16002676Seschrock 16014543Smarks /* 16024543Smarks * First validate permission to set all of the properties 16034543Smarks */ 16042676Seschrock elem = NULL; 16052676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 16064670Sahrens const char *propname = nvpair_name(elem); 16074670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 16082676Seschrock 16095094Slling if (prop == ZPROP_INVAL) { 16102676Seschrock /* 16112676Seschrock * If this is a user-defined property, it must be a 16122676Seschrock * string, and there is no further validation to do. 16132676Seschrock */ 16149396SMatthew.Ahrens@Sun.COM if (zfs_prop_user(propname) && 16159396SMatthew.Ahrens@Sun.COM nvpair_type(elem) == DATA_TYPE_STRING) { 16169396SMatthew.Ahrens@Sun.COM if (error = zfs_secpolicy_write_perms(name, 16179396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERPROP, CRED())) 16189396SMatthew.Ahrens@Sun.COM return (error); 16199396SMatthew.Ahrens@Sun.COM continue; 16209396SMatthew.Ahrens@Sun.COM } 16219396SMatthew.Ahrens@Sun.COM 16229396SMatthew.Ahrens@Sun.COM if (!issnap && zfs_prop_userquota(propname) && 16239396SMatthew.Ahrens@Sun.COM nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) { 16249396SMatthew.Ahrens@Sun.COM const char *perm; 16259396SMatthew.Ahrens@Sun.COM const char *up = zfs_userquota_prop_prefixes 16269396SMatthew.Ahrens@Sun.COM [ZFS_PROP_USERQUOTA]; 16279396SMatthew.Ahrens@Sun.COM if (strncmp(propname, up, strlen(up)) == 0) 16289396SMatthew.Ahrens@Sun.COM perm = ZFS_DELEG_PERM_USERQUOTA; 16299396SMatthew.Ahrens@Sun.COM else 16309396SMatthew.Ahrens@Sun.COM perm = ZFS_DELEG_PERM_GROUPQUOTA; 16319396SMatthew.Ahrens@Sun.COM if (error = zfs_secpolicy_write_perms(name, 16329396SMatthew.Ahrens@Sun.COM perm, CRED())) 16339396SMatthew.Ahrens@Sun.COM return (error); 16349396SMatthew.Ahrens@Sun.COM continue; 16359396SMatthew.Ahrens@Sun.COM } 16369396SMatthew.Ahrens@Sun.COM 16379396SMatthew.Ahrens@Sun.COM return (EINVAL); 16382676Seschrock } 16392676Seschrock 16409396SMatthew.Ahrens@Sun.COM if (issnap) 16419396SMatthew.Ahrens@Sun.COM return (EINVAL); 16429396SMatthew.Ahrens@Sun.COM 16434787Sahrens if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 16444670Sahrens return (error); 16452676Seschrock 16464670Sahrens /* 16474670Sahrens * Check that this value is valid for this pool version 16484670Sahrens */ 16494670Sahrens switch (prop) { 16503886Sahl case ZFS_PROP_COMPRESSION: 16513886Sahl /* 16523886Sahl * If the user specified gzip compression, make sure 16533886Sahl * the SPA supports it. We ignore any errors here since 16543886Sahl * we'll catch them later. 16553886Sahl */ 16563886Sahl if (nvpair_type(elem) == DATA_TYPE_UINT64 && 16577042Sgw25295 nvpair_value_uint64(elem, &intval) == 0) { 16587042Sgw25295 if (intval >= ZIO_COMPRESS_GZIP_1 && 16597042Sgw25295 intval <= ZIO_COMPRESS_GZIP_9 && 16607184Stimh zfs_earlier_version(name, 16615331Samw SPA_VERSION_GZIP_COMPRESSION)) 16625331Samw return (ENOTSUP); 16637042Sgw25295 16647042Sgw25295 /* 16657042Sgw25295 * If this is a bootable dataset then 16667042Sgw25295 * verify that the compression algorithm 16677042Sgw25295 * is supported for booting. We must return 16687042Sgw25295 * something other than ENOTSUP since it 16697042Sgw25295 * implies a downrev pool version. 16707042Sgw25295 */ 16717042Sgw25295 if (zfs_is_bootfs(name) && 16727042Sgw25295 !BOOTFS_COMPRESS_VALID(intval)) 16737042Sgw25295 return (ERANGE); 16743886Sahl } 16753886Sahl break; 16764603Sahrens 16774603Sahrens case ZFS_PROP_COPIES: 16789396SMatthew.Ahrens@Sun.COM if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS)) 16795331Samw return (ENOTSUP); 16804603Sahrens break; 16815977Smarks 16825977Smarks case ZFS_PROP_SHARESMB: 16836689Smaybee if (zpl_earlier_version(name, ZPL_VERSION_FUID)) 16845977Smarks return (ENOTSUP); 16855977Smarks break; 16868053SMark.Shellenbaum@Sun.COM 16878053SMark.Shellenbaum@Sun.COM case ZFS_PROP_ACLINHERIT: 16888053SMark.Shellenbaum@Sun.COM if (nvpair_type(elem) == DATA_TYPE_UINT64 && 16898053SMark.Shellenbaum@Sun.COM nvpair_value_uint64(elem, &intval) == 0) 16908053SMark.Shellenbaum@Sun.COM if (intval == ZFS_ACL_PASSTHROUGH_X && 16918053SMark.Shellenbaum@Sun.COM zfs_earlier_version(name, 16928053SMark.Shellenbaum@Sun.COM SPA_VERSION_PASSTHROUGH_X)) 16938053SMark.Shellenbaum@Sun.COM return (ENOTSUP); 16944603Sahrens } 16954543Smarks } 16964543Smarks 16978697SRichard.Morris@Sun.COM VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 16984543Smarks elem = NULL; 16994543Smarks while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 17004670Sahrens const char *propname = nvpair_name(elem); 17014670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 17024543Smarks 17035094Slling if (prop == ZPROP_INVAL) { 17049396SMatthew.Ahrens@Sun.COM if (zfs_prop_userquota(propname)) { 17059396SMatthew.Ahrens@Sun.COM uint64_t *valary; 17069396SMatthew.Ahrens@Sun.COM unsigned int vallen; 17079396SMatthew.Ahrens@Sun.COM const char *domain; 17089396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t type; 17099396SMatthew.Ahrens@Sun.COM uint64_t rid; 17109396SMatthew.Ahrens@Sun.COM uint64_t quota; 17119396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 17129396SMatthew.Ahrens@Sun.COM 17139396SMatthew.Ahrens@Sun.COM VERIFY(nvpair_value_uint64_array(elem, 17149396SMatthew.Ahrens@Sun.COM &valary, &vallen) == 0); 17159396SMatthew.Ahrens@Sun.COM VERIFY(vallen == 3); 17169396SMatthew.Ahrens@Sun.COM type = valary[0]; 17179396SMatthew.Ahrens@Sun.COM rid = valary[1]; 17189396SMatthew.Ahrens@Sun.COM quota = valary[2]; 17199396SMatthew.Ahrens@Sun.COM domain = propname + 17209396SMatthew.Ahrens@Sun.COM strlen(zfs_userquota_prop_prefixes[type]); 17219396SMatthew.Ahrens@Sun.COM 172210298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(name, FTAG, &zfsvfs); 17239396SMatthew.Ahrens@Sun.COM if (error == 0) { 17249396SMatthew.Ahrens@Sun.COM error = zfs_set_userquota(zfsvfs, 17259396SMatthew.Ahrens@Sun.COM type, domain, rid, quota); 17269396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 17279396SMatthew.Ahrens@Sun.COM } 17289396SMatthew.Ahrens@Sun.COM if (error == 0) 17299396SMatthew.Ahrens@Sun.COM continue; 17309396SMatthew.Ahrens@Sun.COM else 17319396SMatthew.Ahrens@Sun.COM goto out; 17329396SMatthew.Ahrens@Sun.COM } else if (zfs_prop_user(propname)) { 17339396SMatthew.Ahrens@Sun.COM VERIFY(nvpair_value_string(elem, &strval) == 0); 17349396SMatthew.Ahrens@Sun.COM error = dsl_prop_set(name, propname, 1, 17359396SMatthew.Ahrens@Sun.COM strlen(strval) + 1, strval); 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 } 17414543Smarks } 17422676Seschrock 17432676Seschrock switch (prop) { 17442676Seschrock case ZFS_PROP_QUOTA: 17452676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17464577Sahrens (error = dsl_dir_set_quota(name, intval)) != 0) 17478697SRichard.Morris@Sun.COM goto out; 17482676Seschrock break; 17492676Seschrock 17505378Sck153898 case ZFS_PROP_REFQUOTA: 17515378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17525378Sck153898 (error = dsl_dataset_set_quota(name, intval)) != 0) 17538697SRichard.Morris@Sun.COM goto out; 17545378Sck153898 break; 17555378Sck153898 17562676Seschrock case ZFS_PROP_RESERVATION: 17572676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17582676Seschrock (error = dsl_dir_set_reservation(name, 17592676Seschrock intval)) != 0) 17608697SRichard.Morris@Sun.COM goto out; 17612676Seschrock break; 1762789Sahrens 17635378Sck153898 case ZFS_PROP_REFRESERVATION: 17645378Sck153898 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17655378Sck153898 (error = dsl_dataset_set_reservation(name, 17665378Sck153898 intval)) != 0) 17678697SRichard.Morris@Sun.COM goto out; 17685378Sck153898 break; 17695378Sck153898 17702676Seschrock case ZFS_PROP_VOLSIZE: 17712676Seschrock if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 17724787Sahrens (error = zvol_set_volsize(name, 17734787Sahrens ddi_driver_major(zfs_dip), intval)) != 0) 17748697SRichard.Morris@Sun.COM goto out; 17752676Seschrock break; 17762676Seschrock 17774577Sahrens case ZFS_PROP_VERSION: 17789396SMatthew.Ahrens@Sun.COM { 17799396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 17809396SMatthew.Ahrens@Sun.COM 17819396SMatthew.Ahrens@Sun.COM if ((error = nvpair_value_uint64(elem, &intval)) != 0) 17829396SMatthew.Ahrens@Sun.COM goto out; 178310298SMatthew.Ahrens@Sun.COM if ((error = zfsvfs_hold(name, FTAG, &zfsvfs)) != 0) 17849396SMatthew.Ahrens@Sun.COM goto out; 17859396SMatthew.Ahrens@Sun.COM error = zfs_set_version(zfsvfs, intval); 17869396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 17879396SMatthew.Ahrens@Sun.COM 17889396SMatthew.Ahrens@Sun.COM if (error == 0 && intval >= ZPL_VERSION_USERSPACE) { 17899396SMatthew.Ahrens@Sun.COM zfs_cmd_t zc = { 0 }; 17909396SMatthew.Ahrens@Sun.COM (void) strcpy(zc.zc_name, name); 17919396SMatthew.Ahrens@Sun.COM (void) zfs_ioc_userspace_upgrade(&zc); 17929396SMatthew.Ahrens@Sun.COM } 17939396SMatthew.Ahrens@Sun.COM if (error) 17948697SRichard.Morris@Sun.COM goto out; 17952676Seschrock break; 17969396SMatthew.Ahrens@Sun.COM } 17972676Seschrock 17982676Seschrock default: 17992676Seschrock if (nvpair_type(elem) == DATA_TYPE_STRING) { 18002676Seschrock if (zfs_prop_get_type(prop) != 18018697SRichard.Morris@Sun.COM PROP_TYPE_STRING) { 18028697SRichard.Morris@Sun.COM error = EINVAL; 18038697SRichard.Morris@Sun.COM goto out; 18048697SRichard.Morris@Sun.COM } 18052676Seschrock } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 18062885Sahrens const char *unused; 18072885Sahrens 18082717Seschrock VERIFY(nvpair_value_uint64(elem, &intval) == 0); 18092676Seschrock 18102676Seschrock switch (zfs_prop_get_type(prop)) { 18114787Sahrens case PROP_TYPE_NUMBER: 18122676Seschrock break; 18134787Sahrens case PROP_TYPE_STRING: 18148697SRichard.Morris@Sun.COM error = EINVAL; 18158697SRichard.Morris@Sun.COM goto out; 18164787Sahrens case PROP_TYPE_INDEX: 18172717Seschrock if (zfs_prop_index_to_string(prop, 18188697SRichard.Morris@Sun.COM intval, &unused) != 0) { 18198697SRichard.Morris@Sun.COM error = EINVAL; 18208697SRichard.Morris@Sun.COM goto out; 18218697SRichard.Morris@Sun.COM } 18222676Seschrock break; 18232676Seschrock default: 18244577Sahrens cmn_err(CE_PANIC, 18254577Sahrens "unknown property type"); 18262676Seschrock break; 18272676Seschrock } 18282676Seschrock } else { 18298697SRichard.Morris@Sun.COM error = EINVAL; 18308697SRichard.Morris@Sun.COM goto out; 18312676Seschrock } 18328697SRichard.Morris@Sun.COM if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0) 18338697SRichard.Morris@Sun.COM goto out; 18342676Seschrock } 18352676Seschrock } 18362676Seschrock 18378697SRichard.Morris@Sun.COM if (nvlist_next_nvpair(genericnvl, NULL) != NULL) { 18388697SRichard.Morris@Sun.COM error = dsl_props_set(name, genericnvl); 18398697SRichard.Morris@Sun.COM } 18408697SRichard.Morris@Sun.COM out: 18418697SRichard.Morris@Sun.COM nvlist_free(genericnvl); 18428697SRichard.Morris@Sun.COM return (error); 1843789Sahrens } 1844789Sahrens 18455367Sahrens /* 18469355SMatthew.Ahrens@Sun.COM * Check that all the properties are valid user properties. 18479355SMatthew.Ahrens@Sun.COM */ 18489355SMatthew.Ahrens@Sun.COM static int 18499355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl) 18509355SMatthew.Ahrens@Sun.COM { 18519355SMatthew.Ahrens@Sun.COM nvpair_t *elem = NULL; 18529355SMatthew.Ahrens@Sun.COM int error = 0; 18539355SMatthew.Ahrens@Sun.COM 18549355SMatthew.Ahrens@Sun.COM while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 18559355SMatthew.Ahrens@Sun.COM const char *propname = nvpair_name(elem); 18569355SMatthew.Ahrens@Sun.COM char *valstr; 18579355SMatthew.Ahrens@Sun.COM 18589355SMatthew.Ahrens@Sun.COM if (!zfs_prop_user(propname) || 18599355SMatthew.Ahrens@Sun.COM nvpair_type(elem) != DATA_TYPE_STRING) 18609355SMatthew.Ahrens@Sun.COM return (EINVAL); 18619355SMatthew.Ahrens@Sun.COM 18629355SMatthew.Ahrens@Sun.COM if (error = zfs_secpolicy_write_perms(fsname, 18639355SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERPROP, CRED())) 18649355SMatthew.Ahrens@Sun.COM return (error); 18659355SMatthew.Ahrens@Sun.COM 18669355SMatthew.Ahrens@Sun.COM if (strlen(propname) >= ZAP_MAXNAMELEN) 18679355SMatthew.Ahrens@Sun.COM return (ENAMETOOLONG); 18689355SMatthew.Ahrens@Sun.COM 18699355SMatthew.Ahrens@Sun.COM VERIFY(nvpair_value_string(elem, &valstr) == 0); 18709355SMatthew.Ahrens@Sun.COM if (strlen(valstr) >= ZAP_MAXVALUELEN) 18719355SMatthew.Ahrens@Sun.COM return (E2BIG); 18729355SMatthew.Ahrens@Sun.COM } 18739355SMatthew.Ahrens@Sun.COM return (0); 18749355SMatthew.Ahrens@Sun.COM } 18759355SMatthew.Ahrens@Sun.COM 18769355SMatthew.Ahrens@Sun.COM /* 18775367Sahrens * inputs: 18785367Sahrens * zc_name name of filesystem 18798697SRichard.Morris@Sun.COM * zc_value name of property to set 18805367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 18817265Sahrens * zc_cookie clear existing local props? 18825367Sahrens * 18835367Sahrens * outputs: none 18845367Sahrens */ 1885789Sahrens static int 18862676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 1887789Sahrens { 18882676Seschrock nvlist_t *nvl; 18892676Seschrock int error; 1890789Sahrens 18915094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 18929643SEric.Taylor@Sun.COM zc->zc_iflags, &nvl)) != 0) 18932676Seschrock return (error); 18942676Seschrock 18957265Sahrens if (zc->zc_cookie) { 18967265Sahrens nvlist_t *origprops; 18977265Sahrens objset_t *os; 18987265Sahrens 189910298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) { 19007265Sahrens if (dsl_prop_get_all(os, &origprops, TRUE) == 0) { 19018536SDavid.Pacheco@Sun.COM clear_props(zc->zc_name, origprops, nvl); 19027265Sahrens nvlist_free(origprops); 19037265Sahrens } 190410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 19057265Sahrens } 19067265Sahrens 19077265Sahrens } 19087265Sahrens 19094787Sahrens error = zfs_set_prop_nvlist(zc->zc_name, nvl); 19104543Smarks 19112676Seschrock nvlist_free(nvl); 19122676Seschrock return (error); 1913789Sahrens } 1914789Sahrens 19155367Sahrens /* 19165367Sahrens * inputs: 19175367Sahrens * zc_name name of filesystem 19185367Sahrens * zc_value name of property to inherit 19195367Sahrens * 19205367Sahrens * outputs: none 19215367Sahrens */ 1922789Sahrens static int 19234849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 19244849Sahrens { 19254849Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 19264849Sahrens return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL)); 19274849Sahrens } 19284849Sahrens 19294849Sahrens static int 19304098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 19313912Slling { 19325094Slling nvlist_t *props; 19333912Slling spa_t *spa; 19345094Slling int error; 19358525SEric.Schrock@Sun.COM nvpair_t *elem; 19363912Slling 19375094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 19389643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) 19393912Slling return (error); 19403912Slling 19418525SEric.Schrock@Sun.COM /* 19428525SEric.Schrock@Sun.COM * If the only property is the configfile, then just do a spa_lookup() 19438525SEric.Schrock@Sun.COM * to handle the faulted case. 19448525SEric.Schrock@Sun.COM */ 19458525SEric.Schrock@Sun.COM elem = nvlist_next_nvpair(props, NULL); 19468525SEric.Schrock@Sun.COM if (elem != NULL && strcmp(nvpair_name(elem), 19478525SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 19488525SEric.Schrock@Sun.COM nvlist_next_nvpair(props, elem) == NULL) { 19498525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 19508525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) { 19518525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 19528525SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 19538525SEric.Schrock@Sun.COM } 19548525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 19558525SEric.Schrock@Sun.COM if (spa != NULL) 19568525SEric.Schrock@Sun.COM return (0); 19578525SEric.Schrock@Sun.COM } 19588525SEric.Schrock@Sun.COM 19593912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 19605094Slling nvlist_free(props); 19613912Slling return (error); 19623912Slling } 19633912Slling 19645094Slling error = spa_prop_set(spa, props); 19653912Slling 19665094Slling nvlist_free(props); 19673912Slling spa_close(spa, FTAG); 19683912Slling 19693912Slling return (error); 19703912Slling } 19713912Slling 19723912Slling static int 19734098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 19743912Slling { 19753912Slling spa_t *spa; 19763912Slling int error; 19773912Slling nvlist_t *nvp = NULL; 19783912Slling 19798525SEric.Schrock@Sun.COM if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 19808525SEric.Schrock@Sun.COM /* 19818525SEric.Schrock@Sun.COM * If the pool is faulted, there may be properties we can still 19828525SEric.Schrock@Sun.COM * get (such as altroot and cachefile), so attempt to get them 19838525SEric.Schrock@Sun.COM * anyway. 19848525SEric.Schrock@Sun.COM */ 19858525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 19868525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) 19878525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 19888525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 19898525SEric.Schrock@Sun.COM } else { 19908525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 19918525SEric.Schrock@Sun.COM spa_close(spa, FTAG); 19928525SEric.Schrock@Sun.COM } 19933912Slling 19943912Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 19953912Slling error = put_nvlist(zc, nvp); 19963912Slling else 19973912Slling error = EFAULT; 19983912Slling 19998525SEric.Schrock@Sun.COM nvlist_free(nvp); 20003912Slling return (error); 20013912Slling } 20023912Slling 20033912Slling static int 20044543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 20054543Smarks { 20064543Smarks nvlist_t *nvp; 20074543Smarks int error; 20084543Smarks uint32_t uid; 20094543Smarks uint32_t gid; 20104543Smarks uint32_t *groups; 20114543Smarks uint_t group_cnt; 20124543Smarks cred_t *usercred; 20134543Smarks 20145094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 20159643SEric.Taylor@Sun.COM zc->zc_iflags, &nvp)) != 0) { 20164543Smarks return (error); 20174543Smarks } 20184543Smarks 20194543Smarks if ((error = nvlist_lookup_uint32(nvp, 20204543Smarks ZFS_DELEG_PERM_UID, &uid)) != 0) { 20214543Smarks nvlist_free(nvp); 20224543Smarks return (EPERM); 20234543Smarks } 20244543Smarks 20254543Smarks if ((error = nvlist_lookup_uint32(nvp, 20264543Smarks ZFS_DELEG_PERM_GID, &gid)) != 0) { 20274543Smarks nvlist_free(nvp); 20284543Smarks return (EPERM); 20294543Smarks } 20304543Smarks 20314543Smarks if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 20324543Smarks &groups, &group_cnt)) != 0) { 20334543Smarks nvlist_free(nvp); 20344543Smarks return (EPERM); 20354543Smarks } 20364543Smarks usercred = cralloc(); 20374543Smarks if ((crsetugid(usercred, uid, gid) != 0) || 20384543Smarks (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 20394543Smarks nvlist_free(nvp); 20404543Smarks crfree(usercred); 20414543Smarks return (EPERM); 20424543Smarks } 20434543Smarks nvlist_free(nvp); 20444543Smarks error = dsl_deleg_access(zc->zc_name, 20454787Sahrens zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred); 20464543Smarks crfree(usercred); 20474543Smarks return (error); 20484543Smarks } 20494543Smarks 20505367Sahrens /* 20515367Sahrens * inputs: 20525367Sahrens * zc_name name of filesystem 20535367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 20545367Sahrens * zc_perm_action allow/unallow flag 20555367Sahrens * 20565367Sahrens * outputs: none 20575367Sahrens */ 20584543Smarks static int 20594543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 20604543Smarks { 20614543Smarks int error; 20624543Smarks nvlist_t *fsaclnv = NULL; 20634543Smarks 20645094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 20659643SEric.Taylor@Sun.COM zc->zc_iflags, &fsaclnv)) != 0) 20664543Smarks return (error); 20674543Smarks 20684543Smarks /* 20694543Smarks * Verify nvlist is constructed correctly 20704543Smarks */ 20714543Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 20724543Smarks nvlist_free(fsaclnv); 20734543Smarks return (EINVAL); 20744543Smarks } 20754543Smarks 20764543Smarks /* 20774543Smarks * If we don't have PRIV_SYS_MOUNT, then validate 20784543Smarks * that user is allowed to hand out each permission in 20794543Smarks * the nvlist(s) 20804543Smarks */ 20814543Smarks 20824787Sahrens error = secpolicy_zfs(CRED()); 20834543Smarks if (error) { 20844787Sahrens if (zc->zc_perm_action == B_FALSE) { 20854787Sahrens error = dsl_deleg_can_allow(zc->zc_name, 20864787Sahrens fsaclnv, CRED()); 20874787Sahrens } else { 20884787Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 20894787Sahrens fsaclnv, CRED()); 20904787Sahrens } 20914543Smarks } 20924543Smarks 20934543Smarks if (error == 0) 20944543Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 20954543Smarks 20964543Smarks nvlist_free(fsaclnv); 20974543Smarks return (error); 20984543Smarks } 20994543Smarks 21005367Sahrens /* 21015367Sahrens * inputs: 21025367Sahrens * zc_name name of filesystem 21035367Sahrens * 21045367Sahrens * outputs: 21055367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 21065367Sahrens */ 21074543Smarks static int 21084543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 21094543Smarks { 21104543Smarks nvlist_t *nvp; 21114543Smarks int error; 21124543Smarks 21134543Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 21144543Smarks error = put_nvlist(zc, nvp); 21154543Smarks nvlist_free(nvp); 21164543Smarks } 21174543Smarks 21184543Smarks return (error); 21194543Smarks } 21204543Smarks 21215367Sahrens /* 2122789Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 2123789Sahrens * or NULL if no suitable entry is found. The caller of this routine 2124789Sahrens * is responsible for releasing the returned vfs pointer. 2125789Sahrens */ 2126789Sahrens static vfs_t * 2127789Sahrens zfs_get_vfs(const char *resource) 2128789Sahrens { 2129789Sahrens struct vfs *vfsp; 2130789Sahrens struct vfs *vfs_found = NULL; 2131789Sahrens 2132789Sahrens vfs_list_read_lock(); 2133789Sahrens vfsp = rootvfs; 2134789Sahrens do { 2135789Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2136789Sahrens VFS_HOLD(vfsp); 2137789Sahrens vfs_found = vfsp; 2138789Sahrens break; 2139789Sahrens } 2140789Sahrens vfsp = vfsp->vfs_next; 2141789Sahrens } while (vfsp != rootvfs); 2142789Sahrens vfs_list_unlock(); 2143789Sahrens return (vfs_found); 2144789Sahrens } 2145789Sahrens 21464543Smarks /* ARGSUSED */ 2147789Sahrens static void 21484543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2149789Sahrens { 21505331Samw zfs_creat_t *zct = arg; 21515498Stimh 21525498Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 21535331Samw } 21545331Samw 21555498Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 21565498Stimh 21575331Samw /* 21585498Stimh * inputs: 21597184Stimh * createprops list of properties requested by creator 21607184Stimh * default_zplver zpl version to use if unspecified in createprops 21617184Stimh * fuids_ok fuids allowed in this version of the spa? 21627184Stimh * os parent objset pointer (NULL if root fs) 21635331Samw * 21645498Stimh * outputs: 21655498Stimh * zplprops values for the zplprops we attach to the master node object 21667184Stimh * is_ci true if requested file system will be purely case-insensitive 21675331Samw * 21685498Stimh * Determine the settings for utf8only, normalization and 21695498Stimh * casesensitivity. Specific values may have been requested by the 21705498Stimh * creator and/or we can inherit values from the parent dataset. If 21715498Stimh * the file system is of too early a vintage, a creator can not 21725498Stimh * request settings for these properties, even if the requested 21735498Stimh * setting is the default value. We don't actually want to create dsl 21745498Stimh * properties for these, so remove them from the source nvlist after 21755498Stimh * processing. 21765331Samw */ 21775331Samw static int 21789396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 21797184Stimh boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops, 21807184Stimh boolean_t *is_ci) 21815331Samw { 21825498Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 21835498Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 21845498Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 21855498Stimh 21865498Stimh ASSERT(zplprops != NULL); 21875498Stimh 21885375Stimh /* 21895498Stimh * Pull out creator prop choices, if any. 21905375Stimh */ 21915498Stimh if (createprops) { 21925498Stimh (void) nvlist_lookup_uint64(createprops, 21937184Stimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 21947184Stimh (void) nvlist_lookup_uint64(createprops, 21955498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 21965498Stimh (void) nvlist_remove_all(createprops, 21975498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 21985498Stimh (void) nvlist_lookup_uint64(createprops, 21995498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 22005498Stimh (void) nvlist_remove_all(createprops, 22015498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 22025498Stimh (void) nvlist_lookup_uint64(createprops, 22035498Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 22045498Stimh (void) nvlist_remove_all(createprops, 22055498Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 22065331Samw } 22075331Samw 22085375Stimh /* 22097184Stimh * If the zpl version requested is whacky or the file system 22107184Stimh * or pool is version is too "young" to support normalization 22117184Stimh * and the creator tried to set a value for one of the props, 22127184Stimh * error out. 22135498Stimh */ 22147184Stimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 22157184Stimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 22167184Stimh (zplver < ZPL_VERSION_NORMALIZATION && 22175498Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 22187184Stimh sense != ZFS_PROP_UNDEFINED))) 22195498Stimh return (ENOTSUP); 22205498Stimh 22215498Stimh /* 22225498Stimh * Put the version in the zplprops 22235498Stimh */ 22245498Stimh VERIFY(nvlist_add_uint64(zplprops, 22255498Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 22265498Stimh 22275498Stimh if (norm == ZFS_PROP_UNDEFINED) 22285498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 22295498Stimh VERIFY(nvlist_add_uint64(zplprops, 22305498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 22315498Stimh 22325498Stimh /* 22335498Stimh * If we're normalizing, names must always be valid UTF-8 strings. 22345498Stimh */ 22355498Stimh if (norm) 22365498Stimh u8 = 1; 22375498Stimh if (u8 == ZFS_PROP_UNDEFINED) 22385498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 22395498Stimh VERIFY(nvlist_add_uint64(zplprops, 22405498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 22415498Stimh 22425498Stimh if (sense == ZFS_PROP_UNDEFINED) 22435498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 22445498Stimh VERIFY(nvlist_add_uint64(zplprops, 22455498Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 22465498Stimh 22476492Stimh if (is_ci) 22486492Stimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 22496492Stimh 22507184Stimh return (0); 22517184Stimh } 22527184Stimh 22537184Stimh static int 22547184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 22557184Stimh nvlist_t *zplprops, boolean_t *is_ci) 22567184Stimh { 22577184Stimh boolean_t fuids_ok = B_TRUE; 22587184Stimh uint64_t zplver = ZPL_VERSION; 22597184Stimh objset_t *os = NULL; 22607184Stimh char parentname[MAXNAMELEN]; 22617184Stimh char *cp; 22627184Stimh int error; 22637184Stimh 22647184Stimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 22657184Stimh cp = strrchr(parentname, '/'); 22667184Stimh ASSERT(cp != NULL); 22677184Stimh cp[0] = '\0'; 22687184Stimh 22699396SMatthew.Ahrens@Sun.COM if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE)) 22709396SMatthew.Ahrens@Sun.COM zplver = ZPL_VERSION_USERSPACE - 1; 22717184Stimh if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) { 22727184Stimh zplver = ZPL_VERSION_FUID - 1; 22737184Stimh fuids_ok = B_FALSE; 22747184Stimh } 22757184Stimh 22767184Stimh /* 22777184Stimh * Open parent object set so we can inherit zplprop values. 22787184Stimh */ 227910298SMatthew.Ahrens@Sun.COM if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 22807184Stimh return (error); 22817184Stimh 22827184Stimh error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops, 22837184Stimh zplprops, is_ci); 228410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 22857184Stimh return (error); 22867184Stimh } 22877184Stimh 22887184Stimh static int 22897184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 22907184Stimh nvlist_t *zplprops, boolean_t *is_ci) 22917184Stimh { 22927184Stimh boolean_t fuids_ok = B_TRUE; 22937184Stimh uint64_t zplver = ZPL_VERSION; 22947184Stimh int error; 22957184Stimh 22967184Stimh if (spa_vers < SPA_VERSION_FUID) { 22977184Stimh zplver = ZPL_VERSION_FUID - 1; 22987184Stimh fuids_ok = B_FALSE; 22997184Stimh } 23007184Stimh 23017184Stimh error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops, 23027184Stimh zplprops, is_ci); 23037184Stimh return (error); 2304789Sahrens } 2305789Sahrens 23065367Sahrens /* 23075367Sahrens * inputs: 23085367Sahrens * zc_objset_type type of objset to create (fs vs zvol) 23095367Sahrens * zc_name name of new objset 23105367Sahrens * zc_value name of snapshot to clone from (may be empty) 23115367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 23125367Sahrens * 23135498Stimh * outputs: none 23145367Sahrens */ 2315789Sahrens static int 2316789Sahrens zfs_ioc_create(zfs_cmd_t *zc) 2317789Sahrens { 2318789Sahrens objset_t *clone; 2319789Sahrens int error = 0; 23205331Samw zfs_creat_t zct; 23214543Smarks nvlist_t *nvprops = NULL; 23224543Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2323789Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2324789Sahrens 2325789Sahrens switch (type) { 2326789Sahrens 2327789Sahrens case DMU_OST_ZFS: 2328789Sahrens cbfunc = zfs_create_cb; 2329789Sahrens break; 2330789Sahrens 2331789Sahrens case DMU_OST_ZVOL: 2332789Sahrens cbfunc = zvol_create_cb; 2333789Sahrens break; 2334789Sahrens 2335789Sahrens default: 23362199Sahrens cbfunc = NULL; 23376423Sgw25295 break; 23382199Sahrens } 23395326Sek110237 if (strchr(zc->zc_name, '@') || 23405326Sek110237 strchr(zc->zc_name, '%')) 2341789Sahrens return (EINVAL); 2342789Sahrens 23432676Seschrock if (zc->zc_nvlist_src != NULL && 23445094Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 23459643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 23462676Seschrock return (error); 23472676Seschrock 23485498Stimh zct.zct_zplprops = NULL; 23495331Samw zct.zct_props = nvprops; 23505331Samw 23512676Seschrock if (zc->zc_value[0] != '\0') { 2352789Sahrens /* 2353789Sahrens * We're creating a clone of an existing snapshot. 2354789Sahrens */ 23552676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 23562676Seschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 23574543Smarks nvlist_free(nvprops); 2358789Sahrens return (EINVAL); 23592676Seschrock } 2360789Sahrens 236110298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_value, FTAG, &clone); 23622676Seschrock if (error) { 23634543Smarks nvlist_free(nvprops); 2364789Sahrens return (error); 23652676Seschrock } 23666492Stimh 236710272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0); 236810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 23695331Samw if (error) { 23705331Samw nvlist_free(nvprops); 23715331Samw return (error); 23725331Samw } 2373789Sahrens } else { 23746492Stimh boolean_t is_insensitive = B_FALSE; 23756492Stimh 23762676Seschrock if (cbfunc == NULL) { 23774543Smarks nvlist_free(nvprops); 23782199Sahrens return (EINVAL); 23792676Seschrock } 23802676Seschrock 2381789Sahrens if (type == DMU_OST_ZVOL) { 23822676Seschrock uint64_t volsize, volblocksize; 23832676Seschrock 23844543Smarks if (nvprops == NULL || 23854543Smarks nvlist_lookup_uint64(nvprops, 23862676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 23872676Seschrock &volsize) != 0) { 23884543Smarks nvlist_free(nvprops); 23892676Seschrock return (EINVAL); 23902676Seschrock } 23912676Seschrock 23924543Smarks if ((error = nvlist_lookup_uint64(nvprops, 23932676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 23942676Seschrock &volblocksize)) != 0 && error != ENOENT) { 23954543Smarks nvlist_free(nvprops); 23962676Seschrock return (EINVAL); 23972676Seschrock } 23981133Seschrock 23992676Seschrock if (error != 0) 24002676Seschrock volblocksize = zfs_prop_default_numeric( 24012676Seschrock ZFS_PROP_VOLBLOCKSIZE); 24022676Seschrock 24032676Seschrock if ((error = zvol_check_volblocksize( 24042676Seschrock volblocksize)) != 0 || 24052676Seschrock (error = zvol_check_volsize(volsize, 24062676Seschrock volblocksize)) != 0) { 24074543Smarks nvlist_free(nvprops); 2408789Sahrens return (error); 24092676Seschrock } 24104577Sahrens } else if (type == DMU_OST_ZFS) { 24115331Samw int error; 24125331Samw 24135498Stimh /* 24145331Samw * We have to have normalization and 24155331Samw * case-folding flags correct when we do the 24165331Samw * file system creation, so go figure them out 24175498Stimh * now. 24185331Samw */ 24195498Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 24205498Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 24215498Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 24227184Stimh zct.zct_zplprops, &is_insensitive); 24235331Samw if (error != 0) { 24245331Samw nvlist_free(nvprops); 24255498Stimh nvlist_free(zct.zct_zplprops); 24265331Samw return (error); 24274577Sahrens } 24282676Seschrock } 242910272SMatthew.Ahrens@Sun.COM error = dmu_objset_create(zc->zc_name, type, 24306492Stimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 24315498Stimh nvlist_free(zct.zct_zplprops); 2432789Sahrens } 24332676Seschrock 24342676Seschrock /* 24352676Seschrock * It would be nice to do this atomically. 24362676Seschrock */ 24372676Seschrock if (error == 0) { 24384787Sahrens if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0) 243910242Schris.kirby@sun.com (void) dmu_objset_destroy(zc->zc_name, B_FALSE); 24402676Seschrock } 24414543Smarks nvlist_free(nvprops); 2442789Sahrens return (error); 2443789Sahrens } 2444789Sahrens 24455367Sahrens /* 24465367Sahrens * inputs: 24475367Sahrens * zc_name name of filesystem 24485367Sahrens * zc_value short name of snapshot 24495367Sahrens * zc_cookie recursive flag 24509396SMatthew.Ahrens@Sun.COM * zc_nvlist_src[_size] property list 24515367Sahrens * 2452*10588SEric.Taylor@Sun.COM * outputs: 2453*10588SEric.Taylor@Sun.COM * zc_value short snapname (i.e. part after the '@') 24545367Sahrens */ 2455789Sahrens static int 24562199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 24572199Sahrens { 24587265Sahrens nvlist_t *nvprops = NULL; 24597265Sahrens int error; 24607265Sahrens boolean_t recursive = zc->zc_cookie; 24617265Sahrens 24622676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 24632199Sahrens return (EINVAL); 24647265Sahrens 24657265Sahrens if (zc->zc_nvlist_src != NULL && 24667265Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 24679643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 24687265Sahrens return (error); 24697265Sahrens 24709355SMatthew.Ahrens@Sun.COM error = zfs_check_userprops(zc->zc_name, nvprops); 24719355SMatthew.Ahrens@Sun.COM if (error) 24729355SMatthew.Ahrens@Sun.COM goto out; 24739355SMatthew.Ahrens@Sun.COM 24749355SMatthew.Ahrens@Sun.COM if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL && 24759355SMatthew.Ahrens@Sun.COM zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) { 24769355SMatthew.Ahrens@Sun.COM error = ENOTSUP; 24779355SMatthew.Ahrens@Sun.COM goto out; 24787265Sahrens } 24799355SMatthew.Ahrens@Sun.COM 24809355SMatthew.Ahrens@Sun.COM error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, 24819355SMatthew.Ahrens@Sun.COM nvprops, recursive); 24829355SMatthew.Ahrens@Sun.COM 24839355SMatthew.Ahrens@Sun.COM out: 24847265Sahrens nvlist_free(nvprops); 24857265Sahrens return (error); 24862199Sahrens } 24872199Sahrens 24884007Smmusante int 24892199Sahrens zfs_unmount_snap(char *name, void *arg) 2490789Sahrens { 24912417Sahrens vfs_t *vfsp = NULL; 24922199Sahrens 24936689Smaybee if (arg) { 24946689Smaybee char *snapname = arg; 24956689Smaybee int len = strlen(name) + strlen(snapname) + 2; 24966689Smaybee char *buf = kmem_alloc(len, KM_SLEEP); 24976689Smaybee 24986689Smaybee (void) strcpy(buf, name); 24996689Smaybee (void) strcat(buf, "@"); 25006689Smaybee (void) strcat(buf, snapname); 25016689Smaybee vfsp = zfs_get_vfs(buf); 25026689Smaybee kmem_free(buf, len); 25032417Sahrens } else if (strchr(name, '@')) { 25042199Sahrens vfsp = zfs_get_vfs(name); 25052199Sahrens } 25062199Sahrens 25072199Sahrens if (vfsp) { 25082199Sahrens /* 25092199Sahrens * Always force the unmount for snapshots. 25102199Sahrens */ 25112199Sahrens int flag = MS_FORCE; 2512789Sahrens int err; 2513789Sahrens 25142199Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 25152199Sahrens VFS_RELE(vfsp); 25162199Sahrens return (err); 25172199Sahrens } 25182199Sahrens VFS_RELE(vfsp); 25192199Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 25202199Sahrens return (err); 25212199Sahrens } 25222199Sahrens return (0); 25232199Sahrens } 25242199Sahrens 25255367Sahrens /* 25265367Sahrens * inputs: 252710242Schris.kirby@sun.com * zc_name name of filesystem 252810242Schris.kirby@sun.com * zc_value short name of snapshot 252910242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 25305367Sahrens * 25315367Sahrens * outputs: none 25325367Sahrens */ 25332199Sahrens static int 25342199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 25352199Sahrens { 25362199Sahrens int err; 2537789Sahrens 25382676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 25392199Sahrens return (EINVAL); 25402199Sahrens err = dmu_objset_find(zc->zc_name, 25412676Seschrock zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 25422199Sahrens if (err) 25432199Sahrens return (err); 254410242Schris.kirby@sun.com return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value, 254510242Schris.kirby@sun.com zc->zc_defer_destroy)); 25462199Sahrens } 25472199Sahrens 25485367Sahrens /* 25495367Sahrens * inputs: 25505367Sahrens * zc_name name of dataset to destroy 25515367Sahrens * zc_objset_type type of objset 255210242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 25535367Sahrens * 25545367Sahrens * outputs: none 25555367Sahrens */ 25562199Sahrens static int 25572199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 25582199Sahrens { 2559*10588SEric.Taylor@Sun.COM int err; 25602199Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 2561*10588SEric.Taylor@Sun.COM err = zfs_unmount_snap(zc->zc_name, NULL); 25622199Sahrens if (err) 25632199Sahrens return (err); 2564789Sahrens } 2565789Sahrens 2566*10588SEric.Taylor@Sun.COM err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy); 2567*10588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 2568*10588SEric.Taylor@Sun.COM zvol_remove_minor(zc->zc_name); 2569*10588SEric.Taylor@Sun.COM return (err); 2570789Sahrens } 2571789Sahrens 25725367Sahrens /* 25735367Sahrens * inputs: 25745446Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 25755367Sahrens * 25765367Sahrens * outputs: none 25775367Sahrens */ 2578789Sahrens static int 2579789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 2580789Sahrens { 258110272SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds, *clone; 25825446Sahrens int error; 258310272SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 258410272SMatthew.Ahrens@Sun.COM char *clone_name; 258510272SMatthew.Ahrens@Sun.COM 258610272SMatthew.Ahrens@Sun.COM error = dsl_dataset_hold(zc->zc_name, FTAG, &ds); 258710272SMatthew.Ahrens@Sun.COM if (error) 258810272SMatthew.Ahrens@Sun.COM return (error); 258910272SMatthew.Ahrens@Sun.COM 259010272SMatthew.Ahrens@Sun.COM /* must not be a snapshot */ 259110272SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds)) { 259210272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 259310272SMatthew.Ahrens@Sun.COM return (EINVAL); 259410272SMatthew.Ahrens@Sun.COM } 259510272SMatthew.Ahrens@Sun.COM 259610272SMatthew.Ahrens@Sun.COM /* must have a most recent snapshot */ 259710272SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 259810272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 259910272SMatthew.Ahrens@Sun.COM return (EINVAL); 260010272SMatthew.Ahrens@Sun.COM } 26015446Sahrens 26025446Sahrens /* 260310272SMatthew.Ahrens@Sun.COM * Create clone of most recent snapshot. 26045446Sahrens */ 260510272SMatthew.Ahrens@Sun.COM clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name); 260610272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT); 26075446Sahrens if (error) 260810272SMatthew.Ahrens@Sun.COM goto out; 260910272SMatthew.Ahrens@Sun.COM 261010298SMatthew.Ahrens@Sun.COM error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone); 261110272SMatthew.Ahrens@Sun.COM if (error) 261210272SMatthew.Ahrens@Sun.COM goto out; 261310272SMatthew.Ahrens@Sun.COM 261410272SMatthew.Ahrens@Sun.COM /* 261510272SMatthew.Ahrens@Sun.COM * Do clone swap. 261610272SMatthew.Ahrens@Sun.COM */ 26179396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 261810298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 26196083Sek110237 if (error == 0) { 26206083Sek110237 int resume_err; 26216083Sek110237 262210272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 262310272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, 262410272SMatthew.Ahrens@Sun.COM B_TRUE); 262510272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 262610272SMatthew.Ahrens@Sun.COM ds = NULL; 262710272SMatthew.Ahrens@Sun.COM } else { 262810272SMatthew.Ahrens@Sun.COM error = EBUSY; 262910272SMatthew.Ahrens@Sun.COM } 263010298SMatthew.Ahrens@Sun.COM resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); 26316083Sek110237 error = error ? error : resume_err; 26326083Sek110237 } 26335446Sahrens VFS_RELE(zfsvfs->z_vfs); 26345446Sahrens } else { 263510272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 263610272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, B_TRUE); 263710272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 263810272SMatthew.Ahrens@Sun.COM ds = NULL; 263910272SMatthew.Ahrens@Sun.COM } else { 264010272SMatthew.Ahrens@Sun.COM error = EBUSY; 264110272SMatthew.Ahrens@Sun.COM } 26425446Sahrens } 264310272SMatthew.Ahrens@Sun.COM 264410272SMatthew.Ahrens@Sun.COM /* 264510272SMatthew.Ahrens@Sun.COM * Destroy clone (which also closes it). 264610272SMatthew.Ahrens@Sun.COM */ 264710272SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(clone, FTAG, B_FALSE); 264810272SMatthew.Ahrens@Sun.COM 264910272SMatthew.Ahrens@Sun.COM out: 265010272SMatthew.Ahrens@Sun.COM strfree(clone_name); 265110272SMatthew.Ahrens@Sun.COM if (ds) 265210272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 26535446Sahrens return (error); 2654789Sahrens } 2655789Sahrens 26565367Sahrens /* 26575367Sahrens * inputs: 26585367Sahrens * zc_name old name of dataset 26595367Sahrens * zc_value new name of dataset 26605367Sahrens * zc_cookie recursive flag (only valid for snapshots) 26615367Sahrens * 26625367Sahrens * outputs: none 26635367Sahrens */ 2664789Sahrens static int 2665789Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 2666789Sahrens { 26674490Svb160487 boolean_t recursive = zc->zc_cookie & 1; 26684007Smmusante 26692676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 26705326Sek110237 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 26715326Sek110237 strchr(zc->zc_value, '%')) 2672789Sahrens return (EINVAL); 2673789Sahrens 26744007Smmusante /* 26754007Smmusante * Unmount snapshot unless we're doing a recursive rename, 26764007Smmusante * in which case the dataset code figures out which snapshots 26774007Smmusante * to unmount. 26784007Smmusante */ 26794007Smmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 2680789Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 26812199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 26822199Sahrens if (err) 26832199Sahrens return (err); 2684789Sahrens } 2685*10588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL) 2686*10588SEric.Taylor@Sun.COM (void) zvol_remove_minor(zc->zc_name); 26874007Smmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 2688789Sahrens } 2689789Sahrens 26906689Smaybee static void 26918536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops) 26926689Smaybee { 26936689Smaybee zfs_cmd_t *zc; 26946689Smaybee nvpair_t *prop; 26956689Smaybee 26966689Smaybee if (props == NULL) 26976689Smaybee return; 26986689Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 26996689Smaybee (void) strcpy(zc->zc_name, dataset); 27006689Smaybee for (prop = nvlist_next_nvpair(props, NULL); prop; 27016689Smaybee prop = nvlist_next_nvpair(props, prop)) { 27028536SDavid.Pacheco@Sun.COM if (newprops != NULL && 27038536SDavid.Pacheco@Sun.COM nvlist_exists(newprops, nvpair_name(prop))) 27048536SDavid.Pacheco@Sun.COM continue; 27056689Smaybee (void) strcpy(zc->zc_value, nvpair_name(prop)); 27066689Smaybee if (zfs_secpolicy_inherit(zc, CRED()) == 0) 27076689Smaybee (void) zfs_ioc_inherit_prop(zc); 27086689Smaybee } 27096689Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 27106689Smaybee } 27116689Smaybee 27125367Sahrens /* 27135367Sahrens * inputs: 27145367Sahrens * zc_name name of containing filesystem 27155367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 27165367Sahrens * zc_value name of snapshot to create 27175367Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 27185367Sahrens * zc_cookie file descriptor to recv from 27195367Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 27205367Sahrens * zc_guid force flag 27215367Sahrens * 27225367Sahrens * outputs: 27235367Sahrens * zc_cookie number of bytes read 27245367Sahrens */ 2725789Sahrens static int 27265367Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 2727789Sahrens { 2728789Sahrens file_t *fp; 27295326Sek110237 objset_t *os; 27305367Sahrens dmu_recv_cookie_t drc; 27315326Sek110237 boolean_t force = (boolean_t)zc->zc_guid; 2732789Sahrens int error, fd; 27335367Sahrens offset_t off; 27345367Sahrens nvlist_t *props = NULL; 27356689Smaybee nvlist_t *origprops = NULL; 27365367Sahrens objset_t *origin = NULL; 27375367Sahrens char *tosnap; 27385367Sahrens char tofs[ZFS_MAXNAMELEN]; 2739789Sahrens 27403265Sahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 27415326Sek110237 strchr(zc->zc_value, '@') == NULL || 27425326Sek110237 strchr(zc->zc_value, '%')) 27433265Sahrens return (EINVAL); 27443265Sahrens 27455367Sahrens (void) strcpy(tofs, zc->zc_value); 27465367Sahrens tosnap = strchr(tofs, '@'); 27475367Sahrens *tosnap = '\0'; 27485367Sahrens tosnap++; 27495367Sahrens 27505367Sahrens if (zc->zc_nvlist_src != NULL && 27515367Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 27529643SEric.Taylor@Sun.COM zc->zc_iflags, &props)) != 0) 27535367Sahrens return (error); 27545367Sahrens 2755789Sahrens fd = zc->zc_cookie; 2756789Sahrens fp = getf(fd); 27575367Sahrens if (fp == NULL) { 27585367Sahrens nvlist_free(props); 2759789Sahrens return (EBADF); 27605367Sahrens } 27615326Sek110237 276210298SMatthew.Ahrens@Sun.COM if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) { 27636689Smaybee /* 27646689Smaybee * If new properties are supplied, they are to completely 27656689Smaybee * replace the existing ones, so stash away the existing ones. 27666689Smaybee */ 276710298SMatthew.Ahrens@Sun.COM (void) dsl_prop_get_all(os, &origprops, B_TRUE); 276810298SMatthew.Ahrens@Sun.COM 276910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 27705326Sek110237 } 27715326Sek110237 27725367Sahrens if (zc->zc_string[0]) { 277310298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_string, FTAG, &origin); 27746689Smaybee if (error) 27756689Smaybee goto out; 27765367Sahrens } 27775367Sahrens 27785367Sahrens error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record, 277910204SMatthew.Ahrens@Sun.COM force, origin, &drc); 27805367Sahrens if (origin) 278110298SMatthew.Ahrens@Sun.COM dmu_objset_rele(origin, FTAG); 27826689Smaybee if (error) 27836689Smaybee goto out; 27845326Sek110237 27855326Sek110237 /* 27866689Smaybee * Reset properties. We do this before we receive the stream 27876689Smaybee * so that the properties are applied to the new data. 27885326Sek110237 */ 27895367Sahrens if (props) { 27908536SDavid.Pacheco@Sun.COM clear_props(tofs, origprops, props); 27916689Smaybee /* 27926689Smaybee * XXX - Note, this is all-or-nothing; should be best-effort. 27936689Smaybee */ 27946689Smaybee (void) zfs_set_prop_nvlist(tofs, props); 27955367Sahrens } 27965367Sahrens 27975367Sahrens off = fp->f_offset; 27985367Sahrens error = dmu_recv_stream(&drc, fp->f_vnode, &off); 27995367Sahrens 280010204SMatthew.Ahrens@Sun.COM if (error == 0) { 280110204SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs = NULL; 280210204SMatthew.Ahrens@Sun.COM 280310204SMatthew.Ahrens@Sun.COM if (getzfsvfs(tofs, &zfsvfs) == 0) { 280410204SMatthew.Ahrens@Sun.COM /* online recv */ 280510204SMatthew.Ahrens@Sun.COM int end_err; 280610298SMatthew.Ahrens@Sun.COM 280710298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 280810204SMatthew.Ahrens@Sun.COM /* 280910204SMatthew.Ahrens@Sun.COM * If the suspend fails, then the recv_end will 281010204SMatthew.Ahrens@Sun.COM * likely also fail, and clean up after itself. 281110204SMatthew.Ahrens@Sun.COM */ 281210204SMatthew.Ahrens@Sun.COM end_err = dmu_recv_end(&drc); 281310204SMatthew.Ahrens@Sun.COM if (error == 0) { 281410204SMatthew.Ahrens@Sun.COM int resume_err = 281510298SMatthew.Ahrens@Sun.COM zfs_resume_fs(zfsvfs, tofs); 281610204SMatthew.Ahrens@Sun.COM error = error ? error : resume_err; 281710204SMatthew.Ahrens@Sun.COM } 281810204SMatthew.Ahrens@Sun.COM error = error ? error : end_err; 281910204SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 282010204SMatthew.Ahrens@Sun.COM } else { 28216689Smaybee error = dmu_recv_end(&drc); 28225367Sahrens } 28236083Sek110237 } 28245367Sahrens 28255367Sahrens zc->zc_cookie = off - fp->f_offset; 28265367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 28275367Sahrens fp->f_offset = off; 28282885Sahrens 28296689Smaybee /* 28306689Smaybee * On error, restore the original props. 28316689Smaybee */ 28326689Smaybee if (error && props) { 28338536SDavid.Pacheco@Sun.COM clear_props(tofs, props, NULL); 28346689Smaybee (void) zfs_set_prop_nvlist(tofs, origprops); 28356689Smaybee } 28366689Smaybee out: 28376689Smaybee nvlist_free(props); 28386689Smaybee nvlist_free(origprops); 2839789Sahrens releasef(fd); 2840789Sahrens return (error); 2841789Sahrens } 2842789Sahrens 28435367Sahrens /* 28445367Sahrens * inputs: 28455367Sahrens * zc_name name of snapshot to send 28465367Sahrens * zc_value short name of incremental fromsnap (may be empty) 28475367Sahrens * zc_cookie file descriptor to send stream to 28485367Sahrens * zc_obj fromorigin flag (mutually exclusive with zc_value) 28495367Sahrens * 28505367Sahrens * outputs: none 28515367Sahrens */ 2852789Sahrens static int 28535367Sahrens zfs_ioc_send(zfs_cmd_t *zc) 2854789Sahrens { 2855789Sahrens objset_t *fromsnap = NULL; 2856789Sahrens objset_t *tosnap; 2857789Sahrens file_t *fp; 2858789Sahrens int error; 28595367Sahrens offset_t off; 2860789Sahrens 286110298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap); 2862789Sahrens if (error) 2863789Sahrens return (error); 2864789Sahrens 28652676Seschrock if (zc->zc_value[0] != '\0') { 28668012SEric.Taylor@Sun.COM char *buf; 28672885Sahrens char *cp; 28682885Sahrens 28698012SEric.Taylor@Sun.COM buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 28708012SEric.Taylor@Sun.COM (void) strncpy(buf, zc->zc_name, MAXPATHLEN); 28712885Sahrens cp = strchr(buf, '@'); 28722885Sahrens if (cp) 28732885Sahrens *(cp+1) = 0; 28748012SEric.Taylor@Sun.COM (void) strncat(buf, zc->zc_value, MAXPATHLEN); 287510298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(buf, FTAG, &fromsnap); 28768012SEric.Taylor@Sun.COM kmem_free(buf, MAXPATHLEN); 2877789Sahrens if (error) { 287810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 2879789Sahrens return (error); 2880789Sahrens } 2881789Sahrens } 2882789Sahrens 2883789Sahrens fp = getf(zc->zc_cookie); 2884789Sahrens if (fp == NULL) { 288510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 2886789Sahrens if (fromsnap) 288710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 2888789Sahrens return (EBADF); 2889789Sahrens } 2890789Sahrens 28915367Sahrens off = fp->f_offset; 28925367Sahrens error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 28935367Sahrens 28945367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 28955367Sahrens fp->f_offset = off; 2896789Sahrens releasef(zc->zc_cookie); 2897789Sahrens if (fromsnap) 289810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 289910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 2900789Sahrens return (error); 2901789Sahrens } 2902789Sahrens 29031544Seschrock static int 29041544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 29051544Seschrock { 29061544Seschrock int id, error; 29071544Seschrock 29081544Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 29091544Seschrock &zc->zc_inject_record); 29101544Seschrock 29111544Seschrock if (error == 0) 29121544Seschrock zc->zc_guid = (uint64_t)id; 29131544Seschrock 29141544Seschrock return (error); 29151544Seschrock } 29161544Seschrock 29171544Seschrock static int 29181544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 29191544Seschrock { 29201544Seschrock return (zio_clear_fault((int)zc->zc_guid)); 29211544Seschrock } 29221544Seschrock 29231544Seschrock static int 29241544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 29251544Seschrock { 29261544Seschrock int id = (int)zc->zc_guid; 29271544Seschrock int error; 29281544Seschrock 29291544Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 29301544Seschrock &zc->zc_inject_record); 29311544Seschrock 29321544Seschrock zc->zc_guid = id; 29331544Seschrock 29341544Seschrock return (error); 29351544Seschrock } 29361544Seschrock 29371544Seschrock static int 29381544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 29391544Seschrock { 29401544Seschrock spa_t *spa; 29411544Seschrock int error; 29422676Seschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 29431544Seschrock 29441544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 29451544Seschrock return (error); 29461544Seschrock 29472676Seschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 29481544Seschrock &count); 29491544Seschrock if (error == 0) 29502676Seschrock zc->zc_nvlist_dst_size = count; 29511544Seschrock else 29522676Seschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 29531544Seschrock 29541544Seschrock spa_close(spa, FTAG); 29551544Seschrock 29561544Seschrock return (error); 29571544Seschrock } 29581544Seschrock 29591544Seschrock static int 29601544Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 29611544Seschrock { 29621544Seschrock spa_t *spa; 29631544Seschrock vdev_t *vd; 29641544Seschrock int error; 29651544Seschrock 29667294Sperrin /* 29677294Sperrin * On zpool clear we also fix up missing slogs 29687294Sperrin */ 29697294Sperrin mutex_enter(&spa_namespace_lock); 29707294Sperrin spa = spa_lookup(zc->zc_name); 29717294Sperrin if (spa == NULL) { 29727294Sperrin mutex_exit(&spa_namespace_lock); 29737294Sperrin return (EIO); 29747294Sperrin } 29757294Sperrin if (spa->spa_log_state == SPA_LOG_MISSING) { 29767294Sperrin /* we need to let spa_open/spa_load clear the chains */ 29777294Sperrin spa->spa_log_state = SPA_LOG_CLEAR; 29787294Sperrin } 29797294Sperrin mutex_exit(&spa_namespace_lock); 29807294Sperrin 29811544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 29821544Seschrock return (error); 29831544Seschrock 29847754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 29851544Seschrock 29862676Seschrock if (zc->zc_guid == 0) { 29871544Seschrock vd = NULL; 29886643Seschrock } else { 29896643Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 29905450Sbrendan if (vd == NULL) { 29917754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, ENODEV); 29925450Sbrendan spa_close(spa, FTAG); 29935450Sbrendan return (ENODEV); 29945450Sbrendan } 29951544Seschrock } 29961544Seschrock 29977754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 29987754SJeff.Bonwick@Sun.COM 29997754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 30007754SJeff.Bonwick@Sun.COM 30017754SJeff.Bonwick@Sun.COM /* 30027754SJeff.Bonwick@Sun.COM * Resume any suspended I/Os. 30037754SJeff.Bonwick@Sun.COM */ 30049234SGeorge.Wilson@Sun.COM if (zio_resume(spa) != 0) 30059234SGeorge.Wilson@Sun.COM error = EIO; 30061544Seschrock 30071544Seschrock spa_close(spa, FTAG); 30081544Seschrock 30099234SGeorge.Wilson@Sun.COM return (error); 30101544Seschrock } 30111544Seschrock 30125367Sahrens /* 30135367Sahrens * inputs: 30145367Sahrens * zc_name name of filesystem 30155367Sahrens * zc_value name of origin snapshot 30165367Sahrens * 3017*10588SEric.Taylor@Sun.COM * outputs: 3018*10588SEric.Taylor@Sun.COM * zc_string name of conflicting snapshot, if there is one 30195367Sahrens */ 30201544Seschrock static int 30212082Seschrock zfs_ioc_promote(zfs_cmd_t *zc) 30222082Seschrock { 30232417Sahrens char *cp; 30242417Sahrens 30252417Sahrens /* 30262417Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 30272417Sahrens * it's easier. 30282417Sahrens */ 30292676Seschrock cp = strchr(zc->zc_value, '@'); 30302417Sahrens if (cp) 30312417Sahrens *cp = '\0'; 30322676Seschrock (void) dmu_objset_find(zc->zc_value, 30332417Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 3034*10588SEric.Taylor@Sun.COM return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 30352082Seschrock } 30362082Seschrock 30374543Smarks /* 30389396SMatthew.Ahrens@Sun.COM * Retrieve a single {user|group}{used|quota}@... property. 30399396SMatthew.Ahrens@Sun.COM * 30409396SMatthew.Ahrens@Sun.COM * inputs: 30419396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 30429396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 30439396SMatthew.Ahrens@Sun.COM * zc_value domain name (eg. "S-1-234-567-89") 30449396SMatthew.Ahrens@Sun.COM * zc_guid RID/UID/GID 30459396SMatthew.Ahrens@Sun.COM * 30469396SMatthew.Ahrens@Sun.COM * outputs: 30479396SMatthew.Ahrens@Sun.COM * zc_cookie property value 30489396SMatthew.Ahrens@Sun.COM */ 30499396SMatthew.Ahrens@Sun.COM static int 30509396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc) 30519396SMatthew.Ahrens@Sun.COM { 30529396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 30539396SMatthew.Ahrens@Sun.COM int error; 30549396SMatthew.Ahrens@Sun.COM 30559396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 30569396SMatthew.Ahrens@Sun.COM return (EINVAL); 30579396SMatthew.Ahrens@Sun.COM 305810298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 30599396SMatthew.Ahrens@Sun.COM if (error) 30609396SMatthew.Ahrens@Sun.COM return (error); 30619396SMatthew.Ahrens@Sun.COM 30629396SMatthew.Ahrens@Sun.COM error = zfs_userspace_one(zfsvfs, 30639396SMatthew.Ahrens@Sun.COM zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 30649396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 30659396SMatthew.Ahrens@Sun.COM 30669396SMatthew.Ahrens@Sun.COM return (error); 30679396SMatthew.Ahrens@Sun.COM } 30689396SMatthew.Ahrens@Sun.COM 30699396SMatthew.Ahrens@Sun.COM /* 30709396SMatthew.Ahrens@Sun.COM * inputs: 30719396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 30729396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 30739396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 30749396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 30759396SMatthew.Ahrens@Sun.COM * 30769396SMatthew.Ahrens@Sun.COM * outputs: 30779396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 30789396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 30799396SMatthew.Ahrens@Sun.COM */ 30809396SMatthew.Ahrens@Sun.COM static int 30819396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc) 30829396SMatthew.Ahrens@Sun.COM { 30839396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 30849396SMatthew.Ahrens@Sun.COM int error; 30859396SMatthew.Ahrens@Sun.COM 308610298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 30879396SMatthew.Ahrens@Sun.COM if (error) 30889396SMatthew.Ahrens@Sun.COM return (error); 30899396SMatthew.Ahrens@Sun.COM 30909396SMatthew.Ahrens@Sun.COM int bufsize = zc->zc_nvlist_dst_size; 30919396SMatthew.Ahrens@Sun.COM void *buf = kmem_alloc(bufsize, KM_SLEEP); 30929396SMatthew.Ahrens@Sun.COM 30939396SMatthew.Ahrens@Sun.COM error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 30949396SMatthew.Ahrens@Sun.COM buf, &zc->zc_nvlist_dst_size); 30959396SMatthew.Ahrens@Sun.COM 30969396SMatthew.Ahrens@Sun.COM if (error == 0) { 30979396SMatthew.Ahrens@Sun.COM error = xcopyout(buf, 30989396SMatthew.Ahrens@Sun.COM (void *)(uintptr_t)zc->zc_nvlist_dst, 30999396SMatthew.Ahrens@Sun.COM zc->zc_nvlist_dst_size); 31009396SMatthew.Ahrens@Sun.COM } 31019396SMatthew.Ahrens@Sun.COM kmem_free(buf, bufsize); 31029396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 31039396SMatthew.Ahrens@Sun.COM 31049396SMatthew.Ahrens@Sun.COM return (error); 31059396SMatthew.Ahrens@Sun.COM } 31069396SMatthew.Ahrens@Sun.COM 31079396SMatthew.Ahrens@Sun.COM /* 31089396SMatthew.Ahrens@Sun.COM * inputs: 31099396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 31109396SMatthew.Ahrens@Sun.COM * 31119396SMatthew.Ahrens@Sun.COM * outputs: 31129396SMatthew.Ahrens@Sun.COM * none 31139396SMatthew.Ahrens@Sun.COM */ 31149396SMatthew.Ahrens@Sun.COM static int 31159396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 31169396SMatthew.Ahrens@Sun.COM { 31179396SMatthew.Ahrens@Sun.COM objset_t *os; 31189396SMatthew.Ahrens@Sun.COM int error; 31199396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 31209396SMatthew.Ahrens@Sun.COM 31219396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 312210298SMatthew.Ahrens@Sun.COM if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 31239396SMatthew.Ahrens@Sun.COM /* 31249396SMatthew.Ahrens@Sun.COM * If userused is not enabled, it may be because the 31259396SMatthew.Ahrens@Sun.COM * objset needs to be closed & reopened (to grow the 31269396SMatthew.Ahrens@Sun.COM * objset_phys_t). Suspend/resume the fs will do that. 31279396SMatthew.Ahrens@Sun.COM */ 312810298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 312910298SMatthew.Ahrens@Sun.COM if (error == 0) 313010298SMatthew.Ahrens@Sun.COM error = zfs_resume_fs(zfsvfs, zc->zc_name); 31319396SMatthew.Ahrens@Sun.COM } 31329396SMatthew.Ahrens@Sun.COM if (error == 0) 31339396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 31349396SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 31359396SMatthew.Ahrens@Sun.COM } else { 313610298SMatthew.Ahrens@Sun.COM /* XXX kind of reading contents without owning */ 313710298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 31389396SMatthew.Ahrens@Sun.COM if (error) 31399396SMatthew.Ahrens@Sun.COM return (error); 31409396SMatthew.Ahrens@Sun.COM 31419396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(os); 314210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 31439396SMatthew.Ahrens@Sun.COM } 31449396SMatthew.Ahrens@Sun.COM 31459396SMatthew.Ahrens@Sun.COM return (error); 31469396SMatthew.Ahrens@Sun.COM } 31479396SMatthew.Ahrens@Sun.COM 31489396SMatthew.Ahrens@Sun.COM /* 31494543Smarks * We don't want to have a hard dependency 31504543Smarks * against some special symbols in sharefs 31515331Samw * nfs, and smbsrv. Determine them if needed when 31524543Smarks * the first file system is shared. 31535331Samw * Neither sharefs, nfs or smbsrv are unloadable modules. 31544543Smarks */ 31555331Samw int (*znfsexport_fs)(void *arg); 31564543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 31575331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 31585331Samw 31595331Samw int zfs_nfsshare_inited; 31605331Samw int zfs_smbshare_inited; 31615331Samw 31624543Smarks ddi_modhandle_t nfs_mod; 31634543Smarks ddi_modhandle_t sharefs_mod; 31645331Samw ddi_modhandle_t smbsrv_mod; 31654543Smarks kmutex_t zfs_share_lock; 31664543Smarks 31674543Smarks static int 31685331Samw zfs_init_sharefs() 31695331Samw { 31705331Samw int error; 31715331Samw 31725331Samw ASSERT(MUTEX_HELD(&zfs_share_lock)); 31735331Samw /* Both NFS and SMB shares also require sharetab support. */ 31745331Samw if (sharefs_mod == NULL && ((sharefs_mod = 31755331Samw ddi_modopen("fs/sharefs", 31765331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 31775331Samw return (ENOSYS); 31785331Samw } 31795331Samw if (zshare_fs == NULL && ((zshare_fs = 31805331Samw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 31815331Samw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 31825331Samw return (ENOSYS); 31835331Samw } 31845331Samw return (0); 31855331Samw } 31865331Samw 31875331Samw static int 31884543Smarks zfs_ioc_share(zfs_cmd_t *zc) 31894543Smarks { 31904543Smarks int error; 31914543Smarks int opcode; 31924543Smarks 31935331Samw switch (zc->zc_share.z_sharetype) { 31945331Samw case ZFS_SHARE_NFS: 31955331Samw case ZFS_UNSHARE_NFS: 31965331Samw if (zfs_nfsshare_inited == 0) { 31975331Samw mutex_enter(&zfs_share_lock); 31985331Samw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 31995331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 32005331Samw mutex_exit(&zfs_share_lock); 32015331Samw return (ENOSYS); 32025331Samw } 32035331Samw if (znfsexport_fs == NULL && 32045331Samw ((znfsexport_fs = (int (*)(void *)) 32055331Samw ddi_modsym(nfs_mod, 32065331Samw "nfs_export", &error)) == NULL)) { 32075331Samw mutex_exit(&zfs_share_lock); 32085331Samw return (ENOSYS); 32095331Samw } 32105331Samw error = zfs_init_sharefs(); 32115331Samw if (error) { 32125331Samw mutex_exit(&zfs_share_lock); 32135331Samw return (ENOSYS); 32145331Samw } 32155331Samw zfs_nfsshare_inited = 1; 32164543Smarks mutex_exit(&zfs_share_lock); 32174543Smarks } 32185331Samw break; 32195331Samw case ZFS_SHARE_SMB: 32205331Samw case ZFS_UNSHARE_SMB: 32215331Samw if (zfs_smbshare_inited == 0) { 32225331Samw mutex_enter(&zfs_share_lock); 32235331Samw if (smbsrv_mod == NULL && ((smbsrv_mod = 32245331Samw ddi_modopen("drv/smbsrv", 32255331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 32265331Samw mutex_exit(&zfs_share_lock); 32275331Samw return (ENOSYS); 32285331Samw } 32295331Samw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 32305331Samw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 32316139Sjb150015 "smb_server_share", &error)) == NULL)) { 32325331Samw mutex_exit(&zfs_share_lock); 32335331Samw return (ENOSYS); 32345331Samw } 32355331Samw error = zfs_init_sharefs(); 32365331Samw if (error) { 32375331Samw mutex_exit(&zfs_share_lock); 32385331Samw return (ENOSYS); 32395331Samw } 32405331Samw zfs_smbshare_inited = 1; 32414543Smarks mutex_exit(&zfs_share_lock); 32424543Smarks } 32435331Samw break; 32445331Samw default: 32455331Samw return (EINVAL); 32464543Smarks } 32474543Smarks 32485331Samw switch (zc->zc_share.z_sharetype) { 32495331Samw case ZFS_SHARE_NFS: 32505331Samw case ZFS_UNSHARE_NFS: 32515331Samw if (error = 32525331Samw znfsexport_fs((void *) 32535331Samw (uintptr_t)zc->zc_share.z_exportdata)) 32545331Samw return (error); 32555331Samw break; 32565331Samw case ZFS_SHARE_SMB: 32575331Samw case ZFS_UNSHARE_SMB: 32585331Samw if (error = zsmbexport_fs((void *) 32595331Samw (uintptr_t)zc->zc_share.z_exportdata, 32605331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 32618845Samw@Sun.COM B_TRUE: B_FALSE)) { 32625331Samw return (error); 32635331Samw } 32645331Samw break; 32655331Samw } 32665331Samw 32675331Samw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 32685331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 32694543Smarks SHAREFS_ADD : SHAREFS_REMOVE; 32704543Smarks 32715331Samw /* 32725331Samw * Add or remove share from sharetab 32735331Samw */ 32744543Smarks error = zshare_fs(opcode, 32754543Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 32764543Smarks zc->zc_share.z_sharemax); 32774543Smarks 32784543Smarks return (error); 32794543Smarks 32804543Smarks } 32814543Smarks 32828845Samw@Sun.COM ace_t full_access[] = { 32838845Samw@Sun.COM {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 32848845Samw@Sun.COM }; 32858845Samw@Sun.COM 32868845Samw@Sun.COM /* 32878845Samw@Sun.COM * Remove all ACL files in shares dir 32888845Samw@Sun.COM */ 32898845Samw@Sun.COM static int 32908845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp) 32918845Samw@Sun.COM { 32928845Samw@Sun.COM zap_cursor_t zc; 32938845Samw@Sun.COM zap_attribute_t zap; 32948845Samw@Sun.COM zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 32958845Samw@Sun.COM int error; 32968845Samw@Sun.COM 32978845Samw@Sun.COM for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 32988845Samw@Sun.COM (error = zap_cursor_retrieve(&zc, &zap)) == 0; 32998845Samw@Sun.COM zap_cursor_advance(&zc)) { 33008845Samw@Sun.COM if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 33018845Samw@Sun.COM NULL, 0)) != 0) 33028845Samw@Sun.COM break; 33038845Samw@Sun.COM } 33048845Samw@Sun.COM zap_cursor_fini(&zc); 33058845Samw@Sun.COM return (error); 33068845Samw@Sun.COM } 33078845Samw@Sun.COM 33088845Samw@Sun.COM static int 33098845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc) 33108845Samw@Sun.COM { 33118845Samw@Sun.COM vnode_t *vp; 33128845Samw@Sun.COM znode_t *dzp; 33138845Samw@Sun.COM vnode_t *resourcevp = NULL; 33148845Samw@Sun.COM znode_t *sharedir; 33158845Samw@Sun.COM zfsvfs_t *zfsvfs; 33168845Samw@Sun.COM nvlist_t *nvlist; 33178845Samw@Sun.COM char *src, *target; 33188845Samw@Sun.COM vattr_t vattr; 33198845Samw@Sun.COM vsecattr_t vsec; 33208845Samw@Sun.COM int error = 0; 33218845Samw@Sun.COM 33228845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 33238845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 33248845Samw@Sun.COM return (error); 33258845Samw@Sun.COM 33268845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 33278845Samw@Sun.COM 33288845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 33298845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 33308845Samw@Sun.COM zc->zc_name) != 0)) { 33318845Samw@Sun.COM VN_RELE(vp); 33328845Samw@Sun.COM return (EINVAL); 33338845Samw@Sun.COM } 33348845Samw@Sun.COM 33358845Samw@Sun.COM dzp = VTOZ(vp); 33368845Samw@Sun.COM zfsvfs = dzp->z_zfsvfs; 33378845Samw@Sun.COM ZFS_ENTER(zfsvfs); 33388845Samw@Sun.COM 33399030SMark.Shellenbaum@Sun.COM /* 33409030SMark.Shellenbaum@Sun.COM * Create share dir if its missing. 33419030SMark.Shellenbaum@Sun.COM */ 33429030SMark.Shellenbaum@Sun.COM mutex_enter(&zfsvfs->z_lock); 33439030SMark.Shellenbaum@Sun.COM if (zfsvfs->z_shares_dir == 0) { 33449030SMark.Shellenbaum@Sun.COM dmu_tx_t *tx; 33459030SMark.Shellenbaum@Sun.COM 33469030SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(zfsvfs->z_os); 33479030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 33489030SMark.Shellenbaum@Sun.COM ZFS_SHARES_DIR); 33499030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 33509030SMark.Shellenbaum@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 33519030SMark.Shellenbaum@Sun.COM if (error) { 33529030SMark.Shellenbaum@Sun.COM dmu_tx_abort(tx); 33539030SMark.Shellenbaum@Sun.COM } else { 33549030SMark.Shellenbaum@Sun.COM error = zfs_create_share_dir(zfsvfs, tx); 33559030SMark.Shellenbaum@Sun.COM dmu_tx_commit(tx); 33569030SMark.Shellenbaum@Sun.COM } 33579030SMark.Shellenbaum@Sun.COM if (error) { 33589030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 33599030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 33609030SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 33619030SMark.Shellenbaum@Sun.COM return (error); 33629030SMark.Shellenbaum@Sun.COM } 33639030SMark.Shellenbaum@Sun.COM } 33649030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 33659030SMark.Shellenbaum@Sun.COM 33669030SMark.Shellenbaum@Sun.COM ASSERT(zfsvfs->z_shares_dir); 33678845Samw@Sun.COM if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 33689030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 33698845Samw@Sun.COM ZFS_EXIT(zfsvfs); 33708845Samw@Sun.COM return (error); 33718845Samw@Sun.COM } 33728845Samw@Sun.COM 33738845Samw@Sun.COM switch (zc->zc_cookie) { 33748845Samw@Sun.COM case ZFS_SMB_ACL_ADD: 33758845Samw@Sun.COM vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 33768845Samw@Sun.COM vattr.va_type = VREG; 33778845Samw@Sun.COM vattr.va_mode = S_IFREG|0777; 33788845Samw@Sun.COM vattr.va_uid = 0; 33798845Samw@Sun.COM vattr.va_gid = 0; 33808845Samw@Sun.COM 33818845Samw@Sun.COM vsec.vsa_mask = VSA_ACE; 33828845Samw@Sun.COM vsec.vsa_aclentp = &full_access; 33838845Samw@Sun.COM vsec.vsa_aclentsz = sizeof (full_access); 33848845Samw@Sun.COM vsec.vsa_aclcnt = 1; 33858845Samw@Sun.COM 33868845Samw@Sun.COM error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 33878845Samw@Sun.COM &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 33888845Samw@Sun.COM if (resourcevp) 33898845Samw@Sun.COM VN_RELE(resourcevp); 33908845Samw@Sun.COM break; 33918845Samw@Sun.COM 33928845Samw@Sun.COM case ZFS_SMB_ACL_REMOVE: 33938845Samw@Sun.COM error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 33948845Samw@Sun.COM NULL, 0); 33958845Samw@Sun.COM break; 33968845Samw@Sun.COM 33978845Samw@Sun.COM case ZFS_SMB_ACL_RENAME: 33988845Samw@Sun.COM if ((error = get_nvlist(zc->zc_nvlist_src, 33999643SEric.Taylor@Sun.COM zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 34008845Samw@Sun.COM VN_RELE(vp); 34018845Samw@Sun.COM ZFS_EXIT(zfsvfs); 34028845Samw@Sun.COM return (error); 34038845Samw@Sun.COM } 34048845Samw@Sun.COM if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 34058845Samw@Sun.COM nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 34068845Samw@Sun.COM &target)) { 34078845Samw@Sun.COM VN_RELE(vp); 34089179SMark.Shellenbaum@Sun.COM VN_RELE(ZTOV(sharedir)); 34098845Samw@Sun.COM ZFS_EXIT(zfsvfs); 34108845Samw@Sun.COM return (error); 34118845Samw@Sun.COM } 34128845Samw@Sun.COM error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 34138845Samw@Sun.COM kcred, NULL, 0); 34148845Samw@Sun.COM nvlist_free(nvlist); 34158845Samw@Sun.COM break; 34168845Samw@Sun.COM 34178845Samw@Sun.COM case ZFS_SMB_ACL_PURGE: 34188845Samw@Sun.COM error = zfs_smb_acl_purge(sharedir); 34198845Samw@Sun.COM break; 34208845Samw@Sun.COM 34218845Samw@Sun.COM default: 34228845Samw@Sun.COM error = EINVAL; 34238845Samw@Sun.COM break; 34248845Samw@Sun.COM } 34258845Samw@Sun.COM 34268845Samw@Sun.COM VN_RELE(vp); 34278845Samw@Sun.COM VN_RELE(ZTOV(sharedir)); 34288845Samw@Sun.COM 34298845Samw@Sun.COM ZFS_EXIT(zfsvfs); 34308845Samw@Sun.COM 34318845Samw@Sun.COM return (error); 34328845Samw@Sun.COM } 34338845Samw@Sun.COM 34344543Smarks /* 343510242Schris.kirby@sun.com * inputs: 343610242Schris.kirby@sun.com * zc_name name of filesystem 343710242Schris.kirby@sun.com * zc_value short name of snap 343810242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 343910242Schris.kirby@sun.com * zc_cookie recursive flag 344010342Schris.kirby@sun.com * zc_temphold set if hold is temporary 344110242Schris.kirby@sun.com * 344210242Schris.kirby@sun.com * outputs: none 344310242Schris.kirby@sun.com */ 344410242Schris.kirby@sun.com static int 344510242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc) 344610242Schris.kirby@sun.com { 344710242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 344810242Schris.kirby@sun.com 344910242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 345010242Schris.kirby@sun.com return (EINVAL); 345110242Schris.kirby@sun.com 345210242Schris.kirby@sun.com return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value, 345310342Schris.kirby@sun.com zc->zc_string, recursive, zc->zc_temphold)); 345410242Schris.kirby@sun.com } 345510242Schris.kirby@sun.com 345610242Schris.kirby@sun.com /* 345710242Schris.kirby@sun.com * inputs: 345810242Schris.kirby@sun.com * zc_name name of dataset from which we're releasing a user reference 345910242Schris.kirby@sun.com * zc_value short name of snap 346010242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 346110242Schris.kirby@sun.com * zc_cookie recursive flag 346210242Schris.kirby@sun.com * 346310242Schris.kirby@sun.com * outputs: none 346410242Schris.kirby@sun.com */ 346510242Schris.kirby@sun.com static int 346610242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc) 346710242Schris.kirby@sun.com { 346810242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 346910242Schris.kirby@sun.com 347010242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 347110242Schris.kirby@sun.com return (EINVAL); 347210242Schris.kirby@sun.com 347310242Schris.kirby@sun.com return (dsl_dataset_user_release(zc->zc_name, zc->zc_value, 347410242Schris.kirby@sun.com zc->zc_string, recursive)); 347510242Schris.kirby@sun.com } 347610242Schris.kirby@sun.com 347710242Schris.kirby@sun.com /* 347810242Schris.kirby@sun.com * inputs: 347910242Schris.kirby@sun.com * zc_name name of filesystem 348010242Schris.kirby@sun.com * 348110242Schris.kirby@sun.com * outputs: 348210242Schris.kirby@sun.com * zc_nvlist_src{_size} nvlist of snapshot holds 348310242Schris.kirby@sun.com */ 348410242Schris.kirby@sun.com static int 348510242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc) 348610242Schris.kirby@sun.com { 348710242Schris.kirby@sun.com nvlist_t *nvp; 348810242Schris.kirby@sun.com int error; 348910242Schris.kirby@sun.com 349010242Schris.kirby@sun.com if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) { 349110242Schris.kirby@sun.com error = put_nvlist(zc, nvp); 349210242Schris.kirby@sun.com nvlist_free(nvp); 349310242Schris.kirby@sun.com } 349410242Schris.kirby@sun.com 349510242Schris.kirby@sun.com return (error); 349610242Schris.kirby@sun.com } 349710242Schris.kirby@sun.com 349810242Schris.kirby@sun.com /* 34994988Sek110237 * pool create, destroy, and export don't log the history as part of 35004988Sek110237 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 35014988Sek110237 * do the logging of those commands. 35024543Smarks */ 3503789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 35049234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35059234SGeorge.Wilson@Sun.COM B_FALSE }, 35069234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35079234SGeorge.Wilson@Sun.COM B_FALSE }, 35089234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35099234SGeorge.Wilson@Sun.COM B_FALSE }, 35109234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35119234SGeorge.Wilson@Sun.COM B_FALSE }, 35129234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE, 35139234SGeorge.Wilson@Sun.COM B_FALSE }, 35149234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE, 35159234SGeorge.Wilson@Sun.COM B_FALSE }, 35169234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE, 35179234SGeorge.Wilson@Sun.COM B_FALSE }, 35189234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35199234SGeorge.Wilson@Sun.COM B_TRUE }, 35209234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE, 35219234SGeorge.Wilson@Sun.COM B_FALSE }, 35229234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35239234SGeorge.Wilson@Sun.COM B_TRUE }, 35249234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35259234SGeorge.Wilson@Sun.COM B_FALSE }, 35269234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35279234SGeorge.Wilson@Sun.COM B_TRUE }, 35289234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35299234SGeorge.Wilson@Sun.COM B_TRUE }, 35309234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35319234SGeorge.Wilson@Sun.COM B_FALSE }, 35329234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35339234SGeorge.Wilson@Sun.COM B_TRUE }, 35349234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35359234SGeorge.Wilson@Sun.COM B_TRUE }, 35369234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35379234SGeorge.Wilson@Sun.COM B_TRUE }, 35389425SEric.Schrock@Sun.COM { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35399425SEric.Schrock@Sun.COM B_TRUE }, 35409234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35419234SGeorge.Wilson@Sun.COM B_FALSE }, 35429234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35439234SGeorge.Wilson@Sun.COM B_FALSE }, 35449234SGeorge.Wilson@Sun.COM { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35459234SGeorge.Wilson@Sun.COM B_FALSE }, 35469234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35479234SGeorge.Wilson@Sun.COM B_FALSE }, 35489234SGeorge.Wilson@Sun.COM { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE }, 35499234SGeorge.Wilson@Sun.COM { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE }, 35509234SGeorge.Wilson@Sun.COM { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 35519234SGeorge.Wilson@Sun.COM B_TRUE}, 35529234SGeorge.Wilson@Sun.COM { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE, 35539234SGeorge.Wilson@Sun.COM B_TRUE }, 35549234SGeorge.Wilson@Sun.COM { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE }, 35559234SGeorge.Wilson@Sun.COM { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE }, 35569234SGeorge.Wilson@Sun.COM { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE }, 35579234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 35589234SGeorge.Wilson@Sun.COM B_FALSE }, 35599234SGeorge.Wilson@Sun.COM { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 35609234SGeorge.Wilson@Sun.COM B_FALSE }, 35619234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE, 35629234SGeorge.Wilson@Sun.COM B_FALSE }, 35639234SGeorge.Wilson@Sun.COM { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE, 35649234SGeorge.Wilson@Sun.COM B_FALSE }, 35659234SGeorge.Wilson@Sun.COM { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE }, 35669234SGeorge.Wilson@Sun.COM { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE, 35679234SGeorge.Wilson@Sun.COM B_TRUE }, 35689234SGeorge.Wilson@Sun.COM { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 35699234SGeorge.Wilson@Sun.COM B_TRUE }, 35709234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE, 35719234SGeorge.Wilson@Sun.COM B_TRUE }, 35729234SGeorge.Wilson@Sun.COM { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE, 35739234SGeorge.Wilson@Sun.COM B_FALSE }, 357410233SGeorge.Wilson@Sun.COM { zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE, 357510233SGeorge.Wilson@Sun.COM B_TRUE }, 35769234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE, 35779234SGeorge.Wilson@Sun.COM B_TRUE }, 35789234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE, 35799234SGeorge.Wilson@Sun.COM B_FALSE }, 35809234SGeorge.Wilson@Sun.COM { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE, 35819234SGeorge.Wilson@Sun.COM B_TRUE }, 35829234SGeorge.Wilson@Sun.COM { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 35839234SGeorge.Wilson@Sun.COM B_FALSE }, 35849234SGeorge.Wilson@Sun.COM { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE, 35859234SGeorge.Wilson@Sun.COM B_FALSE }, 35869234SGeorge.Wilson@Sun.COM { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE }, 35879234SGeorge.Wilson@Sun.COM { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE, 35889234SGeorge.Wilson@Sun.COM B_TRUE }, 35899234SGeorge.Wilson@Sun.COM { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE, 35909396SMatthew.Ahrens@Sun.COM B_FALSE }, 35919396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, 35929396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 35939396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, 35949396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 35959396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 35969396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_TRUE }, 359710242Schris.kirby@sun.com { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE }, 359810242Schris.kirby@sun.com { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE, 359910242Schris.kirby@sun.com B_TRUE }, 360010242Schris.kirby@sun.com { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 360110242Schris.kirby@sun.com B_TRUE } 3602789Sahrens }; 3603789Sahrens 36049234SGeorge.Wilson@Sun.COM int 36059234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type) 36069234SGeorge.Wilson@Sun.COM { 36079234SGeorge.Wilson@Sun.COM spa_t *spa; 36089234SGeorge.Wilson@Sun.COM int error; 36099234SGeorge.Wilson@Sun.COM 36109234SGeorge.Wilson@Sun.COM ASSERT(type == POOL_NAME || type == DATASET_NAME); 36119234SGeorge.Wilson@Sun.COM 36129396SMatthew.Ahrens@Sun.COM error = spa_open(name, &spa, FTAG); 36139234SGeorge.Wilson@Sun.COM if (error == 0) { 36149234SGeorge.Wilson@Sun.COM if (spa_suspended(spa)) 36159234SGeorge.Wilson@Sun.COM error = EAGAIN; 36169234SGeorge.Wilson@Sun.COM spa_close(spa, FTAG); 36179234SGeorge.Wilson@Sun.COM } 36189234SGeorge.Wilson@Sun.COM return (error); 36199234SGeorge.Wilson@Sun.COM } 36209234SGeorge.Wilson@Sun.COM 3621789Sahrens static int 3622789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 3623789Sahrens { 3624789Sahrens zfs_cmd_t *zc; 3625789Sahrens uint_t vec; 36262199Sahrens int error, rc; 3627789Sahrens 3628789Sahrens if (getminor(dev) != 0) 3629789Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 3630789Sahrens 3631789Sahrens vec = cmd - ZFS_IOC; 36324787Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 3633789Sahrens 3634789Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 3635789Sahrens return (EINVAL); 3636789Sahrens 3637789Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 3638789Sahrens 36399643SEric.Taylor@Sun.COM error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 3640789Sahrens 3641*10588SEric.Taylor@Sun.COM if ((error == 0) && !(flag & FKIOCTL)) 36424543Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 3643789Sahrens 3644789Sahrens /* 3645789Sahrens * Ensure that all pool/dataset names are valid before we pass down to 3646789Sahrens * the lower layers. 3647789Sahrens */ 3648789Sahrens if (error == 0) { 3649789Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 36509643SEric.Taylor@Sun.COM zc->zc_iflags = flag & FKIOCTL; 3651789Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 36524577Sahrens case POOL_NAME: 3653789Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 3654789Sahrens error = EINVAL; 36559234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 36569234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 36579234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 3658789Sahrens break; 3659789Sahrens 36604577Sahrens case DATASET_NAME: 3661789Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 3662789Sahrens error = EINVAL; 36639234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 36649234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 36659234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 3666789Sahrens break; 36672856Snd150628 36684577Sahrens case NO_NAME: 36692856Snd150628 break; 3670789Sahrens } 3671789Sahrens } 3672789Sahrens 3673789Sahrens if (error == 0) 3674789Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 3675789Sahrens 36769643SEric.Taylor@Sun.COM rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 36774543Smarks if (error == 0) { 36782199Sahrens error = rc; 36799396SMatthew.Ahrens@Sun.COM if (zfs_ioc_vec[vec].zvec_his_log) 36804543Smarks zfs_log_history(zc); 36814543Smarks } 3682789Sahrens 3683789Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 3684789Sahrens return (error); 3685789Sahrens } 3686789Sahrens 3687789Sahrens static int 3688789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 3689789Sahrens { 3690789Sahrens if (cmd != DDI_ATTACH) 3691789Sahrens return (DDI_FAILURE); 3692789Sahrens 3693789Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 3694789Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 3695789Sahrens return (DDI_FAILURE); 3696789Sahrens 3697789Sahrens zfs_dip = dip; 3698789Sahrens 3699789Sahrens ddi_report_dev(dip); 3700789Sahrens 3701789Sahrens return (DDI_SUCCESS); 3702789Sahrens } 3703789Sahrens 3704789Sahrens static int 3705789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3706789Sahrens { 3707789Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 3708789Sahrens return (DDI_FAILURE); 3709789Sahrens 3710789Sahrens if (cmd != DDI_DETACH) 3711789Sahrens return (DDI_FAILURE); 3712789Sahrens 3713789Sahrens zfs_dip = NULL; 3714789Sahrens 3715789Sahrens ddi_prop_remove_all(dip); 3716789Sahrens ddi_remove_minor_node(dip, NULL); 3717789Sahrens 3718789Sahrens return (DDI_SUCCESS); 3719789Sahrens } 3720789Sahrens 3721789Sahrens /*ARGSUSED*/ 3722789Sahrens static int 3723789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 3724789Sahrens { 3725789Sahrens switch (infocmd) { 3726789Sahrens case DDI_INFO_DEVT2DEVINFO: 3727789Sahrens *result = zfs_dip; 3728789Sahrens return (DDI_SUCCESS); 3729789Sahrens 3730789Sahrens case DDI_INFO_DEVT2INSTANCE: 3731849Sbonwick *result = (void *)0; 3732789Sahrens return (DDI_SUCCESS); 3733789Sahrens } 3734789Sahrens 3735789Sahrens return (DDI_FAILURE); 3736789Sahrens } 3737789Sahrens 3738789Sahrens /* 3739789Sahrens * OK, so this is a little weird. 3740789Sahrens * 3741789Sahrens * /dev/zfs is the control node, i.e. minor 0. 3742789Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 3743789Sahrens * 3744789Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 3745789Sahrens * so most of the standard driver entry points are in zvol.c. 3746789Sahrens */ 3747789Sahrens static struct cb_ops zfs_cb_ops = { 3748789Sahrens zvol_open, /* open */ 3749789Sahrens zvol_close, /* close */ 3750789Sahrens zvol_strategy, /* strategy */ 3751789Sahrens nodev, /* print */ 37526423Sgw25295 zvol_dump, /* dump */ 3753789Sahrens zvol_read, /* read */ 3754789Sahrens zvol_write, /* write */ 3755789Sahrens zfsdev_ioctl, /* ioctl */ 3756789Sahrens nodev, /* devmap */ 3757789Sahrens nodev, /* mmap */ 3758789Sahrens nodev, /* segmap */ 3759789Sahrens nochpoll, /* poll */ 3760789Sahrens ddi_prop_op, /* prop_op */ 3761789Sahrens NULL, /* streamtab */ 3762789Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 3763789Sahrens CB_REV, /* version */ 37643638Sbillm nodev, /* async read */ 37653638Sbillm nodev, /* async write */ 3766789Sahrens }; 3767789Sahrens 3768789Sahrens static struct dev_ops zfs_dev_ops = { 3769789Sahrens DEVO_REV, /* version */ 3770789Sahrens 0, /* refcnt */ 3771789Sahrens zfs_info, /* info */ 3772789Sahrens nulldev, /* identify */ 3773789Sahrens nulldev, /* probe */ 3774789Sahrens zfs_attach, /* attach */ 3775789Sahrens zfs_detach, /* detach */ 3776789Sahrens nodev, /* reset */ 3777789Sahrens &zfs_cb_ops, /* driver operations */ 37787656SSherry.Moore@Sun.COM NULL, /* no bus operations */ 37797656SSherry.Moore@Sun.COM NULL, /* power */ 37807656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */ 3781789Sahrens }; 3782789Sahrens 3783789Sahrens static struct modldrv zfs_modldrv = { 37847656SSherry.Moore@Sun.COM &mod_driverops, 37857656SSherry.Moore@Sun.COM "ZFS storage pool", 37867656SSherry.Moore@Sun.COM &zfs_dev_ops 3787789Sahrens }; 3788789Sahrens 3789789Sahrens static struct modlinkage modlinkage = { 3790789Sahrens MODREV_1, 3791789Sahrens (void *)&zfs_modlfs, 3792789Sahrens (void *)&zfs_modldrv, 3793789Sahrens NULL 3794789Sahrens }; 3795789Sahrens 37964720Sfr157268 37974720Sfr157268 uint_t zfs_fsyncer_key; 37985326Sek110237 extern uint_t rrw_tsd_key; 37994720Sfr157268 3800789Sahrens int 3801789Sahrens _init(void) 3802789Sahrens { 3803789Sahrens int error; 3804789Sahrens 3805849Sbonwick spa_init(FREAD | FWRITE); 3806849Sbonwick zfs_init(); 3807849Sbonwick zvol_init(); 3808849Sbonwick 3809849Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 3810849Sbonwick zvol_fini(); 3811849Sbonwick zfs_fini(); 3812849Sbonwick spa_fini(); 3813789Sahrens return (error); 3814849Sbonwick } 3815789Sahrens 38164720Sfr157268 tsd_create(&zfs_fsyncer_key, NULL); 38175326Sek110237 tsd_create(&rrw_tsd_key, NULL); 38184720Sfr157268 3819789Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 3820789Sahrens ASSERT(error == 0); 38214543Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 3822789Sahrens 3823789Sahrens return (0); 3824789Sahrens } 3825789Sahrens 3826789Sahrens int 3827789Sahrens _fini(void) 3828789Sahrens { 3829789Sahrens int error; 3830789Sahrens 38311544Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 3832789Sahrens return (EBUSY); 3833789Sahrens 3834789Sahrens if ((error = mod_remove(&modlinkage)) != 0) 3835789Sahrens return (error); 3836789Sahrens 3837789Sahrens zvol_fini(); 3838789Sahrens zfs_fini(); 3839789Sahrens spa_fini(); 38405331Samw if (zfs_nfsshare_inited) 38414543Smarks (void) ddi_modclose(nfs_mod); 38425331Samw if (zfs_smbshare_inited) 38435331Samw (void) ddi_modclose(smbsrv_mod); 38445331Samw if (zfs_nfsshare_inited || zfs_smbshare_inited) 38454543Smarks (void) ddi_modclose(sharefs_mod); 3846789Sahrens 38474720Sfr157268 tsd_destroy(&zfs_fsyncer_key); 3848789Sahrens ldi_ident_release(zfs_li); 3849789Sahrens zfs_li = NULL; 38504543Smarks mutex_destroy(&zfs_share_lock); 3851789Sahrens 3852789Sahrens return (error); 3853789Sahrens } 3854789Sahrens 3855789Sahrens int 3856789Sahrens _info(struct modinfo *modinfop) 3857789Sahrens { 3858789Sahrens return (mod_info(&modlinkage, modinfop)); 3859789Sahrens } 3860