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 pgs = kmalloc(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 /* to balance the find_get_page ref the other pages get that is
218 * decrefed on teardown.. */
219 page_cache_get(page);
221 unsigned long index ;
224 pgs[npgs].off = ((obd_off)page->index) << PAGE_CACHE_SHIFT;
226 pgs[npgs].count = PAGE_SIZE;
227 /* XXX Workaround for BA OSTs returning short reads at EOF.
228 * The linux OST will return the full page, zero-filled at the
229 * end, which will just overwrite the data we set here. Bug
230 * 593 relates to fixing this properly.
232 if (inode->i_size < pgs[npgs].off + PAGE_SIZE) {
233 int count = inode->i_size - pgs[npgs].off;
234 void *addr = kmap(page);
235 pgs[npgs].count = count;
236 //POISON(addr, 0x7c, count);
237 memset(addr + count, 0, PAGE_SIZE - count);
242 if (npgs == max_pages)
246 * find pages ahead of us that we can read in.
247 * grab_cache_page waits on pages that are locked so
248 * we first try find_get_page, which doesn't. this stops
249 * the worst case behaviour of racing threads waiting on
250 * each other, but doesn't remove it entirely.
252 for (index = page->index + 1, page = NULL;
253 page == NULL && index < end_index; index++) {
255 /* see if the page already exists and needs updating */
256 page = find_get_page(inode->i_mapping, index);
258 if (Page_Uptodate(page) || TryLockPage(page))
260 if (!page->mapping || Page_Uptodate(page))
263 /* ok, we have to create it.. */
264 page = grab_cache_page(inode->i_mapping, index);
267 if (Page_Uptodate(page))
276 page_cache_release(page);
282 if ((oa = obdo_alloc()) == NULL) {
283 CERROR("ENOMEM allocing obdo\n");
285 } else if ((set = ptlrpc_prep_set()) == NULL) {
286 CERROR("ENOMEM allocing request set\n");
290 struct ll_file_data *fd = file->private_data;
292 oa->o_id = lli->lli_smd->lsm_object_id;
293 memcpy(obdo_handle(oa), &fd->fd_ost_och.och_fh,
294 sizeof(fd->fd_ost_och.och_fh));
295 oa->o_valid = OBD_MD_FLID | OBD_MD_FLHANDLE;
296 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME);
298 rc = obd_brw_async(OBD_BRW_READ, ll_i2obdconn(inode), oa,
299 ll_i2info(inode)->lli_smd, npgs, pgs,
302 rc = ptlrpc_set_wait(set);
303 ptlrpc_set_destroy(set);
305 obdo_refresh_inode(inode, oa, oa->o_valid);
306 if (rc && rc != -EIO)
307 CERROR("error from obd_brw_async: rc = %d\n", rc);
315 SetPageUptodate(page);
317 page_cache_release(page);
324 /* this isn't where truncate starts. roughly:
325 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
326 * we grab the lock back in setattr_raw to avoid races. */
327 void ll_truncate(struct inode *inode)
329 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
333 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
334 inode->i_generation, inode);
336 /* object not yet allocated */
338 CERROR("truncate on inode %lu with no objects\n", inode->i_ino);
343 /* vmtruncate will just throw away our dirty pages, make sure
344 * we don't think they're still dirty, being careful to round
345 * i_size to the first whole page that was tossed */
346 err = ll_clear_dirty_pages(ll_i2obdconn(inode), lsm,
347 (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT,
350 oa.o_id = lsm->lsm_object_id;
351 oa.o_valid = OBD_MD_FLID;
352 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
353 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
355 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
356 oa.o_id, inode->i_size);
358 /* truncate == punch from new size to absolute end of file */
359 err = obd_punch(ll_i2obdconn(inode), &oa, lsm, inode->i_size,
360 OBD_OBJECT_EOF, NULL);
362 CERROR("obd_truncate fails (%d) ino %lu\n", err, inode->i_ino);
364 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
365 OBD_MD_FLATIME | OBD_MD_FLMTIME |
372 //#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
374 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
377 struct inode *inode = page->mapping->host;
378 struct ll_inode_info *lli = ll_i2info(inode);
379 struct ll_file_data *fd = file->private_data;
380 struct lov_stripe_md *lsm = lli->lli_smd;
381 obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
387 if (!PageLocked(page))
390 if (PageUptodate(page))
393 //POISON(addr + from, 0xca, to - from);
395 /* Check to see if we should return -EIO right away */
398 pg.count = PAGE_SIZE;
400 rc = obd_brw(OBD_BRW_CHECK, ll_i2obdconn(inode), NULL, lsm, 1,&pg,NULL);
404 /* We're completely overwriting an existing page, so _don't_ set it up
405 * to date until commit_write */
406 if (from == 0 && to == PAGE_SIZE)
409 /* If are writing to a new page, no need to read old data.
410 * the extent locking and getattr procedures in ll_file_write have
411 * guaranteed that i_size is stable enough for our zeroing needs */
412 if (inode->i_size <= offset) {
413 memset(kmap(page), 0, PAGE_SIZE);
415 GOTO(prepare_done, rc = 0);
418 oa.o_id = lsm->lsm_object_id;
419 oa.o_mode = inode->i_mode;
420 memcpy(obdo_handle(&oa), &fd->fd_ost_och.och_fh,
421 sizeof(fd->fd_ost_och.och_fh));
422 oa.o_valid = OBD_MD_FLID |OBD_MD_FLMODE |OBD_MD_FLTYPE |OBD_MD_FLHANDLE;
424 rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
426 obdo_refresh_inode(inode, &oa, oa.o_valid);
431 SetPageUptodate(page);
437 * background file writeback. This is called regularly from kupdated to write
438 * dirty data, from kswapd when memory is low, and from filemap_fdatasync when
439 * super blocks or inodes are synced..
441 * obd_brw errors down in _batch_writepage are ignored, so pages are always
442 * unlocked. Also, there is nobody to return an error code to from here - the
443 * application may not even be running anymore.
445 * this should be async so that things like kswapd can have a chance to
446 * free some more pages that our allocating writeback may need, but it isn't
449 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
450 static unsigned long ll_local_cache_dirty_pages;
451 static unsigned long ll_max_dirty_pages = 20 * 1024 * 1024 / PAGE_SIZE;
453 static spinlock_t ll_local_cache_page_count_lock = SPIN_LOCK_UNLOCKED;
455 int ll_rd_dirty_pages(char *page, char **start, off_t off, int count, int *eof,
458 unsigned long dirty_count;
459 spin_lock(&ll_local_cache_page_count_lock);
460 dirty_count = ll_local_cache_dirty_pages;
461 spin_unlock(&ll_local_cache_page_count_lock);
462 return snprintf(page, count, "%lu\n", dirty_count);
465 int ll_rd_max_dirty_pages(char *page, char **start, off_t off, int count,
466 int *eof, void *data)
468 unsigned long max_dirty;
469 spin_lock(&ll_local_cache_page_count_lock);
470 max_dirty = ll_max_dirty_pages;
471 spin_unlock(&ll_local_cache_page_count_lock);
472 return snprintf(page, count, "%lu\n", max_dirty);
475 int ll_wr_max_dirty_pages(struct file *file, const char *buffer,
476 unsigned long count, void *data)
478 unsigned long max_dirty;
479 signed long max_dirty_signed;
480 char kernbuf[20], *end;
482 if (count > (sizeof(kernbuf) - 1))
485 if (copy_from_user(kernbuf, buffer, count))
488 kernbuf[count] = '\0';
490 max_dirty_signed = simple_strtol(kernbuf, &end, 0);
493 max_dirty = (unsigned long)max_dirty_signed;
496 if (max_dirty < ll_local_cache_dirty_pages)
497 flush_to_new_max_dirty();
500 spin_lock(&ll_local_cache_page_count_lock);
501 CDEBUG(D_CACHE, "changing max_dirty from %lu to %lu\n",
502 ll_max_dirty_pages, max_dirty);
503 ll_max_dirty_pages = max_dirty;
504 spin_unlock(&ll_local_cache_page_count_lock);
508 static int ll_local_cache_full(void)
511 spin_lock(&ll_local_cache_page_count_lock);
512 if (ll_max_dirty_pages &&
513 ll_local_cache_dirty_pages >= ll_max_dirty_pages) {
516 spin_unlock(&ll_local_cache_page_count_lock);
517 /* XXX instrument? */
518 /* XXX trigger async writeback when full, or 75% of full? */
522 static void ll_local_cache_flushed_pages(unsigned long pgcount)
524 unsigned long dirty_count;
525 spin_lock(&ll_local_cache_page_count_lock);
526 dirty_count = ll_local_cache_dirty_pages;
527 ll_local_cache_dirty_pages -= pgcount;
528 CDEBUG(D_CACHE, "dirty pages: %lu->%lu)\n",
529 dirty_count, ll_local_cache_dirty_pages);
530 spin_unlock(&ll_local_cache_page_count_lock);
531 LASSERT(dirty_count >= pgcount);
534 static void ll_local_cache_dirtied_pages(unsigned long pgcount)
536 unsigned long dirty_count;
537 spin_lock(&ll_local_cache_page_count_lock);
538 dirty_count = ll_local_cache_dirty_pages;
539 ll_local_cache_dirty_pages += pgcount;
540 CDEBUG(D_CACHE, "dirty pages: %lu->%lu\n",
541 dirty_count, ll_local_cache_dirty_pages);
542 spin_unlock(&ll_local_cache_page_count_lock);
543 /* XXX track maximum cached, report to lprocfs */
546 int ll_clear_dirty_pages(struct lustre_handle *conn, struct lov_stripe_md *lsm,
547 unsigned long start, unsigned long end)
549 unsigned long cleared;
553 rc = obd_clear_dirty_pages(conn, lsm, start, end, &cleared);
555 ll_local_cache_flushed_pages(cleared);
559 int ll_mark_dirty_page(struct lustre_handle *conn, struct lov_stripe_md *lsm,
565 if (ll_local_cache_full())
568 rc = obd_mark_page_dirty(conn, lsm, index);
570 ll_local_cache_dirtied_pages(1);
574 static int ll_writepage(struct page *page)
576 struct inode *inode = page->mapping->host;
580 CDEBUG(D_CACHE, "page %p [lau %d] inode %p\n", page,
581 PageLaunder(page), inode);
582 LASSERT(PageLocked(page));
584 oa.o_id = ll_i2info(inode)->lli_smd->lsm_object_id;
585 oa.o_valid = OBD_MD_FLID;
586 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
587 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
589 RETURN(ll_batch_writepage(inode, &oa, page));
593 * we really don't want to start writeback here, we want to give callers some
594 * time to further dirty the pages before we write them out.
596 static int ll_commit_write(struct file *file, struct page *page,
597 unsigned from, unsigned to)
599 struct inode *inode = page->mapping->host;
604 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
605 LASSERT(inode == file->f_dentry->d_inode);
606 LASSERT(PageLocked(page));
608 CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
609 inode, page, from, to, page->index);
610 if (!PageDirty(page)) {
611 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
612 LPROC_LL_DIRTY_MISSES);
613 rc = ll_mark_dirty_page(ll_i2obdconn(inode),
614 ll_i2info(inode)->lli_smd,
616 if (rc < 0 && rc != -EDQUOT)
617 RETURN(rc); /* XXX lproc counter here? */
619 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
620 LPROC_LL_DIRTY_HITS);
623 size = (((obd_off)page->index) << PAGE_SHIFT) + to;
624 if (size > inode->i_size)
625 inode->i_size = size;
627 SetPageUptodate(page);
628 set_page_dirty(page);
630 /* This means that we've hit either the local cache limit or the limit
631 * of the OST's grant. */
633 struct ll_file_data *fd = file->private_data;
637 oa.o_id = ll_i2info(inode)->lli_smd->lsm_object_id;
638 memcpy(obdo_handle(&oa), &fd->fd_ost_och.och_fh,
639 sizeof(fd->fd_ost_och.och_fh));
640 oa.o_valid = OBD_MD_FLID | OBD_MD_FLHANDLE;
641 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
642 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
644 rc = ll_batch_writepage(inode, &oa, page);
645 lock_page(page); /* caller expects to unlock */
650 } /* ll_commit_write */
652 static int ll_writepage(struct page *page,
653 struct writeback_control *wbc)
658 static int ll_commit_write(struct file *file, struct page *page,
659 unsigned from, unsigned to)
665 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
666 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
667 unsigned long blocknr, int blocksize)
669 struct ll_inode_info *lli = ll_i2info(inode);
670 struct lov_stripe_md *lsm = lli->lli_smd;
671 struct brw_page *pga;
672 struct ptlrpc_request_set *set;
674 int length, i, flags, rc = 0;
678 if (!lsm || !lsm->lsm_object_id)
681 if ((iobuf->offset & (blocksize - 1)) ||
682 (iobuf->length & (blocksize - 1)))
685 set = ptlrpc_prep_set();
689 OBD_ALLOC(pga, sizeof(*pga) * iobuf->nr_pages);
691 ptlrpc_set_destroy(set);
695 flags = (rw == WRITE ? OBD_BRW_CREATE : 0) /* | OBD_BRW_DIRECTIO */;
696 offset = ((obd_off)blocknr << inode->i_blkbits);
697 length = iobuf->length;
699 for (i = 0, length = iobuf->length; length > 0;
700 length -= pga[i].count, offset += pga[i].count, i++) { /*i last!*/
701 pga[i].pg = iobuf->maplist[i];
703 /* To the end of the page, or the length, whatever is less */
704 pga[i].count = min_t(int, PAGE_SIZE - (offset & ~PAGE_MASK),
708 //POISON(kmap(iobuf->maplist[i]), 0xc5, PAGE_SIZE);
709 //kunmap(iobuf->maplist[i]);
713 oa.o_id = lsm->lsm_object_id;
714 oa.o_valid = OBD_MD_FLID;
715 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
716 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
719 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
720 LPROC_LL_DIRECT_WRITE, iobuf->length);
722 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
723 LPROC_LL_DIRECT_READ, iobuf->length);
724 rc = obd_brw_async(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
725 ll_i2obdconn(inode), &oa, lsm, iobuf->nr_pages, pga,
728 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
729 "error from obd_brw_async: rc = %d\n", rc);
731 rc = ptlrpc_set_wait(set);
733 CERROR("error from callback: rc = %d\n", rc);
735 ptlrpc_set_destroy(set);
739 OBD_FREE(pga, sizeof(*pga) * iobuf->nr_pages);
746 struct address_space_operations ll_aops = {
747 readpage: ll_readpage,
748 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
749 direct_IO: ll_direct_IO,
751 writepage: ll_writepage,
752 sync_page: block_sync_page,
753 prepare_write: ll_prepare_write,
754 commit_write: ll_commit_write,