fs/ufs/balloc.c
Source file repositories/reference/linux-study-clean/fs/ufs/balloc.c
File Facts
- System
- Linux kernel
- Corpus path
fs/ufs/balloc.c- Extension
.c- Size
- 27568 bytes
- Lines
- 937
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: implementation source
- Status
- source 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.
- 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/fs.hlinux/stat.hlinux/time.hlinux/string.hlinux/buffer_head.hlinux/capability.hlinux/bitops.hlinux/bio.hasm/byteorder.hufs_fs.hufs.hswab.hutil.h
Detected Declarations
function adjust_free_blocksfunction ufs_free_fragmentsfunction ufs_free_blocksfunction ufs_change_blocknrfunction ufs_clear_fragsfunction ufs_new_fragmentsfunction try_add_fragsfunction ufs_add_fragmentsfunction ufs_alloc_fragmentsfunction ufs_alloccg_blockfunction ubh_scancfunction ufs_bitmap_searchfunction ufs_clusteracct
Annotated Snippet
if (ubh_isblockset(uspi, ucpi, i)) {
ufs_error(sb, "ufs_free_blocks", "freeing free fragment");
}
ubh_setblock(uspi, ucpi, i);
inode_sub_bytes(inode, uspi->s_fpb << uspi->s_fshift);
adjust_free_blocks(sb, ucg, ucpi, i, 1);
}
ubh_mark_buffer_dirty (USPI_UBH(uspi));
ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
if (sb->s_flags & SB_SYNCHRONOUS)
ubh_sync_block(UCPI_UBH(ucpi));
if (overflow) {
fragment += count;
count = overflow;
goto do_more;
}
ufs_mark_sb_dirty(sb);
mutex_unlock(&UFS_SB(sb)->s_lock);
UFSD("EXIT\n");
return;
failed_unlock:
mutex_unlock(&UFS_SB(sb)->s_lock);
failed:
UFSD("EXIT (FAILED)\n");
return;
}
/*
* Modify inode page cache in such way:
* have - blocks with b_blocknr equal to oldb...oldb+count-1
* get - blocks with b_blocknr equal to newb...newb+count-1
* also we suppose that oldb...oldb+count-1 blocks
* situated at the end of file.
*
* We can come here from ufs_writepage or ufs_prepare_write,
* locked_folio is argument of these functions, so we already lock it.
*/
static void ufs_change_blocknr(struct inode *inode, sector_t beg,
unsigned int count, sector_t oldb,
sector_t newb, struct folio *locked_folio)
{
struct folio *folio;
const unsigned blks_per_page =
1 << (PAGE_SHIFT - inode->i_blkbits);
const unsigned mask = blks_per_page - 1;
struct address_space * const mapping = inode->i_mapping;
pgoff_t index, cur_index, last_index;
unsigned pos, j, lblock;
sector_t end, i;
struct buffer_head *head, *bh;
UFSD("ENTER, ino %llu, count %u, oldb %llu, newb %llu\n",
inode->i_ino, count,
(unsigned long long)oldb, (unsigned long long)newb);
BUG_ON(!folio_test_locked(locked_folio));
cur_index = locked_folio->index;
end = count + beg;
last_index = end >> (PAGE_SHIFT - inode->i_blkbits);
for (i = beg; i < end; i = (i | mask) + 1) {
index = i >> (PAGE_SHIFT - inode->i_blkbits);
if (likely(cur_index != index)) {
folio = ufs_get_locked_folio(mapping, index);
if (!folio) /* it was truncated */
continue;
if (IS_ERR(folio)) {/* or EIO */
ufs_error(inode->i_sb, __func__,
"read of page %llu failed\n",
(unsigned long long)index);
continue;
}
} else
folio = locked_folio;
head = folio_buffers(folio);
bh = head;
pos = i & mask;
for (j = 0; j < pos; ++j)
bh = bh->b_this_page;
if (unlikely(index == last_index))
lblock = end & mask;
else
lblock = blks_per_page;
Annotation
- Immediate include surface: `linux/fs.h`, `linux/stat.h`, `linux/time.h`, `linux/string.h`, `linux/buffer_head.h`, `linux/capability.h`, `linux/bitops.h`, `linux/bio.h`.
- Detected declarations: `function adjust_free_blocks`, `function ufs_free_fragments`, `function ufs_free_blocks`, `function ufs_change_blocknr`, `function ufs_clear_frags`, `function ufs_new_fragments`, `function try_add_frags`, `function ufs_add_fragments`, `function ufs_alloc_fragments`, `function ufs_alloccg_block`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: source 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.