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 http://www.opensolaris.org/os/licensing. 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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/dmu.h> 29 #include <sys/dmu_objset.h> 30 #include <sys/dmu_tx.h> 31 #include <sys/dsl_dataset.h> 32 #include <sys/dsl_dir.h> 33 #include <sys/dsl_prop.h> 34 #include <sys/dsl_synctask.h> 35 #include <sys/spa.h> 36 #include <sys/zio_checksum.h> /* for the default checksum value */ 37 #include <sys/zap.h> 38 #include <sys/fs/zfs.h> 39 40 #include "zfs_prop.h" 41 42 static int 43 dodefault(const char *propname, int intsz, int numint, void *buf) 44 { 45 zfs_prop_t prop; 46 47 /* 48 * The setonce properties are read-only, BUT they still 49 * have a default value that can be used as the initial 50 * value. 51 */ 52 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL || 53 (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop))) 54 return (ENOENT); 55 56 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) { 57 if (intsz != 1) 58 return (EOVERFLOW); 59 (void) strncpy(buf, zfs_prop_default_string(prop), 60 numint); 61 } else { 62 if (intsz != 8 || numint < 1) 63 return (EOVERFLOW); 64 65 *(uint64_t *)buf = zfs_prop_default_numeric(prop); 66 } 67 68 return (0); 69 } 70 71 static int 72 dsl_prop_get_impl(dsl_dir_t *dd, const char *propname, 73 int intsz, int numint, void *buf, char *setpoint) 74 { 75 int err = ENOENT; 76 zfs_prop_t prop; 77 78 if (setpoint) 79 setpoint[0] = '\0'; 80 81 prop = zfs_name_to_prop(propname); 82 83 /* 84 * Note: dd may be NULL, therefore we shouldn't dereference it 85 * ouside this loop. 86 */ 87 for (; dd != NULL; dd = dd->dd_parent) { 88 objset_t *mos = dd->dd_pool->dp_meta_objset; 89 ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock)); 90 err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, 91 propname, intsz, numint, buf); 92 if (err != ENOENT) { 93 if (setpoint) 94 dsl_dir_name(dd, setpoint); 95 break; 96 } 97 98 /* 99 * Break out of this loop for non-inheritable properties. 100 */ 101 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 102 break; 103 } 104 if (err == ENOENT) 105 err = dodefault(propname, intsz, numint, buf); 106 107 return (err); 108 } 109 110 /* 111 * Register interest in the named property. We'll call the callback 112 * once to notify it of the current property value, and again each time 113 * the property changes, until this callback is unregistered. 114 * 115 * Return 0 on success, errno if the prop is not an integer value. 116 */ 117 int 118 dsl_prop_register(dsl_dataset_t *ds, const char *propname, 119 dsl_prop_changed_cb_t *callback, void *cbarg) 120 { 121 dsl_dir_t *dd = ds->ds_dir; 122 uint64_t value; 123 dsl_prop_cb_record_t *cbr; 124 int err; 125 int need_rwlock; 126 127 need_rwlock = !RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock); 128 if (need_rwlock) 129 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 130 131 err = dsl_prop_get_impl(dd, propname, 8, 1, &value, NULL); 132 if (err != 0) { 133 rw_exit(&dd->dd_pool->dp_config_rwlock); 134 return (err); 135 } 136 137 cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP); 138 cbr->cbr_ds = ds; 139 cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP); 140 (void) strcpy((char *)cbr->cbr_propname, propname); 141 cbr->cbr_func = callback; 142 cbr->cbr_arg = cbarg; 143 mutex_enter(&dd->dd_lock); 144 list_insert_head(&dd->dd_prop_cbs, cbr); 145 mutex_exit(&dd->dd_lock); 146 147 cbr->cbr_func(cbr->cbr_arg, value); 148 149 VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, dd->dd_object, 150 NULL, cbr, &dd)); 151 if (need_rwlock) 152 rw_exit(&dd->dd_pool->dp_config_rwlock); 153 /* Leave dataset open until this callback is unregistered */ 154 return (0); 155 } 156 157 int 158 dsl_prop_get_ds(dsl_dir_t *dd, const char *propname, 159 int intsz, int numints, void *buf, char *setpoint) 160 { 161 int err; 162 163 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 164 err = dsl_prop_get_impl(dd, propname, intsz, numints, buf, setpoint); 165 rw_exit(&dd->dd_pool->dp_config_rwlock); 166 167 return (err); 168 } 169 170 /* 171 * Get property when config lock is already held. 172 */ 173 int dsl_prop_get_ds_locked(dsl_dir_t *dd, const char *propname, 174 int intsz, int numints, void *buf, char *setpoint) 175 { 176 ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock)); 177 return (dsl_prop_get_impl(dd, propname, intsz, numints, buf, setpoint)); 178 } 179 180 int 181 dsl_prop_get(const char *ddname, const char *propname, 182 int intsz, int numints, void *buf, char *setpoint) 183 { 184 dsl_dir_t *dd; 185 const char *tail; 186 int err; 187 188 err = dsl_dir_open(ddname, FTAG, &dd, &tail); 189 if (err) 190 return (err); 191 if (tail && tail[0] != '@') { 192 dsl_dir_close(dd, FTAG); 193 return (ENOENT); 194 } 195 196 err = dsl_prop_get_ds(dd, propname, intsz, numints, buf, setpoint); 197 198 dsl_dir_close(dd, FTAG); 199 return (err); 200 } 201 202 /* 203 * Get the current property value. It may have changed by the time this 204 * function returns, so it is NOT safe to follow up with 205 * dsl_prop_register() and assume that the value has not changed in 206 * between. 207 * 208 * Return 0 on success, ENOENT if ddname is invalid. 209 */ 210 int 211 dsl_prop_get_integer(const char *ddname, const char *propname, 212 uint64_t *valuep, char *setpoint) 213 { 214 return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint)); 215 } 216 217 /* 218 * Unregister this callback. Return 0 on success, ENOENT if ddname is 219 * invalid, ENOMSG if no matching callback registered. 220 */ 221 int 222 dsl_prop_unregister(dsl_dataset_t *ds, const char *propname, 223 dsl_prop_changed_cb_t *callback, void *cbarg) 224 { 225 dsl_dir_t *dd = ds->ds_dir; 226 dsl_prop_cb_record_t *cbr; 227 228 mutex_enter(&dd->dd_lock); 229 for (cbr = list_head(&dd->dd_prop_cbs); 230 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) { 231 if (cbr->cbr_ds == ds && 232 cbr->cbr_func == callback && 233 cbr->cbr_arg == cbarg && 234 strcmp(cbr->cbr_propname, propname) == 0) 235 break; 236 } 237 238 if (cbr == NULL) { 239 mutex_exit(&dd->dd_lock); 240 return (ENOMSG); 241 } 242 243 list_remove(&dd->dd_prop_cbs, cbr); 244 mutex_exit(&dd->dd_lock); 245 kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1); 246 kmem_free(cbr, sizeof (dsl_prop_cb_record_t)); 247 248 /* Clean up from dsl_prop_register */ 249 dsl_dir_close(dd, cbr); 250 return (0); 251 } 252 253 /* 254 * Return the number of callbacks that are registered for this dataset. 255 */ 256 int 257 dsl_prop_numcb(dsl_dataset_t *ds) 258 { 259 dsl_dir_t *dd = ds->ds_dir; 260 dsl_prop_cb_record_t *cbr; 261 int num = 0; 262 263 mutex_enter(&dd->dd_lock); 264 for (cbr = list_head(&dd->dd_prop_cbs); 265 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) { 266 if (cbr->cbr_ds == ds) 267 num++; 268 } 269 mutex_exit(&dd->dd_lock); 270 271 return (num); 272 } 273 274 static void 275 dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj, 276 const char *propname, uint64_t value, int first) 277 { 278 dsl_dir_t *dd; 279 dsl_prop_cb_record_t *cbr; 280 objset_t *mos = dp->dp_meta_objset; 281 zap_cursor_t zc; 282 zap_attribute_t za; 283 int err; 284 285 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 286 err = dsl_dir_open_obj(dp, ddobj, NULL, FTAG, &dd); 287 if (err) 288 return; 289 290 if (!first) { 291 /* 292 * If the prop is set here, then this change is not 293 * being inherited here or below; stop the recursion. 294 */ 295 err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname, 296 8, 1, &value); 297 if (err == 0) { 298 dsl_dir_close(dd, FTAG); 299 return; 300 } 301 ASSERT3U(err, ==, ENOENT); 302 } 303 304 mutex_enter(&dd->dd_lock); 305 for (cbr = list_head(&dd->dd_prop_cbs); 306 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) { 307 if (strcmp(cbr->cbr_propname, propname) == 0) { 308 cbr->cbr_func(cbr->cbr_arg, value); 309 } 310 } 311 mutex_exit(&dd->dd_lock); 312 313 for (zap_cursor_init(&zc, mos, 314 dd->dd_phys->dd_child_dir_zapobj); 315 zap_cursor_retrieve(&zc, &za) == 0; 316 zap_cursor_advance(&zc)) { 317 /* XXX recursion could blow stack; esp. za! */ 318 dsl_prop_changed_notify(dp, za.za_first_integer, 319 propname, value, FALSE); 320 } 321 zap_cursor_fini(&zc); 322 dsl_dir_close(dd, FTAG); 323 } 324 325 struct prop_set_arg { 326 const char *name; 327 int intsz; 328 int numints; 329 const void *buf; 330 }; 331 332 333 static void 334 dsl_prop_set_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 335 { 336 dsl_dir_t *dd = arg1; 337 struct prop_set_arg *psa = arg2; 338 objset_t *mos = dd->dd_pool->dp_meta_objset; 339 uint64_t zapobj = dd->dd_phys->dd_props_zapobj; 340 uint64_t intval; 341 int isint; 342 char valbuf[32]; 343 char *valstr; 344 345 isint = (dodefault(psa->name, 8, 1, &intval) == 0); 346 347 if (psa->numints == 0) { 348 int err = zap_remove(mos, zapobj, psa->name, tx); 349 ASSERT(err == 0 || err == ENOENT); 350 if (isint) { 351 VERIFY(0 == dsl_prop_get_impl(dd->dd_parent, 352 psa->name, 8, 1, &intval, NULL)); 353 } 354 } else { 355 VERIFY(0 == zap_update(mos, zapobj, psa->name, 356 psa->intsz, psa->numints, psa->buf, tx)); 357 if (isint) 358 intval = *(uint64_t *)psa->buf; 359 } 360 361 if (isint) { 362 dsl_prop_changed_notify(dd->dd_pool, 363 dd->dd_object, psa->name, intval, TRUE); 364 } 365 if (isint) { 366 (void) snprintf(valbuf, sizeof (valbuf), 367 "%lld", (longlong_t)intval); 368 valstr = valbuf; 369 } else { 370 valstr = (char *)psa->buf; 371 } 372 spa_history_internal_log((psa->numints == 0) ? LOG_DS_INHERIT : 373 LOG_DS_PROPSET, dd->dd_pool->dp_spa, tx, cr, 374 "%s=%s dataset = %llu", psa->name, valstr, 375 dd->dd_phys->dd_head_dataset_obj); 376 } 377 378 int 379 dsl_prop_set_dd(dsl_dir_t *dd, const char *propname, 380 int intsz, int numints, const void *buf) 381 { 382 struct prop_set_arg psa; 383 384 psa.name = propname; 385 psa.intsz = intsz; 386 psa.numints = numints; 387 psa.buf = buf; 388 389 return (dsl_sync_task_do(dd->dd_pool, 390 NULL, dsl_prop_set_sync, dd, &psa, 2)); 391 } 392 393 int 394 dsl_prop_set(const char *ddname, const char *propname, 395 int intsz, int numints, const void *buf) 396 { 397 dsl_dir_t *dd; 398 int err; 399 400 /* 401 * We must do these checks before we get to the syncfunc, since 402 * it can't fail. 403 */ 404 if (strlen(propname) >= ZAP_MAXNAMELEN) 405 return (ENAMETOOLONG); 406 if (intsz * numints >= ZAP_MAXVALUELEN) 407 return (E2BIG); 408 409 err = dsl_dir_open(ddname, FTAG, &dd, NULL); 410 if (err) 411 return (err); 412 err = dsl_prop_set_dd(dd, propname, intsz, numints, buf); 413 dsl_dir_close(dd, FTAG); 414 return (err); 415 } 416 417 /* 418 * Iterate over all properties for this dataset and return them in an nvlist. 419 */ 420 int 421 dsl_prop_get_all(objset_t *os, nvlist_t **nvp) 422 { 423 dsl_dataset_t *ds = os->os->os_dsl_dataset; 424 dsl_dir_t *dd = ds->ds_dir; 425 boolean_t snapshot; 426 int err = 0; 427 dsl_pool_t *dp; 428 objset_t *mos; 429 430 snapshot = dsl_dataset_is_snapshot(ds); 431 432 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 433 434 dp = dd->dd_pool; 435 mos = dp->dp_meta_objset; 436 437 rw_enter(&dp->dp_config_rwlock, RW_READER); 438 for (; dd != NULL; dd = dd->dd_parent) { 439 char setpoint[MAXNAMELEN]; 440 zap_cursor_t zc; 441 zap_attribute_t za; 442 443 dsl_dir_name(dd, setpoint); 444 445 for (zap_cursor_init(&zc, mos, dd->dd_phys->dd_props_zapobj); 446 (err = zap_cursor_retrieve(&zc, &za)) == 0; 447 zap_cursor_advance(&zc)) { 448 nvlist_t *propval; 449 zfs_prop_t prop; 450 /* 451 * Skip non-inheritable properties. 452 */ 453 if ((prop = zfs_name_to_prop(za.za_name)) != 454 ZPROP_INVAL && !zfs_prop_inheritable(prop) && 455 dd != ds->ds_dir) 456 continue; 457 458 if (snapshot && 459 !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT)) 460 continue; 461 462 if (nvlist_lookup_nvlist(*nvp, za.za_name, 463 &propval) == 0) 464 continue; 465 466 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, 467 KM_SLEEP) == 0); 468 if (za.za_integer_length == 1) { 469 /* 470 * String property 471 */ 472 char *tmp = kmem_alloc(za.za_num_integers, 473 KM_SLEEP); 474 err = zap_lookup(mos, 475 dd->dd_phys->dd_props_zapobj, 476 za.za_name, 1, za.za_num_integers, 477 tmp); 478 if (err != 0) { 479 kmem_free(tmp, za.za_num_integers); 480 break; 481 } 482 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, 483 tmp) == 0); 484 kmem_free(tmp, za.za_num_integers); 485 } else { 486 /* 487 * Integer property 488 */ 489 ASSERT(za.za_integer_length == 8); 490 (void) nvlist_add_uint64(propval, ZPROP_VALUE, 491 za.za_first_integer); 492 } 493 494 VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, 495 setpoint) == 0); 496 VERIFY(nvlist_add_nvlist(*nvp, za.za_name, 497 propval) == 0); 498 nvlist_free(propval); 499 } 500 zap_cursor_fini(&zc); 501 502 if (err != ENOENT) 503 break; 504 err = 0; 505 } 506 rw_exit(&dp->dp_config_rwlock); 507 508 return (err); 509 } 510 511 void 512 dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value) 513 { 514 nvlist_t *propval; 515 516 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 517 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0); 518 VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0); 519 nvlist_free(propval); 520 } 521 522 void 523 dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value) 524 { 525 nvlist_t *propval; 526 527 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 528 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0); 529 VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0); 530 nvlist_free(propval); 531 } 532