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