drbd_main.c 112 KB

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