Home | History | Annotate | Download | only in zfs
      1    789    ahrens /*
      2    789    ahrens  * CDDL HEADER START
      3    789    ahrens  *
      4    789    ahrens  * The contents of this file are subject to the terms of the
      5   1485     lling  * Common Development and Distribution License (the "License").
      6   1485     lling  * You may not use this file except in compliance with the License.
      7    789    ahrens  *
      8    789    ahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9    789    ahrens  * or http://www.opensolaris.org/os/licensing.
     10    789    ahrens  * See the License for the specific language governing permissions
     11    789    ahrens  * and limitations under the License.
     12    789    ahrens  *
     13    789    ahrens  * When distributing Covered Code, include this CDDL HEADER in each
     14    789    ahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15    789    ahrens  * If applicable, add the following below this CDDL HEADER, with the
     16    789    ahrens  * fields enclosed by brackets "[]" replaced with your own identifying
     17    789    ahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
     18    789    ahrens  *
     19    789    ahrens  * CDDL HEADER END
     20    789    ahrens  */
     21    789    ahrens /*
     22   8525      Eric  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23    789    ahrens  * Use is subject to license terms.
     24    789    ahrens  */
     25    789    ahrens 
     26    789    ahrens #include <sys/types.h>
     27    789    ahrens #include <sys/param.h>
     28    789    ahrens #include <sys/errno.h>
     29    789    ahrens #include <sys/uio.h>
     30    789    ahrens #include <sys/buf.h>
     31    789    ahrens #include <sys/modctl.h>
     32    789    ahrens #include <sys/open.h>
     33    789    ahrens #include <sys/file.h>
     34    789    ahrens #include <sys/kmem.h>
     35    789    ahrens #include <sys/conf.h>
     36    789    ahrens #include <sys/cmn_err.h>
     37    789    ahrens #include <sys/stat.h>
     38    789    ahrens #include <sys/zfs_ioctl.h>
     39  10972       Ric #include <sys/zfs_vfsops.h>
     40   5331       amw #include <sys/zfs_znode.h>
     41    789    ahrens #include <sys/zap.h>
     42    789    ahrens #include <sys/spa.h>
     43   3912     lling #include <sys/spa_impl.h>
     44    789    ahrens #include <sys/vdev.h>
     45  10972       Ric #include <sys/priv_impl.h>
     46    789    ahrens #include <sys/dmu.h>
     47    789    ahrens #include <sys/dsl_dir.h>
     48    789    ahrens #include <sys/dsl_dataset.h>
     49    789    ahrens #include <sys/dsl_prop.h>
     50   4543     marks #include <sys/dsl_deleg.h>
     51   4543     marks #include <sys/dmu_objset.h>
     52    789    ahrens #include <sys/ddi.h>
     53    789    ahrens #include <sys/sunddi.h>
     54    789    ahrens #include <sys/sunldi.h>
     55    789    ahrens #include <sys/policy.h>
     56    789    ahrens #include <sys/zone.h>
     57    789    ahrens #include <sys/nvpair.h>
     58    789    ahrens #include <sys/pathname.h>
     59    789    ahrens #include <sys/mount.h>
     60    789    ahrens #include <sys/sdt.h>
     61    789    ahrens #include <sys/fs/zfs.h>
     62    789    ahrens #include <sys/zfs_ctldir.h>
     63   5331       amw #include <sys/zfs_dir.h>
     64   2885    ahrens #include <sys/zvol.h>
     65   4543     marks #include <sharefs/share.h>
     66   5326  ek110237 #include <sys/dmu_objset.h>
     67    789    ahrens 
     68    789    ahrens #include "zfs_namecheck.h"
     69   2676  eschrock #include "zfs_prop.h"
     70   4543     marks #include "zfs_deleg.h"
     71    789    ahrens 
     72    789    ahrens extern struct modlfs zfs_modlfs;
     73    789    ahrens 
     74    789    ahrens extern void zfs_init(void);
     75    789    ahrens extern void zfs_fini(void);
     76    789    ahrens 
     77    789    ahrens ldi_ident_t zfs_li = NULL;
     78    789    ahrens dev_info_t *zfs_dip;
     79    789    ahrens 
     80    789    ahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
     81   4543     marks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
     82    789    ahrens 
     83   9234    George typedef enum {
     84   9234    George 	NO_NAME,
     85   9234    George 	POOL_NAME,
     86   9234    George 	DATASET_NAME
     87   9234    George } zfs_ioc_namecheck_t;
     88   9234    George 
     89    789    ahrens typedef struct zfs_ioc_vec {
     90    789    ahrens 	zfs_ioc_func_t		*zvec_func;
     91    789    ahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
     92   9234    George 	zfs_ioc_namecheck_t	zvec_namecheck;
     93   4543     marks 	boolean_t		zvec_his_log;
     94   9234    George 	boolean_t		zvec_pool_check;
     95    789    ahrens } zfs_ioc_vec_t;
     96    789    ahrens 
     97   9396   Matthew /* This array is indexed by zfs_userquota_prop_t */
     98   9396   Matthew static const char *userquota_perms[] = {
     99   9396   Matthew 	ZFS_DELEG_PERM_USERUSED,
    100   9396   Matthew 	ZFS_DELEG_PERM_USERQUOTA,
    101   9396   Matthew 	ZFS_DELEG_PERM_GROUPUSED,
    102   9396   Matthew 	ZFS_DELEG_PERM_GROUPQUOTA,
    103   9396   Matthew };
    104   9396   Matthew 
    105   9396   Matthew static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
    106  11022       Tom static int zfs_check_settable(const char *name, nvpair_t *property,
    107  11022       Tom     cred_t *cr);
    108  11022       Tom static int zfs_check_clearable(char *dataset, nvlist_t *props,
    109  11022       Tom     nvlist_t **errors);
    110   7184      timh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
    111   7184      timh     boolean_t *);
    112  11022       Tom int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
    113   7184      timh 
    114    789    ahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
    115    789    ahrens void
    116    789    ahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
    117    789    ahrens {
    118    789    ahrens 	const char *newfile;
    119    789    ahrens 	char buf[256];
    120    789    ahrens 	va_list adx;
    121    789    ahrens 
    122    789    ahrens 	/*
    123    789    ahrens 	 * Get rid of annoying "../common/" prefix to filename.
    124    789    ahrens 	 */
    125    789    ahrens 	newfile = strrchr(file, '/');
    126    789    ahrens 	if (newfile != NULL) {
    127    789    ahrens 		newfile = newfile + 1; /* Get rid of leading / */
    128    789    ahrens 	} else {
    129    789    ahrens 		newfile = file;
    130    789    ahrens 	}
    131    789    ahrens 
    132    789    ahrens 	va_start(adx, fmt);
    133    789    ahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
    134    789    ahrens 	va_end(adx);
    135    789    ahrens 
    136    789    ahrens 	/*
    137    789    ahrens 	 * To get this data, use the zfs-dprintf probe as so:
    138    789    ahrens 	 * dtrace -q -n 'zfs-dprintf \
    139    789    ahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
    140    789    ahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
    141    789    ahrens 	 * arg0 = file name
    142    789    ahrens 	 * arg1 = function name
    143    789    ahrens 	 * arg2 = line number
    144    789    ahrens 	 * arg3 = message
    145    789    ahrens 	 */
    146    789    ahrens 	DTRACE_PROBE4(zfs__dprintf,
    147    789    ahrens 	    char *, newfile, char *, func, int, line, char *, buf);
    148    789    ahrens }
    149    789    ahrens 
    150   4543     marks static void
    151   4715  ek110237 history_str_free(char *buf)
    152   4715  ek110237 {
    153   4715  ek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
    154   4715  ek110237 }
    155   4715  ek110237 
    156   4715  ek110237 static char *
    157   4715  ek110237 history_str_get(zfs_cmd_t *zc)
    158   4715  ek110237 {
    159   4715  ek110237 	char *buf;
    160   4715  ek110237 
    161   4715  ek110237 	if (zc->zc_history == NULL)
    162   4715  ek110237 		return (NULL);
    163   4715  ek110237 
    164   4715  ek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
    165   4715  ek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
    166   4715  ek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
    167   4715  ek110237 		history_str_free(buf);
    168   4715  ek110237 		return (NULL);
    169   4715  ek110237 	}
    170   4715  ek110237 
    171   4715  ek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
    172   4715  ek110237 
    173   4715  ek110237 	return (buf);
    174   7042   gw25295 }
    175   7042   gw25295 
    176   7042   gw25295 /*
    177   7042   gw25295  * Check to see if the named dataset is currently defined as bootable
    178   7042   gw25295  */
    179   7042   gw25295 static boolean_t
    180   7042   gw25295 zfs_is_bootfs(const char *name)
    181   7042   gw25295 {
    182  10298   Matthew 	objset_t *os;
    183   7042   gw25295 
    184  10298   Matthew 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
    185  10298   Matthew 		boolean_t ret;
    186  10922      Jeff 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
    187  10298   Matthew 		dmu_objset_rele(os, FTAG);
    188  10298   Matthew 		return (ret);
    189   7042   gw25295 	}
    190  10298   Matthew 	return (B_FALSE);
    191   5331       amw }
    192   5331       amw 
    193   5375      timh /*
    194   7184      timh  * zfs_earlier_version
    195   5375      timh  *
    196   5375      timh  *	Return non-zero if the spa version is less than requested version.
    197   5375      timh  */
    198   5331       amw static int
    199   7184      timh zfs_earlier_version(const char *name, int version)
    200   5331       amw {
    201   5331       amw 	spa_t *spa;
    202   5331       amw 
    203   5331       amw 	if (spa_open(name, &spa, FTAG) == 0) {
    204   5331       amw 		if (spa_version(spa) < version) {
    205   5331       amw 			spa_close(spa, FTAG);
    206   5331       amw 			return (1);
    207   5331       amw 		}
    208   5331       amw 		spa_close(spa, FTAG);
    209   5331       amw 	}
    210   5331       amw 	return (0);
    211   5977     marks }
    212   5977     marks 
    213   5977     marks /*
    214   6689    maybee  * zpl_earlier_version
    215   5977     marks  *
    216   6689    maybee  * Return TRUE if the ZPL version is less than requested version.
    217   5977     marks  */
    218   6689    maybee static boolean_t
    219   6689    maybee zpl_earlier_version(const char *name, int version)
    220   5977     marks {
    221   5977     marks 	objset_t *os;
    222   6689    maybee 	boolean_t rc = B_TRUE;
    223   5977     marks 
    224  10298   Matthew 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
    225   6689    maybee 		uint64_t zplversion;
    226   5977     marks 
    227  10298   Matthew 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
    228  10298   Matthew 			dmu_objset_rele(os, FTAG);
    229  10298   Matthew 			return (B_TRUE);
    230  10298   Matthew 		}
    231  10298   Matthew 		/* XXX reading from non-owned objset */
    232   6689    maybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
    233   6689    maybee 			rc = zplversion < version;
    234  10298   Matthew 		dmu_objset_rele(os, FTAG);
    235   5977     marks 	}
    236   5977     marks 	return (rc);
    237   4715  ek110237 }
    238   4715  ek110237 
    239   4715  ek110237 static void
    240   4543     marks zfs_log_history(zfs_cmd_t *zc)
    241   4543     marks {
    242   4543     marks 	spa_t *spa;
    243   4603    ahrens 	char *buf;
    244   4543     marks 
    245   4715  ek110237 	if ((buf = history_str_get(zc)) == NULL)
    246   4543     marks 		return;
    247   4543     marks 
    248   4715  ek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
    249   4715  ek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
    250   4715  ek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
    251   4715  ek110237 		spa_close(spa, FTAG);
    252   4543     marks 	}
    253   4715  ek110237 	history_str_free(buf);
    254   4543     marks }
    255   4543     marks 
    256    789    ahrens /*
    257    789    ahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
    258    789    ahrens  * and can be used in the local zone, as there is no associated dataset.
    259    789    ahrens  */
    260    789    ahrens /* ARGSUSED */
    261    789    ahrens static int
    262   4543     marks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
    263    789    ahrens {
    264    789    ahrens 	return (0);
    265    789    ahrens }
    266    789    ahrens 
    267    789    ahrens /*
    268    789    ahrens  * Policy for dataset read operations (list children, get statistics).  Requires
    269    789    ahrens  * no privileges, but must be visible in the local zone.
    270    789    ahrens  */
    271    789    ahrens /* ARGSUSED */
    272    789    ahrens static int
    273   4543     marks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
    274    789    ahrens {
    275    789    ahrens 	if (INGLOBALZONE(curproc) ||
    276   4543     marks 	    zone_dataset_visible(zc->zc_name, NULL))
    277    789    ahrens 		return (0);
    278    789    ahrens 
    279    789    ahrens 	return (ENOENT);
    280    789    ahrens }
    281    789    ahrens 
    282    789    ahrens static int
    283    789    ahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
    284    789    ahrens {
    285    789    ahrens 	uint64_t zoned;
    286    789    ahrens 	int writable = 1;
    287    789    ahrens 
    288    789    ahrens 	/*
    289    789    ahrens 	 * The dataset must be visible by this zone -- check this first
    290    789    ahrens 	 * so they don't see EPERM on something they shouldn't know about.
    291    789    ahrens 	 */
    292    789    ahrens 	if (!INGLOBALZONE(curproc) &&
    293    789    ahrens 	    !zone_dataset_visible(dataset, &writable))
    294    789    ahrens 		return (ENOENT);
    295    789    ahrens 
    296    789    ahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
    297    789    ahrens 		return (ENOENT);
    298    789    ahrens 
    299    789    ahrens 	if (INGLOBALZONE(curproc)) {
    300    789    ahrens 		/*
    301    789    ahrens 		 * If the fs is zoned, only root can access it from the
    302    789    ahrens 		 * global zone.
    303    789    ahrens 		 */
    304    789    ahrens 		if (secpolicy_zfs(cr) && zoned)
    305    789    ahrens 			return (EPERM);
    306    789    ahrens 	} else {
    307    789    ahrens 		/*
    308    789    ahrens 		 * If we are in a local zone, the 'zoned' property must be set.
    309    789    ahrens 		 */
    310    789    ahrens 		if (!zoned)
    311    789    ahrens 			return (EPERM);
    312    789    ahrens 
    313    789    ahrens 		/* must be writable by this zone */
    314    789    ahrens 		if (!writable)
    315    789    ahrens 			return (EPERM);
    316    789    ahrens 	}
    317    789    ahrens 	return (0);
    318    789    ahrens }
    319    789    ahrens 
    320    789    ahrens int
    321   4543     marks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
    322    789    ahrens {
    323    789    ahrens 	int error;
    324    789    ahrens 
    325   4543     marks 	error = zfs_dozonecheck(name, cr);
    326   4543     marks 	if (error == 0) {
    327   4543     marks 		error = secpolicy_zfs(cr);
    328   4670    ahrens 		if (error)
    329   4543     marks 			error = dsl_deleg_access(name, perm, cr);
    330   4543     marks 	}
    331   4543     marks 	return (error);
    332   4543     marks }
    333   4543     marks 
    334  10972       Ric /*
    335  10972       Ric  * Policy for setting the security label property.
    336  10972       Ric  *
    337  10972       Ric  * Returns 0 for success, non-zero for access and other errors.
    338  11022       Tom  */
    339  11022       Tom static int
    340  11022       Tom zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
    341  10972       Ric {
    342  10972       Ric 	char		ds_hexsl[MAXNAMELEN];
    343  10972       Ric 	bslabel_t	ds_sl, new_sl;
    344  10972       Ric 	boolean_t	new_default = FALSE;
    345  10972       Ric 	uint64_t	zoned;
    346  10972       Ric 	int		needed_priv = -1;
    347  10972       Ric 	int		error;
    348  10972       Ric 
    349  10972       Ric 	/* First get the existing dataset label. */
    350  10972       Ric 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
    351  10972       Ric 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
    352  10972       Ric 	if (error)
    353  10972       Ric 		return (EPERM);
    354  10972       Ric 
    355  10972       Ric 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
    356  10972       Ric 		new_default = TRUE;
    357  10972       Ric 
    358  10972       Ric 	/* The label must be translatable */
    359  10972       Ric 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
    360  10972       Ric 		return (EINVAL);
    361  10972       Ric 
    362  10972       Ric 	/*
    363  10972       Ric 	 * In a non-global zone, disallow attempts to set a label that
    364  10972       Ric 	 * doesn't match that of the zone; otherwise no other checks
    365  10972       Ric 	 * are needed.
    366  10972       Ric 	 */
    367  10972       Ric 	if (!INGLOBALZONE(curproc)) {
    368  10972       Ric 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
    369  10972       Ric 			return (EPERM);
    370  10972       Ric 		return (0);
    371  10972       Ric 	}
    372  10972       Ric 
    373  10972       Ric 	/*
    374  10972       Ric 	 * For global-zone datasets (i.e., those whose zoned property is
    375  10972       Ric 	 * "off", verify that the specified new label is valid for the
    376  10972       Ric 	 * global zone.
    377  10972       Ric 	 */
    378  10972       Ric 	if (dsl_prop_get_integer(name,
    379  10972       Ric 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
    380  10972       Ric 		return (EPERM);
    381  10972       Ric 	if (!zoned) {
    382  10972       Ric 		if (zfs_check_global_label(name, strval) != 0)
    383  10972       Ric 			return (EPERM);
    384  10972       Ric 	}
    385  10972       Ric 
    386  10972       Ric 	/*
    387  10972       Ric 	 * If the existing dataset label is nondefault, check if the
    388  10972       Ric 	 * dataset is mounted (label cannot be changed while mounted).
    389  10972       Ric 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
    390  10972       Ric 	 * mounted (or isn't a dataset, doesn't exist, ...).
    391  10972       Ric 	 */
    392  10972       Ric 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
    393  11022       Tom 		objset_t *os;
    394  11022       Tom 		static char *setsl_tag = "setsl_tag";
    395  11022       Tom 
    396  10972       Ric 		/*
    397  10972       Ric 		 * Try to own the dataset; abort if there is any error,
    398  10972       Ric 		 * (e.g., already mounted, in use, or other error).
    399  10972       Ric 		 */
    400  10972       Ric 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
    401  11022       Tom 		    setsl_tag, &os);
    402  11022       Tom 		if (error)
    403  11022       Tom 			return (EPERM);
    404  11022       Tom 
    405  11022       Tom 		dmu_objset_disown(os, setsl_tag);
    406  10972       Ric 
    407  10972       Ric 		if (new_default) {
    408  10972       Ric 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
    409  10972       Ric 			goto out_check;
    410  10972       Ric 		}
    411  10972       Ric 
    412  10972       Ric 		if (hexstr_to_label(strval, &new_sl) != 0)
    413  10972       Ric 			return (EPERM);
    414  10972       Ric 
    415  10972       Ric 		if (blstrictdom(&ds_sl, &new_sl))
    416  10972       Ric 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
    417  10972       Ric 		else if (blstrictdom(&new_sl, &ds_sl))
    418  10972       Ric 			needed_priv = PRIV_FILE_UPGRADE_SL;
    419  10972       Ric 	} else {
    420  10972       Ric 		/* dataset currently has a default label */
    421  10972       Ric 		if (!new_default)
    422  10972       Ric 			needed_priv = PRIV_FILE_UPGRADE_SL;
    423  10972       Ric 	}
    424  10972       Ric 
    425  10972       Ric out_check:
    426  10972       Ric 	if (needed_priv != -1)
    427  10972       Ric 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
    428  10972       Ric 	return (0);
    429  10972       Ric }
    430  10972       Ric 
    431   4543     marks static int
    432  11022       Tom zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
    433  11022       Tom     cred_t *cr)
    434  11022       Tom {
    435  11022       Tom 	char *strval;
    436  11022       Tom 
    437   4543     marks 	/*
    438   4543     marks 	 * Check permissions for special properties.
    439   4543     marks 	 */
    440   4543     marks 	switch (prop) {
    441   4543     marks 	case ZFS_PROP_ZONED:
    442   4543     marks 		/*
    443   4543     marks 		 * Disallow setting of 'zoned' from within a local zone.
    444   4543     marks 		 */
    445   4543     marks 		if (!INGLOBALZONE(curproc))
    446   4543     marks 			return (EPERM);
    447   4543     marks 		break;
    448   4543     marks 
    449   4543     marks 	case ZFS_PROP_QUOTA:
    450   4543     marks 		if (!INGLOBALZONE(curproc)) {
    451   4543     marks 			uint64_t zoned;
    452   4543     marks 			char setpoint[MAXNAMELEN];
    453   4543     marks 			/*
    454   4543     marks 			 * Unprivileged users are allowed to modify the
    455   4543     marks 			 * quota on things *under* (ie. contained by)
    456   4543     marks 			 * the thing they own.
    457   4543     marks 			 */
    458  11022       Tom 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
    459   4543     marks 			    setpoint))
    460   4543     marks 				return (EPERM);
    461  11022       Tom 			if (!zoned || strlen(dsname) <= strlen(setpoint))
    462   4543     marks 				return (EPERM);
    463   4543     marks 		}
    464  10972       Ric 		break;
    465  10972       Ric 
    466  10972       Ric 	case ZFS_PROP_MLSLABEL:
    467  10972       Ric 		if (!is_system_labeled())
    468  10972       Ric 			return (EPERM);
    469  11022       Tom 
    470  11022       Tom 		if (nvpair_value_string(propval, &strval) == 0) {
    471  11022       Tom 			int err;
    472  11022       Tom 
    473  11022       Tom 			err = zfs_set_slabel_policy(dsname, strval, CRED());
    474  11022       Tom 			if (err != 0)
    475  11022       Tom 				return (err);
    476  11022       Tom 		}
    477  11022       Tom 		break;
    478  11022       Tom 	}
    479  11022       Tom 
    480  11022       Tom 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
    481   4543     marks }
    482   4543     marks 
    483   4543     marks int
    484   4543     marks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
    485   4543     marks {
    486   4543     marks 	int error;
    487   4543     marks 
    488   4543     marks 	error = zfs_dozonecheck(zc->zc_name, cr);
    489   4543     marks 	if (error)
    490    789    ahrens 		return (error);
    491    789    ahrens 
    492   4543     marks 	/*
    493   4543     marks 	 * permission to set permissions will be evaluated later in
    494   4543     marks 	 * dsl_deleg_can_allow()
    495   4543     marks 	 */
    496   4543     marks 	return (0);
    497    789    ahrens }
    498    789    ahrens 
    499   4543     marks int
    500   4543     marks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
    501   4543     marks {
    502  10588      Eric 	return (zfs_secpolicy_write_perms(zc->zc_name,
    503  10588      Eric 	    ZFS_DELEG_PERM_ROLLBACK, cr));
    504   4543     marks }
    505   4543     marks 
    506   4543     marks int
    507   4543     marks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
    508   4543     marks {
    509   4543     marks 	return (zfs_secpolicy_write_perms(zc->zc_name,
    510   4543     marks 	    ZFS_DELEG_PERM_SEND, cr));
    511   4543     marks }
    512   4543     marks 
    513   8845       amw static int
    514   8845       amw zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
    515   8845       amw {
    516   8845       amw 	vnode_t *vp;
    517   8845       amw 	int error;
    518   8845       amw 
    519   8845       amw 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
    520   8845       amw 	    NO_FOLLOW, NULL, &vp)) != 0)
    521   8845       amw 		return (error);
    522   8845       amw 
    523   8845       amw 	/* Now make sure mntpnt and dataset are ZFS */
    524   8845       amw 
    525   8845       amw 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
    526   8845       amw 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
    527   8845       amw 	    zc->zc_name) != 0)) {
    528   8845       amw 		VN_RELE(vp);
    529   8845       amw 		return (EPERM);
    530   8845       amw 	}
    531   8845       amw 
    532   8845       amw 	VN_RELE(vp);
    533   8845       amw 	return (dsl_deleg_access(zc->zc_name,
    534   8845       amw 	    ZFS_DELEG_PERM_SHARE, cr));
    535   8845       amw }
    536   8845       amw 
    537   4543     marks int
    538   4543     marks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
    539   4543     marks {
    540   4543     marks 	if (!INGLOBALZONE(curproc))
    541   4543     marks 		return (EPERM);
    542   4543     marks 
    543   5367    ahrens 	if (secpolicy_nfs(cr) == 0) {
    544   4543     marks 		return (0);
    545   4543     marks 	} else {
    546   8845       amw 		return (zfs_secpolicy_deleg_share(zc, cr));
    547   8845       amw 	}
    548   8845       amw }
    549   4543     marks 
    550   8845       amw int
    551   8845       amw zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
    552   8845       amw {
    553   8845       amw 	if (!INGLOBALZONE(curproc))
    554   8845       amw 		return (EPERM);
    555   4543     marks 
    556   8845       amw 	if (secpolicy_smb(cr) == 0) {
    557   8845       amw 		return (0);
    558   8845       amw 	} else {
    559   8845       amw 		return (zfs_secpolicy_deleg_share(zc, cr));
    560   4543     marks 	}
    561   4543     marks }
    562   4543     marks 
    563    789    ahrens static int
    564   4543     marks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
    565    789    ahrens {
    566    789    ahrens 	char *cp;
    567    789    ahrens 
    568    789    ahrens 	/*
    569    789    ahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
    570    789    ahrens 	 */
    571   4543     marks 	(void) strncpy(parent, datasetname, parentsize);
    572   4543     marks 	cp = strrchr(parent, '@');
    573    789    ahrens 	if (cp != NULL) {
    574    789    ahrens 		cp[0] = '\0';
    575    789    ahrens 	} else {
    576   4543     marks 		cp = strrchr(parent, '/');
    577    789    ahrens 		if (cp == NULL)
    578    789    ahrens 			return (ENOENT);
    579    789    ahrens 		cp[0] = '\0';
    580    789    ahrens 	}
    581    789    ahrens 
    582   4543     marks 	return (0);
    583   4543     marks }
    584   4543     marks 
    585   4543     marks int
    586   4543     marks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
    587   4543     marks {
    588   4543     marks 	int error;
    589   4543     marks 
    590   4543     marks 	if ((error = zfs_secpolicy_write_perms(name,
    591   4543     marks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
    592   4543     marks 		return (error);
    593   4543     marks 
    594   4543     marks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
    595   4543     marks }
    596   4543     marks 
    597   4543     marks static int
    598   4543     marks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
    599   4543     marks {
    600   4543     marks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
    601   4543     marks }
    602   4543     marks 
    603   4543     marks /*
    604   4543     marks  * Must have sys_config privilege to check the iscsi permission
    605   4543     marks  */
    606   4543     marks /* ARGSUSED */
    607   4543     marks static int
    608   4543     marks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
    609   4543     marks {
    610   4543     marks 	return (secpolicy_zfs(cr));
    611   4543     marks }
    612   4543     marks 
    613   4543     marks int
    614   4543     marks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
    615   4543     marks {
    616  11022       Tom 	char	parentname[MAXNAMELEN];
    617   4543     marks 	int	error;
    618   4543     marks 
    619   4543     marks 	if ((error = zfs_secpolicy_write_perms(from,
    620   4543     marks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
    621   4543     marks 		return (error);
    622   4543     marks 
    623   4543     marks 	if ((error = zfs_secpolicy_write_perms(from,
    624   4543     marks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
    625   4543     marks 		return (error);
    626   4543     marks 
    627   4543     marks 	if ((error = zfs_get_parent(to, parentname,
    628   4543     marks 	    sizeof (parentname))) != 0)
    629   4543     marks 		return (error);
    630   4543     marks 
    631   4543     marks 	if ((error = zfs_secpolicy_write_perms(parentname,
    632   4543     marks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
    633   4543     marks 		return (error);
    634   4543     marks 
    635   4543     marks 	if ((error = zfs_secpolicy_write_perms(parentname,
    636   4543     marks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
    637   4543     marks 		return (error);
    638   4543     marks 
    639   4543     marks 	return (error);
    640   4543     marks }
    641   4543     marks 
    642   4543     marks static int
    643   4543     marks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
    644   4543     marks {
    645   4543     marks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
    646   4543     marks }
    647   4543     marks 
    648   4543     marks static int
    649   4543     marks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
    650   4543     marks {
    651  11022       Tom 	char	parentname[MAXNAMELEN];
    652   4543     marks 	objset_t *clone;
    653   4543     marks 	int error;
    654   4543     marks 
    655   4543     marks 	error = zfs_secpolicy_write_perms(zc->zc_name,
    656   4543     marks 	    ZFS_DELEG_PERM_PROMOTE, cr);
    657   4543     marks 	if (error)
    658   4543     marks 		return (error);
    659   4543     marks 
    660  10298   Matthew 	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
    661   4543     marks 
    662   4543     marks 	if (error == 0) {
    663   4543     marks 		dsl_dataset_t *pclone = NULL;
    664   4543     marks 		dsl_dir_t *dd;
    665  10298   Matthew 		dd = clone->os_dsl_dataset->ds_dir;
    666   4543     marks 
    667   4543     marks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
    668   6689    maybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
    669   6689    maybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
    670   4543     marks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
    671   4543     marks 		if (error) {
    672  10298   Matthew 			dmu_objset_rele(clone, FTAG);
    673   4543     marks 			return (error);
    674   4543     marks 		}
    675   4543     marks 
    676   4543     marks 		error = zfs_secpolicy_write_perms(zc->zc_name,
    677   4543     marks 		    ZFS_DELEG_PERM_MOUNT, cr);
    678   4543     marks 
    679   4543     marks 		dsl_dataset_name(pclone, parentname);
    680  10298   Matthew 		dmu_objset_rele(clone, FTAG);
    681   6689    maybee 		dsl_dataset_rele(pclone, FTAG);
    682   4543     marks 		if (error == 0)
    683   4543     marks 			error = zfs_secpolicy_write_perms(parentname,
    684   4543     marks 			    ZFS_DELEG_PERM_PROMOTE, cr);
    685   4543     marks 	}
    686   4543     marks 	return (error);
    687   4543     marks }
    688   4543     marks 
    689   4543     marks static int
    690   4543     marks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
    691   4543     marks {
    692   4543     marks 	int error;
    693   4543     marks 
    694   4543     marks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
    695   4543     marks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
    696   4543     marks 		return (error);
    697   4543     marks 
    698   4543     marks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
    699   4543     marks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
    700   4543     marks 		return (error);
    701   4543     marks 
    702   4543     marks 	return (zfs_secpolicy_write_perms(zc->zc_name,
    703   4543     marks 	    ZFS_DELEG_PERM_CREATE, cr));
    704   4543     marks }
    705   4543     marks 
    706   4543     marks int
    707   4543     marks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
    708   4543     marks {
    709  10588      Eric 	return (zfs_secpolicy_write_perms(name,
    710  10588      Eric 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
    711   4543     marks }
    712   4543     marks 
    713   4543     marks static int
    714   4543     marks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
    715   4543     marks {
    716   4543     marks 
    717   4543     marks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
    718   4543     marks }
    719   4543     marks 
    720   4543     marks static int
    721   4543     marks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
    722   4543     marks {
    723  11022       Tom 	char	parentname[MAXNAMELEN];
    724  11022       Tom 	int	error;
    725   4543     marks 
    726   4543     marks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
    727   4543     marks 	    sizeof (parentname))) != 0)
    728   4543     marks 		return (error);
    729   4543     marks 
    730   4543     marks 	if (zc->zc_value[0] != '\0') {
    731   4543     marks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
    732   4543     marks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
    733   4543     marks 			return (error);
    734   4543     marks 	}
    735   4543     marks 
    736   4543     marks 	if ((error = zfs_secpolicy_write_perms(parentname,
    737   4543     marks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
    738   4543     marks 		return (error);
    739   4543     marks 
    740   4543     marks 	error = zfs_secpolicy_write_perms(parentname,
    741   4543     marks 	    ZFS_DELEG_PERM_MOUNT, cr);
    742   4543     marks 
    743   4543     marks 	return (error);
    744   4543     marks }
    745   4543     marks 
    746   4543     marks static int
    747   4543     marks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
    748   4543     marks {
    749   4543     marks 	int error;
    750   4543     marks 
    751   4543     marks 	error = secpolicy_fs_unmount(cr, NULL);
    752   4543     marks 	if (error) {
    753   4543     marks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
    754   4543     marks 	}
    755   4543     marks 	return (error);
    756    789    ahrens }
    757    789    ahrens 
    758    789    ahrens /*
    759    789    ahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
    760    789    ahrens  * SYS_CONFIG privilege, which is not available in a local zone.
    761    789    ahrens  */
    762    789    ahrens /* ARGSUSED */
    763    789    ahrens static int
    764   4543     marks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
    765    789    ahrens {
    766    789    ahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
    767    789    ahrens 		return (EPERM);
    768    789    ahrens 
    769    789    ahrens 	return (0);
    770    789    ahrens }
    771    789    ahrens 
    772    789    ahrens /*
    773   1544  eschrock  * Policy for fault injection.  Requires all privileges.
    774   1544  eschrock  */
    775   1544  eschrock /* ARGSUSED */
    776   1544  eschrock static int
    777   4543     marks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
    778   1544  eschrock {
    779   1544  eschrock 	return (secpolicy_zinject(cr));
    780   4849    ahrens }
    781   4849    ahrens 
    782   4849    ahrens static int
    783   4849    ahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
    784   4849    ahrens {
    785   4849    ahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
    786   4849    ahrens 
    787   5094     lling 	if (prop == ZPROP_INVAL) {
    788   4849    ahrens 		if (!zfs_prop_user(zc->zc_value))
    789   4849    ahrens 			return (EINVAL);
    790   4849    ahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
    791   4849    ahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
    792   4849    ahrens 	} else {
    793  11022       Tom 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
    794  11022       Tom 		    NULL, cr));
    795   4849    ahrens 	}
    796   1544  eschrock }
    797   1544  eschrock 
    798   9396   Matthew static int
    799   9396   Matthew zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
    800   9396   Matthew {
    801   9396   Matthew 	int err = zfs_secpolicy_read(zc, cr);
    802   9396   Matthew 	if (err)
    803   9396   Matthew 		return (err);
    804   9396   Matthew 
    805   9396   Matthew 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
    806   9396   Matthew 		return (EINVAL);
    807   9396   Matthew 
    808   9396   Matthew 	if (zc->zc_value[0] == 0) {
    809   9396   Matthew 		/*
    810   9396   Matthew 		 * They are asking about a posix uid/gid.  If it's
    811   9396   Matthew 		 * themself, allow it.
    812   9396   Matthew 		 */
    813   9396   Matthew 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
    814   9396   Matthew 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
    815   9396   Matthew 			if (zc->zc_guid == crgetuid(cr))
    816   9396   Matthew 				return (0);
    817   9396   Matthew 		} else {
    818   9396   Matthew 			if (groupmember(zc->zc_guid, cr))
    819   9396   Matthew 				return (0);
    820   9396   Matthew 		}
    821   9396   Matthew 	}
    822   9396   Matthew 
    823   9396   Matthew 	return (zfs_secpolicy_write_perms(zc->zc_name,
    824   9396   Matthew 	    userquota_perms[zc->zc_objset_type], cr));
    825   9396   Matthew }
    826   9396   Matthew 
    827   9396   Matthew static int
    828   9396   Matthew zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
    829   9396   Matthew {
    830   9396   Matthew 	int err = zfs_secpolicy_read(zc, cr);
    831   9396   Matthew 	if (err)
    832   9396   Matthew 		return (err);
    833   9396   Matthew 
    834   9396   Matthew 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
    835   9396   Matthew 		return (EINVAL);
    836   9396   Matthew 
    837   9396   Matthew 	return (zfs_secpolicy_write_perms(zc->zc_name,
    838   9396   Matthew 	    userquota_perms[zc->zc_objset_type], cr));
    839   9396   Matthew }
    840   9396   Matthew 
    841   9396   Matthew static int
    842   9396   Matthew zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
    843   9396   Matthew {
    844  11022       Tom 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
    845  11022       Tom 	    NULL, cr));
    846  10242     chris }
    847  10242     chris 
    848  10242     chris static int
    849  10242     chris zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
    850  10242     chris {
    851  10242     chris 	return (zfs_secpolicy_write_perms(zc->zc_name,
    852  10242     chris 	    ZFS_DELEG_PERM_HOLD, cr));
    853  10242     chris }
    854  10242     chris 
    855  10242     chris static int
    856  10242     chris zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
    857  10242     chris {
    858  10242     chris 	return (zfs_secpolicy_write_perms(zc->zc_name,
    859  10242     chris 	    ZFS_DELEG_PERM_RELEASE, cr));
    860   9396   Matthew }
    861   9396   Matthew 
    862   1544  eschrock /*
    863    789    ahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
    864    789    ahrens  */
    865    789    ahrens static int
    866   9643      Eric get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
    867    789    ahrens {
    868    789    ahrens 	char *packed;
    869    789    ahrens 	int error;
    870   5094     lling 	nvlist_t *list = NULL;
    871    789    ahrens 
    872    789    ahrens 	/*
    873   2676  eschrock 	 * Read in and unpack the user-supplied nvlist.
    874    789    ahrens 	 */
    875   5094     lling 	if (size == 0)
    876    789    ahrens 		return (EINVAL);
    877    789    ahrens 
    878    789    ahrens 	packed = kmem_alloc(size, KM_SLEEP);
    879    789    ahrens 
    880   9643      Eric 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
    881   9643      Eric 	    iflag)) != 0) {
    882    789    ahrens 		kmem_free(packed, size);
    883    789    ahrens 		return (error);
    884    789    ahrens 	}
    885    789    ahrens 
    886   5094     lling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
    887    789    ahrens 		kmem_free(packed, size);
    888    789    ahrens 		return (error);
    889    789    ahrens 	}
    890    789    ahrens 
    891    789    ahrens 	kmem_free(packed, size);
    892    789    ahrens 
    893   5094     lling 	*nvp = list;
    894  11022       Tom 	return (0);
    895  11022       Tom }
    896  11022       Tom 
    897  11022       Tom static int
    898  11022       Tom fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
    899  11022       Tom {
    900  11022       Tom 	size_t size;
    901  11022       Tom 
    902  11022       Tom 	VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
    903  11022       Tom 
    904  11022       Tom 	if (size > zc->zc_nvlist_dst_size) {
    905  11022       Tom 		nvpair_t *more_errors;
    906  11022       Tom 		int n = 0;
    907  11022       Tom 
    908  11022       Tom 		if (zc->zc_nvlist_dst_size < 1024)
    909  11022       Tom 			return (ENOMEM);
    910  11022       Tom 
    911  11022       Tom 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
    912  11022       Tom 		more_errors = nvlist_prev_nvpair(*errors, NULL);
    913  11022       Tom 
    914  11022       Tom 		do {
    915  11022       Tom 			nvpair_t *pair = nvlist_prev_nvpair(*errors,
    916  11022       Tom 			    more_errors);
    917  11022       Tom 			VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
    918  11022       Tom 			n++;
    919  11022       Tom 			VERIFY(nvlist_size(*errors, &size,
    920  11022       Tom 			    NV_ENCODE_NATIVE) == 0);
    921  11022       Tom 		} while (size > zc->zc_nvlist_dst_size);
    922  11022       Tom 
    923  11022       Tom 		VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
    924  11022       Tom 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
    925  11022       Tom 		ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
    926  11022       Tom 		ASSERT(size <= zc->zc_nvlist_dst_size);
    927  11022       Tom 	}
    928  11022       Tom 
    929    789    ahrens 	return (0);
    930    789    ahrens }
    931    789    ahrens 
    932    789    ahrens static int
    933   2676  eschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
    934   2676  eschrock {
    935   2676  eschrock 	char *packed = NULL;
    936   2676  eschrock 	size_t size;
    937   2676  eschrock 	int error;
    938   2676  eschrock 
    939   2676  eschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
    940   2676  eschrock 
    941   2676  eschrock 	if (size > zc->zc_nvlist_dst_size) {
    942   2676  eschrock 		error = ENOMEM;
    943   2676  eschrock 	} else {
    944   4611     marks 		packed = kmem_alloc(size, KM_SLEEP);
    945   2676  eschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
    946   2676  eschrock 		    KM_SLEEP) == 0);
    947   9643      Eric 		error = ddi_copyout(packed,
    948   9643      Eric 		    (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags);
    949   2676  eschrock 		kmem_free(packed, size);
    950   2676  eschrock 	}
    951   2676  eschrock 
    952   2676  eschrock 	zc->zc_nvlist_dst_size = size;
    953   2676  eschrock 	return (error);
    954   9396   Matthew }
    955   9396   Matthew 
    956   9396   Matthew static int
    957  11185      Sean getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
    958   9396   Matthew {
    959   9396   Matthew 	objset_t *os;
    960   9396   Matthew 	int error;
    961   9396   Matthew 
    962  10298   Matthew 	error = dmu_objset_hold(dsname, FTAG, &os);
    963   9396   Matthew 	if (error)
    964   9396   Matthew 		return (error);
    965  10298   Matthew 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
    966  10298   Matthew 		dmu_objset_rele(os, FTAG);
    967  10298   Matthew 		return (EINVAL);
    968  10298   Matthew 	}
    969   9396   Matthew 
    970  10298   Matthew 	mutex_enter(&os->os_user_ptr_lock);
    971  11185      Sean 	*zfvp = dmu_objset_get_user(os);
    972  11185      Sean 	if (*zfvp) {
    973  11185      Sean 		VFS_HOLD((*zfvp)->z_vfs);
    974   9396   Matthew 	} else {
    975   9396   Matthew 		error = ESRCH;
    976   9396   Matthew 	}
    977  10298   Matthew 	mutex_exit(&os->os_user_ptr_lock);
    978  10298   Matthew 	dmu_objset_rele(os, FTAG);
    979   9396   Matthew 	return (error);
    980   9396   Matthew }
    981   9396   Matthew 
    982   9396   Matthew /*
    983   9396   Matthew  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
    984   9396   Matthew  * case its z_vfs will be NULL, and it will be opened as the owner.
    985   9396   Matthew  */
    986   9396   Matthew static int
    987  11185      Sean zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp)
    988  11185      Sean {
    989  11185      Sean 	int error = 0;
    990  11185      Sean 
    991  11185      Sean 	if (getzfsvfs(name, zfvp) != 0)
    992  11185      Sean 		error = zfsvfs_create(name, zfvp);
    993  11185      Sean 	if (error == 0) {
    994  11185      Sean 		rrw_enter(&(*zfvp)->z_teardown_lock, RW_READER, tag);
    995  11185      Sean 		if ((*zfvp)->z_unmounted) {
    996   9396   Matthew 			/*
    997   9396   Matthew 			 * XXX we could probably try again, since the unmounting
    998   9396   Matthew 			 * thread should be just about to disassociate the
    999   9396   Matthew 			 * objset from the zfsvfs.
   1000   9396   Matthew 			 */
   1001  11185      Sean 			rrw_exit(&(*zfvp)->z_teardown_lock, tag);
   1002   9396   Matthew 			return (EBUSY);
   1003   9396   Matthew 		}
   1004   9396   Matthew 	}
   1005   9396   Matthew 	return (error);
   1006   9396   Matthew }
   1007   9396   Matthew 
   1008   9396   Matthew static void
   1009   9396   Matthew zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
   1010   9396   Matthew {
   1011   9396   Matthew 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
   1012   9396   Matthew 
   1013   9396   Matthew 	if (zfsvfs->z_vfs) {
   1014   9396   Matthew 		VFS_RELE(zfsvfs->z_vfs);
   1015   9396   Matthew 	} else {
   1016  10298   Matthew 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
   1017   9396   Matthew 		zfsvfs_free(zfsvfs);
   1018   9396   Matthew 	}
   1019   2676  eschrock }
   1020   2676  eschrock 
   1021   2676  eschrock static int
   1022    789    ahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
   1023    789    ahrens {
   1024    789    ahrens 	int error;
   1025   5094     lling 	nvlist_t *config, *props = NULL;
   1026   7184      timh 	nvlist_t *rootprops = NULL;
   1027   7184      timh 	nvlist_t *zplprops = NULL;
   1028   4715  ek110237 	char *buf;
   1029    789    ahrens 
   1030   5094     lling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
   1031   9643      Eric 	    zc->zc_iflags, &config))
   1032   4988  ek110237 		return (error);
   1033   5094     lling 
   1034   5094     lling 	if (zc->zc_nvlist_src_size != 0 && (error =
   1035   9643      Eric 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   1036   9643      Eric 	    zc->zc_iflags, &props))) {
   1037   5094     lling 		nvlist_free(config);
   1038   5094     lling 		return (error);
   1039   5094     lling 	}
   1040   4715  ek110237 
   1041   7184      timh 	if (props) {
   1042   7184      timh 		nvlist_t *nvl = NULL;
   1043   7184      timh 		uint64_t version = SPA_VERSION;
   1044   7184      timh 
   1045   7184      timh 		(void) nvlist_lookup_uint64(props,
   1046   7184      timh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
   1047   7184      timh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
   1048   7184      timh 			error = EINVAL;
   1049   7184      timh 			goto pool_props_bad;
   1050   7184      timh 		}
   1051   7184      timh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
   1052   7184      timh 		if (nvl) {
   1053   7184      timh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
   1054   7184      timh 			if (error != 0) {
   1055   7184      timh 				nvlist_free(config);
   1056   7184      timh 				nvlist_free(props);
   1057   7184      timh 				return (error);
   1058   7184      timh 			}
   1059   7184      timh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
   1060   7184      timh 		}
   1061   7184      timh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
   1062   7184      timh 		error = zfs_fill_zplprops_root(version, rootprops,
   1063   7184      timh 		    zplprops, NULL);
   1064   7184      timh 		if (error)
   1065   7184      timh 			goto pool_props_bad;
   1066   7184      timh 	}
   1067   7184      timh 
   1068   4988  ek110237 	buf = history_str_get(zc);
   1069    789    ahrens 
   1070   7184      timh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
   1071   7184      timh 
   1072   7184      timh 	/*
   1073   7184      timh 	 * Set the remaining root properties
   1074   7184      timh 	 */
   1075  11022       Tom 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
   1076  11022       Tom 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
   1077   7184      timh 		(void) spa_destroy(zc->zc_name);
   1078    789    ahrens 
   1079   4988  ek110237 	if (buf != NULL)
   1080   4988  ek110237 		history_str_free(buf);
   1081   5094     lling 
   1082   7184      timh pool_props_bad:
   1083   7184      timh 	nvlist_free(rootprops);
   1084   7184      timh 	nvlist_free(zplprops);
   1085    789    ahrens 	nvlist_free(config);
   1086   7184      timh 	nvlist_free(props);
   1087    789    ahrens 
   1088    789    ahrens 	return (error);
   1089    789    ahrens }
   1090    789    ahrens 
   1091    789    ahrens static int
   1092    789    ahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
   1093    789    ahrens {
   1094   4543     marks 	int error;
   1095   4543     marks 	zfs_log_history(zc);
   1096   4543     marks 	error = spa_destroy(zc->zc_name);
   1097  10588      Eric 	if (error == 0)
   1098  10588      Eric 		zvol_remove_minors(zc->zc_name);
   1099   4543     marks 	return (error);
   1100    789    ahrens }
   1101    789    ahrens 
   1102    789    ahrens static int
   1103    789    ahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
   1104    789    ahrens {
   1105   5094     lling 	nvlist_t *config, *props = NULL;
   1106    789    ahrens 	uint64_t guid;
   1107  10921       Tim 	int error;
   1108    789    ahrens 
   1109   5094     lling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
   1110   9643      Eric 	    zc->zc_iflags, &config)) != 0)
   1111    789    ahrens 		return (error);
   1112   5094     lling 
   1113   5094     lling 	if (zc->zc_nvlist_src_size != 0 && (error =
   1114   9643      Eric 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   1115   9643      Eric 	    zc->zc_iflags, &props))) {
   1116   5094     lling 		nvlist_free(config);
   1117   5094     lling 		return (error);
   1118   5094     lling 	}
   1119    789    ahrens 
   1120    789    ahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
   1121   1544  eschrock 	    guid != zc->zc_guid)
   1122    789    ahrens 		error = EINVAL;
   1123   6643  eschrock 	else if (zc->zc_cookie)
   1124  10921       Tim 		error = spa_import_verbatim(zc->zc_name, config, props);
   1125    789    ahrens 	else
   1126   5094     lling 		error = spa_import(zc->zc_name, config, props);
   1127  10921       Tim 
   1128  10921       Tim 	if (zc->zc_nvlist_dst != 0)
   1129  10921       Tim 		(void) put_nvlist(zc, config);
   1130    789    ahrens 
   1131    789    ahrens 	nvlist_free(config);
   1132   5094     lling 
   1133   5094     lling 	if (props)
   1134   5094     lling 		nvlist_free(props);
   1135    789    ahrens 
   1136    789    ahrens 	return (error);
   1137    789    ahrens }
   1138    789    ahrens 
   1139    789    ahrens static int
   1140    789    ahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
   1141    789    ahrens {
   1142   4543     marks 	int error;
   1143   7214     lling 	boolean_t force = (boolean_t)zc->zc_cookie;
   1144   8211    George 	boolean_t hardforce = (boolean_t)zc->zc_guid;
   1145   7214     lling 
   1146   4543     marks 	zfs_log_history(zc);
   1147   8211    George 	error = spa_export(zc->zc_name, NULL, force, hardforce);
   1148  10588      Eric 	if (error == 0)
   1149  10588      Eric 		zvol_remove_minors(zc->zc_name);
   1150   4543     marks 	return (error);
   1151    789    ahrens }
   1152    789    ahrens 
   1153    789    ahrens static int
   1154    789    ahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
   1155    789    ahrens {
   1156    789    ahrens 	nvlist_t *configs;
   1157    789    ahrens 	int error;
   1158    789    ahrens 
   1159    789    ahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
   1160    789    ahrens 		return (EEXIST);
   1161    789    ahrens 
   1162   2676  eschrock 	error = put_nvlist(zc, configs);
   1163    789    ahrens 
   1164    789    ahrens 	nvlist_free(configs);
   1165    789    ahrens 
   1166    789    ahrens 	return (error);
   1167    789    ahrens }
   1168    789    ahrens 
   1169    789    ahrens static int
   1170    789    ahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
   1171    789    ahrens {
   1172    789    ahrens 	nvlist_t *config;
   1173    789    ahrens 	int error;
   1174   1544  eschrock 	int ret = 0;
   1175    789    ahrens 
   1176   2676  eschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
   1177   2676  eschrock 	    sizeof (zc->zc_value));
   1178    789    ahrens 
   1179    789    ahrens 	if (config != NULL) {
   1180   2676  eschrock 		ret = put_nvlist(zc, config);
   1181    789    ahrens 		nvlist_free(config);
   1182   1544  eschrock 
   1183   1544  eschrock 		/*
   1184   1544  eschrock 		 * The config may be present even if 'error' is non-zero.
   1185   1544  eschrock 		 * In this case we return success, and preserve the real errno
   1186   1544  eschrock 		 * in 'zc_cookie'.
   1187   1544  eschrock 		 */
   1188   1544  eschrock 		zc->zc_cookie = error;
   1189    789    ahrens 	} else {
   1190   1544  eschrock 		ret = error;
   1191    789    ahrens 	}
   1192    789    ahrens 
   1193   1544  eschrock 	return (ret);
   1194    789    ahrens }
   1195    789    ahrens 
   1196    789    ahrens /*
   1197    789    ahrens  * Try to import the given pool, returning pool stats as appropriate so that
   1198    789    ahrens  * user land knows which devices are available and overall pool health.
   1199    789    ahrens  */
   1200    789    ahrens static int
   1201    789    ahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
   1202    789    ahrens {
   1203    789    ahrens 	nvlist_t *tryconfig, *config;
   1204    789    ahrens 	int error;
   1205    789    ahrens 
   1206   5094     lling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
   1207   9643      Eric 	    zc->zc_iflags, &tryconfig)) != 0)
   1208    789    ahrens 		return (error);
   1209    789    ahrens 
   1210    789    ahrens 	config = spa_tryimport(tryconfig);
   1211    789    ahrens 
   1212    789    ahrens 	nvlist_free(tryconfig);
   1213    789    ahrens 
   1214    789    ahrens 	if (config == NULL)
   1215    789    ahrens 		return (EINVAL);
   1216    789    ahrens 
   1217   2676  eschrock 	error = put_nvlist(zc, config);
   1218    789    ahrens 	nvlist_free(config);
   1219    789    ahrens 
   1220    789    ahrens 	return (error);
   1221    789    ahrens }
   1222    789    ahrens 
   1223    789    ahrens static int
   1224    789    ahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
   1225    789    ahrens {
   1226    789    ahrens 	spa_t *spa;
   1227    789    ahrens 	int error;
   1228    789    ahrens 
   1229   2926  ek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1230   2926  ek110237 		return (error);
   1231   2926  ek110237 
   1232   7046    ahrens 	error = spa_scrub(spa, zc->zc_cookie);
   1233   2926  ek110237 
   1234   2926  ek110237 	spa_close(spa, FTAG);
   1235   2926  ek110237 
   1236    789    ahrens 	return (error);
   1237    789    ahrens }
   1238    789    ahrens 
   1239    789    ahrens static int
   1240    789    ahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
   1241    789    ahrens {
   1242    789    ahrens 	spa_t *spa;
   1243    789    ahrens 	int error;
   1244    789    ahrens 
   1245    789    ahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
   1246    789    ahrens 	if (error == 0) {
   1247    789    ahrens 		spa_freeze(spa);
   1248   1760  eschrock 		spa_close(spa, FTAG);
   1249   1760  eschrock 	}
   1250   1760  eschrock 	return (error);
   1251   1760  eschrock }
   1252   1760  eschrock 
   1253   1760  eschrock static int
   1254   1760  eschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
   1255   1760  eschrock {
   1256   1760  eschrock 	spa_t *spa;
   1257   1760  eschrock 	int error;
   1258   1760  eschrock 
   1259   2926  ek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1260   2926  ek110237 		return (error);
   1261   5118     lling 
   1262   5118     lling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
   1263   5118     lling 		spa_close(spa, FTAG);
   1264   5118     lling 		return (EINVAL);
   1265   5118     lling 	}
   1266   2926  ek110237 
   1267   5094     lling 	spa_upgrade(spa, zc->zc_cookie);
   1268   2926  ek110237 	spa_close(spa, FTAG);
   1269   2926  ek110237 
   1270   2926  ek110237 	return (error);
   1271   2926  ek110237 }
   1272   2926  ek110237 
   1273   2926  ek110237 static int
   1274   2926  ek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
   1275   2926  ek110237 {
   1276   2926  ek110237 	spa_t *spa;
   1277   2926  ek110237 	char *hist_buf;
   1278   2926  ek110237 	uint64_t size;
   1279   2926  ek110237 	int error;
   1280   2926  ek110237 
   1281   2926  ek110237 	if ((size = zc->zc_history_len) == 0)
   1282   2926  ek110237 		return (EINVAL);
   1283   2926  ek110237 
   1284   2926  ek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1285   2926  ek110237 		return (error);
   1286   2926  ek110237 
   1287   4577    ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
   1288   3863  ek110237 		spa_close(spa, FTAG);
   1289   3863  ek110237 		return (ENOTSUP);
   1290   3863  ek110237 	}
   1291   3863  ek110237 
   1292   2926  ek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
   1293   2926  ek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
   1294   2926  ek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
   1295   9643      Eric 		error = ddi_copyout(hist_buf,
   1296   9643      Eric 		    (void *)(uintptr_t)zc->zc_history,
   1297   9643      Eric 		    zc->zc_history_len, zc->zc_iflags);
   1298   2926  ek110237 	}
   1299   2926  ek110237 
   1300   2926  ek110237 	spa_close(spa, FTAG);
   1301   2926  ek110237 	kmem_free(hist_buf, size);
   1302   3444  ek110237 	return (error);
   1303   3444  ek110237 }
   1304   3444  ek110237 
   1305   3444  ek110237 static int
   1306   3444  ek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
   1307   3444  ek110237 {
   1308   3444  ek110237 	int error;
   1309   3444  ek110237 
   1310   3912     lling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
   1311   3444  ek110237 		return (error);
   1312   3444  ek110237 
   1313   3444  ek110237 	return (0);
   1314   3444  ek110237 }
   1315   3444  ek110237 
   1316  10298   Matthew /*
   1317  10298   Matthew  * inputs:
   1318  10298   Matthew  * zc_name		name of filesystem
   1319  10298   Matthew  * zc_obj		object to find
   1320  10298   Matthew  *
   1321  10298   Matthew  * outputs:
   1322  10298   Matthew  * zc_value		name of object
   1323  10298   Matthew  */
   1324   3444  ek110237 static int
   1325   3444  ek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
   1326   3444  ek110237 {
   1327  10298   Matthew 	objset_t *os;
   1328   3444  ek110237 	int error;
   1329   3444  ek110237 
   1330  10298   Matthew 	/* XXX reading from objset not owned */
   1331  10298   Matthew 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
   1332   3444  ek110237 		return (error);
   1333  10298   Matthew 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
   1334  10298   Matthew 		dmu_objset_rele(os, FTAG);
   1335  10298   Matthew 		return (EINVAL);
   1336  10298   Matthew 	}
   1337  10298   Matthew 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
   1338   3444  ek110237 	    sizeof (zc->zc_value));
   1339  10298   Matthew 	dmu_objset_rele(os, FTAG);
   1340   2926  ek110237 
   1341    789    ahrens 	return (error);
   1342    789    ahrens }
   1343    789    ahrens 
   1344    789    ahrens static int
   1345    789    ahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
   1346    789    ahrens {
   1347    789    ahrens 	spa_t *spa;
   1348    789    ahrens 	int error;
   1349   6423   gw25295 	nvlist_t *config, **l2cache, **spares;
   1350   6423   gw25295 	uint_t nl2cache = 0, nspares = 0;
   1351    789    ahrens 
   1352    789    ahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
   1353    789    ahrens 	if (error != 0)
   1354    789    ahrens 		return (error);
   1355   3912     lling 
   1356   5450   brendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
   1357   9643      Eric 	    zc->zc_iflags, &config);
   1358   5450   brendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
   1359   5450   brendan 	    &l2cache, &nl2cache);
   1360   5450   brendan 
   1361   6423   gw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
   1362   6423   gw25295 	    &spares, &nspares);
   1363   6423   gw25295 
   1364   3912     lling 	/*
   1365   3912     lling 	 * A root pool with concatenated devices is not supported.
   1366   6423   gw25295 	 * Thus, can not add a device to a root pool.
   1367   6423   gw25295 	 *
   1368   6423   gw25295 	 * Intent log device can not be added to a rootpool because
   1369   6423   gw25295 	 * during mountroot, zil is replayed, a seperated log device
   1370   6423   gw25295 	 * can not be accessed during the mountroot time.
   1371   6423   gw25295 	 *
   1372   6423   gw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
   1373   3912     lling 	 */
   1374  10922      Jeff 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
   1375   3912     lling 		spa_close(spa, FTAG);
   1376   3912     lling 		return (EDOM);
   1377   3912     lling 	}
   1378    789    ahrens 
   1379   5450   brendan 	if (error == 0) {
   1380    789    ahrens 		error = spa_vdev_add(spa, config);
   1381    789    ahrens 		nvlist_free(config);
   1382    789    ahrens 	}
   1383    789    ahrens 	spa_close(spa, FTAG);
   1384    789    ahrens 	return (error);
   1385    789    ahrens }
   1386    789    ahrens 
   1387    789    ahrens static int
   1388    789    ahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
   1389    789    ahrens {
   1390   2082  eschrock 	spa_t *spa;
   1391   2082  eschrock 	int error;
   1392   2082  eschrock 
   1393   2082  eschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
   1394   2082  eschrock 	if (error != 0)
   1395   2082  eschrock 		return (error);
   1396   2082  eschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
   1397   2082  eschrock 	spa_close(spa, FTAG);
   1398   2082  eschrock 	return (error);
   1399    789    ahrens }
   1400    789    ahrens 
   1401    789    ahrens static int
   1402   4451  eschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
   1403    789    ahrens {
   1404    789    ahrens 	spa_t *spa;
   1405    789    ahrens 	int error;
   1406   4451  eschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
   1407    789    ahrens 
   1408   2926  ek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1409    789    ahrens 		return (error);
   1410   4451  eschrock 	switch (zc->zc_cookie) {
   1411   4451  eschrock 	case VDEV_STATE_ONLINE:
   1412   4451  eschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
   1413   4451  eschrock 		break;
   1414    789    ahrens 
   1415   4451  eschrock 	case VDEV_STATE_OFFLINE:
   1416   4451  eschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
   1417   4451  eschrock 		break;
   1418    789    ahrens 
   1419   4451  eschrock 	case VDEV_STATE_FAULTED:
   1420  10817      Eric 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
   1421  10817      Eric 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
   1422  10817      Eric 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
   1423  10817      Eric 
   1424  10817      Eric 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
   1425   4451  eschrock 		break;
   1426   4451  eschrock 
   1427   4451  eschrock 	case VDEV_STATE_DEGRADED:
   1428  10817      Eric 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
   1429  10817      Eric 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
   1430  10817      Eric 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
   1431  10817      Eric 
   1432  10817      Eric 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
   1433   4451  eschrock 		break;
   1434   4451  eschrock 
   1435   4451  eschrock 	default:
   1436   4451  eschrock 		error = EINVAL;
   1437   4451  eschrock 	}
   1438   4451  eschrock 	zc->zc_cookie = newstate;
   1439    789    ahrens 	spa_close(spa, FTAG);
   1440    789    ahrens 	return (error);
   1441    789    ahrens }
   1442    789    ahrens 
   1443    789    ahrens static int
   1444    789    ahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
   1445    789    ahrens {
   1446    789    ahrens 	spa_t *spa;
   1447    789    ahrens 	int replacing = zc->zc_cookie;
   1448    789    ahrens 	nvlist_t *config;
   1449    789    ahrens 	int error;
   1450    789    ahrens 
   1451   2926  ek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1452    789    ahrens 		return (error);
   1453    789    ahrens 
   1454   5094     lling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
   1455   9643      Eric 	    zc->zc_iflags, &config)) == 0) {
   1456   1544  eschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
   1457    789    ahrens 		nvlist_free(config);
   1458    789    ahrens 	}
   1459    789    ahrens 
   1460    789    ahrens 	spa_close(spa, FTAG);
   1461    789    ahrens 	return (error);
   1462    789    ahrens }
   1463    789    ahrens 
   1464    789    ahrens static int
   1465    789    ahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
   1466    789    ahrens {
   1467    789    ahrens 	spa_t *spa;
   1468    789    ahrens 	int error;
   1469    789    ahrens 
   1470   2926  ek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1471    789    ahrens 		return (error);
   1472    789    ahrens 
   1473   8241      Jeff 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
   1474    789    ahrens 
   1475    789    ahrens 	spa_close(spa, FTAG);
   1476    789    ahrens 	return (error);
   1477    789    ahrens }
   1478    789    ahrens 
   1479    789    ahrens static int
   1480   1354  eschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
   1481   1354  eschrock {
   1482   1354  eschrock 	spa_t *spa;
   1483   2676  eschrock 	char *path = zc->zc_value;
   1484   1544  eschrock 	uint64_t guid = zc->zc_guid;
   1485   1354  eschrock 	int error;
   1486   1354  eschrock 
   1487   1354  eschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
   1488   1354  eschrock 	if (error != 0)
   1489   1354  eschrock 		return (error);
   1490   1354  eschrock 
   1491   1354  eschrock 	error = spa_vdev_setpath(spa, guid, path);
   1492   9425      Eric 	spa_close(spa, FTAG);
   1493   9425      Eric 	return (error);
   1494   9425      Eric }
   1495   9425      Eric 
   1496   9425      Eric static int
   1497   9425      Eric zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
   1498   9425      Eric {
   1499   9425      Eric 	spa_t *spa;
   1500   9425      Eric 	char *fru = zc->zc_value;
   1501   9425      Eric 	uint64_t guid = zc->zc_guid;
   1502   9425      Eric 	int error;
   1503   9425      Eric 
   1504   9425      Eric 	error = spa_open(zc->zc_name, &spa, FTAG);
   1505   9425      Eric 	if (error != 0)
   1506   9425      Eric 		return (error);
   1507   9425      Eric 
   1508   9425      Eric 	error = spa_vdev_setfru(spa, guid, fru);
   1509   1354  eschrock 	spa_close(spa, FTAG);
   1510   1354  eschrock 	return (error);
   1511   1354  eschrock }
   1512   1354  eschrock 
   1513   5367    ahrens /*
   1514   5367    ahrens  * inputs:
   1515   5367    ahrens  * zc_name		name of filesystem
   1516   5367    ahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
   1517   5367    ahrens  *
   1518   5367    ahrens  * outputs:
   1519   5367    ahrens  * zc_objset_stats	stats
   1520   5367    ahrens  * zc_nvlist_dst	property nvlist
   1521   5367    ahrens  * zc_nvlist_dst_size	size of property nvlist
   1522   5367    ahrens  */
   1523   1354  eschrock static int
   1524    789    ahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
   1525    789    ahrens {
   1526    789    ahrens 	objset_t *os = NULL;
   1527    789    ahrens 	int error;
   1528   1356  eschrock 	nvlist_t *nv;
   1529    789    ahrens 
   1530  10298   Matthew 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
   1531    789    ahrens 		return (error);
   1532    789    ahrens 
   1533   2885    ahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
   1534    789    ahrens 
   1535   2856  nd150628 	if (zc->zc_nvlist_dst != 0 &&
   1536  11022       Tom 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
   1537   2885    ahrens 		dmu_objset_stats(os, nv);
   1538   3087    ahrens 		/*
   1539   5147  rm160521 		 * NB: zvol_get_stats() will read the objset contents,
   1540   3087    ahrens 		 * which we aren't supposed to do with a
   1541   6689    maybee 		 * DS_MODE_USER hold, because it could be
   1542   3087    ahrens 		 * inconsistent.  So this is a bit of a workaround...
   1543  10298   Matthew 		 * XXX reading with out owning
   1544   3087    ahrens 		 */
   1545   4577    ahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
   1546   4577    ahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
   1547   4577    ahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
   1548   4577    ahrens 		}
   1549  11022       Tom 		error = put_nvlist(zc, nv);
   1550  11022       Tom 		nvlist_free(nv);
   1551  11022       Tom 	}
   1552  11022       Tom 
   1553  11022       Tom 	dmu_objset_rele(os, FTAG);
   1554  11022       Tom 	return (error);
   1555  11022       Tom }
   1556  11022       Tom 
   1557  11022       Tom /*
   1558  11022       Tom  * inputs:
   1559  11022       Tom  * zc_name		name of filesystem
   1560  11022       Tom  * zc_nvlist_dst_size	size of buffer for property nvlist
   1561  11022       Tom  *
   1562  11022       Tom  * outputs:
   1563  11022       Tom  * zc_nvlist_dst	received property nvlist
   1564  11022       Tom  * zc_nvlist_dst_size	size of received property nvlist
   1565  11022       Tom  *
   1566  11022       Tom  * Gets received properties (distinct from local properties on or after
   1567  11022       Tom  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
   1568  11022       Tom  * local property values.
   1569  11022       Tom  */
   1570  11022       Tom static int
   1571  11022       Tom zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
   1572  11022       Tom {
   1573  11022       Tom 	objset_t *os = NULL;
   1574  11022       Tom 	int error;
   1575  11022       Tom 	nvlist_t *nv;
   1576  11022       Tom 
   1577  11022       Tom 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
   1578  11022       Tom 		return (error);
   1579  11022       Tom 
   1580  11022       Tom 	/*
   1581  11022       Tom 	 * Without this check, we would return local property values if the
   1582  11022       Tom 	 * caller has not already received properties on or after
   1583  11022       Tom 	 * SPA_VERSION_RECVD_PROPS.
   1584  11022       Tom 	 */
   1585  11022       Tom 	if (!dsl_prop_get_hasrecvd(os)) {
   1586  11022       Tom 		dmu_objset_rele(os, FTAG);
   1587  11022       Tom 		return (ENOTSUP);
   1588  11022       Tom 	}
   1589  11022       Tom 
   1590  11022       Tom 	if (zc->zc_nvlist_dst != 0 &&
   1591  11022       Tom 	    (error = dsl_prop_get_received(os, &nv)) == 0) {
   1592   2676  eschrock 		error = put_nvlist(zc, nv);
   1593   1356  eschrock 		nvlist_free(nv);
   1594   1356  eschrock 	}
   1595    789    ahrens 
   1596  10298   Matthew 	dmu_objset_rele(os, FTAG);
   1597    789    ahrens 	return (error);
   1598   5147  rm160521 }
   1599   5147  rm160521 
   1600   5498      timh static int
   1601   5498      timh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
   1602   5498      timh {
   1603   5498      timh 	uint64_t value;
   1604   5498      timh 	int error;
   1605   5498      timh 
   1606   5498      timh 	/*
   1607   5498      timh 	 * zfs_get_zplprop() will either find a value or give us
   1608   5498      timh 	 * the default value (if there is one).
   1609   5498      timh 	 */
   1610   5498      timh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
   1611   5498      timh 		return (error);
   1612   5498      timh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
   1613   5498      timh 	return (0);
   1614   5498      timh }
   1615   5498      timh 
   1616   5498      timh /*
   1617   5498      timh  * inputs:
   1618   5498      timh  * zc_name		name of filesystem
   1619   5498      timh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
   1620   5498      timh  *
   1621   5498      timh  * outputs:
   1622   5498      timh  * zc_nvlist_dst	zpl property nvlist
   1623   5498      timh  * zc_nvlist_dst_size	size of zpl property nvlist
   1624   5498      timh  */
   1625   5498      timh static int
   1626   5498      timh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
   1627   5498      timh {
   1628   5498      timh 	objset_t *os;
   1629   5498      timh 	int err;
   1630   5498      timh 
   1631  10298   Matthew 	/* XXX reading without owning */
   1632  10298   Matthew 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
   1633   5498      timh 		return (err);
   1634   5498      timh 
   1635   5498      timh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
   1636   5498      timh 
   1637   5498      timh 	/*
   1638   5498      timh 	 * NB: nvl_add_zplprop() will read the objset contents,
   1639   6689    maybee 	 * which we aren't supposed to do with a DS_MODE_USER
   1640   6689    maybee 	 * hold, because it could be inconsistent.
   1641   5498      timh 	 */
   1642   5498      timh 	if (zc->zc_nvlist_dst != NULL &&
   1643   5498      timh 	    !zc->zc_objset_stats.dds_inconsistent &&
   1644   5498      timh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
   1645   5498      timh 		nvlist_t *nv;
   1646   5498      timh 
   1647   5498      timh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
   1648   5498      timh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
   1649   5498      timh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
   1650   5498      timh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
   1651   5498      timh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
   1652   5498      timh 			err = put_nvlist(zc, nv);
   1653   5498      timh 		nvlist_free(nv);
   1654   5498      timh 	} else {
   1655   5498      timh 		err = ENOENT;
   1656   5498      timh 	}
   1657  10298   Matthew 	dmu_objset_rele(os, FTAG);
   1658   5498      timh 	return (err);
   1659   5498      timh }
   1660   5498      timh 
   1661   9396   Matthew static boolean_t
   1662   9396   Matthew dataset_name_hidden(const char *name)
   1663   9396   Matthew {
   1664   9396   Matthew 	/*
   1665   9396   Matthew 	 * Skip over datasets that are not visible in this zone,
   1666   9396   Matthew 	 * internal datasets (which have a $ in their name), and
   1667   9396   Matthew 	 * temporary datasets (which have a % in their name).
   1668   9396   Matthew 	 */
   1669   9396   Matthew 	if (strchr(name, '$') != NULL)
   1670   9396   Matthew 		return (B_TRUE);
   1671   9396   Matthew 	if (strchr(name, '%') != NULL)
   1672   9396   Matthew 		return (B_TRUE);
   1673   9396   Matthew 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
   1674   9396   Matthew 		return (B_TRUE);
   1675   9396   Matthew 	return (B_FALSE);
   1676   9396   Matthew }
   1677   9396   Matthew 
   1678   5367    ahrens /*
   1679   5367    ahrens  * inputs:
   1680   5367    ahrens  * zc_name		name of filesystem
   1681   5367    ahrens  * zc_cookie		zap cursor
   1682   5367    ahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
   1683   5367    ahrens  *
   1684   5367    ahrens  * outputs:
   1685   5367    ahrens  * zc_name		name of next filesystem
   1686   9396   Matthew  * zc_cookie		zap cursor
   1687   5367    ahrens  * zc_objset_stats	stats
   1688   5367    ahrens  * zc_nvlist_dst	property nvlist
   1689   5367    ahrens  * zc_nvlist_dst_size	size of property nvlist
   1690   5367    ahrens  */
   1691   5147  rm160521 static int
   1692    789    ahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
   1693    789    ahrens {
   1694    885    ahrens 	objset_t *os;
   1695    789    ahrens 	int error;
   1696    789    ahrens 	char *p;
   1697    789    ahrens 
   1698  10298   Matthew 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
   1699    885    ahrens 		if (error == ENOENT)
   1700    885    ahrens 			error = ESRCH;
   1701    885    ahrens 		return (error);
   1702    789    ahrens 	}
   1703    789    ahrens 
   1704    789    ahrens 	p = strrchr(zc->zc_name, '/');
   1705    789    ahrens 	if (p == NULL || p[1] != '\0')
   1706    789    ahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
   1707    789    ahrens 	p = zc->zc_name + strlen(zc->zc_name);
   1708    789    ahrens 
   1709   8697   Richard 	/*
   1710   8697   Richard 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
   1711   8697   Richard 	 * but is not declared void because its called by dmu_objset_find().
   1712   8697   Richard 	 */
   1713   8415   Richard 	if (zc->zc_cookie == 0) {
   1714   8415   Richard 		uint64_t cookie = 0;
   1715   8415   Richard 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
   1716   8415   Richard 
   1717   8415   Richard 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
   1718   8697   Richard 			(void) dmu_objset_prefetch(p, NULL);
   1719   8415   Richard 	}
   1720   8415   Richard 
   1721    789    ahrens 	do {
   1722    885    ahrens 		error = dmu_dir_list_next(os,
   1723    885    ahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
   1724    885    ahrens 		    NULL, &zc->zc_cookie);
   1725    789    ahrens 		if (error == ENOENT)
   1726    789    ahrens 			error = ESRCH;
   1727  10588      Eric 	} while (error == 0 && dataset_name_hidden(zc->zc_name) &&
   1728  10588      Eric 	    !(zc->zc_iflags & FKIOCTL));
   1729  10298   Matthew 	dmu_objset_rele(os, FTAG);
   1730    789    ahrens 
   1731  10588      Eric 	/*
   1732  10588      Eric 	 * If it's an internal dataset (ie. with a '$' in its name),
   1733  10588      Eric 	 * don't try to get stats for it, otherwise we'll return ENOENT.
   1734  10588      Eric 	 */
   1735  10588      Eric 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
   1736    885    ahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
   1737    789    ahrens 	return (error);
   1738    789    ahrens }
   1739    789    ahrens 
   1740   5367    ahrens /*
   1741   5367    ahrens  * inputs:
   1742   5367    ahrens  * zc_name		name of filesystem
   1743   5367    ahrens  * zc_cookie		zap cursor
   1744   5367    ahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
   1745   5367    ahrens  *
   1746   5367    ahrens  * outputs:
   1747   5367    ahrens  * zc_name		name of next snapshot
   1748   5367    ahrens  * zc_objset_stats	stats
   1749   5367    ahrens  * zc_nvlist_dst	property nvlist
   1750   5367    ahrens  * zc_nvlist_dst_size	size of property nvlist
   1751   5367    ahrens  */
   1752    789    ahrens static int
   1753    789    ahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
   1754    789    ahrens {
   1755    885    ahrens 	objset_t *os;
   1756    789    ahrens 	int error;
   1757    789    ahrens 
   1758  10474   Richard 	if (zc->zc_cookie == 0)
   1759  10474   Richard 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
   1760  10474   Richard 		    NULL, DS_FIND_SNAPSHOTS);
   1761  10474   Richard 
   1762  10298   Matthew 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
   1763   6689    maybee 	if (error)
   1764   6689    maybee 		return (error == ENOENT ? ESRCH : error);
   1765    789    ahrens 
   1766   1003     lling 	/*
   1767   1003     lling 	 * A dataset name of maximum length cannot have any snapshots,
   1768   1003     lling 	 * so exit immediately.
   1769   1003     lling 	 */
   1770   1003     lling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
   1771  10298   Matthew 		dmu_objset_rele(os, FTAG);
   1772   1003     lling 		return (ESRCH);
   1773    789    ahrens 	}
   1774    789    ahrens 
   1775    885    ahrens 	error = dmu_snapshot_list_next(os,
   1776    885    ahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
   1777   5663  ck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
   1778  10298   Matthew 	dmu_objset_rele(os, FTAG);
   1779   6689    maybee 	if (error == 0)
   1780   6689    maybee 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
   1781   6689    maybee 	else if (error == ENOENT)
   1782    789    ahrens 		error = ESRCH;
   1783    789    ahrens 
   1784   5367    ahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
   1785   6689    maybee 	if (error)
   1786   5367    ahrens 		*strchr(zc->zc_name, '@') = '\0';
   1787    789    ahrens 	return (error);
   1788    789    ahrens }
   1789    789    ahrens 
   1790  11022       Tom static int
   1791  11022       Tom zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
   1792  11022       Tom {
   1793  11022       Tom 	const char *propname = nvpair_name(pair);
   1794  11022       Tom 	uint64_t *valary;
   1795  11022       Tom 	unsigned int vallen;
   1796  11022       Tom 	const char *domain;
   1797  11022       Tom 	zfs_userquota_prop_t type;
   1798  11022       Tom 	uint64_t rid;
   1799  11022       Tom 	uint64_t quota;
   1800  11022       Tom 	zfsvfs_t *zfsvfs;
   1801  11022       Tom 	int err;
   1802  11022       Tom 
   1803  11022       Tom 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
   1804  11022       Tom 		nvlist_t *attrs;
   1805  11022       Tom 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
   1806  11022       Tom 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
   1807  11022       Tom 		    &pair) == 0);
   1808  11022       Tom 	}
   1809  11022       Tom 
   1810  11022       Tom 	VERIFY(nvpair_value_uint64_array(pair, &valary, &vallen) == 0);
   1811  11022       Tom 	VERIFY(vallen == 3);
   1812  11022       Tom 	type = valary[0];
   1813  11022       Tom 	rid = valary[1];
   1814  11022       Tom 	quota = valary[2];
   1815  11022       Tom 	/*
   1816  11022       Tom 	 * The propname is encoded as
   1817  11022       Tom 	 * userquota@<rid>-<domain>.
   1818  11022       Tom 	 */
   1819  11022       Tom 	domain = strchr(propname, '-') + 1;
   1820  11022       Tom 
   1821  11022       Tom 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs);
   1822  11022       Tom 	if (err == 0) {
   1823  11022       Tom 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
   1824  11022       Tom 		zfsvfs_rele(zfsvfs, FTAG);
   1825  11022       Tom 	}
   1826  11022       Tom 
   1827  11022       Tom 	return (err);
   1828  11022       Tom }
   1829  11022       Tom 
   1830  11022       Tom /*
   1831  11022       Tom  * If the named property is one that has a special function to set its value,
   1832  11022       Tom  * return 0 on success and a positive error code on failure; otherwise if it is
   1833  11022       Tom  * not one of the special properties handled by this function, return -1.
   1834  11022       Tom  *
   1835  11022       Tom  * XXX: It would be better for callers of the properety interface if we handled
   1836  11022       Tom  * these special cases in dsl_prop.c (in the dsl layer).
   1837  11022       Tom  */
   1838  11022       Tom static int
   1839  11022       Tom zfs_prop_set_special(const char *dsname, zprop_source_t source,
   1840  11022       Tom     nvpair_t *pair)
   1841  11022       Tom {
   1842  11022       Tom 	const char *propname = nvpair_name(pair);
   1843  11022       Tom 	zfs_prop_t prop = zfs_name_to_prop(propname);
   1844  11022       Tom 	uint64_t intval;
   1845  11022       Tom 	int err;
   1846  11022       Tom 
   1847  11022       Tom 	if (prop == ZPROP_INVAL) {
   1848  11022       Tom 		if (zfs_prop_userquota(propname))
   1849  11022       Tom 			return (zfs_prop_set_userquota(dsname, pair));
   1850  11022       Tom 		return (-1);
   1851  11022       Tom 	}
   1852  11022       Tom 
   1853  11022       Tom 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
   1854  11022       Tom 		nvlist_t *attrs;
   1855  11022       Tom 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
   1856  11022       Tom 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
   1857  11022       Tom 		    &pair) == 0);
   1858  11022       Tom 	}
   1859  11022       Tom 
   1860  11022       Tom 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
   1861  11022       Tom 		return (-1);
   1862  11022       Tom 
   1863  11022       Tom 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
   1864  11022       Tom 
   1865  11022       Tom 	switch (prop) {
   1866  11022       Tom 	case ZFS_PROP_QUOTA:
   1867  11022       Tom 		err = dsl_dir_set_quota(dsname, source, intval);
   1868  11022       Tom 		break;
   1869  11022       Tom 	case ZFS_PROP_REFQUOTA:
   1870  11022       Tom 		err = dsl_dataset_set_quota(dsname, source, intval);
   1871  11022       Tom 		break;
   1872  11022       Tom 	case ZFS_PROP_RESERVATION:
   1873  11022       Tom 		err = dsl_dir_set_reservation(dsname, source, intval);
   1874  11022       Tom 		break;
   1875  11022       Tom 	case ZFS_PROP_REFRESERVATION:
   1876  11022       Tom 		err = dsl_dataset_set_reservation(dsname, source, intval);
   1877  11022       Tom 		break;
   1878  11022       Tom 	case ZFS_PROP_VOLSIZE:
   1879  11022       Tom 		err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
   1880  11022       Tom 		    intval);
   1881  11022       Tom 		break;
   1882  11022       Tom 	case ZFS_PROP_VERSION:
   1883  11022       Tom 	{
   1884  11022       Tom 		zfsvfs_t *zfsvfs;
   1885  11022       Tom 
   1886  11022       Tom 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs)) != 0)
   1887  11022       Tom 			break;
   1888  11022       Tom 
   1889  11022       Tom 		err = zfs_set_version(zfsvfs, intval);
   1890  11022       Tom 		zfsvfs_rele(zfsvfs, FTAG);
   1891  11022       Tom 
   1892  11022       Tom 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
   1893  11147    George 			zfs_cmd_t *zc;
   1894  11147    George 
   1895  11147    George 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
   1896  11147    George 			(void) strcpy(zc->zc_name, dsname);
   1897  11147    George 			(void) zfs_ioc_userspace_upgrade(zc);
   1898  11147    George 			kmem_free(zc, sizeof (zfs_cmd_t));
   1899  11022       Tom 		}
   1900  11022       Tom 		break;
   1901  11022       Tom 	}
   1902  11022       Tom 
   1903  11022       Tom 	default:
   1904  11022       Tom 		err = -1;
   1905  11022       Tom 	}
   1906  11022       Tom 
   1907  11022       Tom 	return (err);
   1908  11022       Tom }
   1909  11022       Tom 
   1910  11022       Tom /*
   1911  11022       Tom  * This function is best effort. If it fails to set any of the given properties,
   1912  11022       Tom  * it continues to set as many as it can and returns the first error
   1913  11022       Tom  * encountered. If the caller provides a non-NULL errlist, it also gives the
   1914  11022       Tom  * complete list of names of all the properties it failed to set along with the
   1915  11022       Tom  * corresponding error numbers. The caller is responsible for freeing the
   1916  11022       Tom  * returned errlist.
   1917  11022       Tom  *
   1918  11022       Tom  * If every property is set successfully, zero is returned and the list pointed
   1919  11022       Tom  * at by errlist is NULL.
   1920  11022       Tom  */
   1921  11022       Tom int
   1922  11022       Tom zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
   1923  11022       Tom     nvlist_t **errlist)
   1924  11022       Tom {
   1925  11022       Tom 	nvpair_t *pair;
   1926  11022       Tom 	nvpair_t *propval;
   1927  11045       Tom 	int rv = 0;
   1928   2676  eschrock 	uint64_t intval;
   1929   2676  eschrock 	char *strval;
   1930   8697   Richard 	nvlist_t *genericnvl;
   1931  11022       Tom 	nvlist_t *errors;
   1932  11022       Tom 	nvlist_t *retrynvl;
   1933  11022       Tom 
   1934  11022       Tom 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
   1935  11022       Tom 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
   1936  11022       Tom 	VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
   1937  11022       Tom 
   1938  11022       Tom retry:
   1939  11022       Tom 	pair = NULL;
   1940  11022       Tom 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
   1941  11022       Tom 		const char *propname = nvpair_name(pair);
   1942   4670    ahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
   1943  11181       Tom 		int err = 0;
   1944   2676  eschrock 
   1945  11022       Tom 		/* decode the property value */
   1946  11022       Tom 		propval = pair;
   1947  11022       Tom 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
   1948  11022       Tom 			nvlist_t *attrs;
   1949  11022       Tom 			VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
   1950  11022       Tom 			VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
   1951  11022       Tom 			    &propval) == 0);
   1952  11022       Tom 		}
   1953  11022       Tom 
   1954  11022       Tom 		/* Validate value type */
   1955   5094     lling 		if (prop == ZPROP_INVAL) {
   1956  11022       Tom 			if (zfs_prop_user(propname)) {
   1957  11022       Tom 				if (nvpair_type(propval) != DATA_TYPE_STRING)
   1958  11022       Tom 					err = EINVAL;
   1959  11022       Tom 			} else if (zfs_prop_userquota(propname)) {
   1960  11022       Tom 				if (nvpair_type(propval) !=
   1961  11022       Tom 				    DATA_TYPE_UINT64_ARRAY)
   1962  11022       Tom 					err = EINVAL;
   1963  11022       Tom 			}
   1964  11022       Tom 		} else {
   1965  11022       Tom 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
   1966  11022       Tom 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
   1967  11022       Tom 					err = EINVAL;
   1968  11022       Tom 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
   1969   2885    ahrens 				const char *unused;
   1970   2885    ahrens 
   1971  11022       Tom 				VERIFY(nvpair_value_uint64(propval,
   1972  11022       Tom 				    &intval) == 0);
   1973   2676  eschrock 
   1974   2676  eschrock 				switch (zfs_prop_get_type(prop)) {
   1975   4787    ahrens 				case PROP_TYPE_NUMBER:
   1976   2676  eschrock 					break;
   1977   4787    ahrens 				case PROP_TYPE_STRING:
   1978  11022       Tom 					err = EINVAL;
   1979  11022       Tom 					break;
   1980   4787    ahrens 				case PROP_TYPE_INDEX:
   1981   2717  eschrock 					if (zfs_prop_index_to_string(prop,
   1982  11022       Tom 					    intval, &unused) != 0)
   1983  11022       Tom 						err = EINVAL;
   1984   2676  eschrock 					break;
   1985   2676  eschrock 				default:
   1986   4577    ahrens 					cmn_err(CE_PANIC,
   1987   4577    ahrens 					    "unknown property type");
   1988   2676  eschrock 				}
   1989   2676  eschrock 			} else {
   1990  11022       Tom 				err = EINVAL;
   1991  11022       Tom 			}
   1992  11022       Tom 		}
   1993  11022       Tom 
   1994  11022       Tom 		/* Validate permissions */
   1995  11022       Tom 		if (err == 0)
   1996  11022       Tom 			err = zfs_check_settable(dsname, pair, CRED());
   1997  11022       Tom 
   1998  11022       Tom 		if (err == 0) {
   1999  11022       Tom 			err = zfs_prop_set_special(dsname, source, pair);
   2000  11022       Tom 			if (err == -1) {
   2001  11022       Tom 				/*
   2002  11022       Tom 				 * For better performance we build up a list of
   2003  11022       Tom 				 * properties to set in a single transaction.
   2004  11022       Tom 				 */
   2005  11022       Tom 				err = nvlist_add_nvpair(genericnvl, pair);
   2006  11022       Tom 			} else if (err != 0 && nvl != retrynvl) {
   2007  11022       Tom 				/*
   2008  11022       Tom 				 * This may be a spurious error caused by
   2009  11022       Tom 				 * receiving quota and reservation out of order.
   2010  11022       Tom 				 * Try again in a second pass.
   2011  11022       Tom 				 */
   2012  11022       Tom 				err = nvlist_add_nvpair(retrynvl, pair);
   2013  11022       Tom 			}
   2014  11022       Tom 		}
   2015  11022       Tom 
   2016  11022       Tom 		if (err != 0)
   2017  11022       Tom 			VERIFY(nvlist_add_int32(errors, propname, err) == 0);
   2018  11022       Tom 	}
   2019  11022       Tom 
   2020  11022       Tom 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
   2021  11022       Tom 		nvl = retrynvl;
   2022  11022       Tom 		goto retry;
   2023  11022       Tom 	}
   2024  11022       Tom 
   2025  11022       Tom 	if (!nvlist_empty(genericnvl) &&
   2026  11022       Tom 	    dsl_props_set(dsname, source, genericnvl) != 0) {
   2027  11022       Tom 		/*
   2028  11022       Tom 		 * If this fails, we still want to set as many properties as we
   2029  11022       Tom 		 * can, so try setting them individually.
   2030  11022       Tom 		 */
   2031  11022       Tom 		pair = NULL;
   2032  11022       Tom 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
   2033  11022       Tom 			const char *propname = nvpair_name(pair);
   2034  11181       Tom 			int err = 0;
   2035  11022       Tom 
   2036  11022       Tom 			propval = pair;
   2037  11022       Tom 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
   2038  11022       Tom 				nvlist_t *attrs;
   2039  11022       Tom 				VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
   2040  11022       Tom 				VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
   2041  11022       Tom 				    &propval) == 0);
   2042  11022       Tom 			}
   2043  11022       Tom 
   2044  11022       Tom 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
   2045  11022       Tom 				VERIFY(nvpair_value_string(propval,
   2046  11022       Tom 				    &strval) == 0);
   2047  11022       Tom 				err = dsl_prop_set(dsname, propname, source, 1,
   2048  11022       Tom 				    strlen(strval) + 1, strval);
   2049  11022       Tom 			} else {
   2050  11022       Tom 				VERIFY(nvpair_value_uint64(propval,
   2051  11022       Tom 				    &intval) == 0);
   2052  11022       Tom 				err = dsl_prop_set(dsname, propname, source, 8,
   2053  11022       Tom 				    1, &intval);
   2054  11022       Tom 			}
   2055  11022       Tom 
   2056  11022       Tom 			if (err != 0) {
   2057  11022       Tom 				VERIFY(nvlist_add_int32(errors, propname,
   2058  11022       Tom 				    err) == 0);
   2059  11022       Tom 			}
   2060  11022       Tom 		}
   2061  11022       Tom 	}
   2062   8697   Richard 	nvlist_free(genericnvl);
   2063  11022       Tom 	nvlist_free(retrynvl);
   2064  11022       Tom 
   2065  11022       Tom 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
   2066  11022       Tom 		nvlist_free(errors);
   2067  11022       Tom 		errors = NULL;
   2068  11022       Tom 	} else {
   2069  11022       Tom 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
   2070  11022       Tom 	}
   2071  11022       Tom 
   2072  11022       Tom 	if (errlist == NULL)
   2073  11022       Tom 		nvlist_free(errors);
   2074  11022       Tom 	else
   2075  11022       Tom 		*errlist = errors;
   2076  11022       Tom 
   2077  11022       Tom 	return (rv);
   2078    789    ahrens }
   2079    789    ahrens 
   2080   5367    ahrens /*
   2081   9355   Matthew  * Check that all the properties are valid user properties.
   2082   9355   Matthew  */
   2083   9355   Matthew static int
   2084   9355   Matthew zfs_check_userprops(char *fsname, nvlist_t *nvl)
   2085   9355   Matthew {
   2086  11022       Tom 	nvpair_t *pair = NULL;
   2087  11022       Tom 	int error = 0;
   2088  11022       Tom 
   2089  11022       Tom 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
   2090  11022       Tom 		const char *propname = nvpair_name(pair);
   2091   9355   Matthew 		char *valstr;
   2092   9355   Matthew 
   2093   9355   Matthew 		if (!zfs_prop_user(propname) ||
   2094  11022       Tom 		    nvpair_type(pair) != DATA_TYPE_STRING)
   2095   9355   Matthew 			return (EINVAL);
   2096   9355   Matthew 
   2097   9355   Matthew 		if (error = zfs_secpolicy_write_perms(fsname,
   2098   9355   Matthew 		    ZFS_DELEG_PERM_USERPROP, CRED()))
   2099   9355   Matthew 			return (error);
   2100   9355   Matthew 
   2101   9355   Matthew 		if (strlen(propname) >= ZAP_MAXNAMELEN)
   2102   9355   Matthew 			return (ENAMETOOLONG);
   2103   9355   Matthew 
   2104  11022       Tom 		VERIFY(nvpair_value_string(pair, &valstr) == 0);
   2105   9355   Matthew 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
   2106   9355   Matthew 			return (E2BIG);
   2107   9355   Matthew 	}
   2108   9355   Matthew 	return (0);
   2109   9355   Matthew }
   2110   9355   Matthew 
   2111  11022       Tom static void
   2112  11022       Tom props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
   2113  11022       Tom {
   2114  11022       Tom 	nvpair_t *pair;
   2115  11022       Tom 
   2116  11022       Tom 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
   2117  11022       Tom 
   2118  11022       Tom 	pair = NULL;
   2119  11022       Tom 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
   2120  11022       Tom 		if (nvlist_exists(skipped, nvpair_name(pair)))
   2121  11022       Tom 			continue;
   2122  11022       Tom 
   2123  11022       Tom 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
   2124  11022       Tom 	}
   2125  11022       Tom }
   2126  11022       Tom 
   2127  11022       Tom static int
   2128  11022       Tom clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
   2129  11022       Tom     nvlist_t *skipped)
   2130  11022       Tom {
   2131  11022       Tom 	int err = 0;
   2132  11022       Tom 	nvlist_t *cleared_props = NULL;
   2133  11022       Tom 	props_skip(props, skipped, &cleared_props);
   2134  11022       Tom 	if (!nvlist_empty(cleared_props)) {
   2135  11022       Tom 		/*
   2136  11022       Tom 		 * Acts on local properties until the dataset has received
   2137  11022       Tom 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
   2138  11022       Tom 		 */
   2139  11022       Tom 		zprop_source_t flags = (ZPROP_SRC_NONE |
   2140  11022       Tom 		    (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
   2141  11022       Tom 		err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
   2142  11022       Tom 	}
   2143  11022       Tom 	nvlist_free(cleared_props);
   2144  11022       Tom 	return (err);
   2145  11022       Tom }
   2146  11022       Tom 
   2147   9355   Matthew /*
   2148   5367    ahrens  * inputs:
   2149   5367    ahrens  * zc_name		name of filesystem
   2150   8697   Richard  * zc_value		name of property to set
   2151   5367    ahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
   2152  11022       Tom  * zc_cookie		received properties flag
   2153  11022       Tom  *
   2154  11022       Tom  * outputs:
   2155  11022       Tom  * zc_nvlist_dst{_size} error for each unapplied received property
   2156   5367    ahrens  */
   2157    789    ahrens static int
   2158   2676  eschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
   2159    789    ahrens {
   2160   2676  eschrock 	nvlist_t *nvl;
   2161  11022       Tom 	boolean_t received = zc->zc_cookie;
   2162  11022       Tom 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
   2163  11022       Tom 	    ZPROP_SRC_LOCAL);
   2164  11022       Tom 	nvlist_t *errors = NULL;
   2165   2676  eschrock 	int error;
   2166    789    ahrens 
   2167   5094     lling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   2168   9643      Eric 	    zc->zc_iflags, &nvl)) != 0)
   2169   2676  eschrock 		return (error);
   2170   7265    ahrens 
   2171  11022       Tom 	if (received) {
   2172   7265    ahrens 		nvlist_t *origprops;
   2173   7265    ahrens 		objset_t *os;
   2174   7265    ahrens 
   2175  10298   Matthew 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
   2176  11022       Tom 			if (dsl_prop_get_received(os, &origprops) == 0) {
   2177  11022       Tom 				(void) clear_received_props(os,
   2178  11022       Tom 				    zc->zc_name, origprops, nvl);
   2179   7265    ahrens 				nvlist_free(origprops);
   2180   7265    ahrens 			}
   2181  11022       Tom 
   2182  11022       Tom 			dsl_prop_set_hasrecvd(os);
   2183  10298   Matthew 			dmu_objset_rele(os, FTAG);
   2184   7265    ahrens 		}
   2185  11022       Tom 	}
   2186  11022       Tom 
   2187  11022       Tom 	error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
   2188  11022       Tom 
   2189  11022       Tom 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
   2190  11022       Tom 		(void) put_nvlist(zc, errors);
   2191  11022       Tom 	}
   2192  11022       Tom 
   2193  11022       Tom 	nvlist_free(errors);
   2194   2676  eschrock 	nvlist_free(nvl);
   2195   3912     lling 	return (error);
   2196   4849    ahrens }
   2197   4849    ahrens 
   2198   5367    ahrens /*
   2199   5367    ahrens  * inputs:
   2200   5367    ahrens  * zc_name		name of filesystem
   2201   5367    ahrens  * zc_value		name of property to inherit
   2202  11022       Tom  * zc_cookie		revert to received value if TRUE
   2203   5367    ahrens  *
   2204   5367    ahrens  * outputs:		none
   2205   5367    ahrens  */
   2206   4849    ahrens static int
   2207   4849    ahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
   2208   4849    ahrens {
   2209  11022       Tom 	const char *propname = zc->zc_value;
   2210  11022       Tom 	zfs_prop_t prop = zfs_name_to_prop(propname);
   2211  11022       Tom 	boolean_t received = zc->zc_cookie;
   2212  11022       Tom 	zprop_source_t source = (received
   2213  11022       Tom 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
   2214  11022       Tom 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
   2215  11022       Tom 
   2216  11022       Tom 	if (received) {
   2217  11022       Tom 		nvlist_t *dummy;
   2218  11022       Tom 		nvpair_t *pair;
   2219  11022       Tom 		zprop_type_t type;
   2220  11022       Tom 		int err;
   2221  11022       Tom 
   2222  11022       Tom 		/*
   2223  11022       Tom 		 * zfs_prop_set_special() expects properties in the form of an
   2224  11022       Tom 		 * nvpair with type info.
   2225  11022       Tom 		 */
   2226  11022       Tom 		if (prop == ZPROP_INVAL) {
   2227  11022       Tom 			if (!zfs_prop_user(propname))
   2228  11022       Tom 				return (EINVAL);
   2229  11022       Tom 
   2230  11022       Tom 			type = PROP_TYPE_STRING;
   2231  11022       Tom 		} else {
   2232  11022       Tom 			type = zfs_prop_get_type(prop);
   2233  11022       Tom 		}
   2234  11022       Tom 
   2235  11022       Tom 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
   2236  11022       Tom 
   2237  11022       Tom 		switch (type) {
   2238  11022       Tom 		case PROP_TYPE_STRING:
   2239  11022       Tom 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
   2240  11022       Tom 			break;
   2241  11022       Tom 		case PROP_TYPE_NUMBER:
   2242  11022       Tom 		case PROP_TYPE_INDEX:
   2243  11022       Tom 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
   2244  11022       Tom 			break;
   2245  11022       Tom 		default:
   2246  11022       Tom 			nvlist_free(dummy);
   2247  11022       Tom 			return (EINVAL);
   2248  11022       Tom 		}
   2249  11022       Tom 
   2250  11022       Tom 		pair = nvlist_next_nvpair(dummy, NULL);
   2251  11022       Tom 		err = zfs_prop_set_special(zc->zc_name, source, pair);
   2252  11022       Tom 		nvlist_free(dummy);
   2253  11022       Tom 		if (err != -1)
   2254  11022       Tom 			return (err); /* special property already handled */
   2255  11022       Tom 	} else {
   2256  11022       Tom 		/*
   2257  11022       Tom 		 * Only check this in the non-received case. We want to allow
   2258  11022       Tom 		 * 'inherit -S' to revert non-inheritable properties like quota
   2259  11022       Tom 		 * and reservation to the received or default values even though
   2260  11022       Tom 		 * they are not considered inheritable.
   2261  11022       Tom 		 */
   2262  11022       Tom 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
   2263  11022       Tom 			return (EINVAL);
   2264  11022       Tom 	}
   2265  11022       Tom 
   2266   4849    ahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
   2267  11022       Tom 	return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
   2268   3912     lling }
   2269   3912     lling 
   2270   3912     lling static int
   2271   4098     lling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
   2272   3912     lling {
   2273   5094     lling 	nvlist_t *props;
   2274   3912     lling 	spa_t *spa;
   2275   5094     lling 	int error;
   2276  11022       Tom 	nvpair_t *pair;
   2277  11022       Tom 
   2278  11022       Tom 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   2279  11022       Tom 	    zc->zc_iflags, &props))
   2280   3912     lling 		return (error);
   2281   8525      Eric 
   2282   8525      Eric 	/*
   2283   8525      Eric 	 * If the only property is the configfile, then just do a spa_lookup()
   2284   8525      Eric 	 * to handle the faulted case.
   2285   8525      Eric 	 */
   2286  11022       Tom 	pair = nvlist_next_nvpair(props, NULL);
   2287  11022       Tom 	if (pair != NULL && strcmp(nvpair_name(pair),
   2288   8525      Eric 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
   2289  11022       Tom 	    nvlist_next_nvpair(props, pair) == NULL) {
   2290   8525      Eric 		mutex_enter(&spa_namespace_lock);
   2291   8525      Eric 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
   2292   8525      Eric 			spa_configfile_set(spa, props, B_FALSE);
   2293   8525      Eric 			spa_config_sync(spa, B_FALSE, B_TRUE);
   2294   8525      Eric 		}
   2295   8525      Eric 		mutex_exit(&spa_namespace_lock);
   2296  10672      Eric 		if (spa != NULL) {
   2297  10672      Eric 			nvlist_free(props);
   2298   8525      Eric 			return (0);
   2299  10672      Eric 		}
   2300   8525      Eric 	}
   2301   3912     lling 
   2302   3912     lling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
   2303   5094     lling 		nvlist_free(props);
   2304   3912     lling 		return (error);
   2305   3912     lling 	}
   2306   3912     lling 
   2307   5094     lling 	error = spa_prop_set(spa, props);
   2308   3912     lling 
   2309   5094     lling 	nvlist_free(props);
   2310   3912     lling 	spa_close(spa, FTAG);
   2311   3912     lling 
   2312   3912     lling 	return (error);
   2313   3912     lling }
   2314   3912     lling 
   2315   3912     lling static int
   2316   4098     lling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
   2317   3912     lling {
   2318   3912     lling 	spa_t *spa;
   2319   3912     lling 	int error;
   2320   3912     lling 	nvlist_t *nvp = NULL;
   2321   3912     lling 
   2322   8525      Eric 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
   2323   8525      Eric 		/*
   2324   8525      Eric 		 * If the pool is faulted, there may be properties we can still
   2325   8525      Eric 		 * get (such as altroot and cachefile), so attempt to get them
   2326   8525      Eric 		 * anyway.
   2327   8525      Eric 		 */
   2328   8525      Eric 		mutex_enter(&spa_namespace_lock);
   2329   8525      Eric 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
   2330   8525      Eric 			error = spa_prop_get(spa, &nvp);
   2331   8525      Eric 		mutex_exit(&spa_namespace_lock);
   2332   8525      Eric 	} else {
   2333   8525      Eric 		error = spa_prop_get(spa, &nvp);
   2334   8525      Eric 		spa_close(spa, FTAG);
   2335   8525      Eric 	}
   2336   3912     lling 
   2337   3912     lling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
   2338   3912     lling 		error = put_nvlist(zc, nvp);
   2339   3912     lling 	else
   2340   3912     lling 		error = EFAULT;
   2341   3912     lling 
   2342   8525      Eric 	nvlist_free(nvp);
   2343   2676  eschrock 	return (error);
   2344    789    ahrens }
   2345    789    ahrens 
   2346    789    ahrens static int
   2347   4543     marks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
   2348   4543     marks {
   2349   4543     marks 	nvlist_t *nvp;
   2350   4543     marks 	int error;
   2351   4543     marks 	uint32_t uid;
   2352   4543     marks 	uint32_t gid;
   2353   4543     marks 	uint32_t *groups;
   2354   4543     marks 	uint_t group_cnt;
   2355   4543     marks 	cred_t	*usercred;
   2356   4543     marks 
   2357   5094     lling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   2358   9643      Eric 	    zc->zc_iflags, &nvp)) != 0) {
   2359   4543     marks 		return (error);
   2360   4543     marks 	}
   2361   4543     marks 
   2362   4543     marks 	if ((error = nvlist_lookup_uint32(nvp,
   2363   4543     marks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
   2364   4543     marks 		nvlist_free(nvp);
   2365   4543     marks 		return (EPERM);
   2366   4543     marks 	}
   2367   4543     marks 
   2368   4543     marks 	if ((error = nvlist_lookup_uint32(nvp,
   2369   4543     marks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
   2370   4543     marks 		nvlist_free(nvp);
   2371   4543     marks 		return (EPERM);
   2372   4543     marks 	}
   2373   4543     marks 
   2374   4543     marks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
   2375   4543     marks 	    &groups, &group_cnt)) != 0) {
   2376   4543     marks 		nvlist_free(nvp);
   2377   4543     marks 		return (EPERM);
   2378   4543     marks 	}
   2379   4543     marks 	usercred = cralloc();
   2380   4543     marks 	if ((crsetugid(usercred, uid, gid) != 0) ||
   2381   4543     marks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
   2382   4543     marks 		nvlist_free(nvp);
   2383   4543     marks 		crfree(usercred);
   2384   4543     marks 		return (EPERM);
   2385   4543     marks 	}
   2386   4543     marks 	nvlist_free(nvp);
   2387   4543     marks 	error = dsl_deleg_access(zc->zc_name,
   2388   4787    ahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
   2389   4543     marks 	crfree(usercred);
   2390   4543     marks 	return (error);
   2391   4543     marks }
   2392   4543     marks 
   2393   5367    ahrens /*
   2394   5367    ahrens  * inputs:
   2395   5367    ahrens  * zc_name		name of filesystem
   2396   5367    ahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
   2397   5367    ahrens  * zc_perm_action	allow/unallow flag
   2398   5367    ahrens  *
   2399   5367    ahrens  * outputs:		none
   2400   5367    ahrens  */
   2401   4543     marks static int
   2402   4543     marks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
   2403   4543     marks {
   2404   4543     marks 	int error;
   2405   4543     marks 	nvlist_t *fsaclnv = NULL;
   2406   4543     marks 
   2407   5094     lling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   2408   9643      Eric 	    zc->zc_iflags, &fsaclnv)) != 0)
   2409   4543     marks 		return (error);
   2410   4543     marks 
   2411   4543     marks 	/*
   2412   4543     marks 	 * Verify nvlist is constructed correctly
   2413   4543     marks 	 */
   2414   4543     marks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
   2415   4543     marks 		nvlist_free(fsaclnv);
   2416   4543     marks 		return (EINVAL);
   2417   4543     marks 	}
   2418   4543     marks 
   2419   4543     marks 	/*
   2420   4543     marks 	 * If we don't have PRIV_SYS_MOUNT, then validate
   2421   4543     marks 	 * that user is allowed to hand out each permission in
   2422   4543     marks 	 * the nvlist(s)
   2423   4543     marks 	 */
   2424   4543     marks 
   2425   4787    ahrens 	error = secpolicy_zfs(CRED());
   2426   4543     marks 	if (error) {
   2427   4787    ahrens 		if (zc->zc_perm_action == B_FALSE) {
   2428   4787    ahrens 			error = dsl_deleg_can_allow(zc->zc_name,
   2429   4787    ahrens 			    fsaclnv, CRED());
   2430   4787    ahrens 		} else {
   2431   4787    ahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
   2432   4787    ahrens 			    fsaclnv, CRED());
   2433   4787    ahrens 		}
   2434   4543     marks 	}
   2435   4543     marks 
   2436   4543     marks 	if (error == 0)
   2437   4543     marks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
   2438   4543     marks 
   2439   4543     marks 	nvlist_free(fsaclnv);
   2440   4543     marks 	return (error);
   2441   4543     marks }
   2442   4543     marks 
   2443   5367    ahrens /*
   2444   5367    ahrens  * inputs:
   2445   5367    ahrens  * zc_name		name of filesystem
   2446   5367    ahrens  *
   2447   5367    ahrens  * outputs:
   2448   5367    ahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
   2449   5367    ahrens  */
   2450   4543     marks static int
   2451   4543     marks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
   2452   4543     marks {
   2453   4543     marks 	nvlist_t *nvp;
   2454   4543     marks 	int error;
   2455   4543     marks 
   2456   4543     marks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
   2457   4543     marks 		error = put_nvlist(zc, nvp);
   2458   4543     marks 		nvlist_free(nvp);
   2459   4543     marks 	}
   2460   4543     marks 
   2461   4543     marks 	return (error);
   2462   4543     marks }
   2463   4543     marks 
   2464   5367    ahrens /*
   2465    789    ahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
   2466    789    ahrens  * or NULL if no suitable entry is found. The caller of this routine
   2467    789    ahrens  * is responsible for releasing the returned vfs pointer.
   2468    789    ahrens  */
   2469    789    ahrens static vfs_t *
   2470    789    ahrens zfs_get_vfs(const char *resource)
   2471    789    ahrens {
   2472    789    ahrens 	struct vfs *vfsp;
   2473    789    ahrens 	struct vfs *vfs_found = NULL;
   2474    789    ahrens 
   2475    789    ahrens 	vfs_list_read_lock();
   2476    789    ahrens 	vfsp = rootvfs;
   2477    789    ahrens 	do {
   2478    789    ahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
   2479    789    ahrens 			VFS_HOLD(vfsp);
   2480    789    ahrens 			vfs_found = vfsp;
   2481    789    ahrens 			break;
   2482    789    ahrens 		}
   2483    789    ahrens 		vfsp = vfsp->vfs_next;
   2484    789    ahrens 	} while (vfsp != rootvfs);
   2485    789    ahrens 	vfs_list_unlock();
   2486    789    ahrens 	return (vfs_found);
   2487    789    ahrens }
   2488    789    ahrens 
   2489   4543     marks /* ARGSUSED */
   2490    789    ahrens static void
   2491   4543     marks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
   2492    789    ahrens {
   2493   5331       amw 	zfs_creat_t *zct = arg;
   2494   4577    ahrens 
   2495   5498      timh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
   2496   5331       amw }
   2497   5331       amw 
   2498   5498      timh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
   2499   5498      timh 
   2500   5331       amw /*
   2501   5498      timh  * inputs:
   2502   7184      timh  * createprops		list of properties requested by creator
   2503   7184      timh  * default_zplver	zpl version to use if unspecified in createprops
   2504   7184      timh  * fuids_ok		fuids allowed in this version of the spa?
   2505   7184      timh  * os			parent objset pointer (NULL if root fs)
   2506   5331       amw  *
   2507   5498      timh  * outputs:
   2508   5498      timh  * zplprops	values for the zplprops we attach to the master node object
   2509   7184      timh  * is_ci	true if requested file system will be purely case-insensitive
   2510   5331       amw  *
   2511   5498      timh  * Determine the settings for utf8only, normalization and
   2512   5498      timh  * casesensitivity.  Specific values may have been requested by the
   2513   5498      timh  * creator and/or we can inherit values from the parent dataset.  If
   2514   5498      timh  * the file system is of too early a vintage, a creator can not
   2515   5498      timh  * request settings for these properties, even if the requested
   2516   5498      timh  * setting is the default value.  We don't actually want to create dsl
   2517   5498      timh  * properties for these, so remove them from the source nvlist after
   2518   5498      timh  * processing.
   2519   5331       amw  */
   2520   5331       amw static int
   2521   9396   Matthew zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
   2522   7184      timh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
   2523   7184      timh     boolean_t *is_ci)
   2524   5331       amw {
   2525   5498      timh 	uint64_t sense = ZFS_PROP_UNDEFINED;
   2526   5498      timh 	uint64_t norm = ZFS_PROP_UNDEFINED;
   2527   5498      timh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
   2528   5331       amw 
   2529   5498      timh 	ASSERT(zplprops != NULL);
   2530   5331       amw 
   2531   5375      timh 	/*
   2532   5498      timh 	 * Pull out creator prop choices, if any.
   2533   5375      timh 	 */
   2534   5498      timh 	if (createprops) {
   2535   7184      timh 		(void) nvlist_lookup_uint64(createprops,
   2536   7184      timh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
   2537   5498      timh 		(void) nvlist_lookup_uint64(createprops,
   2538   5498      timh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
   2539   5498      timh 		(void) nvlist_remove_all(createprops,
   2540   5498      timh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
   2541   5498      timh 		(void) nvlist_lookup_uint64(createprops,
   2542   5498      timh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
   2543   5498      timh 		(void) nvlist_remove_all(createprops,
   2544   5498      timh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
   2545   5498      timh 		(void) nvlist_lookup_uint64(createprops,
   2546   5498      timh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
   2547   5498      timh 		(void) nvlist_remove_all(createprops,
   2548   5498      timh 		    zfs_prop_to_name(ZFS_PROP_CASE));
   2549   5331       amw 	}
   2550   5331       amw 
   2551   5375      timh 	/*
   2552   7184      timh 	 * If the zpl version requested is whacky or the file system
   2553   7184      timh 	 * or pool is version is too "young" to support normalization
   2554   7184      timh 	 * and the creator tried to set a value for one of the props,
   2555   7184      timh 	 * error out.
   2556   5375      timh 	 */
   2557   7184      timh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
   2558   7184      timh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
   2559   7184      timh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
   2560   5498      timh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
   2561   7184      timh 	    sense != ZFS_PROP_UNDEFINED)))
   2562   5375      timh 		return (ENOTSUP);
   2563   5375      timh 
   2564   5498      timh 	/*
   2565   5498      timh 	 * Put the version in the zplprops
   2566   5498      timh 	 */
   2567   5498      timh 	VERIFY(nvlist_add_uint64(zplprops,
   2568   5498      timh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
   2569   5498      timh 
   2570   5498      timh 	if (norm == ZFS_PROP_UNDEFINED)
   2571   5498      timh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
   2572   5498      timh 	VERIFY(nvlist_add_uint64(zplprops,
   2573   5498      timh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
   2574   5498      timh 
   2575   5498      timh 	/*
   2576   5498      timh 	 * If we're normalizing, names must always be valid UTF-8 strings.
   2577   5498      timh 	 */
   2578   5498      timh 	if (norm)
   2579   5498      timh 		u8 = 1;
   2580   5498      timh 	if (u8 == ZFS_PROP_UNDEFINED)
   2581   5498      timh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
   2582   5498      timh 	VERIFY(nvlist_add_uint64(zplprops,
   2583   5498      timh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
   2584   5498      timh 
   2585   5498      timh 	if (sense == ZFS_PROP_UNDEFINED)
   2586   5498      timh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
   2587   5498      timh 	VERIFY(nvlist_add_uint64(zplprops,
   2588   5498      timh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
   2589   5498      timh 
   2590   6492      timh 	if (is_ci)
   2591   6492      timh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
   2592   6492      timh 
   2593   7184      timh 	return (0);
   2594   7184      timh }
   2595   7184      timh 
   2596   7184      timh static int
   2597   7184      timh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
   2598   7184      timh     nvlist_t *zplprops, boolean_t *is_ci)
   2599   7184      timh {
   2600   7184      timh 	boolean_t fuids_ok = B_TRUE;
   2601   7184      timh 	uint64_t zplver = ZPL_VERSION;
   2602   7184      timh 	objset_t *os = NULL;
   2603   7184      timh 	char parentname[MAXNAMELEN];
   2604   7184      timh 	char *cp;
   2605   7184      timh 	int error;
   2606   7184      timh 
   2607   7184      timh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
   2608   7184      timh 	cp = strrchr(parentname, '/');
   2609   7184      timh 	ASSERT(cp != NULL);
   2610   7184      timh 	cp[0] = '\0';
   2611   7184      timh 
   2612   9396   Matthew 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
   2613   9396   Matthew 		zplver = ZPL_VERSION_USERSPACE - 1;
   2614   7184      timh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
   2615   7184      timh 		zplver = ZPL_VERSION_FUID - 1;
   2616   7184      timh 		fuids_ok = B_FALSE;
   2617   7184      timh 	}
   2618   7184      timh 
   2619   7184      timh 	/*
   2620   7184      timh 	 * Open parent object set so we can inherit zplprop values.
   2621   7184      timh 	 */
   2622  10298   Matthew 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
   2623   7184      timh 		return (error);
   2624   7184      timh 
   2625   7184      timh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
   2626   7184      timh 	    zplprops, is_ci);
   2627  10298   Matthew 	dmu_objset_rele(os, FTAG);
   2628   7184      timh 	return (error);
   2629   7184      timh }
   2630   7184      timh 
   2631   7184      timh static int
   2632   7184      timh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
   2633   7184      timh     nvlist_t *zplprops, boolean_t *is_ci)
   2634   7184      timh {
   2635   7184      timh 	boolean_t fuids_ok = B_TRUE;
   2636   7184      timh 	uint64_t zplver = ZPL_VERSION;
   2637   7184      timh 	int error;
   2638   7184      timh 
   2639   7184      timh 	if (spa_vers < SPA_VERSION_FUID) {
   2640   7184      timh 		zplver = ZPL_VERSION_FUID - 1;
   2641   7184      timh 		fuids_ok = B_FALSE;
   2642   7184      timh 	}
   2643   7184      timh 
   2644   7184      timh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
   2645   7184      timh 	    zplprops, is_ci);
   2646   7184      timh 	return (error);
   2647    789    ahrens }
   2648    789    ahrens 
   2649   5367    ahrens /*
   2650   5367    ahrens  * inputs:
   2651   5367    ahrens  * zc_objset_type	type of objset to create (fs vs zvol)
   2652   5367    ahrens  * zc_name		name of new objset
   2653   5367    ahrens  * zc_value		name of snapshot to clone from (may be empty)
   2654   5367    ahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
   2655   5367    ahrens  *
   2656   5498      timh  * outputs: none
   2657   5367    ahrens  */
   2658    789    ahrens static int
   2659    789    ahrens zfs_ioc_create(zfs_cmd_t *zc)
   2660    789    ahrens {
   2661    789    ahrens 	objset_t *clone;
   2662    789    ahrens 	int error = 0;
   2663   5331       amw 	zfs_creat_t zct;
   2664   4543     marks 	nvlist_t *nvprops = NULL;
   2665   4543     marks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
   2666    789    ahrens 	dmu_objset_type_t type = zc->zc_objset_type;
   2667    789    ahrens 
   2668    789    ahrens 	switch (type) {
   2669    789    ahrens 
   2670    789    ahrens 	case DMU_OST_ZFS:
   2671    789    ahrens 		cbfunc = zfs_create_cb;
   2672    789    ahrens 		break;
   2673    789    ahrens 
   2674    789    ahrens 	case DMU_OST_ZVOL:
   2675    789    ahrens 		cbfunc = zvol_create_cb;
   2676    789    ahrens 		break;
   2677    789    ahrens 
   2678    789    ahrens 	default:
   2679   2199    ahrens 		cbfunc = NULL;
   2680   6423   gw25295 		break;
   2681   2199    ahrens 	}
   2682   5326  ek110237 	if (strchr(zc->zc_name, '@') ||
   2683   5326  ek110237 	    strchr(zc->zc_name, '%'))
   2684    789    ahrens 		return (EINVAL);
   2685    789    ahrens 
   2686   2676  eschrock 	if (zc->zc_nvlist_src != NULL &&
   2687   5094     lling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   2688   9643      Eric 	    zc->zc_iflags, &nvprops)) != 0)
   2689   2676  eschrock 		return (error);
   2690   2676  eschrock 
   2691   5498      timh 	zct.zct_zplprops = NULL;
   2692   5331       amw 	zct.zct_props = nvprops;
   2693   5331       amw 
   2694   2676  eschrock 	if (zc->zc_value[0] != '\0') {
   2695    789    ahrens 		/*
   2696    789    ahrens 		 * We're creating a clone of an existing snapshot.
   2697    789    ahrens 		 */
   2698   2676  eschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
   2699   2676  eschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
   2700   4543     marks 			nvlist_free(nvprops);
   2701    789    ahrens 			return (EINVAL);
   2702   2676  eschrock 		}
   2703    789    ahrens 
   2704  10298   Matthew 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
   2705   2676  eschrock 		if (error) {
   2706   4543     marks 			nvlist_free(nvprops);
   2707    789    ahrens 			return (error);
   2708   2676  eschrock 		}
   2709   6492      timh 
   2710  10272   Matthew 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
   2711  10298   Matthew 		dmu_objset_rele(clone, FTAG);
   2712   5331       amw 		if (error) {
   2713   5331       amw 			nvlist_free(nvprops);
   2714   5331       amw 			return (error);
   2715   5331       amw 		}
   2716    789    ahrens 	} else {
   2717   6492      timh 		boolean_t is_insensitive = B_FALSE;
   2718   6492      timh 
   2719   2676  eschrock 		if (cbfunc == NULL) {
   2720   4543     marks 			nvlist_free(nvprops);
   2721   2199    ahrens 			return (EINVAL);
   2722   2676  eschrock 		}
   2723   2676  eschrock 
   2724    789    ahrens 		if (type == DMU_OST_ZVOL) {
   2725   2676  eschrock 			uint64_t volsize, volblocksize;
   2726   1133  eschrock 
   2727   4543     marks 			if (nvprops == NULL ||
   2728   4543     marks 			    nvlist_lookup_uint64(nvprops,
   2729   2676  eschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
   2730   2676  eschrock 			    &volsize) != 0) {
   2731   4543     marks 				nvlist_free(nvprops);
   2732   2676  eschrock 				return (EINVAL);
   2733   2676  eschrock 			}
   2734   2676  eschrock 
   2735   4543     marks 			if ((error = nvlist_lookup_uint64(nvprops,
   2736   2676  eschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
   2737   2676  eschrock 			    &volblocksize)) != 0 && error != ENOENT) {
   2738   4543     marks 				nvlist_free(nvprops);
   2739   2676  eschrock 				return (EINVAL);
   2740   2676  eschrock 			}
   2741   2676  eschrock 
   2742   2676  eschrock 			if (error != 0)
   2743   2676  eschrock 				volblocksize = zfs_prop_default_numeric(
   2744   2676  eschrock 				    ZFS_PROP_VOLBLOCKSIZE);
   2745   2676  eschrock 
   2746   2676  eschrock 			if ((error = zvol_check_volblocksize(
   2747   2676  eschrock 			    volblocksize)) != 0 ||
   2748   2676  eschrock 			    (error = zvol_check_volsize(volsize,
   2749   2676  eschrock 			    volblocksize)) != 0) {
   2750   4543     marks 				nvlist_free(nvprops);
   2751    789    ahrens 				return (error);
   2752   4577    ahrens 			}
   2753   4577    ahrens 		} else if (type == DMU_OST_ZFS) {
   2754   5331       amw 			int error;
   2755   5331       amw 
   2756   5331       amw 			/*
   2757   5331       amw 			 * We have to have normalization and
   2758   5331       amw 			 * case-folding flags correct when we do the
   2759   5331       amw 			 * file system creation, so go figure them out
   2760   5498      timh 			 * now.
   2761   5331       amw 			 */
   2762   5498      timh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
   2763   5498      timh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
   2764   5498      timh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
   2765   7184      timh 			    zct.zct_zplprops, &is_insensitive);
   2766   5331       amw 			if (error != 0) {
   2767   5331       amw 				nvlist_free(nvprops);
   2768   5498      timh 				nvlist_free(zct.zct_zplprops);
   2769   5331       amw 				return (error);
   2770   2676  eschrock 			}
   2771   2676  eschrock 		}
   2772  10272   Matthew 		error = dmu_objset_create(zc->zc_name, type,
   2773   6492      timh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
   2774   5498      timh 		nvlist_free(zct.zct_zplprops);
   2775    789    ahrens 	}
   2776   2676  eschrock 
   2777   2676  eschrock 	/*
   2778   2676  eschrock 	 * It would be nice to do this atomically.
   2779   2676  eschrock 	 */
   2780   2676  eschrock 	if (error == 0) {
   2781  11022       Tom 		error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
   2782  11022       Tom 		    nvprops, NULL);
   2783  11022       Tom 		if (error != 0)
   2784  10242     chris 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
   2785   2676  eschrock 	}
   2786   4543     marks 	nvlist_free(nvprops);
   2787    789    ahrens 	return (error);
   2788    789    ahrens }
   2789    789    ahrens 
   2790   5367    ahrens /*
   2791   5367    ahrens  * inputs:
   2792   5367    ahrens  * zc_name	name of filesystem
   2793   5367    ahrens  * zc_value	short name of snapshot
   2794   5367    ahrens  * zc_cookie	recursive flag
   2795   9396   Matthew  * zc_nvlist_src[_size] property list
   2796   5367    ahrens  *
   2797  10588      Eric  * outputs:
   2798  10588      Eric  * zc_value	short snapname (i.e. part after the '@')
   2799   5367    ahrens  */
   2800    789    ahrens static int
   2801   2199    ahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
   2802   2199    ahrens {
   2803   7265    ahrens 	nvlist_t *nvprops = NULL;
   2804   7265    ahrens 	int error;
   2805   7265    ahrens 	boolean_t recursive = zc->zc_cookie;
   2806   7265    ahrens 
   2807   2676  eschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
   2808   2199    ahrens 		return (EINVAL);
   2809   7265    ahrens 
   2810   7265    ahrens 	if (zc->zc_nvlist_src != NULL &&
   2811   7265    ahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   2812   9643      Eric 	    zc->zc_iflags, &nvprops)) != 0)
   2813   7265    ahrens 		return (error);
   2814   7265    ahrens 
   2815   9355   Matthew 	error = zfs_check_userprops(zc->zc_name, nvprops);
   2816   9355   Matthew 	if (error)
   2817   9355   Matthew 		goto out;
   2818   7265    ahrens 
   2819  11022       Tom 	if (!nvlist_empty(nvprops) &&
   2820   9355   Matthew 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
   2821   9355   Matthew 		error = ENOTSUP;
   2822   9355   Matthew 		goto out;
   2823   7265    ahrens 	}
   2824   9355   Matthew 
   2825   9355   Matthew 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
   2826   9355   Matthew 	    nvprops, recursive);
   2827   9355   Matthew 
   2828   9355   Matthew out:
   2829   7265    ahrens 	nvlist_free(nvprops);
   2830   7265    ahrens 	return (error);
   2831   2199    ahrens }
   2832   2199    ahrens 
   2833   4007  mmusante int
   2834   2199    ahrens zfs_unmount_snap(char *name, void *arg)
   2835   2199    ahrens {
   2836   2417    ahrens 	vfs_t *vfsp = NULL;
   2837   2199    ahrens 
   2838   6689    maybee 	if (arg) {
   2839   6689    maybee 		char *snapname = arg;
   2840   6689    maybee 		int len = strlen(name) + strlen(snapname) + 2;
   2841   6689    maybee 		char *buf = kmem_alloc(len, KM_SLEEP);
   2842   2199    ahrens 
   2843   6689    maybee 		(void) strcpy(buf, name);
   2844   6689    maybee 		(void) strcat(buf, "@");
   2845   6689    maybee 		(void) strcat(buf, snapname);
   2846   6689    maybee 		vfsp = zfs_get_vfs(buf);
   2847   6689    maybee 		kmem_free(buf, len);
   2848   2417    ahrens 	} else if (strchr(name, '@')) {
   2849   2199    ahrens 		vfsp = zfs_get_vfs(name);
   2850   2199    ahrens 	}
   2851   2199    ahrens 
   2852   2199    ahrens 	if (vfsp) {
   2853   2199    ahrens 		/*
   2854   2199    ahrens 		 * Always force the unmount for snapshots.
   2855   2199    ahrens 		 */
   2856   2199    ahrens 		int flag = MS_FORCE;
   2857   2199    ahrens 		int err;
   2858   2199    ahrens 
   2859   2199    ahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
   2860   2199    ahrens 			VFS_RELE(vfsp);
   2861   2199    ahrens 			return (err);
   2862   2199    ahrens 		}
   2863   2199    ahrens 		VFS_RELE(vfsp);
   2864   2199    ahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
   2865   2199    ahrens 			return (err);
   2866   2199    ahrens 	}
   2867   2199    ahrens 	return (0);
   2868   2199    ahrens }
   2869   2199    ahrens 
   2870   5367    ahrens /*
   2871   5367    ahrens  * inputs:
   2872  10242     chris  * zc_name		name of filesystem
   2873  10242     chris  * zc_value		short name of snapshot
   2874  10242     chris  * zc_defer_destroy	mark for deferred destroy
   2875   5367    ahrens  *
   2876   5367    ahrens  * outputs:	none
   2877   5367    ahrens  */
   2878   2199    ahrens static int
   2879   2199    ahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
   2880   2199    ahrens {
   2881   2199    ahrens 	int err;
   2882   2199    ahrens 
   2883   2676  eschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
   2884   2199    ahrens 		return (EINVAL);
   2885   2199    ahrens 	err = dmu_objset_find(zc->zc_name,
   2886   2676  eschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
   2887   2199    ahrens 	if (err)
   2888   2199    ahrens 		return (err);
   2889  10242     chris 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
   2890  10242     chris 	    zc->zc_defer_destroy));
   2891   2199    ahrens }
   2892   2199    ahrens 
   2893   5367    ahrens /*
   2894   5367    ahrens  * inputs:
   2895   5367    ahrens  * zc_name		name of dataset to destroy
   2896   5367    ahrens  * zc_objset_type	type of objset
   2897  10242     chris  * zc_defer_destroy	mark for deferred destroy
   2898   5367    ahrens  *
   2899   5367    ahrens  * outputs:		none
   2900   5367    ahrens  */
   2901   2199    ahrens static int
   2902    789    ahrens zfs_ioc_destroy(zfs_cmd_t *zc)
   2903    789    ahrens {
   2904  10588      Eric 	int err;
   2905   2199    ahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
   2906  10588      Eric 		err = zfs_unmount_snap(zc->zc_name, NULL);
   2907   2199    ahrens 		if (err)
   2908   2199    ahrens 			return (err);
   2909    789    ahrens 	}
   2910    789    ahrens 
   2911  10588      Eric 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
   2912  10588      Eric 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
   2913  10693     chris 		(void) zvol_remove_minor(zc->zc_name);
   2914  10588      Eric 	return (err);
   2915    789    ahrens }
   2916    789    ahrens 
   2917   5367    ahrens /*
   2918   5367    ahrens  * inputs:
   2919   5446    ahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
   2920   5367    ahrens  *
   2921   5367    ahrens  * outputs:	none
   2922   5367    ahrens  */
   2923    789    ahrens static int
   2924    789    ahrens zfs_ioc_rollback(zfs_cmd_t *zc)
   2925    789    ahrens {
   2926  10272   Matthew 	dsl_dataset_t *ds, *clone;
   2927   5446    ahrens 	int error;
   2928  10272   Matthew 	zfsvfs_t *zfsvfs;
   2929  10272   Matthew 	char *clone_name;
   2930   5446    ahrens 
   2931  10272   Matthew 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
   2932   5446    ahrens 	if (error)
   2933   5446    ahrens 		return (error);
   2934   5446    ahrens 
   2935  10272   Matthew 	/* must not be a snapshot */
   2936  10272   Matthew 	if (dsl_dataset_is_snapshot(ds)) {
   2937  10272   Matthew 		dsl_dataset_rele(ds, FTAG);
   2938  10272   Matthew 		return (EINVAL);
   2939  10272   Matthew 	}
   2940  10272   Matthew 
   2941  10272   Matthew 	/* must have a most recent snapshot */
   2942  10272   Matthew 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
   2943  10272   Matthew 		dsl_dataset_rele(ds, FTAG);
   2944  10272   Matthew 		return (EINVAL);
   2945  10272   Matthew 	}
   2946  10272   Matthew 
   2947  10272   Matthew 	/*
   2948  10272   Matthew 	 * Create clone of most recent snapshot.
   2949  10272   Matthew 	 */
   2950  10272   Matthew 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
   2951  10272   Matthew 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
   2952  10272   Matthew 	if (error)
   2953  10272   Matthew 		goto out;
   2954  10272   Matthew 
   2955  10298   Matthew 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
   2956  10272   Matthew 	if (error)
   2957  10272   Matthew 		goto out;
   2958  10272   Matthew 
   2959  10272   Matthew 	/*
   2960  10272   Matthew 	 * Do clone swap.
   2961  10272   Matthew 	 */
   2962   9396   Matthew 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
   2963  10298   Matthew 		error = zfs_suspend_fs(zfsvfs);
   2964   6083  ek110237 		if (error == 0) {
   2965   6083  ek110237 			int resume_err;
   2966   5446    ahrens 
   2967  10272   Matthew 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
   2968  10272   Matthew 				error = dsl_dataset_clone_swap(clone, ds,
   2969  10272   Matthew 				    B_TRUE);
   2970  10272   Matthew 				dsl_dataset_disown(ds, FTAG);
   2971  10272   Matthew 				ds = NULL;
   2972  10272   Matthew 			} else {
   2973  10272   Matthew 				error = EBUSY;
   2974  10272   Matthew 			}
   2975  10298   Matthew 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
   2976   6083  ek110237 			error = error ? error : resume_err;
   2977   6083  ek110237 		}
   2978   5446    ahrens 		VFS_RELE(zfsvfs->z_vfs);
   2979   5446    ahrens 	} else {
   2980  10272   Matthew 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
   2981  10272   Matthew 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
   2982  10272   Matthew 			dsl_dataset_disown(ds, FTAG);
   2983  10272   Matthew 			ds = NULL;
   2984  10272   Matthew 		} else {
   2985  10272   Matthew 			error = EBUSY;
   2986  10272   Matthew 		}
   2987   5446    ahrens 	}
   2988   5446    ahrens 
   2989  10272   Matthew 	/*
   2990  10272   Matthew 	 * Destroy clone (which also closes it).
   2991  10272   Matthew 	 */
   2992  10272   Matthew 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
   2993  10272   Matthew 
   2994  10272   Matthew out:
   2995  10272   Matthew 	strfree(clone_name);
   2996  10272   Matthew 	if (ds)
   2997  10272   Matthew 		dsl_dataset_rele(ds, FTAG);
   2998   5446    ahrens 	return (error);
   2999    789    ahrens }
   3000    789    ahrens 
   3001   5367    ahrens /*
   3002   5367    ahrens  * inputs:
   3003   5367    ahrens  * zc_name	old name of dataset
   3004   5367    ahrens  * zc_value	new name of dataset
   3005   5367    ahrens  * zc_cookie	recursive flag (only valid for snapshots)
   3006   5367    ahrens  *
   3007   5367    ahrens  * outputs:	none
   3008   5367    ahrens  */
   3009    789    ahrens static int
   3010    789    ahrens zfs_ioc_rename(zfs_cmd_t *zc)
   3011    789    ahrens {
   3012   4490  vb160487 	boolean_t recursive = zc->zc_cookie & 1;
   3013   4007  mmusante 
   3014   2676  eschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
   3015   5326  ek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
   3016   5326  ek110237 	    strchr(zc->zc_value, '%'))
   3017    789    ahrens 		return (EINVAL);
   3018    789    ahrens 
   3019   4007  mmusante 	/*
   3020   4007  mmusante 	 * Unmount snapshot unless we're doing a recursive rename,
   3021   4007  mmusante 	 * in which case the dataset code figures out which snapshots
   3022   4007  mmusante 	 * to unmount.
   3023   4007  mmusante 	 */
   3024   4007  mmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
   3025    789    ahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
   3026   2199    ahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
   3027   2199    ahrens 		if (err)
   3028   2199    ahrens 			return (err);
   3029    789    ahrens 	}
   3030  10588      Eric 	if (zc->zc_objset_type == DMU_OST_ZVOL)
   3031  10588      Eric 		(void) zvol_remove_minor(zc->zc_name);
   3032   6689    maybee 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
   3033   6689    maybee }
   3034    789    ahrens 
   3035  11022       Tom static int
   3036  11022       Tom zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
   3037  11022       Tom {
   3038  11022       Tom 	const char *propname = nvpair_name(pair);
   3039  11022       Tom 	boolean_t issnap = (strchr(dsname, '@') != NULL);
   3040  11022       Tom 	zfs_prop_t prop = zfs_name_to_prop(propname);
   3041  11022       Tom 	uint64_t intval;
   3042  11022       Tom 	int err;
   3043  11022       Tom 
   3044  11022       Tom 	if (prop == ZPROP_INVAL) {
   3045  11022       Tom 		if (zfs_prop_user(propname)) {
   3046  11022       Tom 			if (err = zfs_secpolicy_write_perms(dsname,
   3047  11022       Tom 			    ZFS_DELEG_PERM_USERPROP, cr))
   3048  11022       Tom 				return (err);
   3049  11022       Tom 			return (0);
   3050  11022       Tom 		}
   3051  11022       Tom 
   3052  11022       Tom 		if (!issnap && zfs_prop_userquota(propname)) {
   3053  11022       Tom 			const char *perm = NULL;
   3054  11022       Tom 			const char *uq_prefix =
   3055  11022       Tom 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
   3056  11022       Tom 			const char *gq_prefix =
   3057  11022       Tom 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
   3058  11022       Tom 
   3059  11022       Tom 			if (strncmp(propname, uq_prefix,
   3060  11022       Tom 			    strlen(uq_prefix)) == 0) {
   3061  11022       Tom 				perm = ZFS_DELEG_PERM_USERQUOTA;
   3062  11022       Tom 			} else if (strncmp(propname, gq_prefix,
   3063  11022       Tom 			    strlen(gq_prefix)) == 0) {
   3064  11022       Tom 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
   3065  11022       Tom 			} else {
   3066  11022       Tom 				/* USERUSED and GROUPUSED are read-only */
   3067  11022       Tom 				return (EINVAL);
   3068  11022       Tom 			}
   3069  11022       Tom 
   3070  11022       Tom 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
   3071  11022       Tom 				return (err);
   3072  11022       Tom 			return (0);
   3073  11022       Tom 		}
   3074  11022