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