- patches.rt/0001-sched-count-of-queued-RT-tasks.patch: Delete.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / balloon / sysfs.c
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 <linux/module.h>
37 #include "common.h"
38
39 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
40 #include <xen/platform-compat.h>
41 #endif
42
43 #define BALLOON_CLASS_NAME "memory"
44
45 #define BALLOON_SHOW(name, format, args...)                     \
46         static ssize_t show_##name(struct sys_device *dev,      \
47                                    char *buf)                   \
48         {                                                       \
49                 return sprintf(buf, format, ##args);            \
50         }                                                       \
51         static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
52
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));
60
61 static ssize_t show_target_kb(struct sys_device *dev, char *buf)
62 {
63         return sprintf(buf, "%lu\n", PAGES2KB(bs.target_pages));
64 }
65
66 static ssize_t store_target_kb(struct sys_device *dev,
67                                const char *buf,
68                                size_t count)
69 {
70         char memstring[64], *endchar;
71         unsigned long long target_bytes;
72
73         if (!capable(CAP_SYS_ADMIN))
74                 return -EPERM;
75         
76         if (count <= 1)
77                 return -EBADMSG; /* runt */
78         if (count > sizeof(memstring))
79                 return -EFBIG;   /* too long */
80         strcpy(memstring, buf);
81         
82         target_bytes = memparse(memstring, &endchar);
83         balloon_set_new_target(target_bytes >> PAGE_SHIFT);
84         
85         return count;
86 }
87
88 static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
89                    show_target_kb, store_target_kb);
90
91 static struct sysdev_attribute *balloon_attrs[] = {
92         &attr_target_kb,
93 };
94
95 static struct attribute *balloon_info_attrs[] = {
96         &attr_current_kb.attr,
97         &attr_low_kb.attr,
98         &attr_high_kb.attr,
99         &attr_hard_limit_kb.attr,
100         &attr_driver_kb.attr,
101         NULL
102 };
103
104 static struct attribute_group balloon_info_group = {
105         .name = "info",
106         .attrs = balloon_info_attrs,
107 };
108
109 static struct sysdev_class balloon_sysdev_class = {
110         set_kset_name(BALLOON_CLASS_NAME),
111 };
112
113 static struct sys_device balloon_sysdev;
114
115 static int register_balloon(struct sys_device *sysdev)
116 {
117         int i, error;
118
119         error = sysdev_class_register(&balloon_sysdev_class);
120         if (error)
121                 return error;
122
123         sysdev->id = 0;
124         sysdev->cls = &balloon_sysdev_class;
125
126         error = sysdev_register(sysdev);
127         if (error) {
128                 sysdev_class_unregister(&balloon_sysdev_class);
129                 return error;
130         }
131
132         for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
133                 error = sysdev_create_file(sysdev, balloon_attrs[i]);
134                 if (error)
135                         goto fail;
136         }
137
138         error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
139         if (error)
140                 goto fail;
141         
142         return 0;
143
144  fail:
145         while (--i >= 0)
146                 sysdev_remove_file(sysdev, balloon_attrs[i]);
147         sysdev_unregister(sysdev);
148         sysdev_class_unregister(&balloon_sysdev_class);
149         return error;
150 }
151
152 static void unregister_balloon(struct sys_device *sysdev)
153 {
154         int i;
155
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);
161 }
162
163 int balloon_sysfs_init(void)
164 {
165         return register_balloon(&balloon_sysdev);
166 }
167
168 void balloon_sysfs_exit(void)
169 {
170         unregister_balloon(&balloon_sysdev);
171 }