+static inline int osp_create_end_seq(const struct lu_env *env,
+ struct osp_device *osp)
+{
+ struct lu_fid *fid = &osp->opd_pre_used_fid;
+ int rc;
+
+ spin_lock(&osp->opd_pre_lock);
+ rc = osp_fid_end_seq(env, fid);
+ spin_unlock(&osp->opd_pre_lock);
+ return rc;
+}
+
+/**
+ * Write fid into last_oid/last_seq file.
+ **/
+int osp_write_last_oid_seq_files(struct lu_env *env, struct osp_device *osp,
+ struct lu_fid *fid, int sync)
+{
+ struct osp_thread_info *oti = osp_env_info(env);
+ struct lu_buf *lb_oid = &oti->osi_lb;
+ struct lu_buf *lb_oseq = &oti->osi_lb2;
+ loff_t oid_off;
+ loff_t oseq_off;
+ struct thandle *th;
+ int rc;
+ ENTRY;
+
+ /* Note: through f_oid is only 32bits, it will also write
+ * 64 bits for oid to keep compatiblity with the previous
+ * version. */
+ lb_oid->lb_buf = &fid->f_oid;
+ lb_oid->lb_len = sizeof(obd_id);
+ oid_off = sizeof(obd_id) * osp->opd_index;
+
+ lb_oseq->lb_buf = &fid->f_seq;
+ lb_oseq->lb_len = sizeof(obd_id);
+ oseq_off = sizeof(obd_id) * osp->opd_index;
+
+ th = dt_trans_create(env, osp->opd_storage);
+ if (IS_ERR(th))
+ RETURN(PTR_ERR(th));
+
+ th->th_sync |= sync;
+ rc = dt_declare_record_write(env, osp->opd_last_used_oid_file,
+ lb_oid->lb_len, oid_off, th);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ rc = dt_declare_record_write(env, osp->opd_last_used_seq_file,
+ lb_oseq->lb_len, oseq_off, th);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ rc = dt_trans_start_local(env, osp->opd_storage, th);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ rc = dt_record_write(env, osp->opd_last_used_oid_file, lb_oid,
+ &oid_off, th);
+ if (rc != 0) {
+ CERROR("%s: can not write to last seq file: rc = %d\n",
+ osp->opd_obd->obd_name, rc);
+ GOTO(out, rc);
+ }
+ rc = dt_record_write(env, osp->opd_last_used_seq_file, lb_oseq,
+ &oseq_off, th);
+ if (rc) {
+ CERROR("%s: can not write to last seq file: rc = %d\n",
+ osp->opd_obd->obd_name, rc);
+ GOTO(out, rc);
+ }
+out:
+ dt_trans_stop(env, osp->opd_storage, th);
+ RETURN(rc);
+}
+
+int osp_precreate_rollover_new_seq(struct lu_env *env, struct osp_device *osp)
+{
+ struct lu_fid *fid = &osp_env_info(env)->osi_fid;
+ struct lu_fid *last_fid = &osp->opd_last_used_fid;
+ int rc;
+ ENTRY;
+
+ rc = seq_client_get_seq(env, osp->opd_obd->u.cli.cl_seq, &fid->f_seq);
+ if (rc != 0) {
+ CERROR("%s: alloc fid error: rc = %d\n",
+ osp->opd_obd->obd_name, rc);
+ RETURN(rc);
+ }
+
+ fid->f_oid = 1;
+ fid->f_ver = 0;
+ LASSERTF(fid_seq(fid) != fid_seq(last_fid),
+ "fid "DFID", last_fid "DFID"\n", PFID(fid),
+ PFID(last_fid));
+
+ rc = osp_write_last_oid_seq_files(env, osp, fid, 1);
+ if (rc != 0) {
+ CERROR("%s: Can not update oid/seq file: rc = %d\n",
+ osp->opd_obd->obd_name, rc);
+ RETURN(rc);
+ }
+
+ LCONSOLE_INFO("%s: update sequence from "LPX64" to "LPX64"\n",
+ osp->opd_obd->obd_name, fid_seq(last_fid),
+ fid_seq(fid));
+ /* Update last_xxx to the new seq */
+ spin_lock(&osp->opd_pre_lock);
+ osp->opd_last_used_fid = *fid;
+ osp->opd_gap_start_fid = *fid;
+ osp->opd_pre_used_fid = *fid;
+ osp->opd_pre_last_created_fid = *fid;
+ spin_unlock(&osp->opd_pre_lock);
+
+ RETURN(rc);
+}
+
+/**
+ * alloc fids for precreation.
+ * rc = 0 Success, @grow is the count of real allocation.
+ * rc = 1 Current seq is used up.
+ * rc < 0 Other error.
+ **/
+static int osp_precreate_fids(const struct lu_env *env, struct osp_device *osp,
+ struct lu_fid *fid, int *grow)