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