1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21ad135b5dSChristopher Siden 22fa9e4066Sahrens /* 233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 241df56adaSMartin Matuska * Portions Copyright 2011 Martin Matuska 25e9103aaeSGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 264e3c9f44SBill Pijewski * Copyright (c) 2012, Joyent, Inc. All rights reserved. 27be6fd75aSMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved. 28a6f561b4SSašo Kiselkov * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 294445fffbSMatthew Ahrens */ 304445fffbSMatthew Ahrens 314445fffbSMatthew Ahrens /* 324445fffbSMatthew Ahrens * ZFS ioctls. 334445fffbSMatthew Ahrens * 344445fffbSMatthew Ahrens * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage 354445fffbSMatthew Ahrens * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool. 364445fffbSMatthew Ahrens * 374445fffbSMatthew Ahrens * There are two ways that we handle ioctls: the legacy way where almost 384445fffbSMatthew Ahrens * all of the logic is in the ioctl callback, and the new way where most 394445fffbSMatthew Ahrens * of the marshalling is handled in the common entry point, zfsdev_ioctl(). 404445fffbSMatthew Ahrens * 414445fffbSMatthew Ahrens * Non-legacy ioctls should be registered by calling 424445fffbSMatthew Ahrens * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked 434445fffbSMatthew Ahrens * from userland by lzc_ioctl(). 444445fffbSMatthew Ahrens * 454445fffbSMatthew Ahrens * The registration arguments are as follows: 464445fffbSMatthew Ahrens * 474445fffbSMatthew Ahrens * const char *name 484445fffbSMatthew Ahrens * The name of the ioctl. This is used for history logging. If the 494445fffbSMatthew Ahrens * ioctl returns successfully (the callback returns 0), and allow_log 504445fffbSMatthew Ahrens * is true, then a history log entry will be recorded with the input & 514445fffbSMatthew Ahrens * output nvlists. The log entry can be printed with "zpool history -i". 524445fffbSMatthew Ahrens * 534445fffbSMatthew Ahrens * zfs_ioc_t ioc 544445fffbSMatthew Ahrens * The ioctl request number, which userland will pass to ioctl(2). 554445fffbSMatthew Ahrens * The ioctl numbers can change from release to release, because 564445fffbSMatthew Ahrens * the caller (libzfs) must be matched to the kernel. 574445fffbSMatthew Ahrens * 584445fffbSMatthew Ahrens * zfs_secpolicy_func_t *secpolicy 594445fffbSMatthew Ahrens * This function will be called before the zfs_ioc_func_t, to 604445fffbSMatthew Ahrens * determine if this operation is permitted. It should return EPERM 614445fffbSMatthew Ahrens * on failure, and 0 on success. Checks include determining if the 624445fffbSMatthew Ahrens * dataset is visible in this zone, and if the user has either all 634445fffbSMatthew Ahrens * zfs privileges in the zone (SYS_MOUNT), or has been granted permission 644445fffbSMatthew Ahrens * to do this operation on this dataset with "zfs allow". 654445fffbSMatthew Ahrens * 664445fffbSMatthew Ahrens * zfs_ioc_namecheck_t namecheck 674445fffbSMatthew Ahrens * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool 684445fffbSMatthew Ahrens * name, a dataset name, or nothing. If the name is not well-formed, 694445fffbSMatthew Ahrens * the ioctl will fail and the callback will not be called. 704445fffbSMatthew Ahrens * Therefore, the callback can assume that the name is well-formed 714445fffbSMatthew Ahrens * (e.g. is null-terminated, doesn't have more than one '@' character, 724445fffbSMatthew Ahrens * doesn't have invalid characters). 734445fffbSMatthew Ahrens * 744445fffbSMatthew Ahrens * zfs_ioc_poolcheck_t pool_check 754445fffbSMatthew Ahrens * This specifies requirements on the pool state. If the pool does 764445fffbSMatthew Ahrens * not meet them (is suspended or is readonly), the ioctl will fail 774445fffbSMatthew Ahrens * and the callback will not be called. If any checks are specified 784445fffbSMatthew Ahrens * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME. 794445fffbSMatthew Ahrens * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED | 804445fffbSMatthew Ahrens * POOL_CHECK_READONLY). 814445fffbSMatthew Ahrens * 824445fffbSMatthew Ahrens * boolean_t smush_outnvlist 834445fffbSMatthew Ahrens * If smush_outnvlist is true, then the output is presumed to be a 844445fffbSMatthew Ahrens * list of errors, and it will be "smushed" down to fit into the 854445fffbSMatthew Ahrens * caller's buffer, by removing some entries and replacing them with a 864445fffbSMatthew Ahrens * single "N_MORE_ERRORS" entry indicating how many were removed. See 874445fffbSMatthew Ahrens * nvlist_smush() for details. If smush_outnvlist is false, and the 884445fffbSMatthew Ahrens * outnvlist does not fit into the userland-provided buffer, then the 894445fffbSMatthew Ahrens * ioctl will fail with ENOMEM. 904445fffbSMatthew Ahrens * 914445fffbSMatthew Ahrens * zfs_ioc_func_t *func 924445fffbSMatthew Ahrens * The callback function that will perform the operation. 934445fffbSMatthew Ahrens * 944445fffbSMatthew Ahrens * The callback should return 0 on success, or an error number on 954445fffbSMatthew Ahrens * failure. If the function fails, the userland ioctl will return -1, 964445fffbSMatthew Ahrens * and errno will be set to the callback's return value. The callback 974445fffbSMatthew Ahrens * will be called with the following arguments: 984445fffbSMatthew Ahrens * 994445fffbSMatthew Ahrens * const char *name 1004445fffbSMatthew Ahrens * The name of the pool or dataset to operate on, from 1014445fffbSMatthew Ahrens * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the 1024445fffbSMatthew Ahrens * expected type (pool, dataset, or none). 1034445fffbSMatthew Ahrens * 1044445fffbSMatthew Ahrens * nvlist_t *innvl 1054445fffbSMatthew Ahrens * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or 1064445fffbSMatthew Ahrens * NULL if no input nvlist was provided. Changes to this nvlist are 1074445fffbSMatthew Ahrens * ignored. If the input nvlist could not be deserialized, the 1084445fffbSMatthew Ahrens * ioctl will fail and the callback will not be called. 1094445fffbSMatthew Ahrens * 1104445fffbSMatthew Ahrens * nvlist_t *outnvl 1114445fffbSMatthew Ahrens * The output nvlist, initially empty. The callback can fill it in, 1124445fffbSMatthew Ahrens * and it will be returned to userland by serializing it into 1134445fffbSMatthew Ahrens * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization 1144445fffbSMatthew Ahrens * fails (e.g. because the caller didn't supply a large enough 1154445fffbSMatthew Ahrens * buffer), then the overall ioctl will fail. See the 1164445fffbSMatthew Ahrens * 'smush_nvlist' argument above for additional behaviors. 1174445fffbSMatthew Ahrens * 1184445fffbSMatthew Ahrens * There are two typical uses of the output nvlist: 1194445fffbSMatthew Ahrens * - To return state, e.g. property values. In this case, 1204445fffbSMatthew Ahrens * smush_outnvlist should be false. If the buffer was not large 1214445fffbSMatthew Ahrens * enough, the caller will reallocate a larger buffer and try 1224445fffbSMatthew Ahrens * the ioctl again. 1234445fffbSMatthew Ahrens * 1244445fffbSMatthew Ahrens * - To return multiple errors from an ioctl which makes on-disk 1254445fffbSMatthew Ahrens * changes. In this case, smush_outnvlist should be true. 1264445fffbSMatthew Ahrens * Ioctls which make on-disk modifications should generally not 1274445fffbSMatthew Ahrens * use the outnvl if they succeed, because the caller can not 1284445fffbSMatthew Ahrens * distinguish between the operation failing, and 1294445fffbSMatthew Ahrens * deserialization failing. 130e9103aaeSGarrett D'Amore */ 131fa9e4066Sahrens 132fa9e4066Sahrens #include <sys/types.h> 133fa9e4066Sahrens #include <sys/param.h> 134fa9e4066Sahrens #include <sys/errno.h> 135fa9e4066Sahrens #include <sys/uio.h> 136fa9e4066Sahrens #include <sys/buf.h> 137fa9e4066Sahrens #include <sys/modctl.h> 138fa9e4066Sahrens #include <sys/open.h> 139fa9e4066Sahrens #include <sys/file.h> 140fa9e4066Sahrens #include <sys/kmem.h> 141fa9e4066Sahrens #include <sys/conf.h> 142fa9e4066Sahrens #include <sys/cmn_err.h> 143fa9e4066Sahrens #include <sys/stat.h> 144fa9e4066Sahrens #include <sys/zfs_ioctl.h> 1454201a95eSRic Aleshire #include <sys/zfs_vfsops.h> 146da6c28aaSamw #include <sys/zfs_znode.h> 147fa9e4066Sahrens #include <sys/zap.h> 148fa9e4066Sahrens #include <sys/spa.h> 149b1b8ab34Slling #include <sys/spa_impl.h> 150fa9e4066Sahrens #include <sys/vdev.h> 1514201a95eSRic Aleshire #include <sys/priv_impl.h> 152fa9e4066Sahrens #include <sys/dmu.h> 153fa9e4066Sahrens #include <sys/dsl_dir.h> 154fa9e4066Sahrens #include <sys/dsl_dataset.h> 155fa9e4066Sahrens #include <sys/dsl_prop.h> 156ecd6cf80Smarks #include <sys/dsl_deleg.h> 157ecd6cf80Smarks #include <sys/dmu_objset.h> 1584e3c9f44SBill Pijewski #include <sys/dmu_impl.h> 1593b2aab18SMatthew Ahrens #include <sys/dmu_tx.h> 160fa9e4066Sahrens #include <sys/ddi.h> 161fa9e4066Sahrens #include <sys/sunddi.h> 162fa9e4066Sahrens #include <sys/sunldi.h> 163fa9e4066Sahrens #include <sys/policy.h> 164fa9e4066Sahrens #include <sys/zone.h> 165fa9e4066Sahrens #include <sys/nvpair.h> 166fa9e4066Sahrens #include <sys/pathname.h> 167fa9e4066Sahrens #include <sys/mount.h> 168fa9e4066Sahrens #include <sys/sdt.h> 169fa9e4066Sahrens #include <sys/fs/zfs.h> 170fa9e4066Sahrens #include <sys/zfs_ctldir.h> 171da6c28aaSamw #include <sys/zfs_dir.h> 172c99e4bdcSChris Kirby #include <sys/zfs_onexit.h> 173a2eea2e1Sahrens #include <sys/zvol.h> 1743f9d6ad7SLin Ling #include <sys/dsl_scan.h> 175ecd6cf80Smarks #include <sharefs/share.h> 176f18faf3fSek #include <sys/dmu_objset.h> 1773b2aab18SMatthew Ahrens #include <sys/dmu_send.h> 1783b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 1793b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h> 180a6f561b4SSašo Kiselkov #include <sys/zfeature.h> 181fa9e4066Sahrens 182fa9e4066Sahrens #include "zfs_namecheck.h" 183e9dbad6fSeschrock #include "zfs_prop.h" 184ecd6cf80Smarks #include "zfs_deleg.h" 1850a586ceaSMark Shellenbaum #include "zfs_comutil.h" 186fa9e4066Sahrens 187fa9e4066Sahrens extern struct modlfs zfs_modlfs; 188fa9e4066Sahrens 189fa9e4066Sahrens extern void zfs_init(void); 190fa9e4066Sahrens extern void zfs_fini(void); 191fa9e4066Sahrens 192fa9e4066Sahrens ldi_ident_t zfs_li = NULL; 193fa9e4066Sahrens dev_info_t *zfs_dip; 194fa9e4066Sahrens 1954445fffbSMatthew Ahrens uint_t zfs_fsyncer_key; 1964445fffbSMatthew Ahrens extern uint_t rrw_tsd_key; 1974445fffbSMatthew Ahrens static uint_t zfs_allow_log_key; 1984445fffbSMatthew Ahrens 1994445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *); 2004445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *); 2014445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *); 202fa9e4066Sahrens 20354d692b7SGeorge Wilson typedef enum { 20454d692b7SGeorge Wilson NO_NAME, 20554d692b7SGeorge Wilson POOL_NAME, 20654d692b7SGeorge Wilson DATASET_NAME 20754d692b7SGeorge Wilson } zfs_ioc_namecheck_t; 20854d692b7SGeorge Wilson 209f9af39baSGeorge Wilson typedef enum { 210f9af39baSGeorge Wilson POOL_CHECK_NONE = 1 << 0, 211f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED = 1 << 1, 2124445fffbSMatthew Ahrens POOL_CHECK_READONLY = 1 << 2, 213f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t; 214f9af39baSGeorge Wilson 215fa9e4066Sahrens typedef struct zfs_ioc_vec { 2164445fffbSMatthew Ahrens zfs_ioc_legacy_func_t *zvec_legacy_func; 217fa9e4066Sahrens zfs_ioc_func_t *zvec_func; 218fa9e4066Sahrens zfs_secpolicy_func_t *zvec_secpolicy; 21954d692b7SGeorge Wilson zfs_ioc_namecheck_t zvec_namecheck; 2204445fffbSMatthew Ahrens boolean_t zvec_allow_log; 221f9af39baSGeorge Wilson zfs_ioc_poolcheck_t zvec_pool_check; 2224445fffbSMatthew Ahrens boolean_t zvec_smush_outnvlist; 2234445fffbSMatthew Ahrens const char *zvec_name; 224fa9e4066Sahrens } zfs_ioc_vec_t; 225fa9e4066Sahrens 22614843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */ 22714843421SMatthew Ahrens static const char *userquota_perms[] = { 22814843421SMatthew Ahrens ZFS_DELEG_PERM_USERUSED, 22914843421SMatthew Ahrens ZFS_DELEG_PERM_USERQUOTA, 23014843421SMatthew Ahrens ZFS_DELEG_PERM_GROUPUSED, 23114843421SMatthew Ahrens ZFS_DELEG_PERM_GROUPQUOTA, 23214843421SMatthew Ahrens }; 23314843421SMatthew Ahrens 23414843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 23592241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property, 23692241e0bSTom Erickson cred_t *cr); 23792241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props, 23892241e0bSTom Erickson nvlist_t **errors); 2390a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 2400a48a24eStimh boolean_t *); 2414445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *); 2424445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp); 2430a48a24eStimh 2443b2aab18SMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature); 245a6f561b4SSašo Kiselkov 246fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 247fa9e4066Sahrens void 248fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 249fa9e4066Sahrens { 250fa9e4066Sahrens const char *newfile; 2513f9d6ad7SLin Ling char buf[512]; 252fa9e4066Sahrens va_list adx; 253fa9e4066Sahrens 254fa9e4066Sahrens /* 255fa9e4066Sahrens * Get rid of annoying "../common/" prefix to filename. 256fa9e4066Sahrens */ 257fa9e4066Sahrens newfile = strrchr(file, '/'); 258fa9e4066Sahrens if (newfile != NULL) { 259fa9e4066Sahrens newfile = newfile + 1; /* Get rid of leading / */ 260fa9e4066Sahrens } else { 261fa9e4066Sahrens newfile = file; 262fa9e4066Sahrens } 263fa9e4066Sahrens 264fa9e4066Sahrens va_start(adx, fmt); 265fa9e4066Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, adx); 266fa9e4066Sahrens va_end(adx); 267fa9e4066Sahrens 268fa9e4066Sahrens /* 269fa9e4066Sahrens * To get this data, use the zfs-dprintf probe as so: 270fa9e4066Sahrens * dtrace -q -n 'zfs-dprintf \ 271fa9e4066Sahrens * /stringof(arg0) == "dbuf.c"/ \ 272fa9e4066Sahrens * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 273fa9e4066Sahrens * arg0 = file name 274fa9e4066Sahrens * arg1 = function name 275fa9e4066Sahrens * arg2 = line number 276fa9e4066Sahrens * arg3 = message 277fa9e4066Sahrens */ 278fa9e4066Sahrens DTRACE_PROBE4(zfs__dprintf, 279fa9e4066Sahrens char *, newfile, char *, func, int, line, char *, buf); 280fa9e4066Sahrens } 281fa9e4066Sahrens 282ecd6cf80Smarks static void 283228975ccSek history_str_free(char *buf) 284228975ccSek { 285228975ccSek kmem_free(buf, HIS_MAX_RECORD_LEN); 286228975ccSek } 287228975ccSek 288228975ccSek static char * 289228975ccSek history_str_get(zfs_cmd_t *zc) 290ecd6cf80Smarks { 29140feaa91Sahrens char *buf; 292ecd6cf80Smarks 293ecd6cf80Smarks if (zc->zc_history == NULL) 294228975ccSek return (NULL); 295e7437265Sahrens 296ecd6cf80Smarks buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 297ecd6cf80Smarks if (copyinstr((void *)(uintptr_t)zc->zc_history, 298ecd6cf80Smarks buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 299228975ccSek history_str_free(buf); 300228975ccSek return (NULL); 301ecd6cf80Smarks } 302ecd6cf80Smarks 303ecd6cf80Smarks buf[HIS_MAX_RECORD_LEN -1] = '\0'; 304ecd6cf80Smarks 305228975ccSek return (buf); 306228975ccSek } 307ecd6cf80Smarks 30815e6edf1Sgw /* 30915e6edf1Sgw * Check to see if the named dataset is currently defined as bootable 31015e6edf1Sgw */ 31115e6edf1Sgw static boolean_t 31215e6edf1Sgw zfs_is_bootfs(const char *name) 31315e6edf1Sgw { 314503ad85cSMatthew Ahrens objset_t *os; 31515e6edf1Sgw 316503ad85cSMatthew Ahrens if (dmu_objset_hold(name, FTAG, &os) == 0) { 317503ad85cSMatthew Ahrens boolean_t ret; 318b24ab676SJeff Bonwick ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os))); 319503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 320503ad85cSMatthew Ahrens return (ret); 32115e6edf1Sgw } 322503ad85cSMatthew Ahrens return (B_FALSE); 32315e6edf1Sgw } 32415e6edf1Sgw 325c2a93d44Stimh /* 326*f7170741SWill Andrews * Return non-zero if the spa version is less than requested version. 327c2a93d44Stimh */ 328da6c28aaSamw static int 3290a48a24eStimh zfs_earlier_version(const char *name, int version) 330da6c28aaSamw { 331da6c28aaSamw spa_t *spa; 332da6c28aaSamw 333da6c28aaSamw if (spa_open(name, &spa, FTAG) == 0) { 334da6c28aaSamw if (spa_version(spa) < version) { 335da6c28aaSamw spa_close(spa, FTAG); 336da6c28aaSamw return (1); 337da6c28aaSamw } 338da6c28aaSamw spa_close(spa, FTAG); 339da6c28aaSamw } 340da6c28aaSamw return (0); 341da6c28aaSamw } 342da6c28aaSamw 3439e6eda55Smarks /* 344745cd3c5Smaybee * Return TRUE if the ZPL version is less than requested version. 3459e6eda55Smarks */ 346745cd3c5Smaybee static boolean_t 347745cd3c5Smaybee zpl_earlier_version(const char *name, int version) 3489e6eda55Smarks { 3499e6eda55Smarks objset_t *os; 350745cd3c5Smaybee boolean_t rc = B_TRUE; 3519e6eda55Smarks 352503ad85cSMatthew Ahrens if (dmu_objset_hold(name, FTAG, &os) == 0) { 353745cd3c5Smaybee uint64_t zplversion; 3549e6eda55Smarks 355503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 356503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 357503ad85cSMatthew Ahrens return (B_TRUE); 358503ad85cSMatthew Ahrens } 359503ad85cSMatthew Ahrens /* XXX reading from non-owned objset */ 360745cd3c5Smaybee if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 361745cd3c5Smaybee rc = zplversion < version; 362503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 3639e6eda55Smarks } 3649e6eda55Smarks return (rc); 3659e6eda55Smarks } 3669e6eda55Smarks 367228975ccSek static void 368228975ccSek zfs_log_history(zfs_cmd_t *zc) 369228975ccSek { 370228975ccSek spa_t *spa; 371228975ccSek char *buf; 372ecd6cf80Smarks 373228975ccSek if ((buf = history_str_get(zc)) == NULL) 374228975ccSek return; 375228975ccSek 376228975ccSek if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 377228975ccSek if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 3784445fffbSMatthew Ahrens (void) spa_history_log(spa, buf); 379228975ccSek spa_close(spa, FTAG); 380228975ccSek } 381228975ccSek history_str_free(buf); 382ecd6cf80Smarks } 383ecd6cf80Smarks 384fa9e4066Sahrens /* 385fa9e4066Sahrens * Policy for top-level read operations (list pools). Requires no privileges, 386fa9e4066Sahrens * and can be used in the local zone, as there is no associated dataset. 387fa9e4066Sahrens */ 388fa9e4066Sahrens /* ARGSUSED */ 389fa9e4066Sahrens static int 3904445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 391fa9e4066Sahrens { 392fa9e4066Sahrens return (0); 393fa9e4066Sahrens } 394fa9e4066Sahrens 395fa9e4066Sahrens /* 396fa9e4066Sahrens * Policy for dataset read operations (list children, get statistics). Requires 397fa9e4066Sahrens * no privileges, but must be visible in the local zone. 398fa9e4066Sahrens */ 399fa9e4066Sahrens /* ARGSUSED */ 400fa9e4066Sahrens static int 4014445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 402fa9e4066Sahrens { 403fa9e4066Sahrens if (INGLOBALZONE(curproc) || 404ecd6cf80Smarks zone_dataset_visible(zc->zc_name, NULL)) 405fa9e4066Sahrens return (0); 406fa9e4066Sahrens 407be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 408fa9e4066Sahrens } 409fa9e4066Sahrens 410fa9e4066Sahrens static int 411a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr) 412fa9e4066Sahrens { 413fa9e4066Sahrens int writable = 1; 414fa9e4066Sahrens 415fa9e4066Sahrens /* 416fa9e4066Sahrens * The dataset must be visible by this zone -- check this first 417fa9e4066Sahrens * so they don't see EPERM on something they shouldn't know about. 418fa9e4066Sahrens */ 419fa9e4066Sahrens if (!INGLOBALZONE(curproc) && 420fa9e4066Sahrens !zone_dataset_visible(dataset, &writable)) 421be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 422fa9e4066Sahrens 423fa9e4066Sahrens if (INGLOBALZONE(curproc)) { 424fa9e4066Sahrens /* 425fa9e4066Sahrens * If the fs is zoned, only root can access it from the 426fa9e4066Sahrens * global zone. 427fa9e4066Sahrens */ 428fa9e4066Sahrens if (secpolicy_zfs(cr) && zoned) 429be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 430fa9e4066Sahrens } else { 431fa9e4066Sahrens /* 432fa9e4066Sahrens * If we are in a local zone, the 'zoned' property must be set. 433fa9e4066Sahrens */ 434fa9e4066Sahrens if (!zoned) 435be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 436fa9e4066Sahrens 437fa9e4066Sahrens /* must be writable by this zone */ 438fa9e4066Sahrens if (!writable) 439be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 440fa9e4066Sahrens } 441fa9e4066Sahrens return (0); 442fa9e4066Sahrens } 443fa9e4066Sahrens 444a7f53a56SChris Kirby static int 445a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr) 446a7f53a56SChris Kirby { 447a7f53a56SChris Kirby uint64_t zoned; 448a7f53a56SChris Kirby 449a7f53a56SChris Kirby if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 450be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 451a7f53a56SChris Kirby 452a7f53a56SChris Kirby return (zfs_dozonecheck_impl(dataset, zoned, cr)); 453a7f53a56SChris Kirby } 454a7f53a56SChris Kirby 455a7f53a56SChris Kirby static int 456a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr) 457a7f53a56SChris Kirby { 458a7f53a56SChris Kirby uint64_t zoned; 459a7f53a56SChris Kirby 4603b2aab18SMatthew Ahrens if (dsl_prop_get_int_ds(ds, "zoned", &zoned)) 461be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 462a7f53a56SChris Kirby 463a7f53a56SChris Kirby return (zfs_dozonecheck_impl(dataset, zoned, cr)); 464a7f53a56SChris Kirby } 465a7f53a56SChris Kirby 4664445fffbSMatthew Ahrens static int 4673b2aab18SMatthew Ahrens zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds, 4683b2aab18SMatthew Ahrens const char *perm, cred_t *cr) 469fa9e4066Sahrens { 470fa9e4066Sahrens int error; 471fa9e4066Sahrens 47219b94df9SMatthew Ahrens error = zfs_dozonecheck_ds(name, ds, cr); 473ecd6cf80Smarks if (error == 0) { 474ecd6cf80Smarks error = secpolicy_zfs(cr); 4753b2aab18SMatthew Ahrens if (error != 0) 4764445fffbSMatthew Ahrens error = dsl_deleg_access_impl(ds, perm, cr); 477ecd6cf80Smarks } 478ecd6cf80Smarks return (error); 479ecd6cf80Smarks } 480ecd6cf80Smarks 4814445fffbSMatthew Ahrens static int 4823b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 483a7f53a56SChris Kirby { 484a7f53a56SChris Kirby int error; 4853b2aab18SMatthew Ahrens dsl_dataset_t *ds; 4863b2aab18SMatthew Ahrens dsl_pool_t *dp; 487a7f53a56SChris Kirby 4883b2aab18SMatthew Ahrens error = dsl_pool_hold(name, FTAG, &dp); 4893b2aab18SMatthew Ahrens if (error != 0) 4903b2aab18SMatthew Ahrens return (error); 4913b2aab18SMatthew Ahrens 4923b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, name, FTAG, &ds); 4933b2aab18SMatthew Ahrens if (error != 0) { 4943b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 4953b2aab18SMatthew Ahrens return (error); 496a7f53a56SChris Kirby } 4973b2aab18SMatthew Ahrens 4983b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr); 4993b2aab18SMatthew Ahrens 5003b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 5013b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 502a7f53a56SChris Kirby return (error); 503a7f53a56SChris Kirby } 504a7f53a56SChris Kirby 5054201a95eSRic Aleshire /* 5064201a95eSRic Aleshire * Policy for setting the security label property. 5074201a95eSRic Aleshire * 5084201a95eSRic Aleshire * Returns 0 for success, non-zero for access and other errors. 5094201a95eSRic Aleshire */ 5104201a95eSRic Aleshire static int 51192241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr) 5124201a95eSRic Aleshire { 5134201a95eSRic Aleshire char ds_hexsl[MAXNAMELEN]; 5144201a95eSRic Aleshire bslabel_t ds_sl, new_sl; 5154201a95eSRic Aleshire boolean_t new_default = FALSE; 5164201a95eSRic Aleshire uint64_t zoned; 5174201a95eSRic Aleshire int needed_priv = -1; 5184201a95eSRic Aleshire int error; 5194201a95eSRic Aleshire 5204201a95eSRic Aleshire /* First get the existing dataset label. */ 5214201a95eSRic Aleshire error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 5224201a95eSRic Aleshire 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 5233b2aab18SMatthew Ahrens if (error != 0) 524be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 5254201a95eSRic Aleshire 5264201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 5274201a95eSRic Aleshire new_default = TRUE; 5284201a95eSRic Aleshire 5294201a95eSRic Aleshire /* The label must be translatable */ 5304201a95eSRic Aleshire if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) 531be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 5324201a95eSRic Aleshire 5334201a95eSRic Aleshire /* 5344201a95eSRic Aleshire * In a non-global zone, disallow attempts to set a label that 5354201a95eSRic Aleshire * doesn't match that of the zone; otherwise no other checks 5364201a95eSRic Aleshire * are needed. 5374201a95eSRic Aleshire */ 5384201a95eSRic Aleshire if (!INGLOBALZONE(curproc)) { 5394201a95eSRic Aleshire if (new_default || !blequal(&new_sl, CR_SL(CRED()))) 540be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 5414201a95eSRic Aleshire return (0); 5424201a95eSRic Aleshire } 5434201a95eSRic Aleshire 5444201a95eSRic Aleshire /* 5454201a95eSRic Aleshire * For global-zone datasets (i.e., those whose zoned property is 5464201a95eSRic Aleshire * "off", verify that the specified new label is valid for the 5474201a95eSRic Aleshire * global zone. 5484201a95eSRic Aleshire */ 5494201a95eSRic Aleshire if (dsl_prop_get_integer(name, 5504201a95eSRic Aleshire zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 551be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 5524201a95eSRic Aleshire if (!zoned) { 5534201a95eSRic Aleshire if (zfs_check_global_label(name, strval) != 0) 554be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 5554201a95eSRic Aleshire } 5564201a95eSRic Aleshire 5574201a95eSRic Aleshire /* 5584201a95eSRic Aleshire * If the existing dataset label is nondefault, check if the 5594201a95eSRic Aleshire * dataset is mounted (label cannot be changed while mounted). 5604201a95eSRic Aleshire * Get the zfsvfs; if there isn't one, then the dataset isn't 5614201a95eSRic Aleshire * mounted (or isn't a dataset, doesn't exist, ...). 5624201a95eSRic Aleshire */ 5634201a95eSRic Aleshire if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { 56492241e0bSTom Erickson objset_t *os; 56592241e0bSTom Erickson static char *setsl_tag = "setsl_tag"; 56692241e0bSTom Erickson 5674201a95eSRic Aleshire /* 5684201a95eSRic Aleshire * Try to own the dataset; abort if there is any error, 5694201a95eSRic Aleshire * (e.g., already mounted, in use, or other error). 5704201a95eSRic Aleshire */ 5714201a95eSRic Aleshire error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, 57292241e0bSTom Erickson setsl_tag, &os); 5733b2aab18SMatthew Ahrens if (error != 0) 574be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 5754201a95eSRic Aleshire 57692241e0bSTom Erickson dmu_objset_disown(os, setsl_tag); 57792241e0bSTom Erickson 5784201a95eSRic Aleshire if (new_default) { 5794201a95eSRic Aleshire needed_priv = PRIV_FILE_DOWNGRADE_SL; 5804201a95eSRic Aleshire goto out_check; 5814201a95eSRic Aleshire } 5824201a95eSRic Aleshire 5834201a95eSRic Aleshire if (hexstr_to_label(strval, &new_sl) != 0) 584be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 5854201a95eSRic Aleshire 5864201a95eSRic Aleshire if (blstrictdom(&ds_sl, &new_sl)) 5874201a95eSRic Aleshire needed_priv = PRIV_FILE_DOWNGRADE_SL; 5884201a95eSRic Aleshire else if (blstrictdom(&new_sl, &ds_sl)) 5894201a95eSRic Aleshire needed_priv = PRIV_FILE_UPGRADE_SL; 5904201a95eSRic Aleshire } else { 5914201a95eSRic Aleshire /* dataset currently has a default label */ 5924201a95eSRic Aleshire if (!new_default) 5934201a95eSRic Aleshire needed_priv = PRIV_FILE_UPGRADE_SL; 5944201a95eSRic Aleshire } 5954201a95eSRic Aleshire 5964201a95eSRic Aleshire out_check: 5974201a95eSRic Aleshire if (needed_priv != -1) 5984201a95eSRic Aleshire return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); 5994201a95eSRic Aleshire return (0); 6004201a95eSRic Aleshire } 6014201a95eSRic Aleshire 602ecd6cf80Smarks static int 60392241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, 60492241e0bSTom Erickson cred_t *cr) 605ecd6cf80Smarks { 60692241e0bSTom Erickson char *strval; 60792241e0bSTom Erickson 608ecd6cf80Smarks /* 609ecd6cf80Smarks * Check permissions for special properties. 610ecd6cf80Smarks */ 611ecd6cf80Smarks switch (prop) { 612ecd6cf80Smarks case ZFS_PROP_ZONED: 613ecd6cf80Smarks /* 614ecd6cf80Smarks * Disallow setting of 'zoned' from within a local zone. 615ecd6cf80Smarks */ 616ecd6cf80Smarks if (!INGLOBALZONE(curproc)) 617be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 618ecd6cf80Smarks break; 619ecd6cf80Smarks 620ecd6cf80Smarks case ZFS_PROP_QUOTA: 621ecd6cf80Smarks if (!INGLOBALZONE(curproc)) { 622ecd6cf80Smarks uint64_t zoned; 623ecd6cf80Smarks char setpoint[MAXNAMELEN]; 624ecd6cf80Smarks /* 625ecd6cf80Smarks * Unprivileged users are allowed to modify the 626ecd6cf80Smarks * quota on things *under* (ie. contained by) 627ecd6cf80Smarks * the thing they own. 628ecd6cf80Smarks */ 62992241e0bSTom Erickson if (dsl_prop_get_integer(dsname, "zoned", &zoned, 630ecd6cf80Smarks setpoint)) 631be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 63292241e0bSTom Erickson if (!zoned || strlen(dsname) <= strlen(setpoint)) 633be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 634ecd6cf80Smarks } 635db870a07Sahrens break; 6364201a95eSRic Aleshire 6374201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 6384201a95eSRic Aleshire if (!is_system_labeled()) 639be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 64092241e0bSTom Erickson 64192241e0bSTom Erickson if (nvpair_value_string(propval, &strval) == 0) { 64292241e0bSTom Erickson int err; 64392241e0bSTom Erickson 64492241e0bSTom Erickson err = zfs_set_slabel_policy(dsname, strval, CRED()); 64592241e0bSTom Erickson if (err != 0) 64692241e0bSTom Erickson return (err); 64792241e0bSTom Erickson } 6484201a95eSRic Aleshire break; 649ecd6cf80Smarks } 650ecd6cf80Smarks 65192241e0bSTom Erickson return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); 652ecd6cf80Smarks } 653ecd6cf80Smarks 6544445fffbSMatthew Ahrens /* ARGSUSED */ 6554445fffbSMatthew Ahrens static int 6564445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 657ecd6cf80Smarks { 658ecd6cf80Smarks int error; 659ecd6cf80Smarks 660ecd6cf80Smarks error = zfs_dozonecheck(zc->zc_name, cr); 6613b2aab18SMatthew Ahrens if (error != 0) 662fa9e4066Sahrens return (error); 663fa9e4066Sahrens 664ecd6cf80Smarks /* 665ecd6cf80Smarks * permission to set permissions will be evaluated later in 666ecd6cf80Smarks * dsl_deleg_can_allow() 667ecd6cf80Smarks */ 668ecd6cf80Smarks return (0); 669ecd6cf80Smarks } 670ecd6cf80Smarks 6714445fffbSMatthew Ahrens /* ARGSUSED */ 6724445fffbSMatthew Ahrens static int 6734445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 674ecd6cf80Smarks { 675681d9761SEric Taylor return (zfs_secpolicy_write_perms(zc->zc_name, 676681d9761SEric Taylor ZFS_DELEG_PERM_ROLLBACK, cr)); 677ecd6cf80Smarks } 678ecd6cf80Smarks 6794445fffbSMatthew Ahrens /* ARGSUSED */ 6804445fffbSMatthew Ahrens static int 6814445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 682ecd6cf80Smarks { 683a7f53a56SChris Kirby dsl_pool_t *dp; 684a7f53a56SChris Kirby dsl_dataset_t *ds; 685a7f53a56SChris Kirby char *cp; 686a7f53a56SChris Kirby int error; 687a7f53a56SChris Kirby 688a7f53a56SChris Kirby /* 689a7f53a56SChris Kirby * Generate the current snapshot name from the given objsetid, then 690a7f53a56SChris Kirby * use that name for the secpolicy/zone checks. 691a7f53a56SChris Kirby */ 692a7f53a56SChris Kirby cp = strchr(zc->zc_name, '@'); 693a7f53a56SChris Kirby if (cp == NULL) 694be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 6953b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 6963b2aab18SMatthew Ahrens if (error != 0) 697a7f53a56SChris Kirby return (error); 698a7f53a56SChris Kirby 699a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 7003b2aab18SMatthew Ahrens if (error != 0) { 7013b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 702a7f53a56SChris Kirby return (error); 7033b2aab18SMatthew Ahrens } 704a7f53a56SChris Kirby 705a7f53a56SChris Kirby dsl_dataset_name(ds, zc->zc_name); 706a7f53a56SChris Kirby 707a7f53a56SChris Kirby error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds, 708a7f53a56SChris Kirby ZFS_DELEG_PERM_SEND, cr); 709a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 7103b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 711a7f53a56SChris Kirby 712a7f53a56SChris Kirby return (error); 713ecd6cf80Smarks } 714ecd6cf80Smarks 7154445fffbSMatthew Ahrens /* ARGSUSED */ 716743a77edSAlan Wright static int 7174445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 7184445fffbSMatthew Ahrens { 7194445fffbSMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name, 7204445fffbSMatthew Ahrens ZFS_DELEG_PERM_SEND, cr)); 7214445fffbSMatthew Ahrens } 7224445fffbSMatthew Ahrens 7234445fffbSMatthew Ahrens /* ARGSUSED */ 7244445fffbSMatthew Ahrens static int 7254445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 726743a77edSAlan Wright { 727743a77edSAlan Wright vnode_t *vp; 728743a77edSAlan Wright int error; 729743a77edSAlan Wright 730743a77edSAlan Wright if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 731743a77edSAlan Wright NO_FOLLOW, NULL, &vp)) != 0) 732743a77edSAlan Wright return (error); 733743a77edSAlan Wright 734743a77edSAlan Wright /* Now make sure mntpnt and dataset are ZFS */ 735743a77edSAlan Wright 736743a77edSAlan Wright if (vp->v_vfsp->vfs_fstype != zfsfstype || 737743a77edSAlan Wright (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 738743a77edSAlan Wright zc->zc_name) != 0)) { 739743a77edSAlan Wright VN_RELE(vp); 740be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 741743a77edSAlan Wright } 742743a77edSAlan Wright 743743a77edSAlan Wright VN_RELE(vp); 744743a77edSAlan Wright return (dsl_deleg_access(zc->zc_name, 745743a77edSAlan Wright ZFS_DELEG_PERM_SHARE, cr)); 746743a77edSAlan Wright } 747743a77edSAlan Wright 748ecd6cf80Smarks int 7494445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 750ecd6cf80Smarks { 751ecd6cf80Smarks if (!INGLOBALZONE(curproc)) 752be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 753ecd6cf80Smarks 7543cb34c60Sahrens if (secpolicy_nfs(cr) == 0) { 755ecd6cf80Smarks return (0); 756ecd6cf80Smarks } else { 7574445fffbSMatthew Ahrens return (zfs_secpolicy_deleg_share(zc, innvl, cr)); 758743a77edSAlan Wright } 759743a77edSAlan Wright } 760ecd6cf80Smarks 761743a77edSAlan Wright int 7624445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 763743a77edSAlan Wright { 764743a77edSAlan Wright if (!INGLOBALZONE(curproc)) 765be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 766ecd6cf80Smarks 767743a77edSAlan Wright if (secpolicy_smb(cr) == 0) { 768743a77edSAlan Wright return (0); 769743a77edSAlan Wright } else { 7704445fffbSMatthew Ahrens return (zfs_secpolicy_deleg_share(zc, innvl, cr)); 771ecd6cf80Smarks } 772fa9e4066Sahrens } 773fa9e4066Sahrens 774fa9e4066Sahrens static int 775ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize) 776fa9e4066Sahrens { 777fa9e4066Sahrens char *cp; 778fa9e4066Sahrens 779fa9e4066Sahrens /* 780fa9e4066Sahrens * Remove the @bla or /bla from the end of the name to get the parent. 781fa9e4066Sahrens */ 782ecd6cf80Smarks (void) strncpy(parent, datasetname, parentsize); 783ecd6cf80Smarks cp = strrchr(parent, '@'); 784fa9e4066Sahrens if (cp != NULL) { 785fa9e4066Sahrens cp[0] = '\0'; 786fa9e4066Sahrens } else { 787ecd6cf80Smarks cp = strrchr(parent, '/'); 788fa9e4066Sahrens if (cp == NULL) 789be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 790fa9e4066Sahrens cp[0] = '\0'; 791ecd6cf80Smarks } 792ecd6cf80Smarks 793ecd6cf80Smarks return (0); 794ecd6cf80Smarks } 795ecd6cf80Smarks 796ecd6cf80Smarks int 797ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 798ecd6cf80Smarks { 799ecd6cf80Smarks int error; 800ecd6cf80Smarks 801ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(name, 802ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 803ecd6cf80Smarks return (error); 804ecd6cf80Smarks 805ecd6cf80Smarks return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 806ecd6cf80Smarks } 807ecd6cf80Smarks 8084445fffbSMatthew Ahrens /* ARGSUSED */ 809ecd6cf80Smarks static int 8104445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 811ecd6cf80Smarks { 812ecd6cf80Smarks return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 813ecd6cf80Smarks } 814ecd6cf80Smarks 815cbf6f6aaSWilliam Gorrell /* 816cbf6f6aaSWilliam Gorrell * Destroying snapshots with delegated permissions requires 8174445fffbSMatthew Ahrens * descendant mount and destroy permissions. 818cbf6f6aaSWilliam Gorrell */ 8194445fffbSMatthew Ahrens /* ARGSUSED */ 820cbf6f6aaSWilliam Gorrell static int 8214445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 822cbf6f6aaSWilliam Gorrell { 8234445fffbSMatthew Ahrens nvlist_t *snaps; 8244445fffbSMatthew Ahrens nvpair_t *pair, *nextpair; 8254445fffbSMatthew Ahrens int error = 0; 826cbf6f6aaSWilliam Gorrell 8274445fffbSMatthew Ahrens if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 828be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 8294445fffbSMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 8304445fffbSMatthew Ahrens pair = nextpair) { 8313b2aab18SMatthew Ahrens dsl_pool_t *dp; 8324445fffbSMatthew Ahrens dsl_dataset_t *ds; 833cbf6f6aaSWilliam Gorrell 8343b2aab18SMatthew Ahrens error = dsl_pool_hold(nvpair_name(pair), FTAG, &dp); 8353b2aab18SMatthew Ahrens if (error != 0) 8363b2aab18SMatthew Ahrens break; 8374445fffbSMatthew Ahrens nextpair = nvlist_next_nvpair(snaps, pair); 8383b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds); 8393b2aab18SMatthew Ahrens if (error == 0) 8404445fffbSMatthew Ahrens dsl_dataset_rele(ds, FTAG); 8413b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 8423b2aab18SMatthew Ahrens 8433b2aab18SMatthew Ahrens if (error == 0) { 8443b2aab18SMatthew Ahrens error = zfs_secpolicy_destroy_perms(nvpair_name(pair), 8453b2aab18SMatthew Ahrens cr); 8464445fffbSMatthew Ahrens } else if (error == ENOENT) { 8474445fffbSMatthew Ahrens /* 8484445fffbSMatthew Ahrens * Ignore any snapshots that don't exist (we consider 8494445fffbSMatthew Ahrens * them "already destroyed"). Remove the name from the 8504445fffbSMatthew Ahrens * nvl here in case the snapshot is created between 8514445fffbSMatthew Ahrens * now and when we try to destroy it (in which case 8524445fffbSMatthew Ahrens * we don't want to destroy it since we haven't 8534445fffbSMatthew Ahrens * checked for permission). 8544445fffbSMatthew Ahrens */ 8554445fffbSMatthew Ahrens fnvlist_remove_nvpair(snaps, pair); 8564445fffbSMatthew Ahrens error = 0; 8574445fffbSMatthew Ahrens } 8584445fffbSMatthew Ahrens if (error != 0) 8594445fffbSMatthew Ahrens break; 8604445fffbSMatthew Ahrens } 861cbf6f6aaSWilliam Gorrell 862cbf6f6aaSWilliam Gorrell return (error); 863cbf6f6aaSWilliam Gorrell } 864cbf6f6aaSWilliam Gorrell 865ecd6cf80Smarks int 866ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 867ecd6cf80Smarks { 86892241e0bSTom Erickson char parentname[MAXNAMELEN]; 869ecd6cf80Smarks int error; 870ecd6cf80Smarks 871ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(from, 872ecd6cf80Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0) 873ecd6cf80Smarks return (error); 874ecd6cf80Smarks 875ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(from, 876ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 877ecd6cf80Smarks return (error); 878ecd6cf80Smarks 879ecd6cf80Smarks if ((error = zfs_get_parent(to, parentname, 880ecd6cf80Smarks sizeof (parentname))) != 0) 881ecd6cf80Smarks return (error); 882ecd6cf80Smarks 883ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname, 884ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 885ecd6cf80Smarks return (error); 886ecd6cf80Smarks 887ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname, 888ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 889ecd6cf80Smarks return (error); 890ecd6cf80Smarks 891ecd6cf80Smarks return (error); 892ecd6cf80Smarks } 893ecd6cf80Smarks 8944445fffbSMatthew Ahrens /* ARGSUSED */ 895ecd6cf80Smarks static int 8964445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 897ecd6cf80Smarks { 898ecd6cf80Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 899ecd6cf80Smarks } 900ecd6cf80Smarks 9014445fffbSMatthew Ahrens /* ARGSUSED */ 902ecd6cf80Smarks static int 9034445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 904ecd6cf80Smarks { 9053b2aab18SMatthew Ahrens dsl_pool_t *dp; 9063b2aab18SMatthew Ahrens dsl_dataset_t *clone; 907ecd6cf80Smarks int error; 908ecd6cf80Smarks 909ecd6cf80Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 910ecd6cf80Smarks ZFS_DELEG_PERM_PROMOTE, cr); 9113b2aab18SMatthew Ahrens if (error != 0) 9123b2aab18SMatthew Ahrens return (error); 9133b2aab18SMatthew Ahrens 9143b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 9153b2aab18SMatthew Ahrens if (error != 0) 916ecd6cf80Smarks return (error); 917ecd6cf80Smarks 9183b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone); 919ecd6cf80Smarks 920ecd6cf80Smarks if (error == 0) { 9213b2aab18SMatthew Ahrens char parentname[MAXNAMELEN]; 9223b2aab18SMatthew Ahrens dsl_dataset_t *origin = NULL; 923ecd6cf80Smarks dsl_dir_t *dd; 9243b2aab18SMatthew Ahrens dd = clone->ds_dir; 925ecd6cf80Smarks 926745cd3c5Smaybee error = dsl_dataset_hold_obj(dd->dd_pool, 9273b2aab18SMatthew Ahrens dd->dd_phys->dd_origin_obj, FTAG, &origin); 9283b2aab18SMatthew Ahrens if (error != 0) { 9293b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 9303b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 931ecd6cf80Smarks return (error); 932ecd6cf80Smarks } 933ecd6cf80Smarks 9343b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone, 935ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr); 936ecd6cf80Smarks 9373b2aab18SMatthew Ahrens dsl_dataset_name(origin, parentname); 9383b2aab18SMatthew Ahrens if (error == 0) { 9393b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms_ds(parentname, origin, 940ecd6cf80Smarks ZFS_DELEG_PERM_PROMOTE, cr); 9413b2aab18SMatthew Ahrens } 9423b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 9433b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 944ecd6cf80Smarks } 9453b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 946ecd6cf80Smarks return (error); 947ecd6cf80Smarks } 948ecd6cf80Smarks 9494445fffbSMatthew Ahrens /* ARGSUSED */ 950ecd6cf80Smarks static int 9514445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 952ecd6cf80Smarks { 953ecd6cf80Smarks int error; 954ecd6cf80Smarks 955ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 956ecd6cf80Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 957ecd6cf80Smarks return (error); 958ecd6cf80Smarks 959ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 960ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 961ecd6cf80Smarks return (error); 962ecd6cf80Smarks 963ecd6cf80Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 964ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)); 965ecd6cf80Smarks } 966ecd6cf80Smarks 967ecd6cf80Smarks int 968ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 969ecd6cf80Smarks { 970681d9761SEric Taylor return (zfs_secpolicy_write_perms(name, 971681d9761SEric Taylor ZFS_DELEG_PERM_SNAPSHOT, cr)); 972ecd6cf80Smarks } 973ecd6cf80Smarks 9744445fffbSMatthew Ahrens /* 9754445fffbSMatthew Ahrens * Check for permission to create each snapshot in the nvlist. 9764445fffbSMatthew Ahrens */ 9774445fffbSMatthew Ahrens /* ARGSUSED */ 978ecd6cf80Smarks static int 9794445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 980ecd6cf80Smarks { 9814445fffbSMatthew Ahrens nvlist_t *snaps; 982d5285caeSGeorge Wilson int error = 0; 9834445fffbSMatthew Ahrens nvpair_t *pair; 984ecd6cf80Smarks 9854445fffbSMatthew Ahrens if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 986be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 9874445fffbSMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 9884445fffbSMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 9894445fffbSMatthew Ahrens char *name = nvpair_name(pair); 9904445fffbSMatthew Ahrens char *atp = strchr(name, '@'); 9914445fffbSMatthew Ahrens 9924445fffbSMatthew Ahrens if (atp == NULL) { 993be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 9944445fffbSMatthew Ahrens break; 9954445fffbSMatthew Ahrens } 9964445fffbSMatthew Ahrens *atp = '\0'; 9974445fffbSMatthew Ahrens error = zfs_secpolicy_snapshot_perms(name, cr); 9984445fffbSMatthew Ahrens *atp = '@'; 9994445fffbSMatthew Ahrens if (error != 0) 10004445fffbSMatthew Ahrens break; 10014445fffbSMatthew Ahrens } 10024445fffbSMatthew Ahrens return (error); 1003ecd6cf80Smarks } 1004ecd6cf80Smarks 10054445fffbSMatthew Ahrens /* ARGSUSED */ 1006ecd6cf80Smarks static int 10074445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 10084445fffbSMatthew Ahrens { 10094445fffbSMatthew Ahrens /* 10104445fffbSMatthew Ahrens * Even root must have a proper TSD so that we know what pool 10114445fffbSMatthew Ahrens * to log to. 10124445fffbSMatthew Ahrens */ 10134445fffbSMatthew Ahrens if (tsd_get(zfs_allow_log_key) == NULL) 1014be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 10154445fffbSMatthew Ahrens return (0); 10164445fffbSMatthew Ahrens } 10174445fffbSMatthew Ahrens 10184445fffbSMatthew Ahrens static int 10194445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1020ecd6cf80Smarks { 102192241e0bSTom Erickson char parentname[MAXNAMELEN]; 102292241e0bSTom Erickson int error; 10234445fffbSMatthew Ahrens char *origin; 1024ecd6cf80Smarks 1025ecd6cf80Smarks if ((error = zfs_get_parent(zc->zc_name, parentname, 1026ecd6cf80Smarks sizeof (parentname))) != 0) 1027ecd6cf80Smarks return (error); 1028fa9e4066Sahrens 10294445fffbSMatthew Ahrens if (nvlist_lookup_string(innvl, "origin", &origin) == 0 && 10304445fffbSMatthew Ahrens (error = zfs_secpolicy_write_perms(origin, 10314445fffbSMatthew Ahrens ZFS_DELEG_PERM_CLONE, cr)) != 0) 10324445fffbSMatthew Ahrens return (error); 1033fa9e4066Sahrens 1034ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname, 1035ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 1036ecd6cf80Smarks return (error); 1037ecd6cf80Smarks 10384445fffbSMatthew Ahrens return (zfs_secpolicy_write_perms(parentname, 10394445fffbSMatthew Ahrens ZFS_DELEG_PERM_MOUNT, cr)); 1040fa9e4066Sahrens } 1041fa9e4066Sahrens 1042fa9e4066Sahrens /* 1043fa9e4066Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 1044fa9e4066Sahrens * SYS_CONFIG privilege, which is not available in a local zone. 1045fa9e4066Sahrens */ 1046fa9e4066Sahrens /* ARGSUSED */ 1047fa9e4066Sahrens static int 10484445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1049fa9e4066Sahrens { 1050fa9e4066Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0) 1051be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM)); 1052fa9e4066Sahrens 1053fa9e4066Sahrens return (0); 1054fa9e4066Sahrens } 1055fa9e4066Sahrens 105699d5e173STim Haley /* 105799d5e173STim Haley * Policy for object to name lookups. 105899d5e173STim Haley */ 105999d5e173STim Haley /* ARGSUSED */ 106099d5e173STim Haley static int 10614445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 106299d5e173STim Haley { 106399d5e173STim Haley int error; 106499d5e173STim Haley 106599d5e173STim Haley if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0) 106699d5e173STim Haley return (0); 106799d5e173STim Haley 106899d5e173STim Haley error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr); 106999d5e173STim Haley return (error); 107099d5e173STim Haley } 107199d5e173STim Haley 1072ea8dc4b6Seschrock /* 1073ea8dc4b6Seschrock * Policy for fault injection. Requires all privileges. 1074ea8dc4b6Seschrock */ 1075ea8dc4b6Seschrock /* ARGSUSED */ 1076ea8dc4b6Seschrock static int 10774445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1078ea8dc4b6Seschrock { 1079ea8dc4b6Seschrock return (secpolicy_zinject(cr)); 1080ea8dc4b6Seschrock } 1081ea8dc4b6Seschrock 10824445fffbSMatthew Ahrens /* ARGSUSED */ 1083e45ce728Sahrens static int 10844445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1085e45ce728Sahrens { 1086e45ce728Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 1087e45ce728Sahrens 1088990b4856Slling if (prop == ZPROP_INVAL) { 1089e45ce728Sahrens if (!zfs_prop_user(zc->zc_value)) 1090be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1091e45ce728Sahrens return (zfs_secpolicy_write_perms(zc->zc_name, 1092e45ce728Sahrens ZFS_DELEG_PERM_USERPROP, cr)); 1093e45ce728Sahrens } else { 109492241e0bSTom Erickson return (zfs_secpolicy_setprop(zc->zc_name, prop, 109592241e0bSTom Erickson NULL, cr)); 1096e45ce728Sahrens } 1097e45ce728Sahrens } 1098e45ce728Sahrens 109914843421SMatthew Ahrens static int 11004445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 110114843421SMatthew Ahrens { 11024445fffbSMatthew Ahrens int err = zfs_secpolicy_read(zc, innvl, cr); 110314843421SMatthew Ahrens if (err) 110414843421SMatthew Ahrens return (err); 110514843421SMatthew Ahrens 110614843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 1107be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 110814843421SMatthew Ahrens 110914843421SMatthew Ahrens if (zc->zc_value[0] == 0) { 111014843421SMatthew Ahrens /* 111114843421SMatthew Ahrens * They are asking about a posix uid/gid. If it's 111214843421SMatthew Ahrens * themself, allow it. 111314843421SMatthew Ahrens */ 111414843421SMatthew Ahrens if (zc->zc_objset_type == ZFS_PROP_USERUSED || 111514843421SMatthew Ahrens zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 111614843421SMatthew Ahrens if (zc->zc_guid == crgetuid(cr)) 111714843421SMatthew Ahrens return (0); 111814843421SMatthew Ahrens } else { 111914843421SMatthew Ahrens if (groupmember(zc->zc_guid, cr)) 112014843421SMatthew Ahrens return (0); 112114843421SMatthew Ahrens } 112214843421SMatthew Ahrens } 112314843421SMatthew Ahrens 112414843421SMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name, 112514843421SMatthew Ahrens userquota_perms[zc->zc_objset_type], cr)); 112614843421SMatthew Ahrens } 112714843421SMatthew Ahrens 112814843421SMatthew Ahrens static int 11294445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 113014843421SMatthew Ahrens { 11314445fffbSMatthew Ahrens int err = zfs_secpolicy_read(zc, innvl, cr); 113214843421SMatthew Ahrens if (err) 113314843421SMatthew Ahrens return (err); 113414843421SMatthew Ahrens 113514843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 1136be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 113714843421SMatthew Ahrens 113814843421SMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name, 113914843421SMatthew Ahrens userquota_perms[zc->zc_objset_type], cr)); 114014843421SMatthew Ahrens } 114114843421SMatthew Ahrens 11424445fffbSMatthew Ahrens /* ARGSUSED */ 114314843421SMatthew Ahrens static int 11444445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 114514843421SMatthew Ahrens { 114692241e0bSTom Erickson return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 114792241e0bSTom Erickson NULL, cr)); 114814843421SMatthew Ahrens } 114914843421SMatthew Ahrens 11504445fffbSMatthew Ahrens /* ARGSUSED */ 1151842727c2SChris Kirby static int 11524445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1153842727c2SChris Kirby { 11543b2aab18SMatthew Ahrens nvpair_t *pair; 11553b2aab18SMatthew Ahrens nvlist_t *holds; 11563b2aab18SMatthew Ahrens int error; 11573b2aab18SMatthew Ahrens 11583b2aab18SMatthew Ahrens error = nvlist_lookup_nvlist(innvl, "holds", &holds); 11593b2aab18SMatthew Ahrens if (error != 0) 1160be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 11613b2aab18SMatthew Ahrens 11623b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; 11633b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(holds, pair)) { 11643b2aab18SMatthew Ahrens char fsname[MAXNAMELEN]; 11653b2aab18SMatthew Ahrens error = dmu_fsname(nvpair_name(pair), fsname); 11663b2aab18SMatthew Ahrens if (error != 0) 11673b2aab18SMatthew Ahrens return (error); 11683b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms(fsname, 11693b2aab18SMatthew Ahrens ZFS_DELEG_PERM_HOLD, cr); 11703b2aab18SMatthew Ahrens if (error != 0) 11713b2aab18SMatthew Ahrens return (error); 11723b2aab18SMatthew Ahrens } 11733b2aab18SMatthew Ahrens return (0); 1174842727c2SChris Kirby } 1175842727c2SChris Kirby 11764445fffbSMatthew Ahrens /* ARGSUSED */ 1177842727c2SChris Kirby static int 11784445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1179842727c2SChris Kirby { 11803b2aab18SMatthew Ahrens nvpair_t *pair; 11813b2aab18SMatthew Ahrens int error; 11823b2aab18SMatthew Ahrens 11833b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL; 11843b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(innvl, pair)) { 11853b2aab18SMatthew Ahrens char fsname[MAXNAMELEN]; 11863b2aab18SMatthew Ahrens error = dmu_fsname(nvpair_name(pair), fsname); 11873b2aab18SMatthew Ahrens if (error != 0) 11883b2aab18SMatthew Ahrens return (error); 11893b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms(fsname, 11903b2aab18SMatthew Ahrens ZFS_DELEG_PERM_RELEASE, cr); 11913b2aab18SMatthew Ahrens if (error != 0) 11923b2aab18SMatthew Ahrens return (error); 11933b2aab18SMatthew Ahrens } 11943b2aab18SMatthew Ahrens return (0); 1195842727c2SChris Kirby } 1196842727c2SChris Kirby 119799d5e173STim Haley /* 119899d5e173STim Haley * Policy for allowing temporary snapshots to be taken or released 119999d5e173STim Haley */ 120099d5e173STim Haley static int 12014445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 120299d5e173STim Haley { 120399d5e173STim Haley /* 120499d5e173STim Haley * A temporary snapshot is the same as a snapshot, 120599d5e173STim Haley * hold, destroy and release all rolled into one. 120699d5e173STim Haley * Delegated diff alone is sufficient that we allow this. 120799d5e173STim Haley */ 120899d5e173STim Haley int error; 120999d5e173STim Haley 121099d5e173STim Haley if ((error = zfs_secpolicy_write_perms(zc->zc_name, 121199d5e173STim Haley ZFS_DELEG_PERM_DIFF, cr)) == 0) 121299d5e173STim Haley return (0); 121399d5e173STim Haley 12144445fffbSMatthew Ahrens error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr); 12153b2aab18SMatthew Ahrens if (error == 0) 12164445fffbSMatthew Ahrens error = zfs_secpolicy_hold(zc, innvl, cr); 12173b2aab18SMatthew Ahrens if (error == 0) 12184445fffbSMatthew Ahrens error = zfs_secpolicy_release(zc, innvl, cr); 12193b2aab18SMatthew Ahrens if (error == 0) 12204445fffbSMatthew Ahrens error = zfs_secpolicy_destroy(zc, innvl, cr); 122199d5e173STim Haley return (error); 122299d5e173STim Haley } 122399d5e173STim Haley 1224fa9e4066Sahrens /* 1225fa9e4066Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t. 1226fa9e4066Sahrens */ 1227fa9e4066Sahrens static int 1228478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 1229fa9e4066Sahrens { 1230fa9e4066Sahrens char *packed; 1231fa9e4066Sahrens int error; 1232990b4856Slling nvlist_t *list = NULL; 1233fa9e4066Sahrens 1234fa9e4066Sahrens /* 1235e9dbad6fSeschrock * Read in and unpack the user-supplied nvlist. 1236fa9e4066Sahrens */ 1237990b4856Slling if (size == 0) 1238be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1239fa9e4066Sahrens 1240fa9e4066Sahrens packed = kmem_alloc(size, KM_SLEEP); 1241fa9e4066Sahrens 1242478ed9adSEric Taylor if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 1243478ed9adSEric Taylor iflag)) != 0) { 1244fa9e4066Sahrens kmem_free(packed, size); 1245fa9e4066Sahrens return (error); 1246fa9e4066Sahrens } 1247fa9e4066Sahrens 1248990b4856Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 1249fa9e4066Sahrens kmem_free(packed, size); 1250fa9e4066Sahrens return (error); 1251fa9e4066Sahrens } 1252fa9e4066Sahrens 1253fa9e4066Sahrens kmem_free(packed, size); 1254fa9e4066Sahrens 1255990b4856Slling *nvp = list; 1256fa9e4066Sahrens return (0); 1257fa9e4066Sahrens } 1258fa9e4066Sahrens 12594445fffbSMatthew Ahrens /* 12604445fffbSMatthew Ahrens * Reduce the size of this nvlist until it can be serialized in 'max' bytes. 12614445fffbSMatthew Ahrens * Entries will be removed from the end of the nvlist, and one int32 entry 12624445fffbSMatthew Ahrens * named "N_MORE_ERRORS" will be added indicating how many entries were 12634445fffbSMatthew Ahrens * removed. 12644445fffbSMatthew Ahrens */ 126592241e0bSTom Erickson static int 12664445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max) 126792241e0bSTom Erickson { 126892241e0bSTom Erickson size_t size; 126992241e0bSTom Erickson 12704445fffbSMatthew Ahrens size = fnvlist_size(errors); 127192241e0bSTom Erickson 12724445fffbSMatthew Ahrens if (size > max) { 127392241e0bSTom Erickson nvpair_t *more_errors; 127492241e0bSTom Erickson int n = 0; 127592241e0bSTom Erickson 12764445fffbSMatthew Ahrens if (max < 1024) 1277be6fd75aSMatthew Ahrens return (SET_ERROR(ENOMEM)); 127892241e0bSTom Erickson 12794445fffbSMatthew Ahrens fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0); 12804445fffbSMatthew Ahrens more_errors = nvlist_prev_nvpair(errors, NULL); 128192241e0bSTom Erickson 128292241e0bSTom Erickson do { 12834445fffbSMatthew Ahrens nvpair_t *pair = nvlist_prev_nvpair(errors, 128492241e0bSTom Erickson more_errors); 12854445fffbSMatthew Ahrens fnvlist_remove_nvpair(errors, pair); 128692241e0bSTom Erickson n++; 12874445fffbSMatthew Ahrens size = fnvlist_size(errors); 12884445fffbSMatthew Ahrens } while (size > max); 128992241e0bSTom Erickson 12904445fffbSMatthew Ahrens fnvlist_remove_nvpair(errors, more_errors); 12914445fffbSMatthew Ahrens fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n); 12924445fffbSMatthew Ahrens ASSERT3U(fnvlist_size(errors), <=, max); 129392241e0bSTom Erickson } 129492241e0bSTom Erickson 129592241e0bSTom Erickson return (0); 129692241e0bSTom Erickson } 129792241e0bSTom Erickson 1298e9dbad6fSeschrock static int 1299e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 1300e9dbad6fSeschrock { 1301e9dbad6fSeschrock char *packed = NULL; 13026e27f868SSam Falkner int error = 0; 1303e9dbad6fSeschrock size_t size; 1304e9dbad6fSeschrock 13054445fffbSMatthew Ahrens size = fnvlist_size(nvl); 1306e9dbad6fSeschrock 1307e9dbad6fSeschrock if (size > zc->zc_nvlist_dst_size) { 1308be6fd75aSMatthew Ahrens error = SET_ERROR(ENOMEM); 1309e9dbad6fSeschrock } else { 13104445fffbSMatthew Ahrens packed = fnvlist_pack(nvl, &size); 13116e27f868SSam Falkner if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 13126e27f868SSam Falkner size, zc->zc_iflags) != 0) 1313be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 13144445fffbSMatthew Ahrens fnvlist_pack_free(packed, size); 1315e9dbad6fSeschrock } 1316e9dbad6fSeschrock 1317e9dbad6fSeschrock zc->zc_nvlist_dst_size = size; 13184445fffbSMatthew Ahrens zc->zc_nvlist_dst_filled = B_TRUE; 1319e9dbad6fSeschrock return (error); 1320e9dbad6fSeschrock } 1321e9dbad6fSeschrock 132214843421SMatthew Ahrens static int 1323af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp) 132414843421SMatthew Ahrens { 132514843421SMatthew Ahrens objset_t *os; 132614843421SMatthew Ahrens int error; 132714843421SMatthew Ahrens 1328503ad85cSMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os); 13293b2aab18SMatthew Ahrens if (error != 0) 133014843421SMatthew Ahrens return (error); 1331503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 1332503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1333be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1334503ad85cSMatthew Ahrens } 133514843421SMatthew Ahrens 1336503ad85cSMatthew Ahrens mutex_enter(&os->os_user_ptr_lock); 1337af4c679fSSean McEnroe *zfvp = dmu_objset_get_user(os); 1338af4c679fSSean McEnroe if (*zfvp) { 1339af4c679fSSean McEnroe VFS_HOLD((*zfvp)->z_vfs); 134014843421SMatthew Ahrens } else { 1341be6fd75aSMatthew Ahrens error = SET_ERROR(ESRCH); 134214843421SMatthew Ahrens } 1343503ad85cSMatthew Ahrens mutex_exit(&os->os_user_ptr_lock); 1344503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 134514843421SMatthew Ahrens return (error); 134614843421SMatthew Ahrens } 134714843421SMatthew Ahrens 134814843421SMatthew Ahrens /* 134914843421SMatthew Ahrens * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 135014843421SMatthew Ahrens * case its z_vfs will be NULL, and it will be opened as the owner. 1351ad135b5dSChristopher Siden * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER, 1352ad135b5dSChristopher Siden * which prevents all vnode ops from running. 135314843421SMatthew Ahrens */ 135414843421SMatthew Ahrens static int 13551412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer) 135614843421SMatthew Ahrens { 135714843421SMatthew Ahrens int error = 0; 135814843421SMatthew Ahrens 1359af4c679fSSean McEnroe if (getzfsvfs(name, zfvp) != 0) 1360af4c679fSSean McEnroe error = zfsvfs_create(name, zfvp); 136114843421SMatthew Ahrens if (error == 0) { 13621412a1a2SMark Shellenbaum rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER : 13631412a1a2SMark Shellenbaum RW_READER, tag); 1364af4c679fSSean McEnroe if ((*zfvp)->z_unmounted) { 136514843421SMatthew Ahrens /* 136614843421SMatthew Ahrens * XXX we could probably try again, since the unmounting 136714843421SMatthew Ahrens * thread should be just about to disassociate the 136814843421SMatthew Ahrens * objset from the zfsvfs. 136914843421SMatthew Ahrens */ 1370af4c679fSSean McEnroe rrw_exit(&(*zfvp)->z_teardown_lock, tag); 1371be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 137214843421SMatthew Ahrens } 137314843421SMatthew Ahrens } 137414843421SMatthew Ahrens return (error); 137514843421SMatthew Ahrens } 137614843421SMatthew Ahrens 137714843421SMatthew Ahrens static void 137814843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 137914843421SMatthew Ahrens { 138014843421SMatthew Ahrens rrw_exit(&zfsvfs->z_teardown_lock, tag); 138114843421SMatthew Ahrens 138214843421SMatthew Ahrens if (zfsvfs->z_vfs) { 138314843421SMatthew Ahrens VFS_RELE(zfsvfs->z_vfs); 138414843421SMatthew Ahrens } else { 1385503ad85cSMatthew Ahrens dmu_objset_disown(zfsvfs->z_os, zfsvfs); 138614843421SMatthew Ahrens zfsvfs_free(zfsvfs); 138714843421SMatthew Ahrens } 138814843421SMatthew Ahrens } 138914843421SMatthew Ahrens 1390fa9e4066Sahrens static int 1391fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc) 1392fa9e4066Sahrens { 1393fa9e4066Sahrens int error; 1394990b4856Slling nvlist_t *config, *props = NULL; 13950a48a24eStimh nvlist_t *rootprops = NULL; 13960a48a24eStimh nvlist_t *zplprops = NULL; 1397fa9e4066Sahrens 1398990b4856Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1399478ed9adSEric Taylor zc->zc_iflags, &config)) 1400fa9e4066Sahrens return (error); 14012a6b87f0Sek 1402990b4856Slling if (zc->zc_nvlist_src_size != 0 && (error = 1403478ed9adSEric Taylor get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1404478ed9adSEric Taylor zc->zc_iflags, &props))) { 1405990b4856Slling nvlist_free(config); 1406990b4856Slling return (error); 1407990b4856Slling } 1408990b4856Slling 14090a48a24eStimh if (props) { 14100a48a24eStimh nvlist_t *nvl = NULL; 14110a48a24eStimh uint64_t version = SPA_VERSION; 14120a48a24eStimh 14130a48a24eStimh (void) nvlist_lookup_uint64(props, 14140a48a24eStimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 1415ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(version)) { 1416be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 14170a48a24eStimh goto pool_props_bad; 14180a48a24eStimh } 14190a48a24eStimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 14200a48a24eStimh if (nvl) { 14210a48a24eStimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 14220a48a24eStimh if (error != 0) { 14230a48a24eStimh nvlist_free(config); 14240a48a24eStimh nvlist_free(props); 14250a48a24eStimh return (error); 14260a48a24eStimh } 14270a48a24eStimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 14280a48a24eStimh } 14290a48a24eStimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 14300a48a24eStimh error = zfs_fill_zplprops_root(version, rootprops, 14310a48a24eStimh zplprops, NULL); 14323b2aab18SMatthew Ahrens if (error != 0) 14330a48a24eStimh goto pool_props_bad; 14340a48a24eStimh } 14350a48a24eStimh 14364445fffbSMatthew Ahrens error = spa_create(zc->zc_name, config, props, zplprops); 14370a48a24eStimh 14380a48a24eStimh /* 14390a48a24eStimh * Set the remaining root properties 14400a48a24eStimh */ 144192241e0bSTom Erickson if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, 144292241e0bSTom Erickson ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) 14430a48a24eStimh (void) spa_destroy(zc->zc_name); 1444fa9e4066Sahrens 14450a48a24eStimh pool_props_bad: 14460a48a24eStimh nvlist_free(rootprops); 14470a48a24eStimh nvlist_free(zplprops); 1448fa9e4066Sahrens nvlist_free(config); 14490a48a24eStimh nvlist_free(props); 1450990b4856Slling 1451fa9e4066Sahrens return (error); 1452fa9e4066Sahrens } 1453fa9e4066Sahrens 1454fa9e4066Sahrens static int 1455fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1456fa9e4066Sahrens { 1457ecd6cf80Smarks int error; 1458ecd6cf80Smarks zfs_log_history(zc); 1459ecd6cf80Smarks error = spa_destroy(zc->zc_name); 1460681d9761SEric Taylor if (error == 0) 1461681d9761SEric Taylor zvol_remove_minors(zc->zc_name); 1462ecd6cf80Smarks return (error); 1463fa9e4066Sahrens } 1464fa9e4066Sahrens 1465fa9e4066Sahrens static int 1466fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc) 1467fa9e4066Sahrens { 1468990b4856Slling nvlist_t *config, *props = NULL; 1469fa9e4066Sahrens uint64_t guid; 1470468c413aSTim Haley int error; 1471fa9e4066Sahrens 1472990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1473478ed9adSEric Taylor zc->zc_iflags, &config)) != 0) 1474990b4856Slling return (error); 1475990b4856Slling 1476990b4856Slling if (zc->zc_nvlist_src_size != 0 && (error = 1477478ed9adSEric Taylor get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1478478ed9adSEric Taylor zc->zc_iflags, &props))) { 1479990b4856Slling nvlist_free(config); 1480fa9e4066Sahrens return (error); 1481990b4856Slling } 1482fa9e4066Sahrens 1483fa9e4066Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1484ea8dc4b6Seschrock guid != zc->zc_guid) 1485be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 1486fa9e4066Sahrens else 14874b964adaSGeorge Wilson error = spa_import(zc->zc_name, config, props, zc->zc_cookie); 1488fa9e4066Sahrens 14894b964adaSGeorge Wilson if (zc->zc_nvlist_dst != 0) { 14904b964adaSGeorge Wilson int err; 14914b964adaSGeorge Wilson 14924b964adaSGeorge Wilson if ((err = put_nvlist(zc, config)) != 0) 14934b964adaSGeorge Wilson error = err; 14944b964adaSGeorge Wilson } 1495468c413aSTim Haley 1496fa9e4066Sahrens nvlist_free(config); 1497fa9e4066Sahrens 1498990b4856Slling if (props) 1499990b4856Slling nvlist_free(props); 1500990b4856Slling 1501fa9e4066Sahrens return (error); 1502fa9e4066Sahrens } 1503fa9e4066Sahrens 1504fa9e4066Sahrens static int 1505fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc) 1506fa9e4066Sahrens { 1507ecd6cf80Smarks int error; 150889a89ebfSlling boolean_t force = (boolean_t)zc->zc_cookie; 1509394ab0cbSGeorge Wilson boolean_t hardforce = (boolean_t)zc->zc_guid; 151089a89ebfSlling 1511ecd6cf80Smarks zfs_log_history(zc); 1512394ab0cbSGeorge Wilson error = spa_export(zc->zc_name, NULL, force, hardforce); 1513681d9761SEric Taylor if (error == 0) 1514681d9761SEric Taylor zvol_remove_minors(zc->zc_name); 1515ecd6cf80Smarks return (error); 1516fa9e4066Sahrens } 1517fa9e4066Sahrens 1518fa9e4066Sahrens static int 1519fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 1520fa9e4066Sahrens { 1521fa9e4066Sahrens nvlist_t *configs; 1522fa9e4066Sahrens int error; 1523fa9e4066Sahrens 1524fa9e4066Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1525be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 1526fa9e4066Sahrens 1527e9dbad6fSeschrock error = put_nvlist(zc, configs); 1528fa9e4066Sahrens 1529fa9e4066Sahrens nvlist_free(configs); 1530fa9e4066Sahrens 1531fa9e4066Sahrens return (error); 1532fa9e4066Sahrens } 1533fa9e4066Sahrens 1534ad135b5dSChristopher Siden /* 1535ad135b5dSChristopher Siden * inputs: 1536ad135b5dSChristopher Siden * zc_name name of the pool 1537ad135b5dSChristopher Siden * 1538ad135b5dSChristopher Siden * outputs: 1539ad135b5dSChristopher Siden * zc_cookie real errno 1540ad135b5dSChristopher Siden * zc_nvlist_dst config nvlist 1541ad135b5dSChristopher Siden * zc_nvlist_dst_size size of config nvlist 1542ad135b5dSChristopher Siden */ 1543fa9e4066Sahrens static int 1544fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 1545fa9e4066Sahrens { 1546fa9e4066Sahrens nvlist_t *config; 1547fa9e4066Sahrens int error; 1548ea8dc4b6Seschrock int ret = 0; 1549fa9e4066Sahrens 1550e9dbad6fSeschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 1551e9dbad6fSeschrock sizeof (zc->zc_value)); 1552fa9e4066Sahrens 1553fa9e4066Sahrens if (config != NULL) { 1554e9dbad6fSeschrock ret = put_nvlist(zc, config); 1555fa9e4066Sahrens nvlist_free(config); 1556ea8dc4b6Seschrock 1557ea8dc4b6Seschrock /* 1558ea8dc4b6Seschrock * The config may be present even if 'error' is non-zero. 1559ea8dc4b6Seschrock * In this case we return success, and preserve the real errno 1560ea8dc4b6Seschrock * in 'zc_cookie'. 1561ea8dc4b6Seschrock */ 1562ea8dc4b6Seschrock zc->zc_cookie = error; 1563fa9e4066Sahrens } else { 1564ea8dc4b6Seschrock ret = error; 1565fa9e4066Sahrens } 1566fa9e4066Sahrens 1567ea8dc4b6Seschrock return (ret); 1568fa9e4066Sahrens } 1569fa9e4066Sahrens 1570fa9e4066Sahrens /* 1571fa9e4066Sahrens * Try to import the given pool, returning pool stats as appropriate so that 1572fa9e4066Sahrens * user land knows which devices are available and overall pool health. 1573fa9e4066Sahrens */ 1574fa9e4066Sahrens static int 1575fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1576fa9e4066Sahrens { 1577fa9e4066Sahrens nvlist_t *tryconfig, *config; 1578fa9e4066Sahrens int error; 1579fa9e4066Sahrens 1580990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1581478ed9adSEric Taylor zc->zc_iflags, &tryconfig)) != 0) 1582fa9e4066Sahrens return (error); 1583fa9e4066Sahrens 1584fa9e4066Sahrens config = spa_tryimport(tryconfig); 1585fa9e4066Sahrens 1586fa9e4066Sahrens nvlist_free(tryconfig); 1587fa9e4066Sahrens 1588fa9e4066Sahrens if (config == NULL) 1589be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1590fa9e4066Sahrens 1591e9dbad6fSeschrock error = put_nvlist(zc, config); 1592fa9e4066Sahrens nvlist_free(config); 1593fa9e4066Sahrens 1594fa9e4066Sahrens return (error); 1595fa9e4066Sahrens } 1596fa9e4066Sahrens 15973f9d6ad7SLin Ling /* 15983f9d6ad7SLin Ling * inputs: 15993f9d6ad7SLin Ling * zc_name name of the pool 16003f9d6ad7SLin Ling * zc_cookie scan func (pool_scan_func_t) 16013f9d6ad7SLin Ling */ 1602fa9e4066Sahrens static int 16033f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc) 1604fa9e4066Sahrens { 1605fa9e4066Sahrens spa_t *spa; 1606fa9e4066Sahrens int error; 1607fa9e4066Sahrens 160806eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 160906eeb2adSek return (error); 161006eeb2adSek 16113f9d6ad7SLin Ling if (zc->zc_cookie == POOL_SCAN_NONE) 16123f9d6ad7SLin Ling error = spa_scan_stop(spa); 16133f9d6ad7SLin Ling else 16143f9d6ad7SLin Ling error = spa_scan(spa, zc->zc_cookie); 161506eeb2adSek 161606eeb2adSek spa_close(spa, FTAG); 161706eeb2adSek 1618fa9e4066Sahrens return (error); 1619fa9e4066Sahrens } 1620fa9e4066Sahrens 1621fa9e4066Sahrens static int 1622fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1623fa9e4066Sahrens { 1624fa9e4066Sahrens spa_t *spa; 1625fa9e4066Sahrens int error; 1626fa9e4066Sahrens 1627fa9e4066Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1628fa9e4066Sahrens if (error == 0) { 1629fa9e4066Sahrens spa_freeze(spa); 1630fa9e4066Sahrens spa_close(spa, FTAG); 1631fa9e4066Sahrens } 1632fa9e4066Sahrens return (error); 1633fa9e4066Sahrens } 1634fa9e4066Sahrens 1635eaca9bbdSeschrock static int 1636eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 1637eaca9bbdSeschrock { 1638eaca9bbdSeschrock spa_t *spa; 1639eaca9bbdSeschrock int error; 1640eaca9bbdSeschrock 164106eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 164206eeb2adSek return (error); 164306eeb2adSek 1644ad135b5dSChristopher Siden if (zc->zc_cookie < spa_version(spa) || 1645ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) { 1646558d2d50Slling spa_close(spa, FTAG); 1647be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1648558d2d50Slling } 1649558d2d50Slling 1650990b4856Slling spa_upgrade(spa, zc->zc_cookie); 165106eeb2adSek spa_close(spa, FTAG); 165206eeb2adSek 165306eeb2adSek return (error); 165406eeb2adSek } 165506eeb2adSek 165606eeb2adSek static int 165706eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc) 165806eeb2adSek { 165906eeb2adSek spa_t *spa; 166006eeb2adSek char *hist_buf; 166106eeb2adSek uint64_t size; 166206eeb2adSek int error; 166306eeb2adSek 166406eeb2adSek if ((size = zc->zc_history_len) == 0) 1665be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 166606eeb2adSek 166706eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 166806eeb2adSek return (error); 166906eeb2adSek 1670e7437265Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 1671d7306b64Sek spa_close(spa, FTAG); 1672be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 1673d7306b64Sek } 1674d7306b64Sek 167506eeb2adSek hist_buf = kmem_alloc(size, KM_SLEEP); 167606eeb2adSek if ((error = spa_history_get(spa, &zc->zc_history_offset, 167706eeb2adSek &zc->zc_history_len, hist_buf)) == 0) { 1678478ed9adSEric Taylor error = ddi_copyout(hist_buf, 1679478ed9adSEric Taylor (void *)(uintptr_t)zc->zc_history, 1680478ed9adSEric Taylor zc->zc_history_len, zc->zc_iflags); 168106eeb2adSek } 168206eeb2adSek 168306eeb2adSek spa_close(spa, FTAG); 168406eeb2adSek kmem_free(hist_buf, size); 168506eeb2adSek return (error); 168606eeb2adSek } 168706eeb2adSek 1688e9103aaeSGarrett D'Amore static int 1689e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc) 1690e9103aaeSGarrett D'Amore { 1691e9103aaeSGarrett D'Amore spa_t *spa; 1692e9103aaeSGarrett D'Amore int error; 1693e9103aaeSGarrett D'Amore 1694e9103aaeSGarrett D'Amore error = spa_open(zc->zc_name, &spa, FTAG); 1695e9103aaeSGarrett D'Amore if (error == 0) { 1696e9103aaeSGarrett D'Amore error = spa_change_guid(spa); 1697e9103aaeSGarrett D'Amore spa_close(spa, FTAG); 1698e9103aaeSGarrett D'Amore } 1699e9103aaeSGarrett D'Amore return (error); 1700e9103aaeSGarrett D'Amore } 1701e9103aaeSGarrett D'Amore 170255434c77Sek static int 170355434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 170455434c77Sek { 17053b2aab18SMatthew Ahrens return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)); 170655434c77Sek } 170755434c77Sek 1708503ad85cSMatthew Ahrens /* 1709503ad85cSMatthew Ahrens * inputs: 1710503ad85cSMatthew Ahrens * zc_name name of filesystem 1711503ad85cSMatthew Ahrens * zc_obj object to find 1712503ad85cSMatthew Ahrens * 1713503ad85cSMatthew Ahrens * outputs: 1714503ad85cSMatthew Ahrens * zc_value name of object 1715503ad85cSMatthew Ahrens */ 171655434c77Sek static int 171755434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc) 171855434c77Sek { 1719503ad85cSMatthew Ahrens objset_t *os; 172055434c77Sek int error; 172155434c77Sek 1722503ad85cSMatthew Ahrens /* XXX reading from objset not owned */ 1723503ad85cSMatthew Ahrens if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 172455434c77Sek return (error); 1725503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 1726503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1727be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1728503ad85cSMatthew Ahrens } 1729503ad85cSMatthew Ahrens error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 173055434c77Sek sizeof (zc->zc_value)); 1731503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 173255434c77Sek 173355434c77Sek return (error); 173455434c77Sek } 173555434c77Sek 173699d5e173STim Haley /* 173799d5e173STim Haley * inputs: 173899d5e173STim Haley * zc_name name of filesystem 173999d5e173STim Haley * zc_obj object to find 174099d5e173STim Haley * 174199d5e173STim Haley * outputs: 174299d5e173STim Haley * zc_stat stats on object 174399d5e173STim Haley * zc_value path to object 174499d5e173STim Haley */ 174599d5e173STim Haley static int 174699d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc) 174799d5e173STim Haley { 174899d5e173STim Haley objset_t *os; 174999d5e173STim Haley int error; 175099d5e173STim Haley 175199d5e173STim Haley /* XXX reading from objset not owned */ 175299d5e173STim Haley if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 175399d5e173STim Haley return (error); 175499d5e173STim Haley if (dmu_objset_type(os) != DMU_OST_ZFS) { 175599d5e173STim Haley dmu_objset_rele(os, FTAG); 1756be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 175799d5e173STim Haley } 175899d5e173STim Haley error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value, 175999d5e173STim Haley sizeof (zc->zc_value)); 176099d5e173STim Haley dmu_objset_rele(os, FTAG); 176199d5e173STim Haley 176299d5e173STim Haley return (error); 176399d5e173STim Haley } 176499d5e173STim Haley 1765fa9e4066Sahrens static int 1766fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1767fa9e4066Sahrens { 1768fa9e4066Sahrens spa_t *spa; 1769fa9e4066Sahrens int error; 1770e7cbe64fSgw nvlist_t *config, **l2cache, **spares; 1771e7cbe64fSgw uint_t nl2cache = 0, nspares = 0; 1772fa9e4066Sahrens 1773fa9e4066Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1774fa9e4066Sahrens if (error != 0) 1775fa9e4066Sahrens return (error); 1776fa9e4066Sahrens 1777fa94a07fSbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1778478ed9adSEric Taylor zc->zc_iflags, &config); 1779fa94a07fSbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 1780fa94a07fSbrendan &l2cache, &nl2cache); 1781fa94a07fSbrendan 1782e7cbe64fSgw (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 1783e7cbe64fSgw &spares, &nspares); 1784e7cbe64fSgw 1785b1b8ab34Slling /* 1786b1b8ab34Slling * A root pool with concatenated devices is not supported. 1787e7cbe64fSgw * Thus, can not add a device to a root pool. 1788e7cbe64fSgw * 1789e7cbe64fSgw * Intent log device can not be added to a rootpool because 1790e7cbe64fSgw * during mountroot, zil is replayed, a seperated log device 1791e7cbe64fSgw * can not be accessed during the mountroot time. 1792e7cbe64fSgw * 1793e7cbe64fSgw * l2cache and spare devices are ok to be added to a rootpool. 1794b1b8ab34Slling */ 1795b24ab676SJeff Bonwick if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) { 17961195e687SMark J Musante nvlist_free(config); 1797b1b8ab34Slling spa_close(spa, FTAG); 1798be6fd75aSMatthew Ahrens return (SET_ERROR(EDOM)); 1799b1b8ab34Slling } 1800b1b8ab34Slling 1801fa94a07fSbrendan if (error == 0) { 1802fa9e4066Sahrens error = spa_vdev_add(spa, config); 1803fa9e4066Sahrens nvlist_free(config); 1804fa9e4066Sahrens } 1805fa9e4066Sahrens spa_close(spa, FTAG); 1806fa9e4066Sahrens return (error); 1807fa9e4066Sahrens } 1808fa9e4066Sahrens 18093f9d6ad7SLin Ling /* 18103f9d6ad7SLin Ling * inputs: 18113f9d6ad7SLin Ling * zc_name name of the pool 18123f9d6ad7SLin Ling * zc_nvlist_conf nvlist of devices to remove 18133f9d6ad7SLin Ling * zc_cookie to stop the remove? 18143f9d6ad7SLin Ling */ 1815fa9e4066Sahrens static int 1816fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1817fa9e4066Sahrens { 181899653d4eSeschrock spa_t *spa; 181999653d4eSeschrock int error; 182099653d4eSeschrock 182199653d4eSeschrock error = spa_open(zc->zc_name, &spa, FTAG); 182299653d4eSeschrock if (error != 0) 182399653d4eSeschrock return (error); 182499653d4eSeschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 182599653d4eSeschrock spa_close(spa, FTAG); 182699653d4eSeschrock return (error); 1827fa9e4066Sahrens } 1828fa9e4066Sahrens 1829fa9e4066Sahrens static int 18303d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1831fa9e4066Sahrens { 1832fa9e4066Sahrens spa_t *spa; 1833fa9e4066Sahrens int error; 18343d7072f8Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1835fa9e4066Sahrens 183606eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1837fa9e4066Sahrens return (error); 18383d7072f8Seschrock switch (zc->zc_cookie) { 18393d7072f8Seschrock case VDEV_STATE_ONLINE: 18403d7072f8Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 18413d7072f8Seschrock break; 1842fa9e4066Sahrens 18433d7072f8Seschrock case VDEV_STATE_OFFLINE: 18443d7072f8Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 18453d7072f8Seschrock break; 1846fa9e4066Sahrens 18473d7072f8Seschrock case VDEV_STATE_FAULTED: 1848069f55e2SEric Schrock if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1849069f55e2SEric Schrock zc->zc_obj != VDEV_AUX_EXTERNAL) 1850069f55e2SEric Schrock zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1851069f55e2SEric Schrock 1852069f55e2SEric Schrock error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 18533d7072f8Seschrock break; 18543d7072f8Seschrock 18553d7072f8Seschrock case VDEV_STATE_DEGRADED: 1856069f55e2SEric Schrock if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1857069f55e2SEric Schrock zc->zc_obj != VDEV_AUX_EXTERNAL) 1858069f55e2SEric Schrock zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1859069f55e2SEric Schrock 1860069f55e2SEric Schrock error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 18613d7072f8Seschrock break; 18623d7072f8Seschrock 18633d7072f8Seschrock default: 1864be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 18653d7072f8Seschrock } 18663d7072f8Seschrock zc->zc_cookie = newstate; 1867fa9e4066Sahrens spa_close(spa, FTAG); 1868fa9e4066Sahrens return (error); 1869fa9e4066Sahrens } 1870fa9e4066Sahrens 1871fa9e4066Sahrens static int 1872fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1873fa9e4066Sahrens { 1874fa9e4066Sahrens spa_t *spa; 1875fa9e4066Sahrens int replacing = zc->zc_cookie; 1876fa9e4066Sahrens nvlist_t *config; 1877fa9e4066Sahrens int error; 1878fa9e4066Sahrens 187906eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1880fa9e4066Sahrens return (error); 1881fa9e4066Sahrens 1882990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1883478ed9adSEric Taylor zc->zc_iflags, &config)) == 0) { 1884ea8dc4b6Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1885fa9e4066Sahrens nvlist_free(config); 1886fa9e4066Sahrens } 1887fa9e4066Sahrens 1888fa9e4066Sahrens spa_close(spa, FTAG); 1889fa9e4066Sahrens return (error); 1890fa9e4066Sahrens } 1891fa9e4066Sahrens 1892fa9e4066Sahrens static int 1893fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1894fa9e4066Sahrens { 1895fa9e4066Sahrens spa_t *spa; 1896fa9e4066Sahrens int error; 1897fa9e4066Sahrens 189806eeb2adSek if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1899fa9e4066Sahrens return (error); 1900fa9e4066Sahrens 19018ad4d6ddSJeff Bonwick error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1902fa9e4066Sahrens 1903fa9e4066Sahrens spa_close(spa, FTAG); 1904fa9e4066Sahrens return (error); 1905fa9e4066Sahrens } 1906fa9e4066Sahrens 19071195e687SMark J Musante static int 19081195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc) 19091195e687SMark J Musante { 19101195e687SMark J Musante spa_t *spa; 19111195e687SMark J Musante nvlist_t *config, *props = NULL; 19121195e687SMark J Musante int error; 19131195e687SMark J Musante boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); 19141195e687SMark J Musante 19151195e687SMark J Musante if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 19161195e687SMark J Musante return (error); 19171195e687SMark J Musante 19181195e687SMark J Musante if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 19191195e687SMark J Musante zc->zc_iflags, &config)) { 19201195e687SMark J Musante spa_close(spa, FTAG); 19211195e687SMark J Musante return (error); 19221195e687SMark J Musante } 19231195e687SMark J Musante 19241195e687SMark J Musante if (zc->zc_nvlist_src_size != 0 && (error = 19251195e687SMark J Musante get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 19261195e687SMark J Musante zc->zc_iflags, &props))) { 19271195e687SMark J Musante spa_close(spa, FTAG); 19281195e687SMark J Musante nvlist_free(config); 19291195e687SMark J Musante return (error); 19301195e687SMark J Musante } 19311195e687SMark J Musante 19321195e687SMark J Musante error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); 19331195e687SMark J Musante 19341195e687SMark J Musante spa_close(spa, FTAG); 19351195e687SMark J Musante 19361195e687SMark J Musante nvlist_free(config); 19371195e687SMark J Musante nvlist_free(props); 19381195e687SMark J Musante 19391195e687SMark J Musante return (error); 19401195e687SMark J Musante } 19411195e687SMark J Musante 1942c67d9675Seschrock static int 1943c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 1944c67d9675Seschrock { 1945c67d9675Seschrock spa_t *spa; 1946e9dbad6fSeschrock char *path = zc->zc_value; 1947ea8dc4b6Seschrock uint64_t guid = zc->zc_guid; 1948c67d9675Seschrock int error; 1949c67d9675Seschrock 1950c67d9675Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 1951c67d9675Seschrock if (error != 0) 1952c67d9675Seschrock return (error); 1953c67d9675Seschrock 1954c67d9675Seschrock error = spa_vdev_setpath(spa, guid, path); 1955c67d9675Seschrock spa_close(spa, FTAG); 1956c67d9675Seschrock return (error); 1957c67d9675Seschrock } 1958c67d9675Seschrock 19596809eb4eSEric Schrock static int 19606809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 19616809eb4eSEric Schrock { 19626809eb4eSEric Schrock spa_t *spa; 19636809eb4eSEric Schrock char *fru = zc->zc_value; 19646809eb4eSEric Schrock uint64_t guid = zc->zc_guid; 19656809eb4eSEric Schrock int error; 19666809eb4eSEric Schrock 19676809eb4eSEric Schrock error = spa_open(zc->zc_name, &spa, FTAG); 19686809eb4eSEric Schrock if (error != 0) 19696809eb4eSEric Schrock return (error); 19706809eb4eSEric Schrock 19716809eb4eSEric Schrock error = spa_vdev_setfru(spa, guid, fru); 19726809eb4eSEric Schrock spa_close(spa, FTAG); 19736809eb4eSEric Schrock return (error); 19746809eb4eSEric Schrock } 19756809eb4eSEric Schrock 1976fa9e4066Sahrens static int 1977a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os) 1978fa9e4066Sahrens { 1979a7f53a56SChris Kirby int error = 0; 19807f7322feSeschrock nvlist_t *nv; 1981fa9e4066Sahrens 1982a2eea2e1Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1983fa9e4066Sahrens 19845ad82045Snd if (zc->zc_nvlist_dst != 0 && 198592241e0bSTom Erickson (error = dsl_prop_get_all(os, &nv)) == 0) { 1986a2eea2e1Sahrens dmu_objset_stats(os, nv); 1987432f72fdSahrens /* 1988bd00f61bSrm * NB: zvol_get_stats() will read the objset contents, 1989432f72fdSahrens * which we aren't supposed to do with a 1990745cd3c5Smaybee * DS_MODE_USER hold, because it could be 1991432f72fdSahrens * inconsistent. So this is a bit of a workaround... 1992503ad85cSMatthew Ahrens * XXX reading with out owning 1993432f72fdSahrens */ 199419b94df9SMatthew Ahrens if (!zc->zc_objset_stats.dds_inconsistent && 199519b94df9SMatthew Ahrens dmu_objset_type(os) == DMU_OST_ZVOL) { 199619b94df9SMatthew Ahrens error = zvol_get_stats(os, nv); 199719b94df9SMatthew Ahrens if (error == EIO) 199819b94df9SMatthew Ahrens return (error); 1999fb09f5aaSMadhav Suresh VERIFY0(error); 2000e7437265Sahrens } 2001e9dbad6fSeschrock error = put_nvlist(zc, nv); 20027f7322feSeschrock nvlist_free(nv); 20037f7322feSeschrock } 2004fa9e4066Sahrens 2005a7f53a56SChris Kirby return (error); 2006a7f53a56SChris Kirby } 2007a7f53a56SChris Kirby 2008a7f53a56SChris Kirby /* 2009a7f53a56SChris Kirby * inputs: 2010a7f53a56SChris Kirby * zc_name name of filesystem 2011a7f53a56SChris Kirby * zc_nvlist_dst_size size of buffer for property nvlist 2012a7f53a56SChris Kirby * 2013a7f53a56SChris Kirby * outputs: 2014a7f53a56SChris Kirby * zc_objset_stats stats 2015a7f53a56SChris Kirby * zc_nvlist_dst property nvlist 2016a7f53a56SChris Kirby * zc_nvlist_dst_size size of property nvlist 2017a7f53a56SChris Kirby */ 2018a7f53a56SChris Kirby static int 2019a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc) 2020a7f53a56SChris Kirby { 20213b2aab18SMatthew Ahrens objset_t *os; 2022a7f53a56SChris Kirby int error; 2023a7f53a56SChris Kirby 20243b2aab18SMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os); 20253b2aab18SMatthew Ahrens if (error == 0) { 20263b2aab18SMatthew Ahrens error = zfs_ioc_objset_stats_impl(zc, os); 20273b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 20283b2aab18SMatthew Ahrens } 2029a7f53a56SChris Kirby 2030fa9e4066Sahrens return (error); 2031fa9e4066Sahrens } 2032fa9e4066Sahrens 203392241e0bSTom Erickson /* 203492241e0bSTom Erickson * inputs: 203592241e0bSTom Erickson * zc_name name of filesystem 203692241e0bSTom Erickson * zc_nvlist_dst_size size of buffer for property nvlist 203792241e0bSTom Erickson * 203892241e0bSTom Erickson * outputs: 203992241e0bSTom Erickson * zc_nvlist_dst received property nvlist 204092241e0bSTom Erickson * zc_nvlist_dst_size size of received property nvlist 204192241e0bSTom Erickson * 204292241e0bSTom Erickson * Gets received properties (distinct from local properties on or after 204392241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 204492241e0bSTom Erickson * local property values. 204592241e0bSTom Erickson */ 204692241e0bSTom Erickson static int 204792241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 204892241e0bSTom Erickson { 20493b2aab18SMatthew Ahrens int error = 0; 205092241e0bSTom Erickson nvlist_t *nv; 205192241e0bSTom Erickson 205292241e0bSTom Erickson /* 205392241e0bSTom Erickson * Without this check, we would return local property values if the 205492241e0bSTom Erickson * caller has not already received properties on or after 205592241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS. 205692241e0bSTom Erickson */ 20573b2aab18SMatthew Ahrens if (!dsl_prop_get_hasrecvd(zc->zc_name)) 2058be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 205992241e0bSTom Erickson 206092241e0bSTom Erickson if (zc->zc_nvlist_dst != 0 && 20613b2aab18SMatthew Ahrens (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) { 206292241e0bSTom Erickson error = put_nvlist(zc, nv); 206392241e0bSTom Erickson nvlist_free(nv); 206492241e0bSTom Erickson } 206592241e0bSTom Erickson 206692241e0bSTom Erickson return (error); 206792241e0bSTom Erickson } 206892241e0bSTom Erickson 2069de8267e0Stimh static int 2070de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 2071de8267e0Stimh { 2072de8267e0Stimh uint64_t value; 2073de8267e0Stimh int error; 2074de8267e0Stimh 2075de8267e0Stimh /* 2076de8267e0Stimh * zfs_get_zplprop() will either find a value or give us 2077de8267e0Stimh * the default value (if there is one). 2078de8267e0Stimh */ 2079de8267e0Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 2080de8267e0Stimh return (error); 2081de8267e0Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 2082de8267e0Stimh return (0); 2083de8267e0Stimh } 2084de8267e0Stimh 20853cb34c60Sahrens /* 20863cb34c60Sahrens * inputs: 20873cb34c60Sahrens * zc_name name of filesystem 2088de8267e0Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 20893cb34c60Sahrens * 20903cb34c60Sahrens * outputs: 2091de8267e0Stimh * zc_nvlist_dst zpl property nvlist 2092de8267e0Stimh * zc_nvlist_dst_size size of zpl property nvlist 20933cb34c60Sahrens */ 2094bd00f61bSrm static int 2095de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 2096bd00f61bSrm { 2097de8267e0Stimh objset_t *os; 2098de8267e0Stimh int err; 2099bd00f61bSrm 2100503ad85cSMatthew Ahrens /* XXX reading without owning */ 2101503ad85cSMatthew Ahrens if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 2102de8267e0Stimh return (err); 2103bd00f61bSrm 2104bd00f61bSrm dmu_objset_fast_stat(os, &zc->zc_objset_stats); 2105bd00f61bSrm 2106bd00f61bSrm /* 2107de8267e0Stimh * NB: nvl_add_zplprop() will read the objset contents, 2108745cd3c5Smaybee * which we aren't supposed to do with a DS_MODE_USER 2109745cd3c5Smaybee * hold, because it could be inconsistent. 2110bd00f61bSrm */ 2111de8267e0Stimh if (zc->zc_nvlist_dst != NULL && 2112de8267e0Stimh !zc->zc_objset_stats.dds_inconsistent && 2113de8267e0Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 2114de8267e0Stimh nvlist_t *nv; 2115de8267e0Stimh 2116de8267e0Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2117de8267e0Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 2118de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 2119de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 2120de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 2121de8267e0Stimh err = put_nvlist(zc, nv); 2122de8267e0Stimh nvlist_free(nv); 2123de8267e0Stimh } else { 2124be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 2125de8267e0Stimh } 2126503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2127de8267e0Stimh return (err); 2128bd00f61bSrm } 2129bd00f61bSrm 213014843421SMatthew Ahrens static boolean_t 213114843421SMatthew Ahrens dataset_name_hidden(const char *name) 213214843421SMatthew Ahrens { 213314843421SMatthew Ahrens /* 213414843421SMatthew Ahrens * Skip over datasets that are not visible in this zone, 213514843421SMatthew Ahrens * internal datasets (which have a $ in their name), and 213614843421SMatthew Ahrens * temporary datasets (which have a % in their name). 213714843421SMatthew Ahrens */ 213814843421SMatthew Ahrens if (strchr(name, '$') != NULL) 213914843421SMatthew Ahrens return (B_TRUE); 214014843421SMatthew Ahrens if (strchr(name, '%') != NULL) 214114843421SMatthew Ahrens return (B_TRUE); 214214843421SMatthew Ahrens if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) 214314843421SMatthew Ahrens return (B_TRUE); 214414843421SMatthew Ahrens return (B_FALSE); 214514843421SMatthew Ahrens } 214614843421SMatthew Ahrens 2147de8267e0Stimh /* 2148de8267e0Stimh * inputs: 2149de8267e0Stimh * zc_name name of filesystem 2150de8267e0Stimh * zc_cookie zap cursor 2151de8267e0Stimh * zc_nvlist_dst_size size of buffer for property nvlist 2152de8267e0Stimh * 2153de8267e0Stimh * outputs: 2154de8267e0Stimh * zc_name name of next filesystem 215514843421SMatthew Ahrens * zc_cookie zap cursor 2156de8267e0Stimh * zc_objset_stats stats 2157de8267e0Stimh * zc_nvlist_dst property nvlist 2158de8267e0Stimh * zc_nvlist_dst_size size of property nvlist 2159de8267e0Stimh */ 2160fa9e4066Sahrens static int 2161fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 2162fa9e4066Sahrens { 216387e5029aSahrens objset_t *os; 2164fa9e4066Sahrens int error; 2165fa9e4066Sahrens char *p; 2166620252bcSChris Kirby size_t orig_len = strlen(zc->zc_name); 2167fa9e4066Sahrens 2168620252bcSChris Kirby top: 2169503ad85cSMatthew Ahrens if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 217087e5029aSahrens if (error == ENOENT) 2171be6fd75aSMatthew Ahrens error = SET_ERROR(ESRCH); 217287e5029aSahrens return (error); 2173fa9e4066Sahrens } 2174fa9e4066Sahrens 2175fa9e4066Sahrens p = strrchr(zc->zc_name, '/'); 2176fa9e4066Sahrens if (p == NULL || p[1] != '\0') 2177fa9e4066Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 2178fa9e4066Sahrens p = zc->zc_name + strlen(zc->zc_name); 2179fa9e4066Sahrens 2180fa9e4066Sahrens do { 218187e5029aSahrens error = dmu_dir_list_next(os, 218287e5029aSahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 218387e5029aSahrens NULL, &zc->zc_cookie); 2184fa9e4066Sahrens if (error == ENOENT) 2185be6fd75aSMatthew Ahrens error = SET_ERROR(ESRCH); 218619b94df9SMatthew Ahrens } while (error == 0 && dataset_name_hidden(zc->zc_name)); 2187503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2188fa9e4066Sahrens 2189681d9761SEric Taylor /* 2190681d9761SEric Taylor * If it's an internal dataset (ie. with a '$' in its name), 2191681d9761SEric Taylor * don't try to get stats for it, otherwise we'll return ENOENT. 2192681d9761SEric Taylor */ 2193620252bcSChris Kirby if (error == 0 && strchr(zc->zc_name, '$') == NULL) { 219487e5029aSahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 2195620252bcSChris Kirby if (error == ENOENT) { 2196620252bcSChris Kirby /* We lost a race with destroy, get the next one. */ 2197620252bcSChris Kirby zc->zc_name[orig_len] = '\0'; 2198620252bcSChris Kirby goto top; 2199620252bcSChris Kirby } 2200620252bcSChris Kirby } 2201fa9e4066Sahrens return (error); 2202fa9e4066Sahrens } 2203fa9e4066Sahrens 22043cb34c60Sahrens /* 22053cb34c60Sahrens * inputs: 22063cb34c60Sahrens * zc_name name of filesystem 22073cb34c60Sahrens * zc_cookie zap cursor 22083cb34c60Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 22093cb34c60Sahrens * 22103cb34c60Sahrens * outputs: 22113cb34c60Sahrens * zc_name name of next snapshot 22123cb34c60Sahrens * zc_objset_stats stats 22133cb34c60Sahrens * zc_nvlist_dst property nvlist 22143cb34c60Sahrens * zc_nvlist_dst_size size of property nvlist 22153cb34c60Sahrens */ 2216fa9e4066Sahrens static int 2217fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 2218fa9e4066Sahrens { 221987e5029aSahrens objset_t *os; 2220fa9e4066Sahrens int error; 2221fa9e4066Sahrens 2222503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os); 22233b2aab18SMatthew Ahrens if (error != 0) { 2224745cd3c5Smaybee return (error == ENOENT ? ESRCH : error); 22253b2aab18SMatthew Ahrens } 2226fa9e4066Sahrens 2227b81d61a6Slling /* 2228b81d61a6Slling * A dataset name of maximum length cannot have any snapshots, 2229b81d61a6Slling * so exit immediately. 2230b81d61a6Slling */ 2231b81d61a6Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 2232503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 2233be6fd75aSMatthew Ahrens return (SET_ERROR(ESRCH)); 2234fa9e4066Sahrens } 2235fa9e4066Sahrens 223687e5029aSahrens error = dmu_snapshot_list_next(os, 223787e5029aSahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 2238a7f53a56SChris Kirby zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie, 2239a7f53a56SChris Kirby NULL); 2240a7f53a56SChris Kirby 2241620252bcSChris Kirby if (error == 0) { 2242a7f53a56SChris Kirby dsl_dataset_t *ds; 2243a7f53a56SChris Kirby dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool; 2244a7f53a56SChris Kirby 2245a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds); 22463b2aab18SMatthew Ahrens if (error == 0) { 2247a7f53a56SChris Kirby objset_t *ossnap; 2248a7f53a56SChris Kirby 2249a7f53a56SChris Kirby error = dmu_objset_from_ds(ds, &ossnap); 2250a7f53a56SChris Kirby if (error == 0) 2251a7f53a56SChris Kirby error = zfs_ioc_objset_stats_impl(zc, ossnap); 2252a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG); 2253620252bcSChris Kirby } 2254620252bcSChris Kirby } else if (error == ENOENT) { 2255be6fd75aSMatthew Ahrens error = SET_ERROR(ESRCH); 2256620252bcSChris Kirby } 2257fa9e4066Sahrens 2258a7f53a56SChris Kirby dmu_objset_rele(os, FTAG); 22593cb34c60Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 22603b2aab18SMatthew Ahrens if (error != 0) 22613cb34c60Sahrens *strchr(zc->zc_name, '@') = '\0'; 2262fa9e4066Sahrens return (error); 2263fa9e4066Sahrens } 2264fa9e4066Sahrens 226592241e0bSTom Erickson static int 226692241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 2267fa9e4066Sahrens { 226892241e0bSTom Erickson const char *propname = nvpair_name(pair); 226992241e0bSTom Erickson uint64_t *valary; 227092241e0bSTom Erickson unsigned int vallen; 227192241e0bSTom Erickson const char *domain; 2272eeb85002STim Haley char *dash; 227392241e0bSTom Erickson zfs_userquota_prop_t type; 227492241e0bSTom Erickson uint64_t rid; 227592241e0bSTom Erickson uint64_t quota; 227692241e0bSTom Erickson zfsvfs_t *zfsvfs; 227792241e0bSTom Erickson int err; 227892241e0bSTom Erickson 227992241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 228092241e0bSTom Erickson nvlist_t *attrs; 228192241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2282eeb85002STim Haley if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2283eeb85002STim Haley &pair) != 0) 2284be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 228592241e0bSTom Erickson } 2286e9dbad6fSeschrock 2287ecd6cf80Smarks /* 2288eeb85002STim Haley * A correctly constructed propname is encoded as 228992241e0bSTom Erickson * userquota@<rid>-<domain>. 2290ecd6cf80Smarks */ 2291eeb85002STim Haley if ((dash = strchr(propname, '-')) == NULL || 2292eeb85002STim Haley nvpair_value_uint64_array(pair, &valary, &vallen) != 0 || 2293eeb85002STim Haley vallen != 3) 2294be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2295eeb85002STim Haley 2296eeb85002STim Haley domain = dash + 1; 2297eeb85002STim Haley type = valary[0]; 2298eeb85002STim Haley rid = valary[1]; 2299eeb85002STim Haley quota = valary[2]; 2300e9dbad6fSeschrock 23011412a1a2SMark Shellenbaum err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE); 230292241e0bSTom Erickson if (err == 0) { 230392241e0bSTom Erickson err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 230492241e0bSTom Erickson zfsvfs_rele(zfsvfs, FTAG); 230592241e0bSTom Erickson } 2306e9dbad6fSeschrock 230792241e0bSTom Erickson return (err); 230892241e0bSTom Erickson } 230914843421SMatthew Ahrens 231092241e0bSTom Erickson /* 231192241e0bSTom Erickson * If the named property is one that has a special function to set its value, 231292241e0bSTom Erickson * return 0 on success and a positive error code on failure; otherwise if it is 231392241e0bSTom Erickson * not one of the special properties handled by this function, return -1. 231492241e0bSTom Erickson * 2315eeb85002STim Haley * XXX: It would be better for callers of the property interface if we handled 231692241e0bSTom Erickson * these special cases in dsl_prop.c (in the dsl layer). 231792241e0bSTom Erickson */ 231892241e0bSTom Erickson static int 231992241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source, 232092241e0bSTom Erickson nvpair_t *pair) 232192241e0bSTom Erickson { 232292241e0bSTom Erickson const char *propname = nvpair_name(pair); 232392241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 232492241e0bSTom Erickson uint64_t intval; 232592241e0bSTom Erickson int err; 2326fa9e4066Sahrens 232792241e0bSTom Erickson if (prop == ZPROP_INVAL) { 232892241e0bSTom Erickson if (zfs_prop_userquota(propname)) 232992241e0bSTom Erickson return (zfs_prop_set_userquota(dsname, pair)); 233092241e0bSTom Erickson return (-1); 233192241e0bSTom Erickson } 233214843421SMatthew Ahrens 233392241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 233492241e0bSTom Erickson nvlist_t *attrs; 233592241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 233692241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 233792241e0bSTom Erickson &pair) == 0); 233892241e0bSTom Erickson } 2339db870a07Sahrens 234092241e0bSTom Erickson if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) 234192241e0bSTom Erickson return (-1); 2342b24ab676SJeff Bonwick 234392241e0bSTom Erickson VERIFY(0 == nvpair_value_uint64(pair, &intval)); 234440feaa91Sahrens 234592241e0bSTom Erickson switch (prop) { 234692241e0bSTom Erickson case ZFS_PROP_QUOTA: 234792241e0bSTom Erickson err = dsl_dir_set_quota(dsname, source, intval); 234892241e0bSTom Erickson break; 234992241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 23503b2aab18SMatthew Ahrens err = dsl_dataset_set_refquota(dsname, source, intval); 235192241e0bSTom Erickson break; 235292241e0bSTom Erickson case ZFS_PROP_RESERVATION: 235392241e0bSTom Erickson err = dsl_dir_set_reservation(dsname, source, intval); 235492241e0bSTom Erickson break; 235592241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 23563b2aab18SMatthew Ahrens err = dsl_dataset_set_refreservation(dsname, source, intval); 235792241e0bSTom Erickson break; 235892241e0bSTom Erickson case ZFS_PROP_VOLSIZE: 2359c61ea566SGeorge Wilson err = zvol_set_volsize(dsname, intval); 236092241e0bSTom Erickson break; 236192241e0bSTom Erickson case ZFS_PROP_VERSION: 236292241e0bSTom Erickson { 236392241e0bSTom Erickson zfsvfs_t *zfsvfs; 23649e6eda55Smarks 23651412a1a2SMark Shellenbaum if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0) 2366b24ab676SJeff Bonwick break; 2367b24ab676SJeff Bonwick 236892241e0bSTom Erickson err = zfs_set_version(zfsvfs, intval); 236992241e0bSTom Erickson zfsvfs_rele(zfsvfs, FTAG); 2370d0f3f37eSMark Shellenbaum 237192241e0bSTom Erickson if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 2372b16da2e2SGeorge Wilson zfs_cmd_t *zc; 2373b16da2e2SGeorge Wilson 2374b16da2e2SGeorge Wilson zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2375b16da2e2SGeorge Wilson (void) strcpy(zc->zc_name, dsname); 2376b16da2e2SGeorge Wilson (void) zfs_ioc_userspace_upgrade(zc); 2377b16da2e2SGeorge Wilson kmem_free(zc, sizeof (zfs_cmd_t)); 237840feaa91Sahrens } 237992241e0bSTom Erickson break; 2380ecd6cf80Smarks } 2381a6f561b4SSašo Kiselkov case ZFS_PROP_COMPRESSION: 2382a6f561b4SSašo Kiselkov { 2383a6f561b4SSašo Kiselkov if (intval == ZIO_COMPRESS_LZ4) { 2384a6f561b4SSašo Kiselkov zfeature_info_t *feature = 2385a6f561b4SSašo Kiselkov &spa_feature_table[SPA_FEATURE_LZ4_COMPRESS]; 2386a6f561b4SSašo Kiselkov spa_t *spa; 2387a6f561b4SSašo Kiselkov 2388a6f561b4SSašo Kiselkov if ((err = spa_open(dsname, &spa, FTAG)) != 0) 2389a6f561b4SSašo Kiselkov return (err); 2390a6f561b4SSašo Kiselkov 2391a6f561b4SSašo Kiselkov /* 2392a6f561b4SSašo Kiselkov * Setting the LZ4 compression algorithm activates 2393a6f561b4SSašo Kiselkov * the feature. 2394a6f561b4SSašo Kiselkov */ 2395a6f561b4SSašo Kiselkov if (!spa_feature_is_active(spa, feature)) { 23963b2aab18SMatthew Ahrens if ((err = zfs_prop_activate_feature(spa, 2397a6f561b4SSašo Kiselkov feature)) != 0) { 2398a6f561b4SSašo Kiselkov spa_close(spa, FTAG); 2399a6f561b4SSašo Kiselkov return (err); 2400a6f561b4SSašo Kiselkov } 2401a6f561b4SSašo Kiselkov } 2402a6f561b4SSašo Kiselkov 2403a6f561b4SSašo Kiselkov spa_close(spa, FTAG); 2404a6f561b4SSašo Kiselkov } 2405a6f561b4SSašo Kiselkov /* 2406a6f561b4SSašo Kiselkov * We still want the default set action to be performed in the 2407a6f561b4SSašo Kiselkov * caller, we only performed zfeature settings here. 2408a6f561b4SSašo Kiselkov */ 2409a6f561b4SSašo Kiselkov err = -1; 2410a6f561b4SSašo Kiselkov break; 2411a6f561b4SSašo Kiselkov } 2412ecd6cf80Smarks 241392241e0bSTom Erickson default: 241492241e0bSTom Erickson err = -1; 241592241e0bSTom Erickson } 2416e9dbad6fSeschrock 241792241e0bSTom Erickson return (err); 241892241e0bSTom Erickson } 2419a9799022Sck 242092241e0bSTom Erickson /* 242192241e0bSTom Erickson * This function is best effort. If it fails to set any of the given properties, 24224445fffbSMatthew Ahrens * it continues to set as many as it can and returns the last error 24234445fffbSMatthew Ahrens * encountered. If the caller provides a non-NULL errlist, it will be filled in 24244445fffbSMatthew Ahrens * with the list of names of all the properties that failed along with the 24254445fffbSMatthew Ahrens * corresponding error numbers. 242692241e0bSTom Erickson * 24274445fffbSMatthew Ahrens * If every property is set successfully, zero is returned and errlist is not 24284445fffbSMatthew Ahrens * modified. 242992241e0bSTom Erickson */ 243092241e0bSTom Erickson int 243192241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 24324445fffbSMatthew Ahrens nvlist_t *errlist) 243392241e0bSTom Erickson { 243492241e0bSTom Erickson nvpair_t *pair; 243592241e0bSTom Erickson nvpair_t *propval; 243602e383d1STom Erickson int rv = 0; 243792241e0bSTom Erickson uint64_t intval; 243892241e0bSTom Erickson char *strval; 24394445fffbSMatthew Ahrens nvlist_t *genericnvl = fnvlist_alloc(); 24404445fffbSMatthew Ahrens nvlist_t *retrynvl = fnvlist_alloc(); 2441a9799022Sck 244292241e0bSTom Erickson retry: 244392241e0bSTom Erickson pair = NULL; 244492241e0bSTom Erickson while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 244592241e0bSTom Erickson const char *propname = nvpair_name(pair); 244692241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 2447cfa69fd2STom Erickson int err = 0; 2448e9dbad6fSeschrock 244992241e0bSTom Erickson /* decode the property value */ 245092241e0bSTom Erickson propval = pair; 245192241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 245292241e0bSTom Erickson nvlist_t *attrs; 24534445fffbSMatthew Ahrens attrs = fnvpair_value_nvlist(pair); 2454eeb85002STim Haley if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2455eeb85002STim Haley &propval) != 0) 2456be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 245714843421SMatthew Ahrens } 2458e9dbad6fSeschrock 245992241e0bSTom Erickson /* Validate value type */ 2460eeb85002STim Haley if (err == 0 && prop == ZPROP_INVAL) { 246192241e0bSTom Erickson if (zfs_prop_user(propname)) { 246292241e0bSTom Erickson if (nvpair_type(propval) != DATA_TYPE_STRING) 2463be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 246492241e0bSTom Erickson } else if (zfs_prop_userquota(propname)) { 246592241e0bSTom Erickson if (nvpair_type(propval) != 246692241e0bSTom Erickson DATA_TYPE_UINT64_ARRAY) 2467be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 246819b94df9SMatthew Ahrens } else { 2469be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 24704201a95eSRic Aleshire } 2471eeb85002STim Haley } else if (err == 0) { 247292241e0bSTom Erickson if (nvpair_type(propval) == DATA_TYPE_STRING) { 247392241e0bSTom Erickson if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 2474be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 247592241e0bSTom Erickson } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 2476a2eea2e1Sahrens const char *unused; 2477a2eea2e1Sahrens 24784445fffbSMatthew Ahrens intval = fnvpair_value_uint64(propval); 2479e9dbad6fSeschrock 2480e9dbad6fSeschrock switch (zfs_prop_get_type(prop)) { 248191ebeef5Sahrens case PROP_TYPE_NUMBER: 2482e9dbad6fSeschrock break; 248391ebeef5Sahrens case PROP_TYPE_STRING: 2484be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 248592241e0bSTom Erickson break; 248691ebeef5Sahrens case PROP_TYPE_INDEX: 2487acd76fe5Seschrock if (zfs_prop_index_to_string(prop, 248892241e0bSTom Erickson intval, &unused) != 0) 2489be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 2490e9dbad6fSeschrock break; 2491e9dbad6fSeschrock default: 2492e7437265Sahrens cmn_err(CE_PANIC, 2493e7437265Sahrens "unknown property type"); 2494e9dbad6fSeschrock } 2495e9dbad6fSeschrock } else { 2496be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 2497e9dbad6fSeschrock } 2498e9dbad6fSeschrock } 249992241e0bSTom Erickson 250092241e0bSTom Erickson /* Validate permissions */ 250192241e0bSTom Erickson if (err == 0) 250292241e0bSTom Erickson err = zfs_check_settable(dsname, pair, CRED()); 250392241e0bSTom Erickson 250492241e0bSTom Erickson if (err == 0) { 250592241e0bSTom Erickson err = zfs_prop_set_special(dsname, source, pair); 250692241e0bSTom Erickson if (err == -1) { 250792241e0bSTom Erickson /* 250892241e0bSTom Erickson * For better performance we build up a list of 250992241e0bSTom Erickson * properties to set in a single transaction. 251092241e0bSTom Erickson */ 251192241e0bSTom Erickson err = nvlist_add_nvpair(genericnvl, pair); 251292241e0bSTom Erickson } else if (err != 0 && nvl != retrynvl) { 251392241e0bSTom Erickson /* 251492241e0bSTom Erickson * This may be a spurious error caused by 251592241e0bSTom Erickson * receiving quota and reservation out of order. 251692241e0bSTom Erickson * Try again in a second pass. 251792241e0bSTom Erickson */ 251892241e0bSTom Erickson err = nvlist_add_nvpair(retrynvl, pair); 251992241e0bSTom Erickson } 252092241e0bSTom Erickson } 252192241e0bSTom Erickson 25224445fffbSMatthew Ahrens if (err != 0) { 25234445fffbSMatthew Ahrens if (errlist != NULL) 25244445fffbSMatthew Ahrens fnvlist_add_int32(errlist, propname, err); 25254445fffbSMatthew Ahrens rv = err; 25264445fffbSMatthew Ahrens } 2527e9dbad6fSeschrock } 2528e9dbad6fSeschrock 252992241e0bSTom Erickson if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 253092241e0bSTom Erickson nvl = retrynvl; 253192241e0bSTom Erickson goto retry; 253292241e0bSTom Erickson } 253392241e0bSTom Erickson 253492241e0bSTom Erickson if (!nvlist_empty(genericnvl) && 253592241e0bSTom Erickson dsl_props_set(dsname, source, genericnvl) != 0) { 253692241e0bSTom Erickson /* 253792241e0bSTom Erickson * If this fails, we still want to set as many properties as we 253892241e0bSTom Erickson * can, so try setting them individually. 253992241e0bSTom Erickson */ 254092241e0bSTom Erickson pair = NULL; 254192241e0bSTom Erickson while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 254292241e0bSTom Erickson const char *propname = nvpair_name(pair); 2543cfa69fd2STom Erickson int err = 0; 254492241e0bSTom Erickson 254592241e0bSTom Erickson propval = pair; 254692241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 254792241e0bSTom Erickson nvlist_t *attrs; 25484445fffbSMatthew Ahrens attrs = fnvpair_value_nvlist(pair); 25494445fffbSMatthew Ahrens propval = fnvlist_lookup_nvpair(attrs, 25504445fffbSMatthew Ahrens ZPROP_VALUE); 255192241e0bSTom Erickson } 255292241e0bSTom Erickson 255392241e0bSTom Erickson if (nvpair_type(propval) == DATA_TYPE_STRING) { 25544445fffbSMatthew Ahrens strval = fnvpair_value_string(propval); 25553b2aab18SMatthew Ahrens err = dsl_prop_set_string(dsname, propname, 25563b2aab18SMatthew Ahrens source, strval); 255792241e0bSTom Erickson } else { 25584445fffbSMatthew Ahrens intval = fnvpair_value_uint64(propval); 25593b2aab18SMatthew Ahrens err = dsl_prop_set_int(dsname, propname, source, 25603b2aab18SMatthew Ahrens intval); 256192241e0bSTom Erickson } 256292241e0bSTom Erickson 256392241e0bSTom Erickson if (err != 0) { 25644445fffbSMatthew Ahrens if (errlist != NULL) { 25654445fffbSMatthew Ahrens fnvlist_add_int32(errlist, propname, 25664445fffbSMatthew Ahrens err); 25674445fffbSMatthew Ahrens } 25684445fffbSMatthew Ahrens rv = err; 256992241e0bSTom Erickson } 257092241e0bSTom Erickson } 25715c0b6a79SRich Morris } 25725c0b6a79SRich Morris nvlist_free(genericnvl); 257392241e0bSTom Erickson nvlist_free(retrynvl); 257492241e0bSTom Erickson 257592241e0bSTom Erickson return (rv); 2576fa9e4066Sahrens } 2577fa9e4066Sahrens 2578ea2f5b9eSMatthew Ahrens /* 2579ea2f5b9eSMatthew Ahrens * Check that all the properties are valid user properties. 2580ea2f5b9eSMatthew Ahrens */ 2581ea2f5b9eSMatthew Ahrens static int 25824445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl) 2583ea2f5b9eSMatthew Ahrens { 258492241e0bSTom Erickson nvpair_t *pair = NULL; 2585ea2f5b9eSMatthew Ahrens int error = 0; 2586ea2f5b9eSMatthew Ahrens 258792241e0bSTom Erickson while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 258892241e0bSTom Erickson const char *propname = nvpair_name(pair); 2589ea2f5b9eSMatthew Ahrens char *valstr; 2590ea2f5b9eSMatthew Ahrens 2591ea2f5b9eSMatthew Ahrens if (!zfs_prop_user(propname) || 259292241e0bSTom Erickson nvpair_type(pair) != DATA_TYPE_STRING) 2593be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2594ea2f5b9eSMatthew Ahrens 2595ea2f5b9eSMatthew Ahrens if (error = zfs_secpolicy_write_perms(fsname, 2596ea2f5b9eSMatthew Ahrens ZFS_DELEG_PERM_USERPROP, CRED())) 2597ea2f5b9eSMatthew Ahrens return (error); 2598ea2f5b9eSMatthew Ahrens 2599ea2f5b9eSMatthew Ahrens if (strlen(propname) >= ZAP_MAXNAMELEN) 2600be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 2601ea2f5b9eSMatthew Ahrens 260292241e0bSTom Erickson VERIFY(nvpair_value_string(pair, &valstr) == 0); 2603ea2f5b9eSMatthew Ahrens if (strlen(valstr) >= ZAP_MAXVALUELEN) 2604ea2f5b9eSMatthew Ahrens return (E2BIG); 2605ea2f5b9eSMatthew Ahrens } 2606ea2f5b9eSMatthew Ahrens return (0); 2607ea2f5b9eSMatthew Ahrens } 2608ea2f5b9eSMatthew Ahrens 260992241e0bSTom Erickson static void 261092241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 261192241e0bSTom Erickson { 261292241e0bSTom Erickson nvpair_t *pair; 261392241e0bSTom Erickson 261492241e0bSTom Erickson VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 261592241e0bSTom Erickson 261692241e0bSTom Erickson pair = NULL; 261792241e0bSTom Erickson while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 261892241e0bSTom Erickson if (nvlist_exists(skipped, nvpair_name(pair))) 261992241e0bSTom Erickson continue; 262092241e0bSTom Erickson 262192241e0bSTom Erickson VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 262292241e0bSTom Erickson } 262392241e0bSTom Erickson } 262492241e0bSTom Erickson 262592241e0bSTom Erickson static int 26263b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props, 262792241e0bSTom Erickson nvlist_t *skipped) 262892241e0bSTom Erickson { 262992241e0bSTom Erickson int err = 0; 263092241e0bSTom Erickson nvlist_t *cleared_props = NULL; 263192241e0bSTom Erickson props_skip(props, skipped, &cleared_props); 263292241e0bSTom Erickson if (!nvlist_empty(cleared_props)) { 263392241e0bSTom Erickson /* 263492241e0bSTom Erickson * Acts on local properties until the dataset has received 263592241e0bSTom Erickson * properties at least once on or after SPA_VERSION_RECVD_PROPS. 263692241e0bSTom Erickson */ 263792241e0bSTom Erickson zprop_source_t flags = (ZPROP_SRC_NONE | 26383b2aab18SMatthew Ahrens (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0)); 26393b2aab18SMatthew Ahrens err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL); 264092241e0bSTom Erickson } 264192241e0bSTom Erickson nvlist_free(cleared_props); 264292241e0bSTom Erickson return (err); 264392241e0bSTom Erickson } 264492241e0bSTom Erickson 26453cb34c60Sahrens /* 26463cb34c60Sahrens * inputs: 26473cb34c60Sahrens * zc_name name of filesystem 26485c0b6a79SRich Morris * zc_value name of property to set 26493cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 265092241e0bSTom Erickson * zc_cookie received properties flag 26513cb34c60Sahrens * 265292241e0bSTom Erickson * outputs: 265392241e0bSTom Erickson * zc_nvlist_dst{_size} error for each unapplied received property 26543cb34c60Sahrens */ 2655fa9e4066Sahrens static int 2656e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 2657fa9e4066Sahrens { 2658e9dbad6fSeschrock nvlist_t *nvl; 265992241e0bSTom Erickson boolean_t received = zc->zc_cookie; 266092241e0bSTom Erickson zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 266192241e0bSTom Erickson ZPROP_SRC_LOCAL); 26624445fffbSMatthew Ahrens nvlist_t *errors; 2663e9dbad6fSeschrock int error; 2664e9dbad6fSeschrock 2665990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2666478ed9adSEric Taylor zc->zc_iflags, &nvl)) != 0) 2667e9dbad6fSeschrock return (error); 2668e9dbad6fSeschrock 266992241e0bSTom Erickson if (received) { 2670bb0ade09Sahrens nvlist_t *origprops; 2671bb0ade09Sahrens 26723b2aab18SMatthew Ahrens if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) { 26733b2aab18SMatthew Ahrens (void) clear_received_props(zc->zc_name, 26743b2aab18SMatthew Ahrens origprops, nvl); 26753b2aab18SMatthew Ahrens nvlist_free(origprops); 2676bb0ade09Sahrens } 26773b2aab18SMatthew Ahrens 26783b2aab18SMatthew Ahrens error = dsl_prop_set_hasrecvd(zc->zc_name); 2679bb0ade09Sahrens } 2680bb0ade09Sahrens 26814445fffbSMatthew Ahrens errors = fnvlist_alloc(); 26823b2aab18SMatthew Ahrens if (error == 0) 26833b2aab18SMatthew Ahrens error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors); 268492241e0bSTom Erickson 268592241e0bSTom Erickson if (zc->zc_nvlist_dst != NULL && errors != NULL) { 268692241e0bSTom Erickson (void) put_nvlist(zc, errors); 268792241e0bSTom Erickson } 2688ecd6cf80Smarks 268992241e0bSTom Erickson nvlist_free(errors); 2690e9dbad6fSeschrock nvlist_free(nvl); 2691e9dbad6fSeschrock return (error); 2692fa9e4066Sahrens } 2693fa9e4066Sahrens 26943cb34c60Sahrens /* 26953cb34c60Sahrens * inputs: 26963cb34c60Sahrens * zc_name name of filesystem 26973cb34c60Sahrens * zc_value name of property to inherit 269892241e0bSTom Erickson * zc_cookie revert to received value if TRUE 26993cb34c60Sahrens * 27003cb34c60Sahrens * outputs: none 27013cb34c60Sahrens */ 2702e45ce728Sahrens static int 2703e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 2704e45ce728Sahrens { 270592241e0bSTom Erickson const char *propname = zc->zc_value; 270692241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 270792241e0bSTom Erickson boolean_t received = zc->zc_cookie; 270892241e0bSTom Erickson zprop_source_t source = (received 270992241e0bSTom Erickson ? ZPROP_SRC_NONE /* revert to received value, if any */ 271092241e0bSTom Erickson : ZPROP_SRC_INHERITED); /* explicitly inherit */ 271192241e0bSTom Erickson 271292241e0bSTom Erickson if (received) { 271392241e0bSTom Erickson nvlist_t *dummy; 271492241e0bSTom Erickson nvpair_t *pair; 271592241e0bSTom Erickson zprop_type_t type; 271692241e0bSTom Erickson int err; 271792241e0bSTom Erickson 271892241e0bSTom Erickson /* 271992241e0bSTom Erickson * zfs_prop_set_special() expects properties in the form of an 272092241e0bSTom Erickson * nvpair with type info. 272192241e0bSTom Erickson */ 272292241e0bSTom Erickson if (prop == ZPROP_INVAL) { 272392241e0bSTom Erickson if (!zfs_prop_user(propname)) 2724be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 272592241e0bSTom Erickson 272692241e0bSTom Erickson type = PROP_TYPE_STRING; 2727a79992aaSTom Erickson } else if (prop == ZFS_PROP_VOLSIZE || 2728a79992aaSTom Erickson prop == ZFS_PROP_VERSION) { 2729be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 273092241e0bSTom Erickson } else { 273192241e0bSTom Erickson type = zfs_prop_get_type(prop); 273292241e0bSTom Erickson } 273392241e0bSTom Erickson 273492241e0bSTom Erickson VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0); 273592241e0bSTom Erickson 273692241e0bSTom Erickson switch (type) { 273792241e0bSTom Erickson case PROP_TYPE_STRING: 273892241e0bSTom Erickson VERIFY(0 == nvlist_add_string(dummy, propname, "")); 273992241e0bSTom Erickson break; 274092241e0bSTom Erickson case PROP_TYPE_NUMBER: 274192241e0bSTom Erickson case PROP_TYPE_INDEX: 274292241e0bSTom Erickson VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 274392241e0bSTom Erickson break; 274492241e0bSTom Erickson default: 274592241e0bSTom Erickson nvlist_free(dummy); 2746be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 274792241e0bSTom Erickson } 274892241e0bSTom Erickson 274992241e0bSTom Erickson pair = nvlist_next_nvpair(dummy, NULL); 275092241e0bSTom Erickson err = zfs_prop_set_special(zc->zc_name, source, pair); 275192241e0bSTom Erickson nvlist_free(dummy); 275292241e0bSTom Erickson if (err != -1) 275392241e0bSTom Erickson return (err); /* special property already handled */ 275492241e0bSTom Erickson } else { 275592241e0bSTom Erickson /* 275692241e0bSTom Erickson * Only check this in the non-received case. We want to allow 275792241e0bSTom Erickson * 'inherit -S' to revert non-inheritable properties like quota 275892241e0bSTom Erickson * and reservation to the received or default values even though 275992241e0bSTom Erickson * they are not considered inheritable. 276092241e0bSTom Erickson */ 276192241e0bSTom Erickson if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 2762be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 276392241e0bSTom Erickson } 276492241e0bSTom Erickson 27654445fffbSMatthew Ahrens /* property name has been validated by zfs_secpolicy_inherit_prop() */ 27663b2aab18SMatthew Ahrens return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source)); 2767e45ce728Sahrens } 2768e45ce728Sahrens 2769b1b8ab34Slling static int 277011a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 2771b1b8ab34Slling { 2772990b4856Slling nvlist_t *props; 2773b1b8ab34Slling spa_t *spa; 2774990b4856Slling int error; 277592241e0bSTom Erickson nvpair_t *pair; 2776b1b8ab34Slling 277792241e0bSTom Erickson if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 277892241e0bSTom Erickson zc->zc_iflags, &props)) 2779b1b8ab34Slling return (error); 2780b1b8ab34Slling 2781379c004dSEric Schrock /* 2782379c004dSEric Schrock * If the only property is the configfile, then just do a spa_lookup() 2783379c004dSEric Schrock * to handle the faulted case. 2784379c004dSEric Schrock */ 278592241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL); 278692241e0bSTom Erickson if (pair != NULL && strcmp(nvpair_name(pair), 2787379c004dSEric Schrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 278892241e0bSTom Erickson nvlist_next_nvpair(props, pair) == NULL) { 2789379c004dSEric Schrock mutex_enter(&spa_namespace_lock); 2790379c004dSEric Schrock if ((spa = spa_lookup(zc->zc_name)) != NULL) { 2791379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 2792379c004dSEric Schrock spa_config_sync(spa, B_FALSE, B_TRUE); 2793379c004dSEric Schrock } 2794379c004dSEric Schrock mutex_exit(&spa_namespace_lock); 2795b693757aSEric Schrock if (spa != NULL) { 2796b693757aSEric Schrock nvlist_free(props); 2797379c004dSEric Schrock return (0); 2798b693757aSEric Schrock } 2799379c004dSEric Schrock } 2800379c004dSEric Schrock 2801b1b8ab34Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2802990b4856Slling nvlist_free(props); 2803b1b8ab34Slling return (error); 2804b1b8ab34Slling } 2805b1b8ab34Slling 2806990b4856Slling error = spa_prop_set(spa, props); 2807b1b8ab34Slling 2808990b4856Slling nvlist_free(props); 2809b1b8ab34Slling spa_close(spa, FTAG); 2810b1b8ab34Slling 2811b1b8ab34Slling return (error); 2812b1b8ab34Slling } 2813b1b8ab34Slling 2814b1b8ab34Slling static int 281511a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 2816b1b8ab34Slling { 2817b1b8ab34Slling spa_t *spa; 2818b1b8ab34Slling int error; 2819b1b8ab34Slling nvlist_t *nvp = NULL; 2820b1b8ab34Slling 2821379c004dSEric Schrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2822379c004dSEric Schrock /* 2823379c004dSEric Schrock * If the pool is faulted, there may be properties we can still 2824379c004dSEric Schrock * get (such as altroot and cachefile), so attempt to get them 2825379c004dSEric Schrock * anyway. 2826379c004dSEric Schrock */ 2827379c004dSEric Schrock mutex_enter(&spa_namespace_lock); 2828379c004dSEric Schrock if ((spa = spa_lookup(zc->zc_name)) != NULL) 2829379c004dSEric Schrock error = spa_prop_get(spa, &nvp); 2830379c004dSEric Schrock mutex_exit(&spa_namespace_lock); 2831379c004dSEric Schrock } else { 2832379c004dSEric Schrock error = spa_prop_get(spa, &nvp); 2833379c004dSEric Schrock spa_close(spa, FTAG); 2834379c004dSEric Schrock } 2835b1b8ab34Slling 2836b1b8ab34Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 2837b1b8ab34Slling error = put_nvlist(zc, nvp); 2838b1b8ab34Slling else 2839be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 2840b1b8ab34Slling 2841379c004dSEric Schrock nvlist_free(nvp); 2842b1b8ab34Slling return (error); 2843b1b8ab34Slling } 2844b1b8ab34Slling 28453cb34c60Sahrens /* 28463cb34c60Sahrens * inputs: 28473cb34c60Sahrens * zc_name name of filesystem 28483cb34c60Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 28493cb34c60Sahrens * zc_perm_action allow/unallow flag 28503cb34c60Sahrens * 28513cb34c60Sahrens * outputs: none 28523cb34c60Sahrens */ 2853ecd6cf80Smarks static int 2854ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 2855ecd6cf80Smarks { 2856ecd6cf80Smarks int error; 2857ecd6cf80Smarks nvlist_t *fsaclnv = NULL; 2858ecd6cf80Smarks 2859990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2860478ed9adSEric Taylor zc->zc_iflags, &fsaclnv)) != 0) 2861ecd6cf80Smarks return (error); 2862ecd6cf80Smarks 2863ecd6cf80Smarks /* 2864ecd6cf80Smarks * Verify nvlist is constructed correctly 2865ecd6cf80Smarks */ 2866ecd6cf80Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 2867ecd6cf80Smarks nvlist_free(fsaclnv); 2868be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2869ecd6cf80Smarks } 2870ecd6cf80Smarks 2871ecd6cf80Smarks /* 2872ecd6cf80Smarks * If we don't have PRIV_SYS_MOUNT, then validate 2873ecd6cf80Smarks * that user is allowed to hand out each permission in 2874ecd6cf80Smarks * the nvlist(s) 2875ecd6cf80Smarks */ 2876ecd6cf80Smarks 287791ebeef5Sahrens error = secpolicy_zfs(CRED()); 28783b2aab18SMatthew Ahrens if (error != 0) { 287991ebeef5Sahrens if (zc->zc_perm_action == B_FALSE) { 288091ebeef5Sahrens error = dsl_deleg_can_allow(zc->zc_name, 288191ebeef5Sahrens fsaclnv, CRED()); 288291ebeef5Sahrens } else { 288391ebeef5Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 288491ebeef5Sahrens fsaclnv, CRED()); 288591ebeef5Sahrens } 2886ecd6cf80Smarks } 2887ecd6cf80Smarks 2888ecd6cf80Smarks if (error == 0) 2889ecd6cf80Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 2890ecd6cf80Smarks 2891ecd6cf80Smarks nvlist_free(fsaclnv); 2892ecd6cf80Smarks return (error); 2893ecd6cf80Smarks } 2894ecd6cf80Smarks 28953cb34c60Sahrens /* 28963cb34c60Sahrens * inputs: 28973cb34c60Sahrens * zc_name name of filesystem 28983cb34c60Sahrens * 28993cb34c60Sahrens * outputs: 29003cb34c60Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 29013cb34c60Sahrens */ 2902ecd6cf80Smarks static int 2903ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 2904ecd6cf80Smarks { 2905ecd6cf80Smarks nvlist_t *nvp; 2906ecd6cf80Smarks int error; 2907ecd6cf80Smarks 2908ecd6cf80Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 2909ecd6cf80Smarks error = put_nvlist(zc, nvp); 2910ecd6cf80Smarks nvlist_free(nvp); 2911ecd6cf80Smarks } 2912ecd6cf80Smarks 2913ecd6cf80Smarks return (error); 2914ecd6cf80Smarks } 2915ecd6cf80Smarks 2916fa9e4066Sahrens /* 2917fa9e4066Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 2918fa9e4066Sahrens * or NULL if no suitable entry is found. The caller of this routine 2919fa9e4066Sahrens * is responsible for releasing the returned vfs pointer. 2920fa9e4066Sahrens */ 2921fa9e4066Sahrens static vfs_t * 2922fa9e4066Sahrens zfs_get_vfs(const char *resource) 2923fa9e4066Sahrens { 2924fa9e4066Sahrens struct vfs *vfsp; 2925fa9e4066Sahrens struct vfs *vfs_found = NULL; 2926fa9e4066Sahrens 2927fa9e4066Sahrens vfs_list_read_lock(); 2928fa9e4066Sahrens vfsp = rootvfs; 2929fa9e4066Sahrens do { 2930fa9e4066Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2931fa9e4066Sahrens VFS_HOLD(vfsp); 2932fa9e4066Sahrens vfs_found = vfsp; 2933fa9e4066Sahrens break; 2934fa9e4066Sahrens } 2935fa9e4066Sahrens vfsp = vfsp->vfs_next; 2936fa9e4066Sahrens } while (vfsp != rootvfs); 2937fa9e4066Sahrens vfs_list_unlock(); 2938fa9e4066Sahrens return (vfs_found); 2939fa9e4066Sahrens } 2940fa9e4066Sahrens 2941ecd6cf80Smarks /* ARGSUSED */ 2942fa9e4066Sahrens static void 2943ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2944fa9e4066Sahrens { 2945da6c28aaSamw zfs_creat_t *zct = arg; 2946da6c28aaSamw 2947de8267e0Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 2948da6c28aaSamw } 2949da6c28aaSamw 2950de8267e0Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 2951da6c28aaSamw 2952da6c28aaSamw /* 2953de8267e0Stimh * inputs: 29540a48a24eStimh * os parent objset pointer (NULL if root fs) 2955*f7170741SWill Andrews * fuids_ok fuids allowed in this version of the spa? 2956*f7170741SWill Andrews * sa_ok SAs allowed in this version of the spa? 2957*f7170741SWill Andrews * createprops list of properties requested by creator 2958de8267e0Stimh * 2959de8267e0Stimh * outputs: 2960de8267e0Stimh * zplprops values for the zplprops we attach to the master node object 29610a48a24eStimh * is_ci true if requested file system will be purely case-insensitive 2962da6c28aaSamw * 2963de8267e0Stimh * Determine the settings for utf8only, normalization and 2964de8267e0Stimh * casesensitivity. Specific values may have been requested by the 2965de8267e0Stimh * creator and/or we can inherit values from the parent dataset. If 2966de8267e0Stimh * the file system is of too early a vintage, a creator can not 2967de8267e0Stimh * request settings for these properties, even if the requested 2968de8267e0Stimh * setting is the default value. We don't actually want to create dsl 2969de8267e0Stimh * properties for these, so remove them from the source nvlist after 2970de8267e0Stimh * processing. 2971da6c28aaSamw */ 2972da6c28aaSamw static int 297314843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 29740a586ceaSMark Shellenbaum boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops, 29750a586ceaSMark Shellenbaum nvlist_t *zplprops, boolean_t *is_ci) 2976da6c28aaSamw { 2977de8267e0Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 2978de8267e0Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 2979de8267e0Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 2980da6c28aaSamw 2981de8267e0Stimh ASSERT(zplprops != NULL); 2982da6c28aaSamw 2983de8267e0Stimh /* 2984de8267e0Stimh * Pull out creator prop choices, if any. 2985de8267e0Stimh */ 2986de8267e0Stimh if (createprops) { 29870a48a24eStimh (void) nvlist_lookup_uint64(createprops, 29880a48a24eStimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 2989de8267e0Stimh (void) nvlist_lookup_uint64(createprops, 2990de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 2991de8267e0Stimh (void) nvlist_remove_all(createprops, 2992de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 2993de8267e0Stimh (void) nvlist_lookup_uint64(createprops, 2994de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 2995de8267e0Stimh (void) nvlist_remove_all(createprops, 2996de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 2997de8267e0Stimh (void) nvlist_lookup_uint64(createprops, 2998de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 2999de8267e0Stimh (void) nvlist_remove_all(createprops, 3000de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 3001de8267e0Stimh } 3002da6c28aaSamw 3003c2a93d44Stimh /* 30040a48a24eStimh * If the zpl version requested is whacky or the file system 30050a48a24eStimh * or pool is version is too "young" to support normalization 30060a48a24eStimh * and the creator tried to set a value for one of the props, 30070a48a24eStimh * error out. 3008c2a93d44Stimh */ 30090a48a24eStimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 30100a48a24eStimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 30110a586ceaSMark Shellenbaum (zplver >= ZPL_VERSION_SA && !sa_ok) || 30120a48a24eStimh (zplver < ZPL_VERSION_NORMALIZATION && 3013de8267e0Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 30140a48a24eStimh sense != ZFS_PROP_UNDEFINED))) 3015be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3016c2a93d44Stimh 3017de8267e0Stimh /* 3018de8267e0Stimh * Put the version in the zplprops 3019de8267e0Stimh */ 3020de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 3021de8267e0Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 3022da6c28aaSamw 3023de8267e0Stimh if (norm == ZFS_PROP_UNDEFINED) 3024de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 3025de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 3026de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 3027da6c28aaSamw 3028c2a93d44Stimh /* 3029de8267e0Stimh * If we're normalizing, names must always be valid UTF-8 strings. 3030c2a93d44Stimh */ 3031de8267e0Stimh if (norm) 3032de8267e0Stimh u8 = 1; 3033de8267e0Stimh if (u8 == ZFS_PROP_UNDEFINED) 3034de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 3035de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 3036de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 3037de8267e0Stimh 3038de8267e0Stimh if (sense == ZFS_PROP_UNDEFINED) 3039de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 3040de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops, 3041de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 3042c2a93d44Stimh 3043ab04eb8eStimh if (is_ci) 3044ab04eb8eStimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 3045ab04eb8eStimh 3046da6c28aaSamw return (0); 3047fa9e4066Sahrens } 3048fa9e4066Sahrens 30490a48a24eStimh static int 30500a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 30510a48a24eStimh nvlist_t *zplprops, boolean_t *is_ci) 30520a48a24eStimh { 30530a586ceaSMark Shellenbaum boolean_t fuids_ok, sa_ok; 30540a48a24eStimh uint64_t zplver = ZPL_VERSION; 30550a48a24eStimh objset_t *os = NULL; 30560a48a24eStimh char parentname[MAXNAMELEN]; 30570a48a24eStimh char *cp; 30580a586ceaSMark Shellenbaum spa_t *spa; 30590a586ceaSMark Shellenbaum uint64_t spa_vers; 30600a48a24eStimh int error; 30610a48a24eStimh 30620a48a24eStimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 30630a48a24eStimh cp = strrchr(parentname, '/'); 30640a48a24eStimh ASSERT(cp != NULL); 30650a48a24eStimh cp[0] = '\0'; 30660a48a24eStimh 30670a586ceaSMark Shellenbaum if ((error = spa_open(dataset, &spa, FTAG)) != 0) 30680a586ceaSMark Shellenbaum return (error); 30690a586ceaSMark Shellenbaum 30700a586ceaSMark Shellenbaum spa_vers = spa_version(spa); 30710a586ceaSMark Shellenbaum spa_close(spa, FTAG); 30720a586ceaSMark Shellenbaum 30730a586ceaSMark Shellenbaum zplver = zfs_zpl_version_map(spa_vers); 30740a586ceaSMark Shellenbaum fuids_ok = (zplver >= ZPL_VERSION_FUID); 30750a586ceaSMark Shellenbaum sa_ok = (zplver >= ZPL_VERSION_SA); 30760a48a24eStimh 30770a48a24eStimh /* 30780a48a24eStimh * Open parent object set so we can inherit zplprop values. 30790a48a24eStimh */ 3080503ad85cSMatthew Ahrens if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 30810a48a24eStimh return (error); 30820a48a24eStimh 30830a586ceaSMark Shellenbaum error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops, 30840a48a24eStimh zplprops, is_ci); 3085503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 30860a48a24eStimh return (error); 30870a48a24eStimh } 30880a48a24eStimh 30890a48a24eStimh static int 30900a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 30910a48a24eStimh nvlist_t *zplprops, boolean_t *is_ci) 30920a48a24eStimh { 30930a586ceaSMark Shellenbaum boolean_t fuids_ok; 30940a586ceaSMark Shellenbaum boolean_t sa_ok; 30950a48a24eStimh uint64_t zplver = ZPL_VERSION; 30960a48a24eStimh int error; 30970a48a24eStimh 30980a586ceaSMark Shellenbaum zplver = zfs_zpl_version_map(spa_vers); 30990a586ceaSMark Shellenbaum fuids_ok = (zplver >= ZPL_VERSION_FUID); 31000a586ceaSMark Shellenbaum sa_ok = (zplver >= ZPL_VERSION_SA); 31010a48a24eStimh 31020a586ceaSMark Shellenbaum error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok, 31030a586ceaSMark Shellenbaum createprops, zplprops, is_ci); 31040a48a24eStimh return (error); 31050a48a24eStimh } 31060a48a24eStimh 31073cb34c60Sahrens /* 31084445fffbSMatthew Ahrens * innvl: { 31094445fffbSMatthew Ahrens * "type" -> dmu_objset_type_t (int32) 31104445fffbSMatthew Ahrens * (optional) "props" -> { prop -> value } 31114445fffbSMatthew Ahrens * } 31123cb34c60Sahrens * 31134445fffbSMatthew Ahrens * outnvl: propname -> error code (int32) 31143cb34c60Sahrens */ 3115fa9e4066Sahrens static int 31164445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3117fa9e4066Sahrens { 3118fa9e4066Sahrens int error = 0; 31194445fffbSMatthew Ahrens zfs_creat_t zct = { 0 }; 3120ecd6cf80Smarks nvlist_t *nvprops = NULL; 3121ecd6cf80Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 31224445fffbSMatthew Ahrens int32_t type32; 31234445fffbSMatthew Ahrens dmu_objset_type_t type; 31244445fffbSMatthew Ahrens boolean_t is_insensitive = B_FALSE; 3125fa9e4066Sahrens 31264445fffbSMatthew Ahrens if (nvlist_lookup_int32(innvl, "type", &type32) != 0) 3127be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 31284445fffbSMatthew Ahrens type = type32; 31294445fffbSMatthew Ahrens (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3130fa9e4066Sahrens 31314445fffbSMatthew Ahrens switch (type) { 3132fa9e4066Sahrens case DMU_OST_ZFS: 3133fa9e4066Sahrens cbfunc = zfs_create_cb; 3134fa9e4066Sahrens break; 3135fa9e4066Sahrens 3136fa9e4066Sahrens case DMU_OST_ZVOL: 3137fa9e4066Sahrens cbfunc = zvol_create_cb; 3138fa9e4066Sahrens break; 3139fa9e4066Sahrens 3140fa9e4066Sahrens default: 31411d452cf5Sahrens cbfunc = NULL; 3142e7cbe64fSgw break; 3143fa9e4066Sahrens } 31444445fffbSMatthew Ahrens if (strchr(fsname, '@') || 31454445fffbSMatthew Ahrens strchr(fsname, '%')) 3146be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3147fa9e4066Sahrens 3148da6c28aaSamw zct.zct_props = nvprops; 3149da6c28aaSamw 31504445fffbSMatthew Ahrens if (cbfunc == NULL) 3151be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 31524445fffbSMatthew Ahrens 31534445fffbSMatthew Ahrens if (type == DMU_OST_ZVOL) { 31544445fffbSMatthew Ahrens uint64_t volsize, volblocksize; 31554445fffbSMatthew Ahrens 31564445fffbSMatthew Ahrens if (nvprops == NULL) 3157be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 31584445fffbSMatthew Ahrens if (nvlist_lookup_uint64(nvprops, 31594445fffbSMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0) 3160be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3161fa9e4066Sahrens 31624445fffbSMatthew Ahrens if ((error = nvlist_lookup_uint64(nvprops, 31634445fffbSMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 31644445fffbSMatthew Ahrens &volblocksize)) != 0 && error != ENOENT) 3165be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 31664445fffbSMatthew Ahrens 31674445fffbSMatthew Ahrens if (error != 0) 31684445fffbSMatthew Ahrens volblocksize = zfs_prop_default_numeric( 31694445fffbSMatthew Ahrens ZFS_PROP_VOLBLOCKSIZE); 31704445fffbSMatthew Ahrens 31714445fffbSMatthew Ahrens if ((error = zvol_check_volblocksize( 31724445fffbSMatthew Ahrens volblocksize)) != 0 || 31734445fffbSMatthew Ahrens (error = zvol_check_volsize(volsize, 31744445fffbSMatthew Ahrens volblocksize)) != 0) 3175fa9e4066Sahrens return (error); 31764445fffbSMatthew Ahrens } else if (type == DMU_OST_ZFS) { 31774445fffbSMatthew Ahrens int error; 3178ab04eb8eStimh 31794445fffbSMatthew Ahrens /* 31804445fffbSMatthew Ahrens * We have to have normalization and 31814445fffbSMatthew Ahrens * case-folding flags correct when we do the 31824445fffbSMatthew Ahrens * file system creation, so go figure them out 31834445fffbSMatthew Ahrens * now. 31844445fffbSMatthew Ahrens */ 31854445fffbSMatthew Ahrens VERIFY(nvlist_alloc(&zct.zct_zplprops, 31864445fffbSMatthew Ahrens NV_UNIQUE_NAME, KM_SLEEP) == 0); 31874445fffbSMatthew Ahrens error = zfs_fill_zplprops(fsname, nvprops, 31884445fffbSMatthew Ahrens zct.zct_zplprops, &is_insensitive); 31894445fffbSMatthew Ahrens if (error != 0) { 31904445fffbSMatthew Ahrens nvlist_free(zct.zct_zplprops); 3191da6c28aaSamw return (error); 3192da6c28aaSamw } 31934445fffbSMatthew Ahrens } 3194ab04eb8eStimh 31954445fffbSMatthew Ahrens error = dmu_objset_create(fsname, type, 31964445fffbSMatthew Ahrens is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 31974445fffbSMatthew Ahrens nvlist_free(zct.zct_zplprops); 31985c5460e9Seschrock 31994445fffbSMatthew Ahrens /* 32004445fffbSMatthew Ahrens * It would be nice to do this atomically. 32014445fffbSMatthew Ahrens */ 32024445fffbSMatthew Ahrens if (error == 0) { 32034445fffbSMatthew Ahrens error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 32044445fffbSMatthew Ahrens nvprops, outnvl); 32054445fffbSMatthew Ahrens if (error != 0) 32063b2aab18SMatthew Ahrens (void) dsl_destroy_head(fsname); 32074445fffbSMatthew Ahrens } 32084445fffbSMatthew Ahrens return (error); 32094445fffbSMatthew Ahrens } 3210e9dbad6fSeschrock 32114445fffbSMatthew Ahrens /* 32124445fffbSMatthew Ahrens * innvl: { 32134445fffbSMatthew Ahrens * "origin" -> name of origin snapshot 32144445fffbSMatthew Ahrens * (optional) "props" -> { prop -> value } 32154445fffbSMatthew Ahrens * } 32164445fffbSMatthew Ahrens * 32174445fffbSMatthew Ahrens * outnvl: propname -> error code (int32) 32184445fffbSMatthew Ahrens */ 32194445fffbSMatthew Ahrens static int 32204445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 32214445fffbSMatthew Ahrens { 32224445fffbSMatthew Ahrens int error = 0; 32234445fffbSMatthew Ahrens nvlist_t *nvprops = NULL; 32244445fffbSMatthew Ahrens char *origin_name; 3225e9dbad6fSeschrock 32264445fffbSMatthew Ahrens if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0) 3227be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 32284445fffbSMatthew Ahrens (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3229e9dbad6fSeschrock 32304445fffbSMatthew Ahrens if (strchr(fsname, '@') || 32314445fffbSMatthew Ahrens strchr(fsname, '%')) 3232be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3233e9dbad6fSeschrock 32344445fffbSMatthew Ahrens if (dataset_namecheck(origin_name, NULL, NULL) != 0) 3235be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 32363b2aab18SMatthew Ahrens error = dmu_objset_clone(fsname, origin_name); 32373b2aab18SMatthew Ahrens if (error != 0) 32384445fffbSMatthew Ahrens return (error); 3239e9dbad6fSeschrock 3240e9dbad6fSeschrock /* 3241e9dbad6fSeschrock * It would be nice to do this atomically. 3242e9dbad6fSeschrock */ 3243e9dbad6fSeschrock if (error == 0) { 32444445fffbSMatthew Ahrens error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 32454445fffbSMatthew Ahrens nvprops, outnvl); 324692241e0bSTom Erickson if (error != 0) 32473b2aab18SMatthew Ahrens (void) dsl_destroy_head(fsname); 3248e9dbad6fSeschrock } 3249fa9e4066Sahrens return (error); 3250fa9e4066Sahrens } 3251fa9e4066Sahrens 32523cb34c60Sahrens /* 32534445fffbSMatthew Ahrens * innvl: { 32544445fffbSMatthew Ahrens * "snaps" -> { snapshot1, snapshot2 } 32554445fffbSMatthew Ahrens * (optional) "props" -> { prop -> value (string) } 32564445fffbSMatthew Ahrens * } 32574445fffbSMatthew Ahrens * 32584445fffbSMatthew Ahrens * outnvl: snapshot -> error code (int32) 32593cb34c60Sahrens */ 3260fa9e4066Sahrens static int 32614445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3262fa9e4066Sahrens { 32634445fffbSMatthew Ahrens nvlist_t *snaps; 32644445fffbSMatthew Ahrens nvlist_t *props = NULL; 32654445fffbSMatthew Ahrens int error, poollen; 32664445fffbSMatthew Ahrens nvpair_t *pair; 3267bb0ade09Sahrens 32684445fffbSMatthew Ahrens (void) nvlist_lookup_nvlist(innvl, "props", &props); 32694445fffbSMatthew Ahrens if ((error = zfs_check_userprops(poolname, props)) != 0) 32704445fffbSMatthew Ahrens return (error); 32714445fffbSMatthew Ahrens 32724445fffbSMatthew Ahrens if (!nvlist_empty(props) && 32734445fffbSMatthew Ahrens zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS)) 3274be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 32754445fffbSMatthew Ahrens 32764445fffbSMatthew Ahrens if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 3277be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 32784445fffbSMatthew Ahrens poollen = strlen(poolname); 32794445fffbSMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 32804445fffbSMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 32814445fffbSMatthew Ahrens const char *name = nvpair_name(pair); 32824445fffbSMatthew Ahrens const char *cp = strchr(name, '@'); 3283bb0ade09Sahrens 32844445fffbSMatthew Ahrens /* 32854445fffbSMatthew Ahrens * The snap name must contain an @, and the part after it must 32864445fffbSMatthew Ahrens * contain only valid characters. 32874445fffbSMatthew Ahrens */ 32884445fffbSMatthew Ahrens if (cp == NULL || snapshot_namecheck(cp + 1, NULL, NULL) != 0) 3289be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3290bb0ade09Sahrens 32914445fffbSMatthew Ahrens /* 32924445fffbSMatthew Ahrens * The snap must be in the specified pool. 32934445fffbSMatthew Ahrens */ 32944445fffbSMatthew Ahrens if (strncmp(name, poolname, poollen) != 0 || 32954445fffbSMatthew Ahrens (name[poollen] != '/' && name[poollen] != '@')) 3296be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 32974445fffbSMatthew Ahrens 32984445fffbSMatthew Ahrens /* This must be the only snap of this fs. */ 32994445fffbSMatthew Ahrens for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair); 33004445fffbSMatthew Ahrens pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) { 33014445fffbSMatthew Ahrens if (strncmp(name, nvpair_name(pair2), cp - name + 1) 33024445fffbSMatthew Ahrens == 0) { 3303be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 33044445fffbSMatthew Ahrens } 33054445fffbSMatthew Ahrens } 33064445fffbSMatthew Ahrens } 3307bb0ade09Sahrens 33083b2aab18SMatthew Ahrens error = dsl_dataset_snapshot(snaps, props, outnvl); 33094445fffbSMatthew Ahrens return (error); 33104445fffbSMatthew Ahrens } 33114445fffbSMatthew Ahrens 33124445fffbSMatthew Ahrens /* 33134445fffbSMatthew Ahrens * innvl: "message" -> string 33144445fffbSMatthew Ahrens */ 33154445fffbSMatthew Ahrens /* ARGSUSED */ 33164445fffbSMatthew Ahrens static int 33174445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl) 33184445fffbSMatthew Ahrens { 33194445fffbSMatthew Ahrens char *message; 33204445fffbSMatthew Ahrens spa_t *spa; 33214445fffbSMatthew Ahrens int error; 33224445fffbSMatthew Ahrens char *poolname; 33234445fffbSMatthew Ahrens 33244445fffbSMatthew Ahrens /* 33254445fffbSMatthew Ahrens * The poolname in the ioctl is not set, we get it from the TSD, 33264445fffbSMatthew Ahrens * which was set at the end of the last successful ioctl that allows 33274445fffbSMatthew Ahrens * logging. The secpolicy func already checked that it is set. 33284445fffbSMatthew Ahrens * Only one log ioctl is allowed after each successful ioctl, so 33294445fffbSMatthew Ahrens * we clear the TSD here. 33304445fffbSMatthew Ahrens */ 33314445fffbSMatthew Ahrens poolname = tsd_get(zfs_allow_log_key); 33324445fffbSMatthew Ahrens (void) tsd_set(zfs_allow_log_key, NULL); 33334445fffbSMatthew Ahrens error = spa_open(poolname, &spa, FTAG); 33344445fffbSMatthew Ahrens strfree(poolname); 33354445fffbSMatthew Ahrens if (error != 0) 33364445fffbSMatthew Ahrens return (error); 33374445fffbSMatthew Ahrens 33384445fffbSMatthew Ahrens if (nvlist_lookup_string(innvl, "message", &message) != 0) { 33394445fffbSMatthew Ahrens spa_close(spa, FTAG); 3340be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3341bb0ade09Sahrens } 3342ea2f5b9eSMatthew Ahrens 33434445fffbSMatthew Ahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 33444445fffbSMatthew Ahrens spa_close(spa, FTAG); 3345be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 33464445fffbSMatthew Ahrens } 3347ea2f5b9eSMatthew Ahrens 33484445fffbSMatthew Ahrens error = spa_history_log(spa, message); 33494445fffbSMatthew Ahrens spa_close(spa, FTAG); 3350bb0ade09Sahrens return (error); 33511d452cf5Sahrens } 3352fa9e4066Sahrens 33533b2aab18SMatthew Ahrens /* 33543b2aab18SMatthew Ahrens * The dp_config_rwlock must not be held when calling this, because the 33553b2aab18SMatthew Ahrens * unmount may need to write out data. 33563b2aab18SMatthew Ahrens * 33573b2aab18SMatthew Ahrens * This function is best-effort. Callers must deal gracefully if it 33583b2aab18SMatthew Ahrens * remains mounted (or is remounted after this call). 33593b2aab18SMatthew Ahrens */ 33603b2aab18SMatthew Ahrens void 33613b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname) 33621d452cf5Sahrens { 33634445fffbSMatthew Ahrens vfs_t *vfsp; 33643b2aab18SMatthew Ahrens zfsvfs_t *zfsvfs; 33651d452cf5Sahrens 33663b2aab18SMatthew Ahrens if (strchr(snapname, '@') == NULL) 33673b2aab18SMatthew Ahrens return; 33681d452cf5Sahrens 33693b2aab18SMatthew Ahrens vfsp = zfs_get_vfs(snapname); 33704445fffbSMatthew Ahrens if (vfsp == NULL) 33713b2aab18SMatthew Ahrens return; 33723b2aab18SMatthew Ahrens 33733b2aab18SMatthew Ahrens zfsvfs = vfsp->vfs_data; 33743b2aab18SMatthew Ahrens ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os))); 33751d452cf5Sahrens 33763b2aab18SMatthew Ahrens if (vn_vfswlock(vfsp->vfs_vnodecovered) != 0) { 33771d452cf5Sahrens VFS_RELE(vfsp); 33783b2aab18SMatthew Ahrens return; 33791d452cf5Sahrens } 33804445fffbSMatthew Ahrens VFS_RELE(vfsp); 33814445fffbSMatthew Ahrens 33824445fffbSMatthew Ahrens /* 33834445fffbSMatthew Ahrens * Always force the unmount for snapshots. 33844445fffbSMatthew Ahrens */ 33853b2aab18SMatthew Ahrens (void) dounmount(vfsp, MS_FORCE, kcred); 33863b2aab18SMatthew Ahrens } 33873b2aab18SMatthew Ahrens 33883b2aab18SMatthew Ahrens /* ARGSUSED */ 33893b2aab18SMatthew Ahrens static int 33903b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg) 33913b2aab18SMatthew Ahrens { 33923b2aab18SMatthew Ahrens zfs_unmount_snap(snapname); 33933b2aab18SMatthew Ahrens return (0); 33943b2aab18SMatthew Ahrens } 33953b2aab18SMatthew Ahrens 33963b2aab18SMatthew Ahrens /* 33973b2aab18SMatthew Ahrens * When a clone is destroyed, its origin may also need to be destroyed, 33983b2aab18SMatthew Ahrens * in which case it must be unmounted. This routine will do that unmount 33993b2aab18SMatthew Ahrens * if necessary. 34003b2aab18SMatthew Ahrens */ 34013b2aab18SMatthew Ahrens void 34023b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname) 34033b2aab18SMatthew Ahrens { 34043b2aab18SMatthew Ahrens int error; 34053b2aab18SMatthew Ahrens objset_t *os; 34063b2aab18SMatthew Ahrens dsl_dataset_t *ds; 34073b2aab18SMatthew Ahrens 34083b2aab18SMatthew Ahrens error = dmu_objset_hold(fsname, FTAG, &os); 34093b2aab18SMatthew Ahrens if (error != 0) 34103b2aab18SMatthew Ahrens return; 34113b2aab18SMatthew Ahrens ds = dmu_objset_ds(os); 34123b2aab18SMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) { 34133b2aab18SMatthew Ahrens char originname[MAXNAMELEN]; 34143b2aab18SMatthew Ahrens dsl_dataset_name(ds->ds_prev, originname); 34153b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 34163b2aab18SMatthew Ahrens zfs_unmount_snap(originname); 34173b2aab18SMatthew Ahrens } else { 34183b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 34193b2aab18SMatthew Ahrens } 34201d452cf5Sahrens } 34211d452cf5Sahrens 34223cb34c60Sahrens /* 34234445fffbSMatthew Ahrens * innvl: { 34244445fffbSMatthew Ahrens * "snaps" -> { snapshot1, snapshot2 } 34254445fffbSMatthew Ahrens * (optional boolean) "defer" 34264445fffbSMatthew Ahrens * } 34274445fffbSMatthew Ahrens * 34284445fffbSMatthew Ahrens * outnvl: snapshot -> error code (int32) 34293cb34c60Sahrens * 34303cb34c60Sahrens */ 34311d452cf5Sahrens static int 34324445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 34331d452cf5Sahrens { 34344445fffbSMatthew Ahrens int poollen; 34354445fffbSMatthew Ahrens nvlist_t *snaps; 343619b94df9SMatthew Ahrens nvpair_t *pair; 34374445fffbSMatthew Ahrens boolean_t defer; 34381d452cf5Sahrens 34394445fffbSMatthew Ahrens if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 3440be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 34414445fffbSMatthew Ahrens defer = nvlist_exists(innvl, "defer"); 344219b94df9SMatthew Ahrens 34434445fffbSMatthew Ahrens poollen = strlen(poolname); 34444445fffbSMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 34454445fffbSMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 344619b94df9SMatthew Ahrens const char *name = nvpair_name(pair); 34474445fffbSMatthew Ahrens 344819b94df9SMatthew Ahrens /* 34494445fffbSMatthew Ahrens * The snap must be in the specified pool. 345019b94df9SMatthew Ahrens */ 34514445fffbSMatthew Ahrens if (strncmp(name, poolname, poollen) != 0 || 34524445fffbSMatthew Ahrens (name[poollen] != '/' && name[poollen] != '@')) 3453be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 345419b94df9SMatthew Ahrens 34553b2aab18SMatthew Ahrens zfs_unmount_snap(name); 345619b94df9SMatthew Ahrens } 345719b94df9SMatthew Ahrens 34583b2aab18SMatthew Ahrens return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl)); 34591d452cf5Sahrens } 34601d452cf5Sahrens 34613cb34c60Sahrens /* 34623cb34c60Sahrens * inputs: 34633cb34c60Sahrens * zc_name name of dataset to destroy 34643cb34c60Sahrens * zc_objset_type type of objset 3465842727c2SChris Kirby * zc_defer_destroy mark for deferred destroy 34663cb34c60Sahrens * 34673cb34c60Sahrens * outputs: none 34683cb34c60Sahrens */ 34691d452cf5Sahrens static int 34701d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 34711d452cf5Sahrens { 3472681d9761SEric Taylor int err; 34733b2aab18SMatthew Ahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) 34743b2aab18SMatthew Ahrens zfs_unmount_snap(zc->zc_name); 3475fa9e4066Sahrens 34763b2aab18SMatthew Ahrens if (strchr(zc->zc_name, '@')) 34773b2aab18SMatthew Ahrens err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy); 34783b2aab18SMatthew Ahrens else 34793b2aab18SMatthew Ahrens err = dsl_destroy_head(zc->zc_name); 3480681d9761SEric Taylor if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 34815c987a37SChris Kirby (void) zvol_remove_minor(zc->zc_name); 3482681d9761SEric Taylor return (err); 3483fa9e4066Sahrens } 3484fa9e4066Sahrens 34853cb34c60Sahrens /* 34863cb34c60Sahrens * inputs: 34874ccbb6e7Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 34883cb34c60Sahrens * 34893cb34c60Sahrens * outputs: none 34903cb34c60Sahrens */ 3491fa9e4066Sahrens static int 3492fa9e4066Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 3493fa9e4066Sahrens { 3494ae46e4c7SMatthew Ahrens zfsvfs_t *zfsvfs; 34953b2aab18SMatthew Ahrens int error; 3496ae46e4c7SMatthew Ahrens 349714843421SMatthew Ahrens if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 3498503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs); 349947f263f4Sek if (error == 0) { 350047f263f4Sek int resume_err; 35014ccbb6e7Sahrens 35023b2aab18SMatthew Ahrens error = dsl_dataset_rollback(zc->zc_name); 3503503ad85cSMatthew Ahrens resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); 350447f263f4Sek error = error ? error : resume_err; 350547f263f4Sek } 35064ccbb6e7Sahrens VFS_RELE(zfsvfs->z_vfs); 35074ccbb6e7Sahrens } else { 35083b2aab18SMatthew Ahrens error = dsl_dataset_rollback(zc->zc_name); 35094ccbb6e7Sahrens } 35103b2aab18SMatthew Ahrens return (error); 35113b2aab18SMatthew Ahrens } 35124ccbb6e7Sahrens 35133b2aab18SMatthew Ahrens static int 35143b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg) 35153b2aab18SMatthew Ahrens { 35163b2aab18SMatthew Ahrens const char *snapname = arg; 35173b2aab18SMatthew Ahrens char fullname[MAXNAMELEN]; 3518ae46e4c7SMatthew Ahrens 35193b2aab18SMatthew Ahrens (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname); 35203b2aab18SMatthew Ahrens zfs_unmount_snap(fullname); 35213b2aab18SMatthew Ahrens return (0); 3522fa9e4066Sahrens } 3523fa9e4066Sahrens 35243cb34c60Sahrens /* 35253cb34c60Sahrens * inputs: 35263cb34c60Sahrens * zc_name old name of dataset 35273cb34c60Sahrens * zc_value new name of dataset 35283cb34c60Sahrens * zc_cookie recursive flag (only valid for snapshots) 35293cb34c60Sahrens * 35303cb34c60Sahrens * outputs: none 35313cb34c60Sahrens */ 3532fa9e4066Sahrens static int 3533fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 3534fa9e4066Sahrens { 35357f1f55eaSvb boolean_t recursive = zc->zc_cookie & 1; 35363b2aab18SMatthew Ahrens char *at; 3537cdf5b4caSmmusante 3538e9dbad6fSeschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 3539f18faf3fSek if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3540f18faf3fSek strchr(zc->zc_value, '%')) 3541be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3542fa9e4066Sahrens 35433b2aab18SMatthew Ahrens at = strchr(zc->zc_name, '@'); 35443b2aab18SMatthew Ahrens if (at != NULL) { 35453b2aab18SMatthew Ahrens /* snaps must be in same fs */ 35463b2aab18SMatthew Ahrens if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1)) 3547be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 35483b2aab18SMatthew Ahrens *at = '\0'; 35493b2aab18SMatthew Ahrens if (zc->zc_objset_type == DMU_OST_ZFS) { 35503b2aab18SMatthew Ahrens int error = dmu_objset_find(zc->zc_name, 35513b2aab18SMatthew Ahrens recursive_unmount, at + 1, 35523b2aab18SMatthew Ahrens recursive ? DS_FIND_CHILDREN : 0); 35533b2aab18SMatthew Ahrens if (error != 0) 35543b2aab18SMatthew Ahrens return (error); 35553b2aab18SMatthew Ahrens } 35563b2aab18SMatthew Ahrens return (dsl_dataset_rename_snapshot(zc->zc_name, 35573b2aab18SMatthew Ahrens at + 1, strchr(zc->zc_value, '@') + 1, recursive)); 35583b2aab18SMatthew Ahrens } else { 35593b2aab18SMatthew Ahrens if (zc->zc_objset_type == DMU_OST_ZVOL) 35603b2aab18SMatthew Ahrens (void) zvol_remove_minor(zc->zc_name); 35613b2aab18SMatthew Ahrens return (dsl_dir_rename(zc->zc_name, zc->zc_value)); 3562fa9e4066Sahrens } 3563fa9e4066Sahrens } 3564fa9e4066Sahrens 356592241e0bSTom Erickson static int 356692241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 356792241e0bSTom Erickson { 356892241e0bSTom Erickson const char *propname = nvpair_name(pair); 356992241e0bSTom Erickson boolean_t issnap = (strchr(dsname, '@') != NULL); 357092241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 357192241e0bSTom Erickson uint64_t intval; 357292241e0bSTom Erickson int err; 357392241e0bSTom Erickson 357492241e0bSTom Erickson if (prop == ZPROP_INVAL) { 357592241e0bSTom Erickson if (zfs_prop_user(propname)) { 357692241e0bSTom Erickson if (err = zfs_secpolicy_write_perms(dsname, 357792241e0bSTom Erickson ZFS_DELEG_PERM_USERPROP, cr)) 357892241e0bSTom Erickson return (err); 357992241e0bSTom Erickson return (0); 358092241e0bSTom Erickson } 358192241e0bSTom Erickson 358292241e0bSTom Erickson if (!issnap && zfs_prop_userquota(propname)) { 358392241e0bSTom Erickson const char *perm = NULL; 358492241e0bSTom Erickson const char *uq_prefix = 358592241e0bSTom Erickson zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 358692241e0bSTom Erickson const char *gq_prefix = 358792241e0bSTom Erickson zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 358892241e0bSTom Erickson 358992241e0bSTom Erickson if (strncmp(propname, uq_prefix, 359092241e0bSTom Erickson strlen(uq_prefix)) == 0) { 359192241e0bSTom Erickson perm = ZFS_DELEG_PERM_USERQUOTA; 359292241e0bSTom Erickson } else if (strncmp(propname, gq_prefix, 359392241e0bSTom Erickson strlen(gq_prefix)) == 0) { 359492241e0bSTom Erickson perm = ZFS_DELEG_PERM_GROUPQUOTA; 359592241e0bSTom Erickson } else { 359692241e0bSTom Erickson /* USERUSED and GROUPUSED are read-only */ 3597be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 359892241e0bSTom Erickson } 359992241e0bSTom Erickson 360092241e0bSTom Erickson if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 360192241e0bSTom Erickson return (err); 360292241e0bSTom Erickson return (0); 360392241e0bSTom Erickson } 360492241e0bSTom Erickson 3605be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 360692241e0bSTom Erickson } 360792241e0bSTom Erickson 360892241e0bSTom Erickson if (issnap) 3609be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 361092241e0bSTom Erickson 361192241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 361292241e0bSTom Erickson /* 361392241e0bSTom Erickson * dsl_prop_get_all_impl() returns properties in this 361492241e0bSTom Erickson * format. 361592241e0bSTom Erickson */ 361692241e0bSTom Erickson nvlist_t *attrs; 361792241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 361892241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 361992241e0bSTom Erickson &pair) == 0); 362092241e0bSTom Erickson } 362192241e0bSTom Erickson 362292241e0bSTom Erickson /* 362392241e0bSTom Erickson * Check that this value is valid for this pool version 362492241e0bSTom Erickson */ 362592241e0bSTom Erickson switch (prop) { 362692241e0bSTom Erickson case ZFS_PROP_COMPRESSION: 362792241e0bSTom Erickson /* 362892241e0bSTom Erickson * If the user specified gzip compression, make sure 362992241e0bSTom Erickson * the SPA supports it. We ignore any errors here since 363092241e0bSTom Erickson * we'll catch them later. 363192241e0bSTom Erickson */ 363292241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_UINT64 && 363392241e0bSTom Erickson nvpair_value_uint64(pair, &intval) == 0) { 363492241e0bSTom Erickson if (intval >= ZIO_COMPRESS_GZIP_1 && 363592241e0bSTom Erickson intval <= ZIO_COMPRESS_GZIP_9 && 363692241e0bSTom Erickson zfs_earlier_version(dsname, 363792241e0bSTom Erickson SPA_VERSION_GZIP_COMPRESSION)) { 3638be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 363992241e0bSTom Erickson } 364092241e0bSTom Erickson 364192241e0bSTom Erickson if (intval == ZIO_COMPRESS_ZLE && 364292241e0bSTom Erickson zfs_earlier_version(dsname, 364392241e0bSTom Erickson SPA_VERSION_ZLE_COMPRESSION)) 3644be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 364592241e0bSTom Erickson 3646a6f561b4SSašo Kiselkov if (intval == ZIO_COMPRESS_LZ4) { 3647a6f561b4SSašo Kiselkov zfeature_info_t *feature = 3648a6f561b4SSašo Kiselkov &spa_feature_table[ 3649a6f561b4SSašo Kiselkov SPA_FEATURE_LZ4_COMPRESS]; 3650a6f561b4SSašo Kiselkov spa_t *spa; 3651a6f561b4SSašo Kiselkov 3652a6f561b4SSašo Kiselkov if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3653a6f561b4SSašo Kiselkov return (err); 3654a6f561b4SSašo Kiselkov 3655a6f561b4SSašo Kiselkov if (!spa_feature_is_enabled(spa, feature)) { 3656a6f561b4SSašo Kiselkov spa_close(spa, FTAG); 3657be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3658a6f561b4SSašo Kiselkov } 3659a6f561b4SSašo Kiselkov spa_close(spa, FTAG); 3660a6f561b4SSašo Kiselkov } 3661a6f561b4SSašo Kiselkov 366292241e0bSTom Erickson /* 366392241e0bSTom Erickson * If this is a bootable dataset then 366492241e0bSTom Erickson * verify that the compression algorithm 366592241e0bSTom Erickson * is supported for booting. We must return 366692241e0bSTom Erickson * something other than ENOTSUP since it 366792241e0bSTom Erickson * implies a downrev pool version. 366892241e0bSTom Erickson */ 366992241e0bSTom Erickson if (zfs_is_bootfs(dsname) && 367092241e0bSTom Erickson !BOOTFS_COMPRESS_VALID(intval)) { 3671be6fd75aSMatthew Ahrens return (SET_ERROR(ERANGE)); 367292241e0bSTom Erickson } 367392241e0bSTom Erickson } 367492241e0bSTom Erickson break; 367592241e0bSTom Erickson 367692241e0bSTom Erickson case ZFS_PROP_COPIES: 367792241e0bSTom Erickson if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 3678be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 367992241e0bSTom Erickson break; 368092241e0bSTom Erickson 368192241e0bSTom Erickson case ZFS_PROP_DEDUP: 368292241e0bSTom Erickson if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 3683be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 368492241e0bSTom Erickson break; 368592241e0bSTom Erickson 368692241e0bSTom Erickson case ZFS_PROP_SHARESMB: 368792241e0bSTom Erickson if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 3688be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 368992241e0bSTom Erickson break; 369092241e0bSTom Erickson 369192241e0bSTom Erickson case ZFS_PROP_ACLINHERIT: 369292241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_UINT64 && 369392241e0bSTom Erickson nvpair_value_uint64(pair, &intval) == 0) { 369492241e0bSTom Erickson if (intval == ZFS_ACL_PASSTHROUGH_X && 369592241e0bSTom Erickson zfs_earlier_version(dsname, 369692241e0bSTom Erickson SPA_VERSION_PASSTHROUGH_X)) 3697be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 369892241e0bSTom Erickson } 369992241e0bSTom Erickson break; 370092241e0bSTom Erickson } 370192241e0bSTom Erickson 370292241e0bSTom Erickson return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 370392241e0bSTom Erickson } 370492241e0bSTom Erickson 3705a6f561b4SSašo Kiselkov /* 3706a6f561b4SSašo Kiselkov * Checks for a race condition to make sure we don't increment a feature flag 3707a6f561b4SSašo Kiselkov * multiple times. 3708a6f561b4SSašo Kiselkov */ 3709a6f561b4SSašo Kiselkov static int 37103b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx) 3711a6f561b4SSašo Kiselkov { 37123b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 37133b2aab18SMatthew Ahrens zfeature_info_t *feature = arg; 3714a6f561b4SSašo Kiselkov 3715a6f561b4SSašo Kiselkov if (!spa_feature_is_active(spa, feature)) 3716a6f561b4SSašo Kiselkov return (0); 3717a6f561b4SSašo Kiselkov else 3718be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 3719a6f561b4SSašo Kiselkov } 3720a6f561b4SSašo Kiselkov 3721a6f561b4SSašo Kiselkov /* 3722a6f561b4SSašo Kiselkov * The callback invoked on feature activation in the sync task caused by 3723a6f561b4SSašo Kiselkov * zfs_prop_activate_feature. 3724a6f561b4SSašo Kiselkov */ 3725a6f561b4SSašo Kiselkov static void 37263b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx) 3727a6f561b4SSašo Kiselkov { 37283b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 37293b2aab18SMatthew Ahrens zfeature_info_t *feature = arg; 3730a6f561b4SSašo Kiselkov 3731a6f561b4SSašo Kiselkov spa_feature_incr(spa, feature, tx); 3732a6f561b4SSašo Kiselkov } 3733a6f561b4SSašo Kiselkov 37343b2aab18SMatthew Ahrens /* 37353b2aab18SMatthew Ahrens * Activates a feature on a pool in response to a property setting. This 37363b2aab18SMatthew Ahrens * creates a new sync task which modifies the pool to reflect the feature 37373b2aab18SMatthew Ahrens * as being active. 37383b2aab18SMatthew Ahrens */ 37393b2aab18SMatthew Ahrens static int 37403b2aab18SMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature) 37413b2aab18SMatthew Ahrens { 37423b2aab18SMatthew Ahrens int err; 37433b2aab18SMatthew Ahrens 37443b2aab18SMatthew Ahrens /* EBUSY here indicates that the feature is already active */ 37453b2aab18SMatthew Ahrens err = dsl_sync_task(spa_name(spa), 37463b2aab18SMatthew Ahrens zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync, 37473b2aab18SMatthew Ahrens feature, 2); 37483b2aab18SMatthew Ahrens 37493b2aab18SMatthew Ahrens if (err != 0 && err != EBUSY) 37503b2aab18SMatthew Ahrens return (err); 37513b2aab18SMatthew Ahrens else 37523b2aab18SMatthew Ahrens return (0); 37533b2aab18SMatthew Ahrens } 37543b2aab18SMatthew Ahrens 375592241e0bSTom Erickson /* 375692241e0bSTom Erickson * Removes properties from the given props list that fail permission checks 375792241e0bSTom Erickson * needed to clear them and to restore them in case of a receive error. For each 375892241e0bSTom Erickson * property, make sure we have both set and inherit permissions. 375992241e0bSTom Erickson * 376092241e0bSTom Erickson * Returns the first error encountered if any permission checks fail. If the 376192241e0bSTom Erickson * caller provides a non-NULL errlist, it also gives the complete list of names 376292241e0bSTom Erickson * of all the properties that failed a permission check along with the 376392241e0bSTom Erickson * corresponding error numbers. The caller is responsible for freeing the 376492241e0bSTom Erickson * returned errlist. 376592241e0bSTom Erickson * 376692241e0bSTom Erickson * If every property checks out successfully, zero is returned and the list 376792241e0bSTom Erickson * pointed at by errlist is NULL. 376892241e0bSTom Erickson */ 376992241e0bSTom Erickson static int 377092241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 3771745cd3c5Smaybee { 3772745cd3c5Smaybee zfs_cmd_t *zc; 377392241e0bSTom Erickson nvpair_t *pair, *next_pair; 377492241e0bSTom Erickson nvlist_t *errors; 377592241e0bSTom Erickson int err, rv = 0; 3776745cd3c5Smaybee 3777745cd3c5Smaybee if (props == NULL) 377892241e0bSTom Erickson return (0); 377992241e0bSTom Erickson 378092241e0bSTom Erickson VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 378192241e0bSTom Erickson 3782745cd3c5Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 3783745cd3c5Smaybee (void) strcpy(zc->zc_name, dataset); 378492241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL); 378592241e0bSTom Erickson while (pair != NULL) { 378692241e0bSTom Erickson next_pair = nvlist_next_nvpair(props, pair); 378792241e0bSTom Erickson 378892241e0bSTom Erickson (void) strcpy(zc->zc_value, nvpair_name(pair)); 378992241e0bSTom Erickson if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 37904445fffbSMatthew Ahrens (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) { 379192241e0bSTom Erickson VERIFY(nvlist_remove_nvpair(props, pair) == 0); 379292241e0bSTom Erickson VERIFY(nvlist_add_int32(errors, 379392241e0bSTom Erickson zc->zc_value, err) == 0); 379492241e0bSTom Erickson } 379592241e0bSTom Erickson pair = next_pair; 3796745cd3c5Smaybee } 3797745cd3c5Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 379892241e0bSTom Erickson 379992241e0bSTom Erickson if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 380092241e0bSTom Erickson nvlist_free(errors); 380192241e0bSTom Erickson errors = NULL; 380292241e0bSTom Erickson } else { 380392241e0bSTom Erickson VERIFY(nvpair_value_int32(pair, &rv) == 0); 380492241e0bSTom Erickson } 380592241e0bSTom Erickson 380692241e0bSTom Erickson if (errlist == NULL) 380792241e0bSTom Erickson nvlist_free(errors); 380892241e0bSTom Erickson else 380992241e0bSTom Erickson *errlist = errors; 381092241e0bSTom Erickson 381192241e0bSTom Erickson return (rv); 381292241e0bSTom Erickson } 381392241e0bSTom Erickson 381492241e0bSTom Erickson static boolean_t 381592241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2) 381692241e0bSTom Erickson { 381792241e0bSTom Erickson if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 381892241e0bSTom Erickson /* dsl_prop_get_all_impl() format */ 381992241e0bSTom Erickson nvlist_t *attrs; 382092241e0bSTom Erickson VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 382192241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 382292241e0bSTom Erickson &p1) == 0); 382392241e0bSTom Erickson } 382492241e0bSTom Erickson 382592241e0bSTom Erickson if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 382692241e0bSTom Erickson nvlist_t *attrs; 382792241e0bSTom Erickson VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 382892241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 382992241e0bSTom Erickson &p2) == 0); 383092241e0bSTom Erickson } 383192241e0bSTom Erickson 383292241e0bSTom Erickson if (nvpair_type(p1) != nvpair_type(p2)) 383392241e0bSTom Erickson return (B_FALSE); 383492241e0bSTom Erickson 383592241e0bSTom Erickson if (nvpair_type(p1) == DATA_TYPE_STRING) { 383692241e0bSTom Erickson char *valstr1, *valstr2; 383792241e0bSTom Erickson 383892241e0bSTom Erickson VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 383992241e0bSTom Erickson VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 384092241e0bSTom Erickson return (strcmp(valstr1, valstr2) == 0); 384192241e0bSTom Erickson } else { 384292241e0bSTom Erickson uint64_t intval1, intval2; 384392241e0bSTom Erickson 384492241e0bSTom Erickson VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 384592241e0bSTom Erickson VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 384692241e0bSTom Erickson return (intval1 == intval2); 384792241e0bSTom Erickson } 3848745cd3c5Smaybee } 3849745cd3c5Smaybee 385092241e0bSTom Erickson /* 385192241e0bSTom Erickson * Remove properties from props if they are not going to change (as determined 385292241e0bSTom Erickson * by comparison with origprops). Remove them from origprops as well, since we 385392241e0bSTom Erickson * do not need to clear or restore properties that won't change. 385492241e0bSTom Erickson */ 385592241e0bSTom Erickson static void 385692241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops) 385792241e0bSTom Erickson { 385892241e0bSTom Erickson nvpair_t *pair, *next_pair; 385992241e0bSTom Erickson 386092241e0bSTom Erickson if (origprops == NULL) 386192241e0bSTom Erickson return; /* all props need to be received */ 386292241e0bSTom Erickson 386392241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL); 386492241e0bSTom Erickson while (pair != NULL) { 386592241e0bSTom Erickson const char *propname = nvpair_name(pair); 386692241e0bSTom Erickson nvpair_t *match; 386792241e0bSTom Erickson 386892241e0bSTom Erickson next_pair = nvlist_next_nvpair(props, pair); 386992241e0bSTom Erickson 387092241e0bSTom Erickson if ((nvlist_lookup_nvpair(origprops, propname, 387192241e0bSTom Erickson &match) != 0) || !propval_equals(pair, match)) 387292241e0bSTom Erickson goto next; /* need to set received value */ 387392241e0bSTom Erickson 387492241e0bSTom Erickson /* don't clear the existing received value */ 387592241e0bSTom Erickson (void) nvlist_remove_nvpair(origprops, match); 387692241e0bSTom Erickson /* don't bother receiving the property */ 387792241e0bSTom Erickson (void) nvlist_remove_nvpair(props, pair); 387892241e0bSTom Erickson next: 387992241e0bSTom Erickson pair = next_pair; 388092241e0bSTom Erickson } 388192241e0bSTom Erickson } 388292241e0bSTom Erickson 388392241e0bSTom Erickson #ifdef DEBUG 388492241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err; 388592241e0bSTom Erickson #endif 388692241e0bSTom Erickson 38873cb34c60Sahrens /* 38883cb34c60Sahrens * inputs: 38893cb34c60Sahrens * zc_name name of containing filesystem 38903cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 38913cb34c60Sahrens * zc_value name of snapshot to create 38923cb34c60Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 38933cb34c60Sahrens * zc_cookie file descriptor to recv from 38943cb34c60Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 38953cb34c60Sahrens * zc_guid force flag 3896c99e4bdcSChris Kirby * zc_cleanup_fd cleanup-on-exit file descriptor 3897c99e4bdcSChris Kirby * zc_action_handle handle for this guid/ds mapping (or zero on first call) 38983cb34c60Sahrens * 38993cb34c60Sahrens * outputs: 39003cb34c60Sahrens * zc_cookie number of bytes read 390192241e0bSTom Erickson * zc_nvlist_dst{_size} error for each unapplied received property 390292241e0bSTom Erickson * zc_obj zprop_errflags_t 3903c99e4bdcSChris Kirby * zc_action_handle handle for this guid/ds mapping 39043cb34c60Sahrens */ 3905fa9e4066Sahrens static int 39063cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 3907fa9e4066Sahrens { 3908fa9e4066Sahrens file_t *fp; 39093cb34c60Sahrens dmu_recv_cookie_t drc; 3910f18faf3fSek boolean_t force = (boolean_t)zc->zc_guid; 391192241e0bSTom Erickson int fd; 391292241e0bSTom Erickson int error = 0; 391392241e0bSTom Erickson int props_error = 0; 391492241e0bSTom Erickson nvlist_t *errors; 39153cb34c60Sahrens offset_t off; 391692241e0bSTom Erickson nvlist_t *props = NULL; /* sent properties */ 391792241e0bSTom Erickson nvlist_t *origprops = NULL; /* existing properties */ 39183b2aab18SMatthew Ahrens char *origin = NULL; 39193cb34c60Sahrens char *tosnap; 39203cb34c60Sahrens char tofs[ZFS_MAXNAMELEN]; 392192241e0bSTom Erickson boolean_t first_recvd_props = B_FALSE; 3922fa9e4066Sahrens 39233ccfa83cSahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3924f18faf3fSek strchr(zc->zc_value, '@') == NULL || 3925f18faf3fSek strchr(zc->zc_value, '%')) 3926be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 39273ccfa83cSahrens 39283cb34c60Sahrens (void) strcpy(tofs, zc->zc_value); 39293cb34c60Sahrens tosnap = strchr(tofs, '@'); 393092241e0bSTom Erickson *tosnap++ = '\0'; 39313cb34c60Sahrens 39323cb34c60Sahrens if (zc->zc_nvlist_src != NULL && 39333cb34c60Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 3934478ed9adSEric Taylor zc->zc_iflags, &props)) != 0) 39353cb34c60Sahrens return (error); 39363cb34c60Sahrens 3937fa9e4066Sahrens fd = zc->zc_cookie; 3938fa9e4066Sahrens fp = getf(fd); 39393cb34c60Sahrens if (fp == NULL) { 39403cb34c60Sahrens nvlist_free(props); 3941be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 39423cb34c60Sahrens } 3943f18faf3fSek 394492241e0bSTom Erickson VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 394592241e0bSTom Erickson 39463b2aab18SMatthew Ahrens if (zc->zc_string[0]) 39473b2aab18SMatthew Ahrens origin = zc->zc_string; 39483b2aab18SMatthew Ahrens 39493b2aab18SMatthew Ahrens error = dmu_recv_begin(tofs, tosnap, 39503b2aab18SMatthew Ahrens &zc->zc_begin_record, force, origin, &drc); 39513b2aab18SMatthew Ahrens if (error != 0) 39523b2aab18SMatthew Ahrens goto out; 39533b2aab18SMatthew Ahrens 39543b2aab18SMatthew Ahrens /* 39553b2aab18SMatthew Ahrens * Set properties before we receive the stream so that they are applied 39563b2aab18SMatthew Ahrens * to the new data. Note that we must call dmu_recv_stream() if 39573b2aab18SMatthew Ahrens * dmu_recv_begin() succeeds. 39583b2aab18SMatthew Ahrens */ 39593b2aab18SMatthew Ahrens if (props != NULL && !drc.drc_newfs) { 39603b2aab18SMatthew Ahrens if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >= 39613b2aab18SMatthew Ahrens SPA_VERSION_RECVD_PROPS && 39623b2aab18SMatthew Ahrens !dsl_prop_get_hasrecvd(tofs)) 396392241e0bSTom Erickson first_recvd_props = B_TRUE; 396492241e0bSTom Erickson 3965745cd3c5Smaybee /* 396692241e0bSTom Erickson * If new received properties are supplied, they are to 396792241e0bSTom Erickson * completely replace the existing received properties, so stash 396892241e0bSTom Erickson * away the existing ones. 3969745cd3c5Smaybee */ 39703b2aab18SMatthew Ahrens if (dsl_prop_get_received(tofs, &origprops) == 0) { 397192241e0bSTom Erickson nvlist_t *errlist = NULL; 397292241e0bSTom Erickson /* 397392241e0bSTom Erickson * Don't bother writing a property if its value won't 397492241e0bSTom Erickson * change (and avoid the unnecessary security checks). 397592241e0bSTom Erickson * 397692241e0bSTom Erickson * The first receive after SPA_VERSION_RECVD_PROPS is a 397792241e0bSTom Erickson * special case where we blow away all local properties 397892241e0bSTom Erickson * regardless. 397992241e0bSTom Erickson */ 398092241e0bSTom Erickson if (!first_recvd_props) 398192241e0bSTom Erickson props_reduce(props, origprops); 39823b2aab18SMatthew Ahrens if (zfs_check_clearable(tofs, origprops, &errlist) != 0) 398392241e0bSTom Erickson (void) nvlist_merge(errors, errlist, 0); 398492241e0bSTom Erickson nvlist_free(errlist); 39853cb34c60Sahrens 39863b2aab18SMatthew Ahrens if (clear_received_props(tofs, origprops, 39873b2aab18SMatthew Ahrens first_recvd_props ? NULL : props) != 0) 398892241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR; 39893b2aab18SMatthew Ahrens } else { 399092241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR; 399192241e0bSTom Erickson } 39923b2aab18SMatthew Ahrens } 399392241e0bSTom Erickson 39943b2aab18SMatthew Ahrens if (props != NULL) { 39953b2aab18SMatthew Ahrens props_error = dsl_prop_set_hasrecvd(tofs); 39963b2aab18SMatthew Ahrens 39973b2aab18SMatthew Ahrens if (props_error == 0) { 39983b2aab18SMatthew Ahrens (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 39993b2aab18SMatthew Ahrens props, errors); 40003b2aab18SMatthew Ahrens } 400192241e0bSTom Erickson } 400292241e0bSTom Erickson 40034445fffbSMatthew Ahrens if (zc->zc_nvlist_dst_size != 0 && 40044445fffbSMatthew Ahrens (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 || 40054445fffbSMatthew Ahrens put_nvlist(zc, errors) != 0)) { 4006745cd3c5Smaybee /* 400792241e0bSTom Erickson * Caller made zc->zc_nvlist_dst less than the minimum expected 400892241e0bSTom Erickson * size or supplied an invalid address. 4009745cd3c5Smaybee */ 4010be6fd75aSMatthew Ahrens props_error = SET_ERROR(EINVAL); 40113cb34c60Sahrens } 40123cb34c60Sahrens 40133cb34c60Sahrens off = fp->f_offset; 4014c99e4bdcSChris Kirby error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd, 4015c99e4bdcSChris Kirby &zc->zc_action_handle); 4016a2eea2e1Sahrens 4017f4b94bdeSMatthew Ahrens if (error == 0) { 4018f4b94bdeSMatthew Ahrens zfsvfs_t *zfsvfs = NULL; 4019745cd3c5Smaybee 4020f4b94bdeSMatthew Ahrens if (getzfsvfs(tofs, &zfsvfs) == 0) { 4021f4b94bdeSMatthew Ahrens /* online recv */ 4022f4b94bdeSMatthew Ahrens int end_err; 4023745cd3c5Smaybee 4024503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs); 4025f4b94bdeSMatthew Ahrens /* 4026f4b94bdeSMatthew Ahrens * If the suspend fails, then the recv_end will 4027f4b94bdeSMatthew Ahrens * likely also fail, and clean up after itself. 4028f4b94bdeSMatthew Ahrens */ 4029f4b94bdeSMatthew Ahrens end_err = dmu_recv_end(&drc); 40305c703fceSGeorge Wilson if (error == 0) 40315c703fceSGeorge Wilson error = zfs_resume_fs(zfsvfs, tofs); 4032f4b94bdeSMatthew Ahrens error = error ? error : end_err; 4033f4b94bdeSMatthew Ahrens VFS_RELE(zfsvfs->z_vfs); 4034745cd3c5Smaybee } else { 4035f4b94bdeSMatthew Ahrens error = dmu_recv_end(&drc); 40363cb34c60Sahrens } 403747f263f4Sek } 40383cb34c60Sahrens 40393cb34c60Sahrens zc->zc_cookie = off - fp->f_offset; 40403cb34c60Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 40413cb34c60Sahrens fp->f_offset = off; 4042a2eea2e1Sahrens 404392241e0bSTom Erickson #ifdef DEBUG 404492241e0bSTom Erickson if (zfs_ioc_recv_inject_err) { 404592241e0bSTom Erickson zfs_ioc_recv_inject_err = B_FALSE; 404692241e0bSTom Erickson error = 1; 404792241e0bSTom Erickson } 404892241e0bSTom Erickson #endif 4049745cd3c5Smaybee /* 4050745cd3c5Smaybee * On error, restore the original props. 4051745cd3c5Smaybee */ 40523b2aab18SMatthew Ahrens if (error != 0 && props != NULL && !drc.drc_newfs) { 40533b2aab18SMatthew Ahrens if (clear_received_props(tofs, props, NULL) != 0) { 40543b2aab18SMatthew Ahrens /* 40553b2aab18SMatthew Ahrens * We failed to clear the received properties. 40563b2aab18SMatthew Ahrens * Since we may have left a $recvd value on the 40573b2aab18SMatthew Ahrens * system, we can't clear the $hasrecvd flag. 40583b2aab18SMatthew Ahrens */ 405992241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 40603b2aab18SMatthew Ahrens } else if (first_recvd_props) { 40613b2aab18SMatthew Ahrens dsl_prop_unset_hasrecvd(tofs); 406292241e0bSTom Erickson } 406392241e0bSTom Erickson 406492241e0bSTom Erickson if (origprops == NULL && !drc.drc_newfs) { 406592241e0bSTom Erickson /* We failed to stash the original properties. */ 406692241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 406792241e0bSTom Erickson } 406892241e0bSTom Erickson 406992241e0bSTom Erickson /* 407092241e0bSTom Erickson * dsl_props_set() will not convert RECEIVED to LOCAL on or 407192241e0bSTom Erickson * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 407292241e0bSTom Erickson * explictly if we're restoring local properties cleared in the 407392241e0bSTom Erickson * first new-style receive. 407492241e0bSTom Erickson */ 407592241e0bSTom Erickson if (origprops != NULL && 407692241e0bSTom Erickson zfs_set_prop_nvlist(tofs, (first_recvd_props ? 407792241e0bSTom Erickson ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 407892241e0bSTom Erickson origprops, NULL) != 0) { 407992241e0bSTom Erickson /* 408092241e0bSTom Erickson * We stashed the original properties but failed to 408192241e0bSTom Erickson * restore them. 408292241e0bSTom Erickson */ 408392241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE; 408492241e0bSTom Erickson } 4085745cd3c5Smaybee } 4086745cd3c5Smaybee out: 4087745cd3c5Smaybee nvlist_free(props); 4088745cd3c5Smaybee nvlist_free(origprops); 408992241e0bSTom Erickson nvlist_free(errors); 4090fa9e4066Sahrens releasef(fd); 409192241e0bSTom Erickson 409292241e0bSTom Erickson if (error == 0) 409392241e0bSTom Erickson error = props_error; 409492241e0bSTom Erickson 4095fa9e4066Sahrens return (error); 4096fa9e4066Sahrens } 4097fa9e4066Sahrens 40983cb34c60Sahrens /* 40993cb34c60Sahrens * inputs: 41003cb34c60Sahrens * zc_name name of snapshot to send 41013cb34c60Sahrens * zc_cookie file descriptor to send stream to 4102a7f53a56SChris Kirby * zc_obj fromorigin flag (mutually exclusive with zc_fromobj) 4103a7f53a56SChris Kirby * zc_sendobj objsetid of snapshot to send 4104a7f53a56SChris Kirby * zc_fromobj objsetid of incremental fromsnap (may be zero) 410519b94df9SMatthew Ahrens * zc_guid if set, estimate size of stream only. zc_cookie is ignored. 410619b94df9SMatthew Ahrens * output size in zc_objset_type. 41073cb34c60Sahrens * 41083cb34c60Sahrens * outputs: none 41093cb34c60Sahrens */ 4110fa9e4066Sahrens static int 41113cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc) 4112fa9e4066Sahrens { 4113fa9e4066Sahrens int error; 41143cb34c60Sahrens offset_t off; 411519b94df9SMatthew Ahrens boolean_t estimate = (zc->zc_guid != 0); 4116fa9e4066Sahrens 41173b2aab18SMatthew Ahrens if (zc->zc_obj != 0) { 41183b2aab18SMatthew Ahrens dsl_pool_t *dp; 41193b2aab18SMatthew Ahrens dsl_dataset_t *tosnap; 4120a7f53a56SChris Kirby 41213b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 41223b2aab18SMatthew Ahrens if (error != 0) 4123a7f53a56SChris Kirby return (error); 41243b2aab18SMatthew Ahrens 41253b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); 41263b2aab18SMatthew Ahrens if (error != 0) { 41273b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 4128fa9e4066Sahrens return (error); 4129fa9e4066Sahrens } 41303b2aab18SMatthew Ahrens 41313b2aab18SMatthew Ahrens if (dsl_dir_is_clone(tosnap->ds_dir)) 41323b2aab18SMatthew Ahrens zc->zc_fromobj = tosnap->ds_dir->dd_phys->dd_origin_obj; 41333b2aab18SMatthew Ahrens dsl_dataset_rele(tosnap, FTAG); 41343b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 41354445fffbSMatthew Ahrens } 41364445fffbSMatthew Ahrens 41373b2aab18SMatthew Ahrens if (estimate) { 41383b2aab18SMatthew Ahrens dsl_pool_t *dp; 41393b2aab18SMatthew Ahrens dsl_dataset_t *tosnap; 41403b2aab18SMatthew Ahrens dsl_dataset_t *fromsnap = NULL; 41414445fffbSMatthew Ahrens 41423b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 41433b2aab18SMatthew Ahrens if (error != 0) 41443b2aab18SMatthew Ahrens return (error); 41453b2aab18SMatthew Ahrens 41463b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); 41473b2aab18SMatthew Ahrens if (error != 0) { 41483b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 41493b2aab18SMatthew Ahrens return (error); 41504445fffbSMatthew Ahrens } 41514445fffbSMatthew Ahrens 41523b2aab18SMatthew Ahrens if (zc->zc_fromobj != 0) { 41533b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, 41543b2aab18SMatthew Ahrens FTAG, &fromsnap); 41553b2aab18SMatthew Ahrens if (error != 0) { 41563b2aab18SMatthew Ahrens dsl_dataset_rele(tosnap, FTAG); 41573b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 41584445fffbSMatthew Ahrens return (error); 41594445fffbSMatthew Ahrens } 41604445fffbSMatthew Ahrens } 4161fa9e4066Sahrens 41624445fffbSMatthew Ahrens error = dmu_send_estimate(tosnap, fromsnap, 416319b94df9SMatthew Ahrens &zc->zc_objset_type); 41643b2aab18SMatthew Ahrens 41653b2aab18SMatthew Ahrens if (fromsnap != NULL) 41663b2aab18SMatthew Ahrens dsl_dataset_rele(fromsnap, FTAG); 41673b2aab18SMatthew Ahrens dsl_dataset_rele(tosnap, FTAG); 41683b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 416919b94df9SMatthew Ahrens } else { 417019b94df9SMatthew Ahrens file_t *fp = getf(zc->zc_cookie); 41713b2aab18SMatthew Ahrens if (fp == NULL) 4172be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 4173fa9e4066Sahrens 417419b94df9SMatthew Ahrens off = fp->f_offset; 41753b2aab18SMatthew Ahrens error = dmu_send_obj(zc->zc_name, zc->zc_sendobj, 41763b2aab18SMatthew Ahrens zc->zc_fromobj, zc->zc_cookie, fp->f_vnode, &off); 4177fa9e4066Sahrens 417819b94df9SMatthew Ahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 417919b94df9SMatthew Ahrens fp->f_offset = off; 418019b94df9SMatthew Ahrens releasef(zc->zc_cookie); 418119b94df9SMatthew Ahrens } 4182fa9e4066Sahrens return (error); 4183fa9e4066Sahrens } 4184fa9e4066Sahrens 41854e3c9f44SBill Pijewski /* 41864e3c9f44SBill Pijewski * inputs: 41874e3c9f44SBill Pijewski * zc_name name of snapshot on which to report progress 41884e3c9f44SBill Pijewski * zc_cookie file descriptor of send stream 41894e3c9f44SBill Pijewski * 41904e3c9f44SBill Pijewski * outputs: 41914e3c9f44SBill Pijewski * zc_cookie number of bytes written in send stream thus far 41924e3c9f44SBill Pijewski */ 41934e3c9f44SBill Pijewski static int 41944e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc) 41954e3c9f44SBill Pijewski { 41963b2aab18SMatthew Ahrens dsl_pool_t *dp; 41974e3c9f44SBill Pijewski dsl_dataset_t *ds; 41984e3c9f44SBill Pijewski dmu_sendarg_t *dsp = NULL; 41994e3c9f44SBill Pijewski int error; 42004e3c9f44SBill Pijewski 42013b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 42023b2aab18SMatthew Ahrens if (error != 0) 42033b2aab18SMatthew Ahrens return (error); 42043b2aab18SMatthew Ahrens 42053b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds); 42063b2aab18SMatthew Ahrens if (error != 0) { 42073b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 42084e3c9f44SBill Pijewski return (error); 42093b2aab18SMatthew Ahrens } 42104e3c9f44SBill Pijewski 42114e3c9f44SBill Pijewski mutex_enter(&ds->ds_sendstream_lock); 42124e3c9f44SBill Pijewski 42134e3c9f44SBill Pijewski /* 42144e3c9f44SBill Pijewski * Iterate over all the send streams currently active on this dataset. 42154e3c9f44SBill Pijewski * If there's one which matches the specified file descriptor _and_ the 42164e3c9f44SBill Pijewski * stream was started by the current process, return the progress of 42174e3c9f44SBill Pijewski * that stream. 42184e3c9f44SBill Pijewski */ 42194e3c9f44SBill Pijewski for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL; 42204e3c9f44SBill Pijewski dsp = list_next(&ds->ds_sendstreams, dsp)) { 42214e3c9f44SBill Pijewski if (dsp->dsa_outfd == zc->zc_cookie && 42224e3c9f44SBill Pijewski dsp->dsa_proc == curproc) 42234e3c9f44SBill Pijewski break; 42244e3c9f44SBill Pijewski } 42254e3c9f44SBill Pijewski 42264e3c9f44SBill Pijewski if (dsp != NULL) 42274e3c9f44SBill Pijewski zc->zc_cookie = *(dsp->dsa_off); 42284e3c9f44SBill Pijewski else 4229be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 42304e3c9f44SBill Pijewski 42314e3c9f44SBill Pijewski mutex_exit(&ds->ds_sendstream_lock); 42324e3c9f44SBill Pijewski dsl_dataset_rele(ds, FTAG); 42333b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 42344e3c9f44SBill Pijewski return (error); 42354e3c9f44SBill Pijewski } 42364e3c9f44SBill Pijewski 4237ea8dc4b6Seschrock static int 4238ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 4239ea8dc4b6Seschrock { 4240ea8dc4b6Seschrock int id, error; 4241ea8dc4b6Seschrock 4242ea8dc4b6Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 4243ea8dc4b6Seschrock &zc->zc_inject_record); 4244ea8dc4b6Seschrock 4245ea8dc4b6Seschrock if (error == 0) 4246ea8dc4b6Seschrock zc->zc_guid = (uint64_t)id; 4247ea8dc4b6Seschrock 4248ea8dc4b6Seschrock return (error); 4249ea8dc4b6Seschrock } 4250ea8dc4b6Seschrock 4251ea8dc4b6Seschrock static int 4252ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 4253ea8dc4b6Seschrock { 4254ea8dc4b6Seschrock return (zio_clear_fault((int)zc->zc_guid)); 4255ea8dc4b6Seschrock } 4256ea8dc4b6Seschrock 4257ea8dc4b6Seschrock static int 4258ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 4259ea8dc4b6Seschrock { 4260ea8dc4b6Seschrock int id = (int)zc->zc_guid; 4261ea8dc4b6Seschrock int error; 4262ea8dc4b6Seschrock 4263ea8dc4b6Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 4264ea8dc4b6Seschrock &zc->zc_inject_record); 4265ea8dc4b6Seschrock 4266ea8dc4b6Seschrock zc->zc_guid = id; 4267ea8dc4b6Seschrock 4268ea8dc4b6Seschrock return (error); 4269ea8dc4b6Seschrock } 4270ea8dc4b6Seschrock 4271ea8dc4b6Seschrock static int 4272ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 4273ea8dc4b6Seschrock { 4274ea8dc4b6Seschrock spa_t *spa; 4275ea8dc4b6Seschrock int error; 4276e9dbad6fSeschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 4277ea8dc4b6Seschrock 4278ea8dc4b6Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 4279ea8dc4b6Seschrock return (error); 4280ea8dc4b6Seschrock 4281e9dbad6fSeschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 4282ea8dc4b6Seschrock &count); 4283ea8dc4b6Seschrock if (error == 0) 4284e9dbad6fSeschrock zc->zc_nvlist_dst_size = count; 4285ea8dc4b6Seschrock else 4286e9dbad6fSeschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 4287ea8dc4b6Seschrock 4288ea8dc4b6Seschrock spa_close(spa, FTAG); 4289ea8dc4b6Seschrock 4290ea8dc4b6Seschrock return (error); 4291ea8dc4b6Seschrock } 4292ea8dc4b6Seschrock 4293ea8dc4b6Seschrock static int 4294ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 4295ea8dc4b6Seschrock { 4296ea8dc4b6Seschrock spa_t *spa; 4297ea8dc4b6Seschrock vdev_t *vd; 4298bb8b5132Sek int error; 4299ea8dc4b6Seschrock 4300b87f3af3Sperrin /* 4301b87f3af3Sperrin * On zpool clear we also fix up missing slogs 4302b87f3af3Sperrin */ 4303b87f3af3Sperrin mutex_enter(&spa_namespace_lock); 4304b87f3af3Sperrin spa = spa_lookup(zc->zc_name); 4305b87f3af3Sperrin if (spa == NULL) { 4306b87f3af3Sperrin mutex_exit(&spa_namespace_lock); 4307be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 4308b87f3af3Sperrin } 4309b24ab676SJeff Bonwick if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 4310b87f3af3Sperrin /* we need to let spa_open/spa_load clear the chains */ 4311b24ab676SJeff Bonwick spa_set_log_state(spa, SPA_LOG_CLEAR); 4312b87f3af3Sperrin } 4313468c413aSTim Haley spa->spa_last_open_failed = 0; 4314b87f3af3Sperrin mutex_exit(&spa_namespace_lock); 4315b87f3af3Sperrin 4316c8ee1847SVictor Latushkin if (zc->zc_cookie & ZPOOL_NO_REWIND) { 4317468c413aSTim Haley error = spa_open(zc->zc_name, &spa, FTAG); 4318468c413aSTim Haley } else { 4319468c413aSTim Haley nvlist_t *policy; 4320468c413aSTim Haley nvlist_t *config = NULL; 4321468c413aSTim Haley 4322468c413aSTim Haley if (zc->zc_nvlist_src == NULL) 4323be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 4324468c413aSTim Haley 4325468c413aSTim Haley if ((error = get_nvlist(zc->zc_nvlist_src, 4326468c413aSTim Haley zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 4327468c413aSTim Haley error = spa_open_rewind(zc->zc_name, &spa, FTAG, 4328468c413aSTim Haley policy, &config); 4329468c413aSTim Haley if (config != NULL) { 43304b964adaSGeorge Wilson int err; 43314b964adaSGeorge Wilson 43324b964adaSGeorge Wilson if ((err = put_nvlist(zc, config)) != 0) 43334b964adaSGeorge Wilson error = err; 4334468c413aSTim Haley nvlist_free(config); 4335468c413aSTim Haley } 4336468c413aSTim Haley nvlist_free(policy); 4337468c413aSTim Haley } 4338468c413aSTim Haley } 4339468c413aSTim Haley 43403b2aab18SMatthew Ahrens if (error != 0) 4341ea8dc4b6Seschrock return (error); 4342ea8dc4b6Seschrock 43438f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 4344ea8dc4b6Seschrock 4345e9dbad6fSeschrock if (zc->zc_guid == 0) { 4346ea8dc4b6Seschrock vd = NULL; 4347c5904d13Seschrock } else { 4348c5904d13Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 4349fa94a07fSbrendan if (vd == NULL) { 4350e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, ENODEV); 4351fa94a07fSbrendan spa_close(spa, FTAG); 4352be6fd75aSMatthew Ahrens return (SET_ERROR(ENODEV)); 4353fa94a07fSbrendan } 4354ea8dc4b6Seschrock } 4355ea8dc4b6Seschrock 4356e14bb325SJeff Bonwick vdev_clear(spa, vd); 4357e14bb325SJeff Bonwick 4358e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 4359ea8dc4b6Seschrock 4360e14bb325SJeff Bonwick /* 4361e14bb325SJeff Bonwick * Resume any suspended I/Os. 4362e14bb325SJeff Bonwick */ 436354d692b7SGeorge Wilson if (zio_resume(spa) != 0) 4364be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 4365ea8dc4b6Seschrock 4366ea8dc4b6Seschrock spa_close(spa, FTAG); 4367ea8dc4b6Seschrock 436854d692b7SGeorge Wilson return (error); 4369ea8dc4b6Seschrock } 4370ea8dc4b6Seschrock 43714263d13fSGeorge Wilson static int 43724263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc) 43734263d13fSGeorge Wilson { 43744263d13fSGeorge Wilson spa_t *spa; 43754263d13fSGeorge Wilson int error; 43764263d13fSGeorge Wilson 43774263d13fSGeorge Wilson error = spa_open(zc->zc_name, &spa, FTAG); 43783b2aab18SMatthew Ahrens if (error != 0) 43794263d13fSGeorge Wilson return (error); 43804263d13fSGeorge Wilson 43814263d13fSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 4382d6afdce2SGeorge Wilson 4383d6afdce2SGeorge Wilson /* 4384d6afdce2SGeorge Wilson * If a resilver is already in progress then set the 4385d6afdce2SGeorge Wilson * spa_scrub_reopen flag to B_TRUE so that we don't restart 4386d6afdce2SGeorge Wilson * the scan as a side effect of the reopen. Otherwise, let 4387d6afdce2SGeorge Wilson * vdev_open() decided if a resilver is required. 4388d6afdce2SGeorge Wilson */ 4389d6afdce2SGeorge Wilson spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool); 43904263d13fSGeorge Wilson vdev_reopen(spa->spa_root_vdev); 4391d6afdce2SGeorge Wilson spa->spa_scrub_reopen = B_FALSE; 4392d6afdce2SGeorge Wilson 43934263d13fSGeorge Wilson (void) spa_vdev_state_exit(spa, NULL, 0); 43944263d13fSGeorge Wilson spa_close(spa, FTAG); 43954263d13fSGeorge Wilson return (0); 43964263d13fSGeorge Wilson } 43973cb34c60Sahrens /* 43983cb34c60Sahrens * inputs: 43993cb34c60Sahrens * zc_name name of filesystem 44003cb34c60Sahrens * zc_value name of origin snapshot 44013cb34c60Sahrens * 4402681d9761SEric Taylor * outputs: 4403681d9761SEric Taylor * zc_string name of conflicting snapshot, if there is one 44043cb34c60Sahrens */ 440599653d4eSeschrock static int 440699653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc) 440799653d4eSeschrock { 44080b69c2f0Sahrens char *cp; 44090b69c2f0Sahrens 44100b69c2f0Sahrens /* 44110b69c2f0Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 44120b69c2f0Sahrens * it's easier. 44130b69c2f0Sahrens */ 4414e9dbad6fSeschrock cp = strchr(zc->zc_value, '@'); 44150b69c2f0Sahrens if (cp) 44160b69c2f0Sahrens *cp = '\0'; 4417e9dbad6fSeschrock (void) dmu_objset_find(zc->zc_value, 44183b2aab18SMatthew Ahrens zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS); 4419681d9761SEric Taylor return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 442099653d4eSeschrock } 442199653d4eSeschrock 442214843421SMatthew Ahrens /* 442314843421SMatthew Ahrens * Retrieve a single {user|group}{used|quota}@... property. 442414843421SMatthew Ahrens * 442514843421SMatthew Ahrens * inputs: 442614843421SMatthew Ahrens * zc_name name of filesystem 442714843421SMatthew Ahrens * zc_objset_type zfs_userquota_prop_t 442814843421SMatthew Ahrens * zc_value domain name (eg. "S-1-234-567-89") 442914843421SMatthew Ahrens * zc_guid RID/UID/GID 443014843421SMatthew Ahrens * 443114843421SMatthew Ahrens * outputs: 443214843421SMatthew Ahrens * zc_cookie property value 443314843421SMatthew Ahrens */ 443414843421SMatthew Ahrens static int 443514843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc) 443614843421SMatthew Ahrens { 443714843421SMatthew Ahrens zfsvfs_t *zfsvfs; 443814843421SMatthew Ahrens int error; 443914843421SMatthew Ahrens 444014843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 4441be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 444214843421SMatthew Ahrens 44431412a1a2SMark Shellenbaum error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 44443b2aab18SMatthew Ahrens if (error != 0) 444514843421SMatthew Ahrens return (error); 444614843421SMatthew Ahrens 444714843421SMatthew Ahrens error = zfs_userspace_one(zfsvfs, 444814843421SMatthew Ahrens zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 444914843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG); 445014843421SMatthew Ahrens 445114843421SMatthew Ahrens return (error); 445214843421SMatthew Ahrens } 445314843421SMatthew Ahrens 445414843421SMatthew Ahrens /* 445514843421SMatthew Ahrens * inputs: 445614843421SMatthew Ahrens * zc_name name of filesystem 445714843421SMatthew Ahrens * zc_cookie zap cursor 445814843421SMatthew Ahrens * zc_objset_type zfs_userquota_prop_t 445914843421SMatthew Ahrens * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 446014843421SMatthew Ahrens * 446114843421SMatthew Ahrens * outputs: 446214843421SMatthew Ahrens * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 446314843421SMatthew Ahrens * zc_cookie zap cursor 446414843421SMatthew Ahrens */ 446514843421SMatthew Ahrens static int 446614843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc) 446714843421SMatthew Ahrens { 446814843421SMatthew Ahrens zfsvfs_t *zfsvfs; 4469eeb85002STim Haley int bufsize = zc->zc_nvlist_dst_size; 447014843421SMatthew Ahrens 4471eeb85002STim Haley if (bufsize <= 0) 4472be6fd75aSMatthew Ahrens return (SET_ERROR(ENOMEM)); 4473eeb85002STim Haley 44741412a1a2SMark Shellenbaum int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 44753b2aab18SMatthew Ahrens if (error != 0) 447614843421SMatthew Ahrens return (error); 447714843421SMatthew Ahrens 447814843421SMatthew Ahrens void *buf = kmem_alloc(bufsize, KM_SLEEP); 447914843421SMatthew Ahrens 448014843421SMatthew Ahrens error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 448114843421SMatthew Ahrens buf, &zc->zc_nvlist_dst_size); 448214843421SMatthew Ahrens 448314843421SMatthew Ahrens if (error == 0) { 448414843421SMatthew Ahrens error = xcopyout(buf, 448514843421SMatthew Ahrens (void *)(uintptr_t)zc->zc_nvlist_dst, 448614843421SMatthew Ahrens zc->zc_nvlist_dst_size); 448714843421SMatthew Ahrens } 448814843421SMatthew Ahrens kmem_free(buf, bufsize); 448914843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG); 449014843421SMatthew Ahrens 449114843421SMatthew Ahrens return (error); 449214843421SMatthew Ahrens } 449314843421SMatthew Ahrens 449414843421SMatthew Ahrens /* 449514843421SMatthew Ahrens * inputs: 449614843421SMatthew Ahrens * zc_name name of filesystem 449714843421SMatthew Ahrens * 449814843421SMatthew Ahrens * outputs: 449914843421SMatthew Ahrens * none 450014843421SMatthew Ahrens */ 450114843421SMatthew Ahrens static int 450214843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 450314843421SMatthew Ahrens { 450414843421SMatthew Ahrens objset_t *os; 45051195e687SMark J Musante int error = 0; 450614843421SMatthew Ahrens zfsvfs_t *zfsvfs; 450714843421SMatthew Ahrens 450814843421SMatthew Ahrens if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 4509503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 451014843421SMatthew Ahrens /* 451114843421SMatthew Ahrens * If userused is not enabled, it may be because the 451214843421SMatthew Ahrens * objset needs to be closed & reopened (to grow the 451314843421SMatthew Ahrens * objset_phys_t). Suspend/resume the fs will do that. 451414843421SMatthew Ahrens */ 4515503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs); 4516503ad85cSMatthew Ahrens if (error == 0) 4517503ad85cSMatthew Ahrens error = zfs_resume_fs(zfsvfs, zc->zc_name); 451814843421SMatthew Ahrens } 451914843421SMatthew Ahrens if (error == 0) 452014843421SMatthew Ahrens error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 452114843421SMatthew Ahrens VFS_RELE(zfsvfs->z_vfs); 452214843421SMatthew Ahrens } else { 4523503ad85cSMatthew Ahrens /* XXX kind of reading contents without owning */ 4524503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os); 45253b2aab18SMatthew Ahrens if (error != 0) 452614843421SMatthew Ahrens return (error); 452714843421SMatthew Ahrens 452814843421SMatthew Ahrens error = dmu_objset_userspace_upgrade(os); 4529503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 453014843421SMatthew Ahrens } 453114843421SMatthew Ahrens 453214843421SMatthew Ahrens return (error); 453314843421SMatthew Ahrens } 453414843421SMatthew Ahrens 4535ecd6cf80Smarks /* 4536ecd6cf80Smarks * We don't want to have a hard dependency 4537ecd6cf80Smarks * against some special symbols in sharefs 4538da6c28aaSamw * nfs, and smbsrv. Determine them if needed when 4539ecd6cf80Smarks * the first file system is shared. 4540da6c28aaSamw * Neither sharefs, nfs or smbsrv are unloadable modules. 4541ecd6cf80Smarks */ 4542da6c28aaSamw int (*znfsexport_fs)(void *arg); 4543ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 4544da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 4545da6c28aaSamw 4546da6c28aaSamw int zfs_nfsshare_inited; 4547da6c28aaSamw int zfs_smbshare_inited; 4548ecd6cf80Smarks 4549ecd6cf80Smarks ddi_modhandle_t nfs_mod; 4550ecd6cf80Smarks ddi_modhandle_t sharefs_mod; 4551da6c28aaSamw ddi_modhandle_t smbsrv_mod; 4552ecd6cf80Smarks kmutex_t zfs_share_lock; 4553ecd6cf80Smarks 4554da6c28aaSamw static int 4555da6c28aaSamw zfs_init_sharefs() 4556da6c28aaSamw { 4557da6c28aaSamw int error; 4558da6c28aaSamw 4559da6c28aaSamw ASSERT(MUTEX_HELD(&zfs_share_lock)); 4560da6c28aaSamw /* Both NFS and SMB shares also require sharetab support. */ 4561da6c28aaSamw if (sharefs_mod == NULL && ((sharefs_mod = 4562da6c28aaSamw ddi_modopen("fs/sharefs", 4563da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) { 4564be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS)); 4565da6c28aaSamw } 4566da6c28aaSamw if (zshare_fs == NULL && ((zshare_fs = 4567da6c28aaSamw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 4568da6c28aaSamw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 4569be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS)); 4570da6c28aaSamw } 4571da6c28aaSamw return (0); 4572da6c28aaSamw } 4573da6c28aaSamw 4574ecd6cf80Smarks static int 4575ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc) 4576ecd6cf80Smarks { 4577ecd6cf80Smarks int error; 4578ecd6cf80Smarks int opcode; 4579ecd6cf80Smarks 4580da6c28aaSamw switch (zc->zc_share.z_sharetype) { 4581da6c28aaSamw case ZFS_SHARE_NFS: 4582da6c28aaSamw case ZFS_UNSHARE_NFS: 4583da6c28aaSamw if (zfs_nfsshare_inited == 0) { 4584da6c28aaSamw mutex_enter(&zfs_share_lock); 4585da6c28aaSamw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 4586da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) { 4587da6c28aaSamw mutex_exit(&zfs_share_lock); 4588be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS)); 4589da6c28aaSamw } 4590da6c28aaSamw if (znfsexport_fs == NULL && 4591da6c28aaSamw ((znfsexport_fs = (int (*)(void *)) 4592da6c28aaSamw ddi_modsym(nfs_mod, 4593da6c28aaSamw "nfs_export", &error)) == NULL)) { 4594da6c28aaSamw mutex_exit(&zfs_share_lock); 4595be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS)); 4596da6c28aaSamw } 4597da6c28aaSamw error = zfs_init_sharefs(); 45983b2aab18SMatthew Ahrens if (error != 0) { 4599da6c28aaSamw mutex_exit(&zfs_share_lock); 4600be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS)); 4601da6c28aaSamw } 4602da6c28aaSamw zfs_nfsshare_inited = 1; 4603ecd6cf80Smarks mutex_exit(&zfs_share_lock); 4604ecd6cf80Smarks } 4605da6c28aaSamw break; 4606da6c28aaSamw case ZFS_SHARE_SMB: 4607da6c28aaSamw case ZFS_UNSHARE_SMB: 4608da6c28aaSamw if (zfs_smbshare_inited == 0) { 4609da6c28aaSamw mutex_enter(&zfs_share_lock); 4610da6c28aaSamw if (smbsrv_mod == NULL && ((smbsrv_mod = 4611da6c28aaSamw ddi_modopen("drv/smbsrv", 4612da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) { 4613da6c28aaSamw mutex_exit(&zfs_share_lock); 4614be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS)); 4615da6c28aaSamw } 4616da6c28aaSamw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 4617da6c28aaSamw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 4618faa1795aSjb "smb_server_share", &error)) == NULL)) { 4619da6c28aaSamw mutex_exit(&zfs_share_lock); 4620be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS)); 4621da6c28aaSamw } 4622da6c28aaSamw error = zfs_init_sharefs(); 46233b2aab18SMatthew Ahrens if (error != 0) { 4624da6c28aaSamw mutex_exit(&zfs_share_lock); 4625be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS)); 4626da6c28aaSamw } 4627da6c28aaSamw zfs_smbshare_inited = 1; 4628ecd6cf80Smarks mutex_exit(&zfs_share_lock); 4629ecd6cf80Smarks } 4630da6c28aaSamw break; 4631da6c28aaSamw default: 4632be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 4633da6c28aaSamw } 4634ecd6cf80Smarks 4635da6c28aaSamw switch (zc->zc_share.z_sharetype) { 4636da6c28aaSamw case ZFS_SHARE_NFS: 4637da6c28aaSamw case ZFS_UNSHARE_NFS: 4638da6c28aaSamw if (error = 4639da6c28aaSamw znfsexport_fs((void *) 4640da6c28aaSamw (uintptr_t)zc->zc_share.z_exportdata)) 4641da6c28aaSamw return (error); 4642da6c28aaSamw break; 4643da6c28aaSamw case ZFS_SHARE_SMB: 4644da6c28aaSamw case ZFS_UNSHARE_SMB: 4645da6c28aaSamw if (error = zsmbexport_fs((void *) 4646da6c28aaSamw (uintptr_t)zc->zc_share.z_exportdata, 4647da6c28aaSamw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 4648743a77edSAlan Wright B_TRUE: B_FALSE)) { 4649da6c28aaSamw return (error); 4650ecd6cf80Smarks } 4651da6c28aaSamw break; 4652ecd6cf80Smarks } 4653ecd6cf80Smarks 4654da6c28aaSamw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 4655da6c28aaSamw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 4656ecd6cf80Smarks SHAREFS_ADD : SHAREFS_REMOVE; 4657ecd6cf80Smarks 4658da6c28aaSamw /* 4659da6c28aaSamw * Add or remove share from sharetab 4660da6c28aaSamw */ 4661ecd6cf80Smarks error = zshare_fs(opcode, 4662ecd6cf80Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 4663ecd6cf80Smarks zc->zc_share.z_sharemax); 4664ecd6cf80Smarks 4665ecd6cf80Smarks return (error); 4666ecd6cf80Smarks 4667ecd6cf80Smarks } 4668ecd6cf80Smarks 4669743a77edSAlan Wright ace_t full_access[] = { 4670743a77edSAlan Wright {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 4671743a77edSAlan Wright }; 4672743a77edSAlan Wright 467399d5e173STim Haley /* 467499d5e173STim Haley * inputs: 467599d5e173STim Haley * zc_name name of containing filesystem 467699d5e173STim Haley * zc_obj object # beyond which we want next in-use object # 467799d5e173STim Haley * 467899d5e173STim Haley * outputs: 467999d5e173STim Haley * zc_obj next in-use object # 468099d5e173STim Haley */ 468199d5e173STim Haley static int 468299d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc) 468399d5e173STim Haley { 468499d5e173STim Haley objset_t *os = NULL; 468599d5e173STim Haley int error; 468699d5e173STim Haley 468799d5e173STim Haley error = dmu_objset_hold(zc->zc_name, FTAG, &os); 46883b2aab18SMatthew Ahrens if (error != 0) 468999d5e173STim Haley return (error); 469099d5e173STim Haley 469199d5e173STim Haley error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 469299d5e173STim Haley os->os_dsl_dataset->ds_phys->ds_prev_snap_txg); 469399d5e173STim Haley 469499d5e173STim Haley dmu_objset_rele(os, FTAG); 469599d5e173STim Haley return (error); 469699d5e173STim Haley } 469799d5e173STim Haley 469899d5e173STim Haley /* 469999d5e173STim Haley * inputs: 470099d5e173STim Haley * zc_name name of filesystem 470199d5e173STim Haley * zc_value prefix name for snapshot 470299d5e173STim Haley * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 470399d5e173STim Haley * 470499d5e173STim Haley * outputs: 47054445fffbSMatthew Ahrens * zc_value short name of new snapshot 470699d5e173STim Haley */ 470799d5e173STim Haley static int 470899d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc) 470999d5e173STim Haley { 471099d5e173STim Haley char *snap_name; 47113b2aab18SMatthew Ahrens char *hold_name; 471299d5e173STim Haley int error; 47133b2aab18SMatthew Ahrens minor_t minor; 471499d5e173STim Haley 47153b2aab18SMatthew Ahrens error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor); 47163b2aab18SMatthew Ahrens if (error != 0) 471799d5e173STim Haley return (error); 471899d5e173STim Haley 47193b2aab18SMatthew Ahrens snap_name = kmem_asprintf("%s-%016llx", zc->zc_value, 47203b2aab18SMatthew Ahrens (u_longlong_t)ddi_get_lbolt64()); 47213b2aab18SMatthew Ahrens hold_name = kmem_asprintf("%%%s", zc->zc_value); 47223b2aab18SMatthew Ahrens 47233b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor, 47243b2aab18SMatthew Ahrens hold_name); 47253b2aab18SMatthew Ahrens if (error == 0) 47263b2aab18SMatthew Ahrens (void) strcpy(zc->zc_value, snap_name); 472799d5e173STim Haley strfree(snap_name); 47283b2aab18SMatthew Ahrens strfree(hold_name); 47293b2aab18SMatthew Ahrens zfs_onexit_fd_rele(zc->zc_cleanup_fd); 47303b2aab18SMatthew Ahrens return (error); 473199d5e173STim Haley } 473299d5e173STim Haley 473399d5e173STim Haley /* 473499d5e173STim Haley * inputs: 473599d5e173STim Haley * zc_name name of "to" snapshot 473699d5e173STim Haley * zc_value name of "from" snapshot 473799d5e173STim Haley * zc_cookie file descriptor to write diff data on 473899d5e173STim Haley * 473999d5e173STim Haley * outputs: 474099d5e173STim Haley * dmu_diff_record_t's to the file descriptor 474199d5e173STim Haley */ 474299d5e173STim Haley static int 474399d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc) 474499d5e173STim Haley { 474599d5e173STim Haley file_t *fp; 474699d5e173STim Haley offset_t off; 474799d5e173STim Haley int error; 474899d5e173STim Haley 474999d5e173STim Haley fp = getf(zc->zc_cookie); 47503b2aab18SMatthew Ahrens if (fp == NULL) 4751be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 475299d5e173STim Haley 475399d5e173STim Haley off = fp->f_offset; 475499d5e173STim Haley 47553b2aab18SMatthew Ahrens error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off); 475699d5e173STim Haley 475799d5e173STim Haley if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 475899d5e173STim Haley fp->f_offset = off; 475999d5e173STim Haley releasef(zc->zc_cookie); 476099d5e173STim Haley 476199d5e173STim Haley return (error); 476299d5e173STim Haley } 476399d5e173STim Haley 4764743a77edSAlan Wright /* 4765743a77edSAlan Wright * Remove all ACL files in shares dir 4766743a77edSAlan Wright */ 4767743a77edSAlan Wright static int 4768743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp) 4769743a77edSAlan Wright { 4770743a77edSAlan Wright zap_cursor_t zc; 4771743a77edSAlan Wright zap_attribute_t zap; 4772743a77edSAlan Wright zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 4773743a77edSAlan Wright int error; 4774743a77edSAlan Wright 4775743a77edSAlan Wright for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 4776743a77edSAlan Wright (error = zap_cursor_retrieve(&zc, &zap)) == 0; 4777743a77edSAlan Wright zap_cursor_advance(&zc)) { 4778743a77edSAlan Wright if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 4779743a77edSAlan Wright NULL, 0)) != 0) 4780743a77edSAlan Wright break; 4781743a77edSAlan Wright } 4782743a77edSAlan Wright zap_cursor_fini(&zc); 4783743a77edSAlan Wright return (error); 4784743a77edSAlan Wright } 4785743a77edSAlan Wright 4786743a77edSAlan Wright static int 4787743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc) 4788743a77edSAlan Wright { 4789743a77edSAlan Wright vnode_t *vp; 4790743a77edSAlan Wright znode_t *dzp; 4791743a77edSAlan Wright vnode_t *resourcevp = NULL; 4792743a77edSAlan Wright znode_t *sharedir; 4793743a77edSAlan Wright zfsvfs_t *zfsvfs; 4794743a77edSAlan Wright nvlist_t *nvlist; 4795743a77edSAlan Wright char *src, *target; 4796743a77edSAlan Wright vattr_t vattr; 4797743a77edSAlan Wright vsecattr_t vsec; 4798743a77edSAlan Wright int error = 0; 4799743a77edSAlan Wright 4800743a77edSAlan Wright if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 4801743a77edSAlan Wright NO_FOLLOW, NULL, &vp)) != 0) 4802743a77edSAlan Wright return (error); 4803743a77edSAlan Wright 4804743a77edSAlan Wright /* Now make sure mntpnt and dataset are ZFS */ 4805743a77edSAlan Wright 4806743a77edSAlan Wright if (vp->v_vfsp->vfs_fstype != zfsfstype || 4807743a77edSAlan Wright (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 4808743a77edSAlan Wright zc->zc_name) != 0)) { 4809743a77edSAlan Wright VN_RELE(vp); 4810be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 4811743a77edSAlan Wright } 4812743a77edSAlan Wright 4813743a77edSAlan Wright dzp = VTOZ(vp); 4814743a77edSAlan Wright zfsvfs = dzp->z_zfsvfs; 4815743a77edSAlan Wright ZFS_ENTER(zfsvfs); 4816743a77edSAlan Wright 48179e1320c0SMark Shellenbaum /* 48189e1320c0SMark Shellenbaum * Create share dir if its missing. 48199e1320c0SMark Shellenbaum */ 48209e1320c0SMark Shellenbaum mutex_enter(&zfsvfs->z_lock); 48219e1320c0SMark Shellenbaum if (zfsvfs->z_shares_dir == 0) { 48229e1320c0SMark Shellenbaum dmu_tx_t *tx; 48239e1320c0SMark Shellenbaum 48249e1320c0SMark Shellenbaum tx = dmu_tx_create(zfsvfs->z_os); 48259e1320c0SMark Shellenbaum dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 48269e1320c0SMark Shellenbaum ZFS_SHARES_DIR); 48279e1320c0SMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 48289e1320c0SMark Shellenbaum error = dmu_tx_assign(tx, TXG_WAIT); 48293b2aab18SMatthew Ahrens if (error != 0) { 48309e1320c0SMark Shellenbaum dmu_tx_abort(tx); 48319e1320c0SMark Shellenbaum } else { 48329e1320c0SMark Shellenbaum error = zfs_create_share_dir(zfsvfs, tx); 48339e1320c0SMark Shellenbaum dmu_tx_commit(tx); 48349e1320c0SMark Shellenbaum } 48353b2aab18SMatthew Ahrens if (error != 0) { 48369e1320c0SMark Shellenbaum mutex_exit(&zfsvfs->z_lock); 48379e1320c0SMark Shellenbaum VN_RELE(vp); 48389e1320c0SMark Shellenbaum ZFS_EXIT(zfsvfs); 48399e1320c0SMark Shellenbaum return (error); 48409e1320c0SMark Shellenbaum } 48419e1320c0SMark Shellenbaum } 48429e1320c0SMark Shellenbaum mutex_exit(&zfsvfs->z_lock); 48439e1320c0SMark Shellenbaum 48449e1320c0SMark Shellenbaum ASSERT(zfsvfs->z_shares_dir); 4845743a77edSAlan Wright if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 48469e1320c0SMark Shellenbaum VN_RELE(vp); 4847743a77edSAlan Wright ZFS_EXIT(zfsvfs); 4848743a77edSAlan Wright return (error); 4849743a77edSAlan Wright } 4850743a77edSAlan Wright 4851743a77edSAlan Wright switch (zc->zc_cookie) { 4852743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 4853743a77edSAlan Wright vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 4854743a77edSAlan Wright vattr.va_type = VREG; 4855743a77edSAlan Wright vattr.va_mode = S_IFREG|0777; 4856743a77edSAlan Wright vattr.va_uid = 0; 4857743a77edSAlan Wright vattr.va_gid = 0; 4858743a77edSAlan Wright 4859743a77edSAlan Wright vsec.vsa_mask = VSA_ACE; 4860743a77edSAlan Wright vsec.vsa_aclentp = &full_access; 4861743a77edSAlan Wright vsec.vsa_aclentsz = sizeof (full_access); 4862743a77edSAlan Wright vsec.vsa_aclcnt = 1; 4863743a77edSAlan Wright 4864743a77edSAlan Wright error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 4865743a77edSAlan Wright &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 4866743a77edSAlan Wright if (resourcevp) 4867743a77edSAlan Wright VN_RELE(resourcevp); 4868743a77edSAlan Wright break; 4869743a77edSAlan Wright 4870743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 4871743a77edSAlan Wright error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 4872743a77edSAlan Wright NULL, 0); 4873743a77edSAlan Wright break; 4874743a77edSAlan Wright 4875743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 4876743a77edSAlan Wright if ((error = get_nvlist(zc->zc_nvlist_src, 4877478ed9adSEric Taylor zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 4878743a77edSAlan Wright VN_RELE(vp); 4879743a77edSAlan Wright ZFS_EXIT(zfsvfs); 4880743a77edSAlan Wright return (error); 4881743a77edSAlan Wright } 4882743a77edSAlan Wright if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 4883743a77edSAlan Wright nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 4884743a77edSAlan Wright &target)) { 4885743a77edSAlan Wright VN_RELE(vp); 488689459e17SMark Shellenbaum VN_RELE(ZTOV(sharedir)); 4887743a77edSAlan Wright ZFS_EXIT(zfsvfs); 48881195e687SMark J Musante nvlist_free(nvlist); 4889743a77edSAlan Wright return (error); 4890743a77edSAlan Wright } 4891743a77edSAlan Wright error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 4892743a77edSAlan Wright kcred, NULL, 0); 4893743a77edSAlan Wright nvlist_free(nvlist); 4894743a77edSAlan Wright break; 4895743a77edSAlan Wright 4896743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 4897743a77edSAlan Wright error = zfs_smb_acl_purge(sharedir); 4898743a77edSAlan Wright break; 4899743a77edSAlan Wright 4900743a77edSAlan Wright default: 4901be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 4902743a77edSAlan Wright break; 4903743a77edSAlan Wright } 4904743a77edSAlan Wright 4905743a77edSAlan Wright VN_RELE(vp); 4906743a77edSAlan Wright VN_RELE(ZTOV(sharedir)); 4907743a77edSAlan Wright 4908743a77edSAlan Wright ZFS_EXIT(zfsvfs); 4909743a77edSAlan Wright 4910743a77edSAlan Wright return (error); 4911743a77edSAlan Wright } 4912743a77edSAlan Wright 4913842727c2SChris Kirby /* 49143b2aab18SMatthew Ahrens * innvl: { 49153b2aab18SMatthew Ahrens * "holds" -> { snapname -> holdname (string), ... } 49163b2aab18SMatthew Ahrens * (optional) "cleanup_fd" -> fd (int32) 49173b2aab18SMatthew Ahrens * } 4918842727c2SChris Kirby * 49193b2aab18SMatthew Ahrens * outnvl: { 49203b2aab18SMatthew Ahrens * snapname -> error value (int32) 49213b2aab18SMatthew Ahrens * ... 49223b2aab18SMatthew Ahrens * } 4923842727c2SChris Kirby */ 49243b2aab18SMatthew Ahrens /* ARGSUSED */ 4925842727c2SChris Kirby static int 49263b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist) 4927842727c2SChris Kirby { 49283b2aab18SMatthew Ahrens nvlist_t *holds; 49293b2aab18SMatthew Ahrens int cleanup_fd = -1; 4930a7f53a56SChris Kirby int error; 4931a7f53a56SChris Kirby minor_t minor = 0; 4932842727c2SChris Kirby 49333b2aab18SMatthew Ahrens error = nvlist_lookup_nvlist(args, "holds", &holds); 49343b2aab18SMatthew Ahrens if (error != 0) 4935be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 4936a7f53a56SChris Kirby 49373b2aab18SMatthew Ahrens if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) { 49383b2aab18SMatthew Ahrens error = zfs_onexit_fd_hold(cleanup_fd, &minor); 49393b2aab18SMatthew Ahrens if (error != 0) 4940a7f53a56SChris Kirby return (error); 4941a7f53a56SChris Kirby } 4942a7f53a56SChris Kirby 49433b2aab18SMatthew Ahrens error = dsl_dataset_user_hold(holds, minor, errlist); 49443b2aab18SMatthew Ahrens if (minor != 0) 49453b2aab18SMatthew Ahrens zfs_onexit_fd_rele(cleanup_fd); 4946a7f53a56SChris Kirby return (error); 4947842727c2SChris Kirby } 4948842727c2SChris Kirby 4949842727c2SChris Kirby /* 49503b2aab18SMatthew Ahrens * innvl is not used. 4951842727c2SChris Kirby * 49523b2aab18SMatthew Ahrens * outnvl: { 49533b2aab18SMatthew Ahrens * holdname -> time added (uint64 seconds since epoch) 49543b2aab18SMatthew Ahrens * ... 49553b2aab18SMatthew Ahrens * } 4956842727c2SChris Kirby */ 49573b2aab18SMatthew Ahrens /* ARGSUSED */ 4958842727c2SChris Kirby static int 49593b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl) 4960842727c2SChris Kirby { 49613b2aab18SMatthew Ahrens return (dsl_dataset_get_holds(snapname, outnvl)); 4962842727c2SChris Kirby } 4963842727c2SChris Kirby 4964842727c2SChris Kirby /* 49653b2aab18SMatthew Ahrens * innvl: { 49663b2aab18SMatthew Ahrens * snapname -> { holdname, ... } 49673b2aab18SMatthew Ahrens * ... 49683b2aab18SMatthew Ahrens * } 4969842727c2SChris Kirby * 49703b2aab18SMatthew Ahrens * outnvl: { 49713b2aab18SMatthew Ahrens * snapname -> error value (int32) 49723b2aab18SMatthew Ahrens * ... 49733b2aab18SMatthew Ahrens * } 4974842727c2SChris Kirby */ 49753b2aab18SMatthew Ahrens /* ARGSUSED */ 4976842727c2SChris Kirby static int 49773b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist) 4978842727c2SChris Kirby { 49793b2aab18SMatthew Ahrens nvpair_t *pair; 4980842727c2SChris Kirby 49813b2aab18SMatthew Ahrens /* 49823b2aab18SMatthew Ahrens * The release may cause the snapshot to be destroyed; make sure it 49833b2aab18SMatthew Ahrens * is not mounted. 49843b2aab18SMatthew Ahrens */ 49853b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; 49863b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(holds, pair)) 49873b2aab18SMatthew Ahrens zfs_unmount_snap(nvpair_name(pair)); 4988842727c2SChris Kirby 49893b2aab18SMatthew Ahrens return (dsl_dataset_user_release(holds, errlist)); 4990842727c2SChris Kirby } 4991842727c2SChris Kirby 499219b94df9SMatthew Ahrens /* 499319b94df9SMatthew Ahrens * inputs: 499419b94df9SMatthew Ahrens * zc_name name of new filesystem or snapshot 499519b94df9SMatthew Ahrens * zc_value full name of old snapshot 499619b94df9SMatthew Ahrens * 499719b94df9SMatthew Ahrens * outputs: 499819b94df9SMatthew Ahrens * zc_cookie space in bytes 499919b94df9SMatthew Ahrens * zc_objset_type compressed space in bytes 500019b94df9SMatthew Ahrens * zc_perm_action uncompressed space in bytes 500119b94df9SMatthew Ahrens */ 500219b94df9SMatthew Ahrens static int 500319b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc) 500419b94df9SMatthew Ahrens { 500519b94df9SMatthew Ahrens int error; 50063b2aab18SMatthew Ahrens dsl_pool_t *dp; 500719b94df9SMatthew Ahrens dsl_dataset_t *new, *old; 500819b94df9SMatthew Ahrens 50093b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 501019b94df9SMatthew Ahrens if (error != 0) 501119b94df9SMatthew Ahrens return (error); 50123b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new); 50133b2aab18SMatthew Ahrens if (error != 0) { 50143b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 50153b2aab18SMatthew Ahrens return (error); 50163b2aab18SMatthew Ahrens } 50173b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old); 501819b94df9SMatthew Ahrens if (error != 0) { 501919b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 50203b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 502119b94df9SMatthew Ahrens return (error); 502219b94df9SMatthew Ahrens } 502319b94df9SMatthew Ahrens 502419b94df9SMatthew Ahrens error = dsl_dataset_space_written(old, new, &zc->zc_cookie, 502519b94df9SMatthew Ahrens &zc->zc_objset_type, &zc->zc_perm_action); 502619b94df9SMatthew Ahrens dsl_dataset_rele(old, FTAG); 502719b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 50283b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 502919b94df9SMatthew Ahrens return (error); 503019b94df9SMatthew Ahrens } 50313b2aab18SMatthew Ahrens 503219b94df9SMatthew Ahrens /* 50334445fffbSMatthew Ahrens * innvl: { 50344445fffbSMatthew Ahrens * "firstsnap" -> snapshot name 50354445fffbSMatthew Ahrens * } 503619b94df9SMatthew Ahrens * 50374445fffbSMatthew Ahrens * outnvl: { 50384445fffbSMatthew Ahrens * "used" -> space in bytes 50394445fffbSMatthew Ahrens * "compressed" -> compressed space in bytes 50404445fffbSMatthew Ahrens * "uncompressed" -> uncompressed space in bytes 50414445fffbSMatthew Ahrens * } 504219b94df9SMatthew Ahrens */ 504319b94df9SMatthew Ahrens static int 50444445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl) 504519b94df9SMatthew Ahrens { 504619b94df9SMatthew Ahrens int error; 50473b2aab18SMatthew Ahrens dsl_pool_t *dp; 504819b94df9SMatthew Ahrens dsl_dataset_t *new, *old; 50494445fffbSMatthew Ahrens char *firstsnap; 50504445fffbSMatthew Ahrens uint64_t used, comp, uncomp; 505119b94df9SMatthew Ahrens 50524445fffbSMatthew Ahrens if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0) 5053be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 50544445fffbSMatthew Ahrens 50553b2aab18SMatthew Ahrens error = dsl_pool_hold(lastsnap, FTAG, &dp); 505619b94df9SMatthew Ahrens if (error != 0) 505719b94df9SMatthew Ahrens return (error); 50583b2aab18SMatthew Ahrens 50593b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, lastsnap, FTAG, &new); 50603b2aab18SMatthew Ahrens if (error != 0) { 50613b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 50623b2aab18SMatthew Ahrens return (error); 50633b2aab18SMatthew Ahrens } 50643b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, firstsnap, FTAG, &old); 506519b94df9SMatthew Ahrens if (error != 0) { 506619b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 50673b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 506819b94df9SMatthew Ahrens return (error); 506919b94df9SMatthew Ahrens } 507019b94df9SMatthew Ahrens 50714445fffbSMatthew Ahrens error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp); 507219b94df9SMatthew Ahrens dsl_dataset_rele(old, FTAG); 507319b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG); 50743b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 50754445fffbSMatthew Ahrens fnvlist_add_uint64(outnvl, "used", used); 50764445fffbSMatthew Ahrens fnvlist_add_uint64(outnvl, "compressed", comp); 50774445fffbSMatthew Ahrens fnvlist_add_uint64(outnvl, "uncompressed", uncomp); 507819b94df9SMatthew Ahrens return (error); 507919b94df9SMatthew Ahrens } 508019b94df9SMatthew Ahrens 5081ecd6cf80Smarks /* 50824445fffbSMatthew Ahrens * innvl: { 50834445fffbSMatthew Ahrens * "fd" -> file descriptor to write stream to (int32) 50844445fffbSMatthew Ahrens * (optional) "fromsnap" -> full snap name to send an incremental from 50854445fffbSMatthew Ahrens * } 50864445fffbSMatthew Ahrens * 50874445fffbSMatthew Ahrens * outnvl is unused 5088ecd6cf80Smarks */ 50894445fffbSMatthew Ahrens /* ARGSUSED */ 50904445fffbSMatthew Ahrens static int 50914445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 50924445fffbSMatthew Ahrens { 50934445fffbSMatthew Ahrens int error; 50944445fffbSMatthew Ahrens offset_t off; 50953b2aab18SMatthew Ahrens char *fromname = NULL; 50964445fffbSMatthew Ahrens int fd; 50974445fffbSMatthew Ahrens 50984445fffbSMatthew Ahrens error = nvlist_lookup_int32(innvl, "fd", &fd); 50994445fffbSMatthew Ahrens if (error != 0) 5100be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 51014445fffbSMatthew Ahrens 51023b2aab18SMatthew Ahrens (void) nvlist_lookup_string(innvl, "fromsnap", &fromname); 51034445fffbSMatthew Ahrens 51044445fffbSMatthew Ahrens file_t *fp = getf(fd); 51053b2aab18SMatthew Ahrens if (fp == NULL) 5106be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 51074445fffbSMatthew Ahrens 51084445fffbSMatthew Ahrens off = fp->f_offset; 51093b2aab18SMatthew Ahrens error = dmu_send(snapname, fromname, fd, fp->f_vnode, &off); 51104445fffbSMatthew Ahrens 51114445fffbSMatthew Ahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 51124445fffbSMatthew Ahrens fp->f_offset = off; 51134445fffbSMatthew Ahrens releasef(fd); 51144445fffbSMatthew Ahrens return (error); 51154445fffbSMatthew Ahrens } 51164445fffbSMatthew Ahrens 51174445fffbSMatthew Ahrens /* 51184445fffbSMatthew Ahrens * Determine approximately how large a zfs send stream will be -- the number 51194445fffbSMatthew Ahrens * of bytes that will be written to the fd supplied to zfs_ioc_send_new(). 51204445fffbSMatthew Ahrens * 51214445fffbSMatthew Ahrens * innvl: { 51224445fffbSMatthew Ahrens * (optional) "fromsnap" -> full snap name to send an incremental from 51234445fffbSMatthew Ahrens * } 51244445fffbSMatthew Ahrens * 51254445fffbSMatthew Ahrens * outnvl: { 51264445fffbSMatthew Ahrens * "space" -> bytes of space (uint64) 51274445fffbSMatthew Ahrens * } 51284445fffbSMatthew Ahrens */ 51294445fffbSMatthew Ahrens static int 51304445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 51314445fffbSMatthew Ahrens { 51323b2aab18SMatthew Ahrens dsl_pool_t *dp; 51333b2aab18SMatthew Ahrens dsl_dataset_t *fromsnap = NULL; 51343b2aab18SMatthew Ahrens dsl_dataset_t *tosnap; 51354445fffbSMatthew Ahrens int error; 51364445fffbSMatthew Ahrens char *fromname; 51374445fffbSMatthew Ahrens uint64_t space; 51384445fffbSMatthew Ahrens 51393b2aab18SMatthew Ahrens error = dsl_pool_hold(snapname, FTAG, &dp); 51403b2aab18SMatthew Ahrens if (error != 0) 51413b2aab18SMatthew Ahrens return (error); 51423b2aab18SMatthew Ahrens 51433b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap); 51443b2aab18SMatthew Ahrens if (error != 0) { 51453b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 51464445fffbSMatthew Ahrens return (error); 51473b2aab18SMatthew Ahrens } 51484445fffbSMatthew Ahrens 51494445fffbSMatthew Ahrens error = nvlist_lookup_string(innvl, "fromsnap", &fromname); 51504445fffbSMatthew Ahrens if (error == 0) { 51513b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap); 51523b2aab18SMatthew Ahrens if (error != 0) { 51533b2aab18SMatthew Ahrens dsl_dataset_rele(tosnap, FTAG); 51543b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 51554445fffbSMatthew Ahrens return (error); 51564445fffbSMatthew Ahrens } 51574445fffbSMatthew Ahrens } 51584445fffbSMatthew Ahrens 51594445fffbSMatthew Ahrens error = dmu_send_estimate(tosnap, fromsnap, &space); 51604445fffbSMatthew Ahrens fnvlist_add_uint64(outnvl, "space", space); 51614445fffbSMatthew Ahrens 51624445fffbSMatthew Ahrens if (fromsnap != NULL) 51633b2aab18SMatthew Ahrens dsl_dataset_rele(fromsnap, FTAG); 51643b2aab18SMatthew Ahrens dsl_dataset_rele(tosnap, FTAG); 51653b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 51664445fffbSMatthew Ahrens return (error); 51674445fffbSMatthew Ahrens } 51684445fffbSMatthew Ahrens 51694445fffbSMatthew Ahrens 51704445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST]; 51714445fffbSMatthew Ahrens 51724445fffbSMatthew Ahrens static void 51734445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 51744445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 51754445fffbSMatthew Ahrens boolean_t log_history, zfs_ioc_poolcheck_t pool_check) 51764445fffbSMatthew Ahrens { 51774445fffbSMatthew Ahrens zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 51784445fffbSMatthew Ahrens 51794445fffbSMatthew Ahrens ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 51804445fffbSMatthew Ahrens ASSERT3U(ioc, <, ZFS_IOC_LAST); 51814445fffbSMatthew Ahrens ASSERT3P(vec->zvec_legacy_func, ==, NULL); 51824445fffbSMatthew Ahrens ASSERT3P(vec->zvec_func, ==, NULL); 51834445fffbSMatthew Ahrens 51844445fffbSMatthew Ahrens vec->zvec_legacy_func = func; 51854445fffbSMatthew Ahrens vec->zvec_secpolicy = secpolicy; 51864445fffbSMatthew Ahrens vec->zvec_namecheck = namecheck; 51874445fffbSMatthew Ahrens vec->zvec_allow_log = log_history; 51884445fffbSMatthew Ahrens vec->zvec_pool_check = pool_check; 51894445fffbSMatthew Ahrens } 51904445fffbSMatthew Ahrens 51914445fffbSMatthew Ahrens /* 51924445fffbSMatthew Ahrens * See the block comment at the beginning of this file for details on 51934445fffbSMatthew Ahrens * each argument to this function. 51944445fffbSMatthew Ahrens */ 51954445fffbSMatthew Ahrens static void 51964445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func, 51974445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 51984445fffbSMatthew Ahrens zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist, 51994445fffbSMatthew Ahrens boolean_t allow_log) 52004445fffbSMatthew Ahrens { 52014445fffbSMatthew Ahrens zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 52024445fffbSMatthew Ahrens 52034445fffbSMatthew Ahrens ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 52044445fffbSMatthew Ahrens ASSERT3U(ioc, <, ZFS_IOC_LAST); 52054445fffbSMatthew Ahrens ASSERT3P(vec->zvec_legacy_func, ==, NULL); 52064445fffbSMatthew Ahrens ASSERT3P(vec->zvec_func, ==, NULL); 52074445fffbSMatthew Ahrens 52084445fffbSMatthew Ahrens /* if we are logging, the name must be valid */ 52094445fffbSMatthew Ahrens ASSERT(!allow_log || namecheck != NO_NAME); 52104445fffbSMatthew Ahrens 52114445fffbSMatthew Ahrens vec->zvec_name = name; 52124445fffbSMatthew Ahrens vec->zvec_func = func; 52134445fffbSMatthew Ahrens vec->zvec_secpolicy = secpolicy; 52144445fffbSMatthew Ahrens vec->zvec_namecheck = namecheck; 52154445fffbSMatthew Ahrens vec->zvec_pool_check = pool_check; 52164445fffbSMatthew Ahrens vec->zvec_smush_outnvlist = smush_outnvlist; 52174445fffbSMatthew Ahrens vec->zvec_allow_log = allow_log; 52184445fffbSMatthew Ahrens } 52194445fffbSMatthew Ahrens 52204445fffbSMatthew Ahrens static void 52214445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 52224445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy, boolean_t log_history, 52234445fffbSMatthew Ahrens zfs_ioc_poolcheck_t pool_check) 52244445fffbSMatthew Ahrens { 52254445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy, 52264445fffbSMatthew Ahrens POOL_NAME, log_history, pool_check); 52274445fffbSMatthew Ahrens } 52284445fffbSMatthew Ahrens 52294445fffbSMatthew Ahrens static void 52304445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 52314445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check) 52324445fffbSMatthew Ahrens { 52334445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy, 52344445fffbSMatthew Ahrens DATASET_NAME, B_FALSE, pool_check); 52354445fffbSMatthew Ahrens } 52364445fffbSMatthew Ahrens 52374445fffbSMatthew Ahrens static void 52384445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 52394445fffbSMatthew Ahrens { 52404445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config, 52414445fffbSMatthew Ahrens POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 52424445fffbSMatthew Ahrens } 52434445fffbSMatthew Ahrens 52444445fffbSMatthew Ahrens static void 52454445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 52464445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy) 52474445fffbSMatthew Ahrens { 52484445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy, 52494445fffbSMatthew Ahrens NO_NAME, B_FALSE, POOL_CHECK_NONE); 52504445fffbSMatthew Ahrens } 52514445fffbSMatthew Ahrens 52524445fffbSMatthew Ahrens static void 52534445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc, 52544445fffbSMatthew Ahrens zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy) 52554445fffbSMatthew Ahrens { 52564445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy, 52574445fffbSMatthew Ahrens DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED); 52584445fffbSMatthew Ahrens } 52594445fffbSMatthew Ahrens 52604445fffbSMatthew Ahrens static void 52614445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 52624445fffbSMatthew Ahrens { 52634445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ioc, func, 52644445fffbSMatthew Ahrens zfs_secpolicy_read); 52654445fffbSMatthew Ahrens } 52664445fffbSMatthew Ahrens 52674445fffbSMatthew Ahrens static void 52684445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 52694445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy) 52704445fffbSMatthew Ahrens { 52714445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy, 52724445fffbSMatthew Ahrens DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 52734445fffbSMatthew Ahrens } 52744445fffbSMatthew Ahrens 52754445fffbSMatthew Ahrens static void 52764445fffbSMatthew Ahrens zfs_ioctl_init(void) 52774445fffbSMatthew Ahrens { 52784445fffbSMatthew Ahrens zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT, 52794445fffbSMatthew Ahrens zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME, 52804445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 52814445fffbSMatthew Ahrens 52824445fffbSMatthew Ahrens zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY, 52834445fffbSMatthew Ahrens zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME, 52844445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE); 52854445fffbSMatthew Ahrens 52864445fffbSMatthew Ahrens zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS, 52874445fffbSMatthew Ahrens zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, 52884445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 52894445fffbSMatthew Ahrens 52904445fffbSMatthew Ahrens zfs_ioctl_register("send", ZFS_IOC_SEND_NEW, 52914445fffbSMatthew Ahrens zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME, 52924445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 52934445fffbSMatthew Ahrens 52944445fffbSMatthew Ahrens zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE, 52954445fffbSMatthew Ahrens zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME, 52964445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 52974445fffbSMatthew Ahrens 52984445fffbSMatthew Ahrens zfs_ioctl_register("create", ZFS_IOC_CREATE, 52994445fffbSMatthew Ahrens zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME, 53004445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 53014445fffbSMatthew Ahrens 53024445fffbSMatthew Ahrens zfs_ioctl_register("clone", ZFS_IOC_CLONE, 53034445fffbSMatthew Ahrens zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME, 53044445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 53054445fffbSMatthew Ahrens 53064445fffbSMatthew Ahrens zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS, 53074445fffbSMatthew Ahrens zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME, 53084445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 53094445fffbSMatthew Ahrens 53103b2aab18SMatthew Ahrens zfs_ioctl_register("hold", ZFS_IOC_HOLD, 53113b2aab18SMatthew Ahrens zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME, 53123b2aab18SMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 53133b2aab18SMatthew Ahrens zfs_ioctl_register("release", ZFS_IOC_RELEASE, 53143b2aab18SMatthew Ahrens zfs_ioc_release, zfs_secpolicy_release, POOL_NAME, 53153b2aab18SMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 53163b2aab18SMatthew Ahrens 53173b2aab18SMatthew Ahrens zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS, 53183b2aab18SMatthew Ahrens zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, 53193b2aab18SMatthew Ahrens POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 53203b2aab18SMatthew Ahrens 53214445fffbSMatthew Ahrens /* IOCTLS that use the legacy function signature */ 53224445fffbSMatthew Ahrens 53234445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze, 53244445fffbSMatthew Ahrens zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY); 53254445fffbSMatthew Ahrens 53264445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create, 53274445fffbSMatthew Ahrens zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 53284445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN, 53294445fffbSMatthew Ahrens zfs_ioc_pool_scan); 53304445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE, 53314445fffbSMatthew Ahrens zfs_ioc_pool_upgrade); 53324445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD, 53334445fffbSMatthew Ahrens zfs_ioc_vdev_add); 53344445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE, 53354445fffbSMatthew Ahrens zfs_ioc_vdev_remove); 53364445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE, 53374445fffbSMatthew Ahrens zfs_ioc_vdev_set_state); 53384445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH, 53394445fffbSMatthew Ahrens zfs_ioc_vdev_attach); 53404445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH, 53414445fffbSMatthew Ahrens zfs_ioc_vdev_detach); 53424445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH, 53434445fffbSMatthew Ahrens zfs_ioc_vdev_setpath); 53444445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU, 53454445fffbSMatthew Ahrens zfs_ioc_vdev_setfru); 53464445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS, 53474445fffbSMatthew Ahrens zfs_ioc_pool_set_props); 53484445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT, 53494445fffbSMatthew Ahrens zfs_ioc_vdev_split); 53504445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID, 53514445fffbSMatthew Ahrens zfs_ioc_pool_reguid); 53524445fffbSMatthew Ahrens 53534445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS, 53544445fffbSMatthew Ahrens zfs_ioc_pool_configs, zfs_secpolicy_none); 53554445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT, 53564445fffbSMatthew Ahrens zfs_ioc_pool_tryimport, zfs_secpolicy_config); 53574445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT, 53584445fffbSMatthew Ahrens zfs_ioc_inject_fault, zfs_secpolicy_inject); 53594445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT, 53604445fffbSMatthew Ahrens zfs_ioc_clear_fault, zfs_secpolicy_inject); 53614445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT, 53624445fffbSMatthew Ahrens zfs_ioc_inject_list_next, zfs_secpolicy_inject); 53634445fffbSMatthew Ahrens 53644445fffbSMatthew Ahrens /* 53654445fffbSMatthew Ahrens * pool destroy, and export don't log the history as part of 53664445fffbSMatthew Ahrens * zfsdev_ioctl, but rather zfs_ioc_pool_export 53674445fffbSMatthew Ahrens * does the logging of those commands. 53684445fffbSMatthew Ahrens */ 53694445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy, 53704445fffbSMatthew Ahrens zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE); 53714445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export, 53724445fffbSMatthew Ahrens zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE); 53734445fffbSMatthew Ahrens 53744445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats, 53754445fffbSMatthew Ahrens zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); 53764445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props, 53774445fffbSMatthew Ahrens zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); 53784445fffbSMatthew Ahrens 53794445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log, 53804445fffbSMatthew Ahrens zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED); 53814445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME, 53824445fffbSMatthew Ahrens zfs_ioc_dsobj_to_dsname, 53834445fffbSMatthew Ahrens zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED); 53844445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY, 53854445fffbSMatthew Ahrens zfs_ioc_pool_get_history, 53864445fffbSMatthew Ahrens zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); 53874445fffbSMatthew Ahrens 53884445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import, 53894445fffbSMatthew Ahrens zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 53904445fffbSMatthew Ahrens 53914445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear, 53924445fffbSMatthew Ahrens zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED); 53934445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen, 53944445fffbSMatthew Ahrens zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED); 53954445fffbSMatthew Ahrens 53964445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN, 53974445fffbSMatthew Ahrens zfs_ioc_space_written); 53984445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS, 53994445fffbSMatthew Ahrens zfs_ioc_objset_recvd_props); 54004445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ, 54014445fffbSMatthew Ahrens zfs_ioc_next_obj); 54024445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL, 54034445fffbSMatthew Ahrens zfs_ioc_get_fsacl); 54044445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS, 54054445fffbSMatthew Ahrens zfs_ioc_objset_stats); 54064445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS, 54074445fffbSMatthew Ahrens zfs_ioc_objset_zplprops); 54084445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT, 54094445fffbSMatthew Ahrens zfs_ioc_dataset_list_next); 54104445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT, 54114445fffbSMatthew Ahrens zfs_ioc_snapshot_list_next); 54124445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS, 54134445fffbSMatthew Ahrens zfs_ioc_send_progress); 54144445fffbSMatthew Ahrens 54154445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF, 54164445fffbSMatthew Ahrens zfs_ioc_diff, zfs_secpolicy_diff); 54174445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS, 54184445fffbSMatthew Ahrens zfs_ioc_obj_to_stats, zfs_secpolicy_diff); 54194445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH, 54204445fffbSMatthew Ahrens zfs_ioc_obj_to_path, zfs_secpolicy_diff); 54214445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE, 54224445fffbSMatthew Ahrens zfs_ioc_userspace_one, zfs_secpolicy_userspace_one); 54234445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY, 54244445fffbSMatthew Ahrens zfs_ioc_userspace_many, zfs_secpolicy_userspace_many); 54254445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND, 54264445fffbSMatthew Ahrens zfs_ioc_send, zfs_secpolicy_send); 54274445fffbSMatthew Ahrens 54284445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop, 54294445fffbSMatthew Ahrens zfs_secpolicy_none); 54304445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy, 54314445fffbSMatthew Ahrens zfs_secpolicy_destroy); 54324445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_ROLLBACK, zfs_ioc_rollback, 54334445fffbSMatthew Ahrens zfs_secpolicy_rollback); 54344445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename, 54354445fffbSMatthew Ahrens zfs_secpolicy_rename); 54364445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv, 54374445fffbSMatthew Ahrens zfs_secpolicy_recv); 54384445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote, 54394445fffbSMatthew Ahrens zfs_secpolicy_promote); 54404445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP, 54414445fffbSMatthew Ahrens zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop); 54424445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl, 54434445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl); 54444445fffbSMatthew Ahrens 54454445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share, 54464445fffbSMatthew Ahrens zfs_secpolicy_share, POOL_CHECK_NONE); 54474445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl, 54484445fffbSMatthew Ahrens zfs_secpolicy_smb_acl, POOL_CHECK_NONE); 54494445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE, 54504445fffbSMatthew Ahrens zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 54514445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 54524445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT, 54534445fffbSMatthew Ahrens zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, 54544445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 54554445fffbSMatthew Ahrens } 5456fa9e4066Sahrens 545754d692b7SGeorge Wilson int 5458f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type, 5459f9af39baSGeorge Wilson zfs_ioc_poolcheck_t check) 546054d692b7SGeorge Wilson { 546154d692b7SGeorge Wilson spa_t *spa; 546254d692b7SGeorge Wilson int error; 546354d692b7SGeorge Wilson 546454d692b7SGeorge Wilson ASSERT(type == POOL_NAME || type == DATASET_NAME); 546554d692b7SGeorge Wilson 5466f9af39baSGeorge Wilson if (check & POOL_CHECK_NONE) 5467f9af39baSGeorge Wilson return (0); 5468f9af39baSGeorge Wilson 546914843421SMatthew Ahrens error = spa_open(name, &spa, FTAG); 547054d692b7SGeorge Wilson if (error == 0) { 5471f9af39baSGeorge Wilson if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa)) 5472be6fd75aSMatthew Ahrens error = SET_ERROR(EAGAIN); 5473f9af39baSGeorge Wilson else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa)) 5474be6fd75aSMatthew Ahrens error = SET_ERROR(EROFS); 547554d692b7SGeorge Wilson spa_close(spa, FTAG); 547654d692b7SGeorge Wilson } 547754d692b7SGeorge Wilson return (error); 547854d692b7SGeorge Wilson } 547954d692b7SGeorge Wilson 5480c99e4bdcSChris Kirby /* 5481c99e4bdcSChris Kirby * Find a free minor number. 5482c99e4bdcSChris Kirby */ 5483c99e4bdcSChris Kirby minor_t 5484c99e4bdcSChris Kirby zfsdev_minor_alloc(void) 5485c99e4bdcSChris Kirby { 5486c99e4bdcSChris Kirby static minor_t last_minor; 5487c99e4bdcSChris Kirby minor_t m; 5488c99e4bdcSChris Kirby 5489c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 5490c99e4bdcSChris Kirby 5491c99e4bdcSChris Kirby for (m = last_minor + 1; m != last_minor; m++) { 5492c99e4bdcSChris Kirby if (m > ZFSDEV_MAX_MINOR) 5493c99e4bdcSChris Kirby m = 1; 5494c99e4bdcSChris Kirby if (ddi_get_soft_state(zfsdev_state, m) == NULL) { 5495c99e4bdcSChris Kirby last_minor = m; 5496c99e4bdcSChris Kirby return (m); 5497c99e4bdcSChris Kirby } 5498c99e4bdcSChris Kirby } 5499c99e4bdcSChris Kirby 5500c99e4bdcSChris Kirby return (0); 5501c99e4bdcSChris Kirby } 5502c99e4bdcSChris Kirby 5503c99e4bdcSChris Kirby static int 5504c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp) 5505c99e4bdcSChris Kirby { 5506c99e4bdcSChris Kirby minor_t minor; 5507c99e4bdcSChris Kirby zfs_soft_state_t *zs; 5508c99e4bdcSChris Kirby 5509c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 5510c99e4bdcSChris Kirby ASSERT(getminor(*devp) == 0); 5511c99e4bdcSChris Kirby 5512c99e4bdcSChris Kirby minor = zfsdev_minor_alloc(); 5513c99e4bdcSChris Kirby if (minor == 0) 5514be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 5515c99e4bdcSChris Kirby 5516c99e4bdcSChris Kirby if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) 5517be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 5518c99e4bdcSChris Kirby 5519c99e4bdcSChris Kirby *devp = makedevice(getemajor(*devp), minor); 5520c99e4bdcSChris Kirby 5521c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, minor); 5522c99e4bdcSChris Kirby zs->zss_type = ZSST_CTLDEV; 5523c99e4bdcSChris Kirby zfs_onexit_init((zfs_onexit_t **)&zs->zss_data); 5524c99e4bdcSChris Kirby 5525c99e4bdcSChris Kirby return (0); 5526c99e4bdcSChris Kirby } 5527c99e4bdcSChris Kirby 5528c99e4bdcSChris Kirby static void 5529c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor) 5530c99e4bdcSChris Kirby { 5531c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 5532c99e4bdcSChris Kirby 5533c99e4bdcSChris Kirby zfs_onexit_destroy(zo); 5534c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5535c99e4bdcSChris Kirby } 5536c99e4bdcSChris Kirby 5537c99e4bdcSChris Kirby void * 5538c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which) 5539c99e4bdcSChris Kirby { 5540c99e4bdcSChris Kirby zfs_soft_state_t *zp; 5541c99e4bdcSChris Kirby 5542c99e4bdcSChris Kirby zp = ddi_get_soft_state(zfsdev_state, minor); 5543c99e4bdcSChris Kirby if (zp == NULL || zp->zss_type != which) 5544c99e4bdcSChris Kirby return (NULL); 5545c99e4bdcSChris Kirby 5546c99e4bdcSChris Kirby return (zp->zss_data); 5547c99e4bdcSChris Kirby } 5548c99e4bdcSChris Kirby 5549c99e4bdcSChris Kirby static int 5550c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr) 5551c99e4bdcSChris Kirby { 5552c99e4bdcSChris Kirby int error = 0; 5553c99e4bdcSChris Kirby 5554c99e4bdcSChris Kirby if (getminor(*devp) != 0) 5555c99e4bdcSChris Kirby return (zvol_open(devp, flag, otyp, cr)); 5556c99e4bdcSChris Kirby 5557c99e4bdcSChris Kirby /* This is the control device. Allocate a new minor if requested. */ 5558c99e4bdcSChris Kirby if (flag & FEXCL) { 5559c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 5560c99e4bdcSChris Kirby error = zfs_ctldev_init(devp); 5561c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 5562c99e4bdcSChris Kirby } 5563c99e4bdcSChris Kirby 5564c99e4bdcSChris Kirby return (error); 5565c99e4bdcSChris Kirby } 5566c99e4bdcSChris Kirby 5567c99e4bdcSChris Kirby static int 5568c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr) 5569c99e4bdcSChris Kirby { 5570c99e4bdcSChris Kirby zfs_onexit_t *zo; 5571c99e4bdcSChris Kirby minor_t minor = getminor(dev); 5572c99e4bdcSChris Kirby 5573c99e4bdcSChris Kirby if (minor == 0) 5574c99e4bdcSChris Kirby return (0); 5575c99e4bdcSChris Kirby 5576c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 5577c99e4bdcSChris Kirby zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV); 5578c99e4bdcSChris Kirby if (zo == NULL) { 5579c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 5580c99e4bdcSChris Kirby return (zvol_close(dev, flag, otyp, cr)); 5581c99e4bdcSChris Kirby } 5582c99e4bdcSChris Kirby zfs_ctldev_destroy(zo, minor); 5583c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 5584c99e4bdcSChris Kirby 5585c99e4bdcSChris Kirby return (0); 5586c99e4bdcSChris Kirby } 5587c99e4bdcSChris Kirby 5588fa9e4066Sahrens static int 5589fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 5590fa9e4066Sahrens { 5591fa9e4066Sahrens zfs_cmd_t *zc; 55924445fffbSMatthew Ahrens uint_t vecnum; 55934445fffbSMatthew Ahrens int error, rc, len; 5594c99e4bdcSChris Kirby minor_t minor = getminor(dev); 55954445fffbSMatthew Ahrens const zfs_ioc_vec_t *vec; 55964445fffbSMatthew Ahrens char *saved_poolname = NULL; 55974445fffbSMatthew Ahrens nvlist_t *innvl = NULL; 5598fa9e4066Sahrens 5599c99e4bdcSChris Kirby if (minor != 0 && 5600c99e4bdcSChris Kirby zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL) 5601fa9e4066Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 5602fa9e4066Sahrens 56034445fffbSMatthew Ahrens vecnum = cmd - ZFS_IOC_FIRST; 560491ebeef5Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 5605fa9e4066Sahrens 56064445fffbSMatthew Ahrens if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 5607be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 56084445fffbSMatthew Ahrens vec = &zfs_ioc_vec[vecnum]; 5609fa9e4066Sahrens 5610fa9e4066Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 5611fa9e4066Sahrens 5612478ed9adSEric Taylor error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 56134445fffbSMatthew Ahrens if (error != 0) { 5614be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 56154445fffbSMatthew Ahrens goto out; 56164445fffbSMatthew Ahrens } 5617fa9e4066Sahrens 56184445fffbSMatthew Ahrens zc->zc_iflags = flag & FKIOCTL; 56194445fffbSMatthew Ahrens if (zc->zc_nvlist_src_size != 0) { 56204445fffbSMatthew Ahrens error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 56214445fffbSMatthew Ahrens zc->zc_iflags, &innvl); 56224445fffbSMatthew Ahrens if (error != 0) 56234445fffbSMatthew Ahrens goto out; 56244445fffbSMatthew Ahrens } 5625fa9e4066Sahrens 5626fa9e4066Sahrens /* 5627fa9e4066Sahrens * Ensure that all pool/dataset names are valid before we pass down to 5628fa9e4066Sahrens * the lower layers. 5629fa9e4066Sahrens */ 56304445fffbSMatthew Ahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 56314445fffbSMatthew Ahrens switch (vec->zvec_namecheck) { 56324445fffbSMatthew Ahrens case POOL_NAME: 56334445fffbSMatthew Ahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 5634be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 56354445fffbSMatthew Ahrens else 5636f9af39baSGeorge Wilson error = pool_status_check(zc->zc_name, 56374445fffbSMatthew Ahrens vec->zvec_namecheck, vec->zvec_pool_check); 56384445fffbSMatthew Ahrens break; 5639fa9e4066Sahrens 56404445fffbSMatthew Ahrens case DATASET_NAME: 56414445fffbSMatthew Ahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 5642be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 56434445fffbSMatthew Ahrens else 5644f9af39baSGeorge Wilson error = pool_status_check(zc->zc_name, 56454445fffbSMatthew Ahrens vec->zvec_namecheck, vec->zvec_pool_check); 56464445fffbSMatthew Ahrens break; 56475ad82045Snd 56484445fffbSMatthew Ahrens case NO_NAME: 56494445fffbSMatthew Ahrens break; 5650fa9e4066Sahrens } 5651fa9e4066Sahrens 5652fa9e4066Sahrens 56534445fffbSMatthew Ahrens if (error == 0 && !(flag & FKIOCTL)) 56544445fffbSMatthew Ahrens error = vec->zvec_secpolicy(zc, innvl, cr); 56554445fffbSMatthew Ahrens 56564445fffbSMatthew Ahrens if (error != 0) 56574445fffbSMatthew Ahrens goto out; 56584445fffbSMatthew Ahrens 56594445fffbSMatthew Ahrens /* legacy ioctls can modify zc_name */ 56604445fffbSMatthew Ahrens len = strcspn(zc->zc_name, "/@") + 1; 56614445fffbSMatthew Ahrens saved_poolname = kmem_alloc(len, KM_SLEEP); 56624445fffbSMatthew Ahrens (void) strlcpy(saved_poolname, zc->zc_name, len); 56634445fffbSMatthew Ahrens 56644445fffbSMatthew Ahrens if (vec->zvec_func != NULL) { 56654445fffbSMatthew Ahrens nvlist_t *outnvl; 56664445fffbSMatthew Ahrens int puterror = 0; 56674445fffbSMatthew Ahrens spa_t *spa; 56684445fffbSMatthew Ahrens nvlist_t *lognv = NULL; 56694445fffbSMatthew Ahrens 56704445fffbSMatthew Ahrens ASSERT(vec->zvec_legacy_func == NULL); 56714445fffbSMatthew Ahrens 56724445fffbSMatthew Ahrens /* 56734445fffbSMatthew Ahrens * Add the innvl to the lognv before calling the func, 56744445fffbSMatthew Ahrens * in case the func changes the innvl. 56754445fffbSMatthew Ahrens */ 56764445fffbSMatthew Ahrens if (vec->zvec_allow_log) { 56774445fffbSMatthew Ahrens lognv = fnvlist_alloc(); 56784445fffbSMatthew Ahrens fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL, 56794445fffbSMatthew Ahrens vec->zvec_name); 56804445fffbSMatthew Ahrens if (!nvlist_empty(innvl)) { 56814445fffbSMatthew Ahrens fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL, 56824445fffbSMatthew Ahrens innvl); 56834445fffbSMatthew Ahrens } 56844445fffbSMatthew Ahrens } 56854445fffbSMatthew Ahrens 56864445fffbSMatthew Ahrens outnvl = fnvlist_alloc(); 56874445fffbSMatthew Ahrens error = vec->zvec_func(zc->zc_name, innvl, outnvl); 56884445fffbSMatthew Ahrens 56894445fffbSMatthew Ahrens if (error == 0 && vec->zvec_allow_log && 56904445fffbSMatthew Ahrens spa_open(zc->zc_name, &spa, FTAG) == 0) { 56914445fffbSMatthew Ahrens if (!nvlist_empty(outnvl)) { 56924445fffbSMatthew Ahrens fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL, 56934445fffbSMatthew Ahrens outnvl); 56944445fffbSMatthew Ahrens } 56954445fffbSMatthew Ahrens (void) spa_history_log_nvl(spa, lognv); 56964445fffbSMatthew Ahrens spa_close(spa, FTAG); 56974445fffbSMatthew Ahrens } 56984445fffbSMatthew Ahrens fnvlist_free(lognv); 56994445fffbSMatthew Ahrens 57004445fffbSMatthew Ahrens if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) { 57014445fffbSMatthew Ahrens int smusherror = 0; 57024445fffbSMatthew Ahrens if (vec->zvec_smush_outnvlist) { 57034445fffbSMatthew Ahrens smusherror = nvlist_smush(outnvl, 57044445fffbSMatthew Ahrens zc->zc_nvlist_dst_size); 57054445fffbSMatthew Ahrens } 57064445fffbSMatthew Ahrens if (smusherror == 0) 57074445fffbSMatthew Ahrens puterror = put_nvlist(zc, outnvl); 57084445fffbSMatthew Ahrens } 57094445fffbSMatthew Ahrens 57104445fffbSMatthew Ahrens if (puterror != 0) 57114445fffbSMatthew Ahrens error = puterror; 57124445fffbSMatthew Ahrens 57134445fffbSMatthew Ahrens nvlist_free(outnvl); 57144445fffbSMatthew Ahrens } else { 57154445fffbSMatthew Ahrens error = vec->zvec_legacy_func(zc); 57164445fffbSMatthew Ahrens } 57174445fffbSMatthew Ahrens 57184445fffbSMatthew Ahrens out: 57194445fffbSMatthew Ahrens nvlist_free(innvl); 5720478ed9adSEric Taylor rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 57214445fffbSMatthew Ahrens if (error == 0 && rc != 0) 5722be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 57234445fffbSMatthew Ahrens if (error == 0 && vec->zvec_allow_log) { 57244445fffbSMatthew Ahrens char *s = tsd_get(zfs_allow_log_key); 57254445fffbSMatthew Ahrens if (s != NULL) 57264445fffbSMatthew Ahrens strfree(s); 57274445fffbSMatthew Ahrens (void) tsd_set(zfs_allow_log_key, saved_poolname); 57284445fffbSMatthew Ahrens } else { 57294445fffbSMatthew Ahrens if (saved_poolname != NULL) 57304445fffbSMatthew Ahrens strfree(saved_poolname); 5731ecd6cf80Smarks } 5732fa9e4066Sahrens 5733fa9e4066Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 5734fa9e4066Sahrens return (error); 5735fa9e4066Sahrens } 5736fa9e4066Sahrens 5737fa9e4066Sahrens static int 5738fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 5739fa9e4066Sahrens { 5740fa9e4066Sahrens if (cmd != DDI_ATTACH) 5741fa9e4066Sahrens return (DDI_FAILURE); 5742fa9e4066Sahrens 5743fa9e4066Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 5744fa9e4066Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 5745fa9e4066Sahrens return (DDI_FAILURE); 5746fa9e4066Sahrens 5747fa9e4066Sahrens zfs_dip = dip; 5748fa9e4066Sahrens 5749fa9e4066Sahrens ddi_report_dev(dip); 5750fa9e4066Sahrens 5751fa9e4066Sahrens return (DDI_SUCCESS); 5752fa9e4066Sahrens } 5753fa9e4066Sahrens 5754fa9e4066Sahrens static int 5755fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 5756fa9e4066Sahrens { 5757fa9e4066Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 5758fa9e4066Sahrens return (DDI_FAILURE); 5759fa9e4066Sahrens 5760fa9e4066Sahrens if (cmd != DDI_DETACH) 5761fa9e4066Sahrens return (DDI_FAILURE); 5762fa9e4066Sahrens 5763fa9e4066Sahrens zfs_dip = NULL; 5764fa9e4066Sahrens 5765fa9e4066Sahrens ddi_prop_remove_all(dip); 5766fa9e4066Sahrens ddi_remove_minor_node(dip, NULL); 5767fa9e4066Sahrens 5768fa9e4066Sahrens return (DDI_SUCCESS); 5769fa9e4066Sahrens } 5770fa9e4066Sahrens 5771fa9e4066Sahrens /*ARGSUSED*/ 5772fa9e4066Sahrens static int 5773fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 5774fa9e4066Sahrens { 5775fa9e4066Sahrens switch (infocmd) { 5776fa9e4066Sahrens case DDI_INFO_DEVT2DEVINFO: 5777fa9e4066Sahrens *result = zfs_dip; 5778fa9e4066Sahrens return (DDI_SUCCESS); 5779fa9e4066Sahrens 5780fa9e4066Sahrens case DDI_INFO_DEVT2INSTANCE: 5781a0965f35Sbonwick *result = (void *)0; 5782fa9e4066Sahrens return (DDI_SUCCESS); 5783fa9e4066Sahrens } 5784fa9e4066Sahrens 5785fa9e4066Sahrens return (DDI_FAILURE); 5786fa9e4066Sahrens } 5787fa9e4066Sahrens 5788fa9e4066Sahrens /* 5789fa9e4066Sahrens * OK, so this is a little weird. 5790fa9e4066Sahrens * 5791fa9e4066Sahrens * /dev/zfs is the control node, i.e. minor 0. 5792fa9e4066Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 5793fa9e4066Sahrens * 5794fa9e4066Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 5795fa9e4066Sahrens * so most of the standard driver entry points are in zvol.c. 5796fa9e4066Sahrens */ 5797fa9e4066Sahrens static struct cb_ops zfs_cb_ops = { 5798c99e4bdcSChris Kirby zfsdev_open, /* open */ 5799c99e4bdcSChris Kirby zfsdev_close, /* close */ 5800fa9e4066Sahrens zvol_strategy, /* strategy */ 5801fa9e4066Sahrens nodev, /* print */ 5802e7cbe64fSgw zvol_dump, /* dump */ 5803fa9e4066Sahrens zvol_read, /* read */ 5804fa9e4066Sahrens zvol_write, /* write */ 5805fa9e4066Sahrens zfsdev_ioctl, /* ioctl */ 5806fa9e4066Sahrens nodev, /* devmap */ 5807fa9e4066Sahrens nodev, /* mmap */ 5808fa9e4066Sahrens nodev, /* segmap */ 5809fa9e4066Sahrens nochpoll, /* poll */ 5810fa9e4066Sahrens ddi_prop_op, /* prop_op */ 5811fa9e4066Sahrens NULL, /* streamtab */ 5812fa9e4066Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 5813fa9e4066Sahrens CB_REV, /* version */ 5814feb08c6bSbillm nodev, /* async read */ 5815feb08c6bSbillm nodev, /* async write */ 5816fa9e4066Sahrens }; 5817fa9e4066Sahrens 5818fa9e4066Sahrens static struct dev_ops zfs_dev_ops = { 5819fa9e4066Sahrens DEVO_REV, /* version */ 5820fa9e4066Sahrens 0, /* refcnt */ 5821fa9e4066Sahrens zfs_info, /* info */ 5822fa9e4066Sahrens nulldev, /* identify */ 5823fa9e4066Sahrens nulldev, /* probe */ 5824fa9e4066Sahrens zfs_attach, /* attach */ 5825fa9e4066Sahrens zfs_detach, /* detach */ 5826fa9e4066Sahrens nodev, /* reset */ 5827fa9e4066Sahrens &zfs_cb_ops, /* driver operations */ 582819397407SSherry Moore NULL, /* no bus operations */ 582919397407SSherry Moore NULL, /* power */ 583019397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 5831fa9e4066Sahrens }; 5832fa9e4066Sahrens 5833fa9e4066Sahrens static struct modldrv zfs_modldrv = { 583419397407SSherry Moore &mod_driverops, 583519397407SSherry Moore "ZFS storage pool", 583619397407SSherry Moore &zfs_dev_ops 5837fa9e4066Sahrens }; 5838fa9e4066Sahrens 5839fa9e4066Sahrens static struct modlinkage modlinkage = { 5840fa9e4066Sahrens MODREV_1, 5841fa9e4066Sahrens (void *)&zfs_modlfs, 5842fa9e4066Sahrens (void *)&zfs_modldrv, 5843fa9e4066Sahrens NULL 5844fa9e4066Sahrens }; 5845fa9e4066Sahrens 58464445fffbSMatthew Ahrens static void 58474445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg) 58484445fffbSMatthew Ahrens { 58494445fffbSMatthew Ahrens char *poolname = arg; 58504445fffbSMatthew Ahrens strfree(poolname); 58514445fffbSMatthew Ahrens } 5852ec533521Sfr 5853fa9e4066Sahrens int 5854fa9e4066Sahrens _init(void) 5855fa9e4066Sahrens { 5856fa9e4066Sahrens int error; 5857fa9e4066Sahrens 5858a0965f35Sbonwick spa_init(FREAD | FWRITE); 5859a0965f35Sbonwick zfs_init(); 5860a0965f35Sbonwick zvol_init(); 58614445fffbSMatthew Ahrens zfs_ioctl_init(); 5862a0965f35Sbonwick 5863a0965f35Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 5864a0965f35Sbonwick zvol_fini(); 5865a0965f35Sbonwick zfs_fini(); 5866a0965f35Sbonwick spa_fini(); 5867fa9e4066Sahrens return (error); 5868a0965f35Sbonwick } 5869fa9e4066Sahrens 5870ec533521Sfr tsd_create(&zfs_fsyncer_key, NULL); 58714445fffbSMatthew Ahrens tsd_create(&rrw_tsd_key, rrw_tsd_destroy); 58724445fffbSMatthew Ahrens tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy); 5873ec533521Sfr 5874fa9e4066Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 5875fa9e4066Sahrens ASSERT(error == 0); 5876ecd6cf80Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 5877fa9e4066Sahrens 5878fa9e4066Sahrens return (0); 5879fa9e4066Sahrens } 5880fa9e4066Sahrens 5881fa9e4066Sahrens int 5882fa9e4066Sahrens _fini(void) 5883fa9e4066Sahrens { 5884fa9e4066Sahrens int error; 5885fa9e4066Sahrens 5886ea8dc4b6Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 5887be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 5888fa9e4066Sahrens 5889fa9e4066Sahrens if ((error = mod_remove(&modlinkage)) != 0) 5890fa9e4066Sahrens return (error); 5891fa9e4066Sahrens 5892fa9e4066Sahrens zvol_fini(); 5893fa9e4066Sahrens zfs_fini(); 5894fa9e4066Sahrens spa_fini(); 5895da6c28aaSamw if (zfs_nfsshare_inited) 5896ecd6cf80Smarks (void) ddi_modclose(nfs_mod); 5897da6c28aaSamw if (zfs_smbshare_inited) 5898da6c28aaSamw (void) ddi_modclose(smbsrv_mod); 5899da6c28aaSamw if (zfs_nfsshare_inited || zfs_smbshare_inited) 5900ecd6cf80Smarks (void) ddi_modclose(sharefs_mod); 5901fa9e4066Sahrens 5902ec533521Sfr tsd_destroy(&zfs_fsyncer_key); 5903fa9e4066Sahrens ldi_ident_release(zfs_li); 5904fa9e4066Sahrens zfs_li = NULL; 5905ecd6cf80Smarks mutex_destroy(&zfs_share_lock); 5906fa9e4066Sahrens 5907fa9e4066Sahrens return (error); 5908fa9e4066Sahrens } 5909fa9e4066Sahrens 5910fa9e4066Sahrens int 5911fa9e4066Sahrens _info(struct modinfo *modinfop) 5912fa9e4066Sahrens { 5913fa9e4066Sahrens return (mod_info(&modlinkage, modinfop)); 5914fa9e4066Sahrens } 5915