blob: dc669dfe5b0d1eb16358051f05bb7abf55d06d9b [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_int.h
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@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
26#ifndef _DRBD_INT_H
27#define _DRBD_INT_H
28
29#include <linux/compiler.h>
30#include <linux/types.h>
31#include <linux/version.h>
32#include <linux/list.h>
33#include <linux/sched.h>
34#include <linux/bitops.h>
35#include <linux/slab.h>
36#include <linux/crypto.h>
Randy Dunlap132cc532009-10-07 19:26:00 +020037#include <linux/ratelimit.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070038#include <linux/tcp.h>
39#include <linux/mutex.h>
40#include <linux/major.h>
41#include <linux/blkdev.h>
42#include <linux/genhd.h>
43#include <net/tcp.h>
44#include <linux/lru_cache.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040045#include <linux/prefetch.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070046
47#ifdef __CHECKER__
48# define __protected_by(x) __attribute__((require_context(x,1,999,"rdwr")))
49# define __protected_read_by(x) __attribute__((require_context(x,1,999,"read")))
50# define __protected_write_by(x) __attribute__((require_context(x,1,999,"write")))
51# define __must_hold(x) __attribute__((context(x,1,1), require_context(x,1,999,"call")))
52#else
53# define __protected_by(x)
54# define __protected_read_by(x)
55# define __protected_write_by(x)
56# define __must_hold(x)
57#endif
58
59#define __no_warn(lock, stmt) do { __acquire(lock); stmt; __release(lock); } while (0)
60
61/* module parameter, defined in drbd_main.c */
62extern unsigned int minor_count;
63extern int disable_sendpage;
64extern int allow_oos;
65extern unsigned int cn_idx;
66
67#ifdef CONFIG_DRBD_FAULT_INJECTION
68extern int enable_faults;
69extern int fault_rate;
70extern int fault_devs;
71#endif
72
73extern char usermode_helper[];
74
75
Philipp Reisnerb411b362009-09-25 16:07:19 -070076/* I don't remember why XCPU ...
77 * This is used to wake the asender,
78 * and to interrupt sending the sending task
79 * on disconnect.
80 */
81#define DRBD_SIG SIGXCPU
82
83/* This is used to stop/restart our threads.
84 * Cannot use SIGTERM nor SIGKILL, since these
85 * are sent out by init on runlevel changes
86 * I choose SIGHUP for now.
87 */
88#define DRBD_SIGKILL SIGHUP
89
Philipp Reisnerb411b362009-09-25 16:07:19 -070090#define ID_IN_SYNC (4711ULL)
91#define ID_OUT_OF_SYNC (4712ULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -070092#define ID_SYNCER (-1ULL)
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +010093
Philipp Reisner4a23f262011-01-11 17:42:17 +010094#define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -070095
96struct drbd_conf;
Philipp Reisner21114382011-01-19 12:26:59 +010097struct drbd_tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -070098
99
100/* to shorten dev_warn(DEV, "msg"); and relatives statements */
101#define DEV (disk_to_dev(mdev->vdisk))
102
103#define D_ASSERT(exp) if (!(exp)) \
104 dev_err(DEV, "ASSERT( " #exp " ) in %s:%d\n", __FILE__, __LINE__)
105
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100106/**
107 * expect - Make an assertion
108 *
109 * Unlike the assert macro, this macro returns a boolean result.
110 */
111#define expect(exp) ({ \
112 bool _bool = (exp); \
113 if (!_bool) \
114 dev_err(DEV, "ASSERTION %s FAILED in %s\n", \
115 #exp, __func__); \
116 _bool; \
117 })
Philipp Reisnerb411b362009-09-25 16:07:19 -0700118
119/* Defines to control fault insertion */
120enum {
121 DRBD_FAULT_MD_WR = 0, /* meta data write */
122 DRBD_FAULT_MD_RD = 1, /* read */
123 DRBD_FAULT_RS_WR = 2, /* resync */
124 DRBD_FAULT_RS_RD = 3,
125 DRBD_FAULT_DT_WR = 4, /* data */
126 DRBD_FAULT_DT_RD = 5,
127 DRBD_FAULT_DT_RA = 6, /* data read ahead */
128 DRBD_FAULT_BM_ALLOC = 7, /* bitmap allocation */
129 DRBD_FAULT_AL_EE = 8, /* alloc ee */
Philipp Reisner6b4388a2010-04-26 14:11:45 +0200130 DRBD_FAULT_RECEIVE = 9, /* Changes some bytes upon receiving a [rs]data block */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700131
132 DRBD_FAULT_MAX,
133};
134
Philipp Reisnerb411b362009-09-25 16:07:19 -0700135extern unsigned int
136_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100137
Philipp Reisnerb411b362009-09-25 16:07:19 -0700138static inline int
139drbd_insert_fault(struct drbd_conf *mdev, unsigned int type) {
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100140#ifdef CONFIG_DRBD_FAULT_INJECTION
Philipp Reisnerb411b362009-09-25 16:07:19 -0700141 return fault_rate &&
142 (enable_faults & (1<<type)) &&
143 _drbd_insert_fault(mdev, type);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700144#else
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100145 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700146#endif
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100147}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700148
149/* integer division, round _UP_ to the next integer */
150#define div_ceil(A, B) ((A)/(B) + ((A)%(B) ? 1 : 0))
151/* usual integer division */
152#define div_floor(A, B) ((A)/(B))
153
154/* drbd_meta-data.c (still in drbd_main.c) */
155/* 4th incarnation of the disk layout. */
156#define DRBD_MD_MAGIC (DRBD_MAGIC+4)
157
158extern struct drbd_conf **minor_table;
159extern struct ratelimit_state drbd_ratelimit_state;
160
161/* on the wire */
162enum drbd_packets {
163 /* receiver (data socket) */
164 P_DATA = 0x00,
165 P_DATA_REPLY = 0x01, /* Response to P_DATA_REQUEST */
166 P_RS_DATA_REPLY = 0x02, /* Response to P_RS_DATA_REQUEST */
167 P_BARRIER = 0x03,
168 P_BITMAP = 0x04,
169 P_BECOME_SYNC_TARGET = 0x05,
170 P_BECOME_SYNC_SOURCE = 0x06,
171 P_UNPLUG_REMOTE = 0x07, /* Used at various times to hint the peer */
172 P_DATA_REQUEST = 0x08, /* Used to ask for a data block */
173 P_RS_DATA_REQUEST = 0x09, /* Used to ask for a data block for resync */
174 P_SYNC_PARAM = 0x0a,
175 P_PROTOCOL = 0x0b,
176 P_UUIDS = 0x0c,
177 P_SIZES = 0x0d,
178 P_STATE = 0x0e,
179 P_SYNC_UUID = 0x0f,
180 P_AUTH_CHALLENGE = 0x10,
181 P_AUTH_RESPONSE = 0x11,
182 P_STATE_CHG_REQ = 0x12,
183
184 /* asender (meta socket */
185 P_PING = 0x13,
186 P_PING_ACK = 0x14,
187 P_RECV_ACK = 0x15, /* Used in protocol B */
188 P_WRITE_ACK = 0x16, /* Used in protocol C */
189 P_RS_WRITE_ACK = 0x17, /* Is a P_WRITE_ACK, additionally call set_in_sync(). */
190 P_DISCARD_ACK = 0x18, /* Used in proto C, two-primaries conflict detection */
191 P_NEG_ACK = 0x19, /* Sent if local disk is unusable */
192 P_NEG_DREPLY = 0x1a, /* Local disk is broken... */
193 P_NEG_RS_DREPLY = 0x1b, /* Local disk is broken... */
194 P_BARRIER_ACK = 0x1c,
195 P_STATE_CHG_REPLY = 0x1d,
196
197 /* "new" commands, no longer fitting into the ordering scheme above */
198
199 P_OV_REQUEST = 0x1e, /* data socket */
200 P_OV_REPLY = 0x1f,
201 P_OV_RESULT = 0x20, /* meta socket */
202 P_CSUM_RS_REQUEST = 0x21, /* data socket */
203 P_RS_IS_IN_SYNC = 0x22, /* meta socket */
204 P_SYNC_PARAM89 = 0x23, /* data socket, protocol version 89 replacement for P_SYNC_PARAM */
205 P_COMPRESSED_BITMAP = 0x24, /* compressed or otherwise encoded bitmap transfer */
Philipp Reisner0ced55a2010-04-30 15:26:20 +0200206 /* P_CKPT_FENCE_REQ = 0x25, * currently reserved for protocol D */
207 /* P_CKPT_DISABLE_REQ = 0x26, * currently reserved for protocol D */
208 P_DELAY_PROBE = 0x27, /* is used on BOTH sockets */
Philipp Reisner73a01a12010-10-27 14:33:00 +0200209 P_OUT_OF_SYNC = 0x28, /* Mark as out of sync (Outrunning), data socket */
Philipp Reisnerd612d302010-12-27 10:53:28 +0100210 P_RS_CANCEL = 0x29, /* meta: Used to cancel RS_DATA_REQUEST packet by SyncSource */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700211
Philipp Reisnerd612d302010-12-27 10:53:28 +0100212 P_MAX_CMD = 0x2A,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700213 P_MAY_IGNORE = 0x100, /* Flag to test if (cmd > P_MAY_IGNORE) ... */
214 P_MAX_OPT_CMD = 0x101,
215
216 /* special command ids for handshake */
217
218 P_HAND_SHAKE_M = 0xfff1, /* First Packet on the MetaSock */
219 P_HAND_SHAKE_S = 0xfff2, /* First Packet on the Socket */
220
221 P_HAND_SHAKE = 0xfffe /* FIXED for the next century! */
222};
223
224static inline const char *cmdname(enum drbd_packets cmd)
225{
226 /* THINK may need to become several global tables
227 * when we want to support more than
228 * one PRO_VERSION */
229 static const char *cmdnames[] = {
230 [P_DATA] = "Data",
231 [P_DATA_REPLY] = "DataReply",
232 [P_RS_DATA_REPLY] = "RSDataReply",
233 [P_BARRIER] = "Barrier",
234 [P_BITMAP] = "ReportBitMap",
235 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
236 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
237 [P_UNPLUG_REMOTE] = "UnplugRemote",
238 [P_DATA_REQUEST] = "DataRequest",
239 [P_RS_DATA_REQUEST] = "RSDataRequest",
240 [P_SYNC_PARAM] = "SyncParam",
241 [P_SYNC_PARAM89] = "SyncParam89",
242 [P_PROTOCOL] = "ReportProtocol",
243 [P_UUIDS] = "ReportUUIDs",
244 [P_SIZES] = "ReportSizes",
245 [P_STATE] = "ReportState",
246 [P_SYNC_UUID] = "ReportSyncUUID",
247 [P_AUTH_CHALLENGE] = "AuthChallenge",
248 [P_AUTH_RESPONSE] = "AuthResponse",
249 [P_PING] = "Ping",
250 [P_PING_ACK] = "PingAck",
251 [P_RECV_ACK] = "RecvAck",
252 [P_WRITE_ACK] = "WriteAck",
253 [P_RS_WRITE_ACK] = "RSWriteAck",
254 [P_DISCARD_ACK] = "DiscardAck",
255 [P_NEG_ACK] = "NegAck",
256 [P_NEG_DREPLY] = "NegDReply",
257 [P_NEG_RS_DREPLY] = "NegRSDReply",
258 [P_BARRIER_ACK] = "BarrierAck",
259 [P_STATE_CHG_REQ] = "StateChgRequest",
260 [P_STATE_CHG_REPLY] = "StateChgReply",
261 [P_OV_REQUEST] = "OVRequest",
262 [P_OV_REPLY] = "OVReply",
263 [P_OV_RESULT] = "OVResult",
Lars Ellenbergc42b6cf2010-03-03 02:44:11 +0100264 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
265 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
266 [P_COMPRESSED_BITMAP] = "CBitmap",
Philipp Reisnera8cdfd82010-05-05 20:53:33 +0200267 [P_DELAY_PROBE] = "DelayProbe",
Philipp Reisner73a01a12010-10-27 14:33:00 +0200268 [P_OUT_OF_SYNC] = "OutOfSync",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700269 [P_MAX_CMD] = NULL,
270 };
271
272 if (cmd == P_HAND_SHAKE_M)
273 return "HandShakeM";
274 if (cmd == P_HAND_SHAKE_S)
275 return "HandShakeS";
276 if (cmd == P_HAND_SHAKE)
277 return "HandShake";
278 if (cmd >= P_MAX_CMD)
279 return "Unknown";
280 return cmdnames[cmd];
281}
282
283/* for sending/receiving the bitmap,
284 * possibly in some encoding scheme */
285struct bm_xfer_ctx {
286 /* "const"
287 * stores total bits and long words
288 * of the bitmap, so we don't need to
289 * call the accessor functions over and again. */
290 unsigned long bm_bits;
291 unsigned long bm_words;
292 /* during xfer, current position within the bitmap */
293 unsigned long bit_offset;
294 unsigned long word_offset;
295
296 /* statistics; index: (h->command == P_BITMAP) */
297 unsigned packets[2];
298 unsigned bytes[2];
299};
300
301extern void INFO_bm_xfer_stats(struct drbd_conf *mdev,
302 const char *direction, struct bm_xfer_ctx *c);
303
304static inline void bm_xfer_ctx_bit_to_word_offset(struct bm_xfer_ctx *c)
305{
306 /* word_offset counts "native long words" (32 or 64 bit),
307 * aligned at 64 bit.
308 * Encoded packet may end at an unaligned bit offset.
309 * In case a fallback clear text packet is transmitted in
310 * between, we adjust this offset back to the last 64bit
311 * aligned "native long word", which makes coding and decoding
312 * the plain text bitmap much more convenient. */
313#if BITS_PER_LONG == 64
314 c->word_offset = c->bit_offset >> 6;
315#elif BITS_PER_LONG == 32
316 c->word_offset = c->bit_offset >> 5;
317 c->word_offset &= ~(1UL);
318#else
319# error "unsupported BITS_PER_LONG"
320#endif
321}
322
323#ifndef __packed
324#define __packed __attribute__((packed))
325#endif
326
327/* This is the layout for a packet on the wire.
328 * The byteorder is the network byte order.
329 * (except block_id and barrier fields.
330 * these are pointers to local structs
331 * and have no relevance for the partner,
332 * which just echoes them as received.)
333 *
334 * NOTE that the payload starts at a long aligned offset,
335 * regardless of 32 or 64 bit arch!
336 */
Philipp Reisner0b70a132010-08-20 13:36:10 +0200337struct p_header80 {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700338 u32 magic;
339 u16 command;
340 u16 length; /* bytes of data after this header */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700341} __packed;
Philipp Reisner0b70a132010-08-20 13:36:10 +0200342
343/* Header for big packets, Used for data packets exceeding 64kB */
344struct p_header95 {
345 u16 magic; /* use DRBD_MAGIC_BIG here */
346 u16 command;
Philipp Reisner00b42532010-10-05 11:19:39 +0200347 u32 length; /* Use only 24 bits of that. Ignore the highest 8 bit. */
Philipp Reisner0b70a132010-08-20 13:36:10 +0200348 u8 payload[0];
349} __packed;
350
Philipp Reisnerc0129492011-01-19 16:58:16 +0100351struct p_header {
352 union {
353 struct p_header80 h80;
354 struct p_header95 h95;
355 };
356 u8 payload[0];
Philipp Reisner0b70a132010-08-20 13:36:10 +0200357};
Philipp Reisnerb411b362009-09-25 16:07:19 -0700358
359/*
360 * short commands, packets without payload, plain p_header:
361 * P_PING
362 * P_PING_ACK
363 * P_BECOME_SYNC_TARGET
364 * P_BECOME_SYNC_SOURCE
365 * P_UNPLUG_REMOTE
366 */
367
368/*
369 * commands with out-of-struct payload:
370 * P_BITMAP (no additional fields)
371 * P_DATA, P_DATA_REPLY (see p_data)
372 * P_COMPRESSED_BITMAP (see receive_compressed_bitmap)
373 */
374
375/* these defines must not be changed without changing the protocol version */
Philipp Reisner76d2e7e2010-08-25 11:58:05 +0200376#define DP_HARDBARRIER 1 /* depricated */
377#define DP_RW_SYNC 2 /* equals REQ_SYNC */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700378#define DP_MAY_SET_IN_SYNC 4
Jens Axboe721a9602011-03-09 11:56:30 +0100379#define DP_UNPLUG 8 /* not used anymore */
Philipp Reisner76d2e7e2010-08-25 11:58:05 +0200380#define DP_FUA 16 /* equals REQ_FUA */
381#define DP_FLUSH 32 /* equals REQ_FLUSH */
382#define DP_DISCARD 64 /* equals REQ_DISCARD */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383
384struct p_data {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100385 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700386 u64 sector; /* 64 bits sector number */
387 u64 block_id; /* to identify the request in protocol B&C */
388 u32 seq_num;
389 u32 dp_flags;
390} __packed;
391
392/*
393 * commands which share a struct:
394 * p_block_ack:
395 * P_RECV_ACK (proto B), P_WRITE_ACK (proto C),
396 * P_DISCARD_ACK (proto C, two-primaries conflict detection)
397 * p_block_req:
398 * P_DATA_REQUEST, P_RS_DATA_REQUEST
399 */
400struct p_block_ack {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100401 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700402 u64 sector;
403 u64 block_id;
404 u32 blksize;
405 u32 seq_num;
406} __packed;
407
408
409struct p_block_req {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100410 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700411 u64 sector;
412 u64 block_id;
413 u32 blksize;
414 u32 pad; /* to multiple of 8 Byte */
415} __packed;
416
417/*
418 * commands with their own struct for additional fields:
419 * P_HAND_SHAKE
420 * P_BARRIER
421 * P_BARRIER_ACK
422 * P_SYNC_PARAM
423 * ReportParams
424 */
425
426struct p_handshake {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100427 struct p_header head; /* Note: You must always use a h80 here */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700428 u32 protocol_min;
429 u32 feature_flags;
430 u32 protocol_max;
431
432 /* should be more than enough for future enhancements
433 * for now, feature_flags and the reserverd array shall be zero.
434 */
435
436 u32 _pad;
437 u64 reserverd[7];
438} __packed;
439/* 80 bytes, FIXED for the next century */
440
441struct p_barrier {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100442 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700443 u32 barrier; /* barrier number _handle_ only */
444 u32 pad; /* to multiple of 8 Byte */
445} __packed;
446
447struct p_barrier_ack {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100448 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700449 u32 barrier;
450 u32 set_size;
451} __packed;
452
453struct p_rs_param {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100454 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455 u32 rate;
456
457 /* Since protocol version 88 and higher. */
458 char verify_alg[0];
459} __packed;
460
461struct p_rs_param_89 {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100462 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700463 u32 rate;
464 /* protocol version 89: */
465 char verify_alg[SHARED_SECRET_MAX];
466 char csums_alg[SHARED_SECRET_MAX];
467} __packed;
468
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200469struct p_rs_param_95 {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100470 struct p_header head;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200471 u32 rate;
472 char verify_alg[SHARED_SECRET_MAX];
473 char csums_alg[SHARED_SECRET_MAX];
474 u32 c_plan_ahead;
475 u32 c_delay_target;
476 u32 c_fill_target;
477 u32 c_max_rate;
478} __packed;
479
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100480enum drbd_conn_flags {
481 CF_WANT_LOSE = 1,
482 CF_DRY_RUN = 2,
483};
484
Philipp Reisnerb411b362009-09-25 16:07:19 -0700485struct p_protocol {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100486 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700487 u32 protocol;
488 u32 after_sb_0p;
489 u32 after_sb_1p;
490 u32 after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100491 u32 conn_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700492 u32 two_primaries;
493
494 /* Since protocol version 87 and higher. */
495 char integrity_alg[0];
496
497} __packed;
498
499struct p_uuids {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100500 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501 u64 uuid[UI_EXTENDED_SIZE];
502} __packed;
503
504struct p_rs_uuid {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100505 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700506 u64 uuid;
507} __packed;
508
509struct p_sizes {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100510 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700511 u64 d_size; /* size of disk */
512 u64 u_size; /* user requested size */
513 u64 c_size; /* current exported size */
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100514 u32 max_bio_size; /* Maximal size of a BIO */
Philipp Reisnere89b5912010-03-24 17:11:33 +0100515 u16 queue_order_type; /* not yet implemented in DRBD*/
516 u16 dds_flags; /* use enum dds_flags here. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700517} __packed;
518
519struct p_state {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100520 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 u32 state;
522} __packed;
523
524struct p_req_state {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100525 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526 u32 mask;
527 u32 val;
528} __packed;
529
530struct p_req_state_reply {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100531 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532 u32 retcode;
533} __packed;
534
535struct p_drbd06_param {
536 u64 size;
537 u32 state;
538 u32 blksize;
539 u32 protocol;
540 u32 version;
541 u32 gen_cnt[5];
542 u32 bit_map_gen[5];
543} __packed;
544
545struct p_discard {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100546 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547 u64 block_id;
548 u32 seq_num;
549 u32 pad;
550} __packed;
551
Philipp Reisner73a01a12010-10-27 14:33:00 +0200552struct p_block_desc {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100553 struct p_header head;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200554 u64 sector;
555 u32 blksize;
556 u32 pad; /* to multiple of 8 Byte */
557} __packed;
558
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559/* Valid values for the encoding field.
560 * Bump proto version when changing this. */
561enum drbd_bitmap_code {
562 /* RLE_VLI_Bytes = 0,
563 * and other bit variants had been defined during
564 * algorithm evaluation. */
565 RLE_VLI_Bits = 2,
566};
567
568struct p_compressed_bm {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100569 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700570 /* (encoding & 0x0f): actual encoding, see enum drbd_bitmap_code
571 * (encoding & 0x80): polarity (set/unset) of first runlength
572 * ((encoding >> 4) & 0x07): pad_bits, number of trailing zero bits
573 * used to pad up to head.length bytes
574 */
575 u8 encoding;
576
577 u8 code[0];
578} __packed;
579
Philipp Reisner0b70a132010-08-20 13:36:10 +0200580struct p_delay_probe93 {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100581 struct p_header head;
Philipp Reisner0b70a132010-08-20 13:36:10 +0200582 u32 seq_num; /* sequence number to match the two probe packets */
583 u32 offset; /* usecs the probe got sent after the reference time point */
Philipp Reisner0ced55a2010-04-30 15:26:20 +0200584} __packed;
585
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586/* DCBP: Drbd Compressed Bitmap Packet ... */
587static inline enum drbd_bitmap_code
588DCBP_get_code(struct p_compressed_bm *p)
589{
590 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
591}
592
593static inline void
594DCBP_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
595{
596 BUG_ON(code & ~0xf);
597 p->encoding = (p->encoding & ~0xf) | code;
598}
599
600static inline int
601DCBP_get_start(struct p_compressed_bm *p)
602{
603 return (p->encoding & 0x80) != 0;
604}
605
606static inline void
607DCBP_set_start(struct p_compressed_bm *p, int set)
608{
609 p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
610}
611
612static inline int
613DCBP_get_pad_bits(struct p_compressed_bm *p)
614{
615 return (p->encoding >> 4) & 0x7;
616}
617
618static inline void
619DCBP_set_pad_bits(struct p_compressed_bm *p, int n)
620{
621 BUG_ON(n & ~0x7);
622 p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
623}
624
625/* one bitmap packet, including the p_header,
626 * should fit within one _architecture independend_ page.
627 * so we need to use the fixed size 4KiB page size
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300628 * most architectures have used for a long time.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700629 */
Philipp Reisnerc0129492011-01-19 16:58:16 +0100630#define BM_PACKET_PAYLOAD_BYTES (4096 - sizeof(struct p_header))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631#define BM_PACKET_WORDS (BM_PACKET_PAYLOAD_BYTES/sizeof(long))
632#define BM_PACKET_VLI_BYTES_MAX (4096 - sizeof(struct p_compressed_bm))
633#if (PAGE_SIZE < 4096)
634/* drbd_send_bitmap / receive_bitmap would break horribly */
635#error "PAGE_SIZE too small"
636#endif
637
638union p_polymorph {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100639 struct p_header header;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700640 struct p_handshake handshake;
641 struct p_data data;
642 struct p_block_ack block_ack;
643 struct p_barrier barrier;
644 struct p_barrier_ack barrier_ack;
645 struct p_rs_param_89 rs_param_89;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200646 struct p_rs_param_95 rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700647 struct p_protocol protocol;
648 struct p_sizes sizes;
649 struct p_uuids uuids;
650 struct p_state state;
651 struct p_req_state req_state;
652 struct p_req_state_reply req_state_reply;
653 struct p_block_req block_req;
Philipp Reisner02918be2010-08-20 14:35:10 +0200654 struct p_delay_probe93 delay_probe93;
655 struct p_rs_uuid rs_uuid;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200656 struct p_block_desc block_desc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700657} __packed;
658
659/**********************************************************************/
660enum drbd_thread_state {
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100661 NONE,
662 RUNNING,
663 EXITING,
664 RESTARTING
Philipp Reisnerb411b362009-09-25 16:07:19 -0700665};
666
667struct drbd_thread {
668 spinlock_t t_lock;
669 struct task_struct *task;
670 struct completion stop;
671 enum drbd_thread_state t_state;
672 int (*function) (struct drbd_thread *);
673 struct drbd_conf *mdev;
674 int reset_cpu_mask;
675};
676
677static inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
678{
679 /* THINK testing the t_state seems to be uncritical in all cases
680 * (but thread_{start,stop}), so we can read it *without* the lock.
681 * --lge */
682
683 smp_rmb();
684 return thi->t_state;
685}
686
Philipp Reisnerb411b362009-09-25 16:07:19 -0700687struct drbd_work;
688typedef int (*drbd_work_cb)(struct drbd_conf *, struct drbd_work *, int cancel);
689struct drbd_work {
690 struct list_head list;
691 drbd_work_cb cb;
692};
693
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100694#include "drbd_interval.h"
695
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696struct drbd_request {
697 struct drbd_work w;
698 struct drbd_conf *mdev;
699
700 /* if local IO is not allowed, will be NULL.
701 * if local IO _is_ allowed, holds the locally submitted bio clone,
702 * or, after local IO completion, the ERR_PTR(error).
703 * see drbd_endio_pri(). */
704 struct bio *private_bio;
705
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100706 struct drbd_interval i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707 unsigned int epoch; /* barrier_nr */
708
709 /* barrier_nr: used to check on "completion" whether this req was in
710 * the current epoch, and we therefore have to close it,
711 * starting a new epoch...
712 */
713
Philipp Reisnerb411b362009-09-25 16:07:19 -0700714 struct list_head tl_requests; /* ring list in the transfer log */
715 struct bio *master_bio; /* master bio pointer */
716 unsigned long rq_state; /* see comments above _req_mod() */
717 int seq_num;
718 unsigned long start_time;
719};
720
721struct drbd_tl_epoch {
722 struct drbd_work w;
723 struct list_head requests; /* requests before */
724 struct drbd_tl_epoch *next; /* pointer to the next barrier */
725 unsigned int br_number; /* the barriers identifier. */
Philipp Reisner7e602c02010-05-27 14:49:27 +0200726 int n_writes; /* number of requests attached before this barrier */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700727};
728
729struct drbd_request;
730
731/* These Tl_epoch_entries may be in one of 6 lists:
732 active_ee .. data packet being written
733 sync_ee .. syncer block being written
734 done_ee .. block written, need to send P_WRITE_ACK
735 read_ee .. [RS]P_DATA_REQUEST being read
736*/
737
738struct drbd_epoch {
739 struct list_head list;
740 unsigned int barrier_nr;
741 atomic_t epoch_size; /* increased on every request added. */
742 atomic_t active; /* increased on every req. added, and dec on every finished. */
743 unsigned long flags;
744};
745
746/* drbd_epoch flag bits */
747enum {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700748 DE_HAVE_BARRIER_NUMBER,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700749};
750
751enum epoch_event {
752 EV_PUT,
753 EV_GOT_BARRIER_NR,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754 EV_BECAME_LAST,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700755 EV_CLEANUP = 32, /* used as flag */
756};
757
Philipp Reisnerb411b362009-09-25 16:07:19 -0700758struct drbd_wq_barrier {
759 struct drbd_work w;
760 struct completion done;
761};
762
763struct digest_info {
764 int digest_size;
765 void *digest;
766};
767
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200768struct drbd_epoch_entry {
769 struct drbd_work w;
Philipp Reisner85719572010-07-21 10:20:17 +0200770 struct drbd_epoch *epoch; /* for writes */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200771 struct drbd_conf *mdev;
772 struct page *pages;
773 atomic_t pending_bios;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +0100774 struct drbd_interval i;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200775 /* see comments on ee flag bits below */
776 unsigned long flags;
Philipp Reisner85719572010-07-21 10:20:17 +0200777 union {
778 u64 block_id;
779 struct digest_info *digest;
780 };
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200781};
782
783/* ee flag bits.
784 * While corresponding bios are in flight, the only modification will be
785 * set_bit WAS_ERROR, which has to be atomic.
786 * If no bios are in flight yet, or all have been completed,
787 * non-atomic modification to ee->flags is ok.
788 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700789enum {
790 __EE_CALL_AL_COMPLETE_IO,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791 __EE_MAY_SET_IN_SYNC,
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200792
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200793 /* In case a barrier failed,
794 * we need to resubmit without the barrier flag. */
795 __EE_RESUBMITTED,
796
797 /* we may have several bios per epoch entry.
798 * if any of those fail, we set this flag atomically
799 * from the endio callback */
800 __EE_WAS_ERROR,
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200801
802 /* This ee has a pointer to a digest instead of a block id */
803 __EE_HAS_DIGEST,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804};
805#define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700806#define EE_MAY_SET_IN_SYNC (1<<__EE_MAY_SET_IN_SYNC)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200807#define EE_RESUBMITTED (1<<__EE_RESUBMITTED)
808#define EE_WAS_ERROR (1<<__EE_WAS_ERROR)
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200809#define EE_HAS_DIGEST (1<<__EE_HAS_DIGEST)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700810
811/* global flag bits */
812enum {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300813 CREATE_BARRIER, /* next P_DATA is preceded by a P_BARRIER */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700814 SIGNAL_ASENDER, /* whether asender wants to be interrupted */
815 SEND_PING, /* whether asender should send a ping asap */
816
Philipp Reisnerb411b362009-09-25 16:07:19 -0700817 UNPLUG_QUEUED, /* only relevant with kernel 2.4 */
818 UNPLUG_REMOTE, /* sending a "UnplugRemote" could help */
819 MD_DIRTY, /* current uuids and flags not yet on disk */
820 DISCARD_CONCURRENT, /* Set on one node, cleared on the peer! */
821 USE_DEGR_WFC_T, /* degr-wfc-timeout instead of wfc-timeout. */
822 CLUSTER_ST_CHANGE, /* Cluster wide state change going on... */
823 CL_ST_CHG_SUCCESS,
824 CL_ST_CHG_FAIL,
825 CRASHED_PRIMARY, /* This node was a crashed primary.
826 * Gets cleared when the state.conn
827 * goes into C_CONNECTED state. */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100828 NO_BARRIER_SUPP, /* underlying block device doesn't implement barriers */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700829 CONSIDER_RESYNC,
830
Philipp Reisnera8a4e512010-08-25 10:21:04 +0200831 MD_NO_FUA, /* Users wants us to not use FUA/FLUSH on meta data dev */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700832 SUSPEND_IO, /* suspend application io */
833 BITMAP_IO, /* suspend application io;
834 once no more io in flight, start bitmap io */
835 BITMAP_IO_QUEUED, /* Started bitmap IO */
Lars Ellenberg82f59cc2010-10-16 12:13:47 +0200836 GO_DISKLESS, /* Disk is being detached, on io-error or admin request. */
837 WAS_IO_ERROR, /* Local disk failed returned IO error */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700838 RESYNC_AFTER_NEG, /* Resync after online grow after the attach&negotiate finished. */
839 NET_CONGESTED, /* The data socket is congested */
840
841 CONFIG_PENDING, /* serialization of (re)configuration requests.
842 * if set, also prevents the device from dying */
843 DEVICE_DYING, /* device became unconfigured,
844 * but worker thread is still handling the cleanup.
845 * reconfiguring (nl_disk_conf, nl_net_conf) is dissalowed,
846 * while this is set. */
847 RESIZE_PENDING, /* Size change detected locally, waiting for the response from
848 * the peer, if it changed there as well. */
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100849 CONN_DRY_RUN, /* Expect disconnect after resync handshake. */
Philipp Reisner309d1602010-03-02 15:03:44 +0100850 GOT_PING_ACK, /* set when we receive a ping_ack packet, misc wait gets woken */
Philipp Reisner43a51822010-06-11 11:26:34 +0200851 NEW_CUR_UUID, /* Create new current UUID when thawing IO */
Philipp Reisner07782862010-08-31 12:00:50 +0200852 AL_SUSPENDED, /* Activity logging is currently suspended. */
Philipp Reisner370a43e2011-01-14 16:03:11 +0100853 AHEAD_TO_SYNC_SOURCE, /* Ahead -> SyncSource queued */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700854};
855
856struct drbd_bitmap; /* opaque for drbd_conf */
857
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100858/* definition of bits in bm_flags to be used in drbd_bm_lock
859 * and drbd_bitmap_io and friends. */
860enum bm_flag {
861 /* do we need to kfree, or vfree bm_pages? */
862 BM_P_VMALLOCED = 0x10000, /* internal use only, will be masked out */
863
864 /* currently locked for bulk operation */
865 BM_LOCKED_MASK = 0x7,
866
867 /* in detail, that is: */
868 BM_DONT_CLEAR = 0x1,
869 BM_DONT_SET = 0x2,
870 BM_DONT_TEST = 0x4,
871
872 /* (test bit, count bit) allowed (common case) */
873 BM_LOCKED_TEST_ALLOWED = 0x3,
874
875 /* testing bits, as well as setting new bits allowed, but clearing bits
876 * would be unexpected. Used during bitmap receive. Setting new bits
877 * requires sending of "out-of-sync" information, though. */
878 BM_LOCKED_SET_ALLOWED = 0x1,
879
880 /* clear is not expected while bitmap is locked for bulk operation */
881};
882
883
Philipp Reisnerb411b362009-09-25 16:07:19 -0700884/* TODO sort members for performance
885 * MAYBE group them further */
886
887/* THINK maybe we actually want to use the default "event/%s" worker threads
888 * or similar in linux 2.6, which uses per cpu data and threads.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700889 */
890struct drbd_work_queue {
891 struct list_head q;
892 struct semaphore s; /* producers up it, worker down()s it */
893 spinlock_t q_lock; /* to protect the list. */
894};
895
896struct drbd_socket {
897 struct drbd_work_queue work;
898 struct mutex mutex;
899 struct socket *socket;
900 /* this way we get our
901 * send/receive buffers off the stack */
902 union p_polymorph sbuf;
903 union p_polymorph rbuf;
904};
905
906struct drbd_md {
907 u64 md_offset; /* sector offset to 'super' block */
908
909 u64 la_size_sect; /* last agreed size, unit sectors */
910 u64 uuid[UI_SIZE];
911 u64 device_uuid;
912 u32 flags;
913 u32 md_size_sect;
914
915 s32 al_offset; /* signed relative sector offset to al area */
916 s32 bm_offset; /* signed relative sector offset to bitmap */
917
918 /* u32 al_nr_extents; important for restoring the AL
919 * is stored into sync_conf.al_extents, which in turn
920 * gets applied to act_log->nr_elements
921 */
922};
923
924/* for sync_conf and other types... */
925#define NL_PACKET(name, number, fields) struct name { fields };
926#define NL_INTEGER(pn,pr,member) int member;
927#define NL_INT64(pn,pr,member) __u64 member;
928#define NL_BIT(pn,pr,member) unsigned member:1;
929#define NL_STRING(pn,pr,member,len) unsigned char member[len]; int member ## _len;
930#include "linux/drbd_nl.h"
931
932struct drbd_backing_dev {
933 struct block_device *backing_bdev;
934 struct block_device *md_bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 struct drbd_md md;
936 struct disk_conf dc; /* The user provided config... */
937 sector_t known_size; /* last known size of that backing device */
938};
939
940struct drbd_md_io {
941 struct drbd_conf *mdev;
942 struct completion event;
943 int error;
944};
945
946struct bm_io_work {
947 struct drbd_work w;
948 char *why;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100949 enum bm_flag flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950 int (*io_fn)(struct drbd_conf *mdev);
951 void (*done)(struct drbd_conf *mdev, int rv);
952};
953
954enum write_ordering_e {
955 WO_none,
956 WO_drain_io,
957 WO_bdev_flush,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958};
959
Philipp Reisner778f2712010-07-06 11:14:00 +0200960struct fifo_buffer {
961 int *values;
962 unsigned int head_index;
963 unsigned int size;
964};
965
Philipp Reisner21114382011-01-19 12:26:59 +0100966struct drbd_tconn { /* is a resource from the config file */
967 char *name; /* Resource name */
968 struct list_head all_tconn; /* List of all drbd_tconn, prot by global_state_lock */
969 struct drbd_conf *volume0; /* TODO: Remove me again */
970
971 struct net_conf *net_conf; /* protected by get_net_conf() and put_net_conf() */
Philipp Reisnerb2fb6db2011-01-19 13:48:44 +0100972 atomic_t net_cnt; /* Users of net_conf */
973 wait_queue_head_t net_cnt_wait;
Philipp Reisnere42325a2011-01-19 13:55:45 +0100974
975 struct drbd_socket data; /* data/barrier/cstate/parameter packets */
976 struct drbd_socket meta; /* ping/ack (metadata) packets */
Philipp Reisner31890f42011-01-19 14:12:51 +0100977 int agreed_pro_version; /* actually used protocol version */
978 unsigned long last_received; /* in jiffies, either socket */
979 unsigned int ko_count;
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100980
Philipp Reisner87eeee42011-01-19 14:16:30 +0100981 spinlock_t req_lock;
982 struct drbd_tl_epoch *unused_spare_tle; /* for pre-allocation */
983 struct drbd_tl_epoch *newest_tle;
984 struct drbd_tl_epoch *oldest_tle;
985 struct list_head out_of_sequence_requests;
986
Philipp Reisnera0638452011-01-19 14:31:32 +0100987 struct crypto_hash *cram_hmac_tfm;
988 struct crypto_hash *integrity_w_tfm; /* to be used by the worker thread */
989 struct crypto_hash *integrity_r_tfm; /* to be used by the receiver thread */
990 void *int_dig_out;
991 void *int_dig_in;
992 void *int_dig_vv;
993
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100994 struct drbd_thread receiver;
995 struct drbd_thread worker;
996 struct drbd_thread asender;
Philipp Reisner21114382011-01-19 12:26:59 +0100997};
998
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999struct drbd_conf {
Philipp Reisner21114382011-01-19 12:26:59 +01001000 struct drbd_tconn *tconn;
1001 int vnr; /* volume number within the connection */
1002
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003 /* things that are stored as / read from meta data on disk */
1004 unsigned long flags;
1005
1006 /* configured by drbdsetup */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001007 struct syncer_conf sync_conf;
1008 struct drbd_backing_dev *ldev __protected_by(local);
1009
1010 sector_t p_size; /* partner's disk size */
1011 struct request_queue *rq_queue;
1012 struct block_device *this_bdev;
1013 struct gendisk *vdisk;
1014
Philipp Reisnerb411b362009-09-25 16:07:19 -07001015 struct drbd_work resync_work,
1016 unplug_work,
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001017 go_diskless,
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001018 md_sync_work,
1019 start_resync_work;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020 struct timer_list resync_timer;
1021 struct timer_list md_sync_timer;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001022 struct timer_list start_resync_timer;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001023 struct timer_list request_timer;
Lars Ellenbergee15b032010-09-03 10:00:09 +02001024#ifdef DRBD_DEBUG_MD_SYNC
1025 struct {
1026 unsigned int line;
1027 const char* func;
1028 } last_md_mark_dirty;
1029#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030
1031 /* Used after attach while negotiating new disk state. */
1032 union drbd_state new_state_tmp;
1033
1034 union drbd_state state;
1035 wait_queue_head_t misc_wait;
1036 wait_queue_head_t state_wait; /* upon each state change. */
1037 unsigned int send_cnt;
1038 unsigned int recv_cnt;
1039 unsigned int read_cnt;
1040 unsigned int writ_cnt;
1041 unsigned int al_writ_cnt;
1042 unsigned int bm_writ_cnt;
1043 atomic_t ap_bio_cnt; /* Requests we need to complete */
1044 atomic_t ap_pending_cnt; /* AP data packets on the wire, ack expected */
1045 atomic_t rs_pending_cnt; /* RS request/data packets on the wire */
1046 atomic_t unacked_cnt; /* Need to send replys for */
1047 atomic_t local_cnt; /* Waiting for local completion */
Philipp Reisnerb2fb6db2011-01-19 13:48:44 +01001048
Andreas Gruenbacherdac13892011-01-21 17:18:39 +01001049 /* Interval tree of pending local requests */
1050 struct rb_root read_requests;
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001051 struct rb_root write_requests;
1052
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001053 /* blocks to resync in this run [unit BM_BLOCK_SIZE] */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001054 unsigned long rs_total;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001055 /* number of resync blocks that failed in this run */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056 unsigned long rs_failed;
1057 /* Syncer's start time [unit jiffies] */
1058 unsigned long rs_start;
1059 /* cumulated time in PausedSyncX state [unit jiffies] */
1060 unsigned long rs_paused;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001061 /* skipped because csum was equal [unit BM_BLOCK_SIZE] */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062 unsigned long rs_same_csum;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001063#define DRBD_SYNC_MARKS 8
1064#define DRBD_SYNC_MARK_STEP (3*HZ)
1065 /* block not up-to-date at mark [unit BM_BLOCK_SIZE] */
1066 unsigned long rs_mark_left[DRBD_SYNC_MARKS];
1067 /* marks's time [unit jiffies] */
1068 unsigned long rs_mark_time[DRBD_SYNC_MARKS];
1069 /* current index into rs_mark_{left,time} */
1070 int rs_last_mark;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001071
1072 /* where does the admin want us to start? (sector) */
1073 sector_t ov_start_sector;
1074 /* where are we now? (sector) */
1075 sector_t ov_position;
1076 /* Start sector of out of sync range (to merge printk reporting). */
1077 sector_t ov_last_oos_start;
1078 /* size of out-of-sync range in sectors. */
1079 sector_t ov_last_oos_size;
1080 unsigned long ov_left; /* in bits */
1081 struct crypto_hash *csums_tfm;
1082 struct crypto_hash *verify_tfm;
1083
Philipp Reisnerb411b362009-09-25 16:07:19 -07001084 struct drbd_bitmap *bitmap;
1085 unsigned long bm_resync_fo; /* bit offset for drbd_bm_find_next */
1086
1087 /* Used to track operations of resync... */
1088 struct lru_cache *resync;
1089 /* Number of locked elements in resync LRU */
1090 unsigned int resync_locked;
1091 /* resync extent number waiting for application requests */
1092 unsigned int resync_wenr;
1093
1094 int open_cnt;
1095 u64 *p_uuid;
1096 struct drbd_epoch *current_epoch;
1097 spinlock_t epoch_lock;
1098 unsigned int epochs;
1099 enum write_ordering_e write_ordering;
Philipp Reisner85719572010-07-21 10:20:17 +02001100 struct list_head active_ee; /* IO in progress (P_DATA gets written to disk) */
1101 struct list_head sync_ee; /* IO in progress (P_RS_DATA_REPLY gets written to disk) */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001102 struct list_head done_ee; /* send ack */
Philipp Reisner85719572010-07-21 10:20:17 +02001103 struct list_head read_ee; /* IO in progress (any read) */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001104 struct list_head net_ee; /* zero-copy network send in progress */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001105
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001106 /* Interval tree of pending remote write requests (struct drbd_epoch_entry) */
1107 struct rb_root epoch_entries;
1108
Philipp Reisnerb411b362009-09-25 16:07:19 -07001109 /* this one is protected by ee_lock, single thread */
1110 struct drbd_epoch_entry *last_write_w_barrier;
1111
1112 int next_barrier_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001113 struct list_head resync_reads;
Lars Ellenberg435f0742010-09-06 12:30:25 +02001114 atomic_t pp_in_use; /* allocated from page pool */
1115 atomic_t pp_in_use_by_net; /* sendpage()d, still referenced by tcp */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116 wait_queue_head_t ee_wait;
1117 struct page *md_io_page; /* one page buffer for md_io */
1118 struct page *md_io_tmpp; /* for logical_block_size != 512 */
1119 struct mutex md_io_mutex; /* protects the md_io_buffer */
1120 spinlock_t al_lock;
1121 wait_queue_head_t al_wait;
1122 struct lru_cache *act_log; /* activity log */
1123 unsigned int al_tr_number;
1124 int al_tr_cycle;
1125 int al_tr_pos; /* position of the next transaction in the journal */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001126 wait_queue_head_t seq_wait;
1127 atomic_t packet_seq;
1128 unsigned int peer_seq;
1129 spinlock_t peer_seq_lock;
1130 unsigned int minor;
1131 unsigned long comm_bm_set; /* communicated number of set bits. */
1132 cpumask_var_t cpu_mask;
1133 struct bm_io_work bm_io_work;
1134 u64 ed_uuid; /* UUID of the exposed data */
1135 struct mutex state_mutex;
1136 char congestion_reason; /* Why we where congested... */
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001137 atomic_t rs_sect_in; /* for incoming resync data rate, SyncTarget */
1138 atomic_t rs_sect_ev; /* for submitted resync data rate, both */
1139 int rs_last_sect_ev; /* counter to compare with */
1140 int rs_last_events; /* counter of read or write "events" (unit sectors)
1141 * on the lower level device when we last looked. */
1142 int c_sync_rate; /* current resync rate after syncer throttle magic */
Philipp Reisner778f2712010-07-06 11:14:00 +02001143 struct fifo_buffer rs_plan_s; /* correction values of resync planer */
1144 int rs_in_flight; /* resync sectors in flight (to proxy, in proxy and from proxy) */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001145 int rs_planed; /* resync sectors already planned */
Philipp Reisner759fbdf2010-10-26 16:02:27 +02001146 atomic_t ap_in_flight; /* App sectors in flight (waiting for ack) */
Philipp Reisner99432fc2011-05-20 16:39:13 +02001147 int peer_max_bio_size;
1148 int local_max_bio_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001149};
1150
1151static inline struct drbd_conf *minor_to_mdev(unsigned int minor)
1152{
1153 struct drbd_conf *mdev;
1154
1155 mdev = minor < minor_count ? minor_table[minor] : NULL;
1156
1157 return mdev;
1158}
1159
1160static inline unsigned int mdev_to_minor(struct drbd_conf *mdev)
1161{
1162 return mdev->minor;
1163}
1164
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001165/* returns 1 if it was successful,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166 * returns 0 if there was no data socket.
1167 * so wherever you are going to use the data.socket, e.g. do
1168 * if (!drbd_get_data_sock(mdev))
1169 * return 0;
1170 * CODE();
1171 * drbd_put_data_sock(mdev);
1172 */
1173static inline int drbd_get_data_sock(struct drbd_conf *mdev)
1174{
Philipp Reisnere42325a2011-01-19 13:55:45 +01001175 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001176 /* drbd_disconnect() could have called drbd_free_sock()
1177 * while we were waiting in down()... */
Philipp Reisnere42325a2011-01-19 13:55:45 +01001178 if (unlikely(mdev->tconn->data.socket == NULL)) {
1179 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180 return 0;
1181 }
1182 return 1;
1183}
1184
1185static inline void drbd_put_data_sock(struct drbd_conf *mdev)
1186{
Philipp Reisnere42325a2011-01-19 13:55:45 +01001187 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001188}
1189
1190/*
1191 * function declarations
1192 *************************/
1193
1194/* drbd_main.c */
1195
1196enum chg_state_flags {
1197 CS_HARD = 1,
1198 CS_VERBOSE = 2,
1199 CS_WAIT_COMPLETE = 4,
1200 CS_SERIALIZE = 8,
1201 CS_ORDERED = CS_WAIT_COMPLETE + CS_SERIALIZE,
1202};
1203
Philipp Reisnere89b5912010-03-24 17:11:33 +01001204enum dds_flags {
1205 DDSF_FORCED = 1,
1206 DDSF_NO_RESYNC = 2, /* Do not run a resync for the new space */
1207};
1208
Philipp Reisnerb411b362009-09-25 16:07:19 -07001209extern void drbd_init_set_defaults(struct drbd_conf *mdev);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001210extern enum drbd_state_rv drbd_change_state(struct drbd_conf *mdev,
1211 enum chg_state_flags f,
1212 union drbd_state mask,
1213 union drbd_state val);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001214extern void drbd_force_state(struct drbd_conf *, union drbd_state,
1215 union drbd_state);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001216extern enum drbd_state_rv _drbd_request_state(struct drbd_conf *,
1217 union drbd_state,
1218 union drbd_state,
1219 enum chg_state_flags);
1220extern enum drbd_state_rv __drbd_set_state(struct drbd_conf *, union drbd_state,
1221 enum chg_state_flags,
1222 struct completion *done);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001223extern void print_st_err(struct drbd_conf *, union drbd_state,
1224 union drbd_state, int);
1225extern int drbd_thread_start(struct drbd_thread *thi);
1226extern void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait);
1227#ifdef CONFIG_SMP
1228extern void drbd_thread_current_set_cpu(struct drbd_conf *mdev);
1229extern void drbd_calc_cpu_mask(struct drbd_conf *mdev);
1230#else
1231#define drbd_thread_current_set_cpu(A) ({})
1232#define drbd_calc_cpu_mask(A) ({})
1233#endif
1234extern void drbd_free_resources(struct drbd_conf *mdev);
1235extern void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
1236 unsigned int set_size);
1237extern void tl_clear(struct drbd_conf *mdev);
1238extern void _tl_add_barrier(struct drbd_conf *, struct drbd_tl_epoch *);
1239extern void drbd_free_sock(struct drbd_conf *mdev);
1240extern int drbd_send(struct drbd_conf *mdev, struct socket *sock,
1241 void *buf, size_t size, unsigned msg_flags);
1242extern int drbd_send_protocol(struct drbd_conf *mdev);
1243extern int drbd_send_uuids(struct drbd_conf *mdev);
1244extern int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev);
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001245extern int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev);
Philipp Reisnere89b5912010-03-24 17:11:33 +01001246extern int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001247extern int _drbd_send_state(struct drbd_conf *mdev);
1248extern int drbd_send_state(struct drbd_conf *mdev);
1249extern int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
Philipp Reisnerc0129492011-01-19 16:58:16 +01001250 enum drbd_packets cmd, struct p_header *h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251 size_t size, unsigned msg_flags);
1252#define USE_DATA_SOCKET 1
1253#define USE_META_SOCKET 0
1254extern int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
Philipp Reisnerc0129492011-01-19 16:58:16 +01001255 enum drbd_packets cmd, struct p_header *h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256 size_t size);
1257extern int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packets cmd,
1258 char *data, size_t size);
1259extern int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc);
1260extern int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr,
1261 u32 set_size);
1262extern int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packets cmd,
1263 struct drbd_epoch_entry *e);
1264extern int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packets cmd,
1265 struct p_block_req *rp);
1266extern int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packets cmd,
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001267 struct p_data *dp, int data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268extern int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packets cmd,
1269 sector_t sector, int blksize, u64 block_id);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001270extern int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001271extern int drbd_send_block(struct drbd_conf *mdev, enum drbd_packets cmd,
1272 struct drbd_epoch_entry *e);
1273extern int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274extern int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1275 sector_t sector, int size, u64 block_id);
1276extern int drbd_send_drequest_csum(struct drbd_conf *mdev,
1277 sector_t sector,int size,
1278 void *digest, int digest_size,
1279 enum drbd_packets cmd);
1280extern int drbd_send_ov_request(struct drbd_conf *mdev,sector_t sector,int size);
1281
1282extern int drbd_send_bitmap(struct drbd_conf *mdev);
1283extern int _drbd_send_bitmap(struct drbd_conf *mdev);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001284extern int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285extern void drbd_free_bc(struct drbd_backing_dev *ldev);
1286extern void drbd_mdev_cleanup(struct drbd_conf *mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01001287void drbd_print_uuids(struct drbd_conf *mdev, const char *text);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001288
Philipp Reisnerb411b362009-09-25 16:07:19 -07001289extern void drbd_md_sync(struct drbd_conf *mdev);
1290extern int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001291extern void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1292extern void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1293extern void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1294extern void _drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1295extern void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local);
1296extern void drbd_md_set_flag(struct drbd_conf *mdev, int flags) __must_hold(local);
1297extern void drbd_md_clear_flag(struct drbd_conf *mdev, int flags)__must_hold(local);
1298extern int drbd_md_test_flag(struct drbd_backing_dev *, int);
Lars Ellenbergee15b032010-09-03 10:00:09 +02001299#ifndef DRBD_DEBUG_MD_SYNC
Philipp Reisnerb411b362009-09-25 16:07:19 -07001300extern void drbd_md_mark_dirty(struct drbd_conf *mdev);
Lars Ellenbergee15b032010-09-03 10:00:09 +02001301#else
1302#define drbd_md_mark_dirty(m) drbd_md_mark_dirty_(m, __LINE__ , __func__ )
1303extern void drbd_md_mark_dirty_(struct drbd_conf *mdev,
1304 unsigned int line, const char *func);
1305#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001306extern void drbd_queue_bitmap_io(struct drbd_conf *mdev,
1307 int (*io_fn)(struct drbd_conf *),
1308 void (*done)(struct drbd_conf *, int),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001309 char *why, enum bm_flag flags);
1310extern int drbd_bitmap_io(struct drbd_conf *mdev,
1311 int (*io_fn)(struct drbd_conf *),
1312 char *why, enum bm_flag flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313extern int drbd_bmio_set_n_write(struct drbd_conf *mdev);
1314extern int drbd_bmio_clear_n_write(struct drbd_conf *mdev);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001315extern void drbd_go_diskless(struct drbd_conf *mdev);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001316extern void drbd_ldev_destroy(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001317
1318
1319/* Meta data layout
1320 We reserve a 128MB Block (4k aligned)
1321 * either at the end of the backing device
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001322 * or on a separate meta data device. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001323
1324#define MD_RESERVED_SECT (128LU << 11) /* 128 MB, unit sectors */
1325/* The following numbers are sectors */
1326#define MD_AL_OFFSET 8 /* 8 Sectors after start of meta area */
1327#define MD_AL_MAX_SIZE 64 /* = 32 kb LOG ~ 3776 extents ~ 14 GB Storage */
1328/* Allows up to about 3.8TB */
1329#define MD_BM_OFFSET (MD_AL_OFFSET + MD_AL_MAX_SIZE)
1330
1331/* Since the smalles IO unit is usually 512 byte */
1332#define MD_SECTOR_SHIFT 9
1333#define MD_SECTOR_SIZE (1<<MD_SECTOR_SHIFT)
1334
1335/* activity log */
1336#define AL_EXTENTS_PT ((MD_SECTOR_SIZE-12)/8-1) /* 61 ; Extents per 512B sector */
1337#define AL_EXTENT_SHIFT 22 /* One extent represents 4M Storage */
1338#define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
1339
1340#if BITS_PER_LONG == 32
1341#define LN2_BPL 5
1342#define cpu_to_lel(A) cpu_to_le32(A)
1343#define lel_to_cpu(A) le32_to_cpu(A)
1344#elif BITS_PER_LONG == 64
1345#define LN2_BPL 6
1346#define cpu_to_lel(A) cpu_to_le64(A)
1347#define lel_to_cpu(A) le64_to_cpu(A)
1348#else
1349#error "LN2 of BITS_PER_LONG unknown!"
1350#endif
1351
1352/* resync bitmap */
1353/* 16MB sized 'bitmap extent' to track syncer usage */
1354struct bm_extent {
1355 int rs_left; /* number of bits set (out of sync) in this extent. */
1356 int rs_failed; /* number of failed resync requests in this extent. */
1357 unsigned long flags;
1358 struct lc_element lce;
1359};
1360
1361#define BME_NO_WRITES 0 /* bm_extent.flags: no more requests on this one! */
1362#define BME_LOCKED 1 /* bm_extent.flags: syncer active on this one. */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001363#define BME_PRIORITY 2 /* finish resync IO on this extent ASAP! App IO waiting! */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364
1365/* drbd_bitmap.c */
1366/*
1367 * We need to store one bit for a block.
1368 * Example: 1GB disk @ 4096 byte blocks ==> we need 32 KB bitmap.
1369 * Bit 0 ==> local node thinks this block is binary identical on both nodes
1370 * Bit 1 ==> local node thinks this block needs to be synced.
1371 */
1372
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001373#define SLEEP_TIME (HZ/10)
1374
Philipp Reisnerb411b362009-09-25 16:07:19 -07001375#define BM_BLOCK_SHIFT 12 /* 4k per bit */
1376#define BM_BLOCK_SIZE (1<<BM_BLOCK_SHIFT)
1377/* (9+3) : 512 bytes @ 8 bits; representing 16M storage
1378 * per sector of on disk bitmap */
1379#define BM_EXT_SHIFT (BM_BLOCK_SHIFT + MD_SECTOR_SHIFT + 3) /* = 24 */
1380#define BM_EXT_SIZE (1<<BM_EXT_SHIFT)
1381
1382#if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
1383#error "HAVE YOU FIXED drbdmeta AS WELL??"
1384#endif
1385
1386/* thus many _storage_ sectors are described by one bit */
1387#define BM_SECT_TO_BIT(x) ((x)>>(BM_BLOCK_SHIFT-9))
1388#define BM_BIT_TO_SECT(x) ((sector_t)(x)<<(BM_BLOCK_SHIFT-9))
1389#define BM_SECT_PER_BIT BM_BIT_TO_SECT(1)
1390
1391/* bit to represented kilo byte conversion */
1392#define Bit2KB(bits) ((bits)<<(BM_BLOCK_SHIFT-10))
1393
1394/* in which _bitmap_ extent (resp. sector) the bit for a certain
1395 * _storage_ sector is located in */
1396#define BM_SECT_TO_EXT(x) ((x)>>(BM_EXT_SHIFT-9))
1397
1398/* how much _storage_ sectors we have per bitmap sector */
1399#define BM_EXT_TO_SECT(x) ((sector_t)(x) << (BM_EXT_SHIFT-9))
1400#define BM_SECT_PER_EXT BM_EXT_TO_SECT(1)
1401
1402/* in one sector of the bitmap, we have this many activity_log extents. */
1403#define AL_EXT_PER_BM_SECT (1 << (BM_EXT_SHIFT - AL_EXTENT_SHIFT))
1404#define BM_WORDS_PER_AL_EXT (1 << (AL_EXTENT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
1405
1406#define BM_BLOCKS_PER_BM_EXT_B (BM_EXT_SHIFT - BM_BLOCK_SHIFT)
1407#define BM_BLOCKS_PER_BM_EXT_MASK ((1<<BM_BLOCKS_PER_BM_EXT_B) - 1)
1408
1409/* the extent in "PER_EXTENT" below is an activity log extent
1410 * we need that many (long words/bytes) to store the bitmap
1411 * of one AL_EXTENT_SIZE chunk of storage.
1412 * we can store the bitmap for that many AL_EXTENTS within
1413 * one sector of the _on_disk_ bitmap:
1414 * bit 0 bit 37 bit 38 bit (512*8)-1
1415 * ...|........|........|.. // ..|........|
1416 * sect. 0 `296 `304 ^(512*8*8)-1
1417 *
1418#define BM_WORDS_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / BITS_PER_LONG )
1419#define BM_BYTES_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / 8 ) // 128
1420#define BM_EXT_PER_SECT ( 512 / BM_BYTES_PER_EXTENT ) // 4
1421 */
1422
1423#define DRBD_MAX_SECTORS_32 (0xffffffffLU)
1424#define DRBD_MAX_SECTORS_BM \
1425 ((MD_RESERVED_SECT - MD_BM_OFFSET) * (1LL<<(BM_EXT_SHIFT-9)))
1426#if DRBD_MAX_SECTORS_BM < DRBD_MAX_SECTORS_32
1427#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1428#define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_BM
Lars Ellenberg36bfc7e2010-01-05 19:33:54 +01001429#elif !defined(CONFIG_LBDAF) && BITS_PER_LONG == 32
Philipp Reisnerb411b362009-09-25 16:07:19 -07001430#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_32
1431#define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_32
1432#else
1433#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1434/* 16 TB in units of sectors */
1435#if BITS_PER_LONG == 32
1436/* adjust by one page worth of bitmap,
1437 * so we won't wrap around in drbd_bm_find_next_bit.
1438 * you should use 64bit OS for that much storage, anyways. */
1439#define DRBD_MAX_SECTORS_FLEX BM_BIT_TO_SECT(0xffff7fff)
1440#else
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001441/* we allow up to 1 PiB now on 64bit architecture with "flexible" meta data */
1442#define DRBD_MAX_SECTORS_FLEX (1UL << 51)
1443/* corresponds to (1UL << 38) bits right now. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001444#endif
1445#endif
1446
Philipp Reisnerd5373382010-08-23 15:18:33 +02001447#define HT_SHIFT 8
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001448#define DRBD_MAX_BIO_SIZE (1U<<(9+HT_SHIFT))
Philipp Reisner99432fc2011-05-20 16:39:13 +02001449#define DRBD_MAX_BIO_SIZE_SAFE (1 << 12) /* Works always = 4k */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001450
Philipp Reisnerd5373382010-08-23 15:18:33 +02001451#define DRBD_MAX_SIZE_H80_PACKET (1 << 15) /* The old header only allows packets up to 32Kib data */
1452
Philipp Reisnerb411b362009-09-25 16:07:19 -07001453extern int drbd_bm_init(struct drbd_conf *mdev);
Philipp Reisner02d9a942010-03-24 16:23:03 +01001454extern int drbd_bm_resize(struct drbd_conf *mdev, sector_t sectors, int set_new_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001455extern void drbd_bm_cleanup(struct drbd_conf *mdev);
1456extern void drbd_bm_set_all(struct drbd_conf *mdev);
1457extern void drbd_bm_clear_all(struct drbd_conf *mdev);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001458/* set/clear/test only a few bits at a time */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459extern int drbd_bm_set_bits(
1460 struct drbd_conf *mdev, unsigned long s, unsigned long e);
1461extern int drbd_bm_clear_bits(
1462 struct drbd_conf *mdev, unsigned long s, unsigned long e);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001463extern int drbd_bm_count_bits(
1464 struct drbd_conf *mdev, const unsigned long s, const unsigned long e);
1465/* bm_set_bits variant for use while holding drbd_bm_lock,
1466 * may process the whole bitmap in one go */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001467extern void _drbd_bm_set_bits(struct drbd_conf *mdev,
1468 const unsigned long s, const unsigned long e);
1469extern int drbd_bm_test_bit(struct drbd_conf *mdev, unsigned long bitnr);
1470extern int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001471extern int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001472extern int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local);
1473extern int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local);
1474extern unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev,
1475 unsigned long al_enr);
1476extern size_t drbd_bm_words(struct drbd_conf *mdev);
1477extern unsigned long drbd_bm_bits(struct drbd_conf *mdev);
1478extern sector_t drbd_bm_capacity(struct drbd_conf *mdev);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001479
1480#define DRBD_END_OF_BITMAP (~(unsigned long)0)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001481extern unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1482/* bm_find_next variants for use while you hold drbd_bm_lock() */
1483extern unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1484extern unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo);
Philipp Reisner07782862010-08-31 12:00:50 +02001485extern unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001486extern unsigned long drbd_bm_total_weight(struct drbd_conf *mdev);
1487extern int drbd_bm_rs_done(struct drbd_conf *mdev);
1488/* for receive_bitmap */
1489extern void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset,
1490 size_t number, unsigned long *buffer);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001491/* for _drbd_send_bitmap */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001492extern void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset,
1493 size_t number, unsigned long *buffer);
1494
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001495extern void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001496extern void drbd_bm_unlock(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497/* drbd_main.c */
1498
1499extern struct kmem_cache *drbd_request_cache;
1500extern struct kmem_cache *drbd_ee_cache; /* epoch entries */
1501extern struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
1502extern struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
1503extern mempool_t *drbd_request_mempool;
1504extern mempool_t *drbd_ee_mempool;
1505
1506extern struct page *drbd_pp_pool; /* drbd's page pool */
1507extern spinlock_t drbd_pp_lock;
1508extern int drbd_pp_vacant;
1509extern wait_queue_head_t drbd_pp_wait;
1510
1511extern rwlock_t global_state_lock;
1512
1513extern struct drbd_conf *drbd_new_device(unsigned int minor);
1514extern void drbd_free_mdev(struct drbd_conf *mdev);
1515
Philipp Reisner21114382011-01-19 12:26:59 +01001516struct drbd_tconn *drbd_new_tconn(char *name);
1517extern void drbd_free_tconn(struct drbd_tconn *tconn);
1518
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519extern int proc_details;
1520
1521/* drbd_req */
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01001522extern int drbd_make_request(struct request_queue *q, struct bio *bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001523extern int drbd_read_remote(struct drbd_conf *mdev, struct drbd_request *req);
1524extern int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec);
1525extern int is_valid_ar_handle(struct drbd_request *, sector_t);
1526
1527
1528/* drbd_nl.c */
1529extern void drbd_suspend_io(struct drbd_conf *mdev);
1530extern void drbd_resume_io(struct drbd_conf *mdev);
1531extern char *ppsize(char *buf, unsigned long long size);
Philipp Reisnera393db62009-12-22 13:35:52 +01001532extern sector_t drbd_new_dev_size(struct drbd_conf *, struct drbd_backing_dev *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001533enum determine_dev_size { dev_size_error = -1, unchanged = 0, shrunk = 1, grew = 2 };
Bart Van Assche24c48302011-05-21 18:32:29 +02001534extern enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *, enum dds_flags) __must_hold(local);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535extern void resync_after_online_grow(struct drbd_conf *);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001536extern void drbd_reconsider_max_bio_size(struct drbd_conf *mdev);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001537extern enum drbd_state_rv drbd_set_role(struct drbd_conf *mdev,
1538 enum drbd_role new_role,
1539 int force);
Philipp Reisner87f7be42010-06-11 13:56:33 +02001540extern enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev);
1541extern void drbd_try_outdate_peer_async(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542extern int drbd_khelper(struct drbd_conf *mdev, char *cmd);
1543
1544/* drbd_worker.c */
1545extern int drbd_worker(struct drbd_thread *thi);
1546extern int drbd_alter_sa(struct drbd_conf *mdev, int na);
1547extern void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side);
1548extern void resume_next_sg(struct drbd_conf *mdev);
1549extern void suspend_other_sg(struct drbd_conf *mdev);
1550extern int drbd_resync_finished(struct drbd_conf *mdev);
1551/* maybe rather drbd_main.c ? */
1552extern int drbd_md_sync_page_io(struct drbd_conf *mdev,
1553 struct drbd_backing_dev *bdev, sector_t sector, int rw);
1554extern void drbd_ov_oos_found(struct drbd_conf*, sector_t, int);
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001555extern void drbd_rs_controller_reset(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556
1557static inline void ov_oos_print(struct drbd_conf *mdev)
1558{
1559 if (mdev->ov_last_oos_size) {
1560 dev_err(DEV, "Out of sync: start=%llu, size=%lu (sectors)\n",
1561 (unsigned long long)mdev->ov_last_oos_start,
1562 (unsigned long)mdev->ov_last_oos_size);
1563 }
1564 mdev->ov_last_oos_size=0;
1565}
1566
1567
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001568extern void drbd_csum_bio(struct drbd_conf *, struct crypto_hash *, struct bio *, void *);
1569extern void drbd_csum_ee(struct drbd_conf *, struct crypto_hash *, struct drbd_epoch_entry *, void *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570/* worker callbacks */
1571extern int w_req_cancel_conflict(struct drbd_conf *, struct drbd_work *, int);
1572extern int w_read_retry_remote(struct drbd_conf *, struct drbd_work *, int);
1573extern int w_e_end_data_req(struct drbd_conf *, struct drbd_work *, int);
1574extern int w_e_end_rsdata_req(struct drbd_conf *, struct drbd_work *, int);
1575extern int w_e_end_csum_rs_req(struct drbd_conf *, struct drbd_work *, int);
1576extern int w_e_end_ov_reply(struct drbd_conf *, struct drbd_work *, int);
1577extern int w_e_end_ov_req(struct drbd_conf *, struct drbd_work *, int);
1578extern int w_ov_finished(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisner794abb72010-12-27 11:51:23 +01001579extern int w_resync_timer(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001580extern int w_resume_next_sg(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001581extern int w_send_write_hint(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001582extern int w_send_dblock(struct drbd_conf *, struct drbd_work *, int);
1583extern int w_send_barrier(struct drbd_conf *, struct drbd_work *, int);
1584extern int w_send_read_req(struct drbd_conf *, struct drbd_work *, int);
1585extern int w_prev_work_done(struct drbd_conf *, struct drbd_work *, int);
1586extern int w_e_reissue(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisner265be2d2010-05-31 10:14:17 +02001587extern int w_restart_disk_io(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001588extern int w_send_oos(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001589extern int w_start_resync(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001590
1591extern void resync_timer_fn(unsigned long data);
Philipp Reisner370a43e2011-01-14 16:03:11 +01001592extern void start_resync_timer_fn(unsigned long data);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593
1594/* drbd_receiver.c */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001595extern int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001596extern int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
1597 const unsigned rw, const int fault_type);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001598extern int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list);
1599extern struct drbd_epoch_entry *drbd_alloc_ee(struct drbd_conf *mdev,
1600 u64 id,
1601 sector_t sector,
1602 unsigned int data_size,
1603 gfp_t gfp_mask) __must_hold(local);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001604extern void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
1605 int is_net);
1606#define drbd_free_ee(m,e) drbd_free_some_ee(m, e, 0)
1607#define drbd_free_net_ee(m,e) drbd_free_some_ee(m, e, 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001608extern void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1609 struct list_head *head);
1610extern void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1611 struct list_head *head);
1612extern void drbd_set_recv_tcq(struct drbd_conf *mdev, int tcq_enabled);
1613extern void _drbd_clear_done_ee(struct drbd_conf *mdev, struct list_head *to_be_freed);
Philipp Reisner191d3cc2011-01-19 14:53:22 +01001614extern void drbd_flush_workqueue(struct drbd_tconn *tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001615
1616/* yes, there is kernel_setsockopt, but only since 2.6.18. we don't need to
1617 * mess with get_fs/set_fs, we know we are KERNEL_DS always. */
1618static inline int drbd_setsockopt(struct socket *sock, int level, int optname,
1619 char __user *optval, int optlen)
1620{
1621 int err;
1622 if (level == SOL_SOCKET)
1623 err = sock_setsockopt(sock, level, optname, optval, optlen);
1624 else
1625 err = sock->ops->setsockopt(sock, level, optname, optval,
1626 optlen);
1627 return err;
1628}
1629
1630static inline void drbd_tcp_cork(struct socket *sock)
1631{
1632 int __user val = 1;
1633 (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1634 (char __user *)&val, sizeof(val));
1635}
1636
1637static inline void drbd_tcp_uncork(struct socket *sock)
1638{
1639 int __user val = 0;
1640 (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1641 (char __user *)&val, sizeof(val));
1642}
1643
1644static inline void drbd_tcp_nodelay(struct socket *sock)
1645{
1646 int __user val = 1;
1647 (void) drbd_setsockopt(sock, SOL_TCP, TCP_NODELAY,
1648 (char __user *)&val, sizeof(val));
1649}
1650
1651static inline void drbd_tcp_quickack(struct socket *sock)
1652{
Lars Ellenberg344fa462010-05-25 14:23:57 +02001653 int __user val = 2;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001654 (void) drbd_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
1655 (char __user *)&val, sizeof(val));
1656}
1657
1658void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo);
1659
1660/* drbd_proc.c */
1661extern struct proc_dir_entry *drbd_proc;
Emese Revfy7d4e9d02009-12-14 00:59:30 +01001662extern const struct file_operations drbd_proc_fops;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001663extern const char *drbd_conn_str(enum drbd_conns s);
1664extern const char *drbd_role_str(enum drbd_role s);
1665
1666/* drbd_actlog.c */
1667extern void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector);
1668extern void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector);
1669extern void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector);
1670extern int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1671extern int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1672extern void drbd_rs_cancel_all(struct drbd_conf *mdev);
1673extern int drbd_rs_del_all(struct drbd_conf *mdev);
1674extern void drbd_rs_failed_io(struct drbd_conf *mdev,
1675 sector_t sector, int size);
1676extern int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *);
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001677extern void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001678extern void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector,
1679 int size, const char *file, const unsigned int line);
1680#define drbd_set_in_sync(mdev, sector, size) \
1681 __drbd_set_in_sync(mdev, sector, size, __FILE__, __LINE__)
Philipp Reisner73a01a12010-10-27 14:33:00 +02001682extern int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001683 int size, const char *file, const unsigned int line);
1684#define drbd_set_out_of_sync(mdev, sector, size) \
1685 __drbd_set_out_of_sync(mdev, sector, size, __FILE__, __LINE__)
1686extern void drbd_al_apply_to_bm(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001687extern void drbd_al_shrink(struct drbd_conf *mdev);
1688
1689
1690/* drbd_nl.c */
1691
1692void drbd_nl_cleanup(void);
1693int __init drbd_nl_init(void);
1694void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state);
1695void drbd_bcast_sync_progress(struct drbd_conf *mdev);
1696void drbd_bcast_ee(struct drbd_conf *mdev,
1697 const char *reason, const int dgs,
1698 const char* seen_hash, const char* calc_hash,
1699 const struct drbd_epoch_entry* e);
1700
1701
1702/**
1703 * DOC: DRBD State macros
1704 *
1705 * These macros are used to express state changes in easily readable form.
1706 *
1707 * The NS macros expand to a mask and a value, that can be bit ored onto the
1708 * current state as soon as the spinlock (req_lock) was taken.
1709 *
1710 * The _NS macros are used for state functions that get called with the
1711 * spinlock. These macros expand directly to the new state value.
1712 *
1713 * Besides the basic forms NS() and _NS() additional _?NS[23] are defined
1714 * to express state changes that affect more than one aspect of the state.
1715 *
1716 * E.g. NS2(conn, C_CONNECTED, peer, R_SECONDARY)
1717 * Means that the network connection was established and that the peer
1718 * is in secondary role.
1719 */
1720#define role_MASK R_MASK
1721#define peer_MASK R_MASK
1722#define disk_MASK D_MASK
1723#define pdsk_MASK D_MASK
1724#define conn_MASK C_MASK
1725#define susp_MASK 1
1726#define user_isp_MASK 1
1727#define aftr_isp_MASK 1
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001728#define susp_nod_MASK 1
1729#define susp_fen_MASK 1
Philipp Reisnerb411b362009-09-25 16:07:19 -07001730
1731#define NS(T, S) \
1732 ({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
1733 ({ union drbd_state val; val.i = 0; val.T = (S); val; })
1734#define NS2(T1, S1, T2, S2) \
1735 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1736 mask.T2 = T2##_MASK; mask; }), \
1737 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1738 val.T2 = (S2); val; })
1739#define NS3(T1, S1, T2, S2, T3, S3) \
1740 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1741 mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
1742 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1743 val.T2 = (S2); val.T3 = (S3); val; })
1744
1745#define _NS(D, T, S) \
1746 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T = (S); __ns; })
1747#define _NS2(D, T1, S1, T2, S2) \
1748 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1749 __ns.T2 = (S2); __ns; })
1750#define _NS3(D, T1, S1, T2, S2, T3, S3) \
1751 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1752 __ns.T2 = (S2); __ns.T3 = (S3); __ns; })
1753
1754/*
1755 * inline helper functions
1756 *************************/
1757
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001758/* see also page_chain_add and friends in drbd_receiver.c */
1759static inline struct page *page_chain_next(struct page *page)
1760{
1761 return (struct page *)page_private(page);
1762}
1763#define page_chain_for_each(page) \
1764 for (; page && ({ prefetch(page_chain_next(page)); 1; }); \
1765 page = page_chain_next(page))
1766#define page_chain_for_each_safe(page, n) \
1767 for (; page && ({ n = page_chain_next(page); 1; }); page = n)
1768
1769static inline int drbd_bio_has_active_page(struct bio *bio)
1770{
1771 struct bio_vec *bvec;
1772 int i;
1773
1774 __bio_for_each_segment(bvec, bio, i, 0) {
1775 if (page_count(bvec->bv_page) > 1)
1776 return 1;
1777 }
1778
1779 return 0;
1780}
1781
1782static inline int drbd_ee_has_active_page(struct drbd_epoch_entry *e)
1783{
1784 struct page *page = e->pages;
1785 page_chain_for_each(page) {
1786 if (page_count(page) > 1)
1787 return 1;
1788 }
1789 return 0;
1790}
1791
1792
Philipp Reisnerb411b362009-09-25 16:07:19 -07001793static inline void drbd_state_lock(struct drbd_conf *mdev)
1794{
1795 wait_event(mdev->misc_wait,
1796 !test_and_set_bit(CLUSTER_ST_CHANGE, &mdev->flags));
1797}
1798
1799static inline void drbd_state_unlock(struct drbd_conf *mdev)
1800{
1801 clear_bit(CLUSTER_ST_CHANGE, &mdev->flags);
1802 wake_up(&mdev->misc_wait);
1803}
1804
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001805static inline enum drbd_state_rv
1806_drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
1807 enum chg_state_flags flags, struct completion *done)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001808{
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001809 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001810
1811 read_lock(&global_state_lock);
1812 rv = __drbd_set_state(mdev, ns, flags, done);
1813 read_unlock(&global_state_lock);
1814
1815 return rv;
1816}
1817
1818/**
1819 * drbd_request_state() - Reqest a state change
1820 * @mdev: DRBD device.
1821 * @mask: mask of state bits to change.
1822 * @val: value of new state bits.
1823 *
1824 * This is the most graceful way of requesting a state change. It is verbose
1825 * quite verbose in case the state change is not possible, and all those
1826 * state changes are globally serialized.
1827 */
1828static inline int drbd_request_state(struct drbd_conf *mdev,
1829 union drbd_state mask,
1830 union drbd_state val)
1831{
1832 return _drbd_request_state(mdev, mask, val, CS_VERBOSE + CS_ORDERED);
1833}
1834
1835#define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
1836static inline void __drbd_chk_io_error_(struct drbd_conf *mdev, int forcedetach, const char *where)
1837{
1838 switch (mdev->ldev->dc.on_io_error) {
1839 case EP_PASS_ON:
1840 if (!forcedetach) {
Lars Ellenberg73835062010-05-27 11:51:56 +02001841 if (__ratelimit(&drbd_ratelimit_state))
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001842 dev_err(DEV, "Local IO failed in %s.\n", where);
Philipp Reisnerd2e17802011-03-14 11:54:47 +01001843 if (mdev->state.disk > D_INCONSISTENT)
1844 _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_HARD, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001845 break;
1846 }
1847 /* NOTE fall through to detach case if forcedetach set */
1848 case EP_DETACH:
1849 case EP_CALL_HELPER:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001850 set_bit(WAS_IO_ERROR, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851 if (mdev->state.disk > D_FAILED) {
1852 _drbd_set_state(_NS(mdev, disk, D_FAILED), CS_HARD, NULL);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001853 dev_err(DEV,
1854 "Local IO failed in %s. Detaching...\n", where);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001855 }
1856 break;
1857 }
1858}
1859
1860/**
1861 * drbd_chk_io_error: Handle the on_io_error setting, should be called from all io completion handlers
1862 * @mdev: DRBD device.
1863 * @error: Error code passed to the IO completion callback
1864 * @forcedetach: Force detach. I.e. the error happened while accessing the meta data
1865 *
1866 * See also drbd_main.c:after_state_ch() if (os.disk > D_FAILED && ns.disk == D_FAILED)
1867 */
1868#define drbd_chk_io_error(m,e,f) drbd_chk_io_error_(m,e,f, __func__)
1869static inline void drbd_chk_io_error_(struct drbd_conf *mdev,
1870 int error, int forcedetach, const char *where)
1871{
1872 if (error) {
1873 unsigned long flags;
Philipp Reisner87eeee42011-01-19 14:16:30 +01001874 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001875 __drbd_chk_io_error_(mdev, forcedetach, where);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001876 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001877 }
1878}
1879
1880
1881/**
1882 * drbd_md_first_sector() - Returns the first sector number of the meta data area
1883 * @bdev: Meta data block device.
1884 *
1885 * BTW, for internal meta data, this happens to be the maximum capacity
1886 * we could agree upon with our peer node.
1887 */
1888static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
1889{
1890 switch (bdev->dc.meta_dev_idx) {
1891 case DRBD_MD_INDEX_INTERNAL:
1892 case DRBD_MD_INDEX_FLEX_INT:
1893 return bdev->md.md_offset + bdev->md.bm_offset;
1894 case DRBD_MD_INDEX_FLEX_EXT:
1895 default:
1896 return bdev->md.md_offset;
1897 }
1898}
1899
1900/**
1901 * drbd_md_last_sector() - Return the last sector number of the meta data area
1902 * @bdev: Meta data block device.
1903 */
1904static inline sector_t drbd_md_last_sector(struct drbd_backing_dev *bdev)
1905{
1906 switch (bdev->dc.meta_dev_idx) {
1907 case DRBD_MD_INDEX_INTERNAL:
1908 case DRBD_MD_INDEX_FLEX_INT:
1909 return bdev->md.md_offset + MD_AL_OFFSET - 1;
1910 case DRBD_MD_INDEX_FLEX_EXT:
1911 default:
1912 return bdev->md.md_offset + bdev->md.md_size_sect;
1913 }
1914}
1915
1916/* Returns the number of 512 byte sectors of the device */
1917static inline sector_t drbd_get_capacity(struct block_device *bdev)
1918{
1919 /* return bdev ? get_capacity(bdev->bd_disk) : 0; */
Mike Snitzer77304d22010-11-08 14:39:12 +01001920 return bdev ? i_size_read(bdev->bd_inode) >> 9 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001921}
1922
1923/**
1924 * drbd_get_max_capacity() - Returns the capacity we announce to out peer
1925 * @bdev: Meta data block device.
1926 *
1927 * returns the capacity we announce to out peer. we clip ourselves at the
1928 * various MAX_SECTORS, because if we don't, current implementation will
1929 * oops sooner or later
1930 */
1931static inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
1932{
1933 sector_t s;
1934 switch (bdev->dc.meta_dev_idx) {
1935 case DRBD_MD_INDEX_INTERNAL:
1936 case DRBD_MD_INDEX_FLEX_INT:
1937 s = drbd_get_capacity(bdev->backing_bdev)
1938 ? min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1939 drbd_md_first_sector(bdev))
1940 : 0;
1941 break;
1942 case DRBD_MD_INDEX_FLEX_EXT:
1943 s = min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1944 drbd_get_capacity(bdev->backing_bdev));
1945 /* clip at maximum size the meta device can support */
1946 s = min_t(sector_t, s,
1947 BM_EXT_TO_SECT(bdev->md.md_size_sect
1948 - bdev->md.bm_offset));
1949 break;
1950 default:
1951 s = min_t(sector_t, DRBD_MAX_SECTORS,
1952 drbd_get_capacity(bdev->backing_bdev));
1953 }
1954 return s;
1955}
1956
1957/**
1958 * drbd_md_ss__() - Return the sector number of our meta data super block
1959 * @mdev: DRBD device.
1960 * @bdev: Meta data block device.
1961 */
1962static inline sector_t drbd_md_ss__(struct drbd_conf *mdev,
1963 struct drbd_backing_dev *bdev)
1964{
1965 switch (bdev->dc.meta_dev_idx) {
1966 default: /* external, some index */
1967 return MD_RESERVED_SECT * bdev->dc.meta_dev_idx;
1968 case DRBD_MD_INDEX_INTERNAL:
1969 /* with drbd08, internal meta data is always "flexible" */
1970 case DRBD_MD_INDEX_FLEX_INT:
1971 /* sizeof(struct md_on_disk_07) == 4k
1972 * position: last 4k aligned block of 4k size */
1973 if (!bdev->backing_bdev) {
1974 if (__ratelimit(&drbd_ratelimit_state)) {
1975 dev_err(DEV, "bdev->backing_bdev==NULL\n");
1976 dump_stack();
1977 }
1978 return 0;
1979 }
1980 return (drbd_get_capacity(bdev->backing_bdev) & ~7ULL)
1981 - MD_AL_OFFSET;
1982 case DRBD_MD_INDEX_FLEX_EXT:
1983 return 0;
1984 }
1985}
1986
1987static inline void
Philipp Reisnerb411b362009-09-25 16:07:19 -07001988drbd_queue_work_front(struct drbd_work_queue *q, struct drbd_work *w)
1989{
1990 unsigned long flags;
1991 spin_lock_irqsave(&q->q_lock, flags);
1992 list_add(&w->list, &q->q);
1993 up(&q->s); /* within the spinlock,
1994 see comment near end of drbd_worker() */
1995 spin_unlock_irqrestore(&q->q_lock, flags);
1996}
1997
1998static inline void
1999drbd_queue_work(struct drbd_work_queue *q, struct drbd_work *w)
2000{
2001 unsigned long flags;
2002 spin_lock_irqsave(&q->q_lock, flags);
2003 list_add_tail(&w->list, &q->q);
2004 up(&q->s); /* within the spinlock,
2005 see comment near end of drbd_worker() */
2006 spin_unlock_irqrestore(&q->q_lock, flags);
2007}
2008
2009static inline void wake_asender(struct drbd_conf *mdev)
2010{
2011 if (test_bit(SIGNAL_ASENDER, &mdev->flags))
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002012 force_sig(DRBD_SIG, mdev->tconn->asender.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002013}
2014
2015static inline void request_ping(struct drbd_conf *mdev)
2016{
2017 set_bit(SEND_PING, &mdev->flags);
2018 wake_asender(mdev);
2019}
2020
2021static inline int drbd_send_short_cmd(struct drbd_conf *mdev,
2022 enum drbd_packets cmd)
2023{
Philipp Reisnerc0129492011-01-19 16:58:16 +01002024 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002025 return drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &h, sizeof(h));
2026}
2027
2028static inline int drbd_send_ping(struct drbd_conf *mdev)
2029{
Philipp Reisnerc0129492011-01-19 16:58:16 +01002030 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002031 return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING, &h, sizeof(h));
2032}
2033
2034static inline int drbd_send_ping_ack(struct drbd_conf *mdev)
2035{
Philipp Reisnerc0129492011-01-19 16:58:16 +01002036 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002037 return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING_ACK, &h, sizeof(h));
2038}
2039
2040static inline void drbd_thread_stop(struct drbd_thread *thi)
2041{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002042 _drbd_thread_stop(thi, false, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002043}
2044
2045static inline void drbd_thread_stop_nowait(struct drbd_thread *thi)
2046{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002047 _drbd_thread_stop(thi, false, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002048}
2049
2050static inline void drbd_thread_restart_nowait(struct drbd_thread *thi)
2051{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002052 _drbd_thread_stop(thi, true, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002053}
2054
2055/* counts how many answer packets packets we expect from our peer,
2056 * for either explicit application requests,
2057 * or implicit barrier packets as necessary.
2058 * increased:
2059 * w_send_barrier
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01002060 * _req_mod(req, QUEUE_FOR_NET_WRITE or QUEUE_FOR_NET_READ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002061 * it is much easier and equally valid to count what we queue for the
2062 * worker, even before it actually was queued or send.
2063 * (drbd_make_request_common; recovery path on read io-error)
2064 * decreased:
2065 * got_BarrierAck (respective tl_clear, tl_clear_barrier)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01002066 * _req_mod(req, DATA_RECEIVED)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002067 * [from receive_DataReply]
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01002068 * _req_mod(req, WRITE_ACKED_BY_PEER or RECV_ACKED_BY_PEER or NEG_ACKED)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002069 * [from got_BlockAck (P_WRITE_ACK, P_RECV_ACK)]
2070 * for some reason it is NOT decreased in got_NegAck,
2071 * but in the resulting cleanup code from report_params.
2072 * we should try to remember the reason for that...
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01002073 * _req_mod(req, SEND_FAILED or SEND_CANCELED)
2074 * _req_mod(req, CONNECTION_LOST_WHILE_PENDING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002075 * [from tl_clear_barrier]
2076 */
2077static inline void inc_ap_pending(struct drbd_conf *mdev)
2078{
2079 atomic_inc(&mdev->ap_pending_cnt);
2080}
2081
2082#define ERR_IF_CNT_IS_NEGATIVE(which) \
2083 if (atomic_read(&mdev->which) < 0) \
2084 dev_err(DEV, "in %s:%d: " #which " = %d < 0 !\n", \
2085 __func__ , __LINE__ , \
2086 atomic_read(&mdev->which))
2087
2088#define dec_ap_pending(mdev) do { \
2089 typecheck(struct drbd_conf *, mdev); \
2090 if (atomic_dec_and_test(&mdev->ap_pending_cnt)) \
2091 wake_up(&mdev->misc_wait); \
2092 ERR_IF_CNT_IS_NEGATIVE(ap_pending_cnt); } while (0)
2093
2094/* counts how many resync-related answers we still expect from the peer
2095 * increase decrease
2096 * C_SYNC_TARGET sends P_RS_DATA_REQUEST (and expects P_RS_DATA_REPLY)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002097 * C_SYNC_SOURCE sends P_RS_DATA_REPLY (and expects P_WRITE_ACK with ID_SYNCER)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002098 * (or P_NEG_ACK with ID_SYNCER)
2099 */
2100static inline void inc_rs_pending(struct drbd_conf *mdev)
2101{
2102 atomic_inc(&mdev->rs_pending_cnt);
2103}
2104
2105#define dec_rs_pending(mdev) do { \
2106 typecheck(struct drbd_conf *, mdev); \
2107 atomic_dec(&mdev->rs_pending_cnt); \
2108 ERR_IF_CNT_IS_NEGATIVE(rs_pending_cnt); } while (0)
2109
2110/* counts how many answers we still need to send to the peer.
2111 * increased on
2112 * receive_Data unless protocol A;
2113 * we need to send a P_RECV_ACK (proto B)
2114 * or P_WRITE_ACK (proto C)
2115 * receive_RSDataReply (recv_resync_read) we need to send a P_WRITE_ACK
2116 * receive_DataRequest (receive_RSDataRequest) we need to send back P_DATA
2117 * receive_Barrier_* we need to send a P_BARRIER_ACK
2118 */
2119static inline void inc_unacked(struct drbd_conf *mdev)
2120{
2121 atomic_inc(&mdev->unacked_cnt);
2122}
2123
2124#define dec_unacked(mdev) do { \
2125 typecheck(struct drbd_conf *, mdev); \
2126 atomic_dec(&mdev->unacked_cnt); \
2127 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2128
2129#define sub_unacked(mdev, n) do { \
2130 typecheck(struct drbd_conf *, mdev); \
2131 atomic_sub(n, &mdev->unacked_cnt); \
2132 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2133
2134
Philipp Reisnerb2fb6db2011-01-19 13:48:44 +01002135static inline void put_net_conf(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002136{
Philipp Reisnerb2fb6db2011-01-19 13:48:44 +01002137 if (atomic_dec_and_test(&tconn->net_cnt))
2138 wake_up(&tconn->net_cnt_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002139}
2140
2141/**
Philipp Reisner89e58e72011-01-19 13:12:45 +01002142 * get_net_conf() - Increase ref count on mdev->tconn->net_conf; Returns 0 if nothing there
Philipp Reisnerb411b362009-09-25 16:07:19 -07002143 * @mdev: DRBD device.
2144 *
Philipp Reisner89e58e72011-01-19 13:12:45 +01002145 * You have to call put_net_conf() when finished working with mdev->tconn->net_conf.
Philipp Reisnerb411b362009-09-25 16:07:19 -07002146 */
Philipp Reisnerb2fb6db2011-01-19 13:48:44 +01002147static inline int get_net_conf(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002148{
2149 int have_net_conf;
2150
Philipp Reisnerb2fb6db2011-01-19 13:48:44 +01002151 atomic_inc(&tconn->net_cnt);
2152 have_net_conf = tconn->volume0->state.conn >= C_UNCONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002153 if (!have_net_conf)
Philipp Reisnerb2fb6db2011-01-19 13:48:44 +01002154 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002155 return have_net_conf;
2156}
2157
2158/**
2159 * get_ldev() - Increase the ref count on mdev->ldev. Returns 0 if there is no ldev
2160 * @M: DRBD device.
2161 *
2162 * You have to call put_ldev() when finished working with mdev->ldev.
2163 */
2164#define get_ldev(M) __cond_lock(local, _get_ldev_if_state(M,D_INCONSISTENT))
2165#define get_ldev_if_state(M,MINS) __cond_lock(local, _get_ldev_if_state(M,MINS))
2166
2167static inline void put_ldev(struct drbd_conf *mdev)
2168{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002169 int i = atomic_dec_return(&mdev->local_cnt);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02002170
2171 /* This may be called from some endio handler,
2172 * so we must not sleep here. */
2173
Philipp Reisnerb411b362009-09-25 16:07:19 -07002174 __release(local);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002175 D_ASSERT(i >= 0);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002176 if (i == 0) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002177 if (mdev->state.disk == D_DISKLESS)
2178 /* even internal references gone, safe to destroy */
2179 drbd_ldev_destroy(mdev);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002180 if (mdev->state.disk == D_FAILED)
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002181 /* all application IO references gone. */
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002182 drbd_go_diskless(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002183 wake_up(&mdev->misc_wait);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002184 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002185}
2186
2187#ifndef __CHECKER__
2188static inline int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
2189{
2190 int io_allowed;
2191
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002192 /* never get a reference while D_DISKLESS */
2193 if (mdev->state.disk == D_DISKLESS)
2194 return 0;
2195
Philipp Reisnerb411b362009-09-25 16:07:19 -07002196 atomic_inc(&mdev->local_cnt);
2197 io_allowed = (mdev->state.disk >= mins);
2198 if (!io_allowed)
2199 put_ldev(mdev);
2200 return io_allowed;
2201}
2202#else
2203extern int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins);
2204#endif
2205
2206/* you must have an "get_ldev" reference */
2207static inline void drbd_get_syncer_progress(struct drbd_conf *mdev,
2208 unsigned long *bits_left, unsigned int *per_mil_done)
2209{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01002210 /* this is to break it at compile time when we change that, in case we
2211 * want to support more than (1<<32) bits on a 32bit arch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002212 typecheck(unsigned long, mdev->rs_total);
2213
2214 /* note: both rs_total and rs_left are in bits, i.e. in
2215 * units of BM_BLOCK_SIZE.
2216 * for the percentage, we don't care. */
2217
Lars Ellenberg439d5952010-11-05 09:52:46 +01002218 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2219 *bits_left = mdev->ov_left;
2220 else
2221 *bits_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002222 /* >> 10 to prevent overflow,
2223 * +1 to prevent division by zero */
2224 if (*bits_left > mdev->rs_total) {
2225 /* doh. maybe a logic bug somewhere.
2226 * may also be just a race condition
2227 * between this and a disconnect during sync.
2228 * for now, just prevent in-kernel buffer overflow.
2229 */
2230 smp_rmb();
2231 dev_warn(DEV, "cs:%s rs_left=%lu > rs_total=%lu (rs_failed %lu)\n",
2232 drbd_conn_str(mdev->state.conn),
2233 *bits_left, mdev->rs_total, mdev->rs_failed);
2234 *per_mil_done = 0;
2235 } else {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01002236 /* Make sure the division happens in long context.
2237 * We allow up to one petabyte storage right now,
2238 * at a granularity of 4k per bit that is 2**38 bits.
2239 * After shift right and multiplication by 1000,
2240 * this should still fit easily into a 32bit long,
2241 * so we don't need a 64bit division on 32bit arch.
2242 * Note: currently we don't support such large bitmaps on 32bit
2243 * arch anyways, but no harm done to be prepared for it here.
2244 */
2245 unsigned int shift = mdev->rs_total >= (1ULL << 32) ? 16 : 10;
2246 unsigned long left = *bits_left >> shift;
2247 unsigned long total = 1UL + (mdev->rs_total >> shift);
2248 unsigned long tmp = 1000UL - left * 1000UL/total;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002249 *per_mil_done = tmp;
2250 }
2251}
2252
2253
2254/* this throttles on-the-fly application requests
2255 * according to max_buffers settings;
2256 * maybe re-implement using semaphores? */
2257static inline int drbd_get_max_buffers(struct drbd_conf *mdev)
2258{
2259 int mxb = 1000000; /* arbitrary limit on open requests */
Philipp Reisnerb2fb6db2011-01-19 13:48:44 +01002260 if (get_net_conf(mdev->tconn)) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002261 mxb = mdev->tconn->net_conf->max_buffers;
Philipp Reisnerb2fb6db2011-01-19 13:48:44 +01002262 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002263 }
2264 return mxb;
2265}
2266
Philipp Reisner37190942010-11-10 12:08:37 +01002267static inline int drbd_state_is_stable(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002268{
Philipp Reisner37190942010-11-10 12:08:37 +01002269 union drbd_state s = mdev->state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002270
2271 /* DO NOT add a default clause, we want the compiler to warn us
2272 * for any newly introduced state we may have forgotten to add here */
2273
2274 switch ((enum drbd_conns)s.conn) {
2275 /* new io only accepted when there is no connection, ... */
2276 case C_STANDALONE:
2277 case C_WF_CONNECTION:
2278 /* ... or there is a well established connection. */
2279 case C_CONNECTED:
2280 case C_SYNC_SOURCE:
2281 case C_SYNC_TARGET:
2282 case C_VERIFY_S:
2283 case C_VERIFY_T:
2284 case C_PAUSED_SYNC_S:
2285 case C_PAUSED_SYNC_T:
Philipp Reisner67531712010-10-27 12:21:30 +02002286 case C_AHEAD:
2287 case C_BEHIND:
Philipp Reisner37190942010-11-10 12:08:37 +01002288 /* transitional states, IO allowed */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002289 case C_DISCONNECTING:
2290 case C_UNCONNECTED:
2291 case C_TIMEOUT:
2292 case C_BROKEN_PIPE:
2293 case C_NETWORK_FAILURE:
2294 case C_PROTOCOL_ERROR:
2295 case C_TEAR_DOWN:
2296 case C_WF_REPORT_PARAMS:
2297 case C_STARTING_SYNC_S:
2298 case C_STARTING_SYNC_T:
Philipp Reisner37190942010-11-10 12:08:37 +01002299 break;
2300
2301 /* Allow IO in BM exchange states with new protocols */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002302 case C_WF_BITMAP_S:
Philipp Reisner31890f42011-01-19 14:12:51 +01002303 if (mdev->tconn->agreed_pro_version < 96)
Philipp Reisner37190942010-11-10 12:08:37 +01002304 return 0;
2305 break;
2306
2307 /* no new io accepted in these states */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002308 case C_WF_BITMAP_T:
2309 case C_WF_SYNC_UUID:
2310 case C_MASK:
2311 /* not "stable" */
2312 return 0;
2313 }
2314
2315 switch ((enum drbd_disk_state)s.disk) {
2316 case D_DISKLESS:
2317 case D_INCONSISTENT:
2318 case D_OUTDATED:
2319 case D_CONSISTENT:
2320 case D_UP_TO_DATE:
2321 /* disk state is stable as well. */
2322 break;
2323
2324 /* no new io accepted during tansitional states */
2325 case D_ATTACHING:
2326 case D_FAILED:
2327 case D_NEGOTIATING:
2328 case D_UNKNOWN:
2329 case D_MASK:
2330 /* not "stable" */
2331 return 0;
2332 }
2333
2334 return 1;
2335}
2336
Philipp Reisnerfb22c402010-09-08 23:20:21 +02002337static inline int is_susp(union drbd_state s)
2338{
2339 return s.susp || s.susp_nod || s.susp_fen;
2340}
2341
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002342static inline bool may_inc_ap_bio(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002343{
2344 int mxb = drbd_get_max_buffers(mdev);
2345
Philipp Reisnerfb22c402010-09-08 23:20:21 +02002346 if (is_susp(mdev->state))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002347 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002348 if (test_bit(SUSPEND_IO, &mdev->flags))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002349 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002350
2351 /* to avoid potential deadlock or bitmap corruption,
2352 * in various places, we only allow new application io
2353 * to start during "stable" states. */
2354
2355 /* no new io accepted when attaching or detaching the disk */
Philipp Reisner37190942010-11-10 12:08:37 +01002356 if (!drbd_state_is_stable(mdev))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002357 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002358
2359 /* since some older kernels don't have atomic_add_unless,
2360 * and we are within the spinlock anyways, we have this workaround. */
2361 if (atomic_read(&mdev->ap_bio_cnt) > mxb)
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002362 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002363 if (test_bit(BITMAP_IO, &mdev->flags))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002364 return false;
2365 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002366}
2367
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002368static inline bool inc_ap_bio_cond(struct drbd_conf *mdev, int count)
Philipp Reisner8869d682010-11-17 18:24:19 +01002369{
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002370 bool rv = false;
Philipp Reisner8869d682010-11-17 18:24:19 +01002371
Philipp Reisner87eeee42011-01-19 14:16:30 +01002372 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002373 rv = may_inc_ap_bio(mdev);
Philipp Reisner8869d682010-11-17 18:24:19 +01002374 if (rv)
2375 atomic_add(count, &mdev->ap_bio_cnt);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002376 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner8869d682010-11-17 18:24:19 +01002377
2378 return rv;
2379}
2380
Philipp Reisner9a25a042010-05-10 16:42:23 +02002381static inline void inc_ap_bio(struct drbd_conf *mdev, int count)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002382{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002383 /* we wait here
2384 * as long as the device is suspended
2385 * until the bitmap is no longer on the fly during connection
2386 * handshake as long as we would exeed the max_buffer limit.
2387 *
2388 * to avoid races with the reconnect code,
2389 * we need to atomic_inc within the spinlock. */
2390
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002391 wait_event(mdev->misc_wait, inc_ap_bio_cond(mdev, count));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002392}
2393
2394static inline void dec_ap_bio(struct drbd_conf *mdev)
2395{
2396 int mxb = drbd_get_max_buffers(mdev);
2397 int ap_bio = atomic_dec_return(&mdev->ap_bio_cnt);
2398
2399 D_ASSERT(ap_bio >= 0);
2400 /* this currently does wake_up for every dec_ap_bio!
2401 * maybe rather introduce some type of hysteresis?
2402 * e.g. (ap_bio == mxb/2 || ap_bio == 0) ? */
2403 if (ap_bio < mxb)
2404 wake_up(&mdev->misc_wait);
2405 if (ap_bio == 0 && test_bit(BITMAP_IO, &mdev->flags)) {
2406 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002407 drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002408 }
2409}
2410
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002411static inline int drbd_set_ed_uuid(struct drbd_conf *mdev, u64 val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002412{
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002413 int changed = mdev->ed_uuid != val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002414 mdev->ed_uuid = val;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002415 return changed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002416}
2417
2418static inline int seq_cmp(u32 a, u32 b)
2419{
2420 /* we assume wrap around at 32bit.
2421 * for wrap around at 24bit (old atomic_t),
2422 * we'd have to
2423 * a <<= 8; b <<= 8;
2424 */
2425 return (s32)(a) - (s32)(b);
2426}
2427#define seq_lt(a, b) (seq_cmp((a), (b)) < 0)
2428#define seq_gt(a, b) (seq_cmp((a), (b)) > 0)
2429#define seq_ge(a, b) (seq_cmp((a), (b)) >= 0)
2430#define seq_le(a, b) (seq_cmp((a), (b)) <= 0)
2431/* CAUTION: please no side effects in arguments! */
2432#define seq_max(a, b) ((u32)(seq_gt((a), (b)) ? (a) : (b)))
2433
2434static inline void update_peer_seq(struct drbd_conf *mdev, unsigned int new_seq)
2435{
2436 unsigned int m;
2437 spin_lock(&mdev->peer_seq_lock);
2438 m = seq_max(mdev->peer_seq, new_seq);
2439 mdev->peer_seq = m;
2440 spin_unlock(&mdev->peer_seq_lock);
2441 if (m == new_seq)
2442 wake_up(&mdev->seq_wait);
2443}
2444
2445static inline void drbd_update_congested(struct drbd_conf *mdev)
2446{
Philipp Reisnere42325a2011-01-19 13:55:45 +01002447 struct sock *sk = mdev->tconn->data.socket->sk;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002448 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
2449 set_bit(NET_CONGESTED, &mdev->flags);
2450}
2451
2452static inline int drbd_queue_order_type(struct drbd_conf *mdev)
2453{
2454 /* sorry, we currently have no working implementation
2455 * of distributed TCQ stuff */
2456#ifndef QUEUE_ORDERED_NONE
2457#define QUEUE_ORDERED_NONE 0
2458#endif
2459 return QUEUE_ORDERED_NONE;
2460}
2461
Philipp Reisnerb411b362009-09-25 16:07:19 -07002462static inline void drbd_md_flush(struct drbd_conf *mdev)
2463{
2464 int r;
2465
Philipp Reisnera8a4e512010-08-25 10:21:04 +02002466 if (test_bit(MD_NO_FUA, &mdev->flags))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002467 return;
2468
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02002469 r = blkdev_issue_flush(mdev->ldev->md_bdev, GFP_KERNEL, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002470 if (r) {
Philipp Reisnera8a4e512010-08-25 10:21:04 +02002471 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002472 dev_err(DEV, "meta data flush failed with status %d, disabling md-flushes\n", r);
2473 }
2474}
2475
2476#endif