drbd_req.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/autoconf.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/drbd.h>
  25. #include "drbd_int.h"
  26. #include "drbd_wrappers.h"
  27. /* The request callbacks will be called in irq context by the IDE drivers,
  28. and in Softirqs/Tasklets/BH context by the SCSI drivers,
  29. and by the receiver and worker in kernel-thread context.
  30. Try to get the locking right :) */
  31. /*
  32. * Objects of type struct drbd_request do only exist on a R_PRIMARY node, and are
  33. * associated with IO requests originating from the block layer above us.
  34. *
  35. * There are quite a few things that may happen to a drbd request
  36. * during its lifetime.
  37. *
  38. * It will be created.
  39. * It will be marked with the intention to be
  40. * submitted to local disk and/or
  41. * send via the network.
  42. *
  43. * It has to be placed on the transfer log and other housekeeping lists,
  44. * In case we have a network connection.
  45. *
  46. * It may be identified as a concurrent (write) request
  47. * and be handled accordingly.
  48. *
  49. * It may me handed over to the local disk subsystem.
  50. * It may be completed by the local disk subsystem,
  51. * either sucessfully or with io-error.
  52. * In case it is a READ request, and it failed locally,
  53. * it may be retried remotely.
  54. *
  55. * It may be queued for sending.
  56. * It may be handed over to the network stack,
  57. * which may fail.
  58. * It may be acknowledged by the "peer" according to the wire_protocol in use.
  59. * this may be a negative ack.
  60. * It may receive a faked ack when the network connection is lost and the
  61. * transfer log is cleaned up.
  62. * Sending may be canceled due to network connection loss.
  63. * When it finally has outlived its time,
  64. * corresponding dirty bits in the resync-bitmap may be cleared or set,
  65. * it will be destroyed,
  66. * and completion will be signalled to the originator,
  67. * with or without "success".
  68. */
  69. enum drbd_req_event {
  70. created,
  71. to_be_send,
  72. to_be_submitted,
  73. /* XXX yes, now I am inconsistent...
  74. * these two are not "events" but "actions"
  75. * oh, well... */
  76. queue_for_net_write,
  77. queue_for_net_read,
  78. send_canceled,
  79. send_failed,
  80. handed_over_to_network,
  81. connection_lost_while_pending,
  82. recv_acked_by_peer,
  83. write_acked_by_peer,
  84. write_acked_by_peer_and_sis, /* and set_in_sync */
  85. conflict_discarded_by_peer,
  86. neg_acked,
  87. barrier_acked, /* in protocol A and B */
  88. data_received, /* (remote read) */
  89. read_completed_with_error,
  90. read_ahead_completed_with_error,
  91. write_completed_with_error,
  92. completed_ok,
  93. nothing, /* for tracing only */
  94. };
  95. /* encoding of request states for now. we don't actually need that many bits.
  96. * we don't need to do atomic bit operations either, since most of the time we
  97. * need to look at the connection state and/or manipulate some lists at the
  98. * same time, so we should hold the request lock anyways.
  99. */
  100. enum drbd_req_state_bits {
  101. /* 210
  102. * 000: no local possible
  103. * 001: to be submitted
  104. * UNUSED, we could map: 011: submitted, completion still pending
  105. * 110: completed ok
  106. * 010: completed with error
  107. */
  108. __RQ_LOCAL_PENDING,
  109. __RQ_LOCAL_COMPLETED,
  110. __RQ_LOCAL_OK,
  111. /* 76543
  112. * 00000: no network possible
  113. * 00001: to be send
  114. * 00011: to be send, on worker queue
  115. * 00101: sent, expecting recv_ack (B) or write_ack (C)
  116. * 11101: sent,
  117. * recv_ack (B) or implicit "ack" (A),
  118. * still waiting for the barrier ack.
  119. * master_bio may already be completed and invalidated.
  120. * 11100: write_acked (C),
  121. * data_received (for remote read, any protocol)
  122. * or finally the barrier ack has arrived (B,A)...
  123. * request can be freed
  124. * 01100: neg-acked (write, protocol C)
  125. * or neg-d-acked (read, any protocol)
  126. * or killed from the transfer log
  127. * during cleanup after connection loss
  128. * request can be freed
  129. * 01000: canceled or send failed...
  130. * request can be freed
  131. */
  132. /* if "SENT" is not set, yet, this can still fail or be canceled.
  133. * if "SENT" is set already, we still wait for an Ack packet.
  134. * when cleared, the master_bio may be completed.
  135. * in (B,A) the request object may still linger on the transaction log
  136. * until the corresponding barrier ack comes in */
  137. __RQ_NET_PENDING,
  138. /* If it is QUEUED, and it is a WRITE, it is also registered in the
  139. * transfer log. Currently we need this flag to avoid conflicts between
  140. * worker canceling the request and tl_clear_barrier killing it from
  141. * transfer log. We should restructure the code so this conflict does
  142. * no longer occur. */
  143. __RQ_NET_QUEUED,
  144. /* well, actually only "handed over to the network stack".
  145. *
  146. * TODO can potentially be dropped because of the similar meaning
  147. * of RQ_NET_SENT and ~RQ_NET_QUEUED.
  148. * however it is not exactly the same. before we drop it
  149. * we must ensure that we can tell a request with network part
  150. * from a request without, regardless of what happens to it. */
  151. __RQ_NET_SENT,
  152. /* when set, the request may be freed (if RQ_NET_QUEUED is clear).
  153. * basically this means the corresponding P_BARRIER_ACK was received */
  154. __RQ_NET_DONE,
  155. /* whether or not we know (C) or pretend (B,A) that the write
  156. * was successfully written on the peer.
  157. */
  158. __RQ_NET_OK,
  159. /* peer called drbd_set_in_sync() for this write */
  160. __RQ_NET_SIS,
  161. /* keep this last, its for the RQ_NET_MASK */
  162. __RQ_NET_MAX,
  163. };
  164. #define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)
  165. #define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)
  166. #define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)
  167. #define RQ_LOCAL_MASK ((RQ_LOCAL_OK << 1)-1) /* 0x07 */
  168. #define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)
  169. #define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)
  170. #define RQ_NET_SENT (1UL << __RQ_NET_SENT)
  171. #define RQ_NET_DONE (1UL << __RQ_NET_DONE)
  172. #define RQ_NET_OK (1UL << __RQ_NET_OK)
  173. #define RQ_NET_SIS (1UL << __RQ_NET_SIS)
  174. /* 0x1f8 */
  175. #define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)
  176. /* epoch entries */
  177. static inline
  178. struct hlist_head *ee_hash_slot(struct drbd_conf *mdev, sector_t sector)
  179. {
  180. BUG_ON(mdev->ee_hash_s == 0);
  181. return mdev->ee_hash +
  182. ((unsigned int)(sector>>HT_SHIFT) % mdev->ee_hash_s);
  183. }
  184. /* transfer log (drbd_request objects) */
  185. static inline
  186. struct hlist_head *tl_hash_slot(struct drbd_conf *mdev, sector_t sector)
  187. {
  188. BUG_ON(mdev->tl_hash_s == 0);
  189. return mdev->tl_hash +
  190. ((unsigned int)(sector>>HT_SHIFT) % mdev->tl_hash_s);
  191. }
  192. /* application reads (drbd_request objects) */
  193. static struct hlist_head *ar_hash_slot(struct drbd_conf *mdev, sector_t sector)
  194. {
  195. return mdev->app_reads_hash
  196. + ((unsigned int)(sector) % APP_R_HSIZE);
  197. }
  198. /* when we receive the answer for a read request,
  199. * verify that we actually know about it */
  200. static inline struct drbd_request *_ar_id_to_req(struct drbd_conf *mdev,
  201. u64 id, sector_t sector)
  202. {
  203. struct hlist_head *slot = ar_hash_slot(mdev, sector);
  204. struct hlist_node *n;
  205. struct drbd_request *req;
  206. hlist_for_each_entry(req, n, slot, colision) {
  207. if ((unsigned long)req == (unsigned long)id) {
  208. D_ASSERT(req->sector == sector);
  209. return req;
  210. }
  211. }
  212. return NULL;
  213. }
  214. static inline struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
  215. struct bio *bio_src)
  216. {
  217. struct bio *bio;
  218. struct drbd_request *req =
  219. mempool_alloc(drbd_request_mempool, GFP_NOIO);
  220. if (likely(req)) {
  221. bio = bio_clone(bio_src, GFP_NOIO); /* XXX cannot fail?? */
  222. req->rq_state = 0;
  223. req->mdev = mdev;
  224. req->master_bio = bio_src;
  225. req->private_bio = bio;
  226. req->epoch = 0;
  227. req->sector = bio->bi_sector;
  228. req->size = bio->bi_size;
  229. req->start_time = jiffies;
  230. INIT_HLIST_NODE(&req->colision);
  231. INIT_LIST_HEAD(&req->tl_requests);
  232. INIT_LIST_HEAD(&req->w.list);
  233. bio->bi_private = req;
  234. bio->bi_end_io = drbd_endio_pri;
  235. bio->bi_next = NULL;
  236. }
  237. return req;
  238. }
  239. static inline void drbd_req_free(struct drbd_request *req)
  240. {
  241. mempool_free(req, drbd_request_mempool);
  242. }
  243. static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
  244. {
  245. return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
  246. }
  247. /* Short lived temporary struct on the stack.
  248. * We could squirrel the error to be returned into
  249. * bio->bi_size, or similar. But that would be too ugly. */
  250. struct bio_and_error {
  251. struct bio *bio;
  252. int error;
  253. };
  254. extern void _req_may_be_done(struct drbd_request *req,
  255. struct bio_and_error *m);
  256. extern void __req_mod(struct drbd_request *req, enum drbd_req_event what,
  257. struct bio_and_error *m);
  258. extern void complete_master_bio(struct drbd_conf *mdev,
  259. struct bio_and_error *m);
  260. /* use this if you don't want to deal with calling complete_master_bio()
  261. * outside the spinlock, e.g. when walking some list on cleanup. */
  262. static inline void _req_mod(struct drbd_request *req, enum drbd_req_event what)
  263. {
  264. struct drbd_conf *mdev = req->mdev;
  265. struct bio_and_error m;
  266. /* __req_mod possibly frees req, do not touch req after that! */
  267. __req_mod(req, what, &m);
  268. if (m.bio)
  269. complete_master_bio(mdev, &m);
  270. }
  271. /* completion of master bio is outside of spinlock.
  272. * If you need it irqsave, do it your self! */
  273. static inline void req_mod(struct drbd_request *req,
  274. enum drbd_req_event what)
  275. {
  276. struct drbd_conf *mdev = req->mdev;
  277. struct bio_and_error m;
  278. spin_lock_irq(&mdev->req_lock);
  279. __req_mod(req, what, &m);
  280. spin_unlock_irq(&mdev->req_lock);
  281. if (m.bio)
  282. complete_master_bio(mdev, &m);
  283. }
  284. #endif