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