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