blob: 68f54050b7ca4459411f85f5820eec66e49dce29 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_req.h
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2006-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 2006-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
8 Copyright (C) 2006-2008, Philipp Reisner <philipp.reisner@linbit.com>.
9
10 DRBD is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 DRBD is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#ifndef _DRBD_REQ_H
26#define _DRBD_REQ_H
27
Philipp Reisnerb411b362009-09-25 16:07:19 -070028#include <linux/module.h>
29
30#include <linux/slab.h>
31#include <linux/drbd.h>
32#include "drbd_int.h"
33#include "drbd_wrappers.h"
34
35/* The request callbacks will be called in irq context by the IDE drivers,
36 and in Softirqs/Tasklets/BH context by the SCSI drivers,
37 and by the receiver and worker in kernel-thread context.
38 Try to get the locking right :) */
39
40/*
41 * Objects of type struct drbd_request do only exist on a R_PRIMARY node, and are
42 * associated with IO requests originating from the block layer above us.
43 *
44 * There are quite a few things that may happen to a drbd request
45 * during its lifetime.
46 *
47 * It will be created.
48 * It will be marked with the intention to be
49 * submitted to local disk and/or
50 * send via the network.
51 *
52 * It has to be placed on the transfer log and other housekeeping lists,
53 * In case we have a network connection.
54 *
55 * It may be identified as a concurrent (write) request
56 * and be handled accordingly.
57 *
58 * It may me handed over to the local disk subsystem.
59 * It may be completed by the local disk subsystem,
Daniel Mack3ad2f3f2010-02-03 08:01:28 +080060 * either successfully or with io-error.
Philipp Reisnerb411b362009-09-25 16:07:19 -070061 * In case it is a READ request, and it failed locally,
62 * it may be retried remotely.
63 *
64 * It may be queued for sending.
65 * It may be handed over to the network stack,
66 * which may fail.
67 * It may be acknowledged by the "peer" according to the wire_protocol in use.
68 * this may be a negative ack.
69 * It may receive a faked ack when the network connection is lost and the
70 * transfer log is cleaned up.
71 * Sending may be canceled due to network connection loss.
72 * When it finally has outlived its time,
73 * corresponding dirty bits in the resync-bitmap may be cleared or set,
74 * it will be destroyed,
75 * and completion will be signalled to the originator,
76 * with or without "success".
77 */
78
79enum drbd_req_event {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +010080 CREATED,
81 TO_BE_SENT,
82 TO_BE_SUBMITTED,
Philipp Reisnerb411b362009-09-25 16:07:19 -070083
84 /* XXX yes, now I am inconsistent...
Philipp Reisner73a01a12010-10-27 14:33:00 +020085 * these are not "events" but "actions"
Philipp Reisnerb411b362009-09-25 16:07:19 -070086 * oh, well... */
Andreas Gruenbacher8554df12011-01-25 15:37:43 +010087 QUEUE_FOR_NET_WRITE,
88 QUEUE_FOR_NET_READ,
89 QUEUE_FOR_SEND_OOS,
Philipp Reisnerb411b362009-09-25 16:07:19 -070090
Andreas Gruenbacher8554df12011-01-25 15:37:43 +010091 SEND_CANCELED,
92 SEND_FAILED,
93 HANDED_OVER_TO_NETWORK,
94 OOS_HANDED_TO_NETWORK,
95 CONNECTION_LOST_WHILE_PENDING,
96 READ_RETRY_REMOTE_CANCELED,
97 RECV_ACKED_BY_PEER,
98 WRITE_ACKED_BY_PEER,
99 WRITE_ACKED_BY_PEER_AND_SIS, /* and set_in_sync */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100100 DISCARD_WRITE,
101 POSTPONE_WRITE,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100102 NEG_ACKED,
103 BARRIER_ACKED, /* in protocol A and B */
104 DATA_RECEIVED, /* (remote read) */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700105
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100106 READ_COMPLETED_WITH_ERROR,
107 READ_AHEAD_COMPLETED_WITH_ERROR,
108 WRITE_COMPLETED_WITH_ERROR,
Philipp Reisnercdfda632011-07-05 15:38:59 +0200109 ABORT_DISK_IO,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100110 COMPLETED_OK,
111 RESEND,
112 FAIL_FROZEN_DISK_IO,
113 RESTART_FROZEN_DISK_IO,
114 NOTHING,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700115};
116
117/* encoding of request states for now. we don't actually need that many bits.
118 * we don't need to do atomic bit operations either, since most of the time we
119 * need to look at the connection state and/or manipulate some lists at the
120 * same time, so we should hold the request lock anyways.
121 */
122enum drbd_req_state_bits {
Philipp Reisnercdfda632011-07-05 15:38:59 +0200123 /* 3210
124 * 0000: no local possible
125 * 0001: to be submitted
Philipp Reisnerb411b362009-09-25 16:07:19 -0700126 * UNUSED, we could map: 011: submitted, completion still pending
Philipp Reisnercdfda632011-07-05 15:38:59 +0200127 * 0110: completed ok
128 * 0010: completed with error
129 * 1001: Aborted (before completion)
130 * 1x10: Aborted and completed -> free
Philipp Reisnerb411b362009-09-25 16:07:19 -0700131 */
132 __RQ_LOCAL_PENDING,
133 __RQ_LOCAL_COMPLETED,
134 __RQ_LOCAL_OK,
Philipp Reisnercdfda632011-07-05 15:38:59 +0200135 __RQ_LOCAL_ABORTED,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700136
Philipp Reisnercdfda632011-07-05 15:38:59 +0200137 /* 87654
Philipp Reisnerb411b362009-09-25 16:07:19 -0700138 * 00000: no network possible
139 * 00001: to be send
140 * 00011: to be send, on worker queue
141 * 00101: sent, expecting recv_ack (B) or write_ack (C)
142 * 11101: sent,
143 * recv_ack (B) or implicit "ack" (A),
144 * still waiting for the barrier ack.
145 * master_bio may already be completed and invalidated.
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100146 * 11100: write acked (C),
147 * data received (for remote read, any protocol)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700148 * or finally the barrier ack has arrived (B,A)...
149 * request can be freed
150 * 01100: neg-acked (write, protocol C)
151 * or neg-d-acked (read, any protocol)
152 * or killed from the transfer log
153 * during cleanup after connection loss
154 * request can be freed
155 * 01000: canceled or send failed...
156 * request can be freed
157 */
158
159 /* if "SENT" is not set, yet, this can still fail or be canceled.
160 * if "SENT" is set already, we still wait for an Ack packet.
161 * when cleared, the master_bio may be completed.
162 * in (B,A) the request object may still linger on the transaction log
163 * until the corresponding barrier ack comes in */
164 __RQ_NET_PENDING,
165
166 /* If it is QUEUED, and it is a WRITE, it is also registered in the
167 * transfer log. Currently we need this flag to avoid conflicts between
168 * worker canceling the request and tl_clear_barrier killing it from
169 * transfer log. We should restructure the code so this conflict does
170 * no longer occur. */
171 __RQ_NET_QUEUED,
172
173 /* well, actually only "handed over to the network stack".
174 *
175 * TODO can potentially be dropped because of the similar meaning
176 * of RQ_NET_SENT and ~RQ_NET_QUEUED.
177 * however it is not exactly the same. before we drop it
178 * we must ensure that we can tell a request with network part
179 * from a request without, regardless of what happens to it. */
180 __RQ_NET_SENT,
181
182 /* when set, the request may be freed (if RQ_NET_QUEUED is clear).
183 * basically this means the corresponding P_BARRIER_ACK was received */
184 __RQ_NET_DONE,
185
186 /* whether or not we know (C) or pretend (B,A) that the write
187 * was successfully written on the peer.
188 */
189 __RQ_NET_OK,
190
191 /* peer called drbd_set_in_sync() for this write */
192 __RQ_NET_SIS,
193
194 /* keep this last, its for the RQ_NET_MASK */
195 __RQ_NET_MAX,
Philipp Reisner288f4222010-05-27 15:07:43 +0200196
197 /* Set when this is a write, clear for a read */
198 __RQ_WRITE,
Philipp Reisner07782862010-08-31 12:00:50 +0200199
200 /* Should call drbd_al_complete_io() for this request... */
201 __RQ_IN_ACT_LOG,
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100202
203 /* The peer has sent a retry ACK */
204 __RQ_POSTPONED,
Philipp Reisner303d1442011-04-13 16:24:47 -0700205
206 /* We expect a receive ACK (wire proto B) */
207 __RQ_EXP_RECEIVE_ACK,
208
209 /* We expect a write ACK (wite proto C) */
210 __RQ_EXP_WRITE_ACK,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700211};
212
213#define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)
214#define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)
215#define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)
Philipp Reisnercdfda632011-07-05 15:38:59 +0200216#define RQ_LOCAL_ABORTED (1UL << __RQ_LOCAL_ABORTED)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700217
Philipp Reisnercdfda632011-07-05 15:38:59 +0200218#define RQ_LOCAL_MASK ((RQ_LOCAL_ABORTED << 1)-1)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700219
220#define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)
221#define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)
222#define RQ_NET_SENT (1UL << __RQ_NET_SENT)
223#define RQ_NET_DONE (1UL << __RQ_NET_DONE)
224#define RQ_NET_OK (1UL << __RQ_NET_OK)
225#define RQ_NET_SIS (1UL << __RQ_NET_SIS)
226
227/* 0x1f8 */
228#define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)
229
Philipp Reisner288f4222010-05-27 15:07:43 +0200230#define RQ_WRITE (1UL << __RQ_WRITE)
Philipp Reisner07782862010-08-31 12:00:50 +0200231#define RQ_IN_ACT_LOG (1UL << __RQ_IN_ACT_LOG)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100232#define RQ_POSTPONED (1UL << __RQ_POSTPONED)
Philipp Reisner303d1442011-04-13 16:24:47 -0700233#define RQ_EXP_RECEIVE_ACK (1UL << __RQ_EXP_RECEIVE_ACK)
234#define RQ_EXP_WRITE_ACK (1UL << __RQ_EXP_WRITE_ACK)
Philipp Reisner288f4222010-05-27 15:07:43 +0200235
Philipp Reisner11b58e72010-05-12 17:08:26 +0200236/* For waking up the frozen transfer log mod_req() has to return if the request
237 should be counted in the epoch object*/
Andreas Gruenbacherf4976092011-07-17 23:06:12 +0200238#define MR_WRITE 1
239#define MR_READ 2
Philipp Reisner11b58e72010-05-12 17:08:26 +0200240
Philipp Reisner5ba82302010-06-10 13:30:36 +0200241static inline void drbd_req_make_private_bio(struct drbd_request *req, struct bio *bio_src)
242{
243 struct bio *bio;
244 bio = bio_clone(bio_src, GFP_NOIO); /* XXX cannot fail?? */
245
246 req->private_bio = bio;
247
248 bio->bi_private = req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +0100249 bio->bi_end_io = drbd_request_endio;
Philipp Reisner5ba82302010-06-10 13:30:36 +0200250 bio->bi_next = NULL;
251}
252
Philipp Reisnerb411b362009-09-25 16:07:19 -0700253/* Short lived temporary struct on the stack.
254 * We could squirrel the error to be returned into
255 * bio->bi_size, or similar. But that would be too ugly. */
256struct bio_and_error {
257 struct bio *bio;
258 int error;
259};
260
261extern void _req_may_be_done(struct drbd_request *req,
262 struct bio_and_error *m);
Philipp Reisner2a806992010-06-09 14:07:43 +0200263extern int __req_mod(struct drbd_request *req, enum drbd_req_event what,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700264 struct bio_and_error *m);
265extern void complete_master_bio(struct drbd_conf *mdev,
266 struct bio_and_error *m);
Philipp Reisner7fde2be2011-03-01 11:08:28 +0100267extern void request_timer_fn(unsigned long data);
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100268extern void tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what);
269extern void _tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700270
271/* use this if you don't want to deal with calling complete_master_bio()
272 * outside the spinlock, e.g. when walking some list on cleanup. */
Philipp Reisner2a806992010-06-09 14:07:43 +0200273static inline int _req_mod(struct drbd_request *req, enum drbd_req_event what)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700274{
Philipp Reisnera21e9292011-02-08 15:08:49 +0100275 struct drbd_conf *mdev = req->w.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700276 struct bio_and_error m;
Philipp Reisner2a806992010-06-09 14:07:43 +0200277 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700278
279 /* __req_mod possibly frees req, do not touch req after that! */
Philipp Reisner2a806992010-06-09 14:07:43 +0200280 rv = __req_mod(req, what, &m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700281 if (m.bio)
282 complete_master_bio(mdev, &m);
Philipp Reisner2a806992010-06-09 14:07:43 +0200283
284 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700285}
286
Philipp Reisner759fbdf2010-10-26 16:02:27 +0200287/* completion of master bio is outside of our spinlock.
288 * We still may or may not be inside some irqs disabled section
289 * of the lower level driver completion callback, so we need to
290 * spin_lock_irqsave here. */
Philipp Reisner2a806992010-06-09 14:07:43 +0200291static inline int req_mod(struct drbd_request *req,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700292 enum drbd_req_event what)
293{
Philipp Reisner759fbdf2010-10-26 16:02:27 +0200294 unsigned long flags;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100295 struct drbd_conf *mdev = req->w.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700296 struct bio_and_error m;
Philipp Reisner2a806992010-06-09 14:07:43 +0200297 int rv;
298
Philipp Reisner87eeee42011-01-19 14:16:30 +0100299 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisner2a806992010-06-09 14:07:43 +0200300 rv = __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100301 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700302
303 if (m.bio)
304 complete_master_bio(mdev, &m);
Philipp Reisner2a806992010-06-09 14:07:43 +0200305
306 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307}
Philipp Reisner6a35c452011-01-17 20:27:30 +0100308
Philipp Reisnerda9fbc22011-03-29 10:52:01 +0200309static inline bool drbd_should_do_remote(union drbd_dev_state s)
Philipp Reisner6a35c452011-01-17 20:27:30 +0100310{
311 return s.pdsk == D_UP_TO_DATE ||
312 (s.pdsk >= D_INCONSISTENT &&
313 s.conn >= C_WF_BITMAP_T &&
314 s.conn < C_AHEAD);
315 /* Before proto 96 that was >= CONNECTED instead of >= C_WF_BITMAP_T.
316 That is equivalent since before 96 IO was frozen in the C_WF_BITMAP*
317 states. */
318}
Philipp Reisnerda9fbc22011-03-29 10:52:01 +0200319static inline bool drbd_should_send_out_of_sync(union drbd_dev_state s)
Philipp Reisner6a35c452011-01-17 20:27:30 +0100320{
321 return s.conn == C_AHEAD || s.conn == C_WF_BITMAP_S;
322 /* pdsk = D_INCONSISTENT as a consequence. Protocol 96 check not necessary
323 since we enter state C_AHEAD only if proto >= 96 */
324}
325
Philipp Reisnerb411b362009-09-25 16:07:19 -0700326#endif