osd_client.c 141 KB

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