Whamcloud - gitweb
bdd8f29622b6ccfe11c32978c0d37485825b9234
[fs/lustre-release.git] / lustre / llite / llite_mmap.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #ifndef AUTOCONF_INCLUDED
40 #include <linux/config.h>
41 #endif
42 #include <linux/kernel.h>
43 #include <linux/mm.h>
44 #include <linux/string.h>
45 #include <linux/stat.h>
46 #include <linux/errno.h>
47 #include <linux/smp_lock.h>
48 #include <linux/unistd.h>
49 #include <linux/version.h>
50 #include <asm/system.h>
51 #include <asm/uaccess.h>
52
53 #include <linux/fs.h>
54 #include <linux/stat.h>
55 #include <asm/uaccess.h>
56 #include <linux/mm.h>
57 #include <linux/pagemap.h>
58 #include <linux/smp_lock.h>
59
60 #define DEBUG_SUBSYSTEM S_LLITE
61
62 #include <lustre_lite.h>
63 #include "llite_internal.h"
64 #include <linux/lustre_compat25.h>
65
66 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
67                        int *type);
68
69 static struct vm_operations_struct ll_file_vm_ops;
70
71 void policy_from_vma(ldlm_policy_data_t *policy,
72                             struct vm_area_struct *vma, unsigned long addr,
73                             size_t count)
74 {
75         policy->l_extent.start = ((addr - vma->vm_start) & CFS_PAGE_MASK) +
76                                  (vma->vm_pgoff << CFS_PAGE_SHIFT);
77         policy->l_extent.end = (policy->l_extent.start + count - 1) |
78                                ~CFS_PAGE_MASK;
79 }
80
81 struct vm_area_struct * our_vma(unsigned long addr, size_t count)
82 {
83         struct mm_struct *mm = current->mm;
84         struct vm_area_struct *vma, *ret = NULL;
85         ENTRY;
86
87         /* No MM (e.g. NFS)? No vmas too. */
88         if (!mm)
89                 RETURN(NULL);
90
91         spin_lock(&mm->page_table_lock);
92         for(vma = find_vma(mm, addr);
93             vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
94                 if (vma->vm_ops && vma->vm_ops == &ll_file_vm_ops &&
95                     vma->vm_flags & VM_SHARED) {
96                         ret = vma;
97                         break;
98                 }
99         }
100         spin_unlock(&mm->page_table_lock);
101         RETURN(ret);
102 }
103
104 /**
105  * API independent part for page fault initialization.
106  * \param vma - virtual memory area addressed to page fault
107  * \param env - corespondent lu_env to processing
108  * \param nest - nested level
109  * \param index - page index corespondent to fault.
110  * \parm ra_flags - vma readahead flags.
111  *
112  * \return allocated and initialized env for fault operation.
113  * \retval EINVAL if env can't allocated
114  * \return other error codes from cl_io_init.
115  */
116 struct cl_io *ll_fault_io_init(struct vm_area_struct *vma,
117                                struct lu_env **env_ret,
118                                struct cl_env_nest *nest,
119                                pgoff_t index, unsigned long *ra_flags)
120 {
121         struct file       *file  = vma->vm_file;
122         struct inode      *inode = file->f_dentry->d_inode;
123         struct cl_io      *io;
124         struct cl_fault_io *fio;
125         struct lu_env     *env;
126         ENTRY;
127
128         *env_ret = NULL;
129         if (ll_file_nolock(file))
130                 RETURN(ERR_PTR(-EOPNOTSUPP));
131
132         /*
133          * page fault can be called when lustre IO is
134          * already active for the current thread, e.g., when doing read/write
135          * against user level buffer mapped from Lustre buffer. To avoid
136          * stomping on existing context, optionally force an allocation of a new
137          * one.
138          */
139         env = cl_env_nested_get(nest);
140         if (IS_ERR(env))
141                  RETURN(ERR_PTR(-EINVAL));
142
143         *env_ret = env;
144
145         io = ccc_env_thread_io(env);
146         io->ci_obj = ll_i2info(inode)->lli_clob;
147         LASSERT(io->ci_obj != NULL);
148
149         fio = &io->u.ci_fault;
150         fio->ft_index      = index;
151         fio->ft_executable = vma->vm_flags&VM_EXEC;
152
153         /*
154          * disable VM_SEQ_READ and use VM_RAND_READ to make sure that
155          * the kernel will not read other pages not covered by ldlm in
156          * filemap_nopage. we do our readahead in ll_readpage.
157          */
158         if (ra_flags != NULL)
159                 *ra_flags = vma->vm_flags & (VM_RAND_READ|VM_SEQ_READ);
160         vma->vm_flags &= ~VM_SEQ_READ;
161         vma->vm_flags |= VM_RAND_READ;
162
163         CDEBUG(D_MMAP, "vm_flags: %lx (%lu %d)\n", vma->vm_flags,
164                fio->ft_index, fio->ft_executable);
165
166         if (cl_io_init(env, io, CIT_FAULT, io->ci_obj) == 0) {
167                 struct ccc_io *cio = ccc_env_io(env);
168                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
169
170                 LASSERT(cio->cui_cl.cis_io == io);
171
172                 /* mmap lock must be MANDATORY
173                  * it has to cache pages. */
174                 io->ci_lockreq = CILR_MANDATORY;
175
176                 cio->cui_fd  = fd;
177         }
178
179         return io;
180 }
181
182 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
183 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
184                             bool *retry)
185 {
186         struct lu_env           *env;
187         struct cl_io            *io;
188         struct vvp_io           *vio;
189         struct cl_env_nest       nest;
190         int                      result;
191         ENTRY;
192
193         LASSERT(vmpage != NULL);
194
195         io = ll_fault_io_init(vma, &env,  &nest, vmpage->index, NULL);
196         if (IS_ERR(io))
197                 GOTO(out, result = PTR_ERR(io));
198
199         result = io->ci_result;
200         if (result < 0)
201                 GOTO(out, result);
202
203         /* Don't enqueue new locks for page_mkwrite().
204          * If the lock has been cancelled then page must have been
205          * truncated, in that case, kernel will handle it.
206          */
207         io->ci_lockreq = CILR_PEEK;
208         io->u.ci_fault.ft_mkwrite = 1;
209         io->u.ci_fault.ft_writable = 1;
210
211         vio = vvp_env_io(env);
212         vio->u.fault.ft_vma    = vma;
213         vio->u.fault.ft_vmpage = vmpage;
214
215         result = cl_io_loop(env, io);
216
217         if (result == -ENODATA) /* peek failed, no lock caching. */
218                 CDEBUG(D_MMAP, "race on page_mkwrite: %lx (%lu %p)\n",
219                        vma->vm_flags, io->u.ci_fault.ft_index, vmpage);
220
221         if (result == 0 || result == -ENODATA) {
222                 lock_page(vmpage);
223                 if (vmpage->mapping == NULL) {
224                         unlock_page(vmpage);
225
226                         /* page was truncated and lock was cancelled, return
227                          * ENODATA so that VM_FAULT_NOPAGE will be returned
228                          * to handle_mm_fault(). */
229                         if (result == 0)
230                                 result = -ENODATA;
231                 } else if (result == -ENODATA) {
232                         /* Invalidate it if the cl_lock is being revoked.
233                          * This piece of code is definitely needed for RHEL5,
234                          * otherwise, SIGBUS will be wrongly returned to
235                          * applications. */
236                         ll_invalidate_page(vmpage);
237                         LASSERT(vmpage->mapping == NULL);
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         ENTRY;
297
298         pg_offset = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
299         io = ll_fault_io_init(vma, &env,  &nest, pg_offset, &ra_flags);
300         if (IS_ERR(io))
301                 return NOPAGE_SIGBUS;
302
303         result = io->ci_result;
304         if (result < 0)
305                 goto out_err;
306
307         io->u.ci_fault.ft_writable = (vma->vm_flags&writable) == writable;
308
309         vio = vvp_env_io(env);
310         vio->u.fault.ft_vma            = vma;
311         vio->u.fault.nopage.ft_address = address;
312         vio->u.fault.nopage.ft_type    = type;
313         vio->u.fault.ft_vmpage         = NULL;
314
315         result = cl_io_loop(env, io);
316         page = vio->u.fault.ft_vmpage;
317         if (result != 0 && page != NULL)
318                 page_cache_release(page);
319
320 out_err:
321         if (result == -ENOMEM)
322                 page = NOPAGE_OOM;
323
324         vma->vm_flags &= ~VM_RAND_READ;
325         vma->vm_flags |= ra_flags;
326
327         cl_io_fini(env, io);
328         cl_env_nested_put(&nest, env);
329
330         RETURN(page);
331 }
332
333 static int ll_page_mkwrite(struct vm_area_struct *vma, struct page *vmpage)
334 {
335         int count = 0;
336         bool printed = false;
337         bool retry;
338         int result;
339
340         do {
341                 retry = false;
342                 result = ll_page_mkwrite0(vma, vmpage, &retry);
343
344                 if (!printed && ++count > 16) {
345                         CWARN("app(%s): the page %lu of file %lu is under heavy"
346                               " contention.\n",
347                               current->comm, page_index(vmpage),
348                               vma->vm_file->f_dentry->d_inode->i_ino);
349                         printed = true;
350                 }
351         } while (retry);
352
353         if (result == 0)
354                 unlock_page(vmpage);
355         else if (result == -ENODATA)
356                 result = 0; /* kernel will know truncate has happened and
357                              * retry */
358
359         return result;
360 }
361
362 #else
363 /**
364  * Lustre implementation of a vm_operations_struct::fault() method, called by
365  * VM to server page fault (both in kernel and user space).
366  *
367  * \param vma - is virtiual area struct related to page fault
368  * \param vmf - structure which describe type and address where hit fault
369  *
370  * \return allocated and filled _locked_ page for address
371  * \retval VM_FAULT_ERROR on general error
372  * \retval NOPAGE_OOM not have memory for allocate new page
373  */
374 static int ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
375 {
376         struct lu_env           *env;
377         struct cl_io            *io;
378         struct vvp_io           *vio = NULL;
379         struct page             *vmpage;
380         unsigned long            ra_flags;
381         struct cl_env_nest       nest;
382         int                      result;
383         int                      fault_ret = 0;
384         ENTRY;
385
386         io = ll_fault_io_init(vma, &env,  &nest, vmf->pgoff, &ra_flags);
387         if (IS_ERR(io))
388                 RETURN(VM_FAULT_ERROR);
389
390         result = io->ci_result;
391         if (result < 0)
392                 goto out_err;
393
394         vio = vvp_env_io(env);
395         vio->u.fault.ft_vma       = vma;
396         vio->u.fault.ft_vmpage    = NULL;
397         vio->u.fault.fault.ft_vmf = vmf;
398
399         result = cl_io_loop(env, io);
400
401         vmpage = vio->u.fault.ft_vmpage;
402         if (result != 0 && vmpage != NULL) {
403                 page_cache_release(vmpage);
404                 vmf->page = NULL;
405         }
406
407         fault_ret = vio->u.fault.fault.ft_flags;
408
409 out_err:
410         if (result != 0 && fault_ret == 0)
411                 fault_ret = VM_FAULT_ERROR;
412
413         vma->vm_flags |= ra_flags;
414
415         cl_io_fini(env, io);
416         cl_env_nested_put(&nest, env);
417
418         CDEBUG(D_MMAP, "%s fault %d/%d\n",
419                cfs_current()->comm, fault_ret, result);
420         RETURN(fault_ret);
421 }
422
423 static int ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
424 {
425         int count = 0;
426         bool printed = false;
427         int result;
428
429 restart:
430         result = ll_fault0(vma, vmf);
431         LASSERT(!(result & VM_FAULT_LOCKED));
432         if (result == 0) {
433                 struct page *vmpage = vmf->page;
434
435                 /* check if this page has been truncated */
436                 lock_page(vmpage);
437                 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
438                         unlock_page(vmpage);
439                         page_cache_release(vmpage);
440                         vmf->page = NULL;
441
442                         if (!printed && ++count > 16) {
443                                 CWARN("the page is under heavy contention,"
444                                       "maybe your app(%s) needs revising :-)\n",
445                                       current->comm);
446                                 printed = true;
447                         }
448
449                         goto restart;
450                 }
451
452                 result |= VM_FAULT_LOCKED;
453         }
454         return result;
455 }
456
457 static int ll_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
458 {
459         int count = 0;
460         bool printed = false;
461         bool retry;
462         int result;
463
464         do {
465                 retry = false;
466                 result = ll_page_mkwrite0(vma, vmf->page, &retry);
467
468                 if (!printed && ++count > 16) {
469                         CWARN("app(%s): the page %lu of file %lu is under heavy"
470                               " contention.\n",
471                               current->comm, vmf->pgoff,
472                               vma->vm_file->f_dentry->d_inode->i_ino);
473                         printed = true;
474                 }
475         } while (retry);
476
477         switch(result) {
478         case 0:
479                 LASSERT(PageLocked(vmf->page));
480                 result = VM_FAULT_LOCKED;
481                 break;
482         case -ENODATA:
483         case -EFAULT:
484                 result = VM_FAULT_NOPAGE;
485                 break;
486         case -ENOMEM:
487                 result = VM_FAULT_OOM;
488                 break;
489         case -EAGAIN:
490                 result = VM_FAULT_RETRY;
491                 break;
492         default:
493                 result = VM_FAULT_SIGBUS;
494                 break;
495         }
496
497         return result;
498 }
499 #endif
500
501 /**
502  *  To avoid cancel the locks covering mmapped region for lock cache pressure,
503  *  we track the mapped vma count in ccc_object::cob_mmap_cnt.
504  */
505 static void ll_vm_open(struct vm_area_struct * vma)
506 {
507         struct inode *inode    = vma->vm_file->f_dentry->d_inode;
508         struct ccc_object *vob = cl_inode2ccc(inode);
509
510         ENTRY;
511         LASSERT(vma->vm_file);
512         LASSERT(cfs_atomic_read(&vob->cob_mmap_cnt) >= 0);
513         cfs_atomic_inc(&vob->cob_mmap_cnt);
514         EXIT;
515 }
516
517 /**
518  * Dual to ll_vm_open().
519  */
520 static void ll_vm_close(struct vm_area_struct *vma)
521 {
522         struct inode      *inode = vma->vm_file->f_dentry->d_inode;
523         struct ccc_object *vob   = cl_inode2ccc(inode);
524
525         ENTRY;
526         LASSERT(vma->vm_file);
527         cfs_atomic_dec(&vob->cob_mmap_cnt);
528         LASSERT(cfs_atomic_read(&vob->cob_mmap_cnt) >= 0);
529         EXIT;
530 }
531
532 #ifndef HAVE_VM_OP_FAULT
533 #ifndef HAVE_FILEMAP_POPULATE
534 static int (*filemap_populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
535 #endif
536 static int ll_populate(struct vm_area_struct *area, unsigned long address,
537                        unsigned long len, pgprot_t prot, unsigned long pgoff,
538                        int nonblock)
539 {
540         int rc = 0;
541         ENTRY;
542
543         /* always set nonblock as true to avoid page read ahead */
544         rc = filemap_populate(area, address, len, prot, pgoff, 1);
545         RETURN(rc);
546 }
547 #endif
548
549 /* return the user space pointer that maps to a file offset via a vma */
550 static inline unsigned long file_to_user(struct vm_area_struct *vma, __u64 byte)
551 {
552         return vma->vm_start + (byte - ((__u64)vma->vm_pgoff << CFS_PAGE_SHIFT));
553
554 }
555
556 /* XXX put nice comment here.  talk about __free_pte -> dirty pages and
557  * nopage's reference passing to the pte */
558 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last)
559 {
560         int rc = -ENOENT;
561         ENTRY;
562
563         LASSERTF(last > first, "last "LPU64" first "LPU64"\n", last, first);
564         if (mapping_mapped(mapping)) {
565                 rc = 0;
566                 unmap_mapping_range(mapping, first + CFS_PAGE_SIZE - 1,
567                                     last - first + 1, 0);
568         }
569
570         RETURN(rc);
571 }
572
573 static struct vm_operations_struct ll_file_vm_ops = {
574 #ifndef HAVE_VM_OP_FAULT
575         .nopage         = ll_nopage,
576         .populate       = ll_populate,
577
578 #else
579         .fault          = ll_fault,
580 #endif
581         .page_mkwrite   = ll_page_mkwrite,
582         .open           = ll_vm_open,
583         .close          = ll_vm_close,
584 };
585
586 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
587 {
588         struct inode *inode = file->f_dentry->d_inode;
589         int rc;
590         ENTRY;
591
592         if (ll_file_nolock(file))
593                 RETURN(-EOPNOTSUPP);
594
595         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MAP, 1);
596         rc = generic_file_mmap(file, vma);
597         if (rc == 0) {
598 #if !defined(HAVE_FILEMAP_POPULATE) && !defined(HAVE_VM_OP_FAULT)
599                 if (!filemap_populate)
600                         filemap_populate = vma->vm_ops->populate;
601 #endif
602                 vma->vm_ops = &ll_file_vm_ops;
603                 vma->vm_ops->open(vma);
604                 /* update the inode's size and mtime */
605                 rc = ll_glimpse_size(inode);
606         }
607
608         RETURN(rc);
609 }