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
| #include <linux/fs.h> #include <linux/init.h> #include <linux/module.h> #include <linux/time.h> #include <linux/uaccess.h>
#include "tinyfs.h"
static int tinyfs_iterate(struct file* filp, struct dir_context* ctx); ssize_t tinyfs_file_read(struct file* filp, char __user* buf, size_t len, loff_t* ppos); ssize_t tinyfs_file_write(struct file* filp, const char __user* buf, size_t len, loff_t* ppos); static int tinyfs_do_create(struct inode* dir, struct dentry* dentry, umode_t mode); static int tinyfs_mkdir(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode); static int tinyfs_create(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode, bool excl); int tinyfs_rmdir(struct inode* dir, struct dentry* dentry); int tinyfs_unlink(struct inode* dir, struct dentry* dentry); struct dentry* tinyfs_lookup(struct inode* parent_inode, struct dentry* child_dentry, unsigned int flags); static struct dentry* tinyfs_mount(struct file_system_type* fs_type, int flags, const char* dev_name, void* data); int tinyfs_fill_super(struct super_block* sb, void* data, int flags);
static struct tinyfs_file_blk* disk[MAX_FILES + 1];
static int files_count = 0;
static void init_disk(void) { for (int i = 0; i < MAX_FILES; i++) { disk[i] = NULL; } }
static struct tinyfs_file_blk* alloc_block(uint8_t idx, mode_t mode) { struct tinyfs_file_blk* blk = kmalloc(sizeof(struct tinyfs_file_blk) + MAX_FILEBYTES, GFP_KERNEL); if (!blk) return NULL;
blk->busy = 1; blk->mode = mode; blk->idx = idx;
if (S_ISDIR(mode)) blk->dir_children = 0; else blk->file_size = 0;
memset(blk->data, 0, MAX_FILEBYTES);
return blk; }
const struct file_operations tinyfs_dir_operations = { .owner = THIS_MODULE, .iterate_shared = tinyfs_iterate, };
static int tinyfs_iterate(struct file* filp, struct dir_context* ctx) { loff_t pos; struct tinyfs_file_blk* blk; struct tinyfs_dir_entry* entry; int i;
pos = filp->f_pos; if (pos) return 0;
blk = (struct tinyfs_file_blk*)filp->f_path.dentry->d_inode->i_private; if (!blk) { pr_err("Null pointer\n"); return -EFAULT; }
if (!S_ISDIR(blk->mode)) { pr_err("Not a directory block\n"); return -ENOTDIR; }
entry = (struct tinyfs_dir_entry*)&blk->data[0]; for (i = 0; i < blk->dir_children; i++) { pr_info("Reading entry %d-th: %s\n", i + 1, entry[i].filename); dir_emit(ctx, entry[i].filename, strnlen(entry[i].filename, MAX_NAMELEN), entry[i].idx, DT_UNKNOWN); ctx->pos++; }
return 0; }
const struct file_operations tinyfs_file_operations = { .read = tinyfs_file_read, .write = tinyfs_file_write, };
ssize_t tinyfs_file_write(struct file* filp, const char __user* buf, size_t len, loff_t* ppos) { struct tinyfs_file_blk* blk; char* buffer;
blk = filp->f_path.dentry->d_inode->i_private; if (!blk) { pr_err("Null pointer\n"); return -EFAULT; }
buffer = (char*)&blk->data[0]; buffer += *ppos;
len = min(MAX_FILEBYTES - (size_t)blk->file_size, len);
if (copy_from_user(buffer, buf, len)) { pr_err("Failed to copy data from user to kernel\n"); return -EFAULT; } *ppos += len; blk->file_size = *ppos;
pr_info("Written %zu bytes to file, total %lld bytes\n", len, *ppos);
return len; }
ssize_t tinyfs_file_read(struct file* filp, char __user* buf, size_t len, loff_t* ppos) { struct tinyfs_file_blk* blk; char* buffer;
blk = (struct tinyfs_file_blk*)filp->f_path.dentry->d_inode->i_private; if (!blk) { pr_err("Null pointer\n"); return -EFAULT; }
if (*ppos > blk->file_size) { pr_err("Failed to read, end of file reached: ppos=%lld, file_size=%d\n", *ppos, blk->file_size); return 0; }
len = min((size_t)blk->file_size, len);
buffer = (char*)&blk->data[0]; if (copy_to_user(buf, buffer, len)) { pr_err("Failed to copy data from kernel to user\n"); return -EFAULT; } *ppos += len;
pr_info("Read successful: ppos=%lld, len=%zu\n", *ppos, len);
return len; }
static struct inode_operations tinyfs_inode_ops = { .create = tinyfs_create, .unlink = tinyfs_unlink, .mkdir = tinyfs_mkdir, .rmdir = tinyfs_rmdir, .lookup = tinyfs_lookup, };
static int tinyfs_do_create(struct inode* dir, struct dentry* dentry, umode_t mode) { struct inode* inode; struct super_block* sb; struct tinyfs_dir_entry* entry; struct tinyfs_file_blk *blk, *pblk; int idx = -1;
sb = dir->i_sb; if (!sb) { pr_err("Null pointer\n"); return -EFAULT; }
for (int i = 2; i < MAX_FILES + 1; i++) { if (!disk[i]) { idx = i; break; } }
if (files_count >= MAX_FILES || -1 == idx) { pr_err("No space left to create file or directory\n"); return -ENOSPC; }
if (!S_ISDIR(mode) && !S_ISREG(mode)) { pr_err("Invalid mode (%u): neither directory nor file\n", mode); return -EINVAL; }
disk[idx] = alloc_block(idx, mode); if (!disk[idx]) { pr_err("Failed to alloc a disk block space\n"); return -ENOMEM; }
inode = new_inode(sb); if (!inode) { pr_err("Failed to create new inode\n"); return -ENOMEM; } inode->i_ino = idx; inode->i_sb = sb; inode->i_op = &tinyfs_inode_ops; inode->__i_atime = inode->__i_mtime = inode->__i_ctime = current_time(inode);
blk = disk[idx]; blk->mode = mode;
if (S_ISDIR(mode)) { blk->dir_children = 0; inode->i_fop = &tinyfs_dir_operations; pr_info("Created directory: %s\n", dentry->d_name.name); } else if (S_ISREG(mode)) { blk->file_size = 0; inode->i_fop = &tinyfs_file_operations; pr_info("Created file: %s\n", dentry->d_name.name); }
inode->i_private = blk;
pblk = (struct tinyfs_file_blk*)dir->i_private; if (pblk->dir_children + 1 > MAX_DENTRY_NR) { pr_err("No space left to save dir entry\n"); return -ENOMEM; }
entry = (struct tinyfs_dir_entry*)&pblk->data[0]; entry += pblk->dir_children;
entry->idx = idx; strncpy(entry->filename, dentry->d_name.name, MAX_NAMELEN - 1);
pblk->dir_children++;
inode_init_owner(&nop_mnt_idmap, inode, dir, mode); d_add(dentry, inode);
files_count++;
return 0; }
static int tinyfs_mkdir(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode) { return tinyfs_do_create(dir, dentry, S_IFDIR | mode); }
static int tinyfs_create(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode, bool excl) { return tinyfs_do_create(dir, dentry, mode); }
static struct inode* tinyfs_iget(struct super_block* sb, int idx) { struct inode* inode; struct tinyfs_file_blk* blk;
blk = disk[idx]; if (!blk) { pr_err("Null pointer\n"); return NULL; }
inode = new_inode(sb); if (!inode) { pr_err("Failed to create a new inode\n"); return NULL; } inode->i_ino = idx; inode->i_sb = sb; inode->i_op = &tinyfs_inode_ops;
if (S_ISDIR(blk->mode)) inode->i_fop = &tinyfs_dir_operations; else if (S_ISREG(blk->mode)) inode->i_fop = &tinyfs_file_operations;
inode->__i_atime = inode->__i_mtime = inode->__i_ctime = current_time(inode); inode->i_private = blk;
return inode; }
struct dentry* tinyfs_lookup(struct inode* parent_inode, struct dentry* child_dentry, unsigned int flags) { struct super_block* sb = parent_inode->i_sb; struct tinyfs_file_blk* blk; struct tinyfs_dir_entry* entry;
blk = (struct tinyfs_file_blk*)parent_inode->i_private;
entry = (struct tinyfs_dir_entry*)&blk->data[0]; for (int i = 0; i < blk->dir_children; i++) { if (!strcmp(entry[i].filename, child_dentry->d_name.name)) { struct inode* inode = tinyfs_iget(sb, entry[i].idx); if (!inode) { pr_err("Null pointer\n"); return NULL; } struct tinyfs_file_blk* inner = (struct tinyfs_file_blk*)inode->i_private; inode_init_owner(&nop_mnt_idmap, inode, parent_inode, inner->mode); d_add(child_dentry, inode); pr_info("found dentry: %s\n", child_dentry->d_name.name); return NULL; } }
return NULL; }
int tinyfs_rmdir(struct inode* dir, struct dentry* dentry) { struct inode* inode = dentry->d_inode; struct tinyfs_file_blk* blk = (struct tinyfs_file_blk*)inode->i_private; struct tinyfs_file_blk* pblk = (struct tinyfs_file_blk*)dir->i_private; struct tinyfs_dir_entry* entry;
if (blk->dir_children > 0) { pr_err("Cannot remove non-empty directory: %s\n", dentry->d_name.name); return -ENOTEMPTY; }
entry = (struct tinyfs_dir_entry*)&pblk->data[0]; for (int i = 0; i < pblk->dir_children; i++) { if (!strcmp(entry[i].filename, dentry->d_name.name)) { for (int j = i; j < pblk->dir_children - 1; j++) { memcpy(&entry[j], &entry[j + 1], sizeof(struct tinyfs_dir_entry)); } pblk->dir_children--; break; } }
disk[blk->idx] = NULL; kfree(blk); blk = NULL;
pr_info("Removed directory: %s\n", dentry->d_name.name);
return simple_rmdir(dir, dentry); }
int tinyfs_unlink(struct inode* dir, struct dentry* dentry) { struct inode* inode = dentry->d_inode; struct tinyfs_file_blk* blk = (struct tinyfs_file_blk*)inode->i_private; struct tinyfs_file_blk* pblk = (struct tinyfs_file_blk*)dir->i_private; struct tinyfs_dir_entry* entry;
entry = (struct tinyfs_dir_entry*)&pblk->data[0]; for (int i = 0; i < pblk->dir_children; i++) { if (!strcmp(entry[i].filename, dentry->d_name.name)) { for (int j = i; j < pblk->dir_children - 1; j++) { memcpy(&entry[j], &entry[j + 1], sizeof(struct tinyfs_dir_entry)); } pblk->dir_children--; break; } }
disk[blk->idx] = NULL; kfree(blk); blk = NULL;
pr_info("Removed file: %s\n", dentry->d_name.name);
return simple_unlink(dir, dentry); }
static const struct super_operations tinyfs_super_operations = { .statfs = simple_statfs, .drop_inode = generic_delete_inode, };
int tinyfs_fill_super(struct super_block* sb, void* data, int flags) { struct inode* root_inode; int mode = S_IFDIR | 0755;
sb->s_magic = TINYFS_MAGIC; sb->s_op = &tinyfs_super_operations;
root_inode = new_inode(sb); if (!root_inode) { pr_err("Failed to create root inode\n"); return -ENOMEM; }
inode_init_owner(&nop_mnt_idmap, root_inode, NULL, mode); root_inode->i_ino = 1; root_inode->i_sb = sb; root_inode->i_op = &tinyfs_inode_ops; root_inode->i_fop = &tinyfs_dir_operations; root_inode->__i_atime = root_inode->__i_mtime = root_inode->__i_ctime = current_time(root_inode);
sb->s_root = d_make_root(root_inode); if (!sb->s_root) { pr_err("Failed to create root dentry\n"); return -ENOMEM; }
files_count++;
disk[1] = alloc_block(1, mode); if (!disk[1]) { pr_err("Failed to alloc a disk block space\n"); return -ENOMEM; } root_inode->i_private = disk[1];
pr_info("tinyfs mounted successfully\n");
return 0; }
static struct dentry* tinyfs_mount(struct file_system_type* fs_type, int flags, const char* dev_name, void* data) {
return mount_nodev(fs_type, flags, data, tinyfs_fill_super); }
static void tinyfs_kill_superblock(struct super_block* sb) { kill_anon_super(sb); pr_info("tinyfs unmounted and resources cleaned up\n"); }
struct file_system_type tinyfs_fs_type = { .owner = THIS_MODULE, .name = "tinyfs", .mount = tinyfs_mount, .kill_sb = tinyfs_kill_superblock, };
static int tinyfs_init(void) { int ret;
init_disk();
ret = register_filesystem(&tinyfs_fs_type); if (ret) pr_err("Register tinyfs failed\n");
pr_info("tinyfs loaded\n"); return ret; }
static void tinyfs_exit(void) { unregister_filesystem(&tinyfs_fs_type);
for (uint8_t i = 0; i < MAX_FILES + 1; i++) { if (disk[i]) { kfree(disk[i]); disk[i] = NULL; } }
pr_info("tinyfs unloaded\n"); }
module_init(tinyfs_init); module_exit(tinyfs_exit);
MODULE_LICENSE("GPL");
|