inode.c 13 KB

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