]>
Commit | Line | Data |
---|---|---|
5d026c72 KB |
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 | { | |
02b9984d | 58 | sync_filesystem(sb); |
5d026c72 KB |
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 | ||
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 | ||
87 | static int qnx6_check_blockptr(__fs32 ptr) | |
88 | { | |
89 | if (ptr == ~(__fs32)0) { | |
90 | printk(KERN_ERR "qnx6: hit unused blockpointer.\n"); | |
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) { | |
130 | printk(KERN_ERR "qnx6:Requested file block number (%u) too big.", | |
131 | no); | |
132 | return 0; | |
133 | } | |
134 | ||
135 | block = qnx6_get_devblock(s, ei->di_block_ptr[levelptr]); | |
136 | ||
137 | for (i = 0; i < depth; i++) { | |
138 | bh = sb_bread(s, block); | |
139 | if (!bh) { | |
140 | printk(KERN_ERR "qnx6:Error reading block (%u)\n", | |
141 | block); | |
142 | return 0; | |
143 | } | |
144 | bitdelta -= ptrbits; | |
145 | levelptr = (no >> bitdelta) & mask; | |
146 | ptr = ((__fs32 *)bh->b_data)[levelptr]; | |
147 | ||
148 | if (!qnx6_check_blockptr(ptr)) | |
149 | return 0; | |
150 | ||
151 | block = qnx6_get_devblock(s, ptr); | |
152 | brelse(bh); | |
153 | } | |
154 | return block; | |
155 | } | |
156 | ||
157 | static int qnx6_statfs(struct dentry *dentry, struct kstatfs *buf) | |
158 | { | |
159 | struct super_block *sb = dentry->d_sb; | |
160 | struct qnx6_sb_info *sbi = QNX6_SB(sb); | |
161 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev); | |
162 | ||
163 | buf->f_type = sb->s_magic; | |
164 | buf->f_bsize = sb->s_blocksize; | |
165 | buf->f_blocks = fs32_to_cpu(sbi, sbi->sb->sb_num_blocks); | |
166 | buf->f_bfree = fs32_to_cpu(sbi, sbi->sb->sb_free_blocks); | |
167 | buf->f_files = fs32_to_cpu(sbi, sbi->sb->sb_num_inodes); | |
168 | buf->f_ffree = fs32_to_cpu(sbi, sbi->sb->sb_free_inodes); | |
169 | buf->f_bavail = buf->f_bfree; | |
170 | buf->f_namelen = QNX6_LONG_NAME_MAX; | |
171 | buf->f_fsid.val[0] = (u32)id; | |
172 | buf->f_fsid.val[1] = (u32)(id >> 32); | |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
177 | /* | |
178 | * Check the root directory of the filesystem to make sure | |
179 | * it really _is_ a qnx6 filesystem, and to check the size | |
180 | * of the directory entry. | |
181 | */ | |
182 | static const char *qnx6_checkroot(struct super_block *s) | |
183 | { | |
184 | static char match_root[2][3] = {".\0\0", "..\0"}; | |
185 | int i, error = 0; | |
186 | struct qnx6_dir_entry *dir_entry; | |
187 | struct inode *root = s->s_root->d_inode; | |
188 | struct address_space *mapping = root->i_mapping; | |
189 | struct page *page = read_mapping_page(mapping, 0, NULL); | |
190 | if (IS_ERR(page)) | |
191 | return "error reading root directory"; | |
192 | kmap(page); | |
193 | dir_entry = page_address(page); | |
194 | for (i = 0; i < 2; i++) { | |
195 | /* maximum 3 bytes - due to match_root limitation */ | |
196 | if (strncmp(dir_entry[i].de_fname, match_root[i], 3)) | |
197 | error = 1; | |
198 | } | |
199 | qnx6_put_page(page); | |
200 | if (error) | |
201 | return "error reading root directory."; | |
202 | return NULL; | |
203 | } | |
204 | ||
205 | #ifdef CONFIG_QNX6FS_DEBUG | |
206 | void qnx6_superblock_debug(struct qnx6_super_block *sb, struct super_block *s) | |
207 | { | |
208 | struct qnx6_sb_info *sbi = QNX6_SB(s); | |
209 | ||
210 | QNX6DEBUG((KERN_INFO "magic: %08x\n", | |
211 | fs32_to_cpu(sbi, sb->sb_magic))); | |
212 | QNX6DEBUG((KERN_INFO "checksum: %08x\n", | |
213 | fs32_to_cpu(sbi, sb->sb_checksum))); | |
214 | QNX6DEBUG((KERN_INFO "serial: %llx\n", | |
215 | fs64_to_cpu(sbi, sb->sb_serial))); | |
216 | QNX6DEBUG((KERN_INFO "flags: %08x\n", | |
217 | fs32_to_cpu(sbi, sb->sb_flags))); | |
218 | QNX6DEBUG((KERN_INFO "blocksize: %08x\n", | |
219 | fs32_to_cpu(sbi, sb->sb_blocksize))); | |
220 | QNX6DEBUG((KERN_INFO "num_inodes: %08x\n", | |
221 | fs32_to_cpu(sbi, sb->sb_num_inodes))); | |
222 | QNX6DEBUG((KERN_INFO "free_inodes: %08x\n", | |
223 | fs32_to_cpu(sbi, sb->sb_free_inodes))); | |
224 | QNX6DEBUG((KERN_INFO "num_blocks: %08x\n", | |
225 | fs32_to_cpu(sbi, sb->sb_num_blocks))); | |
226 | QNX6DEBUG((KERN_INFO "free_blocks: %08x\n", | |
227 | fs32_to_cpu(sbi, sb->sb_free_blocks))); | |
228 | QNX6DEBUG((KERN_INFO "inode_levels: %02x\n", | |
229 | sb->Inode.levels)); | |
230 | } | |
231 | #endif | |
232 | ||
233 | enum { | |
234 | Opt_mmifs, | |
235 | Opt_err | |
236 | }; | |
237 | ||
238 | static const match_table_t tokens = { | |
239 | {Opt_mmifs, "mmi_fs"}, | |
240 | {Opt_err, NULL} | |
241 | }; | |
242 | ||
243 | static int qnx6_parse_options(char *options, struct super_block *sb) | |
244 | { | |
245 | char *p; | |
246 | struct qnx6_sb_info *sbi = QNX6_SB(sb); | |
247 | substring_t args[MAX_OPT_ARGS]; | |
248 | ||
249 | if (!options) | |
250 | return 1; | |
251 | ||
252 | while ((p = strsep(&options, ",")) != NULL) { | |
253 | int token; | |
254 | if (!*p) | |
255 | continue; | |
256 | ||
257 | token = match_token(p, tokens, args); | |
258 | switch (token) { | |
259 | case Opt_mmifs: | |
260 | set_opt(sbi->s_mount_opt, MMI_FS); | |
261 | break; | |
262 | default: | |
263 | return 0; | |
264 | } | |
265 | } | |
266 | return 1; | |
267 | } | |
268 | ||
269 | static struct buffer_head *qnx6_check_first_superblock(struct super_block *s, | |
270 | int offset, int silent) | |
271 | { | |
272 | struct qnx6_sb_info *sbi = QNX6_SB(s); | |
273 | struct buffer_head *bh; | |
274 | struct qnx6_super_block *sb; | |
275 | ||
276 | /* Check the superblock signatures | |
277 | start with the first superblock */ | |
278 | bh = sb_bread(s, offset); | |
279 | if (!bh) { | |
280 | printk(KERN_ERR "qnx6: unable to read the first superblock\n"); | |
281 | return NULL; | |
282 | } | |
283 | sb = (struct qnx6_super_block *)bh->b_data; | |
284 | if (fs32_to_cpu(sbi, sb->sb_magic) != QNX6_SUPER_MAGIC) { | |
285 | sbi->s_bytesex = BYTESEX_BE; | |
286 | if (fs32_to_cpu(sbi, sb->sb_magic) == QNX6_SUPER_MAGIC) { | |
287 | /* we got a big endian fs */ | |
288 | QNX6DEBUG((KERN_INFO "qnx6: fs got different" | |
8a168ca7 | 289 | " endianness.\n")); |
5d026c72 KB |
290 | return bh; |
291 | } else | |
292 | sbi->s_bytesex = BYTESEX_LE; | |
293 | if (!silent) { | |
294 | if (offset == 0) { | |
295 | printk(KERN_ERR "qnx6: wrong signature (magic)" | |
296 | " in superblock #1.\n"); | |
297 | } else { | |
298 | printk(KERN_INFO "qnx6: wrong signature (magic)" | |
299 | " at position (0x%lx) - will try" | |
300 | " alternative position (0x0000).\n", | |
301 | offset * s->s_blocksize); | |
302 | } | |
303 | } | |
304 | brelse(bh); | |
305 | return NULL; | |
306 | } | |
307 | return bh; | |
308 | } | |
309 | ||
310 | static struct inode *qnx6_private_inode(struct super_block *s, | |
311 | struct qnx6_root_node *p); | |
312 | ||
313 | static int qnx6_fill_super(struct super_block *s, void *data, int silent) | |
314 | { | |
315 | struct buffer_head *bh1 = NULL, *bh2 = NULL; | |
316 | struct qnx6_super_block *sb1 = NULL, *sb2 = NULL; | |
317 | struct qnx6_sb_info *sbi; | |
318 | struct inode *root; | |
319 | const char *errmsg; | |
320 | struct qnx6_sb_info *qs; | |
321 | int ret = -EINVAL; | |
322 | u64 offset; | |
323 | int bootblock_offset = QNX6_BOOTBLOCK_SIZE; | |
324 | ||
325 | qs = kzalloc(sizeof(struct qnx6_sb_info), GFP_KERNEL); | |
326 | if (!qs) | |
327 | return -ENOMEM; | |
328 | s->s_fs_info = qs; | |
329 | ||
330 | /* Superblock always is 512 Byte long */ | |
331 | if (!sb_set_blocksize(s, QNX6_SUPERBLOCK_SIZE)) { | |
332 | printk(KERN_ERR "qnx6: unable to set blocksize\n"); | |
333 | goto outnobh; | |
334 | } | |
335 | ||
336 | /* parse the mount-options */ | |
337 | if (!qnx6_parse_options((char *) data, s)) { | |
338 | printk(KERN_ERR "qnx6: invalid mount options.\n"); | |
339 | goto outnobh; | |
340 | } | |
341 | if (test_opt(s, MMI_FS)) { | |
342 | sb1 = qnx6_mmi_fill_super(s, silent); | |
343 | if (sb1) | |
344 | goto mmi_success; | |
345 | else | |
346 | goto outnobh; | |
347 | } | |
348 | sbi = QNX6_SB(s); | |
349 | sbi->s_bytesex = BYTESEX_LE; | |
350 | /* Check the superblock signatures | |
351 | start with the first superblock */ | |
352 | bh1 = qnx6_check_first_superblock(s, | |
353 | bootblock_offset / QNX6_SUPERBLOCK_SIZE, silent); | |
354 | if (!bh1) { | |
355 | /* try again without bootblock offset */ | |
356 | bh1 = qnx6_check_first_superblock(s, 0, silent); | |
357 | if (!bh1) { | |
358 | printk(KERN_ERR "qnx6: unable to read the first superblock\n"); | |
359 | goto outnobh; | |
360 | } | |
361 | /* seems that no bootblock at partition start */ | |
362 | bootblock_offset = 0; | |
363 | } | |
364 | sb1 = (struct qnx6_super_block *)bh1->b_data; | |
365 | ||
366 | #ifdef CONFIG_QNX6FS_DEBUG | |
367 | qnx6_superblock_debug(sb1, s); | |
368 | #endif | |
369 | ||
370 | /* checksum check - start at byte 8 and end at byte 512 */ | |
371 | if (fs32_to_cpu(sbi, sb1->sb_checksum) != | |
372 | crc32_be(0, (char *)(bh1->b_data + 8), 504)) { | |
373 | printk(KERN_ERR "qnx6: superblock #1 checksum error\n"); | |
374 | goto out; | |
375 | } | |
376 | ||
377 | /* set new blocksize */ | |
378 | if (!sb_set_blocksize(s, fs32_to_cpu(sbi, sb1->sb_blocksize))) { | |
379 | printk(KERN_ERR "qnx6: unable to set blocksize\n"); | |
380 | goto out; | |
381 | } | |
382 | /* blocksize invalidates bh - pull it back in */ | |
383 | brelse(bh1); | |
384 | bh1 = sb_bread(s, bootblock_offset >> s->s_blocksize_bits); | |
385 | if (!bh1) | |
386 | goto outnobh; | |
387 | sb1 = (struct qnx6_super_block *)bh1->b_data; | |
388 | ||
389 | /* calculate second superblock blocknumber */ | |
390 | offset = fs32_to_cpu(sbi, sb1->sb_num_blocks) + | |
391 | (bootblock_offset >> s->s_blocksize_bits) + | |
392 | (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits); | |
393 | ||
394 | /* set bootblock offset */ | |
395 | sbi->s_blks_off = (bootblock_offset >> s->s_blocksize_bits) + | |
396 | (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits); | |
397 | ||
398 | /* next the second superblock */ | |
399 | bh2 = sb_bread(s, offset); | |
400 | if (!bh2) { | |
401 | printk(KERN_ERR "qnx6: unable to read the second superblock\n"); | |
402 | goto out; | |
403 | } | |
404 | sb2 = (struct qnx6_super_block *)bh2->b_data; | |
405 | if (fs32_to_cpu(sbi, sb2->sb_magic) != QNX6_SUPER_MAGIC) { | |
406 | if (!silent) | |
407 | printk(KERN_ERR "qnx6: wrong signature (magic)" | |
408 | " in superblock #2.\n"); | |
409 | goto out; | |
410 | } | |
411 | ||
412 | /* checksum check - start at byte 8 and end at byte 512 */ | |
413 | if (fs32_to_cpu(sbi, sb2->sb_checksum) != | |
414 | crc32_be(0, (char *)(bh2->b_data + 8), 504)) { | |
415 | printk(KERN_ERR "qnx6: superblock #2 checksum error\n"); | |
416 | goto out; | |
417 | } | |
418 | ||
419 | if (fs64_to_cpu(sbi, sb1->sb_serial) >= | |
420 | fs64_to_cpu(sbi, sb2->sb_serial)) { | |
421 | /* superblock #1 active */ | |
422 | sbi->sb_buf = bh1; | |
423 | sbi->sb = (struct qnx6_super_block *)bh1->b_data; | |
424 | brelse(bh2); | |
425 | printk(KERN_INFO "qnx6: superblock #1 active\n"); | |
426 | } else { | |
427 | /* superblock #2 active */ | |
428 | sbi->sb_buf = bh2; | |
429 | sbi->sb = (struct qnx6_super_block *)bh2->b_data; | |
430 | brelse(bh1); | |
431 | printk(KERN_INFO "qnx6: superblock #2 active\n"); | |
432 | } | |
433 | mmi_success: | |
434 | /* sanity check - limit maximum indirect pointer levels */ | |
435 | if (sb1->Inode.levels > QNX6_PTR_MAX_LEVELS) { | |
436 | printk(KERN_ERR "qnx6: too many inode levels (max %i, sb %i)\n", | |
437 | QNX6_PTR_MAX_LEVELS, sb1->Inode.levels); | |
438 | goto out; | |
439 | } | |
440 | if (sb1->Longfile.levels > QNX6_PTR_MAX_LEVELS) { | |
441 | printk(KERN_ERR "qnx6: too many longfilename levels" | |
442 | " (max %i, sb %i)\n", | |
443 | QNX6_PTR_MAX_LEVELS, sb1->Longfile.levels); | |
444 | goto out; | |
445 | } | |
446 | s->s_op = &qnx6_sops; | |
447 | s->s_magic = QNX6_SUPER_MAGIC; | |
448 | s->s_flags |= MS_RDONLY; /* Yup, read-only yet */ | |
449 | ||
450 | /* ease the later tree level calculations */ | |
451 | sbi = QNX6_SB(s); | |
452 | sbi->s_ptrbits = ilog2(s->s_blocksize / 4); | |
453 | sbi->inodes = qnx6_private_inode(s, &sb1->Inode); | |
454 | if (!sbi->inodes) | |
455 | goto out; | |
456 | sbi->longfile = qnx6_private_inode(s, &sb1->Longfile); | |
457 | if (!sbi->longfile) | |
458 | goto out1; | |
459 | ||
460 | /* prefetch root inode */ | |
461 | root = qnx6_iget(s, QNX6_ROOT_INO); | |
462 | if (IS_ERR(root)) { | |
463 | printk(KERN_ERR "qnx6: get inode failed\n"); | |
464 | ret = PTR_ERR(root); | |
465 | goto out2; | |
466 | } | |
467 | ||
468 | ret = -ENOMEM; | |
469 | s->s_root = d_make_root(root); | |
470 | if (!s->s_root) | |
471 | goto out2; | |
472 | ||
473 | ret = -EINVAL; | |
474 | errmsg = qnx6_checkroot(s); | |
475 | if (errmsg != NULL) { | |
476 | if (!silent) | |
477 | printk(KERN_ERR "qnx6: %s\n", errmsg); | |
478 | goto out3; | |
479 | } | |
480 | return 0; | |
481 | ||
482 | out3: | |
483 | dput(s->s_root); | |
484 | s->s_root = NULL; | |
485 | out2: | |
486 | iput(sbi->longfile); | |
487 | out1: | |
488 | iput(sbi->inodes); | |
489 | out: | |
490 | if (bh1) | |
491 | brelse(bh1); | |
492 | if (bh2) | |
493 | brelse(bh2); | |
494 | outnobh: | |
495 | kfree(qs); | |
496 | s->s_fs_info = NULL; | |
497 | return ret; | |
498 | } | |
499 | ||
500 | static void qnx6_put_super(struct super_block *sb) | |
501 | { | |
502 | struct qnx6_sb_info *qs = QNX6_SB(sb); | |
503 | brelse(qs->sb_buf); | |
504 | iput(qs->longfile); | |
505 | iput(qs->inodes); | |
506 | kfree(qs); | |
507 | sb->s_fs_info = NULL; | |
508 | return; | |
509 | } | |
510 | ||
511 | static sector_t qnx6_bmap(struct address_space *mapping, sector_t block) | |
512 | { | |
513 | return generic_block_bmap(mapping, block, qnx6_get_block); | |
514 | } | |
515 | static const struct address_space_operations qnx6_aops = { | |
516 | .readpage = qnx6_readpage, | |
517 | .readpages = qnx6_readpages, | |
518 | .bmap = qnx6_bmap | |
519 | }; | |
520 | ||
521 | static struct inode *qnx6_private_inode(struct super_block *s, | |
522 | struct qnx6_root_node *p) | |
523 | { | |
524 | struct inode *inode = new_inode(s); | |
525 | if (inode) { | |
526 | struct qnx6_inode_info *ei = QNX6_I(inode); | |
527 | struct qnx6_sb_info *sbi = QNX6_SB(s); | |
528 | inode->i_size = fs64_to_cpu(sbi, p->size); | |
529 | memcpy(ei->di_block_ptr, p->ptr, sizeof(p->ptr)); | |
530 | ei->di_filelevels = p->levels; | |
531 | inode->i_mode = S_IFREG | S_IRUSR; /* probably wrong */ | |
532 | inode->i_mapping->a_ops = &qnx6_aops; | |
533 | } | |
534 | return inode; | |
535 | } | |
536 | ||
537 | struct inode *qnx6_iget(struct super_block *sb, unsigned ino) | |
538 | { | |
539 | struct qnx6_sb_info *sbi = QNX6_SB(sb); | |
540 | struct qnx6_inode_entry *raw_inode; | |
541 | struct inode *inode; | |
542 | struct qnx6_inode_info *ei; | |
543 | struct address_space *mapping; | |
544 | struct page *page; | |
545 | u32 n, offs; | |
546 | ||
547 | inode = iget_locked(sb, ino); | |
548 | if (!inode) | |
549 | return ERR_PTR(-ENOMEM); | |
550 | if (!(inode->i_state & I_NEW)) | |
551 | return inode; | |
552 | ||
553 | ei = QNX6_I(inode); | |
554 | ||
555 | inode->i_mode = 0; | |
556 | ||
557 | if (ino == 0) { | |
558 | printk(KERN_ERR "qnx6: bad inode number on dev %s: %u is " | |
559 | "out of range\n", | |
560 | sb->s_id, ino); | |
561 | iget_failed(inode); | |
562 | return ERR_PTR(-EIO); | |
563 | } | |
564 | n = (ino - 1) >> (PAGE_CACHE_SHIFT - QNX6_INODE_SIZE_BITS); | |
565 | offs = (ino - 1) & (~PAGE_CACHE_MASK >> QNX6_INODE_SIZE_BITS); | |
566 | mapping = sbi->inodes->i_mapping; | |
567 | page = read_mapping_page(mapping, n, NULL); | |
568 | if (IS_ERR(page)) { | |
569 | printk(KERN_ERR "qnx6: major problem: unable to read inode from " | |
570 | "dev %s\n", sb->s_id); | |
571 | iget_failed(inode); | |
572 | return ERR_CAST(page); | |
573 | } | |
574 | kmap(page); | |
575 | raw_inode = ((struct qnx6_inode_entry *)page_address(page)) + offs; | |
576 | ||
577 | inode->i_mode = fs16_to_cpu(sbi, raw_inode->di_mode); | |
85a03d1b EB |
578 | i_uid_write(inode, (uid_t)fs32_to_cpu(sbi, raw_inode->di_uid)); |
579 | i_gid_write(inode, (gid_t)fs32_to_cpu(sbi, raw_inode->di_gid)); | |
5d026c72 KB |
580 | inode->i_size = fs64_to_cpu(sbi, raw_inode->di_size); |
581 | inode->i_mtime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_mtime); | |
582 | inode->i_mtime.tv_nsec = 0; | |
583 | inode->i_atime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_atime); | |
584 | inode->i_atime.tv_nsec = 0; | |
585 | inode->i_ctime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_ctime); | |
586 | inode->i_ctime.tv_nsec = 0; | |
587 | ||
588 | /* calc blocks based on 512 byte blocksize */ | |
589 | inode->i_blocks = (inode->i_size + 511) >> 9; | |
590 | ||
591 | memcpy(&ei->di_block_ptr, &raw_inode->di_block_ptr, | |
592 | sizeof(raw_inode->di_block_ptr)); | |
593 | ei->di_filelevels = raw_inode->di_filelevels; | |
594 | ||
595 | if (S_ISREG(inode->i_mode)) { | |
596 | inode->i_fop = &generic_ro_fops; | |
597 | inode->i_mapping->a_ops = &qnx6_aops; | |
598 | } else if (S_ISDIR(inode->i_mode)) { | |
599 | inode->i_op = &qnx6_dir_inode_operations; | |
600 | inode->i_fop = &qnx6_dir_operations; | |
601 | inode->i_mapping->a_ops = &qnx6_aops; | |
602 | } else if (S_ISLNK(inode->i_mode)) { | |
603 | inode->i_op = &page_symlink_inode_operations; | |
604 | inode->i_mapping->a_ops = &qnx6_aops; | |
605 | } else | |
606 | init_special_inode(inode, inode->i_mode, 0); | |
607 | qnx6_put_page(page); | |
608 | unlock_new_inode(inode); | |
609 | return inode; | |
610 | } | |
611 | ||
612 | static struct kmem_cache *qnx6_inode_cachep; | |
613 | ||
614 | static struct inode *qnx6_alloc_inode(struct super_block *sb) | |
615 | { | |
616 | struct qnx6_inode_info *ei; | |
617 | ei = kmem_cache_alloc(qnx6_inode_cachep, GFP_KERNEL); | |
618 | if (!ei) | |
619 | return NULL; | |
620 | return &ei->vfs_inode; | |
621 | } | |
622 | ||
623 | static void qnx6_i_callback(struct rcu_head *head) | |
624 | { | |
625 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
5d026c72 KB |
626 | kmem_cache_free(qnx6_inode_cachep, QNX6_I(inode)); |
627 | } | |
628 | ||
629 | static void qnx6_destroy_inode(struct inode *inode) | |
630 | { | |
631 | call_rcu(&inode->i_rcu, qnx6_i_callback); | |
632 | } | |
633 | ||
634 | static void init_once(void *foo) | |
635 | { | |
636 | struct qnx6_inode_info *ei = (struct qnx6_inode_info *) foo; | |
637 | ||
638 | inode_init_once(&ei->vfs_inode); | |
639 | } | |
640 | ||
641 | static int init_inodecache(void) | |
642 | { | |
643 | qnx6_inode_cachep = kmem_cache_create("qnx6_inode_cache", | |
644 | sizeof(struct qnx6_inode_info), | |
645 | 0, (SLAB_RECLAIM_ACCOUNT| | |
646 | SLAB_MEM_SPREAD), | |
647 | init_once); | |
648 | if (!qnx6_inode_cachep) | |
649 | return -ENOMEM; | |
650 | return 0; | |
651 | } | |
652 | ||
653 | static void destroy_inodecache(void) | |
654 | { | |
8c0a8537 KS |
655 | /* |
656 | * Make sure all delayed rcu free inodes are flushed before we | |
657 | * destroy cache. | |
658 | */ | |
659 | rcu_barrier(); | |
5d026c72 KB |
660 | kmem_cache_destroy(qnx6_inode_cachep); |
661 | } | |
662 | ||
663 | static struct dentry *qnx6_mount(struct file_system_type *fs_type, | |
664 | int flags, const char *dev_name, void *data) | |
665 | { | |
666 | return mount_bdev(fs_type, flags, dev_name, data, qnx6_fill_super); | |
667 | } | |
668 | ||
669 | static struct file_system_type qnx6_fs_type = { | |
670 | .owner = THIS_MODULE, | |
671 | .name = "qnx6", | |
672 | .mount = qnx6_mount, | |
673 | .kill_sb = kill_block_super, | |
674 | .fs_flags = FS_REQUIRES_DEV, | |
675 | }; | |
7f78e035 | 676 | MODULE_ALIAS_FS("qnx6"); |
5d026c72 KB |
677 | |
678 | static int __init init_qnx6_fs(void) | |
679 | { | |
680 | int err; | |
681 | ||
682 | err = init_inodecache(); | |
683 | if (err) | |
684 | return err; | |
685 | ||
686 | err = register_filesystem(&qnx6_fs_type); | |
687 | if (err) { | |
688 | destroy_inodecache(); | |
689 | return err; | |
690 | } | |
691 | ||
692 | printk(KERN_INFO "QNX6 filesystem 1.0.0 registered.\n"); | |
693 | return 0; | |
694 | } | |
695 | ||
696 | static void __exit exit_qnx6_fs(void) | |
697 | { | |
698 | unregister_filesystem(&qnx6_fs_type); | |
699 | destroy_inodecache(); | |
700 | } | |
701 | ||
702 | module_init(init_qnx6_fs) | |
703 | module_exit(exit_qnx6_fs) | |
704 | MODULE_LICENSE("GPL"); |