Whamcloud - gitweb
b=14230
[fs/lustre-release.git] / lustre / mds / mds_open.c
index b2d4416..61dc455 100644 (file)
@@ -7,20 +7,23 @@
  *   Author: Phil Schwan <phil@clusterfs.com>
  *   Author: Mike Shaver <shaver@clusterfs.com>
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ *   This file is part of the Lustre file system, http://www.lustre.org
+ *   Lustre is a trademark of Cluster File Systems, Inc.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ *   You may have signed or agreed to another license before downloading
+ *   this software.  If so, you are bound by the terms and conditions
+ *   of that agreement, and the following does not apply to you.  See the
+ *   LICENSE file included with this distribution for more information.
  *
- *   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.
+ *   If you did not agree to a different license, then this copy of Lustre
+ *   is open source software; you can redistribute it and/or modify it
+ *   under the terms of version 2 of the GNU General Public License as
+ *   published by the Free Software Foundation.
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *   In either case, 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
+ *   license text for more details.
  */
 
 #ifndef EXPORT_SYMTAB
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/version.h>
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
-# include <linux/buffer_head.h>
-# include <linux/workqueue.h>
-#else
-# include <linux/locks.h>
-#endif
+#include <linux/buffer_head.h>
+#include <linux/workqueue.h>
 
-#include <linux/obd_class.h>
-#include <linux/obd_lov.h>
-#include <linux/lustre_fsfilt.h>
-#include <linux/lprocfs_status.h>
-#include <linux/lustre_gs.h>
+#include <obd_class.h>
+#include <obd_lov.h>
+#include <lustre_fsfilt.h>
+#include <lprocfs_status.h>
 
 #include "mds_internal.h"
 
@@ -69,6 +67,9 @@ static void mds_mfd_addref(void *mfdp)
                atomic_read(&mfd->mfd_refcount));
 }
 
+/* Create a new mds_file_data struct.
+ * One reference for handle+med_open_head list and dropped by mds_mfd_unlink(),
+ * one reference for the caller of this function. */
 struct mds_file_data *mds_mfd_new(void)
 {
         struct mds_file_data *mfd;
@@ -81,43 +82,50 @@ struct mds_file_data *mds_mfd_new(void)
 
         atomic_set(&mfd->mfd_refcount, 2);
 
-        INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
+        CFS_INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
+        CFS_INIT_LIST_HEAD(&mfd->mfd_list);
         class_handle_hash(&mfd->mfd_handle, mds_mfd_addref);
 
         return mfd;
 }
 
-struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
+/* Get a new reference on the mfd pointed to by handle, if handle is still
+ * valid.  Caller must drop reference with mds_mfd_put(). */
+static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
 {
         ENTRY;
         LASSERT(handle != NULL);
         RETURN(class_handle2object(handle->cookie));
 }
 
-void mds_mfd_put(struct mds_file_data *mfd)
+/* Drop mfd reference, freeing struct if this is the last one. */
+static void mds_mfd_put(struct mds_file_data *mfd)
 {
         CDEBUG(D_INFO, "PUTting mfd %p : new refcount %d\n", mfd,
                atomic_read(&mfd->mfd_refcount) - 1);
         LASSERT(atomic_read(&mfd->mfd_refcount) > 0 &&
                 atomic_read(&mfd->mfd_refcount) < 0x5a5a);
         if (atomic_dec_and_test(&mfd->mfd_refcount)) {
-                LASSERT(list_empty(&mfd->mfd_handle.h_link));
-                OBD_FREE(mfd, sizeof *mfd);
+                OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
         }
 }
 
-static void mds_mfd_destroy(struct mds_file_data *mfd)
+/* Remove the mfd handle so that it cannot be found by open/close again.
+ * Caller must hold med_open_lock for mfd_list manipulation. */
+void mds_mfd_unlink(struct mds_file_data *mfd, int decref)
 {
         class_handle_unhash(&mfd->mfd_handle);
-        mds_mfd_put(mfd);
+        list_del_init(&mfd->mfd_list);
+        if (decref)
+                mds_mfd_put(mfd);
 }
 
 /* Caller must hold mds->mds_epoch_sem */
 static int mds_alloc_filterdata(struct inode *inode)
 {
-        LASSERT(LUSTRE_FILTERDATA(inode) == NULL);
-        OBD_ALLOC(LUSTRE_FILTERDATA(inode), sizeof(struct mds_filter_data));
-        if (LUSTRE_FILTERDATA(inode) == NULL)
+        LASSERT(inode->i_filterdata == NULL);
+        OBD_ALLOC(inode->i_filterdata, sizeof(struct mds_filter_data));
+        if (inode->i_filterdata == NULL)
                 return -ENOMEM;
         LASSERT(igrab(inode) == inode);
         return 0;
@@ -126,9 +134,9 @@ static int mds_alloc_filterdata(struct inode *inode)
 /* Caller must hold mds->mds_epoch_sem */
 static void mds_free_filterdata(struct inode *inode)
 {
-        LASSERT(LUSTRE_FILTERDATA(inode) != NULL);
-        OBD_FREE(LUSTRE_FILTERDATA(inode), sizeof(struct mds_filter_data));
-        LUSTRE_FILTERDATA(inode) = NULL;
+        LASSERT(inode->i_filterdata != NULL);
+        OBD_FREE(inode->i_filterdata, sizeof(struct mds_filter_data));
+        inode->i_filterdata = NULL;
         iput(inode);
 }
 
@@ -155,6 +163,7 @@ static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
                 RETURN(-ETXTBSY);
         }
 
+
         if (MDS_FILTERDATA(inode) && MDS_FILTERDATA(inode)->io_epoch != 0) {
                 CDEBUG(D_INODE, "continuing MDS epoch "LPU64" for ino %lu/%u\n",
                        MDS_FILTERDATA(inode)->io_epoch, inode->i_ino,
@@ -162,22 +171,19 @@ static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
                 goto out;
         }
 
-        if (MDS_FILTERDATA(inode) == NULL)
+        if (inode->i_filterdata == NULL)
                 mds_alloc_filterdata(inode);
-        if (MDS_FILTERDATA(inode) == NULL) {
+        if (inode->i_filterdata == NULL) {
                 rc = -ENOMEM;
                 goto out;
         }
-        if (epoch > mds->mds_io_epoch) {
+        if (epoch > mds->mds_io_epoch)
                 mds->mds_io_epoch = epoch;
-                CDEBUG(D_INODE, "repair MDS epoch "LPU64" for ino %lu/%u\n",
-                       mds->mds_io_epoch, inode->i_ino, inode->i_generation);
-        } else {
+        else
                 mds->mds_io_epoch++;
-                CDEBUG(D_INODE, "starting MDS epoch "LPU64" for ino %lu/%u\n",
-                       mds->mds_io_epoch, inode->i_ino, inode->i_generation);
-        }
         MDS_FILTERDATA(inode)->io_epoch = mds->mds_io_epoch;
+        CDEBUG(D_INODE, "starting MDS epoch "LPU64" for ino %lu/%u\n",
+               mds->mds_io_epoch, inode->i_ino, inode->i_generation);
  out:
         if (rc == 0)
                 atomic_inc(&inode->i_writecount);
@@ -237,44 +243,41 @@ int mds_query_write_access(struct inode *inode)
 /* This replaces the VFS dentry_open, it manages mfd and writecount */
 static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
                                              struct vfsmount *mnt, int flags,
-                                             struct ptlrpc_request *req,
-                                             struct mds_update_record *rec)
+                                             struct ptlrpc_request *req)
 {
         struct mds_export_data *med = &req->rq_export->exp_mds_data;
         struct mds_obd *mds = mds_req2mds(req);
         struct mds_file_data *mfd;
         struct mds_body *body;
-        int rc = 0;
+        int error;
         ENTRY;
 
         mfd = mds_mfd_new();
         if (mfd == NULL) {
                 CERROR("mds: out of memory\n");
-                GOTO(cleanup_dentry, rc = -ENOMEM);
+                GOTO(cleanup_dentry, error = -ENOMEM);
         }
 
-        body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
+        body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
 
         if (flags & FMODE_WRITE) {
                 /* FIXME: in recovery, need to pass old epoch here */
-                rc = mds_get_write_access(mds, dentry->d_inode, rec->ur_ioepoch);
-                if (rc)
-                        GOTO(cleanup_mfd, rc);
+                error = mds_get_write_access(mds, dentry->d_inode, 0);
+                if (error)
+                        GOTO(cleanup_mfd, error);
                 body->io_epoch = MDS_FILTERDATA(dentry->d_inode)->io_epoch;
-        } else if (flags & FMODE_EXEC) {
-                rc = mds_deny_write_access(mds, dentry->d_inode);
-                if (rc)
-                        GOTO(cleanup_mfd, rc);
+        } else if (flags & MDS_FMODE_EXEC) {
+                error = mds_deny_write_access(mds, dentry->d_inode);
+                if (error)
+                        GOTO(cleanup_mfd, error);
         }
 
         dget(dentry);
 
-        /* FIXME: invalidate update locks when first open for write comes in */
-
-        /* mark the file as open to handle open-unlink. */
-        DOWN_WRITE_I_ALLOC_SEM(dentry->d_inode);
+        /* Mark the file as open to handle open-unlink. */
+        MDS_DOWN_WRITE_ORPHAN_SEM(dentry->d_inode);
         mds_orphan_open_inc(dentry->d_inode);
-        UP_WRITE_I_ALLOC_SEM(dentry->d_inode);
+        MDS_UP_WRITE_ORPHAN_SEM(dentry->d_inode);
 
         mfd->mfd_mode = flags;
         mfd->mfd_dentry = dentry;
@@ -283,119 +286,100 @@ static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
         spin_lock(&med->med_open_lock);
         list_add(&mfd->mfd_list, &med->med_open_head);
         spin_unlock(&med->med_open_lock);
-        mds_mfd_put(mfd);
 
         body->handle.cookie = mfd->mfd_handle.h_cookie;
+
         RETURN(mfd);
+
 cleanup_mfd:
         mds_mfd_put(mfd);
-        mds_mfd_destroy(mfd);
+        mds_mfd_unlink(mfd, 1);
 cleanup_dentry:
-        return ERR_PTR(rc);
-}
-
-static inline void
-mds_objids_from_lmm(obd_id *ids, struct lov_mds_md *lmm,
-                    struct lov_desc *desc)
-{
-        int i;
-        
-        for (i = 0; i < le32_to_cpu(lmm->lmm_stripe_count); i++) {
-                ids[le32_to_cpu(lmm->lmm_objects[i].l_ost_idx)] =
-                        le64_to_cpu(lmm->lmm_objects[i].l_object_id);
-        }
+        return ERR_PTR(error);
 }
 
-/* must be called with i_sem held */
-int
-mds_create_objects(struct obd_device *obd, struct ptlrpc_request *req,
-                   int offset, struct mds_update_record *rec,
-                   struct dentry *dchild, void **handle,
-                   obd_id **ids)
+/* Must be called with i_mutex held */
+static int mds_create_objects(struct ptlrpc_request *req, int offset,
+                              struct mds_update_record *rec,
+                              struct mds_obd *mds, struct obd_device *obd,
+                              struct dentry *dchild, void **handle,
+                              struct lov_mds_md **objid)
 {
         struct inode *inode = dchild->d_inode;
-        struct mds_obd *mds = &obd->u.mds;
         struct obd_trans_info oti = { 0 };
-        struct lov_stripe_md *lsm = NULL;
         struct lov_mds_md *lmm = NULL;
-        int rc, lmm_bufsize, lmm_size;
-        struct obdo *oa = NULL;
+        int rc, lmm_size;
         struct mds_body *body;
+        struct obd_info oinfo = { { { 0 } } };
         void *lmm_buf;
         ENTRY;
 
-        if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
-            !(rec->ur_flags & FMODE_WRITE))
+        if (!S_ISREG(inode->i_mode))
+                RETURN(0);
+        if (!md_should_create(rec->ur_flags))
                 RETURN(0);
 
-        body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
+        body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
 
-        if (!S_ISREG(inode->i_mode))
-                RETURN(0);
         if (body->valid & OBD_MD_FLEASIZE)
                 RETURN(0);
 
-        OBD_ALLOC(*ids, mds->mds_dt_desc.ld_tgt_count * sizeof(**ids));
-        if (*ids == NULL)
-                RETURN(-ENOMEM);
-        oti.oti_objid = *ids;
-                
+        oti_init(&oti, req);
+
         /* replay case */
         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
-                LASSERT(id_ino(rec->ur_id2));
+                if (rec->ur_fid2->id == 0) {
+                        DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
+                        RETURN(-EFAULT);
+                }
+
                 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
                 lmm_size = rec->ur_eadatalen;
                 lmm = rec->ur_eadata;
                 LASSERT(lmm);
 
                 if (*handle == NULL)
-                        *handle = fsfilt_start(obd, inode, FSFILT_OP_CREATE, NULL);
+                        *handle = fsfilt_start(obd,inode,FSFILT_OP_CREATE,NULL);
                 if (IS_ERR(*handle)) {
                         rc = PTR_ERR(*handle);
                         *handle = NULL;
-                        RETURN(rc);
+                        GOTO(out_ids, rc);
                 }
 
-                mds_objids_from_lmm(*ids, lmm, &mds->mds_dt_desc);
-
-                lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, 0);
-                lmm_bufsize = req->rq_repmsg->buflens[offset];
-                LASSERT(lmm_buf != NULL);
-                LASSERT(lmm_bufsize >= lmm_size);
-
-                memcpy(lmm_buf, lmm, lmm_size);
-                rc = fsfilt_set_md(obd, inode, *handle, lmm,
-                                   lmm_size, EA_LOV);
+                rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
                 if (rc)
                         CERROR("open replay failed to set md:%d\n", rc);
-                RETURN(0);
+                lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, lmm_size);
+                LASSERT(lmm_buf);
+                memcpy(lmm_buf, lmm, lmm_size);
+
+                *objid = lmm_buf;
+                RETURN(rc);
         }
 
-        if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_ALLOC_OBDO))
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
                 GOTO(out_ids, rc = -ENOMEM);
 
-        oa = obdo_alloc();
-        if (oa == NULL)
-                RETURN(-ENOMEM);
-        oa->o_mode = S_IFREG | 0600;
-        oa->o_id = inode->i_ino;
-        oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
-        oa->o_generation = inode->i_generation;
-        oa->o_uid = 0; /* must have 0 uid / gid on OST */
-        oa->o_gid = 0;
-        oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
-                        OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLGROUP;
-        oa->o_size = 0;
-
-        obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
+        OBDO_ALLOC(oinfo.oi_oa);
+        if (oinfo.oi_oa == NULL)
+                GOTO(out_ids, rc = -ENOMEM);
+        oinfo.oi_oa->o_uid = 0; /* must have 0 uid / gid on OST */
+        oinfo.oi_oa->o_gid = 0;
+        oinfo.oi_oa->o_mode = S_IFREG | 0600;
+        oinfo.oi_oa->o_id = inode->i_ino;
+        oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLFLAGS |
+                OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID;
+        oinfo.oi_oa->o_size = 0;
+
+        obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
                         OBD_MD_FLMTIME | OBD_MD_FLCTIME);
 
         if (!(rec->ur_flags & MDS_OPEN_HAS_OBJS)) {
                 /* check if things like lfs setstripe are sending us the ea */
                 if (rec->ur_flags & MDS_OPEN_HAS_EA) {
                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
-                                           mds->mds_dt_exp,
-                                           0, &lsm, rec->ur_eadata);
+                                           mds->mds_osc_exp,
+                                           0, &oinfo.oi_md, rec->ur_eadata);
                         if (rc)
                                 GOTO(out_oa, rc);
                 } else {
@@ -405,24 +389,23 @@ mds_create_objects(struct obd_device *obd, struct ptlrpc_request *req,
 
                         lmm_size = mds->mds_max_mdsize;
                         rc = mds_get_md(obd, dchild->d_parent->d_inode,
-                                        lmm, &lmm_size, 1, 0);
+                                        lmm, &lmm_size, 1);
                         if (rc > 0)
                                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
-                                                   mds->mds_dt_exp,
-                                                   0, &lsm, lmm);
+                                                   mds->mds_osc_exp,
+                                                   0, &oinfo.oi_md, lmm);
                         OBD_FREE(lmm, mds->mds_max_mdsize);
                         if (rc)
                                 GOTO(out_oa, rc);
                 }
-
-                LASSERT(oa->o_gr >= FILTER_GROUP_FIRST_MDS);
-                oti.oti_flags |= OBD_MODE_CROW;
-                rc = obd_create(mds->mds_dt_exp, oa, NULL, 0, &lsm, &oti);
-
+                rc = obd_create(mds->mds_osc_exp, oinfo.oi_oa,
+                                &oinfo.oi_md, &oti);
                 if (rc) {
-                        CDEBUG((rc == -ENOSPC ? D_INODE : D_ERROR),
-                               "error creating objects for "
-                               "inode %lu: rc = %d\n",
+                        int level = D_ERROR;
+                        if (rc == -ENOSPC)
+                                level = D_INODE;
+                        CDEBUG(level, "error creating objects for "
+                                      "inode %lu: rc = %d\n",
                                inode->i_ino, rc);
                         if (rc > 0) {
                                 CERROR("obd_create returned invalid "
@@ -432,29 +415,32 @@ mds_create_objects(struct obd_device *obd, struct ptlrpc_request *req,
                         GOTO(out_oa, rc);
                 }
         } else {
-                rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_dt_exp,
-                                   0, &lsm, rec->ur_eadata);
-                if (rc)
+                rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_osc_exp,
+                                   0, &oinfo.oi_md, rec->ur_eadata);
+                if (rc) {
                         GOTO(out_oa, rc);
-
-                lsm->lsm_object_id = oa->o_id;
-                lsm->lsm_object_gr = oa->o_gr;
+                }
+                oinfo.oi_md->lsm_object_id = oinfo.oi_oa->o_id;
+                oinfo.oi_md->lsm_object_gr = oinfo.oi_oa->o_gr;
         }
-        if (inode->i_size) {
-                oa->o_size = inode->i_size;
-                obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
-                                OBD_MD_FLMTIME | OBD_MD_FLCTIME | OBD_MD_FLSIZE);
-                
-                /* pack lustre id to oss */
-                *(obdo_id(oa)) = body->id1;
-                oa->o_valid |= OBD_MD_FLIFID;
-
-                rc = obd_setattr(mds->mds_dt_exp, oa, lsm, &oti, NULL);
+        if (i_size_read(inode)) {
+                oinfo.oi_oa->o_size = i_size_read(inode);
+                obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE |
+                                OBD_MD_FLATIME | OBD_MD_FLMTIME |
+                                OBD_MD_FLCTIME | OBD_MD_FLSIZE);
+
+                /* pack lustre id to OST */
+                oinfo.oi_oa->o_fid = body->fid1.id;
+                oinfo.oi_oa->o_generation = body->fid1.generation;
+                oinfo.oi_oa->o_valid |= OBD_MD_FLFID | OBD_MD_FLGENER;
+
+                rc = obd_setattr_rqset(mds->mds_osc_exp, &oinfo, &oti);
                 if (rc) {
                         CERROR("error setting attrs for inode %lu: rc %d\n",
                                inode->i_ino, rc);
                         if (rc > 0) {
-                                CERROR("obd_setattr returned bad rc %d\n", rc);
+                                CERROR("obd_setattr_async returned bad rc %d\n",
+                                       rc);
                                 rc = -EIO;
                         }
                         GOTO(out_oa, rc);
@@ -462,18 +448,15 @@ mds_create_objects(struct obd_device *obd, struct ptlrpc_request *req,
         }
 
         body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
-        obdo_refresh_inode(inode, oa, OBD_MD_FLBLKSZ);
+        obdo_refresh_inode(inode, oinfo.oi_oa, OBD_MD_FLBLKSZ);
 
-        LASSERT(lsm && lsm->lsm_object_id);
+        LASSERT(oinfo.oi_md && oinfo.oi_md->lsm_object_id);
         lmm = NULL;
-        rc = obd_packmd(mds->mds_dt_exp, &lmm, lsm);
-        if (!id_ino(rec->ur_id2))
-                obd_free_memmd(mds->mds_dt_exp, &lsm);
+        rc = obd_packmd(mds->mds_osc_exp, &lmm, oinfo.oi_md);
         if (rc < 0) {
                 CERROR("cannot pack lsm, err = %d\n", rc);
                 GOTO(out_oa, rc);
         }
-
         lmm_size = rc;
         body->eadatasize = rc;
 
@@ -482,82 +465,27 @@ mds_create_objects(struct obd_device *obd, struct ptlrpc_request *req,
         if (IS_ERR(*handle)) {
                 rc = PTR_ERR(*handle);
                 *handle = NULL;
-                GOTO(out_oa, rc);
+                GOTO(free_diskmd, rc);
         }
 
-        rc = fsfilt_set_md(obd, inode, *handle, lmm,
-                           lmm_size, EA_LOV);
-        
-        lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, 0);
-        lmm_bufsize = req->rq_repmsg->buflens[offset];
+        rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
+        lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, lmm_size);
         LASSERT(lmm_buf);
-        LASSERT(lmm_bufsize >= lmm_size);
-
         memcpy(lmm_buf, lmm, lmm_size);
-        obd_free_diskmd(mds->mds_dt_exp, &lmm);
+
+        *objid = lmm_buf; // save for mds_lov_update_objid
+
+free_diskmd:
+        obd_free_diskmd(mds->mds_osc_exp, &lmm);
 out_oa:
         oti_free_cookies(&oti);
-        obdo_free(oa);
+        OBDO_FREE(oinfo.oi_oa);
 out_ids:
-        if (rc) {
-                OBD_FREE(*ids, mds->mds_dt_desc.ld_tgt_count * sizeof(**ids));
-                *ids = NULL;
-        }
-        if (lsm)
-                obd_free_memmd(mds->mds_dt_exp, &lsm);
+        if (oinfo.oi_md)
+                obd_free_memmd(mds->mds_osc_exp, &oinfo.oi_md);
         RETURN(rc);
 }
 
-int
-mds_destroy_object(struct obd_device *obd,
-                   struct inode *inode, int async)
-{
-        struct mds_obd *mds = &obd->u.mds;
-        struct lov_mds_md *lmm = NULL;
-        int rc, lmm_size;
-        ENTRY;
-
-        LASSERT(inode != NULL);
-
-        if (inode->i_nlink != 0) {
-                CDEBUG(D_INODE, "attempt to destroy OSS object when "
-                       "i_nlink == %d\n", (int)inode->i_nlink);
-                RETURN(0);
-        }
-        
-        OBD_ALLOC(lmm, mds->mds_max_mdsize);
-        if (lmm == NULL)
-                RETURN(-ENOMEM);
-
-        lmm_size = mds->mds_max_mdsize;
-        rc = mds_get_md(obd, inode, lmm, &lmm_size, 1, 0);
-        if (rc < 0) {
-                CERROR("no stripe info for %lu/%lu inode\n",
-                       (unsigned long)inode->i_ino,
-                       (unsigned long)inode->i_generation);
-                GOTO(out_free_lmm, rc);
-        }
-
-        if (rc > 0) {
-                /* asynchronously unlink objecect on OSS */
-                rc = mds_unlink_object(mds, inode, lmm, lmm_size,
-                                       NULL, 0, async);
-                if (rc) {
-                        CERROR("error unlinking object on OSS, "
-                               "err %d\n", rc);
-                        GOTO(out_free_lmm, rc);
-                }
-        } else {
-                CDEBUG(D_INODE, "no stripping info found for inode "
-                     "%lu/%lu\n", (unsigned long)inode->i_ino, 
-                     (unsigned long)inode->i_generation);
-        }
-        EXIT;
-out_free_lmm:
-        OBD_FREE(lmm, mds->mds_max_mdsize);
-        return rc;
-}
-
 static void reconstruct_open(struct mds_update_record *rec, int offset,
                              struct ptlrpc_request *req,
                              struct lustre_handle *child_lockh)
@@ -567,21 +495,21 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
         struct mds_obd *mds = mds_req2mds(req);
         struct mds_file_data *mfd;
         struct obd_device *obd = req->rq_export->exp_obd;
-        struct dentry *parent = NULL, *dchild;
+        struct dentry *parent, *dchild;
         struct ldlm_reply *rep;
         struct mds_body *body;
+        int rc;
         struct list_head *t;
         int put_child = 1;
-        int rc;
         ENTRY;
 
-        LASSERT(offset == 3); /* only called via intent */
-        rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
-        body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
+        LASSERT(offset == DLM_INTENT_REC_OFF); /* only called via intent */
+        rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
+        body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
 
         /* copy rc, transno and disp; steal locks */
         mds_req_from_mcd(req, mcd);
-        intent_set_disposition(rep, mcd->mcd_last_data);
+        intent_set_disposition(rep, le32_to_cpu(mcd->mcd_last_data));
 
         /* Only replay if create or open actually happened. */
         if (!intent_disposition(rep, DISP_OPEN_CREATE | DISP_OPEN_OPEN) ) {
@@ -589,41 +517,29 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                 return; /* error looking up parent or child */
         }
 
-        /* first, we try to open the file by fid. by the time of this
-         * request, inode can be an orphan and parent can disappear */
-        if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
-                CDEBUG(D_HA, "OPEN by fid "DLID4" (RESENT|REPLAY)\n",
-                       OLID4(rec->ur_id2));
-                dchild = mds_id2dentry(obd, rec->ur_id2, NULL);
-        } else if (rec->ur_namelen == 1) {
-                CDEBUG(D_HA, "OPEN by fid "DLID4" (RESENT)\n",
-                       OLID4(rec->ur_id1));
-                dchild = mds_id2dentry(obd, rec->ur_id1, NULL);
-        } else {
-                parent = mds_id2dentry(obd, rec->ur_id1, NULL);
-                LASSERTF(!IS_ERR(parent), "lid "DLID4" rc %ld\n", 
-                                OLID4(rec->ur_id1), PTR_ERR(parent));
-                dchild = ll_lookup_one_len(rec->ur_name, parent, 
-                                rec->ur_namelen - 1);
-                LASSERTF(!IS_ERR(dchild), "parent "DLID4" child %s rc %ld\n",
-                                OLID4(rec->ur_id1), rec->ur_name, PTR_ERR(dchild));
-        }
+        parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
+        LASSERT(!IS_ERR(parent));
+
+        dchild = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
+        LASSERT(!IS_ERR(dchild));
 
         if (!dchild->d_inode)
                 GOTO(out_dput, 0); /* child not present to open */
 
         /* At this point, we know we have a child. We'll send
-         * it back _unless_ it not created and open failed.  */
+         * it back _unless_ it not created and open failed.
+         */
         if (intent_disposition(rep, DISP_OPEN_OPEN) &&
             !intent_disposition(rep, DISP_OPEN_CREATE) &&
-            req->rq_status)
+            req->rq_status) {
                 GOTO(out_dput, 0);
+        }
 
-        /* get lock (write for O_CREAT, read otherwise) */
-        mds_pack_inode2body(obd, body, dchild->d_inode, 1);
+        mds_pack_inode2fid(&body->fid1, dchild->d_inode);
+        mds_pack_inode2body(body, dchild->d_inode);
         if (S_ISREG(dchild->d_inode->i_mode)) {
-                rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
-                                 dchild->d_inode, 1, 0);
+                rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
+                                 body, dchild->d_inode, 1);
 
                 if (rc)
                         LASSERT(rc == req->rq_status);
@@ -633,7 +549,22 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
         }
-        
+
+        if (!(rec->ur_flags & MDS_OPEN_JOIN_FILE))
+                lustre_shrink_reply(req, DLM_REPLY_REC_OFF + 1,
+                                    body->eadatasize, 0);
+
+        if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
+            !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
+                int acl_off = DLM_REPLY_REC_OFF + (body->eadatasize ? 2 : 1);
+
+                rc = mds_pack_acl(med, dchild->d_inode, req->rq_repmsg,
+                                  body, acl_off);
+                lustre_shrink_reply(req, acl_off, body->aclsize, 0);
+                if (!req->rq_status && rc)
+                        req->rq_status = rc;
+        }
+
         /* If we have -EEXIST as the status, and we were asked to create
          * exclusively, we can tell we failed because the file already existed.
          */
@@ -656,22 +587,31 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                 GOTO(out_dput, 0);
 
         mfd = NULL;
+        spin_lock(&med->med_open_lock);
         list_for_each(t, &med->med_open_head) {
                 mfd = list_entry(t, struct mds_file_data, mfd_list);
-                if (mfd->mfd_xid == req->rq_xid)
+                if (mfd->mfd_xid == req->rq_xid) {
+                        mds_mfd_addref(mfd);
                         break;
+                }
                 mfd = NULL;
         }
+        spin_unlock(&med->med_open_lock);
 
         /* #warning "XXX fixme" bug 2991 */
         /* Here it used to LASSERT(mfd) if exp_outstanding_reply != NULL.
          * Now that exp_outstanding_reply is a list, it's just using mfd != NULL
          * to detect a re-open */
         if (mfd == NULL) {
+                if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
+                        rc = mds_join_file(rec, req, dchild, NULL);
+                        if (rc)
+                                GOTO(out_dput, rc);
+                }
                 mntget(mds->mds_vfsmnt);
                 CERROR("Re-opened file \n");
                 mfd = mds_dentry_open(dchild, mds->mds_vfsmnt,
-                                      rec->ur_flags & ~MDS_OPEN_TRUNC, req, rec);
+                                      rec->ur_flags & ~MDS_OPEN_TRUNC, req);
                 if (!mfd) {
                         CERROR("mds: out of memory\n");
                         GOTO(out_dput, req->rq_status = -ENOMEM);
@@ -683,24 +623,33 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                        mfd->mfd_handle.h_cookie);
         }
 
+        mds_mfd_put(mfd);
+
  out_dput:
         if (put_child)
                 l_dput(dchild);
-        if (parent)
-                l_dput(parent);
+        l_dput(parent);
         EXIT;
 }
 
 /* do NOT or the MAY_*'s, you'll get the weakest */
-int accmode(int flags)
+static int accmode(struct inode *inode, int flags)
 {
         int res = 0;
 
+        /* Sadly, NFSD reopens a file repeatedly during operation, so the
+         * "acc_mode = 0" allowance for newly-created files isn't honoured.
+         * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
+         * owner can write to a file even if it is marked readonly to hide
+         * its brokenness. (bug 5781) */
+        if (flags & MDS_OPEN_OWNEROVERRIDE && inode->i_uid == current->fsuid)
+                return 0;
+
         if (flags & FMODE_READ)
                 res = MAY_READ;
         if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
                 res |= MAY_WRITE;
-        if (flags & FMODE_EXEC)
+        if (flags & MDS_FMODE_EXEC)
                 res = MAY_EXEC;
         return res;
 }
@@ -708,182 +657,138 @@ int accmode(int flags)
 /* Handles object creation, actual opening, and I/O epoch */
 static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
                            struct mds_body *body, int flags, void **handle,
-                           struct mds_update_record *rec, struct ldlm_reply *rep)
+                           struct mds_update_record *rec,struct ldlm_reply *rep,
+                           struct lustre_handle *lockh)
 {
-        struct obd_device *obd = req->rq_export->exp_obd;
         struct mds_obd *mds = mds_req2mds(req);
-        struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
+        struct obd_device *obd = req->rq_export->exp_obd;
         struct mds_file_data *mfd = NULL;
-        obd_id *ids = NULL;
-        unsigned mode;
-        int rc = 0, reply_off;
+        struct lov_mds_md *lmm = NULL; /* object IDs created */
+        int rc = 0;
         ENTRY;
 
         /* atomically create objects if necessary */
-        down(&dchild->d_inode->i_sem);
-        mode = dchild->d_inode->i_mode;
+        LOCK_INODE_MUTEX(dchild->d_inode);
 
-        if ((S_ISREG(mode) && !(body->valid & OBD_MD_FLEASIZE)) || 
-            (S_ISDIR(mode) && !(body->valid & OBD_MD_FLDIREA))) {
-                rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
-                                 dchild->d_inode, 0, 0);
+        if (S_ISREG(dchild->d_inode->i_mode) &&
+            !(body->valid & OBD_MD_FLEASIZE)) {
+                rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
+                                 body, dchild->d_inode, 0);
                 if (rc) {
-                        up(&dchild->d_inode->i_sem);
+                        UNLOCK_INODE_MUTEX(dchild->d_inode);
                         RETURN(rc);
                 }
         }
-
         if (rec != NULL) {
                 if ((body->valid & OBD_MD_FLEASIZE) &&
                     (rec->ur_flags & MDS_OPEN_HAS_EA)) {
-                        up(&dchild->d_inode->i_sem);
+                        UNLOCK_INODE_MUTEX(dchild->d_inode);
                         RETURN(-EEXIST);
                 }
-                
-                if (!(body->valid & OBD_MD_FLEASIZE)) {
-                        /* no EA: create objects */
-                        rc = mds_create_objects(obd, req, 2, rec,
-                                                dchild, handle, &ids);
-                        if (rc) {
-                                CERROR("mds_create_object: rc = %d\n", rc);
-                                up(&dchild->d_inode->i_sem);
+                if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
+                        UNLOCK_INODE_MUTEX(dchild->d_inode);
+                        rc = mds_join_file(rec, req, dchild, lockh);
+                        if (rc)
                                 RETURN(rc);
-                        }
+                        LOCK_INODE_MUTEX(dchild->d_inode);
                 }
-                
-                if (S_ISREG(dchild->d_inode->i_mode) &&
-                    (body->valid & OBD_MD_FLEASIZE)) {
-                        rc = mds_revalidate_lov_ea(obd, dchild->d_inode,
-                                                   req->rq_repmsg, 2);
-                        if (!rc)
-                                rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
-                                                 dchild->d_inode, 0, 0);
+                if (!(body->valid & OBD_MD_FLEASIZE) &&
+                    !(body->valid & OBD_MD_FLMODEASIZE)) {
+                        /* no EA: create objects */
+                        rc = mds_create_objects(req, DLM_REPLY_REC_OFF + 1, rec,
+                                                mds, obd, dchild, handle, &lmm);
                         if (rc) {
-                                up(&dchild->d_inode->i_sem);
+                                CERROR("mds_create_objects: rc = %d\n", rc);
+                                UNLOCK_INODE_MUTEX(dchild->d_inode);
                                 RETURN(rc);
                         }
                 }
         }
-
-        reply_off = 3;
-        rc = mds_pack_acl(req, reply_off, body, dchild->d_inode);
-        reply_off += 2;
-
-        if (rc < 0) {
-                CERROR("pack posix acl: rc = %d\n", rc);
-                up(&dchild->d_inode->i_sem);
-                RETURN(rc);
+        /* If the inode has no EA data, then MDS holds size, mtime */
+        if (S_ISREG(dchild->d_inode->i_mode) &&
+            !(body->valid & OBD_MD_FLEASIZE)) {
+                body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
+                                OBD_MD_FLATIME | OBD_MD_FLMTIME);
         }
+        UNLOCK_INODE_MUTEX(dchild->d_inode);
 
-        rc = mds_pack_gskey(obd, req->rq_repmsg, &reply_off, body, 
-                            dchild->d_inode); 
-        if (rc < 0) {
-                CERROR("mds_pack_gskey: rc = %d\n", rc);
-                up(&dchild->d_inode->i_sem);
-                RETURN(rc);
-        }
+        if (rec && !(rec->ur_flags & MDS_OPEN_JOIN_FILE))
+                lustre_shrink_reply(req, DLM_REPLY_REC_OFF + 1,
+                                    body->eadatasize, 0);
 
-        if (S_ISREG(mode)) {
-                struct lustre_capa capa = {
-                        .lc_uid   = rec->ur_uc.luc_uid,
-                        .lc_op    = capa_op(rec->ur_flags),
-                        .lc_ino   = dchild->d_inode->i_ino,
-                        .lc_igen  = dchild->d_inode->i_generation,
-                        .lc_mdsid = mds->mds_num,
-                };
-
-                rc = mds_pack_capa(obd, med, NULL, &capa, req,
-                                   &reply_off, body);
-                if (rc < 0) {
-                        CERROR("mds_pack_capa: rc = %d\n", rc);
-                        up(&dchild->d_inode->i_sem);
-                        RETURN(rc);
-                }
-        } else {
-                reply_off++;
-        }
+        if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
+            rec && !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
+                int acl_off = DLM_REPLY_REC_OFF + (body->eadatasize ? 2 : 1);
 
-        /* If the inode has no EA data, then MDSs hold size, mtime */
-        if (S_ISREG(mode) && !(body->valid & OBD_MD_FLEASIZE)) {
-                body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
-                                OBD_MD_FLATIME | OBD_MD_FLMTIME);
+                rc = mds_pack_acl(&req->rq_export->exp_mds_data,
+                                  dchild->d_inode, req->rq_repmsg,
+                                  body, acl_off);
+                lustre_shrink_reply(req, acl_off, body->aclsize, 0);
+                if (rc)
+                        RETURN(rc);
         }
 
-        up(&dchild->d_inode->i_sem);
-
         intent_set_disposition(rep, DISP_OPEN_OPEN);
-        mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req, rec);
+        mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
         if (IS_ERR(mfd))
                 RETURN(PTR_ERR(mfd));
 
         CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
                mfd->mfd_handle.h_cookie);
 
-        if (ids != NULL) {
-                mds_dt_update_objids(obd, ids);
-                OBD_FREE(ids, sizeof(*ids) * mds->mds_dt_desc.ld_tgt_count);
-        }
+        if (lmm != NULL)
+                mds_lov_update_objids(obd, lmm);
+
+        if (rc) /* coverity[deadcode] */
+                mds_mfd_unlink(mfd, 1);
 
+        mds_mfd_put(mfd);
         RETURN(rc);
 }
 
-static int mds_open_by_id(struct ptlrpc_request *req,
-                          struct lustre_id *id,
-                          struct mds_body *body, int flags,
-                          struct mds_update_record *rec,
-                          struct ldlm_reply *rep)
+static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
+                           struct mds_body *body, int flags,
+                           struct mds_update_record *rec,struct ldlm_reply *rep)
 {
         struct mds_obd *mds = mds_req2mds(req);
-        struct inode *pending_dir = mds->mds_pending_dir->d_inode;
         struct dentry *dchild;
-        char idname[LL_ID_NAMELEN];
-        int idlen = 0, rc;
+        char fidname[LL_FID_NAMELEN];
+        int fidlen = 0, rc;
         void *handle = NULL;
         ENTRY;
 
-        down(&pending_dir->i_sem);
-        idlen = ll_id2str(idname, id_ino(id), id_gen(id));
-        dchild = lookup_one_len(idname, mds->mds_pending_dir,
-                                idlen);
+        fidlen = ll_fid2str(fidname, fid->id, fid->generation);
+        dchild = ll_lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
         if (IS_ERR(dchild)) {
-                up(&pending_dir->i_sem);
                 rc = PTR_ERR(dchild);
-                CERROR("error looking up %s in PENDING: rc = %d\n", 
-                       idname, rc);
+                CERROR("error looking up %s in PENDING: rc = %d\n",fidname, rc);
                 RETURN(rc);
         }
 
-        up(&pending_dir->i_sem);
         if (dchild->d_inode != NULL) {
                 mds_inode_set_orphan(dchild->d_inode);
-                mds_pack_inode2body(req2obd(req), body,
-                                    dchild->d_inode, 1);
-                
-                intent_set_disposition(rep, DISP_LOOKUP_EXECD);
-                intent_set_disposition(rep, DISP_LOOKUP_POS);
                 CWARN("Orphan %s found and opened in PENDING directory\n",
-                       idname);
-                goto open;
-        }
-        l_dput(dchild);
+                       fidname);
+        } else {
+                l_dput(dchild);
 
-        /* we didn't find it in PENDING so it isn't an orphan.  See if it was a
-         * regular inode that was previously created. */
-        dchild = mds_id2dentry(req2obd(req), id, NULL);
-        if (IS_ERR(dchild))
-                RETURN(PTR_ERR(dchild));
+                /* We didn't find it in PENDING so it isn't an orphan.  See
+                 * if it was a regular inode that was previously created. */
+                dchild = mds_fid2dentry(mds, fid, NULL);
+                if (IS_ERR(dchild))
+                        RETURN(PTR_ERR(dchild));
+        }
 
-        mds_pack_inode2body(req2obd(req), body,
-                            dchild->d_inode, 1);
-        
+        mds_pack_inode2fid(&body->fid1, dchild->d_inode);
+        mds_pack_inode2body(body, dchild->d_inode);
         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
         intent_set_disposition(rep, DISP_LOOKUP_POS);
 
- open:
-        rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep);
-        rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
-                                req, rc, rep ? rep->lock_policy_res1 : 0);
+        rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep, NULL);
+        rc = mds_finish_transno(mds, dchild->d_inode, handle,
+                                req, rc, rep ? rep->lock_policy_res1 : 0, 0);
         /* XXX what do we do here if mds_finish_transno itself failed? */
+
         l_dput(dchild);
         RETURN(rc);
 }
@@ -891,32 +796,32 @@ static int mds_open_by_id(struct ptlrpc_request *req,
 int mds_pin(struct ptlrpc_request *req, int offset)
 {
         struct obd_device *obd = req->rq_export->exp_obd;
-        struct mds_body *request_body, *reply_body;
+        struct mds_body *reqbody, *repbody;
         struct lvfs_run_ctxt saved;
-        int rc, size = sizeof(*reply_body);
+        int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
         ENTRY;
 
-        request_body = lustre_msg_buf(req->rq_reqmsg, offset,
-                                      sizeof(*request_body));
+        reqbody = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*reqbody));
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
+        rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 RETURN(rc);
-        reply_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply_body));
+
+        repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                 sizeof(*repbody));
 
         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
-        rc = mds_open_by_id(req, &request_body->id1, reply_body,
-                            request_body->flags, NULL, NULL);
+        rc = mds_open_by_fid(req, &reqbody->fid1, repbody, reqbody->flags, NULL,
+                             NULL);
         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 
         RETURN(rc);
 }
 
-/*
- * get a lock on the ino to sync with creation WRT inode reuse (bug 2029). If
- * child_lockh is NULL we just get the lock as a barrier to wait for other
- * holders of this lock, and drop it right away again.
- */
+/*  Get an internal lock on the inode number (but not generation) to sync
+ *  new inode creation with inode unlink (bug 2029).  If child_lockh is NULL
+ *  we just get the lock as a barrier to wait for other holders of this lock,
+ *  and drop it right away again. */
 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
                        struct lustre_handle *child_lockh)
 {
@@ -924,17 +829,16 @@ int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
         struct lustre_handle lockh;
         int lock_flags = LDLM_FL_ATOMIC_CB;
         int rc;
-        ENTRY;
 
         if (child_lockh == NULL)
                 child_lockh = &lockh;
 
-        rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, child_res_id,
-                              LDLM_PLAIN, NULL, LCK_EX, &lock_flags,
-                              mds_blocking_ast, ldlm_completion_ast, NULL,
-                              NULL, NULL, 0, NULL, child_lockh);
+        rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
+                                    LDLM_PLAIN, NULL, LCK_EX, &lock_flags,
+                                    ldlm_blocking_ast, ldlm_completion_ast,
+                                    NULL, NULL, 0, NULL, child_lockh);
         if (rc != ELDLM_OK)
-                CERROR("ldlm_cli_enqueue: %d\n", rc);
+                CERROR("ldlm_cli_enqueue_local: %d\n", rc);
         else if (child_lockh == &lockh)
                 ldlm_lock_decref(child_lockh, LCK_EX);
 
@@ -946,35 +850,41 @@ int mds_open(struct mds_update_record *rec, int offset,
 {
         /* XXX ALLOCATE _something_ - 464 bytes on stack here */
         struct obd_device *obd = req->rq_export->exp_obd;
-        struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
         struct mds_obd *mds = mds_req2mds(req);
         struct ldlm_reply *rep = NULL;
         struct mds_body *body = NULL;
         struct dentry *dchild = NULL, *dparent = NULL;
-        struct lustre_handle parent_lockh[2] = {{0}, {0}};
+        struct mds_export_data *med;
+        struct lustre_handle parent_lockh;
         int rc = 0, cleanup_phase = 0, acc_mode, created = 0;
-        int parent_mode = LCK_PR;
+        int parent_mode = LCK_CR;
         void *handle = NULL;
-        struct dentry_params dp;
-        struct mea *mea = NULL;
-        int mea_size, update_mode;
-        int child_mode = LCK_PR;
-        struct lustre_id sid;
-        __u64 fid = 0;
+        struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
+        unsigned int qcids[MAXQUOTAS] = { current->fsuid, current->fsgid };
+        unsigned int qpids[MAXQUOTAS] = { 0, 0 };
+        int child_mode = LCK_CR;
+        /* Always returning LOOKUP lock if open succesful to guard
+           dentry on client. */
+        ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_LOOKUP}};
+        struct ldlm_res_id child_res_id = { .name = {0}};
+        int lock_flags = 0;
         ENTRY;
 
-        DEBUG_REQ(D_INODE, req, "parent "DLID4" name %*s mode %o",
-                  OLID4(rec->ur_id1), rec->ur_namelen - 1, rec->ur_name,
-                  rec->ur_mode);
-
-        OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
-                         (obd_timeout + 1) / 4);
-
-        if (offset == 3) { /* intent */
-                rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
-                body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
-        } else if (offset == 1) { /* non-intent reint */
-                body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
+        mds_counter_incr(req->rq_export, LPROC_MDS_OPEN);
+
+        OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
+                               (obd_timeout + 1) / 4);
+
+        CLASSERT(MAXQUOTAS < 4);
+        if (offset == DLM_INTENT_REC_OFF) { /* intent */
+                rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
+                                     sizeof(*rep));
+                body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
+                                      sizeof(*body));
+        } else if (offset == REQ_REC_OFF) { /* non-intent reint */
+                body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                      sizeof(*body));
+                LBUG(); /* XXX: not supported yet? */
         } else {
                 body = NULL;
                 LBUG();
@@ -982,73 +892,67 @@ int mds_open(struct mds_update_record *rec, int offset,
 
         MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
 
-        /*
-         * Step 0: If we are passed a id, then we assume the client already
-         * opened this file and is only replaying the RPC, so we open the inode
-         * by fid (at some large expense in security).
-         */
-        if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
-                CDEBUG(D_HA, "open obj "DLID4" name %*s mode %o\n",
-                       OLID4(rec->ur_id2), rec->ur_namelen - 1, 
-                       rec->ur_name, rec->ur_mode);
-
-                LASSERT(id_ino(rec->ur_id2));
+        /* Step 0: If we are passed a fid, then we assume the client already
+         * opened this file and is only replaying the RPC, so we open the
+         * inode by fid (at some large expense in security). */
+        /*XXX liblustre use mds_open_by_fid to implement LL_IOC_LOV_SETSTRIPE */
+        if (((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
+             (req->rq_export->exp_libclient && rec->ur_flags&MDS_OPEN_HAS_EA))&&
+            !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
+                if (rec->ur_fid2->id == 0) {
+                        struct ldlm_lock *lock = ldlm_handle2lock(child_lockh);
+                        if (lock) {
+                                LDLM_ERROR(lock, "fid2 not set on open replay");
+                                LDLM_LOCK_PUT(lock);
+                        }
+                        DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
+                        RETURN(-EFAULT);
+                }
 
-                rc = mds_open_by_id(req, rec->ur_id2, body,
-                                    rec->ur_flags, rec, rep);
+                rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
+                                     rec, rep);
                 if (rc != -ENOENT) {
-                        mds_body_do_reverse_map(med, body);
+                        if (req->rq_export->exp_libclient &&
+                            rec->ur_flags & MDS_OPEN_HAS_EA)
+                                RETURN(0);
+
                         RETURN(rc);
                 }
 
                 /* We didn't find the correct inode on disk either, so we
                  * need to re-create it via a regular replay. */
-                LASSERT(rec->ur_flags & MDS_OPEN_CREAT);
-        } else {
-                LASSERT(!id_ino(rec->ur_id2));
+                if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
+                        DEBUG_REQ(D_ERROR, req,"OPEN_CREAT not in open replay");
+                        RETURN(-EFAULT);
+                }
+        } else if (rec->ur_fid2->id) {
+                DEBUG_REQ(D_ERROR, req, "fid2 "LPU64"/%u on open non-replay",
+                          rec->ur_fid2->id, rec->ur_fid2->generation);
+                RETURN(-EFAULT);
         }
 
-        LASSERT(offset == 3); /* If we got here, we must be called via intent */
+        /* If we got here, we must be called via intent */
+        LASSERT(offset == DLM_INTENT_REC_OFF);
 
+        med = &req->rq_export->exp_mds_data;
         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
                 RETURN(-ENOMEM);
         }
 
-        acc_mode = accmode(rec->ur_flags);
-
         /* Step 1: Find and lock the parent */
-        if (rec->ur_flags & MDS_OPEN_CREAT) {
-                /* XXX Well, in fact we only need this lock mode change if
-                   in addition to O_CREAT, the file does not exist.
-                   But we do not know if it exists or not yet */
-                parent_mode = LCK_PW;
-        }
-        
-        if (rec->ur_namelen == 1) {
-                /* client (LMV) wants to open the file by id */
-                CDEBUG(D_OTHER, "OPEN by id "DLID4"\n", OLID4(rec->ur_id1));
-                dchild = mds_id2dentry(obd, rec->ur_id1, NULL);
-                if (IS_ERR(dchild)) {
-                        rc = PTR_ERR(dparent);
-                        CERROR("child lookup by an id error %d\n", rc);
-                        GOTO(cleanup, rc);
-                }
-                goto got_child;
-        }
-       
-restart:
-        dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, parent_mode,
-                                       parent_lockh, &update_mode, rec->ur_name,
-                                       rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
+        if (rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_JOIN_FILE))
+                parent_mode = LCK_EX;
+        dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, parent_mode,
+                                        &parent_lockh, MDS_INODELOCK_UPDATE);
         if (IS_ERR(dparent)) {
                 rc = PTR_ERR(dparent);
                 if (rc != -ENOENT) {
-                        CERROR("parent lookup for "DLID4" failed, error %d\n",
-                               OLID4(rec->ur_id1), rc);
+                        CERROR("parent "LPU64"/%u lookup error %d\n",
+                               rec->ur_fid1->id, rec->ur_fid1->generation, rc);
                 } else {
                         /* Just cannot find parent - make it look like
-                           usual negative lookup to avoid extra MDS RPC */
+                         * usual negative lookup to avoid extra MDS RPC */
                         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
                         intent_set_disposition(rep, DISP_LOOKUP_NEG);
                 }
@@ -1058,142 +962,56 @@ restart:
 
         cleanup_phase = 1; /* parent dentry and lock */
 
-        /* try to retrieve MEA data for this dir */
-        rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
-        if (rc)
-                GOTO(cleanup, rc);
-       
-        if (mea != NULL && mea->mea_count) {
-                /*
-                 * dir is already splitted, check is requested filename should *
-                 * live at this MDS or at another one.
-                 */
-                int i;
-                i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
-                if (mea->mea_master != id_group(&mea->mea_ids[i])) {
-                        CDEBUG(D_OTHER,
-                               "%s: inapropriate MDS(%d) for %lu/%u:%s."
-                               " should be %lu(%d)\n", obd->obd_name,
-                               mea->mea_master, dparent->d_inode->i_ino,
-                               dparent->d_inode->i_generation, rec->ur_name,
-                               (unsigned long)id_group(&mea->mea_ids[i]), i);
-                        GOTO(cleanup, rc = -ERESTART);
-                }
+        if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
+                dchild = dget(dparent);
+                cleanup_phase = 2; /* child dentry */
+                acc_mode = accmode(dchild->d_inode, rec->ur_flags);
+                GOTO(found_child, rc);
         }
 
         /* Step 2: Lookup the child */
-        dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
+
+        if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
+            (rec->ur_flags & MDS_OPEN_LOCK) && (rec->ur_namelen == 1)) {
+                /* hack for nfsd with no_subtree_check, it will use anon
+                 * dentry w/o filename to open the file. the anon dentry's
+                 * parent was set to itself, so rec->ur_fid1 is the file.
+                 * And in MDC it cannot derive the dentry's parent dentry,
+                 * hence the file's name, so we hack here in MDS,
+                 * refer to bug 13030. */
+                dchild = mds_fid2dentry(mds, rec->ur_fid1, NULL);
+        } else {
+                dchild = ll_lookup_one_len(rec->ur_name, dparent,
+                                           rec->ur_namelen - 1);
+        }
         if (IS_ERR(dchild)) {
                 rc = PTR_ERR(dchild);
                 dchild = NULL; /* don't confuse mds_finish_transno() below */
                 GOTO(cleanup, rc);
         }
 
-got_child:
         cleanup_phase = 2; /* child dentry */
 
-        if (dchild->d_flags & DCACHE_CROSS_REF) {
-                CDEBUG(D_OTHER, "cross reference: "DLID4"\n", OLID4(rec->ur_id1));
-                LASSERT(rec->ur_namelen > 1);
-                
-                /* we're gonna acquire LOOKUP lock on the child,
-                 * but we have already locked parent and our order
-                 * may conflict with enqueue_order_locks(). so,
-                 * drop parent lock and acquire both the locks in
-                 * common order. bug 6190 */
-#ifdef S_PDIROPS
-                if (parent_lockh[1].cookie != 0)
-                        ldlm_lock_decref(parent_lockh + 1, update_mode);
-#endif
-                ldlm_lock_decref(parent_lockh, parent_mode);
-                l_dput(dchild);
-                l_dput(dparent);
-                cleanup_phase = 0;
-
-                rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
-                                                 parent_lockh, &dparent,
-                                                 LCK_PR, MDS_INODELOCK_UPDATE,
-                                                 &update_mode, rec->ur_name,
-                                                 rec->ur_namelen, child_lockh,
-                                                 &dchild, LCK_PR,
-                                                 MDS_INODELOCK_LOOKUP);
-
-                if (rc)
-                        GOTO(cleanup, rc);
-
-#ifdef S_PDIROPS
-                if (parent_lockh[1].cookie != 0)
-                        ldlm_lock_decref(parent_lockh + 1, update_mode);
-#endif
-                ldlm_lock_decref(parent_lockh, LCK_PR);
-
-                if (dchild->d_inode || !(dchild->d_flags & DCACHE_CROSS_REF)) {
-                        CDEBUG(D_OTHER, "race: name changed (%p)\n",
-                               dchild->d_inode);
-                        if (dchild->d_inode)
-                                ldlm_lock_decref(child_lockh, LCK_PR);
-                        l_dput(dchild);
-                        l_dput(dparent);
-                        GOTO(restart, rc);
-                }
-
-                mds_pack_dentry2body(obd, body, dchild, 1);
-                intent_set_disposition(rep, DISP_LOOKUP_POS);
-                intent_set_disposition(rep, DISP_LOOKUP_EXECD);
-
-                if (mea)
-                        OBD_FREE(mea, mea_size);
-                l_dput(dchild);
-                l_dput(dparent);
-                RETURN(rc);
-        }
-        
         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
-
         if (dchild->d_inode)
                 intent_set_disposition(rep, DISP_LOOKUP_POS);
         else
                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
 
-        /* Step 3: If the child was negative, and we're supposed to, create it.*/
+        /*Step 3: If the child was negative, and we're supposed to, create it.*/
         if (dchild->d_inode == NULL) {
-                unsigned long ino;
+                unsigned long ino = rec->ur_fid2->id;
                 struct iattr iattr;
                 struct inode *inode;
-                
-                /* get parent id: ldlm lock on the parent protects ea */
-                rc = mds_read_inode_sid(obd, dparent->d_inode, &sid);
-                if (rc) {
-                        CERROR("can't read parent inode id. ino(%lu) rc(%d)\n",
-                                dparent->d_inode->i_ino, rc);
-                        GOTO(cleanup, rc);
-                }
-
-                ino = (unsigned long)id_ino(rec->ur_id2);
-                
-                rc = mds_try_to_split_dir(obd, dparent, &mea, 0,
-                                          update_mode);
-                
-                CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d\n",
-                       obd->obd_name, dparent->d_inode->i_ino,
-                       dparent->d_inode->i_generation, rc);
-
-                if (rc > 0) {
-                        /* dir got splitted */
-                        GOTO(cleanup, rc = -ERESTART);
-                } else if (rc < 0) {
-                        /* error happened during spitting */
-                        GOTO(cleanup, rc);
-                }
 
                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
-                        /*
-                         * dentry is negative and we weren't supposed to create
-                         * object, get out of here.
-                         */
+                        /* It's negative and we weren't supposed to create it */
                         GOTO(cleanup, rc = -ENOENT);
                 }
 
+                if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
+                        GOTO(cleanup, rc = -EROFS);
+
                 intent_set_disposition(rep, DISP_OPEN_CREATE);
                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_CREATE,
                                       NULL);
@@ -1202,19 +1020,11 @@ got_child:
                         handle = NULL;
                         GOTO(cleanup, rc);
                 }
-                if (id_fid(rec->ur_id2))
-                        fid = id_fid(rec->ur_id2); 
-                else 
-                        fid = mds_alloc_fid(obd);
-                
                 dchild->d_fsdata = (void *) &dp;
-                dp.p_ptr = req;
-                dp.p_inum = ino;
-                
-                dp.p_fid = fid;
-                dp.p_group = mds->mds_num;
+                dp.ldp_ptr = req;
+                dp.ldp_inum = ino;
 
-                rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode, NULL);
+                rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode,NULL);
                 if (dchild->d_fsdata == (void *)(unsigned long)ino)
                         dchild->d_fsdata = NULL;
 
@@ -1222,14 +1032,11 @@ got_child:
                         CDEBUG(D_INODE, "error during create: %d\n", rc);
                         GOTO(cleanup, rc);
                 }
-
                 inode = dchild->d_inode;
-
                 if (ino) {
                         LASSERT(ino == inode->i_ino);
-                        
-                        /* written as part of setattr */
-                        inode->i_generation = id_gen(rec->ur_id2);
+                        /* Written as part of setattr */
+                        inode->i_generation = rec->ur_fid2->generation;
                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
                                inode->i_ino, inode->i_generation);
                 }
@@ -1239,11 +1046,11 @@ got_child:
                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
 
-                iattr.ia_uid = rec->ur_fsuid;
+                iattr.ia_uid = current->fsuid;  /* set by push_ctxt already */
                 if (dparent->d_inode->i_mode & S_ISGID)
                         iattr.ia_gid = dparent->d_inode->i_gid;
                 else
-                        iattr.ia_gid = rec->ur_fsgid;
+                        iattr.ia_gid = current->fsgid;
 
                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
                         ATTR_MTIME | ATTR_CTIME;
@@ -1257,31 +1064,22 @@ got_child:
                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
                 if (rc)
                         CERROR("error on parent setattr: rc = %d\n", rc);
-                else {
-                        MD_COUNTER_INCREMENT(obd, create);
-                }
-
-                mds_inode2id(obd, &body->id1, inode, fid);
-                mds_update_inode_ids(obd, dchild->d_inode, handle, &body->id1, &sid);
 
-                if ((rec->ur_flags & MDS_OPEN_HAS_KEY)) { 
-                        rc = mds_set_gskey(obd, handle, dchild->d_inode, 
-                                           rec->ur_ea2data, rec->ur_ea2datalen, 
-                                           ATTR_KEY | ATTR_MAC);
-                        if (rc) {
-                                CERROR("error in set gs key rc %d\n", rc); 
-                        }
-                }
-
-                rc = fsfilt_commit(obd, dchild->d_inode->i_sb, dchild->d_inode, handle, 0);
+                rc = fsfilt_commit(obd, dchild->d_inode, handle, 0);
                 handle = NULL;
                 acc_mode = 0;           /* Don't check for permissions */
+        } else {
+                acc_mode = accmode(dchild->d_inode, rec->ur_flags);
         }
-        mds_pack_inode2body(obd, body, dchild->d_inode, 1);
-       
+
         LASSERTF(!mds_inode_is_orphan(dchild->d_inode),
-                 "dchild %.*s (%p) inode %p\n", dchild->d_name.len,
-                 dchild->d_name.name, dchild, dchild->d_inode);
+                 "dchild %.*s (%p) inode %p/%lu/%u\n", dchild->d_name.len,
+                 dchild->d_name.name, dchild, dchild->d_inode,
+                 dchild->d_inode->i_ino, dchild->d_inode->i_generation);
+
+found_child:
+        mds_pack_inode2fid(&body->fid1, dchild->d_inode);
+        mds_pack_inode2body(body, dchild->d_inode);
 
         if (S_ISREG(dchild->d_inode->i_mode)) {
                 /* Check permissions etc */
@@ -1289,9 +1087,9 @@ got_child:
                 if (rc != 0)
                         GOTO(cleanup, rc);
 
-                /* Can't write to a read-only file */
-                if (IS_RDONLY(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0)
-                        GOTO(cleanup, rc = -EPERM);
+                if ((req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY) &&
+                    (acc_mode & MAY_WRITE))
+                        GOTO(cleanup, rc = -EROFS);
 
                 /* An append-only file must be opened in append mode for
                  * writing */
@@ -1308,140 +1106,89 @@ got_child:
                 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
         }
 
+        /* if we are following a symlink, don't open */
+        if (S_ISLNK(dchild->d_inode->i_mode))
+                GOTO(cleanup_no_trans, rc = 0);
+
         if (S_ISDIR(dchild->d_inode->i_mode)) {
                 if (rec->ur_flags & MDS_OPEN_CREAT ||
                     rec->ur_flags & FMODE_WRITE) {
                         /* we are trying to create or write a exist dir */
                         GOTO(cleanup, rc = -EISDIR);
                 }
-                if (ll_permission(dchild->d_inode, acc_mode, NULL))
+                if (rec->ur_flags & MDS_FMODE_EXEC) {
+                        /* we are trying to exec a directory */
                         GOTO(cleanup, rc = -EACCES);
-
-                /* 
-                 * here was checking for possible GNS mount points to skip their
-                 * open. I removed it as its detection based only on SUID bit is
-                 * not reliable. Opening such a dirs should not cause any
-                 * problems, at least tests show that. --umka
-                 */
-        }
-
-        /* if we are following a symlink, don't open */
-        if (S_ISLNK(dchild->d_inode->i_mode))
-                GOTO(cleanup_no_trans, rc = 0);
-
-        if ((rec->ur_flags & MDS_OPEN_DIRECTORY) &&
-            !S_ISDIR(dchild->d_inode->i_mode))
+                }
+                if (ll_permission(dchild->d_inode, acc_mode, NULL)) {
+                        intent_set_disposition(rep, DISP_OPEN_OPEN);
+                        GOTO(cleanup, rc = -EACCES);
+                }
+        } else if (rec->ur_flags & MDS_OPEN_DIRECTORY) {
                 GOTO(cleanup, rc = -ENOTDIR);
+        }
 
-        /* check permission even it's special files */
-        if (S_ISCHR(dchild->d_inode->i_mode) ||
-            S_ISBLK(dchild->d_inode->i_mode) ||
-            S_ISFIFO(dchild->d_inode->i_mode) ||
-            S_ISSOCK(dchild->d_inode->i_mode)) {
-                rc = ll_permission(dchild->d_inode, acc_mode, NULL);
-                if (rc != 0)
-                        GOTO(cleanup, rc);
+        if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
+                                 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
+                GOTO(cleanup, rc = -EAGAIN);
         }
 
-       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
-               obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
-               GOTO(cleanup, rc = -EAGAIN);
-       }
+        /* Obtain OPEN lock as well */
+        policy.l_inodebits.bits |= MDS_INODELOCK_OPEN;
 
-#if 0
         /* We cannot use acc_mode here, because it is zeroed in case of
            creating a file, so we get wrong lockmode */
-        if (accmode(rec->ur_flags) & MAY_WRITE)
-               child_mode = LCK_CW;
-        else if (accmode(rec->ur_flags) & MAY_EXEC)
+        if (accmode(dchild->d_inode, rec->ur_flags) & MAY_WRITE)
+                child_mode = LCK_CW;
+        else if (accmode(dchild->d_inode, rec->ur_flags) & MAY_EXEC)
                 child_mode = LCK_PR;
         else
                 child_mode = LCK_CR;
 
-        /* Always returning LOOKUP lock if open succesful to guard
-         * dentry on client. In case of replay we do not get a lock
-         * assuming that the caller has it already */
-        if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
-                struct ldlm_res_id child_res_id = { .name = {0}};
-                ldlm_policy_data_t policy;
-                int lock_flags = LDLM_FL_ATOMIC_CB;
-                
-                /* LOOKUP lock will protect dentry on client -bzzz */
-                policy.l_inodebits.bits = MDS_INODELOCK_LOOKUP |
-                                                MDS_INODELOCK_OPEN;
-
-                child_res_id.name[0] = id_fid(&body->id1);
-                child_res_id.name[1] = id_group(&body->id1);
-
-                rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
-                                      child_res_id, LDLM_IBITS, &policy,
-                                      child_mode, &lock_flags,
-                                      mds_blocking_ast, ldlm_completion_ast,
-                                      NULL, NULL, NULL, 0, NULL, child_lockh);
+        if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
+             (rec->ur_flags & MDS_OPEN_LOCK)) {
+                /* In case of replay we do not get a lock assuming that the
+                   caller has it already */
+                child_res_id.name[0] = dchild->d_inode->i_ino;
+                child_res_id.name[1] = dchild->d_inode->i_generation;
+
+                rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
+                                            LDLM_IBITS, &policy, child_mode,
+                                            &lock_flags, ldlm_blocking_ast,
+                                            ldlm_completion_ast, NULL, NULL,
+                                            0, NULL, child_lockh);
                 if (rc != ELDLM_OK)
                         GOTO(cleanup, rc);
 
+                /* Let mds_intent_policy know that we have a lock to return */
+                intent_set_disposition(rep, DISP_OPEN_LOCK);
                 cleanup_phase = 3;
         }
-#else
-/* re-enable test 24n in sanity.sh: it needs LOOKUP lock on open */
-#endif
 
-        /* Step 5: mds_open it */
-        rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle,
-                             rec, rep);
-        if (rc)
-                GOTO(cleanup, rc);
-
-        /* reintegration case */
-        if ((rec->ur_flags & MDS_REINT_REQ)) {
-                rc = mds_fidmap_add(obd, &body->id1);
-                if (rc < 0) {
-                        CERROR("can't create fid->ino mapping, err %d\n",
-                               rc);
-                } else {
-                       rc = 0;
-               }
-        }
-        
-        /* if this is a writer, we have to invalidate client's
-         * update locks in order to make sure they don't use
-         * isize/iblocks from mds anymore.
-         * FIXME: can cause a deadlock, use mds_get_parent_child_locked()
-         * XXX: optimization is to do this for first writer only */
-        if (accmode(rec->ur_flags) & MAY_WRITE) {
-                struct ldlm_res_id child_res_id = { .name = {0}};
-                ldlm_policy_data_t sz_policy;
-                struct lustre_handle sz_lockh;
-                int lock_flags = LDLM_FL_ATOMIC_CB;
-
-                child_res_id.name[0] = id_fid(&body->id1);
-                child_res_id.name[1] = id_group(&body->id1);
-                sz_policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
-
-                rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
-                                      child_res_id, LDLM_IBITS, &sz_policy,
-                                      LCK_PW, &lock_flags, mds_blocking_ast,
-                                      ldlm_completion_ast, NULL, NULL, NULL,
-                                      0, NULL, &sz_lockh);
-                if (rc == ELDLM_OK)
-                        ldlm_lock_decref(&sz_lockh, LCK_PW);
-                else
-                        CERROR("can't invalidate client's update locks\n");
+        if (!S_ISREG(dchild->d_inode->i_mode) &&
+            !S_ISDIR(dchild->d_inode->i_mode) &&
+            (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH)) {
+                /* If client supports this, do not return open handle for
+                 * special device nodes */
+                GOTO(cleanup_no_trans, rc = 0);
         }
 
-       EXIT;
-cleanup:
+        /* Step 5: mds_open it */
+        rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
+                             rep, &parent_lockh);
+        GOTO(cleanup, rc);
+
+ cleanup:
         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
-                                req, rc, rep ? rep->lock_policy_res1 : 0);
+                                req, rc, rep ? rep->lock_policy_res1 : 0, 0);
 
-cleanup_no_trans:
+ cleanup_no_trans:
         switch (cleanup_phase) {
         case 3:
-                if (rc) {
+                if (rc)
+                        /* It is safe to leave IT_OPEN_LOCK set, if rc is not 0,
+                         * mds_intent_policy won't try to return any locks */
                         ldlm_lock_decref(child_lockh, child_mode);
-                        child_lockh->cookie = 0;
-                }
         case 2:
                 if (rc && created) {
                         int err = vfs_unlink(dparent->d_inode, dchild);
@@ -1452,50 +1199,36 @@ cleanup_no_trans:
                         }
                 } else if (created) {
                         mds_lock_new_child(obd, dchild->d_inode, NULL);
+                        /* save uid/gid for quota acquire/release */
+                        qpids[USRQUOTA] = dparent->d_inode->i_uid;
+                        qpids[GRPQUOTA] = dparent->d_inode->i_gid;
                 }
-                /* audit stuff for OPEN */
-                if (offset == 3) {
-                        mds_audit(req, dchild, rec->ur_name,
-                                  rec->ur_namelen - 1, AUDIT_OPEN, rc);
-                }
-
                 l_dput(dchild);
         case 1:
                 if (dparent == NULL)
                         break;
 
                 l_dput(dparent);
-#ifdef S_PDIROPS
-                if (parent_lockh[1].cookie != 0)
-                        ldlm_lock_decref(parent_lockh + 1, update_mode);
-#endif
                 if (rc)
-                        ldlm_lock_decref(parent_lockh, parent_mode);
+                        ldlm_lock_decref(&parent_lockh, parent_mode);
                 else
-                        ptlrpc_save_lock (req, parent_lockh, parent_mode);
+                        ptlrpc_save_lock(req, &parent_lockh, parent_mode);
         }
-        if (mea)
-                OBD_FREE(mea, mea_size);
-        if (rc == 0)
-                atomic_inc(&mds->mds_open_count);
+        /* trigger dqacq on the owner of child and parent */
+        lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
+                      FSFILT_OP_CREATE);
 
-        mds_body_do_reverse_map(med, body);
-
-        /*
-         * If we have not taken the "open" lock, we may not return 0 here,
-         * because caller expects 0 to mean "lock is taken", and it needs
-         * nonzero return here for caller to return EDLM_LOCK_ABORTED to
-         * client. Later caller should rewrite the return value back to zero
-         * if it to be used any further.
+        /* If we have not taken the "open" lock, we may not return 0 here,
+           because caller expects 0 to mean "lock is taken", and it needs
+           nonzero return here for caller to return EDLM_LOCK_ABORTED to
+           client. Later caller should rewrite the return value back to zero
+           if it to be used any further
          */
-        if ((cleanup_phase != 3) && !rc)
-                rc = ENOLCK;
-
         RETURN(rc);
 }
 
 /* Close a "file descriptor" and possibly unlink an orphan from the
- * PENDING directory.  Caller must hold child->i_sem, this drops it. 
+ * PENDING directory.  Caller must hold child->i_mutex, this drops it.
  *
  * If we are being called from mds_disconnect() because the client has
  * disappeared, then req == NULL and we do not update last_rcvd because
@@ -1503,107 +1236,83 @@ cleanup_no_trans:
  * (it will not even _have_ an entry in last_rcvd anymore).
  *
  * Returns EAGAIN if the client needs to get more data and re-close. */
-int mds_mfd_close(struct ptlrpc_request *req, int offset,
-                  struct obd_device *obd, struct mds_file_data *mfd,
-                  int unlink_orphan)
+int mds_mfd_close(struct ptlrpc_request *req, int offset,struct obd_device *obd,
+                  struct mds_file_data *mfd, int unlink_orphan,
+                  struct lov_mds_md *lmm, int lmm_size,
+                  struct llog_cookie *logcookies, int cookies_size,
+                  __u64 *valid)
 {
         struct inode *inode = mfd->mfd_dentry->d_inode;
-        char idname[LL_ID_NAMELEN];
-        int last_orphan, idlen, rc = 0, cleanup_phase = 0;
+        char fidname[LL_FID_NAMELEN];
+        int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
         struct dentry *pending_child = NULL;
         struct mds_obd *mds = &obd->u.mds;
         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
         void *handle = NULL;
         struct mds_body *request_body = NULL, *reply_body = NULL;
-        struct dentry_params dp;
+        struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
         struct iattr iattr = { 0 };
-        struct llog_create_locks *lcl = NULL;
         ENTRY;
 
         if (req && req->rq_reqmsg != NULL)
                 request_body = lustre_msg_buf(req->rq_reqmsg, offset,
                                               sizeof(*request_body));
         if (req && req->rq_repmsg != NULL)
-                reply_body = lustre_msg_buf(req->rq_repmsg, 0,
+                reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
                                             sizeof(*reply_body));
 
-        if (request_body && (request_body->valid & OBD_MD_FLSIZE)) {
-                /* we set i_size/i_blocks here, nobody will see
-                 * them until all write references are dropped.
-                 * btw, we hold one reference */
-                LASSERT(mfd->mfd_mode & FMODE_WRITE);
-                LASSERT(request_body->valid & OBD_MD_FLEPOCH);
-                LASSERT(MDS_FILTERDATA(inode));
-                if (MDS_FILTERDATA(inode)->io_epoch != request_body->io_epoch)
-                        CDEBUG(D_ERROR, "try to update attr. for old epoch "
-                               LPD64" while current "LPD64"\n",
-                               MDS_FILTERDATA(inode)->io_epoch,
-                               request_body->io_epoch);
-                i_size_write(inode, request_body->size);
-                inode->i_blocks = request_body->blocks;
-                LTIME_S(inode->i_mtime) = (request_body->mtime);
-
-                LTIME_S(iattr.ia_mtime) = request_body->mtime;
-                iattr.ia_size = inode->i_size;
-                iattr.ia_valid |= ATTR_SIZE|ATTR_MTIME;
-                mds_inode_unset_attrs_old(inode);
-        }
+        fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
 
-        idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
-        CDEBUG(D_INODE, "inode %p ino %s nlink %d orphan %d\n", inode, 
-               idname, inode->i_nlink, mds_orphan_open_count(inode));
+        CDEBUG(D_INODE, "inode %p ino %s nlink %d orphan %d\n", inode, fidname,
+               inode->i_nlink, mds_orphan_open_count(inode));
 
         last_orphan = mds_orphan_open_dec_test(inode) &&
-                mds_inode_is_orphan(inode);
-        UP_WRITE_I_ALLOC_SEM(inode);
+                      mds_inode_is_orphan(inode);
 
         /* this is half of the actual "close" */
         if (mfd->mfd_mode & FMODE_WRITE) {
                 rc = mds_put_write_access(mds, inode, request_body,
                                           last_orphan && unlink_orphan);
-        } else if (mfd->mfd_mode & FMODE_EXEC) {
+        } else if (mfd->mfd_mode & MDS_FMODE_EXEC) {
                 mds_allow_write_access(inode);
         }
+        /* here writecount change also needs protection from orphan write sem. 
+         * so drop orphan write sem after mds_put_write_access, bz 12888. */
+        MDS_UP_WRITE_ORPHAN_SEM(inode);
 
         if (last_orphan && unlink_orphan) {
-                struct lov_mds_md *lmm = NULL;
                 int stripe_count = 0;
                 LASSERT(rc == 0); /* mds_put_write_access must have succeeded */
 
-                CDEBUG(D_HA, "destroying orphan object %s\n", idname);
-                
+                CDEBUG(D_INODE, "destroying orphan object %s\n", fidname);
+
                 if ((S_ISREG(inode->i_mode) && inode->i_nlink != 1) ||
                     (S_ISDIR(inode->i_mode) && inode->i_nlink != 2))
                         CERROR("found \"orphan\" %s %s with link count %d\n",
                                S_ISREG(inode->i_mode) ? "file" : "dir",
-                               idname, inode->i_nlink);
-                /*
-                 * sadly, there is no easy way to save pending_child from
-                 * mds_reint_unlink() into mfd, so we need to re-lookup, but
-                 * normally it will still be in the dcache.
-                 */
-                down(&pending_dir->i_sem);
-                cleanup_phase = 1; /* up(i_sem) when finished */
-                pending_child = lookup_one_len(idname, mds->mds_pending_dir,
-                                               idlen);
+                               fidname, inode->i_nlink);
+
+                /* Sadly, there is no easy way to save pending_child from
+                 * mds_reint_unlink() into mfd, so we need to re-lookup,
+                 * but normally it will still be in the dcache. */
+                LOCK_INODE_MUTEX(pending_dir);
+                cleanup_phase = 1; /* UNLOCK_INODE_MUTEX(pending_dir) when finished */
+                pending_child = lookup_one_len(fidname, mds->mds_pending_dir,
+                                               fidlen);
                 if (IS_ERR(pending_child))
                         GOTO(cleanup, rc = PTR_ERR(pending_child));
-                if (pending_child->d_inode == NULL) {
-                        CERROR("orphan %s has been removed\n", idname);
-                        GOTO(cleanup, rc = 0);
-                }
+                LASSERT(pending_child->d_inode != NULL);
 
                 cleanup_phase = 2; /* dput(pending_child) when finished */
                 if (S_ISDIR(pending_child->d_inode->i_mode)) {
                         rc = vfs_rmdir(pending_dir, pending_child);
                         if (rc)
                                 CERROR("error unlinking orphan dir %s: rc %d\n",
-                                       idname, rc);
+                                       fidname,rc);
                         goto out;
                 }
 
-                if (req != NULL && req->rq_repmsg != NULL) {
-                        lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
+                if (lmm != NULL) {
                         stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
                 }
 
@@ -1615,34 +1324,20 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                         GOTO(cleanup, rc);
                 }
 
+                if (lmm != NULL && (*valid & OBD_MD_FLEASIZE) &&
+                    mds_log_op_unlink(obd, lmm, lmm_size,
+                                      logcookies, cookies_size) > 0) {
+                        *valid |= OBD_MD_FLCOOKIE;
+                }
+
+                dp.ldp_inum = 0;
+                dp.ldp_ptr = req;
                 pending_child->d_fsdata = (void *) &dp;
-                dp.p_inum = 0;
-                dp.p_ptr = req;
                 rc = vfs_unlink(pending_dir, pending_child);
                 if (rc)
-                        CERROR("error unlinking orphan %s: rc %d\n",
-                               idname, rc);
-
-                if (req != NULL && req->rq_repmsg != NULL &&
-                    (reply_body->valid & OBD_MD_FLEASIZE) &&
-                    mds_log_op_unlink(obd, pending_child->d_inode,
-                                      lmm, req->rq_repmsg->buflens[1],
-                                      lustre_msg_buf(req->rq_repmsg, 2, 0),
-                                      req->rq_repmsg->buflens[2], &lcl) > 0) {
-                        reply_body->valid |= OBD_MD_FLCOOKIE;
-                }
-               
-               rc = mds_destroy_object(obd, inode, 1);
-               if (rc) {
-                       CERROR("cannot destroy OSS object on close, err %d\n",
-                              rc);
-                       rc = 0;
-               }
+                        CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
 
                 goto out; /* Don't bother updating attrs on unlinked inode */
-        } else if ((mfd->mfd_mode & FMODE_WRITE) && rc == 0) {
-                /* last writer closed file - let's update i_size/i_blocks */
-                mds_validate_size(obd, inode, request_body, &iattr);
         }
 
 #if 0
@@ -1654,13 +1349,13 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                  */
 
                 if (request_body->valid & OBD_MD_FLMTIME &&
-                    LTIME_S(request_body->mtime) > LTIME_S(inode->i_mtime)) {
-                        LTIME_S(iattr.ia_mtime) = LTIME_S(request_body->mtime);
+                    LTIME_S(iattr.ia_mtime) > LTIME_S(inode->i_mtime)) {
+                        LTIME_S(iattr.ia_mtime) = request_body->mtime;
                         iattr.ia_valid |= ATTR_MTIME;
                 }
                 if (request_body->valid & OBD_MD_FLCTIME &&
-                    LTIME_S(request_body->ctime) > LTIME_S(inode->i_ctime)) {
-                        LTIME_S(iattr.ia_ctime) = LTIME_S(request_body->ctime);
+                    LTIME_S(iattr.ia_ctime) > LTIME_S(inode->i_ctime)) {
+                        LTIME_S(iattr.ia_ctime) = request_body->ctime;
                         iattr.ia_valid |= ATTR_CTIME;
                 }
 
@@ -1669,10 +1364,7 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                         iattr.ia_valid |= ATTR_SIZE;
                         iattr.ia_size = request_body->size;
                 }
-                /* if (request_body->valid & OBD_MD_FLBLOCKS) {
-                        iattr.ia_valid |= ATTR_BLOCKS;
-                        iattr.ia_blocks = request_body->blocks
-                } */
+                /* iattr.ia_blocks = request_body->blocks */
 
         }
 #endif
@@ -1681,50 +1373,37 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                  * it is more out-of-date than the specified limit.  If we
                  * are already going to write out the atime then do it anyway.
                  * */
-                if ((request_body->atime >
-                     LTIME_S(inode->i_atime) + MAX_ATIME_DIFF) ||
+                LTIME_S(iattr.ia_atime) = request_body->atime;
+                if ((LTIME_S(iattr.ia_atime) >
+                     LTIME_S(inode->i_atime) + mds->mds_atime_diff) ||
                     (iattr.ia_valid != 0 &&
-                     request_body->atime > LTIME_S(inode->i_atime))) {
-                        LTIME_S(iattr.ia_atime) = request_body->atime;
+                     LTIME_S(iattr.ia_atime) > LTIME_S(inode->i_atime)))
                         iattr.ia_valid |= ATTR_ATIME;
-                }
         }
 
         if (iattr.ia_valid != 0) {
-                if (handle == NULL)
-                        handle = fsfilt_start(obd,inode,FSFILT_OP_SETATTR,NULL);
-                if (IS_ERR(handle))
-                        GOTO(cleanup, rc = PTR_ERR(handle));
+                handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
+                if (IS_ERR(handle)) {
+                        rc = PTR_ERR(handle);
+                        handle = NULL;
+                        GOTO(cleanup, rc);
+                }
                 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
                 if (rc)
-                        CERROR("error in setattr(%s): rc %d\n", idname, rc);
+                        CERROR("error in setattr(%s): rc %d\n", fidname, rc);
         }
 out:
         /* If other clients have this file open for write, rc will be > 0 */
         if (rc > 0)
                 rc = 0;
-        if (!obd->obd_recovering && mds_inode_has_old_attrs(inode)
-                        && !mds_inode_is_orphan(inode)
-                        && atomic_read(&inode->i_writecount) == 0
-                        && inode->i_nlink != 0) {
-                CERROR("leave inode %lu/%u with old attributes (nlink = %d)\n",
-                       inode->i_ino, inode->i_generation, inode->i_nlink);
-        }
         l_dput(mfd->mfd_dentry);
-        mds_mfd_destroy(mfd);
+        mds_mfd_put(mfd);
 
  cleanup:
-        atomic_dec(&mds->mds_open_count);
         if (req != NULL && reply_body != NULL) {
-                rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0);
+                rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0, 0);
         } else if (handle) {
-                int err, force_sync = 0;
-
-                if (req && req->rq_export)
-                        force_sync = req->rq_export->exp_sync;
-
-                err = fsfilt_commit(obd, mds->mds_sb, pending_dir, handle, 
-                                    force_sync);
+                int err = fsfilt_commit(obd, pending_dir, handle, 0);
                 if (err) {
                         CERROR("error committing close: %d\n", err);
                         if (!rc)
@@ -1734,149 +1413,13 @@ out:
 
         switch (cleanup_phase) {
         case 2:
-                if (lcl != NULL)
-                        ptlrpc_save_llog_lock(req, lcl);
                 dput(pending_child);
         case 1:
-                up(&pending_dir->i_sem);
+                UNLOCK_INODE_MUTEX(pending_dir);
         }
         RETURN(rc);
 }
 
-static int mds_extent_lock_callback(struct ldlm_lock *lock,
-                                    struct ldlm_lock_desc *new, void *data,
-                                    int flag)
-{
-        struct lustre_handle lockh = { 0 };
-        int rc;
-        ENTRY;
-
-        switch (flag) {
-        case LDLM_CB_BLOCKING:
-                ldlm_lock2handle(lock, &lockh);
-                rc = ldlm_cli_cancel(&lockh);
-                if (rc != ELDLM_OK)
-                        CERROR("ldlm_cli_cancel failed: %d\n", rc);
-                break;
-        case LDLM_CB_CANCELING: {
-                break;
-        }
-        default:
-                LBUG();
-        }
-
-        RETURN(0);
-}
-__u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
-__u64 lov_merge_blocks(struct lov_stripe_md *lsm);
-__u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
-
-int mds_validate_size(struct obd_device *obd, struct inode *inode,
-                      struct mds_body *body, struct iattr *iattr)
-{
-        ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
-        struct lustre_handle lockh = { 0 };
-        struct lov_stripe_md *lsm = NULL;
-        int rc, len, flags;
-        void *lmm = NULL;
-        ENTRY;
-
-        /* we update i_size/i_blocks only for regular files */
-        if (!S_ISREG(inode->i_mode))
-                RETURN(0);
-
-        /* we shouldn't fetch size from OSTes during recovery - deadlock */
-        if (obd->obd_recovering) {
-                CERROR("size-on-mds has no support on OST yet\n");
-                RETURN(0);
-        }
-
-        /* if nobody modified attrs. we're lucky */
-        if (!mds_inode_has_old_attrs(inode))
-                RETURN(0);
-
-        /* 1: client didn't send actual i_size/i_blocks
-         * 2: we seem to be last writer
-         * 3: the file doesn't look like to be disappeared
-         * conclusion: we're gonna fetch them from OSSes */
-
-        down(&inode->i_sem);
-        len = fsfilt_get_md(obd, inode, NULL, 0, EA_LOV);
-        up(&inode->i_sem);
-        
-        if (len < 0) {
-                CERROR("error getting inode %lu MD: %d\n", inode->i_ino, len);
-                GOTO(cleanup, rc = len);
-        } else if (len == 0) {
-                CDEBUG(D_INODE, "no LOV in inode %lu\n", inode->i_ino);
-                GOTO(cleanup, rc = 0);
-        }
-
-        OBD_ALLOC(lmm, len);
-        if (lmm == NULL) {
-                CERROR("can't allocate memory\n");
-                GOTO(cleanup, rc = -ENOMEM);
-        }
-
-        down(&inode->i_sem);
-        rc = fsfilt_get_md(obd, inode, lmm, len, EA_LOV);
-        up(&inode->i_sem);
-        
-        if (rc < 0) {
-                CERROR("error getting inode %lu MD: %d\n", inode->i_ino, rc);
-                GOTO(cleanup, rc);
-        }
-
-        rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, lmm, len);
-        if (rc < 0) {
-                CERROR("error getting inode %lu MD: %d\n", inode->i_ino, rc);
-                GOTO(cleanup, rc);
-        }
-        
-        CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", inode->i_ino);
-        
-        flags = LDLM_FL_HAS_INTENT;
-        rc = obd_enqueue(obd->u.mds.mds_dt_exp, lsm, LDLM_EXTENT, &policy,
-                         LCK_PR, &flags, mds_extent_lock_callback,
-                         ldlm_completion_ast, NULL, NULL,
-                         sizeof(struct ost_lvb), lustre_swab_ost_lvb, &lockh);
-        if (rc == -ENOENT) {
-                /* while we were enqueueing lock on OST, another thread
-                 * unlinked the file and started OST object destoying.
-                 * it's safe to return 0 here */
-                GOTO(cleanup, rc = 0);
-        } else if (rc != 0) {
-                CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
-                GOTO(cleanup, rc);
-        }
-
-        CDEBUG(D_INODE, "LOV reports "LPD64"/%lu for "DLID4" [%s%s%s]\n",
-               inode->i_size, inode->i_blocks, OLID4(&body->id1),
-               atomic_read(&inode->i_writecount) > 1 ? "U" : "",
-               mds_inode_has_old_attrs(inode) ? "D" : "",
-               mds_inode_is_orphan(inode) ? "O" : "");
-
-        i_size_write(inode, lov_merge_size(lsm, 0));
-        inode->i_blocks = lov_merge_blocks(lsm);
-        LTIME_S(inode->i_mtime) = lov_merge_mtime(lsm, LTIME_S(inode->i_mtime));
-        iattr->ia_size = inode->i_size;
-        LTIME_S(iattr->ia_mtime) = LTIME_S(inode->i_mtime);
-        iattr->ia_valid |= ATTR_SIZE | ATTR_MTIME;
-        
-        DOWN_WRITE_I_ALLOC_SEM(inode);
-        mds_inode_unset_attrs_old(inode);
-        UP_WRITE_I_ALLOC_SEM(inode);
-
-        obd_cancel(obd->u.mds.mds_dt_exp, lsm, LCK_PR, &lockh);
-        
-cleanup:
-        if (lsm != NULL)
-                obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
-        if (lmm != NULL)
-                OBD_FREE(lmm, len);
-        RETURN(rc);
-}
-
 int mds_close(struct ptlrpc_request *req, int offset)
 {
         struct mds_export_data *med = &req->rq_export->exp_mds_data;
@@ -1885,26 +1428,28 @@ int mds_close(struct ptlrpc_request *req, int offset)
         struct mds_file_data *mfd;
         struct lvfs_run_ctxt saved;
         struct inode *inode;
-        int rc, repsize[3] = {sizeof(struct mds_body),
-                              obd->u.mds.mds_max_mdsize,
-                              obd->u.mds.mds_max_cookiesize};
+        int rc, repsize[4] = { sizeof(struct ptlrpc_body),
+                               sizeof(struct mds_body),
+                               obd->u.mds.mds_max_mdsize,
+                               obd->u.mds.mds_max_cookiesize };
+        struct mds_body *reply_body;
+        struct lov_mds_md *lmm;
+        int lmm_size;
+        struct llog_cookie *logcookies;
+        int cookies_size;
         ENTRY;
-        MD_COUNTER_INCREMENT(obd, close);
 
-        rc = lustre_pack_reply(req, 3, repsize, NULL);
-        if (rc) {
-                CERROR("lustre_pack_reply: rc = %d\n", rc);
+        rc = lustre_pack_reply(req, 4, repsize, NULL);
+        if (rc)
                 req->rq_status = rc;
-        } else {
+                /* continue on to drop local open even if we can't send reply */
+        else
                 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
-        }
 
-        if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
-                DEBUG_REQ(D_HA, req, "close replay");
-                memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
-                       lustre_msg_buf(req->rq_reqmsg, offset + 1, 0),
-                       req->rq_repmsg->buflens[2]);
-        }
+        CDEBUG(D_INODE, "close req->rep_len %d mdsize %d cookiesize %d\n",
+               req->rq_replen,
+               obd->u.mds.mds_max_mdsize, obd->u.mds.mds_max_cookiesize);
+        mds_counter_incr(req->rq_export, LPROC_MDS_CLOSE);
 
         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
                                   lustre_swab_mds_body);
@@ -1914,65 +1459,60 @@ int mds_close(struct ptlrpc_request *req, int offset)
                 RETURN(-EFAULT);
         }
 
-        if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
-                /* do some stuff */ ;
-
+        spin_lock(&med->med_open_lock);
         mfd = mds_handle2mfd(&body->handle);
         if (mfd == NULL) {
+                spin_unlock(&med->med_open_lock);
                 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
-                          ": cookie "LPX64, id_ino(&body->id1), body->handle.cookie);
+                          ": cookie "LPX64, body->fid1.id, body->handle.cookie);
                 req->rq_status = -ESTALE;
                 RETURN(-ESTALE);
         }
+        /* Remove mfd handle so it can't be found again.  We consume mfd_list
+         * reference here, but still have mds_handle2mfd ref until mfd_close. */
+        mds_mfd_unlink(mfd, 1);
+        spin_unlock(&med->med_open_lock);
 
         inode = mfd->mfd_dentry->d_inode;
-
-        /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
-        DOWN_WRITE_I_ALLOC_SEM(inode); /* mds_mfd_close drops this */
-
-        if (body->flags & MDS_BFLAG_DIRTY_EPOCH) {
-                /* the client modified data through the handle
-                 * we need to care about attrs. -bzzz */
-                mds_inode_set_attrs_old(inode);
-        }
-
+        /* child orphan sem protects orphan_dec_test && is_orphan race */
+        MDS_DOWN_WRITE_ORPHAN_SEM(inode); /* mds_mfd_close drops this */
         if (mds_inode_is_orphan(inode) && mds_orphan_open_count(inode) == 1) {
-                struct mds_body *rep_body;
+                body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
+                                      sizeof(*body));
+                LASSERT(body != NULL);
 
-                rep_body = lustre_msg_buf(req->rq_repmsg, 0,
-                                          sizeof (*rep_body));
-                LASSERT(rep_body != NULL);
-
-                mds_pack_inode2body(obd, rep_body, inode,
-                                    (body->valid & OBD_MD_FID) ? 1 : 0);
-                
-                mds_pack_md(obd, req->rq_repmsg, 1, rep_body, 
-                           inode, MDS_PACK_MD_LOCK, 0);
+                mds_pack_inode2fid(&body->fid1, inode);
+                mds_pack_inode2body(body, inode);
+                mds_pack_md(obd, req->rq_repmsg, REPLY_REC_OFF + 1, body, inode,
+                            MDS_PACK_MD_LOCK);
         }
-        spin_lock(&med->med_open_lock);
-        list_del(&mfd->mfd_list);
-        spin_unlock(&med->med_open_lock);
 
         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
-        req->rq_status = mds_mfd_close(req, offset, obd, mfd, 1);
+        reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*reply_body));
+        lmm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, 0);
+        lmm_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1),
+        logcookies = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 2, 0);
+        cookies_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 2);
+        req->rq_status = mds_mfd_close(req, offset, obd, mfd, 1,
+                                       lmm, lmm_size, logcookies, cookies_size,
+                                       &reply_body->valid);
         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 
+        mds_shrink_reply(obd, req, body, REPLY_REC_OFF + 1);
         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
                 req->rq_status = -ENOMEM;
-                mds_mfd_put(mfd);
                 RETURN(-ENOMEM);
         }
 
-        mds_mfd_put(mfd);
-        RETURN(0);
+        RETURN(rc);
 }
 
-
 int mds_done_writing(struct ptlrpc_request *req, int offset)
 {
         struct mds_body *body;
-        int rc, size = sizeof(struct mds_body);
+        int rc, size[2] = { sizeof(struct ptlrpc_body),
+                            sizeof(struct mds_body) };
         ENTRY;
 
         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
@@ -1985,11 +1525,9 @@ int mds_done_writing(struct ptlrpc_request *req, int offset)
                 RETURN(-EFAULT);
         }
 
-        rc = lustre_pack_reply(req, 1, &size, NULL);
-        if (rc) {
-                CERROR("lustre_pack_reply: rc = %d\n", rc);
+        rc = lustre_pack_reply(req, 2, size, NULL);
+        if (rc)
                 req->rq_status = rc;
-        }
 
         RETURN(0);
 }