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 { 64*5cffff9eSWengang Wang int ret = VM_FAULT_NOPAGE; 650378da0fSTao Ma struct inode *inode = file->f_path.dentry->d_inode; 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 /* 77*5cffff9eSWengang Wang * There are cases that lead to the page no longer bebongs to the 78*5cffff9eSWengang Wang * mapping. 79*5cffff9eSWengang Wang * 1) pagecache truncates locally due to memory pressure. 80*5cffff9eSWengang Wang * 2) pagecache truncates when another is taking EX lock against 81*5cffff9eSWengang Wang * inode lock. see ocfs2_data_convert_worker. 82*5cffff9eSWengang Wang * 83*5cffff9eSWengang Wang * The i_size check doesn't catch the case where nodes truncated and 84*5cffff9eSWengang Wang * then re-extended the file. We'll re-check the page mapping after 85*5cffff9eSWengang Wang * taking the page lock inside of ocfs2_write_begin_nolock(). 86*5cffff9eSWengang Wang * 87*5cffff9eSWengang Wang * Let VM retry with these cases. 887307de80SMark Fasheh */ 89*5cffff9eSWengang Wang if ((page->mapping != inode->i_mapping) || 90*5cffff9eSWengang Wang (!PageUptodate(page)) || 91*5cffff9eSWengang 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); 112*5cffff9eSWengang Wang if (ret == -ENOMEM) 113*5cffff9eSWengang Wang ret = VM_FAULT_OOM; 114*5cffff9eSWengang Wang else 115*5cffff9eSWengang Wang ret = VM_FAULT_SIGBUS; 1167307de80SMark Fasheh goto out; 1177307de80SMark Fasheh } 1187307de80SMark Fasheh 119*5cffff9eSWengang Wang if (!locked_page) { 120*5cffff9eSWengang Wang ret = VM_FAULT_NOPAGE; 1217307de80SMark Fasheh goto out; 1227307de80SMark Fasheh } 123*5cffff9eSWengang Wang ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page, 124*5cffff9eSWengang Wang fsdata); 1257307de80SMark Fasheh BUG_ON(ret != len); 126*5cffff9eSWengang 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; 1347307de80SMark Fasheh struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 1357307de80SMark Fasheh struct buffer_head *di_bh = NULL; 136e4b963f1SJoel Becker sigset_t oldset; 137e4b963f1SJoel Becker int ret; 1387307de80SMark Fasheh 139e4b963f1SJoel Becker ocfs2_block_signals(&oldset); 1407307de80SMark Fasheh 1417307de80SMark Fasheh /* 1427307de80SMark Fasheh * The cluster locks taken will block a truncate from another 1437307de80SMark Fasheh * node. Taking the data lock will also ensure that we don't 1447307de80SMark Fasheh * attempt page truncation as part of a downconvert. 1457307de80SMark Fasheh */ 146e63aecb6SMark Fasheh ret = ocfs2_inode_lock(inode, &di_bh, 1); 1477307de80SMark Fasheh if (ret < 0) { 1487307de80SMark Fasheh mlog_errno(ret); 1497307de80SMark Fasheh goto out; 1507307de80SMark Fasheh } 1517307de80SMark Fasheh 1527307de80SMark Fasheh /* 1537307de80SMark Fasheh * The alloc sem should be enough to serialize with 1547307de80SMark Fasheh * ocfs2_truncate_file() changing i_size as well as any thread 1557307de80SMark Fasheh * modifying the inode btree. 1567307de80SMark Fasheh */ 1577307de80SMark Fasheh down_write(&OCFS2_I(inode)->ip_alloc_sem); 1587307de80SMark Fasheh 1590378da0fSTao Ma ret = __ocfs2_page_mkwrite(vma->vm_file, di_bh, page); 1607307de80SMark Fasheh 1617307de80SMark Fasheh up_write(&OCFS2_I(inode)->ip_alloc_sem); 1627307de80SMark Fasheh 1637307de80SMark Fasheh brelse(di_bh); 164e63aecb6SMark Fasheh ocfs2_inode_unlock(inode, 1); 1657307de80SMark Fasheh 1667307de80SMark Fasheh out: 167e4b963f1SJoel Becker ocfs2_unblock_signals(&oldset); 1687307de80SMark Fasheh return ret; 1697307de80SMark Fasheh } 1707307de80SMark Fasheh 171f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct ocfs2_file_vm_ops = { 17254cb8821SNick Piggin .fault = ocfs2_fault, 1737307de80SMark Fasheh .page_mkwrite = ocfs2_page_mkwrite, 174ccd979bdSMark Fasheh }; 175ccd979bdSMark Fasheh 176870f4817SChristoph Hellwig int ocfs2_mmap(struct file *file, struct vm_area_struct *vma) 177ccd979bdSMark Fasheh { 17825899deeSTiger Yang int ret = 0, lock_level = 0; 179ccd979bdSMark Fasheh 180e63aecb6SMark Fasheh ret = ocfs2_inode_lock_atime(file->f_dentry->d_inode, 18125899deeSTiger Yang file->f_vfsmnt, &lock_level); 18225899deeSTiger Yang if (ret < 0) { 18325899deeSTiger Yang mlog_errno(ret); 18425899deeSTiger Yang goto out; 18525899deeSTiger Yang } 186e63aecb6SMark Fasheh ocfs2_inode_unlock(file->f_dentry->d_inode, lock_level); 18725899deeSTiger Yang out: 188ccd979bdSMark Fasheh vma->vm_ops = &ocfs2_file_vm_ops; 189d0217ac0SNick Piggin vma->vm_flags |= VM_CAN_NONLINEAR; 190ccd979bdSMark Fasheh return 0; 191ccd979bdSMark Fasheh } 192ccd979bdSMark Fasheh 193