drbd_req.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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_send,
  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_discarded_by_peer,
  88. neg_acked,
  89. barrier_acked, /* in protocol A and B */
  90. data_received, /* (remote read) */
  91. read_completed_with_error,
  92. read_ahead_completed_with_error,
  93. write_completed_with_error,
  94. abort_disk_io,
  95. completed_ok,
  96. resend,
  97. fail_frozen_disk_io,
  98. restart_frozen_disk_io,
  99. nothing, /* for tracing only */
  100. };
  101. /* encoding of request states for now. we don't actually need that many bits.
  102. * we don't need to do atomic bit operations either, since most of the time we
  103. * need to look at the connection state and/or manipulate some lists at the
  104. * same time, so we should hold the request lock anyways.
  105. */
  106. enum drbd_req_state_bits {
  107. /* 3210
  108. * 0000: no local possible
  109. * 0001: to be submitted
  110. * UNUSED, we could map: 011: submitted, completion still pending
  111. * 0110: completed ok
  112. * 0010: completed with error
  113. * 1001: Aborted (before completion)
  114. * 1x10: Aborted and completed -> free
  115. */
  116. __RQ_LOCAL_PENDING,
  117. __RQ_LOCAL_COMPLETED,
  118. __RQ_LOCAL_OK,
  119. __RQ_LOCAL_ABORTED,
  120. /* 87654
  121. * 00000: no network possible
  122. * 00001: to be send
  123. * 00011: to be send, on worker queue
  124. * 00101: sent, expecting recv_ack (B) or write_ack (C)
  125. * 11101: sent,
  126. * recv_ack (B) or implicit "ack" (A),
  127. * still waiting for the barrier ack.
  128. * master_bio may already be completed and invalidated.
  129. * 11100: write_acked (C),
  130. * data_received (for remote read, any protocol)
  131. * or finally the barrier ack has arrived (B,A)...
  132. * request can be freed
  133. * 01100: neg-acked (write, protocol C)
  134. * or neg-d-acked (read, any protocol)
  135. * or killed from the transfer log
  136. * during cleanup after connection loss
  137. * request can be freed
  138. * 01000: canceled or send failed...
  139. * request can be freed
  140. */
  141. /* if "SENT" is not set, yet, this can still fail or be canceled.
  142. * if "SENT" is set already, we still wait for an Ack packet.
  143. * when cleared, the master_bio may be completed.
  144. * in (B,A) the request object may still linger on the transaction log
  145. * until the corresponding barrier ack comes in */
  146. __RQ_NET_PENDING,
  147. /* If it is QUEUED, and it is a WRITE, it is also registered in the
  148. * transfer log. Currently we need this flag to avoid conflicts between
  149. * worker canceling the request and tl_clear_barrier killing it from
  150. * transfer log. We should restructure the code so this conflict does
  151. * no longer occur. */
  152. __RQ_NET_QUEUED,
  153. /* well, actually only "handed over to the network stack".
  154. *
  155. * TODO can potentially be dropped because of the similar meaning
  156. * of RQ_NET_SENT and ~RQ_NET_QUEUED.
  157. * however it is not exactly the same. before we drop it
  158. * we must ensure that we can tell a request with network part
  159. * from a request without, regardless of what happens to it. */
  160. __RQ_NET_SENT,
  161. /* when set, the request may be freed (if RQ_NET_QUEUED is clear).
  162. * basically this means the corresponding P_BARRIER_ACK was received */
  163. __RQ_NET_DONE,
  164. /* whether or not we know (C) or pretend (B,A) that the write
  165. * was successfully written on the peer.
  166. */
  167. __RQ_NET_OK,
  168. /* peer called drbd_set_in_sync() for this write */
  169. __RQ_NET_SIS,
  170. /* keep this last, its for the RQ_NET_MASK */
  171. __RQ_NET_MAX,
  172. /* Set when this is a write, clear for a read */
  173. __RQ_WRITE,
  174. /* Should call drbd_al_complete_io() for this request... */
  175. __RQ_IN_ACT_LOG,
  176. };
  177. #define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)
  178. #define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)
  179. #define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)
  180. #define RQ_LOCAL_ABORTED (1UL << __RQ_LOCAL_ABORTED)
  181. #define RQ_LOCAL_MASK ((RQ_LOCAL_ABORTED << 1)-1)
  182. #define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)
  183. #define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)
  184. #define RQ_NET_SENT (1UL << __RQ_NET_SENT)
  185. #define RQ_NET_DONE (1UL << __RQ_NET_DONE)
  186. #define RQ_NET_OK (1UL << __RQ_NET_OK)
  187. #define RQ_NET_SIS (1UL << __RQ_NET_SIS)
  188. /* 0x1f8 */
  189. #define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)
  190. #define RQ_WRITE (1UL << __RQ_WRITE)
  191. #define RQ_IN_ACT_LOG (1UL << __RQ_IN_ACT_LOG)
  192. /* For waking up the frozen transfer log mod_req() has to return if the request
  193. should be counted in the epoch object*/
  194. #define MR_WRITE_SHIFT 0
  195. #define MR_WRITE (1 << MR_WRITE_SHIFT)
  196. #define MR_READ_SHIFT 1
  197. #define MR_READ (1 << MR_READ_SHIFT)
  198. /* epoch entries */
  199. static inline
  200. struct hlist_head *ee_hash_slot(struct drbd_conf *mdev, sector_t sector)
  201. {
  202. BUG_ON(mdev->ee_hash_s == 0);
  203. return mdev->ee_hash +
  204. ((unsigned int)(sector>>HT_SHIFT) % mdev->ee_hash_s);
  205. }
  206. /* transfer log (drbd_request objects) */
  207. static inline
  208. struct hlist_head *tl_hash_slot(struct drbd_conf *mdev, sector_t sector)
  209. {
  210. BUG_ON(mdev->tl_hash_s == 0);
  211. return mdev->tl_hash +
  212. ((unsigned int)(sector>>HT_SHIFT) % mdev->tl_hash_s);
  213. }
  214. /* application reads (drbd_request objects) */
  215. static struct hlist_head *ar_hash_slot(struct drbd_conf *mdev, sector_t sector)
  216. {
  217. return mdev->app_reads_hash
  218. + ((unsigned int)(sector) % APP_R_HSIZE);
  219. }
  220. /* when we receive the answer for a read request,
  221. * verify that we actually know about it */
  222. static inline struct drbd_request *_ar_id_to_req(struct drbd_conf *mdev,
  223. u64 id, sector_t sector)
  224. {
  225. struct hlist_head *slot = ar_hash_slot(mdev, sector);
  226. struct hlist_node *n;
  227. struct drbd_request *req;
  228. hlist_for_each_entry(req, n, slot, collision) {
  229. if ((unsigned long)req == (unsigned long)id) {
  230. D_ASSERT(req->sector == sector);
  231. return req;
  232. }
  233. }
  234. return NULL;
  235. }
  236. static inline void drbd_req_make_private_bio(struct drbd_request *req, struct bio *bio_src)
  237. {
  238. struct bio *bio;
  239. bio = bio_clone(bio_src, GFP_NOIO); /* XXX cannot fail?? */
  240. req->private_bio = bio;
  241. bio->bi_private = req;
  242. bio->bi_end_io = drbd_endio_pri;
  243. bio->bi_next = NULL;
  244. }
  245. static inline struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
  246. struct bio *bio_src)
  247. {
  248. struct drbd_request *req =
  249. mempool_alloc(drbd_request_mempool, GFP_NOIO);
  250. if (likely(req)) {
  251. drbd_req_make_private_bio(req, bio_src);
  252. req->rq_state = bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0;
  253. req->mdev = mdev;
  254. req->master_bio = bio_src;
  255. req->epoch = 0;
  256. req->sector = bio_src->bi_sector;
  257. req->size = bio_src->bi_size;
  258. INIT_HLIST_NODE(&req->collision);
  259. INIT_LIST_HEAD(&req->tl_requests);
  260. INIT_LIST_HEAD(&req->w.list);
  261. }
  262. return req;
  263. }
  264. static inline void drbd_req_free(struct drbd_request *req)
  265. {
  266. mempool_free(req, drbd_request_mempool);
  267. }
  268. static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
  269. {
  270. return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
  271. }
  272. /* Short lived temporary struct on the stack.
  273. * We could squirrel the error to be returned into
  274. * bio->bi_size, or similar. But that would be too ugly. */
  275. struct bio_and_error {
  276. struct bio *bio;
  277. int error;
  278. };
  279. extern void _req_may_be_done(struct drbd_request *req,
  280. struct bio_and_error *m);
  281. extern int __req_mod(struct drbd_request *req, enum drbd_req_event what,
  282. struct bio_and_error *m);
  283. extern void complete_master_bio(struct drbd_conf *mdev,
  284. struct bio_and_error *m);
  285. extern void request_timer_fn(unsigned long data);
  286. extern void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what);
  287. /* use this if you don't want to deal with calling complete_master_bio()
  288. * outside the spinlock, e.g. when walking some list on cleanup. */
  289. static inline int _req_mod(struct drbd_request *req, enum drbd_req_event what)
  290. {
  291. struct drbd_conf *mdev = req->mdev;
  292. struct bio_and_error m;
  293. int rv;
  294. /* __req_mod possibly frees req, do not touch req after that! */
  295. rv = __req_mod(req, what, &m);
  296. if (m.bio)
  297. complete_master_bio(mdev, &m);
  298. return rv;
  299. }
  300. /* completion of master bio is outside of our spinlock.
  301. * We still may or may not be inside some irqs disabled section
  302. * of the lower level driver completion callback, so we need to
  303. * spin_lock_irqsave here. */
  304. static inline int req_mod(struct drbd_request *req,
  305. enum drbd_req_event what)
  306. {
  307. unsigned long flags;
  308. struct drbd_conf *mdev = req->mdev;
  309. struct bio_and_error m;
  310. int rv;
  311. spin_lock_irqsave(&mdev->req_lock, flags);
  312. rv = __req_mod(req, what, &m);
  313. spin_unlock_irqrestore(&mdev->req_lock, flags);
  314. if (m.bio)
  315. complete_master_bio(mdev, &m);
  316. return rv;
  317. }
  318. static inline bool drbd_should_do_remote(union drbd_state s)
  319. {
  320. return s.pdsk == D_UP_TO_DATE ||
  321. (s.pdsk >= D_INCONSISTENT &&
  322. s.conn >= C_WF_BITMAP_T &&
  323. s.conn < C_AHEAD);
  324. /* Before proto 96 that was >= CONNECTED instead of >= C_WF_BITMAP_T.
  325. That is equivalent since before 96 IO was frozen in the C_WF_BITMAP*
  326. states. */
  327. }
  328. static inline bool drbd_should_send_oos(union drbd_state s)
  329. {
  330. return s.conn == C_AHEAD || s.conn == C_WF_BITMAP_S;
  331. /* pdsk = D_INCONSISTENT as a consequence. Protocol 96 check not necessary
  332. since we enter state C_AHEAD only if proto >= 96 */
  333. }
  334. #endif