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