osd_client.c 141 KB

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