fedora core 6 1.2949 + vserver 2.2.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_path.dentry,buffer)))
159                         goto out;
160         }
161         pr_debug("%s: count = %zd, 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         /* if buf is assumed to contain a string, terminate it by \0,
195            so e.g. sscanf() can scan the string easily */
196         buffer->page[count] = 0;
197         return error ? -EFAULT : count;
198 }
199
200
201 /**
202  *      flush_write_buffer - push buffer to kobject.
203  *      @dentry:        dentry to the attribute
204  *      @buffer:        data buffer for file.
205  *      @count:         number of bytes
206  *
207  *      Get the correct pointers for the kobject and the attribute we're
208  *      dealing with, then call the store() method for the attribute, 
209  *      passing the buffer that we acquired in fill_write_buffer().
210  */
211
212 static int 
213 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
214 {
215         struct attribute * attr = to_attr(dentry);
216         struct kobject * kobj = to_kobj(dentry->d_parent);
217         struct sysfs_ops * ops = buffer->ops;
218
219         return ops->store(kobj,attr,buffer->page,count);
220 }
221
222
223 /**
224  *      sysfs_write_file - write an attribute.
225  *      @file:  file pointer
226  *      @buf:   data to write
227  *      @count: number of bytes
228  *      @ppos:  starting offset
229  *
230  *      Similar to sysfs_read_file(), though working in the opposite direction.
231  *      We allocate and fill the data from the user in fill_write_buffer(),
232  *      then push it to the kobject in flush_write_buffer().
233  *      There is no easy way for us to know if userspace is only doing a partial
234  *      write, so we don't support them. We expect the entire buffer to come
235  *      on the first write. 
236  *      Hint: if you're writing a value, first read the file, modify only the
237  *      the value you're changing, then write entire buffer back. 
238  */
239
240 static ssize_t
241 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
242 {
243         struct sysfs_buffer * buffer = file->private_data;
244         ssize_t len;
245
246         down(&buffer->sem);
247         len = fill_write_buffer(buffer, buf, count);
248         if (len > 0)
249                 len = flush_write_buffer(file->f_path.dentry, buffer, len);
250         if (len > 0)
251                 *ppos += len;
252         up(&buffer->sem);
253         return len;
254 }
255
256 static int check_perm(struct inode * inode, struct file * file)
257 {
258         struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
259         struct attribute * attr = to_attr(file->f_path.dentry);
260         struct sysfs_buffer * buffer;
261         struct sysfs_ops * ops = NULL;
262         int error = 0;
263
264         if (!kobj || !attr)
265                 goto Einval;
266
267         /* Grab the module reference for this attribute if we have one */
268         if (!try_module_get(attr->owner)) {
269                 error = -ENODEV;
270                 goto Done;
271         }
272
273         /* if the kobject has no ktype, then we assume that it is a subsystem
274          * itself, and use ops for it.
275          */
276         if (kobj->kset && kobj->kset->ktype)
277                 ops = kobj->kset->ktype->sysfs_ops;
278         else if (kobj->ktype)
279                 ops = kobj->ktype->sysfs_ops;
280         else
281                 ops = &subsys_sysfs_ops;
282
283         /* No sysfs operations, either from having no subsystem,
284          * or the subsystem have no operations.
285          */
286         if (!ops)
287                 goto Eaccess;
288
289         /* File needs write support.
290          * The inode's perms must say it's ok, 
291          * and we must have a store method.
292          */
293         if (file->f_mode & FMODE_WRITE) {
294
295                 if (!(inode->i_mode & S_IWUGO) || !ops->store)
296                         goto Eaccess;
297
298         }
299
300         /* File needs read support.
301          * The inode's perms must say it's ok, and we there
302          * must be a show method for it.
303          */
304         if (file->f_mode & FMODE_READ) {
305                 if (!(inode->i_mode & S_IRUGO) || !ops->show)
306                         goto Eaccess;
307         }
308
309         /* No error? Great, allocate a buffer for the file, and store it
310          * it in file->private_data for easy access.
311          */
312         buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
313         if (buffer) {
314                 init_MUTEX(&buffer->sem);
315                 buffer->needs_read_fill = 1;
316                 buffer->ops = ops;
317                 file->private_data = buffer;
318         } else
319                 error = -ENOMEM;
320         goto Done;
321
322  Einval:
323         error = -EINVAL;
324         goto Done;
325  Eaccess:
326         error = -EACCES;
327         module_put(attr->owner);
328  Done:
329         if (error && kobj)
330                 kobject_put(kobj);
331         return error;
332 }
333
334 char last_sysfs_file[PATH_MAX];
335
336 static int sysfs_open_file(struct inode * inode, struct file * filp)
337 {
338         char *p = d_path(filp->f_dentry, sysfs_mount, last_sysfs_file,
339                         sizeof(last_sysfs_file));
340         if (p)
341                 memmove(last_sysfs_file, p, strlen(p) + 1);
342         return check_perm(inode,filp);
343 }
344
345 static int sysfs_release(struct inode * inode, struct file * filp)
346 {
347         struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
348         struct attribute * attr = to_attr(filp->f_path.dentry);
349         struct module * owner = attr->owner;
350         struct sysfs_buffer * buffer = filp->private_data;
351
352         if (kobj) 
353                 kobject_put(kobj);
354         /* After this point, attr should not be accessed. */
355         module_put(owner);
356
357         if (buffer) {
358                 if (buffer->page)
359                         free_page((unsigned long)buffer->page);
360                 kfree(buffer);
361         }
362         return 0;
363 }
364
365 /* Sysfs attribute files are pollable.  The idea is that you read
366  * the content and then you use 'poll' or 'select' to wait for
367  * the content to change.  When the content changes (assuming the
368  * manager for the kobject supports notification), poll will
369  * return POLLERR|POLLPRI, and select will return the fd whether
370  * it is waiting for read, write, or exceptions.
371  * Once poll/select indicates that the value has changed, you
372  * need to close and re-open the file, as simply seeking and reading
373  * again will not get new data, or reset the state of 'poll'.
374  * Reminder: this only works for attributes which actively support
375  * it, and it is not possible to test an attribute from userspace
376  * to see if it supports poll (Nether 'poll' or 'select' return
377  * an appropriate error code).  When in doubt, set a suitable timeout value.
378  */
379 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
380 {
381         struct sysfs_buffer * buffer = filp->private_data;
382         struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
383         struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
384         int res = 0;
385
386         poll_wait(filp, &kobj->poll, wait);
387
388         if (buffer->event != atomic_read(&sd->s_event)) {
389                 res = POLLERR|POLLPRI;
390                 buffer->needs_read_fill = 1;
391         }
392
393         return res;
394 }
395
396
397 static struct dentry *step_down(struct dentry *dir, const char * name)
398 {
399         struct dentry * de;
400
401         if (dir == NULL || dir->d_inode == NULL)
402                 return NULL;
403
404         mutex_lock(&dir->d_inode->i_mutex);
405         de = lookup_one_len(name, dir, strlen(name));
406         mutex_unlock(&dir->d_inode->i_mutex);
407         dput(dir);
408         if (IS_ERR(de))
409                 return NULL;
410         if (de->d_inode == NULL) {
411                 dput(de);
412                 return NULL;
413         }
414         return de;
415 }
416
417 void sysfs_notify(struct kobject * k, char *dir, char *attr)
418 {
419         struct dentry *de = k->dentry;
420         if (de)
421                 dget(de);
422         if (de && dir)
423                 de = step_down(de, dir);
424         if (de && attr)
425                 de = step_down(de, attr);
426         if (de) {
427                 struct sysfs_dirent * sd = de->d_fsdata;
428                 if (sd)
429                         atomic_inc(&sd->s_event);
430                 wake_up_interruptible(&k->poll);
431                 dput(de);
432         }
433 }
434 EXPORT_SYMBOL_GPL(sysfs_notify);
435
436 const struct file_operations sysfs_file_operations = {
437         .read           = sysfs_read_file,
438         .write          = sysfs_write_file,
439         .llseek         = generic_file_llseek,
440         .open           = sysfs_open_file,
441         .release        = sysfs_release,
442         .poll           = sysfs_poll,
443 };
444
445
446 int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
447 {
448         struct sysfs_dirent * parent_sd = dir->d_fsdata;
449         umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
450         int error = -EEXIST;
451
452         mutex_lock(&dir->d_inode->i_mutex);
453         if (!sysfs_dirent_exist(parent_sd, attr->name))
454                 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
455                                           mode, type);
456         mutex_unlock(&dir->d_inode->i_mutex);
457
458         return error;
459 }
460
461
462 /**
463  *      sysfs_create_file - create an attribute file for an object.
464  *      @kobj:  object we're creating for. 
465  *      @attr:  atrribute descriptor.
466  */
467
468 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
469 {
470         BUG_ON(!kobj || !kobj->dentry || !attr);
471
472         return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
473
474 }
475
476
477 /**
478  * sysfs_update_file - update the modified timestamp on an object attribute.
479  * @kobj: object we're acting for.
480  * @attr: attribute descriptor.
481  */
482 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
483 {
484         struct dentry * dir = kobj->dentry;
485         struct dentry * victim;
486         int res = -ENOENT;
487
488         mutex_lock(&dir->d_inode->i_mutex);
489         victim = lookup_one_len(attr->name, dir, strlen(attr->name));
490         if (!IS_ERR(victim)) {
491                 /* make sure dentry is really there */
492                 if (victim->d_inode && 
493                     (victim->d_parent->d_inode == dir->d_inode)) {
494                         victim->d_inode->i_mtime = CURRENT_TIME;
495                         fsnotify_modify(victim);
496                         res = 0;
497                 } else
498                         d_drop(victim);
499                 
500                 /**
501                  * Drop the reference acquired from lookup_one_len() above.
502                  */
503                 dput(victim);
504         }
505         mutex_unlock(&dir->d_inode->i_mutex);
506
507         return res;
508 }
509
510
511 /**
512  * sysfs_chmod_file - update the modified mode value on an object attribute.
513  * @kobj: object we're acting for.
514  * @attr: attribute descriptor.
515  * @mode: file permissions.
516  *
517  */
518 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
519 {
520         struct dentry *dir = kobj->dentry;
521         struct dentry *victim;
522         struct inode * inode;
523         struct iattr newattrs;
524         int res = -ENOENT;
525
526         mutex_lock(&dir->d_inode->i_mutex);
527         victim = lookup_one_len(attr->name, dir, strlen(attr->name));
528         if (!IS_ERR(victim)) {
529                 if (victim->d_inode &&
530                     (victim->d_parent->d_inode == dir->d_inode)) {
531                         inode = victim->d_inode;
532                         mutex_lock(&inode->i_mutex);
533                         newattrs.ia_mode = (mode & S_IALLUGO) |
534                                                 (inode->i_mode & ~S_IALLUGO);
535                         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
536                         res = notify_change(victim, &newattrs);
537                         mutex_unlock(&inode->i_mutex);
538                 }
539                 dput(victim);
540         }
541         mutex_unlock(&dir->d_inode->i_mutex);
542
543         return res;
544 }
545 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
546
547
548 /**
549  *      sysfs_remove_file - remove an object attribute.
550  *      @kobj:  object we're acting for.
551  *      @attr:  attribute descriptor.
552  *
553  *      Hash the attribute name and kill the victim.
554  */
555
556 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
557 {
558         sysfs_hash_and_remove(kobj->dentry,attr->name);
559 }
560
561
562 EXPORT_SYMBOL_GPL(sysfs_create_file);
563 EXPORT_SYMBOL_GPL(sysfs_remove_file);
564 EXPORT_SYMBOL_GPL(sysfs_update_file);