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