2 * QNX6 file system, Linux implementation.
8 * 01-02-2012 by Kai Bankett (chaosman@ontika.net) : first release.
9 * 16-02-2012 pagemap extension by Al Viro
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/highuid.h>
17 #include <linux/pagemap.h>
18 #include <linux/buffer_head.h>
19 #include <linux/writeback.h>
20 #include <linux/statfs.h>
21 #include <linux/parser.h>
22 #include <linux/seq_file.h>
23 #include <linux/mount.h>
24 #include <linux/crc32.h>
25 #include <linux/mpage.h>
28 static const struct super_operations qnx6_sops
;
30 static void qnx6_put_super(struct super_block
*sb
);
31 static struct inode
*qnx6_alloc_inode(struct super_block
*sb
);
32 static void qnx6_destroy_inode(struct inode
*inode
);
33 static int qnx6_remount(struct super_block
*sb
, int *flags
, char *data
);
34 static int qnx6_statfs(struct dentry
*dentry
, struct kstatfs
*buf
);
35 static int qnx6_show_options(struct seq_file
*seq
, struct dentry
*root
);
37 static const struct super_operations qnx6_sops
= {
38 .alloc_inode
= qnx6_alloc_inode
,
39 .destroy_inode
= qnx6_destroy_inode
,
40 .put_super
= qnx6_put_super
,
41 .statfs
= qnx6_statfs
,
42 .remount_fs
= qnx6_remount
,
43 .show_options
= qnx6_show_options
,
46 static int qnx6_show_options(struct seq_file
*seq
, struct dentry
*root
)
48 struct super_block
*sb
= root
->d_sb
;
49 struct qnx6_sb_info
*sbi
= QNX6_SB(sb
);
51 if (sbi
->s_mount_opt
& QNX6_MOUNT_MMI_FS
)
52 seq_puts(seq
, ",mmi_fs");
56 static int qnx6_remount(struct super_block
*sb
, int *flags
, char *data
)
62 static unsigned qnx6_get_devblock(struct super_block
*sb
, __fs32 block
)
64 struct qnx6_sb_info
*sbi
= QNX6_SB(sb
);
65 return fs32_to_cpu(sbi
, block
) + sbi
->s_blks_off
;
68 static unsigned qnx6_block_map(struct inode
*inode
, unsigned iblock
);
70 static int qnx6_get_block(struct inode
*inode
, sector_t iblock
,
71 struct buffer_head
*bh
, int create
)
75 QNX6DEBUG((KERN_INFO
"qnx6: qnx6_get_block inode=[%ld] iblock=[%ld]\n",
76 inode
->i_ino
, (unsigned long)iblock
));
78 phys
= qnx6_block_map(inode
, iblock
);
80 /* logical block is before EOF */
81 map_bh(bh
, inode
->i_sb
, phys
);
86 static int qnx6_check_blockptr(__fs32 ptr
)
88 if (ptr
== ~(__fs32
)0) {
89 printk(KERN_ERR
"qnx6: hit unused blockpointer.\n");
95 static int qnx6_readpage(struct file
*file
, struct page
*page
)
97 return mpage_readpage(page
, qnx6_get_block
);
100 static int qnx6_readpages(struct file
*file
, struct address_space
*mapping
,
101 struct list_head
*pages
, unsigned nr_pages
)
103 return mpage_readpages(mapping
, pages
, nr_pages
, qnx6_get_block
);
107 * returns the block number for the no-th element in the tree
108 * inodebits requred as there are multiple inodes in one inode block
110 static unsigned qnx6_block_map(struct inode
*inode
, unsigned no
)
112 struct super_block
*s
= inode
->i_sb
;
113 struct qnx6_sb_info
*sbi
= QNX6_SB(s
);
114 struct qnx6_inode_info
*ei
= QNX6_I(inode
);
116 struct buffer_head
*bh
;
119 int ptrbits
= sbi
->s_ptrbits
;
121 u32 mask
= (1 << ptrbits
) - 1;
122 int depth
= ei
->di_filelevels
;
125 bitdelta
= ptrbits
* depth
;
126 levelptr
= no
>> bitdelta
;
128 if (levelptr
> QNX6_NO_DIRECT_POINTERS
- 1) {
129 printk(KERN_ERR
"qnx6:Requested file block number (%u) too big.",
134 block
= qnx6_get_devblock(s
, ei
->di_block_ptr
[levelptr
]);
136 for (i
= 0; i
< depth
; i
++) {
137 bh
= sb_bread(s
, block
);
139 printk(KERN_ERR
"qnx6:Error reading block (%u)\n",
144 levelptr
= (no
>> bitdelta
) & mask
;
145 ptr
= ((__fs32
*)bh
->b_data
)[levelptr
];
147 if (!qnx6_check_blockptr(ptr
))
150 block
= qnx6_get_devblock(s
, ptr
);
156 static int qnx6_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
158 struct super_block
*sb
= dentry
->d_sb
;
159 struct qnx6_sb_info
*sbi
= QNX6_SB(sb
);
160 u64 id
= huge_encode_dev(sb
->s_bdev
->bd_dev
);
162 buf
->f_type
= sb
->s_magic
;
163 buf
->f_bsize
= sb
->s_blocksize
;
164 buf
->f_blocks
= fs32_to_cpu(sbi
, sbi
->sb
->sb_num_blocks
);
165 buf
->f_bfree
= fs32_to_cpu(sbi
, sbi
->sb
->sb_free_blocks
);
166 buf
->f_files
= fs32_to_cpu(sbi
, sbi
->sb
->sb_num_inodes
);
167 buf
->f_ffree
= fs32_to_cpu(sbi
, sbi
->sb
->sb_free_inodes
);
168 buf
->f_bavail
= buf
->f_bfree
;
169 buf
->f_namelen
= QNX6_LONG_NAME_MAX
;
170 buf
->f_fsid
.val
[0] = (u32
)id
;
171 buf
->f_fsid
.val
[1] = (u32
)(id
>> 32);
177 * Check the root directory of the filesystem to make sure
178 * it really _is_ a qnx6 filesystem, and to check the size
179 * of the directory entry.
181 static const char *qnx6_checkroot(struct super_block
*s
)
183 static char match_root
[2][3] = {".\0\0", "..\0"};
185 struct qnx6_dir_entry
*dir_entry
;
186 struct inode
*root
= s
->s_root
->d_inode
;
187 struct address_space
*mapping
= root
->i_mapping
;
188 struct page
*page
= read_mapping_page(mapping
, 0, NULL
);
190 return "error reading root directory";
192 dir_entry
= page_address(page
);
193 for (i
= 0; i
< 2; i
++) {
194 /* maximum 3 bytes - due to match_root limitation */
195 if (strncmp(dir_entry
[i
].de_fname
, match_root
[i
], 3))
200 return "error reading root directory.";
204 #ifdef CONFIG_QNX6FS_DEBUG
205 void qnx6_superblock_debug(struct qnx6_super_block
*sb
, struct super_block
*s
)
207 struct qnx6_sb_info
*sbi
= QNX6_SB(s
);
209 QNX6DEBUG((KERN_INFO
"magic: %08x\n",
210 fs32_to_cpu(sbi
, sb
->sb_magic
)));
211 QNX6DEBUG((KERN_INFO
"checksum: %08x\n",
212 fs32_to_cpu(sbi
, sb
->sb_checksum
)));
213 QNX6DEBUG((KERN_INFO
"serial: %llx\n",
214 fs64_to_cpu(sbi
, sb
->sb_serial
)));
215 QNX6DEBUG((KERN_INFO
"flags: %08x\n",
216 fs32_to_cpu(sbi
, sb
->sb_flags
)));
217 QNX6DEBUG((KERN_INFO
"blocksize: %08x\n",
218 fs32_to_cpu(sbi
, sb
->sb_blocksize
)));
219 QNX6DEBUG((KERN_INFO
"num_inodes: %08x\n",
220 fs32_to_cpu(sbi
, sb
->sb_num_inodes
)));
221 QNX6DEBUG((KERN_INFO
"free_inodes: %08x\n",
222 fs32_to_cpu(sbi
, sb
->sb_free_inodes
)));
223 QNX6DEBUG((KERN_INFO
"num_blocks: %08x\n",
224 fs32_to_cpu(sbi
, sb
->sb_num_blocks
)));
225 QNX6DEBUG((KERN_INFO
"free_blocks: %08x\n",
226 fs32_to_cpu(sbi
, sb
->sb_free_blocks
)));
227 QNX6DEBUG((KERN_INFO
"inode_levels: %02x\n",
237 static const match_table_t tokens
= {
238 {Opt_mmifs
, "mmi_fs"},
242 static int qnx6_parse_options(char *options
, struct super_block
*sb
)
245 struct qnx6_sb_info
*sbi
= QNX6_SB(sb
);
246 substring_t args
[MAX_OPT_ARGS
];
251 while ((p
= strsep(&options
, ",")) != NULL
) {
256 token
= match_token(p
, tokens
, args
);
259 set_opt(sbi
->s_mount_opt
, MMI_FS
);
268 static struct buffer_head
*qnx6_check_first_superblock(struct super_block
*s
,
269 int offset
, int silent
)
271 struct qnx6_sb_info
*sbi
= QNX6_SB(s
);
272 struct buffer_head
*bh
;
273 struct qnx6_super_block
*sb
;
275 /* Check the superblock signatures
276 start with the first superblock */
277 bh
= sb_bread(s
, offset
);
279 printk(KERN_ERR
"qnx6: unable to read the first superblock\n");
282 sb
= (struct qnx6_super_block
*)bh
->b_data
;
283 if (fs32_to_cpu(sbi
, sb
->sb_magic
) != QNX6_SUPER_MAGIC
) {
284 sbi
->s_bytesex
= BYTESEX_BE
;
285 if (fs32_to_cpu(sbi
, sb
->sb_magic
) == QNX6_SUPER_MAGIC
) {
286 /* we got a big endian fs */
287 QNX6DEBUG((KERN_INFO
"qnx6: fs got different"
291 sbi
->s_bytesex
= BYTESEX_LE
;
294 printk(KERN_ERR
"qnx6: wrong signature (magic)"
295 " in superblock #1.\n");
297 printk(KERN_INFO
"qnx6: wrong signature (magic)"
298 " at position (0x%lx) - will try"
299 " alternative position (0x0000).\n",
300 offset
* s
->s_blocksize
);
309 static struct inode
*qnx6_private_inode(struct super_block
*s
,
310 struct qnx6_root_node
*p
);
312 static int qnx6_fill_super(struct super_block
*s
, void *data
, int silent
)
314 struct buffer_head
*bh1
= NULL
, *bh2
= NULL
;
315 struct qnx6_super_block
*sb1
= NULL
, *sb2
= NULL
;
316 struct qnx6_sb_info
*sbi
;
319 struct qnx6_sb_info
*qs
;
322 int bootblock_offset
= QNX6_BOOTBLOCK_SIZE
;
324 qs
= kzalloc(sizeof(struct qnx6_sb_info
), GFP_KERNEL
);
329 /* Superblock always is 512 Byte long */
330 if (!sb_set_blocksize(s
, QNX6_SUPERBLOCK_SIZE
)) {
331 printk(KERN_ERR
"qnx6: unable to set blocksize\n");
335 /* parse the mount-options */
336 if (!qnx6_parse_options((char *) data
, s
)) {
337 printk(KERN_ERR
"qnx6: invalid mount options.\n");
340 if (test_opt(s
, MMI_FS
)) {
341 sb1
= qnx6_mmi_fill_super(s
, silent
);
348 sbi
->s_bytesex
= BYTESEX_LE
;
349 /* Check the superblock signatures
350 start with the first superblock */
351 bh1
= qnx6_check_first_superblock(s
,
352 bootblock_offset
/ QNX6_SUPERBLOCK_SIZE
, silent
);
354 /* try again without bootblock offset */
355 bh1
= qnx6_check_first_superblock(s
, 0, silent
);
357 printk(KERN_ERR
"qnx6: unable to read the first superblock\n");
360 /* seems that no bootblock at partition start */
361 bootblock_offset
= 0;
363 sb1
= (struct qnx6_super_block
*)bh1
->b_data
;
365 #ifdef CONFIG_QNX6FS_DEBUG
366 qnx6_superblock_debug(sb1
, s
);
369 /* checksum check - start at byte 8 and end at byte 512 */
370 if (fs32_to_cpu(sbi
, sb1
->sb_checksum
) !=
371 crc32_be(0, (char *)(bh1
->b_data
+ 8), 504)) {
372 printk(KERN_ERR
"qnx6: superblock #1 checksum error\n");
376 /* set new blocksize */
377 if (!sb_set_blocksize(s
, fs32_to_cpu(sbi
, sb1
->sb_blocksize
))) {
378 printk(KERN_ERR
"qnx6: unable to set blocksize\n");
381 /* blocksize invalidates bh - pull it back in */
383 bh1
= sb_bread(s
, bootblock_offset
>> s
->s_blocksize_bits
);
386 sb1
= (struct qnx6_super_block
*)bh1
->b_data
;
388 /* calculate second superblock blocknumber */
389 offset
= fs32_to_cpu(sbi
, sb1
->sb_num_blocks
) +
390 (bootblock_offset
>> s
->s_blocksize_bits
) +
391 (QNX6_SUPERBLOCK_AREA
>> s
->s_blocksize_bits
);
393 /* set bootblock offset */
394 sbi
->s_blks_off
= (bootblock_offset
>> s
->s_blocksize_bits
) +
395 (QNX6_SUPERBLOCK_AREA
>> s
->s_blocksize_bits
);
397 /* next the second superblock */
398 bh2
= sb_bread(s
, offset
);
400 printk(KERN_ERR
"qnx6: unable to read the second superblock\n");
403 sb2
= (struct qnx6_super_block
*)bh2
->b_data
;
404 if (fs32_to_cpu(sbi
, sb2
->sb_magic
) != QNX6_SUPER_MAGIC
) {
406 printk(KERN_ERR
"qnx6: wrong signature (magic)"
407 " in superblock #2.\n");
411 /* checksum check - start at byte 8 and end at byte 512 */
412 if (fs32_to_cpu(sbi
, sb2
->sb_checksum
) !=
413 crc32_be(0, (char *)(bh2
->b_data
+ 8), 504)) {
414 printk(KERN_ERR
"qnx6: superblock #2 checksum error\n");
418 if (fs64_to_cpu(sbi
, sb1
->sb_serial
) >=
419 fs64_to_cpu(sbi
, sb2
->sb_serial
)) {
420 /* superblock #1 active */
422 sbi
->sb
= (struct qnx6_super_block
*)bh1
->b_data
;
424 printk(KERN_INFO
"qnx6: superblock #1 active\n");
426 /* superblock #2 active */
428 sbi
->sb
= (struct qnx6_super_block
*)bh2
->b_data
;
430 printk(KERN_INFO
"qnx6: superblock #2 active\n");
433 /* sanity check - limit maximum indirect pointer levels */
434 if (sb1
->Inode
.levels
> QNX6_PTR_MAX_LEVELS
) {
435 printk(KERN_ERR
"qnx6: too many inode levels (max %i, sb %i)\n",
436 QNX6_PTR_MAX_LEVELS
, sb1
->Inode
.levels
);
439 if (sb1
->Longfile
.levels
> QNX6_PTR_MAX_LEVELS
) {
440 printk(KERN_ERR
"qnx6: too many longfilename levels"
441 " (max %i, sb %i)\n",
442 QNX6_PTR_MAX_LEVELS
, sb1
->Longfile
.levels
);
445 s
->s_op
= &qnx6_sops
;
446 s
->s_magic
= QNX6_SUPER_MAGIC
;
447 s
->s_flags
|= MS_RDONLY
; /* Yup, read-only yet */
449 /* ease the later tree level calculations */
451 sbi
->s_ptrbits
= ilog2(s
->s_blocksize
/ 4);
452 sbi
->inodes
= qnx6_private_inode(s
, &sb1
->Inode
);
455 sbi
->longfile
= qnx6_private_inode(s
, &sb1
->Longfile
);
459 /* prefetch root inode */
460 root
= qnx6_iget(s
, QNX6_ROOT_INO
);
462 printk(KERN_ERR
"qnx6: get inode failed\n");
468 s
->s_root
= d_make_root(root
);
473 errmsg
= qnx6_checkroot(s
);
474 if (errmsg
!= NULL
) {
476 printk(KERN_ERR
"qnx6: %s\n", errmsg
);
499 static void qnx6_put_super(struct super_block
*sb
)
501 struct qnx6_sb_info
*qs
= QNX6_SB(sb
);
506 sb
->s_fs_info
= NULL
;
510 static sector_t
qnx6_bmap(struct address_space
*mapping
, sector_t block
)
512 return generic_block_bmap(mapping
, block
, qnx6_get_block
);
514 static const struct address_space_operations qnx6_aops
= {
515 .readpage
= qnx6_readpage
,
516 .readpages
= qnx6_readpages
,
520 static struct inode
*qnx6_private_inode(struct super_block
*s
,
521 struct qnx6_root_node
*p
)
523 struct inode
*inode
= new_inode(s
);
525 struct qnx6_inode_info
*ei
= QNX6_I(inode
);
526 struct qnx6_sb_info
*sbi
= QNX6_SB(s
);
527 inode
->i_size
= fs64_to_cpu(sbi
, p
->size
);
528 memcpy(ei
->di_block_ptr
, p
->ptr
, sizeof(p
->ptr
));
529 ei
->di_filelevels
= p
->levels
;
530 inode
->i_mode
= S_IFREG
| S_IRUSR
; /* probably wrong */
531 inode
->i_mapping
->a_ops
= &qnx6_aops
;
536 struct inode
*qnx6_iget(struct super_block
*sb
, unsigned ino
)
538 struct qnx6_sb_info
*sbi
= QNX6_SB(sb
);
539 struct qnx6_inode_entry
*raw_inode
;
541 struct qnx6_inode_info
*ei
;
542 struct address_space
*mapping
;
546 inode
= iget_locked(sb
, ino
);
548 return ERR_PTR(-ENOMEM
);
549 if (!(inode
->i_state
& I_NEW
))
557 printk(KERN_ERR
"qnx6: bad inode number on dev %s: %u is "
561 return ERR_PTR(-EIO
);
563 n
= (ino
- 1) >> (PAGE_CACHE_SHIFT
- QNX6_INODE_SIZE_BITS
);
564 offs
= (ino
- 1) & (~PAGE_CACHE_MASK
>> QNX6_INODE_SIZE_BITS
);
565 mapping
= sbi
->inodes
->i_mapping
;
566 page
= read_mapping_page(mapping
, n
, NULL
);
568 printk(KERN_ERR
"qnx6: major problem: unable to read inode from "
569 "dev %s\n", sb
->s_id
);
571 return ERR_CAST(page
);
574 raw_inode
= ((struct qnx6_inode_entry
*)page_address(page
)) + offs
;
576 inode
->i_mode
= fs16_to_cpu(sbi
, raw_inode
->di_mode
);
577 i_uid_write(inode
, (uid_t
)fs32_to_cpu(sbi
, raw_inode
->di_uid
));
578 i_gid_write(inode
, (gid_t
)fs32_to_cpu(sbi
, raw_inode
->di_gid
));
579 inode
->i_size
= fs64_to_cpu(sbi
, raw_inode
->di_size
);
580 inode
->i_mtime
.tv_sec
= fs32_to_cpu(sbi
, raw_inode
->di_mtime
);
581 inode
->i_mtime
.tv_nsec
= 0;
582 inode
->i_atime
.tv_sec
= fs32_to_cpu(sbi
, raw_inode
->di_atime
);
583 inode
->i_atime
.tv_nsec
= 0;
584 inode
->i_ctime
.tv_sec
= fs32_to_cpu(sbi
, raw_inode
->di_ctime
);
585 inode
->i_ctime
.tv_nsec
= 0;
587 /* calc blocks based on 512 byte blocksize */
588 inode
->i_blocks
= (inode
->i_size
+ 511) >> 9;
590 memcpy(&ei
->di_block_ptr
, &raw_inode
->di_block_ptr
,
591 sizeof(raw_inode
->di_block_ptr
));
592 ei
->di_filelevels
= raw_inode
->di_filelevels
;
594 if (S_ISREG(inode
->i_mode
)) {
595 inode
->i_fop
= &generic_ro_fops
;
596 inode
->i_mapping
->a_ops
= &qnx6_aops
;
597 } else if (S_ISDIR(inode
->i_mode
)) {
598 inode
->i_op
= &qnx6_dir_inode_operations
;
599 inode
->i_fop
= &qnx6_dir_operations
;
600 inode
->i_mapping
->a_ops
= &qnx6_aops
;
601 } else if (S_ISLNK(inode
->i_mode
)) {
602 inode
->i_op
= &page_symlink_inode_operations
;
603 inode
->i_mapping
->a_ops
= &qnx6_aops
;
605 init_special_inode(inode
, inode
->i_mode
, 0);
607 unlock_new_inode(inode
);
611 static struct kmem_cache
*qnx6_inode_cachep
;
613 static struct inode
*qnx6_alloc_inode(struct super_block
*sb
)
615 struct qnx6_inode_info
*ei
;
616 ei
= kmem_cache_alloc(qnx6_inode_cachep
, GFP_KERNEL
);
619 return &ei
->vfs_inode
;
622 static void qnx6_i_callback(struct rcu_head
*head
)
624 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
625 kmem_cache_free(qnx6_inode_cachep
, QNX6_I(inode
));
628 static void qnx6_destroy_inode(struct inode
*inode
)
630 call_rcu(&inode
->i_rcu
, qnx6_i_callback
);
633 static void init_once(void *foo
)
635 struct qnx6_inode_info
*ei
= (struct qnx6_inode_info
*) foo
;
637 inode_init_once(&ei
->vfs_inode
);
640 static int init_inodecache(void)
642 qnx6_inode_cachep
= kmem_cache_create("qnx6_inode_cache",
643 sizeof(struct qnx6_inode_info
),
644 0, (SLAB_RECLAIM_ACCOUNT
|
647 if (!qnx6_inode_cachep
)
652 static void destroy_inodecache(void)
655 * Make sure all delayed rcu free inodes are flushed before we
659 kmem_cache_destroy(qnx6_inode_cachep
);
662 static struct dentry
*qnx6_mount(struct file_system_type
*fs_type
,
663 int flags
, const char *dev_name
, void *data
)
665 return mount_bdev(fs_type
, flags
, dev_name
, data
, qnx6_fill_super
);
668 static struct file_system_type qnx6_fs_type
= {
669 .owner
= THIS_MODULE
,
672 .kill_sb
= kill_block_super
,
673 .fs_flags
= FS_REQUIRES_DEV
,
676 static int __init
init_qnx6_fs(void)
680 err
= init_inodecache();
684 err
= register_filesystem(&qnx6_fs_type
);
686 destroy_inodecache();
690 printk(KERN_INFO
"QNX6 filesystem 1.0.0 registered.\n");
694 static void __exit
exit_qnx6_fs(void)
696 unregister_filesystem(&qnx6_fs_type
);
697 destroy_inodecache();
700 module_init(init_qnx6_fs
)
701 module_exit(exit_qnx6_fs
)
702 MODULE_LICENSE("GPL");