forked from sysprog21/simplefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap.h
92 lines (79 loc) · 2.56 KB
/
bitmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef SIMPLEFS_BITMAP_H
#define SIMPLEFS_BITMAP_H
#include <linux/bitmap.h>
#include "simplefs.h"
/*
* Return the first bit we found and clear the the following `len` consecutive
* free bit(s) (set to 1) in a given in-memory bitmap spanning over multiple
* blocks. Return 0 if no enough free bit(s) were found (we assume that the
* first bit is never free because of the superblock and the root inode, thus
* allowing us to use 0 as an error value).
*/
static inline uint32_t get_first_free_bits(unsigned long *freemap,
unsigned long size,
uint32_t len)
{
uint32_t bit, prev = 0, count = 0;
for_each_set_bit (bit, freemap, size) {
if (prev != bit - 1)
count = 0;
prev = bit;
if (++count == len) {
bitmap_clear(freemap, bit - len + 1, len);
return bit - len + 1;
}
}
return 0;
}
/*
* Return an unused inode number and mark it used.
* Return 0 if no free inode was found.
*/
static inline uint32_t get_free_inode(struct simplefs_sb_info *sbi)
{
uint32_t ret = get_first_free_bits(sbi->ifree_bitmap, sbi->nr_inodes, 1);
if (ret)
sbi->nr_free_inodes--;
return ret;
}
/*
* Return `len` unused block(s) number and mark it used.
* Return 0 if no enough free block(s) were found.
*/
static inline uint32_t get_free_blocks(struct simplefs_sb_info *sbi,
uint32_t len)
{
uint32_t ret = get_first_free_bits(sbi->bfree_bitmap, sbi->nr_blocks, len);
if (ret)
sbi->nr_free_blocks -= len;
return ret;
}
/* Mark the `len` bit(s) from i-th bit in freemap as free (i.e. 1) */
static inline int put_free_bits(unsigned long *freemap,
unsigned long size,
uint32_t i,
uint32_t len)
{
/* i is greater than freemap size */
if (i + len - 1 > size)
return -1;
bitmap_set(freemap, i, len);
return 0;
}
/* Mark an inode as unused */
static inline void put_inode(struct simplefs_sb_info *sbi, uint32_t ino)
{
if (put_free_bits(sbi->ifree_bitmap, sbi->nr_inodes, ino, 1))
return;
sbi->nr_free_inodes++;
}
/* Mark len block(s) as unused */
static inline void put_blocks(struct simplefs_sb_info *sbi,
uint32_t bno,
uint32_t len)
{
if (put_free_bits(sbi->bfree_bitmap, sbi->nr_blocks, bno, len))
return;
sbi->nr_free_blocks += len;
}
#endif /* SIMPLEFS_BITMAP_H */