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
7 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
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 if (WARN_ON_ONCE(!ops->show))
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);
59 count = ops->show(kobj, of->kn->priv, buf);
64 * The code works fine with PAGE_SIZE return but it's likely to
65 * indicate truncated result or overflow in normal use cases.
67 if (count >= (ssize_t)PAGE_SIZE) {
68 printk("fill_read_buffer: %pS returned bad count\n",
70 /* Try to struggle along */
71 count = PAGE_SIZE - 1;
73 seq_commit(sf, count);
77 static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
78 size_t count, loff_t pos)
80 struct bin_attribute *battr = of->kn->priv;
81 struct kobject *kobj = of->kn->parent->priv;
82 loff_t size = file_inode(of->file)->i_size;
90 if (pos + count > size)
97 return battr->read(of->file, kobj, battr, buf, pos, count);
100 /* kernfs read callback for regular sysfs files with pre-alloc */
101 static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
102 size_t count, loff_t pos)
104 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
105 struct kobject *kobj = of->kn->parent->priv;
109 * If buf != of->prealloc_buf, we don't know how
110 * large it is, so cannot safely pass it to ->show
112 if (WARN_ON_ONCE(buf != of->prealloc_buf))
114 len = ops->show(kobj, of->kn->priv, buf);
121 memmove(buf, buf + pos, len);
123 return min_t(ssize_t, count, len);
126 /* kernfs write callback for regular sysfs files */
127 static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
128 size_t count, loff_t pos)
130 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
131 struct kobject *kobj = of->kn->parent->priv;
136 return ops->store(kobj, of->kn->priv, buf, count);
139 /* kernfs write callback for bin sysfs files */
140 static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
141 size_t count, loff_t pos)
143 struct bin_attribute *battr = of->kn->priv;
144 struct kobject *kobj = of->kn->parent->priv;
145 loff_t size = file_inode(of->file)->i_size;
150 count = min_t(ssize_t, count, size - pos);
158 return battr->write(of->file, kobj, battr, buf, pos, count);
161 static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
162 struct vm_area_struct *vma)
164 struct bin_attribute *battr = of->kn->priv;
165 struct kobject *kobj = of->kn->parent->priv;
167 return battr->mmap(of->file, kobj, battr, vma);
170 static int sysfs_kf_bin_open(struct kernfs_open_file *of)
172 struct bin_attribute *battr = of->kn->priv;
174 if (battr->f_mapping)
175 of->file->f_mapping = battr->f_mapping();
180 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
182 struct kernfs_node *kn = kobj->sd, *tmp;
185 kn = kernfs_find_and_get(kn, dir);
190 tmp = kernfs_find_and_get(kn, attr);
200 EXPORT_SYMBOL_GPL(sysfs_notify);
202 static const struct kernfs_ops sysfs_file_kfops_empty = {
205 static const struct kernfs_ops sysfs_file_kfops_ro = {
206 .seq_show = sysfs_kf_seq_show,
209 static const struct kernfs_ops sysfs_file_kfops_wo = {
210 .write = sysfs_kf_write,
213 static const struct kernfs_ops sysfs_file_kfops_rw = {
214 .seq_show = sysfs_kf_seq_show,
215 .write = sysfs_kf_write,
218 static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
219 .read = sysfs_kf_read,
223 static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
224 .write = sysfs_kf_write,
228 static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
229 .read = sysfs_kf_read,
230 .write = sysfs_kf_write,
234 static const struct kernfs_ops sysfs_bin_kfops_ro = {
235 .read = sysfs_kf_bin_read,
238 static const struct kernfs_ops sysfs_bin_kfops_wo = {
239 .write = sysfs_kf_bin_write,
242 static const struct kernfs_ops sysfs_bin_kfops_rw = {
243 .read = sysfs_kf_bin_read,
244 .write = sysfs_kf_bin_write,
247 static const struct kernfs_ops sysfs_bin_kfops_mmap = {
248 .read = sysfs_kf_bin_read,
249 .write = sysfs_kf_bin_write,
250 .mmap = sysfs_kf_bin_mmap,
251 .open = sysfs_kf_bin_open,
254 int sysfs_add_file_mode_ns(struct kernfs_node *parent,
255 const struct attribute *attr, umode_t mode, kuid_t uid,
256 kgid_t gid, const void *ns)
258 struct kobject *kobj = parent->priv;
259 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
260 struct lock_class_key *key = NULL;
261 const struct kernfs_ops *ops = NULL;
262 struct kernfs_node *kn;
264 /* every kobject with an attribute needs a ktype assigned */
265 if (WARN(!sysfs_ops, KERN_ERR
266 "missing sysfs attribute operations for kobject: %s\n",
270 if (mode & SYSFS_PREALLOC) {
271 if (sysfs_ops->show && sysfs_ops->store)
272 ops = &sysfs_prealloc_kfops_rw;
273 else if (sysfs_ops->show)
274 ops = &sysfs_prealloc_kfops_ro;
275 else if (sysfs_ops->store)
276 ops = &sysfs_prealloc_kfops_wo;
278 if (sysfs_ops->show && sysfs_ops->store)
279 ops = &sysfs_file_kfops_rw;
280 else if (sysfs_ops->show)
281 ops = &sysfs_file_kfops_ro;
282 else if (sysfs_ops->store)
283 ops = &sysfs_file_kfops_wo;
287 ops = &sysfs_file_kfops_empty;
289 #ifdef CONFIG_DEBUG_LOCK_ALLOC
290 if (!attr->ignore_lockdep)
291 key = attr->key ?: (struct lock_class_key *)&attr->skey;
294 kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
295 PAGE_SIZE, ops, (void *)attr, ns, key);
297 if (PTR_ERR(kn) == -EEXIST)
298 sysfs_warn_dup(parent, attr->name);
304 int sysfs_add_bin_file_mode_ns(struct kernfs_node *parent,
305 const struct bin_attribute *battr, umode_t mode,
306 kuid_t uid, kgid_t gid, const void *ns)
308 const struct attribute *attr = &battr->attr;
309 struct lock_class_key *key = NULL;
310 const struct kernfs_ops *ops;
311 struct kernfs_node *kn;
314 ops = &sysfs_bin_kfops_mmap;
315 else if (battr->read && battr->write)
316 ops = &sysfs_bin_kfops_rw;
317 else if (battr->read)
318 ops = &sysfs_bin_kfops_ro;
319 else if (battr->write)
320 ops = &sysfs_bin_kfops_wo;
322 ops = &sysfs_file_kfops_empty;
324 #ifdef CONFIG_DEBUG_LOCK_ALLOC
325 if (!attr->ignore_lockdep)
326 key = attr->key ?: (struct lock_class_key *)&attr->skey;
329 kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
330 battr->size, ops, (void *)attr, ns, key);
332 if (PTR_ERR(kn) == -EEXIST)
333 sysfs_warn_dup(parent, attr->name);
340 * sysfs_create_file_ns - create an attribute file for an object with custom ns
341 * @kobj: object we're creating for
342 * @attr: attribute descriptor
343 * @ns: namespace the new file should belong to
345 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
351 if (WARN_ON(!kobj || !kobj->sd || !attr))
354 kobject_get_ownership(kobj, &uid, &gid);
355 return sysfs_add_file_mode_ns(kobj->sd, attr, attr->mode, uid, gid, ns);
357 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
359 int sysfs_create_files(struct kobject *kobj, const struct attribute * const *ptr)
364 for (i = 0; ptr[i] && !err; i++)
365 err = sysfs_create_file(kobj, ptr[i]);
368 sysfs_remove_file(kobj, ptr[i]);
371 EXPORT_SYMBOL_GPL(sysfs_create_files);
374 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
375 * @kobj: object we're acting for.
376 * @attr: attribute descriptor.
377 * @group: group name.
379 int sysfs_add_file_to_group(struct kobject *kobj,
380 const struct attribute *attr, const char *group)
382 struct kernfs_node *parent;
388 parent = kernfs_find_and_get(kobj->sd, group);
397 kobject_get_ownership(kobj, &uid, &gid);
398 error = sysfs_add_file_mode_ns(parent, attr, attr->mode, uid, gid,
404 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
407 * sysfs_chmod_file - update the modified mode value on an object attribute.
408 * @kobj: object we're acting for.
409 * @attr: attribute descriptor.
410 * @mode: file permissions.
413 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
416 struct kernfs_node *kn;
417 struct iattr newattrs;
420 kn = kernfs_find_and_get(kobj->sd, attr->name);
424 newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
425 newattrs.ia_valid = ATTR_MODE;
427 rc = kernfs_setattr(kn, &newattrs);
432 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
435 * sysfs_break_active_protection - break "active" protection
436 * @kobj: The kernel object @attr is associated with.
437 * @attr: The attribute to break the "active" protection for.
439 * With sysfs, just like kernfs, deletion of an attribute is postponed until
440 * all active .show() and .store() callbacks have finished unless this function
441 * is called. Hence this function is useful in methods that implement self
444 struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
445 const struct attribute *attr)
447 struct kernfs_node *kn;
450 kn = kernfs_find_and_get(kobj->sd, attr->name);
452 kernfs_break_active_protection(kn);
455 EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
458 * sysfs_unbreak_active_protection - restore "active" protection
459 * @kn: Pointer returned by sysfs_break_active_protection().
461 * Undo the effects of sysfs_break_active_protection(). Since this function
462 * calls kernfs_put() on the kernfs node that corresponds to the 'attr'
463 * argument passed to sysfs_break_active_protection() that attribute may have
464 * been removed between the sysfs_break_active_protection() and
465 * sysfs_unbreak_active_protection() calls, it is not safe to access @kn after
466 * this function has returned.
468 void sysfs_unbreak_active_protection(struct kernfs_node *kn)
470 struct kobject *kobj = kn->parent->priv;
472 kernfs_unbreak_active_protection(kn);
476 EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
479 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
480 * @kobj: object we're acting for
481 * @attr: attribute descriptor
482 * @ns: namespace tag of the file to remove
484 * Hash the attribute name and namespace tag and kill the victim.
486 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
489 struct kernfs_node *parent = kobj->sd;
491 kernfs_remove_by_name_ns(parent, attr->name, ns);
493 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
496 * sysfs_remove_file_self - remove an object attribute from its own method
497 * @kobj: object we're acting for
498 * @attr: attribute descriptor
500 * See kernfs_remove_self() for details.
502 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
504 struct kernfs_node *parent = kobj->sd;
505 struct kernfs_node *kn;
508 kn = kernfs_find_and_get(parent, attr->name);
509 if (WARN_ON_ONCE(!kn))
512 ret = kernfs_remove_self(kn);
517 EXPORT_SYMBOL_GPL(sysfs_remove_file_self);
519 void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *ptr)
523 for (i = 0; ptr[i]; i++)
524 sysfs_remove_file(kobj, ptr[i]);
526 EXPORT_SYMBOL_GPL(sysfs_remove_files);
529 * sysfs_remove_file_from_group - remove an attribute file from a group.
530 * @kobj: object we're acting for.
531 * @attr: attribute descriptor.
532 * @group: group name.
534 void sysfs_remove_file_from_group(struct kobject *kobj,
535 const struct attribute *attr, const char *group)
537 struct kernfs_node *parent;
540 parent = kernfs_find_and_get(kobj->sd, group);
547 kernfs_remove_by_name(parent, attr->name);
551 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
554 * sysfs_create_bin_file - create binary file for object.
556 * @attr: attribute descriptor.
558 int sysfs_create_bin_file(struct kobject *kobj,
559 const struct bin_attribute *attr)
564 if (WARN_ON(!kobj || !kobj->sd || !attr))
567 kobject_get_ownership(kobj, &uid, &gid);
568 return sysfs_add_bin_file_mode_ns(kobj->sd, attr, attr->attr.mode, uid,
571 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
574 * sysfs_remove_bin_file - remove binary file for object.
576 * @attr: attribute descriptor.
578 void sysfs_remove_bin_file(struct kobject *kobj,
579 const struct bin_attribute *attr)
581 kernfs_remove_by_name(kobj->sd, attr->attr.name);
583 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
585 static int internal_change_owner(struct kernfs_node *kn, kuid_t kuid,
588 struct iattr newattrs = {
589 .ia_valid = ATTR_UID | ATTR_GID,
593 return kernfs_setattr(kn, &newattrs);
597 * sysfs_link_change_owner - change owner of a sysfs file.
598 * @kobj: object of the kernfs_node the symlink is located in.
599 * @targ: object of the kernfs_node the symlink points to.
600 * @name: name of the link.
601 * @kuid: new owner's kuid
602 * @kgid: new owner's kgid
604 * This function looks up the sysfs symlink entry @name under @kobj and changes
605 * the ownership to @kuid/@kgid. The symlink is looked up in the namespace of
608 * Returns 0 on success or error code on failure.
610 int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ,
611 const char *name, kuid_t kuid, kgid_t kgid)
613 struct kernfs_node *kn = NULL;
616 if (!name || !kobj->state_in_sysfs || !targ->state_in_sysfs)
620 kn = kernfs_find_and_get_ns(kobj->sd, name, targ->sd->ns);
625 if (kernfs_type(kn) != KERNFS_LINK)
627 if (kn->symlink.target_kn->priv != targ)
630 error = internal_change_owner(kn, kuid, kgid);
638 * sysfs_file_change_owner - change owner of a sysfs file.
640 * @name: name of the file to change.
641 * @kuid: new owner's kuid
642 * @kgid: new owner's kgid
644 * This function looks up the sysfs entry @name under @kobj and changes the
645 * ownership to @kuid/@kgid.
647 * Returns 0 on success or error code on failure.
649 int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid,
652 struct kernfs_node *kn;
658 if (!kobj->state_in_sysfs)
661 kn = kernfs_find_and_get(kobj->sd, name);
665 error = internal_change_owner(kn, kuid, kgid);
671 EXPORT_SYMBOL_GPL(sysfs_file_change_owner);
674 * sysfs_change_owner - change owner of the given object.
676 * @kuid: new owner's kuid
677 * @kgid: new owner's kgid
679 * Change the owner of the default directory, files, groups, and attributes of
680 * @kobj to @kuid/@kgid. Note that sysfs_change_owner mirrors how the sysfs
681 * entries for a kobject are added by driver core. In summary,
682 * sysfs_change_owner() takes care of the default directory entry for @kobj,
683 * the default attributes associated with the ktype of @kobj and the default
684 * attributes associated with the ktype of @kobj.
685 * Additional properties not added by driver core have to be changed by the
686 * driver or subsystem which created them. This is similar to how
687 * driver/subsystem specific entries are removed.
689 * Returns 0 on success or error code on failure.
691 int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid)
694 const struct kobj_type *ktype;
696 if (!kobj->state_in_sysfs)
699 /* Change the owner of the kobject itself. */
700 error = internal_change_owner(kobj->sd, kuid, kgid);
704 ktype = get_ktype(kobj);
707 * Change owner of the default groups associated with the
710 error = sysfs_groups_change_owner(kobj, ktype->default_groups,
718 EXPORT_SYMBOL_GPL(sysfs_change_owner);
721 * sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer.
722 * @buf: start of PAGE_SIZE buffer.
724 * @...: optional arguments to @format
727 * Returns number of characters written to @buf.
729 int sysfs_emit(char *buf, const char *fmt, ...)
734 if (WARN(!buf || offset_in_page(buf),
735 "invalid sysfs_emit: buf:%p\n", buf))
739 len = vscnprintf(buf, PAGE_SIZE, fmt, args);
744 EXPORT_SYMBOL_GPL(sysfs_emit);
747 * sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer.
748 * @buf: start of PAGE_SIZE buffer.
749 * @at: offset in @buf to start write in bytes
750 * @at must be >= 0 && < PAGE_SIZE
752 * @...: optional arguments to @fmt
755 * Returns number of characters written starting at &@buf[@at].
757 int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
762 if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
763 "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
767 len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
772 EXPORT_SYMBOL_GPL(sysfs_emit_at);