Whamcloud - gitweb
b=16150
[fs/lustre-release.git] / lustre / liblustre / rw.c
index 47ac443..53bda1f 100644 (file)
@@ -1,24 +1,41 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Lustre Light block IO
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *  Copyright (c) 2002, 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.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/liblustre/rw.c
+ *
+ * Lustre Light block IO
  */
 
 #define DEBUG_SUBSYSTEM S_LLITE
 #include <assert.h>
 #include <time.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/queue.h>
 #include <fcntl.h>
+#include <sys/uio.h>
 
 #include <sysio.h>
+#ifdef HAVE_XTIO_H
+#include <xtio.h>
+#endif
 #include <fs.h>
 #include <mount.h>
 #include <inode.h>
+#ifdef HAVE_FILE_H
 #include <file.h>
+#endif
 
 #undef LIST_HEAD
 
 #include "llite_lib.h"
 
+struct llu_io_group
+{
+        struct obd_io_group    *lig_oig;
+        struct inode           *lig_inode;
+        struct lustre_rw_params *lig_params;
+        int                     lig_maxpages;
+        int                     lig_npages;
+        __u64                   lig_rwcount;
+        struct ll_async_page   *lig_llaps;
+        cfs_page_t             *lig_pages;
+        void                   *lig_llap_cookies;
+};
+
+#define LLU_IO_GROUP_SIZE(x) \
+        (sizeof(struct llu_io_group) + \
+         (sizeof(struct ll_async_page) + \
+          sizeof(cfs_page_t) + \
+          llap_cookie_size) * (x))
+
+struct llu_io_session
+{
+        struct inode           *lis_inode;
+        int                     lis_cmd;
+        int                     lis_max_groups;
+        int                     lis_ngroups;
+        struct llu_io_group    *lis_groups[0];
+};
+#define LLU_IO_SESSION_SIZE(x)  \
+        (sizeof(struct llu_io_session) + (x) * 2 * sizeof(void *))
+
+
+typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
+                                _SYSIO_OFF_T pos, ssize_t len,
+                                void *private);
+
+size_t llap_cookie_size;
+
 static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
 {
         struct llu_inode_info *lli = llu_i2info(inode);
@@ -49,8 +110,7 @@ static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock
         struct {
                 char name[16];
                 struct ldlm_lock *lock;
-                struct lov_stripe_md *lsm;
-        } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
+        } key = { .name = KEY_LOCK_TO_STRIPE, .lock = lock };
         __u32 stripe, vallen = sizeof(stripe);
         int rc;
         ENTRY;
@@ -59,7 +119,7 @@ static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock
                 RETURN(0);
 
         /* get our offset in the lov */
-        rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
+        rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe, lsm);
         if (rc != 0) {
                 CERROR("obd_get_info: rc = %d\n", rc);
                 LBUG();
@@ -68,9 +128,9 @@ static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock
         RETURN(stripe);
 }
 
-static int llu_extent_lock_callback(struct ldlm_lock *lock,
-                                    struct ldlm_lock_desc *new, void *data,
-                                    int flag)
+int llu_extent_lock_cancel_cb(struct ldlm_lock *lock,
+                              struct ldlm_lock_desc *new, void *data,
+                              int flag)
 {
         struct lustre_handle lockh = { 0 };
         int rc;
@@ -110,12 +170,14 @@ static int llu_extent_lock_callback(struct ldlm_lock *lock,
                 lsm = lli->lli_smd;
 
                 stripe = llu_lock_to_stripe_offset(inode, lock);
+                lock_res_and_lock(lock);
                 kms = ldlm_extent_shift_kms(lock,
-                                            lsm->lsm_oinfo[stripe].loi_kms);
-                if (lsm->lsm_oinfo[stripe].loi_kms != kms)
+                                            lsm->lsm_oinfo[stripe]->loi_kms);
+                unlock_res_and_lock(lock);
+                if (lsm->lsm_oinfo[stripe]->loi_kms != kms)
                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
-                                   lsm->lsm_oinfo[stripe].loi_kms, kms);
-                lsm->lsm_oinfo[stripe].loi_kms = kms;
+                                   lsm->lsm_oinfo[stripe]->loi_kms, kms);
+                lsm->lsm_oinfo[stripe]->loi_kms = kms;
 iput:
                 I_RELE(inode);
                 break;
@@ -133,7 +195,7 @@ static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
         struct inode *inode = llu_inode_from_lock(lock);
         struct llu_inode_info *lli;
         struct ost_lvb *lvb;
-        int rc, size = sizeof(*lvb), stripe = 0;
+        int rc, stripe = 0;
         ENTRY;
 
         if (inode == NULL)
@@ -148,180 +210,186 @@ static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
         if (lli->lli_smd->lsm_stripe_count > 1)
                 stripe = llu_lock_to_stripe_offset(inode, lock);
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
+        req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
+                             sizeof(*lvb));
+        rc = req_capsule_server_pack(&req->rq_pill);
         if (rc) {
-                CERROR("lustre_pack_reply: %d\n", rc);
+                CERROR("failed pack reply: %d\n", rc);
                 GOTO(iput, rc);
         }
 
-        lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
-        lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
+        lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
+        lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe]->loi_kms;
 
-        LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
-                   lli->lli_st_size, stripe, lvb->lvb_size);
+        LDLM_DEBUG(lock, "i_size: "LPU64" -> stripe number %u -> kms "LPU64,
+                   (__u64)llu_i2stat(inode)->st_size, stripe,lvb->lvb_size);
  iput:
         I_RELE(inode);
  out:
         /* These errors are normal races, so we don't want to fill the console
          * with messages by calling ptlrpc_error() */
         if (rc == -ELDLM_NO_LOCK_DATA)
-                lustre_pack_reply(req, 0, NULL, NULL);
+                lustre_pack_reply(req, 1, NULL, NULL);
 
         req->rq_status = rc;
         return rc;
 }
 
-__u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
-__u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
-
-/* NB: lov_merge_size will prefer locally cached writes if they extend the
- * file (because it prefers KMS over RSS when larger) */
-int llu_glimpse_size(struct inode *inode, struct ost_lvb *lvb)
+static int llu_merge_lvb(struct inode *inode)
 {
         struct llu_inode_info *lli = llu_i2info(inode);
         struct llu_sb_info *sbi = llu_i2sbi(inode);
-        ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
-        struct lustre_handle lockh = { 0 };
-        int rc, flags = LDLM_FL_HAS_INTENT;
+        struct intnl_stat *st = llu_i2stat(inode);
+        struct ost_lvb lvb;
+        int rc;
         ENTRY;
 
-        CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", lli->lli_st_ino);
-
-        rc = obd_enqueue(sbi->ll_osc_exp, lli->lli_smd, LDLM_EXTENT, &policy,
-                         LCK_PR, &flags, llu_extent_lock_callback,
-                         ldlm_completion_ast, llu_glimpse_callback, inode,
-                         sizeof(*lvb), lustre_swab_ost_lvb, &lockh);
-        if (rc > 0)
-                RETURN(-EIO);
-
-        lvb->lvb_size = lov_merge_size(lli->lli_smd, 0);
-        //inode->i_mtime = lov_merge_mtime(lli->lli_smd, inode->i_mtime);
+        inode_init_lvb(inode, &lvb);
+        rc = obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
+        st->st_size = lvb.lvb_size;
+        st->st_blocks = lvb.lvb_blocks;
+        /* handle st_blocks overflow gracefully */
+        if (st->st_blocks < lvb.lvb_blocks)
+                st->st_blocks = ~0UL;
+        st->st_mtime = lvb.lvb_mtime;
+        st->st_atime = lvb.lvb_atime;
+        st->st_ctime = lvb.lvb_ctime;
 
-        CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64"\n", lvb->lvb_size);
+        RETURN(rc);
+}
 
-        obd_cancel(sbi->ll_osc_exp, lli->lli_smd, LCK_PR, &lockh);
+int llu_local_size(struct inode *inode)
+{
+        ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
+        struct llu_inode_info *lli = llu_i2info(inode);
+        struct llu_sb_info *sbi = llu_i2sbi(inode);
+        struct lustre_handle lockh = { 0 };
+        int flags = 0;
+        int rc;
+        ENTRY;
 
+        if (lli->lli_smd->lsm_stripe_count == 0)
+                RETURN(0);
+        
+        rc = obd_match(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT,
+                       &policy, LCK_PR, &flags, inode, &lockh);
+        if (rc < 0)
+                RETURN(rc);
+        else if (rc == 0)
+                RETURN(-ENODATA);
+        
+        rc = llu_merge_lvb(inode);
+        obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
         RETURN(rc);
 }
 
-int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
-                    struct lov_stripe_md *lsm, int mode,
-                    ldlm_policy_data_t *policy, struct lustre_handle *lockh,
-                    int ast_flags)
+/* NB: lov_merge_size will prefer locally cached writes if they extend the
+ * file (because it prefers KMS over RSS when larger) */
+int llu_glimpse_size(struct inode *inode)
 {
-        struct llu_sb_info *sbi = llu_i2sbi(inode);
         struct llu_inode_info *lli = llu_i2info(inode);
+        struct intnl_stat *st = llu_i2stat(inode);
+        struct llu_sb_info *sbi = llu_i2sbi(inode);
+        struct lustre_handle lockh = { 0 };
+        struct ldlm_enqueue_info einfo = { 0 };
+        struct obd_info oinfo = { { { 0 } } };
         int rc;
         ENTRY;
 
-        LASSERT(lockh->cookie == 0);
+        /* If size is cached on the mds, skip glimpse. */
+        if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
+                RETURN(0);
 
-        /* XXX phil: can we do this?  won't it screw the file size up? */
-        if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
-            (sbi->ll_flags & LL_SBI_NOLCK))
+        CDEBUG(D_DLMTRACE, "Glimpsing inode "LPU64"\n", (__u64)st->st_ino);
+
+        if (!lli->lli_smd) {
+                CDEBUG(D_DLMTRACE, "No objects for inode "LPU64"\n", 
+                       (__u64)st->st_ino);
                 RETURN(0);
+        }
 
-        CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
-               lli->lli_st_ino, policy->l_extent.start, policy->l_extent.end);
+        einfo.ei_type = LDLM_EXTENT;
+        einfo.ei_mode = LCK_PR;
+        einfo.ei_cb_bl = osc_extent_blocking_cb;
+        einfo.ei_cb_cp = ldlm_completion_ast;
+        einfo.ei_cb_gl = llu_glimpse_callback;
+        einfo.ei_cbdata = inode;
 
-        rc = obd_enqueue(sbi->ll_osc_exp, lsm, LDLM_EXTENT, policy, mode,
-                         &ast_flags, llu_extent_lock_callback,
-                         ldlm_completion_ast, llu_glimpse_callback, inode,
-                         sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
-        if (rc > 0)
-                rc = -EIO;
+        oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
+        oinfo.oi_lockh = &lockh;
+        oinfo.oi_md = lli->lli_smd;
+        oinfo.oi_flags = LDLM_FL_HAS_INTENT;
 
-        if (policy->l_extent.start == 0 &&
-            policy->l_extent.end == OBD_OBJECT_EOF)
-                lli->lli_st_size = lov_merge_size(lsm, 1);
+        rc = obd_enqueue_rqset(sbi->ll_dt_exp, &oinfo, &einfo);
+        if (rc) {
+                CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
+                RETURN(rc > 0 ? -EIO : rc);
+        }
 
-        //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
+        rc = llu_merge_lvb(inode);
+        CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
+               (__u64)st->st_size, (__u64)st->st_blocks);
 
         RETURN(rc);
 }
 
-#if 0
-int llu_extent_lock_no_validate(struct ll_file_data *fd,
-                                struct inode *inode,
-                                struct lov_stripe_md *lsm,
-                                int mode,
-                                struct ldlm_extent *extent,
-                                struct lustre_handle *lockh,
-                                int ast_flags)
+int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
+                    struct lov_stripe_md *lsm, int mode,
+                    ldlm_policy_data_t *policy, struct lustre_handle *lockh,
+                    int ast_flags)
 {
         struct llu_sb_info *sbi = llu_i2sbi(inode);
-        struct llu_inode_info *lli = llu_i2info(inode);
+        struct intnl_stat *st = llu_i2stat(inode);
+        struct ldlm_enqueue_info einfo = { 0 };
+        struct obd_info oinfo = { { { 0 } } };
+        struct ost_lvb lvb;
         int rc;
         ENTRY;
 
-        LASSERT(lockh->cookie == 0);
+        LASSERT(!lustre_handle_is_used(lockh));
+        CLASSERT(ELDLM_OK == 0);
 
         /* XXX phil: can we do this?  won't it screw the file size up? */
         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
-            (sbi->ll_flags & LL_SBI_NOLCK))
+            (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
                 RETURN(0);
 
-        CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
-               lli->lli_st_ino, extent->start, extent->end);
-
-        rc = obd_enqueue(sbi->ll_osc_exp, lsm, NULL, LDLM_EXTENT, extent,
-                         sizeof(extent), mode, &ast_flags,
-                         llu_extent_lock_callback, inode, lockh);
-
-        RETURN(rc);
-}
+        CDEBUG(D_DLMTRACE, "Locking inode %llu, start "LPU64" end "LPU64"\n",
+               (unsigned long long)st->st_ino, policy->l_extent.start,
+               policy->l_extent.end);
 
-/*
- * this grabs a lock and manually implements behaviour that makes it look like
- * the OST is returning the file size with each lock acquisition.
- */
-int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
-                    struct lov_stripe_md *lsm, int mode,
-                    struct ldlm_extent *extent, struct lustre_handle *lockh,
-                    int nonblock)
-{
-        struct llu_inode_info *lli = llu_i2info(inode);
-        struct obd_export *exp = llu_i2obdexp(inode);
-        struct ldlm_extent size_lock;
-        struct lustre_handle match_lockh = {0};
-        int flags, rc, matched;
-        int astflags = nonblock ? LDLM_FL_BLOCK_NOWAIT : 0;
-        ENTRY;
+        einfo.ei_type = LDLM_EXTENT;
+        einfo.ei_mode = mode;
+        einfo.ei_cb_bl = osc_extent_blocking_cb;
+        einfo.ei_cb_cp = ldlm_completion_ast;
+        einfo.ei_cb_gl = llu_glimpse_callback;
+        einfo.ei_cbdata = inode;
 
-        rc = llu_extent_lock_no_validate(fd, inode, lsm, mode, extent,
-                                         lockh, astflags);
-        if (rc != ELDLM_OK)
-                RETURN(rc);
+        oinfo.oi_policy = *policy;
+        oinfo.oi_lockh = lockh;
+        oinfo.oi_md = lsm;
+        oinfo.oi_flags = ast_flags;
 
-        if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
-                RETURN(0);
+        rc = obd_enqueue(sbi->ll_dt_exp, &oinfo, &einfo, NULL);
+        *policy = oinfo.oi_policy;
+        if (rc > 0)
+                rc = -EIO;
 
-        rc = llu_inode_getattr(inode, lsm);
-        if (rc) {
-                llu_extent_unlock(fd, inode, lsm, mode, lockh);
-                RETURN(rc);
-        }
+        inode_init_lvb(inode, &lvb);
+        obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 1);
+        if (policy->l_extent.start == 0 &&
+            policy->l_extent.end == OBD_OBJECT_EOF)
+                st->st_size = lvb.lvb_size;
 
-        size_lock.start = lli->lli_st_size;
-        size_lock.end = OBD_OBJECT_EOF;
-
-        /* XXX I bet we should be checking the lock ignore flags.. */
-        /* FIXME use LDLM_FL_TEST_LOCK instead */
-        flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
-        matched = obd_match(exp, lsm, LDLM_EXTENT, &size_lock,
-                            sizeof(size_lock), LCK_PR, &flags, inode,
-                            &match_lockh);
-
-        /* hey, alright, we hold a size lock that covers the size we
-         * just found, its not going to change for a while.. */
-        if (matched == 1) {
-                set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
-                obd_cancel(exp, lsm, LCK_PR, &match_lockh);
+        if (rc == 0) {
+                st->st_mtime = lvb.lvb_mtime;
+                st->st_atime = lvb.lvb_atime;
+                st->st_ctime = lvb.lvb_ctime;
         }
 
-        RETURN(0);
+        RETURN(rc);
 }
-#endif
 
 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
                 struct lov_stripe_md *lsm, int mode,
@@ -331,12 +399,14 @@ int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
         int rc;
         ENTRY;
 
+        CLASSERT(ELDLM_OK == 0);
+
         /* XXX phil: can we do this?  won't it screw the file size up? */
         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
-            (sbi->ll_flags & LL_SBI_NOLCK))
+            (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
                 RETURN(0);
 
-        rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
+        rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
 
         RETURN(rc);
 }
@@ -347,17 +417,16 @@ struct ll_async_page {
         int             llap_magic;
         void           *llap_cookie;
         int             llap_queued;
-        struct page    *llap_page;
+        cfs_page_t     *llap_page;
         struct inode   *llap_inode;
 };
 
-static struct ll_async_page *llap_from_cookie(void *cookie)
+static inline struct ll_async_page *llap_from_cookie(void *ptr)
 {
-        struct ll_async_page *llap = cookie;
-        if (llap->llap_magic != LLAP_MAGIC)
-                return ERR_PTR(-EINVAL);
-        return llap;
-};
+        struct ll_async_page *ap = ptr;
+        LASSERT(ap->llap_magic == LLAP_MAGIC);
+        return ap;
+}
 
 static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
 {
@@ -368,568 +437,531 @@ static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
         ENTRY;
 
         llap = llap_from_cookie(data);
-        if (IS_ERR(llap)) {
-                EXIT;
-                return;
-        }
-
         inode = llap->llap_inode;
         lsm = llu_i2info(inode)->lli_smd;
 
         oa->o_id = lsm->lsm_object_id;
         oa->o_valid = OBD_MD_FLID;
         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
-        if (cmd == OBD_BRW_WRITE)
-                valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
+        if (cmd & OBD_BRW_WRITE)
+                valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
+                        OBD_MD_FLUID | OBD_MD_FLGID |
+                        OBD_MD_FLFID | OBD_MD_FLGENER;
 
         obdo_from_inode(oa, inode, valid_flags);
         EXIT;
 }
 
-/* called for each page in a completed rpc.*/
-static void llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
+static void llu_ap_update_obdo(void *data, int cmd, struct obdo *oa,
+                               obd_valid valid)
 {
         struct ll_async_page *llap;
-        struct page *page;
+        ENTRY;
 
         llap = llap_from_cookie(data);
-        if (IS_ERR(llap)) {
-                EXIT;
-                return;
-        }
+        obdo_from_inode(oa, llap->llap_inode, valid);
+
+        EXIT;
+}
+
+/* called for each page in a completed rpc.*/
+static int llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
+{
+        struct ll_async_page *llap;
+        cfs_page_t *page;
+        ENTRY;
 
+        llap = llap_from_cookie(data);
         llap->llap_queued = 0;
         page = llap->llap_page;
 
         if (rc != 0) {
-                if (cmd == OBD_BRW_WRITE)
-                        CERROR("writeback error on page %p index %ld: %d\n", 
+                if (cmd & OBD_BRW_WRITE)
+                        CERROR("writeback error on page %p index %ld: %d\n",
                                page, page->index, rc);
         }
-        EXIT;
+        RETURN(0);
+}
+
+static struct obd_capa * llu_ap_lookup_capa(void *data, int cmd)
+{
+        return NULL;
 }
 
 static struct obd_async_page_ops llu_async_page_ops = {
         .ap_make_ready =        NULL,
         .ap_refresh_count =     NULL,
         .ap_fill_obdo =         llu_ap_fill_obdo,
+        .ap_update_obdo =       llu_ap_update_obdo,
         .ap_completion =        llu_ap_completion,
+        .ap_lookup_capa =       llu_ap_lookup_capa,
 };
 
-static
-struct llu_sysio_cookie* get_sysio_cookie(struct inode *inode, int maxpages)
-{
-        struct llu_sysio_cookie *cookie;
-        int rc;
-
-        OBD_ALLOC(cookie, LLU_SYSIO_COOKIE_SIZE(maxpages));
-        if (cookie == NULL)
-                goto out;
-
-        I_REF(inode);
-        cookie->lsc_inode = inode;
-        cookie->lsc_maxpages = maxpages;
-        cookie->lsc_llap = (struct ll_async_page *)(cookie + 1);
-        cookie->lsc_pages = (struct page *) (cookie->lsc_llap + maxpages);
-
-        rc = oig_init(&cookie->lsc_oig);
-        if (rc) {
-                OBD_FREE(cookie, LLU_SYSIO_COOKIE_SIZE(maxpages));
-                cookie = NULL;
-        }
-
-out:
-        return cookie;
-}
-
-static
-void put_sysio_cookie(struct llu_sysio_cookie *cookie)
+static int llu_queue_pio(int cmd, struct llu_io_group *group,
+                         char *buf, size_t count, loff_t pos)
 {
-        struct lov_stripe_md *lsm = llu_i2info(cookie->lsc_inode)->lli_smd;
-        struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
-        struct ll_async_page *llap = cookie->lsc_llap;
-#ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
-        struct page *pages = cookie->lsc_pages;
-#endif
-        int i;
-
-        for (i = 0; i< cookie->lsc_maxpages; i++) {
-                if (llap[i].llap_cookie)
-                        obd_teardown_async_page(exp, lsm, NULL,
-                                                llap[i].llap_cookie);
-#ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
-                if (pages[i]._managed) {
-                        free(pages[i].addr);
-                        pages[i]._managed = 0;
-                }
-#endif
-        }
-
-        I_RELE(cookie->lsc_inode);
-
-        oig_release(cookie->lsc_oig);
-        OBD_FREE(cookie, LLU_SYSIO_COOKIE_SIZE(cookie->lsc_maxpages));
-}
-
-#ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
-/* Note: these code should be removed finally, don't need
- * more cleanup
- */
-static
-int prepare_unaligned_write(struct llu_sysio_cookie *cookie)
-{
-        struct inode *inode = cookie->lsc_inode;
-        struct llu_inode_info *lli = llu_i2info(inode);
-        struct lov_stripe_md *lsm = lli->lli_smd;
-        struct obdo oa;
-        struct page *pages = cookie->lsc_pages;
-        int i, pgidx[2] = {0, cookie->lsc_npages-1};
-        int rc;
-        ENTRY;
-
-        for (i = 0; i < 2; i++) {
-                struct page *oldpage = &pages[pgidx[i]];
-                struct page newpage;
-                struct brw_page pg;
-                char *newbuf;
-
-                if (i == 0 && pgidx[0] == pgidx[1])
-                        continue;
-
-                LASSERT(oldpage->_offset + oldpage->_count <= PAGE_CACHE_SIZE);
-
-                if (oldpage->_count == PAGE_CACHE_SIZE)
-                        continue;
-
-                if (oldpage->index << PAGE_CACHE_SHIFT >=
-                    lli->lli_st_size)
-                        continue;
-
-                newbuf = malloc(PAGE_CACHE_SIZE);
-                if (!newbuf)
-                        return -ENOMEM;
-
-                newpage.index = oldpage->index;
-                newpage.addr = newbuf;
-
-                pg.pg = &newpage;
-                pg.off = ((obd_off)newpage.index << PAGE_CACHE_SHIFT);
-                if (pg.off + PAGE_CACHE_SIZE > lli->lli_st_size)
-                        pg.count = lli->lli_st_size % PAGE_CACHE_SIZE;
-                else
-                        pg.count = PAGE_CACHE_SIZE;
-                pg.flag = 0;
-
-                oa.o_id = lsm->lsm_object_id;
-                oa.o_mode = lli->lli_st_mode;
-                oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
-
-                /* issue read */
-                rc = obd_brw(OBD_BRW_READ, llu_i2obdexp(inode), &oa, lsm, 1, &pg, NULL);
-                if (rc) {
-                        free(newbuf);
-                        RETURN(rc);
-                }
-
-                /* copy page content, and reset page params */
-                memcpy(newbuf + oldpage->_offset,
-                       (char*)oldpage->addr + oldpage->_offset,
-                       oldpage->_count);
-
-                oldpage->addr = newbuf;
-                if ((((obd_off)oldpage->index << PAGE_CACHE_SHIFT) +
-                    oldpage->_offset + oldpage->_count) > lli->lli_st_size)
-                        oldpage->_count += oldpage->_offset;
-                else
-                        oldpage->_count = PAGE_CACHE_SIZE;
-                oldpage->_offset = 0;
-                oldpage->_managed = 1;
-        }
-
-        RETURN(0);
-}
-#endif
-
-static
-int llu_prep_async_io(struct llu_sysio_cookie *cookie, int cmd,
-                      char *buf, loff_t pos, size_t count)
-{
-        struct llu_inode_info *lli = llu_i2info(cookie->lsc_inode);
+        struct llu_inode_info *lli = llu_i2info(group->lig_inode);
+        struct intnl_stat *st = llu_i2stat(group->lig_inode);
         struct lov_stripe_md *lsm = lli->lli_smd;
-        struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
-        struct page *pages = cookie->lsc_pages;
-        struct ll_async_page *llap = cookie->lsc_llap;
-        int i, rc, npages = 0;
+        struct obd_export *exp = llu_i2obdexp(group->lig_inode);
+        cfs_page_t *pages = &group->lig_pages[group->lig_npages],*page = pages;
+        struct ll_async_page *llap = &group->lig_llaps[group->lig_npages];
+        void *llap_cookie = group->lig_llap_cookies +
+                llap_cookie_size * group->lig_npages;
+        int i, rc, npages = 0, ret_bytes = 0;
+        int local_lock;
         ENTRY;
 
         if (!exp)
                 RETURN(-EINVAL);
 
+        local_lock = group->lig_params->lrp_lock_mode != LCK_NL;
         /* prepare the pages array */
        do {
                 unsigned long index, offset, bytes;
 
-                offset = (pos & ~PAGE_CACHE_MASK);
-                index = pos >> PAGE_CACHE_SHIFT;
-                bytes = PAGE_CACHE_SIZE - offset;
+                offset = (pos & ~CFS_PAGE_MASK);
+                index = pos >> CFS_PAGE_SHIFT;
+                bytes = CFS_PAGE_SIZE - offset;
                 if (bytes > count)
                         bytes = count;
 
                 /* prevent read beyond file range */
-                if ((cmd == OBD_BRW_READ) &&
-                    (pos + bytes) >= lli->lli_st_size) {
-                        if (pos >= lli->lli_st_size)
+                if (/* local_lock && */
+                    cmd == OBD_BRW_READ && pos + bytes >= st->st_size) {
+                        if (pos >= st->st_size)
                                 break;
-                        bytes = lli->lli_st_size - pos;
+                        bytes = st->st_size - pos;
                 }
 
                 /* prepare page for this index */
-                pages[npages].index = index;
-                pages[npages].addr = buf - offset;
+                page->index = index;
+                page->addr = buf - offset;
 
-                pages[npages]._offset = offset;
-                pages[npages]._count = bytes;
+                page->_offset = offset;
+                page->_count = bytes;
 
+                page++;
                 npages++;
                 count -= bytes;
                 pos += bytes;
                 buf += bytes;
 
-                cookie->lsc_rwcount += bytes;
+                group->lig_rwcount += bytes;
+                ret_bytes += bytes;
         } while (count);
 
-        cookie->lsc_npages = npages;
+        group->lig_npages += npages;
 
-#ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
-        if (cmd == OBD_BRW_WRITE) {
-                rc = prepare_unaligned_write(cookie);
-                if (rc)
-                        RETURN(rc);
-        }
-#endif
-
-        for (i = 0; i < npages; i++) {
-                llap[i].llap_magic = LLAP_MAGIC;
-                rc = obd_prep_async_page(exp, lsm, NULL, &pages[i],
-                                         (obd_off)pages[i].index << PAGE_SHIFT,
+        for (i = 0, page = pages; i < npages;
+             i++, page++, llap++, llap_cookie += llap_cookie_size){
+                llap->llap_magic = LLAP_MAGIC;
+                llap->llap_cookie = llap_cookie;
+                rc = obd_prep_async_page(exp, lsm, NULL, page,
+                                         (obd_off)page->index << CFS_PAGE_SHIFT,
                                          &llu_async_page_ops,
-                                         &llap[i], &llap[i].llap_cookie);
+                                         llap, &llap->llap_cookie,
+                                         1 /* no cache in liblustre at all */,
+                                         NULL);
                 if (rc) {
-                        llap[i].llap_cookie = NULL;
+                        LASSERT(rc < 0);
+                        llap->llap_cookie = NULL;
                         RETURN(rc);
                 }
-                CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n",
-                       &llap[i], &pages[i], llap[i].llap_cookie,
-                       (obd_off)pages[i].index << PAGE_SHIFT);
-                pages[i].private = (unsigned long)&llap[i];
-                llap[i].llap_page = &pages[i];
-                llap[i].llap_inode = cookie->lsc_inode;
-
-                rc = obd_queue_group_io(exp, lsm, NULL, cookie->lsc_oig,
-                                        llap[i].llap_cookie, cmd,
-                                        pages[i]._offset, pages[i]._count, 0,
+
+                CDEBUG(D_CACHE, "llap %p page %p group %p obj off "LPU64"\n",
+                       llap, page, llap->llap_cookie,
+                       (obd_off)pages->index << CFS_PAGE_SHIFT);
+                page->private = (unsigned long)llap;
+                llap->llap_page = page;
+                llap->llap_inode = group->lig_inode;
+
+                rc = obd_queue_group_io(exp, lsm, NULL, group->lig_oig,
+                                        llap->llap_cookie, cmd,
+                                        page->_offset, page->_count,
+                                        group->lig_params->lrp_brw_flags,
                                         ASYNC_READY | ASYNC_URGENT |
                                         ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
-                if (rc)
+                if (!local_lock && cmd == OBD_BRW_READ) {
+                        /*
+                         * In OST-side locking case short reads cannot be
+                         * detected properly.
+                         *
+                         * The root of the problem is that
+                         *
+                         * kms = lov_merge_size(lsm, 1);
+                         * if (end >= kms)
+                         *         glimpse_size(inode);
+                         * else
+                         *         st->st_size = kms;
+                         *
+                         * logic in the read code (both llite and liblustre)
+                         * only works correctly when client holds DLM lock on
+                         * [start, end]. Without DLM lock KMS can be
+                         * completely out of date, and client can either make
+                         * spurious short-read (missing concurrent write), or
+                         * return stale data (missing concurrent
+                         * truncate). For llite client this is fatal, because
+                         * incorrect data are cached and can be later sent
+                         * back to the server (vide bug 5047). This is hard to
+                         * fix by handling short-reads on the server, as there
+                         * is no easy way to communicate file size (or amount
+                         * of bytes read/written) back to the client,
+                         * _especially_ because OSC pages can be sliced and
+                         * dices into multiple RPCs arbitrary. Fortunately,
+                         * liblustre doesn't cache data and the worst case is
+                         * that we get race with concurrent write or truncate.
+                         */
+                }
+                if (rc) {
+                        LASSERT(rc < 0);
                         RETURN(rc);
+                }
 
-                llap[i].llap_queued = 1;
+                llap->llap_queued = 1;
         }
 
-        RETURN(0);
+        RETURN(ret_bytes);
 }
 
 static
-int llu_start_async_io(struct llu_sysio_cookie *cookie)
+struct llu_io_group * get_io_group(struct inode *inode, int maxpages,
+                                   struct lustre_rw_params *params)
 {
-        struct lov_stripe_md *lsm = llu_i2info(cookie->lsc_inode)->lli_smd;
-        struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
+        struct llu_io_group *group;
+        int rc;
 
-        return obd_trigger_group_io(exp, lsm, NULL, cookie->lsc_oig);
-}
+        if (!llap_cookie_size)
+                llap_cookie_size = obd_prep_async_page(llu_i2obdexp(inode),
+                                                       NULL, NULL, NULL, 0,
+                                                       NULL, NULL, NULL, 0,
+                                                       NULL);
 
-/*
- * read/write a continuous buffer for an inode (zero-copy)
- */
-struct llu_sysio_cookie*
-llu_rw(int cmd, struct inode *inode, char *buf, size_t count, loff_t pos)
-{
-        struct llu_sysio_cookie *cookie;
-        int max_pages, rc;
-        ENTRY;
+        OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
+        if (!group)
+                return ERR_PTR(-ENOMEM);
 
-        max_pages = (count >> PAGE_SHIFT) + 2;
+        I_REF(inode);
+        group->lig_inode = inode;
+        group->lig_maxpages = maxpages;
+        group->lig_params = params;
+        group->lig_llaps = (struct ll_async_page *)(group + 1);
+        group->lig_pages = (cfs_page_t *)(&group->lig_llaps[maxpages]);
+        group->lig_llap_cookies = (void *)(&group->lig_pages[maxpages]);
+
+        rc = oig_init(&group->lig_oig);
+        if (rc) {
+                OBD_FREE(group, LLU_IO_GROUP_SIZE(maxpages));
+                return ERR_PTR(rc);
+        }
 
-        cookie = get_sysio_cookie(inode, max_pages);
-        if (!cookie)
-                RETURN(ERR_PTR(-ENOMEM));
+        return group;
+}
 
-        rc = llu_prep_async_io(cookie, cmd, buf, pos, count);
-        if (rc)
-                GOTO(out_cleanup, rc);
+static int max_io_pages(ssize_t len, int iovlen)
+{
+        return (((len + CFS_PAGE_SIZE -1) / CFS_PAGE_SIZE) + 2 + iovlen - 1);
+}
 
-        rc = llu_start_async_io(cookie);
-        if (rc)
-                GOTO(out_cleanup, rc);
+static
+void put_io_group(struct llu_io_group *group)
+{
+        struct lov_stripe_md *lsm = llu_i2info(group->lig_inode)->lli_smd;
+        struct obd_export *exp = llu_i2obdexp(group->lig_inode);
+        struct ll_async_page *llap = group->lig_llaps;
+        int i;
 
-/*
-        rc = oig_wait(&oig);
-        if (rc) {
-                CERROR("file i/o error!\n");
-                rw_count = rc;
+        for (i = 0; i < group->lig_npages; i++, llap++) {
+                if (llap->llap_cookie)
+                        obd_teardown_async_page(exp, lsm, NULL,
+                                                llap->llap_cookie);
         }
-*/
-        RETURN(cookie);
 
-out_cleanup:
-        put_sysio_cookie(cookie);
-        RETURN(ERR_PTR(rc));
-}
+        I_RELE(group->lig_inode);
 
-void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
-                      obd_off size);
+        oig_release(group->lig_oig);
+        OBD_FREE(group, LLU_IO_GROUP_SIZE(group->lig_maxpages));
+}
 
-struct llu_sysio_callback_args*
-llu_file_write(struct inode *inode, const struct iovec *iovec,
-               size_t iovlen, loff_t pos)
+static
+ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
+                        _SYSIO_OFF_T pos, ssize_t len,
+                        void *private)
 {
+        struct llu_io_session *session = (struct llu_io_session *) private;
+        struct inode *inode = session->lis_inode;
         struct llu_inode_info *lli = llu_i2info(inode);
+        struct intnl_stat *st = llu_i2stat(inode);
         struct ll_file_data *fd = lli->lli_file_data;
         struct lustre_handle lockh = {0};
         struct lov_stripe_md *lsm = lli->lli_smd;
         struct obd_export *exp = NULL;
-        ldlm_policy_data_t policy;
-        struct llu_sysio_callback_args *lsca;
-        struct llu_sysio_cookie *cookie;
-        int astflag = (lli->lli_open_flags & O_NONBLOCK) ?
-                       LDLM_FL_BLOCK_NOWAIT : 0;
-        ldlm_error_t err;
-        int iovidx;
+        struct llu_io_group *iogroup;
+        struct lustre_rw_params p;
+        struct ost_lvb lvb;
+        __u64 kms;
+        int err, is_read, iovidx, ret;
+        int local_lock;
+        ssize_t ret_len = len;
         ENTRY;
 
-        /* XXX consider other types later */
-        if (!S_ISREG(lli->lli_st_mode))
-                LBUG();
-
-        LASSERT(iovlen <= MAX_IOVEC);
+        /* in a large iov read/write we'll be repeatedly called.
+         * so give a chance to answer cancel ast here
+         */
+        liblustre_wait_event(0);
 
         exp = llu_i2obdexp(inode);
         if (exp == NULL)
-                RETURN(ERR_PTR(-EINVAL));
+                RETURN(-EINVAL);
+
+        if (len == 0 || iovlen == 0)
+                RETURN(0);
 
-        OBD_ALLOC(lsca, sizeof(*lsca));
-        if (!lsca)
-                RETURN(ERR_PTR(-ENOMEM));
+        if (pos + len > lli->lli_maxbytes)
+                RETURN(-ERANGE);
+
+        lustre_build_lock_params(session->lis_cmd, lli->lli_open_flags,
+                                 lli->lli_sbi->ll_lco.lco_flags,
+                                 pos, len, &p);
+
+        iogroup = get_io_group(inode, max_io_pages(len, iovlen), &p);
+        if (IS_ERR(iogroup))
+                RETURN(PTR_ERR(iogroup));
+
+        local_lock = p.lrp_lock_mode != LCK_NL;
+
+        err = llu_extent_lock(fd, inode, lsm, p.lrp_lock_mode, &p.lrp_policy,
+                              &lockh, p.lrp_ast_flags);
+        if (err != ELDLM_OK)
+                GOTO(err_put, err);
+
+        is_read = (session->lis_cmd == OBD_BRW_READ);
+        if (is_read) {
+                /*
+                 * If OST-side locking is used, KMS can be completely out of
+                 * date, and, hence, cannot be used for short-read
+                 * detection. Rely in OST to handle short reads in that case.
+                 */
+                inode_init_lvb(inode, &lvb);
+                obd_merge_lvb(exp, lsm, &lvb, 1);
+                kms = lvb.lvb_size;
+                /* extent.end is last byte of the range */
+                if (p.lrp_policy.l_extent.end >= kms) {
+                        /* A glimpse is necessary to determine whether
+                         * we return a short read or some zeroes at
+                         * the end of the buffer
+                         *
+                         * In the case of OST-side locking KMS can be
+                         * completely out of date and short-reads maybe
+                         * mishandled. See llu_queue_pio() for more detailed
+                         * comment.
+                         */
+                        if ((err = llu_glimpse_size(inode))) {
+                                GOTO(err_unlock, err);
+                        }
+                } else {
+                        st->st_size = kms;
+                }
+        } else if (lli->lli_open_flags & O_APPEND) {
+                pos = st->st_size;
+        }
 
-        /* FIXME optimize the following extent locking */
         for (iovidx = 0; iovidx < iovlen; iovidx++) {
-                char *buf = (char*)iovec[iovidx].iov_base;
+                char *buf = (char *) iovec[iovidx].iov_base;
                 size_t count = iovec[iovidx].iov_len;
 
-                if (count == 0)
+                if (!count)
                         continue;
+                if (len < count)
+                        count = len;
+                if (IS_BAD_PTR(buf) || IS_BAD_PTR(buf + count)) {
+                        GOTO(err_unlock, err = -EFAULT);
+                }
 
-                if (pos + count > lli->lli_maxbytes)
-                        GOTO(err_out, err = -ERANGE);
-
-                /* FIXME libsysio haven't handle O_APPEND?? */
-                policy.l_extent.start = pos;
-                policy.l_extent.end = pos + count - 1;
-
-                err = llu_extent_lock(fd, inode, lsm, LCK_PW, &policy,
-                                      &lockh, astflag);
-                if (err != ELDLM_OK)
-                        GOTO(err_out, err = -ENOLCK);
-
-                CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
-                       lli->lli_st_ino, count, pos);
-
-                cookie = llu_rw(OBD_BRW_WRITE, inode, buf, count, pos);
-                if (!IS_ERR(cookie)) {
-                        /* save cookie */
-                        lsca->cookies[lsca->ncookies++] = cookie;
-                        pos += count;
-                        lov_increase_kms(exp, lsm, pos);
-                        /* file size grow */
-                        if (pos > lli->lli_st_size)
-                                lli->lli_st_size = pos;
+                if (is_read) {
+                        if (/* local_lock && */ pos >= st->st_size)
+                                break;
                 } else {
-                        llu_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
-                        GOTO(err_out, err = PTR_ERR(cookie));
+                        if (pos >= lli->lli_maxbytes) {
+                                GOTO(err_unlock, err = -EFBIG);
+                        }
+                        if (pos + count >= lli->lli_maxbytes)
+                                count = lli->lli_maxbytes - pos;
                 }
 
-                /* XXX errors? */
-                err = llu_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
-                if (err)
-                        CERROR("extent unlock error %d\n", err);
+                ret = llu_queue_pio(session->lis_cmd, iogroup, buf, count, pos);
+                if (ret < 0) {
+                        GOTO(err_unlock, err = ret);
+                } else {
+                        pos += ret;
+                        if (!is_read) {
+                                LASSERT(ret == count);
+                                obd_adjust_kms(exp, lsm, pos, 0);
+                                /* file size grow immediately */
+                                if (pos > st->st_size)
+                                        st->st_size = pos;
+                        }
+                        len -= ret;
+                        if (!len)
+                                break;
+                }
         }
+        LASSERT(len == 0 || is_read); /* libsysio should guarantee this */
 
-        RETURN(lsca);
+        err = obd_trigger_group_io(exp, lsm, NULL, iogroup->lig_oig);
+        if (err)
+                GOTO(err_unlock, err);
 
-err_out:
-        /* teardown all async stuff */
-        while (lsca->ncookies--) {
-                put_sysio_cookie(lsca->cookies[lsca->ncookies]);
+        err = oig_wait(iogroup->lig_oig);
+        if (err) {
+                CERROR("%s error: %s\n", is_read ? "read" : "write", strerror(-err));
+                GOTO(err_unlock, err);
         }
-        OBD_FREE(lsca, sizeof(*lsca));
 
-        RETURN(ERR_PTR(err));
+        ret = llu_extent_unlock(fd, inode, lsm, p.lrp_lock_mode, &lockh);
+        if (ret)
+                CERROR("extent unlock error %d\n", ret);
+
+        session->lis_groups[session->lis_ngroups++] = iogroup;
+        RETURN(ret_len);
+
+err_unlock:
+        llu_extent_unlock(fd, inode, lsm, p.lrp_lock_mode, &lockh);
+err_put:
+        put_io_group(iogroup);
+        RETURN((ssize_t)err);
 }
 
-#if 0
-static void llu_update_atime(struct inode *inode)
+static
+struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
 {
-        struct llu_inode_info *lli = llu_i2info(inode);
+        struct llu_io_session *session;
 
-#ifdef USE_ATIME
-        struct iattr attr;
+        OBD_ALLOC(session, LLU_IO_SESSION_SIZE(ngroups));
+        if (!session)
+                return NULL;
 
-        attr.ia_atime = LTIME_S(CURRENT_TIME);
-        attr.ia_valid = ATTR_ATIME;
+        I_REF(ino);
+        session->lis_inode = ino;
+        session->lis_max_groups = ngroups;
+        session->lis_cmd = cmd;
+        return session;
+}
 
-        if (lli->lli_st_atime == attr.ia_atime) return;
-        if (IS_RDONLY(inode)) return;
-        if (IS_NOATIME(inode)) return;
+static void put_io_session(struct llu_io_session *session)
+{
+        int i;
 
-        /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
-        llu_inode_setattr(inode, &attr, 0);
-#else
-        /* update atime, but don't explicitly write it out just this change */
-        inode->i_atime = CURRENT_TIME;
-#endif
+        for (i = 0; i < session->lis_ngroups; i++) {
+                if (session->lis_groups[i]) {
+                        put_io_group(session->lis_groups[i]);
+                        session->lis_groups[i] = NULL;
+                }
+        }
+
+        I_RELE(session->lis_inode);
+        OBD_FREE(session, LLU_IO_SESSION_SIZE(session->lis_max_groups));
 }
-#endif
 
-struct llu_sysio_callback_args*
-llu_file_read(struct inode *inode, const struct iovec *iovec,
-              size_t iovlen, loff_t pos)
+static int llu_file_rwx(struct inode *ino,
+                        struct ioctx *ioctx,
+                        int read)
 {
-        struct llu_inode_info *lli = llu_i2info(inode);
-        struct ll_file_data *fd = lli->lli_file_data;
-        struct lov_stripe_md *lsm = lli->lli_smd;
-        struct lustre_handle lockh = { 0 };
-        ldlm_policy_data_t policy;
-        struct llu_sysio_callback_args *lsca;
-        struct llu_sysio_cookie *cookie;
-        int astflag = (lli->lli_open_flags & O_NONBLOCK) ?
-                       LDLM_FL_BLOCK_NOWAIT : 0;
-        __u64 kms;
-        int iovidx;
-
-        ldlm_error_t err;
+        struct llu_io_session *session;
+        ssize_t cc;
+        int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
         ENTRY;
 
-        OBD_ALLOC(lsca, sizeof(*lsca));
-        if (!lsca)
-                RETURN(ERR_PTR(-ENOMEM));
-
-        for (iovidx = 0; iovidx < iovlen; iovidx++) {
-                char *buf = iovec[iovidx].iov_base;
-                size_t count = iovec[iovidx].iov_len;
+        LASSERT(ioctx->ioctx_xtvlen >= 0);
+        LASSERT(ioctx->ioctx_iovlen >= 0);
 
-                /* "If nbyte is 0, read() will return 0 and have no other results."
-                 *                      -- Single Unix Spec */
-                if (count == 0)
-                        continue;
+        liblustre_wait_event(0);
 
-                policy.l_extent.start = pos;
-                policy.l_extent.end = pos + count - 1;
-
-                err = llu_extent_lock(fd, inode, lsm, LCK_PR, &policy,
-                                      &lockh, astflag);
-                if (err != ELDLM_OK)
-                        GOTO(err_out, err = -ENOLCK);
-
-                kms = lov_merge_size(lsm, 1);
-                if (policy.l_extent.end > kms) {
-                        /* A glimpse is necessary to determine whether we
-                         * return a short read or some zeroes at the end of
-                         * the buffer */
-                        struct ost_lvb lvb;
-                        if (llu_glimpse_size(inode, &lvb)) {
-                                llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
-                                GOTO(err_out, err = -ENOLCK);
-                        }
-                        lli->lli_st_size = lvb.lvb_size;
-                } else {
-                        lli->lli_st_size = kms;
-                }
+        if (!ioctx->ioctx_xtvlen)
+                RETURN(0);
 
-                CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld, "
-                       "i_size "LPU64"\n", lli->lli_st_ino, count, pos,
-                       lli->lli_st_size);
+        /* XXX consider other types later */
+        if (S_ISDIR(llu_i2stat(ino)->st_mode))
+                RETURN(-EISDIR);
+        if (!S_ISREG(llu_i2stat(ino)->st_mode))
+                RETURN(-EOPNOTSUPP);
+
+        session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
+        if (!session)
+                RETURN(-ENOMEM);
+
+        cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
+                                      ioctx->ioctx_iov, ioctx->ioctx_iovlen,
+                                      llu_file_prwv, session);
+
+        if (cc >= 0) {
+                LASSERT(!ioctx->ioctx_cc);
+                ioctx->ioctx_private = session;
+                cc = 0;
+        } else {
+                put_io_session(session);
+        }
 
-                if (pos >= lli->lli_st_size) {
-                        llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
-                        break;
-                }
+        liblustre_wait_event(0);
+        RETURN(cc);
+}
 
-                cookie = llu_rw(OBD_BRW_READ, inode, buf, count, pos);
-                if (!IS_ERR(cookie)) {
-                        /* save cookie */
-                        lsca->cookies[lsca->ncookies++] = cookie;
-                        pos += count;
-                } else {
-                        llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
-                        GOTO(err_out, err = PTR_ERR(cookie));
-                }
+int llu_iop_read(struct inode *ino,
+                 struct ioctx *ioctx)
+{
+        /* BUG: 5972 */
+        struct intnl_stat *st = llu_i2stat(ino);
+        st->st_atime = CURRENT_TIME;
 
-                /* XXX errors? */
-                err = llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
-                if (err)
-                        CERROR("extent_unlock fail: %d\n", err);
-        }
-#if 0
-        if (readed > 0)
-                llu_update_atime(inode);
-#endif
-        RETURN(lsca);
+        return llu_file_rwx(ino, ioctx, 1);
+}
 
-err_out:
-        /* teardown all async stuff */
-        while (lsca->ncookies--) {
-                put_sysio_cookie(lsca->cookies[lsca->ncookies]);
-        }
-        OBD_FREE(lsca, sizeof(*lsca));
+int llu_iop_write(struct inode *ino,
+                  struct ioctx *ioctx)
+{
+        struct intnl_stat *st = llu_i2stat(ino);
+        st->st_mtime = st->st_ctime = CURRENT_TIME;
 
-        RETURN(ERR_PTR(err));
+        return llu_file_rwx(ino, ioctx, 0);
 }
 
-int llu_iop_iodone(struct ioctx *ioctxp)
+int llu_iop_iodone(struct ioctx *ioctx)
 {
-        struct llu_sysio_callback_args *lsca = ioctxp->ioctx_private;
-        struct llu_sysio_cookie *cookie;
+        struct llu_io_session *session;
+        struct llu_io_group *group;
         int i, err = 0, rc = 0;
         ENTRY;
 
-        /* write/read(fd, buf, 0) */
-        if (!lsca) {
-                ioctxp->ioctx_cc = 0;
-                RETURN(1);
-        }
+        liblustre_wait_event(0);
 
-        LASSERT(!IS_ERR(lsca));
+        session = (struct llu_io_session *) ioctx->ioctx_private;
+        LASSERT(session);
+        LASSERT(!IS_ERR(session));
 
-        for (i = 0; i < lsca->ncookies; i++) {
-                cookie = lsca->cookies[i];
-                if (cookie) {
-                        err = oig_wait(cookie->lsc_oig);
-                        if (err && !rc)
-                                rc = err;
+        for (i = 0; i < session->lis_ngroups; i++) {
+                group = session->lis_groups[i];
+                if (group) {
+                        if (!rc) {
+                                err = oig_wait(group->lig_oig);
+                                if (err)
+                                        rc = err;
+                        }
                         if (!rc)
-                                ioctxp->ioctx_cc += cookie->lsc_rwcount;
-                        put_sysio_cookie(cookie);
+                                ioctx->ioctx_cc += group->lig_rwcount;
+                        put_io_group(group);
+                        session->lis_groups[i] = NULL;
                 }
         }
 
         if (rc) {
                 LASSERT(rc < 0);
-                ioctxp->ioctx_cc = -1;
-                ioctxp->ioctx_errno = -rc;
+                ioctx->ioctx_cc = -1;
+                ioctx->ioctx_errno = -rc;
         }
 
-        OBD_FREE(lsca, sizeof(*lsca));
-        ioctxp->ioctx_private = NULL;
+        put_io_session(session);
+        ioctx->ioctx_private = NULL;
+        liblustre_wait_event(0);
 
         RETURN(1);
 }