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