xref: /linux/fs/ocfs2/mmap.c (revision 11bac80004499ea59f361ef2a5516c84b6eab675)
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 
47*11bac800SDave Jiang static int ocfs2_fault(struct vm_fault *vmf)
48ccd979bdSMark Fasheh {
49*11bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
50e4b963f1SJoel Becker 	sigset_t oldset;
51e4b963f1SJoel Becker 	int ret;
52ccd979bdSMark Fasheh 
53e4b963f1SJoel Becker 	ocfs2_block_signals(&oldset);
54*11bac800SDave Jiang 	ret = filemap_fault(vmf);
55e4b963f1SJoel Becker 	ocfs2_unblock_signals(&oldset);
56ccd979bdSMark Fasheh 
57*11bac800SDave Jiang 	trace_ocfs2_fault(OCFS2_I(vma->vm_file->f_mapping->host)->ip_blkno,
58*11bac800SDave Jiang 			  vma, vmf->page, vmf->pgoff);
59d0217ac0SNick Piggin 	return ret;
60ccd979bdSMark Fasheh }
61ccd979bdSMark Fasheh 
620378da0fSTao Ma static int __ocfs2_page_mkwrite(struct file *file, struct buffer_head *di_bh,
637307de80SMark Fasheh 				struct page *page)
647307de80SMark Fasheh {
655cffff9eSWengang Wang 	int ret = VM_FAULT_NOPAGE;
66496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
677307de80SMark Fasheh 	struct address_space *mapping = inode->i_mapping;
6818336338SNick Piggin 	loff_t pos = page_offset(page);
6909cbfeafSKirill A. Shutemov 	unsigned int len = PAGE_SIZE;
707307de80SMark Fasheh 	pgoff_t last_index;
717307de80SMark Fasheh 	struct page *locked_page = NULL;
727307de80SMark Fasheh 	void *fsdata;
737307de80SMark Fasheh 	loff_t size = i_size_read(inode);
747307de80SMark Fasheh 
7509cbfeafSKirill A. Shutemov 	last_index = (size - 1) >> PAGE_SHIFT;
767307de80SMark Fasheh 
777307de80SMark Fasheh 	/*
785cffff9eSWengang Wang 	 * There are cases that lead to the page no longer bebongs to the
795cffff9eSWengang Wang 	 * mapping.
805cffff9eSWengang Wang 	 * 1) pagecache truncates locally due to memory pressure.
815cffff9eSWengang Wang 	 * 2) pagecache truncates when another is taking EX lock against
825cffff9eSWengang Wang 	 * inode lock. see ocfs2_data_convert_worker.
835cffff9eSWengang Wang 	 *
845cffff9eSWengang Wang 	 * The i_size check doesn't catch the case where nodes truncated and
855cffff9eSWengang Wang 	 * then re-extended the file. We'll re-check the page mapping after
865cffff9eSWengang Wang 	 * taking the page lock inside of ocfs2_write_begin_nolock().
875cffff9eSWengang Wang 	 *
885cffff9eSWengang Wang 	 * Let VM retry with these cases.
897307de80SMark Fasheh 	 */
905cffff9eSWengang Wang 	if ((page->mapping != inode->i_mapping) ||
915cffff9eSWengang Wang 	    (!PageUptodate(page)) ||
925cffff9eSWengang Wang 	    (page_offset(page) >= size))
937307de80SMark Fasheh 		goto out;
947307de80SMark Fasheh 
957307de80SMark Fasheh 	/*
967307de80SMark Fasheh 	 * Call ocfs2_write_begin() and ocfs2_write_end() to take
977307de80SMark Fasheh 	 * advantage of the allocation code there. We pass a write
987307de80SMark Fasheh 	 * length of the whole page (chopped to i_size) to make sure
997307de80SMark Fasheh 	 * the whole thing is allocated.
1007307de80SMark Fasheh 	 *
1017307de80SMark Fasheh 	 * Since we know the page is up to date, we don't have to
1027307de80SMark Fasheh 	 * worry about ocfs2_write_begin() skipping some buffer reads
1037307de80SMark Fasheh 	 * because the "write" would invalidate their data.
1047307de80SMark Fasheh 	 */
1057307de80SMark Fasheh 	if (page->index == last_index)
10609cbfeafSKirill A. Shutemov 		len = ((size - 1) & ~PAGE_MASK) + 1;
1077307de80SMark Fasheh 
108c1ad1e3cSRyan Ding 	ret = ocfs2_write_begin_nolock(mapping, pos, len, OCFS2_WRITE_MMAP,
109c1ad1e3cSRyan Ding 				       &locked_page, &fsdata, di_bh, page);
1107307de80SMark Fasheh 	if (ret) {
1117307de80SMark Fasheh 		if (ret != -ENOSPC)
1127307de80SMark Fasheh 			mlog_errno(ret);
1135cffff9eSWengang Wang 		if (ret == -ENOMEM)
1145cffff9eSWengang Wang 			ret = VM_FAULT_OOM;
1155cffff9eSWengang Wang 		else
1165cffff9eSWengang Wang 			ret = VM_FAULT_SIGBUS;
1177307de80SMark Fasheh 		goto out;
1187307de80SMark Fasheh 	}
1197307de80SMark Fasheh 
1205cffff9eSWengang Wang 	if (!locked_page) {
1215cffff9eSWengang Wang 		ret = VM_FAULT_NOPAGE;
1227307de80SMark Fasheh 		goto out;
1237307de80SMark Fasheh 	}
12407f38d97Spiaojun 	ret = ocfs2_write_end_nolock(mapping, pos, len, len, fsdata);
1257307de80SMark Fasheh 	BUG_ON(ret != len);
1265cffff9eSWengang Wang 	ret = VM_FAULT_LOCKED;
1277307de80SMark Fasheh out:
1287307de80SMark Fasheh 	return ret;
1297307de80SMark Fasheh }
1307307de80SMark Fasheh 
131*11bac800SDave Jiang static int ocfs2_page_mkwrite(struct vm_fault *vmf)
1327307de80SMark Fasheh {
133c2ec175cSNick Piggin 	struct page *page = vmf->page;
134*11bac800SDave Jiang 	struct inode *inode = file_inode(vmf->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);
150566e8dfdSJan Kara 		if (ret == -ENOMEM)
151566e8dfdSJan Kara 			ret = VM_FAULT_OOM;
152566e8dfdSJan Kara 		else
153566e8dfdSJan Kara 			ret = VM_FAULT_SIGBUS;
1547307de80SMark Fasheh 		goto out;
1557307de80SMark Fasheh 	}
1567307de80SMark Fasheh 
1577307de80SMark Fasheh 	/*
1587307de80SMark Fasheh 	 * The alloc sem should be enough to serialize with
1597307de80SMark Fasheh 	 * ocfs2_truncate_file() changing i_size as well as any thread
1607307de80SMark Fasheh 	 * modifying the inode btree.
1617307de80SMark Fasheh 	 */
1627307de80SMark Fasheh 	down_write(&OCFS2_I(inode)->ip_alloc_sem);
1637307de80SMark Fasheh 
164*11bac800SDave Jiang 	ret = __ocfs2_page_mkwrite(vmf->vma->vm_file, di_bh, page);
1657307de80SMark Fasheh 
1667307de80SMark Fasheh 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
1677307de80SMark Fasheh 
1687307de80SMark Fasheh 	brelse(di_bh);
169e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 1);
1707307de80SMark Fasheh 
1717307de80SMark Fasheh out:
172e4b963f1SJoel Becker 	ocfs2_unblock_signals(&oldset);
173fef6925cSJan Kara 	sb_end_pagefault(inode->i_sb);
1747307de80SMark Fasheh 	return ret;
1757307de80SMark Fasheh }
1767307de80SMark Fasheh 
177f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct ocfs2_file_vm_ops = {
17854cb8821SNick Piggin 	.fault		= ocfs2_fault,
1797307de80SMark Fasheh 	.page_mkwrite	= ocfs2_page_mkwrite,
180ccd979bdSMark Fasheh };
181ccd979bdSMark Fasheh 
182870f4817SChristoph Hellwig int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
183ccd979bdSMark Fasheh {
18425899deeSTiger Yang 	int ret = 0, lock_level = 0;
185ccd979bdSMark Fasheh 
186496ad9aaSAl Viro 	ret = ocfs2_inode_lock_atime(file_inode(file),
187182be684SAl Viro 				    file->f_path.mnt, &lock_level);
18825899deeSTiger Yang 	if (ret < 0) {
18925899deeSTiger Yang 		mlog_errno(ret);
19025899deeSTiger Yang 		goto out;
19125899deeSTiger Yang 	}
192496ad9aaSAl Viro 	ocfs2_inode_unlock(file_inode(file), lock_level);
19325899deeSTiger Yang out:
194ccd979bdSMark Fasheh 	vma->vm_ops = &ocfs2_file_vm_ops;
195ccd979bdSMark Fasheh 	return 0;
196ccd979bdSMark Fasheh }
197ccd979bdSMark Fasheh 
198