mmap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * mmap.c
  5. *
  6. * Code to deal with the mess that is clustered mmap.
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/highmem.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/uio.h>
  30. #include <linux/signal.h>
  31. #include <linux/rbtree.h>
  32. #define MLOG_MASK_PREFIX ML_FILE_IO
  33. #include <cluster/masklog.h>
  34. #include "ocfs2.h"
  35. #include "aops.h"
  36. #include "dlmglue.h"
  37. #include "file.h"
  38. #include "inode.h"
  39. #include "mmap.h"
  40. static inline int ocfs2_vm_op_block_sigs(sigset_t *blocked, sigset_t *oldset)
  41. {
  42. /* The best way to deal with signals in the vm path is
  43. * to block them upfront, rather than allowing the
  44. * locking paths to return -ERESTARTSYS. */
  45. sigfillset(blocked);
  46. /* We should technically never get a bad return value
  47. * from sigprocmask */
  48. return sigprocmask(SIG_BLOCK, blocked, oldset);
  49. }
  50. static inline int ocfs2_vm_op_unblock_sigs(sigset_t *oldset)
  51. {
  52. return sigprocmask(SIG_SETMASK, oldset, NULL);
  53. }
  54. static int ocfs2_fault(struct vm_area_struct *area, struct vm_fault *vmf)
  55. {
  56. sigset_t blocked, oldset;
  57. int error, ret;
  58. mlog_entry("(area=%p, page offset=%lu)\n", area, vmf->pgoff);
  59. error = ocfs2_vm_op_block_sigs(&blocked, &oldset);
  60. if (error < 0) {
  61. mlog_errno(error);
  62. ret = VM_FAULT_SIGBUS;
  63. goto out;
  64. }
  65. ret = filemap_fault(area, vmf);
  66. error = ocfs2_vm_op_unblock_sigs(&oldset);
  67. if (error < 0)
  68. mlog_errno(error);
  69. out:
  70. mlog_exit_ptr(vmf->page);
  71. return ret;
  72. }
  73. static int __ocfs2_page_mkwrite(struct inode *inode, struct buffer_head *di_bh,
  74. struct page *page)
  75. {
  76. int ret;
  77. struct address_space *mapping = inode->i_mapping;
  78. loff_t pos = page_offset(page);
  79. unsigned int len = PAGE_CACHE_SIZE;
  80. pgoff_t last_index;
  81. struct page *locked_page = NULL;
  82. void *fsdata;
  83. loff_t size = i_size_read(inode);
  84. /*
  85. * Another node might have truncated while we were waiting on
  86. * cluster locks.
  87. */
  88. last_index = size >> PAGE_CACHE_SHIFT;
  89. if (page->index > last_index) {
  90. ret = -EINVAL;
  91. goto out;
  92. }
  93. /*
  94. * The i_size check above doesn't catch the case where nodes
  95. * truncated and then re-extended the file. We'll re-check the
  96. * page mapping after taking the page lock inside of
  97. * ocfs2_write_begin_nolock().
  98. */
  99. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  100. /*
  101. * the page has been umapped in ocfs2_data_downconvert_worker.
  102. * So return 0 here and let VFS retry.
  103. */
  104. ret = 0;
  105. goto out;
  106. }
  107. /*
  108. * Call ocfs2_write_begin() and ocfs2_write_end() to take
  109. * advantage of the allocation code there. We pass a write
  110. * length of the whole page (chopped to i_size) to make sure
  111. * the whole thing is allocated.
  112. *
  113. * Since we know the page is up to date, we don't have to
  114. * worry about ocfs2_write_begin() skipping some buffer reads
  115. * because the "write" would invalidate their data.
  116. */
  117. if (page->index == last_index)
  118. len = size & ~PAGE_CACHE_MASK;
  119. ret = ocfs2_write_begin_nolock(mapping, pos, len, 0, &locked_page,
  120. &fsdata, di_bh, page);
  121. if (ret) {
  122. if (ret != -ENOSPC)
  123. mlog_errno(ret);
  124. goto out;
  125. }
  126. ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page,
  127. fsdata);
  128. if (ret < 0) {
  129. mlog_errno(ret);
  130. goto out;
  131. }
  132. BUG_ON(ret != len);
  133. ret = 0;
  134. out:
  135. return ret;
  136. }
  137. static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  138. {
  139. struct page *page = vmf->page;
  140. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  141. struct buffer_head *di_bh = NULL;
  142. sigset_t blocked, oldset;
  143. int ret, ret2;
  144. ret = ocfs2_vm_op_block_sigs(&blocked, &oldset);
  145. if (ret < 0) {
  146. mlog_errno(ret);
  147. return ret;
  148. }
  149. /*
  150. * The cluster locks taken will block a truncate from another
  151. * node. Taking the data lock will also ensure that we don't
  152. * attempt page truncation as part of a downconvert.
  153. */
  154. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  155. if (ret < 0) {
  156. mlog_errno(ret);
  157. goto out;
  158. }
  159. /*
  160. * The alloc sem should be enough to serialize with
  161. * ocfs2_truncate_file() changing i_size as well as any thread
  162. * modifying the inode btree.
  163. */
  164. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  165. ret = __ocfs2_page_mkwrite(inode, di_bh, page);
  166. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  167. brelse(di_bh);
  168. ocfs2_inode_unlock(inode, 1);
  169. out:
  170. ret2 = ocfs2_vm_op_unblock_sigs(&oldset);
  171. if (ret2 < 0)
  172. mlog_errno(ret2);
  173. if (ret)
  174. ret = VM_FAULT_SIGBUS;
  175. return ret;
  176. }
  177. static const struct vm_operations_struct ocfs2_file_vm_ops = {
  178. .fault = ocfs2_fault,
  179. .page_mkwrite = ocfs2_page_mkwrite,
  180. };
  181. int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
  182. {
  183. int ret = 0, lock_level = 0;
  184. ret = ocfs2_inode_lock_atime(file->f_dentry->d_inode,
  185. file->f_vfsmnt, &lock_level);
  186. if (ret < 0) {
  187. mlog_errno(ret);
  188. goto out;
  189. }
  190. ocfs2_inode_unlock(file->f_dentry->d_inode, lock_level);
  191. out:
  192. vma->vm_ops = &ocfs2_file_vm_ops;
  193. vma->vm_flags |= VM_CAN_NONLINEAR;
  194. return 0;
  195. }