-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamei.c
More file actions
626 lines (522 loc) · 17.3 KB
/
Copy pathnamei.c
File metadata and controls
626 lines (522 loc) · 17.3 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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// SPDX-License-Identifier: GPL-2.0-only
/*
* beamfs - Filename / directory entry operations
* Author: Aurélien DESBRIERES <aurelien@hackers.camp>
*
* Implements: create, mkdir, unlink, rmdir, link, rename
*/
#include <linux/fs.h>
#include <linux/buffer_head.h>
#include <linux/pagemap.h>
#include <linux/slab.h>
#include <linux/time.h>
#include "beamfs.h"
/* ------------------------------------------------------------------ */
/* Helper: write a raw beamfs_inode to disk */
/* ------------------------------------------------------------------ */
int beamfs_write_inode_raw(struct inode *inode)
{
struct super_block *sb = inode->i_sb;
struct beamfs_sb_info *sbi = BEAMFS_SB(sb);
struct beamfs_inode_info *fi = BEAMFS_I(inode);
struct beamfs_inode *raw;
struct buffer_head *bh;
unsigned long inodes_per_block;
unsigned long block, offset;
inodes_per_block = BEAMFS_BLOCK_SIZE / sizeof(struct beamfs_inode);
block = le64_to_cpu(sbi->s_beamfs_sb->s_inode_table_blk)
+ (inode->i_ino - 1) / inodes_per_block;
offset = (inode->i_ino - 1) % inodes_per_block;
bh = sb_bread(sb, block);
if (!bh)
return -EIO;
raw = (struct beamfs_inode *)bh->b_data + offset;
raw->i_mode = cpu_to_le16(inode->i_mode);
raw->i_uid = cpu_to_le32(i_uid_read(inode));
raw->i_gid = cpu_to_le32(i_gid_read(inode));
raw->i_nlink = cpu_to_le16(inode->i_nlink);
raw->i_size = cpu_to_le64(inode->i_size);
raw->i_atime = cpu_to_le64(inode_get_atime_sec(inode) * NSEC_PER_SEC
+ inode_get_atime_nsec(inode));
raw->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode) * NSEC_PER_SEC
+ inode_get_mtime_nsec(inode));
raw->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode) * NSEC_PER_SEC
+ inode_get_ctime_nsec(inode));
raw->i_flags = cpu_to_le32(fi->i_flags);
memcpy(raw->i_direct, fi->i_direct, sizeof(fi->i_direct));
raw->i_indirect = fi->i_indirect;
raw->i_dindirect = fi->i_dindirect;
raw->i_tindirect = fi->i_tindirect;
raw->i_crc32 = beamfs_crc32(raw,
offsetof(struct beamfs_inode, i_crc32));
/*
* Compute RS parity over the first BEAMFS_INODE_RS_DATA bytes
* (everything up to i_reserved, including i_crc32). Parity goes
* into i_reserved[0..15]; i_reserved[16..83] is forced to zero so
* the layout is deterministic and any non-zero byte there at read
* time signals a tampered inode.
*
* Under BEAMFS_DATA_PROTECTION_INODE_UNIVERSAL this parity is the
* authoritative correction record for the inode. mkfs.beamfs writes
* the equivalent parity on the root inode at format time; the
* kernel maintains it on every subsequent inode write.
*/
memset(&raw->i_reserved[BEAMFS_RS_PARITY], 0,
sizeof(raw->i_reserved) - BEAMFS_RS_PARITY);
beamfs_rs_encode((u8 *)raw, BEAMFS_INODE_RS_DATA, raw->i_reserved);
mark_buffer_dirty(bh);
brelse(bh);
return 0;
}
/* ------------------------------------------------------------------ */
/* Helper: add a directory entry to a directory inode */
/* ------------------------------------------------------------------ */
static int beamfs_add_dirent(struct inode *dir, const struct qstr *name,
u64 ino, unsigned int file_type)
{
struct super_block *sb = dir->i_sb;
struct beamfs_inode_info *fi = BEAMFS_I(dir);
struct beamfs_dir_entry *de;
struct buffer_head *bh;
unsigned int offset;
u64 block_no;
int i;
/* Look for space in existing direct blocks */
for (i = 0; i < BEAMFS_DIRECT_BLOCKS; i++) {
block_no = le64_to_cpu(fi->i_direct[i]);
if (!block_no)
break;
bh = sb_bread(sb, block_no);
if (!bh)
return -EIO;
offset = 0;
while (offset + sizeof(*de) <= BEAMFS_BLOCK_SIZE) {
de = (struct beamfs_dir_entry *)(bh->b_data + offset);
/*
* Free slot: d_ino == 0. Includes never-used trailing
* slots and slots freed by beamfs_del_dirent. We must
* scan the whole block to find a free slot, even past
* deleted entries; otherwise blocks fill up wastefully
* and ENOSPC strikes early.
*/
if (!de->d_ino) {
de->d_ino = cpu_to_le64(ino);
de->d_name_len = name->len;
de->d_file_type = file_type;
de->d_rec_len = cpu_to_le16(
sizeof(struct beamfs_dir_entry));
memcpy(de->d_name, name->name, name->len);
de->d_name[name->len] = '\0';
mark_buffer_dirty(bh);
brelse(bh);
inode_set_mtime_to_ts(dir,
current_time(dir));
mark_inode_dirty(dir);
return 0;
}
offset += sizeof(struct beamfs_dir_entry);
}
brelse(bh);
}
/* Need a new block */
if (i >= BEAMFS_DIRECT_BLOCKS)
return -ENOSPC;
block_no = beamfs_alloc_block(sb);
if (!block_no)
return -ENOSPC;
bh = sb_bread(sb, block_no);
if (!bh) {
beamfs_free_block(sb, block_no);
return -EIO;
}
memset(bh->b_data, 0, BEAMFS_BLOCK_SIZE);
de = (struct beamfs_dir_entry *)bh->b_data;
de->d_ino = cpu_to_le64(ino);
de->d_name_len = name->len;
de->d_file_type = file_type;
de->d_rec_len = cpu_to_le16(sizeof(struct beamfs_dir_entry));
memcpy(de->d_name, name->name, name->len);
de->d_name[name->len] = '\0';
mark_buffer_dirty(bh);
brelse(bh);
fi->i_direct[i] = cpu_to_le64(block_no);
dir->i_size += BEAMFS_BLOCK_SIZE;
inode_set_mtime_to_ts(dir, current_time(dir));
mark_inode_dirty(dir);
return 0;
}
/* ------------------------------------------------------------------ */
/* Helper: remove a directory entry from a directory */
/* ------------------------------------------------------------------ */
static int beamfs_del_dirent(struct inode *dir, const struct qstr *name)
{
struct super_block *sb = dir->i_sb;
struct beamfs_inode_info *fi = BEAMFS_I(dir);
struct beamfs_dir_entry *de;
struct buffer_head *bh;
unsigned int offset;
u64 block_no;
int i;
for (i = 0; i < BEAMFS_DIRECT_BLOCKS; i++) {
block_no = le64_to_cpu(fi->i_direct[i]);
if (!block_no)
break;
bh = sb_bread(sb, block_no);
if (!bh)
return -EIO;
offset = 0;
while (offset + sizeof(*de) <= BEAMFS_BLOCK_SIZE) {
de = (struct beamfs_dir_entry *)(bh->b_data + offset);
/*
* Match target by d_ino != 0 + name compare. Must scan
* the whole block: a previous unlink may have left a
* hole (d_ino == 0) before our target.
*/
if (de->d_ino &&
de->d_name_len == name->len &&
!memcmp(de->d_name, name->name, name->len)) {
/* Zero out the entry (mark as free) */
memset(de, 0, sizeof(*de));
mark_buffer_dirty(bh);
brelse(bh);
inode_set_mtime_to_ts(dir,
current_time(dir));
mark_inode_dirty(dir);
return 0;
}
offset += sizeof(struct beamfs_dir_entry);
}
brelse(bh);
}
return -ENOENT;
}
/* ------------------------------------------------------------------ */
/* Helper: allocate and initialize a new VFS inode */
/* ------------------------------------------------------------------ */
struct inode *beamfs_new_inode(struct inode *dir, umode_t mode)
{
struct super_block *sb = dir->i_sb;
struct inode *inode;
struct beamfs_inode_info *fi;
u64 ino;
ino = beamfs_alloc_inode_num(sb);
if (!ino)
return ERR_PTR(-ENOSPC);
inode = new_inode(sb);
if (!inode)
return ERR_PTR(-ENOMEM);
inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
inode->i_ino = ino;
inode->i_size = 0;
inode_set_atime_to_ts(inode, current_time(inode));
inode_set_mtime_to_ts(inode, current_time(inode));
inode_set_ctime_to_ts(inode, current_time(inode));
fi = BEAMFS_I(inode);
memset(fi->i_direct, 0, sizeof(fi->i_direct));
fi->i_indirect = 0;
fi->i_dindirect = 0;
fi->i_flags = 0;
if (S_ISDIR(mode)) {
inode->i_op = &beamfs_dir_inode_operations;
inode->i_fop = &beamfs_dir_operations;
set_nlink(inode, 2);
} else {
struct beamfs_sb_info *sbi = BEAMFS_SB(inode->i_sb);
inode->i_op = &beamfs_file_inode_operations;
if (sbi->s_scheme == BEAMFS_DATA_PROTECTION_UNIVERSAL_INLINE) {
/*
* v2 INLINE: per-block RS FEC requires gather/scatter
* across 16 subblocks per disk block. Single-page
* folios only; large folios deferred to v2.1.
*/
inode->i_fop = &beamfs_inline_file_operations;
inode->i_mapping->a_ops = &beamfs_inline_aops;
mapping_set_folio_order_range(inode->i_mapping, 0, 0);
} else {
/* legacy iomap path (scheme=5 INODE_UNIVERSAL) */
inode->i_fop = &beamfs_file_operations;
inode->i_mapping->a_ops = &beamfs_aops;
}
set_nlink(inode, 1);
}
if (insert_inode_locked(inode) < 0) {
make_bad_inode(inode);
iput(inode);
return ERR_PTR(-EIO);
}
mark_inode_dirty(inode);
return inode;
}
/* ------------------------------------------------------------------ */
/* create - create a regular file */
/* ------------------------------------------------------------------ */
static int beamfs_create(struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, umode_t mode, bool excl)
{
struct inode *inode;
int ret;
inode = beamfs_new_inode(dir, mode | S_IFREG);
if (IS_ERR(inode))
return PTR_ERR(inode);
ret = beamfs_write_inode_raw(inode);
if (ret)
goto out_iput;
ret = beamfs_add_dirent(dir, &dentry->d_name, inode->i_ino, 1 /* DT_REG */);
if (ret)
goto out_iput;
ret = beamfs_write_inode_raw(dir);
if (ret)
goto out_iput;
d_instantiate(dentry, inode);
unlock_new_inode(inode);
return 0;
out_iput:
unlock_new_inode(inode);
iput(inode);
return ret;
}
/* ------------------------------------------------------------------ */
/* mkdir - create a directory */
/* ------------------------------------------------------------------ */
static struct dentry *beamfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, umode_t mode)
{
struct inode *inode;
int ret;
inode_inc_link_count(dir);
inode = beamfs_new_inode(dir, mode | S_IFDIR);
if (IS_ERR(inode)) {
inode_dec_link_count(dir);
return ERR_CAST(inode);
}
/* Add . and .. entries */
ret = beamfs_add_dirent(inode, &(struct qstr)QSTR_INIT(".", 1),
inode->i_ino, 4 /* DT_DIR */);
if (ret)
goto out_fail;
ret = beamfs_add_dirent(inode, &(struct qstr)QSTR_INIT("..", 2),
dir->i_ino, 4 /* DT_DIR */);
if (ret)
goto out_fail;
ret = beamfs_write_inode_raw(inode);
if (ret)
goto out_fail;
ret = beamfs_add_dirent(dir, &dentry->d_name, inode->i_ino,
4 /* DT_DIR */);
if (ret)
goto out_fail;
ret = beamfs_write_inode_raw(dir);
if (ret)
goto out_fail;
d_instantiate(dentry, inode);
unlock_new_inode(inode);
return NULL;
out_fail:
unlock_new_inode(inode);
inode_dec_link_count(inode);
inode_dec_link_count(inode);
iput(inode);
inode_dec_link_count(dir);
return ERR_PTR(ret);
}
/* ------------------------------------------------------------------ */
/* unlink - remove a file */
/* ------------------------------------------------------------------ */
static int beamfs_unlink(struct inode *dir, struct dentry *dentry)
{
struct inode *inode = d_inode(dentry);
int ret;
ret = beamfs_del_dirent(dir, &dentry->d_name);
if (ret)
return ret;
inode_set_ctime_to_ts(inode, current_time(inode));
inode_dec_link_count(inode);
beamfs_write_inode_raw(dir);
return 0;
}
/* ------------------------------------------------------------------ */
/* rmdir - remove an empty directory */
/* ------------------------------------------------------------------ */
static int beamfs_rmdir(struct inode *dir, struct dentry *dentry)
{
struct inode *inode = d_inode(dentry);
struct beamfs_inode_info *fi = BEAMFS_I(inode);
struct super_block *sb = inode->i_sb;
struct buffer_head *bh;
struct beamfs_dir_entry *de;
unsigned long block_no;
unsigned int offset;
int i, ret;
/*
* Verify the directory is empty: scan all direct blocks and check
* that no entries other than '.' and '..' exist. Testing i_nlink > 2
* is insufficient - regular files do not increment nlink on the parent,
* so a directory with only files can have nlink == 2 but still be
* non-empty.
*/
for (i = 0; i < BEAMFS_DIRECT_BLOCKS; i++) {
block_no = le64_to_cpu(fi->i_direct[i]);
if (!block_no)
break;
bh = sb_bread(sb, block_no);
if (!bh)
return -EIO;
offset = 0;
while (offset + sizeof(*de) <= BEAMFS_BLOCK_SIZE) {
de = (struct beamfs_dir_entry *)(bh->b_data + offset);
/*
* Skip free slots (d_ino == 0). A directory with a
* hole followed by live entries must NOT be reported
* empty: keep scanning past holes.
*/
if (de->d_ino &&
!(de->d_name_len == 1 && de->d_name[0] == '.') &&
!(de->d_name_len == 2 && de->d_name[0] == '.'
&& de->d_name[1] == '.')) {
brelse(bh);
return -ENOTEMPTY;
}
offset += sizeof(struct beamfs_dir_entry);
}
brelse(bh);
}
ret = beamfs_del_dirent(dir, &dentry->d_name);
if (ret)
return ret;
inode_dec_link_count(inode);
inode_dec_link_count(inode);
inode_dec_link_count(dir);
beamfs_write_inode_raw(dir);
return 0;
}
/* ------------------------------------------------------------------ */
/* link - create a hard link */
/* ------------------------------------------------------------------ */
static int beamfs_link(struct dentry *old_dentry, struct inode *dir,
struct dentry *dentry)
{
struct inode *inode = d_inode(old_dentry);
int ret;
inode_set_ctime_to_ts(inode, current_time(inode));
inode_inc_link_count(inode);
ret = beamfs_add_dirent(dir, &dentry->d_name, inode->i_ino, 1);
if (ret) {
inode_dec_link_count(inode);
return ret;
}
beamfs_write_inode_raw(inode);
beamfs_write_inode_raw(dir);
d_instantiate(dentry, inode);
ihold(inode);
return 0;
}
/* ------------------------------------------------------------------ */
/* write_inode - VFS super_op: persist inode to disk */
/* ------------------------------------------------------------------ */
int beamfs_write_inode(struct inode *inode, struct writeback_control *wbc)
{
return beamfs_write_inode_raw(inode);
}
/* ------------------------------------------------------------------ */
/* dir inode_operations - exported */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* rename - move/rename a directory entry */
/* ------------------------------------------------------------------ */
static int beamfs_rename(struct mnt_idmap *idmap,
struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry,
unsigned int flags)
{
struct inode *old_inode = d_inode(old_dentry);
struct inode *new_inode = d_inode(new_dentry);
int is_dir = S_ISDIR(old_inode->i_mode);
int ret;
/* beamfs v1: no RENAME_EXCHANGE or RENAME_WHITEOUT */
if (flags & ~RENAME_NOREPLACE)
return -EINVAL;
if (flags & RENAME_NOREPLACE && new_inode)
return -EEXIST;
/*
* If destination exists, unlink it first.
* For directories: target must be empty (nlink == 2: . and ..)
*/
if (new_inode) {
if (is_dir) {
if (new_inode->i_nlink > 2)
return -ENOTEMPTY;
}
ret = beamfs_del_dirent(new_dir, &new_dentry->d_name);
if (ret)
return ret;
if (is_dir) {
inode_dec_link_count(new_inode);
inode_dec_link_count(new_inode);
inode_dec_link_count(new_dir);
} else {
inode_dec_link_count(new_inode);
}
inode_set_ctime_to_ts(new_inode, current_time(new_inode));
}
/* Add entry in new_dir */
ret = beamfs_add_dirent(new_dir, &new_dentry->d_name,
old_inode->i_ino,
is_dir ? 4 /* DT_DIR */ : 1 /* DT_REG */);
if (ret)
return ret;
/* Remove entry from old_dir */
ret = beamfs_del_dirent(old_dir, &old_dentry->d_name);
if (ret) {
pr_err("beamfs: rename: del_dirent failed after add, fs may be inconsistent\n");
return ret;
}
/*
* Update ".." in the moved directory to point to new_dir.
* Also fix nlink on old_dir and new_dir.
*/
if (is_dir && old_dir != new_dir) {
struct qstr dotdot = QSTR_INIT("..", 2);
ret = beamfs_del_dirent(old_inode, &dotdot);
if (ret)
return ret;
ret = beamfs_add_dirent(old_inode, &dotdot,
new_dir->i_ino, 4 /* DT_DIR */);
if (ret)
return ret;
inode_dec_link_count(old_dir);
inode_inc_link_count(new_dir);
ret = beamfs_write_inode_raw(old_inode);
if (ret)
return ret;
}
/* Update timestamps */
inode_set_ctime_to_ts(old_inode, current_time(old_inode));
inode_set_mtime_to_ts(old_dir, current_time(old_dir));
inode_set_ctime_to_ts(old_dir, current_time(old_dir));
inode_set_mtime_to_ts(new_dir, current_time(new_dir));
inode_set_ctime_to_ts(new_dir, current_time(new_dir));
/* Persist all touched inodes */
ret = beamfs_write_inode_raw(old_inode);
if (ret)
return ret;
ret = beamfs_write_inode_raw(old_dir);
if (ret)
return ret;
if (old_dir != new_dir) {
ret = beamfs_write_inode_raw(new_dir);
if (ret)
return ret;
}
if (new_inode)
beamfs_write_inode_raw(new_inode);
return 0;
}
const struct inode_operations beamfs_dir_inode_operations = {
.lookup = beamfs_lookup,
.create = beamfs_create,
.mkdir = beamfs_mkdir,
.unlink = beamfs_unlink,
.rmdir = beamfs_rmdir,
.link = beamfs_link,
.rename = beamfs_rename,
};