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