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