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/config.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/malloc.h>
21 #include <linux/qnx4_fs.h>
23 #include <linux/locks.h>
24 #include <linux/init.h>
25 #include <linux/highuid.h>
26 #include <linux/smp_lock.h>
28 #include <asm/uaccess.h>
30 #define QNX4_VERSION 4
31 #define QNX4_BMNAME ".bitmap"
33 static struct super_operations qnx4_sops
;
35 #ifdef CONFIG_QNX4FS_RW
37 int qnx4_sync_inode(struct inode
*inode
)
41 struct buffer_head
*bh
;
43 bh
= qnx4_update_inode(inode
);
44 if (bh
&& buffer_dirty(bh
))
46 ll_rw_block(WRITE
, 1, &bh
);
48 if (buffer_req(bh
) && !buffer_uptodate(bh
))
50 printk ("IO error syncing qnx4 inode [%s:%08lx]\n",
51 kdevname(inode
->i_dev
), inode
->i_ino
);
63 static void qnx4_delete_inode(struct inode
*inode
)
65 QNX4DEBUG(("qnx4: deleting inode [%lu]\n", (unsigned long) inode
->i_ino
));
69 qnx4_free_inode(inode
);
73 static void qnx4_write_super(struct super_block
*sb
)
75 QNX4DEBUG(("qnx4: write_super\n"));
79 static void qnx4_write_inode(struct inode
*inode
, int unused
)
81 struct qnx4_inode_entry
*raw_inode
;
83 struct buffer_head
*bh
;
86 QNX4DEBUG(("qnx4: write inode 1.\n"));
87 if (inode
->i_nlink
== 0) {
91 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
92 kdevname(inode
->i_dev
), ino
);
95 QNX4DEBUG(("qnx4: write inode 2.\n"));
96 block
= ino
/ QNX4_INODES_PER_BLOCK
;
98 if (!(bh
= bread(inode
->i_dev
, block
, QNX4_BLOCK_SIZE
))) {
99 printk("qnx4: major problem: unable to read inode from dev "
100 "%s\n", kdevname(inode
->i_dev
));
104 raw_inode
= ((struct qnx4_inode_entry
*) bh
->b_data
) +
105 (ino
% QNX4_INODES_PER_BLOCK
);
106 raw_inode
->di_mode
= cpu_to_le16(inode
->i_mode
);
107 raw_inode
->di_uid
= cpu_to_le16(fs_high2lowuid(inode
->i_uid
));
108 raw_inode
->di_gid
= cpu_to_le16(fs_high2lowgid(inode
->i_gid
));
109 raw_inode
->di_nlink
= cpu_to_le16(inode
->i_nlink
);
110 raw_inode
->di_size
= cpu_to_le32(inode
->i_size
);
111 raw_inode
->di_mtime
= cpu_to_le32(inode
->i_mtime
);
112 raw_inode
->di_atime
= cpu_to_le32(inode
->i_atime
);
113 raw_inode
->di_ctime
= cpu_to_le32(inode
->i_ctime
);
114 raw_inode
->di_first_xtnt
.xtnt_size
= cpu_to_le32(inode
->i_blocks
);
115 mark_buffer_dirty(bh
);
122 static struct super_block
*qnx4_read_super(struct super_block
*, void *, int);
123 static void qnx4_put_super(struct super_block
*sb
);
124 static void qnx4_read_inode(struct inode
*);
125 static int qnx4_remount(struct super_block
*sb
, int *flags
, char *data
);
126 static int qnx4_statfs(struct super_block
*, struct statfs
*);
128 static struct super_operations qnx4_sops
=
130 read_inode
: qnx4_read_inode
,
131 #ifdef CONFIG_QNX4FS_RW
132 write_inode
: qnx4_write_inode
,
133 delete_inode
: qnx4_delete_inode
,
135 put_super
: qnx4_put_super
,
136 #ifdef CONFIG_QNX4FS_RW
137 write_super
: qnx4_write_super
,
140 remount_fs
: qnx4_remount
,
143 static int qnx4_remount(struct super_block
*sb
, int *flags
, char *data
)
145 struct qnx4_sb_info
*qs
;
148 qs
->Version
= QNX4_VERSION
;
149 if (*flags
& MS_RDONLY
) {
152 mark_buffer_dirty(qs
->sb_buf
);
157 struct buffer_head
*qnx4_getblk(struct inode
*inode
, int nr
,
160 struct buffer_head
*result
= NULL
;
163 nr
= qnx4_block_map( inode
, nr
);
165 result
= getblk(inode
->i_dev
, nr
, QNX4_BLOCK_SIZE
);
172 tmp
= qnx4_new_block(inode
->i_sb
);
176 result
= getblk(inode
->i_dev
, tmp
, QNX4_BLOCK_SIZE
);
178 qnx4_free_block(inode
->i_sb
, tmp
);
184 inode
->i_ctime
= CURRENT_TIME
;
185 mark_inode_dirty(inode
);
189 struct buffer_head
*qnx4_bread(struct inode
*inode
, int block
, int create
)
191 struct buffer_head
*bh
;
193 bh
= qnx4_getblk(inode
, block
, create
);
194 if (!bh
|| buffer_uptodate(bh
)) {
197 ll_rw_block(READ
, 1, &bh
);
199 if (buffer_uptodate(bh
)) {
207 int qnx4_get_block( struct inode
*inode
, long iblock
, struct buffer_head
*bh
, int create
)
211 QNX4DEBUG(("qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode
->i_ino
,iblock
));
213 phys
= qnx4_block_map( inode
, iblock
);
215 // logical block is before EOF
216 bh
->b_dev
= inode
->i_dev
;
217 bh
->b_blocknr
= phys
;
218 bh
->b_state
|= (1UL << BH_Mapped
);
219 } else if ( create
) {
225 unsigned long qnx4_block_map( struct inode
*inode
, long iblock
)
229 unsigned long block
= 0;
230 struct buffer_head
*bh
= 0;
231 struct qnx4_xblk
*xblk
= 0;
232 struct qnx4_inode_info
*qnx4_inode
= &inode
->u
.qnx4_i
;
233 qnx4_nxtnt_t nxtnt
= le16_to_cpu(qnx4_inode
->i_num_xtnts
);
235 if ( iblock
< le32_to_cpu(qnx4_inode
->i_first_xtnt
.xtnt_size
) ) {
236 // iblock is in the first extent. This is easy.
237 block
= le32_to_cpu(qnx4_inode
->i_first_xtnt
.xtnt_blk
) + iblock
- 1;
239 // iblock is beyond first extent. We have to follow the extent chain.
240 i_xblk
= le32_to_cpu(qnx4_inode
->i_xblk
);
241 offset
= iblock
- le32_to_cpu(qnx4_inode
->i_first_xtnt
.xtnt_size
);
243 while ( --nxtnt
> 0 ) {
245 // read next xtnt block.
246 bh
= bread( inode
->i_dev
, i_xblk
- 1, QNX4_BLOCK_SIZE
);
248 QNX4DEBUG(("qnx4: I/O error reading xtnt block [%ld])\n", i_xblk
- 1));
251 xblk
= (struct qnx4_xblk
*)bh
->b_data
;
252 if ( memcmp( xblk
->xblk_signature
, "IamXblk", 7 ) ) {
253 QNX4DEBUG(("qnx4: block at %ld is not a valid xtnt\n", qnx4_inode
->i_xblk
));
257 if ( offset
< le32_to_cpu(xblk
->xblk_xtnts
[ix
].xtnt_size
) ) {
259 block
= le32_to_cpu(xblk
->xblk_xtnts
[ix
].xtnt_blk
) + offset
- 1;
262 offset
-= le32_to_cpu(xblk
->xblk_xtnts
[ix
].xtnt_size
);
263 if ( ++ix
>= xblk
->xblk_num_xtnts
) {
264 i_xblk
= le32_to_cpu(xblk
->xblk_next_xblk
);
274 QNX4DEBUG(("qnx4: mapping block %ld of inode %ld = %ld\n",iblock
,inode
->i_ino
,block
));
278 static int qnx4_statfs(struct super_block
*sb
, struct statfs
*buf
)
280 buf
->f_type
= sb
->s_magic
;
281 buf
->f_bsize
= sb
->s_blocksize
;
282 buf
->f_blocks
= le32_to_cpu(sb
->u
.qnx4_sb
.BitMap
->di_size
) * 8;
283 buf
->f_bfree
= qnx4_count_free_blocks(sb
);
284 buf
->f_bavail
= buf
->f_bfree
;
285 buf
->f_namelen
= QNX4_NAME_MAX
;
291 * Check the root directory of the filesystem to make sure
292 * it really _is_ a qnx4 filesystem, and to check the size
293 * of the directory entry.
295 static const char *qnx4_checkroot(struct super_block
*sb
)
297 struct buffer_head
*bh
;
298 struct qnx4_inode_entry
*rootdir
;
303 if (*(sb
->u
.qnx4_sb
.sb
->RootDir
.di_fname
) != '/') {
304 return "no qnx4 filesystem (no root dir).";
306 QNX4DEBUG(("QNX4 filesystem found on dev %s.\n", kdevname(sb
->s_dev
)));
307 rd
= le32_to_cpu(sb
->u
.qnx4_sb
.sb
->RootDir
.di_first_xtnt
.xtnt_blk
) - 1;
308 rl
= le32_to_cpu(sb
->u
.qnx4_sb
.sb
->RootDir
.di_first_xtnt
.xtnt_size
);
309 for (j
= 0; j
< rl
; j
++) {
310 bh
= bread(sb
->s_dev
, rd
+ j
, QNX4_BLOCK_SIZE
); /* root dir, first block */
312 return "unable to read root entry.";
314 for (i
= 0; i
< QNX4_INODES_PER_BLOCK
; i
++) {
315 rootdir
= (struct qnx4_inode_entry
*) (bh
->b_data
+ i
* QNX4_DIR_ENTRY_SIZE
);
316 if (rootdir
->di_fname
!= NULL
) {
317 QNX4DEBUG(("Rootdir entry found : [%s]\n", rootdir
->di_fname
));
318 if (!strncmp(rootdir
->di_fname
, QNX4_BMNAME
, sizeof QNX4_BMNAME
)) {
320 sb
->u
.qnx4_sb
.BitMap
= kmalloc( sizeof( struct qnx4_inode_entry
), GFP_KERNEL
);
321 memcpy( sb
->u
.qnx4_sb
.BitMap
, rootdir
, sizeof( struct qnx4_inode_entry
) ); /* keep bitmap inode known */
332 return "bitmap file not found.";
338 static struct super_block
*qnx4_read_super(struct super_block
*s
,
339 void *data
, int silent
)
341 struct buffer_head
*bh
;
342 kdev_t dev
= s
->s_dev
;
346 set_blocksize(dev
, QNX4_BLOCK_SIZE
);
347 s
->s_blocksize
= QNX4_BLOCK_SIZE
;
348 s
->s_blocksize_bits
= QNX4_BLOCK_SIZE_BITS
;
350 /* Check the boot signature. Since the qnx4 code is
351 dangerous, we should leave as quickly as possible
352 if we don't belong here... */
353 bh
= bread(dev
, 0, QNX4_BLOCK_SIZE
);
355 printk("qnx4: unable to read the boot sector\n");
358 if ( memcmp( (char*)bh
->b_data
+ 4, "QNX4FS", 6 ) ) {
360 printk("qnx4: wrong fsid in boot sector.\n");
365 bh
= bread(dev
, 1, QNX4_BLOCK_SIZE
);
367 printk("qnx4: unable to read the superblock\n");
370 s
->s_op
= &qnx4_sops
;
371 s
->s_magic
= QNX4_SUPER_MAGIC
;
372 #ifndef CONFIG_QNX4FS_RW
373 s
->s_flags
|= MS_RDONLY
; /* Yup, read-only yet */
375 s
->u
.qnx4_sb
.sb_buf
= bh
;
376 s
->u
.qnx4_sb
.sb
= (struct qnx4_super_block
*) bh
->b_data
;
379 /* check before allocating dentries, inodes, .. */
380 errmsg
= qnx4_checkroot(s
);
381 if (errmsg
!= NULL
) {
383 printk("qnx4: %s\n", errmsg
);
387 /* does root not have inode number QNX4_ROOT_INO ?? */
388 root
= iget(s
, QNX4_ROOT_INO
* QNX4_INODES_PER_BLOCK
);
390 printk("qnx4: get inode failed\n");
394 s
->s_root
= d_alloc_root(root
);
395 if (s
->s_root
== NULL
)
412 static void qnx4_put_super(struct super_block
*sb
)
414 kfree( sb
->u
.qnx4_sb
.BitMap
);
418 static int qnx4_writepage(struct file
*file
, struct page
*page
)
420 return block_write_full_page(page
,qnx4_get_block
);
422 static int qnx4_readpage(struct file
*file
, struct page
*page
)
424 return block_read_full_page(page
,qnx4_get_block
);
426 static int qnx4_prepare_write(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
428 return cont_prepare_write(page
,from
,to
,qnx4_get_block
,
429 &((struct inode
*)page
->mapping
->host
)->u
.qnx4_i
.mmu_private
);
431 static int qnx4_bmap(struct address_space
*mapping
, long block
)
433 return generic_block_bmap(mapping
,block
,qnx4_get_block
);
435 struct address_space_operations qnx4_aops
= {
436 readpage
: qnx4_readpage
,
437 writepage
: qnx4_writepage
,
438 sync_page
: block_sync_page
,
439 prepare_write
: qnx4_prepare_write
,
440 commit_write
: generic_commit_write
,
444 static void qnx4_read_inode(struct inode
*inode
)
446 struct buffer_head
*bh
;
447 struct qnx4_inode_entry
*raw_inode
;
453 QNX4DEBUG(("Reading inode : [%d]\n", ino
));
455 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
456 kdevname(inode
->i_dev
), ino
);
459 block
= ino
/ QNX4_INODES_PER_BLOCK
;
461 if (!(bh
= bread(inode
->i_dev
, block
, QNX4_BLOCK_SIZE
))) {
462 printk("qnx4: major problem: unable to read inode from dev "
463 "%s\n", kdevname(inode
->i_dev
));
466 raw_inode
= ((struct qnx4_inode_entry
*) bh
->b_data
) +
467 (ino
% QNX4_INODES_PER_BLOCK
);
469 inode
->i_mode
= le16_to_cpu(raw_inode
->di_mode
);
470 inode
->i_uid
= (uid_t
)le16_to_cpu(raw_inode
->di_uid
);
471 inode
->i_gid
= (gid_t
)le16_to_cpu(raw_inode
->di_gid
);
472 inode
->i_nlink
= le16_to_cpu(raw_inode
->di_nlink
);
473 inode
->i_size
= le32_to_cpu(raw_inode
->di_size
);
474 inode
->i_mtime
= le32_to_cpu(raw_inode
->di_mtime
);
475 inode
->i_atime
= le32_to_cpu(raw_inode
->di_atime
);
476 inode
->i_ctime
= le32_to_cpu(raw_inode
->di_ctime
);
477 inode
->i_blocks
= le32_to_cpu(raw_inode
->di_first_xtnt
.xtnt_size
);
478 inode
->i_blksize
= QNX4_DIR_ENTRY_SIZE
;
480 memcpy(&inode
->u
.qnx4_i
, (struct qnx4_inode_info
*) raw_inode
, QNX4_DIR_ENTRY_SIZE
);
481 if (S_ISREG(inode
->i_mode
)) {
482 inode
->i_op
= &qnx4_file_inode_operations
;
483 inode
->i_fop
= &qnx4_file_operations
;
484 inode
->i_mapping
->a_ops
= &qnx4_aops
;
485 inode
->u
.qnx4_i
.mmu_private
= inode
->i_size
;
486 } else if (S_ISDIR(inode
->i_mode
)) {
487 inode
->i_op
= &qnx4_dir_inode_operations
;
488 inode
->i_fop
= &qnx4_dir_operations
;
489 } else if (S_ISLNK(inode
->i_mode
)) {
490 inode
->i_op
= &page_symlink_inode_operations
;
491 inode
->i_mapping
->a_ops
= &qnx4_aops
;
492 inode
->u
.qnx4_i
.mmu_private
= inode
->i_size
;
494 printk("qnx4: bad inode %d on dev %s\n",ino
,kdevname(inode
->i_dev
));
498 static DECLARE_FSTYPE_DEV(qnx4_fs_type
, "qnx4", qnx4_read_super
);
500 static int __init
init_qnx4_fs(void)
502 printk("QNX4 filesystem 0.2.2 registered.\n");
503 return register_filesystem(&qnx4_fs_type
);
506 static void __exit
exit_qnx4_fs(void)
508 unregister_filesystem(&qnx4_fs_type
);
513 module_init(init_qnx4_fs
)
514 module_exit(exit_qnx4_fs
)