inode.c 11 KB

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