Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / ost / ost_handler.c
index b1fe341..abb4f66 100644 (file)
@@ -1,33 +1,42 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
- *   Author: Peter J. Braam <braam@clusterfs.com>
- *   Author: Phil Schwan <phil@clusterfs.com>
+ * GPL HEADER START
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   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.
+ * 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.
  *
- *   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.
+ * 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).
  *
- *   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.
+ * 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
  *
- *  Storage Target Handling functions
- *  Lustre Object Server Module (OST)
+ * 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.
  *
- *  This server is single threaded at present (but can easily be multi
- *  threaded). For testing and management it is treated as an
- *  obd_device, although it does not export a full OBD method table
- *  (the requests are coming in over the wire, so object target
- *  modules do not have a full method table.)
+ * 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/ost/ost_handler.c
+ *
+ * Author: Peter J. Braam <braam@clusterfs.com>
+ * Author: Phil Schwan <phil@clusterfs.com>
  */
 
 #ifndef EXPORT_SYMTAB
 #define DEBUG_SUBSYSTEM S_OST
 
 #include <linux/module.h>
-#include <linux/obd_ost.h>
-#include <linux/lustre_net.h>
-#include <linux/lustre_dlm.h>
-#include <linux/lustre_export.h>
+#include <obd_cksum.h>
+#include <obd_ost.h>
+#include <lustre_net.h>
+#include <lustre_dlm.h>
+#include <lustre_export.h>
+#include <lustre_debug.h>
 #include <linux/init.h>
-#include <linux/lprocfs_status.h>
-#include <linux/lustre_commit_confd.h>
+#include <lprocfs_status.h>
 #include <libcfs/list.h>
-#include <linux/lustre_sec.h>
-#include <linux/lustre_audit.h>
+#include <lustre_quota.h>
+#include "ost_internal.h"
 
-void oti_init(struct obd_trans_info *oti, struct ptlrpc_request *req)
-{
-        if (oti == NULL)
-                return;
-        memset(oti, 0, sizeof *oti);
+static int oss_num_threads;
+CFS_MODULE_PARM(oss_num_threads, "i", int, 0444,
+                "number of OSS service threads to start");
 
-        if (req->rq_repmsg && req->rq_reqmsg != 0)
-                oti->oti_transno = req->rq_repmsg->transno;
-}
+static int ost_num_threads;
+CFS_MODULE_PARM(ost_num_threads, "i", int, 0444,
+                "number of OST service threads to start (deprecated)");
+
+static int oss_num_create_threads;
+CFS_MODULE_PARM(oss_num_create_threads, "i", int, 0444,
+                "number of OSS create threads to start");
 
 void oti_to_request(struct obd_trans_info *oti, struct ptlrpc_request *req)
 {
@@ -66,73 +78,102 @@ void oti_to_request(struct obd_trans_info *oti, struct ptlrpc_request *req)
                 return;
 
         if (req->rq_repmsg)
-                req->rq_repmsg->transno = oti->oti_transno;
+                lustre_msg_set_transno(req->rq_repmsg, oti->oti_transno);
+        req->rq_transno = oti->oti_transno;
 
         /* XXX 4 == entries in oti_ack_locks??? */
         for (ack_lock = oti->oti_ack_locks, i = 0; i < 4; i++, ack_lock++) {
                 if (!ack_lock->mode)
                         break;
                 /* XXX not even calling target_send_reply in some cases... */
-                ptlrpc_save_lock (req, &ack_lock->lock, ack_lock->mode);
+                ptlrpc_save_lock (req, &ack_lock->lock, ack_lock->mode, 0);
         }
 }
 
-static int ost_destroy(struct obd_export *exp, struct ptlrpc_request *req, 
+static int ost_destroy(struct obd_export *exp, struct ptlrpc_request *req,
                        struct obd_trans_info *oti)
 {
         struct ost_body *body, *repbody;
-        int rc, size = sizeof(*body);
+        __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
+        int rc;
         ENTRY;
 
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
+        body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
+                                  lustre_swab_ost_body);
         if (body == NULL)
                 RETURN(-EFAULT);
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        if (body->oa.o_id == 0)
+                RETURN(-EPROTO);
+
+        if (lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1)) {
+                struct ldlm_request *dlm;
+                dlm = lustre_swab_reqbuf(req, REQ_REC_OFF + 1, sizeof(*dlm),
+                                         lustre_swab_ldlm_request);
+                if (dlm == NULL)
+                        RETURN (-EFAULT);
+                ldlm_request_cancel(req, dlm, 0);
+        }
+
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 RETURN(rc);
 
         if (body->oa.o_valid & OBD_MD_FLCOOKIE)
-                oti->oti_logcookies = obdo_logcookie(&body->oa);
-        repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
+                oti->oti_logcookies = &body->oa.o_lcookie;
+        repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                 sizeof(*repbody));
         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
-        req->rq_status = obd_destroy(exp, &body->oa, NULL, oti);
+        req->rq_status = obd_destroy(exp, &body->oa, NULL, oti, NULL);
         RETURN(0);
 }
 
 static int ost_getattr(struct obd_export *exp, struct ptlrpc_request *req)
 {
         struct ost_body *body, *repbody;
-        int rc, size = sizeof(*body);
+        struct obd_info oinfo = { { { 0 } } };
+        __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
+        int rc;
         ENTRY;
 
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
+        body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
+                                  lustre_swab_ost_body);
         if (body == NULL)
                 RETURN(-EFAULT);
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 RETURN(rc);
 
-        repbody = lustre_msg_buf (req->rq_repmsg, 0, sizeof(*repbody));
-        memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
-        req->rq_status = obd_getattr(exp, &repbody->oa, NULL);
+        repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                 sizeof(*repbody));
+        repbody->oa = body->oa;
+
+        oinfo.oi_oa = &repbody->oa;
+        if (oinfo.oi_oa->o_valid & OBD_MD_FLOSSCAPA)
+                oinfo.oi_capa = lustre_unpack_capa(req->rq_reqmsg,
+                                                   REQ_REC_OFF + 1);
+        req->rq_status = obd_getattr(exp, &oinfo);
         RETURN(0);
 }
 
 static int ost_statfs(struct ptlrpc_request *req)
 {
         struct obd_statfs *osfs;
-        int rc, size = sizeof(*osfs);
+        __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*osfs) };
+        int rc;
         ENTRY;
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 RETURN(rc);
 
-        osfs = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*osfs));
+        osfs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*osfs));
 
-        req->rq_status = obd_statfs(req->rq_export->exp_obd, osfs, jiffies-HZ);
+        req->rq_status = obd_statfs(req->rq_export->exp_obd, osfs,
+                                    cfs_time_current_64() - HZ, 0);
+        if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
+                osfs->os_bfree = osfs->os_bavail = 64;
         if (req->rq_status != 0)
                 CERROR("ost: statfs failed: rc %d\n", req->rq_status);
 
@@ -143,91 +184,200 @@ static int ost_create(struct obd_export *exp, struct ptlrpc_request *req,
                       struct obd_trans_info *oti)
 {
         struct ost_body *body, *repbody;
-        int rc, size = sizeof(*repbody);
+        __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
+        int rc;
         ENTRY;
 
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
+        body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
+                                  lustre_swab_ost_body);
         if (body == NULL)
                 RETURN(-EFAULT);
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 RETURN(rc);
 
-        repbody = lustre_msg_buf (req->rq_repmsg, 0, sizeof(*repbody));
+        repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                 sizeof(*repbody));
         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
-        oti->oti_logcookies = obdo_logcookie(&repbody->oa);
-        req->rq_status = obd_create(exp, &repbody->oa, NULL, 0, NULL, oti);
+        oti->oti_logcookies = &repbody->oa.o_lcookie;
+        req->rq_status = obd_create(exp, &repbody->oa, NULL, oti);
         //obd_log_cancel(conn, NULL, 1, oti->oti_logcookies, 0);
         RETURN(0);
 }
 
-static int ost_punch(struct obd_export *exp, struct ptlrpc_request *req, 
+/*
+ * Helper function for ost_punch(): if asked by client, acquire [size, EOF]
+ * lock on the file being truncated.
+ */
+static int ost_punch_lock_get(struct obd_export *exp, struct obdo *oa,
+                              struct lustre_handle *lh)
+{
+        int flags;
+        struct ldlm_res_id res_id;
+        ldlm_policy_data_t policy;
+        __u64 start;
+        __u64 finis;
+
+        ENTRY;
+
+        osc_build_res_name(oa->o_id, oa->o_gr, &res_id);
+        LASSERT(!lustre_handle_is_used(lh));
+
+        if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
+            !(oa->o_flags & OBD_FL_TRUNCLOCK))
+                RETURN(0);
+
+        CDEBUG(D_INODE, "OST-side truncate lock.\n");
+
+        start = oa->o_size;
+        finis = start + oa->o_blocks;
+
+        /*
+         * standard truncate optimization: if file body is completely
+         * destroyed, don't send data back to the server.
+         */
+        flags = (start == 0) ? LDLM_AST_DISCARD_DATA : 0;
+
+        policy.l_extent.start = start & CFS_PAGE_MASK;
+
+        /*
+         * If ->o_blocks is EOF it means "lock till the end of the
+         * file". Otherwise, it's size of a hole being punched (in bytes)
+         */
+        if (oa->o_blocks == OBD_OBJECT_EOF || finis < start)
+                policy.l_extent.end = OBD_OBJECT_EOF;
+        else
+                policy.l_extent.end = finis | ~CFS_PAGE_MASK;
+
+        RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, &res_id,
+                                      LDLM_EXTENT, &policy, LCK_PW, &flags,
+                                      ldlm_blocking_ast, ldlm_completion_ast,
+                                      ldlm_glimpse_ast, NULL, 0, NULL,
+                                      NULL, lh));
+}
+
+/*
+ * Helper function for ost_punch(): release lock acquired by
+ * ost_punch_lock_get(), if any.
+ */
+static void ost_punch_lock_put(struct obd_export *exp, struct obdo *oa,
+                               struct lustre_handle *lh)
+{
+        ENTRY;
+        if (lustre_handle_is_used(lh))
+                ldlm_lock_decref(lh, LCK_PW);
+        EXIT;
+}
+
+static int ost_punch(struct obd_export *exp, struct ptlrpc_request *req,
                      struct obd_trans_info *oti)
 {
+        struct obd_info oinfo = { { { 0 } } };
         struct ost_body *body, *repbody;
-        int rc, size = sizeof(*repbody);
+        __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
+        int rc;
+        struct lustre_handle lh = {0,};
         ENTRY;
 
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
+        /* check that we do support OBD_CONNECT_TRUNCLOCK. */
+        CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
+
+        body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
+                                  lustre_swab_ost_body);
         if (body == NULL)
                 RETURN(-EFAULT);
 
-        if ((body->oa.o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
+        oinfo.oi_oa = &body->oa;
+        oinfo.oi_policy.l_extent.start = oinfo.oi_oa->o_size;
+        oinfo.oi_policy.l_extent.end = oinfo.oi_oa->o_blocks;
+
+        if ((oinfo.oi_oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
-                RETURN(-EINVAL);
+                RETURN(-EPROTO);
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 RETURN(rc);
 
-        repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
-        memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
-        req->rq_status = obd_punch(exp, &repbody->oa, NULL, repbody->oa.o_size,
-                                   repbody->oa.o_blocks, oti);
-        RETURN(0);
+        repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                 sizeof(*repbody));
+        rc = ost_punch_lock_get(exp, oinfo.oi_oa, &lh);
+        if (rc == 0) {
+                if (oinfo.oi_oa->o_valid & OBD_MD_FLFLAGS &&
+                    oinfo.oi_oa->o_flags == OBD_FL_TRUNCLOCK)
+                        /*
+                         * If OBD_FL_TRUNCLOCK is the only bit set in
+                         * ->o_flags, clear OBD_MD_FLFLAGS to avoid falling
+                         * through filter_setattr() to filter_iocontrol().
+                         */
+                        oinfo.oi_oa->o_valid &= ~OBD_MD_FLFLAGS;
+
+                if (oinfo.oi_oa->o_valid & OBD_MD_FLOSSCAPA)
+                        oinfo.oi_capa = lustre_unpack_capa(req->rq_reqmsg,
+                                                           REQ_REC_OFF + 1);
+                req->rq_status = obd_punch(exp, &oinfo, oti, NULL);
+                ost_punch_lock_put(exp, oinfo.oi_oa, &lh);
+        }
+        repbody->oa = *oinfo.oi_oa;
+        RETURN(rc);
 }
 
 static int ost_sync(struct obd_export *exp, struct ptlrpc_request *req)
 {
         struct ost_body *body, *repbody;
-        int rc, size = sizeof(*repbody);
+        struct lustre_capa *capa = NULL;
+        __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
+        int rc;
         ENTRY;
 
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
+        body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
+                                  lustre_swab_ost_body);
         if (body == NULL)
                 RETURN(-EFAULT);
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
+                capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 1);
+
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 RETURN(rc);
 
-        repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
+        repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                 sizeof(*repbody));
         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
         req->rq_status = obd_sync(exp, &repbody->oa, NULL, repbody->oa.o_size,
-                                  repbody->oa.o_blocks);
+                                  repbody->oa.o_blocks, capa);
         RETURN(0);
 }
 
-static int ost_setattr(struct obd_export *exp, struct ptlrpc_request *req, 
+static int ost_setattr(struct obd_export *exp, struct ptlrpc_request *req,
                        struct obd_trans_info *oti)
 {
         struct ost_body *body, *repbody;
-        int rc, size = sizeof(*repbody);
+        __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
+        int rc;
+        struct obd_info oinfo = { { { 0 } } };
         ENTRY;
 
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
+        body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
+                                  lustre_swab_ost_body);
         if (body == NULL)
                 RETURN(-EFAULT);
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 RETURN(rc);
 
-        repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
-        memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
+        repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                 sizeof(*repbody));
+        repbody->oa = body->oa;
 
-        req->rq_status = obd_setattr(exp, &repbody->oa, NULL, oti);
+        oinfo.oi_oa = &repbody->oa;
+        if (oinfo.oi_oa->o_valid & OBD_MD_FLOSSCAPA)
+                oinfo.oi_capa = lustre_unpack_capa(req->rq_reqmsg,
+                                                   REQ_REC_OFF + 1);
+        req->rq_status = obd_setattr(exp, &oinfo, oti);
         RETURN(0);
 }
 
@@ -240,187 +390,238 @@ static int ost_bulk_timeout(void *data)
         RETURN(1);
 }
 
-static int get_per_page_niobufs(struct obd_ioobj *ioo, int nioo,
-                                struct niobuf_remote *rnb, int nrnb,
-                                struct niobuf_remote **pp_rnbp)
+static __u32 ost_checksum_bulk(struct ptlrpc_bulk_desc *desc, int opc,
+                               cksum_type_t cksum_type)
 {
-        /* Copy a remote niobuf, splitting it into page-sized chunks
-         * and setting ioo[i].ioo_bufcnt accordingly */
-        struct niobuf_remote *pp_rnb;
-        int   i;
-        int   j;
-        int   page;
-        int   rnbidx = 0;
-        int   npages = 0;
-
-        /* first count and check the number of pages required */
-        for (i = 0; i < nioo; i++)
-                for (j = 0; j < ioo->ioo_bufcnt; j++, rnbidx++) {
-                        obd_off offset = rnb[rnbidx].offset;
-                        obd_off p0 = offset >> PAGE_SHIFT;
-                        obd_off pn = (offset + rnb[rnbidx].len - 1)>>PAGE_SHIFT;
-
-                        LASSERT(rnbidx < nrnb);
-
-                        npages += (pn + 1 - p0);
-
-                        if (rnb[rnbidx].len == 0) {
-                                CERROR("zero len BRW: obj %d objid "LPX64
-                                       " buf %u\n", i, ioo[i].ioo_id, j);
-                                return -EINVAL;
-                        }
-                        if (j > 0 &&
-                            rnb[rnbidx].offset <= rnb[rnbidx-1].offset) {
-                                CERROR("unordered BRW: obj %d objid "LPX64
-                                       " buf %u offset "LPX64" <= "LPX64"\n",
-                                       i, ioo[i].ioo_id, j, rnb[rnbidx].offset,
-                                       rnb[rnbidx].offset);
-                                return -EINVAL;
-                        }
+        __u32 cksum;
+        int i;
+
+        cksum = init_checksum(cksum_type);
+        for (i = 0; i < desc->bd_iov_count; i++) {
+                struct page *page = desc->bd_iov[i].kiov_page;
+                int off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
+                char *ptr = kmap(page) + off;
+                int len = desc->bd_iov[i].kiov_len;
+
+                /* corrupt the data before we compute the checksum, to
+                 * simulate a client->OST data error */
+                if (i == 0 && opc == OST_WRITE &&
+                    OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_RECEIVE))
+                        memcpy(ptr, "bad3", min(4, len));
+                cksum = compute_checksum(cksum, ptr, len, cksum_type);
+                /* corrupt the data after we compute the checksum, to
+                 * simulate an OST->client data error */
+                if (i == 0 && opc == OST_READ &&
+                    OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_SEND)) {
+                        memcpy(ptr, "bad4", min(4, len));
+                        /* nobody should use corrupted page again */
+                        ClearPageUptodate(page);
                 }
+                kunmap(page);
+        }
 
-        LASSERT(rnbidx == nrnb);
+        return cksum;
+}
 
-        if (npages == nrnb) {       /* all niobufs are for single pages */
-                *pp_rnbp = rnb;
-                return npages;
-        }
+static int ost_brw_lock_get(int mode, struct obd_export *exp,
+                            struct obd_ioobj *obj, struct niobuf_remote *nb,
+                            struct lustre_handle *lh)
+{
+        int flags                 = 0;
+        int nrbufs                = obj->ioo_bufcnt;
+        struct ldlm_res_id res_id;
+        ldlm_policy_data_t policy;
+        int i;
+        ENTRY;
 
-        OBD_ALLOC(pp_rnb, sizeof(*pp_rnb) * npages);
-        if (pp_rnb == NULL)
-                return -ENOMEM;
+        osc_build_res_name(obj->ioo_id, obj->ioo_gr, &res_id);
+        LASSERT(mode == LCK_PR || mode == LCK_PW);
+        LASSERT(!lustre_handle_is_used(lh));
 
-        /* now do the actual split */
-        page = rnbidx = 0;
-        for (i = 0; i < nioo; i++) {
-                int  obj_pages = 0;
+        if (nrbufs == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
+                RETURN(0);
 
-                for (j = 0; j < ioo[i].ioo_bufcnt; j++, rnbidx++) {
-                        obd_off off = rnb[rnbidx].offset;
-                        int     nob = rnb[rnbidx].len;
+        /* EXPENSIVE ASSERTION */
+        for (i = 1; i < nrbufs; i ++)
+                LASSERT((nb[0].flags & OBD_BRW_SRVLOCK) ==
+                        (nb[i].flags & OBD_BRW_SRVLOCK));
 
-                        LASSERT(rnbidx < nrnb);
-                        do {
-                                obd_off  poff = off & (PAGE_SIZE - 1);
-                                int      pnob = (poff + nob > PAGE_SIZE) ?
-                                                PAGE_SIZE - poff : nob;
-
-                                LASSERT(page < npages);
-                                pp_rnb[page].len = pnob;
-                                pp_rnb[page].offset = off;
-                                pp_rnb[page].flags = rnb[rnbidx].flags;
-
-                                CDEBUG(0, "   obj %d id "LPX64
-                                       "page %d(%d) "LPX64" for %d, flg %x\n",
-                                       i, ioo[i].ioo_id, obj_pages, page,
-                                       pp_rnb[page].offset, pp_rnb[page].len,
-                                       pp_rnb[page].flags);
-                                page++;
-                                obj_pages++;
-
-                                off += pnob;
-                                nob -= pnob;
-                        } while (nob > 0);
-                        LASSERT(nob == 0);
-                }
-                ioo[i].ioo_bufcnt = obj_pages;
-        }
-        LASSERT(page == npages);
+        policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
+        policy.l_extent.end   = (nb[nrbufs - 1].offset +
+                                 nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
 
-        *pp_rnbp = pp_rnb;
-        return npages;
+        RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, &res_id,
+                                      LDLM_EXTENT, &policy, mode, &flags,
+                                      ldlm_blocking_ast, ldlm_completion_ast,
+                                      ldlm_glimpse_ast, NULL, 0, NULL,
+                                      NULL, lh));
 }
 
-static void free_per_page_niobufs (int npages, struct niobuf_remote *pp_rnb,
-                                   struct niobuf_remote *rnb)
+static void ost_brw_lock_put(int mode,
+                             struct obd_ioobj *obj, struct niobuf_remote *niob,
+                             struct lustre_handle *lh)
 {
-        if (pp_rnb == rnb)                      /* didn't allocate above */
-                return;
-
-        OBD_FREE(pp_rnb, sizeof(*pp_rnb) * npages);
+        ENTRY;
+        LASSERT(mode == LCK_PR || mode == LCK_PW);
+        LASSERT((obj->ioo_bufcnt > 0 && (niob[0].flags & OBD_BRW_SRVLOCK)) ==
+                lustre_handle_is_used(lh));
+        if (lustre_handle_is_used(lh))
+                ldlm_lock_decref(lh, mode);
+        EXIT;
 }
 
-#if CHECKSUM_BULK
-obd_count ost_checksum_bulk(struct ptlrpc_bulk_desc *desc)
+struct ost_prolong_data {
+        struct obd_export *opd_exp;
+        ldlm_policy_data_t opd_policy;
+        ldlm_mode_t opd_mode;
+};
+
+static int ost_prolong_locks_iter(struct ldlm_lock *lock, void *data)
 {
-        obd_count cksum = 0;
-        struct ptlrpc_bulk_page *bp;
+        struct ost_prolong_data *opd = data;
+
+        LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
 
-        list_for_each_entry(bp, &desc->bd_page_list, bp_link) {
-                ost_checksum(&cksum, kmap(bp->bp_page) + bp->bp_pageoffset,
-                             bp->bp_buflen);
-                kunmap(bp->bp_page);
+        if (lock->l_req_mode != lock->l_granted_mode) {
+                /* scan granted locks only */
+                return LDLM_ITER_STOP;
         }
 
-        return cksum;
+        if (lock->l_export != opd->opd_exp) {
+                /* prolong locks only for given client */
+                return LDLM_ITER_CONTINUE;
+        }
+
+        if (!(lock->l_granted_mode & opd->opd_mode)) {
+                /* we aren't interesting in all type of locks */
+                return LDLM_ITER_CONTINUE;
+        }
+
+        if (lock->l_policy_data.l_extent.end < opd->opd_policy.l_extent.start ||
+            lock->l_policy_data.l_extent.start > opd->opd_policy.l_extent.end) {
+                /* the request doesn't cross the lock, skip it */
+                return LDLM_ITER_CONTINUE;
+        }
+
+        if (!(lock->l_flags & LDLM_FL_AST_SENT)) {
+                /* ignore locks not being cancelled */
+                return LDLM_ITER_CONTINUE;
+        }
+
+        /* OK. this is a possible lock the user holds doing I/O
+         * let's refresh eviction timer for it */
+        ldlm_refresh_waiting_lock(lock);
+
+        return LDLM_ITER_CONTINUE;
 }
-#endif
 
-static void ost_stime_record(struct ptlrpc_request *req, struct timeval *start,
-                             unsigned rw, unsigned phase)
+static void ost_prolong_locks(struct obd_export *exp, struct obd_ioobj *obj,
+                              struct niobuf_remote *nb, struct obdo *oa,
+                              ldlm_mode_t mode)
 {
-        struct obd_device *obd = req->rq_svc->srv_obddev;
-        struct timeval stop;
-        int ind = rw *3 + phase;
-         
-        if (obd && obd->obd_type && obd->obd_type->typ_name) {
-                if (!strcmp(obd->obd_type->typ_name, OBD_OST_DEVICENAME)) {
-                        struct ost_obd *ost = NULL;
-                        
-                        ost = &obd->u.ost;
-                        if (ind >= (sizeof(ost->ost_stimes) / 
-                                    sizeof(ost->ost_stimes[0])))
-                               return;
-                        do_gettimeofday(&stop);
-
-                        spin_lock(&ost->ost_lock);
-                        lprocfs_stime_record(&ost->ost_stimes[ind],&stop,start);
-                        spin_unlock(&ost->ost_lock);
-                        memcpy(start, &stop, sizeof(*start));
+        struct ldlm_res_id res_id;
+        int nrbufs = obj->ioo_bufcnt;
+        struct ost_prolong_data opd;
+        ENTRY;
+
+        osc_build_res_name(obj->ioo_id, obj->ioo_gr, &res_id);
+
+        opd.opd_mode = mode;
+        opd.opd_exp = exp;
+        opd.opd_policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
+        opd.opd_policy.l_extent.end = (nb[nrbufs - 1].offset +
+                                       nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
+
+        CDEBUG(D_DLMTRACE,"refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
+               res_id.name[0], res_id.name[1], opd.opd_policy.l_extent.start,
+               opd.opd_policy.l_extent.end);
+
+        if (oa->o_valid & OBD_MD_FLHANDLE) {
+                struct ldlm_lock *lock;
+
+                lock = ldlm_handle2lock(&oa->o_handle);
+                if (lock != NULL) {
+                        ost_prolong_locks_iter(lock, &opd);
+                        LDLM_LOCK_PUT(lock);
+                        EXIT;
+                        return;
                 }
-       } 
+        }
+
+        ldlm_resource_iterate(exp->exp_obd->obd_namespace, &res_id,
+                              ost_prolong_locks_iter, &opd);
+
+        EXIT;
 }
 
-static int ost_brw_read(struct ptlrpc_request *req)
+static int ost_brw_read(struct ptlrpc_request *req, struct obd_trans_info *oti)
 {
-        struct ptlrpc_bulk_desc *desc;
-        struct niobuf_remote    *remote_nb;
-        struct niobuf_remote    *pp_rnb;
-        struct niobuf_local     *local_nb;
-        struct obd_ioobj        *ioo;
-        struct ost_body         *body, *repbody;
-        struct l_wait_info       lwi;
-        struct obd_trans_info    oti = { 0 };
-        int                      size[1] = { sizeof(*body) };
-        int                      comms_error = 0;
-        int                      niocount;
-        int                      npages;
-        int                      nob = 0;
-        int                      rc;
-        int                      i;
-        struct timeval           start;
+        struct ptlrpc_bulk_desc *desc = NULL;
+        struct obd_export *exp = req->rq_export;
+        struct niobuf_remote *remote_nb;
+        struct niobuf_local *local_nb;
+        struct obd_ioobj *ioo;
+        struct ost_body *body, *repbody;
+        struct lustre_capa *capa = NULL;
+        struct l_wait_info lwi;
+        struct lustre_handle lockh = { 0 };
+        __u32  size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
+        int objcount, niocount, npages, nob = 0, rc, i;
+        int no_reply = 0;
         ENTRY;
 
+        req->rq_bulk_read = 1;
+
         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
                 GOTO(out, rc = -EIO);
 
-        OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
-                         (obd_timeout + 1) / 4);
+        OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
+
+        /* Check if there is eviction in progress, and if so, wait for it to
+         * finish */
+        if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
+                lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
+                rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
+                        !atomic_read(&exp->exp_obd->obd_evict_inprogress),
+                        &lwi);
+        }
+        if (exp->exp_failed)
+                GOTO(out, rc = -ENOTCONN);
 
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
+        body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
+                                  lustre_swab_ost_body);
         if (body == NULL) {
                 CERROR("Missing/short ost_body\n");
                 GOTO(out, rc = -EFAULT);
         }
 
-        ioo = lustre_swab_reqbuf(req, 1, sizeof(*ioo), lustre_swab_obd_ioobj);
+        objcount = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1) /
+                   sizeof(*ioo);
+        if (objcount == 0) {
+                CERROR("Missing/short ioobj\n");
+                GOTO(out, rc = -EFAULT);
+        }
+        if (objcount > 1) {
+                CERROR("too many ioobjs (%d)\n", objcount);
+                GOTO(out, rc = -EFAULT);
+        }
+
+        ioo = lustre_swab_reqbuf(req, REQ_REC_OFF + 1, sizeof(*ioo),
+                                 lustre_swab_obd_ioobj);
         if (ioo == NULL) {
                 CERROR("Missing/short ioobj\n");
                 GOTO(out, rc = -EFAULT);
         }
 
         niocount = ioo->ioo_bufcnt;
-        remote_nb = lustre_swab_reqbuf(req, 2, niocount * sizeof(*remote_nb),
+        if (niocount > PTLRPC_MAX_BRW_PAGES) {
+                DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)",
+                          niocount);
+                GOTO(out, rc = -EFAULT);
+        }
+
+        remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
+                                       niocount * sizeof(*remote_nb),
                                        lustre_swab_niobuf_remote);
         if (remote_nb == NULL) {
                 CERROR("Missing/short niobuf\n");
@@ -431,34 +632,50 @@ static int ost_brw_read(struct ptlrpc_request *req)
                         lustre_swab_niobuf_remote (&remote_nb[i]);
         }
 
-        rc = lustre_pack_reply(req, 1, size, NULL);
+        if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
+                capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 3);
+
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 GOTO(out, rc);
 
-        /* FIXME all niobuf splitting should be done in obdfilter if needed */
-        /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
-        npages = get_per_page_niobufs(ioo, 1, remote_nb, niocount, &pp_rnb);
-        if (npages < 0)
-                GOTO(out, rc = npages);
+        /*
+         * Per-thread array of struct niobuf_{local,remote}'s was allocated by
+         * ost_thread_init().
+         */
+        local_nb = ost_tls(req)->local;
 
-        OBD_ALLOC(local_nb, sizeof(*local_nb) * npages);
-        if (local_nb == NULL)
-                GOTO(out_pp_rnb, rc = -ENOMEM);
+        rc = ost_brw_lock_get(LCK_PR, exp, ioo, remote_nb, &lockh);
+        if (rc != 0)
+                GOTO(out_bulk, rc);
 
-        desc = ptlrpc_prep_bulk_exp (req, npages, 
-                                     BULK_PUT_SOURCE, OST_BULK_PORTAL);
-        if (desc == NULL)
-                GOTO(out_local, rc = -ENOMEM);
+        /*
+         * If getting the lock took more time than
+         * client was willing to wait, drop it. b=11330
+         */
+        if (cfs_time_current_sec() > req->rq_deadline ||
+            OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
+                no_reply = 1;
+                CERROR("Dropping timed-out read from %s because locking"
+                       "object "LPX64" took %ld seconds (limit was %ld).\n",
+                       libcfs_id2str(req->rq_peer), ioo->ioo_id,
+                       cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
+                       req->rq_deadline - req->rq_arrival_time.tv_sec);
+                GOTO(out_lock, rc = -ETIMEDOUT);
+        }
 
-        do_gettimeofday(&start);
-        rc = obd_preprw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
-                        ioo, npages, pp_rnb, local_nb, &oti);
-        ost_stime_record(req, &start, 0, 0);
+        npages = OST_THREAD_POOL_SIZE;
+        rc = obd_preprw(OBD_BRW_READ, exp, &body->oa, 1, ioo,
+                        remote_nb, &npages, local_nb, oti, capa);
         if (rc != 0)
-                GOTO(out_bulk, rc);
+                GOTO(out_lock, rc);
 
-        /* We're finishing using body->oa as an input variable */
-        body->oa.o_valid = 0;
+        desc = ptlrpc_prep_bulk_exp(req, npages,
+                                     BULK_PUT_SOURCE, OST_BULK_PORTAL);
+        if (desc == NULL) /* XXX: check all cleanup stuff */
+                GOTO(out, rc = -ENOMEM);
+
+        ost_prolong_locks(exp, ioo, remote_nb, &body->oa, LCK_PW | LCK_PR);
 
         nob = 0;
         for (i = 0; i < npages; i++) {
@@ -469,16 +686,15 @@ static int ost_brw_read(struct ptlrpc_request *req)
                         break;
                 }
 
-                LASSERT(page_rc <= pp_rnb[i].len);
                 nob += page_rc;
                 if (page_rc != 0) {             /* some data! */
                         LASSERT (local_nb[i].page != NULL);
                         ptlrpc_prep_bulk_page(desc, local_nb[i].page,
-                                              pp_rnb[i].offset & (PAGE_SIZE-1),
+                                              local_nb[i].offset & ~CFS_PAGE_MASK,
                                               page_rc);
                 }
 
-                if (page_rc != pp_rnb[i].len) { /* short read */
+                if (page_rc != local_nb[i].len) { /* short read */
                         /* All subsequent pages should be 0 */
                         while(++i < npages)
                                 LASSERT(local_nb[i].rc == 0);
@@ -486,18 +702,71 @@ static int ost_brw_read(struct ptlrpc_request *req)
                 }
         }
 
+        if (body->oa.o_valid & OBD_MD_FLCKSUM) {
+                cksum_type_t cksum_type = OBD_CKSUM_CRC32;
+
+                if (body->oa.o_valid & OBD_MD_FLFLAGS)
+                        cksum_type = cksum_type_unpack(body->oa.o_flags);
+                body->oa.o_flags = cksum_type_pack(cksum_type);
+                body->oa.o_valid = OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
+                body->oa.o_cksum = ost_checksum_bulk(desc, OST_READ, cksum_type);
+                CDEBUG(D_PAGE,"checksum at read origin: %x\n",body->oa.o_cksum);
+        } else {
+                body->oa.o_valid = 0;
+        }
+        /* We're finishing using body->oa as an input variable */
+
+        /* Check if client was evicted while we were doing i/o before touching
+           network */
         if (rc == 0) {
-                rc = ptlrpc_start_bulk_transfer(desc);
+                /* Check if there is eviction in progress, and if so, wait for
+                 * it to finish */
+                if (unlikely(atomic_read(&exp->exp_obd->
+                                                obd_evict_inprogress))) {
+                        lwi = LWI_INTR(NULL, NULL);
+                        rc = l_wait_event(exp->exp_obd->
+                                                obd_evict_inprogress_waitq,
+                                          !atomic_read(&exp->exp_obd->
+                                                        obd_evict_inprogress),
+                                          &lwi);
+                }
+                if (exp->exp_failed)
+                        rc = -ENOTCONN;
+                else {
+                        sptlrpc_svc_wrap_bulk(req, desc);
+
+                        rc = ptlrpc_start_bulk_transfer(desc);
+                }
+
                 if (rc == 0) {
-                        lwi = LWI_TIMEOUT(obd_timeout * HZ / 4,
-                                          ost_bulk_timeout, desc);
-                        rc = l_wait_event(desc->bd_waitq,
-                                          !ptlrpc_bulk_active(desc), &lwi);
-                        LASSERT(rc == 0 || rc == -ETIMEDOUT);
+                        time_t start = cfs_time_current_sec();
+                        do {
+                                long timeoutl = req->rq_deadline -
+                                        cfs_time_current_sec();
+                                cfs_duration_t timeout = (timeoutl <= 0 || rc) ?
+                                        CFS_TICK : cfs_time_seconds(timeoutl);
+                                lwi = LWI_TIMEOUT_INTERVAL(timeout,
+                                                           cfs_time_seconds(1),
+                                                           ost_bulk_timeout,
+                                                           desc);
+                                rc = l_wait_event(desc->bd_waitq,
+                                                  !ptlrpc_bulk_active(desc) ||
+                                                  exp->exp_failed, &lwi);
+                                LASSERT(rc == 0 || rc == -ETIMEDOUT);
+                                /* Wait again if we changed deadline */
+                        } while ((rc == -ETIMEDOUT) &&
+                                 (req->rq_deadline > cfs_time_current_sec()));
+
                         if (rc == -ETIMEDOUT) {
-                                DEBUG_REQ(D_ERROR, req, "timeout on bulk PUT"
-                                          ", expt_conn_cnt = %u",
-                                          req->rq_export->exp_conn_cnt);
+                                DEBUG_REQ(D_ERROR, req,
+                                          "timeout on bulk PUT after %ld%+lds",
+                                          req->rq_deadline - start,
+                                          cfs_time_current_sec() -
+                                          req->rq_deadline);
+                                ptlrpc_abort_bulk(desc);
+                        } else if (exp->exp_failed) {
+                                DEBUG_REQ(D_ERROR, req, "Eviction on bulk PUT");
+                                rc = -ENOTCONN;
                                 ptlrpc_abort_bulk(desc);
                         } else if (!desc->bd_success ||
                                    desc->bd_nob_transferred != desc->bd_nob) {
@@ -510,108 +779,119 @@ static int ost_brw_read(struct ptlrpc_request *req)
                                 rc = -ETIMEDOUT;
                         }
                 } else {
-                        DEBUG_REQ(D_ERROR, req, "bulk PUT failed: rc %d\n", rc);
+                        DEBUG_REQ(D_ERROR, req, "bulk PUT failed: rc %d", rc);
                 }
-                comms_error = rc != 0;
+                no_reply = rc != 0;
         }
 
-        ost_stime_record(req, &start, 0, 1);
         /* Must commit after prep above in all cases */
-        rc = obd_commitrw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
-                          ioo, npages, local_nb, &oti, rc);
-        ost_stime_record(req, &start, 0, 2);
+        rc = obd_commitrw(OBD_BRW_READ, exp, &body->oa, 1, ioo,
+                          remote_nb, npages, local_nb, oti, rc);
 
         if (rc == 0) {
-                repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
+                repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                         sizeof(*repbody));
                 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
-
-#if CHECKSUM_BULK
-                repbody->oa.o_cksum = ost_checksum_bulk(desc);
-                repbody->oa.o_valid |= OBD_MD_FLCKSUM;
-#endif
         }
 
- out_bulk:
-        ptlrpc_free_bulk(desc);
- out_local:
-        OBD_FREE(local_nb, sizeof(*local_nb) * npages);
- out_pp_rnb:
-        free_per_page_niobufs(npages, pp_rnb, remote_nb);
- out:
+out_lock:
+        ost_brw_lock_put(LCK_PR, ioo, remote_nb, &lockh);
+out_bulk:
+        if (desc)
+                ptlrpc_free_bulk(desc);
+out:
         LASSERT(rc <= 0);
         if (rc == 0) {
                 req->rq_status = nob;
+                ptlrpc_lprocfs_brw(req, nob);
+                target_committed_to_req(req);
                 ptlrpc_reply(req);
-        } else if (!comms_error) {
-                /* only reply if comms OK */
+        } else if (!no_reply) {
+                /* Only reply if there was no comms problem with bulk */
+                target_committed_to_req(req);
                 req->rq_status = rc;
                 ptlrpc_error(req);
         } else {
-                if (req->rq_reply_state != NULL) {
-                        /* reply out callback would free */
-                        lustre_free_reply_state (req->rq_reply_state);
-                }
-                if (req->rq_reqmsg->conn_cnt == req->rq_export->exp_conn_cnt) {
-                        CERROR("bulk IO comms error: "
-                               "evicting %s@%s id %s\n",
-                               req->rq_export->exp_client_uuid.uuid,
-                               req->rq_export->exp_connection->c_remote_uuid.uuid,
-                               req->rq_peerstr);
-                        ptlrpc_fail_export(req->rq_export);
-                } else {
-                        CERROR("ignoring bulk IO comms error: "
-                               "client reconnected %s@%s id %s\n",  
-                               req->rq_export->exp_client_uuid.uuid,
-                               req->rq_export->exp_connection->c_remote_uuid.uuid,
-                               req->rq_peerstr);
-                }
+                /* reply out callback would free */
+                ptlrpc_req_drop_rs(req);
+                CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
+                      "client will retry\n",
+                      exp->exp_obd->obd_name,
+                      exp->exp_client_uuid.uuid,
+                      exp->exp_connection->c_remote_uuid.uuid,
+                      libcfs_id2str(req->rq_peer));
         }
 
         RETURN(rc);
 }
 
-int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
+static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
 {
-        struct ptlrpc_bulk_desc *desc;
+        struct ptlrpc_bulk_desc *desc = NULL;
+        struct obd_export       *exp = req->rq_export;
         struct niobuf_remote    *remote_nb;
-        struct niobuf_remote    *pp_rnb;
         struct niobuf_local     *local_nb;
         struct obd_ioobj        *ioo;
         struct ost_body         *body, *repbody;
         struct l_wait_info       lwi;
+        struct lustre_handle     lockh = {0};
+        struct lustre_capa      *capa = NULL;
         __u32                   *rcs;
-        int                      size[2] = { sizeof(*body) };
-        int                      objcount, niocount, npages;
-        int                      comms_error = 0;
-        int                      rc, swab, i, j;
-        struct timeval           start;        
+        __u32 size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
+        int objcount, niocount, npages;
+        int rc, swab, i, j;
+        obd_count                client_cksum = 0, server_cksum = 0;
+        cksum_type_t             cksum_type = OBD_CKSUM_CRC32;
+        int                      no_reply = 0;
         ENTRY;
 
+        req->rq_bulk_write = 1;
+
         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
                 GOTO(out, rc = -EIO);
+        if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK2))
+                GOTO(out, rc = -EFAULT);
 
         /* pause before transaction has been started */
-        OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
-                         (obd_timeout + 1) / 4);
+        OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
+
+        /* Check if there is eviction in progress, and if so, wait for it to
+         * finish */
+        if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
+                lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
+                rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
+                        !atomic_read(&exp->exp_obd->obd_evict_inprogress),
+                        &lwi);
+        }
+        if (exp->exp_failed)
+                GOTO(out, rc = -ENOTCONN);
 
         swab = lustre_msg_swabbed(req->rq_reqmsg);
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
+        body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
+                                  lustre_swab_ost_body);
         if (body == NULL) {
                 CERROR("Missing/short ost_body\n");
                 GOTO(out, rc = -EFAULT);
         }
 
-        LASSERT_REQSWAB(req, 1);
-        objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
+        lustre_set_req_swabbed(req, REQ_REC_OFF + 1);
+        objcount = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1) /
+                   sizeof(*ioo);
         if (objcount == 0) {
                 CERROR("Missing/short ioobj\n");
                 GOTO(out, rc = -EFAULT);
         }
-        ioo = lustre_msg_buf (req->rq_reqmsg, 1, objcount * sizeof(*ioo));
+        if (objcount > 1) {
+                CERROR("too many ioobjs (%d)\n", objcount);
+                GOTO(out, rc = -EFAULT);
+        }
+
+        ioo = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1,
+                             objcount * sizeof(*ioo));
         LASSERT (ioo != NULL);
         for (niocount = i = 0; i < objcount; i++) {
                 if (swab)
-                        lustre_swab_obd_ioobj (&ioo[i]);
+                        lustre_swab_obd_ioobj(&ioo[i]);
                 if (ioo[i].ioo_bufcnt == 0) {
                         CERROR("ioo[%d] has zero bufcnt\n", i);
                         GOTO(out, rc = -EFAULT);
@@ -619,7 +899,14 @@ int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
                 niocount += ioo[i].ioo_bufcnt;
         }
 
-        remote_nb = lustre_swab_reqbuf(req, 2, niocount * sizeof(*remote_nb),
+        if (niocount > PTLRPC_MAX_BRW_PAGES) {
+                DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)",
+                          niocount);
+                GOTO(out, rc = -EFAULT);
+        }
+
+        remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
+                                       niocount * sizeof(*remote_nb),
                                        lustre_swab_niobuf_remote);
         if (remote_nb == NULL) {
                 CERROR("Missing/short niobuf\n");
@@ -630,319 +917,418 @@ int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
                         lustre_swab_niobuf_remote (&remote_nb[i]);
         }
 
-        size[1] = niocount * sizeof(*rcs);
-        rc = lustre_pack_reply(req, 2, size, NULL);
+        if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
+                capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 3);
+
+        size[REPLY_REC_OFF + 1] = niocount * sizeof(*rcs);
+        rc = lustre_pack_reply(req, 3, size, NULL);
         if (rc != 0)
                 GOTO(out, rc);
-        rcs = lustre_msg_buf(req->rq_repmsg, 1, niocount * sizeof(*rcs));
+        OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_PACK, obd_fail_val);
+        rcs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
+                             niocount * sizeof(*rcs));
 
-#if 0
-        /* Do snap options here*/
-        rc = obd_do_cow(req->rq_export, ioo, objcount, remote_nb);
-        if (rc)
-                GOTO(out, rc);
-#endif
+        /*
+         * Per-thread array of struct niobuf_{local,remote}'s was allocated by
+         * ost_thread_init().
+         */
+        local_nb = ost_tls(req)->local;
 
-        /* FIXME all niobuf splitting should be done in obdfilter if needed */
-        /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
-        npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
-        if (npages < 0)
-                GOTO(out, rc = npages);
+        rc = ost_brw_lock_get(LCK_PW, exp, ioo, remote_nb, &lockh);
+        if (rc != 0)
+                GOTO(out_bulk, rc);
 
-        OBD_ALLOC(local_nb, sizeof(*local_nb) * npages);
-        if (local_nb == NULL)
-                GOTO(out_pp_rnb, rc = -ENOMEM);
+        /*
+         * If getting the lock took more time than
+         * client was willing to wait, drop it. b=11330
+         */
+        if (cfs_time_current_sec() > req->rq_deadline ||
+            OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
+                no_reply = 1;
+                CERROR("Dropping timed-out write from %s because locking "
+                       "object "LPX64" took %ld seconds (limit was %ld).\n",
+                       libcfs_id2str(req->rq_peer), ioo->ioo_id,
+                       cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
+                       req->rq_deadline - req->rq_arrival_time.tv_sec);
+                GOTO(out_lock, rc = -ETIMEDOUT);
+        }
 
-        desc = ptlrpc_prep_bulk_exp (req, npages, 
-                                     BULK_GET_SINK, OST_BULK_PORTAL);
-        if (desc == NULL)
-                GOTO(out_local, rc = -ENOMEM);
+        ost_prolong_locks(exp, ioo, remote_nb,&body->oa,  LCK_PW);
 
-        do_gettimeofday(&start);
-        rc = obd_preprw(OBD_BRW_WRITE, req->rq_export, &body->oa, objcount,
-                        ioo, npages, pp_rnb, local_nb, oti);
-        ost_stime_record(req, &start, 1, 0);
+        /* obd_preprw clobbers oa->valid, so save what we need */
+        if (body->oa.o_valid & OBD_MD_FLCKSUM) {
+                client_cksum = body->oa.o_cksum;
+                if (body->oa.o_valid & OBD_MD_FLFLAGS)
+                        cksum_type = cksum_type_unpack(body->oa.o_flags);
+        }
+
+        /* Because we already sync grant info with client when reconnect,
+         * grant info will be cleared for resent req, then fed_grant and
+         * total_grant will not be modified in following preprw_write */
+        if (lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) {
+                DEBUG_REQ(D_CACHE, req, "clear resent/replay req grant info");
+                body->oa.o_valid &= ~OBD_MD_FLGRANT;
+        }
+
+        npages = OST_THREAD_POOL_SIZE;
+        rc = obd_preprw(OBD_BRW_WRITE, exp, &body->oa, objcount,
+                        ioo, remote_nb, &npages, local_nb, oti, capa);
         if (rc != 0)
-                GOTO(out_bulk, rc);
+                GOTO(out_lock, rc);
+
+        desc = ptlrpc_prep_bulk_exp(req, npages,
+                                     BULK_GET_SINK, OST_BULK_PORTAL);
+        if (desc == NULL)
+                GOTO(out, rc = -ENOMEM);
 
         /* NB Having prepped, we must commit... */
 
         for (i = 0; i < npages; i++)
-                ptlrpc_prep_bulk_page(desc, local_nb[i].page, 
-                                      pp_rnb[i].offset & (PAGE_SIZE - 1),
-                                      pp_rnb[i].len);
-
-        rc = ptlrpc_start_bulk_transfer (desc);
+                ptlrpc_prep_bulk_page(desc, local_nb[i].page,
+                                      local_nb[i].offset & ~CFS_PAGE_MASK,
+                                      local_nb[i].len);
+
+        /* Check if client was evicted while we were doing i/o before touching
+           network */
+        if (desc->bd_export->exp_failed)
+                rc = -ENOTCONN;
+        else
+                rc = ptlrpc_start_bulk_transfer (desc);
         if (rc == 0) {
-                lwi = LWI_TIMEOUT(obd_timeout * HZ / 4,
-                                  ost_bulk_timeout, desc);
-                rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), 
-                                  &lwi);
-                LASSERT(rc == 0 || rc == -ETIMEDOUT);
+                time_t start = cfs_time_current_sec();
+                do {
+                        long timeoutl = req->rq_deadline -
+                                cfs_time_current_sec();
+                        cfs_duration_t timeout = (timeoutl <= 0 || rc) ?
+                                CFS_TICK : cfs_time_seconds(timeoutl);
+                        lwi = LWI_TIMEOUT_INTERVAL(timeout, cfs_time_seconds(1),
+                                                   ost_bulk_timeout, desc);
+                        rc = l_wait_event(desc->bd_waitq,
+                                          !ptlrpc_bulk_active(desc) ||
+                                          desc->bd_export->exp_failed, &lwi);
+                        LASSERT(rc == 0 || rc == -ETIMEDOUT);
+                        /* Wait again if we changed deadline */
+                } while ((rc == -ETIMEDOUT) &&
+                         (req->rq_deadline > cfs_time_current_sec()));
+
                 if (rc == -ETIMEDOUT) {
-                        DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
+                        DEBUG_REQ(D_ERROR, req,
+                                  "timeout on bulk GET after %ld%+lds",
+                                  req->rq_deadline - start,
+                                  cfs_time_current_sec() -
+                                  req->rq_deadline);
+                        ptlrpc_abort_bulk(desc);
+                } else if (desc->bd_export->exp_failed) {
+                        DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
+                        rc = -ENOTCONN;
                         ptlrpc_abort_bulk(desc);
                 } else if (!desc->bd_success ||
                            desc->bd_nob_transferred != desc->bd_nob) {
                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
-                                  desc->bd_success ? 
+                                  desc->bd_success ?
                                   "truncated" : "network error on",
                                   desc->bd_nob_transferred, desc->bd_nob);
                         /* XXX should this be a different errno? */
                         rc = -ETIMEDOUT;
                 }
         } else {
-                DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d\n", rc);
+                DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
         }
-        comms_error = rc != 0;
+        no_reply = rc != 0;
+
+        if (rc == 0)
+                sptlrpc_svc_unwrap_bulk(req, desc);
 
-        repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
+        repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                 sizeof(*repbody));
         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
 
-#if CHECKSUM_BULK
-        if (rc == 0 && (body->oa.o_valid & OBD_MD_FLCKSUM) != 0) {
+        if (unlikely(client_cksum != 0 && rc == 0)) {
                 static int cksum_counter;
-                obd_count client_cksum = body->oa.o_cksum;
-                obd_count cksum = ost_checksum_bulk(desc);
-
-                if (client_cksum != cksum) {
-                        CERROR("Bad checksum: client %x, server %x id %s\n",
-                               client_cksum, cksum,
-                               req->rq_peerstr);
-                        cksum_counter = 1;
-                        repbody->oa.o_cksum = cksum;
-                } else {
-                        cksum_counter++;
-                        if ((cksum_counter & (-cksum_counter)) == cksum_counter)
-                                CWARN("Checksum %u from NID %s: %x OK\n",         
-                                      cksum_counter, req->rq_peerstr, cksum);
+                repbody->oa.o_valid |= OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
+                repbody->oa.o_flags &= ~OBD_FL_CKSUM_ALL;
+                repbody->oa.o_flags |= cksum_type_pack(cksum_type);
+                server_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
+                repbody->oa.o_cksum = server_cksum;
+                cksum_counter++;
+                if (unlikely(client_cksum != server_cksum)) {
+                        CERROR("client csum %x, server csum %x\n",
+                               client_cksum, server_cksum);
+                        cksum_counter = 0;
+                } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
+                        CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
+                               cksum_counter, libcfs_id2str(req->rq_peer),
+                               server_cksum);
                 }
         }
-#endif
-        ost_stime_record(req, &start, 1, 1);
+
         /* Must commit after prep above in all cases */
-        rc = obd_commitrw(OBD_BRW_WRITE, req->rq_export, &repbody->oa,
-                          objcount, ioo, npages, local_nb, oti, rc);
+        rc = obd_commitrw(OBD_BRW_WRITE, exp, &repbody->oa, objcount, ioo,
+                          remote_nb, npages, local_nb, oti, rc);
+
+        if (unlikely(client_cksum != server_cksum && rc == 0)) {
+                int  new_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
+                char *msg;
+                char *via;
+                char *router;
+
+                if (new_cksum == server_cksum)
+                        msg = "changed in transit before arrival at OST";
+                else if (new_cksum == client_cksum)
+                        msg = "initial checksum before message complete";
+                else
+                        msg = "changed in transit AND after initial checksum";
+
+                if (req->rq_peer.nid == desc->bd_sender) {
+                        via = router = "";
+                } else {
+                        via = " via ";
+                        router = libcfs_nid2str(desc->bd_sender);
+                }
+
+                LCONSOLE_ERROR_MSG(0x168, "%s: BAD WRITE CHECKSUM: %s from "
+                                   "%s%s%s inum "LPU64"/"LPU64" object "
+                                   LPU64"/"LPU64" extent ["LPU64"-"LPU64"]\n",
+                                   exp->exp_obd->obd_name, msg,
+                                   libcfs_id2str(req->rq_peer),
+                                   via, router,
+                                   body->oa.o_valid & OBD_MD_FLFID ?
+                                                body->oa.o_fid : (__u64)0,
+                                   body->oa.o_valid & OBD_MD_FLFID ?
+                                                body->oa.o_generation :(__u64)0,
+                                   body->oa.o_id,
+                                   body->oa.o_valid & OBD_MD_FLGROUP ?
+                                                body->oa.o_gr : (__u64)0,
+                                   local_nb[0].offset,
+                                   local_nb[npages-1].offset +
+                                   local_nb[npages-1].len - 1 );
+                CERROR("client csum %x, original server csum %x, "
+                       "server csum now %x\n",
+                       client_cksum, server_cksum, new_cksum);
+        }
 
-        ost_stime_record(req, &start, 1, 2);
         if (rc == 0) {
-#if CHECKSUM_BULK
-                repbody->oa.o_cksum = ost_checksum_bulk(desc);
-                repbody->oa.o_valid |= OBD_MD_FLCKSUM;
-#endif
+                int nob = 0;
+
                 /* set per-requested niobuf return codes */
                 for (i = j = 0; i < niocount; i++) {
-                        int nob = remote_nb[i].len;
+                        int len = remote_nb[i].len;
 
+                        nob += len;
                         rcs[i] = 0;
                         do {
                                 LASSERT(j < npages);
                                 if (local_nb[j].rc < 0)
                                         rcs[i] = local_nb[j].rc;
-                                nob -= pp_rnb[j].len;
+                                len -= local_nb[j].len;
                                 j++;
-                        } while (nob > 0);
-                        LASSERT(nob == 0);
+                        } while (len > 0);
+                        LASSERT(len == 0);
                 }
                 LASSERT(j == npages);
+                ptlrpc_lprocfs_brw(req, nob);
         }
-        /*XXX This write extents only for write-back cache extents*/
-        rc = obd_write_extents(req->rq_export, ioo, objcount, niocount, 
-                               local_nb, rc);
- out_bulk:
-        ptlrpc_free_bulk(desc);
- out_local:
-        OBD_FREE(local_nb, sizeof(*local_nb) * npages);
- out_pp_rnb:
-        free_per_page_niobufs(npages, pp_rnb, remote_nb);
- out:
+
+out_lock:
+        ost_brw_lock_put(LCK_PW, ioo, remote_nb, &lockh);
+out_bulk:
+        if (desc)
+                ptlrpc_free_bulk(desc);
+out:
         if (rc == 0) {
                 oti_to_request(oti, req);
+                target_committed_to_req(req);
                 rc = ptlrpc_reply(req);
-        } else if (!comms_error) {
+        } else if (!no_reply) {
                 /* Only reply if there was no comms problem with bulk */
+                target_committed_to_req(req);
                 req->rq_status = rc;
                 ptlrpc_error(req);
         } else {
-                if (req->rq_reply_state != NULL) {
-                        /* reply out callback would free */
-                        lustre_free_reply_state (req->rq_reply_state);
-                }
-                if (req->rq_reqmsg->conn_cnt == req->rq_export->exp_conn_cnt) {
-                        CERROR("%s: bulk IO comm error evicting %s@%s id %s\n",
-                               req->rq_export->exp_obd->obd_name,
-                               req->rq_export->exp_client_uuid.uuid,
-                               req->rq_export->exp_connection->c_remote_uuid.uuid,
-                               req->rq_peerstr);
-                        ptlrpc_fail_export(req->rq_export);
-                } else {
-                        CERROR("ignoring bulk IO comms error: "
-                               "client reconnected %s@%s id %s\n",
-                               req->rq_export->exp_client_uuid.uuid,
-                               req->rq_export->exp_connection->c_remote_uuid.uuid,
-                               req->rq_peerstr);
-                }
+                /* reply out callback would free */
+                ptlrpc_req_drop_rs(req);
+                CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
+                      "client will retry\n",
+                      exp->exp_obd->obd_name,
+                      exp->exp_client_uuid.uuid,
+                      exp->exp_connection->c_remote_uuid.uuid,
+                      libcfs_id2str(req->rq_peer));
         }
         RETURN(rc);
 }
-EXPORT_SYMBOL(ost_brw_write);
 
-static int ost_san_brw(struct ptlrpc_request *req, int cmd)
+static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
 {
-        struct niobuf_remote *remote_nb, *res_nb, *pp_rnb;
-        struct obd_ioobj *ioo;
-        struct ost_body *body, *repbody;
-        int rc, i, objcount, niocount, size[2] = {sizeof(*body)}, npages;
-        int swab;
+        char *key, *val = NULL;
+        int keylen, vallen, rc = 0;
         ENTRY;
 
-        /* XXX not set to use latest protocol */
-
-        swab = lustre_msg_swabbed(req->rq_reqmsg);
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
-        if (body == NULL) {
-                CERROR("Missing/short ost_body\n");
-                GOTO(out, rc = -EFAULT);
-        }
-
-        ioo = lustre_swab_reqbuf(req, 1, sizeof(*ioo), lustre_swab_obd_ioobj);
-        if (ioo == NULL) {
-                CERROR("Missing/short ioobj\n");
-                GOTO(out, rc = -EFAULT);
-        }
-        objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
-        niocount = ioo[0].ioo_bufcnt;
-        for (i = 1; i < objcount; i++) {
-                if (swab)
-                        lustre_swab_obd_ioobj (&ioo[i]);
-                niocount += ioo[i].ioo_bufcnt;
-        }
-
-        remote_nb = lustre_swab_reqbuf(req, 2, niocount * sizeof(*remote_nb),
-                                       lustre_swab_niobuf_remote);
-        if (remote_nb == NULL) {
-                CERROR("Missing/short niobuf\n");
-                GOTO(out, rc = -EFAULT);
-        }
-        if (swab) {                             /* swab the remaining niobufs */
-                for (i = 1; i < niocount; i++)
-                        lustre_swab_niobuf_remote (&remote_nb[i]);
+        key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
+        if (key == NULL) {
+                DEBUG_REQ(D_HA, req, "no set_info key");
+                RETURN(-EFAULT);
         }
+        keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
 
-        /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
-        npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
-        if (npages < 0)
-                GOTO (out, rc = npages);
-        size[1] = npages * sizeof(*pp_rnb);
-        rc = lustre_pack_reply(req, 2, size, NULL);
+        rc = lustre_pack_reply(req, 1, NULL, NULL);
         if (rc)
-                GOTO(out_pp_rnb, rc);
+                RETURN(rc);
 
-        req->rq_status = obd_san_preprw(cmd, req->rq_export, &body->oa,
-                                        objcount, ioo, npages, pp_rnb);
+        vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
+        if (vallen)
+                val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
 
-        if (req->rq_status)
-                GOTO(out_pp_rnb, rc = 0);
+        if (KEY_IS(KEY_EVICT_BY_NID)) {
+                if (val && vallen)
+                        obd_export_evict_by_nid(exp->exp_obd, val);
 
-        repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
-        memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
+                GOTO(out, rc = 0);
+        }
 
-        res_nb = lustre_msg_buf(req->rq_repmsg, 1, size[1]);
-        memcpy(res_nb, remote_nb, size[1]);
-        rc = 0;
-out_pp_rnb:
-        free_per_page_niobufs(npages, pp_rnb, remote_nb);
+        rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
 out:
-        if (rc) {
-                req->rq_status = rc;
-                ptlrpc_error(req);
-        } else
-                ptlrpc_reply(req);
-
-        return rc;
+        lustre_msg_set_status(req->rq_repmsg, 0);
+        RETURN(rc);
 }
 
-static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
+static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
 {
-        char *key, *val;
-        int keylen, rc = 0;
+        void *key, *reply;
+        int keylen, replylen, rc = 0;
+        struct req_capsule *pill = &req->rq_pill;
         ENTRY;
 
-        key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
+        req_capsule_set(&req->rq_pill, &RQF_OST_GET_INFO_GENERIC);
+
+        /* this common part for get_info rpc */
+        key = req_capsule_client_get(pill, &RMF_SETINFO_KEY);
         if (key == NULL) {
-                DEBUG_REQ(D_HA, req, "no set_info key");
+                DEBUG_REQ(D_HA, req, "no get_info key");
                 RETURN(-EFAULT);
         }
-        keylen = req->rq_reqmsg->buflens[0];
+        keylen = req_capsule_get_size(pill, &RMF_SETINFO_KEY, RCL_CLIENT);
 
-        rc = lustre_pack_reply(req, 0, NULL, NULL);
+        rc = obd_get_info(exp, keylen, key, &replylen, NULL, NULL);
         if (rc)
                 RETURN(rc);
 
-        val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
-        
-        if (keylen == 8 && memcmp(key, "auditlog", 8) == 0) {
-                lustre_swab_reqbuf(req, 1, sizeof(struct audit_msg),
-                                   lustre_swab_audit_msg);
-        }
-        else if (keylen == 5 && strcmp(key, "audit") == 0) {
-                lustre_swab_reqbuf(req, 1, sizeof(struct audit_attr_msg),
-                                   lustre_swab_audit_attr);
-        }
-        else if (keylen == 9 && strcmp(key, "audit_obj") == 0) {
-                lustre_swab_reqbuf(req, 1, sizeof(struct obdo),
-                                   lustre_swab_obdo);
-        }
+        req_capsule_set_size(pill, &RMF_GENERIC_DATA,
+                             RCL_SERVER, replylen);
+
+        rc = req_capsule_server_pack(pill);
+        if (rc)
+                RETURN(rc);
+
+        reply = req_capsule_server_get(pill, &RMF_GENERIC_DATA);
+        if (reply == NULL)
+                RETURN(-ENOMEM);
+
+        /* call again to fill in the reply buffer */
+        rc = obd_get_info(exp, keylen, key, &replylen, reply, NULL);
 
-        rc = obd_set_info(exp, keylen, key, req->rq_reqmsg->buflens[1], val);
-        req->rq_repmsg->status = 0;
+        lustre_msg_set_status(req->rq_repmsg, 0);
         RETURN(rc);
 }
 
-static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
+static int ost_handle_quotactl(struct ptlrpc_request *req)
 {
-        char *key;
-        int keylen, rc = 0, size = sizeof(obd_id);
-        obd_id *reply;
+        struct obd_quotactl *oqctl, *repoqc;
+        __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*repoqc) };
+        int rc;
         ENTRY;
 
-        key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
-        if (key == NULL) {
-                DEBUG_REQ(D_HA, req, "no get_info key");
-                RETURN(-EFAULT);
-        }
-        keylen = req->rq_reqmsg->buflens[0];
-
-        if (keylen < strlen("last_id") || memcmp(key, "last_id", 7) != 0)
-                RETURN(-EPROTO);
+        oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
+                                   lustre_swab_obd_quotactl);
+        if (oqctl == NULL)
+                GOTO(out, rc = -EPROTO);
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
-                RETURN(rc);
+                GOTO(out, rc);
+
+        repoqc = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*repoqc));
 
-        reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply));
-        rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
-        req->rq_repmsg->status = 0;
+        req->rq_status = obd_quotactl(req->rq_export, oqctl);
+        *repoqc = *oqctl;
+out:
         RETURN(rc);
 }
 
+static int ost_handle_quotacheck(struct ptlrpc_request *req)
+{
+        struct obd_quotactl *oqctl;
+        int rc;
+        ENTRY;
+
+        oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
+        if (oqctl == NULL)
+                RETURN(-EPROTO);
+
+        rc = req_capsule_server_pack(&req->rq_pill);
+        if (rc) {
+                CERROR("ost: out of memory while packing quotacheck reply\n");
+                RETURN(-ENOMEM);
+        }
+
+        req->rq_status = obd_quotacheck(req->rq_export, oqctl);
+        RETURN(0);
+}
+
 static int ost_llog_handle_connect(struct obd_export *exp,
-                                  struct ptlrpc_request *req)
+                                   struct ptlrpc_request *req)
 {
         struct llogd_conn_body *body;
         int rc;
         ENTRY;
 
-        body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
+        body = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*body));
         rc = obd_llog_connect(exp, body);
         RETURN(rc);
 }
 
+static int filter_export_check_flavor(struct filter_obd *filter,
+                                      struct obd_export *exp,
+                                      struct ptlrpc_request *req)
+{
+        int     rc = 0;
+
+        /* FIXME
+         * this should be done in filter_connect()/filter_reconnect(), but
+         * we can't obtain information like NID, which stored in incoming
+         * request, thus can't decide what flavor to use. so we do it here.
+         *
+         * This hack should be removed after the OST stack be rewritten, just
+         * like what we are doing in mdt_obd_connect()/mdt_obd_reconnect().
+         */
+        if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_INVALID)
+                return 0;
+
+        CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
+        spin_lock(&exp->exp_lock);
+        exp->exp_sp_peer = req->rq_sp_from;
+
+        read_lock(&filter->fo_sptlrpc_lock);
+        sptlrpc_rule_set_choose(&filter->fo_sptlrpc_rset, exp->exp_sp_peer,
+                                req->rq_peer.nid, &exp->exp_flvr);
+        read_unlock(&filter->fo_sptlrpc_lock);
+
+        if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
+                CERROR("invalid rpc flavor %x, expect %x, from %s\n",
+                       req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
+                       libcfs_nid2str(req->rq_peer.nid));
+                exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
+                rc = -EACCES;
+        }
+
+        spin_unlock(&exp->exp_lock);
+
+        return rc;
+}
+
 static int ost_filter_recovery_request(struct ptlrpc_request *req,
                                        struct obd_device *obd, int *process)
 {
-        switch (req->rq_reqmsg->opc) {
+        switch (lustre_msg_get_opc(req->rq_reqmsg)) {
         case OST_CONNECT: /* This will never get here, but for completeness. */
         case OST_DISCONNECT:
                *process = 1;
@@ -962,10 +1348,8 @@ static int ost_filter_recovery_request(struct ptlrpc_request *req,
 
         default:
                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
-                *process = 0;
-                /* XXX what should we set rq_status to here? */
-                req->rq_status = -EAGAIN;
-                RETURN(ptlrpc_error(req));
+                *process = -EAGAIN;
+                RETURN(0);
         }
 }
 
@@ -973,27 +1357,39 @@ int ost_msg_check_version(struct lustre_msg *msg)
 {
         int rc;
 
-        switch(msg->opc) {
+        switch(lustre_msg_get_opc(msg)) {
         case OST_CONNECT:
         case OST_DISCONNECT:
         case OBD_PING:
+        case SEC_CTX_INIT:
+        case SEC_CTX_INIT_CONT:
+        case SEC_CTX_FINI:
+                rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
+                if (rc)
+                        CERROR("bad opc %u version %08x, expecting %08x\n",
+                               lustre_msg_get_opc(msg),
+                               lustre_msg_get_version(msg),
+                               LUSTRE_OBD_VERSION);
+                break;
         case OST_CREATE:
         case OST_DESTROY:
         case OST_GETATTR:
         case OST_SETATTR:
         case OST_WRITE:
         case OST_READ:
-        case OST_SAN_READ:
-        case OST_SAN_WRITE:
         case OST_PUNCH:
         case OST_STATFS:
         case OST_SYNC:
         case OST_SET_INFO:
         case OST_GET_INFO:
-                rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
+        case OST_QUOTACHECK:
+        case OST_QUOTACTL:
+                rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
                 if (rc)
                         CERROR("bad opc %u version %08x, expecting %08x\n",
-                               msg->opc, msg->version, LUSTRE_OBD_VERSION);
+                               lustre_msg_get_opc(msg),
+                               lustre_msg_get_version(msg),
+                               LUSTRE_OST_VERSION);
                 break;
         case LDLM_ENQUEUE:
         case LDLM_CONVERT:
@@ -1003,60 +1399,57 @@ int ost_msg_check_version(struct lustre_msg *msg)
                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
                 if (rc)
                         CERROR("bad opc %u version %08x, expecting %08x\n",
-                               msg->opc, msg->version, LUSTRE_DLM_VERSION);
+                               lustre_msg_get_opc(msg),
+                               lustre_msg_get_version(msg),
+                               LUSTRE_DLM_VERSION);
                 break;
-        case OBD_LOG_CANCEL:
         case LLOG_ORIGIN_CONNECT:
+        case OBD_LOG_CANCEL:
                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
                 if (rc)
                         CERROR("bad opc %u version %08x, expecting %08x\n",
-                               msg->opc, msg->version, LUSTRE_LOG_VERSION);
-                break;
-        case SEC_INIT:
-        case SEC_INIT_CONTINUE:
-        case SEC_FINI:
-                rc = 0;
+                               lustre_msg_get_opc(msg),
+                               lustre_msg_get_version(msg),
+                               LUSTRE_LOG_VERSION);
                 break;
         default:
-                CERROR("OST unexpected opcode %d\n", msg->opc);
+                CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
                 rc = -ENOTSUPP;
-                break;
         }
         return rc;
 }
 
+/* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
 int ost_handle(struct ptlrpc_request *req)
 {
+        struct obd_trans_info trans_info = { 0, };
+        struct obd_trans_info *oti = &trans_info;
         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
-        struct obd_trans_info *oti = NULL;
         struct obd_device *obd = NULL;
         ENTRY;
 
         LASSERT(current->journal_info == NULL);
 
-        rc = ost_msg_check_version(req->rq_reqmsg);
-        if (rc) {
-                CERROR("OST drop mal-formed request\n");
-                RETURN(rc);
+        /* primordial rpcs don't affect server recovery */
+        switch (lustre_msg_get_opc(req->rq_reqmsg)) {
+        case SEC_CTX_INIT:
+        case SEC_CTX_INIT_CONT:
+        case SEC_CTX_FINI:
+                GOTO(out, rc = 0);
         }
 
-        /* Security opc should NOT trigger any recovery events */
-        if (req->rq_reqmsg->opc == SEC_INIT ||
-            req->rq_reqmsg->opc == SEC_INIT_CONTINUE ||
-            req->rq_reqmsg->opc == SEC_FINI) {
-                GOTO(out_check_req, rc = 0);
-        }
+        req_capsule_init(&req->rq_pill, req, RCL_SERVER);
 
         /* XXX identical to MDS */
-        if (req->rq_reqmsg->opc != OST_CONNECT) {
+        if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
                 int recovering;
 
                 if (req->rq_export == NULL) {
                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
-                               req->rq_reqmsg->opc,
-                               req->rq_peerstr);
+                               lustre_msg_get_opc(req->rq_reqmsg),
+                               libcfs_id2str(req->rq_peer));
                         req->rq_status = -ENOTCONN;
-                        GOTO(out_check_req, rc = -ENOTCONN);
+                        GOTO(out, rc = -ENOTCONN);
                 }
 
                 obd = req->rq_export->exp_obd;
@@ -1065,12 +1458,12 @@ int ost_handle(struct ptlrpc_request *req)
                 spin_lock_bh(&obd->obd_processing_task_lock);
                 recovering = obd->obd_recovering;
                 spin_unlock_bh(&obd->obd_processing_task_lock);
-               if (recovering) {
+                if (recovering) {
                         rc = ost_filter_recovery_request(req, obd,
                                                          &should_process);
                         if (rc || !should_process)
                                 RETURN(rc);
-                        if (should_process < 0) {
+                        else if (should_process < 0) {
                                 req->rq_status = should_process;
                                 rc = ptlrpc_error(req);
                                 RETURN(rc);
@@ -1078,126 +1471,180 @@ int ost_handle(struct ptlrpc_request *req)
                 }
         }
 
-       OBD_ALLOC(oti, sizeof(*oti));
-       if (oti == NULL)
-               RETURN(-ENOMEM);
-               
         oti_init(oti, req);
 
-        switch (req->rq_reqmsg->opc) {
+        rc = ost_msg_check_version(req->rq_reqmsg);
+        if (rc)
+                RETURN(rc);
+
+        switch (lustre_msg_get_opc(req->rq_reqmsg)) {
         case OST_CONNECT: {
                 CDEBUG(D_INODE, "connect\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_CONNECT_NET, out_free_oti, rc = 0);
+                req_capsule_set(&req->rq_pill, &RQF_OST_CONNECT);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET))
+                        RETURN(0);
                 rc = target_handle_connect(req);
-                if (!rc)
-                        obd = req->rq_export->exp_obd;
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET2))
+                        RETURN(0);
+                if (!rc) {
+                        struct obd_export *exp = req->rq_export;
+
+                        obd = exp->exp_obd;
+
+                        rc = filter_export_check_flavor(&obd->u.filter,
+                                                        exp, req);
+                }
                 break;
         }
         case OST_DISCONNECT:
                 CDEBUG(D_INODE, "disconnect\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_DISCONNECT_NET, out_free_oti, rc = 0);
+                req_capsule_set(&req->rq_pill, &RQF_OST_DISCONNECT);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_DISCONNECT_NET))
+                        RETURN(0);
                 rc = target_handle_disconnect(req);
                 break;
         case OST_CREATE:
                 CDEBUG(D_INODE, "create\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_ENOSPC, out_check_req, rc = -ENOSPC);
-                OBD_FAIL_GOTO(OBD_FAIL_OST_EROFS, out_check_req, rc = -EROFS);
-                OBD_FAIL_GOTO(OBD_FAIL_OST_CREATE_NET, out_free_oti, rc = 0);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_CREATE_NET))
+                        RETURN(0);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
+                        GOTO(out, rc = -ENOSPC);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
+                        GOTO(out, rc = -EROFS);
                 rc = ost_create(req->rq_export, req, oti);
                 break;
         case OST_DESTROY:
                 CDEBUG(D_INODE, "destroy\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_DESTROY_NET, out_free_oti, rc = 0);
-                OBD_FAIL_GOTO(OBD_FAIL_OST_EROFS, out_check_req, rc = -EROFS);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_DESTROY_NET))
+                        RETURN(0);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
+                        GOTO(out, rc = -EROFS);
                 rc = ost_destroy(req->rq_export, req, oti);
                 break;
         case OST_GETATTR:
                 CDEBUG(D_INODE, "getattr\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_GETATTR_NET, out_free_oti, rc = 0);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_GETATTR_NET))
+                        RETURN(0);
                 rc = ost_getattr(req->rq_export, req);
                 break;
         case OST_SETATTR:
                 CDEBUG(D_INODE, "setattr\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_SETATTR_NET, out_free_oti, rc = 0);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_NET))
+                        RETURN(0);
                 rc = ost_setattr(req->rq_export, req, oti);
                 break;
         case OST_WRITE:
                 CDEBUG(D_INODE, "write\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_BRW_NET, out_free_oti, rc = 0);
-                OBD_FAIL_GOTO(OBD_FAIL_OST_ENOSPC, out_check_req, rc = -ENOSPC);
-                OBD_FAIL_GOTO(OBD_FAIL_OST_EROFS, out_check_req, rc = -EROFS);
+                /* req->rq_request_portal would be nice, if it was set */
+                if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
+                        CERROR("%s: deny write request from %s to portal %u\n",
+                               req->rq_export->exp_obd->obd_name,
+                               obd_export_nid2str(req->rq_export),
+                               req->rq_rqbd->rqbd_service->srv_req_portal);
+                        GOTO(out, rc = -EPROTO);
+                }
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
+                        RETURN(0);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
+                        GOTO(out, rc = -ENOSPC);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
+                        GOTO(out, rc = -EROFS);
                 rc = ost_brw_write(req, oti);
                 LASSERT(current->journal_info == NULL);
-                /* ost_brw sends its own replies */
-                GOTO(out_free_oti, rc);
+                /* ost_brw_write sends its own replies */
+                RETURN(rc);
         case OST_READ:
                 CDEBUG(D_INODE, "read\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_BRW_NET, out_free_oti, rc = 0);
-                rc = ost_brw_read(req);
+                /* req->rq_request_portal would be nice, if it was set */
+                if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
+                        CERROR("%s: deny read request from %s to portal %u\n",
+                               req->rq_export->exp_obd->obd_name,
+                               obd_export_nid2str(req->rq_export),
+                               req->rq_rqbd->rqbd_service->srv_req_portal);
+                        GOTO(out, rc = -EPROTO);
+                }
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
+                        RETURN(0);
+                rc = ost_brw_read(req, oti);
                 LASSERT(current->journal_info == NULL);
-                /* ost_brw sends its own replies */
-                GOTO(out_free_oti, rc);
-        case OST_SAN_READ:
-                CDEBUG(D_INODE, "san read\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_BRW_NET, out_free_oti, rc = 0);
-                rc = ost_san_brw(req, OBD_BRW_READ);
-                /* ost_san_brw sends its own replies */
-                GOTO(out_free_oti, rc);
-        case OST_SAN_WRITE:
-                CDEBUG(D_INODE, "san write\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_BRW_NET, out_free_oti, rc = 0);
-                rc = ost_san_brw(req, OBD_BRW_WRITE);
-                /* ost_san_brw sends its own replies */
-                GOTO(out_free_oti, rc);
+                /* ost_brw_read sends its own replies */
+                RETURN(rc);
         case OST_PUNCH:
                 CDEBUG(D_INODE, "punch\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_PUNCH_NET, out_free_oti, rc = 0);
-                OBD_FAIL_GOTO(OBD_FAIL_OST_EROFS, out_check_req, rc = -EROFS);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_PUNCH_NET))
+                        RETURN(0);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
+                        GOTO(out, rc = -EROFS);
                 rc = ost_punch(req->rq_export, req, oti);
                 break;
         case OST_STATFS:
                 CDEBUG(D_INODE, "statfs\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_STATFS_NET, out_free_oti, rc = 0);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_NET))
+                        RETURN(0);
                 rc = ost_statfs(req);
                 break;
         case OST_SYNC:
                 CDEBUG(D_INODE, "sync\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OST_SYNC_NET, out_free_oti, rc = 0);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_SYNC_NET))
+                        RETURN(0);
                 rc = ost_sync(req->rq_export, req);
                 break;
         case OST_SET_INFO:
                 DEBUG_REQ(D_INODE, req, "set_info");
+                req_capsule_set(&req->rq_pill, &RQF_OST_SET_INFO);
                 rc = ost_set_info(req->rq_export, req);
                 break;
         case OST_GET_INFO:
                 DEBUG_REQ(D_INODE, req, "get_info");
                 rc = ost_get_info(req->rq_export, req);
                 break;
+        case OST_QUOTACHECK:
+                CDEBUG(D_INODE, "quotacheck\n");
+                req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACHECK);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACHECK_NET))
+                        RETURN(0);
+                rc = ost_handle_quotacheck(req);
+                break;
+        case OST_QUOTACTL:
+                CDEBUG(D_INODE, "quotactl\n");
+                req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACTL);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACTL_NET))
+                        RETURN(0);
+                rc = ost_handle_quotactl(req);
+                break;
         case OBD_PING:
                 DEBUG_REQ(D_INODE, req, "ping");
+                req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
                 rc = target_handle_ping(req);
                 break;
         /* FIXME - just reply status */
         case LLOG_ORIGIN_CONNECT:
-                DEBUG_REQ(D_INODE, req, "log connect\n");
-                rc = ost_llog_handle_connect(req->rq_export, req); 
+                DEBUG_REQ(D_INODE, req, "log connect");
+                req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_CONNECT);
+                rc = ost_llog_handle_connect(req->rq_export, req);
                 req->rq_status = rc;
-                rc = lustre_pack_reply(req, 0, NULL, NULL);
+                rc = req_capsule_server_pack(&req->rq_pill);
                 if (rc)
-                        GOTO(out_free_oti, rc);
-                GOTO(out_free_oti, rc = ptlrpc_reply(req));
+                        RETURN(rc);
+                RETURN(ptlrpc_reply(req));
         case OBD_LOG_CANCEL:
                 CDEBUG(D_INODE, "log cancel\n");
-                OBD_FAIL_GOTO(OBD_FAIL_OBD_LOG_CANCEL_NET, out_free_oti, rc = 0);
+                req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
+                        RETURN(0);
                 rc = llog_origin_handle_cancel(req);
+                if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
+                        RETURN(0);
                 req->rq_status = rc;
-                rc = lustre_pack_reply(req, 0, NULL, NULL);
+                rc = req_capsule_server_pack(&req->rq_pill);
                 if (rc)
-                        GOTO(out_free_oti, rc);
-                GOTO(out_free_oti, rc = ptlrpc_reply(req));
+                        RETURN(rc);
+                RETURN(ptlrpc_reply(req));
         case LDLM_ENQUEUE:
                 CDEBUG(D_INODE, "enqueue\n");
-                OBD_FAIL_GOTO(OBD_FAIL_LDLM_ENQUEUE, out_free_oti, rc = 0);
+                req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
+                if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE))
+                        RETURN(0);
                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
                                          ldlm_server_blocking_ast,
                                          ldlm_server_glimpse_ast);
@@ -1205,12 +1652,16 @@ int ost_handle(struct ptlrpc_request *req)
                 break;
         case LDLM_CONVERT:
                 CDEBUG(D_INODE, "convert\n");
-                OBD_FAIL_GOTO(OBD_FAIL_LDLM_CONVERT, out_free_oti, rc = 0);
+                req_capsule_set(&req->rq_pill, &RQF_LDLM_CONVERT);
+                if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CONVERT))
+                        RETURN(0);
                 rc = ldlm_handle_convert(req);
                 break;
         case LDLM_CANCEL:
                 CDEBUG(D_INODE, "cancel\n");
-                OBD_FAIL_GOTO(OBD_FAIL_LDLM_CANCEL, out_free_oti, rc = 0);
+                req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
+                if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
+                        RETURN(0);
                 rc = ldlm_handle_cancel(req);
                 break;
         case LDLM_BL_CALLBACK:
@@ -1219,60 +1670,83 @@ int ost_handle(struct ptlrpc_request *req)
                 CERROR("callbacks should not happen on OST\n");
                 /* fall through */
         default:
-                CERROR("Unexpected opcode %d\n", req->rq_reqmsg->opc);
+                CERROR("Unexpected opcode %d\n",
+                       lustre_msg_get_opc(req->rq_reqmsg));
                 req->rq_status = -ENOTSUPP;
                 rc = ptlrpc_error(req);
-                GOTO(out_free_oti, rc);
+                RETURN(rc);
         }
 
         LASSERT(current->journal_info == NULL);
 
         EXIT;
         /* If we're DISCONNECTing, the export_data is already freed */
-        if (!rc && req->rq_reqmsg->opc != OST_DISCONNECT) {
-                if (!obd->obd_no_transno) {
-                        req->rq_repmsg->last_committed =
-                                obd->obd_last_committed;
-                } else {
-                        DEBUG_REQ(D_IOCTL, req,
-                                  "not sending last_committed update");
-                }
-                CDEBUG(D_INFO, "last_committed "LPU64", xid "LPX64"\n",
-                       obd->obd_last_committed, req->rq_xid);
-        }
-
-out_check_req:
+        if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
+                target_committed_to_req(req);
 
+out:
         if (!rc)
                 oti_to_request(oti, req);
+
         target_send_reply(req, rc, fail);
-        rc = 0;
-        
-out_free_oti:
-        if (oti)
-                OBD_FREE(oti, sizeof(*oti));
-        return rc;
+        return 0;
 }
 EXPORT_SYMBOL(ost_handle);
-
-int ost_attach(struct obd_device *dev, obd_count len, void *data)
+/*
+ * free per-thread pool created by ost_thread_init().
+ */
+static void ost_thread_done(struct ptlrpc_thread *thread)
 {
-        struct lprocfs_static_vars lvars;
+        struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
+                                             * Storage */
+
+        ENTRY;
 
-        lprocfs_init_vars(ost,&lvars);
-        return lprocfs_obd_attach(dev, lvars.obd_vars);
+        LASSERT(thread != NULL);
+
+        /*
+         * be prepared to handle partially-initialized pools (because this is
+         * called from ost_thread_init() for cleanup.
+         */
+        tls = thread->t_data;
+        if (tls != NULL) {
+                OBD_FREE_PTR(tls);
+                thread->t_data = NULL;
+        }
+        EXIT;
 }
 
-int ost_detach(struct obd_device *dev)
+/*
+ * initialize per-thread page pool (bug 5137).
+ */
+static int ost_thread_init(struct ptlrpc_thread *thread)
 {
-        return lprocfs_obd_detach(dev);
+        struct ost_thread_local_cache *tls;
+
+        ENTRY;
+
+        LASSERT(thread != NULL);
+        LASSERT(thread->t_data == NULL);
+        LASSERTF(thread->t_id <= OSS_THREADS_MAX, "%u\n", thread->t_id);
+
+        OBD_ALLOC_PTR(tls);
+        if (tls == NULL)
+                RETURN(-ENOMEM);
+        thread->t_data = tls;
+        RETURN(0);
 }
 
-extern struct file_operations ost_stimes_fops;
+#define OST_WATCHDOG_TIMEOUT (obd_timeout * 1000)
 
-static int ost_setup(struct obd_device *obd, obd_count len, void *buf)
+/* Sigh - really, this is an OSS, the _server_, not the _target_ */
+static int ost_setup(struct obd_device *obd, struct lustre_cfg* lcfg)
 {
         struct ost_obd *ost = &obd->u.ost;
+        struct lprocfs_static_vars lvars;
+        int oss_min_threads;
+        int oss_max_threads;
+        int oss_min_create_threads;
+        int oss_max_create_threads;
         int rc;
         ENTRY;
 
@@ -1280,80 +1754,122 @@ static int ost_setup(struct obd_device *obd, obd_count len, void *buf)
         if (rc)
                 RETURN(rc);
 
-        rc = llog_start_commit_thread();
-        if (rc < 0)
-                RETURN(rc);
+        lprocfs_ost_init_vars(&lvars);
+        lprocfs_obd_setup(obd, lvars.obd_vars);
+
+        sema_init(&ost->ost_health_sem, 1);
 
-        lprocfs_obd_seq_create(obd, "service_times", 0444, &ost_stimes_fops,
-                               obd);
+        if (oss_num_threads) {
+                /* If oss_num_threads is set, it is the min and the max. */
+                if (oss_num_threads > OSS_THREADS_MAX)
+                        oss_num_threads = OSS_THREADS_MAX;
+                if (oss_num_threads < OSS_THREADS_MIN)
+                        oss_num_threads = OSS_THREADS_MIN;
+                oss_max_threads = oss_min_threads = oss_num_threads;
+        } else {
+                /* Base min threads on memory and cpus */
+                oss_min_threads = num_possible_cpus() * num_physpages >>
+                        (27 - CFS_PAGE_SHIFT);
+                if (oss_min_threads < OSS_THREADS_MIN)
+                        oss_min_threads = OSS_THREADS_MIN;
+                /* Insure a 4x range for dynamic threads */
+                if (oss_min_threads > OSS_THREADS_MAX / 4)
+                        oss_min_threads = OSS_THREADS_MAX / 4;
+                oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4);
+        }
 
         ost->ost_service =
                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
-                                OST_REQUEST_PORTAL, OSC_REPLY_PORTAL, 30000,
-                                ost_handle, "ost",
-                                obd->obd_proc_entry);
+                                OST_MAXREPSIZE, OST_REQUEST_PORTAL,
+                                OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
+                                ost_handle, LUSTRE_OSS_NAME,
+                                obd->obd_proc_entry, target_print_req,
+                                oss_min_threads, oss_max_threads,
+                                "ll_ost", LCT_DT_THREAD);
         if (ost->ost_service == NULL) {
                 CERROR("failed to start service\n");
-                RETURN(-ENOMEM);
+                GOTO(out_lprocfs, rc = -ENOMEM);
         }
 
-        rc = ptlrpc_start_n_threads(obd, ost->ost_service, OST_NUM_THREADS,
-                                    "ll_ost");
+        rc = ptlrpc_start_threads(obd, ost->ost_service);
         if (rc)
                 GOTO(out_service, rc = -EINVAL);
 
+        if (oss_num_create_threads) {
+                if (oss_num_create_threads > OSS_MAX_CREATE_THREADS)
+                        oss_num_create_threads = OSS_MAX_CREATE_THREADS;
+                if (oss_num_create_threads < OSS_MIN_CREATE_THREADS)
+                        oss_num_create_threads = OSS_MIN_CREATE_THREADS;
+                oss_min_create_threads = oss_max_create_threads =
+                        oss_num_create_threads;
+        } else {
+                oss_min_create_threads = OSS_MIN_CREATE_THREADS;
+                oss_max_create_threads = OSS_MAX_CREATE_THREADS;
+        }
+
         ost->ost_create_service =
                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
-                                OST_CREATE_PORTAL, OSC_REPLY_PORTAL, 30000,
+                                OST_MAXREPSIZE, OST_CREATE_PORTAL,
+                                OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
                                 ost_handle, "ost_create",
-                                obd->obd_proc_entry);
+                                obd->obd_proc_entry, target_print_req,
+                                oss_min_create_threads, oss_max_create_threads,
+                                "ll_ost_creat", LCT_DT_THREAD);
         if (ost->ost_create_service == NULL) {
                 CERROR("failed to start OST create service\n");
                 GOTO(out_service, rc = -ENOMEM);
         }
 
-
-        spin_lock_init(&ost->ost_lock);
-        ost->ost_service->srv_obddev = obd;
-        
-        rc = ptlrpc_start_n_threads(obd, ost->ost_create_service, 1,
-                                    "ll_ost_creat");
+        rc = ptlrpc_start_threads(obd, ost->ost_create_service);
         if (rc)
                 GOTO(out_create, rc = -EINVAL);
 
-        ost->ost_destroy_service =
+        ost->ost_io_service =
                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
-                                OST_DESTROY_PORTAL, OSC_REPLY_PORTAL, 30000,
-                                ost_handle, "ost_destroy",
-                                obd->obd_proc_entry);
-        if (ost->ost_destroy_service == NULL) {
-                CERROR("failed to start service\n");
+                                OST_MAXREPSIZE, OST_IO_PORTAL,
+                                OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
+                                ost_handle, "ost_io",
+                                obd->obd_proc_entry, target_print_req,
+                                oss_min_threads, oss_max_threads,
+                                "ll_ost_io", LCT_DT_THREAD);
+        if (ost->ost_io_service == NULL) {
+                CERROR("failed to start OST I/O service\n");
                 GOTO(out_create, rc = -ENOMEM);
         }
 
-        rc = ptlrpc_start_n_threads(obd, ost->ost_destroy_service,
-                                    OST_NUM_THREADS, "ll_dstr_ost");
+        ost->ost_io_service->srv_init = ost_thread_init;
+        ost->ost_io_service->srv_done = ost_thread_done;
+        ost->ost_io_service->srv_cpu_affinity = 1;
+        rc = ptlrpc_start_threads(obd, ost->ost_io_service);
         if (rc)
-                GOTO(out_destroy, rc = -EINVAL);
+                GOTO(out_io, rc = -EINVAL);
+
+        ping_evictor_start();
 
         RETURN(0);
 
-out_destroy:
-        ptlrpc_unregister_service(ost->ost_destroy_service);
+out_io:
+        ptlrpc_unregister_service(ost->ost_io_service);
+        ost->ost_io_service = NULL;
 out_create:
         ptlrpc_unregister_service(ost->ost_create_service);
+        ost->ost_create_service = NULL;
 out_service:
         ptlrpc_unregister_service(ost->ost_service);
+        ost->ost_service = NULL;
+out_lprocfs:
+        lprocfs_obd_cleanup(obd);
         RETURN(rc);
 }
 
-extern void lgss_svc_cache_purge_all(void);
-static int ost_cleanup(struct obd_device *obd, int flags)
+static int ost_cleanup(struct obd_device *obd)
 {
         struct ost_obd *ost = &obd->u.ost;
         int err = 0;
         ENTRY;
 
+        ping_evictor_stop();
+
         spin_lock_bh(&obd->obd_processing_task_lock);
         if (obd->obd_recovering) {
                 target_cancel_recovery_timer(obd);
@@ -1361,47 +1877,80 @@ static int ost_cleanup(struct obd_device *obd, int flags)
         }
         spin_unlock_bh(&obd->obd_processing_task_lock);
 
-        ptlrpc_stop_all_threads(ost->ost_service);
+        down(&ost->ost_health_sem);
         ptlrpc_unregister_service(ost->ost_service);
-
-        ptlrpc_stop_all_threads(ost->ost_create_service);
         ptlrpc_unregister_service(ost->ost_create_service);
+        ptlrpc_unregister_service(ost->ost_io_service);
+        ost->ost_service = NULL;
+        ost->ost_create_service = NULL;
+        up(&ost->ost_health_sem);
 
-        ptlrpc_stop_all_threads(ost->ost_destroy_service);
-        ptlrpc_unregister_service(ost->ost_destroy_service);
+        lprocfs_obd_cleanup(obd);
 
-#ifdef ENABLE_GSS
-        /* XXX */
-        lgss_svc_cache_purge_all();
-#endif
         RETURN(err);
 }
 
+static int ost_health_check(struct obd_device *obd)
+{
+        struct ost_obd *ost = &obd->u.ost;
+        int rc = 0;
+
+        down(&ost->ost_health_sem);
+        rc |= ptlrpc_service_health_check(ost->ost_service);
+        rc |= ptlrpc_service_health_check(ost->ost_create_service);
+        rc |= ptlrpc_service_health_check(ost->ost_io_service);
+        up(&ost->ost_health_sem);
+
+        /*
+         * health_check to return 0 on healthy
+         * and 1 on unhealthy.
+         */
+        if( rc != 0)
+                rc = 1;
+
+        return rc;
+}
+
+struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
+{
+        return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
+}
+
 /* use obd ops to offer management infrastructure */
 static struct obd_ops ost_obd_ops = {
         .o_owner        = THIS_MODULE,
-        .o_attach       = ost_attach,
-        .o_detach       = ost_detach,
         .o_setup        = ost_setup,
         .o_cleanup      = ost_cleanup,
+        .o_health_check = ost_health_check,
 };
 
+
 static int __init ost_init(void)
 {
         struct lprocfs_static_vars lvars;
+        int rc;
         ENTRY;
 
-        lprocfs_init_vars(ost,&lvars);
-        RETURN(class_register_type(&ost_obd_ops, NULL, lvars.module_vars,
-                                   OBD_OST_DEVICENAME));
+        lprocfs_ost_init_vars(&lvars);
+        rc = class_register_type(&ost_obd_ops, NULL, lvars.module_vars,
+                                 LUSTRE_OSS_NAME, NULL);
+
+        if (ost_num_threads != 0 && oss_num_threads == 0) {
+                LCONSOLE_INFO("ost_num_threads module parameter is deprecated, "
+                              "use oss_num_threads instead or unset both for "
+                              "dynamic thread startup\n");
+                oss_num_threads = ost_num_threads;
+        }
+
+        RETURN(rc);
 }
 
 static void /*__exit*/ ost_exit(void)
 {
-        class_unregister_type(OBD_OST_DEVICENAME);
+        class_unregister_type(LUSTRE_OSS_NAME);
 }
 
-MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
+MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
 MODULE_LICENSE("GPL");