drbd_main.c 112 KB

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