-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathbitmap.h
105 lines (91 loc) · 3.07 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
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef SIMPLEFS_BITMAP_H
#define SIMPLEFS_BITMAP_H
#include <linux/bitmap.h>
#include "simplefs.h"
/* Returns the first bit found and clears the following 'len' consecutive
* free bits (sets them to 1) in a given in-memory bitmap spanning multiple
* blocks. Returns 0 if an adequate number of free bits were not found.
* Assumes the first bit is never free (reserved for the superblock and the
* root inode), allowing the use of 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.
* Clean the block content.
* Return 0 if no enough free block(s) were found.
*/
static inline uint32_t get_free_blocks(struct super_block *sb, uint32_t len)
{
struct simplefs_sb_info *sbi = SIMPLEFS_SB(sb);
uint32_t ret = get_first_free_bits(sbi->bfree_bitmap, sbi->nr_blocks, len);
uint32_t i;
if (!ret) /* No enough free blocks */
return 0;
sbi->nr_free_blocks -= len;
for (i = 0; i < len; i++) {
struct buffer_head *bh = sb_bread(sb, ret + i);
if (!bh) {
pr_err("get_free_blocks: sb_bread failed for block %d\n", ret + i);
sbi->nr_free_blocks += len;
return -EIO;
}
memset(bh->b_data, 0, SIMPLEFS_BLOCK_SIZE);
mark_buffer_dirty(bh);
sync_dirty_buffer(bh); /* write the buffer to disk */
brelse(bh);
}
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 */