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