drbd_main.c 108 KB

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