blob: 7beb374451b3d3823a78352e0d6b03be8bfd5e77 [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 Reisnerb8907332011-01-27 14:07:51 +010046#include <linux/drbd.h>
47#include "drbd_state.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070048
49#ifdef __CHECKER__
50# define __protected_by(x) __attribute__((require_context(x,1,999,"rdwr")))
51# define __protected_read_by(x) __attribute__((require_context(x,1,999,"read")))
52# define __protected_write_by(x) __attribute__((require_context(x,1,999,"write")))
53# define __must_hold(x) __attribute__((context(x,1,1), require_context(x,1,999,"call")))
54#else
55# define __protected_by(x)
56# define __protected_read_by(x)
57# define __protected_write_by(x)
58# define __must_hold(x)
59#endif
60
61#define __no_warn(lock, stmt) do { __acquire(lock); stmt; __release(lock); } while (0)
62
63/* module parameter, defined in drbd_main.c */
64extern unsigned int minor_count;
65extern int disable_sendpage;
66extern int allow_oos;
67extern unsigned int cn_idx;
68
69#ifdef CONFIG_DRBD_FAULT_INJECTION
70extern int enable_faults;
71extern int fault_rate;
72extern int fault_devs;
73#endif
74
75extern char usermode_helper[];
76
77
Philipp Reisnerb411b362009-09-25 16:07:19 -070078/* I don't remember why XCPU ...
79 * This is used to wake the asender,
80 * and to interrupt sending the sending task
81 * on disconnect.
82 */
83#define DRBD_SIG SIGXCPU
84
85/* This is used to stop/restart our threads.
86 * Cannot use SIGTERM nor SIGKILL, since these
87 * are sent out by init on runlevel changes
88 * I choose SIGHUP for now.
89 */
90#define DRBD_SIGKILL SIGHUP
91
Philipp Reisnerb411b362009-09-25 16:07:19 -070092#define ID_IN_SYNC (4711ULL)
93#define ID_OUT_OF_SYNC (4712ULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -070094#define ID_SYNCER (-1ULL)
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +010095
Philipp Reisner4a23f262011-01-11 17:42:17 +010096#define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -070097
98struct drbd_conf;
Philipp Reisner21114382011-01-19 12:26:59 +010099struct drbd_tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700100
101
102/* to shorten dev_warn(DEV, "msg"); and relatives statements */
103#define DEV (disk_to_dev(mdev->vdisk))
104
105#define D_ASSERT(exp) if (!(exp)) \
106 dev_err(DEV, "ASSERT( " #exp " ) in %s:%d\n", __FILE__, __LINE__)
107
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100108/**
109 * expect - Make an assertion
110 *
111 * Unlike the assert macro, this macro returns a boolean result.
112 */
113#define expect(exp) ({ \
114 bool _bool = (exp); \
115 if (!_bool) \
116 dev_err(DEV, "ASSERTION %s FAILED in %s\n", \
117 #exp, __func__); \
118 _bool; \
119 })
Philipp Reisnerb411b362009-09-25 16:07:19 -0700120
121/* Defines to control fault insertion */
122enum {
123 DRBD_FAULT_MD_WR = 0, /* meta data write */
124 DRBD_FAULT_MD_RD = 1, /* read */
125 DRBD_FAULT_RS_WR = 2, /* resync */
126 DRBD_FAULT_RS_RD = 3,
127 DRBD_FAULT_DT_WR = 4, /* data */
128 DRBD_FAULT_DT_RD = 5,
129 DRBD_FAULT_DT_RA = 6, /* data read ahead */
130 DRBD_FAULT_BM_ALLOC = 7, /* bitmap allocation */
131 DRBD_FAULT_AL_EE = 8, /* alloc ee */
Philipp Reisner6b4388a2010-04-26 14:11:45 +0200132 DRBD_FAULT_RECEIVE = 9, /* Changes some bytes upon receiving a [rs]data block */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700133
134 DRBD_FAULT_MAX,
135};
136
Philipp Reisnerb411b362009-09-25 16:07:19 -0700137extern unsigned int
138_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100139
Philipp Reisnerb411b362009-09-25 16:07:19 -0700140static inline int
141drbd_insert_fault(struct drbd_conf *mdev, unsigned int type) {
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100142#ifdef CONFIG_DRBD_FAULT_INJECTION
Philipp Reisnerb411b362009-09-25 16:07:19 -0700143 return fault_rate &&
144 (enable_faults & (1<<type)) &&
145 _drbd_insert_fault(mdev, type);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700146#else
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100147 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700148#endif
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100149}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700150
151/* integer division, round _UP_ to the next integer */
152#define div_ceil(A, B) ((A)/(B) + ((A)%(B) ? 1 : 0))
153/* usual integer division */
154#define div_floor(A, B) ((A)/(B))
155
156/* drbd_meta-data.c (still in drbd_main.c) */
157/* 4th incarnation of the disk layout. */
158#define DRBD_MD_MAGIC (DRBD_MAGIC+4)
159
160extern struct drbd_conf **minor_table;
161extern struct ratelimit_state drbd_ratelimit_state;
162
163/* on the wire */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100164enum drbd_packet {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700165 /* receiver (data socket) */
166 P_DATA = 0x00,
167 P_DATA_REPLY = 0x01, /* Response to P_DATA_REQUEST */
168 P_RS_DATA_REPLY = 0x02, /* Response to P_RS_DATA_REQUEST */
169 P_BARRIER = 0x03,
170 P_BITMAP = 0x04,
171 P_BECOME_SYNC_TARGET = 0x05,
172 P_BECOME_SYNC_SOURCE = 0x06,
173 P_UNPLUG_REMOTE = 0x07, /* Used at various times to hint the peer */
174 P_DATA_REQUEST = 0x08, /* Used to ask for a data block */
175 P_RS_DATA_REQUEST = 0x09, /* Used to ask for a data block for resync */
176 P_SYNC_PARAM = 0x0a,
177 P_PROTOCOL = 0x0b,
178 P_UUIDS = 0x0c,
179 P_SIZES = 0x0d,
180 P_STATE = 0x0e,
181 P_SYNC_UUID = 0x0f,
182 P_AUTH_CHALLENGE = 0x10,
183 P_AUTH_RESPONSE = 0x11,
184 P_STATE_CHG_REQ = 0x12,
185
186 /* asender (meta socket */
187 P_PING = 0x13,
188 P_PING_ACK = 0x14,
189 P_RECV_ACK = 0x15, /* Used in protocol B */
190 P_WRITE_ACK = 0x16, /* Used in protocol C */
191 P_RS_WRITE_ACK = 0x17, /* Is a P_WRITE_ACK, additionally call set_in_sync(). */
192 P_DISCARD_ACK = 0x18, /* Used in proto C, two-primaries conflict detection */
193 P_NEG_ACK = 0x19, /* Sent if local disk is unusable */
194 P_NEG_DREPLY = 0x1a, /* Local disk is broken... */
195 P_NEG_RS_DREPLY = 0x1b, /* Local disk is broken... */
196 P_BARRIER_ACK = 0x1c,
197 P_STATE_CHG_REPLY = 0x1d,
198
199 /* "new" commands, no longer fitting into the ordering scheme above */
200
201 P_OV_REQUEST = 0x1e, /* data socket */
202 P_OV_REPLY = 0x1f,
203 P_OV_RESULT = 0x20, /* meta socket */
204 P_CSUM_RS_REQUEST = 0x21, /* data socket */
205 P_RS_IS_IN_SYNC = 0x22, /* meta socket */
206 P_SYNC_PARAM89 = 0x23, /* data socket, protocol version 89 replacement for P_SYNC_PARAM */
207 P_COMPRESSED_BITMAP = 0x24, /* compressed or otherwise encoded bitmap transfer */
Philipp Reisner0ced55a2010-04-30 15:26:20 +0200208 /* P_CKPT_FENCE_REQ = 0x25, * currently reserved for protocol D */
209 /* P_CKPT_DISABLE_REQ = 0x26, * currently reserved for protocol D */
210 P_DELAY_PROBE = 0x27, /* is used on BOTH sockets */
Philipp Reisner73a01a12010-10-27 14:33:00 +0200211 P_OUT_OF_SYNC = 0x28, /* Mark as out of sync (Outrunning), data socket */
Philipp Reisnerd612d302010-12-27 10:53:28 +0100212 P_RS_CANCEL = 0x29, /* meta: Used to cancel RS_DATA_REQUEST packet by SyncSource */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700213
Philipp Reisnerd612d302010-12-27 10:53:28 +0100214 P_MAX_CMD = 0x2A,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215 P_MAY_IGNORE = 0x100, /* Flag to test if (cmd > P_MAY_IGNORE) ... */
216 P_MAX_OPT_CMD = 0x101,
217
218 /* special command ids for handshake */
219
220 P_HAND_SHAKE_M = 0xfff1, /* First Packet on the MetaSock */
221 P_HAND_SHAKE_S = 0xfff2, /* First Packet on the Socket */
222
223 P_HAND_SHAKE = 0xfffe /* FIXED for the next century! */
224};
225
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100226extern const char *cmdname(enum drbd_packet cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700227
228/* for sending/receiving the bitmap,
229 * possibly in some encoding scheme */
230struct bm_xfer_ctx {
231 /* "const"
232 * stores total bits and long words
233 * of the bitmap, so we don't need to
234 * call the accessor functions over and again. */
235 unsigned long bm_bits;
236 unsigned long bm_words;
237 /* during xfer, current position within the bitmap */
238 unsigned long bit_offset;
239 unsigned long word_offset;
240
241 /* statistics; index: (h->command == P_BITMAP) */
242 unsigned packets[2];
243 unsigned bytes[2];
244};
245
246extern void INFO_bm_xfer_stats(struct drbd_conf *mdev,
247 const char *direction, struct bm_xfer_ctx *c);
248
249static inline void bm_xfer_ctx_bit_to_word_offset(struct bm_xfer_ctx *c)
250{
251 /* word_offset counts "native long words" (32 or 64 bit),
252 * aligned at 64 bit.
253 * Encoded packet may end at an unaligned bit offset.
254 * In case a fallback clear text packet is transmitted in
255 * between, we adjust this offset back to the last 64bit
256 * aligned "native long word", which makes coding and decoding
257 * the plain text bitmap much more convenient. */
258#if BITS_PER_LONG == 64
259 c->word_offset = c->bit_offset >> 6;
260#elif BITS_PER_LONG == 32
261 c->word_offset = c->bit_offset >> 5;
262 c->word_offset &= ~(1UL);
263#else
264# error "unsupported BITS_PER_LONG"
265#endif
266}
267
268#ifndef __packed
269#define __packed __attribute__((packed))
270#endif
271
272/* This is the layout for a packet on the wire.
273 * The byteorder is the network byte order.
274 * (except block_id and barrier fields.
275 * these are pointers to local structs
276 * and have no relevance for the partner,
277 * which just echoes them as received.)
278 *
279 * NOTE that the payload starts at a long aligned offset,
280 * regardless of 32 or 64 bit arch!
281 */
Philipp Reisner0b70a132010-08-20 13:36:10 +0200282struct p_header80 {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700283 u32 magic;
284 u16 command;
285 u16 length; /* bytes of data after this header */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700286} __packed;
Philipp Reisner0b70a132010-08-20 13:36:10 +0200287
288/* Header for big packets, Used for data packets exceeding 64kB */
289struct p_header95 {
290 u16 magic; /* use DRBD_MAGIC_BIG here */
291 u16 command;
Philipp Reisner00b42532010-10-05 11:19:39 +0200292 u32 length; /* Use only 24 bits of that. Ignore the highest 8 bit. */
Philipp Reisner0b70a132010-08-20 13:36:10 +0200293} __packed;
294
Philipp Reisnerc0129492011-01-19 16:58:16 +0100295struct p_header {
296 union {
297 struct p_header80 h80;
298 struct p_header95 h95;
299 };
300 u8 payload[0];
Philipp Reisner0b70a132010-08-20 13:36:10 +0200301};
Philipp Reisnerb411b362009-09-25 16:07:19 -0700302
303/*
304 * short commands, packets without payload, plain p_header:
305 * P_PING
306 * P_PING_ACK
307 * P_BECOME_SYNC_TARGET
308 * P_BECOME_SYNC_SOURCE
309 * P_UNPLUG_REMOTE
310 */
311
312/*
313 * commands with out-of-struct payload:
314 * P_BITMAP (no additional fields)
315 * P_DATA, P_DATA_REPLY (see p_data)
316 * P_COMPRESSED_BITMAP (see receive_compressed_bitmap)
317 */
318
319/* these defines must not be changed without changing the protocol version */
Philipp Reisner76d2e7e2010-08-25 11:58:05 +0200320#define DP_HARDBARRIER 1 /* depricated */
321#define DP_RW_SYNC 2 /* equals REQ_SYNC */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700322#define DP_MAY_SET_IN_SYNC 4
Jens Axboe721a9602011-03-09 11:56:30 +0100323#define DP_UNPLUG 8 /* not used anymore */
Philipp Reisner76d2e7e2010-08-25 11:58:05 +0200324#define DP_FUA 16 /* equals REQ_FUA */
325#define DP_FLUSH 32 /* equals REQ_FLUSH */
326#define DP_DISCARD 64 /* equals REQ_DISCARD */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700327
328struct p_data {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100329 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700330 u64 sector; /* 64 bits sector number */
331 u64 block_id; /* to identify the request in protocol B&C */
332 u32 seq_num;
333 u32 dp_flags;
334} __packed;
335
336/*
337 * commands which share a struct:
338 * p_block_ack:
339 * P_RECV_ACK (proto B), P_WRITE_ACK (proto C),
340 * P_DISCARD_ACK (proto C, two-primaries conflict detection)
341 * p_block_req:
342 * P_DATA_REQUEST, P_RS_DATA_REQUEST
343 */
344struct p_block_ack {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100345 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700346 u64 sector;
347 u64 block_id;
348 u32 blksize;
349 u32 seq_num;
350} __packed;
351
352
353struct p_block_req {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100354 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700355 u64 sector;
356 u64 block_id;
357 u32 blksize;
358 u32 pad; /* to multiple of 8 Byte */
359} __packed;
360
361/*
362 * commands with their own struct for additional fields:
363 * P_HAND_SHAKE
364 * P_BARRIER
365 * P_BARRIER_ACK
366 * P_SYNC_PARAM
367 * ReportParams
368 */
369
370struct p_handshake {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100371 struct p_header head; /* Note: You must always use a h80 here */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700372 u32 protocol_min;
373 u32 feature_flags;
374 u32 protocol_max;
375
376 /* should be more than enough for future enhancements
377 * for now, feature_flags and the reserverd array shall be zero.
378 */
379
380 u32 _pad;
381 u64 reserverd[7];
382} __packed;
383/* 80 bytes, FIXED for the next century */
384
385struct p_barrier {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100386 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700387 u32 barrier; /* barrier number _handle_ only */
388 u32 pad; /* to multiple of 8 Byte */
389} __packed;
390
391struct p_barrier_ack {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100392 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700393 u32 barrier;
394 u32 set_size;
395} __packed;
396
397struct p_rs_param {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100398 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700399 u32 rate;
400
401 /* Since protocol version 88 and higher. */
402 char verify_alg[0];
403} __packed;
404
405struct p_rs_param_89 {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100406 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700407 u32 rate;
408 /* protocol version 89: */
409 char verify_alg[SHARED_SECRET_MAX];
410 char csums_alg[SHARED_SECRET_MAX];
411} __packed;
412
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200413struct p_rs_param_95 {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100414 struct p_header head;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200415 u32 rate;
416 char verify_alg[SHARED_SECRET_MAX];
417 char csums_alg[SHARED_SECRET_MAX];
418 u32 c_plan_ahead;
419 u32 c_delay_target;
420 u32 c_fill_target;
421 u32 c_max_rate;
422} __packed;
423
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100424enum drbd_conn_flags {
425 CF_WANT_LOSE = 1,
426 CF_DRY_RUN = 2,
427};
428
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429struct p_protocol {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100430 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700431 u32 protocol;
432 u32 after_sb_0p;
433 u32 after_sb_1p;
434 u32 after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100435 u32 conn_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700436 u32 two_primaries;
437
438 /* Since protocol version 87 and higher. */
439 char integrity_alg[0];
440
441} __packed;
442
443struct p_uuids {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100444 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700445 u64 uuid[UI_EXTENDED_SIZE];
446} __packed;
447
448struct p_rs_uuid {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100449 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700450 u64 uuid;
451} __packed;
452
453struct p_sizes {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100454 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455 u64 d_size; /* size of disk */
456 u64 u_size; /* user requested size */
457 u64 c_size; /* current exported size */
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100458 u32 max_bio_size; /* Maximal size of a BIO */
Philipp Reisnere89b5912010-03-24 17:11:33 +0100459 u16 queue_order_type; /* not yet implemented in DRBD*/
460 u16 dds_flags; /* use enum dds_flags here. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700461} __packed;
462
463struct p_state {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100464 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700465 u32 state;
466} __packed;
467
468struct p_req_state {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100469 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700470 u32 mask;
471 u32 val;
472} __packed;
473
474struct p_req_state_reply {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100475 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700476 u32 retcode;
477} __packed;
478
479struct p_drbd06_param {
480 u64 size;
481 u32 state;
482 u32 blksize;
483 u32 protocol;
484 u32 version;
485 u32 gen_cnt[5];
486 u32 bit_map_gen[5];
487} __packed;
488
489struct p_discard {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100490 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700491 u64 block_id;
492 u32 seq_num;
493 u32 pad;
494} __packed;
495
Philipp Reisner73a01a12010-10-27 14:33:00 +0200496struct p_block_desc {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100497 struct p_header head;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200498 u64 sector;
499 u32 blksize;
500 u32 pad; /* to multiple of 8 Byte */
501} __packed;
502
Philipp Reisnerb411b362009-09-25 16:07:19 -0700503/* Valid values for the encoding field.
504 * Bump proto version when changing this. */
505enum drbd_bitmap_code {
506 /* RLE_VLI_Bytes = 0,
507 * and other bit variants had been defined during
508 * algorithm evaluation. */
509 RLE_VLI_Bits = 2,
510};
511
512struct p_compressed_bm {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100513 struct p_header head;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700514 /* (encoding & 0x0f): actual encoding, see enum drbd_bitmap_code
515 * (encoding & 0x80): polarity (set/unset) of first runlength
516 * ((encoding >> 4) & 0x07): pad_bits, number of trailing zero bits
517 * used to pad up to head.length bytes
518 */
519 u8 encoding;
520
521 u8 code[0];
522} __packed;
523
Philipp Reisner0b70a132010-08-20 13:36:10 +0200524struct p_delay_probe93 {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100525 struct p_header head;
Philipp Reisner0b70a132010-08-20 13:36:10 +0200526 u32 seq_num; /* sequence number to match the two probe packets */
527 u32 offset; /* usecs the probe got sent after the reference time point */
Philipp Reisner0ced55a2010-04-30 15:26:20 +0200528} __packed;
529
Philipp Reisnerb411b362009-09-25 16:07:19 -0700530/* DCBP: Drbd Compressed Bitmap Packet ... */
531static inline enum drbd_bitmap_code
532DCBP_get_code(struct p_compressed_bm *p)
533{
534 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
535}
536
537static inline void
538DCBP_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
539{
540 BUG_ON(code & ~0xf);
541 p->encoding = (p->encoding & ~0xf) | code;
542}
543
544static inline int
545DCBP_get_start(struct p_compressed_bm *p)
546{
547 return (p->encoding & 0x80) != 0;
548}
549
550static inline void
551DCBP_set_start(struct p_compressed_bm *p, int set)
552{
553 p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
554}
555
556static inline int
557DCBP_get_pad_bits(struct p_compressed_bm *p)
558{
559 return (p->encoding >> 4) & 0x7;
560}
561
562static inline void
563DCBP_set_pad_bits(struct p_compressed_bm *p, int n)
564{
565 BUG_ON(n & ~0x7);
566 p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
567}
568
569/* one bitmap packet, including the p_header,
570 * should fit within one _architecture independend_ page.
571 * so we need to use the fixed size 4KiB page size
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300572 * most architectures have used for a long time.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573 */
Philipp Reisnerc0129492011-01-19 16:58:16 +0100574#define BM_PACKET_PAYLOAD_BYTES (4096 - sizeof(struct p_header))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575#define BM_PACKET_WORDS (BM_PACKET_PAYLOAD_BYTES/sizeof(long))
576#define BM_PACKET_VLI_BYTES_MAX (4096 - sizeof(struct p_compressed_bm))
577#if (PAGE_SIZE < 4096)
578/* drbd_send_bitmap / receive_bitmap would break horribly */
579#error "PAGE_SIZE too small"
580#endif
581
582union p_polymorph {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100583 struct p_header header;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584 struct p_handshake handshake;
585 struct p_data data;
586 struct p_block_ack block_ack;
587 struct p_barrier barrier;
588 struct p_barrier_ack barrier_ack;
589 struct p_rs_param_89 rs_param_89;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200590 struct p_rs_param_95 rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700591 struct p_protocol protocol;
592 struct p_sizes sizes;
593 struct p_uuids uuids;
594 struct p_state state;
595 struct p_req_state req_state;
596 struct p_req_state_reply req_state_reply;
597 struct p_block_req block_req;
Philipp Reisner02918be2010-08-20 14:35:10 +0200598 struct p_delay_probe93 delay_probe93;
599 struct p_rs_uuid rs_uuid;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200600 struct p_block_desc block_desc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700601} __packed;
602
603/**********************************************************************/
604enum drbd_thread_state {
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100605 NONE,
606 RUNNING,
607 EXITING,
608 RESTARTING
Philipp Reisnerb411b362009-09-25 16:07:19 -0700609};
610
611struct drbd_thread {
612 spinlock_t t_lock;
613 struct task_struct *task;
614 struct completion stop;
615 enum drbd_thread_state t_state;
616 int (*function) (struct drbd_thread *);
617 struct drbd_conf *mdev;
618 int reset_cpu_mask;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100619 char name[9];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700620};
621
622static inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
623{
624 /* THINK testing the t_state seems to be uncritical in all cases
625 * (but thread_{start,stop}), so we can read it *without* the lock.
626 * --lge */
627
628 smp_rmb();
629 return thi->t_state;
630}
631
Philipp Reisnerb411b362009-09-25 16:07:19 -0700632struct drbd_work;
633typedef int (*drbd_work_cb)(struct drbd_conf *, struct drbd_work *, int cancel);
634struct drbd_work {
635 struct list_head list;
636 drbd_work_cb cb;
637};
638
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100639#include "drbd_interval.h"
640
Philipp Reisnerb411b362009-09-25 16:07:19 -0700641struct drbd_request {
642 struct drbd_work w;
643 struct drbd_conf *mdev;
644
645 /* if local IO is not allowed, will be NULL.
646 * if local IO _is_ allowed, holds the locally submitted bio clone,
647 * or, after local IO completion, the ERR_PTR(error).
648 * see drbd_endio_pri(). */
649 struct bio *private_bio;
650
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100651 struct drbd_interval i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700652 unsigned int epoch; /* barrier_nr */
653
654 /* barrier_nr: used to check on "completion" whether this req was in
655 * the current epoch, and we therefore have to close it,
656 * starting a new epoch...
657 */
658
Philipp Reisnerb411b362009-09-25 16:07:19 -0700659 struct list_head tl_requests; /* ring list in the transfer log */
660 struct bio *master_bio; /* master bio pointer */
661 unsigned long rq_state; /* see comments above _req_mod() */
662 int seq_num;
663 unsigned long start_time;
664};
665
666struct drbd_tl_epoch {
667 struct drbd_work w;
668 struct list_head requests; /* requests before */
669 struct drbd_tl_epoch *next; /* pointer to the next barrier */
670 unsigned int br_number; /* the barriers identifier. */
Philipp Reisner7e602c02010-05-27 14:49:27 +0200671 int n_writes; /* number of requests attached before this barrier */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700672};
673
Philipp Reisnerb411b362009-09-25 16:07:19 -0700674struct drbd_epoch {
675 struct list_head list;
676 unsigned int barrier_nr;
677 atomic_t epoch_size; /* increased on every request added. */
678 atomic_t active; /* increased on every req. added, and dec on every finished. */
679 unsigned long flags;
680};
681
682/* drbd_epoch flag bits */
683enum {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700684 DE_HAVE_BARRIER_NUMBER,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700685};
686
687enum epoch_event {
688 EV_PUT,
689 EV_GOT_BARRIER_NR,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690 EV_BECAME_LAST,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700691 EV_CLEANUP = 32, /* used as flag */
692};
693
Philipp Reisnerb411b362009-09-25 16:07:19 -0700694struct drbd_wq_barrier {
695 struct drbd_work w;
696 struct completion done;
697};
698
699struct digest_info {
700 int digest_size;
701 void *digest;
702};
703
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100704struct drbd_peer_request {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200705 struct drbd_work w;
Philipp Reisner85719572010-07-21 10:20:17 +0200706 struct drbd_epoch *epoch; /* for writes */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200707 struct drbd_conf *mdev;
708 struct page *pages;
709 atomic_t pending_bios;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +0100710 struct drbd_interval i;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200711 /* see comments on ee flag bits below */
712 unsigned long flags;
Philipp Reisner85719572010-07-21 10:20:17 +0200713 union {
714 u64 block_id;
715 struct digest_info *digest;
716 };
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200717};
718
719/* ee flag bits.
720 * While corresponding bios are in flight, the only modification will be
721 * set_bit WAS_ERROR, which has to be atomic.
722 * If no bios are in flight yet, or all have been completed,
723 * non-atomic modification to ee->flags is ok.
724 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725enum {
726 __EE_CALL_AL_COMPLETE_IO,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700727 __EE_MAY_SET_IN_SYNC,
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200728
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200729 /* In case a barrier failed,
730 * we need to resubmit without the barrier flag. */
731 __EE_RESUBMITTED,
732
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +0100733 /* we may have several bios per peer request.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200734 * if any of those fail, we set this flag atomically
735 * from the endio callback */
736 __EE_WAS_ERROR,
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200737
738 /* This ee has a pointer to a digest instead of a block id */
739 __EE_HAS_DIGEST,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740};
741#define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700742#define EE_MAY_SET_IN_SYNC (1<<__EE_MAY_SET_IN_SYNC)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200743#define EE_RESUBMITTED (1<<__EE_RESUBMITTED)
744#define EE_WAS_ERROR (1<<__EE_WAS_ERROR)
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200745#define EE_HAS_DIGEST (1<<__EE_HAS_DIGEST)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700746
747/* global flag bits */
748enum {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300749 CREATE_BARRIER, /* next P_DATA is preceded by a P_BARRIER */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700750 SIGNAL_ASENDER, /* whether asender wants to be interrupted */
751 SEND_PING, /* whether asender should send a ping asap */
752
Philipp Reisnerb411b362009-09-25 16:07:19 -0700753 UNPLUG_QUEUED, /* only relevant with kernel 2.4 */
754 UNPLUG_REMOTE, /* sending a "UnplugRemote" could help */
755 MD_DIRTY, /* current uuids and flags not yet on disk */
756 DISCARD_CONCURRENT, /* Set on one node, cleared on the peer! */
757 USE_DEGR_WFC_T, /* degr-wfc-timeout instead of wfc-timeout. */
758 CLUSTER_ST_CHANGE, /* Cluster wide state change going on... */
759 CL_ST_CHG_SUCCESS,
760 CL_ST_CHG_FAIL,
761 CRASHED_PRIMARY, /* This node was a crashed primary.
762 * Gets cleared when the state.conn
763 * goes into C_CONNECTED state. */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100764 NO_BARRIER_SUPP, /* underlying block device doesn't implement barriers */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700765 CONSIDER_RESYNC,
766
Philipp Reisnera8a4e512010-08-25 10:21:04 +0200767 MD_NO_FUA, /* Users wants us to not use FUA/FLUSH on meta data dev */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700768 SUSPEND_IO, /* suspend application io */
769 BITMAP_IO, /* suspend application io;
770 once no more io in flight, start bitmap io */
771 BITMAP_IO_QUEUED, /* Started bitmap IO */
Lars Ellenberg82f59cc2010-10-16 12:13:47 +0200772 GO_DISKLESS, /* Disk is being detached, on io-error or admin request. */
773 WAS_IO_ERROR, /* Local disk failed returned IO error */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700774 RESYNC_AFTER_NEG, /* Resync after online grow after the attach&negotiate finished. */
775 NET_CONGESTED, /* The data socket is congested */
776
777 CONFIG_PENDING, /* serialization of (re)configuration requests.
778 * if set, also prevents the device from dying */
779 DEVICE_DYING, /* device became unconfigured,
780 * but worker thread is still handling the cleanup.
781 * reconfiguring (nl_disk_conf, nl_net_conf) is dissalowed,
782 * while this is set. */
783 RESIZE_PENDING, /* Size change detected locally, waiting for the response from
784 * the peer, if it changed there as well. */
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100785 CONN_DRY_RUN, /* Expect disconnect after resync handshake. */
Philipp Reisner309d1602010-03-02 15:03:44 +0100786 GOT_PING_ACK, /* set when we receive a ping_ack packet, misc wait gets woken */
Philipp Reisner43a51822010-06-11 11:26:34 +0200787 NEW_CUR_UUID, /* Create new current UUID when thawing IO */
Philipp Reisner07782862010-08-31 12:00:50 +0200788 AL_SUSPENDED, /* Activity logging is currently suspended. */
Philipp Reisner370a43e2011-01-14 16:03:11 +0100789 AHEAD_TO_SYNC_SOURCE, /* Ahead -> SyncSource queued */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790};
791
792struct drbd_bitmap; /* opaque for drbd_conf */
793
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100794/* definition of bits in bm_flags to be used in drbd_bm_lock
795 * and drbd_bitmap_io and friends. */
796enum bm_flag {
797 /* do we need to kfree, or vfree bm_pages? */
798 BM_P_VMALLOCED = 0x10000, /* internal use only, will be masked out */
799
800 /* currently locked for bulk operation */
801 BM_LOCKED_MASK = 0x7,
802
803 /* in detail, that is: */
804 BM_DONT_CLEAR = 0x1,
805 BM_DONT_SET = 0x2,
806 BM_DONT_TEST = 0x4,
807
808 /* (test bit, count bit) allowed (common case) */
809 BM_LOCKED_TEST_ALLOWED = 0x3,
810
811 /* testing bits, as well as setting new bits allowed, but clearing bits
812 * would be unexpected. Used during bitmap receive. Setting new bits
813 * requires sending of "out-of-sync" information, though. */
814 BM_LOCKED_SET_ALLOWED = 0x1,
815
816 /* clear is not expected while bitmap is locked for bulk operation */
817};
818
819
Philipp Reisnerb411b362009-09-25 16:07:19 -0700820/* TODO sort members for performance
821 * MAYBE group them further */
822
823/* THINK maybe we actually want to use the default "event/%s" worker threads
824 * or similar in linux 2.6, which uses per cpu data and threads.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700825 */
826struct drbd_work_queue {
827 struct list_head q;
828 struct semaphore s; /* producers up it, worker down()s it */
829 spinlock_t q_lock; /* to protect the list. */
830};
831
832struct drbd_socket {
833 struct drbd_work_queue work;
834 struct mutex mutex;
835 struct socket *socket;
836 /* this way we get our
837 * send/receive buffers off the stack */
838 union p_polymorph sbuf;
839 union p_polymorph rbuf;
840};
841
842struct drbd_md {
843 u64 md_offset; /* sector offset to 'super' block */
844
845 u64 la_size_sect; /* last agreed size, unit sectors */
846 u64 uuid[UI_SIZE];
847 u64 device_uuid;
848 u32 flags;
849 u32 md_size_sect;
850
851 s32 al_offset; /* signed relative sector offset to al area */
852 s32 bm_offset; /* signed relative sector offset to bitmap */
853
854 /* u32 al_nr_extents; important for restoring the AL
855 * is stored into sync_conf.al_extents, which in turn
856 * gets applied to act_log->nr_elements
857 */
858};
859
860/* for sync_conf and other types... */
861#define NL_PACKET(name, number, fields) struct name { fields };
862#define NL_INTEGER(pn,pr,member) int member;
863#define NL_INT64(pn,pr,member) __u64 member;
864#define NL_BIT(pn,pr,member) unsigned member:1;
865#define NL_STRING(pn,pr,member,len) unsigned char member[len]; int member ## _len;
866#include "linux/drbd_nl.h"
867
868struct drbd_backing_dev {
869 struct block_device *backing_bdev;
870 struct block_device *md_bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700871 struct drbd_md md;
872 struct disk_conf dc; /* The user provided config... */
873 sector_t known_size; /* last known size of that backing device */
874};
875
876struct drbd_md_io {
877 struct drbd_conf *mdev;
878 struct completion event;
879 int error;
880};
881
882struct bm_io_work {
883 struct drbd_work w;
884 char *why;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100885 enum bm_flag flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700886 int (*io_fn)(struct drbd_conf *mdev);
887 void (*done)(struct drbd_conf *mdev, int rv);
888};
889
890enum write_ordering_e {
891 WO_none,
892 WO_drain_io,
893 WO_bdev_flush,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894};
895
Philipp Reisner778f2712010-07-06 11:14:00 +0200896struct fifo_buffer {
897 int *values;
898 unsigned int head_index;
899 unsigned int size;
900};
901
Philipp Reisner21114382011-01-19 12:26:59 +0100902struct drbd_tconn { /* is a resource from the config file */
903 char *name; /* Resource name */
904 struct list_head all_tconn; /* List of all drbd_tconn, prot by global_state_lock */
905 struct drbd_conf *volume0; /* TODO: Remove me again */
906
907 struct net_conf *net_conf; /* protected by get_net_conf() and put_net_conf() */
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100908 atomic_t net_cnt; /* Users of net_conf */
909 wait_queue_head_t net_cnt_wait;
Philipp Reisnere42325a2011-01-19 13:55:45 +0100910
911 struct drbd_socket data; /* data/barrier/cstate/parameter packets */
912 struct drbd_socket meta; /* ping/ack (metadata) packets */
Philipp Reisner31890f42011-01-19 14:12:51 +0100913 int agreed_pro_version; /* actually used protocol version */
914 unsigned long last_received; /* in jiffies, either socket */
915 unsigned int ko_count;
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100916
Philipp Reisner87eeee42011-01-19 14:16:30 +0100917 spinlock_t req_lock;
918 struct drbd_tl_epoch *unused_spare_tle; /* for pre-allocation */
919 struct drbd_tl_epoch *newest_tle;
920 struct drbd_tl_epoch *oldest_tle;
921 struct list_head out_of_sequence_requests;
922
Philipp Reisnera0638452011-01-19 14:31:32 +0100923 struct crypto_hash *cram_hmac_tfm;
924 struct crypto_hash *integrity_w_tfm; /* to be used by the worker thread */
925 struct crypto_hash *integrity_r_tfm; /* to be used by the receiver thread */
926 void *int_dig_out;
927 void *int_dig_in;
928 void *int_dig_vv;
929
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100930 struct drbd_thread receiver;
931 struct drbd_thread worker;
932 struct drbd_thread asender;
Philipp Reisner21114382011-01-19 12:26:59 +0100933};
934
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935struct drbd_conf {
Philipp Reisner21114382011-01-19 12:26:59 +0100936 struct drbd_tconn *tconn;
937 int vnr; /* volume number within the connection */
938
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 /* things that are stored as / read from meta data on disk */
940 unsigned long flags;
941
942 /* configured by drbdsetup */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700943 struct syncer_conf sync_conf;
944 struct drbd_backing_dev *ldev __protected_by(local);
945
946 sector_t p_size; /* partner's disk size */
947 struct request_queue *rq_queue;
948 struct block_device *this_bdev;
949 struct gendisk *vdisk;
950
Philipp Reisnerb411b362009-09-25 16:07:19 -0700951 struct drbd_work resync_work,
952 unplug_work,
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +0200953 go_diskless,
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200954 md_sync_work,
955 start_resync_work;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700956 struct timer_list resync_timer;
957 struct timer_list md_sync_timer;
Philipp Reisner370a43e2011-01-14 16:03:11 +0100958 struct timer_list start_resync_timer;
Philipp Reisner7fde2be2011-03-01 11:08:28 +0100959 struct timer_list request_timer;
Lars Ellenbergee15b032010-09-03 10:00:09 +0200960#ifdef DRBD_DEBUG_MD_SYNC
961 struct {
962 unsigned int line;
963 const char* func;
964 } last_md_mark_dirty;
965#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966
967 /* Used after attach while negotiating new disk state. */
968 union drbd_state new_state_tmp;
969
970 union drbd_state state;
971 wait_queue_head_t misc_wait;
972 wait_queue_head_t state_wait; /* upon each state change. */
973 unsigned int send_cnt;
974 unsigned int recv_cnt;
975 unsigned int read_cnt;
976 unsigned int writ_cnt;
977 unsigned int al_writ_cnt;
978 unsigned int bm_writ_cnt;
979 atomic_t ap_bio_cnt; /* Requests we need to complete */
980 atomic_t ap_pending_cnt; /* AP data packets on the wire, ack expected */
981 atomic_t rs_pending_cnt; /* RS request/data packets on the wire */
982 atomic_t unacked_cnt; /* Need to send replys for */
983 atomic_t local_cnt; /* Waiting for local completion */
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100984
Andreas Gruenbacherdac13892011-01-21 17:18:39 +0100985 /* Interval tree of pending local requests */
986 struct rb_root read_requests;
Andreas Gruenbacherde696712011-01-20 15:00:24 +0100987 struct rb_root write_requests;
988
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100989 /* blocks to resync in this run [unit BM_BLOCK_SIZE] */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700990 unsigned long rs_total;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100991 /* number of resync blocks that failed in this run */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700992 unsigned long rs_failed;
993 /* Syncer's start time [unit jiffies] */
994 unsigned long rs_start;
995 /* cumulated time in PausedSyncX state [unit jiffies] */
996 unsigned long rs_paused;
Lars Ellenberg1d7734a02010-08-11 21:21:50 +0200997 /* skipped because csum was equal [unit BM_BLOCK_SIZE] */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700998 unsigned long rs_same_csum;
Lars Ellenberg1d7734a02010-08-11 21:21:50 +0200999#define DRBD_SYNC_MARKS 8
1000#define DRBD_SYNC_MARK_STEP (3*HZ)
1001 /* block not up-to-date at mark [unit BM_BLOCK_SIZE] */
1002 unsigned long rs_mark_left[DRBD_SYNC_MARKS];
1003 /* marks's time [unit jiffies] */
1004 unsigned long rs_mark_time[DRBD_SYNC_MARKS];
1005 /* current index into rs_mark_{left,time} */
1006 int rs_last_mark;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001007
1008 /* where does the admin want us to start? (sector) */
1009 sector_t ov_start_sector;
1010 /* where are we now? (sector) */
1011 sector_t ov_position;
1012 /* Start sector of out of sync range (to merge printk reporting). */
1013 sector_t ov_last_oos_start;
1014 /* size of out-of-sync range in sectors. */
1015 sector_t ov_last_oos_size;
1016 unsigned long ov_left; /* in bits */
1017 struct crypto_hash *csums_tfm;
1018 struct crypto_hash *verify_tfm;
1019
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020 struct drbd_bitmap *bitmap;
1021 unsigned long bm_resync_fo; /* bit offset for drbd_bm_find_next */
1022
1023 /* Used to track operations of resync... */
1024 struct lru_cache *resync;
1025 /* Number of locked elements in resync LRU */
1026 unsigned int resync_locked;
1027 /* resync extent number waiting for application requests */
1028 unsigned int resync_wenr;
1029
1030 int open_cnt;
1031 u64 *p_uuid;
1032 struct drbd_epoch *current_epoch;
1033 spinlock_t epoch_lock;
1034 unsigned int epochs;
1035 enum write_ordering_e write_ordering;
Philipp Reisner85719572010-07-21 10:20:17 +02001036 struct list_head active_ee; /* IO in progress (P_DATA gets written to disk) */
1037 struct list_head sync_ee; /* IO in progress (P_RS_DATA_REPLY gets written to disk) */
Andreas Gruenbacher18b75d72011-02-04 15:36:22 +01001038 struct list_head done_ee; /* need to send P_WRITE_ACK */
1039 struct list_head read_ee; /* [RS]P_DATA_REQUEST being read */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001040 struct list_head net_ee; /* zero-copy network send in progress */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001041
Philipp Reisnerb411b362009-09-25 16:07:19 -07001042 int next_barrier_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043 struct list_head resync_reads;
Lars Ellenberg435f0742010-09-06 12:30:25 +02001044 atomic_t pp_in_use; /* allocated from page pool */
1045 atomic_t pp_in_use_by_net; /* sendpage()d, still referenced by tcp */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046 wait_queue_head_t ee_wait;
1047 struct page *md_io_page; /* one page buffer for md_io */
1048 struct page *md_io_tmpp; /* for logical_block_size != 512 */
1049 struct mutex md_io_mutex; /* protects the md_io_buffer */
1050 spinlock_t al_lock;
1051 wait_queue_head_t al_wait;
1052 struct lru_cache *act_log; /* activity log */
1053 unsigned int al_tr_number;
1054 int al_tr_cycle;
1055 int al_tr_pos; /* position of the next transaction in the journal */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056 wait_queue_head_t seq_wait;
1057 atomic_t packet_seq;
1058 unsigned int peer_seq;
1059 spinlock_t peer_seq_lock;
1060 unsigned int minor;
1061 unsigned long comm_bm_set; /* communicated number of set bits. */
1062 cpumask_var_t cpu_mask;
1063 struct bm_io_work bm_io_work;
1064 u64 ed_uuid; /* UUID of the exposed data */
1065 struct mutex state_mutex;
1066 char congestion_reason; /* Why we where congested... */
Lars Ellenberg1d7734a02010-08-11 21:21:50 +02001067 atomic_t rs_sect_in; /* for incoming resync data rate, SyncTarget */
1068 atomic_t rs_sect_ev; /* for submitted resync data rate, both */
1069 int rs_last_sect_ev; /* counter to compare with */
1070 int rs_last_events; /* counter of read or write "events" (unit sectors)
1071 * on the lower level device when we last looked. */
1072 int c_sync_rate; /* current resync rate after syncer throttle magic */
Philipp Reisner778f2712010-07-06 11:14:00 +02001073 struct fifo_buffer rs_plan_s; /* correction values of resync planer */
1074 int rs_in_flight; /* resync sectors in flight (to proxy, in proxy and from proxy) */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001075 int rs_planed; /* resync sectors already planned */
Philipp Reisner759fbdf2010-10-26 16:02:27 +02001076 atomic_t ap_in_flight; /* App sectors in flight (waiting for ack) */
Philipp Reisner99432fc2011-05-20 16:39:13 +02001077 int peer_max_bio_size;
1078 int local_max_bio_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001079};
1080
1081static inline struct drbd_conf *minor_to_mdev(unsigned int minor)
1082{
1083 struct drbd_conf *mdev;
1084
1085 mdev = minor < minor_count ? minor_table[minor] : NULL;
1086
1087 return mdev;
1088}
1089
1090static inline unsigned int mdev_to_minor(struct drbd_conf *mdev)
1091{
1092 return mdev->minor;
1093}
1094
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001095/* returns 1 if it was successful,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096 * returns 0 if there was no data socket.
1097 * so wherever you are going to use the data.socket, e.g. do
1098 * if (!drbd_get_data_sock(mdev))
1099 * return 0;
1100 * CODE();
1101 * drbd_put_data_sock(mdev);
1102 */
1103static inline int drbd_get_data_sock(struct drbd_conf *mdev)
1104{
Philipp Reisnere42325a2011-01-19 13:55:45 +01001105 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106 /* drbd_disconnect() could have called drbd_free_sock()
1107 * while we were waiting in down()... */
Philipp Reisnere42325a2011-01-19 13:55:45 +01001108 if (unlikely(mdev->tconn->data.socket == NULL)) {
1109 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001110 return 0;
1111 }
1112 return 1;
1113}
1114
1115static inline void drbd_put_data_sock(struct drbd_conf *mdev)
1116{
Philipp Reisnere42325a2011-01-19 13:55:45 +01001117 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001118}
1119
1120/*
1121 * function declarations
1122 *************************/
1123
1124/* drbd_main.c */
1125
Philipp Reisnere89b5912010-03-24 17:11:33 +01001126enum dds_flags {
1127 DDSF_FORCED = 1,
1128 DDSF_NO_RESYNC = 2, /* Do not run a resync for the new space */
1129};
1130
Philipp Reisnerb411b362009-09-25 16:07:19 -07001131extern void drbd_init_set_defaults(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001132extern int drbd_thread_start(struct drbd_thread *thi);
1133extern void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait);
Philipp Reisnerbed879a2011-02-04 14:00:37 +01001134extern char *drbd_task_to_thread_name(struct drbd_conf *mdev, struct task_struct *task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001135#ifdef CONFIG_SMP
1136extern void drbd_thread_current_set_cpu(struct drbd_conf *mdev);
1137extern void drbd_calc_cpu_mask(struct drbd_conf *mdev);
1138#else
1139#define drbd_thread_current_set_cpu(A) ({})
1140#define drbd_calc_cpu_mask(A) ({})
1141#endif
1142extern void drbd_free_resources(struct drbd_conf *mdev);
1143extern void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
1144 unsigned int set_size);
1145extern void tl_clear(struct drbd_conf *mdev);
1146extern void _tl_add_barrier(struct drbd_conf *, struct drbd_tl_epoch *);
1147extern void drbd_free_sock(struct drbd_conf *mdev);
1148extern int drbd_send(struct drbd_conf *mdev, struct socket *sock,
1149 void *buf, size_t size, unsigned msg_flags);
1150extern int drbd_send_protocol(struct drbd_conf *mdev);
1151extern int drbd_send_uuids(struct drbd_conf *mdev);
1152extern int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev);
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001153extern int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev);
Philipp Reisnere89b5912010-03-24 17:11:33 +01001154extern int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001155extern int _drbd_send_state(struct drbd_conf *mdev);
1156extern int drbd_send_state(struct drbd_conf *mdev);
1157extern int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001158 enum drbd_packet cmd, struct p_header *h,
1159 size_t size, unsigned msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001160#define USE_DATA_SOCKET 1
1161#define USE_META_SOCKET 0
1162extern int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001163 enum drbd_packet cmd, struct p_header *h, size_t size);
1164extern int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packet cmd,
1165 char *data, size_t size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166extern int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc);
1167extern int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr,
1168 u32 set_size);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001169extern int drbd_send_ack(struct drbd_conf *, enum drbd_packet,
1170 struct drbd_peer_request *);
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001171extern int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
1172 struct p_block_req *rp);
1173extern int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
1174 struct p_data *dp, int data_size);
1175extern int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001176 sector_t sector, int blksize, u64 block_id);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001177extern int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001178extern int drbd_send_block(struct drbd_conf *, enum drbd_packet,
1179 struct drbd_peer_request *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180extern int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001181extern int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1182 sector_t sector, int size, u64 block_id);
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001183extern int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector,
1184 int size, void *digest, int digest_size,
1185 enum drbd_packet cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001186extern int drbd_send_ov_request(struct drbd_conf *mdev,sector_t sector,int size);
1187
1188extern int drbd_send_bitmap(struct drbd_conf *mdev);
1189extern int _drbd_send_bitmap(struct drbd_conf *mdev);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001190extern int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191extern void drbd_free_bc(struct drbd_backing_dev *ldev);
1192extern void drbd_mdev_cleanup(struct drbd_conf *mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01001193void drbd_print_uuids(struct drbd_conf *mdev, const char *text);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195extern void drbd_md_sync(struct drbd_conf *mdev);
1196extern int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197extern void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1198extern void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1199extern void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1200extern void _drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1201extern void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local);
1202extern void drbd_md_set_flag(struct drbd_conf *mdev, int flags) __must_hold(local);
1203extern void drbd_md_clear_flag(struct drbd_conf *mdev, int flags)__must_hold(local);
1204extern int drbd_md_test_flag(struct drbd_backing_dev *, int);
Lars Ellenbergee15b032010-09-03 10:00:09 +02001205#ifndef DRBD_DEBUG_MD_SYNC
Philipp Reisnerb411b362009-09-25 16:07:19 -07001206extern void drbd_md_mark_dirty(struct drbd_conf *mdev);
Lars Ellenbergee15b032010-09-03 10:00:09 +02001207#else
1208#define drbd_md_mark_dirty(m) drbd_md_mark_dirty_(m, __LINE__ , __func__ )
1209extern void drbd_md_mark_dirty_(struct drbd_conf *mdev,
1210 unsigned int line, const char *func);
1211#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001212extern void drbd_queue_bitmap_io(struct drbd_conf *mdev,
1213 int (*io_fn)(struct drbd_conf *),
1214 void (*done)(struct drbd_conf *, int),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001215 char *why, enum bm_flag flags);
1216extern int drbd_bitmap_io(struct drbd_conf *mdev,
1217 int (*io_fn)(struct drbd_conf *),
1218 char *why, enum bm_flag flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001219extern int drbd_bmio_set_n_write(struct drbd_conf *mdev);
1220extern int drbd_bmio_clear_n_write(struct drbd_conf *mdev);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001221extern void drbd_go_diskless(struct drbd_conf *mdev);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001222extern void drbd_ldev_destroy(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001223
1224
1225/* Meta data layout
1226 We reserve a 128MB Block (4k aligned)
1227 * either at the end of the backing device
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001228 * or on a separate meta data device. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001229
1230#define MD_RESERVED_SECT (128LU << 11) /* 128 MB, unit sectors */
1231/* The following numbers are sectors */
1232#define MD_AL_OFFSET 8 /* 8 Sectors after start of meta area */
1233#define MD_AL_MAX_SIZE 64 /* = 32 kb LOG ~ 3776 extents ~ 14 GB Storage */
1234/* Allows up to about 3.8TB */
1235#define MD_BM_OFFSET (MD_AL_OFFSET + MD_AL_MAX_SIZE)
1236
1237/* Since the smalles IO unit is usually 512 byte */
1238#define MD_SECTOR_SHIFT 9
1239#define MD_SECTOR_SIZE (1<<MD_SECTOR_SHIFT)
1240
1241/* activity log */
1242#define AL_EXTENTS_PT ((MD_SECTOR_SIZE-12)/8-1) /* 61 ; Extents per 512B sector */
1243#define AL_EXTENT_SHIFT 22 /* One extent represents 4M Storage */
1244#define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
1245
1246#if BITS_PER_LONG == 32
1247#define LN2_BPL 5
1248#define cpu_to_lel(A) cpu_to_le32(A)
1249#define lel_to_cpu(A) le32_to_cpu(A)
1250#elif BITS_PER_LONG == 64
1251#define LN2_BPL 6
1252#define cpu_to_lel(A) cpu_to_le64(A)
1253#define lel_to_cpu(A) le64_to_cpu(A)
1254#else
1255#error "LN2 of BITS_PER_LONG unknown!"
1256#endif
1257
1258/* resync bitmap */
1259/* 16MB sized 'bitmap extent' to track syncer usage */
1260struct bm_extent {
1261 int rs_left; /* number of bits set (out of sync) in this extent. */
1262 int rs_failed; /* number of failed resync requests in this extent. */
1263 unsigned long flags;
1264 struct lc_element lce;
1265};
1266
1267#define BME_NO_WRITES 0 /* bm_extent.flags: no more requests on this one! */
1268#define BME_LOCKED 1 /* bm_extent.flags: syncer active on this one. */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001269#define BME_PRIORITY 2 /* finish resync IO on this extent ASAP! App IO waiting! */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270
1271/* drbd_bitmap.c */
1272/*
1273 * We need to store one bit for a block.
1274 * Example: 1GB disk @ 4096 byte blocks ==> we need 32 KB bitmap.
1275 * Bit 0 ==> local node thinks this block is binary identical on both nodes
1276 * Bit 1 ==> local node thinks this block needs to be synced.
1277 */
1278
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001279#define SLEEP_TIME (HZ/10)
1280
Philipp Reisnerb411b362009-09-25 16:07:19 -07001281#define BM_BLOCK_SHIFT 12 /* 4k per bit */
1282#define BM_BLOCK_SIZE (1<<BM_BLOCK_SHIFT)
1283/* (9+3) : 512 bytes @ 8 bits; representing 16M storage
1284 * per sector of on disk bitmap */
1285#define BM_EXT_SHIFT (BM_BLOCK_SHIFT + MD_SECTOR_SHIFT + 3) /* = 24 */
1286#define BM_EXT_SIZE (1<<BM_EXT_SHIFT)
1287
1288#if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
1289#error "HAVE YOU FIXED drbdmeta AS WELL??"
1290#endif
1291
1292/* thus many _storage_ sectors are described by one bit */
1293#define BM_SECT_TO_BIT(x) ((x)>>(BM_BLOCK_SHIFT-9))
1294#define BM_BIT_TO_SECT(x) ((sector_t)(x)<<(BM_BLOCK_SHIFT-9))
1295#define BM_SECT_PER_BIT BM_BIT_TO_SECT(1)
1296
1297/* bit to represented kilo byte conversion */
1298#define Bit2KB(bits) ((bits)<<(BM_BLOCK_SHIFT-10))
1299
1300/* in which _bitmap_ extent (resp. sector) the bit for a certain
1301 * _storage_ sector is located in */
1302#define BM_SECT_TO_EXT(x) ((x)>>(BM_EXT_SHIFT-9))
1303
1304/* how much _storage_ sectors we have per bitmap sector */
1305#define BM_EXT_TO_SECT(x) ((sector_t)(x) << (BM_EXT_SHIFT-9))
1306#define BM_SECT_PER_EXT BM_EXT_TO_SECT(1)
1307
1308/* in one sector of the bitmap, we have this many activity_log extents. */
1309#define AL_EXT_PER_BM_SECT (1 << (BM_EXT_SHIFT - AL_EXTENT_SHIFT))
1310#define BM_WORDS_PER_AL_EXT (1 << (AL_EXTENT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
1311
1312#define BM_BLOCKS_PER_BM_EXT_B (BM_EXT_SHIFT - BM_BLOCK_SHIFT)
1313#define BM_BLOCKS_PER_BM_EXT_MASK ((1<<BM_BLOCKS_PER_BM_EXT_B) - 1)
1314
1315/* the extent in "PER_EXTENT" below is an activity log extent
1316 * we need that many (long words/bytes) to store the bitmap
1317 * of one AL_EXTENT_SIZE chunk of storage.
1318 * we can store the bitmap for that many AL_EXTENTS within
1319 * one sector of the _on_disk_ bitmap:
1320 * bit 0 bit 37 bit 38 bit (512*8)-1
1321 * ...|........|........|.. // ..|........|
1322 * sect. 0 `296 `304 ^(512*8*8)-1
1323 *
1324#define BM_WORDS_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / BITS_PER_LONG )
1325#define BM_BYTES_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / 8 ) // 128
1326#define BM_EXT_PER_SECT ( 512 / BM_BYTES_PER_EXTENT ) // 4
1327 */
1328
1329#define DRBD_MAX_SECTORS_32 (0xffffffffLU)
1330#define DRBD_MAX_SECTORS_BM \
1331 ((MD_RESERVED_SECT - MD_BM_OFFSET) * (1LL<<(BM_EXT_SHIFT-9)))
1332#if DRBD_MAX_SECTORS_BM < DRBD_MAX_SECTORS_32
1333#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1334#define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_BM
Lars Ellenberg36bfc7e2010-01-05 19:33:54 +01001335#elif !defined(CONFIG_LBDAF) && BITS_PER_LONG == 32
Philipp Reisnerb411b362009-09-25 16:07:19 -07001336#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_32
1337#define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_32
1338#else
1339#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1340/* 16 TB in units of sectors */
1341#if BITS_PER_LONG == 32
1342/* adjust by one page worth of bitmap,
1343 * so we won't wrap around in drbd_bm_find_next_bit.
1344 * you should use 64bit OS for that much storage, anyways. */
1345#define DRBD_MAX_SECTORS_FLEX BM_BIT_TO_SECT(0xffff7fff)
1346#else
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001347/* we allow up to 1 PiB now on 64bit architecture with "flexible" meta data */
1348#define DRBD_MAX_SECTORS_FLEX (1UL << 51)
1349/* corresponds to (1UL << 38) bits right now. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001350#endif
1351#endif
1352
Philipp Reisnerd5373382010-08-23 15:18:33 +02001353#define HT_SHIFT 8
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001354#define DRBD_MAX_BIO_SIZE (1U<<(9+HT_SHIFT))
Philipp Reisner99432fc2011-05-20 16:39:13 +02001355#define DRBD_MAX_BIO_SIZE_SAFE (1 << 12) /* Works always = 4k */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356
Philipp Reisnerd5373382010-08-23 15:18:33 +02001357#define DRBD_MAX_SIZE_H80_PACKET (1 << 15) /* The old header only allows packets up to 32Kib data */
1358
Philipp Reisnerb411b362009-09-25 16:07:19 -07001359extern int drbd_bm_init(struct drbd_conf *mdev);
Philipp Reisner02d9a942010-03-24 16:23:03 +01001360extern int drbd_bm_resize(struct drbd_conf *mdev, sector_t sectors, int set_new_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001361extern void drbd_bm_cleanup(struct drbd_conf *mdev);
1362extern void drbd_bm_set_all(struct drbd_conf *mdev);
1363extern void drbd_bm_clear_all(struct drbd_conf *mdev);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001364/* set/clear/test only a few bits at a time */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001365extern int drbd_bm_set_bits(
1366 struct drbd_conf *mdev, unsigned long s, unsigned long e);
1367extern int drbd_bm_clear_bits(
1368 struct drbd_conf *mdev, unsigned long s, unsigned long e);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001369extern int drbd_bm_count_bits(
1370 struct drbd_conf *mdev, const unsigned long s, const unsigned long e);
1371/* bm_set_bits variant for use while holding drbd_bm_lock,
1372 * may process the whole bitmap in one go */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001373extern void _drbd_bm_set_bits(struct drbd_conf *mdev,
1374 const unsigned long s, const unsigned long e);
1375extern int drbd_bm_test_bit(struct drbd_conf *mdev, unsigned long bitnr);
1376extern int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001377extern int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001378extern int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local);
1379extern int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local);
1380extern unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev,
1381 unsigned long al_enr);
1382extern size_t drbd_bm_words(struct drbd_conf *mdev);
1383extern unsigned long drbd_bm_bits(struct drbd_conf *mdev);
1384extern sector_t drbd_bm_capacity(struct drbd_conf *mdev);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001385
1386#define DRBD_END_OF_BITMAP (~(unsigned long)0)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001387extern unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1388/* bm_find_next variants for use while you hold drbd_bm_lock() */
1389extern unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1390extern unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo);
Philipp Reisner07782862010-08-31 12:00:50 +02001391extern unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001392extern unsigned long drbd_bm_total_weight(struct drbd_conf *mdev);
1393extern int drbd_bm_rs_done(struct drbd_conf *mdev);
1394/* for receive_bitmap */
1395extern void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset,
1396 size_t number, unsigned long *buffer);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001397/* for _drbd_send_bitmap */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001398extern void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset,
1399 size_t number, unsigned long *buffer);
1400
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001401extern void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001402extern void drbd_bm_unlock(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001403/* drbd_main.c */
1404
1405extern struct kmem_cache *drbd_request_cache;
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001406extern struct kmem_cache *drbd_ee_cache; /* peer requests */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001407extern struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
1408extern struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
1409extern mempool_t *drbd_request_mempool;
1410extern mempool_t *drbd_ee_mempool;
1411
1412extern struct page *drbd_pp_pool; /* drbd's page pool */
1413extern spinlock_t drbd_pp_lock;
1414extern int drbd_pp_vacant;
1415extern wait_queue_head_t drbd_pp_wait;
1416
1417extern rwlock_t global_state_lock;
1418
1419extern struct drbd_conf *drbd_new_device(unsigned int minor);
1420extern void drbd_free_mdev(struct drbd_conf *mdev);
1421
Philipp Reisner21114382011-01-19 12:26:59 +01001422struct drbd_tconn *drbd_new_tconn(char *name);
1423extern void drbd_free_tconn(struct drbd_tconn *tconn);
1424
Philipp Reisnerb411b362009-09-25 16:07:19 -07001425extern int proc_details;
1426
1427/* drbd_req */
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01001428extern int drbd_make_request(struct request_queue *q, struct bio *bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001429extern int drbd_read_remote(struct drbd_conf *mdev, struct drbd_request *req);
1430extern int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec);
1431extern int is_valid_ar_handle(struct drbd_request *, sector_t);
1432
1433
1434/* drbd_nl.c */
1435extern void drbd_suspend_io(struct drbd_conf *mdev);
1436extern void drbd_resume_io(struct drbd_conf *mdev);
1437extern char *ppsize(char *buf, unsigned long long size);
Philipp Reisnera393db62009-12-22 13:35:52 +01001438extern sector_t drbd_new_dev_size(struct drbd_conf *, struct drbd_backing_dev *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001439enum determine_dev_size { dev_size_error = -1, unchanged = 0, shrunk = 1, grew = 2 };
Bart Van Assche24c48302011-05-21 18:32:29 +02001440extern enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *, enum dds_flags) __must_hold(local);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001441extern void resync_after_online_grow(struct drbd_conf *);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001442extern void drbd_reconsider_max_bio_size(struct drbd_conf *mdev);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001443extern enum drbd_state_rv drbd_set_role(struct drbd_conf *mdev,
1444 enum drbd_role new_role,
1445 int force);
Philipp Reisner87f7be42010-06-11 13:56:33 +02001446extern enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev);
1447extern void drbd_try_outdate_peer_async(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001448extern int drbd_khelper(struct drbd_conf *mdev, char *cmd);
1449
1450/* drbd_worker.c */
1451extern int drbd_worker(struct drbd_thread *thi);
1452extern int drbd_alter_sa(struct drbd_conf *mdev, int na);
1453extern void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side);
1454extern void resume_next_sg(struct drbd_conf *mdev);
1455extern void suspend_other_sg(struct drbd_conf *mdev);
1456extern int drbd_resync_finished(struct drbd_conf *mdev);
1457/* maybe rather drbd_main.c ? */
1458extern int drbd_md_sync_page_io(struct drbd_conf *mdev,
1459 struct drbd_backing_dev *bdev, sector_t sector, int rw);
1460extern void drbd_ov_oos_found(struct drbd_conf*, sector_t, int);
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001461extern void drbd_rs_controller_reset(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462
1463static inline void ov_oos_print(struct drbd_conf *mdev)
1464{
1465 if (mdev->ov_last_oos_size) {
1466 dev_err(DEV, "Out of sync: start=%llu, size=%lu (sectors)\n",
1467 (unsigned long long)mdev->ov_last_oos_start,
1468 (unsigned long)mdev->ov_last_oos_size);
1469 }
1470 mdev->ov_last_oos_size=0;
1471}
1472
1473
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001474extern void drbd_csum_bio(struct drbd_conf *, struct crypto_hash *, struct bio *, void *);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001475extern void drbd_csum_ee(struct drbd_conf *, struct crypto_hash *,
1476 struct drbd_peer_request *, void *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001477/* worker callbacks */
1478extern int w_req_cancel_conflict(struct drbd_conf *, struct drbd_work *, int);
1479extern int w_read_retry_remote(struct drbd_conf *, struct drbd_work *, int);
1480extern int w_e_end_data_req(struct drbd_conf *, struct drbd_work *, int);
1481extern int w_e_end_rsdata_req(struct drbd_conf *, struct drbd_work *, int);
1482extern int w_e_end_csum_rs_req(struct drbd_conf *, struct drbd_work *, int);
1483extern int w_e_end_ov_reply(struct drbd_conf *, struct drbd_work *, int);
1484extern int w_e_end_ov_req(struct drbd_conf *, struct drbd_work *, int);
1485extern int w_ov_finished(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisner794abb72010-12-27 11:51:23 +01001486extern int w_resync_timer(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001487extern int w_resume_next_sg(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001488extern int w_send_write_hint(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001489extern int w_send_dblock(struct drbd_conf *, struct drbd_work *, int);
1490extern int w_send_barrier(struct drbd_conf *, struct drbd_work *, int);
1491extern int w_send_read_req(struct drbd_conf *, struct drbd_work *, int);
1492extern int w_prev_work_done(struct drbd_conf *, struct drbd_work *, int);
1493extern int w_e_reissue(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisner265be2d2010-05-31 10:14:17 +02001494extern int w_restart_disk_io(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001495extern int w_send_oos(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001496extern int w_start_resync(struct drbd_conf *, struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497
1498extern void resync_timer_fn(unsigned long data);
Philipp Reisner370a43e2011-01-14 16:03:11 +01001499extern void start_resync_timer_fn(unsigned long data);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001500
1501/* drbd_receiver.c */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001502extern int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001503extern int drbd_submit_ee(struct drbd_conf *, struct drbd_peer_request *,
1504 const unsigned, const int);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001505extern int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001506extern struct drbd_peer_request *drbd_alloc_ee(struct drbd_conf *,
1507 u64, sector_t, unsigned int,
1508 gfp_t) __must_hold(local);
1509extern void drbd_free_some_ee(struct drbd_conf *, struct drbd_peer_request *,
1510 int);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001511#define drbd_free_ee(m,e) drbd_free_some_ee(m, e, 0)
1512#define drbd_free_net_ee(m,e) drbd_free_some_ee(m, e, 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513extern void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1514 struct list_head *head);
1515extern void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1516 struct list_head *head);
1517extern void drbd_set_recv_tcq(struct drbd_conf *mdev, int tcq_enabled);
1518extern void _drbd_clear_done_ee(struct drbd_conf *mdev, struct list_head *to_be_freed);
Philipp Reisner191d3cc2011-01-19 14:53:22 +01001519extern void drbd_flush_workqueue(struct drbd_tconn *tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001520
1521/* yes, there is kernel_setsockopt, but only since 2.6.18. we don't need to
1522 * mess with get_fs/set_fs, we know we are KERNEL_DS always. */
1523static inline int drbd_setsockopt(struct socket *sock, int level, int optname,
1524 char __user *optval, int optlen)
1525{
1526 int err;
1527 if (level == SOL_SOCKET)
1528 err = sock_setsockopt(sock, level, optname, optval, optlen);
1529 else
1530 err = sock->ops->setsockopt(sock, level, optname, optval,
1531 optlen);
1532 return err;
1533}
1534
1535static inline void drbd_tcp_cork(struct socket *sock)
1536{
1537 int __user val = 1;
1538 (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1539 (char __user *)&val, sizeof(val));
1540}
1541
1542static inline void drbd_tcp_uncork(struct socket *sock)
1543{
1544 int __user val = 0;
1545 (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1546 (char __user *)&val, sizeof(val));
1547}
1548
1549static inline void drbd_tcp_nodelay(struct socket *sock)
1550{
1551 int __user val = 1;
1552 (void) drbd_setsockopt(sock, SOL_TCP, TCP_NODELAY,
1553 (char __user *)&val, sizeof(val));
1554}
1555
1556static inline void drbd_tcp_quickack(struct socket *sock)
1557{
Lars Ellenberg344fa462010-05-25 14:23:57 +02001558 int __user val = 2;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001559 (void) drbd_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
1560 (char __user *)&val, sizeof(val));
1561}
1562
1563void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo);
1564
1565/* drbd_proc.c */
1566extern struct proc_dir_entry *drbd_proc;
Emese Revfy7d4e9d02009-12-14 00:59:30 +01001567extern const struct file_operations drbd_proc_fops;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001568extern const char *drbd_conn_str(enum drbd_conns s);
1569extern const char *drbd_role_str(enum drbd_role s);
1570
1571/* drbd_actlog.c */
1572extern void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector);
1573extern void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector);
1574extern void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector);
1575extern int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1576extern int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1577extern void drbd_rs_cancel_all(struct drbd_conf *mdev);
1578extern int drbd_rs_del_all(struct drbd_conf *mdev);
1579extern void drbd_rs_failed_io(struct drbd_conf *mdev,
1580 sector_t sector, int size);
1581extern int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *);
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001582extern void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001583extern void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector,
1584 int size, const char *file, const unsigned int line);
1585#define drbd_set_in_sync(mdev, sector, size) \
1586 __drbd_set_in_sync(mdev, sector, size, __FILE__, __LINE__)
Philipp Reisner73a01a12010-10-27 14:33:00 +02001587extern int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001588 int size, const char *file, const unsigned int line);
1589#define drbd_set_out_of_sync(mdev, sector, size) \
1590 __drbd_set_out_of_sync(mdev, sector, size, __FILE__, __LINE__)
1591extern void drbd_al_apply_to_bm(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592extern void drbd_al_shrink(struct drbd_conf *mdev);
1593
1594
1595/* drbd_nl.c */
1596
1597void drbd_nl_cleanup(void);
1598int __init drbd_nl_init(void);
1599void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state);
1600void drbd_bcast_sync_progress(struct drbd_conf *mdev);
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001601void drbd_bcast_ee(struct drbd_conf *, const char *, const int, const char *,
1602 const char *, const struct drbd_peer_request *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001603
1604
1605/**
1606 * DOC: DRBD State macros
1607 *
1608 * These macros are used to express state changes in easily readable form.
1609 *
1610 * The NS macros expand to a mask and a value, that can be bit ored onto the
1611 * current state as soon as the spinlock (req_lock) was taken.
1612 *
1613 * The _NS macros are used for state functions that get called with the
1614 * spinlock. These macros expand directly to the new state value.
1615 *
1616 * Besides the basic forms NS() and _NS() additional _?NS[23] are defined
1617 * to express state changes that affect more than one aspect of the state.
1618 *
1619 * E.g. NS2(conn, C_CONNECTED, peer, R_SECONDARY)
1620 * Means that the network connection was established and that the peer
1621 * is in secondary role.
1622 */
1623#define role_MASK R_MASK
1624#define peer_MASK R_MASK
1625#define disk_MASK D_MASK
1626#define pdsk_MASK D_MASK
1627#define conn_MASK C_MASK
1628#define susp_MASK 1
1629#define user_isp_MASK 1
1630#define aftr_isp_MASK 1
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001631#define susp_nod_MASK 1
1632#define susp_fen_MASK 1
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633
1634#define NS(T, S) \
1635 ({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
1636 ({ union drbd_state val; val.i = 0; val.T = (S); val; })
1637#define NS2(T1, S1, T2, S2) \
1638 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1639 mask.T2 = T2##_MASK; mask; }), \
1640 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1641 val.T2 = (S2); val; })
1642#define NS3(T1, S1, T2, S2, T3, S3) \
1643 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1644 mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
1645 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1646 val.T2 = (S2); val.T3 = (S3); val; })
1647
1648#define _NS(D, T, S) \
1649 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T = (S); __ns; })
1650#define _NS2(D, T1, S1, T2, S2) \
1651 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1652 __ns.T2 = (S2); __ns; })
1653#define _NS3(D, T1, S1, T2, S2, T3, S3) \
1654 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1655 __ns.T2 = (S2); __ns.T3 = (S3); __ns; })
1656
1657/*
1658 * inline helper functions
1659 *************************/
1660
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001661/* see also page_chain_add and friends in drbd_receiver.c */
1662static inline struct page *page_chain_next(struct page *page)
1663{
1664 return (struct page *)page_private(page);
1665}
1666#define page_chain_for_each(page) \
1667 for (; page && ({ prefetch(page_chain_next(page)); 1; }); \
1668 page = page_chain_next(page))
1669#define page_chain_for_each_safe(page, n) \
1670 for (; page && ({ n = page_chain_next(page); 1; }); page = n)
1671
1672static inline int drbd_bio_has_active_page(struct bio *bio)
1673{
1674 struct bio_vec *bvec;
1675 int i;
1676
1677 __bio_for_each_segment(bvec, bio, i, 0) {
1678 if (page_count(bvec->bv_page) > 1)
1679 return 1;
1680 }
1681
1682 return 0;
1683}
1684
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001685static inline int drbd_ee_has_active_page(struct drbd_peer_request *peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001686{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001687 struct page *page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001688 page_chain_for_each(page) {
1689 if (page_count(page) > 1)
1690 return 1;
1691 }
1692 return 0;
1693}
1694
1695
Philipp Reisnerb8907332011-01-27 14:07:51 +01001696
1697
1698
1699
Philipp Reisnerb411b362009-09-25 16:07:19 -07001700static inline void drbd_state_lock(struct drbd_conf *mdev)
1701{
1702 wait_event(mdev->misc_wait,
1703 !test_and_set_bit(CLUSTER_ST_CHANGE, &mdev->flags));
1704}
1705
1706static inline void drbd_state_unlock(struct drbd_conf *mdev)
1707{
1708 clear_bit(CLUSTER_ST_CHANGE, &mdev->flags);
1709 wake_up(&mdev->misc_wait);
1710}
1711
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001712static inline enum drbd_state_rv
1713_drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
1714 enum chg_state_flags flags, struct completion *done)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001715{
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001716 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001717
1718 read_lock(&global_state_lock);
1719 rv = __drbd_set_state(mdev, ns, flags, done);
1720 read_unlock(&global_state_lock);
1721
1722 return rv;
1723}
1724
Philipp Reisnerb411b362009-09-25 16:07:19 -07001725#define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
1726static inline void __drbd_chk_io_error_(struct drbd_conf *mdev, int forcedetach, const char *where)
1727{
1728 switch (mdev->ldev->dc.on_io_error) {
1729 case EP_PASS_ON:
1730 if (!forcedetach) {
Lars Ellenberg73835062010-05-27 11:51:56 +02001731 if (__ratelimit(&drbd_ratelimit_state))
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001732 dev_err(DEV, "Local IO failed in %s.\n", where);
Philipp Reisnerd2e17802011-03-14 11:54:47 +01001733 if (mdev->state.disk > D_INCONSISTENT)
1734 _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_HARD, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001735 break;
1736 }
1737 /* NOTE fall through to detach case if forcedetach set */
1738 case EP_DETACH:
1739 case EP_CALL_HELPER:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001740 set_bit(WAS_IO_ERROR, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741 if (mdev->state.disk > D_FAILED) {
1742 _drbd_set_state(_NS(mdev, disk, D_FAILED), CS_HARD, NULL);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001743 dev_err(DEV,
1744 "Local IO failed in %s. Detaching...\n", where);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745 }
1746 break;
1747 }
1748}
1749
1750/**
1751 * drbd_chk_io_error: Handle the on_io_error setting, should be called from all io completion handlers
1752 * @mdev: DRBD device.
1753 * @error: Error code passed to the IO completion callback
1754 * @forcedetach: Force detach. I.e. the error happened while accessing the meta data
1755 *
1756 * See also drbd_main.c:after_state_ch() if (os.disk > D_FAILED && ns.disk == D_FAILED)
1757 */
1758#define drbd_chk_io_error(m,e,f) drbd_chk_io_error_(m,e,f, __func__)
1759static inline void drbd_chk_io_error_(struct drbd_conf *mdev,
1760 int error, int forcedetach, const char *where)
1761{
1762 if (error) {
1763 unsigned long flags;
Philipp Reisner87eeee42011-01-19 14:16:30 +01001764 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001765 __drbd_chk_io_error_(mdev, forcedetach, where);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001766 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001767 }
1768}
1769
1770
1771/**
1772 * drbd_md_first_sector() - Returns the first sector number of the meta data area
1773 * @bdev: Meta data block device.
1774 *
1775 * BTW, for internal meta data, this happens to be the maximum capacity
1776 * we could agree upon with our peer node.
1777 */
1778static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
1779{
1780 switch (bdev->dc.meta_dev_idx) {
1781 case DRBD_MD_INDEX_INTERNAL:
1782 case DRBD_MD_INDEX_FLEX_INT:
1783 return bdev->md.md_offset + bdev->md.bm_offset;
1784 case DRBD_MD_INDEX_FLEX_EXT:
1785 default:
1786 return bdev->md.md_offset;
1787 }
1788}
1789
1790/**
1791 * drbd_md_last_sector() - Return the last sector number of the meta data area
1792 * @bdev: Meta data block device.
1793 */
1794static inline sector_t drbd_md_last_sector(struct drbd_backing_dev *bdev)
1795{
1796 switch (bdev->dc.meta_dev_idx) {
1797 case DRBD_MD_INDEX_INTERNAL:
1798 case DRBD_MD_INDEX_FLEX_INT:
1799 return bdev->md.md_offset + MD_AL_OFFSET - 1;
1800 case DRBD_MD_INDEX_FLEX_EXT:
1801 default:
1802 return bdev->md.md_offset + bdev->md.md_size_sect;
1803 }
1804}
1805
1806/* Returns the number of 512 byte sectors of the device */
1807static inline sector_t drbd_get_capacity(struct block_device *bdev)
1808{
1809 /* return bdev ? get_capacity(bdev->bd_disk) : 0; */
Mike Snitzer77304d22010-11-08 14:39:12 +01001810 return bdev ? i_size_read(bdev->bd_inode) >> 9 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001811}
1812
1813/**
1814 * drbd_get_max_capacity() - Returns the capacity we announce to out peer
1815 * @bdev: Meta data block device.
1816 *
1817 * returns the capacity we announce to out peer. we clip ourselves at the
1818 * various MAX_SECTORS, because if we don't, current implementation will
1819 * oops sooner or later
1820 */
1821static inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
1822{
1823 sector_t s;
1824 switch (bdev->dc.meta_dev_idx) {
1825 case DRBD_MD_INDEX_INTERNAL:
1826 case DRBD_MD_INDEX_FLEX_INT:
1827 s = drbd_get_capacity(bdev->backing_bdev)
1828 ? min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1829 drbd_md_first_sector(bdev))
1830 : 0;
1831 break;
1832 case DRBD_MD_INDEX_FLEX_EXT:
1833 s = min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1834 drbd_get_capacity(bdev->backing_bdev));
1835 /* clip at maximum size the meta device can support */
1836 s = min_t(sector_t, s,
1837 BM_EXT_TO_SECT(bdev->md.md_size_sect
1838 - bdev->md.bm_offset));
1839 break;
1840 default:
1841 s = min_t(sector_t, DRBD_MAX_SECTORS,
1842 drbd_get_capacity(bdev->backing_bdev));
1843 }
1844 return s;
1845}
1846
1847/**
1848 * drbd_md_ss__() - Return the sector number of our meta data super block
1849 * @mdev: DRBD device.
1850 * @bdev: Meta data block device.
1851 */
1852static inline sector_t drbd_md_ss__(struct drbd_conf *mdev,
1853 struct drbd_backing_dev *bdev)
1854{
1855 switch (bdev->dc.meta_dev_idx) {
1856 default: /* external, some index */
1857 return MD_RESERVED_SECT * bdev->dc.meta_dev_idx;
1858 case DRBD_MD_INDEX_INTERNAL:
1859 /* with drbd08, internal meta data is always "flexible" */
1860 case DRBD_MD_INDEX_FLEX_INT:
1861 /* sizeof(struct md_on_disk_07) == 4k
1862 * position: last 4k aligned block of 4k size */
1863 if (!bdev->backing_bdev) {
1864 if (__ratelimit(&drbd_ratelimit_state)) {
1865 dev_err(DEV, "bdev->backing_bdev==NULL\n");
1866 dump_stack();
1867 }
1868 return 0;
1869 }
1870 return (drbd_get_capacity(bdev->backing_bdev) & ~7ULL)
1871 - MD_AL_OFFSET;
1872 case DRBD_MD_INDEX_FLEX_EXT:
1873 return 0;
1874 }
1875}
1876
1877static inline void
Philipp Reisnerb411b362009-09-25 16:07:19 -07001878drbd_queue_work_front(struct drbd_work_queue *q, struct drbd_work *w)
1879{
1880 unsigned long flags;
1881 spin_lock_irqsave(&q->q_lock, flags);
1882 list_add(&w->list, &q->q);
1883 up(&q->s); /* within the spinlock,
1884 see comment near end of drbd_worker() */
1885 spin_unlock_irqrestore(&q->q_lock, flags);
1886}
1887
1888static inline void
1889drbd_queue_work(struct drbd_work_queue *q, struct drbd_work *w)
1890{
1891 unsigned long flags;
1892 spin_lock_irqsave(&q->q_lock, flags);
1893 list_add_tail(&w->list, &q->q);
1894 up(&q->s); /* within the spinlock,
1895 see comment near end of drbd_worker() */
1896 spin_unlock_irqrestore(&q->q_lock, flags);
1897}
1898
1899static inline void wake_asender(struct drbd_conf *mdev)
1900{
1901 if (test_bit(SIGNAL_ASENDER, &mdev->flags))
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001902 force_sig(DRBD_SIG, mdev->tconn->asender.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001903}
1904
1905static inline void request_ping(struct drbd_conf *mdev)
1906{
1907 set_bit(SEND_PING, &mdev->flags);
1908 wake_asender(mdev);
1909}
1910
1911static inline int drbd_send_short_cmd(struct drbd_conf *mdev,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001912 enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001913{
Philipp Reisnerc0129492011-01-19 16:58:16 +01001914 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001915 return drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &h, sizeof(h));
1916}
1917
1918static inline int drbd_send_ping(struct drbd_conf *mdev)
1919{
Philipp Reisnerc0129492011-01-19 16:58:16 +01001920 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001921 return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING, &h, sizeof(h));
1922}
1923
1924static inline int drbd_send_ping_ack(struct drbd_conf *mdev)
1925{
Philipp Reisnerc0129492011-01-19 16:58:16 +01001926 struct p_header h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001927 return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING_ACK, &h, sizeof(h));
1928}
1929
1930static inline void drbd_thread_stop(struct drbd_thread *thi)
1931{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001932 _drbd_thread_stop(thi, false, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001933}
1934
1935static inline void drbd_thread_stop_nowait(struct drbd_thread *thi)
1936{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001937 _drbd_thread_stop(thi, false, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001938}
1939
1940static inline void drbd_thread_restart_nowait(struct drbd_thread *thi)
1941{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001942 _drbd_thread_stop(thi, true, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001943}
1944
1945/* counts how many answer packets packets we expect from our peer,
1946 * for either explicit application requests,
1947 * or implicit barrier packets as necessary.
1948 * increased:
1949 * w_send_barrier
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001950 * _req_mod(req, QUEUE_FOR_NET_WRITE or QUEUE_FOR_NET_READ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001951 * it is much easier and equally valid to count what we queue for the
1952 * worker, even before it actually was queued or send.
1953 * (drbd_make_request_common; recovery path on read io-error)
1954 * decreased:
1955 * got_BarrierAck (respective tl_clear, tl_clear_barrier)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001956 * _req_mod(req, DATA_RECEIVED)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001957 * [from receive_DataReply]
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001958 * _req_mod(req, WRITE_ACKED_BY_PEER or RECV_ACKED_BY_PEER or NEG_ACKED)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001959 * [from got_BlockAck (P_WRITE_ACK, P_RECV_ACK)]
1960 * for some reason it is NOT decreased in got_NegAck,
1961 * but in the resulting cleanup code from report_params.
1962 * we should try to remember the reason for that...
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001963 * _req_mod(req, SEND_FAILED or SEND_CANCELED)
1964 * _req_mod(req, CONNECTION_LOST_WHILE_PENDING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001965 * [from tl_clear_barrier]
1966 */
1967static inline void inc_ap_pending(struct drbd_conf *mdev)
1968{
1969 atomic_inc(&mdev->ap_pending_cnt);
1970}
1971
1972#define ERR_IF_CNT_IS_NEGATIVE(which) \
1973 if (atomic_read(&mdev->which) < 0) \
1974 dev_err(DEV, "in %s:%d: " #which " = %d < 0 !\n", \
1975 __func__ , __LINE__ , \
1976 atomic_read(&mdev->which))
1977
1978#define dec_ap_pending(mdev) do { \
1979 typecheck(struct drbd_conf *, mdev); \
1980 if (atomic_dec_and_test(&mdev->ap_pending_cnt)) \
1981 wake_up(&mdev->misc_wait); \
1982 ERR_IF_CNT_IS_NEGATIVE(ap_pending_cnt); } while (0)
1983
1984/* counts how many resync-related answers we still expect from the peer
1985 * increase decrease
1986 * C_SYNC_TARGET sends P_RS_DATA_REQUEST (and expects P_RS_DATA_REPLY)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001987 * C_SYNC_SOURCE sends P_RS_DATA_REPLY (and expects P_WRITE_ACK with ID_SYNCER)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001988 * (or P_NEG_ACK with ID_SYNCER)
1989 */
1990static inline void inc_rs_pending(struct drbd_conf *mdev)
1991{
1992 atomic_inc(&mdev->rs_pending_cnt);
1993}
1994
1995#define dec_rs_pending(mdev) do { \
1996 typecheck(struct drbd_conf *, mdev); \
1997 atomic_dec(&mdev->rs_pending_cnt); \
1998 ERR_IF_CNT_IS_NEGATIVE(rs_pending_cnt); } while (0)
1999
2000/* counts how many answers we still need to send to the peer.
2001 * increased on
2002 * receive_Data unless protocol A;
2003 * we need to send a P_RECV_ACK (proto B)
2004 * or P_WRITE_ACK (proto C)
2005 * receive_RSDataReply (recv_resync_read) we need to send a P_WRITE_ACK
2006 * receive_DataRequest (receive_RSDataRequest) we need to send back P_DATA
2007 * receive_Barrier_* we need to send a P_BARRIER_ACK
2008 */
2009static inline void inc_unacked(struct drbd_conf *mdev)
2010{
2011 atomic_inc(&mdev->unacked_cnt);
2012}
2013
2014#define dec_unacked(mdev) do { \
2015 typecheck(struct drbd_conf *, mdev); \
2016 atomic_dec(&mdev->unacked_cnt); \
2017 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2018
2019#define sub_unacked(mdev, n) do { \
2020 typecheck(struct drbd_conf *, mdev); \
2021 atomic_sub(n, &mdev->unacked_cnt); \
2022 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2023
2024
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002025static inline void put_net_conf(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002026{
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002027 if (atomic_dec_and_test(&tconn->net_cnt))
2028 wake_up(&tconn->net_cnt_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002029}
2030
2031/**
Philipp Reisner89e58e72011-01-19 13:12:45 +01002032 * get_net_conf() - Increase ref count on mdev->tconn->net_conf; Returns 0 if nothing there
Philipp Reisnerb411b362009-09-25 16:07:19 -07002033 * @mdev: DRBD device.
2034 *
Philipp Reisner89e58e72011-01-19 13:12:45 +01002035 * You have to call put_net_conf() when finished working with mdev->tconn->net_conf.
Philipp Reisnerb411b362009-09-25 16:07:19 -07002036 */
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002037static inline int get_net_conf(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002038{
2039 int have_net_conf;
2040
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002041 atomic_inc(&tconn->net_cnt);
2042 have_net_conf = tconn->volume0->state.conn >= C_UNCONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002043 if (!have_net_conf)
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002044 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002045 return have_net_conf;
2046}
2047
2048/**
2049 * get_ldev() - Increase the ref count on mdev->ldev. Returns 0 if there is no ldev
2050 * @M: DRBD device.
2051 *
2052 * You have to call put_ldev() when finished working with mdev->ldev.
2053 */
2054#define get_ldev(M) __cond_lock(local, _get_ldev_if_state(M,D_INCONSISTENT))
2055#define get_ldev_if_state(M,MINS) __cond_lock(local, _get_ldev_if_state(M,MINS))
2056
2057static inline void put_ldev(struct drbd_conf *mdev)
2058{
Lars Ellenberg1d7734a02010-08-11 21:21:50 +02002059 int i = atomic_dec_return(&mdev->local_cnt);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02002060
2061 /* This may be called from some endio handler,
2062 * so we must not sleep here. */
2063
Philipp Reisnerb411b362009-09-25 16:07:19 -07002064 __release(local);
Lars Ellenberg1d7734a02010-08-11 21:21:50 +02002065 D_ASSERT(i >= 0);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002066 if (i == 0) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002067 if (mdev->state.disk == D_DISKLESS)
2068 /* even internal references gone, safe to destroy */
2069 drbd_ldev_destroy(mdev);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002070 if (mdev->state.disk == D_FAILED)
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002071 /* all application IO references gone. */
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002072 drbd_go_diskless(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002073 wake_up(&mdev->misc_wait);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002074 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002075}
2076
2077#ifndef __CHECKER__
2078static inline int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
2079{
2080 int io_allowed;
2081
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002082 /* never get a reference while D_DISKLESS */
2083 if (mdev->state.disk == D_DISKLESS)
2084 return 0;
2085
Philipp Reisnerb411b362009-09-25 16:07:19 -07002086 atomic_inc(&mdev->local_cnt);
2087 io_allowed = (mdev->state.disk >= mins);
2088 if (!io_allowed)
2089 put_ldev(mdev);
2090 return io_allowed;
2091}
2092#else
2093extern int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins);
2094#endif
2095
2096/* you must have an "get_ldev" reference */
2097static inline void drbd_get_syncer_progress(struct drbd_conf *mdev,
2098 unsigned long *bits_left, unsigned int *per_mil_done)
2099{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01002100 /* this is to break it at compile time when we change that, in case we
2101 * want to support more than (1<<32) bits on a 32bit arch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002102 typecheck(unsigned long, mdev->rs_total);
2103
2104 /* note: both rs_total and rs_left are in bits, i.e. in
2105 * units of BM_BLOCK_SIZE.
2106 * for the percentage, we don't care. */
2107
Lars Ellenberg439d5952010-11-05 09:52:46 +01002108 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2109 *bits_left = mdev->ov_left;
2110 else
2111 *bits_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002112 /* >> 10 to prevent overflow,
2113 * +1 to prevent division by zero */
2114 if (*bits_left > mdev->rs_total) {
2115 /* doh. maybe a logic bug somewhere.
2116 * may also be just a race condition
2117 * between this and a disconnect during sync.
2118 * for now, just prevent in-kernel buffer overflow.
2119 */
2120 smp_rmb();
2121 dev_warn(DEV, "cs:%s rs_left=%lu > rs_total=%lu (rs_failed %lu)\n",
2122 drbd_conn_str(mdev->state.conn),
2123 *bits_left, mdev->rs_total, mdev->rs_failed);
2124 *per_mil_done = 0;
2125 } else {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01002126 /* Make sure the division happens in long context.
2127 * We allow up to one petabyte storage right now,
2128 * at a granularity of 4k per bit that is 2**38 bits.
2129 * After shift right and multiplication by 1000,
2130 * this should still fit easily into a 32bit long,
2131 * so we don't need a 64bit division on 32bit arch.
2132 * Note: currently we don't support such large bitmaps on 32bit
2133 * arch anyways, but no harm done to be prepared for it here.
2134 */
2135 unsigned int shift = mdev->rs_total >= (1ULL << 32) ? 16 : 10;
2136 unsigned long left = *bits_left >> shift;
2137 unsigned long total = 1UL + (mdev->rs_total >> shift);
2138 unsigned long tmp = 1000UL - left * 1000UL/total;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002139 *per_mil_done = tmp;
2140 }
2141}
2142
2143
2144/* this throttles on-the-fly application requests
2145 * according to max_buffers settings;
2146 * maybe re-implement using semaphores? */
2147static inline int drbd_get_max_buffers(struct drbd_conf *mdev)
2148{
2149 int mxb = 1000000; /* arbitrary limit on open requests */
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002150 if (get_net_conf(mdev->tconn)) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002151 mxb = mdev->tconn->net_conf->max_buffers;
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002152 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002153 }
2154 return mxb;
2155}
2156
Philipp Reisner37190942010-11-10 12:08:37 +01002157static inline int drbd_state_is_stable(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002158{
Philipp Reisner37190942010-11-10 12:08:37 +01002159 union drbd_state s = mdev->state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002160
2161 /* DO NOT add a default clause, we want the compiler to warn us
2162 * for any newly introduced state we may have forgotten to add here */
2163
2164 switch ((enum drbd_conns)s.conn) {
2165 /* new io only accepted when there is no connection, ... */
2166 case C_STANDALONE:
2167 case C_WF_CONNECTION:
2168 /* ... or there is a well established connection. */
2169 case C_CONNECTED:
2170 case C_SYNC_SOURCE:
2171 case C_SYNC_TARGET:
2172 case C_VERIFY_S:
2173 case C_VERIFY_T:
2174 case C_PAUSED_SYNC_S:
2175 case C_PAUSED_SYNC_T:
Philipp Reisner67531712010-10-27 12:21:30 +02002176 case C_AHEAD:
2177 case C_BEHIND:
Philipp Reisner37190942010-11-10 12:08:37 +01002178 /* transitional states, IO allowed */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002179 case C_DISCONNECTING:
2180 case C_UNCONNECTED:
2181 case C_TIMEOUT:
2182 case C_BROKEN_PIPE:
2183 case C_NETWORK_FAILURE:
2184 case C_PROTOCOL_ERROR:
2185 case C_TEAR_DOWN:
2186 case C_WF_REPORT_PARAMS:
2187 case C_STARTING_SYNC_S:
2188 case C_STARTING_SYNC_T:
Philipp Reisner37190942010-11-10 12:08:37 +01002189 break;
2190
2191 /* Allow IO in BM exchange states with new protocols */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002192 case C_WF_BITMAP_S:
Philipp Reisner31890f42011-01-19 14:12:51 +01002193 if (mdev->tconn->agreed_pro_version < 96)
Philipp Reisner37190942010-11-10 12:08:37 +01002194 return 0;
2195 break;
2196
2197 /* no new io accepted in these states */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002198 case C_WF_BITMAP_T:
2199 case C_WF_SYNC_UUID:
2200 case C_MASK:
2201 /* not "stable" */
2202 return 0;
2203 }
2204
2205 switch ((enum drbd_disk_state)s.disk) {
2206 case D_DISKLESS:
2207 case D_INCONSISTENT:
2208 case D_OUTDATED:
2209 case D_CONSISTENT:
2210 case D_UP_TO_DATE:
2211 /* disk state is stable as well. */
2212 break;
2213
2214 /* no new io accepted during tansitional states */
2215 case D_ATTACHING:
2216 case D_FAILED:
2217 case D_NEGOTIATING:
2218 case D_UNKNOWN:
2219 case D_MASK:
2220 /* not "stable" */
2221 return 0;
2222 }
2223
2224 return 1;
2225}
2226
Philipp Reisnerfb22c402010-09-08 23:20:21 +02002227static inline int is_susp(union drbd_state s)
2228{
2229 return s.susp || s.susp_nod || s.susp_fen;
2230}
2231
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002232static inline bool may_inc_ap_bio(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233{
2234 int mxb = drbd_get_max_buffers(mdev);
2235
Philipp Reisnerfb22c402010-09-08 23:20:21 +02002236 if (is_susp(mdev->state))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002237 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002238 if (test_bit(SUSPEND_IO, &mdev->flags))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002239 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002240
2241 /* to avoid potential deadlock or bitmap corruption,
2242 * in various places, we only allow new application io
2243 * to start during "stable" states. */
2244
2245 /* no new io accepted when attaching or detaching the disk */
Philipp Reisner37190942010-11-10 12:08:37 +01002246 if (!drbd_state_is_stable(mdev))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002247 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002248
2249 /* since some older kernels don't have atomic_add_unless,
2250 * and we are within the spinlock anyways, we have this workaround. */
2251 if (atomic_read(&mdev->ap_bio_cnt) > mxb)
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002252 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002253 if (test_bit(BITMAP_IO, &mdev->flags))
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002254 return false;
2255 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002256}
2257
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002258static inline bool inc_ap_bio_cond(struct drbd_conf *mdev, int count)
Philipp Reisner8869d682010-11-17 18:24:19 +01002259{
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002260 bool rv = false;
Philipp Reisner8869d682010-11-17 18:24:19 +01002261
Philipp Reisner87eeee42011-01-19 14:16:30 +01002262 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002263 rv = may_inc_ap_bio(mdev);
Philipp Reisner8869d682010-11-17 18:24:19 +01002264 if (rv)
2265 atomic_add(count, &mdev->ap_bio_cnt);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002266 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner8869d682010-11-17 18:24:19 +01002267
2268 return rv;
2269}
2270
Philipp Reisner9a25a042010-05-10 16:42:23 +02002271static inline void inc_ap_bio(struct drbd_conf *mdev, int count)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002272{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002273 /* we wait here
2274 * as long as the device is suspended
2275 * until the bitmap is no longer on the fly during connection
2276 * handshake as long as we would exeed the max_buffer limit.
2277 *
2278 * to avoid races with the reconnect code,
2279 * we need to atomic_inc within the spinlock. */
2280
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002281 wait_event(mdev->misc_wait, inc_ap_bio_cond(mdev, count));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002282}
2283
2284static inline void dec_ap_bio(struct drbd_conf *mdev)
2285{
2286 int mxb = drbd_get_max_buffers(mdev);
2287 int ap_bio = atomic_dec_return(&mdev->ap_bio_cnt);
2288
2289 D_ASSERT(ap_bio >= 0);
2290 /* this currently does wake_up for every dec_ap_bio!
2291 * maybe rather introduce some type of hysteresis?
2292 * e.g. (ap_bio == mxb/2 || ap_bio == 0) ? */
2293 if (ap_bio < mxb)
2294 wake_up(&mdev->misc_wait);
2295 if (ap_bio == 0 && test_bit(BITMAP_IO, &mdev->flags)) {
2296 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002297 drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298 }
2299}
2300
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002301static inline int drbd_set_ed_uuid(struct drbd_conf *mdev, u64 val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002302{
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002303 int changed = mdev->ed_uuid != val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002304 mdev->ed_uuid = val;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002305 return changed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002306}
2307
Philipp Reisnerb411b362009-09-25 16:07:19 -07002308static inline int drbd_queue_order_type(struct drbd_conf *mdev)
2309{
2310 /* sorry, we currently have no working implementation
2311 * of distributed TCQ stuff */
2312#ifndef QUEUE_ORDERED_NONE
2313#define QUEUE_ORDERED_NONE 0
2314#endif
2315 return QUEUE_ORDERED_NONE;
2316}
2317
Philipp Reisnerb411b362009-09-25 16:07:19 -07002318static inline void drbd_md_flush(struct drbd_conf *mdev)
2319{
2320 int r;
2321
Philipp Reisnera8a4e512010-08-25 10:21:04 +02002322 if (test_bit(MD_NO_FUA, &mdev->flags))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002323 return;
2324
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02002325 r = blkdev_issue_flush(mdev->ldev->md_bdev, GFP_KERNEL, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002326 if (r) {
Philipp Reisnera8a4e512010-08-25 10:21:04 +02002327 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002328 dev_err(DEV, "meta data flush failed with status %d, disabling md-flushes\n", r);
2329 }
2330}
2331
2332#endif