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