Whamcloud - gitweb
LU-2607 mdd: preserve input arguments in mdd_create()
[fs/lustre-release.git] / lustre / mdd / mdd_dir.c
index 0095d89..6ba30e3 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Whamcloud, Inc.
+ * Copyright (c) 2011, 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -63,18 +63,24 @@ static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
 static int mdd_declare_links_add(const struct lu_env *env,
                                  struct mdd_object *mdd_obj,
                                  struct thandle *handle);
-static int mdd_links_add(const struct lu_env *env,
-                         struct mdd_object *mdd_obj,
-                         const struct lu_fid *pfid,
-                         const struct lu_name *lname,
-                         struct thandle *handle, int first);
+static inline int mdd_links_add(const struct lu_env *env,
+                               struct mdd_object *mdd_obj,
+                               const struct lu_fid *pfid,
+                               const struct lu_name *lname,
+                               struct thandle *handle, int first);
+static inline int mdd_links_del(const struct lu_env *env,
+                               struct mdd_object *mdd_obj,
+                               const struct lu_fid *pfid,
+                               const struct lu_name *lname,
+                               struct thandle *handle);
 static int mdd_links_rename(const struct lu_env *env,
-                            struct mdd_object *mdd_obj,
-                            const struct lu_fid *oldpfid,
-                            const struct lu_name *oldlname,
-                            const struct lu_fid *newpfid,
-                            const struct lu_name *newlname,
-                            struct thandle *handle);
+                           struct mdd_object *mdd_obj,
+                           const struct lu_fid *oldpfid,
+                           const struct lu_name *oldlname,
+                           const struct lu_fid *newpfid,
+                           const struct lu_name *newlname,
+                           struct thandle *handle,
+                           int first, int check);
 
 static int
 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
@@ -339,31 +345,31 @@ int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
  * VTX feature has been checked already, no need check again.
  */
 static inline int mdd_is_sticky(const struct lu_env *env,
-                                struct mdd_object *pobj,
-                                struct mdd_object *cobj)
+                               struct mdd_object *pobj,
+                               struct mdd_object *cobj)
 {
-        struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
-        struct md_ucred *uc = md_ucred(env);
-        int rc;
+       struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
+       struct lu_ucred *uc = lu_ucred_assert(env);
+       int rc;
 
-        if (pobj) {
-                rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
-                if (rc)
-                        return rc;
+       if (pobj) {
+               rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
+               if (rc)
+                       return rc;
 
-                if (!(tmp_la->la_mode & S_ISVTX) ||
-                     (tmp_la->la_uid == uc->mu_fsuid))
-                        return 0;
-        }
+               if (!(tmp_la->la_mode & S_ISVTX) ||
+                   (tmp_la->la_uid == uc->uc_fsuid))
+                       return 0;
+       }
 
-        rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
-        if (rc)
-                return rc;
+       rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
+       if (rc)
+               return rc;
 
-        if (tmp_la->la_uid == uc->mu_fsuid)
-                return 0;
+       if (tmp_la->la_uid == uc->uc_fsuid)
+               return 0;
 
-        return !mdd_capable(uc, CFS_CAP_FOWNER);
+       return !mdd_capable(uc, CFS_CAP_FOWNER);
 }
 
 /*
@@ -493,27 +499,28 @@ static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *
 }
 
 static int __mdd_index_insert_only(const struct lu_env *env,
-                                   struct mdd_object *pobj,
-                                   const struct lu_fid *lf, const char *name,
-                                   struct thandle *handle,
-                                   struct lustre_capa *capa)
+                                  struct mdd_object *pobj,
+                                  const struct lu_fid *lf, const char *name,
+                                  struct thandle *handle,
+                                  struct lustre_capa *capa)
 {
-        struct dt_object *next = mdd_object_child(pobj);
-        int               rc;
-        ENTRY;
+       struct dt_object *next = mdd_object_child(pobj);
+       int               rc;
+       ENTRY;
 
-        if (dt_try_as_dir(env, next)) {
-                struct md_ucred  *uc = md_ucred(env);
+       if (dt_try_as_dir(env, next)) {
+               struct lu_ucred  *uc = lu_ucred_check(env);
+               int ignore_quota;
 
-                rc = next->do_index_ops->dio_insert(env, next,
-                                                    (struct dt_rec*)lf,
-                                                    (const struct dt_key *)name,
-                                                    handle, capa, uc->mu_cap &
-                                                    CFS_CAP_SYS_RESOURCE_MASK);
-        } else {
-                rc = -ENOTDIR;
-        }
-        RETURN(rc);
+               ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
+               rc = next->do_index_ops->dio_insert(env, next,
+                                                   (struct dt_rec*)lf,
+                                                   (const struct dt_key *)name,
+                                                   handle, capa, ignore_quota);
+       } else {
+               rc = -ENOTDIR;
+       }
+       RETURN(rc);
 }
 
 /* insert named index, add reference if isdir */
@@ -697,12 +704,12 @@ int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
        rec->cr_hdr.lrh_type = CHANGELOG_REC;
        rec->cr.cr_time = cl_time();
 
-       cfs_spin_lock(&mdd->mdd_cl.mc_lock);
+       spin_lock(&mdd->mdd_cl.mc_lock);
        /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
         * but as long as the MDD transactions are ordered correctly for e.g.
         * rename conflicts, I don't think this should matter. */
        rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
-       cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
+       spin_unlock(&mdd->mdd_cl.mc_lock);
 
        ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
        if (ctxt == NULL)
@@ -735,12 +742,12 @@ int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
        rec->cr_hdr.lrh_type = CHANGELOG_REC;
        rec->cr.cr_time = cl_time();
 
-       cfs_spin_lock(&mdd->mdd_cl.mc_lock);
+       spin_lock(&mdd->mdd_cl.mc_lock);
        /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
         * but as long as the MDD transactions are ordered correctly for e.g.
         * rename conflicts, I don't think this should matter. */
        rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
-       cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
+       spin_unlock(&mdd->mdd_cl.mc_lock);
 
        ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
        if (ctxt == NULL)
@@ -871,7 +878,6 @@ static int mdd_changelog_ext_ns_store(const struct lu_env  *env,
        rec->cr.cr_namelen = tname->ln_namelen;
        memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
        if (sname) {
-               LASSERT(sfid != NULL);
                rec->cr.cr_name[tname->ln_namelen] = '\0';
                memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
                        sname->ln_namelen);
@@ -1212,9 +1218,8 @@ static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
                                mdd_object_capa(env, mdd_cobj));
 
         if (!is_dir)
-                /* old files may not have link ea; ignore errors */
-                mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
-                                 lname, NULL, NULL, handle);
+               /* old files may not have link ea; ignore errors */
+               mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
 
        /* if object is removed then we can't get its attrs, use last get */
        if (cattr->la_nlink == 0) {
@@ -1431,9 +1436,19 @@ int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
          *  (2) maybe, the child attributes should be set in OSD when creation.
          */
 
+       /*
+        * inode mode has been set in creation time, and it's based on umask,
+        * la_mode and acl, don't set here again! (which will go wrong
+        * because below function doesn't consider umask).
+        * I'd suggest set all object attributes in creation time, see above.
+        */
+       LASSERT(attr->la_valid & LA_MODE);
+       attr->la_valid &= ~LA_MODE;
        rc = mdd_attr_set_internal(env, child, attr, handle, 0);
-        if (rc != 0)
-                RETURN(rc);
+       /* arguments are supposed to stay the same */
+       attr->la_valid |= LA_MODE;
+       if (rc != 0)
+               RETURN(rc);
 
        if (S_ISDIR(attr->la_mode)) {
                 /* Add "." and ".." for newly created dir */
@@ -1783,7 +1798,7 @@ static int mdd_create(const struct lu_env *env, struct md_object *pobj,
         inserted = 1;
 
         if (S_ISLNK(attr->la_mode)) {
-                struct md_ucred  *uc = md_ucred(env);
+               struct lu_ucred  *uc = lu_ucred_assert(env);
                 struct dt_object *dt = mdd_object_child(son);
                 const char *target_name = spec->u.sp_symname;
                 int sym_len = strlen(target_name);
@@ -1791,10 +1806,10 @@ static int mdd_create(const struct lu_env *env, struct md_object *pobj,
                 loff_t pos = 0;
 
                 buf = mdd_buf_get_const(env, target_name, sym_len);
-                rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
-                                                mdd_object_capa(env, son),
-                                                uc->mu_cap &
-                                                CFS_CAP_SYS_RESOURCE_MASK);
+               rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
+                                               mdd_object_capa(env, son),
+                                               uc->uc_cap &
+                                               CFS_CAP_SYS_RESOURCE_MASK);
 
                 if (rc == sym_len)
                         rc = 0;
@@ -1855,7 +1870,7 @@ out_stop:
 out_free:
         /* The child object shouldn't be cached anymore */
         if (rc)
-                cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
+               set_bit(LU_OBJECT_HEARD_BANSHEE,
                             &child->mo_lu.lo_header->loh_flags);
         return rc;
 }
@@ -2076,7 +2091,9 @@ static int mdd_rename(const struct lu_env *env,
         struct thandle *handle;
         const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
         const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
-        int is_dir;
+        bool is_dir;
+       bool tobj_ref = 0;
+       bool tobj_locked = 0;
        unsigned cl_flags = 0;
         int rc, rc2;
         ENTRY;
@@ -2201,8 +2218,8 @@ static int mdd_rename(const struct lu_env *env,
          */
         if (tobj && mdd_object_exists(mdd_tobj)) {
                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
+               tobj_locked = 1;
                 if (mdd_is_dead_obj(mdd_tobj)) {
-                        mdd_write_unlock(env, mdd_tobj);
                         /* shld not be dead, something is wrong */
                         CERROR("tobj is dead, something is wrong\n");
                         rc = -EINVAL;
@@ -2213,31 +2230,51 @@ static int mdd_rename(const struct lu_env *env,
                 /* Remove dot reference. */
                if (S_ISDIR(tg_attr->la_mode))
                         mdo_ref_del(env, mdd_tobj, handle);
+               tobj_ref = 1;
 
                /* fetch updated nlink */
                rc = mdd_la_get(env, mdd_tobj, tg_attr,
                                mdd_object_capa(env, mdd_tobj));
-               if (rc)
+               if (rc != 0) {
+                       CERROR("%s: Failed to get nlink for tobj "
+                               DFID": rc = %d\n",
+                               mdd2obd_dev(mdd)->obd_name,
+                               PFID(tpobj_fid), rc);
                        GOTO(fixup_tpobj, rc);
+               }
 
-                la->la_valid = LA_CTIME;
-                rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
-                if (rc)
-                        GOTO(fixup_tpobj, rc);
+               la->la_valid = LA_CTIME;
+               rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
+               if (rc != 0) {
+                       CERROR("%s: Failed to set ctime for tobj "
+                               DFID": rc = %d\n",
+                               mdd2obd_dev(mdd)->obd_name,
+                               PFID(tpobj_fid), rc);
+                       GOTO(fixup_tpobj, rc);
+               }
 
                /* XXX: this transfer to ma will be removed with LOD/OSP */
                ma->ma_attr = *tg_attr;
                ma->ma_valid |= MA_INODE;
-                rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
-                mdd_write_unlock(env, mdd_tobj);
-                if (rc)
-                        GOTO(fixup_tpobj, rc);
+               rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
+               if (rc != 0) {
+                       CERROR("%s: Failed to unlink tobj "
+                               DFID": rc = %d\n",
+                               mdd2obd_dev(mdd)->obd_name,
+                               PFID(tpobj_fid), rc);
+                       GOTO(fixup_tpobj, rc);
+               }
 
                /* fetch updated nlink */
                rc = mdd_la_get(env, mdd_tobj, tg_attr,
                                mdd_object_capa(env, mdd_tobj));
-               if (rc)
+               if (rc != 0) {
+                       CERROR("%s: Failed to get nlink for tobj "
+                               DFID": rc = %d\n",
+                               mdd2obd_dev(mdd)->obd_name,
+                               PFID(tpobj_fid), rc);
                        GOTO(fixup_tpobj, rc);
+               }
                /* XXX: this transfer to ma will be removed with LOD/OSP */
                ma->ma_attr = *tg_attr;
                ma->ma_valid |= MA_INODE;
@@ -2259,8 +2296,8 @@ static int mdd_rename(const struct lu_env *env,
 
         if (rc == 0 && mdd_sobj) {
                 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
-                rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
-                                      mdo2fid(mdd_tpobj), ltname, handle);
+               rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
+                                     mdo2fid(mdd_tpobj), ltname, handle, 0, 0);
                 if (rc == -ENOENT)
                         /* Old files might not have EA entry */
                         mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
@@ -2282,6 +2319,12 @@ fixup_tpobj:
 
                 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
                     !mdd_is_dead_obj(mdd_tobj)) {
+                       if (tobj_ref) {
+                               mdo_ref_add(env, mdd_tobj, handle);
+                               if (is_dir)
+                                       mdo_ref_add(env, mdd_tobj, handle);
+                       }
+
                         rc2 = __mdd_index_insert(env, mdd_tpobj,
                                          mdo2fid(mdd_tobj), tname,
                                          is_dir, handle,
@@ -2315,6 +2358,8 @@ fixup_spobj2:
                         CWARN("sp obj fix error %d\n",rc2);
         }
 cleanup:
+       if (tobj_locked)
+               mdd_write_unlock(env, mdd_tobj);
         if (likely(tdlh) && sdlh != tdlh)
                 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
         if (likely(sdlh))
@@ -2334,10 +2379,92 @@ out_pending:
         return rc;
 }
 
-/** enable/disable storing of hardlink info */
-int mdd_linkea_enable = 1;
-CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
-                "record hardlink info in EAs");
+/**
+ * The data that link search is done on.
+ */
+struct mdd_link_data {
+       /**
+        * Buffer to keep link EA body.
+        */
+       struct lu_buf           *ml_buf;
+       /**
+        * The matched header, entry and its lenght in the EA
+        */
+       struct link_ea_header   *ml_leh;
+       struct link_ea_entry    *ml_lee;
+       int                      ml_reclen;
+};
+
+static int mdd_links_new(const struct lu_env *env,
+                        struct mdd_link_data *ldata)
+{
+       ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
+       if (ldata->ml_buf->lb_buf == NULL)
+               return -ENOMEM;
+       ldata->ml_leh = ldata->ml_buf->lb_buf;
+       ldata->ml_leh->leh_magic = LINK_EA_MAGIC;
+       ldata->ml_leh->leh_len = sizeof(struct link_ea_header);
+       ldata->ml_leh->leh_reccount = 0;
+       return 0;
+}
+
+/** Read the link EA into a temp buffer.
+ * Uses the mdd_thread_info::mti_big_buf since it is generally large.
+ * A pointer to the buffer is stored in \a ldata::ml_buf.
+ *
+ * \retval 0 or error
+ */
+int mdd_links_read(const struct lu_env *env,
+                  struct mdd_object *mdd_obj,
+                  struct mdd_link_data *ldata)
+{
+       struct lustre_capa *capa;
+       struct link_ea_header *leh;
+       int rc;
+
+       /* First try a small buf */
+       LASSERT(env != NULL);
+       ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
+       if (ldata->ml_buf->lb_buf == NULL)
+               return -ENOMEM;
+
+       if (!mdd_object_exists(mdd_obj))
+               return -ENODATA;
+
+       capa = mdd_object_capa(env, mdd_obj);
+       rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
+                          XATTR_NAME_LINK, capa);
+       if (rc == -ERANGE) {
+               /* Buf was too small, figure out what we need. */
+               mdd_buf_put(ldata->ml_buf);
+               rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
+                                  XATTR_NAME_LINK, capa);
+               if (rc < 0)
+                       return rc;
+               ldata->ml_buf = mdd_buf_alloc(env, rc);
+               if (ldata->ml_buf->lb_buf == NULL)
+                       return -ENOMEM;
+               rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
+                                  XATTR_NAME_LINK, capa);
+       }
+       if (rc < 0)
+               return rc;
+
+       leh = ldata->ml_buf->lb_buf;
+       if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
+               leh->leh_magic = LINK_EA_MAGIC;
+               leh->leh_reccount = __swab32(leh->leh_reccount);
+               leh->leh_len = __swab64(leh->leh_len);
+               /* entries are swabbed by mdd_lee_unpack */
+       }
+       if (leh->leh_magic != LINK_EA_MAGIC)
+               return -EINVAL;
+       if (leh->leh_reccount == 0)
+               return -ENODATA;
+
+       ldata->ml_leh = leh;
+       return 0;
+}
 
 /** Read the link EA into a temp buffer.
  * Uses the name_buf since it is generally large.
@@ -2345,50 +2472,24 @@ CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
  * \retval ptr to \a lu_buf (always \a mti_big_buf)
  */
 struct lu_buf *mdd_links_get(const struct lu_env *env,
-                             struct mdd_object *mdd_obj)
+                            struct mdd_object *mdd_obj)
 {
-        struct lu_buf *buf;
-        struct lustre_capa *capa;
-        struct link_ea_header *leh;
-        int rc;
-
-        /* First try a small buf */
-        buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
-        if (buf->lb_buf == NULL)
-                return ERR_PTR(-ENOMEM);
+       struct mdd_link_data ldata = { 0 };
+       int rc;
 
-       if (!mdd_object_exists(mdd_obj))
-               return ERR_PTR(-ENODATA);
-
-        capa = mdd_object_capa(env, mdd_obj);
-        rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
-        if (rc == -ERANGE) {
-                /* Buf was too small, figure out what we need. */
-                mdd_buf_put(buf);
-                rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
-                if (rc < 0)
-                        return ERR_PTR(rc);
-                buf = mdd_buf_alloc(env, rc);
-                if (buf->lb_buf == NULL)
-                        return ERR_PTR(-ENOMEM);
-                rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
-        }
-        if (rc < 0)
-                return ERR_PTR(rc);
-
-        leh = buf->lb_buf;
-        if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
-                leh->leh_magic = LINK_EA_MAGIC;
-                leh->leh_reccount = __swab32(leh->leh_reccount);
-                leh->leh_len = __swab64(leh->leh_len);
-                /* entries are swabbed by mdd_lee_unpack */
-        }
-        if (leh->leh_magic != LINK_EA_MAGIC)
-                return ERR_PTR(-EINVAL);
-        if (leh->leh_reccount == 0)
-                return ERR_PTR(-ENODATA);
+       rc = mdd_links_read(env, mdd_obj, &ldata);
+       return rc ? ERR_PTR(rc) : ldata.ml_buf;
+}
 
-        return buf;
+static int mdd_links_write(const struct lu_env *env,
+                          struct mdd_object *mdd_obj,
+                          struct mdd_link_data *ldata,
+                          struct thandle *handle)
+{
+       const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ml_buf->lb_buf,
+                                                    ldata->ml_leh->leh_len);
+       return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
+                            mdd_object_capa(env, mdd_obj));
 }
 
 /** Pack a link_ea_entry.
@@ -2422,34 +2523,6 @@ void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
         lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
 }
 
-/** Add a record to the end of link ea buf */
-static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
-                           const struct lu_fid *pfid,
-                           const struct lu_name *lname)
-{
-        struct link_ea_header *leh;
-        struct link_ea_entry *lee;
-        int reclen;
-
-        if (lname == NULL || pfid == NULL)
-                return -EINVAL;
-
-        /* Make sure our buf is big enough for the new one */
-        leh = buf->lb_buf;
-        reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
-        if (leh->leh_len + reclen > buf->lb_len) {
-                if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
-                        return -ENOMEM;
-        }
-
-        leh = buf->lb_buf;
-        lee = buf->lb_buf + leh->leh_len;
-        reclen = mdd_lee_pack(lee, lname, pfid);
-        leh->leh_len += reclen;
-        leh->leh_reccount++;
-        return 0;
-}
-
 static int mdd_declare_links_add(const struct lu_env *env,
                                  struct mdd_object *mdd_obj,
                                  struct thandle *handle)
@@ -2470,153 +2543,257 @@ static int mdd_declare_links_add(const struct lu_env *env,
  */
 #define LINKEA_MAX_COUNT 128
 
-static int mdd_links_add(const struct lu_env *env,
-                         struct mdd_object *mdd_obj,
-                         const struct lu_fid *pfid,
-                         const struct lu_name *lname,
-                         struct thandle *handle, int first)
+/** Add a record to the end of link ea buf */
+static int mdd_links_add_buf(const struct lu_env *env,
+                            struct mdd_link_data *ldata,
+                            const struct lu_name *lname,
+                            const struct lu_fid *pfid)
 {
-        struct lu_buf *buf;
-        struct link_ea_header *leh;
-        int rc;
-        ENTRY;
+       LASSERT(ldata->ml_leh != NULL);
 
-        if (!mdd_linkea_enable)
-                RETURN(0);
+       if (lname == NULL || pfid == NULL)
+               return -EINVAL;
 
-        buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
-        if (IS_ERR(buf)) {
-                rc = PTR_ERR(buf);
-                if (rc != -ENODATA) {
-                        CERROR("link_ea read failed %d "DFID"\n", rc,
-                               PFID(mdd_object_fid(mdd_obj)));
-                        RETURN (rc);
-                }
-                /* empty EA; start one */
-                buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
-                if (buf->lb_buf == NULL)
-                        RETURN(-ENOMEM);
-                leh = buf->lb_buf;
-                leh->leh_magic = LINK_EA_MAGIC;
-                leh->leh_len = sizeof(struct link_ea_header);
-                leh->leh_reccount = 0;
-        }
+       /* Make sure our buf is big enough for the new one */
+       if (ldata->ml_leh->leh_reccount > LINKEA_MAX_COUNT)
+               return -EOVERFLOW;
 
-        leh = buf->lb_buf;
-        if (leh->leh_reccount > LINKEA_MAX_COUNT)
-                RETURN(-EOVERFLOW);
+       ldata->ml_reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
+       if (ldata->ml_leh->leh_len + ldata->ml_reclen >
+           ldata->ml_buf->lb_len) {
+               if (mdd_buf_grow(env, ldata->ml_leh->leh_len +
+                                ldata->ml_reclen) < 0)
+                       return -ENOMEM;
+       }
 
-        rc = __mdd_links_add(env, buf, pfid, lname);
-        if (rc)
-                RETURN(rc);
+       ldata->ml_leh = ldata->ml_buf->lb_buf;
+       ldata->ml_lee = ldata->ml_buf->lb_buf + ldata->ml_leh->leh_len;
+       ldata->ml_reclen = mdd_lee_pack(ldata->ml_lee, lname, pfid);
+       ldata->ml_leh->leh_len += ldata->ml_reclen;
+       ldata->ml_leh->leh_reccount++;
+       CDEBUG(D_INODE, "New link_ea name '%.*s' is added\n",
+              lname->ln_namelen, lname->ln_name);
+       return 0;
+}
 
-        leh = buf->lb_buf;
-       rc = mdo_xattr_set(env, mdd_obj,
-                             mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
-                            XATTR_NAME_LINK, 0, handle,
-                            mdd_object_capa(env, mdd_obj));
-        if (rc) {
-                if (rc == -ENOSPC)
-                        CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
-                               PFID(mdd_object_fid(mdd_obj)));
-                else
-                        CERROR("link_ea add failed %d "DFID"\n", rc,
-                               PFID(mdd_object_fid(mdd_obj)));
-        }
+/** Del the current record from the link ea buf */
+static void mdd_links_del_buf(const struct lu_env *env,
+                             struct mdd_link_data *ldata,
+                             const struct lu_name *lname)
+{
+       LASSERT(ldata->ml_leh != NULL);
 
-        if (buf->lb_len > OBD_ALLOC_BIG)
-                /* if we vmalloced a large buffer drop it */
-                mdd_buf_put(buf);
+       ldata->ml_leh->leh_reccount--;
+       ldata->ml_leh->leh_len -= ldata->ml_reclen;
+       memmove(ldata->ml_lee, (char *)ldata->ml_lee + ldata->ml_reclen,
+               (char *)ldata->ml_leh + ldata->ml_leh->leh_len -
+               (char *)ldata->ml_lee);
+       CDEBUG(D_INODE, "Old link_ea name '%.*s' is removed\n",
+              lname->ln_namelen, lname->ln_name);
 
-        RETURN (rc);
 }
 
-static int mdd_links_rename(const struct lu_env *env,
-                            struct mdd_object *mdd_obj,
-                            const struct lu_fid *oldpfid,
-                            const struct lu_name *oldlname,
-                            const struct lu_fid *newpfid,
-                            const struct lu_name *newlname,
-                            struct thandle *handle)
+/**
+ * Check if such a link exists in linkEA.
+ *
+ * \param mdd_obj object being handled
+ * \param pfid parent fid the link to be found for
+ * \param lname name in the parent's directory entry pointing to this object
+ * \param ldata link data the search to be done on
+ *
+ * \retval   0 success
+ * \retval -ENOENT link does not exist
+ * \retval -ve on error
+ */
+static int mdd_links_find(const struct lu_env  *env,
+                         struct mdd_object    *mdd_obj,
+                         struct mdd_link_data *ldata,
+                         const struct lu_name *lname,
+                         const struct lu_fid  *pfid)
 {
-        struct lu_buf  *buf;
-        struct link_ea_header *leh;
-        struct link_ea_entry  *lee;
-        struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
-        struct lu_fid  *tmpfid = &mdd_env_info(env)->mti_fid;
-        int reclen = 0;
-        int count;
-        int rc, rc2 = 0;
-        ENTRY;
+       struct lu_name *tmpname = &mdd_env_info(env)->mti_name2;
+       struct lu_fid  *tmpfid = &mdd_env_info(env)->mti_fid;
+       int count;
+
+       LASSERT(ldata->ml_leh != NULL);
+
+       /* link #0 */
+       ldata->ml_lee = (struct link_ea_entry *)(ldata->ml_leh + 1);
+
+       for (count = 0; count < ldata->ml_leh->leh_reccount; count++) {
+               mdd_lee_unpack(ldata->ml_lee, &ldata->ml_reclen,
+                              tmpname, tmpfid);
+               if (tmpname->ln_namelen == lname->ln_namelen &&
+                   lu_fid_eq(tmpfid, pfid) &&
+                   (strncmp(tmpname->ln_name, lname->ln_name,
+                            tmpname->ln_namelen) == 0))
+                       break;
+               ldata->ml_lee = (struct link_ea_entry *)((char *)ldata->ml_lee +
+                                                        ldata->ml_reclen);
+       }
 
-        if (!mdd_linkea_enable)
-                RETURN(0);
+       if (count == ldata->ml_leh->leh_reccount) {
+               CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
+                      lname->ln_namelen, lname->ln_name);
+               return -ENOENT;
+       }
+       return 0;
+}
 
-        if (mdd_obj->mod_flags & DEAD_OBJ)
-                /* No more links, don't bother */
-                RETURN(0);
+static int __mdd_links_add(const struct lu_env *env,
+                          struct mdd_object *mdd_obj,
+                          struct mdd_link_data *ldata,
+                          const struct lu_name *lname,
+                          const struct lu_fid *pfid,
+                          int first, int check)
+{
+       int rc;
 
-        buf = mdd_links_get(env, mdd_obj);
-        if (IS_ERR(buf)) {
-                rc = PTR_ERR(buf);
-                if (rc == -ENODATA)
-                        CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
-                               rc, PFID(mdd_object_fid(mdd_obj)));
-                else
-                        CERROR("link_ea read failed %d "DFID"\n",
-                               rc, PFID(mdd_object_fid(mdd_obj)));
-                RETURN(rc);
-        }
-        leh = buf->lb_buf;
-        lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
-
-        /* Find the old record */
-        for(count = 0; count < leh->leh_reccount; count++) {
-                mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
-                if (tmpname->ln_namelen == oldlname->ln_namelen &&
-                    lu_fid_eq(tmpfid, oldpfid) &&
-                    (strncmp(tmpname->ln_name, oldlname->ln_name,
-                             tmpname->ln_namelen) == 0))
-                        break;
-                lee = (struct link_ea_entry *)((char *)lee + reclen);
-        }
-        if ((count + 1) > leh->leh_reccount) {
-                CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
-                       oldlname->ln_namelen, oldlname->ln_name);
-                GOTO(out, rc = -ENOENT);
-        }
+       if (ldata->ml_leh == NULL) {
+               rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
+               if (rc) {
+                       if (rc != -ENODATA)
+                               return rc;
+                       rc = mdd_links_new(env, ldata);
+                       if (rc)
+                               return rc;
+               }
+       }
 
-        /* Remove the old record */
-        leh->leh_reccount--;
-        leh->leh_len -= reclen;
-        memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
-                (char *)lee);
-
-        /* If renaming, add the new record */
-        if (newpfid != NULL) {
-                /* if the add fails, we still delete the out-of-date old link */
-                rc2 = __mdd_links_add(env, buf, newpfid, newlname);
-                leh = buf->lb_buf;
-        }
+       if (check) {
+               rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
+               if (rc && rc != -ENOENT)
+                       return rc;
+               if (rc == 0)
+                       return -EEXIST;
+       }
+
+       return mdd_links_add_buf(env, ldata, lname, pfid);
+}
+
+static int __mdd_links_del(const struct lu_env *env,
+                          struct mdd_object *mdd_obj,
+                          struct mdd_link_data *ldata,
+                          const struct lu_name *lname,
+                          const struct lu_fid *pfid)
+{
+       int rc;
+
+       if (ldata->ml_leh == NULL) {
+               rc = mdd_links_read(env, mdd_obj, ldata);
+               if (rc)
+                       return rc;
+       }
+
+       rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
+       if (rc)
+               return rc;
 
-       rc = mdo_xattr_set(env, mdd_obj,
-                          mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
-                          XATTR_NAME_LINK, 0, handle,
-                          mdd_object_capa(env, mdd_obj));
+       mdd_links_del_buf(env, ldata, lname);
+       return 0;
+}
+
+static int mdd_links_rename(const struct lu_env *env,
+                           struct mdd_object *mdd_obj,
+                           const struct lu_fid *oldpfid,
+                           const struct lu_name *oldlname,
+                           const struct lu_fid *newpfid,
+                           const struct lu_name *newlname,
+                           struct thandle *handle,
+                           int first, int check)
+{
+       struct mdd_link_data ldata = { 0 };
+       int updated = 0;
+       int rc2 = 0;
+       int rc = 0;
+       ENTRY;
+
+       LASSERT(oldpfid != NULL || newpfid != NULL);
+
+       if (mdd_obj->mod_flags & DEAD_OBJ)
+               /* No more links, don't bother */
+               RETURN(0);
 
+       if (oldpfid != NULL) {
+               rc = __mdd_links_del(env, mdd_obj, &ldata,
+                                    oldlname, oldpfid);
+               if (rc) {
+                       if ((check == 0) ||
+                           (rc != -ENODATA && rc != -ENOENT))
+                               GOTO(out, rc);
+                       /* No changes done. */
+                       rc = 0;
+               } else {
+                       updated = 1;
+               }
+       }
+
+       /* If renaming, add the new record */
+       if (newpfid != NULL) {
+               /* even if the add fails, we still delete the out-of-date
+                * old link */
+               rc2 = __mdd_links_add(env, mdd_obj, &ldata,
+                                     newlname, newpfid, first, check);
+               if (rc2 == -EEXIST)
+                       rc2 = 0;
+               else if (rc2 == 0)
+                       updated = 1;
+       }
+
+       if (updated)
+               rc = mdd_links_write(env, mdd_obj, &ldata, handle);
+       EXIT;
 out:
-        if (rc == 0)
-                rc = rc2;
-        if (rc)
-                CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
-                       oldlname->ln_namelen, oldlname->ln_name, rc,
-                       PFID(mdd_object_fid(mdd_obj)));
+       if (rc == 0)
+               rc = rc2;
+       if (rc) {
+               int error = 1;
+               if (rc == -EOVERFLOW || rc == - ENOENT)
+                       error = 0;
+               if (oldpfid == NULL)
+                       CDEBUG(error ? D_ERROR : D_OTHER,
+                              "link_ea add '%.*s' failed %d "DFID"\n",
+                              newlname->ln_namelen, newlname->ln_name,
+                              rc, PFID(mdd_object_fid(mdd_obj)));
+               else if (newpfid == NULL)
+                       CDEBUG(error ? D_ERROR : D_OTHER,
+                              "link_ea del '%.*s' failed %d "DFID"\n",
+                              oldlname->ln_namelen, oldlname->ln_name,
+                              rc, PFID(mdd_object_fid(mdd_obj)));
+               else
+                       CDEBUG(error ? D_ERROR : D_OTHER,
+                              "link_ea rename '%.*s'->'%.*s' failed %d "
+                              DFID"\n",
+                              oldlname->ln_namelen, oldlname->ln_name,
+                              newlname->ln_namelen, newlname->ln_name,
+                              rc, PFID(mdd_object_fid(mdd_obj)));
+       }
+
+       if (ldata.ml_buf && ldata.ml_buf->lb_len > OBD_ALLOC_BIG)
+               /* if we vmalloced a large buffer drop it */
+               mdd_buf_put(ldata.ml_buf);
+
+       return rc;
+}
 
-        if (buf->lb_len > OBD_ALLOC_BIG)
-                /* if we vmalloced a large buffer drop it */
-                mdd_buf_put(buf);
+static inline int mdd_links_add(const struct lu_env *env,
+                               struct mdd_object *mdd_obj,
+                               const struct lu_fid *pfid,
+                               const struct lu_name *lname,
+                               struct thandle *handle, int first)
+{
+       return mdd_links_rename(env, mdd_obj, NULL, NULL,
+                               pfid, lname, handle, first, 0);
+}
 
-        RETURN (rc);
+static inline int mdd_links_del(const struct lu_env *env,
+                               struct mdd_object *mdd_obj,
+                               const struct lu_fid *pfid,
+                               const struct lu_name *lname,
+                               struct thandle *handle)
+{
+       return mdd_links_rename(env, mdd_obj, pfid, lname,
+                               NULL, NULL, handle, 0, 0);
 }
 
 const struct md_dir_operations mdd_dir_ops = {