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*54cb8821SNick Piggin static struct page *ocfs2_fault(struct vm_area_struct *area, 64*54cb8821SNick Piggin struct fault_data *fdata) 65ccd979bdSMark Fasheh { 66*54cb8821SNick Piggin struct page *page = NULL; 67ccd979bdSMark Fasheh sigset_t blocked, oldset; 68ccd979bdSMark Fasheh int ret; 69ccd979bdSMark Fasheh 70*54cb8821SNick Piggin mlog_entry("(area=%p, page offset=%lu)\n", area, fdata->pgoff); 71ccd979bdSMark Fasheh 727307de80SMark Fasheh ret = ocfs2_vm_op_block_sigs(&blocked, &oldset); 73ccd979bdSMark Fasheh if (ret < 0) { 74*54cb8821SNick Piggin fdata->type = VM_FAULT_SIGBUS; 75ccd979bdSMark Fasheh mlog_errno(ret); 76ccd979bdSMark Fasheh goto out; 77ccd979bdSMark Fasheh } 78ccd979bdSMark Fasheh 79*54cb8821SNick Piggin page = filemap_fault(area, fdata); 80ccd979bdSMark Fasheh 817307de80SMark Fasheh ret = ocfs2_vm_op_unblock_sigs(&oldset); 82ccd979bdSMark Fasheh if (ret < 0) 83ccd979bdSMark Fasheh mlog_errno(ret); 84ccd979bdSMark Fasheh out: 85ccd979bdSMark Fasheh mlog_exit_ptr(page); 86ccd979bdSMark Fasheh return page; 87ccd979bdSMark Fasheh } 88ccd979bdSMark Fasheh 897307de80SMark Fasheh static int __ocfs2_page_mkwrite(struct inode *inode, struct buffer_head *di_bh, 907307de80SMark Fasheh struct page *page) 917307de80SMark Fasheh { 927307de80SMark Fasheh int ret; 937307de80SMark Fasheh struct address_space *mapping = inode->i_mapping; 947307de80SMark Fasheh loff_t pos = page->index << PAGE_CACHE_SHIFT; 957307de80SMark Fasheh unsigned int len = PAGE_CACHE_SIZE; 967307de80SMark Fasheh pgoff_t last_index; 977307de80SMark Fasheh struct page *locked_page = NULL; 987307de80SMark Fasheh void *fsdata; 997307de80SMark Fasheh loff_t size = i_size_read(inode); 1007307de80SMark Fasheh 1017307de80SMark Fasheh /* 1027307de80SMark Fasheh * Another node might have truncated while we were waiting on 1037307de80SMark Fasheh * cluster locks. 1047307de80SMark Fasheh */ 1057307de80SMark Fasheh last_index = size >> PAGE_CACHE_SHIFT; 1067307de80SMark Fasheh if (page->index > last_index) { 1077307de80SMark Fasheh ret = -EINVAL; 1087307de80SMark Fasheh goto out; 1097307de80SMark Fasheh } 1107307de80SMark Fasheh 1117307de80SMark Fasheh /* 1127307de80SMark Fasheh * The i_size check above doesn't catch the case where nodes 1137307de80SMark Fasheh * truncated and then re-extended the file. We'll re-check the 1147307de80SMark Fasheh * page mapping after taking the page lock inside of 1157307de80SMark Fasheh * ocfs2_write_begin_nolock(). 1167307de80SMark Fasheh */ 1177307de80SMark Fasheh if (!PageUptodate(page) || page->mapping != inode->i_mapping) { 1187307de80SMark Fasheh ret = -EINVAL; 1197307de80SMark Fasheh goto out; 1207307de80SMark Fasheh } 1217307de80SMark Fasheh 1227307de80SMark Fasheh /* 1237307de80SMark Fasheh * Call ocfs2_write_begin() and ocfs2_write_end() to take 1247307de80SMark Fasheh * advantage of the allocation code there. We pass a write 1257307de80SMark Fasheh * length of the whole page (chopped to i_size) to make sure 1267307de80SMark Fasheh * the whole thing is allocated. 1277307de80SMark Fasheh * 1287307de80SMark Fasheh * Since we know the page is up to date, we don't have to 1297307de80SMark Fasheh * worry about ocfs2_write_begin() skipping some buffer reads 1307307de80SMark Fasheh * because the "write" would invalidate their data. 1317307de80SMark Fasheh */ 1327307de80SMark Fasheh if (page->index == last_index) 1337307de80SMark Fasheh len = size & ~PAGE_CACHE_MASK; 1347307de80SMark Fasheh 1357307de80SMark Fasheh ret = ocfs2_write_begin_nolock(mapping, pos, len, 0, &locked_page, 1367307de80SMark Fasheh &fsdata, di_bh, page); 1377307de80SMark Fasheh if (ret) { 1387307de80SMark Fasheh if (ret != -ENOSPC) 1397307de80SMark Fasheh mlog_errno(ret); 1407307de80SMark Fasheh goto out; 1417307de80SMark Fasheh } 1427307de80SMark Fasheh 1437307de80SMark Fasheh ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page, 1447307de80SMark Fasheh fsdata); 1457307de80SMark Fasheh if (ret < 0) { 1467307de80SMark Fasheh mlog_errno(ret); 1477307de80SMark Fasheh goto out; 1487307de80SMark Fasheh } 1497307de80SMark Fasheh BUG_ON(ret != len); 1507307de80SMark Fasheh ret = 0; 1517307de80SMark Fasheh out: 1527307de80SMark Fasheh return ret; 1537307de80SMark Fasheh } 1547307de80SMark Fasheh 1557307de80SMark Fasheh static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct page *page) 1567307de80SMark Fasheh { 1577307de80SMark Fasheh struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 1587307de80SMark Fasheh struct buffer_head *di_bh = NULL; 1597307de80SMark Fasheh sigset_t blocked, oldset; 1607307de80SMark Fasheh int ret, ret2; 1617307de80SMark Fasheh 1627307de80SMark Fasheh ret = ocfs2_vm_op_block_sigs(&blocked, &oldset); 1637307de80SMark Fasheh if (ret < 0) { 1647307de80SMark Fasheh mlog_errno(ret); 1657307de80SMark Fasheh return ret; 1667307de80SMark Fasheh } 1677307de80SMark Fasheh 1687307de80SMark Fasheh /* 1697307de80SMark Fasheh * The cluster locks taken will block a truncate from another 1707307de80SMark Fasheh * node. Taking the data lock will also ensure that we don't 1717307de80SMark Fasheh * attempt page truncation as part of a downconvert. 1727307de80SMark Fasheh */ 1737307de80SMark Fasheh ret = ocfs2_meta_lock(inode, &di_bh, 1); 1747307de80SMark Fasheh if (ret < 0) { 1757307de80SMark Fasheh mlog_errno(ret); 1767307de80SMark Fasheh goto out; 1777307de80SMark Fasheh } 1787307de80SMark Fasheh 1797307de80SMark Fasheh /* 1807307de80SMark Fasheh * The alloc sem should be enough to serialize with 1817307de80SMark Fasheh * ocfs2_truncate_file() changing i_size as well as any thread 1827307de80SMark Fasheh * modifying the inode btree. 1837307de80SMark Fasheh */ 1847307de80SMark Fasheh down_write(&OCFS2_I(inode)->ip_alloc_sem); 1857307de80SMark Fasheh 1867307de80SMark Fasheh ret = ocfs2_data_lock(inode, 1); 1877307de80SMark Fasheh if (ret < 0) { 1887307de80SMark Fasheh mlog_errno(ret); 1897307de80SMark Fasheh goto out_meta_unlock; 1907307de80SMark Fasheh } 1917307de80SMark Fasheh 1927307de80SMark Fasheh ret = __ocfs2_page_mkwrite(inode, di_bh, page); 1937307de80SMark Fasheh 1947307de80SMark Fasheh ocfs2_data_unlock(inode, 1); 1957307de80SMark Fasheh 1967307de80SMark Fasheh out_meta_unlock: 1977307de80SMark Fasheh up_write(&OCFS2_I(inode)->ip_alloc_sem); 1987307de80SMark Fasheh 1997307de80SMark Fasheh brelse(di_bh); 2007307de80SMark Fasheh ocfs2_meta_unlock(inode, 1); 2017307de80SMark Fasheh 2027307de80SMark Fasheh out: 2037307de80SMark Fasheh ret2 = ocfs2_vm_op_unblock_sigs(&oldset); 2047307de80SMark Fasheh if (ret2 < 0) 2057307de80SMark Fasheh mlog_errno(ret2); 2067307de80SMark Fasheh 2077307de80SMark Fasheh return ret; 2087307de80SMark Fasheh } 2097307de80SMark Fasheh 210ccd979bdSMark Fasheh static struct vm_operations_struct ocfs2_file_vm_ops = { 211*54cb8821SNick Piggin .fault = ocfs2_fault, 2127307de80SMark Fasheh .page_mkwrite = ocfs2_page_mkwrite, 213ccd979bdSMark Fasheh }; 214ccd979bdSMark Fasheh 215870f4817SChristoph Hellwig int ocfs2_mmap(struct file *file, struct vm_area_struct *vma) 216ccd979bdSMark Fasheh { 21725899deeSTiger Yang int ret = 0, lock_level = 0; 218ccd979bdSMark Fasheh 21925899deeSTiger Yang ret = ocfs2_meta_lock_atime(file->f_dentry->d_inode, 22025899deeSTiger Yang file->f_vfsmnt, &lock_level); 22125899deeSTiger Yang if (ret < 0) { 22225899deeSTiger Yang mlog_errno(ret); 22325899deeSTiger Yang goto out; 22425899deeSTiger Yang } 22525899deeSTiger Yang ocfs2_meta_unlock(file->f_dentry->d_inode, lock_level); 22625899deeSTiger Yang out: 227ccd979bdSMark Fasheh vma->vm_ops = &ocfs2_file_vm_ops; 228*54cb8821SNick Piggin vma->vm_flags |= VM_CAN_INVALIDATE | VM_CAN_NONLINEAR; 229ccd979bdSMark Fasheh return 0; 230ccd979bdSMark Fasheh } 231ccd979bdSMark Fasheh 232