Whamcloud - gitweb
9092e980ea1cb1113da51d7752c6515d310db58e
[fs/lustre-release.git] / lustre / llite / llite_mmap.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
43 #include <linux/version.h>
44 #include <asm/uaccess.h>
45
46 #include <linux/fs.h>
47 #include <linux/stat.h>
48 #include <asm/uaccess.h>
49 #include <linux/mm.h>
50 #include <linux/pagemap.h>
51
52 #define DEBUG_SUBSYSTEM S_LLITE
53
54 #include "llite_internal.h"
55 #include <lustre_compat.h>
56
57 static const struct vm_operations_struct ll_file_vm_ops;
58
59 void policy_from_vma(union ldlm_policy_data *policy, struct vm_area_struct *vma,
60                      unsigned long addr, size_t count)
61 {
62         policy->l_extent.start = ((addr - vma->vm_start) & PAGE_MASK) +
63                                  (vma->vm_pgoff << PAGE_CACHE_SHIFT);
64         policy->l_extent.end = (policy->l_extent.start + count - 1) |
65                                ~PAGE_MASK;
66 }
67
68 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
69                                size_t count)
70 {
71         struct vm_area_struct *vma, *ret = NULL;
72         ENTRY;
73
74         /* mmap_sem must have been held by caller. */
75         LASSERT(!down_write_trylock(&mm->mmap_sem));
76
77         for(vma = find_vma(mm, addr);
78             vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
79                 if (vma->vm_ops && vma->vm_ops == &ll_file_vm_ops &&
80                     vma->vm_flags & VM_SHARED) {
81                         ret = vma;
82                         break;
83                 }
84         }
85         RETURN(ret);
86 }
87
88 /**
89  * API independent part for page fault initialization.
90  * \param vma - virtual memory area addressed to page fault
91  * \param env - corespondent lu_env to processing
92  * \param nest - nested level
93  * \param index - page index corespondent to fault.
94  * \parm ra_flags - vma readahead flags.
95  *
96  * \return allocated and initialized env for fault operation.
97  * \retval EINVAL if env can't allocated
98  * \return other error codes from cl_io_init.
99  */
100 static struct cl_io *
101 ll_fault_io_init(struct vm_area_struct *vma, struct lu_env **env_ret,
102                  struct cl_env_nest *nest, pgoff_t index,
103                  unsigned long *ra_flags)
104 {
105         struct file            *file = vma->vm_file;
106         struct inode           *inode = file->f_path.dentry->d_inode;
107         struct cl_io           *io;
108         struct cl_fault_io     *fio;
109         struct lu_env          *env;
110         int                     rc;
111         ENTRY;
112
113         *env_ret = NULL;
114         if (ll_file_nolock(file))
115                 RETURN(ERR_PTR(-EOPNOTSUPP));
116
117         /*
118          * page fault can be called when lustre IO is
119          * already active for the current thread, e.g., when doing read/write
120          * against user level buffer mapped from Lustre buffer. To avoid
121          * stomping on existing context, optionally force an allocation of a new
122          * one.
123          */
124         env = cl_env_nested_get(nest);
125         if (IS_ERR(env))
126                  RETURN(ERR_PTR(-EINVAL));
127
128         *env_ret = env;
129
130         io = vvp_env_thread_io(env);
131         io->ci_obj = ll_i2info(inode)->lli_clob;
132         LASSERT(io->ci_obj != NULL);
133
134         fio = &io->u.ci_fault;
135         fio->ft_index      = index;
136         fio->ft_executable = vma->vm_flags&VM_EXEC;
137
138         /*
139          * disable VM_SEQ_READ and use VM_RAND_READ to make sure that
140          * the kernel will not read other pages not covered by ldlm in
141          * filemap_nopage. we do our readahead in ll_readpage.
142          */
143         if (ra_flags != NULL)
144                 *ra_flags = vma->vm_flags & (VM_RAND_READ|VM_SEQ_READ);
145         vma->vm_flags &= ~VM_SEQ_READ;
146         vma->vm_flags |= VM_RAND_READ;
147
148         CDEBUG(D_MMAP, "vm_flags: %lx (%lu %d)\n", vma->vm_flags,
149                fio->ft_index, fio->ft_executable);
150
151         rc = cl_io_init(env, io, CIT_FAULT, io->ci_obj);
152         if (rc == 0) {
153                 struct vvp_io *vio = vvp_env_io(env);
154                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
155
156                 LASSERT(vio->vui_cl.cis_io == io);
157
158                 /* mmap lock must be MANDATORY it has to cache
159                  * pages. */
160                 io->ci_lockreq = CILR_MANDATORY;
161                 vio->vui_fd = fd;
162         } else {
163                 LASSERT(rc < 0);
164                 cl_io_fini(env, io);
165                 cl_env_nested_put(nest, env);
166                 io = ERR_PTR(rc);
167         }
168
169         return io;
170 }
171
172 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
173 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
174                             bool *retry)
175 {
176         struct lu_env           *env;
177         struct cl_io            *io;
178         struct vvp_io           *vio;
179         struct cl_env_nest       nest;
180         int                      result;
181         sigset_t                 set;
182         struct inode             *inode;
183         struct ll_inode_info     *lli;
184         ENTRY;
185
186         LASSERT(vmpage != NULL);
187
188         io = ll_fault_io_init(vma, &env,  &nest, vmpage->index, NULL);
189         if (IS_ERR(io))
190                 GOTO(out, result = PTR_ERR(io));
191
192         result = io->ci_result;
193         if (result < 0)
194                 GOTO(out_io, result);
195
196         io->u.ci_fault.ft_mkwrite = 1;
197         io->u.ci_fault.ft_writable = 1;
198
199         vio = vvp_env_io(env);
200         vio->u.fault.ft_vma    = vma;
201         vio->u.fault.ft_vmpage = vmpage;
202
203         set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
204
205         /* we grab lli_trunc_sem to exclude truncate case.
206          * Otherwise, we could add dirty pages into osc cache
207          * while truncate is on-going. */
208         inode = vvp_object_inode(io->ci_obj);
209         lli = ll_i2info(inode);
210         down_read(&lli->lli_trunc_sem);
211
212         result = cl_io_loop(env, io);
213
214         up_read(&lli->lli_trunc_sem);
215
216         cfs_restore_sigs(set);
217
218         if (result == 0) {
219                 lock_page(vmpage);
220                 if (vmpage->mapping == NULL) {
221                         unlock_page(vmpage);
222
223                         /* page was truncated and lock was cancelled, return
224                          * ENODATA so that VM_FAULT_NOPAGE will be returned
225                          * to handle_mm_fault(). */
226                         if (result == 0)
227                                 result = -ENODATA;
228                 } else if (!PageDirty(vmpage)) {
229                         /* race, the page has been cleaned by ptlrpcd after
230                          * it was unlocked, it has to be added into dirty
231                          * cache again otherwise this soon-to-dirty page won't
232                          * consume any grants, even worse if this page is being
233                          * transferred because it will break RPC checksum.
234                          */
235                         unlock_page(vmpage);
236
237                         CDEBUG(D_MMAP, "Race on page_mkwrite %p/%lu, page has "
238                                "been written out, retry.\n",
239                                vmpage, vmpage->index);
240
241                         *retry = true;
242                         result = -EAGAIN;
243                 }
244
245                 if (result == 0)
246                         ll_file_set_flag(lli, LLIF_DATA_MODIFIED);
247         }
248         EXIT;
249
250 out_io:
251         cl_io_fini(env, io);
252         cl_env_nested_put(&nest, env);
253 out:
254         CDEBUG(D_MMAP, "%s mkwrite with %d\n", current->comm, result);
255         LASSERT(ergo(result == 0, PageLocked(vmpage)));
256
257         return result;
258 }
259
260 static inline int to_fault_error(int result)
261 {
262         switch(result) {
263         case 0:
264                 result = VM_FAULT_LOCKED;
265                 break;
266         case -EFAULT:
267                 result = VM_FAULT_NOPAGE;
268                 break;
269         case -ENOMEM:
270                 result = VM_FAULT_OOM;
271                 break;
272         default:
273                 result = VM_FAULT_SIGBUS;
274                 break;
275         }
276         return result;
277 }
278
279 /**
280  * Lustre implementation of a vm_operations_struct::fault() method, called by
281  * VM to server page fault (both in kernel and user space).
282  *
283  * \param vma - is virtiual area struct related to page fault
284  * \param vmf - structure which describe type and address where hit fault
285  *
286  * \return allocated and filled _locked_ page for address
287  * \retval VM_FAULT_ERROR on general error
288  * \retval NOPAGE_OOM not have memory for allocate new page
289  */
290 static int ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
291 {
292         struct lu_env           *env;
293         struct cl_io            *io;
294         struct vvp_io           *vio = NULL;
295         struct page             *vmpage;
296         unsigned long            ra_flags;
297         struct cl_env_nest       nest;
298         int                      result;
299         int                      fault_ret = 0;
300         ENTRY;
301
302         io = ll_fault_io_init(vma, &env,  &nest, vmf->pgoff, &ra_flags);
303         if (IS_ERR(io))
304                 RETURN(to_fault_error(PTR_ERR(io)));
305
306         result = io->ci_result;
307         if (result == 0) {
308                 vio = vvp_env_io(env);
309                 vio->u.fault.ft_vma       = vma;
310                 vio->u.fault.ft_vmpage    = NULL;
311                 vio->u.fault.ft_vmf = vmf;
312                 vio->u.fault.ft_flags = 0;
313                 vio->u.fault.ft_flags_valid = 0;
314
315                 /* May call ll_readpage() */
316                 ll_cl_add(vma->vm_file, env, io);
317
318                 result = cl_io_loop(env, io);
319
320                 ll_cl_remove(vma->vm_file, env);
321
322                 /* ft_flags are only valid if we reached
323                  * the call to filemap_fault */
324                 if (vio->u.fault.ft_flags_valid)
325                         fault_ret = vio->u.fault.ft_flags;
326
327                 vmpage = vio->u.fault.ft_vmpage;
328                 if (result != 0 && vmpage != NULL) {
329                         page_cache_release(vmpage);
330                         vmf->page = NULL;
331                 }
332         }
333         cl_io_fini(env, io);
334         cl_env_nested_put(&nest, env);
335
336         vma->vm_flags |= ra_flags;
337         if (result != 0 && !(fault_ret & VM_FAULT_RETRY))
338                 fault_ret |= to_fault_error(result);
339
340         CDEBUG(D_MMAP, "%s fault %d/%d\n",
341                current->comm, fault_ret, result);
342         RETURN(fault_ret);
343 }
344
345 static int ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
346 {
347         int count = 0;
348         bool printed = false;
349         int result;
350         sigset_t set;
351
352         /* Only SIGKILL and SIGTERM is allowed for fault/nopage/mkwrite
353          * so that it can be killed by admin but not cause segfault by
354          * other signals. */
355         set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
356
357 restart:
358         result = ll_fault0(vma, vmf);
359         LASSERT(!(result & VM_FAULT_LOCKED));
360         if (result == 0) {
361                 struct page *vmpage = vmf->page;
362
363                 /* check if this page has been truncated */
364                 lock_page(vmpage);
365                 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
366                         unlock_page(vmpage);
367                         page_cache_release(vmpage);
368                         vmf->page = NULL;
369
370                         if (!printed && ++count > 16) {
371                                 CWARN("the page is under heavy contention,"
372                                       "maybe your app(%s) needs revising :-)\n",
373                                       current->comm);
374                                 printed = true;
375                         }
376
377                         goto restart;
378                 }
379
380                 result |= VM_FAULT_LOCKED;
381         }
382         cfs_restore_sigs(set);
383         return result;
384 }
385
386 static int ll_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
387 {
388         int count = 0;
389         bool printed = false;
390         bool retry;
391         int result;
392
393         do {
394                 retry = false;
395                 result = ll_page_mkwrite0(vma, vmf->page, &retry);
396
397                 if (!printed && ++count > 16) {
398                         const struct dentry *de = vma->vm_file->f_path.dentry;
399
400                         CWARN("app(%s): the page %lu of file "DFID" is under"
401                               " heavy contention\n",
402                               current->comm, vmf->pgoff,
403                               PFID(ll_inode2fid(de->d_inode)));
404                         printed = true;
405                 }
406         } while (retry);
407
408         switch(result) {
409         case 0:
410                 LASSERT(PageLocked(vmf->page));
411                 result = VM_FAULT_LOCKED;
412                 break;
413         case -ENODATA:
414         case -EFAULT:
415                 result = VM_FAULT_NOPAGE;
416                 break;
417         case -ENOMEM:
418                 result = VM_FAULT_OOM;
419                 break;
420         case -EAGAIN:
421                 result = VM_FAULT_RETRY;
422                 break;
423         default:
424                 result = VM_FAULT_SIGBUS;
425                 break;
426         }
427
428         return result;
429 }
430
431 /**
432  *  To avoid cancel the locks covering mmapped region for lock cache pressure,
433  *  we track the mapped vma count in vvp_object::vob_mmap_cnt.
434  */
435 static void ll_vm_open(struct vm_area_struct * vma)
436 {
437         struct inode *inode    = vma->vm_file->f_path.dentry->d_inode;
438         struct vvp_object *vob = cl_inode2vvp(inode);
439
440         ENTRY;
441         LASSERT(vma->vm_file);
442         LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
443         atomic_inc(&vob->vob_mmap_cnt);
444         EXIT;
445 }
446
447 /**
448  * Dual to ll_vm_open().
449  */
450 static void ll_vm_close(struct vm_area_struct *vma)
451 {
452         struct inode      *inode = vma->vm_file->f_path.dentry->d_inode;
453         struct vvp_object *vob   = cl_inode2vvp(inode);
454
455         ENTRY;
456         LASSERT(vma->vm_file);
457         atomic_dec(&vob->vob_mmap_cnt);
458         LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
459         EXIT;
460 }
461
462 /* XXX put nice comment here.  talk about __free_pte -> dirty pages and
463  * nopage's reference passing to the pte */
464 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last)
465 {
466         int rc = -ENOENT;
467         ENTRY;
468
469         LASSERTF(last > first, "last "LPU64" first "LPU64"\n", last, first);
470         if (mapping_mapped(mapping)) {
471                 rc = 0;
472                 unmap_mapping_range(mapping, first + PAGE_CACHE_SIZE - 1,
473                                     last - first + 1, 0);
474         }
475
476         RETURN(rc);
477 }
478
479 static const struct vm_operations_struct ll_file_vm_ops = {
480         .fault                  = ll_fault,
481         .page_mkwrite           = ll_page_mkwrite,
482         .open                   = ll_vm_open,
483         .close                  = ll_vm_close,
484 };
485
486 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
487 {
488         struct inode *inode = file->f_path.dentry->d_inode;
489         int rc;
490         ENTRY;
491
492         if (ll_file_nolock(file))
493                 RETURN(-EOPNOTSUPP);
494
495         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MAP, 1);
496         rc = generic_file_mmap(file, vma);
497         if (rc == 0) {
498                 vma->vm_ops = &ll_file_vm_ops;
499                 vma->vm_ops->open(vma);
500                 /* update the inode's size and mtime */
501                 rc = ll_glimpse_size(inode);
502         }
503
504         RETURN(rc);
505 }