xref: /linux/drivers/block/drbd/drbd_int.h (revision dac1389ccc273b5486f2931c64c8e1672f233727)
1b411b363SPhilipp Reisner /*
2b411b363SPhilipp Reisner   drbd_int.h
3b411b363SPhilipp Reisner 
4b411b363SPhilipp Reisner   This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5b411b363SPhilipp Reisner 
6b411b363SPhilipp Reisner   Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7b411b363SPhilipp Reisner   Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8b411b363SPhilipp Reisner   Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9b411b363SPhilipp Reisner 
10b411b363SPhilipp Reisner   drbd is free software; you can redistribute it and/or modify
11b411b363SPhilipp Reisner   it under the terms of the GNU General Public License as published by
12b411b363SPhilipp Reisner   the Free Software Foundation; either version 2, or (at your option)
13b411b363SPhilipp Reisner   any later version.
14b411b363SPhilipp Reisner 
15b411b363SPhilipp Reisner   drbd is distributed in the hope that it will be useful,
16b411b363SPhilipp Reisner   but WITHOUT ANY WARRANTY; without even the implied warranty of
17b411b363SPhilipp Reisner   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
18b411b363SPhilipp Reisner   GNU General Public License for more details.
19b411b363SPhilipp Reisner 
20b411b363SPhilipp Reisner   You should have received a copy of the GNU General Public License
21b411b363SPhilipp Reisner   along with drbd; see the file COPYING.  If not, write to
22b411b363SPhilipp Reisner   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23b411b363SPhilipp Reisner 
24b411b363SPhilipp Reisner */
25b411b363SPhilipp Reisner 
26b411b363SPhilipp Reisner #ifndef _DRBD_INT_H
27b411b363SPhilipp Reisner #define _DRBD_INT_H
28b411b363SPhilipp Reisner 
29b411b363SPhilipp Reisner #include <linux/compiler.h>
30b411b363SPhilipp Reisner #include <linux/types.h>
31b411b363SPhilipp Reisner #include <linux/version.h>
32b411b363SPhilipp Reisner #include <linux/list.h>
33b411b363SPhilipp Reisner #include <linux/sched.h>
34b411b363SPhilipp Reisner #include <linux/bitops.h>
35b411b363SPhilipp Reisner #include <linux/slab.h>
36b411b363SPhilipp Reisner #include <linux/crypto.h>
37132cc538SRandy Dunlap #include <linux/ratelimit.h>
38b411b363SPhilipp Reisner #include <linux/tcp.h>
39b411b363SPhilipp Reisner #include <linux/mutex.h>
40b411b363SPhilipp Reisner #include <linux/major.h>
41b411b363SPhilipp Reisner #include <linux/blkdev.h>
42b411b363SPhilipp Reisner #include <linux/genhd.h>
43b411b363SPhilipp Reisner #include <net/tcp.h>
44b411b363SPhilipp Reisner #include <linux/lru_cache.h>
4570c71606SPaul Gortmaker #include <linux/prefetch.h>
46b411b363SPhilipp Reisner 
47b411b363SPhilipp Reisner #ifdef __CHECKER__
48b411b363SPhilipp Reisner # define __protected_by(x)       __attribute__((require_context(x,1,999,"rdwr")))
49b411b363SPhilipp Reisner # define __protected_read_by(x)  __attribute__((require_context(x,1,999,"read")))
50b411b363SPhilipp Reisner # define __protected_write_by(x) __attribute__((require_context(x,1,999,"write")))
51b411b363SPhilipp Reisner # define __must_hold(x)       __attribute__((context(x,1,1), require_context(x,1,999,"call")))
52b411b363SPhilipp Reisner #else
53b411b363SPhilipp Reisner # define __protected_by(x)
54b411b363SPhilipp Reisner # define __protected_read_by(x)
55b411b363SPhilipp Reisner # define __protected_write_by(x)
56b411b363SPhilipp Reisner # define __must_hold(x)
57b411b363SPhilipp Reisner #endif
58b411b363SPhilipp Reisner 
59b411b363SPhilipp Reisner #define __no_warn(lock, stmt) do { __acquire(lock); stmt; __release(lock); } while (0)
60b411b363SPhilipp Reisner 
61b411b363SPhilipp Reisner /* module parameter, defined in drbd_main.c */
62b411b363SPhilipp Reisner extern unsigned int minor_count;
63b411b363SPhilipp Reisner extern int disable_sendpage;
64b411b363SPhilipp Reisner extern int allow_oos;
65b411b363SPhilipp Reisner extern unsigned int cn_idx;
66b411b363SPhilipp Reisner 
67b411b363SPhilipp Reisner #ifdef CONFIG_DRBD_FAULT_INJECTION
68b411b363SPhilipp Reisner extern int enable_faults;
69b411b363SPhilipp Reisner extern int fault_rate;
70b411b363SPhilipp Reisner extern int fault_devs;
71b411b363SPhilipp Reisner #endif
72b411b363SPhilipp Reisner 
73b411b363SPhilipp Reisner extern char usermode_helper[];
74b411b363SPhilipp Reisner 
75b411b363SPhilipp Reisner 
76b411b363SPhilipp Reisner /* I don't remember why XCPU ...
77b411b363SPhilipp Reisner  * This is used to wake the asender,
78b411b363SPhilipp Reisner  * and to interrupt sending the sending task
79b411b363SPhilipp Reisner  * on disconnect.
80b411b363SPhilipp Reisner  */
81b411b363SPhilipp Reisner #define DRBD_SIG SIGXCPU
82b411b363SPhilipp Reisner 
83b411b363SPhilipp Reisner /* This is used to stop/restart our threads.
84b411b363SPhilipp Reisner  * Cannot use SIGTERM nor SIGKILL, since these
85b411b363SPhilipp Reisner  * are sent out by init on runlevel changes
86b411b363SPhilipp Reisner  * I choose SIGHUP for now.
87b411b363SPhilipp Reisner  */
88b411b363SPhilipp Reisner #define DRBD_SIGKILL SIGHUP
89b411b363SPhilipp Reisner 
90b411b363SPhilipp Reisner #define ID_IN_SYNC      (4711ULL)
91b411b363SPhilipp Reisner #define ID_OUT_OF_SYNC  (4712ULL)
92b411b363SPhilipp Reisner #define ID_SYNCER (-1ULL)
93579b57edSAndreas Gruenbacher 
944a23f264SPhilipp Reisner #define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
95b411b363SPhilipp Reisner 
96b411b363SPhilipp Reisner struct drbd_conf;
97b411b363SPhilipp Reisner 
98b411b363SPhilipp Reisner 
99b411b363SPhilipp Reisner /* to shorten dev_warn(DEV, "msg"); and relatives statements */
100b411b363SPhilipp Reisner #define DEV (disk_to_dev(mdev->vdisk))
101b411b363SPhilipp Reisner 
102b411b363SPhilipp Reisner #define D_ASSERT(exp)	if (!(exp)) \
103b411b363SPhilipp Reisner 	 dev_err(DEV, "ASSERT( " #exp " ) in %s:%d\n", __FILE__, __LINE__)
104b411b363SPhilipp Reisner 
105b411b363SPhilipp Reisner #define ERR_IF(exp) if (({						\
106b411b363SPhilipp Reisner 	int _b = (exp) != 0;						\
1073beec1d4SLars Ellenberg 	if (_b) dev_err(DEV, "ASSERT FAILED: %s: (%s) in %s:%d\n",	\
108b411b363SPhilipp Reisner 			__func__, #exp, __FILE__, __LINE__);		\
109b411b363SPhilipp Reisner 	_b;								\
110b411b363SPhilipp Reisner 	}))
111b411b363SPhilipp Reisner 
112b411b363SPhilipp Reisner /* Defines to control fault insertion */
113b411b363SPhilipp Reisner enum {
114b411b363SPhilipp Reisner 	DRBD_FAULT_MD_WR = 0,	/* meta data write */
115b411b363SPhilipp Reisner 	DRBD_FAULT_MD_RD = 1,	/*           read  */
116b411b363SPhilipp Reisner 	DRBD_FAULT_RS_WR = 2,	/* resync          */
117b411b363SPhilipp Reisner 	DRBD_FAULT_RS_RD = 3,
118b411b363SPhilipp Reisner 	DRBD_FAULT_DT_WR = 4,	/* data            */
119b411b363SPhilipp Reisner 	DRBD_FAULT_DT_RD = 5,
120b411b363SPhilipp Reisner 	DRBD_FAULT_DT_RA = 6,	/* data read ahead */
121b411b363SPhilipp Reisner 	DRBD_FAULT_BM_ALLOC = 7,	/* bitmap allocation */
122b411b363SPhilipp Reisner 	DRBD_FAULT_AL_EE = 8,	/* alloc ee */
1236b4388acSPhilipp Reisner 	DRBD_FAULT_RECEIVE = 9, /* Changes some bytes upon receiving a [rs]data block */
124b411b363SPhilipp Reisner 
125b411b363SPhilipp Reisner 	DRBD_FAULT_MAX,
126b411b363SPhilipp Reisner };
127b411b363SPhilipp Reisner 
128b411b363SPhilipp Reisner extern unsigned int
129b411b363SPhilipp Reisner _drbd_insert_fault(struct drbd_conf *mdev, unsigned int type);
1300cf9d27eSAndreas Gruenbacher 
131b411b363SPhilipp Reisner static inline int
132b411b363SPhilipp Reisner drbd_insert_fault(struct drbd_conf *mdev, unsigned int type) {
1330cf9d27eSAndreas Gruenbacher #ifdef CONFIG_DRBD_FAULT_INJECTION
134b411b363SPhilipp Reisner 	return fault_rate &&
135b411b363SPhilipp Reisner 		(enable_faults & (1<<type)) &&
136b411b363SPhilipp Reisner 		_drbd_insert_fault(mdev, type);
137b411b363SPhilipp Reisner #else
1380cf9d27eSAndreas Gruenbacher 	return 0;
139b411b363SPhilipp Reisner #endif
1400cf9d27eSAndreas Gruenbacher }
141b411b363SPhilipp Reisner 
142b411b363SPhilipp Reisner /* integer division, round _UP_ to the next integer */
143b411b363SPhilipp Reisner #define div_ceil(A, B) ((A)/(B) + ((A)%(B) ? 1 : 0))
144b411b363SPhilipp Reisner /* usual integer division */
145b411b363SPhilipp Reisner #define div_floor(A, B) ((A)/(B))
146b411b363SPhilipp Reisner 
147b411b363SPhilipp Reisner /* drbd_meta-data.c (still in drbd_main.c) */
148b411b363SPhilipp Reisner /* 4th incarnation of the disk layout. */
149b411b363SPhilipp Reisner #define DRBD_MD_MAGIC (DRBD_MAGIC+4)
150b411b363SPhilipp Reisner 
151b411b363SPhilipp Reisner extern struct drbd_conf **minor_table;
152b411b363SPhilipp Reisner extern struct ratelimit_state drbd_ratelimit_state;
153b411b363SPhilipp Reisner 
154b411b363SPhilipp Reisner /* on the wire */
155b411b363SPhilipp Reisner enum drbd_packets {
156b411b363SPhilipp Reisner 	/* receiver (data socket) */
157b411b363SPhilipp Reisner 	P_DATA		      = 0x00,
158b411b363SPhilipp Reisner 	P_DATA_REPLY	      = 0x01, /* Response to P_DATA_REQUEST */
159b411b363SPhilipp Reisner 	P_RS_DATA_REPLY	      = 0x02, /* Response to P_RS_DATA_REQUEST */
160b411b363SPhilipp Reisner 	P_BARRIER	      = 0x03,
161b411b363SPhilipp Reisner 	P_BITMAP	      = 0x04,
162b411b363SPhilipp Reisner 	P_BECOME_SYNC_TARGET  = 0x05,
163b411b363SPhilipp Reisner 	P_BECOME_SYNC_SOURCE  = 0x06,
164b411b363SPhilipp Reisner 	P_UNPLUG_REMOTE	      = 0x07, /* Used at various times to hint the peer */
165b411b363SPhilipp Reisner 	P_DATA_REQUEST	      = 0x08, /* Used to ask for a data block */
166b411b363SPhilipp Reisner 	P_RS_DATA_REQUEST     = 0x09, /* Used to ask for a data block for resync */
167b411b363SPhilipp Reisner 	P_SYNC_PARAM	      = 0x0a,
168b411b363SPhilipp Reisner 	P_PROTOCOL	      = 0x0b,
169b411b363SPhilipp Reisner 	P_UUIDS		      = 0x0c,
170b411b363SPhilipp Reisner 	P_SIZES		      = 0x0d,
171b411b363SPhilipp Reisner 	P_STATE		      = 0x0e,
172b411b363SPhilipp Reisner 	P_SYNC_UUID	      = 0x0f,
173b411b363SPhilipp Reisner 	P_AUTH_CHALLENGE      = 0x10,
174b411b363SPhilipp Reisner 	P_AUTH_RESPONSE	      = 0x11,
175b411b363SPhilipp Reisner 	P_STATE_CHG_REQ	      = 0x12,
176b411b363SPhilipp Reisner 
177b411b363SPhilipp Reisner 	/* asender (meta socket */
178b411b363SPhilipp Reisner 	P_PING		      = 0x13,
179b411b363SPhilipp Reisner 	P_PING_ACK	      = 0x14,
180b411b363SPhilipp Reisner 	P_RECV_ACK	      = 0x15, /* Used in protocol B */
181b411b363SPhilipp Reisner 	P_WRITE_ACK	      = 0x16, /* Used in protocol C */
182b411b363SPhilipp Reisner 	P_RS_WRITE_ACK	      = 0x17, /* Is a P_WRITE_ACK, additionally call set_in_sync(). */
183b411b363SPhilipp Reisner 	P_DISCARD_ACK	      = 0x18, /* Used in proto C, two-primaries conflict detection */
184b411b363SPhilipp Reisner 	P_NEG_ACK	      = 0x19, /* Sent if local disk is unusable */
185b411b363SPhilipp Reisner 	P_NEG_DREPLY	      = 0x1a, /* Local disk is broken... */
186b411b363SPhilipp Reisner 	P_NEG_RS_DREPLY	      = 0x1b, /* Local disk is broken... */
187b411b363SPhilipp Reisner 	P_BARRIER_ACK	      = 0x1c,
188b411b363SPhilipp Reisner 	P_STATE_CHG_REPLY     = 0x1d,
189b411b363SPhilipp Reisner 
190b411b363SPhilipp Reisner 	/* "new" commands, no longer fitting into the ordering scheme above */
191b411b363SPhilipp Reisner 
192b411b363SPhilipp Reisner 	P_OV_REQUEST	      = 0x1e, /* data socket */
193b411b363SPhilipp Reisner 	P_OV_REPLY	      = 0x1f,
194b411b363SPhilipp Reisner 	P_OV_RESULT	      = 0x20, /* meta socket */
195b411b363SPhilipp Reisner 	P_CSUM_RS_REQUEST     = 0x21, /* data socket */
196b411b363SPhilipp Reisner 	P_RS_IS_IN_SYNC	      = 0x22, /* meta socket */
197b411b363SPhilipp Reisner 	P_SYNC_PARAM89	      = 0x23, /* data socket, protocol version 89 replacement for P_SYNC_PARAM */
198b411b363SPhilipp Reisner 	P_COMPRESSED_BITMAP   = 0x24, /* compressed or otherwise encoded bitmap transfer */
1990ced55a3SPhilipp Reisner 	/* P_CKPT_FENCE_REQ      = 0x25, * currently reserved for protocol D */
2000ced55a3SPhilipp Reisner 	/* P_CKPT_DISABLE_REQ    = 0x26, * currently reserved for protocol D */
2010ced55a3SPhilipp Reisner 	P_DELAY_PROBE         = 0x27, /* is used on BOTH sockets */
20273a01a18SPhilipp Reisner 	P_OUT_OF_SYNC         = 0x28, /* Mark as out of sync (Outrunning), data socket */
203d612d309SPhilipp Reisner 	P_RS_CANCEL           = 0x29, /* meta: Used to cancel RS_DATA_REQUEST packet by SyncSource */
204b411b363SPhilipp Reisner 
205d612d309SPhilipp Reisner 	P_MAX_CMD	      = 0x2A,
206b411b363SPhilipp Reisner 	P_MAY_IGNORE	      = 0x100, /* Flag to test if (cmd > P_MAY_IGNORE) ... */
207b411b363SPhilipp Reisner 	P_MAX_OPT_CMD	      = 0x101,
208b411b363SPhilipp Reisner 
209b411b363SPhilipp Reisner 	/* special command ids for handshake */
210b411b363SPhilipp Reisner 
211b411b363SPhilipp Reisner 	P_HAND_SHAKE_M	      = 0xfff1, /* First Packet on the MetaSock */
212b411b363SPhilipp Reisner 	P_HAND_SHAKE_S	      = 0xfff2, /* First Packet on the Socket */
213b411b363SPhilipp Reisner 
214b411b363SPhilipp Reisner 	P_HAND_SHAKE	      = 0xfffe	/* FIXED for the next century! */
215b411b363SPhilipp Reisner };
216b411b363SPhilipp Reisner 
217b411b363SPhilipp Reisner static inline const char *cmdname(enum drbd_packets cmd)
218b411b363SPhilipp Reisner {
219b411b363SPhilipp Reisner 	/* THINK may need to become several global tables
220b411b363SPhilipp Reisner 	 * when we want to support more than
221b411b363SPhilipp Reisner 	 * one PRO_VERSION */
222b411b363SPhilipp Reisner 	static const char *cmdnames[] = {
223b411b363SPhilipp Reisner 		[P_DATA]	        = "Data",
224b411b363SPhilipp Reisner 		[P_DATA_REPLY]	        = "DataReply",
225b411b363SPhilipp Reisner 		[P_RS_DATA_REPLY]	= "RSDataReply",
226b411b363SPhilipp Reisner 		[P_BARRIER]	        = "Barrier",
227b411b363SPhilipp Reisner 		[P_BITMAP]	        = "ReportBitMap",
228b411b363SPhilipp Reisner 		[P_BECOME_SYNC_TARGET]  = "BecomeSyncTarget",
229b411b363SPhilipp Reisner 		[P_BECOME_SYNC_SOURCE]  = "BecomeSyncSource",
230b411b363SPhilipp Reisner 		[P_UNPLUG_REMOTE]	= "UnplugRemote",
231b411b363SPhilipp Reisner 		[P_DATA_REQUEST]	= "DataRequest",
232b411b363SPhilipp Reisner 		[P_RS_DATA_REQUEST]     = "RSDataRequest",
233b411b363SPhilipp Reisner 		[P_SYNC_PARAM]	        = "SyncParam",
234b411b363SPhilipp Reisner 		[P_SYNC_PARAM89]	= "SyncParam89",
235b411b363SPhilipp Reisner 		[P_PROTOCOL]            = "ReportProtocol",
236b411b363SPhilipp Reisner 		[P_UUIDS]	        = "ReportUUIDs",
237b411b363SPhilipp Reisner 		[P_SIZES]	        = "ReportSizes",
238b411b363SPhilipp Reisner 		[P_STATE]	        = "ReportState",
239b411b363SPhilipp Reisner 		[P_SYNC_UUID]           = "ReportSyncUUID",
240b411b363SPhilipp Reisner 		[P_AUTH_CHALLENGE]      = "AuthChallenge",
241b411b363SPhilipp Reisner 		[P_AUTH_RESPONSE]	= "AuthResponse",
242b411b363SPhilipp Reisner 		[P_PING]		= "Ping",
243b411b363SPhilipp Reisner 		[P_PING_ACK]	        = "PingAck",
244b411b363SPhilipp Reisner 		[P_RECV_ACK]	        = "RecvAck",
245b411b363SPhilipp Reisner 		[P_WRITE_ACK]	        = "WriteAck",
246b411b363SPhilipp Reisner 		[P_RS_WRITE_ACK]	= "RSWriteAck",
247b411b363SPhilipp Reisner 		[P_DISCARD_ACK]	        = "DiscardAck",
248b411b363SPhilipp Reisner 		[P_NEG_ACK]	        = "NegAck",
249b411b363SPhilipp Reisner 		[P_NEG_DREPLY]	        = "NegDReply",
250b411b363SPhilipp Reisner 		[P_NEG_RS_DREPLY]	= "NegRSDReply",
251b411b363SPhilipp Reisner 		[P_BARRIER_ACK]	        = "BarrierAck",
252b411b363SPhilipp Reisner 		[P_STATE_CHG_REQ]       = "StateChgRequest",
253b411b363SPhilipp Reisner 		[P_STATE_CHG_REPLY]     = "StateChgReply",
254b411b363SPhilipp Reisner 		[P_OV_REQUEST]          = "OVRequest",
255b411b363SPhilipp Reisner 		[P_OV_REPLY]            = "OVReply",
256b411b363SPhilipp Reisner 		[P_OV_RESULT]           = "OVResult",
257c42b6cf4SLars Ellenberg 		[P_CSUM_RS_REQUEST]     = "CsumRSRequest",
258c42b6cf4SLars Ellenberg 		[P_RS_IS_IN_SYNC]	= "CsumRSIsInSync",
259c42b6cf4SLars Ellenberg 		[P_COMPRESSED_BITMAP]   = "CBitmap",
260a8cdfd8dSPhilipp Reisner 		[P_DELAY_PROBE]         = "DelayProbe",
26173a01a18SPhilipp Reisner 		[P_OUT_OF_SYNC]		= "OutOfSync",
262b411b363SPhilipp Reisner 		[P_MAX_CMD]	        = NULL,
263b411b363SPhilipp Reisner 	};
264b411b363SPhilipp Reisner 
265b411b363SPhilipp Reisner 	if (cmd == P_HAND_SHAKE_M)
266b411b363SPhilipp Reisner 		return "HandShakeM";
267b411b363SPhilipp Reisner 	if (cmd == P_HAND_SHAKE_S)
268b411b363SPhilipp Reisner 		return "HandShakeS";
269b411b363SPhilipp Reisner 	if (cmd == P_HAND_SHAKE)
270b411b363SPhilipp Reisner 		return "HandShake";
271b411b363SPhilipp Reisner 	if (cmd >= P_MAX_CMD)
272b411b363SPhilipp Reisner 		return "Unknown";
273b411b363SPhilipp Reisner 	return cmdnames[cmd];
274b411b363SPhilipp Reisner }
275b411b363SPhilipp Reisner 
276b411b363SPhilipp Reisner /* for sending/receiving the bitmap,
277b411b363SPhilipp Reisner  * possibly in some encoding scheme */
278b411b363SPhilipp Reisner struct bm_xfer_ctx {
279b411b363SPhilipp Reisner 	/* "const"
280b411b363SPhilipp Reisner 	 * stores total bits and long words
281b411b363SPhilipp Reisner 	 * of the bitmap, so we don't need to
282b411b363SPhilipp Reisner 	 * call the accessor functions over and again. */
283b411b363SPhilipp Reisner 	unsigned long bm_bits;
284b411b363SPhilipp Reisner 	unsigned long bm_words;
285b411b363SPhilipp Reisner 	/* during xfer, current position within the bitmap */
286b411b363SPhilipp Reisner 	unsigned long bit_offset;
287b411b363SPhilipp Reisner 	unsigned long word_offset;
288b411b363SPhilipp Reisner 
289b411b363SPhilipp Reisner 	/* statistics; index: (h->command == P_BITMAP) */
290b411b363SPhilipp Reisner 	unsigned packets[2];
291b411b363SPhilipp Reisner 	unsigned bytes[2];
292b411b363SPhilipp Reisner };
293b411b363SPhilipp Reisner 
294b411b363SPhilipp Reisner extern void INFO_bm_xfer_stats(struct drbd_conf *mdev,
295b411b363SPhilipp Reisner 		const char *direction, struct bm_xfer_ctx *c);
296b411b363SPhilipp Reisner 
297b411b363SPhilipp Reisner static inline void bm_xfer_ctx_bit_to_word_offset(struct bm_xfer_ctx *c)
298b411b363SPhilipp Reisner {
299b411b363SPhilipp Reisner 	/* word_offset counts "native long words" (32 or 64 bit),
300b411b363SPhilipp Reisner 	 * aligned at 64 bit.
301b411b363SPhilipp Reisner 	 * Encoded packet may end at an unaligned bit offset.
302b411b363SPhilipp Reisner 	 * In case a fallback clear text packet is transmitted in
303b411b363SPhilipp Reisner 	 * between, we adjust this offset back to the last 64bit
304b411b363SPhilipp Reisner 	 * aligned "native long word", which makes coding and decoding
305b411b363SPhilipp Reisner 	 * the plain text bitmap much more convenient.  */
306b411b363SPhilipp Reisner #if BITS_PER_LONG == 64
307b411b363SPhilipp Reisner 	c->word_offset = c->bit_offset >> 6;
308b411b363SPhilipp Reisner #elif BITS_PER_LONG == 32
309b411b363SPhilipp Reisner 	c->word_offset = c->bit_offset >> 5;
310b411b363SPhilipp Reisner 	c->word_offset &= ~(1UL);
311b411b363SPhilipp Reisner #else
312b411b363SPhilipp Reisner # error "unsupported BITS_PER_LONG"
313b411b363SPhilipp Reisner #endif
314b411b363SPhilipp Reisner }
315b411b363SPhilipp Reisner 
316b411b363SPhilipp Reisner #ifndef __packed
317b411b363SPhilipp Reisner #define __packed __attribute__((packed))
318b411b363SPhilipp Reisner #endif
319b411b363SPhilipp Reisner 
320b411b363SPhilipp Reisner /* This is the layout for a packet on the wire.
321b411b363SPhilipp Reisner  * The byteorder is the network byte order.
322b411b363SPhilipp Reisner  *     (except block_id and barrier fields.
323b411b363SPhilipp Reisner  *	these are pointers to local structs
324b411b363SPhilipp Reisner  *	and have no relevance for the partner,
325b411b363SPhilipp Reisner  *	which just echoes them as received.)
326b411b363SPhilipp Reisner  *
327b411b363SPhilipp Reisner  * NOTE that the payload starts at a long aligned offset,
328b411b363SPhilipp Reisner  * regardless of 32 or 64 bit arch!
329b411b363SPhilipp Reisner  */
3300b70a13dSPhilipp Reisner struct p_header80 {
331b411b363SPhilipp Reisner 	u32	  magic;
332b411b363SPhilipp Reisner 	u16	  command;
333b411b363SPhilipp Reisner 	u16	  length;	/* bytes of data after this header */
334b411b363SPhilipp Reisner 	u8	  payload[0];
335b411b363SPhilipp Reisner } __packed;
3360b70a13dSPhilipp Reisner 
3370b70a13dSPhilipp Reisner /* Header for big packets, Used for data packets exceeding 64kB */
3380b70a13dSPhilipp Reisner struct p_header95 {
3390b70a13dSPhilipp Reisner 	u16	  magic;	/* use DRBD_MAGIC_BIG here */
3400b70a13dSPhilipp Reisner 	u16	  command;
34100b42537SPhilipp Reisner 	u32	  length;	/* Use only 24 bits of that. Ignore the highest 8 bit. */
3420b70a13dSPhilipp Reisner 	u8	  payload[0];
3430b70a13dSPhilipp Reisner } __packed;
3440b70a13dSPhilipp Reisner 
3450b70a13dSPhilipp Reisner union p_header {
3460b70a13dSPhilipp Reisner 	struct p_header80 h80;
3470b70a13dSPhilipp Reisner 	struct p_header95 h95;
3480b70a13dSPhilipp Reisner };
349b411b363SPhilipp Reisner 
350b411b363SPhilipp Reisner /*
351b411b363SPhilipp Reisner  * short commands, packets without payload, plain p_header:
352b411b363SPhilipp Reisner  *   P_PING
353b411b363SPhilipp Reisner  *   P_PING_ACK
354b411b363SPhilipp Reisner  *   P_BECOME_SYNC_TARGET
355b411b363SPhilipp Reisner  *   P_BECOME_SYNC_SOURCE
356b411b363SPhilipp Reisner  *   P_UNPLUG_REMOTE
357b411b363SPhilipp Reisner  */
358b411b363SPhilipp Reisner 
359b411b363SPhilipp Reisner /*
360b411b363SPhilipp Reisner  * commands with out-of-struct payload:
361b411b363SPhilipp Reisner  *   P_BITMAP    (no additional fields)
362b411b363SPhilipp Reisner  *   P_DATA, P_DATA_REPLY (see p_data)
363b411b363SPhilipp Reisner  *   P_COMPRESSED_BITMAP (see receive_compressed_bitmap)
364b411b363SPhilipp Reisner  */
365b411b363SPhilipp Reisner 
366b411b363SPhilipp Reisner /* these defines must not be changed without changing the protocol version */
36776d2e7ecSPhilipp Reisner #define DP_HARDBARRIER	      1 /* depricated */
36876d2e7ecSPhilipp Reisner #define DP_RW_SYNC	      2 /* equals REQ_SYNC    */
369b411b363SPhilipp Reisner #define DP_MAY_SET_IN_SYNC    4
370721a9602SJens Axboe #define DP_UNPLUG             8 /* not used anymore   */
37176d2e7ecSPhilipp Reisner #define DP_FUA               16 /* equals REQ_FUA     */
37276d2e7ecSPhilipp Reisner #define DP_FLUSH             32 /* equals REQ_FLUSH   */
37376d2e7ecSPhilipp Reisner #define DP_DISCARD           64 /* equals REQ_DISCARD */
374b411b363SPhilipp Reisner 
375b411b363SPhilipp Reisner struct p_data {
3760b70a13dSPhilipp Reisner 	union p_header head;
377b411b363SPhilipp Reisner 	u64	    sector;    /* 64 bits sector number */
378b411b363SPhilipp Reisner 	u64	    block_id;  /* to identify the request in protocol B&C */
379b411b363SPhilipp Reisner 	u32	    seq_num;
380b411b363SPhilipp Reisner 	u32	    dp_flags;
381b411b363SPhilipp Reisner } __packed;
382b411b363SPhilipp Reisner 
383b411b363SPhilipp Reisner /*
384b411b363SPhilipp Reisner  * commands which share a struct:
385b411b363SPhilipp Reisner  *  p_block_ack:
386b411b363SPhilipp Reisner  *   P_RECV_ACK (proto B), P_WRITE_ACK (proto C),
387b411b363SPhilipp Reisner  *   P_DISCARD_ACK (proto C, two-primaries conflict detection)
388b411b363SPhilipp Reisner  *  p_block_req:
389b411b363SPhilipp Reisner  *   P_DATA_REQUEST, P_RS_DATA_REQUEST
390b411b363SPhilipp Reisner  */
391b411b363SPhilipp Reisner struct p_block_ack {
3920b70a13dSPhilipp Reisner 	struct p_header80 head;
393b411b363SPhilipp Reisner 	u64	    sector;
394b411b363SPhilipp Reisner 	u64	    block_id;
395b411b363SPhilipp Reisner 	u32	    blksize;
396b411b363SPhilipp Reisner 	u32	    seq_num;
397b411b363SPhilipp Reisner } __packed;
398b411b363SPhilipp Reisner 
399b411b363SPhilipp Reisner 
400b411b363SPhilipp Reisner struct p_block_req {
4010b70a13dSPhilipp Reisner 	struct p_header80 head;
402b411b363SPhilipp Reisner 	u64 sector;
403b411b363SPhilipp Reisner 	u64 block_id;
404b411b363SPhilipp Reisner 	u32 blksize;
405b411b363SPhilipp Reisner 	u32 pad;	/* to multiple of 8 Byte */
406b411b363SPhilipp Reisner } __packed;
407b411b363SPhilipp Reisner 
408b411b363SPhilipp Reisner /*
409b411b363SPhilipp Reisner  * commands with their own struct for additional fields:
410b411b363SPhilipp Reisner  *   P_HAND_SHAKE
411b411b363SPhilipp Reisner  *   P_BARRIER
412b411b363SPhilipp Reisner  *   P_BARRIER_ACK
413b411b363SPhilipp Reisner  *   P_SYNC_PARAM
414b411b363SPhilipp Reisner  *   ReportParams
415b411b363SPhilipp Reisner  */
416b411b363SPhilipp Reisner 
417b411b363SPhilipp Reisner struct p_handshake {
4180b70a13dSPhilipp Reisner 	struct p_header80 head;	/* 8 bytes */
419b411b363SPhilipp Reisner 	u32 protocol_min;
420b411b363SPhilipp Reisner 	u32 feature_flags;
421b411b363SPhilipp Reisner 	u32 protocol_max;
422b411b363SPhilipp Reisner 
423b411b363SPhilipp Reisner 	/* should be more than enough for future enhancements
424b411b363SPhilipp Reisner 	 * for now, feature_flags and the reserverd array shall be zero.
425b411b363SPhilipp Reisner 	 */
426b411b363SPhilipp Reisner 
427b411b363SPhilipp Reisner 	u32 _pad;
428b411b363SPhilipp Reisner 	u64 reserverd[7];
429b411b363SPhilipp Reisner } __packed;
430b411b363SPhilipp Reisner /* 80 bytes, FIXED for the next century */
431b411b363SPhilipp Reisner 
432b411b363SPhilipp Reisner struct p_barrier {
4330b70a13dSPhilipp Reisner 	struct p_header80 head;
434b411b363SPhilipp Reisner 	u32 barrier;	/* barrier number _handle_ only */
435b411b363SPhilipp Reisner 	u32 pad;	/* to multiple of 8 Byte */
436b411b363SPhilipp Reisner } __packed;
437b411b363SPhilipp Reisner 
438b411b363SPhilipp Reisner struct p_barrier_ack {
4390b70a13dSPhilipp Reisner 	struct p_header80 head;
440b411b363SPhilipp Reisner 	u32 barrier;
441b411b363SPhilipp Reisner 	u32 set_size;
442b411b363SPhilipp Reisner } __packed;
443b411b363SPhilipp Reisner 
444b411b363SPhilipp Reisner struct p_rs_param {
4450b70a13dSPhilipp Reisner 	struct p_header80 head;
446b411b363SPhilipp Reisner 	u32 rate;
447b411b363SPhilipp Reisner 
448b411b363SPhilipp Reisner 	      /* Since protocol version 88 and higher. */
449b411b363SPhilipp Reisner 	char verify_alg[0];
450b411b363SPhilipp Reisner } __packed;
451b411b363SPhilipp Reisner 
452b411b363SPhilipp Reisner struct p_rs_param_89 {
4530b70a13dSPhilipp Reisner 	struct p_header80 head;
454b411b363SPhilipp Reisner 	u32 rate;
455b411b363SPhilipp Reisner         /* protocol version 89: */
456b411b363SPhilipp Reisner 	char verify_alg[SHARED_SECRET_MAX];
457b411b363SPhilipp Reisner 	char csums_alg[SHARED_SECRET_MAX];
458b411b363SPhilipp Reisner } __packed;
459b411b363SPhilipp Reisner 
4608e26f9ccSPhilipp Reisner struct p_rs_param_95 {
4610b70a13dSPhilipp Reisner 	struct p_header80 head;
4628e26f9ccSPhilipp Reisner 	u32 rate;
4638e26f9ccSPhilipp Reisner 	char verify_alg[SHARED_SECRET_MAX];
4648e26f9ccSPhilipp Reisner 	char csums_alg[SHARED_SECRET_MAX];
4658e26f9ccSPhilipp Reisner 	u32 c_plan_ahead;
4668e26f9ccSPhilipp Reisner 	u32 c_delay_target;
4678e26f9ccSPhilipp Reisner 	u32 c_fill_target;
4688e26f9ccSPhilipp Reisner 	u32 c_max_rate;
4698e26f9ccSPhilipp Reisner } __packed;
4708e26f9ccSPhilipp Reisner 
471cf14c2e9SPhilipp Reisner enum drbd_conn_flags {
472cf14c2e9SPhilipp Reisner 	CF_WANT_LOSE = 1,
473cf14c2e9SPhilipp Reisner 	CF_DRY_RUN = 2,
474cf14c2e9SPhilipp Reisner };
475cf14c2e9SPhilipp Reisner 
476b411b363SPhilipp Reisner struct p_protocol {
4770b70a13dSPhilipp Reisner 	struct p_header80 head;
478b411b363SPhilipp Reisner 	u32 protocol;
479b411b363SPhilipp Reisner 	u32 after_sb_0p;
480b411b363SPhilipp Reisner 	u32 after_sb_1p;
481b411b363SPhilipp Reisner 	u32 after_sb_2p;
482cf14c2e9SPhilipp Reisner 	u32 conn_flags;
483b411b363SPhilipp Reisner 	u32 two_primaries;
484b411b363SPhilipp Reisner 
485b411b363SPhilipp Reisner               /* Since protocol version 87 and higher. */
486b411b363SPhilipp Reisner 	char integrity_alg[0];
487b411b363SPhilipp Reisner 
488b411b363SPhilipp Reisner } __packed;
489b411b363SPhilipp Reisner 
490b411b363SPhilipp Reisner struct p_uuids {
4910b70a13dSPhilipp Reisner 	struct p_header80 head;
492b411b363SPhilipp Reisner 	u64 uuid[UI_EXTENDED_SIZE];
493b411b363SPhilipp Reisner } __packed;
494b411b363SPhilipp Reisner 
495b411b363SPhilipp Reisner struct p_rs_uuid {
4960b70a13dSPhilipp Reisner 	struct p_header80 head;
497b411b363SPhilipp Reisner 	u64	    uuid;
498b411b363SPhilipp Reisner } __packed;
499b411b363SPhilipp Reisner 
500b411b363SPhilipp Reisner struct p_sizes {
5010b70a13dSPhilipp Reisner 	struct p_header80 head;
502b411b363SPhilipp Reisner 	u64	    d_size;  /* size of disk */
503b411b363SPhilipp Reisner 	u64	    u_size;  /* user requested size */
504b411b363SPhilipp Reisner 	u64	    c_size;  /* current exported size */
5051816a2b4SLars Ellenberg 	u32	    max_bio_size;  /* Maximal size of a BIO */
506e89b591cSPhilipp Reisner 	u16	    queue_order_type;  /* not yet implemented in DRBD*/
507e89b591cSPhilipp Reisner 	u16	    dds_flags; /* use enum dds_flags here. */
508b411b363SPhilipp Reisner } __packed;
509b411b363SPhilipp Reisner 
510b411b363SPhilipp Reisner struct p_state {
5110b70a13dSPhilipp Reisner 	struct p_header80 head;
512b411b363SPhilipp Reisner 	u32	    state;
513b411b363SPhilipp Reisner } __packed;
514b411b363SPhilipp Reisner 
515b411b363SPhilipp Reisner struct p_req_state {
5160b70a13dSPhilipp Reisner 	struct p_header80 head;
517b411b363SPhilipp Reisner 	u32	    mask;
518b411b363SPhilipp Reisner 	u32	    val;
519b411b363SPhilipp Reisner } __packed;
520b411b363SPhilipp Reisner 
521b411b363SPhilipp Reisner struct p_req_state_reply {
5220b70a13dSPhilipp Reisner 	struct p_header80 head;
523b411b363SPhilipp Reisner 	u32	    retcode;
524b411b363SPhilipp Reisner } __packed;
525b411b363SPhilipp Reisner 
526b411b363SPhilipp Reisner struct p_drbd06_param {
527b411b363SPhilipp Reisner 	u64	  size;
528b411b363SPhilipp Reisner 	u32	  state;
529b411b363SPhilipp Reisner 	u32	  blksize;
530b411b363SPhilipp Reisner 	u32	  protocol;
531b411b363SPhilipp Reisner 	u32	  version;
532b411b363SPhilipp Reisner 	u32	  gen_cnt[5];
533b411b363SPhilipp Reisner 	u32	  bit_map_gen[5];
534b411b363SPhilipp Reisner } __packed;
535b411b363SPhilipp Reisner 
536b411b363SPhilipp Reisner struct p_discard {
5370b70a13dSPhilipp Reisner 	struct p_header80 head;
538b411b363SPhilipp Reisner 	u64	    block_id;
539b411b363SPhilipp Reisner 	u32	    seq_num;
540b411b363SPhilipp Reisner 	u32	    pad;
541b411b363SPhilipp Reisner } __packed;
542b411b363SPhilipp Reisner 
54373a01a18SPhilipp Reisner struct p_block_desc {
54473a01a18SPhilipp Reisner 	struct p_header80 head;
54573a01a18SPhilipp Reisner 	u64 sector;
54673a01a18SPhilipp Reisner 	u32 blksize;
54773a01a18SPhilipp Reisner 	u32 pad;	/* to multiple of 8 Byte */
54873a01a18SPhilipp Reisner } __packed;
54973a01a18SPhilipp Reisner 
550b411b363SPhilipp Reisner /* Valid values for the encoding field.
551b411b363SPhilipp Reisner  * Bump proto version when changing this. */
552b411b363SPhilipp Reisner enum drbd_bitmap_code {
553b411b363SPhilipp Reisner 	/* RLE_VLI_Bytes = 0,
554b411b363SPhilipp Reisner 	 * and other bit variants had been defined during
555b411b363SPhilipp Reisner 	 * algorithm evaluation. */
556b411b363SPhilipp Reisner 	RLE_VLI_Bits = 2,
557b411b363SPhilipp Reisner };
558b411b363SPhilipp Reisner 
559b411b363SPhilipp Reisner struct p_compressed_bm {
5600b70a13dSPhilipp Reisner 	struct p_header80 head;
561b411b363SPhilipp Reisner 	/* (encoding & 0x0f): actual encoding, see enum drbd_bitmap_code
562b411b363SPhilipp Reisner 	 * (encoding & 0x80): polarity (set/unset) of first runlength
563b411b363SPhilipp Reisner 	 * ((encoding >> 4) & 0x07): pad_bits, number of trailing zero bits
564b411b363SPhilipp Reisner 	 * used to pad up to head.length bytes
565b411b363SPhilipp Reisner 	 */
566b411b363SPhilipp Reisner 	u8 encoding;
567b411b363SPhilipp Reisner 
568b411b363SPhilipp Reisner 	u8 code[0];
569b411b363SPhilipp Reisner } __packed;
570b411b363SPhilipp Reisner 
5710b70a13dSPhilipp Reisner struct p_delay_probe93 {
5720b70a13dSPhilipp Reisner 	struct p_header80 head;
5730ced55a3SPhilipp Reisner 	u32     seq_num; /* sequence number to match the two probe packets */
5740ced55a3SPhilipp Reisner 	u32     offset;  /* usecs the probe got sent after the reference time point */
5750ced55a3SPhilipp Reisner } __packed;
5760ced55a3SPhilipp Reisner 
577b411b363SPhilipp Reisner /* DCBP: Drbd Compressed Bitmap Packet ... */
578b411b363SPhilipp Reisner static inline enum drbd_bitmap_code
579b411b363SPhilipp Reisner DCBP_get_code(struct p_compressed_bm *p)
580b411b363SPhilipp Reisner {
581b411b363SPhilipp Reisner 	return (enum drbd_bitmap_code)(p->encoding & 0x0f);
582b411b363SPhilipp Reisner }
583b411b363SPhilipp Reisner 
584b411b363SPhilipp Reisner static inline void
585b411b363SPhilipp Reisner DCBP_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
586b411b363SPhilipp Reisner {
587b411b363SPhilipp Reisner 	BUG_ON(code & ~0xf);
588b411b363SPhilipp Reisner 	p->encoding = (p->encoding & ~0xf) | code;
589b411b363SPhilipp Reisner }
590b411b363SPhilipp Reisner 
591b411b363SPhilipp Reisner static inline int
592b411b363SPhilipp Reisner DCBP_get_start(struct p_compressed_bm *p)
593b411b363SPhilipp Reisner {
594b411b363SPhilipp Reisner 	return (p->encoding & 0x80) != 0;
595b411b363SPhilipp Reisner }
596b411b363SPhilipp Reisner 
597b411b363SPhilipp Reisner static inline void
598b411b363SPhilipp Reisner DCBP_set_start(struct p_compressed_bm *p, int set)
599b411b363SPhilipp Reisner {
600b411b363SPhilipp Reisner 	p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
601b411b363SPhilipp Reisner }
602b411b363SPhilipp Reisner 
603b411b363SPhilipp Reisner static inline int
604b411b363SPhilipp Reisner DCBP_get_pad_bits(struct p_compressed_bm *p)
605b411b363SPhilipp Reisner {
606b411b363SPhilipp Reisner 	return (p->encoding >> 4) & 0x7;
607b411b363SPhilipp Reisner }
608b411b363SPhilipp Reisner 
609b411b363SPhilipp Reisner static inline void
610b411b363SPhilipp Reisner DCBP_set_pad_bits(struct p_compressed_bm *p, int n)
611b411b363SPhilipp Reisner {
612b411b363SPhilipp Reisner 	BUG_ON(n & ~0x7);
613b411b363SPhilipp Reisner 	p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
614b411b363SPhilipp Reisner }
615b411b363SPhilipp Reisner 
616b411b363SPhilipp Reisner /* one bitmap packet, including the p_header,
617b411b363SPhilipp Reisner  * should fit within one _architecture independend_ page.
618b411b363SPhilipp Reisner  * so we need to use the fixed size 4KiB page size
61925985edcSLucas De Marchi  * most architectures have used for a long time.
620b411b363SPhilipp Reisner  */
6210b70a13dSPhilipp Reisner #define BM_PACKET_PAYLOAD_BYTES (4096 - sizeof(struct p_header80))
622b411b363SPhilipp Reisner #define BM_PACKET_WORDS (BM_PACKET_PAYLOAD_BYTES/sizeof(long))
623b411b363SPhilipp Reisner #define BM_PACKET_VLI_BYTES_MAX (4096 - sizeof(struct p_compressed_bm))
624b411b363SPhilipp Reisner #if (PAGE_SIZE < 4096)
625b411b363SPhilipp Reisner /* drbd_send_bitmap / receive_bitmap would break horribly */
626b411b363SPhilipp Reisner #error "PAGE_SIZE too small"
627b411b363SPhilipp Reisner #endif
628b411b363SPhilipp Reisner 
629b411b363SPhilipp Reisner union p_polymorph {
63002918be2SPhilipp Reisner         union p_header           header;
631b411b363SPhilipp Reisner         struct p_handshake       handshake;
632b411b363SPhilipp Reisner         struct p_data            data;
633b411b363SPhilipp Reisner         struct p_block_ack       block_ack;
634b411b363SPhilipp Reisner         struct p_barrier         barrier;
635b411b363SPhilipp Reisner         struct p_barrier_ack     barrier_ack;
636b411b363SPhilipp Reisner         struct p_rs_param_89     rs_param_89;
6378e26f9ccSPhilipp Reisner         struct p_rs_param_95     rs_param_95;
638b411b363SPhilipp Reisner         struct p_protocol        protocol;
639b411b363SPhilipp Reisner         struct p_sizes           sizes;
640b411b363SPhilipp Reisner         struct p_uuids           uuids;
641b411b363SPhilipp Reisner         struct p_state           state;
642b411b363SPhilipp Reisner         struct p_req_state       req_state;
643b411b363SPhilipp Reisner         struct p_req_state_reply req_state_reply;
644b411b363SPhilipp Reisner         struct p_block_req       block_req;
64502918be2SPhilipp Reisner 	struct p_delay_probe93   delay_probe93;
64602918be2SPhilipp Reisner 	struct p_rs_uuid         rs_uuid;
64773a01a18SPhilipp Reisner 	struct p_block_desc      block_desc;
648b411b363SPhilipp Reisner } __packed;
649b411b363SPhilipp Reisner 
650b411b363SPhilipp Reisner /**********************************************************************/
651b411b363SPhilipp Reisner enum drbd_thread_state {
652b411b363SPhilipp Reisner 	None,
653b411b363SPhilipp Reisner 	Running,
654b411b363SPhilipp Reisner 	Exiting,
655b411b363SPhilipp Reisner 	Restarting
656b411b363SPhilipp Reisner };
657b411b363SPhilipp Reisner 
658b411b363SPhilipp Reisner struct drbd_thread {
659b411b363SPhilipp Reisner 	spinlock_t t_lock;
660b411b363SPhilipp Reisner 	struct task_struct *task;
661b411b363SPhilipp Reisner 	struct completion stop;
662b411b363SPhilipp Reisner 	enum drbd_thread_state t_state;
663b411b363SPhilipp Reisner 	int (*function) (struct drbd_thread *);
664b411b363SPhilipp Reisner 	struct drbd_conf *mdev;
665b411b363SPhilipp Reisner 	int reset_cpu_mask;
666b411b363SPhilipp Reisner };
667b411b363SPhilipp Reisner 
668b411b363SPhilipp Reisner static inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
669b411b363SPhilipp Reisner {
670b411b363SPhilipp Reisner 	/* THINK testing the t_state seems to be uncritical in all cases
671b411b363SPhilipp Reisner 	 * (but thread_{start,stop}), so we can read it *without* the lock.
672b411b363SPhilipp Reisner 	 *	--lge */
673b411b363SPhilipp Reisner 
674b411b363SPhilipp Reisner 	smp_rmb();
675b411b363SPhilipp Reisner 	return thi->t_state;
676b411b363SPhilipp Reisner }
677b411b363SPhilipp Reisner 
678b411b363SPhilipp Reisner struct drbd_work;
679b411b363SPhilipp Reisner typedef int (*drbd_work_cb)(struct drbd_conf *, struct drbd_work *, int cancel);
680b411b363SPhilipp Reisner struct drbd_work {
681b411b363SPhilipp Reisner 	struct list_head list;
682b411b363SPhilipp Reisner 	drbd_work_cb cb;
683b411b363SPhilipp Reisner };
684b411b363SPhilipp Reisner 
685ace652acSAndreas Gruenbacher #include "drbd_interval.h"
686ace652acSAndreas Gruenbacher 
687b411b363SPhilipp Reisner struct drbd_request {
688b411b363SPhilipp Reisner 	struct drbd_work w;
689b411b363SPhilipp Reisner 	struct drbd_conf *mdev;
690b411b363SPhilipp Reisner 
691b411b363SPhilipp Reisner 	/* if local IO is not allowed, will be NULL.
692b411b363SPhilipp Reisner 	 * if local IO _is_ allowed, holds the locally submitted bio clone,
693b411b363SPhilipp Reisner 	 * or, after local IO completion, the ERR_PTR(error).
694b411b363SPhilipp Reisner 	 * see drbd_endio_pri(). */
695b411b363SPhilipp Reisner 	struct bio *private_bio;
696b411b363SPhilipp Reisner 
69724c4830cSBart Van Assche 	struct hlist_node collision;
698ace652acSAndreas Gruenbacher 	struct drbd_interval i;
699b411b363SPhilipp Reisner 	unsigned int epoch; /* barrier_nr */
700b411b363SPhilipp Reisner 
701b411b363SPhilipp Reisner 	/* barrier_nr: used to check on "completion" whether this req was in
702b411b363SPhilipp Reisner 	 * the current epoch, and we therefore have to close it,
703b411b363SPhilipp Reisner 	 * starting a new epoch...
704b411b363SPhilipp Reisner 	 */
705b411b363SPhilipp Reisner 
706b411b363SPhilipp Reisner 	struct list_head tl_requests; /* ring list in the transfer log */
707b411b363SPhilipp Reisner 	struct bio *master_bio;       /* master bio pointer */
708b411b363SPhilipp Reisner 	unsigned long rq_state; /* see comments above _req_mod() */
709b411b363SPhilipp Reisner 	int seq_num;
710b411b363SPhilipp Reisner 	unsigned long start_time;
711b411b363SPhilipp Reisner };
712b411b363SPhilipp Reisner 
713b411b363SPhilipp Reisner struct drbd_tl_epoch {
714b411b363SPhilipp Reisner 	struct drbd_work w;
715b411b363SPhilipp Reisner 	struct list_head requests; /* requests before */
716b411b363SPhilipp Reisner 	struct drbd_tl_epoch *next; /* pointer to the next barrier */
717b411b363SPhilipp Reisner 	unsigned int br_number;  /* the barriers identifier. */
7187e602c0aSPhilipp Reisner 	int n_writes;	/* number of requests attached before this barrier */
719b411b363SPhilipp Reisner };
720b411b363SPhilipp Reisner 
721b411b363SPhilipp Reisner struct drbd_request;
722b411b363SPhilipp Reisner 
723b411b363SPhilipp Reisner /* These Tl_epoch_entries may be in one of 6 lists:
724b411b363SPhilipp Reisner    active_ee .. data packet being written
725b411b363SPhilipp Reisner    sync_ee   .. syncer block being written
726b411b363SPhilipp Reisner    done_ee   .. block written, need to send P_WRITE_ACK
727b411b363SPhilipp Reisner    read_ee   .. [RS]P_DATA_REQUEST being read
728b411b363SPhilipp Reisner */
729b411b363SPhilipp Reisner 
730b411b363SPhilipp Reisner struct drbd_epoch {
731b411b363SPhilipp Reisner 	struct list_head list;
732b411b363SPhilipp Reisner 	unsigned int barrier_nr;
733b411b363SPhilipp Reisner 	atomic_t epoch_size; /* increased on every request added. */
734b411b363SPhilipp Reisner 	atomic_t active;     /* increased on every req. added, and dec on every finished. */
735b411b363SPhilipp Reisner 	unsigned long flags;
736b411b363SPhilipp Reisner };
737b411b363SPhilipp Reisner 
738b411b363SPhilipp Reisner /* drbd_epoch flag bits */
739b411b363SPhilipp Reisner enum {
740b411b363SPhilipp Reisner 	DE_HAVE_BARRIER_NUMBER,
741b411b363SPhilipp Reisner };
742b411b363SPhilipp Reisner 
743b411b363SPhilipp Reisner enum epoch_event {
744b411b363SPhilipp Reisner 	EV_PUT,
745b411b363SPhilipp Reisner 	EV_GOT_BARRIER_NR,
746b411b363SPhilipp Reisner 	EV_BECAME_LAST,
747b411b363SPhilipp Reisner 	EV_CLEANUP = 32, /* used as flag */
748b411b363SPhilipp Reisner };
749b411b363SPhilipp Reisner 
750b411b363SPhilipp Reisner struct drbd_wq_barrier {
751b411b363SPhilipp Reisner 	struct drbd_work w;
752b411b363SPhilipp Reisner 	struct completion done;
753b411b363SPhilipp Reisner };
754b411b363SPhilipp Reisner 
755b411b363SPhilipp Reisner struct digest_info {
756b411b363SPhilipp Reisner 	int digest_size;
757b411b363SPhilipp Reisner 	void *digest;
758b411b363SPhilipp Reisner };
759b411b363SPhilipp Reisner 
76045bb912bSLars Ellenberg struct drbd_epoch_entry {
76145bb912bSLars Ellenberg 	struct drbd_work w;
76224c4830cSBart Van Assche 	struct hlist_node collision;
76385719573SPhilipp Reisner 	struct drbd_epoch *epoch; /* for writes */
76445bb912bSLars Ellenberg 	struct drbd_conf *mdev;
76545bb912bSLars Ellenberg 	struct page *pages;
76645bb912bSLars Ellenberg 	atomic_t pending_bios;
76745bb912bSLars Ellenberg 	unsigned int size;
76845bb912bSLars Ellenberg 	/* see comments on ee flag bits below */
76945bb912bSLars Ellenberg 	unsigned long flags;
77045bb912bSLars Ellenberg 	sector_t sector;
77185719573SPhilipp Reisner 	union {
77245bb912bSLars Ellenberg 		u64 block_id;
77385719573SPhilipp Reisner 		struct digest_info *digest;
77485719573SPhilipp Reisner 	};
77545bb912bSLars Ellenberg };
77645bb912bSLars Ellenberg 
77745bb912bSLars Ellenberg /* ee flag bits.
77845bb912bSLars Ellenberg  * While corresponding bios are in flight, the only modification will be
77945bb912bSLars Ellenberg  * set_bit WAS_ERROR, which has to be atomic.
78045bb912bSLars Ellenberg  * If no bios are in flight yet, or all have been completed,
78145bb912bSLars Ellenberg  * non-atomic modification to ee->flags is ok.
78245bb912bSLars Ellenberg  */
783b411b363SPhilipp Reisner enum {
784b411b363SPhilipp Reisner 	__EE_CALL_AL_COMPLETE_IO,
785b411b363SPhilipp Reisner 	__EE_MAY_SET_IN_SYNC,
78645bb912bSLars Ellenberg 
78745bb912bSLars Ellenberg 	/* In case a barrier failed,
78845bb912bSLars Ellenberg 	 * we need to resubmit without the barrier flag. */
78945bb912bSLars Ellenberg 	__EE_RESUBMITTED,
79045bb912bSLars Ellenberg 
79145bb912bSLars Ellenberg 	/* we may have several bios per epoch entry.
79245bb912bSLars Ellenberg 	 * if any of those fail, we set this flag atomically
79345bb912bSLars Ellenberg 	 * from the endio callback */
79445bb912bSLars Ellenberg 	__EE_WAS_ERROR,
795c36c3cedSLars Ellenberg 
796c36c3cedSLars Ellenberg 	/* This ee has a pointer to a digest instead of a block id */
797c36c3cedSLars Ellenberg 	__EE_HAS_DIGEST,
798b411b363SPhilipp Reisner };
799b411b363SPhilipp Reisner #define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
800b411b363SPhilipp Reisner #define EE_MAY_SET_IN_SYNC     (1<<__EE_MAY_SET_IN_SYNC)
80145bb912bSLars Ellenberg #define	EE_RESUBMITTED         (1<<__EE_RESUBMITTED)
80245bb912bSLars Ellenberg #define EE_WAS_ERROR           (1<<__EE_WAS_ERROR)
803c36c3cedSLars Ellenberg #define EE_HAS_DIGEST          (1<<__EE_HAS_DIGEST)
804b411b363SPhilipp Reisner 
805b411b363SPhilipp Reisner /* global flag bits */
806b411b363SPhilipp Reisner enum {
80725985edcSLucas De Marchi 	CREATE_BARRIER,		/* next P_DATA is preceded by a P_BARRIER */
808b411b363SPhilipp Reisner 	SIGNAL_ASENDER,		/* whether asender wants to be interrupted */
809b411b363SPhilipp Reisner 	SEND_PING,		/* whether asender should send a ping asap */
810b411b363SPhilipp Reisner 
811b411b363SPhilipp Reisner 	UNPLUG_QUEUED,		/* only relevant with kernel 2.4 */
812b411b363SPhilipp Reisner 	UNPLUG_REMOTE,		/* sending a "UnplugRemote" could help */
813b411b363SPhilipp Reisner 	MD_DIRTY,		/* current uuids and flags not yet on disk */
814b411b363SPhilipp Reisner 	DISCARD_CONCURRENT,	/* Set on one node, cleared on the peer! */
815b411b363SPhilipp Reisner 	USE_DEGR_WFC_T,		/* degr-wfc-timeout instead of wfc-timeout. */
816b411b363SPhilipp Reisner 	CLUSTER_ST_CHANGE,	/* Cluster wide state change going on... */
817b411b363SPhilipp Reisner 	CL_ST_CHG_SUCCESS,
818b411b363SPhilipp Reisner 	CL_ST_CHG_FAIL,
819b411b363SPhilipp Reisner 	CRASHED_PRIMARY,	/* This node was a crashed primary.
820b411b363SPhilipp Reisner 				 * Gets cleared when the state.conn
821b411b363SPhilipp Reisner 				 * goes into C_CONNECTED state. */
82219f843aaSLars Ellenberg 	NO_BARRIER_SUPP,	/* underlying block device doesn't implement barriers */
823b411b363SPhilipp Reisner 	CONSIDER_RESYNC,
824b411b363SPhilipp Reisner 
825a8a4e51eSPhilipp Reisner 	MD_NO_FUA,		/* Users wants us to not use FUA/FLUSH on meta data dev */
826b411b363SPhilipp Reisner 	SUSPEND_IO,		/* suspend application io */
827b411b363SPhilipp Reisner 	BITMAP_IO,		/* suspend application io;
828b411b363SPhilipp Reisner 				   once no more io in flight, start bitmap io */
829b411b363SPhilipp Reisner 	BITMAP_IO_QUEUED,       /* Started bitmap IO */
83082f59cc6SLars Ellenberg 	GO_DISKLESS,		/* Disk is being detached, on io-error or admin request. */
83182f59cc6SLars Ellenberg 	WAS_IO_ERROR,		/* Local disk failed returned IO error */
832b411b363SPhilipp Reisner 	RESYNC_AFTER_NEG,       /* Resync after online grow after the attach&negotiate finished. */
833b411b363SPhilipp Reisner 	NET_CONGESTED,		/* The data socket is congested */
834b411b363SPhilipp Reisner 
835b411b363SPhilipp Reisner 	CONFIG_PENDING,		/* serialization of (re)configuration requests.
836b411b363SPhilipp Reisner 				 * if set, also prevents the device from dying */
837b411b363SPhilipp Reisner 	DEVICE_DYING,		/* device became unconfigured,
838b411b363SPhilipp Reisner 				 * but worker thread is still handling the cleanup.
839b411b363SPhilipp Reisner 				 * reconfiguring (nl_disk_conf, nl_net_conf) is dissalowed,
840b411b363SPhilipp Reisner 				 * while this is set. */
841b411b363SPhilipp Reisner 	RESIZE_PENDING,		/* Size change detected locally, waiting for the response from
842b411b363SPhilipp Reisner 				 * the peer, if it changed there as well. */
843cf14c2e9SPhilipp Reisner 	CONN_DRY_RUN,		/* Expect disconnect after resync handshake. */
844309d1608SPhilipp Reisner 	GOT_PING_ACK,		/* set when we receive a ping_ack packet, misc wait gets woken */
84543a5182cSPhilipp Reisner 	NEW_CUR_UUID,		/* Create new current UUID when thawing IO */
8460778286aSPhilipp Reisner 	AL_SUSPENDED,		/* Activity logging is currently suspended. */
847370a43e7SPhilipp Reisner 	AHEAD_TO_SYNC_SOURCE,   /* Ahead -> SyncSource queued */
848b411b363SPhilipp Reisner };
849b411b363SPhilipp Reisner 
850b411b363SPhilipp Reisner struct drbd_bitmap; /* opaque for drbd_conf */
851b411b363SPhilipp Reisner 
85220ceb2b2SLars Ellenberg /* definition of bits in bm_flags to be used in drbd_bm_lock
85320ceb2b2SLars Ellenberg  * and drbd_bitmap_io and friends. */
85420ceb2b2SLars Ellenberg enum bm_flag {
85520ceb2b2SLars Ellenberg 	/* do we need to kfree, or vfree bm_pages? */
85620ceb2b2SLars Ellenberg 	BM_P_VMALLOCED = 0x10000, /* internal use only, will be masked out */
85720ceb2b2SLars Ellenberg 
85820ceb2b2SLars Ellenberg 	/* currently locked for bulk operation */
85920ceb2b2SLars Ellenberg 	BM_LOCKED_MASK = 0x7,
86020ceb2b2SLars Ellenberg 
86120ceb2b2SLars Ellenberg 	/* in detail, that is: */
86220ceb2b2SLars Ellenberg 	BM_DONT_CLEAR = 0x1,
86320ceb2b2SLars Ellenberg 	BM_DONT_SET   = 0x2,
86420ceb2b2SLars Ellenberg 	BM_DONT_TEST  = 0x4,
86520ceb2b2SLars Ellenberg 
86620ceb2b2SLars Ellenberg 	/* (test bit, count bit) allowed (common case) */
86720ceb2b2SLars Ellenberg 	BM_LOCKED_TEST_ALLOWED = 0x3,
86820ceb2b2SLars Ellenberg 
86920ceb2b2SLars Ellenberg 	/* testing bits, as well as setting new bits allowed, but clearing bits
87020ceb2b2SLars Ellenberg 	 * would be unexpected.  Used during bitmap receive.  Setting new bits
87120ceb2b2SLars Ellenberg 	 * requires sending of "out-of-sync" information, though. */
87220ceb2b2SLars Ellenberg 	BM_LOCKED_SET_ALLOWED = 0x1,
87320ceb2b2SLars Ellenberg 
87420ceb2b2SLars Ellenberg 	/* clear is not expected while bitmap is locked for bulk operation */
87520ceb2b2SLars Ellenberg };
87620ceb2b2SLars Ellenberg 
87720ceb2b2SLars Ellenberg 
878b411b363SPhilipp Reisner /* TODO sort members for performance
879b411b363SPhilipp Reisner  * MAYBE group them further */
880b411b363SPhilipp Reisner 
881b411b363SPhilipp Reisner /* THINK maybe we actually want to use the default "event/%s" worker threads
882b411b363SPhilipp Reisner  * or similar in linux 2.6, which uses per cpu data and threads.
883b411b363SPhilipp Reisner  */
884b411b363SPhilipp Reisner struct drbd_work_queue {
885b411b363SPhilipp Reisner 	struct list_head q;
886b411b363SPhilipp Reisner 	struct semaphore s; /* producers up it, worker down()s it */
887b411b363SPhilipp Reisner 	spinlock_t q_lock;  /* to protect the list. */
888b411b363SPhilipp Reisner };
889b411b363SPhilipp Reisner 
890b411b363SPhilipp Reisner struct drbd_socket {
891b411b363SPhilipp Reisner 	struct drbd_work_queue work;
892b411b363SPhilipp Reisner 	struct mutex mutex;
893b411b363SPhilipp Reisner 	struct socket    *socket;
894b411b363SPhilipp Reisner 	/* this way we get our
895b411b363SPhilipp Reisner 	 * send/receive buffers off the stack */
896b411b363SPhilipp Reisner 	union p_polymorph sbuf;
897b411b363SPhilipp Reisner 	union p_polymorph rbuf;
898b411b363SPhilipp Reisner };
899b411b363SPhilipp Reisner 
900b411b363SPhilipp Reisner struct drbd_md {
901b411b363SPhilipp Reisner 	u64 md_offset;		/* sector offset to 'super' block */
902b411b363SPhilipp Reisner 
903b411b363SPhilipp Reisner 	u64 la_size_sect;	/* last agreed size, unit sectors */
904b411b363SPhilipp Reisner 	u64 uuid[UI_SIZE];
905b411b363SPhilipp Reisner 	u64 device_uuid;
906b411b363SPhilipp Reisner 	u32 flags;
907b411b363SPhilipp Reisner 	u32 md_size_sect;
908b411b363SPhilipp Reisner 
909b411b363SPhilipp Reisner 	s32 al_offset;	/* signed relative sector offset to al area */
910b411b363SPhilipp Reisner 	s32 bm_offset;	/* signed relative sector offset to bitmap */
911b411b363SPhilipp Reisner 
912b411b363SPhilipp Reisner 	/* u32 al_nr_extents;	   important for restoring the AL
913b411b363SPhilipp Reisner 	 * is stored into  sync_conf.al_extents, which in turn
914b411b363SPhilipp Reisner 	 * gets applied to act_log->nr_elements
915b411b363SPhilipp Reisner 	 */
916b411b363SPhilipp Reisner };
917b411b363SPhilipp Reisner 
918b411b363SPhilipp Reisner /* for sync_conf and other types... */
919b411b363SPhilipp Reisner #define NL_PACKET(name, number, fields) struct name { fields };
920b411b363SPhilipp Reisner #define NL_INTEGER(pn,pr,member) int member;
921b411b363SPhilipp Reisner #define NL_INT64(pn,pr,member) __u64 member;
922b411b363SPhilipp Reisner #define NL_BIT(pn,pr,member)   unsigned member:1;
923b411b363SPhilipp Reisner #define NL_STRING(pn,pr,member,len) unsigned char member[len]; int member ## _len;
924b411b363SPhilipp Reisner #include "linux/drbd_nl.h"
925b411b363SPhilipp Reisner 
926b411b363SPhilipp Reisner struct drbd_backing_dev {
927b411b363SPhilipp Reisner 	struct block_device *backing_bdev;
928b411b363SPhilipp Reisner 	struct block_device *md_bdev;
929b411b363SPhilipp Reisner 	struct drbd_md md;
930b411b363SPhilipp Reisner 	struct disk_conf dc; /* The user provided config... */
931b411b363SPhilipp Reisner 	sector_t known_size; /* last known size of that backing device */
932b411b363SPhilipp Reisner };
933b411b363SPhilipp Reisner 
934b411b363SPhilipp Reisner struct drbd_md_io {
935b411b363SPhilipp Reisner 	struct drbd_conf *mdev;
936b411b363SPhilipp Reisner 	struct completion event;
937b411b363SPhilipp Reisner 	int error;
938b411b363SPhilipp Reisner };
939b411b363SPhilipp Reisner 
940b411b363SPhilipp Reisner struct bm_io_work {
941b411b363SPhilipp Reisner 	struct drbd_work w;
942b411b363SPhilipp Reisner 	char *why;
94320ceb2b2SLars Ellenberg 	enum bm_flag flags;
944b411b363SPhilipp Reisner 	int (*io_fn)(struct drbd_conf *mdev);
945b411b363SPhilipp Reisner 	void (*done)(struct drbd_conf *mdev, int rv);
946b411b363SPhilipp Reisner };
947b411b363SPhilipp Reisner 
948b411b363SPhilipp Reisner enum write_ordering_e {
949b411b363SPhilipp Reisner 	WO_none,
950b411b363SPhilipp Reisner 	WO_drain_io,
951b411b363SPhilipp Reisner 	WO_bdev_flush,
952b411b363SPhilipp Reisner };
953b411b363SPhilipp Reisner 
954778f271dSPhilipp Reisner struct fifo_buffer {
955778f271dSPhilipp Reisner 	int *values;
956778f271dSPhilipp Reisner 	unsigned int head_index;
957778f271dSPhilipp Reisner 	unsigned int size;
958778f271dSPhilipp Reisner };
959778f271dSPhilipp Reisner 
960b411b363SPhilipp Reisner struct drbd_conf {
961b411b363SPhilipp Reisner 	/* things that are stored as / read from meta data on disk */
962b411b363SPhilipp Reisner 	unsigned long flags;
963b411b363SPhilipp Reisner 
964b411b363SPhilipp Reisner 	/* configured by drbdsetup */
965b411b363SPhilipp Reisner 	struct net_conf *net_conf; /* protected by get_net_conf() and put_net_conf() */
966b411b363SPhilipp Reisner 	struct syncer_conf sync_conf;
967b411b363SPhilipp Reisner 	struct drbd_backing_dev *ldev __protected_by(local);
968b411b363SPhilipp Reisner 
969b411b363SPhilipp Reisner 	sector_t p_size;     /* partner's disk size */
970b411b363SPhilipp Reisner 	struct request_queue *rq_queue;
971b411b363SPhilipp Reisner 	struct block_device *this_bdev;
972b411b363SPhilipp Reisner 	struct gendisk	    *vdisk;
973b411b363SPhilipp Reisner 
974b411b363SPhilipp Reisner 	struct drbd_socket data; /* data/barrier/cstate/parameter packets */
975b411b363SPhilipp Reisner 	struct drbd_socket meta; /* ping/ack (metadata) packets */
976b411b363SPhilipp Reisner 	int agreed_pro_version;  /* actually used protocol version */
977b411b363SPhilipp Reisner 	unsigned long last_received; /* in jiffies, either socket */
978b411b363SPhilipp Reisner 	unsigned int ko_count;
979b411b363SPhilipp Reisner 	struct drbd_work  resync_work,
980b411b363SPhilipp Reisner 			  unplug_work,
981e9e6f3ecSLars Ellenberg 			  go_diskless,
982c4752ef1SPhilipp Reisner 			  md_sync_work,
983c4752ef1SPhilipp Reisner 			  start_resync_work;
984b411b363SPhilipp Reisner 	struct timer_list resync_timer;
985b411b363SPhilipp Reisner 	struct timer_list md_sync_timer;
986370a43e7SPhilipp Reisner 	struct timer_list start_resync_timer;
9877fde2be9SPhilipp Reisner 	struct timer_list request_timer;
988ee15b038SLars Ellenberg #ifdef DRBD_DEBUG_MD_SYNC
989ee15b038SLars Ellenberg 	struct {
990ee15b038SLars Ellenberg 		unsigned int line;
991ee15b038SLars Ellenberg 		const char* func;
992ee15b038SLars Ellenberg 	} last_md_mark_dirty;
993ee15b038SLars Ellenberg #endif
994b411b363SPhilipp Reisner 
995b411b363SPhilipp Reisner 	/* Used after attach while negotiating new disk state. */
996b411b363SPhilipp Reisner 	union drbd_state new_state_tmp;
997b411b363SPhilipp Reisner 
998b411b363SPhilipp Reisner 	union drbd_state state;
999b411b363SPhilipp Reisner 	wait_queue_head_t misc_wait;
1000b411b363SPhilipp Reisner 	wait_queue_head_t state_wait;  /* upon each state change. */
100184dfb9f5SPhilipp Reisner 	wait_queue_head_t net_cnt_wait;
1002b411b363SPhilipp Reisner 	unsigned int send_cnt;
1003b411b363SPhilipp Reisner 	unsigned int recv_cnt;
1004b411b363SPhilipp Reisner 	unsigned int read_cnt;
1005b411b363SPhilipp Reisner 	unsigned int writ_cnt;
1006b411b363SPhilipp Reisner 	unsigned int al_writ_cnt;
1007b411b363SPhilipp Reisner 	unsigned int bm_writ_cnt;
1008b411b363SPhilipp Reisner 	atomic_t ap_bio_cnt;	 /* Requests we need to complete */
1009b411b363SPhilipp Reisner 	atomic_t ap_pending_cnt; /* AP data packets on the wire, ack expected */
1010b411b363SPhilipp Reisner 	atomic_t rs_pending_cnt; /* RS request/data packets on the wire */
1011b411b363SPhilipp Reisner 	atomic_t unacked_cnt;	 /* Need to send replys for */
1012b411b363SPhilipp Reisner 	atomic_t local_cnt;	 /* Waiting for local completion */
1013b411b363SPhilipp Reisner 	atomic_t net_cnt;	 /* Users of net_conf */
1014b411b363SPhilipp Reisner 	spinlock_t req_lock;
1015b411b363SPhilipp Reisner 	struct drbd_tl_epoch *unused_spare_tle; /* for pre-allocation */
1016b411b363SPhilipp Reisner 	struct drbd_tl_epoch *newest_tle;
1017b411b363SPhilipp Reisner 	struct drbd_tl_epoch *oldest_tle;
1018b411b363SPhilipp Reisner 	struct list_head out_of_sequence_requests;
1019b411b363SPhilipp Reisner 	struct hlist_head *tl_hash;
1020b411b363SPhilipp Reisner 	unsigned int tl_hash_s;
1021b411b363SPhilipp Reisner 
1022*dac1389cSAndreas Gruenbacher 	/* Interval tree of pending local requests */
1023*dac1389cSAndreas Gruenbacher 	struct rb_root read_requests;
1024de696716SAndreas Gruenbacher 	struct rb_root write_requests;
1025de696716SAndreas Gruenbacher 
10264b0715f0SLars Ellenberg 	/* blocks to resync in this run [unit BM_BLOCK_SIZE] */
1027b411b363SPhilipp Reisner 	unsigned long rs_total;
10284b0715f0SLars Ellenberg 	/* number of resync blocks that failed in this run */
1029b411b363SPhilipp Reisner 	unsigned long rs_failed;
1030b411b363SPhilipp Reisner 	/* Syncer's start time [unit jiffies] */
1031b411b363SPhilipp Reisner 	unsigned long rs_start;
1032b411b363SPhilipp Reisner 	/* cumulated time in PausedSyncX state [unit jiffies] */
1033b411b363SPhilipp Reisner 	unsigned long rs_paused;
10341d7734a0SLars Ellenberg 	/* skipped because csum was equal [unit BM_BLOCK_SIZE] */
1035b411b363SPhilipp Reisner 	unsigned long rs_same_csum;
10361d7734a0SLars Ellenberg #define DRBD_SYNC_MARKS 8
10371d7734a0SLars Ellenberg #define DRBD_SYNC_MARK_STEP (3*HZ)
10381d7734a0SLars Ellenberg 	/* block not up-to-date at mark [unit BM_BLOCK_SIZE] */
10391d7734a0SLars Ellenberg 	unsigned long rs_mark_left[DRBD_SYNC_MARKS];
10401d7734a0SLars Ellenberg 	/* marks's time [unit jiffies] */
10411d7734a0SLars Ellenberg 	unsigned long rs_mark_time[DRBD_SYNC_MARKS];
10421d7734a0SLars Ellenberg 	/* current index into rs_mark_{left,time} */
10431d7734a0SLars Ellenberg 	int rs_last_mark;
1044b411b363SPhilipp Reisner 
1045b411b363SPhilipp Reisner 	/* where does the admin want us to start? (sector) */
1046b411b363SPhilipp Reisner 	sector_t ov_start_sector;
1047b411b363SPhilipp Reisner 	/* where are we now? (sector) */
1048b411b363SPhilipp Reisner 	sector_t ov_position;
1049b411b363SPhilipp Reisner 	/* Start sector of out of sync range (to merge printk reporting). */
1050b411b363SPhilipp Reisner 	sector_t ov_last_oos_start;
1051b411b363SPhilipp Reisner 	/* size of out-of-sync range in sectors. */
1052b411b363SPhilipp Reisner 	sector_t ov_last_oos_size;
1053b411b363SPhilipp Reisner 	unsigned long ov_left; /* in bits */
1054b411b363SPhilipp Reisner 	struct crypto_hash *csums_tfm;
1055b411b363SPhilipp Reisner 	struct crypto_hash *verify_tfm;
1056b411b363SPhilipp Reisner 
1057b411b363SPhilipp Reisner 	struct drbd_thread receiver;
1058b411b363SPhilipp Reisner 	struct drbd_thread worker;
1059b411b363SPhilipp Reisner 	struct drbd_thread asender;
1060b411b363SPhilipp Reisner 	struct drbd_bitmap *bitmap;
1061b411b363SPhilipp Reisner 	unsigned long bm_resync_fo; /* bit offset for drbd_bm_find_next */
1062b411b363SPhilipp Reisner 
1063b411b363SPhilipp Reisner 	/* Used to track operations of resync... */
1064b411b363SPhilipp Reisner 	struct lru_cache *resync;
1065b411b363SPhilipp Reisner 	/* Number of locked elements in resync LRU */
1066b411b363SPhilipp Reisner 	unsigned int resync_locked;
1067b411b363SPhilipp Reisner 	/* resync extent number waiting for application requests */
1068b411b363SPhilipp Reisner 	unsigned int resync_wenr;
1069b411b363SPhilipp Reisner 
1070b411b363SPhilipp Reisner 	int open_cnt;
1071b411b363SPhilipp Reisner 	u64 *p_uuid;
1072b411b363SPhilipp Reisner 	struct drbd_epoch *current_epoch;
1073b411b363SPhilipp Reisner 	spinlock_t epoch_lock;
1074b411b363SPhilipp Reisner 	unsigned int epochs;
1075b411b363SPhilipp Reisner 	enum write_ordering_e write_ordering;
107685719573SPhilipp Reisner 	struct list_head active_ee; /* IO in progress (P_DATA gets written to disk) */
107785719573SPhilipp Reisner 	struct list_head sync_ee;   /* IO in progress (P_RS_DATA_REPLY gets written to disk) */
1078b411b363SPhilipp Reisner 	struct list_head done_ee;   /* send ack */
107985719573SPhilipp Reisner 	struct list_head read_ee;   /* IO in progress (any read) */
1080b411b363SPhilipp Reisner 	struct list_head net_ee;    /* zero-copy network send in progress */
1081b411b363SPhilipp Reisner 	struct hlist_head *ee_hash; /* is proteced by req_lock! */
1082b411b363SPhilipp Reisner 	unsigned int ee_hash_s;
1083b411b363SPhilipp Reisner 
1084b411b363SPhilipp Reisner 	/* this one is protected by ee_lock, single thread */
1085b411b363SPhilipp Reisner 	struct drbd_epoch_entry *last_write_w_barrier;
1086b411b363SPhilipp Reisner 
1087b411b363SPhilipp Reisner 	int next_barrier_nr;
1088b411b363SPhilipp Reisner 	struct hlist_head *app_reads_hash; /* is proteced by req_lock */
1089b411b363SPhilipp Reisner 	struct list_head resync_reads;
1090435f0740SLars Ellenberg 	atomic_t pp_in_use;		/* allocated from page pool */
1091435f0740SLars Ellenberg 	atomic_t pp_in_use_by_net;	/* sendpage()d, still referenced by tcp */
1092b411b363SPhilipp Reisner 	wait_queue_head_t ee_wait;
1093b411b363SPhilipp Reisner 	struct page *md_io_page;	/* one page buffer for md_io */
1094b411b363SPhilipp Reisner 	struct page *md_io_tmpp;	/* for logical_block_size != 512 */
1095b411b363SPhilipp Reisner 	struct mutex md_io_mutex;	/* protects the md_io_buffer */
1096b411b363SPhilipp Reisner 	spinlock_t al_lock;
1097b411b363SPhilipp Reisner 	wait_queue_head_t al_wait;
1098b411b363SPhilipp Reisner 	struct lru_cache *act_log;	/* activity log */
1099b411b363SPhilipp Reisner 	unsigned int al_tr_number;
1100b411b363SPhilipp Reisner 	int al_tr_cycle;
1101b411b363SPhilipp Reisner 	int al_tr_pos;   /* position of the next transaction in the journal */
1102b411b363SPhilipp Reisner 	struct crypto_hash *cram_hmac_tfm;
1103b411b363SPhilipp Reisner 	struct crypto_hash *integrity_w_tfm; /* to be used by the worker thread */
1104b411b363SPhilipp Reisner 	struct crypto_hash *integrity_r_tfm; /* to be used by the receiver thread */
1105b411b363SPhilipp Reisner 	void *int_dig_out;
1106b411b363SPhilipp Reisner 	void *int_dig_in;
1107b411b363SPhilipp Reisner 	void *int_dig_vv;
1108b411b363SPhilipp Reisner 	wait_queue_head_t seq_wait;
1109b411b363SPhilipp Reisner 	atomic_t packet_seq;
1110b411b363SPhilipp Reisner 	unsigned int peer_seq;
1111b411b363SPhilipp Reisner 	spinlock_t peer_seq_lock;
1112b411b363SPhilipp Reisner 	unsigned int minor;
1113b411b363SPhilipp Reisner 	unsigned long comm_bm_set; /* communicated number of set bits. */
1114b411b363SPhilipp Reisner 	cpumask_var_t cpu_mask;
1115b411b363SPhilipp Reisner 	struct bm_io_work bm_io_work;
1116b411b363SPhilipp Reisner 	u64 ed_uuid; /* UUID of the exposed data */
1117b411b363SPhilipp Reisner 	struct mutex state_mutex;
1118b411b363SPhilipp Reisner 	char congestion_reason;  /* Why we where congested... */
11191d7734a0SLars Ellenberg 	atomic_t rs_sect_in; /* for incoming resync data rate, SyncTarget */
11201d7734a0SLars Ellenberg 	atomic_t rs_sect_ev; /* for submitted resync data rate, both */
11211d7734a0SLars Ellenberg 	int rs_last_sect_ev; /* counter to compare with */
11221d7734a0SLars Ellenberg 	int rs_last_events;  /* counter of read or write "events" (unit sectors)
11231d7734a0SLars Ellenberg 			      * on the lower level device when we last looked. */
11241d7734a0SLars Ellenberg 	int c_sync_rate; /* current resync rate after syncer throttle magic */
1125778f271dSPhilipp Reisner 	struct fifo_buffer rs_plan_s; /* correction values of resync planer */
1126778f271dSPhilipp Reisner 	int rs_in_flight; /* resync sectors in flight (to proxy, in proxy and from proxy) */
112725985edcSLucas De Marchi 	int rs_planed;    /* resync sectors already planned */
1128759fbdfbSPhilipp Reisner 	atomic_t ap_in_flight; /* App sectors in flight (waiting for ack) */
112999432fccSPhilipp Reisner 	int peer_max_bio_size;
113099432fccSPhilipp Reisner 	int local_max_bio_size;
1131b411b363SPhilipp Reisner };
1132b411b363SPhilipp Reisner 
1133b411b363SPhilipp Reisner static inline struct drbd_conf *minor_to_mdev(unsigned int minor)
1134b411b363SPhilipp Reisner {
1135b411b363SPhilipp Reisner 	struct drbd_conf *mdev;
1136b411b363SPhilipp Reisner 
1137b411b363SPhilipp Reisner 	mdev = minor < minor_count ? minor_table[minor] : NULL;
1138b411b363SPhilipp Reisner 
1139b411b363SPhilipp Reisner 	return mdev;
1140b411b363SPhilipp Reisner }
1141b411b363SPhilipp Reisner 
1142b411b363SPhilipp Reisner static inline unsigned int mdev_to_minor(struct drbd_conf *mdev)
1143b411b363SPhilipp Reisner {
1144b411b363SPhilipp Reisner 	return mdev->minor;
1145b411b363SPhilipp Reisner }
1146b411b363SPhilipp Reisner 
114725985edcSLucas De Marchi /* returns 1 if it was successful,
1148b411b363SPhilipp Reisner  * returns 0 if there was no data socket.
1149b411b363SPhilipp Reisner  * so wherever you are going to use the data.socket, e.g. do
1150b411b363SPhilipp Reisner  * if (!drbd_get_data_sock(mdev))
1151b411b363SPhilipp Reisner  *	return 0;
1152b411b363SPhilipp Reisner  *	CODE();
1153b411b363SPhilipp Reisner  * drbd_put_data_sock(mdev);
1154b411b363SPhilipp Reisner  */
1155b411b363SPhilipp Reisner static inline int drbd_get_data_sock(struct drbd_conf *mdev)
1156b411b363SPhilipp Reisner {
1157b411b363SPhilipp Reisner 	mutex_lock(&mdev->data.mutex);
1158b411b363SPhilipp Reisner 	/* drbd_disconnect() could have called drbd_free_sock()
1159b411b363SPhilipp Reisner 	 * while we were waiting in down()... */
1160b411b363SPhilipp Reisner 	if (unlikely(mdev->data.socket == NULL)) {
1161b411b363SPhilipp Reisner 		mutex_unlock(&mdev->data.mutex);
1162b411b363SPhilipp Reisner 		return 0;
1163b411b363SPhilipp Reisner 	}
1164b411b363SPhilipp Reisner 	return 1;
1165b411b363SPhilipp Reisner }
1166b411b363SPhilipp Reisner 
1167b411b363SPhilipp Reisner static inline void drbd_put_data_sock(struct drbd_conf *mdev)
1168b411b363SPhilipp Reisner {
1169b411b363SPhilipp Reisner 	mutex_unlock(&mdev->data.mutex);
1170b411b363SPhilipp Reisner }
1171b411b363SPhilipp Reisner 
1172b411b363SPhilipp Reisner /*
1173b411b363SPhilipp Reisner  * function declarations
1174b411b363SPhilipp Reisner  *************************/
1175b411b363SPhilipp Reisner 
1176b411b363SPhilipp Reisner /* drbd_main.c */
1177b411b363SPhilipp Reisner 
1178b411b363SPhilipp Reisner enum chg_state_flags {
1179b411b363SPhilipp Reisner 	CS_HARD	= 1,
1180b411b363SPhilipp Reisner 	CS_VERBOSE = 2,
1181b411b363SPhilipp Reisner 	CS_WAIT_COMPLETE = 4,
1182b411b363SPhilipp Reisner 	CS_SERIALIZE    = 8,
1183b411b363SPhilipp Reisner 	CS_ORDERED      = CS_WAIT_COMPLETE + CS_SERIALIZE,
1184b411b363SPhilipp Reisner };
1185b411b363SPhilipp Reisner 
1186e89b591cSPhilipp Reisner enum dds_flags {
1187e89b591cSPhilipp Reisner 	DDSF_FORCED    = 1,
1188e89b591cSPhilipp Reisner 	DDSF_NO_RESYNC = 2, /* Do not run a resync for the new space */
1189e89b591cSPhilipp Reisner };
1190e89b591cSPhilipp Reisner 
1191b411b363SPhilipp Reisner extern void drbd_init_set_defaults(struct drbd_conf *mdev);
1192bf885f8aSAndreas Gruenbacher extern enum drbd_state_rv drbd_change_state(struct drbd_conf *mdev,
1193bf885f8aSAndreas Gruenbacher 					    enum chg_state_flags f,
1194bf885f8aSAndreas Gruenbacher 					    union drbd_state mask,
1195bf885f8aSAndreas Gruenbacher 					    union drbd_state val);
1196b411b363SPhilipp Reisner extern void drbd_force_state(struct drbd_conf *, union drbd_state,
1197b411b363SPhilipp Reisner 			union drbd_state);
1198bf885f8aSAndreas Gruenbacher extern enum drbd_state_rv _drbd_request_state(struct drbd_conf *,
1199bf885f8aSAndreas Gruenbacher 					      union drbd_state,
1200bf885f8aSAndreas Gruenbacher 					      union drbd_state,
1201bf885f8aSAndreas Gruenbacher 					      enum chg_state_flags);
1202bf885f8aSAndreas Gruenbacher extern enum drbd_state_rv __drbd_set_state(struct drbd_conf *, union drbd_state,
1203bf885f8aSAndreas Gruenbacher 					   enum chg_state_flags,
1204bf885f8aSAndreas Gruenbacher 					   struct completion *done);
1205b411b363SPhilipp Reisner extern void print_st_err(struct drbd_conf *, union drbd_state,
1206b411b363SPhilipp Reisner 			union drbd_state, int);
1207b411b363SPhilipp Reisner extern int  drbd_thread_start(struct drbd_thread *thi);
1208b411b363SPhilipp Reisner extern void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait);
1209b411b363SPhilipp Reisner #ifdef CONFIG_SMP
1210b411b363SPhilipp Reisner extern void drbd_thread_current_set_cpu(struct drbd_conf *mdev);
1211b411b363SPhilipp Reisner extern void drbd_calc_cpu_mask(struct drbd_conf *mdev);
1212b411b363SPhilipp Reisner #else
1213b411b363SPhilipp Reisner #define drbd_thread_current_set_cpu(A) ({})
1214b411b363SPhilipp Reisner #define drbd_calc_cpu_mask(A) ({})
1215b411b363SPhilipp Reisner #endif
1216b411b363SPhilipp Reisner extern void drbd_free_resources(struct drbd_conf *mdev);
1217b411b363SPhilipp Reisner extern void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
1218b411b363SPhilipp Reisner 		       unsigned int set_size);
1219b411b363SPhilipp Reisner extern void tl_clear(struct drbd_conf *mdev);
1220b411b363SPhilipp Reisner extern void _tl_add_barrier(struct drbd_conf *, struct drbd_tl_epoch *);
1221b411b363SPhilipp Reisner extern void drbd_free_sock(struct drbd_conf *mdev);
1222b411b363SPhilipp Reisner extern int drbd_send(struct drbd_conf *mdev, struct socket *sock,
1223b411b363SPhilipp Reisner 			void *buf, size_t size, unsigned msg_flags);
1224b411b363SPhilipp Reisner extern int drbd_send_protocol(struct drbd_conf *mdev);
1225b411b363SPhilipp Reisner extern int drbd_send_uuids(struct drbd_conf *mdev);
1226b411b363SPhilipp Reisner extern int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev);
12275a22db89SLars Ellenberg extern int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev);
1228e89b591cSPhilipp Reisner extern int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags);
1229b411b363SPhilipp Reisner extern int _drbd_send_state(struct drbd_conf *mdev);
1230b411b363SPhilipp Reisner extern int drbd_send_state(struct drbd_conf *mdev);
1231b411b363SPhilipp Reisner extern int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
12320b70a13dSPhilipp Reisner 			enum drbd_packets cmd, struct p_header80 *h,
1233b411b363SPhilipp Reisner 			size_t size, unsigned msg_flags);
1234b411b363SPhilipp Reisner #define USE_DATA_SOCKET 1
1235b411b363SPhilipp Reisner #define USE_META_SOCKET 0
1236b411b363SPhilipp Reisner extern int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
12370b70a13dSPhilipp Reisner 			enum drbd_packets cmd, struct p_header80 *h,
1238b411b363SPhilipp Reisner 			size_t size);
1239b411b363SPhilipp Reisner extern int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packets cmd,
1240b411b363SPhilipp Reisner 			char *data, size_t size);
1241b411b363SPhilipp Reisner extern int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc);
1242b411b363SPhilipp Reisner extern int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr,
1243b411b363SPhilipp Reisner 			u32 set_size);
1244b411b363SPhilipp Reisner extern int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packets cmd,
1245b411b363SPhilipp Reisner 			struct drbd_epoch_entry *e);
1246b411b363SPhilipp Reisner extern int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packets cmd,
1247b411b363SPhilipp Reisner 			struct p_block_req *rp);
1248b411b363SPhilipp Reisner extern int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packets cmd,
12492b2bf214SLars Ellenberg 			struct p_data *dp, int data_size);
1250b411b363SPhilipp Reisner extern int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packets cmd,
1251b411b363SPhilipp Reisner 			    sector_t sector, int blksize, u64 block_id);
125273a01a18SPhilipp Reisner extern int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req);
1253b411b363SPhilipp Reisner extern int drbd_send_block(struct drbd_conf *mdev, enum drbd_packets cmd,
1254b411b363SPhilipp Reisner 			   struct drbd_epoch_entry *e);
1255b411b363SPhilipp Reisner extern int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req);
1256b411b363SPhilipp Reisner extern int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1257b411b363SPhilipp Reisner 			      sector_t sector, int size, u64 block_id);
1258b411b363SPhilipp Reisner extern int drbd_send_drequest_csum(struct drbd_conf *mdev,
1259b411b363SPhilipp Reisner 				   sector_t sector,int size,
1260b411b363SPhilipp Reisner 				   void *digest, int digest_size,
1261b411b363SPhilipp Reisner 				   enum drbd_packets cmd);
1262b411b363SPhilipp Reisner extern int drbd_send_ov_request(struct drbd_conf *mdev,sector_t sector,int size);
1263b411b363SPhilipp Reisner 
1264b411b363SPhilipp Reisner extern int drbd_send_bitmap(struct drbd_conf *mdev);
1265b411b363SPhilipp Reisner extern int _drbd_send_bitmap(struct drbd_conf *mdev);
1266bf885f8aSAndreas Gruenbacher extern int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode);
1267b411b363SPhilipp Reisner extern void drbd_free_bc(struct drbd_backing_dev *ldev);
1268b411b363SPhilipp Reisner extern void drbd_mdev_cleanup(struct drbd_conf *mdev);
126962b0da3aSLars Ellenberg void drbd_print_uuids(struct drbd_conf *mdev, const char *text);
1270b411b363SPhilipp Reisner 
1271b411b363SPhilipp Reisner extern void drbd_md_sync(struct drbd_conf *mdev);
1272b411b363SPhilipp Reisner extern int  drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev);
1273b411b363SPhilipp Reisner extern void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1274b411b363SPhilipp Reisner extern void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1275b411b363SPhilipp Reisner extern void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1276b411b363SPhilipp Reisner extern void _drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1277b411b363SPhilipp Reisner extern void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local);
1278b411b363SPhilipp Reisner extern void drbd_md_set_flag(struct drbd_conf *mdev, int flags) __must_hold(local);
1279b411b363SPhilipp Reisner extern void drbd_md_clear_flag(struct drbd_conf *mdev, int flags)__must_hold(local);
1280b411b363SPhilipp Reisner extern int drbd_md_test_flag(struct drbd_backing_dev *, int);
1281ee15b038SLars Ellenberg #ifndef DRBD_DEBUG_MD_SYNC
1282b411b363SPhilipp Reisner extern void drbd_md_mark_dirty(struct drbd_conf *mdev);
1283ee15b038SLars Ellenberg #else
1284ee15b038SLars Ellenberg #define drbd_md_mark_dirty(m)	drbd_md_mark_dirty_(m, __LINE__ , __func__ )
1285ee15b038SLars Ellenberg extern void drbd_md_mark_dirty_(struct drbd_conf *mdev,
1286ee15b038SLars Ellenberg 		unsigned int line, const char *func);
1287ee15b038SLars Ellenberg #endif
1288b411b363SPhilipp Reisner extern void drbd_queue_bitmap_io(struct drbd_conf *mdev,
1289b411b363SPhilipp Reisner 				 int (*io_fn)(struct drbd_conf *),
1290b411b363SPhilipp Reisner 				 void (*done)(struct drbd_conf *, int),
129120ceb2b2SLars Ellenberg 				 char *why, enum bm_flag flags);
129220ceb2b2SLars Ellenberg extern int drbd_bitmap_io(struct drbd_conf *mdev,
129320ceb2b2SLars Ellenberg 		int (*io_fn)(struct drbd_conf *),
129420ceb2b2SLars Ellenberg 		char *why, enum bm_flag flags);
1295b411b363SPhilipp Reisner extern int drbd_bmio_set_n_write(struct drbd_conf *mdev);
1296b411b363SPhilipp Reisner extern int drbd_bmio_clear_n_write(struct drbd_conf *mdev);
1297e9e6f3ecSLars Ellenberg extern void drbd_go_diskless(struct drbd_conf *mdev);
129882f59cc6SLars Ellenberg extern void drbd_ldev_destroy(struct drbd_conf *mdev);
1299b411b363SPhilipp Reisner 
1300b411b363SPhilipp Reisner 
1301b411b363SPhilipp Reisner /* Meta data layout
1302b411b363SPhilipp Reisner    We reserve a 128MB Block (4k aligned)
1303b411b363SPhilipp Reisner    * either at the end of the backing device
13043ad2f3fbSDaniel Mack    * or on a separate meta data device. */
1305b411b363SPhilipp Reisner 
1306b411b363SPhilipp Reisner #define MD_RESERVED_SECT (128LU << 11)  /* 128 MB, unit sectors */
1307b411b363SPhilipp Reisner /* The following numbers are sectors */
1308b411b363SPhilipp Reisner #define MD_AL_OFFSET 8	    /* 8 Sectors after start of meta area */
1309b411b363SPhilipp Reisner #define MD_AL_MAX_SIZE 64   /* = 32 kb LOG  ~ 3776 extents ~ 14 GB Storage */
1310b411b363SPhilipp Reisner /* Allows up to about 3.8TB */
1311b411b363SPhilipp Reisner #define MD_BM_OFFSET (MD_AL_OFFSET + MD_AL_MAX_SIZE)
1312b411b363SPhilipp Reisner 
1313b411b363SPhilipp Reisner /* Since the smalles IO unit is usually 512 byte */
1314b411b363SPhilipp Reisner #define MD_SECTOR_SHIFT	 9
1315b411b363SPhilipp Reisner #define MD_SECTOR_SIZE	 (1<<MD_SECTOR_SHIFT)
1316b411b363SPhilipp Reisner 
1317b411b363SPhilipp Reisner /* activity log */
1318b411b363SPhilipp Reisner #define AL_EXTENTS_PT ((MD_SECTOR_SIZE-12)/8-1) /* 61 ; Extents per 512B sector */
1319b411b363SPhilipp Reisner #define AL_EXTENT_SHIFT 22		 /* One extent represents 4M Storage */
1320b411b363SPhilipp Reisner #define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
1321b411b363SPhilipp Reisner 
1322b411b363SPhilipp Reisner #if BITS_PER_LONG == 32
1323b411b363SPhilipp Reisner #define LN2_BPL 5
1324b411b363SPhilipp Reisner #define cpu_to_lel(A) cpu_to_le32(A)
1325b411b363SPhilipp Reisner #define lel_to_cpu(A) le32_to_cpu(A)
1326b411b363SPhilipp Reisner #elif BITS_PER_LONG == 64
1327b411b363SPhilipp Reisner #define LN2_BPL 6
1328b411b363SPhilipp Reisner #define cpu_to_lel(A) cpu_to_le64(A)
1329b411b363SPhilipp Reisner #define lel_to_cpu(A) le64_to_cpu(A)
1330b411b363SPhilipp Reisner #else
1331b411b363SPhilipp Reisner #error "LN2 of BITS_PER_LONG unknown!"
1332b411b363SPhilipp Reisner #endif
1333b411b363SPhilipp Reisner 
1334b411b363SPhilipp Reisner /* resync bitmap */
1335b411b363SPhilipp Reisner /* 16MB sized 'bitmap extent' to track syncer usage */
1336b411b363SPhilipp Reisner struct bm_extent {
1337b411b363SPhilipp Reisner 	int rs_left; /* number of bits set (out of sync) in this extent. */
1338b411b363SPhilipp Reisner 	int rs_failed; /* number of failed resync requests in this extent. */
1339b411b363SPhilipp Reisner 	unsigned long flags;
1340b411b363SPhilipp Reisner 	struct lc_element lce;
1341b411b363SPhilipp Reisner };
1342b411b363SPhilipp Reisner 
1343b411b363SPhilipp Reisner #define BME_NO_WRITES  0  /* bm_extent.flags: no more requests on this one! */
1344b411b363SPhilipp Reisner #define BME_LOCKED     1  /* bm_extent.flags: syncer active on this one. */
1345e3555d85SPhilipp Reisner #define BME_PRIORITY   2  /* finish resync IO on this extent ASAP! App IO waiting! */
1346b411b363SPhilipp Reisner 
1347b411b363SPhilipp Reisner /* drbd_bitmap.c */
1348b411b363SPhilipp Reisner /*
1349b411b363SPhilipp Reisner  * We need to store one bit for a block.
1350b411b363SPhilipp Reisner  * Example: 1GB disk @ 4096 byte blocks ==> we need 32 KB bitmap.
1351b411b363SPhilipp Reisner  * Bit 0 ==> local node thinks this block is binary identical on both nodes
1352b411b363SPhilipp Reisner  * Bit 1 ==> local node thinks this block needs to be synced.
1353b411b363SPhilipp Reisner  */
1354b411b363SPhilipp Reisner 
13558e26f9ccSPhilipp Reisner #define SLEEP_TIME (HZ/10)
13568e26f9ccSPhilipp Reisner 
1357b411b363SPhilipp Reisner #define BM_BLOCK_SHIFT  12			 /* 4k per bit */
1358b411b363SPhilipp Reisner #define BM_BLOCK_SIZE	 (1<<BM_BLOCK_SHIFT)
1359b411b363SPhilipp Reisner /* (9+3) : 512 bytes @ 8 bits; representing 16M storage
1360b411b363SPhilipp Reisner  * per sector of on disk bitmap */
1361b411b363SPhilipp Reisner #define BM_EXT_SHIFT	 (BM_BLOCK_SHIFT + MD_SECTOR_SHIFT + 3)  /* = 24 */
1362b411b363SPhilipp Reisner #define BM_EXT_SIZE	 (1<<BM_EXT_SHIFT)
1363b411b363SPhilipp Reisner 
1364b411b363SPhilipp Reisner #if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
1365b411b363SPhilipp Reisner #error "HAVE YOU FIXED drbdmeta AS WELL??"
1366b411b363SPhilipp Reisner #endif
1367b411b363SPhilipp Reisner 
1368b411b363SPhilipp Reisner /* thus many _storage_ sectors are described by one bit */
1369b411b363SPhilipp Reisner #define BM_SECT_TO_BIT(x)   ((x)>>(BM_BLOCK_SHIFT-9))
1370b411b363SPhilipp Reisner #define BM_BIT_TO_SECT(x)   ((sector_t)(x)<<(BM_BLOCK_SHIFT-9))
1371b411b363SPhilipp Reisner #define BM_SECT_PER_BIT     BM_BIT_TO_SECT(1)
1372b411b363SPhilipp Reisner 
1373b411b363SPhilipp Reisner /* bit to represented kilo byte conversion */
1374b411b363SPhilipp Reisner #define Bit2KB(bits) ((bits)<<(BM_BLOCK_SHIFT-10))
1375b411b363SPhilipp Reisner 
1376b411b363SPhilipp Reisner /* in which _bitmap_ extent (resp. sector) the bit for a certain
1377b411b363SPhilipp Reisner  * _storage_ sector is located in */
1378b411b363SPhilipp Reisner #define BM_SECT_TO_EXT(x)   ((x)>>(BM_EXT_SHIFT-9))
1379b411b363SPhilipp Reisner 
1380b411b363SPhilipp Reisner /* how much _storage_ sectors we have per bitmap sector */
1381b411b363SPhilipp Reisner #define BM_EXT_TO_SECT(x)   ((sector_t)(x) << (BM_EXT_SHIFT-9))
1382b411b363SPhilipp Reisner #define BM_SECT_PER_EXT     BM_EXT_TO_SECT(1)
1383b411b363SPhilipp Reisner 
1384b411b363SPhilipp Reisner /* in one sector of the bitmap, we have this many activity_log extents. */
1385b411b363SPhilipp Reisner #define AL_EXT_PER_BM_SECT  (1 << (BM_EXT_SHIFT - AL_EXTENT_SHIFT))
1386b411b363SPhilipp Reisner #define BM_WORDS_PER_AL_EXT (1 << (AL_EXTENT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
1387b411b363SPhilipp Reisner 
1388b411b363SPhilipp Reisner #define BM_BLOCKS_PER_BM_EXT_B (BM_EXT_SHIFT - BM_BLOCK_SHIFT)
1389b411b363SPhilipp Reisner #define BM_BLOCKS_PER_BM_EXT_MASK  ((1<<BM_BLOCKS_PER_BM_EXT_B) - 1)
1390b411b363SPhilipp Reisner 
1391b411b363SPhilipp Reisner /* the extent in "PER_EXTENT" below is an activity log extent
1392b411b363SPhilipp Reisner  * we need that many (long words/bytes) to store the bitmap
1393b411b363SPhilipp Reisner  *		     of one AL_EXTENT_SIZE chunk of storage.
1394b411b363SPhilipp Reisner  * we can store the bitmap for that many AL_EXTENTS within
1395b411b363SPhilipp Reisner  * one sector of the _on_disk_ bitmap:
1396b411b363SPhilipp Reisner  * bit	 0	  bit 37   bit 38	     bit (512*8)-1
1397b411b363SPhilipp Reisner  *	     ...|........|........|.. // ..|........|
1398b411b363SPhilipp Reisner  * sect. 0	 `296	  `304			   ^(512*8*8)-1
1399b411b363SPhilipp Reisner  *
1400b411b363SPhilipp Reisner #define BM_WORDS_PER_EXT    ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / BITS_PER_LONG )
1401b411b363SPhilipp Reisner #define BM_BYTES_PER_EXT    ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / 8 )  // 128
1402b411b363SPhilipp Reisner #define BM_EXT_PER_SECT	    ( 512 / BM_BYTES_PER_EXTENT )	 //   4
1403b411b363SPhilipp Reisner  */
1404b411b363SPhilipp Reisner 
1405b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS_32 (0xffffffffLU)
1406b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS_BM \
1407b411b363SPhilipp Reisner 	  ((MD_RESERVED_SECT - MD_BM_OFFSET) * (1LL<<(BM_EXT_SHIFT-9)))
1408b411b363SPhilipp Reisner #if DRBD_MAX_SECTORS_BM < DRBD_MAX_SECTORS_32
1409b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS      DRBD_MAX_SECTORS_BM
1410b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_BM
141136bfc7e2SLars Ellenberg #elif !defined(CONFIG_LBDAF) && BITS_PER_LONG == 32
1412b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS      DRBD_MAX_SECTORS_32
1413b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_32
1414b411b363SPhilipp Reisner #else
1415b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS      DRBD_MAX_SECTORS_BM
1416b411b363SPhilipp Reisner /* 16 TB in units of sectors */
1417b411b363SPhilipp Reisner #if BITS_PER_LONG == 32
1418b411b363SPhilipp Reisner /* adjust by one page worth of bitmap,
1419b411b363SPhilipp Reisner  * so we won't wrap around in drbd_bm_find_next_bit.
1420b411b363SPhilipp Reisner  * you should use 64bit OS for that much storage, anyways. */
1421b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS_FLEX BM_BIT_TO_SECT(0xffff7fff)
1422b411b363SPhilipp Reisner #else
14234b0715f0SLars Ellenberg /* we allow up to 1 PiB now on 64bit architecture with "flexible" meta data */
14244b0715f0SLars Ellenberg #define DRBD_MAX_SECTORS_FLEX (1UL << 51)
14254b0715f0SLars Ellenberg /* corresponds to (1UL << 38) bits right now. */
1426b411b363SPhilipp Reisner #endif
1427b411b363SPhilipp Reisner #endif
1428b411b363SPhilipp Reisner 
1429b411b363SPhilipp Reisner /* Sector shift value for the "hash" functions of tl_hash and ee_hash tables.
1430d5373389SPhilipp Reisner  * With a value of 8 all IO in one 128K block make it to the same slot of the
1431b411b363SPhilipp Reisner  * hash table. */
1432d5373389SPhilipp Reisner #define HT_SHIFT 8
14331816a2b4SLars Ellenberg #define DRBD_MAX_BIO_SIZE (1U<<(9+HT_SHIFT))
143499432fccSPhilipp Reisner #define DRBD_MAX_BIO_SIZE_SAFE (1 << 12)       /* Works always = 4k */
1435b411b363SPhilipp Reisner 
1436d5373389SPhilipp Reisner #define DRBD_MAX_SIZE_H80_PACKET (1 << 15) /* The old header only allows packets up to 32Kib data */
1437d5373389SPhilipp Reisner 
1438b411b363SPhilipp Reisner /* Number of elements in the app_reads_hash */
1439b411b363SPhilipp Reisner #define APP_R_HSIZE 15
1440b411b363SPhilipp Reisner 
1441b411b363SPhilipp Reisner extern int  drbd_bm_init(struct drbd_conf *mdev);
144202d9a94bSPhilipp Reisner extern int  drbd_bm_resize(struct drbd_conf *mdev, sector_t sectors, int set_new_bits);
1443b411b363SPhilipp Reisner extern void drbd_bm_cleanup(struct drbd_conf *mdev);
1444b411b363SPhilipp Reisner extern void drbd_bm_set_all(struct drbd_conf *mdev);
1445b411b363SPhilipp Reisner extern void drbd_bm_clear_all(struct drbd_conf *mdev);
14464b0715f0SLars Ellenberg /* set/clear/test only a few bits at a time */
1447b411b363SPhilipp Reisner extern int  drbd_bm_set_bits(
1448b411b363SPhilipp Reisner 		struct drbd_conf *mdev, unsigned long s, unsigned long e);
1449b411b363SPhilipp Reisner extern int  drbd_bm_clear_bits(
1450b411b363SPhilipp Reisner 		struct drbd_conf *mdev, unsigned long s, unsigned long e);
14514b0715f0SLars Ellenberg extern int drbd_bm_count_bits(
14524b0715f0SLars Ellenberg 	struct drbd_conf *mdev, const unsigned long s, const unsigned long e);
14534b0715f0SLars Ellenberg /* bm_set_bits variant for use while holding drbd_bm_lock,
14544b0715f0SLars Ellenberg  * may process the whole bitmap in one go */
1455b411b363SPhilipp Reisner extern void _drbd_bm_set_bits(struct drbd_conf *mdev,
1456b411b363SPhilipp Reisner 		const unsigned long s, const unsigned long e);
1457b411b363SPhilipp Reisner extern int  drbd_bm_test_bit(struct drbd_conf *mdev, unsigned long bitnr);
1458b411b363SPhilipp Reisner extern int  drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr);
145919f843aaSLars Ellenberg extern int  drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local);
1460b411b363SPhilipp Reisner extern int  drbd_bm_read(struct drbd_conf *mdev) __must_hold(local);
1461b411b363SPhilipp Reisner extern int  drbd_bm_write(struct drbd_conf *mdev) __must_hold(local);
1462b411b363SPhilipp Reisner extern unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev,
1463b411b363SPhilipp Reisner 		unsigned long al_enr);
1464b411b363SPhilipp Reisner extern size_t	     drbd_bm_words(struct drbd_conf *mdev);
1465b411b363SPhilipp Reisner extern unsigned long drbd_bm_bits(struct drbd_conf *mdev);
1466b411b363SPhilipp Reisner extern sector_t      drbd_bm_capacity(struct drbd_conf *mdev);
14674b0715f0SLars Ellenberg 
14684b0715f0SLars Ellenberg #define DRBD_END_OF_BITMAP	(~(unsigned long)0)
1469b411b363SPhilipp Reisner extern unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1470b411b363SPhilipp Reisner /* bm_find_next variants for use while you hold drbd_bm_lock() */
1471b411b363SPhilipp Reisner extern unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1472b411b363SPhilipp Reisner extern unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo);
14730778286aSPhilipp Reisner extern unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev);
1474b411b363SPhilipp Reisner extern unsigned long drbd_bm_total_weight(struct drbd_conf *mdev);
1475b411b363SPhilipp Reisner extern int drbd_bm_rs_done(struct drbd_conf *mdev);
1476b411b363SPhilipp Reisner /* for receive_bitmap */
1477b411b363SPhilipp Reisner extern void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset,
1478b411b363SPhilipp Reisner 		size_t number, unsigned long *buffer);
147919f843aaSLars Ellenberg /* for _drbd_send_bitmap */
1480b411b363SPhilipp Reisner extern void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset,
1481b411b363SPhilipp Reisner 		size_t number, unsigned long *buffer);
1482b411b363SPhilipp Reisner 
148320ceb2b2SLars Ellenberg extern void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags);
1484b411b363SPhilipp Reisner extern void drbd_bm_unlock(struct drbd_conf *mdev);
1485b411b363SPhilipp Reisner /* drbd_main.c */
1486b411b363SPhilipp Reisner 
1487b411b363SPhilipp Reisner extern struct kmem_cache *drbd_request_cache;
1488b411b363SPhilipp Reisner extern struct kmem_cache *drbd_ee_cache;	/* epoch entries */
1489b411b363SPhilipp Reisner extern struct kmem_cache *drbd_bm_ext_cache;	/* bitmap extents */
1490b411b363SPhilipp Reisner extern struct kmem_cache *drbd_al_ext_cache;	/* activity log extents */
1491b411b363SPhilipp Reisner extern mempool_t *drbd_request_mempool;
1492b411b363SPhilipp Reisner extern mempool_t *drbd_ee_mempool;
1493b411b363SPhilipp Reisner 
1494b411b363SPhilipp Reisner extern struct page *drbd_pp_pool; /* drbd's page pool */
1495b411b363SPhilipp Reisner extern spinlock_t   drbd_pp_lock;
1496b411b363SPhilipp Reisner extern int	    drbd_pp_vacant;
1497b411b363SPhilipp Reisner extern wait_queue_head_t drbd_pp_wait;
1498b411b363SPhilipp Reisner 
1499b411b363SPhilipp Reisner extern rwlock_t global_state_lock;
1500b411b363SPhilipp Reisner 
1501b411b363SPhilipp Reisner extern struct drbd_conf *drbd_new_device(unsigned int minor);
1502b411b363SPhilipp Reisner extern void drbd_free_mdev(struct drbd_conf *mdev);
1503b411b363SPhilipp Reisner 
1504b411b363SPhilipp Reisner extern int proc_details;
1505b411b363SPhilipp Reisner 
1506b411b363SPhilipp Reisner /* drbd_req */
15072f58dcfcSAndreas Gruenbacher extern int drbd_make_request(struct request_queue *q, struct bio *bio);
1508b411b363SPhilipp Reisner extern int drbd_read_remote(struct drbd_conf *mdev, struct drbd_request *req);
1509b411b363SPhilipp Reisner extern int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec);
1510b411b363SPhilipp Reisner extern int is_valid_ar_handle(struct drbd_request *, sector_t);
1511b411b363SPhilipp Reisner 
1512b411b363SPhilipp Reisner 
1513b411b363SPhilipp Reisner /* drbd_nl.c */
1514b411b363SPhilipp Reisner extern void drbd_suspend_io(struct drbd_conf *mdev);
1515b411b363SPhilipp Reisner extern void drbd_resume_io(struct drbd_conf *mdev);
1516b411b363SPhilipp Reisner extern char *ppsize(char *buf, unsigned long long size);
1517a393db6fSPhilipp Reisner extern sector_t drbd_new_dev_size(struct drbd_conf *, struct drbd_backing_dev *, int);
1518b411b363SPhilipp Reisner enum determine_dev_size { dev_size_error = -1, unchanged = 0, shrunk = 1, grew = 2 };
151924c4830cSBart Van Assche extern enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *, enum dds_flags) __must_hold(local);
1520b411b363SPhilipp Reisner extern void resync_after_online_grow(struct drbd_conf *);
152199432fccSPhilipp Reisner extern void drbd_reconsider_max_bio_size(struct drbd_conf *mdev);
1522bf885f8aSAndreas Gruenbacher extern enum drbd_state_rv drbd_set_role(struct drbd_conf *mdev,
1523bf885f8aSAndreas Gruenbacher 					enum drbd_role new_role,
1524b411b363SPhilipp Reisner 					int force);
152587f7be4cSPhilipp Reisner extern enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev);
152687f7be4cSPhilipp Reisner extern void drbd_try_outdate_peer_async(struct drbd_conf *mdev);
1527b411b363SPhilipp Reisner extern int drbd_khelper(struct drbd_conf *mdev, char *cmd);
1528b411b363SPhilipp Reisner 
1529b411b363SPhilipp Reisner /* drbd_worker.c */
1530b411b363SPhilipp Reisner extern int drbd_worker(struct drbd_thread *thi);
1531b411b363SPhilipp Reisner extern int drbd_alter_sa(struct drbd_conf *mdev, int na);
1532b411b363SPhilipp Reisner extern void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side);
1533b411b363SPhilipp Reisner extern void resume_next_sg(struct drbd_conf *mdev);
1534b411b363SPhilipp Reisner extern void suspend_other_sg(struct drbd_conf *mdev);
1535b411b363SPhilipp Reisner extern int drbd_resync_finished(struct drbd_conf *mdev);
1536b411b363SPhilipp Reisner /* maybe rather drbd_main.c ? */
1537b411b363SPhilipp Reisner extern int drbd_md_sync_page_io(struct drbd_conf *mdev,
1538b411b363SPhilipp Reisner 		struct drbd_backing_dev *bdev, sector_t sector, int rw);
1539b411b363SPhilipp Reisner extern void drbd_ov_oos_found(struct drbd_conf*, sector_t, int);
15409bd28d3cSLars Ellenberg extern void drbd_rs_controller_reset(struct drbd_conf *mdev);
1541b411b363SPhilipp Reisner 
1542b411b363SPhilipp Reisner static inline void ov_oos_print(struct drbd_conf *mdev)
1543b411b363SPhilipp Reisner {
1544b411b363SPhilipp Reisner 	if (mdev->ov_last_oos_size) {
1545b411b363SPhilipp Reisner 		dev_err(DEV, "Out of sync: start=%llu, size=%lu (sectors)\n",
1546b411b363SPhilipp Reisner 		     (unsigned long long)mdev->ov_last_oos_start,
1547b411b363SPhilipp Reisner 		     (unsigned long)mdev->ov_last_oos_size);
1548b411b363SPhilipp Reisner 	}
1549b411b363SPhilipp Reisner 	mdev->ov_last_oos_size=0;
1550b411b363SPhilipp Reisner }
1551b411b363SPhilipp Reisner 
1552b411b363SPhilipp Reisner 
155345bb912bSLars Ellenberg extern void drbd_csum_bio(struct drbd_conf *, struct crypto_hash *, struct bio *, void *);
155445bb912bSLars Ellenberg extern void drbd_csum_ee(struct drbd_conf *, struct crypto_hash *, struct drbd_epoch_entry *, void *);
1555b411b363SPhilipp Reisner /* worker callbacks */
1556b411b363SPhilipp Reisner extern int w_req_cancel_conflict(struct drbd_conf *, struct drbd_work *, int);
1557b411b363SPhilipp Reisner extern int w_read_retry_remote(struct drbd_conf *, struct drbd_work *, int);
1558b411b363SPhilipp Reisner extern int w_e_end_data_req(struct drbd_conf *, struct drbd_work *, int);
1559b411b363SPhilipp Reisner extern int w_e_end_rsdata_req(struct drbd_conf *, struct drbd_work *, int);
1560b411b363SPhilipp Reisner extern int w_e_end_csum_rs_req(struct drbd_conf *, struct drbd_work *, int);
1561b411b363SPhilipp Reisner extern int w_e_end_ov_reply(struct drbd_conf *, struct drbd_work *, int);
1562b411b363SPhilipp Reisner extern int w_e_end_ov_req(struct drbd_conf *, struct drbd_work *, int);
1563b411b363SPhilipp Reisner extern int w_ov_finished(struct drbd_conf *, struct drbd_work *, int);
1564794abb75SPhilipp Reisner extern int w_resync_timer(struct drbd_conf *, struct drbd_work *, int);
1565b411b363SPhilipp Reisner extern int w_resume_next_sg(struct drbd_conf *, struct drbd_work *, int);
1566b411b363SPhilipp Reisner extern int w_send_write_hint(struct drbd_conf *, struct drbd_work *, int);
1567b411b363SPhilipp Reisner extern int w_send_dblock(struct drbd_conf *, struct drbd_work *, int);
1568b411b363SPhilipp Reisner extern int w_send_barrier(struct drbd_conf *, struct drbd_work *, int);
1569b411b363SPhilipp Reisner extern int w_send_read_req(struct drbd_conf *, struct drbd_work *, int);
1570b411b363SPhilipp Reisner extern int w_prev_work_done(struct drbd_conf *, struct drbd_work *, int);
1571b411b363SPhilipp Reisner extern int w_e_reissue(struct drbd_conf *, struct drbd_work *, int);
1572265be2d0SPhilipp Reisner extern int w_restart_disk_io(struct drbd_conf *, struct drbd_work *, int);
157373a01a18SPhilipp Reisner extern int w_send_oos(struct drbd_conf *, struct drbd_work *, int);
1574c4752ef1SPhilipp Reisner extern int w_start_resync(struct drbd_conf *, struct drbd_work *, int);
1575b411b363SPhilipp Reisner 
1576b411b363SPhilipp Reisner extern void resync_timer_fn(unsigned long data);
1577370a43e7SPhilipp Reisner extern void start_resync_timer_fn(unsigned long data);
1578b411b363SPhilipp Reisner 
1579b411b363SPhilipp Reisner /* drbd_receiver.c */
1580e3555d85SPhilipp Reisner extern int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector);
158145bb912bSLars Ellenberg extern int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
158245bb912bSLars Ellenberg 		const unsigned rw, const int fault_type);
1583b411b363SPhilipp Reisner extern int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list);
1584b411b363SPhilipp Reisner extern struct drbd_epoch_entry *drbd_alloc_ee(struct drbd_conf *mdev,
1585b411b363SPhilipp Reisner 					    u64 id,
1586b411b363SPhilipp Reisner 					    sector_t sector,
1587b411b363SPhilipp Reisner 					    unsigned int data_size,
1588b411b363SPhilipp Reisner 					    gfp_t gfp_mask) __must_hold(local);
1589435f0740SLars Ellenberg extern void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
1590435f0740SLars Ellenberg 		int is_net);
1591435f0740SLars Ellenberg #define drbd_free_ee(m,e)	drbd_free_some_ee(m, e, 0)
1592435f0740SLars Ellenberg #define drbd_free_net_ee(m,e)	drbd_free_some_ee(m, e, 1)
1593b411b363SPhilipp Reisner extern void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1594b411b363SPhilipp Reisner 		struct list_head *head);
1595b411b363SPhilipp Reisner extern void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1596b411b363SPhilipp Reisner 		struct list_head *head);
1597b411b363SPhilipp Reisner extern void drbd_set_recv_tcq(struct drbd_conf *mdev, int tcq_enabled);
1598b411b363SPhilipp Reisner extern void _drbd_clear_done_ee(struct drbd_conf *mdev, struct list_head *to_be_freed);
1599b411b363SPhilipp Reisner extern void drbd_flush_workqueue(struct drbd_conf *mdev);
1600b411b363SPhilipp Reisner 
1601b411b363SPhilipp Reisner /* yes, there is kernel_setsockopt, but only since 2.6.18. we don't need to
1602b411b363SPhilipp Reisner  * mess with get_fs/set_fs, we know we are KERNEL_DS always. */
1603b411b363SPhilipp Reisner static inline int drbd_setsockopt(struct socket *sock, int level, int optname,
1604b411b363SPhilipp Reisner 			char __user *optval, int optlen)
1605b411b363SPhilipp Reisner {
1606b411b363SPhilipp Reisner 	int err;
1607b411b363SPhilipp Reisner 	if (level == SOL_SOCKET)
1608b411b363SPhilipp Reisner 		err = sock_setsockopt(sock, level, optname, optval, optlen);
1609b411b363SPhilipp Reisner 	else
1610b411b363SPhilipp Reisner 		err = sock->ops->setsockopt(sock, level, optname, optval,
1611b411b363SPhilipp Reisner 					    optlen);
1612b411b363SPhilipp Reisner 	return err;
1613b411b363SPhilipp Reisner }
1614b411b363SPhilipp Reisner 
1615b411b363SPhilipp Reisner static inline void drbd_tcp_cork(struct socket *sock)
1616b411b363SPhilipp Reisner {
1617b411b363SPhilipp Reisner 	int __user val = 1;
1618b411b363SPhilipp Reisner 	(void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1619b411b363SPhilipp Reisner 			(char __user *)&val, sizeof(val));
1620b411b363SPhilipp Reisner }
1621b411b363SPhilipp Reisner 
1622b411b363SPhilipp Reisner static inline void drbd_tcp_uncork(struct socket *sock)
1623b411b363SPhilipp Reisner {
1624b411b363SPhilipp Reisner 	int __user val = 0;
1625b411b363SPhilipp Reisner 	(void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1626b411b363SPhilipp Reisner 			(char __user *)&val, sizeof(val));
1627b411b363SPhilipp Reisner }
1628b411b363SPhilipp Reisner 
1629b411b363SPhilipp Reisner static inline void drbd_tcp_nodelay(struct socket *sock)
1630b411b363SPhilipp Reisner {
1631b411b363SPhilipp Reisner 	int __user val = 1;
1632b411b363SPhilipp Reisner 	(void) drbd_setsockopt(sock, SOL_TCP, TCP_NODELAY,
1633b411b363SPhilipp Reisner 			(char __user *)&val, sizeof(val));
1634b411b363SPhilipp Reisner }
1635b411b363SPhilipp Reisner 
1636b411b363SPhilipp Reisner static inline void drbd_tcp_quickack(struct socket *sock)
1637b411b363SPhilipp Reisner {
1638344fa462SLars Ellenberg 	int __user val = 2;
1639b411b363SPhilipp Reisner 	(void) drbd_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
1640b411b363SPhilipp Reisner 			(char __user *)&val, sizeof(val));
1641b411b363SPhilipp Reisner }
1642b411b363SPhilipp Reisner 
1643b411b363SPhilipp Reisner void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo);
1644b411b363SPhilipp Reisner 
1645b411b363SPhilipp Reisner /* drbd_proc.c */
1646b411b363SPhilipp Reisner extern struct proc_dir_entry *drbd_proc;
16477d4e9d09SEmese Revfy extern const struct file_operations drbd_proc_fops;
1648b411b363SPhilipp Reisner extern const char *drbd_conn_str(enum drbd_conns s);
1649b411b363SPhilipp Reisner extern const char *drbd_role_str(enum drbd_role s);
1650b411b363SPhilipp Reisner 
1651b411b363SPhilipp Reisner /* drbd_actlog.c */
1652b411b363SPhilipp Reisner extern void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector);
1653b411b363SPhilipp Reisner extern void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector);
1654b411b363SPhilipp Reisner extern void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector);
1655b411b363SPhilipp Reisner extern int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1656b411b363SPhilipp Reisner extern int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1657b411b363SPhilipp Reisner extern void drbd_rs_cancel_all(struct drbd_conf *mdev);
1658b411b363SPhilipp Reisner extern int drbd_rs_del_all(struct drbd_conf *mdev);
1659b411b363SPhilipp Reisner extern void drbd_rs_failed_io(struct drbd_conf *mdev,
1660b411b363SPhilipp Reisner 		sector_t sector, int size);
1661b411b363SPhilipp Reisner extern int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *);
1662ea5442afSLars Ellenberg extern void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go);
1663b411b363SPhilipp Reisner extern void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector,
1664b411b363SPhilipp Reisner 		int size, const char *file, const unsigned int line);
1665b411b363SPhilipp Reisner #define drbd_set_in_sync(mdev, sector, size) \
1666b411b363SPhilipp Reisner 	__drbd_set_in_sync(mdev, sector, size, __FILE__, __LINE__)
166773a01a18SPhilipp Reisner extern int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector,
1668b411b363SPhilipp Reisner 		int size, const char *file, const unsigned int line);
1669b411b363SPhilipp Reisner #define drbd_set_out_of_sync(mdev, sector, size) \
1670b411b363SPhilipp Reisner 	__drbd_set_out_of_sync(mdev, sector, size, __FILE__, __LINE__)
1671b411b363SPhilipp Reisner extern void drbd_al_apply_to_bm(struct drbd_conf *mdev);
1672b411b363SPhilipp Reisner extern void drbd_al_shrink(struct drbd_conf *mdev);
1673b411b363SPhilipp Reisner 
1674b411b363SPhilipp Reisner 
1675b411b363SPhilipp Reisner /* drbd_nl.c */
1676b411b363SPhilipp Reisner 
1677b411b363SPhilipp Reisner void drbd_nl_cleanup(void);
1678b411b363SPhilipp Reisner int __init drbd_nl_init(void);
1679b411b363SPhilipp Reisner void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state);
1680b411b363SPhilipp Reisner void drbd_bcast_sync_progress(struct drbd_conf *mdev);
1681b411b363SPhilipp Reisner void drbd_bcast_ee(struct drbd_conf *mdev,
1682b411b363SPhilipp Reisner 		const char *reason, const int dgs,
1683b411b363SPhilipp Reisner 		const char* seen_hash, const char* calc_hash,
1684b411b363SPhilipp Reisner 		const struct drbd_epoch_entry* e);
1685b411b363SPhilipp Reisner 
1686b411b363SPhilipp Reisner 
1687b411b363SPhilipp Reisner /**
1688b411b363SPhilipp Reisner  * DOC: DRBD State macros
1689b411b363SPhilipp Reisner  *
1690b411b363SPhilipp Reisner  * These macros are used to express state changes in easily readable form.
1691b411b363SPhilipp Reisner  *
1692b411b363SPhilipp Reisner  * The NS macros expand to a mask and a value, that can be bit ored onto the
1693b411b363SPhilipp Reisner  * current state as soon as the spinlock (req_lock) was taken.
1694b411b363SPhilipp Reisner  *
1695b411b363SPhilipp Reisner  * The _NS macros are used for state functions that get called with the
1696b411b363SPhilipp Reisner  * spinlock. These macros expand directly to the new state value.
1697b411b363SPhilipp Reisner  *
1698b411b363SPhilipp Reisner  * Besides the basic forms NS() and _NS() additional _?NS[23] are defined
1699b411b363SPhilipp Reisner  * to express state changes that affect more than one aspect of the state.
1700b411b363SPhilipp Reisner  *
1701b411b363SPhilipp Reisner  * E.g. NS2(conn, C_CONNECTED, peer, R_SECONDARY)
1702b411b363SPhilipp Reisner  * Means that the network connection was established and that the peer
1703b411b363SPhilipp Reisner  * is in secondary role.
1704b411b363SPhilipp Reisner  */
1705b411b363SPhilipp Reisner #define role_MASK R_MASK
1706b411b363SPhilipp Reisner #define peer_MASK R_MASK
1707b411b363SPhilipp Reisner #define disk_MASK D_MASK
1708b411b363SPhilipp Reisner #define pdsk_MASK D_MASK
1709b411b363SPhilipp Reisner #define conn_MASK C_MASK
1710b411b363SPhilipp Reisner #define susp_MASK 1
1711b411b363SPhilipp Reisner #define user_isp_MASK 1
1712b411b363SPhilipp Reisner #define aftr_isp_MASK 1
1713fb22c402SPhilipp Reisner #define susp_nod_MASK 1
1714fb22c402SPhilipp Reisner #define susp_fen_MASK 1
1715b411b363SPhilipp Reisner 
1716b411b363SPhilipp Reisner #define NS(T, S) \
1717b411b363SPhilipp Reisner 	({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
1718b411b363SPhilipp Reisner 	({ union drbd_state val; val.i = 0; val.T = (S); val; })
1719b411b363SPhilipp Reisner #define NS2(T1, S1, T2, S2) \
1720b411b363SPhilipp Reisner 	({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1721b411b363SPhilipp Reisner 	  mask.T2 = T2##_MASK; mask; }), \
1722b411b363SPhilipp Reisner 	({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1723b411b363SPhilipp Reisner 	  val.T2 = (S2); val; })
1724b411b363SPhilipp Reisner #define NS3(T1, S1, T2, S2, T3, S3) \
1725b411b363SPhilipp Reisner 	({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1726b411b363SPhilipp Reisner 	  mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
1727b411b363SPhilipp Reisner 	({ union drbd_state val;  val.i = 0; val.T1 = (S1); \
1728b411b363SPhilipp Reisner 	  val.T2 = (S2); val.T3 = (S3); val; })
1729b411b363SPhilipp Reisner 
1730b411b363SPhilipp Reisner #define _NS(D, T, S) \
1731b411b363SPhilipp Reisner 	D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T = (S); __ns; })
1732b411b363SPhilipp Reisner #define _NS2(D, T1, S1, T2, S2) \
1733b411b363SPhilipp Reisner 	D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1734b411b363SPhilipp Reisner 	__ns.T2 = (S2); __ns; })
1735b411b363SPhilipp Reisner #define _NS3(D, T1, S1, T2, S2, T3, S3) \
1736b411b363SPhilipp Reisner 	D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1737b411b363SPhilipp Reisner 	__ns.T2 = (S2); __ns.T3 = (S3); __ns; })
1738b411b363SPhilipp Reisner 
1739b411b363SPhilipp Reisner /*
1740b411b363SPhilipp Reisner  * inline helper functions
1741b411b363SPhilipp Reisner  *************************/
1742b411b363SPhilipp Reisner 
174345bb912bSLars Ellenberg /* see also page_chain_add and friends in drbd_receiver.c */
174445bb912bSLars Ellenberg static inline struct page *page_chain_next(struct page *page)
174545bb912bSLars Ellenberg {
174645bb912bSLars Ellenberg 	return (struct page *)page_private(page);
174745bb912bSLars Ellenberg }
174845bb912bSLars Ellenberg #define page_chain_for_each(page) \
174945bb912bSLars Ellenberg 	for (; page && ({ prefetch(page_chain_next(page)); 1; }); \
175045bb912bSLars Ellenberg 			page = page_chain_next(page))
175145bb912bSLars Ellenberg #define page_chain_for_each_safe(page, n) \
175245bb912bSLars Ellenberg 	for (; page && ({ n = page_chain_next(page); 1; }); page = n)
175345bb912bSLars Ellenberg 
175445bb912bSLars Ellenberg static inline int drbd_bio_has_active_page(struct bio *bio)
175545bb912bSLars Ellenberg {
175645bb912bSLars Ellenberg 	struct bio_vec *bvec;
175745bb912bSLars Ellenberg 	int i;
175845bb912bSLars Ellenberg 
175945bb912bSLars Ellenberg 	__bio_for_each_segment(bvec, bio, i, 0) {
176045bb912bSLars Ellenberg 		if (page_count(bvec->bv_page) > 1)
176145bb912bSLars Ellenberg 			return 1;
176245bb912bSLars Ellenberg 	}
176345bb912bSLars Ellenberg 
176445bb912bSLars Ellenberg 	return 0;
176545bb912bSLars Ellenberg }
176645bb912bSLars Ellenberg 
176745bb912bSLars Ellenberg static inline int drbd_ee_has_active_page(struct drbd_epoch_entry *e)
176845bb912bSLars Ellenberg {
176945bb912bSLars Ellenberg 	struct page *page = e->pages;
177045bb912bSLars Ellenberg 	page_chain_for_each(page) {
177145bb912bSLars Ellenberg 		if (page_count(page) > 1)
177245bb912bSLars Ellenberg 			return 1;
177345bb912bSLars Ellenberg 	}
177445bb912bSLars Ellenberg 	return 0;
177545bb912bSLars Ellenberg }
177645bb912bSLars Ellenberg 
177745bb912bSLars Ellenberg 
1778b411b363SPhilipp Reisner static inline void drbd_state_lock(struct drbd_conf *mdev)
1779b411b363SPhilipp Reisner {
1780b411b363SPhilipp Reisner 	wait_event(mdev->misc_wait,
1781b411b363SPhilipp Reisner 		   !test_and_set_bit(CLUSTER_ST_CHANGE, &mdev->flags));
1782b411b363SPhilipp Reisner }
1783b411b363SPhilipp Reisner 
1784b411b363SPhilipp Reisner static inline void drbd_state_unlock(struct drbd_conf *mdev)
1785b411b363SPhilipp Reisner {
1786b411b363SPhilipp Reisner 	clear_bit(CLUSTER_ST_CHANGE, &mdev->flags);
1787b411b363SPhilipp Reisner 	wake_up(&mdev->misc_wait);
1788b411b363SPhilipp Reisner }
1789b411b363SPhilipp Reisner 
1790bf885f8aSAndreas Gruenbacher static inline enum drbd_state_rv
1791bf885f8aSAndreas Gruenbacher _drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
1792bf885f8aSAndreas Gruenbacher 		enum chg_state_flags flags, struct completion *done)
1793b411b363SPhilipp Reisner {
1794bf885f8aSAndreas Gruenbacher 	enum drbd_state_rv rv;
1795b411b363SPhilipp Reisner 
1796b411b363SPhilipp Reisner 	read_lock(&global_state_lock);
1797b411b363SPhilipp Reisner 	rv = __drbd_set_state(mdev, ns, flags, done);
1798b411b363SPhilipp Reisner 	read_unlock(&global_state_lock);
1799b411b363SPhilipp Reisner 
1800b411b363SPhilipp Reisner 	return rv;
1801b411b363SPhilipp Reisner }
1802b411b363SPhilipp Reisner 
1803b411b363SPhilipp Reisner /**
1804b411b363SPhilipp Reisner  * drbd_request_state() - Reqest a state change
1805b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1806b411b363SPhilipp Reisner  * @mask:	mask of state bits to change.
1807b411b363SPhilipp Reisner  * @val:	value of new state bits.
1808b411b363SPhilipp Reisner  *
1809b411b363SPhilipp Reisner  * This is the most graceful way of requesting a state change. It is verbose
1810b411b363SPhilipp Reisner  * quite verbose in case the state change is not possible, and all those
1811b411b363SPhilipp Reisner  * state changes are globally serialized.
1812b411b363SPhilipp Reisner  */
1813b411b363SPhilipp Reisner static inline int drbd_request_state(struct drbd_conf *mdev,
1814b411b363SPhilipp Reisner 				     union drbd_state mask,
1815b411b363SPhilipp Reisner 				     union drbd_state val)
1816b411b363SPhilipp Reisner {
1817b411b363SPhilipp Reisner 	return _drbd_request_state(mdev, mask, val, CS_VERBOSE + CS_ORDERED);
1818b411b363SPhilipp Reisner }
1819b411b363SPhilipp Reisner 
1820b411b363SPhilipp Reisner #define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
1821b411b363SPhilipp Reisner static inline void __drbd_chk_io_error_(struct drbd_conf *mdev, int forcedetach, const char *where)
1822b411b363SPhilipp Reisner {
1823b411b363SPhilipp Reisner 	switch (mdev->ldev->dc.on_io_error) {
1824b411b363SPhilipp Reisner 	case EP_PASS_ON:
1825b411b363SPhilipp Reisner 		if (!forcedetach) {
18267383506cSLars Ellenberg 			if (__ratelimit(&drbd_ratelimit_state))
182782f59cc6SLars Ellenberg 				dev_err(DEV, "Local IO failed in %s.\n", where);
1828d2e17807SPhilipp Reisner 			if (mdev->state.disk > D_INCONSISTENT)
1829738a84b2SPhilipp Reisner 				_drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_HARD, NULL);
1830b411b363SPhilipp Reisner 			break;
1831b411b363SPhilipp Reisner 		}
1832b411b363SPhilipp Reisner 		/* NOTE fall through to detach case if forcedetach set */
1833b411b363SPhilipp Reisner 	case EP_DETACH:
1834b411b363SPhilipp Reisner 	case EP_CALL_HELPER:
183582f59cc6SLars Ellenberg 		set_bit(WAS_IO_ERROR, &mdev->flags);
1836b411b363SPhilipp Reisner 		if (mdev->state.disk > D_FAILED) {
1837b411b363SPhilipp Reisner 			_drbd_set_state(_NS(mdev, disk, D_FAILED), CS_HARD, NULL);
183882f59cc6SLars Ellenberg 			dev_err(DEV,
183982f59cc6SLars Ellenberg 				"Local IO failed in %s. Detaching...\n", where);
1840b411b363SPhilipp Reisner 		}
1841b411b363SPhilipp Reisner 		break;
1842b411b363SPhilipp Reisner 	}
1843b411b363SPhilipp Reisner }
1844b411b363SPhilipp Reisner 
1845b411b363SPhilipp Reisner /**
1846b411b363SPhilipp Reisner  * drbd_chk_io_error: Handle the on_io_error setting, should be called from all io completion handlers
1847b411b363SPhilipp Reisner  * @mdev:	 DRBD device.
1848b411b363SPhilipp Reisner  * @error:	 Error code passed to the IO completion callback
1849b411b363SPhilipp Reisner  * @forcedetach: Force detach. I.e. the error happened while accessing the meta data
1850b411b363SPhilipp Reisner  *
1851b411b363SPhilipp Reisner  * See also drbd_main.c:after_state_ch() if (os.disk > D_FAILED && ns.disk == D_FAILED)
1852b411b363SPhilipp Reisner  */
1853b411b363SPhilipp Reisner #define drbd_chk_io_error(m,e,f) drbd_chk_io_error_(m,e,f, __func__)
1854b411b363SPhilipp Reisner static inline void drbd_chk_io_error_(struct drbd_conf *mdev,
1855b411b363SPhilipp Reisner 	int error, int forcedetach, const char *where)
1856b411b363SPhilipp Reisner {
1857b411b363SPhilipp Reisner 	if (error) {
1858b411b363SPhilipp Reisner 		unsigned long flags;
1859b411b363SPhilipp Reisner 		spin_lock_irqsave(&mdev->req_lock, flags);
1860b411b363SPhilipp Reisner 		__drbd_chk_io_error_(mdev, forcedetach, where);
1861b411b363SPhilipp Reisner 		spin_unlock_irqrestore(&mdev->req_lock, flags);
1862b411b363SPhilipp Reisner 	}
1863b411b363SPhilipp Reisner }
1864b411b363SPhilipp Reisner 
1865b411b363SPhilipp Reisner 
1866b411b363SPhilipp Reisner /**
1867b411b363SPhilipp Reisner  * drbd_md_first_sector() - Returns the first sector number of the meta data area
1868b411b363SPhilipp Reisner  * @bdev:	Meta data block device.
1869b411b363SPhilipp Reisner  *
1870b411b363SPhilipp Reisner  * BTW, for internal meta data, this happens to be the maximum capacity
1871b411b363SPhilipp Reisner  * we could agree upon with our peer node.
1872b411b363SPhilipp Reisner  */
1873b411b363SPhilipp Reisner static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
1874b411b363SPhilipp Reisner {
1875b411b363SPhilipp Reisner 	switch (bdev->dc.meta_dev_idx) {
1876b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_INTERNAL:
1877b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_INT:
1878b411b363SPhilipp Reisner 		return bdev->md.md_offset + bdev->md.bm_offset;
1879b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_EXT:
1880b411b363SPhilipp Reisner 	default:
1881b411b363SPhilipp Reisner 		return bdev->md.md_offset;
1882b411b363SPhilipp Reisner 	}
1883b411b363SPhilipp Reisner }
1884b411b363SPhilipp Reisner 
1885b411b363SPhilipp Reisner /**
1886b411b363SPhilipp Reisner  * drbd_md_last_sector() - Return the last sector number of the meta data area
1887b411b363SPhilipp Reisner  * @bdev:	Meta data block device.
1888b411b363SPhilipp Reisner  */
1889b411b363SPhilipp Reisner static inline sector_t drbd_md_last_sector(struct drbd_backing_dev *bdev)
1890b411b363SPhilipp Reisner {
1891b411b363SPhilipp Reisner 	switch (bdev->dc.meta_dev_idx) {
1892b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_INTERNAL:
1893b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_INT:
1894b411b363SPhilipp Reisner 		return bdev->md.md_offset + MD_AL_OFFSET - 1;
1895b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_EXT:
1896b411b363SPhilipp Reisner 	default:
1897b411b363SPhilipp Reisner 		return bdev->md.md_offset + bdev->md.md_size_sect;
1898b411b363SPhilipp Reisner 	}
1899b411b363SPhilipp Reisner }
1900b411b363SPhilipp Reisner 
1901b411b363SPhilipp Reisner /* Returns the number of 512 byte sectors of the device */
1902b411b363SPhilipp Reisner static inline sector_t drbd_get_capacity(struct block_device *bdev)
1903b411b363SPhilipp Reisner {
1904b411b363SPhilipp Reisner 	/* return bdev ? get_capacity(bdev->bd_disk) : 0; */
190577304d2aSMike Snitzer 	return bdev ? i_size_read(bdev->bd_inode) >> 9 : 0;
1906b411b363SPhilipp Reisner }
1907b411b363SPhilipp Reisner 
1908b411b363SPhilipp Reisner /**
1909b411b363SPhilipp Reisner  * drbd_get_max_capacity() - Returns the capacity we announce to out peer
1910b411b363SPhilipp Reisner  * @bdev:	Meta data block device.
1911b411b363SPhilipp Reisner  *
1912b411b363SPhilipp Reisner  * returns the capacity we announce to out peer.  we clip ourselves at the
1913b411b363SPhilipp Reisner  * various MAX_SECTORS, because if we don't, current implementation will
1914b411b363SPhilipp Reisner  * oops sooner or later
1915b411b363SPhilipp Reisner  */
1916b411b363SPhilipp Reisner static inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
1917b411b363SPhilipp Reisner {
1918b411b363SPhilipp Reisner 	sector_t s;
1919b411b363SPhilipp Reisner 	switch (bdev->dc.meta_dev_idx) {
1920b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_INTERNAL:
1921b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_INT:
1922b411b363SPhilipp Reisner 		s = drbd_get_capacity(bdev->backing_bdev)
1923b411b363SPhilipp Reisner 			? min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1924b411b363SPhilipp Reisner 					drbd_md_first_sector(bdev))
1925b411b363SPhilipp Reisner 			: 0;
1926b411b363SPhilipp Reisner 		break;
1927b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_EXT:
1928b411b363SPhilipp Reisner 		s = min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1929b411b363SPhilipp Reisner 				drbd_get_capacity(bdev->backing_bdev));
1930b411b363SPhilipp Reisner 		/* clip at maximum size the meta device can support */
1931b411b363SPhilipp Reisner 		s = min_t(sector_t, s,
1932b411b363SPhilipp Reisner 			BM_EXT_TO_SECT(bdev->md.md_size_sect
1933b411b363SPhilipp Reisner 				     - bdev->md.bm_offset));
1934b411b363SPhilipp Reisner 		break;
1935b411b363SPhilipp Reisner 	default:
1936b411b363SPhilipp Reisner 		s = min_t(sector_t, DRBD_MAX_SECTORS,
1937b411b363SPhilipp Reisner 				drbd_get_capacity(bdev->backing_bdev));
1938b411b363SPhilipp Reisner 	}
1939b411b363SPhilipp Reisner 	return s;
1940b411b363SPhilipp Reisner }
1941b411b363SPhilipp Reisner 
1942b411b363SPhilipp Reisner /**
1943b411b363SPhilipp Reisner  * drbd_md_ss__() - Return the sector number of our meta data super block
1944b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1945b411b363SPhilipp Reisner  * @bdev:	Meta data block device.
1946b411b363SPhilipp Reisner  */
1947b411b363SPhilipp Reisner static inline sector_t drbd_md_ss__(struct drbd_conf *mdev,
1948b411b363SPhilipp Reisner 				    struct drbd_backing_dev *bdev)
1949b411b363SPhilipp Reisner {
1950b411b363SPhilipp Reisner 	switch (bdev->dc.meta_dev_idx) {
1951b411b363SPhilipp Reisner 	default: /* external, some index */
1952b411b363SPhilipp Reisner 		return MD_RESERVED_SECT * bdev->dc.meta_dev_idx;
1953b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_INTERNAL:
1954b411b363SPhilipp Reisner 		/* with drbd08, internal meta data is always "flexible" */
1955b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_INT:
1956b411b363SPhilipp Reisner 		/* sizeof(struct md_on_disk_07) == 4k
1957b411b363SPhilipp Reisner 		 * position: last 4k aligned block of 4k size */
1958b411b363SPhilipp Reisner 		if (!bdev->backing_bdev) {
1959b411b363SPhilipp Reisner 			if (__ratelimit(&drbd_ratelimit_state)) {
1960b411b363SPhilipp Reisner 				dev_err(DEV, "bdev->backing_bdev==NULL\n");
1961b411b363SPhilipp Reisner 				dump_stack();
1962b411b363SPhilipp Reisner 			}
1963b411b363SPhilipp Reisner 			return 0;
1964b411b363SPhilipp Reisner 		}
1965b411b363SPhilipp Reisner 		return (drbd_get_capacity(bdev->backing_bdev) & ~7ULL)
1966b411b363SPhilipp Reisner 			- MD_AL_OFFSET;
1967b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_EXT:
1968b411b363SPhilipp Reisner 		return 0;
1969b411b363SPhilipp Reisner 	}
1970b411b363SPhilipp Reisner }
1971b411b363SPhilipp Reisner 
1972b411b363SPhilipp Reisner static inline void
1973b411b363SPhilipp Reisner drbd_queue_work_front(struct drbd_work_queue *q, struct drbd_work *w)
1974b411b363SPhilipp Reisner {
1975b411b363SPhilipp Reisner 	unsigned long flags;
1976b411b363SPhilipp Reisner 	spin_lock_irqsave(&q->q_lock, flags);
1977b411b363SPhilipp Reisner 	list_add(&w->list, &q->q);
1978b411b363SPhilipp Reisner 	up(&q->s); /* within the spinlock,
1979b411b363SPhilipp Reisner 		      see comment near end of drbd_worker() */
1980b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&q->q_lock, flags);
1981b411b363SPhilipp Reisner }
1982b411b363SPhilipp Reisner 
1983b411b363SPhilipp Reisner static inline void
1984b411b363SPhilipp Reisner drbd_queue_work(struct drbd_work_queue *q, struct drbd_work *w)
1985b411b363SPhilipp Reisner {
1986b411b363SPhilipp Reisner 	unsigned long flags;
1987b411b363SPhilipp Reisner 	spin_lock_irqsave(&q->q_lock, flags);
1988b411b363SPhilipp Reisner 	list_add_tail(&w->list, &q->q);
1989b411b363SPhilipp Reisner 	up(&q->s); /* within the spinlock,
1990b411b363SPhilipp Reisner 		      see comment near end of drbd_worker() */
1991b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&q->q_lock, flags);
1992b411b363SPhilipp Reisner }
1993b411b363SPhilipp Reisner 
1994b411b363SPhilipp Reisner static inline void wake_asender(struct drbd_conf *mdev)
1995b411b363SPhilipp Reisner {
1996b411b363SPhilipp Reisner 	if (test_bit(SIGNAL_ASENDER, &mdev->flags))
1997b411b363SPhilipp Reisner 		force_sig(DRBD_SIG, mdev->asender.task);
1998b411b363SPhilipp Reisner }
1999b411b363SPhilipp Reisner 
2000b411b363SPhilipp Reisner static inline void request_ping(struct drbd_conf *mdev)
2001b411b363SPhilipp Reisner {
2002b411b363SPhilipp Reisner 	set_bit(SEND_PING, &mdev->flags);
2003b411b363SPhilipp Reisner 	wake_asender(mdev);
2004b411b363SPhilipp Reisner }
2005b411b363SPhilipp Reisner 
2006b411b363SPhilipp Reisner static inline int drbd_send_short_cmd(struct drbd_conf *mdev,
2007b411b363SPhilipp Reisner 	enum drbd_packets cmd)
2008b411b363SPhilipp Reisner {
20090b70a13dSPhilipp Reisner 	struct p_header80 h;
2010b411b363SPhilipp Reisner 	return drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &h, sizeof(h));
2011b411b363SPhilipp Reisner }
2012b411b363SPhilipp Reisner 
2013b411b363SPhilipp Reisner static inline int drbd_send_ping(struct drbd_conf *mdev)
2014b411b363SPhilipp Reisner {
20150b70a13dSPhilipp Reisner 	struct p_header80 h;
2016b411b363SPhilipp Reisner 	return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING, &h, sizeof(h));
2017b411b363SPhilipp Reisner }
2018b411b363SPhilipp Reisner 
2019b411b363SPhilipp Reisner static inline int drbd_send_ping_ack(struct drbd_conf *mdev)
2020b411b363SPhilipp Reisner {
20210b70a13dSPhilipp Reisner 	struct p_header80 h;
2022b411b363SPhilipp Reisner 	return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING_ACK, &h, sizeof(h));
2023b411b363SPhilipp Reisner }
2024b411b363SPhilipp Reisner 
2025b411b363SPhilipp Reisner static inline void drbd_thread_stop(struct drbd_thread *thi)
2026b411b363SPhilipp Reisner {
202781e84650SAndreas Gruenbacher 	_drbd_thread_stop(thi, false, true);
2028b411b363SPhilipp Reisner }
2029b411b363SPhilipp Reisner 
2030b411b363SPhilipp Reisner static inline void drbd_thread_stop_nowait(struct drbd_thread *thi)
2031b411b363SPhilipp Reisner {
203281e84650SAndreas Gruenbacher 	_drbd_thread_stop(thi, false, false);
2033b411b363SPhilipp Reisner }
2034b411b363SPhilipp Reisner 
2035b411b363SPhilipp Reisner static inline void drbd_thread_restart_nowait(struct drbd_thread *thi)
2036b411b363SPhilipp Reisner {
203781e84650SAndreas Gruenbacher 	_drbd_thread_stop(thi, true, false);
2038b411b363SPhilipp Reisner }
2039b411b363SPhilipp Reisner 
2040b411b363SPhilipp Reisner /* counts how many answer packets packets we expect from our peer,
2041b411b363SPhilipp Reisner  * for either explicit application requests,
2042b411b363SPhilipp Reisner  * or implicit barrier packets as necessary.
2043b411b363SPhilipp Reisner  * increased:
2044b411b363SPhilipp Reisner  *  w_send_barrier
2045b411b363SPhilipp Reisner  *  _req_mod(req, queue_for_net_write or queue_for_net_read);
2046b411b363SPhilipp Reisner  *    it is much easier and equally valid to count what we queue for the
2047b411b363SPhilipp Reisner  *    worker, even before it actually was queued or send.
2048b411b363SPhilipp Reisner  *    (drbd_make_request_common; recovery path on read io-error)
2049b411b363SPhilipp Reisner  * decreased:
2050b411b363SPhilipp Reisner  *  got_BarrierAck (respective tl_clear, tl_clear_barrier)
2051b411b363SPhilipp Reisner  *  _req_mod(req, data_received)
2052b411b363SPhilipp Reisner  *     [from receive_DataReply]
2053b411b363SPhilipp Reisner  *  _req_mod(req, write_acked_by_peer or recv_acked_by_peer or neg_acked)
2054b411b363SPhilipp Reisner  *     [from got_BlockAck (P_WRITE_ACK, P_RECV_ACK)]
2055b411b363SPhilipp Reisner  *     for some reason it is NOT decreased in got_NegAck,
2056b411b363SPhilipp Reisner  *     but in the resulting cleanup code from report_params.
2057b411b363SPhilipp Reisner  *     we should try to remember the reason for that...
2058b411b363SPhilipp Reisner  *  _req_mod(req, send_failed or send_canceled)
2059b411b363SPhilipp Reisner  *  _req_mod(req, connection_lost_while_pending)
2060b411b363SPhilipp Reisner  *     [from tl_clear_barrier]
2061b411b363SPhilipp Reisner  */
2062b411b363SPhilipp Reisner static inline void inc_ap_pending(struct drbd_conf *mdev)
2063b411b363SPhilipp Reisner {
2064b411b363SPhilipp Reisner 	atomic_inc(&mdev->ap_pending_cnt);
2065b411b363SPhilipp Reisner }
2066b411b363SPhilipp Reisner 
2067b411b363SPhilipp Reisner #define ERR_IF_CNT_IS_NEGATIVE(which)				\
2068b411b363SPhilipp Reisner 	if (atomic_read(&mdev->which) < 0)			\
2069b411b363SPhilipp Reisner 		dev_err(DEV, "in %s:%d: " #which " = %d < 0 !\n",	\
2070b411b363SPhilipp Reisner 		    __func__ , __LINE__ ,			\
2071b411b363SPhilipp Reisner 		    atomic_read(&mdev->which))
2072b411b363SPhilipp Reisner 
2073b411b363SPhilipp Reisner #define dec_ap_pending(mdev)	do {				\
2074b411b363SPhilipp Reisner 	typecheck(struct drbd_conf *, mdev);			\
2075b411b363SPhilipp Reisner 	if (atomic_dec_and_test(&mdev->ap_pending_cnt))		\
2076b411b363SPhilipp Reisner 		wake_up(&mdev->misc_wait);			\
2077b411b363SPhilipp Reisner 	ERR_IF_CNT_IS_NEGATIVE(ap_pending_cnt); } while (0)
2078b411b363SPhilipp Reisner 
2079b411b363SPhilipp Reisner /* counts how many resync-related answers we still expect from the peer
2080b411b363SPhilipp Reisner  *		     increase			decrease
2081b411b363SPhilipp Reisner  * C_SYNC_TARGET sends P_RS_DATA_REQUEST (and expects P_RS_DATA_REPLY)
208225985edcSLucas De Marchi  * C_SYNC_SOURCE sends P_RS_DATA_REPLY   (and expects P_WRITE_ACK with ID_SYNCER)
2083b411b363SPhilipp Reisner  *					   (or P_NEG_ACK with ID_SYNCER)
2084b411b363SPhilipp Reisner  */
2085b411b363SPhilipp Reisner static inline void inc_rs_pending(struct drbd_conf *mdev)
2086b411b363SPhilipp Reisner {
2087b411b363SPhilipp Reisner 	atomic_inc(&mdev->rs_pending_cnt);
2088b411b363SPhilipp Reisner }
2089b411b363SPhilipp Reisner 
2090b411b363SPhilipp Reisner #define dec_rs_pending(mdev)	do {				\
2091b411b363SPhilipp Reisner 	typecheck(struct drbd_conf *, mdev);			\
2092b411b363SPhilipp Reisner 	atomic_dec(&mdev->rs_pending_cnt);			\
2093b411b363SPhilipp Reisner 	ERR_IF_CNT_IS_NEGATIVE(rs_pending_cnt); } while (0)
2094b411b363SPhilipp Reisner 
2095b411b363SPhilipp Reisner /* counts how many answers we still need to send to the peer.
2096b411b363SPhilipp Reisner  * increased on
2097b411b363SPhilipp Reisner  *  receive_Data	unless protocol A;
2098b411b363SPhilipp Reisner  *			we need to send a P_RECV_ACK (proto B)
2099b411b363SPhilipp Reisner  *			or P_WRITE_ACK (proto C)
2100b411b363SPhilipp Reisner  *  receive_RSDataReply (recv_resync_read) we need to send a P_WRITE_ACK
2101b411b363SPhilipp Reisner  *  receive_DataRequest (receive_RSDataRequest) we need to send back P_DATA
2102b411b363SPhilipp Reisner  *  receive_Barrier_*	we need to send a P_BARRIER_ACK
2103b411b363SPhilipp Reisner  */
2104b411b363SPhilipp Reisner static inline void inc_unacked(struct drbd_conf *mdev)
2105b411b363SPhilipp Reisner {
2106b411b363SPhilipp Reisner 	atomic_inc(&mdev->unacked_cnt);
2107b411b363SPhilipp Reisner }
2108b411b363SPhilipp Reisner 
2109b411b363SPhilipp Reisner #define dec_unacked(mdev)	do {				\
2110b411b363SPhilipp Reisner 	typecheck(struct drbd_conf *, mdev);			\
2111b411b363SPhilipp Reisner 	atomic_dec(&mdev->unacked_cnt);				\
2112b411b363SPhilipp Reisner 	ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2113b411b363SPhilipp Reisner 
2114b411b363SPhilipp Reisner #define sub_unacked(mdev, n)	do {				\
2115b411b363SPhilipp Reisner 	typecheck(struct drbd_conf *, mdev);			\
2116b411b363SPhilipp Reisner 	atomic_sub(n, &mdev->unacked_cnt);			\
2117b411b363SPhilipp Reisner 	ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2118b411b363SPhilipp Reisner 
2119b411b363SPhilipp Reisner 
2120b411b363SPhilipp Reisner static inline void put_net_conf(struct drbd_conf *mdev)
2121b411b363SPhilipp Reisner {
2122b411b363SPhilipp Reisner 	if (atomic_dec_and_test(&mdev->net_cnt))
212384dfb9f5SPhilipp Reisner 		wake_up(&mdev->net_cnt_wait);
2124b411b363SPhilipp Reisner }
2125b411b363SPhilipp Reisner 
2126b411b363SPhilipp Reisner /**
2127b411b363SPhilipp Reisner  * get_net_conf() - Increase ref count on mdev->net_conf; Returns 0 if nothing there
2128b411b363SPhilipp Reisner  * @mdev:	DRBD device.
2129b411b363SPhilipp Reisner  *
2130b411b363SPhilipp Reisner  * You have to call put_net_conf() when finished working with mdev->net_conf.
2131b411b363SPhilipp Reisner  */
2132b411b363SPhilipp Reisner static inline int get_net_conf(struct drbd_conf *mdev)
2133b411b363SPhilipp Reisner {
2134b411b363SPhilipp Reisner 	int have_net_conf;
2135b411b363SPhilipp Reisner 
2136b411b363SPhilipp Reisner 	atomic_inc(&mdev->net_cnt);
2137b411b363SPhilipp Reisner 	have_net_conf = mdev->state.conn >= C_UNCONNECTED;
2138b411b363SPhilipp Reisner 	if (!have_net_conf)
2139b411b363SPhilipp Reisner 		put_net_conf(mdev);
2140b411b363SPhilipp Reisner 	return have_net_conf;
2141b411b363SPhilipp Reisner }
2142b411b363SPhilipp Reisner 
2143b411b363SPhilipp Reisner /**
2144b411b363SPhilipp Reisner  * get_ldev() - Increase the ref count on mdev->ldev. Returns 0 if there is no ldev
2145b411b363SPhilipp Reisner  * @M:		DRBD device.
2146b411b363SPhilipp Reisner  *
2147b411b363SPhilipp Reisner  * You have to call put_ldev() when finished working with mdev->ldev.
2148b411b363SPhilipp Reisner  */
2149b411b363SPhilipp Reisner #define get_ldev(M) __cond_lock(local, _get_ldev_if_state(M,D_INCONSISTENT))
2150b411b363SPhilipp Reisner #define get_ldev_if_state(M,MINS) __cond_lock(local, _get_ldev_if_state(M,MINS))
2151b411b363SPhilipp Reisner 
2152b411b363SPhilipp Reisner static inline void put_ldev(struct drbd_conf *mdev)
2153b411b363SPhilipp Reisner {
21541d7734a0SLars Ellenberg 	int i = atomic_dec_return(&mdev->local_cnt);
21559a0d9d03SLars Ellenberg 
21569a0d9d03SLars Ellenberg 	/* This may be called from some endio handler,
21579a0d9d03SLars Ellenberg 	 * so we must not sleep here. */
21589a0d9d03SLars Ellenberg 
2159b411b363SPhilipp Reisner 	__release(local);
21601d7734a0SLars Ellenberg 	D_ASSERT(i >= 0);
2161e9e6f3ecSLars Ellenberg 	if (i == 0) {
216282f59cc6SLars Ellenberg 		if (mdev->state.disk == D_DISKLESS)
216382f59cc6SLars Ellenberg 			/* even internal references gone, safe to destroy */
216482f59cc6SLars Ellenberg 			drbd_ldev_destroy(mdev);
2165e9e6f3ecSLars Ellenberg 		if (mdev->state.disk == D_FAILED)
216682f59cc6SLars Ellenberg 			/* all application IO references gone. */
2167e9e6f3ecSLars Ellenberg 			drbd_go_diskless(mdev);
2168b411b363SPhilipp Reisner 		wake_up(&mdev->misc_wait);
2169b411b363SPhilipp Reisner 	}
2170e9e6f3ecSLars Ellenberg }
2171b411b363SPhilipp Reisner 
2172b411b363SPhilipp Reisner #ifndef __CHECKER__
2173b411b363SPhilipp Reisner static inline int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
2174b411b363SPhilipp Reisner {
2175b411b363SPhilipp Reisner 	int io_allowed;
2176b411b363SPhilipp Reisner 
217782f59cc6SLars Ellenberg 	/* never get a reference while D_DISKLESS */
217882f59cc6SLars Ellenberg 	if (mdev->state.disk == D_DISKLESS)
217982f59cc6SLars Ellenberg 		return 0;
218082f59cc6SLars Ellenberg 
2181b411b363SPhilipp Reisner 	atomic_inc(&mdev->local_cnt);
2182b411b363SPhilipp Reisner 	io_allowed = (mdev->state.disk >= mins);
2183b411b363SPhilipp Reisner 	if (!io_allowed)
2184b411b363SPhilipp Reisner 		put_ldev(mdev);
2185b411b363SPhilipp Reisner 	return io_allowed;
2186b411b363SPhilipp Reisner }
2187b411b363SPhilipp Reisner #else
2188b411b363SPhilipp Reisner extern int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins);
2189b411b363SPhilipp Reisner #endif
2190b411b363SPhilipp Reisner 
2191b411b363SPhilipp Reisner /* you must have an "get_ldev" reference */
2192b411b363SPhilipp Reisner static inline void drbd_get_syncer_progress(struct drbd_conf *mdev,
2193b411b363SPhilipp Reisner 		unsigned long *bits_left, unsigned int *per_mil_done)
2194b411b363SPhilipp Reisner {
21954b0715f0SLars Ellenberg 	/* this is to break it at compile time when we change that, in case we
21964b0715f0SLars Ellenberg 	 * want to support more than (1<<32) bits on a 32bit arch. */
2197b411b363SPhilipp Reisner 	typecheck(unsigned long, mdev->rs_total);
2198b411b363SPhilipp Reisner 
2199b411b363SPhilipp Reisner 	/* note: both rs_total and rs_left are in bits, i.e. in
2200b411b363SPhilipp Reisner 	 * units of BM_BLOCK_SIZE.
2201b411b363SPhilipp Reisner 	 * for the percentage, we don't care. */
2202b411b363SPhilipp Reisner 
2203439d5953SLars Ellenberg 	if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2204439d5953SLars Ellenberg 		*bits_left = mdev->ov_left;
2205439d5953SLars Ellenberg 	else
2206b411b363SPhilipp Reisner 		*bits_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
2207b411b363SPhilipp Reisner 	/* >> 10 to prevent overflow,
2208b411b363SPhilipp Reisner 	 * +1 to prevent division by zero */
2209b411b363SPhilipp Reisner 	if (*bits_left > mdev->rs_total) {
2210b411b363SPhilipp Reisner 		/* doh. maybe a logic bug somewhere.
2211b411b363SPhilipp Reisner 		 * may also be just a race condition
2212b411b363SPhilipp Reisner 		 * between this and a disconnect during sync.
2213b411b363SPhilipp Reisner 		 * for now, just prevent in-kernel buffer overflow.
2214b411b363SPhilipp Reisner 		 */
2215b411b363SPhilipp Reisner 		smp_rmb();
2216b411b363SPhilipp Reisner 		dev_warn(DEV, "cs:%s rs_left=%lu > rs_total=%lu (rs_failed %lu)\n",
2217b411b363SPhilipp Reisner 				drbd_conn_str(mdev->state.conn),
2218b411b363SPhilipp Reisner 				*bits_left, mdev->rs_total, mdev->rs_failed);
2219b411b363SPhilipp Reisner 		*per_mil_done = 0;
2220b411b363SPhilipp Reisner 	} else {
22214b0715f0SLars Ellenberg 		/* Make sure the division happens in long context.
22224b0715f0SLars Ellenberg 		 * We allow up to one petabyte storage right now,
22234b0715f0SLars Ellenberg 		 * at a granularity of 4k per bit that is 2**38 bits.
22244b0715f0SLars Ellenberg 		 * After shift right and multiplication by 1000,
22254b0715f0SLars Ellenberg 		 * this should still fit easily into a 32bit long,
22264b0715f0SLars Ellenberg 		 * so we don't need a 64bit division on 32bit arch.
22274b0715f0SLars Ellenberg 		 * Note: currently we don't support such large bitmaps on 32bit
22284b0715f0SLars Ellenberg 		 * arch anyways, but no harm done to be prepared for it here.
22294b0715f0SLars Ellenberg 		 */
22304b0715f0SLars Ellenberg 		unsigned int shift = mdev->rs_total >= (1ULL << 32) ? 16 : 10;
22314b0715f0SLars Ellenberg 		unsigned long left = *bits_left >> shift;
22324b0715f0SLars Ellenberg 		unsigned long total = 1UL + (mdev->rs_total >> shift);
22334b0715f0SLars Ellenberg 		unsigned long tmp = 1000UL - left * 1000UL/total;
2234b411b363SPhilipp Reisner 		*per_mil_done = tmp;
2235b411b363SPhilipp Reisner 	}
2236b411b363SPhilipp Reisner }
2237b411b363SPhilipp Reisner 
2238b411b363SPhilipp Reisner 
2239b411b363SPhilipp Reisner /* this throttles on-the-fly application requests
2240b411b363SPhilipp Reisner  * according to max_buffers settings;
2241b411b363SPhilipp Reisner  * maybe re-implement using semaphores? */
2242b411b363SPhilipp Reisner static inline int drbd_get_max_buffers(struct drbd_conf *mdev)
2243b411b363SPhilipp Reisner {
2244b411b363SPhilipp Reisner 	int mxb = 1000000; /* arbitrary limit on open requests */
2245b411b363SPhilipp Reisner 	if (get_net_conf(mdev)) {
2246b411b363SPhilipp Reisner 		mxb = mdev->net_conf->max_buffers;
2247b411b363SPhilipp Reisner 		put_net_conf(mdev);
2248b411b363SPhilipp Reisner 	}
2249b411b363SPhilipp Reisner 	return mxb;
2250b411b363SPhilipp Reisner }
2251b411b363SPhilipp Reisner 
22523719094eSPhilipp Reisner static inline int drbd_state_is_stable(struct drbd_conf *mdev)
2253b411b363SPhilipp Reisner {
22543719094eSPhilipp Reisner 	union drbd_state s = mdev->state;
2255b411b363SPhilipp Reisner 
2256b411b363SPhilipp Reisner 	/* DO NOT add a default clause, we want the compiler to warn us
2257b411b363SPhilipp Reisner 	 * for any newly introduced state we may have forgotten to add here */
2258b411b363SPhilipp Reisner 
2259b411b363SPhilipp Reisner 	switch ((enum drbd_conns)s.conn) {
2260b411b363SPhilipp Reisner 	/* new io only accepted when there is no connection, ... */
2261b411b363SPhilipp Reisner 	case C_STANDALONE:
2262b411b363SPhilipp Reisner 	case C_WF_CONNECTION:
2263b411b363SPhilipp Reisner 	/* ... or there is a well established connection. */
2264b411b363SPhilipp Reisner 	case C_CONNECTED:
2265b411b363SPhilipp Reisner 	case C_SYNC_SOURCE:
2266b411b363SPhilipp Reisner 	case C_SYNC_TARGET:
2267b411b363SPhilipp Reisner 	case C_VERIFY_S:
2268b411b363SPhilipp Reisner 	case C_VERIFY_T:
2269b411b363SPhilipp Reisner 	case C_PAUSED_SYNC_S:
2270b411b363SPhilipp Reisner 	case C_PAUSED_SYNC_T:
227167531718SPhilipp Reisner 	case C_AHEAD:
227267531718SPhilipp Reisner 	case C_BEHIND:
22733719094eSPhilipp Reisner 		/* transitional states, IO allowed */
2274b411b363SPhilipp Reisner 	case C_DISCONNECTING:
2275b411b363SPhilipp Reisner 	case C_UNCONNECTED:
2276b411b363SPhilipp Reisner 	case C_TIMEOUT:
2277b411b363SPhilipp Reisner 	case C_BROKEN_PIPE:
2278b411b363SPhilipp Reisner 	case C_NETWORK_FAILURE:
2279b411b363SPhilipp Reisner 	case C_PROTOCOL_ERROR:
2280b411b363SPhilipp Reisner 	case C_TEAR_DOWN:
2281b411b363SPhilipp Reisner 	case C_WF_REPORT_PARAMS:
2282b411b363SPhilipp Reisner 	case C_STARTING_SYNC_S:
2283b411b363SPhilipp Reisner 	case C_STARTING_SYNC_T:
22843719094eSPhilipp Reisner 		break;
22853719094eSPhilipp Reisner 
22863719094eSPhilipp Reisner 		/* Allow IO in BM exchange states with new protocols */
2287b411b363SPhilipp Reisner 	case C_WF_BITMAP_S:
22883719094eSPhilipp Reisner 		if (mdev->agreed_pro_version < 96)
22893719094eSPhilipp Reisner 			return 0;
22903719094eSPhilipp Reisner 		break;
22913719094eSPhilipp Reisner 
22923719094eSPhilipp Reisner 		/* no new io accepted in these states */
2293b411b363SPhilipp Reisner 	case C_WF_BITMAP_T:
2294b411b363SPhilipp Reisner 	case C_WF_SYNC_UUID:
2295b411b363SPhilipp Reisner 	case C_MASK:
2296b411b363SPhilipp Reisner 		/* not "stable" */
2297b411b363SPhilipp Reisner 		return 0;
2298b411b363SPhilipp Reisner 	}
2299b411b363SPhilipp Reisner 
2300b411b363SPhilipp Reisner 	switch ((enum drbd_disk_state)s.disk) {
2301b411b363SPhilipp Reisner 	case D_DISKLESS:
2302b411b363SPhilipp Reisner 	case D_INCONSISTENT:
2303b411b363SPhilipp Reisner 	case D_OUTDATED:
2304b411b363SPhilipp Reisner 	case D_CONSISTENT:
2305b411b363SPhilipp Reisner 	case D_UP_TO_DATE:
2306b411b363SPhilipp Reisner 		/* disk state is stable as well. */
2307b411b363SPhilipp Reisner 		break;
2308b411b363SPhilipp Reisner 
2309b411b363SPhilipp Reisner 	/* no new io accepted during tansitional states */
2310b411b363SPhilipp Reisner 	case D_ATTACHING:
2311b411b363SPhilipp Reisner 	case D_FAILED:
2312b411b363SPhilipp Reisner 	case D_NEGOTIATING:
2313b411b363SPhilipp Reisner 	case D_UNKNOWN:
2314b411b363SPhilipp Reisner 	case D_MASK:
2315b411b363SPhilipp Reisner 		/* not "stable" */
2316b411b363SPhilipp Reisner 		return 0;
2317b411b363SPhilipp Reisner 	}
2318b411b363SPhilipp Reisner 
2319b411b363SPhilipp Reisner 	return 1;
2320b411b363SPhilipp Reisner }
2321b411b363SPhilipp Reisner 
2322fb22c402SPhilipp Reisner static inline int is_susp(union drbd_state s)
2323fb22c402SPhilipp Reisner {
2324fb22c402SPhilipp Reisner 	return s.susp || s.susp_nod || s.susp_fen;
2325fb22c402SPhilipp Reisner }
2326fb22c402SPhilipp Reisner 
23271b881ef7SAndreas Gruenbacher static inline bool may_inc_ap_bio(struct drbd_conf *mdev)
2328b411b363SPhilipp Reisner {
2329b411b363SPhilipp Reisner 	int mxb = drbd_get_max_buffers(mdev);
2330b411b363SPhilipp Reisner 
2331fb22c402SPhilipp Reisner 	if (is_susp(mdev->state))
23321b881ef7SAndreas Gruenbacher 		return false;
2333b411b363SPhilipp Reisner 	if (test_bit(SUSPEND_IO, &mdev->flags))
23341b881ef7SAndreas Gruenbacher 		return false;
2335b411b363SPhilipp Reisner 
2336b411b363SPhilipp Reisner 	/* to avoid potential deadlock or bitmap corruption,
2337b411b363SPhilipp Reisner 	 * in various places, we only allow new application io
2338b411b363SPhilipp Reisner 	 * to start during "stable" states. */
2339b411b363SPhilipp Reisner 
2340b411b363SPhilipp Reisner 	/* no new io accepted when attaching or detaching the disk */
23413719094eSPhilipp Reisner 	if (!drbd_state_is_stable(mdev))
23421b881ef7SAndreas Gruenbacher 		return false;
2343b411b363SPhilipp Reisner 
2344b411b363SPhilipp Reisner 	/* since some older kernels don't have atomic_add_unless,
2345b411b363SPhilipp Reisner 	 * and we are within the spinlock anyways, we have this workaround.  */
2346b411b363SPhilipp Reisner 	if (atomic_read(&mdev->ap_bio_cnt) > mxb)
23471b881ef7SAndreas Gruenbacher 		return false;
2348b411b363SPhilipp Reisner 	if (test_bit(BITMAP_IO, &mdev->flags))
23491b881ef7SAndreas Gruenbacher 		return false;
23501b881ef7SAndreas Gruenbacher 	return true;
2351b411b363SPhilipp Reisner }
2352b411b363SPhilipp Reisner 
23531b881ef7SAndreas Gruenbacher static inline bool inc_ap_bio_cond(struct drbd_conf *mdev, int count)
23548869d683SPhilipp Reisner {
23551b881ef7SAndreas Gruenbacher 	bool rv = false;
23568869d683SPhilipp Reisner 
23578869d683SPhilipp Reisner 	spin_lock_irq(&mdev->req_lock);
23581b881ef7SAndreas Gruenbacher 	rv = may_inc_ap_bio(mdev);
23598869d683SPhilipp Reisner 	if (rv)
23608869d683SPhilipp Reisner 		atomic_add(count, &mdev->ap_bio_cnt);
23618869d683SPhilipp Reisner 	spin_unlock_irq(&mdev->req_lock);
23628869d683SPhilipp Reisner 
23638869d683SPhilipp Reisner 	return rv;
23648869d683SPhilipp Reisner }
23658869d683SPhilipp Reisner 
23669a25a04cSPhilipp Reisner static inline void inc_ap_bio(struct drbd_conf *mdev, int count)
2367b411b363SPhilipp Reisner {
2368b411b363SPhilipp Reisner 	/* we wait here
2369b411b363SPhilipp Reisner 	 *    as long as the device is suspended
2370b411b363SPhilipp Reisner 	 *    until the bitmap is no longer on the fly during connection
2371b411b363SPhilipp Reisner 	 *    handshake as long as we would exeed the max_buffer limit.
2372b411b363SPhilipp Reisner 	 *
2373b411b363SPhilipp Reisner 	 * to avoid races with the reconnect code,
2374b411b363SPhilipp Reisner 	 * we need to atomic_inc within the spinlock. */
2375b411b363SPhilipp Reisner 
23761b881ef7SAndreas Gruenbacher 	wait_event(mdev->misc_wait, inc_ap_bio_cond(mdev, count));
2377b411b363SPhilipp Reisner }
2378b411b363SPhilipp Reisner 
2379b411b363SPhilipp Reisner static inline void dec_ap_bio(struct drbd_conf *mdev)
2380b411b363SPhilipp Reisner {
2381b411b363SPhilipp Reisner 	int mxb = drbd_get_max_buffers(mdev);
2382b411b363SPhilipp Reisner 	int ap_bio = atomic_dec_return(&mdev->ap_bio_cnt);
2383b411b363SPhilipp Reisner 
2384b411b363SPhilipp Reisner 	D_ASSERT(ap_bio >= 0);
2385b411b363SPhilipp Reisner 	/* this currently does wake_up for every dec_ap_bio!
2386b411b363SPhilipp Reisner 	 * maybe rather introduce some type of hysteresis?
2387b411b363SPhilipp Reisner 	 * e.g. (ap_bio == mxb/2 || ap_bio == 0) ? */
2388b411b363SPhilipp Reisner 	if (ap_bio < mxb)
2389b411b363SPhilipp Reisner 		wake_up(&mdev->misc_wait);
2390b411b363SPhilipp Reisner 	if (ap_bio == 0 && test_bit(BITMAP_IO, &mdev->flags)) {
2391b411b363SPhilipp Reisner 		if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
2392b411b363SPhilipp Reisner 			drbd_queue_work(&mdev->data.work, &mdev->bm_io_work.w);
2393b411b363SPhilipp Reisner 	}
2394b411b363SPhilipp Reisner }
2395b411b363SPhilipp Reisner 
239662b0da3aSLars Ellenberg static inline int drbd_set_ed_uuid(struct drbd_conf *mdev, u64 val)
2397b411b363SPhilipp Reisner {
239862b0da3aSLars Ellenberg 	int changed = mdev->ed_uuid != val;
2399b411b363SPhilipp Reisner 	mdev->ed_uuid = val;
240062b0da3aSLars Ellenberg 	return changed;
2401b411b363SPhilipp Reisner }
2402b411b363SPhilipp Reisner 
2403b411b363SPhilipp Reisner static inline int seq_cmp(u32 a, u32 b)
2404b411b363SPhilipp Reisner {
2405b411b363SPhilipp Reisner 	/* we assume wrap around at 32bit.
2406b411b363SPhilipp Reisner 	 * for wrap around at 24bit (old atomic_t),
2407b411b363SPhilipp Reisner 	 * we'd have to
2408b411b363SPhilipp Reisner 	 *  a <<= 8; b <<= 8;
2409b411b363SPhilipp Reisner 	 */
2410b411b363SPhilipp Reisner 	return (s32)(a) - (s32)(b);
2411b411b363SPhilipp Reisner }
2412b411b363SPhilipp Reisner #define seq_lt(a, b) (seq_cmp((a), (b)) < 0)
2413b411b363SPhilipp Reisner #define seq_gt(a, b) (seq_cmp((a), (b)) > 0)
2414b411b363SPhilipp Reisner #define seq_ge(a, b) (seq_cmp((a), (b)) >= 0)
2415b411b363SPhilipp Reisner #define seq_le(a, b) (seq_cmp((a), (b)) <= 0)
2416b411b363SPhilipp Reisner /* CAUTION: please no side effects in arguments! */
2417b411b363SPhilipp Reisner #define seq_max(a, b) ((u32)(seq_gt((a), (b)) ? (a) : (b)))
2418b411b363SPhilipp Reisner 
2419b411b363SPhilipp Reisner static inline void update_peer_seq(struct drbd_conf *mdev, unsigned int new_seq)
2420b411b363SPhilipp Reisner {
2421b411b363SPhilipp Reisner 	unsigned int m;
2422b411b363SPhilipp Reisner 	spin_lock(&mdev->peer_seq_lock);
2423b411b363SPhilipp Reisner 	m = seq_max(mdev->peer_seq, new_seq);
2424b411b363SPhilipp Reisner 	mdev->peer_seq = m;
2425b411b363SPhilipp Reisner 	spin_unlock(&mdev->peer_seq_lock);
2426b411b363SPhilipp Reisner 	if (m == new_seq)
2427b411b363SPhilipp Reisner 		wake_up(&mdev->seq_wait);
2428b411b363SPhilipp Reisner }
2429b411b363SPhilipp Reisner 
2430b411b363SPhilipp Reisner static inline void drbd_update_congested(struct drbd_conf *mdev)
2431b411b363SPhilipp Reisner {
2432b411b363SPhilipp Reisner 	struct sock *sk = mdev->data.socket->sk;
2433b411b363SPhilipp Reisner 	if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
2434b411b363SPhilipp Reisner 		set_bit(NET_CONGESTED, &mdev->flags);
2435b411b363SPhilipp Reisner }
2436b411b363SPhilipp Reisner 
2437b411b363SPhilipp Reisner static inline int drbd_queue_order_type(struct drbd_conf *mdev)
2438b411b363SPhilipp Reisner {
2439b411b363SPhilipp Reisner 	/* sorry, we currently have no working implementation
2440b411b363SPhilipp Reisner 	 * of distributed TCQ stuff */
2441b411b363SPhilipp Reisner #ifndef QUEUE_ORDERED_NONE
2442b411b363SPhilipp Reisner #define QUEUE_ORDERED_NONE 0
2443b411b363SPhilipp Reisner #endif
2444b411b363SPhilipp Reisner 	return QUEUE_ORDERED_NONE;
2445b411b363SPhilipp Reisner }
2446b411b363SPhilipp Reisner 
2447b411b363SPhilipp Reisner static inline void drbd_md_flush(struct drbd_conf *mdev)
2448b411b363SPhilipp Reisner {
2449b411b363SPhilipp Reisner 	int r;
2450b411b363SPhilipp Reisner 
2451a8a4e51eSPhilipp Reisner 	if (test_bit(MD_NO_FUA, &mdev->flags))
2452b411b363SPhilipp Reisner 		return;
2453b411b363SPhilipp Reisner 
2454dd3932edSChristoph Hellwig 	r = blkdev_issue_flush(mdev->ldev->md_bdev, GFP_KERNEL, NULL);
2455b411b363SPhilipp Reisner 	if (r) {
2456a8a4e51eSPhilipp Reisner 		set_bit(MD_NO_FUA, &mdev->flags);
2457b411b363SPhilipp Reisner 		dev_err(DEV, "meta data flush failed with status %d, disabling md-flushes\n", r);
2458b411b363SPhilipp Reisner 	}
2459b411b363SPhilipp Reisner }
2460b411b363SPhilipp Reisner 
2461b411b363SPhilipp Reisner #endif
2462