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/seq_file.h>
23 #include <linux/crc32.h>
24 #include <linux/mpage.h>
25 #include <linux/fs_parser.h>
26 #include <linux/fs_context.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_reconfigure(struct fs_context *fc);
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 .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_reconfigure(struct fs_context *fc)
58 struct super_block *sb = fc->root->d_sb;
61 fc->sb_flags |= SB_RDONLY;
65 static unsigned qnx6_get_devblock(struct super_block *sb, __fs32 block)
67 struct qnx6_sb_info *sbi = QNX6_SB(sb);
68 return fs32_to_cpu(sbi, block) + sbi->s_blks_off;
71 static unsigned qnx6_block_map(struct inode *inode, unsigned iblock);
73 static int qnx6_get_block(struct inode *inode, sector_t iblock,
74 struct buffer_head *bh, int create)
78 pr_debug("qnx6_get_block inode=[%ld] iblock=[%ld]\n",
79 inode->i_ino, (unsigned long)iblock);
81 phys = qnx6_block_map(inode, iblock);
83 /* logical block is before EOF */
84 map_bh(bh, inode->i_sb, phys);
89 static int qnx6_check_blockptr(__fs32 ptr)
91 if (ptr == ~(__fs32)0) {
92 pr_err("hit unused blockpointer.\n");
98 static int qnx6_read_folio(struct file *file, struct folio *folio)
100 return mpage_read_folio(folio, qnx6_get_block);
103 static void qnx6_readahead(struct readahead_control *rac)
105 mpage_readahead(rac, 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 = u64_to_fsid(id);
176 * Check the root directory of the filesystem to make sure
177 * it really _is_ a qnx6 filesystem, and to check the size
178 * of the directory entry.
180 static const char *qnx6_checkroot(struct super_block *s)
183 struct qnx6_dir_entry *dir_entry;
184 struct inode *root = d_inode(s->s_root);
185 struct address_space *mapping = root->i_mapping;
186 struct folio *folio = read_mapping_folio(mapping, 0, NULL);
189 return "error reading root directory";
190 dir_entry = kmap_local_folio(folio, 0);
191 if (memcmp(dir_entry[0].de_fname, ".", 2) ||
192 memcmp(dir_entry[1].de_fname, "..", 3))
194 folio_release_kmap(folio, dir_entry);
196 return "error reading root directory.";
200 #ifdef CONFIG_QNX6FS_DEBUG
201 void qnx6_superblock_debug(struct qnx6_super_block *sb, struct super_block *s)
203 struct qnx6_sb_info *sbi = QNX6_SB(s);
205 pr_debug("magic: %08x\n", fs32_to_cpu(sbi, sb->sb_magic));
206 pr_debug("checksum: %08x\n", fs32_to_cpu(sbi, sb->sb_checksum));
207 pr_debug("serial: %llx\n", fs64_to_cpu(sbi, sb->sb_serial));
208 pr_debug("flags: %08x\n", fs32_to_cpu(sbi, sb->sb_flags));
209 pr_debug("blocksize: %08x\n", fs32_to_cpu(sbi, sb->sb_blocksize));
210 pr_debug("num_inodes: %08x\n", fs32_to_cpu(sbi, sb->sb_num_inodes));
211 pr_debug("free_inodes: %08x\n", fs32_to_cpu(sbi, sb->sb_free_inodes));
212 pr_debug("num_blocks: %08x\n", fs32_to_cpu(sbi, sb->sb_num_blocks));
213 pr_debug("free_blocks: %08x\n", fs32_to_cpu(sbi, sb->sb_free_blocks));
214 pr_debug("inode_levels: %02x\n", sb->Inode.levels);
222 struct qnx6_context {
223 unsigned long s_mount_opts;
226 static const struct fs_parameter_spec qnx6_param_spec[] = {
227 fsparam_flag ("mmi_fs", Opt_mmifs),
231 static int qnx6_parse_param(struct fs_context *fc, struct fs_parameter *param)
233 struct qnx6_context *ctx = fc->fs_private;
234 struct fs_parse_result result;
237 opt = fs_parse(fc, qnx6_param_spec, param, &result);
243 ctx->s_mount_opts |= QNX6_MOUNT_MMI_FS;
251 static struct buffer_head *qnx6_check_first_superblock(struct super_block *s,
252 int offset, int silent)
254 struct qnx6_sb_info *sbi = QNX6_SB(s);
255 struct buffer_head *bh;
256 struct qnx6_super_block *sb;
258 /* Check the superblock signatures
259 start with the first superblock */
260 bh = sb_bread(s, offset);
262 pr_err("unable to read the first superblock\n");
265 sb = (struct qnx6_super_block *)bh->b_data;
266 if (fs32_to_cpu(sbi, sb->sb_magic) != QNX6_SUPER_MAGIC) {
267 sbi->s_bytesex = BYTESEX_BE;
268 if (fs32_to_cpu(sbi, sb->sb_magic) == QNX6_SUPER_MAGIC) {
269 /* we got a big endian fs */
270 pr_debug("fs got different endianness.\n");
273 sbi->s_bytesex = BYTESEX_LE;
276 pr_err("wrong signature (magic) in superblock #1.\n");
278 pr_info("wrong signature (magic) at position (0x%lx) - will try alternative position (0x0000).\n",
279 offset * s->s_blocksize);
288 static struct inode *qnx6_private_inode(struct super_block *s,
289 struct qnx6_root_node *p);
291 static int qnx6_fill_super(struct super_block *s, struct fs_context *fc)
293 struct buffer_head *bh1 = NULL, *bh2 = NULL;
294 struct qnx6_super_block *sb1 = NULL, *sb2 = NULL;
295 struct qnx6_sb_info *sbi;
296 struct qnx6_context *ctx = fc->fs_private;
299 struct qnx6_sb_info *qs;
302 int bootblock_offset = QNX6_BOOTBLOCK_SIZE;
303 int silent = fc->sb_flags & SB_SILENT;
305 qs = kzalloc(sizeof(struct qnx6_sb_info), GFP_KERNEL);
309 qs->s_mount_opt = ctx->s_mount_opts;
311 /* Superblock always is 512 Byte long */
312 if (!sb_set_blocksize(s, QNX6_SUPERBLOCK_SIZE)) {
313 pr_err("unable to set blocksize\n");
317 if (qs->s_mount_opt == QNX6_MOUNT_MMI_FS) {
318 sb1 = qnx6_mmi_fill_super(s, silent);
325 sbi->s_bytesex = BYTESEX_LE;
326 /* Check the superblock signatures
327 start with the first superblock */
328 bh1 = qnx6_check_first_superblock(s,
329 bootblock_offset / QNX6_SUPERBLOCK_SIZE, silent);
331 /* try again without bootblock offset */
332 bh1 = qnx6_check_first_superblock(s, 0, silent);
334 pr_err("unable to read the first superblock\n");
337 /* seems that no bootblock at partition start */
338 bootblock_offset = 0;
340 sb1 = (struct qnx6_super_block *)bh1->b_data;
342 #ifdef CONFIG_QNX6FS_DEBUG
343 qnx6_superblock_debug(sb1, s);
346 /* checksum check - start at byte 8 and end at byte 512 */
347 if (fs32_to_cpu(sbi, sb1->sb_checksum) !=
348 crc32_be(0, (char *)(bh1->b_data + 8), 504)) {
349 pr_err("superblock #1 checksum error\n");
353 /* set new blocksize */
354 if (!sb_set_blocksize(s, fs32_to_cpu(sbi, sb1->sb_blocksize))) {
355 pr_err("unable to set blocksize\n");
358 /* blocksize invalidates bh - pull it back in */
360 bh1 = sb_bread(s, bootblock_offset >> s->s_blocksize_bits);
363 sb1 = (struct qnx6_super_block *)bh1->b_data;
365 /* calculate second superblock blocknumber */
366 offset = fs32_to_cpu(sbi, sb1->sb_num_blocks) +
367 (bootblock_offset >> s->s_blocksize_bits) +
368 (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits);
370 /* set bootblock offset */
371 sbi->s_blks_off = (bootblock_offset >> s->s_blocksize_bits) +
372 (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits);
374 /* next the second superblock */
375 bh2 = sb_bread(s, offset);
377 pr_err("unable to read the second superblock\n");
380 sb2 = (struct qnx6_super_block *)bh2->b_data;
381 if (fs32_to_cpu(sbi, sb2->sb_magic) != QNX6_SUPER_MAGIC) {
383 pr_err("wrong signature (magic) in superblock #2.\n");
387 /* checksum check - start at byte 8 and end at byte 512 */
388 if (fs32_to_cpu(sbi, sb2->sb_checksum) !=
389 crc32_be(0, (char *)(bh2->b_data + 8), 504)) {
390 pr_err("superblock #2 checksum error\n");
394 if (fs64_to_cpu(sbi, sb1->sb_serial) >=
395 fs64_to_cpu(sbi, sb2->sb_serial)) {
396 /* superblock #1 active */
398 sbi->sb = (struct qnx6_super_block *)bh1->b_data;
400 pr_info("superblock #1 active\n");
402 /* superblock #2 active */
404 sbi->sb = (struct qnx6_super_block *)bh2->b_data;
406 pr_info("superblock #2 active\n");
409 /* sanity check - limit maximum indirect pointer levels */
410 if (sb1->Inode.levels > QNX6_PTR_MAX_LEVELS) {
411 pr_err("too many inode levels (max %i, sb %i)\n",
412 QNX6_PTR_MAX_LEVELS, sb1->Inode.levels);
415 if (sb1->Longfile.levels > QNX6_PTR_MAX_LEVELS) {
416 pr_err("too many longfilename levels (max %i, sb %i)\n",
417 QNX6_PTR_MAX_LEVELS, sb1->Longfile.levels);
420 s->s_op = &qnx6_sops;
421 s->s_magic = QNX6_SUPER_MAGIC;
422 s->s_flags |= SB_RDONLY; /* Yup, read-only yet */
424 s->s_time_max = U32_MAX;
426 /* ease the later tree level calculations */
428 sbi->s_ptrbits = ilog2(s->s_blocksize / 4);
429 sbi->inodes = qnx6_private_inode(s, &sb1->Inode);
432 sbi->longfile = qnx6_private_inode(s, &sb1->Longfile);
436 /* prefetch root inode */
437 root = qnx6_iget(s, QNX6_ROOT_INO);
439 pr_err("get inode failed\n");
445 s->s_root = d_make_root(root);
450 errmsg = qnx6_checkroot(s);
451 if (errmsg != NULL) {
453 pr_err("%s\n", errmsg);
474 static void qnx6_put_super(struct super_block *sb)
476 struct qnx6_sb_info *qs = QNX6_SB(sb);
481 sb->s_fs_info = NULL;
485 static sector_t qnx6_bmap(struct address_space *mapping, sector_t block)
487 return generic_block_bmap(mapping, block, qnx6_get_block);
489 static const struct address_space_operations qnx6_aops = {
490 .read_folio = qnx6_read_folio,
491 .readahead = qnx6_readahead,
495 static struct inode *qnx6_private_inode(struct super_block *s,
496 struct qnx6_root_node *p)
498 struct inode *inode = new_inode(s);
500 struct qnx6_inode_info *ei = QNX6_I(inode);
501 struct qnx6_sb_info *sbi = QNX6_SB(s);
502 inode->i_size = fs64_to_cpu(sbi, p->size);
503 memcpy(ei->di_block_ptr, p->ptr, sizeof(p->ptr));
504 ei->di_filelevels = p->levels;
505 inode->i_mode = S_IFREG | S_IRUSR; /* probably wrong */
506 inode->i_mapping->a_ops = &qnx6_aops;
511 struct inode *qnx6_iget(struct super_block *sb, unsigned ino)
513 struct qnx6_sb_info *sbi = QNX6_SB(sb);
514 struct qnx6_inode_entry *raw_inode;
516 struct qnx6_inode_info *ei;
517 struct address_space *mapping;
521 inode = iget_locked(sb, ino);
523 return ERR_PTR(-ENOMEM);
524 if (!(inode->i_state & I_NEW))
532 pr_err("bad inode number on dev %s: %u is out of range\n",
535 return ERR_PTR(-EIO);
537 n = (ino - 1) >> (PAGE_SHIFT - QNX6_INODE_SIZE_BITS);
538 mapping = sbi->inodes->i_mapping;
539 folio = read_mapping_folio(mapping, n, NULL);
541 pr_err("major problem: unable to read inode from dev %s\n",
544 return ERR_CAST(folio);
546 offs = offset_in_folio(folio, (ino - 1) << QNX6_INODE_SIZE_BITS);
547 raw_inode = kmap_local_folio(folio, offs);
549 inode->i_mode = fs16_to_cpu(sbi, raw_inode->di_mode);
550 i_uid_write(inode, (uid_t)fs32_to_cpu(sbi, raw_inode->di_uid));
551 i_gid_write(inode, (gid_t)fs32_to_cpu(sbi, raw_inode->di_gid));
552 inode->i_size = fs64_to_cpu(sbi, raw_inode->di_size);
553 inode_set_mtime(inode, fs32_to_cpu(sbi, raw_inode->di_mtime), 0);
554 inode_set_atime(inode, fs32_to_cpu(sbi, raw_inode->di_atime), 0);
555 inode_set_ctime(inode, fs32_to_cpu(sbi, raw_inode->di_ctime), 0);
557 /* calc blocks based on 512 byte blocksize */
558 inode->i_blocks = (inode->i_size + 511) >> 9;
560 memcpy(&ei->di_block_ptr, &raw_inode->di_block_ptr,
561 sizeof(raw_inode->di_block_ptr));
562 ei->di_filelevels = raw_inode->di_filelevels;
564 if (S_ISREG(inode->i_mode)) {
565 inode->i_fop = &generic_ro_fops;
566 inode->i_mapping->a_ops = &qnx6_aops;
567 } else if (S_ISDIR(inode->i_mode)) {
568 inode->i_op = &qnx6_dir_inode_operations;
569 inode->i_fop = &qnx6_dir_operations;
570 inode->i_mapping->a_ops = &qnx6_aops;
571 } else if (S_ISLNK(inode->i_mode)) {
572 inode->i_op = &page_symlink_inode_operations;
573 inode_nohighmem(inode);
574 inode->i_mapping->a_ops = &qnx6_aops;
576 init_special_inode(inode, inode->i_mode, 0);
577 folio_release_kmap(folio, raw_inode);
578 unlock_new_inode(inode);
582 static struct kmem_cache *qnx6_inode_cachep;
584 static struct inode *qnx6_alloc_inode(struct super_block *sb)
586 struct qnx6_inode_info *ei;
587 ei = alloc_inode_sb(sb, qnx6_inode_cachep, GFP_KERNEL);
590 return &ei->vfs_inode;
593 static void qnx6_free_inode(struct inode *inode)
595 kmem_cache_free(qnx6_inode_cachep, QNX6_I(inode));
598 static void init_once(void *foo)
600 struct qnx6_inode_info *ei = (struct qnx6_inode_info *) foo;
602 inode_init_once(&ei->vfs_inode);
605 static int init_inodecache(void)
607 qnx6_inode_cachep = kmem_cache_create("qnx6_inode_cache",
608 sizeof(struct qnx6_inode_info),
609 0, (SLAB_RECLAIM_ACCOUNT|
612 if (!qnx6_inode_cachep)
617 static void destroy_inodecache(void)
620 * Make sure all delayed rcu free inodes are flushed before we
624 kmem_cache_destroy(qnx6_inode_cachep);
627 static int qnx6_get_tree(struct fs_context *fc)
629 return get_tree_bdev(fc, qnx6_fill_super);
632 static void qnx6_free_fc(struct fs_context *fc)
634 kfree(fc->fs_private);
637 static const struct fs_context_operations qnx6_context_ops = {
638 .parse_param = qnx6_parse_param,
639 .get_tree = qnx6_get_tree,
640 .reconfigure = qnx6_reconfigure,
641 .free = qnx6_free_fc,
644 static int qnx6_init_fs_context(struct fs_context *fc)
646 struct qnx6_context *ctx;
648 ctx = kzalloc(sizeof(struct qnx6_context), GFP_KERNEL);
651 fc->ops = &qnx6_context_ops;
652 fc->fs_private = ctx;
657 static struct file_system_type qnx6_fs_type = {
658 .owner = THIS_MODULE,
660 .kill_sb = kill_block_super,
661 .fs_flags = FS_REQUIRES_DEV,
662 .init_fs_context = qnx6_init_fs_context,
663 .parameters = qnx6_param_spec,
665 MODULE_ALIAS_FS("qnx6");
667 static int __init init_qnx6_fs(void)
671 err = init_inodecache();
675 err = register_filesystem(&qnx6_fs_type);
677 destroy_inodecache();
681 pr_info("QNX6 filesystem 1.0.0 registered.\n");
685 static void __exit exit_qnx6_fs(void)
687 unregister_filesystem(&qnx6_fs_type);
688 destroy_inodecache();
691 module_init(init_qnx6_fs)
692 module_exit(exit_qnx6_fs)
693 MODULE_DESCRIPTION("QNX6 file system");
694 MODULE_LICENSE("GPL");