drbd_int.h 81 KB

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