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