xref: /openbmc/linux/fs/ocfs2/mmap.c (revision 496ad9aa8ef448058e36ca7a787c61f2e63f0f54)
1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*-
2ccd979bdSMark Fasheh  * vim: noexpandtab sw=8 ts=8 sts=0:
3ccd979bdSMark Fasheh  *
4ccd979bdSMark Fasheh  * mmap.c
5ccd979bdSMark Fasheh  *
6ccd979bdSMark Fasheh  * Code to deal with the mess that is clustered mmap.
7ccd979bdSMark Fasheh  *
8ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9ccd979bdSMark Fasheh  *
10ccd979bdSMark Fasheh  * This program is free software; you can redistribute it and/or
11ccd979bdSMark Fasheh  * modify it under the terms of the GNU General Public
12ccd979bdSMark Fasheh  * License as published by the Free Software Foundation; either
13ccd979bdSMark Fasheh  * version 2 of the License, or (at your option) any later version.
14ccd979bdSMark Fasheh  *
15ccd979bdSMark Fasheh  * This program is distributed in the hope that it will be useful,
16ccd979bdSMark Fasheh  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ccd979bdSMark Fasheh  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18ccd979bdSMark Fasheh  * General Public License for more details.
19ccd979bdSMark Fasheh  *
20ccd979bdSMark Fasheh  * You should have received a copy of the GNU General Public
21ccd979bdSMark Fasheh  * License along with this program; if not, write to the
22ccd979bdSMark Fasheh  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23ccd979bdSMark Fasheh  * Boston, MA 021110-1307, USA.
24ccd979bdSMark Fasheh  */
25ccd979bdSMark Fasheh 
26ccd979bdSMark Fasheh #include <linux/fs.h>
27ccd979bdSMark Fasheh #include <linux/types.h>
28ccd979bdSMark Fasheh #include <linux/highmem.h>
29ccd979bdSMark Fasheh #include <linux/pagemap.h>
30ccd979bdSMark Fasheh #include <linux/uio.h>
31ccd979bdSMark Fasheh #include <linux/signal.h>
32ccd979bdSMark Fasheh #include <linux/rbtree.h>
33ccd979bdSMark Fasheh 
34ccd979bdSMark Fasheh #include <cluster/masklog.h>
35ccd979bdSMark Fasheh 
36ccd979bdSMark Fasheh #include "ocfs2.h"
37ccd979bdSMark Fasheh 
387307de80SMark Fasheh #include "aops.h"
39ccd979bdSMark Fasheh #include "dlmglue.h"
40ccd979bdSMark Fasheh #include "file.h"
41ccd979bdSMark Fasheh #include "inode.h"
42ccd979bdSMark Fasheh #include "mmap.h"
43e4b963f1SJoel Becker #include "super.h"
44614a9e84STao Ma #include "ocfs2_trace.h"
45ccd979bdSMark Fasheh 
467307de80SMark Fasheh 
47d0217ac0SNick Piggin static int ocfs2_fault(struct vm_area_struct *area, struct vm_fault *vmf)
48ccd979bdSMark Fasheh {
49e4b963f1SJoel Becker 	sigset_t oldset;
50e4b963f1SJoel Becker 	int ret;
51ccd979bdSMark Fasheh 
52e4b963f1SJoel Becker 	ocfs2_block_signals(&oldset);
53d0217ac0SNick Piggin 	ret = filemap_fault(area, vmf);
54e4b963f1SJoel Becker 	ocfs2_unblock_signals(&oldset);
55ccd979bdSMark Fasheh 
56614a9e84STao Ma 	trace_ocfs2_fault(OCFS2_I(area->vm_file->f_mapping->host)->ip_blkno,
57614a9e84STao Ma 			  area, vmf->page, vmf->pgoff);
58d0217ac0SNick Piggin 	return ret;
59ccd979bdSMark Fasheh }
60ccd979bdSMark Fasheh 
610378da0fSTao Ma static int __ocfs2_page_mkwrite(struct file *file, struct buffer_head *di_bh,
627307de80SMark Fasheh 				struct page *page)
637307de80SMark Fasheh {
645cffff9eSWengang Wang 	int ret = VM_FAULT_NOPAGE;
65*496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
667307de80SMark Fasheh 	struct address_space *mapping = inode->i_mapping;
6718336338SNick Piggin 	loff_t pos = page_offset(page);
687307de80SMark Fasheh 	unsigned int len = PAGE_CACHE_SIZE;
697307de80SMark Fasheh 	pgoff_t last_index;
707307de80SMark Fasheh 	struct page *locked_page = NULL;
717307de80SMark Fasheh 	void *fsdata;
727307de80SMark Fasheh 	loff_t size = i_size_read(inode);
737307de80SMark Fasheh 
74f63afdb2STao Ma 	last_index = (size - 1) >> PAGE_CACHE_SHIFT;
757307de80SMark Fasheh 
767307de80SMark Fasheh 	/*
775cffff9eSWengang Wang 	 * There are cases that lead to the page no longer bebongs to the
785cffff9eSWengang Wang 	 * mapping.
795cffff9eSWengang Wang 	 * 1) pagecache truncates locally due to memory pressure.
805cffff9eSWengang Wang 	 * 2) pagecache truncates when another is taking EX lock against
815cffff9eSWengang Wang 	 * inode lock. see ocfs2_data_convert_worker.
825cffff9eSWengang Wang 	 *
835cffff9eSWengang Wang 	 * The i_size check doesn't catch the case where nodes truncated and
845cffff9eSWengang Wang 	 * then re-extended the file. We'll re-check the page mapping after
855cffff9eSWengang Wang 	 * taking the page lock inside of ocfs2_write_begin_nolock().
865cffff9eSWengang Wang 	 *
875cffff9eSWengang Wang 	 * Let VM retry with these cases.
887307de80SMark Fasheh 	 */
895cffff9eSWengang Wang 	if ((page->mapping != inode->i_mapping) ||
905cffff9eSWengang Wang 	    (!PageUptodate(page)) ||
915cffff9eSWengang Wang 	    (page_offset(page) >= size))
927307de80SMark Fasheh 		goto out;
937307de80SMark Fasheh 
947307de80SMark Fasheh 	/*
957307de80SMark Fasheh 	 * Call ocfs2_write_begin() and ocfs2_write_end() to take
967307de80SMark Fasheh 	 * advantage of the allocation code there. We pass a write
977307de80SMark Fasheh 	 * length of the whole page (chopped to i_size) to make sure
987307de80SMark Fasheh 	 * the whole thing is allocated.
997307de80SMark Fasheh 	 *
1007307de80SMark Fasheh 	 * Since we know the page is up to date, we don't have to
1017307de80SMark Fasheh 	 * worry about ocfs2_write_begin() skipping some buffer reads
1027307de80SMark Fasheh 	 * because the "write" would invalidate their data.
1037307de80SMark Fasheh 	 */
1047307de80SMark Fasheh 	if (page->index == last_index)
105f63afdb2STao Ma 		len = ((size - 1) & ~PAGE_CACHE_MASK) + 1;
1067307de80SMark Fasheh 
1070378da0fSTao Ma 	ret = ocfs2_write_begin_nolock(file, mapping, pos, len, 0, &locked_page,
1087307de80SMark Fasheh 				       &fsdata, di_bh, page);
1097307de80SMark Fasheh 	if (ret) {
1107307de80SMark Fasheh 		if (ret != -ENOSPC)
1117307de80SMark Fasheh 			mlog_errno(ret);
1125cffff9eSWengang Wang 		if (ret == -ENOMEM)
1135cffff9eSWengang Wang 			ret = VM_FAULT_OOM;
1145cffff9eSWengang Wang 		else
1155cffff9eSWengang Wang 			ret = VM_FAULT_SIGBUS;
1167307de80SMark Fasheh 		goto out;
1177307de80SMark Fasheh 	}
1187307de80SMark Fasheh 
1195cffff9eSWengang Wang 	if (!locked_page) {
1205cffff9eSWengang Wang 		ret = VM_FAULT_NOPAGE;
1217307de80SMark Fasheh 		goto out;
1227307de80SMark Fasheh 	}
1235cffff9eSWengang Wang 	ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page,
1245cffff9eSWengang Wang 				     fsdata);
1257307de80SMark Fasheh 	BUG_ON(ret != len);
1265cffff9eSWengang Wang 	ret = VM_FAULT_LOCKED;
1277307de80SMark Fasheh out:
1287307de80SMark Fasheh 	return ret;
1297307de80SMark Fasheh }
1307307de80SMark Fasheh 
131c2ec175cSNick Piggin static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1327307de80SMark Fasheh {
133c2ec175cSNick Piggin 	struct page *page = vmf->page;
134*496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
1357307de80SMark Fasheh 	struct buffer_head *di_bh = NULL;
136e4b963f1SJoel Becker 	sigset_t oldset;
137e4b963f1SJoel Becker 	int ret;
1387307de80SMark Fasheh 
139fef6925cSJan Kara 	sb_start_pagefault(inode->i_sb);
140e4b963f1SJoel Becker 	ocfs2_block_signals(&oldset);
1417307de80SMark Fasheh 
1427307de80SMark Fasheh 	/*
1437307de80SMark Fasheh 	 * The cluster locks taken will block a truncate from another
1447307de80SMark Fasheh 	 * node. Taking the data lock will also ensure that we don't
1457307de80SMark Fasheh 	 * attempt page truncation as part of a downconvert.
1467307de80SMark Fasheh 	 */
147e63aecb6SMark Fasheh 	ret = ocfs2_inode_lock(inode, &di_bh, 1);
1487307de80SMark Fasheh 	if (ret < 0) {
1497307de80SMark Fasheh 		mlog_errno(ret);
1507307de80SMark Fasheh 		goto out;
1517307de80SMark Fasheh 	}
1527307de80SMark Fasheh 
1537307de80SMark Fasheh 	/*
1547307de80SMark Fasheh 	 * The alloc sem should be enough to serialize with
1557307de80SMark Fasheh 	 * ocfs2_truncate_file() changing i_size as well as any thread
1567307de80SMark Fasheh 	 * modifying the inode btree.
1577307de80SMark Fasheh 	 */
1587307de80SMark Fasheh 	down_write(&OCFS2_I(inode)->ip_alloc_sem);
1597307de80SMark Fasheh 
1600378da0fSTao Ma 	ret = __ocfs2_page_mkwrite(vma->vm_file, di_bh, page);
1617307de80SMark Fasheh 
1627307de80SMark Fasheh 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
1637307de80SMark Fasheh 
1647307de80SMark Fasheh 	brelse(di_bh);
165e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 1);
1667307de80SMark Fasheh 
1677307de80SMark Fasheh out:
168e4b963f1SJoel Becker 	ocfs2_unblock_signals(&oldset);
169fef6925cSJan Kara 	sb_end_pagefault(inode->i_sb);
1707307de80SMark Fasheh 	return ret;
1717307de80SMark Fasheh }
1727307de80SMark Fasheh 
173f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct ocfs2_file_vm_ops = {
17454cb8821SNick Piggin 	.fault		= ocfs2_fault,
1757307de80SMark Fasheh 	.page_mkwrite	= ocfs2_page_mkwrite,
1760b173bc4SKonstantin Khlebnikov 	.remap_pages	= generic_file_remap_pages,
177ccd979bdSMark Fasheh };
178ccd979bdSMark Fasheh 
179870f4817SChristoph Hellwig int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
180ccd979bdSMark Fasheh {
18125899deeSTiger Yang 	int ret = 0, lock_level = 0;
182ccd979bdSMark Fasheh 
183*496ad9aaSAl Viro 	ret = ocfs2_inode_lock_atime(file_inode(file),
18425899deeSTiger Yang 				    file->f_vfsmnt, &lock_level);
18525899deeSTiger Yang 	if (ret < 0) {
18625899deeSTiger Yang 		mlog_errno(ret);
18725899deeSTiger Yang 		goto out;
18825899deeSTiger Yang 	}
189*496ad9aaSAl Viro 	ocfs2_inode_unlock(file_inode(file), lock_level);
19025899deeSTiger Yang out:
191ccd979bdSMark Fasheh 	vma->vm_ops = &ocfs2_file_vm_ops;
192ccd979bdSMark Fasheh 	return 0;
193ccd979bdSMark Fasheh }
194ccd979bdSMark Fasheh 
195