[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / qnx4 / inode.c
blobc894b7ecac0097b519bd6890053dab54086437bd
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>
28 #include <linux/vfs.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 sync_dirty_buffer(bh);
48 if (buffer_req(bh) && !buffer_uptodate(bh))
50 printk ("IO error syncing qnx4 inode [%s:%08lx]\n",
51 inode->i_sb->s_id, 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 inode->i_size = 0;
67 qnx4_truncate(inode);
68 lock_kernel();
69 qnx4_free_inode(inode);
70 unlock_kernel();
73 static void qnx4_write_super(struct super_block *sb)
75 lock_kernel();
76 QNX4DEBUG(("qnx4: write_super\n"));
77 sb->s_dirt = 0;
78 unlock_kernel();
81 static void qnx4_write_inode(struct inode *inode, int unused)
83 struct qnx4_inode_entry *raw_inode;
84 int block, ino;
85 struct buffer_head *bh;
86 ino = inode->i_ino;
88 QNX4DEBUG(("qnx4: write inode 1.\n"));
89 if (inode->i_nlink == 0) {
90 return;
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;
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;
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();
122 #endif
124 static void qnx4_put_super(struct super_block *sb);
125 static struct inode *qnx4_alloc_inode(struct super_block *sb);
126 static void qnx4_destroy_inode(struct inode *inode);
127 static void qnx4_read_inode(struct inode *);
128 static int qnx4_remount(struct super_block *sb, int *flags, char *data);
129 static int qnx4_statfs(struct super_block *, struct kstatfs *);
131 static struct super_operations qnx4_sops =
133 .alloc_inode = qnx4_alloc_inode,
134 .destroy_inode = qnx4_destroy_inode,
135 .read_inode = qnx4_read_inode,
136 .put_super = qnx4_put_super,
137 .statfs = qnx4_statfs,
138 .remount_fs = qnx4_remount,
139 #ifdef CONFIG_QNX4FS_RW
140 .write_inode = qnx4_write_inode,
141 .delete_inode = qnx4_delete_inode,
142 .write_super = qnx4_write_super,
143 #endif
146 static int qnx4_remount(struct super_block *sb, int *flags, char *data)
148 struct qnx4_sb_info *qs;
150 qs = qnx4_sb(sb);
151 qs->Version = QNX4_VERSION;
152 if (*flags & MS_RDONLY) {
153 return 0;
155 mark_buffer_dirty(qs->sb_buf);
157 return 0;
160 struct buffer_head *qnx4_getblk(struct inode *inode, int nr,
161 int create)
163 struct buffer_head *result = NULL;
165 if ( nr >= 0 )
166 nr = qnx4_block_map( inode, nr );
167 if (nr) {
168 result = sb_getblk(inode->i_sb, nr);
169 return result;
171 if (!create) {
172 return NULL;
174 #if 0
175 tmp = qnx4_new_block(inode->i_sb);
176 if (!tmp) {
177 return NULL;
179 result = sb_getblk(inode->i_sb, tmp);
180 if (tst) {
181 qnx4_free_block(inode->i_sb, tmp);
182 brelse(result);
183 goto repeat;
185 tst = tmp;
186 #endif
187 inode->i_ctime = CURRENT_TIME;
188 mark_inode_dirty(inode);
189 return result;
192 struct buffer_head *qnx4_bread(struct inode *inode, int block, int create)
194 struct buffer_head *bh;
196 bh = qnx4_getblk(inode, block, create);
197 if (!bh || buffer_uptodate(bh)) {
198 return bh;
200 ll_rw_block(READ, 1, &bh);
201 wait_on_buffer(bh);
202 if (buffer_uptodate(bh)) {
203 return bh;
205 brelse(bh);
207 return NULL;
210 int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
212 unsigned long phys;
214 QNX4DEBUG(("qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
216 phys = qnx4_block_map( inode, iblock );
217 if ( phys ) {
218 // logical block is before EOF
219 map_bh(bh, inode->i_sb, phys);
220 } else if ( create ) {
221 // to be done.
223 return 0;
226 unsigned long qnx4_block_map( struct inode *inode, long iblock )
228 int ix;
229 long offset, i_xblk;
230 unsigned long block = 0;
231 struct buffer_head *bh = 0;
232 struct qnx4_xblk *xblk = 0;
233 struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
234 qnx4_nxtnt_t nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
236 if ( iblock < le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size) ) {
237 // iblock is in the first extent. This is easy.
238 block = le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_blk) + iblock - 1;
239 } else {
240 // iblock is beyond first extent. We have to follow the extent chain.
241 i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
242 offset = iblock - le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size);
243 ix = 0;
244 while ( --nxtnt > 0 ) {
245 if ( ix == 0 ) {
246 // read next xtnt block.
247 bh = sb_bread(inode->i_sb, i_xblk - 1);
248 if ( !bh ) {
249 QNX4DEBUG(("qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
250 return -EIO;
252 xblk = (struct qnx4_xblk*)bh->b_data;
253 if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
254 QNX4DEBUG(("qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
255 return -EIO;
258 if ( offset < le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size) ) {
259 // got it!
260 block = le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_blk) + offset - 1;
261 break;
263 offset -= le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size);
264 if ( ++ix >= xblk->xblk_num_xtnts ) {
265 i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
266 ix = 0;
267 brelse( bh );
268 bh = 0;
271 if ( bh )
272 brelse( bh );
275 QNX4DEBUG(("qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
276 return block;
279 static int qnx4_statfs(struct super_block *sb, struct kstatfs *buf)
281 lock_kernel();
283 buf->f_type = sb->s_magic;
284 buf->f_bsize = sb->s_blocksize;
285 buf->f_blocks = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
286 buf->f_bfree = qnx4_count_free_blocks(sb);
287 buf->f_bavail = buf->f_bfree;
288 buf->f_namelen = QNX4_NAME_MAX;
290 unlock_kernel();
292 return 0;
296 * Check the root directory of the filesystem to make sure
297 * it really _is_ a qnx4 filesystem, and to check the size
298 * of the directory entry.
300 static const char *qnx4_checkroot(struct super_block *sb)
302 struct buffer_head *bh;
303 struct qnx4_inode_entry *rootdir;
304 int rd, rl;
305 int i, j;
306 int found = 0;
308 if (*(qnx4_sb(sb)->sb->RootDir.di_fname) != '/') {
309 return "no qnx4 filesystem (no root dir).";
310 } else {
311 QNX4DEBUG(("QNX4 filesystem found on dev %s.\n", sb->s_id));
312 rd = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_blk) - 1;
313 rl = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_size);
314 for (j = 0; j < rl; j++) {
315 bh = sb_bread(sb, rd + j); /* root dir, first block */
316 if (bh == NULL) {
317 return "unable to read root entry.";
319 for (i = 0; i < QNX4_INODES_PER_BLOCK; i++) {
320 rootdir = (struct qnx4_inode_entry *) (bh->b_data + i * QNX4_DIR_ENTRY_SIZE);
321 if (rootdir->di_fname != NULL) {
322 QNX4DEBUG(("Rootdir entry found : [%s]\n", rootdir->di_fname));
323 if (!strncmp(rootdir->di_fname, QNX4_BMNAME, sizeof QNX4_BMNAME)) {
324 found = 1;
325 qnx4_sb(sb)->BitMap = kmalloc( sizeof( struct qnx4_inode_entry ), GFP_KERNEL );
326 if (!qnx4_sb(sb)->BitMap) {
327 brelse (bh);
328 return "not enough memory for bitmap inode";
330 memcpy( qnx4_sb(sb)->BitMap, rootdir, sizeof( struct qnx4_inode_entry ) ); /* keep bitmap inode known */
331 break;
335 brelse(bh);
336 if (found != 0) {
337 break;
340 if (found == 0) {
341 return "bitmap file not found.";
344 return NULL;
347 static int qnx4_fill_super(struct super_block *s, void *data, int silent)
349 struct buffer_head *bh;
350 struct inode *root;
351 const char *errmsg;
352 struct qnx4_sb_info *qs;
354 qs = kmalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
355 if (!qs)
356 return -ENOMEM;
357 s->s_fs_info = qs;
358 memset(qs, 0, sizeof(struct qnx4_sb_info));
360 sb_set_blocksize(s, QNX4_BLOCK_SIZE);
362 /* Check the superblock signature. Since the qnx4 code is
363 dangerous, we should leave as quickly as possible
364 if we don't belong here... */
365 bh = sb_bread(s, 1);
366 if (!bh) {
367 printk("qnx4: unable to read the superblock\n");
368 goto outnobh;
370 if ( le32_to_cpu( *(__u32*)bh->b_data ) != QNX4_SUPER_MAGIC ) {
371 if (!silent)
372 printk("qnx4: wrong fsid in superblock.\n");
373 goto out;
375 s->s_op = &qnx4_sops;
376 s->s_magic = QNX4_SUPER_MAGIC;
377 #ifndef CONFIG_QNX4FS_RW
378 s->s_flags |= MS_RDONLY; /* Yup, read-only yet */
379 #endif
380 qnx4_sb(s)->sb_buf = bh;
381 qnx4_sb(s)->sb = (struct qnx4_super_block *) bh->b_data;
384 /* check before allocating dentries, inodes, .. */
385 errmsg = qnx4_checkroot(s);
386 if (errmsg != NULL) {
387 if (!silent)
388 printk("qnx4: %s\n", errmsg);
389 goto out;
392 /* does root not have inode number QNX4_ROOT_INO ?? */
393 root = iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
394 if (!root) {
395 printk("qnx4: get inode failed\n");
396 goto out;
399 s->s_root = d_alloc_root(root);
400 if (s->s_root == NULL)
401 goto outi;
403 brelse(bh);
405 return 0;
407 outi:
408 iput(root);
409 out:
410 brelse(bh);
411 outnobh:
412 kfree(qs);
413 s->s_fs_info = NULL;
414 return -EINVAL;
417 static void qnx4_put_super(struct super_block *sb)
419 struct qnx4_sb_info *qs = qnx4_sb(sb);
420 kfree( qs->BitMap );
421 kfree( qs );
422 sb->s_fs_info = NULL;
423 return;
426 static int qnx4_writepage(struct page *page, struct writeback_control *wbc)
428 return block_write_full_page(page,qnx4_get_block, wbc);
430 static int qnx4_readpage(struct file *file, struct page *page)
432 return block_read_full_page(page,qnx4_get_block);
434 static int qnx4_prepare_write(struct file *file, struct page *page,
435 unsigned from, unsigned to)
437 struct qnx4_inode_info *qnx4_inode = qnx4_i(page->mapping->host);
438 return cont_prepare_write(page, from, to, qnx4_get_block,
439 &qnx4_inode->mmu_private);
441 static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
443 return generic_block_bmap(mapping,block,qnx4_get_block);
445 struct address_space_operations qnx4_aops = {
446 .readpage = qnx4_readpage,
447 .writepage = qnx4_writepage,
448 .sync_page = block_sync_page,
449 .prepare_write = qnx4_prepare_write,
450 .commit_write = generic_commit_write,
451 .bmap = qnx4_bmap
454 static void qnx4_read_inode(struct inode *inode)
456 struct buffer_head *bh;
457 struct qnx4_inode_entry *raw_inode;
458 int block, ino;
459 struct super_block *sb = inode->i_sb;
460 struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
462 ino = inode->i_ino;
463 inode->i_mode = 0;
465 QNX4DEBUG(("Reading inode : [%d]\n", ino));
466 if (!ino) {
467 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
468 sb->s_id, ino);
469 return;
471 block = ino / QNX4_INODES_PER_BLOCK;
473 if (!(bh = sb_bread(sb, block))) {
474 printk("qnx4: major problem: unable to read inode from dev "
475 "%s\n", sb->s_id);
476 return;
478 raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
479 (ino % QNX4_INODES_PER_BLOCK);
481 inode->i_mode = le16_to_cpu(raw_inode->di_mode);
482 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->di_uid);
483 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->di_gid);
484 inode->i_nlink = le16_to_cpu(raw_inode->di_nlink);
485 inode->i_size = le32_to_cpu(raw_inode->di_size);
486 inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->di_mtime);
487 inode->i_mtime.tv_nsec = 0;
488 inode->i_atime.tv_sec = le32_to_cpu(raw_inode->di_atime);
489 inode->i_atime.tv_nsec = 0;
490 inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->di_ctime);
491 inode->i_ctime.tv_nsec = 0;
492 inode->i_blocks = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
493 inode->i_blksize = QNX4_DIR_ENTRY_SIZE;
495 memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
496 if (S_ISREG(inode->i_mode)) {
497 inode->i_op = &qnx4_file_inode_operations;
498 inode->i_fop = &qnx4_file_operations;
499 inode->i_mapping->a_ops = &qnx4_aops;
500 qnx4_i(inode)->mmu_private = inode->i_size;
501 } else if (S_ISDIR(inode->i_mode)) {
502 inode->i_op = &qnx4_dir_inode_operations;
503 inode->i_fop = &qnx4_dir_operations;
504 } else if (S_ISLNK(inode->i_mode)) {
505 inode->i_op = &page_symlink_inode_operations;
506 inode->i_mapping->a_ops = &qnx4_aops;
507 qnx4_i(inode)->mmu_private = inode->i_size;
508 } else
509 printk("qnx4: bad inode %d on dev %s\n",ino,sb->s_id);
510 brelse(bh);
513 static kmem_cache_t *qnx4_inode_cachep;
515 static struct inode *qnx4_alloc_inode(struct super_block *sb)
517 struct qnx4_inode_info *ei;
518 ei = kmem_cache_alloc(qnx4_inode_cachep, SLAB_KERNEL);
519 if (!ei)
520 return NULL;
521 return &ei->vfs_inode;
524 static void qnx4_destroy_inode(struct inode *inode)
526 kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
529 static void init_once(void *foo, kmem_cache_t * cachep,
530 unsigned long flags)
532 struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
534 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
535 SLAB_CTOR_CONSTRUCTOR)
536 inode_init_once(&ei->vfs_inode);
539 static int init_inodecache(void)
541 qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
542 sizeof(struct qnx4_inode_info),
543 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
544 init_once, NULL);
545 if (qnx4_inode_cachep == NULL)
546 return -ENOMEM;
547 return 0;
550 static void destroy_inodecache(void)
552 if (kmem_cache_destroy(qnx4_inode_cachep))
553 printk(KERN_INFO
554 "qnx4_inode_cache: not all structures were freed\n");
557 static struct super_block *qnx4_get_sb(struct file_system_type *fs_type,
558 int flags, const char *dev_name, void *data)
560 return get_sb_bdev(fs_type, flags, dev_name, data, qnx4_fill_super);
563 static struct file_system_type qnx4_fs_type = {
564 .owner = THIS_MODULE,
565 .name = "qnx4",
566 .get_sb = qnx4_get_sb,
567 .kill_sb = kill_block_super,
568 .fs_flags = FS_REQUIRES_DEV,
571 static int __init init_qnx4_fs(void)
573 int err;
575 err = init_inodecache();
576 if (err)
577 return err;
579 err = register_filesystem(&qnx4_fs_type);
580 if (err) {
581 destroy_inodecache();
582 return err;
585 printk("QNX4 filesystem 0.2.3 registered.\n");
586 return 0;
589 static void __exit exit_qnx4_fs(void)
591 unregister_filesystem(&qnx4_fs_type);
592 destroy_inodecache();
595 module_init(init_qnx4_fs)
596 module_exit(exit_qnx4_fs)
597 MODULE_LICENSE("GPL");