fs/sysfs/file.c
Source file repositories/reference/linux-study-clean/fs/sysfs/file.c
File Facts
- System
- Linux kernel
- Corpus path
fs/sysfs/file.c- Extension
.c- Size
- 20918 bytes
- Lines
- 822
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/module.hlinux/kobject.hlinux/slab.hlinux/list.hlinux/mutex.hlinux/seq_file.hlinux/mm.hsysfs.h
Detected Declarations
function Copyrightfunction sysfs_kf_seq_showfunction sysfs_kf_bin_readfunction sysfs_kf_readfunction sysfs_kf_writefunction sysfs_kf_bin_writefunction sysfs_kf_bin_mmapfunction sysfs_kf_bin_llseekfunction sysfs_kf_bin_openfunction sysfs_notifyfunction sysfs_add_file_mode_nsfunction sysfs_add_bin_file_mode_nsfunction sysfs_create_file_nsfunction sysfs_create_filesfunction sysfs_add_file_to_groupfunction sysfs_chmod_filefunction sysfs_break_active_protectionfunction sysfs_remove_file_nsfunction kernfs_remove_selffunction sysfs_remove_filesfunction sysfs_remove_file_from_groupfunction sysfs_create_bin_filefunction sysfs_remove_bin_filefunction internal_change_ownerfunction sysfs_link_change_ownerfunction sysfs_file_change_ownerfunction sysfs_change_ownerfunction sysfs_emitfunction sysfs_emit_atfunction sysfs_bin_attr_simple_readexport sysfs_notifyexport sysfs_create_file_nsexport sysfs_create_filesexport sysfs_add_file_to_groupexport sysfs_chmod_fileexport sysfs_break_active_protectionexport sysfs_unbreak_active_protectionexport sysfs_remove_file_nsexport sysfs_remove_file_selfexport sysfs_remove_filesexport sysfs_remove_file_from_groupexport sysfs_create_bin_fileexport sysfs_remove_bin_fileexport sysfs_emitexport sysfs_emit_atexport sysfs_bin_attr_simple_read
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* fs/sysfs/file.c - sysfs regular (text) file implementation
*
* Copyright (c) 2001-3 Patrick Mochel
* Copyright (c) 2007 SUSE Linux Products GmbH
* Copyright (c) 2007 Tejun Heo <teheo@suse.de>
*
* Please see Documentation/filesystems/sysfs.rst for more information.
*/
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/seq_file.h>
#include <linux/mm.h>
#include "sysfs.h"
static struct kobject *sysfs_file_kobj(struct kernfs_node *kn)
{
guard(rcu)();
return rcu_dereference(kn->__parent)->priv;
}
/*
* Determine ktype->sysfs_ops for the given kernfs_node. This function
* must be called while holding an active reference.
*/
static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
{
struct kobject *kobj = sysfs_file_kobj(kn);
if (kn->flags & KERNFS_LOCKDEP)
lockdep_assert_held(kn);
return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
}
/*
* Reads on sysfs are handled through seq_file, which takes care of hairy
* details like buffering and seeking. The following function pipes
* sysfs_ops->show() result through seq_file.
*/
static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
{
struct kernfs_open_file *of = sf->private;
struct kobject *kobj = sysfs_file_kobj(of->kn);
const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
ssize_t count;
char *buf;
if (WARN_ON_ONCE(!ops->show))
return -EINVAL;
/* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
count = seq_get_buf(sf, &buf);
if (count < PAGE_SIZE) {
seq_commit(sf, -1);
return 0;
}
memset(buf, 0, PAGE_SIZE);
count = ops->show(kobj, of->kn->priv, buf);
if (count < 0)
return count;
/*
* The code works fine with PAGE_SIZE return but it's likely to
* indicate truncated result or overflow in normal use cases.
*/
if (count >= PAGE_SIZE) {
WARN(1, "OOB write or bad count %zd at %pS\n", count, ops->show);
/* Try to struggle along */
count = PAGE_SIZE - 1;
}
seq_commit(sf, count);
return 0;
}
static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
size_t count, loff_t pos)
{
const struct bin_attribute *battr = of->kn->priv;
struct kobject *kobj = sysfs_file_kobj(of->kn);
loff_t size = file_inode(of->file)->i_size;
if (!count)
return 0;
Annotation
- Immediate include surface: `linux/module.h`, `linux/kobject.h`, `linux/slab.h`, `linux/list.h`, `linux/mutex.h`, `linux/seq_file.h`, `linux/mm.h`, `sysfs.h`.
- Detected declarations: `function Copyright`, `function sysfs_kf_seq_show`, `function sysfs_kf_bin_read`, `function sysfs_kf_read`, `function sysfs_kf_write`, `function sysfs_kf_bin_write`, `function sysfs_kf_bin_mmap`, `function sysfs_kf_bin_llseek`, `function sysfs_kf_bin_open`, `function sysfs_notify`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.