4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #ifndef AUTOCONF_INCLUDED
38 #include <linux/config.h>
40 #include <linux/kernel.h>
42 #include <linux/string.h>
43 #include <linux/stat.h>
44 #include <linux/errno.h>
45 #include <linux/unistd.h>
46 #include <linux/version.h>
47 #include <asm/system.h>
48 #include <asm/uaccess.h>
51 #include <linux/stat.h>
52 #include <asm/uaccess.h>
54 #include <linux/pagemap.h>
56 #define DEBUG_SUBSYSTEM S_LLITE
58 #include <lustre_lite.h>
59 #include "llite_internal.h"
60 #include <linux/lustre_compat25.h>
62 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
65 static struct vm_operations_struct ll_file_vm_ops;
67 void policy_from_vma(ldlm_policy_data_t *policy,
68 struct vm_area_struct *vma, unsigned long addr,
71 policy->l_extent.start = ((addr - vma->vm_start) & CFS_PAGE_MASK) +
72 (vma->vm_pgoff << CFS_PAGE_SHIFT);
73 policy->l_extent.end = (policy->l_extent.start + count - 1) |
77 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
80 struct vm_area_struct *vma, *ret = NULL;
83 /* mmap_sem must have been held by caller. */
84 LASSERT(!down_write_trylock(&mm->mmap_sem));
86 for(vma = find_vma(mm, addr);
87 vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
88 if (vma->vm_ops && vma->vm_ops == &ll_file_vm_ops &&
89 vma->vm_flags & VM_SHARED) {
98 * API independent part for page fault initialization.
99 * \param vma - virtual memory area addressed to page fault
100 * \param env - corespondent lu_env to processing
101 * \param nest - nested level
102 * \param index - page index corespondent to fault.
103 * \parm ra_flags - vma readahead flags.
105 * \return allocated and initialized env for fault operation.
106 * \retval EINVAL if env can't allocated
107 * \return other error codes from cl_io_init.
109 struct cl_io *ll_fault_io_init(struct vm_area_struct *vma,
110 struct lu_env **env_ret,
111 struct cl_env_nest *nest,
112 pgoff_t index, unsigned long *ra_flags)
114 struct file *file = vma->vm_file;
115 struct inode *inode = file->f_dentry->d_inode;
117 struct cl_fault_io *fio;
122 if (ll_file_nolock(file))
123 RETURN(ERR_PTR(-EOPNOTSUPP));
126 * page fault can be called when lustre IO is
127 * already active for the current thread, e.g., when doing read/write
128 * against user level buffer mapped from Lustre buffer. To avoid
129 * stomping on existing context, optionally force an allocation of a new
132 env = cl_env_nested_get(nest);
134 RETURN(ERR_PTR(-EINVAL));
138 io = ccc_env_thread_io(env);
139 io->ci_obj = ll_i2info(inode)->lli_clob;
140 LASSERT(io->ci_obj != NULL);
142 fio = &io->u.ci_fault;
143 fio->ft_index = index;
144 fio->ft_executable = vma->vm_flags&VM_EXEC;
147 * disable VM_SEQ_READ and use VM_RAND_READ to make sure that
148 * the kernel will not read other pages not covered by ldlm in
149 * filemap_nopage. we do our readahead in ll_readpage.
151 if (ra_flags != NULL)
152 *ra_flags = vma->vm_flags & (VM_RAND_READ|VM_SEQ_READ);
153 vma->vm_flags &= ~VM_SEQ_READ;
154 vma->vm_flags |= VM_RAND_READ;
156 CDEBUG(D_MMAP, "vm_flags: %lx (%lu %d)\n", vma->vm_flags,
157 fio->ft_index, fio->ft_executable);
159 if (cl_io_init(env, io, CIT_FAULT, io->ci_obj) == 0) {
160 struct ccc_io *cio = ccc_env_io(env);
161 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
163 LASSERT(cio->cui_cl.cis_io == io);
165 /* mmap lock must be MANDATORY
166 * it has to cache pages. */
167 io->ci_lockreq = CILR_MANDATORY;
175 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
176 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
182 struct cl_env_nest nest;
187 LASSERT(vmpage != NULL);
189 io = ll_fault_io_init(vma, &env, &nest, vmpage->index, NULL);
191 GOTO(out, result = PTR_ERR(io));
193 result = io->ci_result;
197 /* Don't enqueue new locks for page_mkwrite().
198 * If the lock has been cancelled then page must have been
199 * truncated, in that case, kernel will handle it.
201 io->ci_lockreq = CILR_PEEK;
202 io->u.ci_fault.ft_mkwrite = 1;
203 io->u.ci_fault.ft_writable = 1;
205 vio = vvp_env_io(env);
206 vio->u.fault.ft_vma = vma;
207 vio->u.fault.ft_vmpage = vmpage;
209 set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
210 result = cl_io_loop(env, io);
211 cfs_restore_sigs(set);
213 if (result == -ENODATA) /* peek failed, no lock caching. */
214 CDEBUG(D_MMAP, "race on page_mkwrite: %lx (%lu %p)\n",
215 vma->vm_flags, io->u.ci_fault.ft_index, vmpage);
217 if (result == 0 || result == -ENODATA) {
219 if (vmpage->mapping == NULL) {
222 /* page was truncated and lock was cancelled, return
223 * ENODATA so that VM_FAULT_NOPAGE will be returned
224 * to handle_mm_fault(). */
227 } else if (result == -ENODATA) {
228 /* Invalidate it if the cl_lock is being revoked.
229 * This piece of code is definitely needed for RHEL5,
230 * otherwise, SIGBUS will be wrongly returned to
232 write_one_page(vmpage, 1);
234 if (vmpage->mapping != NULL) {
235 ll_invalidate_page(vmpage);
236 LASSERT(vmpage->mapping == NULL);
239 } else if (!PageDirty(vmpage)) {
240 /* race, the page has been cleaned by ptlrpcd after
241 * it was unlocked, it has to be added into dirty
242 * cache again otherwise this soon-to-dirty page won't
243 * consume any grants, even worse if this page is being
244 * transferred because it will break RPC checksum.
248 CDEBUG(D_MMAP, "Race on page_mkwrite %p/%lu, page has "
249 "been written out, retry.\n",
250 vmpage, vmpage->index);
260 cl_env_nested_put(&nest, env);
262 CDEBUG(D_MMAP, "%s mkwrite with %d\n", cfs_current()->comm, result);
264 LASSERT(ergo(result == 0, PageLocked(vmpage)));
269 #ifndef HAVE_VM_OP_FAULT
271 * Lustre implementation of a vm_operations_struct::nopage() method, called by
272 * VM to server page fault (both in kernel and user space).
274 * This function sets up CIT_FAULT cl_io that does the job.
276 * \param vma - is virtiual area struct related to page fault
277 * \param address - address when hit fault
278 * \param type - of fault
280 * \return allocated and filled _unlocked_ page for address
281 * \retval NOPAGE_SIGBUS if page not exist on this address
282 * \retval NOPAGE_OOM not have memory for allocate new page
284 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
288 struct cl_env_nest nest;
290 struct page *page = NOPAGE_SIGBUS;
291 struct vvp_io *vio = NULL;
292 unsigned long ra_flags;
295 const unsigned long writable = VM_SHARED|VM_WRITE;
299 pg_offset = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
300 io = ll_fault_io_init(vma, &env, &nest, pg_offset, &ra_flags);
302 return NOPAGE_SIGBUS;
304 result = io->ci_result;
308 io->u.ci_fault.ft_writable = (vma->vm_flags&writable) == writable;
310 vio = vvp_env_io(env);
311 vio->u.fault.ft_vma = vma;
312 vio->u.fault.nopage.ft_address = address;
313 vio->u.fault.nopage.ft_type = type;
314 vio->u.fault.ft_vmpage = NULL;
316 set = cfs_block_sigsinv(sigmask(SIGKILL)|sigmask(SIGTERM));
317 result = cl_io_loop(env, io);
318 cfs_restore_sigs(set);
320 page = vio->u.fault.ft_vmpage;
321 if (result != 0 && page != NULL) {
322 page_cache_release(page);
323 page = NOPAGE_SIGBUS;
327 if (result == -ENOMEM)
330 vma->vm_flags &= ~VM_RAND_READ;
331 vma->vm_flags |= ra_flags;
334 cl_env_nested_put(&nest, env);
341 static inline int to_fault_error(int result)
345 result = VM_FAULT_LOCKED;
348 result = VM_FAULT_NOPAGE;
351 result = VM_FAULT_OOM;
354 result = VM_FAULT_SIGBUS;
361 * Lustre implementation of a vm_operations_struct::fault() method, called by
362 * VM to server page fault (both in kernel and user space).
364 * \param vma - is virtiual area struct related to page fault
365 * \param vmf - structure which describe type and address where hit fault
367 * \return allocated and filled _locked_ page for address
368 * \retval VM_FAULT_ERROR on general error
369 * \retval NOPAGE_OOM not have memory for allocate new page
371 static int ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
375 struct vvp_io *vio = NULL;
377 unsigned long ra_flags;
378 struct cl_env_nest nest;
383 io = ll_fault_io_init(vma, &env, &nest, vmf->pgoff, &ra_flags);
385 RETURN(to_fault_error(PTR_ERR(io)));
387 result = io->ci_result;
389 vio = vvp_env_io(env);
390 vio->u.fault.ft_vma = vma;
391 vio->u.fault.ft_vmpage = NULL;
392 vio->u.fault.fault.ft_vmf = vmf;
394 result = cl_io_loop(env, io);
396 fault_ret = vio->u.fault.fault.ft_flags;
397 vmpage = vio->u.fault.ft_vmpage;
398 if (result != 0 && vmpage != NULL) {
399 page_cache_release(vmpage);
404 cl_env_nested_put(&nest, env);
406 vma->vm_flags |= ra_flags;
407 if (result != 0 && !(fault_ret & VM_FAULT_RETRY))
408 fault_ret |= to_fault_error(result);
410 CDEBUG(D_MMAP, "%s fault %d/%d\n",
411 cfs_current()->comm, fault_ret, result);
415 static int ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
418 bool printed = false;
422 /* Only SIGKILL and SIGTERM is allowed for fault/nopage/mkwrite
423 * so that it can be killed by admin but not cause segfault by
425 set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
428 result = ll_fault0(vma, vmf);
429 LASSERT(!(result & VM_FAULT_LOCKED));
431 struct page *vmpage = vmf->page;
433 /* check if this page has been truncated */
435 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
437 page_cache_release(vmpage);
440 if (!printed && ++count > 16) {
441 CWARN("the page is under heavy contention,"
442 "maybe your app(%s) needs revising :-)\n",
450 result |= VM_FAULT_LOCKED;
452 cfs_restore_sigs(set);
457 #ifndef HAVE_PGMKWRITE_USE_VMFAULT
458 static int ll_page_mkwrite(struct vm_area_struct *vma, struct page *vmpage)
461 bool printed = false;
467 result = ll_page_mkwrite0(vma, vmpage, &retry);
469 if (!printed && ++count > 16) {
470 CWARN("app(%s): the page %lu of file %lu is under heavy"
472 current->comm, page_index(vmpage),
473 vma->vm_file->f_dentry->d_inode->i_ino);
480 else if (result == -ENODATA)
481 result = 0; /* kernel will know truncate has happened and
487 static int ll_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
490 bool printed = false;
496 result = ll_page_mkwrite0(vma, vmf->page, &retry);
498 if (!printed && ++count > 16) {
499 CWARN("app(%s): the page %lu of file %lu is under heavy"
501 current->comm, vmf->pgoff,
502 vma->vm_file->f_dentry->d_inode->i_ino);
509 LASSERT(PageLocked(vmf->page));
510 result = VM_FAULT_LOCKED;
514 result = VM_FAULT_NOPAGE;
517 result = VM_FAULT_OOM;
520 result = VM_FAULT_RETRY;
523 result = VM_FAULT_SIGBUS;
532 * To avoid cancel the locks covering mmapped region for lock cache pressure,
533 * we track the mapped vma count in ccc_object::cob_mmap_cnt.
535 static void ll_vm_open(struct vm_area_struct * vma)
537 struct inode *inode = vma->vm_file->f_dentry->d_inode;
538 struct ccc_object *vob = cl_inode2ccc(inode);
541 LASSERT(vma->vm_file);
542 LASSERT(cfs_atomic_read(&vob->cob_mmap_cnt) >= 0);
543 cfs_atomic_inc(&vob->cob_mmap_cnt);
548 * Dual to ll_vm_open().
550 static void ll_vm_close(struct vm_area_struct *vma)
552 struct inode *inode = vma->vm_file->f_dentry->d_inode;
553 struct ccc_object *vob = cl_inode2ccc(inode);
556 LASSERT(vma->vm_file);
557 cfs_atomic_dec(&vob->cob_mmap_cnt);
558 LASSERT(cfs_atomic_read(&vob->cob_mmap_cnt) >= 0);
562 #ifndef HAVE_VM_OP_FAULT
563 #ifndef HAVE_FILEMAP_POPULATE
564 static int (*filemap_populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
566 static int ll_populate(struct vm_area_struct *area, unsigned long address,
567 unsigned long len, pgprot_t prot, unsigned long pgoff,
573 /* always set nonblock as true to avoid page read ahead */
574 rc = filemap_populate(area, address, len, prot, pgoff, 1);
579 /* return the user space pointer that maps to a file offset via a vma */
580 static inline unsigned long file_to_user(struct vm_area_struct *vma, __u64 byte)
582 return vma->vm_start + (byte - ((__u64)vma->vm_pgoff << CFS_PAGE_SHIFT));
586 /* XXX put nice comment here. talk about __free_pte -> dirty pages and
587 * nopage's reference passing to the pte */
588 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last)
593 LASSERTF(last > first, "last "LPU64" first "LPU64"\n", last, first);
594 if (mapping_mapped(mapping)) {
596 unmap_mapping_range(mapping, first + CFS_PAGE_SIZE - 1,
597 last - first + 1, 0);
603 static struct vm_operations_struct ll_file_vm_ops = {
604 #ifndef HAVE_VM_OP_FAULT
606 .populate = ll_populate,
611 .page_mkwrite = ll_page_mkwrite,
613 .close = ll_vm_close,
616 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
618 struct inode *inode = file->f_dentry->d_inode;
622 if (ll_file_nolock(file))
625 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MAP, 1);
626 rc = generic_file_mmap(file, vma);
628 #if !defined(HAVE_FILEMAP_POPULATE) && !defined(HAVE_VM_OP_FAULT)
629 if (!filemap_populate)
630 filemap_populate = vma->vm_ops->populate;
632 vma->vm_ops = &ll_file_vm_ops;
633 vma->vm_ops->open(vma);
634 /* update the inode's size and mtime */
635 rc = ll_glimpse_size(inode);