xref: /freebsd/sys/contrib/openzfs/module/zfs/zcp_get.c (revision b1c1ee4429fcca8f69873a8be66184e68e1b19d7)
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
get_objset_type(dsl_dataset_t * ds,zfs_type_t * type)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
get_objset_type_name(dsl_dataset_t * ds,char * str)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
get_prop_src(lua_State * state,const char * setpoint,zfs_prop_t prop)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
zcp_handle_error(lua_State * state,const char * dataset_name,const char * property_name,int error)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
zcp_get_user_prop(lua_State * state,dsl_pool_t * dp,const char * dataset_name,const char * property_name)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
get_dsl_dir_prop(dsl_dataset_t * ds,zfs_prop_t zfs_prop,uint64_t * val)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
get_special_prop(lua_State * state,dsl_dataset_t * ds,const char * dsname,zfs_prop_t zfs_prop)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_ENCRYPTION:
382 	case ZFS_PROP_KEYSTATUS:
383 	case ZFS_PROP_KEYFORMAT: {
384 		/* provide defaults in case no crypto obj exists */
385 		setpoint[0] = '\0';
386 		if (zfs_prop == ZFS_PROP_ENCRYPTION)
387 			numval = ZIO_CRYPT_OFF;
388 		else if (zfs_prop == ZFS_PROP_KEYFORMAT)
389 			numval = ZFS_KEYFORMAT_NONE;
390 		else if (zfs_prop == ZFS_PROP_KEYSTATUS)
391 			numval = ZFS_KEYSTATUS_NONE;
392 
393 		nvlist_t *nvl, *propval;
394 		nvl = fnvlist_alloc();
395 		dsl_dataset_crypt_stats(ds, nvl);
396 		if (nvlist_lookup_nvlist(nvl, zfs_prop_to_name(zfs_prop),
397 		    &propval) == 0) {
398 			const char *source;
399 
400 			(void) nvlist_lookup_uint64(propval, ZPROP_VALUE,
401 			    &numval);
402 			if (nvlist_lookup_string(propval, ZPROP_SOURCE,
403 			    &source) == 0)
404 				strlcpy(setpoint, source, sizeof (setpoint));
405 		}
406 		nvlist_free(nvl);
407 		break;
408 	}
409 
410 	case ZFS_PROP_ENCRYPTION_ROOT: {
411 		setpoint[0] = '\0';
412 		strval[0] = '\0';
413 
414 		nvlist_t *nvl, *propval;
415 		nvl = fnvlist_alloc();
416 		dsl_dataset_crypt_stats(ds, nvl);
417 		if (nvlist_lookup_nvlist(nvl, zfs_prop_to_name(zfs_prop),
418 		    &propval) == 0) {
419 			const char *dsname;
420 			const char *source;
421 
422 			if (nvlist_lookup_string(propval, ZPROP_VALUE,
423 			    &dsname) == 0)
424 				strlcpy(strval, dsname, ZAP_MAXVALUELEN);
425 			if (nvlist_lookup_string(propval, ZPROP_SOURCE,
426 			    &source) == 0)
427 				strlcpy(setpoint, source, sizeof (setpoint));
428 		}
429 		nvlist_free(nvl);
430 		break;
431 	}
432 
433 	case ZFS_PROP_SNAPSHOTS_CHANGED:
434 		numval = dsl_dir_snap_cmtime(ds->ds_dir).tv_sec;
435 		break;
436 
437 	default:
438 		/* Did not match these props, check in the dsl_dir */
439 		error = get_dsl_dir_prop(ds, zfs_prop, &numval);
440 	}
441 	if (error != 0) {
442 		kmem_free(strval, ZAP_MAXVALUELEN);
443 		return (error);
444 	}
445 
446 	switch (prop_type) {
447 	case PROP_TYPE_NUMBER: {
448 		(void) lua_pushnumber(state, numval);
449 		break;
450 	}
451 	case PROP_TYPE_STRING: {
452 		(void) lua_pushstring(state, strval);
453 		break;
454 	}
455 	case PROP_TYPE_INDEX: {
456 		const char *propval;
457 		error = zfs_prop_index_to_string(zfs_prop, numval, &propval);
458 		if (error != 0) {
459 			kmem_free(strval, ZAP_MAXVALUELEN);
460 			return (error);
461 		}
462 		(void) lua_pushstring(state, propval);
463 		break;
464 	}
465 	}
466 	kmem_free(strval, ZAP_MAXVALUELEN);
467 
468 	/* Push the source to the stack */
469 	get_prop_src(state, setpoint, zfs_prop);
470 	return (0);
471 }
472 
473 /*
474  * Look up a property and its source in the zap object. If the value is
475  * present and successfully retrieved, push the value and source on the
476  * lua stack and return 0. On failure, return a non-zero error value.
477  */
478 static int
get_zap_prop(lua_State * state,dsl_dataset_t * ds,zfs_prop_t zfs_prop)479 get_zap_prop(lua_State *state, dsl_dataset_t *ds, zfs_prop_t zfs_prop)
480 {
481 	int error = 0;
482 	char setpoint[ZFS_MAX_DATASET_NAME_LEN];
483 	char *strval = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
484 	uint64_t numval;
485 	const char *prop_name = zfs_prop_to_name(zfs_prop);
486 	zprop_type_t prop_type = zfs_prop_get_type(zfs_prop);
487 
488 	if (prop_type == PROP_TYPE_STRING) {
489 		/* Push value to lua stack */
490 		error = dsl_prop_get_ds(ds, prop_name, 1,
491 		    ZAP_MAXVALUELEN, strval, setpoint);
492 		if (error == 0)
493 			(void) lua_pushstring(state, strval);
494 	} else {
495 		error = dsl_prop_get_ds(ds, prop_name, sizeof (numval),
496 		    1, &numval, setpoint);
497 		if (error != 0)
498 			goto out;
499 #ifdef _KERNEL
500 		/* Fill in temporary value for prop, if applicable */
501 		(void) zfs_get_temporary_prop(ds, zfs_prop, &numval, setpoint);
502 #else
503 		kmem_free(strval, ZAP_MAXVALUELEN);
504 		return (luaL_error(state,
505 		    "temporary properties only supported in kernel mode",
506 		    prop_name));
507 #endif
508 		/* Push value to lua stack */
509 		if (prop_type == PROP_TYPE_INDEX) {
510 			const char *propval;
511 			error = zfs_prop_index_to_string(zfs_prop, numval,
512 			    &propval);
513 			if (error == 0)
514 				(void) lua_pushstring(state, propval);
515 		} else {
516 			if (error == 0)
517 				(void) lua_pushnumber(state, numval);
518 		}
519 	}
520 out:
521 	kmem_free(strval, ZAP_MAXVALUELEN);
522 	if (error == 0)
523 		get_prop_src(state, setpoint, zfs_prop);
524 	return (error);
525 }
526 
527 /*
528  * Determine whether property is valid for a given dataset
529  */
530 boolean_t
prop_valid_for_ds(dsl_dataset_t * ds,zfs_prop_t zfs_prop)531 prop_valid_for_ds(dsl_dataset_t *ds, zfs_prop_t zfs_prop)
532 {
533 	zfs_type_t zfs_type = ZFS_TYPE_INVALID;
534 
535 	/* properties not supported */
536 	if ((zfs_prop == ZFS_PROP_ISCSIOPTIONS) ||
537 	    (zfs_prop == ZFS_PROP_MOUNTED))
538 		return (B_FALSE);
539 
540 	/* if we want the origin prop, ds must be a clone */
541 	if ((zfs_prop == ZFS_PROP_ORIGIN) && (!dsl_dir_is_clone(ds->ds_dir)))
542 		return (B_FALSE);
543 
544 	int error = get_objset_type(ds, &zfs_type);
545 	if (error != 0)
546 		return (B_FALSE);
547 	return (zfs_prop_valid_for_type(zfs_prop, zfs_type, B_FALSE));
548 }
549 
550 /*
551  * Look up a given dataset property. On success return 2, the number of
552  * values pushed to the lua stack (property value and source). On a fatal
553  * error, longjmp. On a non fatal error push nothing.
554  */
555 static int
zcp_get_system_prop(lua_State * state,dsl_pool_t * dp,const char * dataset_name,zfs_prop_t zfs_prop)556 zcp_get_system_prop(lua_State *state, dsl_pool_t *dp, const char *dataset_name,
557     zfs_prop_t zfs_prop)
558 {
559 	int error;
560 	/*
561 	 * zcp_dataset_hold will either successfully return the requested
562 	 * dataset or throw a lua error and longjmp out of the zfs.get_prop call
563 	 * without returning.
564 	 */
565 	dsl_dataset_t *ds = zcp_dataset_hold(state, dp, dataset_name, FTAG);
566 	if (ds == NULL)
567 		return (1); /* not reached; zcp_dataset_hold() longjmp'd */
568 
569 	/* Check that the property is valid for the given dataset */
570 	const char *prop_name = zfs_prop_to_name(zfs_prop);
571 	if (!prop_valid_for_ds(ds, zfs_prop)) {
572 		dsl_dataset_rele(ds, FTAG);
573 		return (0);
574 	}
575 
576 	/* Check if the property can be accessed directly */
577 	error = get_special_prop(state, ds, dataset_name, zfs_prop);
578 	if (error == 0) {
579 		dsl_dataset_rele(ds, FTAG);
580 		/* The value and source have been pushed by get_special_prop */
581 		return (2);
582 	}
583 	if (error != ENOENT) {
584 		dsl_dataset_rele(ds, FTAG);
585 		return (zcp_handle_error(state, dataset_name,
586 		    prop_name, error));
587 	}
588 
589 	/* If we were unable to find it, look in the zap object */
590 	error = get_zap_prop(state, ds, zfs_prop);
591 	dsl_dataset_rele(ds, FTAG);
592 	if (error != 0) {
593 		return (zcp_handle_error(state, dataset_name,
594 		    prop_name, error));
595 	}
596 	/* The value and source have been pushed by get_zap_prop */
597 	return (2);
598 }
599 
600 #ifdef _KERNEL
601 static zfs_userquota_prop_t
get_userquota_prop(const char * prop_name)602 get_userquota_prop(const char *prop_name)
603 {
604 	zfs_userquota_prop_t type;
605 	/* Figure out the property type ({user|group}{quota|used}) */
606 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
607 		if (strncmp(prop_name, zfs_userquota_prop_prefixes[type],
608 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
609 			break;
610 	}
611 	return (type);
612 }
613 
614 /*
615  * Given the name of a zfs_userquota_prop, this function determines the
616  * prop type as well as the numeric group/user ids based on the string
617  * following the '@' in the property name. On success, returns 0. On failure,
618  * returns a non-zero error.
619  * 'domain' must be free'd by caller using kmem_strfree()
620  */
621 static int
parse_userquota_prop(const char * prop_name,zfs_userquota_prop_t * type,char ** domain,uint64_t * rid)622 parse_userquota_prop(const char *prop_name, zfs_userquota_prop_t *type,
623     char **domain, uint64_t *rid)
624 {
625 	char *cp, *end, *domain_val;
626 
627 	*type = get_userquota_prop(prop_name);
628 	if (*type >= ZFS_NUM_USERQUOTA_PROPS)
629 		return (EINVAL);
630 
631 	*rid = 0;
632 	cp = strchr(prop_name, '@') + 1;
633 	if (strncmp(cp, "S-1-", 4) == 0) {
634 		/*
635 		 * It's a numeric SID (eg "S-1-234-567-89") and we want to
636 		 * separate the domain id and the rid
637 		 */
638 		int domain_len = strrchr(cp, '-') - cp;
639 		domain_val = kmem_alloc(domain_len + 1, KM_SLEEP);
640 		(void) strlcpy(domain_val, cp, domain_len + 1);
641 		cp += domain_len + 1;
642 
643 		(void) ddi_strtoll(cp, &end, 10, (longlong_t *)rid);
644 		if (*end != '\0') {
645 			kmem_strfree(domain_val);
646 			return (EINVAL);
647 		}
648 	} else {
649 		/* It's only a user/group ID (eg "12345"), just get the rid */
650 		domain_val = NULL;
651 		(void) ddi_strtoll(cp, &end, 10, (longlong_t *)rid);
652 		if (*end != '\0')
653 			return (EINVAL);
654 	}
655 	*domain = domain_val;
656 	return (0);
657 }
658 
659 /*
660  * Look up {user|group}{quota|used} property for given dataset. On success
661  * push the value (quota or used amount) and the setpoint. On failure, push
662  * a lua error.
663  */
664 static int
zcp_get_userquota_prop(lua_State * state,dsl_pool_t * dp,const char * dataset_name,const char * prop_name)665 zcp_get_userquota_prop(lua_State *state, dsl_pool_t *dp,
666     const char *dataset_name, const char *prop_name)
667 {
668 	zfsvfs_t *zfvp;
669 	zfsvfs_t *zfsvfs;
670 	int error;
671 	zfs_userquota_prop_t type;
672 	char *domain;
673 	uint64_t rid, value = 0;
674 	objset_t *os;
675 
676 	dsl_dataset_t *ds = zcp_dataset_hold(state, dp, dataset_name, FTAG);
677 	if (ds == NULL)
678 		return (1); /* not reached; zcp_dataset_hold() longjmp'd */
679 
680 	error = parse_userquota_prop(prop_name, &type, &domain, &rid);
681 	if (error == 0) {
682 		error = dmu_objset_from_ds(ds, &os);
683 		if (error == 0) {
684 			zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
685 			error = zfsvfs_create_impl(&zfvp, zfsvfs, os);
686 			if (error == 0) {
687 				error = zfs_userspace_one(zfvp, type, domain,
688 				    rid, &value);
689 				zfsvfs_free(zfvp);
690 			}
691 		}
692 		if (domain != NULL)
693 			kmem_strfree(domain);
694 	}
695 	dsl_dataset_rele(ds, FTAG);
696 
697 	if ((value == 0) && ((type == ZFS_PROP_USERQUOTA) ||
698 	    (type == ZFS_PROP_GROUPQUOTA)))
699 		error = SET_ERROR(ENOENT);
700 	if (error != 0) {
701 		return (zcp_handle_error(state, dataset_name,
702 		    prop_name, error));
703 	}
704 
705 	(void) lua_pushnumber(state, value);
706 	(void) lua_pushstring(state, dataset_name);
707 	return (2);
708 }
709 #endif
710 
711 /*
712  * Determines the name of the snapshot referenced in the written property
713  * name. Returns snapshot name in snap_name, a buffer that must be at least
714  * as large as ZFS_MAX_DATASET_NAME_LEN
715  */
716 static void
parse_written_prop(const char * dataset_name,const char * prop_name,char * snap_name)717 parse_written_prop(const char *dataset_name, const char *prop_name,
718     char *snap_name)
719 {
720 	ASSERT(zfs_prop_written(prop_name));
721 	const char *name = prop_name + ZFS_WRITTEN_PROP_PREFIX_LEN;
722 	if (strchr(name, '@') == NULL) {
723 		(void) snprintf(snap_name, ZFS_MAX_DATASET_NAME_LEN, "%s@%s",
724 		    dataset_name, name);
725 	} else {
726 		(void) strlcpy(snap_name, name, ZFS_MAX_DATASET_NAME_LEN);
727 	}
728 }
729 
730 /*
731  * Look up written@ property for given dataset. On success
732  * push the value and the setpoint. If error is fatal, we will
733  * longjmp, otherwise push nothing.
734  */
735 static int
zcp_get_written_prop(lua_State * state,dsl_pool_t * dp,const char * dataset_name,const char * prop_name)736 zcp_get_written_prop(lua_State *state, dsl_pool_t *dp,
737     const char *dataset_name, const char *prop_name)
738 {
739 	char snap_name[ZFS_MAX_DATASET_NAME_LEN];
740 	uint64_t used, comp, uncomp;
741 	dsl_dataset_t *old;
742 	int error = 0;
743 
744 	parse_written_prop(dataset_name, prop_name, snap_name);
745 	dsl_dataset_t *new = zcp_dataset_hold(state, dp, dataset_name, FTAG);
746 	if (new == NULL)
747 		return (1); /* not reached; zcp_dataset_hold() longjmp'd */
748 
749 	error = dsl_dataset_hold(dp, snap_name, FTAG, &old);
750 	if (error != 0) {
751 		dsl_dataset_rele(new, FTAG);
752 		return (zcp_dataset_hold_error(state, dp, snap_name,
753 		    error));
754 	}
755 	error = dsl_dataset_space_written(old, new,
756 	    &used, &comp, &uncomp);
757 
758 	dsl_dataset_rele(old, FTAG);
759 	dsl_dataset_rele(new, FTAG);
760 
761 	if (error != 0) {
762 		return (zcp_handle_error(state, dataset_name,
763 		    snap_name, error));
764 	}
765 	(void) lua_pushnumber(state, used);
766 	(void) lua_pushstring(state, dataset_name);
767 	return (2);
768 }
769 
770 static int zcp_get_prop(lua_State *state);
771 static const zcp_lib_info_t zcp_get_prop_info = {
772 	.name = "get_prop",
773 	.func = zcp_get_prop,
774 	.pargs = {
775 	    { .za_name = "dataset", .za_lua_type = LUA_TSTRING },
776 	    { .za_name = "property", .za_lua_type =  LUA_TSTRING },
777 	    {NULL, 0}
778 	},
779 	.kwargs = {
780 	    {NULL, 0}
781 	}
782 };
783 
784 static int
zcp_get_prop(lua_State * state)785 zcp_get_prop(lua_State *state)
786 {
787 	const char *dataset_name;
788 	const char *property_name;
789 	dsl_pool_t *dp = zcp_run_info(state)->zri_pool;
790 	const zcp_lib_info_t *libinfo = &zcp_get_prop_info;
791 
792 	zcp_parse_args(state, libinfo->name, libinfo->pargs, libinfo->kwargs);
793 
794 	dataset_name = lua_tostring(state, 1);
795 	property_name = lua_tostring(state, 2);
796 
797 	/* User defined property */
798 	if (zfs_prop_user(property_name)) {
799 		return (zcp_get_user_prop(state, dp,
800 		    dataset_name, property_name));
801 	}
802 	/* userspace property */
803 	if (zfs_prop_userquota(property_name)) {
804 #ifdef _KERNEL
805 		return (zcp_get_userquota_prop(state, dp,
806 		    dataset_name, property_name));
807 #else
808 		return (luaL_error(state,
809 		    "user quota properties only supported in kernel mode",
810 		    property_name));
811 #endif
812 	}
813 	/* written@ property */
814 	if (zfs_prop_written(property_name)) {
815 		return (zcp_get_written_prop(state, dp,
816 		    dataset_name, property_name));
817 	}
818 
819 	zfs_prop_t zfs_prop = zfs_name_to_prop(property_name);
820 	/* Valid system property */
821 	if (zfs_prop != ZPROP_INVAL) {
822 		return (zcp_get_system_prop(state, dp, dataset_name,
823 		    zfs_prop));
824 	}
825 
826 	/* Invalid property name */
827 	return (luaL_error(state,
828 	    "'%s' is not a valid property", property_name));
829 }
830 
831 int
zcp_load_get_lib(lua_State * state)832 zcp_load_get_lib(lua_State *state)
833 {
834 	lua_pushcclosure(state, zcp_get_prop_info.func, 0);
835 	lua_setfield(state, -2, zcp_get_prop_info.name);
836 
837 	return (1);
838 }
839