file.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * OMFS (as used by RIO Karma) file operations.
  3. * Copyright (C) 2005 Bob Copeland <me@bobcopeland.com>
  4. * Released under GPL v2.
  5. */
  6. #include <linux/version.h>
  7. #include <linux/module.h>
  8. #include <linux/fs.h>
  9. #include <linux/buffer_head.h>
  10. #include <linux/mpage.h>
  11. #include "omfs.h"
  12. static u32 omfs_max_extents(struct omfs_sb_info *sbi, int offset)
  13. {
  14. return (sbi->s_sys_blocksize - offset -
  15. sizeof(struct omfs_extent)) /
  16. sizeof(struct omfs_extent_entry) + 1;
  17. }
  18. void omfs_make_empty_table(struct buffer_head *bh, int offset)
  19. {
  20. struct omfs_extent *oe = (struct omfs_extent *) &bh->b_data[offset];
  21. oe->e_next = ~cpu_to_be64(0ULL);
  22. oe->e_extent_count = cpu_to_be32(1),
  23. oe->e_fill = cpu_to_be32(0x22),
  24. oe->e_entry.e_cluster = ~cpu_to_be64(0ULL);
  25. oe->e_entry.e_blocks = ~cpu_to_be64(0ULL);
  26. }
  27. int omfs_shrink_inode(struct inode *inode)
  28. {
  29. struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  30. struct omfs_extent *oe;
  31. struct omfs_extent_entry *entry;
  32. struct buffer_head *bh;
  33. u64 next, last;
  34. u32 extent_count;
  35. u32 max_extents;
  36. int ret;
  37. /* traverse extent table, freeing each entry that is greater
  38. * than inode->i_size;
  39. */
  40. next = inode->i_ino;
  41. /* only support truncate -> 0 for now */
  42. ret = -EIO;
  43. if (inode->i_size != 0)
  44. goto out;
  45. bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next));
  46. if (!bh)
  47. goto out;
  48. oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]);
  49. max_extents = omfs_max_extents(sbi, OMFS_EXTENT_START);
  50. for (;;) {
  51. if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next))
  52. goto out_brelse;
  53. extent_count = be32_to_cpu(oe->e_extent_count);
  54. if (extent_count > max_extents)
  55. goto out_brelse;
  56. last = next;
  57. next = be64_to_cpu(oe->e_next);
  58. entry = &oe->e_entry;
  59. /* ignore last entry as it is the terminator */
  60. for (; extent_count > 1; extent_count--) {
  61. u64 start, count;
  62. start = be64_to_cpu(entry->e_cluster);
  63. count = be64_to_cpu(entry->e_blocks);
  64. omfs_clear_range(inode->i_sb, start, (int) count);
  65. entry++;
  66. }
  67. omfs_make_empty_table(bh, (char *) oe - bh->b_data);
  68. mark_buffer_dirty(bh);
  69. brelse(bh);
  70. if (last != inode->i_ino)
  71. omfs_clear_range(inode->i_sb, last, sbi->s_mirrors);
  72. if (next == ~0)
  73. break;
  74. bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next));
  75. if (!bh)
  76. goto out;
  77. oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);
  78. max_extents = omfs_max_extents(sbi, OMFS_EXTENT_CONT);
  79. }
  80. ret = 0;
  81. out:
  82. return ret;
  83. out_brelse:
  84. brelse(bh);
  85. return ret;
  86. }
  87. static void omfs_truncate(struct inode *inode)
  88. {
  89. omfs_shrink_inode(inode);
  90. mark_inode_dirty(inode);
  91. }
  92. /*
  93. * Add new blocks to the current extent, or create new entries/continuations
  94. * as necessary.
  95. */
  96. static int omfs_grow_extent(struct inode *inode, struct omfs_extent *oe,
  97. u64 *ret_block)
  98. {
  99. struct omfs_extent_entry *terminator;
  100. struct omfs_extent_entry *entry = &oe->e_entry;
  101. struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  102. u32 extent_count = be32_to_cpu(oe->e_extent_count);
  103. u64 new_block = 0;
  104. u32 max_count;
  105. int new_count;
  106. int ret = 0;
  107. /* reached the end of the extent table with no blocks mapped.
  108. * there are three possibilities for adding: grow last extent,
  109. * add a new extent to the current extent table, and add a
  110. * continuation inode. in last two cases need an allocator for
  111. * sbi->s_cluster_size
  112. */
  113. /* TODO: handle holes */
  114. /* should always have a terminator */
  115. if (extent_count < 1)
  116. return -EIO;
  117. /* trivially grow current extent, if next block is not taken */
  118. terminator = entry + extent_count - 1;
  119. if (extent_count > 1) {
  120. entry = terminator-1;
  121. new_block = be64_to_cpu(entry->e_cluster) +
  122. be64_to_cpu(entry->e_blocks);
  123. if (omfs_allocate_block(inode->i_sb, new_block)) {
  124. entry->e_blocks =
  125. cpu_to_be64(be64_to_cpu(entry->e_blocks) + 1);
  126. terminator->e_blocks = ~(cpu_to_be64(
  127. be64_to_cpu(~terminator->e_blocks) + 1));
  128. goto out;
  129. }
  130. }
  131. max_count = omfs_max_extents(sbi, OMFS_EXTENT_START);
  132. /* TODO: add a continuation block here */
  133. if (be32_to_cpu(oe->e_extent_count) > max_count-1)
  134. return -EIO;
  135. /* try to allocate a new cluster */
  136. ret = omfs_allocate_range(inode->i_sb, 1, sbi->s_clustersize,
  137. &new_block, &new_count);
  138. if (ret)
  139. goto out_fail;
  140. /* copy terminator down an entry */
  141. entry = terminator;
  142. terminator++;
  143. memcpy(terminator, entry, sizeof(struct omfs_extent_entry));
  144. entry->e_cluster = cpu_to_be64(new_block);
  145. entry->e_blocks = cpu_to_be64((u64) new_count);
  146. terminator->e_blocks = ~(cpu_to_be64(
  147. be64_to_cpu(~terminator->e_blocks) + (u64) new_count));
  148. /* write in new entry */
  149. oe->e_extent_count = cpu_to_be32(1 + be32_to_cpu(oe->e_extent_count));
  150. out:
  151. *ret_block = new_block;
  152. out_fail:
  153. return ret;
  154. }
  155. /*
  156. * Scans across the directory table for a given file block number.
  157. * If block not found, return 0.
  158. */
  159. static sector_t find_block(struct inode *inode, struct omfs_extent_entry *ent,
  160. sector_t block, int count, int *left)
  161. {
  162. /* count > 1 because of terminator */
  163. sector_t searched = 0;
  164. for (; count > 1; count--) {
  165. int numblocks = clus_to_blk(OMFS_SB(inode->i_sb),
  166. be64_to_cpu(ent->e_blocks));
  167. if (block >= searched &&
  168. block < searched + numblocks) {
  169. /*
  170. * found it at cluster + (block - searched)
  171. * numblocks - (block - searched) is remainder
  172. */
  173. *left = numblocks - (block - searched);
  174. return clus_to_blk(OMFS_SB(inode->i_sb),
  175. be64_to_cpu(ent->e_cluster)) +
  176. block - searched;
  177. }
  178. searched += numblocks;
  179. ent++;
  180. }
  181. return 0;
  182. }
  183. static int omfs_get_block(struct inode *inode, sector_t block,
  184. struct buffer_head *bh_result, int create)
  185. {
  186. struct buffer_head *bh;
  187. sector_t next, offset;
  188. int ret;
  189. u64 new_block;
  190. u32 max_extents;
  191. int extent_count;
  192. struct omfs_extent *oe;
  193. struct omfs_extent_entry *entry;
  194. struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  195. int max_blocks = bh_result->b_size >> inode->i_blkbits;
  196. int remain;
  197. ret = -EIO;
  198. bh = sb_bread(inode->i_sb, clus_to_blk(sbi, inode->i_ino));
  199. if (!bh)
  200. goto out;
  201. oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]);
  202. max_extents = omfs_max_extents(sbi, OMFS_EXTENT_START);
  203. next = inode->i_ino;
  204. for (;;) {
  205. if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next))
  206. goto out_brelse;
  207. extent_count = be32_to_cpu(oe->e_extent_count);
  208. next = be64_to_cpu(oe->e_next);
  209. entry = &oe->e_entry;
  210. if (extent_count > max_extents)
  211. goto out_brelse;
  212. offset = find_block(inode, entry, block, extent_count, &remain);
  213. if (offset > 0) {
  214. ret = 0;
  215. map_bh(bh_result, inode->i_sb, offset);
  216. if (remain > max_blocks)
  217. remain = max_blocks;
  218. bh_result->b_size = (remain << inode->i_blkbits);
  219. goto out_brelse;
  220. }
  221. if (next == ~0)
  222. break;
  223. brelse(bh);
  224. bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next));
  225. if (!bh)
  226. goto out;
  227. oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);
  228. max_extents = omfs_max_extents(sbi, OMFS_EXTENT_CONT);
  229. }
  230. if (create) {
  231. ret = omfs_grow_extent(inode, oe, &new_block);
  232. if (ret == 0) {
  233. mark_buffer_dirty(bh);
  234. mark_inode_dirty(inode);
  235. map_bh(bh_result, inode->i_sb,
  236. clus_to_blk(sbi, new_block));
  237. }
  238. }
  239. out_brelse:
  240. brelse(bh);
  241. out:
  242. return ret;
  243. }
  244. static int omfs_readpage(struct file *file, struct page *page)
  245. {
  246. return block_read_full_page(page, omfs_get_block);
  247. }
  248. static int omfs_readpages(struct file *file, struct address_space *mapping,
  249. struct list_head *pages, unsigned nr_pages)
  250. {
  251. return mpage_readpages(mapping, pages, nr_pages, omfs_get_block);
  252. }
  253. static int omfs_writepage(struct page *page, struct writeback_control *wbc)
  254. {
  255. return block_write_full_page(page, omfs_get_block, wbc);
  256. }
  257. static int
  258. omfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  259. {
  260. return mpage_writepages(mapping, wbc, omfs_get_block);
  261. }
  262. static int omfs_write_begin(struct file *file, struct address_space *mapping,
  263. loff_t pos, unsigned len, unsigned flags,
  264. struct page **pagep, void **fsdata)
  265. {
  266. *pagep = NULL;
  267. return block_write_begin(file, mapping, pos, len, flags,
  268. pagep, fsdata, omfs_get_block);
  269. }
  270. static sector_t omfs_bmap(struct address_space *mapping, sector_t block)
  271. {
  272. return generic_block_bmap(mapping, block, omfs_get_block);
  273. }
  274. struct file_operations omfs_file_operations = {
  275. .llseek = generic_file_llseek,
  276. .read = do_sync_read,
  277. .write = do_sync_write,
  278. .aio_read = generic_file_aio_read,
  279. .aio_write = generic_file_aio_write,
  280. .mmap = generic_file_mmap,
  281. .fsync = simple_fsync,
  282. .splice_read = generic_file_splice_read,
  283. };
  284. struct inode_operations omfs_file_inops = {
  285. .truncate = omfs_truncate
  286. };
  287. struct address_space_operations omfs_aops = {
  288. .readpage = omfs_readpage,
  289. .readpages = omfs_readpages,
  290. .writepage = omfs_writepage,
  291. .writepages = omfs_writepages,
  292. .sync_page = block_sync_page,
  293. .write_begin = omfs_write_begin,
  294. .write_end = generic_write_end,
  295. .bmap = omfs_bmap,
  296. };