+
+ if (!dev->od_is_ost) {
+ rc = osd_mdt_init(env, dev);
+ if (rc) {
+ osd_ost_fini(dev);
+ RETURN(rc);
+ }
+
+ mdt_init = true;
+ }
+
+ rc = osd_index_backup_dir_init(env, dev);
+ if (rc) {
+ osd_ost_fini(dev);
+ if (mdt_init)
+ osd_mdt_fini(dev);
+ }
+
+ RETURN(rc);
+}
+
+static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
+{
+ struct osd_obj_seq *osd_seq;
+
+ list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
+ if (osd_seq->oos_seq == seq)
+ return osd_seq;
+ }
+ return NULL;
+}
+
+static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
+{
+ struct osd_obj_seq *osd_seq;
+
+ read_lock(&map->om_seq_list_lock);
+ osd_seq = osd_seq_find_locked(map, seq);
+ read_unlock(&map->om_seq_list_lock);
+ return osd_seq;
+}
+
+void osd_obj_map_fini(struct osd_device *dev)
+{
+ osd_index_backup_dir_fini(dev);
+ osd_ost_fini(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,
+ handle_t *th)
+{
+ struct inode *parent = dir->d_inode;
+ 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_ost_attrs.loa_lma;
+ struct lu_fid *oi_fid = &lma->lma_self_fid;
+ int rc;
+
+ ENTRY;
+
+ LASSERT(th != NULL);
+ LASSERT(th->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);
+
+ dquot_initialize(parent);
+ inode_lock(parent);
+ bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
+ if (IS_ERR(bh))
+ GOTO(out, rc = PTR_ERR(bh));
+
+ 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);
+ }
+
+ /*
+ * The EA inode should NOT be in OI, old OI scrub may added
+ * such OI mapping by wrong, replace it.
+ */
+ if (unlikely(osd_is_ea_inode(inode))) {
+ iput(inode);
+ goto update;
+ }
+
+ rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
+ if (rc == -ENODATA) {
+ rc = osd_get_idif(info, inode, dentry, oi_fid);
+ if (rc > 0 || rc == -ENODATA) {
+ oi_fid = NULL;
+ rc = 0;
+ }
+ }
+ iput(inode);
+
+ if (rc != 0)
+ GOTO(out, rc);
+
+ /*
+ * 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 (oi_fid != NULL && 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);
+ }
+
+ if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
+ __u32 idx1 = fid_idif_ost_idx(fid);
+ __u32 idx2 = fid_idif_ost_idx(oi_fid);
+ struct ost_id *ostid = &info->oti_ostid;
+ struct lu_fid *tfid = &info->oti_fid3;
+
+ LASSERTF(idx1 == 0 || idx1 == osd->od_index,
+ "invalid given FID "DFID", not match the "
+ "device index %u\n", PFID(fid), osd->od_index);
+
+ if (idx1 != idx2) {
+ if (idx1 == 0 && idx2 == osd->od_index) {
+ fid_to_ostid(fid, ostid);
+ ostid_to_fid(tfid, ostid, idx2);
+ if (lu_fid_eq(tfid, oi_fid)) {
+ CERROR("%s: the FID "DFID" is used by "
+ "two objects(2): %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);
+ }
+ } else if (idx2 == 0 && idx1 == osd->od_index) {
+ fid_to_ostid(oi_fid, ostid);
+ ostid_to_fid(tfid, ostid, idx1);
+ if (lu_fid_eq(tfid, fid)) {
+ CERROR("%s: the FID "DFID" is used by "
+ "two objects(2): %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(th, bh);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ de->inode = cpu_to_le32(id->oii_ino);
+ rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
+
+ GOTO(out, rc);
+
+out:
+ if (!IS_ERR(bh))
+ brelse(bh);
+ inode_unlock(parent);
+ return rc;
+}
+
+static int osd_obj_del_entry(struct osd_thread_info *info,
+ struct osd_device *osd,
+ struct dentry *dird, char *name,
+ handle_t *th)
+{
+ struct ldiskfs_dir_entry_2 *de;
+ struct buffer_head *bh;
+ struct dentry *child;
+ struct inode *dir = dird->d_inode;
+ int rc;
+
+ ENTRY;
+
+ LASSERT(th != NULL);
+ LASSERT(th->h_transaction != NULL);
+
+ child = &info->oti_child_dentry;
+ child->d_name.hash = 0;
+ child->d_name.name = name;
+ child->d_name.len = strlen(name);
+ child->d_parent = dird;
+ child->d_inode = NULL;
+
+ dquot_initialize(dir);
+ inode_lock(dir);
+ bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
+ if (IS_ERR(bh)) {
+ rc = PTR_ERR(bh);
+ } else {
+ rc = ldiskfs_delete_entry(th, dir, de, bh);
+ brelse(bh);
+ }
+ inode_unlock(dir);
+
+ RETURN(rc);
+}
+
+static int osd_obj_add_entry(struct osd_thread_info *info,
+ struct osd_device *osd,
+ struct dentry *dir, char *name,
+ const struct osd_inode_id *id,
+ handle_t *th)
+{
+ struct dentry *child;
+ struct inode *inode;
+ int rc;
+
+ ENTRY;
+
+ if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
+ RETURN(0);
+
+ LASSERT(th != NULL);
+ LASSERT(th->h_transaction != NULL);
+
+ inode = info->oti_inode;
+ if (unlikely(inode == NULL)) {
+ struct ldiskfs_inode_info *lii;
+
+ OBD_ALLOC_PTR(lii);
+ if (lii == NULL)
+ RETURN(-ENOMEM);
+ inode = info->oti_inode = &lii->vfs_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;
+
+ if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
+ inode->i_ino++;
+
+ dquot_initialize(dir->d_inode);
+ inode_lock(dir->d_inode);
+ rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
+ inode_unlock(dir->d_inode);
+
+ RETURN(rc);
+}
+
+/**
+ * Use %llu for legacy OST sequences, but use %llx for new
+ * sequences names, so that the O/{seq}/dN/{oid} more closely
+ * follows the DFID/PFID format. This makes it easier to map from
+ * 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, size_t name_size, u64 seq)
+{
+ snprintf(seq_name, name_size,
+ (fid_seq_is_rsvd(seq) ||
+ fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
+ fid_seq_is_idif(seq) ? 0 : seq);
+}
+
+static inline void osd_oid_name(char *name, size_t name_size,
+ const struct lu_fid *fid, u64 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))) ? "%llu" : "%llx", id);
+}
+
+/* external locking is required */
+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;
+ int rc = 0;
+ int i;
+ char dir_name[32];
+
+ ENTRY;
+
+ if (osd_seq->oos_root != NULL)
+ RETURN(0);
+
+ LASSERT(map);
+ LASSERT(map->om_root);
+
+ osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
+
+ seq_dir = simple_mkdir(info->oti_env, osd, map->om_root, NULL, dir_name,
+ LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0755, NULL);
+ 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);
+
+ osd_seq->oos_root = seq_dir;
+
+ LASSERT(osd_seq->oos_dirs == NULL);
+ OBD_ALLOC(osd_seq->oos_dirs,
+ sizeof(seq_dir) * osd_seq->oos_subdir_count);
+ if (osd_seq->oos_dirs == NULL)
+ GOTO(out_put, rc = -ENOMEM);
+
+ for (i = 0; i < osd_seq->oos_subdir_count; i++) {
+ struct dentry *dir;
+
+ snprintf(dir_name, sizeof(dir_name), "d%u", i);
+ dir = simple_mkdir(info->oti_env, osd, osd_seq->oos_root, NULL,
+ dir_name, LMAC_NOT_IN_OI | LMAC_FID_ON_OST,
+ 0700, NULL);
+ if (IS_ERR(dir)) {
+ GOTO(out_free, rc = PTR_ERR(dir));
+ } else if (dir->d_inode == NULL) {
+ dput(dir);
+ GOTO(out_free, rc = -EFAULT);
+ }
+
+ osd_seq->oos_dirs[i] = dir;