drbd_nl.c 144 KB

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