drbd_int.h 80 KB

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