2 * fs/sysfs/file.c - sysfs regular (text) file implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
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>
23 #include "../kernfs/kernfs-internal.h"
26 * Determine ktype->sysfs_ops for the given kernfs_node. This function
27 * must be called while holding an active reference.
29 static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
31 struct kobject *kobj = kn->parent->priv;
33 if (kn->flags & KERNFS_LOCKDEP)
34 lockdep_assert_held(kn);
35 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
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.
43 static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
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);
51 /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
52 count = seq_get_buf(sf, &buf);
53 if (count < PAGE_SIZE) {
57 memset(buf, 0, PAGE_SIZE);
60 * Invoke show(). Control may reach here via seq file lseek even
61 * if @ops->show() isn't implemented.
64 count = ops->show(kobj, of->kn->priv, buf);
70 * The code works fine with PAGE_SIZE return but it's likely to
71 * indicate truncated result or overflow in normal use cases.
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;
79 seq_commit(sf, count);
83 static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
84 size_t count, loff_t pos)
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;
96 if (pos + count > size)
103 return battr->read(of->file, kobj, battr, buf, pos, count);
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)
110 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
111 struct kobject *kobj = of->kn->parent->priv;
115 * If buf != of->prealloc_buf, we don't know how
116 * large it is, so cannot safely pass it to ->show
118 if (WARN_ON_ONCE(buf != of->prealloc_buf))
120 len = ops->show(kobj, of->kn->priv, buf);
127 memmove(buf, buf + pos, len);
129 return min_t(ssize_t, count, len);
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)
136 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
137 struct kobject *kobj = of->kn->parent->priv;
142 return ops->store(kobj, of->kn->priv, buf, count);
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)
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;
156 count = min_t(ssize_t, count, size - pos);
164 return battr->write(of->file, kobj, battr, buf, pos, count);
167 static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
168 struct vm_area_struct *vma)
170 struct bin_attribute *battr = of->kn->priv;
171 struct kobject *kobj = of->kn->parent->priv;
173 return battr->mmap(of->file, kobj, battr, vma);
176 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
178 struct kernfs_node *kn = kobj->sd, *tmp;
181 kn = kernfs_find_and_get(kn, dir);
186 tmp = kernfs_find_and_get(kn, attr);
196 EXPORT_SYMBOL_GPL(sysfs_notify);
198 static const struct kernfs_ops sysfs_file_kfops_empty = {
201 static const struct kernfs_ops sysfs_file_kfops_ro = {
202 .seq_show = sysfs_kf_seq_show,
205 static const struct kernfs_ops sysfs_file_kfops_wo = {
206 .write = sysfs_kf_write,
209 static const struct kernfs_ops sysfs_file_kfops_rw = {
210 .seq_show = sysfs_kf_seq_show,
211 .write = sysfs_kf_write,
214 static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
215 .read = sysfs_kf_read,
219 static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
220 .write = sysfs_kf_write,
224 static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
225 .read = sysfs_kf_read,
226 .write = sysfs_kf_write,
230 static const struct kernfs_ops sysfs_bin_kfops_ro = {
231 .read = sysfs_kf_bin_read,
234 static const struct kernfs_ops sysfs_bin_kfops_wo = {
235 .write = sysfs_kf_bin_write,
238 static const struct kernfs_ops sysfs_bin_kfops_rw = {
239 .read = sysfs_kf_bin_read,
240 .write = sysfs_kf_bin_write,
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,
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)
253 struct lock_class_key *key = NULL;
254 const struct kernfs_ops *ops;
255 struct kernfs_node *kn;
259 struct kobject *kobj = parent->priv;
260 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
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",
268 if (sysfs_ops->show && sysfs_ops->store) {
269 if (mode & SYSFS_PREALLOC)
270 ops = &sysfs_prealloc_kfops_rw;
272 ops = &sysfs_file_kfops_rw;
273 } else if (sysfs_ops->show) {
274 if (mode & SYSFS_PREALLOC)
275 ops = &sysfs_prealloc_kfops_ro;
277 ops = &sysfs_file_kfops_ro;
278 } else if (sysfs_ops->store) {
279 if (mode & SYSFS_PREALLOC)
280 ops = &sysfs_prealloc_kfops_wo;
282 ops = &sysfs_file_kfops_wo;
284 ops = &sysfs_file_kfops_empty;
288 struct bin_attribute *battr = (void *)attr;
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;
299 ops = &sysfs_file_kfops_empty;
304 #ifdef CONFIG_DEBUG_LOCK_ALLOC
305 if (!attr->ignore_lockdep)
306 key = attr->key ?: (struct lock_class_key *)&attr->skey;
308 kn = __kernfs_create_file(parent, attr->name, mode & 0777, size, ops,
309 (void *)attr, ns, key);
311 if (PTR_ERR(kn) == -EEXIST)
312 sysfs_warn_dup(parent, attr->name);
318 int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
321 return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
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
330 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
333 BUG_ON(!kobj || !kobj->sd || !attr);
335 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
338 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
340 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
345 for (i = 0; ptr[i] && !err; i++)
346 err = sysfs_create_file(kobj, ptr[i]);
349 sysfs_remove_file(kobj, ptr[i]);
352 EXPORT_SYMBOL_GPL(sysfs_create_files);
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.
360 int sysfs_add_file_to_group(struct kobject *kobj,
361 const struct attribute *attr, const char *group)
363 struct kernfs_node *parent;
367 parent = kernfs_find_and_get(kobj->sd, group);
376 error = sysfs_add_file(parent, attr, false);
381 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
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.
390 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
393 struct kernfs_node *kn;
394 struct iattr newattrs;
397 kn = kernfs_find_and_get(kobj->sd, attr->name);
401 newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
402 newattrs.ia_valid = ATTR_MODE;
404 rc = kernfs_setattr(kn, &newattrs);
409 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
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.
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
421 struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
422 const struct attribute *attr)
424 struct kernfs_node *kn;
427 kn = kernfs_find_and_get(kobj->sd, attr->name);
429 kernfs_break_active_protection(kn);
432 EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
435 * sysfs_unbreak_active_protection - restore "active" protection
436 * @kn: Pointer returned by sysfs_break_active_protection().
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.
445 void sysfs_unbreak_active_protection(struct kernfs_node *kn)
447 struct kobject *kobj = kn->parent->priv;
449 kernfs_unbreak_active_protection(kn);
453 EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
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
461 * Hash the attribute name and namespace tag and kill the victim.
463 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
466 struct kernfs_node *parent = kobj->sd;
468 kernfs_remove_by_name_ns(parent, attr->name, ns);
470 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
473 * sysfs_remove_file_self - remove an object attribute from its own method
474 * @kobj: object we're acting for
475 * @attr: attribute descriptor
477 * See kernfs_remove_self() for details.
479 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
481 struct kernfs_node *parent = kobj->sd;
482 struct kernfs_node *kn;
485 kn = kernfs_find_and_get(parent, attr->name);
486 if (WARN_ON_ONCE(!kn))
489 ret = kernfs_remove_self(kn);
495 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
498 for (i = 0; ptr[i]; i++)
499 sysfs_remove_file(kobj, ptr[i]);
501 EXPORT_SYMBOL_GPL(sysfs_remove_files);
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.
509 void sysfs_remove_file_from_group(struct kobject *kobj,
510 const struct attribute *attr, const char *group)
512 struct kernfs_node *parent;
515 parent = kernfs_find_and_get(kobj->sd, group);
522 kernfs_remove_by_name(parent, attr->name);
526 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
529 * sysfs_create_bin_file - create binary file for object.
531 * @attr: attribute descriptor.
533 int sysfs_create_bin_file(struct kobject *kobj,
534 const struct bin_attribute *attr)
536 BUG_ON(!kobj || !kobj->sd || !attr);
538 return sysfs_add_file(kobj->sd, &attr->attr, true);
540 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
543 * sysfs_remove_bin_file - remove binary file for object.
545 * @attr: attribute descriptor.
547 void sysfs_remove_bin_file(struct kobject *kobj,
548 const struct bin_attribute *attr)
550 kernfs_remove_by_name(kobj->sd, attr->attr.name);
552 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
555 * sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer.
556 * @buf: start of PAGE_SIZE buffer.
558 * @...: optional arguments to @format
561 * Returns number of characters written to @buf.
563 int sysfs_emit(char *buf, const char *fmt, ...)
568 if (WARN(!buf, "invalid sysfs_emit: buf:%p\n", buf))
572 len = vscnprintf(buf, PAGE_SIZE, fmt, args);
577 EXPORT_SYMBOL_GPL(sysfs_emit);
580 * sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer.
581 * @buf: start of PAGE_SIZE buffer.
582 * @at: offset in @buf to start write in bytes
583 * @at must be >= 0 && < PAGE_SIZE
585 * @...: optional arguments to @fmt
588 * Returns number of characters written starting at &@buf[@at].
590 int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
595 if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
596 "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
600 len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
605 EXPORT_SYMBOL_GPL(sysfs_emit_at);