drbd_int.h 79 KB

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