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