nodemask: use new node_to_cpumask_ptr function
[safe/jmp/linux-2.6] / drivers / base / node.c
1 /*
2  * drivers/base/node.c - basic Node class support
3  */
4
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <linux/node.h>
10 #include <linux/hugetlb.h>
11 #include <linux/cpumask.h>
12 #include <linux/topology.h>
13 #include <linux/nodemask.h>
14 #include <linux/cpu.h>
15 #include <linux/device.h>
16
17 static struct sysdev_class node_class = {
18         .name = "node",
19 };
20
21
22 static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
23 {
24         struct node *node_dev = to_node(dev);
25         node_to_cpumask_ptr(mask, node_dev->sysdev.id);
26         int len;
27
28         /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
29         BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
30
31         len = cpumask_scnprintf(buf, PAGE_SIZE-2, *mask);
32         buf[len++] = '\n';
33         buf[len] = '\0';
34         return len;
35 }
36
37 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
38
39 #define K(x) ((x) << (PAGE_SHIFT - 10))
40 static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
41 {
42         int n;
43         int nid = dev->id;
44         struct sysinfo i;
45
46         si_meminfo_node(&i, nid);
47
48         n = sprintf(buf, "\n"
49                        "Node %d MemTotal:     %8lu kB\n"
50                        "Node %d MemFree:      %8lu kB\n"
51                        "Node %d MemUsed:      %8lu kB\n"
52                        "Node %d Active:       %8lu kB\n"
53                        "Node %d Inactive:     %8lu kB\n"
54 #ifdef CONFIG_HIGHMEM
55                        "Node %d HighTotal:    %8lu kB\n"
56                        "Node %d HighFree:     %8lu kB\n"
57                        "Node %d LowTotal:     %8lu kB\n"
58                        "Node %d LowFree:      %8lu kB\n"
59 #endif
60                        "Node %d Dirty:        %8lu kB\n"
61                        "Node %d Writeback:    %8lu kB\n"
62                        "Node %d FilePages:    %8lu kB\n"
63                        "Node %d Mapped:       %8lu kB\n"
64                        "Node %d AnonPages:    %8lu kB\n"
65                        "Node %d PageTables:   %8lu kB\n"
66                        "Node %d NFS_Unstable: %8lu kB\n"
67                        "Node %d Bounce:       %8lu kB\n"
68                        "Node %d Slab:         %8lu kB\n"
69                        "Node %d SReclaimable: %8lu kB\n"
70                        "Node %d SUnreclaim:   %8lu kB\n",
71                        nid, K(i.totalram),
72                        nid, K(i.freeram),
73                        nid, K(i.totalram - i.freeram),
74                        nid, node_page_state(nid, NR_ACTIVE),
75                        nid, node_page_state(nid, NR_INACTIVE),
76 #ifdef CONFIG_HIGHMEM
77                        nid, K(i.totalhigh),
78                        nid, K(i.freehigh),
79                        nid, K(i.totalram - i.totalhigh),
80                        nid, K(i.freeram - i.freehigh),
81 #endif
82                        nid, K(node_page_state(nid, NR_FILE_DIRTY)),
83                        nid, K(node_page_state(nid, NR_WRITEBACK)),
84                        nid, K(node_page_state(nid, NR_FILE_PAGES)),
85                        nid, K(node_page_state(nid, NR_FILE_MAPPED)),
86                        nid, K(node_page_state(nid, NR_ANON_PAGES)),
87                        nid, K(node_page_state(nid, NR_PAGETABLE)),
88                        nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
89                        nid, K(node_page_state(nid, NR_BOUNCE)),
90                        nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
91                                 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
92                        nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
93                        nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
94         n += hugetlb_report_node_meminfo(nid, buf + n);
95         return n;
96 }
97
98 #undef K
99 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
100
101 static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
102 {
103         return sprintf(buf,
104                        "numa_hit %lu\n"
105                        "numa_miss %lu\n"
106                        "numa_foreign %lu\n"
107                        "interleave_hit %lu\n"
108                        "local_node %lu\n"
109                        "other_node %lu\n",
110                        node_page_state(dev->id, NUMA_HIT),
111                        node_page_state(dev->id, NUMA_MISS),
112                        node_page_state(dev->id, NUMA_FOREIGN),
113                        node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
114                        node_page_state(dev->id, NUMA_LOCAL),
115                        node_page_state(dev->id, NUMA_OTHER));
116 }
117 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
118
119 static ssize_t node_read_distance(struct sys_device * dev, char * buf)
120 {
121         int nid = dev->id;
122         int len = 0;
123         int i;
124
125         /* buf currently PAGE_SIZE, need ~4 chars per node */
126         BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
127
128         for_each_online_node(i)
129                 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
130
131         len += sprintf(buf + len, "\n");
132         return len;
133 }
134 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
135
136
137 /*
138  * register_node - Setup a sysfs device for a node.
139  * @num - Node number to use when creating the device.
140  *
141  * Initialize and register the node device.
142  */
143 int register_node(struct node *node, int num, struct node *parent)
144 {
145         int error;
146
147         node->sysdev.id = num;
148         node->sysdev.cls = &node_class;
149         error = sysdev_register(&node->sysdev);
150
151         if (!error){
152                 sysdev_create_file(&node->sysdev, &attr_cpumap);
153                 sysdev_create_file(&node->sysdev, &attr_meminfo);
154                 sysdev_create_file(&node->sysdev, &attr_numastat);
155                 sysdev_create_file(&node->sysdev, &attr_distance);
156         }
157         return error;
158 }
159
160 /**
161  * unregister_node - unregister a node device
162  * @node: node going away
163  *
164  * Unregisters a node device @node.  All the devices on the node must be
165  * unregistered before calling this function.
166  */
167 void unregister_node(struct node *node)
168 {
169         sysdev_remove_file(&node->sysdev, &attr_cpumap);
170         sysdev_remove_file(&node->sysdev, &attr_meminfo);
171         sysdev_remove_file(&node->sysdev, &attr_numastat);
172         sysdev_remove_file(&node->sysdev, &attr_distance);
173
174         sysdev_unregister(&node->sysdev);
175 }
176
177 struct node node_devices[MAX_NUMNODES];
178
179 /*
180  * register cpu under node
181  */
182 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
183 {
184         if (node_online(nid)) {
185                 struct sys_device *obj = get_cpu_sysdev(cpu);
186                 if (!obj)
187                         return 0;
188                 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
189                                          &obj->kobj,
190                                          kobject_name(&obj->kobj));
191          }
192
193         return 0;
194 }
195
196 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
197 {
198         if (node_online(nid)) {
199                 struct sys_device *obj = get_cpu_sysdev(cpu);
200                 if (obj)
201                         sysfs_remove_link(&node_devices[nid].sysdev.kobj,
202                                          kobject_name(&obj->kobj));
203         }
204         return 0;
205 }
206
207 int register_one_node(int nid)
208 {
209         int error = 0;
210         int cpu;
211
212         if (node_online(nid)) {
213                 int p_node = parent_node(nid);
214                 struct node *parent = NULL;
215
216                 if (p_node != nid)
217                         parent = &node_devices[p_node];
218
219                 error = register_node(&node_devices[nid], nid, parent);
220
221                 /* link cpu under this node */
222                 for_each_present_cpu(cpu) {
223                         if (cpu_to_node(cpu) == nid)
224                                 register_cpu_under_node(cpu, nid);
225                 }
226         }
227
228         return error;
229
230 }
231
232 void unregister_one_node(int nid)
233 {
234         unregister_node(&node_devices[nid]);
235 }
236
237 /*
238  * node states attributes
239  */
240
241 static ssize_t print_nodes_state(enum node_states state, char *buf)
242 {
243         int n;
244
245         n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
246         if (n > 0 && PAGE_SIZE > n + 1) {
247                 *(buf + n++) = '\n';
248                 *(buf + n++) = '\0';
249         }
250         return n;
251 }
252
253 static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
254 {
255         return print_nodes_state(N_POSSIBLE, buf);
256 }
257
258 static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
259 {
260         return print_nodes_state(N_ONLINE, buf);
261 }
262
263 static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
264                                                 char *buf)
265 {
266         return print_nodes_state(N_NORMAL_MEMORY, buf);
267 }
268
269 static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
270 {
271         return print_nodes_state(N_CPU, buf);
272 }
273
274 static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
275 static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
276 static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
277                                                                         NULL);
278 static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
279
280 #ifdef CONFIG_HIGHMEM
281 static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
282                                                  char *buf)
283 {
284         return print_nodes_state(N_HIGH_MEMORY, buf);
285 }
286
287 static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
288                                                                          NULL);
289 #endif
290
291 struct sysdev_class_attribute *node_state_attr[] = {
292         &attr_possible,
293         &attr_online,
294         &attr_has_normal_memory,
295 #ifdef CONFIG_HIGHMEM
296         &attr_has_high_memory,
297 #endif
298         &attr_has_cpu,
299 };
300
301 static int node_states_init(void)
302 {
303         int i;
304         int err = 0;
305
306         for (i = 0;  i < NR_NODE_STATES; i++) {
307                 int ret;
308                 ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
309                 if (!err)
310                         err = ret;
311         }
312         return err;
313 }
314
315 static int __init register_node_type(void)
316 {
317         int ret;
318
319         ret = sysdev_class_register(&node_class);
320         if (!ret)
321                 ret = node_states_init();
322
323         /*
324          * Note:  we're not going to unregister the node class if we fail
325          * to register the node state class attribute files.
326          */
327         return ret;
328 }
329 postcore_initcall(register_node_type);