inode.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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->i_data.a_ops = &efs_symlink_aops;
  136. break;
  137. case S_IFCHR:
  138. case S_IFBLK:
  139. case S_IFIFO:
  140. init_special_inode(inode, inode->i_mode, device);
  141. break;
  142. default:
  143. pr_warn("unsupported inode mode %o\n", inode->i_mode);
  144. goto read_inode_error;
  145. break;
  146. }
  147. unlock_new_inode(inode);
  148. return inode;
  149. read_inode_error:
  150. pr_warn("failed to read inode %lu\n", inode->i_ino);
  151. iget_failed(inode);
  152. return ERR_PTR(-EIO);
  153. }
  154. static inline efs_block_t
  155. efs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sb) {
  156. efs_block_t start;
  157. efs_block_t length;
  158. efs_block_t offset;
  159. /*
  160. * given an extent and a logical block within a file,
  161. * can this block be found within this extent ?
  162. */
  163. start = ptr->cooked.ex_bn;
  164. length = ptr->cooked.ex_length;
  165. offset = ptr->cooked.ex_offset;
  166. if ((block >= offset) && (block < offset+length)) {
  167. return(sb->fs_start + start + block - offset);
  168. } else {
  169. return 0;
  170. }
  171. }
  172. efs_block_t efs_map_block(struct inode *inode, efs_block_t block) {
  173. struct efs_sb_info *sb = SUPER_INFO(inode->i_sb);
  174. struct efs_inode_info *in = INODE_INFO(inode);
  175. struct buffer_head *bh = NULL;
  176. int cur, last, first = 1;
  177. int ibase, ioffset, dirext, direxts, indext, indexts;
  178. efs_block_t iblock, result = 0, lastblock = 0;
  179. efs_extent ext, *exts;
  180. last = in->lastextent;
  181. if (in->numextents <= EFS_DIRECTEXTENTS) {
  182. /* first check the last extent we returned */
  183. if ((result = efs_extent_check(&in->extents[last], block, sb)))
  184. return result;
  185. /* if we only have one extent then nothing can be found */
  186. if (in->numextents == 1) {
  187. pr_err("%s() failed to map (1 extent)\n", __func__);
  188. return 0;
  189. }
  190. direxts = in->numextents;
  191. /*
  192. * check the stored extents in the inode
  193. * start with next extent and check forwards
  194. */
  195. for(dirext = 1; dirext < direxts; dirext++) {
  196. cur = (last + dirext) % in->numextents;
  197. if ((result = efs_extent_check(&in->extents[cur], block, sb))) {
  198. in->lastextent = cur;
  199. return result;
  200. }
  201. }
  202. pr_err("%s() failed to map block %u (dir)\n", __func__, block);
  203. return 0;
  204. }
  205. pr_debug("%s(): indirect search for logical block %u\n",
  206. __func__, block);
  207. direxts = in->extents[0].cooked.ex_offset;
  208. indexts = in->numextents;
  209. for(indext = 0; indext < indexts; indext++) {
  210. cur = (last + indext) % indexts;
  211. /*
  212. * work out which direct extent contains `cur'.
  213. *
  214. * also compute ibase: i.e. the number of the first
  215. * indirect extent contained within direct extent `cur'.
  216. *
  217. */
  218. ibase = 0;
  219. for(dirext = 0; cur < ibase && dirext < direxts; dirext++) {
  220. ibase += in->extents[dirext].cooked.ex_length *
  221. (EFS_BLOCKSIZE / sizeof(efs_extent));
  222. }
  223. if (dirext == direxts) {
  224. /* should never happen */
  225. pr_err("couldn't find direct extent for indirect extent %d (block %u)\n",
  226. cur, block);
  227. if (bh) brelse(bh);
  228. return 0;
  229. }
  230. /* work out block number and offset of this indirect extent */
  231. iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn +
  232. (cur - ibase) /
  233. (EFS_BLOCKSIZE / sizeof(efs_extent));
  234. ioffset = (cur - ibase) %
  235. (EFS_BLOCKSIZE / sizeof(efs_extent));
  236. if (first || lastblock != iblock) {
  237. if (bh) brelse(bh);
  238. bh = sb_bread(inode->i_sb, iblock);
  239. if (!bh) {
  240. pr_err("%s() failed at block %d\n",
  241. __func__, iblock);
  242. return 0;
  243. }
  244. pr_debug("%s(): read indirect extent block %d\n",
  245. __func__, iblock);
  246. first = 0;
  247. lastblock = iblock;
  248. }
  249. exts = (efs_extent *) bh->b_data;
  250. extent_copy(&(exts[ioffset]), &ext);
  251. if (ext.cooked.ex_magic != 0) {
  252. pr_err("extent %d has bad magic number in block %d\n",
  253. cur, iblock);
  254. if (bh) brelse(bh);
  255. return 0;
  256. }
  257. if ((result = efs_extent_check(&ext, block, sb))) {
  258. if (bh) brelse(bh);
  259. in->lastextent = cur;
  260. return result;
  261. }
  262. }
  263. if (bh) brelse(bh);
  264. pr_err("%s() failed to map block %u (indir)\n", __func__, block);
  265. return 0;
  266. }
  267. MODULE_LICENSE("GPL");