+ 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(th, bh);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ de->inode = cpu_to_le32(id->oii_ino);
+ rc = ldiskfs_journal_dirty_metadata(th, 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,
+ 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;
+
+ ll_vfs_dq_init(dir);
+ mutex_lock(&dir->i_mutex);
+ rc = -ENOENT;
+ bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
+ if (bh) {
+ rc = ldiskfs_delete_entry(th, dir, de, bh);
+ brelse(bh);
+ }
+ mutex_unlock(&dir->i_mutex);
+
+ RETURN(rc);
+}
+
+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;
+ 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++;
+
+ ll_vfs_dq_init(dir->d_inode);
+ mutex_lock(&dir->d_inode->i_mutex);
+ rc = osd_ldiskfs_add_entry(th, child, inode, NULL);
+ mutex_unlock(&dir->d_inode->i_mutex);
+
+ RETURN(rc);
+}
+
+/**
+ * Use LPU64 for legacy OST sequences, but use LPX64i 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, obd_seq 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, size_t name_size,
+ const struct lu_fid *fid, obd_id 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_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 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(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);
+
+ inode = seq_dir->d_inode;
+ 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);
+ 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(osd_seq->oos_root, osd->od_mnt, dir_name,
+ 0700, 1);
+ if (IS_ERR(dir)) {
+ GOTO(out_free, rc = PTR_ERR(dir));
+ } else if (dir->d_inode == NULL) {
+ dput(dir);
+ GOTO(out_free, rc = -EFAULT);
+ }
+
+ inode = dir->d_inode;
+ 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) {
+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;
+ }
+out_err:
+ RETURN(rc);
+}
+
+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;
+ int rc = 0;
+ ENTRY;
+
+ map = osd->od_ost_map;
+ LASSERT(map);
+ LASSERT(map->om_root);
+
+ osd_seq = osd_seq_find(map, seq);
+ if (likely(osd_seq != NULL))
+ RETURN(osd_seq);
+
+ /* Serializing init process */
+ mutex_lock(&map->om_dir_init_mutex);
+
+ /* Check whether the seq has been added */
+ read_lock(&map->om_seq_list_lock);
+ osd_seq = osd_seq_find_locked(map, seq);
+ if (osd_seq != NULL) {
+ read_unlock(&map->om_seq_list_lock);
+ GOTO(cleanup, rc = 0);
+ }
+ read_unlock(&map->om_seq_list_lock);
+
+ OBD_ALLOC_PTR(osd_seq);
+ if (osd_seq == NULL)
+ GOTO(cleanup, rc = -ENOMEM);
+
+ CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
+ osd_seq->oos_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(info, osd, osd_seq);
+ if (rc != 0)
+ GOTO(cleanup, rc);
+
+ write_lock(&map->om_seq_list_lock);
+ cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
+ write_unlock(&map->om_seq_list_lock);
+
+cleanup:
+ mutex_unlock(&map->om_dir_init_mutex);
+ if (rc != 0) {
+ if (osd_seq != NULL)
+ OBD_FREE_PTR(osd_seq);
+ RETURN(ERR_PTR(rc));
+ }
+
+ RETURN(osd_seq);