1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * fs/bfs/inode.c 4 * BFS superblock and inode operations. 5 * Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com> 6 * From fs/minix, Copyright (C) 1991, 1992 Linus Torvalds. 7 * Made endianness-clean by Andrew Stribblehill <ads@wompom.org>, 2005. 8 */ 9 10 #include <linux/module.h> 11 #include <linux/mm.h> 12 #include <linux/slab.h> 13 #include <linux/init.h> 14 #include <linux/fs.h> 15 #include <linux/buffer_head.h> 16 #include <linux/vfs.h> 17 #include <linux/writeback.h> 18 #include <linux/uio.h> 19 #include <linux/uaccess.h> 20 #include <linux/fs_context.h> 21 #include "bfs.h" 22 23 MODULE_AUTHOR("Tigran Aivazian <aivazian.tigran@gmail.com>"); 24 MODULE_DESCRIPTION("SCO UnixWare BFS filesystem for Linux"); 25 MODULE_LICENSE("GPL"); 26 27 #undef DEBUG 28 29 #ifdef DEBUG 30 #define dprintf(x...) printf(x) 31 #else 32 #define dprintf(x...) 33 #endif 34 35 struct inode *bfs_iget(struct super_block *sb, unsigned long ino) 36 { 37 struct bfs_inode *di; 38 struct inode *inode; 39 struct buffer_head *bh; 40 int block, off; 41 42 inode = iget_locked(sb, ino); 43 if (!inode) 44 return ERR_PTR(-ENOMEM); 45 if (!(inode_state_read_once(inode) & I_NEW)) 46 return inode; 47 48 if ((ino < BFS_ROOT_INO) || (ino > BFS_SB(inode->i_sb)->si_lasti)) { 49 printf("Bad inode number %s:%08lx\n", inode->i_sb->s_id, ino); 50 goto error; 51 } 52 53 block = (ino - BFS_ROOT_INO) / BFS_INODES_PER_BLOCK + 1; 54 bh = sb_bread(inode->i_sb, block); 55 if (!bh) { 56 printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, 57 ino); 58 goto error; 59 } 60 61 off = (ino - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK; 62 di = (struct bfs_inode *)bh->b_data + off; 63 64 /* 65 * https://martin.hinner.info/fs/bfs/bfs-structure.html explains that 66 * BFS in SCO UnixWare environment used only lower 9 bits of di->i_mode 67 * value. This means that, although bfs_write_inode() saves whole 68 * inode->i_mode bits (which include S_IFMT bits and S_IS{UID,GID,VTX} 69 * bits), middle 7 bits of di->i_mode value can be garbage when these 70 * bits were not saved by bfs_write_inode(). 71 * Since we can't tell whether middle 7 bits are garbage, use only 72 * lower 12 bits (i.e. tolerate S_IS{UID,GID,VTX} bits possibly being 73 * garbage) and reconstruct S_IFMT bits for Linux environment from 74 * di->i_vtype value. 75 */ 76 inode->i_mode = 0x00000FFF & le32_to_cpu(di->i_mode); 77 if (le32_to_cpu(di->i_vtype) == BFS_VDIR) { 78 inode->i_mode |= S_IFDIR; 79 inode->i_op = &bfs_dir_inops; 80 inode->i_fop = &bfs_dir_operations; 81 } else if (le32_to_cpu(di->i_vtype) == BFS_VREG) { 82 inode->i_mode |= S_IFREG; 83 inode->i_op = &bfs_file_inops; 84 inode->i_fop = &bfs_file_operations; 85 inode->i_mapping->a_ops = &bfs_aops; 86 } else { 87 brelse(bh); 88 printf("Unknown vtype=%u %s:%08lx\n", 89 le32_to_cpu(di->i_vtype), inode->i_sb->s_id, ino); 90 goto error; 91 } 92 93 BFS_I(inode)->i_sblock = le32_to_cpu(di->i_sblock); 94 BFS_I(inode)->i_eblock = le32_to_cpu(di->i_eblock); 95 BFS_I(inode)->i_dsk_ino = le16_to_cpu(di->i_ino); 96 i_uid_write(inode, le32_to_cpu(di->i_uid)); 97 i_gid_write(inode, le32_to_cpu(di->i_gid)); 98 set_nlink(inode, le32_to_cpu(di->i_nlink)); 99 inode->i_size = BFS_FILESIZE(di); 100 inode->i_blocks = BFS_FILEBLOCKS(di); 101 inode_set_atime(inode, le32_to_cpu(di->i_atime), 0); 102 inode_set_mtime(inode, le32_to_cpu(di->i_mtime), 0); 103 inode_set_ctime(inode, le32_to_cpu(di->i_ctime), 0); 104 105 brelse(bh); 106 unlock_new_inode(inode); 107 return inode; 108 109 error: 110 iget_failed(inode); 111 return ERR_PTR(-EIO); 112 } 113 114 static struct bfs_inode *find_inode(struct super_block *sb, u16 ino, struct buffer_head **p) 115 { 116 if ((ino < BFS_ROOT_INO) || (ino > BFS_SB(sb)->si_lasti)) { 117 printf("Bad inode number %s:%08x\n", sb->s_id, ino); 118 return ERR_PTR(-EIO); 119 } 120 121 ino -= BFS_ROOT_INO; 122 123 *p = sb_bread(sb, 1 + ino / BFS_INODES_PER_BLOCK); 124 if (!*p) { 125 printf("Unable to read inode %s:%08x\n", sb->s_id, ino); 126 return ERR_PTR(-EIO); 127 } 128 129 return (struct bfs_inode *)(*p)->b_data + ino % BFS_INODES_PER_BLOCK; 130 } 131 132 static int bfs_write_inode(struct inode *inode, struct writeback_control *wbc) 133 { 134 struct bfs_sb_info *info = BFS_SB(inode->i_sb); 135 unsigned int ino = (u16)inode->i_ino; 136 unsigned long i_sblock; 137 struct bfs_inode *di; 138 struct buffer_head *bh; 139 140 dprintf("ino=%08x\n", ino); 141 142 di = find_inode(inode->i_sb, ino, &bh); 143 if (IS_ERR(di)) 144 return PTR_ERR(di); 145 146 mutex_lock(&info->bfs_lock); 147 148 if (ino == BFS_ROOT_INO) 149 di->i_vtype = cpu_to_le32(BFS_VDIR); 150 else 151 di->i_vtype = cpu_to_le32(BFS_VREG); 152 153 di->i_ino = cpu_to_le16(ino); 154 di->i_mode = cpu_to_le32(inode->i_mode); 155 di->i_uid = cpu_to_le32(i_uid_read(inode)); 156 di->i_gid = cpu_to_le32(i_gid_read(inode)); 157 di->i_nlink = cpu_to_le32(inode->i_nlink); 158 di->i_atime = cpu_to_le32(inode_get_atime_sec(inode)); 159 di->i_mtime = cpu_to_le32(inode_get_mtime_sec(inode)); 160 di->i_ctime = cpu_to_le32(inode_get_ctime_sec(inode)); 161 i_sblock = BFS_I(inode)->i_sblock; 162 di->i_sblock = cpu_to_le32(i_sblock); 163 di->i_eblock = cpu_to_le32(BFS_I(inode)->i_eblock); 164 di->i_eoffset = cpu_to_le32(i_sblock * BFS_BSIZE + inode->i_size - 1); 165 166 mark_buffer_dirty(bh); 167 brelse(bh); 168 mutex_unlock(&info->bfs_lock); 169 set_inode_metadata_writeback(inode); 170 return 0; 171 } 172 173 static int bfs_sync_inode_metadata(struct inode *inode, 174 struct writeback_control *wbc) 175 { 176 int err = 0; 177 struct bfs_inode *di; 178 struct buffer_head *bh; 179 180 di = find_inode(inode->i_sb, (u16)inode->i_ino, &bh); 181 if (IS_ERR(di)) 182 return PTR_ERR(di); 183 184 sync_dirty_buffer(bh); 185 if (buffer_write_io_error(bh)) { 186 err = -EIO; 187 goto out; 188 } 189 err = mmb_sync(&BFS_I(inode)->i_metadata_bhs); 190 out: 191 brelse(bh); 192 return err; 193 } 194 195 static void bfs_evict_inode(struct inode *inode) 196 { 197 unsigned long ino = inode->i_ino; 198 struct bfs_inode *di; 199 struct buffer_head *bh; 200 struct super_block *s = inode->i_sb; 201 struct bfs_sb_info *info = BFS_SB(s); 202 struct bfs_inode_info *bi = BFS_I(inode); 203 204 dprintf("ino=%08lx\n", ino); 205 206 truncate_inode_pages_final(&inode->i_data); 207 if (inode->i_nlink) 208 mmb_sync(&BFS_I(inode)->i_metadata_bhs); 209 mmb_invalidate(&BFS_I(inode)->i_metadata_bhs); 210 clear_inode(inode); 211 212 if (inode->i_nlink) 213 return; 214 215 di = find_inode(s, inode->i_ino, &bh); 216 if (IS_ERR(di)) 217 return; 218 219 mutex_lock(&info->bfs_lock); 220 /* clear on-disk inode */ 221 memset(di, 0, sizeof(struct bfs_inode)); 222 mark_buffer_dirty(bh); 223 brelse(bh); 224 225 if (bi->i_dsk_ino) { 226 if (bi->i_sblock) 227 info->si_freeb += bi->i_eblock + 1 - bi->i_sblock; 228 info->si_freei++; 229 clear_bit(ino, info->si_imap); 230 bfs_dump_imap("evict_inode", s); 231 } 232 233 /* 234 * If this was the last file, make the previous block 235 * "last block of the last file" even if there is no 236 * real file there, saves us 1 gap. 237 */ 238 if (info->si_lf_eblk == bi->i_eblock) 239 info->si_lf_eblk = bi->i_sblock - 1; 240 mutex_unlock(&info->bfs_lock); 241 } 242 243 static void bfs_put_super(struct super_block *s) 244 { 245 struct bfs_sb_info *info = BFS_SB(s); 246 247 if (!info) 248 return; 249 250 mutex_destroy(&info->bfs_lock); 251 kfree(info); 252 s->s_fs_info = NULL; 253 } 254 255 static int bfs_statfs(struct dentry *dentry, struct kstatfs *buf) 256 { 257 struct super_block *s = dentry->d_sb; 258 struct bfs_sb_info *info = BFS_SB(s); 259 u64 id = huge_encode_dev(s->s_bdev->bd_dev); 260 buf->f_type = BFS_MAGIC; 261 buf->f_bsize = s->s_blocksize; 262 buf->f_blocks = info->si_blocks; 263 buf->f_bfree = buf->f_bavail = info->si_freeb; 264 buf->f_files = info->si_lasti + 1 - BFS_ROOT_INO; 265 buf->f_ffree = info->si_freei; 266 buf->f_fsid = u64_to_fsid(id); 267 buf->f_namelen = BFS_NAMELEN; 268 return 0; 269 } 270 271 static struct kmem_cache *bfs_inode_cachep; 272 273 static struct inode *bfs_alloc_inode(struct super_block *sb) 274 { 275 struct bfs_inode_info *bi; 276 bi = alloc_inode_sb(sb, bfs_inode_cachep, GFP_KERNEL); 277 if (!bi) 278 return NULL; 279 mmb_init(&bi->i_metadata_bhs, &bi->vfs_inode.i_data); 280 281 return &bi->vfs_inode; 282 } 283 284 static void bfs_free_inode(struct inode *inode) 285 { 286 kmem_cache_free(bfs_inode_cachep, BFS_I(inode)); 287 } 288 289 static void init_once(void *foo) 290 { 291 struct bfs_inode_info *bi = foo; 292 293 inode_init_once(&bi->vfs_inode); 294 } 295 296 static int __init init_inodecache(void) 297 { 298 bfs_inode_cachep = kmem_cache_create("bfs_inode_cache", 299 sizeof(struct bfs_inode_info), 300 0, (SLAB_RECLAIM_ACCOUNT| 301 SLAB_ACCOUNT), 302 init_once); 303 if (bfs_inode_cachep == NULL) 304 return -ENOMEM; 305 return 0; 306 } 307 308 static void destroy_inodecache(void) 309 { 310 /* 311 * Make sure all delayed rcu free inodes are flushed before we 312 * destroy cache. 313 */ 314 rcu_barrier(); 315 kmem_cache_destroy(bfs_inode_cachep); 316 } 317 318 static const struct super_operations bfs_sops = { 319 .alloc_inode = bfs_alloc_inode, 320 .free_inode = bfs_free_inode, 321 .write_inode = bfs_write_inode, 322 .sync_inode_metadata = bfs_sync_inode_metadata, 323 .evict_inode = bfs_evict_inode, 324 .put_super = bfs_put_super, 325 .statfs = bfs_statfs, 326 }; 327 328 void bfs_dump_imap(const char *prefix, struct super_block *s) 329 { 330 #ifdef DEBUG 331 int i; 332 char *tmpbuf = kzalloc(PAGE_SIZE, GFP_KERNEL); 333 334 if (!tmpbuf) 335 return; 336 for (i = BFS_SB(s)->si_lasti; i >= 0; i--) { 337 if (i > PAGE_SIZE - 100) break; 338 if (test_bit(i, BFS_SB(s)->si_imap)) 339 strcat(tmpbuf, "1"); 340 else 341 strcat(tmpbuf, "0"); 342 } 343 printf("%s: lasti=%08lx <%s>\n", prefix, BFS_SB(s)->si_lasti, tmpbuf); 344 kfree(tmpbuf); 345 #endif 346 } 347 348 static int bfs_fill_super(struct super_block *s, struct fs_context *fc) 349 { 350 struct buffer_head *bh, *sbh; 351 struct bfs_super_block *bfs_sb; 352 struct inode *inode; 353 unsigned i; 354 struct bfs_sb_info *info; 355 int ret = -EINVAL; 356 unsigned long i_sblock, i_eblock, i_eoff, s_size; 357 int silent = fc->sb_flags & SB_SILENT; 358 359 info = kzalloc_obj(*info); 360 if (!info) 361 return -ENOMEM; 362 mutex_init(&info->bfs_lock); 363 s->s_fs_info = info; 364 s->s_time_min = 0; 365 s->s_time_max = U32_MAX; 366 367 if (!sb_set_blocksize(s, BFS_BSIZE)) 368 goto out; 369 370 sbh = sb_bread(s, 0); 371 if (!sbh) 372 goto out; 373 bfs_sb = (struct bfs_super_block *)sbh->b_data; 374 if (le32_to_cpu(bfs_sb->s_magic) != BFS_MAGIC) { 375 if (!silent) 376 printf("No BFS filesystem on %s (magic=%08x)\n", s->s_id, le32_to_cpu(bfs_sb->s_magic)); 377 goto out1; 378 } 379 if (BFS_UNCLEAN(bfs_sb, s) && !silent) 380 printf("%s is unclean, continuing\n", s->s_id); 381 382 s->s_magic = BFS_MAGIC; 383 384 if (le32_to_cpu(bfs_sb->s_start) > le32_to_cpu(bfs_sb->s_end) || 385 le32_to_cpu(bfs_sb->s_start) < sizeof(struct bfs_super_block) + sizeof(struct bfs_dirent)) { 386 printf("Superblock is corrupted on %s\n", s->s_id); 387 goto out1; 388 } 389 390 info->si_lasti = (le32_to_cpu(bfs_sb->s_start) - BFS_BSIZE) / sizeof(struct bfs_inode) + BFS_ROOT_INO - 1; 391 if (info->si_lasti == BFS_MAX_LASTI) 392 printf("NOTE: filesystem %s was created with 512 inodes, the real maximum is 511, mounting anyway\n", s->s_id); 393 else if (info->si_lasti > BFS_MAX_LASTI) { 394 printf("Impossible last inode number %lu > %d on %s\n", info->si_lasti, BFS_MAX_LASTI, s->s_id); 395 goto out1; 396 } 397 for (i = 0; i < BFS_ROOT_INO; i++) 398 set_bit(i, info->si_imap); 399 400 s->s_op = &bfs_sops; 401 inode = bfs_iget(s, BFS_ROOT_INO); 402 if (IS_ERR(inode)) { 403 ret = PTR_ERR(inode); 404 goto out1; 405 } 406 s->s_root = d_make_root(inode); 407 if (!s->s_root) { 408 ret = -ENOMEM; 409 goto out1; 410 } 411 412 info->si_blocks = (le32_to_cpu(bfs_sb->s_end) + 1) >> BFS_BSIZE_BITS; 413 info->si_freeb = (le32_to_cpu(bfs_sb->s_end) + 1 - le32_to_cpu(bfs_sb->s_start)) >> BFS_BSIZE_BITS; 414 info->si_freei = 0; 415 info->si_lf_eblk = 0; 416 417 /* can we read the last block? */ 418 bh = sb_bread(s, info->si_blocks - 1); 419 if (!bh) { 420 printf("Last block not available on %s: %lu\n", s->s_id, info->si_blocks - 1); 421 ret = -EIO; 422 goto out2; 423 } 424 brelse(bh); 425 426 bh = NULL; 427 for (i = BFS_ROOT_INO; i <= info->si_lasti; i++) { 428 struct bfs_inode *di; 429 int block = (i - BFS_ROOT_INO) / BFS_INODES_PER_BLOCK + 1; 430 int off = (i - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK; 431 unsigned long eblock; 432 433 if (!off) { 434 brelse(bh); 435 bh = sb_bread(s, block); 436 } 437 438 if (!bh) 439 continue; 440 441 di = (struct bfs_inode *)bh->b_data + off; 442 443 /* test if filesystem is not corrupted */ 444 445 i_eoff = le32_to_cpu(di->i_eoffset); 446 i_sblock = le32_to_cpu(di->i_sblock); 447 i_eblock = le32_to_cpu(di->i_eblock); 448 s_size = le32_to_cpu(bfs_sb->s_end); 449 450 if (i_sblock > info->si_blocks || 451 i_eblock > info->si_blocks || 452 i_sblock > i_eblock || 453 (i_eoff != le32_to_cpu(-1) && i_eoff > s_size) || 454 i_sblock * BFS_BSIZE > i_eoff) { 455 456 printf("Inode 0x%08x corrupted on %s\n", i, s->s_id); 457 458 brelse(bh); 459 ret = -EIO; 460 goto out2; 461 } 462 463 if (!di->i_ino) { 464 info->si_freei++; 465 continue; 466 } 467 set_bit(i, info->si_imap); 468 info->si_freeb -= BFS_FILEBLOCKS(di); 469 470 eblock = le32_to_cpu(di->i_eblock); 471 if (eblock > info->si_lf_eblk) 472 info->si_lf_eblk = eblock; 473 } 474 brelse(bh); 475 brelse(sbh); 476 bfs_dump_imap("fill_super", s); 477 return 0; 478 479 out2: 480 dput(s->s_root); 481 s->s_root = NULL; 482 out1: 483 brelse(sbh); 484 out: 485 mutex_destroy(&info->bfs_lock); 486 kfree(info); 487 s->s_fs_info = NULL; 488 return ret; 489 } 490 491 static int bfs_get_tree(struct fs_context *fc) 492 { 493 return get_tree_bdev(fc, bfs_fill_super); 494 } 495 496 static const struct fs_context_operations bfs_context_ops = { 497 .get_tree = bfs_get_tree, 498 }; 499 500 static int bfs_init_fs_context(struct fs_context *fc) 501 { 502 fc->ops = &bfs_context_ops; 503 504 return 0; 505 } 506 507 static struct file_system_type bfs_fs_type = { 508 .owner = THIS_MODULE, 509 .name = "bfs", 510 .init_fs_context = bfs_init_fs_context, 511 .kill_sb = kill_block_super, 512 .fs_flags = FS_REQUIRES_DEV, 513 }; 514 MODULE_ALIAS_FS("bfs"); 515 516 static int __init init_bfs_fs(void) 517 { 518 int err = init_inodecache(); 519 if (err) 520 goto out1; 521 err = register_filesystem(&bfs_fs_type); 522 if (err) 523 goto out; 524 return 0; 525 out: 526 destroy_inodecache(); 527 out1: 528 return err; 529 } 530 531 static void __exit exit_bfs_fs(void) 532 { 533 unregister_filesystem(&bfs_fs_type); 534 destroy_inodecache(); 535 } 536 537 module_init(init_bfs_fs) 538 module_exit(exit_bfs_fs) 539