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