Merge tag 'net-6.12-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-2.6-block.git] / drivers / block / drbd / drbd_int.h
CommitLineData
93c68cc4 1/* SPDX-License-Identifier: GPL-2.0-only */
b411b363
PR
2/*
3 drbd_int.h
4
5 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
6
7 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
8 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
9 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
10
b411b363
PR
11
12*/
13
14#ifndef _DRBD_INT_H
15#define _DRBD_INT_H
16
9534d671 17#include <crypto/hash.h>
b411b363
PR
18#include <linux/compiler.h>
19#include <linux/types.h>
b411b363 20#include <linux/list.h>
3f07c014 21#include <linux/sched/signal.h>
b411b363
PR
22#include <linux/bitops.h>
23#include <linux/slab.h>
132cc538 24#include <linux/ratelimit.h>
b411b363
PR
25#include <linux/tcp.h>
26#include <linux/mutex.h>
27#include <linux/major.h>
28#include <linux/blkdev.h>
66114cad 29#include <linux/backing-dev.h>
062e879c 30#include <linux/idr.h>
9d5059c9 31#include <linux/dynamic_debug.h>
b411b363
PR
32#include <net/tcp.h>
33#include <linux/lru_cache.h>
70c71606 34#include <linux/prefetch.h>
3b98c0c2 35#include <linux/drbd_genl_api.h>
b8907339 36#include <linux/drbd.h>
20f2a34a 37#include <linux/drbd_config.h>
d9f65229 38#include "drbd_strings.h"
b8907339 39#include "drbd_state.h"
a3603a6e 40#include "drbd_protocol.h"
136160c1 41#include "drbd_polymorph_printk.h"
b411b363 42
183ece30 43/* shared module parameters, defined in drbd_main.c */
b411b363 44#ifdef CONFIG_DRBD_FAULT_INJECTION
183ece30
RK
45extern int drbd_enable_faults;
46extern int drbd_fault_rate;
b411b363
PR
47#endif
48
183ece30 49extern unsigned int drbd_minor_count;
8ab761e1 50extern char drbd_usermode_helper[];
183ece30 51extern int drbd_proc_details;
b411b363
PR
52
53
b411b363
PR
54/* This is used to stop/restart our threads.
55 * Cannot use SIGTERM nor SIGKILL, since these
56 * are sent out by init on runlevel changes
57 * I choose SIGHUP for now.
58 */
59#define DRBD_SIGKILL SIGHUP
60
b411b363
PR
61#define ID_IN_SYNC (4711ULL)
62#define ID_OUT_OF_SYNC (4712ULL)
b411b363 63#define ID_SYNCER (-1ULL)
579b57ed 64
4a23f264 65#define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
b411b363 66
54761697 67struct drbd_device;
bde89a9e 68struct drbd_connection;
8164dd6c 69struct drbd_peer_device;
b411b363 70
b411b363
PR
71/* Defines to control fault insertion */
72enum {
73 DRBD_FAULT_MD_WR = 0, /* meta data write */
74 DRBD_FAULT_MD_RD = 1, /* read */
75 DRBD_FAULT_RS_WR = 2, /* resync */
76 DRBD_FAULT_RS_RD = 3,
77 DRBD_FAULT_DT_WR = 4, /* data */
78 DRBD_FAULT_DT_RD = 5,
79 DRBD_FAULT_DT_RA = 6, /* data read ahead */
80 DRBD_FAULT_BM_ALLOC = 7, /* bitmap allocation */
81 DRBD_FAULT_AL_EE = 8, /* alloc ee */
6b4388ac 82 DRBD_FAULT_RECEIVE = 9, /* Changes some bytes upon receiving a [rs]data block */
b411b363
PR
83
84 DRBD_FAULT_MAX,
85};
86
b411b363 87extern unsigned int
b30ab791 88_drbd_insert_fault(struct drbd_device *device, unsigned int type);
0cf9d27e 89
b411b363 90static inline int
b30ab791 91drbd_insert_fault(struct drbd_device *device, unsigned int type) {
0cf9d27e 92#ifdef CONFIG_DRBD_FAULT_INJECTION
183ece30
RK
93 return drbd_fault_rate &&
94 (drbd_enable_faults & (1<<type)) &&
b30ab791 95 _drbd_insert_fault(device, type);
b411b363 96#else
0cf9d27e 97 return 0;
b411b363 98#endif
0cf9d27e 99}
b411b363
PR
100
101/* integer division, round _UP_ to the next integer */
102#define div_ceil(A, B) ((A)/(B) + ((A)%(B) ? 1 : 0))
103/* usual integer division */
104#define div_floor(A, B) ((A)/(B))
105
b411b363 106extern struct ratelimit_state drbd_ratelimit_state;
05a10ec7 107extern struct idr drbd_devices; /* RCU, updates: genl_lock() */
77c556f6 108extern struct list_head drbd_resources; /* RCU, updates: genl_lock() */
b411b363 109
d8763023 110extern const char *cmdname(enum drbd_packet cmd);
b411b363
PR
111
112/* for sending/receiving the bitmap,
113 * possibly in some encoding scheme */
114struct bm_xfer_ctx {
115 /* "const"
116 * stores total bits and long words
117 * of the bitmap, so we don't need to
118 * call the accessor functions over and again. */
119 unsigned long bm_bits;
120 unsigned long bm_words;
121 /* during xfer, current position within the bitmap */
122 unsigned long bit_offset;
123 unsigned long word_offset;
124
125 /* statistics; index: (h->command == P_BITMAP) */
126 unsigned packets[2];
127 unsigned bytes[2];
128};
129
5e54c2a6
AG
130extern void INFO_bm_xfer_stats(struct drbd_peer_device *peer_device,
131 const char *direction, struct bm_xfer_ctx *c);
b411b363
PR
132
133static inline void bm_xfer_ctx_bit_to_word_offset(struct bm_xfer_ctx *c)
134{
135 /* word_offset counts "native long words" (32 or 64 bit),
136 * aligned at 64 bit.
137 * Encoded packet may end at an unaligned bit offset.
138 * In case a fallback clear text packet is transmitted in
139 * between, we adjust this offset back to the last 64bit
140 * aligned "native long word", which makes coding and decoding
141 * the plain text bitmap much more convenient. */
142#if BITS_PER_LONG == 64
143 c->word_offset = c->bit_offset >> 6;
144#elif BITS_PER_LONG == 32
145 c->word_offset = c->bit_offset >> 5;
146 c->word_offset &= ~(1UL);
147#else
148# error "unsupported BITS_PER_LONG"
149#endif
150}
151
bde89a9e 152extern unsigned int drbd_header_size(struct drbd_connection *connection);
b411b363 153
b411b363
PR
154/**********************************************************************/
155enum drbd_thread_state {
e77a0a5c
AG
156 NONE,
157 RUNNING,
158 EXITING,
159 RESTARTING
b411b363
PR
160};
161
162struct drbd_thread {
163 spinlock_t t_lock;
164 struct task_struct *task;
165 struct completion stop;
166 enum drbd_thread_state t_state;
167 int (*function) (struct drbd_thread *);
2457b6d5 168 struct drbd_resource *resource;
bde89a9e 169 struct drbd_connection *connection;
b411b363 170 int reset_cpu_mask;
c60b0251 171 const char *name;
b411b363
PR
172};
173
174static inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
175{
176 /* THINK testing the t_state seems to be uncritical in all cases
177 * (but thread_{start,stop}), so we can read it *without* the lock.
178 * --lge */
179
180 smp_rmb();
181 return thi->t_state;
182}
183
b411b363
PR
184struct drbd_work {
185 struct list_head list;
309a8348 186 int (*cb)(struct drbd_work *, int cancel);
84b8c06b
AG
187};
188
189struct drbd_device_work {
190 struct drbd_work w;
191 struct drbd_device *device;
b411b363
PR
192};
193
ace652ac
AG
194#include "drbd_interval.h"
195
54761697 196extern int drbd_wait_misc(struct drbd_device *, struct drbd_interval *);
7be8da07 197
28bc3b8c
AG
198extern void lock_all_resources(void);
199extern void unlock_all_resources(void);
200
b411b363
PR
201struct drbd_request {
202 struct drbd_work w;
84b8c06b 203 struct drbd_device *device;
b411b363
PR
204
205 /* if local IO is not allowed, will be NULL.
206 * if local IO _is_ allowed, holds the locally submitted bio clone,
207 * or, after local IO completion, the ERR_PTR(error).
fcefa62e 208 * see drbd_request_endio(). */
b411b363
PR
209 struct bio *private_bio;
210
ace652ac 211 struct drbd_interval i;
b411b363 212
b6dd1a89 213 /* epoch: used to check on "completion" whether this req was in
b411b363 214 * the current epoch, and we therefore have to close it,
b6dd1a89
LE
215 * causing a p_barrier packet to be send, starting a new epoch.
216 *
217 * This corresponds to "barrier" in struct p_barrier[_ack],
218 * and to "barrier_nr" in struct drbd_epoch (and various
219 * comments/function parameters/local variable names).
b411b363 220 */
b6dd1a89 221 unsigned int epoch;
b411b363 222
b411b363
PR
223 struct list_head tl_requests; /* ring list in the transfer log */
224 struct bio *master_bio; /* master bio pointer */
e5f891b2 225
844a6ae7
LE
226 /* see struct drbd_device */
227 struct list_head req_pending_master_completion;
228 struct list_head req_pending_local;
229
e5f891b2
LE
230 /* for generic IO accounting */
231 unsigned long start_jif;
232
233 /* for DRBD internal statistics */
234
235 /* Minimal set of time stamps to determine if we wait for activity log
236 * transactions, local disk or peer. 32 bit "jiffies" are good enough,
237 * we don't expect a DRBD request to be stalled for several month.
238 */
239
240 /* before actual request processing */
241 unsigned long in_actlog_jif;
242
243 /* local disk */
244 unsigned long pre_submit_jif;
245
246 /* per connection */
247 unsigned long pre_send_jif;
248 unsigned long acked_jif;
249 unsigned long net_done_jif;
250
251 /* Possibly even more detail to track each phase:
252 * master_completion_jif
253 * how long did it take to complete the master bio
254 * (application visible latency)
255 * allocated_jif
256 * how long the master bio was blocked until we finally allocated
257 * a tracking struct
258 * in_actlog_jif
259 * how long did we wait for activity log transactions
260 *
261 * net_queued_jif
262 * when did we finally queue it for sending
263 * pre_send_jif
264 * when did we start sending it
265 * post_send_jif
266 * how long did we block in the network stack trying to send it
267 * acked_jif
268 * when did we receive (or fake, in protocol A) a remote ACK
269 * net_done_jif
270 * when did we receive final acknowledgement (P_BARRIER_ACK),
271 * or decide, e.g. on connection loss, that we do no longer expect
272 * anything from this peer for this request.
273 *
274 * pre_submit_jif
275 * post_sub_jif
276 * when did we start submiting to the lower level device,
277 * and how long did we block in that submit function
278 * local_completion_jif
279 * how long did it take the lower level device to complete this request
280 */
281
b411b363 282
b406777e
LE
283 /* once it hits 0, we may complete the master_bio */
284 atomic_t completion_ref;
285 /* once it hits 0, we may destroy this drbd_request object */
286 struct kref kref;
b411b363 287
a0d856df 288 unsigned rq_state; /* see comments above _req_mod() */
b411b363 289};
b411b363
PR
290
291struct drbd_epoch {
bde89a9e 292 struct drbd_connection *connection;
b411b363
PR
293 struct list_head list;
294 unsigned int barrier_nr;
295 atomic_t epoch_size; /* increased on every request added. */
296 atomic_t active; /* increased on every req. added, and dec on every finished. */
297 unsigned long flags;
298};
299
300/* drbd_epoch flag bits */
301enum {
b411b363 302 DE_HAVE_BARRIER_NUMBER,
b411b363
PR
303};
304
305enum epoch_event {
306 EV_PUT,
307 EV_GOT_BARRIER_NR,
b411b363 308 EV_BECAME_LAST,
b411b363
PR
309 EV_CLEANUP = 32, /* used as flag */
310};
311
b411b363
PR
312struct digest_info {
313 int digest_size;
314 void *digest;
315};
316
f6ffca9f 317struct drbd_peer_request {
a8cd15ba
AG
318 struct drbd_work w;
319 struct drbd_peer_device *peer_device;
85719573 320 struct drbd_epoch *epoch; /* for writes */
45bb912b 321 struct page *pages;
ce668b6d 322 blk_opf_t opf;
45bb912b 323 atomic_t pending_bios;
010f6e67 324 struct drbd_interval i;
45bb912b
LE
325 /* see comments on ee flag bits below */
326 unsigned long flags;
21ae5d7f 327 unsigned long submit_jif;
85719573
PR
328 union {
329 u64 block_id;
330 struct digest_info *digest;
331 };
45bb912b
LE
332};
333
ce668b6d
CB
334/* Equivalent to bio_op and req_op. */
335#define peer_req_op(peer_req) \
336 ((peer_req)->opf & REQ_OP_MASK)
337
45bb912b
LE
338/* ee flag bits.
339 * While corresponding bios are in flight, the only modification will be
340 * set_bit WAS_ERROR, which has to be atomic.
341 * If no bios are in flight yet, or all have been completed,
342 * non-atomic modification to ee->flags is ok.
343 */
b411b363
PR
344enum {
345 __EE_CALL_AL_COMPLETE_IO,
b411b363 346 __EE_MAY_SET_IN_SYNC,
45bb912b 347
9305455a 348 /* is this a TRIM aka REQ_OP_DISCARD? */
f31e583a
LE
349 __EE_TRIM,
350 /* explicit zero-out requested, or
351 * our lower level cannot handle trim,
352 * and we want to fall back to zeroout instead */
353 __EE_ZEROOUT,
a0fb3c47 354
45bb912b
LE
355 /* In case a barrier failed,
356 * we need to resubmit without the barrier flag. */
357 __EE_RESUBMITTED,
358
6c852bec 359 /* we may have several bios per peer request.
45bb912b
LE
360 * if any of those fail, we set this flag atomically
361 * from the endio callback */
362 __EE_WAS_ERROR,
c36c3ced
LE
363
364 /* This ee has a pointer to a digest instead of a block id */
365 __EE_HAS_DIGEST,
7be8da07
AG
366
367 /* Conflicting local requests need to be restarted after this request */
368 __EE_RESTART_REQUESTS,
303d1448
PR
369
370 /* The peer wants a write ACK for this (wire proto C) */
371 __EE_SEND_WRITE_ACK,
302bdeae
PR
372
373 /* Is set when net_conf had two_primaries set while creating this peer_req */
374 __EE_IN_INTERVAL_TREE,
21ae5d7f
LE
375
376 /* for debugfs: */
377 /* has this been submitted, or does it still wait for something else? */
378 __EE_SUBMITTED,
379
380 /* this is/was a write request */
381 __EE_WRITE,
382
9104d31a
LE
383 /* this is/was a write same request */
384 __EE_WRITE_SAME,
385
21ae5d7f
LE
386 /* this originates from application on peer
387 * (not some resync or verify or other DRBD internal request) */
388 __EE_APPLICATION,
700ca8c0
PR
389
390 /* If it contains only 0 bytes, send back P_RS_DEALLOCATED */
391 __EE_RS_THIN_REQ,
b411b363
PR
392};
393#define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
b411b363 394#define EE_MAY_SET_IN_SYNC (1<<__EE_MAY_SET_IN_SYNC)
f31e583a
LE
395#define EE_TRIM (1<<__EE_TRIM)
396#define EE_ZEROOUT (1<<__EE_ZEROOUT)
a0fb3c47 397#define EE_RESUBMITTED (1<<__EE_RESUBMITTED)
45bb912b 398#define EE_WAS_ERROR (1<<__EE_WAS_ERROR)
c36c3ced 399#define EE_HAS_DIGEST (1<<__EE_HAS_DIGEST)
7be8da07 400#define EE_RESTART_REQUESTS (1<<__EE_RESTART_REQUESTS)
303d1448 401#define EE_SEND_WRITE_ACK (1<<__EE_SEND_WRITE_ACK)
302bdeae 402#define EE_IN_INTERVAL_TREE (1<<__EE_IN_INTERVAL_TREE)
21ae5d7f
LE
403#define EE_SUBMITTED (1<<__EE_SUBMITTED)
404#define EE_WRITE (1<<__EE_WRITE)
9104d31a 405#define EE_WRITE_SAME (1<<__EE_WRITE_SAME)
21ae5d7f 406#define EE_APPLICATION (1<<__EE_APPLICATION)
700ca8c0 407#define EE_RS_THIN_REQ (1<<__EE_RS_THIN_REQ)
b411b363 408
b30ab791 409/* flag bits per device */
b411b363 410enum {
b411b363
PR
411 UNPLUG_REMOTE, /* sending a "UnplugRemote" could help */
412 MD_DIRTY, /* current uuids and flags not yet on disk */
b411b363 413 USE_DEGR_WFC_T, /* degr-wfc-timeout instead of wfc-timeout. */
b411b363
PR
414 CL_ST_CHG_SUCCESS,
415 CL_ST_CHG_FAIL,
416 CRASHED_PRIMARY, /* This node was a crashed primary.
417 * Gets cleared when the state.conn
418 * goes into C_CONNECTED state. */
b411b363
PR
419 CONSIDER_RESYNC,
420
a8a4e51e 421 MD_NO_FUA, /* Users wants us to not use FUA/FLUSH on meta data dev */
5ab7d2c0 422
b411b363
PR
423 BITMAP_IO, /* suspend application io;
424 once no more io in flight, start bitmap io */
425 BITMAP_IO_QUEUED, /* Started bitmap IO */
a2a3c74f
LE
426 WAS_IO_ERROR, /* Local disk failed, returned IO error */
427 WAS_READ_ERROR, /* Local disk READ failed (set additionally to the above) */
383606e0 428 FORCE_DETACH, /* Force-detach from local disk, aborting any pending local IO */
b411b363 429 RESYNC_AFTER_NEG, /* Resync after online grow after the attach&negotiate finished. */
b411b363
PR
430 RESIZE_PENDING, /* Size change detected locally, waiting for the response from
431 * the peer, if it changed there as well. */
43a5182c 432 NEW_CUR_UUID, /* Create new current UUID when thawing IO */
0778286a 433 AL_SUSPENDED, /* Activity logging is currently suspended. */
370a43e7 434 AHEAD_TO_SYNC_SOURCE, /* Ahead -> SyncSource queued */
e64a3294 435 B_RS_H_DONE, /* Before resync handler done (already executed) */
08b165ba 436 DISCARD_MY_DATA, /* discard_my_data flag per volume */
380207d0 437 READ_BALANCE_RR,
e334f550 438
f418815f
LE
439 FLUSH_PENDING, /* if set, device->flush_jif is when we submitted that flush
440 * from drbd_flush_after_epoch() */
441
e334f550
LE
442 /* cleared only after backing device related structures have been destroyed. */
443 GOING_DISKLESS, /* Disk is being detached, because of io-error, or admin request. */
444
445 /* to be used in drbd_device_post_work() */
446 GO_DISKLESS, /* tell worker to schedule cleanup before detach */
447 DESTROY_DISK, /* tell worker to close backing devices and destroy related structures. */
ac0acb9e
LE
448 MD_SYNC, /* tell worker to call drbd_md_sync() */
449 RS_START, /* tell worker to start resync/OV */
e334f550
LE
450 RS_PROGRESS, /* tell worker that resync made significant progress */
451 RS_DONE, /* tell worker that resync is done */
b411b363
PR
452};
453
54761697 454struct drbd_bitmap; /* opaque for drbd_device */
b411b363 455
20ceb2b2
LE
456/* definition of bits in bm_flags to be used in drbd_bm_lock
457 * and drbd_bitmap_io and friends. */
458enum bm_flag {
20ceb2b2 459 /* currently locked for bulk operation */
0e8488ad 460 BM_LOCKED_MASK = 0xf,
20ceb2b2
LE
461
462 /* in detail, that is: */
463 BM_DONT_CLEAR = 0x1,
464 BM_DONT_SET = 0x2,
465 BM_DONT_TEST = 0x4,
466
0e8488ad
LE
467 /* so we can mark it locked for bulk operation,
468 * and still allow all non-bulk operations */
469 BM_IS_LOCKED = 0x8,
470
20ceb2b2 471 /* (test bit, count bit) allowed (common case) */
0e8488ad 472 BM_LOCKED_TEST_ALLOWED = BM_DONT_CLEAR | BM_DONT_SET | BM_IS_LOCKED,
20ceb2b2
LE
473
474 /* testing bits, as well as setting new bits allowed, but clearing bits
475 * would be unexpected. Used during bitmap receive. Setting new bits
476 * requires sending of "out-of-sync" information, though. */
0e8488ad 477 BM_LOCKED_SET_ALLOWED = BM_DONT_CLEAR | BM_IS_LOCKED,
20ceb2b2 478
0e8488ad
LE
479 /* for drbd_bm_write_copy_pages, everything is allowed,
480 * only concurrent bulk operations are locked out. */
481 BM_LOCKED_CHANGE_ALLOWED = BM_IS_LOCKED,
20ceb2b2
LE
482};
483
b411b363
PR
484struct drbd_work_queue {
485 struct list_head q;
b411b363 486 spinlock_t q_lock; /* to protect the list. */
8c0785a5 487 wait_queue_head_t q_wait;
b411b363
PR
488};
489
490struct drbd_socket {
b411b363
PR
491 struct mutex mutex;
492 struct socket *socket;
493 /* this way we get our
494 * send/receive buffers off the stack */
5a87d920 495 void *sbuf;
e6ef8a5c 496 void *rbuf;
b411b363
PR
497};
498
499struct drbd_md {
500 u64 md_offset; /* sector offset to 'super' block */
501
502 u64 la_size_sect; /* last agreed size, unit sectors */
9f2247bb 503 spinlock_t uuid_lock;
b411b363
PR
504 u64 uuid[UI_SIZE];
505 u64 device_uuid;
506 u32 flags;
507 u32 md_size_sect;
508
ae8bf312 509 s32 al_offset; /* signed relative sector offset to activity log */
b411b363 510 s32 bm_offset; /* signed relative sector offset to bitmap */
3a4d4eb3
LE
511
512 /* cached value of bdev->disk_conf->meta_dev_idx (see below) */
513 s32 meta_dev_idx;
514
515 /* see al_tr_number_to_on_disk_sector() */
516 u32 al_stripes;
517 u32 al_stripe_size_4k;
518 u32 al_size_4k; /* cached product of the above */
b411b363
PR
519};
520
b411b363
PR
521struct drbd_backing_dev {
522 struct block_device *backing_bdev;
20e6a8d0 523 struct file *backing_bdev_file;
b411b363 524 struct block_device *md_bdev;
20e6a8d0 525 struct file *f_md_bdev;
b411b363 526 struct drbd_md md;
0500813f 527 struct disk_conf *disk_conf; /* RCU, for updates: resource->conf_update */
b411b363
PR
528 sector_t known_size; /* last known size of that backing device */
529};
530
531struct drbd_md_io {
e37d2438
LE
532 struct page *page;
533 unsigned long start_jif; /* last call to drbd_md_get_buffer */
534 unsigned long submit_jif; /* last _drbd_md_sync_page_io() submit */
535 const char *current_use;
536 atomic_t in_use;
0c464425 537 unsigned int done;
b411b363
PR
538 int error;
539};
540
541struct bm_io_work {
542 struct drbd_work w;
8164dd6c 543 struct drbd_peer_device *peer_device;
b411b363 544 char *why;
20ceb2b2 545 enum bm_flag flags;
8164dd6c 546 int (*io_fn)(struct drbd_device *device, struct drbd_peer_device *peer_device);
b30ab791 547 void (*done)(struct drbd_device *device, int rv);
b411b363
PR
548};
549
778f271d 550struct fifo_buffer {
778f271d
PR
551 unsigned int head_index;
552 unsigned int size;
9958c857 553 int total; /* sum of all values */
150849c5 554 int values[] __counted_by(size);
778f271d 555};
6a365874 556extern struct fifo_buffer *fifo_alloc(unsigned int fifo_size);
778f271d 557
bde89a9e 558/* flag bits per connection */
01a311a5
PR
559enum {
560 NET_CONGESTED, /* The data socket is congested */
427c0434 561 RESOLVE_CONFLICTS, /* Set on one node, cleared on the peer! */
668700b4 562 SEND_PING,
2a67d8b9 563 GOT_PING_ACK, /* set when we receive a ping_ack packet, ping_wait gets woken */
4d0fc3fd 564 CONN_WD_ST_CHG_REQ, /* A cluster wide state change on the connection is active */
fc3b10a4
PR
565 CONN_WD_ST_CHG_OKAY,
566 CONN_WD_ST_CHG_FAIL,
8169e41b 567 CONN_DRY_RUN, /* Expect disconnect after resync handshake. */
6936fcb4 568 CREATE_BARRIER, /* next P_DATA is preceded by a P_BARRIER */
a1096a6e 569 STATE_SENT, /* Do not change state/UUIDs while this is set */
6f3465ed
LE
570 CALLBACK_PENDING, /* Whether we have a call_usermodehelper(, UMH_WAIT_PROC)
571 * pending, from drbd worker context.
6f3465ed 572 */
b66623e3 573 DISCONNECT_SENT,
e334f550
LE
574
575 DEVICE_WORK_PENDING, /* tell worker that some device has pending work */
01a311a5
PR
576};
577
a2972846
AG
578enum which_state { NOW, OLD = NOW, NEW };
579
77c556f6
AG
580struct drbd_resource {
581 char *name;
4d3d5aa8
LE
582#ifdef CONFIG_DEBUG_FS
583 struct dentry *debugfs_res;
584 struct dentry *debugfs_res_volumes;
585 struct dentry *debugfs_res_connections;
586 struct dentry *debugfs_res_in_flight_summary;
587#endif
77c556f6 588 struct kref kref;
803ea134 589 struct idr devices; /* volume number to device mapping */
77c556f6
AG
590 struct list_head connections;
591 struct list_head resources;
eb6bea67 592 struct res_opts res_opts;
0500813f 593 struct mutex conf_update; /* mutex for ready-copy-update of net_conf and disk_conf */
9e276872 594 struct mutex adm_mutex; /* mutex to serialize administrative requests */
0500813f 595 spinlock_t req_lock;
6bbf53ca
AG
596
597 unsigned susp:1; /* IO suspended by user */
598 unsigned susp_nod:1; /* IO suspended because no data */
599 unsigned susp_fen:1; /* IO suspended because fence peer handler runs */
625a6ba2 600
e9526580
PR
601 enum write_ordering_e write_ordering;
602
625a6ba2 603 cpumask_var_t cpu_mask;
77c556f6
AG
604};
605
944410e9
LE
606struct drbd_thread_timing_details
607{
608 unsigned long start_jif;
609 void *cb_addr;
610 const char *caller_fn;
611 unsigned int line;
612 unsigned int cb_nr;
613};
614
77c556f6
AG
615struct drbd_connection {
616 struct list_head connections;
617 struct drbd_resource *resource;
4d3d5aa8
LE
618#ifdef CONFIG_DEBUG_FS
619 struct dentry *debugfs_conn;
620 struct dentry *debugfs_conn_callback_history;
621 struct dentry *debugfs_conn_oldest_requests;
622#endif
9dc9fbb3 623 struct kref kref;
c06ece6b 624 struct idr peer_devices; /* volume number to peer device mapping */
8410da8f
PR
625 enum drbd_conns cstate; /* Only C_STANDALONE to C_WF_REPORT_PARAMS */
626 struct mutex cstate_mutex; /* Protects graceful disconnects */
28e448bb 627 unsigned int connect_cnt; /* Inc each time a connection is established */
2111438b 628
062e879c 629 unsigned long flags;
44ed167d 630 struct net_conf *net_conf; /* content protected by rcu */
91fd4dad 631 wait_queue_head_t ping_wait; /* Woken upon reception of a ping, and a state change */
e42325a5 632
089c075d
AG
633 struct sockaddr_storage my_addr;
634 int my_addr_len;
635 struct sockaddr_storage peer_addr;
636 int peer_addr_len;
637
e42325a5
PR
638 struct drbd_socket data; /* data/barrier/cstate/parameter packets */
639 struct drbd_socket meta; /* ping/ack (metadata) packets */
31890f4a 640 int agreed_pro_version; /* actually used protocol version */
20c68fde 641 u32 agreed_features;
31890f4a
PR
642 unsigned long last_received; /* in jiffies, either socket */
643 unsigned int ko_count;
e6b3ea83 644
b6dd1a89 645 struct list_head transfer_log; /* all requests not yet fully processed */
87eeee41 646
9534d671 647 struct crypto_shash *cram_hmac_tfm;
3d0e6375
KC
648 struct crypto_shash *integrity_tfm; /* checksums we compute, updates protected by connection->data->mutex */
649 struct crypto_shash *peer_integrity_tfm; /* checksums we verify, only accessed from receiver thread */
650 struct crypto_shash *csums_tfm;
651 struct crypto_shash *verify_tfm;
a0638456
PR
652 void *int_dig_in;
653 void *int_dig_vv;
654
b6dd1a89 655 /* receiver side */
12038a3a
PR
656 struct drbd_epoch *current_epoch;
657 spinlock_t epoch_lock;
658 unsigned int epochs;
b379c41e 659 atomic_t current_tle_nr; /* transfer log epoch number */
b6dd1a89 660 unsigned current_tle_writes; /* writes seen within this tl epoch */
4b0007c0 661
07be15b1 662 unsigned long last_reconnect_jif;
c51a0ef3
LE
663 /* empty member on older kernels without blk_start_plug() */
664 struct blk_plug receiver_plug;
e6b3ea83
PR
665 struct drbd_thread receiver;
666 struct drbd_thread worker;
1c03e520 667 struct drbd_thread ack_receiver;
668700b4 668 struct workqueue_struct *ack_sender;
b6dd1a89 669
7753a4c1
LE
670 /* cached pointers,
671 * so we can look up the oldest pending requests more quickly.
672 * protected by resource->req_lock */
673 struct drbd_request *req_next; /* DRBD 9: todo.req_next */
674 struct drbd_request *req_ack_pending;
675 struct drbd_request *req_not_net_done;
676
b6dd1a89 677 /* sender side */
d5b27b01 678 struct drbd_work_queue sender_work;
b6dd1a89 679
944410e9
LE
680#define DRBD_THREAD_DETAILS_HIST 16
681 unsigned int w_cb_nr; /* keeps counting up */
682 unsigned int r_cb_nr; /* keeps counting up */
683 struct drbd_thread_timing_details w_timing_details[DRBD_THREAD_DETAILS_HIST];
684 struct drbd_thread_timing_details r_timing_details[DRBD_THREAD_DETAILS_HIST];
685
b6dd1a89 686 struct {
84d34f2f
LE
687 unsigned long last_sent_barrier_jif;
688
b6dd1a89
LE
689 /* whether this sender thread
690 * has processed a single write yet. */
691 bool seen_any_write_yet;
692
693 /* Which barrier number to send with the next P_BARRIER */
694 int current_epoch_nr;
695
696 /* how many write requests have been sent
697 * with req->epoch == current_epoch_nr.
698 * If none, no P_BARRIER will be sent. */
699 unsigned current_epoch_writes;
700 } send;
778f271d
PR
701};
702
a2972846
AG
703static inline bool has_net_conf(struct drbd_connection *connection)
704{
705 bool has_net_conf;
706
707 rcu_read_lock();
708 has_net_conf = rcu_dereference(connection->net_conf);
709 rcu_read_unlock();
710
711 return has_net_conf;
712}
713
944410e9
LE
714void __update_timing_details(
715 struct drbd_thread_timing_details *tdp,
716 unsigned int *cb_nr,
717 void *cb,
718 const char *fn, const unsigned int line);
719
720#define update_worker_timing_details(c, cb) \
721 __update_timing_details(c->w_timing_details, &c->w_cb_nr, cb, __func__ , __LINE__ )
722#define update_receiver_timing_details(c, cb) \
723 __update_timing_details(c->r_timing_details, &c->r_cb_nr, cb, __func__ , __LINE__ )
724
113fef9e
LE
725struct submit_worker {
726 struct workqueue_struct *wq;
727 struct work_struct worker;
728
844a6ae7 729 /* protected by ..->resource->req_lock */
113fef9e
LE
730 struct list_head writes;
731};
732
a6b32bc3
AG
733struct drbd_peer_device {
734 struct list_head peer_devices;
735 struct drbd_device *device;
bde89a9e 736 struct drbd_connection *connection;
668700b4 737 struct work_struct send_acks_work;
4d3d5aa8
LE
738#ifdef CONFIG_DEBUG_FS
739 struct dentry *debugfs_peer_dev;
740#endif
a6b32bc3
AG
741};
742
743struct drbd_device {
d8628a86 744 struct drbd_resource *resource;
a6b32bc3 745 struct list_head peer_devices;
4ce49266 746 struct list_head pending_bitmap_io;
4d3d5aa8
LE
747
748 unsigned long flush_jif;
749#ifdef CONFIG_DEBUG_FS
750 struct dentry *debugfs_minor;
751 struct dentry *debugfs_vol;
752 struct dentry *debugfs_vol_oldest_requests;
753 struct dentry *debugfs_vol_act_log_extents;
754 struct dentry *debugfs_vol_resync_extents;
755 struct dentry *debugfs_vol_data_gen_id;
f5ec0173 756 struct dentry *debugfs_vol_ed_gen_id;
4d3d5aa8
LE
757#endif
758
759 unsigned int vnr; /* volume number within the connection */
760 unsigned int minor; /* device minor number */
761
81fa2e67 762 struct kref kref;
2111438b 763
b411b363
PR
764 /* things that are stored as / read from meta data on disk */
765 unsigned long flags;
766
767 /* configured by drbdsetup */
9cf766a4 768 struct drbd_backing_dev *ldev;
b411b363
PR
769
770 sector_t p_size; /* partner's disk size */
771 struct request_queue *rq_queue;
b411b363
PR
772 struct gendisk *vdisk;
773
07be15b1 774 unsigned long last_reattach_jif;
84b8c06b
AG
775 struct drbd_work resync_work;
776 struct drbd_work unplug_work;
b411b363
PR
777 struct timer_list resync_timer;
778 struct timer_list md_sync_timer;
370a43e7 779 struct timer_list start_resync_timer;
7fde2be9 780 struct timer_list request_timer;
b411b363
PR
781
782 /* Used after attach while negotiating new disk state. */
783 union drbd_state new_state_tmp;
784
da9fbc27 785 union drbd_dev_state state;
b411b363
PR
786 wait_queue_head_t misc_wait;
787 wait_queue_head_t state_wait; /* upon each state change. */
788 unsigned int send_cnt;
789 unsigned int recv_cnt;
790 unsigned int read_cnt;
791 unsigned int writ_cnt;
792 unsigned int al_writ_cnt;
793 unsigned int bm_writ_cnt;
794 atomic_t ap_bio_cnt; /* Requests we need to complete */
ad3fee79 795 atomic_t ap_actlog_cnt; /* Requests waiting for activity log */
b411b363
PR
796 atomic_t ap_pending_cnt; /* AP data packets on the wire, ack expected */
797 atomic_t rs_pending_cnt; /* RS request/data packets on the wire */
d942ae44 798 atomic_t unacked_cnt; /* Need to send replies for */
b411b363 799 atomic_t local_cnt; /* Waiting for local completion */
7dbb4386 800 atomic_t suspend_cnt;
b2fb6dbe 801
dac1389c
AG
802 /* Interval tree of pending local requests */
803 struct rb_root read_requests;
de696716 804 struct rb_root write_requests;
b411b363 805
844a6ae7
LE
806 /* for statistics and timeouts */
807 /* [0] read, [1] write */
808 struct list_head pending_master_completion[2];
809 struct list_head pending_completion[2];
810
aaaba345
LE
811 /* use checksums for *this* resync */
812 bool use_csums;
4b0715f0 813 /* blocks to resync in this run [unit BM_BLOCK_SIZE] */
b411b363 814 unsigned long rs_total;
4b0715f0 815 /* number of resync blocks that failed in this run */
b411b363
PR
816 unsigned long rs_failed;
817 /* Syncer's start time [unit jiffies] */
818 unsigned long rs_start;
819 /* cumulated time in PausedSyncX state [unit jiffies] */
820 unsigned long rs_paused;
1d7734a0
LE
821 /* skipped because csum was equal [unit BM_BLOCK_SIZE] */
822 unsigned long rs_same_csum;
823#define DRBD_SYNC_MARKS 8
824#define DRBD_SYNC_MARK_STEP (3*HZ)
b411b363 825 /* block not up-to-date at mark [unit BM_BLOCK_SIZE] */
1d7734a0 826 unsigned long rs_mark_left[DRBD_SYNC_MARKS];
b411b363 827 /* marks's time [unit jiffies] */
1d7734a0
LE
828 unsigned long rs_mark_time[DRBD_SYNC_MARKS];
829 /* current index into rs_mark_{left,time} */
830 int rs_last_mark;
328e0f12 831 unsigned long rs_last_bcast; /* [unit jiffies] */
b411b363
PR
832
833 /* where does the admin want us to start? (sector) */
834 sector_t ov_start_sector;
02b91b55 835 sector_t ov_stop_sector;
b411b363
PR
836 /* where are we now? (sector) */
837 sector_t ov_position;
838 /* Start sector of out of sync range (to merge printk reporting). */
839 sector_t ov_last_oos_start;
840 /* size of out-of-sync range in sectors. */
841 sector_t ov_last_oos_size;
842 unsigned long ov_left; /* in bits */
b411b363 843
b411b363
PR
844 struct drbd_bitmap *bitmap;
845 unsigned long bm_resync_fo; /* bit offset for drbd_bm_find_next */
846
847 /* Used to track operations of resync... */
848 struct lru_cache *resync;
849 /* Number of locked elements in resync LRU */
850 unsigned int resync_locked;
851 /* resync extent number waiting for application requests */
852 unsigned int resync_wenr;
853
854 int open_cnt;
855 u64 *p_uuid;
4b0007c0 856
85719573
PR
857 struct list_head active_ee; /* IO in progress (P_DATA gets written to disk) */
858 struct list_head sync_ee; /* IO in progress (P_RS_DATA_REPLY gets written to disk) */
18b75d75
AG
859 struct list_head done_ee; /* need to send P_WRITE_ACK */
860 struct list_head read_ee; /* [RS]P_DATA_REQUEST being read */
b411b363 861 struct list_head net_ee; /* zero-copy network send in progress */
b411b363 862
b411b363 863 struct list_head resync_reads;
435f0740
LE
864 atomic_t pp_in_use; /* allocated from page pool */
865 atomic_t pp_in_use_by_net; /* sendpage()d, still referenced by tcp */
b411b363 866 wait_queue_head_t ee_wait;
cc94c650 867 struct drbd_md_io md_io;
b411b363
PR
868 spinlock_t al_lock;
869 wait_queue_head_t al_wait;
870 struct lru_cache *act_log; /* activity log */
871 unsigned int al_tr_number;
872 int al_tr_cycle;
b411b363
PR
873 wait_queue_head_t seq_wait;
874 atomic_t packet_seq;
875 unsigned int peer_seq;
876 spinlock_t peer_seq_lock;
b411b363 877 unsigned long comm_bm_set; /* communicated number of set bits. */
b411b363
PR
878 struct bm_io_work bm_io_work;
879 u64 ed_uuid; /* UUID of the exposed data */
8410da8f 880 struct mutex own_state_mutex;
a6b32bc3 881 struct mutex *state_mutex; /* either own_state_mutex or first_peer_device(device)->connection->cstate_mutex */
b411b363 882 char congestion_reason; /* Why we where congested... */
1d7734a0
LE
883 atomic_t rs_sect_in; /* for incoming resync data rate, SyncTarget */
884 atomic_t rs_sect_ev; /* for submitted resync data rate, both */
885 int rs_last_sect_ev; /* counter to compare with */
886 int rs_last_events; /* counter of read or write "events" (unit sectors)
887 * on the lower level device when we last looked. */
888 int c_sync_rate; /* current resync rate after syncer throttle magic */
bde89a9e 889 struct fifo_buffer *rs_plan_s; /* correction values of resync planer (RCU, connection->conn_update) */
778f271d 890 int rs_in_flight; /* resync sectors in flight (to proxy, in proxy and from proxy) */
759fbdfb 891 atomic_t ap_in_flight; /* App sectors in flight (waiting for ack) */
db141b2f
LE
892 unsigned int peer_max_bio_size;
893 unsigned int local_max_bio_size;
113fef9e
LE
894
895 /* any requests that would block in drbd_make_request()
896 * are deferred to this single-threaded work queue */
897 struct submit_worker submit;
b411b363
PR
898};
899
4ce49266
LE
900struct drbd_bm_aio_ctx {
901 struct drbd_device *device;
902 struct list_head list; /* on device->pending_bitmap_io */;
903 unsigned long start_jif;
904 atomic_t in_flight;
905 unsigned int done;
906 unsigned flags;
907#define BM_AIO_COPY_PAGES 1
908#define BM_AIO_WRITE_HINTED 2
909#define BM_AIO_WRITE_ALL_PAGES 4
910#define BM_AIO_READ 8
911 int error;
912 struct kref kref;
913};
914
a910b123
LE
915struct drbd_config_context {
916 /* assigned from drbd_genlmsghdr */
917 unsigned int minor;
918 /* assigned from request attributes, if present */
919 unsigned int volume;
920#define VOLUME_UNSPECIFIED (-1U)
921 /* pointer into the request skb,
922 * limited lifetime! */
923 char *resource_name;
924 struct nlattr *my_addr;
925 struct nlattr *peer_addr;
926
927 /* reply buffer */
928 struct sk_buff *reply_skb;
929 /* pointer into reply buffer */
930 struct drbd_genlmsghdr *reply_dh;
931 /* resolved from attributes, if possible */
932 struct drbd_device *device;
933 struct drbd_resource *resource;
934 struct drbd_connection *connection;
935};
936
b30ab791 937static inline struct drbd_device *minor_to_device(unsigned int minor)
b411b363 938{
05a10ec7 939 return (struct drbd_device *)idr_find(&drbd_devices, minor);
b411b363
PR
940}
941
a6b32bc3
AG
942static inline struct drbd_peer_device *first_peer_device(struct drbd_device *device)
943{
ec4a3407 944 return list_first_entry_or_null(&device->peer_devices, struct drbd_peer_device, peer_devices);
a6b32bc3
AG
945}
946
a2972846
AG
947static inline struct drbd_peer_device *
948conn_peer_device(struct drbd_connection *connection, int volume_number)
949{
950 return idr_find(&connection->peer_devices, volume_number);
951}
952
77c556f6
AG
953#define for_each_resource(resource, _resources) \
954 list_for_each_entry(resource, _resources, resources)
955
956#define for_each_resource_rcu(resource, _resources) \
957 list_for_each_entry_rcu(resource, _resources, resources)
958
959#define for_each_resource_safe(resource, tmp, _resources) \
960 list_for_each_entry_safe(resource, tmp, _resources, resources)
961
962#define for_each_connection(connection, resource) \
963 list_for_each_entry(connection, &resource->connections, connections)
964
965#define for_each_connection_rcu(connection, resource) \
966 list_for_each_entry_rcu(connection, &resource->connections, connections)
967
968#define for_each_connection_safe(connection, tmp, resource) \
969 list_for_each_entry_safe(connection, tmp, &resource->connections, connections)
970
a6b32bc3
AG
971#define for_each_peer_device(peer_device, device) \
972 list_for_each_entry(peer_device, &device->peer_devices, peer_devices)
973
974#define for_each_peer_device_rcu(peer_device, device) \
975 list_for_each_entry_rcu(peer_device, &device->peer_devices, peer_devices)
976
977#define for_each_peer_device_safe(peer_device, tmp, device) \
978 list_for_each_entry_safe(peer_device, tmp, &device->peer_devices, peer_devices)
979
b30ab791 980static inline unsigned int device_to_minor(struct drbd_device *device)
b411b363 981{
b30ab791 982 return device->minor;
b411b363
PR
983}
984
b411b363
PR
985/*
986 * function declarations
987 *************************/
988
989/* drbd_main.c */
990
e89b591c
PR
991enum dds_flags {
992 DDSF_FORCED = 1,
993 DDSF_NO_RESYNC = 2, /* Do not run a resync for the new space */
994};
995
b30ab791 996extern void drbd_init_set_defaults(struct drbd_device *device);
b411b363
PR
997extern int drbd_thread_start(struct drbd_thread *thi);
998extern void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait);
999#ifdef CONFIG_SMP
80822284 1000extern void drbd_thread_current_set_cpu(struct drbd_thread *thi);
b411b363
PR
1001#else
1002#define drbd_thread_current_set_cpu(A) ({})
b411b363 1003#endif
bde89a9e 1004extern void tl_release(struct drbd_connection *, unsigned int barrier_nr,
b411b363 1005 unsigned int set_size);
bde89a9e
AG
1006extern void tl_clear(struct drbd_connection *);
1007extern void drbd_free_sock(struct drbd_connection *connection);
1008extern int drbd_send(struct drbd_connection *connection, struct socket *sock,
bedbd2a5 1009 void *buf, size_t size, unsigned msg_flags);
bde89a9e 1010extern int drbd_send_all(struct drbd_connection *, struct socket *, void *, size_t,
fb708e40
AG
1011 unsigned);
1012
bde89a9e
AG
1013extern int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cmd);
1014extern int drbd_send_protocol(struct drbd_connection *connection);
69a22773
AG
1015extern int drbd_send_uuids(struct drbd_peer_device *);
1016extern int drbd_send_uuids_skip_initial_sync(struct drbd_peer_device *);
1017extern void drbd_gen_and_send_sync_uuid(struct drbd_peer_device *);
1018extern int drbd_send_sizes(struct drbd_peer_device *, int trigger_reply, enum dds_flags flags);
1019extern int drbd_send_state(struct drbd_peer_device *, union drbd_state s);
1020extern int drbd_send_current_state(struct drbd_peer_device *);
1021extern int drbd_send_sync_param(struct drbd_peer_device *);
bde89a9e 1022extern void drbd_send_b_ack(struct drbd_connection *connection, u32 barrier_nr,
d4e67d7c 1023 u32 set_size);
69a22773 1024extern int drbd_send_ack(struct drbd_peer_device *, enum drbd_packet,
f6ffca9f 1025 struct drbd_peer_request *);
69a22773 1026extern void drbd_send_ack_rp(struct drbd_peer_device *, enum drbd_packet,
a9a9994d 1027 struct p_block_req *rp);
69a22773 1028extern void drbd_send_ack_dp(struct drbd_peer_device *, enum drbd_packet,
a9a9994d 1029 struct p_data *dp, int data_size);
69a22773 1030extern int drbd_send_ack_ex(struct drbd_peer_device *, enum drbd_packet,
b411b363 1031 sector_t sector, int blksize, u64 block_id);
69a22773
AG
1032extern int drbd_send_out_of_sync(struct drbd_peer_device *, struct drbd_request *);
1033extern int drbd_send_block(struct drbd_peer_device *, enum drbd_packet,
f6ffca9f 1034 struct drbd_peer_request *);
69a22773
AG
1035extern int drbd_send_dblock(struct drbd_peer_device *, struct drbd_request *req);
1036extern int drbd_send_drequest(struct drbd_peer_device *, int cmd,
b411b363 1037 sector_t sector, int size, u64 block_id);
69a22773 1038extern int drbd_send_drequest_csum(struct drbd_peer_device *, sector_t sector,
d8763023
AG
1039 int size, void *digest, int digest_size,
1040 enum drbd_packet cmd);
69a22773 1041extern int drbd_send_ov_request(struct drbd_peer_device *, sector_t sector, int size);
b411b363 1042
8164dd6c 1043extern int drbd_send_bitmap(struct drbd_device *device, struct drbd_peer_device *peer_device);
69a22773 1044extern void drbd_send_sr_reply(struct drbd_peer_device *, enum drbd_state_rv retcode);
bde89a9e 1045extern void conn_send_sr_reply(struct drbd_connection *connection, enum drbd_state_rv retcode);
700ca8c0 1046extern int drbd_send_rs_deallocated(struct drbd_peer_device *, struct drbd_peer_request *);
63a7c8ad 1047extern void drbd_backing_dev_free(struct drbd_device *device, struct drbd_backing_dev *ldev);
b30ab791 1048extern void drbd_device_cleanup(struct drbd_device *device);
c51a0ef3
LE
1049extern void drbd_print_uuids(struct drbd_device *device, const char *text);
1050extern void drbd_queue_unplug(struct drbd_device *device);
b411b363 1051
bde89a9e 1052extern void conn_md_sync(struct drbd_connection *connection);
b30ab791
AG
1053extern void drbd_md_write(struct drbd_device *device, void *buffer);
1054extern void drbd_md_sync(struct drbd_device *device);
1055extern int drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev);
1056extern void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local);
1057extern void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local);
1058extern void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local);
1059extern void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local);
1060extern void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local);
1061extern void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local);
1062extern void drbd_md_set_flag(struct drbd_device *device, int flags) __must_hold(local);
1063extern void drbd_md_clear_flag(struct drbd_device *device, int flags)__must_hold(local);
b411b363 1064extern int drbd_md_test_flag(struct drbd_backing_dev *, int);
b30ab791 1065extern void drbd_md_mark_dirty(struct drbd_device *device);
b30ab791 1066extern void drbd_queue_bitmap_io(struct drbd_device *device,
8164dd6c 1067 int (*io_fn)(struct drbd_device *, struct drbd_peer_device *),
54761697 1068 void (*done)(struct drbd_device *, int),
8164dd6c
AG
1069 char *why, enum bm_flag flags,
1070 struct drbd_peer_device *peer_device);
b30ab791 1071extern int drbd_bitmap_io(struct drbd_device *device,
8164dd6c
AG
1072 int (*io_fn)(struct drbd_device *, struct drbd_peer_device *),
1073 char *why, enum bm_flag flags,
1074 struct drbd_peer_device *peer_device);
b30ab791 1075extern int drbd_bitmap_io_from_worker(struct drbd_device *device,
8164dd6c
AG
1076 int (*io_fn)(struct drbd_device *, struct drbd_peer_device *),
1077 char *why, enum bm_flag flags,
1078 struct drbd_peer_device *peer_device);
1079extern int drbd_bmio_set_n_write(struct drbd_device *device,
1080 struct drbd_peer_device *peer_device) __must_hold(local);
1081extern int drbd_bmio_clear_n_write(struct drbd_device *device,
1082 struct drbd_peer_device *peer_device) __must_hold(local);
b411b363 1083
b411b363 1084/* Meta data layout
ae8bf312
LE
1085 *
1086 * We currently have two possible layouts.
1087 * Offsets in (512 byte) sectors.
1088 * external:
1089 * |----------- md_size_sect ------------------|
1090 * [ 4k superblock ][ activity log ][ Bitmap ]
1091 * | al_offset == 8 |
1092 * | bm_offset = al_offset + X |
1093 * ==> bitmap sectors = md_size_sect - bm_offset
1094 *
1095 * Variants:
1096 * old, indexed fixed size meta data:
1097 *
1098 * internal:
1099 * |----------- md_size_sect ------------------|
1100 * [data.....][ Bitmap ][ activity log ][ 4k superblock ][padding*]
1101 * | al_offset < 0 |
1102 * | bm_offset = al_offset - Y |
1103 * ==> bitmap sectors = Y = al_offset - bm_offset
1104 *
1105 * [padding*] are zero or up to 7 unused 512 Byte sectors to the
1106 * end of the device, so that the [4k superblock] will be 4k aligned.
1107 *
1108 * The activity log consists of 4k transaction blocks,
1109 * which are written in a ring-buffer, or striped ring-buffer like fashion,
1110 * which are writtensize used to be fixed 32kB,
1111 * but is about to become configurable.
1112 */
b411b363 1113
ae8bf312
LE
1114/* Our old fixed size meta data layout
1115 * allows up to about 3.8TB, so if you want more,
7ad651b5 1116 * you need to use the "flexible" meta data format. */
ae8bf312
LE
1117#define MD_128MB_SECT (128LLU << 11) /* 128 MB, unit sectors */
1118#define MD_4kB_SECT 8
1119#define MD_32kB_SECT 64
7ad651b5
LE
1120
1121/* One activity log extent represents 4M of storage */
1122#define AL_EXTENT_SHIFT 22
b411b363
PR
1123#define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
1124
7ad651b5
LE
1125/* We could make these currently hardcoded constants configurable
1126 * variables at create-md time (or even re-configurable at runtime?).
1127 * Which will require some more changes to the DRBD "super block"
1128 * and attach code.
1129 *
1130 * updates per transaction:
1131 * This many changes to the active set can be logged with one transaction.
1132 * This number is arbitrary.
1133 * context per transaction:
1134 * This many context extent numbers are logged with each transaction.
1135 * This number is resulting from the transaction block size (4k), the layout
1136 * of the transaction header, and the number of updates per transaction.
1137 * See drbd_actlog.c:struct al_transaction_on_disk
1138 * */
1139#define AL_UPDATES_PER_TRANSACTION 64 // arbitrary
1140#define AL_CONTEXT_PER_TRANSACTION 919 // (4096 - 36 - 6*64)/4
1141
b411b363
PR
1142#if BITS_PER_LONG == 32
1143#define LN2_BPL 5
1144#define cpu_to_lel(A) cpu_to_le32(A)
1145#define lel_to_cpu(A) le32_to_cpu(A)
1146#elif BITS_PER_LONG == 64
1147#define LN2_BPL 6
1148#define cpu_to_lel(A) cpu_to_le64(A)
1149#define lel_to_cpu(A) le64_to_cpu(A)
1150#else
1151#error "LN2 of BITS_PER_LONG unknown!"
1152#endif
1153
1154/* resync bitmap */
1155/* 16MB sized 'bitmap extent' to track syncer usage */
1156struct bm_extent {
1157 int rs_left; /* number of bits set (out of sync) in this extent. */
1158 int rs_failed; /* number of failed resync requests in this extent. */
1159 unsigned long flags;
1160 struct lc_element lce;
1161};
1162
1163#define BME_NO_WRITES 0 /* bm_extent.flags: no more requests on this one! */
1164#define BME_LOCKED 1 /* bm_extent.flags: syncer active on this one. */
e3555d85 1165#define BME_PRIORITY 2 /* finish resync IO on this extent ASAP! App IO waiting! */
b411b363
PR
1166
1167/* drbd_bitmap.c */
1168/*
1169 * We need to store one bit for a block.
1170 * Example: 1GB disk @ 4096 byte blocks ==> we need 32 KB bitmap.
1171 * Bit 0 ==> local node thinks this block is binary identical on both nodes
1172 * Bit 1 ==> local node thinks this block needs to be synced.
1173 */
1174
8e26f9cc
PR
1175#define SLEEP_TIME (HZ/10)
1176
45dfffeb
LE
1177/* We do bitmap IO in units of 4k blocks.
1178 * We also still have a hardcoded 4k per bit relation. */
1179#define BM_BLOCK_SHIFT 12 /* 4k per bit */
b411b363 1180#define BM_BLOCK_SIZE (1<<BM_BLOCK_SHIFT)
45dfffeb
LE
1181/* mostly arbitrarily set the represented size of one bitmap extent,
1182 * aka resync extent, to 16 MiB (which is also 512 Byte worth of bitmap
1183 * at 4k per bit resolution) */
1184#define BM_EXT_SHIFT 24 /* 16 MiB per resync extent */
b411b363
PR
1185#define BM_EXT_SIZE (1<<BM_EXT_SHIFT)
1186
1187#if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
1188#error "HAVE YOU FIXED drbdmeta AS WELL??"
1189#endif
1190
1191/* thus many _storage_ sectors are described by one bit */
1192#define BM_SECT_TO_BIT(x) ((x)>>(BM_BLOCK_SHIFT-9))
1193#define BM_BIT_TO_SECT(x) ((sector_t)(x)<<(BM_BLOCK_SHIFT-9))
1194#define BM_SECT_PER_BIT BM_BIT_TO_SECT(1)
1195
1196/* bit to represented kilo byte conversion */
1197#define Bit2KB(bits) ((bits)<<(BM_BLOCK_SHIFT-10))
1198
1199/* in which _bitmap_ extent (resp. sector) the bit for a certain
1200 * _storage_ sector is located in */
1201#define BM_SECT_TO_EXT(x) ((x)>>(BM_EXT_SHIFT-9))
5ab7d2c0 1202#define BM_BIT_TO_EXT(x) ((x) >> (BM_EXT_SHIFT - BM_BLOCK_SHIFT))
b411b363 1203
5ab7d2c0 1204/* first storage sector a bitmap extent corresponds to */
b411b363 1205#define BM_EXT_TO_SECT(x) ((sector_t)(x) << (BM_EXT_SHIFT-9))
5ab7d2c0 1206/* how much _storage_ sectors we have per bitmap extent */
b411b363 1207#define BM_SECT_PER_EXT BM_EXT_TO_SECT(1)
5ab7d2c0
LE
1208/* how many bits are covered by one bitmap extent (resync extent) */
1209#define BM_BITS_PER_EXT (1UL << (BM_EXT_SHIFT - BM_BLOCK_SHIFT))
1210
1211#define BM_BLOCKS_PER_BM_EXT_MASK (BM_BITS_PER_EXT - 1)
1212
b411b363
PR
1213
1214/* in one sector of the bitmap, we have this many activity_log extents. */
1215#define AL_EXT_PER_BM_SECT (1 << (BM_EXT_SHIFT - AL_EXTENT_SHIFT))
b411b363 1216
b411b363
PR
1217/* the extent in "PER_EXTENT" below is an activity log extent
1218 * we need that many (long words/bytes) to store the bitmap
1219 * of one AL_EXTENT_SIZE chunk of storage.
1220 * we can store the bitmap for that many AL_EXTENTS within
1221 * one sector of the _on_disk_ bitmap:
1222 * bit 0 bit 37 bit 38 bit (512*8)-1
1223 * ...|........|........|.. // ..|........|
1224 * sect. 0 `296 `304 ^(512*8*8)-1
1225 *
1226#define BM_WORDS_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / BITS_PER_LONG )
1227#define BM_BYTES_PER_EXT ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / 8 ) // 128
1228#define BM_EXT_PER_SECT ( 512 / BM_BYTES_PER_EXTENT ) // 4
1229 */
1230
1231#define DRBD_MAX_SECTORS_32 (0xffffffffLU)
ae8bf312
LE
1232/* we have a certain meta data variant that has a fixed on-disk size of 128
1233 * MiB, of which 4k are our "superblock", and 32k are the fixed size activity
1234 * log, leaving this many sectors for the bitmap.
1235 */
1236
1237#define DRBD_MAX_SECTORS_FIXED_BM \
1238 ((MD_128MB_SECT - MD_32kB_SECT - MD_4kB_SECT) * (1LL<<(BM_EXT_SHIFT-9)))
ae8bf312 1239#define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_FIXED_BM
b411b363
PR
1240/* 16 TB in units of sectors */
1241#if BITS_PER_LONG == 32
1242/* adjust by one page worth of bitmap,
1243 * so we won't wrap around in drbd_bm_find_next_bit.
1244 * you should use 64bit OS for that much storage, anyways. */
1245#define DRBD_MAX_SECTORS_FLEX BM_BIT_TO_SECT(0xffff7fff)
1246#else
4b0715f0
LE
1247/* we allow up to 1 PiB now on 64bit architecture with "flexible" meta data */
1248#define DRBD_MAX_SECTORS_FLEX (1UL << 51)
1249/* corresponds to (1UL << 38) bits right now. */
b411b363 1250#endif
b411b363 1251
8bf223c2 1252/* Estimate max bio size as 256 * PAGE_SIZE,
ea1754a0 1253 * so for typical PAGE_SIZE of 4k, that is (1<<20) Byte.
23361cf3
LE
1254 * Since we may live in a mixed-platform cluster,
1255 * we limit us to a platform agnostic constant here for now.
1256 * A followup commit may allow even bigger BIO sizes,
1257 * once we thought that through. */
98683650 1258#define DRBD_MAX_BIO_SIZE (1U << 20)
a8affc03 1259#if DRBD_MAX_BIO_SIZE > (BIO_MAX_VECS << PAGE_SHIFT)
23361cf3
LE
1260#error Architecture not supported: DRBD_MAX_BIO_SIZE > BIO_MAX_SIZE
1261#endif
db141b2f 1262#define DRBD_MAX_BIO_SIZE_SAFE (1U << 12) /* Works always = 4k */
b411b363 1263
98683650
PR
1264#define DRBD_MAX_SIZE_H80_PACKET (1U << 15) /* Header 80 only allows packets up to 32KiB data */
1265#define DRBD_MAX_BIO_SIZE_P95 (1U << 17) /* Protocol 95 to 99 allows bios up to 128KiB */
b411b363 1266
505675f9
LE
1267/* For now, don't allow more than half of what we can "activate" in one
1268 * activity log transaction to be discarded in one go. We may need to rework
1269 * drbd_al_begin_io() to allow for even larger discard ranges */
9104d31a
LE
1270#define DRBD_MAX_BATCH_BIO_SIZE (AL_UPDATES_PER_TRANSACTION/2*AL_EXTENT_SIZE)
1271#define DRBD_MAX_BBIO_SECTORS (DRBD_MAX_BATCH_BIO_SIZE >> 9)
a0fb3c47 1272
b30ab791
AG
1273extern int drbd_bm_init(struct drbd_device *device);
1274extern int drbd_bm_resize(struct drbd_device *device, sector_t sectors, int set_new_bits);
1275extern void drbd_bm_cleanup(struct drbd_device *device);
1276extern void drbd_bm_set_all(struct drbd_device *device);
1277extern void drbd_bm_clear_all(struct drbd_device *device);
4b0715f0 1278/* set/clear/test only a few bits at a time */
b411b363 1279extern int drbd_bm_set_bits(
b30ab791 1280 struct drbd_device *device, unsigned long s, unsigned long e);
b411b363 1281extern int drbd_bm_clear_bits(
b30ab791 1282 struct drbd_device *device, unsigned long s, unsigned long e);
4b0715f0 1283extern int drbd_bm_count_bits(
b30ab791 1284 struct drbd_device *device, const unsigned long s, const unsigned long e);
4b0715f0
LE
1285/* bm_set_bits variant for use while holding drbd_bm_lock,
1286 * may process the whole bitmap in one go */
b30ab791 1287extern void _drbd_bm_set_bits(struct drbd_device *device,
b411b363 1288 const unsigned long s, const unsigned long e);
b30ab791
AG
1289extern int drbd_bm_test_bit(struct drbd_device *device, unsigned long bitnr);
1290extern int drbd_bm_e_weight(struct drbd_device *device, unsigned long enr);
8164dd6c
AG
1291extern int drbd_bm_read(struct drbd_device *device,
1292 struct drbd_peer_device *peer_device) __must_hold(local);
b30ab791 1293extern void drbd_bm_mark_for_writeout(struct drbd_device *device, int page_nr);
8164dd6c
AG
1294extern int drbd_bm_write(struct drbd_device *device,
1295 struct drbd_peer_device *peer_device) __must_hold(local);
27ea1d87 1296extern void drbd_bm_reset_al_hints(struct drbd_device *device) __must_hold(local);
b30ab791 1297extern int drbd_bm_write_hinted(struct drbd_device *device) __must_hold(local);
c7a58db4 1298extern int drbd_bm_write_lazy(struct drbd_device *device, unsigned upper_idx) __must_hold(local);
8164dd6c
AG
1299extern int drbd_bm_write_all(struct drbd_device *device,
1300 struct drbd_peer_device *peer_device) __must_hold(local);
1301extern int drbd_bm_write_copy_pages(struct drbd_device *device,
1302 struct drbd_peer_device *peer_device) __must_hold(local);
b30ab791
AG
1303extern size_t drbd_bm_words(struct drbd_device *device);
1304extern unsigned long drbd_bm_bits(struct drbd_device *device);
1305extern sector_t drbd_bm_capacity(struct drbd_device *device);
4b0715f0
LE
1306
1307#define DRBD_END_OF_BITMAP (~(unsigned long)0)
b30ab791 1308extern unsigned long drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo);
b411b363 1309/* bm_find_next variants for use while you hold drbd_bm_lock() */
b30ab791
AG
1310extern unsigned long _drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo);
1311extern unsigned long _drbd_bm_find_next_zero(struct drbd_device *device, unsigned long bm_fo);
1312extern unsigned long _drbd_bm_total_weight(struct drbd_device *device);
1313extern unsigned long drbd_bm_total_weight(struct drbd_device *device);
b411b363 1314/* for receive_bitmap */
b30ab791 1315extern void drbd_bm_merge_lel(struct drbd_device *device, size_t offset,
b411b363 1316 size_t number, unsigned long *buffer);
19f843aa 1317/* for _drbd_send_bitmap */
b30ab791 1318extern void drbd_bm_get_lel(struct drbd_device *device, size_t offset,
b411b363
PR
1319 size_t number, unsigned long *buffer);
1320
b30ab791
AG
1321extern void drbd_bm_lock(struct drbd_device *device, char *why, enum bm_flag flags);
1322extern void drbd_bm_unlock(struct drbd_device *device);
b411b363
PR
1323/* drbd_main.c */
1324
1325extern struct kmem_cache *drbd_request_cache;
6c852bec 1326extern struct kmem_cache *drbd_ee_cache; /* peer requests */
b411b363
PR
1327extern struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
1328extern struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
0892fac8
KO
1329extern mempool_t drbd_request_mempool;
1330extern mempool_t drbd_ee_mempool;
b411b363 1331
4281808f
LE
1332/* drbd's page pool, used to buffer data received from the peer,
1333 * or data requested by the peer.
1334 *
1335 * This does not have an emergency reserve.
1336 *
1337 * When allocating from this pool, it first takes pages from the pool.
1338 * Only if the pool is depleted will try to allocate from the system.
1339 *
1340 * The assumption is that pages taken from this pool will be processed,
1341 * and given back, "quickly", and then can be recycled, so we can avoid
1342 * frequent calls to alloc_page(), and still will be able to make progress even
1343 * under memory pressure.
1344 */
1345extern struct page *drbd_pp_pool;
b411b363
PR
1346extern spinlock_t drbd_pp_lock;
1347extern int drbd_pp_vacant;
1348extern wait_queue_head_t drbd_pp_wait;
1349
4281808f
LE
1350/* We also need a standard (emergency-reserve backed) page pool
1351 * for meta data IO (activity log, bitmap).
1352 * We can keep it global, as long as it is used as "N pages at a time".
1353 * 128 should be plenty, currently we probably can get away with as few as 1.
1354 */
1355#define DRBD_MIN_POOL_PAGES 128
0892fac8 1356extern mempool_t drbd_md_io_page_pool;
4281808f 1357
9476f39d
LE
1358/* We also need to make sure we get a bio
1359 * when we need it for housekeeping purposes */
0892fac8 1360extern struct bio_set drbd_md_io_bio_set;
9476f39d 1361
8cb0defb 1362/* And a bio_set for cloning */
0892fac8 1363extern struct bio_set drbd_io_bio_set;
8cb0defb 1364
28bc3b8c 1365extern struct mutex resources_mutex;
b411b363 1366
bde89a9e 1367extern int conn_lowest_minor(struct drbd_connection *connection);
a910b123 1368extern enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsigned int minor);
05a10ec7 1369extern void drbd_destroy_device(struct kref *kref);
a910b123 1370extern void drbd_delete_device(struct drbd_device *device);
b411b363 1371
77c556f6
AG
1372extern struct drbd_resource *drbd_create_resource(const char *name);
1373extern void drbd_free_resource(struct drbd_resource *resource);
1374
eb6bea67 1375extern int set_resource_options(struct drbd_resource *resource, struct res_opts *res_opts);
bde89a9e 1376extern struct drbd_connection *conn_create(const char *name, struct res_opts *res_opts);
05a10ec7 1377extern void drbd_destroy_connection(struct kref *kref);
bde89a9e 1378extern struct drbd_connection *conn_get_by_addrs(void *my_addr, int my_addr_len,
089c075d 1379 void *peer_addr, int peer_addr_len);
4bc76048 1380extern struct drbd_resource *drbd_find_resource(const char *name);
77c556f6 1381extern void drbd_destroy_resource(struct kref *kref);
bde89a9e 1382extern void conn_free_crypto(struct drbd_connection *connection);
b411b363 1383
b411b363 1384/* drbd_req */
113fef9e 1385extern void do_submit(struct work_struct *ws);
370276ba 1386extern void __drbd_make_request(struct drbd_device *, struct bio *);
3e08773c 1387void drbd_submit_bio(struct bio *bio);
b411b363
PR
1388
1389/* drbd_nl.c */
a2972846
AG
1390
1391extern struct mutex notification_mutex;
1392
b30ab791
AG
1393extern void drbd_suspend_io(struct drbd_device *device);
1394extern void drbd_resume_io(struct drbd_device *device);
b411b363 1395extern char *ppsize(char *buf, unsigned long long size);
54761697 1396extern sector_t drbd_new_dev_size(struct drbd_device *, struct drbd_backing_dev *, sector_t, int);
e96c9633 1397enum determine_dev_size {
d752b269
PR
1398 DS_ERROR_SHRINK = -3,
1399 DS_ERROR_SPACE_MD = -2,
e96c9633
PR
1400 DS_ERROR = -1,
1401 DS_UNCHANGED = 0,
1402 DS_SHRUNK = 1,
57737adc
PR
1403 DS_GREW = 2,
1404 DS_GREW_FROM_ZERO = 3,
e96c9633 1405};
d752b269 1406extern enum determine_dev_size
54761697
AG
1407drbd_determine_dev_size(struct drbd_device *, enum dds_flags, struct resize_parms *) __must_hold(local);
1408extern void resync_after_online_grow(struct drbd_device *);
9104d31a
LE
1409extern void drbd_reconsider_queue_parameters(struct drbd_device *device,
1410 struct drbd_backing_dev *bdev, struct o_qlim *o);
b30ab791 1411extern enum drbd_state_rv drbd_set_role(struct drbd_device *device,
bf885f8a
AG
1412 enum drbd_role new_role,
1413 int force);
bde89a9e
AG
1414extern bool conn_try_outdate_peer(struct drbd_connection *connection);
1415extern void conn_try_outdate_peer_async(struct drbd_connection *connection);
7e5fec31 1416extern enum drbd_peer_state conn_khelper(struct drbd_connection *connection, char *cmd);
b30ab791 1417extern int drbd_khelper(struct drbd_device *device, char *cmd);
b411b363
PR
1418
1419/* drbd_worker.c */
d40e5671 1420/* bi_end_io handlers */
4246a0b6
CH
1421extern void drbd_md_endio(struct bio *bio);
1422extern void drbd_peer_request_endio(struct bio *bio);
1423extern void drbd_request_endio(struct bio *bio);
b411b363 1424extern int drbd_worker(struct drbd_thread *thi);
b30ab791
AG
1425enum drbd_ret_code drbd_resync_after_valid(struct drbd_device *device, int o_minor);
1426void drbd_resync_after_changed(struct drbd_device *device);
1427extern void drbd_start_resync(struct drbd_device *device, enum drbd_conns side);
1428extern void resume_next_sg(struct drbd_device *device);
1429extern void suspend_other_sg(struct drbd_device *device);
0d11f3cf 1430extern int drbd_resync_finished(struct drbd_peer_device *peer_device);
b411b363 1431/* maybe rather drbd_main.c ? */
e37d2438 1432extern void *drbd_md_get_buffer(struct drbd_device *device, const char *intent);
b30ab791
AG
1433extern void drbd_md_put_buffer(struct drbd_device *device);
1434extern int drbd_md_sync_page_io(struct drbd_device *device,
9945172a 1435 struct drbd_backing_dev *bdev, sector_t sector, enum req_op op);
0d11f3cf
CB
1436extern void drbd_ov_out_of_sync_found(struct drbd_peer_device *peer_device,
1437 sector_t sector, int size);
b30ab791 1438extern void wait_until_done_or_force_detached(struct drbd_device *device,
44edfb0d 1439 struct drbd_backing_dev *bdev, unsigned int *done);
0d11f3cf 1440extern void drbd_rs_controller_reset(struct drbd_peer_device *peer_device);
b411b363 1441
0d11f3cf 1442static inline void ov_out_of_sync_print(struct drbd_peer_device *peer_device)
b411b363 1443{
0d11f3cf
CB
1444 struct drbd_device *device = peer_device->device;
1445
b30ab791 1446 if (device->ov_last_oos_size) {
0d11f3cf 1447 drbd_err(peer_device, "Out of sync: start=%llu, size=%lu (sectors)\n",
b30ab791
AG
1448 (unsigned long long)device->ov_last_oos_start,
1449 (unsigned long)device->ov_last_oos_size);
b411b363 1450 }
b30ab791 1451 device->ov_last_oos_size = 0;
b411b363
PR
1452}
1453
1454
3d0e6375
KC
1455extern void drbd_csum_bio(struct crypto_shash *, struct bio *, void *);
1456extern void drbd_csum_ee(struct crypto_shash *, struct drbd_peer_request *,
1457 void *);
b411b363 1458/* worker callbacks */
99920dc5
AG
1459extern int w_e_end_data_req(struct drbd_work *, int);
1460extern int w_e_end_rsdata_req(struct drbd_work *, int);
1461extern int w_e_end_csum_rs_req(struct drbd_work *, int);
1462extern int w_e_end_ov_reply(struct drbd_work *, int);
1463extern int w_e_end_ov_req(struct drbd_work *, int);
1464extern int w_ov_finished(struct drbd_work *, int);
1465extern int w_resync_timer(struct drbd_work *, int);
1466extern int w_send_write_hint(struct drbd_work *, int);
99920dc5 1467extern int w_send_dblock(struct drbd_work *, int);
99920dc5 1468extern int w_send_read_req(struct drbd_work *, int);
99920dc5 1469extern int w_restart_disk_io(struct drbd_work *, int);
8f7bed77 1470extern int w_send_out_of_sync(struct drbd_work *, int);
b411b363 1471
2bccef39
KC
1472extern void resync_timer_fn(struct timer_list *t);
1473extern void start_resync_timer_fn(struct timer_list *t);
b411b363 1474
a0fb3c47
LE
1475extern void drbd_endio_write_sec_final(struct drbd_peer_request *peer_req);
1476
b411b363 1477/* drbd_receiver.c */
f31e583a
LE
1478extern int drbd_issue_discard_or_zero_out(struct drbd_device *device,
1479 sector_t start, unsigned int nr_sectors, int flags);
753c6191 1480extern int drbd_receiver(struct drbd_thread *thi);
1c03e520 1481extern int drbd_ack_receiver(struct drbd_thread *thi);
668700b4 1482extern void drbd_send_acks_wf(struct work_struct *ws);
e8299874 1483extern bool drbd_rs_c_min_rate_throttle(struct drbd_device *device);
0d11f3cf 1484extern bool drbd_rs_should_slow_down(struct drbd_peer_device *peer_device, sector_t sector,
ad3fee79 1485 bool throttle_if_app_is_waiting);
ce668b6d 1486extern int drbd_submit_peer_request(struct drbd_peer_request *peer_req);
54761697 1487extern int drbd_free_peer_reqs(struct drbd_device *, struct list_head *);
69a22773 1488extern struct drbd_peer_request *drbd_alloc_peer_req(struct drbd_peer_device *, u64,
0db55363 1489 sector_t, unsigned int,
9104d31a 1490 unsigned int,
0db55363 1491 gfp_t) __must_hold(local);
54761697 1492extern void __drbd_free_peer_req(struct drbd_device *, struct drbd_peer_request *,
3967deb1
AG
1493 int);
1494#define drbd_free_peer_req(m,e) __drbd_free_peer_req(m, e, 0)
1495#define drbd_free_net_peer_req(m,e) __drbd_free_peer_req(m, e, 1)
69a22773 1496extern struct page *drbd_alloc_pages(struct drbd_peer_device *, unsigned int, bool);
b30ab791 1497extern void _drbd_clear_done_ee(struct drbd_device *device, struct list_head *to_be_freed);
69a22773 1498extern int drbd_connected(struct drbd_peer_device *);
b411b363 1499
d40e5671 1500/* sets the number of 512 byte sectors of our virtual device */
d5412e8d 1501void drbd_set_my_capacity(struct drbd_device *device, sector_t size);
d40e5671
PR
1502
1503/*
1504 * used to submit our private bio
1505 */
ed00aabd 1506static inline void drbd_submit_bio_noacct(struct drbd_device *device,
d40e5671
PR
1507 int fault_type, struct bio *bio)
1508{
1509 __release(local);
309dca30
CH
1510 if (!bio->bi_bdev) {
1511 drbd_err(device, "drbd_submit_bio_noacct: bio->bi_bdev == NULL\n");
4e4cbee9 1512 bio->bi_status = BLK_STS_IOERR;
4246a0b6 1513 bio_endio(bio);
d40e5671
PR
1514 return;
1515 }
1516
1517 if (drbd_insert_fault(device, fault_type))
4246a0b6 1518 bio_io_error(bio);
d40e5671 1519 else
ed00aabd 1520 submit_bio_noacct(bio);
d40e5671
PR
1521}
1522
8fe39aac
PR
1523void drbd_bump_write_ordering(struct drbd_resource *resource, struct drbd_backing_dev *bdev,
1524 enum write_ordering_e wo);
b411b363
PR
1525
1526/* drbd_proc.c */
1527extern struct proc_dir_entry *drbd_proc;
004fd11d 1528int drbd_seq_show(struct seq_file *seq, void *v);
b411b363
PR
1529
1530/* drbd_actlog.c */
e4d7d6f4 1531extern bool drbd_al_begin_io_prepare(struct drbd_device *device, struct drbd_interval *i);
b30ab791 1532extern int drbd_al_begin_io_nonblock(struct drbd_device *device, struct drbd_interval *i);
4dd726f0 1533extern void drbd_al_begin_io_commit(struct drbd_device *device);
b30ab791 1534extern bool drbd_al_begin_io_fastpath(struct drbd_device *device, struct drbd_interval *i);
4dd726f0 1535extern void drbd_al_begin_io(struct drbd_device *device, struct drbd_interval *i);
b30ab791
AG
1536extern void drbd_al_complete_io(struct drbd_device *device, struct drbd_interval *i);
1537extern void drbd_rs_complete_io(struct drbd_device *device, sector_t sector);
1538extern int drbd_rs_begin_io(struct drbd_device *device, sector_t sector);
0d11f3cf 1539extern int drbd_try_rs_begin_io(struct drbd_peer_device *peer_device, sector_t sector);
b30ab791
AG
1540extern void drbd_rs_cancel_all(struct drbd_device *device);
1541extern int drbd_rs_del_all(struct drbd_device *device);
0d11f3cf 1542extern void drbd_rs_failed_io(struct drbd_peer_device *peer_device,
b411b363 1543 sector_t sector, int size);
0d11f3cf 1544extern void drbd_advance_rs_marks(struct drbd_peer_device *peer_device, unsigned long still_to_go);
5ab7d2c0
LE
1545
1546enum update_sync_bits_mode { RECORD_RS_FAILED, SET_OUT_OF_SYNC, SET_IN_SYNC };
0d11f3cf 1547extern int __drbd_change_sync(struct drbd_peer_device *peer_device, sector_t sector, int size,
179e20b8 1548 enum update_sync_bits_mode mode);
0d11f3cf
CB
1549#define drbd_set_in_sync(peer_device, sector, size) \
1550 __drbd_change_sync(peer_device, sector, size, SET_IN_SYNC)
1551#define drbd_set_out_of_sync(peer_device, sector, size) \
1552 __drbd_change_sync(peer_device, sector, size, SET_OUT_OF_SYNC)
1553#define drbd_rs_failed_io(peer_device, sector, size) \
1554 __drbd_change_sync(peer_device, sector, size, RECORD_RS_FAILED)
b30ab791 1555extern void drbd_al_shrink(struct drbd_device *device);
5f7c0124 1556extern int drbd_al_initialize(struct drbd_device *, void *);
b411b363 1557
b411b363 1558/* drbd_nl.c */
3b98c0c2
LE
1559/* state info broadcast */
1560struct sib_info {
1561 enum drbd_state_info_bcast_reason sib_reason;
1562 union {
1563 struct {
1564 char *helper_name;
1565 unsigned helper_exit_code;
1566 };
1567 struct {
1568 union drbd_state os;
1569 union drbd_state ns;
1570 };
1571 };
1572};
b30ab791 1573void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib);
b411b363 1574
aadb22ba 1575extern int notify_resource_state(struct sk_buff *,
a2972846
AG
1576 unsigned int,
1577 struct drbd_resource *,
1578 struct resource_info *,
1579 enum drbd_notification_type);
aadb22ba 1580extern int notify_device_state(struct sk_buff *,
a2972846
AG
1581 unsigned int,
1582 struct drbd_device *,
1583 struct device_info *,
1584 enum drbd_notification_type);
aadb22ba 1585extern int notify_connection_state(struct sk_buff *,
a2972846
AG
1586 unsigned int,
1587 struct drbd_connection *,
1588 struct connection_info *,
1589 enum drbd_notification_type);
aadb22ba 1590extern int notify_peer_device_state(struct sk_buff *,
a2972846
AG
1591 unsigned int,
1592 struct drbd_peer_device *,
1593 struct peer_device_info *,
1594 enum drbd_notification_type);
1595extern void notify_helper(enum drbd_notification_type, struct drbd_device *,
1596 struct drbd_connection *, const char *, int);
1597
b411b363
PR
1598/*
1599 * inline helper functions
1600 *************************/
1601
45bb912b
LE
1602/* see also page_chain_add and friends in drbd_receiver.c */
1603static inline struct page *page_chain_next(struct page *page)
1604{
1605 return (struct page *)page_private(page);
1606}
1607#define page_chain_for_each(page) \
1608 for (; page && ({ prefetch(page_chain_next(page)); 1; }); \
1609 page = page_chain_next(page))
1610#define page_chain_for_each_safe(page, n) \
1611 for (; page && ({ n = page_chain_next(page); 1; }); page = n)
1612
45bb912b 1613
045417f7 1614static inline int drbd_peer_req_has_active_page(struct drbd_peer_request *peer_req)
45bb912b 1615{
db830c46 1616 struct page *page = peer_req->pages;
45bb912b
LE
1617 page_chain_for_each(page) {
1618 if (page_count(page) > 1)
1619 return 1;
1620 }
1621 return 0;
1622}
1623
b30ab791 1624static inline union drbd_state drbd_read_state(struct drbd_device *device)
b411b363 1625{
6bbf53ca 1626 struct drbd_resource *resource = device->resource;
78bae59b
PR
1627 union drbd_state rv;
1628
b30ab791 1629 rv.i = device->state.i;
6bbf53ca
AG
1630 rv.susp = resource->susp;
1631 rv.susp_nod = resource->susp_nod;
1632 rv.susp_fen = resource->susp_fen;
78bae59b
PR
1633
1634 return rv;
b411b363
PR
1635}
1636
383606e0 1637enum drbd_force_detach_flags {
a2a3c74f
LE
1638 DRBD_READ_ERROR,
1639 DRBD_WRITE_ERROR,
383606e0
LE
1640 DRBD_META_IO_ERROR,
1641 DRBD_FORCE_DETACH,
1642};
1643
b411b363 1644#define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
b30ab791 1645static inline void __drbd_chk_io_error_(struct drbd_device *device,
a2a3c74f 1646 enum drbd_force_detach_flags df,
383606e0 1647 const char *where)
b411b363 1648{
daeda1cc
PR
1649 enum drbd_io_error_p ep;
1650
1651 rcu_read_lock();
b30ab791 1652 ep = rcu_dereference(device->ldev->disk_conf)->on_io_error;
daeda1cc
PR
1653 rcu_read_unlock();
1654 switch (ep) {
1655 case EP_PASS_ON: /* FIXME would this be better named "Ignore"? */
a2a3c74f 1656 if (df == DRBD_READ_ERROR || df == DRBD_WRITE_ERROR) {
e3fa02d7 1657 if (drbd_ratelimit())
d0180171 1658 drbd_err(device, "Local IO failed in %s.\n", where);
b30ab791
AG
1659 if (device->state.disk > D_INCONSISTENT)
1660 _drbd_set_state(_NS(device, disk, D_INCONSISTENT), CS_HARD, NULL);
b411b363
PR
1661 break;
1662 }
df561f66 1663 fallthrough; /* for DRBD_META_IO_ERROR or DRBD_FORCE_DETACH */
b411b363
PR
1664 case EP_DETACH:
1665 case EP_CALL_HELPER:
a2a3c74f
LE
1666 /* Remember whether we saw a READ or WRITE error.
1667 *
1668 * Recovery of the affected area for WRITE failure is covered
1669 * by the activity log.
1670 * READ errors may fall outside that area though. Certain READ
1671 * errors can be "healed" by writing good data to the affected
1672 * blocks, which triggers block re-allocation in lower layers.
1673 *
1674 * If we can not write the bitmap after a READ error,
1675 * we may need to trigger a full sync (see w_go_diskless()).
1676 *
1677 * Force-detach is not really an IO error, but rather a
1678 * desperate measure to try to deal with a completely
1679 * unresponsive lower level IO stack.
1680 * Still it should be treated as a WRITE error.
1681 *
1682 * Meta IO error is always WRITE error:
1683 * we read meta data only once during attach,
1684 * which will fail in case of errors.
1685 */
b30ab791 1686 set_bit(WAS_IO_ERROR, &device->flags);
a2a3c74f 1687 if (df == DRBD_READ_ERROR)
b30ab791 1688 set_bit(WAS_READ_ERROR, &device->flags);
a2a3c74f 1689 if (df == DRBD_FORCE_DETACH)
b30ab791
AG
1690 set_bit(FORCE_DETACH, &device->flags);
1691 if (device->state.disk > D_FAILED) {
1692 _drbd_set_state(_NS(device, disk, D_FAILED), CS_HARD, NULL);
d0180171 1693 drbd_err(device,
82f59cc6 1694 "Local IO failed in %s. Detaching...\n", where);
b411b363
PR
1695 }
1696 break;
1697 }
1698}
1699
1700/**
1701 * drbd_chk_io_error: Handle the on_io_error setting, should be called from all io completion handlers
b30ab791 1702 * @device: DRBD device.
b411b363
PR
1703 * @error: Error code passed to the IO completion callback
1704 * @forcedetach: Force detach. I.e. the error happened while accessing the meta data
1705 *
1706 * See also drbd_main.c:after_state_ch() if (os.disk > D_FAILED && ns.disk == D_FAILED)
1707 */
1708#define drbd_chk_io_error(m,e,f) drbd_chk_io_error_(m,e,f, __func__)
b30ab791 1709static inline void drbd_chk_io_error_(struct drbd_device *device,
383606e0 1710 int error, enum drbd_force_detach_flags forcedetach, const char *where)
b411b363
PR
1711{
1712 if (error) {
1713 unsigned long flags;
0500813f 1714 spin_lock_irqsave(&device->resource->req_lock, flags);
b30ab791 1715 __drbd_chk_io_error_(device, forcedetach, where);
0500813f 1716 spin_unlock_irqrestore(&device->resource->req_lock, flags);
b411b363
PR
1717 }
1718}
1719
1720
1721/**
1722 * drbd_md_first_sector() - Returns the first sector number of the meta data area
1723 * @bdev: Meta data block device.
1724 *
1725 * BTW, for internal meta data, this happens to be the maximum capacity
1726 * we could agree upon with our peer node.
1727 */
68e41a43 1728static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
b411b363 1729{
68e41a43 1730 switch (bdev->md.meta_dev_idx) {
b411b363
PR
1731 case DRBD_MD_INDEX_INTERNAL:
1732 case DRBD_MD_INDEX_FLEX_INT:
1733 return bdev->md.md_offset + bdev->md.bm_offset;
1734 case DRBD_MD_INDEX_FLEX_EXT:
1735 default:
1736 return bdev->md.md_offset;
1737 }
1738}
1739
1740/**
1741 * drbd_md_last_sector() - Return the last sector number of the meta data area
1742 * @bdev: Meta data block device.
1743 */
1744static inline sector_t drbd_md_last_sector(struct drbd_backing_dev *bdev)
1745{
68e41a43 1746 switch (bdev->md.meta_dev_idx) {
b411b363
PR
1747 case DRBD_MD_INDEX_INTERNAL:
1748 case DRBD_MD_INDEX_FLEX_INT:
ae8bf312 1749 return bdev->md.md_offset + MD_4kB_SECT -1;
b411b363
PR
1750 case DRBD_MD_INDEX_FLEX_EXT:
1751 default:
ae8bf312 1752 return bdev->md.md_offset + bdev->md.md_size_sect -1;
b411b363
PR
1753 }
1754}
1755
1756/* Returns the number of 512 byte sectors of the device */
1757static inline sector_t drbd_get_capacity(struct block_device *bdev)
1758{
da7b3924 1759 return bdev ? bdev_nr_sectors(bdev) : 0;
b411b363
PR
1760}
1761
1762/**
1763 * drbd_get_max_capacity() - Returns the capacity we announce to out peer
1764 * @bdev: Meta data block device.
1765 *
1766 * returns the capacity we announce to out peer. we clip ourselves at the
1767 * various MAX_SECTORS, because if we don't, current implementation will
1768 * oops sooner or later
1769 */
1770static inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
1771{
1772 sector_t s;
daeda1cc 1773
68e41a43 1774 switch (bdev->md.meta_dev_idx) {
b411b363
PR
1775 case DRBD_MD_INDEX_INTERNAL:
1776 case DRBD_MD_INDEX_FLEX_INT:
1777 s = drbd_get_capacity(bdev->backing_bdev)
1778 ? min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
68e41a43 1779 drbd_md_first_sector(bdev))
b411b363
PR
1780 : 0;
1781 break;
1782 case DRBD_MD_INDEX_FLEX_EXT:
1783 s = min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
1784 drbd_get_capacity(bdev->backing_bdev));
1785 /* clip at maximum size the meta device can support */
1786 s = min_t(sector_t, s,
1787 BM_EXT_TO_SECT(bdev->md.md_size_sect
1788 - bdev->md.bm_offset));
1789 break;
1790 default:
1791 s = min_t(sector_t, DRBD_MAX_SECTORS,
1792 drbd_get_capacity(bdev->backing_bdev));
1793 }
1794 return s;
1795}
1796
1797/**
3a4d4eb3 1798 * drbd_md_ss() - Return the sector number of our meta data super block
b411b363
PR
1799 * @bdev: Meta data block device.
1800 */
3a4d4eb3 1801static inline sector_t drbd_md_ss(struct drbd_backing_dev *bdev)
b411b363 1802{
3a4d4eb3 1803 const int meta_dev_idx = bdev->md.meta_dev_idx;
daeda1cc 1804
3a4d4eb3
LE
1805 if (meta_dev_idx == DRBD_MD_INDEX_FLEX_EXT)
1806 return 0;
daeda1cc 1807
3a4d4eb3
LE
1808 /* Since drbd08, internal meta data is always "flexible".
1809 * position: last 4k aligned block of 4k size */
1810 if (meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1811 meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)
ae8bf312 1812 return (drbd_get_capacity(bdev->backing_bdev) & ~7ULL) - 8;
3a4d4eb3
LE
1813
1814 /* external, some index; this is the old fixed size layout */
1815 return MD_128MB_SECT * bdev->md.meta_dev_idx;
b411b363
PR
1816}
1817
b411b363
PR
1818static inline void
1819drbd_queue_work(struct drbd_work_queue *q, struct drbd_work *w)
1820{
1821 unsigned long flags;
1822 spin_lock_irqsave(&q->q_lock, flags);
1823 list_add_tail(&w->list, &q->q);
b411b363 1824 spin_unlock_irqrestore(&q->q_lock, flags);
8c0785a5 1825 wake_up(&q->q_wait);
b411b363
PR
1826}
1827
15e26f6a
LE
1828static inline void
1829drbd_queue_work_if_unqueued(struct drbd_work_queue *q, struct drbd_work *w)
1830{
1831 unsigned long flags;
1832 spin_lock_irqsave(&q->q_lock, flags);
1833 if (list_empty_careful(&w->list))
1834 list_add_tail(&w->list, &q->q);
1835 spin_unlock_irqrestore(&q->q_lock, flags);
1836 wake_up(&q->q_wait);
1837}
1838
e334f550
LE
1839static inline void
1840drbd_device_post_work(struct drbd_device *device, int work_bit)
1841{
1842 if (!test_and_set_bit(work_bit, &device->flags)) {
1843 struct drbd_connection *connection =
1844 first_peer_device(device)->connection;
1845 struct drbd_work_queue *q = &connection->sender_work;
1846 if (!test_and_set_bit(DEVICE_WORK_PENDING, &connection->flags))
1847 wake_up(&q->q_wait);
1848 }
1849}
1850
b5043c5e
AG
1851extern void drbd_flush_workqueue(struct drbd_work_queue *work_queue);
1852
668700b4
PR
1853/* To get the ack_receiver out of the blocking network stack,
1854 * so it can change its sk_rcvtimeo from idle- to ping-timeout,
1855 * and send a ping, we need to send a signal.
1856 * Which signal we send is irrelevant. */
1857static inline void wake_ack_receiver(struct drbd_connection *connection)
b411b363 1858{
668700b4
PR
1859 struct task_struct *task = connection->ack_receiver.task;
1860 if (task && get_t_state(&connection->ack_receiver) == RUNNING)
fee10990 1861 send_sig(SIGXCPU, task, 1);
b411b363
PR
1862}
1863
bde89a9e 1864static inline void request_ping(struct drbd_connection *connection)
b411b363 1865{
bde89a9e 1866 set_bit(SEND_PING, &connection->flags);
668700b4 1867 wake_ack_receiver(connection);
b411b363
PR
1868}
1869
bde89a9e 1870extern void *conn_prepare_command(struct drbd_connection *, struct drbd_socket *);
69a22773 1871extern void *drbd_prepare_command(struct drbd_peer_device *, struct drbd_socket *);
bde89a9e 1872extern int conn_send_command(struct drbd_connection *, struct drbd_socket *,
dba58587
AG
1873 enum drbd_packet, unsigned int, void *,
1874 unsigned int);
69a22773 1875extern int drbd_send_command(struct drbd_peer_device *, struct drbd_socket *,
dba58587
AG
1876 enum drbd_packet, unsigned int, void *,
1877 unsigned int);
b411b363 1878
bde89a9e
AG
1879extern int drbd_send_ping(struct drbd_connection *connection);
1880extern int drbd_send_ping_ack(struct drbd_connection *connection);
69a22773 1881extern int drbd_send_state_req(struct drbd_peer_device *, union drbd_state, union drbd_state);
bde89a9e 1882extern int conn_send_state_req(struct drbd_connection *, union drbd_state, union drbd_state);
b411b363
PR
1883
1884static inline void drbd_thread_stop(struct drbd_thread *thi)
1885{
81e84650 1886 _drbd_thread_stop(thi, false, true);
b411b363
PR
1887}
1888
1889static inline void drbd_thread_stop_nowait(struct drbd_thread *thi)
1890{
81e84650 1891 _drbd_thread_stop(thi, false, false);
b411b363
PR
1892}
1893
1894static inline void drbd_thread_restart_nowait(struct drbd_thread *thi)
1895{
81e84650 1896 _drbd_thread_stop(thi, true, false);
b411b363
PR
1897}
1898
1899/* counts how many answer packets packets we expect from our peer,
1900 * for either explicit application requests,
1901 * or implicit barrier packets as necessary.
1902 * increased:
1903 * w_send_barrier
8554df1c 1904 * _req_mod(req, QUEUE_FOR_NET_WRITE or QUEUE_FOR_NET_READ);
b411b363
PR
1905 * it is much easier and equally valid to count what we queue for the
1906 * worker, even before it actually was queued or send.
1907 * (drbd_make_request_common; recovery path on read io-error)
1908 * decreased:
1909 * got_BarrierAck (respective tl_clear, tl_clear_barrier)
8554df1c 1910 * _req_mod(req, DATA_RECEIVED)
b411b363 1911 * [from receive_DataReply]
8554df1c 1912 * _req_mod(req, WRITE_ACKED_BY_PEER or RECV_ACKED_BY_PEER or NEG_ACKED)
b411b363
PR
1913 * [from got_BlockAck (P_WRITE_ACK, P_RECV_ACK)]
1914 * for some reason it is NOT decreased in got_NegAck,
1915 * but in the resulting cleanup code from report_params.
1916 * we should try to remember the reason for that...
8554df1c
AG
1917 * _req_mod(req, SEND_FAILED or SEND_CANCELED)
1918 * _req_mod(req, CONNECTION_LOST_WHILE_PENDING)
b411b363
PR
1919 * [from tl_clear_barrier]
1920 */
b30ab791 1921static inline void inc_ap_pending(struct drbd_device *device)
b411b363 1922{
b30ab791 1923 atomic_inc(&device->ap_pending_cnt);
b411b363
PR
1924}
1925
33f7d316
AG
1926#define dec_ap_pending(device) ((void)expect((device), __dec_ap_pending(device) >= 0))
1927static inline int __dec_ap_pending(struct drbd_device *device)
49559d87 1928{
33f7d316
AG
1929 int ap_pending_cnt = atomic_dec_return(&device->ap_pending_cnt);
1930
1931 if (ap_pending_cnt == 0)
b30ab791 1932 wake_up(&device->misc_wait);
33f7d316 1933 return ap_pending_cnt;
49559d87 1934}
b411b363
PR
1935
1936/* counts how many resync-related answers we still expect from the peer
1937 * increase decrease
1938 * C_SYNC_TARGET sends P_RS_DATA_REQUEST (and expects P_RS_DATA_REPLY)
25985edc 1939 * C_SYNC_SOURCE sends P_RS_DATA_REPLY (and expects P_WRITE_ACK with ID_SYNCER)
b411b363
PR
1940 * (or P_NEG_ACK with ID_SYNCER)
1941 */
0d11f3cf 1942static inline void inc_rs_pending(struct drbd_peer_device *peer_device)
b411b363 1943{
0d11f3cf 1944 atomic_inc(&peer_device->device->rs_pending_cnt);
b411b363
PR
1945}
1946
0d11f3cf
CB
1947#define dec_rs_pending(peer_device) \
1948 ((void)expect((peer_device), __dec_rs_pending(peer_device) >= 0))
1949static inline int __dec_rs_pending(struct drbd_peer_device *peer_device)
49559d87 1950{
0d11f3cf 1951 return atomic_dec_return(&peer_device->device->rs_pending_cnt);
49559d87 1952}
b411b363
PR
1953
1954/* counts how many answers we still need to send to the peer.
1955 * increased on
1956 * receive_Data unless protocol A;
1957 * we need to send a P_RECV_ACK (proto B)
1958 * or P_WRITE_ACK (proto C)
1959 * receive_RSDataReply (recv_resync_read) we need to send a P_WRITE_ACK
1960 * receive_DataRequest (receive_RSDataRequest) we need to send back P_DATA
1961 * receive_Barrier_* we need to send a P_BARRIER_ACK
1962 */
b30ab791 1963static inline void inc_unacked(struct drbd_device *device)
b411b363 1964{
b30ab791 1965 atomic_inc(&device->unacked_cnt);
b411b363
PR
1966}
1967
33f7d316
AG
1968#define dec_unacked(device) ((void)expect(device, __dec_unacked(device) >= 0))
1969static inline int __dec_unacked(struct drbd_device *device)
b411b363 1970{
33f7d316 1971 return atomic_dec_return(&device->unacked_cnt);
b411b363
PR
1972}
1973
33f7d316
AG
1974#define sub_unacked(device, n) ((void)expect(device, __sub_unacked(device) >= 0))
1975static inline int __sub_unacked(struct drbd_device *device, int n)
b411b363 1976{
33f7d316 1977 return atomic_sub_return(n, &device->unacked_cnt);
b411b363
PR
1978}
1979
5052fee2
LE
1980static inline bool is_sync_target_state(enum drbd_conns connection_state)
1981{
1982 return connection_state == C_SYNC_TARGET ||
1983 connection_state == C_PAUSED_SYNC_T;
1984}
1985
1986static inline bool is_sync_source_state(enum drbd_conns connection_state)
1987{
1988 return connection_state == C_SYNC_SOURCE ||
1989 connection_state == C_PAUSED_SYNC_S;
1990}
1991
5ab7d2c0
LE
1992static inline bool is_sync_state(enum drbd_conns connection_state)
1993{
5052fee2
LE
1994 return is_sync_source_state(connection_state) ||
1995 is_sync_target_state(connection_state);
5ab7d2c0
LE
1996}
1997
b411b363 1998/**
b30ab791 1999 * get_ldev() - Increase the ref count on device->ldev. Returns 0 if there is no ldev
d1b80853
AG
2000 * @_device: DRBD device.
2001 * @_min_state: Minimum device state required for success.
b411b363 2002 *
b30ab791 2003 * You have to call put_ldev() when finished working with device->ldev.
b411b363 2004 */
d1b80853
AG
2005#define get_ldev_if_state(_device, _min_state) \
2006 (_get_ldev_if_state((_device), (_min_state)) ? \
2007 ({ __acquire(x); true; }) : false)
2008#define get_ldev(_device) get_ldev_if_state(_device, D_INCONSISTENT)
b411b363 2009
b30ab791 2010static inline void put_ldev(struct drbd_device *device)
b411b363 2011{
11f8b2b6 2012 enum drbd_disk_state disk_state = device->state.disk;
ba3c6fb8
LE
2013 /* We must check the state *before* the atomic_dec becomes visible,
2014 * or we have a theoretical race where someone hitting zero,
2015 * while state still D_FAILED, will then see D_DISKLESS in the
2016 * condition below and calling into destroy, where he must not, yet. */
b30ab791 2017 int i = atomic_dec_return(&device->local_cnt);
9a0d9d03
LE
2018
2019 /* This may be called from some endio handler,
2020 * so we must not sleep here. */
2021
b411b363 2022 __release(local);
0b0ba1ef 2023 D_ASSERT(device, i >= 0);
e9e6f3ec 2024 if (i == 0) {
11f8b2b6 2025 if (disk_state == D_DISKLESS)
82f59cc6 2026 /* even internal references gone, safe to destroy */
e334f550 2027 drbd_device_post_work(device, DESTROY_DISK);
11f8b2b6 2028 if (disk_state == D_FAILED)
82f59cc6 2029 /* all application IO references gone. */
e334f550
LE
2030 if (!test_and_set_bit(GOING_DISKLESS, &device->flags))
2031 drbd_device_post_work(device, GO_DISKLESS);
b30ab791 2032 wake_up(&device->misc_wait);
e9e6f3ec 2033 }
b411b363
PR
2034}
2035
2036#ifndef __CHECKER__
b30ab791 2037static inline int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins)
b411b363
PR
2038{
2039 int io_allowed;
2040
82f59cc6 2041 /* never get a reference while D_DISKLESS */
b30ab791 2042 if (device->state.disk == D_DISKLESS)
82f59cc6
LE
2043 return 0;
2044
b30ab791
AG
2045 atomic_inc(&device->local_cnt);
2046 io_allowed = (device->state.disk >= mins);
b411b363 2047 if (!io_allowed)
b30ab791 2048 put_ldev(device);
b411b363
PR
2049 return io_allowed;
2050}
2051#else
b30ab791 2052extern int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins);
b411b363
PR
2053#endif
2054
b411b363
PR
2055/* this throttles on-the-fly application requests
2056 * according to max_buffers settings;
2057 * maybe re-implement using semaphores? */
b30ab791 2058static inline int drbd_get_max_buffers(struct drbd_device *device)
b411b363 2059{
44ed167d
PR
2060 struct net_conf *nc;
2061 int mxb;
2062
2063 rcu_read_lock();
a6b32bc3 2064 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
44ed167d
PR
2065 mxb = nc ? nc->max_buffers : 1000000; /* arbitrary limit on open requests */
2066 rcu_read_unlock();
2067
b411b363
PR
2068 return mxb;
2069}
2070
b30ab791 2071static inline int drbd_state_is_stable(struct drbd_device *device)
b411b363 2072{
b30ab791 2073 union drbd_dev_state s = device->state;
b411b363
PR
2074
2075 /* DO NOT add a default clause, we want the compiler to warn us
2076 * for any newly introduced state we may have forgotten to add here */
2077
2078 switch ((enum drbd_conns)s.conn) {
2079 /* new io only accepted when there is no connection, ... */
2080 case C_STANDALONE:
2081 case C_WF_CONNECTION:
2082 /* ... or there is a well established connection. */
2083 case C_CONNECTED:
2084 case C_SYNC_SOURCE:
2085 case C_SYNC_TARGET:
2086 case C_VERIFY_S:
2087 case C_VERIFY_T:
2088 case C_PAUSED_SYNC_S:
2089 case C_PAUSED_SYNC_T:
67531718
PR
2090 case C_AHEAD:
2091 case C_BEHIND:
3719094e 2092 /* transitional states, IO allowed */
b411b363
PR
2093 case C_DISCONNECTING:
2094 case C_UNCONNECTED:
2095 case C_TIMEOUT:
2096 case C_BROKEN_PIPE:
2097 case C_NETWORK_FAILURE:
2098 case C_PROTOCOL_ERROR:
2099 case C_TEAR_DOWN:
2100 case C_WF_REPORT_PARAMS:
2101 case C_STARTING_SYNC_S:
2102 case C_STARTING_SYNC_T:
3719094e
PR
2103 break;
2104
2105 /* Allow IO in BM exchange states with new protocols */
b411b363 2106 case C_WF_BITMAP_S:
a6b32bc3 2107 if (first_peer_device(device)->connection->agreed_pro_version < 96)
3719094e
PR
2108 return 0;
2109 break;
2110
2111 /* no new io accepted in these states */
b411b363
PR
2112 case C_WF_BITMAP_T:
2113 case C_WF_SYNC_UUID:
2114 case C_MASK:
2115 /* not "stable" */
2116 return 0;
2117 }
2118
2119 switch ((enum drbd_disk_state)s.disk) {
2120 case D_DISKLESS:
2121 case D_INCONSISTENT:
2122 case D_OUTDATED:
2123 case D_CONSISTENT:
2124 case D_UP_TO_DATE:
5ca1de03 2125 case D_FAILED:
b411b363
PR
2126 /* disk state is stable as well. */
2127 break;
2128
d942ae44 2129 /* no new io accepted during transitional states */
b411b363 2130 case D_ATTACHING:
b411b363
PR
2131 case D_NEGOTIATING:
2132 case D_UNKNOWN:
2133 case D_MASK:
2134 /* not "stable" */
2135 return 0;
2136 }
2137
2138 return 1;
2139}
2140
b30ab791 2141static inline int drbd_suspended(struct drbd_device *device)
fb22c402 2142{
6bbf53ca 2143 struct drbd_resource *resource = device->resource;
8e0af25f 2144
6bbf53ca 2145 return resource->susp || resource->susp_fen || resource->susp_nod;
fb22c402
PR
2146}
2147
b30ab791 2148static inline bool may_inc_ap_bio(struct drbd_device *device)
b411b363 2149{
b30ab791 2150 int mxb = drbd_get_max_buffers(device);
b411b363 2151
b30ab791 2152 if (drbd_suspended(device))
1b881ef7 2153 return false;
7dbb4386 2154 if (atomic_read(&device->suspend_cnt))
1b881ef7 2155 return false;
b411b363
PR
2156
2157 /* to avoid potential deadlock or bitmap corruption,
2158 * in various places, we only allow new application io
2159 * to start during "stable" states. */
2160
2161 /* no new io accepted when attaching or detaching the disk */
b30ab791 2162 if (!drbd_state_is_stable(device))
1b881ef7 2163 return false;
b411b363
PR
2164
2165 /* since some older kernels don't have atomic_add_unless,
2166 * and we are within the spinlock anyways, we have this workaround. */
b30ab791 2167 if (atomic_read(&device->ap_bio_cnt) > mxb)
1b881ef7 2168 return false;
b30ab791 2169 if (test_bit(BITMAP_IO, &device->flags))
1b881ef7
AG
2170 return false;
2171 return true;
b411b363
PR
2172}
2173
b30ab791 2174static inline bool inc_ap_bio_cond(struct drbd_device *device)
b411b363 2175{
1b881ef7 2176 bool rv = false;
8869d683 2177
0500813f 2178 spin_lock_irq(&device->resource->req_lock);
b30ab791 2179 rv = may_inc_ap_bio(device);
8869d683 2180 if (rv)
b30ab791 2181 atomic_inc(&device->ap_bio_cnt);
0500813f 2182 spin_unlock_irq(&device->resource->req_lock);
8869d683
PR
2183
2184 return rv;
2185}
b411b363 2186
b30ab791 2187static inline void inc_ap_bio(struct drbd_device *device)
8869d683 2188{
b411b363
PR
2189 /* we wait here
2190 * as long as the device is suspended
2191 * until the bitmap is no longer on the fly during connection
d942ae44 2192 * handshake as long as we would exceed the max_buffer limit.
b411b363
PR
2193 *
2194 * to avoid races with the reconnect code,
2195 * we need to atomic_inc within the spinlock. */
2196
b30ab791 2197 wait_event(device->misc_wait, inc_ap_bio_cond(device));
b411b363
PR
2198}
2199
b30ab791 2200static inline void dec_ap_bio(struct drbd_device *device)
b411b363 2201{
b30ab791
AG
2202 int mxb = drbd_get_max_buffers(device);
2203 int ap_bio = atomic_dec_return(&device->ap_bio_cnt);
b411b363 2204
0b0ba1ef 2205 D_ASSERT(device, ap_bio >= 0);
7ee1fb93 2206
b30ab791
AG
2207 if (ap_bio == 0 && test_bit(BITMAP_IO, &device->flags)) {
2208 if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags))
84b8c06b
AG
2209 drbd_queue_work(&first_peer_device(device)->
2210 connection->sender_work,
2211 &device->bm_io_work.w);
7ee1fb93
LE
2212 }
2213
b411b363
PR
2214 /* this currently does wake_up for every dec_ap_bio!
2215 * maybe rather introduce some type of hysteresis?
2216 * e.g. (ap_bio == mxb/2 || ap_bio == 0) ? */
2217 if (ap_bio < mxb)
b30ab791 2218 wake_up(&device->misc_wait);
b411b363
PR
2219}
2220
b30ab791 2221static inline bool verify_can_do_stop_sector(struct drbd_device *device)
58ffa580 2222{
a6b32bc3
AG
2223 return first_peer_device(device)->connection->agreed_pro_version >= 97 &&
2224 first_peer_device(device)->connection->agreed_pro_version != 100;
58ffa580
LE
2225}
2226
b30ab791 2227static inline int drbd_set_ed_uuid(struct drbd_device *device, u64 val)
b411b363 2228{
b30ab791
AG
2229 int changed = device->ed_uuid != val;
2230 device->ed_uuid = val;
62b0da3a 2231 return changed;
b411b363
PR
2232}
2233
b30ab791 2234static inline int drbd_queue_order_type(struct drbd_device *device)
b411b363
PR
2235{
2236 /* sorry, we currently have no working implementation
2237 * of distributed TCQ stuff */
2238#ifndef QUEUE_ORDERED_NONE
2239#define QUEUE_ORDERED_NONE 0
2240#endif
2241 return QUEUE_ORDERED_NONE;
2242}
2243
77c556f6
AG
2244static inline struct drbd_connection *first_connection(struct drbd_resource *resource)
2245{
ec4a3407 2246 return list_first_entry_or_null(&resource->connections,
77c556f6
AG
2247 struct drbd_connection, connections);
2248}
2249
b411b363 2250#endif