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