mmap.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include "super.h"
  41. static int ocfs2_fault(struct vm_area_struct *area, struct vm_fault *vmf)
  42. {
  43. sigset_t oldset;
  44. int ret;
  45. mlog_entry("(area=%p, page offset=%lu)\n", area, vmf->pgoff);
  46. ocfs2_block_signals(&oldset);
  47. ret = filemap_fault(area, vmf);
  48. ocfs2_unblock_signals(&oldset);
  49. mlog_exit_ptr(vmf->page);
  50. return ret;
  51. }
  52. static int __ocfs2_page_mkwrite(struct inode *inode, struct buffer_head *di_bh,
  53. struct page *page)
  54. {
  55. int ret;
  56. struct address_space *mapping = inode->i_mapping;
  57. loff_t pos = page_offset(page);
  58. unsigned int len = PAGE_CACHE_SIZE;
  59. pgoff_t last_index;
  60. struct page *locked_page = NULL;
  61. void *fsdata;
  62. loff_t size = i_size_read(inode);
  63. /*
  64. * Another node might have truncated while we were waiting on
  65. * cluster locks.
  66. * We don't check size == 0 before the shift. This is borrowed
  67. * from do_generic_file_read.
  68. */
  69. last_index = (size - 1) >> PAGE_CACHE_SHIFT;
  70. if (unlikely(!size || page->index > last_index)) {
  71. ret = -EINVAL;
  72. goto out;
  73. }
  74. /*
  75. * The i_size check above doesn't catch the case where nodes
  76. * truncated and then re-extended the file. We'll re-check the
  77. * page mapping after taking the page lock inside of
  78. * ocfs2_write_begin_nolock().
  79. */
  80. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  81. /*
  82. * the page has been umapped in ocfs2_data_downconvert_worker.
  83. * So return 0 here and let VFS retry.
  84. */
  85. ret = 0;
  86. goto out;
  87. }
  88. /*
  89. * Call ocfs2_write_begin() and ocfs2_write_end() to take
  90. * advantage of the allocation code there. We pass a write
  91. * length of the whole page (chopped to i_size) to make sure
  92. * the whole thing is allocated.
  93. *
  94. * Since we know the page is up to date, we don't have to
  95. * worry about ocfs2_write_begin() skipping some buffer reads
  96. * because the "write" would invalidate their data.
  97. */
  98. if (page->index == last_index)
  99. len = ((size - 1) & ~PAGE_CACHE_MASK) + 1;
  100. ret = ocfs2_write_begin_nolock(mapping, pos, len, 0, &locked_page,
  101. &fsdata, di_bh, page);
  102. if (ret) {
  103. if (ret != -ENOSPC)
  104. mlog_errno(ret);
  105. goto out;
  106. }
  107. ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page,
  108. fsdata);
  109. if (ret < 0) {
  110. mlog_errno(ret);
  111. goto out;
  112. }
  113. BUG_ON(ret != len);
  114. ret = 0;
  115. out:
  116. return ret;
  117. }
  118. static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  119. {
  120. struct page *page = vmf->page;
  121. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  122. struct buffer_head *di_bh = NULL;
  123. sigset_t oldset;
  124. int ret;
  125. ocfs2_block_signals(&oldset);
  126. /*
  127. * The cluster locks taken will block a truncate from another
  128. * node. Taking the data lock will also ensure that we don't
  129. * attempt page truncation as part of a downconvert.
  130. */
  131. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  132. if (ret < 0) {
  133. mlog_errno(ret);
  134. goto out;
  135. }
  136. /*
  137. * The alloc sem should be enough to serialize with
  138. * ocfs2_truncate_file() changing i_size as well as any thread
  139. * modifying the inode btree.
  140. */
  141. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  142. ret = __ocfs2_page_mkwrite(inode, di_bh, page);
  143. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  144. brelse(di_bh);
  145. ocfs2_inode_unlock(inode, 1);
  146. out:
  147. ocfs2_unblock_signals(&oldset);
  148. if (ret)
  149. ret = VM_FAULT_SIGBUS;
  150. return ret;
  151. }
  152. static const struct vm_operations_struct ocfs2_file_vm_ops = {
  153. .fault = ocfs2_fault,
  154. .page_mkwrite = ocfs2_page_mkwrite,
  155. };
  156. int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
  157. {
  158. int ret = 0, lock_level = 0;
  159. ret = ocfs2_inode_lock_atime(file->f_dentry->d_inode,
  160. file->f_vfsmnt, &lock_level);
  161. if (ret < 0) {
  162. mlog_errno(ret);
  163. goto out;
  164. }
  165. ocfs2_inode_unlock(file->f_dentry->d_inode, lock_level);
  166. out:
  167. vma->vm_ops = &ocfs2_file_vm_ops;
  168. vma->vm_flags |= VM_CAN_NONLINEAR;
  169. return 0;
  170. }