file.c 9.0 KB

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