-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_inline.c
More file actions
346 lines (312 loc) · 13.1 KB
/
file_inline.c
File metadata and controls
346 lines (312 loc) · 13.1 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// SPDX-License-Identifier: GPL-2.0-only
/*
* beamfs - File operations for BEAMFS_DATA_PROTECTION_UNIVERSAL_INLINE (v2)
*
* Per-block Reed-Solomon FEC on user data: each 4096-byte disk block
* holds 16 RS(255,239) shortened subblocks (3824 user bytes + 256 parity
* + 16 pad bytes).
*
* This file implements the data path for scheme=2 (UNIVERSAL_INLINE).
* The legacy iomap-based path in file.c is preserved for scheme=5
* (INODE_UNIVERSAL); the dispatch happens in inode.c / namei.c when
* setting i_fop and a_ops based on sbi->s_scheme.
*
* Threat model: validates against RadFI v0.1.0+ (single-bit flip in
* bio_vec page payload during submit_bio_noacct). MIL-STD-883 SEE
* coverage: up to 128 bytes corruption per disk block (8 byte symbols
* per subblock * 16 subblocks).
*
* Author: roastercode - Aurelien DESBRIERES <aurelien@hackers.camp>
*/
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/buffer_head.h>
#include <linux/uio.h>
#include <linux/writeback.h>
#include "beamfs.h"
/* ------------------------------------------------------------------------- */
/* Forward declarations of v2 ops (stubs, populated in subsequent stages) */
/* ------------------------------------------------------------------------- */
static int beamfs_inline_read_folio(struct file *file,
struct folio *folio);
static int beamfs_inline_writepages(struct address_space *mapping,
struct writeback_control *wbc);
static void beamfs_inline_readahead(struct readahead_control *rac);
static int beamfs_inline_write_begin(const struct kiocb *iocb,
struct address_space *mapping,
loff_t pos, unsigned int len,
struct folio **foliop, void **fsdata);
static int beamfs_inline_write_end(const struct kiocb *iocb,
struct address_space *mapping,
loff_t pos, unsigned int len,
unsigned int copied,
struct folio *folio, void *fsdata);
static ssize_t beamfs_inline_file_write_iter(struct kiocb *iocb,
struct iov_iter *from);
/* ------------------------------------------------------------------------- */
/* Block-mapping helpers (v2 INLINE) */
/* */
/* These mirror the layout used by beamfs_iomap_begin() in file.c (legacy */
/* iomap path, scheme=5 INODE_UNIVERSAL): direct blocks 0..11 in */
/* fi->i_direct[], single indirect via fi->i_indirect (512 entries) for */
/* iblocks 12..523. Maximum mapped iblock in v1 layout: 524 -> ~2.0 MiB of */
/* logical user data per file at 3824 user bytes per disk block. */
/* */
/* Pure lookup, no allocation. Used by read_folio (4b2.2) and as the read */
/* leg of write_begin (4b3) RMW. The allocating variant lives below */
/* (added in 4b3). */
/* */
/* Returns: */
/* 0 + *phys_out = block number (mapped) */
/* 0 + *phys_out = 0 (HOLE: not allocated yet) */
/* <0 on error (-EIO indirect read fail, */
/* -EOPNOTSUPP beyond v1 capacity, */
/* -EINVAL on null phys_out) */
/* ------------------------------------------------------------------------- */
static int beamfs_inline_lookup_phys(struct inode *inode, u64 iblock_logical,
u64 *phys_out)
{
struct beamfs_inode_info *fi = BEAMFS_I(inode);
struct super_block *sb = inode->i_sb;
struct buffer_head *ibh;
__le64 *ptrs;
u64 indirect_blk;
u64 indirect_slot;
u64 phys;
if (!phys_out)
return -EINVAL;
*phys_out = 0;
if (iblock_logical < BEAMFS_DIRECT_BLOCKS) {
*phys_out = le64_to_cpu(fi->i_direct[iblock_logical]);
return 0;
}
if (iblock_logical < BEAMFS_DIRECT_BLOCKS + BEAMFS_INDIRECT_PTRS) {
indirect_slot = iblock_logical - BEAMFS_DIRECT_BLOCKS;
indirect_blk = le64_to_cpu(fi->i_indirect);
if (!indirect_blk)
return 0; /* HOLE: indirect block not yet allocated */
ibh = sb_bread(sb, indirect_blk);
if (!ibh) {
pr_err_ratelimited("beamfs/inline: failed to read indirect block %llu\n",
(unsigned long long)indirect_blk);
return -EIO;
}
ptrs = (__le64 *)ibh->b_data;
phys = le64_to_cpu(ptrs[indirect_slot]);
brelse(ibh);
*phys_out = phys;
return 0;
}
pr_err_ratelimited("beamfs/inline: iblock %llu beyond v1 indirect capacity\n",
(unsigned long long)iblock_logical);
return -EOPNOTSUPP;
}
/* ------------------------------------------------------------------------- */
/* read_folio (v2 INLINE) -- per-block RS(255,239) FEC on data blocks. */
/* */
/* Each disk block is laid out as 16 interleaved subblocks of 255 bytes */
/* (239 user data || 16 parity), followed by 16 bytes of zero pad. We: */
/* 1) look up the physical block for folio->index */
/* 2) handle HOLE (zero-fill, return 0) */
/* 3) sb_bread the disk block */
/* 4) RS-decode all 16 subblocks in place (corrects up to 8 byte symbols */
/* per subblock, i.e. 128 bytes per disk block) */
/* 5) on uncorrectable: -EIO, no folio uptodate marking */
/* 6) on success: gather 16 * 239 bytes into the folio, zero the pad zone */
/* 7) if any subblock was corrected: write back the repaired disk block */
/* synchronously (durable autonomic repair, mirrors alloc.c bitmap path)*/
/* */
/* Single-page folios only (mapping_set_folio_order_range(0,0) in inode */
/* setup). MIL-STD-883 SEE coverage: validates beamfs resistance to RadFI */
/* single-bit and multi-byte payload corruption injected at submit_bio. */
/* ------------------------------------------------------------------------- */
static int beamfs_inline_read_folio(struct file *file, struct folio *folio)
{
struct inode *inode = folio->mapping->host;
struct super_block *sb = inode->i_sb;
struct buffer_head *bh = NULL;
u64 iblock_logical;
u64 phys = 0;
int rs_results[BEAMFS_DATA_INLINE_SUBBLOCKS];
int rs_positions[BEAMFS_DATA_INLINE_SUBBLOCKS *
(BEAMFS_RS_PARITY / 2)];
bool corrected = false;
bool uncorrectable = false;
u8 *dst;
unsigned int i;
int ret;
/* Single-page folios are guaranteed by mapping_set_folio_order_range. */
if (WARN_ON_ONCE(folio_size(folio) != BEAMFS_BLOCK_SIZE)) {
ret = -EIO;
goto out_unlock;
}
iblock_logical = folio->index;
ret = beamfs_inline_lookup_phys(inode, iblock_logical, &phys);
if (ret < 0)
goto out_unlock;
if (phys == 0) {
/* HOLE: zero the folio, mark uptodate, done. */
dst = kmap_local_folio(folio, 0);
memset(dst, 0, BEAMFS_BLOCK_SIZE);
flush_dcache_folio(folio);
kunmap_local(dst);
folio_end_read(folio, true);
return 0;
}
bh = sb_bread(sb, phys);
if (!bh) {
pr_err_ratelimited("beamfs/inline: sb_bread failed phys=%llu\n",
(unsigned long long)phys);
ret = -EIO;
goto out_unlock;
}
/*
* Decode 16 RS(255,239) shortened subblocks in place. Same layout
* as the bitmap path in alloc.c: data and parity are interleaved
* with stride BEAMFS_SUBBLOCK_TOTAL (255), parity offset 239.
*/
beamfs_rs_decode_region(
(u8 *)bh->b_data, BEAMFS_SUBBLOCK_TOTAL,
(u8 *)bh->b_data + BEAMFS_SUBBLOCK_DATA, BEAMFS_SUBBLOCK_TOTAL,
BEAMFS_SUBBLOCK_DATA, BEAMFS_DATA_INLINE_SUBBLOCKS,
rs_results,
rs_positions,
BEAMFS_RS_PARITY / 2);
for (i = 0; i < BEAMFS_DATA_INLINE_SUBBLOCKS; i++) {
int rc = rs_results[i];
if (rc < 0) {
/*
* Journal the uncorrectable event before raising the
* error: forensic record takes priority over the alert.
* See Documentation/format-v4.md section 6.5.
*/
beamfs_log_rs_event(sb,
(u64)phys * BEAMFS_DATA_INLINE_SUBBLOCKS + i,
NULL, 0,
BEAMFS_SUBBLOCK_DATA);
pr_err_ratelimited("beamfs/inline: ino=%lu iblock=%llu subblock=%u uncorrectable\n",
inode->i_ino,
(unsigned long long)iblock_logical,
i);
uncorrectable = true;
} else if (rc > 0) {
unsigned int np = (unsigned int)rc;
int *pos = rs_positions +
(size_t)i * (BEAMFS_RS_PARITY / 2);
if (np > BEAMFS_RS_PARITY / 2)
np = BEAMFS_RS_PARITY / 2;
pr_warn_ratelimited("beamfs/inline: ino=%lu iblock=%llu subblock=%u: %d symbol(s) corrected\n",
inode->i_ino,
(unsigned long long)iblock_logical,
i, rc);
beamfs_log_rs_event(sb,
(u64)phys * BEAMFS_DATA_INLINE_SUBBLOCKS + i,
pos, np,
BEAMFS_SUBBLOCK_DATA);
corrected = true;
}
}
if (uncorrectable) {
ret = -EIO;
goto out_brelse;
}
/*
* Gather: 16 segments of 239 user bytes from the buffer head into
* the folio. Skip 16 parity bytes between subblocks. Zero the pad
* zone (3824..4096) in the folio.
*/
dst = kmap_local_folio(folio, 0);
for (i = 0; i < BEAMFS_DATA_INLINE_SUBBLOCKS; i++) {
memcpy(dst + (size_t)i * BEAMFS_SUBBLOCK_DATA,
(u8 *)bh->b_data + (size_t)i * BEAMFS_SUBBLOCK_TOTAL,
BEAMFS_SUBBLOCK_DATA);
}
memset(dst + BEAMFS_DATA_INLINE_BYTES, 0, BEAMFS_DATA_INLINE_PAD +
(BEAMFS_BLOCK_SIZE - BEAMFS_DATA_INLINE_TOTAL -
BEAMFS_DATA_INLINE_PAD));
flush_dcache_folio(folio);
kunmap_local(dst);
/*
* Durable autonomic repair: if RS corrected any subblock, write the
* repaired disk block back synchronously so the on-disk image is
* healed before the next read. Same pattern as the bitmap recovery
* path in alloc.c.
*/
if (corrected) {
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
}
brelse(bh);
folio_end_read(folio, true);
return 0;
out_brelse:
brelse(bh);
out_unlock:
folio_unlock(folio);
return ret;
}
/* ------------------------------------------------------------------------- */
/* writepages -- to be implemented in stage 4b4 */
/* ------------------------------------------------------------------------- */
static int beamfs_inline_writepages(struct address_space *mapping,
struct writeback_control *wbc)
{
pr_warn_ratelimited("beamfs/inline: writepages not implemented yet\n");
return 0;
}
/* ------------------------------------------------------------------------- */
/* readahead -- to be implemented in stage 4b5 (initial: per-folio loop) */
/* ------------------------------------------------------------------------- */
static void beamfs_inline_readahead(struct readahead_control *rac)
{
struct folio *folio;
while ((folio = readahead_folio(rac)))
beamfs_inline_read_folio(rac->file, folio);
}
/* ------------------------------------------------------------------------- */
/* write_begin / write_end -- to be implemented in stage 4b3 */
/* ------------------------------------------------------------------------- */
static int beamfs_inline_write_begin(const struct kiocb *iocb,
struct address_space *mapping,
loff_t pos, unsigned int len,
struct folio **foliop, void **fsdata)
{
pr_warn_ratelimited("beamfs/inline: write_begin not implemented yet\n");
return -EOPNOTSUPP;
}
static int beamfs_inline_write_end(const struct kiocb *iocb,
struct address_space *mapping,
loff_t pos, unsigned int len,
unsigned int copied,
struct folio *folio, void *fsdata)
{
pr_warn_ratelimited("beamfs/inline: write_end not implemented yet\n");
return -EOPNOTSUPP;
}
/* ------------------------------------------------------------------------- */
/* write_iter entry point */
/* ------------------------------------------------------------------------- */
static ssize_t beamfs_inline_file_write_iter(struct kiocb *iocb,
struct iov_iter *from)
{
return generic_perform_write(iocb, from);
}
/* ------------------------------------------------------------------------- */
/* Public ops structures */
/* ------------------------------------------------------------------------- */
const struct address_space_operations beamfs_inline_aops = {
.read_folio = beamfs_inline_read_folio,
.writepages = beamfs_inline_writepages,
.readahead = beamfs_inline_readahead,
.write_begin = beamfs_inline_write_begin,
.write_end = beamfs_inline_write_end,
.dirty_folio = filemap_dirty_folio,
};
const struct file_operations beamfs_inline_file_operations = {
.llseek = generic_file_llseek,
.read_iter = generic_file_read_iter,
.write_iter = beamfs_inline_file_write_iter,
.fsync = generic_file_fsync,
.splice_read = filemap_splice_read,
};