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.
23 #ifndef AUTOCONF_INCLUDED
24 #include <linux/config.h>
26 #include <linux/kernel.h>
28 #include <linux/string.h>
29 #include <linux/stat.h>
30 #include <linux/errno.h>
31 #include <linux/smp_lock.h>
32 #include <linux/unistd.h>
33 #include <linux/version.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
38 #include <linux/stat.h>
39 #include <asm/uaccess.h>
41 #include <linux/pagemap.h>
42 #include <linux/smp_lock.h>
44 #define DEBUG_SUBSYSTEM S_LLITE
46 //#include <lustre_mdc.h>
47 #include <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 cfs_mem_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;
66 struct obd_info oinfo = { { { 0 } } };
72 pg.off = ((obd_off)page->index) << CFS_PAGE_SHIFT;
74 if ((cmd & OBD_BRW_WRITE) && (pg.off+CFS_PAGE_SIZE>i_size_read(inode)))
75 pg.count = i_size_read(inode) % CFS_PAGE_SIZE;
77 pg.count = CFS_PAGE_SIZE;
79 LL_CDEBUG_PAGE(D_PAGE, page, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
80 cmd & OBD_BRW_WRITE ? "write" : "read", pg.count,
81 inode->i_ino, pg.off, pg.off);
83 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
84 LPU64"\n", inode->i_ino, inode, i_size_read(inode),
85 page->mapping->host, i_size_read(page->mapping->host),
91 if (cmd & OBD_BRW_WRITE)
92 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_WRITE,
95 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_READ,
99 /* NB partial write, so we might not have CAPA_OPC_OSS_READ capa */
100 opc = cmd & OBD_BRW_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
101 oinfo.oi_capa = ll_osscapa_get(inode, opc);
102 rc = obd_brw(cmd, ll_i2dtexp(inode), &oinfo, 1, &pg, NULL);
103 capa_put(oinfo.oi_capa);
105 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
107 CERROR("error from obd_brw: rc = %d\n", rc);
111 /* this isn't where truncate starts. roughly:
112 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate. setattr_raw grabs
113 * DLM lock on [size, EOF], i_mutex, ->lli_size_sem, and WRITE_I_ALLOC_SEM to
116 * must be called under ->lli_size_sem */
117 void ll_truncate(struct inode *inode)
119 struct ll_inode_info *lli = ll_i2info(inode);
120 struct obd_info oinfo = { { { 0 } } };
125 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %Lu=%#Lx\n",inode->i_ino,
126 inode->i_generation, inode, i_size_read(inode),
129 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_TRUNC, 1);
130 if (lli->lli_size_sem_owner != current) {
136 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
141 LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
143 /* XXX I'm pretty sure this is a hack to paper over a more fundamental
145 lov_stripe_lock(lli->lli_smd);
146 inode_init_lvb(inode, &lvb);
147 rc = obd_merge_lvb(ll_i2dtexp(inode), lli->lli_smd, &lvb, 0);
148 if (lvb.lvb_size == i_size_read(inode) && rc == 0) {
149 CDEBUG(D_VFSTRACE, "skipping punch for obj "LPX64", %Lu=%#Lx\n",
150 lli->lli_smd->lsm_object_id, i_size_read(inode),
152 lov_stripe_unlock(lli->lli_smd);
156 obd_adjust_kms(ll_i2dtexp(inode), lli->lli_smd, i_size_read(inode), 1);
157 lov_stripe_unlock(lli->lli_smd);
159 if (unlikely((ll_i2sbi(inode)->ll_flags & LL_SBI_CHECKSUM) &&
160 (i_size_read(inode) & ~CFS_PAGE_MASK))) {
161 /* If the truncate leaves behind a partial page, update its
163 struct page *page = find_get_page(inode->i_mapping,
164 i_size_read(inode) >>
167 struct ll_async_page *llap = llap_cast_private(page);
169 char *kaddr = kmap_atomic(page, KM_USER0);
170 llap->llap_checksum =
171 init_checksum(OSC_DEFAULT_CKSUM);
172 llap->llap_checksum =
173 compute_checksum(llap->llap_checksum,
174 kaddr, CFS_PAGE_SIZE,
176 kunmap_atomic(kaddr, KM_USER0);
178 page_cache_release(page);
182 CDEBUG(D_INFO, "calling punch for "LPX64" (new size %Lu=%#Lx)\n",
183 lli->lli_smd->lsm_object_id, i_size_read(inode), i_size_read(inode));
185 oinfo.oi_md = lli->lli_smd;
186 oinfo.oi_policy.l_extent.start = i_size_read(inode);
187 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
189 oa.o_id = lli->lli_smd->lsm_object_id;
190 oa.o_gr = lli->lli_smd->lsm_object_gr;
191 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
193 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLMODE |
194 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
195 OBD_MD_FLFID | OBD_MD_FLGENER);
197 ll_inode_size_unlock(inode, 0);
199 oinfo.oi_capa = ll_osscapa_get(inode, CAPA_OPC_OSS_TRUNC);
200 rc = obd_punch_rqset(ll_i2dtexp(inode), &oinfo, NULL);
201 ll_truncate_free_capa(oinfo.oi_capa);
203 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
205 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
206 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
211 ll_inode_size_unlock(inode, 0);
214 int ll_prepare_write(struct file *file, struct page *page, unsigned from,
217 struct inode *inode = page->mapping->host;
218 struct ll_inode_info *lli = ll_i2info(inode);
219 struct lov_stripe_md *lsm = lli->lli_smd;
220 obd_off offset = ((obd_off)page->index) << CFS_PAGE_SHIFT;
221 struct obd_info oinfo = { { { 0 } } };
228 LASSERT(PageLocked(page));
229 (void)llap_cast_private(page); /* assertion */
231 /* Check to see if we should return -EIO right away */
234 pga.count = CFS_PAGE_SIZE;
237 oa.o_mode = inode->i_mode;
238 oa.o_id = lsm->lsm_object_id;
239 oa.o_gr = lsm->lsm_object_gr;
240 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE |
241 OBD_MD_FLTYPE | OBD_MD_FLGROUP;
242 obdo_from_inode(&oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
246 rc = obd_brw(OBD_BRW_CHECK, ll_i2dtexp(inode), &oinfo, 1, &pga, NULL);
250 if (PageUptodate(page)) {
251 LL_CDEBUG_PAGE(D_PAGE, page, "uptodate\n");
255 /* We're completely overwriting an existing page, so _don't_ set it up
256 * to date until commit_write */
257 if (from == 0 && to == CFS_PAGE_SIZE) {
258 LL_CDEBUG_PAGE(D_PAGE, page, "full page write\n");
259 POISON_PAGE(page, 0x11);
263 /* If are writing to a new page, no need to read old data. The extent
264 * locking will have updated the KMS, and for our purposes here we can
265 * treat it like i_size. */
266 lov_stripe_lock(lsm);
267 inode_init_lvb(inode, &lvb);
268 obd_merge_lvb(ll_i2dtexp(inode), lsm, &lvb, 1);
269 lov_stripe_unlock(lsm);
270 if (lvb.lvb_size <= offset) {
271 char *kaddr = kmap_atomic(page, KM_USER0);
272 LL_CDEBUG_PAGE(D_PAGE, page, "kms "LPU64" <= offset "LPU64"\n",
273 lvb.lvb_size, offset);
274 memset(kaddr, 0, CFS_PAGE_SIZE);
275 kunmap_atomic(kaddr, KM_USER0);
276 GOTO(prepare_done, rc = 0);
279 /* XXX could be an async ocp read.. read-ahead? */
280 rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
282 /* bug 1598: don't clobber blksize */
283 oa.o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
284 obdo_refresh_inode(inode, &oa, oa.o_valid);
290 SetPageUptodate(page);
295 static int ll_ap_make_ready(void *data, int cmd)
297 struct ll_async_page *llap;
301 llap = LLAP_FROM_COOKIE(data);
302 page = llap->llap_page;
304 LASSERTF(!(cmd & OBD_BRW_READ), "cmd %x page %p ino %lu index %lu\n", cmd, page,
305 page->mapping->host->i_ino, page->index);
307 /* we're trying to write, but the page is locked.. come back later */
308 if (TryLockPage(page))
311 LASSERT(!PageWriteback(page));
313 /* if we left PageDirty we might get another writepage call
314 * in the future. list walkers are bright enough
315 * to check page dirty so we can leave it on whatever list
316 * its on. XXX also, we're called with the cli list so if
317 * we got the page cache list we'd create a lock inversion
318 * with the removepage path which gets the page lock then the
320 LASSERTF(!PageWriteback(page),"cmd %x page %p ino %lu index %lu\n", cmd, page,
321 page->mapping->host->i_ino, page->index);
322 clear_page_dirty_for_io(page);
324 /* This actually clears the dirty bit in the radix tree.*/
325 set_page_writeback(page);
327 LL_CDEBUG_PAGE(D_PAGE, page, "made ready\n");
328 page_cache_get(page);
333 /* We have two reasons for giving llite the opportunity to change the
334 * write length of a given queued page as it builds the RPC containing
337 * 1) Further extending writes may have landed in the page cache
338 * since a partial write first queued this page requiring us
339 * to write more from the page cache. (No further races are possible, since
340 * by the time this is called, the page is locked.)
341 * 2) We might have raced with truncate and want to avoid performing
342 * write RPCs that are just going to be thrown away by the
343 * truncate's punch on the storage targets.
345 * The kms serves these purposes as it is set at both truncate and extending
348 static int ll_ap_refresh_count(void *data, int cmd)
350 struct ll_inode_info *lli;
351 struct ll_async_page *llap;
352 struct lov_stripe_md *lsm;
359 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
360 LASSERT(cmd != OBD_BRW_READ);
362 llap = LLAP_FROM_COOKIE(data);
363 page = llap->llap_page;
364 inode = page->mapping->host;
365 lli = ll_i2info(inode);
368 lov_stripe_lock(lsm);
369 inode_init_lvb(inode, &lvb);
370 obd_merge_lvb(ll_i2dtexp(inode), lsm, &lvb, 1);
372 lov_stripe_unlock(lsm);
374 /* catch race with truncate */
375 if (((__u64)page->index << CFS_PAGE_SHIFT) >= kms)
378 /* catch sub-page write at end of file */
379 if (((__u64)page->index << CFS_PAGE_SHIFT) + CFS_PAGE_SIZE > kms)
380 return kms % CFS_PAGE_SIZE;
382 return CFS_PAGE_SIZE;
385 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
387 struct lov_stripe_md *lsm;
388 obd_flag valid_flags;
390 lsm = ll_i2info(inode)->lli_smd;
392 oa->o_id = lsm->lsm_object_id;
393 oa->o_gr = lsm->lsm_object_gr;
394 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
395 valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
396 if (cmd & OBD_BRW_WRITE) {
397 oa->o_valid |= OBD_MD_FLEPOCH;
398 oa->o_easize = ll_i2info(inode)->lli_ioepoch;
400 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
401 OBD_MD_FLUID | OBD_MD_FLGID |
402 OBD_MD_FLFID | OBD_MD_FLGENER;
405 obdo_from_inode(oa, inode, valid_flags);
408 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
410 struct ll_async_page *llap;
413 llap = LLAP_FROM_COOKIE(data);
414 ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
419 static void ll_ap_update_obdo(void *data, int cmd, struct obdo *oa,
422 struct ll_async_page *llap;
425 llap = LLAP_FROM_COOKIE(data);
426 obdo_from_inode(oa, llap->llap_page->mapping->host, valid);
431 static struct obd_capa *ll_ap_lookup_capa(void *data, int cmd)
433 struct ll_async_page *llap = LLAP_FROM_COOKIE(data);
434 int opc = cmd & OBD_BRW_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
436 return ll_osscapa_get(llap->llap_page->mapping->host, opc);
439 static struct obd_async_page_ops ll_async_page_ops = {
440 .ap_make_ready = ll_ap_make_ready,
441 .ap_refresh_count = ll_ap_refresh_count,
442 .ap_fill_obdo = ll_ap_fill_obdo,
443 .ap_update_obdo = ll_ap_update_obdo,
444 .ap_completion = ll_ap_completion,
445 .ap_lookup_capa = ll_ap_lookup_capa,
448 struct ll_async_page *llap_cast_private(struct page *page)
450 struct ll_async_page *llap = (struct ll_async_page *)page_private(page);
452 LASSERTF(llap == NULL || llap->llap_magic == LLAP_MAGIC,
453 "page %p private %lu gave magic %d which != %d\n",
454 page, page_private(page), llap->llap_magic, LLAP_MAGIC);
459 /* Try to shrink the page cache for the @sbi filesystem by 1/@shrink_fraction.
461 * There is an llap attached onto every page in lustre, linked off @sbi.
462 * We add an llap to the list so we don't lose our place during list walking.
463 * If llaps in the list are being moved they will only move to the end
464 * of the LRU, and we aren't terribly interested in those pages here (we
465 * start at the beginning of the list where the least-used llaps are.
467 int llap_shrink_cache(struct ll_sb_info *sbi, int shrink_fraction)
469 struct ll_async_page *llap, dummy_llap = { .llap_magic = 0xd11ad11a };
470 unsigned long total, want, count = 0;
472 total = sbi->ll_async_page_count;
474 /* There can be a large number of llaps (600k or more in a large
475 * memory machine) so the VM 1/6 shrink ratio is likely too much.
476 * Since we are freeing pages also, we don't necessarily want to
477 * shrink so much. Limit to 40MB of pages + llaps per call. */
478 if (shrink_fraction == 0)
479 want = sbi->ll_async_page_count - sbi->ll_async_page_max + 32;
481 want = (total + shrink_fraction - 1) / shrink_fraction;
483 if (want > 40 << (20 - CFS_PAGE_SHIFT))
484 want = 40 << (20 - CFS_PAGE_SHIFT);
486 CDEBUG(D_CACHE, "shrinking %lu of %lu pages (1/%d)\n",
487 want, total, shrink_fraction);
489 spin_lock(&sbi->ll_lock);
490 list_add(&dummy_llap.llap_pglist_item, &sbi->ll_pglist);
492 while (--total >= 0 && count < want) {
496 if (unlikely(need_resched())) {
497 spin_unlock(&sbi->ll_lock);
499 spin_lock(&sbi->ll_lock);
502 llap = llite_pglist_next_llap(sbi,&dummy_llap.llap_pglist_item);
503 list_del_init(&dummy_llap.llap_pglist_item);
507 page = llap->llap_page;
508 LASSERT(page != NULL);
510 list_add(&dummy_llap.llap_pglist_item, &llap->llap_pglist_item);
512 /* Page needs/undergoing IO */
513 if (TryLockPage(page)) {
514 LL_CDEBUG_PAGE(D_PAGE, page, "can't lock\n");
518 keep = (llap->llap_write_queued || PageDirty(page) ||
519 PageWriteback(page) || (!PageUptodate(page) &&
520 llap->llap_origin != LLAP_ORIGIN_READAHEAD));
522 LL_CDEBUG_PAGE(D_PAGE, page,"%s LRU page: %s%s%s%s%s origin %s\n",
523 keep ? "keep" : "drop",
524 llap->llap_write_queued ? "wq " : "",
525 PageDirty(page) ? "pd " : "",
526 PageUptodate(page) ? "" : "!pu ",
527 PageWriteback(page) ? "wb" : "",
528 llap->llap_defer_uptodate ? "" : "!du",
529 llap_origins[llap->llap_origin]);
531 /* If page is dirty or undergoing IO don't discard it */
537 page_cache_get(page);
538 spin_unlock(&sbi->ll_lock);
540 if (page->mapping != NULL) {
541 ll_teardown_mmaps(page->mapping,
542 (__u64)page->index << CFS_PAGE_SHIFT,
543 ((__u64)page->index << CFS_PAGE_SHIFT)|
545 if (!PageDirty(page) && !page_mapped(page)) {
546 ll_ra_accounting(llap, page->mapping);
547 ll_truncate_complete_page(page);
550 LL_CDEBUG_PAGE(D_PAGE, page, "Not dropping page"
558 page_cache_release(page);
560 spin_lock(&sbi->ll_lock);
562 list_del(&dummy_llap.llap_pglist_item);
563 spin_unlock(&sbi->ll_lock);
565 CDEBUG(D_CACHE, "shrank %lu/%lu and left %lu unscanned\n",
571 struct ll_async_page *llap_from_page(struct page *page, unsigned origin)
573 struct ll_async_page *llap;
574 struct obd_export *exp;
575 struct inode *inode = page->mapping->host;
576 struct ll_sb_info *sbi;
581 static int triggered;
584 LL_CDEBUG_PAGE(D_ERROR, page, "Bug 10047. Wrong anon "
586 libcfs_debug_dumpstack(NULL);
589 RETURN(ERR_PTR(-EINVAL));
591 sbi = ll_i2sbi(inode);
592 LASSERT(ll_async_page_slab);
593 LASSERTF(origin < LLAP__ORIGIN_MAX, "%u\n", origin);
595 llap = llap_cast_private(page);
597 /* move to end of LRU list, except when page is just about to
599 if (origin != LLAP_ORIGIN_REMOVEPAGE) {
600 spin_lock(&sbi->ll_lock);
601 sbi->ll_pglist_gen++;
602 list_del_init(&llap->llap_pglist_item);
603 list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
604 spin_unlock(&sbi->ll_lock);
609 exp = ll_i2dtexp(page->mapping->host);
611 RETURN(ERR_PTR(-EINVAL));
613 /* limit the number of lustre-cached pages */
614 if (sbi->ll_async_page_count >= sbi->ll_async_page_max)
615 llap_shrink_cache(sbi, 0);
617 OBD_SLAB_ALLOC(llap, ll_async_page_slab, CFS_ALLOC_STD,
618 ll_async_page_slab_size);
620 RETURN(ERR_PTR(-ENOMEM));
621 llap->llap_magic = LLAP_MAGIC;
622 llap->llap_cookie = (void *)llap + size_round(sizeof(*llap));
624 rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
625 (obd_off)page->index << CFS_PAGE_SHIFT,
626 &ll_async_page_ops, llap, &llap->llap_cookie);
628 OBD_SLAB_FREE(llap, ll_async_page_slab,
629 ll_async_page_slab_size);
633 CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
634 page, llap->llap_cookie, (obd_off)page->index << CFS_PAGE_SHIFT);
635 /* also zeroing the PRIVBITS low order bitflags */
636 __set_page_ll_data(page, llap);
637 llap->llap_page = page;
638 spin_lock(&sbi->ll_lock);
639 sbi->ll_pglist_gen++;
640 sbi->ll_async_page_count++;
641 list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
642 INIT_LIST_HEAD(&llap->llap_pending_write);
643 spin_unlock(&sbi->ll_lock);
646 if (unlikely(sbi->ll_flags & LL_SBI_CHECKSUM)) {
648 char *kaddr = kmap_atomic(page, KM_USER0);
649 csum = init_checksum(OSC_DEFAULT_CKSUM);
650 csum = compute_checksum(csum, kaddr, CFS_PAGE_SIZE,
652 kunmap_atomic(kaddr, KM_USER0);
653 if (origin == LLAP_ORIGIN_READAHEAD ||
654 origin == LLAP_ORIGIN_READPAGE) {
655 llap->llap_checksum = 0;
656 } else if (origin == LLAP_ORIGIN_COMMIT_WRITE ||
657 llap->llap_checksum == 0) {
658 llap->llap_checksum = csum;
659 CDEBUG(D_PAGE, "page %p cksum %x\n", page, csum);
660 } else if (llap->llap_checksum == csum) {
661 /* origin == LLAP_ORIGIN_WRITEPAGE */
662 CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
665 /* origin == LLAP_ORIGIN_WRITEPAGE */
666 LL_CDEBUG_PAGE(D_ERROR, page, "old cksum %x != new "
667 "%x!\n", llap->llap_checksum, csum);
671 llap->llap_origin = origin;
675 static int queue_or_sync_write(struct obd_export *exp, struct inode *inode,
676 struct ll_async_page *llap,
677 unsigned to, obd_flag async_flags)
679 unsigned long size_index = i_size_read(inode) >> CFS_PAGE_SHIFT;
680 struct obd_io_group *oig;
681 struct ll_sb_info *sbi = ll_i2sbi(inode);
682 int rc, noquot = llap->llap_ignore_quota ? OBD_BRW_NOQUOTA : 0;
685 /* _make_ready only sees llap once we've unlocked the page */
686 llap->llap_write_queued = 1;
687 rc = obd_queue_async_io(exp, ll_i2info(inode)->lli_smd, NULL,
688 llap->llap_cookie, OBD_BRW_WRITE | noquot,
689 0, 0, 0, async_flags);
691 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "write queued\n");
695 llap->llap_write_queued = 0;
696 /* Do not pass llap here as it is sync write. */
697 llap_write_pending(inode, NULL);
703 /* make full-page requests if we are not at EOF (bug 4410) */
704 if (to != CFS_PAGE_SIZE && llap->llap_page->index < size_index) {
705 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
706 "sync write before EOF: size_index %lu, to %d\n",
709 } else if (to != CFS_PAGE_SIZE && llap->llap_page->index == size_index) {
710 int size_to = i_size_read(inode) & ~CFS_PAGE_MASK;
711 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
712 "sync write at EOF: size_index %lu, to %d/%d\n",
713 size_index, to, size_to);
718 /* compare the checksum once before the page leaves llite */
719 if (unlikely((sbi->ll_flags & LL_SBI_CHECKSUM) &&
720 llap->llap_checksum != 0)) {
722 struct page *page = llap->llap_page;
723 char *kaddr = kmap_atomic(page, KM_USER0);
724 csum = init_checksum(OSC_DEFAULT_CKSUM);
725 csum = compute_checksum(csum, kaddr, CFS_PAGE_SIZE,
727 kunmap_atomic(kaddr, KM_USER0);
728 if (llap->llap_checksum == csum) {
729 CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
732 CERROR("page %p old cksum %x != new cksum %x!\n",
733 page, llap->llap_checksum, csum);
737 rc = obd_queue_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig,
738 llap->llap_cookie, OBD_BRW_WRITE | noquot,
739 0, to, 0, ASYNC_READY | ASYNC_URGENT |
740 ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
744 rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
750 if (!rc && async_flags & ASYNC_READY) {
751 unlock_page(llap->llap_page);
752 if (PageWriteback(llap->llap_page)) {
753 end_page_writeback(llap->llap_page);
757 if (rc == 0 && llap_write_complete(inode, llap))
758 ll_queue_done_writing(inode, 0);
760 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "sync write returned %d\n", rc);
768 /* update our write count to account for i_size increases that may have
769 * happened since we've queued the page for io. */
771 /* be careful not to return success without setting the page Uptodate or
772 * the next pass through prepare_write will read in stale data from disk. */
773 int ll_commit_write(struct file *file, struct page *page, unsigned from,
776 struct inode *inode = page->mapping->host;
777 struct ll_inode_info *lli = ll_i2info(inode);
778 struct lov_stripe_md *lsm = lli->lli_smd;
779 struct obd_export *exp;
780 struct ll_async_page *llap;
785 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
786 LASSERT(inode == file->f_dentry->d_inode);
787 LASSERT(PageLocked(page));
789 CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
790 inode, page, from, to, page->index);
792 llap = llap_from_page(page, LLAP_ORIGIN_COMMIT_WRITE);
794 RETURN(PTR_ERR(llap));
796 exp = ll_i2dtexp(inode);
800 llap->llap_ignore_quota = capable(CAP_SYS_RESOURCE);
803 * queue a write for some time in the future the first time we
806 * This is different from what other file systems do: they usually
807 * just mark page (and some of its buffers) dirty and rely on
808 * balance_dirty_pages() to start a write-back. Lustre wants write-back
809 * to be started earlier for the following reasons:
811 * (1) with a large number of clients we need to limit the amount
812 * of cached data on the clients a lot;
814 * (2) large compute jobs generally want compute-only then io-only
815 * and the IO should complete as quickly as possible;
817 * (3) IO is batched up to the RPC size and is async until the
818 * client max cache is hit
819 * (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
822 if (!PageDirty(page)) {
823 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRTY_MISSES, 1);
825 rc = queue_or_sync_write(exp, inode, llap, to, 0);
829 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRTY_HITS, 1);
832 /* put the page in the page cache, from now on ll_removepage is
833 * responsible for cleaning up the llap.
834 * only set page dirty when it's queued to be write out */
835 if (llap->llap_write_queued)
836 set_page_dirty(page);
839 size = (((obd_off)page->index) << CFS_PAGE_SHIFT) + to;
840 ll_inode_size_lock(inode, 0);
842 lov_stripe_lock(lsm);
843 obd_adjust_kms(exp, lsm, size, 0);
844 lov_stripe_unlock(lsm);
845 if (size > i_size_read(inode))
846 i_size_write(inode, size);
847 SetPageUptodate(page);
848 } else if (size > i_size_read(inode)) {
849 /* this page beyond the pales of i_size, so it can't be
850 * truncated in ll_p_r_e during lock revoking. we must
851 * teardown our book-keeping here. */
854 ll_inode_size_unlock(inode, 0);
858 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
860 struct ll_ra_info *ra = &sbi->ll_ra_info;
864 spin_lock(&sbi->ll_lock);
865 ret = min(ra->ra_max_pages - ra->ra_cur_pages, len);
866 ra->ra_cur_pages += ret;
867 spin_unlock(&sbi->ll_lock);
872 static void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
874 struct ll_ra_info *ra = &sbi->ll_ra_info;
875 spin_lock(&sbi->ll_lock);
876 LASSERTF(ra->ra_cur_pages >= len, "r_c_p %lu len %lu\n",
877 ra->ra_cur_pages, len);
878 ra->ra_cur_pages -= len;
879 spin_unlock(&sbi->ll_lock);
882 /* called for each page in a completed rpc.*/
883 int ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
885 struct ll_async_page *llap;
890 llap = LLAP_FROM_COOKIE(data);
891 page = llap->llap_page;
892 LASSERT(PageLocked(page));
893 LASSERT(CheckWriteback(page,cmd));
895 LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
897 if (cmd & OBD_BRW_READ && llap->llap_defer_uptodate)
898 ll_ra_count_put(ll_i2sbi(page->mapping->host), 1);
901 if (cmd & OBD_BRW_READ) {
902 if (!llap->llap_defer_uptodate)
903 SetPageUptodate(page);
905 llap->llap_write_queued = 0;
907 ClearPageError(page);
909 if (cmd & OBD_BRW_READ) {
910 llap->llap_defer_uptodate = 0;
914 set_bit(AS_ENOSPC, &page->mapping->flags);
916 set_bit(AS_EIO, &page->mapping->flags);
921 if (cmd & OBD_BRW_WRITE) {
922 /* Only rc == 0, write succeed, then this page could be deleted
923 * from the pending_writing list
925 if (rc == 0 && llap_write_complete(page->mapping->host, llap))
926 ll_queue_done_writing(page->mapping->host, 0);
929 if (PageWriteback(page)) {
930 end_page_writeback(page);
932 page_cache_release(page);
937 /* the kernel calls us here when a page is unhashed from the page cache.
938 * the page will be locked and the kernel is holding a spinlock, so
939 * we need to be careful. we're just tearing down our book-keeping
941 void ll_removepage(struct page *page)
943 struct inode *inode = page->mapping->host;
944 struct obd_export *exp;
945 struct ll_async_page *llap;
946 struct ll_sb_info *sbi = ll_i2sbi(inode);
950 LASSERT(!in_interrupt());
952 /* sync pages or failed read pages can leave pages in the page
953 * cache that don't have our data associated with them anymore */
954 if (page_private(page) == 0) {
959 LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
961 exp = ll_i2dtexp(inode);
963 CERROR("page %p ind %lu gave null export\n", page, page->index);
968 llap = llap_from_page(page, LLAP_ORIGIN_REMOVEPAGE);
970 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
971 page->index, PTR_ERR(llap));
976 if (llap_write_complete(inode, llap))
977 ll_queue_done_writing(inode, 0);
979 rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
982 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
984 /* this unconditional free is only safe because the page lock
985 * is providing exclusivity to memory pressure/truncate/writeback..*/
986 __clear_page_ll_data(page);
988 spin_lock(&sbi->ll_lock);
989 if (!list_empty(&llap->llap_pglist_item))
990 list_del_init(&llap->llap_pglist_item);
991 sbi->ll_pglist_gen++;
992 sbi->ll_async_page_count--;
993 spin_unlock(&sbi->ll_lock);
994 OBD_SLAB_FREE(llap, ll_async_page_slab, ll_async_page_slab_size);
998 static int ll_page_matches(struct page *page, int fd_flags)
1000 struct lustre_handle match_lockh = {0};
1001 struct inode *inode = page->mapping->host;
1002 ldlm_policy_data_t page_extent;
1006 if (unlikely(fd_flags & LL_FILE_GROUP_LOCKED))
1009 page_extent.l_extent.start = (__u64)page->index << CFS_PAGE_SHIFT;
1010 page_extent.l_extent.end =
1011 page_extent.l_extent.start + CFS_PAGE_SIZE - 1;
1012 flags = LDLM_FL_TEST_LOCK | LDLM_FL_BLOCK_GRANTED;
1013 if (!(fd_flags & LL_FILE_READAHEAD))
1014 flags |= LDLM_FL_CBPENDING;
1015 matches = obd_match(ll_i2sbi(inode)->ll_dt_exp,
1016 ll_i2info(inode)->lli_smd, LDLM_EXTENT,
1017 &page_extent, LCK_PR | LCK_PW, &flags, inode,
1022 static int ll_issue_page_read(struct obd_export *exp,
1023 struct ll_async_page *llap,
1024 struct obd_io_group *oig, int defer)
1026 struct page *page = llap->llap_page;
1029 page_cache_get(page);
1030 llap->llap_defer_uptodate = defer;
1031 llap->llap_ra_used = 0;
1032 rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
1033 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
1034 CFS_PAGE_SIZE, 0, ASYNC_COUNT_STABLE |
1035 ASYNC_READY | ASYNC_URGENT);
1037 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
1038 page_cache_release(page);
1043 static void ll_ra_stats_inc_unlocked(struct ll_ra_info *ra, enum ra_stat which)
1045 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
1046 ra->ra_stats[which]++;
1049 static void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
1051 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
1052 struct ll_ra_info *ra = &ll_i2sbi(mapping->host)->ll_ra_info;
1054 spin_lock(&sbi->ll_lock);
1055 ll_ra_stats_inc_unlocked(ra, which);
1056 spin_unlock(&sbi->ll_lock);
1059 void ll_ra_accounting(struct ll_async_page *llap, struct address_space *mapping)
1061 if (!llap->llap_defer_uptodate || llap->llap_ra_used)
1064 ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
1067 #define RAS_CDEBUG(ras) \
1069 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
1070 "csr %lu sf %lu sp %lu sl %lu \n", \
1071 ras->ras_last_readpage, ras->ras_consecutive_requests, \
1072 ras->ras_consecutive_pages, ras->ras_window_start, \
1073 ras->ras_window_len, ras->ras_next_readahead, \
1074 ras->ras_requests, ras->ras_request_index, \
1075 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
1076 ras->ras_stride_pages, ras->ras_stride_length)
1078 static int index_in_window(unsigned long index, unsigned long point,
1079 unsigned long before, unsigned long after)
1081 unsigned long start = point - before, end = point + after;
1088 return start <= index && index <= end;
1091 static struct ll_readahead_state *ll_ras_get(struct file *f)
1093 struct ll_file_data *fd;
1095 fd = LUSTRE_FPRIVATE(f);
1099 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
1101 struct ll_readahead_state *ras;
1103 ras = ll_ras_get(f);
1105 spin_lock(&ras->ras_lock);
1106 ras->ras_requests++;
1107 ras->ras_request_index = 0;
1108 ras->ras_consecutive_requests++;
1109 rar->lrr_reader = current;
1111 list_add(&rar->lrr_linkage, &ras->ras_read_beads);
1112 spin_unlock(&ras->ras_lock);
1115 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
1117 struct ll_readahead_state *ras;
1119 ras = ll_ras_get(f);
1121 spin_lock(&ras->ras_lock);
1122 list_del_init(&rar->lrr_linkage);
1123 spin_unlock(&ras->ras_lock);
1126 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
1128 struct ll_ra_read *scan;
1130 list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
1131 if (scan->lrr_reader == current)
1137 struct ll_ra_read *ll_ra_read_get(struct file *f)
1139 struct ll_readahead_state *ras;
1140 struct ll_ra_read *bead;
1142 ras = ll_ras_get(f);
1144 spin_lock(&ras->ras_lock);
1145 bead = ll_ra_read_get_locked(ras);
1146 spin_unlock(&ras->ras_lock);
1150 static int ll_read_ahead_page(struct obd_export *exp, struct obd_io_group *oig,
1151 int index, struct address_space *mapping)
1153 struct ll_async_page *llap;
1155 unsigned int gfp_mask = 0;
1158 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
1160 gfp_mask |= __GFP_NOWARN;
1162 page = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
1164 ll_ra_stats_inc(mapping, RA_STAT_FAILED_GRAB_PAGE);
1165 CDEBUG(D_READA, "g_c_p_n failed\n");
1169 /* Check if page was truncated or reclaimed */
1170 if (page->mapping != mapping) {
1171 ll_ra_stats_inc(mapping, RA_STAT_WRONG_GRAB_PAGE);
1172 CDEBUG(D_READA, "g_c_p_n returned invalid page\n");
1173 GOTO(unlock_page, rc = 0);
1176 /* we do this first so that we can see the page in the /proc
1178 llap = llap_from_page(page, LLAP_ORIGIN_READAHEAD);
1179 if (IS_ERR(llap) || llap->llap_defer_uptodate) {
1180 if (PTR_ERR(llap) == -ENOLCK) {
1181 ll_ra_stats_inc(mapping, RA_STAT_FAILED_MATCH);
1182 CDEBUG(D_READA | D_PAGE,
1183 "Adding page to cache failed index "
1185 CDEBUG(D_READA, "nolock page\n");
1186 GOTO(unlock_page, rc = -ENOLCK);
1188 CDEBUG(D_READA, "read-ahead page\n");
1189 GOTO(unlock_page, rc = 0);
1192 /* skip completed pages */
1193 if (Page_Uptodate(page))
1194 GOTO(unlock_page, rc = 0);
1196 /* bail out when we hit the end of the lock. */
1197 rc = ll_issue_page_read(exp, llap, oig, 1);
1199 LL_CDEBUG_PAGE(D_READA | D_PAGE, page, "started read-ahead\n");
1204 LL_CDEBUG_PAGE(D_READA | D_PAGE, page, "skipping read-ahead\n");
1206 page_cache_release(page);
1210 /* ra_io_arg will be filled in the beginning of ll_readahead with
1211 * ras_lock, then the following ll_read_ahead_pages will read RA
1212 * pages according to this arg, all the items in this structure are
1213 * counted by page index.
1216 unsigned long ria_start; /* start offset of read-ahead*/
1217 unsigned long ria_end; /* end offset of read-ahead*/
1218 /* If stride read pattern is detected, ria_stoff means where
1219 * stride read is started. Note: for normal read-ahead, the
1220 * value here is meaningless, and also it will not be accessed*/
1222 /* ria_length and ria_pages are the length and pages length in the
1223 * stride I/O mode. And they will also be used to check whether
1224 * it is stride I/O read-ahead in the read-ahead pages*/
1225 unsigned long ria_length;
1226 unsigned long ria_pages;
1229 #define RIA_DEBUG(ria) \
1230 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
1231 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
1234 #define RAS_INCREASE_STEP (1024 * 1024 >> CFS_PAGE_SHIFT)
1236 static inline int stride_io_mode(struct ll_readahead_state *ras)
1238 return ras->ras_consecutive_stride_requests > 1;
1241 /* The function calculates how much pages will be read in
1242 * [off, off + length], which will be read by stride I/O mode,
1243 * stride_offset = st_off, stride_lengh = st_len,
1244 * stride_pages = st_pgs
1246 static unsigned long
1247 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
1248 unsigned long off, unsigned length)
1250 unsigned long cont_len = st_off > off ? st_off - off : 0;
1251 unsigned long stride_len = length + off > st_off ?
1252 length + off + 1 - st_off : 0;
1253 unsigned long left, pg_count;
1255 if (st_len == 0 || length == 0)
1258 left = do_div(stride_len, st_len);
1259 left = min(left, st_pgs);
1261 pg_count = left + stride_len * st_pgs + cont_len;
1263 LASSERT(pg_count >= left);
1265 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %u"
1266 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
1271 static int ria_page_count(struct ra_io_arg *ria)
1273 __u64 length = ria->ria_end >= ria->ria_start ?
1274 ria->ria_end - ria->ria_start + 1 : 0;
1276 return stride_pg_count(ria->ria_stoff, ria->ria_length,
1277 ria->ria_pages, ria->ria_start,
1281 /*Check whether the index is in the defined ra-window */
1282 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
1284 /* If ria_length == ria_pages, it means non-stride I/O mode,
1285 * idx should always inside read-ahead window in this case
1286 * For stride I/O mode, just check whether the idx is inside
1288 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
1289 (idx - ria->ria_stoff) % ria->ria_length < ria->ria_pages;
1292 static int ll_read_ahead_pages(struct obd_export *exp,
1293 struct obd_io_group *oig,
1294 struct ra_io_arg *ria,
1295 unsigned long *reserved_pages,
1296 struct address_space *mapping,
1297 unsigned long *ra_end)
1299 int rc, count = 0, stride_ria;
1300 unsigned long page_idx;
1302 LASSERT(ria != NULL);
1305 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
1306 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
1307 *reserved_pages > 0; page_idx++) {
1308 if (ras_inside_ra_window(page_idx, ria)) {
1309 /* If the page is inside the read-ahead window*/
1310 rc = ll_read_ahead_page(exp, oig, page_idx, mapping);
1312 (*reserved_pages)--;
1314 } else if (rc == -ENOLCK)
1316 } else if (stride_ria) {
1317 /* If it is not in the read-ahead window, and it is
1318 * read-ahead mode, then check whether it should skip
1321 /* FIXME: This assertion only is valid when it is for
1322 * forward read-ahead, it will be fixed when backward
1323 * read-ahead is implemented */
1324 LASSERTF(page_idx > ria->ria_stoff, "since %lu in the"
1325 " gap of ra window,it should bigger than stride"
1326 " offset %lu \n", page_idx, ria->ria_stoff);
1328 offset = page_idx - ria->ria_stoff;
1329 offset = offset % (ria->ria_length);
1330 if (offset > ria->ria_pages) {
1331 page_idx += ria->ria_length - offset;
1332 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
1333 ria->ria_length - offset);
1342 static int ll_readahead(struct ll_readahead_state *ras,
1343 struct obd_export *exp, struct address_space *mapping,
1344 struct obd_io_group *oig, int flags)
1346 unsigned long start = 0, end = 0, reserved;
1347 unsigned long ra_end, len;
1348 struct inode *inode;
1349 struct lov_stripe_md *lsm;
1350 struct ll_ra_read *bead;
1352 struct ra_io_arg ria = { 0 };
1357 inode = mapping->host;
1358 lsm = ll_i2info(inode)->lli_smd;
1360 lov_stripe_lock(lsm);
1361 inode_init_lvb(inode, &lvb);
1362 obd_merge_lvb(ll_i2dtexp(inode), lsm, &lvb, 1);
1364 lov_stripe_unlock(lsm);
1366 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
1370 spin_lock(&ras->ras_lock);
1371 bead = ll_ra_read_get_locked(ras);
1372 /* Enlarge the RA window to encompass the full read */
1373 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
1374 bead->lrr_start + bead->lrr_count) {
1375 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
1376 ras->ras_window_start;
1378 /* Reserve a part of the read-ahead window that we'll be issuing */
1379 if (ras->ras_window_len) {
1380 start = ras->ras_next_readahead;
1381 end = ras->ras_window_start + ras->ras_window_len - 1;
1384 /* Truncate RA window to end of file */
1385 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
1386 ras->ras_next_readahead = max(end, end + 1);
1389 ria.ria_start = start;
1391 /* If stride I/O mode is detected, get stride window*/
1392 if (stride_io_mode(ras)) {
1393 ria.ria_length = ras->ras_stride_length;
1394 ria.ria_pages = ras->ras_stride_pages;
1396 spin_unlock(&ras->ras_lock);
1399 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
1402 len = ria_page_count(&ria);
1406 reserved = ll_ra_count_get(ll_i2sbi(inode), len);
1408 if (reserved < end - start + 1)
1409 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
1411 CDEBUG(D_READA, "reserved page %lu \n", reserved);
1413 ret = ll_read_ahead_pages(exp, oig, &ria, &reserved, mapping, &ra_end);
1415 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
1417 ll_ra_count_put(ll_i2sbi(inode), reserved);
1419 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
1420 ll_ra_stats_inc(mapping, RA_STAT_EOF);
1422 /* if we didn't get to the end of the region we reserved from
1423 * the ras we need to go back and update the ras so that the
1424 * next read-ahead tries from where we left off. we only do so
1425 * if the region we failed to issue read-ahead on is still ahead
1426 * of the app and behind the next index to start read-ahead from */
1427 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
1428 ra_end, end, ria.ria_end);
1430 if (ra_end != (end + 1)) {
1431 spin_lock(&ras->ras_lock);
1432 if (ra_end < ras->ras_next_readahead &&
1433 index_in_window(ra_end, ras->ras_window_start, 0,
1434 ras->ras_window_len)) {
1435 ras->ras_next_readahead = ra_end;
1438 spin_unlock(&ras->ras_lock);
1444 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
1446 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
1449 /* called with the ras_lock held or from places where it doesn't matter */
1450 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
1452 ras->ras_last_readpage = index;
1453 ras->ras_consecutive_requests = 0;
1454 ras->ras_consecutive_pages = 0;
1455 ras->ras_window_len = 0;
1456 ras_set_start(ras, index);
1457 ras->ras_next_readahead = max(ras->ras_window_start, index);
1462 /* called with the ras_lock held or from places where it doesn't matter */
1463 static void ras_stride_reset(struct ll_readahead_state *ras)
1465 ras->ras_consecutive_stride_requests = 0;
1469 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
1471 spin_lock_init(&ras->ras_lock);
1473 ras->ras_requests = 0;
1474 INIT_LIST_HEAD(&ras->ras_read_beads);
1477 /* Check whether the read request is in the stride window.
1478 * If it is in the stride window, return 1, otherwise return 0.
1479 * and also update stride_gap and stride_pages.
1481 static int index_in_stride_window(unsigned long index,
1482 struct ll_readahead_state *ras,
1483 struct inode *inode)
1485 int stride_gap = index - ras->ras_last_readpage - 1;
1487 LASSERT(stride_gap != 0);
1489 if (ras->ras_consecutive_pages == 0)
1492 /*Otherwise check the stride by itself */
1493 if ((ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
1494 ras->ras_consecutive_pages == ras->ras_stride_pages)
1497 if (stride_gap >= 0) {
1499 * only set stride_pages, stride_length if
1500 * it is forward reading ( stride_gap > 0)
1502 ras->ras_stride_pages = ras->ras_consecutive_pages;
1503 ras->ras_stride_length = stride_gap + ras->ras_consecutive_pages;
1506 * If stride_gap < 0,(back_forward reading),
1507 * reset the stride_pages/length.
1508 * FIXME:back_ward stride I/O read.
1511 ras->ras_stride_pages = 0;
1512 ras->ras_stride_length = 0;
1519 static unsigned long
1520 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
1522 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
1523 ras->ras_stride_pages, ras->ras_stride_offset,
1527 /* Stride Read-ahead window will be increased inc_len according to
1528 * stride I/O pattern */
1529 static void ras_stride_increase_window(struct ll_readahead_state *ras,
1530 struct ll_ra_info *ra,
1531 unsigned long inc_len)
1533 unsigned long left, step, window_len;
1534 unsigned long stride_len;
1536 LASSERT(ras->ras_stride_length > 0);
1538 stride_len = ras->ras_window_start + ras->ras_window_len -
1539 ras->ras_stride_offset;
1541 LASSERTF(stride_len >= 0, "window_start %lu, window_len %lu"
1542 " stride_offset %lu\n", ras->ras_window_start,
1543 ras->ras_window_len, ras->ras_stride_offset);
1545 left = stride_len % ras->ras_stride_length;
1547 window_len = ras->ras_window_len - left;
1549 if (left < ras->ras_stride_pages)
1552 left = ras->ras_stride_pages + inc_len;
1554 LASSERT(ras->ras_stride_pages != 0);
1556 step = left / ras->ras_stride_pages;
1557 left %= ras->ras_stride_pages;
1559 window_len += step * ras->ras_stride_length + left;
1561 if (stride_page_count(ras, window_len) <= ra->ra_max_pages)
1562 ras->ras_window_len = window_len;
1567 /* Set stride I/O read-ahead window start offset */
1568 static void ras_set_stride_offset(struct ll_readahead_state *ras)
1570 unsigned long window_len = ras->ras_next_readahead -
1571 ras->ras_window_start;
1574 LASSERT(ras->ras_stride_length != 0);
1576 left = window_len % ras->ras_stride_length;
1578 ras->ras_stride_offset = ras->ras_next_readahead - left;
1583 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1584 struct ll_readahead_state *ras, unsigned long index,
1587 struct ll_ra_info *ra = &sbi->ll_ra_info;
1588 int zero = 0, stride_zero = 0, stride_detect = 0, ra_miss = 0;
1591 spin_lock(&sbi->ll_lock);
1592 spin_lock(&ras->ras_lock);
1594 ll_ra_stats_inc_unlocked(ra, hit ? RA_STAT_HIT : RA_STAT_MISS);
1596 /* reset the read-ahead window in two cases. First when the app seeks
1597 * or reads to some other part of the file. Secondly if we get a
1598 * read-ahead miss that we think we've previously issued. This can
1599 * be a symptom of there being so many read-ahead pages that the VM is
1600 * reclaiming it before we get to it. */
1601 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1603 ll_ra_stats_inc_unlocked(ra, RA_STAT_DISTANT_READPAGE);
1604 /* check whether it is in stride I/O mode*/
1605 if (!index_in_stride_window(index, ras, inode))
1607 } else if (!hit && ras->ras_window_len &&
1608 index < ras->ras_next_readahead &&
1609 index_in_window(index, ras->ras_window_start, 0,
1610 ras->ras_window_len)) {
1613 /* If it hits read-ahead miss and the stride I/O is still
1614 * not detected, reset stride stuff to re-detect the whole
1615 * stride I/O mode to avoid complication */
1616 if (!stride_io_mode(ras))
1618 ll_ra_stats_inc_unlocked(ra, RA_STAT_MISS_IN_WINDOW);
1621 /* On the second access to a file smaller than the tunable
1622 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1623 * file up to ra_max_pages. This is simply a best effort and
1624 * only occurs once per open file. Normal RA behavior is reverted
1625 * to for subsequent IO. The mmap case does not increment
1626 * ras_requests and thus can never trigger this behavior. */
1627 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1630 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1633 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1634 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages);
1637 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1638 ras->ras_window_start = 0;
1639 ras->ras_last_readpage = 0;
1640 ras->ras_next_readahead = 0;
1641 ras->ras_window_len = min(ra->ra_max_pages,
1642 ra->ra_max_read_ahead_whole_pages);
1643 GOTO(out_unlock, 0);
1648 /* If it is discontinuous read, check
1649 * whether it is stride I/O mode*/
1651 ras_reset(ras, index);
1652 ras->ras_consecutive_pages++;
1653 ras_stride_reset(ras);
1655 GOTO(out_unlock, 0);
1657 /* The read is still in stride window or
1658 * it hits read-ahead miss */
1660 /* If ra-window miss is hitted, which probably means VM
1661 * pressure, and some read-ahead pages were reclaimed.So
1662 * the length of ra-window will not increased, but also
1663 * not reset to avoid redetecting the stride I/O mode.*/
1664 ras->ras_consecutive_requests = 0;
1666 ras->ras_consecutive_pages = 0;
1667 if (++ras->ras_consecutive_stride_requests > 1)
1672 } else if (ras->ras_consecutive_stride_requests > 1) {
1673 /* If this is contiguous read but in stride I/O mode
1674 * currently, check whether stride step still is valid,
1675 * if invalid, it will reset the stride ra window*/
1676 if (ras->ras_consecutive_pages + 1 > ras->ras_stride_pages)
1677 ras_stride_reset(ras);
1680 ras->ras_last_readpage = index;
1681 ras->ras_consecutive_pages++;
1682 ras_set_start(ras, index);
1683 ras->ras_next_readahead = max(ras->ras_window_start,
1684 ras->ras_next_readahead);
1687 /* Trigger RA in the mmap case where ras_consecutive_requests
1688 * is not incremented and thus can't be used to trigger RA */
1689 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1690 ras->ras_window_len = RAS_INCREASE_STEP;
1691 GOTO(out_unlock, 0);
1694 /* Initially reset the stride window offset to next_readahead*/
1695 if (ras->ras_consecutive_stride_requests == 2 && stride_detect)
1696 ras_set_stride_offset(ras);
1698 /* The initial ras_window_len is set to the request size. To avoid
1699 * uselessly reading and discarding pages for random IO the window is
1700 * only increased once per consecutive request received. */
1701 if ((ras->ras_consecutive_requests > 1 &&
1702 !ras->ras_request_index) || stride_detect) {
1703 if (stride_io_mode(ras))
1704 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
1706 ras->ras_window_len = min(ras->ras_window_len +
1713 ras->ras_request_index++;
1714 spin_unlock(&ras->ras_lock);
1715 spin_unlock(&sbi->ll_lock);
1719 int ll_writepage(struct page *page)
1721 struct inode *inode = page->mapping->host;
1722 struct ll_inode_info *lli = ll_i2info(inode);
1723 struct obd_export *exp;
1724 struct ll_async_page *llap;
1728 LASSERT(PageLocked(page));
1730 exp = ll_i2dtexp(inode);
1732 GOTO(out, rc = -EINVAL);
1734 llap = llap_from_page(page, LLAP_ORIGIN_WRITEPAGE);
1736 GOTO(out, rc = PTR_ERR(llap));
1738 LASSERT(!PageWriteback(page));
1739 set_page_writeback(page);
1741 page_cache_get(page);
1742 if (llap->llap_write_queued) {
1743 LL_CDEBUG_PAGE(D_PAGE, page, "marking urgent\n");
1744 rc = obd_set_async_flags(exp, lli->lli_smd, NULL,
1746 ASYNC_READY | ASYNC_URGENT);
1748 rc = queue_or_sync_write(exp, inode, llap, CFS_PAGE_SIZE,
1749 ASYNC_READY | ASYNC_URGENT);
1752 page_cache_release(page);
1755 if (!lli->lli_async_rc)
1756 lli->lli_async_rc = rc;
1757 /* re-dirty page on error so it retries write */
1758 if (PageWriteback(page)) {
1759 end_page_writeback(page);
1761 /* resend page only for not started IO*/
1762 if (!PageError(page))
1763 ll_redirty_page(page);
1770 * for now we do our readpage the same on both 2.4 and 2.5. The kernel's
1771 * read-ahead assumes it is valid to issue readpage all the way up to
1772 * i_size, but our dlm locks make that not the case. We disable the
1773 * kernel's read-ahead and do our own by walking ahead in the page cache
1774 * checking for dlm lock coverage. the main difference between 2.4 and
1775 * 2.6 is how read-ahead gets batched and issued, but we're using our own,
1776 * so they look the same.
1778 int ll_readpage(struct file *filp, struct page *page)
1780 struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
1781 struct inode *inode = page->mapping->host;
1782 struct obd_export *exp;
1783 struct ll_async_page *llap;
1784 struct obd_io_group *oig = NULL;
1788 LASSERT(PageLocked(page));
1789 LASSERT(!PageUptodate(page));
1790 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset=%Lu=%#Lx\n",
1791 inode->i_ino, inode->i_generation, inode,
1792 (((loff_t)page->index) << CFS_PAGE_SHIFT),
1793 (((loff_t)page->index) << CFS_PAGE_SHIFT));
1794 LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
1796 if (!ll_i2info(inode)->lli_smd) {
1797 /* File with no objects - one big hole */
1798 /* We use this just for remove_from_page_cache that is not
1799 * exported, we'd make page back up to date. */
1800 ll_truncate_complete_page(page);
1801 clear_page(kmap(page));
1803 SetPageUptodate(page);
1808 rc = oig_init(&oig);
1812 exp = ll_i2dtexp(inode);
1814 GOTO(out, rc = -EINVAL);
1816 llap = llap_from_page(page, LLAP_ORIGIN_READPAGE);
1818 GOTO(out, rc = PTR_ERR(llap));
1820 if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1821 ras_update(ll_i2sbi(inode), inode, &fd->fd_ras, page->index,
1822 llap->llap_defer_uptodate);
1825 if (llap->llap_defer_uptodate) {
1826 /* This is the callpath if we got the page from a readahead */
1827 llap->llap_ra_used = 1;
1828 rc = ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1831 obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd,
1833 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
1834 SetPageUptodate(page);
1836 GOTO(out_oig, rc = 0);
1839 if (likely((fd->fd_flags & LL_FILE_IGNORE_LOCK) == 0)) {
1840 rc = ll_page_matches(page, fd->fd_flags);
1842 LL_CDEBUG_PAGE(D_ERROR, page,
1843 "lock match failed: rc %d\n", rc);
1848 CWARN("ino %lu page %lu (%llu) not covered by "
1849 "a lock (mmap?). check debug logs.\n",
1850 inode->i_ino, page->index,
1851 (long long)page->index << CFS_PAGE_SHIFT);
1855 rc = ll_issue_page_read(exp, llap, oig, 0);
1859 LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
1860 /* We have just requested the actual page we want, see if we can tack
1861 * on some readahead to that page's RPC before it is sent. */
1862 if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1863 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1866 rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);