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