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