drbd_int.h 76 KB

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