blob: 9f6a04080e9f76aadfdfedf8d0e1cb408dbcba2a [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"
Philipp Reisnerb411b362009-09-25 16:07:19 -070033
34/* The request callbacks will be called in irq context by the IDE drivers,
35 and in Softirqs/Tasklets/BH context by the SCSI drivers,
36 and by the receiver and worker in kernel-thread context.
37 Try to get the locking right :) */
38
39/*
40 * Objects of type struct drbd_request do only exist on a R_PRIMARY node, and are
41 * associated with IO requests originating from the block layer above us.
42 *
43 * There are quite a few things that may happen to a drbd request
44 * during its lifetime.
45 *
46 * It will be created.
47 * It will be marked with the intention to be
48 * submitted to local disk and/or
49 * send via the network.
50 *
51 * It has to be placed on the transfer log and other housekeeping lists,
52 * In case we have a network connection.
53 *
54 * It may be identified as a concurrent (write) request
55 * and be handled accordingly.
56 *
57 * It may me handed over to the local disk subsystem.
58 * It may be completed by the local disk subsystem,
Daniel Mack3ad2f3f2010-02-03 08:01:28 +080059 * either successfully or with io-error.
Philipp Reisnerb411b362009-09-25 16:07:19 -070060 * In case it is a READ request, and it failed locally,
61 * it may be retried remotely.
62 *
63 * It may be queued for sending.
64 * It may be handed over to the network stack,
65 * which may fail.
66 * It may be acknowledged by the "peer" according to the wire_protocol in use.
67 * this may be a negative ack.
68 * It may receive a faked ack when the network connection is lost and the
69 * transfer log is cleaned up.
70 * Sending may be canceled due to network connection loss.
71 * When it finally has outlived its time,
72 * corresponding dirty bits in the resync-bitmap may be cleared or set,
73 * it will be destroyed,
74 * and completion will be signalled to the originator,
75 * with or without "success".
76 */
77
78enum drbd_req_event {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +010079 CREATED,
80 TO_BE_SENT,
81 TO_BE_SUBMITTED,
Philipp Reisnerb411b362009-09-25 16:07:19 -070082
83 /* XXX yes, now I am inconsistent...
Philipp Reisner73a01a12010-10-27 14:33:00 +020084 * these are not "events" but "actions"
Philipp Reisnerb411b362009-09-25 16:07:19 -070085 * oh, well... */
Andreas Gruenbacher8554df12011-01-25 15:37:43 +010086 QUEUE_FOR_NET_WRITE,
87 QUEUE_FOR_NET_READ,
88 QUEUE_FOR_SEND_OOS,
Philipp Reisnerb411b362009-09-25 16:07:19 -070089
Lars Ellenberg7074e4a2013-03-27 14:08:41 +010090 /* An empty flush is queued as P_BARRIER,
91 * which will cause it to complete "successfully",
92 * even if the local disk flush failed.
93 *
94 * Just like "real" requests, empty flushes (blkdev_issue_flush()) will
95 * only see an error if neither local nor remote data is reachable. */
96 QUEUE_AS_DRBD_BARRIER,
97
Andreas Gruenbacher8554df12011-01-25 15:37:43 +010098 SEND_CANCELED,
99 SEND_FAILED,
100 HANDED_OVER_TO_NETWORK,
101 OOS_HANDED_TO_NETWORK,
102 CONNECTION_LOST_WHILE_PENDING,
103 READ_RETRY_REMOTE_CANCELED,
104 RECV_ACKED_BY_PEER,
105 WRITE_ACKED_BY_PEER,
106 WRITE_ACKED_BY_PEER_AND_SIS, /* and set_in_sync */
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +0200107 CONFLICT_RESOLVED,
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100108 POSTPONE_WRITE,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100109 NEG_ACKED,
110 BARRIER_ACKED, /* in protocol A and B */
111 DATA_RECEIVED, /* (remote read) */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700112
Lars Ellenberg2f632ae2014-04-28 18:43:24 +0200113 COMPLETED_OK,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100114 READ_COMPLETED_WITH_ERROR,
115 READ_AHEAD_COMPLETED_WITH_ERROR,
116 WRITE_COMPLETED_WITH_ERROR,
Lars Ellenberg2f632ae2014-04-28 18:43:24 +0200117 DISCARD_COMPLETED_NOTSUPP,
118 DISCARD_COMPLETED_WITH_ERROR,
119
Philipp Reisnercdfda632011-07-05 15:38:59 +0200120 ABORT_DISK_IO,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100121 RESEND,
122 FAIL_FROZEN_DISK_IO,
123 RESTART_FROZEN_DISK_IO,
124 NOTHING,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700125};
126
127/* encoding of request states for now. we don't actually need that many bits.
128 * we don't need to do atomic bit operations either, since most of the time we
129 * need to look at the connection state and/or manipulate some lists at the
130 * same time, so we should hold the request lock anyways.
131 */
132enum drbd_req_state_bits {
Philipp Reisnercdfda632011-07-05 15:38:59 +0200133 /* 3210
134 * 0000: no local possible
135 * 0001: to be submitted
Philipp Reisnerb411b362009-09-25 16:07:19 -0700136 * UNUSED, we could map: 011: submitted, completion still pending
Philipp Reisnercdfda632011-07-05 15:38:59 +0200137 * 0110: completed ok
138 * 0010: completed with error
139 * 1001: Aborted (before completion)
140 * 1x10: Aborted and completed -> free
Philipp Reisnerb411b362009-09-25 16:07:19 -0700141 */
142 __RQ_LOCAL_PENDING,
143 __RQ_LOCAL_COMPLETED,
144 __RQ_LOCAL_OK,
Philipp Reisnercdfda632011-07-05 15:38:59 +0200145 __RQ_LOCAL_ABORTED,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700146
Philipp Reisnercdfda632011-07-05 15:38:59 +0200147 /* 87654
Philipp Reisnerb411b362009-09-25 16:07:19 -0700148 * 00000: no network possible
149 * 00001: to be send
150 * 00011: to be send, on worker queue
151 * 00101: sent, expecting recv_ack (B) or write_ack (C)
152 * 11101: sent,
153 * recv_ack (B) or implicit "ack" (A),
154 * still waiting for the barrier ack.
155 * master_bio may already be completed and invalidated.
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100156 * 11100: write acked (C),
157 * data received (for remote read, any protocol)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700158 * or finally the barrier ack has arrived (B,A)...
159 * request can be freed
160 * 01100: neg-acked (write, protocol C)
161 * or neg-d-acked (read, any protocol)
162 * or killed from the transfer log
163 * during cleanup after connection loss
164 * request can be freed
165 * 01000: canceled or send failed...
166 * request can be freed
167 */
168
169 /* if "SENT" is not set, yet, this can still fail or be canceled.
170 * if "SENT" is set already, we still wait for an Ack packet.
171 * when cleared, the master_bio may be completed.
172 * in (B,A) the request object may still linger on the transaction log
173 * until the corresponding barrier ack comes in */
174 __RQ_NET_PENDING,
175
176 /* If it is QUEUED, and it is a WRITE, it is also registered in the
177 * transfer log. Currently we need this flag to avoid conflicts between
178 * worker canceling the request and tl_clear_barrier killing it from
179 * transfer log. We should restructure the code so this conflict does
180 * no longer occur. */
181 __RQ_NET_QUEUED,
182
183 /* well, actually only "handed over to the network stack".
184 *
185 * TODO can potentially be dropped because of the similar meaning
186 * of RQ_NET_SENT and ~RQ_NET_QUEUED.
187 * however it is not exactly the same. before we drop it
188 * we must ensure that we can tell a request with network part
189 * from a request without, regardless of what happens to it. */
190 __RQ_NET_SENT,
191
192 /* when set, the request may be freed (if RQ_NET_QUEUED is clear).
193 * basically this means the corresponding P_BARRIER_ACK was received */
194 __RQ_NET_DONE,
195
196 /* whether or not we know (C) or pretend (B,A) that the write
197 * was successfully written on the peer.
198 */
199 __RQ_NET_OK,
200
201 /* peer called drbd_set_in_sync() for this write */
202 __RQ_NET_SIS,
203
204 /* keep this last, its for the RQ_NET_MASK */
205 __RQ_NET_MAX,
Philipp Reisner288f4222010-05-27 15:07:43 +0200206
207 /* Set when this is a write, clear for a read */
208 __RQ_WRITE,
Philipp Reisner07782862010-08-31 12:00:50 +0200209
210 /* Should call drbd_al_complete_io() for this request... */
211 __RQ_IN_ACT_LOG,
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100212
213 /* The peer has sent a retry ACK */
214 __RQ_POSTPONED,
Philipp Reisner303d1442011-04-13 16:24:47 -0700215
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100216 /* would have been completed,
217 * but was not, because of drbd_suspended() */
218 __RQ_COMPLETION_SUSP,
219
Philipp Reisner303d1442011-04-13 16:24:47 -0700220 /* We expect a receive ACK (wire proto B) */
221 __RQ_EXP_RECEIVE_ACK,
222
223 /* We expect a write ACK (wite proto C) */
224 __RQ_EXP_WRITE_ACK,
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100225
226 /* waiting for a barrier ack, did an extra kref_get */
227 __RQ_EXP_BARR_ACK,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700228};
229
230#define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)
231#define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)
232#define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)
Philipp Reisnercdfda632011-07-05 15:38:59 +0200233#define RQ_LOCAL_ABORTED (1UL << __RQ_LOCAL_ABORTED)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700234
Philipp Reisnercdfda632011-07-05 15:38:59 +0200235#define RQ_LOCAL_MASK ((RQ_LOCAL_ABORTED << 1)-1)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700236
237#define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)
238#define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)
239#define RQ_NET_SENT (1UL << __RQ_NET_SENT)
240#define RQ_NET_DONE (1UL << __RQ_NET_DONE)
241#define RQ_NET_OK (1UL << __RQ_NET_OK)
242#define RQ_NET_SIS (1UL << __RQ_NET_SIS)
243
244/* 0x1f8 */
245#define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)
246
Philipp Reisner288f4222010-05-27 15:07:43 +0200247#define RQ_WRITE (1UL << __RQ_WRITE)
Philipp Reisner07782862010-08-31 12:00:50 +0200248#define RQ_IN_ACT_LOG (1UL << __RQ_IN_ACT_LOG)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100249#define RQ_POSTPONED (1UL << __RQ_POSTPONED)
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100250#define RQ_COMPLETION_SUSP (1UL << __RQ_COMPLETION_SUSP)
Philipp Reisner303d1442011-04-13 16:24:47 -0700251#define RQ_EXP_RECEIVE_ACK (1UL << __RQ_EXP_RECEIVE_ACK)
252#define RQ_EXP_WRITE_ACK (1UL << __RQ_EXP_WRITE_ACK)
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100253#define RQ_EXP_BARR_ACK (1UL << __RQ_EXP_BARR_ACK)
Philipp Reisner288f4222010-05-27 15:07:43 +0200254
Philipp Reisner11b58e72010-05-12 17:08:26 +0200255/* For waking up the frozen transfer log mod_req() has to return if the request
256 should be counted in the epoch object*/
Andreas Gruenbacherf4976092011-07-17 23:06:12 +0200257#define MR_WRITE 1
258#define MR_READ 2
Philipp Reisner11b58e72010-05-12 17:08:26 +0200259
Philipp Reisner5ba82302010-06-10 13:30:36 +0200260static inline void drbd_req_make_private_bio(struct drbd_request *req, struct bio *bio_src)
261{
262 struct bio *bio;
263 bio = bio_clone(bio_src, GFP_NOIO); /* XXX cannot fail?? */
264
265 req->private_bio = bio;
266
267 bio->bi_private = req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +0100268 bio->bi_end_io = drbd_request_endio;
Philipp Reisner5ba82302010-06-10 13:30:36 +0200269 bio->bi_next = NULL;
270}
271
Philipp Reisnerb411b362009-09-25 16:07:19 -0700272/* Short lived temporary struct on the stack.
273 * We could squirrel the error to be returned into
Kent Overstreet4f024f32013-10-11 15:44:27 -0700274 * bio->bi_iter.bi_size, or similar. But that would be too ugly. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700275struct bio_and_error {
276 struct bio *bio;
277 int error;
278};
279
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200280extern void start_new_tl_epoch(struct drbd_connection *connection);
Lars Ellenberg9a278a72012-07-24 10:12:36 +0200281extern void drbd_req_destroy(struct kref *kref);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700282extern void _req_may_be_done(struct drbd_request *req,
283 struct bio_and_error *m);
Philipp Reisner2a806992010-06-09 14:07:43 +0200284extern int __req_mod(struct drbd_request *req, enum drbd_req_event what,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700285 struct bio_and_error *m);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200286extern void complete_master_bio(struct drbd_device *device,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700287 struct bio_and_error *m);
Philipp Reisner7fde2be2011-03-01 11:08:28 +0100288extern void request_timer_fn(unsigned long data);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200289extern void tl_restart(struct drbd_connection *connection, enum drbd_req_event what);
290extern void _tl_restart(struct drbd_connection *connection, enum drbd_req_event what);
Lars Ellenberg8ce953a2014-02-27 09:46:18 +0100291extern void tl_abort_disk_io(struct drbd_device *device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700292
Lars Ellenberg2312f0b32011-11-24 10:36:25 +0100293/* this is in drbd_main.c */
Lars Ellenberg9d05e7c2012-07-17 10:05:04 +0200294extern void drbd_restart_request(struct drbd_request *req);
Lars Ellenberg2312f0b32011-11-24 10:36:25 +0100295
Philipp Reisnerb411b362009-09-25 16:07:19 -0700296/* use this if you don't want to deal with calling complete_master_bio()
297 * outside the spinlock, e.g. when walking some list on cleanup. */
Philipp Reisner2a806992010-06-09 14:07:43 +0200298static inline int _req_mod(struct drbd_request *req, enum drbd_req_event what)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700299{
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200300 struct drbd_device *device = req->device;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700301 struct bio_and_error m;
Philipp Reisner2a806992010-06-09 14:07:43 +0200302 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700303
304 /* __req_mod possibly frees req, do not touch req after that! */
Philipp Reisner2a806992010-06-09 14:07:43 +0200305 rv = __req_mod(req, what, &m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700306 if (m.bio)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200307 complete_master_bio(device, &m);
Philipp Reisner2a806992010-06-09 14:07:43 +0200308
309 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700310}
311
Philipp Reisner759fbdf2010-10-26 16:02:27 +0200312/* completion of master bio is outside of our spinlock.
313 * We still may or may not be inside some irqs disabled section
314 * of the lower level driver completion callback, so we need to
315 * spin_lock_irqsave here. */
Philipp Reisner2a806992010-06-09 14:07:43 +0200316static inline int req_mod(struct drbd_request *req,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700317 enum drbd_req_event what)
318{
Philipp Reisner759fbdf2010-10-26 16:02:27 +0200319 unsigned long flags;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200320 struct drbd_device *device = req->device;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700321 struct bio_and_error m;
Philipp Reisner2a806992010-06-09 14:07:43 +0200322 int rv;
323
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200324 spin_lock_irqsave(&device->resource->req_lock, flags);
Philipp Reisner2a806992010-06-09 14:07:43 +0200325 rv = __req_mod(req, what, &m);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200326 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700327
328 if (m.bio)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200329 complete_master_bio(device, &m);
Philipp Reisner2a806992010-06-09 14:07:43 +0200330
331 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700332}
Philipp Reisner6a35c452011-01-17 20:27:30 +0100333
Philipp Reisnerda9fbc22011-03-29 10:52:01 +0200334static inline bool drbd_should_do_remote(union drbd_dev_state s)
Philipp Reisner6a35c452011-01-17 20:27:30 +0100335{
336 return s.pdsk == D_UP_TO_DATE ||
337 (s.pdsk >= D_INCONSISTENT &&
338 s.conn >= C_WF_BITMAP_T &&
339 s.conn < C_AHEAD);
340 /* Before proto 96 that was >= CONNECTED instead of >= C_WF_BITMAP_T.
341 That is equivalent since before 96 IO was frozen in the C_WF_BITMAP*
342 states. */
343}
Philipp Reisnerda9fbc22011-03-29 10:52:01 +0200344static inline bool drbd_should_send_out_of_sync(union drbd_dev_state s)
Philipp Reisner6a35c452011-01-17 20:27:30 +0100345{
346 return s.conn == C_AHEAD || s.conn == C_WF_BITMAP_S;
347 /* pdsk = D_INCONSISTENT as a consequence. Protocol 96 check not necessary
348 since we enter state C_AHEAD only if proto >= 96 */
349}
350
Philipp Reisnerb411b362009-09-25 16:07:19 -0700351#endif