drbd_int.h 81 KB

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