/
janfs_fs.c
430 lines (342 loc) · 12.6 KB
/
janfs_fs.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
//-----------------------------------------------------------------------------
//
// Just Another Network File System for Linux.
//
// Copyright (C) 2013 Luiz Gustavo Pereira da Silva.
// 2013 Gustavo Pantuza.
//
// This file is released under the GPL.
//
//-----------------------------------------------------------------------------
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <linux/statfs.h>
#include <linux/backing-dev.h>
#include <asm/uaccess.h>
#include <linux/pagemap.h>
#include <linux/slab.h>
#include "socket.h"
#include "janfs_fs.h"
//-----------------------------------------------------------------------------
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Luiz Gustavo / Gustavo Pantuza");
//-----------------------------------------------------------------------------
#define JANFS_MAGIC 0xafafafaf
#define JANFS_BSIZE 1024
//-----------------------------------------------------------------------------
static const struct super_operations janfs_super_operations;
static const struct inode_operations janfs_dir_inode_operations;
//-----------------------------------------------------------------------------
// Local Functions Prototypes
//-----------------------------------------------------------------------------
static int janfs_statfs(struct dentry *dentry,
struct kstatfs *buf);
static struct inode *janfs_get_inode(struct super_block *sb,
const struct inode *dir,
umode_t mode,
dev_t dev);
static void janfs_kill_sb(struct super_block *sb);
//-----------------------------------------------------------------------------
// Local Variables
//-----------------------------------------------------------------------------
static char mount_path[512];
//-----------------------------------------------------------------------------
static struct backing_dev_info janfs_backing_dev_info = {
.name = "janfs",
.ra_pages = 0, /* No readahead */
.capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK |
BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY |
BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP,
};
//-----------------------------------------------------------------------------
static const struct inode_operations janfs_dir_inode_operations = {
// .create = janfs_create,
.lookup = simple_lookup,
.link = simple_link,
.unlink = simple_unlink,
// .symlink = janfs_symlink,
// .mkdir = janfs_mkdir,
.rmdir = simple_rmdir,
// .mknod = janfs_mknod,
.rename = simple_rename,
};
//-----------------------------------------------------------------------------
const struct file_operations janfs_file_operations = {
.read = do_sync_read,
.aio_read = generic_file_aio_read,
.write = do_sync_write,
.aio_write = generic_file_aio_write,
.mmap = generic_file_mmap,
.fsync = noop_fsync,
.splice_read = generic_file_splice_read,
.splice_write = generic_file_splice_write,
.llseek = generic_file_llseek,
};
//-----------------------------------------------------------------------------
const struct inode_operations janfs_file_inode_operations = {
.setattr = simple_setattr,
.getattr = simple_getattr,
};
//-----------------------------------------------------------------------------
const struct address_space_operations janfs_aops = {
.readpage = simple_readpage,
.write_begin = simple_write_begin,
.write_end = simple_write_end,
// .set_page_dirty = __set_page_dirty_no_writeback,
};
//-----------------------------------------------------------------------------
// The filesystem registry structure. Holds the name of the filesystem type
// and a function that can construct a superblock.
//-----------------------------------------------------------------------------
static struct file_system_type janfs_fstype = {
.owner = THIS_MODULE,
.name = "janfs",
.mount = janfs_mount,
.kill_sb = janfs_kill_sb,
.fs_flags = FS_USERNS_MOUNT,
};
//-----------------------------------------------------------------------------
static const struct super_operations janfs_super_operations = {
.statfs = janfs_statfs,
.drop_inode = generic_delete_inode,
.show_options = generic_show_options,
};
/*
void mount_cmd(char* data)
{
unsigned char recv_buf[512];
unsigned short recv_size = 0;
printk("mount_cmd INI: data[%s].\n", data);
recv_size = sizeof(recv_buf);
if (srv_cmd(MOUNT_CMD, data, strlen(data), recv_buf, recv_size) != 0) {
printk(KERN_ERR " Could not send or receive the mount command.\n");
return;
}
// Set root inode
}
*/
//-----------------------------------------------------------------------------
// Read remote directory "path"
//-----------------------------------------------------------------------------
static void janfs_read_dir(char* path, struct FileDesc** file_list,
unsigned short* nfiles)
{
printk("janfs_read_dir INI: path[%s].\n", path);
srv_read_dir(path, file_list, nfiles);
printk(" janfs_read_dir returned [%d] files in directory [%s].\n", *nfiles, path);
}
//-----------------------------------------------------------------------------
// Populates directory
//-----------------------------------------------------------------------------
static struct dentry * janfs_create_file(struct super_block *sb,
struct dentry *dir,
struct FileDesc desc)
{
struct dentry* dentry;
struct inode *i;
struct qstr hname;
// Hashed version of the name
hname.name = desc.name;
hname.len = strlen (desc.name);
hname.hash = full_name_hash(desc.name, hname.len);
// Create dentry and the inode to associate with it
dentry = d_alloc(dir, &hname);
if (!dentry)
goto out_err;
i = janfs_get_inode(sb, NULL, S_IFREG | 0755, 0);
if (!i)
goto out_err_put;
// Add inode to dentry cache
d_add(dentry, i);
printk("janfs_create_file created name[%s], type[%c], size[%lu].\n", desc.name, desc.type, desc.size);
return dentry;
out_err_put:
dput(dentry);
out_err:
return 0;
}
//-----------------------------------------------------------------------------
// Read the superblock
//-----------------------------------------------------------------------------
int janfs_fill_super(struct super_block *sb, void *data, int silent)
{
struct inode* i = NULL;
struct FileDesc *root_files = NULL;
unsigned short n, nfiles = 0;
// Fill in the superblock
sb->s_blocksize = PAGE_CACHE_SIZE;
sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
sb->s_magic = JANFS_MAGIC;
sb->s_flags |= MS_NOATIME; // Do not update access times
sb->s_op = &janfs_super_operations;
//sb->s_d_op = &janfs_dentry_operations;
// Make the root inode for this filesystem
i = janfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
sb->s_root = d_make_root(i);
if (!sb->s_root)
return -ENOMEM;
// Read root directory
janfs_read_dir(mount_path, &root_files, &nfiles);
// Populates root
for (n = 0; n < nfiles; n++) {
janfs_create_file(sb, sb->s_root, root_files[n]);
}
return 0;
/*
struct tree_descr *root_files = NULL;
// Read root directory
janfs_read_dir(mount_path, &root_files);
// Populates root directory
return simple_fill_super(sb, JANFS_MAGIC, root_files);
*/
}
//-----------------------------------------------------------------------------
// Mount the filesystem
// From docs: the mount() method must return the root dentry of the tree
// requested by caller. An active reference to its superblock must be
// grabbed and the superblock must be locked. On failure it should return
// ERR_PTR(error).
//-----------------------------------------------------------------------------
struct dentry *janfs_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
{
int ret = 0, remote_port = 0;
char remote_address[32];
char* token = NULL;
char* data_buf = NULL;
printk("Janfs mounting filesystem: data[%s], dev_name[%s].\n", (char*)data, dev_name);
data_buf = kstrdup(dev_name, GFP_KERNEL);
if (!data_buf) {
printk(" Error duplicating string [%s].\n", dev_name);
goto out_err_ret;
}
// Remote address
token = strsep(&data_buf, ":");
if (!token) {
printk(" Error spliting string [%s].\n", data_buf);
goto out_err;
}
strncpy(remote_address, token, sizeof(remote_address));
// Remote port
token = strsep(&data_buf, ":");
if (!token) {
printk(" Error spliting string [%s].\n", data_buf);
goto out_err;
}
if (kstrtoint(token, 10, &remote_port) != 0) {
printk(" Error parsing integer from string [%s].\n", token);
goto out_err;
}
// Copy remote mount path
token = strsep(&data_buf, ":");
if (!token) {
printk(" Error spliting string [%s].\n", data_buf);
goto out_err;
}
strncpy(mount_path, token, sizeof(mount_path));
// Free duplicated string
kfree(data_buf);
printk("Mounting janfs with remote addr=%s, port=%d and path=%s.\n",
remote_address, remote_port, token);
ret = create_client_socket();
if (ret) {
printk(KERN_ERR " Could not create client socket.\n");
goto out_err;
}
// Connect to specified address
ret = connect_server(remote_address, remote_port);
if (ret) {
printk(KERN_ERR " Could not connect to remote host.\n");
goto out_err;
}
return mount_nodev(fs_type, flags, data, janfs_fill_super);
out_err:
kfree(data_buf);
out_err_ret:
return ERR_PTR(-ENOMEM);
}
//-----------------------------------------------------------------------------
// Mount the filesystem
//-----------------------------------------------------------------------------
static void janfs_kill_sb(struct super_block *sb)
{
kfree(sb->s_fs_info);
kill_litter_super(sb);
}
//-----------------------------------------------------------------------------
// Return information about an JANFS volume
//-----------------------------------------------------------------------------
int janfs_statfs(struct dentry *dentry, struct kstatfs *buf)
{
// struct super_block *sb = dentry->d_sb;
buf->f_type = JANFS_MAGIC;
buf->f_bsize = JANFS_BSIZE;
// buf->f_blocks = JANFS_SB(sb)->blocks;
// buf->f_bfree = 0;
// buf->f_bavail = 0;
// buf->f_files = JANFS_SB(sb)->files;
// buf->f_ffree = 0;
buf->f_namelen = 256;
return 0;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
struct inode *janfs_get_inode(struct super_block *sb,
const struct inode *dir, umode_t mode, dev_t dev)
{
struct inode * inode = new_inode(sb);
if (inode) {
inode->i_ino = get_next_ino();
inode_init_owner(inode, dir, mode);
inode->i_mapping->a_ops = &janfs_aops;
inode->i_mapping->backing_dev_info = &janfs_backing_dev_info;
mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
//mapping_set_unevictable(inode->i_mapping);
inode->i_flags |= S_NOATIME|S_NOCMTIME;
switch (mode & S_IFMT) {
default:
init_special_inode(inode, mode, dev);
break;
case S_IFREG:
inode->i_op = &janfs_file_inode_operations;
inode->i_fop = &janfs_file_operations;
break;
case S_IFDIR:
inode->i_op = &janfs_dir_inode_operations;
inode->i_fop = &simple_dir_operations;
// directory inodes start off with i_nlink == 2 (for "." entry)
inc_nlink(inode);
break;
case S_IFLNK:
inode->i_op = &page_symlink_inode_operations;
break;
}
}
return inode;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/*
* Attaches the new filesystem to the kernel.
*/
static int __init janfs_init(void)
{
printk("Registering janfs filesystem.\n");
return register_filesystem(&janfs_fstype);
}
/*
* Removes the filesystem from the kernel.
*/
static void __exit janfs_exit(void)
{
printk("Removing janfs filesystem.\n");
close_client_socket();
unregister_filesystem(&janfs_fstype);
}
module_init(janfs_init)
module_exit(janfs_exit)