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