drbd_req.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 sucessfully 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_send,
  71. to_be_submitted,
  72. /* XXX yes, now I am inconsistent...
  73. * these two are not "events" but "actions"
  74. * oh, well... */
  75. queue_for_net_write,
  76. queue_for_net_read,
  77. send_canceled,
  78. send_failed,
  79. handed_over_to_network,
  80. connection_lost_while_pending,
  81. recv_acked_by_peer,
  82. write_acked_by_peer,
  83. write_acked_by_peer_and_sis, /* and set_in_sync */
  84. conflict_discarded_by_peer,
  85. neg_acked,
  86. barrier_acked, /* in protocol A and B */
  87. data_received, /* (remote read) */
  88. read_completed_with_error,
  89. read_ahead_completed_with_error,
  90. write_completed_with_error,
  91. completed_ok,
  92. nothing, /* for tracing only */
  93. };
  94. /* encoding of request states for now. we don't actually need that many bits.
  95. * we don't need to do atomic bit operations either, since most of the time we
  96. * need to look at the connection state and/or manipulate some lists at the
  97. * same time, so we should hold the request lock anyways.
  98. */
  99. enum drbd_req_state_bits {
  100. /* 210
  101. * 000: no local possible
  102. * 001: to be submitted
  103. * UNUSED, we could map: 011: submitted, completion still pending
  104. * 110: completed ok
  105. * 010: completed with error
  106. */
  107. __RQ_LOCAL_PENDING,
  108. __RQ_LOCAL_COMPLETED,
  109. __RQ_LOCAL_OK,
  110. /* 76543
  111. * 00000: no network possible
  112. * 00001: to be send
  113. * 00011: to be send, on worker queue
  114. * 00101: sent, expecting recv_ack (B) or write_ack (C)
  115. * 11101: sent,
  116. * recv_ack (B) or implicit "ack" (A),
  117. * still waiting for the barrier ack.
  118. * master_bio may already be completed and invalidated.
  119. * 11100: write_acked (C),
  120. * data_received (for remote read, any protocol)
  121. * or finally the barrier ack has arrived (B,A)...
  122. * request can be freed
  123. * 01100: neg-acked (write, protocol C)
  124. * or neg-d-acked (read, any protocol)
  125. * or killed from the transfer log
  126. * during cleanup after connection loss
  127. * request can be freed
  128. * 01000: canceled or send failed...
  129. * request can be freed
  130. */
  131. /* if "SENT" is not set, yet, this can still fail or be canceled.
  132. * if "SENT" is set already, we still wait for an Ack packet.
  133. * when cleared, the master_bio may be completed.
  134. * in (B,A) the request object may still linger on the transaction log
  135. * until the corresponding barrier ack comes in */
  136. __RQ_NET_PENDING,
  137. /* If it is QUEUED, and it is a WRITE, it is also registered in the
  138. * transfer log. Currently we need this flag to avoid conflicts between
  139. * worker canceling the request and tl_clear_barrier killing it from
  140. * transfer log. We should restructure the code so this conflict does
  141. * no longer occur. */
  142. __RQ_NET_QUEUED,
  143. /* well, actually only "handed over to the network stack".
  144. *
  145. * TODO can potentially be dropped because of the similar meaning
  146. * of RQ_NET_SENT and ~RQ_NET_QUEUED.
  147. * however it is not exactly the same. before we drop it
  148. * we must ensure that we can tell a request with network part
  149. * from a request without, regardless of what happens to it. */
  150. __RQ_NET_SENT,
  151. /* when set, the request may be freed (if RQ_NET_QUEUED is clear).
  152. * basically this means the corresponding P_BARRIER_ACK was received */
  153. __RQ_NET_DONE,
  154. /* whether or not we know (C) or pretend (B,A) that the write
  155. * was successfully written on the peer.
  156. */
  157. __RQ_NET_OK,
  158. /* peer called drbd_set_in_sync() for this write */
  159. __RQ_NET_SIS,
  160. /* keep this last, its for the RQ_NET_MASK */
  161. __RQ_NET_MAX,
  162. };
  163. #define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)
  164. #define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)
  165. #define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)
  166. #define RQ_LOCAL_MASK ((RQ_LOCAL_OK << 1)-1) /* 0x07 */
  167. #define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)
  168. #define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)
  169. #define RQ_NET_SENT (1UL << __RQ_NET_SENT)
  170. #define RQ_NET_DONE (1UL << __RQ_NET_DONE)
  171. #define RQ_NET_OK (1UL << __RQ_NET_OK)
  172. #define RQ_NET_SIS (1UL << __RQ_NET_SIS)
  173. /* 0x1f8 */
  174. #define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)
  175. /* epoch entries */
  176. static inline
  177. struct hlist_head *ee_hash_slot(struct drbd_conf *mdev, sector_t sector)
  178. {
  179. BUG_ON(mdev->ee_hash_s == 0);
  180. return mdev->ee_hash +
  181. ((unsigned int)(sector>>HT_SHIFT) % mdev->ee_hash_s);
  182. }
  183. /* transfer log (drbd_request objects) */
  184. static inline
  185. struct hlist_head *tl_hash_slot(struct drbd_conf *mdev, sector_t sector)
  186. {
  187. BUG_ON(mdev->tl_hash_s == 0);
  188. return mdev->tl_hash +
  189. ((unsigned int)(sector>>HT_SHIFT) % mdev->tl_hash_s);
  190. }
  191. /* application reads (drbd_request objects) */
  192. static struct hlist_head *ar_hash_slot(struct drbd_conf *mdev, sector_t sector)
  193. {
  194. return mdev->app_reads_hash
  195. + ((unsigned int)(sector) % APP_R_HSIZE);
  196. }
  197. /* when we receive the answer for a read request,
  198. * verify that we actually know about it */
  199. static inline struct drbd_request *_ar_id_to_req(struct drbd_conf *mdev,
  200. u64 id, sector_t sector)
  201. {
  202. struct hlist_head *slot = ar_hash_slot(mdev, sector);
  203. struct hlist_node *n;
  204. struct drbd_request *req;
  205. hlist_for_each_entry(req, n, slot, colision) {
  206. if ((unsigned long)req == (unsigned long)id) {
  207. D_ASSERT(req->sector == sector);
  208. return req;
  209. }
  210. }
  211. return NULL;
  212. }
  213. static inline struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
  214. struct bio *bio_src)
  215. {
  216. struct bio *bio;
  217. struct drbd_request *req =
  218. mempool_alloc(drbd_request_mempool, GFP_NOIO);
  219. if (likely(req)) {
  220. bio = bio_clone(bio_src, GFP_NOIO); /* XXX cannot fail?? */
  221. req->rq_state = 0;
  222. req->mdev = mdev;
  223. req->master_bio = bio_src;
  224. req->private_bio = bio;
  225. req->epoch = 0;
  226. req->sector = bio->bi_sector;
  227. req->size = bio->bi_size;
  228. req->start_time = jiffies;
  229. INIT_HLIST_NODE(&req->colision);
  230. INIT_LIST_HEAD(&req->tl_requests);
  231. INIT_LIST_HEAD(&req->w.list);
  232. bio->bi_private = req;
  233. bio->bi_end_io = drbd_endio_pri;
  234. bio->bi_next = NULL;
  235. }
  236. return req;
  237. }
  238. static inline void drbd_req_free(struct drbd_request *req)
  239. {
  240. mempool_free(req, drbd_request_mempool);
  241. }
  242. static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
  243. {
  244. return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
  245. }
  246. /* Short lived temporary struct on the stack.
  247. * We could squirrel the error to be returned into
  248. * bio->bi_size, or similar. But that would be too ugly. */
  249. struct bio_and_error {
  250. struct bio *bio;
  251. int error;
  252. };
  253. extern void _req_may_be_done(struct drbd_request *req,
  254. struct bio_and_error *m);
  255. extern void __req_mod(struct drbd_request *req, enum drbd_req_event what,
  256. struct bio_and_error *m);
  257. extern void complete_master_bio(struct drbd_conf *mdev,
  258. struct bio_and_error *m);
  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 void _req_mod(struct drbd_request *req, enum drbd_req_event what)
  262. {
  263. struct drbd_conf *mdev = req->mdev;
  264. struct bio_and_error m;
  265. /* __req_mod possibly frees req, do not touch req after that! */
  266. __req_mod(req, what, &m);
  267. if (m.bio)
  268. complete_master_bio(mdev, &m);
  269. }
  270. /* completion of master bio is outside of spinlock.
  271. * If you need it irqsave, do it your self! */
  272. static inline void req_mod(struct drbd_request *req,
  273. enum drbd_req_event what)
  274. {
  275. struct drbd_conf *mdev = req->mdev;
  276. struct bio_and_error m;
  277. spin_lock_irq(&mdev->req_lock);
  278. __req_mod(req, what, &m);
  279. spin_unlock_irq(&mdev->req_lock);
  280. if (m.bio)
  281. complete_master_bio(mdev, &m);
  282. }
  283. #endif