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