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