GNU Linux-libre 4.4.288-gnu1
[releases.git] / fs / sysfs / file.c
1 /*
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.
11  */
12
13 #include <linux/module.h>
14 #include <linux/kobject.h>
15 #include <linux/kallsyms.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/seq_file.h>
20 #include <linux/mm.h>
21
22 #include "sysfs.h"
23 #include "../kernfs/kernfs-internal.h"
24
25 /*
26  * Determine ktype->sysfs_ops for the given kernfs_node.  This function
27  * must be called while holding an active reference.
28  */
29 static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
30 {
31         struct kobject *kobj = kn->parent->priv;
32
33         if (kn->flags & KERNFS_LOCKDEP)
34                 lockdep_assert_held(kn);
35         return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
36 }
37
38 /*
39  * Reads on sysfs are handled through seq_file, which takes care of hairy
40  * details like buffering and seeking.  The following function pipes
41  * sysfs_ops->show() result through seq_file.
42  */
43 static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
44 {
45         struct kernfs_open_file *of = sf->private;
46         struct kobject *kobj = of->kn->parent->priv;
47         const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
48         ssize_t count;
49         char *buf;
50
51         /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
52         count = seq_get_buf(sf, &buf);
53         if (count < PAGE_SIZE) {
54                 seq_commit(sf, -1);
55                 return 0;
56         }
57         memset(buf, 0, PAGE_SIZE);
58
59         /*
60          * Invoke show().  Control may reach here via seq file lseek even
61          * if @ops->show() isn't implemented.
62          */
63         if (ops->show) {
64                 count = ops->show(kobj, of->kn->priv, buf);
65                 if (count < 0)
66                         return count;
67         }
68
69         /*
70          * The code works fine with PAGE_SIZE return but it's likely to
71          * indicate truncated result or overflow in normal use cases.
72          */
73         if (count >= (ssize_t)PAGE_SIZE) {
74                 print_symbol("fill_read_buffer: %s returned bad count\n",
75                         (unsigned long)ops->show);
76                 /* Try to struggle along */
77                 count = PAGE_SIZE - 1;
78         }
79         seq_commit(sf, count);
80         return 0;
81 }
82
83 static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
84                                  size_t count, loff_t pos)
85 {
86         struct bin_attribute *battr = of->kn->priv;
87         struct kobject *kobj = of->kn->parent->priv;
88         loff_t size = file_inode(of->file)->i_size;
89
90         if (!count)
91                 return 0;
92
93         if (size) {
94                 if (pos >= size)
95                         return 0;
96                 if (pos + count > size)
97                         count = size - pos;
98         }
99
100         if (!battr->read)
101                 return -EIO;
102
103         return battr->read(of->file, kobj, battr, buf, pos, count);
104 }
105
106 /* kernfs read callback for regular sysfs files with pre-alloc */
107 static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
108                              size_t count, loff_t pos)
109 {
110         const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
111         struct kobject *kobj = of->kn->parent->priv;
112         ssize_t len;
113
114         /*
115          * If buf != of->prealloc_buf, we don't know how
116          * large it is, so cannot safely pass it to ->show
117          */
118         if (WARN_ON_ONCE(buf != of->prealloc_buf))
119                 return 0;
120         len = ops->show(kobj, of->kn->priv, buf);
121         if (len < 0)
122                 return len;
123         if (pos) {
124                 if (len <= pos)
125                         return 0;
126                 len -= pos;
127                 memmove(buf, buf + pos, len);
128         }
129         return min_t(ssize_t, count, len);
130 }
131
132 /* kernfs write callback for regular sysfs files */
133 static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
134                               size_t count, loff_t pos)
135 {
136         const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
137         struct kobject *kobj = of->kn->parent->priv;
138
139         if (!count)
140                 return 0;
141
142         return ops->store(kobj, of->kn->priv, buf, count);
143 }
144
145 /* kernfs write callback for bin sysfs files */
146 static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
147                                   size_t count, loff_t pos)
148 {
149         struct bin_attribute *battr = of->kn->priv;
150         struct kobject *kobj = of->kn->parent->priv;
151         loff_t size = file_inode(of->file)->i_size;
152
153         if (size) {
154                 if (size <= pos)
155                         return -EFBIG;
156                 count = min_t(ssize_t, count, size - pos);
157         }
158         if (!count)
159                 return 0;
160
161         if (!battr->write)
162                 return -EIO;
163
164         return battr->write(of->file, kobj, battr, buf, pos, count);
165 }
166
167 static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
168                              struct vm_area_struct *vma)
169 {
170         struct bin_attribute *battr = of->kn->priv;
171         struct kobject *kobj = of->kn->parent->priv;
172
173         return battr->mmap(of->file, kobj, battr, vma);
174 }
175
176 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
177 {
178         struct kernfs_node *kn = kobj->sd, *tmp;
179
180         if (kn && dir)
181                 kn = kernfs_find_and_get(kn, dir);
182         else
183                 kernfs_get(kn);
184
185         if (kn && attr) {
186                 tmp = kernfs_find_and_get(kn, attr);
187                 kernfs_put(kn);
188                 kn = tmp;
189         }
190
191         if (kn) {
192                 kernfs_notify(kn);
193                 kernfs_put(kn);
194         }
195 }
196 EXPORT_SYMBOL_GPL(sysfs_notify);
197
198 static const struct kernfs_ops sysfs_file_kfops_empty = {
199 };
200
201 static const struct kernfs_ops sysfs_file_kfops_ro = {
202         .seq_show       = sysfs_kf_seq_show,
203 };
204
205 static const struct kernfs_ops sysfs_file_kfops_wo = {
206         .write          = sysfs_kf_write,
207 };
208
209 static const struct kernfs_ops sysfs_file_kfops_rw = {
210         .seq_show       = sysfs_kf_seq_show,
211         .write          = sysfs_kf_write,
212 };
213
214 static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
215         .read           = sysfs_kf_read,
216         .prealloc       = true,
217 };
218
219 static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
220         .write          = sysfs_kf_write,
221         .prealloc       = true,
222 };
223
224 static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
225         .read           = sysfs_kf_read,
226         .write          = sysfs_kf_write,
227         .prealloc       = true,
228 };
229
230 static const struct kernfs_ops sysfs_bin_kfops_ro = {
231         .read           = sysfs_kf_bin_read,
232 };
233
234 static const struct kernfs_ops sysfs_bin_kfops_wo = {
235         .write          = sysfs_kf_bin_write,
236 };
237
238 static const struct kernfs_ops sysfs_bin_kfops_rw = {
239         .read           = sysfs_kf_bin_read,
240         .write          = sysfs_kf_bin_write,
241 };
242
243 static const struct kernfs_ops sysfs_bin_kfops_mmap = {
244         .read           = sysfs_kf_bin_read,
245         .write          = sysfs_kf_bin_write,
246         .mmap           = sysfs_kf_bin_mmap,
247 };
248
249 int sysfs_add_file_mode_ns(struct kernfs_node *parent,
250                            const struct attribute *attr, bool is_bin,
251                            umode_t mode, const void *ns)
252 {
253         struct lock_class_key *key = NULL;
254         const struct kernfs_ops *ops;
255         struct kernfs_node *kn;
256         loff_t size;
257
258         if (!is_bin) {
259                 struct kobject *kobj = parent->priv;
260                 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
261
262                 /* every kobject with an attribute needs a ktype assigned */
263                 if (WARN(!sysfs_ops, KERN_ERR
264                          "missing sysfs attribute operations for kobject: %s\n",
265                          kobject_name(kobj)))
266                         return -EINVAL;
267
268                 if (sysfs_ops->show && sysfs_ops->store) {
269                         if (mode & SYSFS_PREALLOC)
270                                 ops = &sysfs_prealloc_kfops_rw;
271                         else
272                                 ops = &sysfs_file_kfops_rw;
273                 } else if (sysfs_ops->show) {
274                         if (mode & SYSFS_PREALLOC)
275                                 ops = &sysfs_prealloc_kfops_ro;
276                         else
277                                 ops = &sysfs_file_kfops_ro;
278                 } else if (sysfs_ops->store) {
279                         if (mode & SYSFS_PREALLOC)
280                                 ops = &sysfs_prealloc_kfops_wo;
281                         else
282                                 ops = &sysfs_file_kfops_wo;
283                 } else
284                         ops = &sysfs_file_kfops_empty;
285
286                 size = PAGE_SIZE;
287         } else {
288                 struct bin_attribute *battr = (void *)attr;
289
290                 if (battr->mmap)
291                         ops = &sysfs_bin_kfops_mmap;
292                 else if (battr->read && battr->write)
293                         ops = &sysfs_bin_kfops_rw;
294                 else if (battr->read)
295                         ops = &sysfs_bin_kfops_ro;
296                 else if (battr->write)
297                         ops = &sysfs_bin_kfops_wo;
298                 else
299                         ops = &sysfs_file_kfops_empty;
300
301                 size = battr->size;
302         }
303
304 #ifdef CONFIG_DEBUG_LOCK_ALLOC
305         if (!attr->ignore_lockdep)
306                 key = attr->key ?: (struct lock_class_key *)&attr->skey;
307 #endif
308         kn = __kernfs_create_file(parent, attr->name, mode & 0777, size, ops,
309                                   (void *)attr, ns, key);
310         if (IS_ERR(kn)) {
311                 if (PTR_ERR(kn) == -EEXIST)
312                         sysfs_warn_dup(parent, attr->name);
313                 return PTR_ERR(kn);
314         }
315         return 0;
316 }
317
318 int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
319                    bool is_bin)
320 {
321         return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
322 }
323
324 /**
325  * sysfs_create_file_ns - create an attribute file for an object with custom ns
326  * @kobj: object we're creating for
327  * @attr: attribute descriptor
328  * @ns: namespace the new file should belong to
329  */
330 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
331                          const void *ns)
332 {
333         BUG_ON(!kobj || !kobj->sd || !attr);
334
335         return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
336
337 }
338 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
339
340 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
341 {
342         int err = 0;
343         int i;
344
345         for (i = 0; ptr[i] && !err; i++)
346                 err = sysfs_create_file(kobj, ptr[i]);
347         if (err)
348                 while (--i >= 0)
349                         sysfs_remove_file(kobj, ptr[i]);
350         return err;
351 }
352 EXPORT_SYMBOL_GPL(sysfs_create_files);
353
354 /**
355  * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
356  * @kobj: object we're acting for.
357  * @attr: attribute descriptor.
358  * @group: group name.
359  */
360 int sysfs_add_file_to_group(struct kobject *kobj,
361                 const struct attribute *attr, const char *group)
362 {
363         struct kernfs_node *parent;
364         int error;
365
366         if (group) {
367                 parent = kernfs_find_and_get(kobj->sd, group);
368         } else {
369                 parent = kobj->sd;
370                 kernfs_get(parent);
371         }
372
373         if (!parent)
374                 return -ENOENT;
375
376         error = sysfs_add_file(parent, attr, false);
377         kernfs_put(parent);
378
379         return error;
380 }
381 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
382
383 /**
384  * sysfs_chmod_file - update the modified mode value on an object attribute.
385  * @kobj: object we're acting for.
386  * @attr: attribute descriptor.
387  * @mode: file permissions.
388  *
389  */
390 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
391                      umode_t mode)
392 {
393         struct kernfs_node *kn;
394         struct iattr newattrs;
395         int rc;
396
397         kn = kernfs_find_and_get(kobj->sd, attr->name);
398         if (!kn)
399                 return -ENOENT;
400
401         newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
402         newattrs.ia_valid = ATTR_MODE;
403
404         rc = kernfs_setattr(kn, &newattrs);
405
406         kernfs_put(kn);
407         return rc;
408 }
409 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
410
411 /**
412  * sysfs_break_active_protection - break "active" protection
413  * @kobj: The kernel object @attr is associated with.
414  * @attr: The attribute to break the "active" protection for.
415  *
416  * With sysfs, just like kernfs, deletion of an attribute is postponed until
417  * all active .show() and .store() callbacks have finished unless this function
418  * is called. Hence this function is useful in methods that implement self
419  * deletion.
420  */
421 struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
422                                                   const struct attribute *attr)
423 {
424         struct kernfs_node *kn;
425
426         kobject_get(kobj);
427         kn = kernfs_find_and_get(kobj->sd, attr->name);
428         if (kn)
429                 kernfs_break_active_protection(kn);
430         return kn;
431 }
432 EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
433
434 /**
435  * sysfs_unbreak_active_protection - restore "active" protection
436  * @kn: Pointer returned by sysfs_break_active_protection().
437  *
438  * Undo the effects of sysfs_break_active_protection(). Since this function
439  * calls kernfs_put() on the kernfs node that corresponds to the 'attr'
440  * argument passed to sysfs_break_active_protection() that attribute may have
441  * been removed between the sysfs_break_active_protection() and
442  * sysfs_unbreak_active_protection() calls, it is not safe to access @kn after
443  * this function has returned.
444  */
445 void sysfs_unbreak_active_protection(struct kernfs_node *kn)
446 {
447         struct kobject *kobj = kn->parent->priv;
448
449         kernfs_unbreak_active_protection(kn);
450         kernfs_put(kn);
451         kobject_put(kobj);
452 }
453 EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
454
455 /**
456  * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
457  * @kobj: object we're acting for
458  * @attr: attribute descriptor
459  * @ns: namespace tag of the file to remove
460  *
461  * Hash the attribute name and namespace tag and kill the victim.
462  */
463 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
464                           const void *ns)
465 {
466         struct kernfs_node *parent = kobj->sd;
467
468         kernfs_remove_by_name_ns(parent, attr->name, ns);
469 }
470 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
471
472 /**
473  * sysfs_remove_file_self - remove an object attribute from its own method
474  * @kobj: object we're acting for
475  * @attr: attribute descriptor
476  *
477  * See kernfs_remove_self() for details.
478  */
479 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
480 {
481         struct kernfs_node *parent = kobj->sd;
482         struct kernfs_node *kn;
483         bool ret;
484
485         kn = kernfs_find_and_get(parent, attr->name);
486         if (WARN_ON_ONCE(!kn))
487                 return false;
488
489         ret = kernfs_remove_self(kn);
490
491         kernfs_put(kn);
492         return ret;
493 }
494
495 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
496 {
497         int i;
498         for (i = 0; ptr[i]; i++)
499                 sysfs_remove_file(kobj, ptr[i]);
500 }
501 EXPORT_SYMBOL_GPL(sysfs_remove_files);
502
503 /**
504  * sysfs_remove_file_from_group - remove an attribute file from a group.
505  * @kobj: object we're acting for.
506  * @attr: attribute descriptor.
507  * @group: group name.
508  */
509 void sysfs_remove_file_from_group(struct kobject *kobj,
510                 const struct attribute *attr, const char *group)
511 {
512         struct kernfs_node *parent;
513
514         if (group) {
515                 parent = kernfs_find_and_get(kobj->sd, group);
516         } else {
517                 parent = kobj->sd;
518                 kernfs_get(parent);
519         }
520
521         if (parent) {
522                 kernfs_remove_by_name(parent, attr->name);
523                 kernfs_put(parent);
524         }
525 }
526 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
527
528 /**
529  *      sysfs_create_bin_file - create binary file for object.
530  *      @kobj:  object.
531  *      @attr:  attribute descriptor.
532  */
533 int sysfs_create_bin_file(struct kobject *kobj,
534                           const struct bin_attribute *attr)
535 {
536         BUG_ON(!kobj || !kobj->sd || !attr);
537
538         return sysfs_add_file(kobj->sd, &attr->attr, true);
539 }
540 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
541
542 /**
543  *      sysfs_remove_bin_file - remove binary file for object.
544  *      @kobj:  object.
545  *      @attr:  attribute descriptor.
546  */
547 void sysfs_remove_bin_file(struct kobject *kobj,
548                            const struct bin_attribute *attr)
549 {
550         kernfs_remove_by_name(kobj->sd, attr->attr.name);
551 }
552 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
553
554 /**
555  *      sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer.
556  *      @buf:   start of PAGE_SIZE buffer.
557  *      @fmt:   format
558  *      @...:   optional arguments to @format
559  *
560  *
561  * Returns number of characters written to @buf.
562  */
563 int sysfs_emit(char *buf, const char *fmt, ...)
564 {
565         va_list args;
566         int len;
567
568         if (WARN(!buf || offset_in_page(buf),
569                  "invalid sysfs_emit: buf:%p\n", buf))
570                 return 0;
571
572         va_start(args, fmt);
573         len = vscnprintf(buf, PAGE_SIZE, fmt, args);
574         va_end(args);
575
576         return len;
577 }
578 EXPORT_SYMBOL_GPL(sysfs_emit);
579
580 /**
581  *      sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer.
582  *      @buf:   start of PAGE_SIZE buffer.
583  *      @at:    offset in @buf to start write in bytes
584  *              @at must be >= 0 && < PAGE_SIZE
585  *      @fmt:   format
586  *      @...:   optional arguments to @fmt
587  *
588  *
589  * Returns number of characters written starting at &@buf[@at].
590  */
591 int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
592 {
593         va_list args;
594         int len;
595
596         if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
597                  "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
598                 return 0;
599
600         va_start(args, fmt);
601         len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
602         va_end(args);
603
604         return len;
605 }
606 EXPORT_SYMBOL_GPL(sysfs_emit_at);