]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/sysfs/file.c
sysfs: prepare path write for unified regular / bin file handling
[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 #include <linux/seq_file.h>
25
26 #include "sysfs.h"
27
28 /*
29  * There's one sysfs_open_file for each open file and one sysfs_open_dirent
30  * for each sysfs_dirent with one or more open files.
31  *
32  * sysfs_dirent->s_attr.open points to sysfs_open_dirent.  s_attr.open is
33  * protected by sysfs_open_dirent_lock.
34  *
35  * filp->private_data points to seq_file whose ->private points to
36  * sysfs_open_file.  sysfs_open_files are chained at
37  * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
38  */
39 static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
40 static DEFINE_MUTEX(sysfs_open_file_mutex);
41
42 struct sysfs_open_dirent {
43         atomic_t                refcnt;
44         atomic_t                event;
45         wait_queue_head_t       poll;
46         struct list_head        files; /* goes through sysfs_open_file.list */
47 };
48
49 struct sysfs_open_file {
50         struct sysfs_dirent     *sd;
51         struct file             *file;
52         struct mutex            mutex;
53         int                     event;
54         struct list_head        list;
55 };
56
57 static bool sysfs_is_bin(struct sysfs_dirent *sd)
58 {
59         return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
60 }
61
62 static struct sysfs_open_file *sysfs_of(struct file *file)
63 {
64         return ((struct seq_file *)file->private_data)->private;
65 }
66
67 /*
68  * Determine ktype->sysfs_ops for the given sysfs_dirent.  This function
69  * must be called while holding an active reference.
70  */
71 static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
72 {
73         struct kobject *kobj = sd->s_parent->s_dir.kobj;
74
75         lockdep_assert_held(sd);
76         return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
77 }
78
79 /*
80  * Reads on sysfs are handled through seq_file, which takes care of hairy
81  * details like buffering and seeking.  The following function pipes
82  * sysfs_ops->show() result through seq_file.
83  */
84 static int sysfs_seq_show(struct seq_file *sf, void *v)
85 {
86         struct sysfs_open_file *of = sf->private;
87         struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
88         const struct sysfs_ops *ops;
89         char *buf;
90         ssize_t count;
91
92         /* acquire buffer and ensure that it's >= PAGE_SIZE */
93         count = seq_get_buf(sf, &buf);
94         if (count < PAGE_SIZE) {
95                 seq_commit(sf, -1);
96                 return 0;
97         }
98
99         /*
100          * Need @of->sd for attr and ops, its parent for kobj.  @of->mutex
101          * nests outside active ref and is just to ensure that the ops
102          * aren't called concurrently for the same open file.
103          */
104         mutex_lock(&of->mutex);
105         if (!sysfs_get_active(of->sd)) {
106                 mutex_unlock(&of->mutex);
107                 return -ENODEV;
108         }
109
110         of->event = atomic_read(&of->sd->s_attr.open->event);
111
112         /*
113          * Lookup @ops and invoke show().  Control may reach here via seq
114          * file lseek even if @ops->show() isn't implemented.
115          */
116         ops = sysfs_file_ops(of->sd);
117         if (ops->show)
118                 count = ops->show(kobj, of->sd->s_attr.attr, buf);
119         else
120                 count = 0;
121
122         sysfs_put_active(of->sd);
123         mutex_unlock(&of->mutex);
124
125         if (count < 0)
126                 return count;
127
128         /*
129          * The code works fine with PAGE_SIZE return but it's likely to
130          * indicate truncated result or overflow in normal use cases.
131          */
132         if (count >= (ssize_t)PAGE_SIZE) {
133                 print_symbol("fill_read_buffer: %s returned bad count\n",
134                         (unsigned long)ops->show);
135                 /* Try to struggle along */
136                 count = PAGE_SIZE - 1;
137         }
138         seq_commit(sf, count);
139         return 0;
140 }
141
142 /**
143  * flush_write_buffer - push buffer to kobject
144  * @of: open file
145  * @buf: data buffer for file
146  * @off: file offset to write to
147  * @count: number of bytes
148  *
149  * Get the correct pointers for the kobject and the attribute we're dealing
150  * with, then call the store() method for it with @buf.
151  */
152 static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
153                               size_t count)
154 {
155         struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
156         int rc = 0;
157
158         /*
159          * Need @of->sd for attr and ops, its parent for kobj.  @of->mutex
160          * nests outside active ref and is just to ensure that the ops
161          * aren't called concurrently for the same open file.
162          */
163         mutex_lock(&of->mutex);
164         if (!sysfs_get_active(of->sd)) {
165                 mutex_unlock(&of->mutex);
166                 return -ENODEV;
167         }
168
169         if (sysfs_is_bin(of->sd)) {
170                 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
171
172                 rc = -EIO;
173                 if (battr->write)
174                         rc = battr->write(of->file, kobj, battr, buf, off,
175                                           count);
176         } else {
177                 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
178
179                 rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
180         }
181
182         sysfs_put_active(of->sd);
183         mutex_unlock(&of->mutex);
184
185         return rc;
186 }
187
188 /**
189  * sysfs_write_file - write an attribute
190  * @file: file pointer
191  * @user_buf: data to write
192  * @count: number of bytes
193  * @ppos: starting offset
194  *
195  * Copy data in from userland and pass it to the matching
196  * sysfs_ops->store() by invoking flush_write_buffer().
197  *
198  * There is no easy way for us to know if userspace is only doing a partial
199  * write, so we don't support them. We expect the entire buffer to come on
200  * the first write.  Hint: if you're writing a value, first read the file,
201  * modify only the the value you're changing, then write entire buffer
202  * back.
203  */
204 static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
205                                 size_t count, loff_t *ppos)
206 {
207         struct sysfs_open_file *of = sysfs_of(file);
208         ssize_t len = min_t(size_t, count, PAGE_SIZE);
209         char *buf;
210
211         if (sysfs_is_bin(of->sd)) {
212                 loff_t size = file_inode(file)->i_size;
213
214                 if (size <= *ppos)
215                         return 0;
216                 len = min_t(ssize_t, len, size - *ppos);
217         }
218
219         if (!len)
220                 return 0;
221
222         buf = kmalloc(len + 1, GFP_KERNEL);
223         if (!buf)
224                 return -ENOMEM;
225
226         if (copy_from_user(buf, user_buf, len)) {
227                 len = -EFAULT;
228                 goto out_free;
229         }
230         buf[len] = '\0';        /* guarantee string termination */
231
232         len = flush_write_buffer(of, buf, *ppos, len);
233         if (len > 0)
234                 *ppos += len;
235 out_free:
236         kfree(buf);
237         return len;
238 }
239
240 /**
241  *      sysfs_get_open_dirent - get or create sysfs_open_dirent
242  *      @sd: target sysfs_dirent
243  *      @of: sysfs_open_file for this instance of open
244  *
245  *      If @sd->s_attr.open exists, increment its reference count;
246  *      otherwise, create one.  @of is chained to the files list.
247  *
248  *      LOCKING:
249  *      Kernel thread context (may sleep).
250  *
251  *      RETURNS:
252  *      0 on success, -errno on failure.
253  */
254 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
255                                  struct sysfs_open_file *of)
256 {
257         struct sysfs_open_dirent *od, *new_od = NULL;
258
259  retry:
260         mutex_lock(&sysfs_open_file_mutex);
261         spin_lock_irq(&sysfs_open_dirent_lock);
262
263         if (!sd->s_attr.open && new_od) {
264                 sd->s_attr.open = new_od;
265                 new_od = NULL;
266         }
267
268         od = sd->s_attr.open;
269         if (od) {
270                 atomic_inc(&od->refcnt);
271                 list_add_tail(&of->list, &od->files);
272         }
273
274         spin_unlock_irq(&sysfs_open_dirent_lock);
275         mutex_unlock(&sysfs_open_file_mutex);
276
277         if (od) {
278                 kfree(new_od);
279                 return 0;
280         }
281
282         /* not there, initialize a new one and retry */
283         new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
284         if (!new_od)
285                 return -ENOMEM;
286
287         atomic_set(&new_od->refcnt, 0);
288         atomic_set(&new_od->event, 1);
289         init_waitqueue_head(&new_od->poll);
290         INIT_LIST_HEAD(&new_od->files);
291         goto retry;
292 }
293
294 /**
295  *      sysfs_put_open_dirent - put sysfs_open_dirent
296  *      @sd: target sysfs_dirent
297  *      @of: associated sysfs_open_file
298  *
299  *      Put @sd->s_attr.open and unlink @of from the files list.  If
300  *      reference count reaches zero, disassociate and free it.
301  *
302  *      LOCKING:
303  *      None.
304  */
305 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
306                                   struct sysfs_open_file *of)
307 {
308         struct sysfs_open_dirent *od = sd->s_attr.open;
309         unsigned long flags;
310
311         mutex_lock(&sysfs_open_file_mutex);
312         spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
313
314         list_del(&of->list);
315         if (atomic_dec_and_test(&od->refcnt))
316                 sd->s_attr.open = NULL;
317         else
318                 od = NULL;
319
320         spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
321         mutex_unlock(&sysfs_open_file_mutex);
322
323         kfree(od);
324 }
325
326 static int sysfs_open_file(struct inode *inode, struct file *file)
327 {
328         struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
329         struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
330         struct sysfs_open_file *of;
331         const struct sysfs_ops *ops;
332         int error = -EACCES;
333
334         /* need attr_sd for attr and ops, its parent for kobj */
335         if (!sysfs_get_active(attr_sd))
336                 return -ENODEV;
337
338         /* every kobject with an attribute needs a ktype assigned */
339         ops = sysfs_file_ops(attr_sd);
340         if (WARN(!ops, KERN_ERR
341                  "missing sysfs attribute operations for kobject: %s\n",
342                  kobject_name(kobj)))
343                 goto err_out;
344
345         /* File needs write support.
346          * The inode's perms must say it's ok,
347          * and we must have a store method.
348          */
349         if (file->f_mode & FMODE_WRITE) {
350                 if (!(inode->i_mode & S_IWUGO) || !ops->store)
351                         goto err_out;
352         }
353
354         /* File needs read support.
355          * The inode's perms must say it's ok, and we there
356          * must be a show method for it.
357          */
358         if (file->f_mode & FMODE_READ) {
359                 if (!(inode->i_mode & S_IRUGO) || !ops->show)
360                         goto err_out;
361         }
362
363         /* allocate a sysfs_open_file for the file */
364         error = -ENOMEM;
365         of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
366         if (!of)
367                 goto err_out;
368
369         mutex_init(&of->mutex);
370         of->sd = attr_sd;
371         of->file = file;
372
373         /*
374          * Always instantiate seq_file even if read access is not
375          * implemented or requested.  This unifies private data access and
376          * most files are readable anyway.
377          */
378         error = single_open(file, sysfs_seq_show, of);
379         if (error)
380                 goto err_free;
381
382         /* seq_file clears PWRITE unconditionally, restore it if WRITE */
383         if (file->f_mode & FMODE_WRITE)
384                 file->f_mode |= FMODE_PWRITE;
385
386         /* make sure we have open dirent struct */
387         error = sysfs_get_open_dirent(attr_sd, of);
388         if (error)
389                 goto err_close;
390
391         /* open succeeded, put active references */
392         sysfs_put_active(attr_sd);
393         return 0;
394
395 err_close:
396         single_release(inode, file);
397 err_free:
398         kfree(of);
399 err_out:
400         sysfs_put_active(attr_sd);
401         return error;
402 }
403
404 static int sysfs_release(struct inode *inode, struct file *filp)
405 {
406         struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
407         struct sysfs_open_file *of = sysfs_of(filp);
408
409         sysfs_put_open_dirent(sd, of);
410         single_release(inode, filp);
411         kfree(of);
412
413         return 0;
414 }
415
416 /* Sysfs attribute files are pollable.  The idea is that you read
417  * the content and then you use 'poll' or 'select' to wait for
418  * the content to change.  When the content changes (assuming the
419  * manager for the kobject supports notification), poll will
420  * return POLLERR|POLLPRI, and select will return the fd whether
421  * it is waiting for read, write, or exceptions.
422  * Once poll/select indicates that the value has changed, you
423  * need to close and re-open the file, or seek to 0 and read again.
424  * Reminder: this only works for attributes which actively support
425  * it, and it is not possible to test an attribute from userspace
426  * to see if it supports poll (Neither 'poll' nor 'select' return
427  * an appropriate error code).  When in doubt, set a suitable timeout value.
428  */
429 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
430 {
431         struct sysfs_open_file *of = sysfs_of(filp);
432         struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
433         struct sysfs_open_dirent *od = attr_sd->s_attr.open;
434
435         /* need parent for the kobj, grab both */
436         if (!sysfs_get_active(attr_sd))
437                 goto trigger;
438
439         poll_wait(filp, &od->poll, wait);
440
441         sysfs_put_active(attr_sd);
442
443         if (of->event != atomic_read(&od->event))
444                 goto trigger;
445
446         return DEFAULT_POLLMASK;
447
448  trigger:
449         return DEFAULT_POLLMASK|POLLERR|POLLPRI;
450 }
451
452 void sysfs_notify_dirent(struct sysfs_dirent *sd)
453 {
454         struct sysfs_open_dirent *od;
455         unsigned long flags;
456
457         spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
458
459         if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
460                 od = sd->s_attr.open;
461                 if (od) {
462                         atomic_inc(&od->event);
463                         wake_up_interruptible(&od->poll);
464                 }
465         }
466
467         spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
468 }
469 EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
470
471 void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
472 {
473         struct sysfs_dirent *sd = k->sd;
474
475         mutex_lock(&sysfs_mutex);
476
477         if (sd && dir)
478                 sd = sysfs_find_dirent(sd, dir, NULL);
479         if (sd && attr)
480                 sd = sysfs_find_dirent(sd, attr, NULL);
481         if (sd)
482                 sysfs_notify_dirent(sd);
483
484         mutex_unlock(&sysfs_mutex);
485 }
486 EXPORT_SYMBOL_GPL(sysfs_notify);
487
488 const struct file_operations sysfs_file_operations = {
489         .read           = seq_read,
490         .write          = sysfs_write_file,
491         .llseek         = seq_lseek,
492         .open           = sysfs_open_file,
493         .release        = sysfs_release,
494         .poll           = sysfs_poll,
495 };
496
497 const struct file_operations sysfs_bin_operations = {
498         .write          = sysfs_write_file,
499         .llseek         = generic_file_llseek,
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);