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