Linux 2.6.31-rc6
[linux-2.6] / fs / qnx4 / inode.c
1 /*
2  * QNX4 file system, Linux implementation.
3  *
4  * Version : 0.2.1
5  *
6  * Using parts of the xiafs filesystem.
7  *
8  * History :
9  *
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.
13  */
14
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>
24 #include "qnx4.h"
25
26 #define QNX4_VERSION  4
27 #define QNX4_BMNAME   ".bitmap"
28
29 static const struct super_operations qnx4_sops;
30
31 #ifdef CONFIG_QNX4FS_RW
32
33 static void qnx4_delete_inode(struct inode *inode)
34 {
35         QNX4DEBUG(("qnx4: deleting inode [%lu]\n", (unsigned long) inode->i_ino));
36         truncate_inode_pages(&inode->i_data, 0);
37         inode->i_size = 0;
38         qnx4_truncate(inode);
39         lock_kernel();
40         qnx4_free_inode(inode);
41         unlock_kernel();
42 }
43
44 static int qnx4_write_inode(struct inode *inode, int do_sync)
45 {
46         struct qnx4_inode_entry *raw_inode;
47         int block, ino;
48         struct buffer_head *bh;
49         ino = inode->i_ino;
50
51         QNX4DEBUG(("qnx4: write inode 1.\n"));
52         if (inode->i_nlink == 0) {
53                 return 0;
54         }
55         if (!ino) {
56                 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
57                        inode->i_sb->s_id, ino);
58                 return -EIO;
59         }
60         QNX4DEBUG(("qnx4: write inode 2.\n"));
61         block = ino / QNX4_INODES_PER_BLOCK;
62         lock_kernel();
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);
66                 unlock_kernel();
67                 return -EIO;
68         }
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);
81         if (do_sync) {
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);
86                         brelse(bh);
87                         unlock_kernel();
88                         return -EIO;
89                 }
90         }
91         brelse(bh);
92         unlock_kernel();
93         return 0;
94 }
95
96 #endif
97
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 *);
103
104 static const struct super_operations qnx4_sops =
105 {
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,
114 #endif
115 };
116
117 static int qnx4_remount(struct super_block *sb, int *flags, char *data)
118 {
119         struct qnx4_sb_info *qs;
120
121         qs = qnx4_sb(sb);
122         qs->Version = QNX4_VERSION;
123 #ifndef CONFIG_QNX4FS_RW
124         *flags |= MS_RDONLY;
125 #endif
126         if (*flags & MS_RDONLY) {
127                 return 0;
128         }
129
130         mark_buffer_dirty(qs->sb_buf);
131
132         return 0;
133 }
134
135 static struct buffer_head *qnx4_getblk(struct inode *inode, int nr,
136                                        int create)
137 {
138         struct buffer_head *result = NULL;
139
140         if ( nr >= 0 )
141                 nr = qnx4_block_map( inode, nr );
142         if (nr) {
143                 result = sb_getblk(inode->i_sb, nr);
144                 return result;
145         }
146         if (!create) {
147                 return NULL;
148         }
149 #if 0
150         tmp = qnx4_new_block(inode->i_sb);
151         if (!tmp) {
152                 return NULL;
153         }
154         result = sb_getblk(inode->i_sb, tmp);
155         if (tst) {
156                 qnx4_free_block(inode->i_sb, tmp);
157                 brelse(result);
158                 goto repeat;
159         }
160         tst = tmp;
161 #endif
162         inode->i_ctime = CURRENT_TIME_SEC;
163         mark_inode_dirty(inode);
164         return result;
165 }
166
167 struct buffer_head *qnx4_bread(struct inode *inode, int block, int create)
168 {
169         struct buffer_head *bh;
170
171         bh = qnx4_getblk(inode, block, create);
172         if (!bh || buffer_uptodate(bh)) {
173                 return bh;
174         }
175         ll_rw_block(READ, 1, &bh);
176         wait_on_buffer(bh);
177         if (buffer_uptodate(bh)) {
178                 return bh;
179         }
180         brelse(bh);
181
182         return NULL;
183 }
184
185 static int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
186 {
187         unsigned long phys;
188
189         QNX4DEBUG(("qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
190
191         phys = qnx4_block_map( inode, iblock );
192         if ( phys ) {
193                 // logical block is before EOF
194                 map_bh(bh, inode->i_sb, phys);
195         } else if ( create ) {
196                 // to be done.
197         }
198         return 0;
199 }
200
201 unsigned long qnx4_block_map( struct inode *inode, long iblock )
202 {
203         int ix;
204         long offset, i_xblk;
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);
210
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;
214         } else {
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);
218                 ix = 0;
219                 while ( --nxtnt > 0 ) {
220                         if ( ix == 0 ) {
221                                 // read next xtnt block.
222                                 bh = sb_bread(inode->i_sb, i_xblk - 1);
223                                 if ( !bh ) {
224                                         QNX4DEBUG(("qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
225                                         return -EIO;
226                                 }
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));
230                                         return -EIO;
231                                 }
232                         }
233                         if ( offset < le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size) ) {
234                                 // got it!
235                                 block = le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_blk) + offset - 1;
236                                 break;
237                         }
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);
241                                 ix = 0;
242                                 brelse( bh );
243                                 bh = NULL;
244                         }
245                 }
246                 if ( bh )
247                         brelse( bh );
248         }
249
250         QNX4DEBUG(("qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
251         return block;
252 }
253
254 static int qnx4_statfs(struct dentry *dentry, struct kstatfs *buf)
255 {
256         struct super_block *sb = dentry->d_sb;
257         u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
258
259         lock_kernel();
260
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);
269
270         unlock_kernel();
271
272         return 0;
273 }
274
275 /*
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.
279  */
280 static const char *qnx4_checkroot(struct super_block *sb)
281 {
282         struct buffer_head *bh;
283         struct qnx4_inode_entry *rootdir;
284         int rd, rl;
285         int i, j;
286         int found = 0;
287
288         if (*(qnx4_sb(sb)->sb->RootDir.di_fname) != '/') {
289                 return "no qnx4 filesystem (no root dir).";
290         } else {
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 */
296                         if (bh == NULL) {
297                                 return "unable to read root entry.";
298                         }
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)) {
304                                                 found = 1;
305                                                 qnx4_sb(sb)->BitMap = kmalloc( sizeof( struct qnx4_inode_entry ), GFP_KERNEL );
306                                                 if (!qnx4_sb(sb)->BitMap) {
307                                                         brelse (bh);
308                                                         return "not enough memory for bitmap inode";
309                                                 }
310                                                 memcpy( qnx4_sb(sb)->BitMap, rootdir, sizeof( struct qnx4_inode_entry ) );      /* keep bitmap inode known */
311                                                 break;
312                                         }
313                                 }
314                         }
315                         brelse(bh);
316                         if (found != 0) {
317                                 break;
318                         }
319                 }
320                 if (found == 0) {
321                         return "bitmap file not found.";
322                 }
323         }
324         return NULL;
325 }
326
327 static int qnx4_fill_super(struct super_block *s, void *data, int silent)
328 {
329         struct buffer_head *bh;
330         struct inode *root;
331         const char *errmsg;
332         struct qnx4_sb_info *qs;
333         int ret = -EINVAL;
334
335         qs = kzalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
336         if (!qs)
337                 return -ENOMEM;
338         s->s_fs_info = qs;
339
340         sb_set_blocksize(s, QNX4_BLOCK_SIZE);
341
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... */
345         bh = sb_bread(s, 1);
346         if (!bh) {
347                 printk("qnx4: unable to read the superblock\n");
348                 goto outnobh;
349         }
350         if ( le32_to_cpup((__le32*) bh->b_data) != QNX4_SUPER_MAGIC ) {
351                 if (!silent)
352                         printk("qnx4: wrong fsid in superblock.\n");
353                 goto out;
354         }
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 */
359 #endif
360         qnx4_sb(s)->sb_buf = bh;
361         qnx4_sb(s)->sb = (struct qnx4_super_block *) bh->b_data;
362
363
364         /* check before allocating dentries, inodes, .. */
365         errmsg = qnx4_checkroot(s);
366         if (errmsg != NULL) {
367                 if (!silent)
368                         printk("qnx4: %s\n", errmsg);
369                 goto out;
370         }
371
372         /* does root not have inode number QNX4_ROOT_INO ?? */
373         root = qnx4_iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
374         if (IS_ERR(root)) {
375                 printk("qnx4: get inode failed\n");
376                 ret = PTR_ERR(root);
377                 goto out;
378         }
379
380         ret = -ENOMEM;
381         s->s_root = d_alloc_root(root);
382         if (s->s_root == NULL)
383                 goto outi;
384
385         brelse(bh);
386
387         return 0;
388
389       outi:
390         iput(root);
391       out:
392         brelse(bh);
393       outnobh:
394         kfree(qs);
395         s->s_fs_info = NULL;
396         return ret;
397 }
398
399 static void qnx4_put_super(struct super_block *sb)
400 {
401         struct qnx4_sb_info *qs = qnx4_sb(sb);
402         kfree( qs->BitMap );
403         kfree( qs );
404         sb->s_fs_info = NULL;
405         return;
406 }
407
408 static int qnx4_writepage(struct page *page, struct writeback_control *wbc)
409 {
410         return block_write_full_page(page,qnx4_get_block, wbc);
411 }
412
413 static int qnx4_readpage(struct file *file, struct page *page)
414 {
415         return block_read_full_page(page,qnx4_get_block);
416 }
417
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)
421 {
422         struct qnx4_inode_info *qnx4_inode = qnx4_i(mapping->host);
423         *pagep = NULL;
424         return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
425                                 qnx4_get_block,
426                                 &qnx4_inode->mmu_private);
427 }
428 static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
429 {
430         return generic_block_bmap(mapping,block,qnx4_get_block);
431 }
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,
438         .bmap           = qnx4_bmap
439 };
440
441 struct inode *qnx4_iget(struct super_block *sb, unsigned long ino)
442 {
443         struct buffer_head *bh;
444         struct qnx4_inode_entry *raw_inode;
445         int block;
446         struct qnx4_inode_entry *qnx4_inode;
447         struct inode *inode;
448
449         inode = iget_locked(sb, ino);
450         if (!inode)
451                 return ERR_PTR(-ENOMEM);
452         if (!(inode->i_state & I_NEW))
453                 return inode;
454
455         qnx4_inode = qnx4_raw_inode(inode);
456         inode->i_mode = 0;
457
458         QNX4DEBUG(("Reading inode : [%d]\n", ino));
459         if (!ino) {
460                 printk(KERN_ERR "qnx4: bad inode number on dev %s: %lu is "
461                                 "out of range\n",
462                        sb->s_id, ino);
463                 iget_failed(inode);
464                 return ERR_PTR(-EIO);
465         }
466         block = ino / QNX4_INODES_PER_BLOCK;
467
468         if (!(bh = sb_bread(sb, block))) {
469                 printk("qnx4: major problem: unable to read inode from dev "
470                        "%s\n", sb->s_id);
471                 iget_failed(inode);
472                 return ERR_PTR(-EIO);
473         }
474         raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
475             (ino % QNX4_INODES_PER_BLOCK);
476
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);
489
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;
503         } else {
504                 printk(KERN_ERR "qnx4: bad inode %lu on dev %s\n",
505                         ino, sb->s_id);
506                 iget_failed(inode);
507                 brelse(bh);
508                 return ERR_PTR(-EIO);
509         }
510         brelse(bh);
511         unlock_new_inode(inode);
512         return inode;
513 }
514
515 static struct kmem_cache *qnx4_inode_cachep;
516
517 static struct inode *qnx4_alloc_inode(struct super_block *sb)
518 {
519         struct qnx4_inode_info *ei;
520         ei = kmem_cache_alloc(qnx4_inode_cachep, GFP_KERNEL);
521         if (!ei)
522                 return NULL;
523         return &ei->vfs_inode;
524 }
525
526 static void qnx4_destroy_inode(struct inode *inode)
527 {
528         kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
529 }
530
531 static void init_once(void *foo)
532 {
533         struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
534
535         inode_init_once(&ei->vfs_inode);
536 }
537
538 static int init_inodecache(void)
539 {
540         qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
541                                              sizeof(struct qnx4_inode_info),
542                                              0, (SLAB_RECLAIM_ACCOUNT|
543                                                 SLAB_MEM_SPREAD),
544                                              init_once);
545         if (qnx4_inode_cachep == NULL)
546                 return -ENOMEM;
547         return 0;
548 }
549
550 static void destroy_inodecache(void)
551 {
552         kmem_cache_destroy(qnx4_inode_cachep);
553 }
554
555 static int qnx4_get_sb(struct file_system_type *fs_type,
556         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
557 {
558         return get_sb_bdev(fs_type, flags, dev_name, data, qnx4_fill_super,
559                            mnt);
560 }
561
562 static struct file_system_type qnx4_fs_type = {
563         .owner          = THIS_MODULE,
564         .name           = "qnx4",
565         .get_sb         = qnx4_get_sb,
566         .kill_sb        = kill_block_super,
567         .fs_flags       = FS_REQUIRES_DEV,
568 };
569
570 static int __init init_qnx4_fs(void)
571 {
572         int err;
573
574         err = init_inodecache();
575         if (err)
576                 return err;
577
578         err = register_filesystem(&qnx4_fs_type);
579         if (err) {
580                 destroy_inodecache();
581                 return err;
582         }
583
584         printk("QNX4 filesystem 0.2.3 registered.\n");
585         return 0;
586 }
587
588 static void __exit exit_qnx4_fs(void)
589 {
590         unregister_filesystem(&qnx4_fs_type);
591         destroy_inodecache();
592 }
593
594 module_init(init_qnx4_fs)
595 module_exit(exit_qnx4_fs)
596 MODULE_LICENSE("GPL");
597