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