file.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * fs/logfs/file.c - prepare_write, commit_write and friends
  3. *
  4. * As should be obvious for Linux kernel code, license is GPLv2
  5. *
  6. * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
  7. */
  8. #include "logfs.h"
  9. #include <linux/sched.h>
  10. #include <linux/writeback.h>
  11. static int logfs_write_begin(struct file *file, struct address_space *mapping,
  12. loff_t pos, unsigned len, unsigned flags,
  13. struct page **pagep, void **fsdata)
  14. {
  15. struct inode *inode = mapping->host;
  16. struct page *page;
  17. pgoff_t index = pos >> PAGE_SHIFT;
  18. page = grab_cache_page_write_begin(mapping, index, flags);
  19. if (!page)
  20. return -ENOMEM;
  21. *pagep = page;
  22. if ((len == PAGE_SIZE) || PageUptodate(page))
  23. return 0;
  24. if ((pos & PAGE_MASK) >= i_size_read(inode)) {
  25. unsigned start = pos & (PAGE_SIZE - 1);
  26. unsigned end = start + len;
  27. /* Reading beyond i_size is simple: memset to zero */
  28. zero_user_segments(page, 0, start, end, PAGE_SIZE);
  29. return 0;
  30. }
  31. return logfs_readpage_nolock(page);
  32. }
  33. static int logfs_write_end(struct file *file, struct address_space *mapping,
  34. loff_t pos, unsigned len, unsigned copied, struct page *page,
  35. void *fsdata)
  36. {
  37. struct inode *inode = mapping->host;
  38. pgoff_t index = page->index;
  39. unsigned start = pos & (PAGE_SIZE - 1);
  40. unsigned end = start + copied;
  41. int ret = 0;
  42. BUG_ON(PAGE_SIZE != inode->i_sb->s_blocksize);
  43. BUG_ON(page->index > I3_BLOCKS);
  44. if (copied < len) {
  45. /*
  46. * Short write of a non-initialized paged. Just tell userspace
  47. * to retry the entire page.
  48. */
  49. if (!PageUptodate(page)) {
  50. copied = 0;
  51. goto out;
  52. }
  53. }
  54. if (copied == 0)
  55. goto out; /* FIXME: do we need to update inode? */
  56. if (i_size_read(inode) < (index << PAGE_SHIFT) + end) {
  57. i_size_write(inode, (index << PAGE_SHIFT) + end);
  58. mark_inode_dirty_sync(inode);
  59. }
  60. SetPageUptodate(page);
  61. if (!PageDirty(page)) {
  62. if (!get_page_reserve(inode, page))
  63. __set_page_dirty_nobuffers(page);
  64. else
  65. ret = logfs_write_buf(inode, page, WF_LOCK);
  66. }
  67. out:
  68. unlock_page(page);
  69. put_page(page);
  70. return ret ? ret : copied;
  71. }
  72. int logfs_readpage(struct file *file, struct page *page)
  73. {
  74. int ret;
  75. ret = logfs_readpage_nolock(page);
  76. unlock_page(page);
  77. return ret;
  78. }
  79. /* Clear the page's dirty flag in the radix tree. */
  80. /* TODO: mucking with PageWriteback is silly. Add a generic function to clear
  81. * the dirty bit from the radix tree for filesystems that don't have to wait
  82. * for page writeback to finish (i.e. any compressing filesystem).
  83. */
  84. static void clear_radix_tree_dirty(struct page *page)
  85. {
  86. BUG_ON(PagePrivate(page) || page->private);
  87. set_page_writeback(page);
  88. end_page_writeback(page);
  89. }
  90. static int __logfs_writepage(struct page *page)
  91. {
  92. struct inode *inode = page->mapping->host;
  93. int err;
  94. err = logfs_write_buf(inode, page, WF_LOCK);
  95. if (err)
  96. set_page_dirty(page);
  97. else
  98. clear_radix_tree_dirty(page);
  99. unlock_page(page);
  100. return err;
  101. }
  102. static int logfs_writepage(struct page *page, struct writeback_control *wbc)
  103. {
  104. struct inode *inode = page->mapping->host;
  105. loff_t i_size = i_size_read(inode);
  106. pgoff_t end_index = i_size >> PAGE_SHIFT;
  107. unsigned offset;
  108. u64 bix;
  109. level_t level;
  110. log_file("logfs_writepage(%lx, %lx, %p)\n", inode->i_ino, page->index,
  111. page);
  112. logfs_unpack_index(page->index, &bix, &level);
  113. /* Indirect blocks are never truncated */
  114. if (level != 0)
  115. return __logfs_writepage(page);
  116. /*
  117. * TODO: everything below is a near-verbatim copy of nobh_writepage().
  118. * The relevant bits should be factored out after logfs is merged.
  119. */
  120. /* Is the page fully inside i_size? */
  121. if (bix < end_index)
  122. return __logfs_writepage(page);
  123. /* Is the page fully outside i_size? (truncate in progress) */
  124. offset = i_size & (PAGE_SIZE-1);
  125. if (bix > end_index || offset == 0) {
  126. unlock_page(page);
  127. return 0; /* don't care */
  128. }
  129. /*
  130. * The page straddles i_size. It must be zeroed out on each and every
  131. * writepage invokation because it may be mmapped. "A file is mapped
  132. * in multiples of the page size. For a file that is not a multiple of
  133. * the page size, the remaining memory is zeroed when mapped, and
  134. * writes to that region are not written out to the file."
  135. */
  136. zero_user_segment(page, offset, PAGE_SIZE);
  137. return __logfs_writepage(page);
  138. }
  139. static void logfs_invalidatepage(struct page *page, unsigned int offset,
  140. unsigned int length)
  141. {
  142. struct logfs_block *block = logfs_block(page);
  143. if (block->reserved_bytes) {
  144. struct super_block *sb = page->mapping->host->i_sb;
  145. struct logfs_super *super = logfs_super(sb);
  146. super->s_dirty_pages -= block->reserved_bytes;
  147. block->ops->free_block(sb, block);
  148. BUG_ON(bitmap_weight(block->alias_map, LOGFS_BLOCK_FACTOR));
  149. } else
  150. move_page_to_btree(page);
  151. BUG_ON(PagePrivate(page) || page->private);
  152. }
  153. static int logfs_releasepage(struct page *page, gfp_t only_xfs_uses_this)
  154. {
  155. return 0; /* None of these are easy to release */
  156. }
  157. long logfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  158. {
  159. struct inode *inode = file_inode(file);
  160. struct logfs_inode *li = logfs_inode(inode);
  161. unsigned int oldflags, flags;
  162. int err;
  163. switch (cmd) {
  164. case FS_IOC_GETFLAGS:
  165. flags = li->li_flags & LOGFS_FL_USER_VISIBLE;
  166. return put_user(flags, (int __user *)arg);
  167. case FS_IOC_SETFLAGS:
  168. if (IS_RDONLY(inode))
  169. return -EROFS;
  170. if (!inode_owner_or_capable(inode))
  171. return -EACCES;
  172. err = get_user(flags, (int __user *)arg);
  173. if (err)
  174. return err;
  175. inode_lock(inode);
  176. oldflags = li->li_flags;
  177. flags &= LOGFS_FL_USER_MODIFIABLE;
  178. flags |= oldflags & ~LOGFS_FL_USER_MODIFIABLE;
  179. li->li_flags = flags;
  180. inode_unlock(inode);
  181. inode->i_ctime = current_time(inode);
  182. mark_inode_dirty_sync(inode);
  183. return 0;
  184. default:
  185. return -ENOTTY;
  186. }
  187. }
  188. int logfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  189. {
  190. struct super_block *sb = file->f_mapping->host->i_sb;
  191. struct inode *inode = file->f_mapping->host;
  192. int ret;
  193. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  194. if (ret)
  195. return ret;
  196. inode_lock(inode);
  197. logfs_get_wblocks(sb, NULL, WF_LOCK);
  198. logfs_write_anchor(sb);
  199. logfs_put_wblocks(sb, NULL, WF_LOCK);
  200. inode_unlock(inode);
  201. return 0;
  202. }
  203. static int logfs_setattr(struct dentry *dentry, struct iattr *attr)
  204. {
  205. struct inode *inode = d_inode(dentry);
  206. int err = 0;
  207. err = setattr_prepare(dentry, attr);
  208. if (err)
  209. return err;
  210. if (attr->ia_valid & ATTR_SIZE) {
  211. err = logfs_truncate(inode, attr->ia_size);
  212. if (err)
  213. return err;
  214. }
  215. setattr_copy(inode, attr);
  216. mark_inode_dirty(inode);
  217. return 0;
  218. }
  219. const struct inode_operations logfs_reg_iops = {
  220. .setattr = logfs_setattr,
  221. };
  222. const struct file_operations logfs_reg_fops = {
  223. .read_iter = generic_file_read_iter,
  224. .write_iter = generic_file_write_iter,
  225. .fsync = logfs_fsync,
  226. .unlocked_ioctl = logfs_ioctl,
  227. .llseek = generic_file_llseek,
  228. .mmap = generic_file_readonly_mmap,
  229. .open = generic_file_open,
  230. };
  231. const struct address_space_operations logfs_reg_aops = {
  232. .invalidatepage = logfs_invalidatepage,
  233. .readpage = logfs_readpage,
  234. .releasepage = logfs_releasepage,
  235. .set_page_dirty = __set_page_dirty_nobuffers,
  236. .writepage = logfs_writepage,
  237. .writepages = generic_writepages,
  238. .write_begin = logfs_write_begin,
  239. .write_end = logfs_write_end,
  240. };