fs: Give dentry to inode_change_ok() instead of inode
[pandora-kernel.git] / fs / proc / proc_sysctl.c
1 /*
2  * /proc/sys support
3  */
4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/poll.h>
7 #include <linux/proc_fs.h>
8 #include <linux/security.h>
9 #include <linux/namei.h>
10 #include "internal.h"
11
12 static const struct dentry_operations proc_sys_dentry_operations;
13 static const struct file_operations proc_sys_file_operations;
14 static const struct inode_operations proc_sys_inode_operations;
15 static const struct file_operations proc_sys_dir_file_operations;
16 static const struct inode_operations proc_sys_dir_operations;
17
18 void proc_sys_poll_notify(struct ctl_table_poll *poll)
19 {
20         if (!poll)
21                 return;
22
23         atomic_inc(&poll->event);
24         wake_up_interruptible(&poll->wait);
25 }
26
27 static struct inode *proc_sys_make_inode(struct super_block *sb,
28                 struct ctl_table_header *head, struct ctl_table *table)
29 {
30         struct inode *inode;
31         struct proc_inode *ei;
32
33         inode = new_inode(sb);
34         if (!inode)
35                 goto out;
36
37         inode->i_ino = get_next_ino();
38
39         sysctl_head_get(head);
40         ei = PROC_I(inode);
41         ei->sysctl = head;
42         ei->sysctl_entry = table;
43
44         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
45         inode->i_mode = table->mode;
46         if (!table->child) {
47                 inode->i_mode |= S_IFREG;
48                 inode->i_op = &proc_sys_inode_operations;
49                 inode->i_fop = &proc_sys_file_operations;
50         } else {
51                 inode->i_mode |= S_IFDIR;
52                 clear_nlink(inode);
53                 inode->i_op = &proc_sys_dir_operations;
54                 inode->i_fop = &proc_sys_dir_file_operations;
55         }
56 out:
57         return inode;
58 }
59
60 static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
61 {
62         int len;
63         for ( ; p->procname; p++) {
64
65                 if (!p->procname)
66                         continue;
67
68                 len = strlen(p->procname);
69                 if (len != name->len)
70                         continue;
71
72                 if (memcmp(p->procname, name->name, len) != 0)
73                         continue;
74
75                 /* I have a match */
76                 return p;
77         }
78         return NULL;
79 }
80
81 static struct ctl_table_header *grab_header(struct inode *inode)
82 {
83         if (PROC_I(inode)->sysctl)
84                 return sysctl_head_grab(PROC_I(inode)->sysctl);
85         else
86                 return sysctl_head_next(NULL);
87 }
88
89 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
90                                         struct nameidata *nd)
91 {
92         struct ctl_table_header *head = grab_header(dir);
93         struct ctl_table *table = PROC_I(dir)->sysctl_entry;
94         struct ctl_table_header *h = NULL;
95         struct qstr *name = &dentry->d_name;
96         struct ctl_table *p;
97         struct inode *inode;
98         struct dentry *err = ERR_PTR(-ENOENT);
99
100         if (IS_ERR(head))
101                 return ERR_CAST(head);
102
103         if (table && !table->child) {
104                 WARN_ON(1);
105                 goto out;
106         }
107
108         table = table ? table->child : head->ctl_table;
109
110         p = find_in_table(table, name);
111         if (!p) {
112                 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
113                         if (h->attached_to != table)
114                                 continue;
115                         p = find_in_table(h->attached_by, name);
116                         if (p)
117                                 break;
118                 }
119         }
120
121         if (!p)
122                 goto out;
123
124         err = ERR_PTR(-ENOMEM);
125         inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
126         if (!inode)
127                 goto out;
128
129         err = NULL;
130         d_set_d_op(dentry, &proc_sys_dentry_operations);
131         d_add(dentry, inode);
132
133 out:
134         if (h)
135                 sysctl_head_finish(h);
136         sysctl_head_finish(head);
137         return err;
138 }
139
140 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
141                 size_t count, loff_t *ppos, int write)
142 {
143         struct inode *inode = filp->f_path.dentry->d_inode;
144         struct ctl_table_header *head = grab_header(inode);
145         struct ctl_table *table = PROC_I(inode)->sysctl_entry;
146         ssize_t error;
147         size_t res;
148
149         if (IS_ERR(head))
150                 return PTR_ERR(head);
151
152         /*
153          * At this point we know that the sysctl was not unregistered
154          * and won't be until we finish.
155          */
156         error = -EPERM;
157         if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
158                 goto out;
159
160         /* if that can happen at all, it should be -EINVAL, not -EISDIR */
161         error = -EINVAL;
162         if (!table->proc_handler)
163                 goto out;
164
165         /* careful: calling conventions are nasty here */
166         res = count;
167         error = table->proc_handler(table, write, buf, &res, ppos);
168         if (!error)
169                 error = res;
170 out:
171         sysctl_head_finish(head);
172
173         return error;
174 }
175
176 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
177                                 size_t count, loff_t *ppos)
178 {
179         return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
180 }
181
182 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
183                                 size_t count, loff_t *ppos)
184 {
185         return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
186 }
187
188 static int proc_sys_open(struct inode *inode, struct file *filp)
189 {
190         struct ctl_table_header *head = grab_header(inode);
191         struct ctl_table *table = PROC_I(inode)->sysctl_entry;
192
193         /* sysctl was unregistered */
194         if (IS_ERR(head))
195                 return PTR_ERR(head);
196
197         if (table->poll)
198                 filp->private_data = proc_sys_poll_event(table->poll);
199
200         sysctl_head_finish(head);
201
202         return 0;
203 }
204
205 static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
206 {
207         struct inode *inode = filp->f_path.dentry->d_inode;
208         struct ctl_table_header *head = grab_header(inode);
209         struct ctl_table *table = PROC_I(inode)->sysctl_entry;
210         unsigned int ret = DEFAULT_POLLMASK;
211         unsigned long event;
212
213         /* sysctl was unregistered */
214         if (IS_ERR(head))
215                 return POLLERR | POLLHUP;
216
217         if (!table->proc_handler)
218                 goto out;
219
220         if (!table->poll)
221                 goto out;
222
223         event = (unsigned long)filp->private_data;
224         poll_wait(filp, &table->poll->wait, wait);
225
226         if (event != atomic_read(&table->poll->event)) {
227                 filp->private_data = proc_sys_poll_event(table->poll);
228                 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
229         }
230
231 out:
232         sysctl_head_finish(head);
233
234         return ret;
235 }
236
237 static int proc_sys_fill_cache(struct file *filp, void *dirent,
238                                 filldir_t filldir,
239                                 struct ctl_table_header *head,
240                                 struct ctl_table *table)
241 {
242         struct dentry *child, *dir = filp->f_path.dentry;
243         struct inode *inode;
244         struct qstr qname;
245         ino_t ino = 0;
246         unsigned type = DT_UNKNOWN;
247
248         qname.name = table->procname;
249         qname.len  = strlen(table->procname);
250         qname.hash = full_name_hash(qname.name, qname.len);
251
252         child = d_lookup(dir, &qname);
253         if (!child) {
254                 child = d_alloc(dir, &qname);
255                 if (child) {
256                         inode = proc_sys_make_inode(dir->d_sb, head, table);
257                         if (!inode) {
258                                 dput(child);
259                                 return -ENOMEM;
260                         } else {
261                                 d_set_d_op(child, &proc_sys_dentry_operations);
262                                 d_add(child, inode);
263                         }
264                 } else {
265                         return -ENOMEM;
266                 }
267         }
268         inode = child->d_inode;
269         ino  = inode->i_ino;
270         type = inode->i_mode >> 12;
271         dput(child);
272         return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
273 }
274
275 static int scan(struct ctl_table_header *head, ctl_table *table,
276                 unsigned long *pos, struct file *file,
277                 void *dirent, filldir_t filldir)
278 {
279
280         for (; table->procname; table++, (*pos)++) {
281                 int res;
282
283                 /* Can't do anything without a proc name */
284                 if (!table->procname)
285                         continue;
286
287                 if (*pos < file->f_pos)
288                         continue;
289
290                 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
291                 if (res)
292                         return res;
293
294                 file->f_pos = *pos + 1;
295         }
296         return 0;
297 }
298
299 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
300 {
301         struct dentry *dentry = filp->f_path.dentry;
302         struct inode *inode = dentry->d_inode;
303         struct ctl_table_header *head = grab_header(inode);
304         struct ctl_table *table = PROC_I(inode)->sysctl_entry;
305         struct ctl_table_header *h = NULL;
306         unsigned long pos;
307         int ret = -EINVAL;
308
309         if (IS_ERR(head))
310                 return PTR_ERR(head);
311
312         if (table && !table->child) {
313                 WARN_ON(1);
314                 goto out;
315         }
316
317         table = table ? table->child : head->ctl_table;
318
319         ret = 0;
320         /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
321         if (filp->f_pos == 0) {
322                 if (filldir(dirent, ".", 1, filp->f_pos,
323                                 inode->i_ino, DT_DIR) < 0)
324                         goto out;
325                 filp->f_pos++;
326         }
327         if (filp->f_pos == 1) {
328                 if (filldir(dirent, "..", 2, filp->f_pos,
329                                 parent_ino(dentry), DT_DIR) < 0)
330                         goto out;
331                 filp->f_pos++;
332         }
333         pos = 2;
334
335         ret = scan(head, table, &pos, filp, dirent, filldir);
336         if (ret)
337                 goto out;
338
339         for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
340                 if (h->attached_to != table)
341                         continue;
342                 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
343                 if (ret) {
344                         sysctl_head_finish(h);
345                         break;
346                 }
347         }
348         ret = 1;
349 out:
350         sysctl_head_finish(head);
351         return ret;
352 }
353
354 static int proc_sys_permission(struct inode *inode, int mask)
355 {
356         /*
357          * sysctl entries that are not writeable,
358          * are _NOT_ writeable, capabilities or not.
359          */
360         struct ctl_table_header *head;
361         struct ctl_table *table;
362         int error;
363
364         /* Executable files are not allowed under /proc/sys/ */
365         if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
366                 return -EACCES;
367
368         head = grab_header(inode);
369         if (IS_ERR(head))
370                 return PTR_ERR(head);
371
372         table = PROC_I(inode)->sysctl_entry;
373         if (!table) /* global root - r-xr-xr-x */
374                 error = mask & MAY_WRITE ? -EACCES : 0;
375         else /* Use the permissions on the sysctl table entry */
376                 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
377
378         sysctl_head_finish(head);
379         return error;
380 }
381
382 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
383 {
384         struct inode *inode = dentry->d_inode;
385         int error;
386
387         if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
388                 return -EPERM;
389
390         error = setattr_prepare(dentry, attr);
391         if (error)
392                 return error;
393
394         if ((attr->ia_valid & ATTR_SIZE) &&
395             attr->ia_size != i_size_read(inode)) {
396                 error = vmtruncate(inode, attr->ia_size);
397                 if (error)
398                         return error;
399         }
400
401         setattr_copy(inode, attr);
402         mark_inode_dirty(inode);
403         return 0;
404 }
405
406 static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
407 {
408         struct inode *inode = dentry->d_inode;
409         struct ctl_table_header *head = grab_header(inode);
410         struct ctl_table *table = PROC_I(inode)->sysctl_entry;
411
412         if (IS_ERR(head))
413                 return PTR_ERR(head);
414
415         generic_fillattr(inode, stat);
416         if (table)
417                 stat->mode = (stat->mode & S_IFMT) | table->mode;
418
419         sysctl_head_finish(head);
420         return 0;
421 }
422
423 static const struct file_operations proc_sys_file_operations = {
424         .open           = proc_sys_open,
425         .poll           = proc_sys_poll,
426         .read           = proc_sys_read,
427         .write          = proc_sys_write,
428         .llseek         = default_llseek,
429 };
430
431 static const struct file_operations proc_sys_dir_file_operations = {
432         .read           = generic_read_dir,
433         .readdir        = proc_sys_readdir,
434         .llseek         = generic_file_llseek,
435 };
436
437 static const struct inode_operations proc_sys_inode_operations = {
438         .permission     = proc_sys_permission,
439         .setattr        = proc_sys_setattr,
440         .getattr        = proc_sys_getattr,
441 };
442
443 static const struct inode_operations proc_sys_dir_operations = {
444         .lookup         = proc_sys_lookup,
445         .permission     = proc_sys_permission,
446         .setattr        = proc_sys_setattr,
447         .getattr        = proc_sys_getattr,
448 };
449
450 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
451 {
452         if (nd->flags & LOOKUP_RCU)
453                 return -ECHILD;
454         return !PROC_I(dentry->d_inode)->sysctl->unregistering;
455 }
456
457 static int proc_sys_delete(const struct dentry *dentry)
458 {
459         return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
460 }
461
462 static int proc_sys_compare(const struct dentry *parent,
463                 const struct inode *pinode,
464                 const struct dentry *dentry, const struct inode *inode,
465                 unsigned int len, const char *str, const struct qstr *name)
466 {
467         struct ctl_table_header *head;
468         /* Although proc doesn't have negative dentries, rcu-walk means
469          * that inode here can be NULL */
470         /* AV: can it, indeed? */
471         if (!inode)
472                 return 1;
473         if (name->len != len)
474                 return 1;
475         if (memcmp(name->name, str, len))
476                 return 1;
477         head = rcu_dereference(PROC_I(inode)->sysctl);
478         return !head || !sysctl_is_seen(head);
479 }
480
481 static const struct dentry_operations proc_sys_dentry_operations = {
482         .d_revalidate   = proc_sys_revalidate,
483         .d_delete       = proc_sys_delete,
484         .d_compare      = proc_sys_compare,
485 };
486
487 int __init proc_sys_init(void)
488 {
489         struct proc_dir_entry *proc_sys_root;
490
491         proc_sys_root = proc_mkdir("sys", NULL);
492         proc_sys_root->proc_iops = &proc_sys_dir_operations;
493         proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
494         proc_sys_root->nlink = 0;
495         return 0;
496 }