/
file.c
329 lines (277 loc) · 6.6 KB
/
file.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* Tempesta DB
*
* File mapping and IO.
*
* Copyright (C) 2014 NatSys Lab. (info@natsys-lab.com).
* Copyright (C) 2015-2021 Tempesta Technologies.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that 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., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/tempesta.h>
#include <linux/topology.h>
#include <linux/writeback.h>
#include "file.h"
/**
* Node of list of free/user memory areas.
* Used for best-fit memory allocation.
*/
typedef struct marea_t {
struct marea_t *prev, *next; /* NULL-terminated list */
unsigned long start;
unsigned long pages;
unsigned long flags;
} MArea;
#define MA_F_USED 0x1
#define MA_FREE(ma) (!((ma)->flags & MA_F_USED))
static DEFINE_MUTEX(map_mtx);
static MArea mas[MAX_NUMNODES];
/**
* Find and return best fit free memory area.
*/
static MArea *
ma_get_best_fit(unsigned long len, int node)
{
MArea *ma, *best_fit = NULL;
unsigned long req_pages = len / PAGE_SIZE;
for (ma = &mas[node]; ma; ma = ma->next) {
if (MA_FREE(ma)
&& ma->pages > req_pages
&& (!best_fit || best_fit->pages > ma->pages))
best_fit = ma;
}
return best_fit;
}
/**
* Split @len length memory area from @ma.
*/
static MArea *
ma_split(MArea *ma, unsigned long len)
{
unsigned long req_pages = len / PAGE_SIZE;
MArea *ret;
BUG_ON(ma->pages < req_pages);
BUG_ON(!MA_FREE(ma));
if (ma->pages == req_pages) {
ma->flags |= MA_F_USED;
return ma;
}
/* ma is larger than we need, split it. */
ret = kmalloc(sizeof(MArea), GFP_KERNEL);
if (!ret)
return NULL;
ma->pages -= req_pages;
ret->pages = req_pages;
ret->flags = MA_F_USED;
/* @ret is the tail of @ma. */
ret->start = ma->start + ma->pages * PAGE_SIZE;
ret->prev = ma;
ret->next = ma->next;
if (ret->next)
ret->next->prev = ret;
ma->next = ret;
return ret;
}
static MArea *
ma_lookup(unsigned long addr, int node)
{
MArea *ma;
for (ma = &mas[node]; ma; ma = ma->next)
if (ma->start == addr)
return ma;
return NULL;
}
/**
* Merge two free memory areas if they aren't the same.
*/
static MArea *
__ma_merge(MArea *left, MArea *right)
{
if (!MA_FREE(left) || !MA_FREE(right))
return left;
BUG_ON(left->start == right->start);
left->pages += right->pages;
left->next = right->next;
if (right->next)
right->next->prev = left;
kfree(right);
return left;
}
/**
* Free a memory area and merge it with free siblings if possible.
* Never tries to free statically allocated MArea.
*/
static void
__ma_free(MArea *ma)
{
ma->flags &= ~MA_F_USED;
if (ma->prev)
ma = __ma_merge(ma->prev, ma);
if (ma->next)
__ma_merge(ma, ma->next);
}
static void
ma_free(unsigned long addr, int node)
{
MArea *ma;
ma = ma_lookup(addr, node);
if (!ma) {
TDB_ERR("Cannot find memory area for %#lx address at node %d\n",
addr, node);
return;
}
__ma_free(ma);
}
/**
* Map file to reserved set of unswappable pages.
*/
static unsigned long
tempesta_map_file(struct file *file, unsigned long len, int node)
{
MArea *ma;
loff_t off = 0;
unsigned long addr = -ENOMEM;
BUG_ON(len & ~TDB_EXT_MASK);
if (file->f_inode->i_size != len) {
TDB_ERR("Bad file size %lld while expected is %lu\n",
file->f_inode->i_size, len);
return -EBADF;
}
mutex_lock(&map_mtx);
ma = ma_get_best_fit(len, node);
if (!ma) {
TDB_ERR("Cannot allocate %lu pages at node %d\n",
len / PAGE_SIZE, node);
goto err;
}
ma = ma_split(ma, len);
if (!ma)
goto err;
get_file(file);
addr = kernel_read(file, (char *)ma->start, len, &off);
if (addr != len) {
TDB_ERR("Cannot read %lu bytes to addr %p, ret = %ld\n",
len, (void *)ma->start, addr);
fput(file);
__ma_free(ma);
addr = -EIO;
goto err;
}
addr = ma->start;
err:
mutex_unlock(&map_mtx);
return addr;
}
/**
* Synchronize memory mapping with the file.
* Called from process context.
*/
static void
tempesta_unmap_file(struct file *file, unsigned long addr, unsigned long len,
int node)
{
MArea *ma;
loff_t off = 0;
ssize_t r;
mutex_lock(&map_mtx);
ma = ma_lookup(addr, node);
if (!ma) {
TDB_WARN("Cannot sync memory area for %#lx address at node"
" %d\n", addr, node);
goto err;
}
r = kernel_write(file, (void *)ma->start, len, &off);
if (r != len) {
TDB_WARN("Cannot sync mapping %lx of size %lu pages\n",
ma->start, ma->pages);
goto err;
}
err:
fput(file);
ma_free(addr, node);
mutex_unlock(&map_mtx);
}
/**
* Open, mmap and mlock the specified file to be able to read and
* write to it in softirqs.
* Use MAP_SHARED to synchronize the mapping with underlying file.
*
* The function must not be called from softirq!
*/
int
tdb_file_open(TDB *db, unsigned long size)
{
unsigned long ret, addr;
struct file *filp;
struct inode *inode;
filp = filp_open(db->path, O_CREAT | O_RDWR, 0600);
if (IS_ERR(filp)) {
TDB_ERR("Cannot open db file %s\n", db->path);
return PTR_ERR(filp);
}
BUG_ON(!filp || !filp->f_path.dentry);
if (!filp->f_op->fallocate) {
TDB_ERR("TDB requires filesystem with fallocate support\n");
filp_close(filp, NULL);
return -EBADF;
}
/* Allocate continuous extents. */
inode = file_inode(filp);
sb_start_write(inode->i_sb);
ret = filp->f_op->fallocate(filp, 0, 0, size);
sb_end_write(inode->i_sb);
if (ret) {
TDB_ERR("Cannot fallocate file, %ld\n", ret);
filp_close(filp, NULL);
return ret;
}
addr = tempesta_map_file(filp, size, db->node);
if (IS_ERR((void *)addr)) {
TDB_ERR("Cannot map file\n");
filp_close(filp, NULL);
return (int)addr;
}
db->filp = filp;
db->hdr = (TdbHdr *)addr;
file_accessed(filp);
return 0;
}
void
tdb_file_close(TDB *db)
{
if (!db->hdr || !db->hdr->dbsz)
return;
tempesta_unmap_file(db->filp, (unsigned long)db->hdr, db->hdr->dbsz,
db->node);
filp_close(db->filp, NULL);
}
int
tdb_init_mappings(void)
{
int node;
TempestaMapping *tm;
for_each_node_with_cpus(node) {
if (tempesta_get_mapping(node, &tm)) {
TDB_ERR("Cannot get mapping for node %d\n", node);
return -ENOMEM;
}
mas[node].start = tm->addr;
mas[node].pages = tm->pages;
}
return 0;
}