mmap.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <cluster/masklog.h>
  33. #include "ocfs2.h"
  34. #include "aops.h"
  35. #include "dlmglue.h"
  36. #include "file.h"
  37. #include "inode.h"
  38. #include "mmap.h"
  39. #include "super.h"
  40. #include "ocfs2_trace.h"
  41. static vm_fault_t ocfs2_fault(struct vm_fault *vmf)
  42. {
  43. struct vm_area_struct *vma = vmf->vma;
  44. sigset_t oldset;
  45. vm_fault_t ret;
  46. ocfs2_block_signals(&oldset);
  47. ret = filemap_fault(vmf);
  48. ocfs2_unblock_signals(&oldset);
  49. trace_ocfs2_fault(OCFS2_I(vma->vm_file->f_mapping->host)->ip_blkno,
  50. vma, vmf->page, vmf->pgoff);
  51. return ret;
  52. }
  53. static vm_fault_t __ocfs2_page_mkwrite(struct file *file,
  54. struct buffer_head *di_bh, struct page *page)
  55. {
  56. int err;
  57. vm_fault_t ret = VM_FAULT_NOPAGE;
  58. struct inode *inode = file_inode(file);
  59. struct address_space *mapping = inode->i_mapping;
  60. loff_t pos = page_offset(page);
  61. unsigned int len = PAGE_SIZE;
  62. pgoff_t last_index;
  63. struct page *locked_page = NULL;
  64. void *fsdata;
  65. loff_t size = i_size_read(inode);
  66. last_index = (size - 1) >> PAGE_SHIFT;
  67. /*
  68. * There are cases that lead to the page no longer bebongs to the
  69. * mapping.
  70. * 1) pagecache truncates locally due to memory pressure.
  71. * 2) pagecache truncates when another is taking EX lock against
  72. * inode lock. see ocfs2_data_convert_worker.
  73. *
  74. * The i_size check doesn't catch the case where nodes truncated and
  75. * then re-extended the file. We'll re-check the page mapping after
  76. * taking the page lock inside of ocfs2_write_begin_nolock().
  77. *
  78. * Let VM retry with these cases.
  79. */
  80. if ((page->mapping != inode->i_mapping) ||
  81. (!PageUptodate(page)) ||
  82. (page_offset(page) >= size))
  83. goto out;
  84. /*
  85. * Call ocfs2_write_begin() and ocfs2_write_end() to take
  86. * advantage of the allocation code there. We pass a write
  87. * length of the whole page (chopped to i_size) to make sure
  88. * the whole thing is allocated.
  89. *
  90. * Since we know the page is up to date, we don't have to
  91. * worry about ocfs2_write_begin() skipping some buffer reads
  92. * because the "write" would invalidate their data.
  93. */
  94. if (page->index == last_index)
  95. len = ((size - 1) & ~PAGE_MASK) + 1;
  96. err = ocfs2_write_begin_nolock(mapping, pos, len, OCFS2_WRITE_MMAP,
  97. &locked_page, &fsdata, di_bh, page);
  98. if (err) {
  99. if (err != -ENOSPC)
  100. mlog_errno(err);
  101. ret = vmf_error(err);
  102. goto out;
  103. }
  104. if (!locked_page) {
  105. ret = VM_FAULT_NOPAGE;
  106. goto out;
  107. }
  108. err = ocfs2_write_end_nolock(mapping, pos, len, len, fsdata);
  109. BUG_ON(err != len);
  110. ret = VM_FAULT_LOCKED;
  111. out:
  112. return ret;
  113. }
  114. static vm_fault_t ocfs2_page_mkwrite(struct vm_fault *vmf)
  115. {
  116. struct page *page = vmf->page;
  117. struct inode *inode = file_inode(vmf->vma->vm_file);
  118. struct buffer_head *di_bh = NULL;
  119. sigset_t oldset;
  120. int err;
  121. vm_fault_t ret;
  122. sb_start_pagefault(inode->i_sb);
  123. ocfs2_block_signals(&oldset);
  124. /*
  125. * The cluster locks taken will block a truncate from another
  126. * node. Taking the data lock will also ensure that we don't
  127. * attempt page truncation as part of a downconvert.
  128. */
  129. err = ocfs2_inode_lock(inode, &di_bh, 1);
  130. if (err < 0) {
  131. mlog_errno(err);
  132. ret = vmf_error(err);
  133. goto out;
  134. }
  135. /*
  136. * The alloc sem should be enough to serialize with
  137. * ocfs2_truncate_file() changing i_size as well as any thread
  138. * modifying the inode btree.
  139. */
  140. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  141. ret = __ocfs2_page_mkwrite(vmf->vma->vm_file, di_bh, page);
  142. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  143. brelse(di_bh);
  144. ocfs2_inode_unlock(inode, 1);
  145. out:
  146. ocfs2_unblock_signals(&oldset);
  147. sb_end_pagefault(inode->i_sb);
  148. return ret;
  149. }
  150. static const struct vm_operations_struct ocfs2_file_vm_ops = {
  151. .fault = ocfs2_fault,
  152. .page_mkwrite = ocfs2_page_mkwrite,
  153. };
  154. int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
  155. {
  156. int ret = 0, lock_level = 0;
  157. ret = ocfs2_inode_lock_atime(file_inode(file),
  158. file->f_path.mnt, &lock_level, 1);
  159. if (ret < 0) {
  160. mlog_errno(ret);
  161. goto out;
  162. }
  163. ocfs2_inode_unlock(file_inode(file), lock_level);
  164. out:
  165. vma->vm_ops = &ocfs2_file_vm_ops;
  166. return 0;
  167. }