Repository navigation
Expand file tree
/
Copy pathfile.c
More file actions
549 lines (507 loc) · 14.4 KB
/
Copy pathfile.c
File metadata and controls
549 lines (507 loc) · 14.4 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// SPDX-License-Identifier: GPL-2.0-only
/*
* beamfs - File operations
* Author: roastercode - Aurelien DESBRIERES <aurelien@hackers.camp>
*/
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/iomap.h>
#include <linux/pagemap.h>
#include <linux/buffer_head.h>
#include <linux/fiemap.h>
#include "beamfs.h"
/* Forward declaration - defined after iomap_ops */
static ssize_t beamfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from);
/*
* generic_file_fsync() was removed along with sync_mapping_buffers() and
* the i_private_list mechanism it drove. Metadata buffer_heads owned by
* an inode now live in a per-inode mapping_metadata_bhs list, flushed
* through mmb_fsync(); ext2_fsync() has the same shape.
*/
static int beamfs_fsync(struct file *file, loff_t start, loff_t end,
int datasync)
{
return simple_fsync(file, start, end, datasync);
}
const struct file_operations beamfs_file_operations = {
.llseek = generic_file_llseek,
.read_iter = generic_file_read_iter,
.write_iter = beamfs_file_write_iter,
.mmap = generic_file_mmap,
.fsync = beamfs_fsync,
.splice_read = filemap_splice_read,
};
const struct inode_operations beamfs_file_inode_operations = {
.getattr = simple_getattr,
.fiemap = beamfs_fiemap, /* S2.2: file-precise bench targeting */
};
/*
* beamfs_iomap_begin -- map a file range to disk blocks for iomap.
* Handles read (no allocation) and write (allocate on demand).
* Supports direct blocks (iblock 0..11) and single indirect
* (iblock 12..523, covering up to ~2 MiB per file).
*/
static int beamfs_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
unsigned int flags, struct iomap *iomap,
struct iomap *srcmap)
{
struct beamfs_inode_info *fi = BEAMFS_I(inode);
struct super_block *sb = inode->i_sb;
u64 iblock = pos >> BEAMFS_BLOCK_SHIFT;
u64 new_block;
u64 phys;
iomap->offset = iblock << BEAMFS_BLOCK_SHIFT;
iomap->length = BEAMFS_BLOCK_SIZE;
iomap->bdev = sb->s_bdev;
iomap->flags = 0;
if (iblock < BEAMFS_DIRECT_BLOCKS) {
/* --- Direct block --- */
phys = le64_to_cpu(fi->i_direct[iblock]);
if (phys) {
iomap->type = IOMAP_MAPPED;
iomap->addr = phys << BEAMFS_BLOCK_SHIFT;
return 0;
}
if (!(flags & IOMAP_WRITE)) {
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
new_block = beamfs_alloc_block(sb, inode);
if (!new_block) {
pr_err("beamfs: iomap: no free blocks\n");
return -ENOSPC;
}
fi->i_direct[iblock] = cpu_to_le64(new_block);
mark_inode_dirty(inode);
iomap->type = IOMAP_MAPPED;
iomap->addr = new_block << BEAMFS_BLOCK_SHIFT;
return 0;
}
if (iblock < BEAMFS_DIRECT_BLOCKS + BEAMFS_INDIRECT_PTRS) {
/* --- Single indirect block --- */
u64 indirect_slot = iblock - BEAMFS_DIRECT_BLOCKS;
u64 indirect_blk = le64_to_cpu(fi->i_indirect);
struct buffer_head *ibh;
__le64 *ptrs;
if (!indirect_blk) {
if (!(flags & IOMAP_WRITE)) {
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
indirect_blk = beamfs_alloc_block(sb, inode);
if (!indirect_blk) {
pr_err("beamfs: iomap: no free blocks for indirect\n");
return -ENOSPC;
}
ibh = sb_getblk(sb, indirect_blk);
if (!ibh) {
beamfs_free_block(sb, indirect_blk, inode);
return -EIO;
}
lock_buffer(ibh);
memset(ibh->b_data, 0, BEAMFS_BLOCK_SIZE);
set_buffer_uptodate(ibh);
unlock_buffer(ibh);
mark_buffer_dirty(ibh);
brelse(ibh);
fi->i_indirect = cpu_to_le64(indirect_blk);
mark_inode_dirty(inode);
}
ibh = sb_bread(sb, indirect_blk);
if (!ibh)
return -EIO;
ptrs = (__le64 *)ibh->b_data;
phys = le64_to_cpu(ptrs[indirect_slot]);
if (phys) {
brelse(ibh);
iomap->type = IOMAP_MAPPED;
iomap->addr = phys << BEAMFS_BLOCK_SHIFT;
return 0;
}
if (!(flags & IOMAP_WRITE)) {
brelse(ibh);
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
new_block = beamfs_alloc_block(sb, inode);
if (!new_block) {
brelse(ibh);
pr_err("beamfs: iomap: no free blocks\n");
return -ENOSPC;
}
ptrs[indirect_slot] = cpu_to_le64(new_block);
mark_buffer_dirty(ibh);
brelse(ibh);
iomap->type = IOMAP_MAPPED;
iomap->addr = new_block << BEAMFS_BLOCK_SHIFT;
return 0;
}
if (iblock < BEAMFS_MAX_IBLOCK_DINDIRECT) {
/* --- Double indirect block (scheme=5 iomap path) --- */
u64 didx = iblock - BEAMFS_MAX_IBLOCK_INDIRECT;
u64 l1_slot = didx / BEAMFS_INDIRECT_PTRS;
u64 l2_slot = didx % BEAMFS_INDIRECT_PTRS;
u64 dindirect_blk, l1_blk;
struct buffer_head *ibh, *l1bh;
__le64 *ptrs;
dindirect_blk = le64_to_cpu(fi->i_dindirect);
if (!dindirect_blk) {
if (!(flags & IOMAP_WRITE)) {
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
dindirect_blk = beamfs_alloc_block(sb, inode);
if (!dindirect_blk) {
pr_err("beamfs: iomap: no free blocks for dindirect\n");
return -ENOSPC;
}
ibh = sb_getblk(sb, dindirect_blk);
if (!ibh) {
beamfs_free_block(sb, dindirect_blk, inode);
return -EIO;
}
lock_buffer(ibh);
memset(ibh->b_data, 0, BEAMFS_BLOCK_SIZE);
set_buffer_uptodate(ibh);
unlock_buffer(ibh);
mark_buffer_dirty(ibh);
brelse(ibh);
fi->i_dindirect = cpu_to_le64(dindirect_blk);
mark_inode_dirty(inode);
}
ibh = sb_bread(sb, dindirect_blk);
if (!ibh)
return -EIO;
ptrs = (__le64 *)ibh->b_data;
l1_blk = le64_to_cpu(ptrs[l1_slot]);
if (!l1_blk) {
if (!(flags & IOMAP_WRITE)) {
brelse(ibh);
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
l1_blk = beamfs_alloc_block(sb, inode);
if (!l1_blk) {
brelse(ibh);
pr_err("beamfs: iomap: no free blocks for dindirect L1\n");
return -ENOSPC;
}
l1bh = sb_getblk(sb, l1_blk);
if (!l1bh) {
beamfs_free_block(sb, l1_blk, inode);
brelse(ibh);
return -EIO;
}
lock_buffer(l1bh);
memset(l1bh->b_data, 0, BEAMFS_BLOCK_SIZE);
set_buffer_uptodate(l1bh);
unlock_buffer(l1bh);
mark_buffer_dirty(l1bh);
brelse(l1bh);
ptrs[l1_slot] = cpu_to_le64(l1_blk);
mark_buffer_dirty(ibh);
}
brelse(ibh);
l1bh = sb_bread(sb, l1_blk);
if (!l1bh)
return -EIO;
ptrs = (__le64 *)l1bh->b_data;
phys = le64_to_cpu(ptrs[l2_slot]);
if (phys) {
brelse(l1bh);
iomap->type = IOMAP_MAPPED;
iomap->addr = phys << BEAMFS_BLOCK_SHIFT;
return 0;
}
if (!(flags & IOMAP_WRITE)) {
brelse(l1bh);
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
new_block = beamfs_alloc_block(sb, inode);
if (!new_block) {
brelse(l1bh);
pr_err("beamfs: iomap: no free blocks (dindirect data)\n");
return -ENOSPC;
}
ptrs[l2_slot] = cpu_to_le64(new_block);
mark_buffer_dirty(l1bh);
brelse(l1bh);
iomap->type = IOMAP_MAPPED;
iomap->addr = new_block << BEAMFS_BLOCK_SHIFT;
return 0;
}
if (iblock < BEAMFS_MAX_IBLOCK_TINDIRECT) {
/* --- Triple indirect block (scheme=5 iomap path) --- */
u64 tidx = iblock - BEAMFS_MAX_IBLOCK_DINDIRECT;
u64 l1_slot = tidx / (BEAMFS_INDIRECT_PTRS * BEAMFS_INDIRECT_PTRS);
u64 l2_slot = (tidx / BEAMFS_INDIRECT_PTRS) % BEAMFS_INDIRECT_PTRS;
u64 l3_slot = tidx % BEAMFS_INDIRECT_PTRS;
u64 tindirect_blk, l1_blk, l2_blk;
struct buffer_head *ibh, *l1bh, *l2bh;
__le64 *ptrs;
tindirect_blk = le64_to_cpu(fi->i_tindirect);
if (!tindirect_blk) {
if (!(flags & IOMAP_WRITE)) {
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
tindirect_blk = beamfs_alloc_block(sb, inode);
if (!tindirect_blk) {
pr_err("beamfs: iomap: no free blocks for tindirect\n");
return -ENOSPC;
}
ibh = sb_getblk(sb, tindirect_blk);
if (!ibh) {
beamfs_free_block(sb, tindirect_blk, inode);
return -EIO;
}
lock_buffer(ibh);
memset(ibh->b_data, 0, BEAMFS_BLOCK_SIZE);
set_buffer_uptodate(ibh);
unlock_buffer(ibh);
mark_buffer_dirty(ibh);
brelse(ibh);
fi->i_tindirect = cpu_to_le64(tindirect_blk);
mark_inode_dirty(inode);
}
ibh = sb_bread(sb, tindirect_blk);
if (!ibh)
return -EIO;
ptrs = (__le64 *)ibh->b_data;
l1_blk = le64_to_cpu(ptrs[l1_slot]);
if (!l1_blk) {
if (!(flags & IOMAP_WRITE)) {
brelse(ibh);
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
l1_blk = beamfs_alloc_block(sb, inode);
if (!l1_blk) {
brelse(ibh);
pr_err("beamfs: iomap: no free blocks for tindirect L1\n");
return -ENOSPC;
}
l1bh = sb_getblk(sb, l1_blk);
if (!l1bh) {
beamfs_free_block(sb, l1_blk, inode);
brelse(ibh);
return -EIO;
}
lock_buffer(l1bh);
memset(l1bh->b_data, 0, BEAMFS_BLOCK_SIZE);
set_buffer_uptodate(l1bh);
unlock_buffer(l1bh);
mark_buffer_dirty(l1bh);
brelse(l1bh);
ptrs[l1_slot] = cpu_to_le64(l1_blk);
mark_buffer_dirty(ibh);
}
brelse(ibh);
l1bh = sb_bread(sb, l1_blk);
if (!l1bh)
return -EIO;
ptrs = (__le64 *)l1bh->b_data;
l2_blk = le64_to_cpu(ptrs[l2_slot]);
if (!l2_blk) {
if (!(flags & IOMAP_WRITE)) {
brelse(l1bh);
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
l2_blk = beamfs_alloc_block(sb, inode);
if (!l2_blk) {
brelse(l1bh);
pr_err("beamfs: iomap: no free blocks for tindirect L2\n");
return -ENOSPC;
}
l2bh = sb_getblk(sb, l2_blk);
if (!l2bh) {
beamfs_free_block(sb, l2_blk, inode);
brelse(l1bh);
return -EIO;
}
lock_buffer(l2bh);
memset(l2bh->b_data, 0, BEAMFS_BLOCK_SIZE);
set_buffer_uptodate(l2bh);
unlock_buffer(l2bh);
mark_buffer_dirty(l2bh);
brelse(l2bh);
ptrs[l2_slot] = cpu_to_le64(l2_blk);
mark_buffer_dirty(l1bh);
}
brelse(l1bh);
l2bh = sb_bread(sb, l2_blk);
if (!l2bh)
return -EIO;
ptrs = (__le64 *)l2bh->b_data;
phys = le64_to_cpu(ptrs[l3_slot]);
if (phys) {
brelse(l2bh);
iomap->type = IOMAP_MAPPED;
iomap->addr = phys << BEAMFS_BLOCK_SHIFT;
return 0;
}
if (!(flags & IOMAP_WRITE)) {
brelse(l2bh);
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
return 0;
}
new_block = beamfs_alloc_block(sb, inode);
if (!new_block) {
brelse(l2bh);
pr_err("beamfs: iomap: no free blocks (tindirect data)\n");
return -ENOSPC;
}
ptrs[l3_slot] = cpu_to_le64(new_block);
mark_buffer_dirty(l2bh);
brelse(l2bh);
iomap->type = IOMAP_MAPPED;
iomap->addr = new_block << BEAMFS_BLOCK_SHIFT;
return 0;
}
/* Beyond triple indirect: not supported */
pr_err_ratelimited("beamfs: iomap: offset beyond tindirect blocks\n");
return -EOPNOTSUPP;
}
static int beamfs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
ssize_t written, unsigned int flags,
struct iomap *iomap)
{
return 0;
}
static const struct iomap_ops beamfs_iomap_ops = {
.iomap_begin = beamfs_iomap_begin,
.iomap_end = beamfs_iomap_end,
};
/*
* S2.2: fiemap support for file-precise bench targeting (TODO.md l.1979).
* Thin wrapper over iomap_fiemap() reusing beamfs_iomap_ops. Pattern
* follows fs/ext2/inode.c::ext2_fiemap() (kernel 7.0.x). Required by
* userspace filefrag(8) to expose the file -> physical block mapping
* for beamfs-bench file-level RS-FEC validation.
*
* Shared by both schemes (UNIVERSAL_INLINE and INODE_UNIVERSAL) since
* both use the same direct[12] + indirect[512] block layout, handled
* uniformly by beamfs_iomap_begin().
*/
int beamfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
u64 start, u64 len)
{
int ret;
loff_t i_size;
inode_lock(inode);
i_size = i_size_read(inode);
/*
* iomap_fiemap() returns -EINVAL for len == 0. Trim the request to
* the file size but never below 1 to keep the call valid for empty
* files (where it will simply return zero extents).
*/
if (i_size == 0)
i_size = 1;
len = min_t(u64, len, i_size);
ret = iomap_fiemap(inode, fieinfo, start, len, &beamfs_iomap_ops);
inode_unlock(inode);
return ret;
}
/*
* Write path - beamfs_iomap_write_ops
* get_folio/put_folio use generic helpers (no journaling required).
*/
static struct folio *beamfs_iomap_get_folio(struct iomap_iter *iter,
loff_t pos, unsigned int len)
{
return iomap_get_folio(iter, pos, len);
}
static void beamfs_iomap_put_folio(struct inode *inode, loff_t pos,
unsigned int copied, struct folio *folio)
{
folio_unlock(folio);
folio_put(folio);
}
static const struct iomap_write_ops beamfs_iomap_write_ops = {
.get_folio = beamfs_iomap_get_folio,
.put_folio = beamfs_iomap_put_folio,
};
static ssize_t beamfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
return iomap_file_buffered_write(iocb, from, &beamfs_iomap_ops,
&beamfs_iomap_write_ops, NULL);
}
/*
* Writeback path - beamfs_writeback_ops
*/
static ssize_t beamfs_writeback_range(struct iomap_writepage_ctx *wpc,
struct folio *folio, u64 offset,
unsigned int len, u64 end_pos)
{
if ((loff_t)offset < wpc->iomap.offset ||
(loff_t)offset >= wpc->iomap.offset + (loff_t)wpc->iomap.length) {
int ret;
memset(&wpc->iomap, 0, sizeof(wpc->iomap));
ret = beamfs_iomap_begin(wpc->inode,
offset, INT_MAX, 0,
&wpc->iomap, NULL);
if (ret)
return ret;
}
return iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
}
static const struct iomap_writeback_ops beamfs_writeback_ops = {
.writeback_range = beamfs_writeback_range,
.writeback_submit = iomap_ioend_writeback_submit,
};
static int beamfs_writepages(struct address_space *mapping,
struct writeback_control *wbc)
{
struct iomap_writepage_ctx wpc = {
.inode = mapping->host,
.wbc = wbc,
.ops = &beamfs_writeback_ops,
};
return iomap_writepages(&wpc);
}
/*
* Read path - uses iomap_bio_read_ops (kernel-provided)
*/
static int beamfs_read_folio(struct file *file, struct folio *folio)
{
struct iomap_read_folio_ctx ctx = {
.ops = &iomap_bio_read_ops,
.cur_folio = folio,
};
iomap_read_folio(&beamfs_iomap_ops, &ctx, NULL);
return 0;
}
static void beamfs_readahead(struct readahead_control *rac)
{
struct iomap_read_folio_ctx ctx = {
.ops = &iomap_bio_read_ops,
.rac = rac,
};
iomap_readahead(&beamfs_iomap_ops, &ctx, NULL);
}
static sector_t beamfs_bmap(struct address_space *mapping, sector_t block)
{
return iomap_bmap(mapping, block, &beamfs_iomap_ops);
}
const struct address_space_operations beamfs_aops = {
.read_folio = beamfs_read_folio,
.readahead = beamfs_readahead,
.writepages = beamfs_writepages,
.bmap = beamfs_bmap,
.dirty_folio = iomap_dirty_folio,
.invalidate_folio = iomap_invalidate_folio,
.release_folio = iomap_release_folio,
.migrate_folio = filemap_migrate_folio,
};