vserver 1.9.3
[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 ssize_t proc_mf_change_vmlinux(struct file *file, 
171                                       const char __user *buf,
172                                       size_t count, loff_t *ppos)
173 {
174         struct inode * inode = file->f_dentry->d_inode;
175         struct proc_dir_entry * dp = PDE(inode);
176         int rc;
177         if (!capable(CAP_SYS_ADMIN))
178                 return -EACCES;
179
180         rc = mf_setVmlinuxChunk(buf, count, *ppos, (u64)dp->data);
181         if (rc < 0)
182                 return rc;
183
184         *ppos += count;
185
186         return count;                   
187 }
188
189 static struct file_operations proc_vmlinux_operations = {
190         .write          = proc_mf_change_vmlinux,
191 };
192
193 static int __init mf_proc_init(void)
194 {
195         struct proc_dir_entry *mf_proc_root;
196         struct proc_dir_entry *ent;
197         struct proc_dir_entry *mf;
198         char name[2];
199         int i;
200
201         mf_proc_root = proc_mkdir("iSeries/mf", NULL);
202         if (!mf_proc_root)
203                 return 1;
204
205         name[1] = '\0';
206         for (i = 0; i < 4; i++) {
207                 name[0] = 'A' + i;
208                 mf = proc_mkdir(name, mf_proc_root);
209                 if (!mf)
210                         return 1;
211
212                 ent = create_proc_entry("cmdline", S_IFREG|S_IRUSR|S_IWUSR, mf);
213                 if (!ent)
214                         return 1;
215                 ent->nlink = 1;
216                 ent->data = (void *)(long)i;
217                 ent->read_proc = proc_mf_dump_cmdline;
218                 ent->write_proc = proc_mf_change_cmdline;
219
220                 if (i == 3)     /* no vmlinux entry for 'D' */
221                         continue;
222
223                 ent = create_proc_entry("vmlinux", S_IFREG|S_IWUSR, mf);
224                 if (!ent)
225                         return 1;
226                 ent->nlink = 1;
227                 ent->data = (void *)(long)i;
228                 ent->proc_fops = &proc_vmlinux_operations;
229         }
230
231         ent = create_proc_entry("side", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
232         if (!ent)
233                 return 1;
234         ent->nlink = 1;
235         ent->data = (void *)0;
236         ent->read_proc = proc_mf_dump_side;
237         ent->write_proc = proc_mf_change_side;
238
239         ent = create_proc_entry("src", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
240         if (!ent)
241                 return 1;
242         ent->nlink = 1;
243         ent->data = (void *)0;
244         ent->read_proc = proc_mf_dump_src;
245         ent->write_proc = proc_mf_change_src;
246
247         return 0;
248 }
249
250 __initcall(mf_proc_init);