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