1 /* 2 * CDDL HEADER START 3 * 4 * This file and its contents are supplied under the terms of the 5 * Common Development and Distribution License ("CDDL"), version 1.0. 6 * You may only use this file in accordance with the terms of version 7 * 1.0 of the CDDL. 8 * 9 * A full copy of the text of the CDDL should have accompanied this 10 * source. A copy of the CDDL is also available via the Internet at 11 * http://www.illumos.org/license/CDDL. 12 * 13 * CDDL HEADER END 14 */ 15 16 /* 17 * Copyright (c) 2016 by Delphix. All rights reserved. 18 */ 19 20 #include <sys/lua/lua.h> 21 #include <sys/lua/lualib.h> 22 #include <sys/lua/lauxlib.h> 23 24 #include <zfs_prop.h> 25 26 #include <sys/dsl_prop.h> 27 #include <sys/dsl_synctask.h> 28 #include <sys/dsl_dataset.h> 29 #include <sys/dsl_dir.h> 30 #include <sys/dmu_objset.h> 31 #include <sys/mntent.h> 32 #include <sys/sunddi.h> 33 #include <sys/zap.h> 34 #include <sys/zcp.h> 35 #include <sys/zcp_iter.h> 36 #include <sys/zcp_global.h> 37 #include <sys/zcp_prop.h> 38 #include <sys/zfs_ioctl.h> 39 #include <sys/zfs_znode.h> 40 #include <sys/zvol.h> 41 42 #ifdef _KERNEL 43 #include <sys/zfs_quota.h> 44 #include <sys/zfs_vfsops.h> 45 #endif 46 47 static int 48 get_objset_type(dsl_dataset_t *ds, zfs_type_t *type) 49 { 50 int error; 51 objset_t *os; 52 error = dmu_objset_from_ds(ds, &os); 53 if (error != 0) 54 return (error); 55 if (ds->ds_is_snapshot) { 56 *type = ZFS_TYPE_SNAPSHOT; 57 } else { 58 switch (os->os_phys->os_type) { 59 case DMU_OST_ZFS: 60 *type = ZFS_TYPE_FILESYSTEM; 61 break; 62 case DMU_OST_ZVOL: 63 *type = ZFS_TYPE_VOLUME; 64 break; 65 default: 66 return (EINVAL); 67 } 68 } 69 return (0); 70 } 71 72 /* 73 * Returns the string name of ds's type in str (a buffer which should be 74 * at least 12 bytes long). 75 */ 76 static int 77 get_objset_type_name(dsl_dataset_t *ds, char *str) 78 { 79 zfs_type_t type = ZFS_TYPE_INVALID; 80 int error = get_objset_type(ds, &type); 81 if (error != 0) 82 return (error); 83 switch (type) { 84 case ZFS_TYPE_SNAPSHOT: 85 (void) strlcpy(str, "snapshot", ZAP_MAXVALUELEN); 86 break; 87 case ZFS_TYPE_FILESYSTEM: 88 (void) strlcpy(str, "filesystem", ZAP_MAXVALUELEN); 89 break; 90 case ZFS_TYPE_VOLUME: 91 (void) strlcpy(str, "volume", ZAP_MAXVALUELEN); 92 break; 93 default: 94 return (EINVAL); 95 } 96 return (0); 97 } 98 99 /* 100 * Determines the source of a property given its setpoint and 101 * property type. It pushes the source to the lua stack. 102 */ 103 static void 104 get_prop_src(lua_State *state, const char *setpoint, zfs_prop_t prop) 105 { 106 if (zfs_prop_readonly(prop) || (prop == ZFS_PROP_VERSION)) { 107 lua_pushnil(state); 108 } else { 109 const char *src; 110 if (strcmp("", setpoint) == 0) { 111 src = "default"; 112 } else { 113 src = setpoint; 114 } 115 (void) lua_pushstring(state, src); 116 } 117 } 118 119 /* 120 * Given an error encountered while getting properties, either longjmp's for 121 * a fatal error or pushes nothing to the stack for a non fatal one. 122 */ 123 static int 124 zcp_handle_error(lua_State *state, const char *dataset_name, 125 const char *property_name, int error) 126 { 127 ASSERT3S(error, !=, 0); 128 if (error == ENOENT) { 129 return (0); 130 } else if (error == EINVAL) { 131 return (luaL_error(state, 132 "property '%s' is not a valid property on dataset '%s'", 133 property_name, dataset_name)); 134 } else if (error == EIO) { 135 return (luaL_error(state, 136 "I/O error while retrieving property '%s' on dataset '%s'", 137 property_name, dataset_name)); 138 } else { 139 return (luaL_error(state, "unexpected error %d while " 140 "retrieving property '%s' on dataset '%s'", 141 error, property_name, dataset_name)); 142 } 143 } 144 145 /* 146 * Look up a user defined property in the zap object. If it exists, push it 147 * and the setpoint onto the stack, otherwise don't push anything. 148 */ 149 static int 150 zcp_get_user_prop(lua_State *state, dsl_pool_t *dp, const char *dataset_name, 151 const char *property_name) 152 { 153 int error; 154 char *buf; 155 char setpoint[ZFS_MAX_DATASET_NAME_LEN]; 156 /* 157 * zcp_dataset_hold will either successfully return the requested 158 * dataset or throw a lua error and longjmp out of the zfs.get_prop call 159 * without returning. 160 */ 161 dsl_dataset_t *ds = zcp_dataset_hold(state, dp, dataset_name, FTAG); 162 if (ds == NULL) 163 return (1); /* not reached; zcp_dataset_hold() longjmp'd */ 164 165 buf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP); 166 error = dsl_prop_get_ds(ds, property_name, 1, ZAP_MAXVALUELEN, 167 buf, setpoint); 168 dsl_dataset_rele(ds, FTAG); 169 170 if (error != 0) { 171 kmem_free(buf, ZAP_MAXVALUELEN); 172 return (zcp_handle_error(state, dataset_name, property_name, 173 error)); 174 } 175 (void) lua_pushstring(state, buf); 176 (void) lua_pushstring(state, setpoint); 177 kmem_free(buf, ZAP_MAXVALUELEN); 178 return (2); 179 } 180 181 /* 182 * Check if the property we're looking for is stored in the ds_dir. If so, 183 * return it in the 'val' argument. Return 0 on success and ENOENT and if 184 * the property is not present. 185 */ 186 static int 187 get_dsl_dir_prop(dsl_dataset_t *ds, zfs_prop_t zfs_prop, 188 uint64_t *val) 189 { 190 dsl_dir_t *dd = ds->ds_dir; 191 mutex_enter(&dd->dd_lock); 192 switch (zfs_prop) { 193 case ZFS_PROP_USEDSNAP: 194 *val = dsl_dir_get_usedsnap(dd); 195 break; 196 case ZFS_PROP_USEDCHILD: 197 *val = dsl_dir_get_usedchild(dd); 198 break; 199 case ZFS_PROP_USEDDS: 200 *val = dsl_dir_get_usedds(dd); 201 break; 202 case ZFS_PROP_USEDREFRESERV: 203 *val = dsl_dir_get_usedrefreserv(dd); 204 break; 205 case ZFS_PROP_LOGICALUSED: 206 *val = dsl_dir_get_logicalused(dd); 207 break; 208 default: 209 mutex_exit(&dd->dd_lock); 210 return (SET_ERROR(ENOENT)); 211 } 212 mutex_exit(&dd->dd_lock); 213 return (0); 214 } 215 216 /* 217 * Check if the property we're looking for is stored at the dsl_dataset or 218 * dsl_dir level. If so, push the property value and source onto the lua stack 219 * and return 0. If it is not present or a failure occurs in lookup, return a 220 * non-zero error value. 221 */ 222 static int 223 get_special_prop(lua_State *state, dsl_dataset_t *ds, const char *dsname, 224 zfs_prop_t zfs_prop) 225 { 226 int error = 0; 227 objset_t *os; 228 uint64_t numval = 0; 229 char *strval = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP); 230 char setpoint[ZFS_MAX_DATASET_NAME_LEN] = 231 "Internal error - setpoint not determined"; 232 zfs_type_t ds_type = ZFS_TYPE_INVALID; 233 zprop_type_t prop_type = zfs_prop_get_type(zfs_prop); 234 (void) get_objset_type(ds, &ds_type); 235 236 switch (zfs_prop) { 237 case ZFS_PROP_REFRATIO: 238 numval = dsl_get_refratio(ds); 239 break; 240 case ZFS_PROP_USED: 241 numval = dsl_get_used(ds); 242 break; 243 case ZFS_PROP_CLONES: { 244 nvlist_t *clones = fnvlist_alloc(); 245 error = get_clones_stat_impl(ds, clones); 246 if (error == 0) { 247 /* push list to lua stack */ 248 VERIFY0(zcp_nvlist_to_lua(state, clones, NULL, 0ULL)); 249 /* source */ 250 (void) lua_pushnil(state); 251 } 252 nvlist_free(clones); 253 kmem_free(strval, ZAP_MAXVALUELEN); 254 return (error); 255 } 256 case ZFS_PROP_COMPRESSRATIO: 257 numval = dsl_get_compressratio(ds); 258 break; 259 case ZFS_PROP_CREATION: 260 numval = dsl_get_creation(ds); 261 break; 262 case ZFS_PROP_REFERENCED: 263 numval = dsl_get_referenced(ds); 264 break; 265 case ZFS_PROP_AVAILABLE: 266 numval = dsl_get_available(ds); 267 break; 268 case ZFS_PROP_LOGICALREFERENCED: 269 numval = dsl_get_logicalreferenced(ds); 270 break; 271 case ZFS_PROP_CREATETXG: 272 numval = dsl_get_creationtxg(ds); 273 break; 274 case ZFS_PROP_GUID: 275 numval = dsl_get_guid(ds); 276 break; 277 case ZFS_PROP_UNIQUE: 278 numval = dsl_get_unique(ds); 279 break; 280 case ZFS_PROP_OBJSETID: 281 numval = dsl_get_objsetid(ds); 282 break; 283 case ZFS_PROP_ORIGIN: 284 dsl_dir_get_origin(ds->ds_dir, strval); 285 break; 286 case ZFS_PROP_USERACCOUNTING: 287 error = dmu_objset_from_ds(ds, &os); 288 if (error == 0) 289 numval = dmu_objset_userspace_present(os); 290 break; 291 case ZFS_PROP_WRITTEN: 292 error = dsl_get_written(ds, &numval); 293 break; 294 case ZFS_PROP_TYPE: 295 error = get_objset_type_name(ds, strval); 296 break; 297 case ZFS_PROP_PREV_SNAP: 298 error = dsl_get_prev_snap(ds, strval); 299 break; 300 case ZFS_PROP_NAME: 301 dsl_dataset_name(ds, strval); 302 break; 303 case ZFS_PROP_MOUNTPOINT: 304 error = dsl_get_mountpoint(ds, dsname, strval, setpoint); 305 break; 306 case ZFS_PROP_VERSION: 307 /* should be a snapshot or filesystem */ 308 ASSERT(ds_type != ZFS_TYPE_VOLUME); 309 error = dmu_objset_from_ds(ds, &os); 310 /* look in the master node for the version */ 311 if (error == 0) { 312 error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 313 sizeof (numval), 1, &numval); 314 } 315 break; 316 case ZFS_PROP_DEFER_DESTROY: 317 numval = dsl_get_defer_destroy(ds); 318 break; 319 case ZFS_PROP_USERREFS: 320 numval = dsl_get_userrefs(ds); 321 break; 322 case ZFS_PROP_FILESYSTEM_COUNT: 323 error = dsl_dir_get_filesystem_count(ds->ds_dir, &numval); 324 (void) strlcpy(setpoint, "", ZFS_MAX_DATASET_NAME_LEN); 325 break; 326 case ZFS_PROP_SNAPSHOT_COUNT: 327 error = dsl_dir_get_snapshot_count(ds->ds_dir, &numval); 328 (void) strlcpy(setpoint, "", ZFS_MAX_DATASET_NAME_LEN); 329 break; 330 case ZFS_PROP_NUMCLONES: 331 numval = dsl_get_numclones(ds); 332 break; 333 case ZFS_PROP_INCONSISTENT: 334 numval = dsl_get_inconsistent(ds); 335 break; 336 case ZFS_PROP_IVSET_GUID: 337 if (dsl_dataset_is_zapified(ds)) { 338 error = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, 339 ds->ds_object, DS_FIELD_IVSET_GUID, 340 sizeof (numval), 1, &numval); 341 } else { 342 error = ENOENT; 343 } 344 break; 345 case ZFS_PROP_RECEIVE_RESUME_TOKEN: { 346 char *token = get_receive_resume_token(ds); 347 if (token != NULL) { 348 (void) strlcpy(strval, token, ZAP_MAXVALUELEN); 349 kmem_strfree(token); 350 } else { 351 error = ENOENT; 352 } 353 break; 354 } 355 case ZFS_PROP_VOLSIZE: 356 ASSERT(ds_type == ZFS_TYPE_VOLUME || 357 ds_type == ZFS_TYPE_SNAPSHOT); 358 error = dmu_objset_from_ds(ds, &os); 359 if (error == 0) { 360 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 361 sizeof (numval), 1, &numval); 362 } 363 if (error == 0) 364 (void) strlcpy(setpoint, dsname, 365 ZFS_MAX_DATASET_NAME_LEN); 366 367 break; 368 case ZFS_PROP_VOLBLOCKSIZE: { 369 ASSERT(ds_type == ZFS_TYPE_VOLUME); 370 dmu_object_info_t doi; 371 error = dmu_objset_from_ds(ds, &os); 372 if (error == 0) { 373 error = dmu_object_info(os, ZVOL_OBJ, &doi); 374 if (error == 0) 375 numval = doi.doi_data_block_size; 376 } 377 break; 378 } 379 380 case ZFS_PROP_KEYSTATUS: 381 case ZFS_PROP_KEYFORMAT: { 382 /* provide defaults in case no crypto obj exists */ 383 setpoint[0] = '\0'; 384 if (zfs_prop == ZFS_PROP_KEYSTATUS) 385 numval = ZFS_KEYSTATUS_NONE; 386 else 387 numval = ZFS_KEYFORMAT_NONE; 388 389 nvlist_t *nvl, *propval; 390 nvl = fnvlist_alloc(); 391 dsl_dataset_crypt_stats(ds, nvl); 392 if (nvlist_lookup_nvlist(nvl, zfs_prop_to_name(zfs_prop), 393 &propval) == 0) { 394 char *source; 395 396 (void) nvlist_lookup_uint64(propval, ZPROP_VALUE, 397 &numval); 398 if (nvlist_lookup_string(propval, ZPROP_SOURCE, 399 &source) == 0) 400 strlcpy(setpoint, source, sizeof (setpoint)); 401 } 402 nvlist_free(nvl); 403 break; 404 } 405 406 default: 407 /* Did not match these props, check in the dsl_dir */ 408 error = get_dsl_dir_prop(ds, zfs_prop, &numval); 409 } 410 if (error != 0) { 411 kmem_free(strval, ZAP_MAXVALUELEN); 412 return (error); 413 } 414 415 switch (prop_type) { 416 case PROP_TYPE_NUMBER: { 417 (void) lua_pushnumber(state, numval); 418 break; 419 } 420 case PROP_TYPE_STRING: { 421 (void) lua_pushstring(state, strval); 422 break; 423 } 424 case PROP_TYPE_INDEX: { 425 const char *propval; 426 error = zfs_prop_index_to_string(zfs_prop, numval, &propval); 427 if (error != 0) { 428 kmem_free(strval, ZAP_MAXVALUELEN); 429 return (error); 430 } 431 (void) lua_pushstring(state, propval); 432 break; 433 } 434 } 435 kmem_free(strval, ZAP_MAXVALUELEN); 436 437 /* Push the source to the stack */ 438 get_prop_src(state, setpoint, zfs_prop); 439 return (0); 440 } 441 442 /* 443 * Look up a property and its source in the zap object. If the value is 444 * present and successfully retrieved, push the value and source on the 445 * lua stack and return 0. On failure, return a non-zero error value. 446 */ 447 static int 448 get_zap_prop(lua_State *state, dsl_dataset_t *ds, zfs_prop_t zfs_prop) 449 { 450 int error = 0; 451 char setpoint[ZFS_MAX_DATASET_NAME_LEN]; 452 char *strval = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP); 453 uint64_t numval; 454 const char *prop_name = zfs_prop_to_name(zfs_prop); 455 zprop_type_t prop_type = zfs_prop_get_type(zfs_prop); 456 457 if (prop_type == PROP_TYPE_STRING) { 458 /* Push value to lua stack */ 459 error = dsl_prop_get_ds(ds, prop_name, 1, 460 ZAP_MAXVALUELEN, strval, setpoint); 461 if (error == 0) 462 (void) lua_pushstring(state, strval); 463 } else { 464 error = dsl_prop_get_ds(ds, prop_name, sizeof (numval), 465 1, &numval, setpoint); 466 467 #ifdef _KERNEL 468 /* Fill in temporary value for prop, if applicable */ 469 (void) zfs_get_temporary_prop(ds, zfs_prop, &numval, setpoint); 470 #else 471 return (luaL_error(state, 472 "temporary properties only supported in kernel mode", 473 prop_name)); 474 #endif 475 /* Push value to lua stack */ 476 if (prop_type == PROP_TYPE_INDEX) { 477 const char *propval; 478 error = zfs_prop_index_to_string(zfs_prop, numval, 479 &propval); 480 if (error == 0) 481 (void) lua_pushstring(state, propval); 482 } else { 483 if (error == 0) 484 (void) lua_pushnumber(state, numval); 485 } 486 } 487 kmem_free(strval, ZAP_MAXVALUELEN); 488 if (error == 0) 489 get_prop_src(state, setpoint, zfs_prop); 490 return (error); 491 } 492 493 /* 494 * Determine whether property is valid for a given dataset 495 */ 496 boolean_t 497 prop_valid_for_ds(dsl_dataset_t *ds, zfs_prop_t zfs_prop) 498 { 499 zfs_type_t zfs_type = ZFS_TYPE_INVALID; 500 501 /* properties not supported */ 502 if ((zfs_prop == ZFS_PROP_ISCSIOPTIONS) || 503 (zfs_prop == ZFS_PROP_MOUNTED)) 504 return (B_FALSE); 505 506 /* if we want the origin prop, ds must be a clone */ 507 if ((zfs_prop == ZFS_PROP_ORIGIN) && (!dsl_dir_is_clone(ds->ds_dir))) 508 return (B_FALSE); 509 510 int error = get_objset_type(ds, &zfs_type); 511 if (error != 0) 512 return (B_FALSE); 513 return (zfs_prop_valid_for_type(zfs_prop, zfs_type, B_FALSE)); 514 } 515 516 /* 517 * Look up a given dataset property. On success return 2, the number of 518 * values pushed to the lua stack (property value and source). On a fatal 519 * error, longjmp. On a non fatal error push nothing. 520 */ 521 static int 522 zcp_get_system_prop(lua_State *state, dsl_pool_t *dp, const char *dataset_name, 523 zfs_prop_t zfs_prop) 524 { 525 int error; 526 /* 527 * zcp_dataset_hold will either successfully return the requested 528 * dataset or throw a lua error and longjmp out of the zfs.get_prop call 529 * without returning. 530 */ 531 dsl_dataset_t *ds = zcp_dataset_hold(state, dp, dataset_name, FTAG); 532 if (ds == NULL) 533 return (1); /* not reached; zcp_dataset_hold() longjmp'd */ 534 535 /* Check that the property is valid for the given dataset */ 536 const char *prop_name = zfs_prop_to_name(zfs_prop); 537 if (!prop_valid_for_ds(ds, zfs_prop)) { 538 dsl_dataset_rele(ds, FTAG); 539 return (0); 540 } 541 542 /* Check if the property can be accessed directly */ 543 error = get_special_prop(state, ds, dataset_name, zfs_prop); 544 if (error == 0) { 545 dsl_dataset_rele(ds, FTAG); 546 /* The value and source have been pushed by get_special_prop */ 547 return (2); 548 } 549 if (error != ENOENT) { 550 dsl_dataset_rele(ds, FTAG); 551 return (zcp_handle_error(state, dataset_name, 552 prop_name, error)); 553 } 554 555 /* If we were unable to find it, look in the zap object */ 556 error = get_zap_prop(state, ds, zfs_prop); 557 dsl_dataset_rele(ds, FTAG); 558 if (error != 0) { 559 return (zcp_handle_error(state, dataset_name, 560 prop_name, error)); 561 } 562 /* The value and source have been pushed by get_zap_prop */ 563 return (2); 564 } 565 566 #ifdef _KERNEL 567 static zfs_userquota_prop_t 568 get_userquota_prop(const char *prop_name) 569 { 570 zfs_userquota_prop_t type; 571 /* Figure out the property type ({user|group}{quota|used}) */ 572 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 573 if (strncmp(prop_name, zfs_userquota_prop_prefixes[type], 574 strlen(zfs_userquota_prop_prefixes[type])) == 0) 575 break; 576 } 577 return (type); 578 } 579 580 /* 581 * Given the name of a zfs_userquota_prop, this function determines the 582 * prop type as well as the numeric group/user ids based on the string 583 * following the '@' in the property name. On success, returns 0. On failure, 584 * returns a non-zero error. 585 * 'domain' must be free'd by caller using kmem_strfree() 586 */ 587 static int 588 parse_userquota_prop(const char *prop_name, zfs_userquota_prop_t *type, 589 char **domain, uint64_t *rid) 590 { 591 char *cp, *end, *domain_val; 592 593 *type = get_userquota_prop(prop_name); 594 if (*type >= ZFS_NUM_USERQUOTA_PROPS) 595 return (EINVAL); 596 597 *rid = 0; 598 cp = strchr(prop_name, '@') + 1; 599 if (strncmp(cp, "S-1-", 4) == 0) { 600 /* 601 * It's a numeric SID (eg "S-1-234-567-89") and we want to 602 * separate the domain id and the rid 603 */ 604 int domain_len = strrchr(cp, '-') - cp; 605 domain_val = kmem_alloc(domain_len + 1, KM_SLEEP); 606 (void) strncpy(domain_val, cp, domain_len); 607 domain_val[domain_len] = '\0'; 608 cp += domain_len + 1; 609 610 (void) ddi_strtoll(cp, &end, 10, (longlong_t *)rid); 611 if (*end != '\0') { 612 kmem_strfree(domain_val); 613 return (EINVAL); 614 } 615 } else { 616 /* It's only a user/group ID (eg "12345"), just get the rid */ 617 domain_val = NULL; 618 (void) ddi_strtoll(cp, &end, 10, (longlong_t *)rid); 619 if (*end != '\0') 620 return (EINVAL); 621 } 622 *domain = domain_val; 623 return (0); 624 } 625 626 /* 627 * Look up {user|group}{quota|used} property for given dataset. On success 628 * push the value (quota or used amount) and the setpoint. On failure, push 629 * a lua error. 630 */ 631 static int 632 zcp_get_userquota_prop(lua_State *state, dsl_pool_t *dp, 633 const char *dataset_name, const char *prop_name) 634 { 635 zfsvfs_t *zfvp; 636 zfsvfs_t *zfsvfs; 637 int error; 638 zfs_userquota_prop_t type; 639 char *domain; 640 uint64_t rid, value = 0; 641 objset_t *os; 642 643 dsl_dataset_t *ds = zcp_dataset_hold(state, dp, dataset_name, FTAG); 644 if (ds == NULL) 645 return (1); /* not reached; zcp_dataset_hold() longjmp'd */ 646 647 error = parse_userquota_prop(prop_name, &type, &domain, &rid); 648 if (error == 0) { 649 error = dmu_objset_from_ds(ds, &os); 650 if (error == 0) { 651 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP); 652 error = zfsvfs_create_impl(&zfvp, zfsvfs, os); 653 if (error == 0) { 654 error = zfs_userspace_one(zfvp, type, domain, 655 rid, &value); 656 zfsvfs_free(zfvp); 657 } 658 } 659 if (domain != NULL) 660 kmem_strfree(domain); 661 } 662 dsl_dataset_rele(ds, FTAG); 663 664 if ((value == 0) && ((type == ZFS_PROP_USERQUOTA) || 665 (type == ZFS_PROP_GROUPQUOTA))) 666 error = SET_ERROR(ENOENT); 667 if (error != 0) { 668 return (zcp_handle_error(state, dataset_name, 669 prop_name, error)); 670 } 671 672 (void) lua_pushnumber(state, value); 673 (void) lua_pushstring(state, dataset_name); 674 return (2); 675 } 676 #endif 677 678 /* 679 * Determines the name of the snapshot referenced in the written property 680 * name. Returns snapshot name in snap_name, a buffer that must be at least 681 * as large as ZFS_MAX_DATASET_NAME_LEN 682 */ 683 static void 684 parse_written_prop(const char *dataset_name, const char *prop_name, 685 char *snap_name) 686 { 687 ASSERT(zfs_prop_written(prop_name)); 688 const char *name = prop_name + ZFS_WRITTEN_PROP_PREFIX_LEN; 689 if (strchr(name, '@') == NULL) { 690 (void) snprintf(snap_name, ZFS_MAX_DATASET_NAME_LEN, "%s@%s", 691 dataset_name, name); 692 } else { 693 (void) strlcpy(snap_name, name, ZFS_MAX_DATASET_NAME_LEN); 694 } 695 } 696 697 /* 698 * Look up written@ property for given dataset. On success 699 * push the value and the setpoint. If error is fatal, we will 700 * longjmp, otherwise push nothing. 701 */ 702 static int 703 zcp_get_written_prop(lua_State *state, dsl_pool_t *dp, 704 const char *dataset_name, const char *prop_name) 705 { 706 char snap_name[ZFS_MAX_DATASET_NAME_LEN]; 707 uint64_t used, comp, uncomp; 708 dsl_dataset_t *old; 709 int error = 0; 710 711 parse_written_prop(dataset_name, prop_name, snap_name); 712 dsl_dataset_t *new = zcp_dataset_hold(state, dp, dataset_name, FTAG); 713 if (new == NULL) 714 return (1); /* not reached; zcp_dataset_hold() longjmp'd */ 715 716 error = dsl_dataset_hold(dp, snap_name, FTAG, &old); 717 if (error != 0) { 718 dsl_dataset_rele(new, FTAG); 719 return (zcp_dataset_hold_error(state, dp, snap_name, 720 error)); 721 } 722 error = dsl_dataset_space_written(old, new, 723 &used, &comp, &uncomp); 724 725 dsl_dataset_rele(old, FTAG); 726 dsl_dataset_rele(new, FTAG); 727 728 if (error != 0) { 729 return (zcp_handle_error(state, dataset_name, 730 snap_name, error)); 731 } 732 (void) lua_pushnumber(state, used); 733 (void) lua_pushstring(state, dataset_name); 734 return (2); 735 } 736 737 static int zcp_get_prop(lua_State *state); 738 static const zcp_lib_info_t zcp_get_prop_info = { 739 .name = "get_prop", 740 .func = zcp_get_prop, 741 .pargs = { 742 { .za_name = "dataset", .za_lua_type = LUA_TSTRING }, 743 { .za_name = "property", .za_lua_type = LUA_TSTRING }, 744 {NULL, 0} 745 }, 746 .kwargs = { 747 {NULL, 0} 748 } 749 }; 750 751 static int 752 zcp_get_prop(lua_State *state) 753 { 754 const char *dataset_name; 755 const char *property_name; 756 dsl_pool_t *dp = zcp_run_info(state)->zri_pool; 757 const zcp_lib_info_t *libinfo = &zcp_get_prop_info; 758 759 zcp_parse_args(state, libinfo->name, libinfo->pargs, libinfo->kwargs); 760 761 dataset_name = lua_tostring(state, 1); 762 property_name = lua_tostring(state, 2); 763 764 /* User defined property */ 765 if (zfs_prop_user(property_name)) { 766 return (zcp_get_user_prop(state, dp, 767 dataset_name, property_name)); 768 } 769 /* userspace property */ 770 if (zfs_prop_userquota(property_name)) { 771 #ifdef _KERNEL 772 return (zcp_get_userquota_prop(state, dp, 773 dataset_name, property_name)); 774 #else 775 return (luaL_error(state, 776 "user quota properties only supported in kernel mode", 777 property_name)); 778 #endif 779 } 780 /* written@ property */ 781 if (zfs_prop_written(property_name)) { 782 return (zcp_get_written_prop(state, dp, 783 dataset_name, property_name)); 784 } 785 786 zfs_prop_t zfs_prop = zfs_name_to_prop(property_name); 787 /* Valid system property */ 788 if (zfs_prop != ZPROP_INVAL) { 789 return (zcp_get_system_prop(state, dp, dataset_name, 790 zfs_prop)); 791 } 792 793 /* Invalid property name */ 794 return (luaL_error(state, 795 "'%s' is not a valid property", property_name)); 796 } 797 798 int 799 zcp_load_get_lib(lua_State *state) 800 { 801 lua_pushcclosure(state, zcp_get_prop_info.func, 0); 802 lua_setfield(state, -2, zcp_get_prop_info.name); 803 804 return (1); 805 } 806