drbd_int.h 80 KB

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