AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
block.c
Go to the documentation of this file.
1
5
6#include <ds/bitmap.h>
7#include <minix.h>
8
17{
18 /* look for a free zone: bit i of the bitmap is zone firstdatazone +
19 * i - 1 (bit 0 is reserved) */
20 blk_t blk = 1;
21 for (; blk <= desc->zmap.max_idx && bitmap_check(&desc->zmap, blk); ++blk);
22
23 /* out of zones */
24 if (blk > desc->zmap.max_idx)
25 return 0;
26
27 bitmap_set(&desc->zmap, blk);
28
29 /* write the bitmap back now (it could be deferred) */
30 vfs_write(desc->supernode, desc->zmap_off, desc->superblock->zmap_blocks * desc->bs, desc->zmap.map);
31
32 return desc->superblock->firstdatazone + blk - 1;
33}
34
42void minix_block_free(struct minix *desc, blk_t blk)
43{
44 /* not a data zone: nothing to free */
45 if (blk < desc->superblock->firstdatazone)
46 return;
47
48 blk = blk - desc->superblock->firstdatazone + 1;
49
50 if (blk > desc->zmap.max_idx)
51 return;
52
53 /* clear its bit; a cached copy of the block (an indirect zone's
54 * table) goes too */
55 bitmap_clear(&desc->zmap, blk);
56 bcache_remove(desc->bcache, blk + desc->superblock->firstdatazone - 1);
57
58 /* write the bitmap back now (it could be deferred) */
59 vfs_write(desc->supernode, desc->zmap_off, desc->superblock->zmap_blocks * desc->bs, desc->zmap.map);
60}
61
70ssize_t minix_block_read(struct minix *desc, blk_t blk, void *buf)
71{
72 return vfs_read(desc->supernode, blk * desc->bs, desc->bs, buf);
73}
74
83ssize_t minix_block_write(struct minix *desc, blk_t blk, void *buf)
84{
85 return vfs_write(desc->supernode, blk * desc->bs, desc->bs, buf);
86}
87
97int minix_bcache_get(struct minix *desc, blk_t blk, void **data)
98{
99 int err = 0;
100 void *r = NULL;
101
102 if ((r = bcache_find(desc->bcache, blk))) {
103 *data = r;
104 return 0;
105 }
106
107 r = kmalloc(desc->bs, &M_BUFFER, 0);
108 if (!r) goto e_nomem;
109
110 if ((err = minix_block_read(desc, blk, r)) < 0)
111 goto error;
112
113 bcache_insert(desc->bcache, blk, r);
114
115 *data = r;
116 return 0;
117
118e_nomem:
119 err = -ENOMEM;
120
121error:
122 return err;
123}
int bcache_remove(struct bcache *bcache, uint64_t off)
Takes a block out of a cache.
Definition bcache.c:73
int bcache_insert(struct bcache *bcache, uint64_t off, void *data)
Adds a block to a cache, which keeps its memory from now on.
Definition bcache.c:48
void * bcache_find(struct bcache *bcache, uint64_t off)
Finds a cached block.
Definition bcache.c:100
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition kvmem.c:123
ssize_t minix_block_write(struct minix *desc, blk_t blk, void *buf)
Writes a block to the disk.
Definition block.c:83
void minix_block_free(struct minix *desc, blk_t blk)
Frees a zone: its bit is cleared in the zone bitmap, which is written back at once,...
Definition block.c:42
ssize_t minix_block_read(struct minix *desc, blk_t blk, void *buf)
Reads a block from the disk.
Definition block.c:70
int minix_bcache_get(struct minix *desc, blk_t blk, void **data)
Gets a block from the block cache, reading it from the disk into the cache if it isn't there.
Definition block.c:97
blk_t minix_block_alloc(struct minix *desc)
Allocates a free zone: its bit is set in the zone bitmap, which is written back to the disk at once.
Definition block.c:16
uint32_t blk_t
Definition minix.h:113
ssize_t vfs_read(struct vnode *vnode, off_t off, size_t size, void *buf)
Reads from a vnode into a kernel buffer.
Definition read.c:52
#define NULL
Definition stddef.h:27
size_t max_idx
Definition bitmap.h:19
bitmap_t * map
Definition bitmap.h:18
A block of the cache.
Definition blkcache.c:53
uint16_t firstdatazone
Definition minix.h:38
uint16_t zmap_blocks
Definition minix.h:36
A mounted minix filesystem.
Definition minix.h:82
uint32_t zmap_off
Definition minix.h:91
size_t bs
Definition minix.h:101
struct minix_superblock * superblock
Definition minix.h:84
struct vnode * supernode
Definition minix.h:83
struct bcache * bcache
Definition minix.h:104
struct bitmap zmap
Definition minix.h:94
long int ssize_t
Definition types.h:12
ssize_t vfs_write(struct vnode *vnode, off_t off, size_t size, void *buf)
Writes to a vnode from a kernel buffer.
Definition write.c:60