-
Notifications
You must be signed in to change notification settings - Fork 9
/
balloc.c
286 lines (253 loc) · 7.59 KB
/
balloc.c
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* PMFS emulated persistence. This file contains code to
* handle data blocks of various sizes efficiently.
*
* Persistent Memory File System
* Copyright (c) 2012-2013, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <linux/fs.h>
#include <linux/bitops.h>
#include "pmfs.h"
void pmfs_init_blockmap(struct super_block *sb, unsigned long init_used_size)
{
struct pmfs_sb_info *sbi = PMFS_SB(sb);
unsigned long num_used_block;
struct pmfs_blocknode *blknode;
num_used_block = (init_used_size + sb->s_blocksize - 1) >>
sb->s_blocksize_bits;
blknode = pmfs_alloc_blocknode(sb);
if (blknode == NULL)
PMFS_ASSERT(0);
blknode->block_low = sbi->block_start;
blknode->block_high = sbi->block_start + num_used_block - 1;
sbi->num_free_blocks -= num_used_block;
list_add(&blknode->link, &sbi->block_inuse_head);
}
static struct pmfs_blocknode *pmfs_next_blocknode(struct pmfs_blocknode *i,
struct list_head *head)
{
if (list_is_last(&i->link, head))
return NULL;
return list_first_entry(&i->link, typeof(*i), link);
}
/* Caller must hold the super_block lock. If start_hint is provided, it is
* only valid until the caller releases the super_block lock. */
void __pmfs_free_block(struct super_block *sb, unsigned long blocknr,
unsigned short btype, struct pmfs_blocknode **start_hint)
{
struct pmfs_sb_info *sbi = PMFS_SB(sb);
struct list_head *head = &(sbi->block_inuse_head);
unsigned long new_block_low;
unsigned long new_block_high;
unsigned long num_blocks = 0;
struct pmfs_blocknode *i;
struct pmfs_blocknode *free_blocknode= NULL;
struct pmfs_blocknode *curr_node;
num_blocks = pmfs_get_numblocks(btype);
new_block_low = blocknr;
new_block_high = blocknr + num_blocks - 1;
BUG_ON(list_empty(head));
if (start_hint && *start_hint &&
new_block_low >= (*start_hint)->block_low)
i = *start_hint;
else
i = list_first_entry(head, typeof(*i), link);
list_for_each_entry_from(i, head, link) {
if (new_block_low > i->block_high) {
/* skip to next blocknode */
continue;
}
if ((new_block_low == i->block_low) &&
(new_block_high == i->block_high)) {
/* fits entire datablock */
if (start_hint)
*start_hint = pmfs_next_blocknode(i, head);
list_del(&i->link);
free_blocknode = i;
sbi->num_blocknode_allocated--;
sbi->num_free_blocks += num_blocks;
goto block_found;
}
if ((new_block_low == i->block_low) &&
(new_block_high < i->block_high)) {
/* Aligns left */
i->block_low = new_block_high + 1;
sbi->num_free_blocks += num_blocks;
if (start_hint)
*start_hint = i;
goto block_found;
}
if ((new_block_low > i->block_low) &&
(new_block_high == i->block_high)) {
/* Aligns right */
i->block_high = new_block_low - 1;
sbi->num_free_blocks += num_blocks;
if (start_hint)
*start_hint = pmfs_next_blocknode(i, head);
goto block_found;
}
if ((new_block_low > i->block_low) &&
(new_block_high < i->block_high)) {
/* Aligns somewhere in the middle */
curr_node = pmfs_alloc_blocknode(sb);
PMFS_ASSERT(curr_node);
if (curr_node == NULL) {
/* returning without freeing the block*/
goto block_found;
}
curr_node->block_low = new_block_high + 1;
curr_node->block_high = i->block_high;
i->block_high = new_block_low - 1;
list_add(&curr_node->link, &i->link);
sbi->num_free_blocks += num_blocks;
if (start_hint)
*start_hint = curr_node;
goto block_found;
}
}
pmfs_error_mng(sb, "Unable to free block %ld\n", blocknr);
block_found:
if (free_blocknode)
__pmfs_free_blocknode(free_blocknode);
}
void pmfs_free_block(struct super_block *sb, unsigned long blocknr,
unsigned short btype)
{
struct pmfs_sb_info *sbi = PMFS_SB(sb);
mutex_lock(&sbi->s_lock);
__pmfs_free_block(sb, blocknr, btype, NULL);
mutex_unlock(&sbi->s_lock);
}
int pmfs_new_block(struct super_block *sb, unsigned long *blocknr,
unsigned short btype, int zero)
{
struct pmfs_sb_info *sbi = PMFS_SB(sb);
struct list_head *head = &(sbi->block_inuse_head);
struct pmfs_blocknode *i, *next_i;
struct pmfs_blocknode *free_blocknode= NULL;
void *bp;
unsigned long num_blocks = 0;
struct pmfs_blocknode *curr_node;
int errval = 0;
bool found = 0;
unsigned long next_block_low;
unsigned long new_block_low;
unsigned long new_block_high;
num_blocks = pmfs_get_numblocks(btype);
mutex_lock(&sbi->s_lock);
list_for_each_entry(i, head, link) {
if (i->link.next == head) {
next_i = NULL;
next_block_low = sbi->block_end;
} else {
next_i = list_entry(i->link.next, typeof(*i), link);
next_block_low = next_i->block_low;
}
new_block_low = (i->block_high + num_blocks) & ~(num_blocks - 1);
new_block_high = new_block_low + num_blocks - 1;
if (new_block_high >= next_block_low) {
/* Does not fit - skip to next blocknode */
continue;
}
if ((new_block_low == (i->block_high + 1)) &&
(new_block_high == (next_block_low - 1)))
{
/* Fill the gap completely */
if (next_i) {
i->block_high = next_i->block_high;
list_del(&next_i->link);
free_blocknode = next_i;
sbi->num_blocknode_allocated--;
} else {
i->block_high = new_block_high;
}
found = 1;
break;
}
if ((new_block_low == (i->block_high + 1)) &&
(new_block_high < (next_block_low - 1))) {
/* Aligns to left */
i->block_high = new_block_high;
found = 1;
break;
}
if ((new_block_low > (i->block_high + 1)) &&
(new_block_high == (next_block_low - 1))) {
/* Aligns to right */
if (next_i) {
/* right node exist */
next_i->block_low = new_block_low;
} else {
/* right node does NOT exist */
curr_node = pmfs_alloc_blocknode(sb);
PMFS_ASSERT(curr_node);
if (curr_node == NULL) {
errval = -ENOSPC;
break;
}
curr_node->block_low = new_block_low;
curr_node->block_high = new_block_high;
list_add(&curr_node->link, &i->link);
}
found = 1;
break;
}
if ((new_block_low > (i->block_high + 1)) &&
(new_block_high < (next_block_low - 1))) {
/* Aligns somewhere in the middle */
curr_node = pmfs_alloc_blocknode(sb);
PMFS_ASSERT(curr_node);
if (curr_node == NULL) {
errval = -ENOSPC;
break;
}
curr_node->block_low = new_block_low;
curr_node->block_high = new_block_high;
list_add(&curr_node->link, &i->link);
found = 1;
break;
}
}
if (found == 1) {
sbi->num_free_blocks -= num_blocks;
}
mutex_unlock(&sbi->s_lock);
if (free_blocknode)
__pmfs_free_blocknode(free_blocknode);
if (found == 0) {
return -ENOSPC;
}
if (zero) {
size_t size;
bp = pmfs_get_block(sb, pmfs_get_block_off(sb, new_block_low, btype));
pmfs_memunlock_block(sb, bp); //TBDTBD: Need to fix this
if (btype == PMFS_BLOCK_TYPE_4K)
size = 0x1 << 12;
else if (btype == PMFS_BLOCK_TYPE_2M)
size = 0x1 << 21;
else
size = 0x1 << 30;
memset_nt(bp, 0, size);
pmfs_memlock_block(sb, bp);
}
*blocknr = new_block_low;
return errval;
}
unsigned long pmfs_count_free_blocks(struct super_block *sb)
{
struct pmfs_sb_info *sbi = PMFS_SB(sb);
return sbi->num_free_blocks;
}