2 * QNX4 file system, Linux implementation.
6 * Using parts of the xiafs filesystem.
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.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/highuid.h>
19 #include <linux/smp_lock.h>
20 #include <linux/pagemap.h>
21 #include <linux/buffer_head.h>
22 #include <linux/writeback.h>
23 #include <linux/statfs.h>
26 #define QNX4_VERSION 4
27 #define QNX4_BMNAME ".bitmap"
29 static const struct super_operations qnx4_sops;
31 #ifdef CONFIG_QNX4FS_RW
33 static void qnx4_delete_inode(struct inode *inode)
35 QNX4DEBUG(("qnx4: deleting inode [%lu]\n", (unsigned long) inode->i_ino));
36 truncate_inode_pages(&inode->i_data, 0);
40 qnx4_free_inode(inode);
44 static int qnx4_write_inode(struct inode *inode, int do_sync)
46 struct qnx4_inode_entry *raw_inode;
48 struct buffer_head *bh;
51 QNX4DEBUG(("qnx4: write inode 1.\n"));
52 if (inode->i_nlink == 0) {
56 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
57 inode->i_sb->s_id, ino);
60 QNX4DEBUG(("qnx4: write inode 2.\n"));
61 block = ino / QNX4_INODES_PER_BLOCK;
63 if (!(bh = sb_bread(inode->i_sb, block))) {
64 printk("qnx4: major problem: unable to read inode from dev "
65 "%s\n", inode->i_sb->s_id);
69 raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
70 (ino % QNX4_INODES_PER_BLOCK);
71 raw_inode->di_mode = cpu_to_le16(inode->i_mode);
72 raw_inode->di_uid = cpu_to_le16(fs_high2lowuid(inode->i_uid));
73 raw_inode->di_gid = cpu_to_le16(fs_high2lowgid(inode->i_gid));
74 raw_inode->di_nlink = cpu_to_le16(inode->i_nlink);
75 raw_inode->di_size = cpu_to_le32(inode->i_size);
76 raw_inode->di_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
77 raw_inode->di_atime = cpu_to_le32(inode->i_atime.tv_sec);
78 raw_inode->di_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
79 raw_inode->di_first_xtnt.xtnt_size = cpu_to_le32(inode->i_blocks);
80 mark_buffer_dirty(bh);
82 sync_dirty_buffer(bh);
83 if (buffer_req(bh) && !buffer_uptodate(bh)) {
84 printk("qnx4: IO error syncing inode [%s:%08x]\n",
85 inode->i_sb->s_id, ino);
98 static void qnx4_put_super(struct super_block *sb);
99 static struct inode *qnx4_alloc_inode(struct super_block *sb);
100 static void qnx4_destroy_inode(struct inode *inode);
101 static int qnx4_remount(struct super_block *sb, int *flags, char *data);
102 static int qnx4_statfs(struct dentry *, struct kstatfs *);
104 static const struct super_operations qnx4_sops =
106 .alloc_inode = qnx4_alloc_inode,
107 .destroy_inode = qnx4_destroy_inode,
108 .put_super = qnx4_put_super,
109 .statfs = qnx4_statfs,
110 .remount_fs = qnx4_remount,
111 #ifdef CONFIG_QNX4FS_RW
112 .write_inode = qnx4_write_inode,
113 .delete_inode = qnx4_delete_inode,
117 static int qnx4_remount(struct super_block *sb, int *flags, char *data)
119 struct qnx4_sb_info *qs;
122 qs->Version = QNX4_VERSION;
123 #ifndef CONFIG_QNX4FS_RW
126 if (*flags & MS_RDONLY) {
130 mark_buffer_dirty(qs->sb_buf);
135 static struct buffer_head *qnx4_getblk(struct inode *inode, int nr,
138 struct buffer_head *result = NULL;
141 nr = qnx4_block_map( inode, nr );
143 result = sb_getblk(inode->i_sb, nr);
150 tmp = qnx4_new_block(inode->i_sb);
154 result = sb_getblk(inode->i_sb, tmp);
156 qnx4_free_block(inode->i_sb, tmp);
162 inode->i_ctime = CURRENT_TIME_SEC;
163 mark_inode_dirty(inode);
167 struct buffer_head *qnx4_bread(struct inode *inode, int block, int create)
169 struct buffer_head *bh;
171 bh = qnx4_getblk(inode, block, create);
172 if (!bh || buffer_uptodate(bh)) {
175 ll_rw_block(READ, 1, &bh);
177 if (buffer_uptodate(bh)) {
185 static int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
189 QNX4DEBUG(("qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
191 phys = qnx4_block_map( inode, iblock );
193 // logical block is before EOF
194 map_bh(bh, inode->i_sb, phys);
195 } else if ( create ) {
201 unsigned long qnx4_block_map( struct inode *inode, long iblock )
205 unsigned long block = 0;
206 struct buffer_head *bh = NULL;
207 struct qnx4_xblk *xblk = NULL;
208 struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
209 u16 nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
211 if ( iblock < le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size) ) {
212 // iblock is in the first extent. This is easy.
213 block = le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_blk) + iblock - 1;
215 // iblock is beyond first extent. We have to follow the extent chain.
216 i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
217 offset = iblock - le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size);
219 while ( --nxtnt > 0 ) {
221 // read next xtnt block.
222 bh = sb_bread(inode->i_sb, i_xblk - 1);
224 QNX4DEBUG(("qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
227 xblk = (struct qnx4_xblk*)bh->b_data;
228 if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
229 QNX4DEBUG(("qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
233 if ( offset < le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size) ) {
235 block = le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_blk) + offset - 1;
238 offset -= le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size);
239 if ( ++ix >= xblk->xblk_num_xtnts ) {
240 i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
250 QNX4DEBUG(("qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
254 static int qnx4_statfs(struct dentry *dentry, struct kstatfs *buf)
256 struct super_block *sb = dentry->d_sb;
257 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
261 buf->f_type = sb->s_magic;
262 buf->f_bsize = sb->s_blocksize;
263 buf->f_blocks = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
264 buf->f_bfree = qnx4_count_free_blocks(sb);
265 buf->f_bavail = buf->f_bfree;
266 buf->f_namelen = QNX4_NAME_MAX;
267 buf->f_fsid.val[0] = (u32)id;
268 buf->f_fsid.val[1] = (u32)(id >> 32);
276 * Check the root directory of the filesystem to make sure
277 * it really _is_ a qnx4 filesystem, and to check the size
278 * of the directory entry.
280 static const char *qnx4_checkroot(struct super_block *sb)
282 struct buffer_head *bh;
283 struct qnx4_inode_entry *rootdir;
288 if (*(qnx4_sb(sb)->sb->RootDir.di_fname) != '/') {
289 return "no qnx4 filesystem (no root dir).";
291 QNX4DEBUG(("QNX4 filesystem found on dev %s.\n", sb->s_id));
292 rd = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_blk) - 1;
293 rl = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_size);
294 for (j = 0; j < rl; j++) {
295 bh = sb_bread(sb, rd + j); /* root dir, first block */
297 return "unable to read root entry.";
299 for (i = 0; i < QNX4_INODES_PER_BLOCK; i++) {
300 rootdir = (struct qnx4_inode_entry *) (bh->b_data + i * QNX4_DIR_ENTRY_SIZE);
301 if (rootdir->di_fname != NULL) {
302 QNX4DEBUG(("Rootdir entry found : [%s]\n", rootdir->di_fname));
303 if (!strncmp(rootdir->di_fname, QNX4_BMNAME, sizeof QNX4_BMNAME)) {
305 qnx4_sb(sb)->BitMap = kmalloc( sizeof( struct qnx4_inode_entry ), GFP_KERNEL );
306 if (!qnx4_sb(sb)->BitMap) {
308 return "not enough memory for bitmap inode";
310 memcpy( qnx4_sb(sb)->BitMap, rootdir, sizeof( struct qnx4_inode_entry ) ); /* keep bitmap inode known */
321 return "bitmap file not found.";
327 static int qnx4_fill_super(struct super_block *s, void *data, int silent)
329 struct buffer_head *bh;
332 struct qnx4_sb_info *qs;
335 qs = kzalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
340 sb_set_blocksize(s, QNX4_BLOCK_SIZE);
342 /* Check the superblock signature. Since the qnx4 code is
343 dangerous, we should leave as quickly as possible
344 if we don't belong here... */
347 printk("qnx4: unable to read the superblock\n");
350 if ( le32_to_cpup((__le32*) bh->b_data) != QNX4_SUPER_MAGIC ) {
352 printk("qnx4: wrong fsid in superblock.\n");
355 s->s_op = &qnx4_sops;
356 s->s_magic = QNX4_SUPER_MAGIC;
357 #ifndef CONFIG_QNX4FS_RW
358 s->s_flags |= MS_RDONLY; /* Yup, read-only yet */
360 qnx4_sb(s)->sb_buf = bh;
361 qnx4_sb(s)->sb = (struct qnx4_super_block *) bh->b_data;
364 /* check before allocating dentries, inodes, .. */
365 errmsg = qnx4_checkroot(s);
366 if (errmsg != NULL) {
368 printk("qnx4: %s\n", errmsg);
372 /* does root not have inode number QNX4_ROOT_INO ?? */
373 root = qnx4_iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
375 printk("qnx4: get inode failed\n");
381 s->s_root = d_alloc_root(root);
382 if (s->s_root == NULL)
399 static void qnx4_put_super(struct super_block *sb)
401 struct qnx4_sb_info *qs = qnx4_sb(sb);
404 sb->s_fs_info = NULL;
408 static int qnx4_writepage(struct page *page, struct writeback_control *wbc)
410 return block_write_full_page(page,qnx4_get_block, wbc);
413 static int qnx4_readpage(struct file *file, struct page *page)
415 return block_read_full_page(page,qnx4_get_block);
418 static int qnx4_write_begin(struct file *file, struct address_space *mapping,
419 loff_t pos, unsigned len, unsigned flags,
420 struct page **pagep, void **fsdata)
422 struct qnx4_inode_info *qnx4_inode = qnx4_i(mapping->host);
424 return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
426 &qnx4_inode->mmu_private);
428 static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
430 return generic_block_bmap(mapping,block,qnx4_get_block);
432 static const struct address_space_operations qnx4_aops = {
433 .readpage = qnx4_readpage,
434 .writepage = qnx4_writepage,
435 .sync_page = block_sync_page,
436 .write_begin = qnx4_write_begin,
437 .write_end = generic_write_end,
441 struct inode *qnx4_iget(struct super_block *sb, unsigned long ino)
443 struct buffer_head *bh;
444 struct qnx4_inode_entry *raw_inode;
446 struct qnx4_inode_entry *qnx4_inode;
449 inode = iget_locked(sb, ino);
451 return ERR_PTR(-ENOMEM);
452 if (!(inode->i_state & I_NEW))
455 qnx4_inode = qnx4_raw_inode(inode);
458 QNX4DEBUG(("Reading inode : [%d]\n", ino));
460 printk(KERN_ERR "qnx4: bad inode number on dev %s: %lu is "
464 return ERR_PTR(-EIO);
466 block = ino / QNX4_INODES_PER_BLOCK;
468 if (!(bh = sb_bread(sb, block))) {
469 printk("qnx4: major problem: unable to read inode from dev "
472 return ERR_PTR(-EIO);
474 raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
475 (ino % QNX4_INODES_PER_BLOCK);
477 inode->i_mode = le16_to_cpu(raw_inode->di_mode);
478 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->di_uid);
479 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->di_gid);
480 inode->i_nlink = le16_to_cpu(raw_inode->di_nlink);
481 inode->i_size = le32_to_cpu(raw_inode->di_size);
482 inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->di_mtime);
483 inode->i_mtime.tv_nsec = 0;
484 inode->i_atime.tv_sec = le32_to_cpu(raw_inode->di_atime);
485 inode->i_atime.tv_nsec = 0;
486 inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->di_ctime);
487 inode->i_ctime.tv_nsec = 0;
488 inode->i_blocks = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
490 memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
491 if (S_ISREG(inode->i_mode)) {
492 inode->i_op = &qnx4_file_inode_operations;
493 inode->i_fop = &qnx4_file_operations;
494 inode->i_mapping->a_ops = &qnx4_aops;
495 qnx4_i(inode)->mmu_private = inode->i_size;
496 } else if (S_ISDIR(inode->i_mode)) {
497 inode->i_op = &qnx4_dir_inode_operations;
498 inode->i_fop = &qnx4_dir_operations;
499 } else if (S_ISLNK(inode->i_mode)) {
500 inode->i_op = &page_symlink_inode_operations;
501 inode->i_mapping->a_ops = &qnx4_aops;
502 qnx4_i(inode)->mmu_private = inode->i_size;
504 printk(KERN_ERR "qnx4: bad inode %lu on dev %s\n",
508 return ERR_PTR(-EIO);
511 unlock_new_inode(inode);
515 static struct kmem_cache *qnx4_inode_cachep;
517 static struct inode *qnx4_alloc_inode(struct super_block *sb)
519 struct qnx4_inode_info *ei;
520 ei = kmem_cache_alloc(qnx4_inode_cachep, GFP_KERNEL);
523 return &ei->vfs_inode;
526 static void qnx4_destroy_inode(struct inode *inode)
528 kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
531 static void init_once(void *foo)
533 struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
535 inode_init_once(&ei->vfs_inode);
538 static int init_inodecache(void)
540 qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
541 sizeof(struct qnx4_inode_info),
542 0, (SLAB_RECLAIM_ACCOUNT|
545 if (qnx4_inode_cachep == NULL)
550 static void destroy_inodecache(void)
552 kmem_cache_destroy(qnx4_inode_cachep);
555 static int qnx4_get_sb(struct file_system_type *fs_type,
556 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
558 return get_sb_bdev(fs_type, flags, dev_name, data, qnx4_fill_super,
562 static struct file_system_type qnx4_fs_type = {
563 .owner = THIS_MODULE,
565 .get_sb = qnx4_get_sb,
566 .kill_sb = kill_block_super,
567 .fs_flags = FS_REQUIRES_DEV,
570 static int __init init_qnx4_fs(void)
574 err = init_inodecache();
578 err = register_filesystem(&qnx4_fs_type);
580 destroy_inodecache();
584 printk("QNX4 filesystem 0.2.3 registered.\n");
588 static void __exit exit_qnx4_fs(void)
590 unregister_filesystem(&qnx4_fs_type);
591 destroy_inodecache();
594 module_init(init_qnx4_fs)
595 module_exit(exit_qnx4_fs)
596 MODULE_LICENSE("GPL");