mmap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/uio.h>
  31. #include <linux/signal.h>
  32. #include <linux/rbtree.h>
  33. #define MLOG_MASK_PREFIX ML_FILE_IO
  34. #include <cluster/masklog.h>
  35. #include "ocfs2.h"
  36. #include "aops.h"
  37. #include "dlmglue.h"
  38. #include "file.h"
  39. #include "inode.h"
  40. #include "mmap.h"
  41. static inline int ocfs2_vm_op_block_sigs(sigset_t *blocked, sigset_t *oldset)
  42. {
  43. /* The best way to deal with signals in the vm path is
  44. * to block them upfront, rather than allowing the
  45. * locking paths to return -ERESTARTSYS. */
  46. sigfillset(blocked);
  47. /* We should technically never get a bad return value
  48. * from sigprocmask */
  49. return sigprocmask(SIG_BLOCK, blocked, oldset);
  50. }
  51. static inline int ocfs2_vm_op_unblock_sigs(sigset_t *oldset)
  52. {
  53. return sigprocmask(SIG_SETMASK, oldset, NULL);
  54. }
  55. static struct page *ocfs2_fault(struct vm_area_struct *area,
  56. struct fault_data *fdata)
  57. {
  58. struct page *page = NULL;
  59. sigset_t blocked, oldset;
  60. int ret;
  61. mlog_entry("(area=%p, page offset=%lu)\n", area, fdata->pgoff);
  62. ret = ocfs2_vm_op_block_sigs(&blocked, &oldset);
  63. if (ret < 0) {
  64. fdata->type = VM_FAULT_SIGBUS;
  65. mlog_errno(ret);
  66. goto out;
  67. }
  68. page = filemap_fault(area, fdata);
  69. ret = ocfs2_vm_op_unblock_sigs(&oldset);
  70. if (ret < 0)
  71. mlog_errno(ret);
  72. out:
  73. mlog_exit_ptr(page);
  74. return page;
  75. }
  76. static int __ocfs2_page_mkwrite(struct inode *inode, struct buffer_head *di_bh,
  77. struct page *page)
  78. {
  79. int ret;
  80. struct address_space *mapping = inode->i_mapping;
  81. loff_t pos = page->index << PAGE_CACHE_SHIFT;
  82. unsigned int len = PAGE_CACHE_SIZE;
  83. pgoff_t last_index;
  84. struct page *locked_page = NULL;
  85. void *fsdata;
  86. loff_t size = i_size_read(inode);
  87. /*
  88. * Another node might have truncated while we were waiting on
  89. * cluster locks.
  90. */
  91. last_index = size >> PAGE_CACHE_SHIFT;
  92. if (page->index > last_index) {
  93. ret = -EINVAL;
  94. goto out;
  95. }
  96. /*
  97. * The i_size check above doesn't catch the case where nodes
  98. * truncated and then re-extended the file. We'll re-check the
  99. * page mapping after taking the page lock inside of
  100. * ocfs2_write_begin_nolock().
  101. */
  102. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  103. ret = -EINVAL;
  104. goto out;
  105. }
  106. /*
  107. * Call ocfs2_write_begin() and ocfs2_write_end() to take
  108. * advantage of the allocation code there. We pass a write
  109. * length of the whole page (chopped to i_size) to make sure
  110. * the whole thing is allocated.
  111. *
  112. * Since we know the page is up to date, we don't have to
  113. * worry about ocfs2_write_begin() skipping some buffer reads
  114. * because the "write" would invalidate their data.
  115. */
  116. if (page->index == last_index)
  117. len = size & ~PAGE_CACHE_MASK;
  118. ret = ocfs2_write_begin_nolock(mapping, pos, len, 0, &locked_page,
  119. &fsdata, di_bh, page);
  120. if (ret) {
  121. if (ret != -ENOSPC)
  122. mlog_errno(ret);
  123. goto out;
  124. }
  125. ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page,
  126. fsdata);
  127. if (ret < 0) {
  128. mlog_errno(ret);
  129. goto out;
  130. }
  131. BUG_ON(ret != len);
  132. ret = 0;
  133. out:
  134. return ret;
  135. }
  136. static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  137. {
  138. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  139. struct buffer_head *di_bh = NULL;
  140. sigset_t blocked, oldset;
  141. int ret, ret2;
  142. ret = ocfs2_vm_op_block_sigs(&blocked, &oldset);
  143. if (ret < 0) {
  144. mlog_errno(ret);
  145. return ret;
  146. }
  147. /*
  148. * The cluster locks taken will block a truncate from another
  149. * node. Taking the data lock will also ensure that we don't
  150. * attempt page truncation as part of a downconvert.
  151. */
  152. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  153. if (ret < 0) {
  154. mlog_errno(ret);
  155. goto out;
  156. }
  157. /*
  158. * The alloc sem should be enough to serialize with
  159. * ocfs2_truncate_file() changing i_size as well as any thread
  160. * modifying the inode btree.
  161. */
  162. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  163. ret = ocfs2_data_lock(inode, 1);
  164. if (ret < 0) {
  165. mlog_errno(ret);
  166. goto out_meta_unlock;
  167. }
  168. ret = __ocfs2_page_mkwrite(inode, di_bh, page);
  169. ocfs2_data_unlock(inode, 1);
  170. out_meta_unlock:
  171. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  172. brelse(di_bh);
  173. ocfs2_meta_unlock(inode, 1);
  174. out:
  175. ret2 = ocfs2_vm_op_unblock_sigs(&oldset);
  176. if (ret2 < 0)
  177. mlog_errno(ret2);
  178. return ret;
  179. }
  180. static struct vm_operations_struct ocfs2_file_vm_ops = {
  181. .fault = ocfs2_fault,
  182. .page_mkwrite = ocfs2_page_mkwrite,
  183. };
  184. int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
  185. {
  186. int ret = 0, lock_level = 0;
  187. ret = ocfs2_meta_lock_atime(file->f_dentry->d_inode,
  188. file->f_vfsmnt, &lock_level);
  189. if (ret < 0) {
  190. mlog_errno(ret);
  191. goto out;
  192. }
  193. ocfs2_meta_unlock(file->f_dentry->d_inode, lock_level);
  194. out:
  195. vma->vm_ops = &ocfs2_file_vm_ops;
  196. vma->vm_flags |= VM_CAN_INVALIDATE | VM_CAN_NONLINEAR;
  197. return 0;
  198. }