Whamcloud - gitweb
7d2651b223cef02a1e36c02544884427cac71d5e
[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  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef AUTOCONF_INCLUDED
23 #include <linux/config.h>
24 #endif
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <linux/unistd.h>
32 #include <linux/version.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35
36 #include <linux/fs.h>
37 #include <linux/stat.h>
38 #include <asm/uaccess.h>
39 #include <asm/segment.h>
40 #include <linux/mm.h>
41 #include <linux/pagemap.h>
42 #include <linux/smp_lock.h>
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45
46 //#include <lustre_mdc.h>
47 #include <lustre_lite.h>
48 #include "llite_internal.h"
49 #include <linux/lustre_compat25.h>
50
51 #define VMA_DEBUG(vma, fmt, arg...)                                     \
52         CDEBUG(D_MMAP, "vma(%p) start(%ld) end(%ld) pgoff(%ld) inode(%p) "   \
53                "ino(%lu) iname(%s): " fmt, vma, vma->vm_start, vma->vm_end,  \
54                vma->vm_pgoff, vma->vm_file->f_dentry->d_inode,               \
55                vma->vm_file->f_dentry->d_inode->i_ino,                       \
56                vma->vm_file->f_dentry->d_iname, ## arg);                     \
57
58
59 struct ll_lock_tree_node {
60         rb_node_t               lt_node;
61         struct list_head        lt_locked_item;
62         __u64                   lt_oid;
63         ldlm_policy_data_t      lt_policy;
64         struct lustre_handle    lt_lockh;
65         ldlm_mode_t             lt_mode;
66         struct inode           *lt_inode;
67 };
68
69 int lt_get_mmap_locks(struct ll_lock_tree *tree,
70                       unsigned long addr, size_t count);
71
72 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
73                        int *type);
74
75 struct ll_lock_tree_node * ll_node_from_inode(struct inode *inode, __u64 start,
76                                               __u64 end, ldlm_mode_t mode)
77 {
78         struct ll_lock_tree_node *node;
79
80         OBD_ALLOC(node, sizeof(*node));
81         if (node == NULL)
82                 RETURN(ERR_PTR(-ENOMEM));
83
84         node->lt_inode = inode;
85         node->lt_oid = ll_i2info(inode)->lli_smd->lsm_object_id;
86         node->lt_policy.l_extent.start = start;
87         node->lt_policy.l_extent.end = end;
88         memset(&node->lt_lockh, 0, sizeof(node->lt_lockh));
89         INIT_LIST_HEAD(&node->lt_locked_item);
90         node->lt_mode = mode;
91
92         return node;
93 }
94
95 int lt_compare(struct ll_lock_tree_node *one, struct ll_lock_tree_node *two)
96 {
97         /* To avoid multiple fs deadlock */
98         if (one->lt_inode->i_sb->s_dev < two->lt_inode->i_sb->s_dev)
99                 return -1;
100         if (one->lt_inode->i_sb->s_dev > two->lt_inode->i_sb->s_dev)
101                 return 1;
102
103         if (one->lt_oid < two->lt_oid)
104                 return -1;
105         if (one->lt_oid > two->lt_oid)
106                 return 1;
107
108         if (one->lt_policy.l_extent.end < two->lt_policy.l_extent.start)
109                 return -1;
110         if (one->lt_policy.l_extent.start > two->lt_policy.l_extent.end)
111                 return 1;
112
113         return 0; /* they are the same object and overlap */
114 }
115
116 static void lt_merge(struct ll_lock_tree_node *dst,
117                      struct ll_lock_tree_node *src)
118 {
119         dst->lt_policy.l_extent.start = min(dst->lt_policy.l_extent.start,
120                                             src->lt_policy.l_extent.start);
121         dst->lt_policy.l_extent.end = max(dst->lt_policy.l_extent.end,
122                                           src->lt_policy.l_extent.end);
123
124         /* XXX could be a real call to the dlm to find superset modes */
125         if (src->lt_mode == LCK_PW && dst->lt_mode != LCK_PW)
126                 dst->lt_mode = LCK_PW;
127 }
128
129 static void lt_insert(struct ll_lock_tree *tree,
130                       struct ll_lock_tree_node *node)
131 {
132         struct ll_lock_tree_node *walk;
133         rb_node_t **p, *parent;
134         ENTRY;
135
136 restart:
137         p = &tree->lt_root.rb_node;
138         parent = NULL;
139         while (*p) {
140                 parent = *p;
141                 walk = rb_entry(parent, struct ll_lock_tree_node, lt_node);
142                 switch (lt_compare(node, walk)) {
143                 case -1:
144                         p = &(*p)->rb_left;
145                         break;
146                 case 1:
147                         p = &(*p)->rb_right;
148                         break;
149                 case 0:
150                         lt_merge(node, walk);
151                         rb_erase(&walk->lt_node, &tree->lt_root);
152                         OBD_FREE(walk, sizeof(*walk));
153                         goto restart;
154                         break;
155                 default:
156                         LBUG();
157                         break;
158                 }
159         }
160         rb_link_node(&node->lt_node, parent, p);
161         rb_insert_color(&node->lt_node, &tree->lt_root);
162         EXIT;
163 }
164
165 static struct ll_lock_tree_node *lt_least_node(struct ll_lock_tree *tree)
166 {
167         rb_node_t *rbnode;
168         struct ll_lock_tree_node *node = NULL;
169
170         for ( rbnode = tree->lt_root.rb_node; rbnode != NULL;
171               rbnode = rbnode->rb_left) {
172                 if (rbnode->rb_left == NULL) {
173                         node = rb_entry(rbnode, struct ll_lock_tree_node,
174                                         lt_node);
175                         break;
176                 }
177         }
178         RETURN(node);
179 }
180
181 int ll_tree_unlock(struct ll_lock_tree *tree)
182 {
183         struct ll_lock_tree_node *node;
184         struct list_head *pos, *n;
185         struct inode *inode;
186         int rc = 0;
187         ENTRY;
188
189         list_for_each_safe(pos, n, &tree->lt_locked_list) {
190                 node = list_entry(pos, struct ll_lock_tree_node,
191                                   lt_locked_item);
192
193                 inode = node->lt_inode;
194                 rc = ll_extent_unlock(tree->lt_fd, inode,
195                                       ll_i2info(inode)->lli_smd, node->lt_mode,
196                                       &node->lt_lockh);
197                 if (rc != 0) {
198                         /* XXX better message */
199                         CERROR("couldn't unlock %d\n", rc);
200                 }
201                 list_del(&node->lt_locked_item);
202                 OBD_FREE(node, sizeof(*node));
203         }
204
205         while ((node = lt_least_node(tree))) {
206                 rb_erase(&node->lt_node, &tree->lt_root);
207                 OBD_FREE(node, sizeof(*node));
208         }
209
210         RETURN(rc);
211 }
212
213 int ll_tree_lock(struct ll_lock_tree *tree,
214                  struct ll_lock_tree_node *first_node,
215                  const char *buf, size_t count, int ast_flags)
216 {
217         struct ll_lock_tree_node *node;
218         int rc = 0;
219         ENTRY;
220
221         tree->lt_root.rb_node = NULL;
222         INIT_LIST_HEAD(&tree->lt_locked_list);
223         if (first_node != NULL)
224                 lt_insert(tree, first_node);
225
226         /* To avoid such subtle deadlock case: client1 try to read file1 to
227          * mmapped file2, on the same time, client2 try to read file2 to
228          * mmapped file1.*/
229         rc = lt_get_mmap_locks(tree, (unsigned long)buf, count);
230         if (rc)
231                 GOTO(out, rc);
232
233         while ((node = lt_least_node(tree))) {
234                 struct inode *inode = node->lt_inode;
235                 rc = ll_extent_lock(tree->lt_fd, inode,
236                                     ll_i2info(inode)->lli_smd, node->lt_mode,
237                                     &node->lt_policy, &node->lt_lockh,
238                                     ast_flags);
239                 if (rc != 0)
240                         GOTO(out, rc);
241
242                 rb_erase(&node->lt_node, &tree->lt_root);
243                 list_add_tail(&node->lt_locked_item, &tree->lt_locked_list);
244         }
245         RETURN(rc);
246 out:
247         ll_tree_unlock(tree);
248         RETURN(rc);
249 }
250
251 static ldlm_mode_t mode_from_vma(struct vm_area_struct *vma)
252 {
253         /* we only want to hold PW locks if the mmap() can generate
254          * writes back to the file and that only happens in shared
255          * writable vmas */
256         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
257                 return LCK_PW;
258         return LCK_PR;
259 }
260
261 static void policy_from_vma(ldlm_policy_data_t *policy,
262                             struct vm_area_struct *vma, unsigned long addr,
263                             size_t count)
264 {
265         policy->l_extent.start = ((addr - vma->vm_start) & CFS_PAGE_MASK) +
266                                  (vma->vm_pgoff << CFS_PAGE_SHIFT);
267         policy->l_extent.end = (policy->l_extent.start + count - 1) |
268                                ~CFS_PAGE_MASK;
269 }
270
271 static struct vm_area_struct * our_vma(unsigned long addr, size_t count)
272 {
273         struct mm_struct *mm = current->mm;
274         struct vm_area_struct *vma, *ret = NULL;
275         ENTRY;
276
277         /* No MM (e.g. NFS)? No vmas too. */
278         if (!mm)
279                 RETURN(NULL);
280
281         spin_lock(&mm->page_table_lock);
282         for(vma = find_vma(mm, addr);
283             vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
284                 if (vma->vm_ops && vma->vm_ops->nopage == ll_nopage &&
285                     vma->vm_flags & VM_SHARED) {
286                         ret = vma;
287                         break;
288                 }
289         }
290         spin_unlock(&mm->page_table_lock);
291         RETURN(ret);
292 }
293
294 int lt_get_mmap_locks(struct ll_lock_tree *tree,
295                       unsigned long addr, size_t count)
296 {
297         struct vm_area_struct *vma;
298         struct ll_lock_tree_node *node;
299         ldlm_policy_data_t policy;
300         struct inode *inode;
301         ENTRY;
302
303         if (count == 0)
304                 RETURN(0);
305
306         /* we need to look up vmas on page aligned addresses */
307         count += addr & (~CFS_PAGE_MASK);
308         addr &= CFS_PAGE_MASK;
309
310         while ((vma = our_vma(addr, count)) != NULL) {
311                 LASSERT(vma->vm_file);
312
313                 inode = vma->vm_file->f_dentry->d_inode;
314                 policy_from_vma(&policy, vma, addr, count);
315                 node = ll_node_from_inode(inode, policy.l_extent.start,
316                                           policy.l_extent.end,
317                                           mode_from_vma(vma));
318                 if (IS_ERR(node)) {
319                         CERROR("not enough mem for lock_tree_node!\n");
320                         RETURN(-ENOMEM);
321                 }
322                 lt_insert(tree, node);
323
324                 if (vma->vm_end - addr >= count)
325                         break;
326                 count -= vma->vm_end - addr;
327                 addr = vma->vm_end;
328         }
329         RETURN(0);
330 }
331
332 /* FIXME: there is a pagefault race goes as follow (only 2.4):
333  * 1. A user process on node A accesses a portion of a mapped file,
334  *    resulting in a page fault.  The pagefault handler invokes the
335  *    ll_nopage function, which reads the page into memory.
336  * 2. A user process on node B writes to the same portion of the file
337  *    (either via mmap or write()), that cause node A to cancel the
338  *    lock and truncate the page.
339  * 3. Node A then executes the rest of do_no_page(), entering the
340  *    now-invalid page into the PTEs.
341  *
342  * Make the whole do_no_page as a hook to cover both the page cache
343  * and page mapping installing with dlm lock would eliminate this race.
344  *
345  * In 2.6, the truncate_count of address_space can cover this race.
346  */
347 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
348                        int *type)
349 {
350         struct file *filp = vma->vm_file;
351         struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
352         struct inode *inode = filp->f_dentry->d_inode;
353         struct lustre_handle lockh = { 0 };
354         ldlm_policy_data_t policy;
355         ldlm_mode_t mode;
356         struct page *page = NULL;
357         struct ll_inode_info *lli = ll_i2info(inode);
358         struct lov_stripe_md *lsm;
359         struct ost_lvb lvb;
360         __u64 kms, old_mtime;
361         unsigned long pgoff, size, rand_read, seq_read;
362         int rc = 0;
363         ENTRY;
364
365         if (lli->lli_smd == NULL) {
366                 CERROR("No lsm on fault?\n");
367                 RETURN(NULL);
368         }
369
370         /* start and end the lock on the first and last bytes in the page */
371         policy_from_vma(&policy, vma, address, CFS_PAGE_SIZE);
372
373         CDEBUG(D_MMAP, "nopage vma %p inode %lu, locking ["LPU64", "LPU64"]\n",
374                vma, inode->i_ino, policy.l_extent.start, policy.l_extent.end);
375
376         mode = mode_from_vma(vma);
377         old_mtime = LTIME_S(inode->i_mtime);
378
379         lsm = lli->lli_smd;
380         rc = ll_extent_lock(fd, inode, lsm, mode, &policy,
381                             &lockh, LDLM_FL_CBPENDING | LDLM_FL_NO_LRU);
382         if (rc != 0)
383                 RETURN(NULL);
384
385         if (vma->vm_flags & VM_EXEC && LTIME_S(inode->i_mtime) != old_mtime)
386                 CWARN("binary changed. inode %lu\n", inode->i_ino);
387
388         lov_stripe_lock(lsm);
389         inode_init_lvb(inode, &lvb);
390         obd_merge_lvb(ll_i2dtexp(inode), lsm, &lvb, 1);
391         kms = lvb.lvb_size;
392
393         pgoff = ((address - vma->vm_start) >> CFS_PAGE_SHIFT) + vma->vm_pgoff;
394         size = (kms + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
395
396         if (pgoff >= size) {
397                 lov_stripe_unlock(lsm);
398                 ll_glimpse_size(inode, LDLM_FL_BLOCK_GRANTED);
399         } else {
400                 /* XXX change inode size without ll_inode_size_lock() held!
401                  *     there is a race condition with truncate path. (see
402                  *     ll_extent_lock) */
403                 /* XXX i_size_write() is not used because it is not safe to
404                  *     take the ll_inode_size_lock() due to a potential lock
405                  *     inversion (bug 6077).  And since it's not safe to use
406                  *     i_size_write() without a covering mutex we do the
407                  *     assignment directly.  It is not critical that the
408                  *     size be correct. */
409                 /* region is within kms and, hence, within real file size (A).
410                  * We need to increase i_size to cover the read region so that
411                  * generic_file_read() will do its job, but that doesn't mean
412                  * the kms size is _correct_, it is only the _minimum_ size.
413                  * If someone does a stat they will get the correct size which
414                  * will always be >= the kms value here.  b=11081 */
415                 if (i_size_read(inode) < kms) {
416                         inode->i_size = kms;
417                         CDEBUG(D_INODE, "ino=%lu, updating i_size %llu\n",
418                                inode->i_ino, i_size_read(inode));
419                 }
420                 lov_stripe_unlock(lsm);
421         }
422
423         /* If mapping is writeable, adjust kms to cover this page,
424          * but do not extend kms beyond actual file size.
425          * policy.l_extent.end is set to the end of the page by policy_from_vma
426          * bug 10919 */
427         lov_stripe_lock(lsm);
428         if (mode == LCK_PW)
429                 obd_adjust_kms(ll_i2dtexp(inode), lsm,
430                                min_t(loff_t, policy.l_extent.end + 1,
431                                i_size_read(inode)), 0);
432         lov_stripe_unlock(lsm);
433
434         /* disable VM_SEQ_READ and use VM_RAND_READ to make sure that
435          * the kernel will not read other pages not covered by ldlm in
436          * filemap_nopage. we do our readahead in ll_readpage.
437          */
438         rand_read = vma->vm_flags & VM_RAND_READ;
439         seq_read = vma->vm_flags & VM_SEQ_READ;
440         vma->vm_flags &= ~ VM_SEQ_READ;
441         vma->vm_flags |= VM_RAND_READ;
442
443         page = filemap_nopage(vma, address, type);
444         LL_CDEBUG_PAGE(D_PAGE, page, "got addr %lu type %lx\n", address,
445                        (long)type);
446         vma->vm_flags &= ~VM_RAND_READ;
447         vma->vm_flags |= (rand_read | seq_read);
448
449         ll_extent_unlock(fd, inode, ll_i2info(inode)->lli_smd, mode, &lockh);
450         RETURN(page);
451 }
452
453 /* To avoid cancel the locks covering mmapped region for lock cache pressure,
454  * we track the mapped vma count by lli_mmap_cnt.
455  * ll_vm_open():  when first vma is linked, split locks from lru.
456  * ll_vm_close(): when last vma is unlinked, join all this file's locks to lru.
457  *
458  * XXX we don't check the if the region of vma/lock for performance.
459  */
460 static void ll_vm_open(struct vm_area_struct * vma)
461 {
462         struct inode *inode = vma->vm_file->f_dentry->d_inode;
463         struct ll_inode_info *lli = ll_i2info(inode);
464         ENTRY;
465
466         LASSERT(vma->vm_file);
467
468         spin_lock(&lli->lli_lock);
469         LASSERT(atomic_read(&lli->lli_mmap_cnt) >= 0);
470
471         atomic_inc(&lli->lli_mmap_cnt);
472         if (atomic_read(&lli->lli_mmap_cnt) == 1) {
473                 struct lov_stripe_md *lsm = lli->lli_smd;
474                 struct ll_sb_info *sbi = ll_i2sbi(inode);
475                 int count;
476
477                 spin_unlock(&lli->lli_lock);
478
479                 if (!lsm)
480                         return;
481                 count = obd_join_lru(sbi->ll_dt_exp, lsm, 0);
482                 VMA_DEBUG(vma, "split %d unused locks from lru\n", count);
483         } else {
484                 spin_unlock(&lli->lli_lock);
485         }
486
487 }
488
489 static void ll_vm_close(struct vm_area_struct *vma)
490 {
491         struct inode *inode = vma->vm_file->f_dentry->d_inode;
492         struct ll_inode_info *lli = ll_i2info(inode);
493         ENTRY;
494
495         LASSERT(vma->vm_file);
496
497         spin_lock(&lli->lli_lock);
498         LASSERT(atomic_read(&lli->lli_mmap_cnt) > 0);
499
500         atomic_dec(&lli->lli_mmap_cnt);
501         if (atomic_read(&lli->lli_mmap_cnt) == 0) {
502                 struct lov_stripe_md *lsm = lli->lli_smd;
503                 struct ll_sb_info *sbi = ll_i2sbi(inode);
504                 int count;
505
506                 spin_unlock(&lli->lli_lock);
507
508                 if (!lsm)
509                         return;
510                 count = obd_join_lru(sbi->ll_dt_exp, lsm, 1);
511                 VMA_DEBUG(vma, "join %d unused locks to lru\n", count);
512         } else {
513                 spin_unlock(&lli->lli_lock);
514         }
515 }
516
517 #ifndef HAVE_FILEMAP_POPULATE
518 static int (*filemap_populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
519 #endif
520 static int ll_populate(struct vm_area_struct *area, unsigned long address,
521                        unsigned long len, pgprot_t prot, unsigned long pgoff,
522                        int nonblock)
523 {
524         int rc = 0;
525         ENTRY;
526
527         /* always set nonblock as true to avoid page read ahead */
528         rc = filemap_populate(area, address, len, prot, pgoff, 1);
529         RETURN(rc);
530 }
531
532 /* return the user space pointer that maps to a file offset via a vma */
533 static inline unsigned long file_to_user(struct vm_area_struct *vma, __u64 byte)
534 {
535         return vma->vm_start + (byte - ((__u64)vma->vm_pgoff << CFS_PAGE_SHIFT));
536
537 }
538
539 /* XXX put nice comment here.  talk about __free_pte -> dirty pages and
540  * nopage's reference passing to the pte */
541 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last)
542 {
543         int rc = -ENOENT;
544         ENTRY;
545
546         LASSERTF(last > first, "last "LPU64" first "LPU64"\n", last, first);
547         if (mapping_mapped(mapping)) {
548                 rc = 0;
549                 unmap_mapping_range(mapping, first + CFS_PAGE_SIZE - 1,
550                                     last - first + 1, 0);
551         }
552
553         RETURN(rc);
554 }
555
556 static struct vm_operations_struct ll_file_vm_ops = {
557         .nopage         = ll_nopage,
558         .open           = ll_vm_open,
559         .close          = ll_vm_close,
560         .populate       = ll_populate,
561 };
562
563 int ll_file_mmap(struct file * file, struct vm_area_struct * vma)
564 {
565         int rc;
566         ENTRY;
567
568         ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode), LPROC_LL_MAP, 1);
569         rc = generic_file_mmap(file, vma);
570         if (rc == 0) {
571 #if !defined(HAVE_FILEMAP_POPULATE)
572                 if (!filemap_populate)
573                         filemap_populate = vma->vm_ops->populate;
574 #endif
575                 vma->vm_ops = &ll_file_vm_ops;
576                 vma->vm_ops->open(vma);
577                 /* update the inode's size and mtime */
578                 rc = ll_glimpse_size(file->f_dentry->d_inode, 0);
579         }
580
581         RETURN(rc);
582 }