1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Martin Matuska. All rights reserved.
25 * Copyright 2019 Joyent, Inc.
26 * Copyright (c) 2022 Hewlett Packard Enterprise Development LP.
27 */
28
29 #include <sys/zfs_context.h>
30 #include <sys/dmu.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_prop.h>
36 #include <sys/dsl_synctask.h>
37 #include <sys/spa.h>
38 #include <sys/zap.h>
39 #include <sys/fs/zfs.h>
40
41 #include "zfs_prop.h"
42
43 #define ZPROP_INHERIT_SUFFIX "$inherit"
44 #define ZPROP_RECVD_SUFFIX "$recvd"
45 #define ZPROP_IUV_SUFFIX "$iuv"
46
47 static int
dodefault(zfs_prop_t prop,int intsz,int numints,void * buf)48 dodefault(zfs_prop_t prop, int intsz, int numints, void *buf)
49 {
50 /*
51 * The setonce properties are read-only, BUT they still
52 * have a default value that can be used as the initial
53 * value.
54 */
55 if (prop == ZPROP_INVAL ||
56 (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
57 return (SET_ERROR(ENOENT));
58
59 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
60 if (intsz != 1)
61 return (SET_ERROR(EOVERFLOW));
62 (void) strlcpy(buf, zfs_prop_default_string(prop),
63 numints);
64 } else {
65 if (intsz != 8 || numints < 1)
66 return (SET_ERROR(EOVERFLOW));
67
68 *(uint64_t *)buf = zfs_prop_default_numeric(prop);
69 }
70
71 return (0);
72 }
73
74 static int
dsl_prop_known_index(zfs_prop_t prop,uint64_t value)75 dsl_prop_known_index(zfs_prop_t prop, uint64_t value)
76 {
77 const char *str = NULL;
78 if (prop != ZPROP_CONT && prop != ZPROP_INVAL &&
79 zfs_prop_get_type(prop) == PROP_TYPE_INDEX)
80 return (!zfs_prop_index_to_string(prop, value, &str));
81
82 return (-1);
83 }
84
85 int
dsl_prop_get_dd(dsl_dir_t * dd,const char * propname,int intsz,int numints,void * buf,char * setpoint,boolean_t snapshot)86 dsl_prop_get_dd(dsl_dir_t *dd, const char *propname,
87 int intsz, int numints, void *buf, char *setpoint, boolean_t snapshot)
88 {
89 int err;
90 dsl_dir_t *target = dd;
91 objset_t *mos = dd->dd_pool->dp_meta_objset;
92 zfs_prop_t prop;
93 boolean_t inheritable;
94 boolean_t inheriting = B_FALSE;
95 char *inheritstr;
96 char *recvdstr;
97 char *iuvstr;
98
99 ASSERT(dsl_pool_config_held(dd->dd_pool));
100
101 if (setpoint)
102 setpoint[0] = '\0';
103
104 prop = zfs_name_to_prop(propname);
105 inheritable = (prop == ZPROP_USERPROP || zfs_prop_inheritable(prop));
106 inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
107 recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
108 iuvstr = kmem_asprintf("%s%s", propname, ZPROP_IUV_SUFFIX);
109
110 /*
111 * Note: dd may become NULL, therefore we shouldn't dereference it
112 * after this loop.
113 */
114 for (; dd != NULL; dd = dd->dd_parent) {
115 if (dd != target || snapshot) {
116 if (!inheritable) {
117 err = SET_ERROR(ENOENT);
118 break;
119 }
120 inheriting = B_TRUE;
121 }
122
123 /* Check for a iuv value. */
124 err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
125 iuvstr, intsz, numints, buf);
126 if (err == 0 && dsl_prop_known_index(prop,
127 *(uint64_t *)buf) != 1)
128 err = ENOENT;
129 if (err != ENOENT) {
130 if (setpoint != NULL && err == 0)
131 dsl_dir_name(dd, setpoint);
132 break;
133 }
134
135 /* Check for a local value. */
136 err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
137 propname, intsz, numints, buf);
138 if (err != ENOENT) {
139 if (setpoint != NULL && err == 0)
140 dsl_dir_name(dd, setpoint);
141 break;
142 }
143
144 /*
145 * Skip the check for a received value if there is an explicit
146 * inheritance entry.
147 */
148 err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
149 inheritstr);
150 if (err != 0 && err != ENOENT)
151 break;
152
153 if (err == ENOENT) {
154 /* Check for a received value. */
155 err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
156 recvdstr, intsz, numints, buf);
157 if (err != ENOENT) {
158 if (setpoint != NULL && err == 0) {
159 if (inheriting) {
160 dsl_dir_name(dd, setpoint);
161 } else {
162 (void) strlcpy(setpoint,
163 ZPROP_SOURCE_VAL_RECVD,
164 MAXNAMELEN);
165 }
166 }
167 break;
168 }
169 }
170
171 /*
172 * If we found an explicit inheritance entry, err is zero even
173 * though we haven't yet found the value, so reinitializing err
174 * at the end of the loop (instead of at the beginning) ensures
175 * that err has a valid post-loop value.
176 */
177 err = SET_ERROR(ENOENT);
178 }
179
180 if (err == ENOENT)
181 err = dodefault(prop, intsz, numints, buf);
182
183 kmem_strfree(inheritstr);
184 kmem_strfree(recvdstr);
185 kmem_strfree(iuvstr);
186
187 return (err);
188 }
189
190 int
dsl_prop_get_ds(dsl_dataset_t * ds,const char * propname,int intsz,int numints,void * buf,char * setpoint)191 dsl_prop_get_ds(dsl_dataset_t *ds, const char *propname,
192 int intsz, int numints, void *buf, char *setpoint)
193 {
194 zfs_prop_t prop = zfs_name_to_prop(propname);
195 boolean_t inheritable;
196 uint64_t zapobj;
197
198 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
199 inheritable = (prop == ZPROP_USERPROP || zfs_prop_inheritable(prop));
200 zapobj = dsl_dataset_phys(ds)->ds_props_obj;
201
202 if (zapobj != 0) {
203 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
204 int err;
205
206 ASSERT(ds->ds_is_snapshot);
207
208 /* Check for a local value. */
209 err = zap_lookup(mos, zapobj, propname, intsz, numints, buf);
210 if (err != ENOENT) {
211 if (setpoint != NULL && err == 0)
212 dsl_dataset_name(ds, setpoint);
213 return (err);
214 }
215
216 /*
217 * Skip the check for a received value if there is an explicit
218 * inheritance entry.
219 */
220 if (inheritable) {
221 char *inheritstr = kmem_asprintf("%s%s", propname,
222 ZPROP_INHERIT_SUFFIX);
223 err = zap_contains(mos, zapobj, inheritstr);
224 kmem_strfree(inheritstr);
225 if (err != 0 && err != ENOENT)
226 return (err);
227 }
228
229 if (err == ENOENT) {
230 /* Check for a received value. */
231 char *recvdstr = kmem_asprintf("%s%s", propname,
232 ZPROP_RECVD_SUFFIX);
233 err = zap_lookup(mos, zapobj, recvdstr,
234 intsz, numints, buf);
235 kmem_strfree(recvdstr);
236 if (err != ENOENT) {
237 if (setpoint != NULL && err == 0)
238 (void) strlcpy(setpoint,
239 ZPROP_SOURCE_VAL_RECVD,
240 MAXNAMELEN);
241 return (err);
242 }
243 }
244 }
245
246 return (dsl_prop_get_dd(ds->ds_dir, propname,
247 intsz, numints, buf, setpoint, ds->ds_is_snapshot));
248 }
249
250 static dsl_prop_record_t *
dsl_prop_record_find(dsl_dir_t * dd,const char * propname)251 dsl_prop_record_find(dsl_dir_t *dd, const char *propname)
252 {
253 dsl_prop_record_t *pr = NULL;
254
255 ASSERT(MUTEX_HELD(&dd->dd_lock));
256
257 for (pr = list_head(&dd->dd_props);
258 pr != NULL; pr = list_next(&dd->dd_props, pr)) {
259 if (strcmp(pr->pr_propname, propname) == 0)
260 break;
261 }
262
263 return (pr);
264 }
265
266 static dsl_prop_record_t *
dsl_prop_record_create(dsl_dir_t * dd,const char * propname)267 dsl_prop_record_create(dsl_dir_t *dd, const char *propname)
268 {
269 dsl_prop_record_t *pr;
270
271 ASSERT(MUTEX_HELD(&dd->dd_lock));
272
273 pr = kmem_alloc(sizeof (dsl_prop_record_t), KM_SLEEP);
274 pr->pr_propname = spa_strdup(propname);
275 list_create(&pr->pr_cbs, sizeof (dsl_prop_cb_record_t),
276 offsetof(dsl_prop_cb_record_t, cbr_pr_node));
277 list_insert_head(&dd->dd_props, pr);
278
279 return (pr);
280 }
281
282 void
dsl_prop_init(dsl_dir_t * dd)283 dsl_prop_init(dsl_dir_t *dd)
284 {
285 list_create(&dd->dd_props, sizeof (dsl_prop_record_t),
286 offsetof(dsl_prop_record_t, pr_node));
287 }
288
289 void
dsl_prop_fini(dsl_dir_t * dd)290 dsl_prop_fini(dsl_dir_t *dd)
291 {
292 dsl_prop_record_t *pr;
293
294 while ((pr = list_remove_head(&dd->dd_props)) != NULL) {
295 list_destroy(&pr->pr_cbs);
296 spa_strfree((char *)pr->pr_propname);
297 kmem_free(pr, sizeof (dsl_prop_record_t));
298 }
299 list_destroy(&dd->dd_props);
300 }
301
302 /*
303 * Register interest in the named property. We'll call the callback
304 * once to notify it of the current property value, and again each time
305 * the property changes, until this callback is unregistered.
306 *
307 * Return 0 on success, errno if the prop is not an integer value.
308 */
309 int
dsl_prop_register(dsl_dataset_t * ds,const char * propname,dsl_prop_changed_cb_t * callback,void * cbarg)310 dsl_prop_register(dsl_dataset_t *ds, const char *propname,
311 dsl_prop_changed_cb_t *callback, void *cbarg)
312 {
313 dsl_dir_t *dd = ds->ds_dir;
314 uint64_t value;
315 dsl_prop_record_t *pr;
316 dsl_prop_cb_record_t *cbr;
317 int err;
318 dsl_pool_t *dp __maybe_unused = dd->dd_pool;
319
320 ASSERT(dsl_pool_config_held(dp));
321
322 err = dsl_prop_get_int_ds(ds, propname, &value);
323 if (err != 0)
324 return (err);
325
326 cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
327 cbr->cbr_ds = ds;
328 cbr->cbr_func = callback;
329 cbr->cbr_arg = cbarg;
330
331 mutex_enter(&dd->dd_lock);
332 pr = dsl_prop_record_find(dd, propname);
333 if (pr == NULL)
334 pr = dsl_prop_record_create(dd, propname);
335 cbr->cbr_pr = pr;
336 list_insert_head(&pr->pr_cbs, cbr);
337 list_insert_head(&ds->ds_prop_cbs, cbr);
338 mutex_exit(&dd->dd_lock);
339
340 cbr->cbr_func(cbr->cbr_arg, value);
341 return (0);
342 }
343
344 int
dsl_prop_get(const char * dsname,const char * propname,int intsz,int numints,void * buf,char * setpoint)345 dsl_prop_get(const char *dsname, const char *propname,
346 int intsz, int numints, void *buf, char *setpoint)
347 {
348 objset_t *os;
349 int error;
350
351 error = dmu_objset_hold(dsname, FTAG, &os);
352 if (error != 0)
353 return (error);
354
355 error = dsl_prop_get_ds(dmu_objset_ds(os), propname,
356 intsz, numints, buf, setpoint);
357
358 dmu_objset_rele(os, FTAG);
359 return (error);
360 }
361
362 /*
363 * Get the current property value. It may have changed by the time this
364 * function returns, so it is NOT safe to follow up with
365 * dsl_prop_register() and assume that the value has not changed in
366 * between.
367 *
368 * Return 0 on success, ENOENT if ddname is invalid.
369 */
370 int
dsl_prop_get_integer(const char * ddname,const char * propname,uint64_t * valuep,char * setpoint)371 dsl_prop_get_integer(const char *ddname, const char *propname,
372 uint64_t *valuep, char *setpoint)
373 {
374 return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
375 }
376
377 int
dsl_prop_get_int_ds(dsl_dataset_t * ds,const char * propname,uint64_t * valuep)378 dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname,
379 uint64_t *valuep)
380 {
381 return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL));
382 }
383
384 /*
385 * Predict the effective value of the given special property if it were set with
386 * the given value and source. This is not a general purpose function. It exists
387 * only to handle the special requirements of the quota and reservation
388 * properties. The fact that these properties are non-inheritable greatly
389 * simplifies the prediction logic.
390 *
391 * Returns 0 on success, a positive error code on failure, or -1 if called with
392 * a property not handled by this function.
393 */
394 int
dsl_prop_predict(dsl_dir_t * dd,const char * propname,zprop_source_t source,uint64_t value,uint64_t * newvalp)395 dsl_prop_predict(dsl_dir_t *dd, const char *propname,
396 zprop_source_t source, uint64_t value, uint64_t *newvalp)
397 {
398 zfs_prop_t prop = zfs_name_to_prop(propname);
399 objset_t *mos;
400 uint64_t zapobj;
401 uint64_t version;
402 char *recvdstr;
403 int err = 0;
404
405 switch (prop) {
406 case ZFS_PROP_QUOTA:
407 case ZFS_PROP_RESERVATION:
408 case ZFS_PROP_REFQUOTA:
409 case ZFS_PROP_REFRESERVATION:
410 break;
411 default:
412 return (-1);
413 }
414
415 mos = dd->dd_pool->dp_meta_objset;
416 zapobj = dsl_dir_phys(dd)->dd_props_zapobj;
417 recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
418
419 version = spa_version(dd->dd_pool->dp_spa);
420 if (version < SPA_VERSION_RECVD_PROPS) {
421 if (source & ZPROP_SRC_NONE)
422 source = ZPROP_SRC_NONE;
423 else if (source & ZPROP_SRC_RECEIVED)
424 source = ZPROP_SRC_LOCAL;
425 }
426
427 switch ((int)source) {
428 case ZPROP_SRC_NONE:
429 /* Revert to the received value, if any. */
430 err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp);
431 if (err == ENOENT)
432 *newvalp = 0;
433 break;
434 case ZPROP_SRC_LOCAL:
435 *newvalp = value;
436 break;
437 case ZPROP_SRC_RECEIVED:
438 /*
439 * If there's no local setting, then the new received value will
440 * be the effective value.
441 */
442 err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
443 if (err == ENOENT)
444 *newvalp = value;
445 break;
446 case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
447 /*
448 * We're clearing the received value, so the local setting (if
449 * it exists) remains the effective value.
450 */
451 err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
452 if (err == ENOENT)
453 *newvalp = 0;
454 break;
455 default:
456 panic("unexpected property source: %d", source);
457 }
458
459 kmem_strfree(recvdstr);
460
461 if (err == ENOENT)
462 return (0);
463
464 return (err);
465 }
466
467 /*
468 * Unregister this callback. Return 0 on success, ENOENT if ddname is
469 * invalid, or ENOMSG if no matching callback registered.
470 *
471 * NOTE: This function is no longer used internally but has been preserved
472 * to prevent breaking external consumers (Lustre, etc).
473 */
474 int
dsl_prop_unregister(dsl_dataset_t * ds,const char * propname,dsl_prop_changed_cb_t * callback,void * cbarg)475 dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
476 dsl_prop_changed_cb_t *callback, void *cbarg)
477 {
478 dsl_dir_t *dd = ds->ds_dir;
479 dsl_prop_cb_record_t *cbr;
480
481 mutex_enter(&dd->dd_lock);
482 for (cbr = list_head(&ds->ds_prop_cbs);
483 cbr; cbr = list_next(&ds->ds_prop_cbs, cbr)) {
484 if (cbr->cbr_ds == ds &&
485 cbr->cbr_func == callback &&
486 cbr->cbr_arg == cbarg &&
487 strcmp(cbr->cbr_pr->pr_propname, propname) == 0)
488 break;
489 }
490
491 if (cbr == NULL) {
492 mutex_exit(&dd->dd_lock);
493 return (SET_ERROR(ENOMSG));
494 }
495
496 list_remove(&ds->ds_prop_cbs, cbr);
497 list_remove(&cbr->cbr_pr->pr_cbs, cbr);
498 mutex_exit(&dd->dd_lock);
499 kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
500
501 return (0);
502 }
503
504 /*
505 * Unregister all callbacks that are registered with the
506 * given callback argument.
507 */
508 void
dsl_prop_unregister_all(dsl_dataset_t * ds,void * cbarg)509 dsl_prop_unregister_all(dsl_dataset_t *ds, void *cbarg)
510 {
511 dsl_prop_cb_record_t *cbr, *next_cbr;
512
513 dsl_dir_t *dd = ds->ds_dir;
514
515 mutex_enter(&dd->dd_lock);
516 next_cbr = list_head(&ds->ds_prop_cbs);
517 while (next_cbr != NULL) {
518 cbr = next_cbr;
519 next_cbr = list_next(&ds->ds_prop_cbs, cbr);
520 if (cbr->cbr_arg == cbarg) {
521 list_remove(&ds->ds_prop_cbs, cbr);
522 list_remove(&cbr->cbr_pr->pr_cbs, cbr);
523 kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
524 }
525 }
526 mutex_exit(&dd->dd_lock);
527 }
528
529 boolean_t
dsl_prop_hascb(dsl_dataset_t * ds)530 dsl_prop_hascb(dsl_dataset_t *ds)
531 {
532 return (!list_is_empty(&ds->ds_prop_cbs));
533 }
534
535 static int
dsl_prop_notify_all_cb(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)536 dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
537 {
538 (void) arg;
539 dsl_dir_t *dd = ds->ds_dir;
540 dsl_prop_record_t *pr;
541 dsl_prop_cb_record_t *cbr;
542
543 mutex_enter(&dd->dd_lock);
544 for (pr = list_head(&dd->dd_props);
545 pr; pr = list_next(&dd->dd_props, pr)) {
546 for (cbr = list_head(&pr->pr_cbs); cbr;
547 cbr = list_next(&pr->pr_cbs, cbr)) {
548 uint64_t value;
549
550 /*
551 * Callback entries do not have holds on their
552 * datasets so that datasets with registered
553 * callbacks are still eligible for eviction.
554 * Unlike operations to update properties on a
555 * single dataset, we are performing a recursive
556 * descent of related head datasets. The caller
557 * of this function only has a dataset hold on
558 * the passed in head dataset, not the snapshots
559 * associated with this dataset. Without a hold,
560 * the dataset pointer within callback records
561 * for snapshots can be invalidated by eviction
562 * at any time.
563 *
564 * Use dsl_dataset_try_add_ref() to verify
565 * that the dataset for a snapshot has not
566 * begun eviction processing and to prevent
567 * eviction from occurring for the duration of
568 * the callback. If the hold attempt fails,
569 * this object is already being evicted and the
570 * callback can be safely ignored.
571 */
572 if (ds != cbr->cbr_ds &&
573 !dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
574 continue;
575
576 if (dsl_prop_get_ds(cbr->cbr_ds,
577 cbr->cbr_pr->pr_propname, sizeof (value), 1,
578 &value, NULL) == 0)
579 cbr->cbr_func(cbr->cbr_arg, value);
580
581 if (ds != cbr->cbr_ds)
582 dsl_dataset_rele(cbr->cbr_ds, FTAG);
583 }
584 }
585 mutex_exit(&dd->dd_lock);
586
587 return (0);
588 }
589
590 /*
591 * Update all property values for ddobj & its descendants. This is used
592 * when renaming the dir.
593 */
594 void
dsl_prop_notify_all(dsl_dir_t * dd)595 dsl_prop_notify_all(dsl_dir_t *dd)
596 {
597 dsl_pool_t *dp = dd->dd_pool;
598 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
599 (void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb,
600 NULL, DS_FIND_CHILDREN);
601 }
602
603 static void
dsl_prop_changed_notify(dsl_pool_t * dp,uint64_t ddobj,const char * propname,uint64_t value,int first)604 dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
605 const char *propname, uint64_t value, int first)
606 {
607 dsl_dir_t *dd;
608 dsl_prop_record_t *pr;
609 dsl_prop_cb_record_t *cbr;
610 objset_t *mos = dp->dp_meta_objset;
611 zap_cursor_t zc;
612 zap_attribute_t *za;
613 int err;
614
615 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
616 err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
617 if (err)
618 return;
619
620 if (!first) {
621 /*
622 * If the prop is set here, then this change is not
623 * being inherited here or below; stop the recursion.
624 */
625 err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
626 propname);
627 if (err == 0) {
628 dsl_dir_rele(dd, FTAG);
629 return;
630 }
631 ASSERT3U(err, ==, ENOENT);
632 }
633
634 mutex_enter(&dd->dd_lock);
635 pr = dsl_prop_record_find(dd, propname);
636 if (pr != NULL) {
637 for (cbr = list_head(&pr->pr_cbs); cbr;
638 cbr = list_next(&pr->pr_cbs, cbr)) {
639 uint64_t propobj;
640
641 /*
642 * cbr->cbr_ds may be invalidated due to eviction,
643 * requiring the use of dsl_dataset_try_add_ref().
644 * See comment block in dsl_prop_notify_all_cb()
645 * for details.
646 */
647 if (!dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
648 continue;
649
650 propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj;
651
652 /*
653 * If the property is not set on this ds, then it is
654 * inherited here; call the callback.
655 */
656 if (propobj == 0 ||
657 zap_contains(mos, propobj, propname) != 0)
658 cbr->cbr_func(cbr->cbr_arg, value);
659
660 dsl_dataset_rele(cbr->cbr_ds, FTAG);
661 }
662 }
663 mutex_exit(&dd->dd_lock);
664
665 za = zap_attribute_alloc();
666 for (zap_cursor_init(&zc, mos,
667 dsl_dir_phys(dd)->dd_child_dir_zapobj);
668 zap_cursor_retrieve(&zc, za) == 0;
669 zap_cursor_advance(&zc)) {
670 dsl_prop_changed_notify(dp, za->za_first_integer,
671 propname, value, FALSE);
672 }
673 zap_attribute_free(za);
674 zap_cursor_fini(&zc);
675 dsl_dir_rele(dd, FTAG);
676 }
677
678
679 /*
680 * For newer values in zfs index type properties, we add a new key
681 * propname$iuv (iuv = Ignore Unknown Values) to the properties zap object
682 * to store the new property value and store the default value in the
683 * existing prop key. So that the propname$iuv key is ignored by the older zfs
684 * versions and the default property value from the existing prop key is
685 * used.
686 */
687 static void
dsl_prop_set_iuv(objset_t * mos,uint64_t zapobj,const char * propname,int intsz,int numints,const void * value,dmu_tx_t * tx)688 dsl_prop_set_iuv(objset_t *mos, uint64_t zapobj, const char *propname,
689 int intsz, int numints, const void *value, dmu_tx_t *tx)
690 {
691 char *iuvstr = kmem_asprintf("%s%s", propname, ZPROP_IUV_SUFFIX);
692 boolean_t iuv = B_FALSE;
693 zfs_prop_t prop = zfs_name_to_prop(propname);
694
695 switch (prop) {
696 case ZFS_PROP_REDUNDANT_METADATA:
697 if (*(uint64_t *)value == ZFS_REDUNDANT_METADATA_SOME ||
698 *(uint64_t *)value == ZFS_REDUNDANT_METADATA_NONE)
699 iuv = B_TRUE;
700 break;
701 case ZFS_PROP_SNAPDIR:
702 if (*(uint64_t *)value == ZFS_SNAPDIR_DISABLED)
703 iuv = B_TRUE;
704 break;
705 default:
706 break;
707 }
708
709 if (iuv) {
710 VERIFY0(zap_update(mos, zapobj, iuvstr, intsz, numints,
711 value, tx));
712 uint64_t val = zfs_prop_default_numeric(prop);
713 VERIFY0(zap_update(mos, zapobj, propname, intsz, numints,
714 &val, tx));
715 } else {
716 zap_remove(mos, zapobj, iuvstr, tx);
717 }
718 kmem_strfree(iuvstr);
719 }
720
721 void
dsl_prop_set_sync_impl(dsl_dataset_t * ds,const char * propname,zprop_source_t source,int intsz,int numints,const void * value,dmu_tx_t * tx)722 dsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname,
723 zprop_source_t source, int intsz, int numints, const void *value,
724 dmu_tx_t *tx)
725 {
726 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
727 uint64_t zapobj, intval, dummy, count;
728 int isint;
729 char valbuf[32];
730 const char *valstr = NULL;
731 char *inheritstr;
732 char *recvdstr;
733 char *iuvstr;
734 char *tbuf = NULL;
735 int err;
736 uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa);
737
738 isint = (dodefault(zfs_name_to_prop(propname), 8, 1, &intval) == 0);
739
740 if (ds->ds_is_snapshot) {
741 ASSERT(version >= SPA_VERSION_SNAP_PROPS);
742 if (dsl_dataset_phys(ds)->ds_props_obj == 0 &&
743 (source & ZPROP_SRC_NONE) == 0) {
744 dmu_buf_will_dirty(ds->ds_dbuf, tx);
745 dsl_dataset_phys(ds)->ds_props_obj =
746 zap_create(mos,
747 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
748 }
749 zapobj = dsl_dataset_phys(ds)->ds_props_obj;
750 } else {
751 zapobj = dsl_dir_phys(ds->ds_dir)->dd_props_zapobj;
752 }
753
754 /* If we are removing objects from a non-existent ZAP just return */
755 if (zapobj == 0)
756 return;
757
758 if (version < SPA_VERSION_RECVD_PROPS) {
759 if (source & ZPROP_SRC_NONE)
760 source = ZPROP_SRC_NONE;
761 else if (source & ZPROP_SRC_RECEIVED)
762 source = ZPROP_SRC_LOCAL;
763 }
764
765 inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
766 recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
767 iuvstr = kmem_asprintf("%s%s", propname, ZPROP_IUV_SUFFIX);
768
769 switch ((int)source) {
770 case ZPROP_SRC_NONE:
771 /*
772 * revert to received value, if any (inherit -S)
773 * - remove propname
774 * - remove propname$inherit
775 */
776 err = zap_remove(mos, zapobj, propname, tx);
777 ASSERT(err == 0 || err == ENOENT);
778 err = zap_remove(mos, zapobj, inheritstr, tx);
779 ASSERT(err == 0 || err == ENOENT);
780 break;
781 case ZPROP_SRC_LOCAL:
782 /*
783 * remove propname$inherit
784 * set propname -> value
785 * set propname$iuv -> new property value
786 */
787 err = zap_remove(mos, zapobj, inheritstr, tx);
788 ASSERT(err == 0 || err == ENOENT);
789 VERIFY0(zap_update(mos, zapobj, propname,
790 intsz, numints, value, tx));
791 (void) dsl_prop_set_iuv(mos, zapobj, propname, intsz,
792 numints, value, tx);
793 break;
794 case ZPROP_SRC_INHERITED:
795 /*
796 * explicitly inherit
797 * - remove propname
798 * - set propname$inherit
799 */
800 err = zap_remove(mos, zapobj, propname, tx);
801 ASSERT(err == 0 || err == ENOENT);
802 err = zap_remove(mos, zapobj, iuvstr, tx);
803 ASSERT(err == 0 || err == ENOENT);
804 if (version >= SPA_VERSION_RECVD_PROPS &&
805 dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) {
806 dummy = 0;
807 VERIFY0(zap_update(mos, zapobj, inheritstr,
808 8, 1, &dummy, tx));
809 }
810 break;
811 case ZPROP_SRC_RECEIVED:
812 /*
813 * set propname$recvd -> value
814 */
815 err = zap_update(mos, zapobj, recvdstr,
816 intsz, numints, value, tx);
817 ASSERT(err == 0);
818 break;
819 case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED):
820 /*
821 * clear local and received settings
822 * - remove propname
823 * - remove propname$inherit
824 * - remove propname$recvd
825 */
826 err = zap_remove(mos, zapobj, propname, tx);
827 ASSERT(err == 0 || err == ENOENT);
828 err = zap_remove(mos, zapobj, inheritstr, tx);
829 ASSERT(err == 0 || err == ENOENT);
830 zfs_fallthrough;
831 case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
832 /*
833 * remove propname$recvd
834 */
835 err = zap_remove(mos, zapobj, recvdstr, tx);
836 ASSERT(err == 0 || err == ENOENT);
837 break;
838 default:
839 cmn_err(CE_PANIC, "unexpected property source: %d", source);
840 }
841
842 kmem_strfree(inheritstr);
843 kmem_strfree(recvdstr);
844 kmem_strfree(iuvstr);
845
846 /*
847 * If we are left with an empty snap zap we can destroy it.
848 * This will prevent unnecessary calls to zap_lookup() in
849 * the "zfs list" and "zfs get" code paths.
850 */
851 if (ds->ds_is_snapshot &&
852 zap_count(mos, zapobj, &count) == 0 && count == 0) {
853 dmu_buf_will_dirty(ds->ds_dbuf, tx);
854 dsl_dataset_phys(ds)->ds_props_obj = 0;
855 zap_destroy(mos, zapobj, tx);
856 }
857
858 if (isint) {
859 VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval));
860
861 if (ds->ds_is_snapshot) {
862 dsl_prop_cb_record_t *cbr;
863 /*
864 * It's a snapshot; nothing can inherit this
865 * property, so just look for callbacks on this
866 * ds here.
867 */
868 mutex_enter(&ds->ds_dir->dd_lock);
869 for (cbr = list_head(&ds->ds_prop_cbs); cbr;
870 cbr = list_next(&ds->ds_prop_cbs, cbr)) {
871 if (strcmp(cbr->cbr_pr->pr_propname,
872 propname) == 0)
873 cbr->cbr_func(cbr->cbr_arg, intval);
874 }
875 mutex_exit(&ds->ds_dir->dd_lock);
876 } else {
877 dsl_prop_changed_notify(ds->ds_dir->dd_pool,
878 ds->ds_dir->dd_object, propname, intval, TRUE);
879 }
880
881 (void) snprintf(valbuf, sizeof (valbuf),
882 "%lld", (longlong_t)intval);
883 valstr = valbuf;
884 } else {
885 if (source == ZPROP_SRC_LOCAL) {
886 valstr = value;
887 } else {
888 tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
889 if (dsl_prop_get_ds(ds, propname, 1,
890 ZAP_MAXVALUELEN, tbuf, NULL) == 0)
891 valstr = tbuf;
892 }
893 }
894
895 spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE ||
896 source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx,
897 "%s=%s", propname, (valstr == NULL ? "" : valstr));
898
899 if (tbuf != NULL)
900 kmem_free(tbuf, ZAP_MAXVALUELEN);
901 }
902
903 int
dsl_prop_set_int(const char * dsname,const char * propname,zprop_source_t source,uint64_t value)904 dsl_prop_set_int(const char *dsname, const char *propname,
905 zprop_source_t source, uint64_t value)
906 {
907 nvlist_t *nvl = fnvlist_alloc();
908 int error;
909
910 fnvlist_add_uint64(nvl, propname, value);
911 error = dsl_props_set(dsname, source, nvl);
912 fnvlist_free(nvl);
913 return (error);
914 }
915
916 int
dsl_prop_set_string(const char * dsname,const char * propname,zprop_source_t source,const char * value)917 dsl_prop_set_string(const char *dsname, const char *propname,
918 zprop_source_t source, const char *value)
919 {
920 nvlist_t *nvl = fnvlist_alloc();
921 int error;
922
923 fnvlist_add_string(nvl, propname, value);
924 error = dsl_props_set(dsname, source, nvl);
925 fnvlist_free(nvl);
926 return (error);
927 }
928
929 int
dsl_prop_inherit(const char * dsname,const char * propname,zprop_source_t source)930 dsl_prop_inherit(const char *dsname, const char *propname,
931 zprop_source_t source)
932 {
933 nvlist_t *nvl = fnvlist_alloc();
934 int error;
935
936 fnvlist_add_boolean(nvl, propname);
937 error = dsl_props_set(dsname, source, nvl);
938 fnvlist_free(nvl);
939 return (error);
940 }
941
942 int
dsl_props_set_check(void * arg,dmu_tx_t * tx)943 dsl_props_set_check(void *arg, dmu_tx_t *tx)
944 {
945 dsl_props_set_arg_t *dpsa = arg;
946 dsl_pool_t *dp = dmu_tx_pool(tx);
947 dsl_dataset_t *ds;
948 uint64_t version;
949 nvpair_t *elem = NULL;
950 int err;
951
952 err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds);
953 if (err != 0)
954 return (err);
955
956 version = spa_version(ds->ds_dir->dd_pool->dp_spa);
957 while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) {
958 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
959 dsl_dataset_rele(ds, FTAG);
960 return (SET_ERROR(ENAMETOOLONG));
961 }
962 if (nvpair_type(elem) == DATA_TYPE_STRING) {
963 const char *valstr = fnvpair_value_string(elem);
964 if (strlen(valstr) >= (version <
965 SPA_VERSION_STMF_PROP ?
966 ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) {
967 dsl_dataset_rele(ds, FTAG);
968 return (SET_ERROR(E2BIG));
969 }
970 }
971 }
972
973 if (ds->ds_is_snapshot && version < SPA_VERSION_SNAP_PROPS) {
974 dsl_dataset_rele(ds, FTAG);
975 return (SET_ERROR(ENOTSUP));
976 }
977 dsl_dataset_rele(ds, FTAG);
978 return (0);
979 }
980
981 void
dsl_props_set_sync_impl(dsl_dataset_t * ds,zprop_source_t source,nvlist_t * props,dmu_tx_t * tx)982 dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source,
983 nvlist_t *props, dmu_tx_t *tx)
984 {
985 nvpair_t *elem = NULL;
986
987 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
988 nvpair_t *pair = elem;
989 const char *name = nvpair_name(pair);
990
991 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
992 /*
993 * This usually happens when we reuse the nvlist_t data
994 * returned by the counterpart dsl_prop_get_all_impl().
995 * For instance we do this to restore the original
996 * received properties when an error occurs in the
997 * zfs_ioc_recv() codepath.
998 */
999 nvlist_t *attrs = fnvpair_value_nvlist(pair);
1000 pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
1001 }
1002
1003 if (nvpair_type(pair) == DATA_TYPE_STRING) {
1004 const char *value = fnvpair_value_string(pair);
1005 dsl_prop_set_sync_impl(ds, name,
1006 source, 1, strlen(value) + 1, value, tx);
1007 } else if (nvpair_type(pair) == DATA_TYPE_UINT64) {
1008 uint64_t intval = fnvpair_value_uint64(pair);
1009 dsl_prop_set_sync_impl(ds, name,
1010 source, sizeof (intval), 1, &intval, tx);
1011 } else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) {
1012 dsl_prop_set_sync_impl(ds, name,
1013 source, 0, 0, NULL, tx);
1014 } else {
1015 panic("invalid nvpair type");
1016 }
1017 }
1018 }
1019
1020 void
dsl_props_set_sync(void * arg,dmu_tx_t * tx)1021 dsl_props_set_sync(void *arg, dmu_tx_t *tx)
1022 {
1023 dsl_props_set_arg_t *dpsa = arg;
1024 dsl_pool_t *dp = dmu_tx_pool(tx);
1025 dsl_dataset_t *ds;
1026
1027 VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds));
1028 dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx);
1029 dsl_dataset_rele(ds, FTAG);
1030 }
1031
1032 /*
1033 * All-or-nothing; if any prop can't be set, nothing will be modified.
1034 */
1035 int
dsl_props_set(const char * dsname,zprop_source_t source,nvlist_t * props)1036 dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props)
1037 {
1038 dsl_props_set_arg_t dpsa;
1039 int nblks = 0;
1040
1041 dpsa.dpsa_dsname = dsname;
1042 dpsa.dpsa_source = source;
1043 dpsa.dpsa_props = props;
1044
1045 /*
1046 * If the source includes NONE, then we will only be removing entries
1047 * from the ZAP object. In that case don't check for ENOSPC.
1048 */
1049 if ((source & ZPROP_SRC_NONE) == 0)
1050 nblks = 2 * fnvlist_num_pairs(props);
1051
1052 return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync,
1053 &dpsa, nblks, ZFS_SPACE_CHECK_RESERVED));
1054 }
1055
1056 typedef enum dsl_prop_getflags {
1057 DSL_PROP_GET_INHERITING = 0x1, /* searching parent of target ds */
1058 DSL_PROP_GET_SNAPSHOT = 0x2, /* snapshot dataset */
1059 DSL_PROP_GET_LOCAL = 0x4, /* local properties */
1060 DSL_PROP_GET_RECEIVED = 0x8, /* received properties */
1061 } dsl_prop_getflags_t;
1062
1063 static int
dsl_prop_get_all_impl(objset_t * mos,uint64_t propobj,const char * setpoint,dsl_prop_getflags_t flags,nvlist_t * nv)1064 dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj,
1065 const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv)
1066 {
1067 zap_cursor_t zc;
1068 zap_attribute_t *za = zap_attribute_alloc();
1069 int err = 0;
1070
1071 for (zap_cursor_init(&zc, mos, propobj);
1072 (err = zap_cursor_retrieve(&zc, za)) == 0;
1073 zap_cursor_advance(&zc)) {
1074 nvlist_t *propval;
1075 zfs_prop_t prop;
1076 char buf[ZAP_MAXNAMELEN];
1077 char *valstr;
1078 const char *suffix;
1079 const char *propname;
1080 const char *source;
1081
1082 suffix = strchr(za->za_name, '$');
1083
1084 if (suffix == NULL) {
1085 /*
1086 * Skip local properties if we only want received
1087 * properties.
1088 */
1089 if (flags & DSL_PROP_GET_RECEIVED)
1090 continue;
1091
1092 propname = za->za_name;
1093 source = setpoint;
1094
1095 /* Skip if iuv entries are preset. */
1096 valstr = kmem_asprintf("%s%s", propname,
1097 ZPROP_IUV_SUFFIX);
1098 err = zap_contains(mos, propobj, valstr);
1099 kmem_strfree(valstr);
1100 if (err == 0)
1101 continue;
1102 } else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) {
1103 /* Skip explicitly inherited entries. */
1104 continue;
1105 } else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) {
1106 if (flags & DSL_PROP_GET_LOCAL)
1107 continue;
1108
1109 (void) strlcpy(buf, za->za_name,
1110 MIN(sizeof (buf), suffix - za->za_name + 1));
1111 propname = buf;
1112
1113 if (!(flags & DSL_PROP_GET_RECEIVED)) {
1114 /* Skip if locally overridden. */
1115 err = zap_contains(mos, propobj, propname);
1116 if (err == 0)
1117 continue;
1118 if (err != ENOENT)
1119 break;
1120
1121 /* Skip if explicitly inherited. */
1122 valstr = kmem_asprintf("%s%s", propname,
1123 ZPROP_INHERIT_SUFFIX);
1124 err = zap_contains(mos, propobj, valstr);
1125 kmem_strfree(valstr);
1126 if (err == 0)
1127 continue;
1128 if (err != ENOENT)
1129 break;
1130 }
1131
1132 source = ((flags & DSL_PROP_GET_INHERITING) ?
1133 setpoint : ZPROP_SOURCE_VAL_RECVD);
1134 } else if (strcmp(suffix, ZPROP_IUV_SUFFIX) == 0) {
1135 (void) strlcpy(buf, za->za_name,
1136 MIN(sizeof (buf), suffix - za->za_name + 1));
1137 propname = buf;
1138 source = setpoint;
1139 prop = zfs_name_to_prop(propname);
1140
1141 if (dsl_prop_known_index(prop,
1142 za->za_first_integer) != 1)
1143 continue;
1144 } else {
1145 /*
1146 * For backward compatibility, skip suffixes we don't
1147 * recognize.
1148 */
1149 continue;
1150 }
1151
1152 prop = zfs_name_to_prop(propname);
1153
1154 /* Skip non-inheritable properties. */
1155 if ((flags & DSL_PROP_GET_INHERITING) &&
1156 prop != ZPROP_USERPROP && !zfs_prop_inheritable(prop))
1157 continue;
1158
1159 /* Skip properties not valid for this type. */
1160 if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_USERPROP &&
1161 !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT, B_FALSE))
1162 continue;
1163
1164 /* Skip properties already defined. */
1165 if (nvlist_exists(nv, propname))
1166 continue;
1167
1168 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1169 if (za->za_integer_length == 1) {
1170 /*
1171 * String property
1172 */
1173 char *tmp = kmem_alloc(za->za_num_integers,
1174 KM_SLEEP);
1175 err = zap_lookup(mos, propobj,
1176 za->za_name, 1, za->za_num_integers, tmp);
1177 if (err != 0) {
1178 kmem_free(tmp, za->za_num_integers);
1179 break;
1180 }
1181 VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
1182 tmp) == 0);
1183 kmem_free(tmp, za->za_num_integers);
1184 } else {
1185 /*
1186 * Integer property
1187 */
1188 ASSERT(za->za_integer_length == 8);
1189 (void) nvlist_add_uint64(propval, ZPROP_VALUE,
1190 za->za_first_integer);
1191 }
1192
1193 VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0);
1194 VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1195 nvlist_free(propval);
1196 }
1197 zap_cursor_fini(&zc);
1198 zap_attribute_free(za);
1199 if (err == ENOENT)
1200 err = 0;
1201 return (err);
1202 }
1203
1204 /*
1205 * Iterate over all properties for this dataset and return them in an nvlist.
1206 */
1207 static int
dsl_prop_get_all_ds(dsl_dataset_t * ds,nvlist_t ** nvp,dsl_prop_getflags_t flags)1208 dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp,
1209 dsl_prop_getflags_t flags)
1210 {
1211 dsl_dir_t *dd = ds->ds_dir;
1212 dsl_pool_t *dp = dd->dd_pool;
1213 objset_t *mos = dp->dp_meta_objset;
1214 int err = 0;
1215 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
1216
1217 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1218
1219 if (ds->ds_is_snapshot)
1220 flags |= DSL_PROP_GET_SNAPSHOT;
1221
1222 ASSERT(dsl_pool_config_held(dp));
1223
1224 if (dsl_dataset_phys(ds)->ds_props_obj != 0) {
1225 ASSERT(flags & DSL_PROP_GET_SNAPSHOT);
1226 dsl_dataset_name(ds, setpoint);
1227 err = dsl_prop_get_all_impl(mos,
1228 dsl_dataset_phys(ds)->ds_props_obj, setpoint, flags, *nvp);
1229 if (err)
1230 goto out;
1231 }
1232
1233 for (; dd != NULL; dd = dd->dd_parent) {
1234 if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) {
1235 if (flags & (DSL_PROP_GET_LOCAL |
1236 DSL_PROP_GET_RECEIVED))
1237 break;
1238 flags |= DSL_PROP_GET_INHERITING;
1239 }
1240 dsl_dir_name(dd, setpoint);
1241 err = dsl_prop_get_all_impl(mos,
1242 dsl_dir_phys(dd)->dd_props_zapobj, setpoint, flags, *nvp);
1243 if (err)
1244 break;
1245 }
1246
1247 out:
1248 if (err) {
1249 nvlist_free(*nvp);
1250 *nvp = NULL;
1251 }
1252 return (err);
1253 }
1254
1255 boolean_t
dsl_prop_get_hasrecvd(const char * dsname)1256 dsl_prop_get_hasrecvd(const char *dsname)
1257 {
1258 uint64_t dummy;
1259
1260 return (0 ==
1261 dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL));
1262 }
1263
1264 static int
dsl_prop_set_hasrecvd_impl(const char * dsname,zprop_source_t source)1265 dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source)
1266 {
1267 uint64_t version;
1268 spa_t *spa;
1269 int error = 0;
1270
1271 VERIFY0(spa_open(dsname, &spa, FTAG));
1272 version = spa_version(spa);
1273 spa_close(spa, FTAG);
1274
1275 if (version >= SPA_VERSION_RECVD_PROPS)
1276 error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0);
1277 return (error);
1278 }
1279
1280 /*
1281 * Call after successfully receiving properties to ensure that only the first
1282 * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
1283 */
1284 int
dsl_prop_set_hasrecvd(const char * dsname)1285 dsl_prop_set_hasrecvd(const char *dsname)
1286 {
1287 int error = 0;
1288 if (!dsl_prop_get_hasrecvd(dsname))
1289 error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL);
1290 return (error);
1291 }
1292
1293 void
dsl_prop_unset_hasrecvd(const char * dsname)1294 dsl_prop_unset_hasrecvd(const char *dsname)
1295 {
1296 VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE));
1297 }
1298
1299 int
dsl_prop_get_all(objset_t * os,nvlist_t ** nvp)1300 dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
1301 {
1302 return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0));
1303 }
1304
1305 int
dsl_prop_get_received(const char * dsname,nvlist_t ** nvp)1306 dsl_prop_get_received(const char *dsname, nvlist_t **nvp)
1307 {
1308 objset_t *os;
1309 int error;
1310
1311 /*
1312 * Received properties are not distinguishable from local properties
1313 * until the dataset has received properties on or after
1314 * SPA_VERSION_RECVD_PROPS.
1315 */
1316 dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ?
1317 DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL);
1318
1319 error = dmu_objset_hold(dsname, FTAG, &os);
1320 if (error != 0)
1321 return (error);
1322 error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags);
1323 dmu_objset_rele(os, FTAG);
1324 return (error);
1325 }
1326
1327 void
dsl_prop_nvlist_add_uint64(nvlist_t * nv,zfs_prop_t prop,uint64_t value)1328 dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
1329 {
1330 nvlist_t *propval;
1331 const char *propname = zfs_prop_to_name(prop);
1332 uint64_t default_value;
1333
1334 if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
1335 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
1336 return;
1337 }
1338
1339 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1340 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
1341 /* Indicate the default source if we can. */
1342 if (dodefault(prop, 8, 1, &default_value) == 0 &&
1343 value == default_value) {
1344 VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0);
1345 }
1346 VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1347 nvlist_free(propval);
1348 }
1349
1350 void
dsl_prop_nvlist_add_string(nvlist_t * nv,zfs_prop_t prop,const char * value)1351 dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
1352 {
1353 nvlist_t *propval;
1354 const char *propname = zfs_prop_to_name(prop);
1355
1356 if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
1357 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
1358 return;
1359 }
1360
1361 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1362 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
1363 VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1364 nvlist_free(propval);
1365 }
1366
1367 #if defined(_KERNEL)
1368 EXPORT_SYMBOL(dsl_prop_register);
1369 EXPORT_SYMBOL(dsl_prop_unregister);
1370 EXPORT_SYMBOL(dsl_prop_unregister_all);
1371 EXPORT_SYMBOL(dsl_prop_get);
1372 EXPORT_SYMBOL(dsl_prop_get_integer);
1373 EXPORT_SYMBOL(dsl_prop_get_all);
1374 EXPORT_SYMBOL(dsl_prop_get_received);
1375 EXPORT_SYMBOL(dsl_prop_get_ds);
1376 EXPORT_SYMBOL(dsl_prop_get_int_ds);
1377 EXPORT_SYMBOL(dsl_prop_get_dd);
1378 EXPORT_SYMBOL(dsl_props_set);
1379 EXPORT_SYMBOL(dsl_prop_set_int);
1380 EXPORT_SYMBOL(dsl_prop_set_string);
1381 EXPORT_SYMBOL(dsl_prop_inherit);
1382 EXPORT_SYMBOL(dsl_prop_predict);
1383 EXPORT_SYMBOL(dsl_prop_nvlist_add_uint64);
1384 EXPORT_SYMBOL(dsl_prop_nvlist_add_string);
1385 #endif
1386