osd_client.c 117 KB

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