+ 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 32 bits, it will also write 64 bits
+ * for oid to keep compatibility with the previous version. */
+ lb_oid->lb_buf = &fid->f_oid;
+ lb_oid->lb_len = sizeof(u64);
+ oid_off = sizeof(u64) * osp->opd_index;
+
+ lb_oseq->lb_buf = &fid->f_seq;
+ lb_oseq->lb_len = sizeof(u64);
+ oseq_off = sizeof(u64) * 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, oid_off, th);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ rc = dt_declare_record_write(env, osp->opd_last_used_seq_file,
+ lb_oseq, 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);
+}
+
+/**
+ * Switch to another sequence
+ *
+ * When a current sequence has no available IDs left, OSP has to switch to
+ * another new sequence. OSP requests it using the regular FLDB protocol
+ * and stores synchronously before that is used in precreated. This is needed
+ * to basically have the sequences referenced (not orphaned), otherwise it's
+ * possible that OST has some objects precreated and the clients have data
+ * written to it, but after MDT failover nobody refers those objects and OSP
+ * has no idea that the sequence need cleanup to be done.
+ * While this is very expensive operation, it's supposed to happen very very
+ * infrequently because sequence has 2^32 or 2^48 objects (depending on type)
+ *
+ * \param[in] env LU environment provided by the caller
+ * \param[in] osp OSP device
+ *
+ * \retval 0 on success
+ * \retval negative negated errno on error
+ */
+static 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);
+}
+
+/**
+ * Find IDs available in current sequence
+ *
+ * The function calculates the highest possible ID and the number of IDs
+ * available in the current sequence OSP is using. The number is limited
+ * artifically by the caller (grow param) and the number of IDs available
+ * in the sequence by nature. The function doesn't require an external
+ * locking.
+ *
+ * \param[in] env LU environment provided by the caller
+ * \param[in] osp OSP device
+ * \param[in] fid FID the caller wants to start with
+ * \param[in] grow how many the caller wants
+ * \param[out] fid the highest calculated FID
+ * \param[out] grow the number of available IDs calculated
+ *
+ * \retval 0 on success, 1 - the sequence is empty
+ */
+static int osp_precreate_fids(const struct lu_env *env, struct osp_device *osp,
+ struct lu_fid *fid, int *grow)
+{
+ struct osp_thread_info *osi = osp_env_info(env);
+ __u64 end;
+ int i = 0;
+
+ if (fid_is_idif(fid)) {
+ struct lu_fid *last_fid;
+ struct ost_id *oi = &osi->osi_oi;
+
+ spin_lock(&osp->opd_pre_lock);
+ last_fid = &osp->opd_pre_last_created_fid;
+ fid_to_ostid(last_fid, oi);
+ end = min(ostid_id(oi) + *grow, IDIF_MAX_OID);
+ *grow = end - ostid_id(oi);
+ ostid_set_id(oi, ostid_id(oi) + *grow);
+ spin_unlock(&osp->opd_pre_lock);
+
+ if (*grow == 0)
+ return 1;
+
+ ostid_to_fid(fid, oi, osp->opd_index);
+ return 0;
+ }
+
+ spin_lock(&osp->opd_pre_lock);
+ *fid = osp->opd_pre_last_created_fid;
+ end = fid->f_oid;
+ end = min((end + *grow), (__u64)LUSTRE_DATA_SEQ_MAX_WIDTH);
+ *grow = end - fid->f_oid;
+ fid->f_oid += end - fid->f_oid;
+ spin_unlock(&osp->opd_pre_lock);
+
+ CDEBUG(D_INFO, "Expect %d, actual %d ["DFID" -- "DFID"]\n",
+ *grow, i, PFID(fid), PFID(&osp->opd_pre_last_created_fid));
+
+ return *grow > 0 ? 0 : 1;
+}
+
+/**
+ * Prepare and send precreate RPC
+ *
+ * The function finds how many objects should be precreated. Then allocates,
+ * prepares and schedules precreate RPC synchronously. Upon reply the function
+ * wake ups the threads waiting for the new objects on this target. If the
+ * target wasn't able to create all the objects requested, then the next
+ * precreate will be asking less objects (i.e. slow precreate down).
+ *
+ * \param[in] env LU environment provided by the caller
+ * \param[in] d OSP device
+ *
+ * \retval 0 on success
+ * \retval negative negated errno on error
+ **/
+static int osp_precreate_send(const struct lu_env *env, struct osp_device *d)
+{
+ struct osp_thread_info *oti = osp_env_info(env);