Whamcloud - gitweb
LU-1323 llite: find_vma() should be protected by mmap_sem
[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, 2012, Whamcloud, Inc.
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 #ifndef AUTOCONF_INCLUDED
38 #include <linux/config.h>
39 #endif
40 #include <linux/kernel.h>
41 #include <linux/mm.h>
42 #include <linux/string.h>
43 #include <linux/stat.h>
44 #include <linux/errno.h>
45 #include <linux/smp_lock.h>
46 #include <linux/unistd.h>
47 #include <linux/version.h>
48 #include <asm/system.h>
49 #include <asm/uaccess.h>
50
51 #include <linux/fs.h>
52 #include <linux/stat.h>
53 #include <asm/uaccess.h>
54 #include <linux/mm.h>
55 #include <linux/pagemap.h>
56 #include <linux/smp_lock.h>
57
58 #define DEBUG_SUBSYSTEM S_LLITE
59
60 #include <lustre_lite.h>
61 #include "llite_internal.h"
62 #include <linux/lustre_compat25.h>
63
64 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
65                        int *type);
66
67 static struct vm_operations_struct ll_file_vm_ops;
68
69 void policy_from_vma(ldlm_policy_data_t *policy,
70                             struct vm_area_struct *vma, unsigned long addr,
71                             size_t count)
72 {
73         policy->l_extent.start = ((addr - vma->vm_start) & CFS_PAGE_MASK) +
74                                  (vma->vm_pgoff << CFS_PAGE_SHIFT);
75         policy->l_extent.end = (policy->l_extent.start + count - 1) |
76                                ~CFS_PAGE_MASK;
77 }
78
79 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
80                                size_t count)
81 {
82         struct vm_area_struct *vma, *ret = NULL;
83         ENTRY;
84
85         /* mmap_sem must have been held by caller. */
86         LASSERT(!down_write_trylock(&mm->mmap_sem));
87
88         for(vma = find_vma(mm, addr);
89             vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
90                 if (vma->vm_ops && vma->vm_ops == &ll_file_vm_ops &&
91                     vma->vm_flags & VM_SHARED) {
92                         ret = vma;
93                         break;
94                 }
95         }
96         RETURN(ret);
97 }
98
99 /**
100  * API independent part for page fault initialization.
101  * \param vma - virtual memory area addressed to page fault
102  * \param env - corespondent lu_env to processing
103  * \param nest - nested level
104  * \param index - page index corespondent to fault.
105  * \parm ra_flags - vma readahead flags.
106  *
107  * \return allocated and initialized env for fault operation.
108  * \retval EINVAL if env can't allocated
109  * \return other error codes from cl_io_init.
110  */
111 struct cl_io *ll_fault_io_init(struct vm_area_struct *vma,
112                                struct lu_env **env_ret,
113                                struct cl_env_nest *nest,
114                                pgoff_t index, unsigned long *ra_flags)
115 {
116         struct file       *file  = vma->vm_file;
117         struct inode      *inode = file->f_dentry->d_inode;
118         struct cl_io      *io;
119         struct cl_fault_io *fio;
120         struct lu_env     *env;
121         ENTRY;
122
123         *env_ret = NULL;
124         if (ll_file_nolock(file))
125                 RETURN(ERR_PTR(-EOPNOTSUPP));
126
127         /*
128          * page fault can be called when lustre IO is
129          * already active for the current thread, e.g., when doing read/write
130          * against user level buffer mapped from Lustre buffer. To avoid
131          * stomping on existing context, optionally force an allocation of a new
132          * one.
133          */
134         env = cl_env_nested_get(nest);
135         if (IS_ERR(env))
136                  RETURN(ERR_PTR(-EINVAL));
137
138         *env_ret = env;
139
140         io = ccc_env_thread_io(env);
141         io->ci_obj = ll_i2info(inode)->lli_clob;
142         LASSERT(io->ci_obj != NULL);
143
144         fio = &io->u.ci_fault;
145         fio->ft_index      = index;
146         fio->ft_executable = vma->vm_flags&VM_EXEC;
147
148         /*
149          * disable VM_SEQ_READ and use VM_RAND_READ to make sure that
150          * the kernel will not read other pages not covered by ldlm in
151          * filemap_nopage. we do our readahead in ll_readpage.
152          */
153         if (ra_flags != NULL)
154                 *ra_flags = vma->vm_flags & (VM_RAND_READ|VM_SEQ_READ);
155         vma->vm_flags &= ~VM_SEQ_READ;
156         vma->vm_flags |= VM_RAND_READ;
157
158         CDEBUG(D_MMAP, "vm_flags: %lx (%lu %d)\n", vma->vm_flags,
159                fio->ft_index, fio->ft_executable);
160
161         if (cl_io_init(env, io, CIT_FAULT, io->ci_obj) == 0) {
162                 struct ccc_io *cio = ccc_env_io(env);
163                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
164
165                 LASSERT(cio->cui_cl.cis_io == io);
166
167                 /* mmap lock must be MANDATORY
168                  * it has to cache pages. */
169                 io->ci_lockreq = CILR_MANDATORY;
170
171                 cio->cui_fd  = fd;
172         }
173
174         return io;
175 }
176
177 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
178 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
179                             bool *retry)
180 {
181         struct lu_env           *env;
182         struct cl_io            *io;
183         struct vvp_io           *vio;
184         struct cl_env_nest       nest;
185         int                      result;
186         ENTRY;
187
188         LASSERT(vmpage != NULL);
189
190         io = ll_fault_io_init(vma, &env,  &nest, vmpage->index, NULL);
191         if (IS_ERR(io))
192                 GOTO(out, result = PTR_ERR(io));
193
194         result = io->ci_result;
195         if (result < 0)
196                 GOTO(out, result);
197
198         /* Don't enqueue new locks for page_mkwrite().
199          * If the lock has been cancelled then page must have been
200          * truncated, in that case, kernel will handle it.
201          */
202         io->ci_lockreq = CILR_PEEK;
203         io->u.ci_fault.ft_mkwrite = 1;
204         io->u.ci_fault.ft_writable = 1;
205
206         vio = vvp_env_io(env);
207         vio->u.fault.ft_vma    = vma;
208         vio->u.fault.ft_vmpage = vmpage;
209
210         result = cl_io_loop(env, io);
211
212         if (result == -ENODATA) /* peek failed, no lock caching. */
213                 CDEBUG(D_MMAP, "race on page_mkwrite: %lx (%lu %p)\n",
214                        vma->vm_flags, io->u.ci_fault.ft_index, vmpage);
215
216         if (result == 0 || result == -ENODATA) {
217                 lock_page(vmpage);
218                 if (vmpage->mapping == NULL) {
219                         unlock_page(vmpage);
220
221                         /* page was truncated and lock was cancelled, return
222                          * ENODATA so that VM_FAULT_NOPAGE will be returned
223                          * to handle_mm_fault(). */
224                         if (result == 0)
225                                 result = -ENODATA;
226                 } else if (result == -ENODATA) {
227                         /* Invalidate it if the cl_lock is being revoked.
228                          * This piece of code is definitely needed for RHEL5,
229                          * otherwise, SIGBUS will be wrongly returned to
230                          * applications. */
231                         write_one_page(vmpage, 1);
232                         lock_page(vmpage);
233                         if (vmpage->mapping != NULL) {
234                                 ll_invalidate_page(vmpage);
235                                 LASSERT(vmpage->mapping == NULL);
236                         }
237                         unlock_page(vmpage);
238                 } else if (!PageDirty(vmpage)) {
239                         /* race, the page has been cleaned by ptlrpcd after
240                          * it was unlocked, it has to be added into dirty
241                          * cache again otherwise this soon-to-dirty page won't
242                          * consume any grants, even worse if this page is being
243                          * transferred because it will break RPC checksum.
244                          */
245                         unlock_page(vmpage);
246
247                         CDEBUG(D_MMAP, "Race on page_mkwrite %p/%lu, page has "
248                                "been written out, retry.\n",
249                                vmpage, vmpage->index);
250
251                         *retry = true;
252                         result = -EAGAIN;
253                 }
254         }
255         EXIT;
256
257 out:
258         cl_io_fini(env, io);
259         cl_env_nested_put(&nest, env);
260
261         CDEBUG(D_MMAP, "%s mkwrite with %d\n", cfs_current()->comm, result);
262
263         LASSERT(ergo(result == 0, PageLocked(vmpage)));
264         return(result);
265 }
266
267
268 #ifndef HAVE_VM_OP_FAULT
269 /**
270  * Lustre implementation of a vm_operations_struct::nopage() method, called by
271  * VM to server page fault (both in kernel and user space).
272  *
273  * This function sets up CIT_FAULT cl_io that does the job.
274  *
275  * \param vma - is virtiual area struct related to page fault
276  * \param address - address when hit fault
277  * \param type - of fault
278  *
279  * \return allocated and filled _unlocked_ page for address
280  * \retval NOPAGE_SIGBUS if page not exist on this address
281  * \retval NOPAGE_OOM not have memory for allocate new page
282  */
283 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
284                        int *type)
285 {
286         struct lu_env           *env;
287         struct cl_env_nest      nest;
288         struct cl_io            *io;
289         struct page             *page  = NOPAGE_SIGBUS;
290         struct vvp_io           *vio = NULL;
291         unsigned long           ra_flags;
292         pgoff_t                 pg_offset;
293         int                     result;
294         const unsigned long     writable = VM_SHARED|VM_WRITE;
295         ENTRY;
296
297         pg_offset = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
298         io = ll_fault_io_init(vma, &env,  &nest, pg_offset, &ra_flags);
299         if (IS_ERR(io))
300                 return NOPAGE_SIGBUS;
301
302         result = io->ci_result;
303         if (result < 0)
304                 goto out_err;
305
306         io->u.ci_fault.ft_writable = (vma->vm_flags&writable) == writable;
307
308         vio = vvp_env_io(env);
309         vio->u.fault.ft_vma            = vma;
310         vio->u.fault.nopage.ft_address = address;
311         vio->u.fault.nopage.ft_type    = type;
312         vio->u.fault.ft_vmpage         = NULL;
313
314         result = cl_io_loop(env, io);
315         page = vio->u.fault.ft_vmpage;
316         if (result != 0 && page != NULL)
317                 page_cache_release(page);
318
319 out_err:
320         if (result == -ENOMEM)
321                 page = NOPAGE_OOM;
322
323         vma->vm_flags &= ~VM_RAND_READ;
324         vma->vm_flags |= ra_flags;
325
326         cl_io_fini(env, io);
327         cl_env_nested_put(&nest, env);
328
329         RETURN(page);
330 }
331
332 #else
333 /**
334  * Lustre implementation of a vm_operations_struct::fault() method, called by
335  * VM to server page fault (both in kernel and user space).
336  *
337  * \param vma - is virtiual area struct related to page fault
338  * \param vmf - structure which describe type and address where hit fault
339  *
340  * \return allocated and filled _locked_ page for address
341  * \retval VM_FAULT_ERROR on general error
342  * \retval NOPAGE_OOM not have memory for allocate new page
343  */
344 static int ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
345 {
346         struct lu_env           *env;
347         struct cl_io            *io;
348         struct vvp_io           *vio = NULL;
349         struct page             *vmpage;
350         unsigned long            ra_flags;
351         struct cl_env_nest       nest;
352         int                      result;
353         int                      fault_ret = 0;
354         ENTRY;
355
356         io = ll_fault_io_init(vma, &env,  &nest, vmf->pgoff, &ra_flags);
357         if (IS_ERR(io))
358                 RETURN(VM_FAULT_ERROR);
359
360         result = io->ci_result;
361         if (result < 0)
362                 goto out_err;
363
364         vio = vvp_env_io(env);
365         vio->u.fault.ft_vma       = vma;
366         vio->u.fault.ft_vmpage    = NULL;
367         vio->u.fault.fault.ft_vmf = vmf;
368
369         result = cl_io_loop(env, io);
370
371         vmpage = vio->u.fault.ft_vmpage;
372         if (result != 0 && vmpage != NULL) {
373                 page_cache_release(vmpage);
374                 vmf->page = NULL;
375         }
376
377         fault_ret = vio->u.fault.fault.ft_flags;
378
379 out_err:
380         if (result != 0 && fault_ret == 0)
381                 fault_ret = VM_FAULT_ERROR;
382
383         vma->vm_flags |= ra_flags;
384
385         cl_io_fini(env, io);
386         cl_env_nested_put(&nest, env);
387
388         CDEBUG(D_MMAP, "%s fault %d/%d\n",
389                cfs_current()->comm, fault_ret, result);
390         RETURN(fault_ret);
391 }
392
393 static int ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
394 {
395         int count = 0;
396         bool printed = false;
397         int result;
398
399 restart:
400         result = ll_fault0(vma, vmf);
401         LASSERT(!(result & VM_FAULT_LOCKED));
402         if (result == 0) {
403                 struct page *vmpage = vmf->page;
404
405                 /* check if this page has been truncated */
406                 lock_page(vmpage);
407                 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
408                         unlock_page(vmpage);
409                         page_cache_release(vmpage);
410                         vmf->page = NULL;
411
412                         if (!printed && ++count > 16) {
413                                 CWARN("the page is under heavy contention,"
414                                       "maybe your app(%s) needs revising :-)\n",
415                                       current->comm);
416                                 printed = true;
417                         }
418
419                         goto restart;
420                 }
421
422                 result |= VM_FAULT_LOCKED;
423         }
424         return result;
425 }
426 #endif
427
428 #ifndef HAVE_PGMKWRITE_USE_VMFAULT
429 static int ll_page_mkwrite(struct vm_area_struct *vma, struct page *vmpage)
430 {
431         int count = 0;
432         bool printed = false;
433         bool retry;
434         int result;
435
436         do {
437                 retry = false;
438                 result = ll_page_mkwrite0(vma, vmpage, &retry);
439
440                 if (!printed && ++count > 16) {
441                         CWARN("app(%s): the page %lu of file %lu is under heavy"
442                               " contention.\n",
443                               current->comm, page_index(vmpage),
444                               vma->vm_file->f_dentry->d_inode->i_ino);
445                         printed = true;
446                 }
447         } while (retry);
448
449         if (result == 0)
450                 unlock_page(vmpage);
451         else if (result == -ENODATA)
452                 result = 0; /* kernel will know truncate has happened and
453                              * retry */
454
455         return result;
456 }
457 #else
458 static int ll_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
459 {
460         int count = 0;
461         bool printed = false;
462         bool retry;
463         int result;
464
465         do {
466                 retry = false;
467                 result = ll_page_mkwrite0(vma, vmf->page, &retry);
468
469                 if (!printed && ++count > 16) {
470                         CWARN("app(%s): the page %lu of file %lu is under heavy"
471                               " contention.\n",
472                               current->comm, vmf->pgoff,
473                               vma->vm_file->f_dentry->d_inode->i_ino);
474                         printed = true;
475                 }
476         } while (retry);
477
478         switch(result) {
479         case 0:
480                 LASSERT(PageLocked(vmf->page));
481                 result = VM_FAULT_LOCKED;
482                 break;
483         case -ENODATA:
484         case -EFAULT:
485                 result = VM_FAULT_NOPAGE;
486                 break;
487         case -ENOMEM:
488                 result = VM_FAULT_OOM;
489                 break;
490         case -EAGAIN:
491                 result = VM_FAULT_RETRY;
492                 break;
493         default:
494                 result = VM_FAULT_SIGBUS;
495                 break;
496         }
497
498         return result;
499 }
500 #endif
501
502 /**
503  *  To avoid cancel the locks covering mmapped region for lock cache pressure,
504  *  we track the mapped vma count in ccc_object::cob_mmap_cnt.
505  */
506 static void ll_vm_open(struct vm_area_struct * vma)
507 {
508         struct inode *inode    = vma->vm_file->f_dentry->d_inode;
509         struct ccc_object *vob = cl_inode2ccc(inode);
510
511         ENTRY;
512         LASSERT(vma->vm_file);
513         LASSERT(cfs_atomic_read(&vob->cob_mmap_cnt) >= 0);
514         cfs_atomic_inc(&vob->cob_mmap_cnt);
515         EXIT;
516 }
517
518 /**
519  * Dual to ll_vm_open().
520  */
521 static void ll_vm_close(struct vm_area_struct *vma)
522 {
523         struct inode      *inode = vma->vm_file->f_dentry->d_inode;
524         struct ccc_object *vob   = cl_inode2ccc(inode);
525
526         ENTRY;
527         LASSERT(vma->vm_file);
528         cfs_atomic_dec(&vob->cob_mmap_cnt);
529         LASSERT(cfs_atomic_read(&vob->cob_mmap_cnt) >= 0);
530         EXIT;
531 }
532
533 #ifndef HAVE_VM_OP_FAULT
534 #ifndef HAVE_FILEMAP_POPULATE
535 static int (*filemap_populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
536 #endif
537 static int ll_populate(struct vm_area_struct *area, unsigned long address,
538                        unsigned long len, pgprot_t prot, unsigned long pgoff,
539                        int nonblock)
540 {
541         int rc = 0;
542         ENTRY;
543
544         /* always set nonblock as true to avoid page read ahead */
545         rc = filemap_populate(area, address, len, prot, pgoff, 1);
546         RETURN(rc);
547 }
548 #endif
549
550 /* return the user space pointer that maps to a file offset via a vma */
551 static inline unsigned long file_to_user(struct vm_area_struct *vma, __u64 byte)
552 {
553         return vma->vm_start + (byte - ((__u64)vma->vm_pgoff << CFS_PAGE_SHIFT));
554
555 }
556
557 /* XXX put nice comment here.  talk about __free_pte -> dirty pages and
558  * nopage's reference passing to the pte */
559 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last)
560 {
561         int rc = -ENOENT;
562         ENTRY;
563
564         LASSERTF(last > first, "last "LPU64" first "LPU64"\n", last, first);
565         if (mapping_mapped(mapping)) {
566                 rc = 0;
567                 unmap_mapping_range(mapping, first + CFS_PAGE_SIZE - 1,
568                                     last - first + 1, 0);
569         }
570
571         RETURN(rc);
572 }
573
574 static struct vm_operations_struct ll_file_vm_ops = {
575 #ifndef HAVE_VM_OP_FAULT
576         .nopage         = ll_nopage,
577         .populate       = ll_populate,
578
579 #else
580         .fault          = ll_fault,
581 #endif
582         .page_mkwrite   = ll_page_mkwrite,
583         .open           = ll_vm_open,
584         .close          = ll_vm_close,
585 };
586
587 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
588 {
589         struct inode *inode = file->f_dentry->d_inode;
590         int rc;
591         ENTRY;
592
593         if (ll_file_nolock(file))
594                 RETURN(-EOPNOTSUPP);
595
596         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MAP, 1);
597         rc = generic_file_mmap(file, vma);
598         if (rc == 0) {
599 #if !defined(HAVE_FILEMAP_POPULATE) && !defined(HAVE_VM_OP_FAULT)
600                 if (!filemap_populate)
601                         filemap_populate = vma->vm_ops->populate;
602 #endif
603                 vma->vm_ops = &ll_file_vm_ops;
604                 vma->vm_ops->open(vma);
605                 /* update the inode's size and mtime */
606                 rc = ll_glimpse_size(inode);
607         }
608
609         RETURN(rc);
610 }