drbd_nl.c 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982
  1. /*
  2. drbd_nl.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. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/drbd.h>
  22. #include <linux/in.h>
  23. #include <linux/fs.h>
  24. #include <linux/file.h>
  25. #include <linux/slab.h>
  26. #include <linux/blkpg.h>
  27. #include <linux/cpumask.h>
  28. #include "drbd_int.h"
  29. #include "drbd_protocol.h"
  30. #include "drbd_req.h"
  31. #include "drbd_state_change.h"
  32. #include <asm/unaligned.h>
  33. #include <linux/drbd_limits.h>
  34. #include <linux/kthread.h>
  35. #include <net/genetlink.h>
  36. /* .doit */
  37. // int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
  38. // int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
  39. int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
  40. int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
  41. int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
  42. int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
  43. int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
  44. int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
  45. int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
  46. int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
  47. int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
  48. int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
  49. int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
  50. int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
  51. int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
  52. int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
  53. int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
  54. int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
  55. int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
  56. int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
  57. int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
  58. int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
  59. int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
  60. int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
  61. int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
  62. int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
  63. int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
  64. /* .dumpit */
  65. int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
  66. int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb);
  67. int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb);
  68. int drbd_adm_dump_devices_done(struct netlink_callback *cb);
  69. int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb);
  70. int drbd_adm_dump_connections_done(struct netlink_callback *cb);
  71. int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb);
  72. int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb);
  73. int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb);
  74. #include <linux/drbd_genl_api.h>
  75. #include "drbd_nla.h"
  76. #include <linux/genl_magic_func.h>
  77. static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
  78. static atomic_t notify_genl_seq = ATOMIC_INIT(2); /* two. */
  79. DEFINE_MUTEX(notification_mutex);
  80. /* used blkdev_get_by_path, to claim our meta data device(s) */
  81. static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
  82. static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
  83. {
  84. genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
  85. if (genlmsg_reply(skb, info))
  86. pr_err("error sending genl reply\n");
  87. }
  88. /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
  89. * reason it could fail was no space in skb, and there are 4k available. */
  90. static int drbd_msg_put_info(struct sk_buff *skb, const char *info)
  91. {
  92. struct nlattr *nla;
  93. int err = -EMSGSIZE;
  94. if (!info || !info[0])
  95. return 0;
  96. nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
  97. if (!nla)
  98. return err;
  99. err = nla_put_string(skb, T_info_text, info);
  100. if (err) {
  101. nla_nest_cancel(skb, nla);
  102. return err;
  103. } else
  104. nla_nest_end(skb, nla);
  105. return 0;
  106. }
  107. /* This would be a good candidate for a "pre_doit" hook,
  108. * and per-family private info->pointers.
  109. * But we need to stay compatible with older kernels.
  110. * If it returns successfully, adm_ctx members are valid.
  111. *
  112. * At this point, we still rely on the global genl_lock().
  113. * If we want to avoid that, and allow "genl_family.parallel_ops", we may need
  114. * to add additional synchronization against object destruction/modification.
  115. */
  116. #define DRBD_ADM_NEED_MINOR 1
  117. #define DRBD_ADM_NEED_RESOURCE 2
  118. #define DRBD_ADM_NEED_CONNECTION 4
  119. static int drbd_adm_prepare(struct drbd_config_context *adm_ctx,
  120. struct sk_buff *skb, struct genl_info *info, unsigned flags)
  121. {
  122. struct drbd_genlmsghdr *d_in = info->userhdr;
  123. const u8 cmd = info->genlhdr->cmd;
  124. int err;
  125. memset(adm_ctx, 0, sizeof(*adm_ctx));
  126. /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
  127. if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
  128. return -EPERM;
  129. adm_ctx->reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  130. if (!adm_ctx->reply_skb) {
  131. err = -ENOMEM;
  132. goto fail;
  133. }
  134. adm_ctx->reply_dh = genlmsg_put_reply(adm_ctx->reply_skb,
  135. info, &drbd_genl_family, 0, cmd);
  136. /* put of a few bytes into a fresh skb of >= 4k will always succeed.
  137. * but anyways */
  138. if (!adm_ctx->reply_dh) {
  139. err = -ENOMEM;
  140. goto fail;
  141. }
  142. adm_ctx->reply_dh->minor = d_in->minor;
  143. adm_ctx->reply_dh->ret_code = NO_ERROR;
  144. adm_ctx->volume = VOLUME_UNSPECIFIED;
  145. if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
  146. struct nlattr *nla;
  147. /* parse and validate only */
  148. err = drbd_cfg_context_from_attrs(NULL, info);
  149. if (err)
  150. goto fail;
  151. /* It was present, and valid,
  152. * copy it over to the reply skb. */
  153. err = nla_put_nohdr(adm_ctx->reply_skb,
  154. info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
  155. info->attrs[DRBD_NLA_CFG_CONTEXT]);
  156. if (err)
  157. goto fail;
  158. /* and assign stuff to the adm_ctx */
  159. nla = nested_attr_tb[__nla_type(T_ctx_volume)];
  160. if (nla)
  161. adm_ctx->volume = nla_get_u32(nla);
  162. nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
  163. if (nla)
  164. adm_ctx->resource_name = nla_data(nla);
  165. adm_ctx->my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
  166. adm_ctx->peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
  167. if ((adm_ctx->my_addr &&
  168. nla_len(adm_ctx->my_addr) > sizeof(adm_ctx->connection->my_addr)) ||
  169. (adm_ctx->peer_addr &&
  170. nla_len(adm_ctx->peer_addr) > sizeof(adm_ctx->connection->peer_addr))) {
  171. err = -EINVAL;
  172. goto fail;
  173. }
  174. }
  175. adm_ctx->minor = d_in->minor;
  176. adm_ctx->device = minor_to_device(d_in->minor);
  177. /* We are protected by the global genl_lock().
  178. * But we may explicitly drop it/retake it in drbd_adm_set_role(),
  179. * so make sure this object stays around. */
  180. if (adm_ctx->device)
  181. kref_get(&adm_ctx->device->kref);
  182. if (adm_ctx->resource_name) {
  183. adm_ctx->resource = drbd_find_resource(adm_ctx->resource_name);
  184. }
  185. if (!adm_ctx->device && (flags & DRBD_ADM_NEED_MINOR)) {
  186. drbd_msg_put_info(adm_ctx->reply_skb, "unknown minor");
  187. return ERR_MINOR_INVALID;
  188. }
  189. if (!adm_ctx->resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
  190. drbd_msg_put_info(adm_ctx->reply_skb, "unknown resource");
  191. if (adm_ctx->resource_name)
  192. return ERR_RES_NOT_KNOWN;
  193. return ERR_INVALID_REQUEST;
  194. }
  195. if (flags & DRBD_ADM_NEED_CONNECTION) {
  196. if (adm_ctx->resource) {
  197. drbd_msg_put_info(adm_ctx->reply_skb, "no resource name expected");
  198. return ERR_INVALID_REQUEST;
  199. }
  200. if (adm_ctx->device) {
  201. drbd_msg_put_info(adm_ctx->reply_skb, "no minor number expected");
  202. return ERR_INVALID_REQUEST;
  203. }
  204. if (adm_ctx->my_addr && adm_ctx->peer_addr)
  205. adm_ctx->connection = conn_get_by_addrs(nla_data(adm_ctx->my_addr),
  206. nla_len(adm_ctx->my_addr),
  207. nla_data(adm_ctx->peer_addr),
  208. nla_len(adm_ctx->peer_addr));
  209. if (!adm_ctx->connection) {
  210. drbd_msg_put_info(adm_ctx->reply_skb, "unknown connection");
  211. return ERR_INVALID_REQUEST;
  212. }
  213. }
  214. /* some more paranoia, if the request was over-determined */
  215. if (adm_ctx->device && adm_ctx->resource &&
  216. adm_ctx->device->resource != adm_ctx->resource) {
  217. pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
  218. adm_ctx->minor, adm_ctx->resource->name,
  219. adm_ctx->device->resource->name);
  220. drbd_msg_put_info(adm_ctx->reply_skb, "minor exists in different resource");
  221. return ERR_INVALID_REQUEST;
  222. }
  223. if (adm_ctx->device &&
  224. adm_ctx->volume != VOLUME_UNSPECIFIED &&
  225. adm_ctx->volume != adm_ctx->device->vnr) {
  226. pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
  227. adm_ctx->minor, adm_ctx->volume,
  228. adm_ctx->device->vnr,
  229. adm_ctx->device->resource->name);
  230. drbd_msg_put_info(adm_ctx->reply_skb, "minor exists as different volume");
  231. return ERR_INVALID_REQUEST;
  232. }
  233. /* still, provide adm_ctx->resource always, if possible. */
  234. if (!adm_ctx->resource) {
  235. adm_ctx->resource = adm_ctx->device ? adm_ctx->device->resource
  236. : adm_ctx->connection ? adm_ctx->connection->resource : NULL;
  237. if (adm_ctx->resource)
  238. kref_get(&adm_ctx->resource->kref);
  239. }
  240. return NO_ERROR;
  241. fail:
  242. nlmsg_free(adm_ctx->reply_skb);
  243. adm_ctx->reply_skb = NULL;
  244. return err;
  245. }
  246. static int drbd_adm_finish(struct drbd_config_context *adm_ctx,
  247. struct genl_info *info, int retcode)
  248. {
  249. if (adm_ctx->device) {
  250. kref_put(&adm_ctx->device->kref, drbd_destroy_device);
  251. adm_ctx->device = NULL;
  252. }
  253. if (adm_ctx->connection) {
  254. kref_put(&adm_ctx->connection->kref, &drbd_destroy_connection);
  255. adm_ctx->connection = NULL;
  256. }
  257. if (adm_ctx->resource) {
  258. kref_put(&adm_ctx->resource->kref, drbd_destroy_resource);
  259. adm_ctx->resource = NULL;
  260. }
  261. if (!adm_ctx->reply_skb)
  262. return -ENOMEM;
  263. adm_ctx->reply_dh->ret_code = retcode;
  264. drbd_adm_send_reply(adm_ctx->reply_skb, info);
  265. return 0;
  266. }
  267. static void setup_khelper_env(struct drbd_connection *connection, char **envp)
  268. {
  269. char *afs;
  270. /* FIXME: A future version will not allow this case. */
  271. if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
  272. return;
  273. switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
  274. case AF_INET6:
  275. afs = "ipv6";
  276. snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
  277. &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
  278. break;
  279. case AF_INET:
  280. afs = "ipv4";
  281. snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
  282. &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
  283. break;
  284. default:
  285. afs = "ssocks";
  286. snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
  287. &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
  288. }
  289. snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
  290. }
  291. int drbd_khelper(struct drbd_device *device, char *cmd)
  292. {
  293. char *envp[] = { "HOME=/",
  294. "TERM=linux",
  295. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  296. (char[20]) { }, /* address family */
  297. (char[60]) { }, /* address */
  298. NULL };
  299. char mb[14];
  300. char *argv[] = {usermode_helper, cmd, mb, NULL };
  301. struct drbd_connection *connection = first_peer_device(device)->connection;
  302. struct sib_info sib;
  303. int ret;
  304. if (current == connection->worker.task)
  305. set_bit(CALLBACK_PENDING, &connection->flags);
  306. snprintf(mb, 14, "minor-%d", device_to_minor(device));
  307. setup_khelper_env(connection, envp);
  308. /* The helper may take some time.
  309. * write out any unsynced meta data changes now */
  310. drbd_md_sync(device);
  311. drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
  312. sib.sib_reason = SIB_HELPER_PRE;
  313. sib.helper_name = cmd;
  314. drbd_bcast_event(device, &sib);
  315. notify_helper(NOTIFY_CALL, device, connection, cmd, 0);
  316. ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
  317. if (ret)
  318. drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
  319. usermode_helper, cmd, mb,
  320. (ret >> 8) & 0xff, ret);
  321. else
  322. drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
  323. usermode_helper, cmd, mb,
  324. (ret >> 8) & 0xff, ret);
  325. sib.sib_reason = SIB_HELPER_POST;
  326. sib.helper_exit_code = ret;
  327. drbd_bcast_event(device, &sib);
  328. notify_helper(NOTIFY_RESPONSE, device, connection, cmd, ret);
  329. if (current == connection->worker.task)
  330. clear_bit(CALLBACK_PENDING, &connection->flags);
  331. if (ret < 0) /* Ignore any ERRNOs we got. */
  332. ret = 0;
  333. return ret;
  334. }
  335. enum drbd_peer_state conn_khelper(struct drbd_connection *connection, char *cmd)
  336. {
  337. char *envp[] = { "HOME=/",
  338. "TERM=linux",
  339. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  340. (char[20]) { }, /* address family */
  341. (char[60]) { }, /* address */
  342. NULL };
  343. char *resource_name = connection->resource->name;
  344. char *argv[] = {usermode_helper, cmd, resource_name, NULL };
  345. int ret;
  346. setup_khelper_env(connection, envp);
  347. conn_md_sync(connection);
  348. drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
  349. /* TODO: conn_bcast_event() ?? */
  350. notify_helper(NOTIFY_CALL, NULL, connection, cmd, 0);
  351. ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
  352. if (ret)
  353. drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
  354. usermode_helper, cmd, resource_name,
  355. (ret >> 8) & 0xff, ret);
  356. else
  357. drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
  358. usermode_helper, cmd, resource_name,
  359. (ret >> 8) & 0xff, ret);
  360. /* TODO: conn_bcast_event() ?? */
  361. notify_helper(NOTIFY_RESPONSE, NULL, connection, cmd, ret);
  362. if (ret < 0) /* Ignore any ERRNOs we got. */
  363. ret = 0;
  364. return ret;
  365. }
  366. static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
  367. {
  368. enum drbd_fencing_p fp = FP_NOT_AVAIL;
  369. struct drbd_peer_device *peer_device;
  370. int vnr;
  371. rcu_read_lock();
  372. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  373. struct drbd_device *device = peer_device->device;
  374. if (get_ldev_if_state(device, D_CONSISTENT)) {
  375. struct disk_conf *disk_conf =
  376. rcu_dereference(peer_device->device->ldev->disk_conf);
  377. fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
  378. put_ldev(device);
  379. }
  380. }
  381. rcu_read_unlock();
  382. return fp;
  383. }
  384. static bool resource_is_supended(struct drbd_resource *resource)
  385. {
  386. return resource->susp || resource->susp_fen || resource->susp_nod;
  387. }
  388. bool conn_try_outdate_peer(struct drbd_connection *connection)
  389. {
  390. struct drbd_resource * const resource = connection->resource;
  391. unsigned int connect_cnt;
  392. union drbd_state mask = { };
  393. union drbd_state val = { };
  394. enum drbd_fencing_p fp;
  395. char *ex_to_string;
  396. int r;
  397. spin_lock_irq(&resource->req_lock);
  398. if (connection->cstate >= C_WF_REPORT_PARAMS) {
  399. drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
  400. spin_unlock_irq(&resource->req_lock);
  401. return false;
  402. }
  403. connect_cnt = connection->connect_cnt;
  404. spin_unlock_irq(&resource->req_lock);
  405. fp = highest_fencing_policy(connection);
  406. switch (fp) {
  407. case FP_NOT_AVAIL:
  408. drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
  409. spin_lock_irq(&resource->req_lock);
  410. if (connection->cstate < C_WF_REPORT_PARAMS) {
  411. _conn_request_state(connection,
  412. (union drbd_state) { { .susp_fen = 1 } },
  413. (union drbd_state) { { .susp_fen = 0 } },
  414. CS_VERBOSE | CS_HARD | CS_DC_SUSP);
  415. /* We are no longer suspended due to the fencing policy.
  416. * We may still be suspended due to the on-no-data-accessible policy.
  417. * If that was OND_IO_ERROR, fail pending requests. */
  418. if (!resource_is_supended(resource))
  419. _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
  420. }
  421. /* Else: in case we raced with a connection handshake,
  422. * let the handshake figure out if we maybe can RESEND,
  423. * and do not resume/fail pending requests here.
  424. * Worst case is we stay suspended for now, which may be
  425. * resolved by either re-establishing the replication link, or
  426. * the next link failure, or eventually the administrator. */
  427. spin_unlock_irq(&resource->req_lock);
  428. return false;
  429. case FP_DONT_CARE:
  430. return true;
  431. default: ;
  432. }
  433. r = conn_khelper(connection, "fence-peer");
  434. switch ((r>>8) & 0xff) {
  435. case P_INCONSISTENT: /* peer is inconsistent */
  436. ex_to_string = "peer is inconsistent or worse";
  437. mask.pdsk = D_MASK;
  438. val.pdsk = D_INCONSISTENT;
  439. break;
  440. case P_OUTDATED: /* peer got outdated, or was already outdated */
  441. ex_to_string = "peer was fenced";
  442. mask.pdsk = D_MASK;
  443. val.pdsk = D_OUTDATED;
  444. break;
  445. case P_DOWN: /* peer was down */
  446. if (conn_highest_disk(connection) == D_UP_TO_DATE) {
  447. /* we will(have) create(d) a new UUID anyways... */
  448. ex_to_string = "peer is unreachable, assumed to be dead";
  449. mask.pdsk = D_MASK;
  450. val.pdsk = D_OUTDATED;
  451. } else {
  452. ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
  453. }
  454. break;
  455. case P_PRIMARY: /* Peer is primary, voluntarily outdate myself.
  456. * This is useful when an unconnected R_SECONDARY is asked to
  457. * become R_PRIMARY, but finds the other peer being active. */
  458. ex_to_string = "peer is active";
  459. drbd_warn(connection, "Peer is primary, outdating myself.\n");
  460. mask.disk = D_MASK;
  461. val.disk = D_OUTDATED;
  462. break;
  463. case P_FENCING:
  464. /* THINK: do we need to handle this
  465. * like case 4, or more like case 5? */
  466. if (fp != FP_STONITH)
  467. drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
  468. ex_to_string = "peer was stonithed";
  469. mask.pdsk = D_MASK;
  470. val.pdsk = D_OUTDATED;
  471. break;
  472. default:
  473. /* The script is broken ... */
  474. drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
  475. return false; /* Eventually leave IO frozen */
  476. }
  477. drbd_info(connection, "fence-peer helper returned %d (%s)\n",
  478. (r>>8) & 0xff, ex_to_string);
  479. /* Not using
  480. conn_request_state(connection, mask, val, CS_VERBOSE);
  481. here, because we might were able to re-establish the connection in the
  482. meantime. */
  483. spin_lock_irq(&resource->req_lock);
  484. if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
  485. if (connection->connect_cnt != connect_cnt)
  486. /* In case the connection was established and droped
  487. while the fence-peer handler was running, ignore it */
  488. drbd_info(connection, "Ignoring fence-peer exit code\n");
  489. else
  490. _conn_request_state(connection, mask, val, CS_VERBOSE);
  491. }
  492. spin_unlock_irq(&resource->req_lock);
  493. return conn_highest_pdsk(connection) <= D_OUTDATED;
  494. }
  495. static int _try_outdate_peer_async(void *data)
  496. {
  497. struct drbd_connection *connection = (struct drbd_connection *)data;
  498. conn_try_outdate_peer(connection);
  499. kref_put(&connection->kref, drbd_destroy_connection);
  500. return 0;
  501. }
  502. void conn_try_outdate_peer_async(struct drbd_connection *connection)
  503. {
  504. struct task_struct *opa;
  505. kref_get(&connection->kref);
  506. /* We may just have force_sig()'ed this thread
  507. * to get it out of some blocking network function.
  508. * Clear signals; otherwise kthread_run(), which internally uses
  509. * wait_on_completion_killable(), will mistake our pending signal
  510. * for a new fatal signal and fail. */
  511. flush_signals(current);
  512. opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
  513. if (IS_ERR(opa)) {
  514. drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
  515. kref_put(&connection->kref, drbd_destroy_connection);
  516. }
  517. }
  518. enum drbd_state_rv
  519. drbd_set_role(struct drbd_device *const device, enum drbd_role new_role, int force)
  520. {
  521. struct drbd_peer_device *const peer_device = first_peer_device(device);
  522. struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
  523. const int max_tries = 4;
  524. enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
  525. struct net_conf *nc;
  526. int try = 0;
  527. int forced = 0;
  528. union drbd_state mask, val;
  529. if (new_role == R_PRIMARY) {
  530. struct drbd_connection *connection;
  531. /* Detect dead peers as soon as possible. */
  532. rcu_read_lock();
  533. for_each_connection(connection, device->resource)
  534. request_ping(connection);
  535. rcu_read_unlock();
  536. }
  537. mutex_lock(device->state_mutex);
  538. mask.i = 0; mask.role = R_MASK;
  539. val.i = 0; val.role = new_role;
  540. while (try++ < max_tries) {
  541. rv = _drbd_request_state_holding_state_mutex(device, mask, val, CS_WAIT_COMPLETE);
  542. /* in case we first succeeded to outdate,
  543. * but now suddenly could establish a connection */
  544. if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
  545. val.pdsk = 0;
  546. mask.pdsk = 0;
  547. continue;
  548. }
  549. if (rv == SS_NO_UP_TO_DATE_DISK && force &&
  550. (device->state.disk < D_UP_TO_DATE &&
  551. device->state.disk >= D_INCONSISTENT)) {
  552. mask.disk = D_MASK;
  553. val.disk = D_UP_TO_DATE;
  554. forced = 1;
  555. continue;
  556. }
  557. if (rv == SS_NO_UP_TO_DATE_DISK &&
  558. device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
  559. D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
  560. if (conn_try_outdate_peer(connection)) {
  561. val.disk = D_UP_TO_DATE;
  562. mask.disk = D_MASK;
  563. }
  564. continue;
  565. }
  566. if (rv == SS_NOTHING_TO_DO)
  567. goto out;
  568. if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
  569. if (!conn_try_outdate_peer(connection) && force) {
  570. drbd_warn(device, "Forced into split brain situation!\n");
  571. mask.pdsk = D_MASK;
  572. val.pdsk = D_OUTDATED;
  573. }
  574. continue;
  575. }
  576. if (rv == SS_TWO_PRIMARIES) {
  577. /* Maybe the peer is detected as dead very soon...
  578. retry at most once more in this case. */
  579. int timeo;
  580. rcu_read_lock();
  581. nc = rcu_dereference(connection->net_conf);
  582. timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
  583. rcu_read_unlock();
  584. schedule_timeout_interruptible(timeo);
  585. if (try < max_tries)
  586. try = max_tries - 1;
  587. continue;
  588. }
  589. if (rv < SS_SUCCESS) {
  590. rv = _drbd_request_state(device, mask, val,
  591. CS_VERBOSE + CS_WAIT_COMPLETE);
  592. if (rv < SS_SUCCESS)
  593. goto out;
  594. }
  595. break;
  596. }
  597. if (rv < SS_SUCCESS)
  598. goto out;
  599. if (forced)
  600. drbd_warn(device, "Forced to consider local data as UpToDate!\n");
  601. /* Wait until nothing is on the fly :) */
  602. wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
  603. /* FIXME also wait for all pending P_BARRIER_ACK? */
  604. if (new_role == R_SECONDARY) {
  605. if (get_ldev(device)) {
  606. device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
  607. put_ldev(device);
  608. }
  609. } else {
  610. mutex_lock(&device->resource->conf_update);
  611. nc = connection->net_conf;
  612. if (nc)
  613. nc->discard_my_data = 0; /* without copy; single bit op is atomic */
  614. mutex_unlock(&device->resource->conf_update);
  615. if (get_ldev(device)) {
  616. if (((device->state.conn < C_CONNECTED ||
  617. device->state.pdsk <= D_FAILED)
  618. && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
  619. drbd_uuid_new_current(device);
  620. device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
  621. put_ldev(device);
  622. }
  623. }
  624. /* writeout of activity log covered areas of the bitmap
  625. * to stable storage done in after state change already */
  626. if (device->state.conn >= C_WF_REPORT_PARAMS) {
  627. /* if this was forced, we should consider sync */
  628. if (forced)
  629. drbd_send_uuids(peer_device);
  630. drbd_send_current_state(peer_device);
  631. }
  632. drbd_md_sync(device);
  633. set_disk_ro(device->vdisk, new_role == R_SECONDARY);
  634. kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
  635. out:
  636. mutex_unlock(device->state_mutex);
  637. return rv;
  638. }
  639. static const char *from_attrs_err_to_txt(int err)
  640. {
  641. return err == -ENOMSG ? "required attribute missing" :
  642. err == -EOPNOTSUPP ? "unknown mandatory attribute" :
  643. err == -EEXIST ? "can not change invariant setting" :
  644. "invalid attribute value";
  645. }
  646. int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
  647. {
  648. struct drbd_config_context adm_ctx;
  649. struct set_role_parms parms;
  650. int err;
  651. enum drbd_ret_code retcode;
  652. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  653. if (!adm_ctx.reply_skb)
  654. return retcode;
  655. if (retcode != NO_ERROR)
  656. goto out;
  657. memset(&parms, 0, sizeof(parms));
  658. if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
  659. err = set_role_parms_from_attrs(&parms, info);
  660. if (err) {
  661. retcode = ERR_MANDATORY_TAG;
  662. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  663. goto out;
  664. }
  665. }
  666. genl_unlock();
  667. mutex_lock(&adm_ctx.resource->adm_mutex);
  668. if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
  669. retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
  670. else
  671. retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
  672. mutex_unlock(&adm_ctx.resource->adm_mutex);
  673. genl_lock();
  674. out:
  675. drbd_adm_finish(&adm_ctx, info, retcode);
  676. return 0;
  677. }
  678. /* Initializes the md.*_offset members, so we are able to find
  679. * the on disk meta data.
  680. *
  681. * We currently have two possible layouts:
  682. * external:
  683. * |----------- md_size_sect ------------------|
  684. * [ 4k superblock ][ activity log ][ Bitmap ]
  685. * | al_offset == 8 |
  686. * | bm_offset = al_offset + X |
  687. * ==> bitmap sectors = md_size_sect - bm_offset
  688. *
  689. * internal:
  690. * |----------- md_size_sect ------------------|
  691. * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
  692. * | al_offset < 0 |
  693. * | bm_offset = al_offset - Y |
  694. * ==> bitmap sectors = Y = al_offset - bm_offset
  695. *
  696. * Activity log size used to be fixed 32kB,
  697. * but is about to become configurable.
  698. */
  699. static void drbd_md_set_sector_offsets(struct drbd_device *device,
  700. struct drbd_backing_dev *bdev)
  701. {
  702. sector_t md_size_sect = 0;
  703. unsigned int al_size_sect = bdev->md.al_size_4k * 8;
  704. bdev->md.md_offset = drbd_md_ss(bdev);
  705. switch (bdev->md.meta_dev_idx) {
  706. default:
  707. /* v07 style fixed size indexed meta data */
  708. bdev->md.md_size_sect = MD_128MB_SECT;
  709. bdev->md.al_offset = MD_4kB_SECT;
  710. bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
  711. break;
  712. case DRBD_MD_INDEX_FLEX_EXT:
  713. /* just occupy the full device; unit: sectors */
  714. bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
  715. bdev->md.al_offset = MD_4kB_SECT;
  716. bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
  717. break;
  718. case DRBD_MD_INDEX_INTERNAL:
  719. case DRBD_MD_INDEX_FLEX_INT:
  720. /* al size is still fixed */
  721. bdev->md.al_offset = -al_size_sect;
  722. /* we need (slightly less than) ~ this much bitmap sectors: */
  723. md_size_sect = drbd_get_capacity(bdev->backing_bdev);
  724. md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
  725. md_size_sect = BM_SECT_TO_EXT(md_size_sect);
  726. md_size_sect = ALIGN(md_size_sect, 8);
  727. /* plus the "drbd meta data super block",
  728. * and the activity log; */
  729. md_size_sect += MD_4kB_SECT + al_size_sect;
  730. bdev->md.md_size_sect = md_size_sect;
  731. /* bitmap offset is adjusted by 'super' block size */
  732. bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
  733. break;
  734. }
  735. }
  736. /* input size is expected to be in KB */
  737. char *ppsize(char *buf, unsigned long long size)
  738. {
  739. /* Needs 9 bytes at max including trailing NUL:
  740. * -1ULL ==> "16384 EB" */
  741. static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
  742. int base = 0;
  743. while (size >= 10000 && base < sizeof(units)-1) {
  744. /* shift + round */
  745. size = (size >> 10) + !!(size & (1<<9));
  746. base++;
  747. }
  748. sprintf(buf, "%u %cB", (unsigned)size, units[base]);
  749. return buf;
  750. }
  751. /* there is still a theoretical deadlock when called from receiver
  752. * on an D_INCONSISTENT R_PRIMARY:
  753. * remote READ does inc_ap_bio, receiver would need to receive answer
  754. * packet from remote to dec_ap_bio again.
  755. * receiver receive_sizes(), comes here,
  756. * waits for ap_bio_cnt == 0. -> deadlock.
  757. * but this cannot happen, actually, because:
  758. * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
  759. * (not connected, or bad/no disk on peer):
  760. * see drbd_fail_request_early, ap_bio_cnt is zero.
  761. * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
  762. * peer may not initiate a resize.
  763. */
  764. /* Note these are not to be confused with
  765. * drbd_adm_suspend_io/drbd_adm_resume_io,
  766. * which are (sub) state changes triggered by admin (drbdsetup),
  767. * and can be long lived.
  768. * This changes an device->flag, is triggered by drbd internals,
  769. * and should be short-lived. */
  770. /* It needs to be a counter, since multiple threads might
  771. independently suspend and resume IO. */
  772. void drbd_suspend_io(struct drbd_device *device)
  773. {
  774. atomic_inc(&device->suspend_cnt);
  775. if (drbd_suspended(device))
  776. return;
  777. wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
  778. }
  779. void drbd_resume_io(struct drbd_device *device)
  780. {
  781. if (atomic_dec_and_test(&device->suspend_cnt))
  782. wake_up(&device->misc_wait);
  783. }
  784. /**
  785. * drbd_determine_dev_size() - Sets the right device size obeying all constraints
  786. * @device: DRBD device.
  787. *
  788. * Returns 0 on success, negative return values indicate errors.
  789. * You should call drbd_md_sync() after calling this function.
  790. */
  791. enum determine_dev_size
  792. drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
  793. {
  794. struct md_offsets_and_sizes {
  795. u64 last_agreed_sect;
  796. u64 md_offset;
  797. s32 al_offset;
  798. s32 bm_offset;
  799. u32 md_size_sect;
  800. u32 al_stripes;
  801. u32 al_stripe_size_4k;
  802. } prev;
  803. sector_t u_size, size;
  804. struct drbd_md *md = &device->ldev->md;
  805. char ppb[10];
  806. void *buffer;
  807. int md_moved, la_size_changed;
  808. enum determine_dev_size rv = DS_UNCHANGED;
  809. /* We may change the on-disk offsets of our meta data below. Lock out
  810. * anything that may cause meta data IO, to avoid acting on incomplete
  811. * layout changes or scribbling over meta data that is in the process
  812. * of being moved.
  813. *
  814. * Move is not exactly correct, btw, currently we have all our meta
  815. * data in core memory, to "move" it we just write it all out, there
  816. * are no reads. */
  817. drbd_suspend_io(device);
  818. buffer = drbd_md_get_buffer(device, __func__); /* Lock meta-data IO */
  819. if (!buffer) {
  820. drbd_resume_io(device);
  821. return DS_ERROR;
  822. }
  823. /* remember current offset and sizes */
  824. prev.last_agreed_sect = md->la_size_sect;
  825. prev.md_offset = md->md_offset;
  826. prev.al_offset = md->al_offset;
  827. prev.bm_offset = md->bm_offset;
  828. prev.md_size_sect = md->md_size_sect;
  829. prev.al_stripes = md->al_stripes;
  830. prev.al_stripe_size_4k = md->al_stripe_size_4k;
  831. if (rs) {
  832. /* rs is non NULL if we should change the AL layout only */
  833. md->al_stripes = rs->al_stripes;
  834. md->al_stripe_size_4k = rs->al_stripe_size / 4;
  835. md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
  836. }
  837. drbd_md_set_sector_offsets(device, device->ldev);
  838. rcu_read_lock();
  839. u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
  840. rcu_read_unlock();
  841. size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
  842. if (size < prev.last_agreed_sect) {
  843. if (rs && u_size == 0) {
  844. /* Remove "rs &&" later. This check should always be active, but
  845. right now the receiver expects the permissive behavior */
  846. drbd_warn(device, "Implicit shrink not allowed. "
  847. "Use --size=%llus for explicit shrink.\n",
  848. (unsigned long long)size);
  849. rv = DS_ERROR_SHRINK;
  850. }
  851. if (u_size > size)
  852. rv = DS_ERROR_SPACE_MD;
  853. if (rv != DS_UNCHANGED)
  854. goto err_out;
  855. }
  856. if (drbd_get_capacity(device->this_bdev) != size ||
  857. drbd_bm_capacity(device) != size) {
  858. int err;
  859. err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
  860. if (unlikely(err)) {
  861. /* currently there is only one error: ENOMEM! */
  862. size = drbd_bm_capacity(device);
  863. if (size == 0) {
  864. drbd_err(device, "OUT OF MEMORY! "
  865. "Could not allocate bitmap!\n");
  866. } else {
  867. drbd_err(device, "BM resizing failed. "
  868. "Leaving size unchanged\n");
  869. }
  870. rv = DS_ERROR;
  871. }
  872. /* racy, see comments above. */
  873. drbd_set_my_capacity(device, size);
  874. md->la_size_sect = size;
  875. drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
  876. (unsigned long long)size>>1);
  877. }
  878. if (rv <= DS_ERROR)
  879. goto err_out;
  880. la_size_changed = (prev.last_agreed_sect != md->la_size_sect);
  881. md_moved = prev.md_offset != md->md_offset
  882. || prev.md_size_sect != md->md_size_sect;
  883. if (la_size_changed || md_moved || rs) {
  884. u32 prev_flags;
  885. /* We do some synchronous IO below, which may take some time.
  886. * Clear the timer, to avoid scary "timer expired!" messages,
  887. * "Superblock" is written out at least twice below, anyways. */
  888. del_timer(&device->md_sync_timer);
  889. /* We won't change the "al-extents" setting, we just may need
  890. * to move the on-disk location of the activity log ringbuffer.
  891. * Lock for transaction is good enough, it may well be "dirty"
  892. * or even "starving". */
  893. wait_event(device->al_wait, lc_try_lock_for_transaction(device->act_log));
  894. /* mark current on-disk bitmap and activity log as unreliable */
  895. prev_flags = md->flags;
  896. md->flags |= MDF_FULL_SYNC | MDF_AL_DISABLED;
  897. drbd_md_write(device, buffer);
  898. drbd_al_initialize(device, buffer);
  899. drbd_info(device, "Writing the whole bitmap, %s\n",
  900. la_size_changed && md_moved ? "size changed and md moved" :
  901. la_size_changed ? "size changed" : "md moved");
  902. /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
  903. drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
  904. "size changed", BM_LOCKED_MASK);
  905. /* on-disk bitmap and activity log is authoritative again
  906. * (unless there was an IO error meanwhile...) */
  907. md->flags = prev_flags;
  908. drbd_md_write(device, buffer);
  909. if (rs)
  910. drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
  911. md->al_stripes, md->al_stripe_size_4k * 4);
  912. }
  913. if (size > prev.last_agreed_sect)
  914. rv = prev.last_agreed_sect ? DS_GREW : DS_GREW_FROM_ZERO;
  915. if (size < prev.last_agreed_sect)
  916. rv = DS_SHRUNK;
  917. if (0) {
  918. err_out:
  919. /* restore previous offset and sizes */
  920. md->la_size_sect = prev.last_agreed_sect;
  921. md->md_offset = prev.md_offset;
  922. md->al_offset = prev.al_offset;
  923. md->bm_offset = prev.bm_offset;
  924. md->md_size_sect = prev.md_size_sect;
  925. md->al_stripes = prev.al_stripes;
  926. md->al_stripe_size_4k = prev.al_stripe_size_4k;
  927. md->al_size_4k = (u64)prev.al_stripes * prev.al_stripe_size_4k;
  928. }
  929. lc_unlock(device->act_log);
  930. wake_up(&device->al_wait);
  931. drbd_md_put_buffer(device);
  932. drbd_resume_io(device);
  933. return rv;
  934. }
  935. sector_t
  936. drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
  937. sector_t u_size, int assume_peer_has_space)
  938. {
  939. sector_t p_size = device->p_size; /* partner's disk size. */
  940. sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
  941. sector_t m_size; /* my size */
  942. sector_t size = 0;
  943. m_size = drbd_get_max_capacity(bdev);
  944. if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
  945. drbd_warn(device, "Resize while not connected was forced by the user!\n");
  946. p_size = m_size;
  947. }
  948. if (p_size && m_size) {
  949. size = min_t(sector_t, p_size, m_size);
  950. } else {
  951. if (la_size_sect) {
  952. size = la_size_sect;
  953. if (m_size && m_size < size)
  954. size = m_size;
  955. if (p_size && p_size < size)
  956. size = p_size;
  957. } else {
  958. if (m_size)
  959. size = m_size;
  960. if (p_size)
  961. size = p_size;
  962. }
  963. }
  964. if (size == 0)
  965. drbd_err(device, "Both nodes diskless!\n");
  966. if (u_size) {
  967. if (u_size > size)
  968. drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
  969. (unsigned long)u_size>>1, (unsigned long)size>>1);
  970. else
  971. size = u_size;
  972. }
  973. return size;
  974. }
  975. /**
  976. * drbd_check_al_size() - Ensures that the AL is of the right size
  977. * @device: DRBD device.
  978. *
  979. * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
  980. * failed, and 0 on success. You should call drbd_md_sync() after you called
  981. * this function.
  982. */
  983. static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
  984. {
  985. struct lru_cache *n, *t;
  986. struct lc_element *e;
  987. unsigned int in_use;
  988. int i;
  989. if (device->act_log &&
  990. device->act_log->nr_elements == dc->al_extents)
  991. return 0;
  992. in_use = 0;
  993. t = device->act_log;
  994. n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
  995. dc->al_extents, sizeof(struct lc_element), 0);
  996. if (n == NULL) {
  997. drbd_err(device, "Cannot allocate act_log lru!\n");
  998. return -ENOMEM;
  999. }
  1000. spin_lock_irq(&device->al_lock);
  1001. if (t) {
  1002. for (i = 0; i < t->nr_elements; i++) {
  1003. e = lc_element_by_index(t, i);
  1004. if (e->refcnt)
  1005. drbd_err(device, "refcnt(%d)==%d\n",
  1006. e->lc_number, e->refcnt);
  1007. in_use += e->refcnt;
  1008. }
  1009. }
  1010. if (!in_use)
  1011. device->act_log = n;
  1012. spin_unlock_irq(&device->al_lock);
  1013. if (in_use) {
  1014. drbd_err(device, "Activity log still in use!\n");
  1015. lc_destroy(n);
  1016. return -EBUSY;
  1017. } else {
  1018. lc_destroy(t);
  1019. }
  1020. drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
  1021. return 0;
  1022. }
  1023. static void blk_queue_discard_granularity(struct request_queue *q, unsigned int granularity)
  1024. {
  1025. q->limits.discard_granularity = granularity;
  1026. }
  1027. static unsigned int drbd_max_discard_sectors(struct drbd_connection *connection)
  1028. {
  1029. /* when we introduced REQ_WRITE_SAME support, we also bumped
  1030. * our maximum supported batch bio size used for discards. */
  1031. if (connection->agreed_features & DRBD_FF_WSAME)
  1032. return DRBD_MAX_BBIO_SECTORS;
  1033. /* before, with DRBD <= 8.4.6, we only allowed up to one AL_EXTENT_SIZE. */
  1034. return AL_EXTENT_SIZE >> 9;
  1035. }
  1036. static void decide_on_discard_support(struct drbd_device *device,
  1037. struct request_queue *q,
  1038. struct request_queue *b,
  1039. bool discard_zeroes_if_aligned)
  1040. {
  1041. /* q = drbd device queue (device->rq_queue)
  1042. * b = backing device queue (device->ldev->backing_bdev->bd_disk->queue),
  1043. * or NULL if diskless
  1044. */
  1045. struct drbd_connection *connection = first_peer_device(device)->connection;
  1046. bool can_do = b ? blk_queue_discard(b) : true;
  1047. if (can_do && b && !b->limits.discard_zeroes_data && !discard_zeroes_if_aligned) {
  1048. can_do = false;
  1049. drbd_info(device, "discard_zeroes_data=0 and discard_zeroes_if_aligned=no: disabling discards\n");
  1050. }
  1051. if (can_do && connection->cstate >= C_CONNECTED && !(connection->agreed_features & DRBD_FF_TRIM)) {
  1052. can_do = false;
  1053. drbd_info(connection, "peer DRBD too old, does not support TRIM: disabling discards\n");
  1054. }
  1055. if (can_do) {
  1056. /* We don't care for the granularity, really.
  1057. * Stacking limits below should fix it for the local
  1058. * device. Whether or not it is a suitable granularity
  1059. * on the remote device is not our problem, really. If
  1060. * you care, you need to use devices with similar
  1061. * topology on all peers. */
  1062. blk_queue_discard_granularity(q, 512);
  1063. q->limits.max_discard_sectors = drbd_max_discard_sectors(connection);
  1064. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  1065. } else {
  1066. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  1067. blk_queue_discard_granularity(q, 0);
  1068. q->limits.max_discard_sectors = 0;
  1069. }
  1070. }
  1071. static void fixup_discard_if_not_supported(struct request_queue *q)
  1072. {
  1073. /* To avoid confusion, if this queue does not support discard, clear
  1074. * max_discard_sectors, which is what lsblk -D reports to the user.
  1075. * Older kernels got this wrong in "stack limits".
  1076. * */
  1077. if (!blk_queue_discard(q)) {
  1078. blk_queue_max_discard_sectors(q, 0);
  1079. blk_queue_discard_granularity(q, 0);
  1080. }
  1081. }
  1082. static void decide_on_write_same_support(struct drbd_device *device,
  1083. struct request_queue *q,
  1084. struct request_queue *b, struct o_qlim *o)
  1085. {
  1086. struct drbd_peer_device *peer_device = first_peer_device(device);
  1087. struct drbd_connection *connection = peer_device->connection;
  1088. bool can_do = b ? b->limits.max_write_same_sectors : true;
  1089. if (can_do && connection->cstate >= C_CONNECTED && !(connection->agreed_features & DRBD_FF_WSAME)) {
  1090. can_do = false;
  1091. drbd_info(peer_device, "peer does not support WRITE_SAME\n");
  1092. }
  1093. if (o) {
  1094. /* logical block size; queue_logical_block_size(NULL) is 512 */
  1095. unsigned int peer_lbs = be32_to_cpu(o->logical_block_size);
  1096. unsigned int me_lbs_b = queue_logical_block_size(b);
  1097. unsigned int me_lbs = queue_logical_block_size(q);
  1098. if (me_lbs_b != me_lbs) {
  1099. drbd_warn(device,
  1100. "logical block size of local backend does not match (drbd:%u, backend:%u); was this a late attach?\n",
  1101. me_lbs, me_lbs_b);
  1102. /* rather disable write same than trigger some BUG_ON later in the scsi layer. */
  1103. can_do = false;
  1104. }
  1105. if (me_lbs_b != peer_lbs) {
  1106. drbd_warn(peer_device, "logical block sizes do not match (me:%u, peer:%u); this may cause problems.\n",
  1107. me_lbs, peer_lbs);
  1108. if (can_do) {
  1109. drbd_dbg(peer_device, "logical block size mismatch: WRITE_SAME disabled.\n");
  1110. can_do = false;
  1111. }
  1112. me_lbs = max(me_lbs, me_lbs_b);
  1113. /* We cannot change the logical block size of an in-use queue.
  1114. * We can only hope that access happens to be properly aligned.
  1115. * If not, the peer will likely produce an IO error, and detach. */
  1116. if (peer_lbs > me_lbs) {
  1117. if (device->state.role != R_PRIMARY) {
  1118. blk_queue_logical_block_size(q, peer_lbs);
  1119. drbd_warn(peer_device, "logical block size set to %u\n", peer_lbs);
  1120. } else {
  1121. drbd_warn(peer_device,
  1122. "current Primary must NOT adjust logical block size (%u -> %u); hope for the best.\n",
  1123. me_lbs, peer_lbs);
  1124. }
  1125. }
  1126. }
  1127. if (can_do && !o->write_same_capable) {
  1128. /* If we introduce an open-coded write-same loop on the receiving side,
  1129. * the peer would present itself as "capable". */
  1130. drbd_dbg(peer_device, "WRITE_SAME disabled (peer device not capable)\n");
  1131. can_do = false;
  1132. }
  1133. }
  1134. blk_queue_max_write_same_sectors(q, can_do ? DRBD_MAX_BBIO_SECTORS : 0);
  1135. }
  1136. static void drbd_setup_queue_param(struct drbd_device *device, struct drbd_backing_dev *bdev,
  1137. unsigned int max_bio_size, struct o_qlim *o)
  1138. {
  1139. struct request_queue * const q = device->rq_queue;
  1140. unsigned int max_hw_sectors = max_bio_size >> 9;
  1141. unsigned int max_segments = 0;
  1142. struct request_queue *b = NULL;
  1143. struct disk_conf *dc;
  1144. bool discard_zeroes_if_aligned = true;
  1145. if (bdev) {
  1146. b = bdev->backing_bdev->bd_disk->queue;
  1147. max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
  1148. rcu_read_lock();
  1149. dc = rcu_dereference(device->ldev->disk_conf);
  1150. max_segments = dc->max_bio_bvecs;
  1151. discard_zeroes_if_aligned = dc->discard_zeroes_if_aligned;
  1152. rcu_read_unlock();
  1153. blk_set_stacking_limits(&q->limits);
  1154. }
  1155. blk_queue_max_hw_sectors(q, max_hw_sectors);
  1156. /* This is the workaround for "bio would need to, but cannot, be split" */
  1157. blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
  1158. blk_queue_segment_boundary(q, PAGE_SIZE-1);
  1159. decide_on_discard_support(device, q, b, discard_zeroes_if_aligned);
  1160. decide_on_write_same_support(device, q, b, o);
  1161. if (b) {
  1162. blk_queue_stack_limits(q, b);
  1163. if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
  1164. drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
  1165. q->backing_dev_info.ra_pages,
  1166. b->backing_dev_info.ra_pages);
  1167. q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
  1168. }
  1169. }
  1170. fixup_discard_if_not_supported(q);
  1171. }
  1172. void drbd_reconsider_queue_parameters(struct drbd_device *device, struct drbd_backing_dev *bdev, struct o_qlim *o)
  1173. {
  1174. unsigned int now, new, local, peer;
  1175. now = queue_max_hw_sectors(device->rq_queue) << 9;
  1176. local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
  1177. peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
  1178. if (bdev) {
  1179. local = queue_max_hw_sectors(bdev->backing_bdev->bd_disk->queue) << 9;
  1180. device->local_max_bio_size = local;
  1181. }
  1182. local = min(local, DRBD_MAX_BIO_SIZE);
  1183. /* We may ignore peer limits if the peer is modern enough.
  1184. Because new from 8.3.8 onwards the peer can use multiple
  1185. BIOs for a single peer_request */
  1186. if (device->state.conn >= C_WF_REPORT_PARAMS) {
  1187. if (first_peer_device(device)->connection->agreed_pro_version < 94)
  1188. peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
  1189. /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
  1190. else if (first_peer_device(device)->connection->agreed_pro_version == 94)
  1191. peer = DRBD_MAX_SIZE_H80_PACKET;
  1192. else if (first_peer_device(device)->connection->agreed_pro_version < 100)
  1193. peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
  1194. else
  1195. peer = DRBD_MAX_BIO_SIZE;
  1196. /* We may later detach and re-attach on a disconnected Primary.
  1197. * Avoid this setting to jump back in that case.
  1198. * We want to store what we know the peer DRBD can handle,
  1199. * not what the peer IO backend can handle. */
  1200. if (peer > device->peer_max_bio_size)
  1201. device->peer_max_bio_size = peer;
  1202. }
  1203. new = min(local, peer);
  1204. if (device->state.role == R_PRIMARY && new < now)
  1205. drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
  1206. if (new != now)
  1207. drbd_info(device, "max BIO size = %u\n", new);
  1208. drbd_setup_queue_param(device, bdev, new, o);
  1209. }
  1210. /* Starts the worker thread */
  1211. static void conn_reconfig_start(struct drbd_connection *connection)
  1212. {
  1213. drbd_thread_start(&connection->worker);
  1214. drbd_flush_workqueue(&connection->sender_work);
  1215. }
  1216. /* if still unconfigured, stops worker again. */
  1217. static void conn_reconfig_done(struct drbd_connection *connection)
  1218. {
  1219. bool stop_threads;
  1220. spin_lock_irq(&connection->resource->req_lock);
  1221. stop_threads = conn_all_vols_unconf(connection) &&
  1222. connection->cstate == C_STANDALONE;
  1223. spin_unlock_irq(&connection->resource->req_lock);
  1224. if (stop_threads) {
  1225. /* ack_receiver thread and ack_sender workqueue are implicitly
  1226. * stopped by receiver in conn_disconnect() */
  1227. drbd_thread_stop(&connection->receiver);
  1228. drbd_thread_stop(&connection->worker);
  1229. }
  1230. }
  1231. /* Make sure IO is suspended before calling this function(). */
  1232. static void drbd_suspend_al(struct drbd_device *device)
  1233. {
  1234. int s = 0;
  1235. if (!lc_try_lock(device->act_log)) {
  1236. drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
  1237. return;
  1238. }
  1239. drbd_al_shrink(device);
  1240. spin_lock_irq(&device->resource->req_lock);
  1241. if (device->state.conn < C_CONNECTED)
  1242. s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
  1243. spin_unlock_irq(&device->resource->req_lock);
  1244. lc_unlock(device->act_log);
  1245. if (s)
  1246. drbd_info(device, "Suspended AL updates\n");
  1247. }
  1248. static bool should_set_defaults(struct genl_info *info)
  1249. {
  1250. unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
  1251. return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
  1252. }
  1253. static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
  1254. {
  1255. /* This is limited by 16 bit "slot" numbers,
  1256. * and by available on-disk context storage.
  1257. *
  1258. * Also (u16)~0 is special (denotes a "free" extent).
  1259. *
  1260. * One transaction occupies one 4kB on-disk block,
  1261. * we have n such blocks in the on disk ring buffer,
  1262. * the "current" transaction may fail (n-1),
  1263. * and there is 919 slot numbers context information per transaction.
  1264. *
  1265. * 72 transaction blocks amounts to more than 2**16 context slots,
  1266. * so cap there first.
  1267. */
  1268. const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
  1269. const unsigned int sufficient_on_disk =
  1270. (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
  1271. /AL_CONTEXT_PER_TRANSACTION;
  1272. unsigned int al_size_4k = bdev->md.al_size_4k;
  1273. if (al_size_4k > sufficient_on_disk)
  1274. return max_al_nr;
  1275. return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
  1276. }
  1277. static bool write_ordering_changed(struct disk_conf *a, struct disk_conf *b)
  1278. {
  1279. return a->disk_barrier != b->disk_barrier ||
  1280. a->disk_flushes != b->disk_flushes ||
  1281. a->disk_drain != b->disk_drain;
  1282. }
  1283. static void sanitize_disk_conf(struct drbd_device *device, struct disk_conf *disk_conf,
  1284. struct drbd_backing_dev *nbc)
  1285. {
  1286. struct request_queue * const q = nbc->backing_bdev->bd_disk->queue;
  1287. if (disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
  1288. disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
  1289. if (disk_conf->al_extents > drbd_al_extents_max(nbc))
  1290. disk_conf->al_extents = drbd_al_extents_max(nbc);
  1291. if (!blk_queue_discard(q)
  1292. || (!q->limits.discard_zeroes_data && !disk_conf->discard_zeroes_if_aligned)) {
  1293. if (disk_conf->rs_discard_granularity) {
  1294. disk_conf->rs_discard_granularity = 0; /* disable feature */
  1295. drbd_info(device, "rs_discard_granularity feature disabled\n");
  1296. }
  1297. }
  1298. if (disk_conf->rs_discard_granularity) {
  1299. int orig_value = disk_conf->rs_discard_granularity;
  1300. int remainder;
  1301. if (q->limits.discard_granularity > disk_conf->rs_discard_granularity)
  1302. disk_conf->rs_discard_granularity = q->limits.discard_granularity;
  1303. remainder = disk_conf->rs_discard_granularity % q->limits.discard_granularity;
  1304. disk_conf->rs_discard_granularity += remainder;
  1305. if (disk_conf->rs_discard_granularity > q->limits.max_discard_sectors << 9)
  1306. disk_conf->rs_discard_granularity = q->limits.max_discard_sectors << 9;
  1307. if (disk_conf->rs_discard_granularity != orig_value)
  1308. drbd_info(device, "rs_discard_granularity changed to %d\n",
  1309. disk_conf->rs_discard_granularity);
  1310. }
  1311. }
  1312. int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
  1313. {
  1314. struct drbd_config_context adm_ctx;
  1315. enum drbd_ret_code retcode;
  1316. struct drbd_device *device;
  1317. struct disk_conf *new_disk_conf, *old_disk_conf;
  1318. struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
  1319. int err, fifo_size;
  1320. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  1321. if (!adm_ctx.reply_skb)
  1322. return retcode;
  1323. if (retcode != NO_ERROR)
  1324. goto finish;
  1325. device = adm_ctx.device;
  1326. mutex_lock(&adm_ctx.resource->adm_mutex);
  1327. /* we also need a disk
  1328. * to change the options on */
  1329. if (!get_ldev(device)) {
  1330. retcode = ERR_NO_DISK;
  1331. goto out;
  1332. }
  1333. new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
  1334. if (!new_disk_conf) {
  1335. retcode = ERR_NOMEM;
  1336. goto fail;
  1337. }
  1338. mutex_lock(&device->resource->conf_update);
  1339. old_disk_conf = device->ldev->disk_conf;
  1340. *new_disk_conf = *old_disk_conf;
  1341. if (should_set_defaults(info))
  1342. set_disk_conf_defaults(new_disk_conf);
  1343. err = disk_conf_from_attrs_for_change(new_disk_conf, info);
  1344. if (err && err != -ENOMSG) {
  1345. retcode = ERR_MANDATORY_TAG;
  1346. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  1347. goto fail_unlock;
  1348. }
  1349. if (!expect(new_disk_conf->resync_rate >= 1))
  1350. new_disk_conf->resync_rate = 1;
  1351. sanitize_disk_conf(device, new_disk_conf, device->ldev);
  1352. if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
  1353. new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
  1354. fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
  1355. if (fifo_size != device->rs_plan_s->size) {
  1356. new_plan = fifo_alloc(fifo_size);
  1357. if (!new_plan) {
  1358. drbd_err(device, "kmalloc of fifo_buffer failed");
  1359. retcode = ERR_NOMEM;
  1360. goto fail_unlock;
  1361. }
  1362. }
  1363. drbd_suspend_io(device);
  1364. wait_event(device->al_wait, lc_try_lock(device->act_log));
  1365. drbd_al_shrink(device);
  1366. err = drbd_check_al_size(device, new_disk_conf);
  1367. lc_unlock(device->act_log);
  1368. wake_up(&device->al_wait);
  1369. drbd_resume_io(device);
  1370. if (err) {
  1371. retcode = ERR_NOMEM;
  1372. goto fail_unlock;
  1373. }
  1374. lock_all_resources();
  1375. retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
  1376. if (retcode == NO_ERROR) {
  1377. rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
  1378. drbd_resync_after_changed(device);
  1379. }
  1380. unlock_all_resources();
  1381. if (retcode != NO_ERROR)
  1382. goto fail_unlock;
  1383. if (new_plan) {
  1384. old_plan = device->rs_plan_s;
  1385. rcu_assign_pointer(device->rs_plan_s, new_plan);
  1386. }
  1387. mutex_unlock(&device->resource->conf_update);
  1388. if (new_disk_conf->al_updates)
  1389. device->ldev->md.flags &= ~MDF_AL_DISABLED;
  1390. else
  1391. device->ldev->md.flags |= MDF_AL_DISABLED;
  1392. if (new_disk_conf->md_flushes)
  1393. clear_bit(MD_NO_FUA, &device->flags);
  1394. else
  1395. set_bit(MD_NO_FUA, &device->flags);
  1396. if (write_ordering_changed(old_disk_conf, new_disk_conf))
  1397. drbd_bump_write_ordering(device->resource, NULL, WO_BDEV_FLUSH);
  1398. if (old_disk_conf->discard_zeroes_if_aligned != new_disk_conf->discard_zeroes_if_aligned)
  1399. drbd_reconsider_queue_parameters(device, device->ldev, NULL);
  1400. drbd_md_sync(device);
  1401. if (device->state.conn >= C_CONNECTED) {
  1402. struct drbd_peer_device *peer_device;
  1403. for_each_peer_device(peer_device, device)
  1404. drbd_send_sync_param(peer_device);
  1405. }
  1406. synchronize_rcu();
  1407. kfree(old_disk_conf);
  1408. kfree(old_plan);
  1409. mod_timer(&device->request_timer, jiffies + HZ);
  1410. goto success;
  1411. fail_unlock:
  1412. mutex_unlock(&device->resource->conf_update);
  1413. fail:
  1414. kfree(new_disk_conf);
  1415. kfree(new_plan);
  1416. success:
  1417. put_ldev(device);
  1418. out:
  1419. mutex_unlock(&adm_ctx.resource->adm_mutex);
  1420. finish:
  1421. drbd_adm_finish(&adm_ctx, info, retcode);
  1422. return 0;
  1423. }
  1424. static struct block_device *open_backing_dev(struct drbd_device *device,
  1425. const char *bdev_path, void *claim_ptr, bool do_bd_link)
  1426. {
  1427. struct block_device *bdev;
  1428. int err = 0;
  1429. bdev = blkdev_get_by_path(bdev_path,
  1430. FMODE_READ | FMODE_WRITE | FMODE_EXCL, claim_ptr);
  1431. if (IS_ERR(bdev)) {
  1432. drbd_err(device, "open(\"%s\") failed with %ld\n",
  1433. bdev_path, PTR_ERR(bdev));
  1434. return bdev;
  1435. }
  1436. if (!do_bd_link)
  1437. return bdev;
  1438. err = bd_link_disk_holder(bdev, device->vdisk);
  1439. if (err) {
  1440. blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1441. drbd_err(device, "bd_link_disk_holder(\"%s\", ...) failed with %d\n",
  1442. bdev_path, err);
  1443. bdev = ERR_PTR(err);
  1444. }
  1445. return bdev;
  1446. }
  1447. static int open_backing_devices(struct drbd_device *device,
  1448. struct disk_conf *new_disk_conf,
  1449. struct drbd_backing_dev *nbc)
  1450. {
  1451. struct block_device *bdev;
  1452. bdev = open_backing_dev(device, new_disk_conf->backing_dev, device, true);
  1453. if (IS_ERR(bdev))
  1454. return ERR_OPEN_DISK;
  1455. nbc->backing_bdev = bdev;
  1456. /*
  1457. * meta_dev_idx >= 0: external fixed size, possibly multiple
  1458. * drbd sharing one meta device. TODO in that case, paranoia
  1459. * check that [md_bdev, meta_dev_idx] is not yet used by some
  1460. * other drbd minor! (if you use drbd.conf + drbdadm, that
  1461. * should check it for you already; but if you don't, or
  1462. * someone fooled it, we need to double check here)
  1463. */
  1464. bdev = open_backing_dev(device, new_disk_conf->meta_dev,
  1465. /* claim ptr: device, if claimed exclusively; shared drbd_m_holder,
  1466. * if potentially shared with other drbd minors */
  1467. (new_disk_conf->meta_dev_idx < 0) ? (void*)device : (void*)drbd_m_holder,
  1468. /* avoid double bd_claim_by_disk() for the same (source,target) tuple,
  1469. * as would happen with internal metadata. */
  1470. (new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_FLEX_INT &&
  1471. new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_INTERNAL));
  1472. if (IS_ERR(bdev))
  1473. return ERR_OPEN_MD_DISK;
  1474. nbc->md_bdev = bdev;
  1475. return NO_ERROR;
  1476. }
  1477. static void close_backing_dev(struct drbd_device *device, struct block_device *bdev,
  1478. bool do_bd_unlink)
  1479. {
  1480. if (!bdev)
  1481. return;
  1482. if (do_bd_unlink)
  1483. bd_unlink_disk_holder(bdev, device->vdisk);
  1484. blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1485. }
  1486. void drbd_backing_dev_free(struct drbd_device *device, struct drbd_backing_dev *ldev)
  1487. {
  1488. if (ldev == NULL)
  1489. return;
  1490. close_backing_dev(device, ldev->md_bdev, ldev->md_bdev != ldev->backing_bdev);
  1491. close_backing_dev(device, ldev->backing_bdev, true);
  1492. kfree(ldev->disk_conf);
  1493. kfree(ldev);
  1494. }
  1495. int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
  1496. {
  1497. struct drbd_config_context adm_ctx;
  1498. struct drbd_device *device;
  1499. struct drbd_peer_device *peer_device;
  1500. struct drbd_connection *connection;
  1501. int err;
  1502. enum drbd_ret_code retcode;
  1503. enum determine_dev_size dd;
  1504. sector_t max_possible_sectors;
  1505. sector_t min_md_device_sectors;
  1506. struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
  1507. struct disk_conf *new_disk_conf = NULL;
  1508. struct lru_cache *resync_lru = NULL;
  1509. struct fifo_buffer *new_plan = NULL;
  1510. union drbd_state ns, os;
  1511. enum drbd_state_rv rv;
  1512. struct net_conf *nc;
  1513. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  1514. if (!adm_ctx.reply_skb)
  1515. return retcode;
  1516. if (retcode != NO_ERROR)
  1517. goto finish;
  1518. device = adm_ctx.device;
  1519. mutex_lock(&adm_ctx.resource->adm_mutex);
  1520. peer_device = first_peer_device(device);
  1521. connection = peer_device->connection;
  1522. conn_reconfig_start(connection);
  1523. /* if you want to reconfigure, please tear down first */
  1524. if (device->state.disk > D_DISKLESS) {
  1525. retcode = ERR_DISK_CONFIGURED;
  1526. goto fail;
  1527. }
  1528. /* It may just now have detached because of IO error. Make sure
  1529. * drbd_ldev_destroy is done already, we may end up here very fast,
  1530. * e.g. if someone calls attach from the on-io-error handler,
  1531. * to realize a "hot spare" feature (not that I'd recommend that) */
  1532. wait_event(device->misc_wait, !test_bit(GOING_DISKLESS, &device->flags));
  1533. /* make sure there is no leftover from previous force-detach attempts */
  1534. clear_bit(FORCE_DETACH, &device->flags);
  1535. clear_bit(WAS_IO_ERROR, &device->flags);
  1536. clear_bit(WAS_READ_ERROR, &device->flags);
  1537. /* and no leftover from previously aborted resync or verify, either */
  1538. device->rs_total = 0;
  1539. device->rs_failed = 0;
  1540. atomic_set(&device->rs_pending_cnt, 0);
  1541. /* allocation not in the IO path, drbdsetup context */
  1542. nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
  1543. if (!nbc) {
  1544. retcode = ERR_NOMEM;
  1545. goto fail;
  1546. }
  1547. spin_lock_init(&nbc->md.uuid_lock);
  1548. new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
  1549. if (!new_disk_conf) {
  1550. retcode = ERR_NOMEM;
  1551. goto fail;
  1552. }
  1553. nbc->disk_conf = new_disk_conf;
  1554. set_disk_conf_defaults(new_disk_conf);
  1555. err = disk_conf_from_attrs(new_disk_conf, info);
  1556. if (err) {
  1557. retcode = ERR_MANDATORY_TAG;
  1558. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  1559. goto fail;
  1560. }
  1561. if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
  1562. new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
  1563. new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
  1564. if (!new_plan) {
  1565. retcode = ERR_NOMEM;
  1566. goto fail;
  1567. }
  1568. if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
  1569. retcode = ERR_MD_IDX_INVALID;
  1570. goto fail;
  1571. }
  1572. rcu_read_lock();
  1573. nc = rcu_dereference(connection->net_conf);
  1574. if (nc) {
  1575. if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
  1576. rcu_read_unlock();
  1577. retcode = ERR_STONITH_AND_PROT_A;
  1578. goto fail;
  1579. }
  1580. }
  1581. rcu_read_unlock();
  1582. retcode = open_backing_devices(device, new_disk_conf, nbc);
  1583. if (retcode != NO_ERROR)
  1584. goto fail;
  1585. if ((nbc->backing_bdev == nbc->md_bdev) !=
  1586. (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
  1587. new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
  1588. retcode = ERR_MD_IDX_INVALID;
  1589. goto fail;
  1590. }
  1591. resync_lru = lc_create("resync", drbd_bm_ext_cache,
  1592. 1, 61, sizeof(struct bm_extent),
  1593. offsetof(struct bm_extent, lce));
  1594. if (!resync_lru) {
  1595. retcode = ERR_NOMEM;
  1596. goto fail;
  1597. }
  1598. /* Read our meta data super block early.
  1599. * This also sets other on-disk offsets. */
  1600. retcode = drbd_md_read(device, nbc);
  1601. if (retcode != NO_ERROR)
  1602. goto fail;
  1603. sanitize_disk_conf(device, new_disk_conf, nbc);
  1604. if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
  1605. drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
  1606. (unsigned long long) drbd_get_max_capacity(nbc),
  1607. (unsigned long long) new_disk_conf->disk_size);
  1608. retcode = ERR_DISK_TOO_SMALL;
  1609. goto fail;
  1610. }
  1611. if (new_disk_conf->meta_dev_idx < 0) {
  1612. max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
  1613. /* at least one MB, otherwise it does not make sense */
  1614. min_md_device_sectors = (2<<10);
  1615. } else {
  1616. max_possible_sectors = DRBD_MAX_SECTORS;
  1617. min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
  1618. }
  1619. if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
  1620. retcode = ERR_MD_DISK_TOO_SMALL;
  1621. drbd_warn(device, "refusing attach: md-device too small, "
  1622. "at least %llu sectors needed for this meta-disk type\n",
  1623. (unsigned long long) min_md_device_sectors);
  1624. goto fail;
  1625. }
  1626. /* Make sure the new disk is big enough
  1627. * (we may currently be R_PRIMARY with no local disk...) */
  1628. if (drbd_get_max_capacity(nbc) <
  1629. drbd_get_capacity(device->this_bdev)) {
  1630. retcode = ERR_DISK_TOO_SMALL;
  1631. goto fail;
  1632. }
  1633. nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
  1634. if (nbc->known_size > max_possible_sectors) {
  1635. drbd_warn(device, "==> truncating very big lower level device "
  1636. "to currently maximum possible %llu sectors <==\n",
  1637. (unsigned long long) max_possible_sectors);
  1638. if (new_disk_conf->meta_dev_idx >= 0)
  1639. drbd_warn(device, "==>> using internal or flexible "
  1640. "meta data may help <<==\n");
  1641. }
  1642. drbd_suspend_io(device);
  1643. /* also wait for the last barrier ack. */
  1644. /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
  1645. * We need a way to either ignore barrier acks for barriers sent before a device
  1646. * was attached, or a way to wait for all pending barrier acks to come in.
  1647. * As barriers are counted per resource,
  1648. * we'd need to suspend io on all devices of a resource.
  1649. */
  1650. wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
  1651. /* and for any other previously queued work */
  1652. drbd_flush_workqueue(&connection->sender_work);
  1653. rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
  1654. retcode = rv; /* FIXME: Type mismatch. */
  1655. drbd_resume_io(device);
  1656. if (rv < SS_SUCCESS)
  1657. goto fail;
  1658. if (!get_ldev_if_state(device, D_ATTACHING))
  1659. goto force_diskless;
  1660. if (!device->bitmap) {
  1661. if (drbd_bm_init(device)) {
  1662. retcode = ERR_NOMEM;
  1663. goto force_diskless_dec;
  1664. }
  1665. }
  1666. if (device->state.conn < C_CONNECTED &&
  1667. device->state.role == R_PRIMARY && device->ed_uuid &&
  1668. (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
  1669. drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
  1670. (unsigned long long)device->ed_uuid);
  1671. retcode = ERR_DATA_NOT_CURRENT;
  1672. goto force_diskless_dec;
  1673. }
  1674. /* Since we are diskless, fix the activity log first... */
  1675. if (drbd_check_al_size(device, new_disk_conf)) {
  1676. retcode = ERR_NOMEM;
  1677. goto force_diskless_dec;
  1678. }
  1679. /* Prevent shrinking of consistent devices ! */
  1680. if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
  1681. drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
  1682. drbd_warn(device, "refusing to truncate a consistent device\n");
  1683. retcode = ERR_DISK_TOO_SMALL;
  1684. goto force_diskless_dec;
  1685. }
  1686. lock_all_resources();
  1687. retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
  1688. if (retcode != NO_ERROR) {
  1689. unlock_all_resources();
  1690. goto force_diskless_dec;
  1691. }
  1692. /* Reset the "barriers don't work" bits here, then force meta data to
  1693. * be written, to ensure we determine if barriers are supported. */
  1694. if (new_disk_conf->md_flushes)
  1695. clear_bit(MD_NO_FUA, &device->flags);
  1696. else
  1697. set_bit(MD_NO_FUA, &device->flags);
  1698. /* Point of no return reached.
  1699. * Devices and memory are no longer released by error cleanup below.
  1700. * now device takes over responsibility, and the state engine should
  1701. * clean it up somewhere. */
  1702. D_ASSERT(device, device->ldev == NULL);
  1703. device->ldev = nbc;
  1704. device->resync = resync_lru;
  1705. device->rs_plan_s = new_plan;
  1706. nbc = NULL;
  1707. resync_lru = NULL;
  1708. new_disk_conf = NULL;
  1709. new_plan = NULL;
  1710. drbd_resync_after_changed(device);
  1711. drbd_bump_write_ordering(device->resource, device->ldev, WO_BDEV_FLUSH);
  1712. unlock_all_resources();
  1713. if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
  1714. set_bit(CRASHED_PRIMARY, &device->flags);
  1715. else
  1716. clear_bit(CRASHED_PRIMARY, &device->flags);
  1717. if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
  1718. !(device->state.role == R_PRIMARY && device->resource->susp_nod))
  1719. set_bit(CRASHED_PRIMARY, &device->flags);
  1720. device->send_cnt = 0;
  1721. device->recv_cnt = 0;
  1722. device->read_cnt = 0;
  1723. device->writ_cnt = 0;
  1724. drbd_reconsider_queue_parameters(device, device->ldev, NULL);
  1725. /* If I am currently not R_PRIMARY,
  1726. * but meta data primary indicator is set,
  1727. * I just now recover from a hard crash,
  1728. * and have been R_PRIMARY before that crash.
  1729. *
  1730. * Now, if I had no connection before that crash
  1731. * (have been degraded R_PRIMARY), chances are that
  1732. * I won't find my peer now either.
  1733. *
  1734. * In that case, and _only_ in that case,
  1735. * we use the degr-wfc-timeout instead of the default,
  1736. * so we can automatically recover from a crash of a
  1737. * degraded but active "cluster" after a certain timeout.
  1738. */
  1739. clear_bit(USE_DEGR_WFC_T, &device->flags);
  1740. if (device->state.role != R_PRIMARY &&
  1741. drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
  1742. !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
  1743. set_bit(USE_DEGR_WFC_T, &device->flags);
  1744. dd = drbd_determine_dev_size(device, 0, NULL);
  1745. if (dd <= DS_ERROR) {
  1746. retcode = ERR_NOMEM_BITMAP;
  1747. goto force_diskless_dec;
  1748. } else if (dd == DS_GREW)
  1749. set_bit(RESYNC_AFTER_NEG, &device->flags);
  1750. if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
  1751. (test_bit(CRASHED_PRIMARY, &device->flags) &&
  1752. drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
  1753. drbd_info(device, "Assuming that all blocks are out of sync "
  1754. "(aka FullSync)\n");
  1755. if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
  1756. "set_n_write from attaching", BM_LOCKED_MASK)) {
  1757. retcode = ERR_IO_MD_DISK;
  1758. goto force_diskless_dec;
  1759. }
  1760. } else {
  1761. if (drbd_bitmap_io(device, &drbd_bm_read,
  1762. "read from attaching", BM_LOCKED_MASK)) {
  1763. retcode = ERR_IO_MD_DISK;
  1764. goto force_diskless_dec;
  1765. }
  1766. }
  1767. if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
  1768. drbd_suspend_al(device); /* IO is still suspended here... */
  1769. spin_lock_irq(&device->resource->req_lock);
  1770. os = drbd_read_state(device);
  1771. ns = os;
  1772. /* If MDF_CONSISTENT is not set go into inconsistent state,
  1773. otherwise investigate MDF_WasUpToDate...
  1774. If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
  1775. otherwise into D_CONSISTENT state.
  1776. */
  1777. if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
  1778. if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
  1779. ns.disk = D_CONSISTENT;
  1780. else
  1781. ns.disk = D_OUTDATED;
  1782. } else {
  1783. ns.disk = D_INCONSISTENT;
  1784. }
  1785. if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
  1786. ns.pdsk = D_OUTDATED;
  1787. rcu_read_lock();
  1788. if (ns.disk == D_CONSISTENT &&
  1789. (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
  1790. ns.disk = D_UP_TO_DATE;
  1791. /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
  1792. MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
  1793. this point, because drbd_request_state() modifies these
  1794. flags. */
  1795. if (rcu_dereference(device->ldev->disk_conf)->al_updates)
  1796. device->ldev->md.flags &= ~MDF_AL_DISABLED;
  1797. else
  1798. device->ldev->md.flags |= MDF_AL_DISABLED;
  1799. rcu_read_unlock();
  1800. /* In case we are C_CONNECTED postpone any decision on the new disk
  1801. state after the negotiation phase. */
  1802. if (device->state.conn == C_CONNECTED) {
  1803. device->new_state_tmp.i = ns.i;
  1804. ns.i = os.i;
  1805. ns.disk = D_NEGOTIATING;
  1806. /* We expect to receive up-to-date UUIDs soon.
  1807. To avoid a race in receive_state, free p_uuid while
  1808. holding req_lock. I.e. atomic with the state change */
  1809. kfree(device->p_uuid);
  1810. device->p_uuid = NULL;
  1811. }
  1812. rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
  1813. spin_unlock_irq(&device->resource->req_lock);
  1814. if (rv < SS_SUCCESS)
  1815. goto force_diskless_dec;
  1816. mod_timer(&device->request_timer, jiffies + HZ);
  1817. if (device->state.role == R_PRIMARY)
  1818. device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
  1819. else
  1820. device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
  1821. drbd_md_mark_dirty(device);
  1822. drbd_md_sync(device);
  1823. kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
  1824. put_ldev(device);
  1825. conn_reconfig_done(connection);
  1826. mutex_unlock(&adm_ctx.resource->adm_mutex);
  1827. drbd_adm_finish(&adm_ctx, info, retcode);
  1828. return 0;
  1829. force_diskless_dec:
  1830. put_ldev(device);
  1831. force_diskless:
  1832. drbd_force_state(device, NS(disk, D_DISKLESS));
  1833. drbd_md_sync(device);
  1834. fail:
  1835. conn_reconfig_done(connection);
  1836. if (nbc) {
  1837. close_backing_dev(device, nbc->md_bdev, nbc->md_bdev != nbc->backing_bdev);
  1838. close_backing_dev(device, nbc->backing_bdev, true);
  1839. kfree(nbc);
  1840. }
  1841. kfree(new_disk_conf);
  1842. lc_destroy(resync_lru);
  1843. kfree(new_plan);
  1844. mutex_unlock(&adm_ctx.resource->adm_mutex);
  1845. finish:
  1846. drbd_adm_finish(&adm_ctx, info, retcode);
  1847. return 0;
  1848. }
  1849. static int adm_detach(struct drbd_device *device, int force)
  1850. {
  1851. enum drbd_state_rv retcode;
  1852. void *buffer;
  1853. int ret;
  1854. if (force) {
  1855. set_bit(FORCE_DETACH, &device->flags);
  1856. drbd_force_state(device, NS(disk, D_FAILED));
  1857. retcode = SS_SUCCESS;
  1858. goto out;
  1859. }
  1860. drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
  1861. buffer = drbd_md_get_buffer(device, __func__); /* make sure there is no in-flight meta-data IO */
  1862. if (buffer) {
  1863. retcode = drbd_request_state(device, NS(disk, D_FAILED));
  1864. drbd_md_put_buffer(device);
  1865. } else /* already <= D_FAILED */
  1866. retcode = SS_NOTHING_TO_DO;
  1867. /* D_FAILED will transition to DISKLESS. */
  1868. drbd_resume_io(device);
  1869. ret = wait_event_interruptible(device->misc_wait,
  1870. device->state.disk != D_FAILED);
  1871. if ((int)retcode == (int)SS_IS_DISKLESS)
  1872. retcode = SS_NOTHING_TO_DO;
  1873. if (ret)
  1874. retcode = ERR_INTR;
  1875. out:
  1876. return retcode;
  1877. }
  1878. /* Detaching the disk is a process in multiple stages. First we need to lock
  1879. * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
  1880. * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
  1881. * internal references as well.
  1882. * Only then we have finally detached. */
  1883. int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
  1884. {
  1885. struct drbd_config_context adm_ctx;
  1886. enum drbd_ret_code retcode;
  1887. struct detach_parms parms = { };
  1888. int err;
  1889. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  1890. if (!adm_ctx.reply_skb)
  1891. return retcode;
  1892. if (retcode != NO_ERROR)
  1893. goto out;
  1894. if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
  1895. err = detach_parms_from_attrs(&parms, info);
  1896. if (err) {
  1897. retcode = ERR_MANDATORY_TAG;
  1898. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  1899. goto out;
  1900. }
  1901. }
  1902. mutex_lock(&adm_ctx.resource->adm_mutex);
  1903. retcode = adm_detach(adm_ctx.device, parms.force_detach);
  1904. mutex_unlock(&adm_ctx.resource->adm_mutex);
  1905. out:
  1906. drbd_adm_finish(&adm_ctx, info, retcode);
  1907. return 0;
  1908. }
  1909. static bool conn_resync_running(struct drbd_connection *connection)
  1910. {
  1911. struct drbd_peer_device *peer_device;
  1912. bool rv = false;
  1913. int vnr;
  1914. rcu_read_lock();
  1915. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  1916. struct drbd_device *device = peer_device->device;
  1917. if (device->state.conn == C_SYNC_SOURCE ||
  1918. device->state.conn == C_SYNC_TARGET ||
  1919. device->state.conn == C_PAUSED_SYNC_S ||
  1920. device->state.conn == C_PAUSED_SYNC_T) {
  1921. rv = true;
  1922. break;
  1923. }
  1924. }
  1925. rcu_read_unlock();
  1926. return rv;
  1927. }
  1928. static bool conn_ov_running(struct drbd_connection *connection)
  1929. {
  1930. struct drbd_peer_device *peer_device;
  1931. bool rv = false;
  1932. int vnr;
  1933. rcu_read_lock();
  1934. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  1935. struct drbd_device *device = peer_device->device;
  1936. if (device->state.conn == C_VERIFY_S ||
  1937. device->state.conn == C_VERIFY_T) {
  1938. rv = true;
  1939. break;
  1940. }
  1941. }
  1942. rcu_read_unlock();
  1943. return rv;
  1944. }
  1945. static enum drbd_ret_code
  1946. _check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
  1947. {
  1948. struct drbd_peer_device *peer_device;
  1949. int i;
  1950. if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
  1951. if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
  1952. return ERR_NEED_APV_100;
  1953. if (new_net_conf->two_primaries != old_net_conf->two_primaries)
  1954. return ERR_NEED_APV_100;
  1955. if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
  1956. return ERR_NEED_APV_100;
  1957. }
  1958. if (!new_net_conf->two_primaries &&
  1959. conn_highest_role(connection) == R_PRIMARY &&
  1960. conn_highest_peer(connection) == R_PRIMARY)
  1961. return ERR_NEED_ALLOW_TWO_PRI;
  1962. if (new_net_conf->two_primaries &&
  1963. (new_net_conf->wire_protocol != DRBD_PROT_C))
  1964. return ERR_NOT_PROTO_C;
  1965. idr_for_each_entry(&connection->peer_devices, peer_device, i) {
  1966. struct drbd_device *device = peer_device->device;
  1967. if (get_ldev(device)) {
  1968. enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
  1969. put_ldev(device);
  1970. if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
  1971. return ERR_STONITH_AND_PROT_A;
  1972. }
  1973. if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
  1974. return ERR_DISCARD_IMPOSSIBLE;
  1975. }
  1976. if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
  1977. return ERR_CONG_NOT_PROTO_A;
  1978. return NO_ERROR;
  1979. }
  1980. static enum drbd_ret_code
  1981. check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
  1982. {
  1983. static enum drbd_ret_code rv;
  1984. struct drbd_peer_device *peer_device;
  1985. int i;
  1986. rcu_read_lock();
  1987. rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
  1988. rcu_read_unlock();
  1989. /* connection->peer_devices protected by genl_lock() here */
  1990. idr_for_each_entry(&connection->peer_devices, peer_device, i) {
  1991. struct drbd_device *device = peer_device->device;
  1992. if (!device->bitmap) {
  1993. if (drbd_bm_init(device))
  1994. return ERR_NOMEM;
  1995. }
  1996. }
  1997. return rv;
  1998. }
  1999. struct crypto {
  2000. struct crypto_ahash *verify_tfm;
  2001. struct crypto_ahash *csums_tfm;
  2002. struct crypto_shash *cram_hmac_tfm;
  2003. struct crypto_ahash *integrity_tfm;
  2004. };
  2005. static int
  2006. alloc_shash(struct crypto_shash **tfm, char *tfm_name, int err_alg)
  2007. {
  2008. if (!tfm_name[0])
  2009. return NO_ERROR;
  2010. *tfm = crypto_alloc_shash(tfm_name, 0, 0);
  2011. if (IS_ERR(*tfm)) {
  2012. *tfm = NULL;
  2013. return err_alg;
  2014. }
  2015. return NO_ERROR;
  2016. }
  2017. static int
  2018. alloc_ahash(struct crypto_ahash **tfm, char *tfm_name, int err_alg)
  2019. {
  2020. if (!tfm_name[0])
  2021. return NO_ERROR;
  2022. *tfm = crypto_alloc_ahash(tfm_name, 0, CRYPTO_ALG_ASYNC);
  2023. if (IS_ERR(*tfm)) {
  2024. *tfm = NULL;
  2025. return err_alg;
  2026. }
  2027. return NO_ERROR;
  2028. }
  2029. static enum drbd_ret_code
  2030. alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
  2031. {
  2032. char hmac_name[CRYPTO_MAX_ALG_NAME];
  2033. enum drbd_ret_code rv;
  2034. rv = alloc_ahash(&crypto->csums_tfm, new_net_conf->csums_alg,
  2035. ERR_CSUMS_ALG);
  2036. if (rv != NO_ERROR)
  2037. return rv;
  2038. rv = alloc_ahash(&crypto->verify_tfm, new_net_conf->verify_alg,
  2039. ERR_VERIFY_ALG);
  2040. if (rv != NO_ERROR)
  2041. return rv;
  2042. rv = alloc_ahash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
  2043. ERR_INTEGRITY_ALG);
  2044. if (rv != NO_ERROR)
  2045. return rv;
  2046. if (new_net_conf->cram_hmac_alg[0] != 0) {
  2047. snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
  2048. new_net_conf->cram_hmac_alg);
  2049. rv = alloc_shash(&crypto->cram_hmac_tfm, hmac_name,
  2050. ERR_AUTH_ALG);
  2051. }
  2052. return rv;
  2053. }
  2054. static void free_crypto(struct crypto *crypto)
  2055. {
  2056. crypto_free_shash(crypto->cram_hmac_tfm);
  2057. crypto_free_ahash(crypto->integrity_tfm);
  2058. crypto_free_ahash(crypto->csums_tfm);
  2059. crypto_free_ahash(crypto->verify_tfm);
  2060. }
  2061. int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
  2062. {
  2063. struct drbd_config_context adm_ctx;
  2064. enum drbd_ret_code retcode;
  2065. struct drbd_connection *connection;
  2066. struct net_conf *old_net_conf, *new_net_conf = NULL;
  2067. int err;
  2068. int ovr; /* online verify running */
  2069. int rsr; /* re-sync running */
  2070. struct crypto crypto = { };
  2071. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
  2072. if (!adm_ctx.reply_skb)
  2073. return retcode;
  2074. if (retcode != NO_ERROR)
  2075. goto finish;
  2076. connection = adm_ctx.connection;
  2077. mutex_lock(&adm_ctx.resource->adm_mutex);
  2078. new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
  2079. if (!new_net_conf) {
  2080. retcode = ERR_NOMEM;
  2081. goto out;
  2082. }
  2083. conn_reconfig_start(connection);
  2084. mutex_lock(&connection->data.mutex);
  2085. mutex_lock(&connection->resource->conf_update);
  2086. old_net_conf = connection->net_conf;
  2087. if (!old_net_conf) {
  2088. drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
  2089. retcode = ERR_INVALID_REQUEST;
  2090. goto fail;
  2091. }
  2092. *new_net_conf = *old_net_conf;
  2093. if (should_set_defaults(info))
  2094. set_net_conf_defaults(new_net_conf);
  2095. err = net_conf_from_attrs_for_change(new_net_conf, info);
  2096. if (err && err != -ENOMSG) {
  2097. retcode = ERR_MANDATORY_TAG;
  2098. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  2099. goto fail;
  2100. }
  2101. retcode = check_net_options(connection, new_net_conf);
  2102. if (retcode != NO_ERROR)
  2103. goto fail;
  2104. /* re-sync running */
  2105. rsr = conn_resync_running(connection);
  2106. if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
  2107. retcode = ERR_CSUMS_RESYNC_RUNNING;
  2108. goto fail;
  2109. }
  2110. /* online verify running */
  2111. ovr = conn_ov_running(connection);
  2112. if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
  2113. retcode = ERR_VERIFY_RUNNING;
  2114. goto fail;
  2115. }
  2116. retcode = alloc_crypto(&crypto, new_net_conf);
  2117. if (retcode != NO_ERROR)
  2118. goto fail;
  2119. rcu_assign_pointer(connection->net_conf, new_net_conf);
  2120. if (!rsr) {
  2121. crypto_free_ahash(connection->csums_tfm);
  2122. connection->csums_tfm = crypto.csums_tfm;
  2123. crypto.csums_tfm = NULL;
  2124. }
  2125. if (!ovr) {
  2126. crypto_free_ahash(connection->verify_tfm);
  2127. connection->verify_tfm = crypto.verify_tfm;
  2128. crypto.verify_tfm = NULL;
  2129. }
  2130. crypto_free_ahash(connection->integrity_tfm);
  2131. connection->integrity_tfm = crypto.integrity_tfm;
  2132. if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
  2133. /* Do this without trying to take connection->data.mutex again. */
  2134. __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
  2135. crypto_free_shash(connection->cram_hmac_tfm);
  2136. connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
  2137. mutex_unlock(&connection->resource->conf_update);
  2138. mutex_unlock(&connection->data.mutex);
  2139. synchronize_rcu();
  2140. kfree(old_net_conf);
  2141. if (connection->cstate >= C_WF_REPORT_PARAMS) {
  2142. struct drbd_peer_device *peer_device;
  2143. int vnr;
  2144. idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
  2145. drbd_send_sync_param(peer_device);
  2146. }
  2147. goto done;
  2148. fail:
  2149. mutex_unlock(&connection->resource->conf_update);
  2150. mutex_unlock(&connection->data.mutex);
  2151. free_crypto(&crypto);
  2152. kfree(new_net_conf);
  2153. done:
  2154. conn_reconfig_done(connection);
  2155. out:
  2156. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2157. finish:
  2158. drbd_adm_finish(&adm_ctx, info, retcode);
  2159. return 0;
  2160. }
  2161. static void connection_to_info(struct connection_info *info,
  2162. struct drbd_connection *connection)
  2163. {
  2164. info->conn_connection_state = connection->cstate;
  2165. info->conn_role = conn_highest_peer(connection);
  2166. }
  2167. static void peer_device_to_info(struct peer_device_info *info,
  2168. struct drbd_peer_device *peer_device)
  2169. {
  2170. struct drbd_device *device = peer_device->device;
  2171. info->peer_repl_state =
  2172. max_t(enum drbd_conns, C_WF_REPORT_PARAMS, device->state.conn);
  2173. info->peer_disk_state = device->state.pdsk;
  2174. info->peer_resync_susp_user = device->state.user_isp;
  2175. info->peer_resync_susp_peer = device->state.peer_isp;
  2176. info->peer_resync_susp_dependency = device->state.aftr_isp;
  2177. }
  2178. int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
  2179. {
  2180. struct connection_info connection_info;
  2181. enum drbd_notification_type flags;
  2182. unsigned int peer_devices = 0;
  2183. struct drbd_config_context adm_ctx;
  2184. struct drbd_peer_device *peer_device;
  2185. struct net_conf *old_net_conf, *new_net_conf = NULL;
  2186. struct crypto crypto = { };
  2187. struct drbd_resource *resource;
  2188. struct drbd_connection *connection;
  2189. enum drbd_ret_code retcode;
  2190. int i;
  2191. int err;
  2192. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
  2193. if (!adm_ctx.reply_skb)
  2194. return retcode;
  2195. if (retcode != NO_ERROR)
  2196. goto out;
  2197. if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
  2198. drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
  2199. retcode = ERR_INVALID_REQUEST;
  2200. goto out;
  2201. }
  2202. /* No need for _rcu here. All reconfiguration is
  2203. * strictly serialized on genl_lock(). We are protected against
  2204. * concurrent reconfiguration/addition/deletion */
  2205. for_each_resource(resource, &drbd_resources) {
  2206. for_each_connection(connection, resource) {
  2207. if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
  2208. !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
  2209. connection->my_addr_len)) {
  2210. retcode = ERR_LOCAL_ADDR;
  2211. goto out;
  2212. }
  2213. if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
  2214. !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
  2215. connection->peer_addr_len)) {
  2216. retcode = ERR_PEER_ADDR;
  2217. goto out;
  2218. }
  2219. }
  2220. }
  2221. mutex_lock(&adm_ctx.resource->adm_mutex);
  2222. connection = first_connection(adm_ctx.resource);
  2223. conn_reconfig_start(connection);
  2224. if (connection->cstate > C_STANDALONE) {
  2225. retcode = ERR_NET_CONFIGURED;
  2226. goto fail;
  2227. }
  2228. /* allocation not in the IO path, drbdsetup / netlink process context */
  2229. new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
  2230. if (!new_net_conf) {
  2231. retcode = ERR_NOMEM;
  2232. goto fail;
  2233. }
  2234. set_net_conf_defaults(new_net_conf);
  2235. err = net_conf_from_attrs(new_net_conf, info);
  2236. if (err && err != -ENOMSG) {
  2237. retcode = ERR_MANDATORY_TAG;
  2238. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  2239. goto fail;
  2240. }
  2241. retcode = check_net_options(connection, new_net_conf);
  2242. if (retcode != NO_ERROR)
  2243. goto fail;
  2244. retcode = alloc_crypto(&crypto, new_net_conf);
  2245. if (retcode != NO_ERROR)
  2246. goto fail;
  2247. ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
  2248. drbd_flush_workqueue(&connection->sender_work);
  2249. mutex_lock(&adm_ctx.resource->conf_update);
  2250. old_net_conf = connection->net_conf;
  2251. if (old_net_conf) {
  2252. retcode = ERR_NET_CONFIGURED;
  2253. mutex_unlock(&adm_ctx.resource->conf_update);
  2254. goto fail;
  2255. }
  2256. rcu_assign_pointer(connection->net_conf, new_net_conf);
  2257. conn_free_crypto(connection);
  2258. connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
  2259. connection->integrity_tfm = crypto.integrity_tfm;
  2260. connection->csums_tfm = crypto.csums_tfm;
  2261. connection->verify_tfm = crypto.verify_tfm;
  2262. connection->my_addr_len = nla_len(adm_ctx.my_addr);
  2263. memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
  2264. connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
  2265. memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
  2266. idr_for_each_entry(&connection->peer_devices, peer_device, i) {
  2267. peer_devices++;
  2268. }
  2269. connection_to_info(&connection_info, connection);
  2270. flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
  2271. mutex_lock(&notification_mutex);
  2272. notify_connection_state(NULL, 0, connection, &connection_info, NOTIFY_CREATE | flags);
  2273. idr_for_each_entry(&connection->peer_devices, peer_device, i) {
  2274. struct peer_device_info peer_device_info;
  2275. peer_device_to_info(&peer_device_info, peer_device);
  2276. flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
  2277. notify_peer_device_state(NULL, 0, peer_device, &peer_device_info, NOTIFY_CREATE | flags);
  2278. }
  2279. mutex_unlock(&notification_mutex);
  2280. mutex_unlock(&adm_ctx.resource->conf_update);
  2281. rcu_read_lock();
  2282. idr_for_each_entry(&connection->peer_devices, peer_device, i) {
  2283. struct drbd_device *device = peer_device->device;
  2284. device->send_cnt = 0;
  2285. device->recv_cnt = 0;
  2286. }
  2287. rcu_read_unlock();
  2288. retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
  2289. conn_reconfig_done(connection);
  2290. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2291. drbd_adm_finish(&adm_ctx, info, retcode);
  2292. return 0;
  2293. fail:
  2294. free_crypto(&crypto);
  2295. kfree(new_net_conf);
  2296. conn_reconfig_done(connection);
  2297. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2298. out:
  2299. drbd_adm_finish(&adm_ctx, info, retcode);
  2300. return 0;
  2301. }
  2302. static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
  2303. {
  2304. enum drbd_state_rv rv;
  2305. rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
  2306. force ? CS_HARD : 0);
  2307. switch (rv) {
  2308. case SS_NOTHING_TO_DO:
  2309. break;
  2310. case SS_ALREADY_STANDALONE:
  2311. return SS_SUCCESS;
  2312. case SS_PRIMARY_NOP:
  2313. /* Our state checking code wants to see the peer outdated. */
  2314. rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
  2315. if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
  2316. rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
  2317. break;
  2318. case SS_CW_FAILED_BY_PEER:
  2319. /* The peer probably wants to see us outdated. */
  2320. rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
  2321. disk, D_OUTDATED), 0);
  2322. if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
  2323. rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
  2324. CS_HARD);
  2325. }
  2326. break;
  2327. default:;
  2328. /* no special handling necessary */
  2329. }
  2330. if (rv >= SS_SUCCESS) {
  2331. enum drbd_state_rv rv2;
  2332. /* No one else can reconfigure the network while I am here.
  2333. * The state handling only uses drbd_thread_stop_nowait(),
  2334. * we want to really wait here until the receiver is no more.
  2335. */
  2336. drbd_thread_stop(&connection->receiver);
  2337. /* Race breaker. This additional state change request may be
  2338. * necessary, if this was a forced disconnect during a receiver
  2339. * restart. We may have "killed" the receiver thread just
  2340. * after drbd_receiver() returned. Typically, we should be
  2341. * C_STANDALONE already, now, and this becomes a no-op.
  2342. */
  2343. rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
  2344. CS_VERBOSE | CS_HARD);
  2345. if (rv2 < SS_SUCCESS)
  2346. drbd_err(connection,
  2347. "unexpected rv2=%d in conn_try_disconnect()\n",
  2348. rv2);
  2349. /* Unlike in DRBD 9, the state engine has generated
  2350. * NOTIFY_DESTROY events before clearing connection->net_conf. */
  2351. }
  2352. return rv;
  2353. }
  2354. int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
  2355. {
  2356. struct drbd_config_context adm_ctx;
  2357. struct disconnect_parms parms;
  2358. struct drbd_connection *connection;
  2359. enum drbd_state_rv rv;
  2360. enum drbd_ret_code retcode;
  2361. int err;
  2362. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
  2363. if (!adm_ctx.reply_skb)
  2364. return retcode;
  2365. if (retcode != NO_ERROR)
  2366. goto fail;
  2367. connection = adm_ctx.connection;
  2368. memset(&parms, 0, sizeof(parms));
  2369. if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
  2370. err = disconnect_parms_from_attrs(&parms, info);
  2371. if (err) {
  2372. retcode = ERR_MANDATORY_TAG;
  2373. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  2374. goto fail;
  2375. }
  2376. }
  2377. mutex_lock(&adm_ctx.resource->adm_mutex);
  2378. rv = conn_try_disconnect(connection, parms.force_disconnect);
  2379. if (rv < SS_SUCCESS)
  2380. retcode = rv; /* FIXME: Type mismatch. */
  2381. else
  2382. retcode = NO_ERROR;
  2383. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2384. fail:
  2385. drbd_adm_finish(&adm_ctx, info, retcode);
  2386. return 0;
  2387. }
  2388. void resync_after_online_grow(struct drbd_device *device)
  2389. {
  2390. int iass; /* I am sync source */
  2391. drbd_info(device, "Resync of new storage after online grow\n");
  2392. if (device->state.role != device->state.peer)
  2393. iass = (device->state.role == R_PRIMARY);
  2394. else
  2395. iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
  2396. if (iass)
  2397. drbd_start_resync(device, C_SYNC_SOURCE);
  2398. else
  2399. _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
  2400. }
  2401. int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
  2402. {
  2403. struct drbd_config_context adm_ctx;
  2404. struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
  2405. struct resize_parms rs;
  2406. struct drbd_device *device;
  2407. enum drbd_ret_code retcode;
  2408. enum determine_dev_size dd;
  2409. bool change_al_layout = false;
  2410. enum dds_flags ddsf;
  2411. sector_t u_size;
  2412. int err;
  2413. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  2414. if (!adm_ctx.reply_skb)
  2415. return retcode;
  2416. if (retcode != NO_ERROR)
  2417. goto finish;
  2418. mutex_lock(&adm_ctx.resource->adm_mutex);
  2419. device = adm_ctx.device;
  2420. if (!get_ldev(device)) {
  2421. retcode = ERR_NO_DISK;
  2422. goto fail;
  2423. }
  2424. memset(&rs, 0, sizeof(struct resize_parms));
  2425. rs.al_stripes = device->ldev->md.al_stripes;
  2426. rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
  2427. if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
  2428. err = resize_parms_from_attrs(&rs, info);
  2429. if (err) {
  2430. retcode = ERR_MANDATORY_TAG;
  2431. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  2432. goto fail_ldev;
  2433. }
  2434. }
  2435. if (device->state.conn > C_CONNECTED) {
  2436. retcode = ERR_RESIZE_RESYNC;
  2437. goto fail_ldev;
  2438. }
  2439. if (device->state.role == R_SECONDARY &&
  2440. device->state.peer == R_SECONDARY) {
  2441. retcode = ERR_NO_PRIMARY;
  2442. goto fail_ldev;
  2443. }
  2444. if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
  2445. retcode = ERR_NEED_APV_93;
  2446. goto fail_ldev;
  2447. }
  2448. rcu_read_lock();
  2449. u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
  2450. rcu_read_unlock();
  2451. if (u_size != (sector_t)rs.resize_size) {
  2452. new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
  2453. if (!new_disk_conf) {
  2454. retcode = ERR_NOMEM;
  2455. goto fail_ldev;
  2456. }
  2457. }
  2458. if (device->ldev->md.al_stripes != rs.al_stripes ||
  2459. device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
  2460. u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
  2461. if (al_size_k > (16 * 1024 * 1024)) {
  2462. retcode = ERR_MD_LAYOUT_TOO_BIG;
  2463. goto fail_ldev;
  2464. }
  2465. if (al_size_k < MD_32kB_SECT/2) {
  2466. retcode = ERR_MD_LAYOUT_TOO_SMALL;
  2467. goto fail_ldev;
  2468. }
  2469. if (device->state.conn != C_CONNECTED && !rs.resize_force) {
  2470. retcode = ERR_MD_LAYOUT_CONNECTED;
  2471. goto fail_ldev;
  2472. }
  2473. change_al_layout = true;
  2474. }
  2475. if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
  2476. device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
  2477. if (new_disk_conf) {
  2478. mutex_lock(&device->resource->conf_update);
  2479. old_disk_conf = device->ldev->disk_conf;
  2480. *new_disk_conf = *old_disk_conf;
  2481. new_disk_conf->disk_size = (sector_t)rs.resize_size;
  2482. rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
  2483. mutex_unlock(&device->resource->conf_update);
  2484. synchronize_rcu();
  2485. kfree(old_disk_conf);
  2486. new_disk_conf = NULL;
  2487. }
  2488. ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
  2489. dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
  2490. drbd_md_sync(device);
  2491. put_ldev(device);
  2492. if (dd == DS_ERROR) {
  2493. retcode = ERR_NOMEM_BITMAP;
  2494. goto fail;
  2495. } else if (dd == DS_ERROR_SPACE_MD) {
  2496. retcode = ERR_MD_LAYOUT_NO_FIT;
  2497. goto fail;
  2498. } else if (dd == DS_ERROR_SHRINK) {
  2499. retcode = ERR_IMPLICIT_SHRINK;
  2500. goto fail;
  2501. }
  2502. if (device->state.conn == C_CONNECTED) {
  2503. if (dd == DS_GREW)
  2504. set_bit(RESIZE_PENDING, &device->flags);
  2505. drbd_send_uuids(first_peer_device(device));
  2506. drbd_send_sizes(first_peer_device(device), 1, ddsf);
  2507. }
  2508. fail:
  2509. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2510. finish:
  2511. drbd_adm_finish(&adm_ctx, info, retcode);
  2512. return 0;
  2513. fail_ldev:
  2514. put_ldev(device);
  2515. kfree(new_disk_conf);
  2516. goto fail;
  2517. }
  2518. int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
  2519. {
  2520. struct drbd_config_context adm_ctx;
  2521. enum drbd_ret_code retcode;
  2522. struct res_opts res_opts;
  2523. int err;
  2524. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
  2525. if (!adm_ctx.reply_skb)
  2526. return retcode;
  2527. if (retcode != NO_ERROR)
  2528. goto fail;
  2529. res_opts = adm_ctx.resource->res_opts;
  2530. if (should_set_defaults(info))
  2531. set_res_opts_defaults(&res_opts);
  2532. err = res_opts_from_attrs(&res_opts, info);
  2533. if (err && err != -ENOMSG) {
  2534. retcode = ERR_MANDATORY_TAG;
  2535. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  2536. goto fail;
  2537. }
  2538. mutex_lock(&adm_ctx.resource->adm_mutex);
  2539. err = set_resource_options(adm_ctx.resource, &res_opts);
  2540. if (err) {
  2541. retcode = ERR_INVALID_REQUEST;
  2542. if (err == -ENOMEM)
  2543. retcode = ERR_NOMEM;
  2544. }
  2545. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2546. fail:
  2547. drbd_adm_finish(&adm_ctx, info, retcode);
  2548. return 0;
  2549. }
  2550. int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
  2551. {
  2552. struct drbd_config_context adm_ctx;
  2553. struct drbd_device *device;
  2554. int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
  2555. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  2556. if (!adm_ctx.reply_skb)
  2557. return retcode;
  2558. if (retcode != NO_ERROR)
  2559. goto out;
  2560. device = adm_ctx.device;
  2561. if (!get_ldev(device)) {
  2562. retcode = ERR_NO_DISK;
  2563. goto out;
  2564. }
  2565. mutex_lock(&adm_ctx.resource->adm_mutex);
  2566. /* If there is still bitmap IO pending, probably because of a previous
  2567. * resync just being finished, wait for it before requesting a new resync.
  2568. * Also wait for it's after_state_ch(). */
  2569. drbd_suspend_io(device);
  2570. wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
  2571. drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
  2572. /* If we happen to be C_STANDALONE R_SECONDARY, just change to
  2573. * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
  2574. * try to start a resync handshake as sync target for full sync.
  2575. */
  2576. if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
  2577. retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
  2578. if (retcode >= SS_SUCCESS) {
  2579. if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
  2580. "set_n_write from invalidate", BM_LOCKED_MASK))
  2581. retcode = ERR_IO_MD_DISK;
  2582. }
  2583. } else
  2584. retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
  2585. drbd_resume_io(device);
  2586. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2587. put_ldev(device);
  2588. out:
  2589. drbd_adm_finish(&adm_ctx, info, retcode);
  2590. return 0;
  2591. }
  2592. static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
  2593. union drbd_state mask, union drbd_state val)
  2594. {
  2595. struct drbd_config_context adm_ctx;
  2596. enum drbd_ret_code retcode;
  2597. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  2598. if (!adm_ctx.reply_skb)
  2599. return retcode;
  2600. if (retcode != NO_ERROR)
  2601. goto out;
  2602. mutex_lock(&adm_ctx.resource->adm_mutex);
  2603. retcode = drbd_request_state(adm_ctx.device, mask, val);
  2604. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2605. out:
  2606. drbd_adm_finish(&adm_ctx, info, retcode);
  2607. return 0;
  2608. }
  2609. static int drbd_bmio_set_susp_al(struct drbd_device *device) __must_hold(local)
  2610. {
  2611. int rv;
  2612. rv = drbd_bmio_set_n_write(device);
  2613. drbd_suspend_al(device);
  2614. return rv;
  2615. }
  2616. int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
  2617. {
  2618. struct drbd_config_context adm_ctx;
  2619. int retcode; /* drbd_ret_code, drbd_state_rv */
  2620. struct drbd_device *device;
  2621. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  2622. if (!adm_ctx.reply_skb)
  2623. return retcode;
  2624. if (retcode != NO_ERROR)
  2625. goto out;
  2626. device = adm_ctx.device;
  2627. if (!get_ldev(device)) {
  2628. retcode = ERR_NO_DISK;
  2629. goto out;
  2630. }
  2631. mutex_lock(&adm_ctx.resource->adm_mutex);
  2632. /* If there is still bitmap IO pending, probably because of a previous
  2633. * resync just being finished, wait for it before requesting a new resync.
  2634. * Also wait for it's after_state_ch(). */
  2635. drbd_suspend_io(device);
  2636. wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
  2637. drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
  2638. /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
  2639. * in the bitmap. Otherwise, try to start a resync handshake
  2640. * as sync source for full sync.
  2641. */
  2642. if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
  2643. /* The peer will get a resync upon connect anyways. Just make that
  2644. into a full resync. */
  2645. retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
  2646. if (retcode >= SS_SUCCESS) {
  2647. if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
  2648. "set_n_write from invalidate_peer",
  2649. BM_LOCKED_SET_ALLOWED))
  2650. retcode = ERR_IO_MD_DISK;
  2651. }
  2652. } else
  2653. retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
  2654. drbd_resume_io(device);
  2655. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2656. put_ldev(device);
  2657. out:
  2658. drbd_adm_finish(&adm_ctx, info, retcode);
  2659. return 0;
  2660. }
  2661. int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
  2662. {
  2663. struct drbd_config_context adm_ctx;
  2664. enum drbd_ret_code retcode;
  2665. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  2666. if (!adm_ctx.reply_skb)
  2667. return retcode;
  2668. if (retcode != NO_ERROR)
  2669. goto out;
  2670. mutex_lock(&adm_ctx.resource->adm_mutex);
  2671. if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
  2672. retcode = ERR_PAUSE_IS_SET;
  2673. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2674. out:
  2675. drbd_adm_finish(&adm_ctx, info, retcode);
  2676. return 0;
  2677. }
  2678. int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
  2679. {
  2680. struct drbd_config_context adm_ctx;
  2681. union drbd_dev_state s;
  2682. enum drbd_ret_code retcode;
  2683. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  2684. if (!adm_ctx.reply_skb)
  2685. return retcode;
  2686. if (retcode != NO_ERROR)
  2687. goto out;
  2688. mutex_lock(&adm_ctx.resource->adm_mutex);
  2689. if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
  2690. s = adm_ctx.device->state;
  2691. if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
  2692. retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
  2693. s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
  2694. } else {
  2695. retcode = ERR_PAUSE_IS_CLEAR;
  2696. }
  2697. }
  2698. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2699. out:
  2700. drbd_adm_finish(&adm_ctx, info, retcode);
  2701. return 0;
  2702. }
  2703. int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
  2704. {
  2705. return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
  2706. }
  2707. int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
  2708. {
  2709. struct drbd_config_context adm_ctx;
  2710. struct drbd_device *device;
  2711. int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
  2712. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  2713. if (!adm_ctx.reply_skb)
  2714. return retcode;
  2715. if (retcode != NO_ERROR)
  2716. goto out;
  2717. mutex_lock(&adm_ctx.resource->adm_mutex);
  2718. device = adm_ctx.device;
  2719. if (test_bit(NEW_CUR_UUID, &device->flags)) {
  2720. if (get_ldev_if_state(device, D_ATTACHING)) {
  2721. drbd_uuid_new_current(device);
  2722. put_ldev(device);
  2723. } else {
  2724. /* This is effectively a multi-stage "forced down".
  2725. * The NEW_CUR_UUID bit is supposedly only set, if we
  2726. * lost the replication connection, and are configured
  2727. * to freeze IO and wait for some fence-peer handler.
  2728. * So we still don't have a replication connection.
  2729. * And now we don't have a local disk either. After
  2730. * resume, we will fail all pending and new IO, because
  2731. * we don't have any data anymore. Which means we will
  2732. * eventually be able to terminate all users of this
  2733. * device, and then take it down. By bumping the
  2734. * "effective" data uuid, we make sure that you really
  2735. * need to tear down before you reconfigure, we will
  2736. * the refuse to re-connect or re-attach (because no
  2737. * matching real data uuid exists).
  2738. */
  2739. u64 val;
  2740. get_random_bytes(&val, sizeof(u64));
  2741. drbd_set_ed_uuid(device, val);
  2742. drbd_warn(device, "Resumed without access to data; please tear down before attempting to re-configure.\n");
  2743. }
  2744. clear_bit(NEW_CUR_UUID, &device->flags);
  2745. }
  2746. drbd_suspend_io(device);
  2747. retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
  2748. if (retcode == SS_SUCCESS) {
  2749. if (device->state.conn < C_CONNECTED)
  2750. tl_clear(first_peer_device(device)->connection);
  2751. if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
  2752. tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
  2753. }
  2754. drbd_resume_io(device);
  2755. mutex_unlock(&adm_ctx.resource->adm_mutex);
  2756. out:
  2757. drbd_adm_finish(&adm_ctx, info, retcode);
  2758. return 0;
  2759. }
  2760. int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
  2761. {
  2762. return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
  2763. }
  2764. static int nla_put_drbd_cfg_context(struct sk_buff *skb,
  2765. struct drbd_resource *resource,
  2766. struct drbd_connection *connection,
  2767. struct drbd_device *device)
  2768. {
  2769. struct nlattr *nla;
  2770. nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
  2771. if (!nla)
  2772. goto nla_put_failure;
  2773. if (device &&
  2774. nla_put_u32(skb, T_ctx_volume, device->vnr))
  2775. goto nla_put_failure;
  2776. if (nla_put_string(skb, T_ctx_resource_name, resource->name))
  2777. goto nla_put_failure;
  2778. if (connection) {
  2779. if (connection->my_addr_len &&
  2780. nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
  2781. goto nla_put_failure;
  2782. if (connection->peer_addr_len &&
  2783. nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
  2784. goto nla_put_failure;
  2785. }
  2786. nla_nest_end(skb, nla);
  2787. return 0;
  2788. nla_put_failure:
  2789. if (nla)
  2790. nla_nest_cancel(skb, nla);
  2791. return -EMSGSIZE;
  2792. }
  2793. /*
  2794. * The generic netlink dump callbacks are called outside the genl_lock(), so
  2795. * they cannot use the simple attribute parsing code which uses global
  2796. * attribute tables.
  2797. */
  2798. static struct nlattr *find_cfg_context_attr(const struct nlmsghdr *nlh, int attr)
  2799. {
  2800. const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
  2801. const int maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
  2802. struct nlattr *nla;
  2803. nla = nla_find(nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen),
  2804. DRBD_NLA_CFG_CONTEXT);
  2805. if (!nla)
  2806. return NULL;
  2807. return drbd_nla_find_nested(maxtype, nla, __nla_type(attr));
  2808. }
  2809. static void resource_to_info(struct resource_info *, struct drbd_resource *);
  2810. int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb)
  2811. {
  2812. struct drbd_genlmsghdr *dh;
  2813. struct drbd_resource *resource;
  2814. struct resource_info resource_info;
  2815. struct resource_statistics resource_statistics;
  2816. int err;
  2817. rcu_read_lock();
  2818. if (cb->args[0]) {
  2819. for_each_resource_rcu(resource, &drbd_resources)
  2820. if (resource == (struct drbd_resource *)cb->args[0])
  2821. goto found_resource;
  2822. err = 0; /* resource was probably deleted */
  2823. goto out;
  2824. }
  2825. resource = list_entry(&drbd_resources,
  2826. struct drbd_resource, resources);
  2827. found_resource:
  2828. list_for_each_entry_continue_rcu(resource, &drbd_resources, resources) {
  2829. goto put_result;
  2830. }
  2831. err = 0;
  2832. goto out;
  2833. put_result:
  2834. dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  2835. cb->nlh->nlmsg_seq, &drbd_genl_family,
  2836. NLM_F_MULTI, DRBD_ADM_GET_RESOURCES);
  2837. err = -ENOMEM;
  2838. if (!dh)
  2839. goto out;
  2840. dh->minor = -1U;
  2841. dh->ret_code = NO_ERROR;
  2842. err = nla_put_drbd_cfg_context(skb, resource, NULL, NULL);
  2843. if (err)
  2844. goto out;
  2845. err = res_opts_to_skb(skb, &resource->res_opts, !capable(CAP_SYS_ADMIN));
  2846. if (err)
  2847. goto out;
  2848. resource_to_info(&resource_info, resource);
  2849. err = resource_info_to_skb(skb, &resource_info, !capable(CAP_SYS_ADMIN));
  2850. if (err)
  2851. goto out;
  2852. resource_statistics.res_stat_write_ordering = resource->write_ordering;
  2853. err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
  2854. if (err)
  2855. goto out;
  2856. cb->args[0] = (long)resource;
  2857. genlmsg_end(skb, dh);
  2858. err = 0;
  2859. out:
  2860. rcu_read_unlock();
  2861. if (err)
  2862. return err;
  2863. return skb->len;
  2864. }
  2865. static void device_to_statistics(struct device_statistics *s,
  2866. struct drbd_device *device)
  2867. {
  2868. memset(s, 0, sizeof(*s));
  2869. s->dev_upper_blocked = !may_inc_ap_bio(device);
  2870. if (get_ldev(device)) {
  2871. struct drbd_md *md = &device->ldev->md;
  2872. u64 *history_uuids = (u64 *)s->history_uuids;
  2873. struct request_queue *q;
  2874. int n;
  2875. spin_lock_irq(&md->uuid_lock);
  2876. s->dev_current_uuid = md->uuid[UI_CURRENT];
  2877. BUILD_BUG_ON(sizeof(s->history_uuids) < UI_HISTORY_END - UI_HISTORY_START + 1);
  2878. for (n = 0; n < UI_HISTORY_END - UI_HISTORY_START + 1; n++)
  2879. history_uuids[n] = md->uuid[UI_HISTORY_START + n];
  2880. for (; n < HISTORY_UUIDS; n++)
  2881. history_uuids[n] = 0;
  2882. s->history_uuids_len = HISTORY_UUIDS;
  2883. spin_unlock_irq(&md->uuid_lock);
  2884. s->dev_disk_flags = md->flags;
  2885. q = bdev_get_queue(device->ldev->backing_bdev);
  2886. s->dev_lower_blocked =
  2887. bdi_congested(&q->backing_dev_info,
  2888. (1 << WB_async_congested) |
  2889. (1 << WB_sync_congested));
  2890. put_ldev(device);
  2891. }
  2892. s->dev_size = drbd_get_capacity(device->this_bdev);
  2893. s->dev_read = device->read_cnt;
  2894. s->dev_write = device->writ_cnt;
  2895. s->dev_al_writes = device->al_writ_cnt;
  2896. s->dev_bm_writes = device->bm_writ_cnt;
  2897. s->dev_upper_pending = atomic_read(&device->ap_bio_cnt);
  2898. s->dev_lower_pending = atomic_read(&device->local_cnt);
  2899. s->dev_al_suspended = test_bit(AL_SUSPENDED, &device->flags);
  2900. s->dev_exposed_data_uuid = device->ed_uuid;
  2901. }
  2902. static int put_resource_in_arg0(struct netlink_callback *cb, int holder_nr)
  2903. {
  2904. if (cb->args[0]) {
  2905. struct drbd_resource *resource =
  2906. (struct drbd_resource *)cb->args[0];
  2907. kref_put(&resource->kref, drbd_destroy_resource);
  2908. }
  2909. return 0;
  2910. }
  2911. int drbd_adm_dump_devices_done(struct netlink_callback *cb) {
  2912. return put_resource_in_arg0(cb, 7);
  2913. }
  2914. static void device_to_info(struct device_info *, struct drbd_device *);
  2915. int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb)
  2916. {
  2917. struct nlattr *resource_filter;
  2918. struct drbd_resource *resource;
  2919. struct drbd_device *uninitialized_var(device);
  2920. int minor, err, retcode;
  2921. struct drbd_genlmsghdr *dh;
  2922. struct device_info device_info;
  2923. struct device_statistics device_statistics;
  2924. struct idr *idr_to_search;
  2925. resource = (struct drbd_resource *)cb->args[0];
  2926. if (!cb->args[0] && !cb->args[1]) {
  2927. resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
  2928. if (resource_filter) {
  2929. retcode = ERR_RES_NOT_KNOWN;
  2930. resource = drbd_find_resource(nla_data(resource_filter));
  2931. if (!resource)
  2932. goto put_result;
  2933. cb->args[0] = (long)resource;
  2934. }
  2935. }
  2936. rcu_read_lock();
  2937. minor = cb->args[1];
  2938. idr_to_search = resource ? &resource->devices : &drbd_devices;
  2939. device = idr_get_next(idr_to_search, &minor);
  2940. if (!device) {
  2941. err = 0;
  2942. goto out;
  2943. }
  2944. idr_for_each_entry_continue(idr_to_search, device, minor) {
  2945. retcode = NO_ERROR;
  2946. goto put_result; /* only one iteration */
  2947. }
  2948. err = 0;
  2949. goto out; /* no more devices */
  2950. put_result:
  2951. dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  2952. cb->nlh->nlmsg_seq, &drbd_genl_family,
  2953. NLM_F_MULTI, DRBD_ADM_GET_DEVICES);
  2954. err = -ENOMEM;
  2955. if (!dh)
  2956. goto out;
  2957. dh->ret_code = retcode;
  2958. dh->minor = -1U;
  2959. if (retcode == NO_ERROR) {
  2960. dh->minor = device->minor;
  2961. err = nla_put_drbd_cfg_context(skb, device->resource, NULL, device);
  2962. if (err)
  2963. goto out;
  2964. if (get_ldev(device)) {
  2965. struct disk_conf *disk_conf =
  2966. rcu_dereference(device->ldev->disk_conf);
  2967. err = disk_conf_to_skb(skb, disk_conf, !capable(CAP_SYS_ADMIN));
  2968. put_ldev(device);
  2969. if (err)
  2970. goto out;
  2971. }
  2972. device_to_info(&device_info, device);
  2973. err = device_info_to_skb(skb, &device_info, !capable(CAP_SYS_ADMIN));
  2974. if (err)
  2975. goto out;
  2976. device_to_statistics(&device_statistics, device);
  2977. err = device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
  2978. if (err)
  2979. goto out;
  2980. cb->args[1] = minor + 1;
  2981. }
  2982. genlmsg_end(skb, dh);
  2983. err = 0;
  2984. out:
  2985. rcu_read_unlock();
  2986. if (err)
  2987. return err;
  2988. return skb->len;
  2989. }
  2990. int drbd_adm_dump_connections_done(struct netlink_callback *cb)
  2991. {
  2992. return put_resource_in_arg0(cb, 6);
  2993. }
  2994. enum { SINGLE_RESOURCE, ITERATE_RESOURCES };
  2995. int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb)
  2996. {
  2997. struct nlattr *resource_filter;
  2998. struct drbd_resource *resource = NULL, *next_resource;
  2999. struct drbd_connection *uninitialized_var(connection);
  3000. int err = 0, retcode;
  3001. struct drbd_genlmsghdr *dh;
  3002. struct connection_info connection_info;
  3003. struct connection_statistics connection_statistics;
  3004. rcu_read_lock();
  3005. resource = (struct drbd_resource *)cb->args[0];
  3006. if (!cb->args[0]) {
  3007. resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
  3008. if (resource_filter) {
  3009. retcode = ERR_RES_NOT_KNOWN;
  3010. resource = drbd_find_resource(nla_data(resource_filter));
  3011. if (!resource)
  3012. goto put_result;
  3013. cb->args[0] = (long)resource;
  3014. cb->args[1] = SINGLE_RESOURCE;
  3015. }
  3016. }
  3017. if (!resource) {
  3018. if (list_empty(&drbd_resources))
  3019. goto out;
  3020. resource = list_first_entry(&drbd_resources, struct drbd_resource, resources);
  3021. kref_get(&resource->kref);
  3022. cb->args[0] = (long)resource;
  3023. cb->args[1] = ITERATE_RESOURCES;
  3024. }
  3025. next_resource:
  3026. rcu_read_unlock();
  3027. mutex_lock(&resource->conf_update);
  3028. rcu_read_lock();
  3029. if (cb->args[2]) {
  3030. for_each_connection_rcu(connection, resource)
  3031. if (connection == (struct drbd_connection *)cb->args[2])
  3032. goto found_connection;
  3033. /* connection was probably deleted */
  3034. goto no_more_connections;
  3035. }
  3036. connection = list_entry(&resource->connections, struct drbd_connection, connections);
  3037. found_connection:
  3038. list_for_each_entry_continue_rcu(connection, &resource->connections, connections) {
  3039. if (!has_net_conf(connection))
  3040. continue;
  3041. retcode = NO_ERROR;
  3042. goto put_result; /* only one iteration */
  3043. }
  3044. no_more_connections:
  3045. if (cb->args[1] == ITERATE_RESOURCES) {
  3046. for_each_resource_rcu(next_resource, &drbd_resources) {
  3047. if (next_resource == resource)
  3048. goto found_resource;
  3049. }
  3050. /* resource was probably deleted */
  3051. }
  3052. goto out;
  3053. found_resource:
  3054. list_for_each_entry_continue_rcu(next_resource, &drbd_resources, resources) {
  3055. mutex_unlock(&resource->conf_update);
  3056. kref_put(&resource->kref, drbd_destroy_resource);
  3057. resource = next_resource;
  3058. kref_get(&resource->kref);
  3059. cb->args[0] = (long)resource;
  3060. cb->args[2] = 0;
  3061. goto next_resource;
  3062. }
  3063. goto out; /* no more resources */
  3064. put_result:
  3065. dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  3066. cb->nlh->nlmsg_seq, &drbd_genl_family,
  3067. NLM_F_MULTI, DRBD_ADM_GET_CONNECTIONS);
  3068. err = -ENOMEM;
  3069. if (!dh)
  3070. goto out;
  3071. dh->ret_code = retcode;
  3072. dh->minor = -1U;
  3073. if (retcode == NO_ERROR) {
  3074. struct net_conf *net_conf;
  3075. err = nla_put_drbd_cfg_context(skb, resource, connection, NULL);
  3076. if (err)
  3077. goto out;
  3078. net_conf = rcu_dereference(connection->net_conf);
  3079. if (net_conf) {
  3080. err = net_conf_to_skb(skb, net_conf, !capable(CAP_SYS_ADMIN));
  3081. if (err)
  3082. goto out;
  3083. }
  3084. connection_to_info(&connection_info, connection);
  3085. err = connection_info_to_skb(skb, &connection_info, !capable(CAP_SYS_ADMIN));
  3086. if (err)
  3087. goto out;
  3088. connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
  3089. err = connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
  3090. if (err)
  3091. goto out;
  3092. cb->args[2] = (long)connection;
  3093. }
  3094. genlmsg_end(skb, dh);
  3095. err = 0;
  3096. out:
  3097. rcu_read_unlock();
  3098. if (resource)
  3099. mutex_unlock(&resource->conf_update);
  3100. if (err)
  3101. return err;
  3102. return skb->len;
  3103. }
  3104. enum mdf_peer_flag {
  3105. MDF_PEER_CONNECTED = 1 << 0,
  3106. MDF_PEER_OUTDATED = 1 << 1,
  3107. MDF_PEER_FENCING = 1 << 2,
  3108. MDF_PEER_FULL_SYNC = 1 << 3,
  3109. };
  3110. static void peer_device_to_statistics(struct peer_device_statistics *s,
  3111. struct drbd_peer_device *peer_device)
  3112. {
  3113. struct drbd_device *device = peer_device->device;
  3114. memset(s, 0, sizeof(*s));
  3115. s->peer_dev_received = device->recv_cnt;
  3116. s->peer_dev_sent = device->send_cnt;
  3117. s->peer_dev_pending = atomic_read(&device->ap_pending_cnt) +
  3118. atomic_read(&device->rs_pending_cnt);
  3119. s->peer_dev_unacked = atomic_read(&device->unacked_cnt);
  3120. s->peer_dev_out_of_sync = drbd_bm_total_weight(device) << (BM_BLOCK_SHIFT - 9);
  3121. s->peer_dev_resync_failed = device->rs_failed << (BM_BLOCK_SHIFT - 9);
  3122. if (get_ldev(device)) {
  3123. struct drbd_md *md = &device->ldev->md;
  3124. spin_lock_irq(&md->uuid_lock);
  3125. s->peer_dev_bitmap_uuid = md->uuid[UI_BITMAP];
  3126. spin_unlock_irq(&md->uuid_lock);
  3127. s->peer_dev_flags =
  3128. (drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND) ?
  3129. MDF_PEER_CONNECTED : 0) +
  3130. (drbd_md_test_flag(device->ldev, MDF_CONSISTENT) &&
  3131. !drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE) ?
  3132. MDF_PEER_OUTDATED : 0) +
  3133. /* FIXME: MDF_PEER_FENCING? */
  3134. (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ?
  3135. MDF_PEER_FULL_SYNC : 0);
  3136. put_ldev(device);
  3137. }
  3138. }
  3139. int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb)
  3140. {
  3141. return put_resource_in_arg0(cb, 9);
  3142. }
  3143. int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb)
  3144. {
  3145. struct nlattr *resource_filter;
  3146. struct drbd_resource *resource;
  3147. struct drbd_device *uninitialized_var(device);
  3148. struct drbd_peer_device *peer_device = NULL;
  3149. int minor, err, retcode;
  3150. struct drbd_genlmsghdr *dh;
  3151. struct idr *idr_to_search;
  3152. resource = (struct drbd_resource *)cb->args[0];
  3153. if (!cb->args[0] && !cb->args[1]) {
  3154. resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
  3155. if (resource_filter) {
  3156. retcode = ERR_RES_NOT_KNOWN;
  3157. resource = drbd_find_resource(nla_data(resource_filter));
  3158. if (!resource)
  3159. goto put_result;
  3160. }
  3161. cb->args[0] = (long)resource;
  3162. }
  3163. rcu_read_lock();
  3164. minor = cb->args[1];
  3165. idr_to_search = resource ? &resource->devices : &drbd_devices;
  3166. device = idr_find(idr_to_search, minor);
  3167. if (!device) {
  3168. next_device:
  3169. minor++;
  3170. cb->args[2] = 0;
  3171. device = idr_get_next(idr_to_search, &minor);
  3172. if (!device) {
  3173. err = 0;
  3174. goto out;
  3175. }
  3176. }
  3177. if (cb->args[2]) {
  3178. for_each_peer_device(peer_device, device)
  3179. if (peer_device == (struct drbd_peer_device *)cb->args[2])
  3180. goto found_peer_device;
  3181. /* peer device was probably deleted */
  3182. goto next_device;
  3183. }
  3184. /* Make peer_device point to the list head (not the first entry). */
  3185. peer_device = list_entry(&device->peer_devices, struct drbd_peer_device, peer_devices);
  3186. found_peer_device:
  3187. list_for_each_entry_continue_rcu(peer_device, &device->peer_devices, peer_devices) {
  3188. if (!has_net_conf(peer_device->connection))
  3189. continue;
  3190. retcode = NO_ERROR;
  3191. goto put_result; /* only one iteration */
  3192. }
  3193. goto next_device;
  3194. put_result:
  3195. dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  3196. cb->nlh->nlmsg_seq, &drbd_genl_family,
  3197. NLM_F_MULTI, DRBD_ADM_GET_PEER_DEVICES);
  3198. err = -ENOMEM;
  3199. if (!dh)
  3200. goto out;
  3201. dh->ret_code = retcode;
  3202. dh->minor = -1U;
  3203. if (retcode == NO_ERROR) {
  3204. struct peer_device_info peer_device_info;
  3205. struct peer_device_statistics peer_device_statistics;
  3206. dh->minor = minor;
  3207. err = nla_put_drbd_cfg_context(skb, device->resource, peer_device->connection, device);
  3208. if (err)
  3209. goto out;
  3210. peer_device_to_info(&peer_device_info, peer_device);
  3211. err = peer_device_info_to_skb(skb, &peer_device_info, !capable(CAP_SYS_ADMIN));
  3212. if (err)
  3213. goto out;
  3214. peer_device_to_statistics(&peer_device_statistics, peer_device);
  3215. err = peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
  3216. if (err)
  3217. goto out;
  3218. cb->args[1] = minor;
  3219. cb->args[2] = (long)peer_device;
  3220. }
  3221. genlmsg_end(skb, dh);
  3222. err = 0;
  3223. out:
  3224. rcu_read_unlock();
  3225. if (err)
  3226. return err;
  3227. return skb->len;
  3228. }
  3229. /*
  3230. * Return the connection of @resource if @resource has exactly one connection.
  3231. */
  3232. static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
  3233. {
  3234. struct list_head *connections = &resource->connections;
  3235. if (list_empty(connections) || connections->next->next != connections)
  3236. return NULL;
  3237. return list_first_entry(&resource->connections, struct drbd_connection, connections);
  3238. }
  3239. static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
  3240. const struct sib_info *sib)
  3241. {
  3242. struct drbd_resource *resource = device->resource;
  3243. struct state_info *si = NULL; /* for sizeof(si->member); */
  3244. struct nlattr *nla;
  3245. int got_ldev;
  3246. int err = 0;
  3247. int exclude_sensitive;
  3248. /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
  3249. * to. So we better exclude_sensitive information.
  3250. *
  3251. * If sib == NULL, this is drbd_adm_get_status, executed synchronously
  3252. * in the context of the requesting user process. Exclude sensitive
  3253. * information, unless current has superuser.
  3254. *
  3255. * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
  3256. * relies on the current implementation of netlink_dump(), which
  3257. * executes the dump callback successively from netlink_recvmsg(),
  3258. * always in the context of the receiving process */
  3259. exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
  3260. got_ldev = get_ldev(device);
  3261. /* We need to add connection name and volume number information still.
  3262. * Minor number is in drbd_genlmsghdr. */
  3263. if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
  3264. goto nla_put_failure;
  3265. if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
  3266. goto nla_put_failure;
  3267. rcu_read_lock();
  3268. if (got_ldev) {
  3269. struct disk_conf *disk_conf;
  3270. disk_conf = rcu_dereference(device->ldev->disk_conf);
  3271. err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
  3272. }
  3273. if (!err) {
  3274. struct net_conf *nc;
  3275. nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
  3276. if (nc)
  3277. err = net_conf_to_skb(skb, nc, exclude_sensitive);
  3278. }
  3279. rcu_read_unlock();
  3280. if (err)
  3281. goto nla_put_failure;
  3282. nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
  3283. if (!nla)
  3284. goto nla_put_failure;
  3285. if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
  3286. nla_put_u32(skb, T_current_state, device->state.i) ||
  3287. nla_put_u64_0pad(skb, T_ed_uuid, device->ed_uuid) ||
  3288. nla_put_u64_0pad(skb, T_capacity,
  3289. drbd_get_capacity(device->this_bdev)) ||
  3290. nla_put_u64_0pad(skb, T_send_cnt, device->send_cnt) ||
  3291. nla_put_u64_0pad(skb, T_recv_cnt, device->recv_cnt) ||
  3292. nla_put_u64_0pad(skb, T_read_cnt, device->read_cnt) ||
  3293. nla_put_u64_0pad(skb, T_writ_cnt, device->writ_cnt) ||
  3294. nla_put_u64_0pad(skb, T_al_writ_cnt, device->al_writ_cnt) ||
  3295. nla_put_u64_0pad(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
  3296. nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
  3297. nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
  3298. nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
  3299. goto nla_put_failure;
  3300. if (got_ldev) {
  3301. int err;
  3302. spin_lock_irq(&device->ldev->md.uuid_lock);
  3303. err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
  3304. spin_unlock_irq(&device->ldev->md.uuid_lock);
  3305. if (err)
  3306. goto nla_put_failure;
  3307. if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
  3308. nla_put_u64_0pad(skb, T_bits_total, drbd_bm_bits(device)) ||
  3309. nla_put_u64_0pad(skb, T_bits_oos,
  3310. drbd_bm_total_weight(device)))
  3311. goto nla_put_failure;
  3312. if (C_SYNC_SOURCE <= device->state.conn &&
  3313. C_PAUSED_SYNC_T >= device->state.conn) {
  3314. if (nla_put_u64_0pad(skb, T_bits_rs_total,
  3315. device->rs_total) ||
  3316. nla_put_u64_0pad(skb, T_bits_rs_failed,
  3317. device->rs_failed))
  3318. goto nla_put_failure;
  3319. }
  3320. }
  3321. if (sib) {
  3322. switch(sib->sib_reason) {
  3323. case SIB_SYNC_PROGRESS:
  3324. case SIB_GET_STATUS_REPLY:
  3325. break;
  3326. case SIB_STATE_CHANGE:
  3327. if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
  3328. nla_put_u32(skb, T_new_state, sib->ns.i))
  3329. goto nla_put_failure;
  3330. break;
  3331. case SIB_HELPER_POST:
  3332. if (nla_put_u32(skb, T_helper_exit_code,
  3333. sib->helper_exit_code))
  3334. goto nla_put_failure;
  3335. /* fall through */
  3336. case SIB_HELPER_PRE:
  3337. if (nla_put_string(skb, T_helper, sib->helper_name))
  3338. goto nla_put_failure;
  3339. break;
  3340. }
  3341. }
  3342. nla_nest_end(skb, nla);
  3343. if (0)
  3344. nla_put_failure:
  3345. err = -EMSGSIZE;
  3346. if (got_ldev)
  3347. put_ldev(device);
  3348. return err;
  3349. }
  3350. int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
  3351. {
  3352. struct drbd_config_context adm_ctx;
  3353. enum drbd_ret_code retcode;
  3354. int err;
  3355. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  3356. if (!adm_ctx.reply_skb)
  3357. return retcode;
  3358. if (retcode != NO_ERROR)
  3359. goto out;
  3360. err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
  3361. if (err) {
  3362. nlmsg_free(adm_ctx.reply_skb);
  3363. return err;
  3364. }
  3365. out:
  3366. drbd_adm_finish(&adm_ctx, info, retcode);
  3367. return 0;
  3368. }
  3369. static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
  3370. {
  3371. struct drbd_device *device;
  3372. struct drbd_genlmsghdr *dh;
  3373. struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
  3374. struct drbd_resource *resource = NULL;
  3375. struct drbd_resource *tmp;
  3376. unsigned volume = cb->args[1];
  3377. /* Open coded, deferred, iteration:
  3378. * for_each_resource_safe(resource, tmp, &drbd_resources) {
  3379. * connection = "first connection of resource or undefined";
  3380. * idr_for_each_entry(&resource->devices, device, i) {
  3381. * ...
  3382. * }
  3383. * }
  3384. * where resource is cb->args[0];
  3385. * and i is cb->args[1];
  3386. *
  3387. * cb->args[2] indicates if we shall loop over all resources,
  3388. * or just dump all volumes of a single resource.
  3389. *
  3390. * This may miss entries inserted after this dump started,
  3391. * or entries deleted before they are reached.
  3392. *
  3393. * We need to make sure the device won't disappear while
  3394. * we are looking at it, and revalidate our iterators
  3395. * on each iteration.
  3396. */
  3397. /* synchronize with conn_create()/drbd_destroy_connection() */
  3398. rcu_read_lock();
  3399. /* revalidate iterator position */
  3400. for_each_resource_rcu(tmp, &drbd_resources) {
  3401. if (pos == NULL) {
  3402. /* first iteration */
  3403. pos = tmp;
  3404. resource = pos;
  3405. break;
  3406. }
  3407. if (tmp == pos) {
  3408. resource = pos;
  3409. break;
  3410. }
  3411. }
  3412. if (resource) {
  3413. next_resource:
  3414. device = idr_get_next(&resource->devices, &volume);
  3415. if (!device) {
  3416. /* No more volumes to dump on this resource.
  3417. * Advance resource iterator. */
  3418. pos = list_entry_rcu(resource->resources.next,
  3419. struct drbd_resource, resources);
  3420. /* Did we dump any volume of this resource yet? */
  3421. if (volume != 0) {
  3422. /* If we reached the end of the list,
  3423. * or only a single resource dump was requested,
  3424. * we are done. */
  3425. if (&pos->resources == &drbd_resources || cb->args[2])
  3426. goto out;
  3427. volume = 0;
  3428. resource = pos;
  3429. goto next_resource;
  3430. }
  3431. }
  3432. dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  3433. cb->nlh->nlmsg_seq, &drbd_genl_family,
  3434. NLM_F_MULTI, DRBD_ADM_GET_STATUS);
  3435. if (!dh)
  3436. goto out;
  3437. if (!device) {
  3438. /* This is a connection without a single volume.
  3439. * Suprisingly enough, it may have a network
  3440. * configuration. */
  3441. struct drbd_connection *connection;
  3442. dh->minor = -1U;
  3443. dh->ret_code = NO_ERROR;
  3444. connection = the_only_connection(resource);
  3445. if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
  3446. goto cancel;
  3447. if (connection) {
  3448. struct net_conf *nc;
  3449. nc = rcu_dereference(connection->net_conf);
  3450. if (nc && net_conf_to_skb(skb, nc, 1) != 0)
  3451. goto cancel;
  3452. }
  3453. goto done;
  3454. }
  3455. D_ASSERT(device, device->vnr == volume);
  3456. D_ASSERT(device, device->resource == resource);
  3457. dh->minor = device_to_minor(device);
  3458. dh->ret_code = NO_ERROR;
  3459. if (nla_put_status_info(skb, device, NULL)) {
  3460. cancel:
  3461. genlmsg_cancel(skb, dh);
  3462. goto out;
  3463. }
  3464. done:
  3465. genlmsg_end(skb, dh);
  3466. }
  3467. out:
  3468. rcu_read_unlock();
  3469. /* where to start the next iteration */
  3470. cb->args[0] = (long)pos;
  3471. cb->args[1] = (pos == resource) ? volume + 1 : 0;
  3472. /* No more resources/volumes/minors found results in an empty skb.
  3473. * Which will terminate the dump. */
  3474. return skb->len;
  3475. }
  3476. /*
  3477. * Request status of all resources, or of all volumes within a single resource.
  3478. *
  3479. * This is a dump, as the answer may not fit in a single reply skb otherwise.
  3480. * Which means we cannot use the family->attrbuf or other such members, because
  3481. * dump is NOT protected by the genl_lock(). During dump, we only have access
  3482. * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
  3483. *
  3484. * Once things are setup properly, we call into get_one_status().
  3485. */
  3486. int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
  3487. {
  3488. const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
  3489. struct nlattr *nla;
  3490. const char *resource_name;
  3491. struct drbd_resource *resource;
  3492. int maxtype;
  3493. /* Is this a followup call? */
  3494. if (cb->args[0]) {
  3495. /* ... of a single resource dump,
  3496. * and the resource iterator has been advanced already? */
  3497. if (cb->args[2] && cb->args[2] != cb->args[0])
  3498. return 0; /* DONE. */
  3499. goto dump;
  3500. }
  3501. /* First call (from netlink_dump_start). We need to figure out
  3502. * which resource(s) the user wants us to dump. */
  3503. nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
  3504. nlmsg_attrlen(cb->nlh, hdrlen),
  3505. DRBD_NLA_CFG_CONTEXT);
  3506. /* No explicit context given. Dump all. */
  3507. if (!nla)
  3508. goto dump;
  3509. maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
  3510. nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
  3511. if (IS_ERR(nla))
  3512. return PTR_ERR(nla);
  3513. /* context given, but no name present? */
  3514. if (!nla)
  3515. return -EINVAL;
  3516. resource_name = nla_data(nla);
  3517. if (!*resource_name)
  3518. return -ENODEV;
  3519. resource = drbd_find_resource(resource_name);
  3520. if (!resource)
  3521. return -ENODEV;
  3522. kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
  3523. /* prime iterators, and set "filter" mode mark:
  3524. * only dump this connection. */
  3525. cb->args[0] = (long)resource;
  3526. /* cb->args[1] = 0; passed in this way. */
  3527. cb->args[2] = (long)resource;
  3528. dump:
  3529. return get_one_status(skb, cb);
  3530. }
  3531. int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
  3532. {
  3533. struct drbd_config_context adm_ctx;
  3534. enum drbd_ret_code retcode;
  3535. struct timeout_parms tp;
  3536. int err;
  3537. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  3538. if (!adm_ctx.reply_skb)
  3539. return retcode;
  3540. if (retcode != NO_ERROR)
  3541. goto out;
  3542. tp.timeout_type =
  3543. adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
  3544. test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
  3545. UT_DEFAULT;
  3546. err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
  3547. if (err) {
  3548. nlmsg_free(adm_ctx.reply_skb);
  3549. return err;
  3550. }
  3551. out:
  3552. drbd_adm_finish(&adm_ctx, info, retcode);
  3553. return 0;
  3554. }
  3555. int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
  3556. {
  3557. struct drbd_config_context adm_ctx;
  3558. struct drbd_device *device;
  3559. enum drbd_ret_code retcode;
  3560. struct start_ov_parms parms;
  3561. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  3562. if (!adm_ctx.reply_skb)
  3563. return retcode;
  3564. if (retcode != NO_ERROR)
  3565. goto out;
  3566. device = adm_ctx.device;
  3567. /* resume from last known position, if possible */
  3568. parms.ov_start_sector = device->ov_start_sector;
  3569. parms.ov_stop_sector = ULLONG_MAX;
  3570. if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
  3571. int err = start_ov_parms_from_attrs(&parms, info);
  3572. if (err) {
  3573. retcode = ERR_MANDATORY_TAG;
  3574. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  3575. goto out;
  3576. }
  3577. }
  3578. mutex_lock(&adm_ctx.resource->adm_mutex);
  3579. /* w_make_ov_request expects position to be aligned */
  3580. device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
  3581. device->ov_stop_sector = parms.ov_stop_sector;
  3582. /* If there is still bitmap IO pending, e.g. previous resync or verify
  3583. * just being finished, wait for it before requesting a new resync. */
  3584. drbd_suspend_io(device);
  3585. wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
  3586. retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
  3587. drbd_resume_io(device);
  3588. mutex_unlock(&adm_ctx.resource->adm_mutex);
  3589. out:
  3590. drbd_adm_finish(&adm_ctx, info, retcode);
  3591. return 0;
  3592. }
  3593. int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
  3594. {
  3595. struct drbd_config_context adm_ctx;
  3596. struct drbd_device *device;
  3597. enum drbd_ret_code retcode;
  3598. int skip_initial_sync = 0;
  3599. int err;
  3600. struct new_c_uuid_parms args;
  3601. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  3602. if (!adm_ctx.reply_skb)
  3603. return retcode;
  3604. if (retcode != NO_ERROR)
  3605. goto out_nolock;
  3606. device = adm_ctx.device;
  3607. memset(&args, 0, sizeof(args));
  3608. if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
  3609. err = new_c_uuid_parms_from_attrs(&args, info);
  3610. if (err) {
  3611. retcode = ERR_MANDATORY_TAG;
  3612. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  3613. goto out_nolock;
  3614. }
  3615. }
  3616. mutex_lock(&adm_ctx.resource->adm_mutex);
  3617. mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
  3618. if (!get_ldev(device)) {
  3619. retcode = ERR_NO_DISK;
  3620. goto out;
  3621. }
  3622. /* this is "skip initial sync", assume to be clean */
  3623. if (device->state.conn == C_CONNECTED &&
  3624. first_peer_device(device)->connection->agreed_pro_version >= 90 &&
  3625. device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
  3626. drbd_info(device, "Preparing to skip initial sync\n");
  3627. skip_initial_sync = 1;
  3628. } else if (device->state.conn != C_STANDALONE) {
  3629. retcode = ERR_CONNECTED;
  3630. goto out_dec;
  3631. }
  3632. drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
  3633. drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
  3634. if (args.clear_bm) {
  3635. err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
  3636. "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
  3637. if (err) {
  3638. drbd_err(device, "Writing bitmap failed with %d\n", err);
  3639. retcode = ERR_IO_MD_DISK;
  3640. }
  3641. if (skip_initial_sync) {
  3642. drbd_send_uuids_skip_initial_sync(first_peer_device(device));
  3643. _drbd_uuid_set(device, UI_BITMAP, 0);
  3644. drbd_print_uuids(device, "cleared bitmap UUID");
  3645. spin_lock_irq(&device->resource->req_lock);
  3646. _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
  3647. CS_VERBOSE, NULL);
  3648. spin_unlock_irq(&device->resource->req_lock);
  3649. }
  3650. }
  3651. drbd_md_sync(device);
  3652. out_dec:
  3653. put_ldev(device);
  3654. out:
  3655. mutex_unlock(device->state_mutex);
  3656. mutex_unlock(&adm_ctx.resource->adm_mutex);
  3657. out_nolock:
  3658. drbd_adm_finish(&adm_ctx, info, retcode);
  3659. return 0;
  3660. }
  3661. static enum drbd_ret_code
  3662. drbd_check_resource_name(struct drbd_config_context *adm_ctx)
  3663. {
  3664. const char *name = adm_ctx->resource_name;
  3665. if (!name || !name[0]) {
  3666. drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
  3667. return ERR_MANDATORY_TAG;
  3668. }
  3669. /* if we want to use these in sysfs/configfs/debugfs some day,
  3670. * we must not allow slashes */
  3671. if (strchr(name, '/')) {
  3672. drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
  3673. return ERR_INVALID_REQUEST;
  3674. }
  3675. return NO_ERROR;
  3676. }
  3677. static void resource_to_info(struct resource_info *info,
  3678. struct drbd_resource *resource)
  3679. {
  3680. info->res_role = conn_highest_role(first_connection(resource));
  3681. info->res_susp = resource->susp;
  3682. info->res_susp_nod = resource->susp_nod;
  3683. info->res_susp_fen = resource->susp_fen;
  3684. }
  3685. int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
  3686. {
  3687. struct drbd_connection *connection;
  3688. struct drbd_config_context adm_ctx;
  3689. enum drbd_ret_code retcode;
  3690. struct res_opts res_opts;
  3691. int err;
  3692. retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
  3693. if (!adm_ctx.reply_skb)
  3694. return retcode;
  3695. if (retcode != NO_ERROR)
  3696. goto out;
  3697. set_res_opts_defaults(&res_opts);
  3698. err = res_opts_from_attrs(&res_opts, info);
  3699. if (err && err != -ENOMSG) {
  3700. retcode = ERR_MANDATORY_TAG;
  3701. drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
  3702. goto out;
  3703. }
  3704. retcode = drbd_check_resource_name(&adm_ctx);
  3705. if (retcode != NO_ERROR)
  3706. goto out;
  3707. if (adm_ctx.resource) {
  3708. if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
  3709. retcode = ERR_INVALID_REQUEST;
  3710. drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
  3711. }
  3712. /* else: still NO_ERROR */
  3713. goto out;
  3714. }
  3715. /* not yet safe for genl_family.parallel_ops */
  3716. mutex_lock(&resources_mutex);
  3717. connection = conn_create(adm_ctx.resource_name, &res_opts);
  3718. mutex_unlock(&resources_mutex);
  3719. if (connection) {
  3720. struct resource_info resource_info;
  3721. mutex_lock(&notification_mutex);
  3722. resource_to_info(&resource_info, connection->resource);
  3723. notify_resource_state(NULL, 0, connection->resource,
  3724. &resource_info, NOTIFY_CREATE);
  3725. mutex_unlock(&notification_mutex);
  3726. } else
  3727. retcode = ERR_NOMEM;
  3728. out:
  3729. drbd_adm_finish(&adm_ctx, info, retcode);
  3730. return 0;
  3731. }
  3732. static void device_to_info(struct device_info *info,
  3733. struct drbd_device *device)
  3734. {
  3735. info->dev_disk_state = device->state.disk;
  3736. }
  3737. int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
  3738. {
  3739. struct drbd_config_context adm_ctx;
  3740. struct drbd_genlmsghdr *dh = info->userhdr;
  3741. enum drbd_ret_code retcode;
  3742. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
  3743. if (!adm_ctx.reply_skb)
  3744. return retcode;
  3745. if (retcode != NO_ERROR)
  3746. goto out;
  3747. if (dh->minor > MINORMASK) {
  3748. drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
  3749. retcode = ERR_INVALID_REQUEST;
  3750. goto out;
  3751. }
  3752. if (adm_ctx.volume > DRBD_VOLUME_MAX) {
  3753. drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
  3754. retcode = ERR_INVALID_REQUEST;
  3755. goto out;
  3756. }
  3757. /* drbd_adm_prepare made sure already
  3758. * that first_peer_device(device)->connection and device->vnr match the request. */
  3759. if (adm_ctx.device) {
  3760. if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
  3761. retcode = ERR_MINOR_OR_VOLUME_EXISTS;
  3762. /* else: still NO_ERROR */
  3763. goto out;
  3764. }
  3765. mutex_lock(&adm_ctx.resource->adm_mutex);
  3766. retcode = drbd_create_device(&adm_ctx, dh->minor);
  3767. if (retcode == NO_ERROR) {
  3768. struct drbd_device *device;
  3769. struct drbd_peer_device *peer_device;
  3770. struct device_info info;
  3771. unsigned int peer_devices = 0;
  3772. enum drbd_notification_type flags;
  3773. device = minor_to_device(dh->minor);
  3774. for_each_peer_device(peer_device, device) {
  3775. if (!has_net_conf(peer_device->connection))
  3776. continue;
  3777. peer_devices++;
  3778. }
  3779. device_to_info(&info, device);
  3780. mutex_lock(&notification_mutex);
  3781. flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
  3782. notify_device_state(NULL, 0, device, &info, NOTIFY_CREATE | flags);
  3783. for_each_peer_device(peer_device, device) {
  3784. struct peer_device_info peer_device_info;
  3785. if (!has_net_conf(peer_device->connection))
  3786. continue;
  3787. peer_device_to_info(&peer_device_info, peer_device);
  3788. flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
  3789. notify_peer_device_state(NULL, 0, peer_device, &peer_device_info,
  3790. NOTIFY_CREATE | flags);
  3791. }
  3792. mutex_unlock(&notification_mutex);
  3793. }
  3794. mutex_unlock(&adm_ctx.resource->adm_mutex);
  3795. out:
  3796. drbd_adm_finish(&adm_ctx, info, retcode);
  3797. return 0;
  3798. }
  3799. static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
  3800. {
  3801. struct drbd_peer_device *peer_device;
  3802. if (device->state.disk == D_DISKLESS &&
  3803. /* no need to be device->state.conn == C_STANDALONE &&
  3804. * we may want to delete a minor from a live replication group.
  3805. */
  3806. device->state.role == R_SECONDARY) {
  3807. struct drbd_connection *connection =
  3808. first_connection(device->resource);
  3809. _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
  3810. CS_VERBOSE + CS_WAIT_COMPLETE);
  3811. /* If the state engine hasn't stopped the sender thread yet, we
  3812. * need to flush the sender work queue before generating the
  3813. * DESTROY events here. */
  3814. if (get_t_state(&connection->worker) == RUNNING)
  3815. drbd_flush_workqueue(&connection->sender_work);
  3816. mutex_lock(&notification_mutex);
  3817. for_each_peer_device(peer_device, device) {
  3818. if (!has_net_conf(peer_device->connection))
  3819. continue;
  3820. notify_peer_device_state(NULL, 0, peer_device, NULL,
  3821. NOTIFY_DESTROY | NOTIFY_CONTINUES);
  3822. }
  3823. notify_device_state(NULL, 0, device, NULL, NOTIFY_DESTROY);
  3824. mutex_unlock(&notification_mutex);
  3825. drbd_delete_device(device);
  3826. return NO_ERROR;
  3827. } else
  3828. return ERR_MINOR_CONFIGURED;
  3829. }
  3830. int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
  3831. {
  3832. struct drbd_config_context adm_ctx;
  3833. enum drbd_ret_code retcode;
  3834. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
  3835. if (!adm_ctx.reply_skb)
  3836. return retcode;
  3837. if (retcode != NO_ERROR)
  3838. goto out;
  3839. mutex_lock(&adm_ctx.resource->adm_mutex);
  3840. retcode = adm_del_minor(adm_ctx.device);
  3841. mutex_unlock(&adm_ctx.resource->adm_mutex);
  3842. out:
  3843. drbd_adm_finish(&adm_ctx, info, retcode);
  3844. return 0;
  3845. }
  3846. static int adm_del_resource(struct drbd_resource *resource)
  3847. {
  3848. struct drbd_connection *connection;
  3849. for_each_connection(connection, resource) {
  3850. if (connection->cstate > C_STANDALONE)
  3851. return ERR_NET_CONFIGURED;
  3852. }
  3853. if (!idr_is_empty(&resource->devices))
  3854. return ERR_RES_IN_USE;
  3855. /* The state engine has stopped the sender thread, so we don't
  3856. * need to flush the sender work queue before generating the
  3857. * DESTROY event here. */
  3858. mutex_lock(&notification_mutex);
  3859. notify_resource_state(NULL, 0, resource, NULL, NOTIFY_DESTROY);
  3860. mutex_unlock(&notification_mutex);
  3861. mutex_lock(&resources_mutex);
  3862. list_del_rcu(&resource->resources);
  3863. mutex_unlock(&resources_mutex);
  3864. /* Make sure all threads have actually stopped: state handling only
  3865. * does drbd_thread_stop_nowait(). */
  3866. list_for_each_entry(connection, &resource->connections, connections)
  3867. drbd_thread_stop(&connection->worker);
  3868. synchronize_rcu();
  3869. drbd_free_resource(resource);
  3870. return NO_ERROR;
  3871. }
  3872. int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
  3873. {
  3874. struct drbd_config_context adm_ctx;
  3875. struct drbd_resource *resource;
  3876. struct drbd_connection *connection;
  3877. struct drbd_device *device;
  3878. int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
  3879. unsigned i;
  3880. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
  3881. if (!adm_ctx.reply_skb)
  3882. return retcode;
  3883. if (retcode != NO_ERROR)
  3884. goto finish;
  3885. resource = adm_ctx.resource;
  3886. mutex_lock(&resource->adm_mutex);
  3887. /* demote */
  3888. for_each_connection(connection, resource) {
  3889. struct drbd_peer_device *peer_device;
  3890. idr_for_each_entry(&connection->peer_devices, peer_device, i) {
  3891. retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
  3892. if (retcode < SS_SUCCESS) {
  3893. drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
  3894. goto out;
  3895. }
  3896. }
  3897. retcode = conn_try_disconnect(connection, 0);
  3898. if (retcode < SS_SUCCESS) {
  3899. drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
  3900. goto out;
  3901. }
  3902. }
  3903. /* detach */
  3904. idr_for_each_entry(&resource->devices, device, i) {
  3905. retcode = adm_detach(device, 0);
  3906. if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
  3907. drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
  3908. goto out;
  3909. }
  3910. }
  3911. /* delete volumes */
  3912. idr_for_each_entry(&resource->devices, device, i) {
  3913. retcode = adm_del_minor(device);
  3914. if (retcode != NO_ERROR) {
  3915. /* "can not happen" */
  3916. drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
  3917. goto out;
  3918. }
  3919. }
  3920. retcode = adm_del_resource(resource);
  3921. out:
  3922. mutex_unlock(&resource->adm_mutex);
  3923. finish:
  3924. drbd_adm_finish(&adm_ctx, info, retcode);
  3925. return 0;
  3926. }
  3927. int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
  3928. {
  3929. struct drbd_config_context adm_ctx;
  3930. struct drbd_resource *resource;
  3931. enum drbd_ret_code retcode;
  3932. retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
  3933. if (!adm_ctx.reply_skb)
  3934. return retcode;
  3935. if (retcode != NO_ERROR)
  3936. goto finish;
  3937. resource = adm_ctx.resource;
  3938. mutex_lock(&resource->adm_mutex);
  3939. retcode = adm_del_resource(resource);
  3940. mutex_unlock(&resource->adm_mutex);
  3941. finish:
  3942. drbd_adm_finish(&adm_ctx, info, retcode);
  3943. return 0;
  3944. }
  3945. void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
  3946. {
  3947. struct sk_buff *msg;
  3948. struct drbd_genlmsghdr *d_out;
  3949. unsigned seq;
  3950. int err = -ENOMEM;
  3951. seq = atomic_inc_return(&drbd_genl_seq);
  3952. msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
  3953. if (!msg)
  3954. goto failed;
  3955. err = -EMSGSIZE;
  3956. d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
  3957. if (!d_out) /* cannot happen, but anyways. */
  3958. goto nla_put_failure;
  3959. d_out->minor = device_to_minor(device);
  3960. d_out->ret_code = NO_ERROR;
  3961. if (nla_put_status_info(msg, device, sib))
  3962. goto nla_put_failure;
  3963. genlmsg_end(msg, d_out);
  3964. err = drbd_genl_multicast_events(msg, GFP_NOWAIT);
  3965. /* msg has been consumed or freed in netlink_broadcast() */
  3966. if (err && err != -ESRCH)
  3967. goto failed;
  3968. return;
  3969. nla_put_failure:
  3970. nlmsg_free(msg);
  3971. failed:
  3972. drbd_err(device, "Error %d while broadcasting event. "
  3973. "Event seq:%u sib_reason:%u\n",
  3974. err, seq, sib->sib_reason);
  3975. }
  3976. static int nla_put_notification_header(struct sk_buff *msg,
  3977. enum drbd_notification_type type)
  3978. {
  3979. struct drbd_notification_header nh = {
  3980. .nh_type = type,
  3981. };
  3982. return drbd_notification_header_to_skb(msg, &nh, true);
  3983. }
  3984. void notify_resource_state(struct sk_buff *skb,
  3985. unsigned int seq,
  3986. struct drbd_resource *resource,
  3987. struct resource_info *resource_info,
  3988. enum drbd_notification_type type)
  3989. {
  3990. struct resource_statistics resource_statistics;
  3991. struct drbd_genlmsghdr *dh;
  3992. bool multicast = false;
  3993. int err;
  3994. if (!skb) {
  3995. seq = atomic_inc_return(&notify_genl_seq);
  3996. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
  3997. err = -ENOMEM;
  3998. if (!skb)
  3999. goto failed;
  4000. multicast = true;
  4001. }
  4002. err = -EMSGSIZE;
  4003. dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_RESOURCE_STATE);
  4004. if (!dh)
  4005. goto nla_put_failure;
  4006. dh->minor = -1U;
  4007. dh->ret_code = NO_ERROR;
  4008. if (nla_put_drbd_cfg_context(skb, resource, NULL, NULL) ||
  4009. nla_put_notification_header(skb, type) ||
  4010. ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
  4011. resource_info_to_skb(skb, resource_info, true)))
  4012. goto nla_put_failure;
  4013. resource_statistics.res_stat_write_ordering = resource->write_ordering;
  4014. err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
  4015. if (err)
  4016. goto nla_put_failure;
  4017. genlmsg_end(skb, dh);
  4018. if (multicast) {
  4019. err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
  4020. /* skb has been consumed or freed in netlink_broadcast() */
  4021. if (err && err != -ESRCH)
  4022. goto failed;
  4023. }
  4024. return;
  4025. nla_put_failure:
  4026. nlmsg_free(skb);
  4027. failed:
  4028. drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
  4029. err, seq);
  4030. }
  4031. void notify_device_state(struct sk_buff *skb,
  4032. unsigned int seq,
  4033. struct drbd_device *device,
  4034. struct device_info *device_info,
  4035. enum drbd_notification_type type)
  4036. {
  4037. struct device_statistics device_statistics;
  4038. struct drbd_genlmsghdr *dh;
  4039. bool multicast = false;
  4040. int err;
  4041. if (!skb) {
  4042. seq = atomic_inc_return(&notify_genl_seq);
  4043. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
  4044. err = -ENOMEM;
  4045. if (!skb)
  4046. goto failed;
  4047. multicast = true;
  4048. }
  4049. err = -EMSGSIZE;
  4050. dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_DEVICE_STATE);
  4051. if (!dh)
  4052. goto nla_put_failure;
  4053. dh->minor = device->minor;
  4054. dh->ret_code = NO_ERROR;
  4055. if (nla_put_drbd_cfg_context(skb, device->resource, NULL, device) ||
  4056. nla_put_notification_header(skb, type) ||
  4057. ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
  4058. device_info_to_skb(skb, device_info, true)))
  4059. goto nla_put_failure;
  4060. device_to_statistics(&device_statistics, device);
  4061. device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
  4062. genlmsg_end(skb, dh);
  4063. if (multicast) {
  4064. err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
  4065. /* skb has been consumed or freed in netlink_broadcast() */
  4066. if (err && err != -ESRCH)
  4067. goto failed;
  4068. }
  4069. return;
  4070. nla_put_failure:
  4071. nlmsg_free(skb);
  4072. failed:
  4073. drbd_err(device, "Error %d while broadcasting event. Event seq:%u\n",
  4074. err, seq);
  4075. }
  4076. void notify_connection_state(struct sk_buff *skb,
  4077. unsigned int seq,
  4078. struct drbd_connection *connection,
  4079. struct connection_info *connection_info,
  4080. enum drbd_notification_type type)
  4081. {
  4082. struct connection_statistics connection_statistics;
  4083. struct drbd_genlmsghdr *dh;
  4084. bool multicast = false;
  4085. int err;
  4086. if (!skb) {
  4087. seq = atomic_inc_return(&notify_genl_seq);
  4088. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
  4089. err = -ENOMEM;
  4090. if (!skb)
  4091. goto failed;
  4092. multicast = true;
  4093. }
  4094. err = -EMSGSIZE;
  4095. dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_CONNECTION_STATE);
  4096. if (!dh)
  4097. goto nla_put_failure;
  4098. dh->minor = -1U;
  4099. dh->ret_code = NO_ERROR;
  4100. if (nla_put_drbd_cfg_context(skb, connection->resource, connection, NULL) ||
  4101. nla_put_notification_header(skb, type) ||
  4102. ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
  4103. connection_info_to_skb(skb, connection_info, true)))
  4104. goto nla_put_failure;
  4105. connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
  4106. connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
  4107. genlmsg_end(skb, dh);
  4108. if (multicast) {
  4109. err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
  4110. /* skb has been consumed or freed in netlink_broadcast() */
  4111. if (err && err != -ESRCH)
  4112. goto failed;
  4113. }
  4114. return;
  4115. nla_put_failure:
  4116. nlmsg_free(skb);
  4117. failed:
  4118. drbd_err(connection, "Error %d while broadcasting event. Event seq:%u\n",
  4119. err, seq);
  4120. }
  4121. void notify_peer_device_state(struct sk_buff *skb,
  4122. unsigned int seq,
  4123. struct drbd_peer_device *peer_device,
  4124. struct peer_device_info *peer_device_info,
  4125. enum drbd_notification_type type)
  4126. {
  4127. struct peer_device_statistics peer_device_statistics;
  4128. struct drbd_resource *resource = peer_device->device->resource;
  4129. struct drbd_genlmsghdr *dh;
  4130. bool multicast = false;
  4131. int err;
  4132. if (!skb) {
  4133. seq = atomic_inc_return(&notify_genl_seq);
  4134. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
  4135. err = -ENOMEM;
  4136. if (!skb)
  4137. goto failed;
  4138. multicast = true;
  4139. }
  4140. err = -EMSGSIZE;
  4141. dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_PEER_DEVICE_STATE);
  4142. if (!dh)
  4143. goto nla_put_failure;
  4144. dh->minor = -1U;
  4145. dh->ret_code = NO_ERROR;
  4146. if (nla_put_drbd_cfg_context(skb, resource, peer_device->connection, peer_device->device) ||
  4147. nla_put_notification_header(skb, type) ||
  4148. ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
  4149. peer_device_info_to_skb(skb, peer_device_info, true)))
  4150. goto nla_put_failure;
  4151. peer_device_to_statistics(&peer_device_statistics, peer_device);
  4152. peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
  4153. genlmsg_end(skb, dh);
  4154. if (multicast) {
  4155. err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
  4156. /* skb has been consumed or freed in netlink_broadcast() */
  4157. if (err && err != -ESRCH)
  4158. goto failed;
  4159. }
  4160. return;
  4161. nla_put_failure:
  4162. nlmsg_free(skb);
  4163. failed:
  4164. drbd_err(peer_device, "Error %d while broadcasting event. Event seq:%u\n",
  4165. err, seq);
  4166. }
  4167. void notify_helper(enum drbd_notification_type type,
  4168. struct drbd_device *device, struct drbd_connection *connection,
  4169. const char *name, int status)
  4170. {
  4171. struct drbd_resource *resource = device ? device->resource : connection->resource;
  4172. struct drbd_helper_info helper_info;
  4173. unsigned int seq = atomic_inc_return(&notify_genl_seq);
  4174. struct sk_buff *skb = NULL;
  4175. struct drbd_genlmsghdr *dh;
  4176. int err;
  4177. strlcpy(helper_info.helper_name, name, sizeof(helper_info.helper_name));
  4178. helper_info.helper_name_len = min(strlen(name), sizeof(helper_info.helper_name));
  4179. helper_info.helper_status = status;
  4180. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
  4181. err = -ENOMEM;
  4182. if (!skb)
  4183. goto fail;
  4184. err = -EMSGSIZE;
  4185. dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_HELPER);
  4186. if (!dh)
  4187. goto fail;
  4188. dh->minor = device ? device->minor : -1;
  4189. dh->ret_code = NO_ERROR;
  4190. mutex_lock(&notification_mutex);
  4191. if (nla_put_drbd_cfg_context(skb, resource, connection, device) ||
  4192. nla_put_notification_header(skb, type) ||
  4193. drbd_helper_info_to_skb(skb, &helper_info, true))
  4194. goto unlock_fail;
  4195. genlmsg_end(skb, dh);
  4196. err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
  4197. skb = NULL;
  4198. /* skb has been consumed or freed in netlink_broadcast() */
  4199. if (err && err != -ESRCH)
  4200. goto unlock_fail;
  4201. mutex_unlock(&notification_mutex);
  4202. return;
  4203. unlock_fail:
  4204. mutex_unlock(&notification_mutex);
  4205. fail:
  4206. nlmsg_free(skb);
  4207. drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
  4208. err, seq);
  4209. }
  4210. static void notify_initial_state_done(struct sk_buff *skb, unsigned int seq)
  4211. {
  4212. struct drbd_genlmsghdr *dh;
  4213. int err;
  4214. err = -EMSGSIZE;
  4215. dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_INITIAL_STATE_DONE);
  4216. if (!dh)
  4217. goto nla_put_failure;
  4218. dh->minor = -1U;
  4219. dh->ret_code = NO_ERROR;
  4220. if (nla_put_notification_header(skb, NOTIFY_EXISTS))
  4221. goto nla_put_failure;
  4222. genlmsg_end(skb, dh);
  4223. return;
  4224. nla_put_failure:
  4225. nlmsg_free(skb);
  4226. pr_err("Error %d sending event. Event seq:%u\n", err, seq);
  4227. }
  4228. static void free_state_changes(struct list_head *list)
  4229. {
  4230. while (!list_empty(list)) {
  4231. struct drbd_state_change *state_change =
  4232. list_first_entry(list, struct drbd_state_change, list);
  4233. list_del(&state_change->list);
  4234. forget_state_change(state_change);
  4235. }
  4236. }
  4237. static unsigned int notifications_for_state_change(struct drbd_state_change *state_change)
  4238. {
  4239. return 1 +
  4240. state_change->n_connections +
  4241. state_change->n_devices +
  4242. state_change->n_devices * state_change->n_connections;
  4243. }
  4244. static int get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
  4245. {
  4246. struct drbd_state_change *state_change = (struct drbd_state_change *)cb->args[0];
  4247. unsigned int seq = cb->args[2];
  4248. unsigned int n;
  4249. enum drbd_notification_type flags = 0;
  4250. /* There is no need for taking notification_mutex here: it doesn't
  4251. matter if the initial state events mix with later state chage
  4252. events; we can always tell the events apart by the NOTIFY_EXISTS
  4253. flag. */
  4254. cb->args[5]--;
  4255. if (cb->args[5] == 1) {
  4256. notify_initial_state_done(skb, seq);
  4257. goto out;
  4258. }
  4259. n = cb->args[4]++;
  4260. if (cb->args[4] < cb->args[3])
  4261. flags |= NOTIFY_CONTINUES;
  4262. if (n < 1) {
  4263. notify_resource_state_change(skb, seq, state_change->resource,
  4264. NOTIFY_EXISTS | flags);
  4265. goto next;
  4266. }
  4267. n--;
  4268. if (n < state_change->n_connections) {
  4269. notify_connection_state_change(skb, seq, &state_change->connections[n],
  4270. NOTIFY_EXISTS | flags);
  4271. goto next;
  4272. }
  4273. n -= state_change->n_connections;
  4274. if (n < state_change->n_devices) {
  4275. notify_device_state_change(skb, seq, &state_change->devices[n],
  4276. NOTIFY_EXISTS | flags);
  4277. goto next;
  4278. }
  4279. n -= state_change->n_devices;
  4280. if (n < state_change->n_devices * state_change->n_connections) {
  4281. notify_peer_device_state_change(skb, seq, &state_change->peer_devices[n],
  4282. NOTIFY_EXISTS | flags);
  4283. goto next;
  4284. }
  4285. next:
  4286. if (cb->args[4] == cb->args[3]) {
  4287. struct drbd_state_change *next_state_change =
  4288. list_entry(state_change->list.next,
  4289. struct drbd_state_change, list);
  4290. cb->args[0] = (long)next_state_change;
  4291. cb->args[3] = notifications_for_state_change(next_state_change);
  4292. cb->args[4] = 0;
  4293. }
  4294. out:
  4295. return skb->len;
  4296. }
  4297. int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
  4298. {
  4299. struct drbd_resource *resource;
  4300. LIST_HEAD(head);
  4301. if (cb->args[5] >= 1) {
  4302. if (cb->args[5] > 1)
  4303. return get_initial_state(skb, cb);
  4304. if (cb->args[0]) {
  4305. struct drbd_state_change *state_change =
  4306. (struct drbd_state_change *)cb->args[0];
  4307. /* connect list to head */
  4308. list_add(&head, &state_change->list);
  4309. free_state_changes(&head);
  4310. }
  4311. return 0;
  4312. }
  4313. cb->args[5] = 2; /* number of iterations */
  4314. mutex_lock(&resources_mutex);
  4315. for_each_resource(resource, &drbd_resources) {
  4316. struct drbd_state_change *state_change;
  4317. state_change = remember_old_state(resource, GFP_KERNEL);
  4318. if (!state_change) {
  4319. if (!list_empty(&head))
  4320. free_state_changes(&head);
  4321. mutex_unlock(&resources_mutex);
  4322. return -ENOMEM;
  4323. }
  4324. copy_old_to_new_state_change(state_change);
  4325. list_add_tail(&state_change->list, &head);
  4326. cb->args[5] += notifications_for_state_change(state_change);
  4327. }
  4328. mutex_unlock(&resources_mutex);
  4329. if (!list_empty(&head)) {
  4330. struct drbd_state_change *state_change =
  4331. list_entry(head.next, struct drbd_state_change, list);
  4332. cb->args[0] = (long)state_change;
  4333. cb->args[3] = notifications_for_state_change(state_change);
  4334. list_del(&head); /* detach list from head */
  4335. }
  4336. cb->args[2] = cb->nlh->nlmsg_seq;
  4337. return get_initial_state(skb, cb);
  4338. }