fs/libfs.c
Source file repositories/reference/linux-study-clean/fs/libfs.c
File Facts
- System
- Linux kernel
- Corpus path
fs/libfs.c- Extension
.c- Size
- 62674 bytes
- Lines
- 2315
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: operation-table or driver-model contract
- Status
- pattern 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.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/blkdev.hlinux/export.hlinux/filelock.hlinux/pagemap.hlinux/slab.hlinux/cred.hlinux/mount.hlinux/vfs.hlinux/quotaops.hlinux/mutex.hlinux/namei.hlinux/exportfs.hlinux/iversion.hlinux/writeback.hlinux/fs_context.hlinux/pseudo_fs.hlinux/fsnotify.hlinux/unicode.hlinux/fscrypt.hlinux/pidfs.hlinux/uaccess.hinternal.h
Detected Declarations
struct simple_attrfunction simple_getattrfunction simple_statfsfunction always_delete_dentryfunction dcache_dir_openfunction dcache_dir_closefunction dcache_dir_lseekfunction dcache_readdirfunction generic_read_dirfunction offset_setfunction dentry2offsetfunction simple_offset_initfunction simple_offset_addfunction simple_offset_replacefunction simple_offset_removefunction simple_offset_renamefunction simple_offset_rename_exchangefunction teardownfunction offset_dir_llseekfunction offset_dir_lookupfunction offset_dir_emitfunction offset_iterate_dirfunction offset_readdirfunction __simple_recursive_removalfunction simple_recursive_removalfunction simple_remove_by_namefunction locked_recursive_removalfunction pseudo_fs_fill_superfunction pseudo_fs_get_treefunction pseudo_fs_freefunction simple_openfunction simple_linkfunction simple_emptyfunction __simple_unlinkfunction __simple_rmdirfunction simple_unlinkfunction simple_rmdirfunction simple_rename_timestampfunction simple_rename_exchangefunction simple_renamefunction simple_setattrfunction simple_read_foliofunction simple_write_beginfunction generic_write_endfunction simple_fill_superfunction simple_pin_fsfunction simple_release_fsfunction simple_read_from_buffer
Annotated Snippet
const struct file_operations simple_dir_operations = {
.open = dcache_dir_open,
.release = dcache_dir_close,
.llseek = dcache_dir_lseek,
.read = generic_read_dir,
.iterate_shared = dcache_readdir,
.fsync = noop_fsync,
};
EXPORT_SYMBOL(simple_dir_operations);
const struct inode_operations simple_dir_inode_operations = {
.lookup = simple_lookup,
};
EXPORT_SYMBOL(simple_dir_inode_operations);
/* simple_offset_add() never assigns these to a dentry */
enum {
DIR_OFFSET_FIRST = 2, /* Find first real entry */
DIR_OFFSET_EOD = S32_MAX,
};
/* simple_offset_add() allocation range */
enum {
DIR_OFFSET_MIN = DIR_OFFSET_FIRST + 1,
DIR_OFFSET_MAX = DIR_OFFSET_EOD - 1,
};
static void offset_set(struct dentry *dentry, long offset)
{
dentry->d_fsdata = (void *)offset;
}
static long dentry2offset(struct dentry *dentry)
{
return (long)dentry->d_fsdata;
}
static struct lock_class_key simple_offset_lock_class;
/**
* simple_offset_init - initialize an offset_ctx
* @octx: directory offset map to be initialized
*
*/
void simple_offset_init(struct offset_ctx *octx)
{
mt_init_flags(&octx->mt, MT_FLAGS_ALLOC_RANGE);
lockdep_set_class(&octx->mt.ma_lock, &simple_offset_lock_class);
octx->next_offset = DIR_OFFSET_MIN;
}
/**
* simple_offset_add - Add an entry to a directory's offset map
* @octx: directory offset ctx to be updated
* @dentry: new dentry being added
*
* Returns zero on success. @octx and the dentry's offset are updated.
* Otherwise, a negative errno value is returned.
*/
int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry)
{
unsigned long offset;
int ret;
if (dentry2offset(dentry) != 0)
return -EBUSY;
ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN,
DIR_OFFSET_MAX, &octx->next_offset,
GFP_KERNEL);
if (unlikely(ret < 0))
return ret == -EBUSY ? -ENOSPC : ret;
offset_set(dentry, offset);
return 0;
}
static int simple_offset_replace(struct offset_ctx *octx, struct dentry *dentry,
long offset)
{
int ret;
ret = mtree_store(&octx->mt, offset, dentry, GFP_KERNEL);
if (ret)
return ret;
offset_set(dentry, offset);
return 0;
}
/**
Annotation
- Immediate include surface: `linux/blkdev.h`, `linux/export.h`, `linux/filelock.h`, `linux/pagemap.h`, `linux/slab.h`, `linux/cred.h`, `linux/mount.h`, `linux/vfs.h`.
- Detected declarations: `struct simple_attr`, `function simple_getattr`, `function simple_statfs`, `function always_delete_dentry`, `function dcache_dir_open`, `function dcache_dir_close`, `function dcache_dir_lseek`, `function dcache_readdir`, `function generic_read_dir`, `function offset_set`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.