Whamcloud - gitweb
r=adilger,phil
[fs/lustre-release.git] / lustre / mds / mds_open.c
index 89f2edb..e959402 100644 (file)
 #endif
 #define DEBUG_SUBSYSTEM S_MDS
 
+#include <linux/version.h>
 #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>
@@ -53,7 +55,7 @@
  * mds_mfd_close - for force closing files when a client dies
  */
 
-/* 
+/*
  * MDS file data handling: file data holds a handle for a file opened
  * by a client.
  */
@@ -111,43 +113,112 @@ static void mds_mfd_destroy(struct mds_file_data *mfd)
 }
 
 
-/* 
- * Write access to a file: executors cause a negative count, 
- * writers a positive count.  The spin lock is needed to perform
- * a check for the sign and then increment or decrement atomically.
- */
+/* Caller must hold mds->mds_epoch_sem */
+static int mds_alloc_filterdata(struct inode *inode)
+{
+        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;
+}
 
-static spinlock_t mds_exec_lock = SPIN_LOCK_UNLOCKED;
-static int mds_get_write_access(struct inode * inode)
+/* Caller must hold mds->mds_epoch_sem */
+static void mds_free_filterdata(struct inode *inode)
 {
-        ENTRY;
-        spin_lock(&mds_exec_lock);
+        LASSERT(inode->i_filterdata != NULL);
+        OBD_FREE(inode->i_filterdata, sizeof(struct mds_filter_data));
+        inode->i_filterdata = NULL;
+        iput(inode);
+}
+
+/* Write access to a file: executors cause a negative count,
+ * writers a positive count.  The semaphore is needed to perform
+ * a check for the sign and then increment or decrement atomically.
+ *
+ * This code is closely tied to the allocation of the d_fsdata and the
+ * MDS epoch, so we use the same semaphore for the whole lot.
+ *
+ * We could use a different semaphore for each file, if it ever shows
+ * up in a profile, which it won't.
+ *
+ * epoch argument is nonzero during recovery */
+static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
+                                __u64 epoch)
+{
+        int rc = 0;
+
+        down(&mds->mds_epoch_sem);
+
         if (atomic_read(&inode->i_writecount) < 0) {
-                spin_unlock(&mds_exec_lock);
+                up(&mds->mds_epoch_sem);
                 RETURN(-ETXTBSY);
         }
-        atomic_inc(&inode->i_writecount);
-        spin_unlock(&mds_exec_lock);
-        RETURN(0);
+
+
+        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,
+                       inode->i_generation);
+                goto out;
+        }
+
+        if (inode->i_filterdata == NULL)
+                mds_alloc_filterdata(inode);
+        if (inode->i_filterdata == NULL) {
+                rc = -ENOMEM;
+                goto out;
+        }
+        if (epoch > mds->mds_io_epoch)
+                mds->mds_io_epoch = epoch;
+        else
+                mds->mds_io_epoch++;
+        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);
+        up(&mds->mds_epoch_sem);
+        return rc;
 }
 
-static int mds_deny_write_access(struct inode *inode)
+/* Returns EAGAIN if the client needs to get size and/or cookies and close
+ * again -- which is never true if the file is about to be unlinked.  Otherwise
+ * returns the number of remaining writers. */
+static int mds_put_write_access(struct mds_obd *mds, struct inode *inode,
+                                struct mds_body *body, int unlinking)
 {
+        int rc = 0;
         ENTRY;
-        spin_lock(&mds_exec_lock);
-        if (atomic_read(&inode->i_writecount) > 0) {
-                spin_unlock(&mds_exec_lock);
-                RETURN(-ETXTBSY);
-        }
+
+        down(&mds->mds_epoch_sem);
         atomic_dec(&inode->i_writecount);
-        spin_unlock(&mds_exec_lock);
-        RETURN(0);
+        rc = atomic_read(&inode->i_writecount);
+        if (rc > 0)
+                GOTO(out, rc);
+#if 0
+        if (!unlinking && !(body->valid & OBD_MD_FLSIZE))
+                GOTO(out, rc = EAGAIN);
+#endif
+        mds_free_filterdata(inode);
+ out:
+        up(&mds->mds_epoch_sem);
+        return rc;
 }
 
-static void mds_put_write_access(struct inode * inode)
+static int mds_deny_write_access(struct mds_obd *mds, struct inode *inode)
 {
         ENTRY;
+        down(&mds->mds_epoch_sem);
+        if (atomic_read(&inode->i_writecount) > 0) {
+                up(&mds->mds_epoch_sem);
+                RETURN(-ETXTBSY);
+        }
         atomic_dec(&inode->i_writecount);
+        up(&mds->mds_epoch_sem);
+        RETURN(0);
 }
 
 static void mds_allow_write_access(struct inode *inode)
@@ -168,22 +239,28 @@ static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
                                              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 error;
         ENTRY;
-        
+
         mfd = mds_mfd_new();
         if (mfd == NULL) {
                 CERROR("mds: out of memory\n");
                 GOTO(cleanup_dentry, error = -ENOMEM);
         }
 
+        body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
+
         if (flags & FMODE_WRITE) {
-                error = mds_get_write_access(dentry->d_inode);
+                /* FIXME: in recovery, need to pass old epoch here */
+                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) {
-                error = mds_deny_write_access(dentry->d_inode);
+                error = mds_deny_write_access(mds, dentry->d_inode);
                 if (error)
                         GOTO(cleanup_mfd, error);
         }
@@ -201,6 +278,9 @@ static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
         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:
@@ -260,7 +340,8 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         }
 
         /* replay case */
-        if (rec->ur_fid2->id) {
+        if(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
+                LASSERT (rec->ur_fid2->id);
                 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
                 lmm_size = rec->ur_eadatalen;
                 lmm = rec->ur_eadata;
@@ -279,6 +360,10 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
                 RETURN(0);
         }
 
+        
+        if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_ALLOC_OBDO))
+                GOTO(out_ids, rc = -ENOMEM);
+
         oa = obdo_alloc();
         if (oa == NULL)
                 GOTO(out_ids, rc = -ENOMEM);
@@ -294,28 +379,38 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|OBD_MD_FLMTIME|
                         OBD_MD_FLCTIME);
 
-        /* check if things like lstripe/lfs stripe are sending us the ea */
-        if (rec->ur_flags & MDS_OPEN_HAS_EA) {
-                rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, mds->mds_osc_exp,
+        if (!(rec->ur_flags & MDS_OPEN_HAS_OBJS)) {
+                /* check if things like lstripe/lfs stripe are sending us the ea */
+                if (rec->ur_flags & MDS_OPEN_HAS_EA) {
+                        rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, 
+                                           mds->mds_osc_exp,
+                                           0, &lsm, rec->ur_eadata);
+                        if (rc)
+                                GOTO(out_oa, rc);
+                }
+                rc = obd_create(mds->mds_osc_exp, oa, &lsm, &oti);
+                if (rc) {
+                        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 "
+                                       "rc %d\n", rc);
+                                rc = -EIO;
+                        }
+                        GOTO(out_oa, rc);
+                }
+        } else {
+                rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_osc_exp,
                                    0, &lsm, rec->ur_eadata);
-                if (rc)
+                if (rc) {
                         GOTO(out_oa, rc);
-        }
-
-        rc = obd_create(mds->mds_osc_exp, oa, &lsm, &oti);
-        if (rc) {
-                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 rc %d\n", rc);
-                        rc = -EIO;
                 }
-                GOTO(out_oa, rc);
+                lsm->lsm_object_id = oa->o_id;
         }
-
         if (inode->i_size) {
                 oa->o_size = inode->i_size;
                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|
@@ -362,84 +457,10 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         RETURN(rc);
 }
 
-/* Caller must hold mds->mds_epoch_sem */
-static int mds_alloc_filterdata(struct inode *inode)
-{
-        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;
-}
-
-/* Caller must hold mds->mds_epoch_sem */
-static void mds_free_filterdata(struct inode *inode)
-{
-        LASSERT(inode->i_filterdata != NULL);
-        OBD_FREE(inode->i_filterdata, sizeof(struct mds_filter_data));
-        inode->i_filterdata = NULL;
-        iput(inode);
-}
-
-/* epoch argument is nonzero during recovery */
-static int mds_open_io_epoch(struct mds_obd *mds, struct inode *inode,
-                             __u64 epoch)
-{
-        int rc = 0;
-
-        down(&mds->mds_epoch_sem);
-        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,
-                       inode->i_generation);
-                goto out;
-        }
-
-        if (inode->i_filterdata == NULL)
-                mds_alloc_filterdata(inode);
-        if (inode->i_filterdata == NULL) {
-                rc = -ENOMEM;
-                goto out;
-        }
-        if (epoch > mds->mds_io_epoch)
-                mds->mds_io_epoch = epoch;
-        else
-                mds->mds_io_epoch++;
-        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:
-        up(&mds->mds_epoch_sem);
-        return rc;
-}
-
-/* Returns EAGAIN if the client needs to get size and/or cookies and close
- * again -- which is never true if the file is about to be unlinked. */
-static int mds_close_io_epoch(struct mds_obd *mds, struct inode *inode,
-                              struct mds_body *body, int unlinking)
-{
-        int rc = 0;
-        ENTRY;
-
-        down(&mds->mds_epoch_sem);
-        if (mds_query_write_access(inode) > 0)
-                GOTO(out, rc);
-#if 0
-        if (!unlinking && !(body->valid & OBD_MD_FLSIZE))
-                GOTO(out, rc = EAGAIN);
-#endif
-        mds_free_filterdata(inode);
- out:
-        up(&mds->mds_epoch_sem);
-        return rc;
-}
-
 static void reconstruct_open(struct mds_update_record *rec, int offset,
                              struct ptlrpc_request *req,
                              struct lustre_handle *child_lockh)
 {
-        struct ptlrpc_request *oldreq = req->rq_export->exp_outstanding_reply;
         struct mds_export_data *med = &req->rq_export->exp_mds_data;
         struct mds_client_data *mcd = med->med_mcd;
         struct mds_obd *mds = mds_req2mds(req);
@@ -537,10 +558,11 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                 mfd = NULL;
         }
 
-        if (oldreq != NULL) {
-                /* if we're not recovering, it had better be found */
-                LASSERT(mfd != NULL);
-        } else if (mfd == NULL) {
+#warning "XXX fixme"
+        /* 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) {
                 mntget(mds->mds_vfsmnt);
                 CERROR("Re-opened file \n");
                 mfd = mds_dentry_open(child, mds->mds_vfsmnt,
@@ -552,11 +574,6 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                 put_child = 0;
         }
 
-        if ((rec->ur_flags & FMODE_WRITE) &&
-            mds_open_io_epoch(mds, child->d_inode, 0) == 0)
-                body->io_epoch = MDS_FILTERDATA(child->d_inode)->io_epoch;
-        body->handle.cookie = mfd->mfd_handle.h_cookie;
-
  out_dput:
         if (put_child)
                 l_dput(child);
@@ -587,7 +604,7 @@ static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
         struct obd_device *obd = req->rq_export->exp_obd;
         struct mds_file_data *mfd = NULL;
         obd_id *ids = NULL; /* object IDs created */
-        int rc;
+        int rc = 0;
         ENTRY;
 
         /* atomically create objects if necessary */
@@ -624,19 +641,14 @@ static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
         if (IS_ERR(mfd))
                 RETURN(PTR_ERR(mfd));
 
-        if ((rec->ur_flags & FMODE_WRITE) &&
-            mds_open_io_epoch(mds, dchild->d_inode, 0) == 0)
-                body->io_epoch = MDS_FILTERDATA(dchild->d_inode)->io_epoch;
-        body->handle.cookie = mfd->mfd_handle.h_cookie;
-
         CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
                mfd->mfd_handle.h_cookie);
         if (ids != NULL) {
                 mds_lov_update_objids(obd, ids);
                 OBD_FREE(ids, sizeof(*ids) * mds->mds_lov_desc.ld_tgt_count);
         }
-        if (rc)
-                mds_mfd_destroy(mfd);
+        //if (rc)
+        //        mds_mfd_destroy(mfd);
         RETURN(rc);
 }
 
@@ -726,7 +738,7 @@ int mds_pin(struct ptlrpc_request *req)
 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
                        struct lustre_handle *child_lockh)
 {
-        struct ldlm_res_id child_res_id = { .name = { inode->i_ino } };
+        struct ldlm_res_id child_res_id = { .name = { inode->i_ino, 0, 1, 0 } };
         struct lustre_handle lockh;
         int lock_flags = 0;
         int rc;
@@ -734,16 +746,16 @@ int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
         if (child_lockh == NULL)
                 child_lockh = &lockh;
 
-        rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
-                              child_res_id, LDLM_PLAIN, NULL, 0,
-                              LCK_EX, &lock_flags, ldlm_completion_ast,
-                              mds_blocking_ast, NULL, child_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);
         if (rc != ELDLM_OK)
                 CERROR("ldlm_cli_enqueue: %d\n", rc);
         else if (child_lockh == &lockh)
                 ldlm_lock_decref(child_lockh, LCK_EX);
 
-        return rc;
+        RETURN(rc);
 }
 
 int mds_open(struct mds_update_record *rec, int offset,
@@ -778,15 +790,23 @@ int mds_open(struct mds_update_record *rec, int offset,
         /* 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). */
-        if (rec->ur_fid2->id) {
+        if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
+                DEBUG_REQ(D_HA, req, "open replay, disp: "LPX64"\n", 
+                          rep->lock_policy_res1);
+
+                LASSERT(rec->ur_fid2->id);
+                
                 rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
                                      rec, rep);
-                if (rc != -ENOENT)
+                if (rc != -ENOENT) 
                         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(!rec->ur_fid2->id);
         }
+
         LASSERT(offset == 2); /* If we got here, we must be called via intent */
 
         med = &req->rq_export->exp_mds_data;
@@ -814,8 +834,11 @@ int mds_open(struct mds_update_record *rec, int offset,
 
         /* Step 2: Lookup the child */
         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
-        if (IS_ERR(dchild))
-                GOTO(cleanup, rc = PTR_ERR(dchild));
+        if (IS_ERR(dchild)) {
+                rc = PTR_ERR(dchild);
+                dchild = NULL; /* don't confuse mds_finish_transno() below */
+                GOTO(cleanup, rc);
+        }
 
         cleanup_phase = 2; /* child dentry */
 
@@ -929,6 +952,16 @@ int mds_open(struct mds_update_record *rec, int offset,
         if ((rec->ur_flags & MDS_OPEN_DIRECTORY) &&
             !S_ISDIR(dchild->d_inode->i_mode))
                 GOTO(cleanup, rc = -ENOTDIR);
+        if (S_ISDIR(dchild->d_inode->i_mode)) { 
+                if (rec->ur_flags & MDS_OPEN_CREAT || rec->ur_flags & FMODE_WRITE) {
+                        /*we are tryying to create or write a exist dir*/
+                        GOTO(cleanup, rc = -EISDIR);
+                }
+                if (ll_permission(dchild->d_inode, acc_mode, NULL)) {
+                        GOTO(cleanup, rc = -EACCES);
+                }
+        }
 
         /* Step 5: mds_open it */
         rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
@@ -938,9 +971,6 @@ int mds_open(struct mds_update_record *rec, int offset,
  cleanup:
         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
                                 req, rc, rep ? rep->lock_policy_res1 : 0);
-        /* XXX what do we do here if mds_finish_transno itself failed? */
-        if (created)
-                mds_lock_new_child(obd, dchild->d_inode, NULL);
 
         switch (cleanup_phase) {
         case 2:
@@ -951,6 +981,8 @@ int mds_open(struct mds_update_record *rec, int offset,
                                        dchild->d_name.len, dchild->d_name.name,
                                        err);
                         }
+                } else if (created) {
+                        mds_lock_new_child(obd, dchild->d_inode, NULL);
                 }
                 l_dput(dchild);
         case 1:
@@ -961,8 +993,10 @@ int mds_open(struct mds_update_record *rec, int offset,
                 if (rc)
                         ldlm_lock_decref(&parent_lockh, parent_mode);
                 else
-                        ldlm_put_lock_into_req(req, &parent_lockh, parent_mode);
+                        ptlrpc_save_lock (req, &parent_lockh, parent_mode);
         }
+        if (rc == 0)
+                atomic_inc(&mds->mds_open_count);
         RETURN(rc);
 }
 
@@ -985,7 +1019,7 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
         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, *reply_body;
+        struct mds_body *request_body = NULL, *reply_body = NULL;
         struct dentry_params dp;
         ENTRY;
 
@@ -1001,19 +1035,18 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
         last_orphan = mds_open_orphan_dec_test(inode) &&
                 mds_inode_is_orphan(inode);
 
-        /* this is the actual "close" */
+        /* this is half of the actual "close" */
         if (mfd->mfd_mode & FMODE_WRITE) {
-                mds_put_write_access(inode);
+                rc = mds_put_write_access(mds, inode, request_body,
+                                          last_orphan && unlink_orphan);
         } else if (mfd->mfd_mode & FMODE_EXEC) {
                 mds_allow_write_access(inode);
         }
 
         if (last_orphan && unlink_orphan) {
-                LASSERT(mds_query_write_access(inode) == 0);
-                if (mfd->mfd_mode & FMODE_WRITE)
-                        rc = mds_close_io_epoch(mds, inode, request_body, 1);
+                LASSERT(rc == 0); /* mds_put_write_access must have succeeded */
 
-                CWARN("destroying orphan object %s\n", fidname);
+                CDEBUG(D_HA, "destroying orphan object %s\n", fidname);
 
                 /* Sadly, there is no easy way to save pending_child from
                  * mds_reint_unlink() into mfd, so we need to re-lookup,
@@ -1036,14 +1069,16 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
                         GOTO(cleanup, rc);
                 }
 
-#ifdef ENABLE_ORPHANS
                 if (req != NULL &&
                     (reply_body->valid & OBD_MD_FLEASIZE) &&
                     mds_log_op_unlink(obd, pending_child->d_inode,
-                                      req->rq_repmsg, 1) > 0) {
+                                lustre_msg_buf(req->rq_repmsg, 1, 0),
+                                req->rq_repmsg->buflens[1],
+                                lustre_msg_buf(req->rq_repmsg, 2, 0),
+                                req->rq_repmsg->buflens[2]) > 0) {
                         reply_body->valid |= OBD_MD_FLCOOKIE;
                 }
-#endif
+
                 pending_child->d_fsdata = (void *) &dp;
                 dp.p_inum = 0;
                 dp.p_ptr = req;
@@ -1053,16 +1088,15 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
                         rc = vfs_unlink(pending_dir, pending_child);
                 if (rc)
                         CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
-        } else if (mfd->mfd_mode & FMODE_WRITE &&
-                   mds_query_write_access(inode) == 0) {
-                // XXX this should probably be abstracted with mds_reint_setattr
+        } else if (mfd->mfd_mode & FMODE_WRITE && rc == 0) {
+                /* Update the on-disk attributes if this was the last write
+                 * close, and all information was provided (i.e., rc == 0)
+                 *
+                 * XXX this should probably be abstracted with mds_reint_setattr
+                 */
+#if 0
                 struct iattr iattr;
 
-                rc = mds_close_io_epoch(mds, inode, request_body, 0);
-                if (rc == EAGAIN)
-                        goto dput;
-
-#if 0
                 /* XXX can't set block count with fsfilt_setattr (!) */
                 iattr.ia_valid = ATTR_CTIME | ATTR_ATIME |
                         ATTR_MTIME | ATTR_SIZE;
@@ -1080,11 +1114,14 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
                         CERROR("error in setattr(%s): rc %d\n", fidname, rc);
 #endif
         }
- dput:
+        /* If other clients have this file open for write, rc will be > 0 */
+        if (rc > 0)
+                rc = 0;
         l_dput(mfd->mfd_dentry);
         mds_mfd_destroy(mfd);
 
  cleanup:
+        atomic_dec(&mds->mds_open_count);
         if (req) {
                 rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0);
         } else if (handle) {
@@ -1118,7 +1155,13 @@ int mds_close(struct ptlrpc_request *req)
                               obd->u.mds.mds_max_cookiesize};
         ENTRY;
 
-        MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
+        rc = lustre_pack_reply(req, 3, repsize, NULL);
+        if (rc) {
+                CERROR("lustre_pack_reply: rc = %d\n", rc);
+                req->rq_status = rc;
+        } else {
+                MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
+        }
 
         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
         if (body == NULL) {
@@ -1138,12 +1181,6 @@ int mds_close(struct ptlrpc_request *req)
                 RETURN(-ESTALE);
         }
 
-        rc = lustre_pack_reply(req, 3, repsize, NULL);
-        if (rc) {
-                CERROR("lustre_pack_reply: rc = %d\n", rc);
-                req->rq_status = rc;
-        }
-
         inode = mfd->mfd_dentry->d_inode;
         if (mds_inode_is_orphan(inode) && mds_open_orphan_count(inode) == 1) {
                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));