drbd_req.h 11 KB

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