1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
22 #include <linux/config.h>
23 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/stat.h>
27 #include <linux/errno.h>
28 #include <linux/smp_lock.h>
29 #include <linux/unistd.h>
30 #include <linux/version.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
35 #include <linux/stat.h>
36 #include <asm/uaccess.h>
37 #include <asm/segment.h>
39 #include <linux/pagemap.h>
40 #include <linux/smp_lock.h>
41 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
42 #include <linux/iobuf.h>
45 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <linux/lustre_mds.h>
48 #include <linux/lustre_lite.h>
49 #include "llite_internal.h"
50 #include <linux/lustre_compat25.h>
52 #define VMA_DEBUG(vma, fmt, arg...) \
53 CDEBUG(D_MMAP, "vma(%p) start(%ld) end(%ld) pgoff(%ld) inode(%p) " \
54 "ino(%lu) iname(%s): " fmt, vma, vma->vm_start, vma->vm_end, \
55 vma->vm_pgoff, vma->vm_file->f_dentry->d_inode, \
56 vma->vm_file->f_dentry->d_inode->i_ino, \
57 vma->vm_file->f_dentry->d_iname, ## arg); \
60 struct ll_lock_tree_node {
62 struct list_head lt_locked_item;
64 ldlm_policy_data_t lt_policy;
65 struct lustre_handle lt_lockh;
67 struct inode *lt_inode;
70 int lt_get_mmap_locks(struct ll_lock_tree *tree,
71 unsigned long addr, size_t count);
73 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
74 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
78 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
82 struct ll_lock_tree_node * ll_node_from_inode(struct inode *inode, __u64 start,
83 __u64 end, ldlm_mode_t mode)
85 struct ll_lock_tree_node *node;
87 OBD_ALLOC(node, sizeof(*node));
89 RETURN(ERR_PTR(-ENOMEM));
91 node->lt_inode = inode;
92 node->lt_oid = ll_i2info(inode)->lli_smd->lsm_object_id;
93 node->lt_policy.l_extent.start = start;
94 node->lt_policy.l_extent.end = end;
95 memset(&node->lt_lockh, 0, sizeof(node->lt_lockh));
96 INIT_LIST_HEAD(&node->lt_locked_item);
102 int lt_compare(struct ll_lock_tree_node *one, struct ll_lock_tree_node *two)
104 /* To avoid multiple fs deadlock */
105 if (one->lt_inode->i_sb->s_dev < two->lt_inode->i_sb->s_dev)
107 if (one->lt_inode->i_sb->s_dev > two->lt_inode->i_sb->s_dev)
110 if (one->lt_oid < two->lt_oid)
112 if (one->lt_oid > two->lt_oid)
115 if (one->lt_policy.l_extent.end < two->lt_policy.l_extent.start)
117 if (one->lt_policy.l_extent.start > two->lt_policy.l_extent.end)
120 return 0; /* they are the same object and overlap */
123 static void lt_merge(struct ll_lock_tree_node *dst,
124 struct ll_lock_tree_node *src)
126 dst->lt_policy.l_extent.start = min(dst->lt_policy.l_extent.start,
127 src->lt_policy.l_extent.start);
128 dst->lt_policy.l_extent.end = max(dst->lt_policy.l_extent.end,
129 src->lt_policy.l_extent.end);
131 /* XXX could be a real call to the dlm to find superset modes */
132 if (src->lt_mode == LCK_PW && dst->lt_mode != LCK_PW)
133 dst->lt_mode = LCK_PW;
136 static void lt_insert(struct ll_lock_tree *tree,
137 struct ll_lock_tree_node *node)
139 struct ll_lock_tree_node *walk;
140 rb_node_t **p, *parent;
144 p = &tree->lt_root.rb_node;
148 walk = rb_entry(parent, struct ll_lock_tree_node, lt_node);
149 switch (lt_compare(node, walk)) {
157 lt_merge(node, walk);
158 rb_erase(&walk->lt_node, &tree->lt_root);
159 OBD_FREE(walk, sizeof(*walk));
167 rb_link_node(&node->lt_node, parent, p);
168 rb_insert_color(&node->lt_node, &tree->lt_root);
172 static struct ll_lock_tree_node *lt_least_node(struct ll_lock_tree *tree)
175 struct ll_lock_tree_node *node = NULL;
177 for ( rbnode = tree->lt_root.rb_node; rbnode != NULL;
178 rbnode = rbnode->rb_left) {
179 if (rbnode->rb_left == NULL) {
180 node = rb_entry(rbnode, struct ll_lock_tree_node,
188 int ll_tree_unlock(struct ll_lock_tree *tree)
190 struct ll_lock_tree_node *node;
191 struct list_head *pos, *n;
196 list_for_each_safe(pos, n, &tree->lt_locked_list) {
197 node = list_entry(pos, struct ll_lock_tree_node,
200 inode = node->lt_inode;
201 rc = ll_extent_unlock(tree->lt_fd, inode,
202 ll_i2info(inode)->lli_smd, node->lt_mode,
205 /* XXX better message */
206 CERROR("couldn't unlock %d\n", rc);
208 list_del(&node->lt_locked_item);
209 OBD_FREE(node, sizeof(*node));
212 while ((node = lt_least_node(tree))) {
213 rb_erase(&node->lt_node, &tree->lt_root);
214 OBD_FREE(node, sizeof(*node));
220 int ll_tree_lock(struct ll_lock_tree *tree,
221 struct ll_lock_tree_node *first_node,
222 const char *buf, size_t count, int ast_flags)
224 struct ll_lock_tree_node *node;
228 tree->lt_root.rb_node = NULL;
229 INIT_LIST_HEAD(&tree->lt_locked_list);
230 if (first_node != NULL)
231 lt_insert(tree, first_node);
233 /* To avoid such subtle deadlock case: client1 try to read file1 to
234 * mmapped file2, on the same time, client2 try to read file2 to
236 rc = lt_get_mmap_locks(tree, (unsigned long)buf, count);
240 while ((node = lt_least_node(tree))) {
241 struct inode *inode = node->lt_inode;
242 rc = ll_extent_lock(tree->lt_fd, inode,
243 ll_i2info(inode)->lli_smd, node->lt_mode,
244 &node->lt_policy, &node->lt_lockh,
249 rb_erase(&node->lt_node, &tree->lt_root);
250 list_add_tail(&node->lt_locked_item, &tree->lt_locked_list);
254 ll_tree_unlock(tree);
258 static ldlm_mode_t mode_from_vma(struct vm_area_struct *vma)
260 /* we only want to hold PW locks if the mmap() can generate
261 * writes back to the file and that only happens in shared
263 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
268 static void policy_from_vma(ldlm_policy_data_t *policy,
269 struct vm_area_struct *vma, unsigned long addr,
272 policy->l_extent.start = ((addr - vma->vm_start) & PAGE_CACHE_MASK) +
273 (vma->vm_pgoff << PAGE_CACHE_SHIFT);
274 policy->l_extent.end = (policy->l_extent.start + count - 1) |
275 (PAGE_CACHE_SIZE - 1);
278 static struct vm_area_struct * our_vma(unsigned long addr, size_t count)
280 struct mm_struct *mm = current->mm;
281 struct vm_area_struct *vma, *ret = NULL;
284 /* No MM (e.g. NFS)? No vmas too. */
288 spin_lock(&mm->page_table_lock);
289 for(vma = find_vma(mm, addr);
290 vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
291 if (vma->vm_ops && vma->vm_ops->nopage == ll_nopage &&
292 vma->vm_flags & VM_SHARED) {
297 spin_unlock(&mm->page_table_lock);
301 int lt_get_mmap_locks(struct ll_lock_tree *tree,
302 unsigned long addr, size_t count)
304 struct vm_area_struct *vma;
305 struct ll_lock_tree_node *node;
306 ldlm_policy_data_t policy;
313 /* we need to look up vmas on page aligned addresses */
314 count += addr & (PAGE_SIZE - 1);
317 while ((vma = our_vma(addr, count)) != NULL) {
318 LASSERT(vma->vm_file);
320 inode = vma->vm_file->f_dentry->d_inode;
321 policy_from_vma(&policy, vma, addr, count);
322 node = ll_node_from_inode(inode, policy.l_extent.start,
326 CERROR("not enough mem for lock_tree_node!\n");
329 lt_insert(tree, node);
331 if (vma->vm_end - addr >= count)
333 count -= vma->vm_end - addr;
339 /* FIXME: there is a pagefault race goes as follow (only 2.4):
340 * 1. A user process on node A accesses a portion of a mapped file,
341 * resulting in a page fault. The pagefault handler invokes the
342 * ll_nopage function, which reads the page into memory.
343 * 2. A user process on node B writes to the same portion of the file
344 * (either via mmap or write()), that cause node A to cancel the
345 * lock and truncate the page.
346 * 3. Node A then executes the rest of do_no_page(), entering the
347 * now-invalid page into the PTEs.
349 * Make the whole do_no_page as a hook to cover both the page cache
350 * and page mapping installing with dlm lock would eliminate this race.
352 * In 2.6, the truncate_count of address_space can cover this race.
354 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
355 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
358 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
359 int type /* unused */)
362 struct file *filp = vma->vm_file;
363 struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
364 struct inode *inode = filp->f_dentry->d_inode;
365 struct lustre_handle lockh = { 0 };
366 ldlm_policy_data_t policy;
368 struct page *page = NULL;
369 struct ll_inode_info *lli = ll_i2info(inode);
370 __u64 kms, old_mtime;
371 unsigned long pgoff, size, rand_read, seq_read;
375 if (lli->lli_smd == NULL) {
376 CERROR("No lsm on fault?\n");
380 /* start and end the lock on the first and last bytes in the page */
381 policy_from_vma(&policy, vma, address, PAGE_CACHE_SIZE);
383 CDEBUG(D_MMAP, "nopage vma %p inode %lu, locking ["LPU64", "LPU64"]\n",
384 vma, inode->i_ino, policy.l_extent.start, policy.l_extent.end);
386 mode = mode_from_vma(vma);
387 old_mtime = LTIME_S(inode->i_mtime);
389 rc = ll_extent_lock(fd, inode, lli->lli_smd, mode, &policy,
390 &lockh, LDLM_FL_CBPENDING | LDLM_FL_NO_LRU);
394 if (vma->vm_flags & VM_EXEC && LTIME_S(inode->i_mtime) != old_mtime)
395 CWARN("binary changed. inode %lu\n", inode->i_ino);
397 /* XXX change inode size without i_sem hold! there is a race condition
398 * with truncate path. (see ll_extent_lock) */
399 //down(&lli->lli_size_sem);
400 kms = lov_merge_size(lli->lli_smd, 1);
401 pgoff = ((address - vma->vm_start) >> PAGE_CACHE_SHIFT) + vma->vm_pgoff;
402 size = (kms + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
405 //up(&lli->lli_size_sem);
406 ll_glimpse_size(inode);
409 //up(&lli->lli_size_sem);
412 /* disable VM_SEQ_READ and use VM_RAND_READ to make sure that
413 * the kernel will not read other pages not covered by ldlm in
414 * filemap_nopage. we do our readahead in ll_readpage.
416 rand_read = vma->vm_flags & VM_RAND_READ;
417 seq_read = vma->vm_flags & VM_SEQ_READ;
418 vma->vm_flags &= ~ VM_SEQ_READ;
419 vma->vm_flags |= VM_RAND_READ;
421 page = filemap_nopage(vma, address, type);
422 LL_CDEBUG_PAGE(D_PAGE, page, "got addr %lu type %lx\n", address,
424 vma->vm_flags &= ~VM_RAND_READ;
425 vma->vm_flags |= (rand_read | seq_read);
427 ll_extent_unlock(fd, inode, ll_i2info(inode)->lli_smd, mode, &lockh);
431 /* To avoid cancel the locks covering mmapped region for lock cache pressure,
432 * we track the mapped vma count by lli_mmap_cnt.
433 * ll_vm_open(): when first vma is linked, split locks from lru.
434 * ll_vm_close(): when last vma is unlinked, join all this file's locks to lru.
436 * XXX we don't check the if the region of vma/lock for performance.
438 static void ll_vm_open(struct vm_area_struct * vma)
440 struct inode *inode = vma->vm_file->f_dentry->d_inode;
441 struct ll_inode_info *lli = ll_i2info(inode);
444 LASSERT(vma->vm_file);
446 spin_lock(&lli->lli_lock);
447 LASSERT(atomic_read(&lli->lli_mmap_cnt) >= 0);
449 atomic_inc(&lli->lli_mmap_cnt);
450 if (atomic_read(&lli->lli_mmap_cnt) == 1) {
451 struct lov_stripe_md *lsm = lli->lli_smd;
452 struct ll_sb_info *sbi = ll_i2sbi(inode);
455 spin_unlock(&lli->lli_lock);
456 count = obd_join_lru(sbi->ll_osc_exp, lsm, 0);
457 VMA_DEBUG(vma, "split %d unused locks from lru\n", count);
459 spin_unlock(&lli->lli_lock);
464 static void ll_vm_close(struct vm_area_struct *vma)
466 struct inode *inode = vma->vm_file->f_dentry->d_inode;
467 struct ll_inode_info *lli = ll_i2info(inode);
470 LASSERT(vma->vm_file);
472 spin_lock(&lli->lli_lock);
473 LASSERT(atomic_read(&lli->lli_mmap_cnt) > 0);
475 atomic_dec(&lli->lli_mmap_cnt);
476 if (atomic_read(&lli->lli_mmap_cnt) == 0) {
477 struct lov_stripe_md *lsm = lli->lli_smd;
478 struct ll_sb_info *sbi = ll_i2sbi(inode);
481 spin_unlock(&lli->lli_lock);
482 count = obd_join_lru(sbi->ll_osc_exp, lsm, 1);
483 VMA_DEBUG(vma, "join %d unused locks to lru\n", count);
485 spin_unlock(&lli->lli_lock);
489 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
490 static int ll_populate(struct vm_area_struct *area, unsigned long address,
491 unsigned long len, pgprot_t prot, unsigned long pgoff,
497 /* always set nonblock as true to avoid page read ahead */
498 rc = filemap_populate(area, address, len, prot, pgoff, 1);
503 /* return the user space pointer that maps to a file offset via a vma */
504 static inline unsigned long file_to_user(struct vm_area_struct *vma, __u64 byte)
506 return vma->vm_start + (byte - ((__u64)vma->vm_pgoff << PAGE_SHIFT));
510 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
511 /* [first, last] are the byte offsets affected.
512 * vm_{start, end} are user addresses of the first byte of the mapping and
513 * the next byte beyond it
514 * vm_pgoff is the page index of the first byte in the mapping */
515 static void teardown_vmas(struct vm_area_struct *vma, __u64 first,
518 unsigned long address, len;
519 for (; vma ; vma = vma->vm_next_share) {
520 if (last >> PAGE_SHIFT < vma->vm_pgoff)
522 if (first >> PAGE_SHIFT >= (vma->vm_pgoff +
523 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
526 /* XXX in case of unmap the cow pages of a running file,
527 * don't unmap these private writeable mapping here!
528 * though that will break private mappping a little.
530 * the clean way is to check the mapping of every page
531 * and just unmap the non-cow pages, just like
532 * unmap_mapping_range() with even_cow=0 in kernel 2.6.
534 if (!(vma->vm_flags & VM_SHARED) &&
535 (vma->vm_flags & VM_WRITE))
538 address = max((unsigned long)vma->vm_start,
539 file_to_user(vma, first));
540 len = min((unsigned long)vma->vm_end,
541 file_to_user(vma, last) + 1) - address;
543 VMA_DEBUG(vma, "zapping vma [first="LPU64" last="LPU64" "
544 "address=%ld len=%ld]\n", first, last, address, len);
546 ll_zap_page_range(vma, address, len);
551 /* XXX put nice comment here. talk about __free_pte -> dirty pages and
552 * nopage's reference passing to the pte */
553 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last)
558 LASSERTF(last > first, "last "LPU64" first "LPU64"\n", last, first);
559 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
560 if (mapping_mapped(mapping)) {
562 unmap_mapping_range(mapping, first + PAGE_SIZE - 1,
563 last - first + 1, 0);
566 spin_lock(&mapping->i_shared_lock);
567 if (mapping->i_mmap != NULL) {
569 teardown_vmas(mapping->i_mmap, first, last);
571 if (mapping->i_mmap_shared != NULL) {
573 teardown_vmas(mapping->i_mmap_shared, first, last);
575 spin_unlock(&mapping->i_shared_lock);
580 static struct vm_operations_struct ll_file_vm_ops = {
583 .close = ll_vm_close,
584 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
585 .populate = ll_populate,
589 int ll_file_mmap(struct file * file, struct vm_area_struct * vma)
594 rc = generic_file_mmap(file, vma);
596 vma->vm_ops = &ll_file_vm_ops;
597 vma->vm_ops->open(vma);
598 /* update the inode's size and mtime */
599 rc = ll_glimpse_size(file->f_dentry->d_inode);