Whamcloud - gitweb
- additional debug to catch the case when client gets openhandle w/o capa
[fs/lustre-release.git] / lustre / mds / mds_open.c
index 9f3f125..f00cac4 100644 (file)
@@ -43,6 +43,7 @@
 #include <linux/obd_lov.h>
 #include <linux/lustre_fsfilt.h>
 #include <linux/lprocfs_status.h>
+#include <linux/lustre_gs.h>
 
 #include "mds_internal.h"
 
@@ -86,14 +87,14 @@ struct mds_file_data *mds_mfd_new(void)
         return mfd;
 }
 
-static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
+struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
 {
         ENTRY;
         LASSERT(handle != NULL);
         RETURN(class_handle2object(handle->cookie));
 }
 
-static void mds_mfd_put(struct mds_file_data *mfd)
+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);
@@ -111,13 +112,12 @@ static void mds_mfd_destroy(struct mds_file_data *mfd)
         mds_mfd_put(mfd);
 }
 
-#ifdef IFILTERDATA_ACTUALLY_USED
 /* 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)
+        LASSERT(LUSTRE_FILTERDATA(inode) == NULL);
+        OBD_ALLOC(LUSTRE_FILTERDATA(inode), sizeof(struct mds_filter_data));
+        if (LUSTRE_FILTERDATA(inode) == NULL)
                 return -ENOMEM;
         LASSERT(igrab(inode) == inode);
         return 0;
@@ -126,12 +126,11 @@ static int mds_alloc_filterdata(struct inode *inode)
 /* 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;
+        LASSERT(LUSTRE_FILTERDATA(inode) != NULL);
+        OBD_FREE(LUSTRE_FILTERDATA(inode), sizeof(struct mds_filter_data));
+        LUSTRE_FILTERDATA(inode) = NULL;
         iput(inode);
 }
-#endif /*IFILTERDATA_ACTUALLY_USED*/
 
 /* Write access to a file: executors cause a negative count,
  * writers a positive count.  The semaphore is needed to perform
@@ -156,7 +155,6 @@ static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
                 RETURN(-ETXTBSY);
         }
 
-#ifdef IFILTERDATA_ACTUALLY_USED
         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,
@@ -164,23 +162,23 @@ static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
                 goto out;
         }
 
-        if (inode->i_filterdata == NULL)
+        if (MDS_FILTERDATA(inode) == NULL)
                 mds_alloc_filterdata(inode);
-        if (inode->i_filterdata == NULL) {
+        if (MDS_FILTERDATA(inode) == NULL) {
                 rc = -ENOMEM;
                 goto out;
         }
-#endif /*IFILTERDATA_ACTUALLY_USED*/
-        if (epoch > mds->mds_io_epoch)
+        if (epoch > mds->mds_io_epoch) {
                 mds->mds_io_epoch = epoch;
-        else
+                CDEBUG(D_INODE, "repair MDS epoch "LPU64" for ino %lu/%u\n",
+                       mds->mds_io_epoch, inode->i_ino, inode->i_generation);
+        } else {
                 mds->mds_io_epoch++;
-#ifdef IFILTERDATA_ACTUALLY_USED
+                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:
-#endif /*IFILTERDATA_ACTUALLY_USED*/
         if (rc == 0)
                 atomic_inc(&inode->i_writecount);
         up(&mds->mds_epoch_sem);
@@ -205,9 +203,7 @@ static int mds_put_write_access(struct mds_obd *mds, struct inode *inode,
         if (!unlinking && !(body->valid & OBD_MD_FLSIZE))
                 GOTO(out, rc = EAGAIN);
 #endif
-#ifdef IFILTERDATA_ACTUALLY_USED
         mds_free_filterdata(inode);
-#endif
  out:
         up(&mds->mds_epoch_sem);
         return rc;
@@ -241,41 +237,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 ptlrpc_request *req,
+                                             struct mds_update_record *rec)
 {
         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;
+        int rc = 0;
         ENTRY;
 
         mfd = mds_mfd_new();
         if (mfd == NULL) {
                 CERROR("mds: out of memory\n");
-                GOTO(cleanup_dentry, error = -ENOMEM);
+                GOTO(cleanup_dentry, rc = -ENOMEM);
         }
 
         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
 
         if (flags & FMODE_WRITE) {
                 /* 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);
-#ifdef IFILTERDATA_ACTUALLY_USED
+                rc = mds_get_write_access(mds, dentry->d_inode, rec->ur_ioepoch);
+                if (rc)
+                        GOTO(cleanup_mfd, rc);
                 body->io_epoch = MDS_FILTERDATA(dentry->d_inode)->io_epoch;
-#endif /*IFILTERDATA_ACTUALLY_USED*/
         } else if (flags & FMODE_EXEC) {
-                error = mds_deny_write_access(mds, dentry->d_inode);
-                if (error)
-                        GOTO(cleanup_mfd, error);
+                rc = mds_deny_write_access(mds, dentry->d_inode);
+                if (rc)
+                        GOTO(cleanup_mfd, rc);
         }
 
         dget(dentry);
 
-        /* Mark the file as open to handle open-unlink. */
+        /* 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);
         mds_orphan_open_inc(dentry->d_inode);
         UP_WRITE_I_ALLOC_SEM(dentry->d_inode);
@@ -290,41 +286,42 @@ static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
         mds_mfd_put(mfd);
 
         body->handle.cookie = mfd->mfd_handle.h_cookie;
-
         RETURN(mfd);
-
 cleanup_mfd:
         mds_mfd_put(mfd);
         mds_mfd_destroy(mfd);
 cleanup_dentry:
-        return ERR_PTR(error);
+        return ERR_PTR(rc);
 }
 
-static void mds_objids_from_lmm(obd_id *ids, struct lov_mds_md *lmm,
-                                struct lov_desc *desc)
+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);
         }
 }
 
-/* Must be called with i_sem 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, 
-                              obd_id **ids)
+/* 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)
 {
-        struct obdo *oa = NULL;
+        struct inode *inode = dchild->d_inode;
+        struct mds_obd *mds = &obd->u.mds;
         struct obd_trans_info oti = { 0 };
-        struct mds_body *body;
         struct lov_stripe_md *lsm = NULL;
         struct lov_mds_md *lmm = NULL;
-        struct inode *inode = dchild->d_inode;
-        void *lmm_buf;
         int rc, lmm_bufsize, lmm_size;
+        struct obdo *oa = NULL;
+        struct mds_body *body;
+        void *lmm_buf;
         ENTRY;
 
         if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
@@ -342,7 +339,7 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         if (*ids == NULL)
                 RETURN(-ENOMEM);
         oti.oti_objid = *ids;
-
+                
         /* replay case */
         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
                 LASSERT(id_ino(rec->ur_id2));
@@ -352,19 +349,20 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
                 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;
-                        GOTO(out_oa, rc);
+                        RETURN(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);
+                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);
@@ -378,19 +376,19 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
 
         oa = obdo_alloc();
         if (oa == NULL)
-                GOTO(out_ids, rc = -ENOMEM);
+                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_FLGENER | OBD_MD_FLTYPE |
-                OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLGROUP;
+        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|OBD_MD_FLMTIME|
-                        OBD_MD_FLCTIME);
+        obdo_from_inode(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 */
@@ -415,15 +413,16 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
                         OBD_FREE(lmm, mds->mds_max_mdsize);
                         if (rc)
                                 GOTO(out_oa, rc);
-                } 
+                }
+
                 LASSERT(oa->o_gr >= FILTER_GROUP_FIRST_MDS);
-                rc = obd_create(mds->mds_dt_exp, oa, &lsm, &oti);
+                oti.oti_flags |= OBD_MODE_CROW;
+                rc = obd_create(mds->mds_dt_exp, oa, NULL, 0, &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",
+                        CDEBUG((rc == -ENOSPC ? D_INODE : D_ERROR),
+                               "error creating objects for "
+                               "inode %lu: rc = %d\n",
                                inode->i_ino, rc);
                         if (rc > 0) {
                                 CERROR("obd_create returned invalid "
@@ -435,17 +434,22 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         } else {
                 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_dt_exp,
                                    0, &lsm, rec->ur_eadata);
-                if (rc) {
+                if (rc)
                         GOTO(out_oa, rc);
-                }
+
                 lsm->lsm_object_id = oa->o_id;
                 lsm->lsm_object_gr = 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);
-                rc = obd_setattr(mds->mds_dt_exp, oa, lsm, &oti);
+                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 (rc) {
                         CERROR("error setting attrs for inode %lu: rc %d\n",
                                inode->i_ino, rc);
@@ -465,7 +469,11 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         rc = obd_packmd(mds->mds_dt_exp, &lmm, lsm);
         if (!id_ino(rec->ur_id2))
                 obd_free_memmd(mds->mds_dt_exp, &lsm);
-        LASSERT(rc >= 0);
+        if (rc < 0) {
+                CERROR("cannot pack lsm, err = %d\n", rc);
+                GOTO(out_oa, rc);
+        }
+
         lmm_size = rc;
         body->eadatasize = rc;
 
@@ -474,7 +482,7 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         if (IS_ERR(*handle)) {
                 rc = PTR_ERR(*handle);
                 *handle = NULL;
-                GOTO(out_ids, rc);
+                GOTO(out_oa, rc);
         }
 
         rc = fsfilt_set_md(obd, inode, *handle, lmm,
@@ -487,19 +495,69 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
 
         memcpy(lmm_buf, lmm, lmm_size);
         obd_free_diskmd(mds->mds_dt_exp, &lmm);
- out_oa:
+out_oa:
         oti_free_cookies(&oti);
         obdo_free(oa);
- out_ids:
+out_ids:
         if (rc) {
                 OBD_FREE(*ids, mds->mds_dt_desc.ld_tgt_count * sizeof(**ids));
                 *ids = NULL;
         }
-        if(lsm)
+        if (lsm)
                 obd_free_memmd(mds->mds_dt_exp, &lsm);
         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)
@@ -509,7 +567,7 @@ 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, *dchild;
+        struct dentry *parent = NULL, *dchild;
         struct ldlm_reply *rep;
         struct mds_body *body;
         struct list_head *t;
@@ -531,14 +589,25 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                 return; /* error looking up parent or child */
         }
 
-        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));
+        /* 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));
+        }
 
         if (!dchild->d_inode)
                 GOTO(out_dput, 0); /* child not present to open */
@@ -563,6 +632,7 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                 if (!(body->valid & OBD_MD_FLEASIZE))
                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
+                DEBUG_REQ(D_ERROR, req, "no capa for "DLID4, OLID4(&body->id1));
         }
         
         /* If we have -EEXIST as the status, and we were asked to create
@@ -602,7 +672,7 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                 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->ur_flags & ~MDS_OPEN_TRUNC, req, rec);
                 if (!mfd) {
                         CERROR("mds: out of memory\n");
                         GOTO(out_dput, req->rq_status = -ENOMEM);
@@ -617,12 +687,13 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
  out_dput:
         if (put_child)
                 l_dput(dchild);
-        l_dput(parent);
+        if (parent)
+                l_dput(parent);
         EXIT;
 }
 
 /* do NOT or the MAY_*'s, you'll get the weakest */
-static int accmode(int flags)
+int accmode(int flags)
 {
         int res = 0;
 
@@ -640,17 +711,19 @@ 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_obd *mds = mds_req2mds(req);
         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 mds_file_data *mfd = NULL;
-        obd_id *ids = NULL; /* object IDs created */
+        obd_id *ids = NULL;
         unsigned mode;
-        int rc = 0;
+        int rc = 0, reply_off;
         ENTRY;
 
         /* atomically create objects if necessary */
         down(&dchild->d_inode->i_sem);
         mode = dchild->d_inode->i_mode;
+
         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,
@@ -660,23 +733,25 @@ static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
                         RETURN(rc);
                 }
         }
+
         if (rec != NULL) {
-                /* no EA: create objects */
                 if ((body->valid & OBD_MD_FLEASIZE) &&
                     (rec->ur_flags & MDS_OPEN_HAS_EA)) {
                         up(&dchild->d_inode->i_sem);
                         RETURN(-EEXIST);
                 }
+                
                 if (!(body->valid & OBD_MD_FLEASIZE)) {
                         /* no EA: create objects */
-                        rc = mds_create_objects(req, 2, rec, mds, obd,
+                        rc = mds_create_objects(obd, req, 2, rec,
                                                 dchild, handle, &ids);
                         if (rc) {
-                                CERROR("mds_create_objects: rc = %d\n", rc);
+                                CERROR("mds_create_object: rc = %d\n", rc);
                                 up(&dchild->d_inode->i_sem);
                                 RETURN(rc);
                         }
                 }
+                
                 if (S_ISREG(dchild->d_inode->i_mode) &&
                     (body->valid & OBD_MD_FLEASIZE)) {
                         rc = mds_revalidate_lov_ea(obd, dchild->d_inode,
@@ -690,34 +765,66 @@ static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
                         }
                 }
         }
-        rc = mds_pack_acl(obd, req->rq_repmsg, 3, body, dchild->d_inode);
+
+        reply_off = 3;
+        rc = mds_pack_acl(req, reply_off, body, dchild->d_inode);
+        reply_off += 2;
+
         if (rc < 0) {
-                CERROR("mds_pack_acl: rc = %d\n", rc);
+                CERROR("pack posix acl: rc = %d\n", rc);
                 up(&dchild->d_inode->i_sem);
                 RETURN(rc);
         }
 
+        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 (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 the inode has no EA data, then MDSs hold size, mtime */
-        if (S_ISREG(dchild->d_inode->i_mode) &&
-            !(body->valid & OBD_MD_FLEASIZE)) {
+        if (S_ISREG(mode) && !(body->valid & OBD_MD_FLEASIZE)) {
                 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
                                 OBD_MD_FLATIME | OBD_MD_FLMTIME);
         }
+
         up(&dchild->d_inode->i_sem);
 
         intent_set_disposition(rep, DISP_OPEN_OPEN);
-        mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
+        mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req, rec);
         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 (rc)
-        //        mds_mfd_destroy(mfd);
+
         RETURN(rc);
 }
 
@@ -761,10 +868,8 @@ static int mds_open_by_id(struct ptlrpc_request *req,
         }
         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.
-         */
+        /* 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));
@@ -818,7 +923,7 @@ int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
 {
         struct ldlm_res_id child_res_id = { .name = { inode->i_ino, 0, 1, 0 } };
         struct lustre_handle lockh;
-        int lock_flags = 0;
+        int lock_flags = LDLM_FL_ATOMIC_CB;
         int rc;
         ENTRY;
 
@@ -855,11 +960,8 @@ int mds_open(struct mds_update_record *rec, int offset,
         struct mea *mea = NULL;
         int mea_size, update_mode;
         int child_mode = LCK_PR;
-        /* 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;
+        struct lustre_id sid;
+        __u64 fid = 0;
         ENTRY;
 
         DEBUG_REQ(D_INODE, req, "parent "DLID4" name %*s mode %o",
@@ -893,8 +995,8 @@ int mds_open(struct mds_update_record *rec, int offset,
 
                 LASSERT(id_ino(rec->ur_id2));
 
-                rc = mds_open_by_id(req, rec->ur_id2, body, rec->ur_flags,
-                                    rec, rep);
+                rc = mds_open_by_id(req, rec->ur_id2, body,
+                                    rec->ur_flags, rec, rep);
                 if (rc != -ENOENT) {
                         mds_body_do_reverse_map(med, body);
                         RETURN(rc);
@@ -962,7 +1064,7 @@ restart:
         if (rc)
                 GOTO(cleanup, rc);
        
-        if (mea != NULL) {
+        if (mea != NULL && mea->mea_count) {
                 /*
                  * dir is already splitted, check is requested filename should *
                  * live at this MDS or at another one.
@@ -993,12 +1095,17 @@ got_child:
 
         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);
@@ -1015,14 +1122,19 @@ got_child:
                 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)) {
-                        /* wow! someone unlink and create new one yet */
-                        CDEBUG(D_OTHER, "nice race, repeat lookup\n");
-                        ldlm_lock_decref(parent_lockh, parent_mode);
-                        ldlm_lock_decref(child_lockh, LCK_PR);
+                        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);
-                        LASSERT(rec->ur_namelen > 1);
                         GOTO(restart, rc);
                 }
 
@@ -1030,11 +1142,6 @@ got_child:
                 intent_set_disposition(rep, DISP_LOOKUP_POS);
                 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
 
-#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);
                 if (mea)
                         OBD_FREE(mea, mea_size);
                 l_dput(dchild);
@@ -1055,6 +1162,14 @@ got_child:
                 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,
@@ -1088,9 +1203,17 @@ 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;
 
                 rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode, NULL);
                 if (dchild->d_fsdata == (void *)(unsigned long)ino)
@@ -1100,7 +1223,9 @@ 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);
                         
@@ -1137,37 +1262,20 @@ got_child:
                         MD_COUNTER_INCREMENT(obd, create);
                 }
 
-                down(&dchild->d_inode->i_sem);
-                if (ino) {
-                        rc = mds_update_inode_sid(obd, dchild->d_inode,
-                                                  handle, rec->ur_id2);
-                        if (rc) {
-                                CERROR("mds_update_inode_sid() failed, "
-                                       "rc = %d\n", rc);
-                        }
-                        id_assign_fid(&body->id1, rec->ur_id2);
+                mds_inode2id(obd, &body->id1, inode, fid);
+                mds_update_inode_ids(obd, dchild->d_inode, handle, &body->id1, &sid);
 
-                        /* 
-                         * make sure, that fid is up-to-date.
-                         */
-                        mds_set_last_fid(obd, id_fid(rec->ur_id2));
-                } else {
-                        rc = mds_alloc_inode_sid(obd, dchild->d_inode,
-                                                 handle, &body->id1);
+                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("mds_alloc_inode_sid() failed, "
-                                       "rc = %d\n", rc);
+                                CERROR("error in set gs key rc %d\n", rc); 
                         }
                 }
-                up(&dchild->d_inode->i_sem);
-                
-                if (!(rec->ur_flags & O_EXCL)) { /* bug 3313 */
-                        rc = fsfilt_commit(obd, dchild->d_inode->i_sb,
-                                           dchild->d_inode, handle, 
-                                           req->rq_export->exp_sync);
-                        handle = NULL;
-                }
 
+                rc = fsfilt_commit(obd, dchild->d_inode->i_sb, dchild->d_inode, handle, 0);
+                handle = NULL;
                 acc_mode = 0;           /* Don't check for permissions */
         }
         mds_pack_inode2body(obd, body, dchild->d_inode, 1);
@@ -1225,15 +1333,23 @@ got_child:
         if ((rec->ur_flags & MDS_OPEN_DIRECTORY) &&
             !S_ISDIR(dchild->d_inode->i_mode))
                 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(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)
@@ -1243,23 +1359,20 @@ got_child:
         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 lustre_id sid;
-               
-               down(&dchild->d_inode->i_sem);
-                rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
-                up(&dchild->d_inode->i_sem);
-                if (rc) {
-                        CERROR("Can't read inode self id, "
-                               "inode %lu, rc %d\n",
-                               dchild->d_inode->i_ino, rc);
-                        GOTO(cleanup, rc);
-                }
+                struct ldlm_res_id child_res_id = { .name = {0}};
+                ldlm_policy_data_t policy;
+                int lock_flags = LDLM_FL_ATOMIC_CB;
                 
-               /* In case of replay we do not get a lock assuming that the
-                   caller has it already */
-                child_res_id.name[0] = id_fid(&sid);
-                child_res_id.name[1] = id_group(&sid);
+                /* 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,
@@ -1271,10 +1384,52 @@ got_child:
 
                 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");
+        }
 
        EXIT;
 cleanup:
@@ -1299,6 +1454,12 @@ cleanup_no_trans:
                 } else if (created) {
                         mds_lock_new_child(obd, dchild->d_inode, NULL);
                 }
+                /* 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)
@@ -1367,6 +1528,28 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                 reply_body = lustre_msg_buf(req->rq_repmsg, 0,
                                             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);
+        }
+
         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));
@@ -1388,12 +1571,6 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                 int stripe_count = 0;
                 LASSERT(rc == 0); /* mds_put_write_access must have succeeded */
 
-                if (obd->obd_recovering) {
-                        CDEBUG(D_HA, "not remove orphan %s until recovery"
-                               " is over\n", idname);
-                        GOTO(out, rc);
-                }
-
                 CDEBUG(D_HA, "destroying orphan object %s\n", idname);
                 
                 if ((S_ISREG(inode->i_mode) && inode->i_nlink != 1) ||
@@ -1412,7 +1589,10 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                                                idlen);
                 if (IS_ERR(pending_child))
                         GOTO(cleanup, rc = PTR_ERR(pending_child));
-                LASSERT(pending_child->d_inode != NULL);
+                if (pending_child->d_inode == NULL) {
+                        CERROR("orphan %s has been removed\n", idname);
+                        GOTO(cleanup, rc = 0);
+                }
 
                 cleanup_phase = 2; /* dput(pending_child) when finished */
                 if (S_ISDIR(pending_child->d_inode->i_mode)) {
@@ -1452,8 +1632,18 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                                       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;
+               }
 
                 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
@@ -1502,7 +1692,8 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
         }
 
         if (iattr.ia_valid != 0) {
-                handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
+                if (handle == NULL)
+                        handle = fsfilt_start(obd,inode,FSFILT_OP_SETATTR,NULL);
                 if (IS_ERR(handle))
                         GOTO(cleanup, rc = PTR_ERR(handle));
                 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
@@ -1513,6 +1704,13 @@ 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);
 
@@ -1546,6 +1744,140 @@ out:
         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;
@@ -1569,7 +1901,7 @@ int mds_close(struct ptlrpc_request *req, int offset)
         }
 
         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
-                DEBUG_REQ(D_HA, req, "close replay\n");
+                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]);
@@ -1595,8 +1927,16 @@ int mds_close(struct ptlrpc_request *req, int offset)
         }
 
         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);
+        }
+
         if (mds_inode_is_orphan(inode) && mds_orphan_open_count(inode) == 1) {
                 struct mds_body *rep_body;
 
@@ -1629,6 +1969,7 @@ int mds_close(struct ptlrpc_request *req, int offset)
         RETURN(0);
 }
 
+
 int mds_done_writing(struct ptlrpc_request *req, int offset)
 {
         struct mds_body *body;