-
Notifications
You must be signed in to change notification settings - Fork 6
/
super.c
557 lines (451 loc) · 12.4 KB
/
super.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
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
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/dcache.h>
#include <linux/statfs.h>
#include <linux/slab.h>
#include <linux/pfn_t.h>
#include <linux/blkdev.h>
#include <linux/random.h>
#include "opensimfs.h"
int opensimfs_support_clwb;
int opensimfs_support_pcommit;
static struct kmem_cache *opensimfs_inode_cachep;
static struct kmem_cache *opensimfs_range_node_cachep;
static void init_once(void *foo);
static int __init init_range_node_cache(void)
{
opensimfs_range_node_cachep = kmem_cache_create(
"opensimfs_range_node_cache",
sizeof(struct opensimfs_range_node),
0,
(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD),
NULL);
if (opensimfs_range_node_cachep == NULL)
return -ENOMEM;
return 0;
}
static void destroy_range_node_cache(void)
{
kmem_cache_destroy(opensimfs_range_node_cachep);
}
static int __init init_inode_cache(void)
{
opensimfs_inode_cachep = kmem_cache_create(
"opensimfs_inode_cache",
sizeof(struct opensimfs_inode_info),
0,
(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD),
init_once);
if (opensimfs_inode_cachep == NULL)
return -ENOMEM;
return 0;
}
static void destroy_inode_cache(void)
{
/*
* Make sure all delayed rcu free inodes are flushed before
* we destroy cache.
*/
rcu_barrier();
kmem_cache_destroy(opensimfs_inode_cachep);
}
static int opensimfs_get_sb_info(
struct super_block *sb,
struct opensimfs_super_block_info *sbi)
{
pfn_t pfn;
void *virt_addr = NULL;
long size;
if (!sb->s_bdev->bd_disk->fops->direct_access) {
return -EINVAL;
}
sbi->s_bdev = sb->s_bdev;
size = sb->s_bdev->bd_disk->fops->direct_access(
sb->s_bdev, 0, &virt_addr, &pfn);
if (size <= 0)
return -EINVAL;
sbi->phys_addr = pfn_t_to_pfn(pfn);
sbi->virt_addr = virt_addr;
sbi->initsize = size;
return 0;
}
static void opensimfs_set_default_opts(
struct opensimfs_super_block_info *sbi)
{
set_mount_opt(sbi->s_mount_opt, OPENSIMFS_MOUNT_HUGEIOREMAP);
set_mount_opt(sbi->s_mount_opt, OPENSIMFS_MOUNT_ERRORS_CONT);
sbi->reserved_blocks = OPENSIMFS_RESERVED_BLOCKS;
/* sbi->cpus = num_online_cpis(); */
sbi->cpus = 1;
}
static void opensimfs_set_blocksize(
struct super_block *sb,
unsigned long size)
{
int bits;
bits = fls(size) - 1;
sb->s_blocksize_bits = bits;
sb->s_blocksize = (1 << bits);
}
void opensimfs_sysfs_init(
struct super_block *sb)
{
}
void opensimfs_sysfs_exit(
struct super_block *sb)
{
}
inline struct opensimfs_inode *opensimfs_get_basic_inode(
struct super_block *sb,
u64 ino)
{
struct opensimfs_super_block_info *sbi = OPENSIMFS_SB(sb);
return (struct opensimfs_inode *)
(sbi->virt_addr +
OPENSIMFS_ROOT_INODE_START +
(ino - OPENSIMFS_ROOT_INO) * OPENSIMFS_INODE_SIZE);
}
inline struct opensimfs_inode *opensimfs_get_special_inode(
struct super_block *sb,
u64 ino)
{
if (ino == 0 || ino >= OPENSIMFS_NORMAL_INODE_START)
return NULL;
return opensimfs_get_basic_inode(sb, ino);
}
/* returns root_inode */
static struct opensimfs_inode *opensimfs_init(
struct super_block *sb,
unsigned long size)
{
unsigned long blocksize;
unsigned long reserved_space, reserved_blocks;
struct opensimfs_super_block *super;
struct opensimfs_super_block_info *sbi = OPENSIMFS_SB(sb);
struct opensimfs_inode *root_i;
sbi->num_blocks = (size >> PAGE_SHIFT);
blocksize = sbi->blocksize = OPENSIMFS_DEF_BLOCK_SIZE_4K;
opensimfs_set_blocksize(sb, blocksize);
blocksize = sb->s_blocksize;
if (sbi->blocksize && sbi->blocksize != blocksize)
sbi->blocksize = blocksize;
reserved_space = OPENSIMFS_SB_SIZE * 4;
reserved_blocks = (reserved_space + blocksize - 1) / blocksize;
super = opensimfs_get_super(sb);
/* clear-out super block and inode table */
memset_nt(super, 0, sbi->reserved_blocks * sbi->blocksize);
super->s_blocksize = cpu_to_le32(blocksize);
super->s_size = cpu_to_le64(size);
super->s_magic = cpu_to_le32(OPENSIMFS_SUPER_MAGIC);
opensimfs_init_blockmap(sb);
if (opensimfs_journal_hard_init(sb) < 0)
return ERR_PTR(-EINVAL);
if (opensimfs_init_inode_inuse_list(sb) < 0)
return ERR_PTR(-EINVAL);
if (opensimfs_init_inode_table(sb) < 0)
return ERR_PTR(-EINVAL);
opensimfs_flush_buffer(super, OPENSIMFS_SB_SIZE, false);
opensimfs_flush_buffer(
(char *)super + OPENSIMFS_SB_SIZE, sizeof(*super), false);
root_i = opensimfs_get_special_inode(sb, OPENSIMFS_ROOT_INO);
/* allocate root inode */
root_i->i_mode = cpu_to_le16(sbi->mode | S_IFDIR);
root_i->i_uid = cpu_to_le32(from_kuid(&init_user_ns, sbi->uid));
root_i->i_gid = cpu_to_le32(from_kgid(&init_user_ns, sbi->gid));
root_i->i_links_count = cpu_to_le16(2);
root_i->i_blk_type = OPENSIMFS_BLOCK_TYPE_4K;
root_i->i_flags = 0;
root_i->i_blocks = cpu_to_le64(1);
root_i->i_size = cpu_to_le64(sb->s_blocksize);
root_i->i_atime = root_i->i_mtime = root_i->i_ctime =
cpu_to_le32(get_seconds());
root_i->opensimfs_ino = OPENSIMFS_ROOT_INO;
root_i->valid = 1;
opensimfs_flush_buffer(root_i, sizeof(*root_i), false);
PERSISTENT_MARK();
PERSISTENT_BARRIER();
return root_i;
}
static loff_t opensimfs_max_file_size(int bits)
{
loff_t res;
res = (1ULL << 63) - 1;
if (res > MAX_LFS_FILESIZE)
res = MAX_LFS_FILESIZE;
return res;
}
static inline
struct opensimfs_range_node *opensimfs_alloc_range_node(
struct super_block *sb)
{
struct opensimfs_range_node *p;
p = (struct opensimfs_range_node *)
kmem_cache_alloc(opensimfs_range_node_cachep, GFP_NOFS);
return p;
}
static inline
void opensimfs_free_range_node(
struct opensimfs_range_node *node)
{
kmem_cache_free(opensimfs_range_node_cachep, node);
}
inline struct opensimfs_range_node *opensimfs_alloc_block_node(
struct super_block *sb)
{
return opensimfs_alloc_range_node(sb);
}
inline void opensimfs_free_block_node(
struct super_block *sb,
struct opensimfs_range_node *node)
{
opensimfs_free_range_node(node);
}
inline struct opensimfs_range_node *opensimfs_alloc_inode_node(
struct super_block *sb)
{
return opensimfs_alloc_range_node(sb);
}
inline void opensimfs_free_inode_node(
struct super_block *sb,
struct opensimfs_range_node *node)
{
opensimfs_free_range_node(node);
}
static struct inode *opensimfs_alloc_inode(
struct super_block *sb)
{
struct opensimfs_inode_info *vi;
vi = kmem_cache_alloc(opensimfs_inode_cachep, GFP_NOFS);
if (!vi)
return NULL;
vi->vfs_inode.i_version = 1;
return &vi->vfs_inode;
}
static void opensimfs_i_callback(struct rcu_head *head)
{
struct inode *inode = container_of(head, struct inode, i_rcu);
struct opensimfs_inode_info *vi = OPENSIMFS_I(inode);
kmem_cache_free(opensimfs_inode_cachep, vi);
}
static void opensimfs_destroy_inode(struct inode *inode)
{
call_rcu(&inode->i_rcu, opensimfs_i_callback);
}
static void opensimfs_put_super(struct super_block *sb)
{
struct opensimfs_super_block_info *sbi = OPENSIMFS_SB(sb);
if (sbi->virt_addr) {
sbi->virt_addr = NULL;
}
opensimfs_sysfs_exit(sb);
kfree(sbi);
sb->s_fs_info = NULL;
}
int opensimfs_statfs(
struct dentry *d,
struct kstatfs *buf)
{
struct super_block *sb = d->d_sb;
struct opensimfs_super_block_info *sbi = OPENSIMFS_SB(sb);
buf->f_type = OPENSIMFS_SUPER_MAGIC;
buf->f_type = OPENSIMFS_SUPER_MAGIC;
buf->f_bsize = sb->s_blocksize;
buf->f_blocks = sbi->num_blocks;
buf->f_bfree = buf->f_bavail = opensimfs_count_free_blocks(sb);
buf->f_files = LONG_MAX;
buf->f_ffree = LONG_MAX;
buf->f_namelen = OPENSIMFS_NAME_LEN;
return 0;
}
int opensimfs_remount(
struct super_block *sb,
int *mntflags,
char *data)
{
struct opensimfs_super_block *ps;
struct opensimfs_super_block_info *sbi = OPENSIMFS_SB(sb);
int ret = -EINVAL;
mutex_lock(&sbi->s_lock);
sb->s_flags =
(sb->s_flags & ~MS_POSIXACL) |
((sbi->s_mount_opt & OPENSIMFS_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
if ((*mntflags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
/* update time stuffs */
ps = opensimfs_get_super(sb);
}
mutex_unlock(&sbi->s_lock);
ret = 0;
return ret;
}
static int opensimfs_show_options(
struct seq_file *seq,
struct dentry *root)
{
return 0;
}
static struct super_operations opensimfs_sops = {
.alloc_inode = opensimfs_alloc_inode,
.destroy_inode = opensimfs_destroy_inode,
.write_inode = opensimfs_write_inode,
.dirty_inode = opensimfs_dirty_inode,
.evict_inode = opensimfs_evict_inode,
.put_super = opensimfs_put_super,
.statfs = opensimfs_statfs,
.remount_fs = opensimfs_remount,
.show_options = opensimfs_show_options,
};
/*
static struct dentry *opensimfs_fh_to_dentry(
struct super_block *sb,
int fh_type)
{
return NULL;
}
static struct dentry *opensimfs_fh_to_parent(
struct fid *fid,
int fh_len,
int fh_type)
{
return NULL;
}
static const struct export_operations opensimfs_export_ops = {
.fh_to_dentry = opensimfs_fh_to_dentry,
.fh_to_parent = opensimfs_fh_to_parent,
.get_parent = opensimfs_get_parent,
};
*/
static int opensimfs_fill_super(
struct super_block *sb,
void *data,
int silent)
{
int retval = -EINVAL;
struct opensimfs_super_block *super;
struct opensimfs_inode *root_pi;
struct opensimfs_inode_info *si;
struct opensimfs_inode_info_header *sih;
struct opensimfs_super_block_info *sbi = NULL;
struct inode *root_i;
u32 random = 0;
sbi = kzalloc(sizeof(struct opensimfs_super_block_info), GFP_KERNEL);
if (!sbi)
return -ENOMEM;
sb->s_fs_info = sbi;
sbi->sb = sb;
opensimfs_set_default_opts(sbi);
if (opensimfs_get_sb_info(sb, sbi))
goto out;
get_random_bytes(&random, sizeof(u32));
atomic_set(&sbi->next_generation, random);
/* initialize with default values */
sbi->mode = (S_IRUGO | S_IXUGO | S_IWUGO);
sbi->uid = current_fsuid();
sbi->gid = current_fsgid();
set_mount_opt(sbi->s_mount_opt, OPENSIMFS_MOUNT_DAX);
clear_mount_opt(sbi->s_mount_opt, OPENSIMFS_MOUNT_PROTECT);
set_mount_opt(sbi->s_mount_opt, OPENSIMFS_MOUNT_HUGEIOREMAP);
opensimfs_sysfs_init(sb);
mutex_init(&sbi->s_lock);
set_mount_opt(sbi->s_mount_opt, OPENSIMFS_MOUNT_MOUNTING);
/* clayc: always initialize now */
set_mount_opt(sbi->s_mount_opt, OPENSIMFS_MOUNT_FORMAT);
if (sbi->s_mount_opt & OPENSIMFS_MOUNT_FORMAT) {
root_pi = opensimfs_init(sb, sbi->initsize);
if (IS_ERR(root_pi))
goto out;
super = opensimfs_get_super(sb);
goto setup_sb;
}
setup_sb:
sb->s_magic = le32_to_cpu(super->s_magic);
sb->s_magic = le32_to_cpu(super->s_magic);
sb->s_op = &opensimfs_sops;
sb->s_maxbytes = opensimfs_max_file_size(sb->s_blocksize_bits);
sb->s_time_gran = 1;
/* sb->s_export_op = &nova_export_ops; */
sb->s_xattr = NULL;
sb->s_flags |= MS_NOSEC;
root_i = opensimfs_iget(sb, OPENSIMFS_ROOT_INO);
if (IS_ERR(root_i)) {
retval = PTR_ERR(root_i);
goto out;
}
si = OPENSIMFS_I(root_i);
sih = &si->header;
opensimfs_new_blocks(sb, &sih->pte_block, 1, OPENSIMFS_BLOCK_TYPE_4K, 1, DATA);
opensimfs_new_blocks(sb, &sih->data_block, 1, OPENSIMFS_BLOCK_TYPE_4K, 1, DATA);
opensimfs_new_blocks(sb, &sih->pfw_pte_block, 1, OPENSIMFS_BLOCK_TYPE_4K, 1, DATA);
opensimfs_new_blocks(sb, &sih->pfw_data_block, 1, OPENSIMFS_BLOCK_TYPE_4K, 1, DATA);
opensimfs_append_dir_init_entries(
sb, root_pi, OPENSIMFS_ROOT_INO, OPENSIMFS_ROOT_INO);
sb->s_root = d_make_root(root_i);
if (!sb->s_root) {
retval = -ENOMEM;
goto out;
}
if (!(sb->s_flags & MS_RDONLY)) {
/* update time stuffs */
}
clear_mount_opt(sbi->s_mount_opt, OPENSIMFS_MOUNT_MOUNTING);
retval = 0;
return retval;
out:
kfree(sbi);
if (sbi->journal_locks) {
kfree(sbi->journal_locks);
sbi->journal_locks = NULL;
}
return retval;
}
static struct dentry *opensimfs_mount(
struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
{
return mount_bdev(fs_type, flags, dev_name, data, opensimfs_fill_super);
}
static struct file_system_type opensimfs_type = {
.owner = THIS_MODULE,
.name = "OpenSIMFS",
.mount = opensimfs_mount,
.kill_sb = kill_block_super,
};
static void init_once(void *foo)
{
struct opensimfs_inode_info *vi = foo;
inode_init_once(&vi->vfs_inode);
}
static int __init init_opensimfs(void)
{
int rc = 0;
if (arch_has_pcommit())
opensimfs_support_pcommit = 1;
if (arch_has_clwb())
opensimfs_support_clwb = 1;
rc = init_range_node_cache();
if (rc)
return rc;
rc = init_inode_cache();
if (rc)
goto out1;
rc = register_filesystem(&opensimfs_type);
if (rc)
goto out2;
return 0;
out2:
destroy_inode_cache();
out1:
destroy_range_node_cache();
return rc;
}
static void __exit exit_opensimfs(void)
{
unregister_filesystem(&opensimfs_type);
destroy_inode_cache();
destroy_range_node_cache();
}
MODULE_AUTHOR("Clay Chang <[email protected]>");
MODULE_DESCRIPTION("OpenSIMFS: An Open Source version of SIMFS");
MODULE_LICENSE("GPL");
module_init(init_opensimfs)
module_exit(exit_opensimfs)