1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Lite I/O page cache routines shared by different kernel revs
6 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/kernel.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>
37 #include <linux/stat.h>
38 #include <asm/uaccess.h>
39 #include <asm/segment.h>
41 #include <linux/pagemap.h>
42 #include <linux/smp_lock.h>
44 #define DEBUG_SUBSYSTEM S_LLITE
46 #include <linux/lustre_mds.h>
47 #include <linux/lustre_lite.h>
48 #include "llite_internal.h"
49 #include <linux/lustre_compat25.h>
51 #ifndef list_for_each_prev_safe
52 #define list_for_each_prev_safe(pos, n, head) \
53 for (pos = (head)->prev, n = pos->prev; pos != (head); \
54 pos = n, n = pos->prev )
57 kmem_cache_t *ll_async_page_slab = NULL;
58 size_t ll_async_page_slab_size = 0;
60 /* SYNCHRONOUS I/O to object storage for an inode */
61 static int ll_brw(int cmd, struct inode *inode, struct obdo *oa,
62 struct page *page, int flags)
64 struct ll_inode_info *lli = ll_i2info(inode);
65 struct lov_stripe_md *lsm = lli->lli_smd;
71 pg.off = ((obd_off)page->index) << PAGE_SHIFT;
73 if (cmd == OBD_BRW_WRITE && (pg.off + PAGE_SIZE > inode->i_size))
74 pg.count = inode->i_size % PAGE_SIZE;
78 LL_CDEBUG_PAGE(D_PAGE, page, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
79 cmd & OBD_BRW_WRITE ? "write" : "read", pg.count,
80 inode->i_ino, pg.off, pg.off);
82 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
84 inode->i_ino, inode, inode->i_size, page->mapping->host,
85 page->mapping->host->i_size, page->index, pg.off);
90 if (cmd == OBD_BRW_WRITE)
91 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
92 LPROC_LL_BRW_WRITE, pg.count);
94 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
95 LPROC_LL_BRW_READ, pg.count);
96 rc = obd_brw(cmd, ll_i2obdexp(inode), oa, lsm, 1, &pg, NULL);
98 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
100 CERROR("error from obd_brw: rc = %d\n", rc);
104 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
106 /* this isn't where truncate starts. roughly:
107 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
108 * we grab the lock back in setattr_raw to avoid races.
110 * must be called with lli_size_sem held */
111 void ll_truncate(struct inode *inode)
113 struct ll_inode_info *lli = ll_i2info(inode);
114 struct lov_stripe_md *lsm = lli->lli_smd;
118 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %llu\n", inode->i_ino,
119 inode->i_generation, inode, inode->i_size);
121 if (lli->lli_size_pid != current->pid) {
127 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
132 LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
134 if (lov_merge_size(lsm, 0) == inode->i_size) {
135 CDEBUG(D_VFSTRACE, "skipping punch for "LPX64" (size = %llu)\n",
136 lsm->lsm_object_id, inode->i_size);
140 CDEBUG(D_INFO, "calling punch for "LPX64" (new size %llu)\n",
141 lsm->lsm_object_id, inode->i_size);
143 oa.o_id = lsm->lsm_object_id;
144 oa.o_valid = OBD_MD_FLID;
145 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLMODE |
146 OBD_MD_FLATIME |OBD_MD_FLMTIME |OBD_MD_FLCTIME);
148 obd_adjust_kms(ll_i2obdexp(inode), lsm, inode->i_size, 1);
150 lli->lli_size_pid = 0;
151 up(&lli->lli_size_sem);
153 rc = obd_punch(ll_i2obdexp(inode), &oa, lsm, inode->i_size,
154 OBD_OBJECT_EOF, NULL);
156 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
158 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE|OBD_MD_FLBLOCKS|
159 OBD_MD_FLATIME | OBD_MD_FLMTIME |
165 lli->lli_size_pid = 0;
166 up(&lli->lli_size_sem);
169 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
170 int ll_prepare_write(struct file *file, struct page *page, unsigned from,
173 struct inode *inode = page->mapping->host;
174 struct ll_inode_info *lli = ll_i2info(inode);
175 struct lov_stripe_md *lsm = lli->lli_smd;
176 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
183 LASSERT(PageLocked(page));
184 (void)llap_cast_private(page); /* assertion */
186 /* Check to see if we should return -EIO right away */
189 pga.count = PAGE_SIZE;
192 oa.o_id = lsm->lsm_object_id;
193 oa.o_mode = inode->i_mode;
194 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
196 rc = obd_brw(OBD_BRW_CHECK, ll_i2obdexp(inode), &oa, lsm, 1, &pga,
201 if (PageUptodate(page)) {
202 LL_CDEBUG_PAGE(D_PAGE, page, "uptodate\n");
206 /* We're completely overwriting an existing page, so _don't_ set it up
207 * to date until commit_write */
208 if (from == 0 && to == PAGE_SIZE) {
209 LL_CDEBUG_PAGE(D_PAGE, page, "full page write\n");
210 POISON_PAGE(page, 0x11);
214 /* If are writing to a new page, no need to read old data. The extent
215 * locking will have updated the KMS, and for our purposes here we can
216 * treat it like i_size. */
217 down(&lli->lli_size_sem);
218 kms = lov_merge_size(lsm, 1);
219 up(&lli->lli_size_sem);
221 LL_CDEBUG_PAGE(D_PAGE, page, "kms "LPU64" <= offset "LPU64"\n",
223 memset(kmap(page), 0, PAGE_SIZE);
225 GOTO(prepare_done, rc = 0);
228 /* XXX could be an async ocp read.. read-ahead? */
229 rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
231 /* bug 1598: don't clobber blksize */
232 oa.o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
233 obdo_refresh_inode(inode, &oa, oa.o_valid);
239 SetPageUptodate(page);
244 struct ll_async_page *llap_from_cookie(void *cookie)
246 struct ll_async_page *llap = cookie;
247 if (llap->llap_magic != LLAP_MAGIC)
248 return ERR_PTR(-EINVAL);
252 static int ll_ap_make_ready(void *data, int cmd)
254 struct ll_async_page *llap;
258 llap = llap_from_cookie(data);
262 page = llap->llap_page;
264 LASSERT(cmd != OBD_BRW_READ);
266 /* we're trying to write, but the page is locked.. come back later */
267 if (TryLockPage(page))
270 LL_CDEBUG_PAGE(D_PAGE, page, "made ready\n");
271 page_cache_get(page);
273 /* if we left PageDirty we might get another writepage call
274 * in the future. list walkers are bright enough
275 * to check page dirty so we can leave it on whatever list
276 * its on. XXX also, we're called with the cli list so if
277 * we got the page cache list we'd create a lock inversion
278 * with the removepage path which gets the page lock then the
280 clear_page_dirty(page);
284 /* We have two reasons for giving llite the opportunity to change the
285 * write length of a given queued page as it builds the RPC containing
288 * 1) Further extending writes may have landed in the page cache
289 * since a partial write first queued this page requiring us
290 * to write more from the page cache. (No further races are possible, since
291 * by the time this is called, the page is locked.)
292 * 2) We might have raced with truncate and want to avoid performing
293 * write RPCs that are just going to be thrown away by the
294 * truncate's punch on the storage targets.
296 * The kms serves these purposes as it is set at both truncate and extending
299 static int ll_ap_refresh_count(void *data, int cmd)
301 struct ll_inode_info *lli;
302 struct ll_async_page *llap;
303 struct lov_stripe_md *lsm;
308 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
309 LASSERT(cmd != OBD_BRW_READ);
311 llap = llap_from_cookie(data);
313 RETURN(PTR_ERR(llap));
315 page = llap->llap_page;
316 lli = ll_i2info(page->mapping->host);
319 //down(&lli->lli_size_sem);
320 kms = lov_merge_size(lsm, 1);
321 //up(&lli->lli_size_sem);
323 /* catch race with truncate */
324 if (((__u64)page->index << PAGE_SHIFT) >= kms)
327 /* catch sub-page write at end of file */
328 if (((__u64)page->index << PAGE_SHIFT) + PAGE_SIZE > kms)
329 return kms % PAGE_SIZE;
334 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
336 struct lov_stripe_md *lsm;
337 obd_flag valid_flags;
339 lsm = ll_i2info(inode)->lli_smd;
341 oa->o_id = lsm->lsm_object_id;
342 oa->o_valid = OBD_MD_FLID;
343 valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
344 if (cmd == OBD_BRW_WRITE) {
345 oa->o_valid |= OBD_MD_FLIFID | OBD_MD_FLEPOCH;
346 mdc_pack_fid(obdo_fid(oa), inode->i_ino, 0, inode->i_mode);
347 oa->o_easize = ll_i2info(inode)->lli_io_epoch;
349 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
350 OBD_MD_FLUID | OBD_MD_FLGID;
353 obdo_from_inode(oa, inode, valid_flags);
356 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
358 struct ll_async_page *llap;
361 llap = llap_from_cookie(data);
367 ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
371 static void ll_ap_get_ucred(void *data, struct obd_ucred *ouc)
373 struct ll_async_page *llap;
375 llap = llap_from_cookie(data);
381 memcpy(ouc, &llap->llap_ouc, sizeof(*ouc));
385 static struct obd_async_page_ops ll_async_page_ops = {
386 .ap_make_ready = ll_ap_make_ready,
387 .ap_refresh_count = ll_ap_refresh_count,
388 .ap_fill_obdo = ll_ap_fill_obdo,
389 .ap_completion = ll_ap_completion,
390 .ap_get_ucred = ll_ap_get_ucred,
393 struct ll_async_page *llap_cast_private(struct page *page)
395 struct ll_async_page *llap = (struct ll_async_page *)page->private;
397 LASSERTF(llap == NULL || llap->llap_magic == LLAP_MAGIC,
398 "page %p private %lu gave magic %d which != %d\n",
399 page, page->private, llap->llap_magic, LLAP_MAGIC);
404 /* Try to shrink the page cache for the @sbi filesystem by 1/@shrink_fraction.
406 * There is an llap attached onto every page in lustre, linked off @sbi.
407 * We add an llap to the list so we don't lose our place during list walking.
408 * If llaps in the list are being moved they will only move to the end
409 * of the LRU, and we aren't terribly interested in those pages here (we
410 * start at the beginning of the list where the least-used llaps are.
412 int llap_shrink_cache(struct ll_sb_info *sbi, int shrink_fraction)
414 struct ll_async_page *llap, dummy_llap = { .llap_magic = 0xd11ad11a };
415 unsigned long total, want, count = 0;
417 total = sbi->ll_async_page_count;
419 /* There can be a large number of llaps (600k or more in a large
420 * memory machine) so the VM 1/6 shrink ratio is likely too much.
421 * Since we are freeing pages also, we don't necessarily want to
422 * shrink so much. Limit to 40MB of pages + llaps per call. */
423 if (shrink_fraction == 0)
424 want = sbi->ll_async_page_count - sbi->ll_async_page_max + 32;
426 want = (total + shrink_fraction - 1) / shrink_fraction;
428 if (want > 40 << (20 - PAGE_CACHE_SHIFT))
429 want = 40 << (20 - PAGE_CACHE_SHIFT);
431 CDEBUG(D_CACHE, "shrinking %lu of %lu pages (1/%d)\n",
432 want, total, shrink_fraction);
434 spin_lock(&sbi->ll_lock);
435 list_add(&dummy_llap.llap_pglist_item, &sbi->ll_pglist);
437 while (--total >= 0 && count < want) {
440 if (unlikely(need_resched())) {
441 spin_unlock(&sbi->ll_lock);
443 spin_lock(&sbi->ll_lock);
446 llap = llite_pglist_next_llap(sbi,&dummy_llap.llap_pglist_item);
447 list_del_init(&dummy_llap.llap_pglist_item);
451 page = llap->llap_page;
452 LASSERT(page != NULL);
454 list_add(&dummy_llap.llap_pglist_item, &llap->llap_pglist_item);
456 /* Page needs/undergoing IO */
457 if (TryLockPage(page)) {
458 LL_CDEBUG_PAGE(D_PAGE, page, "can't lock\n");
462 /* If page is dirty or undergoing IO don't discard it */
463 if (llap->llap_write_queued || PageDirty(page) ||
464 (!PageUptodate(page) &&
465 llap->llap_origin != LLAP_ORIGIN_READAHEAD)) {
467 LL_CDEBUG_PAGE(D_PAGE, page, "can't drop from cache: "
468 "%s%s%s%s origin %s\n",
469 llap->llap_write_queued ? "wq " : "",
470 PageDirty(page) ? "pd " : "",
471 PageUptodate(page) ? "" : "!pu ",
472 llap->llap_defer_uptodate ? "" : "!du",
473 llap_origins[llap->llap_origin]);
477 page_cache_get(page);
478 spin_unlock(&sbi->ll_lock);
481 LL_CDEBUG_PAGE(D_PAGE, page, "drop from cache %lu/%lu\n",
483 if (page->mapping != NULL) {
484 ll_ra_accounting(page, page->mapping);
485 ll_truncate_complete_page(page);
488 page_cache_release(page);
490 spin_lock(&sbi->ll_lock);
492 list_del(&dummy_llap.llap_pglist_item);
493 spin_unlock(&sbi->ll_lock);
495 CDEBUG(D_CACHE, "shrank %lu/%lu and left %lu unscanned\n",
501 struct ll_async_page *llap_from_page(struct page *page, unsigned origin)
503 struct ll_async_page *llap;
504 struct obd_export *exp;
505 struct inode *inode = page->mapping->host;
506 struct ll_sb_info *sbi = ll_i2sbi(inode);
510 LASSERT(ll_async_page_slab);
511 LASSERTF(origin < LLAP__ORIGIN_MAX, "%u\n", origin);
513 llap = llap_cast_private(page);
515 /* move to end of LRU list */
516 spin_lock(&sbi->ll_lock);
517 sbi->ll_pglist_gen++;
518 list_del_init(&llap->llap_pglist_item);
519 list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
520 spin_unlock(&sbi->ll_lock);
524 exp = ll_i2obdexp(page->mapping->host);
526 RETURN(ERR_PTR(-EINVAL));
528 /* limit the number of lustre-cached pages */
529 if (sbi->ll_async_page_count >= sbi->ll_async_page_max)
530 llap_shrink_cache(sbi, 0);
532 OBD_SLAB_ALLOC(llap, ll_async_page_slab, SLAB_KERNEL,
533 ll_async_page_slab_size);
535 RETURN(ERR_PTR(-ENOMEM));
536 llap->llap_magic = LLAP_MAGIC;
537 llap->llap_cookie = (void *)llap + size_round(sizeof(*llap));
539 rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
540 (obd_off)page->index << PAGE_SHIFT,
541 &ll_async_page_ops, llap, &llap->llap_cookie);
543 OBD_SLAB_FREE(llap, ll_async_page_slab,
544 ll_async_page_slab_size);
548 CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
549 page, llap->llap_cookie, (obd_off)page->index << PAGE_SHIFT);
550 /* also zeroing the PRIVBITS low order bitflags */
551 __set_page_ll_data(page, llap);
552 llap->llap_page = page;
554 spin_lock(&sbi->ll_lock);
555 sbi->ll_pglist_gen++;
556 sbi->ll_async_page_count++;
557 list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
558 spin_unlock(&sbi->ll_lock);
561 llap->llap_origin = origin;
565 static int queue_or_sync_write(struct obd_export *exp, struct inode *inode,
566 struct ll_async_page *llap,
567 unsigned to, obd_flag async_flags)
569 unsigned long size_index = inode->i_size >> PAGE_SHIFT;
570 struct obd_io_group *oig;
574 /* _make_ready only sees llap once we've unlocked the page */
575 llap->llap_write_queued = 1;
576 rc = obd_queue_async_io(exp, ll_i2info(inode)->lli_smd, NULL,
577 llap->llap_cookie, OBD_BRW_WRITE, 0, 0, 0,
580 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "write queued\n");
581 //llap_write_pending(inode, llap);
585 llap->llap_write_queued = 0;
591 /* make full-page requests if we are not at EOF (bug 4410) */
592 if (to != PAGE_SIZE && llap->llap_page->index < size_index) {
593 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
594 "sync write before EOF: size_index %lu, to %d\n",
597 } else if (to != PAGE_SIZE && llap->llap_page->index == size_index) {
598 int size_to = inode->i_size & ~PAGE_MASK;
599 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
600 "sync write at EOF: size_index %lu, to %d/%d\n",
601 size_index, to, size_to);
606 rc = obd_queue_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig,
607 llap->llap_cookie, OBD_BRW_WRITE, 0, to, 0,
608 ASYNC_READY | ASYNC_URGENT |
609 ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
613 rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
619 if (!rc && async_flags & ASYNC_READY)
620 unlock_page(llap->llap_page);
622 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "sync write returned %d\n", rc);
630 /* update our write count to account for i_size increases that may have
631 * happened since we've queued the page for io. */
633 /* be careful not to return success without setting the page Uptodate or
634 * the next pass through prepare_write will read in stale data from disk. */
635 int ll_commit_write(struct file *file, struct page *page, unsigned from,
638 struct inode *inode = page->mapping->host;
639 struct ll_inode_info *lli = ll_i2info(inode);
640 struct lov_stripe_md *lsm = lli->lli_smd;
641 struct obd_export *exp;
642 struct ll_async_page *llap;
643 struct ll_uctxt ctxt;
648 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
649 LASSERT(inode == file->f_dentry->d_inode);
650 LASSERT(PageLocked(page));
652 CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
653 inode, page, from, to, page->index);
655 llap = llap_from_page(page, LLAP_ORIGIN_COMMIT_WRITE);
657 RETURN(PTR_ERR(llap));
659 exp = ll_i2obdexp(inode);
663 /* set user credit information for this page */
664 llap->llap_ouc.ouc_fsuid = current->fsuid;
665 llap->llap_ouc.ouc_fsgid = current->fsgid;
666 llap->llap_ouc.ouc_cap = current->cap_effective;
667 ll_i2uctxt(&ctxt, inode, NULL);
668 llap->llap_ouc.ouc_suppgid1 = ctxt.gid1;
670 /* queue a write for some time in the future the first time we
672 if (!PageDirty(page)) {
673 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
674 LPROC_LL_DIRTY_MISSES);
676 rc = queue_or_sync_write(exp, inode, llap, to, 0);
680 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
681 LPROC_LL_DIRTY_HITS);
684 /* put the page in the page cache, from now on ll_removepage is
685 * responsible for cleaning up the llap.
686 * only set page dirty when it's queued to be write out */
687 if (llap->llap_write_queued)
688 set_page_dirty(page);
691 size = (((obd_off)page->index) << PAGE_SHIFT) + to;
692 down(&lli->lli_size_sem);
694 obd_adjust_kms(exp, lsm, size, 0);
695 if (size > inode->i_size)
696 inode->i_size = size;
697 SetPageUptodate(page);
698 } else if (size > inode->i_size) {
699 /* this page beyond the pales of i_size, so it can't be
700 * truncated in ll_p_r_e during lock revoking. we must
701 * teardown our book-keeping here. */
704 up(&lli->lli_size_sem);
708 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
710 struct ll_ra_info *ra = &sbi->ll_ra_info;
714 spin_lock(&sbi->ll_lock);
715 ret = min(ra->ra_max_pages - ra->ra_cur_pages, len);
716 ra->ra_cur_pages += ret;
717 spin_unlock(&sbi->ll_lock);
722 static void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
724 struct ll_ra_info *ra = &sbi->ll_ra_info;
725 spin_lock(&sbi->ll_lock);
726 LASSERTF(ra->ra_cur_pages >= len, "r_c_p %lu len %lu\n",
727 ra->ra_cur_pages, len);
728 ra->ra_cur_pages -= len;
729 spin_unlock(&sbi->ll_lock);
732 /* called for each page in a completed rpc.*/
733 void ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
735 struct ll_async_page *llap;
739 llap = llap_from_cookie(data);
745 page = llap->llap_page;
746 LASSERT(PageLocked(page));
748 LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
750 if (cmd == OBD_BRW_READ && llap->llap_defer_uptodate)
751 ll_ra_count_put(ll_i2sbi(page->mapping->host), 1);
754 if (cmd == OBD_BRW_READ) {
755 if (!llap->llap_defer_uptodate)
756 SetPageUptodate(page);
758 llap->llap_write_queued = 0;
760 ClearPageError(page);
762 if (cmd == OBD_BRW_READ) {
763 llap->llap_defer_uptodate = 0;
765 ll_redirty_page(page);
772 if (0 && cmd == OBD_BRW_WRITE) {
773 llap_write_complete(page->mapping->host, llap);
774 ll_try_done_writing(page->mapping->host);
777 if (PageWriteback(page)) {
778 end_page_writeback(page);
780 page_cache_release(page);
784 /* the kernel calls us here when a page is unhashed from the page cache.
785 * the page will be locked and the kernel is holding a spinlock, so
786 * we need to be careful. we're just tearing down our book-keeping
788 void ll_removepage(struct page *page)
790 struct inode *inode = page->mapping->host;
791 struct obd_export *exp;
792 struct ll_async_page *llap;
793 struct ll_sb_info *sbi = ll_i2sbi(inode);
797 LASSERT(!in_interrupt());
799 /* sync pages or failed read pages can leave pages in the page
800 * cache that don't have our data associated with them anymore */
801 if (page->private == 0) {
806 LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
808 exp = ll_i2obdexp(inode);
810 CERROR("page %p ind %lu gave null export\n", page, page->index);
815 llap = llap_from_page(page, 0);
817 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
818 page->index, PTR_ERR(llap));
823 //llap_write_complete(inode, llap);
824 rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
827 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
829 /* this unconditional free is only safe because the page lock
830 * is providing exclusivity to memory pressure/truncate/writeback..*/
831 __clear_page_ll_data(page);
833 spin_lock(&sbi->ll_lock);
834 if (!list_empty(&llap->llap_pglist_item))
835 list_del_init(&llap->llap_pglist_item);
836 sbi->ll_pglist_gen++;
837 sbi->ll_async_page_count--;
838 spin_unlock(&sbi->ll_lock);
839 OBD_SLAB_FREE(llap, ll_async_page_slab, ll_async_page_slab_size);
843 static int ll_page_matches(struct page *page, int readahead)
845 struct lustre_handle match_lockh = {0};
846 struct inode *inode = page->mapping->host;
847 ldlm_policy_data_t page_extent;
851 page_extent.l_extent.start = (__u64)page->index << PAGE_CACHE_SHIFT;
852 page_extent.l_extent.end =
853 page_extent.l_extent.start + PAGE_CACHE_SIZE - 1;
854 flags = LDLM_FL_TEST_LOCK;
856 flags |= LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
857 matches = obd_match(ll_i2sbi(inode)->ll_osc_exp,
858 ll_i2info(inode)->lli_smd, LDLM_EXTENT,
859 &page_extent, LCK_PR | LCK_PW, &flags, inode,
864 static int ll_issue_page_read(struct obd_export *exp,
865 struct ll_async_page *llap,
866 struct obd_io_group *oig, int defer)
868 struct page *page = llap->llap_page;
871 page_cache_get(page);
872 llap->llap_defer_uptodate = defer;
873 llap->llap_ra_used = 0;
874 rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
875 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
876 PAGE_SIZE, 0, ASYNC_COUNT_STABLE | ASYNC_READY
879 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
880 page_cache_release(page);
885 static void ll_ra_stats_inc_unlocked(struct ll_ra_info *ra, enum ra_stat which)
887 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
888 ra->ra_stats[which]++;
891 static void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
893 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
894 struct ll_ra_info *ra = &ll_i2sbi(mapping->host)->ll_ra_info;
896 spin_lock(&sbi->ll_lock);
897 ll_ra_stats_inc_unlocked(ra, which);
898 spin_unlock(&sbi->ll_lock);
901 void ll_ra_accounting(struct page *page, struct address_space *mapping)
903 struct ll_async_page *llap;
905 llap = llap_from_page(page, LLAP_ORIGIN_WRITEPAGE);
909 if (!llap->llap_defer_uptodate || llap->llap_ra_used)
912 ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
915 #define RAS_CDEBUG(ras) \
916 CDEBUG(D_READA, "lrp %lu c %lu ws %lu wl %lu nra %lu\n", \
917 ras->ras_last_readpage, ras->ras_consecutive, \
918 ras->ras_window_start, ras->ras_window_len, \
919 ras->ras_next_readahead);
921 static int index_in_window(unsigned long index, unsigned long point,
922 unsigned long before, unsigned long after)
924 unsigned long start = point - before, end = point + after;
931 return start <= index && index <= end;
934 static int ll_readahead(struct ll_readahead_state *ras,
935 struct obd_export *exp, struct address_space *mapping,
936 struct obd_io_group *oig, int flags)
938 unsigned long i, start = 0, end = 0, reserved;
939 struct ll_async_page *llap;
941 int rc, ret = 0, match_failed = 0;
943 unsigned int gfp_mask;
946 kms = lov_merge_size(ll_i2info(mapping->host)->lli_smd, 1);
948 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
952 spin_lock(&ras->ras_lock);
953 /* reserve a part of the read-ahead window that we'll be issuing */
954 if (ras->ras_window_len) {
955 start = ras->ras_next_readahead;
956 end = ras->ras_window_start + ras->ras_window_len - 1;
957 end = min(end, (unsigned long)((kms - 1) >> PAGE_CACHE_SHIFT));
958 ras->ras_next_readahead = max(end, end + 1);
962 spin_unlock(&ras->ras_lock);
965 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
969 reserved = ll_ra_count_get(ll_i2sbi(mapping->host), end - start + 1);
970 if (reserved < end - start + 1)
971 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
973 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
975 gfp_mask |= __GFP_NOWARN;
978 for (i = start; reserved > 0 && !match_failed && i <= end; i++) {
979 /* skip locked pages from previous readpage calls */
980 page = grab_cache_page_nowait_gfp(mapping, i, gfp_mask);
982 CDEBUG(D_READA, "g_c_p_n failed\n");
986 /* we do this first so that we can see the page in the /proc
988 llap = llap_from_page(page, LLAP_ORIGIN_READAHEAD);
989 if (IS_ERR(llap) || llap->llap_defer_uptodate)
992 /* skip completed pages */
993 if (Page_Uptodate(page))
996 /* bail when we hit the end of the lock. */
997 if ((rc = ll_page_matches(page, 1)) <= 0) {
998 LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
999 "lock match failed: rc %d\n", rc);
1000 ll_ra_stats_inc(mapping, RA_STAT_FAILED_MATCH);
1005 rc = ll_issue_page_read(exp, llap, oig, 1);
1009 LL_CDEBUG_PAGE(D_READA| D_PAGE, page,
1010 "started read-ahead\n");
1014 LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
1015 "skipping read-ahead\n");
1019 page_cache_release(page);
1022 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
1024 ll_ra_count_put(ll_i2sbi(mapping->host), reserved);
1025 if (i == end + 1 && end == (kms >> PAGE_CACHE_SHIFT))
1026 ll_ra_stats_inc(mapping, RA_STAT_EOF);
1028 /* if we didn't get to the end of the region we reserved from
1029 * the ras we need to go back and update the ras so that the
1030 * next read-ahead tries from where we left off. we only do so
1031 * if the region we failed to issue read-ahead on is still ahead
1032 * of the app and behind the next index to start read-ahead from */
1034 spin_lock(&ras->ras_lock);
1035 if (i < ras->ras_next_readahead &&
1036 index_in_window(i, ras->ras_window_start, 0,
1037 ras->ras_window_len)) {
1038 ras->ras_next_readahead = i;
1041 spin_unlock(&ras->ras_lock);
1047 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
1049 ras->ras_window_start = index & (~(PTLRPC_MAX_BRW_PAGES - 1));
1052 /* called with the ras_lock held or from places where it doesn't matter */
1053 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
1055 ras->ras_last_readpage = index;
1056 ras->ras_consecutive = 1;
1057 ras->ras_window_len = 0;
1058 ras_set_start(ras, index);
1059 ras->ras_next_readahead = ras->ras_window_start;
1064 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
1066 spin_lock_init(&ras->ras_lock);
1070 static void ras_update(struct ll_sb_info *sbi, struct ll_readahead_state *ras,
1071 unsigned long index, unsigned hit)
1073 struct ll_ra_info *ra = &sbi->ll_ra_info;
1077 spin_lock(&sbi->ll_lock);
1078 spin_lock(&ras->ras_lock);
1080 ll_ra_stats_inc_unlocked(ra, hit ? RA_STAT_HIT : RA_STAT_MISS);
1082 /* reset the read-ahead window in two cases. First when the app seeks
1083 * or reads to some other part of the file. Secondly if we get a
1084 * read-ahead miss that we think we've previously issued. This can
1085 * be a symptom of there being so many read-ahead pages that the VM is
1086 * reclaiming it before we get to it. */
1087 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1089 ll_ra_stats_inc_unlocked(ra, RA_STAT_DISTANT_READPAGE);
1090 } else if (!hit && ras->ras_window_len &&
1091 index < ras->ras_next_readahead &&
1092 index_in_window(index, ras->ras_window_start, 0,
1093 ras->ras_window_len)) {
1095 ll_ra_stats_inc_unlocked(ra, RA_STAT_MISS_IN_WINDOW);
1099 ras_reset(ras, index);
1100 GOTO(out_unlock, 0);
1103 ras->ras_last_readpage = index;
1104 ras->ras_consecutive++;
1105 ras_set_start(ras, index);
1106 ras->ras_next_readahead = max(ras->ras_window_start,
1107 ras->ras_next_readahead);
1109 /* wait for a few pages to arrive before issuing readahead to avoid
1110 * the worst overutilization */
1111 if (ras->ras_consecutive == 3) {
1112 ras->ras_window_len = PTLRPC_MAX_BRW_PAGES;
1113 GOTO(out_unlock, 0);
1116 /* we need to increase the window sometimes. we'll arbitrarily
1117 * do it half-way through the pages in an rpc */
1118 if ((index & (PTLRPC_MAX_BRW_PAGES - 1)) ==
1119 (PTLRPC_MAX_BRW_PAGES >> 1)) {
1120 ras->ras_window_len += PTLRPC_MAX_BRW_PAGES;
1121 ras->ras_window_len = min(ras->ras_window_len,
1128 spin_unlock(&ras->ras_lock);
1129 spin_unlock(&sbi->ll_lock);
1133 int ll_writepage(struct page *page)
1135 struct inode *inode = page->mapping->host;
1136 struct ll_inode_info *lli = ll_i2info(inode);
1137 struct obd_export *exp;
1138 struct ll_async_page *llap;
1142 LASSERT(!PageDirty(page));
1143 LASSERT(PageLocked(page));
1145 exp = ll_i2obdexp(inode);
1147 GOTO(out, rc = -EINVAL);
1149 llap = llap_from_page(page, LLAP_ORIGIN_WRITEPAGE);
1151 GOTO(out, rc = PTR_ERR(llap));
1153 page_cache_get(page);
1154 if (llap->llap_write_queued) {
1155 LL_CDEBUG_PAGE(D_PAGE, page, "marking urgent\n");
1156 rc = obd_set_async_flags(exp, lli->lli_smd, NULL,
1158 ASYNC_READY | ASYNC_URGENT);
1160 rc = queue_or_sync_write(exp, inode, llap,
1161 PAGE_SIZE, ASYNC_READY | ASYNC_URGENT);
1164 page_cache_release(page);
1167 if (!lli->lli_async_rc)
1168 lli->lli_async_rc = rc;
1169 /* re-dirty page on error so it retries write */
1170 ll_redirty_page(page);
1177 * for now we do our readpage the same on both 2.4 and 2.5. The kernel's
1178 * read-ahead assumes it is valid to issue readpage all the way up to
1179 * i_size, but our dlm locks make that not the case. We disable the
1180 * kernel's read-ahead and do our own by walking ahead in the page cache
1181 * checking for dlm lock coverage. the main difference between 2.4 and
1182 * 2.6 is how read-ahead gets batched and issued, but we're using our own,
1183 * so they look the same.
1185 int ll_readpage(struct file *filp, struct page *page)
1187 struct ll_file_data *fd = filp->private_data;
1188 struct inode *inode = page->mapping->host;
1189 struct obd_export *exp;
1190 struct ll_async_page *llap;
1191 struct obd_io_group *oig = NULL;
1195 LASSERT(PageLocked(page));
1196 LASSERT(!PageUptodate(page));
1197 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset="LPX64"\n",
1198 inode->i_ino, inode->i_generation, inode,
1199 (((obd_off)page->index) << PAGE_SHIFT));
1200 LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
1202 rc = oig_init(&oig);
1206 exp = ll_i2obdexp(inode);
1208 GOTO(out, rc = -EINVAL);
1210 llap = llap_from_page(page, LLAP_ORIGIN_READPAGE);
1212 GOTO(out, rc = PTR_ERR(llap));
1214 if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1215 ras_update(ll_i2sbi(inode), &fd->fd_ras, page->index,
1216 llap->llap_defer_uptodate);
1218 if (llap->llap_defer_uptodate) {
1219 llap->llap_ra_used = 1;
1220 rc = ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1223 obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd,
1225 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
1226 SetPageUptodate(page);
1228 GOTO(out_oig, rc = 0);
1231 rc = ll_page_matches(page, 0);
1233 LL_CDEBUG_PAGE(D_ERROR, page, "lock match failed: rc %d\n", rc);
1238 CWARN("ino %lu page %lu (%llu) not covered by "
1239 "a lock (mmap?). check debug logs.\n",
1240 inode->i_ino, page->index,
1241 (long long)page->index << PAGE_CACHE_SHIFT);
1244 rc = ll_issue_page_read(exp, llap, oig, 0);
1248 LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
1249 if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1250 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1253 rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);