xref: /linux/drivers/block/drbd/drbd_int.h (revision d752b2696072ed52fd5afab08b601e2220a3b87e)
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/list.h>
32b411b363SPhilipp Reisner #include <linux/sched.h>
33b411b363SPhilipp Reisner #include <linux/bitops.h>
34b411b363SPhilipp Reisner #include <linux/slab.h>
35b411b363SPhilipp Reisner #include <linux/crypto.h>
36132cc538SRandy Dunlap #include <linux/ratelimit.h>
37b411b363SPhilipp Reisner #include <linux/tcp.h>
38b411b363SPhilipp Reisner #include <linux/mutex.h>
39b411b363SPhilipp Reisner #include <linux/major.h>
40b411b363SPhilipp Reisner #include <linux/blkdev.h>
41b411b363SPhilipp Reisner #include <linux/genhd.h>
42062e879cSPhilipp Reisner #include <linux/idr.h>
43b411b363SPhilipp Reisner #include <net/tcp.h>
44b411b363SPhilipp Reisner #include <linux/lru_cache.h>
4570c71606SPaul Gortmaker #include <linux/prefetch.h>
463b98c0c2SLars Ellenberg #include <linux/drbd_genl_api.h>
47b8907339SPhilipp Reisner #include <linux/drbd.h>
48b8907339SPhilipp Reisner #include "drbd_state.h"
49b411b363SPhilipp Reisner 
50b411b363SPhilipp Reisner #ifdef __CHECKER__
51b411b363SPhilipp Reisner # define __protected_by(x)       __attribute__((require_context(x,1,999,"rdwr")))
52b411b363SPhilipp Reisner # define __protected_read_by(x)  __attribute__((require_context(x,1,999,"read")))
53b411b363SPhilipp Reisner # define __protected_write_by(x) __attribute__((require_context(x,1,999,"write")))
54b411b363SPhilipp Reisner # define __must_hold(x)       __attribute__((context(x,1,1), require_context(x,1,999,"call")))
55b411b363SPhilipp Reisner #else
56b411b363SPhilipp Reisner # define __protected_by(x)
57b411b363SPhilipp Reisner # define __protected_read_by(x)
58b411b363SPhilipp Reisner # define __protected_write_by(x)
59b411b363SPhilipp Reisner # define __must_hold(x)
60b411b363SPhilipp Reisner #endif
61b411b363SPhilipp Reisner 
62b411b363SPhilipp Reisner #define __no_warn(lock, stmt) do { __acquire(lock); stmt; __release(lock); } while (0)
63b411b363SPhilipp Reisner 
64b411b363SPhilipp Reisner /* module parameter, defined in drbd_main.c */
65b411b363SPhilipp Reisner extern unsigned int minor_count;
6690ab5ee9SRusty Russell extern bool disable_sendpage;
6790ab5ee9SRusty Russell extern bool allow_oos;
68b411b363SPhilipp Reisner 
69b411b363SPhilipp Reisner #ifdef CONFIG_DRBD_FAULT_INJECTION
70b411b363SPhilipp Reisner extern int enable_faults;
71b411b363SPhilipp Reisner extern int fault_rate;
72b411b363SPhilipp Reisner extern int fault_devs;
73b411b363SPhilipp Reisner #endif
74b411b363SPhilipp Reisner 
75b411b363SPhilipp Reisner extern char usermode_helper[];
76b411b363SPhilipp Reisner 
77b411b363SPhilipp Reisner 
78b411b363SPhilipp Reisner /* I don't remember why XCPU ...
79b411b363SPhilipp Reisner  * This is used to wake the asender,
80b411b363SPhilipp Reisner  * and to interrupt sending the sending task
81b411b363SPhilipp Reisner  * on disconnect.
82b411b363SPhilipp Reisner  */
83b411b363SPhilipp Reisner #define DRBD_SIG SIGXCPU
84b411b363SPhilipp Reisner 
85b411b363SPhilipp Reisner /* This is used to stop/restart our threads.
86b411b363SPhilipp Reisner  * Cannot use SIGTERM nor SIGKILL, since these
87b411b363SPhilipp Reisner  * are sent out by init on runlevel changes
88b411b363SPhilipp Reisner  * I choose SIGHUP for now.
89b411b363SPhilipp Reisner  */
90b411b363SPhilipp Reisner #define DRBD_SIGKILL SIGHUP
91b411b363SPhilipp Reisner 
92b411b363SPhilipp Reisner #define ID_IN_SYNC      (4711ULL)
93b411b363SPhilipp Reisner #define ID_OUT_OF_SYNC  (4712ULL)
94b411b363SPhilipp Reisner #define ID_SYNCER (-1ULL)
95579b57edSAndreas Gruenbacher 
964a23f264SPhilipp Reisner #define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
97b411b363SPhilipp Reisner 
98b411b363SPhilipp Reisner struct drbd_conf;
992111438bSPhilipp Reisner struct drbd_tconn;
100b411b363SPhilipp Reisner 
101b411b363SPhilipp Reisner 
102b411b363SPhilipp Reisner /* to shorten dev_warn(DEV, "msg"); and relatives statements */
103b411b363SPhilipp Reisner #define DEV (disk_to_dev(mdev->vdisk))
104b411b363SPhilipp Reisner 
10560ae4966SPhilipp Reisner #define conn_printk(LEVEL, TCONN, FMT, ARGS...) \
10660ae4966SPhilipp Reisner 	printk(LEVEL "d-con %s: " FMT, TCONN->name , ## ARGS)
10760ae4966SPhilipp Reisner #define conn_alert(TCONN, FMT, ARGS...)  conn_printk(KERN_ALERT, TCONN, FMT, ## ARGS)
10860ae4966SPhilipp Reisner #define conn_crit(TCONN, FMT, ARGS...)   conn_printk(KERN_CRIT, TCONN, FMT, ## ARGS)
10960ae4966SPhilipp Reisner #define conn_err(TCONN, FMT, ARGS...)    conn_printk(KERN_ERR, TCONN, FMT, ## ARGS)
11060ae4966SPhilipp Reisner #define conn_warn(TCONN, FMT, ARGS...)   conn_printk(KERN_WARNING, TCONN, FMT, ## ARGS)
11160ae4966SPhilipp Reisner #define conn_notice(TCONN, FMT, ARGS...) conn_printk(KERN_NOTICE, TCONN, FMT, ## ARGS)
11260ae4966SPhilipp Reisner #define conn_info(TCONN, FMT, ARGS...)   conn_printk(KERN_INFO, TCONN, FMT, ## ARGS)
11360ae4966SPhilipp Reisner #define conn_dbg(TCONN, FMT, ARGS...)    conn_printk(KERN_DEBUG, TCONN, FMT, ## ARGS)
11460ae4966SPhilipp Reisner 
115b411b363SPhilipp Reisner #define D_ASSERT(exp)	if (!(exp)) \
116b411b363SPhilipp Reisner 	 dev_err(DEV, "ASSERT( " #exp " ) in %s:%d\n", __FILE__, __LINE__)
117b411b363SPhilipp Reisner 
118841ce241SAndreas Gruenbacher /**
119841ce241SAndreas Gruenbacher  * expect  -  Make an assertion
120841ce241SAndreas Gruenbacher  *
121841ce241SAndreas Gruenbacher  * Unlike the assert macro, this macro returns a boolean result.
122841ce241SAndreas Gruenbacher  */
123841ce241SAndreas Gruenbacher #define expect(exp) ({								\
124841ce241SAndreas Gruenbacher 		bool _bool = (exp);						\
125841ce241SAndreas Gruenbacher 		if (!_bool)							\
126841ce241SAndreas Gruenbacher 			dev_err(DEV, "ASSERTION %s FAILED in %s\n",		\
127841ce241SAndreas Gruenbacher 			        #exp, __func__);				\
128841ce241SAndreas Gruenbacher 		_bool;								\
129841ce241SAndreas Gruenbacher 		})
130b411b363SPhilipp Reisner 
131b411b363SPhilipp Reisner /* Defines to control fault insertion */
132b411b363SPhilipp Reisner enum {
133b411b363SPhilipp Reisner 	DRBD_FAULT_MD_WR = 0,	/* meta data write */
134b411b363SPhilipp Reisner 	DRBD_FAULT_MD_RD = 1,	/*           read  */
135b411b363SPhilipp Reisner 	DRBD_FAULT_RS_WR = 2,	/* resync          */
136b411b363SPhilipp Reisner 	DRBD_FAULT_RS_RD = 3,
137b411b363SPhilipp Reisner 	DRBD_FAULT_DT_WR = 4,	/* data            */
138b411b363SPhilipp Reisner 	DRBD_FAULT_DT_RD = 5,
139b411b363SPhilipp Reisner 	DRBD_FAULT_DT_RA = 6,	/* data read ahead */
140b411b363SPhilipp Reisner 	DRBD_FAULT_BM_ALLOC = 7,	/* bitmap allocation */
141b411b363SPhilipp Reisner 	DRBD_FAULT_AL_EE = 8,	/* alloc ee */
1426b4388acSPhilipp Reisner 	DRBD_FAULT_RECEIVE = 9, /* Changes some bytes upon receiving a [rs]data block */
143b411b363SPhilipp Reisner 
144b411b363SPhilipp Reisner 	DRBD_FAULT_MAX,
145b411b363SPhilipp Reisner };
146b411b363SPhilipp Reisner 
147b411b363SPhilipp Reisner extern unsigned int
148b411b363SPhilipp Reisner _drbd_insert_fault(struct drbd_conf *mdev, unsigned int type);
1490cf9d27eSAndreas Gruenbacher 
150b411b363SPhilipp Reisner static inline int
151b411b363SPhilipp Reisner drbd_insert_fault(struct drbd_conf *mdev, unsigned int type) {
1520cf9d27eSAndreas Gruenbacher #ifdef CONFIG_DRBD_FAULT_INJECTION
153b411b363SPhilipp Reisner 	return fault_rate &&
154b411b363SPhilipp Reisner 		(enable_faults & (1<<type)) &&
155b411b363SPhilipp Reisner 		_drbd_insert_fault(mdev, type);
156b411b363SPhilipp Reisner #else
1570cf9d27eSAndreas Gruenbacher 	return 0;
158b411b363SPhilipp Reisner #endif
1590cf9d27eSAndreas Gruenbacher }
160b411b363SPhilipp Reisner 
161b411b363SPhilipp Reisner /* integer division, round _UP_ to the next integer */
162b411b363SPhilipp Reisner #define div_ceil(A, B) ((A)/(B) + ((A)%(B) ? 1 : 0))
163b411b363SPhilipp Reisner /* usual integer division */
164b411b363SPhilipp Reisner #define div_floor(A, B) ((A)/(B))
165b411b363SPhilipp Reisner 
166b411b363SPhilipp Reisner extern struct ratelimit_state drbd_ratelimit_state;
167c141ebdaSPhilipp Reisner extern struct idr minors; /* RCU, updates: genl_lock() */
168c141ebdaSPhilipp Reisner extern struct list_head drbd_tconns; /* RCU, updates: genl_lock() */
169b411b363SPhilipp Reisner 
170b411b363SPhilipp Reisner /* on the wire */
171d8763023SAndreas Gruenbacher enum drbd_packet {
172b411b363SPhilipp Reisner 	/* receiver (data socket) */
173b411b363SPhilipp Reisner 	P_DATA		      = 0x00,
174b411b363SPhilipp Reisner 	P_DATA_REPLY	      = 0x01, /* Response to P_DATA_REQUEST */
175b411b363SPhilipp Reisner 	P_RS_DATA_REPLY	      = 0x02, /* Response to P_RS_DATA_REQUEST */
176b411b363SPhilipp Reisner 	P_BARRIER	      = 0x03,
177b411b363SPhilipp Reisner 	P_BITMAP	      = 0x04,
178b411b363SPhilipp Reisner 	P_BECOME_SYNC_TARGET  = 0x05,
179b411b363SPhilipp Reisner 	P_BECOME_SYNC_SOURCE  = 0x06,
180b411b363SPhilipp Reisner 	P_UNPLUG_REMOTE	      = 0x07, /* Used at various times to hint the peer */
181b411b363SPhilipp Reisner 	P_DATA_REQUEST	      = 0x08, /* Used to ask for a data block */
182b411b363SPhilipp Reisner 	P_RS_DATA_REQUEST     = 0x09, /* Used to ask for a data block for resync */
183b411b363SPhilipp Reisner 	P_SYNC_PARAM	      = 0x0a,
184b411b363SPhilipp Reisner 	P_PROTOCOL	      = 0x0b,
185b411b363SPhilipp Reisner 	P_UUIDS		      = 0x0c,
186b411b363SPhilipp Reisner 	P_SIZES		      = 0x0d,
187b411b363SPhilipp Reisner 	P_STATE		      = 0x0e,
188b411b363SPhilipp Reisner 	P_SYNC_UUID	      = 0x0f,
189b411b363SPhilipp Reisner 	P_AUTH_CHALLENGE      = 0x10,
190b411b363SPhilipp Reisner 	P_AUTH_RESPONSE	      = 0x11,
191b411b363SPhilipp Reisner 	P_STATE_CHG_REQ	      = 0x12,
192b411b363SPhilipp Reisner 
193b411b363SPhilipp Reisner 	/* asender (meta socket */
194b411b363SPhilipp Reisner 	P_PING		      = 0x13,
195b411b363SPhilipp Reisner 	P_PING_ACK	      = 0x14,
196b411b363SPhilipp Reisner 	P_RECV_ACK	      = 0x15, /* Used in protocol B */
197b411b363SPhilipp Reisner 	P_WRITE_ACK	      = 0x16, /* Used in protocol C */
198b411b363SPhilipp Reisner 	P_RS_WRITE_ACK	      = 0x17, /* Is a P_WRITE_ACK, additionally call set_in_sync(). */
199d4dabbe2SLars Ellenberg 	P_SUPERSEDED	      = 0x18, /* Used in proto C, two-primaries conflict detection */
200b411b363SPhilipp Reisner 	P_NEG_ACK	      = 0x19, /* Sent if local disk is unusable */
201b411b363SPhilipp Reisner 	P_NEG_DREPLY	      = 0x1a, /* Local disk is broken... */
202b411b363SPhilipp Reisner 	P_NEG_RS_DREPLY	      = 0x1b, /* Local disk is broken... */
203b411b363SPhilipp Reisner 	P_BARRIER_ACK	      = 0x1c,
204b411b363SPhilipp Reisner 	P_STATE_CHG_REPLY     = 0x1d,
205b411b363SPhilipp Reisner 
206b411b363SPhilipp Reisner 	/* "new" commands, no longer fitting into the ordering scheme above */
207b411b363SPhilipp Reisner 
208b411b363SPhilipp Reisner 	P_OV_REQUEST	      = 0x1e, /* data socket */
209b411b363SPhilipp Reisner 	P_OV_REPLY	      = 0x1f,
210b411b363SPhilipp Reisner 	P_OV_RESULT	      = 0x20, /* meta socket */
211b411b363SPhilipp Reisner 	P_CSUM_RS_REQUEST     = 0x21, /* data socket */
212b411b363SPhilipp Reisner 	P_RS_IS_IN_SYNC	      = 0x22, /* meta socket */
213b411b363SPhilipp Reisner 	P_SYNC_PARAM89	      = 0x23, /* data socket, protocol version 89 replacement for P_SYNC_PARAM */
214b411b363SPhilipp Reisner 	P_COMPRESSED_BITMAP   = 0x24, /* compressed or otherwise encoded bitmap transfer */
2150ced55a3SPhilipp Reisner 	/* P_CKPT_FENCE_REQ      = 0x25, * currently reserved for protocol D */
2160ced55a3SPhilipp Reisner 	/* P_CKPT_DISABLE_REQ    = 0x26, * currently reserved for protocol D */
2170ced55a3SPhilipp Reisner 	P_DELAY_PROBE         = 0x27, /* is used on BOTH sockets */
21873a01a18SPhilipp Reisner 	P_OUT_OF_SYNC         = 0x28, /* Mark as out of sync (Outrunning), data socket */
219d612d309SPhilipp Reisner 	P_RS_CANCEL           = 0x29, /* meta: Used to cancel RS_DATA_REQUEST packet by SyncSource */
220cf29c9d8SPhilipp Reisner 	P_CONN_ST_CHG_REQ     = 0x2a, /* data sock: Connection wide state request */
221cf29c9d8SPhilipp Reisner 	P_CONN_ST_CHG_REPLY   = 0x2b, /* meta sock: Connection side state req reply */
2227be8da07SAndreas Gruenbacher 	P_RETRY_WRITE	      = 0x2c, /* Protocol C: retry conflicting write request */
223036b17eaSPhilipp Reisner 	P_PROTOCOL_UPDATE     = 0x2d, /* data sock: is used in established connections */
224b411b363SPhilipp Reisner 
225b411b363SPhilipp Reisner 	P_MAY_IGNORE	      = 0x100, /* Flag to test if (cmd > P_MAY_IGNORE) ... */
226b411b363SPhilipp Reisner 	P_MAX_OPT_CMD	      = 0x101,
227b411b363SPhilipp Reisner 
228b411b363SPhilipp Reisner 	/* special command ids for handshake */
229b411b363SPhilipp Reisner 
230e5d6f33aSAndreas Gruenbacher 	P_INITIAL_META	      = 0xfff1, /* First Packet on the MetaSock */
231e5d6f33aSAndreas Gruenbacher 	P_INITIAL_DATA	      = 0xfff2, /* First Packet on the Socket */
232b411b363SPhilipp Reisner 
2336038178eSAndreas Gruenbacher 	P_CONNECTION_FEATURES = 0xfffe	/* FIXED for the next century! */
234b411b363SPhilipp Reisner };
235b411b363SPhilipp Reisner 
236d8763023SAndreas Gruenbacher extern const char *cmdname(enum drbd_packet cmd);
237b411b363SPhilipp Reisner 
238b411b363SPhilipp Reisner /* for sending/receiving the bitmap,
239b411b363SPhilipp Reisner  * possibly in some encoding scheme */
240b411b363SPhilipp Reisner struct bm_xfer_ctx {
241b411b363SPhilipp Reisner 	/* "const"
242b411b363SPhilipp Reisner 	 * stores total bits and long words
243b411b363SPhilipp Reisner 	 * of the bitmap, so we don't need to
244b411b363SPhilipp Reisner 	 * call the accessor functions over and again. */
245b411b363SPhilipp Reisner 	unsigned long bm_bits;
246b411b363SPhilipp Reisner 	unsigned long bm_words;
247b411b363SPhilipp Reisner 	/* during xfer, current position within the bitmap */
248b411b363SPhilipp Reisner 	unsigned long bit_offset;
249b411b363SPhilipp Reisner 	unsigned long word_offset;
250b411b363SPhilipp Reisner 
251b411b363SPhilipp Reisner 	/* statistics; index: (h->command == P_BITMAP) */
252b411b363SPhilipp Reisner 	unsigned packets[2];
253b411b363SPhilipp Reisner 	unsigned bytes[2];
254b411b363SPhilipp Reisner };
255b411b363SPhilipp Reisner 
256b411b363SPhilipp Reisner extern void INFO_bm_xfer_stats(struct drbd_conf *mdev,
257b411b363SPhilipp Reisner 		const char *direction, struct bm_xfer_ctx *c);
258b411b363SPhilipp Reisner 
259b411b363SPhilipp Reisner static inline void bm_xfer_ctx_bit_to_word_offset(struct bm_xfer_ctx *c)
260b411b363SPhilipp Reisner {
261b411b363SPhilipp Reisner 	/* word_offset counts "native long words" (32 or 64 bit),
262b411b363SPhilipp Reisner 	 * aligned at 64 bit.
263b411b363SPhilipp Reisner 	 * Encoded packet may end at an unaligned bit offset.
264b411b363SPhilipp Reisner 	 * In case a fallback clear text packet is transmitted in
265b411b363SPhilipp Reisner 	 * between, we adjust this offset back to the last 64bit
266b411b363SPhilipp Reisner 	 * aligned "native long word", which makes coding and decoding
267b411b363SPhilipp Reisner 	 * the plain text bitmap much more convenient.  */
268b411b363SPhilipp Reisner #if BITS_PER_LONG == 64
269b411b363SPhilipp Reisner 	c->word_offset = c->bit_offset >> 6;
270b411b363SPhilipp Reisner #elif BITS_PER_LONG == 32
271b411b363SPhilipp Reisner 	c->word_offset = c->bit_offset >> 5;
272b411b363SPhilipp Reisner 	c->word_offset &= ~(1UL);
273b411b363SPhilipp Reisner #else
274b411b363SPhilipp Reisner # error "unsupported BITS_PER_LONG"
275b411b363SPhilipp Reisner #endif
276b411b363SPhilipp Reisner }
277b411b363SPhilipp Reisner 
278b411b363SPhilipp Reisner #ifndef __packed
279b411b363SPhilipp Reisner #define __packed __attribute__((packed))
280b411b363SPhilipp Reisner #endif
281b411b363SPhilipp Reisner 
282b411b363SPhilipp Reisner /* This is the layout for a packet on the wire.
283b411b363SPhilipp Reisner  * The byteorder is the network byte order.
284b411b363SPhilipp Reisner  *     (except block_id and barrier fields.
285b411b363SPhilipp Reisner  *	these are pointers to local structs
286b411b363SPhilipp Reisner  *	and have no relevance for the partner,
287b411b363SPhilipp Reisner  *	which just echoes them as received.)
288b411b363SPhilipp Reisner  *
289b411b363SPhilipp Reisner  * NOTE that the payload starts at a long aligned offset,
290b411b363SPhilipp Reisner  * regardless of 32 or 64 bit arch!
291b411b363SPhilipp Reisner  */
2920b70a13dSPhilipp Reisner struct p_header80 {
293b411b363SPhilipp Reisner 	u32	  magic;
294b411b363SPhilipp Reisner 	u16	  command;
295b411b363SPhilipp Reisner 	u16	  length;	/* bytes of data after this header */
296b411b363SPhilipp Reisner } __packed;
2970b70a13dSPhilipp Reisner 
2980b70a13dSPhilipp Reisner /* Header for big packets, Used for data packets exceeding 64kB */
2990b70a13dSPhilipp Reisner struct p_header95 {
3000b70a13dSPhilipp Reisner 	u16	  magic;	/* use DRBD_MAGIC_BIG here */
3010b70a13dSPhilipp Reisner 	u16	  command;
302b55d84baSAndreas Gruenbacher 	u32	  length;
3030b70a13dSPhilipp Reisner } __packed;
3040b70a13dSPhilipp Reisner 
3050c8e36d9SAndreas Gruenbacher struct p_header100 {
3060c8e36d9SAndreas Gruenbacher 	u32	  magic;
3070c8e36d9SAndreas Gruenbacher 	u16	  volume;
3080c8e36d9SAndreas Gruenbacher 	u16	  command;
3090c8e36d9SAndreas Gruenbacher 	u32	  length;
3100c8e36d9SAndreas Gruenbacher 	u32	  pad;
3110c8e36d9SAndreas Gruenbacher } __packed;
312b411b363SPhilipp Reisner 
31352b061a4SAndreas Gruenbacher extern unsigned int drbd_header_size(struct drbd_tconn *tconn);
314b411b363SPhilipp Reisner 
315b411b363SPhilipp Reisner /* these defines must not be changed without changing the protocol version */
31676d2e7ecSPhilipp Reisner #define DP_HARDBARRIER	      1 /* depricated */
31776d2e7ecSPhilipp Reisner #define DP_RW_SYNC	      2 /* equals REQ_SYNC    */
318b411b363SPhilipp Reisner #define DP_MAY_SET_IN_SYNC    4
319721a9602SJens Axboe #define DP_UNPLUG             8 /* not used anymore   */
32076d2e7ecSPhilipp Reisner #define DP_FUA               16 /* equals REQ_FUA     */
32176d2e7ecSPhilipp Reisner #define DP_FLUSH             32 /* equals REQ_FLUSH   */
32276d2e7ecSPhilipp Reisner #define DP_DISCARD           64 /* equals REQ_DISCARD */
323303d1448SPhilipp Reisner #define DP_SEND_RECEIVE_ACK 128 /* This is a proto B write request */
324303d1448SPhilipp Reisner #define DP_SEND_WRITE_ACK   256 /* This is a proto C write request */
325b411b363SPhilipp Reisner 
326b411b363SPhilipp Reisner struct p_data {
327b411b363SPhilipp Reisner 	u64	    sector;    /* 64 bits sector number */
328b411b363SPhilipp Reisner 	u64	    block_id;  /* to identify the request in protocol B&C */
329b411b363SPhilipp Reisner 	u32	    seq_num;
330b411b363SPhilipp Reisner 	u32	    dp_flags;
331b411b363SPhilipp Reisner } __packed;
332b411b363SPhilipp Reisner 
333b411b363SPhilipp Reisner /*
334b411b363SPhilipp Reisner  * commands which share a struct:
335b411b363SPhilipp Reisner  *  p_block_ack:
336b411b363SPhilipp Reisner  *   P_RECV_ACK (proto B), P_WRITE_ACK (proto C),
337d4dabbe2SLars Ellenberg  *   P_SUPERSEDED (proto C, two-primaries conflict detection)
338b411b363SPhilipp Reisner  *  p_block_req:
339b411b363SPhilipp Reisner  *   P_DATA_REQUEST, P_RS_DATA_REQUEST
340b411b363SPhilipp Reisner  */
341b411b363SPhilipp Reisner struct p_block_ack {
342b411b363SPhilipp Reisner 	u64	    sector;
343b411b363SPhilipp Reisner 	u64	    block_id;
344b411b363SPhilipp Reisner 	u32	    blksize;
345b411b363SPhilipp Reisner 	u32	    seq_num;
346b411b363SPhilipp Reisner } __packed;
347b411b363SPhilipp Reisner 
348b411b363SPhilipp Reisner struct p_block_req {
349b411b363SPhilipp Reisner 	u64 sector;
350b411b363SPhilipp Reisner 	u64 block_id;
351b411b363SPhilipp Reisner 	u32 blksize;
352b411b363SPhilipp Reisner 	u32 pad;	/* to multiple of 8 Byte */
353b411b363SPhilipp Reisner } __packed;
354b411b363SPhilipp Reisner 
355b411b363SPhilipp Reisner /*
356b411b363SPhilipp Reisner  * commands with their own struct for additional fields:
3576038178eSAndreas Gruenbacher  *   P_CONNECTION_FEATURES
358b411b363SPhilipp Reisner  *   P_BARRIER
359b411b363SPhilipp Reisner  *   P_BARRIER_ACK
360b411b363SPhilipp Reisner  *   P_SYNC_PARAM
361b411b363SPhilipp Reisner  *   ReportParams
362b411b363SPhilipp Reisner  */
363b411b363SPhilipp Reisner 
3646038178eSAndreas Gruenbacher struct p_connection_features {
365b411b363SPhilipp Reisner 	u32 protocol_min;
366b411b363SPhilipp Reisner 	u32 feature_flags;
367b411b363SPhilipp Reisner 	u32 protocol_max;
368b411b363SPhilipp Reisner 
369b411b363SPhilipp Reisner 	/* should be more than enough for future enhancements
370d942ae44SPhilipp Reisner 	 * for now, feature_flags and the reserved array shall be zero.
371b411b363SPhilipp Reisner 	 */
372b411b363SPhilipp Reisner 
373b411b363SPhilipp Reisner 	u32 _pad;
374d942ae44SPhilipp Reisner 	u64 reserved[7];
375b411b363SPhilipp Reisner } __packed;
376b411b363SPhilipp Reisner 
377b411b363SPhilipp Reisner struct p_barrier {
378b411b363SPhilipp Reisner 	u32 barrier;	/* barrier number _handle_ only */
379b411b363SPhilipp Reisner 	u32 pad;	/* to multiple of 8 Byte */
380b411b363SPhilipp Reisner } __packed;
381b411b363SPhilipp Reisner 
382b411b363SPhilipp Reisner struct p_barrier_ack {
383b411b363SPhilipp Reisner 	u32 barrier;
384b411b363SPhilipp Reisner 	u32 set_size;
385b411b363SPhilipp Reisner } __packed;
386b411b363SPhilipp Reisner 
387b411b363SPhilipp Reisner struct p_rs_param {
3886394b935SAndreas Gruenbacher 	u32 resync_rate;
389b411b363SPhilipp Reisner 
390b411b363SPhilipp Reisner 	      /* Since protocol version 88 and higher. */
391b411b363SPhilipp Reisner 	char verify_alg[0];
392b411b363SPhilipp Reisner } __packed;
393b411b363SPhilipp Reisner 
394b411b363SPhilipp Reisner struct p_rs_param_89 {
3956394b935SAndreas Gruenbacher 	u32 resync_rate;
396b411b363SPhilipp Reisner         /* protocol version 89: */
397b411b363SPhilipp Reisner 	char verify_alg[SHARED_SECRET_MAX];
398b411b363SPhilipp Reisner 	char csums_alg[SHARED_SECRET_MAX];
399b411b363SPhilipp Reisner } __packed;
400b411b363SPhilipp Reisner 
4018e26f9ccSPhilipp Reisner struct p_rs_param_95 {
4026394b935SAndreas Gruenbacher 	u32 resync_rate;
4038e26f9ccSPhilipp Reisner 	char verify_alg[SHARED_SECRET_MAX];
4048e26f9ccSPhilipp Reisner 	char csums_alg[SHARED_SECRET_MAX];
4058e26f9ccSPhilipp Reisner 	u32 c_plan_ahead;
4068e26f9ccSPhilipp Reisner 	u32 c_delay_target;
4078e26f9ccSPhilipp Reisner 	u32 c_fill_target;
4088e26f9ccSPhilipp Reisner 	u32 c_max_rate;
4098e26f9ccSPhilipp Reisner } __packed;
4108e26f9ccSPhilipp Reisner 
411cf14c2e9SPhilipp Reisner enum drbd_conn_flags {
4126139f60dSAndreas Gruenbacher 	CF_DISCARD_MY_DATA = 1,
413cf14c2e9SPhilipp Reisner 	CF_DRY_RUN = 2,
414cf14c2e9SPhilipp Reisner };
415cf14c2e9SPhilipp Reisner 
416b411b363SPhilipp Reisner struct p_protocol {
417b411b363SPhilipp Reisner 	u32 protocol;
418b411b363SPhilipp Reisner 	u32 after_sb_0p;
419b411b363SPhilipp Reisner 	u32 after_sb_1p;
420b411b363SPhilipp Reisner 	u32 after_sb_2p;
421cf14c2e9SPhilipp Reisner 	u32 conn_flags;
422b411b363SPhilipp Reisner 	u32 two_primaries;
423b411b363SPhilipp Reisner 
424b411b363SPhilipp Reisner               /* Since protocol version 87 and higher. */
425b411b363SPhilipp Reisner 	char integrity_alg[0];
426b411b363SPhilipp Reisner 
427b411b363SPhilipp Reisner } __packed;
428b411b363SPhilipp Reisner 
429b411b363SPhilipp Reisner struct p_uuids {
430b411b363SPhilipp Reisner 	u64 uuid[UI_EXTENDED_SIZE];
431b411b363SPhilipp Reisner } __packed;
432b411b363SPhilipp Reisner 
433b411b363SPhilipp Reisner struct p_rs_uuid {
434b411b363SPhilipp Reisner 	u64	    uuid;
435b411b363SPhilipp Reisner } __packed;
436b411b363SPhilipp Reisner 
437b411b363SPhilipp Reisner struct p_sizes {
438b411b363SPhilipp Reisner 	u64	    d_size;  /* size of disk */
439b411b363SPhilipp Reisner 	u64	    u_size;  /* user requested size */
440b411b363SPhilipp Reisner 	u64	    c_size;  /* current exported size */
4411816a2b4SLars Ellenberg 	u32	    max_bio_size;  /* Maximal size of a BIO */
442e89b591cSPhilipp Reisner 	u16	    queue_order_type;  /* not yet implemented in DRBD*/
443e89b591cSPhilipp Reisner 	u16	    dds_flags; /* use enum dds_flags here. */
444b411b363SPhilipp Reisner } __packed;
445b411b363SPhilipp Reisner 
446b411b363SPhilipp Reisner struct p_state {
447b411b363SPhilipp Reisner 	u32	    state;
448b411b363SPhilipp Reisner } __packed;
449b411b363SPhilipp Reisner 
450b411b363SPhilipp Reisner struct p_req_state {
451b411b363SPhilipp Reisner 	u32	    mask;
452b411b363SPhilipp Reisner 	u32	    val;
453b411b363SPhilipp Reisner } __packed;
454b411b363SPhilipp Reisner 
455b411b363SPhilipp Reisner struct p_req_state_reply {
456b411b363SPhilipp Reisner 	u32	    retcode;
457b411b363SPhilipp Reisner } __packed;
458b411b363SPhilipp Reisner 
459b411b363SPhilipp Reisner struct p_drbd06_param {
460b411b363SPhilipp Reisner 	u64	  size;
461b411b363SPhilipp Reisner 	u32	  state;
462b411b363SPhilipp Reisner 	u32	  blksize;
463b411b363SPhilipp Reisner 	u32	  protocol;
464b411b363SPhilipp Reisner 	u32	  version;
465b411b363SPhilipp Reisner 	u32	  gen_cnt[5];
466b411b363SPhilipp Reisner 	u32	  bit_map_gen[5];
467b411b363SPhilipp Reisner } __packed;
468b411b363SPhilipp Reisner 
46973a01a18SPhilipp Reisner struct p_block_desc {
47073a01a18SPhilipp Reisner 	u64 sector;
47173a01a18SPhilipp Reisner 	u32 blksize;
47273a01a18SPhilipp Reisner 	u32 pad;	/* to multiple of 8 Byte */
47373a01a18SPhilipp Reisner } __packed;
47473a01a18SPhilipp Reisner 
475b411b363SPhilipp Reisner /* Valid values for the encoding field.
476b411b363SPhilipp Reisner  * Bump proto version when changing this. */
477b411b363SPhilipp Reisner enum drbd_bitmap_code {
478b411b363SPhilipp Reisner 	/* RLE_VLI_Bytes = 0,
479b411b363SPhilipp Reisner 	 * and other bit variants had been defined during
480b411b363SPhilipp Reisner 	 * algorithm evaluation. */
481b411b363SPhilipp Reisner 	RLE_VLI_Bits = 2,
482b411b363SPhilipp Reisner };
483b411b363SPhilipp Reisner 
484b411b363SPhilipp Reisner struct p_compressed_bm {
485b411b363SPhilipp Reisner 	/* (encoding & 0x0f): actual encoding, see enum drbd_bitmap_code
486b411b363SPhilipp Reisner 	 * (encoding & 0x80): polarity (set/unset) of first runlength
487b411b363SPhilipp Reisner 	 * ((encoding >> 4) & 0x07): pad_bits, number of trailing zero bits
488b411b363SPhilipp Reisner 	 * used to pad up to head.length bytes
489b411b363SPhilipp Reisner 	 */
490b411b363SPhilipp Reisner 	u8 encoding;
491b411b363SPhilipp Reisner 
492b411b363SPhilipp Reisner 	u8 code[0];
493b411b363SPhilipp Reisner } __packed;
494b411b363SPhilipp Reisner 
4950b70a13dSPhilipp Reisner struct p_delay_probe93 {
4960ced55a3SPhilipp Reisner 	u32     seq_num; /* sequence number to match the two probe packets */
4970ced55a3SPhilipp Reisner 	u32     offset;  /* usecs the probe got sent after the reference time point */
4980ced55a3SPhilipp Reisner } __packed;
4990ced55a3SPhilipp Reisner 
50050d0b1adSAndreas Gruenbacher /*
50150d0b1adSAndreas Gruenbacher  * Bitmap packets need to fit within a single page on the sender and receiver,
50250d0b1adSAndreas Gruenbacher  * so we are limited to 4 KiB (and not to PAGE_SIZE, which can be bigger).
503b411b363SPhilipp Reisner  */
504e6ef8a5cSAndreas Gruenbacher #define DRBD_SOCKET_BUFFER_SIZE 4096
505b411b363SPhilipp Reisner 
506b411b363SPhilipp Reisner /**********************************************************************/
507b411b363SPhilipp Reisner enum drbd_thread_state {
508e77a0a5cSAndreas Gruenbacher 	NONE,
509e77a0a5cSAndreas Gruenbacher 	RUNNING,
510e77a0a5cSAndreas Gruenbacher 	EXITING,
511e77a0a5cSAndreas Gruenbacher 	RESTARTING
512b411b363SPhilipp Reisner };
513b411b363SPhilipp Reisner 
514b411b363SPhilipp Reisner struct drbd_thread {
515b411b363SPhilipp Reisner 	spinlock_t t_lock;
516b411b363SPhilipp Reisner 	struct task_struct *task;
517b411b363SPhilipp Reisner 	struct completion stop;
518b411b363SPhilipp Reisner 	enum drbd_thread_state t_state;
519b411b363SPhilipp Reisner 	int (*function) (struct drbd_thread *);
520392c8801SPhilipp Reisner 	struct drbd_tconn *tconn;
521b411b363SPhilipp Reisner 	int reset_cpu_mask;
522bed879aeSPhilipp Reisner 	char name[9];
523b411b363SPhilipp Reisner };
524b411b363SPhilipp Reisner 
525b411b363SPhilipp Reisner static inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
526b411b363SPhilipp Reisner {
527b411b363SPhilipp Reisner 	/* THINK testing the t_state seems to be uncritical in all cases
528b411b363SPhilipp Reisner 	 * (but thread_{start,stop}), so we can read it *without* the lock.
529b411b363SPhilipp Reisner 	 *	--lge */
530b411b363SPhilipp Reisner 
531b411b363SPhilipp Reisner 	smp_rmb();
532b411b363SPhilipp Reisner 	return thi->t_state;
533b411b363SPhilipp Reisner }
534b411b363SPhilipp Reisner 
535b411b363SPhilipp Reisner struct drbd_work {
536b411b363SPhilipp Reisner 	struct list_head list;
537309a8348SAndreas Gruenbacher 	int (*cb)(struct drbd_work *, int cancel);
53800d56944SPhilipp Reisner 	union {
539a21e9298SPhilipp Reisner 		struct drbd_conf *mdev;
54000d56944SPhilipp Reisner 		struct drbd_tconn *tconn;
54100d56944SPhilipp Reisner 	};
542b411b363SPhilipp Reisner };
543b411b363SPhilipp Reisner 
544ace652acSAndreas Gruenbacher #include "drbd_interval.h"
545ace652acSAndreas Gruenbacher 
5467be8da07SAndreas Gruenbacher extern int drbd_wait_misc(struct drbd_conf *, struct drbd_interval *);
5477be8da07SAndreas Gruenbacher 
548b411b363SPhilipp Reisner struct drbd_request {
549b411b363SPhilipp Reisner 	struct drbd_work w;
550b411b363SPhilipp Reisner 
551b411b363SPhilipp Reisner 	/* if local IO is not allowed, will be NULL.
552b411b363SPhilipp Reisner 	 * if local IO _is_ allowed, holds the locally submitted bio clone,
553b411b363SPhilipp Reisner 	 * or, after local IO completion, the ERR_PTR(error).
554fcefa62eSAndreas Gruenbacher 	 * see drbd_request_endio(). */
555b411b363SPhilipp Reisner 	struct bio *private_bio;
556b411b363SPhilipp Reisner 
557ace652acSAndreas Gruenbacher 	struct drbd_interval i;
558b411b363SPhilipp Reisner 
559b6dd1a89SLars Ellenberg 	/* epoch: used to check on "completion" whether this req was in
560b411b363SPhilipp Reisner 	 * the current epoch, and we therefore have to close it,
561b6dd1a89SLars Ellenberg 	 * causing a p_barrier packet to be send, starting a new epoch.
562b6dd1a89SLars Ellenberg 	 *
563b6dd1a89SLars Ellenberg 	 * This corresponds to "barrier" in struct p_barrier[_ack],
564b6dd1a89SLars Ellenberg 	 * and to "barrier_nr" in struct drbd_epoch (and various
565b6dd1a89SLars Ellenberg 	 * comments/function parameters/local variable names).
566b411b363SPhilipp Reisner 	 */
567b6dd1a89SLars Ellenberg 	unsigned int epoch;
568b411b363SPhilipp Reisner 
569b411b363SPhilipp Reisner 	struct list_head tl_requests; /* ring list in the transfer log */
570b411b363SPhilipp Reisner 	struct bio *master_bio;       /* master bio pointer */
571b411b363SPhilipp Reisner 	unsigned long start_time;
572b406777eSLars Ellenberg 
573b406777eSLars Ellenberg 	/* once it hits 0, we may complete the master_bio */
574b406777eSLars Ellenberg 	atomic_t completion_ref;
575b406777eSLars Ellenberg 	/* once it hits 0, we may destroy this drbd_request object */
576b406777eSLars Ellenberg 	struct kref kref;
577a0d856dfSLars Ellenberg 
578a0d856dfSLars Ellenberg 	unsigned rq_state; /* see comments above _req_mod() */
579b411b363SPhilipp Reisner };
580b411b363SPhilipp Reisner 
581b411b363SPhilipp Reisner struct drbd_epoch {
5829ed57dcbSLars Ellenberg 	struct drbd_tconn *tconn;
583b411b363SPhilipp Reisner 	struct list_head list;
584b411b363SPhilipp Reisner 	unsigned int barrier_nr;
585b411b363SPhilipp Reisner 	atomic_t epoch_size; /* increased on every request added. */
586b411b363SPhilipp Reisner 	atomic_t active;     /* increased on every req. added, and dec on every finished. */
587b411b363SPhilipp Reisner 	unsigned long flags;
588b411b363SPhilipp Reisner };
589b411b363SPhilipp Reisner 
590b411b363SPhilipp Reisner /* drbd_epoch flag bits */
591b411b363SPhilipp Reisner enum {
592b411b363SPhilipp Reisner 	DE_HAVE_BARRIER_NUMBER,
593b411b363SPhilipp Reisner };
594b411b363SPhilipp Reisner 
595b411b363SPhilipp Reisner enum epoch_event {
596b411b363SPhilipp Reisner 	EV_PUT,
597b411b363SPhilipp Reisner 	EV_GOT_BARRIER_NR,
598b411b363SPhilipp Reisner 	EV_BECAME_LAST,
599b411b363SPhilipp Reisner 	EV_CLEANUP = 32, /* used as flag */
600b411b363SPhilipp Reisner };
601b411b363SPhilipp Reisner 
602b411b363SPhilipp Reisner struct drbd_wq_barrier {
603b411b363SPhilipp Reisner 	struct drbd_work w;
604b411b363SPhilipp Reisner 	struct completion done;
605b411b363SPhilipp Reisner };
606b411b363SPhilipp Reisner 
607b411b363SPhilipp Reisner struct digest_info {
608b411b363SPhilipp Reisner 	int digest_size;
609b411b363SPhilipp Reisner 	void *digest;
610b411b363SPhilipp Reisner };
611b411b363SPhilipp Reisner 
612f6ffca9fSAndreas Gruenbacher struct drbd_peer_request {
61345bb912bSLars Ellenberg 	struct drbd_work w;
61485719573SPhilipp Reisner 	struct drbd_epoch *epoch; /* for writes */
61545bb912bSLars Ellenberg 	struct page *pages;
61645bb912bSLars Ellenberg 	atomic_t pending_bios;
617010f6e67SAndreas Gruenbacher 	struct drbd_interval i;
61845bb912bSLars Ellenberg 	/* see comments on ee flag bits below */
61945bb912bSLars Ellenberg 	unsigned long flags;
62085719573SPhilipp Reisner 	union {
62145bb912bSLars Ellenberg 		u64 block_id;
62285719573SPhilipp Reisner 		struct digest_info *digest;
62385719573SPhilipp Reisner 	};
62445bb912bSLars Ellenberg };
62545bb912bSLars Ellenberg 
62645bb912bSLars Ellenberg /* ee flag bits.
62745bb912bSLars Ellenberg  * While corresponding bios are in flight, the only modification will be
62845bb912bSLars Ellenberg  * set_bit WAS_ERROR, which has to be atomic.
62945bb912bSLars Ellenberg  * If no bios are in flight yet, or all have been completed,
63045bb912bSLars Ellenberg  * non-atomic modification to ee->flags is ok.
63145bb912bSLars Ellenberg  */
632b411b363SPhilipp Reisner enum {
633b411b363SPhilipp Reisner 	__EE_CALL_AL_COMPLETE_IO,
634b411b363SPhilipp Reisner 	__EE_MAY_SET_IN_SYNC,
63545bb912bSLars Ellenberg 
63645bb912bSLars Ellenberg 	/* In case a barrier failed,
63745bb912bSLars Ellenberg 	 * we need to resubmit without the barrier flag. */
63845bb912bSLars Ellenberg 	__EE_RESUBMITTED,
63945bb912bSLars Ellenberg 
6406c852becSAndreas Gruenbacher 	/* we may have several bios per peer request.
64145bb912bSLars Ellenberg 	 * if any of those fail, we set this flag atomically
64245bb912bSLars Ellenberg 	 * from the endio callback */
64345bb912bSLars Ellenberg 	__EE_WAS_ERROR,
644c36c3cedSLars Ellenberg 
645c36c3cedSLars Ellenberg 	/* This ee has a pointer to a digest instead of a block id */
646c36c3cedSLars Ellenberg 	__EE_HAS_DIGEST,
6477be8da07SAndreas Gruenbacher 
6487be8da07SAndreas Gruenbacher 	/* Conflicting local requests need to be restarted after this request */
6497be8da07SAndreas Gruenbacher 	__EE_RESTART_REQUESTS,
650303d1448SPhilipp Reisner 
651303d1448SPhilipp Reisner 	/* The peer wants a write ACK for this (wire proto C) */
652303d1448SPhilipp Reisner 	__EE_SEND_WRITE_ACK,
653302bdeaeSPhilipp Reisner 
654302bdeaeSPhilipp Reisner 	/* Is set when net_conf had two_primaries set while creating this peer_req */
655302bdeaeSPhilipp Reisner 	__EE_IN_INTERVAL_TREE,
656b411b363SPhilipp Reisner };
657b411b363SPhilipp Reisner #define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
658b411b363SPhilipp Reisner #define EE_MAY_SET_IN_SYNC     (1<<__EE_MAY_SET_IN_SYNC)
65945bb912bSLars Ellenberg #define	EE_RESUBMITTED         (1<<__EE_RESUBMITTED)
66045bb912bSLars Ellenberg #define EE_WAS_ERROR           (1<<__EE_WAS_ERROR)
661c36c3cedSLars Ellenberg #define EE_HAS_DIGEST          (1<<__EE_HAS_DIGEST)
6627be8da07SAndreas Gruenbacher #define EE_RESTART_REQUESTS	(1<<__EE_RESTART_REQUESTS)
663303d1448SPhilipp Reisner #define EE_SEND_WRITE_ACK	(1<<__EE_SEND_WRITE_ACK)
664302bdeaeSPhilipp Reisner #define EE_IN_INTERVAL_TREE	(1<<__EE_IN_INTERVAL_TREE)
665b411b363SPhilipp Reisner 
66601a311a5SPhilipp Reisner /* flag bits per mdev */
667b411b363SPhilipp Reisner enum {
668b411b363SPhilipp Reisner 	UNPLUG_REMOTE,		/* sending a "UnplugRemote" could help */
669b411b363SPhilipp Reisner 	MD_DIRTY,		/* current uuids and flags not yet on disk */
670b411b363SPhilipp Reisner 	USE_DEGR_WFC_T,		/* degr-wfc-timeout instead of wfc-timeout. */
671b411b363SPhilipp Reisner 	CL_ST_CHG_SUCCESS,
672b411b363SPhilipp Reisner 	CL_ST_CHG_FAIL,
673b411b363SPhilipp Reisner 	CRASHED_PRIMARY,	/* This node was a crashed primary.
674b411b363SPhilipp Reisner 				 * Gets cleared when the state.conn
675b411b363SPhilipp Reisner 				 * goes into C_CONNECTED state. */
676b411b363SPhilipp Reisner 	CONSIDER_RESYNC,
677b411b363SPhilipp Reisner 
678a8a4e51eSPhilipp Reisner 	MD_NO_FUA,		/* Users wants us to not use FUA/FLUSH on meta data dev */
679b411b363SPhilipp Reisner 	SUSPEND_IO,		/* suspend application io */
680b411b363SPhilipp Reisner 	BITMAP_IO,		/* suspend application io;
681b411b363SPhilipp Reisner 				   once no more io in flight, start bitmap io */
682b411b363SPhilipp Reisner 	BITMAP_IO_QUEUED,       /* Started bitmap IO */
68382f59cc6SLars Ellenberg 	GO_DISKLESS,		/* Disk is being detached, on io-error or admin request. */
684a2a3c74fSLars Ellenberg 	WAS_IO_ERROR,		/* Local disk failed, returned IO error */
685a2a3c74fSLars Ellenberg 	WAS_READ_ERROR,		/* Local disk READ failed (set additionally to the above) */
686383606e0SLars Ellenberg 	FORCE_DETACH,		/* Force-detach from local disk, aborting any pending local IO */
687b411b363SPhilipp Reisner 	RESYNC_AFTER_NEG,       /* Resync after online grow after the attach&negotiate finished. */
688b411b363SPhilipp Reisner 	RESIZE_PENDING,		/* Size change detected locally, waiting for the response from
689b411b363SPhilipp Reisner 				 * the peer, if it changed there as well. */
69043a5182cSPhilipp Reisner 	NEW_CUR_UUID,		/* Create new current UUID when thawing IO */
6910778286aSPhilipp Reisner 	AL_SUSPENDED,		/* Activity logging is currently suspended. */
692370a43e7SPhilipp Reisner 	AHEAD_TO_SYNC_SOURCE,   /* Ahead -> SyncSource queued */
693e64a3294SPhilipp Reisner 	B_RS_H_DONE,		/* Before resync handler done (already executed) */
69408b165baSPhilipp Reisner 	DISCARD_MY_DATA,	/* discard_my_data flag per volume */
695380207d0SPhilipp Reisner 	READ_BALANCE_RR,
696b411b363SPhilipp Reisner };
697b411b363SPhilipp Reisner 
698b411b363SPhilipp Reisner struct drbd_bitmap; /* opaque for drbd_conf */
699b411b363SPhilipp Reisner 
70020ceb2b2SLars Ellenberg /* definition of bits in bm_flags to be used in drbd_bm_lock
70120ceb2b2SLars Ellenberg  * and drbd_bitmap_io and friends. */
70220ceb2b2SLars Ellenberg enum bm_flag {
70320ceb2b2SLars Ellenberg 	/* do we need to kfree, or vfree bm_pages? */
70420ceb2b2SLars Ellenberg 	BM_P_VMALLOCED = 0x10000, /* internal use only, will be masked out */
70520ceb2b2SLars Ellenberg 
70620ceb2b2SLars Ellenberg 	/* currently locked for bulk operation */
7070e8488adSLars Ellenberg 	BM_LOCKED_MASK = 0xf,
70820ceb2b2SLars Ellenberg 
70920ceb2b2SLars Ellenberg 	/* in detail, that is: */
71020ceb2b2SLars Ellenberg 	BM_DONT_CLEAR = 0x1,
71120ceb2b2SLars Ellenberg 	BM_DONT_SET   = 0x2,
71220ceb2b2SLars Ellenberg 	BM_DONT_TEST  = 0x4,
71320ceb2b2SLars Ellenberg 
7140e8488adSLars Ellenberg 	/* so we can mark it locked for bulk operation,
7150e8488adSLars Ellenberg 	 * and still allow all non-bulk operations */
7160e8488adSLars Ellenberg 	BM_IS_LOCKED  = 0x8,
7170e8488adSLars Ellenberg 
71820ceb2b2SLars Ellenberg 	/* (test bit, count bit) allowed (common case) */
7190e8488adSLars Ellenberg 	BM_LOCKED_TEST_ALLOWED = BM_DONT_CLEAR | BM_DONT_SET | BM_IS_LOCKED,
72020ceb2b2SLars Ellenberg 
72120ceb2b2SLars Ellenberg 	/* testing bits, as well as setting new bits allowed, but clearing bits
72220ceb2b2SLars Ellenberg 	 * would be unexpected.  Used during bitmap receive.  Setting new bits
72320ceb2b2SLars Ellenberg 	 * requires sending of "out-of-sync" information, though. */
7240e8488adSLars Ellenberg 	BM_LOCKED_SET_ALLOWED = BM_DONT_CLEAR | BM_IS_LOCKED,
72520ceb2b2SLars Ellenberg 
7260e8488adSLars Ellenberg 	/* for drbd_bm_write_copy_pages, everything is allowed,
7270e8488adSLars Ellenberg 	 * only concurrent bulk operations are locked out. */
7280e8488adSLars Ellenberg 	BM_LOCKED_CHANGE_ALLOWED = BM_IS_LOCKED,
72920ceb2b2SLars Ellenberg };
73020ceb2b2SLars Ellenberg 
731b411b363SPhilipp Reisner struct drbd_work_queue {
732b411b363SPhilipp Reisner 	struct list_head q;
733b411b363SPhilipp Reisner 	spinlock_t q_lock;  /* to protect the list. */
7348c0785a5SLars Ellenberg 	wait_queue_head_t q_wait;
735b411b363SPhilipp Reisner };
736b411b363SPhilipp Reisner 
737b411b363SPhilipp Reisner struct drbd_socket {
738b411b363SPhilipp Reisner 	struct mutex mutex;
739b411b363SPhilipp Reisner 	struct socket    *socket;
740b411b363SPhilipp Reisner 	/* this way we get our
741b411b363SPhilipp Reisner 	 * send/receive buffers off the stack */
7425a87d920SAndreas Gruenbacher 	void *sbuf;
743e6ef8a5cSAndreas Gruenbacher 	void *rbuf;
744b411b363SPhilipp Reisner };
745b411b363SPhilipp Reisner 
746b411b363SPhilipp Reisner struct drbd_md {
747b411b363SPhilipp Reisner 	u64 md_offset;		/* sector offset to 'super' block */
748b411b363SPhilipp Reisner 
749b411b363SPhilipp Reisner 	u64 la_size_sect;	/* last agreed size, unit sectors */
7509f2247bbSPhilipp Reisner 	spinlock_t uuid_lock;
751b411b363SPhilipp Reisner 	u64 uuid[UI_SIZE];
752b411b363SPhilipp Reisner 	u64 device_uuid;
753b411b363SPhilipp Reisner 	u32 flags;
754b411b363SPhilipp Reisner 	u32 md_size_sect;
755b411b363SPhilipp Reisner 
756ae8bf312SLars Ellenberg 	s32 al_offset;	/* signed relative sector offset to activity log */
757b411b363SPhilipp Reisner 	s32 bm_offset;	/* signed relative sector offset to bitmap */
7583a4d4eb3SLars Ellenberg 
7593a4d4eb3SLars Ellenberg 	/* cached value of bdev->disk_conf->meta_dev_idx (see below) */
7603a4d4eb3SLars Ellenberg 	s32 meta_dev_idx;
7613a4d4eb3SLars Ellenberg 
7623a4d4eb3SLars Ellenberg 	/* see al_tr_number_to_on_disk_sector() */
7633a4d4eb3SLars Ellenberg 	u32 al_stripes;
7643a4d4eb3SLars Ellenberg 	u32 al_stripe_size_4k;
7653a4d4eb3SLars Ellenberg 	u32 al_size_4k; /* cached product of the above */
766b411b363SPhilipp Reisner };
767b411b363SPhilipp Reisner 
768b411b363SPhilipp Reisner struct drbd_backing_dev {
769b411b363SPhilipp Reisner 	struct block_device *backing_bdev;
770b411b363SPhilipp Reisner 	struct block_device *md_bdev;
771b411b363SPhilipp Reisner 	struct drbd_md md;
772daeda1ccSPhilipp Reisner 	struct disk_conf *disk_conf; /* RCU, for updates: mdev->tconn->conf_update */
773b411b363SPhilipp Reisner 	sector_t known_size; /* last known size of that backing device */
774b411b363SPhilipp Reisner };
775b411b363SPhilipp Reisner 
776b411b363SPhilipp Reisner struct drbd_md_io {
7770c464425SPhilipp Reisner 	unsigned int done;
778b411b363SPhilipp Reisner 	int error;
779b411b363SPhilipp Reisner };
780b411b363SPhilipp Reisner 
781b411b363SPhilipp Reisner struct bm_io_work {
782b411b363SPhilipp Reisner 	struct drbd_work w;
783b411b363SPhilipp Reisner 	char *why;
78420ceb2b2SLars Ellenberg 	enum bm_flag flags;
785b411b363SPhilipp Reisner 	int (*io_fn)(struct drbd_conf *mdev);
786b411b363SPhilipp Reisner 	void (*done)(struct drbd_conf *mdev, int rv);
787b411b363SPhilipp Reisner };
788b411b363SPhilipp Reisner 
789b411b363SPhilipp Reisner enum write_ordering_e {
790b411b363SPhilipp Reisner 	WO_none,
791b411b363SPhilipp Reisner 	WO_drain_io,
792b411b363SPhilipp Reisner 	WO_bdev_flush,
793b411b363SPhilipp Reisner };
794b411b363SPhilipp Reisner 
795778f271dSPhilipp Reisner struct fifo_buffer {
796778f271dSPhilipp Reisner 	unsigned int head_index;
797778f271dSPhilipp Reisner 	unsigned int size;
7989958c857SPhilipp Reisner 	int total; /* sum of all values */
7999958c857SPhilipp Reisner 	int values[0];
800778f271dSPhilipp Reisner };
8019958c857SPhilipp Reisner extern struct fifo_buffer *fifo_alloc(int fifo_size);
802778f271dSPhilipp Reisner 
80301a311a5SPhilipp Reisner /* flag bits per tconn */
80401a311a5SPhilipp Reisner enum {
80501a311a5SPhilipp Reisner 	NET_CONGESTED,		/* The data socket is congested */
806427c0434SLars Ellenberg 	RESOLVE_CONFLICTS,	/* Set on one node, cleared on the peer! */
807e43ef195SPhilipp Reisner 	SEND_PING,		/* whether asender should send a ping asap */
808808e37b8SPhilipp Reisner 	SIGNAL_ASENDER,		/* whether asender wants to be interrupted */
8092a67d8b9SPhilipp Reisner 	GOT_PING_ACK,		/* set when we receive a ping_ack packet, ping_wait gets woken */
8104d0fc3fdSPhilipp Reisner 	CONN_WD_ST_CHG_REQ,	/* A cluster wide state change on the connection is active */
811fc3b10a4SPhilipp Reisner 	CONN_WD_ST_CHG_OKAY,
812fc3b10a4SPhilipp Reisner 	CONN_WD_ST_CHG_FAIL,
8138169e41bSPhilipp Reisner 	CONN_DRY_RUN,		/* Expect disconnect after resync handshake. */
8146936fcb4SPhilipp Reisner 	CREATE_BARRIER,		/* next P_DATA is preceded by a P_BARRIER */
815a1096a6eSPhilipp Reisner 	STATE_SENT,		/* Do not change state/UUIDs while this is set */
8166f3465edSLars Ellenberg 	CALLBACK_PENDING,	/* Whether we have a call_usermodehelper(, UMH_WAIT_PROC)
8176f3465edSLars Ellenberg 				 * pending, from drbd worker context.
8186f3465edSLars Ellenberg 				 * If set, bdi_write_congested() returns true,
8196f3465edSLars Ellenberg 				 * so shrink_page_list() would not recurse into,
8206f3465edSLars Ellenberg 				 * and potentially deadlock on, this drbd worker.
8216f3465edSLars Ellenberg 				 */
822b66623e3SPhilipp Reisner 	DISCONNECT_SENT,
82301a311a5SPhilipp Reisner };
82401a311a5SPhilipp Reisner 
8252111438bSPhilipp Reisner struct drbd_tconn {			/* is a resource from the config file */
8262111438bSPhilipp Reisner 	char *name;			/* Resource name */
827543cc10bSLars Ellenberg 	struct list_head all_tconn;	/* linked on global drbd_tconns */
8289dc9fbb3SPhilipp Reisner 	struct kref kref;
829062e879cSPhilipp Reisner 	struct idr volumes;		/* <tconn, vnr> to mdev mapping */
830bbeb641cSPhilipp Reisner 	enum drbd_conns cstate;		/* Only C_STANDALONE to C_WF_REPORT_PARAMS */
8318e0af25fSPhilipp Reisner 	unsigned susp:1;		/* IO suspended by user */
8328e0af25fSPhilipp Reisner 	unsigned susp_nod:1;		/* IO suspended because no data */
8338e0af25fSPhilipp Reisner 	unsigned susp_fen:1;		/* IO suspended because fence peer handler runs */
8348410da8fSPhilipp Reisner 	struct mutex cstate_mutex;	/* Protects graceful disconnects */
83528e448bbSPhilipp Reisner 	unsigned int connect_cnt;	/* Inc each time a connection is established */
8362111438bSPhilipp Reisner 
837062e879cSPhilipp Reisner 	unsigned long flags;
83844ed167dSPhilipp Reisner 	struct net_conf *net_conf;	/* content protected by rcu */
839a0095508SPhilipp Reisner 	struct mutex conf_update;	/* mutex for ready-copy-update of net_conf and disk_conf */
8402a67d8b9SPhilipp Reisner 	wait_queue_head_t ping_wait;	/* Woken upon reception of a ping, and a state change */
841f399002eSLars Ellenberg 	struct res_opts res_opts;
842e42325a5SPhilipp Reisner 
843089c075dSAndreas Gruenbacher 	struct sockaddr_storage my_addr;
844089c075dSAndreas Gruenbacher 	int my_addr_len;
845089c075dSAndreas Gruenbacher 	struct sockaddr_storage peer_addr;
846089c075dSAndreas Gruenbacher 	int peer_addr_len;
847089c075dSAndreas Gruenbacher 
848e42325a5SPhilipp Reisner 	struct drbd_socket data;	/* data/barrier/cstate/parameter packets */
849e42325a5SPhilipp Reisner 	struct drbd_socket meta;	/* ping/ack (metadata) packets */
85031890f4aSPhilipp Reisner 	int agreed_pro_version;		/* actually used protocol version */
85131890f4aSPhilipp Reisner 	unsigned long last_received;	/* in jiffies, either socket */
85231890f4aSPhilipp Reisner 	unsigned int ko_count;
853e6b3ea83SPhilipp Reisner 
85487eeee41SPhilipp Reisner 	spinlock_t req_lock;
855b6dd1a89SLars Ellenberg 
856b6dd1a89SLars Ellenberg 	struct list_head transfer_log;	/* all requests not yet fully processed */
85787eeee41SPhilipp Reisner 
858a0638456SPhilipp Reisner 	struct crypto_hash *cram_hmac_tfm;
85946e1ce41SPhilipp Reisner 	struct crypto_hash *integrity_tfm;  /* checksums we compute, updates protected by tconn->data->mutex */
860036b17eaSPhilipp Reisner 	struct crypto_hash *peer_integrity_tfm;  /* checksums we verify, only accessed from receiver thread  */
861f399002eSLars Ellenberg 	struct crypto_hash *csums_tfm;
862f399002eSLars Ellenberg 	struct crypto_hash *verify_tfm;
863a0638456SPhilipp Reisner 	void *int_dig_in;
864a0638456SPhilipp Reisner 	void *int_dig_vv;
865a0638456SPhilipp Reisner 
866b6dd1a89SLars Ellenberg 	/* receiver side */
86712038a3aSPhilipp Reisner 	struct drbd_epoch *current_epoch;
86812038a3aSPhilipp Reisner 	spinlock_t epoch_lock;
86912038a3aSPhilipp Reisner 	unsigned int epochs;
8704b0007c0SPhilipp Reisner 	enum write_ordering_e write_ordering;
871b379c41eSLars Ellenberg 	atomic_t current_tle_nr;	/* transfer log epoch number */
872b6dd1a89SLars Ellenberg 	unsigned current_tle_writes;	/* writes seen within this tl epoch */
8734b0007c0SPhilipp Reisner 
87407be15b1SLars Ellenberg 	unsigned long last_reconnect_jif;
875e6b3ea83SPhilipp Reisner 	struct drbd_thread receiver;
876e6b3ea83SPhilipp Reisner 	struct drbd_thread worker;
877e6b3ea83SPhilipp Reisner 	struct drbd_thread asender;
87880822284SPhilipp Reisner 	cpumask_var_t cpu_mask;
879b6dd1a89SLars Ellenberg 
880b6dd1a89SLars Ellenberg 	/* sender side */
881d5b27b01SLars Ellenberg 	struct drbd_work_queue sender_work;
882b6dd1a89SLars Ellenberg 
883b6dd1a89SLars Ellenberg 	struct {
884b6dd1a89SLars Ellenberg 		/* whether this sender thread
885b6dd1a89SLars Ellenberg 		 * has processed a single write yet. */
886b6dd1a89SLars Ellenberg 		bool seen_any_write_yet;
887b6dd1a89SLars Ellenberg 
888b6dd1a89SLars Ellenberg 		/* Which barrier number to send with the next P_BARRIER */
889b6dd1a89SLars Ellenberg 		int current_epoch_nr;
890b6dd1a89SLars Ellenberg 
891b6dd1a89SLars Ellenberg 		/* how many write requests have been sent
892b6dd1a89SLars Ellenberg 		 * with req->epoch == current_epoch_nr.
893b6dd1a89SLars Ellenberg 		 * If none, no P_BARRIER will be sent. */
894b6dd1a89SLars Ellenberg 		unsigned current_epoch_writes;
895b6dd1a89SLars Ellenberg 	} send;
896b411b363SPhilipp Reisner };
897b411b363SPhilipp Reisner 
898113fef9eSLars Ellenberg struct submit_worker {
899113fef9eSLars Ellenberg 	struct workqueue_struct *wq;
900113fef9eSLars Ellenberg 	struct work_struct worker;
901113fef9eSLars Ellenberg 
902113fef9eSLars Ellenberg 	spinlock_t lock;
903113fef9eSLars Ellenberg 	struct list_head writes;
904113fef9eSLars Ellenberg };
905113fef9eSLars Ellenberg 
906b411b363SPhilipp Reisner struct drbd_conf {
9072111438bSPhilipp Reisner 	struct drbd_tconn *tconn;
9082111438bSPhilipp Reisner 	int vnr;			/* volume number within the connection */
90981fa2e67SPhilipp Reisner 	struct kref kref;
9102111438bSPhilipp Reisner 
911b411b363SPhilipp Reisner 	/* things that are stored as / read from meta data on disk */
912b411b363SPhilipp Reisner 	unsigned long flags;
913b411b363SPhilipp Reisner 
914b411b363SPhilipp Reisner 	/* configured by drbdsetup */
915b411b363SPhilipp Reisner 	struct drbd_backing_dev *ldev __protected_by(local);
916b411b363SPhilipp Reisner 
917b411b363SPhilipp Reisner 	sector_t p_size;     /* partner's disk size */
918b411b363SPhilipp Reisner 	struct request_queue *rq_queue;
919b411b363SPhilipp Reisner 	struct block_device *this_bdev;
920b411b363SPhilipp Reisner 	struct gendisk	    *vdisk;
921b411b363SPhilipp Reisner 
92207be15b1SLars Ellenberg 	unsigned long last_reattach_jif;
923b411b363SPhilipp Reisner 	struct drbd_work  resync_work,
924b411b363SPhilipp Reisner 			  unplug_work,
925e9e6f3ecSLars Ellenberg 			  go_diskless,
926c4752ef1SPhilipp Reisner 			  md_sync_work,
927c4752ef1SPhilipp Reisner 			  start_resync_work;
928b411b363SPhilipp Reisner 	struct timer_list resync_timer;
929b411b363SPhilipp Reisner 	struct timer_list md_sync_timer;
930370a43e7SPhilipp Reisner 	struct timer_list start_resync_timer;
9317fde2be9SPhilipp Reisner 	struct timer_list request_timer;
932ee15b038SLars Ellenberg #ifdef DRBD_DEBUG_MD_SYNC
933ee15b038SLars Ellenberg 	struct {
934ee15b038SLars Ellenberg 		unsigned int line;
935ee15b038SLars Ellenberg 		const char* func;
936ee15b038SLars Ellenberg 	} last_md_mark_dirty;
937ee15b038SLars Ellenberg #endif
938b411b363SPhilipp Reisner 
939b411b363SPhilipp Reisner 	/* Used after attach while negotiating new disk state. */
940b411b363SPhilipp Reisner 	union drbd_state new_state_tmp;
941b411b363SPhilipp Reisner 
942da9fbc27SPhilipp Reisner 	union drbd_dev_state state;
943b411b363SPhilipp Reisner 	wait_queue_head_t misc_wait;
944b411b363SPhilipp Reisner 	wait_queue_head_t state_wait;  /* upon each state change. */
945b411b363SPhilipp Reisner 	unsigned int send_cnt;
946b411b363SPhilipp Reisner 	unsigned int recv_cnt;
947b411b363SPhilipp Reisner 	unsigned int read_cnt;
948b411b363SPhilipp Reisner 	unsigned int writ_cnt;
949b411b363SPhilipp Reisner 	unsigned int al_writ_cnt;
950b411b363SPhilipp Reisner 	unsigned int bm_writ_cnt;
951b411b363SPhilipp Reisner 	atomic_t ap_bio_cnt;	 /* Requests we need to complete */
952b411b363SPhilipp Reisner 	atomic_t ap_pending_cnt; /* AP data packets on the wire, ack expected */
953b411b363SPhilipp Reisner 	atomic_t rs_pending_cnt; /* RS request/data packets on the wire */
954d942ae44SPhilipp Reisner 	atomic_t unacked_cnt;	 /* Need to send replies for */
955b411b363SPhilipp Reisner 	atomic_t local_cnt;	 /* Waiting for local completion */
956b2fb6dbeSPhilipp Reisner 
957dac1389cSAndreas Gruenbacher 	/* Interval tree of pending local requests */
958dac1389cSAndreas Gruenbacher 	struct rb_root read_requests;
959de696716SAndreas Gruenbacher 	struct rb_root write_requests;
960b411b363SPhilipp Reisner 
9614b0715f0SLars Ellenberg 	/* blocks to resync in this run [unit BM_BLOCK_SIZE] */
962b411b363SPhilipp Reisner 	unsigned long rs_total;
9634b0715f0SLars Ellenberg 	/* number of resync blocks that failed in this run */
964b411b363SPhilipp Reisner 	unsigned long rs_failed;
965b411b363SPhilipp Reisner 	/* Syncer's start time [unit jiffies] */
966b411b363SPhilipp Reisner 	unsigned long rs_start;
967b411b363SPhilipp Reisner 	/* cumulated time in PausedSyncX state [unit jiffies] */
968b411b363SPhilipp Reisner 	unsigned long rs_paused;
9691d7734a0SLars Ellenberg 	/* skipped because csum was equal [unit BM_BLOCK_SIZE] */
970b411b363SPhilipp Reisner 	unsigned long rs_same_csum;
9711d7734a0SLars Ellenberg #define DRBD_SYNC_MARKS 8
9721d7734a0SLars Ellenberg #define DRBD_SYNC_MARK_STEP (3*HZ)
9731d7734a0SLars Ellenberg 	/* block not up-to-date at mark [unit BM_BLOCK_SIZE] */
9741d7734a0SLars Ellenberg 	unsigned long rs_mark_left[DRBD_SYNC_MARKS];
9751d7734a0SLars Ellenberg 	/* marks's time [unit jiffies] */
9761d7734a0SLars Ellenberg 	unsigned long rs_mark_time[DRBD_SYNC_MARKS];
9771d7734a0SLars Ellenberg 	/* current index into rs_mark_{left,time} */
9781d7734a0SLars Ellenberg 	int rs_last_mark;
979328e0f12SPhilipp Reisner 	unsigned long rs_last_bcast; /* [unit jiffies] */
980b411b363SPhilipp Reisner 
981b411b363SPhilipp Reisner 	/* where does the admin want us to start? (sector) */
982b411b363SPhilipp Reisner 	sector_t ov_start_sector;
98302b91b55SLars Ellenberg 	sector_t ov_stop_sector;
984b411b363SPhilipp Reisner 	/* where are we now? (sector) */
985b411b363SPhilipp Reisner 	sector_t ov_position;
986b411b363SPhilipp Reisner 	/* Start sector of out of sync range (to merge printk reporting). */
987b411b363SPhilipp Reisner 	sector_t ov_last_oos_start;
988b411b363SPhilipp Reisner 	/* size of out-of-sync range in sectors. */
989b411b363SPhilipp Reisner 	sector_t ov_last_oos_size;
990b411b363SPhilipp Reisner 	unsigned long ov_left; /* in bits */
991b411b363SPhilipp Reisner 
992b411b363SPhilipp Reisner 	struct drbd_bitmap *bitmap;
993b411b363SPhilipp Reisner 	unsigned long bm_resync_fo; /* bit offset for drbd_bm_find_next */
994b411b363SPhilipp Reisner 
995b411b363SPhilipp Reisner 	/* Used to track operations of resync... */
996b411b363SPhilipp Reisner 	struct lru_cache *resync;
997b411b363SPhilipp Reisner 	/* Number of locked elements in resync LRU */
998b411b363SPhilipp Reisner 	unsigned int resync_locked;
999b411b363SPhilipp Reisner 	/* resync extent number waiting for application requests */
1000b411b363SPhilipp Reisner 	unsigned int resync_wenr;
1001b411b363SPhilipp Reisner 
1002b411b363SPhilipp Reisner 	int open_cnt;
1003b411b363SPhilipp Reisner 	u64 *p_uuid;
10044b0007c0SPhilipp Reisner 
100585719573SPhilipp Reisner 	struct list_head active_ee; /* IO in progress (P_DATA gets written to disk) */
100685719573SPhilipp Reisner 	struct list_head sync_ee;   /* IO in progress (P_RS_DATA_REPLY gets written to disk) */
100718b75d75SAndreas Gruenbacher 	struct list_head done_ee;   /* need to send P_WRITE_ACK */
100818b75d75SAndreas Gruenbacher 	struct list_head read_ee;   /* [RS]P_DATA_REQUEST being read */
1009b411b363SPhilipp Reisner 	struct list_head net_ee;    /* zero-copy network send in progress */
1010b411b363SPhilipp Reisner 
1011b411b363SPhilipp Reisner 	int next_barrier_nr;
1012b411b363SPhilipp Reisner 	struct list_head resync_reads;
1013435f0740SLars Ellenberg 	atomic_t pp_in_use;		/* allocated from page pool */
1014435f0740SLars Ellenberg 	atomic_t pp_in_use_by_net;	/* sendpage()d, still referenced by tcp */
1015b411b363SPhilipp Reisner 	wait_queue_head_t ee_wait;
1016b411b363SPhilipp Reisner 	struct page *md_io_page;	/* one page buffer for md_io */
1017cc94c650SPhilipp Reisner 	struct drbd_md_io md_io;
1018e1711731SPhilipp Reisner 	atomic_t md_io_in_use;		/* protects the md_io, md_io_page and md_io_tmpp */
1019b411b363SPhilipp Reisner 	spinlock_t al_lock;
1020b411b363SPhilipp Reisner 	wait_queue_head_t al_wait;
1021b411b363SPhilipp Reisner 	struct lru_cache *act_log;	/* activity log */
1022b411b363SPhilipp Reisner 	unsigned int al_tr_number;
1023b411b363SPhilipp Reisner 	int al_tr_cycle;
1024b411b363SPhilipp Reisner 	wait_queue_head_t seq_wait;
1025b411b363SPhilipp Reisner 	atomic_t packet_seq;
1026b411b363SPhilipp Reisner 	unsigned int peer_seq;
1027b411b363SPhilipp Reisner 	spinlock_t peer_seq_lock;
1028b411b363SPhilipp Reisner 	unsigned int minor;
1029b411b363SPhilipp Reisner 	unsigned long comm_bm_set; /* communicated number of set bits. */
1030b411b363SPhilipp Reisner 	struct bm_io_work bm_io_work;
1031b411b363SPhilipp Reisner 	u64 ed_uuid; /* UUID of the exposed data */
10328410da8fSPhilipp Reisner 	struct mutex own_state_mutex;
10338410da8fSPhilipp Reisner 	struct mutex *state_mutex; /* either own_state_mutex or mdev->tconn->cstate_mutex */
1034b411b363SPhilipp Reisner 	char congestion_reason;  /* Why we where congested... */
10351d7734a0SLars Ellenberg 	atomic_t rs_sect_in; /* for incoming resync data rate, SyncTarget */
10361d7734a0SLars Ellenberg 	atomic_t rs_sect_ev; /* for submitted resync data rate, both */
10371d7734a0SLars Ellenberg 	int rs_last_sect_ev; /* counter to compare with */
10381d7734a0SLars Ellenberg 	int rs_last_events;  /* counter of read or write "events" (unit sectors)
10391d7734a0SLars Ellenberg 			      * on the lower level device when we last looked. */
10401d7734a0SLars Ellenberg 	int c_sync_rate; /* current resync rate after syncer throttle magic */
1041813472ceSPhilipp Reisner 	struct fifo_buffer *rs_plan_s; /* correction values of resync planer (RCU, tconn->conn_update) */
1042778f271dSPhilipp Reisner 	int rs_in_flight; /* resync sectors in flight (to proxy, in proxy and from proxy) */
1043759fbdfbSPhilipp Reisner 	atomic_t ap_in_flight; /* App sectors in flight (waiting for ack) */
1044db141b2fSLars Ellenberg 	unsigned int peer_max_bio_size;
1045db141b2fSLars Ellenberg 	unsigned int local_max_bio_size;
1046113fef9eSLars Ellenberg 
1047113fef9eSLars Ellenberg 	/* any requests that would block in drbd_make_request()
1048113fef9eSLars Ellenberg 	 * are deferred to this single-threaded work queue */
1049113fef9eSLars Ellenberg 	struct submit_worker submit;
1050b411b363SPhilipp Reisner };
1051b411b363SPhilipp Reisner 
1052b411b363SPhilipp Reisner static inline struct drbd_conf *minor_to_mdev(unsigned int minor)
1053b411b363SPhilipp Reisner {
105481a5d60eSPhilipp Reisner 	return (struct drbd_conf *)idr_find(&minors, minor);
1055b411b363SPhilipp Reisner }
1056b411b363SPhilipp Reisner 
1057b411b363SPhilipp Reisner static inline unsigned int mdev_to_minor(struct drbd_conf *mdev)
1058b411b363SPhilipp Reisner {
1059b411b363SPhilipp Reisner 	return mdev->minor;
1060b411b363SPhilipp Reisner }
1061b411b363SPhilipp Reisner 
1062eefc2f7dSPhilipp Reisner static inline struct drbd_conf *vnr_to_mdev(struct drbd_tconn *tconn, int vnr)
1063b411b363SPhilipp Reisner {
1064eefc2f7dSPhilipp Reisner 	return (struct drbd_conf *)idr_find(&tconn->volumes, vnr);
1065b411b363SPhilipp Reisner }
1066b411b363SPhilipp Reisner 
1067b411b363SPhilipp Reisner /*
1068b411b363SPhilipp Reisner  * function declarations
1069b411b363SPhilipp Reisner  *************************/
1070b411b363SPhilipp Reisner 
1071b411b363SPhilipp Reisner /* drbd_main.c */
1072b411b363SPhilipp Reisner 
1073e89b591cSPhilipp Reisner enum dds_flags {
1074e89b591cSPhilipp Reisner 	DDSF_FORCED    = 1,
1075e89b591cSPhilipp Reisner 	DDSF_NO_RESYNC = 2, /* Do not run a resync for the new space */
1076e89b591cSPhilipp Reisner };
1077e89b591cSPhilipp Reisner 
1078b411b363SPhilipp Reisner extern void drbd_init_set_defaults(struct drbd_conf *mdev);
1079b411b363SPhilipp Reisner extern int  drbd_thread_start(struct drbd_thread *thi);
1080b411b363SPhilipp Reisner extern void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait);
1081392c8801SPhilipp Reisner extern char *drbd_task_to_thread_name(struct drbd_tconn *tconn, struct task_struct *task);
1082b411b363SPhilipp Reisner #ifdef CONFIG_SMP
108380822284SPhilipp Reisner extern void drbd_thread_current_set_cpu(struct drbd_thread *thi);
108480822284SPhilipp Reisner extern void drbd_calc_cpu_mask(struct drbd_tconn *tconn);
1085b411b363SPhilipp Reisner #else
1086b411b363SPhilipp Reisner #define drbd_thread_current_set_cpu(A) ({})
1087b411b363SPhilipp Reisner #define drbd_calc_cpu_mask(A) ({})
1088b411b363SPhilipp Reisner #endif
10892f5cdd0bSPhilipp Reisner extern void tl_release(struct drbd_tconn *, unsigned int barrier_nr,
1090b411b363SPhilipp Reisner 		       unsigned int set_size);
10912f5cdd0bSPhilipp Reisner extern void tl_clear(struct drbd_tconn *);
1092360cc740SPhilipp Reisner extern void drbd_free_sock(struct drbd_tconn *tconn);
1093bedbd2a5SPhilipp Reisner extern int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
1094b411b363SPhilipp Reisner 		     void *buf, size_t size, unsigned msg_flags);
1095fb708e40SAndreas Gruenbacher extern int drbd_send_all(struct drbd_tconn *, struct socket *, void *, size_t,
1096fb708e40SAndreas Gruenbacher 			 unsigned);
1097fb708e40SAndreas Gruenbacher 
1098d659f2aaSPhilipp Reisner extern int __drbd_send_protocol(struct drbd_tconn *tconn, enum drbd_packet cmd);
1099dc8228d1SPhilipp Reisner extern int drbd_send_protocol(struct drbd_tconn *tconn);
1100b411b363SPhilipp Reisner extern int drbd_send_uuids(struct drbd_conf *mdev);
1101b411b363SPhilipp Reisner extern int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev);
11029c1b7f72SAndreas Gruenbacher extern void drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev);
1103e89b591cSPhilipp Reisner extern int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags);
1104f479ea06SLars Ellenberg extern int drbd_send_state(struct drbd_conf *mdev, union drbd_state s);
1105f479ea06SLars Ellenberg extern int drbd_send_current_state(struct drbd_conf *mdev);
1106f399002eSLars Ellenberg extern int drbd_send_sync_param(struct drbd_conf *mdev);
11079ed57dcbSLars Ellenberg extern void drbd_send_b_ack(struct drbd_tconn *tconn, u32 barrier_nr,
1108b411b363SPhilipp Reisner 			    u32 set_size);
1109f6ffca9fSAndreas Gruenbacher extern int drbd_send_ack(struct drbd_conf *, enum drbd_packet,
1110f6ffca9fSAndreas Gruenbacher 			 struct drbd_peer_request *);
1111a9a9994dSAndreas Gruenbacher extern void drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
1112b411b363SPhilipp Reisner 			     struct p_block_req *rp);
1113a9a9994dSAndreas Gruenbacher extern void drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
11142b2bf214SLars Ellenberg 			     struct p_data *dp, int data_size);
1115d8763023SAndreas Gruenbacher extern int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
1116b411b363SPhilipp Reisner 			    sector_t sector, int blksize, u64 block_id);
11178f7bed77SAndreas Gruenbacher extern int drbd_send_out_of_sync(struct drbd_conf *, struct drbd_request *);
1118f6ffca9fSAndreas Gruenbacher extern int drbd_send_block(struct drbd_conf *, enum drbd_packet,
1119f6ffca9fSAndreas Gruenbacher 			   struct drbd_peer_request *);
1120b411b363SPhilipp Reisner extern int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req);
1121b411b363SPhilipp Reisner extern int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1122b411b363SPhilipp Reisner 			      sector_t sector, int size, u64 block_id);
1123d8763023SAndreas Gruenbacher extern int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector,
1124d8763023SAndreas Gruenbacher 				   int size, void *digest, int digest_size,
1125d8763023SAndreas Gruenbacher 				   enum drbd_packet cmd);
1126b411b363SPhilipp Reisner extern int drbd_send_ov_request(struct drbd_conf *mdev,sector_t sector,int size);
1127b411b363SPhilipp Reisner 
1128b411b363SPhilipp Reisner extern int drbd_send_bitmap(struct drbd_conf *mdev);
11292f4e7abeSAndreas Gruenbacher extern void drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode);
11309f5bdc33SAndreas Gruenbacher extern void conn_send_sr_reply(struct drbd_tconn *tconn, enum drbd_state_rv retcode);
1131b411b363SPhilipp Reisner extern void drbd_free_bc(struct drbd_backing_dev *ldev);
1132b411b363SPhilipp Reisner extern void drbd_mdev_cleanup(struct drbd_conf *mdev);
113362b0da3aSLars Ellenberg void drbd_print_uuids(struct drbd_conf *mdev, const char *text);
1134b411b363SPhilipp Reisner 
113519fffd7bSPhilipp Reisner extern void conn_md_sync(struct drbd_tconn *tconn);
1136*d752b269SPhilipp Reisner extern void drbd_md_write(struct drbd_conf *mdev, void *buffer);
1137b411b363SPhilipp Reisner extern void drbd_md_sync(struct drbd_conf *mdev);
1138b411b363SPhilipp Reisner extern int  drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev);
1139b411b363SPhilipp Reisner extern void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1140b411b363SPhilipp Reisner extern void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1141b411b363SPhilipp Reisner extern void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1142b411b363SPhilipp Reisner extern void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local);
11439f2247bbSPhilipp Reisner extern void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local);
11449f2247bbSPhilipp Reisner extern void __drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1145b411b363SPhilipp Reisner extern void drbd_md_set_flag(struct drbd_conf *mdev, int flags) __must_hold(local);
1146b411b363SPhilipp Reisner extern void drbd_md_clear_flag(struct drbd_conf *mdev, int flags)__must_hold(local);
1147b411b363SPhilipp Reisner extern int drbd_md_test_flag(struct drbd_backing_dev *, int);
1148ee15b038SLars Ellenberg #ifndef DRBD_DEBUG_MD_SYNC
1149b411b363SPhilipp Reisner extern void drbd_md_mark_dirty(struct drbd_conf *mdev);
1150ee15b038SLars Ellenberg #else
1151ee15b038SLars Ellenberg #define drbd_md_mark_dirty(m)	drbd_md_mark_dirty_(m, __LINE__ , __func__ )
1152ee15b038SLars Ellenberg extern void drbd_md_mark_dirty_(struct drbd_conf *mdev,
1153ee15b038SLars Ellenberg 		unsigned int line, const char *func);
1154ee15b038SLars Ellenberg #endif
1155b411b363SPhilipp Reisner extern void drbd_queue_bitmap_io(struct drbd_conf *mdev,
1156b411b363SPhilipp Reisner 				 int (*io_fn)(struct drbd_conf *),
1157b411b363SPhilipp Reisner 				 void (*done)(struct drbd_conf *, int),
115820ceb2b2SLars Ellenberg 				 char *why, enum bm_flag flags);
115920ceb2b2SLars Ellenberg extern int drbd_bitmap_io(struct drbd_conf *mdev,
116020ceb2b2SLars Ellenberg 		int (*io_fn)(struct drbd_conf *),
116120ceb2b2SLars Ellenberg 		char *why, enum bm_flag flags);
1162edc9f5ebSLars Ellenberg extern int drbd_bitmap_io_from_worker(struct drbd_conf *mdev,
1163edc9f5ebSLars Ellenberg 		int (*io_fn)(struct drbd_conf *),
1164edc9f5ebSLars Ellenberg 		char *why, enum bm_flag flags);
1165b411b363SPhilipp Reisner extern int drbd_bmio_set_n_write(struct drbd_conf *mdev);
1166b411b363SPhilipp Reisner extern int drbd_bmio_clear_n_write(struct drbd_conf *mdev);
116782f59cc6SLars Ellenberg extern void drbd_ldev_destroy(struct drbd_conf *mdev);
1168b411b363SPhilipp Reisner 
1169b411b363SPhilipp Reisner /* Meta data layout
1170ae8bf312SLars Ellenberg  *
1171ae8bf312SLars Ellenberg  * We currently have two possible layouts.
1172ae8bf312SLars Ellenberg  * Offsets in (512 byte) sectors.
1173ae8bf312SLars Ellenberg  * external:
1174ae8bf312SLars Ellenberg  *   |----------- md_size_sect ------------------|
1175ae8bf312SLars Ellenberg  *   [ 4k superblock ][ activity log ][  Bitmap  ]
1176ae8bf312SLars Ellenberg  *   | al_offset == 8 |
1177ae8bf312SLars Ellenberg  *   | bm_offset = al_offset + X      |
1178ae8bf312SLars Ellenberg  *  ==> bitmap sectors = md_size_sect - bm_offset
1179ae8bf312SLars Ellenberg  *
1180ae8bf312SLars Ellenberg  *  Variants:
1181ae8bf312SLars Ellenberg  *     old, indexed fixed size meta data:
1182ae8bf312SLars Ellenberg  *
1183ae8bf312SLars Ellenberg  * internal:
1184ae8bf312SLars Ellenberg  *            |----------- md_size_sect ------------------|
1185ae8bf312SLars Ellenberg  * [data.....][  Bitmap  ][ activity log ][ 4k superblock ][padding*]
1186ae8bf312SLars Ellenberg  *                        | al_offset < 0 |
1187ae8bf312SLars Ellenberg  *            | bm_offset = al_offset - Y |
1188ae8bf312SLars Ellenberg  *  ==> bitmap sectors = Y = al_offset - bm_offset
1189ae8bf312SLars Ellenberg  *
1190ae8bf312SLars Ellenberg  *  [padding*] are zero or up to 7 unused 512 Byte sectors to the
1191ae8bf312SLars Ellenberg  *  end of the device, so that the [4k superblock] will be 4k aligned.
1192ae8bf312SLars Ellenberg  *
1193ae8bf312SLars Ellenberg  *  The activity log consists of 4k transaction blocks,
1194ae8bf312SLars Ellenberg  *  which are written in a ring-buffer, or striped ring-buffer like fashion,
1195ae8bf312SLars Ellenberg  *  which are writtensize used to be fixed 32kB,
1196ae8bf312SLars Ellenberg  *  but is about to become configurable.
1197ae8bf312SLars Ellenberg  */
1198b411b363SPhilipp Reisner 
1199ae8bf312SLars Ellenberg /* Our old fixed size meta data layout
1200ae8bf312SLars Ellenberg  * allows up to about 3.8TB, so if you want more,
12017ad651b5SLars Ellenberg  * you need to use the "flexible" meta data format. */
1202ae8bf312SLars Ellenberg #define MD_128MB_SECT (128LLU << 11)  /* 128 MB, unit sectors */
1203ae8bf312SLars Ellenberg #define MD_4kB_SECT	 8
1204ae8bf312SLars Ellenberg #define MD_32kB_SECT	64
1205b411b363SPhilipp Reisner 
12067ad651b5SLars Ellenberg /* One activity log extent represents 4M of storage */
12077ad651b5SLars Ellenberg #define AL_EXTENT_SHIFT 22
1208b411b363SPhilipp Reisner #define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
1209b411b363SPhilipp Reisner 
12107ad651b5SLars Ellenberg /* We could make these currently hardcoded constants configurable
12117ad651b5SLars Ellenberg  * variables at create-md time (or even re-configurable at runtime?).
12127ad651b5SLars Ellenberg  * Which will require some more changes to the DRBD "super block"
12137ad651b5SLars Ellenberg  * and attach code.
12147ad651b5SLars Ellenberg  *
12157ad651b5SLars Ellenberg  * updates per transaction:
12167ad651b5SLars Ellenberg  *   This many changes to the active set can be logged with one transaction.
12177ad651b5SLars Ellenberg  *   This number is arbitrary.
12187ad651b5SLars Ellenberg  * context per transaction:
12197ad651b5SLars Ellenberg  *   This many context extent numbers are logged with each transaction.
12207ad651b5SLars Ellenberg  *   This number is resulting from the transaction block size (4k), the layout
12217ad651b5SLars Ellenberg  *   of the transaction header, and the number of updates per transaction.
12227ad651b5SLars Ellenberg  *   See drbd_actlog.c:struct al_transaction_on_disk
12237ad651b5SLars Ellenberg  * */
12247ad651b5SLars Ellenberg #define AL_UPDATES_PER_TRANSACTION	 64	// arbitrary
12257ad651b5SLars Ellenberg #define AL_CONTEXT_PER_TRANSACTION	919	// (4096 - 36 - 6*64)/4
12267ad651b5SLars Ellenberg 
1227b411b363SPhilipp Reisner #if BITS_PER_LONG == 32
1228b411b363SPhilipp Reisner #define LN2_BPL 5
1229b411b363SPhilipp Reisner #define cpu_to_lel(A) cpu_to_le32(A)
1230b411b363SPhilipp Reisner #define lel_to_cpu(A) le32_to_cpu(A)
1231b411b363SPhilipp Reisner #elif BITS_PER_LONG == 64
1232b411b363SPhilipp Reisner #define LN2_BPL 6
1233b411b363SPhilipp Reisner #define cpu_to_lel(A) cpu_to_le64(A)
1234b411b363SPhilipp Reisner #define lel_to_cpu(A) le64_to_cpu(A)
1235b411b363SPhilipp Reisner #else
1236b411b363SPhilipp Reisner #error "LN2 of BITS_PER_LONG unknown!"
1237b411b363SPhilipp Reisner #endif
1238b411b363SPhilipp Reisner 
1239b411b363SPhilipp Reisner /* resync bitmap */
1240b411b363SPhilipp Reisner /* 16MB sized 'bitmap extent' to track syncer usage */
1241b411b363SPhilipp Reisner struct bm_extent {
1242b411b363SPhilipp Reisner 	int rs_left; /* number of bits set (out of sync) in this extent. */
1243b411b363SPhilipp Reisner 	int rs_failed; /* number of failed resync requests in this extent. */
1244b411b363SPhilipp Reisner 	unsigned long flags;
1245b411b363SPhilipp Reisner 	struct lc_element lce;
1246b411b363SPhilipp Reisner };
1247b411b363SPhilipp Reisner 
1248b411b363SPhilipp Reisner #define BME_NO_WRITES  0  /* bm_extent.flags: no more requests on this one! */
1249b411b363SPhilipp Reisner #define BME_LOCKED     1  /* bm_extent.flags: syncer active on this one. */
1250e3555d85SPhilipp Reisner #define BME_PRIORITY   2  /* finish resync IO on this extent ASAP! App IO waiting! */
1251b411b363SPhilipp Reisner 
1252b411b363SPhilipp Reisner /* drbd_bitmap.c */
1253b411b363SPhilipp Reisner /*
1254b411b363SPhilipp Reisner  * We need to store one bit for a block.
1255b411b363SPhilipp Reisner  * Example: 1GB disk @ 4096 byte blocks ==> we need 32 KB bitmap.
1256b411b363SPhilipp Reisner  * Bit 0 ==> local node thinks this block is binary identical on both nodes
1257b411b363SPhilipp Reisner  * Bit 1 ==> local node thinks this block needs to be synced.
1258b411b363SPhilipp Reisner  */
1259b411b363SPhilipp Reisner 
12608e26f9ccSPhilipp Reisner #define SLEEP_TIME (HZ/10)
12618e26f9ccSPhilipp Reisner 
126245dfffebSLars Ellenberg /* We do bitmap IO in units of 4k blocks.
126345dfffebSLars Ellenberg  * We also still have a hardcoded 4k per bit relation. */
1264b411b363SPhilipp Reisner #define BM_BLOCK_SHIFT	12			 /* 4k per bit */
1265b411b363SPhilipp Reisner #define BM_BLOCK_SIZE	 (1<<BM_BLOCK_SHIFT)
126645dfffebSLars Ellenberg /* mostly arbitrarily set the represented size of one bitmap extent,
126745dfffebSLars Ellenberg  * aka resync extent, to 16 MiB (which is also 512 Byte worth of bitmap
126845dfffebSLars Ellenberg  * at 4k per bit resolution) */
126945dfffebSLars Ellenberg #define BM_EXT_SHIFT	 24	/* 16 MiB per resync extent */
1270b411b363SPhilipp Reisner #define BM_EXT_SIZE	 (1<<BM_EXT_SHIFT)
1271b411b363SPhilipp Reisner 
1272b411b363SPhilipp Reisner #if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
1273b411b363SPhilipp Reisner #error "HAVE YOU FIXED drbdmeta AS WELL??"
1274b411b363SPhilipp Reisner #endif
1275b411b363SPhilipp Reisner 
1276b411b363SPhilipp Reisner /* thus many _storage_ sectors are described by one bit */
1277b411b363SPhilipp Reisner #define BM_SECT_TO_BIT(x)   ((x)>>(BM_BLOCK_SHIFT-9))
1278b411b363SPhilipp Reisner #define BM_BIT_TO_SECT(x)   ((sector_t)(x)<<(BM_BLOCK_SHIFT-9))
1279b411b363SPhilipp Reisner #define BM_SECT_PER_BIT     BM_BIT_TO_SECT(1)
1280b411b363SPhilipp Reisner 
1281b411b363SPhilipp Reisner /* bit to represented kilo byte conversion */
1282b411b363SPhilipp Reisner #define Bit2KB(bits) ((bits)<<(BM_BLOCK_SHIFT-10))
1283b411b363SPhilipp Reisner 
1284b411b363SPhilipp Reisner /* in which _bitmap_ extent (resp. sector) the bit for a certain
1285b411b363SPhilipp Reisner  * _storage_ sector is located in */
1286b411b363SPhilipp Reisner #define BM_SECT_TO_EXT(x)   ((x)>>(BM_EXT_SHIFT-9))
1287b411b363SPhilipp Reisner 
1288b411b363SPhilipp Reisner /* how much _storage_ sectors we have per bitmap sector */
1289b411b363SPhilipp Reisner #define BM_EXT_TO_SECT(x)   ((sector_t)(x) << (BM_EXT_SHIFT-9))
1290b411b363SPhilipp Reisner #define BM_SECT_PER_EXT     BM_EXT_TO_SECT(1)
1291b411b363SPhilipp Reisner 
1292b411b363SPhilipp Reisner /* in one sector of the bitmap, we have this many activity_log extents. */
1293b411b363SPhilipp Reisner #define AL_EXT_PER_BM_SECT  (1 << (BM_EXT_SHIFT - AL_EXTENT_SHIFT))
1294b411b363SPhilipp Reisner 
1295b411b363SPhilipp Reisner #define BM_BLOCKS_PER_BM_EXT_B (BM_EXT_SHIFT - BM_BLOCK_SHIFT)
1296b411b363SPhilipp Reisner #define BM_BLOCKS_PER_BM_EXT_MASK  ((1<<BM_BLOCKS_PER_BM_EXT_B) - 1)
1297b411b363SPhilipp Reisner 
1298b411b363SPhilipp Reisner /* the extent in "PER_EXTENT" below is an activity log extent
1299b411b363SPhilipp Reisner  * we need that many (long words/bytes) to store the bitmap
1300b411b363SPhilipp Reisner  *		     of one AL_EXTENT_SIZE chunk of storage.
1301b411b363SPhilipp Reisner  * we can store the bitmap for that many AL_EXTENTS within
1302b411b363SPhilipp Reisner  * one sector of the _on_disk_ bitmap:
1303b411b363SPhilipp Reisner  * bit	 0	  bit 37   bit 38	     bit (512*8)-1
1304b411b363SPhilipp Reisner  *	     ...|........|........|.. // ..|........|
1305b411b363SPhilipp Reisner  * sect. 0	 `296	  `304			   ^(512*8*8)-1
1306b411b363SPhilipp Reisner  *
1307b411b363SPhilipp Reisner #define BM_WORDS_PER_EXT    ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / BITS_PER_LONG )
1308b411b363SPhilipp Reisner #define BM_BYTES_PER_EXT    ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / 8 )  // 128
1309b411b363SPhilipp Reisner #define BM_EXT_PER_SECT	    ( 512 / BM_BYTES_PER_EXTENT )	 //   4
1310b411b363SPhilipp Reisner  */
1311b411b363SPhilipp Reisner 
1312b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS_32 (0xffffffffLU)
1313ae8bf312SLars Ellenberg /* we have a certain meta data variant that has a fixed on-disk size of 128
1314ae8bf312SLars Ellenberg  * MiB, of which 4k are our "superblock", and 32k are the fixed size activity
1315ae8bf312SLars Ellenberg  * log, leaving this many sectors for the bitmap.
1316ae8bf312SLars Ellenberg  */
1317ae8bf312SLars Ellenberg 
1318ae8bf312SLars Ellenberg #define DRBD_MAX_SECTORS_FIXED_BM \
1319ae8bf312SLars Ellenberg 	  ((MD_128MB_SECT - MD_32kB_SECT - MD_4kB_SECT) * (1LL<<(BM_EXT_SHIFT-9)))
1320ae8bf312SLars Ellenberg #if !defined(CONFIG_LBDAF) && BITS_PER_LONG == 32
1321b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS      DRBD_MAX_SECTORS_32
1322b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_32
1323b411b363SPhilipp Reisner #else
1324ae8bf312SLars Ellenberg #define DRBD_MAX_SECTORS      DRBD_MAX_SECTORS_FIXED_BM
1325b411b363SPhilipp Reisner /* 16 TB in units of sectors */
1326b411b363SPhilipp Reisner #if BITS_PER_LONG == 32
1327b411b363SPhilipp Reisner /* adjust by one page worth of bitmap,
1328b411b363SPhilipp Reisner  * so we won't wrap around in drbd_bm_find_next_bit.
1329b411b363SPhilipp Reisner  * you should use 64bit OS for that much storage, anyways. */
1330b411b363SPhilipp Reisner #define DRBD_MAX_SECTORS_FLEX BM_BIT_TO_SECT(0xffff7fff)
1331b411b363SPhilipp Reisner #else
13324b0715f0SLars Ellenberg /* we allow up to 1 PiB now on 64bit architecture with "flexible" meta data */
13334b0715f0SLars Ellenberg #define DRBD_MAX_SECTORS_FLEX (1UL << 51)
13344b0715f0SLars Ellenberg /* corresponds to (1UL << 38) bits right now. */
1335b411b363SPhilipp Reisner #endif
1336b411b363SPhilipp Reisner #endif
1337b411b363SPhilipp Reisner 
133823361cf3SLars Ellenberg /* BIO_MAX_SIZE is 256 * PAGE_CACHE_SIZE,
133923361cf3SLars Ellenberg  * so for typical PAGE_CACHE_SIZE of 4k, that is (1<<20) Byte.
134023361cf3SLars Ellenberg  * Since we may live in a mixed-platform cluster,
134123361cf3SLars Ellenberg  * we limit us to a platform agnostic constant here for now.
134223361cf3SLars Ellenberg  * A followup commit may allow even bigger BIO sizes,
134323361cf3SLars Ellenberg  * once we thought that through. */
134498683650SPhilipp Reisner #define DRBD_MAX_BIO_SIZE (1U << 20)
134523361cf3SLars Ellenberg #if DRBD_MAX_BIO_SIZE > BIO_MAX_SIZE
134623361cf3SLars Ellenberg #error Architecture not supported: DRBD_MAX_BIO_SIZE > BIO_MAX_SIZE
134723361cf3SLars Ellenberg #endif
1348db141b2fSLars Ellenberg #define DRBD_MAX_BIO_SIZE_SAFE (1U << 12)       /* Works always = 4k */
1349b411b363SPhilipp Reisner 
135098683650SPhilipp Reisner #define DRBD_MAX_SIZE_H80_PACKET (1U << 15) /* Header 80 only allows packets up to 32KiB data */
135198683650SPhilipp Reisner #define DRBD_MAX_BIO_SIZE_P95    (1U << 17) /* Protocol 95 to 99 allows bios up to 128KiB */
1352b411b363SPhilipp Reisner 
1353b411b363SPhilipp Reisner extern int  drbd_bm_init(struct drbd_conf *mdev);
135402d9a94bSPhilipp Reisner extern int  drbd_bm_resize(struct drbd_conf *mdev, sector_t sectors, int set_new_bits);
1355b411b363SPhilipp Reisner extern void drbd_bm_cleanup(struct drbd_conf *mdev);
1356b411b363SPhilipp Reisner extern void drbd_bm_set_all(struct drbd_conf *mdev);
1357b411b363SPhilipp Reisner extern void drbd_bm_clear_all(struct drbd_conf *mdev);
13584b0715f0SLars Ellenberg /* set/clear/test only a few bits at a time */
1359b411b363SPhilipp Reisner extern int  drbd_bm_set_bits(
1360b411b363SPhilipp Reisner 		struct drbd_conf *mdev, unsigned long s, unsigned long e);
1361b411b363SPhilipp Reisner extern int  drbd_bm_clear_bits(
1362b411b363SPhilipp Reisner 		struct drbd_conf *mdev, unsigned long s, unsigned long e);
13634b0715f0SLars Ellenberg extern int drbd_bm_count_bits(
13644b0715f0SLars Ellenberg 	struct drbd_conf *mdev, const unsigned long s, const unsigned long e);
13654b0715f0SLars Ellenberg /* bm_set_bits variant for use while holding drbd_bm_lock,
13664b0715f0SLars Ellenberg  * may process the whole bitmap in one go */
1367b411b363SPhilipp Reisner extern void _drbd_bm_set_bits(struct drbd_conf *mdev,
1368b411b363SPhilipp Reisner 		const unsigned long s, const unsigned long e);
1369b411b363SPhilipp Reisner extern int  drbd_bm_test_bit(struct drbd_conf *mdev, unsigned long bitnr);
1370b411b363SPhilipp Reisner extern int  drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr);
137119f843aaSLars Ellenberg extern int  drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local);
1372b411b363SPhilipp Reisner extern int  drbd_bm_read(struct drbd_conf *mdev) __must_hold(local);
137345dfffebSLars Ellenberg extern void drbd_bm_mark_for_writeout(struct drbd_conf *mdev, int page_nr);
1374b411b363SPhilipp Reisner extern int  drbd_bm_write(struct drbd_conf *mdev) __must_hold(local);
137545dfffebSLars Ellenberg extern int  drbd_bm_write_hinted(struct drbd_conf *mdev) __must_hold(local);
1376d1aa4d04SPhilipp Reisner extern int drbd_bm_write_all(struct drbd_conf *mdev) __must_hold(local);
13770e8488adSLars Ellenberg extern int  drbd_bm_write_copy_pages(struct drbd_conf *mdev) __must_hold(local);
1378b411b363SPhilipp Reisner extern size_t	     drbd_bm_words(struct drbd_conf *mdev);
1379b411b363SPhilipp Reisner extern unsigned long drbd_bm_bits(struct drbd_conf *mdev);
1380b411b363SPhilipp Reisner extern sector_t      drbd_bm_capacity(struct drbd_conf *mdev);
13814b0715f0SLars Ellenberg 
13824b0715f0SLars Ellenberg #define DRBD_END_OF_BITMAP	(~(unsigned long)0)
1383b411b363SPhilipp Reisner extern unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1384b411b363SPhilipp Reisner /* bm_find_next variants for use while you hold drbd_bm_lock() */
1385b411b363SPhilipp Reisner extern unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1386b411b363SPhilipp Reisner extern unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo);
13870778286aSPhilipp Reisner extern unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev);
1388b411b363SPhilipp Reisner extern unsigned long drbd_bm_total_weight(struct drbd_conf *mdev);
1389b411b363SPhilipp Reisner extern int drbd_bm_rs_done(struct drbd_conf *mdev);
1390b411b363SPhilipp Reisner /* for receive_bitmap */
1391b411b363SPhilipp Reisner extern void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset,
1392b411b363SPhilipp Reisner 		size_t number, unsigned long *buffer);
139319f843aaSLars Ellenberg /* for _drbd_send_bitmap */
1394b411b363SPhilipp Reisner extern void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset,
1395b411b363SPhilipp Reisner 		size_t number, unsigned long *buffer);
1396b411b363SPhilipp Reisner 
139720ceb2b2SLars Ellenberg extern void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags);
1398b411b363SPhilipp Reisner extern void drbd_bm_unlock(struct drbd_conf *mdev);
1399b411b363SPhilipp Reisner /* drbd_main.c */
1400b411b363SPhilipp Reisner 
1401b411b363SPhilipp Reisner extern struct kmem_cache *drbd_request_cache;
14026c852becSAndreas Gruenbacher extern struct kmem_cache *drbd_ee_cache;	/* peer requests */
1403b411b363SPhilipp Reisner extern struct kmem_cache *drbd_bm_ext_cache;	/* bitmap extents */
1404b411b363SPhilipp Reisner extern struct kmem_cache *drbd_al_ext_cache;	/* activity log extents */
1405b411b363SPhilipp Reisner extern mempool_t *drbd_request_mempool;
1406b411b363SPhilipp Reisner extern mempool_t *drbd_ee_mempool;
1407b411b363SPhilipp Reisner 
14084281808fSLars Ellenberg /* drbd's page pool, used to buffer data received from the peer,
14094281808fSLars Ellenberg  * or data requested by the peer.
14104281808fSLars Ellenberg  *
14114281808fSLars Ellenberg  * This does not have an emergency reserve.
14124281808fSLars Ellenberg  *
14134281808fSLars Ellenberg  * When allocating from this pool, it first takes pages from the pool.
14144281808fSLars Ellenberg  * Only if the pool is depleted will try to allocate from the system.
14154281808fSLars Ellenberg  *
14164281808fSLars Ellenberg  * The assumption is that pages taken from this pool will be processed,
14174281808fSLars Ellenberg  * and given back, "quickly", and then can be recycled, so we can avoid
14184281808fSLars Ellenberg  * frequent calls to alloc_page(), and still will be able to make progress even
14194281808fSLars Ellenberg  * under memory pressure.
14204281808fSLars Ellenberg  */
14214281808fSLars Ellenberg extern struct page *drbd_pp_pool;
1422b411b363SPhilipp Reisner extern spinlock_t   drbd_pp_lock;
1423b411b363SPhilipp Reisner extern int	    drbd_pp_vacant;
1424b411b363SPhilipp Reisner extern wait_queue_head_t drbd_pp_wait;
1425b411b363SPhilipp Reisner 
14264281808fSLars Ellenberg /* We also need a standard (emergency-reserve backed) page pool
14274281808fSLars Ellenberg  * for meta data IO (activity log, bitmap).
14284281808fSLars Ellenberg  * We can keep it global, as long as it is used as "N pages at a time".
14294281808fSLars Ellenberg  * 128 should be plenty, currently we probably can get away with as few as 1.
14304281808fSLars Ellenberg  */
14314281808fSLars Ellenberg #define DRBD_MIN_POOL_PAGES	128
14324281808fSLars Ellenberg extern mempool_t *drbd_md_io_page_pool;
14334281808fSLars Ellenberg 
14349476f39dSLars Ellenberg /* We also need to make sure we get a bio
14359476f39dSLars Ellenberg  * when we need it for housekeeping purposes */
14369476f39dSLars Ellenberg extern struct bio_set *drbd_md_io_bio_set;
14379476f39dSLars Ellenberg /* to allocate from that set */
14389476f39dSLars Ellenberg extern struct bio *bio_alloc_drbd(gfp_t gfp_mask);
14399476f39dSLars Ellenberg 
1440b411b363SPhilipp Reisner extern rwlock_t global_state_lock;
1441b411b363SPhilipp Reisner 
144280883197SPhilipp Reisner extern int conn_lowest_minor(struct drbd_tconn *tconn);
1443774b3055SPhilipp Reisner enum drbd_ret_code conn_new_minor(struct drbd_tconn *tconn, unsigned int minor, int vnr);
144481fa2e67SPhilipp Reisner extern void drbd_minor_destroy(struct kref *kref);
1445b411b363SPhilipp Reisner 
1446afbbfa88SAndreas Gruenbacher extern int set_resource_options(struct drbd_tconn *tconn, struct res_opts *res_opts);
1447afbbfa88SAndreas Gruenbacher extern struct drbd_tconn *conn_create(const char *name, struct res_opts *res_opts);
14489dc9fbb3SPhilipp Reisner extern void conn_destroy(struct kref *kref);
14490ace9dfaSPhilipp Reisner struct drbd_tconn *conn_get_by_name(const char *name);
1450089c075dSAndreas Gruenbacher extern struct drbd_tconn *conn_get_by_addrs(void *my_addr, int my_addr_len,
1451089c075dSAndreas Gruenbacher 					    void *peer_addr, int peer_addr_len);
145291fd4dadSPhilipp Reisner extern void conn_free_crypto(struct drbd_tconn *tconn);
1453b411b363SPhilipp Reisner 
1454b411b363SPhilipp Reisner extern int proc_details;
1455b411b363SPhilipp Reisner 
1456b411b363SPhilipp Reisner /* drbd_req */
1457113fef9eSLars Ellenberg extern void do_submit(struct work_struct *ws);
14585df69eceSLars Ellenberg extern void __drbd_make_request(struct drbd_conf *, struct bio *, unsigned long);
14595a7bbad2SChristoph Hellwig extern void drbd_make_request(struct request_queue *q, struct bio *bio);
1460b411b363SPhilipp Reisner extern int drbd_read_remote(struct drbd_conf *mdev, struct drbd_request *req);
1461b411b363SPhilipp Reisner extern int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec);
1462b411b363SPhilipp Reisner extern int is_valid_ar_handle(struct drbd_request *, sector_t);
1463b411b363SPhilipp Reisner 
1464b411b363SPhilipp Reisner 
1465b411b363SPhilipp Reisner /* drbd_nl.c */
14668432b314SLars Ellenberg extern int drbd_msg_put_info(const char *info);
1467b411b363SPhilipp Reisner extern void drbd_suspend_io(struct drbd_conf *mdev);
1468b411b363SPhilipp Reisner extern void drbd_resume_io(struct drbd_conf *mdev);
1469b411b363SPhilipp Reisner extern char *ppsize(char *buf, unsigned long long size);
1470ef5e44a6SPhilipp Reisner extern sector_t drbd_new_dev_size(struct drbd_conf *, struct drbd_backing_dev *, sector_t, int);
1471e96c9633SPhilipp Reisner enum determine_dev_size {
1472*d752b269SPhilipp Reisner 	DS_ERROR_SHRINK = -3,
1473*d752b269SPhilipp Reisner 	DS_ERROR_SPACE_MD = -2,
1474e96c9633SPhilipp Reisner 	DS_ERROR = -1,
1475e96c9633SPhilipp Reisner 	DS_UNCHANGED = 0,
1476e96c9633SPhilipp Reisner 	DS_SHRUNK = 1,
1477e96c9633SPhilipp Reisner 	DS_GREW = 2
1478e96c9633SPhilipp Reisner };
1479*d752b269SPhilipp Reisner extern enum determine_dev_size
1480*d752b269SPhilipp Reisner drbd_determine_dev_size(struct drbd_conf *, enum dds_flags, struct resize_parms *) __must_hold(local);
1481b411b363SPhilipp Reisner extern void resync_after_online_grow(struct drbd_conf *);
148299432fccSPhilipp Reisner extern void drbd_reconsider_max_bio_size(struct drbd_conf *mdev);
1483bf885f8aSAndreas Gruenbacher extern enum drbd_state_rv drbd_set_role(struct drbd_conf *mdev,
1484bf885f8aSAndreas Gruenbacher 					enum drbd_role new_role,
1485b411b363SPhilipp Reisner 					int force);
1486cb703454SPhilipp Reisner extern bool conn_try_outdate_peer(struct drbd_tconn *tconn);
1487cb703454SPhilipp Reisner extern void conn_try_outdate_peer_async(struct drbd_tconn *tconn);
1488b411b363SPhilipp Reisner extern int drbd_khelper(struct drbd_conf *mdev, char *cmd);
1489b411b363SPhilipp Reisner 
1490b411b363SPhilipp Reisner /* drbd_worker.c */
1491b411b363SPhilipp Reisner extern int drbd_worker(struct drbd_thread *thi);
149295f8efd0SAndreas Gruenbacher enum drbd_ret_code drbd_resync_after_valid(struct drbd_conf *mdev, int o_minor);
149395f8efd0SAndreas Gruenbacher void drbd_resync_after_changed(struct drbd_conf *mdev);
1494b411b363SPhilipp Reisner extern void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side);
1495b411b363SPhilipp Reisner extern void resume_next_sg(struct drbd_conf *mdev);
1496b411b363SPhilipp Reisner extern void suspend_other_sg(struct drbd_conf *mdev);
1497b411b363SPhilipp Reisner extern int drbd_resync_finished(struct drbd_conf *mdev);
1498b411b363SPhilipp Reisner /* maybe rather drbd_main.c ? */
1499e1711731SPhilipp Reisner extern void *drbd_md_get_buffer(struct drbd_conf *mdev);
1500e1711731SPhilipp Reisner extern void drbd_md_put_buffer(struct drbd_conf *mdev);
1501b411b363SPhilipp Reisner extern int drbd_md_sync_page_io(struct drbd_conf *mdev,
1502b411b363SPhilipp Reisner 		struct drbd_backing_dev *bdev, sector_t sector, int rw);
15038f7bed77SAndreas Gruenbacher extern void drbd_ov_out_of_sync_found(struct drbd_conf *, sector_t, int);
150444edfb0dSLars Ellenberg extern void wait_until_done_or_force_detached(struct drbd_conf *mdev,
150544edfb0dSLars Ellenberg 		struct drbd_backing_dev *bdev, unsigned int *done);
15069bd28d3cSLars Ellenberg extern void drbd_rs_controller_reset(struct drbd_conf *mdev);
1507b411b363SPhilipp Reisner 
15088f7bed77SAndreas Gruenbacher static inline void ov_out_of_sync_print(struct drbd_conf *mdev)
1509b411b363SPhilipp Reisner {
1510b411b363SPhilipp Reisner 	if (mdev->ov_last_oos_size) {
1511b411b363SPhilipp Reisner 		dev_err(DEV, "Out of sync: start=%llu, size=%lu (sectors)\n",
1512b411b363SPhilipp Reisner 		     (unsigned long long)mdev->ov_last_oos_start,
1513b411b363SPhilipp Reisner 		     (unsigned long)mdev->ov_last_oos_size);
1514b411b363SPhilipp Reisner 	}
1515b411b363SPhilipp Reisner 	mdev->ov_last_oos_size=0;
1516b411b363SPhilipp Reisner }
1517b411b363SPhilipp Reisner 
1518b411b363SPhilipp Reisner 
151945bb912bSLars Ellenberg extern void drbd_csum_bio(struct drbd_conf *, struct crypto_hash *, struct bio *, void *);
1520f6ffca9fSAndreas Gruenbacher extern void drbd_csum_ee(struct drbd_conf *, struct crypto_hash *,
1521f6ffca9fSAndreas Gruenbacher 			 struct drbd_peer_request *, void *);
1522b411b363SPhilipp Reisner /* worker callbacks */
152399920dc5SAndreas Gruenbacher extern int w_e_end_data_req(struct drbd_work *, int);
152499920dc5SAndreas Gruenbacher extern int w_e_end_rsdata_req(struct drbd_work *, int);
152599920dc5SAndreas Gruenbacher extern int w_e_end_csum_rs_req(struct drbd_work *, int);
152699920dc5SAndreas Gruenbacher extern int w_e_end_ov_reply(struct drbd_work *, int);
152799920dc5SAndreas Gruenbacher extern int w_e_end_ov_req(struct drbd_work *, int);
152899920dc5SAndreas Gruenbacher extern int w_ov_finished(struct drbd_work *, int);
152999920dc5SAndreas Gruenbacher extern int w_resync_timer(struct drbd_work *, int);
153099920dc5SAndreas Gruenbacher extern int w_send_write_hint(struct drbd_work *, int);
153199920dc5SAndreas Gruenbacher extern int w_make_resync_request(struct drbd_work *, int);
153299920dc5SAndreas Gruenbacher extern int w_send_dblock(struct drbd_work *, int);
153399920dc5SAndreas Gruenbacher extern int w_send_read_req(struct drbd_work *, int);
153499920dc5SAndreas Gruenbacher extern int w_prev_work_done(struct drbd_work *, int);
153599920dc5SAndreas Gruenbacher extern int w_e_reissue(struct drbd_work *, int);
153699920dc5SAndreas Gruenbacher extern int w_restart_disk_io(struct drbd_work *, int);
15378f7bed77SAndreas Gruenbacher extern int w_send_out_of_sync(struct drbd_work *, int);
153899920dc5SAndreas Gruenbacher extern int w_start_resync(struct drbd_work *, int);
1539b411b363SPhilipp Reisner 
1540b411b363SPhilipp Reisner extern void resync_timer_fn(unsigned long data);
1541370a43e7SPhilipp Reisner extern void start_resync_timer_fn(unsigned long data);
1542b411b363SPhilipp Reisner 
1543b411b363SPhilipp Reisner /* drbd_receiver.c */
1544e3555d85SPhilipp Reisner extern int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector);
1545fbe29decSAndreas Gruenbacher extern int drbd_submit_peer_request(struct drbd_conf *,
1546fbe29decSAndreas Gruenbacher 				    struct drbd_peer_request *, const unsigned,
1547fbe29decSAndreas Gruenbacher 				    const int);
15487721f567SAndreas Gruenbacher extern int drbd_free_peer_reqs(struct drbd_conf *, struct list_head *);
15490db55363SAndreas Gruenbacher extern struct drbd_peer_request *drbd_alloc_peer_req(struct drbd_conf *, u64,
15500db55363SAndreas Gruenbacher 						     sector_t, unsigned int,
1551f6ffca9fSAndreas Gruenbacher 						     gfp_t) __must_hold(local);
15523967deb1SAndreas Gruenbacher extern void __drbd_free_peer_req(struct drbd_conf *, struct drbd_peer_request *,
1553f6ffca9fSAndreas Gruenbacher 				 int);
15543967deb1SAndreas Gruenbacher #define drbd_free_peer_req(m,e) __drbd_free_peer_req(m, e, 0)
15553967deb1SAndreas Gruenbacher #define drbd_free_net_peer_req(m,e) __drbd_free_peer_req(m, e, 1)
1556c37c8ecfSAndreas Gruenbacher extern struct page *drbd_alloc_pages(struct drbd_conf *, unsigned int, bool);
1557b411b363SPhilipp Reisner extern void drbd_set_recv_tcq(struct drbd_conf *mdev, int tcq_enabled);
1558b411b363SPhilipp Reisner extern void _drbd_clear_done_ee(struct drbd_conf *mdev, struct list_head *to_be_freed);
15590e29d163SPhilipp Reisner extern void conn_flush_workqueue(struct drbd_tconn *tconn);
1560c141ebdaSPhilipp Reisner extern int drbd_connected(struct drbd_conf *mdev);
15610e29d163SPhilipp Reisner static inline void drbd_flush_workqueue(struct drbd_conf *mdev)
1562b411b363SPhilipp Reisner {
15630e29d163SPhilipp Reisner 	conn_flush_workqueue(mdev->tconn);
15640e29d163SPhilipp Reisner }
1565b411b363SPhilipp Reisner 
1566ed439848SLars Ellenberg /* Yes, there is kernel_setsockopt, but only since 2.6.18.
1567ed439848SLars Ellenberg  * So we have our own copy of it here. */
1568b411b363SPhilipp Reisner static inline int drbd_setsockopt(struct socket *sock, int level, int optname,
1569ed439848SLars Ellenberg 				  char *optval, int optlen)
1570b411b363SPhilipp Reisner {
1571ed439848SLars Ellenberg 	mm_segment_t oldfs = get_fs();
1572ed439848SLars Ellenberg 	char __user *uoptval;
1573b411b363SPhilipp Reisner 	int err;
1574ed439848SLars Ellenberg 
1575ed439848SLars Ellenberg 	uoptval = (char __user __force *)optval;
1576ed439848SLars Ellenberg 
1577ed439848SLars Ellenberg 	set_fs(KERNEL_DS);
1578b411b363SPhilipp Reisner 	if (level == SOL_SOCKET)
1579ed439848SLars Ellenberg 		err = sock_setsockopt(sock, level, optname, uoptval, optlen);
1580b411b363SPhilipp Reisner 	else
1581ed439848SLars Ellenberg 		err = sock->ops->setsockopt(sock, level, optname, uoptval,
1582b411b363SPhilipp Reisner 					    optlen);
1583ed439848SLars Ellenberg 	set_fs(oldfs);
1584b411b363SPhilipp Reisner 	return err;
1585b411b363SPhilipp Reisner }
1586b411b363SPhilipp Reisner 
1587b411b363SPhilipp Reisner static inline void drbd_tcp_cork(struct socket *sock)
1588b411b363SPhilipp Reisner {
1589ed439848SLars Ellenberg 	int val = 1;
1590b411b363SPhilipp Reisner 	(void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1591ed439848SLars Ellenberg 			(char*)&val, sizeof(val));
1592b411b363SPhilipp Reisner }
1593b411b363SPhilipp Reisner 
1594b411b363SPhilipp Reisner static inline void drbd_tcp_uncork(struct socket *sock)
1595b411b363SPhilipp Reisner {
1596ed439848SLars Ellenberg 	int val = 0;
1597b411b363SPhilipp Reisner 	(void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1598ed439848SLars Ellenberg 			(char*)&val, sizeof(val));
1599b411b363SPhilipp Reisner }
1600b411b363SPhilipp Reisner 
1601b411b363SPhilipp Reisner static inline void drbd_tcp_nodelay(struct socket *sock)
1602b411b363SPhilipp Reisner {
1603ed439848SLars Ellenberg 	int val = 1;
1604b411b363SPhilipp Reisner 	(void) drbd_setsockopt(sock, SOL_TCP, TCP_NODELAY,
1605ed439848SLars Ellenberg 			(char*)&val, sizeof(val));
1606b411b363SPhilipp Reisner }
1607b411b363SPhilipp Reisner 
1608b411b363SPhilipp Reisner static inline void drbd_tcp_quickack(struct socket *sock)
1609b411b363SPhilipp Reisner {
1610ed439848SLars Ellenberg 	int val = 2;
1611b411b363SPhilipp Reisner 	(void) drbd_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
1612ed439848SLars Ellenberg 			(char*)&val, sizeof(val));
1613b411b363SPhilipp Reisner }
1614b411b363SPhilipp Reisner 
16154b0007c0SPhilipp Reisner void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo);
1616b411b363SPhilipp Reisner 
1617b411b363SPhilipp Reisner /* drbd_proc.c */
1618b411b363SPhilipp Reisner extern struct proc_dir_entry *drbd_proc;
16197d4e9d09SEmese Revfy extern const struct file_operations drbd_proc_fops;
1620b411b363SPhilipp Reisner extern const char *drbd_conn_str(enum drbd_conns s);
1621b411b363SPhilipp Reisner extern const char *drbd_role_str(enum drbd_role s);
1622b411b363SPhilipp Reisner 
1623b411b363SPhilipp Reisner /* drbd_actlog.c */
162408a1ddabSLars Ellenberg extern int drbd_al_begin_io_nonblock(struct drbd_conf *mdev, struct drbd_interval *i);
162508a1ddabSLars Ellenberg extern void drbd_al_begin_io_commit(struct drbd_conf *mdev, bool delegate);
1626b5bc8e08SLars Ellenberg extern bool drbd_al_begin_io_fastpath(struct drbd_conf *mdev, struct drbd_interval *i);
162756392d2fSLars Ellenberg extern void drbd_al_begin_io(struct drbd_conf *mdev, struct drbd_interval *i, bool delegate);
1628181286adSLars Ellenberg extern void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i);
1629b411b363SPhilipp Reisner extern void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector);
1630b411b363SPhilipp Reisner extern int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1631b411b363SPhilipp Reisner extern int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1632b411b363SPhilipp Reisner extern void drbd_rs_cancel_all(struct drbd_conf *mdev);
1633b411b363SPhilipp Reisner extern int drbd_rs_del_all(struct drbd_conf *mdev);
1634b411b363SPhilipp Reisner extern void drbd_rs_failed_io(struct drbd_conf *mdev,
1635b411b363SPhilipp Reisner 		sector_t sector, int size);
1636ea5442afSLars Ellenberg extern void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go);
1637b411b363SPhilipp Reisner extern void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector,
1638b411b363SPhilipp Reisner 		int size, const char *file, const unsigned int line);
1639b411b363SPhilipp Reisner #define drbd_set_in_sync(mdev, sector, size) \
1640b411b363SPhilipp Reisner 	__drbd_set_in_sync(mdev, sector, size, __FILE__, __LINE__)
164173a01a18SPhilipp Reisner extern int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector,
1642b411b363SPhilipp Reisner 		int size, const char *file, const unsigned int line);
1643b411b363SPhilipp Reisner #define drbd_set_out_of_sync(mdev, sector, size) \
1644b411b363SPhilipp Reisner 	__drbd_set_out_of_sync(mdev, sector, size, __FILE__, __LINE__)
1645b411b363SPhilipp Reisner extern void drbd_al_shrink(struct drbd_conf *mdev);
1646*d752b269SPhilipp Reisner extern int drbd_initialize_al(struct drbd_conf *, void *);
1647b411b363SPhilipp Reisner 
1648b411b363SPhilipp Reisner /* drbd_nl.c */
16493b98c0c2SLars Ellenberg /* state info broadcast */
16503b98c0c2SLars Ellenberg struct sib_info {
16513b98c0c2SLars Ellenberg 	enum drbd_state_info_bcast_reason sib_reason;
16523b98c0c2SLars Ellenberg 	union {
16533b98c0c2SLars Ellenberg 		struct {
16543b98c0c2SLars Ellenberg 			char *helper_name;
16553b98c0c2SLars Ellenberg 			unsigned helper_exit_code;
16563b98c0c2SLars Ellenberg 		};
16573b98c0c2SLars Ellenberg 		struct {
16583b98c0c2SLars Ellenberg 			union drbd_state os;
16593b98c0c2SLars Ellenberg 			union drbd_state ns;
16603b98c0c2SLars Ellenberg 		};
16613b98c0c2SLars Ellenberg 	};
16623b98c0c2SLars Ellenberg };
16633b98c0c2SLars Ellenberg void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib);
1664b411b363SPhilipp Reisner 
1665b411b363SPhilipp Reisner /*
1666b411b363SPhilipp Reisner  * inline helper functions
1667b411b363SPhilipp Reisner  *************************/
1668b411b363SPhilipp Reisner 
166945bb912bSLars Ellenberg /* see also page_chain_add and friends in drbd_receiver.c */
167045bb912bSLars Ellenberg static inline struct page *page_chain_next(struct page *page)
167145bb912bSLars Ellenberg {
167245bb912bSLars Ellenberg 	return (struct page *)page_private(page);
167345bb912bSLars Ellenberg }
167445bb912bSLars Ellenberg #define page_chain_for_each(page) \
167545bb912bSLars Ellenberg 	for (; page && ({ prefetch(page_chain_next(page)); 1; }); \
167645bb912bSLars Ellenberg 			page = page_chain_next(page))
167745bb912bSLars Ellenberg #define page_chain_for_each_safe(page, n) \
167845bb912bSLars Ellenberg 	for (; page && ({ n = page_chain_next(page); 1; }); page = n)
167945bb912bSLars Ellenberg 
168045bb912bSLars Ellenberg 
1681045417f7SAndreas Gruenbacher static inline int drbd_peer_req_has_active_page(struct drbd_peer_request *peer_req)
168245bb912bSLars Ellenberg {
1683db830c46SAndreas Gruenbacher 	struct page *page = peer_req->pages;
168445bb912bSLars Ellenberg 	page_chain_for_each(page) {
168545bb912bSLars Ellenberg 		if (page_count(page) > 1)
168645bb912bSLars Ellenberg 			return 1;
168745bb912bSLars Ellenberg 	}
168845bb912bSLars Ellenberg 	return 0;
168945bb912bSLars Ellenberg }
169045bb912bSLars Ellenberg 
1691bf885f8aSAndreas Gruenbacher static inline enum drbd_state_rv
1692bf885f8aSAndreas Gruenbacher _drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
1693bf885f8aSAndreas Gruenbacher 		enum chg_state_flags flags, struct completion *done)
1694b411b363SPhilipp Reisner {
1695bf885f8aSAndreas Gruenbacher 	enum drbd_state_rv rv;
1696b411b363SPhilipp Reisner 
1697b411b363SPhilipp Reisner 	read_lock(&global_state_lock);
1698b411b363SPhilipp Reisner 	rv = __drbd_set_state(mdev, ns, flags, done);
1699b411b363SPhilipp Reisner 	read_unlock(&global_state_lock);
1700b411b363SPhilipp Reisner 
1701b411b363SPhilipp Reisner 	return rv;
1702b411b363SPhilipp Reisner }
1703b411b363SPhilipp Reisner 
170478bae59bSPhilipp Reisner static inline union drbd_state drbd_read_state(struct drbd_conf *mdev)
1705b411b363SPhilipp Reisner {
170678bae59bSPhilipp Reisner 	union drbd_state rv;
170778bae59bSPhilipp Reisner 
1708da9fbc27SPhilipp Reisner 	rv.i = mdev->state.i;
17098e0af25fSPhilipp Reisner 	rv.susp = mdev->tconn->susp;
17108e0af25fSPhilipp Reisner 	rv.susp_nod = mdev->tconn->susp_nod;
17118e0af25fSPhilipp Reisner 	rv.susp_fen = mdev->tconn->susp_fen;
171278bae59bSPhilipp Reisner 
171378bae59bSPhilipp Reisner 	return rv;
1714b411b363SPhilipp Reisner }
1715b411b363SPhilipp Reisner 
1716383606e0SLars Ellenberg enum drbd_force_detach_flags {
1717a2a3c74fSLars Ellenberg 	DRBD_READ_ERROR,
1718a2a3c74fSLars Ellenberg 	DRBD_WRITE_ERROR,
1719383606e0SLars Ellenberg 	DRBD_META_IO_ERROR,
1720383606e0SLars Ellenberg 	DRBD_FORCE_DETACH,
1721383606e0SLars Ellenberg };
1722383606e0SLars Ellenberg 
1723b411b363SPhilipp Reisner #define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
1724383606e0SLars Ellenberg static inline void __drbd_chk_io_error_(struct drbd_conf *mdev,
1725a2a3c74fSLars Ellenberg 		enum drbd_force_detach_flags df,
1726383606e0SLars Ellenberg 		const char *where)
1727b411b363SPhilipp Reisner {
1728daeda1ccSPhilipp Reisner 	enum drbd_io_error_p ep;
1729daeda1ccSPhilipp Reisner 
1730daeda1ccSPhilipp Reisner 	rcu_read_lock();
1731daeda1ccSPhilipp Reisner 	ep = rcu_dereference(mdev->ldev->disk_conf)->on_io_error;
1732daeda1ccSPhilipp Reisner 	rcu_read_unlock();
1733daeda1ccSPhilipp Reisner 	switch (ep) {
1734daeda1ccSPhilipp Reisner 	case EP_PASS_ON: /* FIXME would this be better named "Ignore"? */
1735a2a3c74fSLars Ellenberg 		if (df == DRBD_READ_ERROR || df == DRBD_WRITE_ERROR) {
17367383506cSLars Ellenberg 			if (__ratelimit(&drbd_ratelimit_state))
173782f59cc6SLars Ellenberg 				dev_err(DEV, "Local IO failed in %s.\n", where);
1738d2e17807SPhilipp Reisner 			if (mdev->state.disk > D_INCONSISTENT)
1739738a84b2SPhilipp Reisner 				_drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_HARD, NULL);
1740b411b363SPhilipp Reisner 			break;
1741b411b363SPhilipp Reisner 		}
1742a2a3c74fSLars Ellenberg 		/* NOTE fall through for DRBD_META_IO_ERROR or DRBD_FORCE_DETACH */
1743b411b363SPhilipp Reisner 	case EP_DETACH:
1744b411b363SPhilipp Reisner 	case EP_CALL_HELPER:
1745a2a3c74fSLars Ellenberg 		/* Remember whether we saw a READ or WRITE error.
1746a2a3c74fSLars Ellenberg 		 *
1747a2a3c74fSLars Ellenberg 		 * Recovery of the affected area for WRITE failure is covered
1748a2a3c74fSLars Ellenberg 		 * by the activity log.
1749a2a3c74fSLars Ellenberg 		 * READ errors may fall outside that area though. Certain READ
1750a2a3c74fSLars Ellenberg 		 * errors can be "healed" by writing good data to the affected
1751a2a3c74fSLars Ellenberg 		 * blocks, which triggers block re-allocation in lower layers.
1752a2a3c74fSLars Ellenberg 		 *
1753a2a3c74fSLars Ellenberg 		 * If we can not write the bitmap after a READ error,
1754a2a3c74fSLars Ellenberg 		 * we may need to trigger a full sync (see w_go_diskless()).
1755a2a3c74fSLars Ellenberg 		 *
1756a2a3c74fSLars Ellenberg 		 * Force-detach is not really an IO error, but rather a
1757a2a3c74fSLars Ellenberg 		 * desperate measure to try to deal with a completely
1758a2a3c74fSLars Ellenberg 		 * unresponsive lower level IO stack.
1759a2a3c74fSLars Ellenberg 		 * Still it should be treated as a WRITE error.
1760a2a3c74fSLars Ellenberg 		 *
1761a2a3c74fSLars Ellenberg 		 * Meta IO error is always WRITE error:
1762a2a3c74fSLars Ellenberg 		 * we read meta data only once during attach,
1763a2a3c74fSLars Ellenberg 		 * which will fail in case of errors.
1764a2a3c74fSLars Ellenberg 		 */
176582f59cc6SLars Ellenberg 		set_bit(WAS_IO_ERROR, &mdev->flags);
1766a2a3c74fSLars Ellenberg 		if (df == DRBD_READ_ERROR)
1767edc9f5ebSLars Ellenberg 			set_bit(WAS_READ_ERROR, &mdev->flags);
1768a2a3c74fSLars Ellenberg 		if (df == DRBD_FORCE_DETACH)
17690c849666SLars Ellenberg 			set_bit(FORCE_DETACH, &mdev->flags);
1770b411b363SPhilipp Reisner 		if (mdev->state.disk > D_FAILED) {
1771b411b363SPhilipp Reisner 			_drbd_set_state(_NS(mdev, disk, D_FAILED), CS_HARD, NULL);
177282f59cc6SLars Ellenberg 			dev_err(DEV,
177382f59cc6SLars Ellenberg 				"Local IO failed in %s. Detaching...\n", where);
1774b411b363SPhilipp Reisner 		}
1775b411b363SPhilipp Reisner 		break;
1776b411b363SPhilipp Reisner 	}
1777b411b363SPhilipp Reisner }
1778b411b363SPhilipp Reisner 
1779b411b363SPhilipp Reisner /**
1780b411b363SPhilipp Reisner  * drbd_chk_io_error: Handle the on_io_error setting, should be called from all io completion handlers
1781b411b363SPhilipp Reisner  * @mdev:	 DRBD device.
1782b411b363SPhilipp Reisner  * @error:	 Error code passed to the IO completion callback
1783b411b363SPhilipp Reisner  * @forcedetach: Force detach. I.e. the error happened while accessing the meta data
1784b411b363SPhilipp Reisner  *
1785b411b363SPhilipp Reisner  * See also drbd_main.c:after_state_ch() if (os.disk > D_FAILED && ns.disk == D_FAILED)
1786b411b363SPhilipp Reisner  */
1787b411b363SPhilipp Reisner #define drbd_chk_io_error(m,e,f) drbd_chk_io_error_(m,e,f, __func__)
1788b411b363SPhilipp Reisner static inline void drbd_chk_io_error_(struct drbd_conf *mdev,
1789383606e0SLars Ellenberg 	int error, enum drbd_force_detach_flags forcedetach, const char *where)
1790b411b363SPhilipp Reisner {
1791b411b363SPhilipp Reisner 	if (error) {
1792b411b363SPhilipp Reisner 		unsigned long flags;
179387eeee41SPhilipp Reisner 		spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1794b411b363SPhilipp Reisner 		__drbd_chk_io_error_(mdev, forcedetach, where);
179587eeee41SPhilipp Reisner 		spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1796b411b363SPhilipp Reisner 	}
1797b411b363SPhilipp Reisner }
1798b411b363SPhilipp Reisner 
1799b411b363SPhilipp Reisner 
1800b411b363SPhilipp Reisner /**
1801b411b363SPhilipp Reisner  * drbd_md_first_sector() - Returns the first sector number of the meta data area
1802b411b363SPhilipp Reisner  * @bdev:	Meta data block device.
1803b411b363SPhilipp Reisner  *
1804b411b363SPhilipp Reisner  * BTW, for internal meta data, this happens to be the maximum capacity
1805b411b363SPhilipp Reisner  * we could agree upon with our peer node.
1806b411b363SPhilipp Reisner  */
180768e41a43SLars Ellenberg static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
1808b411b363SPhilipp Reisner {
180968e41a43SLars Ellenberg 	switch (bdev->md.meta_dev_idx) {
1810b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_INTERNAL:
1811b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_INT:
1812b411b363SPhilipp Reisner 		return bdev->md.md_offset + bdev->md.bm_offset;
1813b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_EXT:
1814b411b363SPhilipp Reisner 	default:
1815b411b363SPhilipp Reisner 		return bdev->md.md_offset;
1816b411b363SPhilipp Reisner 	}
1817b411b363SPhilipp Reisner }
1818b411b363SPhilipp Reisner 
1819b411b363SPhilipp Reisner /**
1820b411b363SPhilipp Reisner  * drbd_md_last_sector() - Return the last sector number of the meta data area
1821b411b363SPhilipp Reisner  * @bdev:	Meta data block device.
1822b411b363SPhilipp Reisner  */
1823b411b363SPhilipp Reisner static inline sector_t drbd_md_last_sector(struct drbd_backing_dev *bdev)
1824b411b363SPhilipp Reisner {
182568e41a43SLars Ellenberg 	switch (bdev->md.meta_dev_idx) {
1826b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_INTERNAL:
1827b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_INT:
1828ae8bf312SLars Ellenberg 		return bdev->md.md_offset + MD_4kB_SECT -1;
1829b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_EXT:
1830b411b363SPhilipp Reisner 	default:
1831ae8bf312SLars Ellenberg 		return bdev->md.md_offset + bdev->md.md_size_sect -1;
1832b411b363SPhilipp Reisner 	}
1833b411b363SPhilipp Reisner }
1834b411b363SPhilipp Reisner 
1835b411b363SPhilipp Reisner /* Returns the number of 512 byte sectors of the device */
1836b411b363SPhilipp Reisner static inline sector_t drbd_get_capacity(struct block_device *bdev)
1837b411b363SPhilipp Reisner {
1838b411b363SPhilipp Reisner 	/* return bdev ? get_capacity(bdev->bd_disk) : 0; */
183977304d2aSMike Snitzer 	return bdev ? i_size_read(bdev->bd_inode) >> 9 : 0;
1840b411b363SPhilipp Reisner }
1841b411b363SPhilipp Reisner 
1842b411b363SPhilipp Reisner /**
1843b411b363SPhilipp Reisner  * drbd_get_max_capacity() - Returns the capacity we announce to out peer
1844b411b363SPhilipp Reisner  * @bdev:	Meta data block device.
1845b411b363SPhilipp Reisner  *
1846b411b363SPhilipp Reisner  * returns the capacity we announce to out peer.  we clip ourselves at the
1847b411b363SPhilipp Reisner  * various MAX_SECTORS, because if we don't, current implementation will
1848b411b363SPhilipp Reisner  * oops sooner or later
1849b411b363SPhilipp Reisner  */
1850b411b363SPhilipp Reisner static inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
1851b411b363SPhilipp Reisner {
1852b411b363SPhilipp Reisner 	sector_t s;
1853daeda1ccSPhilipp Reisner 
185468e41a43SLars Ellenberg 	switch (bdev->md.meta_dev_idx) {
1855b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_INTERNAL:
1856b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_INT:
1857b411b363SPhilipp Reisner 		s = drbd_get_capacity(bdev->backing_bdev)
1858b411b363SPhilipp Reisner 			? min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
185968e41a43SLars Ellenberg 				drbd_md_first_sector(bdev))
1860b411b363SPhilipp Reisner 			: 0;
1861b411b363SPhilipp Reisner 		break;
1862b411b363SPhilipp Reisner 	case DRBD_MD_INDEX_FLEX_EXT:
1863b411b363SPhilipp Reisner 		s = min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1864b411b363SPhilipp Reisner 				drbd_get_capacity(bdev->backing_bdev));
1865b411b363SPhilipp Reisner 		/* clip at maximum size the meta device can support */
1866b411b363SPhilipp Reisner 		s = min_t(sector_t, s,
1867b411b363SPhilipp Reisner 			BM_EXT_TO_SECT(bdev->md.md_size_sect
1868b411b363SPhilipp Reisner 				     - bdev->md.bm_offset));
1869b411b363SPhilipp Reisner 		break;
1870b411b363SPhilipp Reisner 	default:
1871b411b363SPhilipp Reisner 		s = min_t(sector_t, DRBD_MAX_SECTORS,
1872b411b363SPhilipp Reisner 				drbd_get_capacity(bdev->backing_bdev));
1873b411b363SPhilipp Reisner 	}
1874b411b363SPhilipp Reisner 	return s;
1875b411b363SPhilipp Reisner }
1876b411b363SPhilipp Reisner 
1877b411b363SPhilipp Reisner /**
18783a4d4eb3SLars Ellenberg  * drbd_md_ss() - Return the sector number of our meta data super block
1879b411b363SPhilipp Reisner  * @bdev:	Meta data block device.
1880b411b363SPhilipp Reisner  */
18813a4d4eb3SLars Ellenberg static inline sector_t drbd_md_ss(struct drbd_backing_dev *bdev)
1882b411b363SPhilipp Reisner {
18833a4d4eb3SLars Ellenberg 	const int meta_dev_idx = bdev->md.meta_dev_idx;
1884daeda1ccSPhilipp Reisner 
18853a4d4eb3SLars Ellenberg 	if (meta_dev_idx == DRBD_MD_INDEX_FLEX_EXT)
1886b411b363SPhilipp Reisner 		return 0;
18873a4d4eb3SLars Ellenberg 
18883a4d4eb3SLars Ellenberg 	/* Since drbd08, internal meta data is always "flexible".
1889ae8bf312SLars Ellenberg 	 * position: last 4k aligned block of 4k size */
18903a4d4eb3SLars Ellenberg 	if (meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
18913a4d4eb3SLars Ellenberg 	    meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)
1892ae8bf312SLars Ellenberg 		return (drbd_get_capacity(bdev->backing_bdev) & ~7ULL) - 8;
18933a4d4eb3SLars Ellenberg 
18943a4d4eb3SLars Ellenberg 	/* external, some index; this is the old fixed size layout */
18953a4d4eb3SLars Ellenberg 	return MD_128MB_SECT * bdev->md.meta_dev_idx;
1896b411b363SPhilipp Reisner }
1897b411b363SPhilipp Reisner 
1898b411b363SPhilipp Reisner static inline void
1899b411b363SPhilipp Reisner drbd_queue_work_front(struct drbd_work_queue *q, struct drbd_work *w)
1900b411b363SPhilipp Reisner {
1901b411b363SPhilipp Reisner 	unsigned long flags;
1902b411b363SPhilipp Reisner 	spin_lock_irqsave(&q->q_lock, flags);
1903b411b363SPhilipp Reisner 	list_add(&w->list, &q->q);
1904b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&q->q_lock, flags);
19058c0785a5SLars Ellenberg 	wake_up(&q->q_wait);
1906b411b363SPhilipp Reisner }
1907b411b363SPhilipp Reisner 
1908b411b363SPhilipp Reisner static inline void
1909b411b363SPhilipp Reisner drbd_queue_work(struct drbd_work_queue *q, struct drbd_work *w)
1910b411b363SPhilipp Reisner {
1911b411b363SPhilipp Reisner 	unsigned long flags;
1912b411b363SPhilipp Reisner 	spin_lock_irqsave(&q->q_lock, flags);
1913b411b363SPhilipp Reisner 	list_add_tail(&w->list, &q->q);
1914b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&q->q_lock, flags);
19158c0785a5SLars Ellenberg 	wake_up(&q->q_wait);
1916b411b363SPhilipp Reisner }
1917b411b363SPhilipp Reisner 
19180625ac19SPhilipp Reisner static inline void wake_asender(struct drbd_tconn *tconn)
1919b411b363SPhilipp Reisner {
19200625ac19SPhilipp Reisner 	if (test_bit(SIGNAL_ASENDER, &tconn->flags))
19210625ac19SPhilipp Reisner 		force_sig(DRBD_SIG, tconn->asender.task);
1922b411b363SPhilipp Reisner }
1923b411b363SPhilipp Reisner 
19240625ac19SPhilipp Reisner static inline void request_ping(struct drbd_tconn *tconn)
1925b411b363SPhilipp Reisner {
19260625ac19SPhilipp Reisner 	set_bit(SEND_PING, &tconn->flags);
19270625ac19SPhilipp Reisner 	wake_asender(tconn);
1928b411b363SPhilipp Reisner }
1929b411b363SPhilipp Reisner 
1930dba58587SAndreas Gruenbacher extern void *conn_prepare_command(struct drbd_tconn *, struct drbd_socket *);
1931dba58587SAndreas Gruenbacher extern void *drbd_prepare_command(struct drbd_conf *, struct drbd_socket *);
1932dba58587SAndreas Gruenbacher extern int conn_send_command(struct drbd_tconn *, struct drbd_socket *,
1933dba58587SAndreas Gruenbacher 			     enum drbd_packet, unsigned int, void *,
1934dba58587SAndreas Gruenbacher 			     unsigned int);
1935dba58587SAndreas Gruenbacher extern int drbd_send_command(struct drbd_conf *, struct drbd_socket *,
1936dba58587SAndreas Gruenbacher 			     enum drbd_packet, unsigned int, void *,
1937dba58587SAndreas Gruenbacher 			     unsigned int);
1938b411b363SPhilipp Reisner 
1939e307f352SAndreas Gruenbacher extern int drbd_send_ping(struct drbd_tconn *tconn);
1940e307f352SAndreas Gruenbacher extern int drbd_send_ping_ack(struct drbd_tconn *tconn);
19419f5bdc33SAndreas Gruenbacher extern int drbd_send_state_req(struct drbd_conf *, union drbd_state, union drbd_state);
19429f5bdc33SAndreas Gruenbacher extern int conn_send_state_req(struct drbd_tconn *, union drbd_state, union drbd_state);
1943b411b363SPhilipp Reisner 
1944b411b363SPhilipp Reisner static inline void drbd_thread_stop(struct drbd_thread *thi)
1945b411b363SPhilipp Reisner {
194681e84650SAndreas Gruenbacher 	_drbd_thread_stop(thi, false, true);
1947b411b363SPhilipp Reisner }
1948b411b363SPhilipp Reisner 
1949b411b363SPhilipp Reisner static inline void drbd_thread_stop_nowait(struct drbd_thread *thi)
1950b411b363SPhilipp Reisner {
195181e84650SAndreas Gruenbacher 	_drbd_thread_stop(thi, false, false);
1952b411b363SPhilipp Reisner }
1953b411b363SPhilipp Reisner 
1954b411b363SPhilipp Reisner static inline void drbd_thread_restart_nowait(struct drbd_thread *thi)
1955b411b363SPhilipp Reisner {
195681e84650SAndreas Gruenbacher 	_drbd_thread_stop(thi, true, false);
1957b411b363SPhilipp Reisner }
1958b411b363SPhilipp Reisner 
1959b411b363SPhilipp Reisner /* counts how many answer packets packets we expect from our peer,
1960b411b363SPhilipp Reisner  * for either explicit application requests,
1961b411b363SPhilipp Reisner  * or implicit barrier packets as necessary.
1962b411b363SPhilipp Reisner  * increased:
1963b411b363SPhilipp Reisner  *  w_send_barrier
19648554df1cSAndreas Gruenbacher  *  _req_mod(req, QUEUE_FOR_NET_WRITE or QUEUE_FOR_NET_READ);
1965b411b363SPhilipp Reisner  *    it is much easier and equally valid to count what we queue for the
1966b411b363SPhilipp Reisner  *    worker, even before it actually was queued or send.
1967b411b363SPhilipp Reisner  *    (drbd_make_request_common; recovery path on read io-error)
1968b411b363SPhilipp Reisner  * decreased:
1969b411b363SPhilipp Reisner  *  got_BarrierAck (respective tl_clear, tl_clear_barrier)
19708554df1cSAndreas Gruenbacher  *  _req_mod(req, DATA_RECEIVED)
1971b411b363SPhilipp Reisner  *     [from receive_DataReply]
19728554df1cSAndreas Gruenbacher  *  _req_mod(req, WRITE_ACKED_BY_PEER or RECV_ACKED_BY_PEER or NEG_ACKED)
1973b411b363SPhilipp Reisner  *     [from got_BlockAck (P_WRITE_ACK, P_RECV_ACK)]
1974b411b363SPhilipp Reisner  *     for some reason it is NOT decreased in got_NegAck,
1975b411b363SPhilipp Reisner  *     but in the resulting cleanup code from report_params.
1976b411b363SPhilipp Reisner  *     we should try to remember the reason for that...
19778554df1cSAndreas Gruenbacher  *  _req_mod(req, SEND_FAILED or SEND_CANCELED)
19788554df1cSAndreas Gruenbacher  *  _req_mod(req, CONNECTION_LOST_WHILE_PENDING)
1979b411b363SPhilipp Reisner  *     [from tl_clear_barrier]
1980b411b363SPhilipp Reisner  */
1981b411b363SPhilipp Reisner static inline void inc_ap_pending(struct drbd_conf *mdev)
1982b411b363SPhilipp Reisner {
1983b411b363SPhilipp Reisner 	atomic_inc(&mdev->ap_pending_cnt);
1984b411b363SPhilipp Reisner }
1985b411b363SPhilipp Reisner 
198649559d87SPhilipp Reisner #define ERR_IF_CNT_IS_NEGATIVE(which, func, line)			\
1987b411b363SPhilipp Reisner 	if (atomic_read(&mdev->which) < 0)				\
1988b411b363SPhilipp Reisner 		dev_err(DEV, "in %s:%d: " #which " = %d < 0 !\n",	\
198949559d87SPhilipp Reisner 			func, line,					\
1990b411b363SPhilipp Reisner 			atomic_read(&mdev->which))
1991b411b363SPhilipp Reisner 
199249559d87SPhilipp Reisner #define dec_ap_pending(mdev) _dec_ap_pending(mdev, __FUNCTION__, __LINE__)
199349559d87SPhilipp Reisner static inline void _dec_ap_pending(struct drbd_conf *mdev, const char *func, int line)
199449559d87SPhilipp Reisner {
199549559d87SPhilipp Reisner 	if (atomic_dec_and_test(&mdev->ap_pending_cnt))
199649559d87SPhilipp Reisner 		wake_up(&mdev->misc_wait);
199749559d87SPhilipp Reisner 	ERR_IF_CNT_IS_NEGATIVE(ap_pending_cnt, func, line);
199849559d87SPhilipp Reisner }
1999b411b363SPhilipp Reisner 
2000b411b363SPhilipp Reisner /* counts how many resync-related answers we still expect from the peer
2001b411b363SPhilipp Reisner  *		     increase			decrease
2002b411b363SPhilipp Reisner  * C_SYNC_TARGET sends P_RS_DATA_REQUEST (and expects P_RS_DATA_REPLY)
200325985edcSLucas De Marchi  * C_SYNC_SOURCE sends P_RS_DATA_REPLY   (and expects P_WRITE_ACK with ID_SYNCER)
2004b411b363SPhilipp Reisner  *					   (or P_NEG_ACK with ID_SYNCER)
2005b411b363SPhilipp Reisner  */
2006b411b363SPhilipp Reisner static inline void inc_rs_pending(struct drbd_conf *mdev)
2007b411b363SPhilipp Reisner {
2008b411b363SPhilipp Reisner 	atomic_inc(&mdev->rs_pending_cnt);
2009b411b363SPhilipp Reisner }
2010b411b363SPhilipp Reisner 
201149559d87SPhilipp Reisner #define dec_rs_pending(mdev) _dec_rs_pending(mdev, __FUNCTION__, __LINE__)
201249559d87SPhilipp Reisner static inline void _dec_rs_pending(struct drbd_conf *mdev, const char *func, int line)
201349559d87SPhilipp Reisner {
201449559d87SPhilipp Reisner 	atomic_dec(&mdev->rs_pending_cnt);
201549559d87SPhilipp Reisner 	ERR_IF_CNT_IS_NEGATIVE(rs_pending_cnt, func, line);
201649559d87SPhilipp Reisner }
2017b411b363SPhilipp Reisner 
2018b411b363SPhilipp Reisner /* counts how many answers we still need to send to the peer.
2019b411b363SPhilipp Reisner  * increased on
2020b411b363SPhilipp Reisner  *  receive_Data	unless protocol A;
2021b411b363SPhilipp Reisner  *			we need to send a P_RECV_ACK (proto B)
2022b411b363SPhilipp Reisner  *			or P_WRITE_ACK (proto C)
2023b411b363SPhilipp Reisner  *  receive_RSDataReply (recv_resync_read) we need to send a P_WRITE_ACK
2024b411b363SPhilipp Reisner  *  receive_DataRequest (receive_RSDataRequest) we need to send back P_DATA
2025b411b363SPhilipp Reisner  *  receive_Barrier_*	we need to send a P_BARRIER_ACK
2026b411b363SPhilipp Reisner  */
2027b411b363SPhilipp Reisner static inline void inc_unacked(struct drbd_conf *mdev)
2028b411b363SPhilipp Reisner {
2029b411b363SPhilipp Reisner 	atomic_inc(&mdev->unacked_cnt);
2030b411b363SPhilipp Reisner }
2031b411b363SPhilipp Reisner 
203249559d87SPhilipp Reisner #define dec_unacked(mdev) _dec_unacked(mdev, __FUNCTION__, __LINE__)
203349559d87SPhilipp Reisner static inline void _dec_unacked(struct drbd_conf *mdev, const char *func, int line)
2034b411b363SPhilipp Reisner {
203549559d87SPhilipp Reisner 	atomic_dec(&mdev->unacked_cnt);
203649559d87SPhilipp Reisner 	ERR_IF_CNT_IS_NEGATIVE(unacked_cnt, func, line);
2037b411b363SPhilipp Reisner }
2038b411b363SPhilipp Reisner 
203949559d87SPhilipp Reisner #define sub_unacked(mdev, n) _sub_unacked(mdev, n, __FUNCTION__, __LINE__)
204049559d87SPhilipp Reisner static inline void _sub_unacked(struct drbd_conf *mdev, int n, const char *func, int line)
2041b411b363SPhilipp Reisner {
204249559d87SPhilipp Reisner 	atomic_sub(n, &mdev->unacked_cnt);
204349559d87SPhilipp Reisner 	ERR_IF_CNT_IS_NEGATIVE(unacked_cnt, func, line);
2044b411b363SPhilipp Reisner }
2045b411b363SPhilipp Reisner 
2046b411b363SPhilipp Reisner /**
2047b411b363SPhilipp Reisner  * get_ldev() - Increase the ref count on mdev->ldev. Returns 0 if there is no ldev
2048b411b363SPhilipp Reisner  * @M:		DRBD device.
2049b411b363SPhilipp Reisner  *
2050b411b363SPhilipp Reisner  * You have to call put_ldev() when finished working with mdev->ldev.
2051b411b363SPhilipp Reisner  */
2052b411b363SPhilipp Reisner #define get_ldev(M) __cond_lock(local, _get_ldev_if_state(M,D_INCONSISTENT))
2053b411b363SPhilipp Reisner #define get_ldev_if_state(M,MINS) __cond_lock(local, _get_ldev_if_state(M,MINS))
2054b411b363SPhilipp Reisner 
2055b411b363SPhilipp Reisner static inline void put_ldev(struct drbd_conf *mdev)
2056b411b363SPhilipp Reisner {
20571d7734a0SLars Ellenberg 	int i = atomic_dec_return(&mdev->local_cnt);
20589a0d9d03SLars Ellenberg 
20599a0d9d03SLars Ellenberg 	/* This may be called from some endio handler,
20609a0d9d03SLars Ellenberg 	 * so we must not sleep here. */
20619a0d9d03SLars Ellenberg 
2062b411b363SPhilipp Reisner 	__release(local);
20631d7734a0SLars Ellenberg 	D_ASSERT(i >= 0);
2064e9e6f3ecSLars Ellenberg 	if (i == 0) {
206582f59cc6SLars Ellenberg 		if (mdev->state.disk == D_DISKLESS)
206682f59cc6SLars Ellenberg 			/* even internal references gone, safe to destroy */
206782f59cc6SLars Ellenberg 			drbd_ldev_destroy(mdev);
20689114d795SLars Ellenberg 		if (mdev->state.disk == D_FAILED) {
206982f59cc6SLars Ellenberg 			/* all application IO references gone. */
20709114d795SLars Ellenberg 			if (!test_and_set_bit(GO_DISKLESS, &mdev->flags))
20719114d795SLars Ellenberg 				drbd_queue_work(&mdev->tconn->sender_work, &mdev->go_diskless);
20729114d795SLars Ellenberg 		}
2073b411b363SPhilipp Reisner 		wake_up(&mdev->misc_wait);
2074b411b363SPhilipp Reisner 	}
2075e9e6f3ecSLars Ellenberg }
2076b411b363SPhilipp Reisner 
2077b411b363SPhilipp Reisner #ifndef __CHECKER__
2078b411b363SPhilipp Reisner static inline int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
2079b411b363SPhilipp Reisner {
2080b411b363SPhilipp Reisner 	int io_allowed;
2081b411b363SPhilipp Reisner 
208282f59cc6SLars Ellenberg 	/* never get a reference while D_DISKLESS */
208382f59cc6SLars Ellenberg 	if (mdev->state.disk == D_DISKLESS)
208482f59cc6SLars Ellenberg 		return 0;
208582f59cc6SLars Ellenberg 
2086b411b363SPhilipp Reisner 	atomic_inc(&mdev->local_cnt);
2087b411b363SPhilipp Reisner 	io_allowed = (mdev->state.disk >= mins);
2088b411b363SPhilipp Reisner 	if (!io_allowed)
2089b411b363SPhilipp Reisner 		put_ldev(mdev);
2090b411b363SPhilipp Reisner 	return io_allowed;
2091b411b363SPhilipp Reisner }
2092b411b363SPhilipp Reisner #else
2093b411b363SPhilipp Reisner extern int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins);
2094b411b363SPhilipp Reisner #endif
2095b411b363SPhilipp Reisner 
2096b411b363SPhilipp Reisner /* you must have an "get_ldev" reference */
2097b411b363SPhilipp Reisner static inline void drbd_get_syncer_progress(struct drbd_conf *mdev,
2098b411b363SPhilipp Reisner 		unsigned long *bits_left, unsigned int *per_mil_done)
2099b411b363SPhilipp Reisner {
21004b0715f0SLars Ellenberg 	/* this is to break it at compile time when we change that, in case we
21014b0715f0SLars Ellenberg 	 * want to support more than (1<<32) bits on a 32bit arch. */
2102b411b363SPhilipp Reisner 	typecheck(unsigned long, mdev->rs_total);
2103b411b363SPhilipp Reisner 
2104b411b363SPhilipp Reisner 	/* note: both rs_total and rs_left are in bits, i.e. in
2105b411b363SPhilipp Reisner 	 * units of BM_BLOCK_SIZE.
2106b411b363SPhilipp Reisner 	 * for the percentage, we don't care. */
2107b411b363SPhilipp Reisner 
2108439d5953SLars Ellenberg 	if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2109439d5953SLars Ellenberg 		*bits_left = mdev->ov_left;
2110439d5953SLars Ellenberg 	else
2111b411b363SPhilipp Reisner 		*bits_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
2112b411b363SPhilipp Reisner 	/* >> 10 to prevent overflow,
2113b411b363SPhilipp Reisner 	 * +1 to prevent division by zero */
2114b411b363SPhilipp Reisner 	if (*bits_left > mdev->rs_total) {
2115b411b363SPhilipp Reisner 		/* doh. maybe a logic bug somewhere.
2116b411b363SPhilipp Reisner 		 * may also be just a race condition
2117b411b363SPhilipp Reisner 		 * between this and a disconnect during sync.
2118b411b363SPhilipp Reisner 		 * for now, just prevent in-kernel buffer overflow.
2119b411b363SPhilipp Reisner 		 */
2120b411b363SPhilipp Reisner 		smp_rmb();
2121b411b363SPhilipp Reisner 		dev_warn(DEV, "cs:%s rs_left=%lu > rs_total=%lu (rs_failed %lu)\n",
2122b411b363SPhilipp Reisner 				drbd_conn_str(mdev->state.conn),
2123b411b363SPhilipp Reisner 				*bits_left, mdev->rs_total, mdev->rs_failed);
2124b411b363SPhilipp Reisner 		*per_mil_done = 0;
2125b411b363SPhilipp Reisner 	} else {
21264b0715f0SLars Ellenberg 		/* Make sure the division happens in long context.
21274b0715f0SLars Ellenberg 		 * We allow up to one petabyte storage right now,
21284b0715f0SLars Ellenberg 		 * at a granularity of 4k per bit that is 2**38 bits.
21294b0715f0SLars Ellenberg 		 * After shift right and multiplication by 1000,
21304b0715f0SLars Ellenberg 		 * this should still fit easily into a 32bit long,
21314b0715f0SLars Ellenberg 		 * so we don't need a 64bit division on 32bit arch.
21324b0715f0SLars Ellenberg 		 * Note: currently we don't support such large bitmaps on 32bit
21334b0715f0SLars Ellenberg 		 * arch anyways, but no harm done to be prepared for it here.
21344b0715f0SLars Ellenberg 		 */
21355f138ce0SDavid Howells 		unsigned int shift = mdev->rs_total > UINT_MAX ? 16 : 10;
21364b0715f0SLars Ellenberg 		unsigned long left = *bits_left >> shift;
21374b0715f0SLars Ellenberg 		unsigned long total = 1UL + (mdev->rs_total >> shift);
21384b0715f0SLars Ellenberg 		unsigned long tmp = 1000UL - left * 1000UL/total;
2139b411b363SPhilipp Reisner 		*per_mil_done = tmp;
2140b411b363SPhilipp Reisner 	}
2141b411b363SPhilipp Reisner }
2142b411b363SPhilipp Reisner 
2143b411b363SPhilipp Reisner 
2144b411b363SPhilipp Reisner /* this throttles on-the-fly application requests
2145b411b363SPhilipp Reisner  * according to max_buffers settings;
2146b411b363SPhilipp Reisner  * maybe re-implement using semaphores? */
2147b411b363SPhilipp Reisner static inline int drbd_get_max_buffers(struct drbd_conf *mdev)
2148b411b363SPhilipp Reisner {
214944ed167dSPhilipp Reisner 	struct net_conf *nc;
215044ed167dSPhilipp Reisner 	int mxb;
215144ed167dSPhilipp Reisner 
215244ed167dSPhilipp Reisner 	rcu_read_lock();
215344ed167dSPhilipp Reisner 	nc = rcu_dereference(mdev->tconn->net_conf);
215444ed167dSPhilipp Reisner 	mxb = nc ? nc->max_buffers : 1000000;  /* arbitrary limit on open requests */
215544ed167dSPhilipp Reisner 	rcu_read_unlock();
215644ed167dSPhilipp Reisner 
2157b411b363SPhilipp Reisner 	return mxb;
2158b411b363SPhilipp Reisner }
2159b411b363SPhilipp Reisner 
21603719094eSPhilipp Reisner static inline int drbd_state_is_stable(struct drbd_conf *mdev)
2161b411b363SPhilipp Reisner {
2162da9fbc27SPhilipp Reisner 	union drbd_dev_state s = mdev->state;
2163b411b363SPhilipp Reisner 
2164b411b363SPhilipp Reisner 	/* DO NOT add a default clause, we want the compiler to warn us
2165b411b363SPhilipp Reisner 	 * for any newly introduced state we may have forgotten to add here */
2166b411b363SPhilipp Reisner 
2167b411b363SPhilipp Reisner 	switch ((enum drbd_conns)s.conn) {
2168b411b363SPhilipp Reisner 	/* new io only accepted when there is no connection, ... */
2169b411b363SPhilipp Reisner 	case C_STANDALONE:
2170b411b363SPhilipp Reisner 	case C_WF_CONNECTION:
2171b411b363SPhilipp Reisner 	/* ... or there is a well established connection. */
2172b411b363SPhilipp Reisner 	case C_CONNECTED:
2173b411b363SPhilipp Reisner 	case C_SYNC_SOURCE:
2174b411b363SPhilipp Reisner 	case C_SYNC_TARGET:
2175b411b363SPhilipp Reisner 	case C_VERIFY_S:
2176b411b363SPhilipp Reisner 	case C_VERIFY_T:
2177b411b363SPhilipp Reisner 	case C_PAUSED_SYNC_S:
2178b411b363SPhilipp Reisner 	case C_PAUSED_SYNC_T:
217967531718SPhilipp Reisner 	case C_AHEAD:
218067531718SPhilipp Reisner 	case C_BEHIND:
21813719094eSPhilipp Reisner 		/* transitional states, IO allowed */
2182b411b363SPhilipp Reisner 	case C_DISCONNECTING:
2183b411b363SPhilipp Reisner 	case C_UNCONNECTED:
2184b411b363SPhilipp Reisner 	case C_TIMEOUT:
2185b411b363SPhilipp Reisner 	case C_BROKEN_PIPE:
2186b411b363SPhilipp Reisner 	case C_NETWORK_FAILURE:
2187b411b363SPhilipp Reisner 	case C_PROTOCOL_ERROR:
2188b411b363SPhilipp Reisner 	case C_TEAR_DOWN:
2189b411b363SPhilipp Reisner 	case C_WF_REPORT_PARAMS:
2190b411b363SPhilipp Reisner 	case C_STARTING_SYNC_S:
2191b411b363SPhilipp Reisner 	case C_STARTING_SYNC_T:
21923719094eSPhilipp Reisner 		break;
21933719094eSPhilipp Reisner 
21943719094eSPhilipp Reisner 		/* Allow IO in BM exchange states with new protocols */
2195b411b363SPhilipp Reisner 	case C_WF_BITMAP_S:
219631890f4aSPhilipp Reisner 		if (mdev->tconn->agreed_pro_version < 96)
21973719094eSPhilipp Reisner 			return 0;
21983719094eSPhilipp Reisner 		break;
21993719094eSPhilipp Reisner 
22003719094eSPhilipp Reisner 		/* no new io accepted in these states */
2201b411b363SPhilipp Reisner 	case C_WF_BITMAP_T:
2202b411b363SPhilipp Reisner 	case C_WF_SYNC_UUID:
2203b411b363SPhilipp Reisner 	case C_MASK:
2204b411b363SPhilipp Reisner 		/* not "stable" */
2205b411b363SPhilipp Reisner 		return 0;
2206b411b363SPhilipp Reisner 	}
2207b411b363SPhilipp Reisner 
2208b411b363SPhilipp Reisner 	switch ((enum drbd_disk_state)s.disk) {
2209b411b363SPhilipp Reisner 	case D_DISKLESS:
2210b411b363SPhilipp Reisner 	case D_INCONSISTENT:
2211b411b363SPhilipp Reisner 	case D_OUTDATED:
2212b411b363SPhilipp Reisner 	case D_CONSISTENT:
2213b411b363SPhilipp Reisner 	case D_UP_TO_DATE:
22145ca1de03SPhilipp Reisner 	case D_FAILED:
2215b411b363SPhilipp Reisner 		/* disk state is stable as well. */
2216b411b363SPhilipp Reisner 		break;
2217b411b363SPhilipp Reisner 
2218d942ae44SPhilipp Reisner 	/* no new io accepted during transitional states */
2219b411b363SPhilipp Reisner 	case D_ATTACHING:
2220b411b363SPhilipp Reisner 	case D_NEGOTIATING:
2221b411b363SPhilipp Reisner 	case D_UNKNOWN:
2222b411b363SPhilipp Reisner 	case D_MASK:
2223b411b363SPhilipp Reisner 		/* not "stable" */
2224b411b363SPhilipp Reisner 		return 0;
2225b411b363SPhilipp Reisner 	}
2226b411b363SPhilipp Reisner 
2227b411b363SPhilipp Reisner 	return 1;
2228b411b363SPhilipp Reisner }
2229b411b363SPhilipp Reisner 
22302aebfabbSPhilipp Reisner static inline int drbd_suspended(struct drbd_conf *mdev)
2231fb22c402SPhilipp Reisner {
22328e0af25fSPhilipp Reisner 	struct drbd_tconn *tconn = mdev->tconn;
22338e0af25fSPhilipp Reisner 
22348e0af25fSPhilipp Reisner 	return tconn->susp || tconn->susp_fen || tconn->susp_nod;
2235fb22c402SPhilipp Reisner }
2236fb22c402SPhilipp Reisner 
22371b881ef7SAndreas Gruenbacher static inline bool may_inc_ap_bio(struct drbd_conf *mdev)
2238b411b363SPhilipp Reisner {
2239b411b363SPhilipp Reisner 	int mxb = drbd_get_max_buffers(mdev);
2240b411b363SPhilipp Reisner 
22412aebfabbSPhilipp Reisner 	if (drbd_suspended(mdev))
22421b881ef7SAndreas Gruenbacher 		return false;
2243b411b363SPhilipp Reisner 	if (test_bit(SUSPEND_IO, &mdev->flags))
22441b881ef7SAndreas Gruenbacher 		return false;
2245b411b363SPhilipp Reisner 
2246b411b363SPhilipp Reisner 	/* to avoid potential deadlock or bitmap corruption,
2247b411b363SPhilipp Reisner 	 * in various places, we only allow new application io
2248b411b363SPhilipp Reisner 	 * to start during "stable" states. */
2249b411b363SPhilipp Reisner 
2250b411b363SPhilipp Reisner 	/* no new io accepted when attaching or detaching the disk */
22513719094eSPhilipp Reisner 	if (!drbd_state_is_stable(mdev))
22521b881ef7SAndreas Gruenbacher 		return false;
2253b411b363SPhilipp Reisner 
2254b411b363SPhilipp Reisner 	/* since some older kernels don't have atomic_add_unless,
2255b411b363SPhilipp Reisner 	 * and we are within the spinlock anyways, we have this workaround.  */
2256b411b363SPhilipp Reisner 	if (atomic_read(&mdev->ap_bio_cnt) > mxb)
22571b881ef7SAndreas Gruenbacher 		return false;
2258b411b363SPhilipp Reisner 	if (test_bit(BITMAP_IO, &mdev->flags))
22591b881ef7SAndreas Gruenbacher 		return false;
22601b881ef7SAndreas Gruenbacher 	return true;
2261b411b363SPhilipp Reisner }
2262b411b363SPhilipp Reisner 
226323361cf3SLars Ellenberg static inline bool inc_ap_bio_cond(struct drbd_conf *mdev)
22648869d683SPhilipp Reisner {
22651b881ef7SAndreas Gruenbacher 	bool rv = false;
22668869d683SPhilipp Reisner 
226787eeee41SPhilipp Reisner 	spin_lock_irq(&mdev->tconn->req_lock);
22681b881ef7SAndreas Gruenbacher 	rv = may_inc_ap_bio(mdev);
22698869d683SPhilipp Reisner 	if (rv)
227023361cf3SLars Ellenberg 		atomic_inc(&mdev->ap_bio_cnt);
227187eeee41SPhilipp Reisner 	spin_unlock_irq(&mdev->tconn->req_lock);
22728869d683SPhilipp Reisner 
22738869d683SPhilipp Reisner 	return rv;
22748869d683SPhilipp Reisner }
22758869d683SPhilipp Reisner 
227623361cf3SLars Ellenberg static inline void inc_ap_bio(struct drbd_conf *mdev)
2277b411b363SPhilipp Reisner {
2278b411b363SPhilipp Reisner 	/* we wait here
2279b411b363SPhilipp Reisner 	 *    as long as the device is suspended
2280b411b363SPhilipp Reisner 	 *    until the bitmap is no longer on the fly during connection
2281d942ae44SPhilipp Reisner 	 *    handshake as long as we would exceed the max_buffer limit.
2282b411b363SPhilipp Reisner 	 *
2283b411b363SPhilipp Reisner 	 * to avoid races with the reconnect code,
2284b411b363SPhilipp Reisner 	 * we need to atomic_inc within the spinlock. */
2285b411b363SPhilipp Reisner 
228623361cf3SLars Ellenberg 	wait_event(mdev->misc_wait, inc_ap_bio_cond(mdev));
2287b411b363SPhilipp Reisner }
2288b411b363SPhilipp Reisner 
2289b411b363SPhilipp Reisner static inline void dec_ap_bio(struct drbd_conf *mdev)
2290b411b363SPhilipp Reisner {
2291b411b363SPhilipp Reisner 	int mxb = drbd_get_max_buffers(mdev);
2292b411b363SPhilipp Reisner 	int ap_bio = atomic_dec_return(&mdev->ap_bio_cnt);
2293b411b363SPhilipp Reisner 
2294b411b363SPhilipp Reisner 	D_ASSERT(ap_bio >= 0);
22957ee1fb93SLars Ellenberg 
2296970fbde1SLars Ellenberg 	if (ap_bio == 0 && test_bit(BITMAP_IO, &mdev->flags)) {
2297970fbde1SLars Ellenberg 		if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
2298970fbde1SLars Ellenberg 			drbd_queue_work(&mdev->tconn->sender_work, &mdev->bm_io_work.w);
22997ee1fb93SLars Ellenberg 	}
23007ee1fb93SLars Ellenberg 
2301b411b363SPhilipp Reisner 	/* this currently does wake_up for every dec_ap_bio!
2302b411b363SPhilipp Reisner 	 * maybe rather introduce some type of hysteresis?
2303b411b363SPhilipp Reisner 	 * e.g. (ap_bio == mxb/2 || ap_bio == 0) ? */
2304b411b363SPhilipp Reisner 	if (ap_bio < mxb)
2305b411b363SPhilipp Reisner 		wake_up(&mdev->misc_wait);
2306b411b363SPhilipp Reisner }
2307b411b363SPhilipp Reisner 
230858ffa580SLars Ellenberg static inline bool verify_can_do_stop_sector(struct drbd_conf *mdev)
230958ffa580SLars Ellenberg {
231058ffa580SLars Ellenberg 	return mdev->tconn->agreed_pro_version >= 97 &&
231158ffa580SLars Ellenberg 		mdev->tconn->agreed_pro_version != 100;
231258ffa580SLars Ellenberg }
231358ffa580SLars Ellenberg 
231462b0da3aSLars Ellenberg static inline int drbd_set_ed_uuid(struct drbd_conf *mdev, u64 val)
2315b411b363SPhilipp Reisner {
231662b0da3aSLars Ellenberg 	int changed = mdev->ed_uuid != val;
2317b411b363SPhilipp Reisner 	mdev->ed_uuid = val;
231862b0da3aSLars Ellenberg 	return changed;
2319b411b363SPhilipp Reisner }
2320b411b363SPhilipp Reisner 
2321b411b363SPhilipp Reisner static inline int drbd_queue_order_type(struct drbd_conf *mdev)
2322b411b363SPhilipp Reisner {
2323b411b363SPhilipp Reisner 	/* sorry, we currently have no working implementation
2324b411b363SPhilipp Reisner 	 * of distributed TCQ stuff */
2325b411b363SPhilipp Reisner #ifndef QUEUE_ORDERED_NONE
2326b411b363SPhilipp Reisner #define QUEUE_ORDERED_NONE 0
2327b411b363SPhilipp Reisner #endif
2328b411b363SPhilipp Reisner 	return QUEUE_ORDERED_NONE;
2329b411b363SPhilipp Reisner }
2330b411b363SPhilipp Reisner 
2331b411b363SPhilipp Reisner static inline void drbd_md_flush(struct drbd_conf *mdev)
2332b411b363SPhilipp Reisner {
2333b411b363SPhilipp Reisner 	int r;
2334b411b363SPhilipp Reisner 
2335fd0017c1SPhilipp Reisner 	if (mdev->ldev == NULL) {
2336fd0017c1SPhilipp Reisner 		dev_warn(DEV, "mdev->ldev == NULL in drbd_md_flush\n");
2337fd0017c1SPhilipp Reisner 		return;
2338fd0017c1SPhilipp Reisner 	}
2339fd0017c1SPhilipp Reisner 
2340a8a4e51eSPhilipp Reisner 	if (test_bit(MD_NO_FUA, &mdev->flags))
2341b411b363SPhilipp Reisner 		return;
2342b411b363SPhilipp Reisner 
23438b45a5c8SLars Ellenberg 	r = blkdev_issue_flush(mdev->ldev->md_bdev, GFP_NOIO, NULL);
2344b411b363SPhilipp Reisner 	if (r) {
2345a8a4e51eSPhilipp Reisner 		set_bit(MD_NO_FUA, &mdev->flags);
2346b411b363SPhilipp Reisner 		dev_err(DEV, "meta data flush failed with status %d, disabling md-flushes\n", r);
2347b411b363SPhilipp Reisner 	}
2348b411b363SPhilipp Reisner }
2349b411b363SPhilipp Reisner 
2350b411b363SPhilipp Reisner #endif
2351