1 // SPDX-License-Identifier: GPL-2.0-only
3 * QNX6 file system, Linux implementation.
10 * 16-02-2012 pagemap extension by Al Viro
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/highuid.h>
18 #include <linux/pagemap.h>
19 #include <linux/buffer_head.h>
20 #include <linux/writeback.h>
21 #include <linux/statfs.h>
22 #include <linux/parser.h>
23 #include <linux/seq_file.h>
24 #include <linux/mount.h>
25 #include <linux/crc32.h>
26 #include <linux/mpage.h>
29 static const struct super_operations qnx6_sops;
31 static void qnx6_put_super(struct super_block *sb);
32 static struct inode *qnx6_alloc_inode(struct super_block *sb);
33 static void qnx6_free_inode(struct inode *inode);
34 static int qnx6_remount(struct super_block *sb, int *flags, char *data);
35 static int qnx6_statfs(struct dentry *dentry, struct kstatfs *buf);
36 static int qnx6_show_options(struct seq_file *seq, struct dentry *root);
38 static const struct super_operations qnx6_sops = {
39 .alloc_inode = qnx6_alloc_inode,
40 .free_inode = qnx6_free_inode,
41 .put_super = qnx6_put_super,
42 .statfs = qnx6_statfs,
43 .remount_fs = qnx6_remount,
44 .show_options = qnx6_show_options,
47 static int qnx6_show_options(struct seq_file *seq, struct dentry *root)
49 struct super_block *sb = root->d_sb;
50 struct qnx6_sb_info *sbi = QNX6_SB(sb);
52 if (sbi->s_mount_opt & QNX6_MOUNT_MMI_FS)
53 seq_puts(seq, ",mmi_fs");
57 static int qnx6_remount(struct super_block *sb, int *flags, char *data)
64 static unsigned qnx6_get_devblock(struct super_block *sb, __fs32 block)
66 struct qnx6_sb_info *sbi = QNX6_SB(sb);
67 return fs32_to_cpu(sbi, block) + sbi->s_blks_off;
70 static unsigned qnx6_block_map(struct inode *inode, unsigned iblock);
72 static int qnx6_get_block(struct inode *inode, sector_t iblock,
73 struct buffer_head *bh, int create)
77 pr_debug("qnx6_get_block inode=[%ld] iblock=[%ld]\n",
78 inode->i_ino, (unsigned long)iblock);
80 phys = qnx6_block_map(inode, iblock);
82 /* logical block is before EOF */
83 map_bh(bh, inode->i_sb, phys);
88 static int qnx6_check_blockptr(__fs32 ptr)
90 if (ptr == ~(__fs32)0) {
91 pr_err("hit unused blockpointer.\n");
97 static int qnx6_readpage(struct file *file, struct page *page)
99 return mpage_readpage(page, qnx6_get_block);
102 static int qnx6_readpages(struct file *file, struct address_space *mapping,
103 struct list_head *pages, unsigned nr_pages)
105 return mpage_readpages(mapping, pages, nr_pages, qnx6_get_block);
109 * returns the block number for the no-th element in the tree
110 * inodebits requred as there are multiple inodes in one inode block
112 static unsigned qnx6_block_map(struct inode *inode, unsigned no)
114 struct super_block *s = inode->i_sb;
115 struct qnx6_sb_info *sbi = QNX6_SB(s);
116 struct qnx6_inode_info *ei = QNX6_I(inode);
118 struct buffer_head *bh;
121 int ptrbits = sbi->s_ptrbits;
123 u32 mask = (1 << ptrbits) - 1;
124 int depth = ei->di_filelevels;
127 bitdelta = ptrbits * depth;
128 levelptr = no >> bitdelta;
130 if (levelptr > QNX6_NO_DIRECT_POINTERS - 1) {
131 pr_err("Requested file block number (%u) too big.", no);
135 block = qnx6_get_devblock(s, ei->di_block_ptr[levelptr]);
137 for (i = 0; i < depth; i++) {
138 bh = sb_bread(s, block);
140 pr_err("Error reading block (%u)\n", block);
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 = d_inode(s->s_root);
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 pr_debug("magic: %08x\n", fs32_to_cpu(sbi, sb->sb_magic));
210 pr_debug("checksum: %08x\n", fs32_to_cpu(sbi, sb->sb_checksum));
211 pr_debug("serial: %llx\n", fs64_to_cpu(sbi, sb->sb_serial));
212 pr_debug("flags: %08x\n", fs32_to_cpu(sbi, sb->sb_flags));
213 pr_debug("blocksize: %08x\n", fs32_to_cpu(sbi, sb->sb_blocksize));
214 pr_debug("num_inodes: %08x\n", fs32_to_cpu(sbi, sb->sb_num_inodes));
215 pr_debug("free_inodes: %08x\n", fs32_to_cpu(sbi, sb->sb_free_inodes));
216 pr_debug("num_blocks: %08x\n", fs32_to_cpu(sbi, sb->sb_num_blocks));
217 pr_debug("free_blocks: %08x\n", fs32_to_cpu(sbi, sb->sb_free_blocks));
218 pr_debug("inode_levels: %02x\n", sb->Inode.levels);
227 static const match_table_t tokens = {
228 {Opt_mmifs, "mmi_fs"},
232 static int qnx6_parse_options(char *options, struct super_block *sb)
235 struct qnx6_sb_info *sbi = QNX6_SB(sb);
236 substring_t args[MAX_OPT_ARGS];
241 while ((p = strsep(&options, ",")) != NULL) {
246 token = match_token(p, tokens, args);
249 set_opt(sbi->s_mount_opt, MMI_FS);
258 static struct buffer_head *qnx6_check_first_superblock(struct super_block *s,
259 int offset, int silent)
261 struct qnx6_sb_info *sbi = QNX6_SB(s);
262 struct buffer_head *bh;
263 struct qnx6_super_block *sb;
265 /* Check the superblock signatures
266 start with the first superblock */
267 bh = sb_bread(s, offset);
269 pr_err("unable to read the first superblock\n");
272 sb = (struct qnx6_super_block *)bh->b_data;
273 if (fs32_to_cpu(sbi, sb->sb_magic) != QNX6_SUPER_MAGIC) {
274 sbi->s_bytesex = BYTESEX_BE;
275 if (fs32_to_cpu(sbi, sb->sb_magic) == QNX6_SUPER_MAGIC) {
276 /* we got a big endian fs */
277 pr_debug("fs got different endianness.\n");
280 sbi->s_bytesex = BYTESEX_LE;
283 pr_err("wrong signature (magic) in superblock #1.\n");
285 pr_info("wrong signature (magic) at position (0x%lx) - will try alternative position (0x0000).\n",
286 offset * s->s_blocksize);
295 static struct inode *qnx6_private_inode(struct super_block *s,
296 struct qnx6_root_node *p);
298 static int qnx6_fill_super(struct super_block *s, void *data, int silent)
300 struct buffer_head *bh1 = NULL, *bh2 = NULL;
301 struct qnx6_super_block *sb1 = NULL, *sb2 = NULL;
302 struct qnx6_sb_info *sbi;
305 struct qnx6_sb_info *qs;
308 int bootblock_offset = QNX6_BOOTBLOCK_SIZE;
310 qs = kzalloc(sizeof(struct qnx6_sb_info), GFP_KERNEL);
315 /* Superblock always is 512 Byte long */
316 if (!sb_set_blocksize(s, QNX6_SUPERBLOCK_SIZE)) {
317 pr_err("unable to set blocksize\n");
321 /* parse the mount-options */
322 if (!qnx6_parse_options((char *) data, s)) {
323 pr_err("invalid mount options.\n");
326 if (test_opt(s, MMI_FS)) {
327 sb1 = qnx6_mmi_fill_super(s, silent);
334 sbi->s_bytesex = BYTESEX_LE;
335 /* Check the superblock signatures
336 start with the first superblock */
337 bh1 = qnx6_check_first_superblock(s,
338 bootblock_offset / QNX6_SUPERBLOCK_SIZE, silent);
340 /* try again without bootblock offset */
341 bh1 = qnx6_check_first_superblock(s, 0, silent);
343 pr_err("unable to read the first superblock\n");
346 /* seems that no bootblock at partition start */
347 bootblock_offset = 0;
349 sb1 = (struct qnx6_super_block *)bh1->b_data;
351 #ifdef CONFIG_QNX6FS_DEBUG
352 qnx6_superblock_debug(sb1, s);
355 /* checksum check - start at byte 8 and end at byte 512 */
356 if (fs32_to_cpu(sbi, sb1->sb_checksum) !=
357 crc32_be(0, (char *)(bh1->b_data + 8), 504)) {
358 pr_err("superblock #1 checksum error\n");
362 /* set new blocksize */
363 if (!sb_set_blocksize(s, fs32_to_cpu(sbi, sb1->sb_blocksize))) {
364 pr_err("unable to set blocksize\n");
367 /* blocksize invalidates bh - pull it back in */
369 bh1 = sb_bread(s, bootblock_offset >> s->s_blocksize_bits);
372 sb1 = (struct qnx6_super_block *)bh1->b_data;
374 /* calculate second superblock blocknumber */
375 offset = fs32_to_cpu(sbi, sb1->sb_num_blocks) +
376 (bootblock_offset >> s->s_blocksize_bits) +
377 (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits);
379 /* set bootblock offset */
380 sbi->s_blks_off = (bootblock_offset >> s->s_blocksize_bits) +
381 (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits);
383 /* next the second superblock */
384 bh2 = sb_bread(s, offset);
386 pr_err("unable to read the second superblock\n");
389 sb2 = (struct qnx6_super_block *)bh2->b_data;
390 if (fs32_to_cpu(sbi, sb2->sb_magic) != QNX6_SUPER_MAGIC) {
392 pr_err("wrong signature (magic) in superblock #2.\n");
396 /* checksum check - start at byte 8 and end at byte 512 */
397 if (fs32_to_cpu(sbi, sb2->sb_checksum) !=
398 crc32_be(0, (char *)(bh2->b_data + 8), 504)) {
399 pr_err("superblock #2 checksum error\n");
403 if (fs64_to_cpu(sbi, sb1->sb_serial) >=
404 fs64_to_cpu(sbi, sb2->sb_serial)) {
405 /* superblock #1 active */
407 sbi->sb = (struct qnx6_super_block *)bh1->b_data;
409 pr_info("superblock #1 active\n");
411 /* superblock #2 active */
413 sbi->sb = (struct qnx6_super_block *)bh2->b_data;
415 pr_info("superblock #2 active\n");
418 /* sanity check - limit maximum indirect pointer levels */
419 if (sb1->Inode.levels > QNX6_PTR_MAX_LEVELS) {
420 pr_err("too many inode levels (max %i, sb %i)\n",
421 QNX6_PTR_MAX_LEVELS, sb1->Inode.levels);
424 if (sb1->Longfile.levels > QNX6_PTR_MAX_LEVELS) {
425 pr_err("too many longfilename levels (max %i, sb %i)\n",
426 QNX6_PTR_MAX_LEVELS, sb1->Longfile.levels);
429 s->s_op = &qnx6_sops;
430 s->s_magic = QNX6_SUPER_MAGIC;
431 s->s_flags |= SB_RDONLY; /* Yup, read-only yet */
433 s->s_time_max = U32_MAX;
435 /* ease the later tree level calculations */
437 sbi->s_ptrbits = ilog2(s->s_blocksize / 4);
438 sbi->inodes = qnx6_private_inode(s, &sb1->Inode);
441 sbi->longfile = qnx6_private_inode(s, &sb1->Longfile);
445 /* prefetch root inode */
446 root = qnx6_iget(s, QNX6_ROOT_INO);
448 pr_err("get inode failed\n");
454 s->s_root = d_make_root(root);
459 errmsg = qnx6_checkroot(s);
460 if (errmsg != NULL) {
462 pr_err("%s\n", errmsg);
485 static void qnx6_put_super(struct super_block *sb)
487 struct qnx6_sb_info *qs = QNX6_SB(sb);
492 sb->s_fs_info = NULL;
496 static sector_t qnx6_bmap(struct address_space *mapping, sector_t block)
498 return generic_block_bmap(mapping, block, qnx6_get_block);
500 static const struct address_space_operations qnx6_aops = {
501 .readpage = qnx6_readpage,
502 .readpages = qnx6_readpages,
506 static struct inode *qnx6_private_inode(struct super_block *s,
507 struct qnx6_root_node *p)
509 struct inode *inode = new_inode(s);
511 struct qnx6_inode_info *ei = QNX6_I(inode);
512 struct qnx6_sb_info *sbi = QNX6_SB(s);
513 inode->i_size = fs64_to_cpu(sbi, p->size);
514 memcpy(ei->di_block_ptr, p->ptr, sizeof(p->ptr));
515 ei->di_filelevels = p->levels;
516 inode->i_mode = S_IFREG | S_IRUSR; /* probably wrong */
517 inode->i_mapping->a_ops = &qnx6_aops;
522 struct inode *qnx6_iget(struct super_block *sb, unsigned ino)
524 struct qnx6_sb_info *sbi = QNX6_SB(sb);
525 struct qnx6_inode_entry *raw_inode;
527 struct qnx6_inode_info *ei;
528 struct address_space *mapping;
532 inode = iget_locked(sb, ino);
534 return ERR_PTR(-ENOMEM);
535 if (!(inode->i_state & I_NEW))
543 pr_err("bad inode number on dev %s: %u is out of range\n",
546 return ERR_PTR(-EIO);
548 n = (ino - 1) >> (PAGE_SHIFT - QNX6_INODE_SIZE_BITS);
549 offs = (ino - 1) & (~PAGE_MASK >> QNX6_INODE_SIZE_BITS);
550 mapping = sbi->inodes->i_mapping;
551 page = read_mapping_page(mapping, n, NULL);
553 pr_err("major problem: unable to read inode from dev %s\n",
556 return ERR_CAST(page);
559 raw_inode = ((struct qnx6_inode_entry *)page_address(page)) + offs;
561 inode->i_mode = fs16_to_cpu(sbi, raw_inode->di_mode);
562 i_uid_write(inode, (uid_t)fs32_to_cpu(sbi, raw_inode->di_uid));
563 i_gid_write(inode, (gid_t)fs32_to_cpu(sbi, raw_inode->di_gid));
564 inode->i_size = fs64_to_cpu(sbi, raw_inode->di_size);
565 inode->i_mtime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_mtime);
566 inode->i_mtime.tv_nsec = 0;
567 inode->i_atime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_atime);
568 inode->i_atime.tv_nsec = 0;
569 inode->i_ctime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_ctime);
570 inode->i_ctime.tv_nsec = 0;
572 /* calc blocks based on 512 byte blocksize */
573 inode->i_blocks = (inode->i_size + 511) >> 9;
575 memcpy(&ei->di_block_ptr, &raw_inode->di_block_ptr,
576 sizeof(raw_inode->di_block_ptr));
577 ei->di_filelevels = raw_inode->di_filelevels;
579 if (S_ISREG(inode->i_mode)) {
580 inode->i_fop = &generic_ro_fops;
581 inode->i_mapping->a_ops = &qnx6_aops;
582 } else if (S_ISDIR(inode->i_mode)) {
583 inode->i_op = &qnx6_dir_inode_operations;
584 inode->i_fop = &qnx6_dir_operations;
585 inode->i_mapping->a_ops = &qnx6_aops;
586 } else if (S_ISLNK(inode->i_mode)) {
587 inode->i_op = &page_symlink_inode_operations;
588 inode_nohighmem(inode);
589 inode->i_mapping->a_ops = &qnx6_aops;
591 init_special_inode(inode, inode->i_mode, 0);
593 unlock_new_inode(inode);
597 static struct kmem_cache *qnx6_inode_cachep;
599 static struct inode *qnx6_alloc_inode(struct super_block *sb)
601 struct qnx6_inode_info *ei;
602 ei = kmem_cache_alloc(qnx6_inode_cachep, GFP_KERNEL);
605 return &ei->vfs_inode;
608 static void qnx6_free_inode(struct inode *inode)
610 kmem_cache_free(qnx6_inode_cachep, QNX6_I(inode));
613 static void init_once(void *foo)
615 struct qnx6_inode_info *ei = (struct qnx6_inode_info *) foo;
617 inode_init_once(&ei->vfs_inode);
620 static int init_inodecache(void)
622 qnx6_inode_cachep = kmem_cache_create("qnx6_inode_cache",
623 sizeof(struct qnx6_inode_info),
624 0, (SLAB_RECLAIM_ACCOUNT|
625 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
627 if (!qnx6_inode_cachep)
632 static void destroy_inodecache(void)
635 * Make sure all delayed rcu free inodes are flushed before we
639 kmem_cache_destroy(qnx6_inode_cachep);
642 static struct dentry *qnx6_mount(struct file_system_type *fs_type,
643 int flags, const char *dev_name, void *data)
645 return mount_bdev(fs_type, flags, dev_name, data, qnx6_fill_super);
648 static struct file_system_type qnx6_fs_type = {
649 .owner = THIS_MODULE,
652 .kill_sb = kill_block_super,
653 .fs_flags = FS_REQUIRES_DEV,
655 MODULE_ALIAS_FS("qnx6");
657 static int __init init_qnx6_fs(void)
661 err = init_inodecache();
665 err = register_filesystem(&qnx6_fs_type);
667 destroy_inodecache();
671 pr_info("QNX6 filesystem 1.0.0 registered.\n");
675 static void __exit exit_qnx6_fs(void)
677 unregister_filesystem(&qnx6_fs_type);
678 destroy_inodecache();
681 module_init(init_qnx6_fs)
682 module_exit(exit_qnx6_fs)
683 MODULE_LICENSE("GPL");