162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci  drbd_int.h
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci  This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci  Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
862306a36Sopenharmony_ci  Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
962306a36Sopenharmony_ci  Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci*/
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#ifndef _DRBD_INT_H
1562306a36Sopenharmony_ci#define _DRBD_INT_H
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <crypto/hash.h>
1862306a36Sopenharmony_ci#include <linux/compiler.h>
1962306a36Sopenharmony_ci#include <linux/types.h>
2062306a36Sopenharmony_ci#include <linux/list.h>
2162306a36Sopenharmony_ci#include <linux/sched/signal.h>
2262306a36Sopenharmony_ci#include <linux/bitops.h>
2362306a36Sopenharmony_ci#include <linux/slab.h>
2462306a36Sopenharmony_ci#include <linux/ratelimit.h>
2562306a36Sopenharmony_ci#include <linux/tcp.h>
2662306a36Sopenharmony_ci#include <linux/mutex.h>
2762306a36Sopenharmony_ci#include <linux/major.h>
2862306a36Sopenharmony_ci#include <linux/blkdev.h>
2962306a36Sopenharmony_ci#include <linux/backing-dev.h>
3062306a36Sopenharmony_ci#include <linux/idr.h>
3162306a36Sopenharmony_ci#include <linux/dynamic_debug.h>
3262306a36Sopenharmony_ci#include <net/tcp.h>
3362306a36Sopenharmony_ci#include <linux/lru_cache.h>
3462306a36Sopenharmony_ci#include <linux/prefetch.h>
3562306a36Sopenharmony_ci#include <linux/drbd_genl_api.h>
3662306a36Sopenharmony_ci#include <linux/drbd.h>
3762306a36Sopenharmony_ci#include <linux/drbd_config.h>
3862306a36Sopenharmony_ci#include "drbd_strings.h"
3962306a36Sopenharmony_ci#include "drbd_state.h"
4062306a36Sopenharmony_ci#include "drbd_protocol.h"
4162306a36Sopenharmony_ci#include "drbd_polymorph_printk.h"
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/* shared module parameters, defined in drbd_main.c */
4462306a36Sopenharmony_ci#ifdef CONFIG_DRBD_FAULT_INJECTION
4562306a36Sopenharmony_ciextern int drbd_enable_faults;
4662306a36Sopenharmony_ciextern int drbd_fault_rate;
4762306a36Sopenharmony_ci#endif
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciextern unsigned int drbd_minor_count;
5062306a36Sopenharmony_ciextern char drbd_usermode_helper[];
5162306a36Sopenharmony_ciextern int drbd_proc_details;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/* This is used to stop/restart our threads.
5562306a36Sopenharmony_ci * Cannot use SIGTERM nor SIGKILL, since these
5662306a36Sopenharmony_ci * are sent out by init on runlevel changes
5762306a36Sopenharmony_ci * I choose SIGHUP for now.
5862306a36Sopenharmony_ci */
5962306a36Sopenharmony_ci#define DRBD_SIGKILL SIGHUP
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define ID_IN_SYNC      (4711ULL)
6262306a36Sopenharmony_ci#define ID_OUT_OF_SYNC  (4712ULL)
6362306a36Sopenharmony_ci#define ID_SYNCER (-1ULL)
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci#define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistruct drbd_device;
6862306a36Sopenharmony_cistruct drbd_connection;
6962306a36Sopenharmony_cistruct drbd_peer_device;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/* Defines to control fault insertion */
7262306a36Sopenharmony_cienum {
7362306a36Sopenharmony_ci	DRBD_FAULT_MD_WR = 0,	/* meta data write */
7462306a36Sopenharmony_ci	DRBD_FAULT_MD_RD = 1,	/*           read  */
7562306a36Sopenharmony_ci	DRBD_FAULT_RS_WR = 2,	/* resync          */
7662306a36Sopenharmony_ci	DRBD_FAULT_RS_RD = 3,
7762306a36Sopenharmony_ci	DRBD_FAULT_DT_WR = 4,	/* data            */
7862306a36Sopenharmony_ci	DRBD_FAULT_DT_RD = 5,
7962306a36Sopenharmony_ci	DRBD_FAULT_DT_RA = 6,	/* data read ahead */
8062306a36Sopenharmony_ci	DRBD_FAULT_BM_ALLOC = 7,	/* bitmap allocation */
8162306a36Sopenharmony_ci	DRBD_FAULT_AL_EE = 8,	/* alloc ee */
8262306a36Sopenharmony_ci	DRBD_FAULT_RECEIVE = 9, /* Changes some bytes upon receiving a [rs]data block */
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	DRBD_FAULT_MAX,
8562306a36Sopenharmony_ci};
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciextern unsigned int
8862306a36Sopenharmony_ci_drbd_insert_fault(struct drbd_device *device, unsigned int type);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cistatic inline int
9162306a36Sopenharmony_cidrbd_insert_fault(struct drbd_device *device, unsigned int type) {
9262306a36Sopenharmony_ci#ifdef CONFIG_DRBD_FAULT_INJECTION
9362306a36Sopenharmony_ci	return drbd_fault_rate &&
9462306a36Sopenharmony_ci		(drbd_enable_faults & (1<<type)) &&
9562306a36Sopenharmony_ci		_drbd_insert_fault(device, type);
9662306a36Sopenharmony_ci#else
9762306a36Sopenharmony_ci	return 0;
9862306a36Sopenharmony_ci#endif
9962306a36Sopenharmony_ci}
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/* integer division, round _UP_ to the next integer */
10262306a36Sopenharmony_ci#define div_ceil(A, B) ((A)/(B) + ((A)%(B) ? 1 : 0))
10362306a36Sopenharmony_ci/* usual integer division */
10462306a36Sopenharmony_ci#define div_floor(A, B) ((A)/(B))
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciextern struct ratelimit_state drbd_ratelimit_state;
10762306a36Sopenharmony_ciextern struct idr drbd_devices; /* RCU, updates: genl_lock() */
10862306a36Sopenharmony_ciextern struct list_head drbd_resources; /* RCU, updates: genl_lock() */
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciextern const char *cmdname(enum drbd_packet cmd);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci/* for sending/receiving the bitmap,
11362306a36Sopenharmony_ci * possibly in some encoding scheme */
11462306a36Sopenharmony_cistruct bm_xfer_ctx {
11562306a36Sopenharmony_ci	/* "const"
11662306a36Sopenharmony_ci	 * stores total bits and long words
11762306a36Sopenharmony_ci	 * of the bitmap, so we don't need to
11862306a36Sopenharmony_ci	 * call the accessor functions over and again. */
11962306a36Sopenharmony_ci	unsigned long bm_bits;
12062306a36Sopenharmony_ci	unsigned long bm_words;
12162306a36Sopenharmony_ci	/* during xfer, current position within the bitmap */
12262306a36Sopenharmony_ci	unsigned long bit_offset;
12362306a36Sopenharmony_ci	unsigned long word_offset;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	/* statistics; index: (h->command == P_BITMAP) */
12662306a36Sopenharmony_ci	unsigned packets[2];
12762306a36Sopenharmony_ci	unsigned bytes[2];
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ciextern void INFO_bm_xfer_stats(struct drbd_peer_device *peer_device,
13162306a36Sopenharmony_ci			       const char *direction, struct bm_xfer_ctx *c);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic inline void bm_xfer_ctx_bit_to_word_offset(struct bm_xfer_ctx *c)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	/* word_offset counts "native long words" (32 or 64 bit),
13662306a36Sopenharmony_ci	 * aligned at 64 bit.
13762306a36Sopenharmony_ci	 * Encoded packet may end at an unaligned bit offset.
13862306a36Sopenharmony_ci	 * In case a fallback clear text packet is transmitted in
13962306a36Sopenharmony_ci	 * between, we adjust this offset back to the last 64bit
14062306a36Sopenharmony_ci	 * aligned "native long word", which makes coding and decoding
14162306a36Sopenharmony_ci	 * the plain text bitmap much more convenient.  */
14262306a36Sopenharmony_ci#if BITS_PER_LONG == 64
14362306a36Sopenharmony_ci	c->word_offset = c->bit_offset >> 6;
14462306a36Sopenharmony_ci#elif BITS_PER_LONG == 32
14562306a36Sopenharmony_ci	c->word_offset = c->bit_offset >> 5;
14662306a36Sopenharmony_ci	c->word_offset &= ~(1UL);
14762306a36Sopenharmony_ci#else
14862306a36Sopenharmony_ci# error "unsupported BITS_PER_LONG"
14962306a36Sopenharmony_ci#endif
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ciextern unsigned int drbd_header_size(struct drbd_connection *connection);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci/**********************************************************************/
15562306a36Sopenharmony_cienum drbd_thread_state {
15662306a36Sopenharmony_ci	NONE,
15762306a36Sopenharmony_ci	RUNNING,
15862306a36Sopenharmony_ci	EXITING,
15962306a36Sopenharmony_ci	RESTARTING
16062306a36Sopenharmony_ci};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistruct drbd_thread {
16362306a36Sopenharmony_ci	spinlock_t t_lock;
16462306a36Sopenharmony_ci	struct task_struct *task;
16562306a36Sopenharmony_ci	struct completion stop;
16662306a36Sopenharmony_ci	enum drbd_thread_state t_state;
16762306a36Sopenharmony_ci	int (*function) (struct drbd_thread *);
16862306a36Sopenharmony_ci	struct drbd_resource *resource;
16962306a36Sopenharmony_ci	struct drbd_connection *connection;
17062306a36Sopenharmony_ci	int reset_cpu_mask;
17162306a36Sopenharmony_ci	const char *name;
17262306a36Sopenharmony_ci};
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci	/* THINK testing the t_state seems to be uncritical in all cases
17762306a36Sopenharmony_ci	 * (but thread_{start,stop}), so we can read it *without* the lock.
17862306a36Sopenharmony_ci	 *	--lge */
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	smp_rmb();
18162306a36Sopenharmony_ci	return thi->t_state;
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_cistruct drbd_work {
18562306a36Sopenharmony_ci	struct list_head list;
18662306a36Sopenharmony_ci	int (*cb)(struct drbd_work *, int cancel);
18762306a36Sopenharmony_ci};
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_cistruct drbd_device_work {
19062306a36Sopenharmony_ci	struct drbd_work w;
19162306a36Sopenharmony_ci	struct drbd_device *device;
19262306a36Sopenharmony_ci};
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci#include "drbd_interval.h"
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ciextern int drbd_wait_misc(struct drbd_device *, struct drbd_interval *);
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ciextern void lock_all_resources(void);
19962306a36Sopenharmony_ciextern void unlock_all_resources(void);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_cistruct drbd_request {
20262306a36Sopenharmony_ci	struct drbd_work w;
20362306a36Sopenharmony_ci	struct drbd_device *device;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	/* if local IO is not allowed, will be NULL.
20662306a36Sopenharmony_ci	 * if local IO _is_ allowed, holds the locally submitted bio clone,
20762306a36Sopenharmony_ci	 * or, after local IO completion, the ERR_PTR(error).
20862306a36Sopenharmony_ci	 * see drbd_request_endio(). */
20962306a36Sopenharmony_ci	struct bio *private_bio;
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	struct drbd_interval i;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	/* epoch: used to check on "completion" whether this req was in
21462306a36Sopenharmony_ci	 * the current epoch, and we therefore have to close it,
21562306a36Sopenharmony_ci	 * causing a p_barrier packet to be send, starting a new epoch.
21662306a36Sopenharmony_ci	 *
21762306a36Sopenharmony_ci	 * This corresponds to "barrier" in struct p_barrier[_ack],
21862306a36Sopenharmony_ci	 * and to "barrier_nr" in struct drbd_epoch (and various
21962306a36Sopenharmony_ci	 * comments/function parameters/local variable names).
22062306a36Sopenharmony_ci	 */
22162306a36Sopenharmony_ci	unsigned int epoch;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	struct list_head tl_requests; /* ring list in the transfer log */
22462306a36Sopenharmony_ci	struct bio *master_bio;       /* master bio pointer */
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	/* see struct drbd_device */
22762306a36Sopenharmony_ci	struct list_head req_pending_master_completion;
22862306a36Sopenharmony_ci	struct list_head req_pending_local;
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	/* for generic IO accounting */
23162306a36Sopenharmony_ci	unsigned long start_jif;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	/* for DRBD internal statistics */
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	/* Minimal set of time stamps to determine if we wait for activity log
23662306a36Sopenharmony_ci	 * transactions, local disk or peer.  32 bit "jiffies" are good enough,
23762306a36Sopenharmony_ci	 * we don't expect a DRBD request to be stalled for several month.
23862306a36Sopenharmony_ci	 */
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	/* before actual request processing */
24162306a36Sopenharmony_ci	unsigned long in_actlog_jif;
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	/* local disk */
24462306a36Sopenharmony_ci	unsigned long pre_submit_jif;
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	/* per connection */
24762306a36Sopenharmony_ci	unsigned long pre_send_jif;
24862306a36Sopenharmony_ci	unsigned long acked_jif;
24962306a36Sopenharmony_ci	unsigned long net_done_jif;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	/* Possibly even more detail to track each phase:
25262306a36Sopenharmony_ci	 *  master_completion_jif
25362306a36Sopenharmony_ci	 *      how long did it take to complete the master bio
25462306a36Sopenharmony_ci	 *      (application visible latency)
25562306a36Sopenharmony_ci	 *  allocated_jif
25662306a36Sopenharmony_ci	 *      how long the master bio was blocked until we finally allocated
25762306a36Sopenharmony_ci	 *      a tracking struct
25862306a36Sopenharmony_ci	 *  in_actlog_jif
25962306a36Sopenharmony_ci	 *      how long did we wait for activity log transactions
26062306a36Sopenharmony_ci	 *
26162306a36Sopenharmony_ci	 *  net_queued_jif
26262306a36Sopenharmony_ci	 *      when did we finally queue it for sending
26362306a36Sopenharmony_ci	 *  pre_send_jif
26462306a36Sopenharmony_ci	 *      when did we start sending it
26562306a36Sopenharmony_ci	 *  post_send_jif
26662306a36Sopenharmony_ci	 *      how long did we block in the network stack trying to send it
26762306a36Sopenharmony_ci	 *  acked_jif
26862306a36Sopenharmony_ci	 *      when did we receive (or fake, in protocol A) a remote ACK
26962306a36Sopenharmony_ci	 *  net_done_jif
27062306a36Sopenharmony_ci	 *      when did we receive final acknowledgement (P_BARRIER_ACK),
27162306a36Sopenharmony_ci	 *      or decide, e.g. on connection loss, that we do no longer expect
27262306a36Sopenharmony_ci	 *      anything from this peer for this request.
27362306a36Sopenharmony_ci	 *
27462306a36Sopenharmony_ci	 *  pre_submit_jif
27562306a36Sopenharmony_ci	 *  post_sub_jif
27662306a36Sopenharmony_ci	 *      when did we start submiting to the lower level device,
27762306a36Sopenharmony_ci	 *      and how long did we block in that submit function
27862306a36Sopenharmony_ci	 *  local_completion_jif
27962306a36Sopenharmony_ci	 *      how long did it take the lower level device to complete this request
28062306a36Sopenharmony_ci	 */
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	/* once it hits 0, we may complete the master_bio */
28462306a36Sopenharmony_ci	atomic_t completion_ref;
28562306a36Sopenharmony_ci	/* once it hits 0, we may destroy this drbd_request object */
28662306a36Sopenharmony_ci	struct kref kref;
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci	unsigned rq_state; /* see comments above _req_mod() */
28962306a36Sopenharmony_ci};
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_cistruct drbd_epoch {
29262306a36Sopenharmony_ci	struct drbd_connection *connection;
29362306a36Sopenharmony_ci	struct list_head list;
29462306a36Sopenharmony_ci	unsigned int barrier_nr;
29562306a36Sopenharmony_ci	atomic_t epoch_size; /* increased on every request added. */
29662306a36Sopenharmony_ci	atomic_t active;     /* increased on every req. added, and dec on every finished. */
29762306a36Sopenharmony_ci	unsigned long flags;
29862306a36Sopenharmony_ci};
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci/* Prototype declaration of function defined in drbd_receiver.c */
30162306a36Sopenharmony_ciint drbdd_init(struct drbd_thread *);
30262306a36Sopenharmony_ciint drbd_asender(struct drbd_thread *);
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci/* drbd_epoch flag bits */
30562306a36Sopenharmony_cienum {
30662306a36Sopenharmony_ci	DE_HAVE_BARRIER_NUMBER,
30762306a36Sopenharmony_ci};
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_cienum epoch_event {
31062306a36Sopenharmony_ci	EV_PUT,
31162306a36Sopenharmony_ci	EV_GOT_BARRIER_NR,
31262306a36Sopenharmony_ci	EV_BECAME_LAST,
31362306a36Sopenharmony_ci	EV_CLEANUP = 32, /* used as flag */
31462306a36Sopenharmony_ci};
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_cistruct digest_info {
31762306a36Sopenharmony_ci	int digest_size;
31862306a36Sopenharmony_ci	void *digest;
31962306a36Sopenharmony_ci};
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_cistruct drbd_peer_request {
32262306a36Sopenharmony_ci	struct drbd_work w;
32362306a36Sopenharmony_ci	struct drbd_peer_device *peer_device;
32462306a36Sopenharmony_ci	struct drbd_epoch *epoch; /* for writes */
32562306a36Sopenharmony_ci	struct page *pages;
32662306a36Sopenharmony_ci	blk_opf_t opf;
32762306a36Sopenharmony_ci	atomic_t pending_bios;
32862306a36Sopenharmony_ci	struct drbd_interval i;
32962306a36Sopenharmony_ci	/* see comments on ee flag bits below */
33062306a36Sopenharmony_ci	unsigned long flags;
33162306a36Sopenharmony_ci	unsigned long submit_jif;
33262306a36Sopenharmony_ci	union {
33362306a36Sopenharmony_ci		u64 block_id;
33462306a36Sopenharmony_ci		struct digest_info *digest;
33562306a36Sopenharmony_ci	};
33662306a36Sopenharmony_ci};
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci/* Equivalent to bio_op and req_op. */
33962306a36Sopenharmony_ci#define peer_req_op(peer_req) \
34062306a36Sopenharmony_ci	((peer_req)->opf & REQ_OP_MASK)
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci/* ee flag bits.
34362306a36Sopenharmony_ci * While corresponding bios are in flight, the only modification will be
34462306a36Sopenharmony_ci * set_bit WAS_ERROR, which has to be atomic.
34562306a36Sopenharmony_ci * If no bios are in flight yet, or all have been completed,
34662306a36Sopenharmony_ci * non-atomic modification to ee->flags is ok.
34762306a36Sopenharmony_ci */
34862306a36Sopenharmony_cienum {
34962306a36Sopenharmony_ci	__EE_CALL_AL_COMPLETE_IO,
35062306a36Sopenharmony_ci	__EE_MAY_SET_IN_SYNC,
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	/* is this a TRIM aka REQ_OP_DISCARD? */
35362306a36Sopenharmony_ci	__EE_TRIM,
35462306a36Sopenharmony_ci	/* explicit zero-out requested, or
35562306a36Sopenharmony_ci	 * our lower level cannot handle trim,
35662306a36Sopenharmony_ci	 * and we want to fall back to zeroout instead */
35762306a36Sopenharmony_ci	__EE_ZEROOUT,
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci	/* In case a barrier failed,
36062306a36Sopenharmony_ci	 * we need to resubmit without the barrier flag. */
36162306a36Sopenharmony_ci	__EE_RESUBMITTED,
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	/* we may have several bios per peer request.
36462306a36Sopenharmony_ci	 * if any of those fail, we set this flag atomically
36562306a36Sopenharmony_ci	 * from the endio callback */
36662306a36Sopenharmony_ci	__EE_WAS_ERROR,
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	/* This ee has a pointer to a digest instead of a block id */
36962306a36Sopenharmony_ci	__EE_HAS_DIGEST,
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	/* Conflicting local requests need to be restarted after this request */
37262306a36Sopenharmony_ci	__EE_RESTART_REQUESTS,
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci	/* The peer wants a write ACK for this (wire proto C) */
37562306a36Sopenharmony_ci	__EE_SEND_WRITE_ACK,
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	/* Is set when net_conf had two_primaries set while creating this peer_req */
37862306a36Sopenharmony_ci	__EE_IN_INTERVAL_TREE,
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	/* for debugfs: */
38162306a36Sopenharmony_ci	/* has this been submitted, or does it still wait for something else? */
38262306a36Sopenharmony_ci	__EE_SUBMITTED,
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	/* this is/was a write request */
38562306a36Sopenharmony_ci	__EE_WRITE,
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	/* this is/was a write same request */
38862306a36Sopenharmony_ci	__EE_WRITE_SAME,
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	/* this originates from application on peer
39162306a36Sopenharmony_ci	 * (not some resync or verify or other DRBD internal request) */
39262306a36Sopenharmony_ci	__EE_APPLICATION,
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	/* If it contains only 0 bytes, send back P_RS_DEALLOCATED */
39562306a36Sopenharmony_ci	__EE_RS_THIN_REQ,
39662306a36Sopenharmony_ci};
39762306a36Sopenharmony_ci#define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
39862306a36Sopenharmony_ci#define EE_MAY_SET_IN_SYNC     (1<<__EE_MAY_SET_IN_SYNC)
39962306a36Sopenharmony_ci#define EE_TRIM                (1<<__EE_TRIM)
40062306a36Sopenharmony_ci#define EE_ZEROOUT             (1<<__EE_ZEROOUT)
40162306a36Sopenharmony_ci#define EE_RESUBMITTED         (1<<__EE_RESUBMITTED)
40262306a36Sopenharmony_ci#define EE_WAS_ERROR           (1<<__EE_WAS_ERROR)
40362306a36Sopenharmony_ci#define EE_HAS_DIGEST          (1<<__EE_HAS_DIGEST)
40462306a36Sopenharmony_ci#define EE_RESTART_REQUESTS	(1<<__EE_RESTART_REQUESTS)
40562306a36Sopenharmony_ci#define EE_SEND_WRITE_ACK	(1<<__EE_SEND_WRITE_ACK)
40662306a36Sopenharmony_ci#define EE_IN_INTERVAL_TREE	(1<<__EE_IN_INTERVAL_TREE)
40762306a36Sopenharmony_ci#define EE_SUBMITTED		(1<<__EE_SUBMITTED)
40862306a36Sopenharmony_ci#define EE_WRITE		(1<<__EE_WRITE)
40962306a36Sopenharmony_ci#define EE_WRITE_SAME		(1<<__EE_WRITE_SAME)
41062306a36Sopenharmony_ci#define EE_APPLICATION		(1<<__EE_APPLICATION)
41162306a36Sopenharmony_ci#define EE_RS_THIN_REQ		(1<<__EE_RS_THIN_REQ)
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci/* flag bits per device */
41462306a36Sopenharmony_cienum {
41562306a36Sopenharmony_ci	UNPLUG_REMOTE,		/* sending a "UnplugRemote" could help */
41662306a36Sopenharmony_ci	MD_DIRTY,		/* current uuids and flags not yet on disk */
41762306a36Sopenharmony_ci	USE_DEGR_WFC_T,		/* degr-wfc-timeout instead of wfc-timeout. */
41862306a36Sopenharmony_ci	CL_ST_CHG_SUCCESS,
41962306a36Sopenharmony_ci	CL_ST_CHG_FAIL,
42062306a36Sopenharmony_ci	CRASHED_PRIMARY,	/* This node was a crashed primary.
42162306a36Sopenharmony_ci				 * Gets cleared when the state.conn
42262306a36Sopenharmony_ci				 * goes into C_CONNECTED state. */
42362306a36Sopenharmony_ci	CONSIDER_RESYNC,
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	MD_NO_FUA,		/* Users wants us to not use FUA/FLUSH on meta data dev */
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci	BITMAP_IO,		/* suspend application io;
42862306a36Sopenharmony_ci				   once no more io in flight, start bitmap io */
42962306a36Sopenharmony_ci	BITMAP_IO_QUEUED,       /* Started bitmap IO */
43062306a36Sopenharmony_ci	WAS_IO_ERROR,		/* Local disk failed, returned IO error */
43162306a36Sopenharmony_ci	WAS_READ_ERROR,		/* Local disk READ failed (set additionally to the above) */
43262306a36Sopenharmony_ci	FORCE_DETACH,		/* Force-detach from local disk, aborting any pending local IO */
43362306a36Sopenharmony_ci	RESYNC_AFTER_NEG,       /* Resync after online grow after the attach&negotiate finished. */
43462306a36Sopenharmony_ci	RESIZE_PENDING,		/* Size change detected locally, waiting for the response from
43562306a36Sopenharmony_ci				 * the peer, if it changed there as well. */
43662306a36Sopenharmony_ci	NEW_CUR_UUID,		/* Create new current UUID when thawing IO */
43762306a36Sopenharmony_ci	AL_SUSPENDED,		/* Activity logging is currently suspended. */
43862306a36Sopenharmony_ci	AHEAD_TO_SYNC_SOURCE,   /* Ahead -> SyncSource queued */
43962306a36Sopenharmony_ci	B_RS_H_DONE,		/* Before resync handler done (already executed) */
44062306a36Sopenharmony_ci	DISCARD_MY_DATA,	/* discard_my_data flag per volume */
44162306a36Sopenharmony_ci	READ_BALANCE_RR,
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	FLUSH_PENDING,		/* if set, device->flush_jif is when we submitted that flush
44462306a36Sopenharmony_ci				 * from drbd_flush_after_epoch() */
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	/* cleared only after backing device related structures have been destroyed. */
44762306a36Sopenharmony_ci	GOING_DISKLESS,		/* Disk is being detached, because of io-error, or admin request. */
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	/* to be used in drbd_device_post_work() */
45062306a36Sopenharmony_ci	GO_DISKLESS,		/* tell worker to schedule cleanup before detach */
45162306a36Sopenharmony_ci	DESTROY_DISK,		/* tell worker to close backing devices and destroy related structures. */
45262306a36Sopenharmony_ci	MD_SYNC,		/* tell worker to call drbd_md_sync() */
45362306a36Sopenharmony_ci	RS_START,		/* tell worker to start resync/OV */
45462306a36Sopenharmony_ci	RS_PROGRESS,		/* tell worker that resync made significant progress */
45562306a36Sopenharmony_ci	RS_DONE,		/* tell worker that resync is done */
45662306a36Sopenharmony_ci};
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_cistruct drbd_bitmap; /* opaque for drbd_device */
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci/* definition of bits in bm_flags to be used in drbd_bm_lock
46162306a36Sopenharmony_ci * and drbd_bitmap_io and friends. */
46262306a36Sopenharmony_cienum bm_flag {
46362306a36Sopenharmony_ci	/* currently locked for bulk operation */
46462306a36Sopenharmony_ci	BM_LOCKED_MASK = 0xf,
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	/* in detail, that is: */
46762306a36Sopenharmony_ci	BM_DONT_CLEAR = 0x1,
46862306a36Sopenharmony_ci	BM_DONT_SET   = 0x2,
46962306a36Sopenharmony_ci	BM_DONT_TEST  = 0x4,
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci	/* so we can mark it locked for bulk operation,
47262306a36Sopenharmony_ci	 * and still allow all non-bulk operations */
47362306a36Sopenharmony_ci	BM_IS_LOCKED  = 0x8,
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	/* (test bit, count bit) allowed (common case) */
47662306a36Sopenharmony_ci	BM_LOCKED_TEST_ALLOWED = BM_DONT_CLEAR | BM_DONT_SET | BM_IS_LOCKED,
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci	/* testing bits, as well as setting new bits allowed, but clearing bits
47962306a36Sopenharmony_ci	 * would be unexpected.  Used during bitmap receive.  Setting new bits
48062306a36Sopenharmony_ci	 * requires sending of "out-of-sync" information, though. */
48162306a36Sopenharmony_ci	BM_LOCKED_SET_ALLOWED = BM_DONT_CLEAR | BM_IS_LOCKED,
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci	/* for drbd_bm_write_copy_pages, everything is allowed,
48462306a36Sopenharmony_ci	 * only concurrent bulk operations are locked out. */
48562306a36Sopenharmony_ci	BM_LOCKED_CHANGE_ALLOWED = BM_IS_LOCKED,
48662306a36Sopenharmony_ci};
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_cistruct drbd_work_queue {
48962306a36Sopenharmony_ci	struct list_head q;
49062306a36Sopenharmony_ci	spinlock_t q_lock;  /* to protect the list. */
49162306a36Sopenharmony_ci	wait_queue_head_t q_wait;
49262306a36Sopenharmony_ci};
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_cistruct drbd_socket {
49562306a36Sopenharmony_ci	struct mutex mutex;
49662306a36Sopenharmony_ci	struct socket    *socket;
49762306a36Sopenharmony_ci	/* this way we get our
49862306a36Sopenharmony_ci	 * send/receive buffers off the stack */
49962306a36Sopenharmony_ci	void *sbuf;
50062306a36Sopenharmony_ci	void *rbuf;
50162306a36Sopenharmony_ci};
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_cistruct drbd_md {
50462306a36Sopenharmony_ci	u64 md_offset;		/* sector offset to 'super' block */
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci	u64 la_size_sect;	/* last agreed size, unit sectors */
50762306a36Sopenharmony_ci	spinlock_t uuid_lock;
50862306a36Sopenharmony_ci	u64 uuid[UI_SIZE];
50962306a36Sopenharmony_ci	u64 device_uuid;
51062306a36Sopenharmony_ci	u32 flags;
51162306a36Sopenharmony_ci	u32 md_size_sect;
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci	s32 al_offset;	/* signed relative sector offset to activity log */
51462306a36Sopenharmony_ci	s32 bm_offset;	/* signed relative sector offset to bitmap */
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ci	/* cached value of bdev->disk_conf->meta_dev_idx (see below) */
51762306a36Sopenharmony_ci	s32 meta_dev_idx;
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	/* see al_tr_number_to_on_disk_sector() */
52062306a36Sopenharmony_ci	u32 al_stripes;
52162306a36Sopenharmony_ci	u32 al_stripe_size_4k;
52262306a36Sopenharmony_ci	u32 al_size_4k; /* cached product of the above */
52362306a36Sopenharmony_ci};
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_cistruct drbd_backing_dev {
52662306a36Sopenharmony_ci	struct block_device *backing_bdev;
52762306a36Sopenharmony_ci	struct block_device *md_bdev;
52862306a36Sopenharmony_ci	struct drbd_md md;
52962306a36Sopenharmony_ci	struct disk_conf *disk_conf; /* RCU, for updates: resource->conf_update */
53062306a36Sopenharmony_ci	sector_t known_size; /* last known size of that backing device */
53162306a36Sopenharmony_ci};
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_cistruct drbd_md_io {
53462306a36Sopenharmony_ci	struct page *page;
53562306a36Sopenharmony_ci	unsigned long start_jif;	/* last call to drbd_md_get_buffer */
53662306a36Sopenharmony_ci	unsigned long submit_jif;	/* last _drbd_md_sync_page_io() submit */
53762306a36Sopenharmony_ci	const char *current_use;
53862306a36Sopenharmony_ci	atomic_t in_use;
53962306a36Sopenharmony_ci	unsigned int done;
54062306a36Sopenharmony_ci	int error;
54162306a36Sopenharmony_ci};
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_cistruct bm_io_work {
54462306a36Sopenharmony_ci	struct drbd_work w;
54562306a36Sopenharmony_ci	struct drbd_peer_device *peer_device;
54662306a36Sopenharmony_ci	char *why;
54762306a36Sopenharmony_ci	enum bm_flag flags;
54862306a36Sopenharmony_ci	int (*io_fn)(struct drbd_device *device, struct drbd_peer_device *peer_device);
54962306a36Sopenharmony_ci	void (*done)(struct drbd_device *device, int rv);
55062306a36Sopenharmony_ci};
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_cistruct fifo_buffer {
55362306a36Sopenharmony_ci	unsigned int head_index;
55462306a36Sopenharmony_ci	unsigned int size;
55562306a36Sopenharmony_ci	int total; /* sum of all values */
55662306a36Sopenharmony_ci	int values[];
55762306a36Sopenharmony_ci};
55862306a36Sopenharmony_ciextern struct fifo_buffer *fifo_alloc(unsigned int fifo_size);
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci/* flag bits per connection */
56162306a36Sopenharmony_cienum {
56262306a36Sopenharmony_ci	NET_CONGESTED,		/* The data socket is congested */
56362306a36Sopenharmony_ci	RESOLVE_CONFLICTS,	/* Set on one node, cleared on the peer! */
56462306a36Sopenharmony_ci	SEND_PING,
56562306a36Sopenharmony_ci	GOT_PING_ACK,		/* set when we receive a ping_ack packet, ping_wait gets woken */
56662306a36Sopenharmony_ci	CONN_WD_ST_CHG_REQ,	/* A cluster wide state change on the connection is active */
56762306a36Sopenharmony_ci	CONN_WD_ST_CHG_OKAY,
56862306a36Sopenharmony_ci	CONN_WD_ST_CHG_FAIL,
56962306a36Sopenharmony_ci	CONN_DRY_RUN,		/* Expect disconnect after resync handshake. */
57062306a36Sopenharmony_ci	CREATE_BARRIER,		/* next P_DATA is preceded by a P_BARRIER */
57162306a36Sopenharmony_ci	STATE_SENT,		/* Do not change state/UUIDs while this is set */
57262306a36Sopenharmony_ci	CALLBACK_PENDING,	/* Whether we have a call_usermodehelper(, UMH_WAIT_PROC)
57362306a36Sopenharmony_ci				 * pending, from drbd worker context.
57462306a36Sopenharmony_ci				 */
57562306a36Sopenharmony_ci	DISCONNECT_SENT,
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_ci	DEVICE_WORK_PENDING,	/* tell worker that some device has pending work */
57862306a36Sopenharmony_ci};
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_cienum which_state { NOW, OLD = NOW, NEW };
58162306a36Sopenharmony_ci
58262306a36Sopenharmony_cistruct drbd_resource {
58362306a36Sopenharmony_ci	char *name;
58462306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
58562306a36Sopenharmony_ci	struct dentry *debugfs_res;
58662306a36Sopenharmony_ci	struct dentry *debugfs_res_volumes;
58762306a36Sopenharmony_ci	struct dentry *debugfs_res_connections;
58862306a36Sopenharmony_ci	struct dentry *debugfs_res_in_flight_summary;
58962306a36Sopenharmony_ci#endif
59062306a36Sopenharmony_ci	struct kref kref;
59162306a36Sopenharmony_ci	struct idr devices;		/* volume number to device mapping */
59262306a36Sopenharmony_ci	struct list_head connections;
59362306a36Sopenharmony_ci	struct list_head resources;
59462306a36Sopenharmony_ci	struct res_opts res_opts;
59562306a36Sopenharmony_ci	struct mutex conf_update;	/* mutex for ready-copy-update of net_conf and disk_conf */
59662306a36Sopenharmony_ci	struct mutex adm_mutex;		/* mutex to serialize administrative requests */
59762306a36Sopenharmony_ci	spinlock_t req_lock;
59862306a36Sopenharmony_ci
59962306a36Sopenharmony_ci	unsigned susp:1;		/* IO suspended by user */
60062306a36Sopenharmony_ci	unsigned susp_nod:1;		/* IO suspended because no data */
60162306a36Sopenharmony_ci	unsigned susp_fen:1;		/* IO suspended because fence peer handler runs */
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci	enum write_ordering_e write_ordering;
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	cpumask_var_t cpu_mask;
60662306a36Sopenharmony_ci};
60762306a36Sopenharmony_ci
60862306a36Sopenharmony_cistruct drbd_thread_timing_details
60962306a36Sopenharmony_ci{
61062306a36Sopenharmony_ci	unsigned long start_jif;
61162306a36Sopenharmony_ci	void *cb_addr;
61262306a36Sopenharmony_ci	const char *caller_fn;
61362306a36Sopenharmony_ci	unsigned int line;
61462306a36Sopenharmony_ci	unsigned int cb_nr;
61562306a36Sopenharmony_ci};
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_cistruct drbd_connection {
61862306a36Sopenharmony_ci	struct list_head connections;
61962306a36Sopenharmony_ci	struct drbd_resource *resource;
62062306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
62162306a36Sopenharmony_ci	struct dentry *debugfs_conn;
62262306a36Sopenharmony_ci	struct dentry *debugfs_conn_callback_history;
62362306a36Sopenharmony_ci	struct dentry *debugfs_conn_oldest_requests;
62462306a36Sopenharmony_ci#endif
62562306a36Sopenharmony_ci	struct kref kref;
62662306a36Sopenharmony_ci	struct idr peer_devices;	/* volume number to peer device mapping */
62762306a36Sopenharmony_ci	enum drbd_conns cstate;		/* Only C_STANDALONE to C_WF_REPORT_PARAMS */
62862306a36Sopenharmony_ci	struct mutex cstate_mutex;	/* Protects graceful disconnects */
62962306a36Sopenharmony_ci	unsigned int connect_cnt;	/* Inc each time a connection is established */
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci	unsigned long flags;
63262306a36Sopenharmony_ci	struct net_conf *net_conf;	/* content protected by rcu */
63362306a36Sopenharmony_ci	wait_queue_head_t ping_wait;	/* Woken upon reception of a ping, and a state change */
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ci	struct sockaddr_storage my_addr;
63662306a36Sopenharmony_ci	int my_addr_len;
63762306a36Sopenharmony_ci	struct sockaddr_storage peer_addr;
63862306a36Sopenharmony_ci	int peer_addr_len;
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci	struct drbd_socket data;	/* data/barrier/cstate/parameter packets */
64162306a36Sopenharmony_ci	struct drbd_socket meta;	/* ping/ack (metadata) packets */
64262306a36Sopenharmony_ci	int agreed_pro_version;		/* actually used protocol version */
64362306a36Sopenharmony_ci	u32 agreed_features;
64462306a36Sopenharmony_ci	unsigned long last_received;	/* in jiffies, either socket */
64562306a36Sopenharmony_ci	unsigned int ko_count;
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ci	struct list_head transfer_log;	/* all requests not yet fully processed */
64862306a36Sopenharmony_ci
64962306a36Sopenharmony_ci	struct crypto_shash *cram_hmac_tfm;
65062306a36Sopenharmony_ci	struct crypto_shash *integrity_tfm;  /* checksums we compute, updates protected by connection->data->mutex */
65162306a36Sopenharmony_ci	struct crypto_shash *peer_integrity_tfm;  /* checksums we verify, only accessed from receiver thread  */
65262306a36Sopenharmony_ci	struct crypto_shash *csums_tfm;
65362306a36Sopenharmony_ci	struct crypto_shash *verify_tfm;
65462306a36Sopenharmony_ci	void *int_dig_in;
65562306a36Sopenharmony_ci	void *int_dig_vv;
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci	/* receiver side */
65862306a36Sopenharmony_ci	struct drbd_epoch *current_epoch;
65962306a36Sopenharmony_ci	spinlock_t epoch_lock;
66062306a36Sopenharmony_ci	unsigned int epochs;
66162306a36Sopenharmony_ci	atomic_t current_tle_nr;	/* transfer log epoch number */
66262306a36Sopenharmony_ci	unsigned current_tle_writes;	/* writes seen within this tl epoch */
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_ci	unsigned long last_reconnect_jif;
66562306a36Sopenharmony_ci	/* empty member on older kernels without blk_start_plug() */
66662306a36Sopenharmony_ci	struct blk_plug receiver_plug;
66762306a36Sopenharmony_ci	struct drbd_thread receiver;
66862306a36Sopenharmony_ci	struct drbd_thread worker;
66962306a36Sopenharmony_ci	struct drbd_thread ack_receiver;
67062306a36Sopenharmony_ci	struct workqueue_struct *ack_sender;
67162306a36Sopenharmony_ci
67262306a36Sopenharmony_ci	/* cached pointers,
67362306a36Sopenharmony_ci	 * so we can look up the oldest pending requests more quickly.
67462306a36Sopenharmony_ci	 * protected by resource->req_lock */
67562306a36Sopenharmony_ci	struct drbd_request *req_next; /* DRBD 9: todo.req_next */
67662306a36Sopenharmony_ci	struct drbd_request *req_ack_pending;
67762306a36Sopenharmony_ci	struct drbd_request *req_not_net_done;
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci	/* sender side */
68062306a36Sopenharmony_ci	struct drbd_work_queue sender_work;
68162306a36Sopenharmony_ci
68262306a36Sopenharmony_ci#define DRBD_THREAD_DETAILS_HIST	16
68362306a36Sopenharmony_ci	unsigned int w_cb_nr; /* keeps counting up */
68462306a36Sopenharmony_ci	unsigned int r_cb_nr; /* keeps counting up */
68562306a36Sopenharmony_ci	struct drbd_thread_timing_details w_timing_details[DRBD_THREAD_DETAILS_HIST];
68662306a36Sopenharmony_ci	struct drbd_thread_timing_details r_timing_details[DRBD_THREAD_DETAILS_HIST];
68762306a36Sopenharmony_ci
68862306a36Sopenharmony_ci	struct {
68962306a36Sopenharmony_ci		unsigned long last_sent_barrier_jif;
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ci		/* whether this sender thread
69262306a36Sopenharmony_ci		 * has processed a single write yet. */
69362306a36Sopenharmony_ci		bool seen_any_write_yet;
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci		/* Which barrier number to send with the next P_BARRIER */
69662306a36Sopenharmony_ci		int current_epoch_nr;
69762306a36Sopenharmony_ci
69862306a36Sopenharmony_ci		/* how many write requests have been sent
69962306a36Sopenharmony_ci		 * with req->epoch == current_epoch_nr.
70062306a36Sopenharmony_ci		 * If none, no P_BARRIER will be sent. */
70162306a36Sopenharmony_ci		unsigned current_epoch_writes;
70262306a36Sopenharmony_ci	} send;
70362306a36Sopenharmony_ci};
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_cistatic inline bool has_net_conf(struct drbd_connection *connection)
70662306a36Sopenharmony_ci{
70762306a36Sopenharmony_ci	bool has_net_conf;
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	rcu_read_lock();
71062306a36Sopenharmony_ci	has_net_conf = rcu_dereference(connection->net_conf);
71162306a36Sopenharmony_ci	rcu_read_unlock();
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ci	return has_net_conf;
71462306a36Sopenharmony_ci}
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_civoid __update_timing_details(
71762306a36Sopenharmony_ci		struct drbd_thread_timing_details *tdp,
71862306a36Sopenharmony_ci		unsigned int *cb_nr,
71962306a36Sopenharmony_ci		void *cb,
72062306a36Sopenharmony_ci		const char *fn, const unsigned int line);
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci#define update_worker_timing_details(c, cb) \
72362306a36Sopenharmony_ci	__update_timing_details(c->w_timing_details, &c->w_cb_nr, cb, __func__ , __LINE__ )
72462306a36Sopenharmony_ci#define update_receiver_timing_details(c, cb) \
72562306a36Sopenharmony_ci	__update_timing_details(c->r_timing_details, &c->r_cb_nr, cb, __func__ , __LINE__ )
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_cistruct submit_worker {
72862306a36Sopenharmony_ci	struct workqueue_struct *wq;
72962306a36Sopenharmony_ci	struct work_struct worker;
73062306a36Sopenharmony_ci
73162306a36Sopenharmony_ci	/* protected by ..->resource->req_lock */
73262306a36Sopenharmony_ci	struct list_head writes;
73362306a36Sopenharmony_ci};
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_cistruct drbd_peer_device {
73662306a36Sopenharmony_ci	struct list_head peer_devices;
73762306a36Sopenharmony_ci	struct drbd_device *device;
73862306a36Sopenharmony_ci	struct drbd_connection *connection;
73962306a36Sopenharmony_ci	struct work_struct send_acks_work;
74062306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
74162306a36Sopenharmony_ci	struct dentry *debugfs_peer_dev;
74262306a36Sopenharmony_ci#endif
74362306a36Sopenharmony_ci};
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_cistruct drbd_device {
74662306a36Sopenharmony_ci	struct drbd_resource *resource;
74762306a36Sopenharmony_ci	struct list_head peer_devices;
74862306a36Sopenharmony_ci	struct list_head pending_bitmap_io;
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ci	unsigned long flush_jif;
75162306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
75262306a36Sopenharmony_ci	struct dentry *debugfs_minor;
75362306a36Sopenharmony_ci	struct dentry *debugfs_vol;
75462306a36Sopenharmony_ci	struct dentry *debugfs_vol_oldest_requests;
75562306a36Sopenharmony_ci	struct dentry *debugfs_vol_act_log_extents;
75662306a36Sopenharmony_ci	struct dentry *debugfs_vol_resync_extents;
75762306a36Sopenharmony_ci	struct dentry *debugfs_vol_data_gen_id;
75862306a36Sopenharmony_ci	struct dentry *debugfs_vol_ed_gen_id;
75962306a36Sopenharmony_ci#endif
76062306a36Sopenharmony_ci
76162306a36Sopenharmony_ci	unsigned int vnr;	/* volume number within the connection */
76262306a36Sopenharmony_ci	unsigned int minor;	/* device minor number */
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci	struct kref kref;
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_ci	/* things that are stored as / read from meta data on disk */
76762306a36Sopenharmony_ci	unsigned long flags;
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci	/* configured by drbdsetup */
77062306a36Sopenharmony_ci	struct drbd_backing_dev *ldev;
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ci	sector_t p_size;     /* partner's disk size */
77362306a36Sopenharmony_ci	struct request_queue *rq_queue;
77462306a36Sopenharmony_ci	struct gendisk	    *vdisk;
77562306a36Sopenharmony_ci
77662306a36Sopenharmony_ci	unsigned long last_reattach_jif;
77762306a36Sopenharmony_ci	struct drbd_work resync_work;
77862306a36Sopenharmony_ci	struct drbd_work unplug_work;
77962306a36Sopenharmony_ci	struct timer_list resync_timer;
78062306a36Sopenharmony_ci	struct timer_list md_sync_timer;
78162306a36Sopenharmony_ci	struct timer_list start_resync_timer;
78262306a36Sopenharmony_ci	struct timer_list request_timer;
78362306a36Sopenharmony_ci
78462306a36Sopenharmony_ci	/* Used after attach while negotiating new disk state. */
78562306a36Sopenharmony_ci	union drbd_state new_state_tmp;
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci	union drbd_dev_state state;
78862306a36Sopenharmony_ci	wait_queue_head_t misc_wait;
78962306a36Sopenharmony_ci	wait_queue_head_t state_wait;  /* upon each state change. */
79062306a36Sopenharmony_ci	unsigned int send_cnt;
79162306a36Sopenharmony_ci	unsigned int recv_cnt;
79262306a36Sopenharmony_ci	unsigned int read_cnt;
79362306a36Sopenharmony_ci	unsigned int writ_cnt;
79462306a36Sopenharmony_ci	unsigned int al_writ_cnt;
79562306a36Sopenharmony_ci	unsigned int bm_writ_cnt;
79662306a36Sopenharmony_ci	atomic_t ap_bio_cnt;	 /* Requests we need to complete */
79762306a36Sopenharmony_ci	atomic_t ap_actlog_cnt;  /* Requests waiting for activity log */
79862306a36Sopenharmony_ci	atomic_t ap_pending_cnt; /* AP data packets on the wire, ack expected */
79962306a36Sopenharmony_ci	atomic_t rs_pending_cnt; /* RS request/data packets on the wire */
80062306a36Sopenharmony_ci	atomic_t unacked_cnt;	 /* Need to send replies for */
80162306a36Sopenharmony_ci	atomic_t local_cnt;	 /* Waiting for local completion */
80262306a36Sopenharmony_ci	atomic_t suspend_cnt;
80362306a36Sopenharmony_ci
80462306a36Sopenharmony_ci	/* Interval tree of pending local requests */
80562306a36Sopenharmony_ci	struct rb_root read_requests;
80662306a36Sopenharmony_ci	struct rb_root write_requests;
80762306a36Sopenharmony_ci
80862306a36Sopenharmony_ci	/* for statistics and timeouts */
80962306a36Sopenharmony_ci	/* [0] read, [1] write */
81062306a36Sopenharmony_ci	struct list_head pending_master_completion[2];
81162306a36Sopenharmony_ci	struct list_head pending_completion[2];
81262306a36Sopenharmony_ci
81362306a36Sopenharmony_ci	/* use checksums for *this* resync */
81462306a36Sopenharmony_ci	bool use_csums;
81562306a36Sopenharmony_ci	/* blocks to resync in this run [unit BM_BLOCK_SIZE] */
81662306a36Sopenharmony_ci	unsigned long rs_total;
81762306a36Sopenharmony_ci	/* number of resync blocks that failed in this run */
81862306a36Sopenharmony_ci	unsigned long rs_failed;
81962306a36Sopenharmony_ci	/* Syncer's start time [unit jiffies] */
82062306a36Sopenharmony_ci	unsigned long rs_start;
82162306a36Sopenharmony_ci	/* cumulated time in PausedSyncX state [unit jiffies] */
82262306a36Sopenharmony_ci	unsigned long rs_paused;
82362306a36Sopenharmony_ci	/* skipped because csum was equal [unit BM_BLOCK_SIZE] */
82462306a36Sopenharmony_ci	unsigned long rs_same_csum;
82562306a36Sopenharmony_ci#define DRBD_SYNC_MARKS 8
82662306a36Sopenharmony_ci#define DRBD_SYNC_MARK_STEP (3*HZ)
82762306a36Sopenharmony_ci	/* block not up-to-date at mark [unit BM_BLOCK_SIZE] */
82862306a36Sopenharmony_ci	unsigned long rs_mark_left[DRBD_SYNC_MARKS];
82962306a36Sopenharmony_ci	/* marks's time [unit jiffies] */
83062306a36Sopenharmony_ci	unsigned long rs_mark_time[DRBD_SYNC_MARKS];
83162306a36Sopenharmony_ci	/* current index into rs_mark_{left,time} */
83262306a36Sopenharmony_ci	int rs_last_mark;
83362306a36Sopenharmony_ci	unsigned long rs_last_bcast; /* [unit jiffies] */
83462306a36Sopenharmony_ci
83562306a36Sopenharmony_ci	/* where does the admin want us to start? (sector) */
83662306a36Sopenharmony_ci	sector_t ov_start_sector;
83762306a36Sopenharmony_ci	sector_t ov_stop_sector;
83862306a36Sopenharmony_ci	/* where are we now? (sector) */
83962306a36Sopenharmony_ci	sector_t ov_position;
84062306a36Sopenharmony_ci	/* Start sector of out of sync range (to merge printk reporting). */
84162306a36Sopenharmony_ci	sector_t ov_last_oos_start;
84262306a36Sopenharmony_ci	/* size of out-of-sync range in sectors. */
84362306a36Sopenharmony_ci	sector_t ov_last_oos_size;
84462306a36Sopenharmony_ci	unsigned long ov_left; /* in bits */
84562306a36Sopenharmony_ci
84662306a36Sopenharmony_ci	struct drbd_bitmap *bitmap;
84762306a36Sopenharmony_ci	unsigned long bm_resync_fo; /* bit offset for drbd_bm_find_next */
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_ci	/* Used to track operations of resync... */
85062306a36Sopenharmony_ci	struct lru_cache *resync;
85162306a36Sopenharmony_ci	/* Number of locked elements in resync LRU */
85262306a36Sopenharmony_ci	unsigned int resync_locked;
85362306a36Sopenharmony_ci	/* resync extent number waiting for application requests */
85462306a36Sopenharmony_ci	unsigned int resync_wenr;
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ci	int open_cnt;
85762306a36Sopenharmony_ci	u64 *p_uuid;
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_ci	struct list_head active_ee; /* IO in progress (P_DATA gets written to disk) */
86062306a36Sopenharmony_ci	struct list_head sync_ee;   /* IO in progress (P_RS_DATA_REPLY gets written to disk) */
86162306a36Sopenharmony_ci	struct list_head done_ee;   /* need to send P_WRITE_ACK */
86262306a36Sopenharmony_ci	struct list_head read_ee;   /* [RS]P_DATA_REQUEST being read */
86362306a36Sopenharmony_ci	struct list_head net_ee;    /* zero-copy network send in progress */
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_ci	int next_barrier_nr;
86662306a36Sopenharmony_ci	struct list_head resync_reads;
86762306a36Sopenharmony_ci	atomic_t pp_in_use;		/* allocated from page pool */
86862306a36Sopenharmony_ci	atomic_t pp_in_use_by_net;	/* sendpage()d, still referenced by tcp */
86962306a36Sopenharmony_ci	wait_queue_head_t ee_wait;
87062306a36Sopenharmony_ci	struct drbd_md_io md_io;
87162306a36Sopenharmony_ci	spinlock_t al_lock;
87262306a36Sopenharmony_ci	wait_queue_head_t al_wait;
87362306a36Sopenharmony_ci	struct lru_cache *act_log;	/* activity log */
87462306a36Sopenharmony_ci	unsigned int al_tr_number;
87562306a36Sopenharmony_ci	int al_tr_cycle;
87662306a36Sopenharmony_ci	wait_queue_head_t seq_wait;
87762306a36Sopenharmony_ci	atomic_t packet_seq;
87862306a36Sopenharmony_ci	unsigned int peer_seq;
87962306a36Sopenharmony_ci	spinlock_t peer_seq_lock;
88062306a36Sopenharmony_ci	unsigned long comm_bm_set; /* communicated number of set bits. */
88162306a36Sopenharmony_ci	struct bm_io_work bm_io_work;
88262306a36Sopenharmony_ci	u64 ed_uuid; /* UUID of the exposed data */
88362306a36Sopenharmony_ci	struct mutex own_state_mutex;
88462306a36Sopenharmony_ci	struct mutex *state_mutex; /* either own_state_mutex or first_peer_device(device)->connection->cstate_mutex */
88562306a36Sopenharmony_ci	char congestion_reason;  /* Why we where congested... */
88662306a36Sopenharmony_ci	atomic_t rs_sect_in; /* for incoming resync data rate, SyncTarget */
88762306a36Sopenharmony_ci	atomic_t rs_sect_ev; /* for submitted resync data rate, both */
88862306a36Sopenharmony_ci	int rs_last_sect_ev; /* counter to compare with */
88962306a36Sopenharmony_ci	int rs_last_events;  /* counter of read or write "events" (unit sectors)
89062306a36Sopenharmony_ci			      * on the lower level device when we last looked. */
89162306a36Sopenharmony_ci	int c_sync_rate; /* current resync rate after syncer throttle magic */
89262306a36Sopenharmony_ci	struct fifo_buffer *rs_plan_s; /* correction values of resync planer (RCU, connection->conn_update) */
89362306a36Sopenharmony_ci	int rs_in_flight; /* resync sectors in flight (to proxy, in proxy and from proxy) */
89462306a36Sopenharmony_ci	atomic_t ap_in_flight; /* App sectors in flight (waiting for ack) */
89562306a36Sopenharmony_ci	unsigned int peer_max_bio_size;
89662306a36Sopenharmony_ci	unsigned int local_max_bio_size;
89762306a36Sopenharmony_ci
89862306a36Sopenharmony_ci	/* any requests that would block in drbd_make_request()
89962306a36Sopenharmony_ci	 * are deferred to this single-threaded work queue */
90062306a36Sopenharmony_ci	struct submit_worker submit;
90162306a36Sopenharmony_ci};
90262306a36Sopenharmony_ci
90362306a36Sopenharmony_cistruct drbd_bm_aio_ctx {
90462306a36Sopenharmony_ci	struct drbd_device *device;
90562306a36Sopenharmony_ci	struct list_head list; /* on device->pending_bitmap_io */;
90662306a36Sopenharmony_ci	unsigned long start_jif;
90762306a36Sopenharmony_ci	atomic_t in_flight;
90862306a36Sopenharmony_ci	unsigned int done;
90962306a36Sopenharmony_ci	unsigned flags;
91062306a36Sopenharmony_ci#define BM_AIO_COPY_PAGES	1
91162306a36Sopenharmony_ci#define BM_AIO_WRITE_HINTED	2
91262306a36Sopenharmony_ci#define BM_AIO_WRITE_ALL_PAGES	4
91362306a36Sopenharmony_ci#define BM_AIO_READ		8
91462306a36Sopenharmony_ci	int error;
91562306a36Sopenharmony_ci	struct kref kref;
91662306a36Sopenharmony_ci};
91762306a36Sopenharmony_ci
91862306a36Sopenharmony_cistruct drbd_config_context {
91962306a36Sopenharmony_ci	/* assigned from drbd_genlmsghdr */
92062306a36Sopenharmony_ci	unsigned int minor;
92162306a36Sopenharmony_ci	/* assigned from request attributes, if present */
92262306a36Sopenharmony_ci	unsigned int volume;
92362306a36Sopenharmony_ci#define VOLUME_UNSPECIFIED		(-1U)
92462306a36Sopenharmony_ci	/* pointer into the request skb,
92562306a36Sopenharmony_ci	 * limited lifetime! */
92662306a36Sopenharmony_ci	char *resource_name;
92762306a36Sopenharmony_ci	struct nlattr *my_addr;
92862306a36Sopenharmony_ci	struct nlattr *peer_addr;
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci	/* reply buffer */
93162306a36Sopenharmony_ci	struct sk_buff *reply_skb;
93262306a36Sopenharmony_ci	/* pointer into reply buffer */
93362306a36Sopenharmony_ci	struct drbd_genlmsghdr *reply_dh;
93462306a36Sopenharmony_ci	/* resolved from attributes, if possible */
93562306a36Sopenharmony_ci	struct drbd_device *device;
93662306a36Sopenharmony_ci	struct drbd_resource *resource;
93762306a36Sopenharmony_ci	struct drbd_connection *connection;
93862306a36Sopenharmony_ci};
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_cistatic inline struct drbd_device *minor_to_device(unsigned int minor)
94162306a36Sopenharmony_ci{
94262306a36Sopenharmony_ci	return (struct drbd_device *)idr_find(&drbd_devices, minor);
94362306a36Sopenharmony_ci}
94462306a36Sopenharmony_ci
94562306a36Sopenharmony_cistatic inline struct drbd_peer_device *first_peer_device(struct drbd_device *device)
94662306a36Sopenharmony_ci{
94762306a36Sopenharmony_ci	return list_first_entry_or_null(&device->peer_devices, struct drbd_peer_device, peer_devices);
94862306a36Sopenharmony_ci}
94962306a36Sopenharmony_ci
95062306a36Sopenharmony_cistatic inline struct drbd_peer_device *
95162306a36Sopenharmony_ciconn_peer_device(struct drbd_connection *connection, int volume_number)
95262306a36Sopenharmony_ci{
95362306a36Sopenharmony_ci	return idr_find(&connection->peer_devices, volume_number);
95462306a36Sopenharmony_ci}
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ci#define for_each_resource(resource, _resources) \
95762306a36Sopenharmony_ci	list_for_each_entry(resource, _resources, resources)
95862306a36Sopenharmony_ci
95962306a36Sopenharmony_ci#define for_each_resource_rcu(resource, _resources) \
96062306a36Sopenharmony_ci	list_for_each_entry_rcu(resource, _resources, resources)
96162306a36Sopenharmony_ci
96262306a36Sopenharmony_ci#define for_each_resource_safe(resource, tmp, _resources) \
96362306a36Sopenharmony_ci	list_for_each_entry_safe(resource, tmp, _resources, resources)
96462306a36Sopenharmony_ci
96562306a36Sopenharmony_ci#define for_each_connection(connection, resource) \
96662306a36Sopenharmony_ci	list_for_each_entry(connection, &resource->connections, connections)
96762306a36Sopenharmony_ci
96862306a36Sopenharmony_ci#define for_each_connection_rcu(connection, resource) \
96962306a36Sopenharmony_ci	list_for_each_entry_rcu(connection, &resource->connections, connections)
97062306a36Sopenharmony_ci
97162306a36Sopenharmony_ci#define for_each_connection_safe(connection, tmp, resource) \
97262306a36Sopenharmony_ci	list_for_each_entry_safe(connection, tmp, &resource->connections, connections)
97362306a36Sopenharmony_ci
97462306a36Sopenharmony_ci#define for_each_peer_device(peer_device, device) \
97562306a36Sopenharmony_ci	list_for_each_entry(peer_device, &device->peer_devices, peer_devices)
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci#define for_each_peer_device_rcu(peer_device, device) \
97862306a36Sopenharmony_ci	list_for_each_entry_rcu(peer_device, &device->peer_devices, peer_devices)
97962306a36Sopenharmony_ci
98062306a36Sopenharmony_ci#define for_each_peer_device_safe(peer_device, tmp, device) \
98162306a36Sopenharmony_ci	list_for_each_entry_safe(peer_device, tmp, &device->peer_devices, peer_devices)
98262306a36Sopenharmony_ci
98362306a36Sopenharmony_cistatic inline unsigned int device_to_minor(struct drbd_device *device)
98462306a36Sopenharmony_ci{
98562306a36Sopenharmony_ci	return device->minor;
98662306a36Sopenharmony_ci}
98762306a36Sopenharmony_ci
98862306a36Sopenharmony_ci/*
98962306a36Sopenharmony_ci * function declarations
99062306a36Sopenharmony_ci *************************/
99162306a36Sopenharmony_ci
99262306a36Sopenharmony_ci/* drbd_main.c */
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_cienum dds_flags {
99562306a36Sopenharmony_ci	DDSF_FORCED    = 1,
99662306a36Sopenharmony_ci	DDSF_NO_RESYNC = 2, /* Do not run a resync for the new space */
99762306a36Sopenharmony_ci};
99862306a36Sopenharmony_ci
99962306a36Sopenharmony_ciextern void drbd_init_set_defaults(struct drbd_device *device);
100062306a36Sopenharmony_ciextern int  drbd_thread_start(struct drbd_thread *thi);
100162306a36Sopenharmony_ciextern void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait);
100262306a36Sopenharmony_ci#ifdef CONFIG_SMP
100362306a36Sopenharmony_ciextern void drbd_thread_current_set_cpu(struct drbd_thread *thi);
100462306a36Sopenharmony_ci#else
100562306a36Sopenharmony_ci#define drbd_thread_current_set_cpu(A) ({})
100662306a36Sopenharmony_ci#endif
100762306a36Sopenharmony_ciextern void tl_release(struct drbd_connection *, unsigned int barrier_nr,
100862306a36Sopenharmony_ci		       unsigned int set_size);
100962306a36Sopenharmony_ciextern void tl_clear(struct drbd_connection *);
101062306a36Sopenharmony_ciextern void drbd_free_sock(struct drbd_connection *connection);
101162306a36Sopenharmony_ciextern int drbd_send(struct drbd_connection *connection, struct socket *sock,
101262306a36Sopenharmony_ci		     void *buf, size_t size, unsigned msg_flags);
101362306a36Sopenharmony_ciextern int drbd_send_all(struct drbd_connection *, struct socket *, void *, size_t,
101462306a36Sopenharmony_ci			 unsigned);
101562306a36Sopenharmony_ci
101662306a36Sopenharmony_ciextern int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cmd);
101762306a36Sopenharmony_ciextern int drbd_send_protocol(struct drbd_connection *connection);
101862306a36Sopenharmony_ciextern int drbd_send_uuids(struct drbd_peer_device *);
101962306a36Sopenharmony_ciextern int drbd_send_uuids_skip_initial_sync(struct drbd_peer_device *);
102062306a36Sopenharmony_ciextern void drbd_gen_and_send_sync_uuid(struct drbd_peer_device *);
102162306a36Sopenharmony_ciextern int drbd_send_sizes(struct drbd_peer_device *, int trigger_reply, enum dds_flags flags);
102262306a36Sopenharmony_ciextern int drbd_send_state(struct drbd_peer_device *, union drbd_state s);
102362306a36Sopenharmony_ciextern int drbd_send_current_state(struct drbd_peer_device *);
102462306a36Sopenharmony_ciextern int drbd_send_sync_param(struct drbd_peer_device *);
102562306a36Sopenharmony_ciextern void drbd_send_b_ack(struct drbd_connection *connection, u32 barrier_nr,
102662306a36Sopenharmony_ci			    u32 set_size);
102762306a36Sopenharmony_ciextern int drbd_send_ack(struct drbd_peer_device *, enum drbd_packet,
102862306a36Sopenharmony_ci			 struct drbd_peer_request *);
102962306a36Sopenharmony_ciextern void drbd_send_ack_rp(struct drbd_peer_device *, enum drbd_packet,
103062306a36Sopenharmony_ci			     struct p_block_req *rp);
103162306a36Sopenharmony_ciextern void drbd_send_ack_dp(struct drbd_peer_device *, enum drbd_packet,
103262306a36Sopenharmony_ci			     struct p_data *dp, int data_size);
103362306a36Sopenharmony_ciextern int drbd_send_ack_ex(struct drbd_peer_device *, enum drbd_packet,
103462306a36Sopenharmony_ci			    sector_t sector, int blksize, u64 block_id);
103562306a36Sopenharmony_ciextern int drbd_send_out_of_sync(struct drbd_peer_device *, struct drbd_request *);
103662306a36Sopenharmony_ciextern int drbd_send_block(struct drbd_peer_device *, enum drbd_packet,
103762306a36Sopenharmony_ci			   struct drbd_peer_request *);
103862306a36Sopenharmony_ciextern int drbd_send_dblock(struct drbd_peer_device *, struct drbd_request *req);
103962306a36Sopenharmony_ciextern int drbd_send_drequest(struct drbd_peer_device *, int cmd,
104062306a36Sopenharmony_ci			      sector_t sector, int size, u64 block_id);
104162306a36Sopenharmony_ciextern int drbd_send_drequest_csum(struct drbd_peer_device *, sector_t sector,
104262306a36Sopenharmony_ci				   int size, void *digest, int digest_size,
104362306a36Sopenharmony_ci				   enum drbd_packet cmd);
104462306a36Sopenharmony_ciextern int drbd_send_ov_request(struct drbd_peer_device *, sector_t sector, int size);
104562306a36Sopenharmony_ci
104662306a36Sopenharmony_ciextern int drbd_send_bitmap(struct drbd_device *device, struct drbd_peer_device *peer_device);
104762306a36Sopenharmony_ciextern void drbd_send_sr_reply(struct drbd_peer_device *, enum drbd_state_rv retcode);
104862306a36Sopenharmony_ciextern void conn_send_sr_reply(struct drbd_connection *connection, enum drbd_state_rv retcode);
104962306a36Sopenharmony_ciextern int drbd_send_rs_deallocated(struct drbd_peer_device *, struct drbd_peer_request *);
105062306a36Sopenharmony_ciextern void drbd_backing_dev_free(struct drbd_device *device, struct drbd_backing_dev *ldev);
105162306a36Sopenharmony_ciextern void drbd_device_cleanup(struct drbd_device *device);
105262306a36Sopenharmony_ciextern void drbd_print_uuids(struct drbd_device *device, const char *text);
105362306a36Sopenharmony_ciextern void drbd_queue_unplug(struct drbd_device *device);
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_ciextern void conn_md_sync(struct drbd_connection *connection);
105662306a36Sopenharmony_ciextern void drbd_md_write(struct drbd_device *device, void *buffer);
105762306a36Sopenharmony_ciextern void drbd_md_sync(struct drbd_device *device);
105862306a36Sopenharmony_ciextern int  drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev);
105962306a36Sopenharmony_ciextern void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local);
106062306a36Sopenharmony_ciextern void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local);
106162306a36Sopenharmony_ciextern void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local);
106262306a36Sopenharmony_ciextern void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local);
106362306a36Sopenharmony_ciextern void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local);
106462306a36Sopenharmony_ciextern void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local);
106562306a36Sopenharmony_ciextern void drbd_md_set_flag(struct drbd_device *device, int flags) __must_hold(local);
106662306a36Sopenharmony_ciextern void drbd_md_clear_flag(struct drbd_device *device, int flags)__must_hold(local);
106762306a36Sopenharmony_ciextern int drbd_md_test_flag(struct drbd_backing_dev *, int);
106862306a36Sopenharmony_ciextern void drbd_md_mark_dirty(struct drbd_device *device);
106962306a36Sopenharmony_ciextern void drbd_queue_bitmap_io(struct drbd_device *device,
107062306a36Sopenharmony_ci				 int (*io_fn)(struct drbd_device *, struct drbd_peer_device *),
107162306a36Sopenharmony_ci				 void (*done)(struct drbd_device *, int),
107262306a36Sopenharmony_ci				 char *why, enum bm_flag flags,
107362306a36Sopenharmony_ci				 struct drbd_peer_device *peer_device);
107462306a36Sopenharmony_ciextern int drbd_bitmap_io(struct drbd_device *device,
107562306a36Sopenharmony_ci		int (*io_fn)(struct drbd_device *, struct drbd_peer_device *),
107662306a36Sopenharmony_ci		char *why, enum bm_flag flags,
107762306a36Sopenharmony_ci		struct drbd_peer_device *peer_device);
107862306a36Sopenharmony_ciextern int drbd_bitmap_io_from_worker(struct drbd_device *device,
107962306a36Sopenharmony_ci		int (*io_fn)(struct drbd_device *, struct drbd_peer_device *),
108062306a36Sopenharmony_ci		char *why, enum bm_flag flags,
108162306a36Sopenharmony_ci		struct drbd_peer_device *peer_device);
108262306a36Sopenharmony_ciextern int drbd_bmio_set_n_write(struct drbd_device *device,
108362306a36Sopenharmony_ci		struct drbd_peer_device *peer_device) __must_hold(local);
108462306a36Sopenharmony_ciextern int drbd_bmio_clear_n_write(struct drbd_device *device,
108562306a36Sopenharmony_ci		struct drbd_peer_device *peer_device) __must_hold(local);
108662306a36Sopenharmony_ci
108762306a36Sopenharmony_ci/* Meta data layout
108862306a36Sopenharmony_ci *
108962306a36Sopenharmony_ci * We currently have two possible layouts.
109062306a36Sopenharmony_ci * Offsets in (512 byte) sectors.
109162306a36Sopenharmony_ci * external:
109262306a36Sopenharmony_ci *   |----------- md_size_sect ------------------|
109362306a36Sopenharmony_ci *   [ 4k superblock ][ activity log ][  Bitmap  ]
109462306a36Sopenharmony_ci *   | al_offset == 8 |
109562306a36Sopenharmony_ci *   | bm_offset = al_offset + X      |
109662306a36Sopenharmony_ci *  ==> bitmap sectors = md_size_sect - bm_offset
109762306a36Sopenharmony_ci *
109862306a36Sopenharmony_ci *  Variants:
109962306a36Sopenharmony_ci *     old, indexed fixed size meta data:
110062306a36Sopenharmony_ci *
110162306a36Sopenharmony_ci * internal:
110262306a36Sopenharmony_ci *            |----------- md_size_sect ------------------|
110362306a36Sopenharmony_ci * [data.....][  Bitmap  ][ activity log ][ 4k superblock ][padding*]
110462306a36Sopenharmony_ci *                        | al_offset < 0 |
110562306a36Sopenharmony_ci *            | bm_offset = al_offset - Y |
110662306a36Sopenharmony_ci *  ==> bitmap sectors = Y = al_offset - bm_offset
110762306a36Sopenharmony_ci *
110862306a36Sopenharmony_ci *  [padding*] are zero or up to 7 unused 512 Byte sectors to the
110962306a36Sopenharmony_ci *  end of the device, so that the [4k superblock] will be 4k aligned.
111062306a36Sopenharmony_ci *
111162306a36Sopenharmony_ci *  The activity log consists of 4k transaction blocks,
111262306a36Sopenharmony_ci *  which are written in a ring-buffer, or striped ring-buffer like fashion,
111362306a36Sopenharmony_ci *  which are writtensize used to be fixed 32kB,
111462306a36Sopenharmony_ci *  but is about to become configurable.
111562306a36Sopenharmony_ci */
111662306a36Sopenharmony_ci
111762306a36Sopenharmony_ci/* Our old fixed size meta data layout
111862306a36Sopenharmony_ci * allows up to about 3.8TB, so if you want more,
111962306a36Sopenharmony_ci * you need to use the "flexible" meta data format. */
112062306a36Sopenharmony_ci#define MD_128MB_SECT (128LLU << 11)  /* 128 MB, unit sectors */
112162306a36Sopenharmony_ci#define MD_4kB_SECT	 8
112262306a36Sopenharmony_ci#define MD_32kB_SECT	64
112362306a36Sopenharmony_ci
112462306a36Sopenharmony_ci/* One activity log extent represents 4M of storage */
112562306a36Sopenharmony_ci#define AL_EXTENT_SHIFT 22
112662306a36Sopenharmony_ci#define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
112762306a36Sopenharmony_ci
112862306a36Sopenharmony_ci/* We could make these currently hardcoded constants configurable
112962306a36Sopenharmony_ci * variables at create-md time (or even re-configurable at runtime?).
113062306a36Sopenharmony_ci * Which will require some more changes to the DRBD "super block"
113162306a36Sopenharmony_ci * and attach code.
113262306a36Sopenharmony_ci *
113362306a36Sopenharmony_ci * updates per transaction:
113462306a36Sopenharmony_ci *   This many changes to the active set can be logged with one transaction.
113562306a36Sopenharmony_ci *   This number is arbitrary.
113662306a36Sopenharmony_ci * context per transaction:
113762306a36Sopenharmony_ci *   This many context extent numbers are logged with each transaction.
113862306a36Sopenharmony_ci *   This number is resulting from the transaction block size (4k), the layout
113962306a36Sopenharmony_ci *   of the transaction header, and the number of updates per transaction.
114062306a36Sopenharmony_ci *   See drbd_actlog.c:struct al_transaction_on_disk
114162306a36Sopenharmony_ci * */
114262306a36Sopenharmony_ci#define AL_UPDATES_PER_TRANSACTION	 64	// arbitrary
114362306a36Sopenharmony_ci#define AL_CONTEXT_PER_TRANSACTION	919	// (4096 - 36 - 6*64)/4
114462306a36Sopenharmony_ci
114562306a36Sopenharmony_ci#if BITS_PER_LONG == 32
114662306a36Sopenharmony_ci#define LN2_BPL 5
114762306a36Sopenharmony_ci#define cpu_to_lel(A) cpu_to_le32(A)
114862306a36Sopenharmony_ci#define lel_to_cpu(A) le32_to_cpu(A)
114962306a36Sopenharmony_ci#elif BITS_PER_LONG == 64
115062306a36Sopenharmony_ci#define LN2_BPL 6
115162306a36Sopenharmony_ci#define cpu_to_lel(A) cpu_to_le64(A)
115262306a36Sopenharmony_ci#define lel_to_cpu(A) le64_to_cpu(A)
115362306a36Sopenharmony_ci#else
115462306a36Sopenharmony_ci#error "LN2 of BITS_PER_LONG unknown!"
115562306a36Sopenharmony_ci#endif
115662306a36Sopenharmony_ci
115762306a36Sopenharmony_ci/* resync bitmap */
115862306a36Sopenharmony_ci/* 16MB sized 'bitmap extent' to track syncer usage */
115962306a36Sopenharmony_cistruct bm_extent {
116062306a36Sopenharmony_ci	int rs_left; /* number of bits set (out of sync) in this extent. */
116162306a36Sopenharmony_ci	int rs_failed; /* number of failed resync requests in this extent. */
116262306a36Sopenharmony_ci	unsigned long flags;
116362306a36Sopenharmony_ci	struct lc_element lce;
116462306a36Sopenharmony_ci};
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_ci#define BME_NO_WRITES  0  /* bm_extent.flags: no more requests on this one! */
116762306a36Sopenharmony_ci#define BME_LOCKED     1  /* bm_extent.flags: syncer active on this one. */
116862306a36Sopenharmony_ci#define BME_PRIORITY   2  /* finish resync IO on this extent ASAP! App IO waiting! */
116962306a36Sopenharmony_ci
117062306a36Sopenharmony_ci/* drbd_bitmap.c */
117162306a36Sopenharmony_ci/*
117262306a36Sopenharmony_ci * We need to store one bit for a block.
117362306a36Sopenharmony_ci * Example: 1GB disk @ 4096 byte blocks ==> we need 32 KB bitmap.
117462306a36Sopenharmony_ci * Bit 0 ==> local node thinks this block is binary identical on both nodes
117562306a36Sopenharmony_ci * Bit 1 ==> local node thinks this block needs to be synced.
117662306a36Sopenharmony_ci */
117762306a36Sopenharmony_ci
117862306a36Sopenharmony_ci#define SLEEP_TIME (HZ/10)
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_ci/* We do bitmap IO in units of 4k blocks.
118162306a36Sopenharmony_ci * We also still have a hardcoded 4k per bit relation. */
118262306a36Sopenharmony_ci#define BM_BLOCK_SHIFT	12			 /* 4k per bit */
118362306a36Sopenharmony_ci#define BM_BLOCK_SIZE	 (1<<BM_BLOCK_SHIFT)
118462306a36Sopenharmony_ci/* mostly arbitrarily set the represented size of one bitmap extent,
118562306a36Sopenharmony_ci * aka resync extent, to 16 MiB (which is also 512 Byte worth of bitmap
118662306a36Sopenharmony_ci * at 4k per bit resolution) */
118762306a36Sopenharmony_ci#define BM_EXT_SHIFT	 24	/* 16 MiB per resync extent */
118862306a36Sopenharmony_ci#define BM_EXT_SIZE	 (1<<BM_EXT_SHIFT)
118962306a36Sopenharmony_ci
119062306a36Sopenharmony_ci#if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
119162306a36Sopenharmony_ci#error "HAVE YOU FIXED drbdmeta AS WELL??"
119262306a36Sopenharmony_ci#endif
119362306a36Sopenharmony_ci
119462306a36Sopenharmony_ci/* thus many _storage_ sectors are described by one bit */
119562306a36Sopenharmony_ci#define BM_SECT_TO_BIT(x)   ((x)>>(BM_BLOCK_SHIFT-9))
119662306a36Sopenharmony_ci#define BM_BIT_TO_SECT(x)   ((sector_t)(x)<<(BM_BLOCK_SHIFT-9))
119762306a36Sopenharmony_ci#define BM_SECT_PER_BIT     BM_BIT_TO_SECT(1)
119862306a36Sopenharmony_ci
119962306a36Sopenharmony_ci/* bit to represented kilo byte conversion */
120062306a36Sopenharmony_ci#define Bit2KB(bits) ((bits)<<(BM_BLOCK_SHIFT-10))
120162306a36Sopenharmony_ci
120262306a36Sopenharmony_ci/* in which _bitmap_ extent (resp. sector) the bit for a certain
120362306a36Sopenharmony_ci * _storage_ sector is located in */
120462306a36Sopenharmony_ci#define BM_SECT_TO_EXT(x)   ((x)>>(BM_EXT_SHIFT-9))
120562306a36Sopenharmony_ci#define BM_BIT_TO_EXT(x)    ((x) >> (BM_EXT_SHIFT - BM_BLOCK_SHIFT))
120662306a36Sopenharmony_ci
120762306a36Sopenharmony_ci/* first storage sector a bitmap extent corresponds to */
120862306a36Sopenharmony_ci#define BM_EXT_TO_SECT(x)   ((sector_t)(x) << (BM_EXT_SHIFT-9))
120962306a36Sopenharmony_ci/* how much _storage_ sectors we have per bitmap extent */
121062306a36Sopenharmony_ci#define BM_SECT_PER_EXT     BM_EXT_TO_SECT(1)
121162306a36Sopenharmony_ci/* how many bits are covered by one bitmap extent (resync extent) */
121262306a36Sopenharmony_ci#define BM_BITS_PER_EXT     (1UL << (BM_EXT_SHIFT - BM_BLOCK_SHIFT))
121362306a36Sopenharmony_ci
121462306a36Sopenharmony_ci#define BM_BLOCKS_PER_BM_EXT_MASK  (BM_BITS_PER_EXT - 1)
121562306a36Sopenharmony_ci
121662306a36Sopenharmony_ci
121762306a36Sopenharmony_ci/* in one sector of the bitmap, we have this many activity_log extents. */
121862306a36Sopenharmony_ci#define AL_EXT_PER_BM_SECT  (1 << (BM_EXT_SHIFT - AL_EXTENT_SHIFT))
121962306a36Sopenharmony_ci
122062306a36Sopenharmony_ci/* the extent in "PER_EXTENT" below is an activity log extent
122162306a36Sopenharmony_ci * we need that many (long words/bytes) to store the bitmap
122262306a36Sopenharmony_ci *		     of one AL_EXTENT_SIZE chunk of storage.
122362306a36Sopenharmony_ci * we can store the bitmap for that many AL_EXTENTS within
122462306a36Sopenharmony_ci * one sector of the _on_disk_ bitmap:
122562306a36Sopenharmony_ci * bit	 0	  bit 37   bit 38	     bit (512*8)-1
122662306a36Sopenharmony_ci *	     ...|........|........|.. // ..|........|
122762306a36Sopenharmony_ci * sect. 0	 `296	  `304			   ^(512*8*8)-1
122862306a36Sopenharmony_ci *
122962306a36Sopenharmony_ci#define BM_WORDS_PER_EXT    ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / BITS_PER_LONG )
123062306a36Sopenharmony_ci#define BM_BYTES_PER_EXT    ( (AL_EXT_SIZE/BM_BLOCK_SIZE) / 8 )  // 128
123162306a36Sopenharmony_ci#define BM_EXT_PER_SECT	    ( 512 / BM_BYTES_PER_EXTENT )	 //   4
123262306a36Sopenharmony_ci */
123362306a36Sopenharmony_ci
123462306a36Sopenharmony_ci#define DRBD_MAX_SECTORS_32 (0xffffffffLU)
123562306a36Sopenharmony_ci/* we have a certain meta data variant that has a fixed on-disk size of 128
123662306a36Sopenharmony_ci * MiB, of which 4k are our "superblock", and 32k are the fixed size activity
123762306a36Sopenharmony_ci * log, leaving this many sectors for the bitmap.
123862306a36Sopenharmony_ci */
123962306a36Sopenharmony_ci
124062306a36Sopenharmony_ci#define DRBD_MAX_SECTORS_FIXED_BM \
124162306a36Sopenharmony_ci	  ((MD_128MB_SECT - MD_32kB_SECT - MD_4kB_SECT) * (1LL<<(BM_EXT_SHIFT-9)))
124262306a36Sopenharmony_ci#define DRBD_MAX_SECTORS      DRBD_MAX_SECTORS_FIXED_BM
124362306a36Sopenharmony_ci/* 16 TB in units of sectors */
124462306a36Sopenharmony_ci#if BITS_PER_LONG == 32
124562306a36Sopenharmony_ci/* adjust by one page worth of bitmap,
124662306a36Sopenharmony_ci * so we won't wrap around in drbd_bm_find_next_bit.
124762306a36Sopenharmony_ci * you should use 64bit OS for that much storage, anyways. */
124862306a36Sopenharmony_ci#define DRBD_MAX_SECTORS_FLEX BM_BIT_TO_SECT(0xffff7fff)
124962306a36Sopenharmony_ci#else
125062306a36Sopenharmony_ci/* we allow up to 1 PiB now on 64bit architecture with "flexible" meta data */
125162306a36Sopenharmony_ci#define DRBD_MAX_SECTORS_FLEX (1UL << 51)
125262306a36Sopenharmony_ci/* corresponds to (1UL << 38) bits right now. */
125362306a36Sopenharmony_ci#endif
125462306a36Sopenharmony_ci
125562306a36Sopenharmony_ci/* Estimate max bio size as 256 * PAGE_SIZE,
125662306a36Sopenharmony_ci * so for typical PAGE_SIZE of 4k, that is (1<<20) Byte.
125762306a36Sopenharmony_ci * Since we may live in a mixed-platform cluster,
125862306a36Sopenharmony_ci * we limit us to a platform agnostic constant here for now.
125962306a36Sopenharmony_ci * A followup commit may allow even bigger BIO sizes,
126062306a36Sopenharmony_ci * once we thought that through. */
126162306a36Sopenharmony_ci#define DRBD_MAX_BIO_SIZE (1U << 20)
126262306a36Sopenharmony_ci#if DRBD_MAX_BIO_SIZE > (BIO_MAX_VECS << PAGE_SHIFT)
126362306a36Sopenharmony_ci#error Architecture not supported: DRBD_MAX_BIO_SIZE > BIO_MAX_SIZE
126462306a36Sopenharmony_ci#endif
126562306a36Sopenharmony_ci#define DRBD_MAX_BIO_SIZE_SAFE (1U << 12)       /* Works always = 4k */
126662306a36Sopenharmony_ci
126762306a36Sopenharmony_ci#define DRBD_MAX_SIZE_H80_PACKET (1U << 15) /* Header 80 only allows packets up to 32KiB data */
126862306a36Sopenharmony_ci#define DRBD_MAX_BIO_SIZE_P95    (1U << 17) /* Protocol 95 to 99 allows bios up to 128KiB */
126962306a36Sopenharmony_ci
127062306a36Sopenharmony_ci/* For now, don't allow more than half of what we can "activate" in one
127162306a36Sopenharmony_ci * activity log transaction to be discarded in one go. We may need to rework
127262306a36Sopenharmony_ci * drbd_al_begin_io() to allow for even larger discard ranges */
127362306a36Sopenharmony_ci#define DRBD_MAX_BATCH_BIO_SIZE	 (AL_UPDATES_PER_TRANSACTION/2*AL_EXTENT_SIZE)
127462306a36Sopenharmony_ci#define DRBD_MAX_BBIO_SECTORS    (DRBD_MAX_BATCH_BIO_SIZE >> 9)
127562306a36Sopenharmony_ci
127662306a36Sopenharmony_ciextern int  drbd_bm_init(struct drbd_device *device);
127762306a36Sopenharmony_ciextern int  drbd_bm_resize(struct drbd_device *device, sector_t sectors, int set_new_bits);
127862306a36Sopenharmony_ciextern void drbd_bm_cleanup(struct drbd_device *device);
127962306a36Sopenharmony_ciextern void drbd_bm_set_all(struct drbd_device *device);
128062306a36Sopenharmony_ciextern void drbd_bm_clear_all(struct drbd_device *device);
128162306a36Sopenharmony_ci/* set/clear/test only a few bits at a time */
128262306a36Sopenharmony_ciextern int  drbd_bm_set_bits(
128362306a36Sopenharmony_ci		struct drbd_device *device, unsigned long s, unsigned long e);
128462306a36Sopenharmony_ciextern int  drbd_bm_clear_bits(
128562306a36Sopenharmony_ci		struct drbd_device *device, unsigned long s, unsigned long e);
128662306a36Sopenharmony_ciextern int drbd_bm_count_bits(
128762306a36Sopenharmony_ci	struct drbd_device *device, const unsigned long s, const unsigned long e);
128862306a36Sopenharmony_ci/* bm_set_bits variant for use while holding drbd_bm_lock,
128962306a36Sopenharmony_ci * may process the whole bitmap in one go */
129062306a36Sopenharmony_ciextern void _drbd_bm_set_bits(struct drbd_device *device,
129162306a36Sopenharmony_ci		const unsigned long s, const unsigned long e);
129262306a36Sopenharmony_ciextern int  drbd_bm_test_bit(struct drbd_device *device, unsigned long bitnr);
129362306a36Sopenharmony_ciextern int  drbd_bm_e_weight(struct drbd_device *device, unsigned long enr);
129462306a36Sopenharmony_ciextern int  drbd_bm_read(struct drbd_device *device,
129562306a36Sopenharmony_ci		struct drbd_peer_device *peer_device) __must_hold(local);
129662306a36Sopenharmony_ciextern void drbd_bm_mark_for_writeout(struct drbd_device *device, int page_nr);
129762306a36Sopenharmony_ciextern int  drbd_bm_write(struct drbd_device *device,
129862306a36Sopenharmony_ci		struct drbd_peer_device *peer_device) __must_hold(local);
129962306a36Sopenharmony_ciextern void drbd_bm_reset_al_hints(struct drbd_device *device) __must_hold(local);
130062306a36Sopenharmony_ciextern int  drbd_bm_write_hinted(struct drbd_device *device) __must_hold(local);
130162306a36Sopenharmony_ciextern int  drbd_bm_write_lazy(struct drbd_device *device, unsigned upper_idx) __must_hold(local);
130262306a36Sopenharmony_ciextern int drbd_bm_write_all(struct drbd_device *device,
130362306a36Sopenharmony_ci		struct drbd_peer_device *peer_device) __must_hold(local);
130462306a36Sopenharmony_ciextern int  drbd_bm_write_copy_pages(struct drbd_device *device,
130562306a36Sopenharmony_ci		struct drbd_peer_device *peer_device) __must_hold(local);
130662306a36Sopenharmony_ciextern size_t	     drbd_bm_words(struct drbd_device *device);
130762306a36Sopenharmony_ciextern unsigned long drbd_bm_bits(struct drbd_device *device);
130862306a36Sopenharmony_ciextern sector_t      drbd_bm_capacity(struct drbd_device *device);
130962306a36Sopenharmony_ci
131062306a36Sopenharmony_ci#define DRBD_END_OF_BITMAP	(~(unsigned long)0)
131162306a36Sopenharmony_ciextern unsigned long drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo);
131262306a36Sopenharmony_ci/* bm_find_next variants for use while you hold drbd_bm_lock() */
131362306a36Sopenharmony_ciextern unsigned long _drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo);
131462306a36Sopenharmony_ciextern unsigned long _drbd_bm_find_next_zero(struct drbd_device *device, unsigned long bm_fo);
131562306a36Sopenharmony_ciextern unsigned long _drbd_bm_total_weight(struct drbd_device *device);
131662306a36Sopenharmony_ciextern unsigned long drbd_bm_total_weight(struct drbd_device *device);
131762306a36Sopenharmony_ci/* for receive_bitmap */
131862306a36Sopenharmony_ciextern void drbd_bm_merge_lel(struct drbd_device *device, size_t offset,
131962306a36Sopenharmony_ci		size_t number, unsigned long *buffer);
132062306a36Sopenharmony_ci/* for _drbd_send_bitmap */
132162306a36Sopenharmony_ciextern void drbd_bm_get_lel(struct drbd_device *device, size_t offset,
132262306a36Sopenharmony_ci		size_t number, unsigned long *buffer);
132362306a36Sopenharmony_ci
132462306a36Sopenharmony_ciextern void drbd_bm_lock(struct drbd_device *device, char *why, enum bm_flag flags);
132562306a36Sopenharmony_ciextern void drbd_bm_unlock(struct drbd_device *device);
132662306a36Sopenharmony_ci/* drbd_main.c */
132762306a36Sopenharmony_ci
132862306a36Sopenharmony_ciextern struct kmem_cache *drbd_request_cache;
132962306a36Sopenharmony_ciextern struct kmem_cache *drbd_ee_cache;	/* peer requests */
133062306a36Sopenharmony_ciextern struct kmem_cache *drbd_bm_ext_cache;	/* bitmap extents */
133162306a36Sopenharmony_ciextern struct kmem_cache *drbd_al_ext_cache;	/* activity log extents */
133262306a36Sopenharmony_ciextern mempool_t drbd_request_mempool;
133362306a36Sopenharmony_ciextern mempool_t drbd_ee_mempool;
133462306a36Sopenharmony_ci
133562306a36Sopenharmony_ci/* drbd's page pool, used to buffer data received from the peer,
133662306a36Sopenharmony_ci * or data requested by the peer.
133762306a36Sopenharmony_ci *
133862306a36Sopenharmony_ci * This does not have an emergency reserve.
133962306a36Sopenharmony_ci *
134062306a36Sopenharmony_ci * When allocating from this pool, it first takes pages from the pool.
134162306a36Sopenharmony_ci * Only if the pool is depleted will try to allocate from the system.
134262306a36Sopenharmony_ci *
134362306a36Sopenharmony_ci * The assumption is that pages taken from this pool will be processed,
134462306a36Sopenharmony_ci * and given back, "quickly", and then can be recycled, so we can avoid
134562306a36Sopenharmony_ci * frequent calls to alloc_page(), and still will be able to make progress even
134662306a36Sopenharmony_ci * under memory pressure.
134762306a36Sopenharmony_ci */
134862306a36Sopenharmony_ciextern struct page *drbd_pp_pool;
134962306a36Sopenharmony_ciextern spinlock_t   drbd_pp_lock;
135062306a36Sopenharmony_ciextern int	    drbd_pp_vacant;
135162306a36Sopenharmony_ciextern wait_queue_head_t drbd_pp_wait;
135262306a36Sopenharmony_ci
135362306a36Sopenharmony_ci/* We also need a standard (emergency-reserve backed) page pool
135462306a36Sopenharmony_ci * for meta data IO (activity log, bitmap).
135562306a36Sopenharmony_ci * We can keep it global, as long as it is used as "N pages at a time".
135662306a36Sopenharmony_ci * 128 should be plenty, currently we probably can get away with as few as 1.
135762306a36Sopenharmony_ci */
135862306a36Sopenharmony_ci#define DRBD_MIN_POOL_PAGES	128
135962306a36Sopenharmony_ciextern mempool_t drbd_md_io_page_pool;
136062306a36Sopenharmony_ci
136162306a36Sopenharmony_ci/* We also need to make sure we get a bio
136262306a36Sopenharmony_ci * when we need it for housekeeping purposes */
136362306a36Sopenharmony_ciextern struct bio_set drbd_md_io_bio_set;
136462306a36Sopenharmony_ci
136562306a36Sopenharmony_ci/* And a bio_set for cloning */
136662306a36Sopenharmony_ciextern struct bio_set drbd_io_bio_set;
136762306a36Sopenharmony_ci
136862306a36Sopenharmony_ciextern struct mutex resources_mutex;
136962306a36Sopenharmony_ci
137062306a36Sopenharmony_ciextern int conn_lowest_minor(struct drbd_connection *connection);
137162306a36Sopenharmony_ciextern enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsigned int minor);
137262306a36Sopenharmony_ciextern void drbd_destroy_device(struct kref *kref);
137362306a36Sopenharmony_ciextern void drbd_delete_device(struct drbd_device *device);
137462306a36Sopenharmony_ci
137562306a36Sopenharmony_ciextern struct drbd_resource *drbd_create_resource(const char *name);
137662306a36Sopenharmony_ciextern void drbd_free_resource(struct drbd_resource *resource);
137762306a36Sopenharmony_ci
137862306a36Sopenharmony_ciextern int set_resource_options(struct drbd_resource *resource, struct res_opts *res_opts);
137962306a36Sopenharmony_ciextern struct drbd_connection *conn_create(const char *name, struct res_opts *res_opts);
138062306a36Sopenharmony_ciextern void drbd_destroy_connection(struct kref *kref);
138162306a36Sopenharmony_ciextern struct drbd_connection *conn_get_by_addrs(void *my_addr, int my_addr_len,
138262306a36Sopenharmony_ci					    void *peer_addr, int peer_addr_len);
138362306a36Sopenharmony_ciextern struct drbd_resource *drbd_find_resource(const char *name);
138462306a36Sopenharmony_ciextern void drbd_destroy_resource(struct kref *kref);
138562306a36Sopenharmony_ciextern void conn_free_crypto(struct drbd_connection *connection);
138662306a36Sopenharmony_ci
138762306a36Sopenharmony_ci/* drbd_req */
138862306a36Sopenharmony_ciextern void do_submit(struct work_struct *ws);
138962306a36Sopenharmony_ciextern void __drbd_make_request(struct drbd_device *, struct bio *);
139062306a36Sopenharmony_civoid drbd_submit_bio(struct bio *bio);
139162306a36Sopenharmony_ciextern int drbd_read_remote(struct drbd_device *device, struct drbd_request *req);
139262306a36Sopenharmony_ciextern int is_valid_ar_handle(struct drbd_request *, sector_t);
139362306a36Sopenharmony_ci
139462306a36Sopenharmony_ci
139562306a36Sopenharmony_ci/* drbd_nl.c */
139662306a36Sopenharmony_ci
139762306a36Sopenharmony_ciextern struct mutex notification_mutex;
139862306a36Sopenharmony_ci
139962306a36Sopenharmony_ciextern void drbd_suspend_io(struct drbd_device *device);
140062306a36Sopenharmony_ciextern void drbd_resume_io(struct drbd_device *device);
140162306a36Sopenharmony_ciextern char *ppsize(char *buf, unsigned long long size);
140262306a36Sopenharmony_ciextern sector_t drbd_new_dev_size(struct drbd_device *, struct drbd_backing_dev *, sector_t, int);
140362306a36Sopenharmony_cienum determine_dev_size {
140462306a36Sopenharmony_ci	DS_ERROR_SHRINK = -3,
140562306a36Sopenharmony_ci	DS_ERROR_SPACE_MD = -2,
140662306a36Sopenharmony_ci	DS_ERROR = -1,
140762306a36Sopenharmony_ci	DS_UNCHANGED = 0,
140862306a36Sopenharmony_ci	DS_SHRUNK = 1,
140962306a36Sopenharmony_ci	DS_GREW = 2,
141062306a36Sopenharmony_ci	DS_GREW_FROM_ZERO = 3,
141162306a36Sopenharmony_ci};
141262306a36Sopenharmony_ciextern enum determine_dev_size
141362306a36Sopenharmony_cidrbd_determine_dev_size(struct drbd_device *, enum dds_flags, struct resize_parms *) __must_hold(local);
141462306a36Sopenharmony_ciextern void resync_after_online_grow(struct drbd_device *);
141562306a36Sopenharmony_ciextern void drbd_reconsider_queue_parameters(struct drbd_device *device,
141662306a36Sopenharmony_ci			struct drbd_backing_dev *bdev, struct o_qlim *o);
141762306a36Sopenharmony_ciextern enum drbd_state_rv drbd_set_role(struct drbd_device *device,
141862306a36Sopenharmony_ci					enum drbd_role new_role,
141962306a36Sopenharmony_ci					int force);
142062306a36Sopenharmony_ciextern bool conn_try_outdate_peer(struct drbd_connection *connection);
142162306a36Sopenharmony_ciextern void conn_try_outdate_peer_async(struct drbd_connection *connection);
142262306a36Sopenharmony_ciextern enum drbd_peer_state conn_khelper(struct drbd_connection *connection, char *cmd);
142362306a36Sopenharmony_ciextern int drbd_khelper(struct drbd_device *device, char *cmd);
142462306a36Sopenharmony_ci
142562306a36Sopenharmony_ci/* drbd_worker.c */
142662306a36Sopenharmony_ci/* bi_end_io handlers */
142762306a36Sopenharmony_ciextern void drbd_md_endio(struct bio *bio);
142862306a36Sopenharmony_ciextern void drbd_peer_request_endio(struct bio *bio);
142962306a36Sopenharmony_ciextern void drbd_request_endio(struct bio *bio);
143062306a36Sopenharmony_ciextern int drbd_worker(struct drbd_thread *thi);
143162306a36Sopenharmony_cienum drbd_ret_code drbd_resync_after_valid(struct drbd_device *device, int o_minor);
143262306a36Sopenharmony_civoid drbd_resync_after_changed(struct drbd_device *device);
143362306a36Sopenharmony_ciextern void drbd_start_resync(struct drbd_device *device, enum drbd_conns side);
143462306a36Sopenharmony_ciextern void resume_next_sg(struct drbd_device *device);
143562306a36Sopenharmony_ciextern void suspend_other_sg(struct drbd_device *device);
143662306a36Sopenharmony_ciextern int drbd_resync_finished(struct drbd_peer_device *peer_device);
143762306a36Sopenharmony_ci/* maybe rather drbd_main.c ? */
143862306a36Sopenharmony_ciextern void *drbd_md_get_buffer(struct drbd_device *device, const char *intent);
143962306a36Sopenharmony_ciextern void drbd_md_put_buffer(struct drbd_device *device);
144062306a36Sopenharmony_ciextern int drbd_md_sync_page_io(struct drbd_device *device,
144162306a36Sopenharmony_ci		struct drbd_backing_dev *bdev, sector_t sector, enum req_op op);
144262306a36Sopenharmony_ciextern void drbd_ov_out_of_sync_found(struct drbd_peer_device *peer_device,
144362306a36Sopenharmony_ci		sector_t sector, int size);
144462306a36Sopenharmony_ciextern void wait_until_done_or_force_detached(struct drbd_device *device,
144562306a36Sopenharmony_ci		struct drbd_backing_dev *bdev, unsigned int *done);
144662306a36Sopenharmony_ciextern void drbd_rs_controller_reset(struct drbd_peer_device *peer_device);
144762306a36Sopenharmony_ci
144862306a36Sopenharmony_cistatic inline void ov_out_of_sync_print(struct drbd_peer_device *peer_device)
144962306a36Sopenharmony_ci{
145062306a36Sopenharmony_ci	struct drbd_device *device = peer_device->device;
145162306a36Sopenharmony_ci
145262306a36Sopenharmony_ci	if (device->ov_last_oos_size) {
145362306a36Sopenharmony_ci		drbd_err(peer_device, "Out of sync: start=%llu, size=%lu (sectors)\n",
145462306a36Sopenharmony_ci		     (unsigned long long)device->ov_last_oos_start,
145562306a36Sopenharmony_ci		     (unsigned long)device->ov_last_oos_size);
145662306a36Sopenharmony_ci	}
145762306a36Sopenharmony_ci	device->ov_last_oos_size = 0;
145862306a36Sopenharmony_ci}
145962306a36Sopenharmony_ci
146062306a36Sopenharmony_ci
146162306a36Sopenharmony_ciextern void drbd_csum_bio(struct crypto_shash *, struct bio *, void *);
146262306a36Sopenharmony_ciextern void drbd_csum_ee(struct crypto_shash *, struct drbd_peer_request *,
146362306a36Sopenharmony_ci			 void *);
146462306a36Sopenharmony_ci/* worker callbacks */
146562306a36Sopenharmony_ciextern int w_e_end_data_req(struct drbd_work *, int);
146662306a36Sopenharmony_ciextern int w_e_end_rsdata_req(struct drbd_work *, int);
146762306a36Sopenharmony_ciextern int w_e_end_csum_rs_req(struct drbd_work *, int);
146862306a36Sopenharmony_ciextern int w_e_end_ov_reply(struct drbd_work *, int);
146962306a36Sopenharmony_ciextern int w_e_end_ov_req(struct drbd_work *, int);
147062306a36Sopenharmony_ciextern int w_ov_finished(struct drbd_work *, int);
147162306a36Sopenharmony_ciextern int w_resync_timer(struct drbd_work *, int);
147262306a36Sopenharmony_ciextern int w_send_write_hint(struct drbd_work *, int);
147362306a36Sopenharmony_ciextern int w_send_dblock(struct drbd_work *, int);
147462306a36Sopenharmony_ciextern int w_send_read_req(struct drbd_work *, int);
147562306a36Sopenharmony_ciextern int w_e_reissue(struct drbd_work *, int);
147662306a36Sopenharmony_ciextern int w_restart_disk_io(struct drbd_work *, int);
147762306a36Sopenharmony_ciextern int w_send_out_of_sync(struct drbd_work *, int);
147862306a36Sopenharmony_ci
147962306a36Sopenharmony_ciextern void resync_timer_fn(struct timer_list *t);
148062306a36Sopenharmony_ciextern void start_resync_timer_fn(struct timer_list *t);
148162306a36Sopenharmony_ci
148262306a36Sopenharmony_ciextern void drbd_endio_write_sec_final(struct drbd_peer_request *peer_req);
148362306a36Sopenharmony_ci
148462306a36Sopenharmony_ci/* drbd_receiver.c */
148562306a36Sopenharmony_ciextern int drbd_issue_discard_or_zero_out(struct drbd_device *device,
148662306a36Sopenharmony_ci		sector_t start, unsigned int nr_sectors, int flags);
148762306a36Sopenharmony_ciextern int drbd_receiver(struct drbd_thread *thi);
148862306a36Sopenharmony_ciextern int drbd_ack_receiver(struct drbd_thread *thi);
148962306a36Sopenharmony_ciextern void drbd_send_ping_wf(struct work_struct *ws);
149062306a36Sopenharmony_ciextern void drbd_send_acks_wf(struct work_struct *ws);
149162306a36Sopenharmony_ciextern bool drbd_rs_c_min_rate_throttle(struct drbd_device *device);
149262306a36Sopenharmony_ciextern bool drbd_rs_should_slow_down(struct drbd_peer_device *peer_device, sector_t sector,
149362306a36Sopenharmony_ci		bool throttle_if_app_is_waiting);
149462306a36Sopenharmony_ciextern int drbd_submit_peer_request(struct drbd_peer_request *peer_req);
149562306a36Sopenharmony_ciextern int drbd_free_peer_reqs(struct drbd_device *, struct list_head *);
149662306a36Sopenharmony_ciextern struct drbd_peer_request *drbd_alloc_peer_req(struct drbd_peer_device *, u64,
149762306a36Sopenharmony_ci						     sector_t, unsigned int,
149862306a36Sopenharmony_ci						     unsigned int,
149962306a36Sopenharmony_ci						     gfp_t) __must_hold(local);
150062306a36Sopenharmony_ciextern void __drbd_free_peer_req(struct drbd_device *, struct drbd_peer_request *,
150162306a36Sopenharmony_ci				 int);
150262306a36Sopenharmony_ci#define drbd_free_peer_req(m,e) __drbd_free_peer_req(m, e, 0)
150362306a36Sopenharmony_ci#define drbd_free_net_peer_req(m,e) __drbd_free_peer_req(m, e, 1)
150462306a36Sopenharmony_ciextern struct page *drbd_alloc_pages(struct drbd_peer_device *, unsigned int, bool);
150562306a36Sopenharmony_ciextern void drbd_set_recv_tcq(struct drbd_device *device, int tcq_enabled);
150662306a36Sopenharmony_ciextern void _drbd_clear_done_ee(struct drbd_device *device, struct list_head *to_be_freed);
150762306a36Sopenharmony_ciextern int drbd_connected(struct drbd_peer_device *);
150862306a36Sopenharmony_ci
150962306a36Sopenharmony_ci/* sets the number of 512 byte sectors of our virtual device */
151062306a36Sopenharmony_civoid drbd_set_my_capacity(struct drbd_device *device, sector_t size);
151162306a36Sopenharmony_ci
151262306a36Sopenharmony_ci/*
151362306a36Sopenharmony_ci * used to submit our private bio
151462306a36Sopenharmony_ci */
151562306a36Sopenharmony_cistatic inline void drbd_submit_bio_noacct(struct drbd_device *device,
151662306a36Sopenharmony_ci					     int fault_type, struct bio *bio)
151762306a36Sopenharmony_ci{
151862306a36Sopenharmony_ci	__release(local);
151962306a36Sopenharmony_ci	if (!bio->bi_bdev) {
152062306a36Sopenharmony_ci		drbd_err(device, "drbd_submit_bio_noacct: bio->bi_bdev == NULL\n");
152162306a36Sopenharmony_ci		bio->bi_status = BLK_STS_IOERR;
152262306a36Sopenharmony_ci		bio_endio(bio);
152362306a36Sopenharmony_ci		return;
152462306a36Sopenharmony_ci	}
152562306a36Sopenharmony_ci
152662306a36Sopenharmony_ci	if (drbd_insert_fault(device, fault_type))
152762306a36Sopenharmony_ci		bio_io_error(bio);
152862306a36Sopenharmony_ci	else
152962306a36Sopenharmony_ci		submit_bio_noacct(bio);
153062306a36Sopenharmony_ci}
153162306a36Sopenharmony_ci
153262306a36Sopenharmony_civoid drbd_bump_write_ordering(struct drbd_resource *resource, struct drbd_backing_dev *bdev,
153362306a36Sopenharmony_ci			      enum write_ordering_e wo);
153462306a36Sopenharmony_ci
153562306a36Sopenharmony_ci/* drbd_proc.c */
153662306a36Sopenharmony_ciextern struct proc_dir_entry *drbd_proc;
153762306a36Sopenharmony_ciint drbd_seq_show(struct seq_file *seq, void *v);
153862306a36Sopenharmony_ci
153962306a36Sopenharmony_ci/* drbd_actlog.c */
154062306a36Sopenharmony_ciextern bool drbd_al_begin_io_prepare(struct drbd_device *device, struct drbd_interval *i);
154162306a36Sopenharmony_ciextern int drbd_al_begin_io_nonblock(struct drbd_device *device, struct drbd_interval *i);
154262306a36Sopenharmony_ciextern void drbd_al_begin_io_commit(struct drbd_device *device);
154362306a36Sopenharmony_ciextern bool drbd_al_begin_io_fastpath(struct drbd_device *device, struct drbd_interval *i);
154462306a36Sopenharmony_ciextern void drbd_al_begin_io(struct drbd_device *device, struct drbd_interval *i);
154562306a36Sopenharmony_ciextern void drbd_al_complete_io(struct drbd_device *device, struct drbd_interval *i);
154662306a36Sopenharmony_ciextern void drbd_rs_complete_io(struct drbd_device *device, sector_t sector);
154762306a36Sopenharmony_ciextern int drbd_rs_begin_io(struct drbd_device *device, sector_t sector);
154862306a36Sopenharmony_ciextern int drbd_try_rs_begin_io(struct drbd_peer_device *peer_device, sector_t sector);
154962306a36Sopenharmony_ciextern void drbd_rs_cancel_all(struct drbd_device *device);
155062306a36Sopenharmony_ciextern int drbd_rs_del_all(struct drbd_device *device);
155162306a36Sopenharmony_ciextern void drbd_rs_failed_io(struct drbd_peer_device *peer_device,
155262306a36Sopenharmony_ci		sector_t sector, int size);
155362306a36Sopenharmony_ciextern void drbd_advance_rs_marks(struct drbd_peer_device *peer_device, unsigned long still_to_go);
155462306a36Sopenharmony_ci
155562306a36Sopenharmony_cienum update_sync_bits_mode { RECORD_RS_FAILED, SET_OUT_OF_SYNC, SET_IN_SYNC };
155662306a36Sopenharmony_ciextern int __drbd_change_sync(struct drbd_peer_device *peer_device, sector_t sector, int size,
155762306a36Sopenharmony_ci		enum update_sync_bits_mode mode);
155862306a36Sopenharmony_ci#define drbd_set_in_sync(peer_device, sector, size) \
155962306a36Sopenharmony_ci	__drbd_change_sync(peer_device, sector, size, SET_IN_SYNC)
156062306a36Sopenharmony_ci#define drbd_set_out_of_sync(peer_device, sector, size) \
156162306a36Sopenharmony_ci	__drbd_change_sync(peer_device, sector, size, SET_OUT_OF_SYNC)
156262306a36Sopenharmony_ci#define drbd_rs_failed_io(peer_device, sector, size) \
156362306a36Sopenharmony_ci	__drbd_change_sync(peer_device, sector, size, RECORD_RS_FAILED)
156462306a36Sopenharmony_ciextern void drbd_al_shrink(struct drbd_device *device);
156562306a36Sopenharmony_ciextern int drbd_al_initialize(struct drbd_device *, void *);
156662306a36Sopenharmony_ci
156762306a36Sopenharmony_ci/* drbd_nl.c */
156862306a36Sopenharmony_ci/* state info broadcast */
156962306a36Sopenharmony_cistruct sib_info {
157062306a36Sopenharmony_ci	enum drbd_state_info_bcast_reason sib_reason;
157162306a36Sopenharmony_ci	union {
157262306a36Sopenharmony_ci		struct {
157362306a36Sopenharmony_ci			char *helper_name;
157462306a36Sopenharmony_ci			unsigned helper_exit_code;
157562306a36Sopenharmony_ci		};
157662306a36Sopenharmony_ci		struct {
157762306a36Sopenharmony_ci			union drbd_state os;
157862306a36Sopenharmony_ci			union drbd_state ns;
157962306a36Sopenharmony_ci		};
158062306a36Sopenharmony_ci	};
158162306a36Sopenharmony_ci};
158262306a36Sopenharmony_civoid drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib);
158362306a36Sopenharmony_ci
158462306a36Sopenharmony_ciextern int notify_resource_state(struct sk_buff *,
158562306a36Sopenharmony_ci				  unsigned int,
158662306a36Sopenharmony_ci				  struct drbd_resource *,
158762306a36Sopenharmony_ci				  struct resource_info *,
158862306a36Sopenharmony_ci				  enum drbd_notification_type);
158962306a36Sopenharmony_ciextern int notify_device_state(struct sk_buff *,
159062306a36Sopenharmony_ci				unsigned int,
159162306a36Sopenharmony_ci				struct drbd_device *,
159262306a36Sopenharmony_ci				struct device_info *,
159362306a36Sopenharmony_ci				enum drbd_notification_type);
159462306a36Sopenharmony_ciextern int notify_connection_state(struct sk_buff *,
159562306a36Sopenharmony_ci				    unsigned int,
159662306a36Sopenharmony_ci				    struct drbd_connection *,
159762306a36Sopenharmony_ci				    struct connection_info *,
159862306a36Sopenharmony_ci				    enum drbd_notification_type);
159962306a36Sopenharmony_ciextern int notify_peer_device_state(struct sk_buff *,
160062306a36Sopenharmony_ci				     unsigned int,
160162306a36Sopenharmony_ci				     struct drbd_peer_device *,
160262306a36Sopenharmony_ci				     struct peer_device_info *,
160362306a36Sopenharmony_ci				     enum drbd_notification_type);
160462306a36Sopenharmony_ciextern void notify_helper(enum drbd_notification_type, struct drbd_device *,
160562306a36Sopenharmony_ci			  struct drbd_connection *, const char *, int);
160662306a36Sopenharmony_ci
160762306a36Sopenharmony_ci/*
160862306a36Sopenharmony_ci * inline helper functions
160962306a36Sopenharmony_ci *************************/
161062306a36Sopenharmony_ci
161162306a36Sopenharmony_ci/* see also page_chain_add and friends in drbd_receiver.c */
161262306a36Sopenharmony_cistatic inline struct page *page_chain_next(struct page *page)
161362306a36Sopenharmony_ci{
161462306a36Sopenharmony_ci	return (struct page *)page_private(page);
161562306a36Sopenharmony_ci}
161662306a36Sopenharmony_ci#define page_chain_for_each(page) \
161762306a36Sopenharmony_ci	for (; page && ({ prefetch(page_chain_next(page)); 1; }); \
161862306a36Sopenharmony_ci			page = page_chain_next(page))
161962306a36Sopenharmony_ci#define page_chain_for_each_safe(page, n) \
162062306a36Sopenharmony_ci	for (; page && ({ n = page_chain_next(page); 1; }); page = n)
162162306a36Sopenharmony_ci
162262306a36Sopenharmony_ci
162362306a36Sopenharmony_cistatic inline int drbd_peer_req_has_active_page(struct drbd_peer_request *peer_req)
162462306a36Sopenharmony_ci{
162562306a36Sopenharmony_ci	struct page *page = peer_req->pages;
162662306a36Sopenharmony_ci	page_chain_for_each(page) {
162762306a36Sopenharmony_ci		if (page_count(page) > 1)
162862306a36Sopenharmony_ci			return 1;
162962306a36Sopenharmony_ci	}
163062306a36Sopenharmony_ci	return 0;
163162306a36Sopenharmony_ci}
163262306a36Sopenharmony_ci
163362306a36Sopenharmony_cistatic inline union drbd_state drbd_read_state(struct drbd_device *device)
163462306a36Sopenharmony_ci{
163562306a36Sopenharmony_ci	struct drbd_resource *resource = device->resource;
163662306a36Sopenharmony_ci	union drbd_state rv;
163762306a36Sopenharmony_ci
163862306a36Sopenharmony_ci	rv.i = device->state.i;
163962306a36Sopenharmony_ci	rv.susp = resource->susp;
164062306a36Sopenharmony_ci	rv.susp_nod = resource->susp_nod;
164162306a36Sopenharmony_ci	rv.susp_fen = resource->susp_fen;
164262306a36Sopenharmony_ci
164362306a36Sopenharmony_ci	return rv;
164462306a36Sopenharmony_ci}
164562306a36Sopenharmony_ci
164662306a36Sopenharmony_cienum drbd_force_detach_flags {
164762306a36Sopenharmony_ci	DRBD_READ_ERROR,
164862306a36Sopenharmony_ci	DRBD_WRITE_ERROR,
164962306a36Sopenharmony_ci	DRBD_META_IO_ERROR,
165062306a36Sopenharmony_ci	DRBD_FORCE_DETACH,
165162306a36Sopenharmony_ci};
165262306a36Sopenharmony_ci
165362306a36Sopenharmony_ci#define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
165462306a36Sopenharmony_cistatic inline void __drbd_chk_io_error_(struct drbd_device *device,
165562306a36Sopenharmony_ci		enum drbd_force_detach_flags df,
165662306a36Sopenharmony_ci		const char *where)
165762306a36Sopenharmony_ci{
165862306a36Sopenharmony_ci	enum drbd_io_error_p ep;
165962306a36Sopenharmony_ci
166062306a36Sopenharmony_ci	rcu_read_lock();
166162306a36Sopenharmony_ci	ep = rcu_dereference(device->ldev->disk_conf)->on_io_error;
166262306a36Sopenharmony_ci	rcu_read_unlock();
166362306a36Sopenharmony_ci	switch (ep) {
166462306a36Sopenharmony_ci	case EP_PASS_ON: /* FIXME would this be better named "Ignore"? */
166562306a36Sopenharmony_ci		if (df == DRBD_READ_ERROR || df == DRBD_WRITE_ERROR) {
166662306a36Sopenharmony_ci			if (drbd_ratelimit())
166762306a36Sopenharmony_ci				drbd_err(device, "Local IO failed in %s.\n", where);
166862306a36Sopenharmony_ci			if (device->state.disk > D_INCONSISTENT)
166962306a36Sopenharmony_ci				_drbd_set_state(_NS(device, disk, D_INCONSISTENT), CS_HARD, NULL);
167062306a36Sopenharmony_ci			break;
167162306a36Sopenharmony_ci		}
167262306a36Sopenharmony_ci		fallthrough;	/* for DRBD_META_IO_ERROR or DRBD_FORCE_DETACH */
167362306a36Sopenharmony_ci	case EP_DETACH:
167462306a36Sopenharmony_ci	case EP_CALL_HELPER:
167562306a36Sopenharmony_ci		/* Remember whether we saw a READ or WRITE error.
167662306a36Sopenharmony_ci		 *
167762306a36Sopenharmony_ci		 * Recovery of the affected area for WRITE failure is covered
167862306a36Sopenharmony_ci		 * by the activity log.
167962306a36Sopenharmony_ci		 * READ errors may fall outside that area though. Certain READ
168062306a36Sopenharmony_ci		 * errors can be "healed" by writing good data to the affected
168162306a36Sopenharmony_ci		 * blocks, which triggers block re-allocation in lower layers.
168262306a36Sopenharmony_ci		 *
168362306a36Sopenharmony_ci		 * If we can not write the bitmap after a READ error,
168462306a36Sopenharmony_ci		 * we may need to trigger a full sync (see w_go_diskless()).
168562306a36Sopenharmony_ci		 *
168662306a36Sopenharmony_ci		 * Force-detach is not really an IO error, but rather a
168762306a36Sopenharmony_ci		 * desperate measure to try to deal with a completely
168862306a36Sopenharmony_ci		 * unresponsive lower level IO stack.
168962306a36Sopenharmony_ci		 * Still it should be treated as a WRITE error.
169062306a36Sopenharmony_ci		 *
169162306a36Sopenharmony_ci		 * Meta IO error is always WRITE error:
169262306a36Sopenharmony_ci		 * we read meta data only once during attach,
169362306a36Sopenharmony_ci		 * which will fail in case of errors.
169462306a36Sopenharmony_ci		 */
169562306a36Sopenharmony_ci		set_bit(WAS_IO_ERROR, &device->flags);
169662306a36Sopenharmony_ci		if (df == DRBD_READ_ERROR)
169762306a36Sopenharmony_ci			set_bit(WAS_READ_ERROR, &device->flags);
169862306a36Sopenharmony_ci		if (df == DRBD_FORCE_DETACH)
169962306a36Sopenharmony_ci			set_bit(FORCE_DETACH, &device->flags);
170062306a36Sopenharmony_ci		if (device->state.disk > D_FAILED) {
170162306a36Sopenharmony_ci			_drbd_set_state(_NS(device, disk, D_FAILED), CS_HARD, NULL);
170262306a36Sopenharmony_ci			drbd_err(device,
170362306a36Sopenharmony_ci				"Local IO failed in %s. Detaching...\n", where);
170462306a36Sopenharmony_ci		}
170562306a36Sopenharmony_ci		break;
170662306a36Sopenharmony_ci	}
170762306a36Sopenharmony_ci}
170862306a36Sopenharmony_ci
170962306a36Sopenharmony_ci/**
171062306a36Sopenharmony_ci * drbd_chk_io_error: Handle the on_io_error setting, should be called from all io completion handlers
171162306a36Sopenharmony_ci * @device:	 DRBD device.
171262306a36Sopenharmony_ci * @error:	 Error code passed to the IO completion callback
171362306a36Sopenharmony_ci * @forcedetach: Force detach. I.e. the error happened while accessing the meta data
171462306a36Sopenharmony_ci *
171562306a36Sopenharmony_ci * See also drbd_main.c:after_state_ch() if (os.disk > D_FAILED && ns.disk == D_FAILED)
171662306a36Sopenharmony_ci */
171762306a36Sopenharmony_ci#define drbd_chk_io_error(m,e,f) drbd_chk_io_error_(m,e,f, __func__)
171862306a36Sopenharmony_cistatic inline void drbd_chk_io_error_(struct drbd_device *device,
171962306a36Sopenharmony_ci	int error, enum drbd_force_detach_flags forcedetach, const char *where)
172062306a36Sopenharmony_ci{
172162306a36Sopenharmony_ci	if (error) {
172262306a36Sopenharmony_ci		unsigned long flags;
172362306a36Sopenharmony_ci		spin_lock_irqsave(&device->resource->req_lock, flags);
172462306a36Sopenharmony_ci		__drbd_chk_io_error_(device, forcedetach, where);
172562306a36Sopenharmony_ci		spin_unlock_irqrestore(&device->resource->req_lock, flags);
172662306a36Sopenharmony_ci	}
172762306a36Sopenharmony_ci}
172862306a36Sopenharmony_ci
172962306a36Sopenharmony_ci
173062306a36Sopenharmony_ci/**
173162306a36Sopenharmony_ci * drbd_md_first_sector() - Returns the first sector number of the meta data area
173262306a36Sopenharmony_ci * @bdev:	Meta data block device.
173362306a36Sopenharmony_ci *
173462306a36Sopenharmony_ci * BTW, for internal meta data, this happens to be the maximum capacity
173562306a36Sopenharmony_ci * we could agree upon with our peer node.
173662306a36Sopenharmony_ci */
173762306a36Sopenharmony_cistatic inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
173862306a36Sopenharmony_ci{
173962306a36Sopenharmony_ci	switch (bdev->md.meta_dev_idx) {
174062306a36Sopenharmony_ci	case DRBD_MD_INDEX_INTERNAL:
174162306a36Sopenharmony_ci	case DRBD_MD_INDEX_FLEX_INT:
174262306a36Sopenharmony_ci		return bdev->md.md_offset + bdev->md.bm_offset;
174362306a36Sopenharmony_ci	case DRBD_MD_INDEX_FLEX_EXT:
174462306a36Sopenharmony_ci	default:
174562306a36Sopenharmony_ci		return bdev->md.md_offset;
174662306a36Sopenharmony_ci	}
174762306a36Sopenharmony_ci}
174862306a36Sopenharmony_ci
174962306a36Sopenharmony_ci/**
175062306a36Sopenharmony_ci * drbd_md_last_sector() - Return the last sector number of the meta data area
175162306a36Sopenharmony_ci * @bdev:	Meta data block device.
175262306a36Sopenharmony_ci */
175362306a36Sopenharmony_cistatic inline sector_t drbd_md_last_sector(struct drbd_backing_dev *bdev)
175462306a36Sopenharmony_ci{
175562306a36Sopenharmony_ci	switch (bdev->md.meta_dev_idx) {
175662306a36Sopenharmony_ci	case DRBD_MD_INDEX_INTERNAL:
175762306a36Sopenharmony_ci	case DRBD_MD_INDEX_FLEX_INT:
175862306a36Sopenharmony_ci		return bdev->md.md_offset + MD_4kB_SECT -1;
175962306a36Sopenharmony_ci	case DRBD_MD_INDEX_FLEX_EXT:
176062306a36Sopenharmony_ci	default:
176162306a36Sopenharmony_ci		return bdev->md.md_offset + bdev->md.md_size_sect -1;
176262306a36Sopenharmony_ci	}
176362306a36Sopenharmony_ci}
176462306a36Sopenharmony_ci
176562306a36Sopenharmony_ci/* Returns the number of 512 byte sectors of the device */
176662306a36Sopenharmony_cistatic inline sector_t drbd_get_capacity(struct block_device *bdev)
176762306a36Sopenharmony_ci{
176862306a36Sopenharmony_ci	return bdev ? bdev_nr_sectors(bdev) : 0;
176962306a36Sopenharmony_ci}
177062306a36Sopenharmony_ci
177162306a36Sopenharmony_ci/**
177262306a36Sopenharmony_ci * drbd_get_max_capacity() - Returns the capacity we announce to out peer
177362306a36Sopenharmony_ci * @bdev:	Meta data block device.
177462306a36Sopenharmony_ci *
177562306a36Sopenharmony_ci * returns the capacity we announce to out peer.  we clip ourselves at the
177662306a36Sopenharmony_ci * various MAX_SECTORS, because if we don't, current implementation will
177762306a36Sopenharmony_ci * oops sooner or later
177862306a36Sopenharmony_ci */
177962306a36Sopenharmony_cistatic inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
178062306a36Sopenharmony_ci{
178162306a36Sopenharmony_ci	sector_t s;
178262306a36Sopenharmony_ci
178362306a36Sopenharmony_ci	switch (bdev->md.meta_dev_idx) {
178462306a36Sopenharmony_ci	case DRBD_MD_INDEX_INTERNAL:
178562306a36Sopenharmony_ci	case DRBD_MD_INDEX_FLEX_INT:
178662306a36Sopenharmony_ci		s = drbd_get_capacity(bdev->backing_bdev)
178762306a36Sopenharmony_ci			? min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
178862306a36Sopenharmony_ci				drbd_md_first_sector(bdev))
178962306a36Sopenharmony_ci			: 0;
179062306a36Sopenharmony_ci		break;
179162306a36Sopenharmony_ci	case DRBD_MD_INDEX_FLEX_EXT:
179262306a36Sopenharmony_ci		s = min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
179362306a36Sopenharmony_ci				drbd_get_capacity(bdev->backing_bdev));
179462306a36Sopenharmony_ci		/* clip at maximum size the meta device can support */
179562306a36Sopenharmony_ci		s = min_t(sector_t, s,
179662306a36Sopenharmony_ci			BM_EXT_TO_SECT(bdev->md.md_size_sect
179762306a36Sopenharmony_ci				     - bdev->md.bm_offset));
179862306a36Sopenharmony_ci		break;
179962306a36Sopenharmony_ci	default:
180062306a36Sopenharmony_ci		s = min_t(sector_t, DRBD_MAX_SECTORS,
180162306a36Sopenharmony_ci				drbd_get_capacity(bdev->backing_bdev));
180262306a36Sopenharmony_ci	}
180362306a36Sopenharmony_ci	return s;
180462306a36Sopenharmony_ci}
180562306a36Sopenharmony_ci
180662306a36Sopenharmony_ci/**
180762306a36Sopenharmony_ci * drbd_md_ss() - Return the sector number of our meta data super block
180862306a36Sopenharmony_ci * @bdev:	Meta data block device.
180962306a36Sopenharmony_ci */
181062306a36Sopenharmony_cistatic inline sector_t drbd_md_ss(struct drbd_backing_dev *bdev)
181162306a36Sopenharmony_ci{
181262306a36Sopenharmony_ci	const int meta_dev_idx = bdev->md.meta_dev_idx;
181362306a36Sopenharmony_ci
181462306a36Sopenharmony_ci	if (meta_dev_idx == DRBD_MD_INDEX_FLEX_EXT)
181562306a36Sopenharmony_ci		return 0;
181662306a36Sopenharmony_ci
181762306a36Sopenharmony_ci	/* Since drbd08, internal meta data is always "flexible".
181862306a36Sopenharmony_ci	 * position: last 4k aligned block of 4k size */
181962306a36Sopenharmony_ci	if (meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
182062306a36Sopenharmony_ci	    meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)
182162306a36Sopenharmony_ci		return (drbd_get_capacity(bdev->backing_bdev) & ~7ULL) - 8;
182262306a36Sopenharmony_ci
182362306a36Sopenharmony_ci	/* external, some index; this is the old fixed size layout */
182462306a36Sopenharmony_ci	return MD_128MB_SECT * bdev->md.meta_dev_idx;
182562306a36Sopenharmony_ci}
182662306a36Sopenharmony_ci
182762306a36Sopenharmony_cistatic inline void
182862306a36Sopenharmony_cidrbd_queue_work(struct drbd_work_queue *q, struct drbd_work *w)
182962306a36Sopenharmony_ci{
183062306a36Sopenharmony_ci	unsigned long flags;
183162306a36Sopenharmony_ci	spin_lock_irqsave(&q->q_lock, flags);
183262306a36Sopenharmony_ci	list_add_tail(&w->list, &q->q);
183362306a36Sopenharmony_ci	spin_unlock_irqrestore(&q->q_lock, flags);
183462306a36Sopenharmony_ci	wake_up(&q->q_wait);
183562306a36Sopenharmony_ci}
183662306a36Sopenharmony_ci
183762306a36Sopenharmony_cistatic inline void
183862306a36Sopenharmony_cidrbd_queue_work_if_unqueued(struct drbd_work_queue *q, struct drbd_work *w)
183962306a36Sopenharmony_ci{
184062306a36Sopenharmony_ci	unsigned long flags;
184162306a36Sopenharmony_ci	spin_lock_irqsave(&q->q_lock, flags);
184262306a36Sopenharmony_ci	if (list_empty_careful(&w->list))
184362306a36Sopenharmony_ci		list_add_tail(&w->list, &q->q);
184462306a36Sopenharmony_ci	spin_unlock_irqrestore(&q->q_lock, flags);
184562306a36Sopenharmony_ci	wake_up(&q->q_wait);
184662306a36Sopenharmony_ci}
184762306a36Sopenharmony_ci
184862306a36Sopenharmony_cistatic inline void
184962306a36Sopenharmony_cidrbd_device_post_work(struct drbd_device *device, int work_bit)
185062306a36Sopenharmony_ci{
185162306a36Sopenharmony_ci	if (!test_and_set_bit(work_bit, &device->flags)) {
185262306a36Sopenharmony_ci		struct drbd_connection *connection =
185362306a36Sopenharmony_ci			first_peer_device(device)->connection;
185462306a36Sopenharmony_ci		struct drbd_work_queue *q = &connection->sender_work;
185562306a36Sopenharmony_ci		if (!test_and_set_bit(DEVICE_WORK_PENDING, &connection->flags))
185662306a36Sopenharmony_ci			wake_up(&q->q_wait);
185762306a36Sopenharmony_ci	}
185862306a36Sopenharmony_ci}
185962306a36Sopenharmony_ci
186062306a36Sopenharmony_ciextern void drbd_flush_workqueue(struct drbd_work_queue *work_queue);
186162306a36Sopenharmony_ci
186262306a36Sopenharmony_ci/* To get the ack_receiver out of the blocking network stack,
186362306a36Sopenharmony_ci * so it can change its sk_rcvtimeo from idle- to ping-timeout,
186462306a36Sopenharmony_ci * and send a ping, we need to send a signal.
186562306a36Sopenharmony_ci * Which signal we send is irrelevant. */
186662306a36Sopenharmony_cistatic inline void wake_ack_receiver(struct drbd_connection *connection)
186762306a36Sopenharmony_ci{
186862306a36Sopenharmony_ci	struct task_struct *task = connection->ack_receiver.task;
186962306a36Sopenharmony_ci	if (task && get_t_state(&connection->ack_receiver) == RUNNING)
187062306a36Sopenharmony_ci		send_sig(SIGXCPU, task, 1);
187162306a36Sopenharmony_ci}
187262306a36Sopenharmony_ci
187362306a36Sopenharmony_cistatic inline void request_ping(struct drbd_connection *connection)
187462306a36Sopenharmony_ci{
187562306a36Sopenharmony_ci	set_bit(SEND_PING, &connection->flags);
187662306a36Sopenharmony_ci	wake_ack_receiver(connection);
187762306a36Sopenharmony_ci}
187862306a36Sopenharmony_ci
187962306a36Sopenharmony_ciextern void *conn_prepare_command(struct drbd_connection *, struct drbd_socket *);
188062306a36Sopenharmony_ciextern void *drbd_prepare_command(struct drbd_peer_device *, struct drbd_socket *);
188162306a36Sopenharmony_ciextern int conn_send_command(struct drbd_connection *, struct drbd_socket *,
188262306a36Sopenharmony_ci			     enum drbd_packet, unsigned int, void *,
188362306a36Sopenharmony_ci			     unsigned int);
188462306a36Sopenharmony_ciextern int drbd_send_command(struct drbd_peer_device *, struct drbd_socket *,
188562306a36Sopenharmony_ci			     enum drbd_packet, unsigned int, void *,
188662306a36Sopenharmony_ci			     unsigned int);
188762306a36Sopenharmony_ci
188862306a36Sopenharmony_ciextern int drbd_send_ping(struct drbd_connection *connection);
188962306a36Sopenharmony_ciextern int drbd_send_ping_ack(struct drbd_connection *connection);
189062306a36Sopenharmony_ciextern int drbd_send_state_req(struct drbd_peer_device *, union drbd_state, union drbd_state);
189162306a36Sopenharmony_ciextern int conn_send_state_req(struct drbd_connection *, union drbd_state, union drbd_state);
189262306a36Sopenharmony_ci
189362306a36Sopenharmony_cistatic inline void drbd_thread_stop(struct drbd_thread *thi)
189462306a36Sopenharmony_ci{
189562306a36Sopenharmony_ci	_drbd_thread_stop(thi, false, true);
189662306a36Sopenharmony_ci}
189762306a36Sopenharmony_ci
189862306a36Sopenharmony_cistatic inline void drbd_thread_stop_nowait(struct drbd_thread *thi)
189962306a36Sopenharmony_ci{
190062306a36Sopenharmony_ci	_drbd_thread_stop(thi, false, false);
190162306a36Sopenharmony_ci}
190262306a36Sopenharmony_ci
190362306a36Sopenharmony_cistatic inline void drbd_thread_restart_nowait(struct drbd_thread *thi)
190462306a36Sopenharmony_ci{
190562306a36Sopenharmony_ci	_drbd_thread_stop(thi, true, false);
190662306a36Sopenharmony_ci}
190762306a36Sopenharmony_ci
190862306a36Sopenharmony_ci/* counts how many answer packets packets we expect from our peer,
190962306a36Sopenharmony_ci * for either explicit application requests,
191062306a36Sopenharmony_ci * or implicit barrier packets as necessary.
191162306a36Sopenharmony_ci * increased:
191262306a36Sopenharmony_ci *  w_send_barrier
191362306a36Sopenharmony_ci *  _req_mod(req, QUEUE_FOR_NET_WRITE or QUEUE_FOR_NET_READ);
191462306a36Sopenharmony_ci *    it is much easier and equally valid to count what we queue for the
191562306a36Sopenharmony_ci *    worker, even before it actually was queued or send.
191662306a36Sopenharmony_ci *    (drbd_make_request_common; recovery path on read io-error)
191762306a36Sopenharmony_ci * decreased:
191862306a36Sopenharmony_ci *  got_BarrierAck (respective tl_clear, tl_clear_barrier)
191962306a36Sopenharmony_ci *  _req_mod(req, DATA_RECEIVED)
192062306a36Sopenharmony_ci *     [from receive_DataReply]
192162306a36Sopenharmony_ci *  _req_mod(req, WRITE_ACKED_BY_PEER or RECV_ACKED_BY_PEER or NEG_ACKED)
192262306a36Sopenharmony_ci *     [from got_BlockAck (P_WRITE_ACK, P_RECV_ACK)]
192362306a36Sopenharmony_ci *     for some reason it is NOT decreased in got_NegAck,
192462306a36Sopenharmony_ci *     but in the resulting cleanup code from report_params.
192562306a36Sopenharmony_ci *     we should try to remember the reason for that...
192662306a36Sopenharmony_ci *  _req_mod(req, SEND_FAILED or SEND_CANCELED)
192762306a36Sopenharmony_ci *  _req_mod(req, CONNECTION_LOST_WHILE_PENDING)
192862306a36Sopenharmony_ci *     [from tl_clear_barrier]
192962306a36Sopenharmony_ci */
193062306a36Sopenharmony_cistatic inline void inc_ap_pending(struct drbd_device *device)
193162306a36Sopenharmony_ci{
193262306a36Sopenharmony_ci	atomic_inc(&device->ap_pending_cnt);
193362306a36Sopenharmony_ci}
193462306a36Sopenharmony_ci
193562306a36Sopenharmony_ci#define dec_ap_pending(device) ((void)expect((device), __dec_ap_pending(device) >= 0))
193662306a36Sopenharmony_cistatic inline int __dec_ap_pending(struct drbd_device *device)
193762306a36Sopenharmony_ci{
193862306a36Sopenharmony_ci	int ap_pending_cnt = atomic_dec_return(&device->ap_pending_cnt);
193962306a36Sopenharmony_ci
194062306a36Sopenharmony_ci	if (ap_pending_cnt == 0)
194162306a36Sopenharmony_ci		wake_up(&device->misc_wait);
194262306a36Sopenharmony_ci	return ap_pending_cnt;
194362306a36Sopenharmony_ci}
194462306a36Sopenharmony_ci
194562306a36Sopenharmony_ci/* counts how many resync-related answers we still expect from the peer
194662306a36Sopenharmony_ci *		     increase			decrease
194762306a36Sopenharmony_ci * C_SYNC_TARGET sends P_RS_DATA_REQUEST (and expects P_RS_DATA_REPLY)
194862306a36Sopenharmony_ci * C_SYNC_SOURCE sends P_RS_DATA_REPLY   (and expects P_WRITE_ACK with ID_SYNCER)
194962306a36Sopenharmony_ci *					   (or P_NEG_ACK with ID_SYNCER)
195062306a36Sopenharmony_ci */
195162306a36Sopenharmony_cistatic inline void inc_rs_pending(struct drbd_peer_device *peer_device)
195262306a36Sopenharmony_ci{
195362306a36Sopenharmony_ci	atomic_inc(&peer_device->device->rs_pending_cnt);
195462306a36Sopenharmony_ci}
195562306a36Sopenharmony_ci
195662306a36Sopenharmony_ci#define dec_rs_pending(peer_device) \
195762306a36Sopenharmony_ci	((void)expect((peer_device), __dec_rs_pending(peer_device) >= 0))
195862306a36Sopenharmony_cistatic inline int __dec_rs_pending(struct drbd_peer_device *peer_device)
195962306a36Sopenharmony_ci{
196062306a36Sopenharmony_ci	return atomic_dec_return(&peer_device->device->rs_pending_cnt);
196162306a36Sopenharmony_ci}
196262306a36Sopenharmony_ci
196362306a36Sopenharmony_ci/* counts how many answers we still need to send to the peer.
196462306a36Sopenharmony_ci * increased on
196562306a36Sopenharmony_ci *  receive_Data	unless protocol A;
196662306a36Sopenharmony_ci *			we need to send a P_RECV_ACK (proto B)
196762306a36Sopenharmony_ci *			or P_WRITE_ACK (proto C)
196862306a36Sopenharmony_ci *  receive_RSDataReply (recv_resync_read) we need to send a P_WRITE_ACK
196962306a36Sopenharmony_ci *  receive_DataRequest (receive_RSDataRequest) we need to send back P_DATA
197062306a36Sopenharmony_ci *  receive_Barrier_*	we need to send a P_BARRIER_ACK
197162306a36Sopenharmony_ci */
197262306a36Sopenharmony_cistatic inline void inc_unacked(struct drbd_device *device)
197362306a36Sopenharmony_ci{
197462306a36Sopenharmony_ci	atomic_inc(&device->unacked_cnt);
197562306a36Sopenharmony_ci}
197662306a36Sopenharmony_ci
197762306a36Sopenharmony_ci#define dec_unacked(device) ((void)expect(device, __dec_unacked(device) >= 0))
197862306a36Sopenharmony_cistatic inline int __dec_unacked(struct drbd_device *device)
197962306a36Sopenharmony_ci{
198062306a36Sopenharmony_ci	return atomic_dec_return(&device->unacked_cnt);
198162306a36Sopenharmony_ci}
198262306a36Sopenharmony_ci
198362306a36Sopenharmony_ci#define sub_unacked(device, n) ((void)expect(device, __sub_unacked(device) >= 0))
198462306a36Sopenharmony_cistatic inline int __sub_unacked(struct drbd_device *device, int n)
198562306a36Sopenharmony_ci{
198662306a36Sopenharmony_ci	return atomic_sub_return(n, &device->unacked_cnt);
198762306a36Sopenharmony_ci}
198862306a36Sopenharmony_ci
198962306a36Sopenharmony_cistatic inline bool is_sync_target_state(enum drbd_conns connection_state)
199062306a36Sopenharmony_ci{
199162306a36Sopenharmony_ci	return	connection_state == C_SYNC_TARGET ||
199262306a36Sopenharmony_ci		connection_state == C_PAUSED_SYNC_T;
199362306a36Sopenharmony_ci}
199462306a36Sopenharmony_ci
199562306a36Sopenharmony_cistatic inline bool is_sync_source_state(enum drbd_conns connection_state)
199662306a36Sopenharmony_ci{
199762306a36Sopenharmony_ci	return	connection_state == C_SYNC_SOURCE ||
199862306a36Sopenharmony_ci		connection_state == C_PAUSED_SYNC_S;
199962306a36Sopenharmony_ci}
200062306a36Sopenharmony_ci
200162306a36Sopenharmony_cistatic inline bool is_sync_state(enum drbd_conns connection_state)
200262306a36Sopenharmony_ci{
200362306a36Sopenharmony_ci	return	is_sync_source_state(connection_state) ||
200462306a36Sopenharmony_ci		is_sync_target_state(connection_state);
200562306a36Sopenharmony_ci}
200662306a36Sopenharmony_ci
200762306a36Sopenharmony_ci/**
200862306a36Sopenharmony_ci * get_ldev() - Increase the ref count on device->ldev. Returns 0 if there is no ldev
200962306a36Sopenharmony_ci * @_device:		DRBD device.
201062306a36Sopenharmony_ci * @_min_state:		Minimum device state required for success.
201162306a36Sopenharmony_ci *
201262306a36Sopenharmony_ci * You have to call put_ldev() when finished working with device->ldev.
201362306a36Sopenharmony_ci */
201462306a36Sopenharmony_ci#define get_ldev_if_state(_device, _min_state)				\
201562306a36Sopenharmony_ci	(_get_ldev_if_state((_device), (_min_state)) ?			\
201662306a36Sopenharmony_ci	 ({ __acquire(x); true; }) : false)
201762306a36Sopenharmony_ci#define get_ldev(_device) get_ldev_if_state(_device, D_INCONSISTENT)
201862306a36Sopenharmony_ci
201962306a36Sopenharmony_cistatic inline void put_ldev(struct drbd_device *device)
202062306a36Sopenharmony_ci{
202162306a36Sopenharmony_ci	enum drbd_disk_state disk_state = device->state.disk;
202262306a36Sopenharmony_ci	/* We must check the state *before* the atomic_dec becomes visible,
202362306a36Sopenharmony_ci	 * or we have a theoretical race where someone hitting zero,
202462306a36Sopenharmony_ci	 * while state still D_FAILED, will then see D_DISKLESS in the
202562306a36Sopenharmony_ci	 * condition below and calling into destroy, where he must not, yet. */
202662306a36Sopenharmony_ci	int i = atomic_dec_return(&device->local_cnt);
202762306a36Sopenharmony_ci
202862306a36Sopenharmony_ci	/* This may be called from some endio handler,
202962306a36Sopenharmony_ci	 * so we must not sleep here. */
203062306a36Sopenharmony_ci
203162306a36Sopenharmony_ci	__release(local);
203262306a36Sopenharmony_ci	D_ASSERT(device, i >= 0);
203362306a36Sopenharmony_ci	if (i == 0) {
203462306a36Sopenharmony_ci		if (disk_state == D_DISKLESS)
203562306a36Sopenharmony_ci			/* even internal references gone, safe to destroy */
203662306a36Sopenharmony_ci			drbd_device_post_work(device, DESTROY_DISK);
203762306a36Sopenharmony_ci		if (disk_state == D_FAILED)
203862306a36Sopenharmony_ci			/* all application IO references gone. */
203962306a36Sopenharmony_ci			if (!test_and_set_bit(GOING_DISKLESS, &device->flags))
204062306a36Sopenharmony_ci				drbd_device_post_work(device, GO_DISKLESS);
204162306a36Sopenharmony_ci		wake_up(&device->misc_wait);
204262306a36Sopenharmony_ci	}
204362306a36Sopenharmony_ci}
204462306a36Sopenharmony_ci
204562306a36Sopenharmony_ci#ifndef __CHECKER__
204662306a36Sopenharmony_cistatic inline int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins)
204762306a36Sopenharmony_ci{
204862306a36Sopenharmony_ci	int io_allowed;
204962306a36Sopenharmony_ci
205062306a36Sopenharmony_ci	/* never get a reference while D_DISKLESS */
205162306a36Sopenharmony_ci	if (device->state.disk == D_DISKLESS)
205262306a36Sopenharmony_ci		return 0;
205362306a36Sopenharmony_ci
205462306a36Sopenharmony_ci	atomic_inc(&device->local_cnt);
205562306a36Sopenharmony_ci	io_allowed = (device->state.disk >= mins);
205662306a36Sopenharmony_ci	if (!io_allowed)
205762306a36Sopenharmony_ci		put_ldev(device);
205862306a36Sopenharmony_ci	return io_allowed;
205962306a36Sopenharmony_ci}
206062306a36Sopenharmony_ci#else
206162306a36Sopenharmony_ciextern int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins);
206262306a36Sopenharmony_ci#endif
206362306a36Sopenharmony_ci
206462306a36Sopenharmony_ci/* this throttles on-the-fly application requests
206562306a36Sopenharmony_ci * according to max_buffers settings;
206662306a36Sopenharmony_ci * maybe re-implement using semaphores? */
206762306a36Sopenharmony_cistatic inline int drbd_get_max_buffers(struct drbd_device *device)
206862306a36Sopenharmony_ci{
206962306a36Sopenharmony_ci	struct net_conf *nc;
207062306a36Sopenharmony_ci	int mxb;
207162306a36Sopenharmony_ci
207262306a36Sopenharmony_ci	rcu_read_lock();
207362306a36Sopenharmony_ci	nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
207462306a36Sopenharmony_ci	mxb = nc ? nc->max_buffers : 1000000;  /* arbitrary limit on open requests */
207562306a36Sopenharmony_ci	rcu_read_unlock();
207662306a36Sopenharmony_ci
207762306a36Sopenharmony_ci	return mxb;
207862306a36Sopenharmony_ci}
207962306a36Sopenharmony_ci
208062306a36Sopenharmony_cistatic inline int drbd_state_is_stable(struct drbd_device *device)
208162306a36Sopenharmony_ci{
208262306a36Sopenharmony_ci	union drbd_dev_state s = device->state;
208362306a36Sopenharmony_ci
208462306a36Sopenharmony_ci	/* DO NOT add a default clause, we want the compiler to warn us
208562306a36Sopenharmony_ci	 * for any newly introduced state we may have forgotten to add here */
208662306a36Sopenharmony_ci
208762306a36Sopenharmony_ci	switch ((enum drbd_conns)s.conn) {
208862306a36Sopenharmony_ci	/* new io only accepted when there is no connection, ... */
208962306a36Sopenharmony_ci	case C_STANDALONE:
209062306a36Sopenharmony_ci	case C_WF_CONNECTION:
209162306a36Sopenharmony_ci	/* ... or there is a well established connection. */
209262306a36Sopenharmony_ci	case C_CONNECTED:
209362306a36Sopenharmony_ci	case C_SYNC_SOURCE:
209462306a36Sopenharmony_ci	case C_SYNC_TARGET:
209562306a36Sopenharmony_ci	case C_VERIFY_S:
209662306a36Sopenharmony_ci	case C_VERIFY_T:
209762306a36Sopenharmony_ci	case C_PAUSED_SYNC_S:
209862306a36Sopenharmony_ci	case C_PAUSED_SYNC_T:
209962306a36Sopenharmony_ci	case C_AHEAD:
210062306a36Sopenharmony_ci	case C_BEHIND:
210162306a36Sopenharmony_ci		/* transitional states, IO allowed */
210262306a36Sopenharmony_ci	case C_DISCONNECTING:
210362306a36Sopenharmony_ci	case C_UNCONNECTED:
210462306a36Sopenharmony_ci	case C_TIMEOUT:
210562306a36Sopenharmony_ci	case C_BROKEN_PIPE:
210662306a36Sopenharmony_ci	case C_NETWORK_FAILURE:
210762306a36Sopenharmony_ci	case C_PROTOCOL_ERROR:
210862306a36Sopenharmony_ci	case C_TEAR_DOWN:
210962306a36Sopenharmony_ci	case C_WF_REPORT_PARAMS:
211062306a36Sopenharmony_ci	case C_STARTING_SYNC_S:
211162306a36Sopenharmony_ci	case C_STARTING_SYNC_T:
211262306a36Sopenharmony_ci		break;
211362306a36Sopenharmony_ci
211462306a36Sopenharmony_ci		/* Allow IO in BM exchange states with new protocols */
211562306a36Sopenharmony_ci	case C_WF_BITMAP_S:
211662306a36Sopenharmony_ci		if (first_peer_device(device)->connection->agreed_pro_version < 96)
211762306a36Sopenharmony_ci			return 0;
211862306a36Sopenharmony_ci		break;
211962306a36Sopenharmony_ci
212062306a36Sopenharmony_ci		/* no new io accepted in these states */
212162306a36Sopenharmony_ci	case C_WF_BITMAP_T:
212262306a36Sopenharmony_ci	case C_WF_SYNC_UUID:
212362306a36Sopenharmony_ci	case C_MASK:
212462306a36Sopenharmony_ci		/* not "stable" */
212562306a36Sopenharmony_ci		return 0;
212662306a36Sopenharmony_ci	}
212762306a36Sopenharmony_ci
212862306a36Sopenharmony_ci	switch ((enum drbd_disk_state)s.disk) {
212962306a36Sopenharmony_ci	case D_DISKLESS:
213062306a36Sopenharmony_ci	case D_INCONSISTENT:
213162306a36Sopenharmony_ci	case D_OUTDATED:
213262306a36Sopenharmony_ci	case D_CONSISTENT:
213362306a36Sopenharmony_ci	case D_UP_TO_DATE:
213462306a36Sopenharmony_ci	case D_FAILED:
213562306a36Sopenharmony_ci		/* disk state is stable as well. */
213662306a36Sopenharmony_ci		break;
213762306a36Sopenharmony_ci
213862306a36Sopenharmony_ci	/* no new io accepted during transitional states */
213962306a36Sopenharmony_ci	case D_ATTACHING:
214062306a36Sopenharmony_ci	case D_NEGOTIATING:
214162306a36Sopenharmony_ci	case D_UNKNOWN:
214262306a36Sopenharmony_ci	case D_MASK:
214362306a36Sopenharmony_ci		/* not "stable" */
214462306a36Sopenharmony_ci		return 0;
214562306a36Sopenharmony_ci	}
214662306a36Sopenharmony_ci
214762306a36Sopenharmony_ci	return 1;
214862306a36Sopenharmony_ci}
214962306a36Sopenharmony_ci
215062306a36Sopenharmony_cistatic inline int drbd_suspended(struct drbd_device *device)
215162306a36Sopenharmony_ci{
215262306a36Sopenharmony_ci	struct drbd_resource *resource = device->resource;
215362306a36Sopenharmony_ci
215462306a36Sopenharmony_ci	return resource->susp || resource->susp_fen || resource->susp_nod;
215562306a36Sopenharmony_ci}
215662306a36Sopenharmony_ci
215762306a36Sopenharmony_cistatic inline bool may_inc_ap_bio(struct drbd_device *device)
215862306a36Sopenharmony_ci{
215962306a36Sopenharmony_ci	int mxb = drbd_get_max_buffers(device);
216062306a36Sopenharmony_ci
216162306a36Sopenharmony_ci	if (drbd_suspended(device))
216262306a36Sopenharmony_ci		return false;
216362306a36Sopenharmony_ci	if (atomic_read(&device->suspend_cnt))
216462306a36Sopenharmony_ci		return false;
216562306a36Sopenharmony_ci
216662306a36Sopenharmony_ci	/* to avoid potential deadlock or bitmap corruption,
216762306a36Sopenharmony_ci	 * in various places, we only allow new application io
216862306a36Sopenharmony_ci	 * to start during "stable" states. */
216962306a36Sopenharmony_ci
217062306a36Sopenharmony_ci	/* no new io accepted when attaching or detaching the disk */
217162306a36Sopenharmony_ci	if (!drbd_state_is_stable(device))
217262306a36Sopenharmony_ci		return false;
217362306a36Sopenharmony_ci
217462306a36Sopenharmony_ci	/* since some older kernels don't have atomic_add_unless,
217562306a36Sopenharmony_ci	 * and we are within the spinlock anyways, we have this workaround.  */
217662306a36Sopenharmony_ci	if (atomic_read(&device->ap_bio_cnt) > mxb)
217762306a36Sopenharmony_ci		return false;
217862306a36Sopenharmony_ci	if (test_bit(BITMAP_IO, &device->flags))
217962306a36Sopenharmony_ci		return false;
218062306a36Sopenharmony_ci	return true;
218162306a36Sopenharmony_ci}
218262306a36Sopenharmony_ci
218362306a36Sopenharmony_cistatic inline bool inc_ap_bio_cond(struct drbd_device *device)
218462306a36Sopenharmony_ci{
218562306a36Sopenharmony_ci	bool rv = false;
218662306a36Sopenharmony_ci
218762306a36Sopenharmony_ci	spin_lock_irq(&device->resource->req_lock);
218862306a36Sopenharmony_ci	rv = may_inc_ap_bio(device);
218962306a36Sopenharmony_ci	if (rv)
219062306a36Sopenharmony_ci		atomic_inc(&device->ap_bio_cnt);
219162306a36Sopenharmony_ci	spin_unlock_irq(&device->resource->req_lock);
219262306a36Sopenharmony_ci
219362306a36Sopenharmony_ci	return rv;
219462306a36Sopenharmony_ci}
219562306a36Sopenharmony_ci
219662306a36Sopenharmony_cistatic inline void inc_ap_bio(struct drbd_device *device)
219762306a36Sopenharmony_ci{
219862306a36Sopenharmony_ci	/* we wait here
219962306a36Sopenharmony_ci	 *    as long as the device is suspended
220062306a36Sopenharmony_ci	 *    until the bitmap is no longer on the fly during connection
220162306a36Sopenharmony_ci	 *    handshake as long as we would exceed the max_buffer limit.
220262306a36Sopenharmony_ci	 *
220362306a36Sopenharmony_ci	 * to avoid races with the reconnect code,
220462306a36Sopenharmony_ci	 * we need to atomic_inc within the spinlock. */
220562306a36Sopenharmony_ci
220662306a36Sopenharmony_ci	wait_event(device->misc_wait, inc_ap_bio_cond(device));
220762306a36Sopenharmony_ci}
220862306a36Sopenharmony_ci
220962306a36Sopenharmony_cistatic inline void dec_ap_bio(struct drbd_device *device)
221062306a36Sopenharmony_ci{
221162306a36Sopenharmony_ci	int mxb = drbd_get_max_buffers(device);
221262306a36Sopenharmony_ci	int ap_bio = atomic_dec_return(&device->ap_bio_cnt);
221362306a36Sopenharmony_ci
221462306a36Sopenharmony_ci	D_ASSERT(device, ap_bio >= 0);
221562306a36Sopenharmony_ci
221662306a36Sopenharmony_ci	if (ap_bio == 0 && test_bit(BITMAP_IO, &device->flags)) {
221762306a36Sopenharmony_ci		if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags))
221862306a36Sopenharmony_ci			drbd_queue_work(&first_peer_device(device)->
221962306a36Sopenharmony_ci				connection->sender_work,
222062306a36Sopenharmony_ci				&device->bm_io_work.w);
222162306a36Sopenharmony_ci	}
222262306a36Sopenharmony_ci
222362306a36Sopenharmony_ci	/* this currently does wake_up for every dec_ap_bio!
222462306a36Sopenharmony_ci	 * maybe rather introduce some type of hysteresis?
222562306a36Sopenharmony_ci	 * e.g. (ap_bio == mxb/2 || ap_bio == 0) ? */
222662306a36Sopenharmony_ci	if (ap_bio < mxb)
222762306a36Sopenharmony_ci		wake_up(&device->misc_wait);
222862306a36Sopenharmony_ci}
222962306a36Sopenharmony_ci
223062306a36Sopenharmony_cistatic inline bool verify_can_do_stop_sector(struct drbd_device *device)
223162306a36Sopenharmony_ci{
223262306a36Sopenharmony_ci	return first_peer_device(device)->connection->agreed_pro_version >= 97 &&
223362306a36Sopenharmony_ci		first_peer_device(device)->connection->agreed_pro_version != 100;
223462306a36Sopenharmony_ci}
223562306a36Sopenharmony_ci
223662306a36Sopenharmony_cistatic inline int drbd_set_ed_uuid(struct drbd_device *device, u64 val)
223762306a36Sopenharmony_ci{
223862306a36Sopenharmony_ci	int changed = device->ed_uuid != val;
223962306a36Sopenharmony_ci	device->ed_uuid = val;
224062306a36Sopenharmony_ci	return changed;
224162306a36Sopenharmony_ci}
224262306a36Sopenharmony_ci
224362306a36Sopenharmony_cistatic inline int drbd_queue_order_type(struct drbd_device *device)
224462306a36Sopenharmony_ci{
224562306a36Sopenharmony_ci	/* sorry, we currently have no working implementation
224662306a36Sopenharmony_ci	 * of distributed TCQ stuff */
224762306a36Sopenharmony_ci#ifndef QUEUE_ORDERED_NONE
224862306a36Sopenharmony_ci#define QUEUE_ORDERED_NONE 0
224962306a36Sopenharmony_ci#endif
225062306a36Sopenharmony_ci	return QUEUE_ORDERED_NONE;
225162306a36Sopenharmony_ci}
225262306a36Sopenharmony_ci
225362306a36Sopenharmony_cistatic inline struct drbd_connection *first_connection(struct drbd_resource *resource)
225462306a36Sopenharmony_ci{
225562306a36Sopenharmony_ci	return list_first_entry_or_null(&resource->connections,
225662306a36Sopenharmony_ci				struct drbd_connection, connections);
225762306a36Sopenharmony_ci}
225862306a36Sopenharmony_ci
225962306a36Sopenharmony_ci#endif
2260