inode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 struct buffer_head *qnx4_getblk(struct inode *inode, int nr,
  48. int create)
  49. {
  50. struct buffer_head *result = NULL;
  51. if ( nr >= 0 )
  52. nr = qnx4_block_map( inode, nr );
  53. if (nr) {
  54. result = sb_getblk(inode->i_sb, nr);
  55. return result;
  56. }
  57. return NULL;
  58. }
  59. struct buffer_head *qnx4_bread(struct inode *inode, int block, int create)
  60. {
  61. struct buffer_head *bh;
  62. bh = qnx4_getblk(inode, block, create);
  63. if (!bh || buffer_uptodate(bh)) {
  64. return bh;
  65. }
  66. ll_rw_block(READ, 1, &bh);
  67. wait_on_buffer(bh);
  68. if (buffer_uptodate(bh)) {
  69. return bh;
  70. }
  71. brelse(bh);
  72. return NULL;
  73. }
  74. static int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
  75. {
  76. unsigned long phys;
  77. QNX4DEBUG((KERN_INFO "qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
  78. phys = qnx4_block_map( inode, iblock );
  79. if ( phys ) {
  80. // logical block is before EOF
  81. map_bh(bh, inode->i_sb, phys);
  82. }
  83. return 0;
  84. }
  85. unsigned long qnx4_block_map( struct inode *inode, long iblock )
  86. {
  87. int ix;
  88. long offset, i_xblk;
  89. unsigned long block = 0;
  90. struct buffer_head *bh = NULL;
  91. struct qnx4_xblk *xblk = NULL;
  92. struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
  93. u16 nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
  94. if ( iblock < le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size) ) {
  95. // iblock is in the first extent. This is easy.
  96. block = le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_blk) + iblock - 1;
  97. } else {
  98. // iblock is beyond first extent. We have to follow the extent chain.
  99. i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
  100. offset = iblock - le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size);
  101. ix = 0;
  102. while ( --nxtnt > 0 ) {
  103. if ( ix == 0 ) {
  104. // read next xtnt block.
  105. bh = sb_bread(inode->i_sb, i_xblk - 1);
  106. if ( !bh ) {
  107. QNX4DEBUG((KERN_ERR "qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
  108. return -EIO;
  109. }
  110. xblk = (struct qnx4_xblk*)bh->b_data;
  111. if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
  112. QNX4DEBUG((KERN_ERR "qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
  113. return -EIO;
  114. }
  115. }
  116. if ( offset < le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size) ) {
  117. // got it!
  118. block = le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_blk) + offset - 1;
  119. break;
  120. }
  121. offset -= le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size);
  122. if ( ++ix >= xblk->xblk_num_xtnts ) {
  123. i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
  124. ix = 0;
  125. brelse( bh );
  126. bh = NULL;
  127. }
  128. }
  129. if ( bh )
  130. brelse( bh );
  131. }
  132. QNX4DEBUG((KERN_INFO "qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
  133. return block;
  134. }
  135. static int qnx4_statfs(struct dentry *dentry, struct kstatfs *buf)
  136. {
  137. struct super_block *sb = dentry->d_sb;
  138. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  139. buf->f_type = sb->s_magic;
  140. buf->f_bsize = sb->s_blocksize;
  141. buf->f_blocks = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
  142. buf->f_bfree = qnx4_count_free_blocks(sb);
  143. buf->f_bavail = buf->f_bfree;
  144. buf->f_namelen = QNX4_NAME_MAX;
  145. buf->f_fsid.val[0] = (u32)id;
  146. buf->f_fsid.val[1] = (u32)(id >> 32);
  147. return 0;
  148. }
  149. /*
  150. * Check the root directory of the filesystem to make sure
  151. * it really _is_ a qnx4 filesystem, and to check the size
  152. * of the directory entry.
  153. */
  154. static const char *qnx4_checkroot(struct super_block *sb)
  155. {
  156. struct buffer_head *bh;
  157. struct qnx4_inode_entry *rootdir;
  158. int rd, rl;
  159. int i, j;
  160. int found = 0;
  161. if (*(qnx4_sb(sb)->sb->RootDir.di_fname) != '/') {
  162. return "no qnx4 filesystem (no root dir).";
  163. } else {
  164. QNX4DEBUG((KERN_NOTICE "QNX4 filesystem found on dev %s.\n", sb->s_id));
  165. rd = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_blk) - 1;
  166. rl = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_size);
  167. for (j = 0; j < rl; j++) {
  168. bh = sb_bread(sb, rd + j); /* root dir, first block */
  169. if (bh == NULL) {
  170. return "unable to read root entry.";
  171. }
  172. for (i = 0; i < QNX4_INODES_PER_BLOCK; i++) {
  173. rootdir = (struct qnx4_inode_entry *) (bh->b_data + i * QNX4_DIR_ENTRY_SIZE);
  174. if (rootdir->di_fname != NULL) {
  175. QNX4DEBUG((KERN_INFO "rootdir entry found : [%s]\n", rootdir->di_fname));
  176. if (!strcmp(rootdir->di_fname,
  177. QNX4_BMNAME)) {
  178. found = 1;
  179. qnx4_sb(sb)->BitMap = kmalloc( sizeof( struct qnx4_inode_entry ), GFP_KERNEL );
  180. if (!qnx4_sb(sb)->BitMap) {
  181. brelse (bh);
  182. return "not enough memory for bitmap inode";
  183. }
  184. memcpy( qnx4_sb(sb)->BitMap, rootdir, sizeof( struct qnx4_inode_entry ) ); /* keep bitmap inode known */
  185. break;
  186. }
  187. }
  188. }
  189. brelse(bh);
  190. if (found != 0) {
  191. break;
  192. }
  193. }
  194. if (found == 0) {
  195. return "bitmap file not found.";
  196. }
  197. }
  198. return NULL;
  199. }
  200. static int qnx4_fill_super(struct super_block *s, void *data, int silent)
  201. {
  202. struct buffer_head *bh;
  203. struct inode *root;
  204. const char *errmsg;
  205. struct qnx4_sb_info *qs;
  206. int ret = -EINVAL;
  207. qs = kzalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
  208. if (!qs)
  209. return -ENOMEM;
  210. s->s_fs_info = qs;
  211. sb_set_blocksize(s, QNX4_BLOCK_SIZE);
  212. /* Check the superblock signature. Since the qnx4 code is
  213. dangerous, we should leave as quickly as possible
  214. if we don't belong here... */
  215. bh = sb_bread(s, 1);
  216. if (!bh) {
  217. printk(KERN_ERR "qnx4: unable to read the superblock\n");
  218. goto outnobh;
  219. }
  220. if ( le32_to_cpup((__le32*) bh->b_data) != QNX4_SUPER_MAGIC ) {
  221. if (!silent)
  222. printk(KERN_ERR "qnx4: wrong fsid in superblock.\n");
  223. goto out;
  224. }
  225. s->s_op = &qnx4_sops;
  226. s->s_magic = QNX4_SUPER_MAGIC;
  227. s->s_flags |= MS_RDONLY; /* Yup, read-only yet */
  228. qnx4_sb(s)->sb_buf = bh;
  229. qnx4_sb(s)->sb = (struct qnx4_super_block *) bh->b_data;
  230. /* check before allocating dentries, inodes, .. */
  231. errmsg = qnx4_checkroot(s);
  232. if (errmsg != NULL) {
  233. if (!silent)
  234. printk(KERN_ERR "qnx4: %s\n", errmsg);
  235. goto out;
  236. }
  237. /* does root not have inode number QNX4_ROOT_INO ?? */
  238. root = qnx4_iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
  239. if (IS_ERR(root)) {
  240. printk(KERN_ERR "qnx4: get inode failed\n");
  241. ret = PTR_ERR(root);
  242. goto out;
  243. }
  244. ret = -ENOMEM;
  245. s->s_root = d_alloc_root(root);
  246. if (s->s_root == NULL)
  247. goto outi;
  248. brelse(bh);
  249. return 0;
  250. outi:
  251. iput(root);
  252. out:
  253. brelse(bh);
  254. outnobh:
  255. kfree(qs);
  256. s->s_fs_info = NULL;
  257. return ret;
  258. }
  259. static void qnx4_put_super(struct super_block *sb)
  260. {
  261. struct qnx4_sb_info *qs = qnx4_sb(sb);
  262. kfree( qs->BitMap );
  263. kfree( qs );
  264. sb->s_fs_info = NULL;
  265. return;
  266. }
  267. static int qnx4_writepage(struct page *page, struct writeback_control *wbc)
  268. {
  269. return block_write_full_page(page,qnx4_get_block, wbc);
  270. }
  271. static int qnx4_readpage(struct file *file, struct page *page)
  272. {
  273. return block_read_full_page(page,qnx4_get_block);
  274. }
  275. static int qnx4_write_begin(struct file *file, struct address_space *mapping,
  276. loff_t pos, unsigned len, unsigned flags,
  277. struct page **pagep, void **fsdata)
  278. {
  279. struct qnx4_inode_info *qnx4_inode = qnx4_i(mapping->host);
  280. int ret;
  281. *pagep = NULL;
  282. ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  283. qnx4_get_block,
  284. &qnx4_inode->mmu_private);
  285. if (unlikely(ret)) {
  286. loff_t isize = mapping->host->i_size;
  287. if (pos + len > isize)
  288. vmtruncate(mapping->host, isize);
  289. }
  290. return ret;
  291. }
  292. static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
  293. {
  294. return generic_block_bmap(mapping,block,qnx4_get_block);
  295. }
  296. static const struct address_space_operations qnx4_aops = {
  297. .readpage = qnx4_readpage,
  298. .writepage = qnx4_writepage,
  299. .write_begin = qnx4_write_begin,
  300. .write_end = generic_write_end,
  301. .bmap = qnx4_bmap
  302. };
  303. struct inode *qnx4_iget(struct super_block *sb, unsigned long ino)
  304. {
  305. struct buffer_head *bh;
  306. struct qnx4_inode_entry *raw_inode;
  307. int block;
  308. struct qnx4_inode_entry *qnx4_inode;
  309. struct inode *inode;
  310. inode = iget_locked(sb, ino);
  311. if (!inode)
  312. return ERR_PTR(-ENOMEM);
  313. if (!(inode->i_state & I_NEW))
  314. return inode;
  315. qnx4_inode = qnx4_raw_inode(inode);
  316. inode->i_mode = 0;
  317. QNX4DEBUG((KERN_INFO "reading inode : [%d]\n", ino));
  318. if (!ino) {
  319. printk(KERN_ERR "qnx4: bad inode number on dev %s: %lu is "
  320. "out of range\n",
  321. sb->s_id, ino);
  322. iget_failed(inode);
  323. return ERR_PTR(-EIO);
  324. }
  325. block = ino / QNX4_INODES_PER_BLOCK;
  326. if (!(bh = sb_bread(sb, block))) {
  327. printk(KERN_ERR "qnx4: major problem: unable to read inode from dev "
  328. "%s\n", sb->s_id);
  329. iget_failed(inode);
  330. return ERR_PTR(-EIO);
  331. }
  332. raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
  333. (ino % QNX4_INODES_PER_BLOCK);
  334. inode->i_mode = le16_to_cpu(raw_inode->di_mode);
  335. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->di_uid);
  336. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->di_gid);
  337. inode->i_nlink = le16_to_cpu(raw_inode->di_nlink);
  338. inode->i_size = le32_to_cpu(raw_inode->di_size);
  339. inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->di_mtime);
  340. inode->i_mtime.tv_nsec = 0;
  341. inode->i_atime.tv_sec = le32_to_cpu(raw_inode->di_atime);
  342. inode->i_atime.tv_nsec = 0;
  343. inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->di_ctime);
  344. inode->i_ctime.tv_nsec = 0;
  345. inode->i_blocks = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
  346. memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
  347. if (S_ISREG(inode->i_mode)) {
  348. inode->i_fop = &generic_ro_fops;
  349. inode->i_mapping->a_ops = &qnx4_aops;
  350. qnx4_i(inode)->mmu_private = inode->i_size;
  351. } else if (S_ISDIR(inode->i_mode)) {
  352. inode->i_op = &qnx4_dir_inode_operations;
  353. inode->i_fop = &qnx4_dir_operations;
  354. } else if (S_ISLNK(inode->i_mode)) {
  355. inode->i_op = &page_symlink_inode_operations;
  356. inode->i_mapping->a_ops = &qnx4_aops;
  357. qnx4_i(inode)->mmu_private = inode->i_size;
  358. } else {
  359. printk(KERN_ERR "qnx4: bad inode %lu on dev %s\n",
  360. ino, sb->s_id);
  361. iget_failed(inode);
  362. brelse(bh);
  363. return ERR_PTR(-EIO);
  364. }
  365. brelse(bh);
  366. unlock_new_inode(inode);
  367. return inode;
  368. }
  369. static struct kmem_cache *qnx4_inode_cachep;
  370. static struct inode *qnx4_alloc_inode(struct super_block *sb)
  371. {
  372. struct qnx4_inode_info *ei;
  373. ei = kmem_cache_alloc(qnx4_inode_cachep, GFP_KERNEL);
  374. if (!ei)
  375. return NULL;
  376. return &ei->vfs_inode;
  377. }
  378. static void qnx4_i_callback(struct rcu_head *head)
  379. {
  380. struct inode *inode = container_of(head, struct inode, i_rcu);
  381. INIT_LIST_HEAD(&inode->i_dentry);
  382. kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
  383. }
  384. static void qnx4_destroy_inode(struct inode *inode)
  385. {
  386. call_rcu(&inode->i_rcu, qnx4_i_callback);
  387. }
  388. static void init_once(void *foo)
  389. {
  390. struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
  391. inode_init_once(&ei->vfs_inode);
  392. }
  393. static int init_inodecache(void)
  394. {
  395. qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
  396. sizeof(struct qnx4_inode_info),
  397. 0, (SLAB_RECLAIM_ACCOUNT|
  398. SLAB_MEM_SPREAD),
  399. init_once);
  400. if (qnx4_inode_cachep == NULL)
  401. return -ENOMEM;
  402. return 0;
  403. }
  404. static void destroy_inodecache(void)
  405. {
  406. kmem_cache_destroy(qnx4_inode_cachep);
  407. }
  408. static struct dentry *qnx4_mount(struct file_system_type *fs_type,
  409. int flags, const char *dev_name, void *data)
  410. {
  411. return mount_bdev(fs_type, flags, dev_name, data, qnx4_fill_super);
  412. }
  413. static struct file_system_type qnx4_fs_type = {
  414. .owner = THIS_MODULE,
  415. .name = "qnx4",
  416. .mount = qnx4_mount,
  417. .kill_sb = kill_block_super,
  418. .fs_flags = FS_REQUIRES_DEV,
  419. };
  420. static int __init init_qnx4_fs(void)
  421. {
  422. int err;
  423. err = init_inodecache();
  424. if (err)
  425. return err;
  426. err = register_filesystem(&qnx4_fs_type);
  427. if (err) {
  428. destroy_inodecache();
  429. return err;
  430. }
  431. printk(KERN_INFO "QNX4 filesystem 0.2.3 registered.\n");
  432. return 0;
  433. }
  434. static void __exit exit_qnx4_fs(void)
  435. {
  436. unregister_filesystem(&qnx4_fs_type);
  437. destroy_inodecache();
  438. }
  439. module_init(init_qnx4_fs)
  440. module_exit(exit_qnx4_fs)
  441. MODULE_LICENSE("GPL");