1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Lite I/O page cache routines shared by different kernel revs
6 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <linux/unistd.h>
32 #include <linux/version.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
37 #include <linux/stat.h>
38 #include <asm/uaccess.h>
39 #include <asm/segment.h>
41 #include <linux/pagemap.h>
42 #include <linux/smp_lock.h>
44 #define DEBUG_SUBSYSTEM S_LLITE
46 #include <linux/lustre_mds.h>
47 #include <linux/lustre_lite.h>
48 #include "llite_internal.h"
49 #include <linux/lustre_compat25.h>
51 #ifndef list_for_each_prev_safe
52 #define list_for_each_prev_safe(pos, n, head) \
53 for (pos = (head)->prev, n = pos->prev; pos != (head); \
54 pos = n, n = pos->prev )
57 kmem_cache_t *ll_async_page_slab = NULL;
58 size_t ll_async_page_slab_size = 0;
60 /* SYNCHRONOUS I/O to object storage for an inode */
61 static int ll_brw(int cmd, struct inode *inode, struct obdo *oa,
62 struct page *page, int flags)
64 struct ll_inode_info *lli = ll_i2info(inode);
65 struct lov_stripe_md *lsm = lli->lli_smd;
71 pg.off = ((obd_off)page->index) << PAGE_SHIFT;
73 if ((cmd & OBD_BRW_WRITE) && (pg.off + PAGE_SIZE > inode->i_size))
74 pg.count = inode->i_size % PAGE_SIZE;
78 LL_CDEBUG_PAGE(D_PAGE, page, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
79 cmd & OBD_BRW_WRITE ? "write" : "read", pg.count,
80 inode->i_ino, pg.off, pg.off);
82 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
84 inode->i_ino, inode, inode->i_size, page->mapping->host,
85 page->mapping->host->i_size, page->index, pg.off);
90 if (cmd & OBD_BRW_WRITE)
91 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
92 LPROC_LL_BRW_WRITE, pg.count);
94 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
95 LPROC_LL_BRW_READ, pg.count);
96 rc = obd_brw(cmd, ll_i2obdexp(inode), oa, lsm, 1, &pg, NULL);
98 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
100 CERROR("error from obd_brw: rc = %d\n", rc);
104 /* this isn't where truncate starts. roughly:
105 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
106 * we grab the lock back in setattr_raw to avoid races.
108 * must be called with lli_size_sem held */
109 void ll_truncate(struct inode *inode)
111 struct ll_inode_info *lli = ll_i2info(inode);
112 struct lov_stripe_md *lsm = lli->lli_smd;
116 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %Lu=%#Lx\n",inode->i_ino,
117 inode->i_generation, inode, inode->i_size, inode->i_size);
119 if (lli->lli_size_pid != current->pid) {
125 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
130 LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
132 /* XXX I'm pretty sure this is a hack to paper over a more fundamental
134 if (lov_merge_size(lsm, 0) == inode->i_size) {
135 CDEBUG(D_VFSTRACE, "skipping punch for obj "LPX64", %Lu=%#Lx\n",
136 lsm->lsm_object_id, inode->i_size, inode->i_size);
140 if (unlikely((ll_i2sbi(inode)->ll_flags & LL_SBI_CHECKSUM) &&
141 (inode->i_size & ~PAGE_MASK))) {
142 /* If the truncate leaves behind a partial page, update its
144 struct page *page = find_get_page(inode->i_mapping,
145 inode->i_size >> PAGE_CACHE_SHIFT);
147 struct ll_async_page *llap = llap_cast_private(page);
149 llap->llap_checksum =
150 crc32_le(0, kmap(page), PAGE_SIZE);
153 page_cache_release(page);
157 CDEBUG(D_INFO, "calling punch for "LPX64" (new size %Lu=%#Lx)\n",
158 lsm->lsm_object_id, inode->i_size, inode->i_size);
160 oa.o_id = lsm->lsm_object_id;
161 oa.o_valid = OBD_MD_FLID;
162 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLMODE |
163 OBD_MD_FLATIME |OBD_MD_FLMTIME |OBD_MD_FLCTIME);
165 obd_adjust_kms(ll_i2obdexp(inode), lsm, inode->i_size, 1);
167 lli->lli_size_pid = 0;
168 up(&lli->lli_size_sem);
170 rc = obd_punch(ll_i2obdexp(inode), &oa, lsm, inode->i_size,
171 OBD_OBJECT_EOF, NULL);
173 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
175 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE|OBD_MD_FLBLOCKS|
176 OBD_MD_FLATIME | OBD_MD_FLMTIME |
182 lli->lli_size_pid = 0;
183 up(&lli->lli_size_sem);
186 int ll_prepare_write(struct file *file, struct page *page, unsigned from,
189 struct inode *inode = page->mapping->host;
190 struct ll_inode_info *lli = ll_i2info(inode);
191 struct lov_stripe_md *lsm = lli->lli_smd;
192 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
199 LASSERT(PageLocked(page));
200 (void)llap_cast_private(page); /* assertion */
202 /* Check to see if we should return -EIO right away */
205 pga.count = PAGE_SIZE;
208 oa.o_id = lsm->lsm_object_id;
209 oa.o_mode = inode->i_mode;
210 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
212 rc = obd_brw(OBD_BRW_CHECK, ll_i2obdexp(inode), &oa, lsm, 1, &pga,
217 if (PageUptodate(page)) {
218 LL_CDEBUG_PAGE(D_PAGE, page, "uptodate\n");
222 /* We're completely overwriting an existing page, so _don't_ set it up
223 * to date until commit_write */
224 if (from == 0 && to == PAGE_SIZE) {
225 LL_CDEBUG_PAGE(D_PAGE, page, "full page write\n");
226 POISON_PAGE(page, 0x11);
230 /* If are writing to a new page, no need to read old data. The extent
231 * locking will have updated the KMS, and for our purposes here we can
232 * treat it like i_size. */
233 down(&lli->lli_size_sem);
234 kms = lov_merge_size(lsm, 1);
235 up(&lli->lli_size_sem);
237 LL_CDEBUG_PAGE(D_PAGE, page, "kms "LPU64" <= offset "LPU64"\n",
239 memset(kmap(page), 0, PAGE_SIZE);
241 GOTO(prepare_done, rc = 0);
244 /* XXX could be an async ocp read.. read-ahead? */
245 rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
247 /* bug 1598: don't clobber blksize */
248 oa.o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
249 obdo_refresh_inode(inode, &oa, oa.o_valid);
255 SetPageUptodate(page);
260 static int ll_ap_make_ready(void *data, int cmd)
262 struct ll_async_page *llap;
266 llap = LLAP_FROM_COOKIE(data);
267 page = llap->llap_page;
269 LASSERT(!(cmd & OBD_BRW_READ));
271 /* we're trying to write, but the page is locked.. come back later */
272 if (TryLockPage(page))
275 LL_CDEBUG_PAGE(D_PAGE, page, "made ready\n");
276 page_cache_get(page);
278 /* if we left PageDirty we might get another writepage call
279 * in the future. list walkers are bright enough
280 * to check page dirty so we can leave it on whatever list
281 * its on. XXX also, we're called with the cli list so if
282 * we got the page cache list we'd create a lock inversion
283 * with the removepage path which gets the page lock then the
285 clear_page_dirty(page);
289 /* We have two reasons for giving llite the opportunity to change the
290 * write length of a given queued page as it builds the RPC containing
293 * 1) Further extending writes may have landed in the page cache
294 * since a partial write first queued this page requiring us
295 * to write more from the page cache. (No further races are possible, since
296 * by the time this is called, the page is locked.)
297 * 2) We might have raced with truncate and want to avoid performing
298 * write RPCs that are just going to be thrown away by the
299 * truncate's punch on the storage targets.
301 * The kms serves these purposes as it is set at both truncate and extending
304 static int ll_ap_refresh_count(void *data, int cmd)
306 struct ll_inode_info *lli;
307 struct ll_async_page *llap;
308 struct lov_stripe_md *lsm;
313 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
314 LASSERT(cmd != OBD_BRW_READ);
316 llap = LLAP_FROM_COOKIE(data);
317 page = llap->llap_page;
318 lli = ll_i2info(page->mapping->host);
321 //down(&lli->lli_size_sem);
322 kms = lov_merge_size(lsm, 1);
323 //up(&lli->lli_size_sem);
325 /* catch race with truncate */
326 if (((__u64)page->index << PAGE_SHIFT) >= kms)
329 /* catch sub-page write at end of file */
330 if (((__u64)page->index << PAGE_SHIFT) + PAGE_SIZE > kms)
331 return kms % PAGE_SIZE;
336 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
338 struct lov_stripe_md *lsm;
339 obd_flag valid_flags;
341 lsm = ll_i2info(inode)->lli_smd;
343 oa->o_id = lsm->lsm_object_id;
344 oa->o_valid = OBD_MD_FLID;
345 valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
346 if (cmd & OBD_BRW_WRITE) {
347 oa->o_valid |= OBD_MD_FLIFID | OBD_MD_FLEPOCH;
348 mdc_pack_fid(obdo_fid(oa), inode->i_ino, 0, inode->i_mode);
349 oa->o_easize = ll_i2info(inode)->lli_io_epoch;
350 oa->o_uid = inode->i_uid;
351 oa->o_gid = inode->i_gid;
353 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
354 OBD_MD_FLUID | OBD_MD_FLGID;
357 obdo_from_inode(oa, inode, valid_flags);
360 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
362 struct ll_async_page *llap;
365 llap = LLAP_FROM_COOKIE(data);
366 ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
370 static struct obd_async_page_ops ll_async_page_ops = {
371 .ap_make_ready = ll_ap_make_ready,
372 .ap_refresh_count = ll_ap_refresh_count,
373 .ap_fill_obdo = ll_ap_fill_obdo,
374 .ap_completion = ll_ap_completion,
377 struct ll_async_page *llap_cast_private(struct page *page)
379 struct ll_async_page *llap = (struct ll_async_page *)page->private;
381 LASSERTF(llap == NULL || llap->llap_magic == LLAP_MAGIC,
382 "page %p private %lu gave magic %d which != %d\n",
383 page, page->private, llap->llap_magic, LLAP_MAGIC);
388 /* Try to shrink the page cache for the @sbi filesystem by 1/@shrink_fraction.
390 * There is an llap attached onto every page in lustre, linked off @sbi.
391 * We add an llap to the list so we don't lose our place during list walking.
392 * If llaps in the list are being moved they will only move to the end
393 * of the LRU, and we aren't terribly interested in those pages here (we
394 * start at the beginning of the list where the least-used llaps are.
396 int llap_shrink_cache(struct ll_sb_info *sbi, int shrink_fraction)
398 struct ll_async_page *llap, dummy_llap = { .llap_magic = 0xd11ad11a };
399 unsigned long total, want, count = 0;
401 total = sbi->ll_async_page_count;
403 /* There can be a large number of llaps (600k or more in a large
404 * memory machine) so the VM 1/6 shrink ratio is likely too much.
405 * Since we are freeing pages also, we don't necessarily want to
406 * shrink so much. Limit to 40MB of pages + llaps per call. */
407 if (shrink_fraction == 0)
408 want = sbi->ll_async_page_count - sbi->ll_async_page_max + 32;
410 want = (total + shrink_fraction - 1) / shrink_fraction;
412 if (want > 40 << (20 - PAGE_CACHE_SHIFT))
413 want = 40 << (20 - PAGE_CACHE_SHIFT);
415 CDEBUG(D_CACHE, "shrinking %lu of %lu pages (1/%d)\n",
416 want, total, shrink_fraction);
418 spin_lock(&sbi->ll_lock);
419 list_add(&dummy_llap.llap_pglist_item, &sbi->ll_pglist);
421 while (--total >= 0 && count < want) {
425 if (unlikely(need_resched())) {
426 spin_unlock(&sbi->ll_lock);
428 spin_lock(&sbi->ll_lock);
431 llap = llite_pglist_next_llap(sbi,&dummy_llap.llap_pglist_item);
432 list_del_init(&dummy_llap.llap_pglist_item);
436 page = llap->llap_page;
437 LASSERT(page != NULL);
439 list_add(&dummy_llap.llap_pglist_item, &llap->llap_pglist_item);
441 /* Page needs/undergoing IO */
442 if (TryLockPage(page)) {
443 LL_CDEBUG_PAGE(D_PAGE, page, "can't lock\n");
447 if (llap->llap_write_queued || PageDirty(page) ||
448 (!PageUptodate(page) &&
449 llap->llap_origin != LLAP_ORIGIN_READAHEAD))
454 LL_CDEBUG_PAGE(D_PAGE, page,"%s LRU page: %s%s%s%s origin %s\n",
455 keep ? "keep" : "drop",
456 llap->llap_write_queued ? "wq " : "",
457 PageDirty(page) ? "pd " : "",
458 PageUptodate(page) ? "" : "!pu ",
459 llap->llap_defer_uptodate ? "" : "!du",
460 llap_origins[llap->llap_origin]);
462 /* If page is dirty or undergoing IO don't discard it */
468 page_cache_get(page);
469 spin_unlock(&sbi->ll_lock);
472 if (page->mapping != NULL) {
473 ll_ra_accounting(page, page->mapping);
474 ll_truncate_complete_page(page);
477 page_cache_release(page);
479 spin_lock(&sbi->ll_lock);
481 list_del(&dummy_llap.llap_pglist_item);
482 spin_unlock(&sbi->ll_lock);
484 CDEBUG(D_CACHE, "shrank %lu/%lu and left %lu unscanned\n",
490 struct ll_async_page *llap_from_page(struct page *page, unsigned origin)
492 struct ll_async_page *llap;
493 struct obd_export *exp;
494 struct inode *inode = page->mapping->host;
495 struct ll_sb_info *sbi = ll_i2sbi(inode);
499 LASSERT(ll_async_page_slab);
500 LASSERTF(origin < LLAP__ORIGIN_MAX, "%u\n", origin);
502 llap = llap_cast_private(page);
504 /* move to end of LRU list */
505 spin_lock(&sbi->ll_lock);
506 sbi->ll_pglist_gen++;
507 list_del_init(&llap->llap_pglist_item);
508 list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
509 spin_unlock(&sbi->ll_lock);
513 exp = ll_i2obdexp(page->mapping->host);
515 RETURN(ERR_PTR(-EINVAL));
517 /* limit the number of lustre-cached pages */
518 if (sbi->ll_async_page_count >= sbi->ll_async_page_max)
519 llap_shrink_cache(sbi, 0);
521 OBD_SLAB_ALLOC(llap, ll_async_page_slab, SLAB_KERNEL,
522 ll_async_page_slab_size);
524 RETURN(ERR_PTR(-ENOMEM));
525 llap->llap_magic = LLAP_MAGIC;
526 llap->llap_cookie = (void *)llap + size_round(sizeof(*llap));
528 rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
529 (obd_off)page->index << PAGE_SHIFT,
530 &ll_async_page_ops, llap, &llap->llap_cookie);
532 OBD_SLAB_FREE(llap, ll_async_page_slab,
533 ll_async_page_slab_size);
537 CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
538 page, llap->llap_cookie, (obd_off)page->index << PAGE_SHIFT);
539 /* also zeroing the PRIVBITS low order bitflags */
540 __set_page_ll_data(page, llap);
541 llap->llap_page = page;
543 spin_lock(&sbi->ll_lock);
544 sbi->ll_pglist_gen++;
545 sbi->ll_async_page_count++;
546 list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
547 spin_unlock(&sbi->ll_lock);
550 if (unlikely(sbi->ll_flags & LL_SBI_CHECKSUM)) {
552 csum = crc32_le(csum, kmap(page), PAGE_SIZE);
554 if (origin == LLAP_ORIGIN_READAHEAD ||
555 origin == LLAP_ORIGIN_READPAGE) {
556 llap->llap_checksum = 0;
557 } else if (origin == LLAP_ORIGIN_COMMIT_WRITE ||
558 llap->llap_checksum == 0) {
559 llap->llap_checksum = csum;
560 CDEBUG(D_PAGE, "page %p cksum %x\n", page, csum);
561 } else if (llap->llap_checksum == csum) {
562 /* origin == LLAP_ORIGIN_WRITEPAGE */
563 CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
566 /* origin == LLAP_ORIGIN_WRITEPAGE */
567 LL_CDEBUG_PAGE(D_ERROR, page, "old cksum %x != new "
568 "%x!\n", llap->llap_checksum, csum);
572 llap->llap_origin = origin;
576 static int queue_or_sync_write(struct obd_export *exp, struct inode *inode,
577 struct ll_async_page *llap,
578 unsigned to, obd_flag async_flags)
580 unsigned long size_index = inode->i_size >> PAGE_SHIFT;
581 struct obd_io_group *oig;
582 struct ll_sb_info *sbi = ll_i2sbi(inode);
583 int rc, noquot = capable(CAP_SYS_RESOURCE) ? OBD_BRW_NOQUOTA : 0;
586 /* _make_ready only sees llap once we've unlocked the page */
587 llap->llap_write_queued = 1;
588 rc = obd_queue_async_io(exp, ll_i2info(inode)->lli_smd, NULL,
589 llap->llap_cookie, OBD_BRW_WRITE | noquot,
590 0, 0, 0, async_flags);
592 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "write queued\n");
593 //llap_write_pending(inode, llap);
597 llap->llap_write_queued = 0;
603 /* make full-page requests if we are not at EOF (bug 4410) */
604 if (to != PAGE_SIZE && llap->llap_page->index < size_index) {
605 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
606 "sync write before EOF: size_index %lu, to %d\n",
609 } else if (to != PAGE_SIZE && llap->llap_page->index == size_index) {
610 int size_to = inode->i_size & ~PAGE_MASK;
611 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
612 "sync write at EOF: size_index %lu, to %d/%d\n",
613 size_index, to, size_to);
618 /* compare the checksum once before the page leaves llite */
619 if (unlikely((sbi->ll_flags & LL_SBI_CHECKSUM) &&
620 llap->llap_checksum != 0)) {
622 struct page *page = llap->llap_page;
623 csum = crc32_le(csum, kmap(page), PAGE_SIZE);
625 if (llap->llap_checksum == csum) {
626 CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
629 CERROR("page %p old cksum %x != new cksum %x!\n",
630 page, llap->llap_checksum, csum);
634 rc = obd_queue_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig,
635 llap->llap_cookie, OBD_BRW_WRITE | noquot,
636 0, to, 0, ASYNC_READY | ASYNC_URGENT |
637 ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
641 rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
647 if (!rc && async_flags & ASYNC_READY)
648 unlock_page(llap->llap_page);
650 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "sync write returned %d\n", rc);
658 /* update our write count to account for i_size increases that may have
659 * happened since we've queued the page for io. */
661 /* be careful not to return success without setting the page Uptodate or
662 * the next pass through prepare_write will read in stale data from disk. */
663 int ll_commit_write(struct file *file, struct page *page, unsigned from,
666 struct inode *inode = page->mapping->host;
667 struct ll_inode_info *lli = ll_i2info(inode);
668 struct lov_stripe_md *lsm = lli->lli_smd;
669 struct obd_export *exp;
670 struct ll_async_page *llap;
675 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
676 LASSERT(inode == file->f_dentry->d_inode);
677 LASSERT(PageLocked(page));
679 CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
680 inode, page, from, to, page->index);
682 llap = llap_from_page(page, LLAP_ORIGIN_COMMIT_WRITE);
684 RETURN(PTR_ERR(llap));
686 exp = ll_i2obdexp(inode);
690 /* queue a write for some time in the future the first time we
692 if (!PageDirty(page)) {
693 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
694 LPROC_LL_DIRTY_MISSES);
696 rc = queue_or_sync_write(exp, inode, llap, to, 0);
700 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
701 LPROC_LL_DIRTY_HITS);
704 /* put the page in the page cache, from now on ll_removepage is
705 * responsible for cleaning up the llap.
706 * only set page dirty when it's queued to be write out */
707 if (llap->llap_write_queued)
708 set_page_dirty(page);
711 size = (((obd_off)page->index) << PAGE_SHIFT) + to;
712 down(&lli->lli_size_sem);
714 obd_adjust_kms(exp, lsm, size, 0);
715 if (size > inode->i_size)
716 inode->i_size = size;
717 SetPageUptodate(page);
718 } else if (size > inode->i_size) {
719 /* this page beyond the pales of i_size, so it can't be
720 * truncated in ll_p_r_e during lock revoking. we must
721 * teardown our book-keeping here. */
724 up(&lli->lli_size_sem);
728 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
730 struct ll_ra_info *ra = &sbi->ll_ra_info;
734 spin_lock(&sbi->ll_lock);
735 ret = min(ra->ra_max_pages - ra->ra_cur_pages, len);
736 ra->ra_cur_pages += ret;
737 spin_unlock(&sbi->ll_lock);
742 static void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
744 struct ll_ra_info *ra = &sbi->ll_ra_info;
745 spin_lock(&sbi->ll_lock);
746 LASSERTF(ra->ra_cur_pages >= len, "r_c_p %lu len %lu\n",
747 ra->ra_cur_pages, len);
748 ra->ra_cur_pages -= len;
749 spin_unlock(&sbi->ll_lock);
752 /* called for each page in a completed rpc.*/
753 void ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
755 struct ll_async_page *llap;
759 llap = LLAP_FROM_COOKIE(data);
760 page = llap->llap_page;
761 LASSERT(PageLocked(page));
763 LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
765 if (cmd & OBD_BRW_READ && llap->llap_defer_uptodate)
766 ll_ra_count_put(ll_i2sbi(page->mapping->host), 1);
769 if (cmd & OBD_BRW_READ) {
770 if (!llap->llap_defer_uptodate)
771 SetPageUptodate(page);
773 llap->llap_write_queued = 0;
775 ClearPageError(page);
777 if (cmd & OBD_BRW_READ) {
778 llap->llap_defer_uptodate = 0;
780 ll_redirty_page(page);
787 if (0 && cmd & OBD_BRW_WRITE) {
788 llap_write_complete(page->mapping->host, llap);
789 ll_try_done_writing(page->mapping->host);
792 if (PageWriteback(page)) {
793 end_page_writeback(page);
795 page_cache_release(page);
799 /* the kernel calls us here when a page is unhashed from the page cache.
800 * the page will be locked and the kernel is holding a spinlock, so
801 * we need to be careful. we're just tearing down our book-keeping
803 void ll_removepage(struct page *page)
805 struct inode *inode = page->mapping->host;
806 struct obd_export *exp;
807 struct ll_async_page *llap;
808 struct ll_sb_info *sbi = ll_i2sbi(inode);
812 LASSERT(!in_interrupt());
814 /* sync pages or failed read pages can leave pages in the page
815 * cache that don't have our data associated with them anymore */
816 if (page->private == 0) {
821 LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
823 exp = ll_i2obdexp(inode);
825 CERROR("page %p ind %lu gave null export\n", page, page->index);
830 llap = llap_from_page(page, 0);
832 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
833 page->index, PTR_ERR(llap));
838 //llap_write_complete(inode, llap);
839 rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
842 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
844 /* this unconditional free is only safe because the page lock
845 * is providing exclusivity to memory pressure/truncate/writeback..*/
846 __clear_page_ll_data(page);
848 spin_lock(&sbi->ll_lock);
849 if (!list_empty(&llap->llap_pglist_item))
850 list_del_init(&llap->llap_pglist_item);
851 sbi->ll_pglist_gen++;
852 sbi->ll_async_page_count--;
853 spin_unlock(&sbi->ll_lock);
854 OBD_SLAB_FREE(llap, ll_async_page_slab, ll_async_page_slab_size);
858 static int ll_page_matches(struct page *page, int fd_flags)
860 struct lustre_handle match_lockh = {0};
861 struct inode *inode = page->mapping->host;
862 ldlm_policy_data_t page_extent;
866 if (unlikely(fd_flags & LL_FILE_GROUP_LOCKED))
869 page_extent.l_extent.start = (__u64)page->index << PAGE_CACHE_SHIFT;
870 page_extent.l_extent.end =
871 page_extent.l_extent.start + PAGE_CACHE_SIZE - 1;
872 flags = LDLM_FL_TEST_LOCK | LDLM_FL_BLOCK_GRANTED;
873 if (!(fd_flags & LL_FILE_READAHEAD))
874 flags |= LDLM_FL_CBPENDING;
875 matches = obd_match(ll_i2sbi(inode)->ll_osc_exp,
876 ll_i2info(inode)->lli_smd, LDLM_EXTENT,
877 &page_extent, LCK_PR | LCK_PW, &flags, inode,
882 static int ll_issue_page_read(struct obd_export *exp,
883 struct ll_async_page *llap,
884 struct obd_io_group *oig, int defer)
886 struct page *page = llap->llap_page;
889 page_cache_get(page);
890 llap->llap_defer_uptodate = defer;
891 llap->llap_ra_used = 0;
892 rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
893 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
894 PAGE_SIZE, 0, ASYNC_COUNT_STABLE | ASYNC_READY |
897 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
898 page_cache_release(page);
903 static void ll_ra_stats_inc_unlocked(struct ll_ra_info *ra, enum ra_stat which)
905 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
906 ra->ra_stats[which]++;
909 static void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
911 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
912 struct ll_ra_info *ra = &ll_i2sbi(mapping->host)->ll_ra_info;
914 spin_lock(&sbi->ll_lock);
915 ll_ra_stats_inc_unlocked(ra, which);
916 spin_unlock(&sbi->ll_lock);
919 void ll_ra_accounting(struct page *page, struct address_space *mapping)
921 struct ll_async_page *llap;
923 llap = llap_from_page(page, LLAP_ORIGIN_WRITEPAGE);
927 if (!llap->llap_defer_uptodate || llap->llap_ra_used)
930 ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
933 #define RAS_CDEBUG(ras) \
934 CDEBUG(D_READA, "lrp %lu c %lu ws %lu wl %lu nra %lu\n", \
935 ras->ras_last_readpage, ras->ras_consecutive, \
936 ras->ras_window_start, ras->ras_window_len, \
937 ras->ras_next_readahead);
939 static int index_in_window(unsigned long index, unsigned long point,
940 unsigned long before, unsigned long after)
942 unsigned long start = point - before, end = point + after;
949 return start <= index && index <= end;
952 static int ll_readahead(struct ll_readahead_state *ras,
953 struct obd_export *exp, struct address_space *mapping,
954 struct obd_io_group *oig, int flags)
956 unsigned long i, start = 0, end = 0, reserved;
957 struct ll_async_page *llap;
959 int rc, ret = 0, match_failed = 0;
961 unsigned int gfp_mask;
964 kms = lov_merge_size(ll_i2info(mapping->host)->lli_smd, 1);
966 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
970 spin_lock(&ras->ras_lock);
971 /* reserve a part of the read-ahead window that we'll be issuing */
972 if (ras->ras_window_len) {
973 start = ras->ras_next_readahead;
974 end = ras->ras_window_start + ras->ras_window_len - 1;
975 end = min(end, (unsigned long)((kms - 1) >> PAGE_CACHE_SHIFT));
976 ras->ras_next_readahead = max(end, end + 1);
980 spin_unlock(&ras->ras_lock);
983 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
987 reserved = ll_ra_count_get(ll_i2sbi(mapping->host), end - start + 1);
988 if (reserved < end - start + 1)
989 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
991 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
993 gfp_mask |= __GFP_NOWARN;
996 for (i = start; reserved > 0 && !match_failed && i <= end; i++) {
997 /* skip locked pages from previous readpage calls */
998 page = grab_cache_page_nowait_gfp(mapping, i, gfp_mask);
1000 ll_ra_stats_inc(mapping, RA_STAT_FAILED_GRAB_PAGE);
1001 CDEBUG(D_READA, "g_c_p_n failed\n");
1005 /* we do this first so that we can see the page in the /proc
1007 llap = llap_from_page(page, LLAP_ORIGIN_READAHEAD);
1008 if (IS_ERR(llap) || llap->llap_defer_uptodate)
1011 /* skip completed pages */
1012 if (Page_Uptodate(page))
1015 /* bail when we hit the end of the lock. */
1016 if ((rc = ll_page_matches(page, flags|LL_FILE_READAHEAD)) <= 0){
1017 LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
1018 "lock match failed: rc %d\n", rc);
1019 ll_ra_stats_inc(mapping, RA_STAT_FAILED_MATCH);
1024 rc = ll_issue_page_read(exp, llap, oig, 1);
1028 LL_CDEBUG_PAGE(D_READA| D_PAGE, page,
1029 "started read-ahead\n");
1033 LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
1034 "skipping read-ahead\n");
1038 page_cache_release(page);
1041 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
1043 ll_ra_count_put(ll_i2sbi(mapping->host), reserved);
1044 if (i == end + 1 && end == (kms >> PAGE_CACHE_SHIFT))
1045 ll_ra_stats_inc(mapping, RA_STAT_EOF);
1047 /* if we didn't get to the end of the region we reserved from
1048 * the ras we need to go back and update the ras so that the
1049 * next read-ahead tries from where we left off. we only do so
1050 * if the region we failed to issue read-ahead on is still ahead
1051 * of the app and behind the next index to start read-ahead from */
1053 spin_lock(&ras->ras_lock);
1054 if (i < ras->ras_next_readahead &&
1055 index_in_window(i, ras->ras_window_start, 0,
1056 ras->ras_window_len)) {
1057 ras->ras_next_readahead = i;
1060 spin_unlock(&ras->ras_lock);
1066 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
1068 ras->ras_window_start = index & (~(PTLRPC_MAX_BRW_PAGES - 1));
1071 /* called with the ras_lock held or from places where it doesn't matter */
1072 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
1074 ras->ras_last_readpage = index;
1075 ras->ras_consecutive = 1;
1076 ras->ras_window_len = 0;
1077 ras_set_start(ras, index);
1078 ras->ras_next_readahead = ras->ras_window_start;
1083 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
1085 spin_lock_init(&ras->ras_lock);
1089 static void ras_update(struct ll_sb_info *sbi, struct ll_readahead_state *ras,
1090 unsigned long index, unsigned hit)
1092 struct ll_ra_info *ra = &sbi->ll_ra_info;
1096 spin_lock(&sbi->ll_lock);
1097 spin_lock(&ras->ras_lock);
1099 ll_ra_stats_inc_unlocked(ra, hit ? RA_STAT_HIT : RA_STAT_MISS);
1101 /* reset the read-ahead window in two cases. First when the app seeks
1102 * or reads to some other part of the file. Secondly if we get a
1103 * read-ahead miss that we think we've previously issued. This can
1104 * be a symptom of there being so many read-ahead pages that the VM is
1105 * reclaiming it before we get to it. */
1106 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1108 ll_ra_stats_inc_unlocked(ra, RA_STAT_DISTANT_READPAGE);
1109 } else if (!hit && ras->ras_window_len &&
1110 index < ras->ras_next_readahead &&
1111 index_in_window(index, ras->ras_window_start, 0,
1112 ras->ras_window_len)) {
1114 ll_ra_stats_inc_unlocked(ra, RA_STAT_MISS_IN_WINDOW);
1118 ras_reset(ras, index);
1119 GOTO(out_unlock, 0);
1122 ras->ras_last_readpage = index;
1123 ras->ras_consecutive++;
1124 ras_set_start(ras, index);
1125 ras->ras_next_readahead = max(ras->ras_window_start,
1126 ras->ras_next_readahead);
1128 /* wait for a few pages to arrive before issuing readahead to avoid
1129 * the worst overutilization */
1130 if (ras->ras_consecutive == 3) {
1131 ras->ras_window_len = PTLRPC_MAX_BRW_PAGES;
1132 GOTO(out_unlock, 0);
1135 /* we need to increase the window sometimes. we'll arbitrarily
1136 * do it half-way through the pages in an rpc */
1137 if ((index & (PTLRPC_MAX_BRW_PAGES - 1)) ==
1138 (PTLRPC_MAX_BRW_PAGES >> 1)) {
1139 ras->ras_window_len += PTLRPC_MAX_BRW_PAGES;
1140 ras->ras_window_len = min(ras->ras_window_len,
1147 spin_unlock(&ras->ras_lock);
1148 spin_unlock(&sbi->ll_lock);
1152 int ll_writepage(struct page *page)
1154 struct inode *inode = page->mapping->host;
1155 struct ll_inode_info *lli = ll_i2info(inode);
1156 struct obd_export *exp;
1157 struct ll_async_page *llap;
1161 LASSERT(!PageDirty(page));
1162 LASSERT(PageLocked(page));
1164 exp = ll_i2obdexp(inode);
1166 GOTO(out, rc = -EINVAL);
1168 llap = llap_from_page(page, LLAP_ORIGIN_WRITEPAGE);
1170 GOTO(out, rc = PTR_ERR(llap));
1172 page_cache_get(page);
1173 if (llap->llap_write_queued) {
1174 LL_CDEBUG_PAGE(D_PAGE, page, "marking urgent\n");
1175 rc = obd_set_async_flags(exp, lli->lli_smd, NULL,
1177 ASYNC_READY | ASYNC_URGENT);
1179 rc = queue_or_sync_write(exp, inode, llap, PAGE_SIZE,
1180 ASYNC_READY | ASYNC_URGENT);
1183 page_cache_release(page);
1186 if (!lli->lli_async_rc)
1187 lli->lli_async_rc = rc;
1188 /* re-dirty page on error so it retries write */
1189 ll_redirty_page(page);
1196 * for now we do our readpage the same on both 2.4 and 2.5. The kernel's
1197 * read-ahead assumes it is valid to issue readpage all the way up to
1198 * i_size, but our dlm locks make that not the case. We disable the
1199 * kernel's read-ahead and do our own by walking ahead in the page cache
1200 * checking for dlm lock coverage. the main difference between 2.4 and
1201 * 2.6 is how read-ahead gets batched and issued, but we're using our own,
1202 * so they look the same.
1204 int ll_readpage(struct file *filp, struct page *page)
1206 struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
1207 struct inode *inode = page->mapping->host;
1208 struct obd_export *exp;
1209 struct ll_async_page *llap;
1210 struct obd_io_group *oig = NULL;
1214 LASSERT(PageLocked(page));
1215 LASSERT(!PageUptodate(page));
1216 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset=%Lu=%#Lx\n",
1217 inode->i_ino, inode->i_generation, inode,
1218 (((loff_t)page->index) << PAGE_SHIFT),
1219 (((loff_t)page->index) << PAGE_SHIFT));
1220 LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
1222 rc = oig_init(&oig);
1226 exp = ll_i2obdexp(inode);
1228 GOTO(out, rc = -EINVAL);
1230 llap = llap_from_page(page, LLAP_ORIGIN_READPAGE);
1232 GOTO(out, rc = PTR_ERR(llap));
1234 if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1235 ras_update(ll_i2sbi(inode), &fd->fd_ras, page->index,
1236 llap->llap_defer_uptodate);
1238 if (llap->llap_defer_uptodate) {
1239 llap->llap_ra_used = 1;
1240 rc = ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1243 obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd,
1245 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
1246 SetPageUptodate(page);
1248 GOTO(out_oig, rc = 0);
1251 rc = ll_page_matches(page, fd->fd_flags);
1253 LL_CDEBUG_PAGE(D_ERROR, page, "lock match failed: rc %d\n", rc);
1258 CWARN("ino %lu page %lu (%llu) not covered by "
1259 "a lock (mmap?). check debug logs.\n",
1260 inode->i_ino, page->index,
1261 (long long)page->index << PAGE_CACHE_SHIFT);
1264 rc = ll_issue_page_read(exp, llap, oig, 0);
1268 LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
1269 if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1270 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1273 rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);