inode.c 11 KB

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