file.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_CACHE_SHIFT;
  18. page = grab_cache_page_write_begin(mapping, index, flags);
  19. if (!page)
  20. return -ENOMEM;
  21. *pagep = page;
  22. if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
  23. return 0;
  24. if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
  25. unsigned start = pos & (PAGE_CACHE_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_CACHE_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_CACHE_SIZE - 1);
  40. unsigned end = start + copied;
  41. int ret = 0;
  42. BUG_ON(PAGE_CACHE_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_CACHE_SHIFT) + end) {
  57. i_size_write(inode, (index << PAGE_CACHE_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. page_cache_release(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_CACHE_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_CACHE_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_CACHE_SIZE);
  137. return __logfs_writepage(page);
  138. }
  139. static void logfs_invalidatepage(struct page *page, unsigned long offset)
  140. {
  141. struct logfs_block *block = logfs_block(page);
  142. if (block->reserved_bytes) {
  143. struct super_block *sb = page->mapping->host->i_sb;
  144. struct logfs_super *super = logfs_super(sb);
  145. super->s_dirty_pages -= block->reserved_bytes;
  146. block->ops->free_block(sb, block);
  147. BUG_ON(bitmap_weight(block->alias_map, LOGFS_BLOCK_FACTOR));
  148. } else
  149. move_page_to_btree(page);
  150. BUG_ON(PagePrivate(page) || page->private);
  151. }
  152. static int logfs_releasepage(struct page *page, gfp_t only_xfs_uses_this)
  153. {
  154. return 0; /* None of these are easy to release */
  155. }
  156. long logfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  157. {
  158. struct inode *inode = file->f_path.dentry->d_inode;
  159. struct logfs_inode *li = logfs_inode(inode);
  160. unsigned int oldflags, flags;
  161. int err;
  162. switch (cmd) {
  163. case FS_IOC_GETFLAGS:
  164. flags = li->li_flags & LOGFS_FL_USER_VISIBLE;
  165. return put_user(flags, (int __user *)arg);
  166. case FS_IOC_SETFLAGS:
  167. if (IS_RDONLY(inode))
  168. return -EROFS;
  169. if (!inode_owner_or_capable(inode))
  170. return -EACCES;
  171. err = get_user(flags, (int __user *)arg);
  172. if (err)
  173. return err;
  174. mutex_lock(&inode->i_mutex);
  175. oldflags = li->li_flags;
  176. flags &= LOGFS_FL_USER_MODIFIABLE;
  177. flags |= oldflags & ~LOGFS_FL_USER_MODIFIABLE;
  178. li->li_flags = flags;
  179. mutex_unlock(&inode->i_mutex);
  180. inode->i_ctime = CURRENT_TIME;
  181. mark_inode_dirty_sync(inode);
  182. return 0;
  183. default:
  184. return -ENOTTY;
  185. }
  186. }
  187. int logfs_fsync(struct file *file, int datasync)
  188. {
  189. struct super_block *sb = file->f_mapping->host->i_sb;
  190. logfs_write_anchor(sb);
  191. return 0;
  192. }
  193. static int logfs_setattr(struct dentry *dentry, struct iattr *attr)
  194. {
  195. struct inode *inode = dentry->d_inode;
  196. int err = 0;
  197. err = inode_change_ok(inode, attr);
  198. if (err)
  199. return err;
  200. if (attr->ia_valid & ATTR_SIZE) {
  201. err = logfs_truncate(inode, attr->ia_size);
  202. if (err)
  203. return err;
  204. }
  205. setattr_copy(inode, attr);
  206. mark_inode_dirty(inode);
  207. return 0;
  208. }
  209. const struct inode_operations logfs_reg_iops = {
  210. .setattr = logfs_setattr,
  211. };
  212. const struct file_operations logfs_reg_fops = {
  213. .aio_read = generic_file_aio_read,
  214. .aio_write = generic_file_aio_write,
  215. .fsync = logfs_fsync,
  216. .unlocked_ioctl = logfs_ioctl,
  217. .llseek = generic_file_llseek,
  218. .mmap = generic_file_readonly_mmap,
  219. .open = generic_file_open,
  220. .read = do_sync_read,
  221. .write = do_sync_write,
  222. };
  223. const struct address_space_operations logfs_reg_aops = {
  224. .invalidatepage = logfs_invalidatepage,
  225. .readpage = logfs_readpage,
  226. .releasepage = logfs_releasepage,
  227. .set_page_dirty = __set_page_dirty_nobuffers,
  228. .writepage = logfs_writepage,
  229. .writepages = generic_writepages,
  230. .write_begin = logfs_write_begin,
  231. .write_end = logfs_write_end,
  232. };