1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Lustre Lite I/O page cache routines shared by different kernel revs
41 #ifndef AUTOCONF_INCLUDED
42 #include <linux/config.h>
44 #include <linux/kernel.h>
46 #include <linux/string.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #include <linux/smp_lock.h>
50 #include <linux/unistd.h>
51 #include <linux/version.h>
52 #include <asm/system.h>
53 #include <asm/uaccess.h>
56 #include <linux/stat.h>
57 #include <asm/uaccess.h>
59 #include <linux/pagemap.h>
60 #include <linux/smp_lock.h>
62 #define DEBUG_SUBSYSTEM S_LLITE
64 #include <linux/page-flags.h>
66 #include <lustre_lite.h>
67 #include "llite_internal.h"
68 #include <linux/lustre_compat25.h>
70 #ifndef list_for_each_prev_safe
71 #define list_for_each_prev_safe(pos, n, head) \
72 for (pos = (head)->prev, n = pos->prev; pos != (head); \
73 pos = n, n = pos->prev )
76 cfs_mem_cache_t *ll_async_page_slab = NULL;
77 size_t ll_async_page_slab_size = 0;
79 /* SYNCHRONOUS I/O to object storage for an inode */
80 static int ll_brw(int cmd, struct inode *inode, struct obdo *oa,
81 struct page *page, int flags)
83 struct ll_inode_info *lli = ll_i2info(inode);
84 struct lov_stripe_md *lsm = lli->lli_smd;
85 struct obd_info oinfo = { { { 0 } } };
91 pg.off = ((obd_off)page->index) << CFS_PAGE_SHIFT;
93 if ((cmd & OBD_BRW_WRITE) && (pg.off+CFS_PAGE_SIZE>i_size_read(inode)))
94 pg.count = i_size_read(inode) % CFS_PAGE_SIZE;
96 pg.count = CFS_PAGE_SIZE;
98 LL_CDEBUG_PAGE(D_PAGE, page, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
99 cmd & OBD_BRW_WRITE ? "write" : "read", pg.count,
100 inode->i_ino, pg.off, pg.off);
102 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
103 LPU64"\n", inode->i_ino, inode, i_size_read(inode),
104 page->mapping->host, i_size_read(page->mapping->host),
105 page->index, pg.off);
110 if (cmd & OBD_BRW_WRITE)
111 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_WRITE,
114 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_READ,
118 rc = obd_brw(cmd, ll_i2obdexp(inode), &oinfo, 1, &pg, NULL);
120 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
122 CERROR("error from obd_brw: rc = %d\n", rc);
126 int ll_file_punch(struct inode * inode, loff_t new_size, int srvlock)
128 struct ll_inode_info *lli = ll_i2info(inode);
129 struct obd_info oinfo = { { { 0 } } };
130 struct obdo oa = { 0 };
135 CDEBUG(D_INFO, "calling punch for "LPX64" (new size %Lu=%#Lx)\n",
136 lli->lli_smd->lsm_object_id, new_size, new_size);
138 oinfo.oi_md = lli->lli_smd;
139 oinfo.oi_policy.l_extent.start = new_size;
140 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
142 oa.o_id = lli->lli_smd->lsm_object_id;
143 oa.o_gr = lli->lli_smd->lsm_object_gr;
144 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
146 valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |OBD_MD_FLFID |
147 OBD_MD_FLATIME | OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLGENER |
150 /* set OBD_MD_FLFLAGS in o_valid, only if we
151 * set OBD_FL_TRUNCLOCK, otherwise ost_punch
152 * and filter_setattr get confused, see the comment
154 oa.o_flags = OBD_FL_TRUNCLOCK;
155 oa.o_valid |= OBD_MD_FLFLAGS;
159 * 1. do not use inode's timestamps because concurrent
160 * stat might fill the inode with out-of-date times,
161 * send current instead
163 * 2.do no update lsm, as long as stat (via
164 * ll_glimpse_size) will bring attributes from osts
166 oa.o_mtime = oa.o_ctime = LTIME_S(CURRENT_TIME);
167 oa.o_valid |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
169 struct ost_lvb *xtimes;
170 /* truncate under locks
172 * 1. update inode's mtime and ctime as long as
173 * concurrent stat (via ll_glimpse_size) might bring
176 * 2. update lsm so that next stat (via
177 * ll_glimpse_size) could get correct values in lsm */
178 OBD_ALLOC_PTR(xtimes);
182 lov_stripe_lock(lli->lli_smd);
183 LTIME_S(inode->i_mtime) = LTIME_S(CURRENT_TIME);
184 LTIME_S(inode->i_ctime) = LTIME_S(CURRENT_TIME);
185 xtimes->lvb_mtime = LTIME_S(inode->i_mtime);
186 xtimes->lvb_ctime = LTIME_S(inode->i_ctime);
187 obd_update_lvb(ll_i2obdexp(inode), lli->lli_smd, xtimes,
188 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
189 lov_stripe_unlock(lli->lli_smd);
190 OBD_FREE_PTR(xtimes);
192 valid |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
194 obdo_from_inode(&oa, inode, valid);
196 rc = obd_punch_rqset(ll_i2obdexp(inode), &oinfo, NULL);
198 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
201 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
202 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
205 /* this isn't where truncate starts. roughly:
206 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate. setattr_raw grabs
207 * DLM lock on [size, EOF], i_mutex, ->lli_size_sem, and WRITE_I_ALLOC_SEM to
210 * must be called under ->lli_size_sem */
211 void ll_truncate(struct inode *inode)
213 struct ll_inode_info *lli = ll_i2info(inode);
214 int srvlock = test_bit(LLI_F_SRVLOCK, &lli->lli_flags);
217 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %Lu=%#Lx\n",
218 inode->i_ino, inode->i_generation, inode, i_size_read(inode),
221 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_TRUNC, 1);
222 if (lli->lli_size_sem_owner != current) {
228 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
233 LASSERT(SEM_COUNT(&lli->lli_size_sem) <= 0);
239 /* XXX I'm pretty sure this is a hack to paper over a more
240 * fundamental race condition. */
241 lov_stripe_lock(lli->lli_smd);
242 inode_init_lvb(inode, &lvb);
243 rc = obd_merge_lvb(ll_i2obdexp(inode), lli->lli_smd, &lvb, 0);
244 inode->i_blocks = lvb.lvb_blocks;
245 if (lvb.lvb_size == i_size_read(inode) && rc == 0) {
246 CDEBUG(D_VFSTRACE, "skipping punch for obj "LPX64
248 lli->lli_smd->lsm_object_id, i_size_read(inode),
250 lov_stripe_unlock(lli->lli_smd);
254 obd_adjust_kms(ll_i2obdexp(inode), lli->lli_smd,
255 i_size_read(inode), 1);
256 lov_stripe_unlock(lli->lli_smd);
259 if (unlikely((ll_i2sbi(inode)->ll_flags & LL_SBI_LLITE_CHECKSUM) &&
260 (i_size_read(inode) & ~CFS_PAGE_MASK))) {
261 /* If the truncate leaves a partial page, update its checksum */
262 struct page *page = find_get_page(inode->i_mapping,
263 i_size_read(inode) >>
266 struct ll_async_page *llap = llap_cast_private(page);
268 char *kaddr = kmap_atomic(page, KM_USER0);
269 llap->llap_checksum =
270 init_checksum(OSC_DEFAULT_CKSUM);
271 llap->llap_checksum =
272 compute_checksum(llap->llap_checksum,
273 kaddr, CFS_PAGE_SIZE,
275 kunmap_atomic(kaddr, KM_USER0);
277 page_cache_release(page);
281 new_size = i_size_read(inode);
282 ll_inode_size_unlock(inode, 0);
284 ll_file_punch(inode, new_size, 0);
286 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LOCKLESS_TRUNC, 1);
292 ll_inode_size_unlock(inode, 0);
295 int ll_prepare_write(struct file *file, struct page *page, unsigned from,
298 struct inode *inode = page->mapping->host;
299 struct ll_inode_info *lli = ll_i2info(inode);
300 struct lov_stripe_md *lsm = lli->lli_smd;
301 obd_off offset = ((obd_off)page->index) << CFS_PAGE_SHIFT;
302 struct obd_info oinfo = { { { 0 } } };
304 struct obdo oa = { 0 };
309 LASSERT(PageLocked(page));
310 (void)llap_cast_private(page); /* assertion */
312 /* Check to see if we should return -EIO right away */
315 pga.count = CFS_PAGE_SIZE;
318 oa.o_mode = inode->i_mode;
319 oa.o_id = lsm->lsm_object_id;
320 oa.o_gr = lsm->lsm_object_gr;
321 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE |
322 OBD_MD_FLTYPE | OBD_MD_FLGROUP;
323 obdo_from_inode(&oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
327 rc = obd_brw(OBD_BRW_CHECK, ll_i2obdexp(inode), &oinfo, 1, &pga, NULL);
331 if (PageUptodate(page)) {
332 LL_CDEBUG_PAGE(D_PAGE, page, "uptodate\n");
336 /* We're completely overwriting an existing page, so _don't_ set it up
337 * to date until commit_write */
338 if (from == 0 && to == CFS_PAGE_SIZE) {
339 LL_CDEBUG_PAGE(D_PAGE, page, "full page write\n");
340 POISON_PAGE(page, 0x11);
344 /* If are writing to a new page, no need to read old data. The extent
345 * locking will have updated the KMS, and for our purposes here we can
346 * treat it like i_size. */
347 lov_stripe_lock(lsm);
348 inode_init_lvb(inode, &lvb);
349 obd_merge_lvb(ll_i2obdexp(inode), lsm, &lvb, 1);
350 lov_stripe_unlock(lsm);
351 if (lvb.lvb_size <= offset) {
352 char *kaddr = kmap_atomic(page, KM_USER0);
353 LL_CDEBUG_PAGE(D_PAGE, page, "kms "LPU64" <= offset "LPU64"\n",
354 lvb.lvb_size, offset);
355 memset(kaddr, 0, CFS_PAGE_SIZE);
356 kunmap_atomic(kaddr, KM_USER0);
357 GOTO(prepare_done, rc = 0);
360 /* XXX could be an async ocp read.. read-ahead? */
361 rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
363 /* bug 1598: don't clobber blksize */
364 oa.o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
365 obdo_refresh_inode(inode, &oa, oa.o_valid);
371 SetPageUptodate(page);
377 * make page ready for ASYNC write
378 * \param data - pointer to llap cookie
379 * \param cmd - is OBD_BRW_* macroses
381 * \retval 0 is page successfully prepared to send
382 * \retval -EAGAIN is page not need to send
384 static int ll_ap_make_ready(void *data, int cmd)
386 struct ll_async_page *llap;
390 llap = LLAP_FROM_COOKIE(data);
391 page = llap->llap_page;
393 /* we're trying to write, but the page is locked.. come back later */
394 if (TryLockPage(page))
397 LASSERTF(!(cmd & OBD_BRW_READ) || !PageWriteback(page),
398 "cmd %x page %p ino %lu index %lu fl %lx\n", cmd, page,
399 page->mapping->host->i_ino, page->index, (long)page->flags);
401 /* if we left PageDirty we might get another writepage call
402 * in the future. list walkers are bright enough
403 * to check page dirty so we can leave it on whatever list
404 * its on. XXX also, we're called with the cli list so if
405 * we got the page cache list we'd create a lock inversion
406 * with the removepage path which gets the page lock then the
408 if(!clear_page_dirty_for_io(page)) {
413 /* This actually clears the dirty bit in the radix tree.*/
414 set_page_writeback(page);
416 LL_CDEBUG_PAGE(D_PAGE, page, "made ready\n");
417 page_cache_get(page);
422 /* We have two reasons for giving llite the opportunity to change the
423 * write length of a given queued page as it builds the RPC containing
426 * 1) Further extending writes may have landed in the page cache
427 * since a partial write first queued this page requiring us
428 * to write more from the page cache. (No further races are possible,
429 * since by the time this is called, the page is locked.)
430 * 2) We might have raced with truncate and want to avoid performing
431 * write RPCs that are just going to be thrown away by the
432 * truncate's punch on the storage targets.
434 * The kms serves these purposes as it is set at both truncate and extending
437 static int ll_ap_refresh_count(void *data, int cmd)
439 struct ll_inode_info *lli;
440 struct ll_async_page *llap;
441 struct lov_stripe_md *lsm;
448 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
449 LASSERT(cmd != OBD_BRW_READ);
451 llap = LLAP_FROM_COOKIE(data);
452 page = llap->llap_page;
453 inode = page->mapping->host;
454 lli = ll_i2info(inode);
457 lov_stripe_lock(lsm);
458 inode_init_lvb(inode, &lvb);
459 obd_merge_lvb(ll_i2obdexp(inode), lsm, &lvb, 1);
461 lov_stripe_unlock(lsm);
463 /* catch race with truncate */
464 if (((__u64)page->index << CFS_PAGE_SHIFT) >= kms)
467 /* catch sub-page write at end of file */
468 if (((__u64)page->index << CFS_PAGE_SHIFT) + CFS_PAGE_SIZE > kms)
469 return kms % CFS_PAGE_SIZE;
471 return CFS_PAGE_SIZE;
474 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
476 struct lov_stripe_md *lsm;
477 obd_flag valid_flags;
479 lsm = ll_i2info(inode)->lli_smd;
481 oa->o_id = lsm->lsm_object_id;
482 oa->o_gr = lsm->lsm_object_gr;
483 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
484 valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
485 if (cmd & OBD_BRW_WRITE) {
486 oa->o_valid |= OBD_MD_FLEPOCH;
487 oa->o_easize = ll_i2info(inode)->lli_io_epoch;
489 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
490 OBD_MD_FLUID | OBD_MD_FLGID |
491 OBD_MD_FLFID | OBD_MD_FLGENER;
494 obdo_from_inode(oa, inode, valid_flags);
495 /* Bug11742 - set the OBD_FL_MMAP flag for memory mapped files */
496 if (atomic_read(&(ll_i2info(inode)->lli_mmap_cnt)) != 0)
497 oa->o_flags |= OBD_FL_MMAP;
500 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
502 struct ll_async_page *llap;
505 llap = LLAP_FROM_COOKIE(data);
506 ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
511 static void ll_ap_update_obdo(void *data, int cmd, struct obdo *oa,
514 struct ll_async_page *llap;
517 llap = LLAP_FROM_COOKIE(data);
518 obdo_from_inode(oa, llap->llap_page->mapping->host, valid);
523 static struct obd_async_page_ops ll_async_page_ops = {
524 .ap_make_ready = ll_ap_make_ready,
525 .ap_refresh_count = ll_ap_refresh_count,
526 .ap_fill_obdo = ll_ap_fill_obdo,
527 .ap_update_obdo = ll_ap_update_obdo,
528 .ap_completion = ll_ap_completion,
531 struct ll_async_page *llap_cast_private(struct page *page)
533 struct ll_async_page *llap = (struct ll_async_page *)page_private(page);
535 LASSERTF(llap == NULL || llap->llap_magic == LLAP_MAGIC,
536 "page %p private %lu gave magic %d which != %d\n",
537 page, page_private(page), llap->llap_magic, LLAP_MAGIC);
542 /* Try to reap @target pages in the specific @cpu's async page list.
544 * There is an llap attached onto every page in lustre, linked off @sbi.
545 * We add an llap to the list so we don't lose our place during list walking.
546 * If llaps in the list are being moved they will only move to the end
547 * of the LRU, and we aren't terribly interested in those pages here (we
548 * start at the beginning of the list where the least-used llaps are. */
549 static inline int llap_shrink_cache_internal(struct ll_sb_info *sbi,
552 struct ll_async_page *llap, dummy_llap = { .llap_magic = 0xd11ad11a };
553 struct ll_pglist_data *pd;
554 struct list_head *head;
557 pd = ll_pglist_cpu_lock(sbi, cpu);
558 head = &pd->llpd_list;
559 list_add(&dummy_llap.llap_pglist_item, head);
560 while (count < target) {
564 if (unlikely(need_resched())) {
565 list_del(&dummy_llap.llap_pglist_item);
566 ll_pglist_cpu_unlock(sbi, cpu);
567 /* vmscan::shrink_slab() have own schedule() */
571 llap = llite_pglist_next_llap(head,
572 &dummy_llap.llap_pglist_item);
573 list_del_init(&dummy_llap.llap_pglist_item);
577 page = llap->llap_page;
578 LASSERT(page != NULL);
580 list_add(&dummy_llap.llap_pglist_item, &llap->llap_pglist_item);
582 /* Page needs/undergoing IO */
583 if (TryLockPage(page)) {
584 LL_CDEBUG_PAGE(D_PAGE, page, "can't lock\n");
588 keep = (llap->llap_write_queued || PageDirty(page) ||
589 PageWriteback(page) || (!PageUptodate(page) &&
590 llap->llap_origin != LLAP_ORIGIN_READAHEAD));
592 LL_CDEBUG_PAGE(D_PAGE, page,
593 "%s LRU page: %s%s%s%s%s origin %s\n",
594 keep ? "keep" : "drop",
595 llap->llap_write_queued ? "wq " : "",
596 PageDirty(page) ? "pd " : "",
597 PageUptodate(page) ? "" : "!pu ",
598 PageWriteback(page) ? "wb" : "",
599 llap->llap_defer_uptodate ? "" : "!du",
600 llap_origins[llap->llap_origin]);
602 /* If page is dirty or undergoing IO don't discard it */
608 page_cache_get(page);
609 ll_pglist_cpu_unlock(sbi, cpu);
611 if (page->mapping != NULL) {
612 ll_teardown_mmaps(page->mapping,
613 (__u64)page->index << CFS_PAGE_SHIFT,
614 ((__u64)page->index << CFS_PAGE_SHIFT)|
616 if (!PageDirty(page) && !page_mapped(page)) {
617 ll_ra_accounting(llap, page->mapping);
618 ll_truncate_complete_page(page);
621 LL_CDEBUG_PAGE(D_PAGE, page,
622 "Not dropping page because it is"
623 " %s\n", PageDirty(page) ?
628 page_cache_release(page);
630 ll_pglist_cpu_lock(sbi, cpu);
632 list_del(&dummy_llap.llap_pglist_item);
633 ll_pglist_cpu_unlock(sbi, cpu);
635 CDEBUG(D_CACHE, "shrank %d, expected %d however. \n", count, target);
640 /* Try to shrink the page cache for the @sbi filesystem by 1/@shrink_fraction.
642 * At first, this code calculates total pages wanted by @shrink_fraction, then
643 * it deduces how many pages should be reaped from each cpu in proportion as
644 * their own # of page count(llpd_count).
646 int llap_shrink_cache(struct ll_sb_info *sbi, int nr_to_scan)
648 unsigned long total, want, percpu_want, count = 0;
651 total = lcounter_read_positive(&sbi->ll_async_page_count);
652 if (total == 0 || nr_to_scan == 0)
657 /* Since we are freeing pages also, we don't necessarily want to
658 * shrink so much. Limit to 40MB of pages + llaps per call. */
659 if (want > 40 << (20 - CFS_PAGE_SHIFT))
660 want = 40 << (20 - CFS_PAGE_SHIFT);
662 CDEBUG(D_CACHE, "shrinking %lu of %lu pages (asked for %u)\n",
663 want, total, nr_to_scan);
665 nr_cpus = num_possible_cpus();
666 cpu = sbi->ll_async_page_clock_hand;
667 /* we at most do one round */
671 cpu = (cpu + 1) % nr_cpus;
672 c = LL_PGLIST_DATA_CPU(sbi, cpu)->llpd_count;
673 if (!cpu_online(cpu))
676 percpu_want = want / ((total / (c + 1)) + 1);
677 if (percpu_want == 0)
680 count += llap_shrink_cache_internal(sbi, cpu, percpu_want);
682 sbi->ll_async_page_clock_hand = cpu;
683 } while (cpu != sbi->ll_async_page_clock_hand);
685 CDEBUG(D_CACHE, "shrank %lu/%lu and left %lu unscanned\n",
688 return lcounter_read_positive(&sbi->ll_async_page_count);
691 /* Rebalance the async page queue len for each cpu. We hope that the cpu
692 * which do much IO job has a relative longer queue len.
693 * This function should be called with preempt disabled.
695 static inline int llap_async_cache_rebalance(struct ll_sb_info *sbi)
697 unsigned long sample = 0, *cpu_sample, bias, slice;
698 struct ll_pglist_data *pd;
701 int w1 = 7, w2 = 3, base = (w1 + w2); /* weight value */
704 if (!spin_trylock(&sbi->ll_async_page_reblnc_lock)) {
705 /* someone else is doing the job */
709 pcnt = &LL_PGLIST_DATA(sbi)->llpd_sample_count;
710 if (!atomic_read(pcnt)) {
711 /* rare case, somebody else has gotten this job done */
712 spin_unlock(&sbi->ll_async_page_reblnc_lock);
716 sbi->ll_async_page_reblnc_count++;
717 cpu_sample = sbi->ll_async_page_sample;
718 memset(cpu_sample, 0, num_possible_cpus() * sizeof(unsigned long));
719 for_each_online_cpu(cpu) {
720 pcnt = &LL_PGLIST_DATA_CPU(sbi, cpu)->llpd_sample_count;
721 cpu_sample[cpu] = atomic_read(pcnt);
723 sample += cpu_sample[cpu];
727 surplus = sbi->ll_async_page_max;
728 slice = surplus / sample + 1;
729 sample /= num_online_cpus();
731 for_each_online_cpu(cpu) {
732 pd = LL_PGLIST_DATA_CPU(sbi, cpu);
733 if (labs((long int)sample - cpu_sample[cpu]) > bias) {
734 unsigned long budget = pd->llpd_budget;
735 /* weighted original queue length and expected queue
736 * length to avoid thrashing. */
737 pd->llpd_budget = (budget * w1) / base +
738 (slice * cpu_sample[cpu]) * w2 / base;
741 surplus -= min_t(int, pd->llpd_budget, surplus);
743 surplus /= cpus_weight(mask) ?: 1;
744 for_each_cpu_mask(cpu, mask)
745 LL_PGLIST_DATA_CPU(sbi, cpu)->llpd_budget += surplus;
746 spin_unlock(&sbi->ll_async_page_reblnc_lock);
748 /* We need to call llap_shrink_cache_internal() for every cpu to
749 * ensure the sbi->ll_async_page_max limit is enforced. */
750 for_each_cpu_mask(cpu, mask) {
751 pd = LL_PGLIST_DATA_CPU(sbi, cpu);
752 llap_shrink_cache_internal(sbi, cpu, max_t(int, pd->llpd_count-
753 pd->llpd_budget, 0) + 32);
759 static struct ll_async_page *llap_from_page_with_lockh(struct page *page,
761 struct lustre_handle *lockh,
764 struct ll_async_page *llap;
765 struct obd_export *exp;
766 struct inode *inode = page->mapping->host;
767 struct ll_sb_info *sbi;
768 struct ll_pglist_data *pd;
773 static int triggered;
776 LL_CDEBUG_PAGE(D_ERROR, page, "Bug 10047. Wrong anon "
778 libcfs_debug_dumpstack(NULL);
781 RETURN(ERR_PTR(-EINVAL));
783 sbi = ll_i2sbi(inode);
784 LASSERT(ll_async_page_slab);
785 LASSERTF(origin < LLAP__ORIGIN_MAX, "%u\n", origin);
787 exp = ll_i2obdexp(page->mapping->host);
789 RETURN(ERR_PTR(-EINVAL));
791 llap = llap_cast_private(page);
793 #if 0 /* disabled since we take lock ref in readahead, see bug 16774/21252 */
794 if (origin == LLAP_ORIGIN_READAHEAD && lockh) {
795 /* the page could belong to another lock for which
796 * we don't hold a reference. We need to check that
797 * a reference is taken on a lock covering this page.
798 * For readpage origin, this is fine because
799 * ll_file_readv() took a reference on lock(s) covering
800 * the whole read. However, for readhead, we don't have
801 * this guarantee, so we need to check that the lock
802 * matched in ll_file_readv() also covers this page */
803 __u64 offset = ((loff_t)page->index) << CFS_PAGE_SHIFT;
804 if (!obd_get_lock(exp, ll_i2info(inode)->lli_smd,
805 &llap->llap_cookie, OBD_BRW_READ,
806 offset, offset + CFS_PAGE_SIZE - 1,
808 RETURN(ERR_PTR(-ENOLCK));
811 /* move to end of LRU list, except when page is just about to
813 if (origin != LLAP_ORIGIN_REMOVEPAGE) {
814 int old_cpu = llap->llap_pglist_cpu;
815 struct ll_pglist_data *old_pd;
817 pd = ll_pglist_double_lock(sbi, old_cpu, &old_pd);
819 while (old_cpu != llap->llap_pglist_cpu) {
820 /* rarely case, someone else is touching this
822 ll_pglist_double_unlock(sbi, old_cpu);
823 old_cpu = llap->llap_pglist_cpu;
824 pd=ll_pglist_double_lock(sbi, old_cpu, &old_pd);
827 list_move(&llap->llap_pglist_item,
830 if (pd->llpd_cpu != old_cpu) {
832 old_pd->llpd_count--;
834 llap->llap_pglist_cpu = pd->llpd_cpu;
837 ll_pglist_double_unlock(sbi, old_cpu);
842 /* limit the number of lustre-cached pages */
844 pd = LL_PGLIST_DATA(sbi);
845 target = pd->llpd_count - pd->llpd_budget;
848 atomic_inc(&pd->llpd_sample_count);
849 if (atomic_read(&pd->llpd_sample_count) >
850 sbi->ll_async_page_sample_max) {
851 pd->llpd_reblnc_count++;
852 rc = llap_async_cache_rebalance(sbi);
854 target = pd->llpd_count - pd->llpd_budget;
856 /* if rc equals 1, it means other cpu is doing the rebalance
857 * job, and our budget # would be modified when we read it.
858 * Furthermore, it is much likely being increased because
859 * we have already reached the rebalance threshold. In this
860 * case, we skip to shrink cache here. */
861 if ((rc == 0) && target > 0)
862 llap_shrink_cache_internal(sbi, cpu, target + 32);
866 OBD_SLAB_ALLOC(llap, ll_async_page_slab, CFS_ALLOC_IO,
867 ll_async_page_slab_size);
869 RETURN(ERR_PTR(-ENOMEM));
870 llap->llap_magic = LLAP_MAGIC;
871 llap->llap_cookie = (void *)llap + size_round(sizeof(*llap));
873 rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
874 (obd_off)page->index << CFS_PAGE_SHIFT,
875 &ll_async_page_ops, llap, &llap->llap_cookie,
878 OBD_SLAB_FREE(llap, ll_async_page_slab,
879 ll_async_page_slab_size);
883 CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
884 page, llap->llap_cookie, (obd_off)page->index << CFS_PAGE_SHIFT);
885 /* also zeroing the PRIVBITS low order bitflags */
886 __set_page_ll_data(page, llap);
887 llap->llap_page = page;
889 lcounter_inc(&sbi->ll_async_page_count);
890 pd = ll_pglist_lock(sbi);
891 list_add_tail(&llap->llap_pglist_item, &pd->llpd_list);
895 llap->llap_pglist_cpu = pd->llpd_cpu;
896 ll_pglist_unlock(sbi);
899 if (unlikely(sbi->ll_flags & LL_SBI_LLITE_CHECKSUM)) {
901 char *kaddr = kmap_atomic(page, KM_USER0);
902 csum = init_checksum(OSC_DEFAULT_CKSUM);
903 csum = compute_checksum(csum, kaddr, CFS_PAGE_SIZE,
905 kunmap_atomic(kaddr, KM_USER0);
906 if (origin == LLAP_ORIGIN_READAHEAD ||
907 origin == LLAP_ORIGIN_READPAGE) {
908 llap->llap_checksum = 0;
909 } else if (origin == LLAP_ORIGIN_COMMIT_WRITE ||
910 llap->llap_checksum == 0) {
911 llap->llap_checksum = csum;
912 CDEBUG(D_PAGE, "page %p cksum %x\n", page, csum);
913 } else if (llap->llap_checksum == csum) {
914 /* origin == LLAP_ORIGIN_WRITEPAGE */
915 CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
918 /* origin == LLAP_ORIGIN_WRITEPAGE */
919 if (!atomic_read(&(ll_i2info(inode)->lli_mmap_cnt))) {
920 LL_CDEBUG_PAGE(D_ERROR, page,
921 "old cksum %x != new %x!\n",
922 llap->llap_checksum, csum);
924 /* mmapped page was modified */
926 "page %p old cksum %x != new %x\n",
927 page, llap->llap_checksum, csum);
929 llap->llap_checksum = csum;
933 llap->llap_origin = origin;
937 static inline struct ll_async_page *llap_from_page(struct page *page,
940 return llap_from_page_with_lockh(page, origin, NULL, 0);
943 static int queue_or_sync_write(struct obd_export *exp, struct inode *inode,
944 struct ll_async_page *llap,
945 unsigned to, obd_flag async_flags)
947 unsigned long size_index = i_size_read(inode) >> CFS_PAGE_SHIFT;
948 struct obd_io_group *oig;
949 struct ll_sb_info *sbi = ll_i2sbi(inode);
950 int rc, noquot = llap->llap_ignore_quota ? OBD_BRW_NOQUOTA : 0;
951 int brwflags = OBD_BRW_ASYNC;
954 /* _make_ready only sees llap once we've unlocked the page */
955 llap->llap_write_queued = 1;
956 rc = obd_queue_async_io(exp, ll_i2info(inode)->lli_smd, NULL,
957 llap->llap_cookie, OBD_BRW_WRITE | noquot,
958 0, 0, brwflags, async_flags);
960 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "write queued\n");
961 llap_write_pending(inode, llap);
965 llap->llap_write_queued = 0;
971 /* make full-page requests if we are not at EOF (bug 4410) */
972 if (to != CFS_PAGE_SIZE && llap->llap_page->index < size_index) {
973 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
974 "sync write before EOF: size_index %lu, to %d\n",
977 } else if (to != CFS_PAGE_SIZE && llap->llap_page->index == size_index){
978 int size_to = i_size_read(inode) & ~CFS_PAGE_MASK;
979 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
980 "sync write at EOF: size_index %lu, to %d/%d\n",
981 size_index, to, size_to);
986 /* compare the checksum once before the page leaves llite */
987 if (unlikely((sbi->ll_flags & LL_SBI_LLITE_CHECKSUM) &&
988 llap->llap_checksum != 0)) {
990 struct page *page = llap->llap_page;
991 char *kaddr = kmap_atomic(page, KM_USER0);
992 csum = init_checksum(OSC_DEFAULT_CKSUM);
993 csum = compute_checksum(csum, kaddr, CFS_PAGE_SIZE,
995 kunmap_atomic(kaddr, KM_USER0);
996 if (llap->llap_checksum == csum) {
997 CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
1000 CERROR("page %p old cksum %x != new cksum %x!\n",
1001 page, llap->llap_checksum, csum);
1005 rc = obd_queue_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig,
1006 llap->llap_cookie, OBD_BRW_WRITE | noquot,
1007 0, to, 0, ASYNC_READY | ASYNC_URGENT |
1008 ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
1012 rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
1018 if (!rc && async_flags & ASYNC_READY) {
1019 unlock_page(llap->llap_page);
1020 if (PageWriteback(llap->llap_page))
1021 end_page_writeback(llap->llap_page);
1024 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "sync write returned %d\n", rc);
1032 /* update our write count to account for i_size increases that may have
1033 * happened since we've queued the page for io. */
1035 /* be careful not to return success without setting the page Uptodate or
1036 * the next pass through prepare_write will read in stale data from disk. */
1037 int ll_commit_write(struct file *file, struct page *page, unsigned from,
1040 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1041 struct inode *inode = page->mapping->host;
1042 struct ll_inode_info *lli = ll_i2info(inode);
1043 struct lov_stripe_md *lsm = lli->lli_smd;
1044 struct obd_export *exp;
1045 struct ll_async_page *llap;
1047 struct lustre_handle *lockh = NULL;
1051 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1052 LASSERT(inode == file->f_dentry->d_inode);
1053 LASSERT(PageLocked(page));
1055 CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
1056 inode, page, from, to, page->index);
1058 if (fd->fd_flags & LL_FILE_GROUP_LOCKED)
1059 lockh = &fd->fd_cwlockh;
1061 llap = llap_from_page_with_lockh(page, LLAP_ORIGIN_COMMIT_WRITE, lockh,
1064 RETURN(PTR_ERR(llap));
1066 exp = ll_i2obdexp(inode);
1070 llap->llap_ignore_quota = cfs_capable(CFS_CAP_SYS_RESOURCE);
1072 /* queue a write for some time in the future the first time we
1074 if (!PageDirty(page)) {
1075 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRTY_MISSES, 1);
1077 rc = queue_or_sync_write(exp, inode, llap, to, 0);
1081 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRTY_HITS, 1);
1084 /* put the page in the page cache, from now on ll_removepage is
1085 * responsible for cleaning up the llap.
1086 * only set page dirty when it's queued to be write out */
1087 if (llap->llap_write_queued)
1088 set_page_dirty(page);
1091 size = (((obd_off)page->index) << CFS_PAGE_SHIFT) + to;
1092 ll_inode_size_lock(inode, 0);
1094 lov_stripe_lock(lsm);
1095 obd_adjust_kms(exp, lsm, size, 0);
1096 lov_stripe_unlock(lsm);
1097 if (size > i_size_read(inode))
1098 i_size_write(inode, size);
1099 SetPageUptodate(page);
1100 } else if (size > i_size_read(inode)) {
1101 /* this page beyond the pales of i_size, so it can't be
1102 * truncated in ll_p_r_e during lock revoking. we must
1103 * teardown our book-keeping here. */
1104 ll_removepage(page);
1106 ll_inode_size_unlock(inode, 0);
1110 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
1112 /* WARNING: This algorithm is used to reduce the contention on
1113 * sbi->ll_lock. It should work well if the ra_max_pages is much
1114 * greater than the single file's read-ahead window.
1116 * TODO: There may exist a `global sync problem' in this implementation.
1117 * Considering the global ra window is 100M, and each file's ra window is 10M,
1118 * there are over 10 files trying to get its ra budget and reach
1119 * ll_ra_count_get at the exactly same time. All of them will get a zero ra
1120 * window, although the global window is 100M. -jay
1122 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
1124 struct ll_ra_info *ra = &sbi->ll_ra_info;
1125 unsigned long ret = 0;
1129 * If read-ahead pages left are less than 1M, do not do read-ahead,
1130 * otherwise it will form small read RPC(< 1M), which hurt server
1131 * performance a lot.
1133 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), len);
1134 if ((int)ret < min((unsigned long)PTLRPC_MAX_BRW_PAGES, len))
1137 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
1138 atomic_sub(ret, &ra->ra_cur_pages);
1145 static void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
1147 struct ll_ra_info *ra = &sbi->ll_ra_info;
1148 atomic_sub(len, &ra->ra_cur_pages);
1151 /* called for each page in a completed rpc.*/
1152 int ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
1154 struct ll_async_page *llap;
1156 struct obd_export *exp;
1161 llap = LLAP_FROM_COOKIE(data);
1162 page = llap->llap_page;
1163 LASSERT(PageLocked(page));
1164 LASSERT(CheckWriteback(page,cmd));
1166 LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
1168 if (cmd & OBD_BRW_READ && llap->llap_defer_uptodate) {
1169 ll_ra_count_put(ll_i2sbi(page->mapping->host), 1);
1171 LASSERT(lustre_handle_is_used(&llap->llap_lockh_granted));
1172 exp = ll_i2obdexp(page->mapping->host);
1173 end = ((loff_t)page->index) << CFS_PAGE_SHIFT;
1174 end += CFS_PAGE_SIZE - 1;
1175 obd_cancel(exp, ll_i2info(page->mapping->host)->lli_smd, LCK_PR,
1176 &llap->llap_lockh_granted, OBD_FAST_LOCK, end);
1180 if (cmd & OBD_BRW_READ) {
1181 if (!llap->llap_defer_uptodate)
1182 SetPageUptodate(page);
1184 llap->llap_write_queued = 0;
1186 ClearPageError(page);
1188 if (cmd & OBD_BRW_READ) {
1189 llap->llap_defer_uptodate = 0;
1193 set_bit(AS_ENOSPC, &page->mapping->flags);
1195 set_bit(AS_EIO, &page->mapping->flags);
1198 /* be carefull about clear WB.
1199 * if WB will cleared after page lock is released - paralel IO can be
1200 * started before ap_make_ready is finished - so we will be have page
1201 * with PG_Writeback set from ->writepage() and completed READ which
1202 * clear this flag */
1203 if ((cmd & OBD_BRW_WRITE) && PageWriteback(page))
1204 end_page_writeback(page);
1208 if (cmd & OBD_BRW_WRITE) {
1209 llap_write_complete(page->mapping->host, llap);
1210 ll_try_done_writing(page->mapping->host);
1213 page_cache_release(page);
1218 static void __ll_put_llap(struct page *page)
1220 struct inode *inode = page->mapping->host;
1221 struct obd_export *exp;
1222 struct ll_async_page *llap;
1223 struct ll_sb_info *sbi = ll_i2sbi(inode);
1224 struct ll_pglist_data *pd;
1228 exp = ll_i2obdexp(inode);
1230 CERROR("page %p ind %lu gave null export\n", page, page->index);
1235 llap = llap_from_page(page, LLAP_ORIGIN_REMOVEPAGE);
1237 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
1238 page->index, PTR_ERR(llap));
1243 //llap_write_complete(inode, llap);
1244 rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
1247 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
1249 /* this unconditional free is only safe because the page lock
1250 * is providing exclusivity to memory pressure/truncate/writeback..*/
1251 __clear_page_ll_data(page);
1253 lcounter_dec(&sbi->ll_async_page_count);
1254 cpu = llap->llap_pglist_cpu;
1255 pd = ll_pglist_cpu_lock(sbi, cpu);
1258 if (!list_empty(&llap->llap_pglist_item))
1259 list_del_init(&llap->llap_pglist_item);
1260 ll_pglist_cpu_unlock(sbi, cpu);
1261 OBD_SLAB_FREE(llap, ll_async_page_slab, ll_async_page_slab_size);
1266 /* the kernel calls us here when a page is unhashed from the page cache.
1267 * the page will be locked and the kernel is holding a spinlock, so
1268 * we need to be careful. we're just tearing down our book-keeping
1270 void ll_removepage(struct page *page)
1272 struct ll_async_page *llap = llap_cast_private(page);
1275 LASSERT(!in_interrupt());
1277 /* sync pages or failed read pages can leave pages in the page
1278 * cache that don't have our data associated with them anymore */
1279 if (page_private(page) == 0) {
1284 ll_ra_accounting(llap, page->mapping);
1285 LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
1286 __ll_put_llap(page);
1291 static int ll_issue_page_read(struct obd_export *exp,
1292 struct ll_async_page *llap,
1293 struct obd_io_group *oig, int defer)
1295 struct page *page = llap->llap_page;
1298 page_cache_get(page);
1299 llap->llap_defer_uptodate = defer;
1300 llap->llap_ra_used = 0;
1301 rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
1302 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
1303 CFS_PAGE_SIZE, 0, ASYNC_COUNT_STABLE |
1304 ASYNC_READY | ASYNC_URGENT);
1306 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
1307 page_cache_release(page);
1312 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
1314 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
1315 lprocfs_counter_incr(sbi->ll_ra_stats, which);
1318 static void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
1320 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
1321 ll_ra_stats_inc_sbi(sbi, which);
1324 void ll_ra_accounting(struct ll_async_page *llap, struct address_space *mapping)
1326 if (!llap->llap_defer_uptodate || llap->llap_ra_used)
1329 ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
1332 #define RAS_CDEBUG(ras) \
1334 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
1335 "csr %lu sf %lu sp %lu sl %lu \n", \
1336 ras->ras_last_readpage, ras->ras_consecutive_requests, \
1337 ras->ras_consecutive_pages, ras->ras_window_start, \
1338 ras->ras_window_len, ras->ras_next_readahead, \
1339 ras->ras_requests, ras->ras_request_index, \
1340 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
1341 ras->ras_stride_pages, ras->ras_stride_length)
1343 static int index_in_window(unsigned long index, unsigned long point,
1344 unsigned long before, unsigned long after)
1346 unsigned long start = point - before, end = point + after;
1353 return start <= index && index <= end;
1356 struct ll_thread_data *ll_td_get()
1359 struct ll_thread_data *ltd = current->journal_info;
1361 LASSERT(ltd == NULL || ltd->ltd_magic == LTD_MAGIC);
1368 void ll_td_set(struct ll_thread_data *ltd)
1372 ltd = current->journal_info;
1373 LASSERT(ltd == NULL || ltd->ltd_magic == LTD_MAGIC);
1374 current->journal_info = NULL;
1378 LASSERT(current->journal_info == NULL);
1379 LASSERT(ltd->ltd_magic == LTD_MAGIC);
1380 current->journal_info = ltd;
1384 static struct ll_readahead_state *ll_ras_get(struct file *f)
1386 struct ll_file_data *fd;
1388 fd = LUSTRE_FPRIVATE(f);
1392 void ll_ra_read_init(struct file *f, struct ll_ra_read *rar,
1393 loff_t offset, size_t count)
1395 struct ll_readahead_state *ras;
1397 ras = ll_ras_get(f);
1399 rar->lrr_start = offset >> CFS_PAGE_SHIFT;
1400 rar->lrr_count = (count + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1402 spin_lock(&ras->ras_lock);
1403 ras->ras_requests++;
1404 ras->ras_request_index = 0;
1405 ras->ras_consecutive_requests++;
1406 rar->lrr_reader = current;
1408 list_add(&rar->lrr_linkage, &ras->ras_read_beads);
1409 spin_unlock(&ras->ras_lock);
1412 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
1414 struct ll_readahead_state *ras;
1416 ras = ll_ras_get(f);
1418 spin_lock(&ras->ras_lock);
1419 list_del_init(&rar->lrr_linkage);
1420 spin_unlock(&ras->ras_lock);
1423 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
1425 struct ll_ra_read *scan;
1427 list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
1428 if (scan->lrr_reader == current)
1434 struct ll_ra_read *ll_ra_read_get(struct file *f)
1436 struct ll_readahead_state *ras;
1437 struct ll_ra_read *bead;
1439 ras = ll_ras_get(f);
1441 spin_lock(&ras->ras_lock);
1442 bead = ll_ra_read_get_locked(ras);
1443 spin_unlock(&ras->ras_lock);
1447 static int ll_read_ahead_page(struct obd_export *exp, struct obd_io_group *oig,
1448 pgoff_t index, struct address_space *mapping)
1450 struct ll_async_page *llap;
1452 unsigned int gfp_mask = 0;
1453 int rc = 0, flags = 0;
1454 struct lustre_handle lockh = { 0 };
1457 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
1459 gfp_mask |= __GFP_NOWARN;
1461 page = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
1463 ll_ra_stats_inc(mapping, RA_STAT_FAILED_GRAB_PAGE);
1464 CDEBUG(D_READA, "g_c_p_n failed\n");
1468 /* Check if page was truncated or reclaimed */
1469 if (page->mapping != mapping) {
1470 ll_ra_stats_inc(mapping, RA_STAT_WRONG_GRAB_PAGE);
1471 CDEBUG(D_READA, "g_c_p_n returned invalid page\n");
1472 GOTO(unlock_page, rc = 0);
1475 #if 0 /* the fast lock stored in ltd can't be guaranteed to be the lock used
1476 * by the llap returned by "llap_from_page_with_lockh" if there is a
1477 * ready llap, for lock check against readahead is disabled.
1478 * see bug 16774/21252 */
1481 if (ltd && ltd->lock_style > 0) {
1482 __u64 offset = ((loff_t)page->index) << CFS_PAGE_SHIFT;
1483 lockh = ltd2lockh(ltd, offset,
1484 offset + CFS_PAGE_SIZE - 1);
1485 if (ltd->lock_style == LL_LOCK_STYLE_FASTLOCK)
1486 flags = OBD_FAST_LOCK;
1490 /* we do this first so that we can see the page in the /proc
1492 llap = llap_from_page_with_lockh(page, LLAP_ORIGIN_READAHEAD, &lockh,
1494 if (IS_ERR(llap) || llap->llap_defer_uptodate) {
1495 /* bail out when we hit the end of the lock. */
1496 if (PTR_ERR(llap) == -ENOLCK) {
1497 ll_ra_stats_inc(mapping, RA_STAT_FAILED_MATCH);
1498 CDEBUG(D_READA | D_PAGE,
1499 "Adding page to cache failed index "
1501 CDEBUG(D_READA, "nolock page\n");
1502 GOTO(unlock_page, rc = -ENOLCK);
1504 CDEBUG(D_READA, "read-ahead page\n");
1505 GOTO(unlock_page, rc = 0);
1508 /* skip completed pages */
1509 if (Page_Uptodate(page))
1510 GOTO(unlock_page, rc = 0);
1512 if (!lustre_handle_is_used(&lockh)) {
1513 start = ((loff_t)index) << CFS_PAGE_SHIFT;
1514 end = start + CFS_PAGE_SIZE - 1;
1515 rc = obd_get_lock(exp,
1516 ll_i2info(mapping->host)->lli_smd,
1517 &llap->llap_cookie, OBD_BRW_READ,
1518 start, end, &lockh, OBD_FAST_LOCK);
1519 /* Is the lock being cancelling? */
1521 GOTO(unlock_page, rc = 0);
1524 llap->llap_lockh_granted = lockh;
1526 rc = ll_issue_page_read(exp, llap, oig, 1);
1528 LL_CDEBUG_PAGE(D_READA | D_PAGE, page, "started read-ahead\n");
1531 llap->llap_lockh_granted.cookie = 0;
1533 if (lustre_handle_is_used(&lockh))
1534 ldlm_lock_decref(&lockh, LCK_PR);
1537 LL_CDEBUG_PAGE(D_READA | D_PAGE, page, "skipping read-ahead\n");
1539 page_cache_release(page);
1543 /* ra_io_arg will be filled in the beginning of ll_readahead with
1544 * ras_lock, then the following ll_read_ahead_pages will read RA
1545 * pages according to this arg, all the items in this structure are
1546 * counted by page index.
1549 unsigned long ria_start; /* start offset of read-ahead*/
1550 unsigned long ria_end; /* end offset of read-ahead*/
1551 /* If stride read pattern is detected, ria_stoff means where
1552 * stride read is started. Note: for normal read-ahead, the
1553 * value here is meaningless, and also it will not be accessed*/
1555 /* ria_length and ria_pages are the length and pages length in the
1556 * stride I/O mode. And they will also be used to check whether
1557 * it is stride I/O read-ahead in the read-ahead pages*/
1558 unsigned long ria_length;
1559 unsigned long ria_pages;
1562 #define RIA_DEBUG(ria) \
1563 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
1564 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
1567 #define INIT_RAS_WINDOW_PAGES PTLRPC_MAX_BRW_PAGES
1569 static inline int stride_io_mode(struct ll_readahead_state *ras)
1571 return ras->ras_consecutive_stride_requests > 1;
1574 /* The function calculates how much pages will be read in
1575 * [off, off + length], in such stride IO area,
1576 * stride_offset = st_off, stride_lengh = st_len,
1577 * stride_pages = st_pgs
1579 * |------------------|*****|------------------|*****|------------|*****|....
1582 * |----- st_len -----|
1584 * How many pages it should read in such pattern
1585 * |-------------------------------------------------------------|
1587 * |<------ length ------->|
1589 * = |<----->| + |-------------------------------------| + |---|
1590 * start_left st_pgs * i end_left
1592 static unsigned long
1593 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
1594 unsigned long off, unsigned long length)
1596 __u64 start = off > st_off ? off - st_off : 0;
1597 __u64 end = off + length > st_off ? off + length - st_off : 0;
1598 unsigned long start_left = 0;
1599 unsigned long end_left = 0;
1600 unsigned long pg_count;
1602 if (st_len == 0 || length == 0 || end == 0)
1605 start_left = do_div(start, st_len);
1606 if (start_left < st_pgs)
1607 start_left = st_pgs - start_left;
1611 end_left = do_div(end, st_len);
1612 if (end_left > st_pgs)
1615 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
1616 start, end, start_left, end_left);
1619 pg_count = end_left - (st_pgs - start_left);
1621 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
1623 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
1624 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
1629 static int ria_page_count(struct ra_io_arg *ria)
1631 __u64 length = ria->ria_end >= ria->ria_start ?
1632 ria->ria_end - ria->ria_start + 1 : 0;
1634 return stride_pg_count(ria->ria_stoff, ria->ria_length,
1635 ria->ria_pages, ria->ria_start,
1639 /*Check whether the index is in the defined ra-window */
1640 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
1642 /* If ria_length == ria_pages, it means non-stride I/O mode,
1643 * idx should always inside read-ahead window in this case
1644 * For stride I/O mode, just check whether the idx is inside
1646 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
1647 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
1648 ria->ria_length < ria->ria_pages);
1651 static int ll_read_ahead_pages(struct obd_export *exp,
1652 struct obd_io_group *oig,
1653 struct ra_io_arg *ria,
1654 unsigned long *reserved_pages,
1655 struct address_space *mapping,
1656 unsigned long *ra_end)
1658 int rc, count = 0, stride_ria;
1659 unsigned long page_idx;
1661 LASSERT(ria != NULL);
1664 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
1665 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
1666 *reserved_pages > 0; page_idx++) {
1667 if (ras_inside_ra_window(page_idx, ria)) {
1668 /* If the page is inside the read-ahead window*/
1669 rc = ll_read_ahead_page(exp, oig, page_idx, mapping);
1671 (*reserved_pages)--;
1673 } else if (rc == -ENOLCK)
1675 } else if (stride_ria) {
1676 /* If it is not in the read-ahead window, and it is
1677 * read-ahead mode, then check whether it should skip
1680 /* FIXME: This assertion only is valid when it is for
1681 * forward read-ahead, it will be fixed when backward
1682 * read-ahead is implemented */
1683 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
1684 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
1685 ria->ria_start, ria->ria_end, ria->ria_stoff,
1686 ria->ria_length, ria->ria_pages);
1688 offset = page_idx - ria->ria_stoff;
1689 offset = offset % (ria->ria_length);
1690 if (offset > ria->ria_pages) {
1691 page_idx += ria->ria_length - offset;
1692 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
1693 ria->ria_length - offset);
1704 * Current readahead process
1707 * ll_file_readv (init ll_readahead_state for the open file)
1710 * |---> ll_readpage (read page)
1713 * | ras_update (update read-ahead window according to read pattern)
1716 * |--- ll_readahead (read_ahead pages)
1719 * During this process, ras_update controls how many ahead pages it should
1720 * read by adjusting read-ahead window(RA window).The window is represented
1721 * by following three varibles (all these values are counted by pages)
1723 * 1. ras_window_start: start offset of the read-ahead window. It is
1724 * initialized as the read offset, then as pages
1725 * are being read, it will be set as the last
1726 * page(Note: it is 1M aligned, so it actually
1727 * is last_page_index & ~index & (~(256 - 1));
1729 * 2. ras_window_len: length of the read-ahead window. The read-ahead window
1730 * length is decided by two factors
1732 * a. It is at least >= current read syscall length.
1733 * b. If continguous read is detected, (Note: it is syscall
1734 * continguous, intead of page-read contingous) the
1735 * read-ahead window len will be increased by 1M each
1737 * c. If stride read pattern is detected, the read-ahead
1738 * window will also be increased 1M but by stride pattern.
1739 * stride pattern is defined by ras_stride_length,
1740 * ras_stride_pages and ras_stride_gap. (see
1741 * ll_readahead_states comments)
1743 * 3. ras_next_readahead: current offset in the read-ahead window, i.e. where
1744 * ll_readahead will start in next next-ahead.
1747 * Cache miss: If memory load is very high, it begins to evicted the page from cache,
1748 * also includes read-ahead pages, once we found read-ahead page is being evicted before
1749 * it is "really" accessed, it will reset the read-ahead window to the current read extent
1750 * i.e. from current page to the end of this read.
1752 * In flight read-ahead amount is controlled by 2 varible (read-ahead rate)
1753 * ra_max_pages: how much max in-flight read-ahead pages on the client.
1754 * ra_max_pages_per_file: how much max in-flight read-ahead pages per file.
1759 static int ll_readahead(struct ll_readahead_state *ras,
1760 struct obd_export *exp, struct address_space *mapping,
1761 struct obd_io_group *oig, int flags)
1763 unsigned long start = 0, end = 0, reserved;
1764 unsigned long ra_end, len;
1765 struct inode *inode;
1766 struct lov_stripe_md *lsm;
1767 struct ll_ra_read *bead;
1769 struct ra_io_arg ria = { 0 };
1774 inode = mapping->host;
1775 lsm = ll_i2info(inode)->lli_smd;
1777 lov_stripe_lock(lsm);
1778 inode_init_lvb(inode, &lvb);
1779 obd_merge_lvb(ll_i2obdexp(inode), lsm, &lvb, 1);
1781 lov_stripe_unlock(lsm);
1783 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
1787 spin_lock(&ras->ras_lock);
1788 bead = ll_ra_read_get_locked(ras);
1789 /* Enlarge the RA window to encompass the full read */
1790 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
1791 bead->lrr_start + bead->lrr_count) {
1792 obd_off read_end = ((obd_off)(bead->lrr_start +
1793 bead->lrr_count))<<CFS_PAGE_SHIFT;
1794 obd_extent_calc(exp, lsm, OBD_CALC_STRIPE_RPC_END_ALIGN,
1796 ras->ras_window_len = ((read_end + 1) >> CFS_PAGE_SHIFT) -
1797 ras->ras_window_start;
1799 /* Reserve a part of the read-ahead window that we'll be issuing */
1800 if (ras->ras_window_len) {
1801 start = ras->ras_next_readahead;
1802 end = ras->ras_window_start + ras->ras_window_len - 1;
1805 /* Truncate RA window to end of file */
1806 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
1807 ras->ras_next_readahead = max(end, end + 1);
1810 ria.ria_start = start;
1812 /* If stride I/O mode is detected, get stride window*/
1813 if (stride_io_mode(ras)) {
1814 ria.ria_stoff = ras->ras_stride_offset;
1815 ria.ria_length = ras->ras_stride_length;
1816 ria.ria_pages = ras->ras_stride_pages;
1818 spin_unlock(&ras->ras_lock);
1821 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
1825 len = ria_page_count(&ria);
1829 reserved = ll_ra_count_get(ll_i2sbi(inode), len);
1831 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
1833 CDEBUG(D_READA, "reserved page %lu \n", reserved);
1835 ret = ll_read_ahead_pages(exp, oig, &ria, &reserved, mapping, &ra_end);
1837 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
1839 ll_ra_count_put(ll_i2sbi(inode), reserved);
1841 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
1842 ll_ra_stats_inc(mapping, RA_STAT_EOF);
1844 /* if we didn't get to the end of the region we reserved from
1845 * the ras we need to go back and update the ras so that the
1846 * next read-ahead tries from where we left off. we only do so
1847 * if the region we failed to issue read-ahead on is still ahead
1848 * of the app and behind the next index to start read-ahead from */
1849 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
1850 ra_end, end, ria.ria_end);
1852 if (ra_end != (end + 1)) {
1853 spin_lock(&ras->ras_lock);
1854 if (ra_end < ras->ras_next_readahead &&
1855 index_in_window(ra_end, ras->ras_window_start, 0,
1856 ras->ras_window_len)) {
1857 ras->ras_next_readahead = ra_end;
1860 spin_unlock(&ras->ras_lock);
1866 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
1868 /* Since stride readahead is sentivite to the offset
1869 * of read-ahead, so we use original offset here,
1870 * instead of ras_window_start, which is 1M aligned*/
1871 if (stride_io_mode(ras))
1872 ras->ras_window_start = index;
1874 ras->ras_window_start = index & (~(INIT_RAS_WINDOW_PAGES - 1));
1877 /* called with the ras_lock held or from places where it doesn't matter */
1878 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
1880 ras->ras_last_readpage = index;
1881 ras->ras_consecutive_requests = 0;
1882 ras->ras_consecutive_pages = 0;
1883 ras->ras_window_len = 0;
1884 ras_set_start(ras, index);
1885 ras->ras_next_readahead = max(ras->ras_window_start, index);
1890 /* called with the ras_lock held or from places where it doesn't matter */
1891 static void ras_stride_reset(struct ll_readahead_state *ras)
1893 ras->ras_consecutive_stride_requests = 0;
1894 ras->ras_stride_length = 0;
1895 ras->ras_stride_pages = 0;
1899 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
1901 spin_lock_init(&ras->ras_lock);
1903 ras->ras_requests = 0;
1904 INIT_LIST_HEAD(&ras->ras_read_beads);
1908 * Check whether the read request is in the stride window.
1909 * If it is in the stride window, return 1, otherwise return 0.
1911 static int index_in_stride_window(unsigned long index,
1912 struct ll_readahead_state *ras,
1913 struct inode *inode)
1915 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
1917 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
1918 ras->ras_stride_pages == ras->ras_stride_length)
1921 /* If it is contiguous read */
1922 if (stride_gap == 0)
1923 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
1925 /*Otherwise check the stride by itself */
1926 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
1927 ras->ras_consecutive_pages == ras->ras_stride_pages;
1930 static void ras_update_stride_detector(struct ll_readahead_state *ras,
1931 unsigned long index)
1933 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
1935 if (!stride_io_mode(ras) && (stride_gap != 0 ||
1936 ras->ras_consecutive_stride_requests == 0)) {
1937 ras->ras_stride_pages = ras->ras_consecutive_pages;
1938 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
1940 LASSERT(ras->ras_request_index == 0);
1941 LASSERT(ras->ras_consecutive_stride_requests == 0);
1943 if (index <= ras->ras_last_readpage) {
1944 /*Reset stride window for forward read*/
1945 ras_stride_reset(ras);
1949 ras->ras_stride_pages = ras->ras_consecutive_pages;
1950 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
1956 static unsigned long
1957 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
1959 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
1960 ras->ras_stride_pages, ras->ras_stride_offset,
1964 /* Stride Read-ahead window will be increased inc_len according to
1965 * the stride I/O pattern.
1967 * |------------------------|------------------------|--------------------|
1968 * ras_stride_offset ras_window_start ras_window_end
1969 * |<---ras_window_len----->|<---ras_inc_len---->|
1971 * ras_stride_offset: where the stride IO mode started,
1972 * Note: stride_page is always in front of stride_gap page, see comments in
1973 * ll_readahead_states and stride_pg_count
1975 * This function calculates how much ras_window should increase(ras_inc_len)
1976 * according to @Inc_len, Note: in stride read-ahead algorithm, stride_gap is
1977 * also acconted in the ras_window_len, so basically,
1979 * ras_inc_len = (inc_len/ stride_page) * (stride_page + stride_gap)
1982 static void ras_stride_increase_window(struct ll_readahead_state *ras,
1983 struct ll_ra_info *ra,
1984 unsigned long inc_len)
1986 unsigned long left, step, window_len;
1987 unsigned long stride_len;
1989 LASSERT(ras->ras_stride_length > 0);
1990 LASSERTF(ras->ras_window_start + ras->ras_window_len
1991 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
1992 " stride_offset %lu sp %lu sl %lu\n", ras->ras_window_start,
1993 ras->ras_window_len, ras->ras_stride_offset, ras->ras_stride_pages,
1994 ras->ras_stride_length);
1996 stride_len = ras->ras_window_start + ras->ras_window_len -
1997 ras->ras_stride_offset;
1999 left = stride_len % ras->ras_stride_length;
2000 window_len = ras->ras_window_len - left;
2002 if (left < ras->ras_stride_pages)
2005 left = ras->ras_stride_pages + inc_len;
2007 LASSERT(ras->ras_stride_pages != 0);
2009 step = left / ras->ras_stride_pages;
2010 left %= ras->ras_stride_pages;
2012 window_len += step * ras->ras_stride_length + left;
2014 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
2015 ras->ras_window_len = window_len;
2020 static void ras_increase_window(struct ll_readahead_state *ras,
2021 struct ll_ra_info *ra, struct inode *inode)
2023 __u64 step = INIT_RAS_WINDOW_PAGES;
2025 if (stride_io_mode(ras))
2026 ras_stride_increase_window(ras, ra, (unsigned long)step);
2028 ras->ras_window_len = min(ras->ras_window_len +
2029 (unsigned long)step,
2033 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
2034 struct ll_readahead_state *ras, unsigned long index,
2037 struct ll_ra_info *ra = &sbi->ll_ra_info;
2038 int zero = 0, stride_detect = 0, ra_miss = 0;
2041 spin_lock(&ras->ras_lock);
2043 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
2045 /* reset the read-ahead window in two cases. First when the app seeks
2046 * or reads to some other part of the file. Secondly if we get a
2047 * read-ahead miss that we think we've previously issued. This can
2048 * be a symptom of there being so many read-ahead pages that the VM is
2049 * reclaiming it before we get to it. */
2050 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
2052 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
2053 } else if (!hit && ras->ras_window_len &&
2054 index < ras->ras_next_readahead &&
2055 index_in_window(index, ras->ras_window_start, 0,
2056 ras->ras_window_len)) {
2058 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
2061 /* On the second access to a file smaller than the tunable
2062 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
2063 * file up to ra_max_pages_per_file. This is simply a best effort
2064 * and only occurs once per open file. Normal RA behavior is reverted
2065 * to for subsequent IO. The mmap case does not increment
2066 * ras_requests and thus can never trigger this behavior. */
2067 if (ras->ras_requests == 2 && !ras->ras_request_index) {
2070 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
2073 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
2074 ra->ra_max_read_ahead_whole_pages,
2075 ra->ra_max_pages_per_file);
2078 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
2079 ras->ras_window_start = 0;
2080 ras->ras_last_readpage = 0;
2081 ras->ras_next_readahead = 0;
2082 ras->ras_window_len = min(ra->ra_max_pages_per_file,
2083 ra->ra_max_read_ahead_whole_pages);
2084 GOTO(out_unlock, 0);
2088 /* check whether it is in stride I/O mode*/
2089 if (!index_in_stride_window(index, ras, inode)) {
2090 if (ras->ras_consecutive_stride_requests == 0 &&
2091 ras->ras_request_index == 0) {
2092 ras_update_stride_detector(ras, index);
2093 ras->ras_consecutive_stride_requests ++;
2095 ras_stride_reset(ras);
2097 ras_reset(ras, index);
2098 ras->ras_consecutive_pages++;
2099 GOTO(out_unlock, 0);
2101 ras->ras_consecutive_pages = 0;
2102 ras->ras_consecutive_requests = 0;
2103 if (++ras->ras_consecutive_stride_requests > 1)
2109 if (index_in_stride_window(index, ras, inode) &&
2110 stride_io_mode(ras)) {
2111 /*If stride-RA hit cache miss, the stride dector
2112 *will not be reset to avoid the overhead of
2113 *redetecting read-ahead mode */
2114 if (index != ras->ras_last_readpage + 1)
2115 ras->ras_consecutive_pages = 0;
2116 ras_reset(ras, index);
2119 /* Reset both stride window and normal RA
2121 ras_reset(ras, index);
2122 ras->ras_consecutive_pages++;
2123 ras_stride_reset(ras);
2124 GOTO(out_unlock, 0);
2126 } else if (stride_io_mode(ras)) {
2127 /* If this is contiguous read but in stride I/O mode
2128 * currently, check whether stride step still is valid,
2129 * if invalid, it will reset the stride ra window*/
2130 if (!index_in_stride_window(index, ras, inode)) {
2131 /* Shrink stride read-ahead window to be zero */
2132 ras_stride_reset(ras);
2133 ras->ras_window_len = 0;
2134 ras->ras_next_readahead = index;
2138 ras->ras_consecutive_pages++;
2139 ras->ras_last_readpage = index;
2140 ras_set_start(ras, index);
2141 ras->ras_next_readahead = max(ras->ras_window_start,
2142 ras->ras_next_readahead);
2145 /* Trigger RA in the mmap case where ras_consecutive_requests
2146 * is not incremented and thus can't be used to trigger RA */
2147 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
2148 ras->ras_window_len = INIT_RAS_WINDOW_PAGES;
2149 GOTO(out_unlock, 0);
2152 /* Initially reset the stride window offset to next_readahead*/
2153 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
2155 * Once stride IO mode is detected, next_readahead should be
2156 * reset to make sure next_readahead > stride offset
2158 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
2159 ras->ras_stride_offset = index;
2160 ras->ras_window_len = INIT_RAS_WINDOW_PAGES;
2163 /* The initial ras_window_len is set to the request size. To avoid
2164 * uselessly reading and discarding pages for random IO the window is
2165 * only increased once per consecutive request received. */
2166 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
2167 !ras->ras_request_index)
2168 ras_increase_window(ras, ra, inode);
2172 ras->ras_request_index++;
2173 spin_unlock(&ras->ras_lock);
2177 int ll_writepage(struct page *page)
2179 struct inode *inode = page->mapping->host;
2180 struct ll_inode_info *lli = ll_i2info(inode);
2181 struct obd_export *exp;
2182 struct ll_async_page *llap;
2183 struct ll_thread_data *ltd;
2184 struct lustre_handle *lockh = NULL;
2188 LASSERT(PageLocked(page));
2190 exp = ll_i2obdexp(inode);
2192 GOTO(out, rc = -EINVAL);
2195 /* currently, no FAST lock in write path */
2196 if (ltd && ltd->lock_style == LL_LOCK_STYLE_TREELOCK) {
2197 __u64 offset = ((loff_t)page->index) << CFS_PAGE_SHIFT;
2198 lockh = ltd2lockh(ltd, offset, offset + CFS_PAGE_SIZE - 1);
2201 llap = llap_from_page_with_lockh(page, LLAP_ORIGIN_WRITEPAGE, lockh, 0);
2203 GOTO(out, rc = PTR_ERR(llap));
2205 LASSERT(!PageWriteback(page));
2206 set_page_writeback(page);
2208 page_cache_get(page);
2209 if (llap->llap_write_queued) {
2210 LL_CDEBUG_PAGE(D_PAGE, page, "marking urgent\n");
2211 rc = obd_set_async_flags(exp, lli->lli_smd, NULL,
2213 ASYNC_READY | ASYNC_URGENT);
2215 rc = queue_or_sync_write(exp, inode, llap, CFS_PAGE_SIZE,
2216 ASYNC_READY | ASYNC_URGENT);
2219 /* re-dirty page on error so it retries write */
2220 if (PageWriteback(page))
2221 end_page_writeback(page);
2223 /* resend page only for not started IO*/
2224 if (!PageError(page))
2225 ll_redirty_page(page);
2227 page_cache_release(page);
2231 if (!lli->lli_async_rc)
2232 lli->lli_async_rc = rc;
2233 /* resend page only for not started IO*/
2240 * for now we do our readpage the same on both 2.4 and 2.5. The kernel's
2241 * read-ahead assumes it is valid to issue readpage all the way up to
2242 * i_size, but our dlm locks make that not the case. We disable the
2243 * kernel's read-ahead and do our own by walking ahead in the page cache
2244 * checking for dlm lock coverage. the main difference between 2.4 and
2245 * 2.6 is how read-ahead gets batched and issued, but we're using our own,
2246 * so they look the same.
2248 int ll_readpage(struct file *filp, struct page *page)
2250 struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
2251 struct inode *inode = page->mapping->host;
2252 struct obd_export *exp;
2253 struct ll_async_page *llap;
2254 struct obd_io_group *oig = NULL;
2255 struct lustre_handle *lockh = NULL;
2259 LASSERT(PageLocked(page));
2260 LASSERT(!PageUptodate(page));
2261 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset=%Lu=%#Lx\n",
2262 inode->i_ino, inode->i_generation, inode,
2263 (((loff_t)page->index) << CFS_PAGE_SHIFT),
2264 (((loff_t)page->index) << CFS_PAGE_SHIFT));
2265 LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
2267 if (!ll_i2info(inode)->lli_smd) {
2268 /* File with no objects - one big hole */
2269 /* We use this just for remove_from_page_cache that is not
2270 * exported, we'd make page back up to date. */
2271 ll_truncate_complete_page(page);
2272 clear_page(kmap(page));
2274 SetPageUptodate(page);
2279 rc = oig_init(&oig);
2283 exp = ll_i2obdexp(inode);
2285 GOTO(out, rc = -EINVAL);
2287 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2288 lockh = &fd->fd_cwlockh;
2290 struct ll_thread_data *ltd;
2292 if (ltd && ltd->lock_style > 0) {
2293 __u64 offset = ((loff_t)page->index) << CFS_PAGE_SHIFT;
2294 lockh = ltd2lockh(ltd, offset,
2295 offset + CFS_PAGE_SIZE - 1);
2296 if (ltd->lock_style == LL_LOCK_STYLE_FASTLOCK)
2297 flags = OBD_FAST_LOCK;
2301 llap = llap_from_page_with_lockh(page, LLAP_ORIGIN_READPAGE, lockh,
2304 if (PTR_ERR(llap) == -ENOLCK) {
2305 CWARN("ino %lu page %lu (%llu) not covered by "
2306 "a lock (mmap?). check debug logs.\n",
2307 inode->i_ino, page->index,
2308 (long long)page->index << PAGE_CACHE_SHIFT);
2310 GOTO(out, rc = PTR_ERR(llap));
2313 if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages_per_file)
2314 ras_update(ll_i2sbi(inode), inode, &fd->fd_ras, page->index,
2315 llap->llap_defer_uptodate);
2318 if (llap->llap_defer_uptodate) {
2319 /* This is the callpath if we got the page from a readahead */
2320 llap->llap_ra_used = 1;
2321 rc = ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
2324 obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd,
2326 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
2327 SetPageUptodate(page);
2329 GOTO(out_oig, rc = 0);
2332 rc = ll_issue_page_read(exp, llap, oig, 0);
2336 LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
2337 /* We have just requested the actual page we want, see if we can tack
2338 * on some readahead to that page's RPC before it is sent. */
2339 if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages_per_file)
2340 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
2343 rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);