fs/ntfs/lcnalloc.c
Source file repositories/reference/linux-study-clean/fs/ntfs/lcnalloc.c
File Facts
- System
- Linux kernel
- Corpus path
fs/ntfs/lcnalloc.c- Extension
.c- Size
- 33239 bytes
- Lines
- 1050
- 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.
- 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.hlcnalloc.hbitmap.hntfs.h
Detected Declarations
function Clusterfunction max_empty_bit_rangefunction IS_ERRfunction kvzallocfunction clusters
Annotated Snippet
switch (*buf) {
case 0:
do {
buf++;
run += 8;
i++;
} while ((i < size) && !*buf);
break;
case 255:
if (run > max_range) {
max_range = run;
start_pos = (s64)i * 8 - run;
}
run = 0;
do {
buf++;
i++;
} while ((i < size) && (*buf == 255));
break;
default:
for (j = 0; j < 8; j++) {
int bit = *buf & (1 << j);
if (bit) {
if (run > max_range) {
max_range = run;
start_pos = (s64)i * 8 + (j - run);
}
run = 0;
} else
run++;
}
i++;
buf++;
}
}
if (run > max_range)
start_pos = (s64)i * 8 - run;
return start_pos;
}
/*
* ntfs_cluster_alloc - allocate clusters on an ntfs volume
* @vol: mounted ntfs volume on which to allocate clusters
* @start_vcn: vcn of the first allocated cluster
* @count: number of clusters to allocate
* @start_lcn: starting lcn at which to allocate the clusters or -1 if none
* @zone: zone from which to allocate (MFT_ZONE or DATA_ZONE)
* @is_extension: if true, the caller is extending an attribute
* @is_contig: if true, require contiguous allocation
* @is_dealloc: if true, the allocation is for deallocation purposes
*
* Allocate @count clusters preferably starting at cluster @start_lcn or at the
* current allocator position if @start_lcn is -1, on the mounted ntfs volume
* @vol. @zone is either DATA_ZONE for allocation of normal clusters or
* MFT_ZONE for allocation of clusters for the master file table, i.e. the
* $MFT/$DATA attribute.
*
* @start_vcn specifies the vcn of the first allocated cluster. This makes
* merging the resulting runlist with the old runlist easier.
*
* If @is_extension is 'true', the caller is allocating clusters to extend an
* attribute and if it is 'false', the caller is allocating clusters to fill a
* hole in an attribute. Practically the difference is that if @is_extension
* is 'true' the returned runlist will be terminated with LCN_ENOENT and if
* @is_extension is 'false' the runlist will be terminated with
* LCN_RL_NOT_MAPPED.
*
* You need to check the return value with IS_ERR(). If this is false, the
* function was successful and the return value is a runlist describing the
* allocated cluster(s). If IS_ERR() is true, the function failed and
* PTR_ERR() gives you the error code.
*
* Notes on the allocation algorithm
* =================================
*
* There are two data zones. First is the area between the end of the mft zone
* and the end of the volume, and second is the area between the start of the
* volume and the start of the mft zone. On unmodified/standard NTFS 1.x
* volumes, the second data zone does not exist due to the mft zone being
* expanded to cover the start of the volume in order to reserve space for the
* mft bitmap attribute.
*
* This is not the prettiest function but the complexity stems from the need of
* implementing the mft vs data zoned approach and from the fact that we have
* access to the lcn bitmap in portions of up to 8192 bytes at a time, so we
* need to cope with crossing over boundaries of two buffers. Further, the
* fact that the allocator allows for caller supplied hints as to the location
Annotation
- Immediate include surface: `linux/blkdev.h`, `lcnalloc.h`, `bitmap.h`, `ntfs.h`.
- Detected declarations: `function Cluster`, `function max_empty_bit_range`, `function IS_ERR`, `function kvzalloc`, `function clusters`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: source 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.