file.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. #include <linux/time.h>
  5. #include "reiserfs.h"
  6. #include "acl.h"
  7. #include "xattr.h"
  8. #include <linux/uaccess.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/swap.h>
  11. #include <linux/writeback.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/quotaops.h>
  15. /*
  16. * We pack the tails of files on file close, not at the time they are written.
  17. * This implies an unnecessary copy of the tail and an unnecessary indirect item
  18. * insertion/balancing, for files that are written in one write.
  19. * It avoids unnecessary tail packings (balances) for files that are written in
  20. * multiple writes and are small enough to have tails.
  21. *
  22. * file_release is called by the VFS layer when the file is closed. If
  23. * this is the last open file descriptor, and the file
  24. * small enough to have a tail, and the tail is currently in an
  25. * unformatted node, the tail is converted back into a direct item.
  26. *
  27. * We use reiserfs_truncate_file to pack the tail, since it already has
  28. * all the conditions coded.
  29. */
  30. static int reiserfs_file_release(struct inode *inode, struct file *filp)
  31. {
  32. struct reiserfs_transaction_handle th;
  33. int err;
  34. int jbegin_failure = 0;
  35. BUG_ON(!S_ISREG(inode->i_mode));
  36. if (!atomic_dec_and_mutex_lock(&REISERFS_I(inode)->openers,
  37. &REISERFS_I(inode)->tailpack))
  38. return 0;
  39. /* fast out for when nothing needs to be done */
  40. if ((!(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
  41. !tail_has_to_be_packed(inode)) &&
  42. REISERFS_I(inode)->i_prealloc_count <= 0) {
  43. mutex_unlock(&REISERFS_I(inode)->tailpack);
  44. return 0;
  45. }
  46. reiserfs_write_lock(inode->i_sb);
  47. /*
  48. * freeing preallocation only involves relogging blocks that
  49. * are already in the current transaction. preallocation gets
  50. * freed at the end of each transaction, so it is impossible for
  51. * us to log any additional blocks (including quota blocks)
  52. */
  53. err = journal_begin(&th, inode->i_sb, 1);
  54. if (err) {
  55. /*
  56. * uh oh, we can't allow the inode to go away while there
  57. * is still preallocation blocks pending. Try to join the
  58. * aborted transaction
  59. */
  60. jbegin_failure = err;
  61. err = journal_join_abort(&th, inode->i_sb);
  62. if (err) {
  63. /*
  64. * hmpf, our choices here aren't good. We can pin
  65. * the inode which will disallow unmount from ever
  66. * happening, we can do nothing, which will corrupt
  67. * random memory on unmount, or we can forcibly
  68. * remove the file from the preallocation list, which
  69. * will leak blocks on disk. Lets pin the inode
  70. * and let the admin know what is going on.
  71. */
  72. igrab(inode);
  73. reiserfs_warning(inode->i_sb, "clm-9001",
  74. "pinning inode %lu because the "
  75. "preallocation can't be freed",
  76. inode->i_ino);
  77. goto out;
  78. }
  79. }
  80. reiserfs_update_inode_transaction(inode);
  81. #ifdef REISERFS_PREALLOCATE
  82. reiserfs_discard_prealloc(&th, inode);
  83. #endif
  84. err = journal_end(&th);
  85. /* copy back the error code from journal_begin */
  86. if (!err)
  87. err = jbegin_failure;
  88. if (!err &&
  89. (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
  90. tail_has_to_be_packed(inode)) {
  91. /*
  92. * if regular file is released by last holder and it has been
  93. * appended (we append by unformatted node only) or its direct
  94. * item(s) had to be converted, then it may have to be
  95. * indirect2direct converted
  96. */
  97. err = reiserfs_truncate_file(inode, 0);
  98. }
  99. out:
  100. reiserfs_write_unlock(inode->i_sb);
  101. mutex_unlock(&REISERFS_I(inode)->tailpack);
  102. return err;
  103. }
  104. static int reiserfs_file_open(struct inode *inode, struct file *file)
  105. {
  106. int err = dquot_file_open(inode, file);
  107. /* somebody might be tailpacking on final close; wait for it */
  108. if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) {
  109. mutex_lock(&REISERFS_I(inode)->tailpack);
  110. atomic_inc(&REISERFS_I(inode)->openers);
  111. mutex_unlock(&REISERFS_I(inode)->tailpack);
  112. }
  113. return err;
  114. }
  115. void reiserfs_vfs_truncate_file(struct inode *inode)
  116. {
  117. mutex_lock(&REISERFS_I(inode)->tailpack);
  118. reiserfs_truncate_file(inode, 1);
  119. mutex_unlock(&REISERFS_I(inode)->tailpack);
  120. }
  121. /* Sync a reiserfs file. */
  122. /*
  123. * FIXME: sync_mapping_buffers() never has anything to sync. Can
  124. * be removed...
  125. */
  126. static int reiserfs_sync_file(struct file *filp, loff_t start, loff_t end,
  127. int datasync)
  128. {
  129. struct inode *inode = filp->f_mapping->host;
  130. int err;
  131. int barrier_done;
  132. err = file_write_and_wait_range(filp, start, end);
  133. if (err)
  134. return err;
  135. inode_lock(inode);
  136. BUG_ON(!S_ISREG(inode->i_mode));
  137. err = sync_mapping_buffers(inode->i_mapping);
  138. reiserfs_write_lock(inode->i_sb);
  139. barrier_done = reiserfs_commit_for_inode(inode);
  140. reiserfs_write_unlock(inode->i_sb);
  141. if (barrier_done != 1 && reiserfs_barrier_flush(inode->i_sb))
  142. blkdev_issue_flush(inode->i_sb->s_bdev);
  143. inode_unlock(inode);
  144. if (barrier_done < 0)
  145. return barrier_done;
  146. return (err < 0) ? -EIO : 0;
  147. }
  148. /* taken fs/buffer.c:__block_commit_write */
  149. int reiserfs_commit_page(struct inode *inode, struct page *page,
  150. unsigned from, unsigned to)
  151. {
  152. unsigned block_start, block_end;
  153. int partial = 0;
  154. unsigned blocksize;
  155. struct buffer_head *bh, *head;
  156. unsigned long i_size_index = inode->i_size >> PAGE_SHIFT;
  157. int new;
  158. int logit = reiserfs_file_data_log(inode);
  159. struct super_block *s = inode->i_sb;
  160. int bh_per_page = PAGE_SIZE / s->s_blocksize;
  161. struct reiserfs_transaction_handle th;
  162. int ret = 0;
  163. th.t_trans_id = 0;
  164. blocksize = i_blocksize(inode);
  165. if (logit) {
  166. reiserfs_write_lock(s);
  167. ret = journal_begin(&th, s, bh_per_page + 1);
  168. if (ret)
  169. goto drop_write_lock;
  170. reiserfs_update_inode_transaction(inode);
  171. }
  172. for (bh = head = page_buffers(page), block_start = 0;
  173. bh != head || !block_start;
  174. block_start = block_end, bh = bh->b_this_page) {
  175. new = buffer_new(bh);
  176. clear_buffer_new(bh);
  177. block_end = block_start + blocksize;
  178. if (block_end <= from || block_start >= to) {
  179. if (!buffer_uptodate(bh))
  180. partial = 1;
  181. } else {
  182. set_buffer_uptodate(bh);
  183. if (logit) {
  184. reiserfs_prepare_for_journal(s, bh, 1);
  185. journal_mark_dirty(&th, bh);
  186. } else if (!buffer_dirty(bh)) {
  187. mark_buffer_dirty(bh);
  188. /*
  189. * do data=ordered on any page past the end
  190. * of file and any buffer marked BH_New.
  191. */
  192. if (reiserfs_data_ordered(inode->i_sb) &&
  193. (new || page->index >= i_size_index)) {
  194. reiserfs_add_ordered_list(inode, bh);
  195. }
  196. }
  197. }
  198. }
  199. if (logit) {
  200. ret = journal_end(&th);
  201. drop_write_lock:
  202. reiserfs_write_unlock(s);
  203. }
  204. /*
  205. * If this is a partial write which happened to make all buffers
  206. * uptodate then we can optimize away a bogus read_folio() for
  207. * the next read(). Here we 'discover' whether the page went
  208. * uptodate as a result of this (potentially partial) write.
  209. */
  210. if (!partial)
  211. SetPageUptodate(page);
  212. return ret;
  213. }
  214. const struct file_operations reiserfs_file_operations = {
  215. .unlocked_ioctl = reiserfs_ioctl,
  216. #ifdef CONFIG_COMPAT
  217. .compat_ioctl = reiserfs_compat_ioctl,
  218. #endif
  219. .mmap = generic_file_mmap,
  220. .open = reiserfs_file_open,
  221. .release = reiserfs_file_release,
  222. .fsync = reiserfs_sync_file,
  223. .read_iter = generic_file_read_iter,
  224. .write_iter = generic_file_write_iter,
  225. .splice_read = generic_file_splice_read,
  226. .splice_write = iter_file_splice_write,
  227. .llseek = generic_file_llseek,
  228. };
  229. const struct inode_operations reiserfs_file_inode_operations = {
  230. .setattr = reiserfs_setattr,
  231. .listxattr = reiserfs_listxattr,
  232. .permission = reiserfs_permission,
  233. .get_acl = reiserfs_get_acl,
  234. .set_acl = reiserfs_set_acl,
  235. .fileattr_get = reiserfs_fileattr_get,
  236. .fileattr_set = reiserfs_fileattr_set,
  237. };