mmap.c 4.3 KB

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