drbd_req.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /* An empty flush is queued as P_BARRIER,
  79. * which will cause it to complete "successfully",
  80. * even if the local disk flush failed.
  81. *
  82. * Just like "real" requests, empty flushes (blkdev_issue_flush()) will
  83. * only see an error if neither local nor remote data is reachable. */
  84. QUEUE_AS_DRBD_BARRIER,
  85. SEND_CANCELED,
  86. SEND_FAILED,
  87. HANDED_OVER_TO_NETWORK,
  88. OOS_HANDED_TO_NETWORK,
  89. CONNECTION_LOST_WHILE_PENDING,
  90. READ_RETRY_REMOTE_CANCELED,
  91. RECV_ACKED_BY_PEER,
  92. WRITE_ACKED_BY_PEER,
  93. WRITE_ACKED_BY_PEER_AND_SIS, /* and set_in_sync */
  94. CONFLICT_RESOLVED,
  95. POSTPONE_WRITE,
  96. NEG_ACKED,
  97. BARRIER_ACKED, /* in protocol A and B */
  98. DATA_RECEIVED, /* (remote read) */
  99. READ_COMPLETED_WITH_ERROR,
  100. READ_AHEAD_COMPLETED_WITH_ERROR,
  101. WRITE_COMPLETED_WITH_ERROR,
  102. ABORT_DISK_IO,
  103. COMPLETED_OK,
  104. RESEND,
  105. FAIL_FROZEN_DISK_IO,
  106. RESTART_FROZEN_DISK_IO,
  107. NOTHING,
  108. };
  109. /* encoding of request states for now. we don't actually need that many bits.
  110. * we don't need to do atomic bit operations either, since most of the time we
  111. * need to look at the connection state and/or manipulate some lists at the
  112. * same time, so we should hold the request lock anyways.
  113. */
  114. enum drbd_req_state_bits {
  115. /* 3210
  116. * 0000: no local possible
  117. * 0001: to be submitted
  118. * UNUSED, we could map: 011: submitted, completion still pending
  119. * 0110: completed ok
  120. * 0010: completed with error
  121. * 1001: Aborted (before completion)
  122. * 1x10: Aborted and completed -> free
  123. */
  124. __RQ_LOCAL_PENDING,
  125. __RQ_LOCAL_COMPLETED,
  126. __RQ_LOCAL_OK,
  127. __RQ_LOCAL_ABORTED,
  128. /* 87654
  129. * 00000: no network possible
  130. * 00001: to be send
  131. * 00011: to be send, on worker queue
  132. * 00101: sent, expecting recv_ack (B) or write_ack (C)
  133. * 11101: sent,
  134. * recv_ack (B) or implicit "ack" (A),
  135. * still waiting for the barrier ack.
  136. * master_bio may already be completed and invalidated.
  137. * 11100: write acked (C),
  138. * data received (for remote read, any protocol)
  139. * or finally the barrier ack has arrived (B,A)...
  140. * request can be freed
  141. * 01100: neg-acked (write, protocol C)
  142. * or neg-d-acked (read, any protocol)
  143. * or killed from the transfer log
  144. * during cleanup after connection loss
  145. * request can be freed
  146. * 01000: canceled or send failed...
  147. * request can be freed
  148. */
  149. /* if "SENT" is not set, yet, this can still fail or be canceled.
  150. * if "SENT" is set already, we still wait for an Ack packet.
  151. * when cleared, the master_bio may be completed.
  152. * in (B,A) the request object may still linger on the transaction log
  153. * until the corresponding barrier ack comes in */
  154. __RQ_NET_PENDING,
  155. /* If it is QUEUED, and it is a WRITE, it is also registered in the
  156. * transfer log. Currently we need this flag to avoid conflicts between
  157. * worker canceling the request and tl_clear_barrier killing it from
  158. * transfer log. We should restructure the code so this conflict does
  159. * no longer occur. */
  160. __RQ_NET_QUEUED,
  161. /* well, actually only "handed over to the network stack".
  162. *
  163. * TODO can potentially be dropped because of the similar meaning
  164. * of RQ_NET_SENT and ~RQ_NET_QUEUED.
  165. * however it is not exactly the same. before we drop it
  166. * we must ensure that we can tell a request with network part
  167. * from a request without, regardless of what happens to it. */
  168. __RQ_NET_SENT,
  169. /* when set, the request may be freed (if RQ_NET_QUEUED is clear).
  170. * basically this means the corresponding P_BARRIER_ACK was received */
  171. __RQ_NET_DONE,
  172. /* whether or not we know (C) or pretend (B,A) that the write
  173. * was successfully written on the peer.
  174. */
  175. __RQ_NET_OK,
  176. /* peer called drbd_set_in_sync() for this write */
  177. __RQ_NET_SIS,
  178. /* keep this last, its for the RQ_NET_MASK */
  179. __RQ_NET_MAX,
  180. /* Set when this is a write, clear for a read */
  181. __RQ_WRITE,
  182. /* Should call drbd_al_complete_io() for this request... */
  183. __RQ_IN_ACT_LOG,
  184. /* The peer has sent a retry ACK */
  185. __RQ_POSTPONED,
  186. /* would have been completed,
  187. * but was not, because of drbd_suspended() */
  188. __RQ_COMPLETION_SUSP,
  189. /* We expect a receive ACK (wire proto B) */
  190. __RQ_EXP_RECEIVE_ACK,
  191. /* We expect a write ACK (wite proto C) */
  192. __RQ_EXP_WRITE_ACK,
  193. /* waiting for a barrier ack, did an extra kref_get */
  194. __RQ_EXP_BARR_ACK,
  195. };
  196. #define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)
  197. #define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)
  198. #define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)
  199. #define RQ_LOCAL_ABORTED (1UL << __RQ_LOCAL_ABORTED)
  200. #define RQ_LOCAL_MASK ((RQ_LOCAL_ABORTED << 1)-1)
  201. #define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)
  202. #define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)
  203. #define RQ_NET_SENT (1UL << __RQ_NET_SENT)
  204. #define RQ_NET_DONE (1UL << __RQ_NET_DONE)
  205. #define RQ_NET_OK (1UL << __RQ_NET_OK)
  206. #define RQ_NET_SIS (1UL << __RQ_NET_SIS)
  207. /* 0x1f8 */
  208. #define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)
  209. #define RQ_WRITE (1UL << __RQ_WRITE)
  210. #define RQ_IN_ACT_LOG (1UL << __RQ_IN_ACT_LOG)
  211. #define RQ_POSTPONED (1UL << __RQ_POSTPONED)
  212. #define RQ_COMPLETION_SUSP (1UL << __RQ_COMPLETION_SUSP)
  213. #define RQ_EXP_RECEIVE_ACK (1UL << __RQ_EXP_RECEIVE_ACK)
  214. #define RQ_EXP_WRITE_ACK (1UL << __RQ_EXP_WRITE_ACK)
  215. #define RQ_EXP_BARR_ACK (1UL << __RQ_EXP_BARR_ACK)
  216. /* For waking up the frozen transfer log mod_req() has to return if the request
  217. should be counted in the epoch object*/
  218. #define MR_WRITE 1
  219. #define MR_READ 2
  220. static inline void drbd_req_make_private_bio(struct drbd_request *req, struct bio *bio_src)
  221. {
  222. struct bio *bio;
  223. bio = bio_clone(bio_src, GFP_NOIO); /* XXX cannot fail?? */
  224. req->private_bio = bio;
  225. bio->bi_private = req;
  226. bio->bi_end_io = drbd_request_endio;
  227. bio->bi_next = NULL;
  228. }
  229. /* Short lived temporary struct on the stack.
  230. * We could squirrel the error to be returned into
  231. * bio->bi_size, or similar. But that would be too ugly. */
  232. struct bio_and_error {
  233. struct bio *bio;
  234. int error;
  235. };
  236. extern void start_new_tl_epoch(struct drbd_tconn *tconn);
  237. extern void drbd_req_destroy(struct kref *kref);
  238. extern void _req_may_be_done(struct drbd_request *req,
  239. struct bio_and_error *m);
  240. extern int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  241. struct bio_and_error *m);
  242. extern void complete_master_bio(struct drbd_conf *mdev,
  243. struct bio_and_error *m);
  244. extern void request_timer_fn(unsigned long data);
  245. extern void tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what);
  246. extern void _tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what);
  247. /* this is in drbd_main.c */
  248. extern void drbd_restart_request(struct drbd_request *req);
  249. /* use this if you don't want to deal with calling complete_master_bio()
  250. * outside the spinlock, e.g. when walking some list on cleanup. */
  251. static inline int _req_mod(struct drbd_request *req, enum drbd_req_event what)
  252. {
  253. struct drbd_conf *mdev = req->w.mdev;
  254. struct bio_and_error m;
  255. int rv;
  256. /* __req_mod possibly frees req, do not touch req after that! */
  257. rv = __req_mod(req, what, &m);
  258. if (m.bio)
  259. complete_master_bio(mdev, &m);
  260. return rv;
  261. }
  262. /* completion of master bio is outside of our spinlock.
  263. * We still may or may not be inside some irqs disabled section
  264. * of the lower level driver completion callback, so we need to
  265. * spin_lock_irqsave here. */
  266. static inline int req_mod(struct drbd_request *req,
  267. enum drbd_req_event what)
  268. {
  269. unsigned long flags;
  270. struct drbd_conf *mdev = req->w.mdev;
  271. struct bio_and_error m;
  272. int rv;
  273. spin_lock_irqsave(&mdev->tconn->req_lock, flags);
  274. rv = __req_mod(req, what, &m);
  275. spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
  276. if (m.bio)
  277. complete_master_bio(mdev, &m);
  278. return rv;
  279. }
  280. static inline bool drbd_should_do_remote(union drbd_dev_state s)
  281. {
  282. return s.pdsk == D_UP_TO_DATE ||
  283. (s.pdsk >= D_INCONSISTENT &&
  284. s.conn >= C_WF_BITMAP_T &&
  285. s.conn < C_AHEAD);
  286. /* Before proto 96 that was >= CONNECTED instead of >= C_WF_BITMAP_T.
  287. That is equivalent since before 96 IO was frozen in the C_WF_BITMAP*
  288. states. */
  289. }
  290. static inline bool drbd_should_send_out_of_sync(union drbd_dev_state s)
  291. {
  292. return s.conn == C_AHEAD || s.conn == C_WF_BITMAP_S;
  293. /* pdsk = D_INCONSISTENT as a consequence. Protocol 96 check not necessary
  294. since we enter state C_AHEAD only if proto >= 96 */
  295. }
  296. #endif