blob: 47b931fe0366eca9939d4b8fbf3ff59c079cbab0 [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 {
80 created,
81 to_be_send,
82 to_be_submitted,
83
84 /* XXX yes, now I am inconsistent...
85 * these two are not "events" but "actions"
86 * oh, well... */
87 queue_for_net_write,
88 queue_for_net_read,
89
90 send_canceled,
91 send_failed,
92 handed_over_to_network,
93 connection_lost_while_pending,
Lars Ellenbergd255e5f2010-05-27 09:45:45 +020094 read_retry_remote_canceled,
Philipp Reisnerb411b362009-09-25 16:07:19 -070095 recv_acked_by_peer,
96 write_acked_by_peer,
97 write_acked_by_peer_and_sis, /* and set_in_sync */
98 conflict_discarded_by_peer,
99 neg_acked,
100 barrier_acked, /* in protocol A and B */
101 data_received, /* (remote read) */
102
103 read_completed_with_error,
104 read_ahead_completed_with_error,
105 write_completed_with_error,
106 completed_ok,
107 nothing, /* for tracing only */
108};
109
110/* encoding of request states for now. we don't actually need that many bits.
111 * we don't need to do atomic bit operations either, since most of the time we
112 * need to look at the connection state and/or manipulate some lists at the
113 * same time, so we should hold the request lock anyways.
114 */
115enum drbd_req_state_bits {
116 /* 210
117 * 000: no local possible
118 * 001: to be submitted
119 * UNUSED, we could map: 011: submitted, completion still pending
120 * 110: completed ok
121 * 010: completed with error
122 */
123 __RQ_LOCAL_PENDING,
124 __RQ_LOCAL_COMPLETED,
125 __RQ_LOCAL_OK,
126
127 /* 76543
128 * 00000: no network possible
129 * 00001: to be send
130 * 00011: to be send, on worker queue
131 * 00101: sent, expecting recv_ack (B) or write_ack (C)
132 * 11101: sent,
133 * recv_ack (B) or implicit "ack" (A),
134 * still waiting for the barrier ack.
135 * master_bio may already be completed and invalidated.
136 * 11100: write_acked (C),
137 * data_received (for remote read, any protocol)
138 * or finally the barrier ack has arrived (B,A)...
139 * request can be freed
140 * 01100: neg-acked (write, protocol C)
141 * or neg-d-acked (read, any protocol)
142 * or killed from the transfer log
143 * during cleanup after connection loss
144 * request can be freed
145 * 01000: canceled or send failed...
146 * request can be freed
147 */
148
149 /* if "SENT" is not set, yet, this can still fail or be canceled.
150 * if "SENT" is set already, we still wait for an Ack packet.
151 * when cleared, the master_bio may be completed.
152 * in (B,A) the request object may still linger on the transaction log
153 * until the corresponding barrier ack comes in */
154 __RQ_NET_PENDING,
155
156 /* If it is QUEUED, and it is a WRITE, it is also registered in the
157 * transfer log. Currently we need this flag to avoid conflicts between
158 * worker canceling the request and tl_clear_barrier killing it from
159 * transfer log. We should restructure the code so this conflict does
160 * no longer occur. */
161 __RQ_NET_QUEUED,
162
163 /* well, actually only "handed over to the network stack".
164 *
165 * TODO can potentially be dropped because of the similar meaning
166 * of RQ_NET_SENT and ~RQ_NET_QUEUED.
167 * however it is not exactly the same. before we drop it
168 * we must ensure that we can tell a request with network part
169 * from a request without, regardless of what happens to it. */
170 __RQ_NET_SENT,
171
172 /* when set, the request may be freed (if RQ_NET_QUEUED is clear).
173 * basically this means the corresponding P_BARRIER_ACK was received */
174 __RQ_NET_DONE,
175
176 /* whether or not we know (C) or pretend (B,A) that the write
177 * was successfully written on the peer.
178 */
179 __RQ_NET_OK,
180
181 /* peer called drbd_set_in_sync() for this write */
182 __RQ_NET_SIS,
183
184 /* keep this last, its for the RQ_NET_MASK */
185 __RQ_NET_MAX,
Philipp Reisner288f4222010-05-27 15:07:43 +0200186
187 /* Set when this is a write, clear for a read */
188 __RQ_WRITE,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700189};
190
191#define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)
192#define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)
193#define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)
194
195#define RQ_LOCAL_MASK ((RQ_LOCAL_OK << 1)-1) /* 0x07 */
196
197#define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)
198#define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)
199#define RQ_NET_SENT (1UL << __RQ_NET_SENT)
200#define RQ_NET_DONE (1UL << __RQ_NET_DONE)
201#define RQ_NET_OK (1UL << __RQ_NET_OK)
202#define RQ_NET_SIS (1UL << __RQ_NET_SIS)
203
204/* 0x1f8 */
205#define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)
206
Philipp Reisner288f4222010-05-27 15:07:43 +0200207#define RQ_WRITE (1UL << __RQ_WRITE)
208
Philipp Reisnerb411b362009-09-25 16:07:19 -0700209/* epoch entries */
210static inline
211struct hlist_head *ee_hash_slot(struct drbd_conf *mdev, sector_t sector)
212{
213 BUG_ON(mdev->ee_hash_s == 0);
214 return mdev->ee_hash +
215 ((unsigned int)(sector>>HT_SHIFT) % mdev->ee_hash_s);
216}
217
218/* transfer log (drbd_request objects) */
219static inline
220struct hlist_head *tl_hash_slot(struct drbd_conf *mdev, sector_t sector)
221{
222 BUG_ON(mdev->tl_hash_s == 0);
223 return mdev->tl_hash +
224 ((unsigned int)(sector>>HT_SHIFT) % mdev->tl_hash_s);
225}
226
227/* application reads (drbd_request objects) */
228static struct hlist_head *ar_hash_slot(struct drbd_conf *mdev, sector_t sector)
229{
230 return mdev->app_reads_hash
231 + ((unsigned int)(sector) % APP_R_HSIZE);
232}
233
234/* when we receive the answer for a read request,
235 * verify that we actually know about it */
236static inline struct drbd_request *_ar_id_to_req(struct drbd_conf *mdev,
237 u64 id, sector_t sector)
238{
239 struct hlist_head *slot = ar_hash_slot(mdev, sector);
240 struct hlist_node *n;
241 struct drbd_request *req;
242
243 hlist_for_each_entry(req, n, slot, colision) {
244 if ((unsigned long)req == (unsigned long)id) {
245 D_ASSERT(req->sector == sector);
246 return req;
247 }
248 }
249 return NULL;
250}
251
252static inline struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
253 struct bio *bio_src)
254{
255 struct bio *bio;
256 struct drbd_request *req =
257 mempool_alloc(drbd_request_mempool, GFP_NOIO);
258 if (likely(req)) {
259 bio = bio_clone(bio_src, GFP_NOIO); /* XXX cannot fail?? */
260
Philipp Reisner288f4222010-05-27 15:07:43 +0200261 req->rq_state = bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700262 req->mdev = mdev;
263 req->master_bio = bio_src;
264 req->private_bio = bio;
265 req->epoch = 0;
266 req->sector = bio->bi_sector;
267 req->size = bio->bi_size;
268 req->start_time = jiffies;
269 INIT_HLIST_NODE(&req->colision);
270 INIT_LIST_HEAD(&req->tl_requests);
271 INIT_LIST_HEAD(&req->w.list);
272
273 bio->bi_private = req;
274 bio->bi_end_io = drbd_endio_pri;
275 bio->bi_next = NULL;
276 }
277 return req;
278}
279
280static inline void drbd_req_free(struct drbd_request *req)
281{
282 mempool_free(req, drbd_request_mempool);
283}
284
285static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
286{
287 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
288}
289
290/* Short lived temporary struct on the stack.
291 * We could squirrel the error to be returned into
292 * bio->bi_size, or similar. But that would be too ugly. */
293struct bio_and_error {
294 struct bio *bio;
295 int error;
296};
297
298extern void _req_may_be_done(struct drbd_request *req,
299 struct bio_and_error *m);
300extern void __req_mod(struct drbd_request *req, enum drbd_req_event what,
301 struct bio_and_error *m);
302extern void complete_master_bio(struct drbd_conf *mdev,
303 struct bio_and_error *m);
304
305/* use this if you don't want to deal with calling complete_master_bio()
306 * outside the spinlock, e.g. when walking some list on cleanup. */
307static inline void _req_mod(struct drbd_request *req, enum drbd_req_event what)
308{
309 struct drbd_conf *mdev = req->mdev;
310 struct bio_and_error m;
311
312 /* __req_mod possibly frees req, do not touch req after that! */
313 __req_mod(req, what, &m);
314 if (m.bio)
315 complete_master_bio(mdev, &m);
316}
317
318/* completion of master bio is outside of spinlock.
319 * If you need it irqsave, do it your self! */
320static inline void req_mod(struct drbd_request *req,
321 enum drbd_req_event what)
322{
323 struct drbd_conf *mdev = req->mdev;
324 struct bio_and_error m;
325 spin_lock_irq(&mdev->req_lock);
326 __req_mod(req, what, &m);
327 spin_unlock_irq(&mdev->req_lock);
328
329 if (m.bio)
330 complete_master_bio(mdev, &m);
331}
332#endif