drbd_main.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231
  1. /*
  2. drbd.c
  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. Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
  8. from Logicworks, Inc. for making SDP replication support possible.
  9. drbd is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2, or (at your option)
  12. any later version.
  13. drbd is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with drbd; see the file COPYING. If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/drbd.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/types.h>
  25. #include <net/sock.h>
  26. #include <linux/ctype.h>
  27. #include <linux/mutex.h>
  28. #include <linux/fs.h>
  29. #include <linux/file.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/init.h>
  32. #include <linux/mm.h>
  33. #include <linux/memcontrol.h>
  34. #include <linux/mm_inline.h>
  35. #include <linux/slab.h>
  36. #include <linux/random.h>
  37. #include <linux/reboot.h>
  38. #include <linux/notifier.h>
  39. #include <linux/kthread.h>
  40. #define __KERNEL_SYSCALLS__
  41. #include <linux/unistd.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/drbd_limits.h>
  44. #include "drbd_int.h"
  45. #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
  46. #include "drbd_vli.h"
  47. static DEFINE_MUTEX(drbd_main_mutex);
  48. int drbdd_init(struct drbd_thread *);
  49. int drbd_worker(struct drbd_thread *);
  50. int drbd_asender(struct drbd_thread *);
  51. int drbd_init(void);
  52. static int drbd_open(struct block_device *bdev, fmode_t mode);
  53. static int drbd_release(struct gendisk *gd, fmode_t mode);
  54. static int w_md_sync(struct drbd_work *w, int unused);
  55. static void md_sync_timer_fn(unsigned long data);
  56. static int w_bitmap_io(struct drbd_work *w, int unused);
  57. static int w_go_diskless(struct drbd_work *w, int unused);
  58. MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
  59. "Lars Ellenberg <lars@linbit.com>");
  60. MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
  61. MODULE_VERSION(REL_VERSION);
  62. MODULE_LICENSE("GPL");
  63. MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices ("
  64. __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
  65. MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
  66. #include <linux/moduleparam.h>
  67. /* allow_open_on_secondary */
  68. MODULE_PARM_DESC(allow_oos, "DONT USE!");
  69. /* thanks to these macros, if compiled into the kernel (not-module),
  70. * this becomes the boot parameter drbd.minor_count */
  71. module_param(minor_count, uint, 0444);
  72. module_param(disable_sendpage, bool, 0644);
  73. module_param(allow_oos, bool, 0);
  74. module_param(proc_details, int, 0644);
  75. #ifdef CONFIG_DRBD_FAULT_INJECTION
  76. int enable_faults;
  77. int fault_rate;
  78. static int fault_count;
  79. int fault_devs;
  80. /* bitmap of enabled faults */
  81. module_param(enable_faults, int, 0664);
  82. /* fault rate % value - applies to all enabled faults */
  83. module_param(fault_rate, int, 0664);
  84. /* count of faults inserted */
  85. module_param(fault_count, int, 0664);
  86. /* bitmap of devices to insert faults on */
  87. module_param(fault_devs, int, 0644);
  88. #endif
  89. /* module parameter, defined */
  90. unsigned int minor_count = DRBD_MINOR_COUNT_DEF;
  91. int disable_sendpage;
  92. int allow_oos;
  93. int proc_details; /* Detail level in proc drbd*/
  94. /* Module parameter for setting the user mode helper program
  95. * to run. Default is /sbin/drbdadm */
  96. char usermode_helper[80] = "/sbin/drbdadm";
  97. module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644);
  98. /* in 2.6.x, our device mapping and config info contains our virtual gendisks
  99. * as member "struct gendisk *vdisk;"
  100. */
  101. struct idr minors;
  102. struct list_head drbd_tconns; /* list of struct drbd_tconn */
  103. DEFINE_MUTEX(drbd_cfg_mutex);
  104. struct kmem_cache *drbd_request_cache;
  105. struct kmem_cache *drbd_ee_cache; /* peer requests */
  106. struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
  107. struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
  108. mempool_t *drbd_request_mempool;
  109. mempool_t *drbd_ee_mempool;
  110. mempool_t *drbd_md_io_page_pool;
  111. struct bio_set *drbd_md_io_bio_set;
  112. /* I do not use a standard mempool, because:
  113. 1) I want to hand out the pre-allocated objects first.
  114. 2) I want to be able to interrupt sleeping allocation with a signal.
  115. Note: This is a single linked list, the next pointer is the private
  116. member of struct page.
  117. */
  118. struct page *drbd_pp_pool;
  119. spinlock_t drbd_pp_lock;
  120. int drbd_pp_vacant;
  121. wait_queue_head_t drbd_pp_wait;
  122. DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
  123. static const struct block_device_operations drbd_ops = {
  124. .owner = THIS_MODULE,
  125. .open = drbd_open,
  126. .release = drbd_release,
  127. };
  128. static void bio_destructor_drbd(struct bio *bio)
  129. {
  130. bio_free(bio, drbd_md_io_bio_set);
  131. }
  132. struct bio *bio_alloc_drbd(gfp_t gfp_mask)
  133. {
  134. struct bio *bio;
  135. if (!drbd_md_io_bio_set)
  136. return bio_alloc(gfp_mask, 1);
  137. bio = bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set);
  138. if (!bio)
  139. return NULL;
  140. bio->bi_destructor = bio_destructor_drbd;
  141. return bio;
  142. }
  143. #ifdef __CHECKER__
  144. /* When checking with sparse, and this is an inline function, sparse will
  145. give tons of false positives. When this is a real functions sparse works.
  146. */
  147. int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
  148. {
  149. int io_allowed;
  150. atomic_inc(&mdev->local_cnt);
  151. io_allowed = (mdev->state.disk >= mins);
  152. if (!io_allowed) {
  153. if (atomic_dec_and_test(&mdev->local_cnt))
  154. wake_up(&mdev->misc_wait);
  155. }
  156. return io_allowed;
  157. }
  158. #endif
  159. /**
  160. * DOC: The transfer log
  161. *
  162. * The transfer log is a single linked list of &struct drbd_tl_epoch objects.
  163. * mdev->tconn->newest_tle points to the head, mdev->tconn->oldest_tle points to the tail
  164. * of the list. There is always at least one &struct drbd_tl_epoch object.
  165. *
  166. * Each &struct drbd_tl_epoch has a circular double linked list of requests
  167. * attached.
  168. */
  169. static int tl_init(struct drbd_tconn *tconn)
  170. {
  171. struct drbd_tl_epoch *b;
  172. /* during device minor initialization, we may well use GFP_KERNEL */
  173. b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL);
  174. if (!b)
  175. return 0;
  176. INIT_LIST_HEAD(&b->requests);
  177. INIT_LIST_HEAD(&b->w.list);
  178. b->next = NULL;
  179. b->br_number = 4711;
  180. b->n_writes = 0;
  181. b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
  182. tconn->oldest_tle = b;
  183. tconn->newest_tle = b;
  184. INIT_LIST_HEAD(&tconn->out_of_sequence_requests);
  185. return 1;
  186. }
  187. static void tl_cleanup(struct drbd_tconn *tconn)
  188. {
  189. if (tconn->oldest_tle != tconn->newest_tle)
  190. conn_err(tconn, "ASSERT FAILED: oldest_tle == newest_tle\n");
  191. if (!list_empty(&tconn->out_of_sequence_requests))
  192. conn_err(tconn, "ASSERT FAILED: list_empty(out_of_sequence_requests)\n");
  193. kfree(tconn->oldest_tle);
  194. tconn->oldest_tle = NULL;
  195. kfree(tconn->unused_spare_tle);
  196. tconn->unused_spare_tle = NULL;
  197. }
  198. /**
  199. * _tl_add_barrier() - Adds a barrier to the transfer log
  200. * @mdev: DRBD device.
  201. * @new: Barrier to be added before the current head of the TL.
  202. *
  203. * The caller must hold the req_lock.
  204. */
  205. void _tl_add_barrier(struct drbd_tconn *tconn, struct drbd_tl_epoch *new)
  206. {
  207. struct drbd_tl_epoch *newest_before;
  208. INIT_LIST_HEAD(&new->requests);
  209. INIT_LIST_HEAD(&new->w.list);
  210. new->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
  211. new->next = NULL;
  212. new->n_writes = 0;
  213. newest_before = tconn->newest_tle;
  214. /* never send a barrier number == 0, because that is special-cased
  215. * when using TCQ for our write ordering code */
  216. new->br_number = (newest_before->br_number+1) ?: 1;
  217. if (tconn->newest_tle != new) {
  218. tconn->newest_tle->next = new;
  219. tconn->newest_tle = new;
  220. }
  221. }
  222. /**
  223. * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL
  224. * @mdev: DRBD device.
  225. * @barrier_nr: Expected identifier of the DRBD write barrier packet.
  226. * @set_size: Expected number of requests before that barrier.
  227. *
  228. * In case the passed barrier_nr or set_size does not match the oldest
  229. * &struct drbd_tl_epoch objects this function will cause a termination
  230. * of the connection.
  231. */
  232. void tl_release(struct drbd_tconn *tconn, unsigned int barrier_nr,
  233. unsigned int set_size)
  234. {
  235. struct drbd_conf *mdev;
  236. struct drbd_tl_epoch *b, *nob; /* next old barrier */
  237. struct list_head *le, *tle;
  238. struct drbd_request *r;
  239. spin_lock_irq(&tconn->req_lock);
  240. b = tconn->oldest_tle;
  241. /* first some paranoia code */
  242. if (b == NULL) {
  243. conn_err(tconn, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
  244. barrier_nr);
  245. goto bail;
  246. }
  247. if (b->br_number != barrier_nr) {
  248. conn_err(tconn, "BAD! BarrierAck #%u received, expected #%u!\n",
  249. barrier_nr, b->br_number);
  250. goto bail;
  251. }
  252. if (b->n_writes != set_size) {
  253. conn_err(tconn, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
  254. barrier_nr, set_size, b->n_writes);
  255. goto bail;
  256. }
  257. /* Clean up list of requests processed during current epoch */
  258. list_for_each_safe(le, tle, &b->requests) {
  259. r = list_entry(le, struct drbd_request, tl_requests);
  260. _req_mod(r, BARRIER_ACKED);
  261. }
  262. /* There could be requests on the list waiting for completion
  263. of the write to the local disk. To avoid corruptions of
  264. slab's data structures we have to remove the lists head.
  265. Also there could have been a barrier ack out of sequence, overtaking
  266. the write acks - which would be a bug and violating write ordering.
  267. To not deadlock in case we lose connection while such requests are
  268. still pending, we need some way to find them for the
  269. _req_mode(CONNECTION_LOST_WHILE_PENDING).
  270. These have been list_move'd to the out_of_sequence_requests list in
  271. _req_mod(, BARRIER_ACKED) above.
  272. */
  273. list_del_init(&b->requests);
  274. mdev = b->w.mdev;
  275. nob = b->next;
  276. if (test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
  277. _tl_add_barrier(tconn, b);
  278. if (nob)
  279. tconn->oldest_tle = nob;
  280. /* if nob == NULL b was the only barrier, and becomes the new
  281. barrier. Therefore tconn->oldest_tle points already to b */
  282. } else {
  283. D_ASSERT(nob != NULL);
  284. tconn->oldest_tle = nob;
  285. kfree(b);
  286. }
  287. spin_unlock_irq(&tconn->req_lock);
  288. dec_ap_pending(mdev);
  289. return;
  290. bail:
  291. spin_unlock_irq(&tconn->req_lock);
  292. conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
  293. }
  294. /**
  295. * _tl_restart() - Walks the transfer log, and applies an action to all requests
  296. * @mdev: DRBD device.
  297. * @what: The action/event to perform with all request objects
  298. *
  299. * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
  300. * RESTART_FROZEN_DISK_IO.
  301. */
  302. void _tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
  303. {
  304. struct drbd_tl_epoch *b, *tmp, **pn;
  305. struct list_head *le, *tle, carry_reads;
  306. struct drbd_request *req;
  307. int rv, n_writes, n_reads;
  308. b = tconn->oldest_tle;
  309. pn = &tconn->oldest_tle;
  310. while (b) {
  311. n_writes = 0;
  312. n_reads = 0;
  313. INIT_LIST_HEAD(&carry_reads);
  314. list_for_each_safe(le, tle, &b->requests) {
  315. req = list_entry(le, struct drbd_request, tl_requests);
  316. rv = _req_mod(req, what);
  317. n_writes += (rv & MR_WRITE) >> MR_WRITE_SHIFT;
  318. n_reads += (rv & MR_READ) >> MR_READ_SHIFT;
  319. }
  320. tmp = b->next;
  321. if (n_writes) {
  322. if (what == RESEND) {
  323. b->n_writes = n_writes;
  324. if (b->w.cb == NULL) {
  325. b->w.cb = w_send_barrier;
  326. inc_ap_pending(b->w.mdev);
  327. set_bit(CREATE_BARRIER, &b->w.mdev->flags);
  328. }
  329. drbd_queue_work(&tconn->data.work, &b->w);
  330. }
  331. pn = &b->next;
  332. } else {
  333. if (n_reads)
  334. list_add(&carry_reads, &b->requests);
  335. /* there could still be requests on that ring list,
  336. * in case local io is still pending */
  337. list_del(&b->requests);
  338. /* dec_ap_pending corresponding to queue_barrier.
  339. * the newest barrier may not have been queued yet,
  340. * in which case w.cb is still NULL. */
  341. if (b->w.cb != NULL)
  342. dec_ap_pending(b->w.mdev);
  343. if (b == tconn->newest_tle) {
  344. /* recycle, but reinit! */
  345. if (tmp != NULL)
  346. conn_err(tconn, "ASSERT FAILED tmp == NULL");
  347. INIT_LIST_HEAD(&b->requests);
  348. list_splice(&carry_reads, &b->requests);
  349. INIT_LIST_HEAD(&b->w.list);
  350. b->w.cb = NULL;
  351. b->br_number = net_random();
  352. b->n_writes = 0;
  353. *pn = b;
  354. break;
  355. }
  356. *pn = tmp;
  357. kfree(b);
  358. }
  359. b = tmp;
  360. list_splice(&carry_reads, &b->requests);
  361. }
  362. }
  363. /**
  364. * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
  365. * @mdev: DRBD device.
  366. *
  367. * This is called after the connection to the peer was lost. The storage covered
  368. * by the requests on the transfer gets marked as our of sync. Called from the
  369. * receiver thread and the worker thread.
  370. */
  371. void tl_clear(struct drbd_tconn *tconn)
  372. {
  373. struct drbd_conf *mdev;
  374. struct list_head *le, *tle;
  375. struct drbd_request *r;
  376. int vnr;
  377. spin_lock_irq(&tconn->req_lock);
  378. _tl_restart(tconn, CONNECTION_LOST_WHILE_PENDING);
  379. /* we expect this list to be empty. */
  380. if (!list_empty(&tconn->out_of_sequence_requests))
  381. conn_err(tconn, "ASSERT FAILED list_empty(&out_of_sequence_requests)\n");
  382. /* but just in case, clean it up anyways! */
  383. list_for_each_safe(le, tle, &tconn->out_of_sequence_requests) {
  384. r = list_entry(le, struct drbd_request, tl_requests);
  385. /* It would be nice to complete outside of spinlock.
  386. * But this is easier for now. */
  387. _req_mod(r, CONNECTION_LOST_WHILE_PENDING);
  388. }
  389. /* ensure bit indicating barrier is required is clear */
  390. idr_for_each_entry(&tconn->volumes, mdev, vnr)
  391. clear_bit(CREATE_BARRIER, &mdev->flags);
  392. spin_unlock_irq(&tconn->req_lock);
  393. }
  394. void tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
  395. {
  396. spin_lock_irq(&tconn->req_lock);
  397. _tl_restart(tconn, what);
  398. spin_unlock_irq(&tconn->req_lock);
  399. }
  400. static int drbd_thread_setup(void *arg)
  401. {
  402. struct drbd_thread *thi = (struct drbd_thread *) arg;
  403. struct drbd_tconn *tconn = thi->tconn;
  404. unsigned long flags;
  405. int retval;
  406. snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
  407. thi->name[0], thi->tconn->name);
  408. restart:
  409. retval = thi->function(thi);
  410. spin_lock_irqsave(&thi->t_lock, flags);
  411. /* if the receiver has been "EXITING", the last thing it did
  412. * was set the conn state to "StandAlone",
  413. * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
  414. * and receiver thread will be "started".
  415. * drbd_thread_start needs to set "RESTARTING" in that case.
  416. * t_state check and assignment needs to be within the same spinlock,
  417. * so either thread_start sees EXITING, and can remap to RESTARTING,
  418. * or thread_start see NONE, and can proceed as normal.
  419. */
  420. if (thi->t_state == RESTARTING) {
  421. conn_info(tconn, "Restarting %s thread\n", thi->name);
  422. thi->t_state = RUNNING;
  423. spin_unlock_irqrestore(&thi->t_lock, flags);
  424. goto restart;
  425. }
  426. thi->task = NULL;
  427. thi->t_state = NONE;
  428. smp_mb();
  429. complete(&thi->stop);
  430. spin_unlock_irqrestore(&thi->t_lock, flags);
  431. conn_info(tconn, "Terminating %s\n", current->comm);
  432. /* Release mod reference taken when thread was started */
  433. module_put(THIS_MODULE);
  434. return retval;
  435. }
  436. static void drbd_thread_init(struct drbd_tconn *tconn, struct drbd_thread *thi,
  437. int (*func) (struct drbd_thread *), char *name)
  438. {
  439. spin_lock_init(&thi->t_lock);
  440. thi->task = NULL;
  441. thi->t_state = NONE;
  442. thi->function = func;
  443. thi->tconn = tconn;
  444. strncpy(thi->name, name, ARRAY_SIZE(thi->name));
  445. }
  446. int drbd_thread_start(struct drbd_thread *thi)
  447. {
  448. struct drbd_tconn *tconn = thi->tconn;
  449. struct task_struct *nt;
  450. unsigned long flags;
  451. /* is used from state engine doing drbd_thread_stop_nowait,
  452. * while holding the req lock irqsave */
  453. spin_lock_irqsave(&thi->t_lock, flags);
  454. switch (thi->t_state) {
  455. case NONE:
  456. conn_info(tconn, "Starting %s thread (from %s [%d])\n",
  457. thi->name, current->comm, current->pid);
  458. /* Get ref on module for thread - this is released when thread exits */
  459. if (!try_module_get(THIS_MODULE)) {
  460. conn_err(tconn, "Failed to get module reference in drbd_thread_start\n");
  461. spin_unlock_irqrestore(&thi->t_lock, flags);
  462. return false;
  463. }
  464. init_completion(&thi->stop);
  465. thi->reset_cpu_mask = 1;
  466. thi->t_state = RUNNING;
  467. spin_unlock_irqrestore(&thi->t_lock, flags);
  468. flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
  469. nt = kthread_create(drbd_thread_setup, (void *) thi,
  470. "drbd_%c_%s", thi->name[0], thi->tconn->name);
  471. if (IS_ERR(nt)) {
  472. conn_err(tconn, "Couldn't start thread\n");
  473. module_put(THIS_MODULE);
  474. return false;
  475. }
  476. spin_lock_irqsave(&thi->t_lock, flags);
  477. thi->task = nt;
  478. thi->t_state = RUNNING;
  479. spin_unlock_irqrestore(&thi->t_lock, flags);
  480. wake_up_process(nt);
  481. break;
  482. case EXITING:
  483. thi->t_state = RESTARTING;
  484. conn_info(tconn, "Restarting %s thread (from %s [%d])\n",
  485. thi->name, current->comm, current->pid);
  486. /* fall through */
  487. case RUNNING:
  488. case RESTARTING:
  489. default:
  490. spin_unlock_irqrestore(&thi->t_lock, flags);
  491. break;
  492. }
  493. return true;
  494. }
  495. void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
  496. {
  497. unsigned long flags;
  498. enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
  499. /* may be called from state engine, holding the req lock irqsave */
  500. spin_lock_irqsave(&thi->t_lock, flags);
  501. if (thi->t_state == NONE) {
  502. spin_unlock_irqrestore(&thi->t_lock, flags);
  503. if (restart)
  504. drbd_thread_start(thi);
  505. return;
  506. }
  507. if (thi->t_state != ns) {
  508. if (thi->task == NULL) {
  509. spin_unlock_irqrestore(&thi->t_lock, flags);
  510. return;
  511. }
  512. thi->t_state = ns;
  513. smp_mb();
  514. init_completion(&thi->stop);
  515. if (thi->task != current)
  516. force_sig(DRBD_SIGKILL, thi->task);
  517. }
  518. spin_unlock_irqrestore(&thi->t_lock, flags);
  519. if (wait)
  520. wait_for_completion(&thi->stop);
  521. }
  522. static struct drbd_thread *drbd_task_to_thread(struct drbd_tconn *tconn, struct task_struct *task)
  523. {
  524. struct drbd_thread *thi =
  525. task == tconn->receiver.task ? &tconn->receiver :
  526. task == tconn->asender.task ? &tconn->asender :
  527. task == tconn->worker.task ? &tconn->worker : NULL;
  528. return thi;
  529. }
  530. char *drbd_task_to_thread_name(struct drbd_tconn *tconn, struct task_struct *task)
  531. {
  532. struct drbd_thread *thi = drbd_task_to_thread(tconn, task);
  533. return thi ? thi->name : task->comm;
  534. }
  535. int conn_lowest_minor(struct drbd_tconn *tconn)
  536. {
  537. int vnr = 0;
  538. struct drbd_conf *mdev;
  539. mdev = idr_get_next(&tconn->volumes, &vnr);
  540. if (!mdev)
  541. return -1;
  542. return mdev_to_minor(mdev);
  543. }
  544. #ifdef CONFIG_SMP
  545. /**
  546. * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
  547. * @mdev: DRBD device.
  548. *
  549. * Forces all threads of a device onto the same CPU. This is beneficial for
  550. * DRBD's performance. May be overwritten by user's configuration.
  551. */
  552. void drbd_calc_cpu_mask(struct drbd_tconn *tconn)
  553. {
  554. int ord, cpu;
  555. /* user override. */
  556. if (cpumask_weight(tconn->cpu_mask))
  557. return;
  558. ord = conn_lowest_minor(tconn) % cpumask_weight(cpu_online_mask);
  559. for_each_online_cpu(cpu) {
  560. if (ord-- == 0) {
  561. cpumask_set_cpu(cpu, tconn->cpu_mask);
  562. return;
  563. }
  564. }
  565. /* should not be reached */
  566. cpumask_setall(tconn->cpu_mask);
  567. }
  568. /**
  569. * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
  570. * @mdev: DRBD device.
  571. * @thi: drbd_thread object
  572. *
  573. * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
  574. * prematurely.
  575. */
  576. void drbd_thread_current_set_cpu(struct drbd_thread *thi)
  577. {
  578. struct task_struct *p = current;
  579. if (!thi->reset_cpu_mask)
  580. return;
  581. thi->reset_cpu_mask = 0;
  582. set_cpus_allowed_ptr(p, thi->tconn->cpu_mask);
  583. }
  584. #endif
  585. static void prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
  586. {
  587. h->magic = cpu_to_be32(DRBD_MAGIC);
  588. h->command = cpu_to_be16(cmd);
  589. h->length = cpu_to_be16(size);
  590. }
  591. static void prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
  592. {
  593. h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
  594. h->command = cpu_to_be16(cmd);
  595. h->length = cpu_to_be32(size);
  596. }
  597. static void _prepare_header(struct drbd_tconn *tconn, int vnr, struct p_header *h,
  598. enum drbd_packet cmd, int size)
  599. {
  600. if (tconn->agreed_pro_version >= 100 || size > DRBD_MAX_SIZE_H80_PACKET)
  601. prepare_header95(&h->h95, cmd, size);
  602. else
  603. prepare_header80(&h->h80, cmd, size);
  604. }
  605. static void prepare_header(struct drbd_conf *mdev, struct p_header *h,
  606. enum drbd_packet cmd, int size)
  607. {
  608. _prepare_header(mdev->tconn, mdev->vnr, h, cmd, size);
  609. }
  610. /* the appropriate socket mutex must be held already */
  611. int _conn_send_cmd(struct drbd_tconn *tconn, int vnr, struct socket *sock,
  612. enum drbd_packet cmd, struct p_header *h, size_t size,
  613. unsigned msg_flags)
  614. {
  615. int err;
  616. _prepare_header(tconn, vnr, h, cmd, size - sizeof(struct p_header));
  617. err = drbd_send_all(tconn, sock, h, size, msg_flags);
  618. if (err && !signal_pending(current))
  619. conn_warn(tconn, "short send %s size=%d\n",
  620. cmdname(cmd), (int)size);
  621. return err;
  622. }
  623. /* don't pass the socket. we may only look at it
  624. * when we hold the appropriate socket mutex.
  625. */
  626. int conn_send_cmd(struct drbd_tconn *tconn, int vnr, struct drbd_socket *sock,
  627. enum drbd_packet cmd, struct p_header *h, size_t size)
  628. {
  629. int err = -EIO;
  630. mutex_lock(&sock->mutex);
  631. if (sock->socket)
  632. err = _conn_send_cmd(tconn, vnr, sock->socket, cmd, h, size, 0);
  633. mutex_unlock(&sock->mutex);
  634. return err;
  635. }
  636. int conn_send_cmd2(struct drbd_tconn *tconn, enum drbd_packet cmd, char *data,
  637. size_t size)
  638. {
  639. struct p_header80 h;
  640. int err;
  641. prepare_header80(&h, cmd, size);
  642. err = drbd_get_data_sock(tconn);
  643. if (!err) {
  644. err = drbd_send_all(tconn, tconn->data.socket, &h, sizeof(h), 0);
  645. if (!err)
  646. err = drbd_send_all(tconn, tconn->data.socket, data, size, 0);
  647. drbd_put_data_sock(tconn);
  648. }
  649. return err;
  650. }
  651. int drbd_send_sync_param(struct drbd_conf *mdev)
  652. {
  653. struct p_rs_param_95 *p;
  654. struct socket *sock;
  655. int size, err;
  656. const int apv = mdev->tconn->agreed_pro_version;
  657. size = apv <= 87 ? sizeof(struct p_rs_param)
  658. : apv == 88 ? sizeof(struct p_rs_param)
  659. + strlen(mdev->tconn->net_conf->verify_alg) + 1
  660. : apv <= 94 ? sizeof(struct p_rs_param_89)
  661. : /* apv >= 95 */ sizeof(struct p_rs_param_95);
  662. /* used from admin command context and receiver/worker context.
  663. * to avoid kmalloc, grab the socket right here,
  664. * then use the pre-allocated sbuf there */
  665. mutex_lock(&mdev->tconn->data.mutex);
  666. sock = mdev->tconn->data.socket;
  667. if (likely(sock != NULL)) {
  668. enum drbd_packet cmd =
  669. apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
  670. p = &mdev->tconn->data.sbuf.rs_param_95;
  671. /* initialize verify_alg and csums_alg */
  672. memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
  673. if (get_ldev(mdev)) {
  674. p->rate = cpu_to_be32(mdev->ldev->dc.resync_rate);
  675. p->c_plan_ahead = cpu_to_be32(mdev->ldev->dc.c_plan_ahead);
  676. p->c_delay_target = cpu_to_be32(mdev->ldev->dc.c_delay_target);
  677. p->c_fill_target = cpu_to_be32(mdev->ldev->dc.c_fill_target);
  678. p->c_max_rate = cpu_to_be32(mdev->ldev->dc.c_max_rate);
  679. put_ldev(mdev);
  680. } else {
  681. p->rate = cpu_to_be32(DRBD_RATE_DEF);
  682. p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF);
  683. p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF);
  684. p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF);
  685. p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF);
  686. }
  687. if (apv >= 88)
  688. strcpy(p->verify_alg, mdev->tconn->net_conf->verify_alg);
  689. if (apv >= 89)
  690. strcpy(p->csums_alg, mdev->tconn->net_conf->csums_alg);
  691. err = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
  692. } else
  693. err = -EIO;
  694. mutex_unlock(&mdev->tconn->data.mutex);
  695. return err;
  696. }
  697. int drbd_send_protocol(struct drbd_tconn *tconn)
  698. {
  699. struct p_protocol *p;
  700. int size, cf, err;
  701. size = sizeof(struct p_protocol);
  702. if (tconn->agreed_pro_version >= 87)
  703. size += strlen(tconn->net_conf->integrity_alg) + 1;
  704. /* we must not recurse into our own queue,
  705. * as that is blocked during handshake */
  706. p = kmalloc(size, GFP_NOIO);
  707. if (p == NULL)
  708. return -ENOMEM;
  709. p->protocol = cpu_to_be32(tconn->net_conf->wire_protocol);
  710. p->after_sb_0p = cpu_to_be32(tconn->net_conf->after_sb_0p);
  711. p->after_sb_1p = cpu_to_be32(tconn->net_conf->after_sb_1p);
  712. p->after_sb_2p = cpu_to_be32(tconn->net_conf->after_sb_2p);
  713. p->two_primaries = cpu_to_be32(tconn->net_conf->two_primaries);
  714. cf = 0;
  715. if (tconn->net_conf->want_lose)
  716. cf |= CF_WANT_LOSE;
  717. if (tconn->net_conf->dry_run) {
  718. if (tconn->agreed_pro_version >= 92)
  719. cf |= CF_DRY_RUN;
  720. else {
  721. conn_err(tconn, "--dry-run is not supported by peer");
  722. kfree(p);
  723. return -EOPNOTSUPP;
  724. }
  725. }
  726. p->conn_flags = cpu_to_be32(cf);
  727. if (tconn->agreed_pro_version >= 87)
  728. strcpy(p->integrity_alg, tconn->net_conf->integrity_alg);
  729. err = conn_send_cmd2(tconn, P_PROTOCOL, p->head.payload, size - sizeof(struct p_header));
  730. kfree(p);
  731. return err;
  732. }
  733. int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
  734. {
  735. struct p_uuids p;
  736. int i;
  737. if (!get_ldev_if_state(mdev, D_NEGOTIATING))
  738. return 0;
  739. for (i = UI_CURRENT; i < UI_SIZE; i++)
  740. p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
  741. mdev->comm_bm_set = drbd_bm_total_weight(mdev);
  742. p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
  743. uuid_flags |= mdev->tconn->net_conf->want_lose ? 1 : 0;
  744. uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
  745. uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
  746. p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
  747. put_ldev(mdev);
  748. return drbd_send_cmd(mdev, &mdev->tconn->data, P_UUIDS, &p.head, sizeof(p));
  749. }
  750. int drbd_send_uuids(struct drbd_conf *mdev)
  751. {
  752. return _drbd_send_uuids(mdev, 0);
  753. }
  754. int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev)
  755. {
  756. return _drbd_send_uuids(mdev, 8);
  757. }
  758. void drbd_print_uuids(struct drbd_conf *mdev, const char *text)
  759. {
  760. if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
  761. u64 *uuid = mdev->ldev->md.uuid;
  762. dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX\n",
  763. text,
  764. (unsigned long long)uuid[UI_CURRENT],
  765. (unsigned long long)uuid[UI_BITMAP],
  766. (unsigned long long)uuid[UI_HISTORY_START],
  767. (unsigned long long)uuid[UI_HISTORY_END]);
  768. put_ldev(mdev);
  769. } else {
  770. dev_info(DEV, "%s effective data uuid: %016llX\n",
  771. text,
  772. (unsigned long long)mdev->ed_uuid);
  773. }
  774. }
  775. void drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
  776. {
  777. struct p_rs_uuid p;
  778. u64 uuid;
  779. D_ASSERT(mdev->state.disk == D_UP_TO_DATE);
  780. uuid = mdev->ldev->md.uuid[UI_BITMAP] + UUID_NEW_BM_OFFSET;
  781. drbd_uuid_set(mdev, UI_BITMAP, uuid);
  782. drbd_print_uuids(mdev, "updated sync UUID");
  783. drbd_md_sync(mdev);
  784. p.uuid = cpu_to_be64(uuid);
  785. drbd_send_cmd(mdev, &mdev->tconn->data, P_SYNC_UUID, &p.head, sizeof(p));
  786. }
  787. int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
  788. {
  789. struct p_sizes p;
  790. sector_t d_size, u_size;
  791. int q_order_type, max_bio_size;
  792. if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
  793. D_ASSERT(mdev->ldev->backing_bdev);
  794. d_size = drbd_get_max_capacity(mdev->ldev);
  795. u_size = mdev->ldev->dc.disk_size;
  796. q_order_type = drbd_queue_order_type(mdev);
  797. max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
  798. max_bio_size = min_t(int, max_bio_size, DRBD_MAX_BIO_SIZE);
  799. put_ldev(mdev);
  800. } else {
  801. d_size = 0;
  802. u_size = 0;
  803. q_order_type = QUEUE_ORDERED_NONE;
  804. max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
  805. }
  806. p.d_size = cpu_to_be64(d_size);
  807. p.u_size = cpu_to_be64(u_size);
  808. p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
  809. p.max_bio_size = cpu_to_be32(max_bio_size);
  810. p.queue_order_type = cpu_to_be16(q_order_type);
  811. p.dds_flags = cpu_to_be16(flags);
  812. return drbd_send_cmd(mdev, &mdev->tconn->data, P_SIZES, &p.head, sizeof(p));
  813. }
  814. /**
  815. * drbd_send_state() - Sends the drbd state to the peer
  816. * @mdev: DRBD device.
  817. */
  818. int drbd_send_state(struct drbd_conf *mdev)
  819. {
  820. struct socket *sock;
  821. struct p_state p;
  822. int err = -EIO;
  823. mutex_lock(&mdev->tconn->data.mutex);
  824. p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
  825. sock = mdev->tconn->data.socket;
  826. if (likely(sock != NULL))
  827. err = _drbd_send_cmd(mdev, sock, P_STATE, &p.head, sizeof(p), 0);
  828. mutex_unlock(&mdev->tconn->data.mutex);
  829. return err;
  830. }
  831. int _conn_send_state_req(struct drbd_tconn *tconn, int vnr, enum drbd_packet cmd,
  832. union drbd_state mask, union drbd_state val)
  833. {
  834. struct p_req_state p;
  835. p.mask = cpu_to_be32(mask.i);
  836. p.val = cpu_to_be32(val.i);
  837. return conn_send_cmd(tconn, vnr, &tconn->data, cmd, &p.head, sizeof(p));
  838. }
  839. void drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode)
  840. {
  841. struct p_req_state_reply p;
  842. p.retcode = cpu_to_be32(retcode);
  843. drbd_send_cmd(mdev, &mdev->tconn->meta, P_STATE_CHG_REPLY, &p.head, sizeof(p));
  844. }
  845. int conn_send_sr_reply(struct drbd_tconn *tconn, enum drbd_state_rv retcode)
  846. {
  847. struct p_req_state_reply p;
  848. enum drbd_packet cmd = tconn->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
  849. p.retcode = cpu_to_be32(retcode);
  850. return !conn_send_cmd(tconn, 0, &tconn->meta, cmd, &p.head, sizeof(p));
  851. }
  852. int fill_bitmap_rle_bits(struct drbd_conf *mdev,
  853. struct p_compressed_bm *p,
  854. struct bm_xfer_ctx *c)
  855. {
  856. struct bitstream bs;
  857. unsigned long plain_bits;
  858. unsigned long tmp;
  859. unsigned long rl;
  860. unsigned len;
  861. unsigned toggle;
  862. int bits;
  863. /* may we use this feature? */
  864. if ((mdev->tconn->net_conf->use_rle == 0) ||
  865. (mdev->tconn->agreed_pro_version < 90))
  866. return 0;
  867. if (c->bit_offset >= c->bm_bits)
  868. return 0; /* nothing to do. */
  869. /* use at most thus many bytes */
  870. bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
  871. memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
  872. /* plain bits covered in this code string */
  873. plain_bits = 0;
  874. /* p->encoding & 0x80 stores whether the first run length is set.
  875. * bit offset is implicit.
  876. * start with toggle == 2 to be able to tell the first iteration */
  877. toggle = 2;
  878. /* see how much plain bits we can stuff into one packet
  879. * using RLE and VLI. */
  880. do {
  881. tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
  882. : _drbd_bm_find_next(mdev, c->bit_offset);
  883. if (tmp == -1UL)
  884. tmp = c->bm_bits;
  885. rl = tmp - c->bit_offset;
  886. if (toggle == 2) { /* first iteration */
  887. if (rl == 0) {
  888. /* the first checked bit was set,
  889. * store start value, */
  890. DCBP_set_start(p, 1);
  891. /* but skip encoding of zero run length */
  892. toggle = !toggle;
  893. continue;
  894. }
  895. DCBP_set_start(p, 0);
  896. }
  897. /* paranoia: catch zero runlength.
  898. * can only happen if bitmap is modified while we scan it. */
  899. if (rl == 0) {
  900. dev_err(DEV, "unexpected zero runlength while encoding bitmap "
  901. "t:%u bo:%lu\n", toggle, c->bit_offset);
  902. return -1;
  903. }
  904. bits = vli_encode_bits(&bs, rl);
  905. if (bits == -ENOBUFS) /* buffer full */
  906. break;
  907. if (bits <= 0) {
  908. dev_err(DEV, "error while encoding bitmap: %d\n", bits);
  909. return 0;
  910. }
  911. toggle = !toggle;
  912. plain_bits += rl;
  913. c->bit_offset = tmp;
  914. } while (c->bit_offset < c->bm_bits);
  915. len = bs.cur.b - p->code + !!bs.cur.bit;
  916. if (plain_bits < (len << 3)) {
  917. /* incompressible with this method.
  918. * we need to rewind both word and bit position. */
  919. c->bit_offset -= plain_bits;
  920. bm_xfer_ctx_bit_to_word_offset(c);
  921. c->bit_offset = c->word_offset * BITS_PER_LONG;
  922. return 0;
  923. }
  924. /* RLE + VLI was able to compress it just fine.
  925. * update c->word_offset. */
  926. bm_xfer_ctx_bit_to_word_offset(c);
  927. /* store pad_bits */
  928. DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
  929. return len;
  930. }
  931. /**
  932. * send_bitmap_rle_or_plain
  933. *
  934. * Return 0 when done, 1 when another iteration is needed, and a negative error
  935. * code upon failure.
  936. */
  937. static int
  938. send_bitmap_rle_or_plain(struct drbd_conf *mdev,
  939. struct p_header *h, struct bm_xfer_ctx *c)
  940. {
  941. struct p_compressed_bm *p = (void*)h;
  942. unsigned long num_words;
  943. int len;
  944. int ok;
  945. len = fill_bitmap_rle_bits(mdev, p, c);
  946. if (len < 0)
  947. return -EIO;
  948. if (len) {
  949. DCBP_set_code(p, RLE_VLI_Bits);
  950. ok = !_drbd_send_cmd(mdev, mdev->tconn->data.socket, P_COMPRESSED_BITMAP, h,
  951. sizeof(*p) + len, 0);
  952. c->packets[0]++;
  953. c->bytes[0] += sizeof(*p) + len;
  954. if (c->bit_offset >= c->bm_bits)
  955. len = 0; /* DONE */
  956. } else {
  957. /* was not compressible.
  958. * send a buffer full of plain text bits instead. */
  959. num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
  960. len = num_words * sizeof(long);
  961. if (len)
  962. drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
  963. ok = !_drbd_send_cmd(mdev, mdev->tconn->data.socket, P_BITMAP,
  964. h, sizeof(struct p_header80) + len, 0);
  965. c->word_offset += num_words;
  966. c->bit_offset = c->word_offset * BITS_PER_LONG;
  967. c->packets[1]++;
  968. c->bytes[1] += sizeof(struct p_header80) + len;
  969. if (c->bit_offset > c->bm_bits)
  970. c->bit_offset = c->bm_bits;
  971. }
  972. if (ok) {
  973. if (len == 0) {
  974. INFO_bm_xfer_stats(mdev, "send", c);
  975. return 0;
  976. } else
  977. return 1;
  978. }
  979. return -EIO;
  980. }
  981. /* See the comment at receive_bitmap() */
  982. int _drbd_send_bitmap(struct drbd_conf *mdev)
  983. {
  984. struct bm_xfer_ctx c;
  985. struct p_header *p;
  986. int err;
  987. if (!expect(mdev->bitmap))
  988. return false;
  989. /* maybe we should use some per thread scratch page,
  990. * and allocate that during initial device creation? */
  991. p = (struct p_header *) __get_free_page(GFP_NOIO);
  992. if (!p) {
  993. dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
  994. return false;
  995. }
  996. if (get_ldev(mdev)) {
  997. if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
  998. dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
  999. drbd_bm_set_all(mdev);
  1000. if (drbd_bm_write(mdev)) {
  1001. /* write_bm did fail! Leave full sync flag set in Meta P_DATA
  1002. * but otherwise process as per normal - need to tell other
  1003. * side that a full resync is required! */
  1004. dev_err(DEV, "Failed to write bitmap to disk!\n");
  1005. } else {
  1006. drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
  1007. drbd_md_sync(mdev);
  1008. }
  1009. }
  1010. put_ldev(mdev);
  1011. }
  1012. c = (struct bm_xfer_ctx) {
  1013. .bm_bits = drbd_bm_bits(mdev),
  1014. .bm_words = drbd_bm_words(mdev),
  1015. };
  1016. do {
  1017. err = send_bitmap_rle_or_plain(mdev, p, &c);
  1018. } while (err > 0);
  1019. free_page((unsigned long) p);
  1020. return err == 0;
  1021. }
  1022. int drbd_send_bitmap(struct drbd_conf *mdev)
  1023. {
  1024. int err;
  1025. if (drbd_get_data_sock(mdev->tconn))
  1026. return -1;
  1027. err = !_drbd_send_bitmap(mdev);
  1028. drbd_put_data_sock(mdev->tconn);
  1029. return err;
  1030. }
  1031. void drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
  1032. {
  1033. struct p_barrier_ack p;
  1034. p.barrier = barrier_nr;
  1035. p.set_size = cpu_to_be32(set_size);
  1036. if (mdev->state.conn >= C_CONNECTED)
  1037. drbd_send_cmd(mdev, &mdev->tconn->meta, P_BARRIER_ACK, &p.head, sizeof(p));
  1038. }
  1039. /**
  1040. * _drbd_send_ack() - Sends an ack packet
  1041. * @mdev: DRBD device.
  1042. * @cmd: Packet command code.
  1043. * @sector: sector, needs to be in big endian byte order
  1044. * @blksize: size in byte, needs to be in big endian byte order
  1045. * @block_id: Id, big endian byte order
  1046. */
  1047. static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
  1048. u64 sector, u32 blksize, u64 block_id)
  1049. {
  1050. struct p_block_ack p;
  1051. p.sector = sector;
  1052. p.block_id = block_id;
  1053. p.blksize = blksize;
  1054. p.seq_num = cpu_to_be32(atomic_inc_return(&mdev->packet_seq));
  1055. if (!mdev->tconn->meta.socket || mdev->state.conn < C_CONNECTED)
  1056. return -EIO;
  1057. return drbd_send_cmd(mdev, &mdev->tconn->meta, cmd, &p.head, sizeof(p));
  1058. }
  1059. /* dp->sector and dp->block_id already/still in network byte order,
  1060. * data_size is payload size according to dp->head,
  1061. * and may need to be corrected for digest size. */
  1062. void drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
  1063. struct p_data *dp, int data_size)
  1064. {
  1065. data_size -= (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
  1066. crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
  1067. _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
  1068. dp->block_id);
  1069. }
  1070. void drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
  1071. struct p_block_req *rp)
  1072. {
  1073. _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
  1074. }
  1075. /**
  1076. * drbd_send_ack() - Sends an ack packet
  1077. * @mdev: DRBD device
  1078. * @cmd: packet command code
  1079. * @peer_req: peer request
  1080. */
  1081. int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
  1082. struct drbd_peer_request *peer_req)
  1083. {
  1084. return _drbd_send_ack(mdev, cmd,
  1085. cpu_to_be64(peer_req->i.sector),
  1086. cpu_to_be32(peer_req->i.size),
  1087. peer_req->block_id);
  1088. }
  1089. /* This function misuses the block_id field to signal if the blocks
  1090. * are is sync or not. */
  1091. int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
  1092. sector_t sector, int blksize, u64 block_id)
  1093. {
  1094. return _drbd_send_ack(mdev, cmd,
  1095. cpu_to_be64(sector),
  1096. cpu_to_be32(blksize),
  1097. cpu_to_be64(block_id));
  1098. }
  1099. int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
  1100. sector_t sector, int size, u64 block_id)
  1101. {
  1102. struct p_block_req p;
  1103. p.sector = cpu_to_be64(sector);
  1104. p.block_id = block_id;
  1105. p.blksize = cpu_to_be32(size);
  1106. return drbd_send_cmd(mdev, &mdev->tconn->data, cmd, &p.head, sizeof(p));
  1107. }
  1108. int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector, int size,
  1109. void *digest, int digest_size, enum drbd_packet cmd)
  1110. {
  1111. int err;
  1112. struct p_block_req p;
  1113. prepare_header(mdev, &p.head, cmd, sizeof(p) - sizeof(struct p_header) + digest_size);
  1114. p.sector = cpu_to_be64(sector);
  1115. p.block_id = ID_SYNCER /* unused */;
  1116. p.blksize = cpu_to_be32(size);
  1117. mutex_lock(&mdev->tconn->data.mutex);
  1118. err = drbd_send_all(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), 0);
  1119. if (!err)
  1120. err = drbd_send_all(mdev->tconn, mdev->tconn->data.socket, digest, digest_size, 0);
  1121. mutex_unlock(&mdev->tconn->data.mutex);
  1122. return err;
  1123. }
  1124. int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
  1125. {
  1126. struct p_block_req p;
  1127. p.sector = cpu_to_be64(sector);
  1128. p.block_id = ID_SYNCER /* unused */;
  1129. p.blksize = cpu_to_be32(size);
  1130. return drbd_send_cmd(mdev, &mdev->tconn->data, P_OV_REQUEST, &p.head, sizeof(p));
  1131. }
  1132. /* called on sndtimeo
  1133. * returns false if we should retry,
  1134. * true if we think connection is dead
  1135. */
  1136. static int we_should_drop_the_connection(struct drbd_tconn *tconn, struct socket *sock)
  1137. {
  1138. int drop_it;
  1139. /* long elapsed = (long)(jiffies - mdev->last_received); */
  1140. drop_it = tconn->meta.socket == sock
  1141. || !tconn->asender.task
  1142. || get_t_state(&tconn->asender) != RUNNING
  1143. || tconn->cstate < C_WF_REPORT_PARAMS;
  1144. if (drop_it)
  1145. return true;
  1146. drop_it = !--tconn->ko_count;
  1147. if (!drop_it) {
  1148. conn_err(tconn, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
  1149. current->comm, current->pid, tconn->ko_count);
  1150. request_ping(tconn);
  1151. }
  1152. return drop_it; /* && (mdev->state == R_PRIMARY) */;
  1153. }
  1154. static void drbd_update_congested(struct drbd_tconn *tconn)
  1155. {
  1156. struct sock *sk = tconn->data.socket->sk;
  1157. if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
  1158. set_bit(NET_CONGESTED, &tconn->flags);
  1159. }
  1160. /* The idea of sendpage seems to be to put some kind of reference
  1161. * to the page into the skb, and to hand it over to the NIC. In
  1162. * this process get_page() gets called.
  1163. *
  1164. * As soon as the page was really sent over the network put_page()
  1165. * gets called by some part of the network layer. [ NIC driver? ]
  1166. *
  1167. * [ get_page() / put_page() increment/decrement the count. If count
  1168. * reaches 0 the page will be freed. ]
  1169. *
  1170. * This works nicely with pages from FSs.
  1171. * But this means that in protocol A we might signal IO completion too early!
  1172. *
  1173. * In order not to corrupt data during a resync we must make sure
  1174. * that we do not reuse our own buffer pages (EEs) to early, therefore
  1175. * we have the net_ee list.
  1176. *
  1177. * XFS seems to have problems, still, it submits pages with page_count == 0!
  1178. * As a workaround, we disable sendpage on pages
  1179. * with page_count == 0 or PageSlab.
  1180. */
  1181. static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
  1182. int offset, size_t size, unsigned msg_flags)
  1183. {
  1184. struct socket *socket;
  1185. void *addr;
  1186. int err;
  1187. socket = mdev->tconn->data.socket;
  1188. addr = kmap(page) + offset;
  1189. err = drbd_send_all(mdev->tconn, socket, addr, size, msg_flags);
  1190. kunmap(page);
  1191. if (!err)
  1192. mdev->send_cnt += size >> 9;
  1193. return err;
  1194. }
  1195. static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
  1196. int offset, size_t size, unsigned msg_flags)
  1197. {
  1198. mm_segment_t oldfs = get_fs();
  1199. int sent, ok;
  1200. int len = size;
  1201. /* e.g. XFS meta- & log-data is in slab pages, which have a
  1202. * page_count of 0 and/or have PageSlab() set.
  1203. * we cannot use send_page for those, as that does get_page();
  1204. * put_page(); and would cause either a VM_BUG directly, or
  1205. * __page_cache_release a page that would actually still be referenced
  1206. * by someone, leading to some obscure delayed Oops somewhere else. */
  1207. if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
  1208. return !_drbd_no_send_page(mdev, page, offset, size, msg_flags);
  1209. msg_flags |= MSG_NOSIGNAL;
  1210. drbd_update_congested(mdev->tconn);
  1211. set_fs(KERNEL_DS);
  1212. do {
  1213. sent = mdev->tconn->data.socket->ops->sendpage(mdev->tconn->data.socket, page,
  1214. offset, len,
  1215. msg_flags);
  1216. if (sent == -EAGAIN) {
  1217. if (we_should_drop_the_connection(mdev->tconn,
  1218. mdev->tconn->data.socket))
  1219. break;
  1220. else
  1221. continue;
  1222. }
  1223. if (sent <= 0) {
  1224. dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
  1225. __func__, (int)size, len, sent);
  1226. break;
  1227. }
  1228. len -= sent;
  1229. offset += sent;
  1230. } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
  1231. set_fs(oldfs);
  1232. clear_bit(NET_CONGESTED, &mdev->tconn->flags);
  1233. ok = (len == 0);
  1234. if (likely(ok))
  1235. mdev->send_cnt += size>>9;
  1236. return ok;
  1237. }
  1238. static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
  1239. {
  1240. struct bio_vec *bvec;
  1241. int i;
  1242. /* hint all but last page with MSG_MORE */
  1243. __bio_for_each_segment(bvec, bio, i, 0) {
  1244. if (_drbd_no_send_page(mdev, bvec->bv_page,
  1245. bvec->bv_offset, bvec->bv_len,
  1246. i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
  1247. return 0;
  1248. }
  1249. return 1;
  1250. }
  1251. static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
  1252. {
  1253. struct bio_vec *bvec;
  1254. int i;
  1255. /* hint all but last page with MSG_MORE */
  1256. __bio_for_each_segment(bvec, bio, i, 0) {
  1257. if (!_drbd_send_page(mdev, bvec->bv_page,
  1258. bvec->bv_offset, bvec->bv_len,
  1259. i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
  1260. return 0;
  1261. }
  1262. return 1;
  1263. }
  1264. static int _drbd_send_zc_ee(struct drbd_conf *mdev,
  1265. struct drbd_peer_request *peer_req)
  1266. {
  1267. struct page *page = peer_req->pages;
  1268. unsigned len = peer_req->i.size;
  1269. /* hint all but last page with MSG_MORE */
  1270. page_chain_for_each(page) {
  1271. unsigned l = min_t(unsigned, len, PAGE_SIZE);
  1272. if (!_drbd_send_page(mdev, page, 0, l,
  1273. page_chain_next(page) ? MSG_MORE : 0))
  1274. return 0;
  1275. len -= l;
  1276. }
  1277. return 1;
  1278. }
  1279. static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
  1280. {
  1281. if (mdev->tconn->agreed_pro_version >= 95)
  1282. return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
  1283. (bi_rw & REQ_FUA ? DP_FUA : 0) |
  1284. (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
  1285. (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
  1286. else
  1287. return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0;
  1288. }
  1289. /* Used to send write requests
  1290. * R_PRIMARY -> Peer (P_DATA)
  1291. */
  1292. int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
  1293. {
  1294. int ok = 1;
  1295. struct p_data p;
  1296. unsigned int dp_flags = 0;
  1297. void *dgb;
  1298. int dgs;
  1299. if (drbd_get_data_sock(mdev->tconn))
  1300. return 0;
  1301. dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
  1302. crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
  1303. prepare_header(mdev, &p.head, P_DATA, sizeof(p) - sizeof(struct p_header) + dgs + req->i.size);
  1304. p.sector = cpu_to_be64(req->i.sector);
  1305. p.block_id = (unsigned long)req;
  1306. p.seq_num = cpu_to_be32(req->seq_num = atomic_inc_return(&mdev->packet_seq));
  1307. dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw);
  1308. if (mdev->state.conn >= C_SYNC_SOURCE &&
  1309. mdev->state.conn <= C_PAUSED_SYNC_T)
  1310. dp_flags |= DP_MAY_SET_IN_SYNC;
  1311. p.dp_flags = cpu_to_be32(dp_flags);
  1312. set_bit(UNPLUG_REMOTE, &mdev->flags);
  1313. ok = (sizeof(p) ==
  1314. drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
  1315. if (ok && dgs) {
  1316. dgb = mdev->tconn->int_dig_out;
  1317. drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, dgb);
  1318. ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
  1319. }
  1320. if (ok) {
  1321. /* For protocol A, we have to memcpy the payload into
  1322. * socket buffers, as we may complete right away
  1323. * as soon as we handed it over to tcp, at which point the data
  1324. * pages may become invalid.
  1325. *
  1326. * For data-integrity enabled, we copy it as well, so we can be
  1327. * sure that even if the bio pages may still be modified, it
  1328. * won't change the data on the wire, thus if the digest checks
  1329. * out ok after sending on this side, but does not fit on the
  1330. * receiving side, we sure have detected corruption elsewhere.
  1331. */
  1332. if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A || dgs)
  1333. ok = _drbd_send_bio(mdev, req->master_bio);
  1334. else
  1335. ok = _drbd_send_zc_bio(mdev, req->master_bio);
  1336. /* double check digest, sometimes buffers have been modified in flight. */
  1337. if (dgs > 0 && dgs <= 64) {
  1338. /* 64 byte, 512 bit, is the largest digest size
  1339. * currently supported in kernel crypto. */
  1340. unsigned char digest[64];
  1341. drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, digest);
  1342. if (memcmp(mdev->tconn->int_dig_out, digest, dgs)) {
  1343. dev_warn(DEV,
  1344. "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
  1345. (unsigned long long)req->i.sector, req->i.size);
  1346. }
  1347. } /* else if (dgs > 64) {
  1348. ... Be noisy about digest too large ...
  1349. } */
  1350. }
  1351. drbd_put_data_sock(mdev->tconn);
  1352. return ok;
  1353. }
  1354. /* answer packet, used to send data back for read requests:
  1355. * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
  1356. * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
  1357. */
  1358. int drbd_send_block(struct drbd_conf *mdev, enum drbd_packet cmd,
  1359. struct drbd_peer_request *peer_req)
  1360. {
  1361. int ok;
  1362. struct p_data p;
  1363. void *dgb;
  1364. int dgs;
  1365. dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
  1366. crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
  1367. prepare_header(mdev, &p.head, cmd, sizeof(p) -
  1368. sizeof(struct p_header80) +
  1369. dgs + peer_req->i.size);
  1370. p.sector = cpu_to_be64(peer_req->i.sector);
  1371. p.block_id = peer_req->block_id;
  1372. p.seq_num = 0; /* unused */
  1373. /* Only called by our kernel thread.
  1374. * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
  1375. * in response to admin command or module unload.
  1376. */
  1377. if (drbd_get_data_sock(mdev->tconn))
  1378. return 0;
  1379. ok = sizeof(p) == drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0);
  1380. if (ok && dgs) {
  1381. dgb = mdev->tconn->int_dig_out;
  1382. drbd_csum_ee(mdev, mdev->tconn->integrity_w_tfm, peer_req, dgb);
  1383. ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
  1384. }
  1385. if (ok)
  1386. ok = _drbd_send_zc_ee(mdev, peer_req);
  1387. drbd_put_data_sock(mdev->tconn);
  1388. return ok;
  1389. }
  1390. int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req)
  1391. {
  1392. struct p_block_desc p;
  1393. p.sector = cpu_to_be64(req->i.sector);
  1394. p.blksize = cpu_to_be32(req->i.size);
  1395. return drbd_send_cmd(mdev, &mdev->tconn->data, P_OUT_OF_SYNC, &p.head, sizeof(p));
  1396. }
  1397. /*
  1398. drbd_send distinguishes two cases:
  1399. Packets sent via the data socket "sock"
  1400. and packets sent via the meta data socket "msock"
  1401. sock msock
  1402. -----------------+-------------------------+------------------------------
  1403. timeout conf.timeout / 2 conf.timeout / 2
  1404. timeout action send a ping via msock Abort communication
  1405. and close all sockets
  1406. */
  1407. /*
  1408. * you must have down()ed the appropriate [m]sock_mutex elsewhere!
  1409. */
  1410. int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
  1411. void *buf, size_t size, unsigned msg_flags)
  1412. {
  1413. struct kvec iov;
  1414. struct msghdr msg;
  1415. int rv, sent = 0;
  1416. if (!sock)
  1417. return -EBADR;
  1418. /* THINK if (signal_pending) return ... ? */
  1419. iov.iov_base = buf;
  1420. iov.iov_len = size;
  1421. msg.msg_name = NULL;
  1422. msg.msg_namelen = 0;
  1423. msg.msg_control = NULL;
  1424. msg.msg_controllen = 0;
  1425. msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  1426. if (sock == tconn->data.socket) {
  1427. tconn->ko_count = tconn->net_conf->ko_count;
  1428. drbd_update_congested(tconn);
  1429. }
  1430. do {
  1431. /* STRANGE
  1432. * tcp_sendmsg does _not_ use its size parameter at all ?
  1433. *
  1434. * -EAGAIN on timeout, -EINTR on signal.
  1435. */
  1436. /* THINK
  1437. * do we need to block DRBD_SIG if sock == &meta.socket ??
  1438. * otherwise wake_asender() might interrupt some send_*Ack !
  1439. */
  1440. rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
  1441. if (rv == -EAGAIN) {
  1442. if (we_should_drop_the_connection(tconn, sock))
  1443. break;
  1444. else
  1445. continue;
  1446. }
  1447. if (rv == -EINTR) {
  1448. flush_signals(current);
  1449. rv = 0;
  1450. }
  1451. if (rv < 0)
  1452. break;
  1453. sent += rv;
  1454. iov.iov_base += rv;
  1455. iov.iov_len -= rv;
  1456. } while (sent < size);
  1457. if (sock == tconn->data.socket)
  1458. clear_bit(NET_CONGESTED, &tconn->flags);
  1459. if (rv <= 0) {
  1460. if (rv != -EAGAIN) {
  1461. conn_err(tconn, "%s_sendmsg returned %d\n",
  1462. sock == tconn->meta.socket ? "msock" : "sock",
  1463. rv);
  1464. conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
  1465. } else
  1466. conn_request_state(tconn, NS(conn, C_TIMEOUT), CS_HARD);
  1467. }
  1468. return sent;
  1469. }
  1470. /**
  1471. * drbd_send_all - Send an entire buffer
  1472. *
  1473. * Returns 0 upon success and a negative error value otherwise.
  1474. */
  1475. int drbd_send_all(struct drbd_tconn *tconn, struct socket *sock, void *buffer,
  1476. size_t size, unsigned msg_flags)
  1477. {
  1478. int err;
  1479. err = drbd_send(tconn, sock, buffer, size, msg_flags);
  1480. if (err < 0)
  1481. return err;
  1482. if (err != size)
  1483. return -EIO;
  1484. return 0;
  1485. }
  1486. static int drbd_open(struct block_device *bdev, fmode_t mode)
  1487. {
  1488. struct drbd_conf *mdev = bdev->bd_disk->private_data;
  1489. unsigned long flags;
  1490. int rv = 0;
  1491. mutex_lock(&drbd_main_mutex);
  1492. spin_lock_irqsave(&mdev->tconn->req_lock, flags);
  1493. /* to have a stable mdev->state.role
  1494. * and no race with updating open_cnt */
  1495. if (mdev->state.role != R_PRIMARY) {
  1496. if (mode & FMODE_WRITE)
  1497. rv = -EROFS;
  1498. else if (!allow_oos)
  1499. rv = -EMEDIUMTYPE;
  1500. }
  1501. if (!rv)
  1502. mdev->open_cnt++;
  1503. spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
  1504. mutex_unlock(&drbd_main_mutex);
  1505. return rv;
  1506. }
  1507. static int drbd_release(struct gendisk *gd, fmode_t mode)
  1508. {
  1509. struct drbd_conf *mdev = gd->private_data;
  1510. mutex_lock(&drbd_main_mutex);
  1511. mdev->open_cnt--;
  1512. mutex_unlock(&drbd_main_mutex);
  1513. return 0;
  1514. }
  1515. static void drbd_set_defaults(struct drbd_conf *mdev)
  1516. {
  1517. /* Beware! The actual layout differs
  1518. * between big endian and little endian */
  1519. mdev->state = (union drbd_state) {
  1520. { .role = R_SECONDARY,
  1521. .peer = R_UNKNOWN,
  1522. .conn = C_STANDALONE,
  1523. .disk = D_DISKLESS,
  1524. .pdsk = D_UNKNOWN,
  1525. .susp = 0,
  1526. .susp_nod = 0,
  1527. .susp_fen = 0
  1528. } };
  1529. }
  1530. void drbd_init_set_defaults(struct drbd_conf *mdev)
  1531. {
  1532. /* the memset(,0,) did most of this.
  1533. * note: only assignments, no allocation in here */
  1534. drbd_set_defaults(mdev);
  1535. atomic_set(&mdev->ap_bio_cnt, 0);
  1536. atomic_set(&mdev->ap_pending_cnt, 0);
  1537. atomic_set(&mdev->rs_pending_cnt, 0);
  1538. atomic_set(&mdev->unacked_cnt, 0);
  1539. atomic_set(&mdev->local_cnt, 0);
  1540. atomic_set(&mdev->pp_in_use, 0);
  1541. atomic_set(&mdev->pp_in_use_by_net, 0);
  1542. atomic_set(&mdev->rs_sect_in, 0);
  1543. atomic_set(&mdev->rs_sect_ev, 0);
  1544. atomic_set(&mdev->ap_in_flight, 0);
  1545. mutex_init(&mdev->md_io_mutex);
  1546. mutex_init(&mdev->own_state_mutex);
  1547. mdev->state_mutex = &mdev->own_state_mutex;
  1548. spin_lock_init(&mdev->al_lock);
  1549. spin_lock_init(&mdev->peer_seq_lock);
  1550. spin_lock_init(&mdev->epoch_lock);
  1551. INIT_LIST_HEAD(&mdev->active_ee);
  1552. INIT_LIST_HEAD(&mdev->sync_ee);
  1553. INIT_LIST_HEAD(&mdev->done_ee);
  1554. INIT_LIST_HEAD(&mdev->read_ee);
  1555. INIT_LIST_HEAD(&mdev->net_ee);
  1556. INIT_LIST_HEAD(&mdev->resync_reads);
  1557. INIT_LIST_HEAD(&mdev->resync_work.list);
  1558. INIT_LIST_HEAD(&mdev->unplug_work.list);
  1559. INIT_LIST_HEAD(&mdev->go_diskless.list);
  1560. INIT_LIST_HEAD(&mdev->md_sync_work.list);
  1561. INIT_LIST_HEAD(&mdev->start_resync_work.list);
  1562. INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
  1563. mdev->resync_work.cb = w_resync_timer;
  1564. mdev->unplug_work.cb = w_send_write_hint;
  1565. mdev->go_diskless.cb = w_go_diskless;
  1566. mdev->md_sync_work.cb = w_md_sync;
  1567. mdev->bm_io_work.w.cb = w_bitmap_io;
  1568. mdev->start_resync_work.cb = w_start_resync;
  1569. mdev->resync_work.mdev = mdev;
  1570. mdev->unplug_work.mdev = mdev;
  1571. mdev->go_diskless.mdev = mdev;
  1572. mdev->md_sync_work.mdev = mdev;
  1573. mdev->bm_io_work.w.mdev = mdev;
  1574. mdev->start_resync_work.mdev = mdev;
  1575. init_timer(&mdev->resync_timer);
  1576. init_timer(&mdev->md_sync_timer);
  1577. init_timer(&mdev->start_resync_timer);
  1578. init_timer(&mdev->request_timer);
  1579. mdev->resync_timer.function = resync_timer_fn;
  1580. mdev->resync_timer.data = (unsigned long) mdev;
  1581. mdev->md_sync_timer.function = md_sync_timer_fn;
  1582. mdev->md_sync_timer.data = (unsigned long) mdev;
  1583. mdev->start_resync_timer.function = start_resync_timer_fn;
  1584. mdev->start_resync_timer.data = (unsigned long) mdev;
  1585. mdev->request_timer.function = request_timer_fn;
  1586. mdev->request_timer.data = (unsigned long) mdev;
  1587. init_waitqueue_head(&mdev->misc_wait);
  1588. init_waitqueue_head(&mdev->state_wait);
  1589. init_waitqueue_head(&mdev->ee_wait);
  1590. init_waitqueue_head(&mdev->al_wait);
  1591. init_waitqueue_head(&mdev->seq_wait);
  1592. /* mdev->tconn->agreed_pro_version gets initialized in drbd_connect() */
  1593. mdev->write_ordering = WO_bdev_flush;
  1594. mdev->resync_wenr = LC_FREE;
  1595. mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
  1596. mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
  1597. }
  1598. void drbd_mdev_cleanup(struct drbd_conf *mdev)
  1599. {
  1600. int i;
  1601. if (mdev->tconn->receiver.t_state != NONE)
  1602. dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
  1603. mdev->tconn->receiver.t_state);
  1604. /* no need to lock it, I'm the only thread alive */
  1605. if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
  1606. dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
  1607. mdev->al_writ_cnt =
  1608. mdev->bm_writ_cnt =
  1609. mdev->read_cnt =
  1610. mdev->recv_cnt =
  1611. mdev->send_cnt =
  1612. mdev->writ_cnt =
  1613. mdev->p_size =
  1614. mdev->rs_start =
  1615. mdev->rs_total =
  1616. mdev->rs_failed = 0;
  1617. mdev->rs_last_events = 0;
  1618. mdev->rs_last_sect_ev = 0;
  1619. for (i = 0; i < DRBD_SYNC_MARKS; i++) {
  1620. mdev->rs_mark_left[i] = 0;
  1621. mdev->rs_mark_time[i] = 0;
  1622. }
  1623. D_ASSERT(mdev->tconn->net_conf == NULL);
  1624. drbd_set_my_capacity(mdev, 0);
  1625. if (mdev->bitmap) {
  1626. /* maybe never allocated. */
  1627. drbd_bm_resize(mdev, 0, 1);
  1628. drbd_bm_cleanup(mdev);
  1629. }
  1630. drbd_free_resources(mdev);
  1631. clear_bit(AL_SUSPENDED, &mdev->flags);
  1632. /*
  1633. * currently we drbd_init_ee only on module load, so
  1634. * we may do drbd_release_ee only on module unload!
  1635. */
  1636. D_ASSERT(list_empty(&mdev->active_ee));
  1637. D_ASSERT(list_empty(&mdev->sync_ee));
  1638. D_ASSERT(list_empty(&mdev->done_ee));
  1639. D_ASSERT(list_empty(&mdev->read_ee));
  1640. D_ASSERT(list_empty(&mdev->net_ee));
  1641. D_ASSERT(list_empty(&mdev->resync_reads));
  1642. D_ASSERT(list_empty(&mdev->tconn->data.work.q));
  1643. D_ASSERT(list_empty(&mdev->tconn->meta.work.q));
  1644. D_ASSERT(list_empty(&mdev->resync_work.list));
  1645. D_ASSERT(list_empty(&mdev->unplug_work.list));
  1646. D_ASSERT(list_empty(&mdev->go_diskless.list));
  1647. drbd_set_defaults(mdev);
  1648. }
  1649. static void drbd_destroy_mempools(void)
  1650. {
  1651. struct page *page;
  1652. while (drbd_pp_pool) {
  1653. page = drbd_pp_pool;
  1654. drbd_pp_pool = (struct page *)page_private(page);
  1655. __free_page(page);
  1656. drbd_pp_vacant--;
  1657. }
  1658. /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
  1659. if (drbd_md_io_bio_set)
  1660. bioset_free(drbd_md_io_bio_set);
  1661. if (drbd_md_io_page_pool)
  1662. mempool_destroy(drbd_md_io_page_pool);
  1663. if (drbd_ee_mempool)
  1664. mempool_destroy(drbd_ee_mempool);
  1665. if (drbd_request_mempool)
  1666. mempool_destroy(drbd_request_mempool);
  1667. if (drbd_ee_cache)
  1668. kmem_cache_destroy(drbd_ee_cache);
  1669. if (drbd_request_cache)
  1670. kmem_cache_destroy(drbd_request_cache);
  1671. if (drbd_bm_ext_cache)
  1672. kmem_cache_destroy(drbd_bm_ext_cache);
  1673. if (drbd_al_ext_cache)
  1674. kmem_cache_destroy(drbd_al_ext_cache);
  1675. drbd_md_io_bio_set = NULL;
  1676. drbd_md_io_page_pool = NULL;
  1677. drbd_ee_mempool = NULL;
  1678. drbd_request_mempool = NULL;
  1679. drbd_ee_cache = NULL;
  1680. drbd_request_cache = NULL;
  1681. drbd_bm_ext_cache = NULL;
  1682. drbd_al_ext_cache = NULL;
  1683. return;
  1684. }
  1685. static int drbd_create_mempools(void)
  1686. {
  1687. struct page *page;
  1688. const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
  1689. int i;
  1690. /* prepare our caches and mempools */
  1691. drbd_request_mempool = NULL;
  1692. drbd_ee_cache = NULL;
  1693. drbd_request_cache = NULL;
  1694. drbd_bm_ext_cache = NULL;
  1695. drbd_al_ext_cache = NULL;
  1696. drbd_pp_pool = NULL;
  1697. drbd_md_io_page_pool = NULL;
  1698. drbd_md_io_bio_set = NULL;
  1699. /* caches */
  1700. drbd_request_cache = kmem_cache_create(
  1701. "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
  1702. if (drbd_request_cache == NULL)
  1703. goto Enomem;
  1704. drbd_ee_cache = kmem_cache_create(
  1705. "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
  1706. if (drbd_ee_cache == NULL)
  1707. goto Enomem;
  1708. drbd_bm_ext_cache = kmem_cache_create(
  1709. "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
  1710. if (drbd_bm_ext_cache == NULL)
  1711. goto Enomem;
  1712. drbd_al_ext_cache = kmem_cache_create(
  1713. "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
  1714. if (drbd_al_ext_cache == NULL)
  1715. goto Enomem;
  1716. /* mempools */
  1717. drbd_md_io_bio_set = bioset_create(DRBD_MIN_POOL_PAGES, 0);
  1718. if (drbd_md_io_bio_set == NULL)
  1719. goto Enomem;
  1720. drbd_md_io_page_pool = mempool_create_page_pool(DRBD_MIN_POOL_PAGES, 0);
  1721. if (drbd_md_io_page_pool == NULL)
  1722. goto Enomem;
  1723. drbd_request_mempool = mempool_create(number,
  1724. mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
  1725. if (drbd_request_mempool == NULL)
  1726. goto Enomem;
  1727. drbd_ee_mempool = mempool_create(number,
  1728. mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
  1729. if (drbd_ee_mempool == NULL)
  1730. goto Enomem;
  1731. /* drbd's page pool */
  1732. spin_lock_init(&drbd_pp_lock);
  1733. for (i = 0; i < number; i++) {
  1734. page = alloc_page(GFP_HIGHUSER);
  1735. if (!page)
  1736. goto Enomem;
  1737. set_page_private(page, (unsigned long)drbd_pp_pool);
  1738. drbd_pp_pool = page;
  1739. }
  1740. drbd_pp_vacant = number;
  1741. return 0;
  1742. Enomem:
  1743. drbd_destroy_mempools(); /* in case we allocated some */
  1744. return -ENOMEM;
  1745. }
  1746. static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
  1747. void *unused)
  1748. {
  1749. /* just so we have it. you never know what interesting things we
  1750. * might want to do here some day...
  1751. */
  1752. return NOTIFY_DONE;
  1753. }
  1754. static struct notifier_block drbd_notifier = {
  1755. .notifier_call = drbd_notify_sys,
  1756. };
  1757. static void drbd_release_ee_lists(struct drbd_conf *mdev)
  1758. {
  1759. int rr;
  1760. rr = drbd_release_ee(mdev, &mdev->active_ee);
  1761. if (rr)
  1762. dev_err(DEV, "%d EEs in active list found!\n", rr);
  1763. rr = drbd_release_ee(mdev, &mdev->sync_ee);
  1764. if (rr)
  1765. dev_err(DEV, "%d EEs in sync list found!\n", rr);
  1766. rr = drbd_release_ee(mdev, &mdev->read_ee);
  1767. if (rr)
  1768. dev_err(DEV, "%d EEs in read list found!\n", rr);
  1769. rr = drbd_release_ee(mdev, &mdev->done_ee);
  1770. if (rr)
  1771. dev_err(DEV, "%d EEs in done list found!\n", rr);
  1772. rr = drbd_release_ee(mdev, &mdev->net_ee);
  1773. if (rr)
  1774. dev_err(DEV, "%d EEs in net list found!\n", rr);
  1775. }
  1776. /* caution. no locking. */
  1777. void drbd_delete_device(unsigned int minor)
  1778. {
  1779. struct drbd_conf *mdev = minor_to_mdev(minor);
  1780. if (!mdev)
  1781. return;
  1782. idr_remove(&mdev->tconn->volumes, mdev->vnr);
  1783. idr_remove(&minors, minor);
  1784. synchronize_rcu();
  1785. /* paranoia asserts */
  1786. D_ASSERT(mdev->open_cnt == 0);
  1787. D_ASSERT(list_empty(&mdev->tconn->data.work.q));
  1788. /* end paranoia asserts */
  1789. del_gendisk(mdev->vdisk);
  1790. /* cleanup stuff that may have been allocated during
  1791. * device (re-)configuration or state changes */
  1792. if (mdev->this_bdev)
  1793. bdput(mdev->this_bdev);
  1794. drbd_free_resources(mdev);
  1795. drbd_release_ee_lists(mdev);
  1796. lc_destroy(mdev->act_log);
  1797. lc_destroy(mdev->resync);
  1798. kfree(mdev->p_uuid);
  1799. /* mdev->p_uuid = NULL; */
  1800. /* cleanup the rest that has been
  1801. * allocated from drbd_new_device
  1802. * and actually free the mdev itself */
  1803. drbd_free_mdev(mdev);
  1804. }
  1805. static void drbd_cleanup(void)
  1806. {
  1807. unsigned int i;
  1808. struct drbd_conf *mdev;
  1809. unregister_reboot_notifier(&drbd_notifier);
  1810. /* first remove proc,
  1811. * drbdsetup uses it's presence to detect
  1812. * whether DRBD is loaded.
  1813. * If we would get stuck in proc removal,
  1814. * but have netlink already deregistered,
  1815. * some drbdsetup commands may wait forever
  1816. * for an answer.
  1817. */
  1818. if (drbd_proc)
  1819. remove_proc_entry("drbd", NULL);
  1820. drbd_genl_unregister();
  1821. idr_for_each_entry(&minors, mdev, i)
  1822. drbd_delete_device(i);
  1823. drbd_destroy_mempools();
  1824. unregister_blkdev(DRBD_MAJOR, "drbd");
  1825. idr_destroy(&minors);
  1826. printk(KERN_INFO "drbd: module cleanup done.\n");
  1827. }
  1828. /**
  1829. * drbd_congested() - Callback for pdflush
  1830. * @congested_data: User data
  1831. * @bdi_bits: Bits pdflush is currently interested in
  1832. *
  1833. * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
  1834. */
  1835. static int drbd_congested(void *congested_data, int bdi_bits)
  1836. {
  1837. struct drbd_conf *mdev = congested_data;
  1838. struct request_queue *q;
  1839. char reason = '-';
  1840. int r = 0;
  1841. if (!may_inc_ap_bio(mdev)) {
  1842. /* DRBD has frozen IO */
  1843. r = bdi_bits;
  1844. reason = 'd';
  1845. goto out;
  1846. }
  1847. if (get_ldev(mdev)) {
  1848. q = bdev_get_queue(mdev->ldev->backing_bdev);
  1849. r = bdi_congested(&q->backing_dev_info, bdi_bits);
  1850. put_ldev(mdev);
  1851. if (r)
  1852. reason = 'b';
  1853. }
  1854. if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->tconn->flags)) {
  1855. r |= (1 << BDI_async_congested);
  1856. reason = reason == 'b' ? 'a' : 'n';
  1857. }
  1858. out:
  1859. mdev->congestion_reason = reason;
  1860. return r;
  1861. }
  1862. static void drbd_init_workqueue(struct drbd_work_queue* wq)
  1863. {
  1864. sema_init(&wq->s, 0);
  1865. spin_lock_init(&wq->q_lock);
  1866. INIT_LIST_HEAD(&wq->q);
  1867. }
  1868. struct drbd_tconn *conn_by_name(const char *name)
  1869. {
  1870. struct drbd_tconn *tconn;
  1871. if (!name || !name[0])
  1872. return NULL;
  1873. mutex_lock(&drbd_cfg_mutex);
  1874. list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
  1875. if (!strcmp(tconn->name, name))
  1876. goto found;
  1877. }
  1878. tconn = NULL;
  1879. found:
  1880. mutex_unlock(&drbd_cfg_mutex);
  1881. return tconn;
  1882. }
  1883. struct drbd_tconn *drbd_new_tconn(const char *name)
  1884. {
  1885. struct drbd_tconn *tconn;
  1886. tconn = kzalloc(sizeof(struct drbd_tconn), GFP_KERNEL);
  1887. if (!tconn)
  1888. return NULL;
  1889. tconn->name = kstrdup(name, GFP_KERNEL);
  1890. if (!tconn->name)
  1891. goto fail;
  1892. if (!zalloc_cpumask_var(&tconn->cpu_mask, GFP_KERNEL))
  1893. goto fail;
  1894. if (!tl_init(tconn))
  1895. goto fail;
  1896. tconn->cstate = C_STANDALONE;
  1897. mutex_init(&tconn->cstate_mutex);
  1898. spin_lock_init(&tconn->req_lock);
  1899. atomic_set(&tconn->net_cnt, 0);
  1900. init_waitqueue_head(&tconn->net_cnt_wait);
  1901. init_waitqueue_head(&tconn->ping_wait);
  1902. idr_init(&tconn->volumes);
  1903. drbd_init_workqueue(&tconn->data.work);
  1904. mutex_init(&tconn->data.mutex);
  1905. drbd_init_workqueue(&tconn->meta.work);
  1906. mutex_init(&tconn->meta.mutex);
  1907. drbd_thread_init(tconn, &tconn->receiver, drbdd_init, "receiver");
  1908. drbd_thread_init(tconn, &tconn->worker, drbd_worker, "worker");
  1909. drbd_thread_init(tconn, &tconn->asender, drbd_asender, "asender");
  1910. tconn->res_opts = (struct res_opts) {
  1911. {}, 0, /* cpu_mask */
  1912. DRBD_ON_NO_DATA_DEF, /* on_no_data */
  1913. };
  1914. mutex_lock(&drbd_cfg_mutex);
  1915. list_add_tail(&tconn->all_tconn, &drbd_tconns);
  1916. mutex_unlock(&drbd_cfg_mutex);
  1917. return tconn;
  1918. fail:
  1919. tl_cleanup(tconn);
  1920. free_cpumask_var(tconn->cpu_mask);
  1921. kfree(tconn->name);
  1922. kfree(tconn);
  1923. return NULL;
  1924. }
  1925. void drbd_free_tconn(struct drbd_tconn *tconn)
  1926. {
  1927. list_del(&tconn->all_tconn);
  1928. idr_destroy(&tconn->volumes);
  1929. free_cpumask_var(tconn->cpu_mask);
  1930. kfree(tconn->name);
  1931. kfree(tconn->int_dig_out);
  1932. kfree(tconn->int_dig_in);
  1933. kfree(tconn->int_dig_vv);
  1934. kfree(tconn);
  1935. }
  1936. enum drbd_ret_code conn_new_minor(struct drbd_tconn *tconn, unsigned int minor, int vnr)
  1937. {
  1938. struct drbd_conf *mdev;
  1939. struct gendisk *disk;
  1940. struct request_queue *q;
  1941. int vnr_got = vnr;
  1942. int minor_got = minor;
  1943. enum drbd_ret_code err = ERR_NOMEM;
  1944. mdev = minor_to_mdev(minor);
  1945. if (mdev)
  1946. return ERR_MINOR_EXISTS;
  1947. /* GFP_KERNEL, we are outside of all write-out paths */
  1948. mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
  1949. if (!mdev)
  1950. return ERR_NOMEM;
  1951. mdev->tconn = tconn;
  1952. mdev->minor = minor;
  1953. mdev->vnr = vnr;
  1954. drbd_init_set_defaults(mdev);
  1955. q = blk_alloc_queue(GFP_KERNEL);
  1956. if (!q)
  1957. goto out_no_q;
  1958. mdev->rq_queue = q;
  1959. q->queuedata = mdev;
  1960. disk = alloc_disk(1);
  1961. if (!disk)
  1962. goto out_no_disk;
  1963. mdev->vdisk = disk;
  1964. set_disk_ro(disk, true);
  1965. disk->queue = q;
  1966. disk->major = DRBD_MAJOR;
  1967. disk->first_minor = minor;
  1968. disk->fops = &drbd_ops;
  1969. sprintf(disk->disk_name, "drbd%d", minor);
  1970. disk->private_data = mdev;
  1971. mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
  1972. /* we have no partitions. we contain only ourselves. */
  1973. mdev->this_bdev->bd_contains = mdev->this_bdev;
  1974. q->backing_dev_info.congested_fn = drbd_congested;
  1975. q->backing_dev_info.congested_data = mdev;
  1976. blk_queue_make_request(q, drbd_make_request);
  1977. /* Setting the max_hw_sectors to an odd value of 8kibyte here
  1978. This triggers a max_bio_size message upon first attach or connect */
  1979. blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
  1980. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  1981. blk_queue_merge_bvec(q, drbd_merge_bvec);
  1982. q->queue_lock = &mdev->tconn->req_lock; /* needed since we use */
  1983. mdev->md_io_page = alloc_page(GFP_KERNEL);
  1984. if (!mdev->md_io_page)
  1985. goto out_no_io_page;
  1986. if (drbd_bm_init(mdev))
  1987. goto out_no_bitmap;
  1988. mdev->read_requests = RB_ROOT;
  1989. mdev->write_requests = RB_ROOT;
  1990. mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
  1991. if (!mdev->current_epoch)
  1992. goto out_no_epoch;
  1993. INIT_LIST_HEAD(&mdev->current_epoch->list);
  1994. mdev->epochs = 1;
  1995. if (!idr_pre_get(&minors, GFP_KERNEL))
  1996. goto out_no_minor_idr;
  1997. if (idr_get_new_above(&minors, mdev, minor, &minor_got))
  1998. goto out_no_minor_idr;
  1999. if (minor_got != minor) {
  2000. err = ERR_MINOR_EXISTS;
  2001. drbd_msg_put_info("requested minor exists already");
  2002. goto out_idr_remove_minor;
  2003. }
  2004. if (!idr_pre_get(&tconn->volumes, GFP_KERNEL))
  2005. goto out_idr_remove_minor;
  2006. if (idr_get_new_above(&tconn->volumes, mdev, vnr, &vnr_got))
  2007. goto out_idr_remove_minor;
  2008. if (vnr_got != vnr) {
  2009. err = ERR_INVALID_REQUEST;
  2010. drbd_msg_put_info("requested volume exists already");
  2011. goto out_idr_remove_vol;
  2012. }
  2013. add_disk(disk);
  2014. /* inherit the connection state */
  2015. mdev->state.conn = tconn->cstate;
  2016. if (mdev->state.conn == C_WF_REPORT_PARAMS)
  2017. drbd_connected(vnr, mdev, tconn);
  2018. return NO_ERROR;
  2019. out_idr_remove_vol:
  2020. idr_remove(&tconn->volumes, vnr_got);
  2021. out_idr_remove_minor:
  2022. idr_remove(&minors, minor_got);
  2023. synchronize_rcu();
  2024. out_no_minor_idr:
  2025. kfree(mdev->current_epoch);
  2026. out_no_epoch:
  2027. drbd_bm_cleanup(mdev);
  2028. out_no_bitmap:
  2029. __free_page(mdev->md_io_page);
  2030. out_no_io_page:
  2031. put_disk(disk);
  2032. out_no_disk:
  2033. blk_cleanup_queue(q);
  2034. out_no_q:
  2035. kfree(mdev);
  2036. return err;
  2037. }
  2038. /* counterpart of drbd_new_device.
  2039. * last part of drbd_delete_device. */
  2040. void drbd_free_mdev(struct drbd_conf *mdev)
  2041. {
  2042. kfree(mdev->current_epoch);
  2043. if (mdev->bitmap) /* should no longer be there. */
  2044. drbd_bm_cleanup(mdev);
  2045. __free_page(mdev->md_io_page);
  2046. put_disk(mdev->vdisk);
  2047. blk_cleanup_queue(mdev->rq_queue);
  2048. kfree(mdev);
  2049. }
  2050. int __init drbd_init(void)
  2051. {
  2052. int err;
  2053. BUILD_BUG_ON(sizeof(struct p_header80) != sizeof(struct p_header95));
  2054. BUILD_BUG_ON(sizeof(struct p_handshake) != 80);
  2055. if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
  2056. printk(KERN_ERR
  2057. "drbd: invalid minor_count (%d)\n", minor_count);
  2058. #ifdef MODULE
  2059. return -EINVAL;
  2060. #else
  2061. minor_count = 8;
  2062. #endif
  2063. }
  2064. err = register_blkdev(DRBD_MAJOR, "drbd");
  2065. if (err) {
  2066. printk(KERN_ERR
  2067. "drbd: unable to register block device major %d\n",
  2068. DRBD_MAJOR);
  2069. return err;
  2070. }
  2071. err = drbd_genl_register();
  2072. if (err) {
  2073. printk(KERN_ERR "drbd: unable to register generic netlink family\n");
  2074. goto fail;
  2075. }
  2076. register_reboot_notifier(&drbd_notifier);
  2077. /*
  2078. * allocate all necessary structs
  2079. */
  2080. err = -ENOMEM;
  2081. init_waitqueue_head(&drbd_pp_wait);
  2082. drbd_proc = NULL; /* play safe for drbd_cleanup */
  2083. idr_init(&minors);
  2084. err = drbd_create_mempools();
  2085. if (err)
  2086. goto fail;
  2087. drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
  2088. if (!drbd_proc) {
  2089. printk(KERN_ERR "drbd: unable to register proc file\n");
  2090. goto fail;
  2091. }
  2092. rwlock_init(&global_state_lock);
  2093. INIT_LIST_HEAD(&drbd_tconns);
  2094. printk(KERN_INFO "drbd: initialized. "
  2095. "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
  2096. API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
  2097. printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
  2098. printk(KERN_INFO "drbd: registered as block device major %d\n",
  2099. DRBD_MAJOR);
  2100. return 0; /* Success! */
  2101. fail:
  2102. drbd_cleanup();
  2103. if (err == -ENOMEM)
  2104. /* currently always the case */
  2105. printk(KERN_ERR "drbd: ran out of memory\n");
  2106. else
  2107. printk(KERN_ERR "drbd: initialization failure\n");
  2108. return err;
  2109. }
  2110. void drbd_free_bc(struct drbd_backing_dev *ldev)
  2111. {
  2112. if (ldev == NULL)
  2113. return;
  2114. blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  2115. blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  2116. kfree(ldev);
  2117. }
  2118. void drbd_free_sock(struct drbd_tconn *tconn)
  2119. {
  2120. if (tconn->data.socket) {
  2121. mutex_lock(&tconn->data.mutex);
  2122. kernel_sock_shutdown(tconn->data.socket, SHUT_RDWR);
  2123. sock_release(tconn->data.socket);
  2124. tconn->data.socket = NULL;
  2125. mutex_unlock(&tconn->data.mutex);
  2126. }
  2127. if (tconn->meta.socket) {
  2128. mutex_lock(&tconn->meta.mutex);
  2129. kernel_sock_shutdown(tconn->meta.socket, SHUT_RDWR);
  2130. sock_release(tconn->meta.socket);
  2131. tconn->meta.socket = NULL;
  2132. mutex_unlock(&tconn->meta.mutex);
  2133. }
  2134. }
  2135. void drbd_free_resources(struct drbd_conf *mdev)
  2136. {
  2137. crypto_free_hash(mdev->tconn->csums_tfm);
  2138. mdev->tconn->csums_tfm = NULL;
  2139. crypto_free_hash(mdev->tconn->verify_tfm);
  2140. mdev->tconn->verify_tfm = NULL;
  2141. crypto_free_hash(mdev->tconn->cram_hmac_tfm);
  2142. mdev->tconn->cram_hmac_tfm = NULL;
  2143. crypto_free_hash(mdev->tconn->integrity_w_tfm);
  2144. mdev->tconn->integrity_w_tfm = NULL;
  2145. crypto_free_hash(mdev->tconn->integrity_r_tfm);
  2146. mdev->tconn->integrity_r_tfm = NULL;
  2147. drbd_free_sock(mdev->tconn);
  2148. __no_warn(local,
  2149. drbd_free_bc(mdev->ldev);
  2150. mdev->ldev = NULL;);
  2151. }
  2152. /* meta data management */
  2153. struct meta_data_on_disk {
  2154. u64 la_size; /* last agreed size. */
  2155. u64 uuid[UI_SIZE]; /* UUIDs. */
  2156. u64 device_uuid;
  2157. u64 reserved_u64_1;
  2158. u32 flags; /* MDF */
  2159. u32 magic;
  2160. u32 md_size_sect;
  2161. u32 al_offset; /* offset to this block */
  2162. u32 al_nr_extents; /* important for restoring the AL */
  2163. /* `-- act_log->nr_elements <-- ldev->dc.al_extents */
  2164. u32 bm_offset; /* offset to the bitmap, from here */
  2165. u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
  2166. u32 la_peer_max_bio_size; /* last peer max_bio_size */
  2167. u32 reserved_u32[3];
  2168. } __packed;
  2169. /**
  2170. * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
  2171. * @mdev: DRBD device.
  2172. */
  2173. void drbd_md_sync(struct drbd_conf *mdev)
  2174. {
  2175. struct meta_data_on_disk *buffer;
  2176. sector_t sector;
  2177. int i;
  2178. del_timer(&mdev->md_sync_timer);
  2179. /* timer may be rearmed by drbd_md_mark_dirty() now. */
  2180. if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
  2181. return;
  2182. /* We use here D_FAILED and not D_ATTACHING because we try to write
  2183. * metadata even if we detach due to a disk failure! */
  2184. if (!get_ldev_if_state(mdev, D_FAILED))
  2185. return;
  2186. mutex_lock(&mdev->md_io_mutex);
  2187. buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
  2188. memset(buffer, 0, 512);
  2189. buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
  2190. for (i = UI_CURRENT; i < UI_SIZE; i++)
  2191. buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
  2192. buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
  2193. buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
  2194. buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
  2195. buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
  2196. buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
  2197. buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
  2198. buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
  2199. buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
  2200. buffer->la_peer_max_bio_size = cpu_to_be32(mdev->peer_max_bio_size);
  2201. D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
  2202. sector = mdev->ldev->md.md_offset;
  2203. if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
  2204. /* this was a try anyways ... */
  2205. dev_err(DEV, "meta data update failed!\n");
  2206. drbd_chk_io_error(mdev, 1, true);
  2207. }
  2208. /* Update mdev->ldev->md.la_size_sect,
  2209. * since we updated it on metadata. */
  2210. mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
  2211. mutex_unlock(&mdev->md_io_mutex);
  2212. put_ldev(mdev);
  2213. }
  2214. /**
  2215. * drbd_md_read() - Reads in the meta data super block
  2216. * @mdev: DRBD device.
  2217. * @bdev: Device from which the meta data should be read in.
  2218. *
  2219. * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case
  2220. * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
  2221. */
  2222. int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
  2223. {
  2224. struct meta_data_on_disk *buffer;
  2225. int i, rv = NO_ERROR;
  2226. if (!get_ldev_if_state(mdev, D_ATTACHING))
  2227. return ERR_IO_MD_DISK;
  2228. mutex_lock(&mdev->md_io_mutex);
  2229. buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
  2230. if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
  2231. /* NOTE: can't do normal error processing here as this is
  2232. called BEFORE disk is attached */
  2233. dev_err(DEV, "Error while reading metadata.\n");
  2234. rv = ERR_IO_MD_DISK;
  2235. goto err;
  2236. }
  2237. if (buffer->magic != cpu_to_be32(DRBD_MD_MAGIC)) {
  2238. dev_err(DEV, "Error while reading metadata, magic not found.\n");
  2239. rv = ERR_MD_INVALID;
  2240. goto err;
  2241. }
  2242. if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
  2243. dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
  2244. be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
  2245. rv = ERR_MD_INVALID;
  2246. goto err;
  2247. }
  2248. if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
  2249. dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
  2250. be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
  2251. rv = ERR_MD_INVALID;
  2252. goto err;
  2253. }
  2254. if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
  2255. dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
  2256. be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
  2257. rv = ERR_MD_INVALID;
  2258. goto err;
  2259. }
  2260. if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
  2261. dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
  2262. be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
  2263. rv = ERR_MD_INVALID;
  2264. goto err;
  2265. }
  2266. bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
  2267. for (i = UI_CURRENT; i < UI_SIZE; i++)
  2268. bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
  2269. bdev->md.flags = be32_to_cpu(buffer->flags);
  2270. bdev->dc.al_extents = be32_to_cpu(buffer->al_nr_extents);
  2271. bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
  2272. spin_lock_irq(&mdev->tconn->req_lock);
  2273. if (mdev->state.conn < C_CONNECTED) {
  2274. int peer;
  2275. peer = be32_to_cpu(buffer->la_peer_max_bio_size);
  2276. peer = max_t(int, peer, DRBD_MAX_BIO_SIZE_SAFE);
  2277. mdev->peer_max_bio_size = peer;
  2278. }
  2279. spin_unlock_irq(&mdev->tconn->req_lock);
  2280. if (bdev->dc.al_extents < 7)
  2281. bdev->dc.al_extents = 127;
  2282. err:
  2283. mutex_unlock(&mdev->md_io_mutex);
  2284. put_ldev(mdev);
  2285. return rv;
  2286. }
  2287. /**
  2288. * drbd_md_mark_dirty() - Mark meta data super block as dirty
  2289. * @mdev: DRBD device.
  2290. *
  2291. * Call this function if you change anything that should be written to
  2292. * the meta-data super block. This function sets MD_DIRTY, and starts a
  2293. * timer that ensures that within five seconds you have to call drbd_md_sync().
  2294. */
  2295. #ifdef DEBUG
  2296. void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func)
  2297. {
  2298. if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) {
  2299. mod_timer(&mdev->md_sync_timer, jiffies + HZ);
  2300. mdev->last_md_mark_dirty.line = line;
  2301. mdev->last_md_mark_dirty.func = func;
  2302. }
  2303. }
  2304. #else
  2305. void drbd_md_mark_dirty(struct drbd_conf *mdev)
  2306. {
  2307. if (!test_and_set_bit(MD_DIRTY, &mdev->flags))
  2308. mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
  2309. }
  2310. #endif
  2311. static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
  2312. {
  2313. int i;
  2314. for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
  2315. mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
  2316. }
  2317. void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
  2318. {
  2319. if (idx == UI_CURRENT) {
  2320. if (mdev->state.role == R_PRIMARY)
  2321. val |= 1;
  2322. else
  2323. val &= ~((u64)1);
  2324. drbd_set_ed_uuid(mdev, val);
  2325. }
  2326. mdev->ldev->md.uuid[idx] = val;
  2327. drbd_md_mark_dirty(mdev);
  2328. }
  2329. void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
  2330. {
  2331. if (mdev->ldev->md.uuid[idx]) {
  2332. drbd_uuid_move_history(mdev);
  2333. mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
  2334. }
  2335. _drbd_uuid_set(mdev, idx, val);
  2336. }
  2337. /**
  2338. * drbd_uuid_new_current() - Creates a new current UUID
  2339. * @mdev: DRBD device.
  2340. *
  2341. * Creates a new current UUID, and rotates the old current UUID into
  2342. * the bitmap slot. Causes an incremental resync upon next connect.
  2343. */
  2344. void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
  2345. {
  2346. u64 val;
  2347. unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
  2348. if (bm_uuid)
  2349. dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
  2350. mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
  2351. get_random_bytes(&val, sizeof(u64));
  2352. _drbd_uuid_set(mdev, UI_CURRENT, val);
  2353. drbd_print_uuids(mdev, "new current UUID");
  2354. /* get it to stable storage _now_ */
  2355. drbd_md_sync(mdev);
  2356. }
  2357. void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
  2358. {
  2359. if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
  2360. return;
  2361. if (val == 0) {
  2362. drbd_uuid_move_history(mdev);
  2363. mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
  2364. mdev->ldev->md.uuid[UI_BITMAP] = 0;
  2365. } else {
  2366. unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
  2367. if (bm_uuid)
  2368. dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
  2369. mdev->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
  2370. }
  2371. drbd_md_mark_dirty(mdev);
  2372. }
  2373. /**
  2374. * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  2375. * @mdev: DRBD device.
  2376. *
  2377. * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
  2378. */
  2379. int drbd_bmio_set_n_write(struct drbd_conf *mdev)
  2380. {
  2381. int rv = -EIO;
  2382. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  2383. drbd_md_set_flag(mdev, MDF_FULL_SYNC);
  2384. drbd_md_sync(mdev);
  2385. drbd_bm_set_all(mdev);
  2386. rv = drbd_bm_write(mdev);
  2387. if (!rv) {
  2388. drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
  2389. drbd_md_sync(mdev);
  2390. }
  2391. put_ldev(mdev);
  2392. }
  2393. return rv;
  2394. }
  2395. /**
  2396. * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  2397. * @mdev: DRBD device.
  2398. *
  2399. * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
  2400. */
  2401. int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
  2402. {
  2403. int rv = -EIO;
  2404. drbd_resume_al(mdev);
  2405. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  2406. drbd_bm_clear_all(mdev);
  2407. rv = drbd_bm_write(mdev);
  2408. put_ldev(mdev);
  2409. }
  2410. return rv;
  2411. }
  2412. static int w_bitmap_io(struct drbd_work *w, int unused)
  2413. {
  2414. struct bm_io_work *work = container_of(w, struct bm_io_work, w);
  2415. struct drbd_conf *mdev = w->mdev;
  2416. int rv = -EIO;
  2417. D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
  2418. if (get_ldev(mdev)) {
  2419. drbd_bm_lock(mdev, work->why, work->flags);
  2420. rv = work->io_fn(mdev);
  2421. drbd_bm_unlock(mdev);
  2422. put_ldev(mdev);
  2423. }
  2424. clear_bit_unlock(BITMAP_IO, &mdev->flags);
  2425. wake_up(&mdev->misc_wait);
  2426. if (work->done)
  2427. work->done(mdev, rv);
  2428. clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
  2429. work->why = NULL;
  2430. work->flags = 0;
  2431. return 1;
  2432. }
  2433. void drbd_ldev_destroy(struct drbd_conf *mdev)
  2434. {
  2435. lc_destroy(mdev->resync);
  2436. mdev->resync = NULL;
  2437. lc_destroy(mdev->act_log);
  2438. mdev->act_log = NULL;
  2439. __no_warn(local,
  2440. drbd_free_bc(mdev->ldev);
  2441. mdev->ldev = NULL;);
  2442. clear_bit(GO_DISKLESS, &mdev->flags);
  2443. }
  2444. static int w_go_diskless(struct drbd_work *w, int unused)
  2445. {
  2446. struct drbd_conf *mdev = w->mdev;
  2447. D_ASSERT(mdev->state.disk == D_FAILED);
  2448. /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
  2449. * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
  2450. * the protected members anymore, though, so once put_ldev reaches zero
  2451. * again, it will be safe to free them. */
  2452. drbd_force_state(mdev, NS(disk, D_DISKLESS));
  2453. return 1;
  2454. }
  2455. void drbd_go_diskless(struct drbd_conf *mdev)
  2456. {
  2457. D_ASSERT(mdev->state.disk == D_FAILED);
  2458. if (!test_and_set_bit(GO_DISKLESS, &mdev->flags))
  2459. drbd_queue_work(&mdev->tconn->data.work, &mdev->go_diskless);
  2460. }
  2461. /**
  2462. * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
  2463. * @mdev: DRBD device.
  2464. * @io_fn: IO callback to be called when bitmap IO is possible
  2465. * @done: callback to be called after the bitmap IO was performed
  2466. * @why: Descriptive text of the reason for doing the IO
  2467. *
  2468. * While IO on the bitmap happens we freeze application IO thus we ensure
  2469. * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
  2470. * called from worker context. It MUST NOT be used while a previous such
  2471. * work is still pending!
  2472. */
  2473. void drbd_queue_bitmap_io(struct drbd_conf *mdev,
  2474. int (*io_fn)(struct drbd_conf *),
  2475. void (*done)(struct drbd_conf *, int),
  2476. char *why, enum bm_flag flags)
  2477. {
  2478. D_ASSERT(current == mdev->tconn->worker.task);
  2479. D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
  2480. D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
  2481. D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
  2482. if (mdev->bm_io_work.why)
  2483. dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
  2484. why, mdev->bm_io_work.why);
  2485. mdev->bm_io_work.io_fn = io_fn;
  2486. mdev->bm_io_work.done = done;
  2487. mdev->bm_io_work.why = why;
  2488. mdev->bm_io_work.flags = flags;
  2489. spin_lock_irq(&mdev->tconn->req_lock);
  2490. set_bit(BITMAP_IO, &mdev->flags);
  2491. if (atomic_read(&mdev->ap_bio_cnt) == 0) {
  2492. if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
  2493. drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
  2494. }
  2495. spin_unlock_irq(&mdev->tconn->req_lock);
  2496. }
  2497. /**
  2498. * drbd_bitmap_io() - Does an IO operation on the whole bitmap
  2499. * @mdev: DRBD device.
  2500. * @io_fn: IO callback to be called when bitmap IO is possible
  2501. * @why: Descriptive text of the reason for doing the IO
  2502. *
  2503. * freezes application IO while that the actual IO operations runs. This
  2504. * functions MAY NOT be called from worker context.
  2505. */
  2506. int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *),
  2507. char *why, enum bm_flag flags)
  2508. {
  2509. int rv;
  2510. D_ASSERT(current != mdev->tconn->worker.task);
  2511. if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
  2512. drbd_suspend_io(mdev);
  2513. drbd_bm_lock(mdev, why, flags);
  2514. rv = io_fn(mdev);
  2515. drbd_bm_unlock(mdev);
  2516. if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
  2517. drbd_resume_io(mdev);
  2518. return rv;
  2519. }
  2520. void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
  2521. {
  2522. if ((mdev->ldev->md.flags & flag) != flag) {
  2523. drbd_md_mark_dirty(mdev);
  2524. mdev->ldev->md.flags |= flag;
  2525. }
  2526. }
  2527. void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
  2528. {
  2529. if ((mdev->ldev->md.flags & flag) != 0) {
  2530. drbd_md_mark_dirty(mdev);
  2531. mdev->ldev->md.flags &= ~flag;
  2532. }
  2533. }
  2534. int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
  2535. {
  2536. return (bdev->md.flags & flag) != 0;
  2537. }
  2538. static void md_sync_timer_fn(unsigned long data)
  2539. {
  2540. struct drbd_conf *mdev = (struct drbd_conf *) data;
  2541. drbd_queue_work_front(&mdev->tconn->data.work, &mdev->md_sync_work);
  2542. }
  2543. static int w_md_sync(struct drbd_work *w, int unused)
  2544. {
  2545. struct drbd_conf *mdev = w->mdev;
  2546. dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
  2547. #ifdef DEBUG
  2548. dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
  2549. mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line);
  2550. #endif
  2551. drbd_md_sync(mdev);
  2552. return 1;
  2553. }
  2554. const char *cmdname(enum drbd_packet cmd)
  2555. {
  2556. /* THINK may need to become several global tables
  2557. * when we want to support more than
  2558. * one PRO_VERSION */
  2559. static const char *cmdnames[] = {
  2560. [P_DATA] = "Data",
  2561. [P_DATA_REPLY] = "DataReply",
  2562. [P_RS_DATA_REPLY] = "RSDataReply",
  2563. [P_BARRIER] = "Barrier",
  2564. [P_BITMAP] = "ReportBitMap",
  2565. [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
  2566. [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
  2567. [P_UNPLUG_REMOTE] = "UnplugRemote",
  2568. [P_DATA_REQUEST] = "DataRequest",
  2569. [P_RS_DATA_REQUEST] = "RSDataRequest",
  2570. [P_SYNC_PARAM] = "SyncParam",
  2571. [P_SYNC_PARAM89] = "SyncParam89",
  2572. [P_PROTOCOL] = "ReportProtocol",
  2573. [P_UUIDS] = "ReportUUIDs",
  2574. [P_SIZES] = "ReportSizes",
  2575. [P_STATE] = "ReportState",
  2576. [P_SYNC_UUID] = "ReportSyncUUID",
  2577. [P_AUTH_CHALLENGE] = "AuthChallenge",
  2578. [P_AUTH_RESPONSE] = "AuthResponse",
  2579. [P_PING] = "Ping",
  2580. [P_PING_ACK] = "PingAck",
  2581. [P_RECV_ACK] = "RecvAck",
  2582. [P_WRITE_ACK] = "WriteAck",
  2583. [P_RS_WRITE_ACK] = "RSWriteAck",
  2584. [P_DISCARD_WRITE] = "DiscardWrite",
  2585. [P_NEG_ACK] = "NegAck",
  2586. [P_NEG_DREPLY] = "NegDReply",
  2587. [P_NEG_RS_DREPLY] = "NegRSDReply",
  2588. [P_BARRIER_ACK] = "BarrierAck",
  2589. [P_STATE_CHG_REQ] = "StateChgRequest",
  2590. [P_STATE_CHG_REPLY] = "StateChgReply",
  2591. [P_OV_REQUEST] = "OVRequest",
  2592. [P_OV_REPLY] = "OVReply",
  2593. [P_OV_RESULT] = "OVResult",
  2594. [P_CSUM_RS_REQUEST] = "CsumRSRequest",
  2595. [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
  2596. [P_COMPRESSED_BITMAP] = "CBitmap",
  2597. [P_DELAY_PROBE] = "DelayProbe",
  2598. [P_OUT_OF_SYNC] = "OutOfSync",
  2599. [P_RETRY_WRITE] = "RetryWrite",
  2600. };
  2601. if (cmd == P_HAND_SHAKE_M)
  2602. return "HandShakeM";
  2603. if (cmd == P_HAND_SHAKE_S)
  2604. return "HandShakeS";
  2605. if (cmd == P_HAND_SHAKE)
  2606. return "HandShake";
  2607. if (cmd >= ARRAY_SIZE(cmdnames))
  2608. return "Unknown";
  2609. return cmdnames[cmd];
  2610. }
  2611. /**
  2612. * drbd_wait_misc - wait for a request to make progress
  2613. * @mdev: device associated with the request
  2614. * @i: the struct drbd_interval embedded in struct drbd_request or
  2615. * struct drbd_peer_request
  2616. */
  2617. int drbd_wait_misc(struct drbd_conf *mdev, struct drbd_interval *i)
  2618. {
  2619. struct net_conf *net_conf = mdev->tconn->net_conf;
  2620. DEFINE_WAIT(wait);
  2621. long timeout;
  2622. if (!net_conf)
  2623. return -ETIMEDOUT;
  2624. timeout = MAX_SCHEDULE_TIMEOUT;
  2625. if (net_conf->ko_count)
  2626. timeout = net_conf->timeout * HZ / 10 * net_conf->ko_count;
  2627. /* Indicate to wake up mdev->misc_wait on progress. */
  2628. i->waiting = true;
  2629. prepare_to_wait(&mdev->misc_wait, &wait, TASK_INTERRUPTIBLE);
  2630. spin_unlock_irq(&mdev->tconn->req_lock);
  2631. timeout = schedule_timeout(timeout);
  2632. finish_wait(&mdev->misc_wait, &wait);
  2633. spin_lock_irq(&mdev->tconn->req_lock);
  2634. if (!timeout || mdev->state.conn < C_CONNECTED)
  2635. return -ETIMEDOUT;
  2636. if (signal_pending(current))
  2637. return -ERESTARTSYS;
  2638. return 0;
  2639. }
  2640. #ifdef CONFIG_DRBD_FAULT_INJECTION
  2641. /* Fault insertion support including random number generator shamelessly
  2642. * stolen from kernel/rcutorture.c */
  2643. struct fault_random_state {
  2644. unsigned long state;
  2645. unsigned long count;
  2646. };
  2647. #define FAULT_RANDOM_MULT 39916801 /* prime */
  2648. #define FAULT_RANDOM_ADD 479001701 /* prime */
  2649. #define FAULT_RANDOM_REFRESH 10000
  2650. /*
  2651. * Crude but fast random-number generator. Uses a linear congruential
  2652. * generator, with occasional help from get_random_bytes().
  2653. */
  2654. static unsigned long
  2655. _drbd_fault_random(struct fault_random_state *rsp)
  2656. {
  2657. long refresh;
  2658. if (!rsp->count--) {
  2659. get_random_bytes(&refresh, sizeof(refresh));
  2660. rsp->state += refresh;
  2661. rsp->count = FAULT_RANDOM_REFRESH;
  2662. }
  2663. rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
  2664. return swahw32(rsp->state);
  2665. }
  2666. static char *
  2667. _drbd_fault_str(unsigned int type) {
  2668. static char *_faults[] = {
  2669. [DRBD_FAULT_MD_WR] = "Meta-data write",
  2670. [DRBD_FAULT_MD_RD] = "Meta-data read",
  2671. [DRBD_FAULT_RS_WR] = "Resync write",
  2672. [DRBD_FAULT_RS_RD] = "Resync read",
  2673. [DRBD_FAULT_DT_WR] = "Data write",
  2674. [DRBD_FAULT_DT_RD] = "Data read",
  2675. [DRBD_FAULT_DT_RA] = "Data read ahead",
  2676. [DRBD_FAULT_BM_ALLOC] = "BM allocation",
  2677. [DRBD_FAULT_AL_EE] = "EE allocation",
  2678. [DRBD_FAULT_RECEIVE] = "receive data corruption",
  2679. };
  2680. return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
  2681. }
  2682. unsigned int
  2683. _drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
  2684. {
  2685. static struct fault_random_state rrs = {0, 0};
  2686. unsigned int ret = (
  2687. (fault_devs == 0 ||
  2688. ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
  2689. (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
  2690. if (ret) {
  2691. fault_count++;
  2692. if (__ratelimit(&drbd_ratelimit_state))
  2693. dev_warn(DEV, "***Simulating %s failure\n",
  2694. _drbd_fault_str(type));
  2695. }
  2696. return ret;
  2697. }
  2698. #endif
  2699. const char *drbd_buildtag(void)
  2700. {
  2701. /* DRBD built from external sources has here a reference to the
  2702. git hash of the source code. */
  2703. static char buildtag[38] = "\0uilt-in";
  2704. if (buildtag[0] == 0) {
  2705. #ifdef CONFIG_MODULES
  2706. if (THIS_MODULE != NULL)
  2707. sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
  2708. else
  2709. #endif
  2710. buildtag[0] = 'b';
  2711. }
  2712. return buildtag;
  2713. }
  2714. module_init(drbd_init)
  2715. module_exit(drbd_cleanup)
  2716. EXPORT_SYMBOL(drbd_conn_str);
  2717. EXPORT_SYMBOL(drbd_role_str);
  2718. EXPORT_SYMBOL(drbd_disk_str);
  2719. EXPORT_SYMBOL(drbd_set_st_err_str);