osd_client.c 137 KB

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