Print this page
3752 want more verifiable dbuf user eviction
Submitted by: Justin Gibbs <justing@spectralogic.com>
Submitted by: Will Andrews <willa@spectralogic.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/dsl_prop.c
+++ new/usr/src/uts/common/fs/zfs/dsl_prop.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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 24 */
25 25
26 26 #include <sys/zfs_context.h>
27 27 #include <sys/dmu.h>
28 28 #include <sys/dmu_objset.h>
29 29 #include <sys/dmu_tx.h>
30 30 #include <sys/dsl_dataset.h>
31 31 #include <sys/dsl_dir.h>
32 32 #include <sys/dsl_prop.h>
33 33 #include <sys/dsl_synctask.h>
34 34 #include <sys/spa.h>
35 35 #include <sys/zap.h>
36 36 #include <sys/fs/zfs.h>
37 37
38 38 #include "zfs_prop.h"
39 39
40 40 #define ZPROP_INHERIT_SUFFIX "$inherit"
41 41 #define ZPROP_RECVD_SUFFIX "$recvd"
42 42
43 43 static int
44 44 dodefault(const char *propname, int intsz, int numints, void *buf)
45 45 {
46 46 zfs_prop_t prop;
47 47
48 48 /*
49 49 * The setonce properties are read-only, BUT they still
50 50 * have a default value that can be used as the initial
51 51 * value.
52 52 */
53 53 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL ||
54 54 (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
55 55 return (SET_ERROR(ENOENT));
56 56
57 57 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
58 58 if (intsz != 1)
59 59 return (SET_ERROR(EOVERFLOW));
60 60 (void) strncpy(buf, zfs_prop_default_string(prop),
61 61 numints);
62 62 } else {
63 63 if (intsz != 8 || numints < 1)
64 64 return (SET_ERROR(EOVERFLOW));
65 65
66 66 *(uint64_t *)buf = zfs_prop_default_numeric(prop);
67 67 }
68 68
69 69 return (0);
70 70 }
71 71
72 72 int
73 73 dsl_prop_get_dd(dsl_dir_t *dd, const char *propname,
74 74 int intsz, int numints, void *buf, char *setpoint, boolean_t snapshot)
75 75 {
76 76 int err = ENOENT;
77 77 dsl_dir_t *target = dd;
78 78 objset_t *mos = dd->dd_pool->dp_meta_objset;
79 79 zfs_prop_t prop;
80 80 boolean_t inheritable;
81 81 boolean_t inheriting = B_FALSE;
82 82 char *inheritstr;
83 83 char *recvdstr;
84 84
85 85 ASSERT(dsl_pool_config_held(dd->dd_pool));
86 86
87 87 if (setpoint)
88 88 setpoint[0] = '\0';
89 89
90 90 prop = zfs_name_to_prop(propname);
91 91 inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
92 92 inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
93 93 recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
94 94
95 95 /*
96 96 * Note: dd may become NULL, therefore we shouldn't dereference it
97 97 * after this loop.
98 98 */
99 99 for (; dd != NULL; dd = dd->dd_parent) {
100 100 if (dd != target || snapshot) {
101 101 if (!inheritable)
102 102 break;
103 103 inheriting = B_TRUE;
104 104 }
105 105
106 106 /* Check for a local value. */
107 107 err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname,
108 108 intsz, numints, buf);
109 109 if (err != ENOENT) {
110 110 if (setpoint != NULL && err == 0)
111 111 dsl_dir_name(dd, setpoint);
112 112 break;
113 113 }
114 114
115 115 /*
116 116 * Skip the check for a received value if there is an explicit
117 117 * inheritance entry.
118 118 */
119 119 err = zap_contains(mos, dd->dd_phys->dd_props_zapobj,
120 120 inheritstr);
121 121 if (err != 0 && err != ENOENT)
122 122 break;
123 123
124 124 if (err == ENOENT) {
125 125 /* Check for a received value. */
126 126 err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj,
127 127 recvdstr, intsz, numints, buf);
128 128 if (err != ENOENT) {
129 129 if (setpoint != NULL && err == 0) {
130 130 if (inheriting) {
131 131 dsl_dir_name(dd, setpoint);
132 132 } else {
133 133 (void) strcpy(setpoint,
134 134 ZPROP_SOURCE_VAL_RECVD);
135 135 }
136 136 }
137 137 break;
138 138 }
139 139 }
140 140
141 141 /*
142 142 * If we found an explicit inheritance entry, err is zero even
143 143 * though we haven't yet found the value, so reinitializing err
144 144 * at the end of the loop (instead of at the beginning) ensures
145 145 * that err has a valid post-loop value.
146 146 */
147 147 err = SET_ERROR(ENOENT);
148 148 }
149 149
150 150 if (err == ENOENT)
151 151 err = dodefault(propname, intsz, numints, buf);
152 152
153 153 strfree(inheritstr);
154 154 strfree(recvdstr);
155 155
156 156 return (err);
157 157 }
158 158
159 159 int
↓ open down ↓ |
159 lines elided |
↑ open up ↑ |
160 160 dsl_prop_get_ds(dsl_dataset_t *ds, const char *propname,
161 161 int intsz, int numints, void *buf, char *setpoint)
162 162 {
163 163 zfs_prop_t prop = zfs_name_to_prop(propname);
164 164 boolean_t inheritable;
165 165 boolean_t snapshot;
166 166 uint64_t zapobj;
167 167
168 168 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
169 169 inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
170 - snapshot = (ds->ds_phys != NULL && dsl_dataset_is_snapshot(ds));
171 - zapobj = (ds->ds_phys == NULL ? 0 : ds->ds_phys->ds_props_obj);
170 + snapshot = (DS_HAS_PHYS(ds) && dsl_dataset_is_snapshot(ds));
171 + zapobj = (DS_HAS_PHYS(ds) ? ds->ds_phys->ds_props_obj : 0);
172 172
173 173 if (zapobj != 0) {
174 174 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
175 175 int err;
176 176
177 177 ASSERT(snapshot);
178 178
179 179 /* Check for a local value. */
180 180 err = zap_lookup(mos, zapobj, propname, intsz, numints, buf);
181 181 if (err != ENOENT) {
182 182 if (setpoint != NULL && err == 0)
183 183 dsl_dataset_name(ds, setpoint);
184 184 return (err);
185 185 }
186 186
187 187 /*
188 188 * Skip the check for a received value if there is an explicit
189 189 * inheritance entry.
190 190 */
191 191 if (inheritable) {
192 192 char *inheritstr = kmem_asprintf("%s%s", propname,
193 193 ZPROP_INHERIT_SUFFIX);
194 194 err = zap_contains(mos, zapobj, inheritstr);
195 195 strfree(inheritstr);
196 196 if (err != 0 && err != ENOENT)
197 197 return (err);
198 198 }
199 199
200 200 if (err == ENOENT) {
201 201 /* Check for a received value. */
202 202 char *recvdstr = kmem_asprintf("%s%s", propname,
203 203 ZPROP_RECVD_SUFFIX);
204 204 err = zap_lookup(mos, zapobj, recvdstr,
205 205 intsz, numints, buf);
206 206 strfree(recvdstr);
207 207 if (err != ENOENT) {
208 208 if (setpoint != NULL && err == 0)
209 209 (void) strcpy(setpoint,
210 210 ZPROP_SOURCE_VAL_RECVD);
211 211 return (err);
212 212 }
213 213 }
214 214 }
215 215
216 216 return (dsl_prop_get_dd(ds->ds_dir, propname,
217 217 intsz, numints, buf, setpoint, snapshot));
218 218 }
219 219
220 220 /*
221 221 * Register interest in the named property. We'll call the callback
222 222 * once to notify it of the current property value, and again each time
223 223 * the property changes, until this callback is unregistered.
224 224 *
225 225 * Return 0 on success, errno if the prop is not an integer value.
226 226 */
227 227 int
228 228 dsl_prop_register(dsl_dataset_t *ds, const char *propname,
229 229 dsl_prop_changed_cb_t *callback, void *cbarg)
230 230 {
231 231 dsl_dir_t *dd = ds->ds_dir;
232 232 dsl_pool_t *dp = dd->dd_pool;
233 233 uint64_t value;
234 234 dsl_prop_cb_record_t *cbr;
235 235 int err;
236 236
237 237 ASSERT(dsl_pool_config_held(dp));
238 238
239 239 err = dsl_prop_get_int_ds(ds, propname, &value);
240 240 if (err != 0)
241 241 return (err);
242 242
243 243 cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
244 244 cbr->cbr_ds = ds;
245 245 cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
246 246 (void) strcpy((char *)cbr->cbr_propname, propname);
247 247 cbr->cbr_func = callback;
248 248 cbr->cbr_arg = cbarg;
249 249 mutex_enter(&dd->dd_lock);
250 250 list_insert_head(&dd->dd_prop_cbs, cbr);
251 251 mutex_exit(&dd->dd_lock);
252 252
253 253 cbr->cbr_func(cbr->cbr_arg, value);
254 254 return (0);
255 255 }
256 256
257 257 int
258 258 dsl_prop_get(const char *dsname, const char *propname,
259 259 int intsz, int numints, void *buf, char *setpoint)
260 260 {
261 261 objset_t *os;
262 262 int error;
263 263
264 264 error = dmu_objset_hold(dsname, FTAG, &os);
265 265 if (error != 0)
266 266 return (error);
267 267
268 268 error = dsl_prop_get_ds(dmu_objset_ds(os), propname,
269 269 intsz, numints, buf, setpoint);
270 270
271 271 dmu_objset_rele(os, FTAG);
272 272 return (error);
273 273 }
274 274
275 275 /*
276 276 * Get the current property value. It may have changed by the time this
277 277 * function returns, so it is NOT safe to follow up with
278 278 * dsl_prop_register() and assume that the value has not changed in
279 279 * between.
280 280 *
281 281 * Return 0 on success, ENOENT if ddname is invalid.
282 282 */
283 283 int
284 284 dsl_prop_get_integer(const char *ddname, const char *propname,
285 285 uint64_t *valuep, char *setpoint)
286 286 {
287 287 return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
288 288 }
289 289
290 290 int
291 291 dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname,
292 292 uint64_t *valuep)
293 293 {
294 294 return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL));
295 295 }
296 296
297 297 /*
298 298 * Predict the effective value of the given special property if it were set with
299 299 * the given value and source. This is not a general purpose function. It exists
300 300 * only to handle the special requirements of the quota and reservation
301 301 * properties. The fact that these properties are non-inheritable greatly
302 302 * simplifies the prediction logic.
303 303 *
304 304 * Returns 0 on success, a positive error code on failure, or -1 if called with
305 305 * a property not handled by this function.
306 306 */
307 307 int
308 308 dsl_prop_predict(dsl_dir_t *dd, const char *propname,
309 309 zprop_source_t source, uint64_t value, uint64_t *newvalp)
310 310 {
311 311 zfs_prop_t prop = zfs_name_to_prop(propname);
312 312 objset_t *mos;
313 313 uint64_t zapobj;
314 314 uint64_t version;
315 315 char *recvdstr;
316 316 int err = 0;
317 317
318 318 switch (prop) {
319 319 case ZFS_PROP_QUOTA:
320 320 case ZFS_PROP_RESERVATION:
321 321 case ZFS_PROP_REFQUOTA:
322 322 case ZFS_PROP_REFRESERVATION:
323 323 break;
324 324 default:
325 325 return (-1);
326 326 }
327 327
328 328 mos = dd->dd_pool->dp_meta_objset;
329 329 zapobj = dd->dd_phys->dd_props_zapobj;
330 330 recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
331 331
332 332 version = spa_version(dd->dd_pool->dp_spa);
333 333 if (version < SPA_VERSION_RECVD_PROPS) {
334 334 if (source & ZPROP_SRC_NONE)
335 335 source = ZPROP_SRC_NONE;
336 336 else if (source & ZPROP_SRC_RECEIVED)
337 337 source = ZPROP_SRC_LOCAL;
338 338 }
339 339
340 340 switch (source) {
341 341 case ZPROP_SRC_NONE:
342 342 /* Revert to the received value, if any. */
343 343 err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp);
344 344 if (err == ENOENT)
345 345 *newvalp = 0;
346 346 break;
347 347 case ZPROP_SRC_LOCAL:
348 348 *newvalp = value;
349 349 break;
350 350 case ZPROP_SRC_RECEIVED:
351 351 /*
352 352 * If there's no local setting, then the new received value will
353 353 * be the effective value.
354 354 */
355 355 err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
356 356 if (err == ENOENT)
357 357 *newvalp = value;
358 358 break;
359 359 case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
360 360 /*
361 361 * We're clearing the received value, so the local setting (if
362 362 * it exists) remains the effective value.
363 363 */
364 364 err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
365 365 if (err == ENOENT)
366 366 *newvalp = 0;
367 367 break;
368 368 default:
369 369 panic("unexpected property source: %d", source);
370 370 }
371 371
372 372 strfree(recvdstr);
373 373
374 374 if (err == ENOENT)
375 375 return (0);
376 376
377 377 return (err);
378 378 }
379 379
380 380 /*
381 381 * Unregister this callback. Return 0 on success, ENOENT if ddname is
382 382 * invalid, ENOMSG if no matching callback registered.
383 383 */
384 384 int
385 385 dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
386 386 dsl_prop_changed_cb_t *callback, void *cbarg)
387 387 {
388 388 dsl_dir_t *dd = ds->ds_dir;
389 389 dsl_prop_cb_record_t *cbr;
390 390
391 391 mutex_enter(&dd->dd_lock);
392 392 for (cbr = list_head(&dd->dd_prop_cbs);
393 393 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
394 394 if (cbr->cbr_ds == ds &&
395 395 cbr->cbr_func == callback &&
396 396 cbr->cbr_arg == cbarg &&
397 397 strcmp(cbr->cbr_propname, propname) == 0)
398 398 break;
399 399 }
400 400
401 401 if (cbr == NULL) {
402 402 mutex_exit(&dd->dd_lock);
403 403 return (SET_ERROR(ENOMSG));
404 404 }
405 405
406 406 list_remove(&dd->dd_prop_cbs, cbr);
407 407 mutex_exit(&dd->dd_lock);
408 408 kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
409 409 kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
410 410
411 411 return (0);
412 412 }
413 413
414 414 boolean_t
415 415 dsl_prop_hascb(dsl_dataset_t *ds)
416 416 {
417 417 dsl_dir_t *dd = ds->ds_dir;
418 418 boolean_t rv = B_FALSE;
419 419 dsl_prop_cb_record_t *cbr;
420 420
421 421 mutex_enter(&dd->dd_lock);
422 422 for (cbr = list_head(&dd->dd_prop_cbs); cbr;
423 423 cbr = list_next(&dd->dd_prop_cbs, cbr)) {
424 424 if (cbr->cbr_ds == ds) {
425 425 rv = B_TRUE;
426 426 break;
427 427 }
428 428 }
429 429 mutex_exit(&dd->dd_lock);
430 430 return (rv);
431 431 }
432 432
433 433 /* ARGSUSED */
434 434 static int
435 435 dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
436 436 {
437 437 dsl_dir_t *dd = ds->ds_dir;
438 438 dsl_prop_cb_record_t *cbr;
439 439
440 440 mutex_enter(&dd->dd_lock);
441 441 for (cbr = list_head(&dd->dd_prop_cbs); cbr;
442 442 cbr = list_next(&dd->dd_prop_cbs, cbr)) {
443 443 uint64_t value;
444 444
445 445 if (dsl_prop_get_ds(cbr->cbr_ds, cbr->cbr_propname,
446 446 sizeof (value), 1, &value, NULL) == 0)
447 447 cbr->cbr_func(cbr->cbr_arg, value);
448 448 }
449 449 mutex_exit(&dd->dd_lock);
450 450
451 451 return (0);
452 452 }
453 453
454 454 /*
455 455 * Update all property values for ddobj & its descendants. This is used
456 456 * when renaming the dir.
457 457 */
458 458 void
459 459 dsl_prop_notify_all(dsl_dir_t *dd)
460 460 {
461 461 dsl_pool_t *dp = dd->dd_pool;
462 462 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
463 463 (void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb,
464 464 NULL, DS_FIND_CHILDREN);
465 465 }
466 466
467 467 static void
468 468 dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
469 469 const char *propname, uint64_t value, int first)
470 470 {
471 471 dsl_dir_t *dd;
472 472 dsl_prop_cb_record_t *cbr;
473 473 objset_t *mos = dp->dp_meta_objset;
474 474 zap_cursor_t zc;
475 475 zap_attribute_t *za;
476 476 int err;
477 477
478 478 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
479 479 err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
480 480 if (err)
481 481 return;
482 482
483 483 if (!first) {
484 484 /*
485 485 * If the prop is set here, then this change is not
486 486 * being inherited here or below; stop the recursion.
487 487 */
488 488 err = zap_contains(mos, dd->dd_phys->dd_props_zapobj, propname);
489 489 if (err == 0) {
490 490 dsl_dir_rele(dd, FTAG);
491 491 return;
492 492 }
493 493 ASSERT3U(err, ==, ENOENT);
494 494 }
495 495
496 496 mutex_enter(&dd->dd_lock);
497 497 for (cbr = list_head(&dd->dd_prop_cbs); cbr;
498 498 cbr = list_next(&dd->dd_prop_cbs, cbr)) {
499 499 uint64_t propobj = cbr->cbr_ds->ds_phys->ds_props_obj;
500 500
501 501 if (strcmp(cbr->cbr_propname, propname) != 0)
502 502 continue;
503 503
504 504 /*
505 505 * If the property is set on this ds, then it is not
506 506 * inherited here; don't call the callback.
507 507 */
508 508 if (propobj && 0 == zap_contains(mos, propobj, propname))
509 509 continue;
510 510
511 511 cbr->cbr_func(cbr->cbr_arg, value);
512 512 }
513 513 mutex_exit(&dd->dd_lock);
514 514
515 515 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
516 516 for (zap_cursor_init(&zc, mos,
517 517 dd->dd_phys->dd_child_dir_zapobj);
518 518 zap_cursor_retrieve(&zc, za) == 0;
519 519 zap_cursor_advance(&zc)) {
520 520 dsl_prop_changed_notify(dp, za->za_first_integer,
521 521 propname, value, FALSE);
522 522 }
523 523 kmem_free(za, sizeof (zap_attribute_t));
524 524 zap_cursor_fini(&zc);
525 525 dsl_dir_rele(dd, FTAG);
526 526 }
527 527
528 528 void
529 529 dsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname,
530 530 zprop_source_t source, int intsz, int numints, const void *value,
531 531 dmu_tx_t *tx)
532 532 {
533 533 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
534 534 uint64_t zapobj, intval, dummy;
535 535 int isint;
↓ open down ↓ |
354 lines elided |
↑ open up ↑ |
536 536 char valbuf[32];
537 537 const char *valstr = NULL;
538 538 char *inheritstr;
539 539 char *recvdstr;
540 540 char *tbuf = NULL;
541 541 int err;
542 542 uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa);
543 543
544 544 isint = (dodefault(propname, 8, 1, &intval) == 0);
545 545
546 - if (ds->ds_phys != NULL && dsl_dataset_is_snapshot(ds)) {
546 + if (DS_HAS_PHYS(ds) && dsl_dataset_is_snapshot(ds)) {
547 547 ASSERT(version >= SPA_VERSION_SNAP_PROPS);
548 548 if (ds->ds_phys->ds_props_obj == 0) {
549 549 dmu_buf_will_dirty(ds->ds_dbuf, tx);
550 550 ds->ds_phys->ds_props_obj =
551 551 zap_create(mos,
552 552 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
553 553 }
554 554 zapobj = ds->ds_phys->ds_props_obj;
555 555 } else {
556 556 zapobj = ds->ds_dir->dd_phys->dd_props_zapobj;
557 557 }
558 558
559 559 if (version < SPA_VERSION_RECVD_PROPS) {
560 560 zfs_prop_t prop = zfs_name_to_prop(propname);
561 561 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION)
562 562 return;
563 563
564 564 if (source & ZPROP_SRC_NONE)
565 565 source = ZPROP_SRC_NONE;
566 566 else if (source & ZPROP_SRC_RECEIVED)
567 567 source = ZPROP_SRC_LOCAL;
568 568 }
569 569
570 570 inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
571 571 recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
572 572
573 573 switch (source) {
574 574 case ZPROP_SRC_NONE:
575 575 /*
576 576 * revert to received value, if any (inherit -S)
577 577 * - remove propname
578 578 * - remove propname$inherit
579 579 */
580 580 err = zap_remove(mos, zapobj, propname, tx);
581 581 ASSERT(err == 0 || err == ENOENT);
582 582 err = zap_remove(mos, zapobj, inheritstr, tx);
583 583 ASSERT(err == 0 || err == ENOENT);
584 584 break;
585 585 case ZPROP_SRC_LOCAL:
586 586 /*
587 587 * remove propname$inherit
588 588 * set propname -> value
589 589 */
590 590 err = zap_remove(mos, zapobj, inheritstr, tx);
591 591 ASSERT(err == 0 || err == ENOENT);
592 592 VERIFY0(zap_update(mos, zapobj, propname,
593 593 intsz, numints, value, tx));
594 594 break;
595 595 case ZPROP_SRC_INHERITED:
596 596 /*
597 597 * explicitly inherit
598 598 * - remove propname
599 599 * - set propname$inherit
600 600 */
601 601 err = zap_remove(mos, zapobj, propname, tx);
602 602 ASSERT(err == 0 || err == ENOENT);
603 603 if (version >= SPA_VERSION_RECVD_PROPS &&
604 604 dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) {
605 605 dummy = 0;
606 606 VERIFY0(zap_update(mos, zapobj, inheritstr,
607 607 8, 1, &dummy, tx));
608 608 }
609 609 break;
610 610 case ZPROP_SRC_RECEIVED:
611 611 /*
612 612 * set propname$recvd -> value
613 613 */
614 614 err = zap_update(mos, zapobj, recvdstr,
615 615 intsz, numints, value, tx);
616 616 ASSERT(err == 0);
617 617 break;
618 618 case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED):
619 619 /*
620 620 * clear local and received settings
621 621 * - remove propname
622 622 * - remove propname$inherit
623 623 * - remove propname$recvd
624 624 */
625 625 err = zap_remove(mos, zapobj, propname, tx);
626 626 ASSERT(err == 0 || err == ENOENT);
627 627 err = zap_remove(mos, zapobj, inheritstr, tx);
628 628 ASSERT(err == 0 || err == ENOENT);
629 629 /* FALLTHRU */
630 630 case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
631 631 /*
632 632 * remove propname$recvd
633 633 */
634 634 err = zap_remove(mos, zapobj, recvdstr, tx);
635 635 ASSERT(err == 0 || err == ENOENT);
636 636 break;
↓ open down ↓ |
80 lines elided |
↑ open up ↑ |
637 637 default:
638 638 cmn_err(CE_PANIC, "unexpected property source: %d", source);
639 639 }
640 640
641 641 strfree(inheritstr);
642 642 strfree(recvdstr);
643 643
644 644 if (isint) {
645 645 VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval));
646 646
647 - if (ds->ds_phys != NULL && dsl_dataset_is_snapshot(ds)) {
647 + if (DS_HAS_PHYS(ds) && dsl_dataset_is_snapshot(ds)) {
648 648 dsl_prop_cb_record_t *cbr;
649 649 /*
650 650 * It's a snapshot; nothing can inherit this
651 651 * property, so just look for callbacks on this
652 652 * ds here.
653 653 */
654 654 mutex_enter(&ds->ds_dir->dd_lock);
655 655 for (cbr = list_head(&ds->ds_dir->dd_prop_cbs); cbr;
656 656 cbr = list_next(&ds->ds_dir->dd_prop_cbs, cbr)) {
657 657 if (cbr->cbr_ds == ds &&
658 658 strcmp(cbr->cbr_propname, propname) == 0)
659 659 cbr->cbr_func(cbr->cbr_arg, intval);
660 660 }
661 661 mutex_exit(&ds->ds_dir->dd_lock);
662 662 } else {
663 663 dsl_prop_changed_notify(ds->ds_dir->dd_pool,
664 664 ds->ds_dir->dd_object, propname, intval, TRUE);
665 665 }
666 666
667 667 (void) snprintf(valbuf, sizeof (valbuf),
668 668 "%lld", (longlong_t)intval);
669 669 valstr = valbuf;
670 670 } else {
671 671 if (source == ZPROP_SRC_LOCAL) {
672 672 valstr = value;
673 673 } else {
674 674 tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
675 675 if (dsl_prop_get_ds(ds, propname, 1,
676 676 ZAP_MAXVALUELEN, tbuf, NULL) == 0)
677 677 valstr = tbuf;
678 678 }
679 679 }
680 680
681 681 spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE ||
682 682 source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx,
683 683 "%s=%s", propname, (valstr == NULL ? "" : valstr));
684 684
685 685 if (tbuf != NULL)
686 686 kmem_free(tbuf, ZAP_MAXVALUELEN);
687 687 }
688 688
689 689 int
690 690 dsl_prop_set_int(const char *dsname, const char *propname,
691 691 zprop_source_t source, uint64_t value)
692 692 {
693 693 nvlist_t *nvl = fnvlist_alloc();
694 694 int error;
695 695
696 696 fnvlist_add_uint64(nvl, propname, value);
697 697 error = dsl_props_set(dsname, source, nvl);
698 698 fnvlist_free(nvl);
699 699 return (error);
700 700 }
701 701
702 702 int
703 703 dsl_prop_set_string(const char *dsname, const char *propname,
704 704 zprop_source_t source, const char *value)
705 705 {
706 706 nvlist_t *nvl = fnvlist_alloc();
707 707 int error;
708 708
709 709 fnvlist_add_string(nvl, propname, value);
710 710 error = dsl_props_set(dsname, source, nvl);
711 711 fnvlist_free(nvl);
712 712 return (error);
713 713 }
714 714
715 715 int
716 716 dsl_prop_inherit(const char *dsname, const char *propname,
717 717 zprop_source_t source)
718 718 {
719 719 nvlist_t *nvl = fnvlist_alloc();
720 720 int error;
721 721
722 722 fnvlist_add_boolean(nvl, propname);
723 723 error = dsl_props_set(dsname, source, nvl);
724 724 fnvlist_free(nvl);
725 725 return (error);
726 726 }
727 727
728 728 typedef struct dsl_props_set_arg {
729 729 const char *dpsa_dsname;
730 730 zprop_source_t dpsa_source;
731 731 nvlist_t *dpsa_props;
732 732 } dsl_props_set_arg_t;
733 733
734 734 static int
735 735 dsl_props_set_check(void *arg, dmu_tx_t *tx)
736 736 {
737 737 dsl_props_set_arg_t *dpsa = arg;
738 738 dsl_pool_t *dp = dmu_tx_pool(tx);
739 739 dsl_dataset_t *ds;
740 740 uint64_t version;
741 741 nvpair_t *elem = NULL;
742 742 int err;
743 743
744 744 err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds);
745 745 if (err != 0)
746 746 return (err);
747 747
748 748 version = spa_version(ds->ds_dir->dd_pool->dp_spa);
749 749 while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) {
750 750 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
751 751 dsl_dataset_rele(ds, FTAG);
752 752 return (SET_ERROR(ENAMETOOLONG));
753 753 }
754 754 if (nvpair_type(elem) == DATA_TYPE_STRING) {
755 755 char *valstr = fnvpair_value_string(elem);
756 756 if (strlen(valstr) >= (version <
757 757 SPA_VERSION_STMF_PROP ?
758 758 ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) {
759 759 dsl_dataset_rele(ds, FTAG);
760 760 return (E2BIG);
761 761 }
762 762 }
763 763 }
764 764
765 765 if (dsl_dataset_is_snapshot(ds) && version < SPA_VERSION_SNAP_PROPS) {
766 766 dsl_dataset_rele(ds, FTAG);
767 767 return (SET_ERROR(ENOTSUP));
768 768 }
769 769 dsl_dataset_rele(ds, FTAG);
770 770 return (0);
771 771 }
772 772
773 773 void
774 774 dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source,
775 775 nvlist_t *props, dmu_tx_t *tx)
776 776 {
777 777 nvpair_t *elem = NULL;
778 778
779 779 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
780 780 nvpair_t *pair = elem;
781 781
782 782 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
783 783 /*
784 784 * dsl_prop_get_all_impl() returns properties in this
785 785 * format.
786 786 */
787 787 nvlist_t *attrs = fnvpair_value_nvlist(pair);
788 788 pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
789 789 }
790 790
791 791 if (nvpair_type(pair) == DATA_TYPE_STRING) {
792 792 const char *value = fnvpair_value_string(pair);
793 793 dsl_prop_set_sync_impl(ds, nvpair_name(pair),
794 794 source, 1, strlen(value) + 1, value, tx);
795 795 } else if (nvpair_type(pair) == DATA_TYPE_UINT64) {
796 796 uint64_t intval = fnvpair_value_uint64(pair);
797 797 dsl_prop_set_sync_impl(ds, nvpair_name(pair),
798 798 source, sizeof (intval), 1, &intval, tx);
799 799 } else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) {
800 800 dsl_prop_set_sync_impl(ds, nvpair_name(pair),
801 801 source, 0, 0, NULL, tx);
802 802 } else {
803 803 panic("invalid nvpair type");
804 804 }
805 805 }
806 806 }
807 807
808 808 static void
809 809 dsl_props_set_sync(void *arg, dmu_tx_t *tx)
810 810 {
811 811 dsl_props_set_arg_t *dpsa = arg;
812 812 dsl_pool_t *dp = dmu_tx_pool(tx);
813 813 dsl_dataset_t *ds;
814 814
815 815 VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds));
816 816 dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx);
817 817 dsl_dataset_rele(ds, FTAG);
818 818 }
819 819
820 820 /*
821 821 * All-or-nothing; if any prop can't be set, nothing will be modified.
822 822 */
823 823 int
824 824 dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props)
825 825 {
826 826 dsl_props_set_arg_t dpsa;
827 827 int nblks = 0;
828 828
829 829 dpsa.dpsa_dsname = dsname;
830 830 dpsa.dpsa_source = source;
831 831 dpsa.dpsa_props = props;
832 832
833 833 /*
834 834 * If the source includes NONE, then we will only be removing entries
835 835 * from the ZAP object. In that case don't check for ENOSPC.
836 836 */
837 837 if ((source & ZPROP_SRC_NONE) == 0)
838 838 nblks = 2 * fnvlist_num_pairs(props);
839 839
840 840 return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync,
841 841 &dpsa, nblks));
842 842 }
843 843
844 844 typedef enum dsl_prop_getflags {
845 845 DSL_PROP_GET_INHERITING = 0x1, /* searching parent of target ds */
846 846 DSL_PROP_GET_SNAPSHOT = 0x2, /* snapshot dataset */
847 847 DSL_PROP_GET_LOCAL = 0x4, /* local properties */
848 848 DSL_PROP_GET_RECEIVED = 0x8 /* received properties */
849 849 } dsl_prop_getflags_t;
850 850
851 851 static int
852 852 dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj,
853 853 const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv)
854 854 {
855 855 zap_cursor_t zc;
856 856 zap_attribute_t za;
857 857 int err = 0;
858 858
859 859 for (zap_cursor_init(&zc, mos, propobj);
860 860 (err = zap_cursor_retrieve(&zc, &za)) == 0;
861 861 zap_cursor_advance(&zc)) {
862 862 nvlist_t *propval;
863 863 zfs_prop_t prop;
864 864 char buf[ZAP_MAXNAMELEN];
865 865 char *valstr;
866 866 const char *suffix;
867 867 const char *propname;
868 868 const char *source;
869 869
870 870 suffix = strchr(za.za_name, '$');
871 871
872 872 if (suffix == NULL) {
873 873 /*
874 874 * Skip local properties if we only want received
875 875 * properties.
876 876 */
877 877 if (flags & DSL_PROP_GET_RECEIVED)
878 878 continue;
879 879
880 880 propname = za.za_name;
881 881 source = setpoint;
882 882 } else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) {
883 883 /* Skip explicitly inherited entries. */
884 884 continue;
885 885 } else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) {
886 886 if (flags & DSL_PROP_GET_LOCAL)
887 887 continue;
888 888
889 889 (void) strncpy(buf, za.za_name, (suffix - za.za_name));
890 890 buf[suffix - za.za_name] = '\0';
891 891 propname = buf;
892 892
893 893 if (!(flags & DSL_PROP_GET_RECEIVED)) {
894 894 /* Skip if locally overridden. */
895 895 err = zap_contains(mos, propobj, propname);
896 896 if (err == 0)
897 897 continue;
898 898 if (err != ENOENT)
899 899 break;
900 900
901 901 /* Skip if explicitly inherited. */
902 902 valstr = kmem_asprintf("%s%s", propname,
903 903 ZPROP_INHERIT_SUFFIX);
904 904 err = zap_contains(mos, propobj, valstr);
905 905 strfree(valstr);
906 906 if (err == 0)
907 907 continue;
908 908 if (err != ENOENT)
909 909 break;
910 910 }
911 911
912 912 source = ((flags & DSL_PROP_GET_INHERITING) ?
913 913 setpoint : ZPROP_SOURCE_VAL_RECVD);
914 914 } else {
915 915 /*
916 916 * For backward compatibility, skip suffixes we don't
917 917 * recognize.
918 918 */
919 919 continue;
920 920 }
921 921
922 922 prop = zfs_name_to_prop(propname);
923 923
924 924 /* Skip non-inheritable properties. */
925 925 if ((flags & DSL_PROP_GET_INHERITING) && prop != ZPROP_INVAL &&
926 926 !zfs_prop_inheritable(prop))
927 927 continue;
928 928
929 929 /* Skip properties not valid for this type. */
930 930 if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_INVAL &&
931 931 !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT))
932 932 continue;
933 933
934 934 /* Skip properties already defined. */
935 935 if (nvlist_exists(nv, propname))
936 936 continue;
937 937
938 938 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
939 939 if (za.za_integer_length == 1) {
940 940 /*
941 941 * String property
942 942 */
943 943 char *tmp = kmem_alloc(za.za_num_integers,
944 944 KM_SLEEP);
945 945 err = zap_lookup(mos, propobj,
946 946 za.za_name, 1, za.za_num_integers, tmp);
947 947 if (err != 0) {
948 948 kmem_free(tmp, za.za_num_integers);
949 949 break;
950 950 }
951 951 VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
952 952 tmp) == 0);
953 953 kmem_free(tmp, za.za_num_integers);
954 954 } else {
955 955 /*
956 956 * Integer property
957 957 */
958 958 ASSERT(za.za_integer_length == 8);
959 959 (void) nvlist_add_uint64(propval, ZPROP_VALUE,
960 960 za.za_first_integer);
961 961 }
962 962
963 963 VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0);
964 964 VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
965 965 nvlist_free(propval);
966 966 }
967 967 zap_cursor_fini(&zc);
968 968 if (err == ENOENT)
969 969 err = 0;
970 970 return (err);
971 971 }
972 972
973 973 /*
974 974 * Iterate over all properties for this dataset and return them in an nvlist.
975 975 */
976 976 static int
977 977 dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp,
978 978 dsl_prop_getflags_t flags)
979 979 {
980 980 dsl_dir_t *dd = ds->ds_dir;
981 981 dsl_pool_t *dp = dd->dd_pool;
982 982 objset_t *mos = dp->dp_meta_objset;
983 983 int err = 0;
984 984 char setpoint[MAXNAMELEN];
985 985
986 986 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
987 987
988 988 if (dsl_dataset_is_snapshot(ds))
989 989 flags |= DSL_PROP_GET_SNAPSHOT;
990 990
991 991 ASSERT(dsl_pool_config_held(dp));
992 992
993 993 if (ds->ds_phys->ds_props_obj != 0) {
994 994 ASSERT(flags & DSL_PROP_GET_SNAPSHOT);
995 995 dsl_dataset_name(ds, setpoint);
996 996 err = dsl_prop_get_all_impl(mos, ds->ds_phys->ds_props_obj,
997 997 setpoint, flags, *nvp);
998 998 if (err)
999 999 goto out;
1000 1000 }
1001 1001
1002 1002 for (; dd != NULL; dd = dd->dd_parent) {
1003 1003 if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) {
1004 1004 if (flags & (DSL_PROP_GET_LOCAL |
1005 1005 DSL_PROP_GET_RECEIVED))
1006 1006 break;
1007 1007 flags |= DSL_PROP_GET_INHERITING;
1008 1008 }
1009 1009 dsl_dir_name(dd, setpoint);
1010 1010 err = dsl_prop_get_all_impl(mos, dd->dd_phys->dd_props_zapobj,
1011 1011 setpoint, flags, *nvp);
1012 1012 if (err)
1013 1013 break;
1014 1014 }
1015 1015 out:
1016 1016 return (err);
1017 1017 }
1018 1018
1019 1019 boolean_t
1020 1020 dsl_prop_get_hasrecvd(const char *dsname)
1021 1021 {
1022 1022 uint64_t dummy;
1023 1023
1024 1024 return (0 ==
1025 1025 dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL));
1026 1026 }
1027 1027
1028 1028 static int
1029 1029 dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source)
1030 1030 {
1031 1031 uint64_t version;
1032 1032 spa_t *spa;
1033 1033 int error = 0;
1034 1034
1035 1035 VERIFY0(spa_open(dsname, &spa, FTAG));
1036 1036 version = spa_version(spa);
1037 1037 spa_close(spa, FTAG);
1038 1038
1039 1039 if (version >= SPA_VERSION_RECVD_PROPS)
1040 1040 error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0);
1041 1041 return (error);
1042 1042 }
1043 1043
1044 1044 /*
1045 1045 * Call after successfully receiving properties to ensure that only the first
1046 1046 * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
1047 1047 */
1048 1048 int
1049 1049 dsl_prop_set_hasrecvd(const char *dsname)
1050 1050 {
1051 1051 int error = 0;
1052 1052 if (!dsl_prop_get_hasrecvd(dsname))
1053 1053 error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL);
1054 1054 return (error);
1055 1055 }
1056 1056
1057 1057 void
1058 1058 dsl_prop_unset_hasrecvd(const char *dsname)
1059 1059 {
1060 1060 VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE));
1061 1061 }
1062 1062
1063 1063 int
1064 1064 dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
1065 1065 {
1066 1066 return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0));
1067 1067 }
1068 1068
1069 1069 int
1070 1070 dsl_prop_get_received(const char *dsname, nvlist_t **nvp)
1071 1071 {
1072 1072 objset_t *os;
1073 1073 int error;
1074 1074
1075 1075 /*
1076 1076 * Received properties are not distinguishable from local properties
1077 1077 * until the dataset has received properties on or after
1078 1078 * SPA_VERSION_RECVD_PROPS.
1079 1079 */
1080 1080 dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ?
1081 1081 DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL);
1082 1082
1083 1083 error = dmu_objset_hold(dsname, FTAG, &os);
1084 1084 if (error != 0)
1085 1085 return (error);
1086 1086 error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags);
1087 1087 dmu_objset_rele(os, FTAG);
1088 1088 return (error);
1089 1089 }
1090 1090
1091 1091 void
1092 1092 dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
1093 1093 {
1094 1094 nvlist_t *propval;
1095 1095 const char *propname = zfs_prop_to_name(prop);
1096 1096 uint64_t default_value;
1097 1097
1098 1098 if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
1099 1099 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
1100 1100 return;
1101 1101 }
1102 1102
1103 1103 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1104 1104 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
1105 1105 /* Indicate the default source if we can. */
1106 1106 if (dodefault(propname, 8, 1, &default_value) == 0 &&
1107 1107 value == default_value) {
1108 1108 VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0);
1109 1109 }
1110 1110 VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1111 1111 nvlist_free(propval);
1112 1112 }
1113 1113
1114 1114 void
1115 1115 dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
1116 1116 {
1117 1117 nvlist_t *propval;
1118 1118 const char *propname = zfs_prop_to_name(prop);
1119 1119
1120 1120 if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
1121 1121 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
1122 1122 return;
1123 1123 }
1124 1124
1125 1125 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1126 1126 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
1127 1127 VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1128 1128 nvlist_free(propval);
1129 1129 }
↓ open down ↓ |
472 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX