osd_client.c 137 KB

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