1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2023 Red Hat
6 #include "pool-sysfs.h"
8 #include <linux/kstrtox.h>
10 #include "memory-alloc.h"
11 #include "string-utils.h"
17 struct pool_attribute {
18 struct attribute attr;
19 ssize_t (*show)(struct vdo *vdo, char *buf);
20 ssize_t (*store)(struct vdo *vdo, const char *value, size_t count);
23 static ssize_t vdo_pool_attr_show(struct kobject *directory, struct attribute *attr,
26 struct pool_attribute *pool_attr = container_of(attr, struct pool_attribute,
28 struct vdo *vdo = container_of(directory, struct vdo, vdo_directory);
30 if (pool_attr->show == NULL)
32 return pool_attr->show(vdo, buf);
35 static ssize_t vdo_pool_attr_store(struct kobject *directory, struct attribute *attr,
36 const char *buf, size_t length)
38 struct pool_attribute *pool_attr = container_of(attr, struct pool_attribute,
40 struct vdo *vdo = container_of(directory, struct vdo, vdo_directory);
42 if (pool_attr->store == NULL)
44 return pool_attr->store(vdo, buf, length);
47 static const struct sysfs_ops vdo_pool_sysfs_ops = {
48 .show = vdo_pool_attr_show,
49 .store = vdo_pool_attr_store,
52 static ssize_t pool_compressing_show(struct vdo *vdo, char *buf)
54 return sprintf(buf, "%s\n", (vdo_get_compressing(vdo) ? "1" : "0"));
57 static ssize_t pool_discards_active_show(struct vdo *vdo, char *buf)
59 return sprintf(buf, "%u\n",
60 get_data_vio_pool_active_discards(vdo->data_vio_pool));
63 static ssize_t pool_discards_limit_show(struct vdo *vdo, char *buf)
65 return sprintf(buf, "%u\n", get_data_vio_pool_discard_limit(vdo->data_vio_pool));
68 static ssize_t pool_discards_limit_store(struct vdo *vdo, const char *buf, size_t length)
73 if ((length > 12) || (kstrtouint(buf, 10, &value) < 0) || (value < 1))
76 result = set_data_vio_pool_discard_limit(vdo->data_vio_pool, value);
77 if (result != VDO_SUCCESS)
83 static ssize_t pool_discards_maximum_show(struct vdo *vdo, char *buf)
85 return sprintf(buf, "%u\n",
86 get_data_vio_pool_maximum_discards(vdo->data_vio_pool));
89 static ssize_t pool_instance_show(struct vdo *vdo, char *buf)
91 return sprintf(buf, "%u\n", vdo->instance);
94 static ssize_t pool_requests_active_show(struct vdo *vdo, char *buf)
96 return sprintf(buf, "%u\n",
97 get_data_vio_pool_active_requests(vdo->data_vio_pool));
100 static ssize_t pool_requests_limit_show(struct vdo *vdo, char *buf)
102 return sprintf(buf, "%u\n", get_data_vio_pool_request_limit(vdo->data_vio_pool));
105 static ssize_t pool_requests_maximum_show(struct vdo *vdo, char *buf)
107 return sprintf(buf, "%u\n",
108 get_data_vio_pool_maximum_requests(vdo->data_vio_pool));
111 static void vdo_pool_release(struct kobject *directory)
113 vdo_free(container_of(directory, struct vdo, vdo_directory));
116 static struct pool_attribute vdo_pool_compressing_attr = {
118 .name = "compressing",
121 .show = pool_compressing_show,
124 static struct pool_attribute vdo_pool_discards_active_attr = {
126 .name = "discards_active",
129 .show = pool_discards_active_show,
132 static struct pool_attribute vdo_pool_discards_limit_attr = {
134 .name = "discards_limit",
137 .show = pool_discards_limit_show,
138 .store = pool_discards_limit_store,
141 static struct pool_attribute vdo_pool_discards_maximum_attr = {
143 .name = "discards_maximum",
146 .show = pool_discards_maximum_show,
149 static struct pool_attribute vdo_pool_instance_attr = {
154 .show = pool_instance_show,
157 static struct pool_attribute vdo_pool_requests_active_attr = {
159 .name = "requests_active",
162 .show = pool_requests_active_show,
165 static struct pool_attribute vdo_pool_requests_limit_attr = {
167 .name = "requests_limit",
170 .show = pool_requests_limit_show,
173 static struct pool_attribute vdo_pool_requests_maximum_attr = {
175 .name = "requests_maximum",
178 .show = pool_requests_maximum_show,
181 static struct attribute *pool_attrs[] = {
182 &vdo_pool_compressing_attr.attr,
183 &vdo_pool_discards_active_attr.attr,
184 &vdo_pool_discards_limit_attr.attr,
185 &vdo_pool_discards_maximum_attr.attr,
186 &vdo_pool_instance_attr.attr,
187 &vdo_pool_requests_active_attr.attr,
188 &vdo_pool_requests_limit_attr.attr,
189 &vdo_pool_requests_maximum_attr.attr,
192 ATTRIBUTE_GROUPS(pool);
194 const struct kobj_type vdo_directory_type = {
195 .release = vdo_pool_release,
196 .sysfs_ops = &vdo_pool_sysfs_ops,
197 .default_groups = pool_groups,