Whamcloud - gitweb
LU-3706 scrub: fix mutex leak in osd_obj_map_recover()
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_compat.c
index f0061c7..2dbd4e4 100644 (file)
@@ -112,7 +112,7 @@ static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
                GOTO(out_up, dchild);
        }
 
-       err = ll_vfs_mkdir(dir->d_inode, dchild, mnt, mode);
+       err = vfs_mkdir(dir->d_inode, dchild, mode);
        if (err)
                GOTO(out_err, err);
 
@@ -172,7 +172,7 @@ static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
        struct osd_mdobj_map    *omm;
        struct dentry           *d;
        struct osd_thread_info  *info = osd_oti_get(env);
-       struct lu_fid           *fid = &info->oti_fid;
+       struct lu_fid           *fid = &info->oti_fid3;
        int                     rc = 0;
        ENTRY;
 
@@ -192,13 +192,15 @@ static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
        if (IS_ERR(d))
                GOTO(cleanup, rc = PTR_ERR(d));
 
+       ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
        omm->omm_remote_parent = d;
 
        /* Set LMA for remote parent inode */
        lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
-       rc = osd_ea_fid_set(info, d->d_inode, fid, 0);
-       if (rc != 0)
-               GOTO(cleanup, rc);
+       rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
+
+       GOTO(cleanup, rc);
+
 cleanup:
        pop_ctxt(&save, &new, NULL);
        if (rc) {
@@ -207,7 +209,7 @@ cleanup:
                OBD_FREE_PTR(omm);
                dev->od_mdt_map = NULL;
        }
-       RETURN(rc);
+       return rc;
 }
 
 static void osd_mdt_fini(struct osd_device *osd)
@@ -318,6 +320,39 @@ int osd_delete_from_remote_parent(const struct lu_env *env,
        RETURN(rc);
 }
 
+int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
+                               struct osd_device *osd,
+                               const struct lu_fid *fid,
+                               struct osd_inode_id *id)
+{
+       struct osd_mdobj_map        *omm = osd->od_mdt_map;
+       char                        *name = oti->oti_name;
+       struct dentry               *parent;
+       struct dentry               *dentry;
+       struct ldiskfs_dir_entry_2 *de;
+       struct buffer_head         *bh;
+       int                         rc;
+       ENTRY;
+
+       parent = omm->omm_remote_parent;
+       sprintf(name, DFID_NOBRACE, PFID(fid));
+       dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
+                                          name, strlen(name));
+       mutex_lock(&parent->d_inode->i_mutex);
+       bh = osd_ldiskfs_find_entry(parent->d_inode, dentry, &de, NULL);
+       if (bh == NULL) {
+               rc = -ENOENT;
+       } else {
+               rc = 0;
+               osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
+               brelse(bh);
+       }
+       mutex_unlock(&parent->d_inode->i_mutex);
+       if (rc == 0)
+               osd_add_oi_cache(oti, osd, id, fid);
+       RETURN(rc);
+}
+
 /*
  * directory structure on legacy OST:
  *
@@ -328,13 +363,16 @@ int osd_delete_from_remote_parent(const struct lu_env *env,
  * CONFIGS
  *
  */
-static int osd_ost_init(struct osd_device *dev)
+static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
 {
-       struct lvfs_run_ctxt  new;
-       struct lvfs_run_ctxt  save;
-       struct dentry        *rootd = osd_sb(dev)->s_root;
-       struct dentry        *d;
-       int                   rc;
+       struct lvfs_run_ctxt     new;
+       struct lvfs_run_ctxt     save;
+       struct dentry           *rootd = osd_sb(dev)->s_root;
+       struct dentry           *d;
+       struct osd_thread_info  *info = osd_oti_get(env);
+       struct inode            *inode;
+       struct lu_fid           *fid = &info->oti_fid3;
+       int                      rc;
        ENTRY;
 
        OBD_ALLOC_PTR(dev->od_ost_map);
@@ -358,21 +396,41 @@ static int osd_ost_init(struct osd_device *dev)
         LASSERT(dev->od_fsops);
         osd_push_ctxt(dev, &new, &save);
 
-        d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
+       d = ll_lookup_one_len("O", rootd, strlen("O"));
        if (IS_ERR(d))
                GOTO(cleanup, rc = PTR_ERR(d));
+       if (d->d_inode == NULL) {
+               dput(d);
+               /* The lookup() may be called again inside simple_mkdir().
+                * Since the repeated lookup() only be called for "/O" at
+                * mount time, it will not affect the whole performance. */
+               d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
+               if (IS_ERR(d))
+                       GOTO(cleanup, rc = PTR_ERR(d));
+
+               /* It is quite probably that the device is new formatted. */
+               dev->od_maybe_new = 1;
+       }
 
-       ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
+       inode = d->d_inode;
+       ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
        dev->od_ost_map->om_root = d;
 
+       /* 'What the @fid is' is not imporatant, because the object
+        * has no OI mapping, and only is visible inside the OSD.*/
+       lu_igif_build(fid, inode->i_ino, inode->i_generation);
+       rc = osd_ea_fid_set(info, inode, fid,
+                           LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
+
+       GOTO(cleanup, rc);
+
 cleanup:
        osd_pop_ctxt(dev, &new, &save);
         if (IS_ERR(d)) {
                 OBD_FREE_PTR(dev->od_ost_map);
                 RETURN(PTR_ERR(d));
         }
-
-       RETURN(rc);
+       return rc;
 }
 
 static void osd_seq_free(struct osd_obj_map *map,
@@ -427,7 +485,7 @@ int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
        ENTRY;
 
        /* prepare structures for OST */
-       rc = osd_ost_init(dev);
+       rc = osd_ost_init(env, dev);
        if (rc)
                RETURN(rc);
 
@@ -464,6 +522,111 @@ void osd_obj_map_fini(struct osd_device *dev)
        osd_mdt_fini(dev);
 }
 
+/**
+ * Update the specified OI mapping.
+ *
+ * \retval   1, changed nothing
+ * \retval   0, changed successfully
+ * \retval -ve, on error
+ */
+static int osd_obj_update_entry(struct osd_thread_info *info,
+                               struct osd_device *osd,
+                               struct dentry *dir, const char *name,
+                               const struct lu_fid *fid,
+                               const struct osd_inode_id *id,
+                               struct thandle *th)
+{
+       struct inode               *parent = dir->d_inode;
+       struct osd_thandle         *oh;
+       struct dentry              *child;
+       struct ldiskfs_dir_entry_2 *de;
+       struct buffer_head         *bh;
+       struct inode               *inode;
+       struct dentry              *dentry = &info->oti_obj_dentry;
+       struct osd_inode_id        *oi_id  = &info->oti_id3;
+       struct lustre_mdt_attrs    *lma    = &info->oti_mdt_attrs;
+       struct lu_fid              *oi_fid = &lma->lma_self_fid;
+       int                         rc;
+       ENTRY;
+
+       oh = container_of(th, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle != NULL);
+       LASSERT(oh->ot_handle->h_transaction != NULL);
+
+       child = &info->oti_child_dentry;
+       child->d_parent = dir;
+       child->d_name.hash = 0;
+       child->d_name.name = name;
+       child->d_name.len = strlen(name);
+
+       ll_vfs_dq_init(parent);
+       mutex_lock(&parent->i_mutex);
+       bh = osd_ldiskfs_find_entry(parent, child, &de, NULL);
+       if (bh == NULL)
+               GOTO(out, rc = -ENOENT);
+
+       if (le32_to_cpu(de->inode) == id->oii_ino)
+               GOTO(out, rc = 1);
+
+       osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
+       inode = osd_iget(info, osd, oi_id);
+       if (IS_ERR(inode)) {
+               rc = PTR_ERR(inode);
+               if (rc == -ENOENT || rc == -ESTALE)
+                       goto update;
+               GOTO(out, rc);
+       }
+
+       rc = osd_get_lma(info, inode, dentry, lma);
+       if (rc == -ENODATA) {
+               rc = osd_get_idif(info, inode, dentry, oi_fid);
+               if (rc > 0) {
+                       oi_fid = NULL;
+                       rc = 0;
+               }
+       }
+       iput(inode);
+
+       /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
+        * either a crashed object or a uninitialized one. Replace it. */
+       if (rc == -ENODATA || oi_fid == NULL)
+               goto update;
+
+       if (rc != 0)
+               GOTO(out, rc);
+
+       if (lu_fid_eq(fid, oi_fid)) {
+               CERROR("%s: the FID "DFID" is used by two objects: "
+                      "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
+                      oi_id->oii_ino, oi_id->oii_gen,
+                      id->oii_ino, id->oii_gen);
+               GOTO(out, rc = -EEXIST);
+       }
+
+update:
+       /* There may be temporary inconsistency: On one hand, the new
+        * object may be referenced by multiple entries, which is out
+        * of our control unless we traverse the whole /O completely,
+        * which is non-flat order and inefficient, should be avoided;
+        * On the other hand, the old object may become orphan if it
+        * is still valid. Since it was referenced by an invalid entry,
+        * making it as invisible temporary may be not worse. OI scrub
+        * will process it later. */
+       rc = ldiskfs_journal_get_write_access(oh->ot_handle, bh);
+       if (rc != 0)
+               GOTO(out, rc);
+
+       de->inode = cpu_to_le32(id->oii_ino);
+       rc = ldiskfs_journal_dirty_metadata(oh->ot_handle, bh);
+
+       GOTO(out, rc);
+
+out:
+       brelse(bh);
+       mutex_unlock(&parent->i_mutex);
+       return rc;
+}
+
 static int osd_obj_del_entry(struct osd_thread_info *info,
                             struct osd_device *osd,
                             struct dentry *dird, char *name,
@@ -508,28 +671,34 @@ int osd_obj_add_entry(struct osd_thread_info *info,
                      const struct osd_inode_id *id,
                      struct thandle *th)
 {
-        struct osd_thandle *oh;
-        struct dentry *child;
-        struct inode *inode;
-        int rc;
+       struct osd_thandle *oh;
+       struct dentry *child;
+       struct inode *inode;
+       int rc;
 
-        ENTRY;
+       ENTRY;
+
+       if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
+               RETURN(0);
 
-        oh = container_of(th, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle != NULL);
-        LASSERT(oh->ot_handle->h_transaction != NULL);
+       oh = container_of(th, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle != NULL);
+       LASSERT(oh->ot_handle->h_transaction != NULL);
 
-        inode = &info->oti_inode;
-        inode->i_sb = osd_sb(osd);
+       inode = &info->oti_inode;
+       inode->i_sb = osd_sb(osd);
        osd_id_to_inode(inode, id);
        inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
 
-        child = &info->oti_child_dentry;
-        child->d_name.hash = 0;
-        child->d_name.name = name;
-        child->d_name.len = strlen(name);
-        child->d_parent = dir;
-        child->d_inode = inode;
+       child = &info->oti_child_dentry;
+       child->d_name.hash = 0;
+       child->d_name.name = name;
+       child->d_name.len = strlen(name);
+       child->d_parent = dir;
+       child->d_inode = inode;
+
+       if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
+               inode->i_ino++;
 
        ll_vfs_dq_init(dir->d_inode);
        mutex_lock(&dir->d_inode->i_mutex);
@@ -546,29 +715,35 @@ int osd_obj_add_entry(struct osd_thread_info *info,
  * debug messages to objects in the future, and the legacy space
  * of FID_SEQ_OST_MDT0 will be unused in the future.
  **/
-static inline void osd_seq_name(char *seq_name, obd_seq seq)
+static inline void osd_seq_name(char *seq_name, size_t name_size, obd_seq seq)
 {
-       sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
-                          fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
-               fid_seq_is_idif(seq) ? 0 : seq);
+       snprintf(seq_name, name_size,
+                (fid_seq_is_rsvd(seq) ||
+                 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
+                fid_seq_is_idif(seq) ? 0 : seq);
 }
 
-static inline void osd_oid_name(char *name, const struct lu_fid *fid, obd_id id)
+static inline void osd_oid_name(char *name, size_t name_size,
+                               const struct lu_fid *fid, obd_id id)
 {
-       sprintf(name, (fid_seq_is_rsvd(fid_seq(fid)) ||
-               fid_seq_is_mdt0(fid_seq(fid)) ||
-               fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
+       snprintf(name, name_size,
+                (fid_seq_is_rsvd(fid_seq(fid)) ||
+                 fid_seq_is_mdt0(fid_seq(fid)) ||
+                 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
 }
 
 /* external locking is required */
-static int osd_seq_load_locked(struct osd_device *osd,
+static int osd_seq_load_locked(struct osd_thread_info *info,
+                              struct osd_device *osd,
                               struct osd_obj_seq *osd_seq)
 {
        struct osd_obj_map  *map = osd->od_ost_map;
        struct dentry       *seq_dir;
+       struct inode        *inode;
+       struct lu_fid       *fid = &info->oti_fid3;
        int                 rc = 0;
        int                 i;
-       char                seq_name[32];
+       char                dir_name[32];
        ENTRY;
 
        if (osd_seq->oos_root != NULL)
@@ -577,17 +752,26 @@ static int osd_seq_load_locked(struct osd_device *osd,
        LASSERT(map);
        LASSERT(map->om_root);
 
-       osd_seq_name(seq_name, osd_seq->oos_seq);
+       osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
 
-       seq_dir = simple_mkdir(map->om_root, osd->od_mnt, seq_name, 0755, 1);
+       seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
        if (IS_ERR(seq_dir))
                GOTO(out_err, rc = PTR_ERR(seq_dir));
        else if (seq_dir->d_inode == NULL)
                GOTO(out_put, rc = -EFAULT);
 
-       ldiskfs_set_inode_state(seq_dir->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
+       inode = seq_dir->d_inode;
+       ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
        osd_seq->oos_root = seq_dir;
 
+       /* 'What the @fid is' is not imporatant, because the object
+        * has no OI mapping, and only is visible inside the OSD.*/
+       lu_igif_build(fid, inode->i_ino, inode->i_generation);
+       rc = osd_ea_fid_set(info, inode, fid,
+                           LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
+       if (rc != 0)
+               GOTO(out_put, rc);
+
        LASSERT(osd_seq->oos_dirs == NULL);
        OBD_ALLOC(osd_seq->oos_dirs,
                  sizeof(seq_dir) * osd_seq->oos_subdir_count);
@@ -596,27 +780,39 @@ static int osd_seq_load_locked(struct osd_device *osd,
 
        for (i = 0; i < osd_seq->oos_subdir_count; i++) {
                struct dentry   *dir;
-               char         name[32];
 
-               sprintf(name, "d%u", i);
-               dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, name,
+               snprintf(dir_name, sizeof(dir_name), "d%u", i);
+               dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
                                   0700, 1);
                if (IS_ERR(dir)) {
-                       rc = PTR_ERR(dir);
-               } else if (dir->d_inode) {
-                       ldiskfs_set_inode_state(dir->d_inode,
-                                               LDISKFS_STATE_LUSTRE_NO_OI);
-                       osd_seq->oos_dirs[i] = dir;
-                       rc = 0;
-               } else {
-                       LBUG();
+                       GOTO(out_free, rc = PTR_ERR(dir));
+               } else if (dir->d_inode == NULL) {
+                       dput(dir);
+                       GOTO(out_free, rc = -EFAULT);
                }
+
+               inode = dir->d_inode;
+               ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
+               osd_seq->oos_dirs[i] = dir;
+
+               /* 'What the @fid is' is not imporatant, because the object
+                * has no OI mapping, and only is visible inside the OSD.*/
+               lu_igif_build(fid, inode->i_ino, inode->i_generation);
+               rc = osd_ea_fid_set(info, inode, fid,
+                                   LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
+               if (rc != 0)
+                       GOTO(out_free, rc);
        }
 
-       if (rc != 0)
-               osd_seq_free(map, osd_seq);
-out_put:
        if (rc != 0) {
+out_free:
+               for (i = 0; i < osd_seq->oos_subdir_count; i++) {
+                       if (osd_seq->oos_dirs[i] != NULL)
+                               dput(osd_seq->oos_dirs[i]);
+               }
+               OBD_FREE(osd_seq->oos_dirs,
+                        sizeof(seq_dir) * osd_seq->oos_subdir_count);
+out_put:
                dput(seq_dir);
                osd_seq->oos_root = NULL;
        }
@@ -624,7 +820,8 @@ out_err:
        RETURN(rc);
 }
 
-struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
+static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
+                                       struct osd_device *osd, obd_seq seq)
 {
        struct osd_obj_map      *map;
        struct osd_obj_seq      *osd_seq;
@@ -660,7 +857,7 @@ struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
        /* Init subdir count to be 32, but each seq can have
         * different subdir count */
        osd_seq->oos_subdir_count = map->om_subdir_count;
-       rc = osd_seq_load_locked(osd, osd_seq);
+       rc = osd_seq_load_locked(info, osd, osd_seq);
        if (rc != 0)
                GOTO(cleanup, rc);
 
@@ -702,7 +899,7 @@ int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
        LASSERT(map->om_root);
 
         fid_to_ostid(fid, ostid);
-       osd_seq = osd_seq_load(dev, ostid_seq(ostid));
+       osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
        if (IS_ERR(osd_seq))
                RETURN(PTR_ERR(osd_seq));
 
@@ -710,7 +907,7 @@ int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
        d_seq = osd_seq->oos_dirs[dirn];
        LASSERT(d_seq);
 
-       osd_oid_name(name, fid, ostid_id(ostid));
+       osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
 
        child = &info->oti_child_dentry;
        child->d_parent = d_seq;
@@ -748,28 +945,40 @@ int osd_obj_map_insert(struct osd_thread_info *info,
        struct osd_obj_seq      *osd_seq;
        struct dentry           *d;
        struct ost_id           *ostid = &info->oti_ostid;
+       obd_id                   oid;
        int                     dirn, rc = 0;
        char                    name[32];
-        ENTRY;
+       ENTRY;
 
-        map = osd->od_ost_map;
-        LASSERT(map);
+       map = osd->od_ost_map;
+       LASSERT(map);
 
        /* map fid to seq:objid */
-        fid_to_ostid(fid, ostid);
+       fid_to_ostid(fid, ostid);
 
-       osd_seq = osd_seq_load(osd, ostid_seq(ostid));
+       oid = ostid_id(ostid);
+       osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
        if (IS_ERR(osd_seq))
                RETURN(PTR_ERR(osd_seq));
 
-       dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
+       dirn = oid & (osd_seq->oos_subdir_count - 1);
        d = osd_seq->oos_dirs[dirn];
-        LASSERT(d);
+       LASSERT(d);
+
+       osd_oid_name(name, sizeof(name), fid, oid);
 
-       osd_oid_name(name, fid, ostid_id(ostid));
+again:
        rc = osd_obj_add_entry(info, osd, d, name, id, th);
+       if (rc == -EEXIST) {
+               rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
+               if (unlikely(rc == -ENOENT))
+                       goto again;
 
-        RETURN(rc);
+               if (unlikely(rc == 1))
+                       RETURN(0);
+       }
+
+       RETURN(rc);
 }
 
 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
@@ -789,7 +998,7 @@ int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
        /* map fid to seq:objid */
         fid_to_ostid(fid, ostid);
 
-       osd_seq = osd_seq_load(osd, ostid_seq(ostid));
+       osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
        if (IS_ERR(osd_seq))
                GOTO(cleanup, rc = PTR_ERR(osd_seq));
 
@@ -797,39 +1006,217 @@ int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
        d = osd_seq->oos_dirs[dirn];
        LASSERT(d);
 
-       osd_oid_name(name, fid, ostid_id(ostid));
+       osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
        rc = osd_obj_del_entry(info, osd, d, name, th);
 cleanup:
         RETURN(rc);
 }
 
-int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
-                       const struct lu_fid *fid,
-                       const struct osd_inode_id *id,
-                       struct thandle *th)
+int osd_obj_map_update(struct osd_thread_info *info,
+                      struct osd_device *osd,
+                      const struct lu_fid *fid,
+                      const struct osd_inode_id *id,
+                      struct thandle *th)
 {
-       struct osd_obj_map      *map = osd->od_ost_map;
-       struct dentry           *root = osd_sb(osd)->s_root;
-       char                    *name;
-       int                     rc = 0;
+       struct osd_obj_seq      *osd_seq;
+       struct dentry           *d;
+       struct ost_id           *ostid = &info->oti_ostid;
+       int                     dirn, rc = 0;
+       char                    name[32];
+       ENTRY;
+
+       fid_to_ostid(fid, ostid);
+       osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
+       if (IS_ERR(osd_seq))
+               RETURN(PTR_ERR(osd_seq));
+
+       dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
+       d = osd_seq->oos_dirs[dirn];
+       LASSERT(d);
+
+       osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
+       rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
+
+       RETURN(rc);
+}
+
+int osd_obj_map_recover(struct osd_thread_info *info,
+                       struct osd_device *osd,
+                       struct inode *src_parent,
+                       struct dentry *src_child,
+                       const struct lu_fid *fid)
+{
+       struct osd_obj_seq         *osd_seq;
+       struct dentry              *tgt_parent;
+       struct dentry              *tgt_child = &info->oti_child_dentry;
+       struct inode               *dir;
+       struct inode               *inode     = src_child->d_inode;
+       struct ost_id              *ostid     = &info->oti_ostid;
+       handle_t                   *jh;
+       struct ldiskfs_dir_entry_2 *de;
+       struct buffer_head         *bh;
+       char                        name[32];
+       int                         dirn;
+       int                         rc        = 0;
        ENTRY;
 
        if (fid_is_last_id(fid)) {
-               struct osd_obj_seq      *osd_seq;
+               osd_seq = osd_seq_load(info, osd, fid_seq(fid));
+               if (IS_ERR(osd_seq))
+                       RETURN(PTR_ERR(osd_seq));
 
-               /* on creation of LAST_ID we create O/<seq> hierarchy */
-               LASSERT(map);
-               osd_seq = osd_seq_load(osd, fid_seq(fid));
+               tgt_parent = osd_seq->oos_root;
+               tgt_child->d_name.name = "LAST_ID";
+               tgt_child->d_name.len = strlen("LAST_ID");
+       } else {
+               fid_to_ostid(fid, ostid);
+               osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
                if (IS_ERR(osd_seq))
                        RETURN(PTR_ERR(osd_seq));
-               rc = osd_obj_add_entry(info, osd, osd_seq->oos_root,
-                                      "LAST_ID", id, th);
+
+               dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
+               tgt_parent = osd_seq->oos_dirs[dirn];
+               osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
+               tgt_child->d_name.name = name;
+               tgt_child->d_name.len = strlen(name);
+       }
+       LASSERT(tgt_parent != NULL);
+
+       dir = tgt_parent->d_inode;
+       tgt_child->d_name.hash = 0;
+       tgt_child->d_parent = tgt_parent;
+       tgt_child->d_inode = inode;
+
+       /* The non-initialized src_child may be destroyed. */
+       jh = ldiskfs_journal_start_sb(osd_sb(osd),
+                               osd_dto_credits_noquota[DTO_INDEX_DELETE] +
+                               osd_dto_credits_noquota[DTO_INDEX_INSERT] +
+                               osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
+       if (IS_ERR(jh))
+               RETURN(PTR_ERR(jh));
+
+       ll_vfs_dq_init(src_parent);
+       ll_vfs_dq_init(dir);
+
+       mutex_lock(&src_parent->i_mutex);
+       mutex_lock(&dir->i_mutex);
+       bh = osd_ldiskfs_find_entry(dir, tgt_child, &de, NULL);
+       if (bh != NULL) {
+               /* XXX: If some other object occupied the same slot. And If such
+                *      inode is zero-sized and with SUID+SGID, then means it is
+                *      a new created one. Maybe we can remove it and insert the
+                *      original one back to the /O/<seq>/d<x>. But there are
+                *      something to be considered:
+                *
+                *      1) The OST-object under /lost+found has crashed LMA.
+                *         So it should not conflict with the current one.
+                *
+                *      2) There are race conditions that: someone may just want
+                *         to modify the current one. Even if the OI scrub takes
+                *         the object lock when remove the current one, it still
+                *         cause the modification to be lost becasue the target
+                *         has been removed when the RPC service thread waiting
+                *         for the lock.
+                *
+                *      So keep it there before we have suitable solution. */
+               brelse(bh);
+
+               mutex_unlock(&dir->i_mutex);
+               mutex_unlock(&src_parent->i_mutex);
+
+               rc = -EEXIST;
+               /* If the src object has never been modified, then remove it. */
+               if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
+                   inode->i_mode & S_ISGID)
+                       rc = vfs_unlink(src_parent, src_child);
+               ldiskfs_journal_stop(jh);
+               RETURN(rc);
+       }
+
+       bh = osd_ldiskfs_find_entry(src_parent, src_child, &de, NULL);
+       if (unlikely(bh == NULL))
+               GOTO(unlock, rc = -ENOENT);
+
+       rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
+       brelse(bh);
+       if (rc != 0)
+               GOTO(unlock, rc);
+
+       rc = osd_ldiskfs_add_entry(jh, tgt_child, inode, NULL);
+
+       GOTO(unlock, rc);
+
+unlock:
+       mutex_unlock(&dir->i_mutex);
+       mutex_unlock(&src_parent->i_mutex);
+       ldiskfs_journal_stop(jh);
+       return rc;
+}
+
+static struct dentry *
+osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
+                    const struct lu_fid *fid, char **name)
+{
+       struct dentry *root = ERR_PTR(-ENOENT);
+
+       if (fid_is_last_id(fid)) {
+               struct osd_obj_seq *osd_seq;
+
+               /* on creation of LAST_ID we create O/<seq> hierarchy */
+               osd_seq = osd_seq_load(info, osd, fid_seq(fid));
+               if (IS_ERR(osd_seq))
+                       RETURN((struct dentry *)osd_seq);
+
+               *name = "LAST_ID";
+               root = osd_seq->oos_root;
        } else {
-               name = osd_lf_fid2name(fid);
-               if (name == NULL)
+               *name = osd_lf_fid2name(fid);
+               if (*name == NULL)
                        CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
-               else if (name[0])
-                       rc = osd_obj_add_entry(info, osd, root, name, id, th);
+               else if ((*name)[0])
+                       root = osd_sb(osd)->s_root;
+       }
+
+       return root;
+}
+
+int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
+                       const struct lu_fid *fid, const struct osd_inode_id *id,
+                       struct thandle *th)
+{
+       struct dentry   *root;
+       char            *name;
+       int              rc;
+       ENTRY;
+
+       root = osd_object_spec_find(info, osd, fid, &name);
+       if (!IS_ERR(root)) {
+               rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
+       } else {
+               rc = PTR_ERR(root);
+               if (rc == -ENOENT)
+                       rc = 1;
+       }
+
+       RETURN(rc);
+}
+
+int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
+                       const struct lu_fid *fid, const struct osd_inode_id *id,
+                       struct thandle *th)
+{
+       struct dentry   *root;
+       char            *name;
+       int              rc;
+       ENTRY;
+
+       root = osd_object_spec_find(info, osd, fid, &name);
+       if (!IS_ERR(root)) {
+               rc = osd_obj_add_entry(info, osd, root, name, id, th);
+       } else {
+               rc = PTR_ERR(root);
+               if (rc == -ENOENT)
+                       rc = 0;
        }
 
        RETURN(rc);
@@ -848,7 +1235,7 @@ int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
        if (fid_is_last_id(fid)) {
                struct osd_obj_seq *osd_seq;
 
-               osd_seq = osd_seq_load(osd, fid_seq(fid));
+               osd_seq = osd_seq_load(info, osd, fid_seq(fid));
                if (IS_ERR(osd_seq))
                        RETURN(PTR_ERR(osd_seq));
                root = osd_seq->oos_root;