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
255878fad7SDan McDonald * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26752fd8daSJosef 'Jeff' Sipek * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27a2afb611SJerry Jelinek * Copyright (c) 2014, Joyent, Inc. All rights reserved.
286de9bb56SMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
29a6f561b4SSašo Kiselkov * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
30a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
31c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
324445fffbSMatthew Ahrens */
334445fffbSMatthew Ahrens
344445fffbSMatthew Ahrens /*
354445fffbSMatthew Ahrens * ZFS ioctls.
364445fffbSMatthew Ahrens *
374445fffbSMatthew Ahrens * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
384445fffbSMatthew Ahrens * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
394445fffbSMatthew Ahrens *
404445fffbSMatthew Ahrens * There are two ways that we handle ioctls: the legacy way where almost
414445fffbSMatthew Ahrens * all of the logic is in the ioctl callback, and the new way where most
424445fffbSMatthew Ahrens * of the marshalling is handled in the common entry point, zfsdev_ioctl().
434445fffbSMatthew Ahrens *
444445fffbSMatthew Ahrens * Non-legacy ioctls should be registered by calling
454445fffbSMatthew Ahrens * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
464445fffbSMatthew Ahrens * from userland by lzc_ioctl().
474445fffbSMatthew Ahrens *
484445fffbSMatthew Ahrens * The registration arguments are as follows:
494445fffbSMatthew Ahrens *
504445fffbSMatthew Ahrens * const char *name
514445fffbSMatthew Ahrens * The name of the ioctl. This is used for history logging. If the
524445fffbSMatthew Ahrens * ioctl returns successfully (the callback returns 0), and allow_log
534445fffbSMatthew Ahrens * is true, then a history log entry will be recorded with the input &
544445fffbSMatthew Ahrens * output nvlists. The log entry can be printed with "zpool history -i".
554445fffbSMatthew Ahrens *
564445fffbSMatthew Ahrens * zfs_ioc_t ioc
574445fffbSMatthew Ahrens * The ioctl request number, which userland will pass to ioctl(2).
584445fffbSMatthew Ahrens * The ioctl numbers can change from release to release, because
594445fffbSMatthew Ahrens * the caller (libzfs) must be matched to the kernel.
604445fffbSMatthew Ahrens *
614445fffbSMatthew Ahrens * zfs_secpolicy_func_t *secpolicy
624445fffbSMatthew Ahrens * This function will be called before the zfs_ioc_func_t, to
634445fffbSMatthew Ahrens * determine if this operation is permitted. It should return EPERM
644445fffbSMatthew Ahrens * on failure, and 0 on success. Checks include determining if the
654445fffbSMatthew Ahrens * dataset is visible in this zone, and if the user has either all
664445fffbSMatthew Ahrens * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
674445fffbSMatthew Ahrens * to do this operation on this dataset with "zfs allow".
684445fffbSMatthew Ahrens *
694445fffbSMatthew Ahrens * zfs_ioc_namecheck_t namecheck
704445fffbSMatthew Ahrens * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
714445fffbSMatthew Ahrens * name, a dataset name, or nothing. If the name is not well-formed,
724445fffbSMatthew Ahrens * the ioctl will fail and the callback will not be called.
734445fffbSMatthew Ahrens * Therefore, the callback can assume that the name is well-formed
744445fffbSMatthew Ahrens * (e.g. is null-terminated, doesn't have more than one '@' character,
754445fffbSMatthew Ahrens * doesn't have invalid characters).
764445fffbSMatthew Ahrens *
774445fffbSMatthew Ahrens * zfs_ioc_poolcheck_t pool_check
784445fffbSMatthew Ahrens * This specifies requirements on the pool state. If the pool does
794445fffbSMatthew Ahrens * not meet them (is suspended or is readonly), the ioctl will fail
804445fffbSMatthew Ahrens * and the callback will not be called. If any checks are specified
814445fffbSMatthew Ahrens * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
824445fffbSMatthew Ahrens * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
834445fffbSMatthew Ahrens * POOL_CHECK_READONLY).
844445fffbSMatthew Ahrens *
854445fffbSMatthew Ahrens * boolean_t smush_outnvlist
864445fffbSMatthew Ahrens * If smush_outnvlist is true, then the output is presumed to be a
874445fffbSMatthew Ahrens * list of errors, and it will be "smushed" down to fit into the
884445fffbSMatthew Ahrens * caller's buffer, by removing some entries and replacing them with a
894445fffbSMatthew Ahrens * single "N_MORE_ERRORS" entry indicating how many were removed. See
904445fffbSMatthew Ahrens * nvlist_smush() for details. If smush_outnvlist is false, and the
914445fffbSMatthew Ahrens * outnvlist does not fit into the userland-provided buffer, then the
924445fffbSMatthew Ahrens * ioctl will fail with ENOMEM.
934445fffbSMatthew Ahrens *
944445fffbSMatthew Ahrens * zfs_ioc_func_t *func
954445fffbSMatthew Ahrens * The callback function that will perform the operation.
964445fffbSMatthew Ahrens *
974445fffbSMatthew Ahrens * The callback should return 0 on success, or an error number on
984445fffbSMatthew Ahrens * failure. If the function fails, the userland ioctl will return -1,
994445fffbSMatthew Ahrens * and errno will be set to the callback's return value. The callback
1004445fffbSMatthew Ahrens * will be called with the following arguments:
1014445fffbSMatthew Ahrens *
1024445fffbSMatthew Ahrens * const char *name
1034445fffbSMatthew Ahrens * The name of the pool or dataset to operate on, from
1044445fffbSMatthew Ahrens * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
1054445fffbSMatthew Ahrens * expected type (pool, dataset, or none).
1064445fffbSMatthew Ahrens *
1074445fffbSMatthew Ahrens * nvlist_t *innvl
1084445fffbSMatthew Ahrens * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
1094445fffbSMatthew Ahrens * NULL if no input nvlist was provided. Changes to this nvlist are
1104445fffbSMatthew Ahrens * ignored. If the input nvlist could not be deserialized, the
1114445fffbSMatthew Ahrens * ioctl will fail and the callback will not be called.
1124445fffbSMatthew Ahrens *
1134445fffbSMatthew Ahrens * nvlist_t *outnvl
1144445fffbSMatthew Ahrens * The output nvlist, initially empty. The callback can fill it in,
1154445fffbSMatthew Ahrens * and it will be returned to userland by serializing it into
1164445fffbSMatthew Ahrens * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
1174445fffbSMatthew Ahrens * fails (e.g. because the caller didn't supply a large enough
1184445fffbSMatthew Ahrens * buffer), then the overall ioctl will fail. See the
1194445fffbSMatthew Ahrens * 'smush_nvlist' argument above for additional behaviors.
1204445fffbSMatthew Ahrens *
1214445fffbSMatthew Ahrens * There are two typical uses of the output nvlist:
1224445fffbSMatthew Ahrens * - To return state, e.g. property values. In this case,
1234445fffbSMatthew Ahrens * smush_outnvlist should be false. If the buffer was not large
1244445fffbSMatthew Ahrens * enough, the caller will reallocate a larger buffer and try
1254445fffbSMatthew Ahrens * the ioctl again.
1264445fffbSMatthew Ahrens *
1274445fffbSMatthew Ahrens * - To return multiple errors from an ioctl which makes on-disk
1284445fffbSMatthew Ahrens * changes. In this case, smush_outnvlist should be true.
1294445fffbSMatthew Ahrens * Ioctls which make on-disk modifications should generally not
1304445fffbSMatthew Ahrens * use the outnvl if they succeed, because the caller can not
1314445fffbSMatthew Ahrens * distinguish between the operation failing, and
1324445fffbSMatthew Ahrens * deserialization failing.
133e9103aaeSGarrett D'Amore */
134fa9e4066Sahrens
135fa9e4066Sahrens #include <sys/types.h>
136fa9e4066Sahrens #include <sys/param.h>
137fa9e4066Sahrens #include <sys/errno.h>
138fa9e4066Sahrens #include <sys/uio.h>
139fa9e4066Sahrens #include <sys/buf.h>
140fa9e4066Sahrens #include <sys/modctl.h>
141fa9e4066Sahrens #include <sys/open.h>
142fa9e4066Sahrens #include <sys/file.h>
143fa9e4066Sahrens #include <sys/kmem.h>
144fa9e4066Sahrens #include <sys/conf.h>
145fa9e4066Sahrens #include <sys/cmn_err.h>
146fa9e4066Sahrens #include <sys/stat.h>
147fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1484201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
149da6c28aaSamw #include <sys/zfs_znode.h>
150fa9e4066Sahrens #include <sys/zap.h>
151fa9e4066Sahrens #include <sys/spa.h>
152b1b8ab34Slling #include <sys/spa_impl.h>
153fa9e4066Sahrens #include <sys/vdev.h>
1544201a95eSRic Aleshire #include <sys/priv_impl.h>
155fa9e4066Sahrens #include <sys/dmu.h>
156fa9e4066Sahrens #include <sys/dsl_dir.h>
157fa9e4066Sahrens #include <sys/dsl_dataset.h>
158fa9e4066Sahrens #include <sys/dsl_prop.h>
159ecd6cf80Smarks #include <sys/dsl_deleg.h>
160ecd6cf80Smarks #include <sys/dmu_objset.h>
1614e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1623b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
163fa9e4066Sahrens #include <sys/ddi.h>
164fa9e4066Sahrens #include <sys/sunddi.h>
165fa9e4066Sahrens #include <sys/sunldi.h>
166fa9e4066Sahrens #include <sys/policy.h>
167fa9e4066Sahrens #include <sys/zone.h>
168fa9e4066Sahrens #include <sys/nvpair.h>
169fa9e4066Sahrens #include <sys/pathname.h>
170fa9e4066Sahrens #include <sys/mount.h>
171fa9e4066Sahrens #include <sys/sdt.h>
172fa9e4066Sahrens #include <sys/fs/zfs.h>
173fa9e4066Sahrens #include <sys/zfs_ctldir.h>
174da6c28aaSamw #include <sys/zfs_dir.h>
175c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
176a2eea2e1Sahrens #include <sys/zvol.h>
1773f9d6ad7SLin Ling #include <sys/dsl_scan.h>
178ecd6cf80Smarks #include <sharefs/share.h>
179f18faf3fSek110237 #include <sys/dmu_objset.h>
1803b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1813b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
18278f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
1833b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
184a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
18545818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
186d78b796cSAndreas Jaekel #include <sys/zfs_events.h>
187fa9e4066Sahrens
188fa9e4066Sahrens #include "zfs_namecheck.h"
189e9dbad6fSeschrock #include "zfs_prop.h"
190ecd6cf80Smarks #include "zfs_deleg.h"
1910a586ceaSMark Shellenbaum #include "zfs_comutil.h"
192fa9e4066Sahrens
193fa9e4066Sahrens extern struct modlfs zfs_modlfs;
194fa9e4066Sahrens
195fa9e4066Sahrens extern void zfs_init(void);
196fa9e4066Sahrens extern void zfs_fini(void);
197fa9e4066Sahrens
198fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
199fa9e4066Sahrens dev_info_t *zfs_dip;
200fa9e4066Sahrens
2014445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
2024445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
2034445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
2044445fffbSMatthew Ahrens
2054445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2064445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2074445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
208fa9e4066Sahrens
20954d692b7SGeorge Wilson typedef enum {
210e7437265Sahrens NO_NAME,
211e7437265Sahrens POOL_NAME,
212e7437265Sahrens DATASET_NAME
21354d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
21454d692b7SGeorge Wilson
215f9af39baSGeorge Wilson typedef enum {
216f9af39baSGeorge Wilson POOL_CHECK_NONE = 1 << 0,
217f9af39baSGeorge Wilson POOL_CHECK_SUSPENDED = 1 << 1,
2184445fffbSMatthew Ahrens POOL_CHECK_READONLY = 1 << 2,
219f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
220f9af39baSGeorge Wilson
22154d692b7SGeorge Wilson typedef struct zfs_ioc_vec {
2224445fffbSMatthew Ahrens zfs_ioc_legacy_func_t *zvec_legacy_func;
22354d692b7SGeorge Wilson zfs_ioc_func_t *zvec_func;
22454d692b7SGeorge Wilson zfs_secpolicy_func_t *zvec_secpolicy;
22554d692b7SGeorge Wilson zfs_ioc_namecheck_t zvec_namecheck;
2264445fffbSMatthew Ahrens boolean_t zvec_allow_log;
227f9af39baSGeorge Wilson zfs_ioc_poolcheck_t zvec_pool_check;
2284445fffbSMatthew Ahrens boolean_t zvec_smush_outnvlist;
2294445fffbSMatthew Ahrens const char *zvec_name;
230fa9e4066Sahrens } zfs_ioc_vec_t;
231fa9e4066Sahrens
23214843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
23314843421SMatthew Ahrens static const char *userquota_perms[] = {
23414843421SMatthew Ahrens ZFS_DELEG_PERM_USERUSED,
23514843421SMatthew Ahrens ZFS_DELEG_PERM_USERQUOTA,
23614843421SMatthew Ahrens ZFS_DELEG_PERM_GROUPUSED,
23714843421SMatthew Ahrens ZFS_DELEG_PERM_GROUPQUOTA,
23814843421SMatthew Ahrens };
23914843421SMatthew Ahrens
24014843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
24192241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
24292241e0bSTom Erickson cred_t *cr);
24392241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
24492241e0bSTom Erickson nvlist_t **errors);
2450a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2460a48a24eStimh boolean_t *);
2474445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2484445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2490a48a24eStimh
2502acef22dSMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
251a6f561b4SSašo Kiselkov
252fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
253fa9e4066Sahrens void
__dprintf(const char * file,const char * func,int line,const char * fmt,...)254fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
255fa9e4066Sahrens {
256fa9e4066Sahrens const char *newfile;
2573f9d6ad7SLin Ling char buf[512];
258fa9e4066Sahrens va_list adx;
259fa9e4066Sahrens
260fa9e4066Sahrens /*
261fa9e4066Sahrens * Get rid of annoying "../common/" prefix to filename.
262fa9e4066Sahrens */
263fa9e4066Sahrens newfile = strrchr(file, '/');
264fa9e4066Sahrens if (newfile != NULL) {
265fa9e4066Sahrens newfile = newfile + 1; /* Get rid of leading / */
266fa9e4066Sahrens } else {
267fa9e4066Sahrens newfile = file;
268fa9e4066Sahrens }
269fa9e4066Sahrens
270fa9e4066Sahrens va_start(adx, fmt);
271fa9e4066Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, adx);
272fa9e4066Sahrens va_end(adx);
273fa9e4066Sahrens
274fa9e4066Sahrens /*
275fa9e4066Sahrens * To get this data, use the zfs-dprintf probe as so:
276fa9e4066Sahrens * dtrace -q -n 'zfs-dprintf \
277fa9e4066Sahrens * /stringof(arg0) == "dbuf.c"/ \
278fa9e4066Sahrens * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
279fa9e4066Sahrens * arg0 = file name
280fa9e4066Sahrens * arg1 = function name
281fa9e4066Sahrens * arg2 = line number
282fa9e4066Sahrens * arg3 = message
283fa9e4066Sahrens */
284fa9e4066Sahrens DTRACE_PROBE4(zfs__dprintf,
285fa9e4066Sahrens char *, newfile, char *, func, int, line, char *, buf);
286fa9e4066Sahrens }
287fa9e4066Sahrens
288ecd6cf80Smarks static void
history_str_free(char * buf)289228975ccSek110237 history_str_free(char *buf)
290228975ccSek110237 {
291228975ccSek110237 kmem_free(buf, HIS_MAX_RECORD_LEN);
292228975ccSek110237 }
293228975ccSek110237
294228975ccSek110237 static char *
history_str_get(zfs_cmd_t * zc)295228975ccSek110237 history_str_get(zfs_cmd_t *zc)
296228975ccSek110237 {
297228975ccSek110237 char *buf;
298228975ccSek110237
299228975ccSek110237 if (zc->zc_history == NULL)
300228975ccSek110237 return (NULL);
301228975ccSek110237
302228975ccSek110237 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
303228975ccSek110237 if (copyinstr((void *)(uintptr_t)zc->zc_history,
304228975ccSek110237 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
305228975ccSek110237 history_str_free(buf);
306228975ccSek110237 return (NULL);
307228975ccSek110237 }
308228975ccSek110237
309228975ccSek110237 buf[HIS_MAX_RECORD_LEN -1] = '\0';
310228975ccSek110237
311228975ccSek110237 return (buf);
312228975ccSek110237 }
313228975ccSek110237
314c2a93d44Stimh /*
31515e6edf1Sgw25295 * Check to see if the named dataset is currently defined as bootable
31615e6edf1Sgw25295 */
31715e6edf1Sgw25295 static boolean_t
zfs_is_bootfs(const char * name)31815e6edf1Sgw25295 zfs_is_bootfs(const char *name)
31915e6edf1Sgw25295 {
32015e6edf1Sgw25295 objset_t *os;
32115e6edf1Sgw25295
322503ad85cSMatthew Ahrens if (dmu_objset_hold(name, FTAG, &os) == 0) {
323503ad85cSMatthew Ahrens boolean_t ret;
324b24ab676SJeff Bonwick ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
325503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
32615e6edf1Sgw25295 return (ret);
32715e6edf1Sgw25295 }
328503ad85cSMatthew Ahrens return (B_FALSE);
329503ad85cSMatthew Ahrens }
33015e6edf1Sgw25295
33115e6edf1Sgw25295 /*
332c2a93d44Stimh * Return non-zero if the spa version is less than requested version.
333c2a93d44Stimh */
334da6c28aaSamw static int
zfs_earlier_version(const char * name,int version)3350a48a24eStimh zfs_earlier_version(const char *name, int version)
336da6c28aaSamw {
337da6c28aaSamw spa_t *spa;
338da6c28aaSamw
339da6c28aaSamw if (spa_open(name, &spa, FTAG) == 0) {
340da6c28aaSamw if (spa_version(spa) < version) {
341da6c28aaSamw spa_close(spa, FTAG);
342da6c28aaSamw return (1);
343da6c28aaSamw }
344da6c28aaSamw spa_close(spa, FTAG);
345da6c28aaSamw }
346da6c28aaSamw return (0);
347da6c28aaSamw }
348da6c28aaSamw
3499e6eda55Smarks /*
350745cd3c5Smaybee * Return TRUE if the ZPL version is less than requested version.
3519e6eda55Smarks */
352745cd3c5Smaybee static boolean_t
zpl_earlier_version(const char * name,int version)353745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3549e6eda55Smarks {
3559e6eda55Smarks objset_t *os;
356745cd3c5Smaybee boolean_t rc = B_TRUE;
3579e6eda55Smarks
358503ad85cSMatthew Ahrens if (dmu_objset_hold(name, FTAG, &os) == 0) {
359745cd3c5Smaybee uint64_t zplversion;
3609e6eda55Smarks
361503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) {
362503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
363503ad85cSMatthew Ahrens return (B_TRUE);
364503ad85cSMatthew Ahrens }
365503ad85cSMatthew Ahrens /* XXX reading from non-owned objset */
366745cd3c5Smaybee if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
367745cd3c5Smaybee rc = zplversion < version;
368503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
3699e6eda55Smarks }
3709e6eda55Smarks return (rc);
3719e6eda55Smarks }
3729e6eda55Smarks
373228975ccSek110237 static void
zfs_log_history(zfs_cmd_t * zc)374ecd6cf80Smarks zfs_log_history(zfs_cmd_t *zc)
375ecd6cf80Smarks {
376ecd6cf80Smarks spa_t *spa;
37740feaa91Sahrens char *buf;
378ecd6cf80Smarks
379228975ccSek110237 if ((buf = history_str_get(zc)) == NULL)
380ecd6cf80Smarks return;
381ecd6cf80Smarks
382228975ccSek110237 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
383e7437265Sahrens if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
3844445fffbSMatthew Ahrens (void) spa_history_log(spa, buf);
385ecd6cf80Smarks spa_close(spa, FTAG);
386228975ccSek110237 }
387228975ccSek110237 history_str_free(buf);
388ecd6cf80Smarks }
389ecd6cf80Smarks
390fa9e4066Sahrens /*
391fa9e4066Sahrens * Policy for top-level read operations (list pools). Requires no privileges,
392fa9e4066Sahrens * and can be used in the local zone, as there is no associated dataset.
393fa9e4066Sahrens */
394fa9e4066Sahrens /* ARGSUSED */
395fa9e4066Sahrens static int
zfs_secpolicy_none(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)3964445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
397fa9e4066Sahrens {
398fa9e4066Sahrens return (0);
399fa9e4066Sahrens }
400fa9e4066Sahrens
401fa9e4066Sahrens /*
402fa9e4066Sahrens * Policy for dataset read operations (list children, get statistics). Requires
403fa9e4066Sahrens * no privileges, but must be visible in the local zone.
404fa9e4066Sahrens */
405fa9e4066Sahrens /* ARGSUSED */
406fa9e4066Sahrens static int
zfs_secpolicy_read(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)4074445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
408fa9e4066Sahrens {
409fa9e4066Sahrens if (INGLOBALZONE(curproc) ||
410ecd6cf80Smarks zone_dataset_visible(zc->zc_name, NULL))
411fa9e4066Sahrens return (0);
412fa9e4066Sahrens
413be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
414fa9e4066Sahrens }
415fa9e4066Sahrens
416fa9e4066Sahrens static int
zfs_dozonecheck_impl(const char * dataset,uint64_t zoned,cred_t * cr)417a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
418fa9e4066Sahrens {
419fa9e4066Sahrens int writable = 1;
420fa9e4066Sahrens
421fa9e4066Sahrens /*
422fa9e4066Sahrens * The dataset must be visible by this zone -- check this first
423fa9e4066Sahrens * so they don't see EPERM on something they shouldn't know about.
424fa9e4066Sahrens */
425fa9e4066Sahrens if (!INGLOBALZONE(curproc) &&
426fa9e4066Sahrens !zone_dataset_visible(dataset, &writable))
427be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
428fa9e4066Sahrens
429fa9e4066Sahrens if (INGLOBALZONE(curproc)) {
430fa9e4066Sahrens /*
431fa9e4066Sahrens * If the fs is zoned, only root can access it from the
432fa9e4066Sahrens * global zone.
433fa9e4066Sahrens */
434fa9e4066Sahrens if (secpolicy_zfs(cr) && zoned)
435be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
436fa9e4066Sahrens } else {
437fa9e4066Sahrens /*
438fa9e4066Sahrens * If we are in a local zone, the 'zoned' property must be set.
439fa9e4066Sahrens */
440fa9e4066Sahrens if (!zoned)
441be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
442fa9e4066Sahrens
443fa9e4066Sahrens /* must be writable by this zone */
444fa9e4066Sahrens if (!writable)
445be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
446fa9e4066Sahrens }
447fa9e4066Sahrens return (0);
448fa9e4066Sahrens }
449fa9e4066Sahrens
450a7f53a56SChris Kirby static int
zfs_dozonecheck(const char * dataset,cred_t * cr)451a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
452a7f53a56SChris Kirby {
453a7f53a56SChris Kirby uint64_t zoned;
454a7f53a56SChris Kirby
455a7f53a56SChris Kirby if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
456be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
457a7f53a56SChris Kirby
458a7f53a56SChris Kirby return (zfs_dozonecheck_impl(dataset, zoned, cr));
459a7f53a56SChris Kirby }
460a7f53a56SChris Kirby
461a7f53a56SChris Kirby static int
zfs_dozonecheck_ds(const char * dataset,dsl_dataset_t * ds,cred_t * cr)462a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
463a7f53a56SChris Kirby {
464a7f53a56SChris Kirby uint64_t zoned;
465a7f53a56SChris Kirby
4663b2aab18SMatthew Ahrens if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
467be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
468a7f53a56SChris Kirby
469a7f53a56SChris Kirby return (zfs_dozonecheck_impl(dataset, zoned, cr));
470a7f53a56SChris Kirby }
471a7f53a56SChris Kirby
4724445fffbSMatthew Ahrens static int
zfs_secpolicy_write_perms_ds(const char * name,dsl_dataset_t * ds,const char * perm,cred_t * cr)473a7f53a56SChris Kirby zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
474a7f53a56SChris Kirby const char *perm, cred_t *cr)
475a7f53a56SChris Kirby {
476a7f53a56SChris Kirby int error;
477a7f53a56SChris Kirby
478a7f53a56SChris Kirby error = zfs_dozonecheck_ds(name, ds, cr);
479a7f53a56SChris Kirby if (error == 0) {
480a7f53a56SChris Kirby error = secpolicy_zfs(cr);
4813b2aab18SMatthew Ahrens if (error != 0)
4824445fffbSMatthew Ahrens error = dsl_deleg_access_impl(ds, perm, cr);
483a7f53a56SChris Kirby }
484a7f53a56SChris Kirby return (error);
485a7f53a56SChris Kirby }
486a7f53a56SChris Kirby
4873b2aab18SMatthew Ahrens static int
zfs_secpolicy_write_perms(const char * name,const char * perm,cred_t * cr)4883b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
4893b2aab18SMatthew Ahrens {
4903b2aab18SMatthew Ahrens int error;
4913b2aab18SMatthew Ahrens dsl_dataset_t *ds;
4923b2aab18SMatthew Ahrens dsl_pool_t *dp;
4933b2aab18SMatthew Ahrens
4943b2aab18SMatthew Ahrens error = dsl_pool_hold(name, FTAG, &dp);
4953b2aab18SMatthew Ahrens if (error != 0)
4963b2aab18SMatthew Ahrens return (error);
4973b2aab18SMatthew Ahrens
4983b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, name, FTAG, &ds);
4993b2aab18SMatthew Ahrens if (error != 0) {
5003b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
5013b2aab18SMatthew Ahrens return (error);
5023b2aab18SMatthew Ahrens }
5033b2aab18SMatthew Ahrens
5043b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5053b2aab18SMatthew Ahrens
5063b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
5073b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
5083b2aab18SMatthew Ahrens return (error);
5093b2aab18SMatthew Ahrens }
5103b2aab18SMatthew Ahrens
5114201a95eSRic Aleshire /*
5124201a95eSRic Aleshire * Policy for setting the security label property.
5134201a95eSRic Aleshire *
5144201a95eSRic Aleshire * Returns 0 for success, non-zero for access and other errors.
5154201a95eSRic Aleshire */
5164201a95eSRic Aleshire static int
zfs_set_slabel_policy(const char * name,char * strval,cred_t * cr)51792241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5184201a95eSRic Aleshire {
5194201a95eSRic Aleshire char ds_hexsl[MAXNAMELEN];
5204201a95eSRic Aleshire bslabel_t ds_sl, new_sl;
5214201a95eSRic Aleshire boolean_t new_default = FALSE;
5224201a95eSRic Aleshire uint64_t zoned;
5234201a95eSRic Aleshire int needed_priv = -1;
5244201a95eSRic Aleshire int error;
5254201a95eSRic Aleshire
5264201a95eSRic Aleshire /* First get the existing dataset label. */
5274201a95eSRic Aleshire error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5284201a95eSRic Aleshire 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5293b2aab18SMatthew Ahrens if (error != 0)
530be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
5314201a95eSRic Aleshire
5324201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5334201a95eSRic Aleshire new_default = TRUE;
5344201a95eSRic Aleshire
5354201a95eSRic Aleshire /* The label must be translatable */
5364201a95eSRic Aleshire if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
537be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
5384201a95eSRic Aleshire
5394201a95eSRic Aleshire /*
5404201a95eSRic Aleshire * In a non-global zone, disallow attempts to set a label that
5414201a95eSRic Aleshire * doesn't match that of the zone; otherwise no other checks
5424201a95eSRic Aleshire * are needed.
5434201a95eSRic Aleshire */
5444201a95eSRic Aleshire if (!INGLOBALZONE(curproc)) {
5454201a95eSRic Aleshire if (new_default || !blequal(&new_sl, CR_SL(CRED())))
546be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
5474201a95eSRic Aleshire return (0);
5484201a95eSRic Aleshire }
5494201a95eSRic Aleshire
5504201a95eSRic Aleshire /*
5514201a95eSRic Aleshire * For global-zone datasets (i.e., those whose zoned property is
5524201a95eSRic Aleshire * "off", verify that the specified new label is valid for the
5534201a95eSRic Aleshire * global zone.
5544201a95eSRic Aleshire */
5554201a95eSRic Aleshire if (dsl_prop_get_integer(name,
5564201a95eSRic Aleshire zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
557be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
5584201a95eSRic Aleshire if (!zoned) {
5594201a95eSRic Aleshire if (zfs_check_global_label(name, strval) != 0)
560be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
5614201a95eSRic Aleshire }
5624201a95eSRic Aleshire
5634201a95eSRic Aleshire /*
5644201a95eSRic Aleshire * If the existing dataset label is nondefault, check if the
5654201a95eSRic Aleshire * dataset is mounted (label cannot be changed while mounted).
5664201a95eSRic Aleshire * Get the zfsvfs; if there isn't one, then the dataset isn't
5674201a95eSRic Aleshire * mounted (or isn't a dataset, doesn't exist, ...).
5684201a95eSRic Aleshire */
5694201a95eSRic Aleshire if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
57092241e0bSTom Erickson objset_t *os;
57192241e0bSTom Erickson static char *setsl_tag = "setsl_tag";
57292241e0bSTom Erickson
5734201a95eSRic Aleshire /*
5744201a95eSRic Aleshire * Try to own the dataset; abort if there is any error,
5754201a95eSRic Aleshire * (e.g., already mounted, in use, or other error).
5764201a95eSRic Aleshire */
5774201a95eSRic Aleshire error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
57892241e0bSTom Erickson setsl_tag, &os);
5793b2aab18SMatthew Ahrens if (error != 0)
580be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
5814201a95eSRic Aleshire
58292241e0bSTom Erickson dmu_objset_disown(os, setsl_tag);
58392241e0bSTom Erickson
5844201a95eSRic Aleshire if (new_default) {
5854201a95eSRic Aleshire needed_priv = PRIV_FILE_DOWNGRADE_SL;
5864201a95eSRic Aleshire goto out_check;
5874201a95eSRic Aleshire }
5884201a95eSRic Aleshire
5894201a95eSRic Aleshire if (hexstr_to_label(strval, &new_sl) != 0)
590be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
5914201a95eSRic Aleshire
5924201a95eSRic Aleshire if (blstrictdom(&ds_sl, &new_sl))
5934201a95eSRic Aleshire needed_priv = PRIV_FILE_DOWNGRADE_SL;
5944201a95eSRic Aleshire else if (blstrictdom(&new_sl, &ds_sl))
5954201a95eSRic Aleshire needed_priv = PRIV_FILE_UPGRADE_SL;
5964201a95eSRic Aleshire } else {
5974201a95eSRic Aleshire /* dataset currently has a default label */
5984201a95eSRic Aleshire if (!new_default)
5994201a95eSRic Aleshire needed_priv = PRIV_FILE_UPGRADE_SL;
6004201a95eSRic Aleshire }
6014201a95eSRic Aleshire
6024201a95eSRic Aleshire out_check:
6034201a95eSRic Aleshire if (needed_priv != -1)
6044201a95eSRic Aleshire return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6054201a95eSRic Aleshire return (0);
6064201a95eSRic Aleshire }
6074201a95eSRic Aleshire
608fa9e4066Sahrens static int
zfs_secpolicy_setprop(const char * dsname,zfs_prop_t prop,nvpair_t * propval,cred_t * cr)60992241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
61092241e0bSTom Erickson cred_t *cr)
611fa9e4066Sahrens {
61292241e0bSTom Erickson char *strval;
61392241e0bSTom Erickson
614ecd6cf80Smarks /*
615ecd6cf80Smarks * Check permissions for special properties.
616ecd6cf80Smarks */
617ecd6cf80Smarks switch (prop) {
618ecd6cf80Smarks case ZFS_PROP_ZONED:
619ecd6cf80Smarks /*
620ecd6cf80Smarks * Disallow setting of 'zoned' from within a local zone.
621ecd6cf80Smarks */
622ecd6cf80Smarks if (!INGLOBALZONE(curproc))
623be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
624ecd6cf80Smarks break;
625ecd6cf80Smarks
626ecd6cf80Smarks case ZFS_PROP_QUOTA:
627a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT:
628a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT:
629ecd6cf80Smarks if (!INGLOBALZONE(curproc)) {
630ecd6cf80Smarks uint64_t zoned;
631*40a5c998SMatthew Ahrens char setpoint[ZFS_MAX_DATASET_NAME_LEN];
632ecd6cf80Smarks /*
633ecd6cf80Smarks * Unprivileged users are allowed to modify the
634a2afb611SJerry Jelinek * limit on things *under* (ie. contained by)
635ecd6cf80Smarks * the thing they own.
636ecd6cf80Smarks */
63792241e0bSTom Erickson if (dsl_prop_get_integer(dsname, "zoned", &zoned,
638ecd6cf80Smarks setpoint))
639be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
64092241e0bSTom Erickson if (!zoned || strlen(dsname) <= strlen(setpoint))
641be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
642ecd6cf80Smarks }
643db870a07Sahrens break;
6444201a95eSRic Aleshire
6454201a95eSRic Aleshire case ZFS_PROP_MLSLABEL:
6464201a95eSRic Aleshire if (!is_system_labeled())
647be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
64892241e0bSTom Erickson
64992241e0bSTom Erickson if (nvpair_value_string(propval, &strval) == 0) {
65092241e0bSTom Erickson int err;
65192241e0bSTom Erickson
65292241e0bSTom Erickson err = zfs_set_slabel_policy(dsname, strval, CRED());
65392241e0bSTom Erickson if (err != 0)
65492241e0bSTom Erickson return (err);
65592241e0bSTom Erickson }
6564201a95eSRic Aleshire break;
657ecd6cf80Smarks }
658ecd6cf80Smarks
65992241e0bSTom Erickson return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
660ecd6cf80Smarks }
661ecd6cf80Smarks
6624445fffbSMatthew Ahrens /* ARGSUSED */
6634445fffbSMatthew Ahrens static int
zfs_secpolicy_set_fsacl(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)6644445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
665ecd6cf80Smarks {
666ecd6cf80Smarks int error;
667ecd6cf80Smarks
668ecd6cf80Smarks error = zfs_dozonecheck(zc->zc_name, cr);
6693b2aab18SMatthew Ahrens if (error != 0)
670ecd6cf80Smarks return (error);
671ecd6cf80Smarks
672ecd6cf80Smarks /*
673ecd6cf80Smarks * permission to set permissions will be evaluated later in
674ecd6cf80Smarks * dsl_deleg_can_allow()
675ecd6cf80Smarks */
676ecd6cf80Smarks return (0);
677ecd6cf80Smarks }
678ecd6cf80Smarks
6794445fffbSMatthew Ahrens /* ARGSUSED */
6804445fffbSMatthew Ahrens static int
zfs_secpolicy_rollback(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)6814445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
682ecd6cf80Smarks {
683681d9761SEric Taylor return (zfs_secpolicy_write_perms(zc->zc_name,
684681d9761SEric Taylor ZFS_DELEG_PERM_ROLLBACK, cr));
685ecd6cf80Smarks }
686ecd6cf80Smarks
6874445fffbSMatthew Ahrens /* ARGSUSED */
6884445fffbSMatthew Ahrens static int
zfs_secpolicy_send(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)6894445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
690ecd6cf80Smarks {
691a7f53a56SChris Kirby dsl_pool_t *dp;
692a7f53a56SChris Kirby dsl_dataset_t *ds;
693a7f53a56SChris Kirby char *cp;
694a7f53a56SChris Kirby int error;
695a7f53a56SChris Kirby
696a7f53a56SChris Kirby /*
697a7f53a56SChris Kirby * Generate the current snapshot name from the given objsetid, then
698a7f53a56SChris Kirby * use that name for the secpolicy/zone checks.
699a7f53a56SChris Kirby */
700a7f53a56SChris Kirby cp = strchr(zc->zc_name, '@');
701a7f53a56SChris Kirby if (cp == NULL)
702be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
7033b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
7043b2aab18SMatthew Ahrens if (error != 0)
705a7f53a56SChris Kirby return (error);
706a7f53a56SChris Kirby
707a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7083b2aab18SMatthew Ahrens if (error != 0) {
7093b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
710a7f53a56SChris Kirby return (error);
7113b2aab18SMatthew Ahrens }
712a7f53a56SChris Kirby
713a7f53a56SChris Kirby dsl_dataset_name(ds, zc->zc_name);
714a7f53a56SChris Kirby
715a7f53a56SChris Kirby error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
716a7f53a56SChris Kirby ZFS_DELEG_PERM_SEND, cr);
717a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG);
7183b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
719a7f53a56SChris Kirby
720a7f53a56SChris Kirby return (error);
721ecd6cf80Smarks }
722ecd6cf80Smarks
7234445fffbSMatthew Ahrens /* ARGSUSED */
724743a77edSAlan Wright static int
zfs_secpolicy_send_new(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)7254445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7264445fffbSMatthew Ahrens {
7274445fffbSMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name,
7284445fffbSMatthew Ahrens ZFS_DELEG_PERM_SEND, cr));
7294445fffbSMatthew Ahrens }
7304445fffbSMatthew Ahrens
7314445fffbSMatthew Ahrens /* ARGSUSED */
7324445fffbSMatthew Ahrens static int
zfs_secpolicy_deleg_share(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)7334445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
734ecd6cf80Smarks {
735ecd6cf80Smarks vnode_t *vp;
736ecd6cf80Smarks int error;
737ecd6cf80Smarks
738ecd6cf80Smarks if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
739ecd6cf80Smarks NO_FOLLOW, NULL, &vp)) != 0)
740ecd6cf80Smarks return (error);
741ecd6cf80Smarks
742ecd6cf80Smarks /* Now make sure mntpnt and dataset are ZFS */
743ecd6cf80Smarks
744ecd6cf80Smarks if (vp->v_vfsp->vfs_fstype != zfsfstype ||
745ecd6cf80Smarks (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
746ecd6cf80Smarks zc->zc_name) != 0)) {
747ecd6cf80Smarks VN_RELE(vp);
748be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
749ecd6cf80Smarks }
750ecd6cf80Smarks
751ecd6cf80Smarks VN_RELE(vp);
752ecd6cf80Smarks return (dsl_deleg_access(zc->zc_name,
753ecd6cf80Smarks ZFS_DELEG_PERM_SHARE, cr));
754ecd6cf80Smarks }
755743a77edSAlan Wright
756743a77edSAlan Wright int
zfs_secpolicy_share(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)7574445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
758743a77edSAlan Wright {
759743a77edSAlan Wright if (!INGLOBALZONE(curproc))
760be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
761743a77edSAlan Wright
762743a77edSAlan Wright if (secpolicy_nfs(cr) == 0) {
763743a77edSAlan Wright return (0);
764743a77edSAlan Wright } else {
7654445fffbSMatthew Ahrens return (zfs_secpolicy_deleg_share(zc, innvl, cr));
766743a77edSAlan Wright }
767743a77edSAlan Wright }
768743a77edSAlan Wright
769743a77edSAlan Wright int
zfs_secpolicy_smb_acl(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)7704445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
771743a77edSAlan Wright {
772743a77edSAlan Wright if (!INGLOBALZONE(curproc))
773be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
774743a77edSAlan Wright
775743a77edSAlan Wright if (secpolicy_smb(cr) == 0) {
776743a77edSAlan Wright return (0);
777743a77edSAlan Wright } else {
7784445fffbSMatthew Ahrens return (zfs_secpolicy_deleg_share(zc, innvl, cr));
779743a77edSAlan Wright }
780ecd6cf80Smarks }
781ecd6cf80Smarks
782ecd6cf80Smarks static int
zfs_get_parent(const char * datasetname,char * parent,int parentsize)783ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
784ecd6cf80Smarks {
785fa9e4066Sahrens char *cp;
786fa9e4066Sahrens
787fa9e4066Sahrens /*
788fa9e4066Sahrens * Remove the @bla or /bla from the end of the name to get the parent.
789fa9e4066Sahrens */
790ecd6cf80Smarks (void) strncpy(parent, datasetname, parentsize);
791ecd6cf80Smarks cp = strrchr(parent, '@');
792fa9e4066Sahrens if (cp != NULL) {
793fa9e4066Sahrens cp[0] = '\0';
794fa9e4066Sahrens } else {
795ecd6cf80Smarks cp = strrchr(parent, '/');
796fa9e4066Sahrens if (cp == NULL)
797be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
798fa9e4066Sahrens cp[0] = '\0';
799fa9e4066Sahrens }
800fa9e4066Sahrens
801ecd6cf80Smarks return (0);
802ecd6cf80Smarks }
803ecd6cf80Smarks
804ecd6cf80Smarks int
zfs_secpolicy_destroy_perms(const char * name,cred_t * cr)805ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
806ecd6cf80Smarks {
807ecd6cf80Smarks int error;
808ecd6cf80Smarks
809ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(name,
810ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0)
811ecd6cf80Smarks return (error);
812ecd6cf80Smarks
813ecd6cf80Smarks return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
814ecd6cf80Smarks }
815ecd6cf80Smarks
8164445fffbSMatthew Ahrens /* ARGSUSED */
817ecd6cf80Smarks static int
zfs_secpolicy_destroy(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)8184445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
819ecd6cf80Smarks {
820ecd6cf80Smarks return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
821ecd6cf80Smarks }
822ecd6cf80Smarks
823ecd6cf80Smarks /*
824cbf6f6aaSWilliam Gorrell * Destroying snapshots with delegated permissions requires
8254445fffbSMatthew Ahrens * descendant mount and destroy permissions.
826cbf6f6aaSWilliam Gorrell */
8274445fffbSMatthew Ahrens /* ARGSUSED */
828cbf6f6aaSWilliam Gorrell static int
zfs_secpolicy_destroy_snaps(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)8294445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
830cbf6f6aaSWilliam Gorrell {
8314445fffbSMatthew Ahrens nvlist_t *snaps;
8324445fffbSMatthew Ahrens nvpair_t *pair, *nextpair;
8334445fffbSMatthew Ahrens int error = 0;
834cbf6f6aaSWilliam Gorrell
8354445fffbSMatthew Ahrens if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
836be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
8374445fffbSMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8384445fffbSMatthew Ahrens pair = nextpair) {
8394445fffbSMatthew Ahrens nextpair = nvlist_next_nvpair(snaps, pair);
84078f17100SMatthew Ahrens error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
84178f17100SMatthew Ahrens if (error == ENOENT) {
8424445fffbSMatthew Ahrens /*
8434445fffbSMatthew Ahrens * Ignore any snapshots that don't exist (we consider
8444445fffbSMatthew Ahrens * them "already destroyed"). Remove the name from the
8454445fffbSMatthew Ahrens * nvl here in case the snapshot is created between
8464445fffbSMatthew Ahrens * now and when we try to destroy it (in which case
8474445fffbSMatthew Ahrens * we don't want to destroy it since we haven't
8484445fffbSMatthew Ahrens * checked for permission).
8494445fffbSMatthew Ahrens */
8504445fffbSMatthew Ahrens fnvlist_remove_nvpair(snaps, pair);
8514445fffbSMatthew Ahrens error = 0;
8524445fffbSMatthew Ahrens }
8534445fffbSMatthew Ahrens if (error != 0)
8544445fffbSMatthew Ahrens break;
8554445fffbSMatthew Ahrens }
856cbf6f6aaSWilliam Gorrell
857cbf6f6aaSWilliam Gorrell return (error);
858cbf6f6aaSWilliam Gorrell }
859cbf6f6aaSWilliam Gorrell
860ecd6cf80Smarks int
zfs_secpolicy_rename_perms(const char * from,const char * to,cred_t * cr)861ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
862ecd6cf80Smarks {
863*40a5c998SMatthew Ahrens char parentname[ZFS_MAX_DATASET_NAME_LEN];
864ecd6cf80Smarks int error;
865ecd6cf80Smarks
866ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(from,
867ecd6cf80Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0)
868ecd6cf80Smarks return (error);
869ecd6cf80Smarks
870ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(from,
871ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0)
872ecd6cf80Smarks return (error);
873ecd6cf80Smarks
874ecd6cf80Smarks if ((error = zfs_get_parent(to, parentname,
875ecd6cf80Smarks sizeof (parentname))) != 0)
876ecd6cf80Smarks return (error);
877ecd6cf80Smarks
878ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname,
879ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0)
880ecd6cf80Smarks return (error);
881ecd6cf80Smarks
882ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname,
883ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0)
884ecd6cf80Smarks return (error);
885ecd6cf80Smarks
886ecd6cf80Smarks return (error);
887ecd6cf80Smarks }
888ecd6cf80Smarks
8894445fffbSMatthew Ahrens /* ARGSUSED */
890ecd6cf80Smarks static int
zfs_secpolicy_rename(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)8914445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
892ecd6cf80Smarks {
893ecd6cf80Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
894ecd6cf80Smarks }
895ecd6cf80Smarks
8964445fffbSMatthew Ahrens /* ARGSUSED */
897ecd6cf80Smarks static int
zfs_secpolicy_promote(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)8984445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
899ecd6cf80Smarks {
9003b2aab18SMatthew Ahrens dsl_pool_t *dp;
9013b2aab18SMatthew Ahrens dsl_dataset_t *clone;
902ecd6cf80Smarks int error;
903ecd6cf80Smarks
904ecd6cf80Smarks error = zfs_secpolicy_write_perms(zc->zc_name,
905ecd6cf80Smarks ZFS_DELEG_PERM_PROMOTE, cr);
9063b2aab18SMatthew Ahrens if (error != 0)
907ecd6cf80Smarks return (error);
908ecd6cf80Smarks
9093b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9103b2aab18SMatthew Ahrens if (error != 0)
9113b2aab18SMatthew Ahrens return (error);
9123b2aab18SMatthew Ahrens
9133b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
914ecd6cf80Smarks
915ecd6cf80Smarks if (error == 0) {
916*40a5c998SMatthew Ahrens char parentname[ZFS_MAX_DATASET_NAME_LEN];
9173b2aab18SMatthew Ahrens dsl_dataset_t *origin = NULL;
918ecd6cf80Smarks dsl_dir_t *dd;
9193b2aab18SMatthew Ahrens dd = clone->ds_dir;
920ecd6cf80Smarks
921745cd3c5Smaybee error = dsl_dataset_hold_obj(dd->dd_pool,
922c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
9233b2aab18SMatthew Ahrens if (error != 0) {
9243b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG);
9253b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
926ecd6cf80Smarks return (error);
927ecd6cf80Smarks }
928ecd6cf80Smarks
9293b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
930ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr);
931ecd6cf80Smarks
9323b2aab18SMatthew Ahrens dsl_dataset_name(origin, parentname);
9333b2aab18SMatthew Ahrens if (error == 0) {
9343b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms_ds(parentname, origin,
935ecd6cf80Smarks ZFS_DELEG_PERM_PROMOTE, cr);
936ecd6cf80Smarks }
9373b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG);
9383b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG);
9393b2aab18SMatthew Ahrens }
9403b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
941ecd6cf80Smarks return (error);
942ecd6cf80Smarks }
943ecd6cf80Smarks
9444445fffbSMatthew Ahrens /* ARGSUSED */
945ecd6cf80Smarks static int
zfs_secpolicy_recv(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)9464445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
947ecd6cf80Smarks {
948ecd6cf80Smarks int error;
949ecd6cf80Smarks
950ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name,
951ecd6cf80Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
952ecd6cf80Smarks return (error);
953ecd6cf80Smarks
954ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name,
955ecd6cf80Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0)
956ecd6cf80Smarks return (error);
957ecd6cf80Smarks
958ecd6cf80Smarks return (zfs_secpolicy_write_perms(zc->zc_name,
959ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr));
960ecd6cf80Smarks }
961ecd6cf80Smarks
962ecd6cf80Smarks int
zfs_secpolicy_snapshot_perms(const char * name,cred_t * cr)963ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
964ecd6cf80Smarks {
965681d9761SEric Taylor return (zfs_secpolicy_write_perms(name,
966681d9761SEric Taylor ZFS_DELEG_PERM_SNAPSHOT, cr));
967ecd6cf80Smarks }
968ecd6cf80Smarks
9694445fffbSMatthew Ahrens /*
9704445fffbSMatthew Ahrens * Check for permission to create each snapshot in the nvlist.
9714445fffbSMatthew Ahrens */
9724445fffbSMatthew Ahrens /* ARGSUSED */
973ecd6cf80Smarks static int
zfs_secpolicy_snapshot(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)9744445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
975ecd6cf80Smarks {
9764445fffbSMatthew Ahrens nvlist_t *snaps;
977d5285caeSGeorge Wilson int error = 0;
9784445fffbSMatthew Ahrens nvpair_t *pair;
979ecd6cf80Smarks
9804445fffbSMatthew Ahrens if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
981be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
9824445fffbSMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
9834445fffbSMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) {
9844445fffbSMatthew Ahrens char *name = nvpair_name(pair);
9854445fffbSMatthew Ahrens char *atp = strchr(name, '@');
9864445fffbSMatthew Ahrens
9874445fffbSMatthew Ahrens if (atp == NULL) {
988be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
9894445fffbSMatthew Ahrens break;
9904445fffbSMatthew Ahrens }
9914445fffbSMatthew Ahrens *atp = '\0';
9924445fffbSMatthew Ahrens error = zfs_secpolicy_snapshot_perms(name, cr);
9934445fffbSMatthew Ahrens *atp = '@';
9944445fffbSMatthew Ahrens if (error != 0)
9954445fffbSMatthew Ahrens break;
9964445fffbSMatthew Ahrens }
9974445fffbSMatthew Ahrens return (error);
9984445fffbSMatthew Ahrens }
9994445fffbSMatthew Ahrens
100078f17100SMatthew Ahrens /*
100178f17100SMatthew Ahrens * Check for permission to create each snapshot in the nvlist.
100278f17100SMatthew Ahrens */
100378f17100SMatthew Ahrens /* ARGSUSED */
100478f17100SMatthew Ahrens static int
zfs_secpolicy_bookmark(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)100578f17100SMatthew Ahrens zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
100678f17100SMatthew Ahrens {
100778f17100SMatthew Ahrens int error = 0;
100878f17100SMatthew Ahrens
100978f17100SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
101078f17100SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
101178f17100SMatthew Ahrens char *name = nvpair_name(pair);
101278f17100SMatthew Ahrens char *hashp = strchr(name, '#');
101378f17100SMatthew Ahrens
101478f17100SMatthew Ahrens if (hashp == NULL) {
101578f17100SMatthew Ahrens error = SET_ERROR(EINVAL);
101678f17100SMatthew Ahrens break;
101778f17100SMatthew Ahrens }
101878f17100SMatthew Ahrens *hashp = '\0';
101978f17100SMatthew Ahrens error = zfs_secpolicy_write_perms(name,
102078f17100SMatthew Ahrens ZFS_DELEG_PERM_BOOKMARK, cr);
102178f17100SMatthew Ahrens *hashp = '#';
102278f17100SMatthew Ahrens if (error != 0)
102378f17100SMatthew Ahrens break;
102478f17100SMatthew Ahrens }
102578f17100SMatthew Ahrens return (error);
102678f17100SMatthew Ahrens }
102778f17100SMatthew Ahrens
102878f17100SMatthew Ahrens /* ARGSUSED */
102978f17100SMatthew Ahrens static int
zfs_secpolicy_destroy_bookmarks(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)103078f17100SMatthew Ahrens zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
103178f17100SMatthew Ahrens {
103278f17100SMatthew Ahrens nvpair_t *pair, *nextpair;
103378f17100SMatthew Ahrens int error = 0;
103478f17100SMatthew Ahrens
103578f17100SMatthew Ahrens for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
103678f17100SMatthew Ahrens pair = nextpair) {
103778f17100SMatthew Ahrens char *name = nvpair_name(pair);
103878f17100SMatthew Ahrens char *hashp = strchr(name, '#');
103978f17100SMatthew Ahrens nextpair = nvlist_next_nvpair(innvl, pair);
104078f17100SMatthew Ahrens
104178f17100SMatthew Ahrens if (hashp == NULL) {
104278f17100SMatthew Ahrens error = SET_ERROR(EINVAL);
104378f17100SMatthew Ahrens break;
104478f17100SMatthew Ahrens }
104578f17100SMatthew Ahrens
104678f17100SMatthew Ahrens *hashp = '\0';
104778f17100SMatthew Ahrens error = zfs_secpolicy_write_perms(name,
104878f17100SMatthew Ahrens ZFS_DELEG_PERM_DESTROY, cr);
104978f17100SMatthew Ahrens *hashp = '#';
105078f17100SMatthew Ahrens if (error == ENOENT) {
105178f17100SMatthew Ahrens /*
105278f17100SMatthew Ahrens * Ignore any filesystems that don't exist (we consider
105378f17100SMatthew Ahrens * their bookmarks "already destroyed"). Remove
105478f17100SMatthew Ahrens * the name from the nvl here in case the filesystem
105578f17100SMatthew Ahrens * is created between now and when we try to destroy
105678f17100SMatthew Ahrens * the bookmark (in which case we don't want to
105778f17100SMatthew Ahrens * destroy it since we haven't checked for permission).
105878f17100SMatthew Ahrens */
105978f17100SMatthew Ahrens fnvlist_remove_nvpair(innvl, pair);
106078f17100SMatthew Ahrens error = 0;
106178f17100SMatthew Ahrens }
106278f17100SMatthew Ahrens if (error != 0)
106378f17100SMatthew Ahrens break;
106478f17100SMatthew Ahrens }
106578f17100SMatthew Ahrens
106678f17100SMatthew Ahrens return (error);
106778f17100SMatthew Ahrens }
106878f17100SMatthew Ahrens
10694445fffbSMatthew Ahrens /* ARGSUSED */
10704445fffbSMatthew Ahrens static int
zfs_secpolicy_log_history(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)10714445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10724445fffbSMatthew Ahrens {
10734445fffbSMatthew Ahrens /*
10744445fffbSMatthew Ahrens * Even root must have a proper TSD so that we know what pool
10754445fffbSMatthew Ahrens * to log to.
10764445fffbSMatthew Ahrens */
10774445fffbSMatthew Ahrens if (tsd_get(zfs_allow_log_key) == NULL)
1078be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
10794445fffbSMatthew Ahrens return (0);
1080ecd6cf80Smarks }
1081ecd6cf80Smarks
1082ecd6cf80Smarks static int
zfs_secpolicy_create_clone(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)10834445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1084ecd6cf80Smarks {
1085*40a5c998SMatthew Ahrens char parentname[ZFS_MAX_DATASET_NAME_LEN];
1086ecd6cf80Smarks int error;
10874445fffbSMatthew Ahrens char *origin;
1088ecd6cf80Smarks
1089ecd6cf80Smarks if ((error = zfs_get_parent(zc->zc_name, parentname,
1090ecd6cf80Smarks sizeof (parentname))) != 0)
1091ecd6cf80Smarks return (error);
1092ecd6cf80Smarks
10934445fffbSMatthew Ahrens if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
10944445fffbSMatthew Ahrens (error = zfs_secpolicy_write_perms(origin,
1095ecd6cf80Smarks ZFS_DELEG_PERM_CLONE, cr)) != 0)
1096ecd6cf80Smarks return (error);
1097ecd6cf80Smarks
1098ecd6cf80Smarks if ((error = zfs_secpolicy_write_perms(parentname,
1099ecd6cf80Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0)
1100ecd6cf80Smarks return (error);
1101ecd6cf80Smarks
11024445fffbSMatthew Ahrens return (zfs_secpolicy_write_perms(parentname,
11034445fffbSMatthew Ahrens ZFS_DELEG_PERM_MOUNT, cr));
1104fa9e4066Sahrens }
1105fa9e4066Sahrens
1106fa9e4066Sahrens /*
1107fa9e4066Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1108fa9e4066Sahrens * SYS_CONFIG privilege, which is not available in a local zone.
1109fa9e4066Sahrens */
1110fa9e4066Sahrens /* ARGSUSED */
1111fa9e4066Sahrens static int
zfs_secpolicy_config(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11124445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1113fa9e4066Sahrens {
1114fa9e4066Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0)
1115be6fd75aSMatthew Ahrens return (SET_ERROR(EPERM));
1116fa9e4066Sahrens
1117fa9e4066Sahrens return (0);
1118fa9e4066Sahrens }
1119fa9e4066Sahrens
1120fa9e4066Sahrens /*
112199d5e173STim Haley * Policy for object to name lookups.
112299d5e173STim Haley */
112399d5e173STim Haley /* ARGSUSED */
112499d5e173STim Haley static int
zfs_secpolicy_diff(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11254445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
112699d5e173STim Haley {
112799d5e173STim Haley int error;
112899d5e173STim Haley
112999d5e173STim Haley if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
113099d5e173STim Haley return (0);
113199d5e173STim Haley
113299d5e173STim Haley error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
113399d5e173STim Haley return (error);
113499d5e173STim Haley }
113599d5e173STim Haley
113699d5e173STim Haley /*
1137ea8dc4b6Seschrock * Policy for fault injection. Requires all privileges.
1138ea8dc4b6Seschrock */
1139ea8dc4b6Seschrock /* ARGSUSED */
1140ea8dc4b6Seschrock static int
zfs_secpolicy_inject(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11414445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1142ea8dc4b6Seschrock {
1143ea8dc4b6Seschrock return (secpolicy_zinject(cr));
1144ea8dc4b6Seschrock }
1145ea8dc4b6Seschrock
11464445fffbSMatthew Ahrens /* ARGSUSED */
1147e45ce728Sahrens static int
zfs_secpolicy_inherit_prop(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11484445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1149e45ce728Sahrens {
1150e45ce728Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1151e45ce728Sahrens
1152990b4856Slling if (prop == ZPROP_INVAL) {
1153e45ce728Sahrens if (!zfs_prop_user(zc->zc_value))
1154be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1155e45ce728Sahrens return (zfs_secpolicy_write_perms(zc->zc_name,
1156e45ce728Sahrens ZFS_DELEG_PERM_USERPROP, cr));
1157e45ce728Sahrens } else {
115892241e0bSTom Erickson return (zfs_secpolicy_setprop(zc->zc_name, prop,
115992241e0bSTom Erickson NULL, cr));
1160e45ce728Sahrens }
1161e45ce728Sahrens }
1162e45ce728Sahrens
116314843421SMatthew Ahrens static int
zfs_secpolicy_userspace_one(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11644445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
116514843421SMatthew Ahrens {
11664445fffbSMatthew Ahrens int err = zfs_secpolicy_read(zc, innvl, cr);
116714843421SMatthew Ahrens if (err)
116814843421SMatthew Ahrens return (err);
116914843421SMatthew Ahrens
117014843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1171be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
117214843421SMatthew Ahrens
117314843421SMatthew Ahrens if (zc->zc_value[0] == 0) {
117414843421SMatthew Ahrens /*
117514843421SMatthew Ahrens * They are asking about a posix uid/gid. If it's
117614843421SMatthew Ahrens * themself, allow it.
117714843421SMatthew Ahrens */
117814843421SMatthew Ahrens if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
117914843421SMatthew Ahrens zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
118014843421SMatthew Ahrens if (zc->zc_guid == crgetuid(cr))
118114843421SMatthew Ahrens return (0);
118214843421SMatthew Ahrens } else {
118314843421SMatthew Ahrens if (groupmember(zc->zc_guid, cr))
118414843421SMatthew Ahrens return (0);
118514843421SMatthew Ahrens }
118614843421SMatthew Ahrens }
118714843421SMatthew Ahrens
118814843421SMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name,
118914843421SMatthew Ahrens userquota_perms[zc->zc_objset_type], cr));
119014843421SMatthew Ahrens }
119114843421SMatthew Ahrens
119214843421SMatthew Ahrens static int
zfs_secpolicy_userspace_many(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11934445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
119414843421SMatthew Ahrens {
11954445fffbSMatthew Ahrens int err = zfs_secpolicy_read(zc, innvl, cr);
119614843421SMatthew Ahrens if (err)
119714843421SMatthew Ahrens return (err);
119814843421SMatthew Ahrens
119914843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1200be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
120114843421SMatthew Ahrens
120214843421SMatthew Ahrens return (zfs_secpolicy_write_perms(zc->zc_name,
120314843421SMatthew Ahrens userquota_perms[zc->zc_objset_type], cr));
120414843421SMatthew Ahrens }
120514843421SMatthew Ahrens
12064445fffbSMatthew Ahrens /* ARGSUSED */
120714843421SMatthew Ahrens static int
zfs_secpolicy_userspace_upgrade(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)12084445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
120914843421SMatthew Ahrens {
121092241e0bSTom Erickson return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
121192241e0bSTom Erickson NULL, cr));
121214843421SMatthew Ahrens }
121314843421SMatthew Ahrens
12144445fffbSMatthew Ahrens /* ARGSUSED */
1215842727c2SChris Kirby static int
zfs_secpolicy_hold(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)12164445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1217842727c2SChris Kirby {
12183b2aab18SMatthew Ahrens nvpair_t *pair;
12193b2aab18SMatthew Ahrens nvlist_t *holds;
12203b2aab18SMatthew Ahrens int error;
12213b2aab18SMatthew Ahrens
12223b2aab18SMatthew Ahrens error = nvlist_lookup_nvlist(innvl, "holds", &holds);
12233b2aab18SMatthew Ahrens if (error != 0)
1224be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
12253b2aab18SMatthew Ahrens
12263b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
12273b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(holds, pair)) {
1228*40a5c998SMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN];
12293b2aab18SMatthew Ahrens error = dmu_fsname(nvpair_name(pair), fsname);
12303b2aab18SMatthew Ahrens if (error != 0)
12313b2aab18SMatthew Ahrens return (error);
12323b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms(fsname,
12333b2aab18SMatthew Ahrens ZFS_DELEG_PERM_HOLD, cr);
12343b2aab18SMatthew Ahrens if (error != 0)
12353b2aab18SMatthew Ahrens return (error);
12363b2aab18SMatthew Ahrens }
12373b2aab18SMatthew Ahrens return (0);
1238842727c2SChris Kirby }
1239842727c2SChris Kirby
12404445fffbSMatthew Ahrens /* ARGSUSED */
1241842727c2SChris Kirby static int
zfs_secpolicy_release(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)12424445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1243842727c2SChris Kirby {
12443b2aab18SMatthew Ahrens nvpair_t *pair;
12453b2aab18SMatthew Ahrens int error;
12463b2aab18SMatthew Ahrens
12473b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
12483b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(innvl, pair)) {
1249*40a5c998SMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN];
12503b2aab18SMatthew Ahrens error = dmu_fsname(nvpair_name(pair), fsname);
12513b2aab18SMatthew Ahrens if (error != 0)
12523b2aab18SMatthew Ahrens return (error);
12533b2aab18SMatthew Ahrens error = zfs_secpolicy_write_perms(fsname,
12543b2aab18SMatthew Ahrens ZFS_DELEG_PERM_RELEASE, cr);
12553b2aab18SMatthew Ahrens if (error != 0)
12563b2aab18SMatthew Ahrens return (error);
12573b2aab18SMatthew Ahrens }
12583b2aab18SMatthew Ahrens return (0);
1259842727c2SChris Kirby }
1260842727c2SChris Kirby
1261ea8dc4b6Seschrock /*
126299d5e173STim Haley * Policy for allowing temporary snapshots to be taken or released
126399d5e173STim Haley */
126499d5e173STim Haley static int
zfs_secpolicy_tmp_snapshot(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)12654445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
126699d5e173STim Haley {
126799d5e173STim Haley /*
126899d5e173STim Haley * A temporary snapshot is the same as a snapshot,
126999d5e173STim Haley * hold, destroy and release all rolled into one.
127099d5e173STim Haley * Delegated diff alone is sufficient that we allow this.
127199d5e173STim Haley */
127299d5e173STim Haley int error;
127399d5e173STim Haley
127499d5e173STim Haley if ((error = zfs_secpolicy_write_perms(zc->zc_name,
127599d5e173STim Haley ZFS_DELEG_PERM_DIFF, cr)) == 0)
127699d5e173STim Haley return (0);
127799d5e173STim Haley
12784445fffbSMatthew Ahrens error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
12793b2aab18SMatthew Ahrens if (error == 0)
12804445fffbSMatthew Ahrens error = zfs_secpolicy_hold(zc, innvl, cr);
12813b2aab18SMatthew Ahrens if (error == 0)
12824445fffbSMatthew Ahrens error = zfs_secpolicy_release(zc, innvl, cr);
12833b2aab18SMatthew Ahrens if (error == 0)
12844445fffbSMatthew Ahrens error = zfs_secpolicy_destroy(zc, innvl, cr);
128599d5e173STim Haley return (error);
128699d5e173STim Haley }
128799d5e173STim Haley
128899d5e173STim Haley /*
1289d78b796cSAndreas Jaekel * Policy for allowing setting the zev callback list.
1290d78b796cSAndreas Jaekel */
1291d78b796cSAndreas Jaekel static int
zfs_secpolicy_set_zev_callbacks(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1292d78b796cSAndreas Jaekel zfs_secpolicy_set_zev_callbacks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1293d78b796cSAndreas Jaekel {
1294d78b796cSAndreas Jaekel /* must be called from kernel context */
1295d78b796cSAndreas Jaekel if (!(zc->zc_iflags & FKIOCTL))
1296d78b796cSAndreas Jaekel return (SET_ERROR(EPERM));
1297d78b796cSAndreas Jaekel /* callback pointer must be unset (set to default value) */
1298d78b796cSAndreas Jaekel rw_enter(&rz_zev_rwlock, RW_READER);
1299d78b796cSAndreas Jaekel if (rz_zev_callbacks != rz_zev_default_callbacks) {
1300d78b796cSAndreas Jaekel rw_exit(&rz_zev_rwlock);
1301d78b796cSAndreas Jaekel return (SET_ERROR(EBUSY));
1302d78b796cSAndreas Jaekel }
1303d78b796cSAndreas Jaekel rw_exit(&rz_zev_rwlock);
1304d78b796cSAndreas Jaekel return (0);
1305d78b796cSAndreas Jaekel }
1306d78b796cSAndreas Jaekel
1307d78b796cSAndreas Jaekel /*
1308d78b796cSAndreas Jaekel * Policy for allowing unsetting/resetting the zev callback list.
1309d78b796cSAndreas Jaekel */
1310d78b796cSAndreas Jaekel static int
zfs_secpolicy_unset_zev_callbacks(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1311d78b796cSAndreas Jaekel zfs_secpolicy_unset_zev_callbacks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1312d78b796cSAndreas Jaekel {
1313d78b796cSAndreas Jaekel /* must be called from kernel context */
1314d78b796cSAndreas Jaekel if (!(zc->zc_iflags & FKIOCTL))
1315d78b796cSAndreas Jaekel return (SET_ERROR(EPERM));
1316d78b796cSAndreas Jaekel return (0);
1317d78b796cSAndreas Jaekel }
1318d78b796cSAndreas Jaekel
1319d78b796cSAndreas Jaekel /*
1320fa9e4066Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t.
1321fa9e4066Sahrens */
1322fa9e4066Sahrens static int
get_nvlist(uint64_t nvl,uint64_t size,int iflag,nvlist_t ** nvp)1323478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1324fa9e4066Sahrens {
1325fa9e4066Sahrens char *packed;
1326fa9e4066Sahrens int error;
1327990b4856Slling nvlist_t *list = NULL;
1328fa9e4066Sahrens
1329fa9e4066Sahrens /*
1330e9dbad6fSeschrock * Read in and unpack the user-supplied nvlist.
1331fa9e4066Sahrens */
1332990b4856Slling if (size == 0)
1333be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1334fa9e4066Sahrens
1335fa9e4066Sahrens packed = kmem_alloc(size, KM_SLEEP);
1336fa9e4066Sahrens
1337478ed9adSEric Taylor if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1338478ed9adSEric Taylor iflag)) != 0) {
1339fa9e4066Sahrens kmem_free(packed, size);
1340c71c00bbSRichard Yao return (SET_ERROR(EFAULT));
1341fa9e4066Sahrens }
1342fa9e4066Sahrens
1343990b4856Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1344fa9e4066Sahrens kmem_free(packed, size);
1345fa9e4066Sahrens return (error);
1346fa9e4066Sahrens }
1347fa9e4066Sahrens
1348fa9e4066Sahrens kmem_free(packed, size);
1349fa9e4066Sahrens
1350990b4856Slling *nvp = list;
1351fa9e4066Sahrens return (0);
1352fa9e4066Sahrens }
1353fa9e4066Sahrens
13544445fffbSMatthew Ahrens /*
13554445fffbSMatthew Ahrens * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
13564445fffbSMatthew Ahrens * Entries will be removed from the end of the nvlist, and one int32 entry
13574445fffbSMatthew Ahrens * named "N_MORE_ERRORS" will be added indicating how many entries were
13584445fffbSMatthew Ahrens * removed.
13594445fffbSMatthew Ahrens */
1360fa9e4066Sahrens static int
nvlist_smush(nvlist_t * errors,size_t max)13614445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
136292241e0bSTom Erickson {
136392241e0bSTom Erickson size_t size;
136492241e0bSTom Erickson
13654445fffbSMatthew Ahrens size = fnvlist_size(errors);
136692241e0bSTom Erickson
13674445fffbSMatthew Ahrens if (size > max) {
136892241e0bSTom Erickson nvpair_t *more_errors;
136992241e0bSTom Erickson int n = 0;
137092241e0bSTom Erickson
13714445fffbSMatthew Ahrens if (max < 1024)
1372be6fd75aSMatthew Ahrens return (SET_ERROR(ENOMEM));
137392241e0bSTom Erickson
13744445fffbSMatthew Ahrens fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
13754445fffbSMatthew Ahrens more_errors = nvlist_prev_nvpair(errors, NULL);
137692241e0bSTom Erickson
137792241e0bSTom Erickson do {
13784445fffbSMatthew Ahrens nvpair_t *pair = nvlist_prev_nvpair(errors,
137992241e0bSTom Erickson more_errors);
13804445fffbSMatthew Ahrens fnvlist_remove_nvpair(errors, pair);
138192241e0bSTom Erickson n++;
13824445fffbSMatthew Ahrens size = fnvlist_size(errors);
13834445fffbSMatthew Ahrens } while (size > max);
138492241e0bSTom Erickson
13854445fffbSMatthew Ahrens fnvlist_remove_nvpair(errors, more_errors);
13864445fffbSMatthew Ahrens fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
13874445fffbSMatthew Ahrens ASSERT3U(fnvlist_size(errors), <=, max);
138892241e0bSTom Erickson }
138992241e0bSTom Erickson
139092241e0bSTom Erickson return (0);
139192241e0bSTom Erickson }
139292241e0bSTom Erickson
139392241e0bSTom Erickson static int
put_nvlist(zfs_cmd_t * zc,nvlist_t * nvl)1394e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1395e9dbad6fSeschrock {
1396e9dbad6fSeschrock char *packed = NULL;
13976e27f868SSam Falkner int error = 0;
1398e9dbad6fSeschrock size_t size;
1399e9dbad6fSeschrock
14004445fffbSMatthew Ahrens size = fnvlist_size(nvl);
1401e9dbad6fSeschrock
1402e9dbad6fSeschrock if (size > zc->zc_nvlist_dst_size) {
1403be6fd75aSMatthew Ahrens error = SET_ERROR(ENOMEM);
1404e9dbad6fSeschrock } else {
14054445fffbSMatthew Ahrens packed = fnvlist_pack(nvl, &size);
14066e27f868SSam Falkner if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
14076e27f868SSam Falkner size, zc->zc_iflags) != 0)
1408be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT);
14094445fffbSMatthew Ahrens fnvlist_pack_free(packed, size);
1410e9dbad6fSeschrock }
1411e9dbad6fSeschrock
1412e9dbad6fSeschrock zc->zc_nvlist_dst_size = size;
14134445fffbSMatthew Ahrens zc->zc_nvlist_dst_filled = B_TRUE;
1414e9dbad6fSeschrock return (error);
1415e9dbad6fSeschrock }
1416e9dbad6fSeschrock
1417e9dbad6fSeschrock static int
getzfsvfs(const char * dsname,zfsvfs_t ** zfvp)1418af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
141914843421SMatthew Ahrens {
142014843421SMatthew Ahrens objset_t *os;
142114843421SMatthew Ahrens int error;
142214843421SMatthew Ahrens
1423503ad85cSMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os);
14243b2aab18SMatthew Ahrens if (error != 0)
142514843421SMatthew Ahrens return (error);
1426503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) {
1427503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
1428be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1429503ad85cSMatthew Ahrens }
143014843421SMatthew Ahrens
1431503ad85cSMatthew Ahrens mutex_enter(&os->os_user_ptr_lock);
1432af4c679fSSean McEnroe *zfvp = dmu_objset_get_user(os);
1433af4c679fSSean McEnroe if (*zfvp) {
1434af4c679fSSean McEnroe VFS_HOLD((*zfvp)->z_vfs);
143514843421SMatthew Ahrens } else {
1436be6fd75aSMatthew Ahrens error = SET_ERROR(ESRCH);
143714843421SMatthew Ahrens }
1438503ad85cSMatthew Ahrens mutex_exit(&os->os_user_ptr_lock);
1439503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
144014843421SMatthew Ahrens return (error);
144114843421SMatthew Ahrens }
144214843421SMatthew Ahrens
144314843421SMatthew Ahrens /*
144414843421SMatthew Ahrens * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
144514843421SMatthew Ahrens * case its z_vfs will be NULL, and it will be opened as the owner.
1446ad135b5dSChristopher Siden * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1447ad135b5dSChristopher Siden * which prevents all vnode ops from running.
144814843421SMatthew Ahrens */
144914843421SMatthew Ahrens static int
zfsvfs_hold(const char * name,void * tag,zfsvfs_t ** zfvp,boolean_t writer)14501412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
145114843421SMatthew Ahrens {
145214843421SMatthew Ahrens int error = 0;
145314843421SMatthew Ahrens
1454af4c679fSSean McEnroe if (getzfsvfs(name, zfvp) != 0)
1455af4c679fSSean McEnroe error = zfsvfs_create(name, zfvp);
145614843421SMatthew Ahrens if (error == 0) {
1457c9030f6cSAlexander Motin rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
14581412a1a2SMark Shellenbaum RW_READER, tag);
1459af4c679fSSean McEnroe if ((*zfvp)->z_unmounted) {
146014843421SMatthew Ahrens /*
146114843421SMatthew Ahrens * XXX we could probably try again, since the unmounting
146214843421SMatthew Ahrens * thread should be just about to disassociate the
146314843421SMatthew Ahrens * objset from the zfsvfs.
146414843421SMatthew Ahrens */
1465c9030f6cSAlexander Motin rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1466be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY));
146714843421SMatthew Ahrens }
146814843421SMatthew Ahrens }
146914843421SMatthew Ahrens return (error);
147014843421SMatthew Ahrens }
147114843421SMatthew Ahrens
147214843421SMatthew Ahrens static void
zfsvfs_rele(zfsvfs_t * zfsvfs,void * tag)147314843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
147414843421SMatthew Ahrens {
1475c9030f6cSAlexander Motin rrm_exit(&zfsvfs->z_teardown_lock, tag);
147614843421SMatthew Ahrens
147714843421SMatthew Ahrens if (zfsvfs->z_vfs) {
147814843421SMatthew Ahrens VFS_RELE(zfsvfs->z_vfs);
147914843421SMatthew Ahrens } else {
1480503ad85cSMatthew Ahrens dmu_objset_disown(zfsvfs->z_os, zfsvfs);
148114843421SMatthew Ahrens zfsvfs_free(zfsvfs);
148214843421SMatthew Ahrens }
148314843421SMatthew Ahrens }
148414843421SMatthew Ahrens
148514843421SMatthew Ahrens static int
zfs_ioc_pool_create(zfs_cmd_t * zc)1486fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1487fa9e4066Sahrens {
1488fa9e4066Sahrens int error;
1489990b4856Slling nvlist_t *config, *props = NULL;
14900a48a24eStimh nvlist_t *rootprops = NULL;
14910a48a24eStimh nvlist_t *zplprops = NULL;
1492fa9e4066Sahrens
1493990b4856Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1494478ed9adSEric Taylor zc->zc_iflags, &config))
1495fa9e4066Sahrens return (error);
14962a6b87f0Sek110237
1497990b4856Slling if (zc->zc_nvlist_src_size != 0 && (error =
1498478ed9adSEric Taylor get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1499478ed9adSEric Taylor zc->zc_iflags, &props))) {
1500990b4856Slling nvlist_free(config);
1501990b4856Slling return (error);
1502990b4856Slling }
1503990b4856Slling
15040a48a24eStimh if (props) {
15050a48a24eStimh nvlist_t *nvl = NULL;
15060a48a24eStimh uint64_t version = SPA_VERSION;
15070a48a24eStimh
15080a48a24eStimh (void) nvlist_lookup_uint64(props,
15090a48a24eStimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1510ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(version)) {
1511be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
15120a48a24eStimh goto pool_props_bad;
15130a48a24eStimh }
15140a48a24eStimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
15150a48a24eStimh if (nvl) {
15160a48a24eStimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
15170a48a24eStimh if (error != 0) {
15180a48a24eStimh nvlist_free(config);
15190a48a24eStimh nvlist_free(props);
15200a48a24eStimh return (error);
15210a48a24eStimh }
15220a48a24eStimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
15230a48a24eStimh }
15240a48a24eStimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
15250a48a24eStimh error = zfs_fill_zplprops_root(version, rootprops,
15260a48a24eStimh zplprops, NULL);
15273b2aab18SMatthew Ahrens if (error != 0)
15280a48a24eStimh goto pool_props_bad;
15290a48a24eStimh }
15300a48a24eStimh
15314445fffbSMatthew Ahrens error = spa_create(zc->zc_name, config, props, zplprops);
15320a48a24eStimh
15330a48a24eStimh /*
15340a48a24eStimh * Set the remaining root properties
15350a48a24eStimh */
153692241e0bSTom Erickson if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
153792241e0bSTom Erickson ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
15380a48a24eStimh (void) spa_destroy(zc->zc_name);
1539fa9e4066Sahrens
15400a48a24eStimh pool_props_bad:
15410a48a24eStimh nvlist_free(rootprops);
15420a48a24eStimh nvlist_free(zplprops);
15432a6b87f0Sek110237 nvlist_free(config);
1544990b4856Slling nvlist_free(props);
1545990b4856Slling
1546fa9e4066Sahrens return (error);
1547fa9e4066Sahrens }
1548fa9e4066Sahrens
1549fa9e4066Sahrens static int
zfs_ioc_pool_destroy(zfs_cmd_t * zc)1550fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1551fa9e4066Sahrens {
1552ecd6cf80Smarks int error;
1553ecd6cf80Smarks zfs_log_history(zc);
1554ecd6cf80Smarks error = spa_destroy(zc->zc_name);
1555681d9761SEric Taylor if (error == 0)
1556681d9761SEric Taylor zvol_remove_minors(zc->zc_name);
1557ecd6cf80Smarks return (error);
1558fa9e4066Sahrens }
1559fa9e4066Sahrens
1560fa9e4066Sahrens static int
zfs_ioc_pool_import(zfs_cmd_t * zc)1561fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1562fa9e4066Sahrens {
1563990b4856Slling nvlist_t *config, *props = NULL;
1564fa9e4066Sahrens uint64_t guid;
1565468c413aSTim Haley int error;
1566fa9e4066Sahrens
1567990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1568478ed9adSEric Taylor zc->zc_iflags, &config)) != 0)
1569fa9e4066Sahrens return (error);
1570fa9e4066Sahrens
1571990b4856Slling if (zc->zc_nvlist_src_size != 0 && (error =
1572478ed9adSEric Taylor get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1573478ed9adSEric Taylor zc->zc_iflags, &props))) {
1574990b4856Slling nvlist_free(config);
1575990b4856Slling return (error);
1576990b4856Slling }
1577990b4856Slling
1578fa9e4066Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1579ea8dc4b6Seschrock guid != zc->zc_guid)
1580be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
1581fa9e4066Sahrens else
15824b964adaSGeorge Wilson error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1583fa9e4066Sahrens
15844b964adaSGeorge Wilson if (zc->zc_nvlist_dst != 0) {
15854b964adaSGeorge Wilson int err;
15864b964adaSGeorge Wilson
15874b964adaSGeorge Wilson if ((err = put_nvlist(zc, config)) != 0)
15884b964adaSGeorge Wilson error = err;
15894b964adaSGeorge Wilson }
1590468c413aSTim Haley
1591fa9e4066Sahrens nvlist_free(config);
1592fa9e4066Sahrens
1593990b4856Slling nvlist_free(props);
1594990b4856Slling
1595fa9e4066Sahrens return (error);
1596fa9e4066Sahrens }
1597fa9e4066Sahrens
1598fa9e4066Sahrens static int
zfs_ioc_pool_export(zfs_cmd_t * zc)1599fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1600fa9e4066Sahrens {
1601ecd6cf80Smarks int error;
160289a89ebfSlling boolean_t force = (boolean_t)zc->zc_cookie;
1603394ab0cbSGeorge Wilson boolean_t hardforce = (boolean_t)zc->zc_guid;
160489a89ebfSlling
1605ecd6cf80Smarks zfs_log_history(zc);
1606394ab0cbSGeorge Wilson error = spa_export(zc->zc_name, NULL, force, hardforce);
1607681d9761SEric Taylor if (error == 0)
1608681d9761SEric Taylor zvol_remove_minors(zc->zc_name);
1609ecd6cf80Smarks return (error);
1610fa9e4066Sahrens }
1611fa9e4066Sahrens
1612fa9e4066Sahrens static int
zfs_ioc_pool_configs(zfs_cmd_t * zc)1613fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1614fa9e4066Sahrens {
1615fa9e4066Sahrens nvlist_t *configs;
1616fa9e4066Sahrens int error;
1617fa9e4066Sahrens
1618fa9e4066Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1619be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST));
1620fa9e4066Sahrens
1621e9dbad6fSeschrock error = put_nvlist(zc, configs);
1622fa9e4066Sahrens
1623fa9e4066Sahrens nvlist_free(configs);
1624fa9e4066Sahrens
1625fa9e4066Sahrens return (error);
1626fa9e4066Sahrens }
1627fa9e4066Sahrens
1628ad135b5dSChristopher Siden /*
1629ad135b5dSChristopher Siden * inputs:
1630ad135b5dSChristopher Siden * zc_name name of the pool
1631ad135b5dSChristopher Siden *
1632ad135b5dSChristopher Siden * outputs:
1633ad135b5dSChristopher Siden * zc_cookie real errno
1634ad135b5dSChristopher Siden * zc_nvlist_dst config nvlist
1635ad135b5dSChristopher Siden * zc_nvlist_dst_size size of config nvlist
1636ad135b5dSChristopher Siden */
1637fa9e4066Sahrens static int
zfs_ioc_pool_stats(zfs_cmd_t * zc)1638fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1639fa9e4066Sahrens {
1640fa9e4066Sahrens nvlist_t *config;
1641fa9e4066Sahrens int error;
1642ea8dc4b6Seschrock int ret = 0;
1643fa9e4066Sahrens
1644e9dbad6fSeschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1645e9dbad6fSeschrock sizeof (zc->zc_value));
1646fa9e4066Sahrens
1647fa9e4066Sahrens if (config != NULL) {
1648e9dbad6fSeschrock ret = put_nvlist(zc, config);
1649fa9e4066Sahrens nvlist_free(config);
1650ea8dc4b6Seschrock
1651ea8dc4b6Seschrock /*
1652ea8dc4b6Seschrock * The config may be present even if 'error' is non-zero.
1653ea8dc4b6Seschrock * In this case we return success, and preserve the real errno
1654ea8dc4b6Seschrock * in 'zc_cookie'.
1655ea8dc4b6Seschrock */
1656ea8dc4b6Seschrock zc->zc_cookie = error;
1657fa9e4066Sahrens } else {
1658ea8dc4b6Seschrock ret = error;
1659fa9e4066Sahrens }
1660fa9e4066Sahrens
1661ea8dc4b6Seschrock return (ret);
1662fa9e4066Sahrens }
1663fa9e4066Sahrens
1664fa9e4066Sahrens /*
1665fa9e4066Sahrens * Try to import the given pool, returning pool stats as appropriate so that
1666fa9e4066Sahrens * user land knows which devices are available and overall pool health.
1667fa9e4066Sahrens */
1668fa9e4066Sahrens static int
zfs_ioc_pool_tryimport(zfs_cmd_t * zc)1669fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1670fa9e4066Sahrens {
1671fa9e4066Sahrens nvlist_t *tryconfig, *config;
1672fa9e4066Sahrens int error;
1673fa9e4066Sahrens
1674990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1675478ed9adSEric Taylor zc->zc_iflags, &tryconfig)) != 0)
1676fa9e4066Sahrens return (error);
1677fa9e4066Sahrens
1678fa9e4066Sahrens config = spa_tryimport(tryconfig);
1679fa9e4066Sahrens
1680fa9e4066Sahrens nvlist_free(tryconfig);
1681fa9e4066Sahrens
1682fa9e4066Sahrens if (config == NULL)
1683be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1684fa9e4066Sahrens
1685e9dbad6fSeschrock error = put_nvlist(zc, config);
1686fa9e4066Sahrens nvlist_free(config);
1687fa9e4066Sahrens
1688fa9e4066Sahrens return (error);
1689fa9e4066Sahrens }
1690fa9e4066Sahrens
16913f9d6ad7SLin Ling /*
16923f9d6ad7SLin Ling * inputs:
16933f9d6ad7SLin Ling * zc_name name of the pool
16943f9d6ad7SLin Ling * zc_cookie scan func (pool_scan_func_t)
16953f9d6ad7SLin Ling */
1696fa9e4066Sahrens static int
zfs_ioc_pool_scan(zfs_cmd_t * zc)16973f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1698fa9e4066Sahrens {
1699fa9e4066Sahrens spa_t *spa;
1700fa9e4066Sahrens int error;
1701fa9e4066Sahrens
170206eeb2adSek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
170306eeb2adSek110237 return (error);
170406eeb2adSek110237
17053f9d6ad7SLin Ling if (zc->zc_cookie == POOL_SCAN_NONE)
17063f9d6ad7SLin Ling error = spa_scan_stop(spa);
17073f9d6ad7SLin Ling else
17083f9d6ad7SLin Ling error = spa_scan(spa, zc->zc_cookie);
170906eeb2adSek110237
1710fa9e4066Sahrens spa_close(spa, FTAG);
171106eeb2adSek110237
1712fa9e4066Sahrens return (error);
1713fa9e4066Sahrens }
1714fa9e4066Sahrens
1715fa9e4066Sahrens static int
zfs_ioc_pool_freeze(zfs_cmd_t * zc)1716fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1717fa9e4066Sahrens {
1718fa9e4066Sahrens spa_t *spa;
1719fa9e4066Sahrens int error;
1720fa9e4066Sahrens
1721fa9e4066Sahrens error = spa_open(zc->zc_name, &spa, FTAG);
1722fa9e4066Sahrens if (error == 0) {
1723fa9e4066Sahrens spa_freeze(spa);
1724fa9e4066Sahrens spa_close(spa, FTAG);
1725fa9e4066Sahrens }
1726fa9e4066Sahrens return (error);
1727fa9e4066Sahrens }
1728fa9e4066Sahrens
1729fa9e4066Sahrens static int
zfs_ioc_arc_info(zfs_cmd_t * zc)1730ce0d9371SArne Jansen zfs_ioc_arc_info(zfs_cmd_t *zc)
1731ce0d9371SArne Jansen {
1732ce0d9371SArne Jansen int ret;
1733ce0d9371SArne Jansen void *buf;
1734ce0d9371SArne Jansen size_t sz = zc->zc_nvlist_dst_size;
1735ce0d9371SArne Jansen size_t returned_bytes;
1736ce0d9371SArne Jansen
1737ce0d9371SArne Jansen if (zc->zc_nvlist_dst == 0)
1738ce0d9371SArne Jansen return (SET_ERROR(EINVAL));
1739ce0d9371SArne Jansen
1740ce0d9371SArne Jansen buf = kmem_alloc(sz, KM_NOSLEEP);
1741ce0d9371SArne Jansen if (buf == NULL)
1742ce0d9371SArne Jansen return (SET_ERROR(ENOMEM));
1743ce0d9371SArne Jansen
1744ce0d9371SArne Jansen ret = arc_dump(zc->zc_obj, buf, sz, &returned_bytes);
1745ce0d9371SArne Jansen if (ret != 0) {
1746ce0d9371SArne Jansen kmem_free(buf, sz);
1747ce0d9371SArne Jansen return (SET_ERROR(ret));
1748ce0d9371SArne Jansen }
1749ce0d9371SArne Jansen
1750ce0d9371SArne Jansen zc->zc_nvlist_dst_filled = 1;
1751ce0d9371SArne Jansen ret = ddi_copyout(buf, (void *)(uintptr_t)zc->zc_nvlist_dst,
1752ce0d9371SArne Jansen returned_bytes, zc->zc_iflags);
1753ce0d9371SArne Jansen kmem_free(buf, sz);
1754ce0d9371SArne Jansen if (ret != 0)
1755ce0d9371SArne Jansen ret = SET_ERROR(EFAULT);
1756ce0d9371SArne Jansen
1757ce0d9371SArne Jansen return (ret);
1758ce0d9371SArne Jansen }
1759ce0d9371SArne Jansen
1760ce0d9371SArne Jansen static int
zfs_ioc_pool_upgrade(zfs_cmd_t * zc)1761eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1762eaca9bbdSeschrock {
1763eaca9bbdSeschrock spa_t *spa;
1764eaca9bbdSeschrock int error;
1765eaca9bbdSeschrock
176606eeb2adSek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
176706eeb2adSek110237 return (error);
176806eeb2adSek110237
1769ad135b5dSChristopher Siden if (zc->zc_cookie < spa_version(spa) ||
1770ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1771558d2d50Slling spa_close(spa, FTAG);
1772be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1773558d2d50Slling }
1774558d2d50Slling
1775990b4856Slling spa_upgrade(spa, zc->zc_cookie);
1776eaca9bbdSeschrock spa_close(spa, FTAG);
177706eeb2adSek110237
177806eeb2adSek110237 return (error);
1779eaca9bbdSeschrock }
178006eeb2adSek110237
178106eeb2adSek110237 static int
zfs_ioc_pool_get_history(zfs_cmd_t * zc)178206eeb2adSek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
178306eeb2adSek110237 {
178406eeb2adSek110237 spa_t *spa;
178506eeb2adSek110237 char *hist_buf;
178606eeb2adSek110237 uint64_t size;
178706eeb2adSek110237 int error;
178806eeb2adSek110237
178906eeb2adSek110237 if ((size = zc->zc_history_len) == 0)
1790be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
179106eeb2adSek110237
179206eeb2adSek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
179306eeb2adSek110237 return (error);
179406eeb2adSek110237
1795e7437265Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1796d7306b64Sek110237 spa_close(spa, FTAG);
1797be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
1798d7306b64Sek110237 }
1799d7306b64Sek110237
180006eeb2adSek110237 hist_buf = kmem_alloc(size, KM_SLEEP);
180106eeb2adSek110237 if ((error = spa_history_get(spa, &zc->zc_history_offset,
180206eeb2adSek110237 &zc->zc_history_len, hist_buf)) == 0) {
1803478ed9adSEric Taylor error = ddi_copyout(hist_buf,
1804478ed9adSEric Taylor (void *)(uintptr_t)zc->zc_history,
1805478ed9adSEric Taylor zc->zc_history_len, zc->zc_iflags);
180606eeb2adSek110237 }
180706eeb2adSek110237
180806eeb2adSek110237 spa_close(spa, FTAG);
180906eeb2adSek110237 kmem_free(hist_buf, size);
181006eeb2adSek110237 return (error);
181106eeb2adSek110237 }
181206eeb2adSek110237
181306eeb2adSek110237 static int
zfs_ioc_pool_reguid(zfs_cmd_t * zc)1814e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1815e9103aaeSGarrett D'Amore {
1816e9103aaeSGarrett D'Amore spa_t *spa;
1817e9103aaeSGarrett D'Amore int error;
1818e9103aaeSGarrett D'Amore
1819e9103aaeSGarrett D'Amore error = spa_open(zc->zc_name, &spa, FTAG);
1820e9103aaeSGarrett D'Amore if (error == 0) {
1821e9103aaeSGarrett D'Amore error = spa_change_guid(spa);
1822e9103aaeSGarrett D'Amore spa_close(spa, FTAG);
1823e9103aaeSGarrett D'Amore }
1824e9103aaeSGarrett D'Amore return (error);
1825e9103aaeSGarrett D'Amore }
1826e9103aaeSGarrett D'Amore
1827e9103aaeSGarrett D'Amore static int
zfs_ioc_dsobj_to_dsname(zfs_cmd_t * zc)182855434c77Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
182955434c77Sek110237 {
18303b2aab18SMatthew Ahrens return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
183155434c77Sek110237 }
183255434c77Sek110237
1833503ad85cSMatthew Ahrens /*
1834503ad85cSMatthew Ahrens * inputs:
1835503ad85cSMatthew Ahrens * zc_name name of filesystem
1836503ad85cSMatthew Ahrens * zc_obj object to find
1837503ad85cSMatthew Ahrens *
1838503ad85cSMatthew Ahrens * outputs:
1839503ad85cSMatthew Ahrens * zc_value name of object
1840503ad85cSMatthew Ahrens */
184155434c77Sek110237 static int
zfs_ioc_obj_to_path(zfs_cmd_t * zc)184255434c77Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
184355434c77Sek110237 {
1844503ad85cSMatthew Ahrens objset_t *os;
184555434c77Sek110237 int error;
184655434c77Sek110237
1847503ad85cSMatthew Ahrens /* XXX reading from objset not owned */
1848503ad85cSMatthew Ahrens if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
184955434c77Sek110237 return (error);
1850503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) {
1851503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
1852be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1853503ad85cSMatthew Ahrens }
1854503ad85cSMatthew Ahrens error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
185555434c77Sek110237 sizeof (zc->zc_value));
1856503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
185755434c77Sek110237
185855434c77Sek110237 return (error);
185955434c77Sek110237 }
186055434c77Sek110237
186199d5e173STim Haley /*
186299d5e173STim Haley * inputs:
186399d5e173STim Haley * zc_name name of filesystem
186499d5e173STim Haley * zc_obj object to find
186599d5e173STim Haley *
186699d5e173STim Haley * outputs:
186799d5e173STim Haley * zc_stat stats on object
186899d5e173STim Haley * zc_value path to object
186999d5e173STim Haley */
187099d5e173STim Haley static int
zfs_ioc_obj_to_stats(zfs_cmd_t * zc)187199d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
187299d5e173STim Haley {
187399d5e173STim Haley objset_t *os;
187499d5e173STim Haley int error;
187599d5e173STim Haley
187699d5e173STim Haley /* XXX reading from objset not owned */
187799d5e173STim Haley if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
187899d5e173STim Haley return (error);
187999d5e173STim Haley if (dmu_objset_type(os) != DMU_OST_ZFS) {
188099d5e173STim Haley dmu_objset_rele(os, FTAG);
1881be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
188299d5e173STim Haley }
188399d5e173STim Haley error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
188499d5e173STim Haley sizeof (zc->zc_value));
188599d5e173STim Haley dmu_objset_rele(os, FTAG);
188699d5e173STim Haley
188799d5e173STim Haley return (error);
188899d5e173STim Haley }
188999d5e173STim Haley
189055434c77Sek110237 static int
zfs_ioc_vdev_add(zfs_cmd_t * zc)1891fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1892fa9e4066Sahrens {
1893fa9e4066Sahrens spa_t *spa;
1894fa9e4066Sahrens int error;
1895e7cbe64fSgw25295 nvlist_t *config, **l2cache, **spares;
1896e7cbe64fSgw25295 uint_t nl2cache = 0, nspares = 0;
1897fa9e4066Sahrens
1898fa9e4066Sahrens error = spa_open(zc->zc_name, &spa, FTAG);
1899fa9e4066Sahrens if (error != 0)
1900fa9e4066Sahrens return (error);
1901fa9e4066Sahrens
1902fa94a07fSbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1903478ed9adSEric Taylor zc->zc_iflags, &config);
1904fa94a07fSbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1905fa94a07fSbrendan &l2cache, &nl2cache);
1906fa94a07fSbrendan
1907e7cbe64fSgw25295 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1908e7cbe64fSgw25295 &spares, &nspares);
1909e7cbe64fSgw25295
1910b1b8ab34Slling /*
1911b1b8ab34Slling * A root pool with concatenated devices is not supported.
1912e7cbe64fSgw25295 * Thus, can not add a device to a root pool.
1913e7cbe64fSgw25295 *
1914e7cbe64fSgw25295 * Intent log device can not be added to a rootpool because
1915e7cbe64fSgw25295 * during mountroot, zil is replayed, a seperated log device
1916e7cbe64fSgw25295 * can not be accessed during the mountroot time.
1917e7cbe64fSgw25295 *
1918e7cbe64fSgw25295 * l2cache and spare devices are ok to be added to a rootpool.
1919b1b8ab34Slling */
1920b24ab676SJeff Bonwick if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
19211195e687SMark J Musante nvlist_free(config);
1922b1b8ab34Slling spa_close(spa, FTAG);
1923be6fd75aSMatthew Ahrens return (SET_ERROR(EDOM));
1924b1b8ab34Slling }
1925b1b8ab34Slling
1926fa94a07fSbrendan if (error == 0) {
1927fa9e4066Sahrens error = spa_vdev_add(spa, config);
1928fa9e4066Sahrens nvlist_free(config);
1929fa9e4066Sahrens }
1930fa9e4066Sahrens spa_close(spa, FTAG);
1931fa9e4066Sahrens return (error);
1932fa9e4066Sahrens }
1933fa9e4066Sahrens
19343f9d6ad7SLin Ling /*
19353f9d6ad7SLin Ling * inputs:
19363f9d6ad7SLin Ling * zc_name name of the pool
19373f9d6ad7SLin Ling * zc_nvlist_conf nvlist of devices to remove
19383f9d6ad7SLin Ling * zc_cookie to stop the remove?
19393f9d6ad7SLin Ling */
1940fa9e4066Sahrens static int
zfs_ioc_vdev_remove(zfs_cmd_t * zc)1941fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1942fa9e4066Sahrens {
194399653d4eSeschrock spa_t *spa;
194499653d4eSeschrock int error;
194599653d4eSeschrock
194699653d4eSeschrock error = spa_open(zc->zc_name, &spa, FTAG);
194799653d4eSeschrock if (error != 0)
194899653d4eSeschrock return (error);
194999653d4eSeschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
195099653d4eSeschrock spa_close(spa, FTAG);
195199653d4eSeschrock return (error);
1952fa9e4066Sahrens }
1953fa9e4066Sahrens
1954fa9e4066Sahrens static int
zfs_ioc_vdev_set_state(zfs_cmd_t * zc)19553d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1956fa9e4066Sahrens {
1957fa9e4066Sahrens spa_t *spa;
1958fa9e4066Sahrens int error;
19593d7072f8Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1960fa9e4066Sahrens
196106eeb2adSek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1962fa9e4066Sahrens return (error);
19633d7072f8Seschrock switch (zc->zc_cookie) {
19643d7072f8Seschrock case VDEV_STATE_ONLINE:
19653d7072f8Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
19663d7072f8Seschrock break;
19673d7072f8Seschrock
19683d7072f8Seschrock case VDEV_STATE_OFFLINE:
19693d7072f8Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
19703d7072f8Seschrock break;
19713d7072f8Seschrock
19723d7072f8Seschrock case VDEV_STATE_FAULTED:
1973069f55e2SEric Schrock if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1974069f55e2SEric Schrock zc->zc_obj != VDEV_AUX_EXTERNAL)
1975069f55e2SEric Schrock zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1976069f55e2SEric Schrock
1977069f55e2SEric Schrock error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
19783d7072f8Seschrock break;
19793d7072f8Seschrock
19803d7072f8Seschrock case VDEV_STATE_DEGRADED:
1981069f55e2SEric Schrock if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1982069f55e2SEric Schrock zc->zc_obj != VDEV_AUX_EXTERNAL)
1983069f55e2SEric Schrock zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1984069f55e2SEric Schrock
1985069f55e2SEric Schrock error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
19863d7072f8Seschrock break;
19873d7072f8Seschrock
19883d7072f8Seschrock default:
1989be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
1990fa9e4066Sahrens }
19913d7072f8Seschrock zc->zc_cookie = newstate;
1992fa9e4066Sahrens spa_close(spa, FTAG);
1993fa9e4066Sahrens return (error);
1994fa9e4066Sahrens }
1995fa9e4066Sahrens
1996fa9e4066Sahrens static int
zfs_ioc_vdev_attach(zfs_cmd_t * zc)1997fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1998fa9e4066Sahrens {
1999fa9e4066Sahrens spa_t *spa;
2000fa9e4066Sahrens int replacing = zc->zc_cookie;
2001fa9e4066Sahrens nvlist_t *config;
2002fa9e4066Sahrens int error;
2003fa9e4066Sahrens
200406eeb2adSek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2005fa9e4066Sahrens return (error);
2006fa9e4066Sahrens
2007990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2008478ed9adSEric Taylor zc->zc_iflags, &config)) == 0) {
2009ea8dc4b6Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2010fa9e4066Sahrens nvlist_free(config);
2011fa9e4066Sahrens }
2012fa9e4066Sahrens
2013fa9e4066Sahrens spa_close(spa, FTAG);
2014fa9e4066Sahrens return (error);
2015fa9e4066Sahrens }
2016fa9e4066Sahrens
2017fa9e4066Sahrens static int
zfs_ioc_vdev_detach(zfs_cmd_t * zc)2018fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2019fa9e4066Sahrens {
2020fa9e4066Sahrens spa_t *spa;
2021fa9e4066Sahrens int error;
2022fa9e4066Sahrens
202306eeb2adSek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2024fa9e4066Sahrens return (error);
2025fa9e4066Sahrens
20268ad4d6ddSJeff Bonwick error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2027fa9e4066Sahrens
2028fa9e4066Sahrens spa_close(spa, FTAG);
2029fa9e4066Sahrens return (error);
2030fa9e4066Sahrens }
2031fa9e4066Sahrens
2032fa9e4066Sahrens static int
zfs_ioc_vdev_split(zfs_cmd_t * zc)20331195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
20341195e687SMark J Musante {
20351195e687SMark J Musante spa_t *spa;
20361195e687SMark J Musante nvlist_t *config, *props = NULL;
20371195e687SMark J Musante int error;
20381195e687SMark J Musante boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
20391195e687SMark J Musante
20401195e687SMark J Musante if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
20411195e687SMark J Musante return (error);
20421195e687SMark J Musante
20431195e687SMark J Musante if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
20441195e687SMark J Musante zc->zc_iflags, &config)) {
20451195e687SMark J Musante spa_close(spa, FTAG);
20461195e687SMark J Musante return (error);
20471195e687SMark J Musante }
20481195e687SMark J Musante
20491195e687SMark J Musante if (zc->zc_nvlist_src_size != 0 && (error =
20501195e687SMark J Musante get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20511195e687SMark J Musante zc->zc_iflags, &props))) {
20521195e687SMark J Musante spa_close(spa, FTAG);
20531195e687SMark J Musante nvlist_free(config);
20541195e687SMark J Musante return (error);
20551195e687SMark J Musante }
20561195e687SMark J Musante
20571195e687SMark J Musante error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
20581195e687SMark J Musante
20591195e687SMark J Musante spa_close(spa, FTAG);
20601195e687SMark J Musante
20611195e687SMark J Musante nvlist_free(config);
20621195e687SMark J Musante nvlist_free(props);
20631195e687SMark J Musante
20641195e687SMark J Musante return (error);
20651195e687SMark J Musante }
20661195e687SMark J Musante
20671195e687SMark J Musante static int
zfs_ioc_vdev_setpath(zfs_cmd_t * zc)2068c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2069c67d9675Seschrock {
2070c67d9675Seschrock spa_t *spa;
2071e9dbad6fSeschrock char *path = zc->zc_value;
2072ea8dc4b6Seschrock uint64_t guid = zc->zc_guid;
2073c67d9675Seschrock int error;
2074c67d9675Seschrock
2075c67d9675Seschrock error = spa_open(zc->zc_name, &spa, FTAG);
2076c67d9675Seschrock if (error != 0)
2077c67d9675Seschrock return (error);
2078c67d9675Seschrock
2079c67d9675Seschrock error = spa_vdev_setpath(spa, guid, path);
2080c67d9675Seschrock spa_close(spa, FTAG);
2081c67d9675Seschrock return (error);
2082c67d9675Seschrock }
2083c67d9675Seschrock
20846809eb4eSEric Schrock static int
zfs_ioc_vdev_setfru(zfs_cmd_t * zc)20856809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
20866809eb4eSEric Schrock {
20876809eb4eSEric Schrock spa_t *spa;
20886809eb4eSEric Schrock char *fru = zc->zc_value;
20896809eb4eSEric Schrock uint64_t guid = zc->zc_guid;
20906809eb4eSEric Schrock int error;
20916809eb4eSEric Schrock
20926809eb4eSEric Schrock error = spa_open(zc->zc_name, &spa, FTAG);
20936809eb4eSEric Schrock if (error != 0)
20946809eb4eSEric Schrock return (error);
20956809eb4eSEric Schrock
20966809eb4eSEric Schrock error = spa_vdev_setfru(spa, guid, fru);
20976809eb4eSEric Schrock spa_close(spa, FTAG);
20986809eb4eSEric Schrock return (error);
20996809eb4eSEric Schrock }
21006809eb4eSEric Schrock
2101c67d9675Seschrock static int
zfs_ioc_objset_stats_impl(zfs_cmd_t * zc,objset_t * os)2102a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2103fa9e4066Sahrens {
2104a7f53a56SChris Kirby int error = 0;
21057f7322feSeschrock nvlist_t *nv;
2106fa9e4066Sahrens
2107a2eea2e1Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2108fa9e4066Sahrens
21095ad82045Snd150628 if (zc->zc_nvlist_dst != 0 &&
211092241e0bSTom Erickson (error = dsl_prop_get_all(os, &nv)) == 0) {
2111a2eea2e1Sahrens dmu_objset_stats(os, nv);
2112432f72fdSahrens /*
2113bd00f61bSrm160521 * NB: zvol_get_stats() will read the objset contents,
2114432f72fdSahrens * which we aren't supposed to do with a
2115745cd3c5Smaybee * DS_MODE_USER hold, because it could be
2116432f72fdSahrens * inconsistent. So this is a bit of a workaround...
2117503ad85cSMatthew Ahrens * XXX reading with out owning
2118432f72fdSahrens */
211919b94df9SMatthew Ahrens if (!zc->zc_objset_stats.dds_inconsistent &&
212019b94df9SMatthew Ahrens dmu_objset_type(os) == DMU_OST_ZVOL) {
212119b94df9SMatthew Ahrens error = zvol_get_stats(os, nv);
212219b94df9SMatthew Ahrens if (error == EIO)
212319b94df9SMatthew Ahrens return (error);
2124fb09f5aaSMadhav Suresh VERIFY0(error);
2125e7437265Sahrens }
2126e9dbad6fSeschrock error = put_nvlist(zc, nv);
21277f7322feSeschrock nvlist_free(nv);
21287f7322feSeschrock }
21297f7322feSeschrock
2130a7f53a56SChris Kirby return (error);
2131a7f53a56SChris Kirby }
2132a7f53a56SChris Kirby
2133a7f53a56SChris Kirby /*
2134a7f53a56SChris Kirby * inputs:
2135a7f53a56SChris Kirby * zc_name name of filesystem
2136a7f53a56SChris Kirby * zc_nvlist_dst_size size of buffer for property nvlist
2137a7f53a56SChris Kirby *
2138a7f53a56SChris Kirby * outputs:
2139a7f53a56SChris Kirby * zc_objset_stats stats
2140a7f53a56SChris Kirby * zc_nvlist_dst property nvlist
2141a7f53a56SChris Kirby * zc_nvlist_dst_size size of property nvlist
2142a7f53a56SChris Kirby */
2143a7f53a56SChris Kirby static int
zfs_ioc_objset_stats(zfs_cmd_t * zc)2144a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2145a7f53a56SChris Kirby {
21463b2aab18SMatthew Ahrens objset_t *os;
2147a7f53a56SChris Kirby int error;
2148a7f53a56SChris Kirby
21493b2aab18SMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os);
21503b2aab18SMatthew Ahrens if (error == 0) {
2151a7f53a56SChris Kirby error = zfs_ioc_objset_stats_impl(zc, os);
2152503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
21533b2aab18SMatthew Ahrens }
2154a7f53a56SChris Kirby
2155fa9e4066Sahrens return (error);
2156fa9e4066Sahrens }
2157fa9e4066Sahrens
215892241e0bSTom Erickson /*
215992241e0bSTom Erickson * inputs:
216092241e0bSTom Erickson * zc_name name of filesystem
216192241e0bSTom Erickson * zc_nvlist_dst_size size of buffer for property nvlist
216292241e0bSTom Erickson *
216392241e0bSTom Erickson * outputs:
216492241e0bSTom Erickson * zc_nvlist_dst received property nvlist
216592241e0bSTom Erickson * zc_nvlist_dst_size size of received property nvlist
216692241e0bSTom Erickson *
216792241e0bSTom Erickson * Gets received properties (distinct from local properties on or after
216892241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
216992241e0bSTom Erickson * local property values.
217092241e0bSTom Erickson */
217192241e0bSTom Erickson static int
zfs_ioc_objset_recvd_props(zfs_cmd_t * zc)217292241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
217392241e0bSTom Erickson {
21743b2aab18SMatthew Ahrens int error = 0;
217592241e0bSTom Erickson nvlist_t *nv;
217692241e0bSTom Erickson
217792241e0bSTom Erickson /*
217892241e0bSTom Erickson * Without this check, we would return local property values if the
217992241e0bSTom Erickson * caller has not already received properties on or after
218092241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS.
218192241e0bSTom Erickson */
21823b2aab18SMatthew Ahrens if (!dsl_prop_get_hasrecvd(zc->zc_name))
2183be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
218492241e0bSTom Erickson
218592241e0bSTom Erickson if (zc->zc_nvlist_dst != 0 &&
21863b2aab18SMatthew Ahrens (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
218792241e0bSTom Erickson error = put_nvlist(zc, nv);
218892241e0bSTom Erickson nvlist_free(nv);
218992241e0bSTom Erickson }
219092241e0bSTom Erickson
219192241e0bSTom Erickson return (error);
219292241e0bSTom Erickson }
219392241e0bSTom Erickson
2194de8267e0Stimh static int
nvl_add_zplprop(objset_t * os,nvlist_t * props,zfs_prop_t prop)2195de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2196de8267e0Stimh {
2197de8267e0Stimh uint64_t value;
2198de8267e0Stimh int error;
2199de8267e0Stimh
2200de8267e0Stimh /*
2201de8267e0Stimh * zfs_get_zplprop() will either find a value or give us
2202de8267e0Stimh * the default value (if there is one).
2203de8267e0Stimh */
2204de8267e0Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2205de8267e0Stimh return (error);
2206de8267e0Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2207de8267e0Stimh return (0);
2208de8267e0Stimh }
2209de8267e0Stimh
2210de8267e0Stimh /*
2211de8267e0Stimh * inputs:
2212de8267e0Stimh * zc_name name of filesystem
2213de8267e0Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist
2214de8267e0Stimh *
2215de8267e0Stimh * outputs:
2216de8267e0Stimh * zc_nvlist_dst zpl property nvlist
2217de8267e0Stimh * zc_nvlist_dst_size size of zpl property nvlist
2218de8267e0Stimh */
2219de8267e0Stimh static int
zfs_ioc_objset_zplprops(zfs_cmd_t * zc)2220de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2221de8267e0Stimh {
2222de8267e0Stimh objset_t *os;
2223de8267e0Stimh int err;
2224de8267e0Stimh
2225503ad85cSMatthew Ahrens /* XXX reading without owning */
2226503ad85cSMatthew Ahrens if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2227de8267e0Stimh return (err);
2228de8267e0Stimh
2229de8267e0Stimh dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2230de8267e0Stimh
2231de8267e0Stimh /*
2232de8267e0Stimh * NB: nvl_add_zplprop() will read the objset contents,
2233745cd3c5Smaybee * which we aren't supposed to do with a DS_MODE_USER
2234745cd3c5Smaybee * hold, because it could be inconsistent.
2235de8267e0Stimh */
2236de8267e0Stimh if (zc->zc_nvlist_dst != NULL &&
2237de8267e0Stimh !zc->zc_objset_stats.dds_inconsistent &&
2238de8267e0Stimh dmu_objset_type(os) == DMU_OST_ZFS) {
2239de8267e0Stimh nvlist_t *nv;
2240de8267e0Stimh
2241de8267e0Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2242de8267e0Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2243de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2244de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2245de8267e0Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2246de8267e0Stimh err = put_nvlist(zc, nv);
2247de8267e0Stimh nvlist_free(nv);
2248de8267e0Stimh } else {
2249be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT);
2250de8267e0Stimh }
2251503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
2252de8267e0Stimh return (err);
2253de8267e0Stimh }
2254de8267e0Stimh
225514843421SMatthew Ahrens static boolean_t
dataset_name_hidden(const char * name)225614843421SMatthew Ahrens dataset_name_hidden(const char *name)
225714843421SMatthew Ahrens {
225814843421SMatthew Ahrens /*
225914843421SMatthew Ahrens * Skip over datasets that are not visible in this zone,
226014843421SMatthew Ahrens * internal datasets (which have a $ in their name), and
226114843421SMatthew Ahrens * temporary datasets (which have a % in their name).
226214843421SMatthew Ahrens */
226314843421SMatthew Ahrens if (strchr(name, '$') != NULL)
226414843421SMatthew Ahrens return (B_TRUE);
226514843421SMatthew Ahrens if (strchr(name, '%') != NULL)
226614843421SMatthew Ahrens return (B_TRUE);
226714843421SMatthew Ahrens if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
226814843421SMatthew Ahrens return (B_TRUE);
226914843421SMatthew Ahrens return (B_FALSE);
227014843421SMatthew Ahrens }
227114843421SMatthew Ahrens
22723cb34c60Sahrens /*
22733cb34c60Sahrens * inputs:
22743cb34c60Sahrens * zc_name name of filesystem
22753cb34c60Sahrens * zc_cookie zap cursor
22763cb34c60Sahrens * zc_nvlist_dst_size size of buffer for property nvlist
22773cb34c60Sahrens *
22783cb34c60Sahrens * outputs:
22793cb34c60Sahrens * zc_name name of next filesystem
228014843421SMatthew Ahrens * zc_cookie zap cursor
22813cb34c60Sahrens * zc_objset_stats stats
22823cb34c60Sahrens * zc_nvlist_dst property nvlist
22833cb34c60Sahrens * zc_nvlist_dst_size size of property nvlist
22843cb34c60Sahrens */
2285fa9e4066Sahrens static int
zfs_ioc_dataset_list_next(zfs_cmd_t * zc)2286fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2287fa9e4066Sahrens {
228887e5029aSahrens objset_t *os;
2289fa9e4066Sahrens int error;
2290fa9e4066Sahrens char *p;
2291620252bcSChris Kirby size_t orig_len = strlen(zc->zc_name);
2292fa9e4066Sahrens
2293620252bcSChris Kirby top:
2294503ad85cSMatthew Ahrens if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
229587e5029aSahrens if (error == ENOENT)
2296be6fd75aSMatthew Ahrens error = SET_ERROR(ESRCH);
229787e5029aSahrens return (error);
2298fa9e4066Sahrens }
2299fa9e4066Sahrens
2300fa9e4066Sahrens p = strrchr(zc->zc_name, '/');
2301fa9e4066Sahrens if (p == NULL || p[1] != '\0')
2302fa9e4066Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2303fa9e4066Sahrens p = zc->zc_name + strlen(zc->zc_name);
2304fa9e4066Sahrens
2305fa9e4066Sahrens do {
230687e5029aSahrens error = dmu_dir_list_next(os,
230787e5029aSahrens sizeof (zc->zc_name) - (p - zc->zc_name), p,
230887e5029aSahrens NULL, &zc->zc_cookie);
2309fa9e4066Sahrens if (error == ENOENT)
2310be6fd75aSMatthew Ahrens error = SET_ERROR(ESRCH);
231119b94df9SMatthew Ahrens } while (error == 0 && dataset_name_hidden(zc->zc_name));
2312503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
2313fa9e4066Sahrens
2314681d9761SEric Taylor /*
2315681d9761SEric Taylor * If it's an internal dataset (ie. with a '$' in its name),
2316681d9761SEric Taylor * don't try to get stats for it, otherwise we'll return ENOENT.
2317681d9761SEric Taylor */
2318620252bcSChris Kirby if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
231987e5029aSahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2320620252bcSChris Kirby if (error == ENOENT) {
2321620252bcSChris Kirby /* We lost a race with destroy, get the next one. */
2322620252bcSChris Kirby zc->zc_name[orig_len] = '\0';
2323620252bcSChris Kirby goto top;
2324620252bcSChris Kirby }
2325620252bcSChris Kirby }
2326fa9e4066Sahrens return (error);
2327fa9e4066Sahrens }
2328fa9e4066Sahrens
23293cb34c60Sahrens /*
23303cb34c60Sahrens * inputs:
23313cb34c60Sahrens * zc_name name of filesystem
23323cb34c60Sahrens * zc_cookie zap cursor
23333cb34c60Sahrens * zc_nvlist_dst_size size of buffer for property nvlist
23343cb34c60Sahrens *
23353cb34c60Sahrens * outputs:
23363cb34c60Sahrens * zc_name name of next snapshot
23373cb34c60Sahrens * zc_objset_stats stats
23383cb34c60Sahrens * zc_nvlist_dst property nvlist
23393cb34c60Sahrens * zc_nvlist_dst_size size of property nvlist
23403cb34c60Sahrens */
2341fa9e4066Sahrens static int
zfs_ioc_snapshot_list_next(zfs_cmd_t * zc)2342fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2343fa9e4066Sahrens {
234487e5029aSahrens objset_t *os;
2345fa9e4066Sahrens int error;
2346fa9e4066Sahrens
2347503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os);
23483b2aab18SMatthew Ahrens if (error != 0) {
2349745cd3c5Smaybee return (error == ENOENT ? ESRCH : error);
23503b2aab18SMatthew Ahrens }
2351fa9e4066Sahrens
2352b81d61a6Slling /*
2353b81d61a6Slling * A dataset name of maximum length cannot have any snapshots,
2354b81d61a6Slling * so exit immediately.
2355b81d61a6Slling */
2356*40a5c998SMatthew Ahrens if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2357*40a5c998SMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN) {
2358503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
2359be6fd75aSMatthew Ahrens return (SET_ERROR(ESRCH));
2360fa9e4066Sahrens }
2361fa9e4066Sahrens
236287e5029aSahrens error = dmu_snapshot_list_next(os,
236387e5029aSahrens sizeof (zc->zc_name) - strlen(zc->zc_name),
2364a7f53a56SChris Kirby zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2365a7f53a56SChris Kirby NULL);
2366a7f53a56SChris Kirby
2367620252bcSChris Kirby if (error == 0) {
2368a7f53a56SChris Kirby dsl_dataset_t *ds;
2369a7f53a56SChris Kirby dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2370a7f53a56SChris Kirby
2371a7f53a56SChris Kirby error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
23723b2aab18SMatthew Ahrens if (error == 0) {
2373a7f53a56SChris Kirby objset_t *ossnap;
2374a7f53a56SChris Kirby
2375a7f53a56SChris Kirby error = dmu_objset_from_ds(ds, &ossnap);
2376a7f53a56SChris Kirby if (error == 0)
2377a7f53a56SChris Kirby error = zfs_ioc_objset_stats_impl(zc, ossnap);
2378a7f53a56SChris Kirby dsl_dataset_rele(ds, FTAG);
2379a7f53a56SChris Kirby }
2380620252bcSChris Kirby } else if (error == ENOENT) {
2381be6fd75aSMatthew Ahrens error = SET_ERROR(ESRCH);
2382620252bcSChris Kirby }
2383fa9e4066Sahrens
2384a7f53a56SChris Kirby dmu_objset_rele(os, FTAG);
23853cb34c60Sahrens /* if we failed, undo the @ that we tacked on to zc_name */
23863b2aab18SMatthew Ahrens if (error != 0)
23873cb34c60Sahrens *strchr(zc->zc_name, '@') = '\0';
2388fa9e4066Sahrens return (error);
2389fa9e4066Sahrens }
2390fa9e4066Sahrens
239192241e0bSTom Erickson static int
zfs_prop_set_userquota(const char * dsname,nvpair_t * pair)239292241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2393e9dbad6fSeschrock {
239492241e0bSTom Erickson const char *propname = nvpair_name(pair);
239514843421SMatthew Ahrens uint64_t *valary;
239614843421SMatthew Ahrens unsigned int vallen;
239714843421SMatthew Ahrens const char *domain;
2398eeb85002STim Haley char *dash;
239914843421SMatthew Ahrens zfs_userquota_prop_t type;
240014843421SMatthew Ahrens uint64_t rid;
240114843421SMatthew Ahrens uint64_t quota;
240214843421SMatthew Ahrens zfsvfs_t *zfsvfs;
240392241e0bSTom Erickson int err;
240414843421SMatthew Ahrens
240592241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
240692241e0bSTom Erickson nvlist_t *attrs;
240792241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2408eeb85002STim Haley if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2409eeb85002STim Haley &pair) != 0)
2410be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
241192241e0bSTom Erickson }
241292241e0bSTom Erickson
2413eeb85002STim Haley /*
2414eeb85002STim Haley * A correctly constructed propname is encoded as
2415eeb85002STim Haley * userquota@<rid>-<domain>.
2416eeb85002STim Haley */
2417eeb85002STim Haley if ((dash = strchr(propname, '-')) == NULL ||
2418eeb85002STim Haley nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2419eeb85002STim Haley vallen != 3)
2420be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
2421eeb85002STim Haley
2422eeb85002STim Haley domain = dash + 1;
242314843421SMatthew Ahrens type = valary[0];
242414843421SMatthew Ahrens rid = valary[1];
242514843421SMatthew Ahrens quota = valary[2];
242614843421SMatthew Ahrens
24271412a1a2SMark Shellenbaum err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
242892241e0bSTom Erickson if (err == 0) {
242992241e0bSTom Erickson err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
243014843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG);
243114843421SMatthew Ahrens }
243292241e0bSTom Erickson
243392241e0bSTom Erickson return (err);
2434ecd6cf80Smarks }
243592241e0bSTom Erickson
243692241e0bSTom Erickson /*
243792241e0bSTom Erickson * If the named property is one that has a special function to set its value,
243892241e0bSTom Erickson * return 0 on success and a positive error code on failure; otherwise if it is
243992241e0bSTom Erickson * not one of the special properties handled by this function, return -1.
244092241e0bSTom Erickson *
2441eeb85002STim Haley * XXX: It would be better for callers of the property interface if we handled
244292241e0bSTom Erickson * these special cases in dsl_prop.c (in the dsl layer).
244392241e0bSTom Erickson */
244492241e0bSTom Erickson static int
zfs_prop_set_special(const char * dsname,zprop_source_t source,nvpair_t * pair)244592241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
244692241e0bSTom Erickson nvpair_t *pair)
244792241e0bSTom Erickson {
244892241e0bSTom Erickson const char *propname = nvpair_name(pair);
244992241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname);
245092241e0bSTom Erickson uint64_t intval;
2451b5152584SMatthew Ahrens int err = -1;
245292241e0bSTom Erickson
245392241e0bSTom Erickson if (prop == ZPROP_INVAL) {
245492241e0bSTom Erickson if (zfs_prop_userquota(propname))
245592241e0bSTom Erickson return (zfs_prop_set_userquota(dsname, pair));
245692241e0bSTom Erickson return (-1);
245714843421SMatthew Ahrens }
2458e9dbad6fSeschrock
245992241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
246092241e0bSTom Erickson nvlist_t *attrs;
246192241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
246292241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
246392241e0bSTom Erickson &pair) == 0);
246492241e0bSTom Erickson }
246592241e0bSTom Erickson
246692241e0bSTom Erickson if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
246792241e0bSTom Erickson return (-1);
246892241e0bSTom Erickson
246992241e0bSTom Erickson VERIFY(0 == nvpair_value_uint64(pair, &intval));
247092241e0bSTom Erickson
2471e9dbad6fSeschrock switch (prop) {
2472e9dbad6fSeschrock case ZFS_PROP_QUOTA:
247392241e0bSTom Erickson err = dsl_dir_set_quota(dsname, source, intval);
2474e9dbad6fSeschrock break;
2475a9799022Sck153898 case ZFS_PROP_REFQUOTA:
24763b2aab18SMatthew Ahrens err = dsl_dataset_set_refquota(dsname, source, intval);
2477a9799022Sck153898 break;
2478a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT:
2479a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT:
2480a2afb611SJerry Jelinek if (intval == UINT64_MAX) {
2481a2afb611SJerry Jelinek /* clearing the limit, just do it */
2482a2afb611SJerry Jelinek err = 0;
2483a2afb611SJerry Jelinek } else {
2484a2afb611SJerry Jelinek err = dsl_dir_activate_fs_ss_limit(dsname);
2485a2afb611SJerry Jelinek }
2486a2afb611SJerry Jelinek /*
2487a2afb611SJerry Jelinek * Set err to -1 to force the zfs_set_prop_nvlist code down the
2488a2afb611SJerry Jelinek * default path to set the value in the nvlist.
2489a2afb611SJerry Jelinek */
2490a2afb611SJerry Jelinek if (err == 0)
2491a2afb611SJerry Jelinek err = -1;
2492a2afb611SJerry Jelinek break;
2493e9dbad6fSeschrock case ZFS_PROP_RESERVATION:
249492241e0bSTom Erickson err = dsl_dir_set_reservation(dsname, source, intval);
2495e9dbad6fSeschrock break;
2496a9799022Sck153898 case ZFS_PROP_REFRESERVATION:
24973b2aab18SMatthew Ahrens err = dsl_dataset_set_refreservation(dsname, source, intval);
2498a9799022Sck153898 break;
2499e9dbad6fSeschrock case ZFS_PROP_VOLSIZE:
2500c61ea566SGeorge Wilson err = zvol_set_volsize(dsname, intval);
2501e9dbad6fSeschrock break;
2502e7437265Sahrens case ZFS_PROP_VERSION:
250314843421SMatthew Ahrens {
250414843421SMatthew Ahrens zfsvfs_t *zfsvfs;
250514843421SMatthew Ahrens
25061412a1a2SMark Shellenbaum if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
250792241e0bSTom Erickson break;
250892241e0bSTom Erickson
250992241e0bSTom Erickson err = zfs_set_version(zfsvfs, intval);
251014843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG);
251114843421SMatthew Ahrens
251292241e0bSTom Erickson if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2513b16da2e2SGeorge Wilson zfs_cmd_t *zc;
2514b16da2e2SGeorge Wilson
2515b16da2e2SGeorge Wilson zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2516b16da2e2SGeorge Wilson (void) strcpy(zc->zc_name, dsname);
2517b16da2e2SGeorge Wilson (void) zfs_ioc_userspace_upgrade(zc);
2518b16da2e2SGeorge Wilson kmem_free(zc, sizeof (zfs_cmd_t));
251914843421SMatthew Ahrens }
25204201a95eSRic Aleshire break;
25214201a95eSRic Aleshire }
2522e9dbad6fSeschrock default:
252392241e0bSTom Erickson err = -1;
25245c0b6a79SRich Morris }
252592241e0bSTom Erickson
252692241e0bSTom Erickson return (err);
252792241e0bSTom Erickson }
252892241e0bSTom Erickson
252992241e0bSTom Erickson /*
253092241e0bSTom Erickson * This function is best effort. If it fails to set any of the given properties,
25314445fffbSMatthew Ahrens * it continues to set as many as it can and returns the last error
25324445fffbSMatthew Ahrens * encountered. If the caller provides a non-NULL errlist, it will be filled in
25334445fffbSMatthew Ahrens * with the list of names of all the properties that failed along with the
25344445fffbSMatthew Ahrens * corresponding error numbers.
253592241e0bSTom Erickson *
25364445fffbSMatthew Ahrens * If every property is set successfully, zero is returned and errlist is not
25374445fffbSMatthew Ahrens * modified.
253892241e0bSTom Erickson */
253992241e0bSTom Erickson int
zfs_set_prop_nvlist(const char * dsname,zprop_source_t source,nvlist_t * nvl,nvlist_t * errlist)254092241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
25414445fffbSMatthew Ahrens nvlist_t *errlist)
254292241e0bSTom Erickson {
254392241e0bSTom Erickson nvpair_t *pair;
254492241e0bSTom Erickson nvpair_t *propval;
254502e383d1STom Erickson int rv = 0;
254692241e0bSTom Erickson uint64_t intval;
254792241e0bSTom Erickson char *strval;
25484445fffbSMatthew Ahrens nvlist_t *genericnvl = fnvlist_alloc();
25494445fffbSMatthew Ahrens nvlist_t *retrynvl = fnvlist_alloc();
255092241e0bSTom Erickson
255192241e0bSTom Erickson retry:
255292241e0bSTom Erickson pair = NULL;
255392241e0bSTom Erickson while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
255492241e0bSTom Erickson const char *propname = nvpair_name(pair);
255592241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname);
2556cfa69fd2STom Erickson int err = 0;
255792241e0bSTom Erickson
255892241e0bSTom Erickson /* decode the property value */
255992241e0bSTom Erickson propval = pair;
256092241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
256192241e0bSTom Erickson nvlist_t *attrs;
25624445fffbSMatthew Ahrens attrs = fnvpair_value_nvlist(pair);
2563eeb85002STim Haley if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2564eeb85002STim Haley &propval) != 0)
2565be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL);
256692241e0bSTom Erickson }
256792241e0bSTom Erickson
256892241e0bSTom Erickson /* Validate value type */
2569eeb85002STim Haley if (err == 0 && prop == ZPROP_INVAL) {
257092241e0bSTom Erickson if (zfs_prop_user(propname)) {
257192241e0bSTom Erickson if (nvpair_type(propval) != DATA_TYPE_STRING)
2572be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL);
257392241e0bSTom Erickson } else if (zfs_prop_userquota(propname)) {
257492241e0bSTom Erickson if (nvpair_type(propval) !=
257592241e0bSTom Erickson DATA_TYPE_UINT64_ARRAY)
2576be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL);
257719b94df9SMatthew Ahrens } else {
2578be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL);
257992241e0bSTom Erickson }
2580eeb85002STim Haley } else if (err == 0) {
258192241e0bSTom Erickson if (nvpair_type(propval) == DATA_TYPE_STRING) {
258292241e0bSTom Erickson if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2583be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL);
258492241e0bSTom Erickson } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2585a2eea2e1Sahrens const char *unused;
2586a2eea2e1Sahrens
25874445fffbSMatthew Ahrens intval = fnvpair_value_uint64(propval);
2588e9dbad6fSeschrock
2589e9dbad6fSeschrock switch (zfs_prop_get_type(prop)) {
259091ebeef5Sahrens case PROP_TYPE_NUMBER:
2591e9dbad6fSeschrock break;
259291ebeef5Sahrens case PROP_TYPE_STRING:
2593be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL);
259492241e0bSTom Erickson break;
259591ebeef5Sahrens case PROP_TYPE_INDEX:
2596acd76fe5Seschrock if (zfs_prop_index_to_string(prop,
259792241e0bSTom Erickson intval, &unused) != 0)
2598be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL);
2599e9dbad6fSeschrock break;
2600e9dbad6fSeschrock default:
2601e7437265Sahrens cmn_err(CE_PANIC,
2602e7437265Sahrens "unknown property type");
2603e9dbad6fSeschrock }
2604e9dbad6fSeschrock } else {
2605be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL);
2606e9dbad6fSeschrock }
2607e9dbad6fSeschrock }
2608e9dbad6fSeschrock
260992241e0bSTom Erickson /* Validate permissions */
261092241e0bSTom Erickson if (err == 0)
261192241e0bSTom Erickson err = zfs_check_settable(dsname, pair, CRED());
261292241e0bSTom Erickson
261392241e0bSTom Erickson if (err == 0) {
261492241e0bSTom Erickson err = zfs_prop_set_special(dsname, source, pair);
261592241e0bSTom Erickson if (err == -1) {
261692241e0bSTom Erickson /*
261792241e0bSTom Erickson * For better performance we build up a list of
261892241e0bSTom Erickson * properties to set in a single transaction.
261992241e0bSTom Erickson */
262092241e0bSTom Erickson err = nvlist_add_nvpair(genericnvl, pair);
262192241e0bSTom Erickson } else if (err != 0 && nvl != retrynvl) {
262292241e0bSTom Erickson /*
262392241e0bSTom Erickson * This may be a spurious error caused by
262492241e0bSTom Erickson * receiving quota and reservation out of order.
262592241e0bSTom Erickson * Try again in a second pass.
262692241e0bSTom Erickson */
262792241e0bSTom Erickson err = nvlist_add_nvpair(retrynvl, pair);
26285c0b6a79SRich Morris }
262992241e0bSTom Erickson }
263092241e0bSTom Erickson
26314445fffbSMatthew Ahrens if (err != 0) {
26324445fffbSMatthew Ahrens if (errlist != NULL)
26334445fffbSMatthew Ahrens fnvlist_add_int32(errlist, propname, err);
26344445fffbSMatthew Ahrens rv = err;
26354445fffbSMatthew Ahrens }
263692241e0bSTom Erickson }
263792241e0bSTom Erickson
263892241e0bSTom Erickson if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
263992241e0bSTom Erickson nvl = retrynvl;
264092241e0bSTom Erickson goto retry;
264192241e0bSTom Erickson }
264292241e0bSTom Erickson
264392241e0bSTom Erickson if (!nvlist_empty(genericnvl) &&
264492241e0bSTom Erickson dsl_props_set(dsname, source, genericnvl) != 0) {
264592241e0bSTom Erickson /*
264692241e0bSTom Erickson * If this fails, we still want to set as many properties as we
264792241e0bSTom Erickson * can, so try setting them individually.
264892241e0bSTom Erickson */
264992241e0bSTom Erickson pair = NULL;
265092241e0bSTom Erickson while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
265192241e0bSTom Erickson const char *propname = nvpair_name(pair);
2652cfa69fd2STom Erickson int err = 0;
265392241e0bSTom Erickson
265492241e0bSTom Erickson propval = pair;
265592241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
265692241e0bSTom Erickson nvlist_t *attrs;
26574445fffbSMatthew Ahrens attrs = fnvpair_value_nvlist(pair);
26584445fffbSMatthew Ahrens propval = fnvlist_lookup_nvpair(attrs,
26594445fffbSMatthew Ahrens ZPROP_VALUE);
266092241e0bSTom Erickson }
266192241e0bSTom Erickson
266292241e0bSTom Erickson if (nvpair_type(propval) == DATA_TYPE_STRING) {
26634445fffbSMatthew Ahrens strval = fnvpair_value_string(propval);
26643b2aab18SMatthew Ahrens err = dsl_prop_set_string(dsname, propname,
26653b2aab18SMatthew Ahrens source, strval);
266692241e0bSTom Erickson } else {
26674445fffbSMatthew Ahrens intval = fnvpair_value_uint64(propval);
26683b2aab18SMatthew Ahrens err = dsl_prop_set_int(dsname, propname, source,
26693b2aab18SMatthew Ahrens intval);
267092241e0bSTom Erickson }
267192241e0bSTom Erickson
267292241e0bSTom Erickson if (err != 0) {
26734445fffbSMatthew Ahrens if (errlist != NULL) {
26744445fffbSMatthew Ahrens fnvlist_add_int32(errlist, propname,
26754445fffbSMatthew Ahrens err);
26764445fffbSMatthew Ahrens }
26774445fffbSMatthew Ahrens rv = err;
267892241e0bSTom Erickson }
267992241e0bSTom Erickson }
268092241e0bSTom Erickson }
26815c0b6a79SRich Morris nvlist_free(genericnvl);
268292241e0bSTom Erickson nvlist_free(retrynvl);
268392241e0bSTom Erickson
268492241e0bSTom Erickson return (rv);
2685e9dbad6fSeschrock }
2686e9dbad6fSeschrock
26873cb34c60Sahrens /*
2688ea2f5b9eSMatthew Ahrens * Check that all the properties are valid user properties.
2689ea2f5b9eSMatthew Ahrens */
2690ea2f5b9eSMatthew Ahrens static int
zfs_check_userprops(const char * fsname,nvlist_t * nvl)26914445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2692ea2f5b9eSMatthew Ahrens {
269392241e0bSTom Erickson nvpair_t *pair = NULL;
2694ea2f5b9eSMatthew Ahrens int error = 0;
2695ea2f5b9eSMatthew Ahrens
269692241e0bSTom Erickson while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
269792241e0bSTom Erickson const char *propname = nvpair_name(pair);
2698ea2f5b9eSMatthew Ahrens
2699ea2f5b9eSMatthew Ahrens if (!zfs_prop_user(propname) ||
270092241e0bSTom Erickson nvpair_type(pair) != DATA_TYPE_STRING)
2701be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
2702ea2f5b9eSMatthew Ahrens
2703ea2f5b9eSMatthew Ahrens if (error = zfs_secpolicy_write_perms(fsname,
2704ea2f5b9eSMatthew Ahrens ZFS_DELEG_PERM_USERPROP, CRED()))
2705ea2f5b9eSMatthew Ahrens return (error);
2706ea2f5b9eSMatthew Ahrens
2707ea2f5b9eSMatthew Ahrens if (strlen(propname) >= ZAP_MAXNAMELEN)
2708be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
2709ea2f5b9eSMatthew Ahrens
271078f17100SMatthew Ahrens if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2711ea2f5b9eSMatthew Ahrens return (E2BIG);
2712ea2f5b9eSMatthew Ahrens }
2713ea2f5b9eSMatthew Ahrens return (0);
2714ea2f5b9eSMatthew Ahrens }
2715ea2f5b9eSMatthew Ahrens
271692241e0bSTom Erickson static void
props_skip(nvlist_t * props,nvlist_t * skipped,nvlist_t ** newprops)271792241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
271892241e0bSTom Erickson {
271992241e0bSTom Erickson nvpair_t *pair;
272092241e0bSTom Erickson
272192241e0bSTom Erickson VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
272292241e0bSTom Erickson
272392241e0bSTom Erickson pair = NULL;
272492241e0bSTom Erickson while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
272592241e0bSTom Erickson if (nvlist_exists(skipped, nvpair_name(pair)))
272692241e0bSTom Erickson continue;
272792241e0bSTom Erickson
272892241e0bSTom Erickson VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
272992241e0bSTom Erickson }
273092241e0bSTom Erickson }
273192241e0bSTom Erickson
273292241e0bSTom Erickson static int
clear_received_props(const char * dsname,nvlist_t * props,nvlist_t * skipped)27333b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
273492241e0bSTom Erickson nvlist_t *skipped)
273592241e0bSTom Erickson {
273692241e0bSTom Erickson int err = 0;
273792241e0bSTom Erickson nvlist_t *cleared_props = NULL;
273892241e0bSTom Erickson props_skip(props, skipped, &cleared_props);
273992241e0bSTom Erickson if (!nvlist_empty(cleared_props)) {
274092241e0bSTom Erickson /*
274192241e0bSTom Erickson * Acts on local properties until the dataset has received
274292241e0bSTom Erickson * properties at least once on or after SPA_VERSION_RECVD_PROPS.
274392241e0bSTom Erickson */
274492241e0bSTom Erickson zprop_source_t flags = (ZPROP_SRC_NONE |
27453b2aab18SMatthew Ahrens (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
27463b2aab18SMatthew Ahrens err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
274792241e0bSTom Erickson }
274892241e0bSTom Erickson nvlist_free(cleared_props);
274992241e0bSTom Erickson return (err);
275092241e0bSTom Erickson }
275192241e0bSTom Erickson
2752ea2f5b9eSMatthew Ahrens /*
27533cb34c60Sahrens * inputs:
27543cb34c60Sahrens * zc_name name of filesystem
27555c0b6a79SRich Morris * zc_value name of property to set
27563cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply
275792241e0bSTom Erickson * zc_cookie received properties flag
27583cb34c60Sahrens *
275992241e0bSTom Erickson * outputs:
276092241e0bSTom Erickson * zc_nvlist_dst{_size} error for each unapplied received property
27613cb34c60Sahrens */
2762e9dbad6fSeschrock static int
zfs_ioc_set_prop(zfs_cmd_t * zc)2763fa9e4066Sahrens zfs_ioc_set_prop(zfs_cmd_t *zc)
2764fa9e4066Sahrens {
2765e9dbad6fSeschrock nvlist_t *nvl;
276692241e0bSTom Erickson boolean_t received = zc->zc_cookie;
276792241e0bSTom Erickson zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
276892241e0bSTom Erickson ZPROP_SRC_LOCAL);
27694445fffbSMatthew Ahrens nvlist_t *errors;
2770e9dbad6fSeschrock int error;
2771e9dbad6fSeschrock
2772990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2773478ed9adSEric Taylor zc->zc_iflags, &nvl)) != 0)
2774e9dbad6fSeschrock return (error);
2775fa9e4066Sahrens
277692241e0bSTom Erickson if (received) {
2777bb0ade09Sahrens nvlist_t *origprops;
2778bb0ade09Sahrens
27793b2aab18SMatthew Ahrens if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
27803b2aab18SMatthew Ahrens (void) clear_received_props(zc->zc_name,
27813b2aab18SMatthew Ahrens origprops, nvl);
2782bb0ade09Sahrens nvlist_free(origprops);
2783bb0ade09Sahrens }
278492241e0bSTom Erickson
27853b2aab18SMatthew Ahrens error = dsl_prop_set_hasrecvd(zc->zc_name);
2786bb0ade09Sahrens }
2787bb0ade09Sahrens
27884445fffbSMatthew Ahrens errors = fnvlist_alloc();
27893b2aab18SMatthew Ahrens if (error == 0)
27904445fffbSMatthew Ahrens error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2791ecd6cf80Smarks
279292241e0bSTom Erickson if (zc->zc_nvlist_dst != NULL && errors != NULL) {
279392241e0bSTom Erickson (void) put_nvlist(zc, errors);
279492241e0bSTom Erickson }
279592241e0bSTom Erickson
279692241e0bSTom Erickson nvlist_free(errors);
2797e9dbad6fSeschrock nvlist_free(nvl);
2798e9dbad6fSeschrock return (error);
2799fa9e4066Sahrens }
2800fa9e4066Sahrens
28013cb34c60Sahrens /*
28023cb34c60Sahrens * inputs:
28033cb34c60Sahrens * zc_name name of filesystem
28043cb34c60Sahrens * zc_value name of property to inherit
280592241e0bSTom Erickson * zc_cookie revert to received value if TRUE
28063cb34c60Sahrens *
28073cb34c60Sahrens * outputs: none
28083cb34c60Sahrens */
2809fa9e4066Sahrens static int
zfs_ioc_inherit_prop(zfs_cmd_t * zc)2810e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2811e45ce728Sahrens {
281292241e0bSTom Erickson const char *propname = zc->zc_value;
281392241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname);
281492241e0bSTom Erickson boolean_t received = zc->zc_cookie;
281592241e0bSTom Erickson zprop_source_t source = (received
281692241e0bSTom Erickson ? ZPROP_SRC_NONE /* revert to received value, if any */
281792241e0bSTom Erickson : ZPROP_SRC_INHERITED); /* explicitly inherit */
281892241e0bSTom Erickson
281992241e0bSTom Erickson if (received) {
282092241e0bSTom Erickson nvlist_t *dummy;
282192241e0bSTom Erickson nvpair_t *pair;
282292241e0bSTom Erickson zprop_type_t type;
282392241e0bSTom Erickson int err;
282492241e0bSTom Erickson
282592241e0bSTom Erickson /*
282692241e0bSTom Erickson * zfs_prop_set_special() expects properties in the form of an
282792241e0bSTom Erickson * nvpair with type info.
282892241e0bSTom Erickson */
282992241e0bSTom Erickson if (prop == ZPROP_INVAL) {
283092241e0bSTom Erickson if (!zfs_prop_user(propname))
2831be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
283292241e0bSTom Erickson
283392241e0bSTom Erickson type = PROP_TYPE_STRING;
2834a79992aaSTom Erickson } else if (prop == ZFS_PROP_VOLSIZE ||
2835a79992aaSTom Erickson prop == ZFS_PROP_VERSION) {
2836be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
283792241e0bSTom Erickson } else {
283892241e0bSTom Erickson type = zfs_prop_get_type(prop);
283992241e0bSTom Erickson }
284092241e0bSTom Erickson
284192241e0bSTom Erickson VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
284292241e0bSTom Erickson
284392241e0bSTom Erickson switch (type) {
284492241e0bSTom Erickson case PROP_TYPE_STRING:
284592241e0bSTom Erickson VERIFY(0 == nvlist_add_string(dummy, propname, ""));
284692241e0bSTom Erickson break;
284792241e0bSTom Erickson case PROP_TYPE_NUMBER:
284892241e0bSTom Erickson case PROP_TYPE_INDEX:
284992241e0bSTom Erickson VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
285092241e0bSTom Erickson break;
285192241e0bSTom Erickson default:
285292241e0bSTom Erickson nvlist_free(dummy);
2853be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
285492241e0bSTom Erickson }
285592241e0bSTom Erickson
285692241e0bSTom Erickson pair = nvlist_next_nvpair(dummy, NULL);
285792241e0bSTom Erickson err = zfs_prop_set_special(zc->zc_name, source, pair);
285892241e0bSTom Erickson nvlist_free(dummy);
285992241e0bSTom Erickson if (err != -1)
286092241e0bSTom Erickson return (err); /* special property already handled */
286192241e0bSTom Erickson } else {
286292241e0bSTom Erickson /*
286392241e0bSTom Erickson * Only check this in the non-received case. We want to allow
286492241e0bSTom Erickson * 'inherit -S' to revert non-inheritable properties like quota
286592241e0bSTom Erickson * and reservation to the received or default values even though
286692241e0bSTom Erickson * they are not considered inheritable.
286792241e0bSTom Erickson */
286892241e0bSTom Erickson if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2869be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
287092241e0bSTom Erickson }
287192241e0bSTom Erickson
28724445fffbSMatthew Ahrens /* property name has been validated by zfs_secpolicy_inherit_prop() */
28733b2aab18SMatthew Ahrens return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2874e45ce728Sahrens }
2875e45ce728Sahrens
2876e45ce728Sahrens static int
zfs_ioc_pool_set_props(zfs_cmd_t * zc)287711a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2878b1b8ab34Slling {
2879990b4856Slling nvlist_t *props;
2880b1b8ab34Slling spa_t *spa;
2881990b4856Slling int error;
288292241e0bSTom Erickson nvpair_t *pair;
2883b1b8ab34Slling
288492241e0bSTom Erickson if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
288592241e0bSTom Erickson zc->zc_iflags, &props))
2886b1b8ab34Slling return (error);
2887b1b8ab34Slling
2888379c004dSEric Schrock /*
2889379c004dSEric Schrock * If the only property is the configfile, then just do a spa_lookup()
2890379c004dSEric Schrock * to handle the faulted case.
2891379c004dSEric Schrock */
289292241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL);
289392241e0bSTom Erickson if (pair != NULL && strcmp(nvpair_name(pair),
2894379c004dSEric Schrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
289592241e0bSTom Erickson nvlist_next_nvpair(props, pair) == NULL) {
2896379c004dSEric Schrock mutex_enter(&spa_namespace_lock);
2897379c004dSEric Schrock if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2898379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE);
2899379c004dSEric Schrock spa_config_sync(spa, B_FALSE, B_TRUE);
2900379c004dSEric Schrock }
2901379c004dSEric Schrock mutex_exit(&spa_namespace_lock);
2902b693757aSEric Schrock if (spa != NULL) {
2903b693757aSEric Schrock nvlist_free(props);
2904379c004dSEric Schrock return (0);
2905379c004dSEric Schrock }
2906b693757aSEric Schrock }
2907379c004dSEric Schrock
2908b1b8ab34Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2909990b4856Slling nvlist_free(props);
2910b1b8ab34Slling return (error);
2911b1b8ab34Slling }
2912b1b8ab34Slling
2913990b4856Slling error = spa_prop_set(spa, props);
2914b1b8ab34Slling
2915990b4856Slling nvlist_free(props);
2916b1b8ab34Slling spa_close(spa, FTAG);
2917b1b8ab34Slling
2918b1b8ab34Slling return (error);
2919b1b8ab34Slling }
2920b1b8ab34Slling
2921b1b8ab34Slling static int
zfs_ioc_pool_get_props(zfs_cmd_t * zc)292211a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2923b1b8ab34Slling {
2924b1b8ab34Slling spa_t *spa;
2925b1b8ab34Slling int error;
2926b1b8ab34Slling nvlist_t *nvp = NULL;
2927b1b8ab34Slling
2928379c004dSEric Schrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2929379c004dSEric Schrock /*
2930379c004dSEric Schrock * If the pool is faulted, there may be properties we can still
2931379c004dSEric Schrock * get (such as altroot and cachefile), so attempt to get them
2932379c004dSEric Schrock * anyway.
2933379c004dSEric Schrock */
2934379c004dSEric Schrock mutex_enter(&spa_namespace_lock);
2935379c004dSEric Schrock if ((spa = spa_lookup(zc->zc_name)) != NULL)
2936990b4856Slling error = spa_prop_get(spa, &nvp);
2937379c004dSEric Schrock mutex_exit(&spa_namespace_lock);
2938379c004dSEric Schrock } else {
2939379c004dSEric Schrock error = spa_prop_get(spa, &nvp);
2940379c004dSEric Schrock spa_close(spa, FTAG);
2941379c004dSEric Schrock }
2942b1b8ab34Slling
2943b1b8ab34Slling if (error == 0 && zc->zc_nvlist_dst != NULL)
2944b1b8ab34Slling error = put_nvlist(zc, nvp);
2945b1b8ab34Slling else
2946be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT);
2947b1b8ab34Slling
2948b1b8ab34Slling nvlist_free(nvp);
2949b1b8ab34Slling return (error);
2950b1b8ab34Slling }
2951b1b8ab34Slling
29523cb34c60Sahrens /*
29533cb34c60Sahrens * inputs:
29543cb34c60Sahrens * zc_name name of filesystem
29553cb34c60Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions
29563cb34c60Sahrens * zc_perm_action allow/unallow flag
29573cb34c60Sahrens *
29583cb34c60Sahrens * outputs: none
29593cb34c60Sahrens */
2960ecd6cf80Smarks static int
zfs_ioc_set_fsacl(zfs_cmd_t * zc)2961ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2962ecd6cf80Smarks {
2963ecd6cf80Smarks int error;
2964ecd6cf80Smarks nvlist_t *fsaclnv = NULL;
2965ecd6cf80Smarks
2966990b4856Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2967478ed9adSEric Taylor zc->zc_iflags, &fsaclnv)) != 0)
2968ecd6cf80Smarks return (error);
2969ecd6cf80Smarks
2970ecd6cf80Smarks /*
2971ecd6cf80Smarks * Verify nvlist is constructed correctly
2972ecd6cf80Smarks */
2973ecd6cf80Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2974ecd6cf80Smarks nvlist_free(fsaclnv);
2975be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
2976ecd6cf80Smarks }
2977ecd6cf80Smarks
2978ecd6cf80Smarks /*
2979ecd6cf80Smarks * If we don't have PRIV_SYS_MOUNT, then validate
2980ecd6cf80Smarks * that user is allowed to hand out each permission in
2981ecd6cf80Smarks * the nvlist(s)
2982ecd6cf80Smarks */
2983ecd6cf80Smarks
298491ebeef5Sahrens error = secpolicy_zfs(CRED());
29853b2aab18SMatthew Ahrens if (error != 0) {
298691ebeef5Sahrens if (zc->zc_perm_action == B_FALSE) {
298791ebeef5Sahrens error = dsl_deleg_can_allow(zc->zc_name,
298891ebeef5Sahrens fsaclnv, CRED());
298991ebeef5Sahrens } else {
299091ebeef5Sahrens error = dsl_deleg_can_unallow(zc->zc_name,
299191ebeef5Sahrens fsaclnv, CRED());
299291ebeef5Sahrens }
2993ecd6cf80Smarks }
2994ecd6cf80Smarks
2995ecd6cf80Smarks if (error == 0)
2996ecd6cf80Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2997ecd6cf80Smarks
2998ecd6cf80Smarks nvlist_free(fsaclnv);
2999ecd6cf80Smarks return (error);
3000ecd6cf80Smarks }
3001ecd6cf80Smarks
30023cb34c60Sahrens /*
30033cb34c60Sahrens * inputs:
30043cb34c60Sahrens * zc_name name of filesystem
30053cb34c60Sahrens *
30063cb34c60Sahrens * outputs:
30073cb34c60Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions
30083cb34c60Sahrens */
3009ecd6cf80Smarks static int
zfs_ioc_get_fsacl(zfs_cmd_t * zc)3010ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3011ecd6cf80Smarks {
3012ecd6cf80Smarks nvlist_t *nvp;
3013ecd6cf80Smarks int error;
3014ecd6cf80Smarks
3015ecd6cf80Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3016ecd6cf80Smarks error = put_nvlist(zc, nvp);
3017ecd6cf80Smarks nvlist_free(nvp);
3018ecd6cf80Smarks }
3019ecd6cf80Smarks
3020ecd6cf80Smarks return (error);
3021ecd6cf80Smarks }
3022ecd6cf80Smarks
30233cb34c60Sahrens /*
3024fa9e4066Sahrens * Search the vfs list for a specified resource. Returns a pointer to it
3025fa9e4066Sahrens * or NULL if no suitable entry is found. The caller of this routine
3026fa9e4066Sahrens * is responsible for releasing the returned vfs pointer.
3027fa9e4066Sahrens */
3028fa9e4066Sahrens static vfs_t *
zfs_get_vfs(const char * resource)3029fa9e4066Sahrens zfs_get_vfs(const char *resource)
3030fa9e4066Sahrens {
3031fa9e4066Sahrens struct vfs *vfsp;
3032fa9e4066Sahrens struct vfs *vfs_found = NULL;
3033fa9e4066Sahrens
3034fa9e4066Sahrens vfs_list_read_lock();
3035fa9e4066Sahrens vfsp = rootvfs;
3036fa9e4066Sahrens do {
3037fa9e4066Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
3038fa9e4066Sahrens VFS_HOLD(vfsp);
3039fa9e4066Sahrens vfs_found = vfsp;
3040fa9e4066Sahrens break;
3041fa9e4066Sahrens }
3042fa9e4066Sahrens vfsp = vfsp->vfs_next;
3043fa9e4066Sahrens } while (vfsp != rootvfs);
3044fa9e4066Sahrens vfs_list_unlock();
3045fa9e4066Sahrens return (vfs_found);
3046fa9e4066Sahrens }
3047fa9e4066Sahrens
3048ecd6cf80Smarks /* ARGSUSED */
3049fa9e4066Sahrens static void
zfs_create_cb(objset_t * os,void * arg,cred_t * cr,dmu_tx_t * tx)3050ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3051fa9e4066Sahrens {
3052da6c28aaSamw zfs_creat_t *zct = arg;
3053e7437265Sahrens
3054de8267e0Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3055da6c28aaSamw }
3056da6c28aaSamw
3057de8267e0Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3058de8267e0Stimh
3059da6c28aaSamw /*
3060de8267e0Stimh * inputs:
30610a48a24eStimh * os parent objset pointer (NULL if root fs)
3062f7170741SWill Andrews * fuids_ok fuids allowed in this version of the spa?
3063f7170741SWill Andrews * sa_ok SAs allowed in this version of the spa?
3064f7170741SWill Andrews * createprops list of properties requested by creator
3065da6c28aaSamw *
3066de8267e0Stimh * outputs:
3067de8267e0Stimh * zplprops values for the zplprops we attach to the master node object
30680a48a24eStimh * is_ci true if requested file system will be purely case-insensitive
3069da6c28aaSamw *
3070de8267e0Stimh * Determine the settings for utf8only, normalization and
3071de8267e0Stimh * casesensitivity. Specific values may have been requested by the
3072de8267e0Stimh * creator and/or we can inherit values from the parent dataset. If
3073de8267e0Stimh * the file system is of too early a vintage, a creator can not
3074de8267e0Stimh * request settings for these properties, even if the requested
3075de8267e0Stimh * setting is the default value. We don't actually want to create dsl
3076de8267e0Stimh * properties for these, so remove them from the source nvlist after
3077de8267e0Stimh * processing.
3078da6c28aaSamw */
3079da6c28aaSamw static int
zfs_fill_zplprops_impl(objset_t * os,uint64_t zplver,boolean_t fuids_ok,boolean_t sa_ok,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)308014843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
30810a586ceaSMark Shellenbaum boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
30820a586ceaSMark Shellenbaum nvlist_t *zplprops, boolean_t *is_ci)
3083da6c28aaSamw {
3084de8267e0Stimh uint64_t sense = ZFS_PROP_UNDEFINED;
3085de8267e0Stimh uint64_t norm = ZFS_PROP_UNDEFINED;
3086de8267e0Stimh uint64_t u8 = ZFS_PROP_UNDEFINED;
3087da6c28aaSamw
3088de8267e0Stimh ASSERT(zplprops != NULL);
3089da6c28aaSamw
3090c2a93d44Stimh /*
3091de8267e0Stimh * Pull out creator prop choices, if any.
3092c2a93d44Stimh */
3093de8267e0Stimh if (createprops) {
3094de8267e0Stimh (void) nvlist_lookup_uint64(createprops,
30950a48a24eStimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
30960a48a24eStimh (void) nvlist_lookup_uint64(createprops,
3097de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3098de8267e0Stimh (void) nvlist_remove_all(createprops,
3099de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3100de8267e0Stimh (void) nvlist_lookup_uint64(createprops,
3101de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3102de8267e0Stimh (void) nvlist_remove_all(createprops,
3103de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3104de8267e0Stimh (void) nvlist_lookup_uint64(createprops,
3105de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3106de8267e0Stimh (void) nvlist_remove_all(createprops,
3107de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE));
3108da6c28aaSamw }
3109da6c28aaSamw
3110c2a93d44Stimh /*
31110a48a24eStimh * If the zpl version requested is whacky or the file system
31120a48a24eStimh * or pool is version is too "young" to support normalization
31130a48a24eStimh * and the creator tried to set a value for one of the props,
31140a48a24eStimh * error out.
3115c2a93d44Stimh */
31160a48a24eStimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
31170a48a24eStimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
31180a586ceaSMark Shellenbaum (zplver >= ZPL_VERSION_SA && !sa_ok) ||
31190a48a24eStimh (zplver < ZPL_VERSION_NORMALIZATION &&
3120de8267e0Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
31210a48a24eStimh sense != ZFS_PROP_UNDEFINED)))
3122be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
3123c2a93d44Stimh
3124de8267e0Stimh /*
3125de8267e0Stimh * Put the version in the zplprops
3126de8267e0Stimh */
3127de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops,
3128de8267e0Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3129de8267e0Stimh
3130de8267e0Stimh if (norm == ZFS_PROP_UNDEFINED)
3131de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3132de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops,
3133de8267e0Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3134de8267e0Stimh
3135de8267e0Stimh /*
3136de8267e0Stimh * If we're normalizing, names must always be valid UTF-8 strings.
3137de8267e0Stimh */
3138de8267e0Stimh if (norm)
3139de8267e0Stimh u8 = 1;
3140de8267e0Stimh if (u8 == ZFS_PROP_UNDEFINED)
3141de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3142de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops,
3143de8267e0Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3144de8267e0Stimh
3145de8267e0Stimh if (sense == ZFS_PROP_UNDEFINED)
3146de8267e0Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3147de8267e0Stimh VERIFY(nvlist_add_uint64(zplprops,
3148de8267e0Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3149de8267e0Stimh
3150ab04eb8eStimh if (is_ci)
3151ab04eb8eStimh *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3152ab04eb8eStimh
3153da6c28aaSamw return (0);
3154fa9e4066Sahrens }
3155fa9e4066Sahrens
31560a48a24eStimh static int
zfs_fill_zplprops(const char * dataset,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)31570a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
31580a48a24eStimh nvlist_t *zplprops, boolean_t *is_ci)
31590a48a24eStimh {
31600a586ceaSMark Shellenbaum boolean_t fuids_ok, sa_ok;
31610a48a24eStimh uint64_t zplver = ZPL_VERSION;
31620a48a24eStimh objset_t *os = NULL;
3163*40a5c998SMatthew Ahrens char parentname[ZFS_MAX_DATASET_NAME_LEN];
31640a48a24eStimh char *cp;
31650a586ceaSMark Shellenbaum spa_t *spa;
31660a586ceaSMark Shellenbaum uint64_t spa_vers;
31670a48a24eStimh int error;
31680a48a24eStimh
31690a48a24eStimh (void) strlcpy(parentname, dataset, sizeof (parentname));
31700a48a24eStimh cp = strrchr(parentname, '/');
31710a48a24eStimh ASSERT(cp != NULL);
31720a48a24eStimh cp[0] = '\0';
31730a48a24eStimh
31740a586ceaSMark Shellenbaum if ((error = spa_open(dataset, &spa, FTAG)) != 0)
31750a586ceaSMark Shellenbaum return (error);
31760a586ceaSMark Shellenbaum
31770a586ceaSMark Shellenbaum spa_vers = spa_version(spa);
31780a586ceaSMark Shellenbaum spa_close(spa, FTAG);
31790a586ceaSMark Shellenbaum
31800a586ceaSMark Shellenbaum zplver = zfs_zpl_version_map(spa_vers);
31810a586ceaSMark Shellenbaum fuids_ok = (zplver >= ZPL_VERSION_FUID);
31820a586ceaSMark Shellenbaum sa_ok = (zplver >= ZPL_VERSION_SA);
31830a48a24eStimh
31840a48a24eStimh /*
31850a48a24eStimh * Open parent object set so we can inherit zplprop values.
31860a48a24eStimh */
3187503ad85cSMatthew Ahrens if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
31880a48a24eStimh return (error);
31890a48a24eStimh
31900a586ceaSMark Shellenbaum error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
31910a48a24eStimh zplprops, is_ci);
3192503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
31930a48a24eStimh return (error);
31940a48a24eStimh }
31950a48a24eStimh
31960a48a24eStimh static int
zfs_fill_zplprops_root(uint64_t spa_vers,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)31970a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
31980a48a24eStimh nvlist_t *zplprops, boolean_t *is_ci)
31990a48a24eStimh {
32000a586ceaSMark Shellenbaum boolean_t fuids_ok;
32010a586ceaSMark Shellenbaum boolean_t sa_ok;
32020a48a24eStimh uint64_t zplver = ZPL_VERSION;
32030a48a24eStimh int error;
32040a48a24eStimh
32050a586ceaSMark Shellenbaum zplver = zfs_zpl_version_map(spa_vers);
32060a586ceaSMark Shellenbaum fuids_ok = (zplver >= ZPL_VERSION_FUID);
32070a586ceaSMark Shellenbaum sa_ok = (zplver >= ZPL_VERSION_SA);
32080a48a24eStimh
32090a586ceaSMark Shellenbaum error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
32100a586ceaSMark Shellenbaum createprops, zplprops, is_ci);
32110a48a24eStimh return (error);
32120a48a24eStimh }
32130a48a24eStimh
32143cb34c60Sahrens /*
32154445fffbSMatthew Ahrens * innvl: {
32164445fffbSMatthew Ahrens * "type" -> dmu_objset_type_t (int32)
32174445fffbSMatthew Ahrens * (optional) "props" -> { prop -> value }
32184445fffbSMatthew Ahrens * }
32193cb34c60Sahrens *
32204445fffbSMatthew Ahrens * outnvl: propname -> error code (int32)
32213cb34c60Sahrens */
3222fa9e4066Sahrens static int
zfs_ioc_create(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)32234445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3224fa9e4066Sahrens {
3225fa9e4066Sahrens int error = 0;
32264445fffbSMatthew Ahrens zfs_creat_t zct = { 0 };
3227ecd6cf80Smarks nvlist_t *nvprops = NULL;
3228ecd6cf80Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
32294445fffbSMatthew Ahrens int32_t type32;
32304445fffbSMatthew Ahrens dmu_objset_type_t type;
32314445fffbSMatthew Ahrens boolean_t is_insensitive = B_FALSE;
32324445fffbSMatthew Ahrens
32334445fffbSMatthew Ahrens if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3234be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
32354445fffbSMatthew Ahrens type = type32;
32364445fffbSMatthew Ahrens (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3237fa9e4066Sahrens
3238fa9e4066Sahrens switch (type) {
3239fa9e4066Sahrens case DMU_OST_ZFS:
3240fa9e4066Sahrens cbfunc = zfs_create_cb;
3241fa9e4066Sahrens break;
3242fa9e4066Sahrens
3243fa9e4066Sahrens case DMU_OST_ZVOL:
3244fa9e4066Sahrens cbfunc = zvol_create_cb;
3245fa9e4066Sahrens break;
3246fa9e4066Sahrens
3247fa9e4066Sahrens default:
32481d452cf5Sahrens cbfunc = NULL;
3249e7cbe64fSgw25295 break;
3250fa9e4066Sahrens }
32514445fffbSMatthew Ahrens if (strchr(fsname, '@') ||
32524445fffbSMatthew Ahrens strchr(fsname, '%'))
3253be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
3254fa9e4066Sahrens
3255da6c28aaSamw zct.zct_props = nvprops;
3256da6c28aaSamw
32574445fffbSMatthew Ahrens if (cbfunc == NULL)
3258be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
3259e9dbad6fSeschrock
3260fa9e4066Sahrens if (type == DMU_OST_ZVOL) {
3261e9dbad6fSeschrock uint64_t volsize, volblocksize;
32625c5460e9Seschrock
32634445fffbSMatthew Ahrens if (nvprops == NULL)
3264be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
32654445fffbSMatthew Ahrens if (nvlist_lookup_uint64(nvprops,
32664445fffbSMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3267be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
32685c5460e9Seschrock
3269ecd6cf80Smarks if ((error = nvlist_lookup_uint64(nvprops,
3270e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
32714445fffbSMatthew Ahrens &volblocksize)) != 0 && error != ENOENT)
3272be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
3273e9dbad6fSeschrock
3274e9dbad6fSeschrock if (error != 0)
3275e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric(
3276e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE);
3277e9dbad6fSeschrock
3278e9dbad6fSeschrock if ((error = zvol_check_volblocksize(
3279e9dbad6fSeschrock volblocksize)) != 0 ||
3280e9dbad6fSeschrock (error = zvol_check_volsize(volsize,
32814445fffbSMatthew Ahrens volblocksize)) != 0)
32825c5460e9Seschrock return (error);
3283e7437265Sahrens } else if (type == DMU_OST_ZFS) {
3284da6c28aaSamw int error;
3285e7437265Sahrens
3286de8267e0Stimh /*
3287da6c28aaSamw * We have to have normalization and
3288da6c28aaSamw * case-folding flags correct when we do the
3289da6c28aaSamw * file system creation, so go figure them out
3290de8267e0Stimh * now.
3291da6c28aaSamw */
3292de8267e0Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops,
3293de8267e0Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0);
32944445fffbSMatthew Ahrens error = zfs_fill_zplprops(fsname, nvprops,
32950a48a24eStimh zct.zct_zplprops, &is_insensitive);
3296da6c28aaSamw if (error != 0) {
3297de8267e0Stimh nvlist_free(zct.zct_zplprops);
3298da6c28aaSamw return (error);
3299da6c28aaSamw }
3300da6c28aaSamw }
33014445fffbSMatthew Ahrens
33024445fffbSMatthew Ahrens error = dmu_objset_create(fsname, type,
3303ab04eb8eStimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3304de8267e0Stimh nvlist_free(zct.zct_zplprops);
3305e9dbad6fSeschrock
3306e9dbad6fSeschrock /*
3307e9dbad6fSeschrock * It would be nice to do this atomically.
3308e9dbad6fSeschrock */
3309e9dbad6fSeschrock if (error == 0) {
33104445fffbSMatthew Ahrens error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33114445fffbSMatthew Ahrens nvprops, outnvl);
331292241e0bSTom Erickson if (error != 0)
33133b2aab18SMatthew Ahrens (void) dsl_destroy_head(fsname);
3314e9dbad6fSeschrock }
3315fa9e4066Sahrens return (error);
3316fa9e4066Sahrens }
3317fa9e4066Sahrens
33183cb34c60Sahrens /*
33194445fffbSMatthew Ahrens * innvl: {
33204445fffbSMatthew Ahrens * "origin" -> name of origin snapshot
33214445fffbSMatthew Ahrens * (optional) "props" -> { prop -> value }
33224445fffbSMatthew Ahrens * }
33233cb34c60Sahrens *
33244445fffbSMatthew Ahrens * outnvl: propname -> error code (int32)
33253cb34c60Sahrens */
3326fa9e4066Sahrens static int
zfs_ioc_clone(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)33274445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3328fa9e4066Sahrens {
33294445fffbSMatthew Ahrens int error = 0;
3330bb0ade09Sahrens nvlist_t *nvprops = NULL;
33314445fffbSMatthew Ahrens char *origin_name;
3332bb0ade09Sahrens
33334445fffbSMatthew Ahrens if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3334be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
33354445fffbSMatthew Ahrens (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
33364445fffbSMatthew Ahrens
33374445fffbSMatthew Ahrens if (strchr(fsname, '@') ||
33384445fffbSMatthew Ahrens strchr(fsname, '%'))
3339be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
3340bb0ade09Sahrens
334123962479SMarcel Telka if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3342be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
33433b2aab18SMatthew Ahrens error = dmu_objset_clone(fsname, origin_name);
33443b2aab18SMatthew Ahrens if (error != 0)
33454445fffbSMatthew Ahrens return (error);
33464445fffbSMatthew Ahrens
33474445fffbSMatthew Ahrens /*
33484445fffbSMatthew Ahrens * It would be nice to do this atomically.
33494445fffbSMatthew Ahrens */
33504445fffbSMatthew Ahrens if (error == 0) {
33514445fffbSMatthew Ahrens error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33524445fffbSMatthew Ahrens nvprops, outnvl);
33534445fffbSMatthew Ahrens if (error != 0)
33543b2aab18SMatthew Ahrens (void) dsl_destroy_head(fsname);
3355bb0ade09Sahrens }
3356bb0ade09Sahrens return (error);
33571d452cf5Sahrens }
33581d452cf5Sahrens
33594445fffbSMatthew Ahrens /*
33604445fffbSMatthew Ahrens * innvl: {
33614445fffbSMatthew Ahrens * "snaps" -> { snapshot1, snapshot2 }
33624445fffbSMatthew Ahrens * (optional) "props" -> { prop -> value (string) }
33634445fffbSMatthew Ahrens * }
33644445fffbSMatthew Ahrens *
33654445fffbSMatthew Ahrens * outnvl: snapshot -> error code (int32)
33664445fffbSMatthew Ahrens */
33674445fffbSMatthew Ahrens static int
zfs_ioc_snapshot(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)33684445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
33694445fffbSMatthew Ahrens {
33704445fffbSMatthew Ahrens nvlist_t *snaps;
33714445fffbSMatthew Ahrens nvlist_t *props = NULL;
33724445fffbSMatthew Ahrens int error, poollen;
33734445fffbSMatthew Ahrens nvpair_t *pair;
33744445fffbSMatthew Ahrens
33754445fffbSMatthew Ahrens (void) nvlist_lookup_nvlist(innvl, "props", &props);
33764445fffbSMatthew Ahrens if ((error = zfs_check_userprops(poolname, props)) != 0)
33774445fffbSMatthew Ahrens return (error);
33784445fffbSMatthew Ahrens
33794445fffbSMatthew Ahrens if (!nvlist_empty(props) &&
33804445fffbSMatthew Ahrens zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3381be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
33824445fffbSMatthew Ahrens
33834445fffbSMatthew Ahrens if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3384be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
33854445fffbSMatthew Ahrens poollen = strlen(poolname);
33864445fffbSMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
33874445fffbSMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) {
33884445fffbSMatthew Ahrens const char *name = nvpair_name(pair);
33894445fffbSMatthew Ahrens const char *cp = strchr(name, '@');
33904445fffbSMatthew Ahrens
33914445fffbSMatthew Ahrens /*
33924445fffbSMatthew Ahrens * The snap name must contain an @, and the part after it must
33934445fffbSMatthew Ahrens * contain only valid characters.
33944445fffbSMatthew Ahrens */
339578f17100SMatthew Ahrens if (cp == NULL ||
339678f17100SMatthew Ahrens zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3397be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
33984445fffbSMatthew Ahrens
33994445fffbSMatthew Ahrens /*
34004445fffbSMatthew Ahrens * The snap must be in the specified pool.
34014445fffbSMatthew Ahrens */
34024445fffbSMatthew Ahrens if (strncmp(name, poolname, poollen) != 0 ||
34034445fffbSMatthew Ahrens (name[poollen] != '/' && name[poollen] != '@'))
3404be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV));
34054445fffbSMatthew Ahrens
34064445fffbSMatthew Ahrens /* This must be the only snap of this fs. */
34074445fffbSMatthew Ahrens for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
34084445fffbSMatthew Ahrens pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
34094445fffbSMatthew Ahrens if (strncmp(name, nvpair_name(pair2), cp - name + 1)
34104445fffbSMatthew Ahrens == 0) {
3411be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV));
34124445fffbSMatthew Ahrens }
34134445fffbSMatthew Ahrens }
34144445fffbSMatthew Ahrens }
34154445fffbSMatthew Ahrens
34163b2aab18SMatthew Ahrens error = dsl_dataset_snapshot(snaps, props, outnvl);
34174445fffbSMatthew Ahrens return (error);
34184445fffbSMatthew Ahrens }
34194445fffbSMatthew Ahrens
34204445fffbSMatthew Ahrens /*
34214445fffbSMatthew Ahrens * innvl: "message" -> string
34224445fffbSMatthew Ahrens */
34234445fffbSMatthew Ahrens /* ARGSUSED */
34244445fffbSMatthew Ahrens static int
zfs_ioc_log_history(const char * unused,nvlist_t * innvl,nvlist_t * outnvl)34254445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
34264445fffbSMatthew Ahrens {
34274445fffbSMatthew Ahrens char *message;
34284445fffbSMatthew Ahrens spa_t *spa;
34294445fffbSMatthew Ahrens int error;
34304445fffbSMatthew Ahrens char *poolname;
34314445fffbSMatthew Ahrens
34324445fffbSMatthew Ahrens /*
34334445fffbSMatthew Ahrens * The poolname in the ioctl is not set, we get it from the TSD,
34344445fffbSMatthew Ahrens * which was set at the end of the last successful ioctl that allows
34354445fffbSMatthew Ahrens * logging. The secpolicy func already checked that it is set.
34364445fffbSMatthew Ahrens * Only one log ioctl is allowed after each successful ioctl, so
34374445fffbSMatthew Ahrens * we clear the TSD here.
34384445fffbSMatthew Ahrens */
34394445fffbSMatthew Ahrens poolname = tsd_get(zfs_allow_log_key);
34404445fffbSMatthew Ahrens (void) tsd_set(zfs_allow_log_key, NULL);
34414445fffbSMatthew Ahrens error = spa_open(poolname, &spa, FTAG);
34424445fffbSMatthew Ahrens strfree(poolname);
34434445fffbSMatthew Ahrens if (error != 0)
34444445fffbSMatthew Ahrens return (error);
34454445fffbSMatthew Ahrens
34464445fffbSMatthew Ahrens if (nvlist_lookup_string(innvl, "message", &message) != 0) {
34474445fffbSMatthew Ahrens spa_close(spa, FTAG);
3448be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
34494445fffbSMatthew Ahrens }
34504445fffbSMatthew Ahrens
34514445fffbSMatthew Ahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
34524445fffbSMatthew Ahrens spa_close(spa, FTAG);
3453be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
34544445fffbSMatthew Ahrens }
34554445fffbSMatthew Ahrens
34564445fffbSMatthew Ahrens error = spa_history_log(spa, message);
34574445fffbSMatthew Ahrens spa_close(spa, FTAG);
34584445fffbSMatthew Ahrens return (error);
34594445fffbSMatthew Ahrens }
34604445fffbSMatthew Ahrens
34613b2aab18SMatthew Ahrens /*
34623b2aab18SMatthew Ahrens * The dp_config_rwlock must not be held when calling this, because the
34633b2aab18SMatthew Ahrens * unmount may need to write out data.
34643b2aab18SMatthew Ahrens *
34653b2aab18SMatthew Ahrens * This function is best-effort. Callers must deal gracefully if it
34663b2aab18SMatthew Ahrens * remains mounted (or is remounted after this call).
3467fc7a6e3fSWill Andrews *
3468fc7a6e3fSWill Andrews * Returns 0 if the argument is not a snapshot, or it is not currently a
3469fc7a6e3fSWill Andrews * filesystem, or we were able to unmount it. Returns error code otherwise.
34703b2aab18SMatthew Ahrens */
3471fc7a6e3fSWill Andrews int
zfs_unmount_snap(const char * snapname)34723b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
34731d452cf5Sahrens {
34744445fffbSMatthew Ahrens vfs_t *vfsp;
34753b2aab18SMatthew Ahrens zfsvfs_t *zfsvfs;
3476fc7a6e3fSWill Andrews int err;
3477fa9e4066Sahrens
34783b2aab18SMatthew Ahrens if (strchr(snapname, '@') == NULL)
3479fc7a6e3fSWill Andrews return (0);
34804445fffbSMatthew Ahrens
34813b2aab18SMatthew Ahrens vfsp = zfs_get_vfs(snapname);
34824445fffbSMatthew Ahrens if (vfsp == NULL)
3483fc7a6e3fSWill Andrews return (0);
34844445fffbSMatthew Ahrens
34853b2aab18SMatthew Ahrens zfsvfs = vfsp->vfs_data;
34863b2aab18SMatthew Ahrens ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
34873b2aab18SMatthew Ahrens
3488fc7a6e3fSWill Andrews err = vn_vfswlock(vfsp->vfs_vnodecovered);
3489fa9e4066Sahrens VFS_RELE(vfsp);
3490fc7a6e3fSWill Andrews if (err != 0)
3491fc7a6e3fSWill Andrews return (SET_ERROR(err));
34924445fffbSMatthew Ahrens
34934445fffbSMatthew Ahrens /*
34944445fffbSMatthew Ahrens * Always force the unmount for snapshots.
34954445fffbSMatthew Ahrens */
34963b2aab18SMatthew Ahrens (void) dounmount(vfsp, MS_FORCE, kcred);
3497fc7a6e3fSWill Andrews return (0);
34983b2aab18SMatthew Ahrens }
34993b2aab18SMatthew Ahrens
35003b2aab18SMatthew Ahrens /* ARGSUSED */
35013b2aab18SMatthew Ahrens static int
zfs_unmount_snap_cb(const char * snapname,void * arg)35023b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
35033b2aab18SMatthew Ahrens {
3504fc7a6e3fSWill Andrews return (zfs_unmount_snap(snapname));
35053b2aab18SMatthew Ahrens }
35063b2aab18SMatthew Ahrens
35073b2aab18SMatthew Ahrens /*
35083b2aab18SMatthew Ahrens * When a clone is destroyed, its origin may also need to be destroyed,
35093b2aab18SMatthew Ahrens * in which case it must be unmounted. This routine will do that unmount
35103b2aab18SMatthew Ahrens * if necessary.
35113b2aab18SMatthew Ahrens */
35123b2aab18SMatthew Ahrens void
zfs_destroy_unmount_origin(const char * fsname)35133b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
35143b2aab18SMatthew Ahrens {
35153b2aab18SMatthew Ahrens int error;
35163b2aab18SMatthew Ahrens objset_t *os;
35173b2aab18SMatthew Ahrens dsl_dataset_t *ds;
35183b2aab18SMatthew Ahrens
35193b2aab18SMatthew Ahrens error = dmu_objset_hold(fsname, FTAG, &os);
35203b2aab18SMatthew Ahrens if (error != 0)
35213b2aab18SMatthew Ahrens return;
35223b2aab18SMatthew Ahrens ds = dmu_objset_ds(os);
35233b2aab18SMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3524*40a5c998SMatthew Ahrens char originname[ZFS_MAX_DATASET_NAME_LEN];
35253b2aab18SMatthew Ahrens dsl_dataset_name(ds->ds_prev, originname);
35263b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG);
3527fc7a6e3fSWill Andrews (void) zfs_unmount_snap(originname);
35283b2aab18SMatthew Ahrens } else {
35293b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG);
35303b2aab18SMatthew Ahrens }
35311d452cf5Sahrens }
35321d452cf5Sahrens
35333cb34c60Sahrens /*
35344445fffbSMatthew Ahrens * innvl: {
35354445fffbSMatthew Ahrens * "snaps" -> { snapshot1, snapshot2 }
35364445fffbSMatthew Ahrens * (optional boolean) "defer"
35374445fffbSMatthew Ahrens * }
35383cb34c60Sahrens *
35394445fffbSMatthew Ahrens * outnvl: snapshot -> error code (int32)
35404445fffbSMatthew Ahrens *
35413cb34c60Sahrens */
354278f17100SMatthew Ahrens /* ARGSUSED */
35431d452cf5Sahrens static int
zfs_ioc_destroy_snaps(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)35444445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
35451d452cf5Sahrens {
35464445fffbSMatthew Ahrens nvlist_t *snaps;
354719b94df9SMatthew Ahrens nvpair_t *pair;
35484445fffbSMatthew Ahrens boolean_t defer;
35491d452cf5Sahrens
35504445fffbSMatthew Ahrens if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3551be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
35524445fffbSMatthew Ahrens defer = nvlist_exists(innvl, "defer");
355319b94df9SMatthew Ahrens
35544445fffbSMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
35554445fffbSMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) {
355678f17100SMatthew Ahrens (void) zfs_unmount_snap(nvpair_name(pair));
355719b94df9SMatthew Ahrens }
355819b94df9SMatthew Ahrens
35593b2aab18SMatthew Ahrens return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
35601d452cf5Sahrens }
35611d452cf5Sahrens
35623cb34c60Sahrens /*
356378f17100SMatthew Ahrens * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
356478f17100SMatthew Ahrens * All bookmarks must be in the same pool.
356578f17100SMatthew Ahrens *
356678f17100SMatthew Ahrens * innvl: {
356778f17100SMatthew Ahrens * bookmark1 -> snapshot1, bookmark2 -> snapshot2
356878f17100SMatthew Ahrens * }
356978f17100SMatthew Ahrens *
357078f17100SMatthew Ahrens * outnvl: bookmark -> error code (int32)
357178f17100SMatthew Ahrens *
357278f17100SMatthew Ahrens */
357378f17100SMatthew Ahrens /* ARGSUSED */
357478f17100SMatthew Ahrens static int
zfs_ioc_bookmark(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)357578f17100SMatthew Ahrens zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
357678f17100SMatthew Ahrens {
357778f17100SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
357878f17100SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
357978f17100SMatthew Ahrens char *snap_name;
358078f17100SMatthew Ahrens
358178f17100SMatthew Ahrens /*
358278f17100SMatthew Ahrens * Verify the snapshot argument.
358378f17100SMatthew Ahrens */
358478f17100SMatthew Ahrens if (nvpair_value_string(pair, &snap_name) != 0)
358578f17100SMatthew Ahrens return (SET_ERROR(EINVAL));
358678f17100SMatthew Ahrens
358778f17100SMatthew Ahrens
358878f17100SMatthew Ahrens /* Verify that the keys (bookmarks) are unique */
358978f17100SMatthew Ahrens for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
359078f17100SMatthew Ahrens pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
359178f17100SMatthew Ahrens if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
359278f17100SMatthew Ahrens return (SET_ERROR(EINVAL));
359378f17100SMatthew Ahrens }
359478f17100SMatthew Ahrens }
359578f17100SMatthew Ahrens
359678f17100SMatthew Ahrens return (dsl_bookmark_create(innvl, outnvl));
359778f17100SMatthew Ahrens }
359878f17100SMatthew Ahrens
359978f17100SMatthew Ahrens /*
360078f17100SMatthew Ahrens * innvl: {
360178f17100SMatthew Ahrens * property 1, property 2, ...
360278f17100SMatthew Ahrens * }
360378f17100SMatthew Ahrens *
360478f17100SMatthew Ahrens * outnvl: {
360578f17100SMatthew Ahrens * bookmark name 1 -> { property 1, property 2, ... },
360678f17100SMatthew Ahrens * bookmark name 2 -> { property 1, property 2, ... }
360778f17100SMatthew Ahrens * }
360878f17100SMatthew Ahrens *
360978f17100SMatthew Ahrens */
361078f17100SMatthew Ahrens static int
zfs_ioc_get_bookmarks(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)361178f17100SMatthew Ahrens zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
361278f17100SMatthew Ahrens {
361378f17100SMatthew Ahrens return (dsl_get_bookmarks(fsname, innvl, outnvl));
361478f17100SMatthew Ahrens }
361578f17100SMatthew Ahrens
361678f17100SMatthew Ahrens /*
361778f17100SMatthew Ahrens * innvl: {
361878f17100SMatthew Ahrens * bookmark name 1, bookmark name 2
361978f17100SMatthew Ahrens * }
362078f17100SMatthew Ahrens *
362178f17100SMatthew Ahrens * outnvl: bookmark -> error code (int32)
362278f17100SMatthew Ahrens *
362378f17100SMatthew Ahrens */
362478f17100SMatthew Ahrens static int
zfs_ioc_destroy_bookmarks(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)362578f17100SMatthew Ahrens zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
362678f17100SMatthew Ahrens nvlist_t *outnvl)
362778f17100SMatthew Ahrens {
362878f17100SMatthew Ahrens int error, poollen;
362978f17100SMatthew Ahrens
363078f17100SMatthew Ahrens poollen = strlen(poolname);
363178f17100SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
363278f17100SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
363378f17100SMatthew Ahrens const char *name = nvpair_name(pair);
363478f17100SMatthew Ahrens const char *cp = strchr(name, '#');
363578f17100SMatthew Ahrens
363678f17100SMatthew Ahrens /*
363778f17100SMatthew Ahrens * The bookmark name must contain an #, and the part after it
363878f17100SMatthew Ahrens * must contain only valid characters.
363978f17100SMatthew Ahrens */
364078f17100SMatthew Ahrens if (cp == NULL ||
364178f17100SMatthew Ahrens zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
364278f17100SMatthew Ahrens return (SET_ERROR(EINVAL));
364378f17100SMatthew Ahrens
364478f17100SMatthew Ahrens /*
364578f17100SMatthew Ahrens * The bookmark must be in the specified pool.
364678f17100SMatthew Ahrens */
364778f17100SMatthew Ahrens if (strncmp(name, poolname, poollen) != 0 ||
364878f17100SMatthew Ahrens (name[poollen] != '/' && name[poollen] != '#'))
364978f17100SMatthew Ahrens return (SET_ERROR(EXDEV));
365078f17100SMatthew Ahrens }
365178f17100SMatthew Ahrens
365278f17100SMatthew Ahrens error = dsl_bookmark_destroy(innvl, outnvl);
365378f17100SMatthew Ahrens return (error);
365478f17100SMatthew Ahrens }
365578f17100SMatthew Ahrens
365678f17100SMatthew Ahrens /*
36573cb34c60Sahrens * inputs:
36583cb34c60Sahrens * zc_name name of dataset to destroy
36593cb34c60Sahrens * zc_objset_type type of objset
3660842727c2SChris Kirby * zc_defer_destroy mark for deferred destroy
36613cb34c60Sahrens *
36623cb34c60Sahrens * outputs: none
36633cb34c60Sahrens */
36641d452cf5Sahrens static int
zfs_ioc_destroy(zfs_cmd_t * zc)36651d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
36661d452cf5Sahrens {
3667681d9761SEric Taylor int err;
3668fc7a6e3fSWill Andrews
3669fc7a6e3fSWill Andrews if (zc->zc_objset_type == DMU_OST_ZFS) {
3670fc7a6e3fSWill Andrews err = zfs_unmount_snap(zc->zc_name);
3671fc7a6e3fSWill Andrews if (err != 0)
3672fc7a6e3fSWill Andrews return (err);
3673fc7a6e3fSWill Andrews }
3674fa9e4066Sahrens
36753b2aab18SMatthew Ahrens if (strchr(zc->zc_name, '@'))
36763b2aab18SMatthew Ahrens err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
36773b2aab18SMatthew Ahrens else
36783b2aab18SMatthew Ahrens err = dsl_destroy_head(zc->zc_name);
3679681d9761SEric Taylor if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
36805c987a37SChris Kirby (void) zvol_remove_minor(zc->zc_name);
3681681d9761SEric Taylor return (err);
3682fa9e4066Sahrens }
3683fa9e4066Sahrens
36843cb34c60Sahrens /*
3685a7027df1SMatthew Ahrens * fsname is name of dataset to rollback (to most recent snapshot)
36863cb34c60Sahrens *
3687a7027df1SMatthew Ahrens * innvl is not used.
3688a7027df1SMatthew Ahrens *
3689a7027df1SMatthew Ahrens * outnvl: "target" -> name of most recent snapshot
3690a7027df1SMatthew Ahrens * }
36913cb34c60Sahrens */
3692a7027df1SMatthew Ahrens /* ARGSUSED */
3693fa9e4066Sahrens static int
zfs_ioc_rollback(const char * fsname,nvlist_t * args,nvlist_t * outnvl)3694a7027df1SMatthew Ahrens zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3695fa9e4066Sahrens {
3696ae46e4c7SMatthew Ahrens zfsvfs_t *zfsvfs;
36973b2aab18SMatthew Ahrens int error;
36984ccbb6e7Sahrens
3699a7027df1SMatthew Ahrens if (getzfsvfs(fsname, &zfsvfs) == 0) {
3700503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs);
370147f263f4Sek110237 if (error == 0) {
370247f263f4Sek110237 int resume_err;
370347f263f4Sek110237
3704a7027df1SMatthew Ahrens error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3705a7027df1SMatthew Ahrens resume_err = zfs_resume_fs(zfsvfs, fsname);
370647f263f4Sek110237 error = error ? error : resume_err;
370747f263f4Sek110237 }
37084ccbb6e7Sahrens VFS_RELE(zfsvfs->z_vfs);
37094ccbb6e7Sahrens } else {
3710a7027df1SMatthew Ahrens error = dsl_dataset_rollback(fsname, NULL, outnvl);
37114ccbb6e7Sahrens }
37124ccbb6e7Sahrens return (error);
3713fa9e4066Sahrens }
3714fa9e4066Sahrens
37153b2aab18SMatthew Ahrens static int
recursive_unmount(const char * fsname,void * arg)37163b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
37173b2aab18SMatthew Ahrens {
37183b2aab18SMatthew Ahrens const char *snapname = arg;
3719*40a5c998SMatthew Ahrens char fullname[ZFS_MAX_DATASET_NAME_LEN];
37203b2aab18SMatthew Ahrens
37213b2aab18SMatthew Ahrens (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3722fc7a6e3fSWill Andrews return (zfs_unmount_snap(fullname));
37233b2aab18SMatthew Ahrens }
37243b2aab18SMatthew Ahrens
37253cb34c60Sahrens /*
37263cb34c60Sahrens * inputs:
37273cb34c60Sahrens * zc_name old name of dataset
37283cb34c60Sahrens * zc_value new name of dataset
37293cb34c60Sahrens * zc_cookie recursive flag (only valid for snapshots)
37303cb34c60Sahrens *
37313cb34c60Sahrens * outputs: none
37323cb34c60Sahrens */
3733fa9e4066Sahrens static int
zfs_ioc_rename(zfs_cmd_t * zc)3734fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3735fa9e4066Sahrens {
37367f1f55eaSvb160487 boolean_t recursive = zc->zc_cookie & 1;
37373b2aab18SMatthew Ahrens char *at;
3738cdf5b4caSmmusante
3739e9dbad6fSeschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3740f18faf3fSek110237 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
374123962479SMarcel Telka strchr(zc->zc_value, '%'))
3742be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
3743fa9e4066Sahrens
37443b2aab18SMatthew Ahrens at = strchr(zc->zc_name, '@');
37453b2aab18SMatthew Ahrens if (at != NULL) {
37463b2aab18SMatthew Ahrens /* snaps must be in same fs */
3747a0c1127bSSteven Hartland int error;
3748a0c1127bSSteven Hartland
37493b2aab18SMatthew Ahrens if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3750be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV));
37513b2aab18SMatthew Ahrens *at = '\0';
37523b2aab18SMatthew Ahrens if (zc->zc_objset_type == DMU_OST_ZFS) {
3753a0c1127bSSteven Hartland error = dmu_objset_find(zc->zc_name,
37543b2aab18SMatthew Ahrens recursive_unmount, at + 1,
37553b2aab18SMatthew Ahrens recursive ? DS_FIND_CHILDREN : 0);
3756a0c1127bSSteven Hartland if (error != 0) {
3757a0c1127bSSteven Hartland *at = '@';
37583b2aab18SMatthew Ahrens return (error);
3759fa9e4066Sahrens }
3760a0c1127bSSteven Hartland }
3761a0c1127bSSteven Hartland error = dsl_dataset_rename_snapshot(zc->zc_name,
3762a0c1127bSSteven Hartland at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3763a0c1127bSSteven Hartland *at = '@';
3764a0c1127bSSteven Hartland
3765a0c1127bSSteven Hartland return (error);
37663b2aab18SMatthew Ahrens } else {
3767681d9761SEric Taylor if (zc->zc_objset_type == DMU_OST_ZVOL)
3768681d9761SEric Taylor (void) zvol_remove_minor(zc->zc_name);
37693b2aab18SMatthew Ahrens return (dsl_dir_rename(zc->zc_name, zc->zc_value));
37703b2aab18SMatthew Ahrens }
3771fa9e4066Sahrens }
3772fa9e4066Sahrens
377392241e0bSTom Erickson static int
zfs_check_settable(const char * dsname,nvpair_t * pair,cred_t * cr)377492241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
377592241e0bSTom Erickson {
377692241e0bSTom Erickson const char *propname = nvpair_name(pair);
377792241e0bSTom Erickson boolean_t issnap = (strchr(dsname, '@') != NULL);
377892241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname);
377992241e0bSTom Erickson uint64_t intval;
378092241e0bSTom Erickson int err;
378192241e0bSTom Erickson
378292241e0bSTom Erickson if (prop == ZPROP_INVAL) {
378392241e0bSTom Erickson if (zfs_prop_user(propname)) {
378492241e0bSTom Erickson if (err = zfs_secpolicy_write_perms(dsname,
378592241e0bSTom Erickson ZFS_DELEG_PERM_USERPROP, cr))
378692241e0bSTom Erickson return (err);
378792241e0bSTom Erickson return (0);
378892241e0bSTom Erickson }
378992241e0bSTom Erickson
379092241e0bSTom Erickson if (!issnap && zfs_prop_userquota(propname)) {
379192241e0bSTom Erickson const char *perm = NULL;
379292241e0bSTom Erickson const char *uq_prefix =
379392241e0bSTom Erickson zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
379492241e0bSTom Erickson const char *gq_prefix =
379592241e0bSTom Erickson zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
379692241e0bSTom Erickson
379792241e0bSTom Erickson if (strncmp(propname, uq_prefix,
379892241e0bSTom Erickson strlen(uq_prefix)) == 0) {
379992241e0bSTom Erickson perm = ZFS_DELEG_PERM_USERQUOTA;
380092241e0bSTom Erickson } else if (strncmp(propname, gq_prefix,
380192241e0bSTom Erickson strlen(gq_prefix)) == 0) {
380292241e0bSTom Erickson perm = ZFS_DELEG_PERM_GROUPQUOTA;
380392241e0bSTom Erickson } else {
380492241e0bSTom Erickson /* USERUSED and GROUPUSED are read-only */
3805be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
380692241e0bSTom Erickson }
380792241e0bSTom Erickson
380892241e0bSTom Erickson if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
380992241e0bSTom Erickson return (err);
381092241e0bSTom Erickson return (0);
381192241e0bSTom Erickson }
381292241e0bSTom Erickson
3813be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
381492241e0bSTom Erickson }
381592241e0bSTom Erickson
381692241e0bSTom Erickson if (issnap)
3817be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
381892241e0bSTom Erickson
381992241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
382092241e0bSTom Erickson /*
382192241e0bSTom Erickson * dsl_prop_get_all_impl() returns properties in this
382292241e0bSTom Erickson * format.
382392241e0bSTom Erickson */
382492241e0bSTom Erickson nvlist_t *attrs;
382592241e0bSTom Erickson VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
382692241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
382792241e0bSTom Erickson &pair) == 0);
382892241e0bSTom Erickson }
382992241e0bSTom Erickson
383092241e0bSTom Erickson /*
383192241e0bSTom Erickson * Check that this value is valid for this pool version
383292241e0bSTom Erickson */
383392241e0bSTom Erickson switch (prop) {
383492241e0bSTom Erickson case ZFS_PROP_COMPRESSION:
383592241e0bSTom Erickson /*
383692241e0bSTom Erickson * If the user specified gzip compression, make sure
383792241e0bSTom Erickson * the SPA supports it. We ignore any errors here since
383892241e0bSTom Erickson * we'll catch them later.
383992241e0bSTom Erickson */
3840b5152584SMatthew Ahrens if (nvpair_value_uint64(pair, &intval) == 0) {
384192241e0bSTom Erickson if (intval >= ZIO_COMPRESS_GZIP_1 &&
384292241e0bSTom Erickson intval <= ZIO_COMPRESS_GZIP_9 &&
384392241e0bSTom Erickson zfs_earlier_version(dsname,
384492241e0bSTom Erickson SPA_VERSION_GZIP_COMPRESSION)) {
3845be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
384692241e0bSTom Erickson }
384792241e0bSTom Erickson
384892241e0bSTom Erickson if (intval == ZIO_COMPRESS_ZLE &&
384992241e0bSTom Erickson zfs_earlier_version(dsname,
385092241e0bSTom Erickson SPA_VERSION_ZLE_COMPRESSION))
3851be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
385292241e0bSTom Erickson
3853a6f561b4SSašo Kiselkov if (intval == ZIO_COMPRESS_LZ4) {
3854a6f561b4SSašo Kiselkov spa_t *spa;
3855a6f561b4SSašo Kiselkov
3856a6f561b4SSašo Kiselkov if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3857a6f561b4SSašo Kiselkov return (err);
3858a6f561b4SSašo Kiselkov
38592acef22dSMatthew Ahrens if (!spa_feature_is_enabled(spa,
38602acef22dSMatthew Ahrens SPA_FEATURE_LZ4_COMPRESS)) {
3861a6f561b4SSašo Kiselkov spa_close(spa, FTAG);
3862be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
3863a6f561b4SSašo Kiselkov }
3864a6f561b4SSašo Kiselkov spa_close(spa, FTAG);
3865a6f561b4SSašo Kiselkov }
3866a6f561b4SSašo Kiselkov
386792241e0bSTom Erickson /*
386892241e0bSTom Erickson * If this is a bootable dataset then
386992241e0bSTom Erickson * verify that the compression algorithm
387092241e0bSTom Erickson * is supported for booting. We must return
387192241e0bSTom Erickson * something other than ENOTSUP since it
387292241e0bSTom Erickson * implies a downrev pool version.
387392241e0bSTom Erickson */
387492241e0bSTom Erickson if (zfs_is_bootfs(dsname) &&
387592241e0bSTom Erickson !BOOTFS_COMPRESS_VALID(intval)) {
3876be6fd75aSMatthew Ahrens return (SET_ERROR(ERANGE));
387792241e0bSTom Erickson }
387892241e0bSTom Erickson }
387992241e0bSTom Erickson break;
388092241e0bSTom Erickson
388192241e0bSTom Erickson case ZFS_PROP_COPIES:
388292241e0bSTom Erickson if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3883be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
388492241e0bSTom Erickson break;
388592241e0bSTom Erickson
3886b5152584SMatthew Ahrens case ZFS_PROP_RECORDSIZE:
3887b5152584SMatthew Ahrens /* Record sizes above 128k need the feature to be enabled */
3888b5152584SMatthew Ahrens if (nvpair_value_uint64(pair, &intval) == 0 &&
3889b5152584SMatthew Ahrens intval > SPA_OLD_MAXBLOCKSIZE) {
3890b5152584SMatthew Ahrens spa_t *spa;
3891b5152584SMatthew Ahrens
3892b5152584SMatthew Ahrens /*
3893b5152584SMatthew Ahrens * If this is a bootable dataset then
3894b5152584SMatthew Ahrens * the we don't allow large (>128K) blocks,
3895b5152584SMatthew Ahrens * because GRUB doesn't support them.
3896b5152584SMatthew Ahrens */
3897b5152584SMatthew Ahrens if (zfs_is_bootfs(dsname) &&
3898b5152584SMatthew Ahrens intval > SPA_OLD_MAXBLOCKSIZE) {
38996de9bb56SMatthew Ahrens return (SET_ERROR(ERANGE));
3900b5152584SMatthew Ahrens }
3901b5152584SMatthew Ahrens
3902b5152584SMatthew Ahrens /*
3903b5152584SMatthew Ahrens * We don't allow setting the property above 1MB,
3904b5152584SMatthew Ahrens * unless the tunable has been changed.
3905b5152584SMatthew Ahrens */
3906b5152584SMatthew Ahrens if (intval > zfs_max_recordsize ||
3907b5152584SMatthew Ahrens intval > SPA_MAXBLOCKSIZE)
39086de9bb56SMatthew Ahrens return (SET_ERROR(ERANGE));
3909b5152584SMatthew Ahrens
3910b5152584SMatthew Ahrens if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3911b5152584SMatthew Ahrens return (err);
3912b5152584SMatthew Ahrens
3913b5152584SMatthew Ahrens if (!spa_feature_is_enabled(spa,
3914b5152584SMatthew Ahrens SPA_FEATURE_LARGE_BLOCKS)) {
3915b5152584SMatthew Ahrens spa_close(spa, FTAG);
3916b5152584SMatthew Ahrens return (SET_ERROR(ENOTSUP));
3917b5152584SMatthew Ahrens }
3918b5152584SMatthew Ahrens spa_close(spa, FTAG);
3919b5152584SMatthew Ahrens }
3920b5152584SMatthew Ahrens break;
3921b5152584SMatthew Ahrens
392292241e0bSTom Erickson case ZFS_PROP_SHARESMB:
392392241e0bSTom Erickson if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3924be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
392592241e0bSTom Erickson break;
392692241e0bSTom Erickson
392792241e0bSTom Erickson case ZFS_PROP_ACLINHERIT:
392892241e0bSTom Erickson if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
392992241e0bSTom Erickson nvpair_value_uint64(pair, &intval) == 0) {
393092241e0bSTom Erickson if (intval == ZFS_ACL_PASSTHROUGH_X &&
393192241e0bSTom Erickson zfs_earlier_version(dsname,
393292241e0bSTom Erickson SPA_VERSION_PASSTHROUGH_X))
3933be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
393492241e0bSTom Erickson }
393592241e0bSTom Erickson break;
393645818ee1SMatthew Ahrens
393745818ee1SMatthew Ahrens case ZFS_PROP_CHECKSUM:
393845818ee1SMatthew Ahrens case ZFS_PROP_DEDUP:
393945818ee1SMatthew Ahrens {
394045818ee1SMatthew Ahrens spa_feature_t feature;
394145818ee1SMatthew Ahrens spa_t *spa;
394245818ee1SMatthew Ahrens
394345818ee1SMatthew Ahrens /* dedup feature version checks */
394445818ee1SMatthew Ahrens if (prop == ZFS_PROP_DEDUP &&
394545818ee1SMatthew Ahrens zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
394645818ee1SMatthew Ahrens return (SET_ERROR(ENOTSUP));
394745818ee1SMatthew Ahrens
394845818ee1SMatthew Ahrens if (nvpair_value_uint64(pair, &intval) != 0)
394945818ee1SMatthew Ahrens return (SET_ERROR(EINVAL));
395045818ee1SMatthew Ahrens
395145818ee1SMatthew Ahrens /* check prop value is enabled in features */
3952971640e6Silovezfs feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
395345818ee1SMatthew Ahrens if (feature == SPA_FEATURE_NONE)
395445818ee1SMatthew Ahrens break;
395545818ee1SMatthew Ahrens
395645818ee1SMatthew Ahrens if ((err = spa_open(dsname, &spa, FTAG)) != 0)
395745818ee1SMatthew Ahrens return (err);
395845818ee1SMatthew Ahrens /*
395945818ee1SMatthew Ahrens * Salted checksums are not supported on root pools.
396045818ee1SMatthew Ahrens */
396145818ee1SMatthew Ahrens if (spa_bootfs(spa) != 0 &&
396245818ee1SMatthew Ahrens intval < ZIO_CHECKSUM_FUNCTIONS &&
396345818ee1SMatthew Ahrens (zio_checksum_table[intval].ci_flags &
396445818ee1SMatthew Ahrens ZCHECKSUM_FLAG_SALTED)) {
396545818ee1SMatthew Ahrens spa_close(spa, FTAG);
396645818ee1SMatthew Ahrens return (SET_ERROR(ERANGE));
396745818ee1SMatthew Ahrens }
396845818ee1SMatthew Ahrens if (!spa_feature_is_enabled(spa, feature)) {
396945818ee1SMatthew Ahrens spa_close(spa, FTAG);
397045818ee1SMatthew Ahrens return (SET_ERROR(ENOTSUP));
397145818ee1SMatthew Ahrens }
397245818ee1SMatthew Ahrens spa_close(spa, FTAG);
397345818ee1SMatthew Ahrens break;
397445818ee1SMatthew Ahrens }
397592241e0bSTom Erickson }
397692241e0bSTom Erickson
397792241e0bSTom Erickson return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
397892241e0bSTom Erickson }
397992241e0bSTom Erickson
398092241e0bSTom Erickson /*
3981a6f561b4SSašo Kiselkov * Checks for a race condition to make sure we don't increment a feature flag
3982a6f561b4SSašo Kiselkov * multiple times.
3983a6f561b4SSašo Kiselkov */
3984a6f561b4SSašo Kiselkov static int
zfs_prop_activate_feature_check(void * arg,dmu_tx_t * tx)39853b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3986a6f561b4SSašo Kiselkov {
39873b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa;
39882acef22dSMatthew Ahrens spa_feature_t *featurep = arg;
3989a6f561b4SSašo Kiselkov
39902acef22dSMatthew Ahrens if (!spa_feature_is_active(spa, *featurep))
3991a6f561b4SSašo Kiselkov return (0);
3992a6f561b4SSašo Kiselkov else
3993be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY));
3994a6f561b4SSašo Kiselkov }
3995a6f561b4SSašo Kiselkov
3996a6f561b4SSašo Kiselkov /*
3997a6f561b4SSašo Kiselkov * The callback invoked on feature activation in the sync task caused by
3998a6f561b4SSašo Kiselkov * zfs_prop_activate_feature.
3999a6f561b4SSašo Kiselkov */
4000a6f561b4SSašo Kiselkov static void
zfs_prop_activate_feature_sync(void * arg,dmu_tx_t * tx)40013b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4002a6f561b4SSašo Kiselkov {
40033b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa;
40042acef22dSMatthew Ahrens spa_feature_t *featurep = arg;
4005a6f561b4SSašo Kiselkov
40062acef22dSMatthew Ahrens spa_feature_incr(spa, *featurep, tx);
4007a6f561b4SSašo Kiselkov }
4008a6f561b4SSašo Kiselkov
4009a6f561b4SSašo Kiselkov /*
40103b2aab18SMatthew Ahrens * Activates a feature on a pool in response to a property setting. This
40113b2aab18SMatthew Ahrens * creates a new sync task which modifies the pool to reflect the feature
40123b2aab18SMatthew Ahrens * as being active.
40133b2aab18SMatthew Ahrens */
40143b2aab18SMatthew Ahrens static int
zfs_prop_activate_feature(spa_t * spa,spa_feature_t feature)40152acef22dSMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
40163b2aab18SMatthew Ahrens {
40173b2aab18SMatthew Ahrens int err;
40183b2aab18SMatthew Ahrens
40193b2aab18SMatthew Ahrens /* EBUSY here indicates that the feature is already active */
40203b2aab18SMatthew Ahrens err = dsl_sync_task(spa_name(spa),
40213b2aab18SMatthew Ahrens zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
40227d46dc6cSMatthew Ahrens &feature, 2, ZFS_SPACE_CHECK_RESERVED);
40233b2aab18SMatthew Ahrens
40243b2aab18SMatthew Ahrens if (err != 0 && err != EBUSY)
40253b2aab18SMatthew Ahrens return (err);
40263b2aab18SMatthew Ahrens else
40273b2aab18SMatthew Ahrens return (0);
40283b2aab18SMatthew Ahrens }
40293b2aab18SMatthew Ahrens
40303b2aab18SMatthew Ahrens /*
403192241e0bSTom Erickson * Removes properties from the given props list that fail permission checks
403292241e0bSTom Erickson * needed to clear them and to restore them in case of a receive error. For each
403392241e0bSTom Erickson * property, make sure we have both set and inherit permissions.
403492241e0bSTom Erickson *
403592241e0bSTom Erickson * Returns the first error encountered if any permission checks fail. If the
403692241e0bSTom Erickson * caller provides a non-NULL errlist, it also gives the complete list of names
403792241e0bSTom Erickson * of all the properties that failed a permission check along with the
403892241e0bSTom Erickson * corresponding error numbers. The caller is responsible for freeing the
403992241e0bSTom Erickson * returned errlist.
404092241e0bSTom Erickson *
404192241e0bSTom Erickson * If every property checks out successfully, zero is returned and the list
404292241e0bSTom Erickson * pointed at by errlist is NULL.
404392241e0bSTom Erickson */
404492241e0bSTom Erickson static int
zfs_check_clearable(char * dataset,nvlist_t * props,nvlist_t ** errlist)404592241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4046745cd3c5Smaybee {
4047745cd3c5Smaybee zfs_cmd_t *zc;
404892241e0bSTom Erickson nvpair_t *pair, *next_pair;
404992241e0bSTom Erickson nvlist_t *errors;
405092241e0bSTom Erickson int err, rv = 0;
4051745cd3c5Smaybee
4052745cd3c5Smaybee if (props == NULL)
405392241e0bSTom Erickson return (0);
405492241e0bSTom Erickson
405592241e0bSTom Erickson VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
405692241e0bSTom Erickson
4057745cd3c5Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4058745cd3c5Smaybee (void) strcpy(zc->zc_name, dataset);
405992241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL);
406092241e0bSTom Erickson while (pair != NULL) {
406192241e0bSTom Erickson next_pair = nvlist_next_nvpair(props, pair);
406292241e0bSTom Erickson
406392241e0bSTom Erickson (void) strcpy(zc->zc_value, nvpair_name(pair));
406492241e0bSTom Erickson if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
40654445fffbSMatthew Ahrens (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
406692241e0bSTom Erickson VERIFY(nvlist_remove_nvpair(props, pair) == 0);
406792241e0bSTom Erickson VERIFY(nvlist_add_int32(errors,
406892241e0bSTom Erickson zc->zc_value, err) == 0);
406992241e0bSTom Erickson }
407092241e0bSTom Erickson pair = next_pair;
4071745cd3c5Smaybee }
4072745cd3c5Smaybee kmem_free(zc, sizeof (zfs_cmd_t));
407392241e0bSTom Erickson
407492241e0bSTom Erickson if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
407592241e0bSTom Erickson nvlist_free(errors);
407692241e0bSTom Erickson errors = NULL;
407792241e0bSTom Erickson } else {
407892241e0bSTom Erickson VERIFY(nvpair_value_int32(pair, &rv) == 0);
4079745cd3c5Smaybee }
4080745cd3c5Smaybee
408192241e0bSTom Erickson if (errlist == NULL)
408292241e0bSTom Erickson nvlist_free(errors);
408392241e0bSTom Erickson else
408492241e0bSTom Erickson *errlist = errors;
408592241e0bSTom Erickson
408692241e0bSTom Erickson return (rv);
408792241e0bSTom Erickson }
408892241e0bSTom Erickson
408992241e0bSTom Erickson static boolean_t
propval_equals(nvpair_t * p1,nvpair_t * p2)409092241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
409192241e0bSTom Erickson {
409292241e0bSTom Erickson if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
409392241e0bSTom Erickson /* dsl_prop_get_all_impl() format */
409492241e0bSTom Erickson nvlist_t *attrs;
409592241e0bSTom Erickson VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
409692241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
409792241e0bSTom Erickson &p1) == 0);
409892241e0bSTom Erickson }
409992241e0bSTom Erickson
410092241e0bSTom Erickson if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
410192241e0bSTom Erickson nvlist_t *attrs;
410292241e0bSTom Erickson VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
410392241e0bSTom Erickson VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
410492241e0bSTom Erickson &p2) == 0);
410592241e0bSTom Erickson }
410692241e0bSTom Erickson
410792241e0bSTom Erickson if (nvpair_type(p1) != nvpair_type(p2))
410892241e0bSTom Erickson return (B_FALSE);
410992241e0bSTom Erickson
411092241e0bSTom Erickson if (nvpair_type(p1) == DATA_TYPE_STRING) {
411192241e0bSTom Erickson char *valstr1, *valstr2;
411292241e0bSTom Erickson
411392241e0bSTom Erickson VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
411492241e0bSTom Erickson VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
411592241e0bSTom Erickson return (strcmp(valstr1, valstr2) == 0);
411692241e0bSTom Erickson } else {
411792241e0bSTom Erickson uint64_t intval1, intval2;
411892241e0bSTom Erickson
411992241e0bSTom Erickson VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
412092241e0bSTom Erickson VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
412192241e0bSTom Erickson return (intval1 == intval2);
412292241e0bSTom Erickson }
412392241e0bSTom Erickson }
412492241e0bSTom Erickson
412592241e0bSTom Erickson /*
412692241e0bSTom Erickson * Remove properties from props if they are not going to change (as determined
412792241e0bSTom Erickson * by comparison with origprops). Remove them from origprops as well, since we
412892241e0bSTom Erickson * do not need to clear or restore properties that won't change.
412992241e0bSTom Erickson */
413092241e0bSTom Erickson static void
props_reduce(nvlist_t * props,nvlist_t * origprops)413192241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
413292241e0bSTom Erickson {
413392241e0bSTom Erickson nvpair_t *pair, *next_pair;
413492241e0bSTom Erickson
413592241e0bSTom Erickson if (origprops == NULL)
413692241e0bSTom Erickson return; /* all props need to be received */
413792241e0bSTom Erickson
413892241e0bSTom Erickson pair = nvlist_next_nvpair(props, NULL);
413992241e0bSTom Erickson while (pair != NULL) {
414092241e0bSTom Erickson const char *propname = nvpair_name(pair);
414192241e0bSTom Erickson nvpair_t *match;
414292241e0bSTom Erickson
414392241e0bSTom Erickson next_pair = nvlist_next_nvpair(props, pair);
414492241e0bSTom Erickson
414592241e0bSTom Erickson if ((nvlist_lookup_nvpair(origprops, propname,
414692241e0bSTom Erickson &match) != 0) || !propval_equals(pair, match))
414792241e0bSTom Erickson goto next; /* need to set received value */
414892241e0bSTom Erickson
414992241e0bSTom Erickson /* don't clear the existing received value */
415092241e0bSTom Erickson (void) nvlist_remove_nvpair(origprops, match);
415192241e0bSTom Erickson /* don't bother receiving the property */
415292241e0bSTom Erickson (void) nvlist_remove_nvpair(props, pair);
415392241e0bSTom Erickson next:
415492241e0bSTom Erickson pair = next_pair;
415592241e0bSTom Erickson }
415692241e0bSTom Erickson }
415792241e0bSTom Erickson
41585878fad7SDan McDonald /*
41595878fad7SDan McDonald * Extract properties that cannot be set PRIOR to the receipt of a dataset.
41605878fad7SDan McDonald * For example, refquota cannot be set until after the receipt of a dataset,
41615878fad7SDan McDonald * because in replication streams, an older/earlier snapshot may exceed the
41625878fad7SDan McDonald * refquota. We want to receive the older/earlier snapshot, but setting
41635878fad7SDan McDonald * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
41645878fad7SDan McDonald * the older/earlier snapshot from being received (with EDQUOT).
41655878fad7SDan McDonald *
41665878fad7SDan McDonald * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
41675878fad7SDan McDonald *
41685878fad7SDan McDonald * libzfs will need to be judicious handling errors encountered by props
41695878fad7SDan McDonald * extracted by this function.
41705878fad7SDan McDonald */
41715878fad7SDan McDonald static nvlist_t *
extract_delay_props(nvlist_t * props)41725878fad7SDan McDonald extract_delay_props(nvlist_t *props)
41735878fad7SDan McDonald {
41745878fad7SDan McDonald nvlist_t *delayprops;
41755878fad7SDan McDonald nvpair_t *nvp, *tmp;
41765878fad7SDan McDonald static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
41775878fad7SDan McDonald int i;
41785878fad7SDan McDonald
41795878fad7SDan McDonald VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
41805878fad7SDan McDonald
41815878fad7SDan McDonald for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
41825878fad7SDan McDonald nvp = nvlist_next_nvpair(props, nvp)) {
41835878fad7SDan McDonald /*
41845878fad7SDan McDonald * strcmp() is safe because zfs_prop_to_name() always returns
41855878fad7SDan McDonald * a bounded string.
41865878fad7SDan McDonald */
41875878fad7SDan McDonald for (i = 0; delayable[i] != 0; i++) {
41885878fad7SDan McDonald if (strcmp(zfs_prop_to_name(delayable[i]),
41895878fad7SDan McDonald nvpair_name(nvp)) == 0) {
41905878fad7SDan McDonald break;
41915878fad7SDan McDonald }
41925878fad7SDan McDonald }
41935878fad7SDan McDonald if (delayable[i] != 0) {
41945878fad7SDan McDonald tmp = nvlist_prev_nvpair(props, nvp);
41955878fad7SDan McDonald VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
41965878fad7SDan McDonald VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
41975878fad7SDan McDonald nvp = tmp;
41985878fad7SDan McDonald }
41995878fad7SDan McDonald }
42005878fad7SDan McDonald
42015878fad7SDan McDonald if (nvlist_empty(delayprops)) {
42025878fad7SDan McDonald nvlist_free(delayprops);
42035878fad7SDan McDonald delayprops = NULL;
42045878fad7SDan McDonald }
42055878fad7SDan McDonald return (delayprops);
42065878fad7SDan McDonald }
42075878fad7SDan McDonald
420892241e0bSTom Erickson #ifdef DEBUG
420992241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
421092241e0bSTom Erickson #endif
421192241e0bSTom Erickson
42123cb34c60Sahrens /*
42133cb34c60Sahrens * inputs:
42143cb34c60Sahrens * zc_name name of containing filesystem
42153cb34c60Sahrens * zc_nvlist_src{_size} nvlist of properties to apply
42163cb34c60Sahrens * zc_value name of snapshot to create
42173cb34c60Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE)
42183cb34c60Sahrens * zc_cookie file descriptor to recv from
42193cb34c60Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped)
42203cb34c60Sahrens * zc_guid force flag
4221c99e4bdcSChris Kirby * zc_cleanup_fd cleanup-on-exit file descriptor
4222c99e4bdcSChris Kirby * zc_action_handle handle for this guid/ds mapping (or zero on first call)
42239c3fd121SMatthew Ahrens * zc_resumable if data is incomplete assume sender will resume
42243cb34c60Sahrens *
42253cb34c60Sahrens * outputs:
42263cb34c60Sahrens * zc_cookie number of bytes read
422792241e0bSTom Erickson * zc_nvlist_dst{_size} error for each unapplied received property
422892241e0bSTom Erickson * zc_obj zprop_errflags_t
4229c99e4bdcSChris Kirby * zc_action_handle handle for this guid/ds mapping
42303cb34c60Sahrens */
4231fa9e4066Sahrens static int
zfs_ioc_recv(zfs_cmd_t * zc)42323cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
4233fa9e4066Sahrens {
4234fa9e4066Sahrens file_t *fp;
42353cb34c60Sahrens dmu_recv_cookie_t drc;
4236f18faf3fSek110237 boolean_t force = (boolean_t)zc->zc_guid;
423792241e0bSTom Erickson int fd;
423892241e0bSTom Erickson int error = 0;
423992241e0bSTom Erickson int props_error = 0;
424092241e0bSTom Erickson nvlist_t *errors;
42413cb34c60Sahrens offset_t off;
424292241e0bSTom Erickson nvlist_t *props = NULL; /* sent properties */
424392241e0bSTom Erickson nvlist_t *origprops = NULL; /* existing properties */
42445878fad7SDan McDonald nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
42453b2aab18SMatthew Ahrens char *origin = NULL;
42463cb34c60Sahrens char *tosnap;
4247*40a5c998SMatthew Ahrens char tofs[ZFS_MAX_DATASET_NAME_LEN];
424892241e0bSTom Erickson boolean_t first_recvd_props = B_FALSE;
4249fa9e4066Sahrens
42503ccfa83cSahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4251f18faf3fSek110237 strchr(zc->zc_value, '@') == NULL ||
4252f18faf3fSek110237 strchr(zc->zc_value, '%'))
4253be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
42543ccfa83cSahrens
42553cb34c60Sahrens (void) strcpy(tofs, zc->zc_value);
42563cb34c60Sahrens tosnap = strchr(tofs, '@');
425792241e0bSTom Erickson *tosnap++ = '\0';
42583cb34c60Sahrens
42593cb34c60Sahrens if (zc->zc_nvlist_src != NULL &&
42603cb34c60Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4261478ed9adSEric Taylor zc->zc_iflags, &props)) != 0)
42623cb34c60Sahrens return (error);
42633cb34c60Sahrens
4264fa9e4066Sahrens fd = zc->zc_cookie;
4265fa9e4066Sahrens fp = getf(fd);
42663cb34c60Sahrens if (fp == NULL) {
42673cb34c60Sahrens nvlist_free(props);
4268be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF));
42693cb34c60Sahrens }
4270a2eea2e1Sahrens
42719c3fd121SMatthew Ahrens errors = fnvlist_alloc();
427292241e0bSTom Erickson
42733b2aab18SMatthew Ahrens if (zc->zc_string[0])
42743b2aab18SMatthew Ahrens origin = zc->zc_string;
42753b2aab18SMatthew Ahrens
42763b2aab18SMatthew Ahrens error = dmu_recv_begin(tofs, tosnap,
42779c3fd121SMatthew Ahrens &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
42783b2aab18SMatthew Ahrens if (error != 0)
42793b2aab18SMatthew Ahrens goto out;
42803b2aab18SMatthew Ahrens
42813b2aab18SMatthew Ahrens /*
42823b2aab18SMatthew Ahrens * Set properties before we receive the stream so that they are applied
42833b2aab18SMatthew Ahrens * to the new data. Note that we must call dmu_recv_stream() if
42843b2aab18SMatthew Ahrens * dmu_recv_begin() succeeds.
42853b2aab18SMatthew Ahrens */
42863b2aab18SMatthew Ahrens if (props != NULL && !drc.drc_newfs) {
42873b2aab18SMatthew Ahrens if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
42883b2aab18SMatthew Ahrens SPA_VERSION_RECVD_PROPS &&
42893b2aab18SMatthew Ahrens !dsl_prop_get_hasrecvd(tofs))
429092241e0bSTom Erickson first_recvd_props = B_TRUE;
429192241e0bSTom Erickson
4292745cd3c5Smaybee /*
429392241e0bSTom Erickson * If new received properties are supplied, they are to
429492241e0bSTom Erickson * completely replace the existing received properties, so stash
429592241e0bSTom Erickson * away the existing ones.
4296745cd3c5Smaybee */
42973b2aab18SMatthew Ahrens if (dsl_prop_get_received(tofs, &origprops) == 0) {
429892241e0bSTom Erickson nvlist_t *errlist = NULL;
429992241e0bSTom Erickson /*
430092241e0bSTom Erickson * Don't bother writing a property if its value won't
430192241e0bSTom Erickson * change (and avoid the unnecessary security checks).
430292241e0bSTom Erickson *
430392241e0bSTom Erickson * The first receive after SPA_VERSION_RECVD_PROPS is a
430492241e0bSTom Erickson * special case where we blow away all local properties
430592241e0bSTom Erickson * regardless.
430692241e0bSTom Erickson */
430792241e0bSTom Erickson if (!first_recvd_props)
430892241e0bSTom Erickson props_reduce(props, origprops);
43093b2aab18SMatthew Ahrens if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
431092241e0bSTom Erickson (void) nvlist_merge(errors, errlist, 0);
431192241e0bSTom Erickson nvlist_free(errlist);
4312745cd3c5Smaybee
43133b2aab18SMatthew Ahrens if (clear_received_props(tofs, origprops,
431492241e0bSTom Erickson first_recvd_props ? NULL : props) != 0)
431592241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR;
431692241e0bSTom Erickson } else {
431792241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NOCLEAR;
431892241e0bSTom Erickson }
431992241e0bSTom Erickson }
432092241e0bSTom Erickson
43213b2aab18SMatthew Ahrens if (props != NULL) {
43223b2aab18SMatthew Ahrens props_error = dsl_prop_set_hasrecvd(tofs);
43233b2aab18SMatthew Ahrens
43243b2aab18SMatthew Ahrens if (props_error == 0) {
43255878fad7SDan McDonald delayprops = extract_delay_props(props);
432692241e0bSTom Erickson (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
43274445fffbSMatthew Ahrens props, errors);
432892241e0bSTom Erickson }
43293b2aab18SMatthew Ahrens }
433092241e0bSTom Erickson
43313cb34c60Sahrens off = fp->f_offset;
4332c99e4bdcSChris Kirby error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4333c99e4bdcSChris Kirby &zc->zc_action_handle);
43343cb34c60Sahrens
4335f4b94bdeSMatthew Ahrens if (error == 0) {
4336f4b94bdeSMatthew Ahrens zfsvfs_t *zfsvfs = NULL;
4337f4b94bdeSMatthew Ahrens
4338f4b94bdeSMatthew Ahrens if (getzfsvfs(tofs, &zfsvfs) == 0) {
4339f4b94bdeSMatthew Ahrens /* online recv */
4340f4b94bdeSMatthew Ahrens int end_err;
4341f18faf3fSek110237
4342503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs);
4343f4b94bdeSMatthew Ahrens /*
4344f4b94bdeSMatthew Ahrens * If the suspend fails, then the recv_end will
4345f4b94bdeSMatthew Ahrens * likely also fail, and clean up after itself.
4346f4b94bdeSMatthew Ahrens */
434791948b51SKeith M Wesolowski end_err = dmu_recv_end(&drc, zfsvfs);
43485c703fceSGeorge Wilson if (error == 0)
43495c703fceSGeorge Wilson error = zfs_resume_fs(zfsvfs, tofs);
4350f4b94bdeSMatthew Ahrens error = error ? error : end_err;
4351f4b94bdeSMatthew Ahrens VFS_RELE(zfsvfs->z_vfs);
4352f4b94bdeSMatthew Ahrens } else {
435391948b51SKeith M Wesolowski error = dmu_recv_end(&drc, NULL);
4354f18faf3fSek110237 }
43555878fad7SDan McDonald
43565878fad7SDan McDonald /* Set delayed properties now, after we're done receiving. */
43575878fad7SDan McDonald if (delayprops != NULL && error == 0) {
43585878fad7SDan McDonald (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
43595878fad7SDan McDonald delayprops, errors);
43605878fad7SDan McDonald }
43615878fad7SDan McDonald }
43625878fad7SDan McDonald
43635878fad7SDan McDonald if (delayprops != NULL) {
43645878fad7SDan McDonald /*
43655878fad7SDan McDonald * Merge delayed props back in with initial props, in case
43665878fad7SDan McDonald * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
43675878fad7SDan McDonald * we have to make sure clear_received_props() includes
43685878fad7SDan McDonald * the delayed properties).
43695878fad7SDan McDonald *
43705878fad7SDan McDonald * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
43715878fad7SDan McDonald * using ASSERT() will be just like a VERIFY.
43725878fad7SDan McDonald */
43735878fad7SDan McDonald ASSERT(nvlist_merge(props, delayprops, 0) == 0);
43745878fad7SDan McDonald nvlist_free(delayprops);
43755878fad7SDan McDonald }
43765878fad7SDan McDonald
43775878fad7SDan McDonald /*
43785878fad7SDan McDonald * Now that all props, initial and delayed, are set, report the prop
43795878fad7SDan McDonald * errors to the caller.
43805878fad7SDan McDonald */
43815878fad7SDan McDonald if (zc->zc_nvlist_dst_size != 0 &&
43825878fad7SDan McDonald (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
43835878fad7SDan McDonald put_nvlist(zc, errors) != 0)) {
43845878fad7SDan McDonald /*
43855878fad7SDan McDonald * Caller made zc->zc_nvlist_dst less than the minimum expected
43865878fad7SDan McDonald * size or supplied an invalid address.
43875878fad7SDan McDonald */
43885878fad7SDan McDonald props_error = SET_ERROR(EINVAL);
4389f4b94bdeSMatthew Ahrens }
43903cb34c60Sahrens
43913cb34c60Sahrens zc->zc_cookie = off - fp->f_offset;
43923cb34c60Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
43933cb34c60Sahrens fp->f_offset = off;
4394a2eea2e1Sahrens
439592241e0bSTom Erickson #ifdef DEBUG
439692241e0bSTom Erickson if (zfs_ioc_recv_inject_err) {
439792241e0bSTom Erickson zfs_ioc_recv_inject_err = B_FALSE;
439892241e0bSTom Erickson error = 1;
439992241e0bSTom Erickson }
440092241e0bSTom Erickson #endif
4401745cd3c5Smaybee /*
4402745cd3c5Smaybee * On error, restore the original props.
4403745cd3c5Smaybee */
44043b2aab18SMatthew Ahrens if (error != 0 && props != NULL && !drc.drc_newfs) {
44053b2aab18SMatthew Ahrens if (clear_received_props(tofs, props, NULL) != 0) {
440692241e0bSTom Erickson /*
440792241e0bSTom Erickson * We failed to clear the received properties.
440892241e0bSTom Erickson * Since we may have left a $recvd value on the
440992241e0bSTom Erickson * system, we can't clear the $hasrecvd flag.
441092241e0bSTom Erickson */
441192241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE;
441292241e0bSTom Erickson } else if (first_recvd_props) {
44133b2aab18SMatthew Ahrens dsl_prop_unset_hasrecvd(tofs);
441492241e0bSTom Erickson }
441592241e0bSTom Erickson
441692241e0bSTom Erickson if (origprops == NULL && !drc.drc_newfs) {
441792241e0bSTom Erickson /* We failed to stash the original properties. */
441892241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE;
441992241e0bSTom Erickson }
442092241e0bSTom Erickson
442192241e0bSTom Erickson /*
442292241e0bSTom Erickson * dsl_props_set() will not convert RECEIVED to LOCAL on or
442392241e0bSTom Erickson * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
442492241e0bSTom Erickson * explictly if we're restoring local properties cleared in the
442592241e0bSTom Erickson * first new-style receive.
442692241e0bSTom Erickson */
442792241e0bSTom Erickson if (origprops != NULL &&
442892241e0bSTom Erickson zfs_set_prop_nvlist(tofs, (first_recvd_props ?
442992241e0bSTom Erickson ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
443092241e0bSTom Erickson origprops, NULL) != 0) {
443192241e0bSTom Erickson /*
443292241e0bSTom Erickson * We stashed the original properties but failed to
443392241e0bSTom Erickson * restore them.
443492241e0bSTom Erickson */
443592241e0bSTom Erickson zc->zc_obj |= ZPROP_ERR_NORESTORE;
443692241e0bSTom Erickson }
4437745cd3c5Smaybee }
4438745cd3c5Smaybee out:
4439745cd3c5Smaybee nvlist_free(props);
4440745cd3c5Smaybee nvlist_free(origprops);
444192241e0bSTom Erickson nvlist_free(errors);
4442fa9e4066Sahrens releasef(fd);
444392241e0bSTom Erickson
444492241e0bSTom Erickson if (error == 0)
444592241e0bSTom Erickson error = props_error;
444692241e0bSTom Erickson
4447fa9e4066Sahrens return (error);
4448fa9e4066Sahrens }
4449fa9e4066Sahrens
44503cb34c60Sahrens /*
44513cb34c60Sahrens * inputs:
44523cb34c60Sahrens * zc_name name of snapshot to send
44533cb34c60Sahrens * zc_cookie file descriptor to send stream to
4454a7f53a56SChris Kirby * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4455a7f53a56SChris Kirby * zc_sendobj objsetid of snapshot to send
4456a7f53a56SChris Kirby * zc_fromobj objsetid of incremental fromsnap (may be zero)
445719b94df9SMatthew Ahrens * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
445819b94df9SMatthew Ahrens * output size in zc_objset_type.
4459b5152584SMatthew Ahrens * zc_flags lzc_send_flags
44603cb34c60Sahrens *
446178f17100SMatthew Ahrens * outputs:
446278f17100SMatthew Ahrens * zc_objset_type estimated size, if zc_guid is set
44633cb34c60Sahrens */
4464fa9e4066Sahrens static int
zfs_ioc_send(zfs_cmd_t * zc)44653cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
4466fa9e4066Sahrens {
4467fa9e4066Sahrens int error;
44683cb34c60Sahrens offset_t off;
446919b94df9SMatthew Ahrens boolean_t estimate = (zc->zc_guid != 0);
44705d7b4d43SMatthew Ahrens boolean_t embedok = (zc->zc_flags & 0x1);
4471b5152584SMatthew Ahrens boolean_t large_block_ok = (zc->zc_flags & 0x2);
4472fa9e4066Sahrens
44733b2aab18SMatthew Ahrens if (zc->zc_obj != 0) {
44743b2aab18SMatthew Ahrens dsl_pool_t *dp;
44753b2aab18SMatthew Ahrens dsl_dataset_t *tosnap;
44763b2aab18SMatthew Ahrens
44773b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
44783b2aab18SMatthew Ahrens if (error != 0)
4479fa9e4066Sahrens return (error);
4480fa9e4066Sahrens
44813b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
44823b2aab18SMatthew Ahrens if (error != 0) {
44833b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
44843b2aab18SMatthew Ahrens return (error);
44853b2aab18SMatthew Ahrens }
44863b2aab18SMatthew Ahrens
44873b2aab18SMatthew Ahrens if (dsl_dir_is_clone(tosnap->ds_dir))
4488c1379625SJustin T. Gibbs zc->zc_fromobj =
4489c1379625SJustin T. Gibbs dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
44903b2aab18SMatthew Ahrens dsl_dataset_rele(tosnap, FTAG);
44913b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
44923b2aab18SMatthew Ahrens }
44933b2aab18SMatthew Ahrens
44943b2aab18SMatthew Ahrens if (estimate) {
44953b2aab18SMatthew Ahrens dsl_pool_t *dp;
44963b2aab18SMatthew Ahrens dsl_dataset_t *tosnap;
44973b2aab18SMatthew Ahrens dsl_dataset_t *fromsnap = NULL;
44983b2aab18SMatthew Ahrens
44993b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
45003b2aab18SMatthew Ahrens if (error != 0)
4501fa9e4066Sahrens return (error);
4502a7f53a56SChris Kirby
45033b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
45043b2aab18SMatthew Ahrens if (error != 0) {
45053b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
4506a7f53a56SChris Kirby return (error);
4507a7f53a56SChris Kirby }
4508a7f53a56SChris Kirby
4509a7f53a56SChris Kirby if (zc->zc_fromobj != 0) {
45103b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
45113b2aab18SMatthew Ahrens FTAG, &fromsnap);
45123b2aab18SMatthew Ahrens if (error != 0) {
45133b2aab18SMatthew Ahrens dsl_dataset_rele(tosnap, FTAG);
45143b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
4515a7f53a56SChris Kirby return (error);
4516a7f53a56SChris Kirby }
45174445fffbSMatthew Ahrens }
45184445fffbSMatthew Ahrens
45194445fffbSMatthew Ahrens error = dmu_send_estimate(tosnap, fromsnap,
452019b94df9SMatthew Ahrens &zc->zc_objset_type);
45213b2aab18SMatthew Ahrens
45223b2aab18SMatthew Ahrens if (fromsnap != NULL)
45233b2aab18SMatthew Ahrens dsl_dataset_rele(fromsnap, FTAG);
45243b2aab18SMatthew Ahrens dsl_dataset_rele(tosnap, FTAG);
45253b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
452619b94df9SMatthew Ahrens } else {
452719b94df9SMatthew Ahrens file_t *fp = getf(zc->zc_cookie);
45283b2aab18SMatthew Ahrens if (fp == NULL)
4529be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF));
4530fa9e4066Sahrens
45313cb34c60Sahrens off = fp->f_offset;
45323b2aab18SMatthew Ahrens error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4533b5152584SMatthew Ahrens zc->zc_fromobj, embedok, large_block_ok,
4534b5152584SMatthew Ahrens zc->zc_cookie, fp->f_vnode, &off);
4535fa9e4066Sahrens
45363cb34c60Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
45373cb34c60Sahrens fp->f_offset = off;
4538fa9e4066Sahrens releasef(zc->zc_cookie);
453919b94df9SMatthew Ahrens }
4540fa9e4066Sahrens return (error);
4541fa9e4066Sahrens }
4542fa9e4066Sahrens
45434e3c9f44SBill Pijewski /*
45444e3c9f44SBill Pijewski * inputs:
45454e3c9f44SBill Pijewski * zc_name name of snapshot on which to report progress
45464e3c9f44SBill Pijewski * zc_cookie file descriptor of send stream
45474e3c9f44SBill Pijewski *
45484e3c9f44SBill Pijewski * outputs:
45494e3c9f44SBill Pijewski * zc_cookie number of bytes written in send stream thus far
45504e3c9f44SBill Pijewski */
45514e3c9f44SBill Pijewski static int
zfs_ioc_send_progress(zfs_cmd_t * zc)45524e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc)
45534e3c9f44SBill Pijewski {
45543b2aab18SMatthew Ahrens dsl_pool_t *dp;
45554e3c9f44SBill Pijewski dsl_dataset_t *ds;
45564e3c9f44SBill Pijewski dmu_sendarg_t *dsp = NULL;
45574e3c9f44SBill Pijewski int error;
45584e3c9f44SBill Pijewski
45593b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
45603b2aab18SMatthew Ahrens if (error != 0)
45614e3c9f44SBill Pijewski return (error);
45624e3c9f44SBill Pijewski
45633b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
45643b2aab18SMatthew Ahrens if (error != 0) {
45653b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
45663b2aab18SMatthew Ahrens return (error);
45673b2aab18SMatthew Ahrens }
45683b2aab18SMatthew Ahrens
45694e3c9f44SBill Pijewski mutex_enter(&ds->ds_sendstream_lock);
45704e3c9f44SBill Pijewski
45714e3c9f44SBill Pijewski /*
45724e3c9f44SBill Pijewski * Iterate over all the send streams currently active on this dataset.
45734e3c9f44SBill Pijewski * If there's one which matches the specified file descriptor _and_ the
45744e3c9f44SBill Pijewski * stream was started by the current process, return the progress of
45754e3c9f44SBill Pijewski * that stream.
45764e3c9f44SBill Pijewski */
45774e3c9f44SBill Pijewski for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
45784e3c9f44SBill Pijewski dsp = list_next(&ds->ds_sendstreams, dsp)) {
45794e3c9f44SBill Pijewski if (dsp->dsa_outfd == zc->zc_cookie &&
45804e3c9f44SBill Pijewski dsp->dsa_proc == curproc)
45814e3c9f44SBill Pijewski break;
45824e3c9f44SBill Pijewski }
45834e3c9f44SBill Pijewski
45844e3c9f44SBill Pijewski if (dsp != NULL)
45854e3c9f44SBill Pijewski zc->zc_cookie = *(dsp->dsa_off);
45864e3c9f44SBill Pijewski else
4587be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT);
45884e3c9f44SBill Pijewski
45894e3c9f44SBill Pijewski mutex_exit(&ds->ds_sendstream_lock);
45904e3c9f44SBill Pijewski dsl_dataset_rele(ds, FTAG);
45913b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
45924e3c9f44SBill Pijewski return (error);
45934e3c9f44SBill Pijewski }
45944e3c9f44SBill Pijewski
4595ea8dc4b6Seschrock static int
zfs_ioc_inject_fault(zfs_cmd_t * zc)4596ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
4597ea8dc4b6Seschrock {
4598ea8dc4b6Seschrock int id, error;
4599ea8dc4b6Seschrock
4600ea8dc4b6Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4601ea8dc4b6Seschrock &zc->zc_inject_record);
4602ea8dc4b6Seschrock
4603ea8dc4b6Seschrock if (error == 0)
4604ea8dc4b6Seschrock zc->zc_guid = (uint64_t)id;
4605ea8dc4b6Seschrock
4606ea8dc4b6Seschrock return (error);
4607ea8dc4b6Seschrock }
4608ea8dc4b6Seschrock
4609ea8dc4b6Seschrock static int
zfs_ioc_clear_fault(zfs_cmd_t * zc)4610ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
4611ea8dc4b6Seschrock {
4612ea8dc4b6Seschrock return (zio_clear_fault((int)zc->zc_guid));
4613ea8dc4b6Seschrock }
4614ea8dc4b6Seschrock
4615ea8dc4b6Seschrock static int
zfs_ioc_inject_list_next(zfs_cmd_t * zc)4616ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4617ea8dc4b6Seschrock {
4618ea8dc4b6Seschrock int id = (int)zc->zc_guid;
4619ea8dc4b6Seschrock int error;
4620ea8dc4b6Seschrock
4621ea8dc4b6Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4622ea8dc4b6Seschrock &zc->zc_inject_record);
4623ea8dc4b6Seschrock
4624ea8dc4b6Seschrock zc->zc_guid = id;
4625ea8dc4b6Seschrock
4626ea8dc4b6Seschrock return (error);
4627ea8dc4b6Seschrock }
4628ea8dc4b6Seschrock
4629ea8dc4b6Seschrock static int
zfs_ioc_error_log(zfs_cmd_t * zc)4630ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
4631ea8dc4b6Seschrock {
4632ea8dc4b6Seschrock spa_t *spa;
4633ea8dc4b6Seschrock int error;
4634e9dbad6fSeschrock size_t count = (size_t)zc->zc_nvlist_dst_size;
4635ea8dc4b6Seschrock
4636ea8dc4b6Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4637ea8dc4b6Seschrock return (error);
4638ea8dc4b6Seschrock
4639e9dbad6fSeschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4640ea8dc4b6Seschrock &count);
4641ea8dc4b6Seschrock if (error == 0)
4642e9dbad6fSeschrock zc->zc_nvlist_dst_size = count;
4643ea8dc4b6Seschrock else
4644e9dbad6fSeschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4645ea8dc4b6Seschrock
4646ea8dc4b6Seschrock spa_close(spa, FTAG);
4647ea8dc4b6Seschrock
4648ea8dc4b6Seschrock return (error);
4649ea8dc4b6Seschrock }
4650ea8dc4b6Seschrock
4651ea8dc4b6Seschrock static int
zfs_ioc_clear(zfs_cmd_t * zc)4652ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
4653ea8dc4b6Seschrock {
4654ea8dc4b6Seschrock spa_t *spa;
4655ea8dc4b6Seschrock vdev_t *vd;
4656bb8b5132Sek110237 int error;
4657ea8dc4b6Seschrock
4658b87f3af3Sperrin /*
4659b87f3af3Sperrin * On zpool clear we also fix up missing slogs
4660b87f3af3Sperrin */
4661b87f3af3Sperrin mutex_enter(&spa_namespace_lock);
4662b87f3af3Sperrin spa = spa_lookup(zc->zc_name);
4663b87f3af3Sperrin if (spa == NULL) {
4664b87f3af3Sperrin mutex_exit(&spa_namespace_lock);
4665be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
4666b87f3af3Sperrin }
4667b24ab676SJeff Bonwick if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4668b87f3af3Sperrin /* we need to let spa_open/spa_load clear the chains */
4669b24ab676SJeff Bonwick spa_set_log_state(spa, SPA_LOG_CLEAR);
4670b87f3af3Sperrin }
4671468c413aSTim Haley spa->spa_last_open_failed = 0;
4672b87f3af3Sperrin mutex_exit(&spa_namespace_lock);
4673b87f3af3Sperrin
4674c8ee1847SVictor Latushkin if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4675468c413aSTim Haley error = spa_open(zc->zc_name, &spa, FTAG);
4676468c413aSTim Haley } else {
4677468c413aSTim Haley nvlist_t *policy;
4678468c413aSTim Haley nvlist_t *config = NULL;
4679468c413aSTim Haley
4680468c413aSTim Haley if (zc->zc_nvlist_src == NULL)
4681be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
4682468c413aSTim Haley
4683468c413aSTim Haley if ((error = get_nvlist(zc->zc_nvlist_src,
4684468c413aSTim Haley zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4685468c413aSTim Haley error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4686468c413aSTim Haley policy, &config);
4687468c413aSTim Haley if (config != NULL) {
46884b964adaSGeorge Wilson int err;
46894b964adaSGeorge Wilson
46904b964adaSGeorge Wilson if ((err = put_nvlist(zc, config)) != 0)
46914b964adaSGeorge Wilson error = err;
4692468c413aSTim Haley nvlist_free(config);
4693468c413aSTim Haley }
4694468c413aSTim Haley nvlist_free(policy);
4695468c413aSTim Haley }
4696468c413aSTim Haley }
4697468c413aSTim Haley
46983b2aab18SMatthew Ahrens if (error != 0)
4699ea8dc4b6Seschrock return (error);
4700ea8dc4b6Seschrock
47018f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE);
4702ea8dc4b6Seschrock
4703e9dbad6fSeschrock if (zc->zc_guid == 0) {
4704ea8dc4b6Seschrock vd = NULL;
4705c5904d13Seschrock } else {
4706c5904d13Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4707fa94a07fSbrendan if (vd == NULL) {
4708e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4709ea8dc4b6Seschrock spa_close(spa, FTAG);
4710be6fd75aSMatthew Ahrens return (SET_ERROR(ENODEV));
4711ea8dc4b6Seschrock }
4712fa94a07fSbrendan }
4713ea8dc4b6Seschrock
4714e14bb325SJeff Bonwick vdev_clear(spa, vd);
4715ea8dc4b6Seschrock
4716e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0);
4717e14bb325SJeff Bonwick
4718e14bb325SJeff Bonwick /*
4719e14bb325SJeff Bonwick * Resume any suspended I/Os.
4720e14bb325SJeff Bonwick */
472154d692b7SGeorge Wilson if (zio_resume(spa) != 0)
4722be6fd75aSMatthew Ahrens error = SET_ERROR(EIO);
4723ea8dc4b6Seschrock
4724ea8dc4b6Seschrock spa_close(spa, FTAG);
4725ea8dc4b6Seschrock
472654d692b7SGeorge Wilson return (error);
4727ea8dc4b6Seschrock }
4728ea8dc4b6Seschrock
47294263d13fSGeorge Wilson static int
zfs_ioc_pool_reopen(zfs_cmd_t * zc)47304263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc)
47314263d13fSGeorge Wilson {
47324263d13fSGeorge Wilson spa_t *spa;
47334263d13fSGeorge Wilson int error;
47344263d13fSGeorge Wilson
47354263d13fSGeorge Wilson error = spa_open(zc->zc_name, &spa, FTAG);
47363b2aab18SMatthew Ahrens if (error != 0)
47374263d13fSGeorge Wilson return (error);
47384263d13fSGeorge Wilson
47394263d13fSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE);
4740d6afdce2SGeorge Wilson
4741d6afdce2SGeorge Wilson /*
4742d6afdce2SGeorge Wilson * If a resilver is already in progress then set the
4743d6afdce2SGeorge Wilson * spa_scrub_reopen flag to B_TRUE so that we don't restart
4744d6afdce2SGeorge Wilson * the scan as a side effect of the reopen. Otherwise, let
4745d6afdce2SGeorge Wilson * vdev_open() decided if a resilver is required.
4746d6afdce2SGeorge Wilson */
4747d6afdce2SGeorge Wilson spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
47484263d13fSGeorge Wilson vdev_reopen(spa->spa_root_vdev);
4749d6afdce2SGeorge Wilson spa->spa_scrub_reopen = B_FALSE;
4750d6afdce2SGeorge Wilson
47514263d13fSGeorge Wilson (void) spa_vdev_state_exit(spa, NULL, 0);
47524263d13fSGeorge Wilson spa_close(spa, FTAG);
47534263d13fSGeorge Wilson return (0);
47544263d13fSGeorge Wilson }
47553cb34c60Sahrens /*
47563cb34c60Sahrens * inputs:
47573cb34c60Sahrens * zc_name name of filesystem
47583cb34c60Sahrens * zc_value name of origin snapshot
47593cb34c60Sahrens *
4760681d9761SEric Taylor * outputs:
4761681d9761SEric Taylor * zc_string name of conflicting snapshot, if there is one
47623cb34c60Sahrens */
4763ea8dc4b6Seschrock static int
zfs_ioc_promote(zfs_cmd_t * zc)476499653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
476599653d4eSeschrock {
47660b69c2f0Sahrens char *cp;
47670b69c2f0Sahrens
47680b69c2f0Sahrens /*
47690b69c2f0Sahrens * We don't need to unmount *all* the origin fs's snapshots, but
47700b69c2f0Sahrens * it's easier.
47710b69c2f0Sahrens */
4772e9dbad6fSeschrock cp = strchr(zc->zc_value, '@');
47730b69c2f0Sahrens if (cp)
47740b69c2f0Sahrens *cp = '\0';
4775e9dbad6fSeschrock (void) dmu_objset_find(zc->zc_value,
47763b2aab18SMatthew Ahrens zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4777681d9761SEric Taylor return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
477899653d4eSeschrock }
477999653d4eSeschrock
4780ecd6cf80Smarks /*
478114843421SMatthew Ahrens * Retrieve a single {user|group}{used|quota}@... property.
478214843421SMatthew Ahrens *
478314843421SMatthew Ahrens * inputs:
478414843421SMatthew Ahrens * zc_name name of filesystem
478514843421SMatthew Ahrens * zc_objset_type zfs_userquota_prop_t
478614843421SMatthew Ahrens * zc_value domain name (eg. "S-1-234-567-89")
478714843421SMatthew Ahrens * zc_guid RID/UID/GID
478814843421SMatthew Ahrens *
478914843421SMatthew Ahrens * outputs:
479014843421SMatthew Ahrens * zc_cookie property value
479114843421SMatthew Ahrens */
479214843421SMatthew Ahrens static int
zfs_ioc_userspace_one(zfs_cmd_t * zc)479314843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
479414843421SMatthew Ahrens {
479514843421SMatthew Ahrens zfsvfs_t *zfsvfs;
479614843421SMatthew Ahrens int error;
479714843421SMatthew Ahrens
479814843421SMatthew Ahrens if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4799be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
480014843421SMatthew Ahrens
48011412a1a2SMark Shellenbaum error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
48023b2aab18SMatthew Ahrens if (error != 0)
480314843421SMatthew Ahrens return (error);
480414843421SMatthew Ahrens
480514843421SMatthew Ahrens error = zfs_userspace_one(zfsvfs,
480614843421SMatthew Ahrens zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
480714843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG);
480814843421SMatthew Ahrens
480914843421SMatthew Ahrens return (error);
481014843421SMatthew Ahrens }
481114843421SMatthew Ahrens
481214843421SMatthew Ahrens /*
481314843421SMatthew Ahrens * inputs:
481414843421SMatthew Ahrens * zc_name name of filesystem
481514843421SMatthew Ahrens * zc_cookie zap cursor
481614843421SMatthew Ahrens * zc_objset_type zfs_userquota_prop_t
481714843421SMatthew Ahrens * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
481814843421SMatthew Ahrens *
481914843421SMatthew Ahrens * outputs:
482014843421SMatthew Ahrens * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
482114843421SMatthew Ahrens * zc_cookie zap cursor
482214843421SMatthew Ahrens */
482314843421SMatthew Ahrens static int
zfs_ioc_userspace_many(zfs_cmd_t * zc)482414843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
482514843421SMatthew Ahrens {
482614843421SMatthew Ahrens zfsvfs_t *zfsvfs;
4827eeb85002STim Haley int bufsize = zc->zc_nvlist_dst_size;
482814843421SMatthew Ahrens
4829eeb85002STim Haley if (bufsize <= 0)
4830be6fd75aSMatthew Ahrens return (SET_ERROR(ENOMEM));
4831eeb85002STim Haley
48321412a1a2SMark Shellenbaum int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
48333b2aab18SMatthew Ahrens if (error != 0)
483414843421SMatthew Ahrens return (error);
483514843421SMatthew Ahrens
483614843421SMatthew Ahrens void *buf = kmem_alloc(bufsize, KM_SLEEP);
483714843421SMatthew Ahrens
483814843421SMatthew Ahrens error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
483914843421SMatthew Ahrens buf, &zc->zc_nvlist_dst_size);
484014843421SMatthew Ahrens
484114843421SMatthew Ahrens if (error == 0) {
484214843421SMatthew Ahrens error = xcopyout(buf,
484314843421SMatthew Ahrens (void *)(uintptr_t)zc->zc_nvlist_dst,
484414843421SMatthew Ahrens zc->zc_nvlist_dst_size);
484514843421SMatthew Ahrens }
484614843421SMatthew Ahrens kmem_free(buf, bufsize);
484714843421SMatthew Ahrens zfsvfs_rele(zfsvfs, FTAG);
484814843421SMatthew Ahrens
484914843421SMatthew Ahrens return (error);
485014843421SMatthew Ahrens }
485114843421SMatthew Ahrens
485214843421SMatthew Ahrens /*
485314843421SMatthew Ahrens * inputs:
485414843421SMatthew Ahrens * zc_name name of filesystem
485514843421SMatthew Ahrens *
485614843421SMatthew Ahrens * outputs:
485714843421SMatthew Ahrens * none
485814843421SMatthew Ahrens */
485914843421SMatthew Ahrens static int
zfs_ioc_userspace_upgrade(zfs_cmd_t * zc)486014843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
486114843421SMatthew Ahrens {
486214843421SMatthew Ahrens objset_t *os;
48631195e687SMark J Musante int error = 0;
486414843421SMatthew Ahrens zfsvfs_t *zfsvfs;
486514843421SMatthew Ahrens
486614843421SMatthew Ahrens if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4867503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
486814843421SMatthew Ahrens /*
486914843421SMatthew Ahrens * If userused is not enabled, it may be because the
487014843421SMatthew Ahrens * objset needs to be closed & reopened (to grow the
487114843421SMatthew Ahrens * objset_phys_t). Suspend/resume the fs will do that.
487214843421SMatthew Ahrens */
4873503ad85cSMatthew Ahrens error = zfs_suspend_fs(zfsvfs);
487491948b51SKeith M Wesolowski if (error == 0) {
487591948b51SKeith M Wesolowski dmu_objset_refresh_ownership(zfsvfs->z_os,
487691948b51SKeith M Wesolowski zfsvfs);
4877503ad85cSMatthew Ahrens error = zfs_resume_fs(zfsvfs, zc->zc_name);
487814843421SMatthew Ahrens }
487991948b51SKeith M Wesolowski }
488014843421SMatthew Ahrens if (error == 0)
488114843421SMatthew Ahrens error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
488214843421SMatthew Ahrens VFS_RELE(zfsvfs->z_vfs);
488314843421SMatthew Ahrens } else {
4884503ad85cSMatthew Ahrens /* XXX kind of reading contents without owning */
4885503ad85cSMatthew Ahrens error = dmu_objset_hold(zc->zc_name, FTAG, &os);
48863b2aab18SMatthew Ahrens if (error != 0)
488714843421SMatthew Ahrens return (error);
488814843421SMatthew Ahrens
488914843421SMatthew Ahrens error = dmu_objset_userspace_upgrade(os);
4890503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
489114843421SMatthew Ahrens }
489214843421SMatthew Ahrens
489314843421SMatthew Ahrens return (error);
489414843421SMatthew Ahrens }
489514843421SMatthew Ahrens
489614843421SMatthew Ahrens /*
4897ecd6cf80Smarks * We don't want to have a hard dependency
4898ecd6cf80Smarks * against some special symbols in sharefs
4899da6c28aaSamw * nfs, and smbsrv. Determine them if needed when
4900ecd6cf80Smarks * the first file system is shared.
4901da6c28aaSamw * Neither sharefs, nfs or smbsrv are unloadable modules.
4902ecd6cf80Smarks */
4903da6c28aaSamw int (*znfsexport_fs)(void *arg);
4904ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4905da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4906ecd6cf80Smarks
4907da6c28aaSamw int zfs_nfsshare_inited;
4908da6c28aaSamw int zfs_smbshare_inited;
4909da6c28aaSamw
4910ecd6cf80Smarks ddi_modhandle_t nfs_mod;
4911ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
4912da6c28aaSamw ddi_modhandle_t smbsrv_mod;
4913ecd6cf80Smarks kmutex_t zfs_share_lock;
4914ecd6cf80Smarks
4915ecd6cf80Smarks static int
zfs_init_sharefs()4916da6c28aaSamw zfs_init_sharefs()
4917da6c28aaSamw {
4918da6c28aaSamw int error;
4919da6c28aaSamw
4920da6c28aaSamw ASSERT(MUTEX_HELD(&zfs_share_lock));
4921da6c28aaSamw /* Both NFS and SMB shares also require sharetab support. */
4922da6c28aaSamw if (sharefs_mod == NULL && ((sharefs_mod =
4923da6c28aaSamw ddi_modopen("fs/sharefs",
4924da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) {
4925be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS));
4926da6c28aaSamw }
4927da6c28aaSamw if (zshare_fs == NULL && ((zshare_fs =
4928da6c28aaSamw (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4929da6c28aaSamw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4930be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS));
4931da6c28aaSamw }
4932da6c28aaSamw return (0);
4933da6c28aaSamw }
4934da6c28aaSamw
4935da6c28aaSamw static int
zfs_ioc_share(zfs_cmd_t * zc)4936ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
4937ecd6cf80Smarks {
4938ecd6cf80Smarks int error;
4939ecd6cf80Smarks int opcode;
4940ecd6cf80Smarks
4941da6c28aaSamw switch (zc->zc_share.z_sharetype) {
4942da6c28aaSamw case ZFS_SHARE_NFS:
4943da6c28aaSamw case ZFS_UNSHARE_NFS:
4944da6c28aaSamw if (zfs_nfsshare_inited == 0) {
4945ecd6cf80Smarks mutex_enter(&zfs_share_lock);
4946da6c28aaSamw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4947da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) {
4948ecd6cf80Smarks mutex_exit(&zfs_share_lock);
4949be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS));
4950ecd6cf80Smarks }
4951da6c28aaSamw if (znfsexport_fs == NULL &&
4952da6c28aaSamw ((znfsexport_fs = (int (*)(void *))
4953da6c28aaSamw ddi_modsym(nfs_mod,
4954da6c28aaSamw "nfs_export", &error)) == NULL)) {
4955ecd6cf80Smarks mutex_exit(&zfs_share_lock);
4956be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS));
4957ecd6cf80Smarks }
4958da6c28aaSamw error = zfs_init_sharefs();
49593b2aab18SMatthew Ahrens if (error != 0) {
4960da6c28aaSamw mutex_exit(&zfs_share_lock);
4961be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS));
4962da6c28aaSamw }
4963da6c28aaSamw zfs_nfsshare_inited = 1;
4964da6c28aaSamw mutex_exit(&zfs_share_lock);
4965da6c28aaSamw }
4966da6c28aaSamw break;
4967da6c28aaSamw case ZFS_SHARE_SMB:
4968da6c28aaSamw case ZFS_UNSHARE_SMB:
4969da6c28aaSamw if (zfs_smbshare_inited == 0) {
4970da6c28aaSamw mutex_enter(&zfs_share_lock);
4971da6c28aaSamw if (smbsrv_mod == NULL && ((smbsrv_mod =
4972da6c28aaSamw ddi_modopen("drv/smbsrv",
4973da6c28aaSamw KRTLD_MODE_FIRST, &error)) == NULL)) {
4974da6c28aaSamw mutex_exit(&zfs_share_lock);
4975be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS));
4976da6c28aaSamw }
4977da6c28aaSamw if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4978da6c28aaSamw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4979faa1795aSjb150015 "smb_server_share", &error)) == NULL)) {
4980da6c28aaSamw mutex_exit(&zfs_share_lock);
4981be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS));
4982da6c28aaSamw }
4983da6c28aaSamw error = zfs_init_sharefs();
49843b2aab18SMatthew Ahrens if (error != 0) {
4985da6c28aaSamw mutex_exit(&zfs_share_lock);
4986be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSYS));
4987da6c28aaSamw }
4988da6c28aaSamw zfs_smbshare_inited = 1;
4989da6c28aaSamw mutex_exit(&zfs_share_lock);
4990da6c28aaSamw }
4991da6c28aaSamw break;
4992da6c28aaSamw default:
4993be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
4994da6c28aaSamw }
4995ecd6cf80Smarks
4996da6c28aaSamw switch (zc->zc_share.z_sharetype) {
4997da6c28aaSamw case ZFS_SHARE_NFS:
4998da6c28aaSamw case ZFS_UNSHARE_NFS:
4999da6c28aaSamw if (error =
5000da6c28aaSamw znfsexport_fs((void *)
5001da6c28aaSamw (uintptr_t)zc->zc_share.z_exportdata))
5002ecd6cf80Smarks return (error);
5003da6c28aaSamw break;
5004da6c28aaSamw case ZFS_SHARE_SMB:
5005da6c28aaSamw case ZFS_UNSHARE_SMB:
5006da6c28aaSamw if (error = zsmbexport_fs((void *)
5007da6c28aaSamw (uintptr_t)zc->zc_share.z_exportdata,
5008da6c28aaSamw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5009da6c28aaSamw B_TRUE: B_FALSE)) {
5010da6c28aaSamw return (error);
5011da6c28aaSamw }
5012da6c28aaSamw break;
5013da6c28aaSamw }
5014ecd6cf80Smarks
5015da6c28aaSamw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5016da6c28aaSamw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5017ecd6cf80Smarks SHAREFS_ADD : SHAREFS_REMOVE;
5018ecd6cf80Smarks
5019da6c28aaSamw /*
5020da6c28aaSamw * Add or remove share from sharetab
5021da6c28aaSamw */
5022ecd6cf80Smarks error = zshare_fs(opcode,
5023ecd6cf80Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata,
5024ecd6cf80Smarks zc->zc_share.z_sharemax);
5025ecd6cf80Smarks
5026ecd6cf80Smarks return (error);
5027ecd6cf80Smarks
5028ecd6cf80Smarks }
5029ecd6cf80Smarks
5030743a77edSAlan Wright ace_t full_access[] = {
5031743a77edSAlan Wright {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5032743a77edSAlan Wright };
5033743a77edSAlan Wright
5034743a77edSAlan Wright /*
503599d5e173STim Haley * inputs:
503699d5e173STim Haley * zc_name name of containing filesystem
503799d5e173STim Haley * zc_obj object # beyond which we want next in-use object #
503899d5e173STim Haley *
503999d5e173STim Haley * outputs:
504099d5e173STim Haley * zc_obj next in-use object #
504199d5e173STim Haley */
504299d5e173STim Haley static int
zfs_ioc_next_obj(zfs_cmd_t * zc)504399d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
504499d5e173STim Haley {
504599d5e173STim Haley objset_t *os = NULL;
504699d5e173STim Haley int error;
504799d5e173STim Haley
504899d5e173STim Haley error = dmu_objset_hold(zc->zc_name, FTAG, &os);
50493b2aab18SMatthew Ahrens if (error != 0)
505099d5e173STim Haley return (error);
505199d5e173STim Haley
505299d5e173STim Haley error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5053c1379625SJustin T. Gibbs dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
505499d5e173STim Haley
505599d5e173STim Haley dmu_objset_rele(os, FTAG);
505699d5e173STim Haley return (error);
505799d5e173STim Haley }
505899d5e173STim Haley
505999d5e173STim Haley /*
506099d5e173STim Haley * inputs:
506199d5e173STim Haley * zc_name name of filesystem
506299d5e173STim Haley * zc_value prefix name for snapshot
506399d5e173STim Haley * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
506499d5e173STim Haley *
506599d5e173STim Haley * outputs:
50664445fffbSMatthew Ahrens * zc_value short name of new snapshot
506799d5e173STim Haley */
506899d5e173STim Haley static int
zfs_ioc_tmp_snapshot(zfs_cmd_t * zc)506999d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
507099d5e173STim Haley {
507199d5e173STim Haley char *snap_name;
50723b2aab18SMatthew Ahrens char *hold_name;
507399d5e173STim Haley int error;
50743b2aab18SMatthew Ahrens minor_t minor;
507599d5e173STim Haley
50763b2aab18SMatthew Ahrens error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
50773b2aab18SMatthew Ahrens if (error != 0)
507899d5e173STim Haley return (error);
507999d5e173STim Haley
50803b2aab18SMatthew Ahrens snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
50813b2aab18SMatthew Ahrens (u_longlong_t)ddi_get_lbolt64());
50823b2aab18SMatthew Ahrens hold_name = kmem_asprintf("%%%s", zc->zc_value);
50833b2aab18SMatthew Ahrens
50843b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
50853b2aab18SMatthew Ahrens hold_name);
50863b2aab18SMatthew Ahrens if (error == 0)
50873b2aab18SMatthew Ahrens (void) strcpy(zc->zc_value, snap_name);
508899d5e173STim Haley strfree(snap_name);
50893b2aab18SMatthew Ahrens strfree(hold_name);
50903b2aab18SMatthew Ahrens zfs_onexit_fd_rele(zc->zc_cleanup_fd);
50913b2aab18SMatthew Ahrens return (error);
509299d5e173STim Haley }
509399d5e173STim Haley
509499d5e173STim Haley /*
509599d5e173STim Haley * inputs:
509699d5e173STim Haley * zc_name name of "to" snapshot
509799d5e173STim Haley * zc_value name of "from" snapshot
509899d5e173STim Haley * zc_cookie file descriptor to write diff data on
509999d5e173STim Haley *
510099d5e173STim Haley * outputs:
510199d5e173STim Haley * dmu_diff_record_t's to the file descriptor
510299d5e173STim Haley */
510399d5e173STim Haley static int
zfs_ioc_diff(zfs_cmd_t * zc)510499d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
510599d5e173STim Haley {
510699d5e173STim Haley file_t *fp;
510799d5e173STim Haley offset_t off;
510899d5e173STim Haley int error;
510999d5e173STim Haley
511099d5e173STim Haley fp = getf(zc->zc_cookie);
51113b2aab18SMatthew Ahrens if (fp == NULL)
5112be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF));
511399d5e173STim Haley
511499d5e173STim Haley off = fp->f_offset;
511599d5e173STim Haley
51163b2aab18SMatthew Ahrens error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
511799d5e173STim Haley
511899d5e173STim Haley if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
511999d5e173STim Haley fp->f_offset = off;
512099d5e173STim Haley releasef(zc->zc_cookie);
512199d5e173STim Haley
512299d5e173STim Haley return (error);
512399d5e173STim Haley }
512499d5e173STim Haley
512599d5e173STim Haley /*
5126743a77edSAlan Wright * Remove all ACL files in shares dir
5127743a77edSAlan Wright */
5128743a77edSAlan Wright static int
zfs_smb_acl_purge(znode_t * dzp)5129743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
5130743a77edSAlan Wright {
5131743a77edSAlan Wright zap_cursor_t zc;
5132743a77edSAlan Wright zap_attribute_t zap;
5133743a77edSAlan Wright zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5134743a77edSAlan Wright int error;
5135743a77edSAlan Wright
5136743a77edSAlan Wright for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5137743a77edSAlan Wright (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5138743a77edSAlan Wright zap_cursor_advance(&zc)) {
5139743a77edSAlan Wright if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5140743a77edSAlan Wright NULL, 0)) != 0)
5141743a77edSAlan Wright break;
5142743a77edSAlan Wright }
5143743a77edSAlan Wright zap_cursor_fini(&zc);
5144743a77edSAlan Wright return (error);
5145743a77edSAlan Wright }
5146743a77edSAlan Wright
5147743a77edSAlan Wright static int
zfs_ioc_smb_acl(zfs_cmd_t * zc)5148743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
5149743a77edSAlan Wright {
5150743a77edSAlan Wright vnode_t *vp;
5151743a77edSAlan Wright znode_t *dzp;
5152743a77edSAlan Wright vnode_t *resourcevp = NULL;
5153743a77edSAlan Wright znode_t *sharedir;
5154743a77edSAlan Wright zfsvfs_t *zfsvfs;
5155743a77edSAlan Wright nvlist_t *nvlist;
5156743a77edSAlan Wright char *src, *target;
5157743a77edSAlan Wright vattr_t vattr;
5158743a77edSAlan Wright vsecattr_t vsec;
5159743a77edSAlan Wright int error = 0;
5160743a77edSAlan Wright
5161743a77edSAlan Wright if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5162743a77edSAlan Wright NO_FOLLOW, NULL, &vp)) != 0)
5163743a77edSAlan Wright return (error);
5164743a77edSAlan Wright
5165743a77edSAlan Wright /* Now make sure mntpnt and dataset are ZFS */
5166743a77edSAlan Wright
5167743a77edSAlan Wright if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5168743a77edSAlan Wright (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5169743a77edSAlan Wright zc->zc_name) != 0)) {
5170743a77edSAlan Wright VN_RELE(vp);
5171be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
5172743a77edSAlan Wright }
5173743a77edSAlan Wright
5174743a77edSAlan Wright dzp = VTOZ(vp);
5175743a77edSAlan Wright zfsvfs = dzp->z_zfsvfs;
5176743a77edSAlan Wright ZFS_ENTER(zfsvfs);
5177743a77edSAlan Wright
51789e1320c0SMark Shellenbaum /*
51799e1320c0SMark Shellenbaum * Create share dir if its missing.
51809e1320c0SMark Shellenbaum */
51819e1320c0SMark Shellenbaum mutex_enter(&zfsvfs->z_lock);
51829e1320c0SMark Shellenbaum if (zfsvfs->z_shares_dir == 0) {
51839e1320c0SMark Shellenbaum dmu_tx_t *tx;
51849e1320c0SMark Shellenbaum
51859e1320c0SMark Shellenbaum tx = dmu_tx_create(zfsvfs->z_os);
51869e1320c0SMark Shellenbaum dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
51879e1320c0SMark Shellenbaum ZFS_SHARES_DIR);
51889e1320c0SMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
51899e1320c0SMark Shellenbaum error = dmu_tx_assign(tx, TXG_WAIT);
51903b2aab18SMatthew Ahrens if (error != 0) {
51919e1320c0SMark Shellenbaum dmu_tx_abort(tx);
51929e1320c0SMark Shellenbaum } else {
51939e1320c0SMark Shellenbaum error = zfs_create_share_dir(zfsvfs, tx);
51949e1320c0SMark Shellenbaum dmu_tx_commit(tx);
51959e1320c0SMark Shellenbaum }
51963b2aab18SMatthew Ahrens if (error != 0) {
51979e1320c0SMark Shellenbaum mutex_exit(&zfsvfs->z_lock);
51989e1320c0SMark Shellenbaum VN_RELE(vp);
51999e1320c0SMark Shellenbaum ZFS_EXIT(zfsvfs);
52009e1320c0SMark Shellenbaum return (error);
52019e1320c0SMark Shellenbaum }
52029e1320c0SMark Shellenbaum }
52039e1320c0SMark Shellenbaum mutex_exit(&zfsvfs->z_lock);
52049e1320c0SMark Shellenbaum
52059e1320c0SMark Shellenbaum ASSERT(zfsvfs->z_shares_dir);
5206743a77edSAlan Wright if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
52079e1320c0SMark Shellenbaum VN_RELE(vp);
5208743a77edSAlan Wright ZFS_EXIT(zfsvfs);
5209743a77edSAlan Wright return (error);
5210743a77edSAlan Wright }
5211743a77edSAlan Wright
5212743a77edSAlan Wright switch (zc->zc_cookie) {
5213743a77edSAlan Wright case ZFS_SMB_ACL_ADD:
5214743a77edSAlan Wright vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5215743a77edSAlan Wright vattr.va_type = VREG;
5216743a77edSAlan Wright vattr.va_mode = S_IFREG|0777;
5217743a77edSAlan Wright vattr.va_uid = 0;
5218743a77edSAlan Wright vattr.va_gid = 0;
5219743a77edSAlan Wright
5220743a77edSAlan Wright vsec.vsa_mask = VSA_ACE;
5221743a77edSAlan Wright vsec.vsa_aclentp = &full_access;
5222743a77edSAlan Wright vsec.vsa_aclentsz = sizeof (full_access);
5223743a77edSAlan Wright vsec.vsa_aclcnt = 1;
5224743a77edSAlan Wright
5225743a77edSAlan Wright error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5226743a77edSAlan Wright &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5227743a77edSAlan Wright if (resourcevp)
5228743a77edSAlan Wright VN_RELE(resourcevp);
5229743a77edSAlan Wright break;
5230743a77edSAlan Wright
5231743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE:
5232743a77edSAlan Wright error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5233743a77edSAlan Wright NULL, 0);
5234743a77edSAlan Wright break;
5235743a77edSAlan Wright
5236743a77edSAlan Wright case ZFS_SMB_ACL_RENAME:
5237743a77edSAlan Wright if ((error = get_nvlist(zc->zc_nvlist_src,
5238478ed9adSEric Taylor zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5239743a77edSAlan Wright VN_RELE(vp);
52408f5190a5SDan McDonald VN_RELE(ZTOV(sharedir));
5241743a77edSAlan Wright ZFS_EXIT(zfsvfs);
5242743a77edSAlan Wright return (error);
5243743a77edSAlan Wright }
5244743a77edSAlan Wright if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5245743a77edSAlan Wright nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5246743a77edSAlan Wright &target)) {
5247743a77edSAlan Wright VN_RELE(vp);
524889459e17SMark Shellenbaum VN_RELE(ZTOV(sharedir));
5249743a77edSAlan Wright ZFS_EXIT(zfsvfs);
52501195e687SMark J Musante nvlist_free(nvlist);
5251743a77edSAlan Wright return (error);
5252743a77edSAlan Wright }
5253743a77edSAlan Wright error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5254743a77edSAlan Wright kcred, NULL, 0);
5255743a77edSAlan Wright nvlist_free(nvlist);
5256743a77edSAlan Wright break;
5257743a77edSAlan Wright
5258743a77edSAlan Wright case ZFS_SMB_ACL_PURGE:
5259743a77edSAlan Wright error = zfs_smb_acl_purge(sharedir);
5260743a77edSAlan Wright break;
5261743a77edSAlan Wright
5262743a77edSAlan Wright default:
5263be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
5264743a77edSAlan Wright break;
5265743a77edSAlan Wright }
5266743a77edSAlan Wright
5267743a77edSAlan Wright VN_RELE(vp);
5268743a77edSAlan Wright VN_RELE(ZTOV(sharedir));
5269743a77edSAlan Wright
5270743a77edSAlan Wright ZFS_EXIT(zfsvfs);
5271743a77edSAlan Wright
5272743a77edSAlan Wright return (error);
5273743a77edSAlan Wright }
5274743a77edSAlan Wright
5275ecd6cf80Smarks /*
52763b2aab18SMatthew Ahrens * innvl: {
52773b2aab18SMatthew Ahrens * "holds" -> { snapname -> holdname (string), ... }
52783b2aab18SMatthew Ahrens * (optional) "cleanup_fd" -> fd (int32)
52793b2aab18SMatthew Ahrens * }
5280842727c2SChris Kirby *
52813b2aab18SMatthew Ahrens * outnvl: {
52823b2aab18SMatthew Ahrens * snapname -> error value (int32)
52833b2aab18SMatthew Ahrens * ...
52843b2aab18SMatthew Ahrens * }
5285842727c2SChris Kirby */
52863b2aab18SMatthew Ahrens /* ARGSUSED */
5287842727c2SChris Kirby static int
zfs_ioc_hold(const char * pool,nvlist_t * args,nvlist_t * errlist)52883b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5289842727c2SChris Kirby {
5290752fd8daSJosef 'Jeff' Sipek nvpair_t *pair;
52913b2aab18SMatthew Ahrens nvlist_t *holds;
52923b2aab18SMatthew Ahrens int cleanup_fd = -1;
5293a7f53a56SChris Kirby int error;
5294a7f53a56SChris Kirby minor_t minor = 0;
5295842727c2SChris Kirby
52963b2aab18SMatthew Ahrens error = nvlist_lookup_nvlist(args, "holds", &holds);
52973b2aab18SMatthew Ahrens if (error != 0)
5298be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
5299842727c2SChris Kirby
5300752fd8daSJosef 'Jeff' Sipek /* make sure the user didn't pass us any invalid (empty) tags */
5301752fd8daSJosef 'Jeff' Sipek for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5302752fd8daSJosef 'Jeff' Sipek pair = nvlist_next_nvpair(holds, pair)) {
5303752fd8daSJosef 'Jeff' Sipek char *htag;
5304752fd8daSJosef 'Jeff' Sipek
5305752fd8daSJosef 'Jeff' Sipek error = nvpair_value_string(pair, &htag);
5306752fd8daSJosef 'Jeff' Sipek if (error != 0)
5307752fd8daSJosef 'Jeff' Sipek return (SET_ERROR(error));
5308752fd8daSJosef 'Jeff' Sipek
5309752fd8daSJosef 'Jeff' Sipek if (strlen(htag) == 0)
5310752fd8daSJosef 'Jeff' Sipek return (SET_ERROR(EINVAL));
5311752fd8daSJosef 'Jeff' Sipek }
5312752fd8daSJosef 'Jeff' Sipek
53133b2aab18SMatthew Ahrens if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
53143b2aab18SMatthew Ahrens error = zfs_onexit_fd_hold(cleanup_fd, &minor);
53153b2aab18SMatthew Ahrens if (error != 0)
5316a7f53a56SChris Kirby return (error);
5317a7f53a56SChris Kirby }
5318a7f53a56SChris Kirby
53193b2aab18SMatthew Ahrens error = dsl_dataset_user_hold(holds, minor, errlist);
53203b2aab18SMatthew Ahrens if (minor != 0)
53213b2aab18SMatthew Ahrens zfs_onexit_fd_rele(cleanup_fd);
5322a7f53a56SChris Kirby return (error);
5323842727c2SChris Kirby }
5324842727c2SChris Kirby
5325842727c2SChris Kirby /*
53263b2aab18SMatthew Ahrens * innvl is not used.
5327842727c2SChris Kirby *
53283b2aab18SMatthew Ahrens * outnvl: {
53293b2aab18SMatthew Ahrens * holdname -> time added (uint64 seconds since epoch)
53303b2aab18SMatthew Ahrens * ...
53313b2aab18SMatthew Ahrens * }
5332842727c2SChris Kirby */
53333b2aab18SMatthew Ahrens /* ARGSUSED */
5334842727c2SChris Kirby static int
zfs_ioc_get_holds(const char * snapname,nvlist_t * args,nvlist_t * outnvl)53353b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5336842727c2SChris Kirby {
53373b2aab18SMatthew Ahrens return (dsl_dataset_get_holds(snapname, outnvl));
5338842727c2SChris Kirby }
5339842727c2SChris Kirby
5340842727c2SChris Kirby /*
53413b2aab18SMatthew Ahrens * innvl: {
53423b2aab18SMatthew Ahrens * snapname -> { holdname, ... }
53433b2aab18SMatthew Ahrens * ...
53443b2aab18SMatthew Ahrens * }
5345842727c2SChris Kirby *
53463b2aab18SMatthew Ahrens * outnvl: {
53473b2aab18SMatthew Ahrens * snapname -> error value (int32)
53483b2aab18SMatthew Ahrens * ...
53493b2aab18SMatthew Ahrens * }
5350842727c2SChris Kirby */
53513b2aab18SMatthew Ahrens /* ARGSUSED */
5352842727c2SChris Kirby static int
zfs_ioc_release(const char * pool,nvlist_t * holds,nvlist_t * errlist)53533b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5354842727c2SChris Kirby {
53553b2aab18SMatthew Ahrens return (dsl_dataset_user_release(holds, errlist));
5356842727c2SChris Kirby }
5357842727c2SChris Kirby
5358842727c2SChris Kirby /*
535919b94df9SMatthew Ahrens * inputs:
536019b94df9SMatthew Ahrens * zc_name name of new filesystem or snapshot
536119b94df9SMatthew Ahrens * zc_value full name of old snapshot
536219b94df9SMatthew Ahrens *
536319b94df9SMatthew Ahrens * outputs:
536419b94df9SMatthew Ahrens * zc_cookie space in bytes
536519b94df9SMatthew Ahrens * zc_objset_type compressed space in bytes
536619b94df9SMatthew Ahrens * zc_perm_action uncompressed space in bytes
536719b94df9SMatthew Ahrens */
536819b94df9SMatthew Ahrens static int
zfs_ioc_space_written(zfs_cmd_t * zc)536919b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
537019b94df9SMatthew Ahrens {
537119b94df9SMatthew Ahrens int error;
53723b2aab18SMatthew Ahrens dsl_pool_t *dp;
537319b94df9SMatthew Ahrens dsl_dataset_t *new, *old;
537419b94df9SMatthew Ahrens
53753b2aab18SMatthew Ahrens error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
537619b94df9SMatthew Ahrens if (error != 0)
537719b94df9SMatthew Ahrens return (error);
53783b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
53793b2aab18SMatthew Ahrens if (error != 0) {
53803b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
53813b2aab18SMatthew Ahrens return (error);
53823b2aab18SMatthew Ahrens }
53833b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
538419b94df9SMatthew Ahrens if (error != 0) {
538519b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG);
53863b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
538719b94df9SMatthew Ahrens return (error);
538819b94df9SMatthew Ahrens }
538919b94df9SMatthew Ahrens
539019b94df9SMatthew Ahrens error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
539119b94df9SMatthew Ahrens &zc->zc_objset_type, &zc->zc_perm_action);
539219b94df9SMatthew Ahrens dsl_dataset_rele(old, FTAG);
539319b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG);
53943b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
539519b94df9SMatthew Ahrens return (error);
539619b94df9SMatthew Ahrens }
53973b2aab18SMatthew Ahrens
539819b94df9SMatthew Ahrens /*
53994445fffbSMatthew Ahrens * innvl: {
54004445fffbSMatthew Ahrens * "firstsnap" -> snapshot name
54014445fffbSMatthew Ahrens * }
540219b94df9SMatthew Ahrens *
54034445fffbSMatthew Ahrens * outnvl: {
54044445fffbSMatthew Ahrens * "used" -> space in bytes
54054445fffbSMatthew Ahrens * "compressed" -> compressed space in bytes
54064445fffbSMatthew Ahrens * "uncompressed" -> uncompressed space in bytes
54074445fffbSMatthew Ahrens * }
540819b94df9SMatthew Ahrens */
540919b94df9SMatthew Ahrens static int
zfs_ioc_space_snaps(const char * lastsnap,nvlist_t * innvl,nvlist_t * outnvl)54104445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
541119b94df9SMatthew Ahrens {
541219b94df9SMatthew Ahrens int error;
54133b2aab18SMatthew Ahrens dsl_pool_t *dp;
541419b94df9SMatthew Ahrens dsl_dataset_t *new, *old;
54154445fffbSMatthew Ahrens char *firstsnap;
54164445fffbSMatthew Ahrens uint64_t used, comp, uncomp;
541719b94df9SMatthew Ahrens
54184445fffbSMatthew Ahrens if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5419be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
54204445fffbSMatthew Ahrens
54213b2aab18SMatthew Ahrens error = dsl_pool_hold(lastsnap, FTAG, &dp);
542219b94df9SMatthew Ahrens if (error != 0)
542319b94df9SMatthew Ahrens return (error);
54243b2aab18SMatthew Ahrens
54253b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
542624218bebSAndriy Gapon if (error == 0 && !new->ds_is_snapshot) {
542724218bebSAndriy Gapon dsl_dataset_rele(new, FTAG);
542824218bebSAndriy Gapon error = SET_ERROR(EINVAL);
542924218bebSAndriy Gapon }
54303b2aab18SMatthew Ahrens if (error != 0) {
54313b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
54323b2aab18SMatthew Ahrens return (error);
54333b2aab18SMatthew Ahrens }
54343b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
543524218bebSAndriy Gapon if (error == 0 && !old->ds_is_snapshot) {
543624218bebSAndriy Gapon dsl_dataset_rele(old, FTAG);
543724218bebSAndriy Gapon error = SET_ERROR(EINVAL);
543824218bebSAndriy Gapon }
543919b94df9SMatthew Ahrens if (error != 0) {
544019b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG);
54413b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
544219b94df9SMatthew Ahrens return (error);
544319b94df9SMatthew Ahrens }
544419b94df9SMatthew Ahrens
54454445fffbSMatthew Ahrens error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
544619b94df9SMatthew Ahrens dsl_dataset_rele(old, FTAG);
544719b94df9SMatthew Ahrens dsl_dataset_rele(new, FTAG);
54483b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
54494445fffbSMatthew Ahrens fnvlist_add_uint64(outnvl, "used", used);
54504445fffbSMatthew Ahrens fnvlist_add_uint64(outnvl, "compressed", comp);
54514445fffbSMatthew Ahrens fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
545219b94df9SMatthew Ahrens return (error);
545319b94df9SMatthew Ahrens }
545419b94df9SMatthew Ahrens
545519b94df9SMatthew Ahrens /*
54564445fffbSMatthew Ahrens * innvl: {
54574445fffbSMatthew Ahrens * "fd" -> file descriptor to write stream to (int32)
54584445fffbSMatthew Ahrens * (optional) "fromsnap" -> full snap name to send an incremental from
5459b5152584SMatthew Ahrens * (optional) "largeblockok" -> (value ignored)
5460b5152584SMatthew Ahrens * indicates that blocks > 128KB are permitted
54615d7b4d43SMatthew Ahrens * (optional) "embedok" -> (value ignored)
54625d7b4d43SMatthew Ahrens * presence indicates DRR_WRITE_EMBEDDED records are permitted
54639c3fd121SMatthew Ahrens * (optional) "resume_object" and "resume_offset" -> (uint64)
54649c3fd121SMatthew Ahrens * if present, resume send stream from specified object and offset.
54654445fffbSMatthew Ahrens * }
54664445fffbSMatthew Ahrens *
54674445fffbSMatthew Ahrens * outnvl is unused
5468ecd6cf80Smarks */
54694445fffbSMatthew Ahrens /* ARGSUSED */
54704445fffbSMatthew Ahrens static int
zfs_ioc_send_new(const char * snapname,nvlist_t * innvl,nvlist_t * outnvl)54714445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
54724445fffbSMatthew Ahrens {
54734445fffbSMatthew Ahrens int error;
54744445fffbSMatthew Ahrens offset_t off;
54753b2aab18SMatthew Ahrens char *fromname = NULL;
54764445fffbSMatthew Ahrens int fd;
5477b5152584SMatthew Ahrens boolean_t largeblockok;
54785d7b4d43SMatthew Ahrens boolean_t embedok;
54799c3fd121SMatthew Ahrens uint64_t resumeobj = 0;
54809c3fd121SMatthew Ahrens uint64_t resumeoff = 0;
54814445fffbSMatthew Ahrens
54824445fffbSMatthew Ahrens error = nvlist_lookup_int32(innvl, "fd", &fd);
54834445fffbSMatthew Ahrens if (error != 0)
5484be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
54854445fffbSMatthew Ahrens
54863b2aab18SMatthew Ahrens (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
54874445fffbSMatthew Ahrens
5488b5152584SMatthew Ahrens largeblockok = nvlist_exists(innvl, "largeblockok");
54895d7b4d43SMatthew Ahrens embedok = nvlist_exists(innvl, "embedok");
54905d7b4d43SMatthew Ahrens
54919c3fd121SMatthew Ahrens (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
54929c3fd121SMatthew Ahrens (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
54939c3fd121SMatthew Ahrens
54944445fffbSMatthew Ahrens file_t *fp = getf(fd);
54953b2aab18SMatthew Ahrens if (fp == NULL)
5496be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF));
54974445fffbSMatthew Ahrens
54984445fffbSMatthew Ahrens off = fp->f_offset;
54999c3fd121SMatthew Ahrens error = dmu_send(snapname, fromname, embedok, largeblockok, fd,
55009c3fd121SMatthew Ahrens resumeobj, resumeoff, fp->f_vnode, &off);
55014445fffbSMatthew Ahrens
55024445fffbSMatthew Ahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
55034445fffbSMatthew Ahrens fp->f_offset = off;
55044445fffbSMatthew Ahrens releasef(fd);
55054445fffbSMatthew Ahrens return (error);
55064445fffbSMatthew Ahrens }
55074445fffbSMatthew Ahrens
55084445fffbSMatthew Ahrens /*
55094445fffbSMatthew Ahrens * Determine approximately how large a zfs send stream will be -- the number
55104445fffbSMatthew Ahrens * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
55114445fffbSMatthew Ahrens *
55124445fffbSMatthew Ahrens * innvl: {
5513643da460SMax Grossman * (optional) "from" -> full snap or bookmark name to send an incremental
5514643da460SMax Grossman * from
55154445fffbSMatthew Ahrens * }
55164445fffbSMatthew Ahrens *
55174445fffbSMatthew Ahrens * outnvl: {
55184445fffbSMatthew Ahrens * "space" -> bytes of space (uint64)
55194445fffbSMatthew Ahrens * }
55204445fffbSMatthew Ahrens */
55214445fffbSMatthew Ahrens static int
zfs_ioc_send_space(const char * snapname,nvlist_t * innvl,nvlist_t * outnvl)55224445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
55234445fffbSMatthew Ahrens {
55243b2aab18SMatthew Ahrens dsl_pool_t *dp;
55253b2aab18SMatthew Ahrens dsl_dataset_t *tosnap;
55264445fffbSMatthew Ahrens int error;
55274445fffbSMatthew Ahrens char *fromname;
55284445fffbSMatthew Ahrens uint64_t space;
55294445fffbSMatthew Ahrens
55303b2aab18SMatthew Ahrens error = dsl_pool_hold(snapname, FTAG, &dp);
55313b2aab18SMatthew Ahrens if (error != 0)
55324445fffbSMatthew Ahrens return (error);
55334445fffbSMatthew Ahrens
55343b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
55353b2aab18SMatthew Ahrens if (error != 0) {
55363b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
55373b2aab18SMatthew Ahrens return (error);
55383b2aab18SMatthew Ahrens }
55393b2aab18SMatthew Ahrens
5540643da460SMax Grossman error = nvlist_lookup_string(innvl, "from", &fromname);
55414445fffbSMatthew Ahrens if (error == 0) {
5542643da460SMax Grossman if (strchr(fromname, '@') != NULL) {
5543643da460SMax Grossman /*
5544643da460SMax Grossman * If from is a snapshot, hold it and use the more
5545643da460SMax Grossman * efficient dmu_send_estimate to estimate send space
5546643da460SMax Grossman * size using deadlists.
5547643da460SMax Grossman */
5548643da460SMax Grossman dsl_dataset_t *fromsnap;
55493b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5550643da460SMax Grossman if (error != 0)
5551643da460SMax Grossman goto out;
5552643da460SMax Grossman error = dmu_send_estimate(tosnap, fromsnap, &space);
5553643da460SMax Grossman dsl_dataset_rele(fromsnap, FTAG);
5554643da460SMax Grossman } else if (strchr(fromname, '#') != NULL) {
5555643da460SMax Grossman /*
5556643da460SMax Grossman * If from is a bookmark, fetch the creation TXG of the
5557643da460SMax Grossman * snapshot it was created from and use that to find
5558643da460SMax Grossman * blocks that were born after it.
5559643da460SMax Grossman */
5560643da460SMax Grossman zfs_bookmark_phys_t frombm;
5561643da460SMax Grossman
5562643da460SMax Grossman error = dsl_bookmark_lookup(dp, fromname, tosnap,
5563643da460SMax Grossman &frombm);
5564643da460SMax Grossman if (error != 0)
5565643da460SMax Grossman goto out;
5566643da460SMax Grossman error = dmu_send_estimate_from_txg(tosnap,
5567643da460SMax Grossman frombm.zbm_creation_txg, &space);
5568643da460SMax Grossman } else {
5569643da460SMax Grossman /*
5570643da460SMax Grossman * from is not properly formatted as a snapshot or
5571643da460SMax Grossman * bookmark
5572643da460SMax Grossman */
5573643da460SMax Grossman error = SET_ERROR(EINVAL);
5574643da460SMax Grossman goto out;
55754445fffbSMatthew Ahrens }
5576643da460SMax Grossman } else {
5577643da460SMax Grossman // If estimating the size of a full send, use dmu_send_estimate
5578643da460SMax Grossman error = dmu_send_estimate(tosnap, NULL, &space);
55794445fffbSMatthew Ahrens }
55804445fffbSMatthew Ahrens
55814445fffbSMatthew Ahrens fnvlist_add_uint64(outnvl, "space", space);
55824445fffbSMatthew Ahrens
5583643da460SMax Grossman out:
55843b2aab18SMatthew Ahrens dsl_dataset_rele(tosnap, FTAG);
55853b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
55864445fffbSMatthew Ahrens return (error);
55874445fffbSMatthew Ahrens }
55884445fffbSMatthew Ahrens
5589d78b796cSAndreas Jaekel static int
zfs_ioc_set_zev_callbacks(const char * unused,nvlist_t * innvl,nvlist_t * outnvl)5590d78b796cSAndreas Jaekel zfs_ioc_set_zev_callbacks(const char *unused, nvlist_t *innvl,
5591d78b796cSAndreas Jaekel nvlist_t *outnvl)
5592d78b796cSAndreas Jaekel {
5593d78b796cSAndreas Jaekel int error;
5594d78b796cSAndreas Jaekel uint64_t cb_addr;
5595d78b796cSAndreas Jaekel /*
5596d78b796cSAndreas Jaekel * Our secpolicy for this op makes sure it's called in
5597d78b796cSAndreas Jaekel * kernel context, and that no other callbacks have
5598d78b796cSAndreas Jaekel * been registered, yet.
5599d78b796cSAndreas Jaekel */
5600d78b796cSAndreas Jaekel error = nvlist_lookup_uint64(innvl, "callbacks", &cb_addr);
5601d78b796cSAndreas Jaekel if (error != 0) {
5602d78b796cSAndreas Jaekel cmn_err(CE_WARN, "set_zev_callbacks nvlist lookup failed (%d)",
5603d78b796cSAndreas Jaekel error);
5604d78b796cSAndreas Jaekel return (error);
5605d78b796cSAndreas Jaekel }
5606d78b796cSAndreas Jaekel /* cb_addr is always a kernel memory address */
5607d78b796cSAndreas Jaekel rw_enter(&rz_zev_rwlock, RW_WRITER);
5608d78b796cSAndreas Jaekel if (rz_zev_callbacks != rz_zev_default_callbacks) {
5609d78b796cSAndreas Jaekel rw_exit(&rz_zev_rwlock);
5610d78b796cSAndreas Jaekel return (EBUSY);
5611d78b796cSAndreas Jaekel }
5612d78b796cSAndreas Jaekel rz_zev_callbacks = (void *)(uintptr_t)cb_addr;
5613d78b796cSAndreas Jaekel rw_exit(&rz_zev_rwlock);
5614d78b796cSAndreas Jaekel return (0);
5615d78b796cSAndreas Jaekel }
5616d78b796cSAndreas Jaekel
5617d78b796cSAndreas Jaekel static int
zfs_ioc_unset_zev_callbacks(const char * unused,nvlist_t * innvl,nvlist_t * outnvl)5618d78b796cSAndreas Jaekel zfs_ioc_unset_zev_callbacks(const char *unused, nvlist_t *innvl,
5619d78b796cSAndreas Jaekel nvlist_t *outnvl)
5620d78b796cSAndreas Jaekel {
5621d78b796cSAndreas Jaekel /*
5622d78b796cSAndreas Jaekel * Our secpolicy for this op makes sure it's called in
5623d78b796cSAndreas Jaekel * kernel context.
5624d78b796cSAndreas Jaekel */
5625d78b796cSAndreas Jaekel rw_enter(&rz_zev_rwlock, RW_WRITER);
5626d78b796cSAndreas Jaekel rz_zev_callbacks = rz_zev_default_callbacks;
5627d78b796cSAndreas Jaekel rw_exit(&rz_zev_rwlock);
5628d78b796cSAndreas Jaekel /* after mutex release, no thread is using the old table anymore. */
5629d78b796cSAndreas Jaekel return (0);
5630d78b796cSAndreas Jaekel }
5631d78b796cSAndreas Jaekel
56324445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
56334445fffbSMatthew Ahrens
56344445fffbSMatthew Ahrens static void
zfs_ioctl_register_legacy(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy,zfs_ioc_namecheck_t namecheck,boolean_t log_history,zfs_ioc_poolcheck_t pool_check)56354445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56364445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
56374445fffbSMatthew Ahrens boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
56384445fffbSMatthew Ahrens {
56394445fffbSMatthew Ahrens zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
56404445fffbSMatthew Ahrens
56414445fffbSMatthew Ahrens ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
56424445fffbSMatthew Ahrens ASSERT3U(ioc, <, ZFS_IOC_LAST);
56434445fffbSMatthew Ahrens ASSERT3P(vec->zvec_legacy_func, ==, NULL);
56444445fffbSMatthew Ahrens ASSERT3P(vec->zvec_func, ==, NULL);
56454445fffbSMatthew Ahrens
56464445fffbSMatthew Ahrens vec->zvec_legacy_func = func;
56474445fffbSMatthew Ahrens vec->zvec_secpolicy = secpolicy;
56484445fffbSMatthew Ahrens vec->zvec_namecheck = namecheck;
56494445fffbSMatthew Ahrens vec->zvec_allow_log = log_history;
56504445fffbSMatthew Ahrens vec->zvec_pool_check = pool_check;
56514445fffbSMatthew Ahrens }
56524445fffbSMatthew Ahrens
56534445fffbSMatthew Ahrens /*
56544445fffbSMatthew Ahrens * See the block comment at the beginning of this file for details on
56554445fffbSMatthew Ahrens * each argument to this function.
56564445fffbSMatthew Ahrens */
56574445fffbSMatthew Ahrens static void
zfs_ioctl_register(const char * name,zfs_ioc_t ioc,zfs_ioc_func_t * func,zfs_secpolicy_func_t * secpolicy,zfs_ioc_namecheck_t namecheck,zfs_ioc_poolcheck_t pool_check,boolean_t smush_outnvlist,boolean_t allow_log)56584445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
56594445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
56604445fffbSMatthew Ahrens zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
56614445fffbSMatthew Ahrens boolean_t allow_log)
56624445fffbSMatthew Ahrens {
56634445fffbSMatthew Ahrens zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
56644445fffbSMatthew Ahrens
56654445fffbSMatthew Ahrens ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
56664445fffbSMatthew Ahrens ASSERT3U(ioc, <, ZFS_IOC_LAST);
56674445fffbSMatthew Ahrens ASSERT3P(vec->zvec_legacy_func, ==, NULL);
56684445fffbSMatthew Ahrens ASSERT3P(vec->zvec_func, ==, NULL);
56694445fffbSMatthew Ahrens
56704445fffbSMatthew Ahrens /* if we are logging, the name must be valid */
56714445fffbSMatthew Ahrens ASSERT(!allow_log || namecheck != NO_NAME);
56724445fffbSMatthew Ahrens
56734445fffbSMatthew Ahrens vec->zvec_name = name;
56744445fffbSMatthew Ahrens vec->zvec_func = func;
56754445fffbSMatthew Ahrens vec->zvec_secpolicy = secpolicy;
56764445fffbSMatthew Ahrens vec->zvec_namecheck = namecheck;
56774445fffbSMatthew Ahrens vec->zvec_pool_check = pool_check;
56784445fffbSMatthew Ahrens vec->zvec_smush_outnvlist = smush_outnvlist;
56794445fffbSMatthew Ahrens vec->zvec_allow_log = allow_log;
56804445fffbSMatthew Ahrens }
56814445fffbSMatthew Ahrens
56824445fffbSMatthew Ahrens static void
zfs_ioctl_register_pool(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy,boolean_t log_history,zfs_ioc_poolcheck_t pool_check)56834445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56844445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
56854445fffbSMatthew Ahrens zfs_ioc_poolcheck_t pool_check)
56864445fffbSMatthew Ahrens {
56874445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy,
56884445fffbSMatthew Ahrens POOL_NAME, log_history, pool_check);
56894445fffbSMatthew Ahrens }
56904445fffbSMatthew Ahrens
56914445fffbSMatthew Ahrens static void
zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy,zfs_ioc_poolcheck_t pool_check)56924445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56934445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
56944445fffbSMatthew Ahrens {
56954445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy,
56964445fffbSMatthew Ahrens DATASET_NAME, B_FALSE, pool_check);
56974445fffbSMatthew Ahrens }
56984445fffbSMatthew Ahrens
56994445fffbSMatthew Ahrens static void
zfs_ioctl_register_pool_modify(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func)57004445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
57014445fffbSMatthew Ahrens {
57024445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
57034445fffbSMatthew Ahrens POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
57044445fffbSMatthew Ahrens }
57054445fffbSMatthew Ahrens
57064445fffbSMatthew Ahrens static void
zfs_ioctl_register_pool_meta(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)57074445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
57084445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy)
57094445fffbSMatthew Ahrens {
57104445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy,
57114445fffbSMatthew Ahrens NO_NAME, B_FALSE, POOL_CHECK_NONE);
57124445fffbSMatthew Ahrens }
57134445fffbSMatthew Ahrens
57144445fffbSMatthew Ahrens static void
zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)57154445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
57164445fffbSMatthew Ahrens zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
57174445fffbSMatthew Ahrens {
57184445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy,
57194445fffbSMatthew Ahrens DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
57204445fffbSMatthew Ahrens }
57214445fffbSMatthew Ahrens
57224445fffbSMatthew Ahrens static void
zfs_ioctl_register_dataset_read(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func)57234445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
57244445fffbSMatthew Ahrens {
57254445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
57264445fffbSMatthew Ahrens zfs_secpolicy_read);
57274445fffbSMatthew Ahrens }
57284445fffbSMatthew Ahrens
57294445fffbSMatthew Ahrens static void
zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)57304445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
57314445fffbSMatthew Ahrens zfs_secpolicy_func_t *secpolicy)
57324445fffbSMatthew Ahrens {
57334445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ioc, func, secpolicy,
57344445fffbSMatthew Ahrens DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
57354445fffbSMatthew Ahrens }
57364445fffbSMatthew Ahrens
57374445fffbSMatthew Ahrens static void
zfs_ioctl_init(void)57384445fffbSMatthew Ahrens zfs_ioctl_init(void)
57394445fffbSMatthew Ahrens {
57404445fffbSMatthew Ahrens zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
57414445fffbSMatthew Ahrens zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
57424445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57434445fffbSMatthew Ahrens
57444445fffbSMatthew Ahrens zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
57454445fffbSMatthew Ahrens zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
57464445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
57474445fffbSMatthew Ahrens
57484445fffbSMatthew Ahrens zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
57494445fffbSMatthew Ahrens zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
57504445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
57514445fffbSMatthew Ahrens
57524445fffbSMatthew Ahrens zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
57534445fffbSMatthew Ahrens zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
57544445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
57554445fffbSMatthew Ahrens
57564445fffbSMatthew Ahrens zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
57574445fffbSMatthew Ahrens zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
57584445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
57594445fffbSMatthew Ahrens
57604445fffbSMatthew Ahrens zfs_ioctl_register("create", ZFS_IOC_CREATE,
57614445fffbSMatthew Ahrens zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
57624445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57634445fffbSMatthew Ahrens
57644445fffbSMatthew Ahrens zfs_ioctl_register("clone", ZFS_IOC_CLONE,
57654445fffbSMatthew Ahrens zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
57664445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57674445fffbSMatthew Ahrens
57684445fffbSMatthew Ahrens zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
57694445fffbSMatthew Ahrens zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
57704445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57714445fffbSMatthew Ahrens
57723b2aab18SMatthew Ahrens zfs_ioctl_register("hold", ZFS_IOC_HOLD,
57733b2aab18SMatthew Ahrens zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
57743b2aab18SMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57753b2aab18SMatthew Ahrens zfs_ioctl_register("release", ZFS_IOC_RELEASE,
57763b2aab18SMatthew Ahrens zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
57773b2aab18SMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57783b2aab18SMatthew Ahrens
57793b2aab18SMatthew Ahrens zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
57803b2aab18SMatthew Ahrens zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
57813b2aab18SMatthew Ahrens POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
57823b2aab18SMatthew Ahrens
5783d78b796cSAndreas Jaekel zfs_ioctl_register("set_zev_callbacks", ZFS_IOC_SET_ZEV_CALLBACKS,
5784d78b796cSAndreas Jaekel zfs_ioc_set_zev_callbacks, zfs_secpolicy_set_zev_callbacks, NO_NAME,
5785d78b796cSAndreas Jaekel POOL_CHECK_NONE, B_TRUE, B_FALSE);
5786d78b796cSAndreas Jaekel
5787d78b796cSAndreas Jaekel zfs_ioctl_register("unset_zev_callbacks", ZFS_IOC_UNSET_ZEV_CALLBACKS,
5788d78b796cSAndreas Jaekel zfs_ioc_unset_zev_callbacks, zfs_secpolicy_unset_zev_callbacks,
5789d78b796cSAndreas Jaekel NO_NAME, POOL_CHECK_NONE, B_TRUE, B_FALSE);
5790a7027df1SMatthew Ahrens zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5791a7027df1SMatthew Ahrens zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5792a7027df1SMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5793a7027df1SMatthew Ahrens
579478f17100SMatthew Ahrens zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
579578f17100SMatthew Ahrens zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
579678f17100SMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
579778f17100SMatthew Ahrens
579878f17100SMatthew Ahrens zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
579978f17100SMatthew Ahrens zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
580078f17100SMatthew Ahrens POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
580178f17100SMatthew Ahrens
580278f17100SMatthew Ahrens zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
580378f17100SMatthew Ahrens zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
580478f17100SMatthew Ahrens POOL_NAME,
580578f17100SMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
580678f17100SMatthew Ahrens
58074445fffbSMatthew Ahrens /* IOCTLS that use the legacy function signature */
58084445fffbSMatthew Ahrens
58094445fffbSMatthew Ahrens zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
58104445fffbSMatthew Ahrens zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
58114445fffbSMatthew Ahrens
5812ce0d9371SArne Jansen zfs_ioctl_register_legacy(ZFS_IOC_ARC_INFO, zfs_ioc_arc_info,
5813ce0d9371SArne Jansen zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
5814ce0d9371SArne Jansen
58154445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
58164445fffbSMatthew Ahrens zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
58174445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
58184445fffbSMatthew Ahrens zfs_ioc_pool_scan);
58194445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
58204445fffbSMatthew Ahrens zfs_ioc_pool_upgrade);
58214445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
58224445fffbSMatthew Ahrens zfs_ioc_vdev_add);
58234445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
58244445fffbSMatthew Ahrens zfs_ioc_vdev_remove);
58254445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
58264445fffbSMatthew Ahrens zfs_ioc_vdev_set_state);
58274445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
58284445fffbSMatthew Ahrens zfs_ioc_vdev_attach);
58294445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
58304445fffbSMatthew Ahrens zfs_ioc_vdev_detach);
58314445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
58324445fffbSMatthew Ahrens zfs_ioc_vdev_setpath);
58334445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
58344445fffbSMatthew Ahrens zfs_ioc_vdev_setfru);
58354445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
58364445fffbSMatthew Ahrens zfs_ioc_pool_set_props);
58374445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
58384445fffbSMatthew Ahrens zfs_ioc_vdev_split);
58394445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
58404445fffbSMatthew Ahrens zfs_ioc_pool_reguid);
58414445fffbSMatthew Ahrens
58424445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
58434445fffbSMatthew Ahrens zfs_ioc_pool_configs, zfs_secpolicy_none);
58444445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
58454445fffbSMatthew Ahrens zfs_ioc_pool_tryimport, zfs_secpolicy_config);
58464445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
58474445fffbSMatthew Ahrens zfs_ioc_inject_fault, zfs_secpolicy_inject);
58484445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
58494445fffbSMatthew Ahrens zfs_ioc_clear_fault, zfs_secpolicy_inject);
58504445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
58514445fffbSMatthew Ahrens zfs_ioc_inject_list_next, zfs_secpolicy_inject);
58524445fffbSMatthew Ahrens
58534445fffbSMatthew Ahrens /*
58544445fffbSMatthew Ahrens * pool destroy, and export don't log the history as part of
58554445fffbSMatthew Ahrens * zfsdev_ioctl, but rather zfs_ioc_pool_export
58564445fffbSMatthew Ahrens * does the logging of those commands.
58574445fffbSMatthew Ahrens */
58584445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
58594445fffbSMatthew Ahrens zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
58604445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
58614445fffbSMatthew Ahrens zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
58624445fffbSMatthew Ahrens
58634445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
58644445fffbSMatthew Ahrens zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
58654445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
58664445fffbSMatthew Ahrens zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
58674445fffbSMatthew Ahrens
58684445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
58694445fffbSMatthew Ahrens zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
58704445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
58714445fffbSMatthew Ahrens zfs_ioc_dsobj_to_dsname,
58724445fffbSMatthew Ahrens zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
58734445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
58744445fffbSMatthew Ahrens zfs_ioc_pool_get_history,
58754445fffbSMatthew Ahrens zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
58764445fffbSMatthew Ahrens
58774445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
58784445fffbSMatthew Ahrens zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
58794445fffbSMatthew Ahrens
58804445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
588122e30981SGeorge Wilson zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
58824445fffbSMatthew Ahrens zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
58834445fffbSMatthew Ahrens zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
58844445fffbSMatthew Ahrens
58854445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
58864445fffbSMatthew Ahrens zfs_ioc_space_written);
58874445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
58884445fffbSMatthew Ahrens zfs_ioc_objset_recvd_props);
58894445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
58904445fffbSMatthew Ahrens zfs_ioc_next_obj);
58914445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
58924445fffbSMatthew Ahrens zfs_ioc_get_fsacl);
58934445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
58944445fffbSMatthew Ahrens zfs_ioc_objset_stats);
58954445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
58964445fffbSMatthew Ahrens zfs_ioc_objset_zplprops);
58974445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
58984445fffbSMatthew Ahrens zfs_ioc_dataset_list_next);
58994445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
59004445fffbSMatthew Ahrens zfs_ioc_snapshot_list_next);
59014445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
59024445fffbSMatthew Ahrens zfs_ioc_send_progress);
59034445fffbSMatthew Ahrens
59044445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
59054445fffbSMatthew Ahrens zfs_ioc_diff, zfs_secpolicy_diff);
59064445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
59074445fffbSMatthew Ahrens zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
59084445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
59094445fffbSMatthew Ahrens zfs_ioc_obj_to_path, zfs_secpolicy_diff);
59104445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
59114445fffbSMatthew Ahrens zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
59124445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
59134445fffbSMatthew Ahrens zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
59144445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
59154445fffbSMatthew Ahrens zfs_ioc_send, zfs_secpolicy_send);
59164445fffbSMatthew Ahrens
59174445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
59184445fffbSMatthew Ahrens zfs_secpolicy_none);
59194445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
59204445fffbSMatthew Ahrens zfs_secpolicy_destroy);
59214445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
59224445fffbSMatthew Ahrens zfs_secpolicy_rename);
59234445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
59244445fffbSMatthew Ahrens zfs_secpolicy_recv);
59254445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
59264445fffbSMatthew Ahrens zfs_secpolicy_promote);
59274445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
59284445fffbSMatthew Ahrens zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
59294445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
59304445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl);
59314445fffbSMatthew Ahrens
59324445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
59334445fffbSMatthew Ahrens zfs_secpolicy_share, POOL_CHECK_NONE);
59344445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
59354445fffbSMatthew Ahrens zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
59364445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
59374445fffbSMatthew Ahrens zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
59384445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
59394445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
59404445fffbSMatthew Ahrens zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
59414445fffbSMatthew Ahrens POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
59424445fffbSMatthew Ahrens }
5943fa9e4066Sahrens
594454d692b7SGeorge Wilson int
pool_status_check(const char * name,zfs_ioc_namecheck_t type,zfs_ioc_poolcheck_t check)5945f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5946f9af39baSGeorge Wilson zfs_ioc_poolcheck_t check)
594754d692b7SGeorge Wilson {
594854d692b7SGeorge Wilson spa_t *spa;
594954d692b7SGeorge Wilson int error;
595054d692b7SGeorge Wilson
595154d692b7SGeorge Wilson ASSERT(type == POOL_NAME || type == DATASET_NAME);
595254d692b7SGeorge Wilson
5953f9af39baSGeorge Wilson if (check & POOL_CHECK_NONE)
5954f9af39baSGeorge Wilson return (0);
5955f9af39baSGeorge Wilson
595614843421SMatthew Ahrens error = spa_open(name, &spa, FTAG);
595754d692b7SGeorge Wilson if (error == 0) {
5958f9af39baSGeorge Wilson if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5959be6fd75aSMatthew Ahrens error = SET_ERROR(EAGAIN);
5960f9af39baSGeorge Wilson else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5961be6fd75aSMatthew Ahrens error = SET_ERROR(EROFS);
596254d692b7SGeorge Wilson spa_close(spa, FTAG);
596354d692b7SGeorge Wilson }
596454d692b7SGeorge Wilson return (error);
596554d692b7SGeorge Wilson }
596654d692b7SGeorge Wilson
5967c99e4bdcSChris Kirby /*
5968c99e4bdcSChris Kirby * Find a free minor number.
5969c99e4bdcSChris Kirby */
5970c99e4bdcSChris Kirby minor_t
zfsdev_minor_alloc(void)5971c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
5972c99e4bdcSChris Kirby {
5973c99e4bdcSChris Kirby static minor_t last_minor;
5974c99e4bdcSChris Kirby minor_t m;
5975c99e4bdcSChris Kirby
5976c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5977c99e4bdcSChris Kirby
5978c99e4bdcSChris Kirby for (m = last_minor + 1; m != last_minor; m++) {
5979c99e4bdcSChris Kirby if (m > ZFSDEV_MAX_MINOR)
5980c99e4bdcSChris Kirby m = 1;
5981c99e4bdcSChris Kirby if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5982c99e4bdcSChris Kirby last_minor = m;
5983c99e4bdcSChris Kirby return (m);
5984c99e4bdcSChris Kirby }
5985c99e4bdcSChris Kirby }
5986c99e4bdcSChris Kirby
5987c99e4bdcSChris Kirby return (0);
5988c99e4bdcSChris Kirby }
5989c99e4bdcSChris Kirby
5990c99e4bdcSChris Kirby static int
zfs_ctldev_init(dev_t * devp)5991c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
5992c99e4bdcSChris Kirby {
5993c99e4bdcSChris Kirby minor_t minor;
5994c99e4bdcSChris Kirby zfs_soft_state_t *zs;
5995c99e4bdcSChris Kirby
5996c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5997c99e4bdcSChris Kirby ASSERT(getminor(*devp) == 0);
5998c99e4bdcSChris Kirby
5999c99e4bdcSChris Kirby minor = zfsdev_minor_alloc();
6000c99e4bdcSChris Kirby if (minor == 0)
6001be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO));
6002c99e4bdcSChris Kirby
6003c99e4bdcSChris Kirby if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6004be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN));
6005c99e4bdcSChris Kirby
6006c99e4bdcSChris Kirby *devp = makedevice(getemajor(*devp), minor);
6007c99e4bdcSChris Kirby
6008c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, minor);
6009c99e4bdcSChris Kirby zs->zss_type = ZSST_CTLDEV;
6010c99e4bdcSChris Kirby zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6011c99e4bdcSChris Kirby
6012c99e4bdcSChris Kirby return (0);
6013c99e4bdcSChris Kirby }
6014c99e4bdcSChris Kirby
6015c99e4bdcSChris Kirby static void
zfs_ctldev_destroy(zfs_onexit_t * zo,minor_t minor)6016c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6017c99e4bdcSChris Kirby {
6018c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6019c99e4bdcSChris Kirby
6020c99e4bdcSChris Kirby zfs_onexit_destroy(zo);
6021c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor);
6022c99e4bdcSChris Kirby }
6023c99e4bdcSChris Kirby
6024c99e4bdcSChris Kirby void *
zfsdev_get_soft_state(minor_t minor,enum zfs_soft_state_type which)6025c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6026c99e4bdcSChris Kirby {
6027c99e4bdcSChris Kirby zfs_soft_state_t *zp;
6028c99e4bdcSChris Kirby
6029c99e4bdcSChris Kirby zp = ddi_get_soft_state(zfsdev_state, minor);
6030c99e4bdcSChris Kirby if (zp == NULL || zp->zss_type != which)
6031c99e4bdcSChris Kirby return (NULL);
6032c99e4bdcSChris Kirby
6033c99e4bdcSChris Kirby return (zp->zss_data);
6034c99e4bdcSChris Kirby }
6035c99e4bdcSChris Kirby
6036c99e4bdcSChris Kirby static int
zfsdev_open(dev_t * devp,int flag,int otyp,cred_t * cr)6037c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
6038c99e4bdcSChris Kirby {
6039c99e4bdcSChris Kirby int error = 0;
6040c99e4bdcSChris Kirby
6041c99e4bdcSChris Kirby if (getminor(*devp) != 0)
6042c99e4bdcSChris Kirby return (zvol_open(devp, flag, otyp, cr));
6043c99e4bdcSChris Kirby
6044c99e4bdcSChris Kirby /* This is the control device. Allocate a new minor if requested. */
6045c99e4bdcSChris Kirby if (flag & FEXCL) {
6046c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock);
6047c99e4bdcSChris Kirby error = zfs_ctldev_init(devp);
6048c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock);
6049c99e4bdcSChris Kirby }
6050c99e4bdcSChris Kirby
6051c99e4bdcSChris Kirby return (error);
6052c99e4bdcSChris Kirby }
6053c99e4bdcSChris Kirby
6054c99e4bdcSChris Kirby static int
zfsdev_close(dev_t dev,int flag,int otyp,cred_t * cr)6055c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
6056c99e4bdcSChris Kirby {
6057c99e4bdcSChris Kirby zfs_onexit_t *zo;
6058c99e4bdcSChris Kirby minor_t minor = getminor(dev);
6059c99e4bdcSChris Kirby
6060c99e4bdcSChris Kirby if (minor == 0)
6061c99e4bdcSChris Kirby return (0);
6062c99e4bdcSChris Kirby
6063c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock);
6064c99e4bdcSChris Kirby zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6065c99e4bdcSChris Kirby if (zo == NULL) {
6066c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock);
6067c99e4bdcSChris Kirby return (zvol_close(dev, flag, otyp, cr));
6068c99e4bdcSChris Kirby }
6069c99e4bdcSChris Kirby zfs_ctldev_destroy(zo, minor);
6070c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock);
6071c99e4bdcSChris Kirby
6072c99e4bdcSChris Kirby return (0);
6073c99e4bdcSChris Kirby }
6074c99e4bdcSChris Kirby
6075fa9e4066Sahrens static int
zfsdev_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp)6076fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
6077fa9e4066Sahrens {
6078fa9e4066Sahrens zfs_cmd_t *zc;
60794445fffbSMatthew Ahrens uint_t vecnum;
60804445fffbSMatthew Ahrens int error, rc, len;
6081c99e4bdcSChris Kirby minor_t minor = getminor(dev);
60824445fffbSMatthew Ahrens const zfs_ioc_vec_t *vec;
60834445fffbSMatthew Ahrens char *saved_poolname = NULL;
60844445fffbSMatthew Ahrens nvlist_t *innvl = NULL;
6085fa9e4066Sahrens
6086c99e4bdcSChris Kirby if (minor != 0 &&
6087c99e4bdcSChris Kirby zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
6088fa9e4066Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
6089fa9e4066Sahrens
60904445fffbSMatthew Ahrens vecnum = cmd - ZFS_IOC_FIRST;
609191ebeef5Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6092fa9e4066Sahrens
60934445fffbSMatthew Ahrens if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6094be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
60954445fffbSMatthew Ahrens vec = &zfs_ioc_vec[vecnum];
6096fa9e4066Sahrens
6097fa9e4066Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6098fa9e4066Sahrens
6099478ed9adSEric Taylor error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
61004445fffbSMatthew Ahrens if (error != 0) {
6101be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT);
61024445fffbSMatthew Ahrens goto out;
61034445fffbSMatthew Ahrens }
6104fa9e4066Sahrens
61054445fffbSMatthew Ahrens zc->zc_iflags = flag & FKIOCTL;
61064445fffbSMatthew Ahrens if (zc->zc_nvlist_src_size != 0) {
61074445fffbSMatthew Ahrens error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
61084445fffbSMatthew Ahrens zc->zc_iflags, &innvl);
61094445fffbSMatthew Ahrens if (error != 0)
61104445fffbSMatthew Ahrens goto out;
61114445fffbSMatthew Ahrens }
6112fa9e4066Sahrens
6113fa9e4066Sahrens /*
6114fa9e4066Sahrens * Ensure that all pool/dataset names are valid before we pass down to
6115fa9e4066Sahrens * the lower layers.
6116fa9e4066Sahrens */
6117fa9e4066Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
61184445fffbSMatthew Ahrens switch (vec->zvec_namecheck) {
6119e7437265Sahrens case POOL_NAME:
6120fa9e4066Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6121be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
61224445fffbSMatthew Ahrens else
612354d692b7SGeorge Wilson error = pool_status_check(zc->zc_name,
61244445fffbSMatthew Ahrens vec->zvec_namecheck, vec->zvec_pool_check);
6125fa9e4066Sahrens break;
6126fa9e4066Sahrens
6127e7437265Sahrens case DATASET_NAME:
612823962479SMarcel Telka if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6129be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
61304445fffbSMatthew Ahrens else
613154d692b7SGeorge Wilson error = pool_status_check(zc->zc_name,
61324445fffbSMatthew Ahrens vec->zvec_namecheck, vec->zvec_pool_check);
6133fa9e4066Sahrens break;
61345ad82045Snd150628
6135e7437265Sahrens case NO_NAME:
61365ad82045Snd150628 break;
6137fa9e4066Sahrens }
61384445fffbSMatthew Ahrens
61394445fffbSMatthew Ahrens
61404445fffbSMatthew Ahrens if (error == 0 && !(flag & FKIOCTL))
61414445fffbSMatthew Ahrens error = vec->zvec_secpolicy(zc, innvl, cr);
61424445fffbSMatthew Ahrens
61434445fffbSMatthew Ahrens if (error != 0)
61444445fffbSMatthew Ahrens goto out;
61454445fffbSMatthew Ahrens
61464445fffbSMatthew Ahrens /* legacy ioctls can modify zc_name */
614778f17100SMatthew Ahrens len = strcspn(zc->zc_name, "/@#") + 1;
61484445fffbSMatthew Ahrens saved_poolname = kmem_alloc(len, KM_SLEEP);
61494445fffbSMatthew Ahrens (void) strlcpy(saved_poolname, zc->zc_name, len);
61504445fffbSMatthew Ahrens
61514445fffbSMatthew Ahrens if (vec->zvec_func != NULL) {
61524445fffbSMatthew Ahrens nvlist_t *outnvl;
61534445fffbSMatthew Ahrens int puterror = 0;
61544445fffbSMatthew Ahrens spa_t *spa;
61554445fffbSMatthew Ahrens nvlist_t *lognv = NULL;
61564445fffbSMatthew Ahrens
61574445fffbSMatthew Ahrens ASSERT(vec->zvec_legacy_func == NULL);
61584445fffbSMatthew Ahrens
61594445fffbSMatthew Ahrens /*
61604445fffbSMatthew Ahrens * Add the innvl to the lognv before calling the func,
61614445fffbSMatthew Ahrens * in case the func changes the innvl.
61624445fffbSMatthew Ahrens */
61634445fffbSMatthew Ahrens if (vec->zvec_allow_log) {
61644445fffbSMatthew Ahrens lognv = fnvlist_alloc();
61654445fffbSMatthew Ahrens fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
61664445fffbSMatthew Ahrens vec->zvec_name);
61674445fffbSMatthew Ahrens if (!nvlist_empty(innvl)) {
61684445fffbSMatthew Ahrens fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
61694445fffbSMatthew Ahrens innvl);
61704445fffbSMatthew Ahrens }
6171fa9e4066Sahrens }
6172fa9e4066Sahrens
61734445fffbSMatthew Ahrens outnvl = fnvlist_alloc();
61744445fffbSMatthew Ahrens error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6175fa9e4066Sahrens
61764445fffbSMatthew Ahrens if (error == 0 && vec->zvec_allow_log &&
61774445fffbSMatthew Ahrens spa_open(zc->zc_name, &spa, FTAG) == 0) {
61784445fffbSMatthew Ahrens if (!nvlist_empty(outnvl)) {
61794445fffbSMatthew Ahrens fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
61804445fffbSMatthew Ahrens outnvl);
61814445fffbSMatthew Ahrens }
61824445fffbSMatthew Ahrens (void) spa_history_log_nvl(spa, lognv);
61834445fffbSMatthew Ahrens spa_close(spa, FTAG);
61844445fffbSMatthew Ahrens }
61854445fffbSMatthew Ahrens fnvlist_free(lognv);
61864445fffbSMatthew Ahrens
61874445fffbSMatthew Ahrens if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
61884445fffbSMatthew Ahrens int smusherror = 0;
61894445fffbSMatthew Ahrens if (vec->zvec_smush_outnvlist) {
61904445fffbSMatthew Ahrens smusherror = nvlist_smush(outnvl,
61914445fffbSMatthew Ahrens zc->zc_nvlist_dst_size);
61924445fffbSMatthew Ahrens }
61934445fffbSMatthew Ahrens if (smusherror == 0)
61944445fffbSMatthew Ahrens puterror = put_nvlist(zc, outnvl);
61954445fffbSMatthew Ahrens }
61964445fffbSMatthew Ahrens
61974445fffbSMatthew Ahrens if (puterror != 0)
61984445fffbSMatthew Ahrens error = puterror;
61994445fffbSMatthew Ahrens
62004445fffbSMatthew Ahrens nvlist_free(outnvl);
62014445fffbSMatthew Ahrens } else {
62024445fffbSMatthew Ahrens error = vec->zvec_legacy_func(zc);
62034445fffbSMatthew Ahrens }
62044445fffbSMatthew Ahrens
62054445fffbSMatthew Ahrens out:
62064445fffbSMatthew Ahrens nvlist_free(innvl);
6207478ed9adSEric Taylor rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
62084445fffbSMatthew Ahrens if (error == 0 && rc != 0)
6209be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT);
62104445fffbSMatthew Ahrens if (error == 0 && vec->zvec_allow_log) {
62114445fffbSMatthew Ahrens char *s = tsd_get(zfs_allow_log_key);
62124445fffbSMatthew Ahrens if (s != NULL)
62134445fffbSMatthew Ahrens strfree(s);
62144445fffbSMatthew Ahrens (void) tsd_set(zfs_allow_log_key, saved_poolname);
62154445fffbSMatthew Ahrens } else {
62164445fffbSMatthew Ahrens if (saved_poolname != NULL)
62174445fffbSMatthew Ahrens strfree(saved_poolname);
6218ecd6cf80Smarks }
6219fa9e4066Sahrens
6220fa9e4066Sahrens kmem_free(zc, sizeof (zfs_cmd_t));
6221fa9e4066Sahrens return (error);
6222fa9e4066Sahrens }
6223fa9e4066Sahrens
6224fa9e4066Sahrens static int
zfs_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)6225fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6226fa9e4066Sahrens {
6227fa9e4066Sahrens if (cmd != DDI_ATTACH)
6228fa9e4066Sahrens return (DDI_FAILURE);
6229fa9e4066Sahrens
6230fa9e4066Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6231fa9e4066Sahrens DDI_PSEUDO, 0) == DDI_FAILURE)
6232fa9e4066Sahrens return (DDI_FAILURE);
6233fa9e4066Sahrens
6234fa9e4066Sahrens zfs_dip = dip;
6235fa9e4066Sahrens
6236fa9e4066Sahrens ddi_report_dev(dip);
6237fa9e4066Sahrens
6238fa9e4066Sahrens return (DDI_SUCCESS);
6239fa9e4066Sahrens }
6240fa9e4066Sahrens
6241fa9e4066Sahrens static int
zfs_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)6242fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6243fa9e4066Sahrens {
6244fa9e4066Sahrens if (spa_busy() || zfs_busy() || zvol_busy())
6245fa9e4066Sahrens return (DDI_FAILURE);
6246fa9e4066Sahrens
6247fa9e4066Sahrens if (cmd != DDI_DETACH)
6248fa9e4066Sahrens return (DDI_FAILURE);
6249fa9e4066Sahrens
6250fa9e4066Sahrens zfs_dip = NULL;
6251fa9e4066Sahrens
6252fa9e4066Sahrens ddi_prop_remove_all(dip);
6253fa9e4066Sahrens ddi_remove_minor_node(dip, NULL);
6254fa9e4066Sahrens
6255fa9e4066Sahrens return (DDI_SUCCESS);
6256fa9e4066Sahrens }
6257fa9e4066Sahrens
6258fa9e4066Sahrens /*ARGSUSED*/
6259fa9e4066Sahrens static int
zfs_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)6260fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6261fa9e4066Sahrens {
6262fa9e4066Sahrens switch (infocmd) {
6263fa9e4066Sahrens case DDI_INFO_DEVT2DEVINFO:
6264fa9e4066Sahrens *result = zfs_dip;
6265fa9e4066Sahrens return (DDI_SUCCESS);
6266fa9e4066Sahrens
6267fa9e4066Sahrens case DDI_INFO_DEVT2INSTANCE:
6268a0965f35Sbonwick *result = (void *)0;
6269fa9e4066Sahrens return (DDI_SUCCESS);
6270fa9e4066Sahrens }
6271fa9e4066Sahrens
6272fa9e4066Sahrens return (DDI_FAILURE);
6273fa9e4066Sahrens }
6274fa9e4066Sahrens
6275fa9e4066Sahrens /*
6276fa9e4066Sahrens * OK, so this is a little weird.
6277fa9e4066Sahrens *
6278fa9e4066Sahrens * /dev/zfs is the control node, i.e. minor 0.
6279fa9e4066Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6280fa9e4066Sahrens *
6281fa9e4066Sahrens * /dev/zfs has basically nothing to do except serve up ioctls,
6282fa9e4066Sahrens * so most of the standard driver entry points are in zvol.c.
6283fa9e4066Sahrens */
6284fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
6285c99e4bdcSChris Kirby zfsdev_open, /* open */
6286c99e4bdcSChris Kirby zfsdev_close, /* close */
6287fa9e4066Sahrens zvol_strategy, /* strategy */
6288fa9e4066Sahrens nodev, /* print */
6289e7cbe64fSgw25295 zvol_dump, /* dump */
6290fa9e4066Sahrens zvol_read, /* read */
6291fa9e4066Sahrens zvol_write, /* write */
6292fa9e4066Sahrens zfsdev_ioctl, /* ioctl */
6293fa9e4066Sahrens nodev, /* devmap */
6294fa9e4066Sahrens nodev, /* mmap */
6295fa9e4066Sahrens nodev, /* segmap */
6296fa9e4066Sahrens nochpoll, /* poll */
6297fa9e4066Sahrens ddi_prop_op, /* prop_op */
6298fa9e4066Sahrens NULL, /* streamtab */
6299fa9e4066Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6300fa9e4066Sahrens CB_REV, /* version */
6301feb08c6bSbillm nodev, /* async read */
6302feb08c6bSbillm nodev, /* async write */
6303fa9e4066Sahrens };
6304fa9e4066Sahrens
6305fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
6306fa9e4066Sahrens DEVO_REV, /* version */
6307fa9e4066Sahrens 0, /* refcnt */
6308fa9e4066Sahrens zfs_info, /* info */
6309fa9e4066Sahrens nulldev, /* identify */
6310fa9e4066Sahrens nulldev, /* probe */
6311fa9e4066Sahrens zfs_attach, /* attach */
6312fa9e4066Sahrens zfs_detach, /* detach */
6313fa9e4066Sahrens nodev, /* reset */
6314fa9e4066Sahrens &zfs_cb_ops, /* driver operations */
631519397407SSherry Moore NULL, /* no bus operations */
631619397407SSherry Moore NULL, /* power */
631719397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
6318fa9e4066Sahrens };
6319fa9e4066Sahrens
6320fa9e4066Sahrens static struct modldrv zfs_modldrv = {
632119397407SSherry Moore &mod_driverops,
632219397407SSherry Moore "ZFS storage pool",
6323e9dbad6fSeschrock &zfs_dev_ops
6324fa9e4066Sahrens };
6325fa9e4066Sahrens
6326fa9e4066Sahrens static struct modlinkage modlinkage = {
6327fa9e4066Sahrens MODREV_1,
6328fa9e4066Sahrens (void *)&zfs_modlfs,
6329fa9e4066Sahrens (void *)&zfs_modldrv,
6330fa9e4066Sahrens NULL
6331fa9e4066Sahrens };
6332fa9e4066Sahrens
63334445fffbSMatthew Ahrens static void
zfs_allow_log_destroy(void * arg)63344445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg)
63354445fffbSMatthew Ahrens {
63364445fffbSMatthew Ahrens char *poolname = arg;
63374445fffbSMatthew Ahrens strfree(poolname);
63384445fffbSMatthew Ahrens }
6339ec533521Sfr157268
6340fa9e4066Sahrens int
_init(void)6341fa9e4066Sahrens _init(void)
6342fa9e4066Sahrens {
6343fa9e4066Sahrens int error;
6344fa9e4066Sahrens
6345fa9e4066Sahrens spa_init(FREAD | FWRITE);
6346fa9e4066Sahrens zfs_init();
6347fa9e4066Sahrens zvol_init();
63484445fffbSMatthew Ahrens zfs_ioctl_init();
6349d78b796cSAndreas Jaekel rz_zev_init();
6350fa9e4066Sahrens
6351a0965f35Sbonwick if ((error = mod_install(&modlinkage)) != 0) {
6352a0965f35Sbonwick zvol_fini();
6353a0965f35Sbonwick zfs_fini();
6354a0965f35Sbonwick spa_fini();
6355a0965f35Sbonwick return (error);
6356a0965f35Sbonwick }
6357a0965f35Sbonwick
6358ec533521Sfr157268 tsd_create(&zfs_fsyncer_key, NULL);
63594445fffbSMatthew Ahrens tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
63604445fffbSMatthew Ahrens tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6361ec533521Sfr157268
6362a0965f35Sbonwick error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6363a0965f35Sbonwick ASSERT(error == 0);
6364ecd6cf80Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6365a0965f35Sbonwick
6366fa9e4066Sahrens return (0);
6367fa9e4066Sahrens }
6368fa9e4066Sahrens
6369fa9e4066Sahrens int
_fini(void)6370fa9e4066Sahrens _fini(void)
6371fa9e4066Sahrens {
6372fa9e4066Sahrens int error;
6373fa9e4066Sahrens
6374ea8dc4b6Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6375be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY));
6376fa9e4066Sahrens
6377fa9e4066Sahrens if ((error = mod_remove(&modlinkage)) != 0)
6378fa9e4066Sahrens return (error);
6379fa9e4066Sahrens
6380d78b796cSAndreas Jaekel rz_zev_fini();
6381fa9e4066Sahrens zvol_fini();
6382fa9e4066Sahrens zfs_fini();
6383fa9e4066Sahrens spa_fini();
6384da6c28aaSamw if (zfs_nfsshare_inited)
6385ecd6cf80Smarks (void) ddi_modclose(nfs_mod);
6386da6c28aaSamw if (zfs_smbshare_inited)
6387da6c28aaSamw (void) ddi_modclose(smbsrv_mod);
6388da6c28aaSamw if (zfs_nfsshare_inited || zfs_smbshare_inited)
6389ecd6cf80Smarks (void) ddi_modclose(sharefs_mod);
6390fa9e4066Sahrens
6391ec533521Sfr157268 tsd_destroy(&zfs_fsyncer_key);
6392fa9e4066Sahrens ldi_ident_release(zfs_li);
6393fa9e4066Sahrens zfs_li = NULL;
6394ecd6cf80Smarks mutex_destroy(&zfs_share_lock);
6395fa9e4066Sahrens
6396fa9e4066Sahrens return (error);
6397fa9e4066Sahrens }
6398fa9e4066Sahrens
6399fa9e4066Sahrens int
_info(struct modinfo * modinfop)6400fa9e4066Sahrens _info(struct modinfo *modinfop)
6401fa9e4066Sahrens {
6402fa9e4066Sahrens return (mod_info(&modlinkage, modinfop));
6403fa9e4066Sahrens }
6404