1 // SPDX-License-Identifier: GPL-2.0
3 * fs/sysfs/file.c - sysfs regular (text) file implementation
5 * Copyright (c) 2001-3 Patrick Mochel
6 * Copyright (c) 2007 SUSE Linux Products GmbH
9 * Please see Documentation/filesystems/sysfs.rst for more information.
12 #include <linux/module.h>
13 #include <linux/kobject.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/mutex.h>
17 #include <linux/seq_file.h>
23 * Determine ktype->sysfs_ops for the given kernfs_node. This function
24 * must be called while holding an active reference.
26 static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
28 struct kobject *kobj = kn->parent->priv;
30 if (kn->flags & KERNFS_LOCKDEP)
31 lockdep_assert_held(kn);
32 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
36 * Reads on sysfs are handled through seq_file, which takes care of hairy
37 * details like buffering and seeking. The following function pipes
38 * sysfs_ops->show() result through seq_file.
40 static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
42 struct kernfs_open_file *of = sf->private;
43 struct kobject *kobj = of->kn->parent->priv;
44 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
48 /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
49 count = seq_get_buf(sf, &buf);
50 if (count < PAGE_SIZE) {
54 memset(buf, 0, PAGE_SIZE);
57 * Invoke show(). Control may reach here via seq file lseek even
58 * if @ops->show() isn't implemented.
61 count = ops->show(kobj, of->kn->priv, buf);
67 * The code works fine with PAGE_SIZE return but it's likely to
68 * indicate truncated result or overflow in normal use cases.
70 if (count >= (ssize_t)PAGE_SIZE) {
71 printk("fill_read_buffer: %pS returned bad count\n",
73 /* Try to struggle along */
74 count = PAGE_SIZE - 1;
76 seq_commit(sf, count);
80 static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
81 size_t count, loff_t pos)
83 struct bin_attribute *battr = of->kn->priv;
84 struct kobject *kobj = of->kn->parent->priv;
85 loff_t size = file_inode(of->file)->i_size;
93 if (pos + count > size)
100 return battr->read(of->file, kobj, battr, buf, pos, count);
103 /* kernfs read callback for regular sysfs files with pre-alloc */
104 static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
105 size_t count, loff_t pos)
107 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
108 struct kobject *kobj = of->kn->parent->priv;
112 * If buf != of->prealloc_buf, we don't know how
113 * large it is, so cannot safely pass it to ->show
115 if (WARN_ON_ONCE(buf != of->prealloc_buf))
117 len = ops->show(kobj, of->kn->priv, buf);
124 memmove(buf, buf + pos, len);
126 return min_t(ssize_t, count, len);
129 /* kernfs write callback for regular sysfs files */
130 static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
131 size_t count, loff_t pos)
133 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
134 struct kobject *kobj = of->kn->parent->priv;
139 return ops->store(kobj, of->kn->priv, buf, count);
142 /* kernfs write callback for bin sysfs files */
143 static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
144 size_t count, loff_t pos)
146 struct bin_attribute *battr = of->kn->priv;
147 struct kobject *kobj = of->kn->parent->priv;
148 loff_t size = file_inode(of->file)->i_size;
153 count = min_t(ssize_t, count, size - pos);
161 return battr->write(of->file, kobj, battr, buf, pos, count);
164 static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
165 struct vm_area_struct *vma)
167 struct bin_attribute *battr = of->kn->priv;
168 struct kobject *kobj = of->kn->parent->priv;
170 return battr->mmap(of->file, kobj, battr, vma);
173 static int sysfs_kf_bin_open(struct kernfs_open_file *of)
175 struct bin_attribute *battr = of->kn->priv;
178 of->file->f_mapping = battr->mapping;
183 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
185 struct kernfs_node *kn = kobj->sd, *tmp;
188 kn = kernfs_find_and_get(kn, dir);
193 tmp = kernfs_find_and_get(kn, attr);
203 EXPORT_SYMBOL_GPL(sysfs_notify);
205 static const struct kernfs_ops sysfs_file_kfops_empty = {
208 static const struct kernfs_ops sysfs_file_kfops_ro = {
209 .seq_show = sysfs_kf_seq_show,
212 static const struct kernfs_ops sysfs_file_kfops_wo = {
213 .write = sysfs_kf_write,
216 static const struct kernfs_ops sysfs_file_kfops_rw = {
217 .seq_show = sysfs_kf_seq_show,
218 .write = sysfs_kf_write,
221 static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
222 .read = sysfs_kf_read,
226 static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
227 .write = sysfs_kf_write,
231 static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
232 .read = sysfs_kf_read,
233 .write = sysfs_kf_write,
237 static const struct kernfs_ops sysfs_bin_kfops_ro = {
238 .read = sysfs_kf_bin_read,
241 static const struct kernfs_ops sysfs_bin_kfops_wo = {
242 .write = sysfs_kf_bin_write,
245 static const struct kernfs_ops sysfs_bin_kfops_rw = {
246 .read = sysfs_kf_bin_read,
247 .write = sysfs_kf_bin_write,
250 static const struct kernfs_ops sysfs_bin_kfops_mmap = {
251 .read = sysfs_kf_bin_read,
252 .write = sysfs_kf_bin_write,
253 .mmap = sysfs_kf_bin_mmap,
254 .open = sysfs_kf_bin_open,
257 int sysfs_add_file_mode_ns(struct kernfs_node *parent,
258 const struct attribute *attr, bool is_bin,
259 umode_t mode, kuid_t uid, kgid_t gid, const void *ns)
261 struct lock_class_key *key = NULL;
262 const struct kernfs_ops *ops;
263 struct kernfs_node *kn;
267 struct kobject *kobj = parent->priv;
268 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
270 /* every kobject with an attribute needs a ktype assigned */
271 if (WARN(!sysfs_ops, KERN_ERR
272 "missing sysfs attribute operations for kobject: %s\n",
276 if (sysfs_ops->show && sysfs_ops->store) {
277 if (mode & SYSFS_PREALLOC)
278 ops = &sysfs_prealloc_kfops_rw;
280 ops = &sysfs_file_kfops_rw;
281 } else if (sysfs_ops->show) {
282 if (mode & SYSFS_PREALLOC)
283 ops = &sysfs_prealloc_kfops_ro;
285 ops = &sysfs_file_kfops_ro;
286 } else if (sysfs_ops->store) {
287 if (mode & SYSFS_PREALLOC)
288 ops = &sysfs_prealloc_kfops_wo;
290 ops = &sysfs_file_kfops_wo;
292 ops = &sysfs_file_kfops_empty;
296 struct bin_attribute *battr = (void *)attr;
299 ops = &sysfs_bin_kfops_mmap;
300 else if (battr->read && battr->write)
301 ops = &sysfs_bin_kfops_rw;
302 else if (battr->read)
303 ops = &sysfs_bin_kfops_ro;
304 else if (battr->write)
305 ops = &sysfs_bin_kfops_wo;
307 ops = &sysfs_file_kfops_empty;
312 #ifdef CONFIG_DEBUG_LOCK_ALLOC
313 if (!attr->ignore_lockdep)
314 key = attr->key ?: (struct lock_class_key *)&attr->skey;
317 kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
318 size, ops, (void *)attr, ns, key);
320 if (PTR_ERR(kn) == -EEXIST)
321 sysfs_warn_dup(parent, attr->name);
328 * sysfs_create_file_ns - create an attribute file for an object with custom ns
329 * @kobj: object we're creating for
330 * @attr: attribute descriptor
331 * @ns: namespace the new file should belong to
333 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
339 if (WARN_ON(!kobj || !kobj->sd || !attr))
342 kobject_get_ownership(kobj, &uid, &gid);
343 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode,
347 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
349 int sysfs_create_files(struct kobject *kobj, const struct attribute * const *ptr)
354 for (i = 0; ptr[i] && !err; i++)
355 err = sysfs_create_file(kobj, ptr[i]);
358 sysfs_remove_file(kobj, ptr[i]);
361 EXPORT_SYMBOL_GPL(sysfs_create_files);
364 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
365 * @kobj: object we're acting for.
366 * @attr: attribute descriptor.
367 * @group: group name.
369 int sysfs_add_file_to_group(struct kobject *kobj,
370 const struct attribute *attr, const char *group)
372 struct kernfs_node *parent;
378 parent = kernfs_find_and_get(kobj->sd, group);
387 kobject_get_ownership(kobj, &uid, &gid);
388 error = sysfs_add_file_mode_ns(parent, attr, false,
389 attr->mode, uid, gid, NULL);
394 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
397 * sysfs_chmod_file - update the modified mode value on an object attribute.
398 * @kobj: object we're acting for.
399 * @attr: attribute descriptor.
400 * @mode: file permissions.
403 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
406 struct kernfs_node *kn;
407 struct iattr newattrs;
410 kn = kernfs_find_and_get(kobj->sd, attr->name);
414 newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
415 newattrs.ia_valid = ATTR_MODE;
417 rc = kernfs_setattr(kn, &newattrs);
422 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
425 * sysfs_break_active_protection - break "active" protection
426 * @kobj: The kernel object @attr is associated with.
427 * @attr: The attribute to break the "active" protection for.
429 * With sysfs, just like kernfs, deletion of an attribute is postponed until
430 * all active .show() and .store() callbacks have finished unless this function
431 * is called. Hence this function is useful in methods that implement self
434 struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
435 const struct attribute *attr)
437 struct kernfs_node *kn;
440 kn = kernfs_find_and_get(kobj->sd, attr->name);
442 kernfs_break_active_protection(kn);
445 EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
448 * sysfs_unbreak_active_protection - restore "active" protection
449 * @kn: Pointer returned by sysfs_break_active_protection().
451 * Undo the effects of sysfs_break_active_protection(). Since this function
452 * calls kernfs_put() on the kernfs node that corresponds to the 'attr'
453 * argument passed to sysfs_break_active_protection() that attribute may have
454 * been removed between the sysfs_break_active_protection() and
455 * sysfs_unbreak_active_protection() calls, it is not safe to access @kn after
456 * this function has returned.
458 void sysfs_unbreak_active_protection(struct kernfs_node *kn)
460 struct kobject *kobj = kn->parent->priv;
462 kernfs_unbreak_active_protection(kn);
466 EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
469 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
470 * @kobj: object we're acting for
471 * @attr: attribute descriptor
472 * @ns: namespace tag of the file to remove
474 * Hash the attribute name and namespace tag and kill the victim.
476 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
479 struct kernfs_node *parent = kobj->sd;
481 kernfs_remove_by_name_ns(parent, attr->name, ns);
483 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
486 * sysfs_remove_file_self - remove an object attribute from its own method
487 * @kobj: object we're acting for
488 * @attr: attribute descriptor
490 * See kernfs_remove_self() for details.
492 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
494 struct kernfs_node *parent = kobj->sd;
495 struct kernfs_node *kn;
498 kn = kernfs_find_and_get(parent, attr->name);
499 if (WARN_ON_ONCE(!kn))
502 ret = kernfs_remove_self(kn);
507 EXPORT_SYMBOL_GPL(sysfs_remove_file_self);
509 void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *ptr)
513 for (i = 0; ptr[i]; i++)
514 sysfs_remove_file(kobj, ptr[i]);
516 EXPORT_SYMBOL_GPL(sysfs_remove_files);
519 * sysfs_remove_file_from_group - remove an attribute file from a group.
520 * @kobj: object we're acting for.
521 * @attr: attribute descriptor.
522 * @group: group name.
524 void sysfs_remove_file_from_group(struct kobject *kobj,
525 const struct attribute *attr, const char *group)
527 struct kernfs_node *parent;
530 parent = kernfs_find_and_get(kobj->sd, group);
537 kernfs_remove_by_name(parent, attr->name);
541 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
544 * sysfs_create_bin_file - create binary file for object.
546 * @attr: attribute descriptor.
548 int sysfs_create_bin_file(struct kobject *kobj,
549 const struct bin_attribute *attr)
554 if (WARN_ON(!kobj || !kobj->sd || !attr))
557 kobject_get_ownership(kobj, &uid, &gid);
558 return sysfs_add_file_mode_ns(kobj->sd, &attr->attr, true,
559 attr->attr.mode, uid, gid, NULL);
561 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
564 * sysfs_remove_bin_file - remove binary file for object.
566 * @attr: attribute descriptor.
568 void sysfs_remove_bin_file(struct kobject *kobj,
569 const struct bin_attribute *attr)
571 kernfs_remove_by_name(kobj->sd, attr->attr.name);
573 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
575 static int internal_change_owner(struct kernfs_node *kn, kuid_t kuid,
578 struct iattr newattrs = {
579 .ia_valid = ATTR_UID | ATTR_GID,
583 return kernfs_setattr(kn, &newattrs);
587 * sysfs_link_change_owner - change owner of a sysfs file.
588 * @kobj: object of the kernfs_node the symlink is located in.
589 * @targ: object of the kernfs_node the symlink points to.
590 * @name: name of the link.
591 * @kuid: new owner's kuid
592 * @kgid: new owner's kgid
594 * This function looks up the sysfs symlink entry @name under @kobj and changes
595 * the ownership to @kuid/@kgid. The symlink is looked up in the namespace of
598 * Returns 0 on success or error code on failure.
600 int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ,
601 const char *name, kuid_t kuid, kgid_t kgid)
603 struct kernfs_node *kn = NULL;
606 if (!name || !kobj->state_in_sysfs || !targ->state_in_sysfs)
610 kn = kernfs_find_and_get_ns(kobj->sd, name, targ->sd->ns);
615 if (kernfs_type(kn) != KERNFS_LINK)
617 if (kn->symlink.target_kn->priv != targ)
620 error = internal_change_owner(kn, kuid, kgid);
628 * sysfs_file_change_owner - change owner of a sysfs file.
630 * @name: name of the file to change.
631 * @kuid: new owner's kuid
632 * @kgid: new owner's kgid
634 * This function looks up the sysfs entry @name under @kobj and changes the
635 * ownership to @kuid/@kgid.
637 * Returns 0 on success or error code on failure.
639 int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid,
642 struct kernfs_node *kn;
648 if (!kobj->state_in_sysfs)
651 kn = kernfs_find_and_get(kobj->sd, name);
655 error = internal_change_owner(kn, kuid, kgid);
661 EXPORT_SYMBOL_GPL(sysfs_file_change_owner);
664 * sysfs_change_owner - change owner of the given object.
666 * @kuid: new owner's kuid
667 * @kgid: new owner's kgid
669 * Change the owner of the default directory, files, groups, and attributes of
670 * @kobj to @kuid/@kgid. Note that sysfs_change_owner mirrors how the sysfs
671 * entries for a kobject are added by driver core. In summary,
672 * sysfs_change_owner() takes care of the default directory entry for @kobj,
673 * the default attributes associated with the ktype of @kobj and the default
674 * attributes associated with the ktype of @kobj.
675 * Additional properties not added by driver core have to be changed by the
676 * driver or subsystem which created them. This is similar to how
677 * driver/subsystem specific entries are removed.
679 * Returns 0 on success or error code on failure.
681 int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid)
684 const struct kobj_type *ktype;
686 if (!kobj->state_in_sysfs)
689 /* Change the owner of the kobject itself. */
690 error = internal_change_owner(kobj->sd, kuid, kgid);
694 ktype = get_ktype(kobj);
696 struct attribute **kattr;
699 * Change owner of the default attributes associated with the
702 for (kattr = ktype->default_attrs; kattr && *kattr; kattr++) {
703 error = sysfs_file_change_owner(kobj, (*kattr)->name,
710 * Change owner of the default groups associated with the
713 error = sysfs_groups_change_owner(kobj, ktype->default_groups,
721 EXPORT_SYMBOL_GPL(sysfs_change_owner);
724 * sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer.
725 * @buf: start of PAGE_SIZE buffer.
727 * @...: optional arguments to @format
730 * Returns number of characters written to @buf.
732 int sysfs_emit(char *buf, const char *fmt, ...)
737 if (WARN(!buf || offset_in_page(buf),
738 "invalid sysfs_emit: buf:%p\n", buf))
742 len = vscnprintf(buf, PAGE_SIZE, fmt, args);
747 EXPORT_SYMBOL_GPL(sysfs_emit);
750 * sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer.
751 * @buf: start of PAGE_SIZE buffer.
752 * @at: offset in @buf to start write in bytes
753 * @at must be >= 0 && < PAGE_SIZE
755 * @...: optional arguments to @fmt
758 * Returns number of characters written starting at &@buf[@at].
760 int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
765 if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
766 "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
770 len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
775 EXPORT_SYMBOL_GPL(sysfs_emit_at);