drbd_main.c 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. drbd.c
  4. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  5. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  6. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  7. Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  8. Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
  9. from Logicworks, Inc. for making SDP replication support possible.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/drbd.h>
  15. #include <linux/uaccess.h>
  16. #include <asm/types.h>
  17. #include <net/sock.h>
  18. #include <linux/ctype.h>
  19. #include <linux/mutex.h>
  20. #include <linux/fs.h>
  21. #include <linux/file.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/init.h>
  24. #include <linux/mm.h>
  25. #include <linux/memcontrol.h>
  26. #include <linux/mm_inline.h>
  27. #include <linux/slab.h>
  28. #include <linux/string.h>
  29. #include <linux/random.h>
  30. #include <linux/reboot.h>
  31. #include <linux/notifier.h>
  32. #include <linux/kthread.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/unistd.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/sched/signal.h>
  37. #include <linux/drbd_limits.h>
  38. #include "drbd_int.h"
  39. #include "drbd_protocol.h"
  40. #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
  41. #include "drbd_vli.h"
  42. #include "drbd_debugfs.h"
  43. static DEFINE_MUTEX(drbd_main_mutex);
  44. static int drbd_open(struct gendisk *disk, blk_mode_t mode);
  45. static void drbd_release(struct gendisk *gd);
  46. static void md_sync_timer_fn(struct timer_list *t);
  47. static int w_bitmap_io(struct drbd_work *w, int unused);
  48. MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
  49. "Lars Ellenberg <lars@linbit.com>");
  50. MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
  51. MODULE_VERSION(REL_VERSION);
  52. MODULE_LICENSE("GPL");
  53. MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices ("
  54. __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
  55. MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
  56. #include <linux/moduleparam.h>
  57. /* thanks to these macros, if compiled into the kernel (not-module),
  58. * these become boot parameters (e.g., drbd.minor_count) */
  59. #ifdef CONFIG_DRBD_FAULT_INJECTION
  60. int drbd_enable_faults;
  61. int drbd_fault_rate;
  62. static int drbd_fault_count;
  63. static int drbd_fault_devs;
  64. /* bitmap of enabled faults */
  65. module_param_named(enable_faults, drbd_enable_faults, int, 0664);
  66. /* fault rate % value - applies to all enabled faults */
  67. module_param_named(fault_rate, drbd_fault_rate, int, 0664);
  68. /* count of faults inserted */
  69. module_param_named(fault_count, drbd_fault_count, int, 0664);
  70. /* bitmap of devices to insert faults on */
  71. module_param_named(fault_devs, drbd_fault_devs, int, 0644);
  72. #endif
  73. /* module parameters we can keep static */
  74. static bool drbd_allow_oos; /* allow_open_on_secondary */
  75. static bool drbd_disable_sendpage;
  76. MODULE_PARM_DESC(allow_oos, "DONT USE!");
  77. module_param_named(allow_oos, drbd_allow_oos, bool, 0);
  78. module_param_named(disable_sendpage, drbd_disable_sendpage, bool, 0644);
  79. /* module parameters we share */
  80. int drbd_proc_details; /* Detail level in proc drbd*/
  81. module_param_named(proc_details, drbd_proc_details, int, 0644);
  82. /* module parameters shared with defaults */
  83. unsigned int drbd_minor_count = DRBD_MINOR_COUNT_DEF;
  84. /* Module parameter for setting the user mode helper program
  85. * to run. Default is /sbin/drbdadm */
  86. char drbd_usermode_helper[80] = "/sbin/drbdadm";
  87. module_param_named(minor_count, drbd_minor_count, uint, 0444);
  88. module_param_string(usermode_helper, drbd_usermode_helper, sizeof(drbd_usermode_helper), 0644);
  89. /* in 2.6.x, our device mapping and config info contains our virtual gendisks
  90. * as member "struct gendisk *vdisk;"
  91. */
  92. struct idr drbd_devices;
  93. struct list_head drbd_resources;
  94. struct mutex resources_mutex;
  95. struct kmem_cache *drbd_request_cache;
  96. struct kmem_cache *drbd_ee_cache; /* peer requests */
  97. struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
  98. struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
  99. mempool_t drbd_request_mempool;
  100. mempool_t drbd_ee_mempool;
  101. mempool_t drbd_md_io_page_pool;
  102. mempool_t drbd_buffer_page_pool;
  103. struct bio_set drbd_md_io_bio_set;
  104. struct bio_set drbd_io_bio_set;
  105. DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
  106. static const struct block_device_operations drbd_ops = {
  107. .owner = THIS_MODULE,
  108. .submit_bio = drbd_submit_bio,
  109. .open = drbd_open,
  110. .release = drbd_release,
  111. };
  112. #ifdef __CHECKER__
  113. /* When checking with sparse, and this is an inline function, sparse will
  114. give tons of false positives. When this is a real functions sparse works.
  115. */
  116. int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins)
  117. {
  118. int io_allowed;
  119. atomic_inc(&device->local_cnt);
  120. io_allowed = (device->state.disk >= mins);
  121. if (!io_allowed) {
  122. if (atomic_dec_and_test(&device->local_cnt))
  123. wake_up(&device->misc_wait);
  124. }
  125. return io_allowed;
  126. }
  127. #endif
  128. /**
  129. * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch
  130. * @connection: DRBD connection.
  131. * @barrier_nr: Expected identifier of the DRBD write barrier packet.
  132. * @set_size: Expected number of requests before that barrier.
  133. *
  134. * In case the passed barrier_nr or set_size does not match the oldest
  135. * epoch of not yet barrier-acked requests, this function will cause a
  136. * termination of the connection.
  137. */
  138. void tl_release(struct drbd_connection *connection, unsigned int barrier_nr,
  139. unsigned int set_size)
  140. {
  141. struct drbd_request *r;
  142. struct drbd_request *req = NULL, *tmp = NULL;
  143. int expect_epoch = 0;
  144. int expect_size = 0;
  145. spin_lock_irq(&connection->resource->req_lock);
  146. /* find oldest not yet barrier-acked write request,
  147. * count writes in its epoch. */
  148. list_for_each_entry(r, &connection->transfer_log, tl_requests) {
  149. const unsigned s = r->rq_state;
  150. if (!req) {
  151. if (!(s & RQ_WRITE))
  152. continue;
  153. if (!(s & RQ_NET_MASK))
  154. continue;
  155. if (s & RQ_NET_DONE)
  156. continue;
  157. req = r;
  158. expect_epoch = req->epoch;
  159. expect_size ++;
  160. } else {
  161. if (r->epoch != expect_epoch)
  162. break;
  163. if (!(s & RQ_WRITE))
  164. continue;
  165. /* if (s & RQ_DONE): not expected */
  166. /* if (!(s & RQ_NET_MASK)): not expected */
  167. expect_size++;
  168. }
  169. }
  170. /* first some paranoia code */
  171. if (req == NULL) {
  172. drbd_err(connection, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
  173. barrier_nr);
  174. goto bail;
  175. }
  176. if (expect_epoch != barrier_nr) {
  177. drbd_err(connection, "BAD! BarrierAck #%u received, expected #%u!\n",
  178. barrier_nr, expect_epoch);
  179. goto bail;
  180. }
  181. if (expect_size != set_size) {
  182. drbd_err(connection, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
  183. barrier_nr, set_size, expect_size);
  184. goto bail;
  185. }
  186. /* Clean up list of requests processed during current epoch. */
  187. /* this extra list walk restart is paranoia,
  188. * to catch requests being barrier-acked "unexpectedly".
  189. * It usually should find the same req again, or some READ preceding it. */
  190. list_for_each_entry(req, &connection->transfer_log, tl_requests)
  191. if (req->epoch == expect_epoch) {
  192. tmp = req;
  193. break;
  194. }
  195. req = list_prepare_entry(tmp, &connection->transfer_log, tl_requests);
  196. list_for_each_entry_safe_from(req, r, &connection->transfer_log, tl_requests) {
  197. struct drbd_peer_device *peer_device;
  198. if (req->epoch != expect_epoch)
  199. break;
  200. peer_device = conn_peer_device(connection, req->device->vnr);
  201. _req_mod(req, BARRIER_ACKED, peer_device);
  202. }
  203. spin_unlock_irq(&connection->resource->req_lock);
  204. return;
  205. bail:
  206. spin_unlock_irq(&connection->resource->req_lock);
  207. conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
  208. }
  209. /**
  210. * _tl_restart() - Walks the transfer log, and applies an action to all requests
  211. * @connection: DRBD connection to operate on.
  212. * @what: The action/event to perform with all request objects
  213. *
  214. * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
  215. * RESTART_FROZEN_DISK_IO.
  216. */
  217. /* must hold resource->req_lock */
  218. void _tl_restart(struct drbd_connection *connection, enum drbd_req_event what)
  219. {
  220. struct drbd_peer_device *peer_device;
  221. struct drbd_request *req, *r;
  222. list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests) {
  223. peer_device = conn_peer_device(connection, req->device->vnr);
  224. _req_mod(req, what, peer_device);
  225. }
  226. }
  227. void tl_restart(struct drbd_connection *connection, enum drbd_req_event what)
  228. {
  229. spin_lock_irq(&connection->resource->req_lock);
  230. _tl_restart(connection, what);
  231. spin_unlock_irq(&connection->resource->req_lock);
  232. }
  233. /**
  234. * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
  235. * @connection: DRBD connection.
  236. *
  237. * This is called after the connection to the peer was lost. The storage covered
  238. * by the requests on the transfer gets marked as our of sync. Called from the
  239. * receiver thread and the worker thread.
  240. */
  241. void tl_clear(struct drbd_connection *connection)
  242. {
  243. tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
  244. }
  245. /**
  246. * tl_abort_disk_io() - Abort disk I/O for all requests for a certain device in the TL
  247. * @device: DRBD device.
  248. */
  249. void tl_abort_disk_io(struct drbd_device *device)
  250. {
  251. struct drbd_connection *connection = first_peer_device(device)->connection;
  252. struct drbd_request *req, *r;
  253. spin_lock_irq(&connection->resource->req_lock);
  254. list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests) {
  255. if (!(req->rq_state & RQ_LOCAL_PENDING))
  256. continue;
  257. if (req->device != device)
  258. continue;
  259. _req_mod(req, ABORT_DISK_IO, NULL);
  260. }
  261. spin_unlock_irq(&connection->resource->req_lock);
  262. }
  263. static int drbd_thread_setup(void *arg)
  264. {
  265. struct drbd_thread *thi = (struct drbd_thread *) arg;
  266. struct drbd_resource *resource = thi->resource;
  267. unsigned long flags;
  268. int retval;
  269. snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
  270. thi->name[0],
  271. resource->name);
  272. allow_kernel_signal(DRBD_SIGKILL);
  273. allow_kernel_signal(SIGXCPU);
  274. restart:
  275. retval = thi->function(thi);
  276. spin_lock_irqsave(&thi->t_lock, flags);
  277. /* if the receiver has been "EXITING", the last thing it did
  278. * was set the conn state to "StandAlone",
  279. * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
  280. * and receiver thread will be "started".
  281. * drbd_thread_start needs to set "RESTARTING" in that case.
  282. * t_state check and assignment needs to be within the same spinlock,
  283. * so either thread_start sees EXITING, and can remap to RESTARTING,
  284. * or thread_start see NONE, and can proceed as normal.
  285. */
  286. if (thi->t_state == RESTARTING) {
  287. drbd_info(resource, "Restarting %s thread\n", thi->name);
  288. thi->t_state = RUNNING;
  289. spin_unlock_irqrestore(&thi->t_lock, flags);
  290. goto restart;
  291. }
  292. thi->task = NULL;
  293. thi->t_state = NONE;
  294. smp_mb();
  295. complete_all(&thi->stop);
  296. spin_unlock_irqrestore(&thi->t_lock, flags);
  297. drbd_info(resource, "Terminating %s\n", current->comm);
  298. /* Release mod reference taken when thread was started */
  299. if (thi->connection)
  300. kref_put(&thi->connection->kref, drbd_destroy_connection);
  301. kref_put(&resource->kref, drbd_destroy_resource);
  302. module_put(THIS_MODULE);
  303. return retval;
  304. }
  305. static void drbd_thread_init(struct drbd_resource *resource, struct drbd_thread *thi,
  306. int (*func) (struct drbd_thread *), const char *name)
  307. {
  308. spin_lock_init(&thi->t_lock);
  309. thi->task = NULL;
  310. thi->t_state = NONE;
  311. thi->function = func;
  312. thi->resource = resource;
  313. thi->connection = NULL;
  314. thi->name = name;
  315. }
  316. int drbd_thread_start(struct drbd_thread *thi)
  317. {
  318. struct drbd_resource *resource = thi->resource;
  319. struct task_struct *nt;
  320. unsigned long flags;
  321. /* is used from state engine doing drbd_thread_stop_nowait,
  322. * while holding the req lock irqsave */
  323. spin_lock_irqsave(&thi->t_lock, flags);
  324. switch (thi->t_state) {
  325. case NONE:
  326. drbd_info(resource, "Starting %s thread (from %s [%d])\n",
  327. thi->name, current->comm, current->pid);
  328. /* Get ref on module for thread - this is released when thread exits */
  329. if (!try_module_get(THIS_MODULE)) {
  330. drbd_err(resource, "Failed to get module reference in drbd_thread_start\n");
  331. spin_unlock_irqrestore(&thi->t_lock, flags);
  332. return false;
  333. }
  334. kref_get(&resource->kref);
  335. if (thi->connection)
  336. kref_get(&thi->connection->kref);
  337. init_completion(&thi->stop);
  338. thi->reset_cpu_mask = 1;
  339. thi->t_state = RUNNING;
  340. spin_unlock_irqrestore(&thi->t_lock, flags);
  341. flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
  342. nt = kthread_create(drbd_thread_setup, (void *) thi,
  343. "drbd_%c_%s", thi->name[0], thi->resource->name);
  344. if (IS_ERR(nt)) {
  345. drbd_err(resource, "Couldn't start thread\n");
  346. if (thi->connection)
  347. kref_put(&thi->connection->kref, drbd_destroy_connection);
  348. kref_put(&resource->kref, drbd_destroy_resource);
  349. module_put(THIS_MODULE);
  350. return false;
  351. }
  352. spin_lock_irqsave(&thi->t_lock, flags);
  353. thi->task = nt;
  354. thi->t_state = RUNNING;
  355. spin_unlock_irqrestore(&thi->t_lock, flags);
  356. wake_up_process(nt);
  357. break;
  358. case EXITING:
  359. thi->t_state = RESTARTING;
  360. drbd_info(resource, "Restarting %s thread (from %s [%d])\n",
  361. thi->name, current->comm, current->pid);
  362. fallthrough;
  363. case RUNNING:
  364. case RESTARTING:
  365. default:
  366. spin_unlock_irqrestore(&thi->t_lock, flags);
  367. break;
  368. }
  369. return true;
  370. }
  371. void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
  372. {
  373. unsigned long flags;
  374. enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
  375. /* may be called from state engine, holding the req lock irqsave */
  376. spin_lock_irqsave(&thi->t_lock, flags);
  377. if (thi->t_state == NONE) {
  378. spin_unlock_irqrestore(&thi->t_lock, flags);
  379. if (restart)
  380. drbd_thread_start(thi);
  381. return;
  382. }
  383. if (thi->t_state != ns) {
  384. if (thi->task == NULL) {
  385. spin_unlock_irqrestore(&thi->t_lock, flags);
  386. return;
  387. }
  388. thi->t_state = ns;
  389. smp_mb();
  390. init_completion(&thi->stop);
  391. if (thi->task != current)
  392. send_sig(DRBD_SIGKILL, thi->task, 1);
  393. }
  394. spin_unlock_irqrestore(&thi->t_lock, flags);
  395. if (wait)
  396. wait_for_completion(&thi->stop);
  397. }
  398. #ifdef CONFIG_SMP
  399. /*
  400. * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
  401. *
  402. * Forces all threads of a resource onto the same CPU. This is beneficial for
  403. * DRBD's performance. May be overwritten by user's configuration.
  404. */
  405. static void drbd_calc_cpu_mask(cpumask_var_t *cpu_mask)
  406. {
  407. unsigned int *resources_per_cpu, min_index = ~0;
  408. resources_per_cpu = kcalloc(nr_cpu_ids, sizeof(*resources_per_cpu),
  409. GFP_KERNEL);
  410. if (resources_per_cpu) {
  411. struct drbd_resource *resource;
  412. unsigned int cpu, min = ~0;
  413. rcu_read_lock();
  414. for_each_resource_rcu(resource, &drbd_resources) {
  415. for_each_cpu(cpu, resource->cpu_mask)
  416. resources_per_cpu[cpu]++;
  417. }
  418. rcu_read_unlock();
  419. for_each_online_cpu(cpu) {
  420. if (resources_per_cpu[cpu] < min) {
  421. min = resources_per_cpu[cpu];
  422. min_index = cpu;
  423. }
  424. }
  425. kfree(resources_per_cpu);
  426. }
  427. if (min_index == ~0) {
  428. cpumask_setall(*cpu_mask);
  429. return;
  430. }
  431. cpumask_set_cpu(min_index, *cpu_mask);
  432. }
  433. /**
  434. * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
  435. * @thi: drbd_thread object
  436. *
  437. * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
  438. * prematurely.
  439. */
  440. void drbd_thread_current_set_cpu(struct drbd_thread *thi)
  441. {
  442. struct drbd_resource *resource = thi->resource;
  443. struct task_struct *p = current;
  444. if (!thi->reset_cpu_mask)
  445. return;
  446. thi->reset_cpu_mask = 0;
  447. set_cpus_allowed_ptr(p, resource->cpu_mask);
  448. }
  449. #else
  450. #define drbd_calc_cpu_mask(A) ({})
  451. #endif
  452. /*
  453. * drbd_header_size - size of a packet header
  454. *
  455. * The header size is a multiple of 8, so any payload following the header is
  456. * word aligned on 64-bit architectures. (The bitmap send and receive code
  457. * relies on this.)
  458. */
  459. unsigned int drbd_header_size(struct drbd_connection *connection)
  460. {
  461. if (connection->agreed_pro_version >= 100) {
  462. BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100), 8));
  463. return sizeof(struct p_header100);
  464. } else {
  465. BUILD_BUG_ON(sizeof(struct p_header80) !=
  466. sizeof(struct p_header95));
  467. BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80), 8));
  468. return sizeof(struct p_header80);
  469. }
  470. }
  471. static unsigned int prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
  472. {
  473. h->magic = cpu_to_be32(DRBD_MAGIC);
  474. h->command = cpu_to_be16(cmd);
  475. h->length = cpu_to_be16(size);
  476. return sizeof(struct p_header80);
  477. }
  478. static unsigned int prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
  479. {
  480. h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
  481. h->command = cpu_to_be16(cmd);
  482. h->length = cpu_to_be32(size);
  483. return sizeof(struct p_header95);
  484. }
  485. static unsigned int prepare_header100(struct p_header100 *h, enum drbd_packet cmd,
  486. int size, int vnr)
  487. {
  488. h->magic = cpu_to_be32(DRBD_MAGIC_100);
  489. h->volume = cpu_to_be16(vnr);
  490. h->command = cpu_to_be16(cmd);
  491. h->length = cpu_to_be32(size);
  492. h->pad = 0;
  493. return sizeof(struct p_header100);
  494. }
  495. static unsigned int prepare_header(struct drbd_connection *connection, int vnr,
  496. void *buffer, enum drbd_packet cmd, int size)
  497. {
  498. if (connection->agreed_pro_version >= 100)
  499. return prepare_header100(buffer, cmd, size, vnr);
  500. else if (connection->agreed_pro_version >= 95 &&
  501. size > DRBD_MAX_SIZE_H80_PACKET)
  502. return prepare_header95(buffer, cmd, size);
  503. else
  504. return prepare_header80(buffer, cmd, size);
  505. }
  506. static void *__conn_prepare_command(struct drbd_connection *connection,
  507. struct drbd_socket *sock)
  508. {
  509. if (!sock->socket)
  510. return NULL;
  511. return sock->sbuf + drbd_header_size(connection);
  512. }
  513. void *conn_prepare_command(struct drbd_connection *connection, struct drbd_socket *sock)
  514. {
  515. void *p;
  516. mutex_lock(&sock->mutex);
  517. p = __conn_prepare_command(connection, sock);
  518. if (!p)
  519. mutex_unlock(&sock->mutex);
  520. return p;
  521. }
  522. void *drbd_prepare_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock)
  523. {
  524. return conn_prepare_command(peer_device->connection, sock);
  525. }
  526. static int __send_command(struct drbd_connection *connection, int vnr,
  527. struct drbd_socket *sock, enum drbd_packet cmd,
  528. unsigned int header_size, void *data,
  529. unsigned int size)
  530. {
  531. int msg_flags;
  532. int err;
  533. /*
  534. * Called with @data == NULL and the size of the data blocks in @size
  535. * for commands that send data blocks. For those commands, omit the
  536. * MSG_MORE flag: this will increase the likelihood that data blocks
  537. * which are page aligned on the sender will end up page aligned on the
  538. * receiver.
  539. */
  540. msg_flags = data ? MSG_MORE : 0;
  541. header_size += prepare_header(connection, vnr, sock->sbuf, cmd,
  542. header_size + size);
  543. err = drbd_send_all(connection, sock->socket, sock->sbuf, header_size,
  544. msg_flags);
  545. if (data && !err)
  546. err = drbd_send_all(connection, sock->socket, data, size, 0);
  547. /* DRBD protocol "pings" are latency critical.
  548. * This is supposed to trigger tcp_push_pending_frames() */
  549. if (!err && (cmd == P_PING || cmd == P_PING_ACK))
  550. tcp_sock_set_nodelay(sock->socket->sk);
  551. return err;
  552. }
  553. static int __conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock,
  554. enum drbd_packet cmd, unsigned int header_size,
  555. void *data, unsigned int size)
  556. {
  557. return __send_command(connection, 0, sock, cmd, header_size, data, size);
  558. }
  559. int conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock,
  560. enum drbd_packet cmd, unsigned int header_size,
  561. void *data, unsigned int size)
  562. {
  563. int err;
  564. err = __conn_send_command(connection, sock, cmd, header_size, data, size);
  565. mutex_unlock(&sock->mutex);
  566. return err;
  567. }
  568. int drbd_send_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock,
  569. enum drbd_packet cmd, unsigned int header_size,
  570. void *data, unsigned int size)
  571. {
  572. int err;
  573. err = __send_command(peer_device->connection, peer_device->device->vnr,
  574. sock, cmd, header_size, data, size);
  575. mutex_unlock(&sock->mutex);
  576. return err;
  577. }
  578. int drbd_send_ping(struct drbd_connection *connection)
  579. {
  580. struct drbd_socket *sock;
  581. sock = &connection->meta;
  582. if (!conn_prepare_command(connection, sock))
  583. return -EIO;
  584. return conn_send_command(connection, sock, P_PING, 0, NULL, 0);
  585. }
  586. int drbd_send_ping_ack(struct drbd_connection *connection)
  587. {
  588. struct drbd_socket *sock;
  589. sock = &connection->meta;
  590. if (!conn_prepare_command(connection, sock))
  591. return -EIO;
  592. return conn_send_command(connection, sock, P_PING_ACK, 0, NULL, 0);
  593. }
  594. int drbd_send_sync_param(struct drbd_peer_device *peer_device)
  595. {
  596. struct drbd_socket *sock;
  597. struct p_rs_param_95 *p;
  598. int size;
  599. const int apv = peer_device->connection->agreed_pro_version;
  600. enum drbd_packet cmd;
  601. struct net_conf *nc;
  602. struct disk_conf *dc;
  603. sock = &peer_device->connection->data;
  604. p = drbd_prepare_command(peer_device, sock);
  605. if (!p)
  606. return -EIO;
  607. rcu_read_lock();
  608. nc = rcu_dereference(peer_device->connection->net_conf);
  609. size = apv <= 87 ? sizeof(struct p_rs_param)
  610. : apv == 88 ? sizeof(struct p_rs_param)
  611. + strlen(nc->verify_alg) + 1
  612. : apv <= 94 ? sizeof(struct p_rs_param_89)
  613. : /* apv >= 95 */ sizeof(struct p_rs_param_95);
  614. cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
  615. /* initialize verify_alg and csums_alg */
  616. BUILD_BUG_ON(sizeof(p->algs) != 2 * SHARED_SECRET_MAX);
  617. memset(&p->algs, 0, sizeof(p->algs));
  618. if (get_ldev(peer_device->device)) {
  619. dc = rcu_dereference(peer_device->device->ldev->disk_conf);
  620. p->resync_rate = cpu_to_be32(dc->resync_rate);
  621. p->c_plan_ahead = cpu_to_be32(dc->c_plan_ahead);
  622. p->c_delay_target = cpu_to_be32(dc->c_delay_target);
  623. p->c_fill_target = cpu_to_be32(dc->c_fill_target);
  624. p->c_max_rate = cpu_to_be32(dc->c_max_rate);
  625. put_ldev(peer_device->device);
  626. } else {
  627. p->resync_rate = cpu_to_be32(DRBD_RESYNC_RATE_DEF);
  628. p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF);
  629. p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF);
  630. p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF);
  631. p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF);
  632. }
  633. if (apv >= 88)
  634. strscpy(p->verify_alg, nc->verify_alg);
  635. if (apv >= 89)
  636. strscpy(p->csums_alg, nc->csums_alg);
  637. rcu_read_unlock();
  638. return drbd_send_command(peer_device, sock, cmd, size, NULL, 0);
  639. }
  640. int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cmd)
  641. {
  642. struct drbd_socket *sock;
  643. struct p_protocol *p;
  644. struct net_conf *nc;
  645. size_t integrity_alg_len;
  646. int size, cf;
  647. sock = &connection->data;
  648. p = __conn_prepare_command(connection, sock);
  649. if (!p)
  650. return -EIO;
  651. rcu_read_lock();
  652. nc = rcu_dereference(connection->net_conf);
  653. if (nc->tentative && connection->agreed_pro_version < 92) {
  654. rcu_read_unlock();
  655. drbd_err(connection, "--dry-run is not supported by peer");
  656. return -EOPNOTSUPP;
  657. }
  658. size = sizeof(*p);
  659. if (connection->agreed_pro_version >= 87) {
  660. integrity_alg_len = strlen(nc->integrity_alg) + 1;
  661. size += integrity_alg_len;
  662. }
  663. p->protocol = cpu_to_be32(nc->wire_protocol);
  664. p->after_sb_0p = cpu_to_be32(nc->after_sb_0p);
  665. p->after_sb_1p = cpu_to_be32(nc->after_sb_1p);
  666. p->after_sb_2p = cpu_to_be32(nc->after_sb_2p);
  667. p->two_primaries = cpu_to_be32(nc->two_primaries);
  668. cf = 0;
  669. if (nc->discard_my_data)
  670. cf |= CF_DISCARD_MY_DATA;
  671. if (nc->tentative)
  672. cf |= CF_DRY_RUN;
  673. p->conn_flags = cpu_to_be32(cf);
  674. if (connection->agreed_pro_version >= 87)
  675. strscpy(p->integrity_alg, nc->integrity_alg, integrity_alg_len);
  676. rcu_read_unlock();
  677. return __conn_send_command(connection, sock, cmd, size, NULL, 0);
  678. }
  679. int drbd_send_protocol(struct drbd_connection *connection)
  680. {
  681. int err;
  682. mutex_lock(&connection->data.mutex);
  683. err = __drbd_send_protocol(connection, P_PROTOCOL);
  684. mutex_unlock(&connection->data.mutex);
  685. return err;
  686. }
  687. static int _drbd_send_uuids(struct drbd_peer_device *peer_device, u64 uuid_flags)
  688. {
  689. struct drbd_device *device = peer_device->device;
  690. struct drbd_socket *sock;
  691. struct p_uuids *p;
  692. int i;
  693. if (!get_ldev_if_state(device, D_NEGOTIATING))
  694. return 0;
  695. sock = &peer_device->connection->data;
  696. p = drbd_prepare_command(peer_device, sock);
  697. if (!p) {
  698. put_ldev(device);
  699. return -EIO;
  700. }
  701. spin_lock_irq(&device->ldev->md.uuid_lock);
  702. for (i = UI_CURRENT; i < UI_SIZE; i++)
  703. p->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
  704. spin_unlock_irq(&device->ldev->md.uuid_lock);
  705. device->comm_bm_set = drbd_bm_total_weight(device);
  706. p->uuid[UI_SIZE] = cpu_to_be64(device->comm_bm_set);
  707. rcu_read_lock();
  708. uuid_flags |= rcu_dereference(peer_device->connection->net_conf)->discard_my_data ? 1 : 0;
  709. rcu_read_unlock();
  710. uuid_flags |= test_bit(CRASHED_PRIMARY, &device->flags) ? 2 : 0;
  711. uuid_flags |= device->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
  712. p->uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
  713. put_ldev(device);
  714. return drbd_send_command(peer_device, sock, P_UUIDS, sizeof(*p), NULL, 0);
  715. }
  716. int drbd_send_uuids(struct drbd_peer_device *peer_device)
  717. {
  718. return _drbd_send_uuids(peer_device, 0);
  719. }
  720. int drbd_send_uuids_skip_initial_sync(struct drbd_peer_device *peer_device)
  721. {
  722. return _drbd_send_uuids(peer_device, 8);
  723. }
  724. void drbd_print_uuids(struct drbd_device *device, const char *text)
  725. {
  726. if (get_ldev_if_state(device, D_NEGOTIATING)) {
  727. u64 *uuid = device->ldev->md.uuid;
  728. drbd_info(device, "%s %016llX:%016llX:%016llX:%016llX\n",
  729. text,
  730. (unsigned long long)uuid[UI_CURRENT],
  731. (unsigned long long)uuid[UI_BITMAP],
  732. (unsigned long long)uuid[UI_HISTORY_START],
  733. (unsigned long long)uuid[UI_HISTORY_END]);
  734. put_ldev(device);
  735. } else {
  736. drbd_info(device, "%s effective data uuid: %016llX\n",
  737. text,
  738. (unsigned long long)device->ed_uuid);
  739. }
  740. }
  741. void drbd_gen_and_send_sync_uuid(struct drbd_peer_device *peer_device)
  742. {
  743. struct drbd_device *device = peer_device->device;
  744. struct drbd_socket *sock;
  745. struct p_rs_uuid *p;
  746. u64 uuid;
  747. D_ASSERT(device, device->state.disk == D_UP_TO_DATE);
  748. uuid = device->ldev->md.uuid[UI_BITMAP];
  749. if (uuid && uuid != UUID_JUST_CREATED)
  750. uuid = uuid + UUID_NEW_BM_OFFSET;
  751. else
  752. get_random_bytes(&uuid, sizeof(u64));
  753. drbd_uuid_set(device, UI_BITMAP, uuid);
  754. drbd_print_uuids(device, "updated sync UUID");
  755. drbd_md_sync(device);
  756. sock = &peer_device->connection->data;
  757. p = drbd_prepare_command(peer_device, sock);
  758. if (p) {
  759. p->uuid = cpu_to_be64(uuid);
  760. drbd_send_command(peer_device, sock, P_SYNC_UUID, sizeof(*p), NULL, 0);
  761. }
  762. }
  763. int drbd_send_sizes(struct drbd_peer_device *peer_device, int trigger_reply, enum dds_flags flags)
  764. {
  765. struct drbd_device *device = peer_device->device;
  766. struct drbd_socket *sock;
  767. struct p_sizes *p;
  768. sector_t d_size, u_size;
  769. int q_order_type;
  770. unsigned int max_bio_size;
  771. unsigned int packet_size;
  772. sock = &peer_device->connection->data;
  773. p = drbd_prepare_command(peer_device, sock);
  774. if (!p)
  775. return -EIO;
  776. packet_size = sizeof(*p);
  777. if (peer_device->connection->agreed_features & DRBD_FF_WSAME)
  778. packet_size += sizeof(p->qlim[0]);
  779. memset(p, 0, packet_size);
  780. if (get_ldev_if_state(device, D_NEGOTIATING)) {
  781. struct block_device *bdev = device->ldev->backing_bdev;
  782. struct request_queue *q = bdev_get_queue(bdev);
  783. d_size = drbd_get_max_capacity(device->ldev);
  784. rcu_read_lock();
  785. u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
  786. rcu_read_unlock();
  787. q_order_type = drbd_queue_order_type(device);
  788. max_bio_size = queue_max_hw_sectors(q) << 9;
  789. max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE);
  790. p->qlim->physical_block_size =
  791. cpu_to_be32(bdev_physical_block_size(bdev));
  792. p->qlim->logical_block_size =
  793. cpu_to_be32(bdev_logical_block_size(bdev));
  794. p->qlim->alignment_offset =
  795. cpu_to_be32(bdev_alignment_offset(bdev));
  796. p->qlim->io_min = cpu_to_be32(bdev_io_min(bdev));
  797. p->qlim->io_opt = cpu_to_be32(bdev_io_opt(bdev));
  798. p->qlim->discard_enabled = !!bdev_max_discard_sectors(bdev);
  799. put_ldev(device);
  800. } else {
  801. struct request_queue *q = device->rq_queue;
  802. p->qlim->physical_block_size =
  803. cpu_to_be32(queue_physical_block_size(q));
  804. p->qlim->logical_block_size =
  805. cpu_to_be32(queue_logical_block_size(q));
  806. p->qlim->alignment_offset = 0;
  807. p->qlim->io_min = cpu_to_be32(queue_io_min(q));
  808. p->qlim->io_opt = cpu_to_be32(queue_io_opt(q));
  809. p->qlim->discard_enabled = 0;
  810. d_size = 0;
  811. u_size = 0;
  812. q_order_type = QUEUE_ORDERED_NONE;
  813. max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
  814. }
  815. if (peer_device->connection->agreed_pro_version <= 94)
  816. max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
  817. else if (peer_device->connection->agreed_pro_version < 100)
  818. max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE_P95);
  819. p->d_size = cpu_to_be64(d_size);
  820. p->u_size = cpu_to_be64(u_size);
  821. if (trigger_reply)
  822. p->c_size = 0;
  823. else
  824. p->c_size = cpu_to_be64(get_capacity(device->vdisk));
  825. p->max_bio_size = cpu_to_be32(max_bio_size);
  826. p->queue_order_type = cpu_to_be16(q_order_type);
  827. p->dds_flags = cpu_to_be16(flags);
  828. return drbd_send_command(peer_device, sock, P_SIZES, packet_size, NULL, 0);
  829. }
  830. /**
  831. * drbd_send_current_state() - Sends the drbd state to the peer
  832. * @peer_device: DRBD peer device.
  833. */
  834. int drbd_send_current_state(struct drbd_peer_device *peer_device)
  835. {
  836. struct drbd_socket *sock;
  837. struct p_state *p;
  838. sock = &peer_device->connection->data;
  839. p = drbd_prepare_command(peer_device, sock);
  840. if (!p)
  841. return -EIO;
  842. p->state = cpu_to_be32(peer_device->device->state.i); /* Within the send mutex */
  843. return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0);
  844. }
  845. /**
  846. * drbd_send_state() - After a state change, sends the new state to the peer
  847. * @peer_device: DRBD peer device.
  848. * @state: the state to send, not necessarily the current state.
  849. *
  850. * Each state change queues an "after_state_ch" work, which will eventually
  851. * send the resulting new state to the peer. If more state changes happen
  852. * between queuing and processing of the after_state_ch work, we still
  853. * want to send each intermediary state in the order it occurred.
  854. */
  855. int drbd_send_state(struct drbd_peer_device *peer_device, union drbd_state state)
  856. {
  857. struct drbd_socket *sock;
  858. struct p_state *p;
  859. sock = &peer_device->connection->data;
  860. p = drbd_prepare_command(peer_device, sock);
  861. if (!p)
  862. return -EIO;
  863. p->state = cpu_to_be32(state.i); /* Within the send mutex */
  864. return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0);
  865. }
  866. int drbd_send_state_req(struct drbd_peer_device *peer_device, union drbd_state mask, union drbd_state val)
  867. {
  868. struct drbd_socket *sock;
  869. struct p_req_state *p;
  870. sock = &peer_device->connection->data;
  871. p = drbd_prepare_command(peer_device, sock);
  872. if (!p)
  873. return -EIO;
  874. p->mask = cpu_to_be32(mask.i);
  875. p->val = cpu_to_be32(val.i);
  876. return drbd_send_command(peer_device, sock, P_STATE_CHG_REQ, sizeof(*p), NULL, 0);
  877. }
  878. int conn_send_state_req(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
  879. {
  880. enum drbd_packet cmd;
  881. struct drbd_socket *sock;
  882. struct p_req_state *p;
  883. cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REQ : P_CONN_ST_CHG_REQ;
  884. sock = &connection->data;
  885. p = conn_prepare_command(connection, sock);
  886. if (!p)
  887. return -EIO;
  888. p->mask = cpu_to_be32(mask.i);
  889. p->val = cpu_to_be32(val.i);
  890. return conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0);
  891. }
  892. void drbd_send_sr_reply(struct drbd_peer_device *peer_device, enum drbd_state_rv retcode)
  893. {
  894. struct drbd_socket *sock;
  895. struct p_req_state_reply *p;
  896. sock = &peer_device->connection->meta;
  897. p = drbd_prepare_command(peer_device, sock);
  898. if (p) {
  899. p->retcode = cpu_to_be32(retcode);
  900. drbd_send_command(peer_device, sock, P_STATE_CHG_REPLY, sizeof(*p), NULL, 0);
  901. }
  902. }
  903. void conn_send_sr_reply(struct drbd_connection *connection, enum drbd_state_rv retcode)
  904. {
  905. struct drbd_socket *sock;
  906. struct p_req_state_reply *p;
  907. enum drbd_packet cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
  908. sock = &connection->meta;
  909. p = conn_prepare_command(connection, sock);
  910. if (p) {
  911. p->retcode = cpu_to_be32(retcode);
  912. conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0);
  913. }
  914. }
  915. static void dcbp_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
  916. {
  917. BUG_ON(code & ~0xf);
  918. p->encoding = (p->encoding & ~0xf) | code;
  919. }
  920. static void dcbp_set_start(struct p_compressed_bm *p, int set)
  921. {
  922. p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
  923. }
  924. static void dcbp_set_pad_bits(struct p_compressed_bm *p, int n)
  925. {
  926. BUG_ON(n & ~0x7);
  927. p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
  928. }
  929. static int fill_bitmap_rle_bits(struct drbd_device *device,
  930. struct p_compressed_bm *p,
  931. unsigned int size,
  932. struct bm_xfer_ctx *c)
  933. {
  934. struct bitstream bs;
  935. unsigned long plain_bits;
  936. unsigned long tmp;
  937. unsigned long rl;
  938. unsigned len;
  939. unsigned toggle;
  940. int bits, use_rle;
  941. /* may we use this feature? */
  942. rcu_read_lock();
  943. use_rle = rcu_dereference(first_peer_device(device)->connection->net_conf)->use_rle;
  944. rcu_read_unlock();
  945. if (!use_rle || first_peer_device(device)->connection->agreed_pro_version < 90)
  946. return 0;
  947. if (c->bit_offset >= c->bm_bits)
  948. return 0; /* nothing to do. */
  949. /* use at most thus many bytes */
  950. bitstream_init(&bs, p->code, size, 0);
  951. memset(p->code, 0, size);
  952. /* plain bits covered in this code string */
  953. plain_bits = 0;
  954. /* p->encoding & 0x80 stores whether the first run length is set.
  955. * bit offset is implicit.
  956. * start with toggle == 2 to be able to tell the first iteration */
  957. toggle = 2;
  958. /* see how much plain bits we can stuff into one packet
  959. * using RLE and VLI. */
  960. do {
  961. tmp = (toggle == 0) ? _drbd_bm_find_next_zero(device, c->bit_offset)
  962. : _drbd_bm_find_next(device, c->bit_offset);
  963. if (tmp == -1UL)
  964. tmp = c->bm_bits;
  965. rl = tmp - c->bit_offset;
  966. if (toggle == 2) { /* first iteration */
  967. if (rl == 0) {
  968. /* the first checked bit was set,
  969. * store start value, */
  970. dcbp_set_start(p, 1);
  971. /* but skip encoding of zero run length */
  972. toggle = !toggle;
  973. continue;
  974. }
  975. dcbp_set_start(p, 0);
  976. }
  977. /* paranoia: catch zero runlength.
  978. * can only happen if bitmap is modified while we scan it. */
  979. if (rl == 0) {
  980. drbd_err(device, "unexpected zero runlength while encoding bitmap "
  981. "t:%u bo:%lu\n", toggle, c->bit_offset);
  982. return -1;
  983. }
  984. bits = vli_encode_bits(&bs, rl);
  985. if (bits == -ENOBUFS) /* buffer full */
  986. break;
  987. if (bits <= 0) {
  988. drbd_err(device, "error while encoding bitmap: %d\n", bits);
  989. return 0;
  990. }
  991. toggle = !toggle;
  992. plain_bits += rl;
  993. c->bit_offset = tmp;
  994. } while (c->bit_offset < c->bm_bits);
  995. len = bs.cur.b - p->code + !!bs.cur.bit;
  996. if (plain_bits < (len << 3)) {
  997. /* incompressible with this method.
  998. * we need to rewind both word and bit position. */
  999. c->bit_offset -= plain_bits;
  1000. bm_xfer_ctx_bit_to_word_offset(c);
  1001. c->bit_offset = c->word_offset * BITS_PER_LONG;
  1002. return 0;
  1003. }
  1004. /* RLE + VLI was able to compress it just fine.
  1005. * update c->word_offset. */
  1006. bm_xfer_ctx_bit_to_word_offset(c);
  1007. /* store pad_bits */
  1008. dcbp_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
  1009. return len;
  1010. }
  1011. /*
  1012. * send_bitmap_rle_or_plain
  1013. *
  1014. * Return 0 when done, 1 when another iteration is needed, and a negative error
  1015. * code upon failure.
  1016. */
  1017. static int
  1018. send_bitmap_rle_or_plain(struct drbd_peer_device *peer_device, struct bm_xfer_ctx *c)
  1019. {
  1020. struct drbd_device *device = peer_device->device;
  1021. struct drbd_socket *sock = &peer_device->connection->data;
  1022. unsigned int header_size = drbd_header_size(peer_device->connection);
  1023. struct p_compressed_bm *p = sock->sbuf + header_size;
  1024. int len, err;
  1025. len = fill_bitmap_rle_bits(device, p,
  1026. DRBD_SOCKET_BUFFER_SIZE - header_size - sizeof(*p), c);
  1027. if (len < 0)
  1028. return -EIO;
  1029. if (len) {
  1030. dcbp_set_code(p, RLE_VLI_Bits);
  1031. err = __send_command(peer_device->connection, device->vnr, sock,
  1032. P_COMPRESSED_BITMAP, sizeof(*p) + len,
  1033. NULL, 0);
  1034. c->packets[0]++;
  1035. c->bytes[0] += header_size + sizeof(*p) + len;
  1036. if (c->bit_offset >= c->bm_bits)
  1037. len = 0; /* DONE */
  1038. } else {
  1039. /* was not compressible.
  1040. * send a buffer full of plain text bits instead. */
  1041. unsigned int data_size;
  1042. unsigned long num_words;
  1043. unsigned long *p = sock->sbuf + header_size;
  1044. data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
  1045. num_words = min_t(size_t, data_size / sizeof(*p),
  1046. c->bm_words - c->word_offset);
  1047. len = num_words * sizeof(*p);
  1048. if (len)
  1049. drbd_bm_get_lel(device, c->word_offset, num_words, p);
  1050. err = __send_command(peer_device->connection, device->vnr, sock, P_BITMAP,
  1051. len, NULL, 0);
  1052. c->word_offset += num_words;
  1053. c->bit_offset = c->word_offset * BITS_PER_LONG;
  1054. c->packets[1]++;
  1055. c->bytes[1] += header_size + len;
  1056. if (c->bit_offset > c->bm_bits)
  1057. c->bit_offset = c->bm_bits;
  1058. }
  1059. if (!err) {
  1060. if (len == 0) {
  1061. INFO_bm_xfer_stats(peer_device, "send", c);
  1062. return 0;
  1063. } else
  1064. return 1;
  1065. }
  1066. return -EIO;
  1067. }
  1068. /* See the comment at receive_bitmap() */
  1069. static int _drbd_send_bitmap(struct drbd_device *device,
  1070. struct drbd_peer_device *peer_device)
  1071. {
  1072. struct bm_xfer_ctx c;
  1073. int err;
  1074. if (!expect(device, device->bitmap))
  1075. return false;
  1076. if (get_ldev(device)) {
  1077. if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC)) {
  1078. drbd_info(device, "Writing the whole bitmap, MDF_FullSync was set.\n");
  1079. drbd_bm_set_all(device);
  1080. if (drbd_bm_write(device, peer_device)) {
  1081. /* write_bm did fail! Leave full sync flag set in Meta P_DATA
  1082. * but otherwise process as per normal - need to tell other
  1083. * side that a full resync is required! */
  1084. drbd_err(device, "Failed to write bitmap to disk!\n");
  1085. } else {
  1086. drbd_md_clear_flag(device, MDF_FULL_SYNC);
  1087. drbd_md_sync(device);
  1088. }
  1089. }
  1090. put_ldev(device);
  1091. }
  1092. c = (struct bm_xfer_ctx) {
  1093. .bm_bits = drbd_bm_bits(device),
  1094. .bm_words = drbd_bm_words(device),
  1095. };
  1096. do {
  1097. err = send_bitmap_rle_or_plain(peer_device, &c);
  1098. } while (err > 0);
  1099. return err == 0;
  1100. }
  1101. int drbd_send_bitmap(struct drbd_device *device, struct drbd_peer_device *peer_device)
  1102. {
  1103. struct drbd_socket *sock = &peer_device->connection->data;
  1104. int err = -1;
  1105. mutex_lock(&sock->mutex);
  1106. if (sock->socket)
  1107. err = !_drbd_send_bitmap(device, peer_device);
  1108. mutex_unlock(&sock->mutex);
  1109. return err;
  1110. }
  1111. void drbd_send_b_ack(struct drbd_connection *connection, u32 barrier_nr, u32 set_size)
  1112. {
  1113. struct drbd_socket *sock;
  1114. struct p_barrier_ack *p;
  1115. if (connection->cstate < C_WF_REPORT_PARAMS)
  1116. return;
  1117. sock = &connection->meta;
  1118. p = conn_prepare_command(connection, sock);
  1119. if (!p)
  1120. return;
  1121. p->barrier = barrier_nr;
  1122. p->set_size = cpu_to_be32(set_size);
  1123. conn_send_command(connection, sock, P_BARRIER_ACK, sizeof(*p), NULL, 0);
  1124. }
  1125. /**
  1126. * _drbd_send_ack() - Sends an ack packet
  1127. * @peer_device: DRBD peer device.
  1128. * @cmd: Packet command code.
  1129. * @sector: sector, needs to be in big endian byte order
  1130. * @blksize: size in byte, needs to be in big endian byte order
  1131. * @block_id: Id, big endian byte order
  1132. */
  1133. static int _drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1134. u64 sector, u32 blksize, u64 block_id)
  1135. {
  1136. struct drbd_socket *sock;
  1137. struct p_block_ack *p;
  1138. if (peer_device->device->state.conn < C_CONNECTED)
  1139. return -EIO;
  1140. sock = &peer_device->connection->meta;
  1141. p = drbd_prepare_command(peer_device, sock);
  1142. if (!p)
  1143. return -EIO;
  1144. p->sector = sector;
  1145. p->block_id = block_id;
  1146. p->blksize = blksize;
  1147. p->seq_num = cpu_to_be32(atomic_inc_return(&peer_device->device->packet_seq));
  1148. return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0);
  1149. }
  1150. /* dp->sector and dp->block_id already/still in network byte order,
  1151. * data_size is payload size according to dp->head,
  1152. * and may need to be corrected for digest size. */
  1153. void drbd_send_ack_dp(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1154. struct p_data *dp, int data_size)
  1155. {
  1156. if (peer_device->connection->peer_integrity_tfm)
  1157. data_size -= crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm);
  1158. _drbd_send_ack(peer_device, cmd, dp->sector, cpu_to_be32(data_size),
  1159. dp->block_id);
  1160. }
  1161. void drbd_send_ack_rp(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1162. struct p_block_req *rp)
  1163. {
  1164. _drbd_send_ack(peer_device, cmd, rp->sector, rp->blksize, rp->block_id);
  1165. }
  1166. /**
  1167. * drbd_send_ack() - Sends an ack packet
  1168. * @peer_device: DRBD peer device
  1169. * @cmd: packet command code
  1170. * @peer_req: peer request
  1171. */
  1172. int drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1173. struct drbd_peer_request *peer_req)
  1174. {
  1175. return _drbd_send_ack(peer_device, cmd,
  1176. cpu_to_be64(peer_req->i.sector),
  1177. cpu_to_be32(peer_req->i.size),
  1178. peer_req->block_id);
  1179. }
  1180. /* This function misuses the block_id field to signal if the blocks
  1181. * are is sync or not. */
  1182. int drbd_send_ack_ex(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1183. sector_t sector, int blksize, u64 block_id)
  1184. {
  1185. return _drbd_send_ack(peer_device, cmd,
  1186. cpu_to_be64(sector),
  1187. cpu_to_be32(blksize),
  1188. cpu_to_be64(block_id));
  1189. }
  1190. int drbd_send_rs_deallocated(struct drbd_peer_device *peer_device,
  1191. struct drbd_peer_request *peer_req)
  1192. {
  1193. struct drbd_socket *sock;
  1194. struct p_block_desc *p;
  1195. sock = &peer_device->connection->data;
  1196. p = drbd_prepare_command(peer_device, sock);
  1197. if (!p)
  1198. return -EIO;
  1199. p->sector = cpu_to_be64(peer_req->i.sector);
  1200. p->blksize = cpu_to_be32(peer_req->i.size);
  1201. p->pad = 0;
  1202. return drbd_send_command(peer_device, sock, P_RS_DEALLOCATED, sizeof(*p), NULL, 0);
  1203. }
  1204. int drbd_send_drequest(struct drbd_peer_device *peer_device, int cmd,
  1205. sector_t sector, int size, u64 block_id)
  1206. {
  1207. struct drbd_socket *sock;
  1208. struct p_block_req *p;
  1209. sock = &peer_device->connection->data;
  1210. p = drbd_prepare_command(peer_device, sock);
  1211. if (!p)
  1212. return -EIO;
  1213. p->sector = cpu_to_be64(sector);
  1214. p->block_id = block_id;
  1215. p->blksize = cpu_to_be32(size);
  1216. return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0);
  1217. }
  1218. int drbd_send_drequest_csum(struct drbd_peer_device *peer_device, sector_t sector, int size,
  1219. void *digest, int digest_size, enum drbd_packet cmd)
  1220. {
  1221. struct drbd_socket *sock;
  1222. struct p_block_req *p;
  1223. /* FIXME: Put the digest into the preallocated socket buffer. */
  1224. sock = &peer_device->connection->data;
  1225. p = drbd_prepare_command(peer_device, sock);
  1226. if (!p)
  1227. return -EIO;
  1228. p->sector = cpu_to_be64(sector);
  1229. p->block_id = ID_SYNCER /* unused */;
  1230. p->blksize = cpu_to_be32(size);
  1231. return drbd_send_command(peer_device, sock, cmd, sizeof(*p), digest, digest_size);
  1232. }
  1233. int drbd_send_ov_request(struct drbd_peer_device *peer_device, sector_t sector, int size)
  1234. {
  1235. struct drbd_socket *sock;
  1236. struct p_block_req *p;
  1237. sock = &peer_device->connection->data;
  1238. p = drbd_prepare_command(peer_device, sock);
  1239. if (!p)
  1240. return -EIO;
  1241. p->sector = cpu_to_be64(sector);
  1242. p->block_id = ID_SYNCER /* unused */;
  1243. p->blksize = cpu_to_be32(size);
  1244. return drbd_send_command(peer_device, sock, P_OV_REQUEST, sizeof(*p), NULL, 0);
  1245. }
  1246. /* called on sndtimeo
  1247. * returns false if we should retry,
  1248. * true if we think connection is dead
  1249. */
  1250. static int we_should_drop_the_connection(struct drbd_connection *connection, struct socket *sock)
  1251. {
  1252. int drop_it;
  1253. /* long elapsed = (long)(jiffies - device->last_received); */
  1254. drop_it = connection->meta.socket == sock
  1255. || !connection->ack_receiver.task
  1256. || get_t_state(&connection->ack_receiver) != RUNNING
  1257. || connection->cstate < C_WF_REPORT_PARAMS;
  1258. if (drop_it)
  1259. return true;
  1260. drop_it = !--connection->ko_count;
  1261. if (!drop_it) {
  1262. drbd_err(connection, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
  1263. current->comm, current->pid, connection->ko_count);
  1264. request_ping(connection);
  1265. }
  1266. return drop_it; /* && (device->state == R_PRIMARY) */;
  1267. }
  1268. static void drbd_update_congested(struct drbd_connection *connection)
  1269. {
  1270. struct sock *sk = connection->data.socket->sk;
  1271. if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
  1272. set_bit(NET_CONGESTED, &connection->flags);
  1273. }
  1274. /* The idea of sendpage seems to be to put some kind of reference
  1275. * to the page into the skb, and to hand it over to the NIC. In
  1276. * this process get_page() gets called.
  1277. *
  1278. * As soon as the page was really sent over the network put_page()
  1279. * gets called by some part of the network layer. [ NIC driver? ]
  1280. *
  1281. * [ get_page() / put_page() increment/decrement the count. If count
  1282. * reaches 0 the page will be freed. ]
  1283. *
  1284. * This works nicely with pages from FSs.
  1285. * But this means that in protocol A we might signal IO completion too early!
  1286. *
  1287. * In order not to corrupt data during a resync we must make sure
  1288. * that we do not reuse our own buffer pages (EEs) to early, therefore
  1289. * we have the net_ee list.
  1290. *
  1291. * XFS seems to have problems, still, it submits pages with page_count == 0!
  1292. * As a workaround, we disable sendpage on pages
  1293. * with page_count == 0 or PageSlab.
  1294. */
  1295. static int _drbd_no_send_page(struct drbd_peer_device *peer_device, struct page *page,
  1296. int offset, size_t size, unsigned msg_flags)
  1297. {
  1298. struct socket *socket;
  1299. void *addr;
  1300. int err;
  1301. socket = peer_device->connection->data.socket;
  1302. addr = kmap(page) + offset;
  1303. err = drbd_send_all(peer_device->connection, socket, addr, size, msg_flags);
  1304. kunmap(page);
  1305. if (!err)
  1306. peer_device->device->send_cnt += size >> 9;
  1307. return err;
  1308. }
  1309. static int _drbd_send_page(struct drbd_peer_device *peer_device, struct page *page,
  1310. int offset, size_t size, unsigned msg_flags)
  1311. {
  1312. struct socket *socket = peer_device->connection->data.socket;
  1313. struct msghdr msg = { .msg_flags = msg_flags, };
  1314. struct bio_vec bvec;
  1315. int len = size;
  1316. int err = -EIO;
  1317. /* e.g. XFS meta- & log-data is in slab pages, which have a
  1318. * page_count of 0 and/or have PageSlab() set.
  1319. * we cannot use send_page for those, as that does get_page();
  1320. * put_page(); and would cause either a VM_BUG directly, or
  1321. * __page_cache_release a page that would actually still be referenced
  1322. * by someone, leading to some obscure delayed Oops somewhere else. */
  1323. if (!drbd_disable_sendpage && sendpages_ok(page, len, offset))
  1324. msg.msg_flags |= MSG_NOSIGNAL | MSG_SPLICE_PAGES;
  1325. drbd_update_congested(peer_device->connection);
  1326. do {
  1327. int sent;
  1328. bvec_set_page(&bvec, page, len, offset);
  1329. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
  1330. sent = sock_sendmsg(socket, &msg);
  1331. if (sent <= 0) {
  1332. if (sent == -EAGAIN) {
  1333. if (we_should_drop_the_connection(peer_device->connection, socket))
  1334. break;
  1335. continue;
  1336. }
  1337. drbd_warn(peer_device->device, "%s: size=%d len=%d sent=%d\n",
  1338. __func__, (int)size, len, sent);
  1339. if (sent < 0)
  1340. err = sent;
  1341. break;
  1342. }
  1343. len -= sent;
  1344. offset += sent;
  1345. } while (len > 0 /* THINK && device->cstate >= C_CONNECTED*/);
  1346. clear_bit(NET_CONGESTED, &peer_device->connection->flags);
  1347. if (len == 0) {
  1348. err = 0;
  1349. peer_device->device->send_cnt += size >> 9;
  1350. }
  1351. return err;
  1352. }
  1353. static int _drbd_send_bio(struct drbd_peer_device *peer_device, struct bio *bio)
  1354. {
  1355. struct bio_vec bvec;
  1356. struct bvec_iter iter;
  1357. /* hint all but last page with MSG_MORE */
  1358. bio_for_each_segment(bvec, bio, iter) {
  1359. int err;
  1360. err = _drbd_no_send_page(peer_device, bvec.bv_page,
  1361. bvec.bv_offset, bvec.bv_len,
  1362. bio_iter_last(bvec, iter)
  1363. ? 0 : MSG_MORE);
  1364. if (err)
  1365. return err;
  1366. }
  1367. return 0;
  1368. }
  1369. static int _drbd_send_zc_bio(struct drbd_peer_device *peer_device, struct bio *bio)
  1370. {
  1371. struct bio_vec bvec;
  1372. struct bvec_iter iter;
  1373. /* hint all but last page with MSG_MORE */
  1374. bio_for_each_segment(bvec, bio, iter) {
  1375. int err;
  1376. err = _drbd_send_page(peer_device, bvec.bv_page,
  1377. bvec.bv_offset, bvec.bv_len,
  1378. bio_iter_last(bvec, iter) ? 0 : MSG_MORE);
  1379. if (err)
  1380. return err;
  1381. }
  1382. return 0;
  1383. }
  1384. static int _drbd_send_zc_ee(struct drbd_peer_device *peer_device,
  1385. struct drbd_peer_request *peer_req)
  1386. {
  1387. bool use_sendpage = !(peer_req->flags & EE_RELEASE_TO_MEMPOOL);
  1388. struct page *page = peer_req->pages;
  1389. unsigned len = peer_req->i.size;
  1390. int err;
  1391. /* hint all but last page with MSG_MORE */
  1392. page_chain_for_each(page) {
  1393. unsigned l = min_t(unsigned, len, PAGE_SIZE);
  1394. if (likely(use_sendpage))
  1395. err = _drbd_send_page(peer_device, page, 0, l,
  1396. page_chain_next(page) ? MSG_MORE : 0);
  1397. else
  1398. err = _drbd_no_send_page(peer_device, page, 0, l,
  1399. page_chain_next(page) ? MSG_MORE : 0);
  1400. if (err)
  1401. return err;
  1402. len -= l;
  1403. }
  1404. return 0;
  1405. }
  1406. static u32 bio_flags_to_wire(struct drbd_connection *connection,
  1407. struct bio *bio)
  1408. {
  1409. if (connection->agreed_pro_version >= 95)
  1410. return (bio->bi_opf & REQ_SYNC ? DP_RW_SYNC : 0) |
  1411. (bio->bi_opf & REQ_FUA ? DP_FUA : 0) |
  1412. (bio->bi_opf & REQ_PREFLUSH ? DP_FLUSH : 0) |
  1413. (bio_op(bio) == REQ_OP_DISCARD ? DP_DISCARD : 0) |
  1414. (bio_op(bio) == REQ_OP_WRITE_ZEROES ?
  1415. ((connection->agreed_features & DRBD_FF_WZEROES) ?
  1416. (DP_ZEROES |(!(bio->bi_opf & REQ_NOUNMAP) ? DP_DISCARD : 0))
  1417. : DP_DISCARD)
  1418. : 0);
  1419. else
  1420. return bio->bi_opf & REQ_SYNC ? DP_RW_SYNC : 0;
  1421. }
  1422. /* Used to send write or TRIM aka REQ_OP_DISCARD requests
  1423. * R_PRIMARY -> Peer (P_DATA, P_TRIM)
  1424. */
  1425. int drbd_send_dblock(struct drbd_peer_device *peer_device, struct drbd_request *req)
  1426. {
  1427. struct drbd_device *device = peer_device->device;
  1428. struct drbd_socket *sock;
  1429. struct p_data *p;
  1430. void *digest_out;
  1431. unsigned int dp_flags = 0;
  1432. int digest_size;
  1433. int err;
  1434. sock = &peer_device->connection->data;
  1435. p = drbd_prepare_command(peer_device, sock);
  1436. digest_size = peer_device->connection->integrity_tfm ?
  1437. crypto_shash_digestsize(peer_device->connection->integrity_tfm) : 0;
  1438. if (!p)
  1439. return -EIO;
  1440. p->sector = cpu_to_be64(req->i.sector);
  1441. p->block_id = (unsigned long)req;
  1442. p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq));
  1443. dp_flags = bio_flags_to_wire(peer_device->connection, req->master_bio);
  1444. if (device->state.conn >= C_SYNC_SOURCE &&
  1445. device->state.conn <= C_PAUSED_SYNC_T)
  1446. dp_flags |= DP_MAY_SET_IN_SYNC;
  1447. if (peer_device->connection->agreed_pro_version >= 100) {
  1448. if (req->rq_state & RQ_EXP_RECEIVE_ACK)
  1449. dp_flags |= DP_SEND_RECEIVE_ACK;
  1450. /* During resync, request an explicit write ack,
  1451. * even in protocol != C */
  1452. if (req->rq_state & RQ_EXP_WRITE_ACK
  1453. || (dp_flags & DP_MAY_SET_IN_SYNC))
  1454. dp_flags |= DP_SEND_WRITE_ACK;
  1455. }
  1456. p->dp_flags = cpu_to_be32(dp_flags);
  1457. if (dp_flags & (DP_DISCARD|DP_ZEROES)) {
  1458. enum drbd_packet cmd = (dp_flags & DP_ZEROES) ? P_ZEROES : P_TRIM;
  1459. struct p_trim *t = (struct p_trim*)p;
  1460. t->size = cpu_to_be32(req->i.size);
  1461. err = __send_command(peer_device->connection, device->vnr, sock, cmd, sizeof(*t), NULL, 0);
  1462. goto out;
  1463. }
  1464. digest_out = p + 1;
  1465. /* our digest is still only over the payload.
  1466. * TRIM does not carry any payload. */
  1467. if (digest_size)
  1468. drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, digest_out);
  1469. err = __send_command(peer_device->connection, device->vnr, sock, P_DATA,
  1470. sizeof(*p) + digest_size, NULL, req->i.size);
  1471. if (!err) {
  1472. /* For protocol A, we have to memcpy the payload into
  1473. * socket buffers, as we may complete right away
  1474. * as soon as we handed it over to tcp, at which point the data
  1475. * pages may become invalid.
  1476. *
  1477. * For data-integrity enabled, we copy it as well, so we can be
  1478. * sure that even if the bio pages may still be modified, it
  1479. * won't change the data on the wire, thus if the digest checks
  1480. * out ok after sending on this side, but does not fit on the
  1481. * receiving side, we sure have detected corruption elsewhere.
  1482. */
  1483. if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)) || digest_size)
  1484. err = _drbd_send_bio(peer_device, req->master_bio);
  1485. else
  1486. err = _drbd_send_zc_bio(peer_device, req->master_bio);
  1487. /* double check digest, sometimes buffers have been modified in flight. */
  1488. if (digest_size > 0 && digest_size <= 64) {
  1489. /* 64 byte, 512 bit, is the largest digest size
  1490. * currently supported in kernel crypto. */
  1491. unsigned char digest[64];
  1492. drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, digest);
  1493. if (memcmp(p + 1, digest, digest_size)) {
  1494. drbd_warn(device,
  1495. "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
  1496. (unsigned long long)req->i.sector, req->i.size);
  1497. }
  1498. } /* else if (digest_size > 64) {
  1499. ... Be noisy about digest too large ...
  1500. } */
  1501. }
  1502. out:
  1503. mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
  1504. return err;
  1505. }
  1506. /* answer packet, used to send data back for read requests:
  1507. * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
  1508. * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
  1509. */
  1510. int drbd_send_block(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1511. struct drbd_peer_request *peer_req)
  1512. {
  1513. struct drbd_device *device = peer_device->device;
  1514. struct drbd_socket *sock;
  1515. struct p_data *p;
  1516. int err;
  1517. int digest_size;
  1518. sock = &peer_device->connection->data;
  1519. p = drbd_prepare_command(peer_device, sock);
  1520. digest_size = peer_device->connection->integrity_tfm ?
  1521. crypto_shash_digestsize(peer_device->connection->integrity_tfm) : 0;
  1522. if (!p)
  1523. return -EIO;
  1524. p->sector = cpu_to_be64(peer_req->i.sector);
  1525. p->block_id = peer_req->block_id;
  1526. p->seq_num = 0; /* unused */
  1527. p->dp_flags = 0;
  1528. if (digest_size)
  1529. drbd_csum_ee(peer_device->connection->integrity_tfm, peer_req, p + 1);
  1530. err = __send_command(peer_device->connection, device->vnr, sock, cmd, sizeof(*p) + digest_size, NULL, peer_req->i.size);
  1531. if (!err)
  1532. err = _drbd_send_zc_ee(peer_device, peer_req);
  1533. mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
  1534. return err;
  1535. }
  1536. int drbd_send_out_of_sync(struct drbd_peer_device *peer_device, struct drbd_request *req)
  1537. {
  1538. struct drbd_socket *sock;
  1539. struct p_block_desc *p;
  1540. sock = &peer_device->connection->data;
  1541. p = drbd_prepare_command(peer_device, sock);
  1542. if (!p)
  1543. return -EIO;
  1544. p->sector = cpu_to_be64(req->i.sector);
  1545. p->blksize = cpu_to_be32(req->i.size);
  1546. return drbd_send_command(peer_device, sock, P_OUT_OF_SYNC, sizeof(*p), NULL, 0);
  1547. }
  1548. /*
  1549. drbd_send distinguishes two cases:
  1550. Packets sent via the data socket "sock"
  1551. and packets sent via the meta data socket "msock"
  1552. sock msock
  1553. -----------------+-------------------------+------------------------------
  1554. timeout conf.timeout / 2 conf.timeout / 2
  1555. timeout action send a ping via msock Abort communication
  1556. and close all sockets
  1557. */
  1558. /*
  1559. * you must have down()ed the appropriate [m]sock_mutex elsewhere!
  1560. */
  1561. int drbd_send(struct drbd_connection *connection, struct socket *sock,
  1562. void *buf, size_t size, unsigned msg_flags)
  1563. {
  1564. struct kvec iov = {.iov_base = buf, .iov_len = size};
  1565. struct msghdr msg = {.msg_flags = msg_flags | MSG_NOSIGNAL};
  1566. int rv, sent = 0;
  1567. if (!sock)
  1568. return -EBADR;
  1569. /* THINK if (signal_pending) return ... ? */
  1570. iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, size);
  1571. if (sock == connection->data.socket) {
  1572. rcu_read_lock();
  1573. connection->ko_count = rcu_dereference(connection->net_conf)->ko_count;
  1574. rcu_read_unlock();
  1575. drbd_update_congested(connection);
  1576. }
  1577. do {
  1578. rv = sock_sendmsg(sock, &msg);
  1579. if (rv == -EAGAIN) {
  1580. if (we_should_drop_the_connection(connection, sock))
  1581. break;
  1582. else
  1583. continue;
  1584. }
  1585. if (rv == -EINTR) {
  1586. flush_signals(current);
  1587. rv = 0;
  1588. }
  1589. if (rv < 0)
  1590. break;
  1591. sent += rv;
  1592. } while (sent < size);
  1593. if (sock == connection->data.socket)
  1594. clear_bit(NET_CONGESTED, &connection->flags);
  1595. if (rv <= 0) {
  1596. if (rv != -EAGAIN) {
  1597. drbd_err(connection, "%s_sendmsg returned %d\n",
  1598. sock == connection->meta.socket ? "msock" : "sock",
  1599. rv);
  1600. conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD);
  1601. } else
  1602. conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD);
  1603. }
  1604. return sent;
  1605. }
  1606. /*
  1607. * drbd_send_all - Send an entire buffer
  1608. *
  1609. * Returns 0 upon success and a negative error value otherwise.
  1610. */
  1611. int drbd_send_all(struct drbd_connection *connection, struct socket *sock, void *buffer,
  1612. size_t size, unsigned msg_flags)
  1613. {
  1614. int err;
  1615. err = drbd_send(connection, sock, buffer, size, msg_flags);
  1616. if (err < 0)
  1617. return err;
  1618. if (err != size)
  1619. return -EIO;
  1620. return 0;
  1621. }
  1622. static int drbd_open(struct gendisk *disk, blk_mode_t mode)
  1623. {
  1624. struct drbd_device *device = disk->private_data;
  1625. unsigned long flags;
  1626. int rv = 0;
  1627. mutex_lock(&drbd_main_mutex);
  1628. spin_lock_irqsave(&device->resource->req_lock, flags);
  1629. /* to have a stable device->state.role
  1630. * and no race with updating open_cnt */
  1631. if (device->state.role != R_PRIMARY) {
  1632. if (mode & BLK_OPEN_WRITE)
  1633. rv = -EROFS;
  1634. else if (!drbd_allow_oos)
  1635. rv = -EMEDIUMTYPE;
  1636. }
  1637. if (!rv)
  1638. device->open_cnt++;
  1639. spin_unlock_irqrestore(&device->resource->req_lock, flags);
  1640. mutex_unlock(&drbd_main_mutex);
  1641. return rv;
  1642. }
  1643. static void drbd_release(struct gendisk *gd)
  1644. {
  1645. struct drbd_device *device = gd->private_data;
  1646. mutex_lock(&drbd_main_mutex);
  1647. device->open_cnt--;
  1648. mutex_unlock(&drbd_main_mutex);
  1649. }
  1650. /* need to hold resource->req_lock */
  1651. void drbd_queue_unplug(struct drbd_device *device)
  1652. {
  1653. if (device->state.pdsk >= D_INCONSISTENT && device->state.conn >= C_CONNECTED) {
  1654. D_ASSERT(device, device->state.role == R_PRIMARY);
  1655. if (test_and_clear_bit(UNPLUG_REMOTE, &device->flags)) {
  1656. drbd_queue_work_if_unqueued(
  1657. &first_peer_device(device)->connection->sender_work,
  1658. &device->unplug_work);
  1659. }
  1660. }
  1661. }
  1662. static void drbd_set_defaults(struct drbd_device *device)
  1663. {
  1664. /* Beware! The actual layout differs
  1665. * between big endian and little endian */
  1666. device->state = (union drbd_dev_state) {
  1667. { .role = R_SECONDARY,
  1668. .peer = R_UNKNOWN,
  1669. .conn = C_STANDALONE,
  1670. .disk = D_DISKLESS,
  1671. .pdsk = D_UNKNOWN,
  1672. } };
  1673. }
  1674. void drbd_init_set_defaults(struct drbd_device *device)
  1675. {
  1676. /* the memset(,0,) did most of this.
  1677. * note: only assignments, no allocation in here */
  1678. drbd_set_defaults(device);
  1679. atomic_set(&device->ap_bio_cnt, 0);
  1680. atomic_set(&device->ap_actlog_cnt, 0);
  1681. atomic_set(&device->ap_pending_cnt, 0);
  1682. atomic_set(&device->rs_pending_cnt, 0);
  1683. atomic_set(&device->unacked_cnt, 0);
  1684. atomic_set(&device->local_cnt, 0);
  1685. atomic_set(&device->pp_in_use_by_net, 0);
  1686. atomic_set(&device->rs_sect_in, 0);
  1687. atomic_set(&device->rs_sect_ev, 0);
  1688. atomic_set(&device->ap_in_flight, 0);
  1689. atomic_set(&device->md_io.in_use, 0);
  1690. mutex_init(&device->own_state_mutex);
  1691. device->state_mutex = &device->own_state_mutex;
  1692. spin_lock_init(&device->al_lock);
  1693. spin_lock_init(&device->peer_seq_lock);
  1694. INIT_LIST_HEAD(&device->active_ee);
  1695. INIT_LIST_HEAD(&device->sync_ee);
  1696. INIT_LIST_HEAD(&device->done_ee);
  1697. INIT_LIST_HEAD(&device->read_ee);
  1698. INIT_LIST_HEAD(&device->resync_reads);
  1699. INIT_LIST_HEAD(&device->resync_work.list);
  1700. INIT_LIST_HEAD(&device->unplug_work.list);
  1701. INIT_LIST_HEAD(&device->bm_io_work.w.list);
  1702. INIT_LIST_HEAD(&device->pending_master_completion[0]);
  1703. INIT_LIST_HEAD(&device->pending_master_completion[1]);
  1704. INIT_LIST_HEAD(&device->pending_completion[0]);
  1705. INIT_LIST_HEAD(&device->pending_completion[1]);
  1706. device->resync_work.cb = w_resync_timer;
  1707. device->unplug_work.cb = w_send_write_hint;
  1708. device->bm_io_work.w.cb = w_bitmap_io;
  1709. timer_setup(&device->resync_timer, resync_timer_fn, 0);
  1710. timer_setup(&device->md_sync_timer, md_sync_timer_fn, 0);
  1711. timer_setup(&device->start_resync_timer, start_resync_timer_fn, 0);
  1712. timer_setup(&device->request_timer, request_timer_fn, 0);
  1713. init_waitqueue_head(&device->misc_wait);
  1714. init_waitqueue_head(&device->state_wait);
  1715. init_waitqueue_head(&device->ee_wait);
  1716. init_waitqueue_head(&device->al_wait);
  1717. init_waitqueue_head(&device->seq_wait);
  1718. device->resync_wenr = LC_FREE;
  1719. device->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
  1720. device->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
  1721. }
  1722. void drbd_set_my_capacity(struct drbd_device *device, sector_t size)
  1723. {
  1724. char ppb[10];
  1725. set_capacity_and_notify(device->vdisk, size);
  1726. drbd_info(device, "size = %s (%llu KB)\n",
  1727. ppsize(ppb, size>>1), (unsigned long long)size>>1);
  1728. }
  1729. void drbd_device_cleanup(struct drbd_device *device)
  1730. {
  1731. int i;
  1732. if (first_peer_device(device)->connection->receiver.t_state != NONE)
  1733. drbd_err(device, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
  1734. first_peer_device(device)->connection->receiver.t_state);
  1735. device->al_writ_cnt =
  1736. device->bm_writ_cnt =
  1737. device->read_cnt =
  1738. device->recv_cnt =
  1739. device->send_cnt =
  1740. device->writ_cnt =
  1741. device->p_size =
  1742. device->rs_start =
  1743. device->rs_total =
  1744. device->rs_failed = 0;
  1745. device->rs_last_events = 0;
  1746. device->rs_last_sect_ev = 0;
  1747. for (i = 0; i < DRBD_SYNC_MARKS; i++) {
  1748. device->rs_mark_left[i] = 0;
  1749. device->rs_mark_time[i] = 0;
  1750. }
  1751. D_ASSERT(device, first_peer_device(device)->connection->net_conf == NULL);
  1752. set_capacity_and_notify(device->vdisk, 0);
  1753. if (device->bitmap) {
  1754. /* maybe never allocated. */
  1755. drbd_bm_resize(device, 0, 1);
  1756. drbd_bm_cleanup(device);
  1757. }
  1758. drbd_backing_dev_free(device, device->ldev);
  1759. device->ldev = NULL;
  1760. clear_bit(AL_SUSPENDED, &device->flags);
  1761. D_ASSERT(device, list_empty(&device->active_ee));
  1762. D_ASSERT(device, list_empty(&device->sync_ee));
  1763. D_ASSERT(device, list_empty(&device->done_ee));
  1764. D_ASSERT(device, list_empty(&device->read_ee));
  1765. D_ASSERT(device, list_empty(&device->resync_reads));
  1766. D_ASSERT(device, list_empty(&first_peer_device(device)->connection->sender_work.q));
  1767. D_ASSERT(device, list_empty(&device->resync_work.list));
  1768. D_ASSERT(device, list_empty(&device->unplug_work.list));
  1769. drbd_set_defaults(device);
  1770. }
  1771. static void drbd_destroy_mempools(void)
  1772. {
  1773. /* D_ASSERT(device, atomic_read(&drbd_pp_vacant)==0); */
  1774. bioset_exit(&drbd_io_bio_set);
  1775. bioset_exit(&drbd_md_io_bio_set);
  1776. mempool_exit(&drbd_buffer_page_pool);
  1777. mempool_exit(&drbd_md_io_page_pool);
  1778. mempool_exit(&drbd_ee_mempool);
  1779. mempool_exit(&drbd_request_mempool);
  1780. kmem_cache_destroy(drbd_ee_cache);
  1781. kmem_cache_destroy(drbd_request_cache);
  1782. kmem_cache_destroy(drbd_bm_ext_cache);
  1783. kmem_cache_destroy(drbd_al_ext_cache);
  1784. drbd_ee_cache = NULL;
  1785. drbd_request_cache = NULL;
  1786. drbd_bm_ext_cache = NULL;
  1787. drbd_al_ext_cache = NULL;
  1788. return;
  1789. }
  1790. static int drbd_create_mempools(void)
  1791. {
  1792. const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * drbd_minor_count;
  1793. int ret;
  1794. /* caches */
  1795. drbd_request_cache = kmem_cache_create(
  1796. "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
  1797. if (drbd_request_cache == NULL)
  1798. goto Enomem;
  1799. drbd_ee_cache = kmem_cache_create(
  1800. "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
  1801. if (drbd_ee_cache == NULL)
  1802. goto Enomem;
  1803. drbd_bm_ext_cache = kmem_cache_create(
  1804. "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
  1805. if (drbd_bm_ext_cache == NULL)
  1806. goto Enomem;
  1807. drbd_al_ext_cache = kmem_cache_create(
  1808. "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
  1809. if (drbd_al_ext_cache == NULL)
  1810. goto Enomem;
  1811. /* mempools */
  1812. ret = bioset_init(&drbd_io_bio_set, BIO_POOL_SIZE, 0, 0);
  1813. if (ret)
  1814. goto Enomem;
  1815. ret = bioset_init(&drbd_md_io_bio_set, DRBD_MIN_POOL_PAGES, 0,
  1816. BIOSET_NEED_BVECS);
  1817. if (ret)
  1818. goto Enomem;
  1819. ret = mempool_init_page_pool(&drbd_md_io_page_pool, DRBD_MIN_POOL_PAGES, 0);
  1820. if (ret)
  1821. goto Enomem;
  1822. ret = mempool_init_page_pool(&drbd_buffer_page_pool, number, 0);
  1823. if (ret)
  1824. goto Enomem;
  1825. ret = mempool_init_slab_pool(&drbd_request_mempool, number,
  1826. drbd_request_cache);
  1827. if (ret)
  1828. goto Enomem;
  1829. ret = mempool_init_slab_pool(&drbd_ee_mempool, number, drbd_ee_cache);
  1830. if (ret)
  1831. goto Enomem;
  1832. return 0;
  1833. Enomem:
  1834. drbd_destroy_mempools(); /* in case we allocated some */
  1835. return -ENOMEM;
  1836. }
  1837. static void drbd_release_all_peer_reqs(struct drbd_device *device)
  1838. {
  1839. int rr;
  1840. rr = drbd_free_peer_reqs(device, &device->active_ee);
  1841. if (rr)
  1842. drbd_err(device, "%d EEs in active list found!\n", rr);
  1843. rr = drbd_free_peer_reqs(device, &device->sync_ee);
  1844. if (rr)
  1845. drbd_err(device, "%d EEs in sync list found!\n", rr);
  1846. rr = drbd_free_peer_reqs(device, &device->read_ee);
  1847. if (rr)
  1848. drbd_err(device, "%d EEs in read list found!\n", rr);
  1849. rr = drbd_free_peer_reqs(device, &device->done_ee);
  1850. if (rr)
  1851. drbd_err(device, "%d EEs in done list found!\n", rr);
  1852. }
  1853. /* caution. no locking. */
  1854. void drbd_destroy_device(struct kref *kref)
  1855. {
  1856. struct drbd_device *device = container_of(kref, struct drbd_device, kref);
  1857. struct drbd_resource *resource = device->resource;
  1858. struct drbd_peer_device *peer_device, *tmp_peer_device;
  1859. timer_shutdown_sync(&device->request_timer);
  1860. /* paranoia asserts */
  1861. D_ASSERT(device, device->open_cnt == 0);
  1862. /* end paranoia asserts */
  1863. /* cleanup stuff that may have been allocated during
  1864. * device (re-)configuration or state changes */
  1865. drbd_backing_dev_free(device, device->ldev);
  1866. device->ldev = NULL;
  1867. drbd_release_all_peer_reqs(device);
  1868. lc_destroy(device->act_log);
  1869. lc_destroy(device->resync);
  1870. kfree(device->p_uuid);
  1871. /* device->p_uuid = NULL; */
  1872. if (device->bitmap) /* should no longer be there. */
  1873. drbd_bm_cleanup(device);
  1874. __free_page(device->md_io.page);
  1875. put_disk(device->vdisk);
  1876. kfree(device->rs_plan_s);
  1877. /* not for_each_connection(connection, resource):
  1878. * those may have been cleaned up and disassociated already.
  1879. */
  1880. for_each_peer_device_safe(peer_device, tmp_peer_device, device) {
  1881. kref_put(&peer_device->connection->kref, drbd_destroy_connection);
  1882. kfree(peer_device);
  1883. }
  1884. if (device->submit.wq)
  1885. destroy_workqueue(device->submit.wq);
  1886. kfree(device);
  1887. kref_put(&resource->kref, drbd_destroy_resource);
  1888. }
  1889. /* One global retry thread, if we need to push back some bio and have it
  1890. * reinserted through our make request function.
  1891. */
  1892. static struct retry_worker {
  1893. struct workqueue_struct *wq;
  1894. struct work_struct worker;
  1895. spinlock_t lock;
  1896. struct list_head writes;
  1897. } retry;
  1898. static void do_retry(struct work_struct *ws)
  1899. {
  1900. struct retry_worker *retry = container_of(ws, struct retry_worker, worker);
  1901. LIST_HEAD(writes);
  1902. struct drbd_request *req, *tmp;
  1903. spin_lock_irq(&retry->lock);
  1904. list_splice_init(&retry->writes, &writes);
  1905. spin_unlock_irq(&retry->lock);
  1906. list_for_each_entry_safe(req, tmp, &writes, tl_requests) {
  1907. struct drbd_device *device = req->device;
  1908. struct bio *bio = req->master_bio;
  1909. bool expected;
  1910. expected =
  1911. expect(device, atomic_read(&req->completion_ref) == 0) &&
  1912. expect(device, req->rq_state & RQ_POSTPONED) &&
  1913. expect(device, (req->rq_state & RQ_LOCAL_PENDING) == 0 ||
  1914. (req->rq_state & RQ_LOCAL_ABORTED) != 0);
  1915. if (!expected)
  1916. drbd_err(device, "req=%p completion_ref=%d rq_state=%x\n",
  1917. req, atomic_read(&req->completion_ref),
  1918. req->rq_state);
  1919. /* We still need to put one kref associated with the
  1920. * "completion_ref" going zero in the code path that queued it
  1921. * here. The request object may still be referenced by a
  1922. * frozen local req->private_bio, in case we force-detached.
  1923. */
  1924. kref_put(&req->kref, drbd_req_destroy);
  1925. /* A single suspended or otherwise blocking device may stall
  1926. * all others as well. Fortunately, this code path is to
  1927. * recover from a situation that "should not happen":
  1928. * concurrent writes in multi-primary setup.
  1929. * In a "normal" lifecycle, this workqueue is supposed to be
  1930. * destroyed without ever doing anything.
  1931. * If it turns out to be an issue anyways, we can do per
  1932. * resource (replication group) or per device (minor) retry
  1933. * workqueues instead.
  1934. */
  1935. /* We are not just doing submit_bio_noacct(),
  1936. * as we want to keep the start_time information. */
  1937. inc_ap_bio(device);
  1938. __drbd_make_request(device, bio);
  1939. }
  1940. }
  1941. /* called via drbd_req_put_completion_ref(),
  1942. * holds resource->req_lock */
  1943. void drbd_restart_request(struct drbd_request *req)
  1944. {
  1945. unsigned long flags;
  1946. spin_lock_irqsave(&retry.lock, flags);
  1947. list_move_tail(&req->tl_requests, &retry.writes);
  1948. spin_unlock_irqrestore(&retry.lock, flags);
  1949. /* Drop the extra reference that would otherwise
  1950. * have been dropped by complete_master_bio.
  1951. * do_retry() needs to grab a new one. */
  1952. dec_ap_bio(req->device);
  1953. queue_work(retry.wq, &retry.worker);
  1954. }
  1955. void drbd_destroy_resource(struct kref *kref)
  1956. {
  1957. struct drbd_resource *resource =
  1958. container_of(kref, struct drbd_resource, kref);
  1959. idr_destroy(&resource->devices);
  1960. free_cpumask_var(resource->cpu_mask);
  1961. kfree(resource->name);
  1962. kfree(resource);
  1963. }
  1964. void drbd_free_resource(struct drbd_resource *resource)
  1965. {
  1966. struct drbd_connection *connection, *tmp;
  1967. for_each_connection_safe(connection, tmp, resource) {
  1968. list_del(&connection->connections);
  1969. drbd_debugfs_connection_cleanup(connection);
  1970. kref_put(&connection->kref, drbd_destroy_connection);
  1971. }
  1972. drbd_debugfs_resource_cleanup(resource);
  1973. kref_put(&resource->kref, drbd_destroy_resource);
  1974. }
  1975. static void drbd_cleanup(void)
  1976. {
  1977. unsigned int i;
  1978. struct drbd_device *device;
  1979. struct drbd_resource *resource, *tmp;
  1980. /* first remove proc,
  1981. * drbdsetup uses it's presence to detect
  1982. * whether DRBD is loaded.
  1983. * If we would get stuck in proc removal,
  1984. * but have netlink already deregistered,
  1985. * some drbdsetup commands may wait forever
  1986. * for an answer.
  1987. */
  1988. if (drbd_proc)
  1989. remove_proc_entry("drbd", NULL);
  1990. if (retry.wq)
  1991. destroy_workqueue(retry.wq);
  1992. drbd_genl_unregister();
  1993. idr_for_each_entry(&drbd_devices, device, i)
  1994. drbd_delete_device(device);
  1995. /* not _rcu since, no other updater anymore. Genl already unregistered */
  1996. for_each_resource_safe(resource, tmp, &drbd_resources) {
  1997. list_del(&resource->resources);
  1998. drbd_free_resource(resource);
  1999. }
  2000. drbd_debugfs_cleanup();
  2001. drbd_destroy_mempools();
  2002. unregister_blkdev(DRBD_MAJOR, "drbd");
  2003. idr_destroy(&drbd_devices);
  2004. pr_info("module cleanup done.\n");
  2005. }
  2006. static void drbd_init_workqueue(struct drbd_work_queue* wq)
  2007. {
  2008. spin_lock_init(&wq->q_lock);
  2009. INIT_LIST_HEAD(&wq->q);
  2010. init_waitqueue_head(&wq->q_wait);
  2011. }
  2012. struct completion_work {
  2013. struct drbd_work w;
  2014. struct completion done;
  2015. };
  2016. static int w_complete(struct drbd_work *w, int cancel)
  2017. {
  2018. struct completion_work *completion_work =
  2019. container_of(w, struct completion_work, w);
  2020. complete(&completion_work->done);
  2021. return 0;
  2022. }
  2023. void drbd_flush_workqueue(struct drbd_work_queue *work_queue)
  2024. {
  2025. struct completion_work completion_work;
  2026. completion_work.w.cb = w_complete;
  2027. init_completion(&completion_work.done);
  2028. drbd_queue_work(work_queue, &completion_work.w);
  2029. wait_for_completion(&completion_work.done);
  2030. }
  2031. struct drbd_resource *drbd_find_resource(const char *name)
  2032. {
  2033. struct drbd_resource *resource;
  2034. if (!name || !name[0])
  2035. return NULL;
  2036. rcu_read_lock();
  2037. for_each_resource_rcu(resource, &drbd_resources) {
  2038. if (!strcmp(resource->name, name)) {
  2039. kref_get(&resource->kref);
  2040. goto found;
  2041. }
  2042. }
  2043. resource = NULL;
  2044. found:
  2045. rcu_read_unlock();
  2046. return resource;
  2047. }
  2048. struct drbd_connection *conn_get_by_addrs(void *my_addr, int my_addr_len,
  2049. void *peer_addr, int peer_addr_len)
  2050. {
  2051. struct drbd_resource *resource;
  2052. struct drbd_connection *connection;
  2053. rcu_read_lock();
  2054. for_each_resource_rcu(resource, &drbd_resources) {
  2055. for_each_connection_rcu(connection, resource) {
  2056. if (connection->my_addr_len == my_addr_len &&
  2057. connection->peer_addr_len == peer_addr_len &&
  2058. !memcmp(&connection->my_addr, my_addr, my_addr_len) &&
  2059. !memcmp(&connection->peer_addr, peer_addr, peer_addr_len)) {
  2060. kref_get(&connection->kref);
  2061. goto found;
  2062. }
  2063. }
  2064. }
  2065. connection = NULL;
  2066. found:
  2067. rcu_read_unlock();
  2068. return connection;
  2069. }
  2070. static int drbd_alloc_socket(struct drbd_socket *socket)
  2071. {
  2072. socket->rbuf = (void *) __get_free_page(GFP_KERNEL);
  2073. if (!socket->rbuf)
  2074. return -ENOMEM;
  2075. socket->sbuf = (void *) __get_free_page(GFP_KERNEL);
  2076. if (!socket->sbuf)
  2077. return -ENOMEM;
  2078. return 0;
  2079. }
  2080. static void drbd_free_socket(struct drbd_socket *socket)
  2081. {
  2082. free_page((unsigned long) socket->sbuf);
  2083. free_page((unsigned long) socket->rbuf);
  2084. }
  2085. void conn_free_crypto(struct drbd_connection *connection)
  2086. {
  2087. drbd_free_sock(connection);
  2088. crypto_free_shash(connection->csums_tfm);
  2089. crypto_free_shash(connection->verify_tfm);
  2090. crypto_free_shash(connection->cram_hmac_tfm);
  2091. crypto_free_shash(connection->integrity_tfm);
  2092. crypto_free_shash(connection->peer_integrity_tfm);
  2093. kfree(connection->int_dig_in);
  2094. kfree(connection->int_dig_vv);
  2095. connection->csums_tfm = NULL;
  2096. connection->verify_tfm = NULL;
  2097. connection->cram_hmac_tfm = NULL;
  2098. connection->integrity_tfm = NULL;
  2099. connection->peer_integrity_tfm = NULL;
  2100. connection->int_dig_in = NULL;
  2101. connection->int_dig_vv = NULL;
  2102. }
  2103. int set_resource_options(struct drbd_resource *resource, struct res_opts *res_opts)
  2104. {
  2105. struct drbd_connection *connection;
  2106. cpumask_var_t new_cpu_mask;
  2107. int err;
  2108. if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL))
  2109. return -ENOMEM;
  2110. /* silently ignore cpu mask on UP kernel */
  2111. if (nr_cpu_ids > 1 && res_opts->cpu_mask[0] != 0) {
  2112. err = bitmap_parse(res_opts->cpu_mask, DRBD_CPU_MASK_SIZE,
  2113. cpumask_bits(new_cpu_mask), nr_cpu_ids);
  2114. if (err == -EOVERFLOW) {
  2115. /* So what. mask it out. */
  2116. cpumask_var_t tmp_cpu_mask;
  2117. if (zalloc_cpumask_var(&tmp_cpu_mask, GFP_KERNEL)) {
  2118. cpumask_setall(tmp_cpu_mask);
  2119. cpumask_and(new_cpu_mask, new_cpu_mask, tmp_cpu_mask);
  2120. drbd_warn(resource, "Overflow in bitmap_parse(%.12s%s), truncating to %u bits\n",
  2121. res_opts->cpu_mask,
  2122. strlen(res_opts->cpu_mask) > 12 ? "..." : "",
  2123. nr_cpu_ids);
  2124. free_cpumask_var(tmp_cpu_mask);
  2125. err = 0;
  2126. }
  2127. }
  2128. if (err) {
  2129. drbd_warn(resource, "bitmap_parse() failed with %d\n", err);
  2130. /* retcode = ERR_CPU_MASK_PARSE; */
  2131. goto fail;
  2132. }
  2133. }
  2134. resource->res_opts = *res_opts;
  2135. if (cpumask_empty(new_cpu_mask))
  2136. drbd_calc_cpu_mask(&new_cpu_mask);
  2137. if (!cpumask_equal(resource->cpu_mask, new_cpu_mask)) {
  2138. cpumask_copy(resource->cpu_mask, new_cpu_mask);
  2139. for_each_connection_rcu(connection, resource) {
  2140. connection->receiver.reset_cpu_mask = 1;
  2141. connection->ack_receiver.reset_cpu_mask = 1;
  2142. connection->worker.reset_cpu_mask = 1;
  2143. }
  2144. }
  2145. err = 0;
  2146. fail:
  2147. free_cpumask_var(new_cpu_mask);
  2148. return err;
  2149. }
  2150. struct drbd_resource *drbd_create_resource(const char *name)
  2151. {
  2152. struct drbd_resource *resource;
  2153. resource = kzalloc_obj(struct drbd_resource);
  2154. if (!resource)
  2155. goto fail;
  2156. resource->name = kstrdup(name, GFP_KERNEL);
  2157. if (!resource->name)
  2158. goto fail_free_resource;
  2159. if (!zalloc_cpumask_var(&resource->cpu_mask, GFP_KERNEL))
  2160. goto fail_free_name;
  2161. kref_init(&resource->kref);
  2162. idr_init(&resource->devices);
  2163. INIT_LIST_HEAD(&resource->connections);
  2164. resource->write_ordering = WO_BDEV_FLUSH;
  2165. list_add_tail_rcu(&resource->resources, &drbd_resources);
  2166. mutex_init(&resource->conf_update);
  2167. mutex_init(&resource->adm_mutex);
  2168. spin_lock_init(&resource->req_lock);
  2169. drbd_debugfs_resource_add(resource);
  2170. return resource;
  2171. fail_free_name:
  2172. kfree(resource->name);
  2173. fail_free_resource:
  2174. kfree(resource);
  2175. fail:
  2176. return NULL;
  2177. }
  2178. /* caller must be under adm_mutex */
  2179. struct drbd_connection *conn_create(const char *name, struct res_opts *res_opts)
  2180. {
  2181. struct drbd_resource *resource;
  2182. struct drbd_connection *connection;
  2183. connection = kzalloc_obj(struct drbd_connection);
  2184. if (!connection)
  2185. return NULL;
  2186. if (drbd_alloc_socket(&connection->data))
  2187. goto fail;
  2188. if (drbd_alloc_socket(&connection->meta))
  2189. goto fail;
  2190. connection->current_epoch = kzalloc_obj(struct drbd_epoch);
  2191. if (!connection->current_epoch)
  2192. goto fail;
  2193. INIT_LIST_HEAD(&connection->transfer_log);
  2194. INIT_LIST_HEAD(&connection->current_epoch->list);
  2195. connection->epochs = 1;
  2196. spin_lock_init(&connection->epoch_lock);
  2197. connection->send.seen_any_write_yet = false;
  2198. connection->send.current_epoch_nr = 0;
  2199. connection->send.current_epoch_writes = 0;
  2200. resource = drbd_create_resource(name);
  2201. if (!resource)
  2202. goto fail;
  2203. connection->cstate = C_STANDALONE;
  2204. mutex_init(&connection->cstate_mutex);
  2205. init_waitqueue_head(&connection->ping_wait);
  2206. idr_init(&connection->peer_devices);
  2207. drbd_init_workqueue(&connection->sender_work);
  2208. mutex_init(&connection->data.mutex);
  2209. mutex_init(&connection->meta.mutex);
  2210. drbd_thread_init(resource, &connection->receiver, drbd_receiver, "receiver");
  2211. connection->receiver.connection = connection;
  2212. drbd_thread_init(resource, &connection->worker, drbd_worker, "worker");
  2213. connection->worker.connection = connection;
  2214. drbd_thread_init(resource, &connection->ack_receiver, drbd_ack_receiver, "ack_recv");
  2215. connection->ack_receiver.connection = connection;
  2216. kref_init(&connection->kref);
  2217. connection->resource = resource;
  2218. if (set_resource_options(resource, res_opts))
  2219. goto fail_resource;
  2220. kref_get(&resource->kref);
  2221. list_add_tail_rcu(&connection->connections, &resource->connections);
  2222. drbd_debugfs_connection_add(connection);
  2223. return connection;
  2224. fail_resource:
  2225. list_del(&resource->resources);
  2226. drbd_free_resource(resource);
  2227. fail:
  2228. kfree(connection->current_epoch);
  2229. drbd_free_socket(&connection->meta);
  2230. drbd_free_socket(&connection->data);
  2231. kfree(connection);
  2232. return NULL;
  2233. }
  2234. void drbd_destroy_connection(struct kref *kref)
  2235. {
  2236. struct drbd_connection *connection = container_of(kref, struct drbd_connection, kref);
  2237. struct drbd_resource *resource = connection->resource;
  2238. if (atomic_read(&connection->current_epoch->epoch_size) != 0)
  2239. drbd_err(connection, "epoch_size:%d\n", atomic_read(&connection->current_epoch->epoch_size));
  2240. kfree(connection->current_epoch);
  2241. idr_destroy(&connection->peer_devices);
  2242. drbd_free_socket(&connection->meta);
  2243. drbd_free_socket(&connection->data);
  2244. kfree(connection->int_dig_in);
  2245. kfree(connection->int_dig_vv);
  2246. kfree(connection);
  2247. kref_put(&resource->kref, drbd_destroy_resource);
  2248. }
  2249. static int init_submitter(struct drbd_device *device)
  2250. {
  2251. /* opencoded create_singlethread_workqueue(),
  2252. * to be able to say "drbd%d", ..., minor */
  2253. device->submit.wq =
  2254. alloc_ordered_workqueue("drbd%u_submit", WQ_MEM_RECLAIM, device->minor);
  2255. if (!device->submit.wq)
  2256. return -ENOMEM;
  2257. INIT_WORK(&device->submit.worker, do_submit);
  2258. INIT_LIST_HEAD(&device->submit.writes);
  2259. return 0;
  2260. }
  2261. enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsigned int minor)
  2262. {
  2263. struct drbd_resource *resource = adm_ctx->resource;
  2264. struct drbd_connection *connection, *n;
  2265. struct drbd_device *device;
  2266. struct drbd_peer_device *peer_device, *tmp_peer_device;
  2267. struct gendisk *disk;
  2268. int id;
  2269. int vnr = adm_ctx->volume;
  2270. enum drbd_ret_code err = ERR_NOMEM;
  2271. struct queue_limits lim = {
  2272. /*
  2273. * Setting the max_hw_sectors to an odd value of 8kibyte here.
  2274. * This triggers a max_bio_size message upon first attach or
  2275. * connect.
  2276. */
  2277. .max_hw_sectors = DRBD_MAX_BIO_SIZE_SAFE >> 8,
  2278. };
  2279. device = minor_to_device(minor);
  2280. if (device)
  2281. return ERR_MINOR_OR_VOLUME_EXISTS;
  2282. /* GFP_KERNEL, we are outside of all write-out paths */
  2283. device = kzalloc_obj(struct drbd_device);
  2284. if (!device)
  2285. return ERR_NOMEM;
  2286. kref_init(&device->kref);
  2287. kref_get(&resource->kref);
  2288. device->resource = resource;
  2289. device->minor = minor;
  2290. device->vnr = vnr;
  2291. drbd_init_set_defaults(device);
  2292. disk = blk_alloc_disk(&lim, NUMA_NO_NODE);
  2293. if (IS_ERR(disk)) {
  2294. err = PTR_ERR(disk);
  2295. goto out_no_disk;
  2296. }
  2297. device->vdisk = disk;
  2298. device->rq_queue = disk->queue;
  2299. set_disk_ro(disk, true);
  2300. disk->major = DRBD_MAJOR;
  2301. disk->first_minor = minor;
  2302. disk->minors = 1;
  2303. disk->fops = &drbd_ops;
  2304. disk->flags |= GENHD_FL_NO_PART;
  2305. sprintf(disk->disk_name, "drbd%d", minor);
  2306. disk->private_data = device;
  2307. device->md_io.page = alloc_page(GFP_KERNEL);
  2308. if (!device->md_io.page)
  2309. goto out_no_io_page;
  2310. if (drbd_bm_init(device))
  2311. goto out_no_bitmap;
  2312. device->read_requests = RB_ROOT;
  2313. device->write_requests = RB_ROOT;
  2314. id = idr_alloc(&drbd_devices, device, minor, minor + 1, GFP_KERNEL);
  2315. if (id < 0) {
  2316. if (id == -ENOSPC)
  2317. err = ERR_MINOR_OR_VOLUME_EXISTS;
  2318. goto out_no_minor_idr;
  2319. }
  2320. kref_get(&device->kref);
  2321. id = idr_alloc(&resource->devices, device, vnr, vnr + 1, GFP_KERNEL);
  2322. if (id < 0) {
  2323. if (id == -ENOSPC)
  2324. err = ERR_MINOR_OR_VOLUME_EXISTS;
  2325. goto out_idr_remove_minor;
  2326. }
  2327. kref_get(&device->kref);
  2328. INIT_LIST_HEAD(&device->peer_devices);
  2329. INIT_LIST_HEAD(&device->pending_bitmap_io);
  2330. for_each_connection(connection, resource) {
  2331. peer_device = kzalloc_obj(struct drbd_peer_device);
  2332. if (!peer_device)
  2333. goto out_idr_remove_from_resource;
  2334. peer_device->connection = connection;
  2335. peer_device->device = device;
  2336. list_add(&peer_device->peer_devices, &device->peer_devices);
  2337. kref_get(&device->kref);
  2338. id = idr_alloc(&connection->peer_devices, peer_device, vnr, vnr + 1, GFP_KERNEL);
  2339. if (id < 0) {
  2340. if (id == -ENOSPC)
  2341. err = ERR_INVALID_REQUEST;
  2342. goto out_idr_remove_from_resource;
  2343. }
  2344. kref_get(&connection->kref);
  2345. INIT_WORK(&peer_device->send_acks_work, drbd_send_acks_wf);
  2346. }
  2347. if (init_submitter(device)) {
  2348. err = ERR_NOMEM;
  2349. goto out_idr_remove_from_resource;
  2350. }
  2351. err = add_disk(disk);
  2352. if (err)
  2353. goto out_destroy_workqueue;
  2354. /* inherit the connection state */
  2355. device->state.conn = first_connection(resource)->cstate;
  2356. if (device->state.conn == C_WF_REPORT_PARAMS) {
  2357. for_each_peer_device(peer_device, device)
  2358. drbd_connected(peer_device);
  2359. }
  2360. /* move to create_peer_device() */
  2361. for_each_peer_device(peer_device, device)
  2362. drbd_debugfs_peer_device_add(peer_device);
  2363. drbd_debugfs_device_add(device);
  2364. return NO_ERROR;
  2365. out_destroy_workqueue:
  2366. destroy_workqueue(device->submit.wq);
  2367. out_idr_remove_from_resource:
  2368. for_each_connection_safe(connection, n, resource) {
  2369. peer_device = idr_remove(&connection->peer_devices, vnr);
  2370. if (peer_device)
  2371. kref_put(&connection->kref, drbd_destroy_connection);
  2372. }
  2373. for_each_peer_device_safe(peer_device, tmp_peer_device, device) {
  2374. list_del(&peer_device->peer_devices);
  2375. kfree(peer_device);
  2376. }
  2377. idr_remove(&resource->devices, vnr);
  2378. out_idr_remove_minor:
  2379. idr_remove(&drbd_devices, minor);
  2380. synchronize_rcu();
  2381. out_no_minor_idr:
  2382. drbd_bm_cleanup(device);
  2383. out_no_bitmap:
  2384. __free_page(device->md_io.page);
  2385. out_no_io_page:
  2386. put_disk(disk);
  2387. out_no_disk:
  2388. kref_put(&resource->kref, drbd_destroy_resource);
  2389. kfree(device);
  2390. return err;
  2391. }
  2392. void drbd_delete_device(struct drbd_device *device)
  2393. {
  2394. struct drbd_resource *resource = device->resource;
  2395. struct drbd_connection *connection;
  2396. struct drbd_peer_device *peer_device;
  2397. /* move to free_peer_device() */
  2398. for_each_peer_device(peer_device, device)
  2399. drbd_debugfs_peer_device_cleanup(peer_device);
  2400. drbd_debugfs_device_cleanup(device);
  2401. for_each_connection(connection, resource) {
  2402. idr_remove(&connection->peer_devices, device->vnr);
  2403. kref_put(&device->kref, drbd_destroy_device);
  2404. }
  2405. idr_remove(&resource->devices, device->vnr);
  2406. kref_put(&device->kref, drbd_destroy_device);
  2407. idr_remove(&drbd_devices, device_to_minor(device));
  2408. kref_put(&device->kref, drbd_destroy_device);
  2409. del_gendisk(device->vdisk);
  2410. synchronize_rcu();
  2411. kref_put(&device->kref, drbd_destroy_device);
  2412. }
  2413. static int __init drbd_init(void)
  2414. {
  2415. int err;
  2416. if (drbd_minor_count < DRBD_MINOR_COUNT_MIN || drbd_minor_count > DRBD_MINOR_COUNT_MAX) {
  2417. pr_err("invalid minor_count (%d)\n", drbd_minor_count);
  2418. #ifdef MODULE
  2419. return -EINVAL;
  2420. #else
  2421. drbd_minor_count = DRBD_MINOR_COUNT_DEF;
  2422. #endif
  2423. }
  2424. err = register_blkdev(DRBD_MAJOR, "drbd");
  2425. if (err) {
  2426. pr_err("unable to register block device major %d\n",
  2427. DRBD_MAJOR);
  2428. return err;
  2429. }
  2430. drbd_proc = NULL; /* play safe for drbd_cleanup */
  2431. idr_init(&drbd_devices);
  2432. mutex_init(&resources_mutex);
  2433. INIT_LIST_HEAD(&drbd_resources);
  2434. err = drbd_genl_register();
  2435. if (err) {
  2436. pr_err("unable to register generic netlink family\n");
  2437. goto fail;
  2438. }
  2439. err = drbd_create_mempools();
  2440. if (err)
  2441. goto fail;
  2442. err = -ENOMEM;
  2443. drbd_proc = proc_create_single("drbd", S_IFREG | 0444 , NULL, drbd_seq_show);
  2444. if (!drbd_proc) {
  2445. pr_err("unable to register proc file\n");
  2446. goto fail;
  2447. }
  2448. retry.wq = create_singlethread_workqueue("drbd-reissue");
  2449. if (!retry.wq) {
  2450. pr_err("unable to create retry workqueue\n");
  2451. goto fail;
  2452. }
  2453. INIT_WORK(&retry.worker, do_retry);
  2454. spin_lock_init(&retry.lock);
  2455. INIT_LIST_HEAD(&retry.writes);
  2456. drbd_debugfs_init();
  2457. pr_info("initialized. "
  2458. "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
  2459. GENL_MAGIC_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
  2460. pr_info("%s\n", drbd_buildtag());
  2461. pr_info("registered as block device major %d\n", DRBD_MAJOR);
  2462. return 0; /* Success! */
  2463. fail:
  2464. drbd_cleanup();
  2465. if (err == -ENOMEM)
  2466. pr_err("ran out of memory\n");
  2467. else
  2468. pr_err("initialization failure\n");
  2469. return err;
  2470. }
  2471. static void drbd_free_one_sock(struct drbd_socket *ds)
  2472. {
  2473. struct socket *s;
  2474. mutex_lock(&ds->mutex);
  2475. s = ds->socket;
  2476. ds->socket = NULL;
  2477. mutex_unlock(&ds->mutex);
  2478. if (s) {
  2479. /* so debugfs does not need to mutex_lock() */
  2480. synchronize_rcu();
  2481. kernel_sock_shutdown(s, SHUT_RDWR);
  2482. sock_release(s);
  2483. }
  2484. }
  2485. void drbd_free_sock(struct drbd_connection *connection)
  2486. {
  2487. if (connection->data.socket)
  2488. drbd_free_one_sock(&connection->data);
  2489. if (connection->meta.socket)
  2490. drbd_free_one_sock(&connection->meta);
  2491. }
  2492. /* meta data management */
  2493. void conn_md_sync(struct drbd_connection *connection)
  2494. {
  2495. struct drbd_peer_device *peer_device;
  2496. int vnr;
  2497. rcu_read_lock();
  2498. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  2499. struct drbd_device *device = peer_device->device;
  2500. kref_get(&device->kref);
  2501. rcu_read_unlock();
  2502. drbd_md_sync(device);
  2503. kref_put(&device->kref, drbd_destroy_device);
  2504. rcu_read_lock();
  2505. }
  2506. rcu_read_unlock();
  2507. }
  2508. /* aligned 4kByte */
  2509. struct meta_data_on_disk {
  2510. u64 la_size_sect; /* last agreed size. */
  2511. u64 uuid[UI_SIZE]; /* UUIDs. */
  2512. u64 device_uuid;
  2513. u64 reserved_u64_1;
  2514. u32 flags; /* MDF */
  2515. u32 magic;
  2516. u32 md_size_sect;
  2517. u32 al_offset; /* offset to this block */
  2518. u32 al_nr_extents; /* important for restoring the AL (userspace) */
  2519. /* `-- act_log->nr_elements <-- ldev->dc.al_extents */
  2520. u32 bm_offset; /* offset to the bitmap, from here */
  2521. u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
  2522. u32 la_peer_max_bio_size; /* last peer max_bio_size */
  2523. /* see al_tr_number_to_on_disk_sector() */
  2524. u32 al_stripes;
  2525. u32 al_stripe_size_4k;
  2526. u8 reserved_u8[4096 - (7*8 + 10*4)];
  2527. } __packed;
  2528. void drbd_md_write(struct drbd_device *device, void *b)
  2529. {
  2530. struct meta_data_on_disk *buffer = b;
  2531. sector_t sector;
  2532. int i;
  2533. memset(buffer, 0, sizeof(*buffer));
  2534. buffer->la_size_sect = cpu_to_be64(get_capacity(device->vdisk));
  2535. for (i = UI_CURRENT; i < UI_SIZE; i++)
  2536. buffer->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
  2537. buffer->flags = cpu_to_be32(device->ldev->md.flags);
  2538. buffer->magic = cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN);
  2539. buffer->md_size_sect = cpu_to_be32(device->ldev->md.md_size_sect);
  2540. buffer->al_offset = cpu_to_be32(device->ldev->md.al_offset);
  2541. buffer->al_nr_extents = cpu_to_be32(device->act_log->nr_elements);
  2542. buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
  2543. buffer->device_uuid = cpu_to_be64(device->ldev->md.device_uuid);
  2544. buffer->bm_offset = cpu_to_be32(device->ldev->md.bm_offset);
  2545. buffer->la_peer_max_bio_size = cpu_to_be32(device->peer_max_bio_size);
  2546. buffer->al_stripes = cpu_to_be32(device->ldev->md.al_stripes);
  2547. buffer->al_stripe_size_4k = cpu_to_be32(device->ldev->md.al_stripe_size_4k);
  2548. D_ASSERT(device, drbd_md_ss(device->ldev) == device->ldev->md.md_offset);
  2549. sector = device->ldev->md.md_offset;
  2550. if (drbd_md_sync_page_io(device, device->ldev, sector, REQ_OP_WRITE)) {
  2551. /* this was a try anyways ... */
  2552. drbd_err(device, "meta data update failed!\n");
  2553. drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
  2554. }
  2555. }
  2556. /**
  2557. * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
  2558. * @device: DRBD device.
  2559. */
  2560. void drbd_md_sync(struct drbd_device *device)
  2561. {
  2562. struct meta_data_on_disk *buffer;
  2563. /* Don't accidentally change the DRBD meta data layout. */
  2564. BUILD_BUG_ON(UI_SIZE != 4);
  2565. BUILD_BUG_ON(sizeof(struct meta_data_on_disk) != 4096);
  2566. timer_delete(&device->md_sync_timer);
  2567. /* timer may be rearmed by drbd_md_mark_dirty() now. */
  2568. if (!test_and_clear_bit(MD_DIRTY, &device->flags))
  2569. return;
  2570. /* We use here D_FAILED and not D_ATTACHING because we try to write
  2571. * metadata even if we detach due to a disk failure! */
  2572. if (!get_ldev_if_state(device, D_FAILED))
  2573. return;
  2574. buffer = drbd_md_get_buffer(device, __func__);
  2575. if (!buffer)
  2576. goto out;
  2577. drbd_md_write(device, buffer);
  2578. /* Update device->ldev->md.la_size_sect,
  2579. * since we updated it on metadata. */
  2580. device->ldev->md.la_size_sect = get_capacity(device->vdisk);
  2581. drbd_md_put_buffer(device);
  2582. out:
  2583. put_ldev(device);
  2584. }
  2585. static int check_activity_log_stripe_size(struct drbd_device *device,
  2586. struct meta_data_on_disk *on_disk,
  2587. struct drbd_md *in_core)
  2588. {
  2589. u32 al_stripes = be32_to_cpu(on_disk->al_stripes);
  2590. u32 al_stripe_size_4k = be32_to_cpu(on_disk->al_stripe_size_4k);
  2591. u64 al_size_4k;
  2592. /* both not set: default to old fixed size activity log */
  2593. if (al_stripes == 0 && al_stripe_size_4k == 0) {
  2594. al_stripes = 1;
  2595. al_stripe_size_4k = MD_32kB_SECT/8;
  2596. }
  2597. /* some paranoia plausibility checks */
  2598. /* we need both values to be set */
  2599. if (al_stripes == 0 || al_stripe_size_4k == 0)
  2600. goto err;
  2601. al_size_4k = (u64)al_stripes * al_stripe_size_4k;
  2602. /* Upper limit of activity log area, to avoid potential overflow
  2603. * problems in al_tr_number_to_on_disk_sector(). As right now, more
  2604. * than 72 * 4k blocks total only increases the amount of history,
  2605. * limiting this arbitrarily to 16 GB is not a real limitation ;-) */
  2606. if (al_size_4k > (16 * 1024 * 1024/4))
  2607. goto err;
  2608. /* Lower limit: we need at least 8 transaction slots (32kB)
  2609. * to not break existing setups */
  2610. if (al_size_4k < MD_32kB_SECT/8)
  2611. goto err;
  2612. in_core->al_stripe_size_4k = al_stripe_size_4k;
  2613. in_core->al_stripes = al_stripes;
  2614. in_core->al_size_4k = al_size_4k;
  2615. return 0;
  2616. err:
  2617. drbd_err(device, "invalid activity log striping: al_stripes=%u, al_stripe_size_4k=%u\n",
  2618. al_stripes, al_stripe_size_4k);
  2619. return -EINVAL;
  2620. }
  2621. static int check_offsets_and_sizes(struct drbd_device *device, struct drbd_backing_dev *bdev)
  2622. {
  2623. sector_t capacity = drbd_get_capacity(bdev->md_bdev);
  2624. struct drbd_md *in_core = &bdev->md;
  2625. s32 on_disk_al_sect;
  2626. s32 on_disk_bm_sect;
  2627. /* The on-disk size of the activity log, calculated from offsets, and
  2628. * the size of the activity log calculated from the stripe settings,
  2629. * should match.
  2630. * Though we could relax this a bit: it is ok, if the striped activity log
  2631. * fits in the available on-disk activity log size.
  2632. * Right now, that would break how resize is implemented.
  2633. * TODO: make drbd_determine_dev_size() (and the drbdmeta tool) aware
  2634. * of possible unused padding space in the on disk layout. */
  2635. if (in_core->al_offset < 0) {
  2636. if (in_core->bm_offset > in_core->al_offset)
  2637. goto err;
  2638. on_disk_al_sect = -in_core->al_offset;
  2639. on_disk_bm_sect = in_core->al_offset - in_core->bm_offset;
  2640. } else {
  2641. if (in_core->al_offset != MD_4kB_SECT)
  2642. goto err;
  2643. if (in_core->bm_offset < in_core->al_offset + in_core->al_size_4k * MD_4kB_SECT)
  2644. goto err;
  2645. on_disk_al_sect = in_core->bm_offset - MD_4kB_SECT;
  2646. on_disk_bm_sect = in_core->md_size_sect - in_core->bm_offset;
  2647. }
  2648. /* old fixed size meta data is exactly that: fixed. */
  2649. if (in_core->meta_dev_idx >= 0) {
  2650. if (in_core->md_size_sect != MD_128MB_SECT
  2651. || in_core->al_offset != MD_4kB_SECT
  2652. || in_core->bm_offset != MD_4kB_SECT + MD_32kB_SECT
  2653. || in_core->al_stripes != 1
  2654. || in_core->al_stripe_size_4k != MD_32kB_SECT/8)
  2655. goto err;
  2656. }
  2657. if (capacity < in_core->md_size_sect)
  2658. goto err;
  2659. if (capacity - in_core->md_size_sect < drbd_md_first_sector(bdev))
  2660. goto err;
  2661. /* should be aligned, and at least 32k */
  2662. if ((on_disk_al_sect & 7) || (on_disk_al_sect < MD_32kB_SECT))
  2663. goto err;
  2664. /* should fit (for now: exactly) into the available on-disk space;
  2665. * overflow prevention is in check_activity_log_stripe_size() above. */
  2666. if (on_disk_al_sect != in_core->al_size_4k * MD_4kB_SECT)
  2667. goto err;
  2668. /* again, should be aligned */
  2669. if (in_core->bm_offset & 7)
  2670. goto err;
  2671. /* FIXME check for device grow with flex external meta data? */
  2672. /* can the available bitmap space cover the last agreed device size? */
  2673. if (on_disk_bm_sect < (in_core->la_size_sect+7)/MD_4kB_SECT/8/512)
  2674. goto err;
  2675. return 0;
  2676. err:
  2677. drbd_err(device, "meta data offsets don't make sense: idx=%d "
  2678. "al_s=%u, al_sz4k=%u, al_offset=%d, bm_offset=%d, "
  2679. "md_size_sect=%u, la_size=%llu, md_capacity=%llu\n",
  2680. in_core->meta_dev_idx,
  2681. in_core->al_stripes, in_core->al_stripe_size_4k,
  2682. in_core->al_offset, in_core->bm_offset, in_core->md_size_sect,
  2683. (unsigned long long)in_core->la_size_sect,
  2684. (unsigned long long)capacity);
  2685. return -EINVAL;
  2686. }
  2687. /**
  2688. * drbd_md_read() - Reads in the meta data super block
  2689. * @device: DRBD device.
  2690. * @bdev: Device from which the meta data should be read in.
  2691. *
  2692. * Return NO_ERROR on success, and an enum drbd_ret_code in case
  2693. * something goes wrong.
  2694. *
  2695. * Called exactly once during drbd_adm_attach(), while still being D_DISKLESS,
  2696. * even before @bdev is assigned to @device->ldev.
  2697. */
  2698. int drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev)
  2699. {
  2700. struct meta_data_on_disk *buffer;
  2701. u32 magic, flags;
  2702. int i, rv = NO_ERROR;
  2703. if (device->state.disk != D_DISKLESS)
  2704. return ERR_DISK_CONFIGURED;
  2705. buffer = drbd_md_get_buffer(device, __func__);
  2706. if (!buffer)
  2707. return ERR_NOMEM;
  2708. /* First, figure out where our meta data superblock is located,
  2709. * and read it. */
  2710. bdev->md.meta_dev_idx = bdev->disk_conf->meta_dev_idx;
  2711. bdev->md.md_offset = drbd_md_ss(bdev);
  2712. /* Even for (flexible or indexed) external meta data,
  2713. * initially restrict us to the 4k superblock for now.
  2714. * Affects the paranoia out-of-range access check in drbd_md_sync_page_io(). */
  2715. bdev->md.md_size_sect = 8;
  2716. if (drbd_md_sync_page_io(device, bdev, bdev->md.md_offset,
  2717. REQ_OP_READ)) {
  2718. /* NOTE: can't do normal error processing here as this is
  2719. called BEFORE disk is attached */
  2720. drbd_err(device, "Error while reading metadata.\n");
  2721. rv = ERR_IO_MD_DISK;
  2722. goto err;
  2723. }
  2724. magic = be32_to_cpu(buffer->magic);
  2725. flags = be32_to_cpu(buffer->flags);
  2726. if (magic == DRBD_MD_MAGIC_84_UNCLEAN ||
  2727. (magic == DRBD_MD_MAGIC_08 && !(flags & MDF_AL_CLEAN))) {
  2728. /* btw: that's Activity Log clean, not "all" clean. */
  2729. drbd_err(device, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n");
  2730. rv = ERR_MD_UNCLEAN;
  2731. goto err;
  2732. }
  2733. rv = ERR_MD_INVALID;
  2734. if (magic != DRBD_MD_MAGIC_08) {
  2735. if (magic == DRBD_MD_MAGIC_07)
  2736. drbd_err(device, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n");
  2737. else
  2738. drbd_err(device, "Meta data magic not found. Did you \"drbdadm create-md\"?\n");
  2739. goto err;
  2740. }
  2741. if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
  2742. drbd_err(device, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
  2743. be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
  2744. goto err;
  2745. }
  2746. /* convert to in_core endian */
  2747. bdev->md.la_size_sect = be64_to_cpu(buffer->la_size_sect);
  2748. for (i = UI_CURRENT; i < UI_SIZE; i++)
  2749. bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
  2750. bdev->md.flags = be32_to_cpu(buffer->flags);
  2751. bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
  2752. bdev->md.md_size_sect = be32_to_cpu(buffer->md_size_sect);
  2753. bdev->md.al_offset = be32_to_cpu(buffer->al_offset);
  2754. bdev->md.bm_offset = be32_to_cpu(buffer->bm_offset);
  2755. if (check_activity_log_stripe_size(device, buffer, &bdev->md))
  2756. goto err;
  2757. if (check_offsets_and_sizes(device, bdev))
  2758. goto err;
  2759. if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
  2760. drbd_err(device, "unexpected bm_offset: %d (expected %d)\n",
  2761. be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
  2762. goto err;
  2763. }
  2764. if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
  2765. drbd_err(device, "unexpected md_size: %u (expected %u)\n",
  2766. be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
  2767. goto err;
  2768. }
  2769. rv = NO_ERROR;
  2770. spin_lock_irq(&device->resource->req_lock);
  2771. if (device->state.conn < C_CONNECTED) {
  2772. unsigned int peer;
  2773. peer = be32_to_cpu(buffer->la_peer_max_bio_size);
  2774. peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE);
  2775. device->peer_max_bio_size = peer;
  2776. }
  2777. spin_unlock_irq(&device->resource->req_lock);
  2778. err:
  2779. drbd_md_put_buffer(device);
  2780. return rv;
  2781. }
  2782. /**
  2783. * drbd_md_mark_dirty() - Mark meta data super block as dirty
  2784. * @device: DRBD device.
  2785. *
  2786. * Call this function if you change anything that should be written to
  2787. * the meta-data super block. This function sets MD_DIRTY, and starts a
  2788. * timer that ensures that within five seconds you have to call drbd_md_sync().
  2789. */
  2790. void drbd_md_mark_dirty(struct drbd_device *device)
  2791. {
  2792. if (!test_and_set_bit(MD_DIRTY, &device->flags))
  2793. mod_timer(&device->md_sync_timer, jiffies + 5*HZ);
  2794. }
  2795. void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local)
  2796. {
  2797. int i;
  2798. for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
  2799. device->ldev->md.uuid[i+1] = device->ldev->md.uuid[i];
  2800. }
  2801. void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
  2802. {
  2803. if (idx == UI_CURRENT) {
  2804. if (device->state.role == R_PRIMARY)
  2805. val |= 1;
  2806. else
  2807. val &= ~((u64)1);
  2808. drbd_set_ed_uuid(device, val);
  2809. }
  2810. device->ldev->md.uuid[idx] = val;
  2811. drbd_md_mark_dirty(device);
  2812. }
  2813. void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
  2814. {
  2815. unsigned long flags;
  2816. spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
  2817. __drbd_uuid_set(device, idx, val);
  2818. spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
  2819. }
  2820. void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
  2821. {
  2822. unsigned long flags;
  2823. spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
  2824. if (device->ldev->md.uuid[idx]) {
  2825. drbd_uuid_move_history(device);
  2826. device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[idx];
  2827. }
  2828. __drbd_uuid_set(device, idx, val);
  2829. spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
  2830. }
  2831. /**
  2832. * drbd_uuid_new_current() - Creates a new current UUID
  2833. * @device: DRBD device.
  2834. *
  2835. * Creates a new current UUID, and rotates the old current UUID into
  2836. * the bitmap slot. Causes an incremental resync upon next connect.
  2837. */
  2838. void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local)
  2839. {
  2840. u64 val;
  2841. unsigned long long bm_uuid;
  2842. get_random_bytes(&val, sizeof(u64));
  2843. spin_lock_irq(&device->ldev->md.uuid_lock);
  2844. bm_uuid = device->ldev->md.uuid[UI_BITMAP];
  2845. if (bm_uuid)
  2846. drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid);
  2847. device->ldev->md.uuid[UI_BITMAP] = device->ldev->md.uuid[UI_CURRENT];
  2848. __drbd_uuid_set(device, UI_CURRENT, val);
  2849. spin_unlock_irq(&device->ldev->md.uuid_lock);
  2850. drbd_print_uuids(device, "new current UUID");
  2851. /* get it to stable storage _now_ */
  2852. drbd_md_sync(device);
  2853. }
  2854. void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local)
  2855. {
  2856. unsigned long flags;
  2857. spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
  2858. if (device->ldev->md.uuid[UI_BITMAP] == 0 && val == 0) {
  2859. spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
  2860. return;
  2861. }
  2862. if (val == 0) {
  2863. drbd_uuid_move_history(device);
  2864. device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP];
  2865. device->ldev->md.uuid[UI_BITMAP] = 0;
  2866. } else {
  2867. unsigned long long bm_uuid = device->ldev->md.uuid[UI_BITMAP];
  2868. if (bm_uuid)
  2869. drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid);
  2870. device->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
  2871. }
  2872. spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
  2873. drbd_md_mark_dirty(device);
  2874. }
  2875. /**
  2876. * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  2877. * @device: DRBD device.
  2878. * @peer_device: Peer DRBD device.
  2879. *
  2880. * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
  2881. */
  2882. int drbd_bmio_set_n_write(struct drbd_device *device,
  2883. struct drbd_peer_device *peer_device) __must_hold(local)
  2884. {
  2885. int rv = -EIO;
  2886. drbd_md_set_flag(device, MDF_FULL_SYNC);
  2887. drbd_md_sync(device);
  2888. drbd_bm_set_all(device);
  2889. rv = drbd_bm_write(device, peer_device);
  2890. if (!rv) {
  2891. drbd_md_clear_flag(device, MDF_FULL_SYNC);
  2892. drbd_md_sync(device);
  2893. }
  2894. return rv;
  2895. }
  2896. /**
  2897. * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  2898. * @device: DRBD device.
  2899. * @peer_device: Peer DRBD device.
  2900. *
  2901. * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
  2902. */
  2903. int drbd_bmio_clear_n_write(struct drbd_device *device,
  2904. struct drbd_peer_device *peer_device) __must_hold(local)
  2905. {
  2906. drbd_resume_al(device);
  2907. drbd_bm_clear_all(device);
  2908. return drbd_bm_write(device, peer_device);
  2909. }
  2910. static int w_bitmap_io(struct drbd_work *w, int unused)
  2911. {
  2912. struct drbd_device *device =
  2913. container_of(w, struct drbd_device, bm_io_work.w);
  2914. struct bm_io_work *work = &device->bm_io_work;
  2915. int rv = -EIO;
  2916. if (work->flags != BM_LOCKED_CHANGE_ALLOWED) {
  2917. int cnt = atomic_read(&device->ap_bio_cnt);
  2918. if (cnt)
  2919. drbd_err(device, "FIXME: ap_bio_cnt %d, expected 0; queued for '%s'\n",
  2920. cnt, work->why);
  2921. }
  2922. if (get_ldev(device)) {
  2923. drbd_bm_lock(device, work->why, work->flags);
  2924. rv = work->io_fn(device, work->peer_device);
  2925. drbd_bm_unlock(device);
  2926. put_ldev(device);
  2927. }
  2928. clear_bit_unlock(BITMAP_IO, &device->flags);
  2929. wake_up(&device->misc_wait);
  2930. if (work->done)
  2931. work->done(device, rv);
  2932. clear_bit(BITMAP_IO_QUEUED, &device->flags);
  2933. work->why = NULL;
  2934. work->flags = 0;
  2935. return 0;
  2936. }
  2937. /**
  2938. * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
  2939. * @device: DRBD device.
  2940. * @io_fn: IO callback to be called when bitmap IO is possible
  2941. * @done: callback to be called after the bitmap IO was performed
  2942. * @why: Descriptive text of the reason for doing the IO
  2943. * @flags: Bitmap flags
  2944. * @peer_device: Peer DRBD device.
  2945. *
  2946. * While IO on the bitmap happens we freeze application IO thus we ensure
  2947. * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
  2948. * called from worker context. It MUST NOT be used while a previous such
  2949. * work is still pending!
  2950. *
  2951. * Its worker function encloses the call of io_fn() by get_ldev() and
  2952. * put_ldev().
  2953. */
  2954. void drbd_queue_bitmap_io(struct drbd_device *device,
  2955. int (*io_fn)(struct drbd_device *, struct drbd_peer_device *),
  2956. void (*done)(struct drbd_device *, int),
  2957. char *why, enum bm_flag flags,
  2958. struct drbd_peer_device *peer_device)
  2959. {
  2960. D_ASSERT(device, current == peer_device->connection->worker.task);
  2961. D_ASSERT(device, !test_bit(BITMAP_IO_QUEUED, &device->flags));
  2962. D_ASSERT(device, !test_bit(BITMAP_IO, &device->flags));
  2963. D_ASSERT(device, list_empty(&device->bm_io_work.w.list));
  2964. if (device->bm_io_work.why)
  2965. drbd_err(device, "FIXME going to queue '%s' but '%s' still pending?\n",
  2966. why, device->bm_io_work.why);
  2967. device->bm_io_work.peer_device = peer_device;
  2968. device->bm_io_work.io_fn = io_fn;
  2969. device->bm_io_work.done = done;
  2970. device->bm_io_work.why = why;
  2971. device->bm_io_work.flags = flags;
  2972. spin_lock_irq(&device->resource->req_lock);
  2973. set_bit(BITMAP_IO, &device->flags);
  2974. /* don't wait for pending application IO if the caller indicates that
  2975. * application IO does not conflict anyways. */
  2976. if (flags == BM_LOCKED_CHANGE_ALLOWED || atomic_read(&device->ap_bio_cnt) == 0) {
  2977. if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags))
  2978. drbd_queue_work(&peer_device->connection->sender_work,
  2979. &device->bm_io_work.w);
  2980. }
  2981. spin_unlock_irq(&device->resource->req_lock);
  2982. }
  2983. /**
  2984. * drbd_bitmap_io() - Does an IO operation on the whole bitmap
  2985. * @device: DRBD device.
  2986. * @io_fn: IO callback to be called when bitmap IO is possible
  2987. * @why: Descriptive text of the reason for doing the IO
  2988. * @flags: Bitmap flags
  2989. * @peer_device: Peer DRBD device.
  2990. *
  2991. * freezes application IO while that the actual IO operations runs. This
  2992. * functions MAY NOT be called from worker context.
  2993. */
  2994. int drbd_bitmap_io(struct drbd_device *device,
  2995. int (*io_fn)(struct drbd_device *, struct drbd_peer_device *),
  2996. char *why, enum bm_flag flags,
  2997. struct drbd_peer_device *peer_device)
  2998. {
  2999. /* Only suspend io, if some operation is supposed to be locked out */
  3000. const bool do_suspend_io = flags & (BM_DONT_CLEAR|BM_DONT_SET|BM_DONT_TEST);
  3001. int rv;
  3002. D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
  3003. if (do_suspend_io)
  3004. drbd_suspend_io(device);
  3005. drbd_bm_lock(device, why, flags);
  3006. rv = io_fn(device, peer_device);
  3007. drbd_bm_unlock(device);
  3008. if (do_suspend_io)
  3009. drbd_resume_io(device);
  3010. return rv;
  3011. }
  3012. void drbd_md_set_flag(struct drbd_device *device, int flag) __must_hold(local)
  3013. {
  3014. if ((device->ldev->md.flags & flag) != flag) {
  3015. drbd_md_mark_dirty(device);
  3016. device->ldev->md.flags |= flag;
  3017. }
  3018. }
  3019. void drbd_md_clear_flag(struct drbd_device *device, int flag) __must_hold(local)
  3020. {
  3021. if ((device->ldev->md.flags & flag) != 0) {
  3022. drbd_md_mark_dirty(device);
  3023. device->ldev->md.flags &= ~flag;
  3024. }
  3025. }
  3026. int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
  3027. {
  3028. return (bdev->md.flags & flag) != 0;
  3029. }
  3030. static void md_sync_timer_fn(struct timer_list *t)
  3031. {
  3032. struct drbd_device *device = timer_container_of(device, t,
  3033. md_sync_timer);
  3034. drbd_device_post_work(device, MD_SYNC);
  3035. }
  3036. const char *cmdname(enum drbd_packet cmd)
  3037. {
  3038. /* THINK may need to become several global tables
  3039. * when we want to support more than
  3040. * one PRO_VERSION */
  3041. static const char *cmdnames[] = {
  3042. [P_DATA] = "Data",
  3043. [P_DATA_REPLY] = "DataReply",
  3044. [P_RS_DATA_REPLY] = "RSDataReply",
  3045. [P_BARRIER] = "Barrier",
  3046. [P_BITMAP] = "ReportBitMap",
  3047. [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
  3048. [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
  3049. [P_UNPLUG_REMOTE] = "UnplugRemote",
  3050. [P_DATA_REQUEST] = "DataRequest",
  3051. [P_RS_DATA_REQUEST] = "RSDataRequest",
  3052. [P_SYNC_PARAM] = "SyncParam",
  3053. [P_PROTOCOL] = "ReportProtocol",
  3054. [P_UUIDS] = "ReportUUIDs",
  3055. [P_SIZES] = "ReportSizes",
  3056. [P_STATE] = "ReportState",
  3057. [P_SYNC_UUID] = "ReportSyncUUID",
  3058. [P_AUTH_CHALLENGE] = "AuthChallenge",
  3059. [P_AUTH_RESPONSE] = "AuthResponse",
  3060. [P_STATE_CHG_REQ] = "StateChgRequest",
  3061. [P_PING] = "Ping",
  3062. [P_PING_ACK] = "PingAck",
  3063. [P_RECV_ACK] = "RecvAck",
  3064. [P_WRITE_ACK] = "WriteAck",
  3065. [P_RS_WRITE_ACK] = "RSWriteAck",
  3066. [P_SUPERSEDED] = "Superseded",
  3067. [P_NEG_ACK] = "NegAck",
  3068. [P_NEG_DREPLY] = "NegDReply",
  3069. [P_NEG_RS_DREPLY] = "NegRSDReply",
  3070. [P_BARRIER_ACK] = "BarrierAck",
  3071. [P_STATE_CHG_REPLY] = "StateChgReply",
  3072. [P_OV_REQUEST] = "OVRequest",
  3073. [P_OV_REPLY] = "OVReply",
  3074. [P_OV_RESULT] = "OVResult",
  3075. [P_CSUM_RS_REQUEST] = "CsumRSRequest",
  3076. [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
  3077. [P_SYNC_PARAM89] = "SyncParam89",
  3078. [P_COMPRESSED_BITMAP] = "CBitmap",
  3079. [P_DELAY_PROBE] = "DelayProbe",
  3080. [P_OUT_OF_SYNC] = "OutOfSync",
  3081. [P_RS_CANCEL] = "RSCancel",
  3082. [P_CONN_ST_CHG_REQ] = "conn_st_chg_req",
  3083. [P_CONN_ST_CHG_REPLY] = "conn_st_chg_reply",
  3084. [P_PROTOCOL_UPDATE] = "protocol_update",
  3085. [P_TRIM] = "Trim",
  3086. [P_RS_THIN_REQ] = "rs_thin_req",
  3087. [P_RS_DEALLOCATED] = "rs_deallocated",
  3088. [P_WSAME] = "WriteSame",
  3089. [P_ZEROES] = "Zeroes",
  3090. /* enum drbd_packet, but not commands - obsoleted flags:
  3091. * P_MAY_IGNORE
  3092. * P_MAX_OPT_CMD
  3093. */
  3094. };
  3095. /* too big for the array: 0xfffX */
  3096. if (cmd == P_INITIAL_META)
  3097. return "InitialMeta";
  3098. if (cmd == P_INITIAL_DATA)
  3099. return "InitialData";
  3100. if (cmd == P_CONNECTION_FEATURES)
  3101. return "ConnectionFeatures";
  3102. if (cmd >= ARRAY_SIZE(cmdnames))
  3103. return "Unknown";
  3104. return cmdnames[cmd];
  3105. }
  3106. /**
  3107. * drbd_wait_misc - wait for a request to make progress
  3108. * @device: device associated with the request
  3109. * @i: the struct drbd_interval embedded in struct drbd_request or
  3110. * struct drbd_peer_request
  3111. */
  3112. int drbd_wait_misc(struct drbd_device *device, struct drbd_interval *i)
  3113. {
  3114. struct net_conf *nc;
  3115. DEFINE_WAIT(wait);
  3116. long timeout;
  3117. rcu_read_lock();
  3118. nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
  3119. if (!nc) {
  3120. rcu_read_unlock();
  3121. return -ETIMEDOUT;
  3122. }
  3123. timeout = nc->ko_count ? nc->timeout * HZ / 10 * nc->ko_count : MAX_SCHEDULE_TIMEOUT;
  3124. rcu_read_unlock();
  3125. /* Indicate to wake up device->misc_wait on progress. */
  3126. i->waiting = true;
  3127. prepare_to_wait(&device->misc_wait, &wait, TASK_INTERRUPTIBLE);
  3128. spin_unlock_irq(&device->resource->req_lock);
  3129. timeout = schedule_timeout(timeout);
  3130. finish_wait(&device->misc_wait, &wait);
  3131. spin_lock_irq(&device->resource->req_lock);
  3132. if (!timeout || device->state.conn < C_CONNECTED)
  3133. return -ETIMEDOUT;
  3134. if (signal_pending(current))
  3135. return -ERESTARTSYS;
  3136. return 0;
  3137. }
  3138. void lock_all_resources(void)
  3139. {
  3140. struct drbd_resource *resource;
  3141. int __maybe_unused i = 0;
  3142. mutex_lock(&resources_mutex);
  3143. local_irq_disable();
  3144. for_each_resource(resource, &drbd_resources)
  3145. spin_lock_nested(&resource->req_lock, i++);
  3146. }
  3147. void unlock_all_resources(void)
  3148. {
  3149. struct drbd_resource *resource;
  3150. for_each_resource(resource, &drbd_resources)
  3151. spin_unlock(&resource->req_lock);
  3152. local_irq_enable();
  3153. mutex_unlock(&resources_mutex);
  3154. }
  3155. #ifdef CONFIG_DRBD_FAULT_INJECTION
  3156. /* Fault insertion support including random number generator shamelessly
  3157. * stolen from kernel/rcutorture.c */
  3158. struct fault_random_state {
  3159. unsigned long state;
  3160. unsigned long count;
  3161. };
  3162. #define FAULT_RANDOM_MULT 39916801 /* prime */
  3163. #define FAULT_RANDOM_ADD 479001701 /* prime */
  3164. #define FAULT_RANDOM_REFRESH 10000
  3165. /*
  3166. * Crude but fast random-number generator. Uses a linear congruential
  3167. * generator, with occasional help from get_random_bytes().
  3168. */
  3169. static unsigned long
  3170. _drbd_fault_random(struct fault_random_state *rsp)
  3171. {
  3172. long refresh;
  3173. if (!rsp->count--) {
  3174. get_random_bytes(&refresh, sizeof(refresh));
  3175. rsp->state += refresh;
  3176. rsp->count = FAULT_RANDOM_REFRESH;
  3177. }
  3178. rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
  3179. return swahw32(rsp->state);
  3180. }
  3181. static char *
  3182. _drbd_fault_str(unsigned int type) {
  3183. static char *_faults[] = {
  3184. [DRBD_FAULT_MD_WR] = "Meta-data write",
  3185. [DRBD_FAULT_MD_RD] = "Meta-data read",
  3186. [DRBD_FAULT_RS_WR] = "Resync write",
  3187. [DRBD_FAULT_RS_RD] = "Resync read",
  3188. [DRBD_FAULT_DT_WR] = "Data write",
  3189. [DRBD_FAULT_DT_RD] = "Data read",
  3190. [DRBD_FAULT_DT_RA] = "Data read ahead",
  3191. [DRBD_FAULT_BM_ALLOC] = "BM allocation",
  3192. [DRBD_FAULT_AL_EE] = "EE allocation",
  3193. [DRBD_FAULT_RECEIVE] = "receive data corruption",
  3194. };
  3195. return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
  3196. }
  3197. unsigned int
  3198. _drbd_insert_fault(struct drbd_device *device, unsigned int type)
  3199. {
  3200. static struct fault_random_state rrs = {0, 0};
  3201. unsigned int ret = (
  3202. (drbd_fault_devs == 0 ||
  3203. ((1 << device_to_minor(device)) & drbd_fault_devs) != 0) &&
  3204. (((_drbd_fault_random(&rrs) % 100) + 1) <= drbd_fault_rate));
  3205. if (ret) {
  3206. drbd_fault_count++;
  3207. if (drbd_ratelimit())
  3208. drbd_warn(device, "***Simulating %s failure\n",
  3209. _drbd_fault_str(type));
  3210. }
  3211. return ret;
  3212. }
  3213. #endif
  3214. module_init(drbd_init)
  3215. module_exit(drbd_cleanup)
  3216. EXPORT_SYMBOL(drbd_conn_str);
  3217. EXPORT_SYMBOL(drbd_role_str);
  3218. EXPORT_SYMBOL(drbd_disk_str);
  3219. EXPORT_SYMBOL(drbd_set_st_err_str);