1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Optimized MPEG FS - inode and super operations. 4 * Copyright (C) 2006 Bob Copeland <me@bobcopeland.com> 5 */ 6 #include <linux/module.h> 7 #include <linux/sched.h> 8 #include <linux/slab.h> 9 #include <linux/fs.h> 10 #include <linux/vfs.h> 11 #include <linux/cred.h> 12 #include <linux/buffer_head.h> 13 #include <linux/vmalloc.h> 14 #include <linux/writeback.h> 15 #include <linux/seq_file.h> 16 #include <linux/crc-itu-t.h> 17 #include <linux/fs_context.h> 18 #include <linux/fs_parser.h> 19 #include "omfs.h" 20 21 MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>"); 22 MODULE_DESCRIPTION("OMFS (ReplayTV/Karma) Filesystem for Linux"); 23 MODULE_LICENSE("GPL"); 24 25 struct buffer_head *omfs_bread(struct super_block *sb, sector_t block) 26 { 27 struct omfs_sb_info *sbi = OMFS_SB(sb); 28 if (block >= sbi->s_num_blocks) 29 return NULL; 30 31 return sb_bread(sb, clus_to_blk(sbi, block)); 32 } 33 34 struct inode *omfs_new_inode(struct inode *dir, umode_t mode) 35 { 36 struct inode *inode; 37 u64 new_block; 38 int err; 39 int len; 40 struct omfs_sb_info *sbi = OMFS_SB(dir->i_sb); 41 42 inode = new_inode(dir->i_sb); 43 if (!inode) 44 return ERR_PTR(-ENOMEM); 45 46 err = omfs_allocate_range(dir->i_sb, sbi->s_mirrors, sbi->s_mirrors, 47 &new_block, &len); 48 if (err) 49 goto fail; 50 51 inode->i_ino = new_block; 52 inode_init_owner(&nop_mnt_idmap, inode, NULL, mode); 53 inode->i_mapping->a_ops = &omfs_aops; 54 55 simple_inode_init_ts(inode); 56 switch (mode & S_IFMT) { 57 case S_IFDIR: 58 inode->i_op = &omfs_dir_inops; 59 inode->i_fop = &omfs_dir_operations; 60 inode->i_size = sbi->s_sys_blocksize; 61 inc_nlink(inode); 62 break; 63 case S_IFREG: 64 inode->i_op = &omfs_file_inops; 65 inode->i_fop = &omfs_file_operations; 66 inode->i_size = 0; 67 break; 68 } 69 70 insert_inode_hash(inode); 71 mark_inode_dirty(inode); 72 return inode; 73 fail: 74 make_bad_inode(inode); 75 iput(inode); 76 return ERR_PTR(err); 77 } 78 79 /* 80 * Update the header checksums for a dirty inode based on its contents. 81 * Caller is expected to hold the buffer head underlying oi and mark it 82 * dirty. 83 */ 84 static void omfs_update_checksums(struct omfs_inode *oi) 85 { 86 int xor, i, ofs = 0, count; 87 u16 crc = 0; 88 unsigned char *ptr = (unsigned char *) oi; 89 90 count = be32_to_cpu(oi->i_head.h_body_size); 91 ofs = sizeof(struct omfs_header); 92 93 crc = crc_itu_t(crc, ptr + ofs, count); 94 oi->i_head.h_crc = cpu_to_be16(crc); 95 96 xor = ptr[0]; 97 for (i = 1; i < OMFS_XOR_COUNT; i++) 98 xor ^= ptr[i]; 99 100 oi->i_head.h_check_xor = xor; 101 } 102 103 static int __omfs_write_inode(struct inode *inode, int wait) 104 { 105 struct omfs_inode *oi; 106 struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb); 107 struct buffer_head *bh, *bh2; 108 u64 ctime; 109 int i; 110 int ret = -EIO; 111 int sync_failed = 0; 112 113 /* get current inode since we may have written sibling ptrs etc. */ 114 bh = omfs_bread(inode->i_sb, inode->i_ino); 115 if (!bh) 116 goto out; 117 118 oi = (struct omfs_inode *) bh->b_data; 119 120 oi->i_head.h_self = cpu_to_be64(inode->i_ino); 121 if (S_ISDIR(inode->i_mode)) 122 oi->i_type = OMFS_DIR; 123 else if (S_ISREG(inode->i_mode)) 124 oi->i_type = OMFS_FILE; 125 else { 126 printk(KERN_WARNING "omfs: unknown file type: %d\n", 127 inode->i_mode); 128 goto out_brelse; 129 } 130 131 oi->i_head.h_body_size = cpu_to_be32(sbi->s_sys_blocksize - 132 sizeof(struct omfs_header)); 133 oi->i_head.h_version = 1; 134 oi->i_head.h_type = OMFS_INODE_NORMAL; 135 oi->i_head.h_magic = OMFS_IMAGIC; 136 oi->i_size = cpu_to_be64(inode->i_size); 137 138 ctime = inode_get_ctime_sec(inode) * 1000LL + 139 ((inode_get_ctime_nsec(inode) + 999)/1000); 140 oi->i_ctime = cpu_to_be64(ctime); 141 142 omfs_update_checksums(oi); 143 144 mark_buffer_dirty(bh); 145 if (wait) { 146 sync_dirty_buffer(bh); 147 if (buffer_req(bh) && !buffer_uptodate(bh)) 148 sync_failed = 1; 149 } 150 151 /* if mirroring writes, copy to next fsblock */ 152 for (i = 1; i < sbi->s_mirrors; i++) { 153 bh2 = omfs_bread(inode->i_sb, inode->i_ino + i); 154 if (!bh2) 155 goto out_brelse; 156 157 memcpy(bh2->b_data, bh->b_data, bh->b_size); 158 mark_buffer_dirty(bh2); 159 if (wait) { 160 sync_dirty_buffer(bh2); 161 if (buffer_req(bh2) && !buffer_uptodate(bh2)) 162 sync_failed = 1; 163 } 164 brelse(bh2); 165 } 166 ret = (sync_failed) ? -EIO : 0; 167 out_brelse: 168 brelse(bh); 169 out: 170 return ret; 171 } 172 173 static int omfs_write_inode(struct inode *inode, struct writeback_control *wbc) 174 { 175 return __omfs_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 176 } 177 178 int omfs_sync_inode(struct inode *inode) 179 { 180 return __omfs_write_inode(inode, 1); 181 } 182 183 /* 184 * called when an entry is deleted, need to clear the bits in the 185 * bitmaps. 186 */ 187 static void omfs_evict_inode(struct inode *inode) 188 { 189 truncate_inode_pages_final(&inode->i_data); 190 clear_inode(inode); 191 192 if (inode->i_nlink) 193 return; 194 195 if (S_ISREG(inode->i_mode)) { 196 inode->i_size = 0; 197 omfs_shrink_inode(inode); 198 } 199 200 omfs_clear_range(inode->i_sb, inode->i_ino, 2); 201 } 202 203 struct inode *omfs_iget(struct super_block *sb, ino_t ino) 204 { 205 struct omfs_sb_info *sbi = OMFS_SB(sb); 206 struct omfs_inode *oi; 207 struct buffer_head *bh; 208 u64 ctime; 209 unsigned long nsecs; 210 struct inode *inode; 211 212 inode = iget_locked(sb, ino); 213 if (!inode) 214 return ERR_PTR(-ENOMEM); 215 if (!(inode->i_state & I_NEW)) 216 return inode; 217 218 bh = omfs_bread(inode->i_sb, ino); 219 if (!bh) 220 goto iget_failed; 221 222 oi = (struct omfs_inode *)bh->b_data; 223 224 /* check self */ 225 if (ino != be64_to_cpu(oi->i_head.h_self)) 226 goto fail_bh; 227 228 inode->i_uid = sbi->s_uid; 229 inode->i_gid = sbi->s_gid; 230 231 ctime = be64_to_cpu(oi->i_ctime); 232 nsecs = do_div(ctime, 1000) * 1000L; 233 234 inode_set_atime(inode, ctime, nsecs); 235 inode_set_mtime(inode, ctime, nsecs); 236 inode_set_ctime(inode, ctime, nsecs); 237 238 inode->i_mapping->a_ops = &omfs_aops; 239 240 switch (oi->i_type) { 241 case OMFS_DIR: 242 inode->i_mode = S_IFDIR | (S_IRWXUGO & ~sbi->s_dmask); 243 inode->i_op = &omfs_dir_inops; 244 inode->i_fop = &omfs_dir_operations; 245 inode->i_size = sbi->s_sys_blocksize; 246 inc_nlink(inode); 247 break; 248 case OMFS_FILE: 249 inode->i_mode = S_IFREG | (S_IRWXUGO & ~sbi->s_fmask); 250 inode->i_fop = &omfs_file_operations; 251 inode->i_size = be64_to_cpu(oi->i_size); 252 break; 253 } 254 brelse(bh); 255 unlock_new_inode(inode); 256 return inode; 257 fail_bh: 258 brelse(bh); 259 iget_failed: 260 iget_failed(inode); 261 return ERR_PTR(-EIO); 262 } 263 264 static void omfs_put_super(struct super_block *sb) 265 { 266 struct omfs_sb_info *sbi = OMFS_SB(sb); 267 kfree(sbi->s_imap); 268 kfree(sbi); 269 sb->s_fs_info = NULL; 270 } 271 272 static int omfs_statfs(struct dentry *dentry, struct kstatfs *buf) 273 { 274 struct super_block *s = dentry->d_sb; 275 struct omfs_sb_info *sbi = OMFS_SB(s); 276 u64 id = huge_encode_dev(s->s_bdev->bd_dev); 277 278 buf->f_type = OMFS_MAGIC; 279 buf->f_bsize = sbi->s_blocksize; 280 buf->f_blocks = sbi->s_num_blocks; 281 buf->f_files = sbi->s_num_blocks; 282 buf->f_namelen = OMFS_NAMELEN; 283 buf->f_fsid = u64_to_fsid(id); 284 285 buf->f_bfree = buf->f_bavail = buf->f_ffree = 286 omfs_count_free(s); 287 288 return 0; 289 } 290 291 /* 292 * Display the mount options in /proc/mounts. 293 */ 294 static int omfs_show_options(struct seq_file *m, struct dentry *root) 295 { 296 struct omfs_sb_info *sbi = OMFS_SB(root->d_sb); 297 umode_t cur_umask = current_umask(); 298 299 if (!uid_eq(sbi->s_uid, current_uid())) 300 seq_printf(m, ",uid=%u", 301 from_kuid_munged(&init_user_ns, sbi->s_uid)); 302 if (!gid_eq(sbi->s_gid, current_gid())) 303 seq_printf(m, ",gid=%u", 304 from_kgid_munged(&init_user_ns, sbi->s_gid)); 305 306 if (sbi->s_dmask == sbi->s_fmask) { 307 if (sbi->s_fmask != cur_umask) 308 seq_printf(m, ",umask=%o", sbi->s_fmask); 309 } else { 310 if (sbi->s_dmask != cur_umask) 311 seq_printf(m, ",dmask=%o", sbi->s_dmask); 312 if (sbi->s_fmask != cur_umask) 313 seq_printf(m, ",fmask=%o", sbi->s_fmask); 314 } 315 316 return 0; 317 } 318 319 static const struct super_operations omfs_sops = { 320 .write_inode = omfs_write_inode, 321 .evict_inode = omfs_evict_inode, 322 .put_super = omfs_put_super, 323 .statfs = omfs_statfs, 324 .show_options = omfs_show_options, 325 }; 326 327 /* 328 * For Rio Karma, there is an on-disk free bitmap whose location is 329 * stored in the root block. For ReplayTV, there is no such free bitmap 330 * so we have to walk the tree. Both inodes and file data are allocated 331 * from the same map. This array can be big (300k) so we allocate 332 * in units of the blocksize. 333 */ 334 static int omfs_get_imap(struct super_block *sb) 335 { 336 unsigned int bitmap_size, array_size; 337 int count; 338 struct omfs_sb_info *sbi = OMFS_SB(sb); 339 struct buffer_head *bh; 340 unsigned long **ptr; 341 sector_t block; 342 343 bitmap_size = DIV_ROUND_UP(sbi->s_num_blocks, 8); 344 array_size = DIV_ROUND_UP(bitmap_size, sb->s_blocksize); 345 346 if (sbi->s_bitmap_ino == ~0ULL) 347 goto out; 348 349 sbi->s_imap_size = array_size; 350 sbi->s_imap = kcalloc(array_size, sizeof(unsigned long *), GFP_KERNEL); 351 if (!sbi->s_imap) 352 goto nomem; 353 354 block = clus_to_blk(sbi, sbi->s_bitmap_ino); 355 if (block >= sbi->s_num_blocks) 356 goto nomem; 357 358 ptr = sbi->s_imap; 359 for (count = bitmap_size; count > 0; count -= sb->s_blocksize) { 360 bh = sb_bread(sb, block++); 361 if (!bh) 362 goto nomem_free; 363 *ptr = kmemdup(bh->b_data, sb->s_blocksize, GFP_KERNEL); 364 if (!*ptr) { 365 brelse(bh); 366 goto nomem_free; 367 } 368 if (count < sb->s_blocksize) 369 memset((void *)*ptr + count, 0xff, 370 sb->s_blocksize - count); 371 brelse(bh); 372 ptr++; 373 } 374 out: 375 return 0; 376 377 nomem_free: 378 for (count = 0; count < array_size; count++) 379 kfree(sbi->s_imap[count]); 380 381 kfree(sbi->s_imap); 382 nomem: 383 sbi->s_imap = NULL; 384 sbi->s_imap_size = 0; 385 return -ENOMEM; 386 } 387 388 struct omfs_mount_options { 389 kuid_t s_uid; 390 kgid_t s_gid; 391 int s_dmask; 392 int s_fmask; 393 }; 394 395 enum { 396 Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask, 397 }; 398 399 static const struct fs_parameter_spec omfs_param_spec[] = { 400 fsparam_uid ("uid", Opt_uid), 401 fsparam_gid ("gid", Opt_gid), 402 fsparam_u32oct ("umask", Opt_umask), 403 fsparam_u32oct ("dmask", Opt_dmask), 404 fsparam_u32oct ("fmask", Opt_fmask), 405 {} 406 }; 407 408 static int 409 omfs_parse_param(struct fs_context *fc, struct fs_parameter *param) 410 { 411 struct omfs_mount_options *opts = fc->fs_private; 412 int token; 413 struct fs_parse_result result; 414 415 /* All options are ignored on remount */ 416 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) 417 return 0; 418 419 token = fs_parse(fc, omfs_param_spec, param, &result); 420 if (token < 0) 421 return token; 422 423 switch (token) { 424 case Opt_uid: 425 opts->s_uid = result.uid; 426 break; 427 case Opt_gid: 428 opts->s_gid = result.gid; 429 break; 430 case Opt_umask: 431 opts->s_fmask = opts->s_dmask = result.uint_32; 432 break; 433 case Opt_dmask: 434 opts->s_dmask = result.uint_32; 435 break; 436 case Opt_fmask: 437 opts->s_fmask = result.uint_32; 438 break; 439 default: 440 return -EINVAL; 441 } 442 443 return 0; 444 } 445 446 static void 447 omfs_set_options(struct omfs_sb_info *sbi, struct omfs_mount_options *opts) 448 { 449 sbi->s_uid = opts->s_uid; 450 sbi->s_gid = opts->s_gid; 451 sbi->s_dmask = opts->s_dmask; 452 sbi->s_fmask = opts->s_fmask; 453 } 454 455 static int omfs_fill_super(struct super_block *sb, struct fs_context *fc) 456 { 457 struct buffer_head *bh, *bh2; 458 struct omfs_super_block *omfs_sb; 459 struct omfs_root_block *omfs_rb; 460 struct omfs_sb_info *sbi; 461 struct inode *root; 462 struct omfs_mount_options *parsed_opts = fc->fs_private; 463 int ret = -EINVAL; 464 int silent = fc->sb_flags & SB_SILENT; 465 466 sbi = kzalloc(sizeof(struct omfs_sb_info), GFP_KERNEL); 467 if (!sbi) 468 return -ENOMEM; 469 470 sb->s_fs_info = sbi; 471 472 omfs_set_options(sbi, parsed_opts); 473 474 sb->s_maxbytes = 0xffffffff; 475 476 sb->s_time_gran = NSEC_PER_MSEC; 477 sb->s_time_min = 0; 478 sb->s_time_max = U64_MAX / MSEC_PER_SEC; 479 480 sb_set_blocksize(sb, 0x200); 481 482 bh = sb_bread(sb, 0); 483 if (!bh) 484 goto end; 485 486 omfs_sb = (struct omfs_super_block *)bh->b_data; 487 488 if (omfs_sb->s_magic != cpu_to_be32(OMFS_MAGIC)) { 489 if (!silent) 490 printk(KERN_ERR "omfs: Invalid superblock (%x)\n", 491 omfs_sb->s_magic); 492 goto out_brelse_bh; 493 } 494 sb->s_magic = OMFS_MAGIC; 495 496 sbi->s_num_blocks = be64_to_cpu(omfs_sb->s_num_blocks); 497 sbi->s_blocksize = be32_to_cpu(omfs_sb->s_blocksize); 498 sbi->s_mirrors = be32_to_cpu(omfs_sb->s_mirrors); 499 sbi->s_root_ino = be64_to_cpu(omfs_sb->s_root_block); 500 sbi->s_sys_blocksize = be32_to_cpu(omfs_sb->s_sys_blocksize); 501 mutex_init(&sbi->s_bitmap_lock); 502 503 if (sbi->s_num_blocks > OMFS_MAX_BLOCKS) { 504 printk(KERN_ERR "omfs: sysblock number (%llx) is out of range\n", 505 (unsigned long long)sbi->s_num_blocks); 506 goto out_brelse_bh; 507 } 508 509 if (sbi->s_sys_blocksize > PAGE_SIZE) { 510 printk(KERN_ERR "omfs: sysblock size (%d) is out of range\n", 511 sbi->s_sys_blocksize); 512 goto out_brelse_bh; 513 } 514 515 if (sbi->s_blocksize < sbi->s_sys_blocksize || 516 sbi->s_blocksize > OMFS_MAX_BLOCK_SIZE) { 517 printk(KERN_ERR "omfs: block size (%d) is out of range\n", 518 sbi->s_blocksize); 519 goto out_brelse_bh; 520 } 521 522 /* 523 * Use sys_blocksize as the fs block since it is smaller than a 524 * page while the fs blocksize can be larger. 525 */ 526 sb_set_blocksize(sb, sbi->s_sys_blocksize); 527 528 /* 529 * ...and the difference goes into a shift. sys_blocksize is always 530 * a power of two factor of blocksize. 531 */ 532 sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) - 533 get_bitmask_order(sbi->s_sys_blocksize); 534 535 bh2 = omfs_bread(sb, be64_to_cpu(omfs_sb->s_root_block)); 536 if (!bh2) 537 goto out_brelse_bh; 538 539 omfs_rb = (struct omfs_root_block *)bh2->b_data; 540 541 sbi->s_bitmap_ino = be64_to_cpu(omfs_rb->r_bitmap); 542 sbi->s_clustersize = be32_to_cpu(omfs_rb->r_clustersize); 543 544 if (sbi->s_num_blocks != be64_to_cpu(omfs_rb->r_num_blocks)) { 545 printk(KERN_ERR "omfs: block count discrepancy between " 546 "super and root blocks (%llx, %llx)\n", 547 (unsigned long long)sbi->s_num_blocks, 548 (unsigned long long)be64_to_cpu(omfs_rb->r_num_blocks)); 549 goto out_brelse_bh2; 550 } 551 552 if (sbi->s_bitmap_ino != ~0ULL && 553 sbi->s_bitmap_ino > sbi->s_num_blocks) { 554 printk(KERN_ERR "omfs: free space bitmap location is corrupt " 555 "(%llx, total blocks %llx)\n", 556 (unsigned long long) sbi->s_bitmap_ino, 557 (unsigned long long) sbi->s_num_blocks); 558 goto out_brelse_bh2; 559 } 560 if (sbi->s_clustersize < 1 || 561 sbi->s_clustersize > OMFS_MAX_CLUSTER_SIZE) { 562 printk(KERN_ERR "omfs: cluster size out of range (%d)", 563 sbi->s_clustersize); 564 goto out_brelse_bh2; 565 } 566 567 ret = omfs_get_imap(sb); 568 if (ret) 569 goto out_brelse_bh2; 570 571 sb->s_op = &omfs_sops; 572 573 root = omfs_iget(sb, be64_to_cpu(omfs_rb->r_root_dir)); 574 if (IS_ERR(root)) { 575 ret = PTR_ERR(root); 576 goto out_brelse_bh2; 577 } 578 579 sb->s_root = d_make_root(root); 580 if (!sb->s_root) { 581 ret = -ENOMEM; 582 goto out_brelse_bh2; 583 } 584 printk(KERN_DEBUG "omfs: Mounted volume %s\n", omfs_rb->r_name); 585 586 ret = 0; 587 out_brelse_bh2: 588 brelse(bh2); 589 out_brelse_bh: 590 brelse(bh); 591 end: 592 if (ret) 593 kfree(sbi); 594 return ret; 595 } 596 597 static int omfs_get_tree(struct fs_context *fc) 598 { 599 return get_tree_bdev(fc, omfs_fill_super); 600 } 601 602 static void omfs_free_fc(struct fs_context *fc); 603 604 static const struct fs_context_operations omfs_context_ops = { 605 .parse_param = omfs_parse_param, 606 .get_tree = omfs_get_tree, 607 .free = omfs_free_fc, 608 }; 609 610 static int omfs_init_fs_context(struct fs_context *fc) 611 { 612 struct omfs_mount_options *opts; 613 614 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 615 if (!opts) 616 return -ENOMEM; 617 618 /* Set mount options defaults */ 619 opts->s_uid = current_uid(); 620 opts->s_gid = current_gid(); 621 opts->s_dmask = opts->s_fmask = current_umask(); 622 623 fc->fs_private = opts; 624 fc->ops = &omfs_context_ops; 625 626 return 0; 627 } 628 629 static void omfs_free_fc(struct fs_context *fc) 630 { 631 kfree(fc->fs_private); 632 } 633 634 static struct file_system_type omfs_fs_type = { 635 .owner = THIS_MODULE, 636 .name = "omfs", 637 .kill_sb = kill_block_super, 638 .fs_flags = FS_REQUIRES_DEV, 639 .init_fs_context = omfs_init_fs_context, 640 .parameters = omfs_param_spec, 641 }; 642 MODULE_ALIAS_FS("omfs"); 643 644 static int __init init_omfs_fs(void) 645 { 646 return register_filesystem(&omfs_fs_type); 647 } 648 649 static void __exit exit_omfs_fs(void) 650 { 651 unregister_filesystem(&omfs_fs_type); 652 } 653 654 module_init(init_omfs_fs); 655 module_exit(exit_omfs_fs); 656