drbd_req.h 12 KB

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