Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / fs / qnx4 / inode.c
bloba1d8fc6be1452498276315390d66f2808df39a98
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/slab.h>
21 #include <linux/fs.h>
22 #include <linux/qnx4_fs.h>
23 #include <linux/init.h>
24 #include <linux/highuid.h>
25 #include <linux/smp_lock.h>
26 #include <linux/pagemap.h>
27 #include <linux/buffer_head.h>
29 #include <asm/uaccess.h>
31 #define QNX4_VERSION 4
32 #define QNX4_BMNAME ".bitmap"
34 static struct super_operations qnx4_sops;
36 #ifdef CONFIG_QNX4FS_RW
38 int qnx4_sync_inode(struct inode *inode)
40 int err = 0;
41 # if 0
42 struct buffer_head *bh;
44 bh = qnx4_update_inode(inode);
45 if (bh && buffer_dirty(bh))
47 ll_rw_block(WRITE, 1, &bh);
48 wait_on_buffer(bh);
49 if (buffer_req(bh) && !buffer_uptodate(bh))
51 printk ("IO error syncing qnx4 inode [%s:%08lx]\n",
52 inode->i_sb->s_id, inode->i_ino);
53 err = -1;
55 brelse (bh);
56 } else if (!bh) {
57 err = -1;
59 # endif
61 return err;
64 static void qnx4_delete_inode(struct inode *inode)
66 QNX4DEBUG(("qnx4: deleting inode [%lu]\n", (unsigned long) inode->i_ino));
67 inode->i_size = 0;
68 qnx4_truncate(inode);
69 lock_kernel();
70 qnx4_free_inode(inode);
71 unlock_kernel();
74 static void qnx4_write_super(struct super_block *sb)
76 lock_kernel();
77 QNX4DEBUG(("qnx4: write_super\n"));
78 sb->s_dirt = 0;
79 unlock_kernel();
82 static void qnx4_write_inode(struct inode *inode, int unused)
84 struct qnx4_inode_entry *raw_inode;
85 int block, ino;
86 struct buffer_head *bh;
87 ino = inode->i_ino;
89 QNX4DEBUG(("qnx4: write inode 1.\n"));
90 if (inode->i_nlink == 0) {
91 return;
93 if (!ino) {
94 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
95 inode->i_sb->s_id, ino);
96 return;
98 QNX4DEBUG(("qnx4: write inode 2.\n"));
99 block = ino / QNX4_INODES_PER_BLOCK;
100 lock_kernel();
101 if (!(bh = sb_bread(inode->i_sb, block))) {
102 printk("qnx4: major problem: unable to read inode from dev "
103 "%s\n", inode->i_sb->s_id);
104 unlock_kernel();
105 return;
107 raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
108 (ino % QNX4_INODES_PER_BLOCK);
109 raw_inode->di_mode = cpu_to_le16(inode->i_mode);
110 raw_inode->di_uid = cpu_to_le16(fs_high2lowuid(inode->i_uid));
111 raw_inode->di_gid = cpu_to_le16(fs_high2lowgid(inode->i_gid));
112 raw_inode->di_nlink = cpu_to_le16(inode->i_nlink);
113 raw_inode->di_size = cpu_to_le32(inode->i_size);
114 raw_inode->di_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
115 raw_inode->di_atime = cpu_to_le32(inode->i_atime.tv_sec);
116 raw_inode->di_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
117 raw_inode->di_first_xtnt.xtnt_size = cpu_to_le32(inode->i_blocks);
118 mark_buffer_dirty(bh);
119 brelse(bh);
120 unlock_kernel();
123 #endif
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 void qnx4_read_inode(struct inode *);
129 static int qnx4_remount(struct super_block *sb, int *flags, char *data);
130 static int qnx4_statfs(struct super_block *, struct statfs *);
132 static struct super_operations qnx4_sops =
134 .alloc_inode = qnx4_alloc_inode,
135 .destroy_inode = qnx4_destroy_inode,
136 .read_inode = qnx4_read_inode,
137 .put_super = qnx4_put_super,
138 .statfs = qnx4_statfs,
139 .remount_fs = qnx4_remount,
140 #ifdef CONFIG_QNX4FS_RW
141 .write_inode = qnx4_write_inode,
142 .delete_inode = qnx4_delete_inode,
143 .write_super = qnx4_write_super,
144 #endif
147 static int qnx4_remount(struct super_block *sb, int *flags, char *data)
149 struct qnx4_sb_info *qs;
151 qs = qnx4_sb(sb);
152 qs->Version = QNX4_VERSION;
153 if (*flags & MS_RDONLY) {
154 return 0;
156 mark_buffer_dirty(qs->sb_buf);
158 return 0;
161 struct buffer_head *qnx4_getblk(struct inode *inode, int nr,
162 int create)
164 struct buffer_head *result = NULL;
166 if ( nr >= 0 )
167 nr = qnx4_block_map( inode, nr );
168 if (nr) {
169 result = sb_getblk(inode->i_sb, nr);
170 return result;
172 if (!create) {
173 return NULL;
175 #if 0
176 tmp = qnx4_new_block(inode->i_sb);
177 if (!tmp) {
178 return NULL;
180 result = sb_getblk(inode->i_sb, tmp);
181 if (tst) {
182 qnx4_free_block(inode->i_sb, tmp);
183 brelse(result);
184 goto repeat;
186 tst = tmp;
187 #endif
188 inode->i_ctime = CURRENT_TIME;
189 mark_inode_dirty(inode);
190 return result;
193 struct buffer_head *qnx4_bread(struct inode *inode, int block, int create)
195 struct buffer_head *bh;
197 bh = qnx4_getblk(inode, block, create);
198 if (!bh || buffer_uptodate(bh)) {
199 return bh;
201 ll_rw_block(READ, 1, &bh);
202 wait_on_buffer(bh);
203 if (buffer_uptodate(bh)) {
204 return bh;
206 brelse(bh);
208 return NULL;
211 int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
213 unsigned long phys;
215 QNX4DEBUG(("qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
217 phys = qnx4_block_map( inode, iblock );
218 if ( phys ) {
219 // logical block is before EOF
220 map_bh(bh, inode->i_sb, phys);
221 } else if ( create ) {
222 // to be done.
224 return 0;
227 unsigned long qnx4_block_map( struct inode *inode, long iblock )
229 int ix;
230 long offset, i_xblk;
231 unsigned long block = 0;
232 struct buffer_head *bh = 0;
233 struct qnx4_xblk *xblk = 0;
234 struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
235 qnx4_nxtnt_t nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
237 if ( iblock < le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size) ) {
238 // iblock is in the first extent. This is easy.
239 block = le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_blk) + iblock - 1;
240 } else {
241 // iblock is beyond first extent. We have to follow the extent chain.
242 i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
243 offset = iblock - le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size);
244 ix = 0;
245 while ( --nxtnt > 0 ) {
246 if ( ix == 0 ) {
247 // read next xtnt block.
248 bh = sb_bread(inode->i_sb, i_xblk - 1);
249 if ( !bh ) {
250 QNX4DEBUG(("qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
251 return -EIO;
253 xblk = (struct qnx4_xblk*)bh->b_data;
254 if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
255 QNX4DEBUG(("qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
256 return -EIO;
259 if ( offset < le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size) ) {
260 // got it!
261 block = le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_blk) + offset - 1;
262 break;
264 offset -= le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size);
265 if ( ++ix >= xblk->xblk_num_xtnts ) {
266 i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
267 ix = 0;
268 brelse( bh );
269 bh = 0;
272 if ( bh )
273 brelse( bh );
276 QNX4DEBUG(("qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
277 return block;
280 static int qnx4_statfs(struct super_block *sb, struct statfs *buf)
282 lock_kernel();
284 buf->f_type = sb->s_magic;
285 buf->f_bsize = sb->s_blocksize;
286 buf->f_blocks = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
287 buf->f_bfree = qnx4_count_free_blocks(sb);
288 buf->f_bavail = buf->f_bfree;
289 buf->f_namelen = QNX4_NAME_MAX;
291 unlock_kernel();
293 return 0;
297 * Check the root directory of the filesystem to make sure
298 * it really _is_ a qnx4 filesystem, and to check the size
299 * of the directory entry.
301 static const char *qnx4_checkroot(struct super_block *sb)
303 struct buffer_head *bh;
304 struct qnx4_inode_entry *rootdir;
305 int rd, rl;
306 int i, j;
307 int found = 0;
309 if (*(qnx4_sb(sb)->sb->RootDir.di_fname) != '/') {
310 return "no qnx4 filesystem (no root dir).";
311 } else {
312 QNX4DEBUG(("QNX4 filesystem found on dev %s.\n", sb->s_id));
313 rd = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_blk) - 1;
314 rl = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_size);
315 for (j = 0; j < rl; j++) {
316 bh = sb_bread(sb, rd + j); /* root dir, first block */
317 if (bh == NULL) {
318 return "unable to read root entry.";
320 for (i = 0; i < QNX4_INODES_PER_BLOCK; i++) {
321 rootdir = (struct qnx4_inode_entry *) (bh->b_data + i * QNX4_DIR_ENTRY_SIZE);
322 if (rootdir->di_fname != NULL) {
323 QNX4DEBUG(("Rootdir entry found : [%s]\n", rootdir->di_fname));
324 if (!strncmp(rootdir->di_fname, QNX4_BMNAME, sizeof QNX4_BMNAME)) {
325 found = 1;
326 qnx4_sb(sb)->BitMap = kmalloc( sizeof( struct qnx4_inode_entry ), GFP_KERNEL );
327 memcpy( qnx4_sb(sb)->BitMap, rootdir, sizeof( struct qnx4_inode_entry ) ); /* keep bitmap inode known */
328 break;
332 brelse(bh);
333 if (found != 0) {
334 break;
337 if (found == 0) {
338 return "bitmap file not found.";
341 return NULL;
344 static int qnx4_fill_super(struct super_block *s, void *data, int silent)
346 struct buffer_head *bh;
347 struct inode *root;
348 const char *errmsg;
349 struct qnx4_sb_info *qs;
351 qs = kmalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
352 if (!qs)
353 return -ENOMEM;
354 s->s_fs_info = qs;
355 memset(qs, 0, sizeof(struct qnx4_sb_info));
357 sb_set_blocksize(s, QNX4_BLOCK_SIZE);
359 /* Check the superblock signature. Since the qnx4 code is
360 dangerous, we should leave as quickly as possible
361 if we don't belong here... */
362 bh = sb_bread(s, 1);
363 if (!bh) {
364 printk("qnx4: unable to read the superblock\n");
365 goto outnobh;
367 if ( le32_to_cpu( *(__u32*)bh->b_data ) != QNX4_SUPER_MAGIC ) {
368 if (!silent)
369 printk("qnx4: wrong fsid in superblock.\n");
370 goto out;
372 s->s_op = &qnx4_sops;
373 s->s_magic = QNX4_SUPER_MAGIC;
374 #ifndef CONFIG_QNX4FS_RW
375 s->s_flags |= MS_RDONLY; /* Yup, read-only yet */
376 #endif
377 qnx4_sb(s)->sb_buf = bh;
378 qnx4_sb(s)->sb = (struct qnx4_super_block *) bh->b_data;
381 /* check before allocating dentries, inodes, .. */
382 errmsg = qnx4_checkroot(s);
383 if (errmsg != NULL) {
384 if (!silent)
385 printk("qnx4: %s\n", errmsg);
386 goto out;
389 /* does root not have inode number QNX4_ROOT_INO ?? */
390 root = iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
391 if (!root) {
392 printk("qnx4: get inode failed\n");
393 goto out;
396 s->s_root = d_alloc_root(root);
397 if (s->s_root == NULL)
398 goto outi;
400 brelse(bh);
402 return 0;
404 outi:
405 iput(root);
406 out:
407 brelse(bh);
408 outnobh:
409 kfree(qs);
410 s->s_fs_info = NULL;
411 return -EINVAL;
414 static void qnx4_put_super(struct super_block *sb)
416 struct qnx4_sb_info *qs = qnx4_sb(sb);
417 kfree( qs->BitMap );
418 kfree( qs );
419 sb->s_fs_info = NULL;
420 return;
423 static int qnx4_writepage(struct page *page)
425 return block_write_full_page(page,qnx4_get_block);
427 static int qnx4_readpage(struct file *file, struct page *page)
429 return block_read_full_page(page,qnx4_get_block);
431 static int qnx4_prepare_write(struct file *file, struct page *page,
432 unsigned from, unsigned to)
434 struct qnx4_inode_info *qnx4_inode = qnx4_i(page->mapping->host);
435 return cont_prepare_write(page, from, to, qnx4_get_block,
436 &qnx4_inode->mmu_private);
438 static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
440 return generic_block_bmap(mapping,block,qnx4_get_block);
442 struct address_space_operations qnx4_aops = {
443 .readpage = qnx4_readpage,
444 .writepage = qnx4_writepage,
445 .sync_page = block_sync_page,
446 .prepare_write = qnx4_prepare_write,
447 .commit_write = generic_commit_write,
448 .bmap = qnx4_bmap
451 static void qnx4_read_inode(struct inode *inode)
453 struct buffer_head *bh;
454 struct qnx4_inode_entry *raw_inode;
455 int block, ino;
456 struct super_block *sb = inode->i_sb;
457 struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
459 ino = inode->i_ino;
460 inode->i_mode = 0;
462 QNX4DEBUG(("Reading inode : [%d]\n", ino));
463 if (!ino) {
464 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
465 sb->s_id, ino);
466 return;
468 block = ino / QNX4_INODES_PER_BLOCK;
470 if (!(bh = sb_bread(sb, block))) {
471 printk("qnx4: major problem: unable to read inode from dev "
472 "%s\n", sb->s_id);
473 return;
475 raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
476 (ino % QNX4_INODES_PER_BLOCK);
478 inode->i_mode = le16_to_cpu(raw_inode->di_mode);
479 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->di_uid);
480 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->di_gid);
481 inode->i_nlink = le16_to_cpu(raw_inode->di_nlink);
482 inode->i_size = le32_to_cpu(raw_inode->di_size);
483 inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->di_mtime);
484 inode->i_mtime.tv_nsec = 0;
485 inode->i_atime.tv_sec = le32_to_cpu(raw_inode->di_atime);
486 inode->i_atime.tv_nsec = 0;
487 inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->di_ctime);
488 inode->i_ctime.tv_nsec = 0;
489 inode->i_blocks = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
490 inode->i_blksize = QNX4_DIR_ENTRY_SIZE;
492 memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
493 if (S_ISREG(inode->i_mode)) {
494 inode->i_op = &qnx4_file_inode_operations;
495 inode->i_fop = &qnx4_file_operations;
496 inode->i_mapping->a_ops = &qnx4_aops;
497 qnx4_i(inode)->mmu_private = inode->i_size;
498 } else if (S_ISDIR(inode->i_mode)) {
499 inode->i_op = &qnx4_dir_inode_operations;
500 inode->i_fop = &qnx4_dir_operations;
501 } else if (S_ISLNK(inode->i_mode)) {
502 inode->i_op = &page_symlink_inode_operations;
503 inode->i_mapping->a_ops = &qnx4_aops;
504 qnx4_i(inode)->mmu_private = inode->i_size;
505 } else
506 printk("qnx4: bad inode %d on dev %s\n",ino,sb->s_id);
507 brelse(bh);
510 static kmem_cache_t *qnx4_inode_cachep;
512 static struct inode *qnx4_alloc_inode(struct super_block *sb)
514 struct qnx4_inode_info *ei;
515 ei = kmem_cache_alloc(qnx4_inode_cachep, SLAB_KERNEL);
516 if (!ei)
517 return NULL;
518 return &ei->vfs_inode;
521 static void qnx4_destroy_inode(struct inode *inode)
523 kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
526 static void init_once(void *foo, kmem_cache_t * cachep,
527 unsigned long flags)
529 struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
531 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
532 SLAB_CTOR_CONSTRUCTOR)
533 inode_init_once(&ei->vfs_inode);
536 static int init_inodecache(void)
538 qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
539 sizeof(struct qnx4_inode_info),
540 0, SLAB_HWCACHE_ALIGN,
541 init_once, NULL);
542 if (qnx4_inode_cachep == NULL)
543 return -ENOMEM;
544 return 0;
547 static void destroy_inodecache(void)
549 if (kmem_cache_destroy(qnx4_inode_cachep))
550 printk(KERN_INFO
551 "qnx4_inode_cache: not all structures were freed\n");
554 static struct super_block *qnx4_get_sb(struct file_system_type *fs_type,
555 int flags, char *dev_name, void *data)
557 return get_sb_bdev(fs_type, flags, dev_name, data, qnx4_fill_super);
560 static struct file_system_type qnx4_fs_type = {
561 .owner = THIS_MODULE,
562 .name = "qnx4",
563 .get_sb = qnx4_get_sb,
564 .kill_sb = kill_block_super,
565 .fs_flags = FS_REQUIRES_DEV,
568 static int __init init_qnx4_fs(void)
570 int err;
572 err = init_inodecache();
573 if (err)
574 return err;
576 err = register_filesystem(&qnx4_fs_type);
577 if (err) {
578 destroy_inodecache();
579 return err;
582 printk("QNX4 filesystem 0.2.3 registered.\n");
583 return 0;
586 static void __exit exit_qnx4_fs(void)
588 unregister_filesystem(&qnx4_fs_type);
589 destroy_inodecache();
592 module_init(init_qnx4_fs)
593 module_exit(exit_qnx4_fs)
594 MODULE_LICENSE("GPL");