drbd_main.c 109 KB

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