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