1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Lite I/O page cache routines shared by different kernel revs
6 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <linux/unistd.h>
32 #include <linux/version.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
37 #include <linux/stat.h>
38 #include <asm/uaccess.h>
39 #include <asm/segment.h>
41 #include <linux/pagemap.h>
42 #include <linux/smp_lock.h>
44 #define DEBUG_SUBSYSTEM S_LLITE
46 #include <linux/lustre_mds.h>
47 #include <linux/lustre_lite.h>
48 #include "llite_internal.h"
49 #include <linux/lustre_compat25.h>
51 #ifndef list_for_each_prev_safe
52 #define list_for_each_prev_safe(pos, n, head) \
53 for (pos = (head)->prev, n = pos->prev; pos != (head); \
54 pos = n, n = pos->prev )
57 /* SYNCHRONOUS I/O to object storage for an inode */
58 static int ll_brw(int cmd, struct inode *inode, struct obdo *oa,
59 struct page *page, int flags)
61 struct ll_inode_info *lli = ll_i2info(inode);
62 struct lov_stripe_md *lsm = lli->lli_smd;
68 do_gettimeofday(&start);
71 pg.disk_offset = pg.page_offset = ((obd_off)page->index) << PAGE_SHIFT;
73 if (cmd == OBD_BRW_WRITE &&
74 (pg.disk_offset + PAGE_SIZE > inode->i_size))
75 pg.count = inode->i_size % PAGE_SIZE;
79 CDEBUG(D_PAGE, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
80 cmd & OBD_BRW_WRITE ? "write" : "read", pg.count, inode->i_ino,
81 pg.disk_offset, pg.disk_offset);
83 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
84 LPU64"\n", inode->i_ino, inode, inode->i_size,
85 page->mapping->host, page->mapping->host->i_size,
86 page->index, pg.disk_offset);
91 if (cmd == OBD_BRW_WRITE)
92 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
93 LPROC_LL_BRW_WRITE, pg.count);
95 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
96 LPROC_LL_BRW_READ, pg.count);
97 rc = obd_brw(cmd, ll_i2dtexp(inode), oa, lsm, 1, &pg, NULL);
99 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
101 CERROR("error from obd_brw: rc = %d\n", rc);
102 ll_stime_record(ll_i2sbi(inode), &start,
103 &ll_i2sbi(inode)->ll_brw_stime);
107 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
110 * this isn't where truncate starts. roughly:
111 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
112 * we grab the lock back in setattr_raw to avoid races.
114 * must be called with lli_size_sem held.
116 void ll_truncate(struct inode *inode)
118 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
119 struct ll_inode_info *lli = ll_i2info(inode);
120 struct obdo *oa = NULL;
124 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %llu\n", inode->i_ino,
125 inode->i_generation, inode, inode->i_size);
127 if (lli->lli_size_pid != current->pid) {
133 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
138 LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
140 if (lov_merge_size(lsm, 0) == inode->i_size) {
141 CDEBUG(D_VFSTRACE, "skipping punch for "LPX64" (size = %llu)\n",
142 lsm->lsm_object_id, inode->i_size);
146 CDEBUG(D_INFO, "calling punch for "LPX64" (new size %llu)\n",
147 lsm->lsm_object_id, inode->i_size);
151 CERROR("cannot alloc oa, error %d\n",
157 oa->o_id = lsm->lsm_object_id;
158 oa->o_gr = lsm->lsm_object_gr;
159 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLIFID;
160 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLMODE |
161 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
162 memcpy(obdo_id(oa), &lli->lli_id, sizeof(lli->lli_id));
164 obd_adjust_kms(ll_i2dtexp(inode), lsm, inode->i_size, 1);
166 lli->lli_size_pid = 0;
167 up(&lli->lli_size_sem);
169 rc = obd_punch(ll_i2dtexp(inode), oa, lsm, inode->i_size,
170 OBD_OBJECT_EOF, NULL);
172 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
174 obdo_to_inode(inode, oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
175 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
183 LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
184 up(&lli->lli_size_sem);
187 struct ll_async_page *llap_cast_private(struct page *page)
189 struct ll_async_page *llap = (struct ll_async_page *)page->private;
191 LASSERTF(llap == NULL || llap->llap_magic == LLAP_MAGIC,
192 "page %p private %lu gave magic %d which != %d\n",
193 page, page->private, llap->llap_magic, LLAP_MAGIC);
197 int ll_prepare_write(struct file *file, struct page *page,
198 unsigned from, unsigned to)
200 struct inode *inode = page->mapping->host;
201 struct ll_inode_info *lli = ll_i2info(inode);
202 struct lov_stripe_md *lsm = lli->lli_smd;
203 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
204 struct obdo *oa = NULL;
210 LASSERT(LLI_DIRTY_HANDLE(inode));
211 LASSERT(PageLocked(page));
212 (void)llap_cast_private(page); /* assertion */
214 /* Check to see if we should return -EIO right away */
216 pga.disk_offset = pga.page_offset = offset;
217 pga.count = PAGE_SIZE;
224 oa->o_id = lsm->lsm_object_id;
225 oa->o_gr = lsm->lsm_object_gr;
226 oa->o_mode = inode->i_mode;
228 oa->o_valid = OBD_MD_FLID | OBD_MD_FLMODE |
229 OBD_MD_FLTYPE | OBD_MD_FLGROUP;
231 rc = obd_brw(OBD_BRW_CHECK, ll_i2dtexp(inode),
232 oa, lsm, 1, &pga, NULL);
234 GOTO(out_free_oa, rc);
236 if (PageUptodate(page))
237 GOTO(out_free_oa, 0);
239 /* We're completely overwriting an existing page, so _don't_ set it up
240 * to date until commit_write */
241 if (from == 0 && to == PAGE_SIZE) {
242 POISON_PAGE(page, 0x11);
243 GOTO(out_free_oa, 0);
246 /* If are writing to a new page, no need to read old data. The extent
247 * locking will have updated the KMS, and for our purposes here we can
248 * treat it like i_size. */
249 down(&lli->lli_size_sem);
250 kms = lov_merge_size(lsm, 1);
251 up(&lli->lli_size_sem);
253 memset(kmap(page), 0, PAGE_SIZE);
255 GOTO(prepare_done, rc = 0);
258 /* XXX could be an async ocp read.. read-ahead? */
259 rc = ll_brw(OBD_BRW_READ, inode, oa, page, 0);
261 /* bug 1598: don't clobber blksize */
262 oa->o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
263 obdo_refresh_inode(inode, oa, oa->o_valid);
264 } else if (rc == -ENOENT) {
265 /* tolerate no entry error here, cause the objects might
266 * not be created yet */
273 SetPageUptodate(page);
279 static int ll_ap_make_ready(void *data, int cmd)
281 struct ll_async_page *llap;
285 llap = LLAP_FROM_COOKIE(data);
286 page = llap->llap_page;
288 LASSERT(cmd != OBD_BRW_READ);
290 /* we're trying to write, but the page is locked.. come back later */
291 if (TryLockPage(page))
294 LL_CDEBUG_PAGE(D_PAGE, page, "made ready\n");
295 page_cache_get(page);
297 /* if we left PageDirty we might get another writepage call
298 * in the future. list walkers are bright enough
299 * to check page dirty so we can leave it on whatever list
300 * its on. XXX also, we're called with the cli list so if
301 * we got the page cache list we'd create a lock inversion
302 * with the removepage path which gets the page lock then the
304 clear_page_dirty(page);
308 /* We have two reasons for giving llite the opportunity to change the
309 * write length of a given queued page as it builds the RPC containing
312 * 1) Further extending writes may have landed in the page cache
313 * since a partial write first queued this page requiring us
314 * to write more from the page cache. (No further races are possible, since
315 * by the time this is called, the page is locked.)
316 * 2) We might have raced with truncate and want to avoid performing
317 * write RPCs that are just going to be thrown away by the
318 * truncate's punch on the storage targets.
320 * The kms serves these purposes as it is set at both truncate and extending
323 static int ll_ap_refresh_count(void *data, int cmd)
325 struct ll_inode_info *lli;
326 struct ll_async_page *llap;
327 struct lov_stripe_md *lsm;
332 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
333 LASSERT(cmd != OBD_BRW_READ);
335 llap = LLAP_FROM_COOKIE(data);
336 page = llap->llap_page;
337 lli = ll_i2info(page->mapping->host);
341 * this callback is called with client lock taken, thus, it should not
342 * sleep or deadlock is possible. --umka
344 // down(&lli->lli_size_sem);
345 kms = lov_merge_size(lsm, 1);
346 // up(&lli->lli_size_sem);
348 /* catch race with truncate */
349 if (((__u64)page->index << PAGE_SHIFT) >= kms)
352 /* catch sub-page write at end of file */
353 if (((__u64)page->index << PAGE_SHIFT) + PAGE_SIZE > kms)
354 return kms % PAGE_SIZE;
359 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
361 struct lov_stripe_md *lsm;
362 obd_valid valid_flags;
364 lsm = ll_i2info(inode)->lli_smd;
366 oa->o_id = lsm->lsm_object_id;
367 oa->o_gr = lsm->lsm_object_gr;
368 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
369 valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
370 if (cmd == OBD_BRW_WRITE || cmd == OBD_BRW_READ) {
371 oa->o_valid |= OBD_MD_FLIFID | OBD_MD_FLEPOCH;
372 *(obdo_id(oa)) = ll_i2info(inode)->lli_id;
373 oa->o_easize = ll_i2info(inode)->lli_io_epoch;
374 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
377 obdo_from_inode(oa, inode, valid_flags);
380 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
382 struct ll_async_page *llap;
385 llap = LLAP_FROM_COOKIE(data);
386 ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
390 static struct obd_async_page_ops ll_async_page_ops = {
391 .ap_make_ready = ll_ap_make_ready,
392 .ap_refresh_count = ll_ap_refresh_count,
393 .ap_fill_obdo = ll_ap_fill_obdo,
394 .ap_completion = ll_ap_completion,
398 /* XXX have the exp be an argument? */
399 struct ll_async_page *llap_from_page(struct page *page, unsigned origin)
401 struct ll_async_page *llap;
402 struct obd_export *exp;
403 struct inode *inode = page->mapping->host;
404 struct ll_sb_info *sbi = ll_i2sbi(inode);
408 LASSERTF(origin < LLAP__ORIGIN_MAX, "%u\n", origin);
410 llap = llap_cast_private(page);
414 exp = ll_i2dtexp(page->mapping->host);
416 RETURN(ERR_PTR(-EINVAL));
418 OBD_ALLOC(llap, sizeof(*llap));
420 RETURN(ERR_PTR(-ENOMEM));
422 llap->llap_magic = LLAP_MAGIC;
423 INIT_LIST_HEAD(&llap->llap_pending_write);
424 rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
425 (obd_off)page->index << PAGE_SHIFT,
426 &ll_async_page_ops, llap, &llap->llap_cookie);
428 OBD_FREE(llap, sizeof(*llap));
432 CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
433 page, llap->llap_cookie, (obd_off)page->index << PAGE_SHIFT);
435 __set_page_ll_data(page, llap);
437 /* also zeroing the PRIVBITS low order bitflags */
438 llap->llap_page = page;
440 spin_lock(&sbi->ll_lock);
441 sbi->ll_pglist_gen++;
442 list_add_tail(&llap->llap_proc_item, &sbi->ll_pglist);
443 spin_unlock(&sbi->ll_lock);
446 llap->llap_origin = origin;
450 static int queue_or_sync_write(struct obd_export *exp,
451 struct lov_stripe_md *lsm,
452 struct ll_async_page *llap,
454 obd_flags async_flags)
456 struct obd_io_group *oig;
460 /* _make_ready only sees llap once we've unlocked the page */
461 llap->llap_write_queued = 1;
462 rc = obd_queue_async_io(exp, lsm, NULL, llap->llap_cookie,
463 OBD_BRW_WRITE, 0, 0, 0, async_flags);
465 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "write queued\n");
466 llap_write_pending(llap->llap_page->mapping->host, llap);
470 llap->llap_write_queued = 0;
475 rc = obd_queue_group_io(exp, lsm, NULL, oig, llap->llap_cookie,
476 OBD_BRW_WRITE, 0, to, 0, ASYNC_READY |
477 ASYNC_URGENT | ASYNC_COUNT_STABLE |
482 rc = obd_trigger_group_io(exp, lsm, NULL, oig);
488 if (!rc && async_flags & ASYNC_READY)
489 unlock_page(llap->llap_page);
491 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
492 "sync write returned %d\n", rc);
501 /* be careful not to return success without setting the page Uptodate or
502 * the next pass through prepare_write will read in stale data from disk. */
503 int ll_commit_write(struct file *file, struct page *page, unsigned from,
506 struct inode *inode = page->mapping->host;
507 struct ll_inode_info *lli = ll_i2info(inode);
508 struct lov_stripe_md *lsm = lli->lli_smd;
509 struct obd_export *exp = NULL;
510 struct ll_async_page *llap;
515 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
516 LASSERT(inode == file->f_dentry->d_inode);
517 LASSERT(PageLocked(page));
518 LASSERT(LLI_DIRTY_HANDLE(inode));
520 CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
521 inode, page, from, to, page->index);
523 llap = llap_from_page(page, LLAP_ORIGIN_COMMIT_WRITE);
525 RETURN(PTR_ERR(llap));
527 exp = ll_i2dtexp(inode);
531 /* queue a write for some time in the future the first time we
533 if (!PageDirty(page)) {
534 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
535 LPROC_LL_DIRTY_MISSES);
537 rc = queue_or_sync_write(exp, ll_i2info(inode)->lli_smd,
542 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
543 LPROC_LL_DIRTY_HITS);
546 /* put the page in the page cache, from now on ll_removepage is
547 * responsible for cleaning up the llap.
548 * don't dirty the page if it has been write out in q_o_s_w */
549 if (llap->llap_write_queued)
550 set_page_dirty(page);
553 size = (((obd_off)page->index) << PAGE_SHIFT) + to;
554 down(&lli->lli_size_sem);
556 obd_adjust_kms(exp, lsm, size, 0);
557 if (size > inode->i_size)
558 inode->i_size = size;
559 SetPageUptodate(page);
560 } else if (size > inode->i_size) {
561 /* this page beyond the pales of i_size, so it can't be
562 * truncated in ll_p_r_e during lock revoking. we must
563 * teardown our book-keeping here. */
566 up(&lli->lli_size_sem);
570 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
572 struct ll_ra_info *ra = &sbi->ll_ra_info;
576 spin_lock(&sbi->ll_lock);
577 ret = min(ra->ra_max_pages - ra->ra_cur_pages, len);
578 ra->ra_cur_pages += ret;
579 spin_unlock(&sbi->ll_lock);
584 static void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
586 struct ll_ra_info *ra = &sbi->ll_ra_info;
587 spin_lock(&sbi->ll_lock);
588 LASSERTF(ra->ra_cur_pages >= len, "r_c_p %lu len %lu\n",
589 ra->ra_cur_pages, len);
590 ra->ra_cur_pages -= len;
591 spin_unlock(&sbi->ll_lock);
594 int ll_writepage(struct page *page)
596 struct inode *inode = page->mapping->host;
597 struct obd_export *exp;
598 struct ll_async_page *llap;
602 LASSERT(!PageDirty(page));
603 LASSERT(PageLocked(page));
604 LASSERT(LLI_DIRTY_HANDLE(inode));
606 exp = ll_i2dtexp(inode);
608 GOTO(out, rc = -EINVAL);
610 llap = llap_from_page(page, LLAP_ORIGIN_WRITEPAGE);
612 GOTO(out, rc = PTR_ERR(llap));
614 page_cache_get(page);
615 if (llap->llap_write_queued) {
616 LL_CDEBUG_PAGE(D_PAGE, page, "marking urgent\n");
617 rc = obd_set_async_flags(exp, ll_i2info(inode)->lli_smd, NULL,
619 ASYNC_READY | ASYNC_URGENT);
621 rc = queue_or_sync_write(exp, ll_i2info(inode)->lli_smd, llap,
622 PAGE_SIZE, ASYNC_READY |
626 page_cache_release(page);
634 /* called for each page in a completed rpc.*/
635 void ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
637 struct ll_async_page *llap;
641 llap = LLAP_FROM_COOKIE(data);
642 page = llap->llap_page;
643 LASSERT(PageLocked(page));
645 LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
647 if (cmd == OBD_BRW_READ && llap->llap_defer_uptodate)
648 ll_ra_count_put(ll_i2sbi(page->mapping->host), 1);
651 if (cmd == OBD_BRW_READ) {
652 if (!llap->llap_defer_uptodate)
653 SetPageUptodate(page);
655 llap->llap_write_queued = 0;
657 ClearPageError(page);
659 if (cmd == OBD_BRW_READ)
660 llap->llap_defer_uptodate = 0;
666 if (cmd == OBD_BRW_WRITE) {
667 llap_write_complete(page->mapping->host, llap);
668 ll_try_done_writing(page->mapping->host);
671 if (PageWriteback(page)) {
672 end_page_writeback(page);
674 page_cache_release(page);
678 /* the kernel calls us here when a page is unhashed from the page cache.
679 * the page will be locked and the kernel is holding a spinlock, so
680 * we need to be careful. we're just tearing down our book-keeping
682 void ll_removepage(struct page *page)
684 struct inode *inode = page->mapping->host;
685 struct obd_export *exp;
686 struct ll_async_page *llap;
687 struct ll_sb_info *sbi = ll_i2sbi(inode);
691 LASSERT(!in_interrupt());
693 /* sync pages or failed read pages can leave pages in the page
694 * cache that don't have our data associated with them anymore */
695 if (page->private == 0) {
700 LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
702 exp = ll_i2dtexp(inode);
704 CERROR("page %p ind %lu gave null export\n", page, page->index);
708 llap = llap_from_page(page, 0);
710 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
711 page->index, PTR_ERR(llap));
716 llap_write_complete(inode, llap);
717 rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
720 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
722 /* this unconditional free is only safe because the page lock
723 * is providing exclusivity to memory pressure/truncate/writeback..*/
724 __clear_page_ll_data(page);
726 spin_lock(&sbi->ll_lock);
727 if (!list_empty(&llap->llap_proc_item))
728 list_del_init(&llap->llap_proc_item);
729 sbi->ll_pglist_gen++;
730 spin_unlock(&sbi->ll_lock);
731 OBD_FREE(llap, sizeof(*llap));
735 static int ll_page_matches(struct page *page, int fd_flags, int readahead)
737 struct lustre_handle match_lockh = {0};
738 struct inode *inode = page->mapping->host;
739 ldlm_policy_data_t page_extent;
743 if (fd_flags & LL_FILE_GROUP_LOCKED)
746 page_extent.l_extent.start = (__u64)page->index << PAGE_CACHE_SHIFT;
747 page_extent.l_extent.end =
748 page_extent.l_extent.start + PAGE_CACHE_SIZE - 1;
749 flags = LDLM_FL_TEST_LOCK;
751 flags |= LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
752 matches = obd_match(ll_i2sbi(inode)->ll_dt_exp,
753 ll_i2info(inode)->lli_smd, LDLM_EXTENT,
754 &page_extent, LCK_PR | LCK_PW, &flags, inode,
759 static int ll_issue_page_read(struct obd_export *exp,
760 struct ll_async_page *llap,
761 struct obd_io_group *oig, int defer)
763 struct page *page = llap->llap_page;
766 page_cache_get(page);
767 llap->llap_defer_uptodate = defer;
768 llap->llap_ra_used = 0;
770 rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
771 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
772 PAGE_SIZE, 0, ASYNC_COUNT_STABLE | ASYNC_READY
775 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
776 page_cache_release(page);
781 static void ll_ra_stats_inc_unlocked(struct ll_ra_info *ra, enum ra_stat which)
783 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
784 ra->ra_stats[which]++;
787 static void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
789 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
790 struct ll_ra_info *ra = &ll_i2sbi(mapping->host)->ll_ra_info;
792 spin_lock(&sbi->ll_lock);
793 ll_ra_stats_inc_unlocked(ra, which);
794 spin_unlock(&sbi->ll_lock);
797 void ll_ra_accounting(struct page *page, struct address_space *mapping)
799 struct ll_async_page *llap;
801 llap = llap_from_page(page, LLAP_ORIGIN_WRITEPAGE);
805 if (!llap->llap_defer_uptodate || llap->llap_ra_used)
808 ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
811 #define RAS_CDEBUG(ras) \
812 CDEBUG(D_READA, "lrp %lu c %lu ws %lu wl %lu nra %lu\n", \
813 ras->ras_last_readpage, ras->ras_consecutive, \
814 ras->ras_window_start, ras->ras_window_len, \
815 ras->ras_next_readahead);
817 static int index_in_window(unsigned long index, unsigned long point,
818 unsigned long before, unsigned long after)
820 unsigned long start = point - before, end = point + after;
827 return start <= index && index <= end;
830 static int ll_readahead(struct ll_readahead_state *ras,
831 struct obd_export *exp, struct address_space *mapping,
832 struct obd_io_group *oig, int flags)
834 unsigned long i, start = 0, end = 0, reserved;
835 struct ll_async_page *llap;
837 int rc, ret = 0, match_failed = 0;
841 kms = lov_merge_size(ll_i2info(mapping->host)->lli_smd, 1);
843 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
846 spin_lock(&ras->ras_lock);
848 /* reserve a part of the read-ahead window that we'll be issuing */
849 if (ras->ras_window_len) {
850 start = ras->ras_next_readahead;
851 end = ras->ras_window_start + ras->ras_window_len - 1;
852 end = min(end, (unsigned long)(kms >> PAGE_CACHE_SHIFT));
853 ras->ras_next_readahead = max(end, end + 1);
858 spin_unlock(&ras->ras_lock);
861 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
865 reserved = ll_ra_count_get(ll_i2sbi(mapping->host), end - start + 1);
866 if (reserved < end - start + 1)
867 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
869 for (i = start; reserved > 0 && !match_failed && i <= end; i++) {
870 /* skip locked pages from previous readpage calls */
871 page = grab_cache_page_nowait(mapping, i);
873 CDEBUG(D_READA, "g_c_p_n failed\n");
877 /* we do this first so that we can see the page in the /proc
879 llap = llap_from_page(page, LLAP_ORIGIN_READAHEAD);
880 if (IS_ERR(llap) || llap->llap_defer_uptodate)
883 /* skip completed pages */
884 if (Page_Uptodate(page))
887 /* bail when we hit the end of the lock. */
888 if ((rc = ll_page_matches(page, flags, 1)) <= 0) {
889 LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
890 "lock match failed: rc %d\n", rc);
891 ll_ra_stats_inc(mapping, RA_STAT_FAILED_MATCH);
896 rc = ll_issue_page_read(exp, llap, oig, 1);
900 LL_CDEBUG_PAGE(D_READA| D_PAGE, page,
901 "started read-ahead\n");
905 LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
906 "skipping read-ahead\n");
910 page_cache_release(page);
913 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
915 ll_ra_count_put(ll_i2sbi(mapping->host), reserved);
917 if (i == end + 1 && end == (kms >> PAGE_CACHE_SHIFT))
918 ll_ra_stats_inc(mapping, RA_STAT_EOF);
920 /* if we didn't get to the end of the region we reserved from
921 * the ras we need to go back and update the ras so that the
922 * next read-ahead tries from where we left off. we only do so
923 * if the region we failed to issue read-ahead on is still ahead
924 * of the app and behind the next index to start read-ahead from */
926 spin_lock(&ras->ras_lock);
927 if (i < ras->ras_next_readahead &&
928 index_in_window(i, ras->ras_window_start, 0,
929 ras->ras_window_len)) {
930 ras->ras_next_readahead = i;
933 spin_unlock(&ras->ras_lock);
939 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
941 ras->ras_window_start = index & (~(PTLRPC_MAX_BRW_PAGES - 1));
944 /* called with the ras_lock held or from places where it doesn't matter */
945 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
947 ras->ras_last_readpage = index;
948 ras->ras_consecutive = 1;
949 ras->ras_window_len = 0;
950 ras_set_start(ras, index);
951 ras->ras_next_readahead = ras->ras_window_start;
956 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
958 spin_lock_init(&ras->ras_lock);
962 static void ras_update(struct ll_sb_info *sbi, struct ll_readahead_state *ras,
963 unsigned long index, unsigned hit)
965 struct ll_ra_info *ra = &sbi->ll_ra_info;
969 spin_lock(&sbi->ll_lock);
970 spin_lock(&ras->ras_lock);
972 ll_ra_stats_inc_unlocked(ra, hit ? RA_STAT_HIT : RA_STAT_MISS);
974 /* reset the read-ahead window in two cases. First when the app seeks
975 * or reads to some other part of the file. Secondly if we get a
976 * read-ahead miss that we think we've previously issued. This can
977 * be a symptom of there being so many read-ahead pages that the VM is
978 * reclaiming it before we get to it. */
979 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
981 ll_ra_stats_inc_unlocked(ra, RA_STAT_DISTANT_READPAGE);
982 } else if (!hit && ras->ras_window_len &&
983 index < ras->ras_next_readahead &&
984 index_in_window(index, ras->ras_window_start, 0,
985 ras->ras_window_len)) {
987 ll_ra_stats_inc_unlocked(ra, RA_STAT_MISS_IN_WINDOW);
991 ras_reset(ras, index);
995 ras->ras_last_readpage = index;
996 ras->ras_consecutive++;
997 ras_set_start(ras, index);
998 ras->ras_next_readahead = max(ras->ras_window_start,
999 ras->ras_next_readahead);
1001 /* wait for a few pages to arrive before issuing readahead to avoid
1002 * the worst overutilization */
1003 if (ras->ras_consecutive == 3) {
1004 ras->ras_window_len = PTLRPC_MAX_BRW_PAGES;
1005 GOTO(out_unlock, 0);
1008 /* we need to increase the window sometimes. we'll arbitrarily
1009 * do it half-way through the pages in an rpc */
1010 if ((index & (PTLRPC_MAX_BRW_PAGES - 1)) ==
1011 (PTLRPC_MAX_BRW_PAGES >> 1)) {
1012 ras->ras_window_len += PTLRPC_MAX_BRW_PAGES;
1013 ras->ras_window_len = min(ras->ras_window_len,
1021 spin_unlock(&ras->ras_lock);
1022 spin_unlock(&sbi->ll_lock);
1026 * for now we do our readpage the same on both 2.4 and 2.5. The kernel's
1027 * read-ahead assumes it is valid to issue readpage all the way up to
1028 * i_size, but our dlm locks make that not the case. We disable the
1029 * kernel's read-ahead and do our own by walking ahead in the page cache
1030 * checking for dlm lock coverage. the main difference between 2.4 and
1031 * 2.6 is how read-ahead gets batched and issued, but we're using our own,
1032 * so they look the same.
1034 int ll_readpage(struct file *filp, struct page *page)
1036 struct ll_file_data *fd = filp->private_data;
1037 struct inode *inode = page->mapping->host;
1038 struct obd_export *exp;
1039 struct ll_async_page *llap;
1040 struct obd_io_group *oig = NULL;
1044 LASSERT(PageLocked(page));
1045 LASSERT(!PageUptodate(page));
1046 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset="LPX64"\n",
1047 inode->i_ino, inode->i_generation, inode,
1048 (((obd_off)page->index) << PAGE_SHIFT));
1049 LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
1051 rc = oig_init(&oig);
1055 exp = ll_i2dtexp(inode);
1057 GOTO(out, rc = -EINVAL);
1059 llap = llap_from_page(page, LLAP_ORIGIN_READPAGE);
1061 GOTO(out, rc = PTR_ERR(llap));
1063 if (ll_i2sbi(inode)->ll_flags & LL_SBI_READAHEAD)
1064 ras_update(ll_i2sbi(inode), &fd->fd_ras, page->index,
1065 llap->llap_defer_uptodate);
1067 if (llap->llap_defer_uptodate) {
1068 llap->llap_ra_used = 1;
1069 rc = ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1072 obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd,
1074 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
1075 SetPageUptodate(page);
1077 GOTO(out_oig, rc = 0);
1080 rc = ll_page_matches(page, fd->fd_flags, 0);
1082 LL_CDEBUG_PAGE(D_ERROR, page, "lock match failed: rc %d\n", rc);
1087 CWARN("ino %lu page %lu (%llu) not covered by "
1088 "a lock (mmap?). check debug logs.\n",
1089 inode->i_ino, page->index,
1090 (long long)page->index << PAGE_CACHE_SHIFT);
1093 rc = ll_issue_page_read(exp, llap, oig, 0);
1097 LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
1098 if (ll_i2sbi(inode)->ll_flags & LL_SBI_READAHEAD)
1099 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1102 rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);