drbd: Moved the state functions into its own source file
[deliverable/linux.git] / drivers / block / drbd / drbd_int.h
CommitLineData
b411b363
PR
1/*
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>
132cc538 37#include <linux/ratelimit.h>
b411b363
PR
38#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>
70c71606 45#include <linux/prefetch.h>
b8907339
PR
46#include <linux/drbd.h>
47#include "drbd_state.h"
b411b363
PR
48
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
b411b363
PR
78/* 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
b411b363
PR
92#define ID_IN_SYNC (4711ULL)
93#define ID_OUT_OF_SYNC (4712ULL)
b411b363 94#define ID_SYNCER (-1ULL)
579b57ed 95
4a23f264 96#define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
b411b363
PR
97
98struct drbd_conf;
2111438b 99struct drbd_tconn;
b411b363
PR
100
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
841ce241
AG
108/**
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 })
b411b363
PR
120
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 */
6b4388ac 132 DRBD_FAULT_RECEIVE = 9, /* Changes some bytes upon receiving a [rs]data block */
b411b363
PR
133
134 DRBD_FAULT_MAX,
135};
136
b411b363
PR
137extern unsigned int
138_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type);
0cf9d27e 139
b411b363
PR
140static inline int
141drbd_insert_fault(struct drbd_conf *mdev, unsigned int type) {
0cf9d27e 142#ifdef CONFIG_DRBD_FAULT_INJECTION
b411b363
PR
143 return fault_rate &&
144 (enable_faults & (1<<type)) &&
145 _drbd_insert_fault(mdev, type);
b411b363 146#else
0cf9d27e 147 return 0;
b411b363 148#endif
0cf9d27e 149}
b411b363
PR
150
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 */
d8763023 164enum drbd_packet {
b411b363
PR
165 /* 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 */
0ced55a3
PR
208 /* 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 */
73a01a18 211 P_OUT_OF_SYNC = 0x28, /* Mark as out of sync (Outrunning), data socket */
d612d309 212 P_RS_CANCEL = 0x29, /* meta: Used to cancel RS_DATA_REQUEST packet by SyncSource */
b411b363 213
d612d309 214 P_MAX_CMD = 0x2A,
b411b363
PR
215 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
d8763023 226extern const char *cmdname(enum drbd_packet cmd);
b411b363
PR
227
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 */
0b70a13d 282struct p_header80 {
b411b363
PR
283 u32 magic;
284 u16 command;
285 u16 length; /* bytes of data after this header */
b411b363 286} __packed;
0b70a13d
PR
287
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;
00b42537 292 u32 length; /* Use only 24 bits of that. Ignore the highest 8 bit. */
0b70a13d
PR
293} __packed;
294
c012949a
PR
295struct p_header {
296 union {
297 struct p_header80 h80;
298 struct p_header95 h95;
299 };
300 u8 payload[0];
0b70a13d 301};
b411b363
PR
302
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 */
76d2e7ec
PR
320#define DP_HARDBARRIER 1 /* depricated */
321#define DP_RW_SYNC 2 /* equals REQ_SYNC */
b411b363 322#define DP_MAY_SET_IN_SYNC 4
721a9602 323#define DP_UNPLUG 8 /* not used anymore */
76d2e7ec
PR
324#define DP_FUA 16 /* equals REQ_FUA */
325#define DP_FLUSH 32 /* equals REQ_FLUSH */
326#define DP_DISCARD 64 /* equals REQ_DISCARD */
b411b363
PR
327
328struct p_data {
c012949a 329 struct p_header head;
b411b363
PR
330 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 {
c012949a 345 struct p_header head;
b411b363
PR
346 u64 sector;
347 u64 block_id;
348 u32 blksize;
349 u32 seq_num;
350} __packed;
351
352
353struct p_block_req {
c012949a 354 struct p_header head;
b411b363
PR
355 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 {
c012949a 371 struct p_header head; /* Note: You must always use a h80 here */
b411b363
PR
372 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 {
c012949a 386 struct p_header head;
b411b363
PR
387 u32 barrier; /* barrier number _handle_ only */
388 u32 pad; /* to multiple of 8 Byte */
389} __packed;
390
391struct p_barrier_ack {
c012949a 392 struct p_header head;
b411b363
PR
393 u32 barrier;
394 u32 set_size;
395} __packed;
396
397struct p_rs_param {
c012949a 398 struct p_header head;
b411b363
PR
399 u32 rate;
400
401 /* Since protocol version 88 and higher. */
402 char verify_alg[0];
403} __packed;
404
405struct p_rs_param_89 {
c012949a 406 struct p_header head;
b411b363
PR
407 u32 rate;
408 /* protocol version 89: */
409 char verify_alg[SHARED_SECRET_MAX];
410 char csums_alg[SHARED_SECRET_MAX];
411} __packed;
412
8e26f9cc 413struct p_rs_param_95 {
c012949a 414 struct p_header head;
8e26f9cc
PR
415 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
cf14c2e9
PR
424enum drbd_conn_flags {
425 CF_WANT_LOSE = 1,
426 CF_DRY_RUN = 2,
427};
428
b411b363 429struct p_protocol {
c012949a 430 struct p_header head;
b411b363
PR
431 u32 protocol;
432 u32 after_sb_0p;
433 u32 after_sb_1p;
434 u32 after_sb_2p;
cf14c2e9 435 u32 conn_flags;
b411b363
PR
436 u32 two_primaries;
437
438 /* Since protocol version 87 and higher. */
439 char integrity_alg[0];
440
441} __packed;
442
443struct p_uuids {
c012949a 444 struct p_header head;
b411b363
PR
445 u64 uuid[UI_EXTENDED_SIZE];
446} __packed;
447
448struct p_rs_uuid {
c012949a 449 struct p_header head;
b411b363
PR
450 u64 uuid;
451} __packed;
452
453struct p_sizes {
c012949a 454 struct p_header head;
b411b363
PR
455 u64 d_size; /* size of disk */
456 u64 u_size; /* user requested size */
457 u64 c_size; /* current exported size */
1816a2b4 458 u32 max_bio_size; /* Maximal size of a BIO */
e89b591c
PR
459 u16 queue_order_type; /* not yet implemented in DRBD*/
460 u16 dds_flags; /* use enum dds_flags here. */
b411b363
PR
461} __packed;
462
463struct p_state {
c012949a 464 struct p_header head;
b411b363
PR
465 u32 state;
466} __packed;
467
468struct p_req_state {
c012949a 469 struct p_header head;
b411b363
PR
470 u32 mask;
471 u32 val;
472} __packed;
473
474struct p_req_state_reply {
c012949a 475 struct p_header head;
b411b363
PR
476 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 {
c012949a 490 struct p_header head;
b411b363
PR
491 u64 block_id;
492 u32 seq_num;
493 u32 pad;
494} __packed;
495
73a01a18 496struct p_block_desc {
c012949a 497 struct p_header head;
73a01a18
PR
498 u64 sector;
499 u32 blksize;
500 u32 pad; /* to multiple of 8 Byte */
501} __packed;
502
b411b363
PR
503/* 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 {
c012949a 513 struct p_header head;
b411b363
PR
514 /* (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
0b70a13d 524struct p_delay_probe93 {
c012949a 525 struct p_header head;
0b70a13d
PR
526 u32 seq_num; /* sequence number to match the two probe packets */
527 u32 offset; /* usecs the probe got sent after the reference time point */
0ced55a3
PR
528} __packed;
529
b411b363
PR
530/* 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
25985edc 572 * most architectures have used for a long time.
b411b363 573 */
c012949a 574#define BM_PACKET_PAYLOAD_BYTES (4096 - sizeof(struct p_header))
b411b363
PR
575#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 {
c012949a 583 struct p_header header;
b411b363
PR
584 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;
8e26f9cc 590 struct p_rs_param_95 rs_param_95;
b411b363
PR
591 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;
02918be2
PR
598 struct p_delay_probe93 delay_probe93;
599 struct p_rs_uuid rs_uuid;
73a01a18 600 struct p_block_desc block_desc;
b411b363
PR
601} __packed;
602
603/**********************************************************************/
604enum drbd_thread_state {
e77a0a5c
AG
605 NONE,
606 RUNNING,
607 EXITING,
608 RESTARTING
b411b363
PR
609};
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;
619};
620
621static inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
622{
623 /* THINK testing the t_state seems to be uncritical in all cases
624 * (but thread_{start,stop}), so we can read it *without* the lock.
625 * --lge */
626
627 smp_rmb();
628 return thi->t_state;
629}
630
b411b363
PR
631struct drbd_work;
632typedef int (*drbd_work_cb)(struct drbd_conf *, struct drbd_work *, int cancel);
633struct drbd_work {
634 struct list_head list;
635 drbd_work_cb cb;
636};
637
ace652ac
AG
638#include "drbd_interval.h"
639
b411b363
PR
640struct drbd_request {
641 struct drbd_work w;
642 struct drbd_conf *mdev;
643
644 /* if local IO is not allowed, will be NULL.
645 * if local IO _is_ allowed, holds the locally submitted bio clone,
646 * or, after local IO completion, the ERR_PTR(error).
647 * see drbd_endio_pri(). */
648 struct bio *private_bio;
649
ace652ac 650 struct drbd_interval i;
b411b363
PR
651 unsigned int epoch; /* barrier_nr */
652
653 /* barrier_nr: used to check on "completion" whether this req was in
654 * the current epoch, and we therefore have to close it,
655 * starting a new epoch...
656 */
657
b411b363
PR
658 struct list_head tl_requests; /* ring list in the transfer log */
659 struct bio *master_bio; /* master bio pointer */
660 unsigned long rq_state; /* see comments above _req_mod() */
661 int seq_num;
662 unsigned long start_time;
663};
664
665struct drbd_tl_epoch {
666 struct drbd_work w;
667 struct list_head requests; /* requests before */
668 struct drbd_tl_epoch *next; /* pointer to the next barrier */
669 unsigned int br_number; /* the barriers identifier. */
7e602c0a 670 int n_writes; /* number of requests attached before this barrier */
b411b363
PR
671};
672
b411b363
PR
673struct drbd_epoch {
674 struct list_head list;
675 unsigned int barrier_nr;
676 atomic_t epoch_size; /* increased on every request added. */
677 atomic_t active; /* increased on every req. added, and dec on every finished. */
678 unsigned long flags;
679};
680
681/* drbd_epoch flag bits */
682enum {
b411b363 683 DE_HAVE_BARRIER_NUMBER,
b411b363
PR
684};
685
686enum epoch_event {
687 EV_PUT,
688 EV_GOT_BARRIER_NR,
b411b363 689 EV_BECAME_LAST,
b411b363
PR
690 EV_CLEANUP = 32, /* used as flag */
691};
692
b411b363
PR
693struct drbd_wq_barrier {
694 struct drbd_work w;
695 struct completion done;
696};
697
698struct digest_info {
699 int digest_size;
700 void *digest;
701};
702
f6ffca9f 703struct drbd_peer_request {
45bb912b 704 struct drbd_work w;
85719573 705 struct drbd_epoch *epoch; /* for writes */
45bb912b
LE
706 struct drbd_conf *mdev;
707 struct page *pages;
708 atomic_t pending_bios;
010f6e67 709 struct drbd_interval i;
45bb912b
LE
710 /* see comments on ee flag bits below */
711 unsigned long flags;
85719573
PR
712 union {
713 u64 block_id;
714 struct digest_info *digest;
715 };
45bb912b
LE
716};
717
718/* ee flag bits.
719 * While corresponding bios are in flight, the only modification will be
720 * set_bit WAS_ERROR, which has to be atomic.
721 * If no bios are in flight yet, or all have been completed,
722 * non-atomic modification to ee->flags is ok.
723 */
b411b363
PR
724enum {
725 __EE_CALL_AL_COMPLETE_IO,
b411b363 726 __EE_MAY_SET_IN_SYNC,
45bb912b 727
45bb912b
LE
728 /* In case a barrier failed,
729 * we need to resubmit without the barrier flag. */
730 __EE_RESUBMITTED,
731
6c852bec 732 /* we may have several bios per peer request.
45bb912b
LE
733 * if any of those fail, we set this flag atomically
734 * from the endio callback */
735 __EE_WAS_ERROR,
c36c3ced
LE
736
737 /* This ee has a pointer to a digest instead of a block id */
738 __EE_HAS_DIGEST,
b411b363
PR
739};
740#define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
b411b363 741#define EE_MAY_SET_IN_SYNC (1<<__EE_MAY_SET_IN_SYNC)
45bb912b
LE
742#define EE_RESUBMITTED (1<<__EE_RESUBMITTED)
743#define EE_WAS_ERROR (1<<__EE_WAS_ERROR)
c36c3ced 744#define EE_HAS_DIGEST (1<<__EE_HAS_DIGEST)
b411b363
PR
745
746/* global flag bits */
747enum {
25985edc 748 CREATE_BARRIER, /* next P_DATA is preceded by a P_BARRIER */
b411b363
PR
749 SIGNAL_ASENDER, /* whether asender wants to be interrupted */
750 SEND_PING, /* whether asender should send a ping asap */
751
b411b363
PR
752 UNPLUG_QUEUED, /* only relevant with kernel 2.4 */
753 UNPLUG_REMOTE, /* sending a "UnplugRemote" could help */
754 MD_DIRTY, /* current uuids and flags not yet on disk */
755 DISCARD_CONCURRENT, /* Set on one node, cleared on the peer! */
756 USE_DEGR_WFC_T, /* degr-wfc-timeout instead of wfc-timeout. */
757 CLUSTER_ST_CHANGE, /* Cluster wide state change going on... */
758 CL_ST_CHG_SUCCESS,
759 CL_ST_CHG_FAIL,
760 CRASHED_PRIMARY, /* This node was a crashed primary.
761 * Gets cleared when the state.conn
762 * goes into C_CONNECTED state. */
19f843aa 763 NO_BARRIER_SUPP, /* underlying block device doesn't implement barriers */
b411b363
PR
764 CONSIDER_RESYNC,
765
a8a4e51e 766 MD_NO_FUA, /* Users wants us to not use FUA/FLUSH on meta data dev */
b411b363
PR
767 SUSPEND_IO, /* suspend application io */
768 BITMAP_IO, /* suspend application io;
769 once no more io in flight, start bitmap io */
770 BITMAP_IO_QUEUED, /* Started bitmap IO */
82f59cc6
LE
771 GO_DISKLESS, /* Disk is being detached, on io-error or admin request. */
772 WAS_IO_ERROR, /* Local disk failed returned IO error */
b411b363
PR
773 RESYNC_AFTER_NEG, /* Resync after online grow after the attach&negotiate finished. */
774 NET_CONGESTED, /* The data socket is congested */
775
776 CONFIG_PENDING, /* serialization of (re)configuration requests.
777 * if set, also prevents the device from dying */
778 DEVICE_DYING, /* device became unconfigured,
779 * but worker thread is still handling the cleanup.
780 * reconfiguring (nl_disk_conf, nl_net_conf) is dissalowed,
781 * while this is set. */
782 RESIZE_PENDING, /* Size change detected locally, waiting for the response from
783 * the peer, if it changed there as well. */
cf14c2e9 784 CONN_DRY_RUN, /* Expect disconnect after resync handshake. */
309d1608 785 GOT_PING_ACK, /* set when we receive a ping_ack packet, misc wait gets woken */
43a5182c 786 NEW_CUR_UUID, /* Create new current UUID when thawing IO */
0778286a 787 AL_SUSPENDED, /* Activity logging is currently suspended. */
370a43e7 788 AHEAD_TO_SYNC_SOURCE, /* Ahead -> SyncSource queued */
b411b363
PR
789};
790
791struct drbd_bitmap; /* opaque for drbd_conf */
792
20ceb2b2
LE
793/* definition of bits in bm_flags to be used in drbd_bm_lock
794 * and drbd_bitmap_io and friends. */
795enum bm_flag {
796 /* do we need to kfree, or vfree bm_pages? */
797 BM_P_VMALLOCED = 0x10000, /* internal use only, will be masked out */
798
799 /* currently locked for bulk operation */
800 BM_LOCKED_MASK = 0x7,
801
802 /* in detail, that is: */
803 BM_DONT_CLEAR = 0x1,
804 BM_DONT_SET = 0x2,
805 BM_DONT_TEST = 0x4,
806
807 /* (test bit, count bit) allowed (common case) */
808 BM_LOCKED_TEST_ALLOWED = 0x3,
809
810 /* testing bits, as well as setting new bits allowed, but clearing bits
811 * would be unexpected. Used during bitmap receive. Setting new bits
812 * requires sending of "out-of-sync" information, though. */
813 BM_LOCKED_SET_ALLOWED = 0x1,
814
815 /* clear is not expected while bitmap is locked for bulk operation */
816};
817
818
b411b363
PR
819/* TODO sort members for performance
820 * MAYBE group them further */
821
822/* THINK maybe we actually want to use the default "event/%s" worker threads
823 * or similar in linux 2.6, which uses per cpu data and threads.
b411b363
PR
824 */
825struct drbd_work_queue {
826 struct list_head q;
827 struct semaphore s; /* producers up it, worker down()s it */
828 spinlock_t q_lock; /* to protect the list. */
829};
830
831struct drbd_socket {
832 struct drbd_work_queue work;
833 struct mutex mutex;
834 struct socket *socket;
835 /* this way we get our
836 * send/receive buffers off the stack */
837 union p_polymorph sbuf;
838 union p_polymorph rbuf;
839};
840
841struct drbd_md {
842 u64 md_offset; /* sector offset to 'super' block */
843
844 u64 la_size_sect; /* last agreed size, unit sectors */
845 u64 uuid[UI_SIZE];
846 u64 device_uuid;
847 u32 flags;
848 u32 md_size_sect;
849
850 s32 al_offset; /* signed relative sector offset to al area */
851 s32 bm_offset; /* signed relative sector offset to bitmap */
852
853 /* u32 al_nr_extents; important for restoring the AL
854 * is stored into sync_conf.al_extents, which in turn
855 * gets applied to act_log->nr_elements
856 */
857};
858
859/* for sync_conf and other types... */
860#define NL_PACKET(name, number, fields) struct name { fields };
861#define NL_INTEGER(pn,pr,member) int member;
862#define NL_INT64(pn,pr,member) __u64 member;
863#define NL_BIT(pn,pr,member) unsigned member:1;
864#define NL_STRING(pn,pr,member,len) unsigned char member[len]; int member ## _len;
865#include "linux/drbd_nl.h"
866
867struct drbd_backing_dev {
868 struct block_device *backing_bdev;
869 struct block_device *md_bdev;
b411b363
PR
870 struct drbd_md md;
871 struct disk_conf dc; /* The user provided config... */
872 sector_t known_size; /* last known size of that backing device */
873};
874
875struct drbd_md_io {
876 struct drbd_conf *mdev;
877 struct completion event;
878 int error;
879};
880
881struct bm_io_work {
882 struct drbd_work w;
883 char *why;
20ceb2b2 884 enum bm_flag flags;
b411b363
PR
885 int (*io_fn)(struct drbd_conf *mdev);
886 void (*done)(struct drbd_conf *mdev, int rv);
887};
888
889enum write_ordering_e {
890 WO_none,
891 WO_drain_io,
892 WO_bdev_flush,
b411b363
PR
893};
894
778f271d
PR
895struct fifo_buffer {
896 int *values;
897 unsigned int head_index;
898 unsigned int size;
899};
900
2111438b
PR
901struct drbd_tconn { /* is a resource from the config file */
902 char *name; /* Resource name */
903 struct list_head all_tconn; /* List of all drbd_tconn, prot by global_state_lock */
904 struct drbd_conf *volume0; /* TODO: Remove me again */
905
906 struct net_conf *net_conf; /* protected by get_net_conf() and put_net_conf() */
b2fb6dbe
PR
907 atomic_t net_cnt; /* Users of net_conf */
908 wait_queue_head_t net_cnt_wait;
e42325a5
PR
909
910 struct drbd_socket data; /* data/barrier/cstate/parameter packets */
911 struct drbd_socket meta; /* ping/ack (metadata) packets */
31890f4a
PR
912 int agreed_pro_version; /* actually used protocol version */
913 unsigned long last_received; /* in jiffies, either socket */
914 unsigned int ko_count;
e6b3ea83 915
87eeee41
PR
916 spinlock_t req_lock;
917 struct drbd_tl_epoch *unused_spare_tle; /* for pre-allocation */
918 struct drbd_tl_epoch *newest_tle;
919 struct drbd_tl_epoch *oldest_tle;
920 struct list_head out_of_sequence_requests;
921
a0638456
PR
922 struct crypto_hash *cram_hmac_tfm;
923 struct crypto_hash *integrity_w_tfm; /* to be used by the worker thread */
924 struct crypto_hash *integrity_r_tfm; /* to be used by the receiver thread */
925 void *int_dig_out;
926 void *int_dig_in;
927 void *int_dig_vv;
928
e6b3ea83
PR
929 struct drbd_thread receiver;
930 struct drbd_thread worker;
931 struct drbd_thread asender;
2111438b
PR
932};
933
b411b363 934struct drbd_conf {
2111438b
PR
935 struct drbd_tconn *tconn;
936 int vnr; /* volume number within the connection */
937
b411b363
PR
938 /* things that are stored as / read from meta data on disk */
939 unsigned long flags;
940
941 /* configured by drbdsetup */
b411b363
PR
942 struct syncer_conf sync_conf;
943 struct drbd_backing_dev *ldev __protected_by(local);
944
945 sector_t p_size; /* partner's disk size */
946 struct request_queue *rq_queue;
947 struct block_device *this_bdev;
948 struct gendisk *vdisk;
949
b411b363
PR
950 struct drbd_work resync_work,
951 unplug_work,
e9e6f3ec 952 go_diskless,
c4752ef1
PR
953 md_sync_work,
954 start_resync_work;
b411b363
PR
955 struct timer_list resync_timer;
956 struct timer_list md_sync_timer;
370a43e7 957 struct timer_list start_resync_timer;
7fde2be9 958 struct timer_list request_timer;
ee15b038
LE
959#ifdef DRBD_DEBUG_MD_SYNC
960 struct {
961 unsigned int line;
962 const char* func;
963 } last_md_mark_dirty;
964#endif
b411b363
PR
965
966 /* Used after attach while negotiating new disk state. */
967 union drbd_state new_state_tmp;
968
969 union drbd_state state;
970 wait_queue_head_t misc_wait;
971 wait_queue_head_t state_wait; /* upon each state change. */
972 unsigned int send_cnt;
973 unsigned int recv_cnt;
974 unsigned int read_cnt;
975 unsigned int writ_cnt;
976 unsigned int al_writ_cnt;
977 unsigned int bm_writ_cnt;
978 atomic_t ap_bio_cnt; /* Requests we need to complete */
979 atomic_t ap_pending_cnt; /* AP data packets on the wire, ack expected */
980 atomic_t rs_pending_cnt; /* RS request/data packets on the wire */
981 atomic_t unacked_cnt; /* Need to send replys for */
982 atomic_t local_cnt; /* Waiting for local completion */
b2fb6dbe 983
dac1389c
AG
984 /* Interval tree of pending local requests */
985 struct rb_root read_requests;
de696716
AG
986 struct rb_root write_requests;
987
4b0715f0 988 /* blocks to resync in this run [unit BM_BLOCK_SIZE] */
b411b363 989 unsigned long rs_total;
4b0715f0 990 /* number of resync blocks that failed in this run */
b411b363
PR
991 unsigned long rs_failed;
992 /* Syncer's start time [unit jiffies] */
993 unsigned long rs_start;
994 /* cumulated time in PausedSyncX state [unit jiffies] */
995 unsigned long rs_paused;
1d7734a0
LE
996 /* skipped because csum was equal [unit BM_BLOCK_SIZE] */
997 unsigned long rs_same_csum;
998#define DRBD_SYNC_MARKS 8
999#define DRBD_SYNC_MARK_STEP (3*HZ)
b411b363 1000 /* block not up-to-date at mark [unit BM_BLOCK_SIZE] */
1d7734a0 1001 unsigned long rs_mark_left[DRBD_SYNC_MARKS];
b411b363 1002 /* marks's time [unit jiffies] */
1d7734a0
LE
1003 unsigned long rs_mark_time[DRBD_SYNC_MARKS];
1004 /* current index into rs_mark_{left,time} */
1005 int rs_last_mark;
b411b363
PR
1006
1007 /* where does the admin want us to start? (sector) */
1008 sector_t ov_start_sector;
1009 /* where are we now? (sector) */
1010 sector_t ov_position;
1011 /* Start sector of out of sync range (to merge printk reporting). */
1012 sector_t ov_last_oos_start;
1013 /* size of out-of-sync range in sectors. */
1014 sector_t ov_last_oos_size;
1015 unsigned long ov_left; /* in bits */
1016 struct crypto_hash *csums_tfm;
1017 struct crypto_hash *verify_tfm;
1018
b411b363
PR
1019 struct drbd_bitmap *bitmap;
1020 unsigned long bm_resync_fo; /* bit offset for drbd_bm_find_next */
1021
1022 /* Used to track operations of resync... */
1023 struct lru_cache *resync;
1024 /* Number of locked elements in resync LRU */
1025 unsigned int resync_locked;
1026 /* resync extent number waiting for application requests */
1027 unsigned int resync_wenr;
1028
1029 int open_cnt;
1030 u64 *p_uuid;
1031 struct drbd_epoch *current_epoch;
1032 spinlock_t epoch_lock;
1033 unsigned int epochs;
1034 enum write_ordering_e write_ordering;
85719573
PR
1035 struct list_head active_ee; /* IO in progress (P_DATA gets written to disk) */
1036 struct list_head sync_ee; /* IO in progress (P_RS_DATA_REPLY gets written to disk) */
18b75d75
AG
1037 struct list_head done_ee; /* need to send P_WRITE_ACK */
1038 struct list_head read_ee; /* [RS]P_DATA_REQUEST being read */
b411b363 1039 struct list_head net_ee; /* zero-copy network send in progress */
b411b363 1040
b411b363 1041 int next_barrier_nr;
b411b363 1042 struct list_head resync_reads;
435f0740
LE
1043 atomic_t pp_in_use; /* allocated from page pool */
1044 atomic_t pp_in_use_by_net; /* sendpage()d, still referenced by tcp */
b411b363
PR
1045 wait_queue_head_t ee_wait;
1046 struct page *md_io_page; /* one page buffer for md_io */
1047 struct page *md_io_tmpp; /* for logical_block_size != 512 */
1048 struct mutex md_io_mutex; /* protects the md_io_buffer */
1049 spinlock_t al_lock;
1050 wait_queue_head_t al_wait;
1051 struct lru_cache *act_log; /* activity log */
1052 unsigned int al_tr_number;
1053 int al_tr_cycle;
1054 int al_tr_pos; /* position of the next transaction in the journal */
b411b363
PR
1055 wait_queue_head_t seq_wait;
1056 atomic_t packet_seq;
1057 unsigned int peer_seq;
1058 spinlock_t peer_seq_lock;
1059 unsigned int minor;
1060 unsigned long comm_bm_set; /* communicated number of set bits. */
1061 cpumask_var_t cpu_mask;
1062 struct bm_io_work bm_io_work;
1063 u64 ed_uuid; /* UUID of the exposed data */
1064 struct mutex state_mutex;
1065 char congestion_reason; /* Why we where congested... */
1d7734a0
LE
1066 atomic_t rs_sect_in; /* for incoming resync data rate, SyncTarget */
1067 atomic_t rs_sect_ev; /* for submitted resync data rate, both */
1068 int rs_last_sect_ev; /* counter to compare with */
1069 int rs_last_events; /* counter of read or write "events" (unit sectors)
1070 * on the lower level device when we last looked. */
1071 int c_sync_rate; /* current resync rate after syncer throttle magic */
778f271d
PR
1072 struct fifo_buffer rs_plan_s; /* correction values of resync planer */
1073 int rs_in_flight; /* resync sectors in flight (to proxy, in proxy and from proxy) */
25985edc 1074 int rs_planed; /* resync sectors already planned */
759fbdfb 1075 atomic_t ap_in_flight; /* App sectors in flight (waiting for ack) */
99432fcc
PR
1076 int peer_max_bio_size;
1077 int local_max_bio_size;
b411b363
PR
1078};
1079
1080static inline struct drbd_conf *minor_to_mdev(unsigned int minor)
1081{
1082 struct drbd_conf *mdev;
1083
1084 mdev = minor < minor_count ? minor_table[minor] : NULL;
1085
1086 return mdev;
1087}
1088
1089static inline unsigned int mdev_to_minor(struct drbd_conf *mdev)
1090{
1091 return mdev->minor;
1092}
1093
25985edc 1094/* returns 1 if it was successful,
b411b363
PR
1095 * returns 0 if there was no data socket.
1096 * so wherever you are going to use the data.socket, e.g. do
1097 * if (!drbd_get_data_sock(mdev))
1098 * return 0;
1099 * CODE();
1100 * drbd_put_data_sock(mdev);
1101 */
1102static inline int drbd_get_data_sock(struct drbd_conf *mdev)
1103{
e42325a5 1104 mutex_lock(&mdev->tconn->data.mutex);
b411b363
PR
1105 /* drbd_disconnect() could have called drbd_free_sock()
1106 * while we were waiting in down()... */
e42325a5
PR
1107 if (unlikely(mdev->tconn->data.socket == NULL)) {
1108 mutex_unlock(&mdev->tconn->data.mutex);
b411b363
PR
1109 return 0;
1110 }
1111 return 1;
1112}
1113
1114static inline void drbd_put_data_sock(struct drbd_conf *mdev)
1115{
e42325a5 1116 mutex_unlock(&mdev->tconn->data.mutex);
b411b363
PR
1117}
1118
1119/*
1120 * function declarations
1121 *************************/
1122
1123/* drbd_main.c */
1124
e89b591c
PR
1125enum dds_flags {
1126 DDSF_FORCED = 1,
1127 DDSF_NO_RESYNC = 2, /* Do not run a resync for the new space */
1128};
1129
b411b363 1130extern void drbd_init_set_defaults(struct drbd_conf *mdev);
b411b363
PR
1131extern int drbd_thread_start(struct drbd_thread *thi);
1132extern void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait);
1133#ifdef CONFIG_SMP
1134extern void drbd_thread_current_set_cpu(struct drbd_conf *mdev);
1135extern void drbd_calc_cpu_mask(struct drbd_conf *mdev);
1136#else
1137#define drbd_thread_current_set_cpu(A) ({})
1138#define drbd_calc_cpu_mask(A) ({})
1139#endif
1140extern void drbd_free_resources(struct drbd_conf *mdev);
1141extern void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
1142 unsigned int set_size);
1143extern void tl_clear(struct drbd_conf *mdev);
1144extern void _tl_add_barrier(struct drbd_conf *, struct drbd_tl_epoch *);
1145extern void drbd_free_sock(struct drbd_conf *mdev);
1146extern int drbd_send(struct drbd_conf *mdev, struct socket *sock,
1147 void *buf, size_t size, unsigned msg_flags);
1148extern int drbd_send_protocol(struct drbd_conf *mdev);
1149extern int drbd_send_uuids(struct drbd_conf *mdev);
1150extern int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev);
5a22db89 1151extern int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev);
e89b591c 1152extern int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags);
b411b363
PR
1153extern int _drbd_send_state(struct drbd_conf *mdev);
1154extern int drbd_send_state(struct drbd_conf *mdev);
1155extern int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
d8763023
AG
1156 enum drbd_packet cmd, struct p_header *h,
1157 size_t size, unsigned msg_flags);
b411b363
PR
1158#define USE_DATA_SOCKET 1
1159#define USE_META_SOCKET 0
1160extern int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
d8763023
AG
1161 enum drbd_packet cmd, struct p_header *h, size_t size);
1162extern int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packet cmd,
1163 char *data, size_t size);
b411b363
PR
1164extern int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc);
1165extern int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr,
1166 u32 set_size);
f6ffca9f
AG
1167extern int drbd_send_ack(struct drbd_conf *, enum drbd_packet,
1168 struct drbd_peer_request *);
d8763023
AG
1169extern int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
1170 struct p_block_req *rp);
1171extern int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
1172 struct p_data *dp, int data_size);
1173extern int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
b411b363 1174 sector_t sector, int blksize, u64 block_id);
73a01a18 1175extern int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req);
f6ffca9f
AG
1176extern int drbd_send_block(struct drbd_conf *, enum drbd_packet,
1177 struct drbd_peer_request *);
b411b363 1178extern int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req);
b411b363
PR
1179extern int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1180 sector_t sector, int size, u64 block_id);
d8763023
AG
1181extern int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector,
1182 int size, void *digest, int digest_size,
1183 enum drbd_packet cmd);
b411b363
PR
1184extern int drbd_send_ov_request(struct drbd_conf *mdev,sector_t sector,int size);
1185
1186extern int drbd_send_bitmap(struct drbd_conf *mdev);
1187extern int _drbd_send_bitmap(struct drbd_conf *mdev);
bf885f8a 1188extern int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode);
b411b363
PR
1189extern void drbd_free_bc(struct drbd_backing_dev *ldev);
1190extern void drbd_mdev_cleanup(struct drbd_conf *mdev);
62b0da3a 1191void drbd_print_uuids(struct drbd_conf *mdev, const char *text);
b411b363 1192
b411b363
PR
1193extern void drbd_md_sync(struct drbd_conf *mdev);
1194extern int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev);
b411b363
PR
1195extern void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1196extern void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
1197extern void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1198extern void _drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
1199extern void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local);
1200extern void drbd_md_set_flag(struct drbd_conf *mdev, int flags) __must_hold(local);
1201extern void drbd_md_clear_flag(struct drbd_conf *mdev, int flags)__must_hold(local);
1202extern int drbd_md_test_flag(struct drbd_backing_dev *, int);
ee15b038 1203#ifndef DRBD_DEBUG_MD_SYNC
b411b363 1204extern void drbd_md_mark_dirty(struct drbd_conf *mdev);
ee15b038
LE
1205#else
1206#define drbd_md_mark_dirty(m) drbd_md_mark_dirty_(m, __LINE__ , __func__ )
1207extern void drbd_md_mark_dirty_(struct drbd_conf *mdev,
1208 unsigned int line, const char *func);
1209#endif
b411b363
PR
1210extern void drbd_queue_bitmap_io(struct drbd_conf *mdev,
1211 int (*io_fn)(struct drbd_conf *),
1212 void (*done)(struct drbd_conf *, int),
20ceb2b2
LE
1213 char *why, enum bm_flag flags);
1214extern int drbd_bitmap_io(struct drbd_conf *mdev,
1215 int (*io_fn)(struct drbd_conf *),
1216 char *why, enum bm_flag flags);
b411b363
PR
1217extern int drbd_bmio_set_n_write(struct drbd_conf *mdev);
1218extern int drbd_bmio_clear_n_write(struct drbd_conf *mdev);
e9e6f3ec 1219extern void drbd_go_diskless(struct drbd_conf *mdev);
82f59cc6 1220extern void drbd_ldev_destroy(struct drbd_conf *mdev);
b411b363
PR
1221
1222
1223/* Meta data layout
1224 We reserve a 128MB Block (4k aligned)
1225 * either at the end of the backing device
3ad2f3fb 1226 * or on a separate meta data device. */
b411b363
PR
1227
1228#define MD_RESERVED_SECT (128LU << 11) /* 128 MB, unit sectors */
1229/* The following numbers are sectors */
1230#define MD_AL_OFFSET 8 /* 8 Sectors after start of meta area */
1231#define MD_AL_MAX_SIZE 64 /* = 32 kb LOG ~ 3776 extents ~ 14 GB Storage */
1232/* Allows up to about 3.8TB */
1233#define MD_BM_OFFSET (MD_AL_OFFSET + MD_AL_MAX_SIZE)
1234
1235/* Since the smalles IO unit is usually 512 byte */
1236#define MD_SECTOR_SHIFT 9
1237#define MD_SECTOR_SIZE (1<<MD_SECTOR_SHIFT)
1238
1239/* activity log */
1240#define AL_EXTENTS_PT ((MD_SECTOR_SIZE-12)/8-1) /* 61 ; Extents per 512B sector */
1241#define AL_EXTENT_SHIFT 22 /* One extent represents 4M Storage */
1242#define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
1243
1244#if BITS_PER_LONG == 32
1245#define LN2_BPL 5
1246#define cpu_to_lel(A) cpu_to_le32(A)
1247#define lel_to_cpu(A) le32_to_cpu(A)
1248#elif BITS_PER_LONG == 64
1249#define LN2_BPL 6
1250#define cpu_to_lel(A) cpu_to_le64(A)
1251#define lel_to_cpu(A) le64_to_cpu(A)
1252#else
1253#error "LN2 of BITS_PER_LONG unknown!"
1254#endif
1255
1256/* resync bitmap */
1257/* 16MB sized 'bitmap extent' to track syncer usage */
1258struct bm_extent {
1259 int rs_left; /* number of bits set (out of sync) in this extent. */
1260 int rs_failed; /* number of failed resync requests in this extent. */
1261 unsigned long flags;
1262 struct lc_element lce;
1263};
1264
1265#define BME_NO_WRITES 0 /* bm_extent.flags: no more requests on this one! */
1266#define BME_LOCKED 1 /* bm_extent.flags: syncer active on this one. */
e3555d85 1267#define BME_PRIORITY 2 /* finish resync IO on this extent ASAP! App IO waiting! */
b411b363
PR
1268
1269/* drbd_bitmap.c */
1270/*
1271 * We need to store one bit for a block.
1272 * Example: 1GB disk @ 4096 byte blocks ==> we need 32 KB bitmap.
1273 * Bit 0 ==> local node thinks this block is binary identical on both nodes
1274 * Bit 1 ==> local node thinks this block needs to be synced.
1275 */
1276
8e26f9cc
PR
1277#define SLEEP_TIME (HZ/10)
1278
b411b363
PR
1279#define BM_BLOCK_SHIFT 12 /* 4k per bit */
1280#define BM_BLOCK_SIZE (1<<BM_BLOCK_SHIFT)
1281/* (9+3) : 512 bytes @ 8 bits; representing 16M storage
1282 * per sector of on disk bitmap */
1283#define BM_EXT_SHIFT (BM_BLOCK_SHIFT + MD_SECTOR_SHIFT + 3) /* = 24 */
1284#define BM_EXT_SIZE (1<<BM_EXT_SHIFT)
1285
1286#if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
1287#error "HAVE YOU FIXED drbdmeta AS WELL??"
1288#endif
1289
1290/* thus many _storage_ sectors are described by one bit */
1291#define BM_SECT_TO_BIT(x) ((x)>>(BM_BLOCK_SHIFT-9))
1292#define BM_BIT_TO_SECT(x) ((sector_t)(x)<<(BM_BLOCK_SHIFT-9))
1293#define BM_SECT_PER_BIT BM_BIT_TO_SECT(1)
1294
1295/* bit to represented kilo byte conversion */
1296#define Bit2KB(bits) ((bits)<<(BM_BLOCK_SHIFT-10))
1297
1298/* in which _bitmap_ extent (resp. sector) the bit for a certain
1299 * _storage_ sector is located in */
1300#define BM_SECT_TO_EXT(x) ((x)>>(BM_EXT_SHIFT-9))
1301
1302/* how much _storage_ sectors we have per bitmap sector */
1303#define BM_EXT_TO_SECT(x) ((sector_t)(x) << (BM_EXT_SHIFT-9))
1304#define BM_SECT_PER_EXT BM_EXT_TO_SECT(1)
1305
1306/* in one sector of the bitmap, we have this many activity_log extents. */
1307#define AL_EXT_PER_BM_SECT (1 << (BM_EXT_SHIFT - AL_EXTENT_SHIFT))
1308#define BM_WORDS_PER_AL_EXT (1 << (AL_EXTENT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
1309
1310#define BM_BLOCKS_PER_BM_EXT_B (BM_EXT_SHIFT - BM_BLOCK_SHIFT)
1311#define BM_BLOCKS_PER_BM_EXT_MASK ((1<<BM_BLOCKS_PER_BM_EXT_B) - 1)
1312
1313/* the extent in "PER_EXTENT" below is an activity log extent
1314 * we need that many (long words/bytes) to store the bitmap
1315 * of one AL_EXTENT_SIZE chunk of storage.
1316 * we can store the bitmap for that many AL_EXTENTS within
1317 * one sector of the _on_disk_ bitmap:
1318 * bit 0 bit 37 bit 38 bit (512*8)-1
1319 * ...|........|........|.. // ..|........|
1320 * sect. 0 `296 `304 ^(512*8*8)-1
1321 *
1322#define BM_WORDS_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / BITS_PER_LONG )
1323#define BM_BYTES_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / 8 ) // 128
1324#define BM_EXT_PER_SECT ( 512 / BM_BYTES_PER_EXTENT ) // 4
1325 */
1326
1327#define DRBD_MAX_SECTORS_32 (0xffffffffLU)
1328#define DRBD_MAX_SECTORS_BM \
1329 ((MD_RESERVED_SECT - MD_BM_OFFSET) * (1LL<<(BM_EXT_SHIFT-9)))
1330#if DRBD_MAX_SECTORS_BM < DRBD_MAX_SECTORS_32
1331#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1332#define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_BM
36bfc7e2 1333#elif !defined(CONFIG_LBDAF) && BITS_PER_LONG == 32
b411b363
PR
1334#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_32
1335#define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_32
1336#else
1337#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1338/* 16 TB in units of sectors */
1339#if BITS_PER_LONG == 32
1340/* adjust by one page worth of bitmap,
1341 * so we won't wrap around in drbd_bm_find_next_bit.
1342 * you should use 64bit OS for that much storage, anyways. */
1343#define DRBD_MAX_SECTORS_FLEX BM_BIT_TO_SECT(0xffff7fff)
1344#else
4b0715f0
LE
1345/* we allow up to 1 PiB now on 64bit architecture with "flexible" meta data */
1346#define DRBD_MAX_SECTORS_FLEX (1UL << 51)
1347/* corresponds to (1UL << 38) bits right now. */
b411b363
PR
1348#endif
1349#endif
1350
d5373389 1351#define HT_SHIFT 8
1816a2b4 1352#define DRBD_MAX_BIO_SIZE (1U<<(9+HT_SHIFT))
99432fcc 1353#define DRBD_MAX_BIO_SIZE_SAFE (1 << 12) /* Works always = 4k */
b411b363 1354
d5373389 1355#define DRBD_MAX_SIZE_H80_PACKET (1 << 15) /* The old header only allows packets up to 32Kib data */
b411b363
PR
1356
1357extern int drbd_bm_init(struct drbd_conf *mdev);
02d9a94b 1358extern int drbd_bm_resize(struct drbd_conf *mdev, sector_t sectors, int set_new_bits);
b411b363
PR
1359extern void drbd_bm_cleanup(struct drbd_conf *mdev);
1360extern void drbd_bm_set_all(struct drbd_conf *mdev);
1361extern void drbd_bm_clear_all(struct drbd_conf *mdev);
4b0715f0 1362/* set/clear/test only a few bits at a time */
b411b363
PR
1363extern int drbd_bm_set_bits(
1364 struct drbd_conf *mdev, unsigned long s, unsigned long e);
1365extern int drbd_bm_clear_bits(
1366 struct drbd_conf *mdev, unsigned long s, unsigned long e);
4b0715f0
LE
1367extern int drbd_bm_count_bits(
1368 struct drbd_conf *mdev, const unsigned long s, const unsigned long e);
1369/* bm_set_bits variant for use while holding drbd_bm_lock,
1370 * may process the whole bitmap in one go */
b411b363
PR
1371extern void _drbd_bm_set_bits(struct drbd_conf *mdev,
1372 const unsigned long s, const unsigned long e);
1373extern int drbd_bm_test_bit(struct drbd_conf *mdev, unsigned long bitnr);
1374extern int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr);
19f843aa 1375extern int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local);
b411b363
PR
1376extern int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local);
1377extern int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local);
1378extern unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev,
1379 unsigned long al_enr);
1380extern size_t drbd_bm_words(struct drbd_conf *mdev);
1381extern unsigned long drbd_bm_bits(struct drbd_conf *mdev);
1382extern sector_t drbd_bm_capacity(struct drbd_conf *mdev);
4b0715f0
LE
1383
1384#define DRBD_END_OF_BITMAP (~(unsigned long)0)
b411b363
PR
1385extern unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1386/* bm_find_next variants for use while you hold drbd_bm_lock() */
1387extern unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo);
1388extern unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo);
0778286a 1389extern unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev);
b411b363
PR
1390extern unsigned long drbd_bm_total_weight(struct drbd_conf *mdev);
1391extern int drbd_bm_rs_done(struct drbd_conf *mdev);
1392/* for receive_bitmap */
1393extern void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset,
1394 size_t number, unsigned long *buffer);
19f843aa 1395/* for _drbd_send_bitmap */
b411b363
PR
1396extern void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset,
1397 size_t number, unsigned long *buffer);
1398
20ceb2b2 1399extern void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags);
b411b363 1400extern void drbd_bm_unlock(struct drbd_conf *mdev);
b411b363
PR
1401/* drbd_main.c */
1402
1403extern struct kmem_cache *drbd_request_cache;
6c852bec 1404extern struct kmem_cache *drbd_ee_cache; /* peer requests */
b411b363
PR
1405extern struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
1406extern struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
1407extern mempool_t *drbd_request_mempool;
1408extern mempool_t *drbd_ee_mempool;
1409
1410extern struct page *drbd_pp_pool; /* drbd's page pool */
1411extern spinlock_t drbd_pp_lock;
1412extern int drbd_pp_vacant;
1413extern wait_queue_head_t drbd_pp_wait;
1414
1415extern rwlock_t global_state_lock;
1416
1417extern struct drbd_conf *drbd_new_device(unsigned int minor);
1418extern void drbd_free_mdev(struct drbd_conf *mdev);
1419
2111438b
PR
1420struct drbd_tconn *drbd_new_tconn(char *name);
1421extern void drbd_free_tconn(struct drbd_tconn *tconn);
1422
b411b363
PR
1423extern int proc_details;
1424
1425/* drbd_req */
2f58dcfc 1426extern int drbd_make_request(struct request_queue *q, struct bio *bio);
b411b363
PR
1427extern int drbd_read_remote(struct drbd_conf *mdev, struct drbd_request *req);
1428extern int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec);
1429extern int is_valid_ar_handle(struct drbd_request *, sector_t);
1430
1431
1432/* drbd_nl.c */
1433extern void drbd_suspend_io(struct drbd_conf *mdev);
1434extern void drbd_resume_io(struct drbd_conf *mdev);
1435extern char *ppsize(char *buf, unsigned long long size);
a393db6f 1436extern sector_t drbd_new_dev_size(struct drbd_conf *, struct drbd_backing_dev *, int);
b411b363 1437enum determine_dev_size { dev_size_error = -1, unchanged = 0, shrunk = 1, grew = 2 };
24c4830c 1438extern enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *, enum dds_flags) __must_hold(local);
b411b363 1439extern void resync_after_online_grow(struct drbd_conf *);
99432fcc 1440extern void drbd_reconsider_max_bio_size(struct drbd_conf *mdev);
bf885f8a
AG
1441extern enum drbd_state_rv drbd_set_role(struct drbd_conf *mdev,
1442 enum drbd_role new_role,
1443 int force);
87f7be4c
PR
1444extern enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev);
1445extern void drbd_try_outdate_peer_async(struct drbd_conf *mdev);
b411b363
PR
1446extern int drbd_khelper(struct drbd_conf *mdev, char *cmd);
1447
1448/* drbd_worker.c */
1449extern int drbd_worker(struct drbd_thread *thi);
1450extern int drbd_alter_sa(struct drbd_conf *mdev, int na);
1451extern void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side);
1452extern void resume_next_sg(struct drbd_conf *mdev);
1453extern void suspend_other_sg(struct drbd_conf *mdev);
1454extern int drbd_resync_finished(struct drbd_conf *mdev);
1455/* maybe rather drbd_main.c ? */
1456extern int drbd_md_sync_page_io(struct drbd_conf *mdev,
1457 struct drbd_backing_dev *bdev, sector_t sector, int rw);
1458extern void drbd_ov_oos_found(struct drbd_conf*, sector_t, int);
9bd28d3c 1459extern void drbd_rs_controller_reset(struct drbd_conf *mdev);
b411b363
PR
1460
1461static inline void ov_oos_print(struct drbd_conf *mdev)
1462{
1463 if (mdev->ov_last_oos_size) {
1464 dev_err(DEV, "Out of sync: start=%llu, size=%lu (sectors)\n",
1465 (unsigned long long)mdev->ov_last_oos_start,
1466 (unsigned long)mdev->ov_last_oos_size);
1467 }
1468 mdev->ov_last_oos_size=0;
1469}
1470
1471
45bb912b 1472extern void drbd_csum_bio(struct drbd_conf *, struct crypto_hash *, struct bio *, void *);
f6ffca9f
AG
1473extern void drbd_csum_ee(struct drbd_conf *, struct crypto_hash *,
1474 struct drbd_peer_request *, void *);
b411b363
PR
1475/* worker callbacks */
1476extern int w_req_cancel_conflict(struct drbd_conf *, struct drbd_work *, int);
1477extern int w_read_retry_remote(struct drbd_conf *, struct drbd_work *, int);
1478extern int w_e_end_data_req(struct drbd_conf *, struct drbd_work *, int);
1479extern int w_e_end_rsdata_req(struct drbd_conf *, struct drbd_work *, int);
1480extern int w_e_end_csum_rs_req(struct drbd_conf *, struct drbd_work *, int);
1481extern int w_e_end_ov_reply(struct drbd_conf *, struct drbd_work *, int);
1482extern int w_e_end_ov_req(struct drbd_conf *, struct drbd_work *, int);
1483extern int w_ov_finished(struct drbd_conf *, struct drbd_work *, int);
794abb75 1484extern int w_resync_timer(struct drbd_conf *, struct drbd_work *, int);
b411b363 1485extern int w_resume_next_sg(struct drbd_conf *, struct drbd_work *, int);
b411b363 1486extern int w_send_write_hint(struct drbd_conf *, struct drbd_work *, int);
b411b363
PR
1487extern int w_send_dblock(struct drbd_conf *, struct drbd_work *, int);
1488extern int w_send_barrier(struct drbd_conf *, struct drbd_work *, int);
1489extern int w_send_read_req(struct drbd_conf *, struct drbd_work *, int);
1490extern int w_prev_work_done(struct drbd_conf *, struct drbd_work *, int);
1491extern int w_e_reissue(struct drbd_conf *, struct drbd_work *, int);
265be2d0 1492extern int w_restart_disk_io(struct drbd_conf *, struct drbd_work *, int);
73a01a18 1493extern int w_send_oos(struct drbd_conf *, struct drbd_work *, int);
c4752ef1 1494extern int w_start_resync(struct drbd_conf *, struct drbd_work *, int);
b411b363
PR
1495
1496extern void resync_timer_fn(unsigned long data);
370a43e7 1497extern void start_resync_timer_fn(unsigned long data);
b411b363
PR
1498
1499/* drbd_receiver.c */
e3555d85 1500extern int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector);
f6ffca9f
AG
1501extern int drbd_submit_ee(struct drbd_conf *, struct drbd_peer_request *,
1502 const unsigned, const int);
b411b363 1503extern int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list);
f6ffca9f
AG
1504extern struct drbd_peer_request *drbd_alloc_ee(struct drbd_conf *,
1505 u64, sector_t, unsigned int,
1506 gfp_t) __must_hold(local);
1507extern void drbd_free_some_ee(struct drbd_conf *, struct drbd_peer_request *,
1508 int);
435f0740
LE
1509#define drbd_free_ee(m,e) drbd_free_some_ee(m, e, 0)
1510#define drbd_free_net_ee(m,e) drbd_free_some_ee(m, e, 1)
b411b363
PR
1511extern void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1512 struct list_head *head);
1513extern void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
1514 struct list_head *head);
1515extern void drbd_set_recv_tcq(struct drbd_conf *mdev, int tcq_enabled);
1516extern void _drbd_clear_done_ee(struct drbd_conf *mdev, struct list_head *to_be_freed);
191d3cc8 1517extern void drbd_flush_workqueue(struct drbd_tconn *tconn);
b411b363
PR
1518
1519/* yes, there is kernel_setsockopt, but only since 2.6.18. we don't need to
1520 * mess with get_fs/set_fs, we know we are KERNEL_DS always. */
1521static inline int drbd_setsockopt(struct socket *sock, int level, int optname,
1522 char __user *optval, int optlen)
1523{
1524 int err;
1525 if (level == SOL_SOCKET)
1526 err = sock_setsockopt(sock, level, optname, optval, optlen);
1527 else
1528 err = sock->ops->setsockopt(sock, level, optname, optval,
1529 optlen);
1530 return err;
1531}
1532
1533static inline void drbd_tcp_cork(struct socket *sock)
1534{
1535 int __user val = 1;
1536 (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1537 (char __user *)&val, sizeof(val));
1538}
1539
1540static inline void drbd_tcp_uncork(struct socket *sock)
1541{
1542 int __user val = 0;
1543 (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
1544 (char __user *)&val, sizeof(val));
1545}
1546
1547static inline void drbd_tcp_nodelay(struct socket *sock)
1548{
1549 int __user val = 1;
1550 (void) drbd_setsockopt(sock, SOL_TCP, TCP_NODELAY,
1551 (char __user *)&val, sizeof(val));
1552}
1553
1554static inline void drbd_tcp_quickack(struct socket *sock)
1555{
344fa462 1556 int __user val = 2;
b411b363
PR
1557 (void) drbd_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
1558 (char __user *)&val, sizeof(val));
1559}
1560
1561void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo);
1562
1563/* drbd_proc.c */
1564extern struct proc_dir_entry *drbd_proc;
7d4e9d09 1565extern const struct file_operations drbd_proc_fops;
b411b363
PR
1566extern const char *drbd_conn_str(enum drbd_conns s);
1567extern const char *drbd_role_str(enum drbd_role s);
1568
1569/* drbd_actlog.c */
1570extern void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector);
1571extern void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector);
1572extern void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector);
1573extern int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1574extern int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
1575extern void drbd_rs_cancel_all(struct drbd_conf *mdev);
1576extern int drbd_rs_del_all(struct drbd_conf *mdev);
1577extern void drbd_rs_failed_io(struct drbd_conf *mdev,
1578 sector_t sector, int size);
1579extern int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *);
ea5442af 1580extern void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go);
b411b363
PR
1581extern void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector,
1582 int size, const char *file, const unsigned int line);
1583#define drbd_set_in_sync(mdev, sector, size) \
1584 __drbd_set_in_sync(mdev, sector, size, __FILE__, __LINE__)
73a01a18 1585extern int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector,
b411b363
PR
1586 int size, const char *file, const unsigned int line);
1587#define drbd_set_out_of_sync(mdev, sector, size) \
1588 __drbd_set_out_of_sync(mdev, sector, size, __FILE__, __LINE__)
1589extern void drbd_al_apply_to_bm(struct drbd_conf *mdev);
b411b363
PR
1590extern void drbd_al_shrink(struct drbd_conf *mdev);
1591
1592
1593/* drbd_nl.c */
1594
1595void drbd_nl_cleanup(void);
1596int __init drbd_nl_init(void);
1597void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state);
1598void drbd_bcast_sync_progress(struct drbd_conf *mdev);
f6ffca9f
AG
1599void drbd_bcast_ee(struct drbd_conf *, const char *, const int, const char *,
1600 const char *, const struct drbd_peer_request *);
b411b363
PR
1601
1602
1603/**
1604 * DOC: DRBD State macros
1605 *
1606 * These macros are used to express state changes in easily readable form.
1607 *
1608 * The NS macros expand to a mask and a value, that can be bit ored onto the
1609 * current state as soon as the spinlock (req_lock) was taken.
1610 *
1611 * The _NS macros are used for state functions that get called with the
1612 * spinlock. These macros expand directly to the new state value.
1613 *
1614 * Besides the basic forms NS() and _NS() additional _?NS[23] are defined
1615 * to express state changes that affect more than one aspect of the state.
1616 *
1617 * E.g. NS2(conn, C_CONNECTED, peer, R_SECONDARY)
1618 * Means that the network connection was established and that the peer
1619 * is in secondary role.
1620 */
1621#define role_MASK R_MASK
1622#define peer_MASK R_MASK
1623#define disk_MASK D_MASK
1624#define pdsk_MASK D_MASK
1625#define conn_MASK C_MASK
1626#define susp_MASK 1
1627#define user_isp_MASK 1
1628#define aftr_isp_MASK 1
fb22c402
PR
1629#define susp_nod_MASK 1
1630#define susp_fen_MASK 1
b411b363
PR
1631
1632#define NS(T, S) \
1633 ({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
1634 ({ union drbd_state val; val.i = 0; val.T = (S); val; })
1635#define NS2(T1, S1, T2, S2) \
1636 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1637 mask.T2 = T2##_MASK; mask; }), \
1638 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1639 val.T2 = (S2); val; })
1640#define NS3(T1, S1, T2, S2, T3, S3) \
1641 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1642 mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
1643 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1644 val.T2 = (S2); val.T3 = (S3); val; })
1645
1646#define _NS(D, T, S) \
1647 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T = (S); __ns; })
1648#define _NS2(D, T1, S1, T2, S2) \
1649 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1650 __ns.T2 = (S2); __ns; })
1651#define _NS3(D, T1, S1, T2, S2, T3, S3) \
1652 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1653 __ns.T2 = (S2); __ns.T3 = (S3); __ns; })
1654
1655/*
1656 * inline helper functions
1657 *************************/
1658
45bb912b
LE
1659/* see also page_chain_add and friends in drbd_receiver.c */
1660static inline struct page *page_chain_next(struct page *page)
1661{
1662 return (struct page *)page_private(page);
1663}
1664#define page_chain_for_each(page) \
1665 for (; page && ({ prefetch(page_chain_next(page)); 1; }); \
1666 page = page_chain_next(page))
1667#define page_chain_for_each_safe(page, n) \
1668 for (; page && ({ n = page_chain_next(page); 1; }); page = n)
1669
1670static inline int drbd_bio_has_active_page(struct bio *bio)
1671{
1672 struct bio_vec *bvec;
1673 int i;
1674
1675 __bio_for_each_segment(bvec, bio, i, 0) {
1676 if (page_count(bvec->bv_page) > 1)
1677 return 1;
1678 }
1679
1680 return 0;
1681}
1682
db830c46 1683static inline int drbd_ee_has_active_page(struct drbd_peer_request *peer_req)
45bb912b 1684{
db830c46 1685 struct page *page = peer_req->pages;
45bb912b
LE
1686 page_chain_for_each(page) {
1687 if (page_count(page) > 1)
1688 return 1;
1689 }
1690 return 0;
1691}
1692
1693
b8907339
PR
1694
1695
1696
1697
b411b363
PR
1698static inline void drbd_state_lock(struct drbd_conf *mdev)
1699{
1700 wait_event(mdev->misc_wait,
1701 !test_and_set_bit(CLUSTER_ST_CHANGE, &mdev->flags));
1702}
1703
1704static inline void drbd_state_unlock(struct drbd_conf *mdev)
1705{
1706 clear_bit(CLUSTER_ST_CHANGE, &mdev->flags);
1707 wake_up(&mdev->misc_wait);
1708}
1709
bf885f8a
AG
1710static inline enum drbd_state_rv
1711_drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
1712 enum chg_state_flags flags, struct completion *done)
b411b363 1713{
bf885f8a 1714 enum drbd_state_rv rv;
b411b363
PR
1715
1716 read_lock(&global_state_lock);
1717 rv = __drbd_set_state(mdev, ns, flags, done);
1718 read_unlock(&global_state_lock);
1719
1720 return rv;
1721}
1722
b411b363
PR
1723#define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
1724static inline void __drbd_chk_io_error_(struct drbd_conf *mdev, int forcedetach, const char *where)
1725{
1726 switch (mdev->ldev->dc.on_io_error) {
1727 case EP_PASS_ON:
1728 if (!forcedetach) {
7383506c 1729 if (__ratelimit(&drbd_ratelimit_state))
82f59cc6 1730 dev_err(DEV, "Local IO failed in %s.\n", where);
d2e17807
PR
1731 if (mdev->state.disk > D_INCONSISTENT)
1732 _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_HARD, NULL);
b411b363
PR
1733 break;
1734 }
1735 /* NOTE fall through to detach case if forcedetach set */
1736 case EP_DETACH:
1737 case EP_CALL_HELPER:
82f59cc6 1738 set_bit(WAS_IO_ERROR, &mdev->flags);
b411b363
PR
1739 if (mdev->state.disk > D_FAILED) {
1740 _drbd_set_state(_NS(mdev, disk, D_FAILED), CS_HARD, NULL);
82f59cc6
LE
1741 dev_err(DEV,
1742 "Local IO failed in %s. Detaching...\n", where);
b411b363
PR
1743 }
1744 break;
1745 }
1746}
1747
1748/**
1749 * drbd_chk_io_error: Handle the on_io_error setting, should be called from all io completion handlers
1750 * @mdev: DRBD device.
1751 * @error: Error code passed to the IO completion callback
1752 * @forcedetach: Force detach. I.e. the error happened while accessing the meta data
1753 *
1754 * See also drbd_main.c:after_state_ch() if (os.disk > D_FAILED && ns.disk == D_FAILED)
1755 */
1756#define drbd_chk_io_error(m,e,f) drbd_chk_io_error_(m,e,f, __func__)
1757static inline void drbd_chk_io_error_(struct drbd_conf *mdev,
1758 int error, int forcedetach, const char *where)
1759{
1760 if (error) {
1761 unsigned long flags;
87eeee41 1762 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
b411b363 1763 __drbd_chk_io_error_(mdev, forcedetach, where);
87eeee41 1764 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
b411b363
PR
1765 }
1766}
1767
1768
1769/**
1770 * drbd_md_first_sector() - Returns the first sector number of the meta data area
1771 * @bdev: Meta data block device.
1772 *
1773 * BTW, for internal meta data, this happens to be the maximum capacity
1774 * we could agree upon with our peer node.
1775 */
1776static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
1777{
1778 switch (bdev->dc.meta_dev_idx) {
1779 case DRBD_MD_INDEX_INTERNAL:
1780 case DRBD_MD_INDEX_FLEX_INT:
1781 return bdev->md.md_offset + bdev->md.bm_offset;
1782 case DRBD_MD_INDEX_FLEX_EXT:
1783 default:
1784 return bdev->md.md_offset;
1785 }
1786}
1787
1788/**
1789 * drbd_md_last_sector() - Return the last sector number of the meta data area
1790 * @bdev: Meta data block device.
1791 */
1792static inline sector_t drbd_md_last_sector(struct drbd_backing_dev *bdev)
1793{
1794 switch (bdev->dc.meta_dev_idx) {
1795 case DRBD_MD_INDEX_INTERNAL:
1796 case DRBD_MD_INDEX_FLEX_INT:
1797 return bdev->md.md_offset + MD_AL_OFFSET - 1;
1798 case DRBD_MD_INDEX_FLEX_EXT:
1799 default:
1800 return bdev->md.md_offset + bdev->md.md_size_sect;
1801 }
1802}
1803
1804/* Returns the number of 512 byte sectors of the device */
1805static inline sector_t drbd_get_capacity(struct block_device *bdev)
1806{
1807 /* return bdev ? get_capacity(bdev->bd_disk) : 0; */
77304d2a 1808 return bdev ? i_size_read(bdev->bd_inode) >> 9 : 0;
b411b363
PR
1809}
1810
1811/**
1812 * drbd_get_max_capacity() - Returns the capacity we announce to out peer
1813 * @bdev: Meta data block device.
1814 *
1815 * returns the capacity we announce to out peer. we clip ourselves at the
1816 * various MAX_SECTORS, because if we don't, current implementation will
1817 * oops sooner or later
1818 */
1819static inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
1820{
1821 sector_t s;
1822 switch (bdev->dc.meta_dev_idx) {
1823 case DRBD_MD_INDEX_INTERNAL:
1824 case DRBD_MD_INDEX_FLEX_INT:
1825 s = drbd_get_capacity(bdev->backing_bdev)
1826 ? min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1827 drbd_md_first_sector(bdev))
1828 : 0;
1829 break;
1830 case DRBD_MD_INDEX_FLEX_EXT:
1831 s = min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1832 drbd_get_capacity(bdev->backing_bdev));
1833 /* clip at maximum size the meta device can support */
1834 s = min_t(sector_t, s,
1835 BM_EXT_TO_SECT(bdev->md.md_size_sect
1836 - bdev->md.bm_offset));
1837 break;
1838 default:
1839 s = min_t(sector_t, DRBD_MAX_SECTORS,
1840 drbd_get_capacity(bdev->backing_bdev));
1841 }
1842 return s;
1843}
1844
1845/**
1846 * drbd_md_ss__() - Return the sector number of our meta data super block
1847 * @mdev: DRBD device.
1848 * @bdev: Meta data block device.
1849 */
1850static inline sector_t drbd_md_ss__(struct drbd_conf *mdev,
1851 struct drbd_backing_dev *bdev)
1852{
1853 switch (bdev->dc.meta_dev_idx) {
1854 default: /* external, some index */
1855 return MD_RESERVED_SECT * bdev->dc.meta_dev_idx;
1856 case DRBD_MD_INDEX_INTERNAL:
1857 /* with drbd08, internal meta data is always "flexible" */
1858 case DRBD_MD_INDEX_FLEX_INT:
1859 /* sizeof(struct md_on_disk_07) == 4k
1860 * position: last 4k aligned block of 4k size */
1861 if (!bdev->backing_bdev) {
1862 if (__ratelimit(&drbd_ratelimit_state)) {
1863 dev_err(DEV, "bdev->backing_bdev==NULL\n");
1864 dump_stack();
1865 }
1866 return 0;
1867 }
1868 return (drbd_get_capacity(bdev->backing_bdev) & ~7ULL)
1869 - MD_AL_OFFSET;
1870 case DRBD_MD_INDEX_FLEX_EXT:
1871 return 0;
1872 }
1873}
1874
b411b363
PR
1875static inline void
1876drbd_queue_work_front(struct drbd_work_queue *q, struct drbd_work *w)
1877{
1878 unsigned long flags;
1879 spin_lock_irqsave(&q->q_lock, flags);
1880 list_add(&w->list, &q->q);
1881 up(&q->s); /* within the spinlock,
1882 see comment near end of drbd_worker() */
1883 spin_unlock_irqrestore(&q->q_lock, flags);
1884}
1885
1886static inline void
1887drbd_queue_work(struct drbd_work_queue *q, struct drbd_work *w)
1888{
1889 unsigned long flags;
1890 spin_lock_irqsave(&q->q_lock, flags);
1891 list_add_tail(&w->list, &q->q);
1892 up(&q->s); /* within the spinlock,
1893 see comment near end of drbd_worker() */
1894 spin_unlock_irqrestore(&q->q_lock, flags);
1895}
1896
1897static inline void wake_asender(struct drbd_conf *mdev)
1898{
1899 if (test_bit(SIGNAL_ASENDER, &mdev->flags))
e6b3ea83 1900 force_sig(DRBD_SIG, mdev->tconn->asender.task);
b411b363
PR
1901}
1902
1903static inline void request_ping(struct drbd_conf *mdev)
1904{
1905 set_bit(SEND_PING, &mdev->flags);
1906 wake_asender(mdev);
1907}
1908
1909static inline int drbd_send_short_cmd(struct drbd_conf *mdev,
d8763023 1910 enum drbd_packet cmd)
b411b363 1911{
c012949a 1912 struct p_header h;
b411b363
PR
1913 return drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &h, sizeof(h));
1914}
1915
1916static inline int drbd_send_ping(struct drbd_conf *mdev)
1917{
c012949a 1918 struct p_header h;
b411b363
PR
1919 return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING, &h, sizeof(h));
1920}
1921
1922static inline int drbd_send_ping_ack(struct drbd_conf *mdev)
1923{
c012949a 1924 struct p_header h;
b411b363
PR
1925 return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING_ACK, &h, sizeof(h));
1926}
1927
1928static inline void drbd_thread_stop(struct drbd_thread *thi)
1929{
81e84650 1930 _drbd_thread_stop(thi, false, true);
b411b363
PR
1931}
1932
1933static inline void drbd_thread_stop_nowait(struct drbd_thread *thi)
1934{
81e84650 1935 _drbd_thread_stop(thi, false, false);
b411b363
PR
1936}
1937
1938static inline void drbd_thread_restart_nowait(struct drbd_thread *thi)
1939{
81e84650 1940 _drbd_thread_stop(thi, true, false);
b411b363
PR
1941}
1942
1943/* counts how many answer packets packets we expect from our peer,
1944 * for either explicit application requests,
1945 * or implicit barrier packets as necessary.
1946 * increased:
1947 * w_send_barrier
8554df1c 1948 * _req_mod(req, QUEUE_FOR_NET_WRITE or QUEUE_FOR_NET_READ);
b411b363
PR
1949 * it is much easier and equally valid to count what we queue for the
1950 * worker, even before it actually was queued or send.
1951 * (drbd_make_request_common; recovery path on read io-error)
1952 * decreased:
1953 * got_BarrierAck (respective tl_clear, tl_clear_barrier)
8554df1c 1954 * _req_mod(req, DATA_RECEIVED)
b411b363 1955 * [from receive_DataReply]
8554df1c 1956 * _req_mod(req, WRITE_ACKED_BY_PEER or RECV_ACKED_BY_PEER or NEG_ACKED)
b411b363
PR
1957 * [from got_BlockAck (P_WRITE_ACK, P_RECV_ACK)]
1958 * for some reason it is NOT decreased in got_NegAck,
1959 * but in the resulting cleanup code from report_params.
1960 * we should try to remember the reason for that...
8554df1c
AG
1961 * _req_mod(req, SEND_FAILED or SEND_CANCELED)
1962 * _req_mod(req, CONNECTION_LOST_WHILE_PENDING)
b411b363
PR
1963 * [from tl_clear_barrier]
1964 */
1965static inline void inc_ap_pending(struct drbd_conf *mdev)
1966{
1967 atomic_inc(&mdev->ap_pending_cnt);
1968}
1969
1970#define ERR_IF_CNT_IS_NEGATIVE(which) \
1971 if (atomic_read(&mdev->which) < 0) \
1972 dev_err(DEV, "in %s:%d: " #which " = %d < 0 !\n", \
1973 __func__ , __LINE__ , \
1974 atomic_read(&mdev->which))
1975
1976#define dec_ap_pending(mdev) do { \
1977 typecheck(struct drbd_conf *, mdev); \
1978 if (atomic_dec_and_test(&mdev->ap_pending_cnt)) \
1979 wake_up(&mdev->misc_wait); \
1980 ERR_IF_CNT_IS_NEGATIVE(ap_pending_cnt); } while (0)
1981
1982/* counts how many resync-related answers we still expect from the peer
1983 * increase decrease
1984 * C_SYNC_TARGET sends P_RS_DATA_REQUEST (and expects P_RS_DATA_REPLY)
25985edc 1985 * C_SYNC_SOURCE sends P_RS_DATA_REPLY (and expects P_WRITE_ACK with ID_SYNCER)
b411b363
PR
1986 * (or P_NEG_ACK with ID_SYNCER)
1987 */
1988static inline void inc_rs_pending(struct drbd_conf *mdev)
1989{
1990 atomic_inc(&mdev->rs_pending_cnt);
1991}
1992
1993#define dec_rs_pending(mdev) do { \
1994 typecheck(struct drbd_conf *, mdev); \
1995 atomic_dec(&mdev->rs_pending_cnt); \
1996 ERR_IF_CNT_IS_NEGATIVE(rs_pending_cnt); } while (0)
1997
1998/* counts how many answers we still need to send to the peer.
1999 * increased on
2000 * receive_Data unless protocol A;
2001 * we need to send a P_RECV_ACK (proto B)
2002 * or P_WRITE_ACK (proto C)
2003 * receive_RSDataReply (recv_resync_read) we need to send a P_WRITE_ACK
2004 * receive_DataRequest (receive_RSDataRequest) we need to send back P_DATA
2005 * receive_Barrier_* we need to send a P_BARRIER_ACK
2006 */
2007static inline void inc_unacked(struct drbd_conf *mdev)
2008{
2009 atomic_inc(&mdev->unacked_cnt);
2010}
2011
2012#define dec_unacked(mdev) do { \
2013 typecheck(struct drbd_conf *, mdev); \
2014 atomic_dec(&mdev->unacked_cnt); \
2015 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2016
2017#define sub_unacked(mdev, n) do { \
2018 typecheck(struct drbd_conf *, mdev); \
2019 atomic_sub(n, &mdev->unacked_cnt); \
2020 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2021
2022
b2fb6dbe 2023static inline void put_net_conf(struct drbd_tconn *tconn)
b411b363 2024{
b2fb6dbe
PR
2025 if (atomic_dec_and_test(&tconn->net_cnt))
2026 wake_up(&tconn->net_cnt_wait);
b411b363
PR
2027}
2028
2029/**
89e58e75 2030 * get_net_conf() - Increase ref count on mdev->tconn->net_conf; Returns 0 if nothing there
b411b363
PR
2031 * @mdev: DRBD device.
2032 *
89e58e75 2033 * You have to call put_net_conf() when finished working with mdev->tconn->net_conf.
b411b363 2034 */
b2fb6dbe 2035static inline int get_net_conf(struct drbd_tconn *tconn)
b411b363
PR
2036{
2037 int have_net_conf;
2038
b2fb6dbe
PR
2039 atomic_inc(&tconn->net_cnt);
2040 have_net_conf = tconn->volume0->state.conn >= C_UNCONNECTED;
b411b363 2041 if (!have_net_conf)
b2fb6dbe 2042 put_net_conf(tconn);
b411b363
PR
2043 return have_net_conf;
2044}
2045
2046/**
2047 * get_ldev() - Increase the ref count on mdev->ldev. Returns 0 if there is no ldev
2048 * @M: DRBD device.
2049 *
2050 * You have to call put_ldev() when finished working with mdev->ldev.
2051 */
2052#define get_ldev(M) __cond_lock(local, _get_ldev_if_state(M,D_INCONSISTENT))
2053#define get_ldev_if_state(M,MINS) __cond_lock(local, _get_ldev_if_state(M,MINS))
2054
2055static inline void put_ldev(struct drbd_conf *mdev)
2056{
1d7734a0 2057 int i = atomic_dec_return(&mdev->local_cnt);
9a0d9d03
LE
2058
2059 /* This may be called from some endio handler,
2060 * so we must not sleep here. */
2061
b411b363 2062 __release(local);
1d7734a0 2063 D_ASSERT(i >= 0);
e9e6f3ec 2064 if (i == 0) {
82f59cc6
LE
2065 if (mdev->state.disk == D_DISKLESS)
2066 /* even internal references gone, safe to destroy */
2067 drbd_ldev_destroy(mdev);
e9e6f3ec 2068 if (mdev->state.disk == D_FAILED)
82f59cc6 2069 /* all application IO references gone. */
e9e6f3ec 2070 drbd_go_diskless(mdev);
b411b363 2071 wake_up(&mdev->misc_wait);
e9e6f3ec 2072 }
b411b363
PR
2073}
2074
2075#ifndef __CHECKER__
2076static inline int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
2077{
2078 int io_allowed;
2079
82f59cc6
LE
2080 /* never get a reference while D_DISKLESS */
2081 if (mdev->state.disk == D_DISKLESS)
2082 return 0;
2083
b411b363
PR
2084 atomic_inc(&mdev->local_cnt);
2085 io_allowed = (mdev->state.disk >= mins);
2086 if (!io_allowed)
2087 put_ldev(mdev);
2088 return io_allowed;
2089}
2090#else
2091extern int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins);
2092#endif
2093
2094/* you must have an "get_ldev" reference */
2095static inline void drbd_get_syncer_progress(struct drbd_conf *mdev,
2096 unsigned long *bits_left, unsigned int *per_mil_done)
2097{
4b0715f0
LE
2098 /* this is to break it at compile time when we change that, in case we
2099 * want to support more than (1<<32) bits on a 32bit arch. */
b411b363
PR
2100 typecheck(unsigned long, mdev->rs_total);
2101
2102 /* note: both rs_total and rs_left are in bits, i.e. in
2103 * units of BM_BLOCK_SIZE.
2104 * for the percentage, we don't care. */
2105
439d5953
LE
2106 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2107 *bits_left = mdev->ov_left;
2108 else
2109 *bits_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
b411b363
PR
2110 /* >> 10 to prevent overflow,
2111 * +1 to prevent division by zero */
2112 if (*bits_left > mdev->rs_total) {
2113 /* doh. maybe a logic bug somewhere.
2114 * may also be just a race condition
2115 * between this and a disconnect during sync.
2116 * for now, just prevent in-kernel buffer overflow.
2117 */
2118 smp_rmb();
2119 dev_warn(DEV, "cs:%s rs_left=%lu > rs_total=%lu (rs_failed %lu)\n",
2120 drbd_conn_str(mdev->state.conn),
2121 *bits_left, mdev->rs_total, mdev->rs_failed);
2122 *per_mil_done = 0;
2123 } else {
4b0715f0
LE
2124 /* Make sure the division happens in long context.
2125 * We allow up to one petabyte storage right now,
2126 * at a granularity of 4k per bit that is 2**38 bits.
2127 * After shift right and multiplication by 1000,
2128 * this should still fit easily into a 32bit long,
2129 * so we don't need a 64bit division on 32bit arch.
2130 * Note: currently we don't support such large bitmaps on 32bit
2131 * arch anyways, but no harm done to be prepared for it here.
2132 */
2133 unsigned int shift = mdev->rs_total >= (1ULL << 32) ? 16 : 10;
2134 unsigned long left = *bits_left >> shift;
2135 unsigned long total = 1UL + (mdev->rs_total >> shift);
2136 unsigned long tmp = 1000UL - left * 1000UL/total;
b411b363
PR
2137 *per_mil_done = tmp;
2138 }
2139}
2140
2141
2142/* this throttles on-the-fly application requests
2143 * according to max_buffers settings;
2144 * maybe re-implement using semaphores? */
2145static inline int drbd_get_max_buffers(struct drbd_conf *mdev)
2146{
2147 int mxb = 1000000; /* arbitrary limit on open requests */
b2fb6dbe 2148 if (get_net_conf(mdev->tconn)) {
89e58e75 2149 mxb = mdev->tconn->net_conf->max_buffers;
b2fb6dbe 2150 put_net_conf(mdev->tconn);
b411b363
PR
2151 }
2152 return mxb;
2153}
2154
3719094e 2155static inline int drbd_state_is_stable(struct drbd_conf *mdev)
b411b363 2156{
3719094e 2157 union drbd_state s = mdev->state;
b411b363
PR
2158
2159 /* DO NOT add a default clause, we want the compiler to warn us
2160 * for any newly introduced state we may have forgotten to add here */
2161
2162 switch ((enum drbd_conns)s.conn) {
2163 /* new io only accepted when there is no connection, ... */
2164 case C_STANDALONE:
2165 case C_WF_CONNECTION:
2166 /* ... or there is a well established connection. */
2167 case C_CONNECTED:
2168 case C_SYNC_SOURCE:
2169 case C_SYNC_TARGET:
2170 case C_VERIFY_S:
2171 case C_VERIFY_T:
2172 case C_PAUSED_SYNC_S:
2173 case C_PAUSED_SYNC_T:
67531718
PR
2174 case C_AHEAD:
2175 case C_BEHIND:
3719094e 2176 /* transitional states, IO allowed */
b411b363
PR
2177 case C_DISCONNECTING:
2178 case C_UNCONNECTED:
2179 case C_TIMEOUT:
2180 case C_BROKEN_PIPE:
2181 case C_NETWORK_FAILURE:
2182 case C_PROTOCOL_ERROR:
2183 case C_TEAR_DOWN:
2184 case C_WF_REPORT_PARAMS:
2185 case C_STARTING_SYNC_S:
2186 case C_STARTING_SYNC_T:
3719094e
PR
2187 break;
2188
2189 /* Allow IO in BM exchange states with new protocols */
b411b363 2190 case C_WF_BITMAP_S:
31890f4a 2191 if (mdev->tconn->agreed_pro_version < 96)
3719094e
PR
2192 return 0;
2193 break;
2194
2195 /* no new io accepted in these states */
b411b363
PR
2196 case C_WF_BITMAP_T:
2197 case C_WF_SYNC_UUID:
2198 case C_MASK:
2199 /* not "stable" */
2200 return 0;
2201 }
2202
2203 switch ((enum drbd_disk_state)s.disk) {
2204 case D_DISKLESS:
2205 case D_INCONSISTENT:
2206 case D_OUTDATED:
2207 case D_CONSISTENT:
2208 case D_UP_TO_DATE:
2209 /* disk state is stable as well. */
2210 break;
2211
2212 /* no new io accepted during tansitional states */
2213 case D_ATTACHING:
2214 case D_FAILED:
2215 case D_NEGOTIATING:
2216 case D_UNKNOWN:
2217 case D_MASK:
2218 /* not "stable" */
2219 return 0;
2220 }
2221
2222 return 1;
2223}
2224
fb22c402
PR
2225static inline int is_susp(union drbd_state s)
2226{
2227 return s.susp || s.susp_nod || s.susp_fen;
2228}
2229
1b881ef7 2230static inline bool may_inc_ap_bio(struct drbd_conf *mdev)
b411b363
PR
2231{
2232 int mxb = drbd_get_max_buffers(mdev);
2233
fb22c402 2234 if (is_susp(mdev->state))
1b881ef7 2235 return false;
b411b363 2236 if (test_bit(SUSPEND_IO, &mdev->flags))
1b881ef7 2237 return false;
b411b363
PR
2238
2239 /* to avoid potential deadlock or bitmap corruption,
2240 * in various places, we only allow new application io
2241 * to start during "stable" states. */
2242
2243 /* no new io accepted when attaching or detaching the disk */
3719094e 2244 if (!drbd_state_is_stable(mdev))
1b881ef7 2245 return false;
b411b363
PR
2246
2247 /* since some older kernels don't have atomic_add_unless,
2248 * and we are within the spinlock anyways, we have this workaround. */
2249 if (atomic_read(&mdev->ap_bio_cnt) > mxb)
1b881ef7 2250 return false;
b411b363 2251 if (test_bit(BITMAP_IO, &mdev->flags))
1b881ef7
AG
2252 return false;
2253 return true;
b411b363
PR
2254}
2255
1b881ef7 2256static inline bool inc_ap_bio_cond(struct drbd_conf *mdev, int count)
b411b363 2257{
1b881ef7 2258 bool rv = false;
8869d683 2259
87eeee41 2260 spin_lock_irq(&mdev->tconn->req_lock);
1b881ef7 2261 rv = may_inc_ap_bio(mdev);
8869d683
PR
2262 if (rv)
2263 atomic_add(count, &mdev->ap_bio_cnt);
87eeee41 2264 spin_unlock_irq(&mdev->tconn->req_lock);
8869d683
PR
2265
2266 return rv;
2267}
b411b363 2268
8869d683
PR
2269static inline void inc_ap_bio(struct drbd_conf *mdev, int count)
2270{
b411b363
PR
2271 /* we wait here
2272 * as long as the device is suspended
2273 * until the bitmap is no longer on the fly during connection
2274 * handshake as long as we would exeed the max_buffer limit.
2275 *
2276 * to avoid races with the reconnect code,
2277 * we need to atomic_inc within the spinlock. */
2278
1b881ef7 2279 wait_event(mdev->misc_wait, inc_ap_bio_cond(mdev, count));
b411b363
PR
2280}
2281
2282static inline void dec_ap_bio(struct drbd_conf *mdev)
2283{
2284 int mxb = drbd_get_max_buffers(mdev);
2285 int ap_bio = atomic_dec_return(&mdev->ap_bio_cnt);
2286
2287 D_ASSERT(ap_bio >= 0);
2288 /* this currently does wake_up for every dec_ap_bio!
2289 * maybe rather introduce some type of hysteresis?
2290 * e.g. (ap_bio == mxb/2 || ap_bio == 0) ? */
2291 if (ap_bio < mxb)
2292 wake_up(&mdev->misc_wait);
2293 if (ap_bio == 0 && test_bit(BITMAP_IO, &mdev->flags)) {
2294 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
e42325a5 2295 drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
b411b363
PR
2296 }
2297}
2298
62b0da3a 2299static inline int drbd_set_ed_uuid(struct drbd_conf *mdev, u64 val)
b411b363 2300{
62b0da3a 2301 int changed = mdev->ed_uuid != val;
b411b363 2302 mdev->ed_uuid = val;
62b0da3a 2303 return changed;
b411b363
PR
2304}
2305
b411b363
PR
2306static inline int drbd_queue_order_type(struct drbd_conf *mdev)
2307{
2308 /* sorry, we currently have no working implementation
2309 * of distributed TCQ stuff */
2310#ifndef QUEUE_ORDERED_NONE
2311#define QUEUE_ORDERED_NONE 0
2312#endif
2313 return QUEUE_ORDERED_NONE;
2314}
2315
b411b363
PR
2316static inline void drbd_md_flush(struct drbd_conf *mdev)
2317{
2318 int r;
2319
a8a4e51e 2320 if (test_bit(MD_NO_FUA, &mdev->flags))
b411b363
PR
2321 return;
2322
dd3932ed 2323 r = blkdev_issue_flush(mdev->ldev->md_bdev, GFP_KERNEL, NULL);
b411b363 2324 if (r) {
a8a4e51e 2325 set_bit(MD_NO_FUA, &mdev->flags);
b411b363
PR
2326 dev_err(DEV, "meta data flush failed with status %d, disabling md-flushes\n", r);
2327 }
2328}
2329
2330#endif
This page took 0.225694 seconds and 5 git commands to generate.