Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 2 | /* |
| 3 | * QNX6 file system, Linux implementation. |
| 4 | * |
| 5 | * Version : 1.0.0 |
| 6 | * |
| 7 | * History : |
| 8 | * |
| 9 | * 01-02-2012 by Kai Bankett (chaosman@ontika.net) : first release. |
| 10 | * 16-02-2012 pagemap extension by Al Viro |
| 11 | * |
| 12 | */ |
| 13 | |
| 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> |
| 27 | #include "qnx6.h" |
| 28 | |
| 29 | static const struct super_operations qnx6_sops; |
| 30 | |
| 31 | static void qnx6_put_super(struct super_block *sb); |
| 32 | static struct inode *qnx6_alloc_inode(struct super_block *sb); |
Al Viro | 45c2a3f | 2019-04-15 20:30:08 -0400 | [diff] [blame] | 33 | static void qnx6_free_inode(struct inode *inode); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 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); |
| 37 | |
| 38 | static const struct super_operations qnx6_sops = { |
| 39 | .alloc_inode = qnx6_alloc_inode, |
Al Viro | 45c2a3f | 2019-04-15 20:30:08 -0400 | [diff] [blame] | 40 | .free_inode = qnx6_free_inode, |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 41 | .put_super = qnx6_put_super, |
| 42 | .statfs = qnx6_statfs, |
| 43 | .remount_fs = qnx6_remount, |
| 44 | .show_options = qnx6_show_options, |
| 45 | }; |
| 46 | |
| 47 | static int qnx6_show_options(struct seq_file *seq, struct dentry *root) |
| 48 | { |
| 49 | struct super_block *sb = root->d_sb; |
| 50 | struct qnx6_sb_info *sbi = QNX6_SB(sb); |
| 51 | |
| 52 | if (sbi->s_mount_opt & QNX6_MOUNT_MMI_FS) |
| 53 | seq_puts(seq, ",mmi_fs"); |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int qnx6_remount(struct super_block *sb, int *flags, char *data) |
| 58 | { |
Theodore Ts'o | 02b9984 | 2014-03-13 10:14:33 -0400 | [diff] [blame] | 59 | sync_filesystem(sb); |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 60 | *flags |= SB_RDONLY; |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static unsigned qnx6_get_devblock(struct super_block *sb, __fs32 block) |
| 65 | { |
| 66 | struct qnx6_sb_info *sbi = QNX6_SB(sb); |
| 67 | return fs32_to_cpu(sbi, block) + sbi->s_blks_off; |
| 68 | } |
| 69 | |
| 70 | static unsigned qnx6_block_map(struct inode *inode, unsigned iblock); |
| 71 | |
| 72 | static int qnx6_get_block(struct inode *inode, sector_t iblock, |
| 73 | struct buffer_head *bh, int create) |
| 74 | { |
| 75 | unsigned phys; |
| 76 | |
Fabian Frederick | fa5a7a4 | 2014-08-08 14:23:07 -0700 | [diff] [blame] | 77 | pr_debug("qnx6_get_block inode=[%ld] iblock=[%ld]\n", |
| 78 | inode->i_ino, (unsigned long)iblock); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 79 | |
| 80 | phys = qnx6_block_map(inode, iblock); |
| 81 | if (phys) { |
| 82 | /* logical block is before EOF */ |
| 83 | map_bh(bh, inode->i_sb, phys); |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int qnx6_check_blockptr(__fs32 ptr) |
| 89 | { |
| 90 | if (ptr == ~(__fs32)0) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 91 | pr_err("hit unused blockpointer.\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | static int qnx6_readpage(struct file *file, struct page *page) |
| 98 | { |
| 99 | return mpage_readpage(page, qnx6_get_block); |
| 100 | } |
| 101 | |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 102 | static void qnx6_readahead(struct readahead_control *rac) |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 103 | { |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 104 | mpage_readahead(rac, qnx6_get_block); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * returns the block number for the no-th element in the tree |
| 109 | * inodebits requred as there are multiple inodes in one inode block |
| 110 | */ |
| 111 | static unsigned qnx6_block_map(struct inode *inode, unsigned no) |
| 112 | { |
| 113 | struct super_block *s = inode->i_sb; |
| 114 | struct qnx6_sb_info *sbi = QNX6_SB(s); |
| 115 | struct qnx6_inode_info *ei = QNX6_I(inode); |
| 116 | unsigned block = 0; |
| 117 | struct buffer_head *bh; |
| 118 | __fs32 ptr; |
| 119 | int levelptr; |
| 120 | int ptrbits = sbi->s_ptrbits; |
| 121 | int bitdelta; |
| 122 | u32 mask = (1 << ptrbits) - 1; |
| 123 | int depth = ei->di_filelevels; |
| 124 | int i; |
| 125 | |
| 126 | bitdelta = ptrbits * depth; |
| 127 | levelptr = no >> bitdelta; |
| 128 | |
| 129 | if (levelptr > QNX6_NO_DIRECT_POINTERS - 1) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 130 | pr_err("Requested file block number (%u) too big.", no); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | block = qnx6_get_devblock(s, ei->di_block_ptr[levelptr]); |
| 135 | |
| 136 | for (i = 0; i < depth; i++) { |
| 137 | bh = sb_bread(s, block); |
| 138 | if (!bh) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 139 | pr_err("Error reading block (%u)\n", block); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | bitdelta -= ptrbits; |
| 143 | levelptr = (no >> bitdelta) & mask; |
| 144 | ptr = ((__fs32 *)bh->b_data)[levelptr]; |
| 145 | |
| 146 | if (!qnx6_check_blockptr(ptr)) |
| 147 | return 0; |
| 148 | |
| 149 | block = qnx6_get_devblock(s, ptr); |
| 150 | brelse(bh); |
| 151 | } |
| 152 | return block; |
| 153 | } |
| 154 | |
| 155 | static int qnx6_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 156 | { |
| 157 | struct super_block *sb = dentry->d_sb; |
| 158 | struct qnx6_sb_info *sbi = QNX6_SB(sb); |
| 159 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev); |
| 160 | |
| 161 | buf->f_type = sb->s_magic; |
| 162 | buf->f_bsize = sb->s_blocksize; |
| 163 | buf->f_blocks = fs32_to_cpu(sbi, sbi->sb->sb_num_blocks); |
| 164 | buf->f_bfree = fs32_to_cpu(sbi, sbi->sb->sb_free_blocks); |
| 165 | buf->f_files = fs32_to_cpu(sbi, sbi->sb->sb_num_inodes); |
| 166 | buf->f_ffree = fs32_to_cpu(sbi, sbi->sb->sb_free_inodes); |
| 167 | buf->f_bavail = buf->f_bfree; |
| 168 | buf->f_namelen = QNX6_LONG_NAME_MAX; |
Al Viro | 6d1349c | 2020-09-18 16:45:50 -0400 | [diff] [blame] | 169 | buf->f_fsid = u64_to_fsid(id); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Check the root directory of the filesystem to make sure |
| 176 | * it really _is_ a qnx6 filesystem, and to check the size |
| 177 | * of the directory entry. |
| 178 | */ |
| 179 | static const char *qnx6_checkroot(struct super_block *s) |
| 180 | { |
| 181 | static char match_root[2][3] = {".\0\0", "..\0"}; |
| 182 | int i, error = 0; |
| 183 | struct qnx6_dir_entry *dir_entry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 184 | struct inode *root = d_inode(s->s_root); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 185 | struct address_space *mapping = root->i_mapping; |
| 186 | struct page *page = read_mapping_page(mapping, 0, NULL); |
| 187 | if (IS_ERR(page)) |
| 188 | return "error reading root directory"; |
| 189 | kmap(page); |
| 190 | dir_entry = page_address(page); |
| 191 | for (i = 0; i < 2; i++) { |
| 192 | /* maximum 3 bytes - due to match_root limitation */ |
| 193 | if (strncmp(dir_entry[i].de_fname, match_root[i], 3)) |
| 194 | error = 1; |
| 195 | } |
| 196 | qnx6_put_page(page); |
| 197 | if (error) |
| 198 | return "error reading root directory."; |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | #ifdef CONFIG_QNX6FS_DEBUG |
| 203 | void qnx6_superblock_debug(struct qnx6_super_block *sb, struct super_block *s) |
| 204 | { |
| 205 | struct qnx6_sb_info *sbi = QNX6_SB(s); |
| 206 | |
Fabian Frederick | fa5a7a4 | 2014-08-08 14:23:07 -0700 | [diff] [blame] | 207 | pr_debug("magic: %08x\n", fs32_to_cpu(sbi, sb->sb_magic)); |
| 208 | pr_debug("checksum: %08x\n", fs32_to_cpu(sbi, sb->sb_checksum)); |
| 209 | pr_debug("serial: %llx\n", fs64_to_cpu(sbi, sb->sb_serial)); |
| 210 | pr_debug("flags: %08x\n", fs32_to_cpu(sbi, sb->sb_flags)); |
| 211 | pr_debug("blocksize: %08x\n", fs32_to_cpu(sbi, sb->sb_blocksize)); |
| 212 | pr_debug("num_inodes: %08x\n", fs32_to_cpu(sbi, sb->sb_num_inodes)); |
| 213 | pr_debug("free_inodes: %08x\n", fs32_to_cpu(sbi, sb->sb_free_inodes)); |
| 214 | pr_debug("num_blocks: %08x\n", fs32_to_cpu(sbi, sb->sb_num_blocks)); |
| 215 | pr_debug("free_blocks: %08x\n", fs32_to_cpu(sbi, sb->sb_free_blocks)); |
| 216 | pr_debug("inode_levels: %02x\n", sb->Inode.levels); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 217 | } |
| 218 | #endif |
| 219 | |
| 220 | enum { |
| 221 | Opt_mmifs, |
| 222 | Opt_err |
| 223 | }; |
| 224 | |
| 225 | static const match_table_t tokens = { |
| 226 | {Opt_mmifs, "mmi_fs"}, |
| 227 | {Opt_err, NULL} |
| 228 | }; |
| 229 | |
| 230 | static int qnx6_parse_options(char *options, struct super_block *sb) |
| 231 | { |
| 232 | char *p; |
| 233 | struct qnx6_sb_info *sbi = QNX6_SB(sb); |
| 234 | substring_t args[MAX_OPT_ARGS]; |
| 235 | |
| 236 | if (!options) |
| 237 | return 1; |
| 238 | |
| 239 | while ((p = strsep(&options, ",")) != NULL) { |
| 240 | int token; |
| 241 | if (!*p) |
| 242 | continue; |
| 243 | |
| 244 | token = match_token(p, tokens, args); |
| 245 | switch (token) { |
| 246 | case Opt_mmifs: |
| 247 | set_opt(sbi->s_mount_opt, MMI_FS); |
| 248 | break; |
| 249 | default: |
| 250 | return 0; |
| 251 | } |
| 252 | } |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | static struct buffer_head *qnx6_check_first_superblock(struct super_block *s, |
| 257 | int offset, int silent) |
| 258 | { |
| 259 | struct qnx6_sb_info *sbi = QNX6_SB(s); |
| 260 | struct buffer_head *bh; |
| 261 | struct qnx6_super_block *sb; |
| 262 | |
| 263 | /* Check the superblock signatures |
| 264 | start with the first superblock */ |
| 265 | bh = sb_bread(s, offset); |
| 266 | if (!bh) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 267 | pr_err("unable to read the first superblock\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 268 | return NULL; |
| 269 | } |
| 270 | sb = (struct qnx6_super_block *)bh->b_data; |
| 271 | if (fs32_to_cpu(sbi, sb->sb_magic) != QNX6_SUPER_MAGIC) { |
| 272 | sbi->s_bytesex = BYTESEX_BE; |
| 273 | if (fs32_to_cpu(sbi, sb->sb_magic) == QNX6_SUPER_MAGIC) { |
| 274 | /* we got a big endian fs */ |
Fabian Frederick | fa5a7a4 | 2014-08-08 14:23:07 -0700 | [diff] [blame] | 275 | pr_debug("fs got different endianness.\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 276 | return bh; |
| 277 | } else |
| 278 | sbi->s_bytesex = BYTESEX_LE; |
| 279 | if (!silent) { |
| 280 | if (offset == 0) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 281 | pr_err("wrong signature (magic) in superblock #1.\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 282 | } else { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 283 | pr_info("wrong signature (magic) at position (0x%lx) - will try alternative position (0x0000).\n", |
Fabian Frederick | e00d5b5 | 2014-08-08 14:23:03 -0700 | [diff] [blame] | 284 | offset * s->s_blocksize); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | brelse(bh); |
| 288 | return NULL; |
| 289 | } |
| 290 | return bh; |
| 291 | } |
| 292 | |
| 293 | static struct inode *qnx6_private_inode(struct super_block *s, |
| 294 | struct qnx6_root_node *p); |
| 295 | |
| 296 | static int qnx6_fill_super(struct super_block *s, void *data, int silent) |
| 297 | { |
| 298 | struct buffer_head *bh1 = NULL, *bh2 = NULL; |
| 299 | struct qnx6_super_block *sb1 = NULL, *sb2 = NULL; |
| 300 | struct qnx6_sb_info *sbi; |
| 301 | struct inode *root; |
| 302 | const char *errmsg; |
| 303 | struct qnx6_sb_info *qs; |
| 304 | int ret = -EINVAL; |
| 305 | u64 offset; |
| 306 | int bootblock_offset = QNX6_BOOTBLOCK_SIZE; |
| 307 | |
| 308 | qs = kzalloc(sizeof(struct qnx6_sb_info), GFP_KERNEL); |
| 309 | if (!qs) |
| 310 | return -ENOMEM; |
| 311 | s->s_fs_info = qs; |
| 312 | |
| 313 | /* Superblock always is 512 Byte long */ |
| 314 | if (!sb_set_blocksize(s, QNX6_SUPERBLOCK_SIZE)) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 315 | pr_err("unable to set blocksize\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 316 | goto outnobh; |
| 317 | } |
| 318 | |
| 319 | /* parse the mount-options */ |
| 320 | if (!qnx6_parse_options((char *) data, s)) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 321 | pr_err("invalid mount options.\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 322 | goto outnobh; |
| 323 | } |
| 324 | if (test_opt(s, MMI_FS)) { |
| 325 | sb1 = qnx6_mmi_fill_super(s, silent); |
| 326 | if (sb1) |
| 327 | goto mmi_success; |
| 328 | else |
| 329 | goto outnobh; |
| 330 | } |
| 331 | sbi = QNX6_SB(s); |
| 332 | sbi->s_bytesex = BYTESEX_LE; |
| 333 | /* Check the superblock signatures |
| 334 | start with the first superblock */ |
| 335 | bh1 = qnx6_check_first_superblock(s, |
| 336 | bootblock_offset / QNX6_SUPERBLOCK_SIZE, silent); |
| 337 | if (!bh1) { |
| 338 | /* try again without bootblock offset */ |
| 339 | bh1 = qnx6_check_first_superblock(s, 0, silent); |
| 340 | if (!bh1) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 341 | pr_err("unable to read the first superblock\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 342 | goto outnobh; |
| 343 | } |
| 344 | /* seems that no bootblock at partition start */ |
| 345 | bootblock_offset = 0; |
| 346 | } |
| 347 | sb1 = (struct qnx6_super_block *)bh1->b_data; |
| 348 | |
| 349 | #ifdef CONFIG_QNX6FS_DEBUG |
| 350 | qnx6_superblock_debug(sb1, s); |
| 351 | #endif |
| 352 | |
| 353 | /* checksum check - start at byte 8 and end at byte 512 */ |
| 354 | if (fs32_to_cpu(sbi, sb1->sb_checksum) != |
| 355 | crc32_be(0, (char *)(bh1->b_data + 8), 504)) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 356 | pr_err("superblock #1 checksum error\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 357 | goto out; |
| 358 | } |
| 359 | |
| 360 | /* set new blocksize */ |
| 361 | if (!sb_set_blocksize(s, fs32_to_cpu(sbi, sb1->sb_blocksize))) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 362 | pr_err("unable to set blocksize\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 363 | goto out; |
| 364 | } |
| 365 | /* blocksize invalidates bh - pull it back in */ |
| 366 | brelse(bh1); |
| 367 | bh1 = sb_bread(s, bootblock_offset >> s->s_blocksize_bits); |
| 368 | if (!bh1) |
| 369 | goto outnobh; |
| 370 | sb1 = (struct qnx6_super_block *)bh1->b_data; |
| 371 | |
| 372 | /* calculate second superblock blocknumber */ |
| 373 | offset = fs32_to_cpu(sbi, sb1->sb_num_blocks) + |
| 374 | (bootblock_offset >> s->s_blocksize_bits) + |
| 375 | (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits); |
| 376 | |
| 377 | /* set bootblock offset */ |
| 378 | sbi->s_blks_off = (bootblock_offset >> s->s_blocksize_bits) + |
| 379 | (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits); |
| 380 | |
| 381 | /* next the second superblock */ |
| 382 | bh2 = sb_bread(s, offset); |
| 383 | if (!bh2) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 384 | pr_err("unable to read the second superblock\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 385 | goto out; |
| 386 | } |
| 387 | sb2 = (struct qnx6_super_block *)bh2->b_data; |
| 388 | if (fs32_to_cpu(sbi, sb2->sb_magic) != QNX6_SUPER_MAGIC) { |
| 389 | if (!silent) |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 390 | pr_err("wrong signature (magic) in superblock #2.\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 391 | goto out; |
| 392 | } |
| 393 | |
| 394 | /* checksum check - start at byte 8 and end at byte 512 */ |
| 395 | if (fs32_to_cpu(sbi, sb2->sb_checksum) != |
| 396 | crc32_be(0, (char *)(bh2->b_data + 8), 504)) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 397 | pr_err("superblock #2 checksum error\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 398 | goto out; |
| 399 | } |
| 400 | |
| 401 | if (fs64_to_cpu(sbi, sb1->sb_serial) >= |
| 402 | fs64_to_cpu(sbi, sb2->sb_serial)) { |
| 403 | /* superblock #1 active */ |
| 404 | sbi->sb_buf = bh1; |
| 405 | sbi->sb = (struct qnx6_super_block *)bh1->b_data; |
| 406 | brelse(bh2); |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 407 | pr_info("superblock #1 active\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 408 | } else { |
| 409 | /* superblock #2 active */ |
| 410 | sbi->sb_buf = bh2; |
| 411 | sbi->sb = (struct qnx6_super_block *)bh2->b_data; |
| 412 | brelse(bh1); |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 413 | pr_info("superblock #2 active\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 414 | } |
| 415 | mmi_success: |
| 416 | /* sanity check - limit maximum indirect pointer levels */ |
| 417 | if (sb1->Inode.levels > QNX6_PTR_MAX_LEVELS) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 418 | pr_err("too many inode levels (max %i, sb %i)\n", |
Fabian Frederick | e00d5b5 | 2014-08-08 14:23:03 -0700 | [diff] [blame] | 419 | QNX6_PTR_MAX_LEVELS, sb1->Inode.levels); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 420 | goto out; |
| 421 | } |
| 422 | if (sb1->Longfile.levels > QNX6_PTR_MAX_LEVELS) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 423 | pr_err("too many longfilename levels (max %i, sb %i)\n", |
Fabian Frederick | e00d5b5 | 2014-08-08 14:23:03 -0700 | [diff] [blame] | 424 | QNX6_PTR_MAX_LEVELS, sb1->Longfile.levels); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 425 | goto out; |
| 426 | } |
| 427 | s->s_op = &qnx6_sops; |
| 428 | s->s_magic = QNX6_SUPER_MAGIC; |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 429 | s->s_flags |= SB_RDONLY; /* Yup, read-only yet */ |
Deepa Dinamani | 22b1396 | 2019-07-30 08:22:29 -0700 | [diff] [blame] | 430 | s->s_time_min = 0; |
| 431 | s->s_time_max = U32_MAX; |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 432 | |
| 433 | /* ease the later tree level calculations */ |
| 434 | sbi = QNX6_SB(s); |
| 435 | sbi->s_ptrbits = ilog2(s->s_blocksize / 4); |
| 436 | sbi->inodes = qnx6_private_inode(s, &sb1->Inode); |
| 437 | if (!sbi->inodes) |
| 438 | goto out; |
| 439 | sbi->longfile = qnx6_private_inode(s, &sb1->Longfile); |
| 440 | if (!sbi->longfile) |
| 441 | goto out1; |
| 442 | |
| 443 | /* prefetch root inode */ |
| 444 | root = qnx6_iget(s, QNX6_ROOT_INO); |
| 445 | if (IS_ERR(root)) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 446 | pr_err("get inode failed\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 447 | ret = PTR_ERR(root); |
| 448 | goto out2; |
| 449 | } |
| 450 | |
| 451 | ret = -ENOMEM; |
| 452 | s->s_root = d_make_root(root); |
| 453 | if (!s->s_root) |
| 454 | goto out2; |
| 455 | |
| 456 | ret = -EINVAL; |
| 457 | errmsg = qnx6_checkroot(s); |
| 458 | if (errmsg != NULL) { |
| 459 | if (!silent) |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 460 | pr_err("%s\n", errmsg); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 461 | goto out3; |
| 462 | } |
| 463 | return 0; |
| 464 | |
| 465 | out3: |
| 466 | dput(s->s_root); |
| 467 | s->s_root = NULL; |
| 468 | out2: |
| 469 | iput(sbi->longfile); |
| 470 | out1: |
| 471 | iput(sbi->inodes); |
| 472 | out: |
| 473 | if (bh1) |
| 474 | brelse(bh1); |
| 475 | if (bh2) |
| 476 | brelse(bh2); |
| 477 | outnobh: |
| 478 | kfree(qs); |
| 479 | s->s_fs_info = NULL; |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | static void qnx6_put_super(struct super_block *sb) |
| 484 | { |
| 485 | struct qnx6_sb_info *qs = QNX6_SB(sb); |
| 486 | brelse(qs->sb_buf); |
| 487 | iput(qs->longfile); |
| 488 | iput(qs->inodes); |
| 489 | kfree(qs); |
| 490 | sb->s_fs_info = NULL; |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | static sector_t qnx6_bmap(struct address_space *mapping, sector_t block) |
| 495 | { |
| 496 | return generic_block_bmap(mapping, block, qnx6_get_block); |
| 497 | } |
| 498 | static const struct address_space_operations qnx6_aops = { |
| 499 | .readpage = qnx6_readpage, |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 500 | .readahead = qnx6_readahead, |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 501 | .bmap = qnx6_bmap |
| 502 | }; |
| 503 | |
| 504 | static struct inode *qnx6_private_inode(struct super_block *s, |
| 505 | struct qnx6_root_node *p) |
| 506 | { |
| 507 | struct inode *inode = new_inode(s); |
| 508 | if (inode) { |
| 509 | struct qnx6_inode_info *ei = QNX6_I(inode); |
| 510 | struct qnx6_sb_info *sbi = QNX6_SB(s); |
| 511 | inode->i_size = fs64_to_cpu(sbi, p->size); |
| 512 | memcpy(ei->di_block_ptr, p->ptr, sizeof(p->ptr)); |
| 513 | ei->di_filelevels = p->levels; |
| 514 | inode->i_mode = S_IFREG | S_IRUSR; /* probably wrong */ |
| 515 | inode->i_mapping->a_ops = &qnx6_aops; |
| 516 | } |
| 517 | return inode; |
| 518 | } |
| 519 | |
| 520 | struct inode *qnx6_iget(struct super_block *sb, unsigned ino) |
| 521 | { |
| 522 | struct qnx6_sb_info *sbi = QNX6_SB(sb); |
| 523 | struct qnx6_inode_entry *raw_inode; |
| 524 | struct inode *inode; |
| 525 | struct qnx6_inode_info *ei; |
| 526 | struct address_space *mapping; |
| 527 | struct page *page; |
| 528 | u32 n, offs; |
| 529 | |
| 530 | inode = iget_locked(sb, ino); |
| 531 | if (!inode) |
| 532 | return ERR_PTR(-ENOMEM); |
| 533 | if (!(inode->i_state & I_NEW)) |
| 534 | return inode; |
| 535 | |
| 536 | ei = QNX6_I(inode); |
| 537 | |
| 538 | inode->i_mode = 0; |
| 539 | |
| 540 | if (ino == 0) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 541 | pr_err("bad inode number on dev %s: %u is out of range\n", |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 542 | sb->s_id, ino); |
| 543 | iget_failed(inode); |
| 544 | return ERR_PTR(-EIO); |
| 545 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 546 | n = (ino - 1) >> (PAGE_SHIFT - QNX6_INODE_SIZE_BITS); |
| 547 | offs = (ino - 1) & (~PAGE_MASK >> QNX6_INODE_SIZE_BITS); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 548 | mapping = sbi->inodes->i_mapping; |
| 549 | page = read_mapping_page(mapping, n, NULL); |
| 550 | if (IS_ERR(page)) { |
Fabian Frederick | e6c3261 | 2014-08-08 14:23:05 -0700 | [diff] [blame] | 551 | pr_err("major problem: unable to read inode from dev %s\n", |
Fabian Frederick | e00d5b5 | 2014-08-08 14:23:03 -0700 | [diff] [blame] | 552 | sb->s_id); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 553 | iget_failed(inode); |
| 554 | return ERR_CAST(page); |
| 555 | } |
| 556 | kmap(page); |
| 557 | raw_inode = ((struct qnx6_inode_entry *)page_address(page)) + offs; |
| 558 | |
| 559 | inode->i_mode = fs16_to_cpu(sbi, raw_inode->di_mode); |
Eric W. Biederman | 85a03d1 | 2012-04-07 17:58:48 -0700 | [diff] [blame] | 560 | i_uid_write(inode, (uid_t)fs32_to_cpu(sbi, raw_inode->di_uid)); |
| 561 | i_gid_write(inode, (gid_t)fs32_to_cpu(sbi, raw_inode->di_gid)); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 562 | inode->i_size = fs64_to_cpu(sbi, raw_inode->di_size); |
| 563 | inode->i_mtime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_mtime); |
| 564 | inode->i_mtime.tv_nsec = 0; |
| 565 | inode->i_atime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_atime); |
| 566 | inode->i_atime.tv_nsec = 0; |
| 567 | inode->i_ctime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_ctime); |
| 568 | inode->i_ctime.tv_nsec = 0; |
| 569 | |
| 570 | /* calc blocks based on 512 byte blocksize */ |
| 571 | inode->i_blocks = (inode->i_size + 511) >> 9; |
| 572 | |
| 573 | memcpy(&ei->di_block_ptr, &raw_inode->di_block_ptr, |
| 574 | sizeof(raw_inode->di_block_ptr)); |
| 575 | ei->di_filelevels = raw_inode->di_filelevels; |
| 576 | |
| 577 | if (S_ISREG(inode->i_mode)) { |
| 578 | inode->i_fop = &generic_ro_fops; |
| 579 | inode->i_mapping->a_ops = &qnx6_aops; |
| 580 | } else if (S_ISDIR(inode->i_mode)) { |
| 581 | inode->i_op = &qnx6_dir_inode_operations; |
| 582 | inode->i_fop = &qnx6_dir_operations; |
| 583 | inode->i_mapping->a_ops = &qnx6_aops; |
| 584 | } else if (S_ISLNK(inode->i_mode)) { |
| 585 | inode->i_op = &page_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 586 | inode_nohighmem(inode); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 587 | inode->i_mapping->a_ops = &qnx6_aops; |
| 588 | } else |
| 589 | init_special_inode(inode, inode->i_mode, 0); |
| 590 | qnx6_put_page(page); |
| 591 | unlock_new_inode(inode); |
| 592 | return inode; |
| 593 | } |
| 594 | |
| 595 | static struct kmem_cache *qnx6_inode_cachep; |
| 596 | |
| 597 | static struct inode *qnx6_alloc_inode(struct super_block *sb) |
| 598 | { |
| 599 | struct qnx6_inode_info *ei; |
| 600 | ei = kmem_cache_alloc(qnx6_inode_cachep, GFP_KERNEL); |
| 601 | if (!ei) |
| 602 | return NULL; |
| 603 | return &ei->vfs_inode; |
| 604 | } |
| 605 | |
Al Viro | 45c2a3f | 2019-04-15 20:30:08 -0400 | [diff] [blame] | 606 | static void qnx6_free_inode(struct inode *inode) |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 607 | { |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 608 | kmem_cache_free(qnx6_inode_cachep, QNX6_I(inode)); |
| 609 | } |
| 610 | |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 611 | static void init_once(void *foo) |
| 612 | { |
| 613 | struct qnx6_inode_info *ei = (struct qnx6_inode_info *) foo; |
| 614 | |
| 615 | inode_init_once(&ei->vfs_inode); |
| 616 | } |
| 617 | |
| 618 | static int init_inodecache(void) |
| 619 | { |
| 620 | qnx6_inode_cachep = kmem_cache_create("qnx6_inode_cache", |
| 621 | sizeof(struct qnx6_inode_info), |
| 622 | 0, (SLAB_RECLAIM_ACCOUNT| |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 623 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 624 | init_once); |
| 625 | if (!qnx6_inode_cachep) |
| 626 | return -ENOMEM; |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static void destroy_inodecache(void) |
| 631 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 632 | /* |
| 633 | * Make sure all delayed rcu free inodes are flushed before we |
| 634 | * destroy cache. |
| 635 | */ |
| 636 | rcu_barrier(); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 637 | kmem_cache_destroy(qnx6_inode_cachep); |
| 638 | } |
| 639 | |
| 640 | static struct dentry *qnx6_mount(struct file_system_type *fs_type, |
| 641 | int flags, const char *dev_name, void *data) |
| 642 | { |
| 643 | return mount_bdev(fs_type, flags, dev_name, data, qnx6_fill_super); |
| 644 | } |
| 645 | |
| 646 | static struct file_system_type qnx6_fs_type = { |
| 647 | .owner = THIS_MODULE, |
| 648 | .name = "qnx6", |
| 649 | .mount = qnx6_mount, |
| 650 | .kill_sb = kill_block_super, |
| 651 | .fs_flags = FS_REQUIRES_DEV, |
| 652 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 653 | MODULE_ALIAS_FS("qnx6"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 654 | |
| 655 | static int __init init_qnx6_fs(void) |
| 656 | { |
| 657 | int err; |
| 658 | |
| 659 | err = init_inodecache(); |
| 660 | if (err) |
| 661 | return err; |
| 662 | |
| 663 | err = register_filesystem(&qnx6_fs_type); |
| 664 | if (err) { |
| 665 | destroy_inodecache(); |
| 666 | return err; |
| 667 | } |
| 668 | |
Fabian Frederick | e00d5b5 | 2014-08-08 14:23:03 -0700 | [diff] [blame] | 669 | pr_info("QNX6 filesystem 1.0.0 registered.\n"); |
Kai Bankett | 5d026c7 | 2012-02-17 05:59:20 +0100 | [diff] [blame] | 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static void __exit exit_qnx6_fs(void) |
| 674 | { |
| 675 | unregister_filesystem(&qnx6_fs_type); |
| 676 | destroy_inodecache(); |
| 677 | } |
| 678 | |
| 679 | module_init(init_qnx6_fs) |
| 680 | module_exit(exit_qnx6_fs) |
| 681 | MODULE_LICENSE("GPL"); |