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
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 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
38 #include <linux/buffer_head.h>
39 #include <linux/mpage.h>
40 #include <linux/writeback.h>
42 #include <linux/iobuf.h>
44 #include <linux/stat.h>
45 #include <asm/uaccess.h>
46 #include <asm/segment.h>
48 #include <linux/pagemap.h>
49 #include <linux/smp_lock.h>
51 #define DEBUG_SUBSYSTEM S_LLITE
53 #include <linux/lustre_mds.h>
54 #include <linux/lustre_lite.h>
55 #include "llite_internal.h"
56 #include <linux/lustre_compat25.h>
59 * Remove page from dirty list
61 static void __set_page_clean(struct page *page)
63 struct address_space *mapping = page->mapping;
69 PGCACHE_WRLOCK(mapping);
71 list_del(&page->list);
72 list_add(&page->list, &mapping->clean_pages);
74 /* XXX doesn't inode_lock protect i_state ? */
75 inode = mapping->host;
76 if (list_empty(&mapping->dirty_pages)) {
77 CDEBUG(D_INODE, "inode clean\n");
78 inode->i_state &= ~I_DIRTY_PAGES;
81 PGCACHE_WRUNLOCK(mapping);
85 void set_page_clean(struct page *page)
87 if (PageDirty(page)) {
89 __set_page_clean(page);
93 /* SYNCHRONOUS I/O to object storage for an inode */
94 static int ll_brw(int cmd, struct inode *inode, struct obdo *oa,
95 struct page *page, int flags)
97 struct ll_inode_info *lli = ll_i2info(inode);
98 struct lov_stripe_md *lsm = lli->lli_smd;
104 pg.off = ((obd_off)page->index) << PAGE_SHIFT;
106 if (cmd == OBD_BRW_WRITE && (pg.off + PAGE_SIZE > inode->i_size))
107 pg.count = inode->i_size % PAGE_SIZE;
109 pg.count = PAGE_SIZE;
111 CDEBUG(D_PAGE, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
112 cmd & OBD_BRW_WRITE ? "write" : "read", pg.count, inode->i_ino,
115 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
117 inode->i_ino, inode, inode->i_size, page->mapping->host,
118 page->mapping->host->i_size, page->index, pg.off);
123 if (cmd == OBD_BRW_WRITE)
124 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
125 LPROC_LL_BRW_WRITE, pg.count);
127 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
128 LPROC_LL_BRW_READ, pg.count);
129 rc = obd_brw(cmd, ll_i2obdconn(inode), oa, lsm, 1, &pg, NULL);
130 if (rc != 0 && rc != -EIO)
131 CERROR("error from obd_brw: rc = %d\n", rc);
137 * we were asked to read a single page but we're going to try and read a batch
138 * of pages all at once. this vaguely simulates 2.5's readpages.
140 static int ll_readpage(struct file *file, struct page *first_page)
142 struct inode *inode = first_page->mapping->host;
143 struct ll_inode_info *lli = ll_i2info(inode);
144 struct page *page = first_page;
145 struct list_head *pos;
146 struct brw_page *pgs;
148 unsigned long end_index, extent_end = 0;
149 struct ptlrpc_request_set *set;
150 int npgs = 0, rc = 0, max_pages;
153 LASSERT(PageLocked(page));
154 LASSERT(!PageUptodate(page));
155 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset="LPX64"\n",
156 inode->i_ino, inode->i_generation, inode,
157 (((obd_off)page->index) << PAGE_SHIFT));
158 LASSERT(atomic_read(&file->f_dentry->d_inode->i_count) > 0);
160 if (inode->i_size <= ((obd_off)page->index) << PAGE_SHIFT) {
161 CERROR("reading beyond EOF\n");
162 memset(kmap(page), 0, PAGE_SIZE);
164 SetPageUptodate(page);
169 /* try to read the file's preferred block size in a one-er */
170 end_index = first_page->index +
171 (inode->i_blksize >> PAGE_CACHE_SHIFT);
172 if (end_index > (inode->i_size >> PAGE_CACHE_SHIFT))
173 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
175 max_pages = ((end_index - first_page->index) << PAGE_CACHE_SHIFT) >>
177 OBD_ALLOC_GFP(pgs, max_pages * sizeof(*pgs), GFP_USER);
182 * find how far we're allowed to read under the extent ll_file_read
185 spin_lock(&lli->lli_read_extent_lock);
186 list_for_each(pos, &lli->lli_read_extents) {
187 struct ll_read_extent *rextent;
188 rextent = list_entry(pos, struct ll_read_extent, re_lli_item);
189 if (rextent->re_task != current)
192 if (rextent->re_extent.end + PAGE_SIZE < rextent->re_extent.end)
193 /* extent wrapping */
196 extent_end = (rextent->re_extent.end + PAGE_SIZE)
198 /* 32bit indexes, 64bit extents.. */
199 if (((u64)extent_end >> PAGE_CACHE_SHIFT) <
200 rextent->re_extent.end)
205 spin_unlock(&lli->lli_read_extent_lock);
207 if (extent_end == 0) {
208 static long next_print;
209 if (time_after(jiffies, next_print)) {
210 next_print = jiffies + 30 * HZ;
211 CDEBUG(D_INODE, "mmap readpage - check locks\n");
213 end_index = page->index + 1;
214 } else if (extent_end < end_index)
215 end_index = extent_end;
217 CDEBUG(D_INFO, "max_pages: %d, extent_end: %lu, end_index: %lu, "
219 max_pages, extent_end, end_index, inode->i_size);
221 /* to balance the find_get_page ref the other pages get that is
222 * decrefed on teardown.. */
223 page_cache_get(page);
225 unsigned long index ;
228 pgs[npgs].off = ((obd_off)page->index) << PAGE_CACHE_SHIFT;
230 pgs[npgs].count = PAGE_SIZE;
231 /* XXX Workaround for BA OSTs returning short reads at EOF.
232 * The linux OST will return the full page, zero-filled at the
233 * end, which will just overwrite the data we set here. Bug
234 * 593 relates to fixing this properly.
236 if (inode->i_size < pgs[npgs].off + PAGE_SIZE) {
237 int count = inode->i_size - pgs[npgs].off;
238 void *addr = kmap(page);
239 pgs[npgs].count = count;
240 //POISON(addr, 0x7c, count);
241 memset(addr + count, 0, PAGE_SIZE - count);
246 if (npgs == max_pages)
250 * find pages ahead of us that we can read in.
251 * grab_cache_page waits on pages that are locked so
252 * we first try find_get_page, which doesn't. this stops
253 * the worst case behaviour of racing threads waiting on
254 * each other, but doesn't remove it entirely.
256 for (index = page->index + 1, page = NULL;
257 page == NULL && index < end_index; index++) {
259 /* see if the page already exists and needs updating */
260 page = find_get_page(inode->i_mapping, index);
262 if (Page_Uptodate(page) || TryLockPage(page))
264 if (!page->mapping || Page_Uptodate(page))
267 /* ok, we have to create it.. */
268 page = grab_cache_page(inode->i_mapping, index);
271 if (Page_Uptodate(page))
280 page_cache_release(page);
286 if ((oa = obdo_alloc()) == NULL) {
287 CERROR("ENOMEM allocing obdo\n");
289 } else if ((set = ptlrpc_prep_set()) == NULL) {
290 CERROR("ENOMEM allocing request set\n");
294 struct ll_file_data *fd = file->private_data;
296 oa->o_id = lli->lli_smd->lsm_object_id;
297 memcpy(obdo_handle(oa), &fd->fd_ost_och.och_fh,
298 sizeof(fd->fd_ost_och.och_fh));
299 oa->o_valid = OBD_MD_FLID | OBD_MD_FLHANDLE;
300 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME);
302 rc = obd_brw_async(OBD_BRW_READ, ll_i2obdconn(inode), oa,
303 ll_i2info(inode)->lli_smd, npgs, pgs,
306 rc = ptlrpc_set_wait(set);
307 ptlrpc_set_destroy(set);
309 obdo_refresh_inode(inode, oa, oa->o_valid);
310 if (rc && rc != -EIO)
311 CERROR("error from obd_brw_async: rc = %d\n", rc);
319 SetPageUptodate(page);
321 page_cache_release(page);
324 OBD_FREE(pgs, max_pages * sizeof(*pgs));
328 /* this isn't where truncate starts. roughly:
329 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
330 * we grab the lock back in setattr_raw to avoid races. */
331 void ll_truncate(struct inode *inode)
333 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
337 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
338 inode->i_generation, inode);
340 /* object not yet allocated */
342 CERROR("truncate on inode %lu with no objects\n", inode->i_ino);
347 /* vmtruncate will just throw away our dirty pages, make sure
348 * we don't think they're still dirty, being careful to round
349 * i_size to the first whole page that was tossed */
350 err = ll_clear_dirty_pages(ll_i2obdconn(inode), lsm,
351 (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT,
354 oa.o_id = lsm->lsm_object_id;
355 oa.o_valid = OBD_MD_FLID;
356 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
357 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
359 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
360 oa.o_id, inode->i_size);
362 /* truncate == punch from new size to absolute end of file */
363 err = obd_punch(ll_i2obdconn(inode), &oa, lsm, inode->i_size,
364 OBD_OBJECT_EOF, NULL);
366 CERROR("obd_truncate fails (%d) ino %lu\n", err, inode->i_ino);
368 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
369 OBD_MD_FLATIME | OBD_MD_FLMTIME |
376 //#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
378 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
381 struct inode *inode = page->mapping->host;
382 struct ll_inode_info *lli = ll_i2info(inode);
383 struct ll_file_data *fd = file->private_data;
384 struct lov_stripe_md *lsm = lli->lli_smd;
385 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
391 if (!PageLocked(page))
394 if (PageUptodate(page))
397 //POISON(addr + from, 0xca, to - from);
399 /* Check to see if we should return -EIO right away */
402 pg.count = PAGE_SIZE;
404 rc = obd_brw(OBD_BRW_CHECK, ll_i2obdconn(inode), NULL, lsm, 1,&pg,NULL);
408 /* We're completely overwriting an existing page, so _don't_ set it up
409 * to date until commit_write */
410 if (from == 0 && to == PAGE_SIZE)
413 /* If are writing to a new page, no need to read old data.
414 * the extent locking and getattr procedures in ll_file_write have
415 * guaranteed that i_size is stable enough for our zeroing needs */
416 if (inode->i_size <= offset) {
417 memset(kmap(page), 0, PAGE_SIZE);
419 GOTO(prepare_done, rc = 0);
422 oa.o_id = lsm->lsm_object_id;
423 oa.o_mode = inode->i_mode;
424 memcpy(obdo_handle(&oa), &fd->fd_ost_och.och_fh,
425 sizeof(fd->fd_ost_och.och_fh));
426 oa.o_valid = OBD_MD_FLID |OBD_MD_FLMODE |OBD_MD_FLTYPE |OBD_MD_FLHANDLE;
428 rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
430 obdo_refresh_inode(inode, &oa, oa.o_valid);
435 SetPageUptodate(page);
441 * background file writeback. This is called regularly from kupdated to write
442 * dirty data, from kswapd when memory is low, and from filemap_fdatasync when
443 * super blocks or inodes are synced..
445 * obd_brw errors down in _batch_writepage are ignored, so pages are always
446 * unlocked. Also, there is nobody to return an error code to from here - the
447 * application may not even be running anymore.
449 * this should be async so that things like kswapd can have a chance to
450 * free some more pages that our allocating writeback may need, but it isn't
453 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
454 static unsigned long ll_local_cache_dirty_pages;
455 static unsigned long ll_max_dirty_pages = 20 * 1024 * 1024 / PAGE_SIZE;
457 static spinlock_t ll_local_cache_page_count_lock = SPIN_LOCK_UNLOCKED;
459 int ll_rd_dirty_pages(char *page, char **start, off_t off, int count, int *eof,
462 unsigned long dirty_count;
463 spin_lock(&ll_local_cache_page_count_lock);
464 dirty_count = ll_local_cache_dirty_pages;
465 spin_unlock(&ll_local_cache_page_count_lock);
466 return snprintf(page, count, "%lu\n", dirty_count);
469 int ll_rd_max_dirty_pages(char *page, char **start, off_t off, int count,
470 int *eof, void *data)
472 unsigned long max_dirty;
473 spin_lock(&ll_local_cache_page_count_lock);
474 max_dirty = ll_max_dirty_pages;
475 spin_unlock(&ll_local_cache_page_count_lock);
476 return snprintf(page, count, "%lu\n", max_dirty);
479 int ll_wr_max_dirty_pages(struct file *file, const char *buffer,
480 unsigned long count, void *data)
482 unsigned long max_dirty;
483 signed long max_dirty_signed;
484 char kernbuf[20], *end;
486 if (count > (sizeof(kernbuf) - 1))
489 if (copy_from_user(kernbuf, buffer, count))
492 kernbuf[count] = '\0';
494 max_dirty_signed = simple_strtol(kernbuf, &end, 0);
497 max_dirty = (unsigned long)max_dirty_signed;
500 if (max_dirty < ll_local_cache_dirty_pages)
501 flush_to_new_max_dirty();
504 spin_lock(&ll_local_cache_page_count_lock);
505 CDEBUG(D_CACHE, "changing max_dirty from %lu to %lu\n",
506 ll_max_dirty_pages, max_dirty);
507 ll_max_dirty_pages = max_dirty;
508 spin_unlock(&ll_local_cache_page_count_lock);
512 static int ll_local_cache_full(void)
515 spin_lock(&ll_local_cache_page_count_lock);
516 if (ll_max_dirty_pages &&
517 ll_local_cache_dirty_pages >= ll_max_dirty_pages) {
520 spin_unlock(&ll_local_cache_page_count_lock);
521 /* XXX instrument? */
522 /* XXX trigger async writeback when full, or 75% of full? */
526 static void ll_local_cache_flushed_pages(unsigned long pgcount)
528 unsigned long dirty_count;
529 spin_lock(&ll_local_cache_page_count_lock);
530 dirty_count = ll_local_cache_dirty_pages;
531 ll_local_cache_dirty_pages -= pgcount;
532 CDEBUG(D_CACHE, "dirty pages: %lu->%lu)\n",
533 dirty_count, ll_local_cache_dirty_pages);
534 spin_unlock(&ll_local_cache_page_count_lock);
535 LASSERT(dirty_count >= pgcount);
538 static void ll_local_cache_dirtied_pages(unsigned long pgcount)
540 unsigned long dirty_count;
541 spin_lock(&ll_local_cache_page_count_lock);
542 dirty_count = ll_local_cache_dirty_pages;
543 ll_local_cache_dirty_pages += pgcount;
544 CDEBUG(D_CACHE, "dirty pages: %lu->%lu\n",
545 dirty_count, ll_local_cache_dirty_pages);
546 spin_unlock(&ll_local_cache_page_count_lock);
547 /* XXX track maximum cached, report to lprocfs */
550 int ll_clear_dirty_pages(struct lustre_handle *conn, struct lov_stripe_md *lsm,
551 unsigned long start, unsigned long end)
553 unsigned long cleared;
557 rc = obd_clear_dirty_pages(conn, lsm, start, end, &cleared);
559 ll_local_cache_flushed_pages(cleared);
563 int ll_mark_dirty_page(struct lustre_handle *conn, struct lov_stripe_md *lsm,
569 if (ll_local_cache_full())
572 rc = obd_mark_page_dirty(conn, lsm, index);
574 ll_local_cache_dirtied_pages(1);
578 static int ll_writepage(struct page *page)
580 struct inode *inode = page->mapping->host;
584 CDEBUG(D_CACHE, "page %p [lau %d] inode %p\n", page,
585 PageLaunder(page), inode);
586 LASSERT(PageLocked(page));
588 oa.o_id = ll_i2info(inode)->lli_smd->lsm_object_id;
589 oa.o_valid = OBD_MD_FLID;
590 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
591 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
593 RETURN(ll_batch_writepage(inode, &oa, page));
597 * we really don't want to start writeback here, we want to give callers some
598 * time to further dirty the pages before we write them out.
600 static int ll_commit_write(struct file *file, struct page *page,
601 unsigned from, unsigned to)
603 struct inode *inode = page->mapping->host;
608 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
609 LASSERT(inode == file->f_dentry->d_inode);
610 LASSERT(PageLocked(page));
612 CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
613 inode, page, from, to, page->index);
614 if (!PageDirty(page)) {
615 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
616 LPROC_LL_DIRTY_MISSES);
617 rc = ll_mark_dirty_page(ll_i2obdconn(inode),
618 ll_i2info(inode)->lli_smd,
620 if (rc < 0 && rc != -EDQUOT)
621 RETURN(rc); /* XXX lproc counter here? */
623 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
624 LPROC_LL_DIRTY_HITS);
627 size = (((obd_off)page->index) << PAGE_SHIFT) + to;
628 if (size > inode->i_size)
629 inode->i_size = size;
631 SetPageUptodate(page);
632 set_page_dirty(page);
634 /* This means that we've hit either the local cache limit or the limit
635 * of the OST's grant. */
637 struct ll_file_data *fd = file->private_data;
641 oa.o_id = ll_i2info(inode)->lli_smd->lsm_object_id;
642 memcpy(obdo_handle(&oa), &fd->fd_ost_och.och_fh,
643 sizeof(fd->fd_ost_och.och_fh));
644 oa.o_valid = OBD_MD_FLID | OBD_MD_FLHANDLE;
645 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
646 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
648 rc = ll_batch_writepage(inode, &oa, page);
649 lock_page(page); /* caller expects to unlock */
654 } /* ll_commit_write */
656 static int ll_writepage(struct page *page,
657 struct writeback_control *wbc)
662 static int ll_commit_write(struct file *file, struct page *page,
663 unsigned from, unsigned to)
669 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
670 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
671 unsigned long blocknr, int blocksize)
673 struct ll_inode_info *lli = ll_i2info(inode);
674 struct lov_stripe_md *lsm = lli->lli_smd;
675 struct brw_page *pga;
676 struct ptlrpc_request_set *set;
678 int length, i, flags, rc = 0;
682 if (!lsm || !lsm->lsm_object_id)
685 /* FIXME: io smaller than PAGE_SIZE is broken on ia64 */
686 if ((iobuf->offset & (PAGE_SIZE - 1)) ||
687 (iobuf->length & (PAGE_SIZE - 1)))
690 set = ptlrpc_prep_set();
694 OBD_ALLOC(pga, sizeof(*pga) * iobuf->nr_pages);
696 ptlrpc_set_destroy(set);
700 flags = (rw == WRITE ? OBD_BRW_CREATE : 0) /* | OBD_BRW_DIRECTIO */;
701 offset = ((obd_off)blocknr << inode->i_blkbits);
702 length = iobuf->length;
704 for (i = 0, length = iobuf->length; length > 0;
705 length -= pga[i].count, offset += pga[i].count, i++) { /*i last!*/
706 pga[i].pg = iobuf->maplist[i];
708 /* To the end of the page, or the length, whatever is less */
709 pga[i].count = min_t(int, PAGE_SIZE - (offset & ~PAGE_MASK),
713 //POISON(kmap(iobuf->maplist[i]), 0xc5, PAGE_SIZE);
714 //kunmap(iobuf->maplist[i]);
718 oa.o_id = lsm->lsm_object_id;
719 oa.o_valid = OBD_MD_FLID;
720 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
721 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
724 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
725 LPROC_LL_DIRECT_WRITE, iobuf->length);
727 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
728 LPROC_LL_DIRECT_READ, iobuf->length);
729 rc = obd_brw_async(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
730 ll_i2obdconn(inode), &oa, lsm, iobuf->nr_pages, pga,
733 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
734 "error from obd_brw_async: rc = %d\n", rc);
736 rc = ptlrpc_set_wait(set);
738 CERROR("error from callback: rc = %d\n", rc);
740 ptlrpc_set_destroy(set);
744 OBD_FREE(pga, sizeof(*pga) * iobuf->nr_pages);
751 struct address_space_operations ll_aops = {
752 readpage: ll_readpage,
753 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
754 direct_IO: ll_direct_IO,
756 writepage: ll_writepage,
757 sync_page: block_sync_page,
758 prepare_write: ll_prepare_write,
759 commit_write: ll_commit_write,