1 /* 2 * QNX4 file system, Linux implementation. 3 * 4 * Version : 0.2.1 5 * 6 * Using parts of the xiafs filesystem. 7 * 8 * History : 9 * 10 * 01-06-1998 by Richard Frowijn : first release. 11 * 20-06-1998 by Frank Denis : Linux 2.1.99+ support, boot signature, misc. 12 * 30-06-1998 by Frank Denis : first step to write inodes. 13 */ 14 15 #include <linux/module.h> 16 #include <linux/types.h> 17 #include <linux/string.h> 18 #include <linux/errno.h> 19 #include <linux/slab.h> 20 #include <linux/fs.h> 21 #include <linux/qnx4_fs.h> 22 #include <linux/init.h> 23 #include <linux/highuid.h> 24 #include <linux/smp_lock.h> 25 #include <linux/pagemap.h> 26 #include <linux/buffer_head.h> 27 #include <linux/vfs.h> 28 #include <asm/uaccess.h> 29 30 #define QNX4_VERSION 4 31 #define QNX4_BMNAME ".bitmap" 32 33 static const struct super_operations qnx4_sops; 34 35 #ifdef CONFIG_QNX4FS_RW 36 37 int qnx4_sync_inode(struct inode *inode) 38 { 39 int err = 0; 40 # if 0 41 struct buffer_head *bh; 42 43 bh = qnx4_update_inode(inode); 44 if (bh && buffer_dirty(bh)) 45 { 46 sync_dirty_buffer(bh); 47 if (buffer_req(bh) && !buffer_uptodate(bh)) 48 { 49 printk ("IO error syncing qnx4 inode [%s:%08lx]\n", 50 inode->i_sb->s_id, inode->i_ino); 51 err = -1; 52 } 53 brelse (bh); 54 } else if (!bh) { 55 err = -1; 56 } 57 # endif 58 59 return err; 60 } 61 62 static void qnx4_delete_inode(struct inode *inode) 63 { 64 QNX4DEBUG(("qnx4: deleting inode [%lu]\n", (unsigned long) inode->i_ino)); 65 truncate_inode_pages(&inode->i_data, 0); 66 inode->i_size = 0; 67 qnx4_truncate(inode); 68 lock_kernel(); 69 qnx4_free_inode(inode); 70 unlock_kernel(); 71 } 72 73 static void qnx4_write_super(struct super_block *sb) 74 { 75 lock_kernel(); 76 QNX4DEBUG(("qnx4: write_super\n")); 77 sb->s_dirt = 0; 78 unlock_kernel(); 79 } 80 81 static int qnx4_write_inode(struct inode *inode, int unused) 82 { 83 struct qnx4_inode_entry *raw_inode; 84 int block, ino; 85 struct buffer_head *bh; 86 ino = inode->i_ino; 87 88 QNX4DEBUG(("qnx4: write inode 1.\n")); 89 if (inode->i_nlink == 0) { 90 return 0; 91 } 92 if (!ino) { 93 printk("qnx4: bad inode number on dev %s: %d is out of range\n", 94 inode->i_sb->s_id, ino); 95 return -EIO; 96 } 97 QNX4DEBUG(("qnx4: write inode 2.\n")); 98 block = ino / QNX4_INODES_PER_BLOCK; 99 lock_kernel(); 100 if (!(bh = sb_bread(inode->i_sb, block))) { 101 printk("qnx4: major problem: unable to read inode from dev " 102 "%s\n", inode->i_sb->s_id); 103 unlock_kernel(); 104 return -EIO; 105 } 106 raw_inode = ((struct qnx4_inode_entry *) bh->b_data) + 107 (ino % QNX4_INODES_PER_BLOCK); 108 raw_inode->di_mode = cpu_to_le16(inode->i_mode); 109 raw_inode->di_uid = cpu_to_le16(fs_high2lowuid(inode->i_uid)); 110 raw_inode->di_gid = cpu_to_le16(fs_high2lowgid(inode->i_gid)); 111 raw_inode->di_nlink = cpu_to_le16(inode->i_nlink); 112 raw_inode->di_size = cpu_to_le32(inode->i_size); 113 raw_inode->di_mtime = cpu_to_le32(inode->i_mtime.tv_sec); 114 raw_inode->di_atime = cpu_to_le32(inode->i_atime.tv_sec); 115 raw_inode->di_ctime = cpu_to_le32(inode->i_ctime.tv_sec); 116 raw_inode->di_first_xtnt.xtnt_size = cpu_to_le32(inode->i_blocks); 117 mark_buffer_dirty(bh); 118 brelse(bh); 119 unlock_kernel(); 120 return 0; 121 } 122 123 #endif 124 125 static void qnx4_put_super(struct super_block *sb); 126 static struct inode *qnx4_alloc_inode(struct super_block *sb); 127 static void qnx4_destroy_inode(struct inode *inode); 128 static int qnx4_remount(struct super_block *sb, int *flags, char *data); 129 static int qnx4_statfs(struct dentry *, struct kstatfs *); 130 131 static const struct super_operations qnx4_sops = 132 { 133 .alloc_inode = qnx4_alloc_inode, 134 .destroy_inode = qnx4_destroy_inode, 135 .put_super = qnx4_put_super, 136 .statfs = qnx4_statfs, 137 .remount_fs = qnx4_remount, 138 #ifdef CONFIG_QNX4FS_RW 139 .write_inode = qnx4_write_inode, 140 .delete_inode = qnx4_delete_inode, 141 .write_super = qnx4_write_super, 142 #endif 143 }; 144 145 static int qnx4_remount(struct super_block *sb, int *flags, char *data) 146 { 147 struct qnx4_sb_info *qs; 148 149 qs = qnx4_sb(sb); 150 qs->Version = QNX4_VERSION; 151 #ifndef CONFIG_QNX4FS_RW 152 *flags |= MS_RDONLY; 153 #endif 154 if (*flags & MS_RDONLY) { 155 return 0; 156 } 157 158 mark_buffer_dirty(qs->sb_buf); 159 160 return 0; 161 } 162 163 static struct buffer_head *qnx4_getblk(struct inode *inode, int nr, 164 int create) 165 { 166 struct buffer_head *result = NULL; 167 168 if ( nr >= 0 ) 169 nr = qnx4_block_map( inode, nr ); 170 if (nr) { 171 result = sb_getblk(inode->i_sb, nr); 172 return result; 173 } 174 if (!create) { 175 return NULL; 176 } 177 #if 0 178 tmp = qnx4_new_block(inode->i_sb); 179 if (!tmp) { 180 return NULL; 181 } 182 result = sb_getblk(inode->i_sb, tmp); 183 if (tst) { 184 qnx4_free_block(inode->i_sb, tmp); 185 brelse(result); 186 goto repeat; 187 } 188 tst = tmp; 189 #endif 190 inode->i_ctime = CURRENT_TIME_SEC; 191 mark_inode_dirty(inode); 192 return result; 193 } 194 195 struct buffer_head *qnx4_bread(struct inode *inode, int block, int create) 196 { 197 struct buffer_head *bh; 198 199 bh = qnx4_getblk(inode, block, create); 200 if (!bh || buffer_uptodate(bh)) { 201 return bh; 202 } 203 ll_rw_block(READ, 1, &bh); 204 wait_on_buffer(bh); 205 if (buffer_uptodate(bh)) { 206 return bh; 207 } 208 brelse(bh); 209 210 return NULL; 211 } 212 213 static int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create ) 214 { 215 unsigned long phys; 216 217 QNX4DEBUG(("qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock)); 218 219 phys = qnx4_block_map( inode, iblock ); 220 if ( phys ) { 221 // logical block is before EOF 222 map_bh(bh, inode->i_sb, phys); 223 } else if ( create ) { 224 // to be done. 225 } 226 return 0; 227 } 228 229 unsigned long qnx4_block_map( struct inode *inode, long iblock ) 230 { 231 int ix; 232 long offset, i_xblk; 233 unsigned long block = 0; 234 struct buffer_head *bh = NULL; 235 struct qnx4_xblk *xblk = NULL; 236 struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode); 237 u16 nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts); 238 239 if ( iblock < le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size) ) { 240 // iblock is in the first extent. This is easy. 241 block = le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_blk) + iblock - 1; 242 } else { 243 // iblock is beyond first extent. We have to follow the extent chain. 244 i_xblk = le32_to_cpu(qnx4_inode->di_xblk); 245 offset = iblock - le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size); 246 ix = 0; 247 while ( --nxtnt > 0 ) { 248 if ( ix == 0 ) { 249 // read next xtnt block. 250 bh = sb_bread(inode->i_sb, i_xblk - 1); 251 if ( !bh ) { 252 QNX4DEBUG(("qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1)); 253 return -EIO; 254 } 255 xblk = (struct qnx4_xblk*)bh->b_data; 256 if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) { 257 QNX4DEBUG(("qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk)); 258 return -EIO; 259 } 260 } 261 if ( offset < le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size) ) { 262 // got it! 263 block = le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_blk) + offset - 1; 264 break; 265 } 266 offset -= le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size); 267 if ( ++ix >= xblk->xblk_num_xtnts ) { 268 i_xblk = le32_to_cpu(xblk->xblk_next_xblk); 269 ix = 0; 270 brelse( bh ); 271 bh = NULL; 272 } 273 } 274 if ( bh ) 275 brelse( bh ); 276 } 277 278 QNX4DEBUG(("qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block)); 279 return block; 280 } 281 282 static int qnx4_statfs(struct dentry *dentry, struct kstatfs *buf) 283 { 284 struct super_block *sb = dentry->d_sb; 285 u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 286 287 lock_kernel(); 288 289 buf->f_type = sb->s_magic; 290 buf->f_bsize = sb->s_blocksize; 291 buf->f_blocks = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8; 292 buf->f_bfree = qnx4_count_free_blocks(sb); 293 buf->f_bavail = buf->f_bfree; 294 buf->f_namelen = QNX4_NAME_MAX; 295 buf->f_fsid.val[0] = (u32)id; 296 buf->f_fsid.val[1] = (u32)(id >> 32); 297 298 unlock_kernel(); 299 300 return 0; 301 } 302 303 /* 304 * Check the root directory of the filesystem to make sure 305 * it really _is_ a qnx4 filesystem, and to check the size 306 * of the directory entry. 307 */ 308 static const char *qnx4_checkroot(struct super_block *sb) 309 { 310 struct buffer_head *bh; 311 struct qnx4_inode_entry *rootdir; 312 int rd, rl; 313 int i, j; 314 int found = 0; 315 316 if (*(qnx4_sb(sb)->sb->RootDir.di_fname) != '/') { 317 return "no qnx4 filesystem (no root dir)."; 318 } else { 319 QNX4DEBUG(("QNX4 filesystem found on dev %s.\n", sb->s_id)); 320 rd = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_blk) - 1; 321 rl = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_size); 322 for (j = 0; j < rl; j++) { 323 bh = sb_bread(sb, rd + j); /* root dir, first block */ 324 if (bh == NULL) { 325 return "unable to read root entry."; 326 } 327 for (i = 0; i < QNX4_INODES_PER_BLOCK; i++) { 328 rootdir = (struct qnx4_inode_entry *) (bh->b_data + i * QNX4_DIR_ENTRY_SIZE); 329 if (rootdir->di_fname != NULL) { 330 QNX4DEBUG(("Rootdir entry found : [%s]\n", rootdir->di_fname)); 331 if (!strncmp(rootdir->di_fname, QNX4_BMNAME, sizeof QNX4_BMNAME)) { 332 found = 1; 333 qnx4_sb(sb)->BitMap = kmalloc( sizeof( struct qnx4_inode_entry ), GFP_KERNEL ); 334 if (!qnx4_sb(sb)->BitMap) { 335 brelse (bh); 336 return "not enough memory for bitmap inode"; 337 } 338 memcpy( qnx4_sb(sb)->BitMap, rootdir, sizeof( struct qnx4_inode_entry ) ); /* keep bitmap inode known */ 339 break; 340 } 341 } 342 } 343 brelse(bh); 344 if (found != 0) { 345 break; 346 } 347 } 348 if (found == 0) { 349 return "bitmap file not found."; 350 } 351 } 352 return NULL; 353 } 354 355 static int qnx4_fill_super(struct super_block *s, void *data, int silent) 356 { 357 struct buffer_head *bh; 358 struct inode *root; 359 const char *errmsg; 360 struct qnx4_sb_info *qs; 361 int ret = -EINVAL; 362 363 qs = kzalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL); 364 if (!qs) 365 return -ENOMEM; 366 s->s_fs_info = qs; 367 368 sb_set_blocksize(s, QNX4_BLOCK_SIZE); 369 370 /* Check the superblock signature. Since the qnx4 code is 371 dangerous, we should leave as quickly as possible 372 if we don't belong here... */ 373 bh = sb_bread(s, 1); 374 if (!bh) { 375 printk("qnx4: unable to read the superblock\n"); 376 goto outnobh; 377 } 378 if ( le32_to_cpup((__le32*) bh->b_data) != QNX4_SUPER_MAGIC ) { 379 if (!silent) 380 printk("qnx4: wrong fsid in superblock.\n"); 381 goto out; 382 } 383 s->s_op = &qnx4_sops; 384 s->s_magic = QNX4_SUPER_MAGIC; 385 #ifndef CONFIG_QNX4FS_RW 386 s->s_flags |= MS_RDONLY; /* Yup, read-only yet */ 387 #endif 388 qnx4_sb(s)->sb_buf = bh; 389 qnx4_sb(s)->sb = (struct qnx4_super_block *) bh->b_data; 390 391 392 /* check before allocating dentries, inodes, .. */ 393 errmsg = qnx4_checkroot(s); 394 if (errmsg != NULL) { 395 if (!silent) 396 printk("qnx4: %s\n", errmsg); 397 goto out; 398 } 399 400 /* does root not have inode number QNX4_ROOT_INO ?? */ 401 root = qnx4_iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK); 402 if (IS_ERR(root)) { 403 printk("qnx4: get inode failed\n"); 404 ret = PTR_ERR(root); 405 goto out; 406 } 407 408 ret = -ENOMEM; 409 s->s_root = d_alloc_root(root); 410 if (s->s_root == NULL) 411 goto outi; 412 413 brelse(bh); 414 415 return 0; 416 417 outi: 418 iput(root); 419 out: 420 brelse(bh); 421 outnobh: 422 kfree(qs); 423 s->s_fs_info = NULL; 424 return ret; 425 } 426 427 static void qnx4_put_super(struct super_block *sb) 428 { 429 struct qnx4_sb_info *qs = qnx4_sb(sb); 430 kfree( qs->BitMap ); 431 kfree( qs ); 432 sb->s_fs_info = NULL; 433 return; 434 } 435 436 static int qnx4_writepage(struct page *page, struct writeback_control *wbc) 437 { 438 return block_write_full_page(page,qnx4_get_block, wbc); 439 } 440 441 static int qnx4_readpage(struct file *file, struct page *page) 442 { 443 return block_read_full_page(page,qnx4_get_block); 444 } 445 446 static int qnx4_write_begin(struct file *file, struct address_space *mapping, 447 loff_t pos, unsigned len, unsigned flags, 448 struct page **pagep, void **fsdata) 449 { 450 struct qnx4_inode_info *qnx4_inode = qnx4_i(mapping->host); 451 *pagep = NULL; 452 return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 453 qnx4_get_block, 454 &qnx4_inode->mmu_private); 455 } 456 static sector_t qnx4_bmap(struct address_space *mapping, sector_t block) 457 { 458 return generic_block_bmap(mapping,block,qnx4_get_block); 459 } 460 static const struct address_space_operations qnx4_aops = { 461 .readpage = qnx4_readpage, 462 .writepage = qnx4_writepage, 463 .sync_page = block_sync_page, 464 .write_begin = qnx4_write_begin, 465 .write_end = generic_write_end, 466 .bmap = qnx4_bmap 467 }; 468 469 struct inode *qnx4_iget(struct super_block *sb, unsigned long ino) 470 { 471 struct buffer_head *bh; 472 struct qnx4_inode_entry *raw_inode; 473 int block; 474 struct qnx4_inode_entry *qnx4_inode; 475 struct inode *inode; 476 477 inode = iget_locked(sb, ino); 478 if (!inode) 479 return ERR_PTR(-ENOMEM); 480 if (!(inode->i_state & I_NEW)) 481 return inode; 482 483 qnx4_inode = qnx4_raw_inode(inode); 484 inode->i_mode = 0; 485 486 QNX4DEBUG(("Reading inode : [%d]\n", ino)); 487 if (!ino) { 488 printk(KERN_ERR "qnx4: bad inode number on dev %s: %lu is " 489 "out of range\n", 490 sb->s_id, ino); 491 iget_failed(inode); 492 return ERR_PTR(-EIO); 493 } 494 block = ino / QNX4_INODES_PER_BLOCK; 495 496 if (!(bh = sb_bread(sb, block))) { 497 printk("qnx4: major problem: unable to read inode from dev " 498 "%s\n", sb->s_id); 499 iget_failed(inode); 500 return ERR_PTR(-EIO); 501 } 502 raw_inode = ((struct qnx4_inode_entry *) bh->b_data) + 503 (ino % QNX4_INODES_PER_BLOCK); 504 505 inode->i_mode = le16_to_cpu(raw_inode->di_mode); 506 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->di_uid); 507 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->di_gid); 508 inode->i_nlink = le16_to_cpu(raw_inode->di_nlink); 509 inode->i_size = le32_to_cpu(raw_inode->di_size); 510 inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->di_mtime); 511 inode->i_mtime.tv_nsec = 0; 512 inode->i_atime.tv_sec = le32_to_cpu(raw_inode->di_atime); 513 inode->i_atime.tv_nsec = 0; 514 inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->di_ctime); 515 inode->i_ctime.tv_nsec = 0; 516 inode->i_blocks = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size); 517 518 memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE); 519 if (S_ISREG(inode->i_mode)) { 520 inode->i_op = &qnx4_file_inode_operations; 521 inode->i_fop = &qnx4_file_operations; 522 inode->i_mapping->a_ops = &qnx4_aops; 523 qnx4_i(inode)->mmu_private = inode->i_size; 524 } else if (S_ISDIR(inode->i_mode)) { 525 inode->i_op = &qnx4_dir_inode_operations; 526 inode->i_fop = &qnx4_dir_operations; 527 } else if (S_ISLNK(inode->i_mode)) { 528 inode->i_op = &page_symlink_inode_operations; 529 inode->i_mapping->a_ops = &qnx4_aops; 530 qnx4_i(inode)->mmu_private = inode->i_size; 531 } else { 532 printk(KERN_ERR "qnx4: bad inode %lu on dev %s\n", 533 ino, sb->s_id); 534 iget_failed(inode); 535 brelse(bh); 536 return ERR_PTR(-EIO); 537 } 538 brelse(bh); 539 unlock_new_inode(inode); 540 return inode; 541 } 542 543 static struct kmem_cache *qnx4_inode_cachep; 544 545 static struct inode *qnx4_alloc_inode(struct super_block *sb) 546 { 547 struct qnx4_inode_info *ei; 548 ei = kmem_cache_alloc(qnx4_inode_cachep, GFP_KERNEL); 549 if (!ei) 550 return NULL; 551 return &ei->vfs_inode; 552 } 553 554 static void qnx4_destroy_inode(struct inode *inode) 555 { 556 kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode)); 557 } 558 559 static void init_once(void *foo) 560 { 561 struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo; 562 563 inode_init_once(&ei->vfs_inode); 564 } 565 566 static int init_inodecache(void) 567 { 568 qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache", 569 sizeof(struct qnx4_inode_info), 570 0, (SLAB_RECLAIM_ACCOUNT| 571 SLAB_MEM_SPREAD), 572 init_once); 573 if (qnx4_inode_cachep == NULL) 574 return -ENOMEM; 575 return 0; 576 } 577 578 static void destroy_inodecache(void) 579 { 580 kmem_cache_destroy(qnx4_inode_cachep); 581 } 582 583 static int qnx4_get_sb(struct file_system_type *fs_type, 584 int flags, const char *dev_name, void *data, struct vfsmount *mnt) 585 { 586 return get_sb_bdev(fs_type, flags, dev_name, data, qnx4_fill_super, 587 mnt); 588 } 589 590 static struct file_system_type qnx4_fs_type = { 591 .owner = THIS_MODULE, 592 .name = "qnx4", 593 .get_sb = qnx4_get_sb, 594 .kill_sb = kill_block_super, 595 .fs_flags = FS_REQUIRES_DEV, 596 }; 597 598 static int __init init_qnx4_fs(void) 599 { 600 int err; 601 602 err = init_inodecache(); 603 if (err) 604 return err; 605 606 err = register_filesystem(&qnx4_fs_type); 607 if (err) { 608 destroy_inodecache(); 609 return err; 610 } 611 612 printk("QNX4 filesystem 0.2.3 registered.\n"); 613 return 0; 614 } 615 616 static void __exit exit_qnx4_fs(void) 617 { 618 unregister_filesystem(&qnx4_fs_type); 619 destroy_inodecache(); 620 } 621 622 module_init(init_qnx4_fs) 623 module_exit(exit_qnx4_fs) 624 MODULE_LICENSE("GPL"); 625 626