sysfs: move sysfs file poll implementation to sysfs_open_dirent
[safe/jmp/linux-2.6] / fs / sysfs / file.c
1 /*
2  * file.c - operations for regular (text) files.
3  */
4
5 #include <linux/module.h>
6 #include <linux/kobject.h>
7 #include <linux/namei.h>
8 #include <linux/poll.h>
9 #include <linux/list.h>
10 #include <linux/mutex.h>
11 #include <asm/uaccess.h>
12
13 #include "sysfs.h"
14
15 #define to_sattr(a) container_of(a,struct subsys_attribute, attr)
16
17 /*
18  * Subsystem file operations.
19  * These operations allow subsystems to have files that can be 
20  * read/written. 
21  */
22 static ssize_t 
23 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
24 {
25         struct kset *kset = to_kset(kobj);
26         struct subsys_attribute * sattr = to_sattr(attr);
27         ssize_t ret = -EIO;
28
29         if (sattr->show)
30                 ret = sattr->show(kset, page);
31         return ret;
32 }
33
34 static ssize_t 
35 subsys_attr_store(struct kobject * kobj, struct attribute * attr, 
36                   const char * page, size_t count)
37 {
38         struct kset *kset = to_kset(kobj);
39         struct subsys_attribute * sattr = to_sattr(attr);
40         ssize_t ret = -EIO;
41
42         if (sattr->store)
43                 ret = sattr->store(kset, page, count);
44         return ret;
45 }
46
47 static struct sysfs_ops subsys_sysfs_ops = {
48         .show   = subsys_attr_show,
49         .store  = subsys_attr_store,
50 };
51
52 /*
53  * There's one sysfs_buffer for each open file and one
54  * sysfs_open_dirent for each sysfs_dirent with one or more open
55  * files.
56  *
57  * filp->private_data points to sysfs_buffer and
58  * sysfs_dirent->s_attr.open points to sysfs_open_dirent.  s_attr.open
59  * is protected by sysfs_open_dirent_lock.
60  */
61 static spinlock_t sysfs_open_dirent_lock = SPIN_LOCK_UNLOCKED;
62
63 struct sysfs_open_dirent {
64         atomic_t                refcnt;
65         atomic_t                event;
66         wait_queue_head_t       poll;
67         struct list_head        buffers; /* goes through sysfs_buffer.list */
68 };
69
70 struct sysfs_buffer {
71         size_t                  count;
72         loff_t                  pos;
73         char                    * page;
74         struct sysfs_ops        * ops;
75         struct mutex            mutex;
76         int                     needs_read_fill;
77         int                     event;
78         struct list_head        list;
79 };
80
81 /**
82  *      fill_read_buffer - allocate and fill buffer from object.
83  *      @dentry:        dentry pointer.
84  *      @buffer:        data buffer for file.
85  *
86  *      Allocate @buffer->page, if it hasn't been already, then call the
87  *      kobject's show() method to fill the buffer with this attribute's 
88  *      data. 
89  *      This is called only once, on the file's first read unless an error
90  *      is returned.
91  */
92 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
93 {
94         struct sysfs_dirent *attr_sd = dentry->d_fsdata;
95         struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
96         struct sysfs_ops * ops = buffer->ops;
97         int ret = 0;
98         ssize_t count;
99
100         if (!buffer->page)
101                 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
102         if (!buffer->page)
103                 return -ENOMEM;
104
105         /* need attr_sd for attr and ops, its parent for kobj */
106         if (!sysfs_get_active_two(attr_sd))
107                 return -ENODEV;
108
109         buffer->event = atomic_read(&attr_sd->s_attr.open->event);
110         count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
111
112         sysfs_put_active_two(attr_sd);
113
114         BUG_ON(count > (ssize_t)PAGE_SIZE);
115         if (count >= 0) {
116                 buffer->needs_read_fill = 0;
117                 buffer->count = count;
118         } else {
119                 ret = count;
120         }
121         return ret;
122 }
123
124 /**
125  *      sysfs_read_file - read an attribute. 
126  *      @file:  file pointer.
127  *      @buf:   buffer to fill.
128  *      @count: number of bytes to read.
129  *      @ppos:  starting offset in file.
130  *
131  *      Userspace wants to read an attribute file. The attribute descriptor
132  *      is in the file's ->d_fsdata. The target object is in the directory's
133  *      ->d_fsdata.
134  *
135  *      We call fill_read_buffer() to allocate and fill the buffer from the
136  *      object's show() method exactly once (if the read is happening from
137  *      the beginning of the file). That should fill the entire buffer with
138  *      all the data the object has to offer for that attribute.
139  *      We then call flush_read_buffer() to copy the buffer to userspace
140  *      in the increments specified.
141  */
142
143 static ssize_t
144 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
145 {
146         struct sysfs_buffer * buffer = file->private_data;
147         ssize_t retval = 0;
148
149         mutex_lock(&buffer->mutex);
150         if (buffer->needs_read_fill) {
151                 retval = fill_read_buffer(file->f_path.dentry,buffer);
152                 if (retval)
153                         goto out;
154         }
155         pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
156                  __FUNCTION__, count, *ppos, buffer->page);
157         retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
158                                          buffer->count);
159 out:
160         mutex_unlock(&buffer->mutex);
161         return retval;
162 }
163
164 /**
165  *      fill_write_buffer - copy buffer from userspace.
166  *      @buffer:        data buffer for file.
167  *      @buf:           data from user.
168  *      @count:         number of bytes in @userbuf.
169  *
170  *      Allocate @buffer->page if it hasn't been already, then
171  *      copy the user-supplied buffer into it.
172  */
173
174 static int 
175 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
176 {
177         int error;
178
179         if (!buffer->page)
180                 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
181         if (!buffer->page)
182                 return -ENOMEM;
183
184         if (count >= PAGE_SIZE)
185                 count = PAGE_SIZE - 1;
186         error = copy_from_user(buffer->page,buf,count);
187         buffer->needs_read_fill = 1;
188         /* if buf is assumed to contain a string, terminate it by \0,
189            so e.g. sscanf() can scan the string easily */
190         buffer->page[count] = 0;
191         return error ? -EFAULT : count;
192 }
193
194
195 /**
196  *      flush_write_buffer - push buffer to kobject.
197  *      @dentry:        dentry to the attribute
198  *      @buffer:        data buffer for file.
199  *      @count:         number of bytes
200  *
201  *      Get the correct pointers for the kobject and the attribute we're
202  *      dealing with, then call the store() method for the attribute, 
203  *      passing the buffer that we acquired in fill_write_buffer().
204  */
205
206 static int
207 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
208 {
209         struct sysfs_dirent *attr_sd = dentry->d_fsdata;
210         struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
211         struct sysfs_ops * ops = buffer->ops;
212         int rc;
213
214         /* need attr_sd for attr and ops, its parent for kobj */
215         if (!sysfs_get_active_two(attr_sd))
216                 return -ENODEV;
217
218         rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
219
220         sysfs_put_active_two(attr_sd);
221
222         return rc;
223 }
224
225
226 /**
227  *      sysfs_write_file - write an attribute.
228  *      @file:  file pointer
229  *      @buf:   data to write
230  *      @count: number of bytes
231  *      @ppos:  starting offset
232  *
233  *      Similar to sysfs_read_file(), though working in the opposite direction.
234  *      We allocate and fill the data from the user in fill_write_buffer(),
235  *      then push it to the kobject in flush_write_buffer().
236  *      There is no easy way for us to know if userspace is only doing a partial
237  *      write, so we don't support them. We expect the entire buffer to come
238  *      on the first write. 
239  *      Hint: if you're writing a value, first read the file, modify only the
240  *      the value you're changing, then write entire buffer back. 
241  */
242
243 static ssize_t
244 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
245 {
246         struct sysfs_buffer * buffer = file->private_data;
247         ssize_t len;
248
249         mutex_lock(&buffer->mutex);
250         len = fill_write_buffer(buffer, buf, count);
251         if (len > 0)
252                 len = flush_write_buffer(file->f_path.dentry, buffer, len);
253         if (len > 0)
254                 *ppos += len;
255         mutex_unlock(&buffer->mutex);
256         return len;
257 }
258
259 /**
260  *      sysfs_get_open_dirent - get or create sysfs_open_dirent
261  *      @sd: target sysfs_dirent
262  *      @buffer: sysfs_buffer for this instance of open
263  *
264  *      If @sd->s_attr.open exists, increment its reference count;
265  *      otherwise, create one.  @buffer is chained to the buffers
266  *      list.
267  *
268  *      LOCKING:
269  *      Kernel thread context (may sleep).
270  *
271  *      RETURNS:
272  *      0 on success, -errno on failure.
273  */
274 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
275                                  struct sysfs_buffer *buffer)
276 {
277         struct sysfs_open_dirent *od, *new_od = NULL;
278
279  retry:
280         spin_lock(&sysfs_open_dirent_lock);
281
282         if (!sd->s_attr.open && new_od) {
283                 sd->s_attr.open = new_od;
284                 new_od = NULL;
285         }
286
287         od = sd->s_attr.open;
288         if (od) {
289                 atomic_inc(&od->refcnt);
290                 list_add_tail(&buffer->list, &od->buffers);
291         }
292
293         spin_unlock(&sysfs_open_dirent_lock);
294
295         if (od) {
296                 kfree(new_od);
297                 return 0;
298         }
299
300         /* not there, initialize a new one and retry */
301         new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
302         if (!new_od)
303                 return -ENOMEM;
304
305         atomic_set(&new_od->refcnt, 0);
306         atomic_set(&new_od->event, 1);
307         init_waitqueue_head(&new_od->poll);
308         INIT_LIST_HEAD(&new_od->buffers);
309         goto retry;
310 }
311
312 /**
313  *      sysfs_put_open_dirent - put sysfs_open_dirent
314  *      @sd: target sysfs_dirent
315  *      @buffer: associated sysfs_buffer
316  *
317  *      Put @sd->s_attr.open and unlink @buffer from the buffers list.
318  *      If reference count reaches zero, disassociate and free it.
319  *
320  *      LOCKING:
321  *      None.
322  */
323 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
324                                   struct sysfs_buffer *buffer)
325 {
326         struct sysfs_open_dirent *od = sd->s_attr.open;
327
328         spin_lock(&sysfs_open_dirent_lock);
329
330         list_del(&buffer->list);
331         if (atomic_dec_and_test(&od->refcnt))
332                 sd->s_attr.open = NULL;
333         else
334                 od = NULL;
335
336         spin_unlock(&sysfs_open_dirent_lock);
337
338         kfree(od);
339 }
340
341 static int sysfs_open_file(struct inode *inode, struct file *file)
342 {
343         struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
344         struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
345         struct sysfs_buffer * buffer;
346         struct sysfs_ops * ops = NULL;
347         int error;
348
349         /* need attr_sd for attr and ops, its parent for kobj */
350         if (!sysfs_get_active_two(attr_sd))
351                 return -ENODEV;
352
353         /* if the kobject has no ktype, then we assume that it is a subsystem
354          * itself, and use ops for it.
355          */
356         if (kobj->kset && kobj->kset->ktype)
357                 ops = kobj->kset->ktype->sysfs_ops;
358         else if (kobj->ktype)
359                 ops = kobj->ktype->sysfs_ops;
360         else
361                 ops = &subsys_sysfs_ops;
362
363         error = -EACCES;
364
365         /* No sysfs operations, either from having no subsystem,
366          * or the subsystem have no operations.
367          */
368         if (!ops)
369                 goto err_out;
370
371         /* File needs write support.
372          * The inode's perms must say it's ok, 
373          * and we must have a store method.
374          */
375         if (file->f_mode & FMODE_WRITE) {
376                 if (!(inode->i_mode & S_IWUGO) || !ops->store)
377                         goto err_out;
378         }
379
380         /* File needs read support.
381          * The inode's perms must say it's ok, and we there
382          * must be a show method for it.
383          */
384         if (file->f_mode & FMODE_READ) {
385                 if (!(inode->i_mode & S_IRUGO) || !ops->show)
386                         goto err_out;
387         }
388
389         /* No error? Great, allocate a buffer for the file, and store it
390          * it in file->private_data for easy access.
391          */
392         error = -ENOMEM;
393         buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
394         if (!buffer)
395                 goto err_out;
396
397         mutex_init(&buffer->mutex);
398         buffer->needs_read_fill = 1;
399         buffer->ops = ops;
400         file->private_data = buffer;
401
402         /* make sure we have open dirent struct */
403         error = sysfs_get_open_dirent(attr_sd, buffer);
404         if (error)
405                 goto err_free;
406
407         /* open succeeded, put active references */
408         sysfs_put_active_two(attr_sd);
409         return 0;
410
411  err_free:
412         kfree(buffer);
413  err_out:
414         sysfs_put_active_two(attr_sd);
415         return error;
416 }
417
418 static int sysfs_release(struct inode *inode, struct file *filp)
419 {
420         struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
421         struct sysfs_buffer *buffer = filp->private_data;
422
423         sysfs_put_open_dirent(sd, buffer);
424
425         if (buffer->page)
426                 free_page((unsigned long)buffer->page);
427         kfree(buffer);
428
429         return 0;
430 }
431
432 /* Sysfs attribute files are pollable.  The idea is that you read
433  * the content and then you use 'poll' or 'select' to wait for
434  * the content to change.  When the content changes (assuming the
435  * manager for the kobject supports notification), poll will
436  * return POLLERR|POLLPRI, and select will return the fd whether
437  * it is waiting for read, write, or exceptions.
438  * Once poll/select indicates that the value has changed, you
439  * need to close and re-open the file, as simply seeking and reading
440  * again will not get new data, or reset the state of 'poll'.
441  * Reminder: this only works for attributes which actively support
442  * it, and it is not possible to test an attribute from userspace
443  * to see if it supports poll (Neither 'poll' nor 'select' return
444  * an appropriate error code).  When in doubt, set a suitable timeout value.
445  */
446 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
447 {
448         struct sysfs_buffer * buffer = filp->private_data;
449         struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
450         struct sysfs_open_dirent *od = attr_sd->s_attr.open;
451
452         /* need parent for the kobj, grab both */
453         if (!sysfs_get_active_two(attr_sd))
454                 goto trigger;
455
456         poll_wait(filp, &od->poll, wait);
457
458         sysfs_put_active_two(attr_sd);
459
460         if (buffer->event != atomic_read(&od->event))
461                 goto trigger;
462
463         return 0;
464
465  trigger:
466         buffer->needs_read_fill = 1;
467         return POLLERR|POLLPRI;
468 }
469
470 void sysfs_notify(struct kobject *k, char *dir, char *attr)
471 {
472         struct sysfs_dirent *sd = k->sd;
473
474         mutex_lock(&sysfs_mutex);
475
476         if (sd && dir)
477                 sd = sysfs_find_dirent(sd, dir);
478         if (sd && attr)
479                 sd = sysfs_find_dirent(sd, attr);
480         if (sd) {
481                 struct sysfs_open_dirent *od;
482
483                 spin_lock(&sysfs_open_dirent_lock);
484
485                 od = sd->s_attr.open;
486                 if (od) {
487                         atomic_inc(&od->event);
488                         wake_up_interruptible(&od->poll);
489                 }
490
491                 spin_unlock(&sysfs_open_dirent_lock);
492         }
493
494         mutex_unlock(&sysfs_mutex);
495 }
496 EXPORT_SYMBOL_GPL(sysfs_notify);
497
498 const struct file_operations sysfs_file_operations = {
499         .read           = sysfs_read_file,
500         .write          = sysfs_write_file,
501         .llseek         = generic_file_llseek,
502         .open           = sysfs_open_file,
503         .release        = sysfs_release,
504         .poll           = sysfs_poll,
505 };
506
507
508 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
509                    int type)
510 {
511         umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
512         struct sysfs_addrm_cxt acxt;
513         struct sysfs_dirent *sd;
514         int rc;
515
516         sd = sysfs_new_dirent(attr->name, mode, type);
517         if (!sd)
518                 return -ENOMEM;
519         sd->s_attr.attr = (void *)attr;
520
521         sysfs_addrm_start(&acxt, dir_sd);
522         rc = sysfs_add_one(&acxt, sd);
523         sysfs_addrm_finish(&acxt);
524
525         if (rc)
526                 sysfs_put(sd);
527
528         return rc;
529 }
530
531
532 /**
533  *      sysfs_create_file - create an attribute file for an object.
534  *      @kobj:  object we're creating for. 
535  *      @attr:  atrribute descriptor.
536  */
537
538 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
539 {
540         BUG_ON(!kobj || !kobj->sd || !attr);
541
542         return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
543
544 }
545
546
547 /**
548  * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
549  * @kobj: object we're acting for.
550  * @attr: attribute descriptor.
551  * @group: group name.
552  */
553 int sysfs_add_file_to_group(struct kobject *kobj,
554                 const struct attribute *attr, const char *group)
555 {
556         struct sysfs_dirent *dir_sd;
557         int error;
558
559         dir_sd = sysfs_get_dirent(kobj->sd, group);
560         if (!dir_sd)
561                 return -ENOENT;
562
563         error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
564         sysfs_put(dir_sd);
565
566         return error;
567 }
568 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
569
570 /**
571  * sysfs_chmod_file - update the modified mode value on an object attribute.
572  * @kobj: object we're acting for.
573  * @attr: attribute descriptor.
574  * @mode: file permissions.
575  *
576  */
577 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
578 {
579         struct sysfs_dirent *victim_sd = NULL;
580         struct dentry *victim = NULL;
581         struct inode * inode;
582         struct iattr newattrs;
583         int rc;
584
585         rc = -ENOENT;
586         victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
587         if (!victim_sd)
588                 goto out;
589
590         mutex_lock(&sysfs_rename_mutex);
591         victim = sysfs_get_dentry(victim_sd);
592         mutex_unlock(&sysfs_rename_mutex);
593         if (IS_ERR(victim)) {
594                 rc = PTR_ERR(victim);
595                 victim = NULL;
596                 goto out;
597         }
598
599         inode = victim->d_inode;
600
601         mutex_lock(&inode->i_mutex);
602
603         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
604         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
605         rc = notify_change(victim, &newattrs);
606
607         if (rc == 0) {
608                 mutex_lock(&sysfs_mutex);
609                 victim_sd->s_mode = newattrs.ia_mode;
610                 mutex_unlock(&sysfs_mutex);
611         }
612
613         mutex_unlock(&inode->i_mutex);
614  out:
615         dput(victim);
616         sysfs_put(victim_sd);
617         return rc;
618 }
619 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
620
621
622 /**
623  *      sysfs_remove_file - remove an object attribute.
624  *      @kobj:  object we're acting for.
625  *      @attr:  attribute descriptor.
626  *
627  *      Hash the attribute name and kill the victim.
628  */
629
630 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
631 {
632         sysfs_hash_and_remove(kobj->sd, attr->name);
633 }
634
635
636 /**
637  * sysfs_remove_file_from_group - remove an attribute file from a group.
638  * @kobj: object we're acting for.
639  * @attr: attribute descriptor.
640  * @group: group name.
641  */
642 void sysfs_remove_file_from_group(struct kobject *kobj,
643                 const struct attribute *attr, const char *group)
644 {
645         struct sysfs_dirent *dir_sd;
646
647         dir_sd = sysfs_get_dirent(kobj->sd, group);
648         if (dir_sd) {
649                 sysfs_hash_and_remove(dir_sd, attr->name);
650                 sysfs_put(dir_sd);
651         }
652 }
653 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
654
655 struct sysfs_schedule_callback_struct {
656         struct kobject          *kobj;
657         void                    (*func)(void *);
658         void                    *data;
659         struct module           *owner;
660         struct work_struct      work;
661 };
662
663 static void sysfs_schedule_callback_work(struct work_struct *work)
664 {
665         struct sysfs_schedule_callback_struct *ss = container_of(work,
666                         struct sysfs_schedule_callback_struct, work);
667
668         (ss->func)(ss->data);
669         kobject_put(ss->kobj);
670         module_put(ss->owner);
671         kfree(ss);
672 }
673
674 /**
675  * sysfs_schedule_callback - helper to schedule a callback for a kobject
676  * @kobj: object we're acting for.
677  * @func: callback function to invoke later.
678  * @data: argument to pass to @func.
679  * @owner: module owning the callback code
680  *
681  * sysfs attribute methods must not unregister themselves or their parent
682  * kobject (which would amount to the same thing).  Attempts to do so will
683  * deadlock, since unregistration is mutually exclusive with driver
684  * callbacks.
685  *
686  * Instead methods can call this routine, which will attempt to allocate
687  * and schedule a workqueue request to call back @func with @data as its
688  * argument in the workqueue's process context.  @kobj will be pinned
689  * until @func returns.
690  *
691  * Returns 0 if the request was submitted, -ENOMEM if storage could not
692  * be allocated, -ENODEV if a reference to @owner isn't available.
693  */
694 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
695                 void *data, struct module *owner)
696 {
697         struct sysfs_schedule_callback_struct *ss;
698
699         if (!try_module_get(owner))
700                 return -ENODEV;
701         ss = kmalloc(sizeof(*ss), GFP_KERNEL);
702         if (!ss) {
703                 module_put(owner);
704                 return -ENOMEM;
705         }
706         kobject_get(kobj);
707         ss->kobj = kobj;
708         ss->func = func;
709         ss->data = data;
710         ss->owner = owner;
711         INIT_WORK(&ss->work, sysfs_schedule_callback_work);
712         schedule_work(&ss->work);
713         return 0;
714 }
715 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
716
717
718 EXPORT_SYMBOL_GPL(sysfs_create_file);
719 EXPORT_SYMBOL_GPL(sysfs_remove_file);