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