inode.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * inode.c
  3. *
  4. * Copyright (c) 1999 Al Smith
  5. *
  6. * Portions derived from work (c) 1995,1996 Christian Vogelgsang,
  7. * and from work (c) 1998 Mike Shaver.
  8. */
  9. #include <linux/buffer_head.h>
  10. #include <linux/module.h>
  11. #include <linux/fs.h>
  12. #include "efs.h"
  13. #include <linux/efs_fs_sb.h>
  14. static int efs_readpage(struct file *file, struct page *page)
  15. {
  16. return block_read_full_page(page,efs_get_block);
  17. }
  18. static sector_t _efs_bmap(struct address_space *mapping, sector_t block)
  19. {
  20. return generic_block_bmap(mapping,block,efs_get_block);
  21. }
  22. static const struct address_space_operations efs_aops = {
  23. .readpage = efs_readpage,
  24. .bmap = _efs_bmap
  25. };
  26. static inline void extent_copy(efs_extent *src, efs_extent *dst) {
  27. /*
  28. * this is slightly evil. it doesn't just copy
  29. * efs_extent from src to dst, it also mangles
  30. * the bits so that dst ends up in cpu byte-order.
  31. */
  32. dst->cooked.ex_magic = (unsigned int) src->raw[0];
  33. dst->cooked.ex_bn = ((unsigned int) src->raw[1] << 16) |
  34. ((unsigned int) src->raw[2] << 8) |
  35. ((unsigned int) src->raw[3] << 0);
  36. dst->cooked.ex_length = (unsigned int) src->raw[4];
  37. dst->cooked.ex_offset = ((unsigned int) src->raw[5] << 16) |
  38. ((unsigned int) src->raw[6] << 8) |
  39. ((unsigned int) src->raw[7] << 0);
  40. return;
  41. }
  42. struct inode *efs_iget(struct super_block *super, unsigned long ino)
  43. {
  44. int i, inode_index;
  45. dev_t device;
  46. u32 rdev;
  47. struct buffer_head *bh;
  48. struct efs_sb_info *sb = SUPER_INFO(super);
  49. struct efs_inode_info *in;
  50. efs_block_t block, offset;
  51. struct efs_dinode *efs_inode;
  52. struct inode *inode;
  53. inode = iget_locked(super, ino);
  54. if (!inode)
  55. return ERR_PTR(-ENOMEM);
  56. if (!(inode->i_state & I_NEW))
  57. return inode;
  58. in = INODE_INFO(inode);
  59. /*
  60. ** EFS layout:
  61. **
  62. ** | cylinder group | cylinder group | cylinder group ..etc
  63. ** |inodes|data |inodes|data |inodes|data ..etc
  64. **
  65. ** work out the inode block index, (considering initially that the
  66. ** inodes are stored as consecutive blocks). then work out the block
  67. ** number of that inode given the above layout, and finally the
  68. ** offset of the inode within that block.
  69. */
  70. inode_index = inode->i_ino /
  71. (EFS_BLOCKSIZE / sizeof(struct efs_dinode));
  72. block = sb->fs_start + sb->first_block +
  73. (sb->group_size * (inode_index / sb->inode_blocks)) +
  74. (inode_index % sb->inode_blocks);
  75. offset = (inode->i_ino %
  76. (EFS_BLOCKSIZE / sizeof(struct efs_dinode))) *
  77. sizeof(struct efs_dinode);
  78. bh = sb_bread(inode->i_sb, block);
  79. if (!bh) {
  80. pr_warn("%s() failed at block %d\n", __func__, block);
  81. goto read_inode_error;
  82. }
  83. efs_inode = (struct efs_dinode *) (bh->b_data + offset);
  84. inode->i_mode = be16_to_cpu(efs_inode->di_mode);
  85. set_nlink(inode, be16_to_cpu(efs_inode->di_nlink));
  86. i_uid_write(inode, (uid_t)be16_to_cpu(efs_inode->di_uid));
  87. i_gid_write(inode, (gid_t)be16_to_cpu(efs_inode->di_gid));
  88. inode->i_size = be32_to_cpu(efs_inode->di_size);
  89. inode->i_atime.tv_sec = be32_to_cpu(efs_inode->di_atime);
  90. inode->i_mtime.tv_sec = be32_to_cpu(efs_inode->di_mtime);
  91. inode->i_ctime.tv_sec = be32_to_cpu(efs_inode->di_ctime);
  92. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  93. /* this is the number of blocks in the file */
  94. if (inode->i_size == 0) {
  95. inode->i_blocks = 0;
  96. } else {
  97. inode->i_blocks = ((inode->i_size - 1) >> EFS_BLOCKSIZE_BITS) + 1;
  98. }
  99. rdev = be16_to_cpu(efs_inode->di_u.di_dev.odev);
  100. if (rdev == 0xffff) {
  101. rdev = be32_to_cpu(efs_inode->di_u.di_dev.ndev);
  102. if (sysv_major(rdev) > 0xfff)
  103. device = 0;
  104. else
  105. device = MKDEV(sysv_major(rdev), sysv_minor(rdev));
  106. } else
  107. device = old_decode_dev(rdev);
  108. /* get the number of extents for this object */
  109. in->numextents = be16_to_cpu(efs_inode->di_numextents);
  110. in->lastextent = 0;
  111. /* copy the extents contained within the inode to memory */
  112. for(i = 0; i < EFS_DIRECTEXTENTS; i++) {
  113. extent_copy(&(efs_inode->di_u.di_extents[i]), &(in->extents[i]));
  114. if (i < in->numextents && in->extents[i].cooked.ex_magic != 0) {
  115. pr_warn("extent %d has bad magic number in inode %lu\n",
  116. i, inode->i_ino);
  117. brelse(bh);
  118. goto read_inode_error;
  119. }
  120. }
  121. brelse(bh);
  122. pr_debug("efs_iget(): inode %lu, extents %d, mode %o\n",
  123. inode->i_ino, in->numextents, inode->i_mode);
  124. switch (inode->i_mode & S_IFMT) {
  125. case S_IFDIR:
  126. inode->i_op = &efs_dir_inode_operations;
  127. inode->i_fop = &efs_dir_operations;
  128. break;
  129. case S_IFREG:
  130. inode->i_fop = &generic_ro_fops;
  131. inode->i_data.a_ops = &efs_aops;
  132. break;
  133. case S_IFLNK:
  134. inode->i_op = &page_symlink_inode_operations;
  135. inode_nohighmem(inode);
  136. inode->i_data.a_ops = &efs_symlink_aops;
  137. break;
  138. case S_IFCHR:
  139. case S_IFBLK:
  140. case S_IFIFO:
  141. init_special_inode(inode, inode->i_mode, device);
  142. break;
  143. default:
  144. pr_warn("unsupported inode mode %o\n", inode->i_mode);
  145. goto read_inode_error;
  146. break;
  147. }
  148. unlock_new_inode(inode);
  149. return inode;
  150. read_inode_error:
  151. pr_warn("failed to read inode %lu\n", inode->i_ino);
  152. iget_failed(inode);
  153. return ERR_PTR(-EIO);
  154. }
  155. static inline efs_block_t
  156. efs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sb) {
  157. efs_block_t start;
  158. efs_block_t length;
  159. efs_block_t offset;
  160. /*
  161. * given an extent and a logical block within a file,
  162. * can this block be found within this extent ?
  163. */
  164. start = ptr->cooked.ex_bn;
  165. length = ptr->cooked.ex_length;
  166. offset = ptr->cooked.ex_offset;
  167. if ((block >= offset) && (block < offset+length)) {
  168. return(sb->fs_start + start + block - offset);
  169. } else {
  170. return 0;
  171. }
  172. }
  173. efs_block_t efs_map_block(struct inode *inode, efs_block_t block) {
  174. struct efs_sb_info *sb = SUPER_INFO(inode->i_sb);
  175. struct efs_inode_info *in = INODE_INFO(inode);
  176. struct buffer_head *bh = NULL;
  177. int cur, last, first = 1;
  178. int ibase, ioffset, dirext, direxts, indext, indexts;
  179. efs_block_t iblock, result = 0, lastblock = 0;
  180. efs_extent ext, *exts;
  181. last = in->lastextent;
  182. if (in->numextents <= EFS_DIRECTEXTENTS) {
  183. /* first check the last extent we returned */
  184. if ((result = efs_extent_check(&in->extents[last], block, sb)))
  185. return result;
  186. /* if we only have one extent then nothing can be found */
  187. if (in->numextents == 1) {
  188. pr_err("%s() failed to map (1 extent)\n", __func__);
  189. return 0;
  190. }
  191. direxts = in->numextents;
  192. /*
  193. * check the stored extents in the inode
  194. * start with next extent and check forwards
  195. */
  196. for(dirext = 1; dirext < direxts; dirext++) {
  197. cur = (last + dirext) % in->numextents;
  198. if ((result = efs_extent_check(&in->extents[cur], block, sb))) {
  199. in->lastextent = cur;
  200. return result;
  201. }
  202. }
  203. pr_err("%s() failed to map block %u (dir)\n", __func__, block);
  204. return 0;
  205. }
  206. pr_debug("%s(): indirect search for logical block %u\n",
  207. __func__, block);
  208. direxts = in->extents[0].cooked.ex_offset;
  209. indexts = in->numextents;
  210. for(indext = 0; indext < indexts; indext++) {
  211. cur = (last + indext) % indexts;
  212. /*
  213. * work out which direct extent contains `cur'.
  214. *
  215. * also compute ibase: i.e. the number of the first
  216. * indirect extent contained within direct extent `cur'.
  217. *
  218. */
  219. ibase = 0;
  220. for(dirext = 0; cur < ibase && dirext < direxts; dirext++) {
  221. ibase += in->extents[dirext].cooked.ex_length *
  222. (EFS_BLOCKSIZE / sizeof(efs_extent));
  223. }
  224. if (dirext == direxts) {
  225. /* should never happen */
  226. pr_err("couldn't find direct extent for indirect extent %d (block %u)\n",
  227. cur, block);
  228. if (bh) brelse(bh);
  229. return 0;
  230. }
  231. /* work out block number and offset of this indirect extent */
  232. iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn +
  233. (cur - ibase) /
  234. (EFS_BLOCKSIZE / sizeof(efs_extent));
  235. ioffset = (cur - ibase) %
  236. (EFS_BLOCKSIZE / sizeof(efs_extent));
  237. if (first || lastblock != iblock) {
  238. if (bh) brelse(bh);
  239. bh = sb_bread(inode->i_sb, iblock);
  240. if (!bh) {
  241. pr_err("%s() failed at block %d\n",
  242. __func__, iblock);
  243. return 0;
  244. }
  245. pr_debug("%s(): read indirect extent block %d\n",
  246. __func__, iblock);
  247. first = 0;
  248. lastblock = iblock;
  249. }
  250. exts = (efs_extent *) bh->b_data;
  251. extent_copy(&(exts[ioffset]), &ext);
  252. if (ext.cooked.ex_magic != 0) {
  253. pr_err("extent %d has bad magic number in block %d\n",
  254. cur, iblock);
  255. if (bh) brelse(bh);
  256. return 0;
  257. }
  258. if ((result = efs_extent_check(&ext, block, sb))) {
  259. if (bh) brelse(bh);
  260. in->lastextent = cur;
  261. return result;
  262. }
  263. }
  264. if (bh) brelse(bh);
  265. pr_err("%s() failed to map block %u (indir)\n", __func__, block);
  266. return 0;
  267. }
  268. MODULE_LICENSE("GPL");