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