1 /******************************************************************************
4 * Xen balloon driver - sysfs interfaces.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation; or, when distributed
9 * separately from the Linux kernel or incorporated into other
10 * software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <linux/capability.h>
32 #include <linux/errno.h>
33 #include <linux/stat.h>
34 #include <linux/string.h>
35 #include <linux/sysdev.h>
36 #include <linux/module.h>
39 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
40 #include <xen/platform-compat.h>
43 #define BALLOON_CLASS_NAME "memory"
45 #define BALLOON_SHOW(name, format, args...) \
46 static ssize_t show_##name(struct sys_device *dev, \
49 return sprintf(buf, format, ##args); \
51 static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
53 BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(bs.current_pages));
54 BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(bs.balloon_low));
55 BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(bs.balloon_high));
56 BALLOON_SHOW(hard_limit_kb,
57 (bs.hard_limit!=~0UL) ? "%lu\n" : "???\n",
58 (bs.hard_limit!=~0UL) ? PAGES2KB(bs.hard_limit) : 0);
59 BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(bs.driver_pages));
61 static ssize_t show_target_kb(struct sys_device *dev, char *buf)
63 return sprintf(buf, "%lu\n", PAGES2KB(bs.target_pages));
66 static ssize_t store_target_kb(struct sys_device *dev,
70 char memstring[64], *endchar;
71 unsigned long long target_bytes;
73 if (!capable(CAP_SYS_ADMIN))
77 return -EBADMSG; /* runt */
78 if (count > sizeof(memstring))
79 return -EFBIG; /* too long */
80 strcpy(memstring, buf);
82 target_bytes = memparse(memstring, &endchar);
83 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
88 static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
89 show_target_kb, store_target_kb);
91 static struct sysdev_attribute *balloon_attrs[] = {
95 static struct attribute *balloon_info_attrs[] = {
96 &attr_current_kb.attr,
99 &attr_hard_limit_kb.attr,
100 &attr_driver_kb.attr,
104 static struct attribute_group balloon_info_group = {
106 .attrs = balloon_info_attrs,
109 static struct sysdev_class balloon_sysdev_class = {
110 set_kset_name(BALLOON_CLASS_NAME),
113 static struct sys_device balloon_sysdev;
115 static int register_balloon(struct sys_device *sysdev)
119 error = sysdev_class_register(&balloon_sysdev_class);
124 sysdev->cls = &balloon_sysdev_class;
126 error = sysdev_register(sysdev);
128 sysdev_class_unregister(&balloon_sysdev_class);
132 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
133 error = sysdev_create_file(sysdev, balloon_attrs[i]);
138 error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
146 sysdev_remove_file(sysdev, balloon_attrs[i]);
147 sysdev_unregister(sysdev);
148 sysdev_class_unregister(&balloon_sysdev_class);
152 static void unregister_balloon(struct sys_device *sysdev)
156 sysfs_remove_group(&sysdev->kobj, &balloon_info_group);
157 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++)
158 sysdev_remove_file(sysdev, balloon_attrs[i]);
159 sysdev_unregister(sysdev);
160 sysdev_class_unregister(&balloon_sysdev_class);
163 int balloon_sysfs_init(void)
165 return register_balloon(&balloon_sysdev);
168 void balloon_sysfs_exit(void)
170 unregister_balloon(&balloon_sysdev);