mmap.c 4.3 KB

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