4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
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>.
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)
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.
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.
28 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/drbd.h>
33 #include "drbd_wrappers.h"
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 :) */
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.
44 * There are quite a few things that may happen to a drbd request
45 * during its lifetime.
48 * It will be marked with the intention to be
49 * submitted to local disk and/or
50 * send via the network.
52 * It has to be placed on the transfer log and other housekeeping lists,
53 * In case we have a network connection.
55 * It may be identified as a concurrent (write) request
56 * and be handled accordingly.
58 * It may me handed over to the local disk subsystem.
59 * It may be completed by the local disk subsystem,
60 * either successfully or with io-error.
61 * In case it is a READ request, and it failed locally,
62 * it may be retried remotely.
64 * It may be queued for sending.
65 * It may be handed over to the network stack,
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".
84 /* XXX yes, now I am inconsistent...
85 * these are not "events" but "actions"
93 HANDED_OVER_TO_NETWORK,
94 OOS_HANDED_TO_NETWORK,
95 CONNECTION_LOST_WHILE_PENDING,
96 READ_RETRY_REMOTE_CANCELED,
99 WRITE_ACKED_BY_PEER_AND_SIS, /* and set_in_sync */
100 CONFLICT_DISCARDED_BY_PEER,
102 BARRIER_ACKED, /* in protocol A and B */
103 DATA_RECEIVED, /* (remote read) */
105 READ_COMPLETED_WITH_ERROR,
106 READ_AHEAD_COMPLETED_WITH_ERROR,
107 WRITE_COMPLETED_WITH_ERROR,
111 RESTART_FROZEN_DISK_IO,
115 /* encoding of request states for now. we don't actually need that many bits.
116 * we don't need to do atomic bit operations either, since most of the time we
117 * need to look at the connection state and/or manipulate some lists at the
118 * same time, so we should hold the request lock anyways.
120 enum drbd_req_state_bits {
122 * 000: no local possible
123 * 001: to be submitted
124 * UNUSED, we could map: 011: submitted, completion still pending
126 * 010: completed with error
129 __RQ_LOCAL_COMPLETED,
133 * 00000: no network possible
135 * 00011: to be send, on worker queue
136 * 00101: sent, expecting recv_ack (B) or write_ack (C)
138 * recv_ack (B) or implicit "ack" (A),
139 * still waiting for the barrier ack.
140 * master_bio may already be completed and invalidated.
141 * 11100: write acked (C),
142 * data received (for remote read, any protocol)
143 * or finally the barrier ack has arrived (B,A)...
144 * request can be freed
145 * 01100: neg-acked (write, protocol C)
146 * or neg-d-acked (read, any protocol)
147 * or killed from the transfer log
148 * during cleanup after connection loss
149 * request can be freed
150 * 01000: canceled or send failed...
151 * request can be freed
154 /* if "SENT" is not set, yet, this can still fail or be canceled.
155 * if "SENT" is set already, we still wait for an Ack packet.
156 * when cleared, the master_bio may be completed.
157 * in (B,A) the request object may still linger on the transaction log
158 * until the corresponding barrier ack comes in */
161 /* If it is QUEUED, and it is a WRITE, it is also registered in the
162 * transfer log. Currently we need this flag to avoid conflicts between
163 * worker canceling the request and tl_clear_barrier killing it from
164 * transfer log. We should restructure the code so this conflict does
165 * no longer occur. */
168 /* well, actually only "handed over to the network stack".
170 * TODO can potentially be dropped because of the similar meaning
171 * of RQ_NET_SENT and ~RQ_NET_QUEUED.
172 * however it is not exactly the same. before we drop it
173 * we must ensure that we can tell a request with network part
174 * from a request without, regardless of what happens to it. */
177 /* when set, the request may be freed (if RQ_NET_QUEUED is clear).
178 * basically this means the corresponding P_BARRIER_ACK was received */
181 /* whether or not we know (C) or pretend (B,A) that the write
182 * was successfully written on the peer.
186 /* peer called drbd_set_in_sync() for this write */
189 /* keep this last, its for the RQ_NET_MASK */
192 /* Set when this is a write, clear for a read */
195 /* Should call drbd_al_complete_io() for this request... */
199 #define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)
200 #define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)
201 #define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)
203 #define RQ_LOCAL_MASK ((RQ_LOCAL_OK << 1)-1) /* 0x07 */
205 #define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)
206 #define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)
207 #define RQ_NET_SENT (1UL << __RQ_NET_SENT)
208 #define RQ_NET_DONE (1UL << __RQ_NET_DONE)
209 #define RQ_NET_OK (1UL << __RQ_NET_OK)
210 #define RQ_NET_SIS (1UL << __RQ_NET_SIS)
213 #define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)
215 #define RQ_WRITE (1UL << __RQ_WRITE)
216 #define RQ_IN_ACT_LOG (1UL << __RQ_IN_ACT_LOG)
218 /* For waking up the frozen transfer log mod_req() has to return if the request
219 should be counted in the epoch object*/
220 #define MR_WRITE_SHIFT 0
221 #define MR_WRITE (1 << MR_WRITE_SHIFT)
222 #define MR_READ_SHIFT 1
223 #define MR_READ (1 << MR_READ_SHIFT)
225 static inline void drbd_req_make_private_bio(struct drbd_request *req, struct bio *bio_src)
228 bio = bio_clone(bio_src, GFP_NOIO); /* XXX cannot fail?? */
230 req->private_bio = bio;
232 bio->bi_private = req;
233 bio->bi_end_io = drbd_endio_pri;
237 /* Short lived temporary struct on the stack.
238 * We could squirrel the error to be returned into
239 * bio->bi_size, or similar. But that would be too ugly. */
240 struct bio_and_error {
245 extern void _req_may_be_done(struct drbd_request *req,
246 struct bio_and_error *m);
247 extern int __req_mod(struct drbd_request *req, enum drbd_req_event what,
248 struct bio_and_error *m);
249 extern void complete_master_bio(struct drbd_conf *mdev,
250 struct bio_and_error *m);
251 extern void request_timer_fn(unsigned long data);
252 extern void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what);
254 /* use this if you don't want to deal with calling complete_master_bio()
255 * outside the spinlock, e.g. when walking some list on cleanup. */
256 static inline int _req_mod(struct drbd_request *req, enum drbd_req_event what)
258 struct drbd_conf *mdev = req->w.mdev;
259 struct bio_and_error m;
262 /* __req_mod possibly frees req, do not touch req after that! */
263 rv = __req_mod(req, what, &m);
265 complete_master_bio(mdev, &m);
270 /* completion of master bio is outside of our spinlock.
271 * We still may or may not be inside some irqs disabled section
272 * of the lower level driver completion callback, so we need to
273 * spin_lock_irqsave here. */
274 static inline int req_mod(struct drbd_request *req,
275 enum drbd_req_event what)
278 struct drbd_conf *mdev = req->w.mdev;
279 struct bio_and_error m;
282 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
283 rv = __req_mod(req, what, &m);
284 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
287 complete_master_bio(mdev, &m);
292 static inline bool drbd_should_do_remote(union drbd_state s)
294 return s.pdsk == D_UP_TO_DATE ||
295 (s.pdsk >= D_INCONSISTENT &&
296 s.conn >= C_WF_BITMAP_T &&
298 /* Before proto 96 that was >= CONNECTED instead of >= C_WF_BITMAP_T.
299 That is equivalent since before 96 IO was frozen in the C_WF_BITMAP*
302 static inline bool drbd_should_send_oos(union drbd_state s)
304 return s.conn == C_AHEAD || s.conn == C_WF_BITMAP_S;
305 /* pdsk = D_INCONSISTENT as a consequence. Protocol 96 check not necessary
306 since we enter state C_AHEAD only if proto >= 96 */