xref: /linux/fs/ocfs2/mmap.c (revision d0217ac04ca6591841e5665f518e38064f4e65bd)
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/slab.h>
29ccd979bdSMark Fasheh #include <linux/highmem.h>
30ccd979bdSMark Fasheh #include <linux/pagemap.h>
31ccd979bdSMark Fasheh #include <linux/uio.h>
32ccd979bdSMark Fasheh #include <linux/signal.h>
33ccd979bdSMark Fasheh #include <linux/rbtree.h>
34ccd979bdSMark Fasheh 
35ccd979bdSMark Fasheh #define MLOG_MASK_PREFIX ML_FILE_IO
36ccd979bdSMark Fasheh #include <cluster/masklog.h>
37ccd979bdSMark Fasheh 
38ccd979bdSMark Fasheh #include "ocfs2.h"
39ccd979bdSMark Fasheh 
407307de80SMark Fasheh #include "aops.h"
41ccd979bdSMark Fasheh #include "dlmglue.h"
42ccd979bdSMark Fasheh #include "file.h"
43ccd979bdSMark Fasheh #include "inode.h"
44ccd979bdSMark Fasheh #include "mmap.h"
45ccd979bdSMark Fasheh 
467307de80SMark Fasheh static inline int ocfs2_vm_op_block_sigs(sigset_t *blocked, sigset_t *oldset)
477307de80SMark Fasheh {
487307de80SMark Fasheh 	/* The best way to deal with signals in the vm path is
497307de80SMark Fasheh 	 * to block them upfront, rather than allowing the
507307de80SMark Fasheh 	 * locking paths to return -ERESTARTSYS. */
517307de80SMark Fasheh 	sigfillset(blocked);
527307de80SMark Fasheh 
537307de80SMark Fasheh 	/* We should technically never get a bad return value
547307de80SMark Fasheh 	 * from sigprocmask */
557307de80SMark Fasheh 	return sigprocmask(SIG_BLOCK, blocked, oldset);
567307de80SMark Fasheh }
577307de80SMark Fasheh 
587307de80SMark Fasheh static inline int ocfs2_vm_op_unblock_sigs(sigset_t *oldset)
597307de80SMark Fasheh {
607307de80SMark Fasheh 	return sigprocmask(SIG_SETMASK, oldset, NULL);
617307de80SMark Fasheh }
627307de80SMark Fasheh 
63*d0217ac0SNick Piggin static int ocfs2_fault(struct vm_area_struct *area, struct vm_fault *vmf)
64ccd979bdSMark Fasheh {
65ccd979bdSMark Fasheh 	sigset_t blocked, oldset;
66*d0217ac0SNick Piggin 	int error, ret;
67ccd979bdSMark Fasheh 
68*d0217ac0SNick Piggin 	mlog_entry("(area=%p, page offset=%lu)\n", area, vmf->pgoff);
69ccd979bdSMark Fasheh 
70*d0217ac0SNick Piggin 	error = ocfs2_vm_op_block_sigs(&blocked, &oldset);
71*d0217ac0SNick Piggin 	if (error < 0) {
72*d0217ac0SNick Piggin 		mlog_errno(error);
73*d0217ac0SNick Piggin 		ret = VM_FAULT_SIGBUS;
74ccd979bdSMark Fasheh 		goto out;
75ccd979bdSMark Fasheh 	}
76ccd979bdSMark Fasheh 
77*d0217ac0SNick Piggin 	ret = filemap_fault(area, vmf);
78ccd979bdSMark Fasheh 
79*d0217ac0SNick Piggin 	error = ocfs2_vm_op_unblock_sigs(&oldset);
80*d0217ac0SNick Piggin 	if (error < 0)
81*d0217ac0SNick Piggin 		mlog_errno(error);
82ccd979bdSMark Fasheh out:
83*d0217ac0SNick Piggin 	mlog_exit_ptr(vmf->page);
84*d0217ac0SNick Piggin 	return ret;
85ccd979bdSMark Fasheh }
86ccd979bdSMark Fasheh 
877307de80SMark Fasheh static int __ocfs2_page_mkwrite(struct inode *inode, struct buffer_head *di_bh,
887307de80SMark Fasheh 				struct page *page)
897307de80SMark Fasheh {
907307de80SMark Fasheh 	int ret;
917307de80SMark Fasheh 	struct address_space *mapping = inode->i_mapping;
927307de80SMark Fasheh 	loff_t pos = page->index << PAGE_CACHE_SHIFT;
937307de80SMark Fasheh 	unsigned int len = PAGE_CACHE_SIZE;
947307de80SMark Fasheh 	pgoff_t last_index;
957307de80SMark Fasheh 	struct page *locked_page = NULL;
967307de80SMark Fasheh 	void *fsdata;
977307de80SMark Fasheh 	loff_t size = i_size_read(inode);
987307de80SMark Fasheh 
997307de80SMark Fasheh 	/*
1007307de80SMark Fasheh 	 * Another node might have truncated while we were waiting on
1017307de80SMark Fasheh 	 * cluster locks.
1027307de80SMark Fasheh 	 */
1037307de80SMark Fasheh 	last_index = size >> PAGE_CACHE_SHIFT;
1047307de80SMark Fasheh 	if (page->index > last_index) {
1057307de80SMark Fasheh 		ret = -EINVAL;
1067307de80SMark Fasheh 		goto out;
1077307de80SMark Fasheh 	}
1087307de80SMark Fasheh 
1097307de80SMark Fasheh 	/*
1107307de80SMark Fasheh 	 * The i_size check above doesn't catch the case where nodes
1117307de80SMark Fasheh 	 * truncated and then re-extended the file. We'll re-check the
1127307de80SMark Fasheh 	 * page mapping after taking the page lock inside of
1137307de80SMark Fasheh 	 * ocfs2_write_begin_nolock().
1147307de80SMark Fasheh 	 */
1157307de80SMark Fasheh 	if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
1167307de80SMark Fasheh 		ret = -EINVAL;
1177307de80SMark Fasheh 		goto out;
1187307de80SMark Fasheh 	}
1197307de80SMark Fasheh 
1207307de80SMark Fasheh 	/*
1217307de80SMark Fasheh 	 * Call ocfs2_write_begin() and ocfs2_write_end() to take
1227307de80SMark Fasheh 	 * advantage of the allocation code there. We pass a write
1237307de80SMark Fasheh 	 * length of the whole page (chopped to i_size) to make sure
1247307de80SMark Fasheh 	 * the whole thing is allocated.
1257307de80SMark Fasheh 	 *
1267307de80SMark Fasheh 	 * Since we know the page is up to date, we don't have to
1277307de80SMark Fasheh 	 * worry about ocfs2_write_begin() skipping some buffer reads
1287307de80SMark Fasheh 	 * because the "write" would invalidate their data.
1297307de80SMark Fasheh 	 */
1307307de80SMark Fasheh 	if (page->index == last_index)
1317307de80SMark Fasheh 		len = size & ~PAGE_CACHE_MASK;
1327307de80SMark Fasheh 
1337307de80SMark Fasheh 	ret = ocfs2_write_begin_nolock(mapping, pos, len, 0, &locked_page,
1347307de80SMark Fasheh 				       &fsdata, di_bh, page);
1357307de80SMark Fasheh 	if (ret) {
1367307de80SMark Fasheh 		if (ret != -ENOSPC)
1377307de80SMark Fasheh 			mlog_errno(ret);
1387307de80SMark Fasheh 		goto out;
1397307de80SMark Fasheh 	}
1407307de80SMark Fasheh 
1417307de80SMark Fasheh 	ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page,
1427307de80SMark Fasheh 				     fsdata);
1437307de80SMark Fasheh 	if (ret < 0) {
1447307de80SMark Fasheh 		mlog_errno(ret);
1457307de80SMark Fasheh 		goto out;
1467307de80SMark Fasheh 	}
1477307de80SMark Fasheh 	BUG_ON(ret != len);
1487307de80SMark Fasheh 	ret = 0;
1497307de80SMark Fasheh out:
1507307de80SMark Fasheh 	return ret;
1517307de80SMark Fasheh }
1527307de80SMark Fasheh 
1537307de80SMark Fasheh static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1547307de80SMark Fasheh {
1557307de80SMark Fasheh 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1567307de80SMark Fasheh 	struct buffer_head *di_bh = NULL;
1577307de80SMark Fasheh 	sigset_t blocked, oldset;
1587307de80SMark Fasheh 	int ret, ret2;
1597307de80SMark Fasheh 
1607307de80SMark Fasheh 	ret = ocfs2_vm_op_block_sigs(&blocked, &oldset);
1617307de80SMark Fasheh 	if (ret < 0) {
1627307de80SMark Fasheh 		mlog_errno(ret);
1637307de80SMark Fasheh 		return ret;
1647307de80SMark Fasheh 	}
1657307de80SMark Fasheh 
1667307de80SMark Fasheh 	/*
1677307de80SMark Fasheh 	 * The cluster locks taken will block a truncate from another
1687307de80SMark Fasheh 	 * node. Taking the data lock will also ensure that we don't
1697307de80SMark Fasheh 	 * attempt page truncation as part of a downconvert.
1707307de80SMark Fasheh 	 */
1717307de80SMark Fasheh 	ret = ocfs2_meta_lock(inode, &di_bh, 1);
1727307de80SMark Fasheh 	if (ret < 0) {
1737307de80SMark Fasheh 		mlog_errno(ret);
1747307de80SMark Fasheh 		goto out;
1757307de80SMark Fasheh 	}
1767307de80SMark Fasheh 
1777307de80SMark Fasheh 	/*
1787307de80SMark Fasheh 	 * The alloc sem should be enough to serialize with
1797307de80SMark Fasheh 	 * ocfs2_truncate_file() changing i_size as well as any thread
1807307de80SMark Fasheh 	 * modifying the inode btree.
1817307de80SMark Fasheh 	 */
1827307de80SMark Fasheh 	down_write(&OCFS2_I(inode)->ip_alloc_sem);
1837307de80SMark Fasheh 
1847307de80SMark Fasheh 	ret = ocfs2_data_lock(inode, 1);
1857307de80SMark Fasheh 	if (ret < 0) {
1867307de80SMark Fasheh 		mlog_errno(ret);
1877307de80SMark Fasheh 		goto out_meta_unlock;
1887307de80SMark Fasheh 	}
1897307de80SMark Fasheh 
1907307de80SMark Fasheh 	ret = __ocfs2_page_mkwrite(inode, di_bh, page);
1917307de80SMark Fasheh 
1927307de80SMark Fasheh 	ocfs2_data_unlock(inode, 1);
1937307de80SMark Fasheh 
1947307de80SMark Fasheh out_meta_unlock:
1957307de80SMark Fasheh 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
1967307de80SMark Fasheh 
1977307de80SMark Fasheh 	brelse(di_bh);
1987307de80SMark Fasheh 	ocfs2_meta_unlock(inode, 1);
1997307de80SMark Fasheh 
2007307de80SMark Fasheh out:
2017307de80SMark Fasheh 	ret2 = ocfs2_vm_op_unblock_sigs(&oldset);
2027307de80SMark Fasheh 	if (ret2 < 0)
2037307de80SMark Fasheh 		mlog_errno(ret2);
2047307de80SMark Fasheh 
2057307de80SMark Fasheh 	return ret;
2067307de80SMark Fasheh }
2077307de80SMark Fasheh 
208ccd979bdSMark Fasheh static struct vm_operations_struct ocfs2_file_vm_ops = {
20954cb8821SNick Piggin 	.fault		= ocfs2_fault,
2107307de80SMark Fasheh 	.page_mkwrite	= ocfs2_page_mkwrite,
211ccd979bdSMark Fasheh };
212ccd979bdSMark Fasheh 
213870f4817SChristoph Hellwig int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
214ccd979bdSMark Fasheh {
21525899deeSTiger Yang 	int ret = 0, lock_level = 0;
216ccd979bdSMark Fasheh 
21725899deeSTiger Yang 	ret = ocfs2_meta_lock_atime(file->f_dentry->d_inode,
21825899deeSTiger Yang 				    file->f_vfsmnt, &lock_level);
21925899deeSTiger Yang 	if (ret < 0) {
22025899deeSTiger Yang 		mlog_errno(ret);
22125899deeSTiger Yang 		goto out;
22225899deeSTiger Yang 	}
22325899deeSTiger Yang 	ocfs2_meta_unlock(file->f_dentry->d_inode, lock_level);
22425899deeSTiger Yang out:
225ccd979bdSMark Fasheh 	vma->vm_ops = &ocfs2_file_vm_ops;
226*d0217ac0SNick Piggin 	vma->vm_flags |= VM_CAN_NONLINEAR;
227ccd979bdSMark Fasheh 	return 0;
228ccd979bdSMark Fasheh }
229ccd979bdSMark Fasheh 
230