osd_client.c 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/module.h>
  4. #include <linux/err.h>
  5. #include <linux/highmem.h>
  6. #include <linux/mm.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #ifdef CONFIG_BLOCK
  11. #include <linux/bio.h>
  12. #endif
  13. #include <linux/ceph/ceph_features.h>
  14. #include <linux/ceph/libceph.h>
  15. #include <linux/ceph/osd_client.h>
  16. #include <linux/ceph/messenger.h>
  17. #include <linux/ceph/decode.h>
  18. #include <linux/ceph/auth.h>
  19. #include <linux/ceph/pagelist.h>
  20. #include <linux/ceph/striper.h>
  21. #define OSD_OPREPLY_FRONT_LEN 512
  22. static struct kmem_cache *ceph_osd_request_cache;
  23. static const struct ceph_connection_operations osd_con_ops;
  24. /*
  25. * Implement client access to distributed object storage cluster.
  26. *
  27. * All data objects are stored within a cluster/cloud of OSDs, or
  28. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  29. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  30. * remote daemons serving up and coordinating consistent and safe
  31. * access to storage.
  32. *
  33. * Cluster membership and the mapping of data objects onto storage devices
  34. * are described by the osd map.
  35. *
  36. * We keep track of pending OSD requests (read, write), resubmit
  37. * requests to different OSDs when the cluster topology/data layout
  38. * change, or retry the affected requests when the communications
  39. * channel with an OSD is reset.
  40. */
  41. static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  42. static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  43. static void link_linger(struct ceph_osd *osd,
  44. struct ceph_osd_linger_request *lreq);
  45. static void unlink_linger(struct ceph_osd *osd,
  46. struct ceph_osd_linger_request *lreq);
  47. static void clear_backoffs(struct ceph_osd *osd);
  48. #if 1
  49. static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
  50. {
  51. bool wrlocked = true;
  52. if (unlikely(down_read_trylock(sem))) {
  53. wrlocked = false;
  54. up_read(sem);
  55. }
  56. return wrlocked;
  57. }
  58. static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
  59. {
  60. WARN_ON(!rwsem_is_locked(&osdc->lock));
  61. }
  62. static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
  63. {
  64. WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
  65. }
  66. static inline void verify_osd_locked(struct ceph_osd *osd)
  67. {
  68. struct ceph_osd_client *osdc = osd->o_osdc;
  69. WARN_ON(!(mutex_is_locked(&osd->lock) &&
  70. rwsem_is_locked(&osdc->lock)) &&
  71. !rwsem_is_wrlocked(&osdc->lock));
  72. }
  73. static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
  74. {
  75. WARN_ON(!mutex_is_locked(&lreq->lock));
  76. }
  77. #else
  78. static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
  79. static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
  80. static inline void verify_osd_locked(struct ceph_osd *osd) { }
  81. static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
  82. #endif
  83. /*
  84. * calculate the mapping of a file extent onto an object, and fill out the
  85. * request accordingly. shorten extent as necessary if it crosses an
  86. * object boundary.
  87. *
  88. * fill osd op in request message.
  89. */
  90. static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
  91. u64 *objnum, u64 *objoff, u64 *objlen)
  92. {
  93. u64 orig_len = *plen;
  94. u32 xlen;
  95. /* object extent? */
  96. ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
  97. objoff, &xlen);
  98. *objlen = xlen;
  99. if (*objlen < orig_len) {
  100. *plen = *objlen;
  101. dout(" skipping last %llu, final file extent %llu~%llu\n",
  102. orig_len - *plen, off, *plen);
  103. }
  104. dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
  105. return 0;
  106. }
  107. static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
  108. {
  109. memset(osd_data, 0, sizeof (*osd_data));
  110. osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
  111. }
  112. /*
  113. * Consumes @pages if @own_pages is true.
  114. */
  115. static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
  116. struct page **pages, u64 length, u32 alignment,
  117. bool pages_from_pool, bool own_pages)
  118. {
  119. osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
  120. osd_data->pages = pages;
  121. osd_data->length = length;
  122. osd_data->alignment = alignment;
  123. osd_data->pages_from_pool = pages_from_pool;
  124. osd_data->own_pages = own_pages;
  125. }
  126. /*
  127. * Consumes a ref on @pagelist.
  128. */
  129. static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
  130. struct ceph_pagelist *pagelist)
  131. {
  132. osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
  133. osd_data->pagelist = pagelist;
  134. }
  135. #ifdef CONFIG_BLOCK
  136. static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
  137. struct ceph_bio_iter *bio_pos,
  138. u32 bio_length)
  139. {
  140. osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
  141. osd_data->bio_pos = *bio_pos;
  142. osd_data->bio_length = bio_length;
  143. }
  144. #endif /* CONFIG_BLOCK */
  145. static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
  146. struct ceph_bvec_iter *bvec_pos,
  147. u32 num_bvecs)
  148. {
  149. osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
  150. osd_data->bvec_pos = *bvec_pos;
  151. osd_data->num_bvecs = num_bvecs;
  152. }
  153. static struct ceph_osd_data *
  154. osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
  155. {
  156. BUG_ON(which >= osd_req->r_num_ops);
  157. return &osd_req->r_ops[which].raw_data_in;
  158. }
  159. struct ceph_osd_data *
  160. osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
  161. unsigned int which)
  162. {
  163. return osd_req_op_data(osd_req, which, extent, osd_data);
  164. }
  165. EXPORT_SYMBOL(osd_req_op_extent_osd_data);
  166. void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
  167. unsigned int which, struct page **pages,
  168. u64 length, u32 alignment,
  169. bool pages_from_pool, bool own_pages)
  170. {
  171. struct ceph_osd_data *osd_data;
  172. osd_data = osd_req_op_raw_data_in(osd_req, which);
  173. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  174. pages_from_pool, own_pages);
  175. }
  176. EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
  177. void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
  178. unsigned int which, struct page **pages,
  179. u64 length, u32 alignment,
  180. bool pages_from_pool, bool own_pages)
  181. {
  182. struct ceph_osd_data *osd_data;
  183. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  184. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  185. pages_from_pool, own_pages);
  186. }
  187. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
  188. void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
  189. unsigned int which, struct ceph_pagelist *pagelist)
  190. {
  191. struct ceph_osd_data *osd_data;
  192. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  193. ceph_osd_data_pagelist_init(osd_data, pagelist);
  194. }
  195. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
  196. #ifdef CONFIG_BLOCK
  197. void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
  198. unsigned int which,
  199. struct ceph_bio_iter *bio_pos,
  200. u32 bio_length)
  201. {
  202. struct ceph_osd_data *osd_data;
  203. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  204. ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
  205. }
  206. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
  207. #endif /* CONFIG_BLOCK */
  208. void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
  209. unsigned int which,
  210. struct bio_vec *bvecs, u32 num_bvecs,
  211. u32 bytes)
  212. {
  213. struct ceph_osd_data *osd_data;
  214. struct ceph_bvec_iter it = {
  215. .bvecs = bvecs,
  216. .iter = { .bi_size = bytes },
  217. };
  218. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  219. ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
  220. }
  221. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
  222. void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
  223. unsigned int which,
  224. struct ceph_bvec_iter *bvec_pos)
  225. {
  226. struct ceph_osd_data *osd_data;
  227. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  228. ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
  229. }
  230. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
  231. static void osd_req_op_cls_request_info_pagelist(
  232. struct ceph_osd_request *osd_req,
  233. unsigned int which, struct ceph_pagelist *pagelist)
  234. {
  235. struct ceph_osd_data *osd_data;
  236. osd_data = osd_req_op_data(osd_req, which, cls, request_info);
  237. ceph_osd_data_pagelist_init(osd_data, pagelist);
  238. }
  239. void osd_req_op_cls_request_data_pagelist(
  240. struct ceph_osd_request *osd_req,
  241. unsigned int which, struct ceph_pagelist *pagelist)
  242. {
  243. struct ceph_osd_data *osd_data;
  244. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  245. ceph_osd_data_pagelist_init(osd_data, pagelist);
  246. osd_req->r_ops[which].cls.indata_len += pagelist->length;
  247. osd_req->r_ops[which].indata_len += pagelist->length;
  248. }
  249. EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
  250. void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
  251. unsigned int which, struct page **pages, u64 length,
  252. u32 alignment, bool pages_from_pool, bool own_pages)
  253. {
  254. struct ceph_osd_data *osd_data;
  255. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  256. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  257. pages_from_pool, own_pages);
  258. osd_req->r_ops[which].cls.indata_len += length;
  259. osd_req->r_ops[which].indata_len += length;
  260. }
  261. EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
  262. void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
  263. unsigned int which,
  264. struct bio_vec *bvecs, u32 num_bvecs,
  265. u32 bytes)
  266. {
  267. struct ceph_osd_data *osd_data;
  268. struct ceph_bvec_iter it = {
  269. .bvecs = bvecs,
  270. .iter = { .bi_size = bytes },
  271. };
  272. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  273. ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
  274. osd_req->r_ops[which].cls.indata_len += bytes;
  275. osd_req->r_ops[which].indata_len += bytes;
  276. }
  277. EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
  278. void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
  279. unsigned int which, struct page **pages, u64 length,
  280. u32 alignment, bool pages_from_pool, bool own_pages)
  281. {
  282. struct ceph_osd_data *osd_data;
  283. osd_data = osd_req_op_data(osd_req, which, cls, response_data);
  284. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  285. pages_from_pool, own_pages);
  286. }
  287. EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
  288. static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
  289. {
  290. switch (osd_data->type) {
  291. case CEPH_OSD_DATA_TYPE_NONE:
  292. return 0;
  293. case CEPH_OSD_DATA_TYPE_PAGES:
  294. return osd_data->length;
  295. case CEPH_OSD_DATA_TYPE_PAGELIST:
  296. return (u64)osd_data->pagelist->length;
  297. #ifdef CONFIG_BLOCK
  298. case CEPH_OSD_DATA_TYPE_BIO:
  299. return (u64)osd_data->bio_length;
  300. #endif /* CONFIG_BLOCK */
  301. case CEPH_OSD_DATA_TYPE_BVECS:
  302. return osd_data->bvec_pos.iter.bi_size;
  303. default:
  304. WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
  305. return 0;
  306. }
  307. }
  308. static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
  309. {
  310. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
  311. int num_pages;
  312. num_pages = calc_pages_for((u64)osd_data->alignment,
  313. (u64)osd_data->length);
  314. ceph_release_page_vector(osd_data->pages, num_pages);
  315. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
  316. ceph_pagelist_release(osd_data->pagelist);
  317. }
  318. ceph_osd_data_init(osd_data);
  319. }
  320. static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
  321. unsigned int which)
  322. {
  323. struct ceph_osd_req_op *op;
  324. BUG_ON(which >= osd_req->r_num_ops);
  325. op = &osd_req->r_ops[which];
  326. switch (op->op) {
  327. case CEPH_OSD_OP_READ:
  328. case CEPH_OSD_OP_WRITE:
  329. case CEPH_OSD_OP_WRITEFULL:
  330. ceph_osd_data_release(&op->extent.osd_data);
  331. break;
  332. case CEPH_OSD_OP_CALL:
  333. ceph_osd_data_release(&op->cls.request_info);
  334. ceph_osd_data_release(&op->cls.request_data);
  335. ceph_osd_data_release(&op->cls.response_data);
  336. break;
  337. case CEPH_OSD_OP_SETXATTR:
  338. case CEPH_OSD_OP_CMPXATTR:
  339. ceph_osd_data_release(&op->xattr.osd_data);
  340. break;
  341. case CEPH_OSD_OP_STAT:
  342. ceph_osd_data_release(&op->raw_data_in);
  343. break;
  344. case CEPH_OSD_OP_NOTIFY_ACK:
  345. ceph_osd_data_release(&op->notify_ack.request_data);
  346. break;
  347. case CEPH_OSD_OP_NOTIFY:
  348. ceph_osd_data_release(&op->notify.request_data);
  349. ceph_osd_data_release(&op->notify.response_data);
  350. break;
  351. case CEPH_OSD_OP_LIST_WATCHERS:
  352. ceph_osd_data_release(&op->list_watchers.response_data);
  353. break;
  354. case CEPH_OSD_OP_COPY_FROM2:
  355. ceph_osd_data_release(&op->copy_from.osd_data);
  356. break;
  357. default:
  358. break;
  359. }
  360. }
  361. /*
  362. * Assumes @t is zero-initialized.
  363. */
  364. static void target_init(struct ceph_osd_request_target *t)
  365. {
  366. ceph_oid_init(&t->base_oid);
  367. ceph_oloc_init(&t->base_oloc);
  368. ceph_oid_init(&t->target_oid);
  369. ceph_oloc_init(&t->target_oloc);
  370. ceph_osds_init(&t->acting);
  371. ceph_osds_init(&t->up);
  372. t->size = -1;
  373. t->min_size = -1;
  374. t->osd = CEPH_HOMELESS_OSD;
  375. }
  376. static void target_copy(struct ceph_osd_request_target *dest,
  377. const struct ceph_osd_request_target *src)
  378. {
  379. ceph_oid_copy(&dest->base_oid, &src->base_oid);
  380. ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
  381. ceph_oid_copy(&dest->target_oid, &src->target_oid);
  382. ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
  383. dest->pgid = src->pgid; /* struct */
  384. dest->spgid = src->spgid; /* struct */
  385. dest->pg_num = src->pg_num;
  386. dest->pg_num_mask = src->pg_num_mask;
  387. ceph_osds_copy(&dest->acting, &src->acting);
  388. ceph_osds_copy(&dest->up, &src->up);
  389. dest->size = src->size;
  390. dest->min_size = src->min_size;
  391. dest->sort_bitwise = src->sort_bitwise;
  392. dest->recovery_deletes = src->recovery_deletes;
  393. dest->flags = src->flags;
  394. dest->used_replica = src->used_replica;
  395. dest->paused = src->paused;
  396. dest->epoch = src->epoch;
  397. dest->last_force_resend = src->last_force_resend;
  398. dest->osd = src->osd;
  399. }
  400. static void target_destroy(struct ceph_osd_request_target *t)
  401. {
  402. ceph_oid_destroy(&t->base_oid);
  403. ceph_oloc_destroy(&t->base_oloc);
  404. ceph_oid_destroy(&t->target_oid);
  405. ceph_oloc_destroy(&t->target_oloc);
  406. }
  407. /*
  408. * requests
  409. */
  410. static void request_release_checks(struct ceph_osd_request *req)
  411. {
  412. WARN_ON(!RB_EMPTY_NODE(&req->r_node));
  413. WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
  414. WARN_ON(!list_empty(&req->r_private_item));
  415. WARN_ON(req->r_osd);
  416. }
  417. static void ceph_osdc_release_request(struct kref *kref)
  418. {
  419. struct ceph_osd_request *req = container_of(kref,
  420. struct ceph_osd_request, r_kref);
  421. unsigned int which;
  422. dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
  423. req->r_request, req->r_reply);
  424. request_release_checks(req);
  425. if (req->r_request)
  426. ceph_msg_put(req->r_request);
  427. if (req->r_reply)
  428. ceph_msg_put(req->r_reply);
  429. for (which = 0; which < req->r_num_ops; which++)
  430. osd_req_op_data_release(req, which);
  431. target_destroy(&req->r_t);
  432. ceph_put_snap_context(req->r_snapc);
  433. if (req->r_mempool)
  434. mempool_free(req, req->r_osdc->req_mempool);
  435. else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
  436. kmem_cache_free(ceph_osd_request_cache, req);
  437. else
  438. kfree(req);
  439. }
  440. void ceph_osdc_get_request(struct ceph_osd_request *req)
  441. {
  442. dout("%s %p (was %d)\n", __func__, req,
  443. kref_read(&req->r_kref));
  444. kref_get(&req->r_kref);
  445. }
  446. EXPORT_SYMBOL(ceph_osdc_get_request);
  447. void ceph_osdc_put_request(struct ceph_osd_request *req)
  448. {
  449. if (req) {
  450. dout("%s %p (was %d)\n", __func__, req,
  451. kref_read(&req->r_kref));
  452. kref_put(&req->r_kref, ceph_osdc_release_request);
  453. }
  454. }
  455. EXPORT_SYMBOL(ceph_osdc_put_request);
  456. static void request_init(struct ceph_osd_request *req)
  457. {
  458. /* req only, each op is zeroed in osd_req_op_init() */
  459. memset(req, 0, sizeof(*req));
  460. kref_init(&req->r_kref);
  461. init_completion(&req->r_completion);
  462. RB_CLEAR_NODE(&req->r_node);
  463. RB_CLEAR_NODE(&req->r_mc_node);
  464. INIT_LIST_HEAD(&req->r_private_item);
  465. target_init(&req->r_t);
  466. }
  467. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  468. struct ceph_snap_context *snapc,
  469. unsigned int num_ops,
  470. bool use_mempool,
  471. gfp_t gfp_flags)
  472. {
  473. struct ceph_osd_request *req;
  474. if (use_mempool) {
  475. BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
  476. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  477. } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
  478. req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
  479. } else {
  480. BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
  481. req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
  482. }
  483. if (unlikely(!req))
  484. return NULL;
  485. request_init(req);
  486. req->r_osdc = osdc;
  487. req->r_mempool = use_mempool;
  488. req->r_num_ops = num_ops;
  489. req->r_snapid = CEPH_NOSNAP;
  490. req->r_snapc = ceph_get_snap_context(snapc);
  491. dout("%s req %p\n", __func__, req);
  492. return req;
  493. }
  494. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  495. static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
  496. {
  497. return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
  498. }
  499. static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
  500. int num_request_data_items,
  501. int num_reply_data_items)
  502. {
  503. struct ceph_osd_client *osdc = req->r_osdc;
  504. struct ceph_msg *msg;
  505. int msg_size;
  506. WARN_ON(req->r_request || req->r_reply);
  507. WARN_ON(ceph_oid_empty(&req->r_base_oid));
  508. WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
  509. /* create request message */
  510. msg_size = CEPH_ENCODING_START_BLK_LEN +
  511. CEPH_PGID_ENCODING_LEN + 1; /* spgid */
  512. msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
  513. msg_size += CEPH_ENCODING_START_BLK_LEN +
  514. sizeof(struct ceph_osd_reqid); /* reqid */
  515. msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
  516. msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
  517. msg_size += CEPH_ENCODING_START_BLK_LEN +
  518. ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
  519. msg_size += 4 + req->r_base_oid.name_len; /* oid */
  520. msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
  521. msg_size += 8; /* snapid */
  522. msg_size += 8; /* snap_seq */
  523. msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
  524. msg_size += 4 + 8; /* retry_attempt, features */
  525. if (req->r_mempool)
  526. msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
  527. num_request_data_items);
  528. else
  529. msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
  530. num_request_data_items, gfp, true);
  531. if (!msg)
  532. return -ENOMEM;
  533. memset(msg->front.iov_base, 0, msg->front.iov_len);
  534. req->r_request = msg;
  535. /* create reply message */
  536. msg_size = OSD_OPREPLY_FRONT_LEN;
  537. msg_size += req->r_base_oid.name_len;
  538. msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
  539. if (req->r_mempool)
  540. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
  541. num_reply_data_items);
  542. else
  543. msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
  544. num_reply_data_items, gfp, true);
  545. if (!msg)
  546. return -ENOMEM;
  547. req->r_reply = msg;
  548. return 0;
  549. }
  550. static bool osd_req_opcode_valid(u16 opcode)
  551. {
  552. switch (opcode) {
  553. #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
  554. __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
  555. #undef GENERATE_CASE
  556. default:
  557. return false;
  558. }
  559. }
  560. static void get_num_data_items(struct ceph_osd_request *req,
  561. int *num_request_data_items,
  562. int *num_reply_data_items)
  563. {
  564. struct ceph_osd_req_op *op;
  565. *num_request_data_items = 0;
  566. *num_reply_data_items = 0;
  567. for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
  568. switch (op->op) {
  569. /* request */
  570. case CEPH_OSD_OP_WRITE:
  571. case CEPH_OSD_OP_WRITEFULL:
  572. case CEPH_OSD_OP_SETXATTR:
  573. case CEPH_OSD_OP_CMPXATTR:
  574. case CEPH_OSD_OP_NOTIFY_ACK:
  575. case CEPH_OSD_OP_COPY_FROM2:
  576. *num_request_data_items += 1;
  577. break;
  578. /* reply */
  579. case CEPH_OSD_OP_STAT:
  580. case CEPH_OSD_OP_READ:
  581. case CEPH_OSD_OP_LIST_WATCHERS:
  582. *num_reply_data_items += 1;
  583. break;
  584. /* both */
  585. case CEPH_OSD_OP_NOTIFY:
  586. *num_request_data_items += 1;
  587. *num_reply_data_items += 1;
  588. break;
  589. case CEPH_OSD_OP_CALL:
  590. *num_request_data_items += 2;
  591. *num_reply_data_items += 1;
  592. break;
  593. default:
  594. WARN_ON(!osd_req_opcode_valid(op->op));
  595. break;
  596. }
  597. }
  598. }
  599. /*
  600. * oid, oloc and OSD op opcode(s) must be filled in before this function
  601. * is called.
  602. */
  603. int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
  604. {
  605. int num_request_data_items, num_reply_data_items;
  606. get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
  607. return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
  608. num_reply_data_items);
  609. }
  610. EXPORT_SYMBOL(ceph_osdc_alloc_messages);
  611. /*
  612. * This is an osd op init function for opcodes that have no data or
  613. * other information associated with them. It also serves as a
  614. * common init routine for all the other init functions, below.
  615. */
  616. struct ceph_osd_req_op *
  617. osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
  618. u16 opcode, u32 flags)
  619. {
  620. struct ceph_osd_req_op *op;
  621. BUG_ON(which >= osd_req->r_num_ops);
  622. BUG_ON(!osd_req_opcode_valid(opcode));
  623. op = &osd_req->r_ops[which];
  624. memset(op, 0, sizeof (*op));
  625. op->op = opcode;
  626. op->flags = flags;
  627. return op;
  628. }
  629. EXPORT_SYMBOL(osd_req_op_init);
  630. void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
  631. unsigned int which, u16 opcode,
  632. u64 offset, u64 length,
  633. u64 truncate_size, u32 truncate_seq)
  634. {
  635. struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
  636. opcode, 0);
  637. size_t payload_len = 0;
  638. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
  639. opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
  640. opcode != CEPH_OSD_OP_TRUNCATE);
  641. op->extent.offset = offset;
  642. op->extent.length = length;
  643. op->extent.truncate_size = truncate_size;
  644. op->extent.truncate_seq = truncate_seq;
  645. if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
  646. payload_len += length;
  647. op->indata_len = payload_len;
  648. }
  649. EXPORT_SYMBOL(osd_req_op_extent_init);
  650. void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
  651. unsigned int which, u64 length)
  652. {
  653. struct ceph_osd_req_op *op;
  654. u64 previous;
  655. BUG_ON(which >= osd_req->r_num_ops);
  656. op = &osd_req->r_ops[which];
  657. previous = op->extent.length;
  658. if (length == previous)
  659. return; /* Nothing to do */
  660. BUG_ON(length > previous);
  661. op->extent.length = length;
  662. if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
  663. op->indata_len -= previous - length;
  664. }
  665. EXPORT_SYMBOL(osd_req_op_extent_update);
  666. void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
  667. unsigned int which, u64 offset_inc)
  668. {
  669. struct ceph_osd_req_op *op, *prev_op;
  670. BUG_ON(which + 1 >= osd_req->r_num_ops);
  671. prev_op = &osd_req->r_ops[which];
  672. op = osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
  673. /* dup previous one */
  674. op->indata_len = prev_op->indata_len;
  675. op->outdata_len = prev_op->outdata_len;
  676. op->extent = prev_op->extent;
  677. /* adjust offset */
  678. op->extent.offset += offset_inc;
  679. op->extent.length -= offset_inc;
  680. if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
  681. op->indata_len -= offset_inc;
  682. }
  683. EXPORT_SYMBOL(osd_req_op_extent_dup_last);
  684. int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
  685. const char *class, const char *method)
  686. {
  687. struct ceph_osd_req_op *op;
  688. struct ceph_pagelist *pagelist;
  689. size_t payload_len = 0;
  690. size_t size;
  691. int ret;
  692. op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
  693. pagelist = ceph_pagelist_alloc(GFP_NOFS);
  694. if (!pagelist)
  695. return -ENOMEM;
  696. op->cls.class_name = class;
  697. size = strlen(class);
  698. BUG_ON(size > (size_t) U8_MAX);
  699. op->cls.class_len = size;
  700. ret = ceph_pagelist_append(pagelist, class, size);
  701. if (ret)
  702. goto err_pagelist_free;
  703. payload_len += size;
  704. op->cls.method_name = method;
  705. size = strlen(method);
  706. BUG_ON(size > (size_t) U8_MAX);
  707. op->cls.method_len = size;
  708. ret = ceph_pagelist_append(pagelist, method, size);
  709. if (ret)
  710. goto err_pagelist_free;
  711. payload_len += size;
  712. osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
  713. op->indata_len = payload_len;
  714. return 0;
  715. err_pagelist_free:
  716. ceph_pagelist_release(pagelist);
  717. return ret;
  718. }
  719. EXPORT_SYMBOL(osd_req_op_cls_init);
  720. int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
  721. u16 opcode, const char *name, const void *value,
  722. size_t size, u8 cmp_op, u8 cmp_mode)
  723. {
  724. struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
  725. opcode, 0);
  726. struct ceph_pagelist *pagelist;
  727. size_t payload_len;
  728. int ret;
  729. BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
  730. pagelist = ceph_pagelist_alloc(GFP_NOFS);
  731. if (!pagelist)
  732. return -ENOMEM;
  733. payload_len = strlen(name);
  734. op->xattr.name_len = payload_len;
  735. ret = ceph_pagelist_append(pagelist, name, payload_len);
  736. if (ret)
  737. goto err_pagelist_free;
  738. op->xattr.value_len = size;
  739. ret = ceph_pagelist_append(pagelist, value, size);
  740. if (ret)
  741. goto err_pagelist_free;
  742. payload_len += size;
  743. op->xattr.cmp_op = cmp_op;
  744. op->xattr.cmp_mode = cmp_mode;
  745. ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
  746. op->indata_len = payload_len;
  747. return 0;
  748. err_pagelist_free:
  749. ceph_pagelist_release(pagelist);
  750. return ret;
  751. }
  752. EXPORT_SYMBOL(osd_req_op_xattr_init);
  753. /*
  754. * @watch_opcode: CEPH_OSD_WATCH_OP_*
  755. */
  756. static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
  757. u8 watch_opcode, u64 cookie, u32 gen)
  758. {
  759. struct ceph_osd_req_op *op;
  760. op = osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
  761. op->watch.cookie = cookie;
  762. op->watch.op = watch_opcode;
  763. op->watch.gen = gen;
  764. }
  765. /*
  766. * prot_ver, timeout and notify payload (may be empty) should already be
  767. * encoded in @request_pl
  768. */
  769. static void osd_req_op_notify_init(struct ceph_osd_request *req, int which,
  770. u64 cookie, struct ceph_pagelist *request_pl)
  771. {
  772. struct ceph_osd_req_op *op;
  773. op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
  774. op->notify.cookie = cookie;
  775. ceph_osd_data_pagelist_init(&op->notify.request_data, request_pl);
  776. op->indata_len = request_pl->length;
  777. }
  778. /*
  779. * @flags: CEPH_OSD_OP_ALLOC_HINT_FLAG_*
  780. */
  781. void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
  782. unsigned int which,
  783. u64 expected_object_size,
  784. u64 expected_write_size,
  785. u32 flags)
  786. {
  787. struct ceph_osd_req_op *op;
  788. op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_SETALLOCHINT, 0);
  789. op->alloc_hint.expected_object_size = expected_object_size;
  790. op->alloc_hint.expected_write_size = expected_write_size;
  791. op->alloc_hint.flags = flags;
  792. /*
  793. * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
  794. * not worth a feature bit. Set FAILOK per-op flag to make
  795. * sure older osds don't trip over an unsupported opcode.
  796. */
  797. op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
  798. }
  799. EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
  800. static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
  801. struct ceph_osd_data *osd_data)
  802. {
  803. u64 length = ceph_osd_data_length(osd_data);
  804. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
  805. BUG_ON(length > (u64) SIZE_MAX);
  806. if (length)
  807. ceph_msg_data_add_pages(msg, osd_data->pages,
  808. length, osd_data->alignment, false);
  809. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
  810. BUG_ON(!length);
  811. ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
  812. #ifdef CONFIG_BLOCK
  813. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
  814. ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
  815. #endif
  816. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
  817. ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
  818. } else {
  819. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
  820. }
  821. }
  822. static u32 osd_req_encode_op(struct ceph_osd_op *dst,
  823. const struct ceph_osd_req_op *src)
  824. {
  825. switch (src->op) {
  826. case CEPH_OSD_OP_STAT:
  827. break;
  828. case CEPH_OSD_OP_READ:
  829. case CEPH_OSD_OP_WRITE:
  830. case CEPH_OSD_OP_WRITEFULL:
  831. case CEPH_OSD_OP_ZERO:
  832. case CEPH_OSD_OP_TRUNCATE:
  833. dst->extent.offset = cpu_to_le64(src->extent.offset);
  834. dst->extent.length = cpu_to_le64(src->extent.length);
  835. dst->extent.truncate_size =
  836. cpu_to_le64(src->extent.truncate_size);
  837. dst->extent.truncate_seq =
  838. cpu_to_le32(src->extent.truncate_seq);
  839. break;
  840. case CEPH_OSD_OP_CALL:
  841. dst->cls.class_len = src->cls.class_len;
  842. dst->cls.method_len = src->cls.method_len;
  843. dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
  844. break;
  845. case CEPH_OSD_OP_WATCH:
  846. dst->watch.cookie = cpu_to_le64(src->watch.cookie);
  847. dst->watch.ver = cpu_to_le64(0);
  848. dst->watch.op = src->watch.op;
  849. dst->watch.gen = cpu_to_le32(src->watch.gen);
  850. break;
  851. case CEPH_OSD_OP_NOTIFY_ACK:
  852. break;
  853. case CEPH_OSD_OP_NOTIFY:
  854. dst->notify.cookie = cpu_to_le64(src->notify.cookie);
  855. break;
  856. case CEPH_OSD_OP_LIST_WATCHERS:
  857. break;
  858. case CEPH_OSD_OP_SETALLOCHINT:
  859. dst->alloc_hint.expected_object_size =
  860. cpu_to_le64(src->alloc_hint.expected_object_size);
  861. dst->alloc_hint.expected_write_size =
  862. cpu_to_le64(src->alloc_hint.expected_write_size);
  863. dst->alloc_hint.flags = cpu_to_le32(src->alloc_hint.flags);
  864. break;
  865. case CEPH_OSD_OP_SETXATTR:
  866. case CEPH_OSD_OP_CMPXATTR:
  867. dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
  868. dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
  869. dst->xattr.cmp_op = src->xattr.cmp_op;
  870. dst->xattr.cmp_mode = src->xattr.cmp_mode;
  871. break;
  872. case CEPH_OSD_OP_CREATE:
  873. case CEPH_OSD_OP_DELETE:
  874. break;
  875. case CEPH_OSD_OP_COPY_FROM2:
  876. dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
  877. dst->copy_from.src_version =
  878. cpu_to_le64(src->copy_from.src_version);
  879. dst->copy_from.flags = src->copy_from.flags;
  880. dst->copy_from.src_fadvise_flags =
  881. cpu_to_le32(src->copy_from.src_fadvise_flags);
  882. break;
  883. default:
  884. pr_err("unsupported osd opcode %s\n",
  885. ceph_osd_op_name(src->op));
  886. WARN_ON(1);
  887. return 0;
  888. }
  889. dst->op = cpu_to_le16(src->op);
  890. dst->flags = cpu_to_le32(src->flags);
  891. dst->payload_len = cpu_to_le32(src->indata_len);
  892. return src->indata_len;
  893. }
  894. /*
  895. * build new request AND message, calculate layout, and adjust file
  896. * extent as needed.
  897. *
  898. * if the file was recently truncated, we include information about its
  899. * old and new size so that the object can be updated appropriately. (we
  900. * avoid synchronously deleting truncated objects because it's slow.)
  901. */
  902. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  903. struct ceph_file_layout *layout,
  904. struct ceph_vino vino,
  905. u64 off, u64 *plen,
  906. unsigned int which, int num_ops,
  907. int opcode, int flags,
  908. struct ceph_snap_context *snapc,
  909. u32 truncate_seq,
  910. u64 truncate_size,
  911. bool use_mempool)
  912. {
  913. struct ceph_osd_request *req;
  914. u64 objnum = 0;
  915. u64 objoff = 0;
  916. u64 objlen = 0;
  917. int r;
  918. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
  919. opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
  920. opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
  921. req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
  922. GFP_NOFS);
  923. if (!req) {
  924. r = -ENOMEM;
  925. goto fail;
  926. }
  927. /* calculate max write size */
  928. r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
  929. if (r)
  930. goto fail;
  931. if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
  932. osd_req_op_init(req, which, opcode, 0);
  933. } else {
  934. u32 object_size = layout->object_size;
  935. u32 object_base = off - objoff;
  936. if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
  937. if (truncate_size <= object_base) {
  938. truncate_size = 0;
  939. } else {
  940. truncate_size -= object_base;
  941. if (truncate_size > object_size)
  942. truncate_size = object_size;
  943. }
  944. }
  945. osd_req_op_extent_init(req, which, opcode, objoff, objlen,
  946. truncate_size, truncate_seq);
  947. }
  948. req->r_base_oloc.pool = layout->pool_id;
  949. req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
  950. ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
  951. req->r_flags = flags | osdc->client->options->read_from_replica;
  952. req->r_snapid = vino.snap;
  953. if (flags & CEPH_OSD_FLAG_WRITE)
  954. req->r_data_offset = off;
  955. if (num_ops > 1)
  956. /*
  957. * This is a special case for ceph_writepages_start(), but it
  958. * also covers ceph_uninline_data(). If more multi-op request
  959. * use cases emerge, we will need a separate helper.
  960. */
  961. r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
  962. else
  963. r = ceph_osdc_alloc_messages(req, GFP_NOFS);
  964. if (r)
  965. goto fail;
  966. return req;
  967. fail:
  968. ceph_osdc_put_request(req);
  969. return ERR_PTR(r);
  970. }
  971. EXPORT_SYMBOL(ceph_osdc_new_request);
  972. /*
  973. * We keep osd requests in an rbtree, sorted by ->r_tid.
  974. */
  975. DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
  976. DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
  977. /*
  978. * Call @fn on each OSD request as long as @fn returns 0.
  979. */
  980. static void for_each_request(struct ceph_osd_client *osdc,
  981. int (*fn)(struct ceph_osd_request *req, void *arg),
  982. void *arg)
  983. {
  984. struct rb_node *n, *p;
  985. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  986. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  987. for (p = rb_first(&osd->o_requests); p; ) {
  988. struct ceph_osd_request *req =
  989. rb_entry(p, struct ceph_osd_request, r_node);
  990. p = rb_next(p);
  991. if (fn(req, arg))
  992. return;
  993. }
  994. }
  995. for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
  996. struct ceph_osd_request *req =
  997. rb_entry(p, struct ceph_osd_request, r_node);
  998. p = rb_next(p);
  999. if (fn(req, arg))
  1000. return;
  1001. }
  1002. }
  1003. static bool osd_homeless(struct ceph_osd *osd)
  1004. {
  1005. return osd->o_osd == CEPH_HOMELESS_OSD;
  1006. }
  1007. static bool osd_registered(struct ceph_osd *osd)
  1008. {
  1009. verify_osdc_locked(osd->o_osdc);
  1010. return !RB_EMPTY_NODE(&osd->o_node);
  1011. }
  1012. /*
  1013. * Assumes @osd is zero-initialized.
  1014. */
  1015. static void osd_init(struct ceph_osd *osd)
  1016. {
  1017. refcount_set(&osd->o_ref, 1);
  1018. RB_CLEAR_NODE(&osd->o_node);
  1019. osd->o_requests = RB_ROOT;
  1020. osd->o_linger_requests = RB_ROOT;
  1021. osd->o_backoff_mappings = RB_ROOT;
  1022. osd->o_backoffs_by_id = RB_ROOT;
  1023. INIT_LIST_HEAD(&osd->o_osd_lru);
  1024. INIT_LIST_HEAD(&osd->o_keepalive_item);
  1025. osd->o_incarnation = 1;
  1026. mutex_init(&osd->lock);
  1027. }
  1028. static void osd_cleanup(struct ceph_osd *osd)
  1029. {
  1030. WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
  1031. WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
  1032. WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
  1033. WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
  1034. WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
  1035. WARN_ON(!list_empty(&osd->o_osd_lru));
  1036. WARN_ON(!list_empty(&osd->o_keepalive_item));
  1037. if (osd->o_auth.authorizer) {
  1038. WARN_ON(osd_homeless(osd));
  1039. ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
  1040. }
  1041. }
  1042. /*
  1043. * Track open sessions with osds.
  1044. */
  1045. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
  1046. {
  1047. struct ceph_osd *osd;
  1048. WARN_ON(onum == CEPH_HOMELESS_OSD);
  1049. osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
  1050. osd_init(osd);
  1051. osd->o_osdc = osdc;
  1052. osd->o_osd = onum;
  1053. ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
  1054. return osd;
  1055. }
  1056. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  1057. {
  1058. if (refcount_inc_not_zero(&osd->o_ref)) {
  1059. dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
  1060. refcount_read(&osd->o_ref));
  1061. return osd;
  1062. } else {
  1063. dout("get_osd %p FAIL\n", osd);
  1064. return NULL;
  1065. }
  1066. }
  1067. static void put_osd(struct ceph_osd *osd)
  1068. {
  1069. dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
  1070. refcount_read(&osd->o_ref) - 1);
  1071. if (refcount_dec_and_test(&osd->o_ref)) {
  1072. osd_cleanup(osd);
  1073. kfree(osd);
  1074. }
  1075. }
  1076. DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
  1077. static void __move_osd_to_lru(struct ceph_osd *osd)
  1078. {
  1079. struct ceph_osd_client *osdc = osd->o_osdc;
  1080. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1081. BUG_ON(!list_empty(&osd->o_osd_lru));
  1082. spin_lock(&osdc->osd_lru_lock);
  1083. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  1084. spin_unlock(&osdc->osd_lru_lock);
  1085. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
  1086. }
  1087. static void maybe_move_osd_to_lru(struct ceph_osd *osd)
  1088. {
  1089. if (RB_EMPTY_ROOT(&osd->o_requests) &&
  1090. RB_EMPTY_ROOT(&osd->o_linger_requests))
  1091. __move_osd_to_lru(osd);
  1092. }
  1093. static void __remove_osd_from_lru(struct ceph_osd *osd)
  1094. {
  1095. struct ceph_osd_client *osdc = osd->o_osdc;
  1096. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1097. spin_lock(&osdc->osd_lru_lock);
  1098. if (!list_empty(&osd->o_osd_lru))
  1099. list_del_init(&osd->o_osd_lru);
  1100. spin_unlock(&osdc->osd_lru_lock);
  1101. }
  1102. /*
  1103. * Close the connection and assign any leftover requests to the
  1104. * homeless session.
  1105. */
  1106. static void close_osd(struct ceph_osd *osd)
  1107. {
  1108. struct ceph_osd_client *osdc = osd->o_osdc;
  1109. struct rb_node *n;
  1110. verify_osdc_wrlocked(osdc);
  1111. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1112. ceph_con_close(&osd->o_con);
  1113. for (n = rb_first(&osd->o_requests); n; ) {
  1114. struct ceph_osd_request *req =
  1115. rb_entry(n, struct ceph_osd_request, r_node);
  1116. n = rb_next(n); /* unlink_request() */
  1117. dout(" reassigning req %p tid %llu\n", req, req->r_tid);
  1118. unlink_request(osd, req);
  1119. link_request(&osdc->homeless_osd, req);
  1120. }
  1121. for (n = rb_first(&osd->o_linger_requests); n; ) {
  1122. struct ceph_osd_linger_request *lreq =
  1123. rb_entry(n, struct ceph_osd_linger_request, node);
  1124. n = rb_next(n); /* unlink_linger() */
  1125. dout(" reassigning lreq %p linger_id %llu\n", lreq,
  1126. lreq->linger_id);
  1127. unlink_linger(osd, lreq);
  1128. link_linger(&osdc->homeless_osd, lreq);
  1129. }
  1130. clear_backoffs(osd);
  1131. __remove_osd_from_lru(osd);
  1132. erase_osd(&osdc->osds, osd);
  1133. put_osd(osd);
  1134. }
  1135. /*
  1136. * reset osd connect
  1137. */
  1138. static int reopen_osd(struct ceph_osd *osd)
  1139. {
  1140. struct ceph_entity_addr *peer_addr;
  1141. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1142. if (RB_EMPTY_ROOT(&osd->o_requests) &&
  1143. RB_EMPTY_ROOT(&osd->o_linger_requests)) {
  1144. close_osd(osd);
  1145. return -ENODEV;
  1146. }
  1147. peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
  1148. if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
  1149. !ceph_con_opened(&osd->o_con)) {
  1150. struct rb_node *n;
  1151. dout("osd addr hasn't changed and connection never opened, "
  1152. "letting msgr retry\n");
  1153. /* touch each r_stamp for handle_timeout()'s benfit */
  1154. for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
  1155. struct ceph_osd_request *req =
  1156. rb_entry(n, struct ceph_osd_request, r_node);
  1157. req->r_stamp = jiffies;
  1158. }
  1159. return -EAGAIN;
  1160. }
  1161. ceph_con_close(&osd->o_con);
  1162. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
  1163. osd->o_incarnation++;
  1164. return 0;
  1165. }
  1166. static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
  1167. bool wrlocked)
  1168. {
  1169. struct ceph_osd *osd;
  1170. if (wrlocked)
  1171. verify_osdc_wrlocked(osdc);
  1172. else
  1173. verify_osdc_locked(osdc);
  1174. if (o != CEPH_HOMELESS_OSD)
  1175. osd = lookup_osd(&osdc->osds, o);
  1176. else
  1177. osd = &osdc->homeless_osd;
  1178. if (!osd) {
  1179. if (!wrlocked)
  1180. return ERR_PTR(-EAGAIN);
  1181. osd = create_osd(osdc, o);
  1182. insert_osd(&osdc->osds, osd);
  1183. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
  1184. &osdc->osdmap->osd_addr[osd->o_osd]);
  1185. }
  1186. dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
  1187. return osd;
  1188. }
  1189. /*
  1190. * Create request <-> OSD session relation.
  1191. *
  1192. * @req has to be assigned a tid, @osd may be homeless.
  1193. */
  1194. static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
  1195. {
  1196. verify_osd_locked(osd);
  1197. WARN_ON(!req->r_tid || req->r_osd);
  1198. dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
  1199. req, req->r_tid);
  1200. if (!osd_homeless(osd))
  1201. __remove_osd_from_lru(osd);
  1202. else
  1203. atomic_inc(&osd->o_osdc->num_homeless);
  1204. get_osd(osd);
  1205. insert_request(&osd->o_requests, req);
  1206. req->r_osd = osd;
  1207. }
  1208. static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
  1209. {
  1210. verify_osd_locked(osd);
  1211. WARN_ON(req->r_osd != osd);
  1212. dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
  1213. req, req->r_tid);
  1214. req->r_osd = NULL;
  1215. erase_request(&osd->o_requests, req);
  1216. put_osd(osd);
  1217. if (!osd_homeless(osd))
  1218. maybe_move_osd_to_lru(osd);
  1219. else
  1220. atomic_dec(&osd->o_osdc->num_homeless);
  1221. }
  1222. static bool __pool_full(struct ceph_pg_pool_info *pi)
  1223. {
  1224. return pi->flags & CEPH_POOL_FLAG_FULL;
  1225. }
  1226. static bool have_pool_full(struct ceph_osd_client *osdc)
  1227. {
  1228. struct rb_node *n;
  1229. for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
  1230. struct ceph_pg_pool_info *pi =
  1231. rb_entry(n, struct ceph_pg_pool_info, node);
  1232. if (__pool_full(pi))
  1233. return true;
  1234. }
  1235. return false;
  1236. }
  1237. static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
  1238. {
  1239. struct ceph_pg_pool_info *pi;
  1240. pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
  1241. if (!pi)
  1242. return false;
  1243. return __pool_full(pi);
  1244. }
  1245. /*
  1246. * Returns whether a request should be blocked from being sent
  1247. * based on the current osdmap and osd_client settings.
  1248. */
  1249. static bool target_should_be_paused(struct ceph_osd_client *osdc,
  1250. const struct ceph_osd_request_target *t,
  1251. struct ceph_pg_pool_info *pi)
  1252. {
  1253. bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
  1254. bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
  1255. ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  1256. __pool_full(pi);
  1257. WARN_ON(pi->id != t->target_oloc.pool);
  1258. return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
  1259. ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
  1260. (osdc->osdmap->epoch < osdc->epoch_barrier);
  1261. }
  1262. static int pick_random_replica(const struct ceph_osds *acting)
  1263. {
  1264. int i = prandom_u32_max(acting->size);
  1265. dout("%s picked osd%d, primary osd%d\n", __func__,
  1266. acting->osds[i], acting->primary);
  1267. return i;
  1268. }
  1269. /*
  1270. * Picks the closest replica based on client's location given by
  1271. * crush_location option. Prefers the primary if the locality is
  1272. * the same.
  1273. */
  1274. static int pick_closest_replica(struct ceph_osd_client *osdc,
  1275. const struct ceph_osds *acting)
  1276. {
  1277. struct ceph_options *opt = osdc->client->options;
  1278. int best_i, best_locality;
  1279. int i = 0, locality;
  1280. do {
  1281. locality = ceph_get_crush_locality(osdc->osdmap,
  1282. acting->osds[i],
  1283. &opt->crush_locs);
  1284. if (i == 0 ||
  1285. (locality >= 0 && best_locality < 0) ||
  1286. (locality >= 0 && best_locality >= 0 &&
  1287. locality < best_locality)) {
  1288. best_i = i;
  1289. best_locality = locality;
  1290. }
  1291. } while (++i < acting->size);
  1292. dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
  1293. acting->osds[best_i], best_locality, acting->primary);
  1294. return best_i;
  1295. }
  1296. enum calc_target_result {
  1297. CALC_TARGET_NO_ACTION = 0,
  1298. CALC_TARGET_NEED_RESEND,
  1299. CALC_TARGET_POOL_DNE,
  1300. };
  1301. static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
  1302. struct ceph_osd_request_target *t,
  1303. bool any_change)
  1304. {
  1305. struct ceph_pg_pool_info *pi;
  1306. struct ceph_pg pgid, last_pgid;
  1307. struct ceph_osds up, acting;
  1308. bool is_read = t->flags & CEPH_OSD_FLAG_READ;
  1309. bool is_write = t->flags & CEPH_OSD_FLAG_WRITE;
  1310. bool force_resend = false;
  1311. bool unpaused = false;
  1312. bool legacy_change = false;
  1313. bool split = false;
  1314. bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
  1315. bool recovery_deletes = ceph_osdmap_flag(osdc,
  1316. CEPH_OSDMAP_RECOVERY_DELETES);
  1317. enum calc_target_result ct_res;
  1318. t->epoch = osdc->osdmap->epoch;
  1319. pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
  1320. if (!pi) {
  1321. t->osd = CEPH_HOMELESS_OSD;
  1322. ct_res = CALC_TARGET_POOL_DNE;
  1323. goto out;
  1324. }
  1325. if (osdc->osdmap->epoch == pi->last_force_request_resend) {
  1326. if (t->last_force_resend < pi->last_force_request_resend) {
  1327. t->last_force_resend = pi->last_force_request_resend;
  1328. force_resend = true;
  1329. } else if (t->last_force_resend == 0) {
  1330. force_resend = true;
  1331. }
  1332. }
  1333. /* apply tiering */
  1334. ceph_oid_copy(&t->target_oid, &t->base_oid);
  1335. ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
  1336. if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
  1337. if (is_read && pi->read_tier >= 0)
  1338. t->target_oloc.pool = pi->read_tier;
  1339. if (is_write && pi->write_tier >= 0)
  1340. t->target_oloc.pool = pi->write_tier;
  1341. pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
  1342. if (!pi) {
  1343. t->osd = CEPH_HOMELESS_OSD;
  1344. ct_res = CALC_TARGET_POOL_DNE;
  1345. goto out;
  1346. }
  1347. }
  1348. __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
  1349. last_pgid.pool = pgid.pool;
  1350. last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
  1351. ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
  1352. if (any_change &&
  1353. ceph_is_new_interval(&t->acting,
  1354. &acting,
  1355. &t->up,
  1356. &up,
  1357. t->size,
  1358. pi->size,
  1359. t->min_size,
  1360. pi->min_size,
  1361. t->pg_num,
  1362. pi->pg_num,
  1363. t->sort_bitwise,
  1364. sort_bitwise,
  1365. t->recovery_deletes,
  1366. recovery_deletes,
  1367. &last_pgid))
  1368. force_resend = true;
  1369. if (t->paused && !target_should_be_paused(osdc, t, pi)) {
  1370. t->paused = false;
  1371. unpaused = true;
  1372. }
  1373. legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
  1374. ceph_osds_changed(&t->acting, &acting,
  1375. t->used_replica || any_change);
  1376. if (t->pg_num)
  1377. split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
  1378. if (legacy_change || force_resend || split) {
  1379. t->pgid = pgid; /* struct */
  1380. ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
  1381. ceph_osds_copy(&t->acting, &acting);
  1382. ceph_osds_copy(&t->up, &up);
  1383. t->size = pi->size;
  1384. t->min_size = pi->min_size;
  1385. t->pg_num = pi->pg_num;
  1386. t->pg_num_mask = pi->pg_num_mask;
  1387. t->sort_bitwise = sort_bitwise;
  1388. t->recovery_deletes = recovery_deletes;
  1389. if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS |
  1390. CEPH_OSD_FLAG_LOCALIZE_READS)) &&
  1391. !is_write && pi->type == CEPH_POOL_TYPE_REP &&
  1392. acting.size > 1) {
  1393. int pos;
  1394. WARN_ON(!is_read || acting.osds[0] != acting.primary);
  1395. if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
  1396. pos = pick_random_replica(&acting);
  1397. } else {
  1398. pos = pick_closest_replica(osdc, &acting);
  1399. }
  1400. t->osd = acting.osds[pos];
  1401. t->used_replica = pos > 0;
  1402. } else {
  1403. t->osd = acting.primary;
  1404. t->used_replica = false;
  1405. }
  1406. }
  1407. if (unpaused || legacy_change || force_resend || split)
  1408. ct_res = CALC_TARGET_NEED_RESEND;
  1409. else
  1410. ct_res = CALC_TARGET_NO_ACTION;
  1411. out:
  1412. dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
  1413. legacy_change, force_resend, split, ct_res, t->osd);
  1414. return ct_res;
  1415. }
  1416. static struct ceph_spg_mapping *alloc_spg_mapping(void)
  1417. {
  1418. struct ceph_spg_mapping *spg;
  1419. spg = kmalloc(sizeof(*spg), GFP_NOIO);
  1420. if (!spg)
  1421. return NULL;
  1422. RB_CLEAR_NODE(&spg->node);
  1423. spg->backoffs = RB_ROOT;
  1424. return spg;
  1425. }
  1426. static void free_spg_mapping(struct ceph_spg_mapping *spg)
  1427. {
  1428. WARN_ON(!RB_EMPTY_NODE(&spg->node));
  1429. WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
  1430. kfree(spg);
  1431. }
  1432. /*
  1433. * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
  1434. * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
  1435. * defined only within a specific spgid; it does not pass anything to
  1436. * children on split, or to another primary.
  1437. */
  1438. DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
  1439. RB_BYPTR, const struct ceph_spg *, node)
  1440. static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
  1441. {
  1442. return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
  1443. }
  1444. static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
  1445. void **pkey, size_t *pkey_len)
  1446. {
  1447. if (hoid->key_len) {
  1448. *pkey = hoid->key;
  1449. *pkey_len = hoid->key_len;
  1450. } else {
  1451. *pkey = hoid->oid;
  1452. *pkey_len = hoid->oid_len;
  1453. }
  1454. }
  1455. static int compare_names(const void *name1, size_t name1_len,
  1456. const void *name2, size_t name2_len)
  1457. {
  1458. int ret;
  1459. ret = memcmp(name1, name2, min(name1_len, name2_len));
  1460. if (!ret) {
  1461. if (name1_len < name2_len)
  1462. ret = -1;
  1463. else if (name1_len > name2_len)
  1464. ret = 1;
  1465. }
  1466. return ret;
  1467. }
  1468. static int hoid_compare(const struct ceph_hobject_id *lhs,
  1469. const struct ceph_hobject_id *rhs)
  1470. {
  1471. void *effective_key1, *effective_key2;
  1472. size_t effective_key1_len, effective_key2_len;
  1473. int ret;
  1474. if (lhs->is_max < rhs->is_max)
  1475. return -1;
  1476. if (lhs->is_max > rhs->is_max)
  1477. return 1;
  1478. if (lhs->pool < rhs->pool)
  1479. return -1;
  1480. if (lhs->pool > rhs->pool)
  1481. return 1;
  1482. if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
  1483. return -1;
  1484. if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
  1485. return 1;
  1486. ret = compare_names(lhs->nspace, lhs->nspace_len,
  1487. rhs->nspace, rhs->nspace_len);
  1488. if (ret)
  1489. return ret;
  1490. hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
  1491. hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
  1492. ret = compare_names(effective_key1, effective_key1_len,
  1493. effective_key2, effective_key2_len);
  1494. if (ret)
  1495. return ret;
  1496. ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
  1497. if (ret)
  1498. return ret;
  1499. if (lhs->snapid < rhs->snapid)
  1500. return -1;
  1501. if (lhs->snapid > rhs->snapid)
  1502. return 1;
  1503. return 0;
  1504. }
  1505. /*
  1506. * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
  1507. * compat stuff here.
  1508. *
  1509. * Assumes @hoid is zero-initialized.
  1510. */
  1511. static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
  1512. {
  1513. u8 struct_v;
  1514. u32 struct_len;
  1515. int ret;
  1516. ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
  1517. &struct_len);
  1518. if (ret)
  1519. return ret;
  1520. if (struct_v < 4) {
  1521. pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
  1522. goto e_inval;
  1523. }
  1524. hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
  1525. GFP_NOIO);
  1526. if (IS_ERR(hoid->key)) {
  1527. ret = PTR_ERR(hoid->key);
  1528. hoid->key = NULL;
  1529. return ret;
  1530. }
  1531. hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
  1532. GFP_NOIO);
  1533. if (IS_ERR(hoid->oid)) {
  1534. ret = PTR_ERR(hoid->oid);
  1535. hoid->oid = NULL;
  1536. return ret;
  1537. }
  1538. ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
  1539. ceph_decode_32_safe(p, end, hoid->hash, e_inval);
  1540. ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
  1541. hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
  1542. GFP_NOIO);
  1543. if (IS_ERR(hoid->nspace)) {
  1544. ret = PTR_ERR(hoid->nspace);
  1545. hoid->nspace = NULL;
  1546. return ret;
  1547. }
  1548. ceph_decode_64_safe(p, end, hoid->pool, e_inval);
  1549. ceph_hoid_build_hash_cache(hoid);
  1550. return 0;
  1551. e_inval:
  1552. return -EINVAL;
  1553. }
  1554. static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
  1555. {
  1556. return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
  1557. 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
  1558. }
  1559. static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
  1560. {
  1561. ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
  1562. ceph_encode_string(p, end, hoid->key, hoid->key_len);
  1563. ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
  1564. ceph_encode_64(p, hoid->snapid);
  1565. ceph_encode_32(p, hoid->hash);
  1566. ceph_encode_8(p, hoid->is_max);
  1567. ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
  1568. ceph_encode_64(p, hoid->pool);
  1569. }
  1570. static void free_hoid(struct ceph_hobject_id *hoid)
  1571. {
  1572. if (hoid) {
  1573. kfree(hoid->key);
  1574. kfree(hoid->oid);
  1575. kfree(hoid->nspace);
  1576. kfree(hoid);
  1577. }
  1578. }
  1579. static struct ceph_osd_backoff *alloc_backoff(void)
  1580. {
  1581. struct ceph_osd_backoff *backoff;
  1582. backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
  1583. if (!backoff)
  1584. return NULL;
  1585. RB_CLEAR_NODE(&backoff->spg_node);
  1586. RB_CLEAR_NODE(&backoff->id_node);
  1587. return backoff;
  1588. }
  1589. static void free_backoff(struct ceph_osd_backoff *backoff)
  1590. {
  1591. WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
  1592. WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
  1593. free_hoid(backoff->begin);
  1594. free_hoid(backoff->end);
  1595. kfree(backoff);
  1596. }
  1597. /*
  1598. * Within a specific spgid, backoffs are managed by ->begin hoid.
  1599. */
  1600. DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
  1601. RB_BYVAL, spg_node);
  1602. static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
  1603. const struct ceph_hobject_id *hoid)
  1604. {
  1605. struct rb_node *n = root->rb_node;
  1606. while (n) {
  1607. struct ceph_osd_backoff *cur =
  1608. rb_entry(n, struct ceph_osd_backoff, spg_node);
  1609. int cmp;
  1610. cmp = hoid_compare(hoid, cur->begin);
  1611. if (cmp < 0) {
  1612. n = n->rb_left;
  1613. } else if (cmp > 0) {
  1614. if (hoid_compare(hoid, cur->end) < 0)
  1615. return cur;
  1616. n = n->rb_right;
  1617. } else {
  1618. return cur;
  1619. }
  1620. }
  1621. return NULL;
  1622. }
  1623. /*
  1624. * Each backoff has a unique id within its OSD session.
  1625. */
  1626. DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
  1627. static void clear_backoffs(struct ceph_osd *osd)
  1628. {
  1629. while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
  1630. struct ceph_spg_mapping *spg =
  1631. rb_entry(rb_first(&osd->o_backoff_mappings),
  1632. struct ceph_spg_mapping, node);
  1633. while (!RB_EMPTY_ROOT(&spg->backoffs)) {
  1634. struct ceph_osd_backoff *backoff =
  1635. rb_entry(rb_first(&spg->backoffs),
  1636. struct ceph_osd_backoff, spg_node);
  1637. erase_backoff(&spg->backoffs, backoff);
  1638. erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
  1639. free_backoff(backoff);
  1640. }
  1641. erase_spg_mapping(&osd->o_backoff_mappings, spg);
  1642. free_spg_mapping(spg);
  1643. }
  1644. }
  1645. /*
  1646. * Set up a temporary, non-owning view into @t.
  1647. */
  1648. static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
  1649. const struct ceph_osd_request_target *t)
  1650. {
  1651. hoid->key = NULL;
  1652. hoid->key_len = 0;
  1653. hoid->oid = t->target_oid.name;
  1654. hoid->oid_len = t->target_oid.name_len;
  1655. hoid->snapid = CEPH_NOSNAP;
  1656. hoid->hash = t->pgid.seed;
  1657. hoid->is_max = false;
  1658. if (t->target_oloc.pool_ns) {
  1659. hoid->nspace = t->target_oloc.pool_ns->str;
  1660. hoid->nspace_len = t->target_oloc.pool_ns->len;
  1661. } else {
  1662. hoid->nspace = NULL;
  1663. hoid->nspace_len = 0;
  1664. }
  1665. hoid->pool = t->target_oloc.pool;
  1666. ceph_hoid_build_hash_cache(hoid);
  1667. }
  1668. static bool should_plug_request(struct ceph_osd_request *req)
  1669. {
  1670. struct ceph_osd *osd = req->r_osd;
  1671. struct ceph_spg_mapping *spg;
  1672. struct ceph_osd_backoff *backoff;
  1673. struct ceph_hobject_id hoid;
  1674. spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
  1675. if (!spg)
  1676. return false;
  1677. hoid_fill_from_target(&hoid, &req->r_t);
  1678. backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
  1679. if (!backoff)
  1680. return false;
  1681. dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
  1682. __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
  1683. backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
  1684. return true;
  1685. }
  1686. /*
  1687. * Keep get_num_data_items() in sync with this function.
  1688. */
  1689. static void setup_request_data(struct ceph_osd_request *req)
  1690. {
  1691. struct ceph_msg *request_msg = req->r_request;
  1692. struct ceph_msg *reply_msg = req->r_reply;
  1693. struct ceph_osd_req_op *op;
  1694. if (req->r_request->num_data_items || req->r_reply->num_data_items)
  1695. return;
  1696. WARN_ON(request_msg->data_length || reply_msg->data_length);
  1697. for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
  1698. switch (op->op) {
  1699. /* request */
  1700. case CEPH_OSD_OP_WRITE:
  1701. case CEPH_OSD_OP_WRITEFULL:
  1702. WARN_ON(op->indata_len != op->extent.length);
  1703. ceph_osdc_msg_data_add(request_msg,
  1704. &op->extent.osd_data);
  1705. break;
  1706. case CEPH_OSD_OP_SETXATTR:
  1707. case CEPH_OSD_OP_CMPXATTR:
  1708. WARN_ON(op->indata_len != op->xattr.name_len +
  1709. op->xattr.value_len);
  1710. ceph_osdc_msg_data_add(request_msg,
  1711. &op->xattr.osd_data);
  1712. break;
  1713. case CEPH_OSD_OP_NOTIFY_ACK:
  1714. ceph_osdc_msg_data_add(request_msg,
  1715. &op->notify_ack.request_data);
  1716. break;
  1717. case CEPH_OSD_OP_COPY_FROM2:
  1718. ceph_osdc_msg_data_add(request_msg,
  1719. &op->copy_from.osd_data);
  1720. break;
  1721. /* reply */
  1722. case CEPH_OSD_OP_STAT:
  1723. ceph_osdc_msg_data_add(reply_msg,
  1724. &op->raw_data_in);
  1725. break;
  1726. case CEPH_OSD_OP_READ:
  1727. ceph_osdc_msg_data_add(reply_msg,
  1728. &op->extent.osd_data);
  1729. break;
  1730. case CEPH_OSD_OP_LIST_WATCHERS:
  1731. ceph_osdc_msg_data_add(reply_msg,
  1732. &op->list_watchers.response_data);
  1733. break;
  1734. /* both */
  1735. case CEPH_OSD_OP_CALL:
  1736. WARN_ON(op->indata_len != op->cls.class_len +
  1737. op->cls.method_len +
  1738. op->cls.indata_len);
  1739. ceph_osdc_msg_data_add(request_msg,
  1740. &op->cls.request_info);
  1741. /* optional, can be NONE */
  1742. ceph_osdc_msg_data_add(request_msg,
  1743. &op->cls.request_data);
  1744. /* optional, can be NONE */
  1745. ceph_osdc_msg_data_add(reply_msg,
  1746. &op->cls.response_data);
  1747. break;
  1748. case CEPH_OSD_OP_NOTIFY:
  1749. ceph_osdc_msg_data_add(request_msg,
  1750. &op->notify.request_data);
  1751. ceph_osdc_msg_data_add(reply_msg,
  1752. &op->notify.response_data);
  1753. break;
  1754. }
  1755. }
  1756. }
  1757. static void encode_pgid(void **p, const struct ceph_pg *pgid)
  1758. {
  1759. ceph_encode_8(p, 1);
  1760. ceph_encode_64(p, pgid->pool);
  1761. ceph_encode_32(p, pgid->seed);
  1762. ceph_encode_32(p, -1); /* preferred */
  1763. }
  1764. static void encode_spgid(void **p, const struct ceph_spg *spgid)
  1765. {
  1766. ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
  1767. encode_pgid(p, &spgid->pgid);
  1768. ceph_encode_8(p, spgid->shard);
  1769. }
  1770. static void encode_oloc(void **p, void *end,
  1771. const struct ceph_object_locator *oloc)
  1772. {
  1773. ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
  1774. ceph_encode_64(p, oloc->pool);
  1775. ceph_encode_32(p, -1); /* preferred */
  1776. ceph_encode_32(p, 0); /* key len */
  1777. if (oloc->pool_ns)
  1778. ceph_encode_string(p, end, oloc->pool_ns->str,
  1779. oloc->pool_ns->len);
  1780. else
  1781. ceph_encode_32(p, 0);
  1782. }
  1783. static void encode_request_partial(struct ceph_osd_request *req,
  1784. struct ceph_msg *msg)
  1785. {
  1786. void *p = msg->front.iov_base;
  1787. void *const end = p + msg->front_alloc_len;
  1788. u32 data_len = 0;
  1789. int i;
  1790. if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
  1791. /* snapshots aren't writeable */
  1792. WARN_ON(req->r_snapid != CEPH_NOSNAP);
  1793. } else {
  1794. WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
  1795. req->r_data_offset || req->r_snapc);
  1796. }
  1797. setup_request_data(req);
  1798. encode_spgid(&p, &req->r_t.spgid); /* actual spg */
  1799. ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
  1800. ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
  1801. ceph_encode_32(&p, req->r_flags);
  1802. /* reqid */
  1803. ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
  1804. memset(p, 0, sizeof(struct ceph_osd_reqid));
  1805. p += sizeof(struct ceph_osd_reqid);
  1806. /* trace */
  1807. memset(p, 0, sizeof(struct ceph_blkin_trace_info));
  1808. p += sizeof(struct ceph_blkin_trace_info);
  1809. ceph_encode_32(&p, 0); /* client_inc, always 0 */
  1810. ceph_encode_timespec64(p, &req->r_mtime);
  1811. p += sizeof(struct ceph_timespec);
  1812. encode_oloc(&p, end, &req->r_t.target_oloc);
  1813. ceph_encode_string(&p, end, req->r_t.target_oid.name,
  1814. req->r_t.target_oid.name_len);
  1815. /* ops, can imply data */
  1816. ceph_encode_16(&p, req->r_num_ops);
  1817. for (i = 0; i < req->r_num_ops; i++) {
  1818. data_len += osd_req_encode_op(p, &req->r_ops[i]);
  1819. p += sizeof(struct ceph_osd_op);
  1820. }
  1821. ceph_encode_64(&p, req->r_snapid); /* snapid */
  1822. if (req->r_snapc) {
  1823. ceph_encode_64(&p, req->r_snapc->seq);
  1824. ceph_encode_32(&p, req->r_snapc->num_snaps);
  1825. for (i = 0; i < req->r_snapc->num_snaps; i++)
  1826. ceph_encode_64(&p, req->r_snapc->snaps[i]);
  1827. } else {
  1828. ceph_encode_64(&p, 0); /* snap_seq */
  1829. ceph_encode_32(&p, 0); /* snaps len */
  1830. }
  1831. ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
  1832. BUG_ON(p > end - 8); /* space for features */
  1833. msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
  1834. /* front_len is finalized in encode_request_finish() */
  1835. msg->front.iov_len = p - msg->front.iov_base;
  1836. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1837. msg->hdr.data_len = cpu_to_le32(data_len);
  1838. /*
  1839. * The header "data_off" is a hint to the receiver allowing it
  1840. * to align received data into its buffers such that there's no
  1841. * need to re-copy it before writing it to disk (direct I/O).
  1842. */
  1843. msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
  1844. dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
  1845. req->r_t.target_oid.name, req->r_t.target_oid.name_len);
  1846. }
  1847. static void encode_request_finish(struct ceph_msg *msg)
  1848. {
  1849. void *p = msg->front.iov_base;
  1850. void *const partial_end = p + msg->front.iov_len;
  1851. void *const end = p + msg->front_alloc_len;
  1852. if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
  1853. /* luminous OSD -- encode features and be done */
  1854. p = partial_end;
  1855. ceph_encode_64(&p, msg->con->peer_features);
  1856. } else {
  1857. struct {
  1858. char spgid[CEPH_ENCODING_START_BLK_LEN +
  1859. CEPH_PGID_ENCODING_LEN + 1];
  1860. __le32 hash;
  1861. __le32 epoch;
  1862. __le32 flags;
  1863. char reqid[CEPH_ENCODING_START_BLK_LEN +
  1864. sizeof(struct ceph_osd_reqid)];
  1865. char trace[sizeof(struct ceph_blkin_trace_info)];
  1866. __le32 client_inc;
  1867. struct ceph_timespec mtime;
  1868. } __packed head;
  1869. struct ceph_pg pgid;
  1870. void *oloc, *oid, *tail;
  1871. int oloc_len, oid_len, tail_len;
  1872. int len;
  1873. /*
  1874. * Pre-luminous OSD -- reencode v8 into v4 using @head
  1875. * as a temporary buffer. Encode the raw PG; the rest
  1876. * is just a matter of moving oloc, oid and tail blobs
  1877. * around.
  1878. */
  1879. memcpy(&head, p, sizeof(head));
  1880. p += sizeof(head);
  1881. oloc = p;
  1882. p += CEPH_ENCODING_START_BLK_LEN;
  1883. pgid.pool = ceph_decode_64(&p);
  1884. p += 4 + 4; /* preferred, key len */
  1885. len = ceph_decode_32(&p);
  1886. p += len; /* nspace */
  1887. oloc_len = p - oloc;
  1888. oid = p;
  1889. len = ceph_decode_32(&p);
  1890. p += len;
  1891. oid_len = p - oid;
  1892. tail = p;
  1893. tail_len = partial_end - p;
  1894. p = msg->front.iov_base;
  1895. ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
  1896. ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
  1897. ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
  1898. ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
  1899. /* reassert_version */
  1900. memset(p, 0, sizeof(struct ceph_eversion));
  1901. p += sizeof(struct ceph_eversion);
  1902. BUG_ON(p >= oloc);
  1903. memmove(p, oloc, oloc_len);
  1904. p += oloc_len;
  1905. pgid.seed = le32_to_cpu(head.hash);
  1906. encode_pgid(&p, &pgid); /* raw pg */
  1907. BUG_ON(p >= oid);
  1908. memmove(p, oid, oid_len);
  1909. p += oid_len;
  1910. /* tail -- ops, snapid, snapc, retry_attempt */
  1911. BUG_ON(p >= tail);
  1912. memmove(p, tail, tail_len);
  1913. p += tail_len;
  1914. msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
  1915. }
  1916. BUG_ON(p > end);
  1917. msg->front.iov_len = p - msg->front.iov_base;
  1918. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1919. dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
  1920. le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
  1921. le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
  1922. le16_to_cpu(msg->hdr.version));
  1923. }
  1924. /*
  1925. * @req has to be assigned a tid and registered.
  1926. */
  1927. static void send_request(struct ceph_osd_request *req)
  1928. {
  1929. struct ceph_osd *osd = req->r_osd;
  1930. verify_osd_locked(osd);
  1931. WARN_ON(osd->o_osd != req->r_t.osd);
  1932. /* backoff? */
  1933. if (should_plug_request(req))
  1934. return;
  1935. /*
  1936. * We may have a previously queued request message hanging
  1937. * around. Cancel it to avoid corrupting the msgr.
  1938. */
  1939. if (req->r_sent)
  1940. ceph_msg_revoke(req->r_request);
  1941. req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
  1942. if (req->r_attempts)
  1943. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1944. else
  1945. WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
  1946. encode_request_partial(req, req->r_request);
  1947. dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
  1948. __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
  1949. req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
  1950. req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
  1951. req->r_attempts);
  1952. req->r_t.paused = false;
  1953. req->r_stamp = jiffies;
  1954. req->r_attempts++;
  1955. req->r_sent = osd->o_incarnation;
  1956. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  1957. ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
  1958. }
  1959. static void maybe_request_map(struct ceph_osd_client *osdc)
  1960. {
  1961. bool continuous = false;
  1962. verify_osdc_locked(osdc);
  1963. WARN_ON(!osdc->osdmap->epoch);
  1964. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  1965. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
  1966. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
  1967. dout("%s osdc %p continuous\n", __func__, osdc);
  1968. continuous = true;
  1969. } else {
  1970. dout("%s osdc %p onetime\n", __func__, osdc);
  1971. }
  1972. if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
  1973. osdc->osdmap->epoch + 1, continuous))
  1974. ceph_monc_renew_subs(&osdc->client->monc);
  1975. }
  1976. static void complete_request(struct ceph_osd_request *req, int err);
  1977. static void send_map_check(struct ceph_osd_request *req);
  1978. static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
  1979. {
  1980. struct ceph_osd_client *osdc = req->r_osdc;
  1981. struct ceph_osd *osd;
  1982. enum calc_target_result ct_res;
  1983. int err = 0;
  1984. bool need_send = false;
  1985. bool promoted = false;
  1986. WARN_ON(req->r_tid);
  1987. dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
  1988. again:
  1989. ct_res = calc_target(osdc, &req->r_t, false);
  1990. if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
  1991. goto promote;
  1992. osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
  1993. if (IS_ERR(osd)) {
  1994. WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
  1995. goto promote;
  1996. }
  1997. if (osdc->abort_err) {
  1998. dout("req %p abort_err %d\n", req, osdc->abort_err);
  1999. err = osdc->abort_err;
  2000. } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
  2001. dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
  2002. osdc->epoch_barrier);
  2003. req->r_t.paused = true;
  2004. maybe_request_map(osdc);
  2005. } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  2006. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
  2007. dout("req %p pausewr\n", req);
  2008. req->r_t.paused = true;
  2009. maybe_request_map(osdc);
  2010. } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
  2011. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
  2012. dout("req %p pauserd\n", req);
  2013. req->r_t.paused = true;
  2014. maybe_request_map(osdc);
  2015. } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  2016. !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
  2017. CEPH_OSD_FLAG_FULL_FORCE)) &&
  2018. (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  2019. pool_full(osdc, req->r_t.base_oloc.pool))) {
  2020. dout("req %p full/pool_full\n", req);
  2021. if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
  2022. err = -ENOSPC;
  2023. } else {
  2024. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL))
  2025. pr_warn_ratelimited("cluster is full (osdmap FULL)\n");
  2026. else
  2027. pr_warn_ratelimited("pool %lld is full or reached quota\n",
  2028. req->r_t.base_oloc.pool);
  2029. req->r_t.paused = true;
  2030. maybe_request_map(osdc);
  2031. }
  2032. } else if (!osd_homeless(osd)) {
  2033. need_send = true;
  2034. } else {
  2035. maybe_request_map(osdc);
  2036. }
  2037. mutex_lock(&osd->lock);
  2038. /*
  2039. * Assign the tid atomically with send_request() to protect
  2040. * multiple writes to the same object from racing with each
  2041. * other, resulting in out of order ops on the OSDs.
  2042. */
  2043. req->r_tid = atomic64_inc_return(&osdc->last_tid);
  2044. link_request(osd, req);
  2045. if (need_send)
  2046. send_request(req);
  2047. else if (err)
  2048. complete_request(req, err);
  2049. mutex_unlock(&osd->lock);
  2050. if (!err && ct_res == CALC_TARGET_POOL_DNE)
  2051. send_map_check(req);
  2052. if (promoted)
  2053. downgrade_write(&osdc->lock);
  2054. return;
  2055. promote:
  2056. up_read(&osdc->lock);
  2057. down_write(&osdc->lock);
  2058. wrlocked = true;
  2059. promoted = true;
  2060. goto again;
  2061. }
  2062. static void account_request(struct ceph_osd_request *req)
  2063. {
  2064. WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
  2065. WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
  2066. req->r_flags |= CEPH_OSD_FLAG_ONDISK;
  2067. atomic_inc(&req->r_osdc->num_requests);
  2068. req->r_start_stamp = jiffies;
  2069. req->r_start_latency = ktime_get();
  2070. }
  2071. static void submit_request(struct ceph_osd_request *req, bool wrlocked)
  2072. {
  2073. ceph_osdc_get_request(req);
  2074. account_request(req);
  2075. __submit_request(req, wrlocked);
  2076. }
  2077. static void finish_request(struct ceph_osd_request *req)
  2078. {
  2079. struct ceph_osd_client *osdc = req->r_osdc;
  2080. WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
  2081. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  2082. req->r_end_latency = ktime_get();
  2083. if (req->r_osd)
  2084. unlink_request(req->r_osd, req);
  2085. atomic_dec(&osdc->num_requests);
  2086. /*
  2087. * If an OSD has failed or returned and a request has been sent
  2088. * twice, it's possible to get a reply and end up here while the
  2089. * request message is queued for delivery. We will ignore the
  2090. * reply, so not a big deal, but better to try and catch it.
  2091. */
  2092. ceph_msg_revoke(req->r_request);
  2093. ceph_msg_revoke_incoming(req->r_reply);
  2094. }
  2095. static void __complete_request(struct ceph_osd_request *req)
  2096. {
  2097. dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
  2098. req->r_tid, req->r_callback, req->r_result);
  2099. if (req->r_callback)
  2100. req->r_callback(req);
  2101. complete_all(&req->r_completion);
  2102. ceph_osdc_put_request(req);
  2103. }
  2104. static void complete_request_workfn(struct work_struct *work)
  2105. {
  2106. struct ceph_osd_request *req =
  2107. container_of(work, struct ceph_osd_request, r_complete_work);
  2108. __complete_request(req);
  2109. }
  2110. /*
  2111. * This is open-coded in handle_reply().
  2112. */
  2113. static void complete_request(struct ceph_osd_request *req, int err)
  2114. {
  2115. dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
  2116. req->r_result = err;
  2117. finish_request(req);
  2118. INIT_WORK(&req->r_complete_work, complete_request_workfn);
  2119. queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
  2120. }
  2121. static void cancel_map_check(struct ceph_osd_request *req)
  2122. {
  2123. struct ceph_osd_client *osdc = req->r_osdc;
  2124. struct ceph_osd_request *lookup_req;
  2125. verify_osdc_wrlocked(osdc);
  2126. lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
  2127. if (!lookup_req)
  2128. return;
  2129. WARN_ON(lookup_req != req);
  2130. erase_request_mc(&osdc->map_checks, req);
  2131. ceph_osdc_put_request(req);
  2132. }
  2133. static void cancel_request(struct ceph_osd_request *req)
  2134. {
  2135. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  2136. cancel_map_check(req);
  2137. finish_request(req);
  2138. complete_all(&req->r_completion);
  2139. ceph_osdc_put_request(req);
  2140. }
  2141. static void abort_request(struct ceph_osd_request *req, int err)
  2142. {
  2143. dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
  2144. cancel_map_check(req);
  2145. complete_request(req, err);
  2146. }
  2147. static int abort_fn(struct ceph_osd_request *req, void *arg)
  2148. {
  2149. int err = *(int *)arg;
  2150. abort_request(req, err);
  2151. return 0; /* continue iteration */
  2152. }
  2153. /*
  2154. * Abort all in-flight requests with @err and arrange for all future
  2155. * requests to be failed immediately.
  2156. */
  2157. void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
  2158. {
  2159. dout("%s osdc %p err %d\n", __func__, osdc, err);
  2160. down_write(&osdc->lock);
  2161. for_each_request(osdc, abort_fn, &err);
  2162. osdc->abort_err = err;
  2163. up_write(&osdc->lock);
  2164. }
  2165. EXPORT_SYMBOL(ceph_osdc_abort_requests);
  2166. void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
  2167. {
  2168. down_write(&osdc->lock);
  2169. osdc->abort_err = 0;
  2170. up_write(&osdc->lock);
  2171. }
  2172. EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
  2173. static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
  2174. {
  2175. if (likely(eb > osdc->epoch_barrier)) {
  2176. dout("updating epoch_barrier from %u to %u\n",
  2177. osdc->epoch_barrier, eb);
  2178. osdc->epoch_barrier = eb;
  2179. /* Request map if we're not to the barrier yet */
  2180. if (eb > osdc->osdmap->epoch)
  2181. maybe_request_map(osdc);
  2182. }
  2183. }
  2184. void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
  2185. {
  2186. down_read(&osdc->lock);
  2187. if (unlikely(eb > osdc->epoch_barrier)) {
  2188. up_read(&osdc->lock);
  2189. down_write(&osdc->lock);
  2190. update_epoch_barrier(osdc, eb);
  2191. up_write(&osdc->lock);
  2192. } else {
  2193. up_read(&osdc->lock);
  2194. }
  2195. }
  2196. EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
  2197. /*
  2198. * We can end up releasing caps as a result of abort_request().
  2199. * In that case, we probably want to ensure that the cap release message
  2200. * has an updated epoch barrier in it, so set the epoch barrier prior to
  2201. * aborting the first request.
  2202. */
  2203. static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
  2204. {
  2205. struct ceph_osd_client *osdc = req->r_osdc;
  2206. bool *victims = arg;
  2207. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  2208. (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  2209. pool_full(osdc, req->r_t.base_oloc.pool))) {
  2210. if (!*victims) {
  2211. update_epoch_barrier(osdc, osdc->osdmap->epoch);
  2212. *victims = true;
  2213. }
  2214. abort_request(req, -ENOSPC);
  2215. }
  2216. return 0; /* continue iteration */
  2217. }
  2218. /*
  2219. * Drop all pending requests that are stalled waiting on a full condition to
  2220. * clear, and complete them with ENOSPC as the return code. Set the
  2221. * osdc->epoch_barrier to the latest map epoch that we've seen if any were
  2222. * cancelled.
  2223. */
  2224. static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
  2225. {
  2226. bool victims = false;
  2227. if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
  2228. (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
  2229. for_each_request(osdc, abort_on_full_fn, &victims);
  2230. }
  2231. static void check_pool_dne(struct ceph_osd_request *req)
  2232. {
  2233. struct ceph_osd_client *osdc = req->r_osdc;
  2234. struct ceph_osdmap *map = osdc->osdmap;
  2235. verify_osdc_wrlocked(osdc);
  2236. WARN_ON(!map->epoch);
  2237. if (req->r_attempts) {
  2238. /*
  2239. * We sent a request earlier, which means that
  2240. * previously the pool existed, and now it does not
  2241. * (i.e., it was deleted).
  2242. */
  2243. req->r_map_dne_bound = map->epoch;
  2244. dout("%s req %p tid %llu pool disappeared\n", __func__, req,
  2245. req->r_tid);
  2246. } else {
  2247. dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
  2248. req, req->r_tid, req->r_map_dne_bound, map->epoch);
  2249. }
  2250. if (req->r_map_dne_bound) {
  2251. if (map->epoch >= req->r_map_dne_bound) {
  2252. /* we had a new enough map */
  2253. pr_info_ratelimited("tid %llu pool does not exist\n",
  2254. req->r_tid);
  2255. complete_request(req, -ENOENT);
  2256. }
  2257. } else {
  2258. send_map_check(req);
  2259. }
  2260. }
  2261. static void map_check_cb(struct ceph_mon_generic_request *greq)
  2262. {
  2263. struct ceph_osd_client *osdc = &greq->monc->client->osdc;
  2264. struct ceph_osd_request *req;
  2265. u64 tid = greq->private_data;
  2266. WARN_ON(greq->result || !greq->u.newest);
  2267. down_write(&osdc->lock);
  2268. req = lookup_request_mc(&osdc->map_checks, tid);
  2269. if (!req) {
  2270. dout("%s tid %llu dne\n", __func__, tid);
  2271. goto out_unlock;
  2272. }
  2273. dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
  2274. req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
  2275. if (!req->r_map_dne_bound)
  2276. req->r_map_dne_bound = greq->u.newest;
  2277. erase_request_mc(&osdc->map_checks, req);
  2278. check_pool_dne(req);
  2279. ceph_osdc_put_request(req);
  2280. out_unlock:
  2281. up_write(&osdc->lock);
  2282. }
  2283. static void send_map_check(struct ceph_osd_request *req)
  2284. {
  2285. struct ceph_osd_client *osdc = req->r_osdc;
  2286. struct ceph_osd_request *lookup_req;
  2287. int ret;
  2288. verify_osdc_wrlocked(osdc);
  2289. lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
  2290. if (lookup_req) {
  2291. WARN_ON(lookup_req != req);
  2292. return;
  2293. }
  2294. ceph_osdc_get_request(req);
  2295. insert_request_mc(&osdc->map_checks, req);
  2296. ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
  2297. map_check_cb, req->r_tid);
  2298. WARN_ON(ret);
  2299. }
  2300. /*
  2301. * lingering requests, watch/notify v2 infrastructure
  2302. */
  2303. static void linger_release(struct kref *kref)
  2304. {
  2305. struct ceph_osd_linger_request *lreq =
  2306. container_of(kref, struct ceph_osd_linger_request, kref);
  2307. dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
  2308. lreq->reg_req, lreq->ping_req);
  2309. WARN_ON(!RB_EMPTY_NODE(&lreq->node));
  2310. WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
  2311. WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
  2312. WARN_ON(!list_empty(&lreq->scan_item));
  2313. WARN_ON(!list_empty(&lreq->pending_lworks));
  2314. WARN_ON(lreq->osd);
  2315. if (lreq->request_pl)
  2316. ceph_pagelist_release(lreq->request_pl);
  2317. if (lreq->notify_id_pages)
  2318. ceph_release_page_vector(lreq->notify_id_pages, 1);
  2319. ceph_osdc_put_request(lreq->reg_req);
  2320. ceph_osdc_put_request(lreq->ping_req);
  2321. target_destroy(&lreq->t);
  2322. kfree(lreq);
  2323. }
  2324. static void linger_put(struct ceph_osd_linger_request *lreq)
  2325. {
  2326. if (lreq)
  2327. kref_put(&lreq->kref, linger_release);
  2328. }
  2329. static struct ceph_osd_linger_request *
  2330. linger_get(struct ceph_osd_linger_request *lreq)
  2331. {
  2332. kref_get(&lreq->kref);
  2333. return lreq;
  2334. }
  2335. static struct ceph_osd_linger_request *
  2336. linger_alloc(struct ceph_osd_client *osdc)
  2337. {
  2338. struct ceph_osd_linger_request *lreq;
  2339. lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
  2340. if (!lreq)
  2341. return NULL;
  2342. kref_init(&lreq->kref);
  2343. mutex_init(&lreq->lock);
  2344. RB_CLEAR_NODE(&lreq->node);
  2345. RB_CLEAR_NODE(&lreq->osdc_node);
  2346. RB_CLEAR_NODE(&lreq->mc_node);
  2347. INIT_LIST_HEAD(&lreq->scan_item);
  2348. INIT_LIST_HEAD(&lreq->pending_lworks);
  2349. init_completion(&lreq->reg_commit_wait);
  2350. init_completion(&lreq->notify_finish_wait);
  2351. lreq->osdc = osdc;
  2352. target_init(&lreq->t);
  2353. dout("%s lreq %p\n", __func__, lreq);
  2354. return lreq;
  2355. }
  2356. DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
  2357. DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
  2358. DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
  2359. /*
  2360. * Create linger request <-> OSD session relation.
  2361. *
  2362. * @lreq has to be registered, @osd may be homeless.
  2363. */
  2364. static void link_linger(struct ceph_osd *osd,
  2365. struct ceph_osd_linger_request *lreq)
  2366. {
  2367. verify_osd_locked(osd);
  2368. WARN_ON(!lreq->linger_id || lreq->osd);
  2369. dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
  2370. osd->o_osd, lreq, lreq->linger_id);
  2371. if (!osd_homeless(osd))
  2372. __remove_osd_from_lru(osd);
  2373. else
  2374. atomic_inc(&osd->o_osdc->num_homeless);
  2375. get_osd(osd);
  2376. insert_linger(&osd->o_linger_requests, lreq);
  2377. lreq->osd = osd;
  2378. }
  2379. static void unlink_linger(struct ceph_osd *osd,
  2380. struct ceph_osd_linger_request *lreq)
  2381. {
  2382. verify_osd_locked(osd);
  2383. WARN_ON(lreq->osd != osd);
  2384. dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
  2385. osd->o_osd, lreq, lreq->linger_id);
  2386. lreq->osd = NULL;
  2387. erase_linger(&osd->o_linger_requests, lreq);
  2388. put_osd(osd);
  2389. if (!osd_homeless(osd))
  2390. maybe_move_osd_to_lru(osd);
  2391. else
  2392. atomic_dec(&osd->o_osdc->num_homeless);
  2393. }
  2394. static bool __linger_registered(struct ceph_osd_linger_request *lreq)
  2395. {
  2396. verify_osdc_locked(lreq->osdc);
  2397. return !RB_EMPTY_NODE(&lreq->osdc_node);
  2398. }
  2399. static bool linger_registered(struct ceph_osd_linger_request *lreq)
  2400. {
  2401. struct ceph_osd_client *osdc = lreq->osdc;
  2402. bool registered;
  2403. down_read(&osdc->lock);
  2404. registered = __linger_registered(lreq);
  2405. up_read(&osdc->lock);
  2406. return registered;
  2407. }
  2408. static void linger_register(struct ceph_osd_linger_request *lreq)
  2409. {
  2410. struct ceph_osd_client *osdc = lreq->osdc;
  2411. verify_osdc_wrlocked(osdc);
  2412. WARN_ON(lreq->linger_id);
  2413. linger_get(lreq);
  2414. lreq->linger_id = ++osdc->last_linger_id;
  2415. insert_linger_osdc(&osdc->linger_requests, lreq);
  2416. }
  2417. static void linger_unregister(struct ceph_osd_linger_request *lreq)
  2418. {
  2419. struct ceph_osd_client *osdc = lreq->osdc;
  2420. verify_osdc_wrlocked(osdc);
  2421. erase_linger_osdc(&osdc->linger_requests, lreq);
  2422. linger_put(lreq);
  2423. }
  2424. static void cancel_linger_request(struct ceph_osd_request *req)
  2425. {
  2426. struct ceph_osd_linger_request *lreq = req->r_priv;
  2427. WARN_ON(!req->r_linger);
  2428. cancel_request(req);
  2429. linger_put(lreq);
  2430. }
  2431. struct linger_work {
  2432. struct work_struct work;
  2433. struct ceph_osd_linger_request *lreq;
  2434. struct list_head pending_item;
  2435. unsigned long queued_stamp;
  2436. union {
  2437. struct {
  2438. u64 notify_id;
  2439. u64 notifier_id;
  2440. void *payload; /* points into @msg front */
  2441. size_t payload_len;
  2442. struct ceph_msg *msg; /* for ceph_msg_put() */
  2443. } notify;
  2444. struct {
  2445. int err;
  2446. } error;
  2447. };
  2448. };
  2449. static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
  2450. work_func_t workfn)
  2451. {
  2452. struct linger_work *lwork;
  2453. lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
  2454. if (!lwork)
  2455. return NULL;
  2456. INIT_WORK(&lwork->work, workfn);
  2457. INIT_LIST_HEAD(&lwork->pending_item);
  2458. lwork->lreq = linger_get(lreq);
  2459. return lwork;
  2460. }
  2461. static void lwork_free(struct linger_work *lwork)
  2462. {
  2463. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2464. mutex_lock(&lreq->lock);
  2465. list_del(&lwork->pending_item);
  2466. mutex_unlock(&lreq->lock);
  2467. linger_put(lreq);
  2468. kfree(lwork);
  2469. }
  2470. static void lwork_queue(struct linger_work *lwork)
  2471. {
  2472. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2473. struct ceph_osd_client *osdc = lreq->osdc;
  2474. verify_lreq_locked(lreq);
  2475. WARN_ON(!list_empty(&lwork->pending_item));
  2476. lwork->queued_stamp = jiffies;
  2477. list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
  2478. queue_work(osdc->notify_wq, &lwork->work);
  2479. }
  2480. static void do_watch_notify(struct work_struct *w)
  2481. {
  2482. struct linger_work *lwork = container_of(w, struct linger_work, work);
  2483. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2484. if (!linger_registered(lreq)) {
  2485. dout("%s lreq %p not registered\n", __func__, lreq);
  2486. goto out;
  2487. }
  2488. WARN_ON(!lreq->is_watch);
  2489. dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
  2490. __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
  2491. lwork->notify.payload_len);
  2492. lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
  2493. lwork->notify.notifier_id, lwork->notify.payload,
  2494. lwork->notify.payload_len);
  2495. out:
  2496. ceph_msg_put(lwork->notify.msg);
  2497. lwork_free(lwork);
  2498. }
  2499. static void do_watch_error(struct work_struct *w)
  2500. {
  2501. struct linger_work *lwork = container_of(w, struct linger_work, work);
  2502. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2503. if (!linger_registered(lreq)) {
  2504. dout("%s lreq %p not registered\n", __func__, lreq);
  2505. goto out;
  2506. }
  2507. dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
  2508. lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
  2509. out:
  2510. lwork_free(lwork);
  2511. }
  2512. static void queue_watch_error(struct ceph_osd_linger_request *lreq)
  2513. {
  2514. struct linger_work *lwork;
  2515. lwork = lwork_alloc(lreq, do_watch_error);
  2516. if (!lwork) {
  2517. pr_err("failed to allocate error-lwork\n");
  2518. return;
  2519. }
  2520. lwork->error.err = lreq->last_error;
  2521. lwork_queue(lwork);
  2522. }
  2523. static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
  2524. int result)
  2525. {
  2526. if (!completion_done(&lreq->reg_commit_wait)) {
  2527. lreq->reg_commit_error = (result <= 0 ? result : 0);
  2528. complete_all(&lreq->reg_commit_wait);
  2529. }
  2530. }
  2531. static void linger_commit_cb(struct ceph_osd_request *req)
  2532. {
  2533. struct ceph_osd_linger_request *lreq = req->r_priv;
  2534. mutex_lock(&lreq->lock);
  2535. if (req != lreq->reg_req) {
  2536. dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
  2537. __func__, lreq, lreq->linger_id, req, lreq->reg_req);
  2538. goto out;
  2539. }
  2540. dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
  2541. lreq->linger_id, req->r_result);
  2542. linger_reg_commit_complete(lreq, req->r_result);
  2543. lreq->committed = true;
  2544. if (!lreq->is_watch) {
  2545. struct ceph_osd_data *osd_data =
  2546. osd_req_op_data(req, 0, notify, response_data);
  2547. void *p = page_address(osd_data->pages[0]);
  2548. WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
  2549. osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
  2550. /* make note of the notify_id */
  2551. if (req->r_ops[0].outdata_len >= sizeof(u64)) {
  2552. lreq->notify_id = ceph_decode_64(&p);
  2553. dout("lreq %p notify_id %llu\n", lreq,
  2554. lreq->notify_id);
  2555. } else {
  2556. dout("lreq %p no notify_id\n", lreq);
  2557. }
  2558. }
  2559. out:
  2560. mutex_unlock(&lreq->lock);
  2561. linger_put(lreq);
  2562. }
  2563. static int normalize_watch_error(int err)
  2564. {
  2565. /*
  2566. * Translate ENOENT -> ENOTCONN so that a delete->disconnection
  2567. * notification and a failure to reconnect because we raced with
  2568. * the delete appear the same to the user.
  2569. */
  2570. if (err == -ENOENT)
  2571. err = -ENOTCONN;
  2572. return err;
  2573. }
  2574. static void linger_reconnect_cb(struct ceph_osd_request *req)
  2575. {
  2576. struct ceph_osd_linger_request *lreq = req->r_priv;
  2577. mutex_lock(&lreq->lock);
  2578. if (req != lreq->reg_req) {
  2579. dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
  2580. __func__, lreq, lreq->linger_id, req, lreq->reg_req);
  2581. goto out;
  2582. }
  2583. dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
  2584. lreq, lreq->linger_id, req->r_result, lreq->last_error);
  2585. if (req->r_result < 0) {
  2586. if (!lreq->last_error) {
  2587. lreq->last_error = normalize_watch_error(req->r_result);
  2588. queue_watch_error(lreq);
  2589. }
  2590. }
  2591. out:
  2592. mutex_unlock(&lreq->lock);
  2593. linger_put(lreq);
  2594. }
  2595. static void send_linger(struct ceph_osd_linger_request *lreq)
  2596. {
  2597. struct ceph_osd_client *osdc = lreq->osdc;
  2598. struct ceph_osd_request *req;
  2599. int ret;
  2600. verify_osdc_wrlocked(osdc);
  2601. mutex_lock(&lreq->lock);
  2602. dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
  2603. if (lreq->reg_req) {
  2604. if (lreq->reg_req->r_osd)
  2605. cancel_linger_request(lreq->reg_req);
  2606. ceph_osdc_put_request(lreq->reg_req);
  2607. }
  2608. req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
  2609. BUG_ON(!req);
  2610. target_copy(&req->r_t, &lreq->t);
  2611. req->r_mtime = lreq->mtime;
  2612. if (lreq->is_watch && lreq->committed) {
  2613. osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_RECONNECT,
  2614. lreq->linger_id, ++lreq->register_gen);
  2615. dout("lreq %p reconnect register_gen %u\n", lreq,
  2616. req->r_ops[0].watch.gen);
  2617. req->r_callback = linger_reconnect_cb;
  2618. } else {
  2619. if (lreq->is_watch) {
  2620. osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_WATCH,
  2621. lreq->linger_id, 0);
  2622. } else {
  2623. lreq->notify_id = 0;
  2624. refcount_inc(&lreq->request_pl->refcnt);
  2625. osd_req_op_notify_init(req, 0, lreq->linger_id,
  2626. lreq->request_pl);
  2627. ceph_osd_data_pages_init(
  2628. osd_req_op_data(req, 0, notify, response_data),
  2629. lreq->notify_id_pages, PAGE_SIZE, 0, false, false);
  2630. }
  2631. dout("lreq %p register\n", lreq);
  2632. req->r_callback = linger_commit_cb;
  2633. }
  2634. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  2635. BUG_ON(ret);
  2636. req->r_priv = linger_get(lreq);
  2637. req->r_linger = true;
  2638. lreq->reg_req = req;
  2639. mutex_unlock(&lreq->lock);
  2640. submit_request(req, true);
  2641. }
  2642. static void linger_ping_cb(struct ceph_osd_request *req)
  2643. {
  2644. struct ceph_osd_linger_request *lreq = req->r_priv;
  2645. mutex_lock(&lreq->lock);
  2646. if (req != lreq->ping_req) {
  2647. dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
  2648. __func__, lreq, lreq->linger_id, req, lreq->ping_req);
  2649. goto out;
  2650. }
  2651. dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
  2652. __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
  2653. lreq->last_error);
  2654. if (lreq->register_gen == req->r_ops[0].watch.gen) {
  2655. if (!req->r_result) {
  2656. lreq->watch_valid_thru = lreq->ping_sent;
  2657. } else if (!lreq->last_error) {
  2658. lreq->last_error = normalize_watch_error(req->r_result);
  2659. queue_watch_error(lreq);
  2660. }
  2661. } else {
  2662. dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
  2663. lreq->register_gen, req->r_ops[0].watch.gen);
  2664. }
  2665. out:
  2666. mutex_unlock(&lreq->lock);
  2667. linger_put(lreq);
  2668. }
  2669. static void send_linger_ping(struct ceph_osd_linger_request *lreq)
  2670. {
  2671. struct ceph_osd_client *osdc = lreq->osdc;
  2672. struct ceph_osd_request *req;
  2673. int ret;
  2674. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
  2675. dout("%s PAUSERD\n", __func__);
  2676. return;
  2677. }
  2678. lreq->ping_sent = jiffies;
  2679. dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
  2680. __func__, lreq, lreq->linger_id, lreq->ping_sent,
  2681. lreq->register_gen);
  2682. if (lreq->ping_req) {
  2683. if (lreq->ping_req->r_osd)
  2684. cancel_linger_request(lreq->ping_req);
  2685. ceph_osdc_put_request(lreq->ping_req);
  2686. }
  2687. req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
  2688. BUG_ON(!req);
  2689. target_copy(&req->r_t, &lreq->t);
  2690. osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_PING, lreq->linger_id,
  2691. lreq->register_gen);
  2692. req->r_callback = linger_ping_cb;
  2693. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  2694. BUG_ON(ret);
  2695. req->r_priv = linger_get(lreq);
  2696. req->r_linger = true;
  2697. lreq->ping_req = req;
  2698. ceph_osdc_get_request(req);
  2699. account_request(req);
  2700. req->r_tid = atomic64_inc_return(&osdc->last_tid);
  2701. link_request(lreq->osd, req);
  2702. send_request(req);
  2703. }
  2704. static void linger_submit(struct ceph_osd_linger_request *lreq)
  2705. {
  2706. struct ceph_osd_client *osdc = lreq->osdc;
  2707. struct ceph_osd *osd;
  2708. down_write(&osdc->lock);
  2709. linger_register(lreq);
  2710. calc_target(osdc, &lreq->t, false);
  2711. osd = lookup_create_osd(osdc, lreq->t.osd, true);
  2712. link_linger(osd, lreq);
  2713. send_linger(lreq);
  2714. up_write(&osdc->lock);
  2715. }
  2716. static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
  2717. {
  2718. struct ceph_osd_client *osdc = lreq->osdc;
  2719. struct ceph_osd_linger_request *lookup_lreq;
  2720. verify_osdc_wrlocked(osdc);
  2721. lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
  2722. lreq->linger_id);
  2723. if (!lookup_lreq)
  2724. return;
  2725. WARN_ON(lookup_lreq != lreq);
  2726. erase_linger_mc(&osdc->linger_map_checks, lreq);
  2727. linger_put(lreq);
  2728. }
  2729. /*
  2730. * @lreq has to be both registered and linked.
  2731. */
  2732. static void __linger_cancel(struct ceph_osd_linger_request *lreq)
  2733. {
  2734. if (lreq->ping_req && lreq->ping_req->r_osd)
  2735. cancel_linger_request(lreq->ping_req);
  2736. if (lreq->reg_req && lreq->reg_req->r_osd)
  2737. cancel_linger_request(lreq->reg_req);
  2738. cancel_linger_map_check(lreq);
  2739. unlink_linger(lreq->osd, lreq);
  2740. linger_unregister(lreq);
  2741. }
  2742. static void linger_cancel(struct ceph_osd_linger_request *lreq)
  2743. {
  2744. struct ceph_osd_client *osdc = lreq->osdc;
  2745. down_write(&osdc->lock);
  2746. if (__linger_registered(lreq))
  2747. __linger_cancel(lreq);
  2748. up_write(&osdc->lock);
  2749. }
  2750. static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
  2751. static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
  2752. {
  2753. struct ceph_osd_client *osdc = lreq->osdc;
  2754. struct ceph_osdmap *map = osdc->osdmap;
  2755. verify_osdc_wrlocked(osdc);
  2756. WARN_ON(!map->epoch);
  2757. if (lreq->register_gen) {
  2758. lreq->map_dne_bound = map->epoch;
  2759. dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
  2760. lreq, lreq->linger_id);
  2761. } else {
  2762. dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
  2763. __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
  2764. map->epoch);
  2765. }
  2766. if (lreq->map_dne_bound) {
  2767. if (map->epoch >= lreq->map_dne_bound) {
  2768. /* we had a new enough map */
  2769. pr_info("linger_id %llu pool does not exist\n",
  2770. lreq->linger_id);
  2771. linger_reg_commit_complete(lreq, -ENOENT);
  2772. __linger_cancel(lreq);
  2773. }
  2774. } else {
  2775. send_linger_map_check(lreq);
  2776. }
  2777. }
  2778. static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
  2779. {
  2780. struct ceph_osd_client *osdc = &greq->monc->client->osdc;
  2781. struct ceph_osd_linger_request *lreq;
  2782. u64 linger_id = greq->private_data;
  2783. WARN_ON(greq->result || !greq->u.newest);
  2784. down_write(&osdc->lock);
  2785. lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
  2786. if (!lreq) {
  2787. dout("%s linger_id %llu dne\n", __func__, linger_id);
  2788. goto out_unlock;
  2789. }
  2790. dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
  2791. __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
  2792. greq->u.newest);
  2793. if (!lreq->map_dne_bound)
  2794. lreq->map_dne_bound = greq->u.newest;
  2795. erase_linger_mc(&osdc->linger_map_checks, lreq);
  2796. check_linger_pool_dne(lreq);
  2797. linger_put(lreq);
  2798. out_unlock:
  2799. up_write(&osdc->lock);
  2800. }
  2801. static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
  2802. {
  2803. struct ceph_osd_client *osdc = lreq->osdc;
  2804. struct ceph_osd_linger_request *lookup_lreq;
  2805. int ret;
  2806. verify_osdc_wrlocked(osdc);
  2807. lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
  2808. lreq->linger_id);
  2809. if (lookup_lreq) {
  2810. WARN_ON(lookup_lreq != lreq);
  2811. return;
  2812. }
  2813. linger_get(lreq);
  2814. insert_linger_mc(&osdc->linger_map_checks, lreq);
  2815. ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
  2816. linger_map_check_cb, lreq->linger_id);
  2817. WARN_ON(ret);
  2818. }
  2819. static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
  2820. {
  2821. int ret;
  2822. dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
  2823. ret = wait_for_completion_killable(&lreq->reg_commit_wait);
  2824. return ret ?: lreq->reg_commit_error;
  2825. }
  2826. static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq,
  2827. unsigned long timeout)
  2828. {
  2829. long left;
  2830. dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
  2831. left = wait_for_completion_killable_timeout(&lreq->notify_finish_wait,
  2832. ceph_timeout_jiffies(timeout));
  2833. if (left <= 0)
  2834. left = left ?: -ETIMEDOUT;
  2835. else
  2836. left = lreq->notify_finish_error; /* completed */
  2837. return left;
  2838. }
  2839. /*
  2840. * Timeout callback, called every N seconds. When 1 or more OSD
  2841. * requests has been active for more than N seconds, we send a keepalive
  2842. * (tag + timestamp) to its OSD to ensure any communications channel
  2843. * reset is detected.
  2844. */
  2845. static void handle_timeout(struct work_struct *work)
  2846. {
  2847. struct ceph_osd_client *osdc =
  2848. container_of(work, struct ceph_osd_client, timeout_work.work);
  2849. struct ceph_options *opts = osdc->client->options;
  2850. unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
  2851. unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
  2852. LIST_HEAD(slow_osds);
  2853. struct rb_node *n, *p;
  2854. dout("%s osdc %p\n", __func__, osdc);
  2855. down_write(&osdc->lock);
  2856. /*
  2857. * ping osds that are a bit slow. this ensures that if there
  2858. * is a break in the TCP connection we will notice, and reopen
  2859. * a connection with that osd (from the fault callback).
  2860. */
  2861. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  2862. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  2863. bool found = false;
  2864. for (p = rb_first(&osd->o_requests); p; ) {
  2865. struct ceph_osd_request *req =
  2866. rb_entry(p, struct ceph_osd_request, r_node);
  2867. p = rb_next(p); /* abort_request() */
  2868. if (time_before(req->r_stamp, cutoff)) {
  2869. dout(" req %p tid %llu on osd%d is laggy\n",
  2870. req, req->r_tid, osd->o_osd);
  2871. found = true;
  2872. }
  2873. if (opts->osd_request_timeout &&
  2874. time_before(req->r_start_stamp, expiry_cutoff)) {
  2875. pr_err_ratelimited("tid %llu on osd%d timeout\n",
  2876. req->r_tid, osd->o_osd);
  2877. abort_request(req, -ETIMEDOUT);
  2878. }
  2879. }
  2880. for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
  2881. struct ceph_osd_linger_request *lreq =
  2882. rb_entry(p, struct ceph_osd_linger_request, node);
  2883. dout(" lreq %p linger_id %llu is served by osd%d\n",
  2884. lreq, lreq->linger_id, osd->o_osd);
  2885. found = true;
  2886. mutex_lock(&lreq->lock);
  2887. if (lreq->is_watch && lreq->committed && !lreq->last_error)
  2888. send_linger_ping(lreq);
  2889. mutex_unlock(&lreq->lock);
  2890. }
  2891. if (found)
  2892. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  2893. }
  2894. if (opts->osd_request_timeout) {
  2895. for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
  2896. struct ceph_osd_request *req =
  2897. rb_entry(p, struct ceph_osd_request, r_node);
  2898. p = rb_next(p); /* abort_request() */
  2899. if (time_before(req->r_start_stamp, expiry_cutoff)) {
  2900. pr_err_ratelimited("tid %llu on osd%d timeout\n",
  2901. req->r_tid, osdc->homeless_osd.o_osd);
  2902. abort_request(req, -ETIMEDOUT);
  2903. }
  2904. }
  2905. }
  2906. if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
  2907. maybe_request_map(osdc);
  2908. while (!list_empty(&slow_osds)) {
  2909. struct ceph_osd *osd = list_first_entry(&slow_osds,
  2910. struct ceph_osd,
  2911. o_keepalive_item);
  2912. list_del_init(&osd->o_keepalive_item);
  2913. ceph_con_keepalive(&osd->o_con);
  2914. }
  2915. up_write(&osdc->lock);
  2916. schedule_delayed_work(&osdc->timeout_work,
  2917. osdc->client->options->osd_keepalive_timeout);
  2918. }
  2919. static void handle_osds_timeout(struct work_struct *work)
  2920. {
  2921. struct ceph_osd_client *osdc =
  2922. container_of(work, struct ceph_osd_client,
  2923. osds_timeout_work.work);
  2924. unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
  2925. struct ceph_osd *osd, *nosd;
  2926. dout("%s osdc %p\n", __func__, osdc);
  2927. down_write(&osdc->lock);
  2928. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  2929. if (time_before(jiffies, osd->lru_ttl))
  2930. break;
  2931. WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
  2932. WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
  2933. close_osd(osd);
  2934. }
  2935. up_write(&osdc->lock);
  2936. schedule_delayed_work(&osdc->osds_timeout_work,
  2937. round_jiffies_relative(delay));
  2938. }
  2939. static int ceph_oloc_decode(void **p, void *end,
  2940. struct ceph_object_locator *oloc)
  2941. {
  2942. u8 struct_v, struct_cv;
  2943. u32 len;
  2944. void *struct_end;
  2945. int ret = 0;
  2946. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  2947. struct_v = ceph_decode_8(p);
  2948. struct_cv = ceph_decode_8(p);
  2949. if (struct_v < 3) {
  2950. pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
  2951. struct_v, struct_cv);
  2952. goto e_inval;
  2953. }
  2954. if (struct_cv > 6) {
  2955. pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
  2956. struct_v, struct_cv);
  2957. goto e_inval;
  2958. }
  2959. len = ceph_decode_32(p);
  2960. ceph_decode_need(p, end, len, e_inval);
  2961. struct_end = *p + len;
  2962. oloc->pool = ceph_decode_64(p);
  2963. *p += 4; /* skip preferred */
  2964. len = ceph_decode_32(p);
  2965. if (len > 0) {
  2966. pr_warn("ceph_object_locator::key is set\n");
  2967. goto e_inval;
  2968. }
  2969. if (struct_v >= 5) {
  2970. bool changed = false;
  2971. len = ceph_decode_32(p);
  2972. if (len > 0) {
  2973. ceph_decode_need(p, end, len, e_inval);
  2974. if (!oloc->pool_ns ||
  2975. ceph_compare_string(oloc->pool_ns, *p, len))
  2976. changed = true;
  2977. *p += len;
  2978. } else {
  2979. if (oloc->pool_ns)
  2980. changed = true;
  2981. }
  2982. if (changed) {
  2983. /* redirect changes namespace */
  2984. pr_warn("ceph_object_locator::nspace is changed\n");
  2985. goto e_inval;
  2986. }
  2987. }
  2988. if (struct_v >= 6) {
  2989. s64 hash = ceph_decode_64(p);
  2990. if (hash != -1) {
  2991. pr_warn("ceph_object_locator::hash is set\n");
  2992. goto e_inval;
  2993. }
  2994. }
  2995. /* skip the rest */
  2996. *p = struct_end;
  2997. out:
  2998. return ret;
  2999. e_inval:
  3000. ret = -EINVAL;
  3001. goto out;
  3002. }
  3003. static int ceph_redirect_decode(void **p, void *end,
  3004. struct ceph_request_redirect *redir)
  3005. {
  3006. u8 struct_v, struct_cv;
  3007. u32 len;
  3008. void *struct_end;
  3009. int ret;
  3010. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  3011. struct_v = ceph_decode_8(p);
  3012. struct_cv = ceph_decode_8(p);
  3013. if (struct_cv > 1) {
  3014. pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
  3015. struct_v, struct_cv);
  3016. goto e_inval;
  3017. }
  3018. len = ceph_decode_32(p);
  3019. ceph_decode_need(p, end, len, e_inval);
  3020. struct_end = *p + len;
  3021. ret = ceph_oloc_decode(p, end, &redir->oloc);
  3022. if (ret)
  3023. goto out;
  3024. len = ceph_decode_32(p);
  3025. if (len > 0) {
  3026. pr_warn("ceph_request_redirect::object_name is set\n");
  3027. goto e_inval;
  3028. }
  3029. /* skip the rest */
  3030. *p = struct_end;
  3031. out:
  3032. return ret;
  3033. e_inval:
  3034. ret = -EINVAL;
  3035. goto out;
  3036. }
  3037. struct MOSDOpReply {
  3038. struct ceph_pg pgid;
  3039. u64 flags;
  3040. int result;
  3041. u32 epoch;
  3042. int num_ops;
  3043. u32 outdata_len[CEPH_OSD_MAX_OPS];
  3044. s32 rval[CEPH_OSD_MAX_OPS];
  3045. int retry_attempt;
  3046. struct ceph_eversion replay_version;
  3047. u64 user_version;
  3048. struct ceph_request_redirect redirect;
  3049. };
  3050. static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
  3051. {
  3052. void *p = msg->front.iov_base;
  3053. void *const end = p + msg->front.iov_len;
  3054. u16 version = le16_to_cpu(msg->hdr.version);
  3055. struct ceph_eversion bad_replay_version;
  3056. u8 decode_redir;
  3057. u32 len;
  3058. int ret;
  3059. int i;
  3060. ceph_decode_32_safe(&p, end, len, e_inval);
  3061. ceph_decode_need(&p, end, len, e_inval);
  3062. p += len; /* skip oid */
  3063. ret = ceph_decode_pgid(&p, end, &m->pgid);
  3064. if (ret)
  3065. return ret;
  3066. ceph_decode_64_safe(&p, end, m->flags, e_inval);
  3067. ceph_decode_32_safe(&p, end, m->result, e_inval);
  3068. ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
  3069. memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
  3070. p += sizeof(bad_replay_version);
  3071. ceph_decode_32_safe(&p, end, m->epoch, e_inval);
  3072. ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
  3073. if (m->num_ops > ARRAY_SIZE(m->outdata_len))
  3074. goto e_inval;
  3075. ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
  3076. e_inval);
  3077. for (i = 0; i < m->num_ops; i++) {
  3078. struct ceph_osd_op *op = p;
  3079. m->outdata_len[i] = le32_to_cpu(op->payload_len);
  3080. p += sizeof(*op);
  3081. }
  3082. ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
  3083. for (i = 0; i < m->num_ops; i++)
  3084. ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
  3085. if (version >= 5) {
  3086. ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
  3087. memcpy(&m->replay_version, p, sizeof(m->replay_version));
  3088. p += sizeof(m->replay_version);
  3089. ceph_decode_64_safe(&p, end, m->user_version, e_inval);
  3090. } else {
  3091. m->replay_version = bad_replay_version; /* struct */
  3092. m->user_version = le64_to_cpu(m->replay_version.version);
  3093. }
  3094. if (version >= 6) {
  3095. if (version >= 7)
  3096. ceph_decode_8_safe(&p, end, decode_redir, e_inval);
  3097. else
  3098. decode_redir = 1;
  3099. } else {
  3100. decode_redir = 0;
  3101. }
  3102. if (decode_redir) {
  3103. ret = ceph_redirect_decode(&p, end, &m->redirect);
  3104. if (ret)
  3105. return ret;
  3106. } else {
  3107. ceph_oloc_init(&m->redirect.oloc);
  3108. }
  3109. return 0;
  3110. e_inval:
  3111. return -EINVAL;
  3112. }
  3113. /*
  3114. * Handle MOSDOpReply. Set ->r_result and call the callback if it is
  3115. * specified.
  3116. */
  3117. static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
  3118. {
  3119. struct ceph_osd_client *osdc = osd->o_osdc;
  3120. struct ceph_osd_request *req;
  3121. struct MOSDOpReply m;
  3122. u64 tid = le64_to_cpu(msg->hdr.tid);
  3123. u32 data_len = 0;
  3124. int ret;
  3125. int i;
  3126. dout("%s msg %p tid %llu\n", __func__, msg, tid);
  3127. down_read(&osdc->lock);
  3128. if (!osd_registered(osd)) {
  3129. dout("%s osd%d unknown\n", __func__, osd->o_osd);
  3130. goto out_unlock_osdc;
  3131. }
  3132. WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
  3133. mutex_lock(&osd->lock);
  3134. req = lookup_request(&osd->o_requests, tid);
  3135. if (!req) {
  3136. dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
  3137. goto out_unlock_session;
  3138. }
  3139. m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
  3140. ret = decode_MOSDOpReply(msg, &m);
  3141. m.redirect.oloc.pool_ns = NULL;
  3142. if (ret) {
  3143. pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
  3144. req->r_tid, ret);
  3145. ceph_msg_dump(msg);
  3146. goto fail_request;
  3147. }
  3148. dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
  3149. __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
  3150. m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
  3151. le64_to_cpu(m.replay_version.version), m.user_version);
  3152. if (m.retry_attempt >= 0) {
  3153. if (m.retry_attempt != req->r_attempts - 1) {
  3154. dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
  3155. req, req->r_tid, m.retry_attempt,
  3156. req->r_attempts - 1);
  3157. goto out_unlock_session;
  3158. }
  3159. } else {
  3160. WARN_ON(1); /* MOSDOpReply v4 is assumed */
  3161. }
  3162. if (!ceph_oloc_empty(&m.redirect.oloc)) {
  3163. dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
  3164. m.redirect.oloc.pool);
  3165. unlink_request(osd, req);
  3166. mutex_unlock(&osd->lock);
  3167. /*
  3168. * Not ceph_oloc_copy() - changing pool_ns is not
  3169. * supported.
  3170. */
  3171. req->r_t.target_oloc.pool = m.redirect.oloc.pool;
  3172. req->r_flags |= CEPH_OSD_FLAG_REDIRECTED |
  3173. CEPH_OSD_FLAG_IGNORE_OVERLAY |
  3174. CEPH_OSD_FLAG_IGNORE_CACHE;
  3175. req->r_tid = 0;
  3176. __submit_request(req, false);
  3177. goto out_unlock_osdc;
  3178. }
  3179. if (m.result == -EAGAIN) {
  3180. dout("req %p tid %llu EAGAIN\n", req, req->r_tid);
  3181. unlink_request(osd, req);
  3182. mutex_unlock(&osd->lock);
  3183. /*
  3184. * The object is missing on the replica or not (yet)
  3185. * readable. Clear pgid to force a resend to the primary
  3186. * via legacy_change.
  3187. */
  3188. req->r_t.pgid.pool = 0;
  3189. req->r_t.pgid.seed = 0;
  3190. WARN_ON(!req->r_t.used_replica);
  3191. req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS |
  3192. CEPH_OSD_FLAG_LOCALIZE_READS);
  3193. req->r_tid = 0;
  3194. __submit_request(req, false);
  3195. goto out_unlock_osdc;
  3196. }
  3197. if (m.num_ops != req->r_num_ops) {
  3198. pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
  3199. req->r_num_ops, req->r_tid);
  3200. goto fail_request;
  3201. }
  3202. for (i = 0; i < req->r_num_ops; i++) {
  3203. dout(" req %p tid %llu op %d rval %d len %u\n", req,
  3204. req->r_tid, i, m.rval[i], m.outdata_len[i]);
  3205. req->r_ops[i].rval = m.rval[i];
  3206. req->r_ops[i].outdata_len = m.outdata_len[i];
  3207. data_len += m.outdata_len[i];
  3208. }
  3209. if (data_len != le32_to_cpu(msg->hdr.data_len)) {
  3210. pr_err("sum of lens %u != %u for tid %llu\n", data_len,
  3211. le32_to_cpu(msg->hdr.data_len), req->r_tid);
  3212. goto fail_request;
  3213. }
  3214. dout("%s req %p tid %llu result %d data_len %u\n", __func__,
  3215. req, req->r_tid, m.result, data_len);
  3216. /*
  3217. * Since we only ever request ONDISK, we should only ever get
  3218. * one (type of) reply back.
  3219. */
  3220. WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
  3221. req->r_result = m.result ?: data_len;
  3222. finish_request(req);
  3223. mutex_unlock(&osd->lock);
  3224. up_read(&osdc->lock);
  3225. __complete_request(req);
  3226. return;
  3227. fail_request:
  3228. complete_request(req, -EIO);
  3229. out_unlock_session:
  3230. mutex_unlock(&osd->lock);
  3231. out_unlock_osdc:
  3232. up_read(&osdc->lock);
  3233. }
  3234. static void set_pool_was_full(struct ceph_osd_client *osdc)
  3235. {
  3236. struct rb_node *n;
  3237. for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
  3238. struct ceph_pg_pool_info *pi =
  3239. rb_entry(n, struct ceph_pg_pool_info, node);
  3240. pi->was_full = __pool_full(pi);
  3241. }
  3242. }
  3243. static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
  3244. {
  3245. struct ceph_pg_pool_info *pi;
  3246. pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
  3247. if (!pi)
  3248. return false;
  3249. return pi->was_full && !__pool_full(pi);
  3250. }
  3251. static enum calc_target_result
  3252. recalc_linger_target(struct ceph_osd_linger_request *lreq)
  3253. {
  3254. struct ceph_osd_client *osdc = lreq->osdc;
  3255. enum calc_target_result ct_res;
  3256. ct_res = calc_target(osdc, &lreq->t, true);
  3257. if (ct_res == CALC_TARGET_NEED_RESEND) {
  3258. struct ceph_osd *osd;
  3259. osd = lookup_create_osd(osdc, lreq->t.osd, true);
  3260. if (osd != lreq->osd) {
  3261. unlink_linger(lreq->osd, lreq);
  3262. link_linger(osd, lreq);
  3263. }
  3264. }
  3265. return ct_res;
  3266. }
  3267. /*
  3268. * Requeue requests whose mapping to an OSD has changed.
  3269. */
  3270. static void scan_requests(struct ceph_osd *osd,
  3271. bool force_resend,
  3272. bool cleared_full,
  3273. bool check_pool_cleared_full,
  3274. struct rb_root *need_resend,
  3275. struct list_head *need_resend_linger)
  3276. {
  3277. struct ceph_osd_client *osdc = osd->o_osdc;
  3278. struct rb_node *n;
  3279. bool force_resend_writes;
  3280. for (n = rb_first(&osd->o_linger_requests); n; ) {
  3281. struct ceph_osd_linger_request *lreq =
  3282. rb_entry(n, struct ceph_osd_linger_request, node);
  3283. enum calc_target_result ct_res;
  3284. n = rb_next(n); /* recalc_linger_target() */
  3285. dout("%s lreq %p linger_id %llu\n", __func__, lreq,
  3286. lreq->linger_id);
  3287. ct_res = recalc_linger_target(lreq);
  3288. switch (ct_res) {
  3289. case CALC_TARGET_NO_ACTION:
  3290. force_resend_writes = cleared_full ||
  3291. (check_pool_cleared_full &&
  3292. pool_cleared_full(osdc, lreq->t.base_oloc.pool));
  3293. if (!force_resend && !force_resend_writes)
  3294. break;
  3295. fallthrough;
  3296. case CALC_TARGET_NEED_RESEND:
  3297. cancel_linger_map_check(lreq);
  3298. /*
  3299. * scan_requests() for the previous epoch(s)
  3300. * may have already added it to the list, since
  3301. * it's not unlinked here.
  3302. */
  3303. if (list_empty(&lreq->scan_item))
  3304. list_add_tail(&lreq->scan_item, need_resend_linger);
  3305. break;
  3306. case CALC_TARGET_POOL_DNE:
  3307. list_del_init(&lreq->scan_item);
  3308. check_linger_pool_dne(lreq);
  3309. break;
  3310. }
  3311. }
  3312. for (n = rb_first(&osd->o_requests); n; ) {
  3313. struct ceph_osd_request *req =
  3314. rb_entry(n, struct ceph_osd_request, r_node);
  3315. enum calc_target_result ct_res;
  3316. n = rb_next(n); /* unlink_request(), check_pool_dne() */
  3317. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  3318. ct_res = calc_target(osdc, &req->r_t, false);
  3319. switch (ct_res) {
  3320. case CALC_TARGET_NO_ACTION:
  3321. force_resend_writes = cleared_full ||
  3322. (check_pool_cleared_full &&
  3323. pool_cleared_full(osdc, req->r_t.base_oloc.pool));
  3324. if (!force_resend &&
  3325. (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
  3326. !force_resend_writes))
  3327. break;
  3328. fallthrough;
  3329. case CALC_TARGET_NEED_RESEND:
  3330. cancel_map_check(req);
  3331. unlink_request(osd, req);
  3332. insert_request(need_resend, req);
  3333. break;
  3334. case CALC_TARGET_POOL_DNE:
  3335. check_pool_dne(req);
  3336. break;
  3337. }
  3338. }
  3339. }
  3340. static int handle_one_map(struct ceph_osd_client *osdc,
  3341. void *p, void *end, bool incremental,
  3342. struct rb_root *need_resend,
  3343. struct list_head *need_resend_linger)
  3344. {
  3345. struct ceph_osdmap *newmap;
  3346. struct rb_node *n;
  3347. bool skipped_map = false;
  3348. bool was_full;
  3349. was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
  3350. set_pool_was_full(osdc);
  3351. if (incremental)
  3352. newmap = osdmap_apply_incremental(&p, end,
  3353. ceph_msgr2(osdc->client),
  3354. osdc->osdmap);
  3355. else
  3356. newmap = ceph_osdmap_decode(&p, end, ceph_msgr2(osdc->client));
  3357. if (IS_ERR(newmap))
  3358. return PTR_ERR(newmap);
  3359. if (newmap != osdc->osdmap) {
  3360. /*
  3361. * Preserve ->was_full before destroying the old map.
  3362. * For pools that weren't in the old map, ->was_full
  3363. * should be false.
  3364. */
  3365. for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
  3366. struct ceph_pg_pool_info *pi =
  3367. rb_entry(n, struct ceph_pg_pool_info, node);
  3368. struct ceph_pg_pool_info *old_pi;
  3369. old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
  3370. if (old_pi)
  3371. pi->was_full = old_pi->was_full;
  3372. else
  3373. WARN_ON(pi->was_full);
  3374. }
  3375. if (osdc->osdmap->epoch &&
  3376. osdc->osdmap->epoch + 1 < newmap->epoch) {
  3377. WARN_ON(incremental);
  3378. skipped_map = true;
  3379. }
  3380. ceph_osdmap_destroy(osdc->osdmap);
  3381. osdc->osdmap = newmap;
  3382. }
  3383. was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
  3384. scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
  3385. need_resend, need_resend_linger);
  3386. for (n = rb_first(&osdc->osds); n; ) {
  3387. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  3388. n = rb_next(n); /* close_osd() */
  3389. scan_requests(osd, skipped_map, was_full, true, need_resend,
  3390. need_resend_linger);
  3391. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  3392. memcmp(&osd->o_con.peer_addr,
  3393. ceph_osd_addr(osdc->osdmap, osd->o_osd),
  3394. sizeof(struct ceph_entity_addr)))
  3395. close_osd(osd);
  3396. }
  3397. return 0;
  3398. }
  3399. static void kick_requests(struct ceph_osd_client *osdc,
  3400. struct rb_root *need_resend,
  3401. struct list_head *need_resend_linger)
  3402. {
  3403. struct ceph_osd_linger_request *lreq, *nlreq;
  3404. enum calc_target_result ct_res;
  3405. struct rb_node *n;
  3406. /* make sure need_resend targets reflect latest map */
  3407. for (n = rb_first(need_resend); n; ) {
  3408. struct ceph_osd_request *req =
  3409. rb_entry(n, struct ceph_osd_request, r_node);
  3410. n = rb_next(n);
  3411. if (req->r_t.epoch < osdc->osdmap->epoch) {
  3412. ct_res = calc_target(osdc, &req->r_t, false);
  3413. if (ct_res == CALC_TARGET_POOL_DNE) {
  3414. erase_request(need_resend, req);
  3415. check_pool_dne(req);
  3416. }
  3417. }
  3418. }
  3419. for (n = rb_first(need_resend); n; ) {
  3420. struct ceph_osd_request *req =
  3421. rb_entry(n, struct ceph_osd_request, r_node);
  3422. struct ceph_osd *osd;
  3423. n = rb_next(n);
  3424. erase_request(need_resend, req); /* before link_request() */
  3425. osd = lookup_create_osd(osdc, req->r_t.osd, true);
  3426. link_request(osd, req);
  3427. if (!req->r_linger) {
  3428. if (!osd_homeless(osd) && !req->r_t.paused)
  3429. send_request(req);
  3430. } else {
  3431. cancel_linger_request(req);
  3432. }
  3433. }
  3434. list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
  3435. if (!osd_homeless(lreq->osd))
  3436. send_linger(lreq);
  3437. list_del_init(&lreq->scan_item);
  3438. }
  3439. }
  3440. /*
  3441. * Process updated osd map.
  3442. *
  3443. * The message contains any number of incremental and full maps, normally
  3444. * indicating some sort of topology change in the cluster. Kick requests
  3445. * off to different OSDs as needed.
  3446. */
  3447. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  3448. {
  3449. void *p = msg->front.iov_base;
  3450. void *const end = p + msg->front.iov_len;
  3451. u32 nr_maps, maplen;
  3452. u32 epoch;
  3453. struct ceph_fsid fsid;
  3454. struct rb_root need_resend = RB_ROOT;
  3455. LIST_HEAD(need_resend_linger);
  3456. bool handled_incremental = false;
  3457. bool was_pauserd, was_pausewr;
  3458. bool pauserd, pausewr;
  3459. int err;
  3460. dout("%s have %u\n", __func__, osdc->osdmap->epoch);
  3461. down_write(&osdc->lock);
  3462. /* verify fsid */
  3463. ceph_decode_need(&p, end, sizeof(fsid), bad);
  3464. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  3465. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  3466. goto bad;
  3467. was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
  3468. was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
  3469. ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  3470. have_pool_full(osdc);
  3471. /* incremental maps */
  3472. ceph_decode_32_safe(&p, end, nr_maps, bad);
  3473. dout(" %d inc maps\n", nr_maps);
  3474. while (nr_maps > 0) {
  3475. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  3476. epoch = ceph_decode_32(&p);
  3477. maplen = ceph_decode_32(&p);
  3478. ceph_decode_need(&p, end, maplen, bad);
  3479. if (osdc->osdmap->epoch &&
  3480. osdc->osdmap->epoch + 1 == epoch) {
  3481. dout("applying incremental map %u len %d\n",
  3482. epoch, maplen);
  3483. err = handle_one_map(osdc, p, p + maplen, true,
  3484. &need_resend, &need_resend_linger);
  3485. if (err)
  3486. goto bad;
  3487. handled_incremental = true;
  3488. } else {
  3489. dout("ignoring incremental map %u len %d\n",
  3490. epoch, maplen);
  3491. }
  3492. p += maplen;
  3493. nr_maps--;
  3494. }
  3495. if (handled_incremental)
  3496. goto done;
  3497. /* full maps */
  3498. ceph_decode_32_safe(&p, end, nr_maps, bad);
  3499. dout(" %d full maps\n", nr_maps);
  3500. while (nr_maps) {
  3501. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  3502. epoch = ceph_decode_32(&p);
  3503. maplen = ceph_decode_32(&p);
  3504. ceph_decode_need(&p, end, maplen, bad);
  3505. if (nr_maps > 1) {
  3506. dout("skipping non-latest full map %u len %d\n",
  3507. epoch, maplen);
  3508. } else if (osdc->osdmap->epoch >= epoch) {
  3509. dout("skipping full map %u len %d, "
  3510. "older than our %u\n", epoch, maplen,
  3511. osdc->osdmap->epoch);
  3512. } else {
  3513. dout("taking full map %u len %d\n", epoch, maplen);
  3514. err = handle_one_map(osdc, p, p + maplen, false,
  3515. &need_resend, &need_resend_linger);
  3516. if (err)
  3517. goto bad;
  3518. }
  3519. p += maplen;
  3520. nr_maps--;
  3521. }
  3522. done:
  3523. /*
  3524. * subscribe to subsequent osdmap updates if full to ensure
  3525. * we find out when we are no longer full and stop returning
  3526. * ENOSPC.
  3527. */
  3528. pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
  3529. pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
  3530. ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  3531. have_pool_full(osdc);
  3532. if (was_pauserd || was_pausewr || pauserd || pausewr ||
  3533. osdc->osdmap->epoch < osdc->epoch_barrier)
  3534. maybe_request_map(osdc);
  3535. kick_requests(osdc, &need_resend, &need_resend_linger);
  3536. ceph_osdc_abort_on_full(osdc);
  3537. ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
  3538. osdc->osdmap->epoch);
  3539. up_write(&osdc->lock);
  3540. wake_up_all(&osdc->client->auth_wq);
  3541. return;
  3542. bad:
  3543. pr_err("osdc handle_map corrupt msg\n");
  3544. ceph_msg_dump(msg);
  3545. up_write(&osdc->lock);
  3546. }
  3547. /*
  3548. * Resubmit requests pending on the given osd.
  3549. */
  3550. static void kick_osd_requests(struct ceph_osd *osd)
  3551. {
  3552. struct rb_node *n;
  3553. clear_backoffs(osd);
  3554. for (n = rb_first(&osd->o_requests); n; ) {
  3555. struct ceph_osd_request *req =
  3556. rb_entry(n, struct ceph_osd_request, r_node);
  3557. n = rb_next(n); /* cancel_linger_request() */
  3558. if (!req->r_linger) {
  3559. if (!req->r_t.paused)
  3560. send_request(req);
  3561. } else {
  3562. cancel_linger_request(req);
  3563. }
  3564. }
  3565. for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
  3566. struct ceph_osd_linger_request *lreq =
  3567. rb_entry(n, struct ceph_osd_linger_request, node);
  3568. send_linger(lreq);
  3569. }
  3570. }
  3571. /*
  3572. * If the osd connection drops, we need to resubmit all requests.
  3573. */
  3574. static void osd_fault(struct ceph_connection *con)
  3575. {
  3576. struct ceph_osd *osd = con->private;
  3577. struct ceph_osd_client *osdc = osd->o_osdc;
  3578. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  3579. down_write(&osdc->lock);
  3580. if (!osd_registered(osd)) {
  3581. dout("%s osd%d unknown\n", __func__, osd->o_osd);
  3582. goto out_unlock;
  3583. }
  3584. if (!reopen_osd(osd))
  3585. kick_osd_requests(osd);
  3586. maybe_request_map(osdc);
  3587. out_unlock:
  3588. up_write(&osdc->lock);
  3589. }
  3590. struct MOSDBackoff {
  3591. struct ceph_spg spgid;
  3592. u32 map_epoch;
  3593. u8 op;
  3594. u64 id;
  3595. struct ceph_hobject_id *begin;
  3596. struct ceph_hobject_id *end;
  3597. };
  3598. static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
  3599. {
  3600. void *p = msg->front.iov_base;
  3601. void *const end = p + msg->front.iov_len;
  3602. u8 struct_v;
  3603. u32 struct_len;
  3604. int ret;
  3605. ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
  3606. if (ret)
  3607. return ret;
  3608. ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
  3609. if (ret)
  3610. return ret;
  3611. ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
  3612. ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
  3613. ceph_decode_8_safe(&p, end, m->op, e_inval);
  3614. ceph_decode_64_safe(&p, end, m->id, e_inval);
  3615. m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
  3616. if (!m->begin)
  3617. return -ENOMEM;
  3618. ret = decode_hoid(&p, end, m->begin);
  3619. if (ret) {
  3620. free_hoid(m->begin);
  3621. return ret;
  3622. }
  3623. m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
  3624. if (!m->end) {
  3625. free_hoid(m->begin);
  3626. return -ENOMEM;
  3627. }
  3628. ret = decode_hoid(&p, end, m->end);
  3629. if (ret) {
  3630. free_hoid(m->begin);
  3631. free_hoid(m->end);
  3632. return ret;
  3633. }
  3634. return 0;
  3635. e_inval:
  3636. return -EINVAL;
  3637. }
  3638. static struct ceph_msg *create_backoff_message(
  3639. const struct ceph_osd_backoff *backoff,
  3640. u32 map_epoch)
  3641. {
  3642. struct ceph_msg *msg;
  3643. void *p, *end;
  3644. int msg_size;
  3645. msg_size = CEPH_ENCODING_START_BLK_LEN +
  3646. CEPH_PGID_ENCODING_LEN + 1; /* spgid */
  3647. msg_size += 4 + 1 + 8; /* map_epoch, op, id */
  3648. msg_size += CEPH_ENCODING_START_BLK_LEN +
  3649. hoid_encoding_size(backoff->begin);
  3650. msg_size += CEPH_ENCODING_START_BLK_LEN +
  3651. hoid_encoding_size(backoff->end);
  3652. msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
  3653. if (!msg)
  3654. return NULL;
  3655. p = msg->front.iov_base;
  3656. end = p + msg->front_alloc_len;
  3657. encode_spgid(&p, &backoff->spgid);
  3658. ceph_encode_32(&p, map_epoch);
  3659. ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
  3660. ceph_encode_64(&p, backoff->id);
  3661. encode_hoid(&p, end, backoff->begin);
  3662. encode_hoid(&p, end, backoff->end);
  3663. BUG_ON(p != end);
  3664. msg->front.iov_len = p - msg->front.iov_base;
  3665. msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
  3666. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  3667. return msg;
  3668. }
  3669. static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
  3670. {
  3671. struct ceph_spg_mapping *spg;
  3672. struct ceph_osd_backoff *backoff;
  3673. struct ceph_msg *msg;
  3674. dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
  3675. m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
  3676. spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
  3677. if (!spg) {
  3678. spg = alloc_spg_mapping();
  3679. if (!spg) {
  3680. pr_err("%s failed to allocate spg\n", __func__);
  3681. return;
  3682. }
  3683. spg->spgid = m->spgid; /* struct */
  3684. insert_spg_mapping(&osd->o_backoff_mappings, spg);
  3685. }
  3686. backoff = alloc_backoff();
  3687. if (!backoff) {
  3688. pr_err("%s failed to allocate backoff\n", __func__);
  3689. return;
  3690. }
  3691. backoff->spgid = m->spgid; /* struct */
  3692. backoff->id = m->id;
  3693. backoff->begin = m->begin;
  3694. m->begin = NULL; /* backoff now owns this */
  3695. backoff->end = m->end;
  3696. m->end = NULL; /* ditto */
  3697. insert_backoff(&spg->backoffs, backoff);
  3698. insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
  3699. /*
  3700. * Ack with original backoff's epoch so that the OSD can
  3701. * discard this if there was a PG split.
  3702. */
  3703. msg = create_backoff_message(backoff, m->map_epoch);
  3704. if (!msg) {
  3705. pr_err("%s failed to allocate msg\n", __func__);
  3706. return;
  3707. }
  3708. ceph_con_send(&osd->o_con, msg);
  3709. }
  3710. static bool target_contained_by(const struct ceph_osd_request_target *t,
  3711. const struct ceph_hobject_id *begin,
  3712. const struct ceph_hobject_id *end)
  3713. {
  3714. struct ceph_hobject_id hoid;
  3715. int cmp;
  3716. hoid_fill_from_target(&hoid, t);
  3717. cmp = hoid_compare(&hoid, begin);
  3718. return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
  3719. }
  3720. static void handle_backoff_unblock(struct ceph_osd *osd,
  3721. const struct MOSDBackoff *m)
  3722. {
  3723. struct ceph_spg_mapping *spg;
  3724. struct ceph_osd_backoff *backoff;
  3725. struct rb_node *n;
  3726. dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
  3727. m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
  3728. backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
  3729. if (!backoff) {
  3730. pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
  3731. __func__, osd->o_osd, m->spgid.pgid.pool,
  3732. m->spgid.pgid.seed, m->spgid.shard, m->id);
  3733. return;
  3734. }
  3735. if (hoid_compare(backoff->begin, m->begin) &&
  3736. hoid_compare(backoff->end, m->end)) {
  3737. pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
  3738. __func__, osd->o_osd, m->spgid.pgid.pool,
  3739. m->spgid.pgid.seed, m->spgid.shard, m->id);
  3740. /* unblock it anyway... */
  3741. }
  3742. spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
  3743. BUG_ON(!spg);
  3744. erase_backoff(&spg->backoffs, backoff);
  3745. erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
  3746. free_backoff(backoff);
  3747. if (RB_EMPTY_ROOT(&spg->backoffs)) {
  3748. erase_spg_mapping(&osd->o_backoff_mappings, spg);
  3749. free_spg_mapping(spg);
  3750. }
  3751. for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
  3752. struct ceph_osd_request *req =
  3753. rb_entry(n, struct ceph_osd_request, r_node);
  3754. if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
  3755. /*
  3756. * Match against @m, not @backoff -- the PG may
  3757. * have split on the OSD.
  3758. */
  3759. if (target_contained_by(&req->r_t, m->begin, m->end)) {
  3760. /*
  3761. * If no other installed backoff applies,
  3762. * resend.
  3763. */
  3764. send_request(req);
  3765. }
  3766. }
  3767. }
  3768. }
  3769. static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
  3770. {
  3771. struct ceph_osd_client *osdc = osd->o_osdc;
  3772. struct MOSDBackoff m;
  3773. int ret;
  3774. down_read(&osdc->lock);
  3775. if (!osd_registered(osd)) {
  3776. dout("%s osd%d unknown\n", __func__, osd->o_osd);
  3777. up_read(&osdc->lock);
  3778. return;
  3779. }
  3780. WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
  3781. mutex_lock(&osd->lock);
  3782. ret = decode_MOSDBackoff(msg, &m);
  3783. if (ret) {
  3784. pr_err("failed to decode MOSDBackoff: %d\n", ret);
  3785. ceph_msg_dump(msg);
  3786. goto out_unlock;
  3787. }
  3788. switch (m.op) {
  3789. case CEPH_OSD_BACKOFF_OP_BLOCK:
  3790. handle_backoff_block(osd, &m);
  3791. break;
  3792. case CEPH_OSD_BACKOFF_OP_UNBLOCK:
  3793. handle_backoff_unblock(osd, &m);
  3794. break;
  3795. default:
  3796. pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
  3797. }
  3798. free_hoid(m.begin);
  3799. free_hoid(m.end);
  3800. out_unlock:
  3801. mutex_unlock(&osd->lock);
  3802. up_read(&osdc->lock);
  3803. }
  3804. /*
  3805. * Process osd watch notifications
  3806. */
  3807. static void handle_watch_notify(struct ceph_osd_client *osdc,
  3808. struct ceph_msg *msg)
  3809. {
  3810. void *p = msg->front.iov_base;
  3811. void *const end = p + msg->front.iov_len;
  3812. struct ceph_osd_linger_request *lreq;
  3813. struct linger_work *lwork;
  3814. u8 proto_ver, opcode;
  3815. u64 cookie, notify_id;
  3816. u64 notifier_id = 0;
  3817. s32 return_code = 0;
  3818. void *payload = NULL;
  3819. u32 payload_len = 0;
  3820. ceph_decode_8_safe(&p, end, proto_ver, bad);
  3821. ceph_decode_8_safe(&p, end, opcode, bad);
  3822. ceph_decode_64_safe(&p, end, cookie, bad);
  3823. p += 8; /* skip ver */
  3824. ceph_decode_64_safe(&p, end, notify_id, bad);
  3825. if (proto_ver >= 1) {
  3826. ceph_decode_32_safe(&p, end, payload_len, bad);
  3827. ceph_decode_need(&p, end, payload_len, bad);
  3828. payload = p;
  3829. p += payload_len;
  3830. }
  3831. if (le16_to_cpu(msg->hdr.version) >= 2)
  3832. ceph_decode_32_safe(&p, end, return_code, bad);
  3833. if (le16_to_cpu(msg->hdr.version) >= 3)
  3834. ceph_decode_64_safe(&p, end, notifier_id, bad);
  3835. down_read(&osdc->lock);
  3836. lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
  3837. if (!lreq) {
  3838. dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
  3839. cookie);
  3840. goto out_unlock_osdc;
  3841. }
  3842. mutex_lock(&lreq->lock);
  3843. dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
  3844. opcode, cookie, lreq, lreq->is_watch);
  3845. if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
  3846. if (!lreq->last_error) {
  3847. lreq->last_error = -ENOTCONN;
  3848. queue_watch_error(lreq);
  3849. }
  3850. } else if (!lreq->is_watch) {
  3851. /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
  3852. if (lreq->notify_id && lreq->notify_id != notify_id) {
  3853. dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
  3854. lreq->notify_id, notify_id);
  3855. } else if (!completion_done(&lreq->notify_finish_wait)) {
  3856. struct ceph_msg_data *data =
  3857. msg->num_data_items ? &msg->data[0] : NULL;
  3858. if (data) {
  3859. if (lreq->preply_pages) {
  3860. WARN_ON(data->type !=
  3861. CEPH_MSG_DATA_PAGES);
  3862. *lreq->preply_pages = data->pages;
  3863. *lreq->preply_len = data->length;
  3864. data->own_pages = false;
  3865. }
  3866. }
  3867. lreq->notify_finish_error = return_code;
  3868. complete_all(&lreq->notify_finish_wait);
  3869. }
  3870. } else {
  3871. /* CEPH_WATCH_EVENT_NOTIFY */
  3872. lwork = lwork_alloc(lreq, do_watch_notify);
  3873. if (!lwork) {
  3874. pr_err("failed to allocate notify-lwork\n");
  3875. goto out_unlock_lreq;
  3876. }
  3877. lwork->notify.notify_id = notify_id;
  3878. lwork->notify.notifier_id = notifier_id;
  3879. lwork->notify.payload = payload;
  3880. lwork->notify.payload_len = payload_len;
  3881. lwork->notify.msg = ceph_msg_get(msg);
  3882. lwork_queue(lwork);
  3883. }
  3884. out_unlock_lreq:
  3885. mutex_unlock(&lreq->lock);
  3886. out_unlock_osdc:
  3887. up_read(&osdc->lock);
  3888. return;
  3889. bad:
  3890. pr_err("osdc handle_watch_notify corrupt msg\n");
  3891. }
  3892. /*
  3893. * Register request, send initial attempt.
  3894. */
  3895. void ceph_osdc_start_request(struct ceph_osd_client *osdc,
  3896. struct ceph_osd_request *req)
  3897. {
  3898. down_read(&osdc->lock);
  3899. submit_request(req, false);
  3900. up_read(&osdc->lock);
  3901. }
  3902. EXPORT_SYMBOL(ceph_osdc_start_request);
  3903. /*
  3904. * Unregister request. If @req was registered, it isn't completed:
  3905. * r_result isn't set and __complete_request() isn't invoked.
  3906. *
  3907. * If @req wasn't registered, this call may have raced with
  3908. * handle_reply(), in which case r_result would already be set and
  3909. * __complete_request() would be getting invoked, possibly even
  3910. * concurrently with this call.
  3911. */
  3912. void ceph_osdc_cancel_request(struct ceph_osd_request *req)
  3913. {
  3914. struct ceph_osd_client *osdc = req->r_osdc;
  3915. down_write(&osdc->lock);
  3916. if (req->r_osd)
  3917. cancel_request(req);
  3918. up_write(&osdc->lock);
  3919. }
  3920. EXPORT_SYMBOL(ceph_osdc_cancel_request);
  3921. /*
  3922. * @timeout: in jiffies, 0 means "wait forever"
  3923. */
  3924. static int wait_request_timeout(struct ceph_osd_request *req,
  3925. unsigned long timeout)
  3926. {
  3927. long left;
  3928. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  3929. left = wait_for_completion_killable_timeout(&req->r_completion,
  3930. ceph_timeout_jiffies(timeout));
  3931. if (left <= 0) {
  3932. left = left ?: -ETIMEDOUT;
  3933. ceph_osdc_cancel_request(req);
  3934. } else {
  3935. left = req->r_result; /* completed */
  3936. }
  3937. return left;
  3938. }
  3939. /*
  3940. * wait for a request to complete
  3941. */
  3942. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  3943. struct ceph_osd_request *req)
  3944. {
  3945. return wait_request_timeout(req, 0);
  3946. }
  3947. EXPORT_SYMBOL(ceph_osdc_wait_request);
  3948. /*
  3949. * sync - wait for all in-flight requests to flush. avoid starvation.
  3950. */
  3951. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  3952. {
  3953. struct rb_node *n, *p;
  3954. u64 last_tid = atomic64_read(&osdc->last_tid);
  3955. again:
  3956. down_read(&osdc->lock);
  3957. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  3958. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  3959. mutex_lock(&osd->lock);
  3960. for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
  3961. struct ceph_osd_request *req =
  3962. rb_entry(p, struct ceph_osd_request, r_node);
  3963. if (req->r_tid > last_tid)
  3964. break;
  3965. if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
  3966. continue;
  3967. ceph_osdc_get_request(req);
  3968. mutex_unlock(&osd->lock);
  3969. up_read(&osdc->lock);
  3970. dout("%s waiting on req %p tid %llu last_tid %llu\n",
  3971. __func__, req, req->r_tid, last_tid);
  3972. wait_for_completion(&req->r_completion);
  3973. ceph_osdc_put_request(req);
  3974. goto again;
  3975. }
  3976. mutex_unlock(&osd->lock);
  3977. }
  3978. up_read(&osdc->lock);
  3979. dout("%s done last_tid %llu\n", __func__, last_tid);
  3980. }
  3981. EXPORT_SYMBOL(ceph_osdc_sync);
  3982. /*
  3983. * Returns a handle, caller owns a ref.
  3984. */
  3985. struct ceph_osd_linger_request *
  3986. ceph_osdc_watch(struct ceph_osd_client *osdc,
  3987. struct ceph_object_id *oid,
  3988. struct ceph_object_locator *oloc,
  3989. rados_watchcb2_t wcb,
  3990. rados_watcherrcb_t errcb,
  3991. void *data)
  3992. {
  3993. struct ceph_osd_linger_request *lreq;
  3994. int ret;
  3995. lreq = linger_alloc(osdc);
  3996. if (!lreq)
  3997. return ERR_PTR(-ENOMEM);
  3998. lreq->is_watch = true;
  3999. lreq->wcb = wcb;
  4000. lreq->errcb = errcb;
  4001. lreq->data = data;
  4002. lreq->watch_valid_thru = jiffies;
  4003. ceph_oid_copy(&lreq->t.base_oid, oid);
  4004. ceph_oloc_copy(&lreq->t.base_oloc, oloc);
  4005. lreq->t.flags = CEPH_OSD_FLAG_WRITE;
  4006. ktime_get_real_ts64(&lreq->mtime);
  4007. linger_submit(lreq);
  4008. ret = linger_reg_commit_wait(lreq);
  4009. if (ret) {
  4010. linger_cancel(lreq);
  4011. goto err_put_lreq;
  4012. }
  4013. return lreq;
  4014. err_put_lreq:
  4015. linger_put(lreq);
  4016. return ERR_PTR(ret);
  4017. }
  4018. EXPORT_SYMBOL(ceph_osdc_watch);
  4019. /*
  4020. * Releases a ref.
  4021. *
  4022. * Times out after mount_timeout to preserve rbd unmap behaviour
  4023. * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
  4024. * with mount_timeout").
  4025. */
  4026. int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
  4027. struct ceph_osd_linger_request *lreq)
  4028. {
  4029. struct ceph_options *opts = osdc->client->options;
  4030. struct ceph_osd_request *req;
  4031. int ret;
  4032. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4033. if (!req)
  4034. return -ENOMEM;
  4035. ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
  4036. ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
  4037. req->r_flags = CEPH_OSD_FLAG_WRITE;
  4038. ktime_get_real_ts64(&req->r_mtime);
  4039. osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_UNWATCH,
  4040. lreq->linger_id, 0);
  4041. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4042. if (ret)
  4043. goto out_put_req;
  4044. ceph_osdc_start_request(osdc, req);
  4045. linger_cancel(lreq);
  4046. linger_put(lreq);
  4047. ret = wait_request_timeout(req, opts->mount_timeout);
  4048. out_put_req:
  4049. ceph_osdc_put_request(req);
  4050. return ret;
  4051. }
  4052. EXPORT_SYMBOL(ceph_osdc_unwatch);
  4053. static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
  4054. u64 notify_id, u64 cookie, void *payload,
  4055. u32 payload_len)
  4056. {
  4057. struct ceph_osd_req_op *op;
  4058. struct ceph_pagelist *pl;
  4059. int ret;
  4060. op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
  4061. pl = ceph_pagelist_alloc(GFP_NOIO);
  4062. if (!pl)
  4063. return -ENOMEM;
  4064. ret = ceph_pagelist_encode_64(pl, notify_id);
  4065. ret |= ceph_pagelist_encode_64(pl, cookie);
  4066. if (payload) {
  4067. ret |= ceph_pagelist_encode_32(pl, payload_len);
  4068. ret |= ceph_pagelist_append(pl, payload, payload_len);
  4069. } else {
  4070. ret |= ceph_pagelist_encode_32(pl, 0);
  4071. }
  4072. if (ret) {
  4073. ceph_pagelist_release(pl);
  4074. return -ENOMEM;
  4075. }
  4076. ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
  4077. op->indata_len = pl->length;
  4078. return 0;
  4079. }
  4080. int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
  4081. struct ceph_object_id *oid,
  4082. struct ceph_object_locator *oloc,
  4083. u64 notify_id,
  4084. u64 cookie,
  4085. void *payload,
  4086. u32 payload_len)
  4087. {
  4088. struct ceph_osd_request *req;
  4089. int ret;
  4090. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4091. if (!req)
  4092. return -ENOMEM;
  4093. ceph_oid_copy(&req->r_base_oid, oid);
  4094. ceph_oloc_copy(&req->r_base_oloc, oloc);
  4095. req->r_flags = CEPH_OSD_FLAG_READ;
  4096. ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
  4097. payload_len);
  4098. if (ret)
  4099. goto out_put_req;
  4100. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4101. if (ret)
  4102. goto out_put_req;
  4103. ceph_osdc_start_request(osdc, req);
  4104. ret = ceph_osdc_wait_request(osdc, req);
  4105. out_put_req:
  4106. ceph_osdc_put_request(req);
  4107. return ret;
  4108. }
  4109. EXPORT_SYMBOL(ceph_osdc_notify_ack);
  4110. /*
  4111. * @timeout: in seconds
  4112. *
  4113. * @preply_{pages,len} are initialized both on success and error.
  4114. * The caller is responsible for:
  4115. *
  4116. * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
  4117. */
  4118. int ceph_osdc_notify(struct ceph_osd_client *osdc,
  4119. struct ceph_object_id *oid,
  4120. struct ceph_object_locator *oloc,
  4121. void *payload,
  4122. u32 payload_len,
  4123. u32 timeout,
  4124. struct page ***preply_pages,
  4125. size_t *preply_len)
  4126. {
  4127. struct ceph_osd_linger_request *lreq;
  4128. int ret;
  4129. WARN_ON(!timeout);
  4130. if (preply_pages) {
  4131. *preply_pages = NULL;
  4132. *preply_len = 0;
  4133. }
  4134. lreq = linger_alloc(osdc);
  4135. if (!lreq)
  4136. return -ENOMEM;
  4137. lreq->request_pl = ceph_pagelist_alloc(GFP_NOIO);
  4138. if (!lreq->request_pl) {
  4139. ret = -ENOMEM;
  4140. goto out_put_lreq;
  4141. }
  4142. ret = ceph_pagelist_encode_32(lreq->request_pl, 1); /* prot_ver */
  4143. ret |= ceph_pagelist_encode_32(lreq->request_pl, timeout);
  4144. ret |= ceph_pagelist_encode_32(lreq->request_pl, payload_len);
  4145. ret |= ceph_pagelist_append(lreq->request_pl, payload, payload_len);
  4146. if (ret) {
  4147. ret = -ENOMEM;
  4148. goto out_put_lreq;
  4149. }
  4150. /* for notify_id */
  4151. lreq->notify_id_pages = ceph_alloc_page_vector(1, GFP_NOIO);
  4152. if (IS_ERR(lreq->notify_id_pages)) {
  4153. ret = PTR_ERR(lreq->notify_id_pages);
  4154. lreq->notify_id_pages = NULL;
  4155. goto out_put_lreq;
  4156. }
  4157. lreq->preply_pages = preply_pages;
  4158. lreq->preply_len = preply_len;
  4159. ceph_oid_copy(&lreq->t.base_oid, oid);
  4160. ceph_oloc_copy(&lreq->t.base_oloc, oloc);
  4161. lreq->t.flags = CEPH_OSD_FLAG_READ;
  4162. linger_submit(lreq);
  4163. ret = linger_reg_commit_wait(lreq);
  4164. if (!ret)
  4165. ret = linger_notify_finish_wait(lreq,
  4166. msecs_to_jiffies(2 * timeout * MSEC_PER_SEC));
  4167. else
  4168. dout("lreq %p failed to initiate notify %d\n", lreq, ret);
  4169. linger_cancel(lreq);
  4170. out_put_lreq:
  4171. linger_put(lreq);
  4172. return ret;
  4173. }
  4174. EXPORT_SYMBOL(ceph_osdc_notify);
  4175. /*
  4176. * Return the number of milliseconds since the watch was last
  4177. * confirmed, or an error. If there is an error, the watch is no
  4178. * longer valid, and should be destroyed with ceph_osdc_unwatch().
  4179. */
  4180. int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
  4181. struct ceph_osd_linger_request *lreq)
  4182. {
  4183. unsigned long stamp, age;
  4184. int ret;
  4185. down_read(&osdc->lock);
  4186. mutex_lock(&lreq->lock);
  4187. stamp = lreq->watch_valid_thru;
  4188. if (!list_empty(&lreq->pending_lworks)) {
  4189. struct linger_work *lwork =
  4190. list_first_entry(&lreq->pending_lworks,
  4191. struct linger_work,
  4192. pending_item);
  4193. if (time_before(lwork->queued_stamp, stamp))
  4194. stamp = lwork->queued_stamp;
  4195. }
  4196. age = jiffies - stamp;
  4197. dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
  4198. lreq, lreq->linger_id, age, lreq->last_error);
  4199. /* we are truncating to msecs, so return a safe upper bound */
  4200. ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
  4201. mutex_unlock(&lreq->lock);
  4202. up_read(&osdc->lock);
  4203. return ret;
  4204. }
  4205. static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
  4206. {
  4207. u8 struct_v;
  4208. u32 struct_len;
  4209. int ret;
  4210. ret = ceph_start_decoding(p, end, 2, "watch_item_t",
  4211. &struct_v, &struct_len);
  4212. if (ret)
  4213. goto bad;
  4214. ret = -EINVAL;
  4215. ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
  4216. ceph_decode_64_safe(p, end, item->cookie, bad);
  4217. ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
  4218. if (struct_v >= 2) {
  4219. ret = ceph_decode_entity_addr(p, end, &item->addr);
  4220. if (ret)
  4221. goto bad;
  4222. } else {
  4223. ret = 0;
  4224. }
  4225. dout("%s %s%llu cookie %llu addr %s\n", __func__,
  4226. ENTITY_NAME(item->name), item->cookie,
  4227. ceph_pr_addr(&item->addr));
  4228. bad:
  4229. return ret;
  4230. }
  4231. static int decode_watchers(void **p, void *end,
  4232. struct ceph_watch_item **watchers,
  4233. u32 *num_watchers)
  4234. {
  4235. u8 struct_v;
  4236. u32 struct_len;
  4237. int i;
  4238. int ret;
  4239. ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
  4240. &struct_v, &struct_len);
  4241. if (ret)
  4242. return ret;
  4243. *num_watchers = ceph_decode_32(p);
  4244. *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
  4245. if (!*watchers)
  4246. return -ENOMEM;
  4247. for (i = 0; i < *num_watchers; i++) {
  4248. ret = decode_watcher(p, end, *watchers + i);
  4249. if (ret) {
  4250. kfree(*watchers);
  4251. return ret;
  4252. }
  4253. }
  4254. return 0;
  4255. }
  4256. /*
  4257. * On success, the caller is responsible for:
  4258. *
  4259. * kfree(watchers);
  4260. */
  4261. int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
  4262. struct ceph_object_id *oid,
  4263. struct ceph_object_locator *oloc,
  4264. struct ceph_watch_item **watchers,
  4265. u32 *num_watchers)
  4266. {
  4267. struct ceph_osd_request *req;
  4268. struct page **pages;
  4269. int ret;
  4270. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4271. if (!req)
  4272. return -ENOMEM;
  4273. ceph_oid_copy(&req->r_base_oid, oid);
  4274. ceph_oloc_copy(&req->r_base_oloc, oloc);
  4275. req->r_flags = CEPH_OSD_FLAG_READ;
  4276. pages = ceph_alloc_page_vector(1, GFP_NOIO);
  4277. if (IS_ERR(pages)) {
  4278. ret = PTR_ERR(pages);
  4279. goto out_put_req;
  4280. }
  4281. osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
  4282. ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
  4283. response_data),
  4284. pages, PAGE_SIZE, 0, false, true);
  4285. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4286. if (ret)
  4287. goto out_put_req;
  4288. ceph_osdc_start_request(osdc, req);
  4289. ret = ceph_osdc_wait_request(osdc, req);
  4290. if (ret >= 0) {
  4291. void *p = page_address(pages[0]);
  4292. void *const end = p + req->r_ops[0].outdata_len;
  4293. ret = decode_watchers(&p, end, watchers, num_watchers);
  4294. }
  4295. out_put_req:
  4296. ceph_osdc_put_request(req);
  4297. return ret;
  4298. }
  4299. EXPORT_SYMBOL(ceph_osdc_list_watchers);
  4300. /*
  4301. * Call all pending notify callbacks - for use after a watch is
  4302. * unregistered, to make sure no more callbacks for it will be invoked
  4303. */
  4304. void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
  4305. {
  4306. dout("%s osdc %p\n", __func__, osdc);
  4307. flush_workqueue(osdc->notify_wq);
  4308. }
  4309. EXPORT_SYMBOL(ceph_osdc_flush_notifies);
  4310. void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
  4311. {
  4312. down_read(&osdc->lock);
  4313. maybe_request_map(osdc);
  4314. up_read(&osdc->lock);
  4315. }
  4316. EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
  4317. /*
  4318. * Execute an OSD class method on an object.
  4319. *
  4320. * @flags: CEPH_OSD_FLAG_*
  4321. * @resp_len: in/out param for reply length
  4322. */
  4323. int ceph_osdc_call(struct ceph_osd_client *osdc,
  4324. struct ceph_object_id *oid,
  4325. struct ceph_object_locator *oloc,
  4326. const char *class, const char *method,
  4327. unsigned int flags,
  4328. struct page *req_page, size_t req_len,
  4329. struct page **resp_pages, size_t *resp_len)
  4330. {
  4331. struct ceph_osd_request *req;
  4332. int ret;
  4333. if (req_len > PAGE_SIZE)
  4334. return -E2BIG;
  4335. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4336. if (!req)
  4337. return -ENOMEM;
  4338. ceph_oid_copy(&req->r_base_oid, oid);
  4339. ceph_oloc_copy(&req->r_base_oloc, oloc);
  4340. req->r_flags = flags;
  4341. ret = osd_req_op_cls_init(req, 0, class, method);
  4342. if (ret)
  4343. goto out_put_req;
  4344. if (req_page)
  4345. osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
  4346. 0, false, false);
  4347. if (resp_pages)
  4348. osd_req_op_cls_response_data_pages(req, 0, resp_pages,
  4349. *resp_len, 0, false, false);
  4350. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4351. if (ret)
  4352. goto out_put_req;
  4353. ceph_osdc_start_request(osdc, req);
  4354. ret = ceph_osdc_wait_request(osdc, req);
  4355. if (ret >= 0) {
  4356. ret = req->r_ops[0].rval;
  4357. if (resp_pages)
  4358. *resp_len = req->r_ops[0].outdata_len;
  4359. }
  4360. out_put_req:
  4361. ceph_osdc_put_request(req);
  4362. return ret;
  4363. }
  4364. EXPORT_SYMBOL(ceph_osdc_call);
  4365. /*
  4366. * reset all osd connections
  4367. */
  4368. void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
  4369. {
  4370. struct rb_node *n;
  4371. down_write(&osdc->lock);
  4372. for (n = rb_first(&osdc->osds); n; ) {
  4373. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  4374. n = rb_next(n);
  4375. if (!reopen_osd(osd))
  4376. kick_osd_requests(osd);
  4377. }
  4378. up_write(&osdc->lock);
  4379. }
  4380. /*
  4381. * init, shutdown
  4382. */
  4383. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  4384. {
  4385. int err;
  4386. dout("init\n");
  4387. osdc->client = client;
  4388. init_rwsem(&osdc->lock);
  4389. osdc->osds = RB_ROOT;
  4390. INIT_LIST_HEAD(&osdc->osd_lru);
  4391. spin_lock_init(&osdc->osd_lru_lock);
  4392. osd_init(&osdc->homeless_osd);
  4393. osdc->homeless_osd.o_osdc = osdc;
  4394. osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
  4395. osdc->last_linger_id = CEPH_LINGER_ID_START;
  4396. osdc->linger_requests = RB_ROOT;
  4397. osdc->map_checks = RB_ROOT;
  4398. osdc->linger_map_checks = RB_ROOT;
  4399. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  4400. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  4401. err = -ENOMEM;
  4402. osdc->osdmap = ceph_osdmap_alloc();
  4403. if (!osdc->osdmap)
  4404. goto out;
  4405. osdc->req_mempool = mempool_create_slab_pool(10,
  4406. ceph_osd_request_cache);
  4407. if (!osdc->req_mempool)
  4408. goto out_map;
  4409. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  4410. PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
  4411. if (err < 0)
  4412. goto out_mempool;
  4413. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  4414. PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
  4415. "osd_op_reply");
  4416. if (err < 0)
  4417. goto out_msgpool;
  4418. err = -ENOMEM;
  4419. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  4420. if (!osdc->notify_wq)
  4421. goto out_msgpool_reply;
  4422. osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
  4423. if (!osdc->completion_wq)
  4424. goto out_notify_wq;
  4425. schedule_delayed_work(&osdc->timeout_work,
  4426. osdc->client->options->osd_keepalive_timeout);
  4427. schedule_delayed_work(&osdc->osds_timeout_work,
  4428. round_jiffies_relative(osdc->client->options->osd_idle_ttl));
  4429. return 0;
  4430. out_notify_wq:
  4431. destroy_workqueue(osdc->notify_wq);
  4432. out_msgpool_reply:
  4433. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  4434. out_msgpool:
  4435. ceph_msgpool_destroy(&osdc->msgpool_op);
  4436. out_mempool:
  4437. mempool_destroy(osdc->req_mempool);
  4438. out_map:
  4439. ceph_osdmap_destroy(osdc->osdmap);
  4440. out:
  4441. return err;
  4442. }
  4443. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  4444. {
  4445. destroy_workqueue(osdc->completion_wq);
  4446. destroy_workqueue(osdc->notify_wq);
  4447. cancel_delayed_work_sync(&osdc->timeout_work);
  4448. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  4449. down_write(&osdc->lock);
  4450. while (!RB_EMPTY_ROOT(&osdc->osds)) {
  4451. struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
  4452. struct ceph_osd, o_node);
  4453. close_osd(osd);
  4454. }
  4455. up_write(&osdc->lock);
  4456. WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
  4457. osd_cleanup(&osdc->homeless_osd);
  4458. WARN_ON(!list_empty(&osdc->osd_lru));
  4459. WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
  4460. WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
  4461. WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
  4462. WARN_ON(atomic_read(&osdc->num_requests));
  4463. WARN_ON(atomic_read(&osdc->num_homeless));
  4464. ceph_osdmap_destroy(osdc->osdmap);
  4465. mempool_destroy(osdc->req_mempool);
  4466. ceph_msgpool_destroy(&osdc->msgpool_op);
  4467. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  4468. }
  4469. int osd_req_op_copy_from_init(struct ceph_osd_request *req,
  4470. u64 src_snapid, u64 src_version,
  4471. struct ceph_object_id *src_oid,
  4472. struct ceph_object_locator *src_oloc,
  4473. u32 src_fadvise_flags,
  4474. u32 dst_fadvise_flags,
  4475. u32 truncate_seq, u64 truncate_size,
  4476. u8 copy_from_flags)
  4477. {
  4478. struct ceph_osd_req_op *op;
  4479. struct page **pages;
  4480. void *p, *end;
  4481. pages = ceph_alloc_page_vector(1, GFP_KERNEL);
  4482. if (IS_ERR(pages))
  4483. return PTR_ERR(pages);
  4484. op = osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
  4485. dst_fadvise_flags);
  4486. op->copy_from.snapid = src_snapid;
  4487. op->copy_from.src_version = src_version;
  4488. op->copy_from.flags = copy_from_flags;
  4489. op->copy_from.src_fadvise_flags = src_fadvise_flags;
  4490. p = page_address(pages[0]);
  4491. end = p + PAGE_SIZE;
  4492. ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
  4493. encode_oloc(&p, end, src_oloc);
  4494. ceph_encode_32(&p, truncate_seq);
  4495. ceph_encode_64(&p, truncate_size);
  4496. op->indata_len = PAGE_SIZE - (end - p);
  4497. ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
  4498. op->indata_len, 0, false, true);
  4499. return 0;
  4500. }
  4501. EXPORT_SYMBOL(osd_req_op_copy_from_init);
  4502. int __init ceph_osdc_setup(void)
  4503. {
  4504. size_t size = sizeof(struct ceph_osd_request) +
  4505. CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
  4506. BUG_ON(ceph_osd_request_cache);
  4507. ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
  4508. 0, 0, NULL);
  4509. return ceph_osd_request_cache ? 0 : -ENOMEM;
  4510. }
  4511. void ceph_osdc_cleanup(void)
  4512. {
  4513. BUG_ON(!ceph_osd_request_cache);
  4514. kmem_cache_destroy(ceph_osd_request_cache);
  4515. ceph_osd_request_cache = NULL;
  4516. }
  4517. /*
  4518. * handle incoming message
  4519. */
  4520. static void osd_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  4521. {
  4522. struct ceph_osd *osd = con->private;
  4523. struct ceph_osd_client *osdc = osd->o_osdc;
  4524. int type = le16_to_cpu(msg->hdr.type);
  4525. switch (type) {
  4526. case CEPH_MSG_OSD_MAP:
  4527. ceph_osdc_handle_map(osdc, msg);
  4528. break;
  4529. case CEPH_MSG_OSD_OPREPLY:
  4530. handle_reply(osd, msg);
  4531. break;
  4532. case CEPH_MSG_OSD_BACKOFF:
  4533. handle_backoff(osd, msg);
  4534. break;
  4535. case CEPH_MSG_WATCH_NOTIFY:
  4536. handle_watch_notify(osdc, msg);
  4537. break;
  4538. default:
  4539. pr_err("received unknown message type %d %s\n", type,
  4540. ceph_msg_type_name(type));
  4541. }
  4542. ceph_msg_put(msg);
  4543. }
  4544. /*
  4545. * Lookup and return message for incoming reply. Don't try to do
  4546. * anything about a larger than preallocated data portion of the
  4547. * message at the moment - for now, just skip the message.
  4548. */
  4549. static struct ceph_msg *get_reply(struct ceph_connection *con,
  4550. struct ceph_msg_header *hdr,
  4551. int *skip)
  4552. {
  4553. struct ceph_osd *osd = con->private;
  4554. struct ceph_osd_client *osdc = osd->o_osdc;
  4555. struct ceph_msg *m = NULL;
  4556. struct ceph_osd_request *req;
  4557. int front_len = le32_to_cpu(hdr->front_len);
  4558. int data_len = le32_to_cpu(hdr->data_len);
  4559. u64 tid = le64_to_cpu(hdr->tid);
  4560. down_read(&osdc->lock);
  4561. if (!osd_registered(osd)) {
  4562. dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
  4563. *skip = 1;
  4564. goto out_unlock_osdc;
  4565. }
  4566. WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
  4567. mutex_lock(&osd->lock);
  4568. req = lookup_request(&osd->o_requests, tid);
  4569. if (!req) {
  4570. dout("%s osd%d tid %llu unknown, skipping\n", __func__,
  4571. osd->o_osd, tid);
  4572. *skip = 1;
  4573. goto out_unlock_session;
  4574. }
  4575. ceph_msg_revoke_incoming(req->r_reply);
  4576. if (front_len > req->r_reply->front_alloc_len) {
  4577. pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
  4578. __func__, osd->o_osd, req->r_tid, front_len,
  4579. req->r_reply->front_alloc_len);
  4580. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
  4581. false);
  4582. if (!m)
  4583. goto out_unlock_session;
  4584. ceph_msg_put(req->r_reply);
  4585. req->r_reply = m;
  4586. }
  4587. if (data_len > req->r_reply->data_length) {
  4588. pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
  4589. __func__, osd->o_osd, req->r_tid, data_len,
  4590. req->r_reply->data_length);
  4591. m = NULL;
  4592. *skip = 1;
  4593. goto out_unlock_session;
  4594. }
  4595. m = ceph_msg_get(req->r_reply);
  4596. dout("get_reply tid %lld %p\n", tid, m);
  4597. out_unlock_session:
  4598. mutex_unlock(&osd->lock);
  4599. out_unlock_osdc:
  4600. up_read(&osdc->lock);
  4601. return m;
  4602. }
  4603. static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
  4604. {
  4605. struct ceph_msg *m;
  4606. int type = le16_to_cpu(hdr->type);
  4607. u32 front_len = le32_to_cpu(hdr->front_len);
  4608. u32 data_len = le32_to_cpu(hdr->data_len);
  4609. m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
  4610. if (!m)
  4611. return NULL;
  4612. if (data_len) {
  4613. struct page **pages;
  4614. pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
  4615. GFP_NOIO);
  4616. if (IS_ERR(pages)) {
  4617. ceph_msg_put(m);
  4618. return NULL;
  4619. }
  4620. ceph_msg_data_add_pages(m, pages, data_len, 0, true);
  4621. }
  4622. return m;
  4623. }
  4624. static struct ceph_msg *osd_alloc_msg(struct ceph_connection *con,
  4625. struct ceph_msg_header *hdr,
  4626. int *skip)
  4627. {
  4628. struct ceph_osd *osd = con->private;
  4629. int type = le16_to_cpu(hdr->type);
  4630. *skip = 0;
  4631. switch (type) {
  4632. case CEPH_MSG_OSD_MAP:
  4633. case CEPH_MSG_OSD_BACKOFF:
  4634. case CEPH_MSG_WATCH_NOTIFY:
  4635. return alloc_msg_with_page_vector(hdr);
  4636. case CEPH_MSG_OSD_OPREPLY:
  4637. return get_reply(con, hdr, skip);
  4638. default:
  4639. pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
  4640. osd->o_osd, type);
  4641. *skip = 1;
  4642. return NULL;
  4643. }
  4644. }
  4645. /*
  4646. * Wrappers to refcount containing ceph_osd struct
  4647. */
  4648. static struct ceph_connection *osd_get_con(struct ceph_connection *con)
  4649. {
  4650. struct ceph_osd *osd = con->private;
  4651. if (get_osd(osd))
  4652. return con;
  4653. return NULL;
  4654. }
  4655. static void osd_put_con(struct ceph_connection *con)
  4656. {
  4657. struct ceph_osd *osd = con->private;
  4658. put_osd(osd);
  4659. }
  4660. /*
  4661. * authentication
  4662. */
  4663. /*
  4664. * Note: returned pointer is the address of a structure that's
  4665. * managed separately. Caller must *not* attempt to free it.
  4666. */
  4667. static struct ceph_auth_handshake *
  4668. osd_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
  4669. {
  4670. struct ceph_osd *o = con->private;
  4671. struct ceph_osd_client *osdc = o->o_osdc;
  4672. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4673. struct ceph_auth_handshake *auth = &o->o_auth;
  4674. int ret;
  4675. ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
  4676. force_new, proto, NULL, NULL);
  4677. if (ret)
  4678. return ERR_PTR(ret);
  4679. return auth;
  4680. }
  4681. static int osd_add_authorizer_challenge(struct ceph_connection *con,
  4682. void *challenge_buf, int challenge_buf_len)
  4683. {
  4684. struct ceph_osd *o = con->private;
  4685. struct ceph_osd_client *osdc = o->o_osdc;
  4686. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4687. return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
  4688. challenge_buf, challenge_buf_len);
  4689. }
  4690. static int osd_verify_authorizer_reply(struct ceph_connection *con)
  4691. {
  4692. struct ceph_osd *o = con->private;
  4693. struct ceph_osd_client *osdc = o->o_osdc;
  4694. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4695. struct ceph_auth_handshake *auth = &o->o_auth;
  4696. return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
  4697. auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
  4698. NULL, NULL, NULL, NULL);
  4699. }
  4700. static int osd_invalidate_authorizer(struct ceph_connection *con)
  4701. {
  4702. struct ceph_osd *o = con->private;
  4703. struct ceph_osd_client *osdc = o->o_osdc;
  4704. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4705. ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  4706. return ceph_monc_validate_auth(&osdc->client->monc);
  4707. }
  4708. static int osd_get_auth_request(struct ceph_connection *con,
  4709. void *buf, int *buf_len,
  4710. void **authorizer, int *authorizer_len)
  4711. {
  4712. struct ceph_osd *o = con->private;
  4713. struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
  4714. struct ceph_auth_handshake *auth = &o->o_auth;
  4715. int ret;
  4716. ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
  4717. buf, buf_len);
  4718. if (ret)
  4719. return ret;
  4720. *authorizer = auth->authorizer_buf;
  4721. *authorizer_len = auth->authorizer_buf_len;
  4722. return 0;
  4723. }
  4724. static int osd_handle_auth_reply_more(struct ceph_connection *con,
  4725. void *reply, int reply_len,
  4726. void *buf, int *buf_len,
  4727. void **authorizer, int *authorizer_len)
  4728. {
  4729. struct ceph_osd *o = con->private;
  4730. struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
  4731. struct ceph_auth_handshake *auth = &o->o_auth;
  4732. int ret;
  4733. ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
  4734. buf, buf_len);
  4735. if (ret)
  4736. return ret;
  4737. *authorizer = auth->authorizer_buf;
  4738. *authorizer_len = auth->authorizer_buf_len;
  4739. return 0;
  4740. }
  4741. static int osd_handle_auth_done(struct ceph_connection *con,
  4742. u64 global_id, void *reply, int reply_len,
  4743. u8 *session_key, int *session_key_len,
  4744. u8 *con_secret, int *con_secret_len)
  4745. {
  4746. struct ceph_osd *o = con->private;
  4747. struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
  4748. struct ceph_auth_handshake *auth = &o->o_auth;
  4749. return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
  4750. session_key, session_key_len,
  4751. con_secret, con_secret_len);
  4752. }
  4753. static int osd_handle_auth_bad_method(struct ceph_connection *con,
  4754. int used_proto, int result,
  4755. const int *allowed_protos, int proto_cnt,
  4756. const int *allowed_modes, int mode_cnt)
  4757. {
  4758. struct ceph_osd *o = con->private;
  4759. struct ceph_mon_client *monc = &o->o_osdc->client->monc;
  4760. int ret;
  4761. if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_OSD,
  4762. used_proto, result,
  4763. allowed_protos, proto_cnt,
  4764. allowed_modes, mode_cnt)) {
  4765. ret = ceph_monc_validate_auth(monc);
  4766. if (ret)
  4767. return ret;
  4768. }
  4769. return -EACCES;
  4770. }
  4771. static void osd_reencode_message(struct ceph_msg *msg)
  4772. {
  4773. int type = le16_to_cpu(msg->hdr.type);
  4774. if (type == CEPH_MSG_OSD_OP)
  4775. encode_request_finish(msg);
  4776. }
  4777. static int osd_sign_message(struct ceph_msg *msg)
  4778. {
  4779. struct ceph_osd *o = msg->con->private;
  4780. struct ceph_auth_handshake *auth = &o->o_auth;
  4781. return ceph_auth_sign_message(auth, msg);
  4782. }
  4783. static int osd_check_message_signature(struct ceph_msg *msg)
  4784. {
  4785. struct ceph_osd *o = msg->con->private;
  4786. struct ceph_auth_handshake *auth = &o->o_auth;
  4787. return ceph_auth_check_message_signature(auth, msg);
  4788. }
  4789. static const struct ceph_connection_operations osd_con_ops = {
  4790. .get = osd_get_con,
  4791. .put = osd_put_con,
  4792. .alloc_msg = osd_alloc_msg,
  4793. .dispatch = osd_dispatch,
  4794. .fault = osd_fault,
  4795. .reencode_message = osd_reencode_message,
  4796. .get_authorizer = osd_get_authorizer,
  4797. .add_authorizer_challenge = osd_add_authorizer_challenge,
  4798. .verify_authorizer_reply = osd_verify_authorizer_reply,
  4799. .invalidate_authorizer = osd_invalidate_authorizer,
  4800. .sign_message = osd_sign_message,
  4801. .check_message_signature = osd_check_message_signature,
  4802. .get_auth_request = osd_get_auth_request,
  4803. .handle_auth_reply_more = osd_handle_auth_reply_more,
  4804. .handle_auth_done = osd_handle_auth_done,
  4805. .handle_auth_bad_method = osd_handle_auth_bad_method,
  4806. };