Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / sysfs / file.c
1 /*
2  * file.c - operations for regular (text) files.
3  */
4
5 #include <linux/module.h>
6 #include <linux/fsnotify.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
9 #include <linux/poll.h>
10 #include <linux/limits.h>
11 #include <asm/uaccess.h>
12 #include <asm/semaphore.h>
13
14 #include "sysfs.h"
15
16 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
17 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
18
19 /*
20  * Subsystem file operations.
21  * These operations allow subsystems to have files that can be 
22  * read/written. 
23  */
24 static ssize_t 
25 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
26 {
27         struct subsystem * s = to_subsys(kobj);
28         struct subsys_attribute * sattr = to_sattr(attr);
29         ssize_t ret = -EIO;
30
31         if (sattr->show)
32                 ret = sattr->show(s,page);
33         return ret;
34 }
35
36 static ssize_t 
37 subsys_attr_store(struct kobject * kobj, struct attribute * attr, 
38                   const char * page, size_t count)
39 {
40         struct subsystem * s = to_subsys(kobj);
41         struct subsys_attribute * sattr = to_sattr(attr);
42         ssize_t ret = -EIO;
43
44         if (sattr->store)
45                 ret = sattr->store(s,page,count);
46         return ret;
47 }
48
49 static struct sysfs_ops subsys_sysfs_ops = {
50         .show   = subsys_attr_show,
51         .store  = subsys_attr_store,
52 };
53
54
55 struct sysfs_buffer {
56         size_t                  count;
57         loff_t                  pos;
58         char                    * page;
59         struct sysfs_ops        * ops;
60         struct semaphore        sem;
61         int                     needs_read_fill;
62         int                     event;
63 };
64
65
66 /**
67  *      fill_read_buffer - allocate and fill buffer from object.
68  *      @dentry:        dentry pointer.
69  *      @buffer:        data buffer for file.
70  *
71  *      Allocate @buffer->page, if it hasn't been already, then call the
72  *      kobject's show() method to fill the buffer with this attribute's 
73  *      data. 
74  *      This is called only once, on the file's first read. 
75  */
76 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
77 {
78         struct sysfs_dirent * sd = dentry->d_fsdata;
79         struct attribute * attr = to_attr(dentry);
80         struct kobject * kobj = to_kobj(dentry->d_parent);
81         struct sysfs_ops * ops = buffer->ops;
82         int ret = 0;
83         ssize_t count;
84
85         if (!buffer->page)
86                 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
87         if (!buffer->page)
88                 return -ENOMEM;
89
90         buffer->event = atomic_read(&sd->s_event);
91         count = ops->show(kobj,attr,buffer->page);
92         buffer->needs_read_fill = 0;
93         BUG_ON(count > (ssize_t)PAGE_SIZE);
94         if (count >= 0)
95                 buffer->count = count;
96         else
97                 ret = count;
98         return ret;
99 }
100
101
102 /**
103  *      flush_read_buffer - push buffer to userspace.
104  *      @buffer:        data buffer for file.
105  *      @buf:           user-passed buffer.
106  *      @count:         number of bytes requested.
107  *      @ppos:          file position.
108  *
109  *      Copy the buffer we filled in fill_read_buffer() to userspace.
110  *      This is done at the reader's leisure, copying and advancing 
111  *      the amount they specify each time.
112  *      This may be called continuously until the buffer is empty.
113  */
114 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
115                              size_t count, loff_t * ppos)
116 {
117         int error;
118
119         if (*ppos > buffer->count)
120                 return 0;
121
122         if (count > (buffer->count - *ppos))
123                 count = buffer->count - *ppos;
124
125         error = copy_to_user(buf,buffer->page + *ppos,count);
126         if (!error)
127                 *ppos += count;
128         return error ? -EFAULT : count;
129 }
130
131 /**
132  *      sysfs_read_file - read an attribute. 
133  *      @file:  file pointer.
134  *      @buf:   buffer to fill.
135  *      @count: number of bytes to read.
136  *      @ppos:  starting offset in file.
137  *
138  *      Userspace wants to read an attribute file. The attribute descriptor
139  *      is in the file's ->d_fsdata. The target object is in the directory's
140  *      ->d_fsdata.
141  *
142  *      We call fill_read_buffer() to allocate and fill the buffer from the
143  *      object's show() method exactly once (if the read is happening from
144  *      the beginning of the file). That should fill the entire buffer with
145  *      all the data the object has to offer for that attribute.
146  *      We then call flush_read_buffer() to copy the buffer to userspace
147  *      in the increments specified.
148  */
149
150 static ssize_t
151 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
152 {
153         struct sysfs_buffer * buffer = file->private_data;
154         ssize_t retval = 0;
155
156         down(&buffer->sem);
157         if (buffer->needs_read_fill) {
158                 if ((retval = fill_read_buffer(file->f_dentry,buffer)))
159                         goto out;
160         }
161         pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
162                  __FUNCTION__,count,*ppos,buffer->page);
163         retval = flush_read_buffer(buffer,buf,count,ppos);
164 out:
165         up(&buffer->sem);
166         return retval;
167 }
168
169
170 /**
171  *      fill_write_buffer - copy buffer from userspace.
172  *      @buffer:        data buffer for file.
173  *      @buf:           data from user.
174  *      @count:         number of bytes in @userbuf.
175  *
176  *      Allocate @buffer->page if it hasn't been already, then
177  *      copy the user-supplied buffer into it.
178  */
179
180 static int 
181 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
182 {
183         int error;
184
185         if (!buffer->page)
186                 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
187         if (!buffer->page)
188                 return -ENOMEM;
189
190         if (count >= PAGE_SIZE)
191                 count = PAGE_SIZE - 1;
192         error = copy_from_user(buffer->page,buf,count);
193         buffer->needs_read_fill = 1;
194         return error ? -EFAULT : count;
195 }
196
197
198 /**
199  *      flush_write_buffer - push buffer to kobject.
200  *      @dentry:        dentry to the attribute
201  *      @buffer:        data buffer for file.
202  *      @count:         number of bytes
203  *
204  *      Get the correct pointers for the kobject and the attribute we're
205  *      dealing with, then call the store() method for the attribute, 
206  *      passing the buffer that we acquired in fill_write_buffer().
207  */
208
209 static int 
210 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
211 {
212         struct attribute * attr = to_attr(dentry);
213         struct kobject * kobj = to_kobj(dentry->d_parent);
214         struct sysfs_ops * ops = buffer->ops;
215
216         return ops->store(kobj,attr,buffer->page,count);
217 }
218
219
220 /**
221  *      sysfs_write_file - write an attribute.
222  *      @file:  file pointer
223  *      @buf:   data to write
224  *      @count: number of bytes
225  *      @ppos:  starting offset
226  *
227  *      Similar to sysfs_read_file(), though working in the opposite direction.
228  *      We allocate and fill the data from the user in fill_write_buffer(),
229  *      then push it to the kobject in flush_write_buffer().
230  *      There is no easy way for us to know if userspace is only doing a partial
231  *      write, so we don't support them. We expect the entire buffer to come
232  *      on the first write. 
233  *      Hint: if you're writing a value, first read the file, modify only the
234  *      the value you're changing, then write entire buffer back. 
235  */
236
237 static ssize_t
238 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
239 {
240         struct sysfs_buffer * buffer = file->private_data;
241         ssize_t len;
242
243         down(&buffer->sem);
244         len = fill_write_buffer(buffer, buf, count);
245         if (len > 0)
246                 len = flush_write_buffer(file->f_dentry, buffer, len);
247         if (len > 0)
248                 *ppos += len;
249         up(&buffer->sem);
250         return len;
251 }
252
253 static int check_perm(struct inode * inode, struct file * file)
254 {
255         struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
256         struct attribute * attr = to_attr(file->f_dentry);
257         struct sysfs_buffer * buffer;
258         struct sysfs_ops * ops = NULL;
259         int error = 0;
260
261         if (!kobj || !attr)
262                 goto Einval;
263
264         /* Grab the module reference for this attribute if we have one */
265         if (!try_module_get(attr->owner)) {
266                 error = -ENODEV;
267                 goto Done;
268         }
269
270         /* if the kobject has no ktype, then we assume that it is a subsystem
271          * itself, and use ops for it.
272          */
273         if (kobj->kset && kobj->kset->ktype)
274                 ops = kobj->kset->ktype->sysfs_ops;
275         else if (kobj->ktype)
276                 ops = kobj->ktype->sysfs_ops;
277         else
278                 ops = &subsys_sysfs_ops;
279
280         /* No sysfs operations, either from having no subsystem,
281          * or the subsystem have no operations.
282          */
283         if (!ops)
284                 goto Eaccess;
285
286         /* File needs write support.
287          * The inode's perms must say it's ok, 
288          * and we must have a store method.
289          */
290         if (file->f_mode & FMODE_WRITE) {
291
292                 if (!(inode->i_mode & S_IWUGO) || !ops->store)
293                         goto Eaccess;
294
295         }
296
297         /* File needs read support.
298          * The inode's perms must say it's ok, and we there
299          * must be a show method for it.
300          */
301         if (file->f_mode & FMODE_READ) {
302                 if (!(inode->i_mode & S_IRUGO) || !ops->show)
303                         goto Eaccess;
304         }
305
306         /* No error? Great, allocate a buffer for the file, and store it
307          * it in file->private_data for easy access.
308          */
309         buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
310         if (buffer) {
311                 init_MUTEX(&buffer->sem);
312                 buffer->needs_read_fill = 1;
313                 buffer->ops = ops;
314                 file->private_data = buffer;
315         } else
316                 error = -ENOMEM;
317         goto Done;
318
319  Einval:
320         error = -EINVAL;
321         goto Done;
322  Eaccess:
323         error = -EACCES;
324         module_put(attr->owner);
325  Done:
326         if (error && kobj)
327                 kobject_put(kobj);
328         return error;
329 }
330
331 char last_sysfs_file[PATH_MAX];
332
333 static int sysfs_open_file(struct inode * inode, struct file * filp)
334 {
335         char *p = d_path(filp->f_dentry, sysfs_mount, last_sysfs_file,
336                         sizeof(last_sysfs_file));
337         if (p)
338                 memmove(last_sysfs_file, p, strlen(p) + 1);
339         return check_perm(inode,filp);
340 }
341
342 static int sysfs_release(struct inode * inode, struct file * filp)
343 {
344         struct kobject * kobj = to_kobj(filp->f_dentry->d_parent);
345         struct attribute * attr = to_attr(filp->f_dentry);
346         struct module * owner = attr->owner;
347         struct sysfs_buffer * buffer = filp->private_data;
348
349         if (kobj) 
350                 kobject_put(kobj);
351         /* After this point, attr should not be accessed. */
352         module_put(owner);
353
354         if (buffer) {
355                 if (buffer->page)
356                         free_page((unsigned long)buffer->page);
357                 kfree(buffer);
358         }
359         return 0;
360 }
361
362 /* Sysfs attribute files are pollable.  The idea is that you read
363  * the content and then you use 'poll' or 'select' to wait for
364  * the content to change.  When the content changes (assuming the
365  * manager for the kobject supports notification), poll will
366  * return POLLERR|POLLPRI, and select will return the fd whether
367  * it is waiting for read, write, or exceptions.
368  * Once poll/select indicates that the value has changed, you
369  * need to close and re-open the file, as simply seeking and reading
370  * again will not get new data, or reset the state of 'poll'.
371  * Reminder: this only works for attributes which actively support
372  * it, and it is not possible to test an attribute from userspace
373  * to see if it supports poll (Nether 'poll' or 'select' return
374  * an appropriate error code).  When in doubt, set a suitable timeout value.
375  */
376 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
377 {
378         struct sysfs_buffer * buffer = filp->private_data;
379         struct kobject * kobj = to_kobj(filp->f_dentry->d_parent);
380         struct sysfs_dirent * sd = filp->f_dentry->d_fsdata;
381         int res = 0;
382
383         poll_wait(filp, &kobj->poll, wait);
384
385         if (buffer->event != atomic_read(&sd->s_event)) {
386                 res = POLLERR|POLLPRI;
387                 buffer->needs_read_fill = 1;
388         }
389
390         return res;
391 }
392
393
394 static struct dentry *step_down(struct dentry *dir, const char * name)
395 {
396         struct dentry * de;
397
398         if (dir == NULL || dir->d_inode == NULL)
399                 return NULL;
400
401         mutex_lock(&dir->d_inode->i_mutex);
402         de = lookup_one_len(name, dir, strlen(name));
403         mutex_unlock(&dir->d_inode->i_mutex);
404         dput(dir);
405         if (IS_ERR(de))
406                 return NULL;
407         if (de->d_inode == NULL) {
408                 dput(de);
409                 return NULL;
410         }
411         return de;
412 }
413
414 void sysfs_notify(struct kobject * k, char *dir, char *attr)
415 {
416         struct dentry *de = k->dentry;
417         if (de)
418                 dget(de);
419         if (de && dir)
420                 de = step_down(de, dir);
421         if (de && attr)
422                 de = step_down(de, attr);
423         if (de) {
424                 struct sysfs_dirent * sd = de->d_fsdata;
425                 if (sd)
426                         atomic_inc(&sd->s_event);
427                 wake_up_interruptible(&k->poll);
428                 dput(de);
429         }
430 }
431 EXPORT_SYMBOL_GPL(sysfs_notify);
432
433 const struct file_operations sysfs_file_operations = {
434         .read           = sysfs_read_file,
435         .write          = sysfs_write_file,
436         .llseek         = generic_file_llseek,
437         .open           = sysfs_open_file,
438         .release        = sysfs_release,
439         .poll           = sysfs_poll,
440 };
441
442
443 int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
444 {
445         struct sysfs_dirent * parent_sd = dir->d_fsdata;
446         umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
447         int error = -EEXIST;
448
449         mutex_lock(&dir->d_inode->i_mutex);
450         if (!sysfs_dirent_exist(parent_sd, attr->name))
451                 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
452                                           mode, type);
453         mutex_unlock(&dir->d_inode->i_mutex);
454
455         return error;
456 }
457
458
459 /**
460  *      sysfs_create_file - create an attribute file for an object.
461  *      @kobj:  object we're creating for. 
462  *      @attr:  atrribute descriptor.
463  */
464
465 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
466 {
467         BUG_ON(!kobj || !kobj->dentry || !attr);
468
469         return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
470
471 }
472
473
474 /**
475  * sysfs_update_file - update the modified timestamp on an object attribute.
476  * @kobj: object we're acting for.
477  * @attr: attribute descriptor.
478  */
479 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
480 {
481         struct dentry * dir = kobj->dentry;
482         struct dentry * victim;
483         int res = -ENOENT;
484
485         mutex_lock(&dir->d_inode->i_mutex);
486         victim = lookup_one_len(attr->name, dir, strlen(attr->name));
487         if (!IS_ERR(victim)) {
488                 /* make sure dentry is really there */
489                 if (victim->d_inode && 
490                     (victim->d_parent->d_inode == dir->d_inode)) {
491                         victim->d_inode->i_mtime = CURRENT_TIME;
492                         fsnotify_modify(victim);
493                         res = 0;
494                 } else
495                         d_drop(victim);
496                 
497                 /**
498                  * Drop the reference acquired from sysfs_get_dentry() above.
499                  */
500                 dput(victim);
501         }
502         mutex_unlock(&dir->d_inode->i_mutex);
503
504         return res;
505 }
506
507
508 /**
509  * sysfs_chmod_file - update the modified mode value on an object attribute.
510  * @kobj: object we're acting for.
511  * @attr: attribute descriptor.
512  * @mode: file permissions.
513  *
514  */
515 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
516 {
517         struct dentry *dir = kobj->dentry;
518         struct dentry *victim;
519         struct inode * inode;
520         struct iattr newattrs;
521         int res = -ENOENT;
522
523         mutex_lock(&dir->d_inode->i_mutex);
524         victim = lookup_one_len(attr->name, dir, strlen(attr->name));
525         if (!IS_ERR(victim)) {
526                 if (victim->d_inode &&
527                     (victim->d_parent->d_inode == dir->d_inode)) {
528                         inode = victim->d_inode;
529                         mutex_lock(&inode->i_mutex);
530                         newattrs.ia_mode = (mode & S_IALLUGO) |
531                                                 (inode->i_mode & ~S_IALLUGO);
532                         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
533                         res = notify_change(victim, &newattrs);
534                         mutex_unlock(&inode->i_mutex);
535                 }
536                 dput(victim);
537         }
538         mutex_unlock(&dir->d_inode->i_mutex);
539
540         return res;
541 }
542 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
543
544
545 /**
546  *      sysfs_remove_file - remove an object attribute.
547  *      @kobj:  object we're acting for.
548  *      @attr:  attribute descriptor.
549  *
550  *      Hash the attribute name and kill the victim.
551  */
552
553 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
554 {
555         sysfs_hash_and_remove(kobj->dentry,attr->name);
556 }
557
558
559 EXPORT_SYMBOL_GPL(sysfs_create_file);
560 EXPORT_SYMBOL_GPL(sysfs_remove_file);
561 EXPORT_SYMBOL_GPL(sysfs_update_file);