file.c 8.5 KB

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