Whamcloud - gitweb
- CROW-related fixes from b_hd_mdref
[fs/lustre-release.git] / lustre / mds / mds_open.c
index 07ea3d8..e76ff94 100644 (file)
@@ -40,7 +40,6 @@
 #endif
 
 #include <linux/obd_class.h>
-#include <linux/lustre_mds.h>
 #include <linux/obd_lov.h>
 #include <linux/lustre_fsfilt.h>
 #include <linux/lprocfs_status.h>
@@ -112,7 +111,7 @@ 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)
 {
@@ -132,6 +131,7 @@ static void mds_free_filterdata(struct inode *inode)
         inode->i_filterdata = 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 +156,7 @@ 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,
@@ -170,14 +170,17 @@ static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
                 rc = -ENOMEM;
                 goto out;
         }
+#endif /*IFILTERDATA_ACTUALLY_USED*/
         if (epoch > mds->mds_io_epoch)
                 mds->mds_io_epoch = epoch;
         else
                 mds->mds_io_epoch++;
+#ifdef IFILTERDATA_ACTUALLY_USED
         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);
@@ -202,7 +205,9 @@ 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;
@@ -233,7 +238,7 @@ int mds_query_write_access(struct inode *inode)
         RETURN(atomic_read(&inode->i_writecount));
 }
 
-/* This replaces the VFS mds_dentry_open, it manages mfd and writecount */
+/* 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)
@@ -242,33 +247,37 @@ static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
         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);
+                rc = mds_get_write_access(mds, dentry->d_inode, 0);
+                if (rc)
+                        GOTO(cleanup_mfd, rc);
+#ifdef IFILTERDATA_ACTUALLY_USED
                 body->io_epoch = MDS_FILTERDATA(dentry->d_inode)->io_epoch;
+#endif
         } 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. */
-        mds_open_orphan_inc(dentry->d_inode);
+        /* 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);
 
         mfd->mfd_mode = flags;
         mfd->mfd_dentry = dentry;
@@ -280,41 +289,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;
+        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 ||
@@ -328,80 +338,97 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         if (body->valid & OBD_MD_FLEASIZE)
                 RETURN(0);
 
-        OBD_ALLOC(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
+        OBD_ALLOC(*ids, mds->mds_dt_desc.ld_tgt_count * sizeof(**ids));
         if (*ids == NULL)
                 RETURN(-ENOMEM);
         oti.oti_objid = *ids;
-
+                
         /* replay case */
-        if(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
-                LASSERT (rec->ur_fid2->id);
+        if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
+                LASSERT(id_ino(rec->ur_id2));
                 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;
-                        GOTO(out_ids, rc);
+                        RETURN(rc);
                 }
 
-                mds_objids_from_lmm(*ids, lmm, &mds->mds_lov_desc);
+                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);
+                rc = fsfilt_set_md(obd, inode, *handle, lmm,
+                                   lmm_size, EA_LOV);
                 if (rc)
                         CERROR("open replay failed to set md:%d\n", rc);
                 RETURN(0);
         }
 
         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_ALLOC_OBDO))
-                GOTO(out_ids, rc = -ENOMEM);
+                RETURN(-ENOMEM);
 
         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 */
                 if (rec->ur_flags & MDS_OPEN_HAS_EA) {
                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
-                                           mds->mds_osc_exp,
+                                           mds->mds_dt_exp,
                                            0, &lsm, rec->ur_eadata);
                         if (rc)
                                 GOTO(out_oa, rc);
                 } else {
-                        /* Per-directory striping default code removed, because
-                         * it uses the same unnamed EA storage as the directory
-                         * striping for CMD. -p */
-                } 
+                        OBD_ALLOC(lmm, mds->mds_max_mdsize);
+                        if (lmm == NULL)
+                                GOTO(out_oa, rc = -ENOMEM);
+
+                        lmm_size = mds->mds_max_mdsize;
+                        rc = mds_get_md(obd, dchild->d_parent->d_inode,
+                                        lmm, &lmm_size, 1, 0);
+                        if (rc > 0)
+                                rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
+                                                   mds->mds_dt_exp,
+                                                   0, &lsm, lmm);
+                        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_osc_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 "
@@ -411,19 +438,20 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
                         GOTO(out_oa, rc);
                 }
         } else {
-                rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_osc_exp,
+                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_osc_exp, oa, lsm, &oti);
+                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);
                 if (rc) {
                         CERROR("error setting attrs for inode %lu: rc %d\n",
                                inode->i_ino, rc);
@@ -440,10 +468,14 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
 
         LASSERT(lsm && lsm->lsm_object_id);
         lmm = NULL;
-        rc = obd_packmd(mds->mds_osc_exp, &lmm, lsm);
-        if (!rec->ur_fid2->id)
-                obd_free_memmd(mds->mds_osc_exp, &lsm);
-        LASSERT(rc >= 0);
+        rc = obd_packmd(mds->mds_dt_exp, &lmm, lsm);
+        if (!id_ino(rec->ur_id2))
+                obd_free_memmd(mds->mds_dt_exp, &lsm);
+        if (rc < 0) {
+                CERROR("cannot pack lsm, err = %d\n", rc);
+                GOTO(out_oa, rc);
+        }
+
         lmm_size = rc;
         body->eadatasize = rc;
 
@@ -452,28 +484,78 @@ 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, lmm_size);
+        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];
         LASSERT(lmm_buf);
         LASSERT(lmm_bufsize >= lmm_size);
 
         memcpy(lmm_buf, lmm, lmm_size);
-        obd_free_diskmd(mds->mds_osc_exp, &lmm);
- out_oa:
+        obd_free_diskmd(mds->mds_dt_exp, &lmm);
+out_oa:
         oti_free_cookies(&oti);
         obdo_free(oa);
- out_ids:
-        if (rc) {
-                OBD_FREE(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
-                *ids = NULL;
-        }
+
+        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)
@@ -483,15 +565,15 @@ 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, *child;
+        struct dentry *parent = NULL, *dchild;
         struct ldlm_reply *rep;
         struct mds_body *body;
-        int rc;
         struct list_head *t;
         int put_child = 1;
+        int rc;
         ENTRY;
 
-        LASSERT(offset == 2);                  /* only called via intent */
+        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));
 
@@ -505,16 +587,21 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
                 return; /* error looking up parent or child */
         }
 
-        parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
-        LASSERTF(!IS_ERR(parent), "fid "LPU64"/%u rc %ld\n", rec->ur_fid1->id,
-                 rec->ur_fid1->generation, PTR_ERR(parent));
-
-        child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
-        LASSERTF(!IS_ERR(child), "parent "LPU64"/%u child %s rc %ld\n",
-                 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
-                 PTR_ERR(child));
+        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 (!child->d_inode)
+        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
@@ -524,11 +611,11 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
             req->rq_status)
                 GOTO(out_dput, 0);
 
-        mds_pack_inode2fid(obd, &body->fid1, child->d_inode);
-        mds_pack_inode2body(obd, body, child->d_inode);
-        if (S_ISREG(child->d_inode->i_mode)) {
+        /* get lock (write for O_CREAT, read otherwise) */
+        mds_pack_inode2body(obd, body, dchild->d_inode, 1);
+        if (S_ISREG(dchild->d_inode->i_mode)) {
                 rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
-                                 child->d_inode, 1);
+                                 dchild->d_inode, 1, 0);
 
                 if (rc)
                         LASSERT(rc == req->rq_status);
@@ -537,13 +624,8 @@ 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);
-        } else {
-                /* XXX need to check this case */
         }
-
-        /* If we're opening a file without an EA, change to a write
-           lock (unless we already have one). */
-
+        
         /* If we have -EEXIST as the status, and we were asked to create
          * exclusively, we can tell we failed because the file already existed.
          */
@@ -580,7 +662,7 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
         if (mfd == NULL) {
                 mntget(mds->mds_vfsmnt);
                 CERROR("Re-opened file \n");
-                mfd = mds_dentry_open(child, mds->mds_vfsmnt,
+                mfd = mds_dentry_open(dchild, mds->mds_vfsmnt,
                                       rec->ur_flags & ~MDS_OPEN_TRUNC, req);
                 if (!mfd) {
                         CERROR("mds: out of memory\n");
@@ -595,8 +677,9 @@ static void reconstruct_open(struct mds_update_record *rec, int offset,
 
  out_dput:
         if (put_child)
-                l_dput(child);
-        l_dput(parent);
+                l_dput(dchild);
+        if (parent)
+                l_dput(parent);
         EXIT;
 }
 
@@ -617,12 +700,12 @@ static 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 mds_obd *mds = mds_req2mds(req);
         struct obd_device *obd = req->rq_export->exp_obd;
+        struct mds_obd *mds = mds_req2mds(req);
         struct mds_file_data *mfd = NULL;
-        obd_id *ids = NULL; /* object IDs created */
+        obd_id *ids = NULL;
         unsigned mode;
         int rc = 0;
         ENTRY;
@@ -630,43 +713,63 @@ static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
         /* 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,
-                                 dchild->d_inode, 0);
+                                 dchild->d_inode, 0, 0);
                 if (rc) {
                         up(&dchild->d_inode->i_sem);
                         RETURN(rc);
                 }
         }
+
         if (rec != NULL) {
-                /* no EA: create objects */
-                rc = mds_create_objects(req, 2, rec, mds, obd,
-                                        dchild, handle, &ids);
-                if (rc) {
-                        CERROR("mds_create_objects: rc = %d\n", rc);
+                if ((body->valid & OBD_MD_FLEASIZE) &&
+                    (rec->ur_flags & MDS_OPEN_HAS_EA)) {
                         up(&dchild->d_inode->i_sem);
-                        RETURN(rc);
+                        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);
+                                RETURN(rc);
+                        }
                 }
+                
                 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);
+                                                 dchild->d_inode, 0, 0);
                         if (rc) {
                                 up(&dchild->d_inode->i_sem);
                                 RETURN(rc);
                         }
                 }
         }
+        
+        rc = mds_pack_acl(obd, req->rq_repmsg, 3, body, dchild->d_inode);
+        if (rc < 0) {
+                CERROR("mds_pack_acl: rc = %d\n", rc);
+                up(&dchild->d_inode->i_sem);
+                RETURN(rc);
+        }
+
         /* 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)) {
                 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);
@@ -676,59 +779,66 @@ static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
 
         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);
+                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);
 }
 
-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)
+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)
 {
         struct mds_obd *mds = mds_req2mds(req);
         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
         struct dentry *dchild;
-        char fidname[LL_FID_NAMELEN];
-        int fidlen = 0, rc;
+        char idname[LL_ID_NAMELEN];
+        int idlen = 0, rc;
         void *handle = NULL;
         ENTRY;
 
         down(&pending_dir->i_sem);
-        fidlen = ll_fid2str(fidname, fid->id, fid->generation);
-        dchild = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
+        idlen = ll_id2str(idname, id_ino(id), id_gen(id));
+        dchild = lookup_one_len(idname, mds->mds_pending_dir,
+                                idlen);
         if (IS_ERR(dchild)) {
                 up(&pending_dir->i_sem);
                 rc = PTR_ERR(dchild);
-                CERROR("error looking up %s in PENDING: rc = %d\n",fidname, rc);
+                CERROR("error looking up %s in PENDING: rc = %d\n", 
+                       idname, rc);
                 RETURN(rc);
         }
 
+        up(&pending_dir->i_sem);
         if (dchild->d_inode != NULL) {
-                up(&pending_dir->i_sem);
                 mds_inode_set_orphan(dchild->d_inode);
-                mds_pack_inode2fid(req2obd(req), &body->fid1, dchild->d_inode);
-                mds_pack_inode2body(req2obd(req), body, 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",
-                       fidname);
+                       idname);
                 goto open;
         }
-        dput(dchild);
-        up(&pending_dir->i_sem);
+        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_fid2dentry(mds, fid, NULL);
+        /*
+         * 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));
 
-        mds_pack_inode2fid(req2obd(req), &body->fid1, dchild->d_inode);
-        mds_pack_inode2body(req2obd(req), body, 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);
 
@@ -737,12 +847,11 @@ static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
         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? */
-
         l_dput(dchild);
         RETURN(rc);
 }
 
-int mds_pin(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;
@@ -750,7 +859,8 @@ int mds_pin(struct ptlrpc_request *req)
         int rc, size = sizeof(*reply_body);
         ENTRY;
 
-        request_body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*request_body));
+        request_body = lustre_msg_buf(req->rq_reqmsg, offset,
+                                      sizeof(*request_body));
 
         rc = lustre_pack_reply(req, 1, &size, NULL);
         if (rc)
@@ -758,16 +868,18 @@ int mds_pin(struct ptlrpc_request *req)
         reply_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply_body));
 
         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
-        rc = mds_open_by_fid(req, &request_body->fid1, reply_body,
-                             request_body->flags, NULL, NULL);
+        rc = mds_open_by_id(req, &request_body->id1, reply_body,
+                            request_body->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 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.
+ */
 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
                        struct lustre_handle *child_lockh)
 {
@@ -775,14 +887,15 @@ int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
         struct lustre_handle lockh;
         int lock_flags = 0;
         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);
+                              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)
@@ -791,51 +904,42 @@ int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
         RETURN(rc);
 }
 
-static int is_mount_object(struct dentry *dparent)
-{
-        struct dentry *dchild;
-
-        if (!(dparent->d_inode->i_mode & S_ISUID))
-                return 0;
-
-        dchild = lookup_one_len(".mntinfo", dparent, strlen(".mntinfo"));
-        if (IS_ERR(dchild) || dchild == NULL)
-                return 0;
-
-        dput(dchild);
-        return 1;
-}
-
 int mds_open(struct mds_update_record *rec, int offset,
              struct ptlrpc_request *req, struct lustre_handle *child_lockh)
 {
         /* 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 mds_export_data *med;
-        struct lustre_handle parent_lockh[2];
+        struct lustre_handle parent_lockh[2] = {{0}, {0}};
         int rc = 0, cleanup_phase = 0, acc_mode, created = 0;
         int parent_mode = LCK_PR;
         void *handle = NULL;
         struct dentry_params dp;
         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;
         ENTRY;
 
-        DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %*s mode %o",
-                  rec->ur_fid1->id, rec->ur_fid1->generation,
-                  rec->ur_namelen - 1, rec->ur_name, rec->ur_mode);
+        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);
 
-        parent_lockh[0].cookie = 0;
-        parent_lockh[1].cookie = 0;
+        OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
+                         (obd_timeout + 1) / 4);
 
-        if (offset == 2) { /* intent */
+        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 == 0) { /* non-intent reint */
+        } else if (offset == 1) { /* non-intent reint */
                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
         } else {
                 body = NULL;
@@ -844,33 +948,34 @@ int mds_open(struct mds_update_record *rec, int offset,
 
         MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
 
-        MDS_UPDATE_COUNTER(mds, MDS_OPEN_COUNT);
-
-        /* 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). */
+        /*
+         * 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) {
-                DEBUG_REQ(D_HA, req, "open replay");
-                CDEBUG(D_HA, "open fid "LPU64"/%u name %*s mode %o\n",
-                          rec->ur_fid2->id, rec->ur_fid2->generation,
-                          rec->ur_namelen - 1, rec->ur_name, rec->ur_mode);
+                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(rec->ur_fid2->id);
+                LASSERT(id_ino(rec->ur_id2));
 
-                rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
-                                     rec, rep);
-                if (rc != -ENOENT)
+                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);
+                }
+
                 /* 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(!id_ino(rec->ur_id2));
         }
 
-        LASSERT(offset == 2); /* If we got here, we must be called via intent */
+        LASSERT(offset == 3); /* If we got here, we must be called via intent */
 
-        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);
@@ -879,7 +984,7 @@ int mds_open(struct mds_update_record *rec, int offset,
         acc_mode = accmode(rec->ur_flags);
 
         /* Step 1: Find and lock the parent */
-        if (rec->ur_flags & O_CREAT) {
+        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 */
@@ -887,27 +992,32 @@ int mds_open(struct mds_update_record *rec, int offset,
         }
         
         if (rec->ur_namelen == 1) {
-                /* client (LMV) wants to open the file by fid */
-                CDEBUG(D_OTHER, "OPEN by fid %u/%u/%u\n",
-                                (unsigned) rec->ur_fid1->mds,
-                                (unsigned) rec->ur_fid1->id,
-                                (unsigned) rec->ur_fid1->generation);
-                dchild = mds_fid2dentry(mds, rec->ur_fid1, NULL);
+                /* 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 a fid error %d\n", rc);
+                        CERROR("child lookup by aid error %d\n", rc);
                         GOTO(cleanup, rc);
                 }
                 goto got_child;
         }
-        
-        dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, parent_mode,
-                                        parent_lockh, &update_mode, rec->ur_name,
-                                        rec->ur_namelen - 1,
-                                        MDS_INODELOCK_UPDATE);
+       
+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 (IS_ERR(dparent)) {
                 rc = PTR_ERR(dparent);
-                CERROR("parent lookup error %d\n", rc);
+                if (rc != -ENOENT) {
+                        CERROR("parent lookup for "DLID4" failed, error %d\n",
+                               OLID4(rec->ur_id1), rc);
+                } else {
+                        /* Just cannot find parent - make it look like
+                           usual negative lookup to avoid extra MDS RPC */
+                        intent_set_disposition(rep, DISP_LOOKUP_EXECD);
+                        intent_set_disposition(rep, DISP_LOOKUP_NEG);
+                }
                 GOTO(cleanup, rc);
         }
         LASSERT(dparent->d_inode != NULL);
@@ -915,22 +1025,24 @@ int mds_open(struct mds_update_record *rec, int offset,
         cleanup_phase = 1; /* parent dentry and lock */
 
         /* try to retrieve MEA data for this dir */
-        rc = mds_get_lmv_attr(obd, dparent->d_inode, &mea, &mea_size);
+        rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
         if (rc)
                 GOTO(cleanup, rc);
        
         if (mea != NULL) {
-                /* dir is already splitted, check is requested filename
-                 * should live at this MDS or at another one */
+                /*
+                 * 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 != mea->mea_fids[i].mds) {
+                if (mea->mea_master != id_group(&mea->mea_ids[i])) {
                         CDEBUG(D_OTHER,
                                "%s: inapropriate MDS(%d) for %lu/%u:%s."
-                               " should be %d(%d)\n", obd->obd_name,
+                               " should be %lu(%d)\n", obd->obd_name,
                                mea->mea_master, dparent->d_inode->i_ino,
                                dparent->d_inode->i_generation, rec->ur_name,
-                               mea->mea_fids[i].mds, i);
+                               (unsigned long)id_group(&mea->mea_ids[i]), i);
                         GOTO(cleanup, rc = -ERESTART);
                 }
         }
@@ -946,35 +1058,55 @@ int mds_open(struct mds_update_record *rec, int offset,
 got_child:
         cleanup_phase = 2; /* child dentry */
 
-        intent_set_disposition(rep, DISP_LOOKUP_EXECD);
-
         if (dchild->d_flags & DCACHE_CROSS_REF) {
-                struct ldlm_res_id res_id = { . name = {0} };
-                ldlm_policy_data_t policy;
-                int flags = 0;
-                CDEBUG(D_OTHER, "cross reference: %lu/%lu/%lu\n",
-                       (unsigned long) dchild->d_mdsnum,
-                       (unsigned long) dchild->d_inum,
-                       (unsigned long) dchild->d_generation);
-                body->valid |= OBD_MD_FLID | OBD_MD_MDS;
-                body->fid1.id = dchild->d_inum;
-                body->fid1.mds = dchild->d_mdsnum;
-                body->fid1.generation = dchild->d_generation;
-                intent_set_disposition(rep, DISP_LOOKUP_POS);
-                res_id.name[0] = dchild->d_inum;
-                res_id.name[1] = dchild->d_generation;
-                policy.l_inodebits.bits = MDS_INODELOCK_LOOKUP;
-                rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
-                                      res_id, LDLM_IBITS, &policy,
-                                      LCK_PR, &flags,
-                                      mds_blocking_ast,
-                                      ldlm_completion_ast, NULL, NULL,
-                                      NULL, 0, NULL, child_lockh);
+                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);
@@ -982,20 +1114,28 @@ got_child:
                 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 = rec->ur_fid2->id;
+                unsigned long ino;
                 struct iattr iattr;
                 struct inode *inode;
-                rc = mds_try_to_split_dir(obd, dparent, &mea, 0, update_mode);
+                
+                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);
@@ -1005,7 +1145,10 @@ got_child:
                 }
 
                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
-                        /* It's negative and we weren't supposed to create it */
+                        /*
+                         * dentry is negative and we weren't supposed to create
+                         * object, get out of here.
+                         */
                         GOTO(cleanup, rc = -ENOENT);
                 }
 
@@ -1021,7 +1164,7 @@ got_child:
                 dp.p_ptr = req;
                 dp.p_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;
 
@@ -1032,8 +1175,9 @@ got_child:
                 inode = dchild->d_inode;
                 if (ino) {
                         LASSERT(ino == inode->i_ino);
-                        /* Written as part of setattr */
-                        inode->i_generation = rec->ur_fid2->generation;
+                        
+                        /* written as part of setattr */
+                        inode->i_generation = id_gen(rec->ur_id2);
                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
                                inode->i_ino, inode->i_generation);
                 }
@@ -1043,11 +1187,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 = rec->ur_fsuid;
                 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 = rec->ur_fsgid;
 
                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
                         ATTR_MTIME | ATTR_CTIME;
@@ -1061,22 +1205,46 @@ got_child:
                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
                 if (rc)
                         CERROR("error on parent setattr: rc = %d\n", rc);
-                else
-                        MDS_UPDATE_COUNTER(mds, MDS_CREATE_COUNT);
+                else {
+                        MD_COUNTER_INCREMENT(obd, create);
+                }
+
+                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);
+
+                        /* 
+                         * 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 (rc) {
+                                CERROR("mds_alloc_inode_sid() failed, "
+                                       "rc = %d\n", rc);
+                        }
+                }
 
                 if (!(rec->ur_flags & O_EXCL)) { /* bug 3313 */
                         rc = fsfilt_commit(obd, dchild->d_inode->i_sb,
-                                           dchild->d_inode, handle, 0);
+                                           dchild->d_inode, handle, 
+                                           req->rq_export->exp_sync);
                         handle = NULL;
                 }
 
                 acc_mode = 0;           /* Don't check for permissions */
         }
-
-        LASSERT(!mds_inode_is_orphan(dchild->d_inode));
-
-        mds_pack_inode2fid(obd, &body->fid1, dchild->d_inode);
-        mds_pack_inode2body(obd, body, dchild->d_inode);
+        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);
 
         if (S_ISREG(dchild->d_inode->i_mode)) {
                 /* Check permissions etc */
@@ -1103,53 +1271,94 @@ got_child:
                 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
         }
 
+        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))
+                        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, rc = 0);
+                GOTO(cleanup_no_trans, rc = 0);
 
         if ((rec->ur_flags & MDS_OPEN_DIRECTORY) &&
             !S_ISDIR(dchild->d_inode->i_mode))
                 GOTO(cleanup, rc = -ENOTDIR);
+               
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
+               obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
+               GOTO(cleanup, rc = -EAGAIN);
+       }
 
-        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)) {
-                        GOTO(cleanup, rc = -EACCES);
-                }
-                if (is_mount_object(dchild)) {
-                        CERROR("Found possible GNS mount object %*s; not "
-                               "opening.\n", dchild->d_name.len,
-                               dchild->d_name.name);
-                        GOTO(cleanup, rc = 0); // success, but don't really open
-                }
+#warning "disable opencache lock for CMD2"
+#if 0
+        /* Obtain OPEN lock as well */
+        policy.l_inodebits.bits |= MDS_INODELOCK_OPEN;
+
+        /* 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)
+                child_mode = LCK_PR;
+        else
+                child_mode = LCK_CR;
+
+        if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
+               /* In case of replay we do not get a lock assuming that the
+                   caller has it already */
+                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 (rc != ELDLM_OK)
+                        GOTO(cleanup, rc);
+
+                cleanup_phase = 3;
         }
+#endif
 
         /* Step 5: mds_open it */
-        rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
-                             rep);
-        GOTO(cleanup, rc);
+        rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle,
+                             rec, rep);
 
- cleanup:
+       EXIT;
+cleanup:
         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
                                 req, rc, rep ? rep->lock_policy_res1 : 0);
 
+cleanup_no_trans:
         switch (cleanup_phase) {
+        case 3:
+                if (rc) {
+                        ldlm_lock_decref(child_lockh, child_mode);
+                        child_lockh->cookie = 0;
+                }
         case 2:
                 if (rc && created) {
                         int err = vfs_unlink(dparent->d_inode, dchild);
                         if (err) {
-                                CERROR("unlink(%*s) in error path: %d\n",
+                                CERROR("unlink(%.*s) in error path: %d\n",
                                        dchild->d_name.len, dchild->d_name.name,
                                        err);
                         }
                 } else if (created) {
-#if 0
                         mds_lock_new_child(obd, dchild->d_inode, NULL);
-#endif
                 }
                 l_dput(dchild);
         case 1:
@@ -1168,11 +1377,26 @@ got_child:
         }
         if (mea)
                 OBD_FREE(mea, mea_size);
+        if (rc == 0)
+                atomic_inc(&mds->mds_open_count);
+
+        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 ((cleanup_phase != 3) && !rc)
+                rc = ENOLCK;
+
         RETURN(rc);
 }
 
 /* Close a "file descriptor" and possibly unlink an orphan from the
- * PENDING directory.
+ * PENDING directory.  Caller must hold child->i_sem, 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
@@ -1180,12 +1404,13 @@ got_child:
  * (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, 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 inode *inode = mfd->mfd_dentry->d_inode;
-        char fidname[LL_FID_NAMELEN];
-        int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
+        char idname[LL_ID_NAMELEN];
+        int last_orphan, idlen, 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;
@@ -1197,16 +1422,19 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
         ENTRY;
 
         if (req && req->rq_reqmsg != NULL)
-                request_body = lustre_msg_buf(req->rq_reqmsg, 0,
+                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,
                                             sizeof(*reply_body));
 
-        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));
 
-        last_orphan = mds_open_orphan_dec_test(inode) &&
+        last_orphan = mds_orphan_open_dec_test(inode) &&
                 mds_inode_is_orphan(inode);
+        UP_WRITE_I_ALLOC_SEM(inode);
 
         /* this is half of the actual "close" */
         if (mfd->mfd_mode & FMODE_WRITE) {
@@ -1221,27 +1449,38 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
                 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", fidname);
-                        GOTO(out, rc);
-                }
-
-                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,
-                 * but normally it will still be in the dcache. */
-                pending_dir = mds->mds_pending_dir->d_inode;
+                CDEBUG(D_HA, "destroying orphan object %s\n", idname);
+                
+                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(fidname, mds->mds_pending_dir,
-                                               fidlen);
+                pending_child = lookup_one_len(idname, mds->mds_pending_dir,
+                                               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)) {
+                        rc = vfs_rmdir(pending_dir, pending_child);
+                        if (rc)
+                                CERROR("error unlinking orphan dir %s: rc %d\n",
+                                       idname, rc);
+                        goto out;
+                }
+
                 if (req != NULL && req->rq_repmsg != NULL) {
                         lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
                         stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
@@ -1258,12 +1497,10 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
                 pending_child->d_fsdata = (void *) &dp;
                 dp.p_inum = 0;
                 dp.p_ptr = req;
-                if (S_ISDIR(pending_child->d_inode->i_mode))
-                        rc = vfs_rmdir(pending_dir, pending_child);
-                else
-                        rc = vfs_unlink(pending_dir, pending_child);
+                rc = vfs_unlink(pending_dir, pending_child);
                 if (rc)
-                        CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
+                        CERROR("error unlinking orphan %s: rc %d\n",
+                               idname, rc);
 
                 if (req != NULL && req->rq_repmsg != NULL &&
                     (reply_body->valid & OBD_MD_FLEASIZE) &&
@@ -1273,10 +1510,18 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
                                       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 */
         }
 
+#if 0
         if (request_body != NULL && 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)
@@ -1284,7 +1529,6 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
                  * XXX this should probably be abstracted with mds_reint_setattr
                  */
 
-#if 0
                 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);
@@ -1306,9 +1550,8 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
                         iattr.ia_blocks = request_body->blocks
                 } */
 
-#endif
         }
-
+#endif
         if (request_body != NULL && request_body->valid & OBD_MD_FLATIME) {
                 /* Only start a transaction to write out only the atime if
                  * it is more out-of-date than the specified limit.  If we
@@ -1329,7 +1572,7 @@ int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
                         GOTO(cleanup, rc = PTR_ERR(handle));
                 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
                 if (rc)
-                        CERROR("error in setattr(%s): rc %d\n", fidname, rc);
+                        CERROR("error in setattr(%s): rc %d\n", idname, rc);
         }
 out:
         /* If other clients have this file open for write, rc will be > 0 */
@@ -1339,10 +1582,17 @@ out:
         mds_mfd_destroy(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);
         } else if (handle) {
-                int err = fsfilt_commit(obd, mds->mds_sb, pending_dir, handle, 0);
+                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);
                 if (err) {
                         CERROR("error committing close: %d\n", err);
                         if (!rc)
@@ -1361,7 +1611,7 @@ out:
         RETURN(rc);
 }
 
-int mds_close(struct ptlrpc_request *req)
+int mds_close(struct ptlrpc_request *req, int offset)
 {
         struct mds_export_data *med = &req->rq_export->exp_mds_data;
         struct obd_device *obd = req->rq_export->exp_obd;
@@ -1373,8 +1623,7 @@ int mds_close(struct ptlrpc_request *req)
                               obd->u.mds.mds_max_mdsize,
                               obd->u.mds.mds_max_cookiesize};
         ENTRY;
-
-        MDS_UPDATE_COUNTER((&obd->u.mds), MDS_CLOSE_COUNT);
+        MD_COUNTER_INCREMENT(obd, close);
 
         rc = lustre_pack_reply(req, 3, repsize, NULL);
         if (rc) {
@@ -1385,14 +1634,14 @@ int mds_close(struct ptlrpc_request *req)
         }
 
         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, 1, 0),
+                       lustre_msg_buf(req->rq_reqmsg, offset + 1, 0),
                        req->rq_repmsg->buflens[2]);
         }
 
-
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
+        body = lustre_swab_reqbuf(req, offset, sizeof(*body),
+                                  lustre_swab_mds_body);
         if (body == NULL) {
                 CERROR("Can't unpack body\n");
                 req->rq_status = -EFAULT;
@@ -1405,26 +1654,33 @@ int mds_close(struct ptlrpc_request *req)
         mfd = mds_handle2mfd(&body->handle);
         if (mfd == NULL) {
                 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
-                          ": cookie "LPX64, body->fid1.id, body->handle.cookie);
+                          ": cookie "LPX64, id_ino(&body->id1), body->handle.cookie);
                 req->rq_status = -ESTALE;
                 RETURN(-ESTALE);
         }
 
         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));
-                LASSERT(body != NULL);
-
-                mds_pack_inode2fid(obd, &body->fid1, inode);
-                mds_pack_inode2body(obd, body, inode);
-                mds_pack_md(obd, req->rq_repmsg, 1, body, inode, 1);
+        /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
+        DOWN_WRITE_I_ALLOC_SEM(inode); /* mds_mfd_close drops this */
+        if (mds_inode_is_orphan(inode) && mds_orphan_open_count(inode) == 1) {
+                struct mds_body *rep_body;
+
+                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);
         }
         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, obd, mfd, 1);
+        req->rq_status = mds_mfd_close(req, offset, obd, mfd, 1);
         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 
         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
@@ -1438,7 +1694,7 @@ int mds_close(struct ptlrpc_request *req)
         RETURN(0);
 }
 
-int mds_done_writing(struct ptlrpc_request *req)
+int mds_done_writing(struct ptlrpc_request *req, int offset)
 {
         struct mds_body *body;
         int rc, size = sizeof(struct mds_body);
@@ -1446,7 +1702,8 @@ int mds_done_writing(struct ptlrpc_request *req)
 
         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
 
-        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
+        body = lustre_swab_reqbuf(req, offset, sizeof(*body),
+                                  lustre_swab_mds_body);
         if (body == NULL) {
                 CERROR("Can't unpack body\n");
                 req->rq_status = -EFAULT;