VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / ppc64 / kernel / mf_proc.c
1 /*
2  * mf_proc.c
3  * Copyright (C) 2001 Kyle A. Lucke  IBM Corporation
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19 #include <linux/init.h>
20 #include <asm/uaccess.h>
21 #include <asm/iSeries/mf.h>
22
23 static int proc_mf_dump_cmdline(char *page, char **start, off_t off,
24                 int count, int *eof, void *data)
25 {
26         int len = count;
27         char *p;
28
29         if (off) {
30                 *eof = 1;
31                 return 0;
32         }
33
34         len = mf_getCmdLine(page, &len, (u64)data);
35    
36         p = page;
37         while (len < (count - 1)) {
38                 if (!*p || *p == '\n')
39                         break;
40                 p++;
41                 len++;
42         }
43         *p = '\n';
44         p++;
45         *p = 0;
46
47         return p - page;
48 }
49
50 #if 0
51 static int proc_mf_dump_vmlinux(char *page, char **start, off_t off,
52                 int count, int *eof, void *data)
53 {
54         int sizeToGet = count;
55
56         if (!capable(CAP_SYS_ADMIN))
57                 return -EACCES;
58
59         if (mf_getVmlinuxChunk(page, &sizeToGet, off, (u64)data) == 0) {
60                 if (sizeToGet != 0) {
61                         *start = page + off;
62                         return sizeToGet;
63                 }
64                 *eof = 1;
65                 return 0;
66         }
67         *eof = 1;
68         return 0;
69 }
70 #endif
71
72 static int proc_mf_dump_side(char *page, char **start, off_t off,
73                 int count, int *eof, void *data)
74 {
75         int len;
76         char mf_current_side = mf_getSide();
77
78         len = sprintf(page, "%c\n", mf_current_side);
79
80         if (len <= (off + count))
81                 *eof = 1;
82         *start = page + off;
83         len -= off;
84         if (len > count)
85                 len = count;
86         if (len < 0)
87                 len = 0;
88         return len;                     
89 }
90
91 static int proc_mf_change_side(struct file *file, const char __user *buffer,
92                 unsigned long count, void *data)
93 {
94         char stkbuf[10];
95
96         if (!capable(CAP_SYS_ADMIN))
97                 return -EACCES;
98
99         if (count > (sizeof(stkbuf) - 1))
100                 count = sizeof(stkbuf) - 1;
101         if (copy_from_user(stkbuf, buffer, count))
102                 return -EFAULT;
103         stkbuf[count] = 0;
104         if ((*stkbuf != 'A') && (*stkbuf != 'B') &&
105             (*stkbuf != 'C') && (*stkbuf != 'D')) {
106                 printk(KERN_ERR "mf_proc.c: proc_mf_change_side: invalid side\n");
107                 return -EINVAL;
108         }
109
110         mf_setSide(*stkbuf);
111
112         return count;
113 }
114
115 static int proc_mf_dump_src(char *page, char **start, off_t off,
116                 int count, int *eof, void *data)
117 {
118         int len;
119
120         mf_getSrcHistory(page, count);
121         len = count;
122         len -= off;                     
123         if (len < count) {              
124                 *eof = 1;               
125                 if (len <= 0)           
126                         return 0;       
127         } else                          
128                 len = count;            
129         *start = page + off;            
130         return len;                     
131 }
132
133 static int proc_mf_change_src(struct file *file, const char __user *buffer,
134                 unsigned long count, void *data)
135 {
136         char stkbuf[10];
137
138         if (!capable(CAP_SYS_ADMIN))
139                 return -EACCES;
140
141         if ((count < 4) && (count != 1)) {
142                 printk(KERN_ERR "mf_proc: invalid src\n");
143                 return -EINVAL;
144         }
145
146         if (count > (sizeof(stkbuf) - 1))
147                 count = sizeof(stkbuf) - 1;
148         if (copy_from_user(stkbuf, buffer, count))
149                 return -EFAULT;
150
151         if ((count == 1) && (*stkbuf == '\0'))
152                 mf_clearSrc();
153         else
154                 mf_displaySrc(*(u32 *)stkbuf);
155
156         return count;                   
157 }
158
159 static int proc_mf_change_cmdline(struct file *file, const char *buffer,
160                 unsigned long count, void *data)
161 {
162         if (!capable(CAP_SYS_ADMIN))
163                 return -EACCES;
164
165         mf_setCmdLine(buffer, count, (u64)data);
166
167         return count;                   
168 }
169
170 static int proc_mf_change_vmlinux(struct file *file, const char *buffer,
171                 unsigned long count, void *data)
172 {
173         int rc;
174         if (!capable(CAP_SYS_ADMIN))
175                 return -EACCES;
176
177         rc = mf_setVmlinuxChunk(buffer, count, file->f_pos, (u64)data);
178         if (rc < 0)
179                 return rc;
180
181         file->f_pos += count;
182
183         return count;                   
184 }
185
186 static int __init mf_proc_init(void)
187 {
188         struct proc_dir_entry *mf_proc_root;
189         struct proc_dir_entry *ent;
190         struct proc_dir_entry *mf;
191         char name[2];
192         int i;
193
194         mf_proc_root = proc_mkdir("iSeries/mf", NULL);
195         if (!mf_proc_root)
196                 return 1;
197
198         name[1] = '\0';
199         for (i = 0; i < 4; i++) {
200                 name[0] = 'A' + i;
201                 mf = proc_mkdir(name, mf_proc_root);
202                 if (!mf)
203                         return 1;
204
205                 ent = create_proc_entry("cmdline", S_IFREG|S_IRUSR|S_IWUSR, mf);
206                 if (!ent)
207                         return 1;
208                 ent->nlink = 1;
209                 ent->data = (void *)(long)i;
210                 ent->read_proc = proc_mf_dump_cmdline;
211                 ent->write_proc = proc_mf_change_cmdline;
212
213                 if (i == 3)     /* no vmlinux entry for 'D' */
214                         continue;
215
216                 ent = create_proc_entry("vmlinux", S_IFREG|S_IWUSR, mf);
217                 if (!ent)
218                         return 1;
219                 ent->nlink = 1;
220                 ent->data = (void *)(long)i;
221 #if 0
222                 if (i == 3) {
223                         /*
224                          * if we had a 'D' vmlinux entry, it would only
225                          * be readable.
226                          */
227                         ent->read_proc = proc_mf_dump_vmlinux;
228                         ent->write_proc = NULL;
229                 } else
230 #endif
231                 {
232                         ent->write_proc = proc_mf_change_vmlinux;
233                         ent->read_proc = NULL;
234                 }
235         }
236
237         ent = create_proc_entry("side", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
238         if (!ent)
239                 return 1;
240         ent->nlink = 1;
241         ent->data = (void *)0;
242         ent->read_proc = proc_mf_dump_side;
243         ent->write_proc = proc_mf_change_side;
244
245         ent = create_proc_entry("src", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
246         if (!ent)
247                 return 1;
248         ent->nlink = 1;
249         ent->data = (void *)0;
250         ent->read_proc = proc_mf_dump_src;
251         ent->write_proc = proc_mf_change_src;
252
253         return 0;
254 }
255
256 __initcall(mf_proc_init);