drbd_int.h 73 KB

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