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