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