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 #include <linux/pagevec.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <linux/lustre_mds.h>
50 #include <linux/lustre_lite.h>
51 #include <linux/lustre_audit.h>
52 #include "llite_internal.h"
53 #include <linux/lustre_compat25.h>
55 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
56 int lt_get_mmap_locks(struct ll_lock_tree *tree, struct inode *inode,
57 unsigned long addr, size_t count);
58 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
59 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
62 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
66 struct ll_lock_tree_node * ll_node_from_inode(struct inode *inode, __u64 start,
67 __u64 end, ldlm_mode_t mode)
69 struct ll_lock_tree_node *node;
71 OBD_ALLOC(node, sizeof(*node));
73 RETURN(ERR_PTR(-ENOMEM));
75 node->lt_oid = ll_i2info(inode)->lli_smd->lsm_object_id;
76 node->lt_policy.l_extent.start = start;
77 node->lt_policy.l_extent.end = end;
78 memset(&node->lt_lockh, 0, sizeof(node->lt_lockh));
79 INIT_LIST_HEAD(&node->lt_locked_item);
85 int lt_compare(struct ll_lock_tree_node *one, struct ll_lock_tree_node *two)
87 /* XXX remove this assert when we really want to use this function
88 * to compare different file's region */
89 LASSERT(one->lt_oid == two->lt_oid);
91 if ( one->lt_oid < two->lt_oid)
93 if ( one->lt_oid > two->lt_oid)
96 if ( one->lt_policy.l_extent.end < two->lt_policy.l_extent.start )
98 if ( one->lt_policy.l_extent.start > two->lt_policy.l_extent.end )
101 return 0; /* they are the same object and overlap */
104 static void lt_merge(struct ll_lock_tree_node *dst,
105 struct ll_lock_tree_node *src)
107 dst->lt_policy.l_extent.start = min(dst->lt_policy.l_extent.start,
108 src->lt_policy.l_extent.start);
109 dst->lt_policy.l_extent.end = max(dst->lt_policy.l_extent.end,
110 src->lt_policy.l_extent.end);
112 /* XXX could be a real call to the dlm to find superset modes */
113 if (src->lt_mode == LCK_PW && dst->lt_mode != LCK_PW)
114 dst->lt_mode = LCK_PW;
117 static void lt_insert(struct ll_lock_tree *tree,
118 struct ll_lock_tree_node *node)
120 struct ll_lock_tree_node *walk;
121 rb_node_t **p, *parent;
125 p = &tree->lt_root.rb_node;
129 walk = rb_entry(parent, struct ll_lock_tree_node, lt_node);
130 switch (lt_compare(node, walk)) {
138 lt_merge(node, walk);
139 rb_erase(&walk->lt_node, &tree->lt_root);
140 OBD_FREE(walk, sizeof(*walk));
148 rb_link_node(&node->lt_node, parent, p);
149 rb_insert_color(&node->lt_node, &tree->lt_root);
153 static struct ll_lock_tree_node *lt_least_node(struct ll_lock_tree *tree)
156 struct ll_lock_tree_node *node = NULL;
158 for ( rbnode = tree->lt_root.rb_node; rbnode != NULL;
159 rbnode = rbnode->rb_left) {
160 if (rbnode->rb_left == NULL) {
161 node = rb_entry(rbnode, struct ll_lock_tree_node,
169 int ll_tree_unlock(struct ll_lock_tree *tree, struct inode *inode)
171 struct ll_lock_tree_node *node;
172 struct list_head *pos, *n;
176 list_for_each_safe(pos, n, &tree->lt_locked_list) {
177 node = list_entry(pos, struct ll_lock_tree_node,
180 rc = ll_extent_unlock(tree->lt_fd, inode,
181 ll_i2info(inode)->lli_smd, node->lt_mode,
184 /* XXX better message */
185 CERROR("couldn't unlock %d\n", rc);
187 list_del(&node->lt_locked_item);
188 OBD_FREE(node, sizeof(*node));
191 while ((node = lt_least_node(tree))) {
192 rb_erase(&node->lt_node, &tree->lt_root);
193 OBD_FREE(node, sizeof(*node));
199 int ll_tree_lock(struct ll_lock_tree *tree,
200 struct ll_lock_tree_node *first_node, struct inode *inode,
201 const char *buf, size_t count, int ast_flags)
203 struct ll_lock_tree_node *node;
207 tree->lt_root.rb_node = NULL;
208 INIT_LIST_HEAD(&tree->lt_locked_list);
209 if (first_node != NULL)
210 lt_insert(tree, first_node);
212 /* order locking. what we have to concern about is ONLY double lock:
213 * the buffer is mapped to exactly this file. */
214 if (mapping_mapped(inode->i_mapping)) {
215 rc = lt_get_mmap_locks(tree, inode, (unsigned long)buf, count);
220 while ((node = lt_least_node(tree))) {
221 struct obd_service_time *stime;
222 stime = (node->lt_mode & LCK_PW) ?
223 &ll_i2sbi(inode)->ll_write_stime :
224 &ll_i2sbi(inode)->ll_read_stime;
226 rc = ll_extent_lock(tree->lt_fd, inode,
227 ll_i2info(inode)->lli_smd, node->lt_mode,
228 &node->lt_policy, &node->lt_lockh,
233 rb_erase(&node->lt_node, &tree->lt_root);
234 list_add_tail(&node->lt_locked_item, &tree->lt_locked_list);
238 ll_tree_unlock(tree, inode);
242 static ldlm_mode_t mode_from_vma(struct vm_area_struct *vma)
244 /* we only want to hold PW locks if the mmap() can generate
245 * writes back to the file and that only happens in shared
247 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
252 static void policy_from_vma(ldlm_policy_data_t *policy,
253 struct vm_area_struct *vma, unsigned long addr,
256 policy->l_extent.start = ((addr - vma->vm_start) & PAGE_CACHE_MASK) +
257 (vma->vm_pgoff << PAGE_CACHE_SHIFT);
258 policy->l_extent.end = (policy->l_extent.start + count - 1) |
259 (PAGE_CACHE_SIZE - 1);
262 static struct vm_area_struct *our_vma(unsigned long addr, size_t count,
265 struct mm_struct *mm = current->mm;
266 struct vm_area_struct *vma, *ret = NULL;
269 spin_lock(&mm->page_table_lock);
270 for(vma = find_vma(mm, addr);
271 vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
272 if (vma->vm_ops && vma->vm_ops->nopage == ll_nopage &&
273 vma->vm_file && vma->vm_file->f_dentry->d_inode == inode) {
278 spin_unlock(&mm->page_table_lock);
282 int lt_get_mmap_locks(struct ll_lock_tree *tree, struct inode *inode,
283 unsigned long addr, size_t count)
285 struct vm_area_struct *vma;
286 struct ll_lock_tree_node *node;
287 ldlm_policy_data_t policy;
293 /* we need to look up vmas on page aligned addresses */
294 count += addr & (PAGE_SIZE - 1);
295 addr -= addr & (PAGE_SIZE - 1);
297 while ((vma = our_vma(addr, count, inode)) != NULL) {
299 policy_from_vma(&policy, vma, addr, count);
300 node = ll_node_from_inode(inode, policy.l_extent.start,
304 CERROR("not enough mem for lock_tree_node!\n");
307 lt_insert(tree, node);
309 if (vma->vm_end - addr >= count)
311 count -= vma->vm_end - addr;
316 /* FIXME: there is a pagefault race goes as follow:
317 * 1. A user process on node A accesses a portion of a mapped file,
318 * resulting in a page fault. The pagefault handler invokes the
319 * ll_nopage function, which reads the page into memory.
320 * 2. A user process on node B writes to the same portion of the file
321 * (either via mmap or write()), that cause node A to cancel the
322 * lock and truncate the page.
323 * 3. Node A then executes the rest of do_no_page(), entering the
324 * now-invalid page into the PTEs.
326 * Make the whole do_no_page as a hook to cover both the page cache
327 * and page mapping installing with dlm lock would eliminate this race.
329 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
330 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
333 struct page *ll_nopage(struct vm_area_struct *vma, unsigned long address,
337 struct file *filp = vma->vm_file;
338 struct ll_file_data *fd = filp->private_data;
339 struct inode *inode = filp->f_dentry->d_inode;
340 struct lustre_handle lockh = { 0 };
341 ldlm_policy_data_t policy;
343 struct page *page = NULL;
344 struct ll_inode_info *lli = ll_i2info(inode);
345 struct obd_service_time *stime;
347 unsigned long pgoff, size, rand_read, seq_read;
351 if (lli->lli_smd == NULL) {
352 CERROR("No lsm on fault?\n");
356 /* start and end the lock on the first and last bytes in the page */
357 policy_from_vma(&policy, vma, address, PAGE_CACHE_SIZE);
359 CDEBUG(D_MMAP, "nopage vma %p inode %lu, locking ["LPU64", "LPU64"]\n",
360 vma, inode->i_ino, policy.l_extent.start, policy.l_extent.end);
362 mode = mode_from_vma(vma);
363 stime = (mode & LCK_PW) ? &ll_i2sbi(inode)->ll_write_stime :
364 &ll_i2sbi(inode)->ll_read_stime;
366 rc = ll_extent_lock(fd, inode, lli->lli_smd, mode, &policy,
367 &lockh, LDLM_FL_CBPENDING, stime);
371 /* XXX change inode size without i_sem hold! there is a race condition
372 * with truncate path. (see ll_extent_lock) */
373 down(&lli->lli_size_sem);
374 kms = lov_merge_size(lli->lli_smd, 1);
375 pgoff = ((address - vma->vm_start) >> PAGE_CACHE_SHIFT) + vma->vm_pgoff;
376 size = (kms + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
379 up(&lli->lli_size_sem);
380 ll_glimpse_size(inode);
383 up(&lli->lli_size_sem);
386 /* disable VM_SEQ_READ and use VM_RAND_READ to make sure that
387 * the kernel will not read other pages not covered by ldlm in
388 * filemap_nopage. we do our readahead in ll_readpage.
390 rand_read = vma->vm_flags & VM_RAND_READ;
391 seq_read = vma->vm_flags & VM_SEQ_READ;
392 vma->vm_flags &= ~ VM_SEQ_READ;
393 vma->vm_flags |= VM_RAND_READ;
395 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
396 page = filemap_nopage(vma, address, type);
398 page = filemap_nopage(vma, address, unused);
400 vma->vm_flags &= ~VM_RAND_READ;
401 vma->vm_flags |= (rand_read | seq_read);
403 ll_extent_unlock(fd, inode, ll_i2info(inode)->lli_smd, mode, &lockh);
407 /* return the user space pointer that maps to a file offset via a vma */
408 static inline unsigned long file_to_user(struct vm_area_struct *vma,
411 return vma->vm_start +
412 (byte - ((__u64)vma->vm_pgoff << PAGE_CACHE_SHIFT));
415 #define VMA_DEBUG(vma, fmt, arg...) \
416 CDEBUG(D_MMAP, "vma(%p) start(%ld) end(%ld) pgoff(%ld) inode(%p) " \
417 "ino(%lu) iname(%s): " fmt, vma, vma->vm_start, vma->vm_end, \
418 vma->vm_pgoff, vma->vm_file->f_dentry->d_inode, \
419 vma->vm_file->f_dentry->d_inode->i_ino, \
420 vma->vm_file->f_dentry->d_iname, ## arg); \
422 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
423 /* [first, last] are the byte offsets affected.
424 * vm_{start, end} are user addresses of the first byte of the mapping and
425 * the next byte beyond it
426 * vm_pgoff is the page index of the first byte in the mapping */
427 static void teardown_vmas(struct vm_area_struct *vma, __u64 first,
430 unsigned long address, len;
431 for (; vma ; vma = vma->vm_next_share) {
432 if (last >> PAGE_SHIFT < vma->vm_pgoff)
434 if (first >> PAGE_CACHE_SHIFT > (vma->vm_pgoff +
435 ((vma->vm_end - vma->vm_start) >> PAGE_CACHE_SHIFT)))
438 /* XXX in case of unmap the cow pages of a running file,
439 * don't unmap these private writeable mapping here!
440 * though that will break private mappping a little.
442 * the clean way is to check the mapping of every page
443 * and just unmap the non-cow pages, just like
444 * unmap_mapping_range() with even_cow=0 in kernel 2.6.
446 if (!(vma->vm_flags & VM_SHARED) &&
447 (vma->vm_flags & VM_WRITE))
450 address = max((unsigned long)vma->vm_start,
451 file_to_user(vma, first));
452 len = min((unsigned long)vma->vm_end,
453 file_to_user(vma, last) + 1) - address;
455 VMA_DEBUG(vma, "zapping vma [first="LPU64" last="LPU64" "
456 "address=%ld len=%ld]\n", first, last, address, len);
458 ll_zap_page_range(vma, address, len);
463 /* XXX put nice comment here. talk about __free_pte -> dirty pages and
464 * nopage's reference passing to the pte */
465 int ll_teardown_mmaps(struct address_space *mapping, __u64 first,
471 LASSERTF(last > first, "last "LPU64" first "LPU64"\n", last, first);
472 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
473 if (mapping_mapped(mapping)) {
475 unmap_mapping_range(mapping, first + PAGE_SIZE - 1,
476 last - first + 1, 0);
479 spin_lock(&mapping->i_shared_lock);
480 if (mapping->i_mmap != NULL) {
482 teardown_vmas(mapping->i_mmap, first, last);
484 if (mapping->i_mmap_shared != NULL) {
486 teardown_vmas(mapping->i_mmap_shared, first, last);
488 spin_unlock(&mapping->i_shared_lock);
495 static void ll_close_vma(struct vm_area_struct *vma)
497 struct inode *inode = vma->vm_file->f_dentry->d_inode;
498 struct address_space *mapping = inode->i_mapping;
499 unsigned long next, size, end;
500 struct ll_async_page *llap;
501 struct obd_export *exp;
505 if (!(vma->vm_flags & VM_SHARED))
508 /* all pte's are synced to mem_map by the moment
509 * we scan backing store and put all dirty pages
510 * onto pending list to track flushing */
512 LASSERT(LLI_DIRTY_HANDLE(inode));
513 exp = ll_i2dtexp(inode);
515 CERROR("can't get export for the inode\n");
519 pagevec_init(&pvec, 0);
520 next = vma->vm_pgoff;
521 size = (vma->vm_end - vma->vm_start) / PAGE_SIZE;
522 end = next + size - 1;
524 CDEBUG(D_INODE, "close vma 0x%p[%lu/%lu/%lu from %lu/%u]\n", vma,
525 next, size, end, inode->i_ino, inode->i_generation);
527 while (next <= end && pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
528 for (i = 0; i < pagevec_count(&pvec); i++) {
529 struct page *page = pvec.pages[i];
531 if (page->index > next)
538 if (page->mapping != mapping || !PageDirty(page)) {
543 llap = llap_from_page(page, LLAP_ORIGIN_COMMIT_WRITE);
545 CERROR("can't get llap\n");
550 llap_write_pending(inode, llap);
553 pagevec_release(&pvec);
557 static struct vm_operations_struct ll_file_vm_ops = {
559 .close = ll_close_vma,
562 /* Audit functions */
563 extern int ll_audit_log (struct inode *, audit_op, int);
565 int ll_file_mmap(struct file * file, struct vm_area_struct * vma)
570 rc = generic_file_mmap(file, vma);
572 struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
573 vma->vm_ops = &ll_file_vm_ops;
575 /* mark i/o epoch dirty */
576 if (vma->vm_flags & VM_SHARED)
577 set_bit(LLI_F_DIRTY_HANDLE, &lli->lli_flags);
580 ll_audit_log(file->f_dentry->d_inode, AUDIT_MMAP, rc);