fs/bfs/file.c
Source file repositories/reference/linux-study-clean/fs/bfs/file.c
File Facts
- System
- Linux kernel
- Corpus path
fs/bfs/file.c- Extension
.c- Size
- 5142 bytes
- Lines
- 204
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/fs.hlinux/mpage.hlinux/buffer_head.hbfs.h
Detected Declarations
function bfs_move_blockfunction bfs_move_blocksfunction bfs_get_blockfunction bfs_writepagesfunction bfs_read_foliofunction bfs_write_failedfunction bfs_write_beginfunction bfs_bmap
Annotated Snippet
const struct file_operations bfs_file_operations = {
.llseek = generic_file_llseek,
.read_iter = generic_file_read_iter,
.write_iter = generic_file_write_iter,
.mmap_prepare = generic_file_mmap_prepare,
.splice_read = filemap_splice_read,
};
static int bfs_move_block(unsigned long from, unsigned long to,
struct super_block *sb)
{
struct buffer_head *bh, *new;
bh = sb_bread(sb, from);
if (!bh)
return -EIO;
new = sb_getblk(sb, to);
memcpy(new->b_data, bh->b_data, bh->b_size);
mark_buffer_dirty(new);
bforget(bh);
brelse(new);
return 0;
}
static int bfs_move_blocks(struct super_block *sb, unsigned long start,
unsigned long end, unsigned long where)
{
unsigned long i;
dprintf("%08lx-%08lx->%08lx\n", start, end, where);
for (i = start; i <= end; i++)
if(bfs_move_block(i, where + i, sb)) {
dprintf("failed to move block %08lx -> %08lx\n", i,
where + i);
return -EIO;
}
return 0;
}
static int bfs_get_block(struct inode *inode, sector_t block,
struct buffer_head *bh_result, int create)
{
unsigned long phys;
int err;
struct super_block *sb = inode->i_sb;
struct bfs_sb_info *info = BFS_SB(sb);
struct bfs_inode_info *bi = BFS_I(inode);
phys = bi->i_sblock + block;
if (!create) {
if (phys <= bi->i_eblock) {
dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
create, (unsigned long)block, phys);
map_bh(bh_result, sb, phys);
}
return 0;
}
/*
* If the file is not empty and the requested block is within the
* range of blocks allocated for this file, we can grant it.
*/
if (bi->i_sblock && (phys <= bi->i_eblock)) {
dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
create, (unsigned long)block, phys);
map_bh(bh_result, sb, phys);
return 0;
}
/* The file will be extended, so let's see if there is enough space. */
if (phys >= info->si_blocks)
return -ENOSPC;
/* The rest has to be protected against itself. */
mutex_lock(&info->bfs_lock);
/*
* If the last data block for this file is the last allocated
* block, we can extend the file trivially, without moving it
* anywhere.
*/
if (bi->i_eblock == info->si_lf_eblk) {
dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
create, (unsigned long)block, phys);
map_bh(bh_result, sb, phys);
info->si_freeb -= phys - bi->i_eblock;
info->si_lf_eblk = bi->i_eblock = phys;
mark_inode_dirty(inode);
err = 0;
goto out;
Annotation
- Immediate include surface: `linux/fs.h`, `linux/mpage.h`, `linux/buffer_head.h`, `bfs.h`.
- Detected declarations: `function bfs_move_block`, `function bfs_move_blocks`, `function bfs_get_block`, `function bfs_writepages`, `function bfs_read_folio`, `function bfs_write_failed`, `function bfs_write_begin`, `function bfs_bmap`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: pattern implementation candidate.
- 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.