xref: /illumos-gate/usr/src/uts/common/fs/zfs/zcp_get.c (revision 6bb6b5762ca4b17cd5fb3c6c123f17489d5635aa)
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 "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
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
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
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
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
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
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  * Takes a dataset, a property, a value and that value's setpoint as
217  * found in the ZAP. Checks if the property has been changed in the vfs.
218  * If so, val and setpoint will be overwritten with updated content.
219  * Otherwise, they are left unchanged.
220  */
221 static int
222 get_temporary_prop(dsl_dataset_t *ds, zfs_prop_t zfs_prop, uint64_t *val,
223     char *setpoint)
224 {
225 #ifndef _KERNEL
226 	return (0);
227 #else
228 	int error;
229 	zfsvfs_t *zfvp;
230 	vfs_t *vfsp;
231 	objset_t *os;
232 	uint64_t tmp = *val;
233 
234 	error = dmu_objset_from_ds(ds, &os);
235 	if (error != 0)
236 		return (error);
237 
238 	error = getzfsvfs_impl(os, &zfvp);
239 	if (error != 0)
240 		return (error);
241 
242 	vfsp = zfvp->z_vfs;
243 
244 	switch (zfs_prop) {
245 	case ZFS_PROP_ATIME:
246 		if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL))
247 			tmp = 0;
248 		if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL))
249 			tmp = 1;
250 		break;
251 	case ZFS_PROP_DEVICES:
252 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
253 			tmp = 0;
254 		if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL))
255 			tmp = 1;
256 		break;
257 	case ZFS_PROP_EXEC:
258 		if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
259 			tmp = 0;
260 		if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL))
261 			tmp = 1;
262 		break;
263 	case ZFS_PROP_SETUID:
264 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
265 			tmp = 0;
266 		if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL))
267 			tmp = 1;
268 		break;
269 	case ZFS_PROP_READONLY:
270 		if (vfs_optionisset(vfsp, MNTOPT_RW, NULL))
271 			tmp = 0;
272 		if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
273 			tmp = 1;
274 		break;
275 	case ZFS_PROP_XATTR:
276 		if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL))
277 			tmp = 0;
278 		if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
279 			tmp = 1;
280 		break;
281 	case ZFS_PROP_NBMAND:
282 		if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL))
283 			tmp = 0;
284 		if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
285 			tmp = 1;
286 		break;
287 	default:
288 		VFS_RELE(vfsp);
289 		return (ENOENT);
290 	}
291 
292 	VFS_RELE(vfsp);
293 	if (tmp != *val) {
294 		(void) strcpy(setpoint, "temporary");
295 		*val = tmp;
296 	}
297 	return (0);
298 #endif
299 }
300 
301 /*
302  * Check if the property we're looking for is stored at the dsl_dataset or
303  * dsl_dir level. If so, push the property value and source onto the lua stack
304  * and return 0. If it is not present or a failure occurs in lookup, return a
305  * non-zero error value.
306  */
307 static int
308 get_special_prop(lua_State *state, dsl_dataset_t *ds, const char *dsname,
309     zfs_prop_t zfs_prop)
310 {
311 	int error = 0;
312 	objset_t *os;
313 	uint64_t numval;
314 	char *strval = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
315 	char setpoint[ZFS_MAX_DATASET_NAME_LEN] =
316 	    "Internal error - setpoint not determined";
317 	zfs_type_t ds_type;
318 	zprop_type_t prop_type = zfs_prop_get_type(zfs_prop);
319 	(void) get_objset_type(ds, &ds_type);
320 
321 	switch (zfs_prop) {
322 	case ZFS_PROP_REFRATIO:
323 		numval = dsl_get_refratio(ds);
324 		break;
325 	case ZFS_PROP_USED:
326 		numval = dsl_get_used(ds);
327 		break;
328 	case ZFS_PROP_CLONES: {
329 		nvlist_t *clones = fnvlist_alloc();
330 		error = get_clones_stat_impl(ds, clones);
331 		if (error == 0) {
332 			/* push list to lua stack */
333 			VERIFY0(zcp_nvlist_to_lua(state, clones, NULL,
334 			    NULL));
335 			/* source */
336 			(void) lua_pushnil(state);
337 		}
338 		nvlist_free(clones);
339 		kmem_free(strval, ZAP_MAXVALUELEN);
340 		return (error);
341 	}
342 	case ZFS_PROP_COMPRESSRATIO:
343 		numval = dsl_get_compressratio(ds);
344 		break;
345 	case ZFS_PROP_CREATION:
346 		numval = dsl_get_creation(ds);
347 		break;
348 	case ZFS_PROP_REFERENCED:
349 		numval = dsl_get_referenced(ds);
350 		break;
351 	case ZFS_PROP_AVAILABLE:
352 		numval = dsl_get_available(ds);
353 		break;
354 	case ZFS_PROP_LOGICALREFERENCED:
355 		numval = dsl_get_logicalreferenced(ds);
356 		break;
357 	case ZFS_PROP_CREATETXG:
358 		numval = dsl_get_creationtxg(ds);
359 		break;
360 	case ZFS_PROP_GUID:
361 		numval = dsl_get_guid(ds);
362 		break;
363 	case ZFS_PROP_UNIQUE:
364 		numval = dsl_get_unique(ds);
365 		break;
366 	case ZFS_PROP_OBJSETID:
367 		numval = dsl_get_objsetid(ds);
368 		break;
369 	case ZFS_PROP_ORIGIN:
370 		dsl_dir_get_origin(ds->ds_dir, strval);
371 		break;
372 	case ZFS_PROP_USERACCOUNTING:
373 		error = dmu_objset_from_ds(ds, &os);
374 		if (error == 0)
375 			numval = dmu_objset_userspace_present(os);
376 		break;
377 	case ZFS_PROP_WRITTEN:
378 		error = dsl_get_written(ds, &numval);
379 		break;
380 	case ZFS_PROP_TYPE:
381 		error = get_objset_type_name(ds, strval);
382 		break;
383 	case ZFS_PROP_PREV_SNAP:
384 		error = dsl_get_prev_snap(ds, strval);
385 		break;
386 	case ZFS_PROP_NAME:
387 		dsl_dataset_name(ds, strval);
388 		break;
389 	case ZFS_PROP_MOUNTPOINT:
390 		error = dsl_get_mountpoint(ds, dsname, strval, setpoint);
391 		break;
392 	case ZFS_PROP_VERSION:
393 		/* should be a snapshot or filesystem */
394 		ASSERT(ds_type != ZFS_TYPE_VOLUME);
395 		error = dmu_objset_from_ds(ds, &os);
396 		/* look in the master node for the version */
397 		if (error == 0) {
398 			error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
399 			    sizeof (numval), 1, &numval);
400 		}
401 		break;
402 	case ZFS_PROP_DEFER_DESTROY:
403 		numval = dsl_get_defer_destroy(ds);
404 		break;
405 	case ZFS_PROP_USERREFS:
406 		numval = dsl_get_userrefs(ds);
407 		break;
408 	case ZFS_PROP_FILESYSTEM_COUNT:
409 		error = dsl_dir_get_filesystem_count(ds->ds_dir, &numval);
410 		(void) strcpy(setpoint, "");
411 		break;
412 	case ZFS_PROP_SNAPSHOT_COUNT:
413 		error = dsl_dir_get_snapshot_count(ds->ds_dir, &numval);
414 		(void) strcpy(setpoint, "");
415 		break;
416 	case ZFS_PROP_REMAPTXG:
417 		error = dsl_dir_get_remaptxg(ds->ds_dir, &numval);
418 		break;
419 	case ZFS_PROP_NUMCLONES:
420 		numval = dsl_get_numclones(ds);
421 		break;
422 	case ZFS_PROP_INCONSISTENT:
423 		numval = dsl_get_inconsistent(ds);
424 		break;
425 	case ZFS_PROP_RECEIVE_RESUME_TOKEN:
426 		VERIFY3U(strlcpy(strval, get_receive_resume_stats_impl(ds),
427 		    ZAP_MAXVALUELEN), <, ZAP_MAXVALUELEN);
428 		if (strcmp(strval, "") == 0) {
429 			VERIFY3U(strlcpy(strval, get_child_receive_stats(ds),
430 			    ZAP_MAXVALUELEN), <, ZAP_MAXVALUELEN);
431 			if (strcmp(strval, "") == 0)
432 				error = ENOENT;
433 		}
434 		break;
435 	case ZFS_PROP_VOLSIZE:
436 		ASSERT(ds_type == ZFS_TYPE_VOLUME);
437 		error = dmu_objset_from_ds(ds, &os);
438 		if (error == 0) {
439 			error = zap_lookup(os, ZVOL_ZAP_OBJ, "size",
440 			    sizeof (numval), 1, &numval);
441 		}
442 		if (error == 0)
443 			(void) strcpy(setpoint, dsname);
444 
445 		break;
446 	case ZFS_PROP_VOLBLOCKSIZE: {
447 		ASSERT(ds_type == ZFS_TYPE_VOLUME);
448 		dmu_object_info_t doi;
449 		error = dmu_objset_from_ds(ds, &os);
450 		if (error == 0) {
451 			error = dmu_object_info(os, ZVOL_OBJ, &doi);
452 			if (error == 0)
453 				numval = doi.doi_data_block_size;
454 		}
455 		break;
456 	}
457 	default:
458 		/* Did not match these props, check in the dsl_dir */
459 		error = get_dsl_dir_prop(ds, zfs_prop, &numval);
460 	}
461 	if (error != 0) {
462 		kmem_free(strval, ZAP_MAXVALUELEN);
463 		return (error);
464 	}
465 
466 	switch (prop_type) {
467 	case PROP_TYPE_NUMBER: {
468 		(void) lua_pushnumber(state, numval);
469 		break;
470 	}
471 	case PROP_TYPE_STRING: {
472 		(void) lua_pushstring(state, strval);
473 		break;
474 	}
475 	case PROP_TYPE_INDEX: {
476 		const char *propval;
477 		error = zfs_prop_index_to_string(zfs_prop, numval, &propval);
478 		if (error != 0) {
479 			kmem_free(strval, ZAP_MAXVALUELEN);
480 			return (error);
481 		}
482 		(void) lua_pushstring(state, propval);
483 		break;
484 	}
485 	}
486 	kmem_free(strval, ZAP_MAXVALUELEN);
487 
488 	/* Push the source to the stack */
489 	get_prop_src(state, setpoint, zfs_prop);
490 	return (0);
491 }
492 
493 /*
494  * Look up a property and its source in the zap object. If the value is
495  * present and successfully retrieved, push the value and source on the
496  * lua stack and return 0. On failure, return a non-zero error value.
497  */
498 static int
499 get_zap_prop(lua_State *state, dsl_dataset_t *ds, zfs_prop_t zfs_prop)
500 {
501 	int error = 0;
502 	char setpoint[ZFS_MAX_DATASET_NAME_LEN];
503 	char *strval = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
504 	uint64_t numval;
505 	const char *prop_name = zfs_prop_to_name(zfs_prop);
506 	zprop_type_t prop_type = zfs_prop_get_type(zfs_prop);
507 
508 	if (prop_type == PROP_TYPE_STRING) {
509 		/* Push value to lua stack */
510 		error = dsl_prop_get_ds(ds, prop_name, 1,
511 		    ZAP_MAXVALUELEN, strval, setpoint);
512 		if (error == 0)
513 			(void) lua_pushstring(state, strval);
514 	} else {
515 		error = dsl_prop_get_ds(ds, prop_name, sizeof (numval),
516 		    1, &numval, setpoint);
517 
518 		/* Fill in temorary value for prop, if applicable */
519 		(void) get_temporary_prop(ds, zfs_prop, &numval, setpoint);
520 
521 		/* Push value to lua stack */
522 		if (prop_type == PROP_TYPE_INDEX) {
523 			const char *propval;
524 			error = zfs_prop_index_to_string(zfs_prop, numval,
525 			    &propval);
526 			if (error == 0)
527 				(void) lua_pushstring(state, propval);
528 		} else {
529 			if (error == 0)
530 				(void) lua_pushnumber(state, numval);
531 		}
532 	}
533 	kmem_free(strval, ZAP_MAXVALUELEN);
534 	if (error == 0)
535 		get_prop_src(state, setpoint, zfs_prop);
536 	return (error);
537 }
538 
539 /*
540  * Determine whether property is valid for a given dataset
541  */
542 boolean_t
543 prop_valid_for_ds(dsl_dataset_t *ds, zfs_prop_t zfs_prop)
544 {
545 	int error;
546 	zfs_type_t zfs_type;
547 
548 	/* properties not supported */
549 	if ((zfs_prop == ZFS_PROP_ISCSIOPTIONS) ||
550 	    (zfs_prop == ZFS_PROP_MOUNTED))
551 		return (B_FALSE);
552 
553 	/* if we want the origin prop, ds must be a clone */
554 	if ((zfs_prop == ZFS_PROP_ORIGIN) && (!dsl_dir_is_clone(ds->ds_dir)))
555 		return (B_FALSE);
556 
557 	error = get_objset_type(ds, &zfs_type);
558 	if (error != 0)
559 		return (B_FALSE);
560 	return (zfs_prop_valid_for_type(zfs_prop, zfs_type));
561 }
562 
563 /*
564  * Look up a given dataset property. On success return 2, the number of
565  * values pushed to the lua stack (property value and source). On a fatal
566  * error, longjmp. On a non fatal error push nothing.
567  */
568 static int
569 zcp_get_system_prop(lua_State *state, dsl_pool_t *dp, const char *dataset_name,
570     zfs_prop_t zfs_prop)
571 {
572 	int error;
573 	/*
574 	 * zcp_dataset_hold will either successfully return the requested
575 	 * dataset or throw a lua error and longjmp out of the zfs.get_prop call
576 	 * without returning.
577 	 */
578 	dsl_dataset_t *ds = zcp_dataset_hold(state, dp, dataset_name, FTAG);
579 	if (ds == NULL)
580 		return (1); /* not reached; zcp_dataset_hold() longjmp'd */
581 
582 	/* Check that the property is valid for the given dataset */
583 	const char *prop_name = zfs_prop_to_name(zfs_prop);
584 	if (!prop_valid_for_ds(ds, zfs_prop)) {
585 		dsl_dataset_rele(ds, FTAG);
586 		return (0);
587 	}
588 
589 	/* Check if the property can be accessed directly */
590 	error = get_special_prop(state, ds, dataset_name, zfs_prop);
591 	if (error == 0) {
592 		dsl_dataset_rele(ds, FTAG);
593 		/* The value and source have been pushed by get_special_prop */
594 		return (2);
595 	}
596 	if (error != ENOENT) {
597 		dsl_dataset_rele(ds, FTAG);
598 		return (zcp_handle_error(state, dataset_name,
599 		    prop_name, error));
600 	}
601 
602 	/* If we were unable to find it, look in the zap object */
603 	error = get_zap_prop(state, ds, zfs_prop);
604 	dsl_dataset_rele(ds, FTAG);
605 	if (error != 0) {
606 		return (zcp_handle_error(state, dataset_name,
607 		    prop_name, error));
608 	}
609 	/* The value and source have been pushed by get_zap_prop */
610 	return (2);
611 }
612 
613 static zfs_userquota_prop_t
614 get_userquota_prop(const char *prop_name)
615 {
616 	zfs_userquota_prop_t type;
617 	/* Figure out the property type ({user|group}{quota|used}) */
618 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
619 		if (strncmp(prop_name, zfs_userquota_prop_prefixes[type],
620 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
621 			break;
622 	}
623 	return (type);
624 }
625 
626 #ifdef _KERNEL
627 /*
628  * Given the name of a zfs_userquota_prop, this function determines the
629  * prop type as well as the numeric group/user ids based on the string
630  * following the '@' in the property name. On success, returns 0. On failure,
631  * returns a non-zero error.
632  * 'domain' must be free'd by caller using strfree()
633  */
634 static int
635 parse_userquota_prop(const char *prop_name, zfs_userquota_prop_t *type,
636     char **domain, uint64_t *rid)
637 {
638 	char *cp, *end, *domain_val;
639 
640 	*type = get_userquota_prop(prop_name);
641 	if (*type >= ZFS_NUM_USERQUOTA_PROPS)
642 		return (EINVAL);
643 
644 	*rid = 0;
645 	cp = strchr(prop_name, '@') + 1;
646 	if (strncmp(cp, "S-1-", 4) == 0) {
647 		/*
648 		 * It's a numeric SID (eg "S-1-234-567-89") and we want to
649 		 * seperate the domain id and the rid
650 		 */
651 		int domain_len = strrchr(cp, '-') - cp;
652 		domain_val = kmem_alloc(domain_len + 1, KM_SLEEP);
653 		(void) strncpy(domain_val, cp, domain_len);
654 		domain_val[domain_len] = '\0';
655 		cp += domain_len + 1;
656 
657 		(void) ddi_strtoll(cp, &end, 10, (longlong_t *)rid);
658 		if (*end != '\0') {
659 			strfree(domain_val);
660 			return (EINVAL);
661 		}
662 	} else {
663 		/* It's only a user/group ID (eg "12345"), just get the rid */
664 		domain_val = NULL;
665 		(void) ddi_strtoll(cp, &end, 10, (longlong_t *)rid);
666 		if (*end != '\0')
667 			return (EINVAL);
668 	}
669 	*domain = domain_val;
670 	return (0);
671 }
672 
673 /*
674  * Look up {user|group}{quota|used} property for given dataset. On success
675  * push the value (quota or used amount) and the setpoint. On failure, push
676  * a lua error.
677  */
678 static int
679 zcp_get_userquota_prop(lua_State *state, dsl_pool_t *dp,
680     const char *dataset_name, const char *prop_name)
681 {
682 	zfsvfs_t *zfvp;
683 	zfsvfs_t *zfsvfs;
684 	int error;
685 	zfs_userquota_prop_t type;
686 	char *domain;
687 	uint64_t rid, value;
688 	objset_t *os;
689 
690 	dsl_dataset_t *ds = zcp_dataset_hold(state, dp, dataset_name, FTAG);
691 	if (ds == NULL)
692 		return (1); /* not reached; zcp_dataset_hold() longjmp'd */
693 
694 	error = parse_userquota_prop(prop_name, &type, &domain, &rid);
695 	if (error == 0) {
696 		error = dmu_objset_from_ds(ds, &os);
697 		if (error == 0) {
698 			zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
699 			error = zfsvfs_create_impl(&zfvp, zfsvfs, os);
700 			if (error == 0) {
701 				error = zfs_userspace_one(zfvp, type, domain,
702 				    rid, &value);
703 				zfsvfs_free(zfvp);
704 			}
705 		}
706 		if (domain != NULL)
707 			strfree(domain);
708 	}
709 	dsl_dataset_rele(ds, FTAG);
710 
711 	if ((value == 0) && ((type == ZFS_PROP_USERQUOTA) ||
712 	    (type == ZFS_PROP_GROUPQUOTA)))
713 		error = ENOENT;
714 	if (error != 0) {
715 		return (zcp_handle_error(state, dataset_name,
716 		    prop_name, error));
717 	}
718 
719 	(void) lua_pushnumber(state, value);
720 	(void) lua_pushstring(state, dataset_name);
721 	return (2);
722 }
723 #endif
724 
725 /*
726  * Determines the name of the snapshot referenced in the written property
727  * name. Returns snapshot name in snap_name, a buffer that must be at least
728  * as large as ZFS_MAX_DATASET_NAME_LEN
729  */
730 static void
731 parse_written_prop(const char *dataset_name, const char *prop_name,
732     char *snap_name)
733 {
734 	ASSERT(zfs_prop_written(prop_name));
735 	const char *name = prop_name + ZFS_WRITTEN_PROP_PREFIX_LEN;
736 	if (strchr(name, '@') == NULL) {
737 		(void) sprintf(snap_name, "%s@%s", dataset_name, name);
738 	} else {
739 		(void) strcpy(snap_name, name);
740 	}
741 }
742 
743 /*
744  * Look up written@ property for given dataset. On success
745  * push the value and the setpoint. If error is fatal, we will
746  * longjmp, otherwise push nothing.
747  */
748 static int
749 zcp_get_written_prop(lua_State *state, dsl_pool_t *dp,
750     const char *dataset_name, const char *prop_name)
751 {
752 	char snap_name[ZFS_MAX_DATASET_NAME_LEN];
753 	uint64_t used, comp, uncomp;
754 	dsl_dataset_t *old;
755 	int error = 0;
756 
757 	parse_written_prop(dataset_name, prop_name, snap_name);
758 	dsl_dataset_t *new = zcp_dataset_hold(state, dp, dataset_name, FTAG);
759 	if (new == NULL)
760 		return (1); /* not reached; zcp_dataset_hold() longjmp'd */
761 
762 	error = dsl_dataset_hold(dp, snap_name, FTAG, &old);
763 	if (error != 0) {
764 		dsl_dataset_rele(new, FTAG);
765 		return (zcp_dataset_hold_error(state, dp, snap_name,
766 		    error));
767 	}
768 	error = dsl_dataset_space_written(old, new,
769 	    &used, &comp, &uncomp);
770 
771 	dsl_dataset_rele(old, FTAG);
772 	dsl_dataset_rele(new, FTAG);
773 
774 	if (error != 0) {
775 		return (zcp_handle_error(state, dataset_name,
776 		    snap_name, error));
777 	}
778 	(void) lua_pushnumber(state, used);
779 	(void) lua_pushstring(state, dataset_name);
780 	return (2);
781 }
782 
783 static int zcp_get_prop(lua_State *state);
784 static zcp_lib_info_t zcp_get_prop_info = {
785 	.name = "get_prop",
786 	.func = zcp_get_prop,
787 	.pargs = {
788 	    { .za_name = "dataset", .za_lua_type = LUA_TSTRING},
789 	    { .za_name = "property", .za_lua_type =  LUA_TSTRING},
790 	    {NULL, NULL}
791 	},
792 	.kwargs = {
793 	    {NULL, NULL}
794 	}
795 };
796 
797 static int
798 zcp_get_prop(lua_State *state)
799 {
800 	const char *dataset_name;
801 	const char *property_name;
802 	dsl_pool_t *dp = zcp_run_info(state)->zri_pool;
803 	zcp_lib_info_t *libinfo = &zcp_get_prop_info;
804 
805 	zcp_parse_args(state, libinfo->name, libinfo->pargs, libinfo->kwargs);
806 
807 	dataset_name = lua_tostring(state, 1);
808 	property_name = lua_tostring(state, 2);
809 
810 	/* User defined property */
811 	if (zfs_prop_user(property_name)) {
812 		return (zcp_get_user_prop(state, dp,
813 		    dataset_name, property_name));
814 	}
815 	/* userspace property */
816 	if (zfs_prop_userquota(property_name)) {
817 #ifdef _KERNEL
818 		return (zcp_get_userquota_prop(state, dp,
819 		    dataset_name, property_name));
820 #else
821 		return (luaL_error(state,
822 		    "user quota properties only supported in kernel mode",
823 		    property_name));
824 #endif
825 	}
826 	/* written@ property */
827 	if (zfs_prop_written(property_name)) {
828 		return (zcp_get_written_prop(state, dp,
829 		    dataset_name, property_name));
830 	}
831 
832 	zfs_prop_t zfs_prop = zfs_name_to_prop(property_name);
833 	/* Valid system property */
834 	if (zfs_prop != ZPROP_INVAL) {
835 		return (zcp_get_system_prop(state, dp, dataset_name,
836 		    zfs_prop));
837 	}
838 
839 	/* Invalid property name */
840 	return (luaL_error(state,
841 	    "'%s' is not a valid property", property_name));
842 }
843 
844 int
845 zcp_load_get_lib(lua_State *state)
846 {
847 	lua_pushcclosure(state, zcp_get_prop_info.func, 0);
848 	lua_setfield(state, -2, zcp_get_prop_info.name);
849 
850 	return (1);
851 }
852