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