original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

703 lines
18 KiB

fs: Limit sys_mount to only request filesystem modules. Modify the request_module to prefix the file system type with "fs-" and add aliases to all of the filesystems that can be built as modules to match. A common practice is to build all of the kernel code and leave code that is not commonly needed as modules, with the result that many users are exposed to any bug anywhere in the kernel. Looking for filesystems with a fs- prefix limits the pool of possible modules that can be loaded by mount to just filesystems trivially making things safer with no real cost. Using aliases means user space can control the policy of which filesystem modules are auto-loaded by editing /etc/modprobe.d/*.conf with blacklist and alias directives. Allowing simple, safe, well understood work-arounds to known problematic software. This also addresses a rare but unfortunate problem where the filesystem name is not the same as it's module name and module auto-loading would not work. While writing this patch I saw a handful of such cases. The most significant being autofs that lives in the module autofs4. This is relevant to user namespaces because we can reach the request module in get_fs_type() without having any special permissions, and people get uncomfortable when a user specified string (in this case the filesystem type) goes all of the way to request_module. After having looked at this issue I don't think there is any particular reason to perform any filtering or permission checks beyond making it clear in the module request that we want a filesystem module. The common pattern in the kernel is to call request_module() without regards to the users permissions. In general all a filesystem module does once loaded is call register_filesystem() and go to sleep. Which means there is not much attack surface exposed by loading a filesytem module unless the filesystem is mounted. In a user namespace filesystems are not mounted unless .fs_flags = FS_USERNS_MOUNT, which most filesystems do not set today. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Acked-by: Kees Cook <keescook@chromium.org> Reported-by: Kees Cook <keescook@google.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
9 years ago
  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. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/highuid.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/buffer_head.h>
  18. #include <linux/writeback.h>
  19. #include <linux/statfs.h>
  20. #include <linux/parser.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/mount.h>
  23. #include <linux/crc32.h>
  24. #include <linux/mpage.h>
  25. #include "qnx6.h"
  26. static const struct super_operations qnx6_sops;
  27. static void qnx6_put_super(struct super_block *sb);
  28. static struct inode *qnx6_alloc_inode(struct super_block *sb);
  29. static void qnx6_destroy_inode(struct inode *inode);
  30. static int qnx6_remount(struct super_block *sb, int *flags, char *data);
  31. static int qnx6_statfs(struct dentry *dentry, struct kstatfs *buf);
  32. static int qnx6_show_options(struct seq_file *seq, struct dentry *root);
  33. static const struct super_operations qnx6_sops = {
  34. .alloc_inode = qnx6_alloc_inode,
  35. .destroy_inode = qnx6_destroy_inode,
  36. .put_super = qnx6_put_super,
  37. .statfs = qnx6_statfs,
  38. .remount_fs = qnx6_remount,
  39. .show_options = qnx6_show_options,
  40. };
  41. static int qnx6_show_options(struct seq_file *seq, struct dentry *root)
  42. {
  43. struct super_block *sb = root->d_sb;
  44. struct qnx6_sb_info *sbi = QNX6_SB(sb);
  45. if (sbi->s_mount_opt & QNX6_MOUNT_MMI_FS)
  46. seq_puts(seq, ",mmi_fs");
  47. return 0;
  48. }
  49. static int qnx6_remount(struct super_block *sb, int *flags, char *data)
  50. {
  51. *flags |= MS_RDONLY;
  52. return 0;
  53. }
  54. static unsigned qnx6_get_devblock(struct super_block *sb, __fs32 block)
  55. {
  56. struct qnx6_sb_info *sbi = QNX6_SB(sb);
  57. return fs32_to_cpu(sbi, block) + sbi->s_blks_off;
  58. }
  59. static unsigned qnx6_block_map(struct inode *inode, unsigned iblock);
  60. static int qnx6_get_block(struct inode *inode, sector_t iblock,
  61. struct buffer_head *bh, int create)
  62. {
  63. unsigned phys;
  64. QNX6DEBUG((KERN_INFO "qnx6: qnx6_get_block inode=[%ld] iblock=[%ld]\n",
  65. inode->i_ino, (unsigned long)iblock));
  66. phys = qnx6_block_map(inode, iblock);
  67. if (phys) {
  68. /* logical block is before EOF */
  69. map_bh(bh, inode->i_sb, phys);
  70. }
  71. return 0;
  72. }
  73. static int qnx6_check_blockptr(__fs32 ptr)
  74. {
  75. if (ptr == ~(__fs32)0) {
  76. printk(KERN_ERR "qnx6: hit unused blockpointer.\n");
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. static int qnx6_readpage(struct file *file, struct page *page)
  82. {
  83. return mpage_readpage(page, qnx6_get_block);
  84. }
  85. static int qnx6_readpages(struct file *file, struct address_space *mapping,
  86. struct list_head *pages, unsigned nr_pages)
  87. {
  88. return mpage_readpages(mapping, pages, nr_pages, qnx6_get_block);
  89. }
  90. /*
  91. * returns the block number for the no-th element in the tree
  92. * inodebits requred as there are multiple inodes in one inode block
  93. */
  94. static unsigned qnx6_block_map(struct inode *inode, unsigned no)
  95. {
  96. struct super_block *s = inode->i_sb;
  97. struct qnx6_sb_info *sbi = QNX6_SB(s);
  98. struct qnx6_inode_info *ei = QNX6_I(inode);
  99. unsigned block = 0;
  100. struct buffer_head *bh;
  101. __fs32 ptr;
  102. int levelptr;
  103. int ptrbits = sbi->s_ptrbits;
  104. int bitdelta;
  105. u32 mask = (1 << ptrbits) - 1;
  106. int depth = ei->di_filelevels;
  107. int i;
  108. bitdelta = ptrbits * depth;
  109. levelptr = no >> bitdelta;
  110. if (levelptr > QNX6_NO_DIRECT_POINTERS - 1) {
  111. printk(KERN_ERR "qnx6:Requested file block number (%u) too big.",
  112. no);
  113. return 0;
  114. }
  115. block = qnx6_get_devblock(s, ei->di_block_ptr[levelptr]);
  116. for (i = 0; i < depth; i++) {
  117. bh = sb_bread(s, block);
  118. if (!bh) {
  119. printk(KERN_ERR "qnx6:Error reading block (%u)\n",
  120. block);
  121. return 0;
  122. }
  123. bitdelta -= ptrbits;
  124. levelptr = (no >> bitdelta) & mask;
  125. ptr = ((__fs32 *)bh->b_data)[levelptr];
  126. if (!qnx6_check_blockptr(ptr))
  127. return 0;
  128. block = qnx6_get_devblock(s, ptr);
  129. brelse(bh);
  130. }
  131. return block;
  132. }
  133. static int qnx6_statfs(struct dentry *dentry, struct kstatfs *buf)
  134. {
  135. struct super_block *sb = dentry->d_sb;
  136. struct qnx6_sb_info *sbi = QNX6_SB(sb);
  137. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  138. buf->f_type = sb->s_magic;
  139. buf->f_bsize = sb->s_blocksize;
  140. buf->f_blocks = fs32_to_cpu(sbi, sbi->sb->sb_num_blocks);
  141. buf->f_bfree = fs32_to_cpu(sbi, sbi->sb->sb_free_blocks);
  142. buf->f_files = fs32_to_cpu(sbi, sbi->sb->sb_num_inodes);
  143. buf->f_ffree = fs32_to_cpu(sbi, sbi->sb->sb_free_inodes);
  144. buf->f_bavail = buf->f_bfree;
  145. buf->f_namelen = QNX6_LONG_NAME_MAX;
  146. buf->f_fsid.val[0] = (u32)id;
  147. buf->f_fsid.val[1] = (u32)(id >> 32);
  148. return 0;
  149. }
  150. /*
  151. * Check the root directory of the filesystem to make sure
  152. * it really _is_ a qnx6 filesystem, and to check the size
  153. * of the directory entry.
  154. */
  155. static const char *qnx6_checkroot(struct super_block *s)
  156. {
  157. static char match_root[2][3] = {".\0\0", "..\0"};
  158. int i, error = 0;
  159. struct qnx6_dir_entry *dir_entry;
  160. struct inode *root = s->s_root->d_inode;
  161. struct address_space *mapping = root->i_mapping;
  162. struct page *page = read_mapping_page(mapping, 0, NULL);
  163. if (IS_ERR(page))
  164. return "error reading root directory";
  165. kmap(page);
  166. dir_entry = page_address(page);
  167. for (i = 0; i < 2; i++) {
  168. /* maximum 3 bytes - due to match_root limitation */
  169. if (strncmp(dir_entry[i].de_fname, match_root[i], 3))
  170. error = 1;
  171. }
  172. qnx6_put_page(page);
  173. if (error)
  174. return "error reading root directory.";
  175. return NULL;
  176. }
  177. #ifdef CONFIG_QNX6FS_DEBUG
  178. void qnx6_superblock_debug(struct qnx6_super_block *sb, struct super_block *s)
  179. {
  180. struct qnx6_sb_info *sbi = QNX6_SB(s);
  181. QNX6DEBUG((KERN_INFO "magic: %08x\n",
  182. fs32_to_cpu(sbi, sb->sb_magic)));
  183. QNX6DEBUG((KERN_INFO "checksum: %08x\n",
  184. fs32_to_cpu(sbi, sb->sb_checksum)));
  185. QNX6DEBUG((KERN_INFO "serial: %llx\n",
  186. fs64_to_cpu(sbi, sb->sb_serial)));
  187. QNX6DEBUG((KERN_INFO "flags: %08x\n",
  188. fs32_to_cpu(sbi, sb->sb_flags)));
  189. QNX6DEBUG((KERN_INFO "blocksize: %08x\n",
  190. fs32_to_cpu(sbi, sb->sb_blocksize)));
  191. QNX6DEBUG((KERN_INFO "num_inodes: %08x\n",
  192. fs32_to_cpu(sbi, sb->sb_num_inodes)));
  193. QNX6DEBUG((KERN_INFO "free_inodes: %08x\n",
  194. fs32_to_cpu(sbi, sb->sb_free_inodes)));
  195. QNX6DEBUG((KERN_INFO "num_blocks: %08x\n",
  196. fs32_to_cpu(sbi, sb->sb_num_blocks)));
  197. QNX6DEBUG((KERN_INFO "free_blocks: %08x\n",
  198. fs32_to_cpu(sbi, sb->sb_free_blocks)));
  199. QNX6DEBUG((KERN_INFO "inode_levels: %02x\n",
  200. sb->Inode.levels));
  201. }
  202. #endif
  203. enum {
  204. Opt_mmifs,
  205. Opt_err
  206. };
  207. static const match_table_t tokens = {
  208. {Opt_mmifs, "mmi_fs"},
  209. {Opt_err, NULL}
  210. };
  211. static int qnx6_parse_options(char *options, struct super_block *sb)
  212. {
  213. char *p;
  214. struct qnx6_sb_info *sbi = QNX6_SB(sb);
  215. substring_t args[MAX_OPT_ARGS];
  216. if (!options)
  217. return 1;
  218. while ((p = strsep(&options, ",")) != NULL) {
  219. int token;
  220. if (!*p)
  221. continue;
  222. token = match_token(p, tokens, args);
  223. switch (token) {
  224. case Opt_mmifs:
  225. set_opt(sbi->s_mount_opt, MMI_FS);
  226. break;
  227. default:
  228. return 0;
  229. }
  230. }
  231. return 1;
  232. }
  233. static struct buffer_head *qnx6_check_first_superblock(struct super_block *s,
  234. int offset, int silent)
  235. {
  236. struct qnx6_sb_info *sbi = QNX6_SB(s);
  237. struct buffer_head *bh;
  238. struct qnx6_super_block *sb;
  239. /* Check the superblock signatures
  240. start with the first superblock */
  241. bh = sb_bread(s, offset);
  242. if (!bh) {
  243. printk(KERN_ERR "qnx6: unable to read the first superblock\n");
  244. return NULL;
  245. }
  246. sb = (struct qnx6_super_block *)bh->b_data;
  247. if (fs32_to_cpu(sbi, sb->sb_magic) != QNX6_SUPER_MAGIC) {
  248. sbi->s_bytesex = BYTESEX_BE;
  249. if (fs32_to_cpu(sbi, sb->sb_magic) == QNX6_SUPER_MAGIC) {
  250. /* we got a big endian fs */
  251. QNX6DEBUG((KERN_INFO "qnx6: fs got different"
  252. " endianness.\n"));
  253. return bh;
  254. } else
  255. sbi->s_bytesex = BYTESEX_LE;
  256. if (!silent) {
  257. if (offset == 0) {
  258. printk(KERN_ERR "qnx6: wrong signature (magic)"
  259. " in superblock #1.\n");
  260. } else {
  261. printk(KERN_INFO "qnx6: wrong signature (magic)"
  262. " at position (0x%lx) - will try"
  263. " alternative position (0x0000).\n",
  264. offset * s->s_blocksize);
  265. }
  266. }
  267. brelse(bh);
  268. return NULL;
  269. }
  270. return bh;
  271. }
  272. static struct inode *qnx6_private_inode(struct super_block *s,
  273. struct qnx6_root_node *p);
  274. static int qnx6_fill_super(struct super_block *s, void *data, int silent)
  275. {
  276. struct buffer_head *bh1 = NULL, *bh2 = NULL;
  277. struct qnx6_super_block *sb1 = NULL, *sb2 = NULL;
  278. struct qnx6_sb_info *sbi;
  279. struct inode *root;
  280. const char *errmsg;
  281. struct qnx6_sb_info *qs;
  282. int ret = -EINVAL;
  283. u64 offset;
  284. int bootblock_offset = QNX6_BOOTBLOCK_SIZE;
  285. qs = kzalloc(sizeof(struct qnx6_sb_info), GFP_KERNEL);
  286. if (!qs)
  287. return -ENOMEM;
  288. s->s_fs_info = qs;
  289. /* Superblock always is 512 Byte long */
  290. if (!sb_set_blocksize(s, QNX6_SUPERBLOCK_SIZE)) {
  291. printk(KERN_ERR "qnx6: unable to set blocksize\n");
  292. goto outnobh;
  293. }
  294. /* parse the mount-options */
  295. if (!qnx6_parse_options((char *) data, s)) {
  296. printk(KERN_ERR "qnx6: invalid mount options.\n");
  297. goto outnobh;
  298. }
  299. if (test_opt(s, MMI_FS)) {
  300. sb1 = qnx6_mmi_fill_super(s, silent);
  301. if (sb1)
  302. goto mmi_success;
  303. else
  304. goto outnobh;
  305. }
  306. sbi = QNX6_SB(s);
  307. sbi->s_bytesex = BYTESEX_LE;
  308. /* Check the superblock signatures
  309. start with the first superblock */
  310. bh1 = qnx6_check_first_superblock(s,
  311. bootblock_offset / QNX6_SUPERBLOCK_SIZE, silent);
  312. if (!bh1) {
  313. /* try again without bootblock offset */
  314. bh1 = qnx6_check_first_superblock(s, 0, silent);
  315. if (!bh1) {
  316. printk(KERN_ERR "qnx6: unable to read the first superblock\n");
  317. goto outnobh;
  318. }
  319. /* seems that no bootblock at partition start */
  320. bootblock_offset = 0;
  321. }
  322. sb1 = (struct qnx6_super_block *)bh1->b_data;
  323. #ifdef CONFIG_QNX6FS_DEBUG
  324. qnx6_superblock_debug(sb1, s);
  325. #endif
  326. /* checksum check - start at byte 8 and end at byte 512 */
  327. if (fs32_to_cpu(sbi, sb1->sb_checksum) !=
  328. crc32_be(0, (char *)(bh1->b_data + 8), 504)) {
  329. printk(KERN_ERR "qnx6: superblock #1 checksum error\n");
  330. goto out;
  331. }
  332. /* set new blocksize */
  333. if (!sb_set_blocksize(s, fs32_to_cpu(sbi, sb1->sb_blocksize))) {
  334. printk(KERN_ERR "qnx6: unable to set blocksize\n");
  335. goto out;
  336. }
  337. /* blocksize invalidates bh - pull it back in */
  338. brelse(bh1);
  339. bh1 = sb_bread(s, bootblock_offset >> s->s_blocksize_bits);
  340. if (!bh1)
  341. goto outnobh;
  342. sb1 = (struct qnx6_super_block *)bh1->b_data;
  343. /* calculate second superblock blocknumber */
  344. offset = fs32_to_cpu(sbi, sb1->sb_num_blocks) +
  345. (bootblock_offset >> s->s_blocksize_bits) +
  346. (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits);
  347. /* set bootblock offset */
  348. sbi->s_blks_off = (bootblock_offset >> s->s_blocksize_bits) +
  349. (QNX6_SUPERBLOCK_AREA >> s->s_blocksize_bits);
  350. /* next the second superblock */
  351. bh2 = sb_bread(s, offset);
  352. if (!bh2) {
  353. printk(KERN_ERR "qnx6: unable to read the second superblock\n");
  354. goto out;
  355. }
  356. sb2 = (struct qnx6_super_block *)bh2->b_data;
  357. if (fs32_to_cpu(sbi, sb2->sb_magic) != QNX6_SUPER_MAGIC) {
  358. if (!silent)
  359. printk(KERN_ERR "qnx6: wrong signature (magic)"
  360. " in superblock #2.\n");
  361. goto out;
  362. }
  363. /* checksum check - start at byte 8 and end at byte 512 */
  364. if (fs32_to_cpu(sbi, sb2->sb_checksum) !=
  365. crc32_be(0, (char *)(bh2->b_data + 8), 504)) {
  366. printk(KERN_ERR "qnx6: superblock #2 checksum error\n");
  367. goto out;
  368. }
  369. if (fs64_to_cpu(sbi, sb1->sb_serial) >=
  370. fs64_to_cpu(sbi, sb2->sb_serial)) {
  371. /* superblock #1 active */
  372. sbi->sb_buf = bh1;
  373. sbi->sb = (struct qnx6_super_block *)bh1->b_data;
  374. brelse(bh2);
  375. printk(KERN_INFO "qnx6: superblock #1 active\n");
  376. } else {
  377. /* superblock #2 active */
  378. sbi->sb_buf = bh2;
  379. sbi->sb = (struct qnx6_super_block *)bh2->b_data;
  380. brelse(bh1);
  381. printk(KERN_INFO "qnx6: superblock #2 active\n");
  382. }
  383. mmi_success:
  384. /* sanity check - limit maximum indirect pointer levels */
  385. if (sb1->Inode.levels > QNX6_PTR_MAX_LEVELS) {
  386. printk(KERN_ERR "qnx6: too many inode levels (max %i, sb %i)\n",
  387. QNX6_PTR_MAX_LEVELS, sb1->Inode.levels);
  388. goto out;
  389. }
  390. if (sb1->Longfile.levels > QNX6_PTR_MAX_LEVELS) {
  391. printk(KERN_ERR "qnx6: too many longfilename levels"
  392. " (max %i, sb %i)\n",
  393. QNX6_PTR_MAX_LEVELS, sb1->Longfile.levels);
  394. goto out;
  395. }
  396. s->s_op = &qnx6_sops;
  397. s->s_magic = QNX6_SUPER_MAGIC;
  398. s->s_flags |= MS_RDONLY; /* Yup, read-only yet */
  399. /* ease the later tree level calculations */
  400. sbi = QNX6_SB(s);
  401. sbi->s_ptrbits = ilog2(s->s_blocksize / 4);
  402. sbi->inodes = qnx6_private_inode(s, &sb1->Inode);
  403. if (!sbi->inodes)
  404. goto out;
  405. sbi->longfile = qnx6_private_inode(s, &sb1->Longfile);
  406. if (!sbi->longfile)
  407. goto out1;
  408. /* prefetch root inode */
  409. root = qnx6_iget(s, QNX6_ROOT_INO);
  410. if (IS_ERR(root)) {
  411. printk(KERN_ERR "qnx6: get inode failed\n");
  412. ret = PTR_ERR(root);
  413. goto out2;
  414. }
  415. ret = -ENOMEM;
  416. s->s_root = d_make_root(root);
  417. if (!s->s_root)
  418. goto out2;
  419. ret = -EINVAL;
  420. errmsg = qnx6_checkroot(s);
  421. if (errmsg != NULL) {
  422. if (!silent)
  423. printk(KERN_ERR "qnx6: %s\n", errmsg);
  424. goto out3;
  425. }
  426. return 0;
  427. out3:
  428. dput(s->s_root);
  429. s->s_root = NULL;
  430. out2:
  431. iput(sbi->longfile);
  432. out1:
  433. iput(sbi->inodes);
  434. out:
  435. if (bh1)
  436. brelse(bh1);
  437. if (bh2)
  438. brelse(bh2);
  439. outnobh:
  440. kfree(qs);
  441. s->s_fs_info = NULL;
  442. return ret;
  443. }
  444. static void qnx6_put_super(struct super_block *sb)
  445. {
  446. struct qnx6_sb_info *qs = QNX6_SB(sb);
  447. brelse(qs->sb_buf);
  448. iput(qs->longfile);
  449. iput(qs->inodes);
  450. kfree(qs);
  451. sb->s_fs_info = NULL;
  452. return;
  453. }
  454. static sector_t qnx6_bmap(struct address_space *mapping, sector_t block)
  455. {
  456. return generic_block_bmap(mapping, block, qnx6_get_block);
  457. }
  458. static const struct address_space_operations qnx6_aops = {
  459. .readpage = qnx6_readpage,
  460. .readpages = qnx6_readpages,
  461. .bmap = qnx6_bmap
  462. };
  463. static struct inode *qnx6_private_inode(struct super_block *s,
  464. struct qnx6_root_node *p)
  465. {
  466. struct inode *inode = new_inode(s);
  467. if (inode) {
  468. struct qnx6_inode_info *ei = QNX6_I(inode);
  469. struct qnx6_sb_info *sbi = QNX6_SB(s);
  470. inode->i_size = fs64_to_cpu(sbi, p->size);
  471. memcpy(ei->di_block_ptr, p->ptr, sizeof(p->ptr));
  472. ei->di_filelevels = p->levels;
  473. inode->i_mode = S_IFREG | S_IRUSR; /* probably wrong */
  474. inode->i_mapping->a_ops = &qnx6_aops;
  475. }
  476. return inode;
  477. }
  478. struct inode *qnx6_iget(struct super_block *sb, unsigned ino)
  479. {
  480. struct qnx6_sb_info *sbi = QNX6_SB(sb);
  481. struct qnx6_inode_entry *raw_inode;
  482. struct inode *inode;
  483. struct qnx6_inode_info *ei;
  484. struct address_space *mapping;
  485. struct page *page;
  486. u32 n, offs;
  487. inode = iget_locked(sb, ino);
  488. if (!inode)
  489. return ERR_PTR(-ENOMEM);
  490. if (!(inode->i_state & I_NEW))
  491. return inode;
  492. ei = QNX6_I(inode);
  493. inode->i_mode = 0;
  494. if (ino == 0) {
  495. printk(KERN_ERR "qnx6: bad inode number on dev %s: %u is "
  496. "out of range\n",
  497. sb->s_id, ino);
  498. iget_failed(inode);
  499. return ERR_PTR(-EIO);
  500. }
  501. n = (ino - 1) >> (PAGE_CACHE_SHIFT - QNX6_INODE_SIZE_BITS);
  502. offs = (ino - 1) & (~PAGE_CACHE_MASK >> QNX6_INODE_SIZE_BITS);
  503. mapping = sbi->inodes->i_mapping;
  504. page = read_mapping_page(mapping, n, NULL);
  505. if (IS_ERR(page)) {
  506. printk(KERN_ERR "qnx6: major problem: unable to read inode from "
  507. "dev %s\n", sb->s_id);
  508. iget_failed(inode);
  509. return ERR_CAST(page);
  510. }
  511. kmap(page);
  512. raw_inode = ((struct qnx6_inode_entry *)page_address(page)) + offs;
  513. inode->i_mode = fs16_to_cpu(sbi, raw_inode->di_mode);
  514. i_uid_write(inode, (uid_t)fs32_to_cpu(sbi, raw_inode->di_uid));
  515. i_gid_write(inode, (gid_t)fs32_to_cpu(sbi, raw_inode->di_gid));
  516. inode->i_size = fs64_to_cpu(sbi, raw_inode->di_size);
  517. inode->i_mtime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_mtime);
  518. inode->i_mtime.tv_nsec = 0;
  519. inode->i_atime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_atime);
  520. inode->i_atime.tv_nsec = 0;
  521. inode->i_ctime.tv_sec = fs32_to_cpu(sbi, raw_inode->di_ctime);
  522. inode->i_ctime.tv_nsec = 0;
  523. /* calc blocks based on 512 byte blocksize */
  524. inode->i_blocks = (inode->i_size + 511) >> 9;
  525. memcpy(&ei->di_block_ptr, &raw_inode->di_block_ptr,
  526. sizeof(raw_inode->di_block_ptr));
  527. ei->di_filelevels = raw_inode->di_filelevels;
  528. if (S_ISREG(inode->i_mode)) {
  529. inode->i_fop = &generic_ro_fops;
  530. inode->i_mapping->a_ops = &qnx6_aops;
  531. } else if (S_ISDIR(inode->i_mode)) {
  532. inode->i_op = &qnx6_dir_inode_operations;
  533. inode->i_fop = &qnx6_dir_operations;
  534. inode->i_mapping->a_ops = &qnx6_aops;
  535. } else if (S_ISLNK(inode->i_mode)) {
  536. inode->i_op = &page_symlink_inode_operations;
  537. inode->i_mapping->a_ops = &qnx6_aops;
  538. } else
  539. init_special_inode(inode, inode->i_mode, 0);
  540. qnx6_put_page(page);
  541. unlock_new_inode(inode);
  542. return inode;
  543. }
  544. static struct kmem_cache *qnx6_inode_cachep;
  545. static struct inode *qnx6_alloc_inode(struct super_block *sb)
  546. {
  547. struct qnx6_inode_info *ei;
  548. ei = kmem_cache_alloc(qnx6_inode_cachep, GFP_KERNEL);
  549. if (!ei)
  550. return NULL;
  551. return &ei->vfs_inode;
  552. }
  553. static void qnx6_i_callback(struct rcu_head *head)
  554. {
  555. struct inode *inode = container_of(head, struct inode, i_rcu);
  556. kmem_cache_free(qnx6_inode_cachep, QNX6_I(inode));
  557. }
  558. static void qnx6_destroy_inode(struct inode *inode)
  559. {
  560. call_rcu(&inode->i_rcu, qnx6_i_callback);
  561. }
  562. static void init_once(void *foo)
  563. {
  564. struct qnx6_inode_info *ei = (struct qnx6_inode_info *) foo;
  565. inode_init_once(&ei->vfs_inode);
  566. }
  567. static int init_inodecache(void)
  568. {
  569. qnx6_inode_cachep = kmem_cache_create("qnx6_inode_cache",
  570. sizeof(struct qnx6_inode_info),
  571. 0, (SLAB_RECLAIM_ACCOUNT|
  572. SLAB_MEM_SPREAD),
  573. init_once);
  574. if (!qnx6_inode_cachep)
  575. return -ENOMEM;
  576. return 0;
  577. }
  578. static void destroy_inodecache(void)
  579. {
  580. /*
  581. * Make sure all delayed rcu free inodes are flushed before we
  582. * destroy cache.
  583. */
  584. rcu_barrier();
  585. kmem_cache_destroy(qnx6_inode_cachep);
  586. }
  587. static struct dentry *qnx6_mount(struct file_system_type *fs_type,
  588. int flags, const char *dev_name, void *data)
  589. {
  590. return mount_bdev(fs_type, flags, dev_name, data, qnx6_fill_super);
  591. }
  592. static struct file_system_type qnx6_fs_type = {
  593. .owner = THIS_MODULE,
  594. .name = "qnx6",
  595. .mount = qnx6_mount,
  596. .kill_sb = kill_block_super,
  597. .fs_flags = FS_REQUIRES_DEV,
  598. };
  599. MODULE_ALIAS_FS("qnx6");
  600. static int __init init_qnx6_fs(void)
  601. {
  602. int err;
  603. err = init_inodecache();
  604. if (err)
  605. return err;
  606. err = register_filesystem(&qnx6_fs_type);
  607. if (err) {
  608. destroy_inodecache();
  609. return err;
  610. }
  611. printk(KERN_INFO "QNX6 filesystem 1.0.0 registered.\n");
  612. return 0;
  613. }
  614. static void __exit exit_qnx6_fs(void)
  615. {
  616. unregister_filesystem(&qnx6_fs_type);
  617. destroy_inodecache();
  618. }
  619. module_init(init_qnx6_fs)
  620. module_exit(exit_qnx6_fs)
  621. MODULE_LICENSE("GPL");