+/* number of slots in reply bitmap */
+#define LUT_REPLY_SLOTS_PER_CHUNK (1<<20)
+#define LUT_REPLY_SLOTS_MAX_CHUNKS 16
+
+/**
+ * Target reply data
+ */
+struct tg_reply_data {
+ /** chain of reply data anchored in tg_export_data */
+ struct list_head trd_list;
+ /** copy of on-disk reply data */
+ struct lsd_reply_data trd_reply;
+ /** versions for Version Based Recovery */
+ __u64 trd_pre_versions[4];
+ /** slot index in reply_data file */
+ int trd_index;
+ /** tag the client used */
+ __u16 trd_tag;
+};
+
+extern struct lu_context_key tgt_session_key;
+
+struct tgt_session_info {
+ /*
+ * The following members will be filled explicitly
+ * with specific data in tgt_ses_init().
+ */
+ struct req_capsule *tsi_pill;
+
+ /*
+ * Lock request for "habeo clavis" operations.
+ */
+ struct ldlm_request *tsi_dlm_req;
+
+ /* although we have export in req, there are cases when it is not
+ * available, e.g. closing files upon export destroy */
+ struct obd_export *tsi_exp;
+ const struct lu_env *tsi_env;
+ struct lu_target *tsi_tgt;
+
+ const struct mdt_body *tsi_mdt_body;
+ struct ost_body *tsi_ost_body;
+ struct lu_object *tsi_corpus;
+
+ struct lu_fid tsi_fid;
+ struct ldlm_res_id tsi_resid;
+
+ /* object affected by VBR, for last_rcvd_update */
+ struct dt_object *tsi_vbr_obj;
+ /* opdata for mdt_reint_open(), has the same value as
+ * ldlm_reply:lock_policy_res1. The tgt_update_last_rcvd() stores
+ * this value onto disk for recovery when tgt_txn_stop_cb() is called.
+ */
+ __u64 tsi_opdata;
+
+ /*
+ * Additional fail id that can be set by handler.
+ */
+ int tsi_reply_fail_id;
+ bool tsi_preprocessed;
+ /* request JobID */
+ char *tsi_jobid;
+
+ /* update replay */
+ __u64 tsi_xid;
+ __u32 tsi_result;
+ __u32 tsi_client_gen;
+};
+
+static inline struct tgt_session_info *tgt_ses_info(const struct lu_env *env)
+{
+ struct tgt_session_info *tsi;
+
+ LASSERT(env->le_ses != NULL);
+ tsi = lu_context_key_get(env->le_ses, &tgt_session_key);
+ LASSERT(tsi);
+ return tsi;
+}
+
+static inline void tgt_vbr_obj_set(const struct lu_env *env,
+ struct dt_object *obj)
+{
+ struct tgt_session_info *tsi;
+
+ if (env->le_ses != NULL) {
+ tsi = tgt_ses_info(env);
+ tsi->tsi_vbr_obj = obj;
+ }
+}
+
+static inline void tgt_opdata_set(const struct lu_env *env, __u64 flags)
+{
+ struct tgt_session_info *tsi;
+
+ if (env->le_ses != NULL) {
+ tsi = tgt_ses_info(env);
+ tsi->tsi_opdata |= flags;
+ }
+}
+
+static inline void tgt_opdata_clear(const struct lu_env *env, __u64 flags)
+{
+ struct tgt_session_info *tsi;
+
+ if (env->le_ses != NULL) {
+ tsi = tgt_ses_info(env);
+ tsi->tsi_opdata &= ~flags;
+ }
+}
+
+/*
+ * Generic unified target support.
+ */
+enum tgt_handler_flags {
+ /*
+ * struct *_body is passed in the incoming message, and object
+ * identified by this fid exists on disk.
+ * *
+ * "habeo corpus" == "I have a body"
+ */
+ HABEO_CORPUS = (1 << 0),
+ /*
+ * struct ldlm_request is passed in the incoming message.
+ *
+ * "habeo clavis" == "I have a key"
+ * */
+ HABEO_CLAVIS = (1 << 1),
+ /*
+ * this request has fixed reply format, so that reply message can be
+ * packed by generic code.
+ *
+ * "habeo refero" == "I have a reply"
+ */
+ HABEO_REFERO = (1 << 2),
+ /*
+ * this request will modify something, so check whether the file system
+ * is readonly or not, then return -EROFS to client asap if necessary.
+ *
+ * "mutabor" == "I shall modify"
+ */
+ MUTABOR = (1 << 3)
+};
+
+struct tgt_handler {
+ /* The name of this handler. */
+ const char *th_name;
+ /* Fail id, check at the beginning */
+ int th_fail_id;
+ /* Operation code */
+ __u32 th_opc;
+ /* Flags in enum tgt_handler_flags */
+ __u32 th_flags;
+ /* Request version for this opcode */
+ int th_version;
+ /* Handler function */
+ int (*th_act)(struct tgt_session_info *tsi);
+ /* Handler function for high priority requests */
+ void (*th_hp)(struct tgt_session_info *tsi);
+ /* Request format for this request */
+ const struct req_format *th_fmt;
+};
+
+struct tgt_opc_slice {
+ __u32 tos_opc_start; /* First op code */
+ __u32 tos_opc_end; /* Last op code */
+ struct tgt_handler *tos_hs; /* Registered handler */
+};
+
+static inline struct ptlrpc_request *tgt_ses_req(struct tgt_session_info *tsi)
+{
+ return tsi->tsi_pill ? tsi->tsi_pill->rc_req : NULL;
+}
+
+static inline __u64 tgt_conn_flags(struct tgt_session_info *tsi)
+{
+ LASSERT(tsi->tsi_exp);
+ return exp_connect_flags(tsi->tsi_exp);
+}
+
+static inline int req_is_replay(struct ptlrpc_request *req)
+{
+ LASSERT(req->rq_reqmsg);
+ return !!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY);
+}
+
+static inline bool tgt_is_multimodrpcs_client(struct obd_export *exp)
+{
+ return exp_connect_flags(exp) & OBD_CONNECT_MULTIMODRPCS;
+}
+
+
+/* target/tgt_handler.c */
+int tgt_request_handle(struct ptlrpc_request *req);
+char *tgt_name(struct lu_target *tgt);
+void tgt_counter_incr(struct obd_export *exp, int opcode);
+int tgt_connect_check_sptlrpc(struct ptlrpc_request *req,
+ struct obd_export *exp);
+int tgt_adapt_sptlrpc_conf(struct lu_target *tgt, int initial);
+int tgt_connect(struct tgt_session_info *tsi);
+int tgt_disconnect(struct tgt_session_info *uti);
+int tgt_obd_ping(struct tgt_session_info *tsi);
+int tgt_enqueue(struct tgt_session_info *tsi);
+int tgt_convert(struct tgt_session_info *tsi);
+int tgt_bl_callback(struct tgt_session_info *tsi);
+int tgt_cp_callback(struct tgt_session_info *tsi);
+int tgt_llog_open(struct tgt_session_info *tsi);
+int tgt_llog_close(struct tgt_session_info *tsi);
+int tgt_llog_destroy(struct tgt_session_info *tsi);
+int tgt_llog_read_header(struct tgt_session_info *tsi);
+int tgt_llog_next_block(struct tgt_session_info *tsi);
+int tgt_llog_prev_block(struct tgt_session_info *tsi);
+int tgt_sec_ctx_init(struct tgt_session_info *tsi);
+int tgt_sec_ctx_init_cont(struct tgt_session_info *tsi);
+int tgt_sec_ctx_fini(struct tgt_session_info *tsi);
+int tgt_sendpage(struct tgt_session_info *tsi, struct lu_rdpg *rdpg, int nob);
+int tgt_send_buffer(struct tgt_session_info *tsi, struct lu_rdbuf *rdbuf);
+int tgt_validate_obdo(struct tgt_session_info *tsi, struct obdo *oa);
+int tgt_sync(const struct lu_env *env, struct lu_target *tgt,
+ struct dt_object *obj, __u64 start, __u64 end);
+
+int tgt_io_thread_init(struct ptlrpc_thread *thread);
+void tgt_io_thread_done(struct ptlrpc_thread *thread);
+
+int tgt_extent_lock(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
+ __u64 start, __u64 end, struct lustre_handle *lh,
+ int mode, __u64 *flags);
+void tgt_extent_unlock(struct lustre_handle *lh, enum ldlm_mode mode);
+int tgt_brw_lock(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
+ struct obd_ioobj *obj, struct niobuf_remote *nb,
+ struct lustre_handle *lh, enum ldlm_mode mode);
+void tgt_brw_unlock(struct obd_ioobj *obj, struct niobuf_remote *niob,
+ struct lustre_handle *lh, enum ldlm_mode mode);
+int tgt_brw_read(struct tgt_session_info *tsi);
+int tgt_brw_write(struct tgt_session_info *tsi);
+int tgt_hpreq_handler(struct ptlrpc_request *req);
+void tgt_register_lfsck_in_notify(int (*notify)(const struct lu_env *,
+ struct dt_device *,
+ struct lfsck_request *,
+ struct thandle *));
+void tgt_register_lfsck_query(int (*query)(const struct lu_env *,
+ struct dt_device *,
+ struct lfsck_request *,
+ struct lfsck_reply *,
+ struct lfsck_query *));
+bool req_can_reconstruct(struct ptlrpc_request *req, struct tg_reply_data *trd);
+
+extern struct tgt_handler tgt_sec_ctx_handlers[];
+extern struct tgt_handler tgt_lfsck_handlers[];
+extern struct tgt_handler tgt_obd_handlers[];
+extern struct tgt_handler tgt_dlm_handlers[];
+extern struct tgt_handler tgt_llog_handlers[];
+extern struct tgt_handler tgt_out_handlers[];
+extern struct tgt_handler fld_handlers[];
+extern struct tgt_handler seq_handlers[];
+