blob: c7504579c46ee73e10082a7eea8690b319d6d22e [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_int.h
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24*/
25
26#ifndef _DRBD_INT_H
27#define _DRBD_INT_H
28
29#include <linux/compiler.h>
30#include <linux/types.h>
31#include <linux/version.h>
32#include <linux/list.h>
33#include <linux/sched.h>
34#include <linux/bitops.h>
35#include <linux/slab.h>
36#include <linux/crypto.h>
Randy Dunlap132cc532009-10-07 19:26:00 +020037#include <linux/ratelimit.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070038#include <linux/tcp.h>
39#include <linux/mutex.h>
40#include <linux/major.h>
41#include <linux/blkdev.h>
42#include <linux/genhd.h>
43#include <net/tcp.h>
44#include <linux/lru_cache.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040045#include <linux/prefetch.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070046
47#ifdef __CHECKER__
48# define __protected_by(x) __attribute__((require_context(x,1,999,"rdwr")))
49# define __protected_read_by(x) __attribute__((require_context(x,1,999,"read")))
50# define __protected_write_by(x) __attribute__((require_context(x,1,999,"write")))
51# define __must_hold(x) __attribute__((context(x,1,1), require_context(x,1,999,"call")))
52#else
53# define __protected_by(x)
54# define __protected_read_by(x)
55# define __protected_write_by(x)
56# define __must_hold(x)
57#endif
58
59#define __no_warn(lock, stmt) do { __acquire(lock); stmt; __release(lock); } while (0)
60
61/* module parameter, defined in drbd_main.c */
62extern unsigned int minor_count;
63extern int disable_sendpage;
64extern int allow_oos;
65extern unsigned int cn_idx;
66
67#ifdef CONFIG_DRBD_FAULT_INJECTION
68extern int enable_faults;
69extern int fault_rate;
70extern int fault_devs;
71#endif
72
73extern char usermode_helper[];
74
75
Philipp Reisnerb411b362009-09-25 16:07:19 -070076/* I don't remember why XCPU ...
77 * This is used to wake the asender,
78 * and to interrupt sending the sending task
79 * on disconnect.
80 */
81#define DRBD_SIG SIGXCPU
82
83/* This is used to stop/restart our threads.
84 * Cannot use SIGTERM nor SIGKILL, since these
85 * are sent out by init on runlevel changes
86 * I choose SIGHUP for now.
87 */
88#define DRBD_SIGKILL SIGHUP
89
Philipp Reisnerb411b362009-09-25 16:07:19 -070090#define ID_IN_SYNC (4711ULL)
91#define ID_OUT_OF_SYNC (4712ULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -070092#define ID_SYNCER (-1ULL)
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +010093
Philipp Reisner4a23f262011-01-11 17:42:17 +010094#define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -070095
96struct drbd_conf;
Philipp Reisner21114382011-01-19 12:26:59 +010097struct drbd_tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -070098
99
100/* to shorten dev_warn(DEV, "msg"); and relatives statements */
101#define DEV (disk_to_dev(mdev->vdisk))
102
103#define D_ASSERT(exp) if (!(exp)) \
104 dev_err(DEV, "ASSERT( " #exp " ) in %s:%d\n", __FILE__, __LINE__)
105
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100106/**
107 * expect - Make an assertion
108 *
109 * Unlike the assert macro, this macro returns a boolean result.
110 */
111#define expect(exp) ({ \
112 bool _bool = (exp); \
113 if (!_bool) \
114 dev_err(DEV, "ASSERTION %s FAILED in %s\n", \
115 #exp, __func__); \
116 _bool; \
117 })
Philipp Reisnerb411b362009-09-25 16:07:19 -0700118
119/* Defines to control fault insertion */
120enum {
121 DRBD_FAULT_MD_WR = 0, /* meta data write */
122 DRBD_FAULT_MD_RD = 1, /* read */
123 DRBD_FAULT_RS_WR = 2, /* resync */
124 DRBD_FAULT_RS_RD = 3,
125 DRBD_FAULT_DT_WR = 4, /* data */
126 DRBD_FAULT_DT_RD = 5,
127 DRBD_FAULT_DT_RA = 6, /* data read ahead */
128 DRBD_FAULT_BM_ALLOC = 7, /* bitmap allocation */
129 DRBD_FAULT_AL_EE = 8, /* alloc ee */
Philipp Reisner6b4388a2010-04-26 14:11:45 +0200130 DRBD_FAULT_RECEIVE = 9, /* Changes some bytes upon receiving a [rs]data block */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700131
132 DRBD_FAULT_MAX,
133};
134
Philipp Reisnerb411b362009-09-25 16:07:19 -0700135extern unsigned int
136_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100137
Philipp Reisnerb411b362009-09-25 16:07:19 -0700138static inline int
139drbd_insert_fault(struct drbd_conf *mdev, unsigned int type) {
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100140#ifdef CONFIG_DRBD_FAULT_INJECTION
Philipp Reisnerb411b362009-09-25 16:07:19 -0700141 return fault_rate &&
142 (enable_faults & (1<<type)) &&
143 _drbd_insert_fault(mdev, type);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700144#else
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100145 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700146#endif
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100147}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700148
149/* integer division, round _UP_ to the next integer */
150#define div_ceil(A, B) ((A)/(B) + ((A)%(B) ? 1 : 0))
151/* usual integer division */
152#define div_floor(A, B) ((A)/(B))
153
154/* drbd_meta-data.c (still in drbd_main.c) */
155/* 4th incarnation of the disk layout. */
156#define DRBD_MD_MAGIC (DRBD_MAGIC+4)
157
158extern struct drbd_conf **minor_table;
159extern struct ratelimit_state drbd_ratelimit_state;
160
161/* on the wire */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100162enum drbd_packet {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700163 /* receiver (data socket) */
164 P_DATA = 0x00,
165 P_DATA_REPLY = 0x01, /* Response to P_DATA_REQUEST */
166 P_RS_DATA_REPLY = 0x02, /* Response to P_RS_DATA_REQUEST */
167 P_BARRIER = 0x03,
168 P_BITMAP = 0x04,
169 P_BECOME_SYNC_TARGET = 0x05,
170 P_BECOME_SYNC_SOURCE = 0x06,
171 P_UNPLUG_REMOTE = 0x07, /* Used at various times to hint the peer */
172 P_DATA_REQUEST = 0x08, /* Used to ask for a data block */
173 P_RS_DATA_REQUEST = 0x09, /* Used to ask for a data block for resync */
174 P_SYNC_PARAM = 0x0a,
175 P_PROTOCOL = 0x0b,
176 P_UUIDS = 0x0c,
177 P_SIZES = 0x0d,
178 P_STATE = 0x0e,
179 P_SYNC_UUID = 0x0f,
180 P_AUTH_CHALLENGE = 0x10,
181 P_AUTH_RESPONSE = 0x11,
182 P_STATE_CHG_REQ = 0x12,
183
184 /* asender (meta socket */
185 P_PING = 0x13,
186 P_PING_ACK = 0x14,
187 P_RECV_ACK = 0x15, /* Used in protocol B */
188 P_WRITE_ACK = 0x16, /* Used in protocol C */
189 P_RS_WRITE_ACK = 0x17, /* Is a P_WRITE_ACK, additionally call set_in_sync(). */
190 P_DISCARD_ACK = 0x18, /* Used in proto C, two-primaries conflict detection */
191 P_NEG_ACK = 0x19, /* Sent if local disk is unusable */
192 P_NEG_DREPLY = 0x1a, /* Local disk is broken... */
193 P_NEG_RS_DREPLY = 0x1b, /* Local disk is broken... */
194 P_BARRIER_ACK = 0x1c,
195 P_STATE_CHG_REPLY = 0x1d,
196
197 /* "new" commands, no longer fitting into the ordering scheme above */
198
199 P_OV_REQUEST = 0x1e, /* data socket */
200 P_OV_REPLY = 0x1f,
201 P_OV_RESULT = 0x20, /* meta socket */
202 P_CSUM_RS_REQUEST = 0x21, /* data socket */
203 P_RS_IS_IN_SYNC = 0x22, /* meta socket */
204 P_SYNC_PARAM89 = 0x23, /* data socket, protocol version 89 replacement for P_SYNC_PARAM */
205 P_COMPRESSED_BITMAP = 0x24, /* compressed or otherwise encoded bitmap transfer */
Philipp Reisner0ced55a2010-04-30 15:26:20 +0200206 /* P_CKPT_FENCE_REQ = 0x25, * currently reserved for protocol D */
207 /* P_CKPT_DISABLE_REQ = 0x26, * currently reserved for protocol D */
208 P_DELAY_PROBE = 0x27, /* is used on BOTH sockets */
Philipp Reisner73a01a12010-10-27 14:33:00 +0200209 P_OUT_OF_SYNC = 0x28, /* Mark as out of sync (Outrunning), data socket */
Philipp Reisnerd612d302010-12-27 10:53:28 +0100210 P_RS_CANCEL = 0x29, /* meta: Used to cancel RS_DATA_REQUEST packet by SyncSource */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700211
Philipp Reisnerd612d302010-12-27 10:53:28 +0100212 P_MAX_CMD = 0x2A,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700213 P_MAY_IGNORE = 0x100, /* Flag to test if (cmd > P_MAY_IGNORE) ... */
214 P_MAX_OPT_CMD = 0x101,
215
216 /* special command ids for handshake */
217
218 P_HAND_SHAKE_M = 0xfff1, /* First Packet on the MetaSock */
219 P_HAND_SHAKE_S = 0xfff2, /* First Packet on the Socket */
220
221 P_HAND_SHAKE = 0xfffe /* FIXED for the next century! */
222};
223
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100224extern const char *cmdname(enum drbd_packet cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700225
226/* for sending/receiving the bitmap,
227 * possibly in some encoding scheme */
228struct bm_xfer_ctx {
229 /* "const"
230 * stores total bits and long words
231 * of the bitmap, so we don't need to
232 * call the accessor functions over and again. */
233 unsigned long bm_bits;
234 unsigned long bm_words;
235 /* during xfer, current position within the bitmap */
236 unsigned long bit_offset;
237 unsigned long word_offset;
238
239 /* statistics; index: (h->command == P_BITMAP) */
240 unsigned packets[2];
241 unsigned bytes[2];
242};
243
244extern void INFO_bm_xfer_stats(struct drbd_conf *mdev,
245 const char *direction, struct bm_xfer_ctx *c);
246
247static inline void bm_xfer_ctx_bit_to_word_offset(struct bm_xfer_ctx *c)
248{
249 /* word_offset counts "native long words" (32 or 64 bit),
250 * aligned at 64 bit.
251 * Encoded packet may end at an unaligned bit offset.
252 * In case a fallback clear text packet is transmitted in
253 * between, we adjust this offset back to the last 64bit
254 * aligned "native long word", which makes coding and decoding
255 * the plain text bitmap much more convenient. */
256#if BITS_PER_LONG == 64
257 c->word_offset = c->bit_offset >> 6;
258#elif BITS_PER_LONG == 32
259 c->word_offset = c->bit_offset >> 5;
260 c->word_offset &= ~(1UL);
261#else
262# error "unsupported BITS_PER_LONG"
263#endif
264}
265
266#ifndef __packed
267#define __packed __attribute__((packed))
268#endif
269
270/* This is the layout for a packet on the wire.
271 * The byteorder is the network byte order.
272 * (except block_id and barrier fields.
273 * these are pointers to local structs
274 * and have no relevance for the partner,
275 * which just echoes them as received.)
276 *
277 * NOTE that the payload starts at a long aligned offset,
278 * regardless of 32 or 64 bit arch!
279 */
Philipp Reisner0b70a132010-08-20 13:36:10 +0200280struct p_header80 {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700281 u32 magic;
282 u16 command;
283 u16 length; /* bytes of data after this header */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700284} __packed;
Philipp Reisner0b70a132010-08-20 13:36:10 +0200285
286/* Header for big packets, Used for data packets exceeding 64kB */
287struct p_header95 {
288 u16 magic; /* use DRBD_MAGIC_BIG here */
289 u16 command;
Philipp Reisner00b42532010-10-05 11:19:39 +0200290 u32 length; /* Use only 24 bits of that. Ignore the highest 8 bit. */
Philipp Reisner0b70a132010-08-20 13:36:10 +0200291} __packed;
292
Philipp Reisnerc0129492011-01-19 16:58:16 +0100293struct p_header {
294 union {
295 struct p_header80 h80;
296 struct p_header95 h95;
297 };
298 u8 payload[0];
Philipp Reisner0b70a132010-08-20 13:36:10 +0200299};
Philipp Reisnerb411b362009-09-25 16:07:19 -0700300
301/*
302 * short commands, packets without payload, plain p_header:
303 * P_PING
304 * P_PING_ACK
305 * P_BECOME_SYNC_TARGET
306 * P_BECOME_SYNC_SOURCE
307 * P_UNPLUG_REMOTE
308 */
309
310/*
311 * commands with out-of-struct payload:
312 * P_BITMAP (no additional fields)
313 * P_DATA, P_DATA_REPLY (see p_data)
314 * P_COMPRESSED_BITMAP (see receive_compressed_bitmap)
315 */
316
317/* these defines must not be changed without changing the protocol version */
Philipp Reisner76d2e7e2010-08-25 11:58:05 +0200318#define DP_HARDBARRIER 1 /* depricated */
319#define DP_RW_SYNC 2 /* equals REQ_SYNC */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700320#define DP_MAY_SET_IN_SYNC 4
Jens Axboe721a9602011-03-09 11:56:30 +0100321#define DP_UNPLUG 8 /* not used anymore */
Philipp Reisner76d2e7e2010-08-25 11:58:05 +0200322#define DP_FUA 16 /* equals REQ_FUA */
323#define DP_FLUSH 32 /* equals REQ_FLUSH */
324#define DP_DISCARD 64 /* equals REQ_DISCARD */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325
326struct p_data {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100327 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700328 u64 sector; /* 64 bits sector number */
329 u64 block_id; /* to identify the request in protocol B&C */
330 u32 seq_num;
331 u32 dp_flags;
332} __packed;
333
334/*
335 * commands which share a struct:
336 * p_block_ack:
337 * P_RECV_ACK (proto B), P_WRITE_ACK (proto C),
338 * P_DISCARD_ACK (proto C, two-primaries conflict detection)
339 * p_block_req:
340 * P_DATA_REQUEST, P_RS_DATA_REQUEST
341 */
342struct p_block_ack {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100343 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700344 u64 sector;
345 u64 block_id;
346 u32 blksize;
347 u32 seq_num;
348} __packed;
349
350
351struct p_block_req {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100352 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700353 u64 sector;
354 u64 block_id;
355 u32 blksize;
356 u32 pad; /* to multiple of 8 Byte */
357} __packed;
358
359/*
360 * commands with their own struct for additional fields:
361 * P_HAND_SHAKE
362 * P_BARRIER
363 * P_BARRIER_ACK
364 * P_SYNC_PARAM
365 * ReportParams
366 */
367
368struct p_handshake {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100369 struct p_header head; /* Note: You must always use a h80 here */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700370 u32 protocol_min;
371 u32 feature_flags;
372 u32 protocol_max;
373
374 /* should be more than enough for future enhancements
375 * for now, feature_flags and the reserverd array shall be zero.
376 */
377
378 u32 _pad;
379 u64 reserverd[7];
380} __packed;
381/* 80 bytes, FIXED for the next century */
382
383struct p_barrier {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100384 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700385 u32 barrier; /* barrier number _handle_ only */
386 u32 pad; /* to multiple of 8 Byte */
387} __packed;
388
389struct p_barrier_ack {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100390 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700391 u32 barrier;
392 u32 set_size;
393} __packed;
394
395struct p_rs_param {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100396 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700397 u32 rate;
398
399 /* Since protocol version 88 and higher. */
400 char verify_alg[0];
401} __packed;
402
403struct p_rs_param_89 {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100404 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700405 u32 rate;
406 /* protocol version 89: */
407 char verify_alg[SHARED_SECRET_MAX];
408 char csums_alg[SHARED_SECRET_MAX];
409} __packed;
410
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200411struct p_rs_param_95 {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100412 struct p_header head;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200413 u32 rate;
414 char verify_alg[SHARED_SECRET_MAX];
415 char csums_alg[SHARED_SECRET_MAX];
416 u32 c_plan_ahead;
417 u32 c_delay_target;
418 u32 c_fill_target;
419 u32 c_max_rate;
420} __packed;
421
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100422enum drbd_conn_flags {
423 CF_WANT_LOSE = 1,
424 CF_DRY_RUN = 2,
425};
426
Philipp Reisnerb411b362009-09-25 16:07:19 -0700427struct p_protocol {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100428 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 u32 protocol;
430 u32 after_sb_0p;
431 u32 after_sb_1p;
432 u32 after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100433 u32 conn_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 u32 two_primaries;
435
436 /* Since protocol version 87 and higher. */
437 char integrity_alg[0];
438
439} __packed;
440
441struct p_uuids {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100442 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700443 u64 uuid[UI_EXTENDED_SIZE];
444} __packed;
445
446struct p_rs_uuid {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100447 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700448 u64 uuid;
449} __packed;
450
451struct p_sizes {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100452 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453 u64 d_size; /* size of disk */
454 u64 u_size; /* user requested size */
455 u64 c_size; /* current exported size */
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100456 u32 max_bio_size; /* Maximal size of a BIO */
Philipp Reisnere89b5912010-03-24 17:11:33 +0100457 u16 queue_order_type; /* not yet implemented in DRBD*/
458 u16 dds_flags; /* use enum dds_flags here. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700459} __packed;
460
461struct p_state {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100462 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700463 u32 state;
464} __packed;
465
466struct p_req_state {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100467 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700468 u32 mask;
469 u32 val;
470} __packed;
471
472struct p_req_state_reply {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100473 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700474 u32 retcode;
475} __packed;
476
477struct p_drbd06_param {
478 u64 size;
479 u32 state;
480 u32 blksize;
481 u32 protocol;
482 u32 version;
483 u32 gen_cnt[5];
484 u32 bit_map_gen[5];
485} __packed;
486
487struct p_discard {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100488 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700489 u64 block_id;
490 u32 seq_num;
491 u32 pad;
492} __packed;
493
Philipp Reisner73a01a12010-10-27 14:33:00 +0200494struct p_block_desc {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100495 struct p_header head;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200496 u64 sector;
497 u32 blksize;
498 u32 pad; /* to multiple of 8 Byte */
499} __packed;
500
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501/* Valid values for the encoding field.
502 * Bump proto version when changing this. */
503enum drbd_bitmap_code {
504 /* RLE_VLI_Bytes = 0,
505 * and other bit variants had been defined during
506 * algorithm evaluation. */
507 RLE_VLI_Bits = 2,
508};
509
510struct p_compressed_bm {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100511 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700512 /* (encoding & 0x0f): actual encoding, see enum drbd_bitmap_code
513 * (encoding & 0x80): polarity (set/unset) of first runlength
514 * ((encoding >> 4) & 0x07): pad_bits, number of trailing zero bits
515 * used to pad up to head.length bytes
516 */
517 u8 encoding;
518
519 u8 code[0];
520} __packed;
521
Philipp Reisner0b70a132010-08-20 13:36:10 +0200522struct p_delay_probe93 {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100523 struct p_header head;
Philipp Reisner0b70a132010-08-20 13:36:10 +0200524 u32 seq_num; /* sequence number to match the two probe packets */
525 u32 offset; /* usecs the probe got sent after the reference time point */
Philipp Reisner0ced55a2010-04-30 15:26:20 +0200526} __packed;
527
Philipp Reisnerb411b362009-09-25 16:07:19 -0700528/* DCBP: Drbd Compressed Bitmap Packet ... */
529static inline enum drbd_bitmap_code
530DCBP_get_code(struct p_compressed_bm *p)
531{
532 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
533}
534
535static inline void
536DCBP_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
537{
538 BUG_ON(code & ~0xf);
539 p->encoding = (p->encoding & ~0xf) | code;
540}
541
542static inline int
543DCBP_get_start(struct p_compressed_bm *p)
544{
545 return (p->encoding & 0x80) != 0;
546}
547
548static inline void
549DCBP_set_start(struct p_compressed_bm *p, int set)
550{
551 p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
552}
553
554static inline int
555DCBP_get_pad_bits(struct p_compressed_bm *p)
556{
557 return (p->encoding >> 4) & 0x7;
558}
559
560static inline void
561DCBP_set_pad_bits(struct p_compressed_bm *p, int n)
562{
563 BUG_ON(n & ~0x7);
564 p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
565}
566
567/* one bitmap packet, including the p_header,
568 * should fit within one _architecture independend_ page.
569 * so we need to use the fixed size 4KiB page size
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300570 * most architectures have used for a long time.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700571 */
Philipp Reisnerc0129492011-01-19 16:58:16 +0100572#define BM_PACKET_PAYLOAD_BYTES (4096 - sizeof(struct p_header))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573#define BM_PACKET_WORDS (BM_PACKET_PAYLOAD_BYTES/sizeof(long))
574#define BM_PACKET_VLI_BYTES_MAX (4096 - sizeof(struct p_compressed_bm))
575#if (PAGE_SIZE < 4096)
576/* drbd_send_bitmap / receive_bitmap would break horribly */
577#error "PAGE_SIZE too small"
578#endif
579
580union p_polymorph {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100581 struct p_header header;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582 struct p_handshake handshake;
583 struct p_data data;
584 struct p_block_ack block_ack;
585 struct p_barrier barrier;
586 struct p_barrier_ack barrier_ack;
587 struct p_rs_param_89 rs_param_89;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200588 struct p_rs_param_95 rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589 struct p_protocol protocol;
590 struct p_sizes sizes;
591 struct p_uuids uuids;
592 struct p_state state;
593 struct p_req_state req_state;
594 struct p_req_state_reply req_state_reply;
595 struct p_block_req block_req;
Philipp Reisner02918be2010-08-20 14:35:10 +0200596 struct p_delay_probe93 delay_probe93;
597 struct p_rs_uuid rs_uuid;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200598 struct p_block_desc block_desc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700599} __packed;
600
601/**********************************************************************/
602enum drbd_thread_state {
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100603 NONE,
604 RUNNING,
605 EXITING,
606 RESTARTING
Philipp Reisnerb411b362009-09-25 16:07:19 -0700607};
608
609struct drbd_thread {
610 spinlock_t t_lock;
611 struct task_struct *task;
612 struct completion stop;
613 enum drbd_thread_state t_state;
614 int (*function) (struct drbd_thread *);
615 struct drbd_conf *mdev;
616 int reset_cpu_mask;
617};
618
619static inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
620{
621 /* THINK testing the t_state seems to be uncritical in all cases
622 * (but thread_{start,stop}), so we can read it *without* the lock.
623 * --lge */
624
625 smp_rmb();
626 return thi->t_state;
627}
628
Philipp Reisnerb411b362009-09-25 16:07:19 -0700629struct drbd_work;
630typedef int (*drbd_work_cb)(struct drbd_conf *, struct drbd_work *, int cancel);
631struct drbd_work {
632 struct list_head list;
633 drbd_work_cb cb;
634};
635
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100636#include "drbd_interval.h"
637
Philipp Reisnerb411b362009-09-25 16:07:19 -0700638struct drbd_request {
639 struct drbd_work w;
640 struct drbd_conf *mdev;
641
642 /* if local IO is not allowed, will be NULL.
643 * if local IO _is_ allowed, holds the locally submitted bio clone,
644 * or, after local IO completion, the ERR_PTR(error).
645 * see drbd_endio_pri(). */
646 struct bio *private_bio;
647
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100648 struct drbd_interval i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700649 unsigned int epoch; /* barrier_nr */
650
651 /* barrier_nr: used to check on "completion" whether this req was in
652 * the current epoch, and we therefore have to close it,
653 * starting a new epoch...
654 */
655
Philipp Reisnerb411b362009-09-25 16:07:19 -0700656 struct list_head tl_requests; /* ring list in the transfer log */
657 struct bio *master_bio; /* master bio pointer */
658 unsigned long rq_state; /* see comments above _req_mod() */
659 int seq_num;
660 unsigned long start_time;
661};
662
663struct drbd_tl_epoch {
664 struct drbd_work w;
665 struct list_head requests; /* requests before */
666 struct drbd_tl_epoch *next; /* pointer to the next barrier */
667 unsigned int br_number; /* the barriers identifier. */
Philipp Reisner7e602c02010-05-27 14:49:27 +0200668 int n_writes; /* number of requests attached before this barrier */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700669};
670
Philipp Reisnerb411b362009-09-25 16:07:19 -0700671struct drbd_epoch {
672 struct list_head list;
673 unsigned int barrier_nr;
674 atomic_t epoch_size; /* increased on every request added. */
675 atomic_t active; /* increased on every req. added, and dec on every finished. */
676 unsigned long flags;
677};
678
679/* drbd_epoch flag bits */
680enum {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700681 DE_HAVE_BARRIER_NUMBER,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700682};
683
684enum epoch_event {
685 EV_PUT,
686 EV_GOT_BARRIER_NR,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700687 EV_BECAME_LAST,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700688 EV_CLEANUP = 32, /* used as flag */
689};
690
Philipp Reisnerb411b362009-09-25 16:07:19 -0700691struct drbd_wq_barrier {
692 struct drbd_work w;
693 struct completion done;
694};
695
696struct digest_info {
697 int digest_size;
698 void *digest;
699};
700
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100701struct drbd_peer_request {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200702 struct drbd_work w;
Philipp Reisner85719572010-07-21 10:20:17 +0200703 struct drbd_epoch *epoch; /* for writes */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200704 struct drbd_conf *mdev;
705 struct page *pages;
706 atomic_t pending_bios;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +0100707 struct drbd_interval i;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200708 /* see comments on ee flag bits below */
709 unsigned long flags;
Philipp Reisner85719572010-07-21 10:20:17 +0200710 union {
711 u64 block_id;
712 struct digest_info *digest;
713 };
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200714};
715
716/* ee flag bits.
717 * While corresponding bios are in flight, the only modification will be
718 * set_bit WAS_ERROR, which has to be atomic.
719 * If no bios are in flight yet, or all have been completed,
720 * non-atomic modification to ee->flags is ok.
721 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700722enum {
723 __EE_CALL_AL_COMPLETE_IO,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700724 __EE_MAY_SET_IN_SYNC,
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200725
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200726 /* In case a barrier failed,
727 * we need to resubmit without the barrier flag. */
728 __EE_RESUBMITTED,
729
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +0100730 /* we may have several bios per peer request.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200731 * if any of those fail, we set this flag atomically
732 * from the endio callback */
733 __EE_WAS_ERROR,
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200734
735 /* This ee has a pointer to a digest instead of a block id */
736 __EE_HAS_DIGEST,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700737};
738#define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700739#define EE_MAY_SET_IN_SYNC (1<<__EE_MAY_SET_IN_SYNC)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200740#define EE_RESUBMITTED (1<<__EE_RESUBMITTED)
741#define EE_WAS_ERROR (1<<__EE_WAS_ERROR)
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200742#define EE_HAS_DIGEST (1<<__EE_HAS_DIGEST)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743
744/* global flag bits */
745enum {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300746 CREATE_BARRIER, /* next P_DATA is preceded by a P_BARRIER */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700747 SIGNAL_ASENDER, /* whether asender wants to be interrupted */
748 SEND_PING, /* whether asender should send a ping asap */
749
Philipp Reisnerb411b362009-09-25 16:07:19 -0700750 UNPLUG_QUEUED, /* only relevant with kernel 2.4 */
751 UNPLUG_REMOTE, /* sending a "UnplugRemote" could help */
752 MD_DIRTY, /* current uuids and flags not yet on disk */
753 DISCARD_CONCURRENT, /* Set on one node, cleared on the peer! */
754 USE_DEGR_WFC_T, /* degr-wfc-timeout instead of wfc-timeout. */
755 CLUSTER_ST_CHANGE, /* Cluster wide state change going on... */
756 CL_ST_CHG_SUCCESS,
757 CL_ST_CHG_FAIL,
758 CRASHED_PRIMARY, /* This node was a crashed primary.
759 * Gets cleared when the state.conn
760 * goes into C_CONNECTED state. */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100761 NO_BARRIER_SUPP, /* underlying block device doesn't implement barriers */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762 CONSIDER_RESYNC,
763
Philipp Reisnera8a4e512010-08-25 10:21:04 +0200764 MD_NO_FUA, /* Users wants us to not use FUA/FLUSH on meta data dev */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700765 SUSPEND_IO, /* suspend application io */
766 BITMAP_IO, /* suspend application io;
767 once no more io in flight, start bitmap io */
768 BITMAP_IO_QUEUED, /* Started bitmap IO */
Lars Ellenberg82f59cc2010-10-16 12:13:47 +0200769 GO_DISKLESS, /* Disk is being detached, on io-error or admin request. */
770 WAS_IO_ERROR, /* Local disk failed returned IO error */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771 RESYNC_AFTER_NEG, /* Resync after online grow after the attach&negotiate finished. */
772 NET_CONGESTED, /* The data socket is congested */
773
774 CONFIG_PENDING, /* serialization of (re)configuration requests.
775 * if set, also prevents the device from dying */
776 DEVICE_DYING, /* device became unconfigured,
777 * but worker thread is still handling the cleanup.
778 * reconfiguring (nl_disk_conf, nl_net_conf) is dissalowed,
779 * while this is set. */
780 RESIZE_PENDING, /* Size change detected locally, waiting for the response from
781 * the peer, if it changed there as well. */
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100782 CONN_DRY_RUN, /* Expect disconnect after resync handshake. */
Philipp Reisner309d1602010-03-02 15:03:44 +0100783 GOT_PING_ACK, /* set when we receive a ping_ack packet, misc wait gets woken */
Philipp Reisner43a51822010-06-11 11:26:34 +0200784 NEW_CUR_UUID, /* Create new current UUID when thawing IO */
Philipp Reisner07782862010-08-31 12:00:50 +0200785 AL_SUSPENDED, /* Activity logging is currently suspended. */
Philipp Reisner370a43e2011-01-14 16:03:11 +0100786 AHEAD_TO_SYNC_SOURCE, /* Ahead -> SyncSource queued */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700787};
788
789struct drbd_bitmap; /* opaque for drbd_conf */
790
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100791/* definition of bits in bm_flags to be used in drbd_bm_lock
792 * and drbd_bitmap_io and friends. */
793enum bm_flag {
794 /* do we need to kfree, or vfree bm_pages? */
795 BM_P_VMALLOCED = 0x10000, /* internal use only, will be masked out */
796
797 /* currently locked for bulk operation */
798 BM_LOCKED_MASK = 0x7,
799
800 /* in detail, that is: */
801 BM_DONT_CLEAR = 0x1,
802 BM_DONT_SET = 0x2,
803 BM_DONT_TEST = 0x4,
804
805 /* (test bit, count bit) allowed (common case) */
806 BM_LOCKED_TEST_ALLOWED = 0x3,
807
808 /* testing bits, as well as setting new bits allowed, but clearing bits
809 * would be unexpected. Used during bitmap receive. Setting new bits
810 * requires sending of "out-of-sync" information, though. */
811 BM_LOCKED_SET_ALLOWED = 0x1,
812
813 /* clear is not expected while bitmap is locked for bulk operation */
814};
815
816
Philipp Reisnerb411b362009-09-25 16:07:19 -0700817/* TODO sort members for performance
818 * MAYBE group them further */
819
820/* THINK maybe we actually want to use the default "event/%s" worker threads
821 * or similar in linux 2.6, which uses per cpu data and threads.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700822 */
823struct drbd_work_queue {
824 struct list_head q;
825 struct semaphore s; /* producers up it, worker down()s it */
826 spinlock_t q_lock; /* to protect the list. */
827};
828
829struct drbd_socket {
830 struct drbd_work_queue work;
831 struct mutex mutex;
832 struct socket *socket;
833 /* this way we get our
834 * send/receive buffers off the stack */
835 union p_polymorph sbuf;
836 union p_polymorph rbuf;
837};
838
839struct drbd_md {
840 u64 md_offset; /* sector offset to 'super' block */
841
842 u64 la_size_sect; /* last agreed size, unit sectors */
843 u64 uuid[UI_SIZE];
844 u64 device_uuid;
845 u32 flags;
846 u32 md_size_sect;
847
848 s32 al_offset; /* signed relative sector offset to al area */
849 s32 bm_offset; /* signed relative sector offset to bitmap */
850
851 /* u32 al_nr_extents; important for restoring the AL
852 * is stored into sync_conf.al_extents, which in turn
853 * gets applied to act_log->nr_elements
854 */
855};
856
857/* for sync_conf and other types... */
858#define NL_PACKET(name, number, fields) struct name { fields };
859#define NL_INTEGER(pn,pr,member) int member;
860#define NL_INT64(pn,pr,member) __u64 member;
861#define NL_BIT(pn,pr,member) unsigned member:1;
862#define NL_STRING(pn,pr,member,len) unsigned char member[len]; int member ## _len;
863#include "linux/drbd_nl.h"
864
865struct drbd_backing_dev {
866 struct block_device *backing_bdev;
867 struct block_device *md_bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700868 struct drbd_md md;
869 struct disk_conf dc; /* The user provided config... */
870 sector_t known_size; /* last known size of that backing device */
871};
872
873struct drbd_md_io {
874 struct drbd_conf *mdev;
875 struct completion event;
876 int error;
877};
878
879struct bm_io_work {
880 struct drbd_work w;
881 char *why;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100882 enum bm_flag flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700883 int (*io_fn)(struct drbd_conf *mdev);
884 void (*done)(struct drbd_conf *mdev, int rv);
885};
886
887enum write_ordering_e {
888 WO_none,
889 WO_drain_io,
890 WO_bdev_flush,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700891};
892
Philipp Reisner778f2712010-07-06 11:14:00 +0200893struct fifo_buffer {
894 int *values;
895 unsigned int head_index;
896 unsigned int size;
897};
898
Philipp Reisner21114382011-01-19 12:26:59 +0100899struct drbd_tconn { /* is a resource from the config file */
900 char *name; /* Resource name */
901 struct list_head all_tconn; /* List of all drbd_tconn, prot by global_state_lock */
902 struct drbd_conf *volume0; /* TODO: Remove me again */
903
904 struct net_conf *net_conf; /* protected by get_net_conf() and put_net_conf() */
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100905 atomic_t net_cnt; /* Users of net_conf */
906 wait_queue_head_t net_cnt_wait;
Philipp Reisnere42325a2011-01-19 13:55:45 +0100907
908 struct drbd_socket data; /* data/barrier/cstate/parameter packets */
909 struct drbd_socket meta; /* ping/ack (metadata) packets */
Philipp Reisner31890f42011-01-19 14:12:51 +0100910 int agreed_pro_version; /* actually used protocol version */
911 unsigned long last_received; /* in jiffies, either socket */
912 unsigned int ko_count;
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100913
Philipp Reisner87eeee42011-01-19 14:16:30 +0100914 spinlock_t req_lock;
915 struct drbd_tl_epoch *unused_spare_tle; /* for pre-allocation */
916 struct drbd_tl_epoch *newest_tle;
917 struct drbd_tl_epoch *oldest_tle;
918 struct list_head out_of_sequence_requests;
919
Philipp Reisnera0638452011-01-19 14:31:32 +0100920 struct crypto_hash *cram_hmac_tfm;
921 struct crypto_hash *integrity_w_tfm; /* to be used by the worker thread */
922 struct crypto_hash *integrity_r_tfm; /* to be used by the receiver thread */
923 void *int_dig_out;
924 void *int_dig_in;
925 void *int_dig_vv;
926
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100927 struct drbd_thread receiver;
928 struct drbd_thread worker;
929 struct drbd_thread asender;
Philipp Reisner21114382011-01-19 12:26:59 +0100930};
931
Philipp Reisnerb411b362009-09-25 16:07:19 -0700932struct drbd_conf {
Philipp Reisner21114382011-01-19 12:26:59 +0100933 struct drbd_tconn *tconn;
934 int vnr; /* volume number within the connection */
935
Philipp Reisnerb411b362009-09-25 16:07:19 -0700936 /* things that are stored as / read from meta data on disk */
937 unsigned long flags;
938
939 /* configured by drbdsetup */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700940 struct syncer_conf sync_conf;
941 struct drbd_backing_dev *ldev __protected_by(local);
942
943 sector_t p_size; /* partner's disk size */
944 struct request_queue *rq_queue;
945 struct block_device *this_bdev;
946 struct gendisk *vdisk;
947
Philipp Reisnerb411b362009-09-25 16:07:19 -0700948 struct drbd_work resync_work,
949 unplug_work,
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +0200950 go_diskless,
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200951 md_sync_work,
952 start_resync_work;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953 struct timer_list resync_timer;
954 struct timer_list md_sync_timer;
Philipp Reisner370a43e2011-01-14 16:03:11 +0100955 struct timer_list start_resync_timer;
Philipp Reisner7fde2be2011-03-01 11:08:28 +0100956 struct timer_list request_timer;
Lars Ellenbergee15b032010-09-03 10:00:09 +0200957#ifdef DRBD_DEBUG_MD_SYNC
958 struct {
959 unsigned int line;
960 const char* func;
961 } last_md_mark_dirty;
962#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963
964 /* Used after attach while negotiating new disk state. */
965 union drbd_state new_state_tmp;
966
967 union drbd_state state;
968 wait_queue_head_t misc_wait;
969 wait_queue_head_t state_wait; /* upon each state change. */
970 unsigned int send_cnt;
971 unsigned int recv_cnt;
972 unsigned int read_cnt;
973 unsigned int writ_cnt;
974 unsigned int al_writ_cnt;
975 unsigned int bm_writ_cnt;
976 atomic_t ap_bio_cnt; /* Requests we need to complete */
977 atomic_t ap_pending_cnt; /* AP data packets on the wire, ack expected */
978 atomic_t rs_pending_cnt; /* RS request/data packets on the wire */
979 atomic_t unacked_cnt; /* Need to send replys for */
980 atomic_t local_cnt; /* Waiting for local completion */
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100981
Andreas Gruenbacherdac13892011-01-21 17:18:39 +0100982 /* Interval tree of pending local requests */
983 struct rb_root read_requests;
Andreas Gruenbacherde696712011-01-20 15:00:24 +0100984 struct rb_root write_requests;
985
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100986 /* blocks to resync in this run [unit BM_BLOCK_SIZE] */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700987 unsigned long rs_total;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100988 /* number of resync blocks that failed in this run */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700989 unsigned long rs_failed;
990 /* Syncer's start time [unit jiffies] */
991 unsigned long rs_start;
992 /* cumulated time in PausedSyncX state [unit jiffies] */
993 unsigned long rs_paused;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200994 /* skipped because csum was equal [unit BM_BLOCK_SIZE] */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700995 unsigned long rs_same_csum;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200996#define DRBD_SYNC_MARKS 8
997#define DRBD_SYNC_MARK_STEP (3*HZ)
998 /* block not up-to-date at mark [unit BM_BLOCK_SIZE] */
999 unsigned long rs_mark_left[DRBD_SYNC_MARKS];
1000 /* marks's time [unit jiffies] */
1001 unsigned long rs_mark_time[DRBD_SYNC_MARKS];
1002 /* current index into rs_mark_{left,time} */
1003 int rs_last_mark;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004
1005 /* where does the admin want us to start? (sector) */
1006 sector_t ov_start_sector;
1007 /* where are we now? (sector) */
1008 sector_t ov_position;
1009 /* Start sector of out of sync range (to merge printk reporting). */
1010 sector_t ov_last_oos_start;
1011 /* size of out-of-sync range in sectors. */
1012 sector_t ov_last_oos_size;
1013 unsigned long ov_left; /* in bits */
1014 struct crypto_hash *csums_tfm;
1015 struct crypto_hash *verify_tfm;
1016
Philipp Reisnerb411b362009-09-25 16:07:19 -07001017 struct drbd_bitmap *bitmap;
1018 unsigned long bm_resync_fo; /* bit offset for drbd_bm_find_next */
1019
1020 /* Used to track operations of resync... */
1021 struct lru_cache *resync;
1022 /* Number of locked elements in resync LRU */
1023 unsigned int resync_locked;
1024 /* resync extent number waiting for application requests */
1025 unsigned int resync_wenr;
1026
1027 int open_cnt;
1028 u64 *p_uuid;
1029 struct drbd_epoch *current_epoch;
1030 spinlock_t epoch_lock;
1031 unsigned int epochs;
1032 enum write_ordering_e write_ordering;
Philipp Reisner85719572010-07-21 10:20:17 +02001033 struct list_head active_ee; /* IO in progress (P_DATA gets written to disk) */
1034 struct list_head sync_ee; /* IO in progress (P_RS_DATA_REPLY gets written to disk) */
Andreas Gruenbacher18b75d72011-02-04 15:36:22 +01001035 struct list_head done_ee; /* need to send P_WRITE_ACK */
1036 struct list_head read_ee; /* [RS]P_DATA_REQUEST being read */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037 struct list_head net_ee; /* zero-copy network send in progress */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001038
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039 int next_barrier_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001040 struct list_head resync_reads;
Lars Ellenberg435f0742010-09-06 12:30:25 +02001041 atomic_t pp_in_use; /* allocated from page pool */
1042 atomic_t pp_in_use_by_net; /* sendpage()d, still referenced by tcp */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043 wait_queue_head_t ee_wait;
1044 struct page *md_io_page; /* one page buffer for md_io */
1045 struct page *md_io_tmpp; /* for logical_block_size != 512 */
1046 struct mutex md_io_mutex; /* protects the md_io_buffer */
1047 spinlock_t al_lock;
1048 wait_queue_head_t al_wait;
1049 struct lru_cache *act_log; /* activity log */
1050 unsigned int al_tr_number;
1051 int al_tr_cycle;
1052 int al_tr_pos; /* position of the next transaction in the journal */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001053 wait_queue_head_t seq_wait;
1054 atomic_t packet_seq;
1055 unsigned int peer_seq;
1056 spinlock_t peer_seq_lock;
1057 unsigned int minor;
1058 unsigned long comm_bm_set; /* communicated number of set bits. */
1059 cpumask_var_t cpu_mask;
1060 struct bm_io_work bm_io_work;
1061 u64 ed_uuid; /* UUID of the exposed data */
1062 struct mutex state_mutex;
1063 char congestion_reason; /* Why we where congested... */
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001064 atomic_t rs_sect_in; /* for incoming resync data rate, SyncTarget */
1065 atomic_t rs_sect_ev; /* for submitted resync data rate, both */
1066 int rs_last_sect_ev; /* counter to compare with */
1067 int rs_last_events; /* counter of read or write "events" (unit sectors)
1068 * on the lower level device when we last looked. */
1069 int c_sync_rate; /* current resync rate after syncer throttle magic */
Philipp Reisner778f2712010-07-06 11:14:00 +02001070 struct fifo_buffer rs_plan_s; /* correction values of resync planer */
1071 int rs_in_flight; /* resync sectors in flight (to proxy, in proxy and from proxy) */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001072 int rs_planed; /* resync sectors already planned */
Philipp Reisner759fbdf2010-10-26 16:02:27 +02001073 atomic_t ap_in_flight; /* App sectors in flight (waiting for ack) */
Philipp Reisner99432fc2011-05-20 16:39:13 +02001074 int peer_max_bio_size;
1075 int local_max_bio_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001076};
1077
1078static inline struct drbd_conf *minor_to_mdev(unsigned int minor)
1079{
1080 struct drbd_conf *mdev;
1081
1082 mdev = minor < minor_count ? minor_table[minor] : NULL;
1083
1084 return mdev;
1085}
1086
1087static inline unsigned int mdev_to_minor(struct drbd_conf *mdev)
1088{
1089 return mdev->minor;
1090}
1091
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001092/* returns 1 if it was successful,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001093 * returns 0 if there was no data socket.
1094 * so wherever you are going to use the data.socket, e.g. do
1095 * if (!drbd_get_data_sock(mdev))
1096 * return 0;
1097 * CODE();
1098 * drbd_put_data_sock(mdev);
1099 */
1100static inline int drbd_get_data_sock(struct drbd_conf *mdev)
1101{
Philipp Reisnere42325a2011-01-19 13:55:45 +01001102 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001103 /* drbd_disconnect() could have called drbd_free_sock()
1104 * while we were waiting in down()... */
Philipp Reisnere42325a2011-01-19 13:55:45 +01001105 if (unlikely(mdev->tconn->data.socket == NULL)) {
1106 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001107 return 0;
1108 }
1109 return 1;
1110}
1111
1112static inline void drbd_put_data_sock(struct drbd_conf *mdev)
1113{
Philipp Reisnere42325a2011-01-19 13:55:45 +01001114 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001115}
1116
1117/*
1118 * function declarations
1119 *************************/
1120
1121/* drbd_main.c */
1122
1123enum chg_state_flags {
1124 CS_HARD = 1,
1125 CS_VERBOSE = 2,
1126 CS_WAIT_COMPLETE = 4,
1127 CS_SERIALIZE = 8,
1128 CS_ORDERED = CS_WAIT_COMPLETE + CS_SERIALIZE,
1129};
1130
Philipp Reisnere89b5912010-03-24 17:11:33 +01001131enum dds_flags {
1132 DDSF_FORCED = 1,
1133 DDSF_NO_RESYNC = 2, /* Do not run a resync for the new space */
1134};
1135
Philipp Reisnerb411b362009-09-25 16:07:19 -07001136extern void drbd_init_set_defaults(struct drbd_conf *mdev);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001137extern enum drbd_state_rv drbd_change_state(struct drbd_conf *mdev,
1138 enum chg_state_flags f,
1139 union drbd_state mask,
1140 union drbd_state val);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141extern void drbd_force_state(struct drbd_conf *, union drbd_state,
1142 union drbd_state);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001143extern enum drbd_state_rv _drbd_request_state(struct drbd_conf *,
1144 union drbd_state,
1145 union drbd_state,
1146 enum chg_state_flags);
1147extern enum drbd_state_rv __drbd_set_state(struct drbd_conf *, union drbd_state,
1148 enum chg_state_flags,
1149 struct completion *done);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150extern void print_st_err(struct drbd_conf *, union drbd_state,
1151 union drbd_state, int);
1152extern int drbd_thread_start(struct drbd_thread *thi);
1153extern void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait);
1154#ifdef CONFIG_SMP
1155extern void drbd_thread_current_set_cpu(struct drbd_conf *mdev);
1156extern void drbd_calc_cpu_mask(struct drbd_conf *mdev);
1157#else
1158#define drbd_thread_current_set_cpu(A) ({})
1159#define drbd_calc_cpu_mask(A) ({})
1160#endif
1161extern void drbd_free_resources(struct drbd_conf *mdev);
1162extern void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
1163 unsigned int set_size);
1164extern void tl_clear(struct drbd_conf *mdev);
1165extern void _tl_add_barrier(struct drbd_conf *, struct drbd_tl_epoch *);
1166extern void drbd_free_sock(struct drbd_conf *mdev);
1167extern int drbd_send(struct drbd_conf *mdev, struct socket *sock,
1168 void *buf, size_t size, unsigned msg_flags);
1169extern int drbd_send_protocol(struct drbd_conf *mdev);
1170extern int drbd_send_uuids(struct drbd_conf *mdev);
1171extern int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev);
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001172extern int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev);
Philipp Reisnere89b5912010-03-24 17:11:33 +01001173extern int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174extern int _drbd_send_state(struct drbd_conf *mdev);
1175extern int drbd_send_state(struct drbd_conf *mdev);
1176extern int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001177 enum drbd_packet cmd, struct p_header *h,
1178 size_t size, unsigned msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179#define USE_DATA_SOCKET 1
1180#define USE_META_SOCKET 0
1181extern int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001182 enum drbd_packet cmd, struct p_header *h, size_t size);
1183extern int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packet cmd,
1184 char *data, size_t size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001185extern int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc);
1186extern int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr,
1187 u32 set_size);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001188extern int drbd_send_ack(struct drbd_conf *, enum drbd_packet,
1189 struct drbd_peer_request *);
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001190extern int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
1191 struct p_block_req *rp);
1192extern int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
1193 struct p_data *dp, int data_size);
1194extern int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195 sector_t sector, int blksize, u64 block_id);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001196extern int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001197extern int drbd_send_block(struct drbd_conf *, enum drbd_packet,
1198 struct drbd_peer_request *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001199extern int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001200extern int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1201 sector_t sector, int size, u64 block_id);
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001202extern int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector,
1203 int size, void *digest, int digest_size,
1204 enum drbd_packet cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205extern int drbd_send_ov_request(struct drbd_conf *mdev,sector_t sector,int size);
1206
1207extern int drbd_send_bitmap(struct drbd_conf *mdev);
1208extern int _drbd_send_bitmap(struct drbd_conf *mdev);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001209extern int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001210extern void drbd_free_bc(struct drbd_backing_dev *ldev);
1211extern void drbd_mdev_cleanup(struct drbd_conf *mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01001212void drbd_print_uuids(struct drbd_conf *mdev, const char *text);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001213
Philipp Reisnerb411b362009-09-25 16:07:19 -07001214extern void drbd_md_sync(struct drbd_conf *mdev);
1215extern int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001216extern void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1217extern void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1218extern void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1219extern void _drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1220extern void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local);
1221extern void drbd_md_set_flag(struct drbd_conf *mdev, int flags) __must_hold(local);
1222extern void drbd_md_clear_flag(struct drbd_conf *mdev, int flags)__must_hold(local);
1223extern int drbd_md_test_flag(struct drbd_backing_dev *, int);
Lars Ellenbergee15b032010-09-03 10:00:09 +02001224#ifndef DRBD_DEBUG_MD_SYNC
Philipp Reisnerb411b362009-09-25 16:07:19 -07001225extern void drbd_md_mark_dirty(struct drbd_conf *mdev);
Lars Ellenbergee15b032010-09-03 10:00:09 +02001226#else
1227#define drbd_md_mark_dirty(m) drbd_md_mark_dirty_(m, __LINE__ , __func__ )
1228extern void drbd_md_mark_dirty_(struct drbd_conf *mdev,
1229 unsigned int line, const char *func);
1230#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001231extern void drbd_queue_bitmap_io(struct drbd_conf *mdev,
1232 int (*io_fn)(struct drbd_conf *),
1233 void (*done)(struct drbd_conf *, int),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001234 char *why, enum bm_flag flags);
1235extern int drbd_bitmap_io(struct drbd_conf *mdev,
1236 int (*io_fn)(struct drbd_conf *),
1237 char *why, enum bm_flag flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001238extern int drbd_bmio_set_n_write(struct drbd_conf *mdev);
1239extern int drbd_bmio_clear_n_write(struct drbd_conf *mdev);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001240extern void drbd_go_diskless(struct drbd_conf *mdev);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001241extern void drbd_ldev_destroy(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242
1243
1244/* Meta data layout
1245 We reserve a 128MB Block (4k aligned)
1246 * either at the end of the backing device
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001247 * or on a separate meta data device. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001248
1249#define MD_RESERVED_SECT (128LU << 11) /* 128 MB, unit sectors */
1250/* The following numbers are sectors */
1251#define MD_AL_OFFSET 8 /* 8 Sectors after start of meta area */
1252#define MD_AL_MAX_SIZE 64 /* = 32 kb LOG ~ 3776 extents ~ 14 GB Storage */
1253/* Allows up to about 3.8TB */
1254#define MD_BM_OFFSET (MD_AL_OFFSET + MD_AL_MAX_SIZE)
1255
1256/* Since the smalles IO unit is usually 512 byte */
1257#define MD_SECTOR_SHIFT 9
1258#define MD_SECTOR_SIZE (1<<MD_SECTOR_SHIFT)
1259
1260/* activity log */
1261#define AL_EXTENTS_PT ((MD_SECTOR_SIZE-12)/8-1) /* 61 ; Extents per 512B sector */
1262#define AL_EXTENT_SHIFT 22 /* One extent represents 4M Storage */
1263#define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
1264
1265#if BITS_PER_LONG == 32
1266#define LN2_BPL 5
1267#define cpu_to_lel(A) cpu_to_le32(A)
1268#define lel_to_cpu(A) le32_to_cpu(A)
1269#elif BITS_PER_LONG == 64
1270#define LN2_BPL 6
1271#define cpu_to_lel(A) cpu_to_le64(A)
1272#define lel_to_cpu(A) le64_to_cpu(A)
1273#else
1274#error "LN2 of BITS_PER_LONG unknown!"
1275#endif
1276
1277/* resync bitmap */
1278/* 16MB sized 'bitmap extent' to track syncer usage */
1279struct bm_extent {
1280 int rs_left; /* number of bits set (out of sync) in this extent. */
1281 int rs_failed; /* number of failed resync requests in this extent. */
1282 unsigned long flags;
1283 struct lc_element lce;
1284};
1285
1286#define BME_NO_WRITES 0 /* bm_extent.flags: no more requests on this one! */
1287#define BME_LOCKED 1 /* bm_extent.flags: syncer active on this one. */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001288#define BME_PRIORITY 2 /* finish resync IO on this extent ASAP! App IO waiting! */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001289
1290/* drbd_bitmap.c */
1291/*
1292 * We need to store one bit for a block.
1293 * Example: 1GB disk @ 4096 byte blocks ==> we need 32 KB bitmap.
1294 * Bit 0 ==> local node thinks this block is binary identical on both nodes
1295 * Bit 1 ==> local node thinks this block needs to be synced.
1296 */
1297
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001298#define SLEEP_TIME (HZ/10)
1299
Philipp Reisnerb411b362009-09-25 16:07:19 -07001300#define BM_BLOCK_SHIFT 12 /* 4k per bit */
1301#define BM_BLOCK_SIZE (1<<BM_BLOCK_SHIFT)
1302/* (9+3) : 512 bytes @ 8 bits; representing 16M storage
1303 * per sector of on disk bitmap */
1304#define BM_EXT_SHIFT (BM_BLOCK_SHIFT + MD_SECTOR_SHIFT + 3) /* = 24 */
1305#define BM_EXT_SIZE (1<<BM_EXT_SHIFT)
1306
1307#if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
1308#error "HAVE YOU FIXED drbdmeta AS WELL??"
1309#endif
1310
1311/* thus many _storage_ sectors are described by one bit */
1312#define BM_SECT_TO_BIT(x) ((x)>>(BM_BLOCK_SHIFT-9))
1313#define BM_BIT_TO_SECT(x) ((sector_t)(x)<<(BM_BLOCK_SHIFT-9))
1314#define BM_SECT_PER_BIT BM_BIT_TO_SECT(1)
1315
1316/* bit to represented kilo byte conversion */
1317#define Bit2KB(bits) ((bits)<<(BM_BLOCK_SHIFT-10))
1318
1319/* in which _bitmap_ extent (resp. sector) the bit for a certain
1320 * _storage_ sector is located in */
1321#define BM_SECT_TO_EXT(x) ((x)>>(BM_EXT_SHIFT-9))
1322
1323/* how much _storage_ sectors we have per bitmap sector */
1324#define BM_EXT_TO_SECT(x) ((sector_t)(x) << (BM_EXT_SHIFT-9))
1325#define BM_SECT_PER_EXT BM_EXT_TO_SECT(1)
1326
1327/* in one sector of the bitmap, we have this many activity_log extents. */
1328#define AL_EXT_PER_BM_SECT (1 << (BM_EXT_SHIFT - AL_EXTENT_SHIFT))
1329#define BM_WORDS_PER_AL_EXT (1 << (AL_EXTENT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
1330
1331#define BM_BLOCKS_PER_BM_EXT_B (BM_EXT_SHIFT - BM_BLOCK_SHIFT)
1332#define BM_BLOCKS_PER_BM_EXT_MASK ((1<<BM_BLOCKS_PER_BM_EXT_B) - 1)
1333
1334/* the extent in "PER_EXTENT" below is an activity log extent
1335 * we need that many (long words/bytes) to store the bitmap
1336 * of one AL_EXTENT_SIZE chunk of storage.
1337 * we can store the bitmap for that many AL_EXTENTS within
1338 * one sector of the _on_disk_ bitmap:
1339 * bit 0 bit 37 bit 38 bit (512*8)-1
1340 * ...|........|........|.. // ..|........|
1341 * sect. 0 `296 `304 ^(512*8*8)-1
1342 *
1343#define BM_WORDS_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / BITS_PER_LONG )
1344#define BM_BYTES_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / 8 ) // 128
1345#define BM_EXT_PER_SECT ( 512 / BM_BYTES_PER_EXTENT ) // 4
1346 */
1347
1348#define DRBD_MAX_SECTORS_32 (0xffffffffLU)
1349#define DRBD_MAX_SECTORS_BM \
1350 ((MD_RESERVED_SECT - MD_BM_OFFSET) * (1LL<<(BM_EXT_SHIFT-9)))
1351#if DRBD_MAX_SECTORS_BM < DRBD_MAX_SECTORS_32
1352#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1353#define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_BM
Lars Ellenberg36bfc7e2010-01-05 19:33:54 +01001354#elif !defined(CONFIG_LBDAF) && BITS_PER_LONG == 32
Philipp Reisnerb411b362009-09-25 16:07:19 -07001355#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_32
1356#define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_32
1357#else
1358#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1359/* 16 TB in units of sectors */
1360#if BITS_PER_LONG == 32
1361/* adjust by one page worth of bitmap,
1362 * so we won't wrap around in drbd_bm_find_next_bit.
1363 * you should use 64bit OS for that much storage, anyways. */
1364#define DRBD_MAX_SECTORS_FLEX BM_BIT_TO_SECT(0xffff7fff)
1365#else
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001366/* we allow up to 1 PiB now on 64bit architecture with "flexible" meta data */
1367#define DRBD_MAX_SECTORS_FLEX (1UL << 51)
1368/* corresponds to (1UL << 38) bits right now. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001369#endif
1370#endif
1371
Philipp Reisnerd5373382010-08-23 15:18:33 +02001372#define HT_SHIFT 8
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001373#define DRBD_MAX_BIO_SIZE (1U<<(9+HT_SHIFT))
Philipp Reisner99432fc2011-05-20 16:39:13 +02001374#define DRBD_MAX_BIO_SIZE_SAFE (1 << 12) /* Works always = 4k */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001375
Philipp Reisnerd5373382010-08-23 15:18:33 +02001376#define DRBD_MAX_SIZE_H80_PACKET (1 << 15) /* The old header only allows packets up to 32Kib data */
1377
Philipp Reisnerb411b362009-09-25 16:07:19 -07001378extern int drbd_bm_init(struct drbd_conf *mdev);
Philipp Reisner02d9a942010-03-24 16:23:03 +01001379extern int drbd_bm_resize(struct drbd_conf *mdev, sector_t sectors, int set_new_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001380extern void drbd_bm_cleanup(struct drbd_conf *mdev);
1381extern void drbd_bm_set_all(struct drbd_conf *mdev);
1382extern void drbd_bm_clear_all(struct drbd_conf *mdev);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001383/* set/clear/test only a few bits at a time */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001384extern int drbd_bm_set_bits(
1385 struct drbd_conf *mdev, unsigned long s, unsigned long e);
1386extern int drbd_bm_clear_bits(
1387 struct drbd_conf *mdev, unsigned long s, unsigned long e);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001388extern int drbd_bm_count_bits(
1389 struct drbd_conf *mdev, const unsigned long s, const unsigned long e);
1390/* bm_set_bits variant for use while holding drbd_bm_lock,
1391 * may process the whole bitmap in one go */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001392extern void _drbd_bm_set_bits(struct drbd_conf *mdev,
1393 const unsigned long s, const unsigned long e);
1394extern int drbd_bm_test_bit(struct drbd_conf *mdev, unsigned long bitnr);
1395extern int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001396extern int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001397extern int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local);
1398extern int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local);
1399extern unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev,
1400 unsigned long al_enr);
1401extern size_t drbd_bm_words(struct drbd_conf *mdev);
1402extern unsigned long drbd_bm_bits(struct drbd_conf *mdev);
1403extern sector_t drbd_bm_capacity(struct drbd_conf *mdev);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001404
1405#define DRBD_END_OF_BITMAP (~(unsigned long)0)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406extern unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1407/* bm_find_next variants for use while you hold drbd_bm_lock() */
1408extern unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1409extern unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo);
Philipp Reisner07782862010-08-31 12:00:50 +02001410extern unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001411extern unsigned long drbd_bm_total_weight(struct drbd_conf *mdev);
1412extern int drbd_bm_rs_done(struct drbd_conf *mdev);
1413/* for receive_bitmap */
1414extern void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset,
1415 size_t number, unsigned long *buffer);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001416/* for _drbd_send_bitmap */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001417extern void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset,
1418 size_t number, unsigned long *buffer);
1419
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001420extern void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001421extern void drbd_bm_unlock(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422/* drbd_main.c */
1423
1424extern struct kmem_cache *drbd_request_cache;
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001425extern struct kmem_cache *drbd_ee_cache; /* peer requests */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426extern struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
1427extern struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
1428extern mempool_t *drbd_request_mempool;
1429extern mempool_t *drbd_ee_mempool;
1430
1431extern struct page *drbd_pp_pool; /* drbd's page pool */
1432extern spinlock_t drbd_pp_lock;
1433extern int drbd_pp_vacant;
1434extern wait_queue_head_t drbd_pp_wait;
1435
1436extern rwlock_t global_state_lock;
1437
1438extern struct drbd_conf *drbd_new_device(unsigned int minor);
1439extern void drbd_free_mdev(struct drbd_conf *mdev);
1440
Philipp Reisner21114382011-01-19 12:26:59 +01001441struct drbd_tconn *drbd_new_tconn(char *name);
1442extern void drbd_free_tconn(struct drbd_tconn *tconn);
1443
Philipp Reisnerb411b362009-09-25 16:07:19 -07001444extern int proc_details;
1445
1446/* drbd_req */
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01001447extern int drbd_make_request(struct request_queue *q, struct bio *bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001448extern int drbd_read_remote(struct drbd_conf *mdev, struct drbd_request *req);
1449extern int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec);
1450extern int is_valid_ar_handle(struct drbd_request *, sector_t);
1451
1452
1453/* drbd_nl.c */
1454extern void drbd_suspend_io(struct drbd_conf *mdev);
1455extern void drbd_resume_io(struct drbd_conf *mdev);
1456extern char *ppsize(char *buf, unsigned long long size);
Philipp Reisnera393db62009-12-22 13:35:52 +01001457extern sector_t drbd_new_dev_size(struct drbd_conf *, struct drbd_backing_dev *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001458enum determine_dev_size { dev_size_error = -1, unchanged = 0, shrunk = 1, grew = 2 };
Bart Van Assche24c48302011-05-21 18:32:29 +02001459extern enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *, enum dds_flags) __must_hold(local);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001460extern void resync_after_online_grow(struct drbd_conf *);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001461extern void drbd_reconsider_max_bio_size(struct drbd_conf *mdev);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001462extern enum drbd_state_rv drbd_set_role(struct drbd_conf *mdev,
1463 enum drbd_role new_role,
1464 int force);
Philipp Reisner87f7be42010-06-11 13:56:33 +02001465extern enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev);
1466extern void drbd_try_outdate_peer_async(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001467extern int drbd_khelper(struct drbd_conf *mdev, char *cmd);
1468
1469/* drbd_worker.c */
1470extern int drbd_worker(struct drbd_thread *thi);
1471extern int drbd_alter_sa(struct drbd_conf *mdev, int na);
1472extern void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side);
1473extern void resume_next_sg(struct drbd_conf *mdev);
1474extern void suspend_other_sg(struct drbd_conf *mdev);
1475extern int drbd_resync_finished(struct drbd_conf *mdev);
1476/* maybe rather drbd_main.c ? */
1477extern int drbd_md_sync_page_io(struct drbd_conf *mdev,
1478 struct drbd_backing_dev *bdev, sector_t sector, int rw);
1479extern void drbd_ov_oos_found(struct drbd_conf*, sector_t, int);
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001480extern void drbd_rs_controller_reset(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001481
1482static inline void ov_oos_print(struct drbd_conf *mdev)
1483{
1484 if (mdev->ov_last_oos_size) {
1485 dev_err(DEV, "Out of sync: start=%llu, size=%lu (sectors)\n",
1486 (unsigned long long)mdev->ov_last_oos_start,
1487 (unsigned long)mdev->ov_last_oos_size);
1488 }
1489 mdev->ov_last_oos_size=0;
1490}
1491
1492
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001493extern void drbd_csum_bio(struct drbd_conf *, struct crypto_hash *, struct bio *, void *);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001494extern void drbd_csum_ee(struct drbd_conf *, struct crypto_hash *,
1495 struct drbd_peer_request *, void *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001496/* worker callbacks */
1497extern int w_req_cancel_conflict(struct drbd_conf *, struct drbd_work *, int);
1498extern int w_read_retry_remote(struct drbd_conf *, struct drbd_work *, int);
1499extern int w_e_end_data_req(struct drbd_conf *, struct drbd_work *, int);
1500extern int w_e_end_rsdata_req(struct drbd_conf *, struct drbd_work *, int);
1501extern int w_e_end_csum_rs_req(struct drbd_conf *, struct drbd_work *, int);
1502extern int w_e_end_ov_reply(struct drbd_conf *, struct drbd_work *, int);
1503extern int w_e_end_ov_req(struct drbd_conf *, struct drbd_work *, int);
1504extern int w_ov_finished(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisner794abb72010-12-27 11:51:23 +01001505extern int w_resync_timer(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001506extern int w_resume_next_sg(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001507extern int w_send_write_hint(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508extern int w_send_dblock(struct drbd_conf *, struct drbd_work *, int);
1509extern int w_send_barrier(struct drbd_conf *, struct drbd_work *, int);
1510extern int w_send_read_req(struct drbd_conf *, struct drbd_work *, int);
1511extern int w_prev_work_done(struct drbd_conf *, struct drbd_work *, int);
1512extern int w_e_reissue(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisner265be2d2010-05-31 10:14:17 +02001513extern int w_restart_disk_io(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001514extern int w_send_oos(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001515extern int w_start_resync(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001516
1517extern void resync_timer_fn(unsigned long data);
Philipp Reisner370a43e2011-01-14 16:03:11 +01001518extern void start_resync_timer_fn(unsigned long data);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519
1520/* drbd_receiver.c */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001521extern int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001522extern int drbd_submit_ee(struct drbd_conf *, struct drbd_peer_request *,
1523 const unsigned, const int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001524extern int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001525extern struct drbd_peer_request *drbd_alloc_ee(struct drbd_conf *,
1526 u64, sector_t, unsigned int,
1527 gfp_t) __must_hold(local);
1528extern void drbd_free_some_ee(struct drbd_conf *, struct drbd_peer_request *,
1529 int);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001530#define drbd_free_ee(m,e) drbd_free_some_ee(m, e, 0)
1531#define drbd_free_net_ee(m,e) drbd_free_some_ee(m, e, 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532extern void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1533 struct list_head *head);
1534extern void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1535 struct list_head *head);
1536extern void drbd_set_recv_tcq(struct drbd_conf *mdev, int tcq_enabled);
1537extern void _drbd_clear_done_ee(struct drbd_conf *mdev, struct list_head *to_be_freed);
Philipp Reisner191d3cc2011-01-19 14:53:22 +01001538extern void drbd_flush_workqueue(struct drbd_tconn *tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001539
1540/* yes, there is kernel_setsockopt, but only since 2.6.18. we don't need to
1541 * mess with get_fs/set_fs, we know we are KERNEL_DS always. */
1542static inline int drbd_setsockopt(struct socket *sock, int level, int optname,
1543 char __user *optval, int optlen)
1544{
1545 int err;
1546 if (level == SOL_SOCKET)
1547 err = sock_setsockopt(sock, level, optname, optval, optlen);
1548 else
1549 err = sock->ops->setsockopt(sock, level, optname, optval,
1550 optlen);
1551 return err;
1552}
1553
1554static inline void drbd_tcp_cork(struct socket *sock)
1555{
1556 int __user val = 1;
1557 (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1558 (char __user *)&val, sizeof(val));
1559}
1560
1561static inline void drbd_tcp_uncork(struct socket *sock)
1562{
1563 int __user val = 0;
1564 (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1565 (char __user *)&val, sizeof(val));
1566}
1567
1568static inline void drbd_tcp_nodelay(struct socket *sock)
1569{
1570 int __user val = 1;
1571 (void) drbd_setsockopt(sock, SOL_TCP, TCP_NODELAY,
1572 (char __user *)&val, sizeof(val));
1573}
1574
1575static inline void drbd_tcp_quickack(struct socket *sock)
1576{
Lars Ellenberg344fa462010-05-25 14:23:57 +02001577 int __user val = 2;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578 (void) drbd_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
1579 (char __user *)&val, sizeof(val));
1580}
1581
1582void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo);
1583
1584/* drbd_proc.c */
1585extern struct proc_dir_entry *drbd_proc;
Emese Revfy7d4e9d02009-12-14 00:59:30 +01001586extern const struct file_operations drbd_proc_fops;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587extern const char *drbd_conn_str(enum drbd_conns s);
1588extern const char *drbd_role_str(enum drbd_role s);
1589
1590/* drbd_actlog.c */
1591extern void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector);
1592extern void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector);
1593extern void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector);
1594extern int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1595extern int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1596extern void drbd_rs_cancel_all(struct drbd_conf *mdev);
1597extern int drbd_rs_del_all(struct drbd_conf *mdev);
1598extern void drbd_rs_failed_io(struct drbd_conf *mdev,
1599 sector_t sector, int size);
1600extern int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *);
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001601extern void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602extern void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector,
1603 int size, const char *file, const unsigned int line);
1604#define drbd_set_in_sync(mdev, sector, size) \
1605 __drbd_set_in_sync(mdev, sector, size, __FILE__, __LINE__)
Philipp Reisner73a01a12010-10-27 14:33:00 +02001606extern int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001607 int size, const char *file, const unsigned int line);
1608#define drbd_set_out_of_sync(mdev, sector, size) \
1609 __drbd_set_out_of_sync(mdev, sector, size, __FILE__, __LINE__)
1610extern void drbd_al_apply_to_bm(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001611extern void drbd_al_shrink(struct drbd_conf *mdev);
1612
1613
1614/* drbd_nl.c */
1615
1616void drbd_nl_cleanup(void);
1617int __init drbd_nl_init(void);
1618void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state);
1619void drbd_bcast_sync_progress(struct drbd_conf *mdev);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001620void drbd_bcast_ee(struct drbd_conf *, const char *, const int, const char *,
1621 const char *, const struct drbd_peer_request *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622
1623
1624/**
1625 * DOC: DRBD State macros
1626 *
1627 * These macros are used to express state changes in easily readable form.
1628 *
1629 * The NS macros expand to a mask and a value, that can be bit ored onto the
1630 * current state as soon as the spinlock (req_lock) was taken.
1631 *
1632 * The _NS macros are used for state functions that get called with the
1633 * spinlock. These macros expand directly to the new state value.
1634 *
1635 * Besides the basic forms NS() and _NS() additional _?NS[23] are defined
1636 * to express state changes that affect more than one aspect of the state.
1637 *
1638 * E.g. NS2(conn, C_CONNECTED, peer, R_SECONDARY)
1639 * Means that the network connection was established and that the peer
1640 * is in secondary role.
1641 */
1642#define role_MASK R_MASK
1643#define peer_MASK R_MASK
1644#define disk_MASK D_MASK
1645#define pdsk_MASK D_MASK
1646#define conn_MASK C_MASK
1647#define susp_MASK 1
1648#define user_isp_MASK 1
1649#define aftr_isp_MASK 1
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001650#define susp_nod_MASK 1
1651#define susp_fen_MASK 1
Philipp Reisnerb411b362009-09-25 16:07:19 -07001652
1653#define NS(T, S) \
1654 ({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
1655 ({ union drbd_state val; val.i = 0; val.T = (S); val; })
1656#define NS2(T1, S1, T2, S2) \
1657 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1658 mask.T2 = T2##_MASK; mask; }), \
1659 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1660 val.T2 = (S2); val; })
1661#define NS3(T1, S1, T2, S2, T3, S3) \
1662 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1663 mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
1664 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1665 val.T2 = (S2); val.T3 = (S3); val; })
1666
1667#define _NS(D, T, S) \
1668 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T = (S); __ns; })
1669#define _NS2(D, T1, S1, T2, S2) \
1670 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1671 __ns.T2 = (S2); __ns; })
1672#define _NS3(D, T1, S1, T2, S2, T3, S3) \
1673 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1674 __ns.T2 = (S2); __ns.T3 = (S3); __ns; })
1675
1676/*
1677 * inline helper functions
1678 *************************/
1679
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001680/* see also page_chain_add and friends in drbd_receiver.c */
1681static inline struct page *page_chain_next(struct page *page)
1682{
1683 return (struct page *)page_private(page);
1684}
1685#define page_chain_for_each(page) \
1686 for (; page && ({ prefetch(page_chain_next(page)); 1; }); \
1687 page = page_chain_next(page))
1688#define page_chain_for_each_safe(page, n) \
1689 for (; page && ({ n = page_chain_next(page); 1; }); page = n)
1690
1691static inline int drbd_bio_has_active_page(struct bio *bio)
1692{
1693 struct bio_vec *bvec;
1694 int i;
1695
1696 __bio_for_each_segment(bvec, bio, i, 0) {
1697 if (page_count(bvec->bv_page) > 1)
1698 return 1;
1699 }
1700
1701 return 0;
1702}
1703
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001704static inline int drbd_ee_has_active_page(struct drbd_peer_request *e)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001705{
1706 struct page *page = e->pages;
1707 page_chain_for_each(page) {
1708 if (page_count(page) > 1)
1709 return 1;
1710 }
1711 return 0;
1712}
1713
1714
Philipp Reisnerb411b362009-09-25 16:07:19 -07001715static inline void drbd_state_lock(struct drbd_conf *mdev)
1716{
1717 wait_event(mdev->misc_wait,
1718 !test_and_set_bit(CLUSTER_ST_CHANGE, &mdev->flags));
1719}
1720
1721static inline void drbd_state_unlock(struct drbd_conf *mdev)
1722{
1723 clear_bit(CLUSTER_ST_CHANGE, &mdev->flags);
1724 wake_up(&mdev->misc_wait);
1725}
1726
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001727static inline enum drbd_state_rv
1728_drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
1729 enum chg_state_flags flags, struct completion *done)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001730{
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001731 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001732
1733 read_lock(&global_state_lock);
1734 rv = __drbd_set_state(mdev, ns, flags, done);
1735 read_unlock(&global_state_lock);
1736
1737 return rv;
1738}
1739
1740/**
1741 * drbd_request_state() - Reqest a state change
1742 * @mdev: DRBD device.
1743 * @mask: mask of state bits to change.
1744 * @val: value of new state bits.
1745 *
1746 * This is the most graceful way of requesting a state change. It is verbose
1747 * quite verbose in case the state change is not possible, and all those
1748 * state changes are globally serialized.
1749 */
1750static inline int drbd_request_state(struct drbd_conf *mdev,
1751 union drbd_state mask,
1752 union drbd_state val)
1753{
1754 return _drbd_request_state(mdev, mask, val, CS_VERBOSE + CS_ORDERED);
1755}
1756
1757#define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
1758static inline void __drbd_chk_io_error_(struct drbd_conf *mdev, int forcedetach, const char *where)
1759{
1760 switch (mdev->ldev->dc.on_io_error) {
1761 case EP_PASS_ON:
1762 if (!forcedetach) {
Lars Ellenberg73835062010-05-27 11:51:56 +02001763 if (__ratelimit(&drbd_ratelimit_state))
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001764 dev_err(DEV, "Local IO failed in %s.\n", where);
Philipp Reisnerd2e17802011-03-14 11:54:47 +01001765 if (mdev->state.disk > D_INCONSISTENT)
1766 _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_HARD, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001767 break;
1768 }
1769 /* NOTE fall through to detach case if forcedetach set */
1770 case EP_DETACH:
1771 case EP_CALL_HELPER:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001772 set_bit(WAS_IO_ERROR, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001773 if (mdev->state.disk > D_FAILED) {
1774 _drbd_set_state(_NS(mdev, disk, D_FAILED), CS_HARD, NULL);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001775 dev_err(DEV,
1776 "Local IO failed in %s. Detaching...\n", where);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001777 }
1778 break;
1779 }
1780}
1781
1782/**
1783 * drbd_chk_io_error: Handle the on_io_error setting, should be called from all io completion handlers
1784 * @mdev: DRBD device.
1785 * @error: Error code passed to the IO completion callback
1786 * @forcedetach: Force detach. I.e. the error happened while accessing the meta data
1787 *
1788 * See also drbd_main.c:after_state_ch() if (os.disk > D_FAILED && ns.disk == D_FAILED)
1789 */
1790#define drbd_chk_io_error(m,e,f) drbd_chk_io_error_(m,e,f, __func__)
1791static inline void drbd_chk_io_error_(struct drbd_conf *mdev,
1792 int error, int forcedetach, const char *where)
1793{
1794 if (error) {
1795 unsigned long flags;
Philipp Reisner87eeee42011-01-19 14:16:30 +01001796 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001797 __drbd_chk_io_error_(mdev, forcedetach, where);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001798 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001799 }
1800}
1801
1802
1803/**
1804 * drbd_md_first_sector() - Returns the first sector number of the meta data area
1805 * @bdev: Meta data block device.
1806 *
1807 * BTW, for internal meta data, this happens to be the maximum capacity
1808 * we could agree upon with our peer node.
1809 */
1810static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
1811{
1812 switch (bdev->dc.meta_dev_idx) {
1813 case DRBD_MD_INDEX_INTERNAL:
1814 case DRBD_MD_INDEX_FLEX_INT:
1815 return bdev->md.md_offset + bdev->md.bm_offset;
1816 case DRBD_MD_INDEX_FLEX_EXT:
1817 default:
1818 return bdev->md.md_offset;
1819 }
1820}
1821
1822/**
1823 * drbd_md_last_sector() - Return the last sector number of the meta data area
1824 * @bdev: Meta data block device.
1825 */
1826static inline sector_t drbd_md_last_sector(struct drbd_backing_dev *bdev)
1827{
1828 switch (bdev->dc.meta_dev_idx) {
1829 case DRBD_MD_INDEX_INTERNAL:
1830 case DRBD_MD_INDEX_FLEX_INT:
1831 return bdev->md.md_offset + MD_AL_OFFSET - 1;
1832 case DRBD_MD_INDEX_FLEX_EXT:
1833 default:
1834 return bdev->md.md_offset + bdev->md.md_size_sect;
1835 }
1836}
1837
1838/* Returns the number of 512 byte sectors of the device */
1839static inline sector_t drbd_get_capacity(struct block_device *bdev)
1840{
1841 /* return bdev ? get_capacity(bdev->bd_disk) : 0; */
Mike Snitzer77304d22010-11-08 14:39:12 +01001842 return bdev ? i_size_read(bdev->bd_inode) >> 9 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001843}
1844
1845/**
1846 * drbd_get_max_capacity() - Returns the capacity we announce to out peer
1847 * @bdev: Meta data block device.
1848 *
1849 * returns the capacity we announce to out peer. we clip ourselves at the
1850 * various MAX_SECTORS, because if we don't, current implementation will
1851 * oops sooner or later
1852 */
1853static inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
1854{
1855 sector_t s;
1856 switch (bdev->dc.meta_dev_idx) {
1857 case DRBD_MD_INDEX_INTERNAL:
1858 case DRBD_MD_INDEX_FLEX_INT:
1859 s = drbd_get_capacity(bdev->backing_bdev)
1860 ? min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1861 drbd_md_first_sector(bdev))
1862 : 0;
1863 break;
1864 case DRBD_MD_INDEX_FLEX_EXT:
1865 s = min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1866 drbd_get_capacity(bdev->backing_bdev));
1867 /* clip at maximum size the meta device can support */
1868 s = min_t(sector_t, s,
1869 BM_EXT_TO_SECT(bdev->md.md_size_sect
1870 - bdev->md.bm_offset));
1871 break;
1872 default:
1873 s = min_t(sector_t, DRBD_MAX_SECTORS,
1874 drbd_get_capacity(bdev->backing_bdev));
1875 }
1876 return s;
1877}
1878
1879/**
1880 * drbd_md_ss__() - Return the sector number of our meta data super block
1881 * @mdev: DRBD device.
1882 * @bdev: Meta data block device.
1883 */
1884static inline sector_t drbd_md_ss__(struct drbd_conf *mdev,
1885 struct drbd_backing_dev *bdev)
1886{
1887 switch (bdev->dc.meta_dev_idx) {
1888 default: /* external, some index */
1889 return MD_RESERVED_SECT * bdev->dc.meta_dev_idx;
1890 case DRBD_MD_INDEX_INTERNAL:
1891 /* with drbd08, internal meta data is always "flexible" */
1892 case DRBD_MD_INDEX_FLEX_INT:
1893 /* sizeof(struct md_on_disk_07) == 4k
1894 * position: last 4k aligned block of 4k size */
1895 if (!bdev->backing_bdev) {
1896 if (__ratelimit(&drbd_ratelimit_state)) {
1897 dev_err(DEV, "bdev->backing_bdev==NULL\n");
1898 dump_stack();
1899 }
1900 return 0;
1901 }
1902 return (drbd_get_capacity(bdev->backing_bdev) & ~7ULL)
1903 - MD_AL_OFFSET;
1904 case DRBD_MD_INDEX_FLEX_EXT:
1905 return 0;
1906 }
1907}
1908
1909static inline void
Philipp Reisnerb411b362009-09-25 16:07:19 -07001910drbd_queue_work_front(struct drbd_work_queue *q, struct drbd_work *w)
1911{
1912 unsigned long flags;
1913 spin_lock_irqsave(&q->q_lock, flags);
1914 list_add(&w->list, &q->q);
1915 up(&q->s); /* within the spinlock,
1916 see comment near end of drbd_worker() */
1917 spin_unlock_irqrestore(&q->q_lock, flags);
1918}
1919
1920static inline void
1921drbd_queue_work(struct drbd_work_queue *q, struct drbd_work *w)
1922{
1923 unsigned long flags;
1924 spin_lock_irqsave(&q->q_lock, flags);
1925 list_add_tail(&w->list, &q->q);
1926 up(&q->s); /* within the spinlock,
1927 see comment near end of drbd_worker() */
1928 spin_unlock_irqrestore(&q->q_lock, flags);
1929}
1930
1931static inline void wake_asender(struct drbd_conf *mdev)
1932{
1933 if (test_bit(SIGNAL_ASENDER, &mdev->flags))
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001934 force_sig(DRBD_SIG, mdev->tconn->asender.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001935}
1936
1937static inline void request_ping(struct drbd_conf *mdev)
1938{
1939 set_bit(SEND_PING, &mdev->flags);
1940 wake_asender(mdev);
1941}
1942
1943static inline int drbd_send_short_cmd(struct drbd_conf *mdev,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001944 enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001945{
Philipp Reisnerc0129492011-01-19 16:58:16 +01001946 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001947 return drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &h, sizeof(h));
1948}
1949
1950static inline int drbd_send_ping(struct drbd_conf *mdev)
1951{
Philipp Reisnerc0129492011-01-19 16:58:16 +01001952 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001953 return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING, &h, sizeof(h));
1954}
1955
1956static inline int drbd_send_ping_ack(struct drbd_conf *mdev)
1957{
Philipp Reisnerc0129492011-01-19 16:58:16 +01001958 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001959 return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING_ACK, &h, sizeof(h));
1960}
1961
1962static inline void drbd_thread_stop(struct drbd_thread *thi)
1963{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001964 _drbd_thread_stop(thi, false, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001965}
1966
1967static inline void drbd_thread_stop_nowait(struct drbd_thread *thi)
1968{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001969 _drbd_thread_stop(thi, false, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001970}
1971
1972static inline void drbd_thread_restart_nowait(struct drbd_thread *thi)
1973{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001974 _drbd_thread_stop(thi, true, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001975}
1976
1977/* counts how many answer packets packets we expect from our peer,
1978 * for either explicit application requests,
1979 * or implicit barrier packets as necessary.
1980 * increased:
1981 * w_send_barrier
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001982 * _req_mod(req, QUEUE_FOR_NET_WRITE or QUEUE_FOR_NET_READ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001983 * it is much easier and equally valid to count what we queue for the
1984 * worker, even before it actually was queued or send.
1985 * (drbd_make_request_common; recovery path on read io-error)
1986 * decreased:
1987 * got_BarrierAck (respective tl_clear, tl_clear_barrier)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001988 * _req_mod(req, DATA_RECEIVED)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001989 * [from receive_DataReply]
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001990 * _req_mod(req, WRITE_ACKED_BY_PEER or RECV_ACKED_BY_PEER or NEG_ACKED)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001991 * [from got_BlockAck (P_WRITE_ACK, P_RECV_ACK)]
1992 * for some reason it is NOT decreased in got_NegAck,
1993 * but in the resulting cleanup code from report_params.
1994 * we should try to remember the reason for that...
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001995 * _req_mod(req, SEND_FAILED or SEND_CANCELED)
1996 * _req_mod(req, CONNECTION_LOST_WHILE_PENDING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001997 * [from tl_clear_barrier]
1998 */
1999static inline void inc_ap_pending(struct drbd_conf *mdev)
2000{
2001 atomic_inc(&mdev->ap_pending_cnt);
2002}
2003
2004#define ERR_IF_CNT_IS_NEGATIVE(which) \
2005 if (atomic_read(&mdev->which) < 0) \
2006 dev_err(DEV, "in %s:%d: " #which " = %d < 0 !\n", \
2007 __func__ , __LINE__ , \
2008 atomic_read(&mdev->which))
2009
2010#define dec_ap_pending(mdev) do { \
2011 typecheck(struct drbd_conf *, mdev); \
2012 if (atomic_dec_and_test(&mdev->ap_pending_cnt)) \
2013 wake_up(&mdev->misc_wait); \
2014 ERR_IF_CNT_IS_NEGATIVE(ap_pending_cnt); } while (0)
2015
2016/* counts how many resync-related answers we still expect from the peer
2017 * increase decrease
2018 * C_SYNC_TARGET sends P_RS_DATA_REQUEST (and expects P_RS_DATA_REPLY)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002019 * C_SYNC_SOURCE sends P_RS_DATA_REPLY (and expects P_WRITE_ACK with ID_SYNCER)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002020 * (or P_NEG_ACK with ID_SYNCER)
2021 */
2022static inline void inc_rs_pending(struct drbd_conf *mdev)
2023{
2024 atomic_inc(&mdev->rs_pending_cnt);
2025}
2026
2027#define dec_rs_pending(mdev) do { \
2028 typecheck(struct drbd_conf *, mdev); \
2029 atomic_dec(&mdev->rs_pending_cnt); \
2030 ERR_IF_CNT_IS_NEGATIVE(rs_pending_cnt); } while (0)
2031
2032/* counts how many answers we still need to send to the peer.
2033 * increased on
2034 * receive_Data unless protocol A;
2035 * we need to send a P_RECV_ACK (proto B)
2036 * or P_WRITE_ACK (proto C)
2037 * receive_RSDataReply (recv_resync_read) we need to send a P_WRITE_ACK
2038 * receive_DataRequest (receive_RSDataRequest) we need to send back P_DATA
2039 * receive_Barrier_* we need to send a P_BARRIER_ACK
2040 */
2041static inline void inc_unacked(struct drbd_conf *mdev)
2042{
2043 atomic_inc(&mdev->unacked_cnt);
2044}
2045
2046#define dec_unacked(mdev) do { \
2047 typecheck(struct drbd_conf *, mdev); \
2048 atomic_dec(&mdev->unacked_cnt); \
2049 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2050
2051#define sub_unacked(mdev, n) do { \
2052 typecheck(struct drbd_conf *, mdev); \
2053 atomic_sub(n, &mdev->unacked_cnt); \
2054 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2055
2056
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002057static inline void put_net_conf(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002058{
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002059 if (atomic_dec_and_test(&tconn->net_cnt))
2060 wake_up(&tconn->net_cnt_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002061}
2062
2063/**
Philipp Reisner89e58e72011-01-19 13:12:45 +01002064 * get_net_conf() - Increase ref count on mdev->tconn->net_conf; Returns 0 if nothing there
Philipp Reisnerb411b362009-09-25 16:07:19 -07002065 * @mdev: DRBD device.
2066 *
Philipp Reisner89e58e72011-01-19 13:12:45 +01002067 * You have to call put_net_conf() when finished working with mdev->tconn->net_conf.
Philipp Reisnerb411b362009-09-25 16:07:19 -07002068 */
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002069static inline int get_net_conf(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002070{
2071 int have_net_conf;
2072
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002073 atomic_inc(&tconn->net_cnt);
2074 have_net_conf = tconn->volume0->state.conn >= C_UNCONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002075 if (!have_net_conf)
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002076 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002077 return have_net_conf;
2078}
2079
2080/**
2081 * get_ldev() - Increase the ref count on mdev->ldev. Returns 0 if there is no ldev
2082 * @M: DRBD device.
2083 *
2084 * You have to call put_ldev() when finished working with mdev->ldev.
2085 */
2086#define get_ldev(M) __cond_lock(local, _get_ldev_if_state(M,D_INCONSISTENT))
2087#define get_ldev_if_state(M,MINS) __cond_lock(local, _get_ldev_if_state(M,MINS))
2088
2089static inline void put_ldev(struct drbd_conf *mdev)
2090{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002091 int i = atomic_dec_return(&mdev->local_cnt);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02002092
2093 /* This may be called from some endio handler,
2094 * so we must not sleep here. */
2095
Philipp Reisnerb411b362009-09-25 16:07:19 -07002096 __release(local);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002097 D_ASSERT(i >= 0);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002098 if (i == 0) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002099 if (mdev->state.disk == D_DISKLESS)
2100 /* even internal references gone, safe to destroy */
2101 drbd_ldev_destroy(mdev);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002102 if (mdev->state.disk == D_FAILED)
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002103 /* all application IO references gone. */
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002104 drbd_go_diskless(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002105 wake_up(&mdev->misc_wait);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002106 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002107}
2108
2109#ifndef __CHECKER__
2110static inline int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
2111{
2112 int io_allowed;
2113
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002114 /* never get a reference while D_DISKLESS */
2115 if (mdev->state.disk == D_DISKLESS)
2116 return 0;
2117
Philipp Reisnerb411b362009-09-25 16:07:19 -07002118 atomic_inc(&mdev->local_cnt);
2119 io_allowed = (mdev->state.disk >= mins);
2120 if (!io_allowed)
2121 put_ldev(mdev);
2122 return io_allowed;
2123}
2124#else
2125extern int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins);
2126#endif
2127
2128/* you must have an "get_ldev" reference */
2129static inline void drbd_get_syncer_progress(struct drbd_conf *mdev,
2130 unsigned long *bits_left, unsigned int *per_mil_done)
2131{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01002132 /* this is to break it at compile time when we change that, in case we
2133 * want to support more than (1<<32) bits on a 32bit arch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002134 typecheck(unsigned long, mdev->rs_total);
2135
2136 /* note: both rs_total and rs_left are in bits, i.e. in
2137 * units of BM_BLOCK_SIZE.
2138 * for the percentage, we don't care. */
2139
Lars Ellenberg439d5952010-11-05 09:52:46 +01002140 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2141 *bits_left = mdev->ov_left;
2142 else
2143 *bits_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002144 /* >> 10 to prevent overflow,
2145 * +1 to prevent division by zero */
2146 if (*bits_left > mdev->rs_total) {
2147 /* doh. maybe a logic bug somewhere.
2148 * may also be just a race condition
2149 * between this and a disconnect during sync.
2150 * for now, just prevent in-kernel buffer overflow.
2151 */
2152 smp_rmb();
2153 dev_warn(DEV, "cs:%s rs_left=%lu > rs_total=%lu (rs_failed %lu)\n",
2154 drbd_conn_str(mdev->state.conn),
2155 *bits_left, mdev->rs_total, mdev->rs_failed);
2156 *per_mil_done = 0;
2157 } else {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01002158 /* Make sure the division happens in long context.
2159 * We allow up to one petabyte storage right now,
2160 * at a granularity of 4k per bit that is 2**38 bits.
2161 * After shift right and multiplication by 1000,
2162 * this should still fit easily into a 32bit long,
2163 * so we don't need a 64bit division on 32bit arch.
2164 * Note: currently we don't support such large bitmaps on 32bit
2165 * arch anyways, but no harm done to be prepared for it here.
2166 */
2167 unsigned int shift = mdev->rs_total >= (1ULL << 32) ? 16 : 10;
2168 unsigned long left = *bits_left >> shift;
2169 unsigned long total = 1UL + (mdev->rs_total >> shift);
2170 unsigned long tmp = 1000UL - left * 1000UL/total;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002171 *per_mil_done = tmp;
2172 }
2173}
2174
2175
2176/* this throttles on-the-fly application requests
2177 * according to max_buffers settings;
2178 * maybe re-implement using semaphores? */
2179static inline int drbd_get_max_buffers(struct drbd_conf *mdev)
2180{
2181 int mxb = 1000000; /* arbitrary limit on open requests */
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002182 if (get_net_conf(mdev->tconn)) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002183 mxb = mdev->tconn->net_conf->max_buffers;
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002184 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002185 }
2186 return mxb;
2187}
2188
Philipp Reisner37190942010-11-10 12:08:37 +01002189static inline int drbd_state_is_stable(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002190{
Philipp Reisner37190942010-11-10 12:08:37 +01002191 union drbd_state s = mdev->state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002192
2193 /* DO NOT add a default clause, we want the compiler to warn us
2194 * for any newly introduced state we may have forgotten to add here */
2195
2196 switch ((enum drbd_conns)s.conn) {
2197 /* new io only accepted when there is no connection, ... */
2198 case C_STANDALONE:
2199 case C_WF_CONNECTION:
2200 /* ... or there is a well established connection. */
2201 case C_CONNECTED:
2202 case C_SYNC_SOURCE:
2203 case C_SYNC_TARGET:
2204 case C_VERIFY_S:
2205 case C_VERIFY_T:
2206 case C_PAUSED_SYNC_S:
2207 case C_PAUSED_SYNC_T:
Philipp Reisner67531712010-10-27 12:21:30 +02002208 case C_AHEAD:
2209 case C_BEHIND:
Philipp Reisner37190942010-11-10 12:08:37 +01002210 /* transitional states, IO allowed */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002211 case C_DISCONNECTING:
2212 case C_UNCONNECTED:
2213 case C_TIMEOUT:
2214 case C_BROKEN_PIPE:
2215 case C_NETWORK_FAILURE:
2216 case C_PROTOCOL_ERROR:
2217 case C_TEAR_DOWN:
2218 case C_WF_REPORT_PARAMS:
2219 case C_STARTING_SYNC_S:
2220 case C_STARTING_SYNC_T:
Philipp Reisner37190942010-11-10 12:08:37 +01002221 break;
2222
2223 /* Allow IO in BM exchange states with new protocols */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002224 case C_WF_BITMAP_S:
Philipp Reisner31890f42011-01-19 14:12:51 +01002225 if (mdev->tconn->agreed_pro_version < 96)
Philipp Reisner37190942010-11-10 12:08:37 +01002226 return 0;
2227 break;
2228
2229 /* no new io accepted in these states */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002230 case C_WF_BITMAP_T:
2231 case C_WF_SYNC_UUID:
2232 case C_MASK:
2233 /* not "stable" */
2234 return 0;
2235 }
2236
2237 switch ((enum drbd_disk_state)s.disk) {
2238 case D_DISKLESS:
2239 case D_INCONSISTENT:
2240 case D_OUTDATED:
2241 case D_CONSISTENT:
2242 case D_UP_TO_DATE:
2243 /* disk state is stable as well. */
2244 break;
2245
2246 /* no new io accepted during tansitional states */
2247 case D_ATTACHING:
2248 case D_FAILED:
2249 case D_NEGOTIATING:
2250 case D_UNKNOWN:
2251 case D_MASK:
2252 /* not "stable" */
2253 return 0;
2254 }
2255
2256 return 1;
2257}
2258
Philipp Reisnerfb22c402010-09-08 23:20:21 +02002259static inline int is_susp(union drbd_state s)
2260{
2261 return s.susp || s.susp_nod || s.susp_fen;
2262}
2263
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002264static inline bool may_inc_ap_bio(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002265{
2266 int mxb = drbd_get_max_buffers(mdev);
2267
Philipp Reisnerfb22c402010-09-08 23:20:21 +02002268 if (is_susp(mdev->state))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002269 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002270 if (test_bit(SUSPEND_IO, &mdev->flags))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002271 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002272
2273 /* to avoid potential deadlock or bitmap corruption,
2274 * in various places, we only allow new application io
2275 * to start during "stable" states. */
2276
2277 /* no new io accepted when attaching or detaching the disk */
Philipp Reisner37190942010-11-10 12:08:37 +01002278 if (!drbd_state_is_stable(mdev))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002279 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002280
2281 /* since some older kernels don't have atomic_add_unless,
2282 * and we are within the spinlock anyways, we have this workaround. */
2283 if (atomic_read(&mdev->ap_bio_cnt) > mxb)
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002284 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002285 if (test_bit(BITMAP_IO, &mdev->flags))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002286 return false;
2287 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002288}
2289
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002290static inline bool inc_ap_bio_cond(struct drbd_conf *mdev, int count)
Philipp Reisner8869d682010-11-17 18:24:19 +01002291{
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002292 bool rv = false;
Philipp Reisner8869d682010-11-17 18:24:19 +01002293
Philipp Reisner87eeee42011-01-19 14:16:30 +01002294 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002295 rv = may_inc_ap_bio(mdev);
Philipp Reisner8869d682010-11-17 18:24:19 +01002296 if (rv)
2297 atomic_add(count, &mdev->ap_bio_cnt);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002298 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner8869d682010-11-17 18:24:19 +01002299
2300 return rv;
2301}
2302
Philipp Reisner9a25a042010-05-10 16:42:23 +02002303static inline void inc_ap_bio(struct drbd_conf *mdev, int count)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002304{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002305 /* we wait here
2306 * as long as the device is suspended
2307 * until the bitmap is no longer on the fly during connection
2308 * handshake as long as we would exeed the max_buffer limit.
2309 *
2310 * to avoid races with the reconnect code,
2311 * we need to atomic_inc within the spinlock. */
2312
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002313 wait_event(mdev->misc_wait, inc_ap_bio_cond(mdev, count));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002314}
2315
2316static inline void dec_ap_bio(struct drbd_conf *mdev)
2317{
2318 int mxb = drbd_get_max_buffers(mdev);
2319 int ap_bio = atomic_dec_return(&mdev->ap_bio_cnt);
2320
2321 D_ASSERT(ap_bio >= 0);
2322 /* this currently does wake_up for every dec_ap_bio!
2323 * maybe rather introduce some type of hysteresis?
2324 * e.g. (ap_bio == mxb/2 || ap_bio == 0) ? */
2325 if (ap_bio < mxb)
2326 wake_up(&mdev->misc_wait);
2327 if (ap_bio == 0 && test_bit(BITMAP_IO, &mdev->flags)) {
2328 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002329 drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002330 }
2331}
2332
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002333static inline int drbd_set_ed_uuid(struct drbd_conf *mdev, u64 val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002334{
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002335 int changed = mdev->ed_uuid != val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002336 mdev->ed_uuid = val;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002337 return changed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002338}
2339
Philipp Reisnerb411b362009-09-25 16:07:19 -07002340static inline int drbd_queue_order_type(struct drbd_conf *mdev)
2341{
2342 /* sorry, we currently have no working implementation
2343 * of distributed TCQ stuff */
2344#ifndef QUEUE_ORDERED_NONE
2345#define QUEUE_ORDERED_NONE 0
2346#endif
2347 return QUEUE_ORDERED_NONE;
2348}
2349
Philipp Reisnerb411b362009-09-25 16:07:19 -07002350static inline void drbd_md_flush(struct drbd_conf *mdev)
2351{
2352 int r;
2353
Philipp Reisnera8a4e512010-08-25 10:21:04 +02002354 if (test_bit(MD_NO_FUA, &mdev->flags))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002355 return;
2356
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02002357 r = blkdev_issue_flush(mdev->ldev->md_bdev, GFP_KERNEL, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002358 if (r) {
Philipp Reisnera8a4e512010-08-25 10:21:04 +02002359 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002360 dev_err(DEV, "meta data flush failed with status %d, disabling md-flushes\n", r);
2361 }
2362}
2363
2364#endif