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