/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=8:tabstop=8:
*
- * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
- * Copyright (c) 2001-2003 Cluster File Systems, Inc.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
*
- * This file is part of Lustre, http://www.lustre.org.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
*
- * Lustre is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
*
- * Lustre is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
*
- * You should have received a copy of the GNU General Public License
- * along with Lustre; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * GPL HEADER END
+ */
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
*/
-#ifdef HAVE_KERNEL_CONFIG_H
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/lustre/llite/rw26.c
+ *
+ * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
+ */
+
+#ifndef AUTOCONF_INCLUDED
#include <linux/config.h>
#endif
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/buffer_head.h>
-#include <linux/mpage.h>
#include <linux/writeback.h>
#include <linux/stat.h>
#include <asm/uaccess.h>
-#include <asm/segment.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/smp_lock.h>
#define DEBUG_SUBSYSTEM S_LLITE
+//#include <lustre_mdc.h>
#include <lustre_lite.h>
#include "llite_internal.h"
#include <linux/lustre_compat25.h>
-static int ll_writepage_26(struct page *page, struct writeback_control *wbc)
+/**
+ * Implements Linux VM address_space::invalidatepage() method. This method is
+ * called when the page is truncate from a file, either as a result of
+ * explicit truncate, or when inode is removed from memory (as a result of
+ * final iput(), umount, or memory pressure induced icache shrinking).
+ *
+ * [0, offset] bytes of the page remain valid (this is for a case of not-page
+ * aligned truncate). Lustre leaves partially truncated page in the cache,
+ * relying on struct inode::i_size to limit further accesses.
+ */
+static int cl_invalidatepage(struct page *vmpage, unsigned long offset)
{
- return ll_writepage(page);
+ struct inode *inode;
+ struct lu_env *env;
+ struct cl_page *page;
+ struct cl_object *obj;
+
+ int result;
+ int refcheck;
+
+ LASSERT(PageLocked(vmpage));
+ LASSERT(!PageWriteback(vmpage));
+
+ /*
+ * It is safe to not check anything in invalidatepage/releasepage
+ * below because they are run with page locked and all our io is
+ * happening with locked page too
+ */
+ result = 0;
+ if (offset == 0) {
+ env = cl_env_get(&refcheck);
+ if (!IS_ERR(env)) {
+ inode = vmpage->mapping->host;
+ obj = ll_i2info(inode)->lli_clob;
+ if (obj != NULL) {
+ page = cl_vmpage_page(vmpage, obj);
+ if (page != NULL) {
+ lu_ref_add(&page->cp_reference,
+ "delete", vmpage);
+ cl_page_delete(env, page);
+ result = 1;
+ lu_ref_del(&page->cp_reference,
+ "delete", vmpage);
+ cl_page_put(env, page);
+ }
+ } else
+ LASSERT(vmpage->private == 0);
+ cl_env_put(env, &refcheck);
+ }
+ }
+ return result;
}
-/* It is safe to not check anything in invalidatepage/releasepage below
- because they are run with page locked and all our io is happening with
- locked page too */
#ifdef HAVE_INVALIDATEPAGE_RETURN_INT
static int ll_invalidatepage(struct page *page, unsigned long offset)
{
- if (offset)
- return 0;
- if (PagePrivate(page))
- ll_removepage(page);
- return 1;
+ return cl_invalidatepage(page, offset);
}
-#else
+#else /* !HAVE_INVALIDATEPAGE_RETURN_INT */
static void ll_invalidatepage(struct page *page, unsigned long offset)
{
- if (offset)
- return;
- if (PagePrivate(page))
- ll_removepage(page);
+ cl_invalidatepage(page, offset);
}
#endif
-static int ll_releasepage(struct page *page, gfp_t gfp_mask)
+#ifdef HAVE_RELEASEPAGE_WITH_INT
+#define RELEASEPAGE_ARG_TYPE int
+#else
+#define RELEASEPAGE_ARG_TYPE gfp_t
+#endif
+static int ll_releasepage(struct page *page, RELEASEPAGE_ARG_TYPE gfp_mask)
{
- if (PagePrivate(page))
- ll_removepage(page);
+ void *cookie;
+
+ cookie = cl_env_reenter();
+ ll_invalidatepage(page, 0);
+ cl_env_reexit(cookie);
return 1;
}
+static int ll_set_page_dirty(struct page *vmpage)
+{
+#if 0
+ struct cl_page *page = vvp_vmpage_page_transient(vmpage);
+ struct vvp_object *obj = cl_inode2vvp(vmpage->mapping->host);
+ struct vvp_page *cpg;
+
+ /*
+ * XXX should page method be called here?
+ */
+ LASSERT(&obj->co_cl == page->cp_obj);
+ cpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
+ /*
+ * XXX cannot do much here, because page is possibly not locked:
+ * sys_munmap()->...
+ * ->unmap_page_range()->zap_pte_range()->set_page_dirty().
+ */
+ vvp_write_pending(obj, cpg);
+#endif
+ RETURN(__set_page_dirty_nobuffers(vmpage));
+}
+
#define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
static inline int ll_get_user_pages(int rw, unsigned long user_addr,
return -EFBIG;
}
- page_count = ((user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT)-
- (user_addr >> CFS_PAGE_SHIFT);
+ page_count = (user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
+ page_count -= user_addr >> CFS_PAGE_SHIFT;
- OBD_ALLOC_GFP(*pages, page_count * sizeof(**pages), GFP_KERNEL);
+ OBD_ALLOC_WAIT(*pages, page_count * sizeof(**pages));
if (*pages) {
down_read(¤t->mm->mmap_sem);
result = get_user_pages(current, current->mm, user_addr,
OBD_FREE(pages, npages * sizeof(*pages));
}
-static ssize_t ll_direct_IO_26_seg(int rw, struct inode *inode,
- struct address_space *mapping,
- struct lov_stripe_md *lsm,
- size_t size, loff_t file_offset,
- struct page **pages, int page_count)
+ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
+ int rw, struct inode *inode,
+ struct ll_dio_pages *pv)
{
- struct brw_page *pga;
- struct obdo oa;
- int i, rc = 0;
- size_t length;
+ struct cl_page *clp;
+ struct ccc_page *clup;
+ struct cl_2queue *queue;
+ struct cl_object *obj = io->ci_obj;
+ struct cl_sync_io *anchor = &ccc_env_info(env)->cti_sync_io;
+ int i;
+ ssize_t rc = 0;
+ loff_t file_offset = pv->ldp_start_offset;
+ size_t size = pv->ldp_size;
+ int page_count = pv->ldp_nr;
+ struct page **pages = pv->ldp_pages;
+ size_t page_size = cl_page_size(obj);
ENTRY;
- OBD_ALLOC(pga, sizeof(*pga) * page_count);
- if (!pga) {
- CDEBUG(D_VFSTRACE, "sizeof(*pga) = %u page_count = %u\n",
- (int)sizeof(*pga), page_count);
- RETURN(-ENOMEM);
- }
+ cl_sync_io_init(anchor, page_count);
+
+ queue = &io->ci_queue;
+ cl_2queue_init(queue);
+ for (i = 0; i < page_count; i++) {
+ if (pv->ldp_offsets)
+ file_offset = pv->ldp_offsets[i];
+ LASSERT(!(file_offset & (page_size - 1)));
+ clp = cl_page_find(env, obj, cl_index(obj, file_offset),
+ pv->ldp_pages[i], CPT_TRANSIENT);
+ if (IS_ERR(clp)) {
+ rc = PTR_ERR(clp);
+ break;
+ }
- for (i = 0, length = size; length > 0;
- length -=pga[i].count, file_offset +=pga[i].count,i++) {/*i last!*/
- pga[i].pg = pages[i];
- pga[i].off = file_offset;
- /* To the end of the page, or the length, whatever is less */
- pga[i].count = min_t(int, CFS_PAGE_SIZE -(file_offset & ~CFS_PAGE_MASK),
- length);
- pga[i].flag = 0;
- if (rw == READ)
- POISON_PAGE(pages[i], 0x0d);
- }
+ /* check the page type: if the page is a host page, then do
+ * write directly */
+ /*
+ * Very rare case that the host pages can be found for
+ * directIO case, since linux kernel truncated all covered
+ * pages before getting here. So, to make the OST happy(to
+ * write a contiguous region), all pages are issued
+ * here. -jay */
+ if (clp->cp_type == CPT_CACHEABLE) {
+ cfs_page_t *vmpage = cl_page_vmpage(env, clp);
+ cfs_page_t *src_page;
+ cfs_page_t *dst_page;
+ void *src;
+ void *dst;
+
+ src_page = (rw == WRITE) ? pages[i] : vmpage;
+ dst_page = (rw == WRITE) ? vmpage : pages[i];
+
+ src = kmap_atomic(src_page, KM_USER0);
+ dst = kmap_atomic(dst_page, KM_USER1);
+ memcpy(dst, (const void *)src, min(page_size, size));
+ kunmap_atomic(dst, KM_USER1);
+ kunmap_atomic(src, KM_USER0);
+
+ /* make sure page will be added to the transfer by
+ * cl_io_submit()->...->vvp_page_prep_write(). */
+ if (rw == WRITE)
+ set_page_dirty(vmpage);
+ /*
+ * If direct-io read finds up-to-date page in the
+ * cache, just copy it to the user space. Page will be
+ * filtered out by vvp_page_prep_read(). This
+ * preserves an invariant, that page is read at most
+ * once, see cl_page_flags::CPF_READ_COMPLETED.
+ */
+ }
- ll_inode_fill_obdo(inode, rw, &oa);
+ rc = cl_page_own(env, io, clp);
+ if (rc) {
+ LASSERT(clp->cp_state == CPS_FREEING);
+ cl_page_put(env, clp);
+ break;
+ }
+
+ clup = cl2ccc_page(cl_page_at(clp, &vvp_device_type));
+ clup->cpg_sync_io = anchor;
+ cl_2queue_add(queue, clp);
+
+ /* drop the reference count for cl_page_find, so that the page
+ * will be freed in cl_2queue_fini. */
+ cl_page_put(env, clp);
+ /*
+ * Set page clip to tell transfer formation engine that page
+ * has to be sent even if it is beyond KMS.
+ */
+ cl_page_clip(env, clp, 0, min(size, page_size));
+ size -= page_size;
+ file_offset += page_size;
+ }
- rc = obd_brw_rqset(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
- ll_i2obdexp(inode), &oa, lsm, page_count, pga, NULL);
if (rc == 0) {
- rc = size;
- if (rw == WRITE) {
- lov_stripe_lock(lsm);
- obd_adjust_kms(ll_i2obdexp(inode), lsm, file_offset, 0);
- lov_stripe_unlock(lsm);
+ rc = cl_io_submit_rw(env, io, rw == READ ? CRT_READ : CRT_WRITE,
+ queue, CRP_NORMAL);
+ if (rc == 0) {
+ /*
+ * If some pages weren't sent for any reason (e.g.,
+ * direct-io read found up-to-date pages in the
+ * cache), count them as completed to avoid infinite
+ * wait.
+ */
+ cl_page_list_for_each(clp, &queue->c2_qin)
+ cl_sync_io_note(anchor, +1);
+ /* wait for the IO to be finished. */
+ rc = cl_sync_io_wait(env, io, &queue->c2_qout,
+ anchor) ?: pv->ldp_size;
}
}
- OBD_FREE(pga, sizeof(*pga) * page_count);
+ cl_2queue_discard(env, io, queue);
+ cl_2queue_disown(env, io, queue);
+ cl_2queue_fini(env, queue);
RETURN(rc);
}
+EXPORT_SYMBOL(ll_direct_rw_pages);
+
+static ssize_t ll_direct_IO_26_seg(const struct lu_env *env, struct cl_io *io,
+ int rw, struct inode *inode,
+ struct address_space *mapping,
+ size_t size, loff_t file_offset,
+ struct page **pages, int page_count)
+{
+ struct ll_dio_pages pvec = { .ldp_pages = pages,
+ .ldp_nr = page_count,
+ .ldp_size = size,
+ .ldp_offsets = NULL,
+ .ldp_start_offset = file_offset
+ };
+
+ return ll_direct_rw_pages(env, io, rw, inode, &pvec);
+}
/* This is the maximum size of a single O_DIRECT request, based on a 128kB
* kmalloc limit. We need to fit all of the brw_page structs, each one
const struct iovec *iov, loff_t file_offset,
unsigned long nr_segs)
{
+ struct lu_env *env;
+ struct cl_io *io;
struct file *file = iocb->ki_filp;
struct inode *inode = file->f_mapping->host;
+ struct ccc_object *obj = cl_inode2ccc(inode);
ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
struct ll_inode_info *lli = ll_i2info(inode);
- unsigned long seg;
+ struct lov_stripe_md *lsm = lli->lli_smd;
+ unsigned long seg = 0;
size_t size = MAX_DIO_SIZE;
+ int refcheck;
ENTRY;
if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
RETURN(-EBADF);
- /* FIXME: io smaller than CFS_PAGE_SIZE is broken on ia64 ??? */
- if ((file_offset & (~CFS_PAGE_MASK)) || (count & ~CFS_PAGE_MASK))
+ /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
+ if ((file_offset & ~CFS_PAGE_MASK) || (count & ~CFS_PAGE_MASK))
RETURN(-EINVAL);
- CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), size="LPSZ" (max "LPSZ
- "), offset=%lld=%llx, pages "LPSZ" (max "LPSZ")\n",
+ CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), size="LPSZ" (max %lu), "
+ "offset=%lld=%llx, pages "LPSZ" (max %lu)\n",
inode->i_ino, inode->i_generation, inode, count, MAX_DIO_SIZE,
file_offset, file_offset, count >> CFS_PAGE_SHIFT,
MAX_DIO_SIZE >> CFS_PAGE_SHIFT);
- if (rw == WRITE)
- lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
- LPROC_LL_DIRECT_WRITE, count);
- else
- lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
- LPROC_LL_DIRECT_READ, count);
-
/* Check that all user buffers are aligned as well */
for (seg = 0; seg < nr_segs; seg++) {
if (((unsigned long)iov[seg].iov_base & ~CFS_PAGE_MASK) ||
RETURN(-EINVAL);
}
+ env = cl_env_get(&refcheck);
+ LASSERT(!IS_ERR(env));
+ io = ccc_env_io(env)->cui_cl.cis_io;
+ LASSERT(io != NULL);
+
+ /* 0. Need locking between buffered and direct access. and race with
+ *size changing by concurrent truncates and writes.
+ * 1. Need inode sem to operate transient pages. */
+ if (rw == READ)
+ LOCK_INODE_MUTEX(inode);
+
+ LASSERT(obj->cob_transient_pages == 0);
for (seg = 0; seg < nr_segs; seg++) {
size_t iov_left = iov[seg].iov_len;
unsigned long user_addr = (unsigned long)iov[seg].iov_base;
+ if (rw == READ) {
+ if (file_offset >= inode->i_size)
+ break;
+ if (file_offset + iov_left > inode->i_size)
+ iov_left = inode->i_size - file_offset;
+ }
+
while (iov_left > 0) {
struct page **pages;
int page_count;
&pages);
LASSERT(page_count != 0);
if (page_count > 0) {
- result = ll_direct_IO_26_seg(rw, inode,
+ result = ll_direct_IO_26_seg(env, io, rw, inode,
file->f_mapping,
- lli->lli_smd,
min(size,iov_left),
file_offset, pages,
page_count);
(int)size);
continue;
}
- if (tot_bytes > 0)
- RETURN(tot_bytes);
- RETURN(page_count < 0 ? page_count : result);
+
+ if (tot_bytes <= 0)
+ tot_bytes = page_count < 0 ? page_count : result;
+ GOTO(out, tot_bytes);
}
tot_bytes += result;
user_addr += result;
}
}
+out:
+ LASSERT(obj->cob_transient_pages == 0);
+ if (rw == READ)
+ UNLOCK_INODE_MUTEX(inode);
+
+ if (tot_bytes > 0) {
+ if (rw == WRITE) {
+ lov_stripe_lock(lsm);
+ obd_adjust_kms(ll_i2dtexp(inode), lsm, file_offset, 0);
+ lov_stripe_unlock(lsm);
+ }
+ }
+
+ cl_env_put(env, &refcheck);
RETURN(tot_bytes);
}
.readpage = ll_readpage,
// .readpages = ll_readpages,
.direct_IO = ll_direct_IO_26,
- .writepage = ll_writepage_26,
+ .writepage = ll_writepage,
.writepages = generic_writepages,
- .set_page_dirty = __set_page_dirty_nobuffers,
+ .set_page_dirty = ll_set_page_dirty,
.sync_page = NULL,
.prepare_write = ll_prepare_write,
.commit_write = ll_commit_write,
.invalidatepage = ll_invalidatepage,
- .releasepage = ll_releasepage,
+ .releasepage = (void *)ll_releasepage,
.bmap = NULL
};
-
-static int wait_on_page_locked_range(struct address_space *mapping,
- pgoff_t start, pgoff_t end)
-{
- pgoff_t index;
- struct page *page;
- int ret = 0;
-
- if (end < start)
- return 0;
-
- for(index = start; index < end; index++) {
- page = find_get_page(mapping, index);
- if (page == NULL)
- continue;
-
- wait_on_page_locked(page);
- if (PageError(page))
- ret = -EIO;
- cond_resched();
- }
-
- /* Check for outstanding write errors */
- if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
- ret = -ENOSPC;
- if (test_and_clear_bit(AS_EIO, &mapping->flags))
- ret = -EIO;
-
- return ret;
-}
-
-int ll_sync_page_range(struct inode *inode, struct address_space *mapping,
- loff_t pos, size_t count)
-{
- pgoff_t start = pos >> CFS_PAGE_SHIFT;
- pgoff_t end = (pos + count - 1) >> CFS_PAGE_SHIFT;
- struct writeback_control wbc;
- int ret;
-
- wbc.sync_mode = WB_SYNC_ALL;
- wbc.nr_to_write = mapping->nrpages * 2;
-#ifdef HAVE_WB_RANGE_START
- wbc.range_start = start;
- wbc.range_end = end;
-#else
- wbc.start = start;
- wbc.end = end;
-#endif
- ret = generic_writepages(mapping, &wbc);
-
- if (ret == 0)
- ret = wait_on_page_locked_range(mapping, start, end);
- return ret;
-}
-