blob: 0042af9a607c23868a043c96f5dd31b905691583 [file] [log] [blame]
Kai Bankett5d026c72012-02-17 05:59:20 +01001/*
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
28static const struct super_operations qnx6_sops;
29
30static void qnx6_put_super(struct super_block *sb);
31static struct inode *qnx6_alloc_inode(struct super_block *sb);
32static void qnx6_destroy_inode(struct inode *inode);
33static int qnx6_remount(struct super_block *sb, int *flags, char *data);
34static int qnx6_statfs(struct dentry *dentry, struct kstatfs *buf);
35static int qnx6_show_options(struct seq_file *seq, struct dentry *root);
36
37static 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
46static 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
56static int qnx6_remount(struct super_block *sb, int *flags, char *data)
57{
Theodore Ts'o02b99842014-03-13 10:14:33 -040058 sync_filesystem(sb);
Kai Bankett5d026c72012-02-17 05:59:20 +010059 *flags |= MS_RDONLY;
60 return 0;
61}
62
63static 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
69static unsigned qnx6_block_map(struct inode *inode, unsigned iblock);
70
71static int qnx6_get_block(struct inode *inode, sector_t iblock,
72 struct buffer_head *bh, int create)
73{
74 unsigned phys;
75
76 QNX6DEBUG((KERN_INFO "qnx6: qnx6_get_block inode=[%ld] iblock=[%ld]\n",
77 inode->i_ino, (unsigned long)iblock));
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
87static int qnx6_check_blockptr(__fs32 ptr)
88{
89 if (ptr == ~(__fs32)0) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -070090 pr_err("qnx6: hit unused blockpointer.\n");
Kai Bankett5d026c72012-02-17 05:59:20 +010091 return 0;
92 }
93 return 1;
94}
95
96static int qnx6_readpage(struct file *file, struct page *page)
97{
98 return mpage_readpage(page, qnx6_get_block);
99}
100
101static 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 */
111static 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 Fredericke00d5b52014-08-08 14:23:03 -0700130 pr_err("qnx6:Requested file block number (%u) too big.", no);
Kai Bankett5d026c72012-02-17 05:59:20 +0100131 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 Fredericke00d5b52014-08-08 14:23:03 -0700139 pr_err("qnx6:Error reading block (%u)\n", block);
Kai Bankett5d026c72012-02-17 05:59:20 +0100140 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
155static 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 */
180static 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
204void qnx6_superblock_debug(struct qnx6_super_block *sb, struct super_block *s)
205{
206 struct qnx6_sb_info *sbi = QNX6_SB(s);
207
208 QNX6DEBUG((KERN_INFO "magic: %08x\n",
209 fs32_to_cpu(sbi, sb->sb_magic)));
210 QNX6DEBUG((KERN_INFO "checksum: %08x\n",
211 fs32_to_cpu(sbi, sb->sb_checksum)));
212 QNX6DEBUG((KERN_INFO "serial: %llx\n",
213 fs64_to_cpu(sbi, sb->sb_serial)));
214 QNX6DEBUG((KERN_INFO "flags: %08x\n",
215 fs32_to_cpu(sbi, sb->sb_flags)));
216 QNX6DEBUG((KERN_INFO "blocksize: %08x\n",
217 fs32_to_cpu(sbi, sb->sb_blocksize)));
218 QNX6DEBUG((KERN_INFO "num_inodes: %08x\n",
219 fs32_to_cpu(sbi, sb->sb_num_inodes)));
220 QNX6DEBUG((KERN_INFO "free_inodes: %08x\n",
221 fs32_to_cpu(sbi, sb->sb_free_inodes)));
222 QNX6DEBUG((KERN_INFO "num_blocks: %08x\n",
223 fs32_to_cpu(sbi, sb->sb_num_blocks)));
224 QNX6DEBUG((KERN_INFO "free_blocks: %08x\n",
225 fs32_to_cpu(sbi, sb->sb_free_blocks)));
226 QNX6DEBUG((KERN_INFO "inode_levels: %02x\n",
227 sb->Inode.levels));
228}
229#endif
230
231enum {
232 Opt_mmifs,
233 Opt_err
234};
235
236static const match_table_t tokens = {
237 {Opt_mmifs, "mmi_fs"},
238 {Opt_err, NULL}
239};
240
241static int qnx6_parse_options(char *options, struct super_block *sb)
242{
243 char *p;
244 struct qnx6_sb_info *sbi = QNX6_SB(sb);
245 substring_t args[MAX_OPT_ARGS];
246
247 if (!options)
248 return 1;
249
250 while ((p = strsep(&options, ",")) != NULL) {
251 int token;
252 if (!*p)
253 continue;
254
255 token = match_token(p, tokens, args);
256 switch (token) {
257 case Opt_mmifs:
258 set_opt(sbi->s_mount_opt, MMI_FS);
259 break;
260 default:
261 return 0;
262 }
263 }
264 return 1;
265}
266
267static struct buffer_head *qnx6_check_first_superblock(struct super_block *s,
268 int offset, int silent)
269{
270 struct qnx6_sb_info *sbi = QNX6_SB(s);
271 struct buffer_head *bh;
272 struct qnx6_super_block *sb;
273
274 /* Check the superblock signatures
275 start with the first superblock */
276 bh = sb_bread(s, offset);
277 if (!bh) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700278 pr_err("qnx6: unable to read the first superblock\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100279 return NULL;
280 }
281 sb = (struct qnx6_super_block *)bh->b_data;
282 if (fs32_to_cpu(sbi, sb->sb_magic) != QNX6_SUPER_MAGIC) {
283 sbi->s_bytesex = BYTESEX_BE;
284 if (fs32_to_cpu(sbi, sb->sb_magic) == QNX6_SUPER_MAGIC) {
285 /* we got a big endian fs */
286 QNX6DEBUG((KERN_INFO "qnx6: fs got different"
Masanari Iida8a168ca2012-12-29 02:00:09 +0900287 " endianness.\n"));
Kai Bankett5d026c72012-02-17 05:59:20 +0100288 return bh;
289 } else
290 sbi->s_bytesex = BYTESEX_LE;
291 if (!silent) {
292 if (offset == 0) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700293 pr_err("qnx6: wrong signature (magic) in superblock #1.\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100294 } else {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700295 pr_info("qnx6: wrong signature (magic) at position (0x%lx) - will try alternative position (0x0000).\n",
296 offset * s->s_blocksize);
Kai Bankett5d026c72012-02-17 05:59:20 +0100297 }
298 }
299 brelse(bh);
300 return NULL;
301 }
302 return bh;
303}
304
305static struct inode *qnx6_private_inode(struct super_block *s,
306 struct qnx6_root_node *p);
307
308static int qnx6_fill_super(struct super_block *s, void *data, int silent)
309{
310 struct buffer_head *bh1 = NULL, *bh2 = NULL;
311 struct qnx6_super_block *sb1 = NULL, *sb2 = NULL;
312 struct qnx6_sb_info *sbi;
313 struct inode *root;
314 const char *errmsg;
315 struct qnx6_sb_info *qs;
316 int ret = -EINVAL;
317 u64 offset;
318 int bootblock_offset = QNX6_BOOTBLOCK_SIZE;
319
320 qs = kzalloc(sizeof(struct qnx6_sb_info), GFP_KERNEL);
321 if (!qs)
322 return -ENOMEM;
323 s->s_fs_info = qs;
324
325 /* Superblock always is 512 Byte long */
326 if (!sb_set_blocksize(s, QNX6_SUPERBLOCK_SIZE)) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700327 pr_err("qnx6: unable to set blocksize\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100328 goto outnobh;
329 }
330
331 /* parse the mount-options */
332 if (!qnx6_parse_options((char *) data, s)) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700333 pr_err("qnx6: invalid mount options.\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100334 goto outnobh;
335 }
336 if (test_opt(s, MMI_FS)) {
337 sb1 = qnx6_mmi_fill_super(s, silent);
338 if (sb1)
339 goto mmi_success;
340 else
341 goto outnobh;
342 }
343 sbi = QNX6_SB(s);
344 sbi->s_bytesex = BYTESEX_LE;
345 /* Check the superblock signatures
346 start with the first superblock */
347 bh1 = qnx6_check_first_superblock(s,
348 bootblock_offset / QNX6_SUPERBLOCK_SIZE, silent);
349 if (!bh1) {
350 /* try again without bootblock offset */
351 bh1 = qnx6_check_first_superblock(s, 0, silent);
352 if (!bh1) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700353 pr_err("qnx6: unable to read the first superblock\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100354 goto outnobh;
355 }
356 /* seems that no bootblock at partition start */
357 bootblock_offset = 0;
358 }
359 sb1 = (struct qnx6_super_block *)bh1->b_data;
360
361#ifdef CONFIG_QNX6FS_DEBUG
362 qnx6_superblock_debug(sb1, s);
363#endif
364
365 /* checksum check - start at byte 8 and end at byte 512 */
366 if (fs32_to_cpu(sbi, sb1->sb_checksum) !=
367 crc32_be(0, (char *)(bh1->b_data + 8), 504)) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700368 pr_err("qnx6: superblock #1 checksum error\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100369 goto out;
370 }
371
372 /* set new blocksize */
373 if (!sb_set_blocksize(s, fs32_to_cpu(sbi, sb1->sb_blocksize))) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700374 pr_err("qnx6: unable to set blocksize\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100375 goto out;
376 }
377 /* blocksize invalidates bh - pull it back in */
378 brelse(bh1);
379 bh1 = sb_bread(s, bootblock_offset >> s->s_blocksize_bits);
380 if (!bh1)
381 goto outnobh;
382 sb1 = (struct qnx6_super_block *)bh1->b_data;
383
384 /* calculate second superblock blocknumber */
385 offset = fs32_to_cpu(sbi, sb1->sb_num_blocks) +
386 (bootblock_offset >> s->s_blocksize_bits) +
387 (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits);
388
389 /* set bootblock offset */
390 sbi->s_blks_off = (bootblock_offset >> s->s_blocksize_bits) +
391 (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits);
392
393 /* next the second superblock */
394 bh2 = sb_bread(s, offset);
395 if (!bh2) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700396 pr_err("qnx6: unable to read the second superblock\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100397 goto out;
398 }
399 sb2 = (struct qnx6_super_block *)bh2->b_data;
400 if (fs32_to_cpu(sbi, sb2->sb_magic) != QNX6_SUPER_MAGIC) {
401 if (!silent)
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700402 pr_err("qnx6: wrong signature (magic) in superblock #2.\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100403 goto out;
404 }
405
406 /* checksum check - start at byte 8 and end at byte 512 */
407 if (fs32_to_cpu(sbi, sb2->sb_checksum) !=
408 crc32_be(0, (char *)(bh2->b_data + 8), 504)) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700409 pr_err("qnx6: superblock #2 checksum error\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100410 goto out;
411 }
412
413 if (fs64_to_cpu(sbi, sb1->sb_serial) >=
414 fs64_to_cpu(sbi, sb2->sb_serial)) {
415 /* superblock #1 active */
416 sbi->sb_buf = bh1;
417 sbi->sb = (struct qnx6_super_block *)bh1->b_data;
418 brelse(bh2);
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700419 pr_info("qnx6: superblock #1 active\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100420 } else {
421 /* superblock #2 active */
422 sbi->sb_buf = bh2;
423 sbi->sb = (struct qnx6_super_block *)bh2->b_data;
424 brelse(bh1);
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700425 pr_info("qnx6: superblock #2 active\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100426 }
427mmi_success:
428 /* sanity check - limit maximum indirect pointer levels */
429 if (sb1->Inode.levels > QNX6_PTR_MAX_LEVELS) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700430 pr_err("qnx6: too many inode levels (max %i, sb %i)\n",
431 QNX6_PTR_MAX_LEVELS, sb1->Inode.levels);
Kai Bankett5d026c72012-02-17 05:59:20 +0100432 goto out;
433 }
434 if (sb1->Longfile.levels > QNX6_PTR_MAX_LEVELS) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700435 pr_err("qnx6: too many longfilename levels (max %i, sb %i)\n",
436 QNX6_PTR_MAX_LEVELS, sb1->Longfile.levels);
Kai Bankett5d026c72012-02-17 05:59:20 +0100437 goto out;
438 }
439 s->s_op = &qnx6_sops;
440 s->s_magic = QNX6_SUPER_MAGIC;
441 s->s_flags |= MS_RDONLY; /* Yup, read-only yet */
442
443 /* ease the later tree level calculations */
444 sbi = QNX6_SB(s);
445 sbi->s_ptrbits = ilog2(s->s_blocksize / 4);
446 sbi->inodes = qnx6_private_inode(s, &sb1->Inode);
447 if (!sbi->inodes)
448 goto out;
449 sbi->longfile = qnx6_private_inode(s, &sb1->Longfile);
450 if (!sbi->longfile)
451 goto out1;
452
453 /* prefetch root inode */
454 root = qnx6_iget(s, QNX6_ROOT_INO);
455 if (IS_ERR(root)) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700456 pr_err("qnx6: get inode failed\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100457 ret = PTR_ERR(root);
458 goto out2;
459 }
460
461 ret = -ENOMEM;
462 s->s_root = d_make_root(root);
463 if (!s->s_root)
464 goto out2;
465
466 ret = -EINVAL;
467 errmsg = qnx6_checkroot(s);
468 if (errmsg != NULL) {
469 if (!silent)
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700470 pr_err("qnx6: %s\n", errmsg);
Kai Bankett5d026c72012-02-17 05:59:20 +0100471 goto out3;
472 }
473 return 0;
474
475out3:
476 dput(s->s_root);
477 s->s_root = NULL;
478out2:
479 iput(sbi->longfile);
480out1:
481 iput(sbi->inodes);
482out:
483 if (bh1)
484 brelse(bh1);
485 if (bh2)
486 brelse(bh2);
487outnobh:
488 kfree(qs);
489 s->s_fs_info = NULL;
490 return ret;
491}
492
493static void qnx6_put_super(struct super_block *sb)
494{
495 struct qnx6_sb_info *qs = QNX6_SB(sb);
496 brelse(qs->sb_buf);
497 iput(qs->longfile);
498 iput(qs->inodes);
499 kfree(qs);
500 sb->s_fs_info = NULL;
501 return;
502}
503
504static sector_t qnx6_bmap(struct address_space *mapping, sector_t block)
505{
506 return generic_block_bmap(mapping, block, qnx6_get_block);
507}
508static const struct address_space_operations qnx6_aops = {
509 .readpage = qnx6_readpage,
510 .readpages = qnx6_readpages,
511 .bmap = qnx6_bmap
512};
513
514static struct inode *qnx6_private_inode(struct super_block *s,
515 struct qnx6_root_node *p)
516{
517 struct inode *inode = new_inode(s);
518 if (inode) {
519 struct qnx6_inode_info *ei = QNX6_I(inode);
520 struct qnx6_sb_info *sbi = QNX6_SB(s);
521 inode->i_size = fs64_to_cpu(sbi, p->size);
522 memcpy(ei->di_block_ptr, p->ptr, sizeof(p->ptr));
523 ei->di_filelevels = p->levels;
524 inode->i_mode = S_IFREG | S_IRUSR; /* probably wrong */
525 inode->i_mapping->a_ops = &qnx6_aops;
526 }
527 return inode;
528}
529
530struct inode *qnx6_iget(struct super_block *sb, unsigned ino)
531{
532 struct qnx6_sb_info *sbi = QNX6_SB(sb);
533 struct qnx6_inode_entry *raw_inode;
534 struct inode *inode;
535 struct qnx6_inode_info *ei;
536 struct address_space *mapping;
537 struct page *page;
538 u32 n, offs;
539
540 inode = iget_locked(sb, ino);
541 if (!inode)
542 return ERR_PTR(-ENOMEM);
543 if (!(inode->i_state & I_NEW))
544 return inode;
545
546 ei = QNX6_I(inode);
547
548 inode->i_mode = 0;
549
550 if (ino == 0) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700551 pr_err("qnx6: bad inode number on dev %s: %u is out of range\n",
Kai Bankett5d026c72012-02-17 05:59:20 +0100552 sb->s_id, ino);
553 iget_failed(inode);
554 return ERR_PTR(-EIO);
555 }
556 n = (ino - 1) >> (PAGE_CACHE_SHIFT - QNX6_INODE_SIZE_BITS);
557 offs = (ino - 1) & (~PAGE_CACHE_MASK >> QNX6_INODE_SIZE_BITS);
558 mapping = sbi->inodes->i_mapping;
559 page = read_mapping_page(mapping, n, NULL);
560 if (IS_ERR(page)) {
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700561 pr_err("qnx6: major problem: unable to read inode from dev %s\n",
562 sb->s_id);
Kai Bankett5d026c72012-02-17 05:59:20 +0100563 iget_failed(inode);
564 return ERR_CAST(page);
565 }
566 kmap(page);
567 raw_inode = ((struct qnx6_inode_entry *)page_address(page)) + offs;
568
569 inode->i_mode = fs16_to_cpu(sbi, raw_inode->di_mode);
Eric W. Biederman85a03d12012-04-07 17:58:48 -0700570 i_uid_write(inode, (uid_t)fs32_to_cpu(sbi, raw_inode->di_uid));
571 i_gid_write(inode, (gid_t)fs32_to_cpu(sbi, raw_inode->di_gid));
Kai Bankett5d026c72012-02-17 05:59:20 +0100572 inode->i_size = fs64_to_cpu(sbi, raw_inode->di_size);
573 inode->i_mtime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_mtime);
574 inode->i_mtime.tv_nsec = 0;
575 inode->i_atime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_atime);
576 inode->i_atime.tv_nsec = 0;
577 inode->i_ctime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_ctime);
578 inode->i_ctime.tv_nsec = 0;
579
580 /* calc blocks based on 512 byte blocksize */
581 inode->i_blocks = (inode->i_size + 511) >> 9;
582
583 memcpy(&ei->di_block_ptr, &raw_inode->di_block_ptr,
584 sizeof(raw_inode->di_block_ptr));
585 ei->di_filelevels = raw_inode->di_filelevels;
586
587 if (S_ISREG(inode->i_mode)) {
588 inode->i_fop = &generic_ro_fops;
589 inode->i_mapping->a_ops = &qnx6_aops;
590 } else if (S_ISDIR(inode->i_mode)) {
591 inode->i_op = &qnx6_dir_inode_operations;
592 inode->i_fop = &qnx6_dir_operations;
593 inode->i_mapping->a_ops = &qnx6_aops;
594 } else if (S_ISLNK(inode->i_mode)) {
595 inode->i_op = &page_symlink_inode_operations;
596 inode->i_mapping->a_ops = &qnx6_aops;
597 } else
598 init_special_inode(inode, inode->i_mode, 0);
599 qnx6_put_page(page);
600 unlock_new_inode(inode);
601 return inode;
602}
603
604static struct kmem_cache *qnx6_inode_cachep;
605
606static struct inode *qnx6_alloc_inode(struct super_block *sb)
607{
608 struct qnx6_inode_info *ei;
609 ei = kmem_cache_alloc(qnx6_inode_cachep, GFP_KERNEL);
610 if (!ei)
611 return NULL;
612 return &ei->vfs_inode;
613}
614
615static void qnx6_i_callback(struct rcu_head *head)
616{
617 struct inode *inode = container_of(head, struct inode, i_rcu);
Kai Bankett5d026c72012-02-17 05:59:20 +0100618 kmem_cache_free(qnx6_inode_cachep, QNX6_I(inode));
619}
620
621static void qnx6_destroy_inode(struct inode *inode)
622{
623 call_rcu(&inode->i_rcu, qnx6_i_callback);
624}
625
626static void init_once(void *foo)
627{
628 struct qnx6_inode_info *ei = (struct qnx6_inode_info *) foo;
629
630 inode_init_once(&ei->vfs_inode);
631}
632
633static int init_inodecache(void)
634{
635 qnx6_inode_cachep = kmem_cache_create("qnx6_inode_cache",
636 sizeof(struct qnx6_inode_info),
637 0, (SLAB_RECLAIM_ACCOUNT|
638 SLAB_MEM_SPREAD),
639 init_once);
640 if (!qnx6_inode_cachep)
641 return -ENOMEM;
642 return 0;
643}
644
645static void destroy_inodecache(void)
646{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000647 /*
648 * Make sure all delayed rcu free inodes are flushed before we
649 * destroy cache.
650 */
651 rcu_barrier();
Kai Bankett5d026c72012-02-17 05:59:20 +0100652 kmem_cache_destroy(qnx6_inode_cachep);
653}
654
655static struct dentry *qnx6_mount(struct file_system_type *fs_type,
656 int flags, const char *dev_name, void *data)
657{
658 return mount_bdev(fs_type, flags, dev_name, data, qnx6_fill_super);
659}
660
661static struct file_system_type qnx6_fs_type = {
662 .owner = THIS_MODULE,
663 .name = "qnx6",
664 .mount = qnx6_mount,
665 .kill_sb = kill_block_super,
666 .fs_flags = FS_REQUIRES_DEV,
667};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800668MODULE_ALIAS_FS("qnx6");
Kai Bankett5d026c72012-02-17 05:59:20 +0100669
670static int __init init_qnx6_fs(void)
671{
672 int err;
673
674 err = init_inodecache();
675 if (err)
676 return err;
677
678 err = register_filesystem(&qnx6_fs_type);
679 if (err) {
680 destroy_inodecache();
681 return err;
682 }
683
Fabian Fredericke00d5b52014-08-08 14:23:03 -0700684 pr_info("QNX6 filesystem 1.0.0 registered.\n");
Kai Bankett5d026c72012-02-17 05:59:20 +0100685 return 0;
686}
687
688static void __exit exit_qnx6_fs(void)
689{
690 unregister_filesystem(&qnx6_fs_type);
691 destroy_inodecache();
692}
693
694module_init(init_qnx6_fs)
695module_exit(exit_qnx6_fs)
696MODULE_LICENSE("GPL");