SFUSE_Documentation
로딩중...
검색중...
일치하는것 없음
bitmap.c
이 파일의 문서화 페이지로 가기
1// File: src/bitmap.c
2
3#include "bitmap.h"
4#include "block.h"
5#include <errno.h>
6#include <unistd.h>
7
11static const uint32_t BITS_PER_BLOCK = SFUSE_BLOCK_SIZE * 8;
12
21int bitmap_load(int fd, uint32_t start_blk, struct sfuse_bitmaps *bmaps,
22 uint32_t count) {
23 uint8_t *buffer = (uint8_t *)bmaps;
24 for (uint32_t i = 0; i < count; ++i) {
25 if (read_block(fd, start_blk + i, buffer + i * SFUSE_BLOCK_SIZE) < 0) {
26 return -EIO;
27 }
28 }
29 return 0;
30}
31
40int bitmap_sync(int fd, uint32_t start_blk, const struct sfuse_bitmaps *bmaps,
41 uint32_t count) {
42 const uint8_t *buffer = (const uint8_t *)bmaps;
43 for (uint32_t i = 0; i < count; ++i) {
44 if (write_block(fd, start_blk + i, buffer + i * SFUSE_BLOCK_SIZE) < 0) {
45 return -EIO;
46 }
47 }
48 return 0;
49}
50
57int alloc_bit(uint8_t *map, uint32_t total_bits) {
58 uint32_t blocks = (total_bits + BITS_PER_BLOCK - 1) / BITS_PER_BLOCK;
59 for (uint32_t b = 0; b < blocks; ++b) {
60 uint8_t *block_ptr = map + b * SFUSE_BLOCK_SIZE;
61 for (uint32_t byte = 0; byte < SFUSE_BLOCK_SIZE; ++byte) {
62 if (block_ptr[byte] == 0xFF)
63 continue;
64 for (uint32_t bit = 0; bit < 8; ++bit) {
65 if (!(block_ptr[byte] & (1u << bit))) {
66 uint32_t index = b * BITS_PER_BLOCK + byte * 8 + bit;
67 if (index < total_bits) {
68 block_ptr[byte] |= (uint8_t)(1u << bit);
69 return index;
70 }
71 }
72 }
73 }
74 }
75 return -ENOSPC;
76}
77
83void free_bit(uint8_t *map, uint32_t idx) {
84 uint32_t byte_index = idx / 8;
85 uint32_t bit_offset = idx % 8;
86 map[byte_index] &= (uint8_t)~(1u << bit_offset);
87}
88
95int alloc_inode(struct sfuse_superblock *sb, struct sfuse_inode_bitmap *imap) {
96 int ino = alloc_bit(imap->map, sb->total_inodes);
97 if (ino >= 0) {
98 sb->free_inodes -= 1;
99 }
100 return ino;
101}
102
109void free_inode(struct sfuse_superblock *sb, struct sfuse_inode_bitmap *imap,
110 uint32_t ino) {
111 free_bit(imap->map, ino);
112 sb->free_inodes += 1;
113}
114
121int alloc_block(struct sfuse_superblock *sb, struct sfuse_block_bitmap *bmap) {
122 int blk = alloc_bit(bmap->map, sb->total_blocks);
123 if (blk >= 0) {
124 sb->free_blocks -= 1;
125 }
126 return blk;
127}
128
135void free_block(struct sfuse_superblock *sb, struct sfuse_block_bitmap *bmap,
136 uint32_t blk) {
137 free_bit(bmap->map, blk);
138 sb->free_blocks += 1;
139}
int alloc_inode(struct sfuse_superblock *sb, struct sfuse_inode_bitmap *imap)
새로운 아이노드 할당
Definition bitmap.c:95
int bitmap_sync(int fd, uint32_t start_blk, const struct sfuse_bitmaps *bmaps, uint32_t count)
메모리에 있는 비트맵을 디스크에 저장
Definition bitmap.c:40
void free_inode(struct sfuse_superblock *sb, struct sfuse_inode_bitmap *imap, uint32_t ino)
아이노드 해제
Definition bitmap.c:109
static const uint32_t BITS_PER_BLOCK
한 블록에 담길 수 있는 비트 수
Definition bitmap.c:11
int alloc_block(struct sfuse_superblock *sb, struct sfuse_block_bitmap *bmap)
새로운 데이터 블록 할당
Definition bitmap.c:121
int bitmap_load(int fd, uint32_t start_blk, struct sfuse_bitmaps *bmaps, uint32_t count)
디스크에서 연속된 비트맵 블록들을 읽어 메모리에 로드
Definition bitmap.c:21
void free_bit(uint8_t *map, uint32_t idx)
비트맵에서 지정한 인덱스 비트를 0으로 설정
Definition bitmap.c:83
int alloc_bit(uint8_t *map, uint32_t total_bits)
비트맵에서 0인 비트를 찾아 1로 설정하고 인덱스를 반환
Definition bitmap.c:57
void free_block(struct sfuse_superblock *sb, struct sfuse_block_bitmap *bmap, uint32_t blk)
데이터 블록 해제
Definition bitmap.c:135
ssize_t read_block(int fd, uint32_t blk, void *out_buf)
지정한 블록 번호의 데이터를 읽어 버퍼에 저장
Definition block.c:15
ssize_t write_block(int fd, uint32_t blk, const void *buf)
버퍼의 내용을 지정한 블록 번호에 기록
Definition block.c:31
아이노드 비트맵과 블록 비트맵을 함께 담는 구조체
Definition bitmap.h:30
데이터 블록 할당 비트맵 (2블록)
Definition bitmap.h:23
uint8_t map[4096 *2]
Definition bitmap.h:24
SFUSE용 비트맵 구조체 (아이노드/블록 할당 상태 추적)
Definition bitmap.h:16
uint8_t map[4096]
Definition bitmap.h:17
슈퍼블록 구조체
Definition super.h:51
uint32_t total_inodes
Definition super.h:53
uint32_t free_inodes
Definition super.h:55
uint32_t free_blocks
Definition super.h:56
uint32_t total_blocks
Definition super.h:54
#define SFUSE_BLOCK_SIZE
블록 크기 (바이트 단위)
Definition super.h:20