drbd_int.h 76 KB

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