ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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/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>
30
31 #define QNX4_VERSION  4
32 #define QNX4_BMNAME   ".bitmap"
33
34 static struct super_operations qnx4_sops;
35
36 #ifdef CONFIG_QNX4FS_RW
37
38 int qnx4_sync_inode(struct inode *inode)
39 {
40         int err = 0;
41 # if 0
42         struct buffer_head *bh;
43
44         bh = qnx4_update_inode(inode);
45         if (bh && buffer_dirty(bh))
46         {
47                 sync_dirty_buffer(bh);
48                 if (buffer_req(bh) && !buffer_uptodate(bh))
49                 {
50                         printk ("IO error syncing qnx4 inode [%s:%08lx]\n",
51                                 inode->i_sb->s_id, inode->i_ino);
52                         err = -1;
53                 }
54                 brelse (bh);
55         } else if (!bh) {
56                 err = -1;
57         }
58 # endif
59
60         return err;
61 }
62
63 static void qnx4_delete_inode(struct inode *inode)
64 {
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();
71 }
72
73 static void qnx4_write_super(struct super_block *sb)
74 {
75         lock_kernel();
76         QNX4DEBUG(("qnx4: write_super\n"));
77         sb->s_dirt = 0;
78         unlock_kernel();
79 }
80
81 static void qnx4_write_inode(struct inode *inode, int unused)
82 {
83         struct qnx4_inode_entry *raw_inode;
84         int block, ino;
85         struct buffer_head *bh;
86         ino = inode->i_ino;
87
88         QNX4DEBUG(("qnx4: write inode 1.\n"));
89         if (inode->i_nlink == 0) {
90                 return;
91         }
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;
96         }
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;
105         }
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();
120 }
121
122 #endif
123
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 *);
130
131 static struct super_operations qnx4_sops =
132 {
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
144 };
145
146 static int qnx4_remount(struct super_block *sb, int *flags, char *data)
147 {
148         struct qnx4_sb_info *qs;
149
150         qs = qnx4_sb(sb);
151         qs->Version = QNX4_VERSION;
152 #ifndef CONFIG_QNX4FS_RW
153         *flags |= MS_RDONLY;
154 #endif
155         if (*flags & MS_RDONLY) {
156                 return 0;
157         }
158
159         mark_buffer_dirty(qs->sb_buf);
160
161         return 0;
162 }
163
164 struct buffer_head *qnx4_getblk(struct inode *inode, int nr,
165                                  int create)
166 {
167         struct buffer_head *result = NULL;
168
169         if ( nr >= 0 )
170                 nr = qnx4_block_map( inode, nr );
171         if (nr) {
172                 result = sb_getblk(inode->i_sb, nr);
173                 return result;
174         }
175         if (!create) {
176                 return NULL;
177         }
178 #if 0
179         tmp = qnx4_new_block(inode->i_sb);
180         if (!tmp) {
181                 return NULL;
182         }
183         result = sb_getblk(inode->i_sb, tmp);
184         if (tst) {
185                 qnx4_free_block(inode->i_sb, tmp);
186                 brelse(result);
187                 goto repeat;
188         }
189         tst = tmp;
190 #endif
191         inode->i_ctime = CURRENT_TIME;
192         mark_inode_dirty(inode);
193         return result;
194 }
195
196 struct buffer_head *qnx4_bread(struct inode *inode, int block, int create)
197 {
198         struct buffer_head *bh;
199
200         bh = qnx4_getblk(inode, block, create);
201         if (!bh || buffer_uptodate(bh)) {
202                 return bh;
203         }
204         ll_rw_block(READ, 1, &bh);
205         wait_on_buffer(bh);
206         if (buffer_uptodate(bh)) {
207                 return bh;
208         }
209         brelse(bh);
210
211         return NULL;
212 }
213
214 int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
215 {
216         unsigned long phys;
217
218         QNX4DEBUG(("qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
219
220         phys = qnx4_block_map( inode, iblock );
221         if ( phys ) {
222                 // logical block is before EOF
223                 map_bh(bh, inode->i_sb, phys);
224         } else if ( create ) {
225                 // to be done.
226         }
227         return 0;
228 }
229
230 unsigned long qnx4_block_map( struct inode *inode, long iblock )
231 {
232         int ix;
233         long offset, i_xblk;
234         unsigned long block = 0;
235         struct buffer_head *bh = 0;
236         struct qnx4_xblk *xblk = 0;
237         struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
238         qnx4_nxtnt_t nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
239
240         if ( iblock < le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size) ) {
241                 // iblock is in the first extent. This is easy.
242                 block = le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_blk) + iblock - 1;
243         } else {
244                 // iblock is beyond first extent. We have to follow the extent chain.
245                 i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
246                 offset = iblock - le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size);
247                 ix = 0;
248                 while ( --nxtnt > 0 ) {
249                         if ( ix == 0 ) {
250                                 // read next xtnt block.
251                                 bh = sb_bread(inode->i_sb, i_xblk - 1);
252                                 if ( !bh ) {
253                                         QNX4DEBUG(("qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
254                                         return -EIO;
255                                 }
256                                 xblk = (struct qnx4_xblk*)bh->b_data;
257                                 if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
258                                         QNX4DEBUG(("qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
259                                         return -EIO;
260                                 }
261                         }
262                         if ( offset < le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size) ) {
263                                 // got it!
264                                 block = le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_blk) + offset - 1;
265                                 break;
266                         }
267                         offset -= le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size);
268                         if ( ++ix >= xblk->xblk_num_xtnts ) {
269                                 i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
270                                 ix = 0;
271                                 brelse( bh );
272                                 bh = 0;
273                         }
274                 }
275                 if ( bh )
276                         brelse( bh );
277         }
278
279         QNX4DEBUG(("qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
280         return block;
281 }
282
283 static int qnx4_statfs(struct super_block *sb, struct kstatfs *buf)
284 {
285         lock_kernel();
286
287         buf->f_type    = sb->s_magic;
288         buf->f_bsize   = sb->s_blocksize;
289         buf->f_blocks  = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
290         buf->f_bfree   = qnx4_count_free_blocks(sb);
291         buf->f_bavail  = buf->f_bfree;
292         buf->f_namelen = QNX4_NAME_MAX;
293
294         unlock_kernel();
295
296         return 0;
297 }
298
299 /*
300  * Check the root directory of the filesystem to make sure
301  * it really _is_ a qnx4 filesystem, and to check the size
302  * of the directory entry.
303  */
304 static const char *qnx4_checkroot(struct super_block *sb)
305 {
306         struct buffer_head *bh;
307         struct qnx4_inode_entry *rootdir;
308         int rd, rl;
309         int i, j;
310         int found = 0;
311
312         if (*(qnx4_sb(sb)->sb->RootDir.di_fname) != '/') {
313                 return "no qnx4 filesystem (no root dir).";
314         } else {
315                 QNX4DEBUG(("QNX4 filesystem found on dev %s.\n", sb->s_id));
316                 rd = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_blk) - 1;
317                 rl = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_size);
318                 for (j = 0; j < rl; j++) {
319                         bh = sb_bread(sb, rd + j);      /* root dir, first block */
320                         if (bh == NULL) {
321                                 return "unable to read root entry.";
322                         }
323                         for (i = 0; i < QNX4_INODES_PER_BLOCK; i++) {
324                                 rootdir = (struct qnx4_inode_entry *) (bh->b_data + i * QNX4_DIR_ENTRY_SIZE);
325                                 if (rootdir->di_fname != NULL) {
326                                         QNX4DEBUG(("Rootdir entry found : [%s]\n", rootdir->di_fname));
327                                         if (!strncmp(rootdir->di_fname, QNX4_BMNAME, sizeof QNX4_BMNAME)) {
328                                                 found = 1;
329                                                 qnx4_sb(sb)->BitMap = kmalloc( sizeof( struct qnx4_inode_entry ), GFP_KERNEL );
330                                                 if (!qnx4_sb(sb)->BitMap) {
331                                                         brelse (bh);
332                                                         return "not enough memory for bitmap inode";
333                                                 }
334                                                 memcpy( qnx4_sb(sb)->BitMap, rootdir, sizeof( struct qnx4_inode_entry ) );      /* keep bitmap inode known */
335                                                 break;
336                                         }
337                                 }
338                         }
339                         brelse(bh);
340                         if (found != 0) {
341                                 break;
342                         }
343                 }
344                 if (found == 0) {
345                         return "bitmap file not found.";
346                 }
347         }
348         return NULL;
349 }
350
351 static int qnx4_fill_super(struct super_block *s, void *data, int silent)
352 {
353         struct buffer_head *bh;
354         struct inode *root;
355         const char *errmsg;
356         struct qnx4_sb_info *qs;
357
358         qs = kmalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
359         if (!qs)
360                 return -ENOMEM;
361         s->s_fs_info = qs;
362         memset(qs, 0, sizeof(struct qnx4_sb_info));
363
364         sb_set_blocksize(s, QNX4_BLOCK_SIZE);
365
366         /* Check the superblock signature. Since the qnx4 code is
367            dangerous, we should leave as quickly as possible
368            if we don't belong here... */
369         bh = sb_bread(s, 1);
370         if (!bh) {
371                 printk("qnx4: unable to read the superblock\n");
372                 goto outnobh;
373         }
374         if ( le32_to_cpu( *(__u32*)bh->b_data ) != QNX4_SUPER_MAGIC ) {
375                 if (!silent)
376                         printk("qnx4: wrong fsid in superblock.\n");
377                 goto out;
378         }
379         s->s_op = &qnx4_sops;
380         s->s_magic = QNX4_SUPER_MAGIC;
381 #ifndef CONFIG_QNX4FS_RW
382         s->s_flags |= MS_RDONLY;        /* Yup, read-only yet */
383 #endif
384         qnx4_sb(s)->sb_buf = bh;
385         qnx4_sb(s)->sb = (struct qnx4_super_block *) bh->b_data;
386
387
388         /* check before allocating dentries, inodes, .. */
389         errmsg = qnx4_checkroot(s);
390         if (errmsg != NULL) {
391                 if (!silent)
392                         printk("qnx4: %s\n", errmsg);
393                 goto out;
394         }
395
396         /* does root not have inode number QNX4_ROOT_INO ?? */
397         root = iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
398         if (!root) {
399                 printk("qnx4: get inode failed\n");
400                 goto out;
401         }
402
403         s->s_root = d_alloc_root(root);
404         if (s->s_root == NULL)
405                 goto outi;
406
407         brelse(bh);
408
409         return 0;
410
411       outi:
412         iput(root);
413       out:
414         brelse(bh);
415       outnobh:
416         kfree(qs);
417         s->s_fs_info = NULL;
418         return -EINVAL;
419 }
420
421 static void qnx4_put_super(struct super_block *sb)
422 {
423         struct qnx4_sb_info *qs = qnx4_sb(sb);
424         kfree( qs->BitMap );
425         kfree( qs );
426         sb->s_fs_info = NULL;
427         return;
428 }
429
430 static int qnx4_writepage(struct page *page, struct writeback_control *wbc)
431 {
432         return block_write_full_page(page,qnx4_get_block, wbc);
433 }
434 static int qnx4_readpage(struct file *file, struct page *page)
435 {
436         return block_read_full_page(page,qnx4_get_block);
437 }
438 static int qnx4_prepare_write(struct file *file, struct page *page,
439                               unsigned from, unsigned to)
440 {
441         struct qnx4_inode_info *qnx4_inode = qnx4_i(page->mapping->host);
442         return cont_prepare_write(page, from, to, qnx4_get_block,
443                                   &qnx4_inode->mmu_private);
444 }
445 static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
446 {
447         return generic_block_bmap(mapping,block,qnx4_get_block);
448 }
449 struct address_space_operations qnx4_aops = {
450         .readpage       = qnx4_readpage,
451         .writepage      = qnx4_writepage,
452         .sync_page      = block_sync_page,
453         .prepare_write  = qnx4_prepare_write,
454         .commit_write   = generic_commit_write,
455         .bmap           = qnx4_bmap
456 };
457
458 static void qnx4_read_inode(struct inode *inode)
459 {
460         struct buffer_head *bh;
461         struct qnx4_inode_entry *raw_inode;
462         int block, ino;
463         struct super_block *sb = inode->i_sb;
464         struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
465
466         ino = inode->i_ino;
467         inode->i_mode = 0;
468
469         QNX4DEBUG(("Reading inode : [%d]\n", ino));
470         if (!ino) {
471                 printk("qnx4: bad inode number on dev %s: %d is out of range\n",
472                        sb->s_id, ino);
473                 return;
474         }
475         block = ino / QNX4_INODES_PER_BLOCK;
476
477         if (!(bh = sb_bread(sb, block))) {
478                 printk("qnx4: major problem: unable to read inode from dev "
479                        "%s\n", sb->s_id);
480                 return;
481         }
482         raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
483             (ino % QNX4_INODES_PER_BLOCK);
484
485         inode->i_mode    = le16_to_cpu(raw_inode->di_mode);
486         inode->i_uid     = (uid_t)le16_to_cpu(raw_inode->di_uid);
487         inode->i_gid     = (gid_t)le16_to_cpu(raw_inode->di_gid);
488         inode->i_nlink   = le16_to_cpu(raw_inode->di_nlink);
489         inode->i_size    = le32_to_cpu(raw_inode->di_size);
490         inode->i_mtime.tv_sec   = le32_to_cpu(raw_inode->di_mtime);
491         inode->i_mtime.tv_nsec = 0;
492         inode->i_atime.tv_sec   = le32_to_cpu(raw_inode->di_atime);
493         inode->i_atime.tv_nsec = 0;
494         inode->i_ctime.tv_sec   = le32_to_cpu(raw_inode->di_ctime);
495         inode->i_ctime.tv_nsec = 0;
496         inode->i_blocks  = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
497         inode->i_blksize = QNX4_DIR_ENTRY_SIZE;
498
499         memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
500         if (S_ISREG(inode->i_mode)) {
501                 inode->i_op = &qnx4_file_inode_operations;
502                 inode->i_fop = &qnx4_file_operations;
503                 inode->i_mapping->a_ops = &qnx4_aops;
504                 qnx4_i(inode)->mmu_private = inode->i_size;
505         } else if (S_ISDIR(inode->i_mode)) {
506                 inode->i_op = &qnx4_dir_inode_operations;
507                 inode->i_fop = &qnx4_dir_operations;
508         } else if (S_ISLNK(inode->i_mode)) {
509                 inode->i_op = &page_symlink_inode_operations;
510                 inode->i_mapping->a_ops = &qnx4_aops;
511                 qnx4_i(inode)->mmu_private = inode->i_size;
512         } else
513                 printk("qnx4: bad inode %d on dev %s\n",ino,sb->s_id);
514         brelse(bh);
515 }
516
517 static kmem_cache_t *qnx4_inode_cachep;
518
519 static struct inode *qnx4_alloc_inode(struct super_block *sb)
520 {
521         struct qnx4_inode_info *ei;
522         ei = kmem_cache_alloc(qnx4_inode_cachep, SLAB_KERNEL);
523         if (!ei)
524                 return NULL;
525         return &ei->vfs_inode;
526 }
527
528 static void qnx4_destroy_inode(struct inode *inode)
529 {
530         kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
531 }
532
533 static void init_once(void *foo, kmem_cache_t * cachep,
534                       unsigned long flags)
535 {
536         struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
537
538         if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
539             SLAB_CTOR_CONSTRUCTOR)
540                 inode_init_once(&ei->vfs_inode);
541 }
542
543 static int init_inodecache(void)
544 {
545         qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
546                                              sizeof(struct qnx4_inode_info),
547                                              0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
548                                              init_once, NULL);
549         if (qnx4_inode_cachep == NULL)
550                 return -ENOMEM;
551         return 0;
552 }
553
554 static void destroy_inodecache(void)
555 {
556         if (kmem_cache_destroy(qnx4_inode_cachep))
557                 printk(KERN_INFO
558                        "qnx4_inode_cache: not all structures were freed\n");
559 }
560
561 static struct super_block *qnx4_get_sb(struct file_system_type *fs_type,
562         int flags, const char *dev_name, void *data)
563 {
564         return get_sb_bdev(fs_type, flags, dev_name, data, qnx4_fill_super);
565 }
566
567 static struct file_system_type qnx4_fs_type = {
568         .owner          = THIS_MODULE,
569         .name           = "qnx4",
570         .get_sb         = qnx4_get_sb,
571         .kill_sb        = kill_block_super,
572         .fs_flags       = FS_REQUIRES_DEV,
573 };
574
575 static int __init init_qnx4_fs(void)
576 {
577         int err;
578
579         err = init_inodecache();
580         if (err)
581                 return err;
582
583         err = register_filesystem(&qnx4_fs_type);
584         if (err) {
585                 destroy_inodecache();
586                 return err;
587         }
588
589         printk("QNX4 filesystem 0.2.3 registered.\n");
590         return 0;
591 }
592
593 static void __exit exit_qnx4_fs(void)
594 {
595         unregister_filesystem(&qnx4_fs_type);
596         destroy_inodecache();
597 }
598
599 module_init(init_qnx4_fs)
600 module_exit(exit_qnx4_fs)
601 MODULE_LICENSE("GPL");
602