[34] | 1 | /****************************************************************************** |
---|
| 2 | * balloon/sysfs.c |
---|
| 3 | * |
---|
| 4 | * Xen balloon driver - sysfs interfaces. |
---|
| 5 | * |
---|
| 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: |
---|
| 11 | * |
---|
| 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: |
---|
| 18 | * |
---|
| 19 | * The above copyright notice and this permission notice shall be included in |
---|
| 20 | * all copies or substantial portions of the Software. |
---|
| 21 | * |
---|
| 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 |
---|
| 28 | * IN THE SOFTWARE. |
---|
| 29 | */ |
---|
| 30 | |
---|
| 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 "common.h" |
---|
| 37 | |
---|
| 38 | #ifdef HAVE_XEN_PLATFORM_COMPAT_H |
---|
| 39 | #include <xen/platform-compat.h> |
---|
| 40 | #endif |
---|
| 41 | |
---|
| 42 | #define BALLOON_CLASS_NAME "memory" |
---|
| 43 | |
---|
| 44 | #define BALLOON_SHOW(name, format, args...) \ |
---|
| 45 | static ssize_t show_##name(struct sys_device *dev, \ |
---|
| 46 | char *buf) \ |
---|
| 47 | { \ |
---|
| 48 | return sprintf(buf, format, ##args); \ |
---|
| 49 | } \ |
---|
| 50 | static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL) |
---|
| 51 | |
---|
| 52 | BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(bs.current_pages)); |
---|
| 53 | BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(bs.balloon_low)); |
---|
| 54 | BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(bs.balloon_high)); |
---|
| 55 | BALLOON_SHOW(hard_limit_kb, |
---|
| 56 | (bs.hard_limit!=~0UL) ? "%lu\n" : "???\n", |
---|
| 57 | (bs.hard_limit!=~0UL) ? PAGES2KB(bs.hard_limit) : 0); |
---|
| 58 | BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(bs.driver_pages)); |
---|
| 59 | |
---|
| 60 | static ssize_t show_target_kb(struct sys_device *dev, char *buf) |
---|
| 61 | { |
---|
| 62 | return sprintf(buf, "%lu\n", PAGES2KB(bs.target_pages)); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | static ssize_t store_target_kb(struct sys_device *dev, |
---|
| 66 | const char *buf, |
---|
| 67 | size_t count) |
---|
| 68 | { |
---|
| 69 | char memstring[64], *endchar; |
---|
| 70 | unsigned long long target_bytes; |
---|
| 71 | |
---|
| 72 | if (!capable(CAP_SYS_ADMIN)) |
---|
| 73 | return -EPERM; |
---|
| 74 | |
---|
| 75 | if (count <= 1) |
---|
| 76 | return -EBADMSG; /* runt */ |
---|
| 77 | if (count > sizeof(memstring)) |
---|
| 78 | return -EFBIG; /* too long */ |
---|
| 79 | strcpy(memstring, buf); |
---|
| 80 | |
---|
| 81 | target_bytes = memparse(memstring, &endchar); |
---|
| 82 | balloon_set_new_target(target_bytes >> PAGE_SHIFT); |
---|
| 83 | |
---|
| 84 | return count; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR, |
---|
| 88 | show_target_kb, store_target_kb); |
---|
| 89 | |
---|
| 90 | static struct sysdev_attribute *balloon_attrs[] = { |
---|
| 91 | &attr_target_kb, |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | static struct attribute *balloon_info_attrs[] = { |
---|
| 95 | &attr_current_kb.attr, |
---|
| 96 | &attr_low_kb.attr, |
---|
| 97 | &attr_high_kb.attr, |
---|
| 98 | &attr_hard_limit_kb.attr, |
---|
| 99 | &attr_driver_kb.attr, |
---|
| 100 | NULL |
---|
| 101 | }; |
---|
| 102 | |
---|
| 103 | static struct attribute_group balloon_info_group = { |
---|
| 104 | .name = "info", |
---|
| 105 | .attrs = balloon_info_attrs, |
---|
| 106 | }; |
---|
| 107 | |
---|
| 108 | static struct sysdev_class balloon_sysdev_class = { |
---|
| 109 | set_kset_name(BALLOON_CLASS_NAME), |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | static struct sys_device balloon_sysdev; |
---|
| 113 | |
---|
| 114 | static int register_balloon(struct sys_device *sysdev) |
---|
| 115 | { |
---|
| 116 | int i, error; |
---|
| 117 | |
---|
| 118 | error = sysdev_class_register(&balloon_sysdev_class); |
---|
| 119 | if (error) |
---|
| 120 | return error; |
---|
| 121 | |
---|
| 122 | sysdev->id = 0; |
---|
| 123 | sysdev->cls = &balloon_sysdev_class; |
---|
| 124 | |
---|
| 125 | error = sysdev_register(sysdev); |
---|
| 126 | if (error) { |
---|
| 127 | sysdev_class_unregister(&balloon_sysdev_class); |
---|
| 128 | return error; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) { |
---|
| 132 | error = sysdev_create_file(sysdev, balloon_attrs[i]); |
---|
| 133 | if (error) |
---|
| 134 | goto fail; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | error = sysfs_create_group(&sysdev->kobj, &balloon_info_group); |
---|
| 138 | if (error) |
---|
| 139 | goto fail; |
---|
| 140 | |
---|
| 141 | return 0; |
---|
| 142 | |
---|
| 143 | fail: |
---|
| 144 | while (--i >= 0) |
---|
| 145 | sysdev_remove_file(sysdev, balloon_attrs[i]); |
---|
| 146 | sysdev_unregister(sysdev); |
---|
| 147 | sysdev_class_unregister(&balloon_sysdev_class); |
---|
| 148 | return error; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | static void unregister_balloon(struct sys_device *sysdev) |
---|
| 152 | { |
---|
| 153 | int i; |
---|
| 154 | |
---|
| 155 | sysfs_remove_group(&sysdev->kobj, &balloon_info_group); |
---|
| 156 | for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) |
---|
| 157 | sysdev_remove_file(sysdev, balloon_attrs[i]); |
---|
| 158 | sysdev_unregister(sysdev); |
---|
| 159 | sysdev_class_unregister(&balloon_sysdev_class); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | int balloon_sysfs_init(void) |
---|
| 163 | { |
---|
| 164 | return register_balloon(&balloon_sysdev); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | void balloon_sysfs_exit(void) |
---|
| 168 | { |
---|
| 169 | unregister_balloon(&balloon_sysdev); |
---|
| 170 | } |
---|