drbd_req.h 11 KB

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