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