inode.c 11 KB

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