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