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