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