- Jaroslav Kysela: ymfpci driver
[davej-history.git] / fs / qnx4 / inode.c
blob45a94520978d2f781864c2d24fd1edbf2300df3f
1 /*
2 * QNX4 file system, Linux implementation.
4 * Version : 0.2.1
6 * Using parts of the xiafs filesystem.
8 * History :
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>
22 #include <linux/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)
39 int err = 0;
40 # if 0
41 struct buffer_head *bh;
43 bh = qnx4_update_inode(inode);
44 if (bh && buffer_dirty(bh))
46 ll_rw_block(WRITE, 1, &bh);
47 wait_on_buffer(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);
52 err = -1;
54 brelse (bh);
55 } else if (!bh) {
56 err = -1;
58 # endif
60 return err;
63 static void qnx4_delete_inode(struct inode *inode)
65 QNX4DEBUG(("qnx4: deleting inode [%lu]\n", (unsigned long) inode->i_ino));
66 lock_kernel();
67 inode->i_size = 0;
68 qnx4_truncate(inode);
69 qnx4_free_inode(inode);
70 unlock_kernel();
73 static void qnx4_write_super(struct super_block *sb)
75 QNX4DEBUG(("qnx4: write_super\n"));
76 sb->s_dirt = 0;
79 static void qnx4_write_inode(struct inode *inode, int unused)
81 struct qnx4_inode_entry *raw_inode;
82 int block, ino;
83 struct buffer_head *bh;
84 ino = inode->i_ino;
86 QNX4DEBUG(("qnx4: write inode 1.\n"));
87 if (inode->i_nlink == 0) {
88 return;
90 if (!ino) {
91 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
92 kdevname(inode->i_dev), ino);
93 return;
95 QNX4DEBUG(("qnx4: write inode 2.\n"));
96 block = ino / QNX4_INODES_PER_BLOCK;
97 lock_kernel();
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));
101 unlock_kernel();
102 return;
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);
116 brelse(bh);
117 unlock_kernel();
120 #endif
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,
134 #endif
135 put_super: qnx4_put_super,
136 #ifdef CONFIG_QNX4FS_RW
137 write_super: qnx4_write_super,
138 #endif
139 statfs: qnx4_statfs,
140 remount_fs: qnx4_remount,
143 static int qnx4_remount(struct super_block *sb, int *flags, char *data)
145 struct qnx4_sb_info *qs;
147 qs = &sb->u.qnx4_sb;
148 qs->Version = QNX4_VERSION;
149 if (*flags & MS_RDONLY) {
150 return 0;
152 mark_buffer_dirty(qs->sb_buf);
154 return 0;
157 struct buffer_head *qnx4_getblk(struct inode *inode, int nr,
158 int create)
160 struct buffer_head *result = NULL;
162 if ( nr >= 0 )
163 nr = qnx4_block_map( inode, nr );
164 if (nr) {
165 result = getblk(inode->i_dev, nr, QNX4_BLOCK_SIZE);
166 return result;
168 if (!create) {
169 return NULL;
171 #if 0
172 tmp = qnx4_new_block(inode->i_sb);
173 if (!tmp) {
174 return NULL;
176 result = getblk(inode->i_dev, tmp, QNX4_BLOCK_SIZE);
177 if (tst) {
178 qnx4_free_block(inode->i_sb, tmp);
179 brelse(result);
180 goto repeat;
182 tst = tmp;
183 #endif
184 inode->i_ctime = CURRENT_TIME;
185 mark_inode_dirty(inode);
186 return result;
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)) {
195 return bh;
197 ll_rw_block(READ, 1, &bh);
198 wait_on_buffer(bh);
199 if (buffer_uptodate(bh)) {
200 return bh;
202 brelse(bh);
204 return NULL;
207 int qnx4_get_block( struct inode *inode, long iblock, struct buffer_head *bh, int create )
209 unsigned long phys;
211 QNX4DEBUG(("qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
213 phys = qnx4_block_map( inode, iblock );
214 if ( phys ) {
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 ) {
220 // to be done.
222 return 0;
225 unsigned long qnx4_block_map( struct inode *inode, long iblock )
227 int ix;
228 long offset, i_xblk;
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;
238 } else {
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);
242 ix = 0;
243 while ( --nxtnt > 0 ) {
244 if ( ix == 0 ) {
245 // read next xtnt block.
246 bh = bread( inode->i_dev, i_xblk - 1, QNX4_BLOCK_SIZE );
247 if ( !bh ) {
248 QNX4DEBUG(("qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
249 return -EIO;
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));
254 return -EIO;
257 if ( offset < le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size) ) {
258 // got it!
259 block = le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_blk) + offset - 1;
260 break;
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);
265 ix = 0;
266 brelse( bh );
267 bh = 0;
270 if ( bh )
271 brelse( bh );
274 QNX4DEBUG(("qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
275 return 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;
287 return 0;
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;
299 int rd, rl;
300 int i, j;
301 int found = 0;
303 if (*(sb->u.qnx4_sb.sb->RootDir.di_fname) != '/') {
304 return "no qnx4 filesystem (no root dir).";
305 } else {
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 */
311 if (bh == NULL) {
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)) {
319 found = 1;
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 */
322 break;
326 brelse(bh);
327 if (found != 0) {
328 break;
331 if (found == 0) {
332 return "bitmap file not found.";
335 return NULL;
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;
343 struct inode *root;
344 const char *errmsg;
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);
354 if (!bh) {
355 printk("qnx4: unable to read the boot sector\n");
356 goto outnobh;
358 if ( memcmp( (char*)bh->b_data + 4, "QNX4FS", 6 ) ) {
359 if (!silent)
360 printk("qnx4: wrong fsid in boot sector.\n");
361 goto out;
363 brelse(bh);
365 bh = bread(dev, 1, QNX4_BLOCK_SIZE);
366 if (!bh) {
367 printk("qnx4: unable to read the superblock\n");
368 goto outnobh;
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 */
374 #endif
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) {
382 if (!silent)
383 printk("qnx4: %s\n", errmsg);
384 goto out;
387 /* does root not have inode number QNX4_ROOT_INO ?? */
388 root = iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
389 if (!root) {
390 printk("qnx4: get inode failed\n");
391 goto out;
394 s->s_root = d_alloc_root(root);
395 if (s->s_root == NULL)
396 goto outi;
398 brelse(bh);
399 s->s_dirt = 1;
401 return s;
403 outi:
404 iput(root);
405 out:
406 brelse(bh);
407 outnobh:
409 return NULL;
412 static void qnx4_put_super(struct super_block *sb)
414 kfree( sb->u.qnx4_sb.BitMap );
415 return;
418 static int qnx4_writepage(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,
441 bmap: qnx4_bmap
444 static void qnx4_read_inode(struct inode *inode)
446 struct buffer_head *bh;
447 struct qnx4_inode_entry *raw_inode;
448 int block, ino;
450 ino = inode->i_ino;
451 inode->i_mode = 0;
453 QNX4DEBUG(("Reading inode : [%d]\n", ino));
454 if (!ino) {
455 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
456 kdevname(inode->i_dev), ino);
457 return;
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));
464 return;
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;
493 } else
494 printk("qnx4: bad inode %d on dev %s\n",ino,kdevname(inode->i_dev));
495 brelse(bh);
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);
511 EXPORT_NO_SYMBOLS;
513 module_init(init_qnx4_fs)
514 module_exit(exit_qnx4_fs)