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
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.
17 * Copyright (c) 2016 by Delphix. All rights reserved.
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>
35 #include <sys/zcp_iter.h>
36 #include <sys/zcp_global.h>
37 #include <sys/zfs_ioctl.h>
38 #include <sys/zfs_znode.h>
42 #include <sys/zfs_vfsops.h>
46 get_objset_type(dsl_dataset_t
*ds
, zfs_type_t
*type
)
50 error
= dmu_objset_from_ds(ds
, &os
);
53 if (ds
->ds_is_snapshot
) {
54 *type
= ZFS_TYPE_SNAPSHOT
;
56 switch (os
->os_phys
->os_type
) {
58 *type
= ZFS_TYPE_FILESYSTEM
;
61 *type
= ZFS_TYPE_VOLUME
;
71 * Returns the string name of ds's type in str (a buffer which should be
72 * at least 12 bytes long).
75 get_objset_type_name(dsl_dataset_t
*ds
, char *str
)
79 error
= get_objset_type(ds
, &type
);
83 case ZFS_TYPE_SNAPSHOT
:
84 (void) strcpy(str
, "snapshot");
86 case ZFS_TYPE_FILESYSTEM
:
87 (void) strcpy(str
, "filesystem");
90 (void) strcpy(str
, "volume");
99 * Determines the source of a property given its setpoint and
100 * property type. It pushes the source to the lua stack.
103 get_prop_src(lua_State
*state
, const char *setpoint
, zfs_prop_t prop
)
105 if (zfs_prop_readonly(prop
) || (prop
== ZFS_PROP_VERSION
)) {
109 if (strcmp("", setpoint
) == 0) {
114 (void) lua_pushstring(state
, src
);
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.
123 zcp_handle_error(lua_State
*state
, const char *dataset_name
,
124 const char *property_name
, int error
)
126 ASSERT3S(error
, !=, 0);
127 if (error
== ENOENT
) {
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
));
138 return (luaL_error(state
, "unexpected error %d while "
139 "retrieving property '%s' on dataset '%s'",
140 error
, property_name
, dataset_name
));
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.
149 zcp_get_user_prop(lua_State
*state
, dsl_pool_t
*dp
, const char *dataset_name
,
150 const char *property_name
)
154 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
];
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
160 dsl_dataset_t
*ds
= zcp_dataset_hold(state
, dp
, dataset_name
, FTAG
);
162 return (1); /* not reached; zcp_dataset_hold() longjmp'd */
164 buf
= kmem_alloc(ZAP_MAXVALUELEN
, KM_SLEEP
);
165 error
= dsl_prop_get_ds(ds
, property_name
, 1, ZAP_MAXVALUELEN
,
167 dsl_dataset_rele(ds
, FTAG
);
170 kmem_free(buf
, ZAP_MAXVALUELEN
);
171 return (zcp_handle_error(state
, dataset_name
, property_name
,
174 (void) lua_pushstring(state
, buf
);
175 (void) lua_pushstring(state
, setpoint
);
176 kmem_free(buf
, ZAP_MAXVALUELEN
);
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.
186 get_dsl_dir_prop(dsl_dataset_t
*ds
, zfs_prop_t zfs_prop
,
189 dsl_dir_t
*dd
= ds
->ds_dir
;
190 mutex_enter(&dd
->dd_lock
);
192 case ZFS_PROP_USEDSNAP
:
193 *val
= dsl_dir_get_usedsnap(dd
);
195 case ZFS_PROP_USEDCHILD
:
196 *val
= dsl_dir_get_usedchild(dd
);
198 case ZFS_PROP_USEDDS
:
199 *val
= dsl_dir_get_usedds(dd
);
201 case ZFS_PROP_USEDREFRESERV
:
202 *val
= dsl_dir_get_usedrefreserv(dd
);
204 case ZFS_PROP_LOGICALUSED
:
205 *val
= dsl_dir_get_logicalused(dd
);
208 mutex_exit(&dd
->dd_lock
);
211 mutex_exit(&dd
->dd_lock
);
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.
222 get_temporary_prop(dsl_dataset_t
*ds
, zfs_prop_t zfs_prop
, uint64_t *val
,
234 error
= dmu_objset_from_ds(ds
, &os
);
238 error
= getzfsvfs_impl(os
, &zfvp
);
246 if (vfs_optionisset(vfsp
, MNTOPT_NOATIME
, NULL
))
248 if (vfs_optionisset(vfsp
, MNTOPT_ATIME
, NULL
))
251 case ZFS_PROP_DEVICES
:
252 if (vfs_optionisset(vfsp
, MNTOPT_NODEVICES
, NULL
))
254 if (vfs_optionisset(vfsp
, MNTOPT_DEVICES
, NULL
))
258 if (vfs_optionisset(vfsp
, MNTOPT_NOEXEC
, NULL
))
260 if (vfs_optionisset(vfsp
, MNTOPT_EXEC
, NULL
))
263 case ZFS_PROP_SETUID
:
264 if (vfs_optionisset(vfsp
, MNTOPT_NOSETUID
, NULL
))
266 if (vfs_optionisset(vfsp
, MNTOPT_SETUID
, NULL
))
269 case ZFS_PROP_READONLY
:
270 if (vfs_optionisset(vfsp
, MNTOPT_RW
, NULL
))
272 if (vfs_optionisset(vfsp
, MNTOPT_RO
, NULL
))
276 if (vfs_optionisset(vfsp
, MNTOPT_NOXATTR
, NULL
))
278 if (vfs_optionisset(vfsp
, MNTOPT_XATTR
, NULL
))
281 case ZFS_PROP_NBMAND
:
282 if (vfs_optionisset(vfsp
, MNTOPT_NONBMAND
, NULL
))
284 if (vfs_optionisset(vfsp
, MNTOPT_NBMAND
, NULL
))
294 (void) strcpy(setpoint
, "temporary");
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.
308 get_special_prop(lua_State
*state
, dsl_dataset_t
*ds
, const char *dsname
,
314 char *strval
= kmem_alloc(ZAP_MAXVALUELEN
, KM_SLEEP
);
315 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
] =
316 "Internal error - setpoint not determined";
318 zprop_type_t prop_type
= zfs_prop_get_type(zfs_prop
);
319 (void) get_objset_type(ds
, &ds_type
);
322 case ZFS_PROP_REFRATIO
:
323 numval
= dsl_get_refratio(ds
);
326 numval
= dsl_get_used(ds
);
328 case ZFS_PROP_CLONES
: {
329 nvlist_t
*clones
= fnvlist_alloc();
330 error
= get_clones_stat_impl(ds
, clones
);
332 /* push list to lua stack */
333 VERIFY0(zcp_nvlist_to_lua(state
, clones
, NULL
, 0));
335 (void) lua_pushnil(state
);
338 kmem_free(strval
, ZAP_MAXVALUELEN
);
341 case ZFS_PROP_COMPRESSRATIO
:
342 numval
= dsl_get_compressratio(ds
);
344 case ZFS_PROP_CREATION
:
345 numval
= dsl_get_creation(ds
);
347 case ZFS_PROP_REFERENCED
:
348 numval
= dsl_get_referenced(ds
);
350 case ZFS_PROP_AVAILABLE
:
351 numval
= dsl_get_available(ds
);
353 case ZFS_PROP_LOGICALREFERENCED
:
354 numval
= dsl_get_logicalreferenced(ds
);
356 case ZFS_PROP_CREATETXG
:
357 numval
= dsl_get_creationtxg(ds
);
360 numval
= dsl_get_guid(ds
);
362 case ZFS_PROP_UNIQUE
:
363 numval
= dsl_get_unique(ds
);
365 case ZFS_PROP_OBJSETID
:
366 numval
= dsl_get_objsetid(ds
);
368 case ZFS_PROP_ORIGIN
:
369 dsl_dir_get_origin(ds
->ds_dir
, strval
);
371 case ZFS_PROP_USERACCOUNTING
:
372 error
= dmu_objset_from_ds(ds
, &os
);
374 numval
= dmu_objset_userspace_present(os
);
376 case ZFS_PROP_WRITTEN
:
377 error
= dsl_get_written(ds
, &numval
);
380 error
= get_objset_type_name(ds
, strval
);
382 case ZFS_PROP_PREV_SNAP
:
383 error
= dsl_get_prev_snap(ds
, strval
);
386 dsl_dataset_name(ds
, strval
);
388 case ZFS_PROP_MOUNTPOINT
:
389 error
= dsl_get_mountpoint(ds
, dsname
, strval
, setpoint
);
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 */
397 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZPL_VERSION_STR
,
398 sizeof (numval
), 1, &numval
);
401 case ZFS_PROP_DEFER_DESTROY
:
402 numval
= dsl_get_defer_destroy(ds
);
404 case ZFS_PROP_USERREFS
:
405 numval
= dsl_get_userrefs(ds
);
407 case ZFS_PROP_FILESYSTEM_COUNT
:
408 error
= dsl_dir_get_filesystem_count(ds
->ds_dir
, &numval
);
409 (void) strcpy(setpoint
, "");
411 case ZFS_PROP_SNAPSHOT_COUNT
:
412 error
= dsl_dir_get_snapshot_count(ds
->ds_dir
, &numval
);
413 (void) strcpy(setpoint
, "");
415 case ZFS_PROP_NUMCLONES
:
416 numval
= dsl_get_numclones(ds
);
418 case ZFS_PROP_INCONSISTENT
:
419 numval
= dsl_get_inconsistent(ds
);
421 case ZFS_PROP_RECEIVE_RESUME_TOKEN
:
422 VERIFY3U(strlcpy(strval
, get_receive_resume_stats_impl(ds
),
423 ZAP_MAXVALUELEN
), <, ZAP_MAXVALUELEN
);
424 if (strcmp(strval
, "") == 0) {
425 VERIFY3U(strlcpy(strval
, get_child_receive_stats(ds
),
426 ZAP_MAXVALUELEN
), <, ZAP_MAXVALUELEN
);
427 if (strcmp(strval
, "") == 0)
431 case ZFS_PROP_VOLSIZE
:
432 ASSERT(ds_type
== ZFS_TYPE_VOLUME
);
433 error
= dmu_objset_from_ds(ds
, &os
);
435 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size",
436 sizeof (numval
), 1, &numval
);
439 (void) strcpy(setpoint
, dsname
);
442 case ZFS_PROP_VOLBLOCKSIZE
: {
443 ASSERT(ds_type
== ZFS_TYPE_VOLUME
);
444 dmu_object_info_t doi
;
445 error
= dmu_objset_from_ds(ds
, &os
);
447 error
= dmu_object_info(os
, ZVOL_OBJ
, &doi
);
449 numval
= doi
.doi_data_block_size
;
454 /* Did not match these props, check in the dsl_dir */
455 error
= get_dsl_dir_prop(ds
, zfs_prop
, &numval
);
458 kmem_free(strval
, ZAP_MAXVALUELEN
);
463 case PROP_TYPE_NUMBER
: {
464 (void) lua_pushnumber(state
, numval
);
467 case PROP_TYPE_STRING
: {
468 (void) lua_pushstring(state
, strval
);
471 case PROP_TYPE_INDEX
: {
473 error
= zfs_prop_index_to_string(zfs_prop
, numval
, &propval
);
475 kmem_free(strval
, ZAP_MAXVALUELEN
);
478 (void) lua_pushstring(state
, propval
);
482 kmem_free(strval
, ZAP_MAXVALUELEN
);
484 /* Push the source to the stack */
485 get_prop_src(state
, setpoint
, zfs_prop
);
490 * Look up a property and its source in the zap object. If the value is
491 * present and successfully retrieved, push the value and source on the
492 * lua stack and return 0. On failure, return a non-zero error value.
495 get_zap_prop(lua_State
*state
, dsl_dataset_t
*ds
, zfs_prop_t zfs_prop
)
498 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
];
499 char *strval
= kmem_alloc(ZAP_MAXVALUELEN
, KM_SLEEP
);
501 const char *prop_name
= zfs_prop_to_name(zfs_prop
);
502 zprop_type_t prop_type
= zfs_prop_get_type(zfs_prop
);
504 if (prop_type
== PROP_TYPE_STRING
) {
505 /* Push value to lua stack */
506 error
= dsl_prop_get_ds(ds
, prop_name
, 1,
507 ZAP_MAXVALUELEN
, strval
, setpoint
);
509 (void) lua_pushstring(state
, strval
);
511 error
= dsl_prop_get_ds(ds
, prop_name
, sizeof (numval
),
512 1, &numval
, setpoint
);
514 /* Fill in temorary value for prop, if applicable */
515 (void) get_temporary_prop(ds
, zfs_prop
, &numval
, setpoint
);
517 /* Push value to lua stack */
518 if (prop_type
== PROP_TYPE_INDEX
) {
520 error
= zfs_prop_index_to_string(zfs_prop
, numval
,
523 (void) lua_pushstring(state
, propval
);
526 (void) lua_pushnumber(state
, numval
);
529 kmem_free(strval
, ZAP_MAXVALUELEN
);
531 get_prop_src(state
, setpoint
, zfs_prop
);
536 * Determine whether property is valid for a given dataset
539 prop_valid_for_ds(dsl_dataset_t
*ds
, zfs_prop_t zfs_prop
)
544 /* properties not supported */
545 if ((zfs_prop
== ZFS_PROP_ISCSIOPTIONS
) ||
546 (zfs_prop
== ZFS_PROP_MOUNTED
))
549 /* if we want the origin prop, ds must be a clone */
550 if ((zfs_prop
== ZFS_PROP_ORIGIN
) && (!dsl_dir_is_clone(ds
->ds_dir
)))
553 error
= get_objset_type(ds
, &zfs_type
);
556 return (zfs_prop_valid_for_type(zfs_prop
, zfs_type
));
560 * Look up a given dataset property. On success return 2, the number of
561 * values pushed to the lua stack (property value and source). On a fatal
562 * error, longjmp. On a non fatal error push nothing.
565 zcp_get_system_prop(lua_State
*state
, dsl_pool_t
*dp
, const char *dataset_name
,
570 * zcp_dataset_hold will either successfully return the requested
571 * dataset or throw a lua error and longjmp out of the zfs.get_prop call
574 dsl_dataset_t
*ds
= zcp_dataset_hold(state
, dp
, dataset_name
, FTAG
);
576 return (1); /* not reached; zcp_dataset_hold() longjmp'd */
578 /* Check that the property is valid for the given dataset */
579 const char *prop_name
= zfs_prop_to_name(zfs_prop
);
580 if (!prop_valid_for_ds(ds
, zfs_prop
)) {
581 dsl_dataset_rele(ds
, FTAG
);
585 /* Check if the property can be accessed directly */
586 error
= get_special_prop(state
, ds
, dataset_name
, zfs_prop
);
588 dsl_dataset_rele(ds
, FTAG
);
589 /* The value and source have been pushed by get_special_prop */
592 if (error
!= ENOENT
) {
593 dsl_dataset_rele(ds
, FTAG
);
594 return (zcp_handle_error(state
, dataset_name
,
598 /* If we were unable to find it, look in the zap object */
599 error
= get_zap_prop(state
, ds
, zfs_prop
);
600 dsl_dataset_rele(ds
, FTAG
);
602 return (zcp_handle_error(state
, dataset_name
,
605 /* The value and source have been pushed by get_zap_prop */
609 static zfs_userquota_prop_t
610 get_userquota_prop(const char *prop_name
)
612 zfs_userquota_prop_t type
;
613 /* Figure out the property type ({user|group}{quota|used}) */
614 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
615 if (strncmp(prop_name
, zfs_userquota_prop_prefixes
[type
],
616 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
624 * Given the name of a zfs_userquota_prop, this function determines the
625 * prop type as well as the numeric group/user ids based on the string
626 * following the '@' in the property name. On success, returns 0. On failure,
627 * returns a non-zero error.
628 * 'domain' must be free'd by caller using strfree()
631 parse_userquota_prop(const char *prop_name
, zfs_userquota_prop_t
*type
,
632 char **domain
, uint64_t *rid
)
634 char *cp
, *end
, *domain_val
;
636 *type
= get_userquota_prop(prop_name
);
637 if (*type
>= ZFS_NUM_USERQUOTA_PROPS
)
641 cp
= strchr(prop_name
, '@') + 1;
642 if (strncmp(cp
, "S-1-", 4) == 0) {
644 * It's a numeric SID (eg "S-1-234-567-89") and we want to
645 * seperate the domain id and the rid
647 int domain_len
= strrchr(cp
, '-') - cp
;
648 domain_val
= kmem_alloc(domain_len
+ 1, KM_SLEEP
);
649 (void) strncpy(domain_val
, cp
, domain_len
);
650 domain_val
[domain_len
] = '\0';
651 cp
+= domain_len
+ 1;
653 (void) ddi_strtoll(cp
, &end
, 10, (longlong_t
*)rid
);
659 /* It's only a user/group ID (eg "12345"), just get the rid */
661 (void) ddi_strtoll(cp
, &end
, 10, (longlong_t
*)rid
);
665 *domain
= domain_val
;
670 * Look up {user|group}{quota|used} property for given dataset. On success
671 * push the value (quota or used amount) and the setpoint. On failure, push
675 zcp_get_userquota_prop(lua_State
*state
, dsl_pool_t
*dp
,
676 const char *dataset_name
, const char *prop_name
)
681 zfs_userquota_prop_t type
;
686 dsl_dataset_t
*ds
= zcp_dataset_hold(state
, dp
, dataset_name
, FTAG
);
688 return (1); /* not reached; zcp_dataset_hold() longjmp'd */
690 error
= parse_userquota_prop(prop_name
, &type
, &domain
, &rid
);
692 error
= dmu_objset_from_ds(ds
, &os
);
694 zfsvfs
= kmem_zalloc(sizeof (zfsvfs_t
), KM_SLEEP
);
695 error
= zfsvfs_create_impl(&zfvp
, zfsvfs
, os
);
697 error
= zfs_userspace_one(zfvp
, type
, domain
,
705 dsl_dataset_rele(ds
, FTAG
);
707 if ((value
== 0) && ((type
== ZFS_PROP_USERQUOTA
) ||
708 (type
== ZFS_PROP_GROUPQUOTA
)))
711 return (zcp_handle_error(state
, dataset_name
,
715 (void) lua_pushnumber(state
, value
);
716 (void) lua_pushstring(state
, dataset_name
);
722 * Determines the name of the snapshot referenced in the written property
723 * name. Returns snapshot name in snap_name, a buffer that must be at least
724 * as large as ZFS_MAX_DATASET_NAME_LEN
727 parse_written_prop(const char *dataset_name
, const char *prop_name
,
730 ASSERT(zfs_prop_written(prop_name
));
731 const char *name
= prop_name
+ ZFS_WRITTEN_PROP_PREFIX_LEN
;
732 if (strchr(name
, '@') == NULL
) {
733 (void) sprintf(snap_name
, "%s@%s", dataset_name
, name
);
735 (void) strcpy(snap_name
, name
);
740 * Look up written@ property for given dataset. On success
741 * push the value and the setpoint. If error is fatal, we will
742 * longjmp, otherwise push nothing.
745 zcp_get_written_prop(lua_State
*state
, dsl_pool_t
*dp
,
746 const char *dataset_name
, const char *prop_name
)
748 char snap_name
[ZFS_MAX_DATASET_NAME_LEN
];
749 uint64_t used
, comp
, uncomp
;
753 parse_written_prop(dataset_name
, prop_name
, snap_name
);
754 dsl_dataset_t
*new = zcp_dataset_hold(state
, dp
, dataset_name
, FTAG
);
756 return (1); /* not reached; zcp_dataset_hold() longjmp'd */
758 error
= dsl_dataset_hold(dp
, snap_name
, FTAG
, &old
);
760 dsl_dataset_rele(new, FTAG
);
761 return (zcp_dataset_hold_error(state
, dp
, snap_name
,
764 error
= dsl_dataset_space_written(old
, new,
765 &used
, &comp
, &uncomp
);
767 dsl_dataset_rele(old
, FTAG
);
768 dsl_dataset_rele(new, FTAG
);
771 return (zcp_handle_error(state
, dataset_name
,
774 (void) lua_pushnumber(state
, used
);
775 (void) lua_pushstring(state
, dataset_name
);
779 static int zcp_get_prop(lua_State
*state
);
780 static zcp_lib_info_t zcp_get_prop_info
= {
782 .func
= zcp_get_prop
,
784 { .za_name
= "dataset", .za_lua_type
= LUA_TSTRING
},
785 { .za_name
= "property", .za_lua_type
= LUA_TSTRING
},
794 zcp_get_prop(lua_State
*state
)
796 const char *dataset_name
;
797 const char *property_name
;
798 dsl_pool_t
*dp
= zcp_run_info(state
)->zri_pool
;
799 zcp_lib_info_t
*libinfo
= &zcp_get_prop_info
;
801 zcp_parse_args(state
, libinfo
->name
, libinfo
->pargs
, libinfo
->kwargs
);
803 dataset_name
= lua_tostring(state
, 1);
804 property_name
= lua_tostring(state
, 2);
806 /* User defined property */
807 if (zfs_prop_user(property_name
)) {
808 return (zcp_get_user_prop(state
, dp
,
809 dataset_name
, property_name
));
811 /* userspace property */
812 if (zfs_prop_userquota(property_name
)) {
814 return (zcp_get_userquota_prop(state
, dp
,
815 dataset_name
, property_name
));
817 return (luaL_error(state
,
818 "user quota properties only supported in kernel mode",
822 /* written@ property */
823 if (zfs_prop_written(property_name
)) {
824 return (zcp_get_written_prop(state
, dp
,
825 dataset_name
, property_name
));
828 zfs_prop_t zfs_prop
= zfs_name_to_prop(property_name
);
829 /* Valid system property */
830 if (zfs_prop
!= ZPROP_INVAL
) {
831 return (zcp_get_system_prop(state
, dp
, dataset_name
,
835 /* Invalid property name */
836 return (luaL_error(state
,
837 "'%s' is not a valid property", property_name
));
841 zcp_load_get_lib(lua_State
*state
)
843 lua_pushcclosure(state
, zcp_get_prop_info
.func
, 0);
844 lua_setfield(state
, -2, zcp_get_prop_info
.name
);