aboutsummaryrefslogtreecommitdiffstats
path: root/fs/sysfs/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/sysfs/file.c')
-rw-r--r--fs/sysfs/file.c961
1 files changed, 182 insertions, 779 deletions
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 35e7d08fe629..810cf6e613e5 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -14,70 +14,23 @@
14#include <linux/kobject.h> 14#include <linux/kobject.h>
15#include <linux/kallsyms.h> 15#include <linux/kallsyms.h>
16#include <linux/slab.h> 16#include <linux/slab.h>
17#include <linux/fsnotify.h>
18#include <linux/namei.h>
19#include <linux/poll.h>
20#include <linux/list.h> 17#include <linux/list.h>
21#include <linux/mutex.h> 18#include <linux/mutex.h>
22#include <linux/limits.h>
23#include <linux/uaccess.h>
24#include <linux/seq_file.h> 19#include <linux/seq_file.h>
25#include <linux/mm.h>
26 20
27#include "sysfs.h" 21#include "sysfs.h"
22#include "../kernfs/kernfs-internal.h"
28 23
29/* 24/*
30 * There's one sysfs_open_file for each open file and one sysfs_open_dirent 25 * Determine ktype->sysfs_ops for the given kernfs_node. This function
31 * for each sysfs_dirent with one or more open files.
32 *
33 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
34 * protected by sysfs_open_dirent_lock.
35 *
36 * filp->private_data points to seq_file whose ->private points to
37 * sysfs_open_file. sysfs_open_files are chained at
38 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
39 */
40static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
41static DEFINE_MUTEX(sysfs_open_file_mutex);
42
43struct sysfs_open_dirent {
44 atomic_t refcnt;
45 atomic_t event;
46 wait_queue_head_t poll;
47 struct list_head files; /* goes through sysfs_open_file.list */
48};
49
50struct sysfs_open_file {
51 struct sysfs_dirent *sd;
52 struct file *file;
53 struct mutex mutex;
54 int event;
55 struct list_head list;
56
57 bool mmapped;
58 const struct vm_operations_struct *vm_ops;
59};
60
61static bool sysfs_is_bin(struct sysfs_dirent *sd)
62{
63 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
64}
65
66static struct sysfs_open_file *sysfs_of(struct file *file)
67{
68 return ((struct seq_file *)file->private_data)->private;
69}
70
71/*
72 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
73 * must be called while holding an active reference. 26 * must be called while holding an active reference.
74 */ 27 */
75static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd) 28static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
76{ 29{
77 struct kobject *kobj = sd->s_parent->s_dir.kobj; 30 struct kobject *kobj = kn->parent->priv;
78 31
79 if (!sysfs_ignore_lockdep(sd)) 32 if (kn->flags & KERNFS_LOCKDEP)
80 lockdep_assert_held(sd); 33 lockdep_assert_held(kn);
81 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL; 34 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
82} 35}
83 36
@@ -86,13 +39,13 @@ static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
86 * details like buffering and seeking. The following function pipes 39 * details like buffering and seeking. The following function pipes
87 * sysfs_ops->show() result through seq_file. 40 * sysfs_ops->show() result through seq_file.
88 */ 41 */
89static int sysfs_seq_show(struct seq_file *sf, void *v) 42static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
90{ 43{
91 struct sysfs_open_file *of = sf->private; 44 struct kernfs_open_file *of = sf->private;
92 struct kobject *kobj = of->sd->s_parent->s_dir.kobj; 45 struct kobject *kobj = of->kn->parent->priv;
93 const struct sysfs_ops *ops; 46 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
94 char *buf;
95 ssize_t count; 47 ssize_t count;
48 char *buf;
96 49
97 /* acquire buffer and ensure that it's >= PAGE_SIZE */ 50 /* acquire buffer and ensure that it's >= PAGE_SIZE */
98 count = seq_get_buf(sf, &buf); 51 count = seq_get_buf(sf, &buf);
@@ -102,34 +55,15 @@ static int sysfs_seq_show(struct seq_file *sf, void *v)
102 } 55 }
103 56
104 /* 57 /*
105 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex 58 * Invoke show(). Control may reach here via seq file lseek even
106 * nests outside active ref and is just to ensure that the ops 59 * if @ops->show() isn't implemented.
107 * aren't called concurrently for the same open file.
108 */ 60 */
109 mutex_lock(&of->mutex); 61 if (ops->show) {
110 if (!sysfs_get_active(of->sd)) { 62 count = ops->show(kobj, of->kn->priv, buf);
111 mutex_unlock(&of->mutex); 63 if (count < 0)
112 return -ENODEV; 64 return count;
113 } 65 }
114 66
115 of->event = atomic_read(&of->sd->s_attr.open->event);
116
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;
126
127 sysfs_put_active(of->sd);
128 mutex_unlock(&of->mutex);
129
130 if (count < 0)
131 return count;
132
133 /* 67 /*
134 * The code works fine with PAGE_SIZE return but it's likely to 68 * The code works fine with PAGE_SIZE return but it's likely to
135 * indicate truncated result or overflow in normal use cases. 69 * indicate truncated result or overflow in normal use cases.
@@ -144,726 +78,194 @@ static int sysfs_seq_show(struct seq_file *sf, void *v)
144 return 0; 78 return 0;
145} 79}
146 80
147/* 81static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
148 * Read method for bin files. As reading a bin file can have side-effects, 82 size_t count, loff_t pos)
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{ 83{
156 struct sysfs_open_file *of = sysfs_of(file); 84 struct bin_attribute *battr = of->kn->priv;
157 struct bin_attribute *battr = of->sd->s_attr.bin_attr; 85 struct kobject *kobj = of->kn->parent->priv;
158 struct kobject *kobj = of->sd->s_parent->s_dir.kobj; 86 loff_t size = file_inode(of->file)->i_size;
159 loff_t size = file_inode(file)->i_size;
160 int count = min_t(size_t, bytes, PAGE_SIZE);
161 loff_t offs = *off;
162 char *buf;
163 87
164 if (!bytes) 88 if (!count)
165 return 0; 89 return 0;
166 90
167 if (size) { 91 if (size) {
168 if (offs > size) 92 if (pos > size)
169 return 0; 93 return 0;
170 if (offs + count > size) 94 if (pos + count > size)
171 count = size - offs; 95 count = size - pos;
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 } 96 }
201 97
202 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); 98 if (!battr->read)
203 99 return -EIO;
204 *off = offs + count;
205 100
206 out_free: 101 return battr->read(of->file, kobj, battr, buf, pos, count);
207 kfree(buf);
208 return count;
209} 102}
210 103
211/** 104/* kernfs write callback for regular sysfs files */
212 * flush_write_buffer - push buffer to kobject 105static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
213 * @of: open file 106 size_t count, loff_t pos)
214 * @buf: data buffer for file
215 * @off: file offset to write to
216 * @count: number of bytes
217 *
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.
220 */
221static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
222 size_t count)
223{ 107{
224 struct kobject *kobj = of->sd->s_parent->s_dir.kobj; 108 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
225 int rc = 0; 109 struct kobject *kobj = of->kn->parent->priv;
226
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);
235 return -ENODEV;
236 }
237 110
238 if (sysfs_is_bin(of->sd)) { 111 if (!count)
239 struct bin_attribute *battr = of->sd->s_attr.bin_attr; 112 return 0;
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 }
250
251 sysfs_put_active(of->sd);
252 mutex_unlock(&of->mutex);
253 113
254 return rc; 114 return ops->store(kobj, of->kn->priv, buf, count);
255} 115}
256 116
257/** 117/* kernfs write callback for bin sysfs files */
258 * sysfs_write_file - write an attribute 118static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
259 * @file: file pointer 119 size_t count, loff_t pos)
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().
266 *
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.
272 */
273static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
274 size_t count, loff_t *ppos)
275{ 120{
276 struct sysfs_open_file *of = sysfs_of(file); 121 struct bin_attribute *battr = of->kn->priv;
277 ssize_t len = min_t(size_t, count, PAGE_SIZE); 122 struct kobject *kobj = of->kn->parent->priv;
278 loff_t size = file_inode(file)->i_size; 123 loff_t size = file_inode(of->file)->i_size;
279 char *buf;
280 124
281 if (sysfs_is_bin(of->sd) && size) { 125 if (size) {
282 if (size <= *ppos) 126 if (size <= pos)
283 return 0; 127 return 0;
284 len = min_t(ssize_t, len, size - *ppos); 128 count = min_t(ssize_t, count, size - pos);
285 } 129 }
286 130 if (!count)
287 if (!len)
288 return 0; 131 return 0;
289 132
290 buf = kmalloc(len + 1, GFP_KERNEL); 133 if (!battr->write)
291 if (!buf) 134 return -EIO;
292 return -ENOMEM;
293 135
294 if (copy_from_user(buf, user_buf, len)) { 136 return battr->write(of->file, kobj, battr, buf, pos, count);
295 len = -EFAULT;
296 goto out_free;
297 }
298 buf[len] = '\0'; /* guarantee string termination */
299
300 len = flush_write_buffer(of, buf, *ppos, len);
301 if (len > 0)
302 *ppos += len;
303out_free:
304 kfree(buf);
305 return len;
306}
307
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} 137}
324 138
325static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 139static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
140 struct vm_area_struct *vma)
326{ 141{
327 struct file *file = vma->vm_file; 142 struct bin_attribute *battr = of->kn->priv;
328 struct sysfs_open_file *of = sysfs_of(file); 143 struct kobject *kobj = of->kn->parent->priv;
329 int ret;
330 144
331 if (!of->vm_ops) 145 return battr->mmap(of->file, kobj, battr, vma);
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} 146}
344 147
345static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma, 148void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
346 struct vm_fault *vmf)
347{ 149{
348 struct file *file = vma->vm_file; 150 struct kernfs_node *kn = kobj->sd, *tmp;
349 struct sysfs_open_file *of = sysfs_of(file);
350 int ret;
351
352 if (!of->vm_ops)
353 return VM_FAULT_SIGBUS;
354 151
355 if (!sysfs_get_active(of->sd)) 152 if (kn && dir)
356 return VM_FAULT_SIGBUS; 153 kn = kernfs_find_and_get(kn, dir);
357
358 ret = 0;
359 if (of->vm_ops->page_mkwrite)
360 ret = of->vm_ops->page_mkwrite(vma, vmf);
361 else 154 else
362 file_update_time(file); 155 kernfs_get(kn);
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);
469 struct bin_attribute *battr = of->sd->s_attr.bin_attr;
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
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 156
499 /* 157 if (kn && attr) {
500 * It is not possible to successfully wrap close. 158 tmp = kernfs_find_and_get(kn, attr);
501 * So error if someone is trying to use close. 159 kernfs_put(kn);
502 */ 160 kn = tmp;
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
519/**
520 * sysfs_get_open_dirent - get or create sysfs_open_dirent
521 * @sd: target sysfs_dirent
522 * @of: sysfs_open_file for this instance of open
523 *
524 * If @sd->s_attr.open exists, increment its reference count;
525 * otherwise, create one. @of is chained to the files list.
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,
534 struct sysfs_open_file *of)
535{
536 struct sysfs_open_dirent *od, *new_od = NULL;
537
538 retry:
539 mutex_lock(&sysfs_open_file_mutex);
540 spin_lock_irq(&sysfs_open_dirent_lock);
541
542 if (!sd->s_attr.open && new_od) {
543 sd->s_attr.open = new_od;
544 new_od = NULL;
545 } 161 }
546 162
547 od = sd->s_attr.open; 163 if (kn) {
548 if (od) { 164 kernfs_notify(kn);
549 atomic_inc(&od->refcnt); 165 kernfs_put(kn);
550 list_add_tail(&of->list, &od->files);
551 }
552
553 spin_unlock_irq(&sysfs_open_dirent_lock);
554 mutex_unlock(&sysfs_open_file_mutex);
555
556 if (od) {
557 kfree(new_od);
558 return 0;
559 } 166 }
167}
168EXPORT_SYMBOL_GPL(sysfs_notify);
560 169
561 /* not there, initialize a new one and retry */ 170static const struct kernfs_ops sysfs_file_kfops_empty = {
562 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL); 171};
563 if (!new_od)
564 return -ENOMEM;
565 172
566 atomic_set(&new_od->refcnt, 0); 173static const struct kernfs_ops sysfs_file_kfops_ro = {
567 atomic_set(&new_od->event, 1); 174 .seq_show = sysfs_kf_seq_show,
568 init_waitqueue_head(&new_od->poll); 175};
569 INIT_LIST_HEAD(&new_od->files);
570 goto retry;
571}
572 176
573/** 177static const struct kernfs_ops sysfs_file_kfops_wo = {
574 * sysfs_put_open_dirent - put sysfs_open_dirent 178 .write = sysfs_kf_write,
575 * @sd: target sysfs_dirent 179};
576 * @of: associated sysfs_open_file
577 *
578 * Put @sd->s_attr.open and unlink @of from the files list. If
579 * reference count reaches zero, disassociate and free it.
580 *
581 * LOCKING:
582 * None.
583 */
584static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
585 struct sysfs_open_file *of)
586{
587 struct sysfs_open_dirent *od = sd->s_attr.open;
588 unsigned long flags;
589 180
590 mutex_lock(&sysfs_open_file_mutex); 181static const struct kernfs_ops sysfs_file_kfops_rw = {
591 spin_lock_irqsave(&sysfs_open_dirent_lock, flags); 182 .seq_show = sysfs_kf_seq_show,
183 .write = sysfs_kf_write,
184};
592 185
593 if (of) 186static const struct kernfs_ops sysfs_bin_kfops_ro = {
594 list_del(&of->list); 187 .read = sysfs_kf_bin_read,
188};
595 189
596 if (atomic_dec_and_test(&od->refcnt)) 190static const struct kernfs_ops sysfs_bin_kfops_wo = {
597 sd->s_attr.open = NULL; 191 .write = sysfs_kf_bin_write,
598 else 192};
599 od = NULL;
600 193
601 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); 194static const struct kernfs_ops sysfs_bin_kfops_rw = {
602 mutex_unlock(&sysfs_open_file_mutex); 195 .read = sysfs_kf_bin_read,
196 .write = sysfs_kf_bin_write,
197};
603 198
604 kfree(od); 199static const struct kernfs_ops sysfs_bin_kfops_mmap = {
605} 200 .read = sysfs_kf_bin_read,
201 .write = sysfs_kf_bin_write,
202 .mmap = sysfs_kf_bin_mmap,
203};
606 204
607static int sysfs_open_file(struct inode *inode, struct file *file) 205int sysfs_add_file_mode_ns(struct kernfs_node *parent,
206 const struct attribute *attr, bool is_bin,
207 umode_t mode, const void *ns)
608{ 208{
609 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; 209 struct lock_class_key *key = NULL;
610 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; 210 const struct kernfs_ops *ops;
611 struct sysfs_open_file *of; 211 struct kernfs_node *kn;
612 bool has_read, has_write; 212 loff_t size;
613 int error = -EACCES;
614
615 /* need attr_sd for attr and ops, its parent for kobj */
616 if (!sysfs_get_active(attr_sd))
617 return -ENODEV;
618 213
619 if (sysfs_is_bin(attr_sd)) { 214 if (!is_bin) {
620 struct bin_attribute *battr = attr_sd->s_attr.bin_attr; 215 struct kobject *kobj = parent->priv;
621 216 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
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);
626 217
627 /* every kobject with an attribute needs a ktype assigned */ 218 /* every kobject with an attribute needs a ktype assigned */
628 if (WARN(!ops, KERN_ERR 219 if (WARN(!sysfs_ops, KERN_ERR
629 "missing sysfs attribute operations for kobject: %s\n", 220 "missing sysfs attribute operations for kobject: %s\n",
630 kobject_name(kobj))) 221 kobject_name(kobj)))
631 goto err_out; 222 return -EINVAL;
632 223
633 has_read = ops->show; 224 if (sysfs_ops->show && sysfs_ops->store)
634 has_write = ops->store; 225 ops = &sysfs_file_kfops_rw;
635 } 226 else if (sysfs_ops->show)
636 227 ops = &sysfs_file_kfops_ro;
637 /* check perms and supported operations */ 228 else if (sysfs_ops->store)
638 if ((file->f_mode & FMODE_WRITE) && 229 ops = &sysfs_file_kfops_wo;
639 (!(inode->i_mode & S_IWUGO) || !has_write)) 230 else
640 goto err_out; 231 ops = &sysfs_file_kfops_empty;
641 232
642 if ((file->f_mode & FMODE_READ) && 233 size = PAGE_SIZE;
643 (!(inode->i_mode & S_IRUGO) || !has_read)) 234 } else {
644 goto err_out; 235 struct bin_attribute *battr = (void *)attr;
645 236
646 /* allocate a sysfs_open_file for the file */ 237 if (battr->mmap)
647 error = -ENOMEM; 238 ops = &sysfs_bin_kfops_mmap;
648 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL); 239 else if (battr->read && battr->write)
649 if (!of) 240 ops = &sysfs_bin_kfops_rw;
650 goto err_out; 241 else if (battr->read)
651 242 ops = &sysfs_bin_kfops_ro;
652 /* 243 else if (battr->write)
653 * The following is done to give a different lockdep key to 244 ops = &sysfs_bin_kfops_wo;
654 * @of->mutex for files which implement mmap. This is a rather 245 else
655 * crude way to avoid false positive lockdep warning around 246 ops = &sysfs_file_kfops_empty;
656 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and 247
657 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under 248 size = battr->size;
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
662 * whether the file is bin or not for now.
663 */
664 if (sysfs_is_bin(attr_sd))
665 mutex_init(&of->mutex);
666 else
667 mutex_init(&of->mutex);
668
669 of->sd = attr_sd;
670 of->file = file;
671
672 /*
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.
676 */
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);
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;
687
688 /* make sure we have open dirent struct */
689 error = sysfs_get_open_dirent(attr_sd, of);
690 if (error)
691 goto err_close;
692
693 /* open succeeded, put active references */
694 sysfs_put_active(attr_sd);
695 return 0;
696
697err_close:
698 single_release(inode, file);
699err_free:
700 kfree(of);
701err_out:
702 sysfs_put_active(attr_sd);
703 return error;
704}
705
706static int sysfs_release(struct inode *inode, struct file *filp)
707{
708 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
709 struct sysfs_open_file *of = sysfs_of(filp);
710
711 sysfs_put_open_dirent(sd, of);
712 single_release(inode, filp);
713 kfree(of);
714
715 return 0;
716}
717
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 } 249 }
739 mutex_unlock(&sysfs_open_file_mutex);
740
741 sysfs_put_open_dirent(sd, NULL);
742}
743
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
751 * need to close and re-open the file, or seek to 0 and read again.
752 * Reminder: this only works for attributes which actively support
753 * it, and it is not possible to test an attribute from userspace
754 * to see if it supports poll (Neither 'poll' nor 'select' return
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{
759 struct sysfs_open_file *of = sysfs_of(filp);
760 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
761 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
762
763 /* need parent for the kobj, grab both */
764 if (!sysfs_get_active(attr_sd))
765 goto trigger;
766
767 poll_wait(filp, &od->poll, wait);
768 250
769 sysfs_put_active(attr_sd); 251#ifdef CONFIG_DEBUG_LOCK_ALLOC
770 252 if (!attr->ignore_lockdep)
771 if (of->event != atomic_read(&od->event)) 253 key = attr->key ?: (struct lock_class_key *)&attr->skey;
772 goto trigger; 254#endif
773 255 kn = __kernfs_create_file(parent, attr->name, mode, size, ops,
774 return DEFAULT_POLLMASK; 256 (void *)attr, ns, true, key);
775 257 if (IS_ERR(kn)) {
776 trigger: 258 if (PTR_ERR(kn) == -EEXIST)
777 return DEFAULT_POLLMASK|POLLERR|POLLPRI; 259 sysfs_warn_dup(parent, attr->name);
778} 260 return PTR_ERR(kn);
779
780void sysfs_notify_dirent(struct sysfs_dirent *sd)
781{
782 struct sysfs_open_dirent *od;
783 unsigned long flags;
784
785 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
786
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 }
793 } 261 }
794 262 return 0;
795 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
796}
797EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
798
799void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
800{
801 struct sysfs_dirent *sd = k->sd;
802
803 mutex_lock(&sysfs_mutex);
804
805 if (sd && dir)
806 sd = sysfs_find_dirent(sd, dir, NULL);
807 if (sd && attr)
808 sd = sysfs_find_dirent(sd, attr, NULL);
809 if (sd)
810 sysfs_notify_dirent(sd);
811
812 mutex_unlock(&sysfs_mutex);
813}
814EXPORT_SYMBOL_GPL(sysfs_notify);
815
816const struct file_operations sysfs_file_operations = {
817 .read = seq_read,
818 .write = sysfs_write_file,
819 .llseek = generic_file_llseek,
820 .open = sysfs_open_file,
821 .release = sysfs_release,
822 .poll = sysfs_poll,
823};
824
825const struct file_operations sysfs_bin_operations = {
826 .read = sysfs_bin_read,
827 .write = sysfs_write_file,
828 .llseek = generic_file_llseek,
829 .mmap = sysfs_bin_mmap,
830 .open = sysfs_open_file,
831 .release = sysfs_release,
832 .poll = sysfs_poll,
833};
834
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)
838{
839 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
840 struct sysfs_addrm_cxt acxt;
841 struct sysfs_dirent *sd;
842 int rc;
843
844 sd = sysfs_new_dirent(attr->name, mode, type);
845 if (!sd)
846 return -ENOMEM;
847
848 sd->s_ns = ns;
849 sd->s_attr.attr = (void *)attr;
850 sysfs_dirent_init_lockdep(sd);
851
852 sysfs_addrm_start(&acxt);
853 rc = sysfs_add_one(&acxt, sd, dir_sd);
854 sysfs_addrm_finish(&acxt);
855
856 if (rc)
857 sysfs_put(sd);
858
859 return rc;
860} 263}
861 264
862 265int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
863int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, 266 bool is_bin)
864 int type)
865{ 267{
866 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL); 268 return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
867} 269}
868 270
869/** 271/**
@@ -877,8 +279,7 @@ int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
877{ 279{
878 BUG_ON(!kobj || !kobj->sd || !attr); 280 BUG_ON(!kobj || !kobj->sd || !attr);
879 281
880 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR, 282 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
881 attr->mode, ns);
882 283
883} 284}
884EXPORT_SYMBOL_GPL(sysfs_create_file_ns); 285EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
@@ -906,19 +307,21 @@ EXPORT_SYMBOL_GPL(sysfs_create_files);
906int sysfs_add_file_to_group(struct kobject *kobj, 307int sysfs_add_file_to_group(struct kobject *kobj,
907 const struct attribute *attr, const char *group) 308 const struct attribute *attr, const char *group)
908{ 309{
909 struct sysfs_dirent *dir_sd; 310 struct kernfs_node *parent;
910 int error; 311 int error;
911 312
912 if (group) 313 if (group) {
913 dir_sd = sysfs_get_dirent(kobj->sd, group); 314 parent = kernfs_find_and_get(kobj->sd, group);
914 else 315 } else {
915 dir_sd = sysfs_get(kobj->sd); 316 parent = kobj->sd;
317 kernfs_get(parent);
318 }
916 319
917 if (!dir_sd) 320 if (!parent)
918 return -ENOENT; 321 return -ENOENT;
919 322
920 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR); 323 error = sysfs_add_file(parent, attr, false);
921 sysfs_put(dir_sd); 324 kernfs_put(parent);
922 325
923 return error; 326 return error;
924} 327}
@@ -934,23 +337,20 @@ EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
934int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, 337int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
935 umode_t mode) 338 umode_t mode)
936{ 339{
937 struct sysfs_dirent *sd; 340 struct kernfs_node *kn;
938 struct iattr newattrs; 341 struct iattr newattrs;
939 int rc; 342 int rc;
940 343
941 mutex_lock(&sysfs_mutex); 344 kn = kernfs_find_and_get(kobj->sd, attr->name);
942 345 if (!kn)
943 rc = -ENOENT; 346 return -ENOENT;
944 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
945 if (!sd)
946 goto out;
947 347
948 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO); 348 newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
949 newattrs.ia_valid = ATTR_MODE; 349 newattrs.ia_valid = ATTR_MODE;
950 rc = sysfs_sd_setattr(sd, &newattrs);
951 350
952 out: 351 rc = kernfs_setattr(kn, &newattrs);
953 mutex_unlock(&sysfs_mutex); 352
353 kernfs_put(kn);
954 return rc; 354 return rc;
955} 355}
956EXPORT_SYMBOL_GPL(sysfs_chmod_file); 356EXPORT_SYMBOL_GPL(sysfs_chmod_file);
@@ -966,9 +366,9 @@ EXPORT_SYMBOL_GPL(sysfs_chmod_file);
966void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, 366void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
967 const void *ns) 367 const void *ns)
968{ 368{
969 struct sysfs_dirent *dir_sd = kobj->sd; 369 struct kernfs_node *parent = kobj->sd;
970 370
971 sysfs_hash_and_remove(dir_sd, attr->name, ns); 371 kernfs_remove_by_name_ns(parent, attr->name, ns);
972} 372}
973EXPORT_SYMBOL_GPL(sysfs_remove_file_ns); 373EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
974 374
@@ -989,15 +389,18 @@ EXPORT_SYMBOL_GPL(sysfs_remove_files);
989void sysfs_remove_file_from_group(struct kobject *kobj, 389void sysfs_remove_file_from_group(struct kobject *kobj,
990 const struct attribute *attr, const char *group) 390 const struct attribute *attr, const char *group)
991{ 391{
992 struct sysfs_dirent *dir_sd; 392 struct kernfs_node *parent;
993 393
994 if (group) 394 if (group) {
995 dir_sd = sysfs_get_dirent(kobj->sd, group); 395 parent = kernfs_find_and_get(kobj->sd, group);
996 else 396 } else {
997 dir_sd = sysfs_get(kobj->sd); 397 parent = kobj->sd;
998 if (dir_sd) { 398 kernfs_get(parent);
999 sysfs_hash_and_remove(dir_sd, attr->name, NULL); 399 }
1000 sysfs_put(dir_sd); 400
401 if (parent) {
402 kernfs_remove_by_name(parent, attr->name);
403 kernfs_put(parent);
1001 } 404 }
1002} 405}
1003EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); 406EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
@@ -1012,7 +415,7 @@ int sysfs_create_bin_file(struct kobject *kobj,
1012{ 415{
1013 BUG_ON(!kobj || !kobj->sd || !attr); 416 BUG_ON(!kobj || !kobj->sd || !attr);
1014 417
1015 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); 418 return sysfs_add_file(kobj->sd, &attr->attr, true);
1016} 419}
1017EXPORT_SYMBOL_GPL(sysfs_create_bin_file); 420EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1018 421
@@ -1024,7 +427,7 @@ EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1024void sysfs_remove_bin_file(struct kobject *kobj, 427void sysfs_remove_bin_file(struct kobject *kobj,
1025 const struct bin_attribute *attr) 428 const struct bin_attribute *attr)
1026{ 429{
1027 sysfs_hash_and_remove(kobj->sd, attr->attr.name, NULL); 430 kernfs_remove_by_name(kobj->sd, attr->attr.name);
1028} 431}
1029EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); 432EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1030 433