drbd_nl.c 147 KB

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