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