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