1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26 /*
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Portions Copyright 2011 Martin Matuska
29 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
30 * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
31 * Copyright 2019 Joyent, Inc.
32 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
33 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
34 * Copyright (c) 2013 Steven Hartland. All rights reserved.
35 * Copyright (c) 2014 Integros [integros.com]
36 * Copyright 2016 Toomas Soome <tsoome@me.com>
37 * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
38 * Copyright 2017 RackTop Systems.
39 * Copyright (c) 2017, Datto, Inc. All rights reserved.
40 * Copyright 2021 The University of Queensland
41 * Copyright 2026 Oxide Computer Company
42 */
43
44 /*
45 * ZFS ioctls.
46 *
47 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
48 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
49 *
50 * There are two ways that we handle ioctls: the legacy way where almost
51 * all of the logic is in the ioctl callback, and the new way where most
52 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
53 *
54 * Non-legacy ioctls should be registered by calling
55 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
56 * from userland by lzc_ioctl().
57 *
58 * The registration arguments are as follows:
59 *
60 * const char *name
61 * The name of the ioctl. This is used for history logging. If the
62 * ioctl returns successfully (the callback returns 0), and allow_log
63 * is true, then a history log entry will be recorded with the input &
64 * output nvlists. The log entry can be printed with "zpool history -i".
65 *
66 * zfs_ioc_t ioc
67 * The ioctl request number, which userland will pass to ioctl(2).
68 * We want newer versions of libzfs and libzfs_core to run against
69 * existing zfs kernel modules (i.e. a deferred reboot after an update).
70 * Therefore the ioctl numbers cannot change from release to release.
71 *
72 * zfs_secpolicy_func_t *secpolicy
73 * This function will be called before the zfs_ioc_func_t, to
74 * determine if this operation is permitted. It should return EPERM
75 * on failure, and 0 on success. Checks include determining if the
76 * dataset is visible in this zone, and if the user has either all
77 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
78 * to do this operation on this dataset with "zfs allow".
79 *
80 * zfs_ioc_namecheck_t namecheck
81 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
82 * name, a dataset name, or nothing. If the name is not well-formed,
83 * the ioctl will fail and the callback will not be called.
84 * Therefore, the callback can assume that the name is well-formed
85 * (e.g. is null-terminated, doesn't have more than one '@' character,
86 * doesn't have invalid characters).
87 *
88 * zfs_ioc_poolcheck_t pool_check
89 * This specifies requirements on the pool state. If the pool does
90 * not meet them (is suspended or is readonly), the ioctl will fail
91 * and the callback will not be called. If any checks are specified
92 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
93 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
94 * POOL_CHECK_READONLY).
95 *
96 * zfs_ioc_key_t *nvl_keys
97 * The list of expected/allowable innvl input keys. This list is used
98 * to validate the nvlist input to the ioctl.
99 *
100 * boolean_t smush_outnvlist
101 * If smush_outnvlist is true, then the output is presumed to be a
102 * list of errors, and it will be "smushed" down to fit into the
103 * caller's buffer, by removing some entries and replacing them with a
104 * single "N_MORE_ERRORS" entry indicating how many were removed. See
105 * nvlist_smush() for details. If smush_outnvlist is false, and the
106 * outnvlist does not fit into the userland-provided buffer, then the
107 * ioctl will fail with ENOMEM.
108 *
109 * zfs_ioc_func_t *func
110 * The callback function that will perform the operation.
111 *
112 * The callback should return 0 on success, or an error number on
113 * failure. If the function fails, the userland ioctl will return -1,
114 * and errno will be set to the callback's return value. The callback
115 * will be called with the following arguments:
116 *
117 * const char *name
118 * The name of the pool or dataset to operate on, from
119 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
120 * expected type (pool, dataset, or none).
121 *
122 * nvlist_t *innvl
123 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
124 * NULL if no input nvlist was provided. Changes to this nvlist are
125 * ignored. If the input nvlist could not be deserialized, the
126 * ioctl will fail and the callback will not be called.
127 *
128 * nvlist_t *outnvl
129 * The output nvlist, initially empty. The callback can fill it in,
130 * and it will be returned to userland by serializing it into
131 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
132 * fails (e.g. because the caller didn't supply a large enough
133 * buffer), then the overall ioctl will fail. See the
134 * 'smush_nvlist' argument above for additional behaviors.
135 *
136 * There are two typical uses of the output nvlist:
137 * - To return state, e.g. property values. In this case,
138 * smush_outnvlist should be false. If the buffer was not large
139 * enough, the caller will reallocate a larger buffer and try
140 * the ioctl again.
141 *
142 * - To return multiple errors from an ioctl which makes on-disk
143 * changes. In this case, smush_outnvlist should be true.
144 * Ioctls which make on-disk modifications should generally not
145 * use the outnvl if they succeed, because the caller can not
146 * distinguish between the operation failing, and
147 * deserialization failing.
148 *
149 * IOCTL Interface Errors
150 *
151 * The following ioctl input errors can be returned:
152 * ZFS_ERR_IOC_CMD_UNAVAIL the ioctl number is not supported by kernel
153 * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel
154 * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing
155 * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type
156 */
157
158 #include <sys/types.h>
159 #include <sys/param.h>
160 #include <sys/errno.h>
161 #include <sys/uio.h>
162 #include <sys/buf.h>
163 #include <sys/modctl.h>
164 #include <sys/open.h>
165 #include <sys/file.h>
166 #include <sys/kmem.h>
167 #include <sys/conf.h>
168 #include <sys/cmn_err.h>
169 #include <sys/stat.h>
170 #include <sys/zfs_ioctl.h>
171 #include <sys/zfs_vfsops.h>
172 #include <sys/zfs_znode.h>
173 #include <sys/zap.h>
174 #include <sys/spa.h>
175 #include <sys/spa_impl.h>
176 #include <sys/vdev.h>
177 #include <sys/priv_impl.h>
178 #include <sys/dmu.h>
179 #include <sys/dsl_dir.h>
180 #include <sys/dsl_dataset.h>
181 #include <sys/dsl_prop.h>
182 #include <sys/dsl_deleg.h>
183 #include <sys/dmu_objset.h>
184 #include <sys/dmu_impl.h>
185 #include <sys/dmu_tx.h>
186 #include <sys/ddi.h>
187 #include <sys/sunddi.h>
188 #include <sys/sunldi.h>
189 #include <sys/policy.h>
190 #include <sys/zone.h>
191 #include <sys/nvpair.h>
192 #include <sys/pathname.h>
193 #include <sys/mount.h>
194 #include <sys/sdt.h>
195 #include <sys/fs/zfs.h>
196 #include <sys/zfs_ctldir.h>
197 #include <sys/zfs_dir.h>
198 #include <sys/zfs_onexit.h>
199 #include <sys/zvol.h>
200 #include <sys/dsl_scan.h>
201 #include <sharefs/share.h>
202 #include <sys/dmu_objset.h>
203 #include <sys/dmu_recv.h>
204 #include <sys/dmu_send.h>
205 #include <sys/dsl_destroy.h>
206 #include <sys/dsl_bookmark.h>
207 #include <sys/dsl_userhold.h>
208 #include <sys/zfeature.h>
209 #include <sys/zcp.h>
210 #include <sys/zio_checksum.h>
211 #include <sys/vdev_removal.h>
212 #include <sys/vdev_impl.h>
213 #include <sys/vdev_initialize.h>
214 #include <sys/vdev_trim.h>
215 #include <sys/dsl_crypt.h>
216
217 #include "zfs_namecheck.h"
218 #include "zfs_prop.h"
219 #include "zfs_deleg.h"
220 #include "zfs_comutil.h"
221
222 #include "lua.h"
223 #include "lauxlib.h"
224
225 extern struct modlfs zfs_modlfs;
226
227 extern void zfs_init(void);
228 extern void zfs_fini(void);
229
230 ldi_ident_t zfs_li = NULL;
231 dev_info_t *zfs_dip;
232
233 extern uint_t rrw_tsd_key;
234 static uint_t zfs_allow_log_key;
235
236 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
237 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
238 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
239
240 /*
241 * IOC Keys are used to document and validate user->kernel interface inputs.
242 * See zfs_keys_recv_new for an example declaration. Any key name that is not
243 * listed will be rejected as input.
244 *
245 * The keyname 'optional' is always allowed, and must be an nvlist if present.
246 * Arguments which older kernels can safely ignore can be placed under the
247 * "optional" key.
248 *
249 * When adding new keys to an existing ioc for new functionality, consider:
250 * - adding an entry into zfs_sysfs.c zfs_features[] list
251 * - updating the libzfs_input_check.c test utility
252 *
253 * Note: in the ZK_WILDCARDLIST case, the name serves as documentation
254 * for the expected name (bookmark, snapshot, property, etc) but there
255 * is no validation in the preflight zfs_check_input_nvpairs() check.
256 */
257 typedef enum {
258 ZK_OPTIONAL = 1 << 0, /* pair is optional */
259 ZK_WILDCARDLIST = 1 << 1, /* one or more unspecified key names */
260 } ioc_key_flag_t;
261
262 /* DATA_TYPE_ANY is used when zkey_type can vary. */
263 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
264
265 typedef struct zfs_ioc_key {
266 const char *zkey_name;
267 data_type_t zkey_type;
268 ioc_key_flag_t zkey_flags;
269 } zfs_ioc_key_t;
270
271 typedef enum {
272 NO_NAME,
273 POOL_NAME,
274 DATASET_NAME
275 } zfs_ioc_namecheck_t;
276
277 typedef enum {
278 POOL_CHECK_NONE = 1 << 0,
279 POOL_CHECK_SUSPENDED = 1 << 1,
280 POOL_CHECK_READONLY = 1 << 2,
281 } zfs_ioc_poolcheck_t;
282
283 typedef struct zfs_ioc_vec {
284 zfs_ioc_legacy_func_t *zvec_legacy_func;
285 zfs_ioc_func_t *zvec_func;
286 zfs_secpolicy_func_t *zvec_secpolicy;
287 zfs_ioc_namecheck_t zvec_namecheck;
288 boolean_t zvec_allow_log;
289 zfs_ioc_poolcheck_t zvec_pool_check;
290 boolean_t zvec_smush_outnvlist;
291 const char *zvec_name;
292 const zfs_ioc_key_t *zvec_nvl_keys;
293 size_t zvec_nvl_key_count;
294 } zfs_ioc_vec_t;
295
296 /* This array is indexed by zfs_userquota_prop_t */
297 static const char *userquota_perms[] = {
298 ZFS_DELEG_PERM_USERUSED,
299 ZFS_DELEG_PERM_USERQUOTA,
300 ZFS_DELEG_PERM_GROUPUSED,
301 ZFS_DELEG_PERM_GROUPQUOTA,
302 ZFS_DELEG_PERM_USEROBJUSED,
303 ZFS_DELEG_PERM_USEROBJQUOTA,
304 ZFS_DELEG_PERM_GROUPOBJUSED,
305 ZFS_DELEG_PERM_GROUPOBJQUOTA,
306 ZFS_DELEG_PERM_PROJECTUSED,
307 ZFS_DELEG_PERM_PROJECTQUOTA,
308 ZFS_DELEG_PERM_PROJECTOBJUSED,
309 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
310 };
311
312 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
313 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
314 static int zfs_check_settable(const char *name, nvpair_t *property,
315 cred_t *cr);
316 static int zfs_check_clearable(char *dataset, nvlist_t *props,
317 nvlist_t **errors);
318 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
319 boolean_t *);
320 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
321 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
322
323 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
324
325 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
326 void
__dprintf(const char * file,const char * func,int line,const char * fmt,...)327 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
328 {
329 const char *newfile;
330 char buf[512];
331 va_list adx;
332
333 /*
334 * Get rid of annoying "../common/" prefix to filename.
335 */
336 newfile = strrchr(file, '/');
337 if (newfile != NULL) {
338 newfile = newfile + 1; /* Get rid of leading / */
339 } else {
340 newfile = file;
341 }
342
343 va_start(adx, fmt);
344 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
345 va_end(adx);
346
347 /*
348 * To get this data, use the zfs-dprintf probe as so:
349 * dtrace -q -n 'zfs-dprintf \
350 * /stringof(arg0) == "dbuf.c"/ \
351 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
352 * arg0 = file name
353 * arg1 = function name
354 * arg2 = line number
355 * arg3 = message
356 */
357 DTRACE_PROBE4(zfs__dprintf,
358 char *, newfile, char *, func, int, line, char *, buf);
359 }
360
361 static void
history_str_free(char * buf)362 history_str_free(char *buf)
363 {
364 kmem_free(buf, HIS_MAX_RECORD_LEN);
365 }
366
367 static char *
history_str_get(zfs_cmd_t * zc)368 history_str_get(zfs_cmd_t *zc)
369 {
370 char *buf;
371
372 if (zc->zc_history == 0)
373 return (NULL);
374
375 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
376 if (copyinstr((void *)(uintptr_t)zc->zc_history,
377 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
378 history_str_free(buf);
379 return (NULL);
380 }
381
382 buf[HIS_MAX_RECORD_LEN -1] = '\0';
383
384 return (buf);
385 }
386
387 /*
388 * Check to see if the named dataset is currently defined as bootable
389 */
390 static boolean_t
zfs_is_bootfs(const char * name)391 zfs_is_bootfs(const char *name)
392 {
393 objset_t *os;
394
395 if (dmu_objset_hold(name, FTAG, &os) == 0) {
396 boolean_t ret;
397 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
398 dmu_objset_rele(os, FTAG);
399 return (ret);
400 }
401 return (B_FALSE);
402 }
403
404 /*
405 * Return non-zero if the spa version is less than requested version.
406 */
407 static int
zfs_earlier_version(const char * name,int version)408 zfs_earlier_version(const char *name, int version)
409 {
410 spa_t *spa;
411
412 if (spa_open(name, &spa, FTAG) == 0) {
413 if (spa_version(spa) < version) {
414 spa_close(spa, FTAG);
415 return (1);
416 }
417 spa_close(spa, FTAG);
418 }
419 return (0);
420 }
421
422 /*
423 * Return TRUE if the ZPL version is less than requested version.
424 */
425 static boolean_t
zpl_earlier_version(const char * name,int version)426 zpl_earlier_version(const char *name, int version)
427 {
428 objset_t *os;
429 boolean_t rc = B_TRUE;
430
431 if (dmu_objset_hold(name, FTAG, &os) == 0) {
432 uint64_t zplversion;
433
434 if (dmu_objset_type(os) != DMU_OST_ZFS) {
435 dmu_objset_rele(os, FTAG);
436 return (B_TRUE);
437 }
438 /* XXX reading from non-owned objset */
439 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
440 rc = zplversion < version;
441 dmu_objset_rele(os, FTAG);
442 }
443 return (rc);
444 }
445
446 static void
zfs_log_history(zfs_cmd_t * zc)447 zfs_log_history(zfs_cmd_t *zc)
448 {
449 spa_t *spa;
450 char *buf;
451
452 if ((buf = history_str_get(zc)) == NULL)
453 return;
454
455 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
456 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
457 (void) spa_history_log(spa, buf);
458 spa_close(spa, FTAG);
459 }
460 history_str_free(buf);
461 }
462
463 /*
464 * Policy for top-level read operations (list pools). Requires no privileges,
465 * and can be used in the local zone, as there is no associated dataset.
466 */
467 /* ARGSUSED */
468 static int
zfs_secpolicy_none(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)469 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
470 {
471 return (0);
472 }
473
474 /*
475 * Policy for dataset read operations (list children, get statistics). Requires
476 * no privileges, but must be visible in the local zone.
477 */
478 /* ARGSUSED */
479 static int
zfs_secpolicy_read(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)480 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
481 {
482 if (INGLOBALZONE(curproc) ||
483 zone_dataset_visible(zc->zc_name, NULL))
484 return (0);
485
486 return (SET_ERROR(ENOENT));
487 }
488
489 static int
zfs_dozonecheck_impl(const char * dataset,uint64_t zoned,cred_t * cr)490 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
491 {
492 int writable = 1;
493
494 /*
495 * The dataset must be visible by this zone -- check this first
496 * so they don't see EPERM on something they shouldn't know about.
497 */
498 if (!INGLOBALZONE(curproc) &&
499 !zone_dataset_visible(dataset, &writable))
500 return (SET_ERROR(ENOENT));
501
502 if (INGLOBALZONE(curproc)) {
503 /*
504 * If the fs is zoned, only root can access it from the
505 * global zone.
506 */
507 if (secpolicy_zfs(cr) && zoned)
508 return (SET_ERROR(EPERM));
509 } else {
510 /*
511 * If we are in a local zone, the 'zoned' property must be set.
512 */
513 if (!zoned)
514 return (SET_ERROR(EPERM));
515
516 /* must be writable by this zone */
517 if (!writable)
518 return (SET_ERROR(EPERM));
519 }
520 return (0);
521 }
522
523 static int
zfs_dozonecheck(const char * dataset,cred_t * cr)524 zfs_dozonecheck(const char *dataset, cred_t *cr)
525 {
526 uint64_t zoned;
527
528 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
529 return (SET_ERROR(ENOENT));
530
531 return (zfs_dozonecheck_impl(dataset, zoned, cr));
532 }
533
534 static int
zfs_dozonecheck_ds(const char * dataset,dsl_dataset_t * ds,cred_t * cr)535 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
536 {
537 uint64_t zoned;
538
539 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
540 return (SET_ERROR(ENOENT));
541
542 return (zfs_dozonecheck_impl(dataset, zoned, cr));
543 }
544
545 static int
zfs_secpolicy_write_perms_ds(const char * name,dsl_dataset_t * ds,const char * perm,cred_t * cr)546 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
547 const char *perm, cred_t *cr)
548 {
549 int error;
550
551 error = zfs_dozonecheck_ds(name, ds, cr);
552 if (error == 0) {
553 error = secpolicy_zfs(cr);
554 if (error != 0)
555 error = dsl_deleg_access_impl(ds, perm, cr);
556 }
557 return (error);
558 }
559
560 static int
zfs_secpolicy_write_perms(const char * name,const char * perm,cred_t * cr)561 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
562 {
563 int error;
564 dsl_dataset_t *ds;
565 dsl_pool_t *dp;
566
567 /*
568 * First do a quick check for root in the global zone, which
569 * is allowed to do all write_perms. This ensures that zfs_ioc_*
570 * will get to handle nonexistent datasets.
571 */
572 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
573 return (0);
574
575 error = dsl_pool_hold(name, FTAG, &dp);
576 if (error != 0)
577 return (error);
578
579 error = dsl_dataset_hold(dp, name, FTAG, &ds);
580 if (error != 0) {
581 dsl_pool_rele(dp, FTAG);
582 return (error);
583 }
584
585 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
586
587 dsl_dataset_rele(ds, FTAG);
588 dsl_pool_rele(dp, FTAG);
589 return (error);
590 }
591
592 /*
593 * Policy for setting the security label property.
594 *
595 * Returns 0 for success, non-zero for access and other errors.
596 */
597 static int
zfs_set_slabel_policy(const char * name,char * strval,cred_t * cr)598 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
599 {
600 char ds_hexsl[MAXNAMELEN];
601 bslabel_t ds_sl, new_sl;
602 boolean_t new_default = FALSE;
603 uint64_t zoned;
604 int needed_priv = -1;
605 int error;
606
607 /* First get the existing dataset label. */
608 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
609 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
610 if (error != 0)
611 return (SET_ERROR(EPERM));
612
613 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
614 new_default = TRUE;
615
616 /* The label must be translatable */
617 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
618 return (SET_ERROR(EINVAL));
619
620 /*
621 * In a non-global zone, disallow attempts to set a label that
622 * doesn't match that of the zone; otherwise no other checks
623 * are needed.
624 */
625 if (!INGLOBALZONE(curproc)) {
626 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
627 return (SET_ERROR(EPERM));
628 return (0);
629 }
630
631 /*
632 * For global-zone datasets (i.e., those whose zoned property is
633 * "off", verify that the specified new label is valid for the
634 * global zone.
635 */
636 if (dsl_prop_get_integer(name,
637 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
638 return (SET_ERROR(EPERM));
639 if (!zoned) {
640 if (zfs_check_global_label(name, strval) != 0)
641 return (SET_ERROR(EPERM));
642 }
643
644 /*
645 * If the existing dataset label is nondefault, check if the
646 * dataset is mounted (label cannot be changed while mounted).
647 * Get the zfsvfs; if there isn't one, then the dataset isn't
648 * mounted (or isn't a dataset, doesn't exist, ...).
649 */
650 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
651 objset_t *os;
652 static char *setsl_tag = "setsl_tag";
653
654 /*
655 * Try to own the dataset; abort if there is any error,
656 * (e.g., already mounted, in use, or other error).
657 */
658 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
659 setsl_tag, &os);
660 if (error != 0)
661 return (SET_ERROR(EPERM));
662
663 dmu_objset_disown(os, B_TRUE, setsl_tag);
664
665 if (new_default) {
666 needed_priv = PRIV_FILE_DOWNGRADE_SL;
667 goto out_check;
668 }
669
670 if (hexstr_to_label(strval, &new_sl) != 0)
671 return (SET_ERROR(EPERM));
672
673 if (blstrictdom(&ds_sl, &new_sl))
674 needed_priv = PRIV_FILE_DOWNGRADE_SL;
675 else if (blstrictdom(&new_sl, &ds_sl))
676 needed_priv = PRIV_FILE_UPGRADE_SL;
677 } else {
678 /* dataset currently has a default label */
679 if (!new_default)
680 needed_priv = PRIV_FILE_UPGRADE_SL;
681 }
682
683 out_check:
684 if (needed_priv != -1)
685 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
686 return (0);
687 }
688
689 static int
zfs_secpolicy_setprop(const char * dsname,zfs_prop_t prop,nvpair_t * propval,cred_t * cr)690 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
691 cred_t *cr)
692 {
693 char *strval;
694
695 /*
696 * Check permissions for special properties.
697 */
698 switch (prop) {
699 case ZFS_PROP_ZONED:
700 /*
701 * Disallow setting of 'zoned' from within a local zone.
702 */
703 if (!INGLOBALZONE(curproc))
704 return (SET_ERROR(EPERM));
705 break;
706
707 case ZFS_PROP_QUOTA:
708 case ZFS_PROP_FILESYSTEM_LIMIT:
709 case ZFS_PROP_SNAPSHOT_LIMIT:
710 if (!INGLOBALZONE(curproc)) {
711 uint64_t zoned;
712 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
713 /*
714 * Unprivileged users are allowed to modify the
715 * limit on things *under* (ie. contained by)
716 * the thing they own.
717 */
718 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
719 setpoint))
720 return (SET_ERROR(EPERM));
721 if (!zoned || strlen(dsname) <= strlen(setpoint))
722 return (SET_ERROR(EPERM));
723 }
724 break;
725
726 case ZFS_PROP_MLSLABEL:
727 if (!is_system_labeled())
728 return (SET_ERROR(EPERM));
729
730 if (nvpair_value_string(propval, &strval) == 0) {
731 int err;
732
733 err = zfs_set_slabel_policy(dsname, strval, CRED());
734 if (err != 0)
735 return (err);
736 }
737 break;
738 }
739
740 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
741 }
742
743 /* ARGSUSED */
744 static int
zfs_secpolicy_set_fsacl(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)745 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
746 {
747 int error;
748
749 error = zfs_dozonecheck(zc->zc_name, cr);
750 if (error != 0)
751 return (error);
752
753 /*
754 * permission to set permissions will be evaluated later in
755 * dsl_deleg_can_allow()
756 */
757 return (0);
758 }
759
760 /* ARGSUSED */
761 static int
zfs_secpolicy_rollback(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)762 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
763 {
764 return (zfs_secpolicy_write_perms(zc->zc_name,
765 ZFS_DELEG_PERM_ROLLBACK, cr));
766 }
767
768 /* ARGSUSED */
769 static int
zfs_secpolicy_send(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)770 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
771 {
772 dsl_pool_t *dp;
773 dsl_dataset_t *ds;
774 char *cp;
775 int error;
776
777 /*
778 * Generate the current snapshot name from the given objsetid, then
779 * use that name for the secpolicy/zone checks.
780 */
781 cp = strchr(zc->zc_name, '@');
782 if (cp == NULL)
783 return (SET_ERROR(EINVAL));
784 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
785 if (error != 0)
786 return (error);
787
788 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
789 if (error != 0) {
790 dsl_pool_rele(dp, FTAG);
791 return (error);
792 }
793
794 dsl_dataset_name(ds, zc->zc_name);
795
796 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
797 ZFS_DELEG_PERM_SEND, cr);
798 dsl_dataset_rele(ds, FTAG);
799 dsl_pool_rele(dp, FTAG);
800
801 return (error);
802 }
803
804 /* ARGSUSED */
805 static int
zfs_secpolicy_send_new(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)806 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
807 {
808 return (zfs_secpolicy_write_perms(zc->zc_name,
809 ZFS_DELEG_PERM_SEND, cr));
810 }
811
812 /* ARGSUSED */
813 static int
zfs_secpolicy_deleg_share(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)814 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
815 {
816 vnode_t *vp;
817 int error;
818
819 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
820 NO_FOLLOW, NULL, &vp)) != 0)
821 return (error);
822
823 /* Now make sure mntpnt and dataset are ZFS */
824
825 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
826 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
827 zc->zc_name) != 0)) {
828 VN_RELE(vp);
829 return (SET_ERROR(EPERM));
830 }
831
832 VN_RELE(vp);
833 return (dsl_deleg_access(zc->zc_name,
834 ZFS_DELEG_PERM_SHARE, cr));
835 }
836
837 int
zfs_secpolicy_share(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)838 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
839 {
840 if (secpolicy_nfs(cr) == 0) {
841 return (0);
842 } else {
843 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
844 }
845 }
846
847 int
zfs_secpolicy_smb_acl(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)848 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
849 {
850 if (secpolicy_smb(cr) == 0) {
851 return (0);
852 } else {
853 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
854 }
855 }
856
857 static int
zfs_get_parent(const char * datasetname,char * parent,int parentsize)858 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
859 {
860 char *cp;
861
862 /*
863 * Remove the @bla or /bla from the end of the name to get the parent.
864 */
865 (void) strncpy(parent, datasetname, parentsize);
866 cp = strrchr(parent, '@');
867 if (cp != NULL) {
868 cp[0] = '\0';
869 } else {
870 cp = strrchr(parent, '/');
871 if (cp == NULL)
872 return (SET_ERROR(ENOENT));
873 cp[0] = '\0';
874 }
875
876 return (0);
877 }
878
879 int
zfs_secpolicy_destroy_perms(const char * name,cred_t * cr)880 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
881 {
882 int error;
883
884 if ((error = zfs_secpolicy_write_perms(name,
885 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
886 return (error);
887
888 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
889 }
890
891 /* ARGSUSED */
892 static int
zfs_secpolicy_destroy(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)893 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
894 {
895 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
896 }
897
898 /*
899 * Destroying snapshots with delegated permissions requires
900 * descendant mount and destroy permissions.
901 */
902 /* ARGSUSED */
903 static int
zfs_secpolicy_destroy_snaps(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)904 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
905 {
906 nvlist_t *snaps;
907 nvpair_t *pair, *nextpair;
908 int error = 0;
909
910 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
911
912 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
913 pair = nextpair) {
914 nextpair = nvlist_next_nvpair(snaps, pair);
915 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
916 if (error == ENOENT) {
917 /*
918 * Ignore any snapshots that don't exist (we consider
919 * them "already destroyed"). Remove the name from the
920 * nvl here in case the snapshot is created between
921 * now and when we try to destroy it (in which case
922 * we don't want to destroy it since we haven't
923 * checked for permission).
924 */
925 fnvlist_remove_nvpair(snaps, pair);
926 error = 0;
927 }
928 if (error != 0)
929 break;
930 }
931
932 return (error);
933 }
934
935 int
zfs_secpolicy_rename_perms(const char * from,const char * to,cred_t * cr)936 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
937 {
938 char parentname[ZFS_MAX_DATASET_NAME_LEN];
939 int error;
940
941 if ((error = zfs_secpolicy_write_perms(from,
942 ZFS_DELEG_PERM_RENAME, cr)) != 0)
943 return (error);
944
945 if ((error = zfs_secpolicy_write_perms(from,
946 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
947 return (error);
948
949 if ((error = zfs_get_parent(to, parentname,
950 sizeof (parentname))) != 0)
951 return (error);
952
953 if ((error = zfs_secpolicy_write_perms(parentname,
954 ZFS_DELEG_PERM_CREATE, cr)) != 0)
955 return (error);
956
957 if ((error = zfs_secpolicy_write_perms(parentname,
958 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
959 return (error);
960
961 return (error);
962 }
963
964 /* ARGSUSED */
965 static int
zfs_secpolicy_rename(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)966 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
967 {
968 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
969 }
970
971 /* ARGSUSED */
972 static int
zfs_secpolicy_promote(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)973 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
974 {
975 dsl_pool_t *dp;
976 dsl_dataset_t *clone;
977 int error;
978
979 error = zfs_secpolicy_write_perms(zc->zc_name,
980 ZFS_DELEG_PERM_PROMOTE, cr);
981 if (error != 0)
982 return (error);
983
984 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
985 if (error != 0)
986 return (error);
987
988 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
989
990 if (error == 0) {
991 char parentname[ZFS_MAX_DATASET_NAME_LEN];
992 dsl_dataset_t *origin = NULL;
993 dsl_dir_t *dd;
994 dd = clone->ds_dir;
995
996 error = dsl_dataset_hold_obj(dd->dd_pool,
997 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
998 if (error != 0) {
999 dsl_dataset_rele(clone, FTAG);
1000 dsl_pool_rele(dp, FTAG);
1001 return (error);
1002 }
1003
1004 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
1005 ZFS_DELEG_PERM_MOUNT, cr);
1006
1007 dsl_dataset_name(origin, parentname);
1008 if (error == 0) {
1009 error = zfs_secpolicy_write_perms_ds(parentname, origin,
1010 ZFS_DELEG_PERM_PROMOTE, cr);
1011 }
1012 dsl_dataset_rele(clone, FTAG);
1013 dsl_dataset_rele(origin, FTAG);
1014 }
1015 dsl_pool_rele(dp, FTAG);
1016 return (error);
1017 }
1018
1019 /* ARGSUSED */
1020 static int
zfs_secpolicy_recv(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1021 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1022 {
1023 int error;
1024
1025 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1026 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
1027 return (error);
1028
1029 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1030 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
1031 return (error);
1032
1033 return (zfs_secpolicy_write_perms(zc->zc_name,
1034 ZFS_DELEG_PERM_CREATE, cr));
1035 }
1036
1037 int
zfs_secpolicy_snapshot_perms(const char * name,cred_t * cr)1038 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1039 {
1040 return (zfs_secpolicy_write_perms(name,
1041 ZFS_DELEG_PERM_SNAPSHOT, cr));
1042 }
1043
1044 /*
1045 * Check for permission to create each snapshot in the nvlist.
1046 */
1047 /* ARGSUSED */
1048 static int
zfs_secpolicy_snapshot(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1049 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1050 {
1051 nvlist_t *snaps;
1052 int error = 0;
1053 nvpair_t *pair;
1054
1055 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
1056
1057 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1058 pair = nvlist_next_nvpair(snaps, pair)) {
1059 char *name = nvpair_name(pair);
1060 char *atp = strchr(name, '@');
1061
1062 if (atp == NULL) {
1063 error = SET_ERROR(EINVAL);
1064 break;
1065 }
1066 *atp = '\0';
1067 error = zfs_secpolicy_snapshot_perms(name, cr);
1068 *atp = '@';
1069 if (error != 0)
1070 break;
1071 }
1072 return (error);
1073 }
1074
1075 /*
1076 * Check for permission to create each bookmark in the nvlist.
1077 */
1078 /* ARGSUSED */
1079 static int
zfs_secpolicy_bookmark(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1080 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1081 {
1082 int error = 0;
1083
1084 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1085 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1086 char *name = nvpair_name(pair);
1087 char *hashp = strchr(name, '#');
1088
1089 if (hashp == NULL) {
1090 error = SET_ERROR(EINVAL);
1091 break;
1092 }
1093 *hashp = '\0';
1094 error = zfs_secpolicy_write_perms(name,
1095 ZFS_DELEG_PERM_BOOKMARK, cr);
1096 *hashp = '#';
1097 if (error != 0)
1098 break;
1099 }
1100 return (error);
1101 }
1102
1103 /* ARGSUSED */
1104 static int
zfs_secpolicy_remap(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1105 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1106 {
1107 return (zfs_secpolicy_write_perms(zc->zc_name,
1108 ZFS_DELEG_PERM_REMAP, cr));
1109 }
1110
1111 /* ARGSUSED */
1112 static int
zfs_secpolicy_destroy_bookmarks(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1113 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1114 {
1115 nvpair_t *pair, *nextpair;
1116 int error = 0;
1117
1118 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1119 pair = nextpair) {
1120 char *name = nvpair_name(pair);
1121 char *hashp = strchr(name, '#');
1122 nextpair = nvlist_next_nvpair(innvl, pair);
1123
1124 if (hashp == NULL) {
1125 error = SET_ERROR(EINVAL);
1126 break;
1127 }
1128
1129 *hashp = '\0';
1130 error = zfs_secpolicy_write_perms(name,
1131 ZFS_DELEG_PERM_DESTROY, cr);
1132 *hashp = '#';
1133 if (error == ENOENT) {
1134 /*
1135 * Ignore any filesystems that don't exist (we consider
1136 * their bookmarks "already destroyed"). Remove
1137 * the name from the nvl here in case the filesystem
1138 * is created between now and when we try to destroy
1139 * the bookmark (in which case we don't want to
1140 * destroy it since we haven't checked for permission).
1141 */
1142 fnvlist_remove_nvpair(innvl, pair);
1143 error = 0;
1144 }
1145 if (error != 0)
1146 break;
1147 }
1148
1149 return (error);
1150 }
1151
1152 /* ARGSUSED */
1153 static int
zfs_secpolicy_log_history(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1154 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1155 {
1156 /*
1157 * Even root must have a proper TSD so that we know what pool
1158 * to log to.
1159 */
1160 if (tsd_get(zfs_allow_log_key) == NULL)
1161 return (SET_ERROR(EPERM));
1162 return (0);
1163 }
1164
1165 static int
zfs_secpolicy_create_clone(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1166 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1167 {
1168 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1169 int error;
1170 char *origin;
1171
1172 if ((error = zfs_get_parent(zc->zc_name, parentname,
1173 sizeof (parentname))) != 0)
1174 return (error);
1175
1176 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1177 (error = zfs_secpolicy_write_perms(origin,
1178 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1179 return (error);
1180
1181 if ((error = zfs_secpolicy_write_perms(parentname,
1182 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1183 return (error);
1184
1185 return (zfs_secpolicy_write_perms(parentname,
1186 ZFS_DELEG_PERM_MOUNT, cr));
1187 }
1188
1189 /*
1190 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1191 * SYS_CONFIG privilege, which is not available in a local zone.
1192 */
1193 /* ARGSUSED */
1194 static int
zfs_secpolicy_config(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1195 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1196 {
1197 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1198 return (SET_ERROR(EPERM));
1199
1200 return (0);
1201 }
1202
1203 /*
1204 * Policy for object to name lookups.
1205 */
1206 /* ARGSUSED */
1207 static int
zfs_secpolicy_diff(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1208 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1209 {
1210 int error;
1211
1212 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1213 return (0);
1214
1215 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1216 return (error);
1217 }
1218
1219 /*
1220 * Policy for fault injection. Requires all privileges.
1221 */
1222 /* ARGSUSED */
1223 static int
zfs_secpolicy_inject(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1224 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1225 {
1226 return (secpolicy_zinject(cr));
1227 }
1228
1229 /* ARGSUSED */
1230 static int
zfs_secpolicy_inherit_prop(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1231 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1232 {
1233 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1234
1235 if (prop == ZPROP_INVAL) {
1236 if (!zfs_prop_user(zc->zc_value))
1237 return (SET_ERROR(EINVAL));
1238 return (zfs_secpolicy_write_perms(zc->zc_name,
1239 ZFS_DELEG_PERM_USERPROP, cr));
1240 } else {
1241 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1242 NULL, cr));
1243 }
1244 }
1245
1246 static int
zfs_secpolicy_userspace_one(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1247 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1248 {
1249 int err = zfs_secpolicy_read(zc, innvl, cr);
1250 if (err)
1251 return (err);
1252
1253 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1254 return (SET_ERROR(EINVAL));
1255
1256 if (zc->zc_value[0] == 0) {
1257 /*
1258 * They are asking about a posix uid/gid. If it's
1259 * themself, allow it.
1260 */
1261 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1262 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1263 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1264 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1265 if (zc->zc_guid == crgetuid(cr))
1266 return (0);
1267 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1268 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1269 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1270 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1271 if (groupmember(zc->zc_guid, cr))
1272 return (0);
1273 }
1274 /* else is for project quota/used */
1275 }
1276
1277 return (zfs_secpolicy_write_perms(zc->zc_name,
1278 userquota_perms[zc->zc_objset_type], cr));
1279 }
1280
1281 static int
zfs_secpolicy_userspace_many(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1282 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1283 {
1284 int err = zfs_secpolicy_read(zc, innvl, cr);
1285 if (err)
1286 return (err);
1287
1288 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1289 return (SET_ERROR(EINVAL));
1290
1291 return (zfs_secpolicy_write_perms(zc->zc_name,
1292 userquota_perms[zc->zc_objset_type], cr));
1293 }
1294
1295 /* ARGSUSED */
1296 static int
zfs_secpolicy_userspace_upgrade(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1297 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1298 {
1299 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1300 NULL, cr));
1301 }
1302
1303 /* ARGSUSED */
1304 static int
zfs_secpolicy_hold(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1305 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1306 {
1307 nvpair_t *pair;
1308 nvlist_t *holds;
1309 int error;
1310
1311 holds = fnvlist_lookup_nvlist(innvl, "holds");
1312
1313 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1314 pair = nvlist_next_nvpair(holds, pair)) {
1315 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1316 error = dmu_fsname(nvpair_name(pair), fsname);
1317 if (error != 0)
1318 return (error);
1319 error = zfs_secpolicy_write_perms(fsname,
1320 ZFS_DELEG_PERM_HOLD, cr);
1321 if (error != 0)
1322 return (error);
1323 }
1324 return (0);
1325 }
1326
1327 /* ARGSUSED */
1328 static int
zfs_secpolicy_release(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1329 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1330 {
1331 nvpair_t *pair;
1332 int error;
1333
1334 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1335 pair = nvlist_next_nvpair(innvl, pair)) {
1336 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1337 error = dmu_fsname(nvpair_name(pair), fsname);
1338 if (error != 0)
1339 return (error);
1340 error = zfs_secpolicy_write_perms(fsname,
1341 ZFS_DELEG_PERM_RELEASE, cr);
1342 if (error != 0)
1343 return (error);
1344 }
1345 return (0);
1346 }
1347
1348 /* ARGSUSED */
1349 static int
zfs_secpolicy_load_key(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1350 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1351 {
1352 return (zfs_secpolicy_write_perms(zc->zc_name,
1353 ZFS_DELEG_PERM_LOAD_KEY, cr));
1354 }
1355
1356 /* ARGSUSED */
1357 static int
zfs_secpolicy_change_key(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1358 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1359 {
1360 return (zfs_secpolicy_write_perms(zc->zc_name,
1361 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1362 }
1363
1364 /*
1365 * Policy for allowing temporary snapshots to be taken or released
1366 */
1367 static int
zfs_secpolicy_tmp_snapshot(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)1368 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1369 {
1370 /*
1371 * A temporary snapshot is the same as a snapshot,
1372 * hold, destroy and release all rolled into one.
1373 * Delegated diff alone is sufficient that we allow this.
1374 */
1375 int error;
1376
1377 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1378 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1379 return (0);
1380
1381 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1382
1383 if (innvl != NULL) {
1384 if (error == 0)
1385 error = zfs_secpolicy_hold(zc, innvl, cr);
1386 if (error == 0)
1387 error = zfs_secpolicy_release(zc, innvl, cr);
1388 if (error == 0)
1389 error = zfs_secpolicy_destroy(zc, innvl, cr);
1390 }
1391 return (error);
1392 }
1393
1394 /*
1395 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1396 */
1397 static int
get_nvlist(uint64_t nvl,uint64_t size,int iflag,nvlist_t ** nvp)1398 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1399 {
1400 char *packed;
1401 int error;
1402 nvlist_t *list = NULL;
1403
1404 /*
1405 * Read in and unpack the user-supplied nvlist.
1406 */
1407 if (size == 0)
1408 return (SET_ERROR(EINVAL));
1409
1410 packed = kmem_alloc(size, KM_SLEEP);
1411
1412 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1413 iflag)) != 0) {
1414 kmem_free(packed, size);
1415 return (SET_ERROR(EFAULT));
1416 }
1417
1418 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1419 kmem_free(packed, size);
1420 return (error);
1421 }
1422
1423 kmem_free(packed, size);
1424
1425 *nvp = list;
1426 return (0);
1427 }
1428
1429 /*
1430 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1431 * Entries will be removed from the end of the nvlist, and one int32 entry
1432 * named "N_MORE_ERRORS" will be added indicating how many entries were
1433 * removed.
1434 */
1435 static int
nvlist_smush(nvlist_t * errors,size_t max)1436 nvlist_smush(nvlist_t *errors, size_t max)
1437 {
1438 size_t size;
1439
1440 size = fnvlist_size(errors);
1441
1442 if (size > max) {
1443 nvpair_t *more_errors;
1444 int n = 0;
1445
1446 if (max < 1024)
1447 return (SET_ERROR(ENOMEM));
1448
1449 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1450 more_errors = nvlist_prev_nvpair(errors, NULL);
1451
1452 do {
1453 nvpair_t *pair = nvlist_prev_nvpair(errors,
1454 more_errors);
1455 fnvlist_remove_nvpair(errors, pair);
1456 n++;
1457 size = fnvlist_size(errors);
1458 } while (size > max);
1459
1460 fnvlist_remove_nvpair(errors, more_errors);
1461 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1462 ASSERT3U(fnvlist_size(errors), <=, max);
1463 }
1464
1465 return (0);
1466 }
1467
1468 static int
put_nvlist(zfs_cmd_t * zc,nvlist_t * nvl)1469 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1470 {
1471 char *packed = NULL;
1472 int error = 0;
1473 size_t size;
1474
1475 size = fnvlist_size(nvl);
1476
1477 if (size > zc->zc_nvlist_dst_size) {
1478 error = SET_ERROR(ENOMEM);
1479 } else {
1480 packed = fnvlist_pack(nvl, &size);
1481 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1482 size, zc->zc_iflags) != 0)
1483 error = SET_ERROR(EFAULT);
1484 fnvlist_pack_free(packed, size);
1485 }
1486
1487 zc->zc_nvlist_dst_size = size;
1488 zc->zc_nvlist_dst_filled = B_TRUE;
1489 return (error);
1490 }
1491
1492 int
getzfsvfs_impl(objset_t * os,zfsvfs_t ** zfvp)1493 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1494 {
1495 int error = 0;
1496 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1497 return (SET_ERROR(EINVAL));
1498 }
1499
1500 mutex_enter(&os->os_user_ptr_lock);
1501 *zfvp = dmu_objset_get_user(os);
1502 if (*zfvp) {
1503 VFS_HOLD((*zfvp)->z_vfs);
1504 } else {
1505 error = SET_ERROR(ESRCH);
1506 }
1507 mutex_exit(&os->os_user_ptr_lock);
1508 return (error);
1509 }
1510
1511 int
getzfsvfs(const char * dsname,zfsvfs_t ** zfvp)1512 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1513 {
1514 objset_t *os;
1515 int error;
1516
1517 error = dmu_objset_hold(dsname, FTAG, &os);
1518 if (error != 0)
1519 return (error);
1520
1521 error = getzfsvfs_impl(os, zfvp);
1522 dmu_objset_rele(os, FTAG);
1523 return (error);
1524 }
1525
1526 /*
1527 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1528 * case its z_vfs will be NULL, and it will be opened as the owner.
1529 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1530 * which prevents all vnode ops from running.
1531 */
1532 static int
zfsvfs_hold(const char * name,void * tag,zfsvfs_t ** zfvp,boolean_t writer)1533 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1534 {
1535 int error = 0;
1536
1537 if (getzfsvfs(name, zfvp) != 0)
1538 error = zfsvfs_create(name, B_FALSE, zfvp);
1539 if (error == 0) {
1540 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1541 RW_READER, tag);
1542 if ((*zfvp)->z_unmounted) {
1543 /*
1544 * XXX we could probably try again, since the unmounting
1545 * thread should be just about to disassociate the
1546 * objset from the zfsvfs.
1547 */
1548 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1549 return (SET_ERROR(EBUSY));
1550 }
1551 }
1552 return (error);
1553 }
1554
1555 static void
zfsvfs_rele(zfsvfs_t * zfsvfs,void * tag)1556 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1557 {
1558 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1559
1560 if (zfsvfs->z_vfs) {
1561 VFS_RELE(zfsvfs->z_vfs);
1562 } else {
1563 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1564 zfsvfs_free(zfsvfs);
1565 }
1566 }
1567
1568 static int
zfs_ioc_pool_create(zfs_cmd_t * zc)1569 zfs_ioc_pool_create(zfs_cmd_t *zc)
1570 {
1571 int error;
1572 nvlist_t *config, *props = NULL;
1573 nvlist_t *rootprops = NULL;
1574 nvlist_t *zplprops = NULL;
1575 char *spa_name = zc->zc_name;
1576 dsl_crypto_params_t *dcp = NULL;
1577
1578 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1579 zc->zc_iflags, &config))
1580 return (error);
1581
1582 if (zc->zc_nvlist_src_size != 0 && (error =
1583 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1584 zc->zc_iflags, &props))) {
1585 nvlist_free(config);
1586 return (error);
1587 }
1588
1589 if (props) {
1590 nvlist_t *nvl = NULL;
1591 nvlist_t *hidden_args = NULL;
1592 uint64_t version = SPA_VERSION;
1593 char *tname;
1594
1595 (void) nvlist_lookup_uint64(props,
1596 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1597 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1598 error = SET_ERROR(EINVAL);
1599 goto pool_props_bad;
1600 }
1601 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1602 if (nvl) {
1603 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1604 if (error != 0) {
1605 nvlist_free(config);
1606 nvlist_free(props);
1607 return (error);
1608 }
1609 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1610 }
1611
1612 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1613 &hidden_args);
1614 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1615 rootprops, hidden_args, &dcp);
1616 if (error != 0) {
1617 nvlist_free(config);
1618 nvlist_free(props);
1619 return (error);
1620 }
1621 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1622
1623 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1624 error = zfs_fill_zplprops_root(version, rootprops,
1625 zplprops, NULL);
1626 if (error != 0)
1627 goto pool_props_bad;
1628
1629 if (nvlist_lookup_string(props,
1630 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1631 spa_name = tname;
1632 }
1633
1634 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1635
1636 /*
1637 * Set the remaining root properties
1638 */
1639 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1640 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1641 (void) spa_destroy(spa_name);
1642
1643 pool_props_bad:
1644 nvlist_free(rootprops);
1645 nvlist_free(zplprops);
1646 nvlist_free(config);
1647 nvlist_free(props);
1648 dsl_crypto_params_free(dcp, !!error);
1649
1650 return (error);
1651 }
1652
1653 static int
zfs_ioc_pool_destroy(zfs_cmd_t * zc)1654 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1655 {
1656 int error;
1657 zfs_log_history(zc);
1658 error = spa_destroy(zc->zc_name);
1659 if (error == 0)
1660 zvol_remove_minors(zc->zc_name);
1661 return (error);
1662 }
1663
1664 static int
zfs_ioc_pool_import(zfs_cmd_t * zc)1665 zfs_ioc_pool_import(zfs_cmd_t *zc)
1666 {
1667 nvlist_t *config, *props = NULL;
1668 uint64_t guid;
1669 int error;
1670
1671 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1672 zc->zc_iflags, &config)) != 0)
1673 return (error);
1674
1675 if (zc->zc_nvlist_src_size != 0 && (error =
1676 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1677 zc->zc_iflags, &props))) {
1678 nvlist_free(config);
1679 return (error);
1680 }
1681
1682 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1683 guid != zc->zc_guid)
1684 error = SET_ERROR(EINVAL);
1685 else
1686 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1687
1688 if (zc->zc_nvlist_dst != 0) {
1689 int err;
1690
1691 if ((err = put_nvlist(zc, config)) != 0)
1692 error = err;
1693 }
1694
1695 nvlist_free(config);
1696
1697 nvlist_free(props);
1698
1699 return (error);
1700 }
1701
1702 static int
zfs_ioc_pool_export(zfs_cmd_t * zc)1703 zfs_ioc_pool_export(zfs_cmd_t *zc)
1704 {
1705 int error;
1706 boolean_t force = (boolean_t)zc->zc_cookie;
1707 boolean_t hardforce = (boolean_t)zc->zc_guid;
1708
1709 zfs_log_history(zc);
1710 error = spa_export(zc->zc_name, NULL, force, hardforce);
1711 if (error == 0)
1712 zvol_remove_minors(zc->zc_name);
1713 return (error);
1714 }
1715
1716 static int
zfs_ioc_pool_configs(zfs_cmd_t * zc)1717 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1718 {
1719 nvlist_t *configs;
1720 int error;
1721
1722 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1723 return (SET_ERROR(EEXIST));
1724
1725 error = put_nvlist(zc, configs);
1726
1727 nvlist_free(configs);
1728
1729 return (error);
1730 }
1731
1732 /*
1733 * inputs:
1734 * zc_name name of the pool
1735 *
1736 * outputs:
1737 * zc_cookie real errno
1738 * zc_nvlist_dst config nvlist
1739 * zc_nvlist_dst_size size of config nvlist
1740 */
1741 static int
zfs_ioc_pool_stats(zfs_cmd_t * zc)1742 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1743 {
1744 nvlist_t *config;
1745 int error;
1746 int ret = 0;
1747
1748 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1749 sizeof (zc->zc_value));
1750
1751 if (config != NULL) {
1752 ret = put_nvlist(zc, config);
1753 nvlist_free(config);
1754
1755 /*
1756 * The config may be present even if 'error' is non-zero.
1757 * In this case we return success, and preserve the real errno
1758 * in 'zc_cookie'.
1759 */
1760 zc->zc_cookie = error;
1761 } else {
1762 ret = error;
1763 }
1764
1765 return (ret);
1766 }
1767
1768 /*
1769 * Try to import the given pool, returning pool stats as appropriate so that
1770 * user land knows which devices are available and overall pool health.
1771 */
1772 static int
zfs_ioc_pool_tryimport(zfs_cmd_t * zc)1773 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1774 {
1775 nvlist_t *tryconfig, *config;
1776 int error;
1777
1778 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1779 zc->zc_iflags, &tryconfig)) != 0)
1780 return (error);
1781
1782 config = spa_tryimport(tryconfig);
1783
1784 nvlist_free(tryconfig);
1785
1786 if (config == NULL)
1787 return (SET_ERROR(EINVAL));
1788
1789 error = put_nvlist(zc, config);
1790 nvlist_free(config);
1791
1792 return (error);
1793 }
1794
1795 /*
1796 * inputs:
1797 * zc_name name of the pool
1798 * zc_cookie scan func (pool_scan_func_t)
1799 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1800 */
1801 static int
zfs_ioc_pool_scan(zfs_cmd_t * zc)1802 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1803 {
1804 spa_t *spa;
1805 int error;
1806
1807 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1808 return (error);
1809
1810 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1811 return (SET_ERROR(EINVAL));
1812
1813 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1814 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1815 else if (zc->zc_cookie == POOL_SCAN_NONE)
1816 error = spa_scan_stop(spa);
1817 else
1818 error = spa_scan(spa, zc->zc_cookie);
1819
1820 spa_close(spa, FTAG);
1821
1822 return (error);
1823 }
1824
1825 static int
zfs_ioc_pool_freeze(zfs_cmd_t * zc)1826 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1827 {
1828 spa_t *spa;
1829 int error;
1830
1831 error = spa_open(zc->zc_name, &spa, FTAG);
1832 if (error == 0) {
1833 spa_freeze(spa);
1834 spa_close(spa, FTAG);
1835 }
1836 return (error);
1837 }
1838
1839 static int
zfs_ioc_pool_upgrade(zfs_cmd_t * zc)1840 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1841 {
1842 spa_t *spa;
1843 int error;
1844
1845 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1846 return (error);
1847
1848 if (zc->zc_cookie < spa_version(spa) ||
1849 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1850 spa_close(spa, FTAG);
1851 return (SET_ERROR(EINVAL));
1852 }
1853
1854 spa_upgrade(spa, zc->zc_cookie);
1855 spa_close(spa, FTAG);
1856
1857 return (error);
1858 }
1859
1860 static int
zfs_ioc_pool_get_history(zfs_cmd_t * zc)1861 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1862 {
1863 spa_t *spa;
1864 char *hist_buf;
1865 uint64_t size;
1866 int error;
1867
1868 if ((size = zc->zc_history_len) == 0)
1869 return (SET_ERROR(EINVAL));
1870
1871 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1872 return (error);
1873
1874 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1875 spa_close(spa, FTAG);
1876 return (SET_ERROR(ENOTSUP));
1877 }
1878
1879 hist_buf = kmem_alloc(size, KM_SLEEP);
1880 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1881 &zc->zc_history_len, hist_buf)) == 0) {
1882 error = ddi_copyout(hist_buf,
1883 (void *)(uintptr_t)zc->zc_history,
1884 zc->zc_history_len, zc->zc_iflags);
1885 }
1886
1887 spa_close(spa, FTAG);
1888 kmem_free(hist_buf, size);
1889 return (error);
1890 }
1891
1892 static int
zfs_ioc_pool_reguid(zfs_cmd_t * zc)1893 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1894 {
1895 spa_t *spa;
1896 int error;
1897
1898 error = spa_open(zc->zc_name, &spa, FTAG);
1899 if (error == 0) {
1900 error = spa_change_guid(spa);
1901 spa_close(spa, FTAG);
1902 }
1903 return (error);
1904 }
1905
1906 static int
zfs_ioc_dsobj_to_dsname(zfs_cmd_t * zc)1907 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1908 {
1909 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1910 }
1911
1912 /*
1913 * inputs:
1914 * zc_name name of filesystem
1915 * zc_obj object to find
1916 *
1917 * outputs:
1918 * zc_value name of object
1919 */
1920 static int
zfs_ioc_obj_to_path(zfs_cmd_t * zc)1921 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1922 {
1923 objset_t *os;
1924 int error;
1925
1926 /* XXX reading from objset not owned */
1927 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1928 FTAG, &os)) != 0)
1929 return (error);
1930 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1931 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1932 return (SET_ERROR(EINVAL));
1933 }
1934 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1935 sizeof (zc->zc_value));
1936 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1937
1938 return (error);
1939 }
1940
1941 /*
1942 * inputs:
1943 * zc_name name of filesystem
1944 * zc_obj object to find
1945 *
1946 * outputs:
1947 * zc_stat stats on object
1948 * zc_value path to object
1949 */
1950 static int
zfs_ioc_obj_to_stats(zfs_cmd_t * zc)1951 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1952 {
1953 objset_t *os;
1954 int error;
1955
1956 /* XXX reading from objset not owned */
1957 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1958 FTAG, &os)) != 0)
1959 return (error);
1960 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1961 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1962 return (SET_ERROR(EINVAL));
1963 }
1964 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1965 sizeof (zc->zc_value));
1966 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1967
1968 return (error);
1969 }
1970
1971 static int
zfs_ioc_vdev_add(zfs_cmd_t * zc)1972 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1973 {
1974 spa_t *spa;
1975 int error;
1976 nvlist_t *config;
1977
1978 error = spa_open(zc->zc_name, &spa, FTAG);
1979 if (error != 0)
1980 return (error);
1981
1982 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1983 zc->zc_iflags, &config);
1984
1985 if (error == 0) {
1986 error = spa_vdev_add(spa, config);
1987 nvlist_free(config);
1988 }
1989 spa_close(spa, FTAG);
1990 return (error);
1991 }
1992
1993 /*
1994 * inputs:
1995 * zc_name name of the pool
1996 * zc_guid guid of vdev to remove
1997 * zc_cookie cancel removal
1998 */
1999 static int
zfs_ioc_vdev_remove(zfs_cmd_t * zc)2000 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
2001 {
2002 spa_t *spa;
2003 int error;
2004
2005 error = spa_open(zc->zc_name, &spa, FTAG);
2006 if (error != 0)
2007 return (error);
2008 if (zc->zc_cookie != 0) {
2009 error = spa_vdev_remove_cancel(spa);
2010 } else {
2011 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
2012 }
2013 spa_close(spa, FTAG);
2014 return (error);
2015 }
2016
2017 static int
zfs_ioc_vdev_set_state(zfs_cmd_t * zc)2018 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
2019 {
2020 spa_t *spa;
2021 int error;
2022 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2023
2024 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2025 return (error);
2026 switch (zc->zc_cookie) {
2027 case VDEV_STATE_ONLINE:
2028 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
2029 break;
2030
2031 case VDEV_STATE_OFFLINE:
2032 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
2033 break;
2034
2035 case VDEV_STATE_FAULTED:
2036 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2037 zc->zc_obj != VDEV_AUX_EXTERNAL)
2038 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2039
2040 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
2041 break;
2042
2043 case VDEV_STATE_DEGRADED:
2044 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2045 zc->zc_obj != VDEV_AUX_EXTERNAL)
2046 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2047
2048 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
2049 break;
2050
2051 default:
2052 error = SET_ERROR(EINVAL);
2053 }
2054 zc->zc_cookie = newstate;
2055 spa_close(spa, FTAG);
2056 return (error);
2057 }
2058
2059 static int
zfs_ioc_vdev_attach(zfs_cmd_t * zc)2060 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
2061 {
2062 spa_t *spa;
2063 int replacing = zc->zc_cookie;
2064 nvlist_t *config;
2065 int error;
2066
2067 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2068 return (error);
2069
2070 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2071 zc->zc_iflags, &config)) == 0) {
2072 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2073 nvlist_free(config);
2074 }
2075
2076 spa_close(spa, FTAG);
2077 return (error);
2078 }
2079
2080 static int
zfs_ioc_vdev_detach(zfs_cmd_t * zc)2081 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2082 {
2083 spa_t *spa;
2084 int error;
2085
2086 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2087 return (error);
2088
2089 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2090
2091 spa_close(spa, FTAG);
2092 return (error);
2093 }
2094
2095 static int
zfs_ioc_vdev_split(zfs_cmd_t * zc)2096 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2097 {
2098 spa_t *spa;
2099 nvlist_t *config, *props = NULL;
2100 int error;
2101 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2102
2103 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2104 return (error);
2105
2106 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2107 zc->zc_iflags, &config)) {
2108 spa_close(spa, FTAG);
2109 return (error);
2110 }
2111
2112 if (zc->zc_nvlist_src_size != 0 && (error =
2113 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2114 zc->zc_iflags, &props))) {
2115 spa_close(spa, FTAG);
2116 nvlist_free(config);
2117 return (error);
2118 }
2119
2120 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2121
2122 spa_close(spa, FTAG);
2123
2124 nvlist_free(config);
2125 nvlist_free(props);
2126
2127 return (error);
2128 }
2129
2130 static int
zfs_ioc_vdev_setpath(zfs_cmd_t * zc)2131 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2132 {
2133 spa_t *spa;
2134 char *path = zc->zc_value;
2135 uint64_t guid = zc->zc_guid;
2136 int error;
2137
2138 error = spa_open(zc->zc_name, &spa, FTAG);
2139 if (error != 0)
2140 return (error);
2141
2142 error = spa_vdev_setpath(spa, guid, path);
2143 spa_close(spa, FTAG);
2144 return (error);
2145 }
2146
2147 static int
zfs_ioc_vdev_setfru(zfs_cmd_t * zc)2148 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2149 {
2150 spa_t *spa;
2151 char *fru = zc->zc_value;
2152 uint64_t guid = zc->zc_guid;
2153 int error;
2154
2155 error = spa_open(zc->zc_name, &spa, FTAG);
2156 if (error != 0)
2157 return (error);
2158
2159 error = spa_vdev_setfru(spa, guid, fru);
2160 spa_close(spa, FTAG);
2161 return (error);
2162 }
2163
2164 static int
zfs_ioc_objset_stats_impl(zfs_cmd_t * zc,objset_t * os)2165 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2166 {
2167 int error = 0;
2168 nvlist_t *nv;
2169
2170 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2171
2172 if (zc->zc_nvlist_dst != 0 &&
2173 (error = dsl_prop_get_all(os, &nv)) == 0) {
2174 dmu_objset_stats(os, nv);
2175 /*
2176 * NB: zvol_get_stats() will read the objset contents,
2177 * which we aren't supposed to do with a
2178 * DS_MODE_USER hold, because it could be
2179 * inconsistent. So this is a bit of a workaround...
2180 * XXX reading with out owning
2181 */
2182 if (!zc->zc_objset_stats.dds_inconsistent &&
2183 dmu_objset_type(os) == DMU_OST_ZVOL) {
2184 error = zvol_get_stats(os, nv);
2185 if (error == EIO)
2186 return (error);
2187 VERIFY0(error);
2188 }
2189 error = put_nvlist(zc, nv);
2190 nvlist_free(nv);
2191 }
2192
2193 return (error);
2194 }
2195
2196 /*
2197 * inputs:
2198 * zc_name name of filesystem
2199 * zc_nvlist_dst_size size of buffer for property nvlist
2200 *
2201 * outputs:
2202 * zc_objset_stats stats
2203 * zc_nvlist_dst property nvlist
2204 * zc_nvlist_dst_size size of property nvlist
2205 */
2206 static int
zfs_ioc_objset_stats(zfs_cmd_t * zc)2207 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2208 {
2209 objset_t *os;
2210 int error;
2211
2212 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2213 if (error == 0) {
2214 error = zfs_ioc_objset_stats_impl(zc, os);
2215 dmu_objset_rele(os, FTAG);
2216 }
2217
2218 return (error);
2219 }
2220
2221 /*
2222 * inputs:
2223 * zc_name name of filesystem
2224 * zc_nvlist_dst_size size of buffer for property nvlist
2225 *
2226 * outputs:
2227 * zc_nvlist_dst received property nvlist
2228 * zc_nvlist_dst_size size of received property nvlist
2229 *
2230 * Gets received properties (distinct from local properties on or after
2231 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2232 * local property values.
2233 */
2234 static int
zfs_ioc_objset_recvd_props(zfs_cmd_t * zc)2235 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2236 {
2237 int error = 0;
2238 nvlist_t *nv;
2239
2240 /*
2241 * Without this check, we would return local property values if the
2242 * caller has not already received properties on or after
2243 * SPA_VERSION_RECVD_PROPS.
2244 */
2245 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2246 return (SET_ERROR(ENOTSUP));
2247
2248 if (zc->zc_nvlist_dst != 0 &&
2249 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2250 error = put_nvlist(zc, nv);
2251 nvlist_free(nv);
2252 }
2253
2254 return (error);
2255 }
2256
2257 static int
nvl_add_zplprop(objset_t * os,nvlist_t * props,zfs_prop_t prop)2258 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2259 {
2260 uint64_t value;
2261 int error;
2262
2263 /*
2264 * zfs_get_zplprop() will either find a value or give us
2265 * the default value (if there is one).
2266 */
2267 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2268 return (error);
2269 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2270 return (0);
2271 }
2272
2273 /*
2274 * inputs:
2275 * zc_name name of filesystem
2276 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2277 *
2278 * outputs:
2279 * zc_nvlist_dst zpl property nvlist
2280 * zc_nvlist_dst_size size of zpl property nvlist
2281 */
2282 static int
zfs_ioc_objset_zplprops(zfs_cmd_t * zc)2283 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2284 {
2285 objset_t *os;
2286 int err;
2287
2288 /* XXX reading without owning */
2289 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2290 return (err);
2291
2292 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2293
2294 /*
2295 * NB: nvl_add_zplprop() will read the objset contents,
2296 * which we aren't supposed to do with a DS_MODE_USER
2297 * hold, because it could be inconsistent.
2298 */
2299 if (zc->zc_nvlist_dst != 0 &&
2300 !zc->zc_objset_stats.dds_inconsistent &&
2301 dmu_objset_type(os) == DMU_OST_ZFS) {
2302 nvlist_t *nv;
2303
2304 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2305 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2306 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2307 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2308 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2309 err = put_nvlist(zc, nv);
2310 nvlist_free(nv);
2311 } else {
2312 err = SET_ERROR(ENOENT);
2313 }
2314 dmu_objset_rele(os, FTAG);
2315 return (err);
2316 }
2317
2318 static boolean_t
dataset_name_hidden(const char * name)2319 dataset_name_hidden(const char *name)
2320 {
2321 /*
2322 * Skip over datasets that are not visible in this zone,
2323 * internal datasets (which have a $ in their name), and
2324 * temporary datasets (which have a % in their name).
2325 */
2326 if (strchr(name, '$') != NULL)
2327 return (B_TRUE);
2328 if (strchr(name, '%') != NULL)
2329 return (B_TRUE);
2330 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2331 return (B_TRUE);
2332 return (B_FALSE);
2333 }
2334
2335 /*
2336 * inputs:
2337 * zc_name name of filesystem
2338 * zc_cookie zap cursor
2339 * zc_nvlist_dst_size size of buffer for property nvlist
2340 *
2341 * outputs:
2342 * zc_name name of next filesystem
2343 * zc_cookie zap cursor
2344 * zc_objset_stats stats
2345 * zc_nvlist_dst property nvlist
2346 * zc_nvlist_dst_size size of property nvlist
2347 */
2348 static int
zfs_ioc_dataset_list_next(zfs_cmd_t * zc)2349 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2350 {
2351 objset_t *os;
2352 int error;
2353 char *p;
2354 size_t orig_len = strlen(zc->zc_name);
2355
2356 top:
2357 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2358 if (error == ENOENT)
2359 error = SET_ERROR(ESRCH);
2360 return (error);
2361 }
2362
2363 p = strrchr(zc->zc_name, '/');
2364 if (p == NULL || p[1] != '\0')
2365 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2366 p = zc->zc_name + strlen(zc->zc_name);
2367
2368 do {
2369 error = dmu_dir_list_next(os,
2370 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2371 NULL, &zc->zc_cookie);
2372 if (error == ENOENT)
2373 error = SET_ERROR(ESRCH);
2374 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2375 dmu_objset_rele(os, FTAG);
2376
2377 /*
2378 * If it's an internal dataset (ie. with a '$' in its name),
2379 * don't try to get stats for it, otherwise we'll return ENOENT.
2380 */
2381 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2382 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2383 if (error == ENOENT) {
2384 /* We lost a race with destroy, get the next one. */
2385 zc->zc_name[orig_len] = '\0';
2386 goto top;
2387 }
2388 }
2389 return (error);
2390 }
2391
2392 /*
2393 * inputs:
2394 * zc_name name of filesystem
2395 * zc_cookie zap cursor
2396 * zc_nvlist_dst_size size of buffer for property nvlist
2397 * zc_simple when set, only name is requested
2398 *
2399 * outputs:
2400 * zc_name name of next snapshot
2401 * zc_objset_stats stats
2402 * zc_nvlist_dst property nvlist
2403 * zc_nvlist_dst_size size of property nvlist
2404 */
2405 static int
zfs_ioc_snapshot_list_next(zfs_cmd_t * zc)2406 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2407 {
2408 objset_t *os;
2409 int error;
2410
2411 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2412 if (error != 0) {
2413 return (error == ENOENT ? ESRCH : error);
2414 }
2415
2416 /*
2417 * A dataset name of maximum length cannot have any snapshots,
2418 * so exit immediately.
2419 */
2420 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2421 ZFS_MAX_DATASET_NAME_LEN) {
2422 dmu_objset_rele(os, FTAG);
2423 return (SET_ERROR(ESRCH));
2424 }
2425
2426 error = dmu_snapshot_list_next(os,
2427 sizeof (zc->zc_name) - strlen(zc->zc_name),
2428 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2429 NULL);
2430
2431 if (error == 0 && !zc->zc_simple) {
2432 dsl_dataset_t *ds;
2433 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2434
2435 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2436 if (error == 0) {
2437 objset_t *ossnap;
2438
2439 error = dmu_objset_from_ds(ds, &ossnap);
2440 if (error == 0)
2441 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2442 dsl_dataset_rele(ds, FTAG);
2443 }
2444 } else if (error == ENOENT) {
2445 error = SET_ERROR(ESRCH);
2446 }
2447
2448 dmu_objset_rele(os, FTAG);
2449 /* if we failed, undo the @ that we tacked on to zc_name */
2450 if (error != 0)
2451 *strchr(zc->zc_name, '@') = '\0';
2452 return (error);
2453 }
2454
2455 static int
zfs_prop_set_userquota(const char * dsname,nvpair_t * pair)2456 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2457 {
2458 const char *propname = nvpair_name(pair);
2459 uint64_t *valary;
2460 unsigned int vallen;
2461 const char *domain;
2462 char *dash;
2463 zfs_userquota_prop_t type;
2464 uint64_t rid;
2465 uint64_t quota;
2466 zfsvfs_t *zfsvfs;
2467 int err;
2468
2469 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2470 nvlist_t *attrs;
2471 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2472 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2473 &pair) != 0)
2474 return (SET_ERROR(EINVAL));
2475 }
2476
2477 /*
2478 * A correctly constructed propname is encoded as
2479 * userquota@<rid>-<domain>.
2480 */
2481 if ((dash = strchr(propname, '-')) == NULL ||
2482 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2483 vallen != 3)
2484 return (SET_ERROR(EINVAL));
2485
2486 domain = dash + 1;
2487 type = valary[0];
2488 rid = valary[1];
2489 quota = valary[2];
2490
2491 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2492 if (err == 0) {
2493 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2494 zfsvfs_rele(zfsvfs, FTAG);
2495 }
2496
2497 return (err);
2498 }
2499
2500 /*
2501 * If the named property is one that has a special function to set its value,
2502 * return 0 on success and a positive error code on failure; otherwise if it is
2503 * not one of the special properties handled by this function, return -1.
2504 *
2505 * XXX: It would be better for callers of the property interface if we handled
2506 * these special cases in dsl_prop.c (in the dsl layer).
2507 */
2508 static int
zfs_prop_set_special(const char * dsname,zprop_source_t source,nvpair_t * pair)2509 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2510 nvpair_t *pair)
2511 {
2512 const char *propname = nvpair_name(pair);
2513 zfs_prop_t prop = zfs_name_to_prop(propname);
2514 uint64_t intval = 0;
2515 char *strval = NULL;
2516 int err = -1;
2517
2518 if (prop == ZPROP_INVAL) {
2519 if (zfs_prop_userquota(propname))
2520 return (zfs_prop_set_userquota(dsname, pair));
2521 return (-1);
2522 }
2523
2524 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2525 nvlist_t *attrs;
2526 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2527 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2528 &pair) == 0);
2529 }
2530
2531 /* all special properties are numeric except for keylocation */
2532 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2533 strval = fnvpair_value_string(pair);
2534 } else {
2535 intval = fnvpair_value_uint64(pair);
2536 }
2537
2538 switch (prop) {
2539 case ZFS_PROP_QUOTA:
2540 err = dsl_dir_set_quota(dsname, source, intval);
2541 break;
2542 case ZFS_PROP_REFQUOTA:
2543 err = dsl_dataset_set_refquota(dsname, source, intval);
2544 break;
2545 case ZFS_PROP_FILESYSTEM_LIMIT:
2546 case ZFS_PROP_SNAPSHOT_LIMIT:
2547 if (intval == UINT64_MAX) {
2548 /* clearing the limit, just do it */
2549 err = 0;
2550 } else {
2551 err = dsl_dir_activate_fs_ss_limit(dsname);
2552 }
2553 /*
2554 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2555 * default path to set the value in the nvlist.
2556 */
2557 if (err == 0)
2558 err = -1;
2559 break;
2560 case ZFS_PROP_KEYLOCATION:
2561 err = dsl_crypto_can_set_keylocation(dsname, strval);
2562
2563 /*
2564 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2565 * default path to set the value in the nvlist.
2566 */
2567 if (err == 0)
2568 err = -1;
2569 break;
2570 case ZFS_PROP_RESERVATION:
2571 err = dsl_dir_set_reservation(dsname, source, intval);
2572 break;
2573 case ZFS_PROP_REFRESERVATION:
2574 err = dsl_dataset_set_refreservation(dsname, source, intval);
2575 break;
2576 case ZFS_PROP_VOLSIZE:
2577 err = zvol_set_volsize(dsname, intval);
2578 break;
2579 case ZFS_PROP_VERSION:
2580 {
2581 zfsvfs_t *zfsvfs;
2582
2583 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2584 break;
2585
2586 err = zfs_set_version(zfsvfs, intval);
2587 zfsvfs_rele(zfsvfs, FTAG);
2588
2589 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2590 zfs_cmd_t *zc;
2591
2592 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2593 (void) strcpy(zc->zc_name, dsname);
2594 (void) zfs_ioc_userspace_upgrade(zc);
2595 (void) zfs_ioc_id_quota_upgrade(zc);
2596 kmem_free(zc, sizeof (zfs_cmd_t));
2597 }
2598 break;
2599 }
2600 default:
2601 err = -1;
2602 }
2603
2604 return (err);
2605 }
2606
2607 /*
2608 * This function is best effort. If it fails to set any of the given properties,
2609 * it continues to set as many as it can and returns the last error
2610 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2611 * with the list of names of all the properties that failed along with the
2612 * corresponding error numbers.
2613 *
2614 * If every property is set successfully, zero is returned and errlist is not
2615 * modified.
2616 */
2617 int
zfs_set_prop_nvlist(const char * dsname,zprop_source_t source,nvlist_t * nvl,nvlist_t * errlist)2618 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2619 nvlist_t *errlist)
2620 {
2621 nvpair_t *pair;
2622 nvpair_t *propval;
2623 int rv = 0;
2624 uint64_t intval;
2625 char *strval;
2626 nvlist_t *genericnvl = fnvlist_alloc();
2627 nvlist_t *retrynvl = fnvlist_alloc();
2628
2629 retry:
2630 pair = NULL;
2631 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2632 const char *propname = nvpair_name(pair);
2633 zfs_prop_t prop = zfs_name_to_prop(propname);
2634 int err = 0;
2635
2636 /* decode the property value */
2637 propval = pair;
2638 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2639 nvlist_t *attrs;
2640 attrs = fnvpair_value_nvlist(pair);
2641 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2642 &propval) != 0)
2643 err = SET_ERROR(EINVAL);
2644 }
2645
2646 /* Validate value type */
2647 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2648 /* inherited properties are expected to be booleans */
2649 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2650 err = SET_ERROR(EINVAL);
2651 } else if (err == 0 && prop == ZPROP_INVAL) {
2652 if (zfs_prop_user(propname)) {
2653 if (nvpair_type(propval) != DATA_TYPE_STRING)
2654 err = SET_ERROR(EINVAL);
2655 } else if (zfs_prop_userquota(propname)) {
2656 if (nvpair_type(propval) !=
2657 DATA_TYPE_UINT64_ARRAY)
2658 err = SET_ERROR(EINVAL);
2659 } else {
2660 err = SET_ERROR(EINVAL);
2661 }
2662 } else if (err == 0) {
2663 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2664 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2665 err = SET_ERROR(EINVAL);
2666 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2667 const char *unused;
2668
2669 intval = fnvpair_value_uint64(propval);
2670
2671 switch (zfs_prop_get_type(prop)) {
2672 case PROP_TYPE_NUMBER:
2673 break;
2674 case PROP_TYPE_STRING:
2675 err = SET_ERROR(EINVAL);
2676 break;
2677 case PROP_TYPE_INDEX:
2678 if (zfs_prop_index_to_string(prop,
2679 intval, &unused) != 0)
2680 err = SET_ERROR(EINVAL);
2681 break;
2682 default:
2683 cmn_err(CE_PANIC,
2684 "unknown property type");
2685 }
2686 } else {
2687 err = SET_ERROR(EINVAL);
2688 }
2689 }
2690
2691 /* Validate permissions */
2692 if (err == 0)
2693 err = zfs_check_settable(dsname, pair, CRED());
2694
2695 if (err == 0) {
2696 if (source == ZPROP_SRC_INHERITED)
2697 err = -1; /* does not need special handling */
2698 else
2699 err = zfs_prop_set_special(dsname, source,
2700 pair);
2701 if (err == -1) {
2702 /*
2703 * For better performance we build up a list of
2704 * properties to set in a single transaction.
2705 */
2706 err = nvlist_add_nvpair(genericnvl, pair);
2707 } else if (err != 0 && nvl != retrynvl) {
2708 /*
2709 * This may be a spurious error caused by
2710 * receiving quota and reservation out of order.
2711 * Try again in a second pass.
2712 */
2713 err = nvlist_add_nvpair(retrynvl, pair);
2714 }
2715 }
2716
2717 if (err != 0) {
2718 if (errlist != NULL)
2719 fnvlist_add_int32(errlist, propname, err);
2720 rv = err;
2721 }
2722 }
2723
2724 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2725 nvl = retrynvl;
2726 goto retry;
2727 }
2728
2729 if (!nvlist_empty(genericnvl) &&
2730 dsl_props_set(dsname, source, genericnvl) != 0) {
2731 /*
2732 * If this fails, we still want to set as many properties as we
2733 * can, so try setting them individually.
2734 */
2735 pair = NULL;
2736 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2737 const char *propname = nvpair_name(pair);
2738 int err = 0;
2739
2740 propval = pair;
2741 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2742 nvlist_t *attrs;
2743 attrs = fnvpair_value_nvlist(pair);
2744 propval = fnvlist_lookup_nvpair(attrs,
2745 ZPROP_VALUE);
2746 }
2747
2748 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2749 strval = fnvpair_value_string(propval);
2750 err = dsl_prop_set_string(dsname, propname,
2751 source, strval);
2752 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2753 err = dsl_prop_inherit(dsname, propname,
2754 source);
2755 } else {
2756 intval = fnvpair_value_uint64(propval);
2757 err = dsl_prop_set_int(dsname, propname, source,
2758 intval);
2759 }
2760
2761 if (err != 0) {
2762 if (errlist != NULL) {
2763 fnvlist_add_int32(errlist, propname,
2764 err);
2765 }
2766 rv = err;
2767 }
2768 }
2769 }
2770 nvlist_free(genericnvl);
2771 nvlist_free(retrynvl);
2772
2773 return (rv);
2774 }
2775
2776 /*
2777 * Check that all the properties are valid user properties.
2778 */
2779 static int
zfs_check_userprops(const char * fsname,nvlist_t * nvl)2780 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2781 {
2782 nvpair_t *pair = NULL;
2783 int error = 0;
2784
2785 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2786 const char *propname = nvpair_name(pair);
2787
2788 if (!zfs_prop_user(propname) ||
2789 nvpair_type(pair) != DATA_TYPE_STRING)
2790 return (SET_ERROR(EINVAL));
2791
2792 if (error = zfs_secpolicy_write_perms(fsname,
2793 ZFS_DELEG_PERM_USERPROP, CRED()))
2794 return (error);
2795
2796 if (strlen(propname) >= ZAP_MAXNAMELEN)
2797 return (SET_ERROR(ENAMETOOLONG));
2798
2799 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2800 return (E2BIG);
2801 }
2802 return (0);
2803 }
2804
2805 static void
props_skip(nvlist_t * props,nvlist_t * skipped,nvlist_t ** newprops)2806 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2807 {
2808 nvpair_t *pair;
2809
2810 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2811
2812 pair = NULL;
2813 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2814 if (nvlist_exists(skipped, nvpair_name(pair)))
2815 continue;
2816
2817 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2818 }
2819 }
2820
2821 static int
clear_received_props(const char * dsname,nvlist_t * props,nvlist_t * skipped)2822 clear_received_props(const char *dsname, nvlist_t *props,
2823 nvlist_t *skipped)
2824 {
2825 int err = 0;
2826 nvlist_t *cleared_props = NULL;
2827 props_skip(props, skipped, &cleared_props);
2828 if (!nvlist_empty(cleared_props)) {
2829 /*
2830 * Acts on local properties until the dataset has received
2831 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2832 */
2833 zprop_source_t flags = (ZPROP_SRC_NONE |
2834 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2835 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2836 }
2837 nvlist_free(cleared_props);
2838 return (err);
2839 }
2840
2841 /*
2842 * inputs:
2843 * zc_name name of filesystem
2844 * zc_value name of property to set
2845 * zc_nvlist_src{_size} nvlist of properties to apply
2846 * zc_cookie received properties flag
2847 *
2848 * outputs:
2849 * zc_nvlist_dst{_size} error for each unapplied received property
2850 */
2851 static int
zfs_ioc_set_prop(zfs_cmd_t * zc)2852 zfs_ioc_set_prop(zfs_cmd_t *zc)
2853 {
2854 nvlist_t *nvl;
2855 boolean_t received = zc->zc_cookie;
2856 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2857 ZPROP_SRC_LOCAL);
2858 nvlist_t *errors;
2859 int error;
2860
2861 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2862 zc->zc_iflags, &nvl)) != 0)
2863 return (error);
2864
2865 if (received) {
2866 nvlist_t *origprops;
2867
2868 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2869 (void) clear_received_props(zc->zc_name,
2870 origprops, nvl);
2871 nvlist_free(origprops);
2872 }
2873
2874 error = dsl_prop_set_hasrecvd(zc->zc_name);
2875 }
2876
2877 errors = fnvlist_alloc();
2878 if (error == 0)
2879 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2880
2881 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2882 (void) put_nvlist(zc, errors);
2883 }
2884
2885 nvlist_free(errors);
2886 nvlist_free(nvl);
2887 return (error);
2888 }
2889
2890 /*
2891 * inputs:
2892 * zc_name name of filesystem
2893 * zc_value name of property to inherit
2894 * zc_cookie revert to received value if TRUE
2895 *
2896 * outputs: none
2897 */
2898 static int
zfs_ioc_inherit_prop(zfs_cmd_t * zc)2899 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2900 {
2901 const char *propname = zc->zc_value;
2902 zfs_prop_t prop = zfs_name_to_prop(propname);
2903 boolean_t received = zc->zc_cookie;
2904 zprop_source_t source = (received
2905 ? ZPROP_SRC_NONE /* revert to received value, if any */
2906 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2907
2908 if (received) {
2909 nvlist_t *dummy;
2910 nvpair_t *pair;
2911 zprop_type_t type;
2912 int err;
2913
2914 /*
2915 * zfs_prop_set_special() expects properties in the form of an
2916 * nvpair with type info.
2917 */
2918 if (prop == ZPROP_INVAL) {
2919 if (!zfs_prop_user(propname))
2920 return (SET_ERROR(EINVAL));
2921
2922 type = PROP_TYPE_STRING;
2923 } else if (prop == ZFS_PROP_VOLSIZE ||
2924 prop == ZFS_PROP_VERSION) {
2925 return (SET_ERROR(EINVAL));
2926 } else {
2927 type = zfs_prop_get_type(prop);
2928 }
2929
2930 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2931
2932 switch (type) {
2933 case PROP_TYPE_STRING:
2934 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2935 break;
2936 case PROP_TYPE_NUMBER:
2937 case PROP_TYPE_INDEX:
2938 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2939 break;
2940 default:
2941 nvlist_free(dummy);
2942 return (SET_ERROR(EINVAL));
2943 }
2944
2945 pair = nvlist_next_nvpair(dummy, NULL);
2946 err = zfs_prop_set_special(zc->zc_name, source, pair);
2947 nvlist_free(dummy);
2948 if (err != -1)
2949 return (err); /* special property already handled */
2950 } else {
2951 /*
2952 * Only check this in the non-received case. We want to allow
2953 * 'inherit -S' to revert non-inheritable properties like quota
2954 * and reservation to the received or default values even though
2955 * they are not considered inheritable.
2956 */
2957 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2958 return (SET_ERROR(EINVAL));
2959 }
2960
2961 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2962 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2963 }
2964
2965 static int
zfs_ioc_pool_set_props(zfs_cmd_t * zc)2966 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2967 {
2968 nvlist_t *props;
2969 spa_t *spa;
2970 int error;
2971 nvpair_t *pair;
2972
2973 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2974 zc->zc_iflags, &props))
2975 return (error);
2976
2977 /*
2978 * If the only property is the configfile, then just do a spa_lookup()
2979 * to handle the faulted case.
2980 */
2981 pair = nvlist_next_nvpair(props, NULL);
2982 if (pair != NULL && strcmp(nvpair_name(pair),
2983 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2984 nvlist_next_nvpair(props, pair) == NULL) {
2985 mutex_enter(&spa_namespace_lock);
2986 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2987 spa_configfile_set(spa, props, B_FALSE);
2988 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2989 }
2990 mutex_exit(&spa_namespace_lock);
2991 if (spa != NULL) {
2992 nvlist_free(props);
2993 return (0);
2994 }
2995 }
2996
2997 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2998 nvlist_free(props);
2999 return (error);
3000 }
3001
3002 error = spa_prop_set(spa, props);
3003
3004 nvlist_free(props);
3005 spa_close(spa, FTAG);
3006
3007 return (error);
3008 }
3009
3010 static int
zfs_ioc_pool_get_props(zfs_cmd_t * zc)3011 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
3012 {
3013 spa_t *spa;
3014 int error;
3015 nvlist_t *nvp = NULL;
3016
3017 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3018 /*
3019 * If the pool is faulted, there may be properties we can still
3020 * get (such as altroot and cachefile), so attempt to get them
3021 * anyway.
3022 */
3023 mutex_enter(&spa_namespace_lock);
3024 if ((spa = spa_lookup(zc->zc_name)) != NULL)
3025 error = spa_prop_get(spa, &nvp);
3026 mutex_exit(&spa_namespace_lock);
3027 } else {
3028 error = spa_prop_get(spa, &nvp);
3029 spa_close(spa, FTAG);
3030 }
3031
3032 if (error == 0 && zc->zc_nvlist_dst != 0)
3033 error = put_nvlist(zc, nvp);
3034 else
3035 error = SET_ERROR(EFAULT);
3036
3037 nvlist_free(nvp);
3038 return (error);
3039 }
3040
3041 /*
3042 * inputs:
3043 * zc_name name of filesystem
3044 * zc_nvlist_src{_size} nvlist of delegated permissions
3045 * zc_perm_action allow/unallow flag
3046 *
3047 * outputs: none
3048 */
3049 static int
zfs_ioc_set_fsacl(zfs_cmd_t * zc)3050 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3051 {
3052 int error;
3053 nvlist_t *fsaclnv = NULL;
3054
3055 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3056 zc->zc_iflags, &fsaclnv)) != 0)
3057 return (error);
3058
3059 /*
3060 * Verify nvlist is constructed correctly
3061 */
3062 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3063 nvlist_free(fsaclnv);
3064 return (SET_ERROR(EINVAL));
3065 }
3066
3067 /*
3068 * If we don't have PRIV_SYS_MOUNT, then validate
3069 * that user is allowed to hand out each permission in
3070 * the nvlist(s)
3071 */
3072
3073 error = secpolicy_zfs(CRED());
3074 if (error != 0) {
3075 if (zc->zc_perm_action == B_FALSE) {
3076 error = dsl_deleg_can_allow(zc->zc_name,
3077 fsaclnv, CRED());
3078 } else {
3079 error = dsl_deleg_can_unallow(zc->zc_name,
3080 fsaclnv, CRED());
3081 }
3082 }
3083
3084 if (error == 0)
3085 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3086
3087 nvlist_free(fsaclnv);
3088 return (error);
3089 }
3090
3091 /*
3092 * inputs:
3093 * zc_name name of filesystem
3094 *
3095 * outputs:
3096 * zc_nvlist_src{_size} nvlist of delegated permissions
3097 */
3098 static int
zfs_ioc_get_fsacl(zfs_cmd_t * zc)3099 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3100 {
3101 nvlist_t *nvp;
3102 int error;
3103
3104 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3105 error = put_nvlist(zc, nvp);
3106 nvlist_free(nvp);
3107 }
3108
3109 return (error);
3110 }
3111
3112 /* ARGSUSED */
3113 static void
zfs_create_cb(objset_t * os,void * arg,cred_t * cr,dmu_tx_t * tx)3114 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3115 {
3116 zfs_creat_t *zct = arg;
3117
3118 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3119 }
3120
3121 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3122
3123 /*
3124 * inputs:
3125 * os parent objset pointer (NULL if root fs)
3126 * fuids_ok fuids allowed in this version of the spa?
3127 * sa_ok SAs allowed in this version of the spa?
3128 * createprops list of properties requested by creator
3129 *
3130 * outputs:
3131 * zplprops values for the zplprops we attach to the master node object
3132 * is_ci true if requested file system will be purely case-insensitive
3133 *
3134 * Determine the settings for utf8only, normalization and
3135 * casesensitivity. Specific values may have been requested by the
3136 * creator and/or we can inherit values from the parent dataset. If
3137 * the file system is of too early a vintage, a creator can not
3138 * request settings for these properties, even if the requested
3139 * setting is the default value. We don't actually want to create dsl
3140 * properties for these, so remove them from the source nvlist after
3141 * processing.
3142 */
3143 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)3144 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3145 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3146 nvlist_t *zplprops, boolean_t *is_ci)
3147 {
3148 uint64_t sense = ZFS_PROP_UNDEFINED;
3149 uint64_t norm = ZFS_PROP_UNDEFINED;
3150 uint64_t u8 = ZFS_PROP_UNDEFINED;
3151
3152 ASSERT(zplprops != NULL);
3153
3154 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3155 return (SET_ERROR(EINVAL));
3156
3157 /*
3158 * Pull out creator prop choices, if any.
3159 */
3160 if (createprops) {
3161 (void) nvlist_lookup_uint64(createprops,
3162 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3163 (void) nvlist_lookup_uint64(createprops,
3164 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3165 (void) nvlist_remove_all(createprops,
3166 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3167 (void) nvlist_lookup_uint64(createprops,
3168 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3169 (void) nvlist_remove_all(createprops,
3170 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3171 (void) nvlist_lookup_uint64(createprops,
3172 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3173 (void) nvlist_remove_all(createprops,
3174 zfs_prop_to_name(ZFS_PROP_CASE));
3175 }
3176
3177 /*
3178 * If the zpl version requested is whacky or the file system
3179 * or pool is version is too "young" to support normalization
3180 * and the creator tried to set a value for one of the props,
3181 * error out.
3182 */
3183 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3184 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3185 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3186 (zplver < ZPL_VERSION_NORMALIZATION &&
3187 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3188 sense != ZFS_PROP_UNDEFINED)))
3189 return (SET_ERROR(ENOTSUP));
3190
3191 /*
3192 * Put the version in the zplprops
3193 */
3194 VERIFY(nvlist_add_uint64(zplprops,
3195 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3196
3197 if (norm == ZFS_PROP_UNDEFINED)
3198 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3199 VERIFY(nvlist_add_uint64(zplprops,
3200 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3201
3202 /*
3203 * If we're normalizing, names must always be valid UTF-8 strings.
3204 */
3205 if (norm)
3206 u8 = 1;
3207 if (u8 == ZFS_PROP_UNDEFINED)
3208 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3209 VERIFY(nvlist_add_uint64(zplprops,
3210 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3211
3212 if (sense == ZFS_PROP_UNDEFINED)
3213 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3214 VERIFY(nvlist_add_uint64(zplprops,
3215 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3216
3217 if (is_ci)
3218 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3219
3220 return (0);
3221 }
3222
3223 static int
zfs_fill_zplprops(const char * dataset,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)3224 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3225 nvlist_t *zplprops, boolean_t *is_ci)
3226 {
3227 boolean_t fuids_ok, sa_ok;
3228 uint64_t zplver = ZPL_VERSION;
3229 objset_t *os = NULL;
3230 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3231 char *cp;
3232 spa_t *spa;
3233 uint64_t spa_vers;
3234 int error;
3235
3236 (void) strlcpy(parentname, dataset, sizeof (parentname));
3237 cp = strrchr(parentname, '/');
3238 ASSERT(cp != NULL);
3239 cp[0] = '\0';
3240
3241 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3242 return (error);
3243
3244 spa_vers = spa_version(spa);
3245 spa_close(spa, FTAG);
3246
3247 zplver = zfs_zpl_version_map(spa_vers);
3248 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3249 sa_ok = (zplver >= ZPL_VERSION_SA);
3250
3251 /*
3252 * Open parent object set so we can inherit zplprop values.
3253 */
3254 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3255 return (error);
3256
3257 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3258 zplprops, is_ci);
3259 dmu_objset_rele(os, FTAG);
3260 return (error);
3261 }
3262
3263 static int
zfs_fill_zplprops_root(uint64_t spa_vers,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)3264 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3265 nvlist_t *zplprops, boolean_t *is_ci)
3266 {
3267 boolean_t fuids_ok;
3268 boolean_t sa_ok;
3269 uint64_t zplver = ZPL_VERSION;
3270 int error;
3271
3272 zplver = zfs_zpl_version_map(spa_vers);
3273 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3274 sa_ok = (zplver >= ZPL_VERSION_SA);
3275
3276 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3277 createprops, zplprops, is_ci);
3278 return (error);
3279 }
3280
3281 /*
3282 * innvl: {
3283 * "type" -> dmu_objset_type_t (int32)
3284 * (optional) "props" -> { prop -> value }
3285 * (optional) "hidden_args" -> { "wkeydata" -> value }
3286 * raw uint8_t array of encryption wrapping key data (32 bytes)
3287 * }
3288 *
3289 * outnvl: propname -> error code (int32)
3290 */
3291
3292 static const zfs_ioc_key_t zfs_keys_create[] = {
3293 {"type", DATA_TYPE_INT32, 0},
3294 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3295 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3296 };
3297
3298 static int
zfs_ioc_create(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)3299 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3300 {
3301 int error = 0;
3302 zfs_creat_t zct = { 0 };
3303 nvlist_t *nvprops = NULL;
3304 nvlist_t *hidden_args = NULL;
3305 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3306 dmu_objset_type_t type;
3307 boolean_t is_insensitive = B_FALSE;
3308 dsl_crypto_params_t *dcp = NULL;
3309
3310 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3311 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3312 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3313
3314 switch (type) {
3315 case DMU_OST_ZFS:
3316 cbfunc = zfs_create_cb;
3317 break;
3318
3319 case DMU_OST_ZVOL:
3320 cbfunc = zvol_create_cb;
3321 break;
3322
3323 default:
3324 cbfunc = NULL;
3325 break;
3326 }
3327 if (strchr(fsname, '@') ||
3328 strchr(fsname, '%'))
3329 return (SET_ERROR(EINVAL));
3330
3331 zct.zct_props = nvprops;
3332
3333 if (cbfunc == NULL)
3334 return (SET_ERROR(EINVAL));
3335
3336 if (type == DMU_OST_ZVOL) {
3337 uint64_t volsize, volblocksize;
3338
3339 if (nvprops == NULL)
3340 return (SET_ERROR(EINVAL));
3341 if (nvlist_lookup_uint64(nvprops,
3342 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3343 return (SET_ERROR(EINVAL));
3344
3345 if ((error = nvlist_lookup_uint64(nvprops,
3346 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3347 &volblocksize)) != 0 && error != ENOENT)
3348 return (SET_ERROR(EINVAL));
3349
3350 if (error != 0)
3351 volblocksize = zfs_prop_default_numeric(
3352 ZFS_PROP_VOLBLOCKSIZE);
3353
3354 if ((error = zvol_check_volblocksize(
3355 volblocksize)) != 0 ||
3356 (error = zvol_check_volsize(volsize,
3357 volblocksize)) != 0)
3358 return (error);
3359 } else if (type == DMU_OST_ZFS) {
3360 int error;
3361
3362 /*
3363 * We have to have normalization and
3364 * case-folding flags correct when we do the
3365 * file system creation, so go figure them out
3366 * now.
3367 */
3368 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3369 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3370 error = zfs_fill_zplprops(fsname, nvprops,
3371 zct.zct_zplprops, &is_insensitive);
3372 if (error != 0) {
3373 nvlist_free(zct.zct_zplprops);
3374 return (error);
3375 }
3376 }
3377
3378 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3379 hidden_args, &dcp);
3380 if (error != 0) {
3381 nvlist_free(zct.zct_zplprops);
3382 return (error);
3383 }
3384
3385 error = dmu_objset_create(fsname, type,
3386 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3387
3388 nvlist_free(zct.zct_zplprops);
3389 dsl_crypto_params_free(dcp, !!error);
3390
3391 /*
3392 * It would be nice to do this atomically.
3393 */
3394 if (error == 0) {
3395 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3396 nvprops, outnvl);
3397 if (error != 0)
3398 (void) dsl_destroy_head(fsname);
3399 }
3400 return (error);
3401 }
3402
3403 /*
3404 * innvl: {
3405 * "origin" -> name of origin snapshot
3406 * (optional) "props" -> { prop -> value }
3407 * (optional) "hidden_args" -> { "wkeydata" -> value }
3408 * raw uint8_t array of encryption wrapping key data (32 bytes)
3409 * }
3410 *
3411 * outnvl: propname -> error code (int32)
3412 */
3413 static const zfs_ioc_key_t zfs_keys_clone[] = {
3414 {"origin", DATA_TYPE_STRING, 0},
3415 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3416 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3417 };
3418
3419 static int
zfs_ioc_clone(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)3420 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3421 {
3422 int error = 0;
3423 nvlist_t *nvprops = NULL;
3424 char *origin_name;
3425
3426 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3427 return (SET_ERROR(EINVAL));
3428 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3429
3430 if (strchr(fsname, '@') ||
3431 strchr(fsname, '%'))
3432 return (SET_ERROR(EINVAL));
3433
3434 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3435 return (SET_ERROR(EINVAL));
3436
3437 error = dmu_objset_clone(fsname, origin_name);
3438
3439 /*
3440 * It would be nice to do this atomically.
3441 */
3442 if (error == 0) {
3443 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3444 nvprops, outnvl);
3445 if (error != 0)
3446 (void) dsl_destroy_head(fsname);
3447 }
3448 return (error);
3449 }
3450
3451 static const zfs_ioc_key_t zfs_keys_remap[] = {
3452 /* no nvl keys */
3453 };
3454
3455 /* ARGSUSED */
3456 static int
zfs_ioc_remap(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)3457 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3458 {
3459 if (strchr(fsname, '@') ||
3460 strchr(fsname, '%'))
3461 return (SET_ERROR(EINVAL));
3462
3463 return (dmu_objset_remap_indirects(fsname));
3464 }
3465
3466 /*
3467 * innvl: {
3468 * "snaps" -> { snapshot1, snapshot2 }
3469 * (optional) "props" -> { prop -> value (string) }
3470 * }
3471 *
3472 * outnvl: snapshot -> error code (int32)
3473 */
3474 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3475 {"snaps", DATA_TYPE_NVLIST, 0},
3476 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3477 };
3478
3479 static int
zfs_ioc_snapshot(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3480 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3481 {
3482 nvlist_t *snaps;
3483 nvlist_t *props = NULL;
3484 int error, poollen;
3485 nvpair_t *pair;
3486
3487 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3488 if ((error = zfs_check_userprops(poolname, props)) != 0)
3489 return (error);
3490
3491 if (!nvlist_empty(props) &&
3492 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3493 return (SET_ERROR(ENOTSUP));
3494
3495 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3496 poollen = strlen(poolname);
3497 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3498 pair = nvlist_next_nvpair(snaps, pair)) {
3499 const char *name = nvpair_name(pair);
3500 const char *cp = strchr(name, '@');
3501
3502 /*
3503 * The snap name must contain an @, and the part after it must
3504 * contain only valid characters.
3505 */
3506 if (cp == NULL ||
3507 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3508 return (SET_ERROR(EINVAL));
3509
3510 /*
3511 * The snap must be in the specified pool.
3512 */
3513 if (strncmp(name, poolname, poollen) != 0 ||
3514 (name[poollen] != '/' && name[poollen] != '@'))
3515 return (SET_ERROR(EXDEV));
3516
3517 /* This must be the only snap of this fs. */
3518 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3519 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3520 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3521 == 0) {
3522 return (SET_ERROR(EXDEV));
3523 }
3524 }
3525 }
3526
3527 error = dsl_dataset_snapshot(snaps, props, outnvl);
3528 return (error);
3529 }
3530
3531 /*
3532 * innvl: "message" -> string
3533 */
3534 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3535 {"message", DATA_TYPE_STRING, 0},
3536 };
3537
3538 /* ARGSUSED */
3539 static int
zfs_ioc_log_history(const char * unused,nvlist_t * innvl,nvlist_t * outnvl)3540 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3541 {
3542 char *message;
3543 spa_t *spa;
3544 int error;
3545 char *poolname;
3546
3547 /*
3548 * The poolname in the ioctl is not set, we get it from the TSD,
3549 * which was set at the end of the last successful ioctl that allows
3550 * logging. The secpolicy func already checked that it is set.
3551 * Only one log ioctl is allowed after each successful ioctl, so
3552 * we clear the TSD here.
3553 */
3554 poolname = tsd_get(zfs_allow_log_key);
3555 (void) tsd_set(zfs_allow_log_key, NULL);
3556 error = spa_open(poolname, &spa, FTAG);
3557 strfree(poolname);
3558 if (error != 0)
3559 return (error);
3560
3561 message = fnvlist_lookup_string(innvl, "message");
3562
3563 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3564 spa_close(spa, FTAG);
3565 return (SET_ERROR(ENOTSUP));
3566 }
3567
3568 error = spa_history_log(spa, message);
3569 spa_close(spa, FTAG);
3570 return (error);
3571 }
3572
3573 /*
3574 * This ioctl is used to set the bootenv configuration on the current
3575 * pool. This configuration is stored in the second padding area of the label,
3576 * and it is used by the bootloader(s) to store bootloader and/or system
3577 * specific data.
3578 * The data is stored as nvlist data stream, and is protected by
3579 * an embedded checksum.
3580 * The version can have two possible values:
3581 * VB_RAW: nvlist should have key GRUB_ENVMAP, value DATA_TYPE_STRING.
3582 * VB_NVLIST: nvlist with arbitrary <key, value> pairs.
3583 */
3584 static const zfs_ioc_key_t zfs_keys_set_bootenv[] = {
3585 {"version", DATA_TYPE_UINT64, 0},
3586 {"<keys>", DATA_TYPE_ANY, ZK_OPTIONAL | ZK_WILDCARDLIST},
3587 };
3588
3589 static int
zfs_ioc_set_bootenv(const char * name,nvlist_t * innvl,nvlist_t * outnvl __unused)3590 zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl,
3591 nvlist_t *outnvl __unused)
3592 {
3593 int error;
3594 spa_t *spa;
3595
3596 if ((error = spa_open(name, &spa, FTAG)) != 0)
3597 return (error);
3598 spa_vdev_state_enter(spa, SCL_ALL);
3599 error = vdev_label_write_bootenv(spa->spa_root_vdev, innvl);
3600 (void) spa_vdev_state_exit(spa, NULL, 0);
3601 spa_close(spa, FTAG);
3602 return (error);
3603 }
3604
3605 static const zfs_ioc_key_t zfs_keys_get_bootenv[] = {
3606 /* no nvl keys */
3607 };
3608
3609 static int
zfs_ioc_get_bootenv(const char * name,nvlist_t * innvl __unused,nvlist_t * outnvl)3610 zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl __unused,
3611 nvlist_t *outnvl)
3612 {
3613 spa_t *spa;
3614 int error;
3615
3616 if ((error = spa_open(name, &spa, FTAG)) != 0)
3617 return (error);
3618 spa_vdev_state_enter(spa, SCL_ALL);
3619 error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl);
3620 (void) spa_vdev_state_exit(spa, NULL, 0);
3621 spa_close(spa, FTAG);
3622 return (error);
3623 }
3624
3625 /*
3626 * The dp_config_rwlock must not be held when calling this, because the
3627 * unmount may need to write out data.
3628 *
3629 * This function is best-effort. Callers must deal gracefully if it
3630 * remains mounted (or is remounted after this call).
3631 *
3632 * Returns 0 if the argument is not a snapshot, or it is not currently a
3633 * filesystem, or we were able to unmount it. Returns error code otherwise.
3634 */
3635 void
zfs_unmount_snap(const char * snapname)3636 zfs_unmount_snap(const char *snapname)
3637 {
3638 vfs_t *vfsp = NULL;
3639 zfsvfs_t *zfsvfs = NULL;
3640
3641 if (strchr(snapname, '@') == NULL)
3642 return;
3643
3644 int err = getzfsvfs(snapname, &zfsvfs);
3645 if (err != 0) {
3646 ASSERT3P(zfsvfs, ==, NULL);
3647 return;
3648 }
3649 vfsp = zfsvfs->z_vfs;
3650
3651 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3652
3653 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3654 VFS_RELE(vfsp);
3655 if (err != 0)
3656 return;
3657
3658 /*
3659 * Always force the unmount for snapshots.
3660 */
3661 (void) dounmount(vfsp, MS_FORCE, kcred);
3662 }
3663
3664 /* ARGSUSED */
3665 static int
zfs_unmount_snap_cb(const char * snapname,void * arg)3666 zfs_unmount_snap_cb(const char *snapname, void *arg)
3667 {
3668 zfs_unmount_snap(snapname);
3669 return (0);
3670 }
3671
3672 /*
3673 * When a clone is destroyed, its origin may also need to be destroyed,
3674 * in which case it must be unmounted. This routine will do that unmount
3675 * if necessary.
3676 */
3677 void
zfs_destroy_unmount_origin(const char * fsname)3678 zfs_destroy_unmount_origin(const char *fsname)
3679 {
3680 int error;
3681 objset_t *os;
3682 dsl_dataset_t *ds;
3683
3684 error = dmu_objset_hold(fsname, FTAG, &os);
3685 if (error != 0)
3686 return;
3687 ds = dmu_objset_ds(os);
3688 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3689 char originname[ZFS_MAX_DATASET_NAME_LEN];
3690 dsl_dataset_name(ds->ds_prev, originname);
3691 dmu_objset_rele(os, FTAG);
3692 zfs_unmount_snap(originname);
3693 } else {
3694 dmu_objset_rele(os, FTAG);
3695 }
3696 }
3697
3698 /*
3699 * innvl: {
3700 * "snaps" -> { snapshot1, snapshot2 }
3701 * (optional boolean) "defer"
3702 * }
3703 *
3704 * outnvl: snapshot -> error code (int32)
3705 *
3706 */
3707 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3708 {"snaps", DATA_TYPE_NVLIST, 0},
3709 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3710 };
3711
3712 /* ARGSUSED */
3713 static int
zfs_ioc_destroy_snaps(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3714 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3715 {
3716 nvlist_t *snaps;
3717 nvpair_t *pair;
3718 boolean_t defer;
3719
3720 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3721 return (SET_ERROR(EINVAL));
3722 defer = nvlist_exists(innvl, "defer");
3723
3724 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3725 pair = nvlist_next_nvpair(snaps, pair)) {
3726 zfs_unmount_snap(nvpair_name(pair));
3727 }
3728
3729 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3730 }
3731
3732 /*
3733 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3734 * All bookmarks must be in the same pool.
3735 *
3736 * innvl: {
3737 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3738 * }
3739 *
3740 * outnvl: bookmark -> error code (int32)
3741 *
3742 */
3743 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3744 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3745 };
3746
3747 /* ARGSUSED */
3748 static int
zfs_ioc_bookmark(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3749 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3750 {
3751 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3752 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3753 char *snap_name;
3754
3755 /*
3756 * Verify the snapshot argument.
3757 */
3758 if (nvpair_value_string(pair, &snap_name) != 0)
3759 return (SET_ERROR(EINVAL));
3760
3761
3762 /* Verify that the keys (bookmarks) are unique */
3763 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3764 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3765 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3766 return (SET_ERROR(EINVAL));
3767 }
3768 }
3769
3770 return (dsl_bookmark_create(innvl, outnvl));
3771 }
3772
3773 /*
3774 * innvl: {
3775 * property 1, property 2, ...
3776 * }
3777 *
3778 * outnvl: {
3779 * bookmark name 1 -> { property 1, property 2, ... },
3780 * bookmark name 2 -> { property 1, property 2, ... }
3781 * }
3782 *
3783 */
3784 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3785 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3786 };
3787
3788 static int
zfs_ioc_get_bookmarks(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)3789 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3790 {
3791 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3792 }
3793
3794 /*
3795 * innvl: {
3796 * bookmark name 1, bookmark name 2
3797 * }
3798 *
3799 * outnvl: bookmark -> error code (int32)
3800 *
3801 */
3802 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3803 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3804 };
3805
3806 static int
zfs_ioc_destroy_bookmarks(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3807 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3808 nvlist_t *outnvl)
3809 {
3810 int error, poollen;
3811
3812 poollen = strlen(poolname);
3813 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3814 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3815 const char *name = nvpair_name(pair);
3816 const char *cp = strchr(name, '#');
3817
3818 /*
3819 * The bookmark name must contain an #, and the part after it
3820 * must contain only valid characters.
3821 */
3822 if (cp == NULL ||
3823 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3824 return (SET_ERROR(EINVAL));
3825
3826 /*
3827 * The bookmark must be in the specified pool.
3828 */
3829 if (strncmp(name, poolname, poollen) != 0 ||
3830 (name[poollen] != '/' && name[poollen] != '#'))
3831 return (SET_ERROR(EXDEV));
3832 }
3833
3834 error = dsl_bookmark_destroy(innvl, outnvl);
3835 return (error);
3836 }
3837
3838 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3839 {"program", DATA_TYPE_STRING, 0},
3840 {"arg", DATA_TYPE_ANY, 0},
3841 {"hidden_args", DATA_TYPE_ANY, ZK_OPTIONAL},
3842 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3843 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3844 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3845 };
3846
3847 static int
zfs_ioc_channel_program(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3848 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3849 nvlist_t *outnvl)
3850 {
3851 char *program;
3852 uint64_t instrlimit, memlimit;
3853 boolean_t sync_flag;
3854 nvpair_t *nvarg = NULL;
3855 nvlist_t *hidden_args = NULL;
3856
3857 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
3858 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3859 sync_flag = B_TRUE;
3860 }
3861 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3862 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3863 }
3864 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3865 memlimit = ZCP_DEFAULT_MEMLIMIT;
3866 }
3867 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
3868
3869 /* hidden args are optional */
3870 if (nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args) == 0) {
3871 nvlist_t *argnvl = fnvpair_value_nvlist(nvarg);
3872 int ret;
3873
3874 ret = nvlist_add_nvlist(argnvl, ZPOOL_HIDDEN_ARGS, hidden_args);
3875 if (ret != 0)
3876 return (ret);
3877 }
3878
3879 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3880 return (EINVAL);
3881 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3882 return (EINVAL);
3883
3884 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3885 nvarg, outnvl));
3886 }
3887
3888 /*
3889 * innvl: unused
3890 * outnvl: empty
3891 */
3892 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3893 /* no nvl keys */
3894 };
3895
3896 /* ARGSUSED */
3897 static int
zfs_ioc_pool_checkpoint(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3898 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3899 {
3900 return (spa_checkpoint(poolname));
3901 }
3902
3903 /*
3904 * innvl: unused
3905 * outnvl: empty
3906 */
3907 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
3908 /* no nvl keys */
3909 };
3910
3911 /* ARGSUSED */
3912 static int
zfs_ioc_pool_discard_checkpoint(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3913 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3914 nvlist_t *outnvl)
3915 {
3916 return (spa_checkpoint_discard(poolname));
3917 }
3918
3919 /*
3920 * inputs:
3921 * zc_name name of dataset to destroy
3922 * zc_defer_destroy mark for deferred destroy
3923 *
3924 * outputs: none
3925 */
3926 static int
zfs_ioc_destroy(zfs_cmd_t * zc)3927 zfs_ioc_destroy(zfs_cmd_t *zc)
3928 {
3929 objset_t *os;
3930 dmu_objset_type_t ost;
3931 int err;
3932
3933 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3934 if (err != 0)
3935 return (err);
3936 ost = dmu_objset_type(os);
3937 dmu_objset_rele(os, FTAG);
3938
3939 if (ost == DMU_OST_ZFS)
3940 zfs_unmount_snap(zc->zc_name);
3941
3942 if (strchr(zc->zc_name, '@')) {
3943 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3944 } else {
3945 err = dsl_destroy_head(zc->zc_name);
3946 if (err == EEXIST) {
3947 /*
3948 * It is possible that the given DS may have
3949 * hidden child (%recv) datasets - "leftovers"
3950 * resulting from the previously interrupted
3951 * 'zfs receive'.
3952 *
3953 * 6 extra bytes for /%recv
3954 */
3955 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
3956
3957 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
3958 zc->zc_name, recv_clone_name) >=
3959 sizeof (namebuf))
3960 return (SET_ERROR(EINVAL));
3961
3962 /*
3963 * Try to remove the hidden child (%recv) and after
3964 * that try to remove the target dataset.
3965 * If the hidden child (%recv) does not exist
3966 * the original error (EEXIST) will be returned
3967 */
3968 err = dsl_destroy_head(namebuf);
3969 if (err == 0)
3970 err = dsl_destroy_head(zc->zc_name);
3971 else if (err == ENOENT)
3972 err = SET_ERROR(EEXIST);
3973 }
3974 }
3975 if (ost == DMU_OST_ZVOL && err == 0)
3976 (void) zvol_remove_minor(zc->zc_name);
3977 return (err);
3978 }
3979
3980 /*
3981 * innvl: {
3982 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
3983 * "initialize_vdevs": { -> guids to initialize (nvlist)
3984 * "vdev_path_1": vdev_guid_1, (uint64),
3985 * "vdev_path_2": vdev_guid_2, (uint64),
3986 * ...
3987 * },
3988 * }
3989 *
3990 * outnvl: {
3991 * "initialize_vdevs": { -> initialization errors (nvlist)
3992 * "vdev_path_1": errno, see function body for possible errnos (uint64)
3993 * "vdev_path_2": errno, ... (uint64)
3994 * ...
3995 * }
3996 * }
3997 *
3998 * EINVAL is returned for an unknown command or if any of the provided vdev
3999 * guids have be specified with a type other than uint64.
4000 */
4001 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
4002 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
4003 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
4004 };
4005
4006 static int
zfs_ioc_pool_initialize(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4007 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4008 {
4009 uint64_t cmd_type;
4010 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
4011 &cmd_type) != 0) {
4012 return (SET_ERROR(EINVAL));
4013 }
4014
4015 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
4016 cmd_type == POOL_INITIALIZE_START ||
4017 cmd_type == POOL_INITIALIZE_SUSPEND)) {
4018 return (SET_ERROR(EINVAL));
4019 }
4020
4021 nvlist_t *vdev_guids;
4022 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
4023 &vdev_guids) != 0) {
4024 return (SET_ERROR(EINVAL));
4025 }
4026
4027 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4028 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4029 uint64_t vdev_guid;
4030 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4031 return (SET_ERROR(EINVAL));
4032 }
4033 }
4034
4035 spa_t *spa;
4036 int error = spa_open(poolname, &spa, FTAG);
4037 if (error != 0)
4038 return (error);
4039
4040 nvlist_t *vdev_errlist = fnvlist_alloc();
4041 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
4042 vdev_errlist);
4043
4044 if (fnvlist_size(vdev_errlist) > 0) {
4045 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
4046 vdev_errlist);
4047 }
4048 fnvlist_free(vdev_errlist);
4049
4050 spa_close(spa, FTAG);
4051 return (total_errors > 0 ? EINVAL : 0);
4052 }
4053
4054 /*
4055 * innvl: {
4056 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
4057 * "trim_vdevs": { -> guids to TRIM (nvlist)
4058 * "vdev_path_1": vdev_guid_1, (uint64),
4059 * "vdev_path_2": vdev_guid_2, (uint64),
4060 * ...
4061 * },
4062 * "trim_rate" -> Target TRIM rate in bytes/sec.
4063 * "trim_secure" -> Set to request a secure TRIM.
4064 * }
4065 *
4066 * outnvl: {
4067 * "trim_vdevs": { -> TRIM errors (nvlist)
4068 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4069 * "vdev_path_2": errno, ... (uint64)
4070 * ...
4071 * }
4072 * }
4073 *
4074 * EINVAL is returned for an unknown command or if any of the provided vdev
4075 * guids have be specified with a type other than uint64.
4076 */
4077 static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
4078 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0},
4079 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0},
4080 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL},
4081 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
4082 };
4083
4084 static int
zfs_ioc_pool_trim(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4085 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4086 {
4087 uint64_t cmd_type;
4088 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
4089 return (SET_ERROR(EINVAL));
4090
4091 if (!(cmd_type == POOL_TRIM_CANCEL ||
4092 cmd_type == POOL_TRIM_START ||
4093 cmd_type == POOL_TRIM_SUSPEND)) {
4094 return (SET_ERROR(EINVAL));
4095 }
4096
4097 nvlist_t *vdev_guids;
4098 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4099 return (SET_ERROR(EINVAL));
4100
4101 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4102 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4103 uint64_t vdev_guid;
4104 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4105 return (SET_ERROR(EINVAL));
4106 }
4107 }
4108
4109 /* Optional, defaults to maximum rate when not provided */
4110 uint64_t rate;
4111 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4112 rate = 0;
4113
4114 /* Optional, defaults to standard TRIM when not provided */
4115 boolean_t secure;
4116 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4117 &secure) != 0) {
4118 secure = B_FALSE;
4119 }
4120
4121 spa_t *spa;
4122 int error = spa_open(poolname, &spa, FTAG);
4123 if (error != 0)
4124 return (error);
4125
4126 nvlist_t *vdev_errlist = fnvlist_alloc();
4127 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4128 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4129
4130 if (fnvlist_size(vdev_errlist) > 0)
4131 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4132
4133 fnvlist_free(vdev_errlist);
4134
4135 spa_close(spa, FTAG);
4136 return (total_errors > 0 ? EINVAL : 0);
4137 }
4138
4139 /*
4140 * This ioctl waits for activity of a particular type to complete. If there is
4141 * no activity of that type in progress, it returns immediately, and the
4142 * returned value "waited" is false. If there is activity in progress, and no
4143 * tag is passed in, the ioctl blocks until all activity of that type is
4144 * complete, and then returns with "waited" set to true.
4145 *
4146 * If a tag is provided, it identifies a particular instance of an activity to
4147 * wait for. Currently, this is only valid for use with 'initialize', because
4148 * that is the only activity for which there can be multiple instances running
4149 * concurrently. In the case of 'initialize', the tag corresponds to the guid of
4150 * the vdev on which to wait.
4151 *
4152 * If a thread waiting in the ioctl receives a signal, the call will return
4153 * immediately, and the return value will be EINTR.
4154 *
4155 * innvl: {
4156 * "wait_activity" -> int32_t
4157 * (optional) "wait_tag" -> uint64_t
4158 * }
4159 *
4160 * outnvl: "waited" -> boolean_t
4161 */
4162 static const zfs_ioc_key_t zfs_keys_pool_wait[] = {
4163 {ZPOOL_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4164 {ZPOOL_WAIT_TAG, DATA_TYPE_UINT64, ZK_OPTIONAL},
4165 };
4166
4167 static int
zfs_ioc_wait(const char * name,nvlist_t * innvl,nvlist_t * outnvl)4168 zfs_ioc_wait(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4169 {
4170 int32_t activity;
4171 uint64_t tag;
4172 boolean_t waited;
4173 int error;
4174
4175 if (nvlist_lookup_int32(innvl, ZPOOL_WAIT_ACTIVITY, &activity) != 0)
4176 return (EINVAL);
4177
4178 if (nvlist_lookup_uint64(innvl, ZPOOL_WAIT_TAG, &tag) == 0)
4179 error = spa_wait_tag(name, activity, tag, &waited);
4180 else
4181 error = spa_wait(name, activity, &waited);
4182
4183 if (error == 0)
4184 fnvlist_add_boolean_value(outnvl, ZPOOL_WAIT_WAITED, waited);
4185
4186 return (error);
4187 }
4188
4189 /*
4190 * fsname is name of dataset to rollback (to most recent snapshot)
4191 *
4192 * innvl may contain name of expected target snapshot
4193 *
4194 * outnvl: "target" -> name of most recent snapshot
4195 * }
4196 */
4197 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4198 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4199 };
4200
4201 /* ARGSUSED */
4202 static int
zfs_ioc_rollback(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)4203 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4204 {
4205 zfsvfs_t *zfsvfs;
4206 char *target = NULL;
4207 int error;
4208
4209 (void) nvlist_lookup_string(innvl, "target", &target);
4210 if (target != NULL) {
4211 const char *cp = strchr(target, '@');
4212
4213 /*
4214 * The snap name must contain an @, and the part after it must
4215 * contain only valid characters.
4216 */
4217 if (cp == NULL ||
4218 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4219 return (SET_ERROR(EINVAL));
4220 }
4221
4222 if (getzfsvfs(fsname, &zfsvfs) == 0) {
4223 dsl_dataset_t *ds;
4224
4225 ds = dmu_objset_ds(zfsvfs->z_os);
4226 error = zfs_suspend_fs(zfsvfs);
4227 if (error == 0) {
4228 int resume_err;
4229
4230 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4231 outnvl);
4232 resume_err = zfs_resume_fs(zfsvfs, ds);
4233 error = error ? error : resume_err;
4234 }
4235 VFS_RELE(zfsvfs->z_vfs);
4236 } else {
4237 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4238 }
4239 return (error);
4240 }
4241
4242 static int
recursive_unmount(const char * fsname,void * arg)4243 recursive_unmount(const char *fsname, void *arg)
4244 {
4245 const char *snapname = arg;
4246 char fullname[ZFS_MAX_DATASET_NAME_LEN];
4247
4248 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
4249 zfs_unmount_snap(fullname);
4250
4251 return (0);
4252 }
4253
4254 /*
4255 * inputs:
4256 * zc_name old name of dataset
4257 * zc_value new name of dataset
4258 * zc_cookie recursive flag (only valid for snapshots)
4259 *
4260 * outputs: none
4261 */
4262 static int
zfs_ioc_rename(zfs_cmd_t * zc)4263 zfs_ioc_rename(zfs_cmd_t *zc)
4264 {
4265 objset_t *os;
4266 dmu_objset_type_t ost;
4267 boolean_t recursive = zc->zc_cookie & 1;
4268 char *at;
4269 int err;
4270
4271 /* "zfs rename" from and to ...%recv datasets should both fail */
4272 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4273 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4274 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4275 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4276 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4277 return (SET_ERROR(EINVAL));
4278
4279 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4280 if (err != 0)
4281 return (err);
4282 ost = dmu_objset_type(os);
4283 dmu_objset_rele(os, FTAG);
4284
4285 at = strchr(zc->zc_name, '@');
4286 if (at != NULL) {
4287 /* snaps must be in same fs */
4288 int error;
4289
4290 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4291 return (SET_ERROR(EXDEV));
4292 *at = '\0';
4293 if (ost == DMU_OST_ZFS) {
4294 error = dmu_objset_find(zc->zc_name,
4295 recursive_unmount, at + 1,
4296 recursive ? DS_FIND_CHILDREN : 0);
4297 if (error != 0) {
4298 *at = '@';
4299 return (error);
4300 }
4301 }
4302 error = dsl_dataset_rename_snapshot(zc->zc_name,
4303 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4304 *at = '@';
4305
4306 return (error);
4307 } else {
4308 if (ost == DMU_OST_ZVOL)
4309 (void) zvol_remove_minor(zc->zc_name);
4310 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4311 }
4312 }
4313
4314 static int
zfs_check_settable(const char * dsname,nvpair_t * pair,cred_t * cr)4315 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4316 {
4317 const char *propname = nvpair_name(pair);
4318 boolean_t issnap = (strchr(dsname, '@') != NULL);
4319 zfs_prop_t prop = zfs_name_to_prop(propname);
4320 uint64_t intval;
4321 int err;
4322
4323 if (prop == ZPROP_INVAL) {
4324 if (zfs_prop_user(propname)) {
4325 if (err = zfs_secpolicy_write_perms(dsname,
4326 ZFS_DELEG_PERM_USERPROP, cr))
4327 return (err);
4328 return (0);
4329 }
4330
4331 if (!issnap && zfs_prop_userquota(propname)) {
4332 const char *perm = NULL;
4333 const char *uq_prefix =
4334 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4335 const char *gq_prefix =
4336 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4337 const char *uiq_prefix =
4338 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4339 const char *giq_prefix =
4340 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4341 const char *pq_prefix =
4342 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4343 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4344 ZFS_PROP_PROJECTOBJQUOTA];
4345
4346 if (strncmp(propname, uq_prefix,
4347 strlen(uq_prefix)) == 0) {
4348 perm = ZFS_DELEG_PERM_USERQUOTA;
4349 } else if (strncmp(propname, uiq_prefix,
4350 strlen(uiq_prefix)) == 0) {
4351 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4352 } else if (strncmp(propname, gq_prefix,
4353 strlen(gq_prefix)) == 0) {
4354 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4355 } else if (strncmp(propname, giq_prefix,
4356 strlen(giq_prefix)) == 0) {
4357 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4358 } else if (strncmp(propname, pq_prefix,
4359 strlen(pq_prefix)) == 0) {
4360 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4361 } else if (strncmp(propname, piq_prefix,
4362 strlen(piq_prefix)) == 0) {
4363 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4364 } else {
4365 /* {USER|GROUP|PROJECT}USED are read-only */
4366 return (SET_ERROR(EINVAL));
4367 }
4368
4369 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
4370 return (err);
4371 return (0);
4372 }
4373
4374 return (SET_ERROR(EINVAL));
4375 }
4376
4377 if (issnap)
4378 return (SET_ERROR(EINVAL));
4379
4380 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4381 /*
4382 * dsl_prop_get_all_impl() returns properties in this
4383 * format.
4384 */
4385 nvlist_t *attrs;
4386 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4387 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4388 &pair) == 0);
4389 }
4390
4391 /*
4392 * Check that this value is valid for this pool version
4393 */
4394 switch (prop) {
4395 case ZFS_PROP_COMPRESSION:
4396 /*
4397 * If the user specified gzip compression, make sure
4398 * the SPA supports it. We ignore any errors here since
4399 * we'll catch them later.
4400 */
4401 if (nvpair_value_uint64(pair, &intval) == 0) {
4402 if (intval >= ZIO_COMPRESS_GZIP_1 &&
4403 intval <= ZIO_COMPRESS_GZIP_9 &&
4404 zfs_earlier_version(dsname,
4405 SPA_VERSION_GZIP_COMPRESSION)) {
4406 return (SET_ERROR(ENOTSUP));
4407 }
4408
4409 if (intval == ZIO_COMPRESS_ZLE &&
4410 zfs_earlier_version(dsname,
4411 SPA_VERSION_ZLE_COMPRESSION))
4412 return (SET_ERROR(ENOTSUP));
4413
4414 if (intval == ZIO_COMPRESS_LZ4) {
4415 spa_t *spa;
4416
4417 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4418 return (err);
4419
4420 if (!spa_feature_is_enabled(spa,
4421 SPA_FEATURE_LZ4_COMPRESS)) {
4422 spa_close(spa, FTAG);
4423 return (SET_ERROR(ENOTSUP));
4424 }
4425 spa_close(spa, FTAG);
4426 }
4427
4428 /*
4429 * If this is a bootable dataset then
4430 * verify that the compression algorithm
4431 * is supported for booting. We must return
4432 * something other than ENOTSUP since it
4433 * implies a downrev pool version.
4434 */
4435 if (zfs_is_bootfs(dsname) &&
4436 !BOOTFS_COMPRESS_VALID(intval)) {
4437 return (SET_ERROR(ERANGE));
4438 }
4439 }
4440 break;
4441
4442 case ZFS_PROP_COPIES:
4443 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4444 return (SET_ERROR(ENOTSUP));
4445 break;
4446
4447 case ZFS_PROP_RECORDSIZE:
4448 /* Record sizes above 128k need the feature to be enabled */
4449 if (nvpair_value_uint64(pair, &intval) == 0 &&
4450 intval > SPA_OLD_MAXBLOCKSIZE) {
4451 spa_t *spa;
4452
4453 /*
4454 * We don't allow setting the property above 1MB,
4455 * unless the tunable has been changed.
4456 */
4457 if (intval > zfs_max_recordsize ||
4458 intval > SPA_MAXBLOCKSIZE)
4459 return (SET_ERROR(ERANGE));
4460
4461 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4462 return (err);
4463
4464 if (!spa_feature_is_enabled(spa,
4465 SPA_FEATURE_LARGE_BLOCKS)) {
4466 spa_close(spa, FTAG);
4467 return (SET_ERROR(ENOTSUP));
4468 }
4469 spa_close(spa, FTAG);
4470 }
4471 break;
4472
4473 case ZFS_PROP_DNODESIZE:
4474 /* Dnode sizes above 512 need the feature to be enabled */
4475 if (nvpair_value_uint64(pair, &intval) == 0 &&
4476 intval != ZFS_DNSIZE_LEGACY) {
4477 spa_t *spa;
4478
4479 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4480 return (err);
4481
4482 if (!spa_feature_is_enabled(spa,
4483 SPA_FEATURE_LARGE_DNODE)) {
4484 spa_close(spa, FTAG);
4485 return (SET_ERROR(ENOTSUP));
4486 }
4487 spa_close(spa, FTAG);
4488 }
4489 break;
4490
4491 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4492 /*
4493 * This property could require the allocation classes
4494 * feature to be active for setting, however we allow
4495 * it so that tests of settable properties succeed.
4496 * The CLI will issue a warning in this case.
4497 */
4498 break;
4499
4500 case ZFS_PROP_SHARESMB:
4501 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4502 return (SET_ERROR(ENOTSUP));
4503 break;
4504
4505 case ZFS_PROP_ACLINHERIT:
4506 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4507 nvpair_value_uint64(pair, &intval) == 0) {
4508 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4509 zfs_earlier_version(dsname,
4510 SPA_VERSION_PASSTHROUGH_X))
4511 return (SET_ERROR(ENOTSUP));
4512 }
4513 break;
4514
4515 case ZFS_PROP_CHECKSUM:
4516 case ZFS_PROP_DEDUP:
4517 {
4518 spa_feature_t feature;
4519 spa_t *spa;
4520
4521 /* dedup feature version checks */
4522 if (prop == ZFS_PROP_DEDUP &&
4523 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4524 return (SET_ERROR(ENOTSUP));
4525
4526 if (nvpair_value_uint64(pair, &intval) != 0)
4527 return (SET_ERROR(EINVAL));
4528
4529 /* check prop value is enabled in features */
4530 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
4531 if (feature == SPA_FEATURE_NONE)
4532 break;
4533
4534 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4535 return (err);
4536
4537 if (!spa_feature_is_enabled(spa, feature)) {
4538 spa_close(spa, FTAG);
4539 return (SET_ERROR(ENOTSUP));
4540 }
4541 spa_close(spa, FTAG);
4542 break;
4543 }
4544 }
4545
4546 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4547 }
4548
4549 /*
4550 * Checks for a race condition to make sure we don't increment a feature flag
4551 * multiple times.
4552 */
4553 static int
zfs_prop_activate_feature_check(void * arg,dmu_tx_t * tx)4554 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4555 {
4556 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4557 spa_feature_t *featurep = arg;
4558
4559 if (!spa_feature_is_active(spa, *featurep))
4560 return (0);
4561 else
4562 return (SET_ERROR(EBUSY));
4563 }
4564
4565 /*
4566 * The callback invoked on feature activation in the sync task caused by
4567 * zfs_prop_activate_feature.
4568 */
4569 static void
zfs_prop_activate_feature_sync(void * arg,dmu_tx_t * tx)4570 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4571 {
4572 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4573 spa_feature_t *featurep = arg;
4574
4575 spa_feature_incr(spa, *featurep, tx);
4576 }
4577
4578 /*
4579 * Activates a feature on a pool in response to a property setting. This
4580 * creates a new sync task which modifies the pool to reflect the feature
4581 * as being active.
4582 */
4583 static int
zfs_prop_activate_feature(spa_t * spa,spa_feature_t feature)4584 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4585 {
4586 int err;
4587
4588 /* EBUSY here indicates that the feature is already active */
4589 err = dsl_sync_task(spa_name(spa),
4590 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4591 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4592
4593 if (err != 0 && err != EBUSY)
4594 return (err);
4595 else
4596 return (0);
4597 }
4598
4599 /*
4600 * Removes properties from the given props list that fail permission checks
4601 * needed to clear them and to restore them in case of a receive error. For each
4602 * property, make sure we have both set and inherit permissions.
4603 *
4604 * Returns the first error encountered if any permission checks fail. If the
4605 * caller provides a non-NULL errlist, it also gives the complete list of names
4606 * of all the properties that failed a permission check along with the
4607 * corresponding error numbers. The caller is responsible for freeing the
4608 * returned errlist.
4609 *
4610 * If every property checks out successfully, zero is returned and the list
4611 * pointed at by errlist is NULL.
4612 */
4613 static int
zfs_check_clearable(char * dataset,nvlist_t * props,nvlist_t ** errlist)4614 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4615 {
4616 zfs_cmd_t *zc;
4617 nvpair_t *pair, *next_pair;
4618 nvlist_t *errors;
4619 int err, rv = 0;
4620
4621 if (props == NULL)
4622 return (0);
4623
4624 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4625
4626 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4627 (void) strcpy(zc->zc_name, dataset);
4628 pair = nvlist_next_nvpair(props, NULL);
4629 while (pair != NULL) {
4630 next_pair = nvlist_next_nvpair(props, pair);
4631
4632 (void) strcpy(zc->zc_value, nvpair_name(pair));
4633 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4634 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4635 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4636 VERIFY(nvlist_add_int32(errors,
4637 zc->zc_value, err) == 0);
4638 }
4639 pair = next_pair;
4640 }
4641 kmem_free(zc, sizeof (zfs_cmd_t));
4642
4643 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4644 nvlist_free(errors);
4645 errors = NULL;
4646 } else {
4647 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4648 }
4649
4650 if (errlist == NULL)
4651 nvlist_free(errors);
4652 else
4653 *errlist = errors;
4654
4655 return (rv);
4656 }
4657
4658 static boolean_t
propval_equals(nvpair_t * p1,nvpair_t * p2)4659 propval_equals(nvpair_t *p1, nvpair_t *p2)
4660 {
4661 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4662 /* dsl_prop_get_all_impl() format */
4663 nvlist_t *attrs;
4664 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4665 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4666 &p1) == 0);
4667 }
4668
4669 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4670 nvlist_t *attrs;
4671 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4672 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4673 &p2) == 0);
4674 }
4675
4676 if (nvpair_type(p1) != nvpair_type(p2))
4677 return (B_FALSE);
4678
4679 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4680 char *valstr1, *valstr2;
4681
4682 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4683 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4684 return (strcmp(valstr1, valstr2) == 0);
4685 } else {
4686 uint64_t intval1, intval2;
4687
4688 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4689 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4690 return (intval1 == intval2);
4691 }
4692 }
4693
4694 /*
4695 * Remove properties from props if they are not going to change (as determined
4696 * by comparison with origprops). Remove them from origprops as well, since we
4697 * do not need to clear or restore properties that won't change.
4698 */
4699 static void
props_reduce(nvlist_t * props,nvlist_t * origprops)4700 props_reduce(nvlist_t *props, nvlist_t *origprops)
4701 {
4702 nvpair_t *pair, *next_pair;
4703
4704 if (origprops == NULL)
4705 return; /* all props need to be received */
4706
4707 pair = nvlist_next_nvpair(props, NULL);
4708 while (pair != NULL) {
4709 const char *propname = nvpair_name(pair);
4710 nvpair_t *match;
4711
4712 next_pair = nvlist_next_nvpair(props, pair);
4713
4714 if ((nvlist_lookup_nvpair(origprops, propname,
4715 &match) != 0) || !propval_equals(pair, match))
4716 goto next; /* need to set received value */
4717
4718 /* don't clear the existing received value */
4719 (void) nvlist_remove_nvpair(origprops, match);
4720 /* don't bother receiving the property */
4721 (void) nvlist_remove_nvpair(props, pair);
4722 next:
4723 pair = next_pair;
4724 }
4725 }
4726
4727 /*
4728 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4729 * For example, refquota cannot be set until after the receipt of a dataset,
4730 * because in replication streams, an older/earlier snapshot may exceed the
4731 * refquota. We want to receive the older/earlier snapshot, but setting
4732 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4733 * the older/earlier snapshot from being received (with EDQUOT).
4734 *
4735 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4736 *
4737 * libzfs will need to be judicious handling errors encountered by props
4738 * extracted by this function.
4739 */
4740 static nvlist_t *
extract_delay_props(nvlist_t * props)4741 extract_delay_props(nvlist_t *props)
4742 {
4743 nvlist_t *delayprops;
4744 nvpair_t *nvp, *tmp;
4745 static const zfs_prop_t delayable[] = {
4746 ZFS_PROP_REFQUOTA,
4747 ZFS_PROP_KEYLOCATION,
4748 0
4749 };
4750 int i;
4751
4752 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4753
4754 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4755 nvp = nvlist_next_nvpair(props, nvp)) {
4756 /*
4757 * strcmp() is safe because zfs_prop_to_name() always returns
4758 * a bounded string.
4759 */
4760 for (i = 0; delayable[i] != 0; i++) {
4761 if (strcmp(zfs_prop_to_name(delayable[i]),
4762 nvpair_name(nvp)) == 0) {
4763 break;
4764 }
4765 }
4766 if (delayable[i] != 0) {
4767 tmp = nvlist_prev_nvpair(props, nvp);
4768 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4769 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4770 nvp = tmp;
4771 }
4772 }
4773
4774 if (nvlist_empty(delayprops)) {
4775 nvlist_free(delayprops);
4776 delayprops = NULL;
4777 }
4778 return (delayprops);
4779 }
4780
4781 #ifdef DEBUG
4782 static boolean_t zfs_ioc_recv_inject_err;
4783 #endif
4784
4785 /*
4786 * nvlist 'errors' is always allocated. It will contain descriptions of
4787 * encountered errors, if any. It's the callers responsibility to free.
4788 */
4789 static int
zfs_ioc_recv_impl(char * tofs,char * tosnap,char * origin,nvlist_t * recvprops,nvlist_t * localprops,nvlist_t * hidden_args,boolean_t force,boolean_t resumable,int input_fd,dmu_replay_record_t * begin_record,int cleanup_fd,uint64_t * read_bytes,uint64_t * errflags,uint64_t * action_handle,nvlist_t ** errors)4790 zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
4791 nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
4792 boolean_t resumable, int input_fd, dmu_replay_record_t *begin_record,
4793 int cleanup_fd, uint64_t *read_bytes, uint64_t *errflags,
4794 uint64_t *action_handle, nvlist_t **errors)
4795 {
4796 dmu_recv_cookie_t drc;
4797 int error = 0;
4798 int props_error = 0;
4799 offset_t off;
4800 nvlist_t *local_delayprops = NULL;
4801 nvlist_t *recv_delayprops = NULL;
4802 nvlist_t *origprops = NULL; /* existing properties */
4803 nvlist_t *origrecvd = NULL; /* existing received properties */
4804 boolean_t first_recvd_props = B_FALSE;
4805 file_t *input_fp;
4806
4807 *read_bytes = 0;
4808 *errflags = 0;
4809 *errors = fnvlist_alloc();
4810
4811 input_fp = getf(input_fd);
4812 if (input_fp == NULL)
4813 return (SET_ERROR(EBADF));
4814
4815 error = dmu_recv_begin(tofs, tosnap, begin_record, force,
4816 resumable, localprops, hidden_args, origin, &drc);
4817 if (error != 0)
4818 goto out;
4819
4820 /*
4821 * Set properties before we receive the stream so that they are applied
4822 * to the new data. Note that we must call dmu_recv_stream() if
4823 * dmu_recv_begin() succeeds.
4824 */
4825 if (recvprops != NULL && !drc.drc_newfs) {
4826 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4827 SPA_VERSION_RECVD_PROPS &&
4828 !dsl_prop_get_hasrecvd(tofs))
4829 first_recvd_props = B_TRUE;
4830
4831 /*
4832 * If new received properties are supplied, they are to
4833 * completely replace the existing received properties,
4834 * so stash away the existing ones.
4835 */
4836 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
4837 nvlist_t *errlist = NULL;
4838 /*
4839 * Don't bother writing a property if its value won't
4840 * change (and avoid the unnecessary security checks).
4841 *
4842 * The first receive after SPA_VERSION_RECVD_PROPS is a
4843 * special case where we blow away all local properties
4844 * regardless.
4845 */
4846 if (!first_recvd_props)
4847 props_reduce(recvprops, origrecvd);
4848 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
4849 (void) nvlist_merge(*errors, errlist, 0);
4850 nvlist_free(errlist);
4851
4852 if (clear_received_props(tofs, origrecvd,
4853 first_recvd_props ? NULL : recvprops) != 0)
4854 *errflags |= ZPROP_ERR_NOCLEAR;
4855 } else {
4856 *errflags |= ZPROP_ERR_NOCLEAR;
4857 }
4858 }
4859
4860 /*
4861 * Stash away existing properties so we can restore them on error unless
4862 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4863 * case "origrecvd" will take care of that.
4864 */
4865 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
4866 objset_t *os;
4867 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4868 if (dsl_prop_get_all(os, &origprops) != 0) {
4869 *errflags |= ZPROP_ERR_NOCLEAR;
4870 }
4871 dmu_objset_rele(os, FTAG);
4872 } else {
4873 *errflags |= ZPROP_ERR_NOCLEAR;
4874 }
4875 }
4876
4877 if (recvprops != NULL) {
4878 props_error = dsl_prop_set_hasrecvd(tofs);
4879
4880 if (props_error == 0) {
4881 recv_delayprops = extract_delay_props(recvprops);
4882 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4883 recvprops, *errors);
4884 }
4885 }
4886
4887 if (localprops != NULL) {
4888 nvlist_t *oprops = fnvlist_alloc();
4889 nvlist_t *xprops = fnvlist_alloc();
4890 nvpair_t *nvp = NULL;
4891
4892 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4893 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
4894 /* -x property */
4895 const char *name = nvpair_name(nvp);
4896 zfs_prop_t prop = zfs_name_to_prop(name);
4897 if (prop != ZPROP_INVAL) {
4898 if (!zfs_prop_inheritable(prop))
4899 continue;
4900 } else if (!zfs_prop_user(name))
4901 continue;
4902 fnvlist_add_boolean(xprops, name);
4903 } else {
4904 /* -o property=value */
4905 fnvlist_add_nvpair(oprops, nvp);
4906 }
4907 }
4908
4909 local_delayprops = extract_delay_props(oprops);
4910 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4911 oprops, *errors);
4912 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
4913 xprops, *errors);
4914
4915 nvlist_free(oprops);
4916 nvlist_free(xprops);
4917 }
4918
4919 off = input_fp->f_offset;
4920 error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd,
4921 action_handle);
4922
4923 if (error == 0) {
4924 zfsvfs_t *zfsvfs = NULL;
4925
4926 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4927 /* online recv */
4928 dsl_dataset_t *ds;
4929 int end_err;
4930
4931 ds = dmu_objset_ds(zfsvfs->z_os);
4932 error = zfs_suspend_fs(zfsvfs);
4933 /*
4934 * If the suspend fails, then the recv_end will
4935 * likely also fail, and clean up after itself.
4936 */
4937 end_err = dmu_recv_end(&drc, zfsvfs);
4938 if (error == 0)
4939 error = zfs_resume_fs(zfsvfs, ds);
4940 error = error ? error : end_err;
4941 VFS_RELE(zfsvfs->z_vfs);
4942 } else {
4943 error = dmu_recv_end(&drc, NULL);
4944 }
4945
4946 /* Set delayed properties now, after we're done receiving. */
4947 if (recv_delayprops != NULL && error == 0) {
4948 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4949 recv_delayprops, *errors);
4950 }
4951 if (local_delayprops != NULL && error == 0) {
4952 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4953 local_delayprops, *errors);
4954 }
4955 }
4956
4957 /*
4958 * Merge delayed props back in with initial props, in case
4959 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4960 * we have to make sure clear_received_props() includes
4961 * the delayed properties).
4962 *
4963 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4964 * using ASSERT() will be just like a VERIFY.
4965 */
4966 if (recv_delayprops != NULL) {
4967 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
4968 nvlist_free(recv_delayprops);
4969 }
4970 if (local_delayprops != NULL) {
4971 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
4972 nvlist_free(local_delayprops);
4973 }
4974
4975 *read_bytes = off - input_fp->f_offset;
4976 if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
4977 input_fp->f_offset = off;
4978
4979 #ifdef DEBUG
4980 if (zfs_ioc_recv_inject_err) {
4981 zfs_ioc_recv_inject_err = B_FALSE;
4982 error = 1;
4983 }
4984 #endif
4985
4986 /*
4987 * On error, restore the original props.
4988 */
4989 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
4990 if (clear_received_props(tofs, recvprops, NULL) != 0) {
4991 /*
4992 * We failed to clear the received properties.
4993 * Since we may have left a $recvd value on the
4994 * system, we can't clear the $hasrecvd flag.
4995 */
4996 *errflags |= ZPROP_ERR_NORESTORE;
4997 } else if (first_recvd_props) {
4998 dsl_prop_unset_hasrecvd(tofs);
4999 }
5000
5001 if (origrecvd == NULL && !drc.drc_newfs) {
5002 /* We failed to stash the original properties. */
5003 *errflags |= ZPROP_ERR_NORESTORE;
5004 }
5005
5006 /*
5007 * dsl_props_set() will not convert RECEIVED to LOCAL on or
5008 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
5009 * explicitly if we're restoring local properties cleared in the
5010 * first new-style receive.
5011 */
5012 if (origrecvd != NULL &&
5013 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
5014 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
5015 origrecvd, NULL) != 0) {
5016 /*
5017 * We stashed the original properties but failed to
5018 * restore them.
5019 */
5020 *errflags |= ZPROP_ERR_NORESTORE;
5021 }
5022 }
5023 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
5024 !first_recvd_props) {
5025 nvlist_t *setprops;
5026 nvlist_t *inheritprops;
5027 nvpair_t *nvp;
5028
5029 if (origprops == NULL) {
5030 /* We failed to stash the original properties. */
5031 *errflags |= ZPROP_ERR_NORESTORE;
5032 goto out;
5033 }
5034
5035 /* Restore original props */
5036 setprops = fnvlist_alloc();
5037 inheritprops = fnvlist_alloc();
5038 nvp = NULL;
5039 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5040 const char *name = nvpair_name(nvp);
5041 const char *source;
5042 nvlist_t *attrs;
5043
5044 if (!nvlist_exists(origprops, name)) {
5045 /*
5046 * Property was not present or was explicitly
5047 * inherited before the receive, restore this.
5048 */
5049 fnvlist_add_boolean(inheritprops, name);
5050 continue;
5051 }
5052 attrs = fnvlist_lookup_nvlist(origprops, name);
5053 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
5054
5055 /* Skip received properties */
5056 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
5057 continue;
5058
5059 if (strcmp(source, tofs) == 0) {
5060 /* Property was locally set */
5061 fnvlist_add_nvlist(setprops, name, attrs);
5062 } else {
5063 /* Property was implicitly inherited */
5064 fnvlist_add_boolean(inheritprops, name);
5065 }
5066 }
5067
5068 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
5069 NULL) != 0)
5070 *errflags |= ZPROP_ERR_NORESTORE;
5071 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
5072 NULL) != 0)
5073 *errflags |= ZPROP_ERR_NORESTORE;
5074
5075 nvlist_free(setprops);
5076 nvlist_free(inheritprops);
5077 }
5078 out:
5079 releasef(input_fd);
5080 nvlist_free(origrecvd);
5081 nvlist_free(origprops);
5082
5083 if (error == 0)
5084 error = props_error;
5085
5086 return (error);
5087 }
5088
5089 /*
5090 * inputs:
5091 * zc_name name of containing filesystem
5092 * zc_nvlist_src{_size} nvlist of received properties to apply
5093 * zc_nvlist_conf{_size} nvlist of local properties to apply
5094 * zc_history_offset{_len} nvlist of hidden args { "wkeydata" -> value }
5095 * zc_value name of snapshot to create
5096 * zc_string name of clone origin (if DRR_FLAG_CLONE)
5097 * zc_cookie file descriptor to recv from
5098 * zc_begin_record the BEGIN record of the stream (not byteswapped)
5099 * zc_guid force flag
5100 * zc_cleanup_fd cleanup-on-exit file descriptor
5101 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
5102 * zc_resumable if data is incomplete assume sender will resume
5103 *
5104 * outputs:
5105 * zc_cookie number of bytes read
5106 * zc_nvlist_dst{_size} error for each unapplied received property
5107 * zc_obj zprop_errflags_t
5108 * zc_action_handle handle for this guid/ds mapping
5109 */
5110 static int
zfs_ioc_recv(zfs_cmd_t * zc)5111 zfs_ioc_recv(zfs_cmd_t *zc)
5112 {
5113 dmu_replay_record_t begin_record;
5114 nvlist_t *errors = NULL;
5115 nvlist_t *recvdprops = NULL;
5116 nvlist_t *localprops = NULL;
5117 nvlist_t *hidden_args = NULL;
5118 char *origin = NULL;
5119 char *tosnap;
5120 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5121 int error = 0;
5122
5123 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5124 strchr(zc->zc_value, '@') == NULL ||
5125 strchr(zc->zc_value, '%') != NULL) {
5126 return (SET_ERROR(EINVAL));
5127 }
5128
5129 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
5130 tosnap = strchr(tofs, '@');
5131 *tosnap++ = '\0';
5132
5133 if (zc->zc_nvlist_src != 0 &&
5134 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5135 zc->zc_iflags, &recvdprops)) != 0) {
5136 goto out;
5137 }
5138
5139 if (zc->zc_nvlist_conf != 0 &&
5140 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
5141 zc->zc_iflags, &localprops)) != 0) {
5142 goto out;
5143 }
5144
5145 if (zc->zc_history_offset != 0 &&
5146 (error = get_nvlist(zc->zc_history_offset, zc->zc_history_len,
5147 zc->zc_iflags, &hidden_args)) != 0) {
5148 goto out;
5149 }
5150
5151 if (zc->zc_string[0])
5152 origin = zc->zc_string;
5153
5154 begin_record.drr_type = DRR_BEGIN;
5155 begin_record.drr_payloadlen = zc->zc_begin_record.drr_payloadlen;
5156 begin_record.drr_u.drr_begin = zc->zc_begin_record.drr_u.drr_begin;
5157
5158 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
5159 hidden_args, zc->zc_guid, zc->zc_resumable, zc->zc_cookie,
5160 &begin_record, zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj,
5161 &zc->zc_action_handle, &errors);
5162
5163 /*
5164 * Now that all props, initial and delayed, are set, report the prop
5165 * errors to the caller.
5166 */
5167 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
5168 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5169 put_nvlist(zc, errors) != 0)) {
5170 /*
5171 * Caller made zc->zc_nvlist_dst less than the minimum expected
5172 * size or supplied an invalid address.
5173 */
5174 error = SET_ERROR(EINVAL);
5175 }
5176
5177 out:
5178 nvlist_free(hidden_args);
5179 nvlist_free(recvdprops);
5180 nvlist_free(localprops);
5181 nvlist_free(errors);
5182
5183 return (error);
5184 }
5185
5186 /*
5187 * inputs:
5188 * zc_name name of snapshot to send
5189 * zc_cookie file descriptor to send stream to
5190 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5191 * zc_sendobj objsetid of snapshot to send
5192 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5193 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5194 * output size in zc_objset_type.
5195 * zc_flags lzc_send_flags
5196 *
5197 * outputs:
5198 * zc_objset_type estimated size, if zc_guid is set
5199 */
5200 static int
zfs_ioc_send(zfs_cmd_t * zc)5201 zfs_ioc_send(zfs_cmd_t *zc)
5202 {
5203 int error;
5204 offset_t off;
5205 boolean_t estimate = (zc->zc_guid != 0);
5206 boolean_t embedok = (zc->zc_flags & 0x1);
5207 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5208 boolean_t compressok = (zc->zc_flags & 0x4);
5209 boolean_t rawok = (zc->zc_flags & 0x8);
5210
5211 if (zc->zc_obj != 0) {
5212 dsl_pool_t *dp;
5213 dsl_dataset_t *tosnap;
5214
5215 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5216 if (error != 0)
5217 return (error);
5218
5219 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5220 if (error != 0) {
5221 dsl_pool_rele(dp, FTAG);
5222 return (error);
5223 }
5224
5225 if (dsl_dir_is_clone(tosnap->ds_dir))
5226 zc->zc_fromobj =
5227 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5228 dsl_dataset_rele(tosnap, FTAG);
5229 dsl_pool_rele(dp, FTAG);
5230 }
5231
5232 if (estimate) {
5233 dsl_pool_t *dp;
5234 dsl_dataset_t *tosnap;
5235 dsl_dataset_t *fromsnap = NULL;
5236
5237 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5238 if (error != 0)
5239 return (error);
5240
5241 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5242 FTAG, &tosnap);
5243 if (error != 0) {
5244 dsl_pool_rele(dp, FTAG);
5245 return (error);
5246 }
5247
5248 if (zc->zc_fromobj != 0) {
5249 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5250 FTAG, &fromsnap);
5251 if (error != 0) {
5252 dsl_dataset_rele(tosnap, FTAG);
5253 dsl_pool_rele(dp, FTAG);
5254 return (error);
5255 }
5256 }
5257
5258 error = dmu_send_estimate(tosnap, fromsnap, compressok || rawok,
5259 &zc->zc_objset_type);
5260
5261 if (fromsnap != NULL)
5262 dsl_dataset_rele(fromsnap, FTAG);
5263 dsl_dataset_rele(tosnap, FTAG);
5264 dsl_pool_rele(dp, FTAG);
5265 } else {
5266 file_t *fp = getf(zc->zc_cookie);
5267 if (fp == NULL)
5268 return (SET_ERROR(EBADF));
5269
5270 off = fp->f_offset;
5271 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5272 zc->zc_fromobj, embedok, large_block_ok, compressok, rawok,
5273 zc->zc_cookie, fp->f_vnode, &off);
5274
5275 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5276 fp->f_offset = off;
5277 releasef(zc->zc_cookie);
5278 }
5279 return (error);
5280 }
5281
5282 /*
5283 * inputs:
5284 * zc_name name of snapshot on which to report progress
5285 * zc_cookie file descriptor of send stream
5286 *
5287 * outputs:
5288 * zc_cookie number of bytes written in send stream thus far
5289 */
5290 static int
zfs_ioc_send_progress(zfs_cmd_t * zc)5291 zfs_ioc_send_progress(zfs_cmd_t *zc)
5292 {
5293 dsl_pool_t *dp;
5294 dsl_dataset_t *ds;
5295 dmu_sendarg_t *dsp = NULL;
5296 int error;
5297
5298 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5299 if (error != 0)
5300 return (error);
5301
5302 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5303 if (error != 0) {
5304 dsl_pool_rele(dp, FTAG);
5305 return (error);
5306 }
5307
5308 mutex_enter(&ds->ds_sendstream_lock);
5309
5310 /*
5311 * Iterate over all the send streams currently active on this dataset.
5312 * If there's one which matches the specified file descriptor _and_ the
5313 * stream was started by the current process, return the progress of
5314 * that stream.
5315 */
5316 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5317 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5318 if (dsp->dsa_outfd == zc->zc_cookie &&
5319 dsp->dsa_proc == curproc)
5320 break;
5321 }
5322
5323 if (dsp != NULL)
5324 zc->zc_cookie = *(dsp->dsa_off);
5325 else
5326 error = SET_ERROR(ENOENT);
5327
5328 mutex_exit(&ds->ds_sendstream_lock);
5329 dsl_dataset_rele(ds, FTAG);
5330 dsl_pool_rele(dp, FTAG);
5331 return (error);
5332 }
5333
5334 static int
zfs_ioc_inject_fault(zfs_cmd_t * zc)5335 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5336 {
5337 int id, error;
5338
5339 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5340 &zc->zc_inject_record);
5341
5342 if (error == 0)
5343 zc->zc_guid = (uint64_t)id;
5344
5345 return (error);
5346 }
5347
5348 static int
zfs_ioc_clear_fault(zfs_cmd_t * zc)5349 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5350 {
5351 return (zio_clear_fault((int)zc->zc_guid));
5352 }
5353
5354 static int
zfs_ioc_inject_list_next(zfs_cmd_t * zc)5355 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5356 {
5357 int id = (int)zc->zc_guid;
5358 int error;
5359
5360 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5361 &zc->zc_inject_record);
5362
5363 zc->zc_guid = id;
5364
5365 return (error);
5366 }
5367
5368 static int
zfs_ioc_error_log(zfs_cmd_t * zc)5369 zfs_ioc_error_log(zfs_cmd_t *zc)
5370 {
5371 spa_t *spa;
5372 int error;
5373 size_t count = (size_t)zc->zc_nvlist_dst_size;
5374
5375 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5376 return (error);
5377
5378 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5379 &count);
5380 if (error == 0)
5381 zc->zc_nvlist_dst_size = count;
5382 else
5383 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5384
5385 spa_close(spa, FTAG);
5386
5387 return (error);
5388 }
5389
5390 static int
zfs_ioc_clear(zfs_cmd_t * zc)5391 zfs_ioc_clear(zfs_cmd_t *zc)
5392 {
5393 spa_t *spa;
5394 vdev_t *vd;
5395 int error;
5396
5397 /*
5398 * On zpool clear we also fix up missing slogs
5399 */
5400 mutex_enter(&spa_namespace_lock);
5401 spa = spa_lookup(zc->zc_name);
5402 if (spa == NULL) {
5403 mutex_exit(&spa_namespace_lock);
5404 return (SET_ERROR(EIO));
5405 }
5406 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5407 /* we need to let spa_open/spa_load clear the chains */
5408 spa_set_log_state(spa, SPA_LOG_CLEAR);
5409 }
5410 spa->spa_last_open_failed = 0;
5411 mutex_exit(&spa_namespace_lock);
5412
5413 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5414 error = spa_open(zc->zc_name, &spa, FTAG);
5415 } else {
5416 nvlist_t *policy;
5417 nvlist_t *config = NULL;
5418
5419 if (zc->zc_nvlist_src == 0)
5420 return (SET_ERROR(EINVAL));
5421
5422 if ((error = get_nvlist(zc->zc_nvlist_src,
5423 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5424 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5425 policy, &config);
5426 if (config != NULL) {
5427 int err;
5428
5429 if ((err = put_nvlist(zc, config)) != 0)
5430 error = err;
5431 nvlist_free(config);
5432 }
5433 nvlist_free(policy);
5434 }
5435 }
5436
5437 if (error != 0)
5438 return (error);
5439
5440 /*
5441 * If multihost is enabled, resuming I/O is unsafe as another
5442 * host may have imported the pool.
5443 */
5444 if (spa_multihost(spa) && spa_suspended(spa))
5445 return (SET_ERROR(EINVAL));
5446
5447 spa_vdev_state_enter(spa, SCL_NONE);
5448
5449 if (zc->zc_guid == 0) {
5450 vd = NULL;
5451 } else {
5452 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5453 if (vd == NULL) {
5454 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
5455 spa_close(spa, FTAG);
5456 return (SET_ERROR(ENODEV));
5457 }
5458 }
5459
5460 vdev_clear(spa, vd);
5461
5462 (void) spa_vdev_state_exit(spa, NULL, 0);
5463
5464 /*
5465 * Resume any suspended I/Os.
5466 */
5467 if (zio_resume(spa) != 0)
5468 error = SET_ERROR(EIO);
5469
5470 spa_close(spa, FTAG);
5471
5472 return (error);
5473 }
5474
5475 static int
zfs_ioc_pool_reopen(zfs_cmd_t * zc)5476 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
5477 {
5478 spa_t *spa;
5479 int error;
5480
5481 error = spa_open(zc->zc_name, &spa, FTAG);
5482 if (error != 0)
5483 return (error);
5484
5485 spa_vdev_state_enter(spa, SCL_NONE);
5486
5487 /*
5488 * If a resilver is already in progress then set the
5489 * spa_scrub_reopen flag to B_TRUE so that we don't restart
5490 * the scan as a side effect of the reopen. Otherwise, let
5491 * vdev_open() decided if a resilver is required.
5492 */
5493 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
5494 vdev_reopen(spa->spa_root_vdev);
5495 spa->spa_scrub_reopen = B_FALSE;
5496
5497 (void) spa_vdev_state_exit(spa, NULL, 0);
5498 spa_close(spa, FTAG);
5499 return (0);
5500 }
5501 /*
5502 * inputs:
5503 * zc_name name of filesystem
5504 *
5505 * outputs:
5506 * zc_string name of conflicting snapshot, if there is one
5507 */
5508 static int
zfs_ioc_promote(zfs_cmd_t * zc)5509 zfs_ioc_promote(zfs_cmd_t *zc)
5510 {
5511 dsl_pool_t *dp;
5512 dsl_dataset_t *ds, *ods;
5513 char origin[ZFS_MAX_DATASET_NAME_LEN];
5514 char *cp;
5515 int error;
5516
5517 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5518 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5519 strchr(zc->zc_name, '%'))
5520 return (SET_ERROR(EINVAL));
5521
5522 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5523 if (error != 0)
5524 return (error);
5525
5526 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5527 if (error != 0) {
5528 dsl_pool_rele(dp, FTAG);
5529 return (error);
5530 }
5531
5532 if (!dsl_dir_is_clone(ds->ds_dir)) {
5533 dsl_dataset_rele(ds, FTAG);
5534 dsl_pool_rele(dp, FTAG);
5535 return (SET_ERROR(EINVAL));
5536 }
5537
5538 error = dsl_dataset_hold_obj(dp,
5539 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5540 if (error != 0) {
5541 dsl_dataset_rele(ds, FTAG);
5542 dsl_pool_rele(dp, FTAG);
5543 return (error);
5544 }
5545
5546 dsl_dataset_name(ods, origin);
5547 dsl_dataset_rele(ods, FTAG);
5548 dsl_dataset_rele(ds, FTAG);
5549 dsl_pool_rele(dp, FTAG);
5550
5551 /*
5552 * We don't need to unmount *all* the origin fs's snapshots, but
5553 * it's easier.
5554 */
5555 cp = strchr(origin, '@');
5556 if (cp)
5557 *cp = '\0';
5558 (void) dmu_objset_find(origin,
5559 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5560 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5561 }
5562
5563 /*
5564 * Retrieve a single {user|group|project}{used|quota}@... property.
5565 *
5566 * inputs:
5567 * zc_name name of filesystem
5568 * zc_objset_type zfs_userquota_prop_t
5569 * zc_value domain name (eg. "S-1-234-567-89")
5570 * zc_guid RID/UID/GID
5571 *
5572 * outputs:
5573 * zc_cookie property value
5574 */
5575 static int
zfs_ioc_userspace_one(zfs_cmd_t * zc)5576 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5577 {
5578 zfsvfs_t *zfsvfs;
5579 int error;
5580
5581 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5582 return (SET_ERROR(EINVAL));
5583
5584 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5585 if (error != 0)
5586 return (error);
5587
5588 error = zfs_userspace_one(zfsvfs,
5589 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5590 zfsvfs_rele(zfsvfs, FTAG);
5591
5592 return (error);
5593 }
5594
5595 /*
5596 * inputs:
5597 * zc_name name of filesystem
5598 * zc_cookie zap cursor
5599 * zc_objset_type zfs_userquota_prop_t
5600 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5601 *
5602 * outputs:
5603 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5604 * zc_cookie zap cursor
5605 */
5606 static int
zfs_ioc_userspace_many(zfs_cmd_t * zc)5607 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5608 {
5609 zfsvfs_t *zfsvfs;
5610 int bufsize = zc->zc_nvlist_dst_size;
5611
5612 if (bufsize <= 0)
5613 return (SET_ERROR(ENOMEM));
5614
5615 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5616 if (error != 0)
5617 return (error);
5618
5619 void *buf = kmem_alloc(bufsize, KM_SLEEP);
5620
5621 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5622 buf, &zc->zc_nvlist_dst_size);
5623
5624 if (error == 0) {
5625 error = xcopyout(buf,
5626 (void *)(uintptr_t)zc->zc_nvlist_dst,
5627 zc->zc_nvlist_dst_size);
5628 }
5629 kmem_free(buf, bufsize);
5630 zfsvfs_rele(zfsvfs, FTAG);
5631
5632 return (error);
5633 }
5634
5635 /*
5636 * inputs:
5637 * zc_name name of filesystem
5638 *
5639 * outputs:
5640 * none
5641 */
5642 static int
zfs_ioc_userspace_upgrade(zfs_cmd_t * zc)5643 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5644 {
5645 objset_t *os;
5646 int error = 0;
5647 zfsvfs_t *zfsvfs;
5648
5649 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5650 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5651 /*
5652 * If userused is not enabled, it may be because the
5653 * objset needs to be closed & reopened (to grow the
5654 * objset_phys_t). Suspend/resume the fs will do that.
5655 */
5656 dsl_dataset_t *ds, *newds;
5657
5658 ds = dmu_objset_ds(zfsvfs->z_os);
5659 error = zfs_suspend_fs(zfsvfs);
5660 if (error == 0) {
5661 dmu_objset_refresh_ownership(ds, &newds,
5662 B_TRUE, zfsvfs);
5663 error = zfs_resume_fs(zfsvfs, newds);
5664 }
5665 }
5666 if (error == 0)
5667 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5668 VFS_RELE(zfsvfs->z_vfs);
5669 } else {
5670 /* XXX kind of reading contents without owning */
5671 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5672 if (error != 0)
5673 return (error);
5674
5675 error = dmu_objset_userspace_upgrade(os);
5676 dmu_objset_rele_flags(os, B_TRUE, FTAG);
5677 }
5678
5679 return (error);
5680 }
5681
5682 /*
5683 * inputs:
5684 * zc_name name of filesystem
5685 *
5686 * outputs:
5687 * none
5688 */
5689 static int
zfs_ioc_id_quota_upgrade(zfs_cmd_t * zc)5690 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
5691 {
5692 objset_t *os;
5693 int error;
5694
5695 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5696 if (error != 0)
5697 return (error);
5698
5699 if (dmu_objset_userobjspace_upgradable(os) ||
5700 dmu_objset_projectquota_upgradable(os)) {
5701 mutex_enter(&os->os_upgrade_lock);
5702 if (os->os_upgrade_id == 0) {
5703 /* clear potential error code and retry */
5704 os->os_upgrade_status = 0;
5705 mutex_exit(&os->os_upgrade_lock);
5706
5707 dmu_objset_id_quota_upgrade(os);
5708 } else {
5709 mutex_exit(&os->os_upgrade_lock);
5710 }
5711
5712 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5713
5714 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5715 error = os->os_upgrade_status;
5716 } else {
5717 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5718 }
5719
5720 dsl_dataset_rele(dmu_objset_ds(os), FTAG);
5721
5722 return (error);
5723 }
5724
5725 /*
5726 * We don't want to have a hard dependency
5727 * against some special symbols in sharefs
5728 * nfs, and smbsrv. Determine them if needed when
5729 * the first file system is shared.
5730 * Neither sharefs, nfs or smbsrv are unloadable modules.
5731 */
5732 int (*znfsexport_fs)(void *arg);
5733 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
5734 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
5735
5736 int zfs_nfsshare_inited;
5737 int zfs_smbshare_inited;
5738
5739 ddi_modhandle_t nfs_mod;
5740 ddi_modhandle_t sharefs_mod;
5741 ddi_modhandle_t smbsrv_mod;
5742 kmutex_t zfs_share_lock;
5743
5744 static int
zfs_init_sharefs()5745 zfs_init_sharefs()
5746 {
5747 int error;
5748
5749 ASSERT(MUTEX_HELD(&zfs_share_lock));
5750 /* Both NFS and SMB shares also require sharetab support. */
5751 if (sharefs_mod == NULL && ((sharefs_mod =
5752 ddi_modopen("fs/sharefs",
5753 KRTLD_MODE_FIRST, &error)) == NULL)) {
5754 return (SET_ERROR(ENOSYS));
5755 }
5756 if (zshare_fs == NULL && ((zshare_fs =
5757 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
5758 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
5759 return (SET_ERROR(ENOSYS));
5760 }
5761 return (0);
5762 }
5763
5764 static int
zfs_ioc_share(zfs_cmd_t * zc)5765 zfs_ioc_share(zfs_cmd_t *zc)
5766 {
5767 int error;
5768 int opcode;
5769
5770 switch (zc->zc_share.z_sharetype) {
5771 case ZFS_SHARE_NFS:
5772 case ZFS_UNSHARE_NFS:
5773 if (zfs_nfsshare_inited == 0) {
5774 mutex_enter(&zfs_share_lock);
5775 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5776 KRTLD_MODE_FIRST, &error)) == NULL)) {
5777 mutex_exit(&zfs_share_lock);
5778 return (SET_ERROR(ENOSYS));
5779 }
5780 if (znfsexport_fs == NULL &&
5781 ((znfsexport_fs = (int (*)(void *))
5782 ddi_modsym(nfs_mod,
5783 "nfs_export", &error)) == NULL)) {
5784 mutex_exit(&zfs_share_lock);
5785 return (SET_ERROR(ENOSYS));
5786 }
5787 error = zfs_init_sharefs();
5788 if (error != 0) {
5789 mutex_exit(&zfs_share_lock);
5790 return (SET_ERROR(ENOSYS));
5791 }
5792 zfs_nfsshare_inited = 1;
5793 mutex_exit(&zfs_share_lock);
5794 }
5795 break;
5796 case ZFS_SHARE_SMB:
5797 case ZFS_UNSHARE_SMB:
5798 if (zfs_smbshare_inited == 0) {
5799 mutex_enter(&zfs_share_lock);
5800 if (smbsrv_mod == NULL && ((smbsrv_mod =
5801 ddi_modopen("drv/smbsrv",
5802 KRTLD_MODE_FIRST, &error)) == NULL)) {
5803 mutex_exit(&zfs_share_lock);
5804 return (SET_ERROR(ENOSYS));
5805 }
5806 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5807 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5808 "smb_server_share", &error)) == NULL)) {
5809 mutex_exit(&zfs_share_lock);
5810 return (SET_ERROR(ENOSYS));
5811 }
5812 error = zfs_init_sharefs();
5813 if (error != 0) {
5814 mutex_exit(&zfs_share_lock);
5815 return (SET_ERROR(ENOSYS));
5816 }
5817 zfs_smbshare_inited = 1;
5818 mutex_exit(&zfs_share_lock);
5819 }
5820 break;
5821 default:
5822 return (SET_ERROR(EINVAL));
5823 }
5824
5825 switch (zc->zc_share.z_sharetype) {
5826 case ZFS_SHARE_NFS:
5827 case ZFS_UNSHARE_NFS:
5828 if (error =
5829 znfsexport_fs((void *)
5830 (uintptr_t)zc->zc_share.z_exportdata))
5831 return (error);
5832 break;
5833 case ZFS_SHARE_SMB:
5834 case ZFS_UNSHARE_SMB:
5835 if (error = zsmbexport_fs((void *)
5836 (uintptr_t)zc->zc_share.z_exportdata,
5837 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5838 B_TRUE: B_FALSE)) {
5839 return (error);
5840 }
5841 break;
5842 }
5843
5844 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5845 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5846 SHAREFS_ADD : SHAREFS_REMOVE;
5847
5848 /*
5849 * Add or remove share from sharetab
5850 */
5851 error = zshare_fs(opcode,
5852 (void *)(uintptr_t)zc->zc_share.z_sharedata,
5853 zc->zc_share.z_sharemax);
5854
5855 return (error);
5856
5857 }
5858
5859 ace_t full_access[] = {
5860 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5861 };
5862
5863 /*
5864 * inputs:
5865 * zc_name name of containing filesystem
5866 * zc_obj object # beyond which we want next in-use object #
5867 *
5868 * outputs:
5869 * zc_obj next in-use object #
5870 */
5871 static int
zfs_ioc_next_obj(zfs_cmd_t * zc)5872 zfs_ioc_next_obj(zfs_cmd_t *zc)
5873 {
5874 objset_t *os = NULL;
5875 int error;
5876
5877 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5878 if (error != 0)
5879 return (error);
5880
5881 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5882 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5883
5884 dmu_objset_rele(os, FTAG);
5885 return (error);
5886 }
5887
5888 /*
5889 * inputs:
5890 * zc_name name of filesystem
5891 * zc_value prefix name for snapshot
5892 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5893 *
5894 * outputs:
5895 * zc_value short name of new snapshot
5896 */
5897 static int
zfs_ioc_tmp_snapshot(zfs_cmd_t * zc)5898 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5899 {
5900 char *snap_name;
5901 char *hold_name;
5902 int error;
5903 minor_t minor;
5904
5905 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5906 if (error != 0)
5907 return (error);
5908
5909 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5910 (u_longlong_t)ddi_get_lbolt64());
5911 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5912
5913 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5914 hold_name);
5915 if (error == 0)
5916 (void) strcpy(zc->zc_value, snap_name);
5917 strfree(snap_name);
5918 strfree(hold_name);
5919 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5920 return (error);
5921 }
5922
5923 /*
5924 * inputs:
5925 * zc_name name of "to" snapshot
5926 * zc_value name of "from" snapshot
5927 * zc_cookie file descriptor to write diff data on
5928 *
5929 * outputs:
5930 * dmu_diff_record_t's to the file descriptor
5931 */
5932 static int
zfs_ioc_diff(zfs_cmd_t * zc)5933 zfs_ioc_diff(zfs_cmd_t *zc)
5934 {
5935 file_t *fp;
5936 offset_t off;
5937 int error;
5938
5939 fp = getf(zc->zc_cookie);
5940 if (fp == NULL)
5941 return (SET_ERROR(EBADF));
5942
5943 off = fp->f_offset;
5944
5945 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5946
5947 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5948 fp->f_offset = off;
5949 releasef(zc->zc_cookie);
5950
5951 return (error);
5952 }
5953
5954 /*
5955 * Remove all ACL files in shares dir
5956 */
5957 static int
zfs_smb_acl_purge(znode_t * dzp)5958 zfs_smb_acl_purge(znode_t *dzp)
5959 {
5960 zap_cursor_t zc;
5961 zap_attribute_t zap;
5962 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5963 int error;
5964
5965 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5966 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5967 zap_cursor_advance(&zc)) {
5968 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5969 NULL, 0)) != 0)
5970 break;
5971 }
5972 zap_cursor_fini(&zc);
5973 return (error);
5974 }
5975
5976 static int
zfs_ioc_smb_acl(zfs_cmd_t * zc)5977 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5978 {
5979 vnode_t *vp;
5980 znode_t *dzp;
5981 vnode_t *resourcevp = NULL;
5982 znode_t *sharedir;
5983 zfsvfs_t *zfsvfs;
5984 nvlist_t *nvlist;
5985 char *src, *target;
5986 vattr_t vattr;
5987 vsecattr_t vsec;
5988 int error = 0;
5989
5990 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5991 NO_FOLLOW, NULL, &vp)) != 0)
5992 return (error);
5993
5994 /* Now make sure mntpnt and dataset are ZFS */
5995
5996 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5997 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5998 zc->zc_name) != 0)) {
5999 VN_RELE(vp);
6000 return (SET_ERROR(EINVAL));
6001 }
6002
6003 dzp = VTOZ(vp);
6004 zfsvfs = dzp->z_zfsvfs;
6005 ZFS_ENTER(zfsvfs);
6006
6007 /*
6008 * Create share dir if its missing.
6009 */
6010 mutex_enter(&zfsvfs->z_lock);
6011 if (zfsvfs->z_shares_dir == 0) {
6012 dmu_tx_t *tx;
6013
6014 tx = dmu_tx_create(zfsvfs->z_os);
6015 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
6016 ZFS_SHARES_DIR);
6017 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
6018 error = dmu_tx_assign(tx, TXG_WAIT);
6019 if (error != 0) {
6020 dmu_tx_abort(tx);
6021 } else {
6022 error = zfs_create_share_dir(zfsvfs, tx);
6023 dmu_tx_commit(tx);
6024 }
6025 if (error != 0) {
6026 mutex_exit(&zfsvfs->z_lock);
6027 VN_RELE(vp);
6028 ZFS_EXIT(zfsvfs);
6029 return (error);
6030 }
6031 }
6032 mutex_exit(&zfsvfs->z_lock);
6033
6034 ASSERT(zfsvfs->z_shares_dir);
6035 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
6036 VN_RELE(vp);
6037 ZFS_EXIT(zfsvfs);
6038 return (error);
6039 }
6040
6041 switch (zc->zc_cookie) {
6042 case ZFS_SMB_ACL_ADD:
6043 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
6044 vattr.va_type = VREG;
6045 vattr.va_mode = S_IFREG|0777;
6046 vattr.va_uid = 0;
6047 vattr.va_gid = 0;
6048
6049 vsec.vsa_mask = VSA_ACE;
6050 vsec.vsa_aclentp = &full_access;
6051 vsec.vsa_aclentsz = sizeof (full_access);
6052 vsec.vsa_aclcnt = 1;
6053
6054 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
6055 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
6056 if (resourcevp)
6057 VN_RELE(resourcevp);
6058 break;
6059
6060 case ZFS_SMB_ACL_REMOVE:
6061 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
6062 NULL, 0);
6063 break;
6064
6065 case ZFS_SMB_ACL_RENAME:
6066 if ((error = get_nvlist(zc->zc_nvlist_src,
6067 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
6068 VN_RELE(vp);
6069 VN_RELE(ZTOV(sharedir));
6070 ZFS_EXIT(zfsvfs);
6071 return (error);
6072 }
6073 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
6074 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
6075 &target)) {
6076 VN_RELE(vp);
6077 VN_RELE(ZTOV(sharedir));
6078 ZFS_EXIT(zfsvfs);
6079 nvlist_free(nvlist);
6080 return (error);
6081 }
6082 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
6083 kcred, NULL, 0);
6084 nvlist_free(nvlist);
6085 break;
6086
6087 case ZFS_SMB_ACL_PURGE:
6088 error = zfs_smb_acl_purge(sharedir);
6089 break;
6090
6091 default:
6092 error = SET_ERROR(EINVAL);
6093 break;
6094 }
6095
6096 VN_RELE(vp);
6097 VN_RELE(ZTOV(sharedir));
6098
6099 ZFS_EXIT(zfsvfs);
6100
6101 return (error);
6102 }
6103
6104 /*
6105 * innvl: {
6106 * "holds" -> { snapname -> holdname (string), ... }
6107 * (optional) "cleanup_fd" -> fd (int32)
6108 * }
6109 *
6110 * outnvl: {
6111 * snapname -> error value (int32)
6112 * ...
6113 * }
6114 */
6115 static const zfs_ioc_key_t zfs_keys_hold[] = {
6116 {"holds", DATA_TYPE_NVLIST, 0},
6117 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6118 };
6119
6120 /* ARGSUSED */
6121 static int
zfs_ioc_hold(const char * pool,nvlist_t * args,nvlist_t * errlist)6122 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
6123 {
6124 nvpair_t *pair;
6125 nvlist_t *holds;
6126 int cleanup_fd = -1;
6127 int error;
6128 minor_t minor = 0;
6129
6130 holds = fnvlist_lookup_nvlist(args, "holds");
6131
6132 /* make sure the user didn't pass us any invalid (empty) tags */
6133 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
6134 pair = nvlist_next_nvpair(holds, pair)) {
6135 char *htag;
6136
6137 error = nvpair_value_string(pair, &htag);
6138 if (error != 0)
6139 return (SET_ERROR(error));
6140
6141 if (strlen(htag) == 0)
6142 return (SET_ERROR(EINVAL));
6143 }
6144
6145 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
6146 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
6147 if (error != 0)
6148 return (error);
6149 }
6150
6151 error = dsl_dataset_user_hold(holds, minor, errlist);
6152 if (minor != 0)
6153 zfs_onexit_fd_rele(cleanup_fd);
6154 return (error);
6155 }
6156
6157 /*
6158 * innvl is not used.
6159 *
6160 * outnvl: {
6161 * holdname -> time added (uint64 seconds since epoch)
6162 * ...
6163 * }
6164 */
6165 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
6166 /* no nvl keys */
6167 };
6168
6169 /* ARGSUSED */
6170 static int
zfs_ioc_get_holds(const char * snapname,nvlist_t * args,nvlist_t * outnvl)6171 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
6172 {
6173 return (dsl_dataset_get_holds(snapname, outnvl));
6174 }
6175
6176 /*
6177 * innvl: {
6178 * snapname -> { holdname, ... }
6179 * ...
6180 * }
6181 *
6182 * outnvl: {
6183 * snapname -> error value (int32)
6184 * ...
6185 * }
6186 */
6187 static const zfs_ioc_key_t zfs_keys_release[] = {
6188 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
6189 };
6190
6191 /* ARGSUSED */
6192 static int
zfs_ioc_release(const char * pool,nvlist_t * holds,nvlist_t * errlist)6193 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
6194 {
6195 return (dsl_dataset_user_release(holds, errlist));
6196 }
6197
6198 /*
6199 * inputs:
6200 * zc_name name of new filesystem or snapshot
6201 * zc_value full name of old snapshot
6202 *
6203 * outputs:
6204 * zc_cookie space in bytes
6205 * zc_objset_type compressed space in bytes
6206 * zc_perm_action uncompressed space in bytes
6207 */
6208 static int
zfs_ioc_space_written(zfs_cmd_t * zc)6209 zfs_ioc_space_written(zfs_cmd_t *zc)
6210 {
6211 int error;
6212 dsl_pool_t *dp;
6213 dsl_dataset_t *new, *old;
6214
6215 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6216 if (error != 0)
6217 return (error);
6218 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6219 if (error != 0) {
6220 dsl_pool_rele(dp, FTAG);
6221 return (error);
6222 }
6223 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6224 if (error != 0) {
6225 dsl_dataset_rele(new, FTAG);
6226 dsl_pool_rele(dp, FTAG);
6227 return (error);
6228 }
6229
6230 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
6231 &zc->zc_objset_type, &zc->zc_perm_action);
6232 dsl_dataset_rele(old, FTAG);
6233 dsl_dataset_rele(new, FTAG);
6234 dsl_pool_rele(dp, FTAG);
6235 return (error);
6236 }
6237
6238 /*
6239 * innvl: {
6240 * "firstsnap" -> snapshot name
6241 * }
6242 *
6243 * outnvl: {
6244 * "used" -> space in bytes
6245 * "compressed" -> compressed space in bytes
6246 * "uncompressed" -> uncompressed space in bytes
6247 * }
6248 */
6249 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6250 {"firstsnap", DATA_TYPE_STRING, 0},
6251 };
6252
6253 static int
zfs_ioc_space_snaps(const char * lastsnap,nvlist_t * innvl,nvlist_t * outnvl)6254 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6255 {
6256 int error;
6257 dsl_pool_t *dp;
6258 dsl_dataset_t *new, *old;
6259 char *firstsnap;
6260 uint64_t used, comp, uncomp;
6261
6262 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6263
6264 error = dsl_pool_hold(lastsnap, FTAG, &dp);
6265 if (error != 0)
6266 return (error);
6267
6268 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6269 if (error == 0 && !new->ds_is_snapshot) {
6270 dsl_dataset_rele(new, FTAG);
6271 error = SET_ERROR(EINVAL);
6272 }
6273 if (error != 0) {
6274 dsl_pool_rele(dp, FTAG);
6275 return (error);
6276 }
6277 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6278 if (error == 0 && !old->ds_is_snapshot) {
6279 dsl_dataset_rele(old, FTAG);
6280 error = SET_ERROR(EINVAL);
6281 }
6282 if (error != 0) {
6283 dsl_dataset_rele(new, FTAG);
6284 dsl_pool_rele(dp, FTAG);
6285 return (error);
6286 }
6287
6288 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6289 dsl_dataset_rele(old, FTAG);
6290 dsl_dataset_rele(new, FTAG);
6291 dsl_pool_rele(dp, FTAG);
6292 fnvlist_add_uint64(outnvl, "used", used);
6293 fnvlist_add_uint64(outnvl, "compressed", comp);
6294 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6295 return (error);
6296 }
6297
6298 /*
6299 * innvl: {
6300 * "fd" -> file descriptor to write stream to (int32)
6301 * (optional) "fromsnap" -> full snap name to send an incremental from
6302 * (optional) "largeblockok" -> (value ignored)
6303 * indicates that blocks > 128KB are permitted
6304 * (optional) "embedok" -> (value ignored)
6305 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6306 * (optional) "compressok" -> (value ignored)
6307 * presence indicates compressed DRR_WRITE records are permitted
6308 * (optional) "rawok" -> (value ignored)
6309 * presence indicates raw encrypted records should be used.
6310 * (optional) "resume_object" and "resume_offset" -> (uint64)
6311 * if present, resume send stream from specified object and offset.
6312 * }
6313 *
6314 * outnvl is unused
6315 */
6316 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6317 {"fd", DATA_TYPE_INT32, 0},
6318 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6319 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6320 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6321 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6322 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6323 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6324 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6325 };
6326
6327 /* ARGSUSED */
6328 static int
zfs_ioc_send_new(const char * snapname,nvlist_t * innvl,nvlist_t * outnvl)6329 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6330 {
6331 int error;
6332 offset_t off;
6333 char *fromname = NULL;
6334 int fd;
6335 boolean_t largeblockok;
6336 boolean_t embedok;
6337 boolean_t compressok;
6338 boolean_t rawok;
6339 uint64_t resumeobj = 0;
6340 uint64_t resumeoff = 0;
6341
6342 fd = fnvlist_lookup_int32(innvl, "fd");
6343
6344 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6345
6346 largeblockok = nvlist_exists(innvl, "largeblockok");
6347 embedok = nvlist_exists(innvl, "embedok");
6348 compressok = nvlist_exists(innvl, "compressok");
6349 rawok = nvlist_exists(innvl, "rawok");
6350
6351 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6352 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6353
6354 file_t *fp = getf(fd);
6355 if (fp == NULL)
6356 return (SET_ERROR(EBADF));
6357
6358 off = fp->f_offset;
6359 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
6360 rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off);
6361
6362 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
6363 fp->f_offset = off;
6364 releasef(fd);
6365 return (error);
6366 }
6367
6368 /*
6369 * Determine approximately how large a zfs send stream will be -- the number
6370 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6371 *
6372 * innvl: {
6373 * (optional) "from" -> full snap or bookmark name to send an incremental
6374 * from
6375 * (optional) "largeblockok" -> (value ignored)
6376 * indicates that blocks > 128KB are permitted
6377 * (optional) "embedok" -> (value ignored)
6378 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6379 * (optional) "compressok" -> (value ignored)
6380 * presence indicates compressed DRR_WRITE records are permitted
6381 * }
6382 *
6383 * outnvl: {
6384 * "space" -> bytes of space (uint64)
6385 * }
6386 */
6387 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6388 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6389 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6390 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6391 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6392 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6393 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6394 };
6395
6396 static int
zfs_ioc_send_space(const char * snapname,nvlist_t * innvl,nvlist_t * outnvl)6397 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6398 {
6399 dsl_pool_t *dp;
6400 dsl_dataset_t *tosnap;
6401 int error;
6402 char *fromname;
6403 boolean_t compressok;
6404 boolean_t rawok;
6405 uint64_t space;
6406
6407 error = dsl_pool_hold(snapname, FTAG, &dp);
6408 if (error != 0)
6409 return (error);
6410
6411 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6412 if (error != 0) {
6413 dsl_pool_rele(dp, FTAG);
6414 return (error);
6415 }
6416
6417 compressok = nvlist_exists(innvl, "compressok");
6418 rawok = nvlist_exists(innvl, "rawok");
6419
6420 error = nvlist_lookup_string(innvl, "from", &fromname);
6421 if (error == 0) {
6422 if (strchr(fromname, '@') != NULL) {
6423 /*
6424 * If from is a snapshot, hold it and use the more
6425 * efficient dmu_send_estimate to estimate send space
6426 * size using deadlists.
6427 */
6428 dsl_dataset_t *fromsnap;
6429 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6430 if (error != 0)
6431 goto out;
6432 error = dmu_send_estimate(tosnap, fromsnap,
6433 compressok || rawok, &space);
6434 dsl_dataset_rele(fromsnap, FTAG);
6435 } else if (strchr(fromname, '#') != NULL) {
6436 /*
6437 * If from is a bookmark, fetch the creation TXG of the
6438 * snapshot it was created from and use that to find
6439 * blocks that were born after it.
6440 */
6441 zfs_bookmark_phys_t frombm;
6442
6443 error = dsl_bookmark_lookup(dp, fromname, tosnap,
6444 &frombm);
6445 if (error != 0)
6446 goto out;
6447 error = dmu_send_estimate_from_txg(tosnap,
6448 frombm.zbm_creation_txg, compressok || rawok,
6449 &space);
6450 } else {
6451 /*
6452 * from is not properly formatted as a snapshot or
6453 * bookmark
6454 */
6455 error = SET_ERROR(EINVAL);
6456 goto out;
6457 }
6458 } else {
6459 /*
6460 * If estimating the size of a full send, use dmu_send_estimate.
6461 */
6462 error = dmu_send_estimate(tosnap, NULL, compressok || rawok,
6463 &space);
6464 }
6465
6466 fnvlist_add_uint64(outnvl, "space", space);
6467
6468 out:
6469 dsl_dataset_rele(tosnap, FTAG);
6470 dsl_pool_rele(dp, FTAG);
6471 return (error);
6472 }
6473
6474 /*
6475 * Sync the currently open TXG to disk for the specified pool.
6476 * This is somewhat similar to 'zfs_sync()'.
6477 * For cases that do not result in error this ioctl will wait for
6478 * the currently open TXG to commit before returning back to the caller.
6479 *
6480 * innvl: {
6481 * "force" -> when true, force uberblock update even if there is no dirty data.
6482 * In addition this will cause the vdev configuration to be written
6483 * out including updating the zpool cache file. (boolean_t)
6484 * }
6485 *
6486 * onvl is unused
6487 */
6488 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6489 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6490 };
6491
6492 /* ARGSUSED */
6493 static int
zfs_ioc_pool_sync(const char * pool,nvlist_t * innvl,nvlist_t * onvl)6494 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6495 {
6496 int err;
6497 boolean_t force = B_FALSE;
6498 spa_t *spa;
6499
6500 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6501 return (err);
6502
6503 if (innvl)
6504 force = fnvlist_lookup_boolean_value(innvl, "force");
6505
6506 if (force) {
6507 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6508 vdev_config_dirty(spa->spa_root_vdev);
6509 spa_config_exit(spa, SCL_CONFIG, FTAG);
6510 }
6511 txg_wait_synced(spa_get_dsl(spa), 0);
6512
6513 spa_close(spa, FTAG);
6514
6515 return (err);
6516 }
6517
6518 /*
6519 * Load a user's wrapping key into the kernel.
6520 * innvl: {
6521 * "hidden_args" -> { "wkeydata" -> value }
6522 * raw uint8_t array of encryption wrapping key data (32 bytes)
6523 * (optional) "noop" -> (value ignored)
6524 * presence indicated key should only be verified, not loaded
6525 * }
6526 */
6527 static const zfs_ioc_key_t zfs_keys_load_key[] = {
6528 {"hidden_args", DATA_TYPE_NVLIST, 0},
6529 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6530 };
6531
6532 /* ARGSUSED */
6533 static int
zfs_ioc_load_key(const char * dsname,nvlist_t * innvl,nvlist_t * outnvl)6534 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6535 {
6536 int ret = 0;
6537 dsl_crypto_params_t *dcp = NULL;
6538 nvlist_t *hidden_args;
6539 boolean_t noop = nvlist_exists(innvl, "noop");
6540
6541 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6542 ret = SET_ERROR(EINVAL);
6543 goto error;
6544 }
6545
6546 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
6547
6548 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6549 hidden_args, &dcp);
6550 if (ret != 0)
6551 goto error;
6552
6553 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6554 if (ret != 0)
6555 goto error;
6556
6557 dsl_crypto_params_free(dcp, noop);
6558
6559 return (0);
6560
6561 error:
6562 dsl_crypto_params_free(dcp, B_TRUE);
6563 return (ret);
6564 }
6565
6566 /*
6567 * Unload a user's wrapping key from the kernel.
6568 * Both innvl and outnvl are unused.
6569 */
6570 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6571 /* no nvl keys */
6572 };
6573
6574 /* ARGSUSED */
6575 static int
zfs_ioc_unload_key(const char * dsname,nvlist_t * innvl,nvlist_t * outnvl)6576 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6577 {
6578 int ret = 0;
6579
6580 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6581 ret = (SET_ERROR(EINVAL));
6582 goto out;
6583 }
6584
6585 ret = spa_keystore_unload_wkey(dsname);
6586 if (ret != 0)
6587 goto out;
6588
6589 out:
6590 return (ret);
6591 }
6592
6593 /*
6594 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6595 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6596 * here to change how the key is derived in userspace.
6597 *
6598 * innvl: {
6599 * "hidden_args" (optional) -> { "wkeydata" -> value }
6600 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6601 * "props" (optional) -> { prop -> value }
6602 * }
6603 *
6604 * outnvl is unused
6605 */
6606 static const zfs_ioc_key_t zfs_keys_change_key[] = {
6607 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6608 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6609 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6610 };
6611
6612 /* ARGSUSED */
6613 static int
zfs_ioc_change_key(const char * dsname,nvlist_t * innvl,nvlist_t * outnvl)6614 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6615 {
6616 int ret;
6617 uint64_t cmd = DCP_CMD_NONE;
6618 dsl_crypto_params_t *dcp = NULL;
6619 nvlist_t *props = NULL, *hidden_args = NULL;
6620
6621 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6622 ret = (SET_ERROR(EINVAL));
6623 goto error;
6624 }
6625
6626 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6627 (void) nvlist_lookup_nvlist(innvl, "props", &props);
6628 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6629
6630 ret = dsl_crypto_params_create_nvlist(cmd, props, hidden_args, &dcp);
6631 if (ret != 0)
6632 goto error;
6633
6634 /* The keylocation property is set from dcp->cp_keylocation. */
6635 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
6636
6637 if ((ret = zfs_check_userprops(dsname, props)) != 0)
6638 goto error;
6639
6640 ret = spa_keystore_change_key(dsname, dcp, props);
6641 if (ret != 0)
6642 goto error;
6643
6644 dsl_crypto_params_free(dcp, B_FALSE);
6645
6646 return (0);
6647
6648 error:
6649 dsl_crypto_params_free(dcp, B_TRUE);
6650 return (ret);
6651 }
6652
6653 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6654
6655 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)6656 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6657 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6658 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6659 {
6660 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6661
6662 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6663 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6664 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6665 ASSERT3P(vec->zvec_func, ==, NULL);
6666
6667 vec->zvec_legacy_func = func;
6668 vec->zvec_secpolicy = secpolicy;
6669 vec->zvec_namecheck = namecheck;
6670 vec->zvec_allow_log = log_history;
6671 vec->zvec_pool_check = pool_check;
6672 }
6673
6674 /*
6675 * See the block comment at the beginning of this file for details on
6676 * each argument to this function.
6677 */
6678 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,const zfs_ioc_key_t * nvl_keys,size_t num_keys)6679 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6680 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6681 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6682 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6683 {
6684 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6685
6686 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6687 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6688 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6689 ASSERT3P(vec->zvec_func, ==, NULL);
6690
6691 /* if we are logging, the name must be valid */
6692 ASSERT(!allow_log || namecheck != NO_NAME);
6693
6694 vec->zvec_name = name;
6695 vec->zvec_func = func;
6696 vec->zvec_secpolicy = secpolicy;
6697 vec->zvec_namecheck = namecheck;
6698 vec->zvec_pool_check = pool_check;
6699 vec->zvec_smush_outnvlist = smush_outnvlist;
6700 vec->zvec_allow_log = allow_log;
6701 vec->zvec_nvl_keys = nvl_keys;
6702 vec->zvec_nvl_key_count = num_keys;
6703 }
6704
6705 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)6706 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6707 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6708 zfs_ioc_poolcheck_t pool_check)
6709 {
6710 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6711 POOL_NAME, log_history, pool_check);
6712 }
6713
6714 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)6715 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6716 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6717 {
6718 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6719 DATASET_NAME, B_FALSE, pool_check);
6720 }
6721
6722 static void
zfs_ioctl_register_pool_modify(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func)6723 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6724 {
6725 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6726 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6727 }
6728
6729 static void
zfs_ioctl_register_pool_meta(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)6730 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6731 zfs_secpolicy_func_t *secpolicy)
6732 {
6733 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6734 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6735 }
6736
6737 static void
zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)6738 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6739 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6740 {
6741 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6742 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6743 }
6744
6745 static void
zfs_ioctl_register_dataset_read(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func)6746 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6747 {
6748 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6749 zfs_secpolicy_read);
6750 }
6751
6752 static void
zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)6753 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6754 zfs_secpolicy_func_t *secpolicy)
6755 {
6756 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6757 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6758 }
6759
6760 static void
zfs_ioctl_init(void)6761 zfs_ioctl_init(void)
6762 {
6763 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6764 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6765 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6766 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
6767
6768 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6769 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6770 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6771 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
6772
6773 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6774 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6775 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6776 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
6777
6778 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6779 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6780 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6781 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
6782
6783 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6784 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6785 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6786 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
6787
6788 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6789 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6790 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6791 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
6792
6793 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6794 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6795 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6796 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
6797
6798 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6799 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
6800 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6801 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
6802
6803 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6804 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6805 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6806 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
6807
6808 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6809 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6810 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6811 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
6812 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6813 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6814 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6815 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
6816
6817 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6818 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6819 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6820 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
6821
6822 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6823 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6824 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6825 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
6826
6827 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6828 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6829 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6830 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
6831
6832 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6833 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6834 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6835 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
6836
6837 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6838 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6839 POOL_NAME,
6840 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6841 zfs_keys_destroy_bookmarks,
6842 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
6843
6844 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6845 zfs_ioc_load_key, zfs_secpolicy_load_key,
6846 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6847 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
6848 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6849 zfs_ioc_unload_key, zfs_secpolicy_load_key,
6850 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6851 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
6852 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6853 zfs_ioc_change_key, zfs_secpolicy_change_key,
6854 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
6855 B_TRUE, B_TRUE, zfs_keys_change_key,
6856 ARRAY_SIZE(zfs_keys_change_key));
6857
6858 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6859 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
6860 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6861 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
6862
6863 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6864 zfs_ioc_channel_program, zfs_secpolicy_config,
6865 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6866 B_TRUE, zfs_keys_channel_program,
6867 ARRAY_SIZE(zfs_keys_channel_program));
6868
6869 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6870 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6871 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6872 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
6873
6874 zfs_ioctl_register("zpool_discard_checkpoint",
6875 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6876 zfs_secpolicy_config, POOL_NAME,
6877 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6878 zfs_keys_pool_discard_checkpoint,
6879 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
6880
6881 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
6882 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
6883 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6884 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
6885
6886 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
6887 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
6888 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6889 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
6890
6891 zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV,
6892 zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME,
6893 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6894 zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv));
6895
6896 zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV,
6897 zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME,
6898 POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE,
6899 zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv));
6900
6901 zfs_ioctl_register("wait", ZFS_IOC_WAIT,
6902 zfs_ioc_wait, zfs_secpolicy_none, POOL_NAME,
6903 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6904 zfs_keys_pool_wait, ARRAY_SIZE(zfs_keys_pool_wait));
6905
6906 /* IOCTLS that use the legacy function signature */
6907
6908 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6909 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6910
6911 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6912 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6913 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6914 zfs_ioc_pool_scan);
6915 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6916 zfs_ioc_pool_upgrade);
6917 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6918 zfs_ioc_vdev_add);
6919 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6920 zfs_ioc_vdev_remove);
6921 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6922 zfs_ioc_vdev_set_state);
6923 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6924 zfs_ioc_vdev_attach);
6925 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6926 zfs_ioc_vdev_detach);
6927 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6928 zfs_ioc_vdev_setpath);
6929 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6930 zfs_ioc_vdev_setfru);
6931 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6932 zfs_ioc_pool_set_props);
6933 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6934 zfs_ioc_vdev_split);
6935 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6936 zfs_ioc_pool_reguid);
6937
6938 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6939 zfs_ioc_pool_configs, zfs_secpolicy_none);
6940 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6941 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6942 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6943 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6944 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6945 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6946 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6947 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6948
6949 /*
6950 * pool destroy, and export don't log the history as part of
6951 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6952 * does the logging of those commands.
6953 */
6954 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6955 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6956 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6957 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6958
6959 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6960 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6961 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6962 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6963
6964 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6965 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
6966 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6967 zfs_ioc_dsobj_to_dsname,
6968 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
6969 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6970 zfs_ioc_pool_get_history,
6971 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6972
6973 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6974 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6975
6976 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6977 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6978 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6979 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
6980
6981 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6982 zfs_ioc_space_written);
6983 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6984 zfs_ioc_objset_recvd_props);
6985 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6986 zfs_ioc_next_obj);
6987 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6988 zfs_ioc_get_fsacl);
6989 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6990 zfs_ioc_objset_stats);
6991 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6992 zfs_ioc_objset_zplprops);
6993 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6994 zfs_ioc_dataset_list_next);
6995 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6996 zfs_ioc_snapshot_list_next);
6997 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6998 zfs_ioc_send_progress);
6999
7000 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
7001 zfs_ioc_diff, zfs_secpolicy_diff);
7002 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
7003 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
7004 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
7005 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
7006 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
7007 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
7008 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
7009 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
7010 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
7011 zfs_ioc_send, zfs_secpolicy_send);
7012
7013 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
7014 zfs_secpolicy_none);
7015 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
7016 zfs_secpolicy_destroy);
7017 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
7018 zfs_secpolicy_rename);
7019 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
7020 zfs_secpolicy_recv);
7021 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
7022 zfs_secpolicy_promote);
7023 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
7024 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
7025 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
7026 zfs_secpolicy_set_fsacl);
7027
7028 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
7029 zfs_secpolicy_share, POOL_CHECK_NONE);
7030 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
7031 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
7032 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
7033 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
7034 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7035 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
7036 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
7037 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7038 }
7039
7040 /*
7041 * Verify that for non-legacy ioctls the input nvlist
7042 * pairs match against the expected input.
7043 *
7044 * Possible errors are:
7045 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
7046 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
7047 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
7048 */
7049 static int
zfs_check_input_nvpairs(nvlist_t * innvl,const zfs_ioc_vec_t * vec)7050 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
7051 {
7052 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
7053 boolean_t required_keys_found = B_FALSE;
7054
7055 /*
7056 * examine each input pair
7057 */
7058 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
7059 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
7060 char *name = nvpair_name(pair);
7061 data_type_t type = nvpair_type(pair);
7062 boolean_t identified = B_FALSE;
7063
7064 /*
7065 * check pair against the documented names and type
7066 */
7067 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7068 /* if not a wild card name, check for an exact match */
7069 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
7070 strcmp(nvl_keys[k].zkey_name, name) != 0)
7071 continue;
7072
7073 identified = B_TRUE;
7074
7075 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
7076 nvl_keys[k].zkey_type != type) {
7077 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
7078 }
7079
7080 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7081 continue;
7082
7083 required_keys_found = B_TRUE;
7084 break;
7085 }
7086
7087 /* allow an 'optional' key, everything else is invalid */
7088 if (!identified &&
7089 (strcmp(name, "optional") != 0 ||
7090 type != DATA_TYPE_NVLIST)) {
7091 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
7092 }
7093 }
7094
7095 /* verify that all required keys were found */
7096 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7097 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7098 continue;
7099
7100 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
7101 /* at least one non-optionial key is expected here */
7102 if (!required_keys_found)
7103 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7104 continue;
7105 }
7106
7107 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
7108 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7109 }
7110
7111 return (0);
7112 }
7113
7114 int
pool_status_check(const char * name,zfs_ioc_namecheck_t type,zfs_ioc_poolcheck_t check)7115 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
7116 zfs_ioc_poolcheck_t check)
7117 {
7118 spa_t *spa;
7119 int error;
7120
7121 ASSERT(type == POOL_NAME || type == DATASET_NAME);
7122
7123 if (check & POOL_CHECK_NONE)
7124 return (0);
7125
7126 error = spa_open(name, &spa, FTAG);
7127 if (error == 0) {
7128 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
7129 error = SET_ERROR(EAGAIN);
7130 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
7131 error = SET_ERROR(EROFS);
7132 spa_close(spa, FTAG);
7133 }
7134 return (error);
7135 }
7136
7137 /*
7138 * Find a free minor number.
7139 */
7140 minor_t
zfsdev_minor_alloc(void)7141 zfsdev_minor_alloc(void)
7142 {
7143 static minor_t last_minor;
7144 minor_t m;
7145
7146 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7147
7148 for (m = last_minor + 1; m != last_minor; m++) {
7149 if (m > ZFSDEV_MAX_MINOR)
7150 m = 1;
7151 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
7152 last_minor = m;
7153 return (m);
7154 }
7155 }
7156
7157 return (0);
7158 }
7159
7160 static int
zfs_ctldev_init(dev_t * devp)7161 zfs_ctldev_init(dev_t *devp)
7162 {
7163 minor_t minor;
7164 zfs_soft_state_t *zs;
7165
7166 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7167 ASSERT(getminor(*devp) == 0);
7168
7169 minor = zfsdev_minor_alloc();
7170 if (minor == 0)
7171 return (SET_ERROR(ENXIO));
7172
7173 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
7174 return (SET_ERROR(EAGAIN));
7175
7176 *devp = makedevice(getemajor(*devp), minor);
7177
7178 zs = ddi_get_soft_state(zfsdev_state, minor);
7179 zs->zss_type = ZSST_CTLDEV;
7180 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
7181
7182 return (0);
7183 }
7184
7185 static void
zfs_ctldev_destroy(zfs_onexit_t * zo,minor_t minor)7186 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
7187 {
7188 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7189
7190 zfs_onexit_destroy(zo);
7191 ddi_soft_state_free(zfsdev_state, minor);
7192 }
7193
7194 void *
zfsdev_get_soft_state(minor_t minor,enum zfs_soft_state_type which)7195 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
7196 {
7197 zfs_soft_state_t *zp;
7198
7199 zp = ddi_get_soft_state(zfsdev_state, minor);
7200 if (zp == NULL || zp->zss_type != which)
7201 return (NULL);
7202
7203 return (zp->zss_data);
7204 }
7205
7206 static int
zfsdev_open(dev_t * devp,int flag,int otyp,cred_t * cr)7207 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
7208 {
7209 int error = 0;
7210
7211 if (getminor(*devp) != 0)
7212 return (zvol_open(devp, flag, otyp, cr));
7213
7214 /* This is the control device. Allocate a new minor if requested. */
7215 if (flag & FEXCL) {
7216 mutex_enter(&zfsdev_state_lock);
7217 error = zfs_ctldev_init(devp);
7218 mutex_exit(&zfsdev_state_lock);
7219 }
7220
7221 return (error);
7222 }
7223
7224 static int
zfsdev_close(dev_t dev,int flag,int otyp,cred_t * cr)7225 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
7226 {
7227 zfs_onexit_t *zo;
7228 minor_t minor = getminor(dev);
7229
7230 if (minor == 0)
7231 return (0);
7232
7233 mutex_enter(&zfsdev_state_lock);
7234 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
7235 if (zo == NULL) {
7236 mutex_exit(&zfsdev_state_lock);
7237 return (zvol_close(dev, flag, otyp, cr));
7238 }
7239 zfs_ctldev_destroy(zo, minor);
7240 mutex_exit(&zfsdev_state_lock);
7241
7242 return (0);
7243 }
7244
7245 static int
zfsdev_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp)7246 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
7247 {
7248 zfs_cmd_t *zc;
7249 uint_t vecnum;
7250 int error, rc, len;
7251 minor_t minor = getminor(dev);
7252 const zfs_ioc_vec_t *vec;
7253 char *saved_poolname = NULL;
7254 nvlist_t *innvl = NULL;
7255
7256 if (minor != 0 &&
7257 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
7258 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
7259
7260 vecnum = cmd - ZFS_IOC_FIRST;
7261 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
7262
7263 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7264 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7265 vec = &zfs_ioc_vec[vecnum];
7266
7267 /*
7268 * The registered ioctl list may be sparse, verify that either
7269 * a normal or legacy handler are registered.
7270 */
7271 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
7272 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7273
7274 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
7275
7276 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
7277 if (error != 0) {
7278 error = SET_ERROR(EFAULT);
7279 goto out;
7280 }
7281
7282 zc->zc_iflags = flag & FKIOCTL;
7283 if (zc->zc_nvlist_src_size != 0) {
7284 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7285 zc->zc_iflags, &innvl);
7286 if (error != 0)
7287 goto out;
7288 }
7289
7290 /*
7291 * Ensure that all pool/dataset names are valid before we pass down to
7292 * the lower layers.
7293 */
7294 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7295 switch (vec->zvec_namecheck) {
7296 case POOL_NAME:
7297 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7298 error = SET_ERROR(EINVAL);
7299 else
7300 error = pool_status_check(zc->zc_name,
7301 vec->zvec_namecheck, vec->zvec_pool_check);
7302 break;
7303
7304 case DATASET_NAME:
7305 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7306 error = SET_ERROR(EINVAL);
7307 else
7308 error = pool_status_check(zc->zc_name,
7309 vec->zvec_namecheck, vec->zvec_pool_check);
7310 break;
7311
7312 case NO_NAME:
7313 break;
7314 }
7315
7316 /*
7317 * Ensure that all input pairs are valid before we pass them down
7318 * to the lower layers.
7319 *
7320 * The vectored functions can use fnvlist_lookup_{type} for any
7321 * required pairs since zfs_check_input_nvpairs() confirmed that
7322 * they exist and are of the correct type.
7323 */
7324 if (error == 0 && vec->zvec_func != NULL) {
7325 error = zfs_check_input_nvpairs(innvl, vec);
7326 if (error != 0)
7327 goto out;
7328 }
7329
7330 if (error == 0)
7331 error = vec->zvec_secpolicy(zc, innvl, cr);
7332
7333 if (error != 0)
7334 goto out;
7335
7336 /* legacy ioctls can modify zc_name */
7337 len = strcspn(zc->zc_name, "/@#") + 1;
7338 saved_poolname = kmem_alloc(len, KM_SLEEP);
7339 (void) strlcpy(saved_poolname, zc->zc_name, len);
7340
7341 if (vec->zvec_func != NULL) {
7342 nvlist_t *outnvl;
7343 int puterror = 0;
7344 spa_t *spa;
7345 nvlist_t *lognv = NULL;
7346
7347 ASSERT(vec->zvec_legacy_func == NULL);
7348
7349 /*
7350 * Add the innvl to the lognv before calling the func,
7351 * in case the func changes the innvl.
7352 */
7353 if (vec->zvec_allow_log) {
7354 lognv = fnvlist_alloc();
7355 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7356 vec->zvec_name);
7357 if (!nvlist_empty(innvl)) {
7358 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7359 innvl);
7360 }
7361 }
7362
7363 outnvl = fnvlist_alloc();
7364 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7365
7366 /*
7367 * Some commands can partially execute, modify state, and still
7368 * return an error. In these cases, attempt to record what
7369 * was modified.
7370 */
7371 if ((error == 0 ||
7372 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7373 vec->zvec_allow_log &&
7374 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7375 if (!nvlist_empty(outnvl)) {
7376 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
7377 outnvl);
7378 }
7379 if (error != 0) {
7380 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7381 error);
7382 }
7383 (void) spa_history_log_nvl(spa, lognv);
7384 spa_close(spa, FTAG);
7385 }
7386 fnvlist_free(lognv);
7387
7388 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7389 int smusherror = 0;
7390 if (vec->zvec_smush_outnvlist) {
7391 smusherror = nvlist_smush(outnvl,
7392 zc->zc_nvlist_dst_size);
7393 }
7394 if (smusherror == 0)
7395 puterror = put_nvlist(zc, outnvl);
7396 }
7397
7398 if (puterror != 0)
7399 error = puterror;
7400
7401 nvlist_free(outnvl);
7402 } else {
7403 error = vec->zvec_legacy_func(zc);
7404 }
7405
7406 out:
7407 nvlist_free(innvl);
7408 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
7409 if (error == 0 && rc != 0)
7410 error = SET_ERROR(EFAULT);
7411 if (error == 0 && vec->zvec_allow_log) {
7412 char *s = tsd_get(zfs_allow_log_key);
7413 if (s != NULL)
7414 strfree(s);
7415 (void) tsd_set(zfs_allow_log_key, saved_poolname);
7416 } else {
7417 if (saved_poolname != NULL)
7418 strfree(saved_poolname);
7419 }
7420
7421 kmem_free(zc, sizeof (zfs_cmd_t));
7422 return (error);
7423 }
7424
7425 static int
zfs_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)7426 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7427 {
7428 if (cmd != DDI_ATTACH)
7429 return (DDI_FAILURE);
7430
7431 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
7432 DDI_PSEUDO, 0) == DDI_FAILURE)
7433 return (DDI_FAILURE);
7434
7435 zfs_dip = dip;
7436
7437 ddi_report_dev(dip);
7438
7439 return (DDI_SUCCESS);
7440 }
7441
7442 static int
zfs_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)7443 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
7444 {
7445 if (spa_busy() || zfs_busy() || zvol_busy())
7446 return (DDI_FAILURE);
7447
7448 if (cmd != DDI_DETACH)
7449 return (DDI_FAILURE);
7450
7451 zfs_dip = NULL;
7452
7453 ddi_prop_remove_all(dip);
7454 ddi_remove_minor_node(dip, NULL);
7455
7456 return (DDI_SUCCESS);
7457 }
7458
7459 /*ARGSUSED*/
7460 static int
zfs_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)7461 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
7462 {
7463 switch (infocmd) {
7464 case DDI_INFO_DEVT2DEVINFO:
7465 *result = zfs_dip;
7466 return (DDI_SUCCESS);
7467
7468 case DDI_INFO_DEVT2INSTANCE:
7469 *result = (void *)0;
7470 return (DDI_SUCCESS);
7471 }
7472
7473 return (DDI_FAILURE);
7474 }
7475
7476 /*
7477 * OK, so this is a little weird.
7478 *
7479 * /dev/zfs is the control node, i.e. minor 0.
7480 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
7481 *
7482 * /dev/zfs has basically nothing to do except serve up ioctls,
7483 * so most of the standard driver entry points are in zvol.c.
7484 */
7485 static struct cb_ops zfs_cb_ops = {
7486 zfsdev_open, /* open */
7487 zfsdev_close, /* close */
7488 zvol_strategy, /* strategy */
7489 nodev, /* print */
7490 zvol_dump, /* dump */
7491 zvol_read, /* read */
7492 zvol_write, /* write */
7493 zfsdev_ioctl, /* ioctl */
7494 nodev, /* devmap */
7495 nodev, /* mmap */
7496 nodev, /* segmap */
7497 nochpoll, /* poll */
7498 ddi_prop_op, /* prop_op */
7499 NULL, /* streamtab */
7500 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
7501 CB_REV, /* version */
7502 nodev, /* async read */
7503 nodev, /* async write */
7504 };
7505
7506 static struct dev_ops zfs_dev_ops = {
7507 DEVO_REV, /* version */
7508 0, /* refcnt */
7509 zfs_info, /* info */
7510 nulldev, /* identify */
7511 nulldev, /* probe */
7512 zfs_attach, /* attach */
7513 zfs_detach, /* detach */
7514 nodev, /* reset */
7515 &zfs_cb_ops, /* driver operations */
7516 NULL, /* no bus operations */
7517 NULL, /* power */
7518 ddi_quiesce_not_needed, /* quiesce */
7519 };
7520
7521 static struct modldrv zfs_modldrv = {
7522 &mod_driverops,
7523 "ZFS storage pool",
7524 &zfs_dev_ops
7525 };
7526
7527 static struct modlinkage modlinkage = {
7528 MODREV_1,
7529 (void *)&zfs_modlfs,
7530 (void *)&zfs_modldrv,
7531 NULL
7532 };
7533
7534 static void
zfs_allow_log_destroy(void * arg)7535 zfs_allow_log_destroy(void *arg)
7536 {
7537 char *poolname = arg;
7538 strfree(poolname);
7539 }
7540
7541 int
_init(void)7542 _init(void)
7543 {
7544 int error;
7545
7546 spa_init(FREAD | FWRITE);
7547 zfs_init();
7548 zvol_init();
7549 zfs_ioctl_init();
7550
7551 if ((error = mod_install(&modlinkage)) != 0) {
7552 zvol_fini();
7553 zfs_fini();
7554 spa_fini();
7555 return (error);
7556 }
7557
7558 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7559 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7560
7561 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
7562 ASSERT(error == 0);
7563 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
7564
7565 return (0);
7566 }
7567
7568 int
_fini(void)7569 _fini(void)
7570 {
7571 int error;
7572
7573 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
7574 return (SET_ERROR(EBUSY));
7575
7576 if ((error = mod_remove(&modlinkage)) != 0)
7577 return (error);
7578
7579 zvol_fini();
7580 zfs_fini();
7581 spa_fini();
7582 if (zfs_nfsshare_inited)
7583 (void) ddi_modclose(nfs_mod);
7584 if (zfs_smbshare_inited)
7585 (void) ddi_modclose(smbsrv_mod);
7586 if (zfs_nfsshare_inited || zfs_smbshare_inited)
7587 (void) ddi_modclose(sharefs_mod);
7588
7589 ldi_ident_release(zfs_li);
7590 zfs_li = NULL;
7591 mutex_destroy(&zfs_share_lock);
7592
7593 return (error);
7594 }
7595
7596 int
_info(struct modinfo * modinfop)7597 _info(struct modinfo *modinfop)
7598 {
7599 return (mod_info(&modlinkage, modinfop));
7600 }
7601