inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QNX4 file system, Linux implementation.
  4. *
  5. * Version : 0.2.1
  6. *
  7. * Using parts of the xiafs filesystem.
  8. *
  9. * History :
  10. *
  11. * 01-06-1998 by Richard Frowijn : first release.
  12. * 20-06-1998 by Frank Denis : Linux 2.1.99+ support, boot signature, misc.
  13. * 30-06-1998 by Frank Denis : first step to write inodes.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/highuid.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/writeback.h>
  22. #include <linux/statfs.h>
  23. #include <linux/fs_context.h>
  24. #include "qnx4.h"
  25. #define QNX4_VERSION 4
  26. #define QNX4_BMNAME ".bitmap"
  27. static const struct super_operations qnx4_sops;
  28. static struct inode *qnx4_alloc_inode(struct super_block *sb);
  29. static void qnx4_free_inode(struct inode *inode);
  30. static int qnx4_statfs(struct dentry *, struct kstatfs *);
  31. static int qnx4_get_tree(struct fs_context *fc);
  32. static const struct super_operations qnx4_sops =
  33. {
  34. .alloc_inode = qnx4_alloc_inode,
  35. .free_inode = qnx4_free_inode,
  36. .statfs = qnx4_statfs,
  37. };
  38. static int qnx4_reconfigure(struct fs_context *fc)
  39. {
  40. struct super_block *sb = fc->root->d_sb;
  41. struct qnx4_sb_info *qs;
  42. sync_filesystem(sb);
  43. qs = qnx4_sb(sb);
  44. qs->Version = QNX4_VERSION;
  45. fc->sb_flags |= SB_RDONLY;
  46. return 0;
  47. }
  48. static const struct fs_context_operations qnx4_context_opts = {
  49. .get_tree = qnx4_get_tree,
  50. .reconfigure = qnx4_reconfigure,
  51. };
  52. static int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
  53. {
  54. unsigned long phys;
  55. QNX4DEBUG((KERN_INFO "qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
  56. phys = qnx4_block_map( inode, iblock );
  57. if ( phys ) {
  58. // logical block is before EOF
  59. map_bh(bh, inode->i_sb, phys);
  60. }
  61. return 0;
  62. }
  63. static inline u32 try_extent(qnx4_xtnt_t *extent, u32 *offset)
  64. {
  65. u32 size = le32_to_cpu(extent->xtnt_size);
  66. if (*offset < size)
  67. return le32_to_cpu(extent->xtnt_blk) + *offset - 1;
  68. *offset -= size;
  69. return 0;
  70. }
  71. unsigned long qnx4_block_map( struct inode *inode, long iblock )
  72. {
  73. int ix;
  74. long i_xblk;
  75. struct buffer_head *bh = NULL;
  76. struct qnx4_xblk *xblk = NULL;
  77. struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
  78. u16 nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
  79. u32 offset = iblock;
  80. u32 block = try_extent(&qnx4_inode->di_first_xtnt, &offset);
  81. if (block) {
  82. // iblock is in the first extent. This is easy.
  83. } else {
  84. // iblock is beyond first extent. We have to follow the extent chain.
  85. i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
  86. ix = 0;
  87. while ( --nxtnt > 0 ) {
  88. if ( ix == 0 ) {
  89. // read next xtnt block.
  90. bh = sb_bread(inode->i_sb, i_xblk - 1);
  91. if ( !bh ) {
  92. QNX4DEBUG((KERN_ERR "qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
  93. return -EIO;
  94. }
  95. xblk = (struct qnx4_xblk*)bh->b_data;
  96. if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
  97. QNX4DEBUG((KERN_ERR "qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
  98. return -EIO;
  99. }
  100. }
  101. block = try_extent(&xblk->xblk_xtnts[ix], &offset);
  102. if (block) {
  103. // got it!
  104. break;
  105. }
  106. if ( ++ix >= xblk->xblk_num_xtnts ) {
  107. i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
  108. ix = 0;
  109. brelse( bh );
  110. bh = NULL;
  111. }
  112. }
  113. if ( bh )
  114. brelse( bh );
  115. }
  116. QNX4DEBUG((KERN_INFO "qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
  117. return block;
  118. }
  119. static int qnx4_statfs(struct dentry *dentry, struct kstatfs *buf)
  120. {
  121. struct super_block *sb = dentry->d_sb;
  122. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  123. buf->f_type = sb->s_magic;
  124. buf->f_bsize = sb->s_blocksize;
  125. buf->f_blocks = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
  126. buf->f_bfree = qnx4_count_free_blocks(sb);
  127. buf->f_bavail = buf->f_bfree;
  128. buf->f_namelen = QNX4_NAME_MAX;
  129. buf->f_fsid = u64_to_fsid(id);
  130. return 0;
  131. }
  132. /*
  133. * Check the root directory of the filesystem to make sure
  134. * it really _is_ a qnx4 filesystem, and to check the size
  135. * of the directory entry.
  136. */
  137. static const char *qnx4_checkroot(struct super_block *sb,
  138. struct qnx4_super_block *s)
  139. {
  140. struct buffer_head *bh;
  141. struct qnx4_inode_entry *rootdir;
  142. int rd, rl;
  143. int i, j;
  144. if (s->RootDir.di_fname[0] != '/' || s->RootDir.di_fname[1] != '\0')
  145. return "no qnx4 filesystem (no root dir).";
  146. QNX4DEBUG((KERN_NOTICE "QNX4 filesystem found on dev %s.\n", sb->s_id));
  147. rd = le32_to_cpu(s->RootDir.di_first_xtnt.xtnt_blk) - 1;
  148. rl = le32_to_cpu(s->RootDir.di_first_xtnt.xtnt_size);
  149. for (j = 0; j < rl; j++) {
  150. bh = sb_bread(sb, rd + j); /* root dir, first block */
  151. if (bh == NULL)
  152. return "unable to read root entry.";
  153. rootdir = (struct qnx4_inode_entry *) bh->b_data;
  154. for (i = 0; i < QNX4_INODES_PER_BLOCK; i++, rootdir++) {
  155. QNX4DEBUG((KERN_INFO "rootdir entry found : [%s]\n", rootdir->di_fname));
  156. if (strcmp(rootdir->di_fname, QNX4_BMNAME) != 0)
  157. continue;
  158. qnx4_sb(sb)->BitMap = kmemdup(rootdir,
  159. sizeof(struct qnx4_inode_entry),
  160. GFP_KERNEL);
  161. brelse(bh);
  162. if (!qnx4_sb(sb)->BitMap)
  163. return "not enough memory for bitmap inode";
  164. /* keep bitmap inode known */
  165. return NULL;
  166. }
  167. brelse(bh);
  168. }
  169. return "bitmap file not found.";
  170. }
  171. static int qnx4_fill_super(struct super_block *s, struct fs_context *fc)
  172. {
  173. struct buffer_head *bh;
  174. struct inode *root;
  175. const char *errmsg;
  176. struct qnx4_sb_info *qs;
  177. int silent = fc->sb_flags & SB_SILENT;
  178. qs = kzalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
  179. if (!qs)
  180. return -ENOMEM;
  181. s->s_fs_info = qs;
  182. sb_set_blocksize(s, QNX4_BLOCK_SIZE);
  183. s->s_op = &qnx4_sops;
  184. s->s_magic = QNX4_SUPER_MAGIC;
  185. s->s_flags |= SB_RDONLY; /* Yup, read-only yet */
  186. s->s_time_min = 0;
  187. s->s_time_max = U32_MAX;
  188. /* Check the superblock signature. Since the qnx4 code is
  189. dangerous, we should leave as quickly as possible
  190. if we don't belong here... */
  191. bh = sb_bread(s, 1);
  192. if (!bh) {
  193. printk(KERN_ERR "qnx4: unable to read the superblock\n");
  194. return -EINVAL;
  195. }
  196. /* check before allocating dentries, inodes, .. */
  197. errmsg = qnx4_checkroot(s, (struct qnx4_super_block *) bh->b_data);
  198. brelse(bh);
  199. if (errmsg != NULL) {
  200. if (!silent)
  201. printk(KERN_ERR "qnx4: %s\n", errmsg);
  202. return -EINVAL;
  203. }
  204. /* does root not have inode number QNX4_ROOT_INO ?? */
  205. root = qnx4_iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
  206. if (IS_ERR(root)) {
  207. printk(KERN_ERR "qnx4: get inode failed\n");
  208. return PTR_ERR(root);
  209. }
  210. s->s_root = d_make_root(root);
  211. if (s->s_root == NULL)
  212. return -ENOMEM;
  213. return 0;
  214. }
  215. static int qnx4_get_tree(struct fs_context *fc)
  216. {
  217. return get_tree_bdev(fc, qnx4_fill_super);
  218. }
  219. static int qnx4_init_fs_context(struct fs_context *fc)
  220. {
  221. fc->ops = &qnx4_context_opts;
  222. return 0;
  223. }
  224. static void qnx4_kill_sb(struct super_block *sb)
  225. {
  226. struct qnx4_sb_info *qs = qnx4_sb(sb);
  227. kill_block_super(sb);
  228. if (qs) {
  229. kfree(qs->BitMap);
  230. kfree(qs);
  231. }
  232. }
  233. static int qnx4_read_folio(struct file *file, struct folio *folio)
  234. {
  235. return block_read_full_folio(folio, qnx4_get_block);
  236. }
  237. static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
  238. {
  239. return generic_block_bmap(mapping,block,qnx4_get_block);
  240. }
  241. static const struct address_space_operations qnx4_aops = {
  242. .read_folio = qnx4_read_folio,
  243. .bmap = qnx4_bmap
  244. };
  245. struct inode *qnx4_iget(struct super_block *sb, unsigned long ino)
  246. {
  247. struct buffer_head *bh;
  248. struct qnx4_inode_entry *raw_inode;
  249. int block;
  250. struct qnx4_inode_entry *qnx4_inode;
  251. struct inode *inode;
  252. inode = iget_locked(sb, ino);
  253. if (!inode)
  254. return ERR_PTR(-ENOMEM);
  255. if (!(inode->i_state & I_NEW))
  256. return inode;
  257. qnx4_inode = qnx4_raw_inode(inode);
  258. inode->i_mode = 0;
  259. QNX4DEBUG((KERN_INFO "reading inode : [%d]\n", ino));
  260. if (!ino) {
  261. printk(KERN_ERR "qnx4: bad inode number on dev %s: %lu is "
  262. "out of range\n",
  263. sb->s_id, ino);
  264. iget_failed(inode);
  265. return ERR_PTR(-EIO);
  266. }
  267. block = ino / QNX4_INODES_PER_BLOCK;
  268. if (!(bh = sb_bread(sb, block))) {
  269. printk(KERN_ERR "qnx4: major problem: unable to read inode from dev "
  270. "%s\n", sb->s_id);
  271. iget_failed(inode);
  272. return ERR_PTR(-EIO);
  273. }
  274. raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
  275. (ino % QNX4_INODES_PER_BLOCK);
  276. inode->i_mode = le16_to_cpu(raw_inode->di_mode);
  277. i_uid_write(inode, (uid_t)le16_to_cpu(raw_inode->di_uid));
  278. i_gid_write(inode, (gid_t)le16_to_cpu(raw_inode->di_gid));
  279. set_nlink(inode, le16_to_cpu(raw_inode->di_nlink));
  280. inode->i_size = le32_to_cpu(raw_inode->di_size);
  281. inode_set_mtime(inode, le32_to_cpu(raw_inode->di_mtime), 0);
  282. inode_set_atime(inode, le32_to_cpu(raw_inode->di_atime), 0);
  283. inode_set_ctime(inode, le32_to_cpu(raw_inode->di_ctime), 0);
  284. inode->i_blocks = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
  285. memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
  286. if (S_ISREG(inode->i_mode)) {
  287. inode->i_fop = &generic_ro_fops;
  288. inode->i_mapping->a_ops = &qnx4_aops;
  289. qnx4_i(inode)->mmu_private = inode->i_size;
  290. } else if (S_ISDIR(inode->i_mode)) {
  291. inode->i_op = &qnx4_dir_inode_operations;
  292. inode->i_fop = &qnx4_dir_operations;
  293. } else if (S_ISLNK(inode->i_mode)) {
  294. inode->i_op = &page_symlink_inode_operations;
  295. inode_nohighmem(inode);
  296. inode->i_mapping->a_ops = &qnx4_aops;
  297. qnx4_i(inode)->mmu_private = inode->i_size;
  298. } else {
  299. printk(KERN_ERR "qnx4: bad inode %lu on dev %s\n",
  300. ino, sb->s_id);
  301. iget_failed(inode);
  302. brelse(bh);
  303. return ERR_PTR(-EIO);
  304. }
  305. brelse(bh);
  306. unlock_new_inode(inode);
  307. return inode;
  308. }
  309. static struct kmem_cache *qnx4_inode_cachep;
  310. static struct inode *qnx4_alloc_inode(struct super_block *sb)
  311. {
  312. struct qnx4_inode_info *ei;
  313. ei = alloc_inode_sb(sb, qnx4_inode_cachep, GFP_KERNEL);
  314. if (!ei)
  315. return NULL;
  316. return &ei->vfs_inode;
  317. }
  318. static void qnx4_free_inode(struct inode *inode)
  319. {
  320. kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
  321. }
  322. static void init_once(void *foo)
  323. {
  324. struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
  325. inode_init_once(&ei->vfs_inode);
  326. }
  327. static int init_inodecache(void)
  328. {
  329. qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
  330. sizeof(struct qnx4_inode_info),
  331. 0, (SLAB_RECLAIM_ACCOUNT|
  332. SLAB_ACCOUNT),
  333. init_once);
  334. if (qnx4_inode_cachep == NULL)
  335. return -ENOMEM;
  336. return 0;
  337. }
  338. static void destroy_inodecache(void)
  339. {
  340. /*
  341. * Make sure all delayed rcu free inodes are flushed before we
  342. * destroy cache.
  343. */
  344. rcu_barrier();
  345. kmem_cache_destroy(qnx4_inode_cachep);
  346. }
  347. static struct file_system_type qnx4_fs_type = {
  348. .owner = THIS_MODULE,
  349. .name = "qnx4",
  350. .kill_sb = qnx4_kill_sb,
  351. .fs_flags = FS_REQUIRES_DEV,
  352. .init_fs_context = qnx4_init_fs_context,
  353. };
  354. MODULE_ALIAS_FS("qnx4");
  355. static int __init init_qnx4_fs(void)
  356. {
  357. int err;
  358. err = init_inodecache();
  359. if (err)
  360. return err;
  361. err = register_filesystem(&qnx4_fs_type);
  362. if (err) {
  363. destroy_inodecache();
  364. return err;
  365. }
  366. printk(KERN_INFO "QNX4 filesystem 0.2.3 registered.\n");
  367. return 0;
  368. }
  369. static void __exit exit_qnx4_fs(void)
  370. {
  371. unregister_filesystem(&qnx4_fs_type);
  372. destroy_inodecache();
  373. }
  374. module_init(init_qnx4_fs)
  375. module_exit(exit_qnx4_fs)
  376. MODULE_DESCRIPTION("QNX4 file system");
  377. MODULE_LICENSE("GPL");