drbd_nl.c 146 KB

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