Whamcloud - gitweb
LU-812 kernel: remove smp_lock.h
[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/unistd.h>
46 #include <linux/version.h>
47 #include <asm/system.h>
48 #include <asm/uaccess.h>
49
50 #include <linux/fs.h>
51 #include <linux/stat.h>
52 #include <asm/uaccess.h>
53 #include <linux/mm.h>
54 #include <linux/pagemap.h>
55
56 #define DEBUG_SUBSYSTEM S_LLITE
57
58 #include <lustre_lite.h>
59 #include "llite_internal.h"
60 #include <linux/lustre_compat25.h>
61
62 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
63                        int *type);
64
65 static struct vm_operations_struct ll_file_vm_ops;
66
67 void policy_from_vma(ldlm_policy_data_t *policy,
68                             struct vm_area_struct *vma, unsigned long addr,
69                             size_t count)
70 {
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) |
74                                ~CFS_PAGE_MASK;
75 }
76
77 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
78                                size_t count)
79 {
80         struct vm_area_struct *vma, *ret = NULL;
81         ENTRY;
82
83         /* mmap_sem must have been held by caller. */
84         LASSERT(!down_write_trylock(&mm->mmap_sem));
85
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) {
90                         ret = vma;
91                         break;
92                 }
93         }
94         RETURN(ret);
95 }
96
97 /**
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.
104  *
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.
108  */
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)
113 {
114         struct file       *file  = vma->vm_file;
115         struct inode      *inode = file->f_dentry->d_inode;
116         struct cl_io      *io;
117         struct cl_fault_io *fio;
118         struct lu_env     *env;
119         ENTRY;
120
121         *env_ret = NULL;
122         if (ll_file_nolock(file))
123                 RETURN(ERR_PTR(-EOPNOTSUPP));
124
125         /*
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
130          * one.
131          */
132         env = cl_env_nested_get(nest);
133         if (IS_ERR(env))
134                  RETURN(ERR_PTR(-EINVAL));
135
136         *env_ret = env;
137
138         io = ccc_env_thread_io(env);
139         io->ci_obj = ll_i2info(inode)->lli_clob;
140         LASSERT(io->ci_obj != NULL);
141
142         fio = &io->u.ci_fault;
143         fio->ft_index      = index;
144         fio->ft_executable = vma->vm_flags&VM_EXEC;
145
146         /*
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.
150          */
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;
155
156         CDEBUG(D_MMAP, "vm_flags: %lx (%lu %d)\n", vma->vm_flags,
157                fio->ft_index, fio->ft_executable);
158
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);
162
163                 LASSERT(cio->cui_cl.cis_io == io);
164
165                 /* mmap lock must be MANDATORY
166                  * it has to cache pages. */
167                 io->ci_lockreq = CILR_MANDATORY;
168
169                 cio->cui_fd  = fd;
170         }
171
172         return io;
173 }
174
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,
177                             bool *retry)
178 {
179         struct lu_env           *env;
180         struct cl_io            *io;
181         struct vvp_io           *vio;
182         struct cl_env_nest       nest;
183         int                      result;
184         cfs_sigset_t             set;
185         ENTRY;
186
187         LASSERT(vmpage != NULL);
188
189         io = ll_fault_io_init(vma, &env,  &nest, vmpage->index, NULL);
190         if (IS_ERR(io))
191                 GOTO(out, result = PTR_ERR(io));
192
193         result = io->ci_result;
194         if (result < 0)
195                 GOTO(out, result);
196
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.
200          */
201         io->ci_lockreq = CILR_PEEK;
202         io->u.ci_fault.ft_mkwrite = 1;
203         io->u.ci_fault.ft_writable = 1;
204
205         vio = vvp_env_io(env);
206         vio->u.fault.ft_vma    = vma;
207         vio->u.fault.ft_vmpage = vmpage;
208
209         set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
210         result = cl_io_loop(env, io);
211         cfs_restore_sigs(set);
212
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);
216
217         if (result == 0 || result == -ENODATA) {
218                 lock_page(vmpage);
219                 if (vmpage->mapping == NULL) {
220                         unlock_page(vmpage);
221
222                         /* page was truncated and lock was cancelled, return
223                          * ENODATA so that VM_FAULT_NOPAGE will be returned
224                          * to handle_mm_fault(). */
225                         if (result == 0)
226                                 result = -ENODATA;
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
231                          * applications. */
232                         write_one_page(vmpage, 1);
233                         lock_page(vmpage);
234                         if (vmpage->mapping != NULL) {
235                                 ll_invalidate_page(vmpage);
236                                 LASSERT(vmpage->mapping == NULL);
237                         }
238                         unlock_page(vmpage);
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.
245                          */
246                         unlock_page(vmpage);
247
248                         CDEBUG(D_MMAP, "Race on page_mkwrite %p/%lu, page has "
249                                "been written out, retry.\n",
250                                vmpage, vmpage->index);
251
252                         *retry = true;
253                         result = -EAGAIN;
254                 }
255         }
256         EXIT;
257
258 out:
259         cl_io_fini(env, io);
260         cl_env_nested_put(&nest, env);
261
262         CDEBUG(D_MMAP, "%s mkwrite with %d\n", cfs_current()->comm, result);
263
264         LASSERT(ergo(result == 0, PageLocked(vmpage)));
265         return(result);
266 }
267
268
269 #ifndef HAVE_VM_OP_FAULT
270 /**
271  * Lustre implementation of a vm_operations_struct::nopage() method, called by
272  * VM to server page fault (both in kernel and user space).
273  *
274  * This function sets up CIT_FAULT cl_io that does the job.
275  *
276  * \param vma - is virtiual area struct related to page fault
277  * \param address - address when hit fault
278  * \param type - of fault
279  *
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
283  */
284 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
285                        int *type)
286 {
287         struct lu_env           *env;
288         struct cl_env_nest      nest;
289         struct cl_io            *io;
290         struct page             *page  = NOPAGE_SIGBUS;
291         struct vvp_io           *vio = NULL;
292         unsigned long           ra_flags;
293         pgoff_t                 pg_offset;
294         int                     result;
295         const unsigned long     writable = VM_SHARED|VM_WRITE;
296         cfs_sigset_t            set;
297         ENTRY;
298
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);
301         if (IS_ERR(io))
302                 return NOPAGE_SIGBUS;
303
304         result = io->ci_result;
305         if (result < 0)
306                 goto out_err;
307
308         io->u.ci_fault.ft_writable = (vma->vm_flags&writable) == writable;
309
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;
315
316         set = cfs_block_sigsinv(sigmask(SIGKILL)|sigmask(SIGTERM));
317         result = cl_io_loop(env, io);
318         cfs_restore_sigs(set);
319
320         page = vio->u.fault.ft_vmpage;
321         if (result != 0 && page != NULL) {
322                 page_cache_release(page);
323                 page = NOPAGE_SIGBUS;
324         }
325
326 out_err:
327         if (result == -ENOMEM)
328                 page = NOPAGE_OOM;
329
330         vma->vm_flags &= ~VM_RAND_READ;
331         vma->vm_flags |= ra_flags;
332
333         cl_io_fini(env, io);
334         cl_env_nested_put(&nest, env);
335
336         RETURN(page);
337 }
338
339 #else
340
341 static inline int to_fault_error(int result)
342 {
343         switch(result) {
344         case 0:
345                 result = VM_FAULT_LOCKED;
346                 break;
347         case -EFAULT:
348                 result = VM_FAULT_NOPAGE;
349                 break;
350         case -ENOMEM:
351                 result = VM_FAULT_OOM;
352                 break;
353         default:
354                 result = VM_FAULT_SIGBUS;
355                 break;
356         }
357         return result;
358 }
359
360 /**
361  * Lustre implementation of a vm_operations_struct::fault() method, called by
362  * VM to server page fault (both in kernel and user space).
363  *
364  * \param vma - is virtiual area struct related to page fault
365  * \param vmf - structure which describe type and address where hit fault
366  *
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
370  */
371 static int ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
372 {
373         struct lu_env           *env;
374         struct cl_io            *io;
375         struct vvp_io           *vio = NULL;
376         struct page             *vmpage;
377         unsigned long            ra_flags;
378         struct cl_env_nest       nest;
379         int                      result;
380         int                      fault_ret = 0;
381         ENTRY;
382
383         io = ll_fault_io_init(vma, &env,  &nest, vmf->pgoff, &ra_flags);
384         if (IS_ERR(io))
385                 RETURN(to_fault_error(PTR_ERR(io)));
386
387         result = io->ci_result;
388         if (result == 0) {
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;
393
394                 result = cl_io_loop(env, io);
395
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);
400                         vmf->page = NULL;
401                 }
402         }
403         cl_io_fini(env, io);
404         cl_env_nested_put(&nest, env);
405
406         vma->vm_flags |= ra_flags;
407         if (result != 0 && !(fault_ret & VM_FAULT_RETRY))
408                 fault_ret |= to_fault_error(result);
409
410         CDEBUG(D_MMAP, "%s fault %d/%d\n",
411                cfs_current()->comm, fault_ret, result);
412         RETURN(fault_ret);
413 }
414
415 static int ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
416 {
417         int count = 0;
418         bool printed = false;
419         int result;
420         cfs_sigset_t set;
421
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
424          * other signals. */
425         set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
426
427 restart:
428         result = ll_fault0(vma, vmf);
429         LASSERT(!(result & VM_FAULT_LOCKED));
430         if (result == 0) {
431                 struct page *vmpage = vmf->page;
432
433                 /* check if this page has been truncated */
434                 lock_page(vmpage);
435                 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
436                         unlock_page(vmpage);
437                         page_cache_release(vmpage);
438                         vmf->page = NULL;
439
440                         if (!printed && ++count > 16) {
441                                 CWARN("the page is under heavy contention,"
442                                       "maybe your app(%s) needs revising :-)\n",
443                                       current->comm);
444                                 printed = true;
445                         }
446
447                         goto restart;
448                 }
449
450                 result |= VM_FAULT_LOCKED;
451         }
452         cfs_restore_sigs(set);
453         return result;
454 }
455 #endif
456
457 #ifndef HAVE_PGMKWRITE_USE_VMFAULT
458 static int ll_page_mkwrite(struct vm_area_struct *vma, struct page *vmpage)
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, vmpage, &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, page_index(vmpage),
473                               vma->vm_file->f_dentry->d_inode->i_ino);
474                         printed = true;
475                 }
476         } while (retry);
477
478         if (result == 0)
479                 unlock_page(vmpage);
480         else if (result == -ENODATA)
481                 result = 0; /* kernel will know truncate has happened and
482                              * retry */
483
484         return result;
485 }
486 #else
487 static int ll_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
488 {
489         int count = 0;
490         bool printed = false;
491         bool retry;
492         int result;
493
494         do {
495                 retry = false;
496                 result = ll_page_mkwrite0(vma, vmf->page, &retry);
497
498                 if (!printed && ++count > 16) {
499                         CWARN("app(%s): the page %lu of file %lu is under heavy"
500                               " contention.\n",
501                               current->comm, vmf->pgoff,
502                               vma->vm_file->f_dentry->d_inode->i_ino);
503                         printed = true;
504                 }
505         } while (retry);
506
507         switch(result) {
508         case 0:
509                 LASSERT(PageLocked(vmf->page));
510                 result = VM_FAULT_LOCKED;
511                 break;
512         case -ENODATA:
513         case -EFAULT:
514                 result = VM_FAULT_NOPAGE;
515                 break;
516         case -ENOMEM:
517                 result = VM_FAULT_OOM;
518                 break;
519         case -EAGAIN:
520                 result = VM_FAULT_RETRY;
521                 break;
522         default:
523                 result = VM_FAULT_SIGBUS;
524                 break;
525         }
526
527         return result;
528 }
529 #endif
530
531 /**
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.
534  */
535 static void ll_vm_open(struct vm_area_struct * vma)
536 {
537         struct inode *inode    = vma->vm_file->f_dentry->d_inode;
538         struct ccc_object *vob = cl_inode2ccc(inode);
539
540         ENTRY;
541         LASSERT(vma->vm_file);
542         LASSERT(cfs_atomic_read(&vob->cob_mmap_cnt) >= 0);
543         cfs_atomic_inc(&vob->cob_mmap_cnt);
544         EXIT;
545 }
546
547 /**
548  * Dual to ll_vm_open().
549  */
550 static void ll_vm_close(struct vm_area_struct *vma)
551 {
552         struct inode      *inode = vma->vm_file->f_dentry->d_inode;
553         struct ccc_object *vob   = cl_inode2ccc(inode);
554
555         ENTRY;
556         LASSERT(vma->vm_file);
557         cfs_atomic_dec(&vob->cob_mmap_cnt);
558         LASSERT(cfs_atomic_read(&vob->cob_mmap_cnt) >= 0);
559         EXIT;
560 }
561
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);
565 #endif
566 static int ll_populate(struct vm_area_struct *area, unsigned long address,
567                        unsigned long len, pgprot_t prot, unsigned long pgoff,
568                        int nonblock)
569 {
570         int rc = 0;
571         ENTRY;
572
573         /* always set nonblock as true to avoid page read ahead */
574         rc = filemap_populate(area, address, len, prot, pgoff, 1);
575         RETURN(rc);
576 }
577 #endif
578
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)
581 {
582         return vma->vm_start + (byte - ((__u64)vma->vm_pgoff << CFS_PAGE_SHIFT));
583
584 }
585
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)
589 {
590         int rc = -ENOENT;
591         ENTRY;
592
593         LASSERTF(last > first, "last "LPU64" first "LPU64"\n", last, first);
594         if (mapping_mapped(mapping)) {
595                 rc = 0;
596                 unmap_mapping_range(mapping, first + CFS_PAGE_SIZE - 1,
597                                     last - first + 1, 0);
598         }
599
600         RETURN(rc);
601 }
602
603 static struct vm_operations_struct ll_file_vm_ops = {
604 #ifndef HAVE_VM_OP_FAULT
605         .nopage         = ll_nopage,
606         .populate       = ll_populate,
607
608 #else
609         .fault          = ll_fault,
610 #endif
611         .page_mkwrite   = ll_page_mkwrite,
612         .open           = ll_vm_open,
613         .close          = ll_vm_close,
614 };
615
616 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
617 {
618         struct inode *inode = file->f_dentry->d_inode;
619         int rc;
620         ENTRY;
621
622         if (ll_file_nolock(file))
623                 RETURN(-EOPNOTSUPP);
624
625         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MAP, 1);
626         rc = generic_file_mmap(file, vma);
627         if (rc == 0) {
628 #if !defined(HAVE_FILEMAP_POPULATE) && !defined(HAVE_VM_OP_FAULT)
629                 if (!filemap_populate)
630                         filemap_populate = vma->vm_ops->populate;
631 #endif
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);
636         }
637
638         RETURN(rc);
639 }