+ struct iam_container *bag;
+ struct iam_iterator *it = &oti->oti_idx_it;
+ struct iam_path_descr *ipd;
+ int rc;
+ ENTRY;
+
+ LASSERT(oi);
+ LASSERT(oi->oi_inode);
+
+ bag = &oi->oi_dir.od_container;
+ ipd = osd_idx_ipd_get(oti->oti_env, bag);
+ if (IS_ERR(ipd))
+ RETURN(-ENOMEM);
+
+ /* got ipd now we can start iterator. */
+ iam_it_init(it, bag, 0, ipd);
+
+ rc = iam_it_get(it, (struct iam_key *)key);
+ if (rc > 0)
+ iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
+ iam_it_put(it);
+ iam_it_fini(it);
+ osd_ipd_put(oti->oti_env, bag, ipd);
+
+ LINVRNT(osd_invariant(obj));
+
+ RETURN(rc);
+}
+
+int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
+ const struct lu_fid *fid, enum oi_check_flags flags)
+{
+ struct lu_seq_range *range = &info->oti_seq_range;
+ int rc;
+ ENTRY;
+
+ if (flags & OI_KNOWN_ON_OST)
+ RETURN(1);
+
+ if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
+ fid_is_llog(fid)) || fid_is_name_llog(fid) ||
+ fid_is_quota(fid))
+ RETURN(0);
+
+ if (fid_is_idif(fid) || fid_is_last_id(fid))
+ RETURN(1);
+
+ if (!(flags & OI_CHECK_FLD))
+ RETURN(0);
+
+ if (osd_seq_site(osd)->ss_server_fld == NULL)
+ RETURN(0);
+
+ rc = osd_fld_lookup(info->oti_env, osd, fid_seq(fid), range);
+ if (rc != 0) {
+ if (rc != -ENOENT)
+ CERROR("%s: lookup FLD "DFID": rc = %d\n",
+ osd_name(osd), PFID(fid), rc);
+ RETURN(0);
+ }
+
+ if (fld_range_is_ost(range))
+ RETURN(1);
+
+ RETURN(0);
+}
+
+static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
+ const struct lu_fid *fid, struct osd_inode_id *id)
+{
+ struct lu_fid *oi_fid = &info->oti_fid2;
+ int rc;
+
+ fid_cpu_to_be(oi_fid, fid);
+ rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
+ (const struct dt_key *)oi_fid);
+ if (rc > 0) {
+ osd_id_unpack(id, id);
+ rc = 0;
+ } else if (rc == 0) {
+ rc = -ENOENT;
+ }
+ return rc;
+}
+
+int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
+ const struct lu_fid *fid, struct osd_inode_id *id,
+ enum oi_check_flags flags)
+{
+ if (unlikely(fid_is_last_id(fid)))
+ return osd_obj_spec_lookup(info, osd, fid, id);
+
+ if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
+ return osd_obj_map_lookup(info, osd, fid, id);
+
+
+ if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
+ int rc;
+ if (fid_is_fs_root(fid)) {
+ osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
+ osd_sb(osd)->s_root->d_inode->i_generation);
+ return 0;
+ }
+ if (unlikely(fid_is_acct(fid)))
+ return osd_acct_obj_lookup(info, osd, fid, id);
+
+ /* For other special FIDs, try OI first, then do spec lookup */
+ rc = __osd_oi_lookup(info, osd, fid, id);
+ if (rc == -ENOENT)
+ return osd_obj_spec_lookup(info, osd, fid, id);
+ return rc;
+ }
+
+ if (!osd->od_igif_inoi && fid_is_igif(fid)) {
+ osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
+ return 0;
+ }
+
+ return __osd_oi_lookup(info, osd, fid, id);
+}
+
+static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
+ const struct dt_rec *rec, const struct dt_key *key,
+ handle_t *th, bool insert)
+{
+ struct iam_container *bag;
+ struct iam_path_descr *ipd;
+ int rc;
+ ENTRY;
+
+ LASSERT(oi);
+ LASSERT(oi->oi_inode);
+ ll_vfs_dq_init(oi->oi_inode);
+
+ bag = &oi->oi_dir.od_container;
+ ipd = osd_idx_ipd_get(oti->oti_env, bag);
+ if (unlikely(ipd == NULL))
+ RETURN(-ENOMEM);
+
+ LASSERT(th != NULL);
+ LASSERT(th->h_transaction != NULL);
+ if (insert)
+ rc = iam_insert(th, bag, (const struct iam_key *)key,
+ (const struct iam_rec *)rec, ipd);
+ else
+ rc = iam_update(th, bag, (const struct iam_key *)key,
+ (const struct iam_rec *)rec, ipd);
+ osd_ipd_put(oti->oti_env, bag, ipd);
+ LINVRNT(osd_invariant(obj));
+ RETURN(rc);
+}
+
+int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
+ const struct lu_fid *fid, const struct osd_inode_id *id,
+ handle_t *th, enum oi_check_flags flags)
+{
+ struct lu_fid *oi_fid = &info->oti_fid2;
+ struct osd_inode_id *oi_id = &info->oti_id2;
+ int rc = 0;
+
+ if (unlikely(fid_is_last_id(fid)))
+ return osd_obj_spec_insert(info, osd, fid, id, th);
+
+ if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
+ return osd_obj_map_insert(info, osd, fid, id, th);
+
+ fid_cpu_to_be(oi_fid, fid);
+ osd_id_pack(oi_id, id);
+ rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
+ (const struct dt_rec *)oi_id,
+ (const struct dt_key *)oi_fid, th, true);
+ if (rc != 0) {
+ struct inode *inode;
+ struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
+
+ if (rc != -EEXIST)
+ return rc;
+
+ rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
+ if (rc != 0)
+ return rc;
+
+ if (unlikely(osd_id_eq(id, oi_id)))
+ return 1;
+
+ /* Check whether the mapping for oi_id is valid or not. */
+ inode = osd_iget(info, osd, oi_id);
+ if (IS_ERR(inode)) {
+ rc = PTR_ERR(inode);
+ if (rc == -ENOENT || rc == -ESTALE)
+ goto update;
+ return 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, &info->oti_obj_dentry, lma);
+ iput(inode);
+ if (rc == -ENODATA)
+ goto update;
+
+ if (rc != 0)
+ return rc;
+
+ if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
+ lu_fid_eq(fid, &lma->lma_self_fid)) {
+ CERROR("%.16s: the FID "DFID" is used by two objects: "
+ "%u/%u %u/%u\n",
+ LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
+ PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
+ id->oii_ino, id->oii_gen);
+ return -EEXIST;
+ }
+
+update:
+ osd_id_pack(oi_id, id);
+ rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
+ (const struct dt_rec *)oi_id,
+ (const struct dt_key *)oi_fid, th, false);
+ if (rc != 0)
+ return rc;
+ }
+
+ if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
+ rc = osd_obj_spec_insert(info, osd, fid, id, th);
+ return rc;