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