inode.c 15 KB

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