sysfs, kernfs: revamp sysfs_dirent active_ref lockdep annotation
[deliverable/linux.git] / fs / sysfs / file.c
CommitLineData
1da177e4 1/*
6d66f5cd
TH
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.
1da177e4
LT
11 */
12
13#include <linux/module.h>
1da177e4 14#include <linux/kobject.h>
815d2d50 15#include <linux/kallsyms.h>
c6f87733 16#include <linux/slab.h>
93265d13 17#include <linux/fsnotify.h>
5f45f1a7 18#include <linux/namei.h>
4508a7a7 19#include <linux/poll.h>
94bebf4d 20#include <linux/list.h>
52e8c209 21#include <linux/mutex.h>
ae87221d 22#include <linux/limits.h>
060cc749 23#include <linux/uaccess.h>
13c589d5 24#include <linux/seq_file.h>
73d97146 25#include <linux/mm.h>
1da177e4
LT
26
27#include "sysfs.h"
28
85a4ffad 29/*
58282d8d 30 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
c75ec764 31 * for each sysfs_dirent with one or more open files.
85a4ffad 32 *
c75ec764
TH
33 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
34 * protected by sysfs_open_dirent_lock.
35 *
13c589d5
TH
36 * filp->private_data points to seq_file whose ->private points to
37 * sysfs_open_file. sysfs_open_files are chained at
58282d8d 38 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
85a4ffad 39 */
d7b37889 40static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
c75ec764 41static DEFINE_MUTEX(sysfs_open_file_mutex);
85a4ffad
TH
42
43struct sysfs_open_dirent {
44 atomic_t refcnt;
a4e8b912
TH
45 atomic_t event;
46 wait_queue_head_t poll;
58282d8d 47 struct list_head files; /* goes through sysfs_open_file.list */
85a4ffad
TH
48};
49
13c589d5
TH
50static struct sysfs_open_file *sysfs_of(struct file *file)
51{
52 return ((struct seq_file *)file->private_data)->private;
53}
54
f6acf8bb
TH
55/*
56 * Determine the kernfs_ops for the given sysfs_dirent. This function must
57 * be called while holding an active reference.
58 */
59static const struct kernfs_ops *kernfs_ops(struct sysfs_dirent *sd)
60{
517e64f5 61 if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
f6acf8bb
TH
62 lockdep_assert_held(sd);
63 return sd->s_attr.ops;
64}
65
375b611e
TH
66/*
67 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
68 * must be called while holding an active reference.
69 */
70static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
71{
7c6e2d36 72 struct kobject *kobj = sd->s_parent->priv;
375b611e 73
517e64f5 74 if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
785a162d 75 lockdep_assert_held(sd);
375b611e
TH
76 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
77}
78
13c589d5
TH
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.
1da177e4 83 */
c2b19daf 84static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
1da177e4 85{
13c589d5 86 struct sysfs_open_file *of = sf->private;
7c6e2d36 87 struct kobject *kobj = of->sd->s_parent->priv;
c2b19daf 88 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
1da177e4 89 ssize_t count;
c2b19daf 90 char *buf;
1da177e4 91
13c589d5
TH
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 }
1da177e4 98
13c589d5 99 /*
c2b19daf
TH
100 * Invoke show(). Control may reach here via seq file lseek even
101 * if @ops->show() isn't implemented.
13c589d5 102 */
c2b19daf 103 if (ops->show) {
7c6e2d36 104 count = ops->show(kobj, of->sd->priv, buf);
c2b19daf
TH
105 if (count < 0)
106 return count;
107 }
0ab66088 108
8118a859
MX
109 /*
110 * The code works fine with PAGE_SIZE return but it's likely to
111 * indicate truncated result or overflow in normal use cases.
112 */
815d2d50
AM
113 if (count >= (ssize_t)PAGE_SIZE) {
114 print_symbol("fill_read_buffer: %s returned bad count\n",
115 (unsigned long)ops->show);
116 /* Try to struggle along */
117 count = PAGE_SIZE - 1;
118 }
13c589d5
TH
119 seq_commit(sf, count);
120 return 0;
1da177e4
LT
121}
122
c2b19daf
TH
123static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
124 size_t count, loff_t pos)
2f0c6b75 125{
7c6e2d36
TH
126 struct bin_attribute *battr = of->sd->priv;
127 struct kobject *kobj = of->sd->s_parent->priv;
c2b19daf 128 loff_t size = file_inode(of->file)->i_size;
2f0c6b75 129
c2b19daf 130 if (!count)
2f0c6b75
TH
131 return 0;
132
133 if (size) {
c2b19daf 134 if (pos > size)
2f0c6b75 135 return 0;
c2b19daf
TH
136 if (pos + count > size)
137 count = size - pos;
2f0c6b75
TH
138 }
139
c2b19daf
TH
140 if (!battr->read)
141 return -EIO;
142
143 return battr->read(of->file, kobj, battr, buf, pos, count);
144}
145
146static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
147{
148 struct sysfs_open_file *of = sf->private;
d19b9846 149 const struct kernfs_ops *ops;
c2b19daf
TH
150
151 /*
152 * @of->mutex nests outside active ref and is just to ensure that
153 * the ops aren't called concurrently for the same open file.
154 */
155 mutex_lock(&of->mutex);
156 if (!sysfs_get_active(of->sd))
157 return ERR_PTR(-ENODEV);
158
d19b9846
TH
159 ops = kernfs_ops(of->sd);
160 if (ops->seq_start) {
161 return ops->seq_start(sf, ppos);
162 } else {
163 /*
164 * The same behavior and code as single_open(). Returns
165 * !NULL if pos is at the beginning; otherwise, NULL.
166 */
167 return NULL + !*ppos;
168 }
c2b19daf
TH
169}
170
171static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
172{
d19b9846
TH
173 struct sysfs_open_file *of = sf->private;
174 const struct kernfs_ops *ops = kernfs_ops(of->sd);
175
176 if (ops->seq_next) {
177 return ops->seq_next(sf, v, ppos);
178 } else {
179 /*
180 * The same behavior and code as single_open(), always
181 * terminate after the initial read.
182 */
183 ++*ppos;
184 return NULL;
185 }
c2b19daf
TH
186}
187
188static void kernfs_seq_stop(struct seq_file *sf, void *v)
189{
190 struct sysfs_open_file *of = sf->private;
d19b9846
TH
191 const struct kernfs_ops *ops = kernfs_ops(of->sd);
192
193 if (ops->seq_stop)
194 ops->seq_stop(sf, v);
c2b19daf
TH
195
196 sysfs_put_active(of->sd);
197 mutex_unlock(&of->mutex);
198}
199
200static int kernfs_seq_show(struct seq_file *sf, void *v)
201{
202 struct sysfs_open_file *of = sf->private;
203
204 of->event = atomic_read(&of->sd->s_attr.open->event);
205
f6acf8bb 206 return of->sd->s_attr.ops->seq_show(sf, v);
c2b19daf
TH
207}
208
209static const struct seq_operations kernfs_seq_ops = {
210 .start = kernfs_seq_start,
211 .next = kernfs_seq_next,
212 .stop = kernfs_seq_stop,
213 .show = kernfs_seq_show,
214};
215
216/*
217 * As reading a bin file can have side-effects, the exact offset and bytes
218 * specified in read(2) call should be passed to the read callback making
219 * it difficult to use seq_file. Implement simplistic custom buffering for
220 * bin files.
221 */
222static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
223 char __user *user_buf, size_t count,
224 loff_t *ppos)
225{
226 ssize_t len = min_t(size_t, count, PAGE_SIZE);
f6acf8bb 227 const struct kernfs_ops *ops;
c2b19daf
TH
228 char *buf;
229
230 buf = kmalloc(len, GFP_KERNEL);
2f0c6b75
TH
231 if (!buf)
232 return -ENOMEM;
233
c2b19daf
TH
234 /*
235 * @of->mutex nests outside active ref and is just to ensure that
236 * the ops aren't called concurrently for the same open file.
237 */
2f0c6b75
TH
238 mutex_lock(&of->mutex);
239 if (!sysfs_get_active(of->sd)) {
c2b19daf 240 len = -ENODEV;
2f0c6b75
TH
241 mutex_unlock(&of->mutex);
242 goto out_free;
243 }
244
f6acf8bb
TH
245 ops = kernfs_ops(of->sd);
246 if (ops->read)
247 len = ops->read(of, buf, len, *ppos);
248 else
249 len = -EINVAL;
2f0c6b75
TH
250
251 sysfs_put_active(of->sd);
252 mutex_unlock(&of->mutex);
253
c2b19daf 254 if (len < 0)
2f0c6b75
TH
255 goto out_free;
256
c2b19daf
TH
257 if (copy_to_user(user_buf, buf, len)) {
258 len = -EFAULT;
2f0c6b75
TH
259 goto out_free;
260 }
261
c2b19daf 262 *ppos += len;
2f0c6b75
TH
263
264 out_free:
265 kfree(buf);
c2b19daf
TH
266 return len;
267}
268
269/**
270 * kernfs_file_read - kernfs vfs read callback
271 * @file: file pointer
272 * @user_buf: data to write
273 * @count: number of bytes
274 * @ppos: starting offset
275 */
276static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
277 size_t count, loff_t *ppos)
278{
279 struct sysfs_open_file *of = sysfs_of(file);
280
f6acf8bb 281 if (of->sd->s_flags & SYSFS_FLAG_HAS_SEQ_SHOW)
c2b19daf 282 return seq_read(file, user_buf, count, ppos);
f6acf8bb
TH
283 else
284 return kernfs_file_direct_read(of, user_buf, count, ppos);
2f0c6b75
TH
285}
286
50b38ca0
TH
287/* kernfs write callback for regular sysfs files */
288static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf,
289 size_t count, loff_t pos)
1da177e4 290{
50b38ca0 291 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
7c6e2d36 292 struct kobject *kobj = of->sd->s_parent->priv;
0ab66088 293
50b38ca0
TH
294 if (!count)
295 return 0;
0ab66088 296
50b38ca0
TH
297 return ops->store(kobj, of->sd->priv, buf, count);
298}
f9b9a621 299
50b38ca0
TH
300/* kernfs write callback for bin sysfs files */
301static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
302 size_t count, loff_t pos)
303{
304 struct bin_attribute *battr = of->sd->priv;
305 struct kobject *kobj = of->sd->s_parent->priv;
306 loff_t size = file_inode(of->file)->i_size;
f9b9a621 307
50b38ca0
TH
308 if (size) {
309 if (size <= pos)
310 return 0;
311 count = min_t(ssize_t, count, size - pos);
f9b9a621 312 }
50b38ca0
TH
313 if (!count)
314 return 0;
0ab66088 315
50b38ca0
TH
316 if (!battr->write)
317 return -EIO;
1da177e4 318
50b38ca0 319 return battr->write(of->file, kobj, battr, buf, pos, count);
1da177e4
LT
320}
321
1da177e4 322/**
50b38ca0 323 * kernfs_file_write - kernfs vfs write callback
8ef445f0
TH
324 * @file: file pointer
325 * @user_buf: data to write
326 * @count: number of bytes
327 * @ppos: starting offset
328 *
50b38ca0
TH
329 * Copy data in from userland and pass it to the matching kernfs write
330 * operation.
1da177e4 331 *
8ef445f0
TH
332 * There is no easy way for us to know if userspace is only doing a partial
333 * write, so we don't support them. We expect the entire buffer to come on
334 * the first write. Hint: if you're writing a value, first read the file,
335 * modify only the the value you're changing, then write entire buffer
336 * back.
1da177e4 337 */
50b38ca0
TH
338static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
339 size_t count, loff_t *ppos)
1da177e4 340{
13c589d5 341 struct sysfs_open_file *of = sysfs_of(file);
f9b9a621 342 ssize_t len = min_t(size_t, count, PAGE_SIZE);
f6acf8bb 343 const struct kernfs_ops *ops;
8ef445f0 344 char *buf;
1da177e4 345
8ef445f0
TH
346 buf = kmalloc(len + 1, GFP_KERNEL);
347 if (!buf)
348 return -ENOMEM;
349
350 if (copy_from_user(buf, user_buf, len)) {
351 len = -EFAULT;
352 goto out_free;
353 }
354 buf[len] = '\0'; /* guarantee string termination */
355
50b38ca0
TH
356 /*
357 * @of->mutex nests outside active ref and is just to ensure that
358 * the ops aren't called concurrently for the same open file.
359 */
360 mutex_lock(&of->mutex);
361 if (!sysfs_get_active(of->sd)) {
362 mutex_unlock(&of->mutex);
363 len = -ENODEV;
364 goto out_free;
365 }
366
f6acf8bb
TH
367 ops = kernfs_ops(of->sd);
368 if (ops->write)
369 len = ops->write(of, buf, len, *ppos);
50b38ca0 370 else
f6acf8bb 371 len = -EINVAL;
50b38ca0
TH
372
373 sysfs_put_active(of->sd);
374 mutex_unlock(&of->mutex);
375
1da177e4
LT
376 if (len > 0)
377 *ppos += len;
8ef445f0
TH
378out_free:
379 kfree(buf);
1da177e4
LT
380 return len;
381}
382
fdbffaa4
TH
383static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
384 struct vm_area_struct *vma)
385{
386 struct bin_attribute *battr = of->sd->priv;
387 struct kobject *kobj = of->sd->s_parent->priv;
388
389 if (!battr->mmap)
390 return -ENODEV;
391
392 return battr->mmap(of->file, kobj, battr, vma);
393}
394
395static void kernfs_vma_open(struct vm_area_struct *vma)
73d97146
TH
396{
397 struct file *file = vma->vm_file;
398 struct sysfs_open_file *of = sysfs_of(file);
399
400 if (!of->vm_ops)
401 return;
402
403 if (!sysfs_get_active(of->sd))
404 return;
405
406 if (of->vm_ops->open)
407 of->vm_ops->open(vma);
408
409 sysfs_put_active(of->sd);
410}
411
fdbffaa4 412static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
73d97146
TH
413{
414 struct file *file = vma->vm_file;
415 struct sysfs_open_file *of = sysfs_of(file);
416 int ret;
417
418 if (!of->vm_ops)
419 return VM_FAULT_SIGBUS;
420
421 if (!sysfs_get_active(of->sd))
422 return VM_FAULT_SIGBUS;
423
424 ret = VM_FAULT_SIGBUS;
425 if (of->vm_ops->fault)
426 ret = of->vm_ops->fault(vma, vmf);
427
428 sysfs_put_active(of->sd);
429 return ret;
430}
431
fdbffaa4
TH
432static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
433 struct vm_fault *vmf)
73d97146
TH
434{
435 struct file *file = vma->vm_file;
436 struct sysfs_open_file *of = sysfs_of(file);
437 int ret;
438
439 if (!of->vm_ops)
440 return VM_FAULT_SIGBUS;
441
442 if (!sysfs_get_active(of->sd))
443 return VM_FAULT_SIGBUS;
444
445 ret = 0;
446 if (of->vm_ops->page_mkwrite)
447 ret = of->vm_ops->page_mkwrite(vma, vmf);
448 else
449 file_update_time(file);
450
451 sysfs_put_active(of->sd);
452 return ret;
453}
454
fdbffaa4
TH
455static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
456 void *buf, int len, int write)
73d97146
TH
457{
458 struct file *file = vma->vm_file;
459 struct sysfs_open_file *of = sysfs_of(file);
460 int ret;
461
462 if (!of->vm_ops)
463 return -EINVAL;
464
465 if (!sysfs_get_active(of->sd))
466 return -EINVAL;
467
468 ret = -EINVAL;
469 if (of->vm_ops->access)
470 ret = of->vm_ops->access(vma, addr, buf, len, write);
471
472 sysfs_put_active(of->sd);
473 return ret;
474}
475
476#ifdef CONFIG_NUMA
fdbffaa4
TH
477static int kernfs_vma_set_policy(struct vm_area_struct *vma,
478 struct mempolicy *new)
73d97146
TH
479{
480 struct file *file = vma->vm_file;
481 struct sysfs_open_file *of = sysfs_of(file);
482 int ret;
483
484 if (!of->vm_ops)
485 return 0;
486
487 if (!sysfs_get_active(of->sd))
488 return -EINVAL;
489
490 ret = 0;
491 if (of->vm_ops->set_policy)
492 ret = of->vm_ops->set_policy(vma, new);
493
494 sysfs_put_active(of->sd);
495 return ret;
496}
497
fdbffaa4
TH
498static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
499 unsigned long addr)
73d97146
TH
500{
501 struct file *file = vma->vm_file;
502 struct sysfs_open_file *of = sysfs_of(file);
503 struct mempolicy *pol;
504
505 if (!of->vm_ops)
506 return vma->vm_policy;
507
508 if (!sysfs_get_active(of->sd))
509 return vma->vm_policy;
510
511 pol = vma->vm_policy;
512 if (of->vm_ops->get_policy)
513 pol = of->vm_ops->get_policy(vma, addr);
514
515 sysfs_put_active(of->sd);
516 return pol;
517}
518
fdbffaa4
TH
519static int kernfs_vma_migrate(struct vm_area_struct *vma,
520 const nodemask_t *from, const nodemask_t *to,
521 unsigned long flags)
73d97146
TH
522{
523 struct file *file = vma->vm_file;
524 struct sysfs_open_file *of = sysfs_of(file);
525 int ret;
526
527 if (!of->vm_ops)
528 return 0;
529
530 if (!sysfs_get_active(of->sd))
531 return 0;
532
533 ret = 0;
534 if (of->vm_ops->migrate)
535 ret = of->vm_ops->migrate(vma, from, to, flags);
536
537 sysfs_put_active(of->sd);
538 return ret;
539}
540#endif
541
fdbffaa4
TH
542static const struct vm_operations_struct kernfs_vm_ops = {
543 .open = kernfs_vma_open,
544 .fault = kernfs_vma_fault,
545 .page_mkwrite = kernfs_vma_page_mkwrite,
546 .access = kernfs_vma_access,
73d97146 547#ifdef CONFIG_NUMA
fdbffaa4
TH
548 .set_policy = kernfs_vma_set_policy,
549 .get_policy = kernfs_vma_get_policy,
550 .migrate = kernfs_vma_migrate,
73d97146
TH
551#endif
552};
553
fdbffaa4 554static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
73d97146
TH
555{
556 struct sysfs_open_file *of = sysfs_of(file);
f6acf8bb 557 const struct kernfs_ops *ops;
73d97146
TH
558 int rc;
559
560 mutex_lock(&of->mutex);
561
73d97146
TH
562 rc = -ENODEV;
563 if (!sysfs_get_active(of->sd))
564 goto out_unlock;
565
f6acf8bb
TH
566 ops = kernfs_ops(of->sd);
567 if (ops->mmap)
568 rc = ops->mmap(of, vma);
73d97146
TH
569 if (rc)
570 goto out_put;
571
572 /*
573 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
574 * to satisfy versions of X which crash if the mmap fails: that
575 * substitutes a new vm_file, and we don't then want bin_vm_ops.
576 */
577 if (vma->vm_file != file)
578 goto out_put;
579
580 rc = -EINVAL;
581 if (of->mmapped && of->vm_ops != vma->vm_ops)
582 goto out_put;
583
584 /*
585 * It is not possible to successfully wrap close.
586 * So error if someone is trying to use close.
587 */
588 rc = -EINVAL;
589 if (vma->vm_ops && vma->vm_ops->close)
590 goto out_put;
591
592 rc = 0;
593 of->mmapped = 1;
594 of->vm_ops = vma->vm_ops;
fdbffaa4 595 vma->vm_ops = &kernfs_vm_ops;
73d97146
TH
596out_put:
597 sysfs_put_active(of->sd);
598out_unlock:
599 mutex_unlock(&of->mutex);
600
601 return rc;
602}
603
85a4ffad
TH
604/**
605 * sysfs_get_open_dirent - get or create sysfs_open_dirent
606 * @sd: target sysfs_dirent
58282d8d 607 * @of: sysfs_open_file for this instance of open
85a4ffad
TH
608 *
609 * If @sd->s_attr.open exists, increment its reference count;
58282d8d 610 * otherwise, create one. @of is chained to the files list.
85a4ffad
TH
611 *
612 * LOCKING:
613 * Kernel thread context (may sleep).
614 *
615 * RETURNS:
616 * 0 on success, -errno on failure.
617 */
618static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
58282d8d 619 struct sysfs_open_file *of)
85a4ffad
TH
620{
621 struct sysfs_open_dirent *od, *new_od = NULL;
622
623 retry:
c75ec764 624 mutex_lock(&sysfs_open_file_mutex);
83db93f4 625 spin_lock_irq(&sysfs_open_dirent_lock);
85a4ffad
TH
626
627 if (!sd->s_attr.open && new_od) {
628 sd->s_attr.open = new_od;
629 new_od = NULL;
630 }
631
632 od = sd->s_attr.open;
633 if (od) {
634 atomic_inc(&od->refcnt);
58282d8d 635 list_add_tail(&of->list, &od->files);
85a4ffad
TH
636 }
637
83db93f4 638 spin_unlock_irq(&sysfs_open_dirent_lock);
c75ec764 639 mutex_unlock(&sysfs_open_file_mutex);
85a4ffad
TH
640
641 if (od) {
642 kfree(new_od);
643 return 0;
644 }
645
646 /* not there, initialize a new one and retry */
647 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
648 if (!new_od)
649 return -ENOMEM;
650
651 atomic_set(&new_od->refcnt, 0);
a4e8b912
TH
652 atomic_set(&new_od->event, 1);
653 init_waitqueue_head(&new_od->poll);
58282d8d 654 INIT_LIST_HEAD(&new_od->files);
85a4ffad
TH
655 goto retry;
656}
657
658/**
659 * sysfs_put_open_dirent - put sysfs_open_dirent
660 * @sd: target sysfs_dirent
58282d8d 661 * @of: associated sysfs_open_file
85a4ffad 662 *
58282d8d
TH
663 * Put @sd->s_attr.open and unlink @of from the files list. If
664 * reference count reaches zero, disassociate and free it.
85a4ffad
TH
665 *
666 * LOCKING:
667 * None.
668 */
669static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
58282d8d 670 struct sysfs_open_file *of)
85a4ffad
TH
671{
672 struct sysfs_open_dirent *od = sd->s_attr.open;
83db93f4 673 unsigned long flags;
85a4ffad 674
c75ec764 675 mutex_lock(&sysfs_open_file_mutex);
83db93f4 676 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
85a4ffad 677
73d97146
TH
678 if (of)
679 list_del(&of->list);
680
85a4ffad
TH
681 if (atomic_dec_and_test(&od->refcnt))
682 sd->s_attr.open = NULL;
683 else
684 od = NULL;
685
83db93f4 686 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
c75ec764 687 mutex_unlock(&sysfs_open_file_mutex);
85a4ffad
TH
688
689 kfree(od);
690}
691
c6fb4495 692static int kernfs_file_open(struct inode *inode, struct file *file)
1da177e4 693{
3e519038 694 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
f6acf8bb 695 const struct kernfs_ops *ops;
58282d8d 696 struct sysfs_open_file *of;
027a485d 697 bool has_read, has_write, has_mmap;
000f2a4d 698 int error = -EACCES;
1da177e4 699
e72ceb8c 700 if (!sysfs_get_active(attr_sd))
0ab66088 701 return -ENODEV;
1da177e4 702
f6acf8bb 703 ops = kernfs_ops(attr_sd);
1da177e4 704
f6acf8bb
TH
705 has_read = ops->seq_show || ops->read || ops->mmap;
706 has_write = ops->write || ops->mmap;
707 has_mmap = ops->mmap;
1da177e4 708
49fe6047
TH
709 /* check perms and supported operations */
710 if ((file->f_mode & FMODE_WRITE) &&
711 (!(inode->i_mode & S_IWUGO) || !has_write))
712 goto err_out;
713
714 if ((file->f_mode & FMODE_READ) &&
715 (!(inode->i_mode & S_IRUGO) || !has_read))
716 goto err_out;
717
13c589d5 718 /* allocate a sysfs_open_file for the file */
0ab66088 719 error = -ENOMEM;
58282d8d
TH
720 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
721 if (!of)
7b595756 722 goto err_out;
1da177e4 723
027a485d
TH
724 /*
725 * The following is done to give a different lockdep key to
726 * @of->mutex for files which implement mmap. This is a rather
727 * crude way to avoid false positive lockdep warning around
728 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
729 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
730 * which mm->mmap_sem nests, while holding @of->mutex. As each
731 * open file has a separate mutex, it's okay as long as those don't
732 * happen on the same file. At this point, we can't easily give
733 * each file a separate locking class. Let's differentiate on
734 * whether the file has mmap or not for now.
735 */
736 if (has_mmap)
737 mutex_init(&of->mutex);
738 else
739 mutex_init(&of->mutex);
740
bcafe4ee
TH
741 of->sd = attr_sd;
742 of->file = file;
13c589d5
TH
743
744 /*
49fe6047
TH
745 * Always instantiate seq_file even if read access doesn't use
746 * seq_file or is not requested. This unifies private data access
747 * and readable regular files are the vast majority anyway.
13c589d5 748 */
f6acf8bb 749 if (ops->seq_show)
c2b19daf 750 error = seq_open(file, &kernfs_seq_ops);
f6acf8bb
TH
751 else
752 error = seq_open(file, NULL);
13c589d5
TH
753 if (error)
754 goto err_free;
755
c2b19daf
TH
756 ((struct seq_file *)file->private_data)->private = of;
757
13c589d5
TH
758 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
759 if (file->f_mode & FMODE_WRITE)
760 file->f_mode |= FMODE_PWRITE;
0ab66088 761
85a4ffad 762 /* make sure we have open dirent struct */
58282d8d 763 error = sysfs_get_open_dirent(attr_sd, of);
85a4ffad 764 if (error)
13c589d5 765 goto err_close;
85a4ffad 766
b05f0548 767 /* open succeeded, put active references */
e72ceb8c 768 sysfs_put_active(attr_sd);
0ab66088
TH
769 return 0;
770
13c589d5 771err_close:
c2b19daf 772 seq_release(inode, file);
13c589d5 773err_free:
58282d8d 774 kfree(of);
13c589d5 775err_out:
e72ceb8c 776 sysfs_put_active(attr_sd);
1da177e4
LT
777 return error;
778}
779
c6fb4495 780static int kernfs_file_release(struct inode *inode, struct file *filp)
1da177e4 781{
85a4ffad 782 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
13c589d5 783 struct sysfs_open_file *of = sysfs_of(filp);
1da177e4 784
58282d8d 785 sysfs_put_open_dirent(sd, of);
c2b19daf 786 seq_release(inode, filp);
58282d8d 787 kfree(of);
50ab1a72 788
1da177e4
LT
789 return 0;
790}
791
73d97146
TH
792void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
793{
794 struct sysfs_open_dirent *od;
795 struct sysfs_open_file *of;
796
f6acf8bb 797 if (!(sd->s_flags & SYSFS_FLAG_HAS_MMAP))
73d97146
TH
798 return;
799
800 spin_lock_irq(&sysfs_open_dirent_lock);
801 od = sd->s_attr.open;
802 if (od)
803 atomic_inc(&od->refcnt);
804 spin_unlock_irq(&sysfs_open_dirent_lock);
805 if (!od)
806 return;
807
808 mutex_lock(&sysfs_open_file_mutex);
809 list_for_each_entry(of, &od->files, list) {
810 struct inode *inode = file_inode(of->file);
811 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
812 }
813 mutex_unlock(&sysfs_open_file_mutex);
814
815 sysfs_put_open_dirent(sd, NULL);
816}
817
4508a7a7
N
818/* Sysfs attribute files are pollable. The idea is that you read
819 * the content and then you use 'poll' or 'select' to wait for
820 * the content to change. When the content changes (assuming the
821 * manager for the kobject supports notification), poll will
822 * return POLLERR|POLLPRI, and select will return the fd whether
823 * it is waiting for read, write, or exceptions.
824 * Once poll/select indicates that the value has changed, you
2424b5dd 825 * need to close and re-open the file, or seek to 0 and read again.
4508a7a7
N
826 * Reminder: this only works for attributes which actively support
827 * it, and it is not possible to test an attribute from userspace
a93720ee 828 * to see if it supports poll (Neither 'poll' nor 'select' return
4508a7a7
N
829 * an appropriate error code). When in doubt, set a suitable timeout value.
830 */
c6fb4495 831static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait)
4508a7a7 832{
13c589d5 833 struct sysfs_open_file *of = sysfs_of(filp);
0ab66088 834 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
a4e8b912 835 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
0ab66088
TH
836
837 /* need parent for the kobj, grab both */
e72ceb8c 838 if (!sysfs_get_active(attr_sd))
0ab66088 839 goto trigger;
4508a7a7 840
a4e8b912 841 poll_wait(filp, &od->poll, wait);
4508a7a7 842
e72ceb8c 843 sysfs_put_active(attr_sd);
0ab66088 844
58282d8d 845 if (of->event != atomic_read(&od->event))
0ab66088 846 goto trigger;
4508a7a7 847
1af3557a 848 return DEFAULT_POLLMASK;
0ab66088
TH
849
850 trigger:
1af3557a 851 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
4508a7a7
N
852}
853
024f6471
TH
854/**
855 * kernfs_notify - notify a kernfs file
856 * @sd: file to notify
857 *
858 * Notify @sd such that poll(2) on @sd wakes up.
859 */
860void kernfs_notify(struct sysfs_dirent *sd)
f1282c84
NB
861{
862 struct sysfs_open_dirent *od;
83db93f4 863 unsigned long flags;
f1282c84 864
83db93f4 865 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
f1282c84 866
fc60bb83
ND
867 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
868 od = sd->s_attr.open;
869 if (od) {
870 atomic_inc(&od->event);
871 wake_up_interruptible(&od->poll);
872 }
f1282c84
NB
873 }
874
83db93f4 875 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
f1282c84 876}
024f6471 877EXPORT_SYMBOL_GPL(kernfs_notify);
f1282c84 878
8c0e3998 879void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
4508a7a7 880{
024f6471 881 struct sysfs_dirent *sd = k->sd, *tmp;
51225039
TH
882
883 if (sd && dir)
024f6471
TH
884 sd = sysfs_get_dirent(sd, dir);
885 else
886 sysfs_get(sd);
887
888 if (sd && attr) {
889 tmp = sysfs_get_dirent(sd, attr);
890 sysfs_put(sd);
891 sd = tmp;
892 }
51225039 893
024f6471
TH
894 if (sd) {
895 kernfs_notify(sd);
896 sysfs_put(sd);
897 }
4508a7a7
N
898}
899EXPORT_SYMBOL_GPL(sysfs_notify);
900
c6fb4495 901const struct file_operations kernfs_file_operations = {
c2b19daf 902 .read = kernfs_file_read,
50b38ca0 903 .write = kernfs_file_write,
044e3bc3 904 .llseek = generic_file_llseek,
fdbffaa4 905 .mmap = kernfs_file_mmap,
c6fb4495
TH
906 .open = kernfs_file_open,
907 .release = kernfs_file_release,
908 .poll = kernfs_file_poll,
f9b9a621
TH
909};
910
f6acf8bb
TH
911static const struct kernfs_ops sysfs_file_kfops_empty = {
912};
913
914static const struct kernfs_ops sysfs_file_kfops_ro = {
915 .seq_show = sysfs_kf_seq_show,
916};
917
918static const struct kernfs_ops sysfs_file_kfops_wo = {
919 .write = sysfs_kf_write,
920};
921
922static const struct kernfs_ops sysfs_file_kfops_rw = {
923 .seq_show = sysfs_kf_seq_show,
924 .write = sysfs_kf_write,
925};
926
927static const struct kernfs_ops sysfs_bin_kfops_ro = {
928 .read = sysfs_kf_bin_read,
929};
930
931static const struct kernfs_ops sysfs_bin_kfops_wo = {
932 .write = sysfs_kf_bin_write,
933};
934
935static const struct kernfs_ops sysfs_bin_kfops_rw = {
936 .read = sysfs_kf_bin_read,
937 .write = sysfs_kf_bin_write,
938 .mmap = sysfs_kf_bin_mmap,
939};
940
58292cbe 941int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
a7dc66df 942 const struct attribute *attr, bool is_bin,
496f7394 943 umode_t mode, const void *ns)
1da177e4 944{
517e64f5 945 struct lock_class_key *key = NULL;
f6acf8bb 946 const struct kernfs_ops *ops;
a26cd722 947 struct sysfs_dirent *sd;
471bd7b7 948 loff_t size;
1da177e4 949
a7dc66df 950 if (!is_bin) {
f6acf8bb
TH
951 struct kobject *kobj = dir_sd->priv;
952 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
953
954 /* every kobject with an attribute needs a ktype assigned */
955 if (WARN(!sysfs_ops, KERN_ERR
956 "missing sysfs attribute operations for kobject: %s\n",
957 kobject_name(kobj)))
958 return -EINVAL;
959
960 if (sysfs_ops->show && sysfs_ops->store)
961 ops = &sysfs_file_kfops_rw;
962 else if (sysfs_ops->show)
963 ops = &sysfs_file_kfops_ro;
964 else if (sysfs_ops->store)
965 ops = &sysfs_file_kfops_wo;
966 else
967 ops = &sysfs_file_kfops_empty;
471bd7b7
TH
968
969 size = PAGE_SIZE;
f6acf8bb
TH
970 } else {
971 struct bin_attribute *battr = (void *)attr;
972
973 if ((battr->read && battr->write) || battr->mmap)
974 ops = &sysfs_bin_kfops_rw;
975 else if (battr->read)
976 ops = &sysfs_bin_kfops_ro;
977 else if (battr->write)
978 ops = &sysfs_bin_kfops_wo;
979 else
980 ops = &sysfs_file_kfops_empty;
471bd7b7
TH
981
982 size = battr->size;
f6acf8bb
TH
983 }
984
517e64f5
TH
985#ifdef CONFIG_DEBUG_LOCK_ALLOC
986 if (!attr->ignore_lockdep)
987 key = attr->key ?: (struct lock_class_key *)&attr->skey;
988#endif
989 sd = kernfs_create_file_ns_key(dir_sd, attr->name, mode, size,
990 ops, (void *)attr, ns, key);
496f7394
TH
991 if (IS_ERR(sd)) {
992 if (PTR_ERR(sd) == -EEXIST)
993 sysfs_warn_dup(dir_sd, attr->name);
994 return PTR_ERR(sd);
995 }
996 return 0;
997}
998
999/**
517e64f5 1000 * kernfs_create_file_ns_key - create a file
496f7394
TH
1001 * @parent: directory to create the file in
1002 * @name: name of the file
1003 * @mode: mode of the file
1004 * @size: size of the file
1005 * @ops: kernfs operations for the file
1006 * @priv: private data for the file
1007 * @ns: optional namespace tag of the file
517e64f5 1008 * @key: lockdep key for the file's active_ref, %NULL to disable lockdep
496f7394
TH
1009 *
1010 * Returns the created node on success, ERR_PTR() value on error.
1011 */
517e64f5
TH
1012struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent,
1013 const char *name,
1014 umode_t mode, loff_t size,
1015 const struct kernfs_ops *ops,
1016 void *priv, const void *ns,
1017 struct lock_class_key *key)
496f7394
TH
1018{
1019 struct sysfs_addrm_cxt acxt;
1020 struct sysfs_dirent *sd;
1021 int rc;
1022
1023 sd = sysfs_new_dirent(name, (mode & S_IALLUGO) | S_IFREG,
1024 SYSFS_KOBJ_ATTR);
3007e997 1025 if (!sd)
496f7394 1026 return ERR_PTR(-ENOMEM);
487505c2 1027
f6acf8bb 1028 sd->s_attr.ops = ops;
471bd7b7 1029 sd->s_attr.size = size;
487505c2 1030 sd->s_ns = ns;
496f7394 1031 sd->priv = priv;
517e64f5
TH
1032
1033#ifdef CONFIG_DEBUG_LOCK_ALLOC
1034 if (key) {
1035 lockdep_init_map(&sd->dep_map, "s_active", key, 0);
1036 sd->s_flags |= SYSFS_FLAG_LOCKDEP;
1037 }
1038#endif
1da177e4 1039
f6acf8bb
TH
1040 /*
1041 * sd->s_attr.ops is accesible only while holding active ref. We
1042 * need to know whether some ops are implemented outside active
1043 * ref. Cache their existence in flags.
1044 */
1045 if (ops->seq_show)
1046 sd->s_flags |= SYSFS_FLAG_HAS_SEQ_SHOW;
1047 if (ops->mmap)
1048 sd->s_flags |= SYSFS_FLAG_HAS_MMAP;
1049
d69ac5a0 1050 sysfs_addrm_start(&acxt);
2d0cfbec 1051 rc = sysfs_add_one(&acxt, sd, parent);
23dc2799 1052 sysfs_addrm_finish(&acxt);
a26cd722 1053
496f7394 1054 if (rc) {
967e35dc 1055 sysfs_put(sd);
496f7394
TH
1056 return ERR_PTR(rc);
1057 }
1058 return sd;
1da177e4
LT
1059}
1060
0f423895 1061int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
a7dc66df 1062 bool is_bin)
0f423895 1063{
a7dc66df 1064 return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL);
0f423895
JB
1065}
1066
1da177e4 1067/**
58292cbe
TH
1068 * sysfs_create_file_ns - create an attribute file for an object with custom ns
1069 * @kobj: object we're creating for
1070 * @attr: attribute descriptor
1071 * @ns: namespace the new file should belong to
1da177e4 1072 */
58292cbe
TH
1073int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
1074 const void *ns)
1da177e4 1075{
608e266a 1076 BUG_ON(!kobj || !kobj->sd || !attr);
1da177e4 1077
a7dc66df 1078 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
1da177e4
LT
1079
1080}
58292cbe 1081EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
1da177e4 1082
1c205ae1
AK
1083int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
1084{
1085 int err = 0;
1086 int i;
1087
1088 for (i = 0; ptr[i] && !err; i++)
1089 err = sysfs_create_file(kobj, ptr[i]);
1090 if (err)
1091 while (--i >= 0)
1092 sysfs_remove_file(kobj, ptr[i]);
1093 return err;
1094}
1b866757 1095EXPORT_SYMBOL_GPL(sysfs_create_files);
1da177e4 1096
dfa87c82
AS
1097/**
1098 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
1099 * @kobj: object we're acting for.
1100 * @attr: attribute descriptor.
1101 * @group: group name.
1102 */
1103int sysfs_add_file_to_group(struct kobject *kobj,
1104 const struct attribute *attr, const char *group)
1105{
608e266a 1106 struct sysfs_dirent *dir_sd;
dfa87c82
AS
1107 int error;
1108
11f24fbd 1109 if (group)
388975cc 1110 dir_sd = sysfs_get_dirent(kobj->sd, group);
11f24fbd
JB
1111 else
1112 dir_sd = sysfs_get(kobj->sd);
1113
608e266a
TH
1114 if (!dir_sd)
1115 return -ENOENT;
1116
a7dc66df 1117 error = sysfs_add_file(dir_sd, attr, false);
608e266a
TH
1118 sysfs_put(dir_sd);
1119
dfa87c82
AS
1120 return error;
1121}
1122EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
1123
31e5abe9
KS
1124/**
1125 * sysfs_chmod_file - update the modified mode value on an object attribute.
1126 * @kobj: object we're acting for.
1127 * @attr: attribute descriptor.
1128 * @mode: file permissions.
1129 *
1130 */
49c19400 1131int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
48176a97 1132 umode_t mode)
31e5abe9 1133{
06fc0d66 1134 struct sysfs_dirent *sd;
bc062b1b 1135 struct iattr newattrs;
51225039
TH
1136 int rc;
1137
5d60418e 1138 sd = sysfs_get_dirent(kobj->sd, attr->name);
06fc0d66 1139 if (!sd)
5d60418e 1140 return -ENOENT;
f88123ea 1141
06fc0d66 1142 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
4c6974f5 1143 newattrs.ia_valid = ATTR_MODE;
f88123ea 1144
5d60418e
TH
1145 rc = kernfs_setattr(sd, &newattrs);
1146
1147 sysfs_put(sd);
51225039 1148 return rc;
31e5abe9
KS
1149}
1150EXPORT_SYMBOL_GPL(sysfs_chmod_file);
1151
1da177e4 1152/**
58292cbe
TH
1153 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
1154 * @kobj: object we're acting for
1155 * @attr: attribute descriptor
1156 * @ns: namespace tag of the file to remove
1da177e4 1157 *
58292cbe 1158 * Hash the attribute name and namespace tag and kill the victim.
1da177e4 1159 */
58292cbe
TH
1160void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
1161 const void *ns)
1da177e4 1162{
58292cbe 1163 struct sysfs_dirent *dir_sd = kobj->sd;
487505c2 1164
879f40d1 1165 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
1da177e4 1166}
58292cbe 1167EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
1da177e4 1168
1b18dc2b 1169void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
1c205ae1
AK
1170{
1171 int i;
1172 for (i = 0; ptr[i]; i++)
1173 sysfs_remove_file(kobj, ptr[i]);
1174}
1b866757 1175EXPORT_SYMBOL_GPL(sysfs_remove_files);
1da177e4 1176
dfa87c82
AS
1177/**
1178 * sysfs_remove_file_from_group - remove an attribute file from a group.
1179 * @kobj: object we're acting for.
1180 * @attr: attribute descriptor.
1181 * @group: group name.
1182 */
1183void sysfs_remove_file_from_group(struct kobject *kobj,
1184 const struct attribute *attr, const char *group)
1185{
608e266a 1186 struct sysfs_dirent *dir_sd;
dfa87c82 1187
11f24fbd 1188 if (group)
388975cc 1189 dir_sd = sysfs_get_dirent(kobj->sd, group);
11f24fbd
JB
1190 else
1191 dir_sd = sysfs_get(kobj->sd);
608e266a 1192 if (dir_sd) {
879f40d1 1193 kernfs_remove_by_name(dir_sd, attr->name);
608e266a 1194 sysfs_put(dir_sd);
dfa87c82
AS
1195 }
1196}
1197EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
1198
3124eb16
TH
1199/**
1200 * sysfs_create_bin_file - create binary file for object.
1201 * @kobj: object.
1202 * @attr: attribute descriptor.
1203 */
1204int sysfs_create_bin_file(struct kobject *kobj,
1205 const struct bin_attribute *attr)
1206{
1207 BUG_ON(!kobj || !kobj->sd || !attr);
1208
a7dc66df 1209 return sysfs_add_file(kobj->sd, &attr->attr, true);
3124eb16
TH
1210}
1211EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1212
1213/**
1214 * sysfs_remove_bin_file - remove binary file for object.
1215 * @kobj: object.
1216 * @attr: attribute descriptor.
1217 */
1218void sysfs_remove_bin_file(struct kobject *kobj,
1219 const struct bin_attribute *attr)
1220{
879f40d1 1221 kernfs_remove_by_name(kobj->sd, attr->attr.name);
3124eb16
TH
1222}
1223EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1224
d9a9cdfb 1225struct sysfs_schedule_callback_struct {
66942064
AC
1226 struct list_head workq_list;
1227 struct kobject *kobj;
d9a9cdfb
AS
1228 void (*func)(void *);
1229 void *data;
523ded71 1230 struct module *owner;
d9a9cdfb
AS
1231 struct work_struct work;
1232};
1233
d110271e 1234static struct workqueue_struct *sysfs_workqueue;
66942064
AC
1235static DEFINE_MUTEX(sysfs_workq_mutex);
1236static LIST_HEAD(sysfs_workq);
d9a9cdfb
AS
1237static void sysfs_schedule_callback_work(struct work_struct *work)
1238{
1239 struct sysfs_schedule_callback_struct *ss = container_of(work,
1240 struct sysfs_schedule_callback_struct, work);
1241
1242 (ss->func)(ss->data);
1243 kobject_put(ss->kobj);
523ded71 1244 module_put(ss->owner);
66942064
AC
1245 mutex_lock(&sysfs_workq_mutex);
1246 list_del(&ss->workq_list);
1247 mutex_unlock(&sysfs_workq_mutex);
d9a9cdfb
AS
1248 kfree(ss);
1249}
1250
1251/**
1252 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1253 * @kobj: object we're acting for.
1254 * @func: callback function to invoke later.
1255 * @data: argument to pass to @func.
523ded71 1256 * @owner: module owning the callback code
d9a9cdfb
AS
1257 *
1258 * sysfs attribute methods must not unregister themselves or their parent
1259 * kobject (which would amount to the same thing). Attempts to do so will
1260 * deadlock, since unregistration is mutually exclusive with driver
1261 * callbacks.
1262 *
1263 * Instead methods can call this routine, which will attempt to allocate
1264 * and schedule a workqueue request to call back @func with @data as its
1265 * argument in the workqueue's process context. @kobj will be pinned
1266 * until @func returns.
1267 *
1268 * Returns 0 if the request was submitted, -ENOMEM if storage could not
66942064
AC
1269 * be allocated, -ENODEV if a reference to @owner isn't available,
1270 * -EAGAIN if a callback has already been scheduled for @kobj.
d9a9cdfb
AS
1271 */
1272int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
523ded71 1273 void *data, struct module *owner)
d9a9cdfb 1274{
66942064 1275 struct sysfs_schedule_callback_struct *ss, *tmp;
d9a9cdfb 1276
523ded71
AS
1277 if (!try_module_get(owner))
1278 return -ENODEV;
66942064
AC
1279
1280 mutex_lock(&sysfs_workq_mutex);
1281 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1282 if (ss->kobj == kobj) {
d110271e 1283 module_put(owner);
66942064
AC
1284 mutex_unlock(&sysfs_workq_mutex);
1285 return -EAGAIN;
1286 }
1287 mutex_unlock(&sysfs_workq_mutex);
1288
d110271e 1289 if (sysfs_workqueue == NULL) {
086a377e 1290 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
d110271e
AC
1291 if (sysfs_workqueue == NULL) {
1292 module_put(owner);
1293 return -ENOMEM;
1294 }
1295 }
1296
d9a9cdfb 1297 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
523ded71
AS
1298 if (!ss) {
1299 module_put(owner);
d9a9cdfb 1300 return -ENOMEM;
523ded71 1301 }
d9a9cdfb
AS
1302 kobject_get(kobj);
1303 ss->kobj = kobj;
1304 ss->func = func;
1305 ss->data = data;
523ded71 1306 ss->owner = owner;
d9a9cdfb 1307 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
66942064
AC
1308 INIT_LIST_HEAD(&ss->workq_list);
1309 mutex_lock(&sysfs_workq_mutex);
1310 list_add_tail(&ss->workq_list, &sysfs_workq);
1311 mutex_unlock(&sysfs_workq_mutex);
d110271e 1312 queue_work(sysfs_workqueue, &ss->work);
d9a9cdfb
AS
1313 return 0;
1314}
1315EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
This page took 0.787062 seconds and 5 git commands to generate.