blob: 3a44e461828aaf67ce900ee04a7f123fe85f3191 [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Fashehccd979b2005-12-15 14:31:24 -08002/* -*- 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.
Mark Fashehccd979b2005-12-15 14:31:24 -080010 */
11
12#include <linux/fs.h>
13#include <linux/types.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080014#include <linux/highmem.h>
15#include <linux/pagemap.h>
16#include <linux/uio.h>
17#include <linux/signal.h>
18#include <linux/rbtree.h>
19
Mark Fashehccd979b2005-12-15 14:31:24 -080020#include <cluster/masklog.h>
21
22#include "ocfs2.h"
23
Mark Fasheh7307de82007-05-09 15:16:19 -070024#include "aops.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080025#include "dlmglue.h"
26#include "file.h"
27#include "inode.h"
28#include "mmap.h"
Joel Beckere4b963f2009-09-02 17:17:36 -070029#include "super.h"
Tao Ma614a9e82011-02-22 21:59:46 +080030#include "ocfs2_trace.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080031
Mark Fasheh7307de82007-05-09 15:16:19 -070032
Souptick Joarderc6137fe2018-06-07 17:04:59 -070033static vm_fault_t ocfs2_fault(struct vm_fault *vmf)
Mark Fashehccd979b2005-12-15 14:31:24 -080034{
Dave Jiang11bac802017-02-24 14:56:41 -080035 struct vm_area_struct *vma = vmf->vma;
Joel Beckere4b963f2009-09-02 17:17:36 -070036 sigset_t oldset;
Souptick Joarderc6137fe2018-06-07 17:04:59 -070037 vm_fault_t ret;
Mark Fashehccd979b2005-12-15 14:31:24 -080038
Joel Beckere4b963f2009-09-02 17:17:36 -070039 ocfs2_block_signals(&oldset);
Dave Jiang11bac802017-02-24 14:56:41 -080040 ret = filemap_fault(vmf);
Joel Beckere4b963f2009-09-02 17:17:36 -070041 ocfs2_unblock_signals(&oldset);
Mark Fashehccd979b2005-12-15 14:31:24 -080042
Dave Jiang11bac802017-02-24 14:56:41 -080043 trace_ocfs2_fault(OCFS2_I(vma->vm_file->f_mapping->host)->ip_blkno,
44 vma, vmf->page, vmf->pgoff);
Nick Piggind0217ac2007-07-19 01:47:03 -070045 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -080046}
47
Souptick Joarderc6137fe2018-06-07 17:04:59 -070048static vm_fault_t __ocfs2_page_mkwrite(struct file *file,
49 struct buffer_head *di_bh, struct page *page)
Mark Fasheh7307de82007-05-09 15:16:19 -070050{
Souptick Joarderc6137fe2018-06-07 17:04:59 -070051 int err;
52 vm_fault_t ret = VM_FAULT_NOPAGE;
Al Viro496ad9a2013-01-23 17:07:38 -050053 struct inode *inode = file_inode(file);
Mark Fasheh7307de82007-05-09 15:16:19 -070054 struct address_space *mapping = inode->i_mapping;
Nick Piggin18336332007-07-20 00:31:45 -070055 loff_t pos = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030056 unsigned int len = PAGE_SIZE;
Mark Fasheh7307de82007-05-09 15:16:19 -070057 pgoff_t last_index;
58 struct page *locked_page = NULL;
59 void *fsdata;
60 loff_t size = i_size_read(inode);
61
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030062 last_index = (size - 1) >> PAGE_SHIFT;
Mark Fasheh7307de82007-05-09 15:16:19 -070063
64 /*
Wengang Wang5cffff92011-07-24 10:36:54 -070065 * There are cases that lead to the page no longer bebongs to the
66 * mapping.
67 * 1) pagecache truncates locally due to memory pressure.
68 * 2) pagecache truncates when another is taking EX lock against
69 * inode lock. see ocfs2_data_convert_worker.
70 *
71 * The i_size check doesn't catch the case where nodes truncated and
72 * then re-extended the file. We'll re-check the page mapping after
73 * taking the page lock inside of ocfs2_write_begin_nolock().
74 *
75 * Let VM retry with these cases.
Mark Fasheh7307de82007-05-09 15:16:19 -070076 */
Wengang Wang5cffff92011-07-24 10:36:54 -070077 if ((page->mapping != inode->i_mapping) ||
78 (!PageUptodate(page)) ||
79 (page_offset(page) >= size))
Mark Fasheh7307de82007-05-09 15:16:19 -070080 goto out;
Mark Fasheh7307de82007-05-09 15:16:19 -070081
82 /*
83 * Call ocfs2_write_begin() and ocfs2_write_end() to take
84 * advantage of the allocation code there. We pass a write
85 * length of the whole page (chopped to i_size) to make sure
86 * the whole thing is allocated.
87 *
88 * Since we know the page is up to date, we don't have to
89 * worry about ocfs2_write_begin() skipping some buffer reads
90 * because the "write" would invalidate their data.
91 */
92 if (page->index == last_index)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030093 len = ((size - 1) & ~PAGE_MASK) + 1;
Mark Fasheh7307de82007-05-09 15:16:19 -070094
Souptick Joarderc6137fe2018-06-07 17:04:59 -070095 err = ocfs2_write_begin_nolock(mapping, pos, len, OCFS2_WRITE_MMAP,
Ryan Dingc1ad1e32016-03-25 14:20:52 -070096 &locked_page, &fsdata, di_bh, page);
Souptick Joarderc6137fe2018-06-07 17:04:59 -070097 if (err) {
98 if (err != -ENOSPC)
99 mlog_errno(err);
100 ret = vmf_error(err);
Mark Fasheh7307de82007-05-09 15:16:19 -0700101 goto out;
102 }
103
Wengang Wang5cffff92011-07-24 10:36:54 -0700104 if (!locked_page) {
105 ret = VM_FAULT_NOPAGE;
Mark Fasheh7307de82007-05-09 15:16:19 -0700106 goto out;
107 }
Souptick Joarderc6137fe2018-06-07 17:04:59 -0700108 err = ocfs2_write_end_nolock(mapping, pos, len, len, fsdata);
109 BUG_ON(err != len);
Wengang Wang5cffff92011-07-24 10:36:54 -0700110 ret = VM_FAULT_LOCKED;
Mark Fasheh7307de82007-05-09 15:16:19 -0700111out:
112 return ret;
113}
114
Souptick Joarderc6137fe2018-06-07 17:04:59 -0700115static vm_fault_t ocfs2_page_mkwrite(struct vm_fault *vmf)
Mark Fasheh7307de82007-05-09 15:16:19 -0700116{
Nick Pigginc2ec1752009-03-31 15:23:21 -0700117 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -0800118 struct inode *inode = file_inode(vmf->vma->vm_file);
Mark Fasheh7307de82007-05-09 15:16:19 -0700119 struct buffer_head *di_bh = NULL;
Joel Beckere4b963f2009-09-02 17:17:36 -0700120 sigset_t oldset;
Souptick Joarderc6137fe2018-06-07 17:04:59 -0700121 int err;
122 vm_fault_t ret;
Mark Fasheh7307de82007-05-09 15:16:19 -0700123
Jan Karafef69252012-06-12 16:20:40 +0200124 sb_start_pagefault(inode->i_sb);
Joel Beckere4b963f2009-09-02 17:17:36 -0700125 ocfs2_block_signals(&oldset);
Mark Fasheh7307de82007-05-09 15:16:19 -0700126
127 /*
128 * The cluster locks taken will block a truncate from another
129 * node. Taking the data lock will also ensure that we don't
130 * attempt page truncation as part of a downconvert.
131 */
Souptick Joarderc6137fe2018-06-07 17:04:59 -0700132 err = ocfs2_inode_lock(inode, &di_bh, 1);
133 if (err < 0) {
134 mlog_errno(err);
135 ret = vmf_error(err);
Mark Fasheh7307de82007-05-09 15:16:19 -0700136 goto out;
137 }
138
139 /*
140 * The alloc sem should be enough to serialize with
141 * ocfs2_truncate_file() changing i_size as well as any thread
142 * modifying the inode btree.
143 */
144 down_write(&OCFS2_I(inode)->ip_alloc_sem);
145
Dave Jiang11bac802017-02-24 14:56:41 -0800146 ret = __ocfs2_page_mkwrite(vmf->vma->vm_file, di_bh, page);
Mark Fasheh7307de82007-05-09 15:16:19 -0700147
Mark Fasheh7307de82007-05-09 15:16:19 -0700148 up_write(&OCFS2_I(inode)->ip_alloc_sem);
149
150 brelse(di_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700151 ocfs2_inode_unlock(inode, 1);
Mark Fasheh7307de82007-05-09 15:16:19 -0700152
153out:
Joel Beckere4b963f2009-09-02 17:17:36 -0700154 ocfs2_unblock_signals(&oldset);
Jan Karafef69252012-06-12 16:20:40 +0200155 sb_end_pagefault(inode->i_sb);
Mark Fasheh7307de82007-05-09 15:16:19 -0700156 return ret;
157}
158
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400159static const struct vm_operations_struct ocfs2_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -0700160 .fault = ocfs2_fault,
Mark Fasheh7307de82007-05-09 15:16:19 -0700161 .page_mkwrite = ocfs2_page_mkwrite,
Mark Fashehccd979b2005-12-15 14:31:24 -0800162};
163
Christoph Hellwig870f4812006-01-09 20:52:01 -0800164int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
Mark Fashehccd979b2005-12-15 14:31:24 -0800165{
Tiger Yang25899de2006-11-15 15:49:02 +0800166 int ret = 0, lock_level = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800167
Al Viro496ad9a2013-01-23 17:07:38 -0500168 ret = ocfs2_inode_lock_atime(file_inode(file),
Gang Hec4c24162018-01-31 16:15:25 -0800169 file->f_path.mnt, &lock_level, 1);
Tiger Yang25899de2006-11-15 15:49:02 +0800170 if (ret < 0) {
171 mlog_errno(ret);
172 goto out;
173 }
Al Viro496ad9a2013-01-23 17:07:38 -0500174 ocfs2_inode_unlock(file_inode(file), lock_level);
Tiger Yang25899de2006-11-15 15:49:02 +0800175out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800176 vma->vm_ops = &ocfs2_file_vm_ops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800177 return 0;
178}
179