Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * mmap.c
0004  *
0005  * Code to deal with the mess that is clustered mmap.
0006  *
0007  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
0008  */
0009 
0010 #include <linux/fs.h>
0011 #include <linux/types.h>
0012 #include <linux/highmem.h>
0013 #include <linux/pagemap.h>
0014 #include <linux/uio.h>
0015 #include <linux/signal.h>
0016 #include <linux/rbtree.h>
0017 
0018 #include <cluster/masklog.h>
0019 
0020 #include "ocfs2.h"
0021 
0022 #include "aops.h"
0023 #include "dlmglue.h"
0024 #include "file.h"
0025 #include "inode.h"
0026 #include "mmap.h"
0027 #include "super.h"
0028 #include "ocfs2_trace.h"
0029 
0030 
0031 static vm_fault_t ocfs2_fault(struct vm_fault *vmf)
0032 {
0033     struct vm_area_struct *vma = vmf->vma;
0034     sigset_t oldset;
0035     vm_fault_t ret;
0036 
0037     ocfs2_block_signals(&oldset);
0038     ret = filemap_fault(vmf);
0039     ocfs2_unblock_signals(&oldset);
0040 
0041     trace_ocfs2_fault(OCFS2_I(vma->vm_file->f_mapping->host)->ip_blkno,
0042               vma, vmf->page, vmf->pgoff);
0043     return ret;
0044 }
0045 
0046 static vm_fault_t __ocfs2_page_mkwrite(struct file *file,
0047             struct buffer_head *di_bh, struct page *page)
0048 {
0049     int err;
0050     vm_fault_t ret = VM_FAULT_NOPAGE;
0051     struct inode *inode = file_inode(file);
0052     struct address_space *mapping = inode->i_mapping;
0053     loff_t pos = page_offset(page);
0054     unsigned int len = PAGE_SIZE;
0055     pgoff_t last_index;
0056     struct page *locked_page = NULL;
0057     void *fsdata;
0058     loff_t size = i_size_read(inode);
0059 
0060     last_index = (size - 1) >> PAGE_SHIFT;
0061 
0062     /*
0063      * There are cases that lead to the page no longer belonging to the
0064      * mapping.
0065      * 1) pagecache truncates locally due to memory pressure.
0066      * 2) pagecache truncates when another is taking EX lock against 
0067      * inode lock. see ocfs2_data_convert_worker.
0068      * 
0069      * The i_size check doesn't catch the case where nodes truncated and
0070      * then re-extended the file. We'll re-check the page mapping after
0071      * taking the page lock inside of ocfs2_write_begin_nolock().
0072      *
0073      * Let VM retry with these cases.
0074      */
0075     if ((page->mapping != inode->i_mapping) ||
0076         (!PageUptodate(page)) ||
0077         (page_offset(page) >= size))
0078         goto out;
0079 
0080     /*
0081      * Call ocfs2_write_begin() and ocfs2_write_end() to take
0082      * advantage of the allocation code there. We pass a write
0083      * length of the whole page (chopped to i_size) to make sure
0084      * the whole thing is allocated.
0085      *
0086      * Since we know the page is up to date, we don't have to
0087      * worry about ocfs2_write_begin() skipping some buffer reads
0088      * because the "write" would invalidate their data.
0089      */
0090     if (page->index == last_index)
0091         len = ((size - 1) & ~PAGE_MASK) + 1;
0092 
0093     err = ocfs2_write_begin_nolock(mapping, pos, len, OCFS2_WRITE_MMAP,
0094                        &locked_page, &fsdata, di_bh, page);
0095     if (err) {
0096         if (err != -ENOSPC)
0097             mlog_errno(err);
0098         ret = vmf_error(err);
0099         goto out;
0100     }
0101 
0102     if (!locked_page) {
0103         ret = VM_FAULT_NOPAGE;
0104         goto out;
0105     }
0106     err = ocfs2_write_end_nolock(mapping, pos, len, len, fsdata);
0107     BUG_ON(err != len);
0108     ret = VM_FAULT_LOCKED;
0109 out:
0110     return ret;
0111 }
0112 
0113 static vm_fault_t ocfs2_page_mkwrite(struct vm_fault *vmf)
0114 {
0115     struct page *page = vmf->page;
0116     struct inode *inode = file_inode(vmf->vma->vm_file);
0117     struct buffer_head *di_bh = NULL;
0118     sigset_t oldset;
0119     int err;
0120     vm_fault_t ret;
0121 
0122     sb_start_pagefault(inode->i_sb);
0123     ocfs2_block_signals(&oldset);
0124 
0125     /*
0126      * The cluster locks taken will block a truncate from another
0127      * node. Taking the data lock will also ensure that we don't
0128      * attempt page truncation as part of a downconvert.
0129      */
0130     err = ocfs2_inode_lock(inode, &di_bh, 1);
0131     if (err < 0) {
0132         mlog_errno(err);
0133         ret = vmf_error(err);
0134         goto out;
0135     }
0136 
0137     /*
0138      * The alloc sem should be enough to serialize with
0139      * ocfs2_truncate_file() changing i_size as well as any thread
0140      * modifying the inode btree.
0141      */
0142     down_write(&OCFS2_I(inode)->ip_alloc_sem);
0143 
0144     ret = __ocfs2_page_mkwrite(vmf->vma->vm_file, di_bh, page);
0145 
0146     up_write(&OCFS2_I(inode)->ip_alloc_sem);
0147 
0148     brelse(di_bh);
0149     ocfs2_inode_unlock(inode, 1);
0150 
0151 out:
0152     ocfs2_unblock_signals(&oldset);
0153     sb_end_pagefault(inode->i_sb);
0154     return ret;
0155 }
0156 
0157 static const struct vm_operations_struct ocfs2_file_vm_ops = {
0158     .fault      = ocfs2_fault,
0159     .page_mkwrite   = ocfs2_page_mkwrite,
0160 };
0161 
0162 int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
0163 {
0164     int ret = 0, lock_level = 0;
0165 
0166     ret = ocfs2_inode_lock_atime(file_inode(file),
0167                     file->f_path.mnt, &lock_level, 1);
0168     if (ret < 0) {
0169         mlog_errno(ret);
0170         goto out;
0171     }
0172     ocfs2_inode_unlock(file_inode(file), lock_level);
0173 out:
0174     vma->vm_ops = &ocfs2_file_vm_ops;
0175     return 0;
0176 }
0177