file.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/module.h>
  7. #include <linux/fs.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/mpage.h>
  10. #include "omfs.h"
  11. static u32 omfs_max_extents(struct omfs_sb_info *sbi, int offset)
  12. {
  13. return (sbi->s_sys_blocksize - offset -
  14. sizeof(struct omfs_extent)) /
  15. sizeof(struct omfs_extent_entry) + 1;
  16. }
  17. void omfs_make_empty_table(struct buffer_head *bh, int offset)
  18. {
  19. struct omfs_extent *oe = (struct omfs_extent *) &bh->b_data[offset];
  20. oe->e_next = ~cpu_to_be64(0ULL);
  21. oe->e_extent_count = cpu_to_be32(1),
  22. oe->e_fill = cpu_to_be32(0x22),
  23. oe->e_entry.e_cluster = ~cpu_to_be64(0ULL);
  24. oe->e_entry.e_blocks = ~cpu_to_be64(0ULL);
  25. }
  26. int omfs_shrink_inode(struct inode *inode)
  27. {
  28. struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  29. struct omfs_extent *oe;
  30. struct omfs_extent_entry *entry;
  31. struct buffer_head *bh;
  32. u64 next, last;
  33. u32 extent_count;
  34. u32 max_extents;
  35. int ret;
  36. /* traverse extent table, freeing each entry that is greater
  37. * than inode->i_size;
  38. */
  39. next = inode->i_ino;
  40. /* only support truncate -> 0 for now */
  41. ret = -EIO;
  42. if (inode->i_size != 0)
  43. goto out;
  44. bh = omfs_bread(inode->i_sb, next);
  45. if (!bh)
  46. goto out;
  47. oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]);
  48. max_extents = omfs_max_extents(sbi, OMFS_EXTENT_START);
  49. for (;;) {
  50. if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next))
  51. goto out_brelse;
  52. extent_count = be32_to_cpu(oe->e_extent_count);
  53. if (extent_count > max_extents)
  54. goto out_brelse;
  55. last = next;
  56. next = be64_to_cpu(oe->e_next);
  57. entry = &oe->e_entry;
  58. /* ignore last entry as it is the terminator */
  59. for (; extent_count > 1; extent_count--) {
  60. u64 start, count;
  61. start = be64_to_cpu(entry->e_cluster);
  62. count = be64_to_cpu(entry->e_blocks);
  63. omfs_clear_range(inode->i_sb, start, (int) count);
  64. entry++;
  65. }
  66. omfs_make_empty_table(bh, (char *) oe - bh->b_data);
  67. mark_buffer_dirty(bh);
  68. brelse(bh);
  69. if (last != inode->i_ino)
  70. omfs_clear_range(inode->i_sb, last, sbi->s_mirrors);
  71. if (next == ~0)
  72. break;
  73. bh = omfs_bread(inode->i_sb, next);
  74. if (!bh)
  75. goto out;
  76. oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);
  77. max_extents = omfs_max_extents(sbi, OMFS_EXTENT_CONT);
  78. }
  79. ret = 0;
  80. out:
  81. return ret;
  82. out_brelse:
  83. brelse(bh);
  84. return ret;
  85. }
  86. static void omfs_truncate(struct inode *inode)
  87. {
  88. omfs_shrink_inode(inode);
  89. mark_inode_dirty(inode);
  90. }
  91. /*
  92. * Add new blocks to the current extent, or create new entries/continuations
  93. * as necessary.
  94. */
  95. static int omfs_grow_extent(struct inode *inode, struct omfs_extent *oe,
  96. u64 *ret_block)
  97. {
  98. struct omfs_extent_entry *terminator;
  99. struct omfs_extent_entry *entry = &oe->e_entry;
  100. struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  101. u32 extent_count = be32_to_cpu(oe->e_extent_count);
  102. u64 new_block = 0;
  103. u32 max_count;
  104. int new_count;
  105. int ret = 0;
  106. /* reached the end of the extent table with no blocks mapped.
  107. * there are three possibilities for adding: grow last extent,
  108. * add a new extent to the current extent table, and add a
  109. * continuation inode. in last two cases need an allocator for
  110. * sbi->s_cluster_size
  111. */
  112. /* TODO: handle holes */
  113. /* should always have a terminator */
  114. if (extent_count < 1)
  115. return -EIO;
  116. /* trivially grow current extent, if next block is not taken */
  117. terminator = entry + extent_count - 1;
  118. if (extent_count > 1) {
  119. entry = terminator-1;
  120. new_block = be64_to_cpu(entry->e_cluster) +
  121. be64_to_cpu(entry->e_blocks);
  122. if (omfs_allocate_block(inode->i_sb, new_block)) {
  123. be64_add_cpu(&entry->e_blocks, 1);
  124. terminator->e_blocks = ~(cpu_to_be64(
  125. be64_to_cpu(~terminator->e_blocks) + 1));
  126. goto out;
  127. }
  128. }
  129. max_count = omfs_max_extents(sbi, OMFS_EXTENT_START);
  130. /* TODO: add a continuation block here */
  131. if (be32_to_cpu(oe->e_extent_count) > max_count-1)
  132. return -EIO;
  133. /* try to allocate a new cluster */
  134. ret = omfs_allocate_range(inode->i_sb, 1, sbi->s_clustersize,
  135. &new_block, &new_count);
  136. if (ret)
  137. goto out_fail;
  138. /* copy terminator down an entry */
  139. entry = terminator;
  140. terminator++;
  141. memcpy(terminator, entry, sizeof(struct omfs_extent_entry));
  142. entry->e_cluster = cpu_to_be64(new_block);
  143. entry->e_blocks = cpu_to_be64((u64) new_count);
  144. terminator->e_blocks = ~(cpu_to_be64(
  145. be64_to_cpu(~terminator->e_blocks) + (u64) new_count));
  146. /* write in new entry */
  147. be32_add_cpu(&oe->e_extent_count, 1);
  148. out:
  149. *ret_block = new_block;
  150. out_fail:
  151. return ret;
  152. }
  153. /*
  154. * Scans across the directory table for a given file block number.
  155. * If block not found, return 0.
  156. */
  157. static sector_t find_block(struct inode *inode, struct omfs_extent_entry *ent,
  158. sector_t block, int count, int *left)
  159. {
  160. /* count > 1 because of terminator */
  161. sector_t searched = 0;
  162. for (; count > 1; count--) {
  163. int numblocks = clus_to_blk(OMFS_SB(inode->i_sb),
  164. be64_to_cpu(ent->e_blocks));
  165. if (block >= searched &&
  166. block < searched + numblocks) {
  167. /*
  168. * found it at cluster + (block - searched)
  169. * numblocks - (block - searched) is remainder
  170. */
  171. *left = numblocks - (block - searched);
  172. return clus_to_blk(OMFS_SB(inode->i_sb),
  173. be64_to_cpu(ent->e_cluster)) +
  174. block - searched;
  175. }
  176. searched += numblocks;
  177. ent++;
  178. }
  179. return 0;
  180. }
  181. static int omfs_get_block(struct inode *inode, sector_t block,
  182. struct buffer_head *bh_result, int create)
  183. {
  184. struct buffer_head *bh;
  185. sector_t next, offset;
  186. int ret;
  187. u64 uninitialized_var(new_block);
  188. u32 max_extents;
  189. int extent_count;
  190. struct omfs_extent *oe;
  191. struct omfs_extent_entry *entry;
  192. struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  193. int max_blocks = bh_result->b_size >> inode->i_blkbits;
  194. int remain;
  195. ret = -EIO;
  196. bh = omfs_bread(inode->i_sb, inode->i_ino);
  197. if (!bh)
  198. goto out;
  199. oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]);
  200. max_extents = omfs_max_extents(sbi, OMFS_EXTENT_START);
  201. next = inode->i_ino;
  202. for (;;) {
  203. if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next))
  204. goto out_brelse;
  205. extent_count = be32_to_cpu(oe->e_extent_count);
  206. next = be64_to_cpu(oe->e_next);
  207. entry = &oe->e_entry;
  208. if (extent_count > max_extents)
  209. goto out_brelse;
  210. offset = find_block(inode, entry, block, extent_count, &remain);
  211. if (offset > 0) {
  212. ret = 0;
  213. map_bh(bh_result, inode->i_sb, offset);
  214. if (remain > max_blocks)
  215. remain = max_blocks;
  216. bh_result->b_size = (remain << inode->i_blkbits);
  217. goto out_brelse;
  218. }
  219. if (next == ~0)
  220. break;
  221. brelse(bh);
  222. bh = omfs_bread(inode->i_sb, next);
  223. if (!bh)
  224. goto out;
  225. oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);
  226. max_extents = omfs_max_extents(sbi, 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 void omfs_write_failed(struct address_space *mapping, loff_t to)
  261. {
  262. struct inode *inode = mapping->host;
  263. if (to > inode->i_size) {
  264. truncate_pagecache(inode, inode->i_size);
  265. omfs_truncate(inode);
  266. }
  267. }
  268. static int omfs_write_begin(struct file *file, struct address_space *mapping,
  269. loff_t pos, unsigned len, unsigned flags,
  270. struct page **pagep, void **fsdata)
  271. {
  272. int ret;
  273. ret = block_write_begin(mapping, pos, len, flags, pagep,
  274. omfs_get_block);
  275. if (unlikely(ret))
  276. omfs_write_failed(mapping, pos + len);
  277. return ret;
  278. }
  279. static sector_t omfs_bmap(struct address_space *mapping, sector_t block)
  280. {
  281. return generic_block_bmap(mapping, block, omfs_get_block);
  282. }
  283. const struct file_operations omfs_file_operations = {
  284. .llseek = generic_file_llseek,
  285. .read_iter = generic_file_read_iter,
  286. .write_iter = generic_file_write_iter,
  287. .mmap = generic_file_mmap,
  288. .fsync = generic_file_fsync,
  289. .splice_read = generic_file_splice_read,
  290. };
  291. static int omfs_setattr(struct dentry *dentry, struct iattr *attr)
  292. {
  293. struct inode *inode = d_inode(dentry);
  294. int error;
  295. error = inode_change_ok(inode, attr);
  296. if (error)
  297. return error;
  298. if ((attr->ia_valid & ATTR_SIZE) &&
  299. attr->ia_size != i_size_read(inode)) {
  300. error = inode_newsize_ok(inode, attr->ia_size);
  301. if (error)
  302. return error;
  303. truncate_setsize(inode, attr->ia_size);
  304. omfs_truncate(inode);
  305. }
  306. setattr_copy(inode, attr);
  307. mark_inode_dirty(inode);
  308. return 0;
  309. }
  310. const struct inode_operations omfs_file_inops = {
  311. .setattr = omfs_setattr,
  312. };
  313. const struct address_space_operations omfs_aops = {
  314. .readpage = omfs_readpage,
  315. .readpages = omfs_readpages,
  316. .writepage = omfs_writepage,
  317. .writepages = omfs_writepages,
  318. .write_begin = omfs_write_begin,
  319. .write_end = generic_write_end,
  320. .bmap = omfs_bmap,
  321. };