drbd_req.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. drbd_req.h
  4. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  5. Copyright (C) 2006-2008, LINBIT Information Technologies GmbH.
  6. Copyright (C) 2006-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. Copyright (C) 2006-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  8. */
  9. #ifndef _DRBD_REQ_H
  10. #define _DRBD_REQ_H
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/drbd.h>
  14. #include "drbd_int.h"
  15. /* The request callbacks will be called in irq context by the IDE drivers,
  16. and in Softirqs/Tasklets/BH context by the SCSI drivers,
  17. and by the receiver and worker in kernel-thread context.
  18. Try to get the locking right :) */
  19. /*
  20. * Objects of type struct drbd_request do only exist on a R_PRIMARY node, and are
  21. * associated with IO requests originating from the block layer above us.
  22. *
  23. * There are quite a few things that may happen to a drbd request
  24. * during its lifetime.
  25. *
  26. * It will be created.
  27. * It will be marked with the intention to be
  28. * submitted to local disk and/or
  29. * send via the network.
  30. *
  31. * It has to be placed on the transfer log and other housekeeping lists,
  32. * In case we have a network connection.
  33. *
  34. * It may be identified as a concurrent (write) request
  35. * and be handled accordingly.
  36. *
  37. * It may me handed over to the local disk subsystem.
  38. * It may be completed by the local disk subsystem,
  39. * either successfully or with io-error.
  40. * In case it is a READ request, and it failed locally,
  41. * it may be retried remotely.
  42. *
  43. * It may be queued for sending.
  44. * It may be handed over to the network stack,
  45. * which may fail.
  46. * It may be acknowledged by the "peer" according to the wire_protocol in use.
  47. * this may be a negative ack.
  48. * It may receive a faked ack when the network connection is lost and the
  49. * transfer log is cleaned up.
  50. * Sending may be canceled due to network connection loss.
  51. * When it finally has outlived its time,
  52. * corresponding dirty bits in the resync-bitmap may be cleared or set,
  53. * it will be destroyed,
  54. * and completion will be signalled to the originator,
  55. * with or without "success".
  56. */
  57. enum drbd_req_event {
  58. CREATED,
  59. TO_BE_SENT,
  60. TO_BE_SUBMITTED,
  61. /* XXX yes, now I am inconsistent...
  62. * these are not "events" but "actions"
  63. * oh, well... */
  64. QUEUE_FOR_NET_WRITE,
  65. QUEUE_FOR_NET_READ,
  66. QUEUE_FOR_SEND_OOS,
  67. /* An empty flush is queued as P_BARRIER,
  68. * which will cause it to complete "successfully",
  69. * even if the local disk flush failed.
  70. *
  71. * Just like "real" requests, empty flushes (blkdev_issue_flush()) will
  72. * only see an error if neither local nor remote data is reachable. */
  73. QUEUE_AS_DRBD_BARRIER,
  74. SEND_CANCELED,
  75. SEND_FAILED,
  76. HANDED_OVER_TO_NETWORK,
  77. OOS_HANDED_TO_NETWORK,
  78. CONNECTION_LOST_WHILE_PENDING,
  79. READ_RETRY_REMOTE_CANCELED,
  80. RECV_ACKED_BY_PEER,
  81. WRITE_ACKED_BY_PEER,
  82. WRITE_ACKED_BY_PEER_AND_SIS, /* and set_in_sync */
  83. CONFLICT_RESOLVED,
  84. POSTPONE_WRITE,
  85. NEG_ACKED,
  86. BARRIER_ACKED, /* in protocol A and B */
  87. DATA_RECEIVED, /* (remote read) */
  88. COMPLETED_OK,
  89. READ_COMPLETED_WITH_ERROR,
  90. READ_AHEAD_COMPLETED_WITH_ERROR,
  91. WRITE_COMPLETED_WITH_ERROR,
  92. DISCARD_COMPLETED_NOTSUPP,
  93. DISCARD_COMPLETED_WITH_ERROR,
  94. ABORT_DISK_IO,
  95. RESEND,
  96. FAIL_FROZEN_DISK_IO,
  97. RESTART_FROZEN_DISK_IO,
  98. NOTHING,
  99. };
  100. /* encoding of request states for now. we don't actually need that many bits.
  101. * we don't need to do atomic bit operations either, since most of the time we
  102. * need to look at the connection state and/or manipulate some lists at the
  103. * same time, so we should hold the request lock anyways.
  104. */
  105. enum drbd_req_state_bits {
  106. /* 3210
  107. * 0000: no local possible
  108. * 0001: to be submitted
  109. * UNUSED, we could map: 011: submitted, completion still pending
  110. * 0110: completed ok
  111. * 0010: completed with error
  112. * 1001: Aborted (before completion)
  113. * 1x10: Aborted and completed -> free
  114. */
  115. __RQ_LOCAL_PENDING,
  116. __RQ_LOCAL_COMPLETED,
  117. __RQ_LOCAL_OK,
  118. __RQ_LOCAL_ABORTED,
  119. /* 87654
  120. * 00000: no network possible
  121. * 00001: to be send
  122. * 00011: to be send, on worker queue
  123. * 00101: sent, expecting recv_ack (B) or write_ack (C)
  124. * 11101: sent,
  125. * recv_ack (B) or implicit "ack" (A),
  126. * still waiting for the barrier ack.
  127. * master_bio may already be completed and invalidated.
  128. * 11100: write acked (C),
  129. * data received (for remote read, any protocol)
  130. * or finally the barrier ack has arrived (B,A)...
  131. * request can be freed
  132. * 01100: neg-acked (write, protocol C)
  133. * or neg-d-acked (read, any protocol)
  134. * or killed from the transfer log
  135. * during cleanup after connection loss
  136. * request can be freed
  137. * 01000: canceled or send failed...
  138. * request can be freed
  139. */
  140. /* if "SENT" is not set, yet, this can still fail or be canceled.
  141. * if "SENT" is set already, we still wait for an Ack packet.
  142. * when cleared, the master_bio may be completed.
  143. * in (B,A) the request object may still linger on the transaction log
  144. * until the corresponding barrier ack comes in */
  145. __RQ_NET_PENDING,
  146. /* If it is QUEUED, and it is a WRITE, it is also registered in the
  147. * transfer log. Currently we need this flag to avoid conflicts between
  148. * worker canceling the request and tl_clear_barrier killing it from
  149. * transfer log. We should restructure the code so this conflict does
  150. * no longer occur. */
  151. __RQ_NET_QUEUED,
  152. /* well, actually only "handed over to the network stack".
  153. *
  154. * TODO can potentially be dropped because of the similar meaning
  155. * of RQ_NET_SENT and ~RQ_NET_QUEUED.
  156. * however it is not exactly the same. before we drop it
  157. * we must ensure that we can tell a request with network part
  158. * from a request without, regardless of what happens to it. */
  159. __RQ_NET_SENT,
  160. /* when set, the request may be freed (if RQ_NET_QUEUED is clear).
  161. * basically this means the corresponding P_BARRIER_ACK was received */
  162. __RQ_NET_DONE,
  163. /* whether or not we know (C) or pretend (B,A) that the write
  164. * was successfully written on the peer.
  165. */
  166. __RQ_NET_OK,
  167. /* peer called drbd_set_in_sync() for this write */
  168. __RQ_NET_SIS,
  169. /* keep this last, its for the RQ_NET_MASK */
  170. __RQ_NET_MAX,
  171. /* Set when this is a write, clear for a read */
  172. __RQ_WRITE,
  173. __RQ_WSAME,
  174. __RQ_UNMAP,
  175. __RQ_ZEROES,
  176. /* Should call drbd_al_complete_io() for this request... */
  177. __RQ_IN_ACT_LOG,
  178. /* This was the most recent request during some blk_finish_plug()
  179. * or its implicit from-schedule equivalent.
  180. * We may use it as hint to send a P_UNPLUG_REMOTE */
  181. __RQ_UNPLUG,
  182. /* The peer has sent a retry ACK */
  183. __RQ_POSTPONED,
  184. /* would have been completed,
  185. * but was not, because of drbd_suspended() */
  186. __RQ_COMPLETION_SUSP,
  187. /* We expect a receive ACK (wire proto B) */
  188. __RQ_EXP_RECEIVE_ACK,
  189. /* We expect a write ACK (wite proto C) */
  190. __RQ_EXP_WRITE_ACK,
  191. /* waiting for a barrier ack, did an extra kref_get */
  192. __RQ_EXP_BARR_ACK,
  193. };
  194. #define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)
  195. #define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)
  196. #define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)
  197. #define RQ_LOCAL_ABORTED (1UL << __RQ_LOCAL_ABORTED)
  198. #define RQ_LOCAL_MASK ((RQ_LOCAL_ABORTED << 1)-1)
  199. #define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)
  200. #define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)
  201. #define RQ_NET_SENT (1UL << __RQ_NET_SENT)
  202. #define RQ_NET_DONE (1UL << __RQ_NET_DONE)
  203. #define RQ_NET_OK (1UL << __RQ_NET_OK)
  204. #define RQ_NET_SIS (1UL << __RQ_NET_SIS)
  205. #define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)
  206. #define RQ_WRITE (1UL << __RQ_WRITE)
  207. #define RQ_WSAME (1UL << __RQ_WSAME)
  208. #define RQ_UNMAP (1UL << __RQ_UNMAP)
  209. #define RQ_ZEROES (1UL << __RQ_ZEROES)
  210. #define RQ_IN_ACT_LOG (1UL << __RQ_IN_ACT_LOG)
  211. #define RQ_UNPLUG (1UL << __RQ_UNPLUG)
  212. #define RQ_POSTPONED (1UL << __RQ_POSTPONED)
  213. #define RQ_COMPLETION_SUSP (1UL << __RQ_COMPLETION_SUSP)
  214. #define RQ_EXP_RECEIVE_ACK (1UL << __RQ_EXP_RECEIVE_ACK)
  215. #define RQ_EXP_WRITE_ACK (1UL << __RQ_EXP_WRITE_ACK)
  216. #define RQ_EXP_BARR_ACK (1UL << __RQ_EXP_BARR_ACK)
  217. /* For waking up the frozen transfer log mod_req() has to return if the request
  218. should be counted in the epoch object*/
  219. #define MR_WRITE 1
  220. #define MR_READ 2
  221. /* Short lived temporary struct on the stack.
  222. * We could squirrel the error to be returned into
  223. * bio->bi_iter.bi_size, or similar. But that would be too ugly. */
  224. struct bio_and_error {
  225. struct bio *bio;
  226. int error;
  227. };
  228. extern void start_new_tl_epoch(struct drbd_connection *connection);
  229. extern void drbd_req_destroy(struct kref *kref);
  230. extern int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  231. struct drbd_peer_device *peer_device,
  232. struct bio_and_error *m);
  233. extern void complete_master_bio(struct drbd_device *device,
  234. struct bio_and_error *m);
  235. extern void request_timer_fn(struct timer_list *t);
  236. extern void tl_restart(struct drbd_connection *connection, enum drbd_req_event what);
  237. extern void _tl_restart(struct drbd_connection *connection, enum drbd_req_event what);
  238. extern void tl_abort_disk_io(struct drbd_device *device);
  239. /* this is in drbd_main.c */
  240. extern void drbd_restart_request(struct drbd_request *req);
  241. /* use this if you don't want to deal with calling complete_master_bio()
  242. * outside the spinlock, e.g. when walking some list on cleanup. */
  243. static inline int _req_mod(struct drbd_request *req, enum drbd_req_event what,
  244. struct drbd_peer_device *peer_device)
  245. {
  246. struct drbd_device *device = req->device;
  247. struct bio_and_error m;
  248. int rv;
  249. /* __req_mod possibly frees req, do not touch req after that! */
  250. rv = __req_mod(req, what, peer_device, &m);
  251. if (m.bio)
  252. complete_master_bio(device, &m);
  253. return rv;
  254. }
  255. /* completion of master bio is outside of our spinlock.
  256. * We still may or may not be inside some irqs disabled section
  257. * of the lower level driver completion callback, so we need to
  258. * spin_lock_irqsave here. */
  259. static inline int req_mod(struct drbd_request *req,
  260. enum drbd_req_event what,
  261. struct drbd_peer_device *peer_device)
  262. {
  263. unsigned long flags;
  264. struct drbd_device *device = req->device;
  265. struct bio_and_error m;
  266. int rv;
  267. spin_lock_irqsave(&device->resource->req_lock, flags);
  268. rv = __req_mod(req, what, peer_device, &m);
  269. spin_unlock_irqrestore(&device->resource->req_lock, flags);
  270. if (m.bio)
  271. complete_master_bio(device, &m);
  272. return rv;
  273. }
  274. extern bool drbd_should_do_remote(union drbd_dev_state);
  275. #endif