mds_client.c 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/fs.h>
  4. #include <linux/wait.h>
  5. #include <linux/slab.h>
  6. #include <linux/gfp.h>
  7. #include <linux/sched.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/ratelimit.h>
  11. #include "super.h"
  12. #include "mds_client.h"
  13. #include <linux/ceph/ceph_features.h>
  14. #include <linux/ceph/messenger.h>
  15. #include <linux/ceph/decode.h>
  16. #include <linux/ceph/pagelist.h>
  17. #include <linux/ceph/auth.h>
  18. #include <linux/ceph/debugfs.h>
  19. /*
  20. * A cluster of MDS (metadata server) daemons is responsible for
  21. * managing the file system namespace (the directory hierarchy and
  22. * inodes) and for coordinating shared access to storage. Metadata is
  23. * partitioning hierarchically across a number of servers, and that
  24. * partition varies over time as the cluster adjusts the distribution
  25. * in order to balance load.
  26. *
  27. * The MDS client is primarily responsible to managing synchronous
  28. * metadata requests for operations like open, unlink, and so forth.
  29. * If there is a MDS failure, we find out about it when we (possibly
  30. * request and) receive a new MDS map, and can resubmit affected
  31. * requests.
  32. *
  33. * For the most part, though, we take advantage of a lossless
  34. * communications channel to the MDS, and do not need to worry about
  35. * timing out or resubmitting requests.
  36. *
  37. * We maintain a stateful "session" with each MDS we interact with.
  38. * Within each session, we sent periodic heartbeat messages to ensure
  39. * any capabilities or leases we have been issues remain valid. If
  40. * the session times out and goes stale, our leases and capabilities
  41. * are no longer valid.
  42. */
  43. struct ceph_reconnect_state {
  44. int nr_caps;
  45. struct ceph_pagelist *pagelist;
  46. unsigned msg_version;
  47. };
  48. static void __wake_requests(struct ceph_mds_client *mdsc,
  49. struct list_head *head);
  50. static const struct ceph_connection_operations mds_con_ops;
  51. /*
  52. * mds reply parsing
  53. */
  54. /*
  55. * parse individual inode info
  56. */
  57. static int parse_reply_info_in(void **p, void *end,
  58. struct ceph_mds_reply_info_in *info,
  59. u64 features)
  60. {
  61. int err = -EIO;
  62. info->in = *p;
  63. *p += sizeof(struct ceph_mds_reply_inode) +
  64. sizeof(*info->in->fragtree.splits) *
  65. le32_to_cpu(info->in->fragtree.nsplits);
  66. ceph_decode_32_safe(p, end, info->symlink_len, bad);
  67. ceph_decode_need(p, end, info->symlink_len, bad);
  68. info->symlink = *p;
  69. *p += info->symlink_len;
  70. if (features & CEPH_FEATURE_DIRLAYOUTHASH)
  71. ceph_decode_copy_safe(p, end, &info->dir_layout,
  72. sizeof(info->dir_layout), bad);
  73. else
  74. memset(&info->dir_layout, 0, sizeof(info->dir_layout));
  75. ceph_decode_32_safe(p, end, info->xattr_len, bad);
  76. ceph_decode_need(p, end, info->xattr_len, bad);
  77. info->xattr_data = *p;
  78. *p += info->xattr_len;
  79. if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
  80. ceph_decode_64_safe(p, end, info->inline_version, bad);
  81. ceph_decode_32_safe(p, end, info->inline_len, bad);
  82. ceph_decode_need(p, end, info->inline_len, bad);
  83. info->inline_data = *p;
  84. *p += info->inline_len;
  85. } else
  86. info->inline_version = CEPH_INLINE_NONE;
  87. if (features & CEPH_FEATURE_MDS_QUOTA) {
  88. u8 struct_v, struct_compat;
  89. u32 struct_len;
  90. /*
  91. * both struct_v and struct_compat are expected to be >= 1
  92. */
  93. ceph_decode_8_safe(p, end, struct_v, bad);
  94. ceph_decode_8_safe(p, end, struct_compat, bad);
  95. if (!struct_v || !struct_compat)
  96. goto bad;
  97. ceph_decode_32_safe(p, end, struct_len, bad);
  98. ceph_decode_need(p, end, struct_len, bad);
  99. ceph_decode_64_safe(p, end, info->max_bytes, bad);
  100. ceph_decode_64_safe(p, end, info->max_files, bad);
  101. } else {
  102. info->max_bytes = 0;
  103. info->max_files = 0;
  104. }
  105. info->pool_ns_len = 0;
  106. info->pool_ns_data = NULL;
  107. if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
  108. ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
  109. if (info->pool_ns_len > 0) {
  110. ceph_decode_need(p, end, info->pool_ns_len, bad);
  111. info->pool_ns_data = *p;
  112. *p += info->pool_ns_len;
  113. }
  114. }
  115. return 0;
  116. bad:
  117. return err;
  118. }
  119. /*
  120. * parse a normal reply, which may contain a (dir+)dentry and/or a
  121. * target inode.
  122. */
  123. static int parse_reply_info_trace(void **p, void *end,
  124. struct ceph_mds_reply_info_parsed *info,
  125. u64 features)
  126. {
  127. int err;
  128. if (info->head->is_dentry) {
  129. err = parse_reply_info_in(p, end, &info->diri, features);
  130. if (err < 0)
  131. goto out_bad;
  132. if (unlikely(*p + sizeof(*info->dirfrag) > end))
  133. goto bad;
  134. info->dirfrag = *p;
  135. *p += sizeof(*info->dirfrag) +
  136. sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
  137. if (unlikely(*p > end))
  138. goto bad;
  139. ceph_decode_32_safe(p, end, info->dname_len, bad);
  140. ceph_decode_need(p, end, info->dname_len, bad);
  141. info->dname = *p;
  142. *p += info->dname_len;
  143. info->dlease = *p;
  144. *p += sizeof(*info->dlease);
  145. }
  146. if (info->head->is_target) {
  147. err = parse_reply_info_in(p, end, &info->targeti, features);
  148. if (err < 0)
  149. goto out_bad;
  150. }
  151. if (unlikely(*p != end))
  152. goto bad;
  153. return 0;
  154. bad:
  155. err = -EIO;
  156. out_bad:
  157. pr_err("problem parsing mds trace %d\n", err);
  158. return err;
  159. }
  160. /*
  161. * parse readdir results
  162. */
  163. static int parse_reply_info_dir(void **p, void *end,
  164. struct ceph_mds_reply_info_parsed *info,
  165. u64 features)
  166. {
  167. u32 num, i = 0;
  168. int err;
  169. info->dir_dir = *p;
  170. if (*p + sizeof(*info->dir_dir) > end)
  171. goto bad;
  172. *p += sizeof(*info->dir_dir) +
  173. sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
  174. if (*p > end)
  175. goto bad;
  176. ceph_decode_need(p, end, sizeof(num) + 2, bad);
  177. num = ceph_decode_32(p);
  178. {
  179. u16 flags = ceph_decode_16(p);
  180. info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
  181. info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
  182. info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
  183. info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
  184. }
  185. if (num == 0)
  186. goto done;
  187. BUG_ON(!info->dir_entries);
  188. if ((unsigned long)(info->dir_entries + num) >
  189. (unsigned long)info->dir_entries + info->dir_buf_size) {
  190. pr_err("dir contents are larger than expected\n");
  191. WARN_ON(1);
  192. goto bad;
  193. }
  194. info->dir_nr = num;
  195. while (num) {
  196. struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
  197. /* dentry */
  198. ceph_decode_need(p, end, sizeof(u32)*2, bad);
  199. rde->name_len = ceph_decode_32(p);
  200. ceph_decode_need(p, end, rde->name_len, bad);
  201. rde->name = *p;
  202. *p += rde->name_len;
  203. dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
  204. rde->lease = *p;
  205. *p += sizeof(struct ceph_mds_reply_lease);
  206. /* inode */
  207. err = parse_reply_info_in(p, end, &rde->inode, features);
  208. if (err < 0)
  209. goto out_bad;
  210. /* ceph_readdir_prepopulate() will update it */
  211. rde->offset = 0;
  212. i++;
  213. num--;
  214. }
  215. done:
  216. if (*p != end)
  217. goto bad;
  218. return 0;
  219. bad:
  220. err = -EIO;
  221. out_bad:
  222. pr_err("problem parsing dir contents %d\n", err);
  223. return err;
  224. }
  225. /*
  226. * parse fcntl F_GETLK results
  227. */
  228. static int parse_reply_info_filelock(void **p, void *end,
  229. struct ceph_mds_reply_info_parsed *info,
  230. u64 features)
  231. {
  232. if (*p + sizeof(*info->filelock_reply) > end)
  233. goto bad;
  234. info->filelock_reply = *p;
  235. *p += sizeof(*info->filelock_reply);
  236. if (unlikely(*p != end))
  237. goto bad;
  238. return 0;
  239. bad:
  240. return -EIO;
  241. }
  242. /*
  243. * parse create results
  244. */
  245. static int parse_reply_info_create(void **p, void *end,
  246. struct ceph_mds_reply_info_parsed *info,
  247. u64 features)
  248. {
  249. if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
  250. if (*p == end) {
  251. info->has_create_ino = false;
  252. } else {
  253. info->has_create_ino = true;
  254. info->ino = ceph_decode_64(p);
  255. }
  256. }
  257. if (unlikely(*p != end))
  258. goto bad;
  259. return 0;
  260. bad:
  261. return -EIO;
  262. }
  263. /*
  264. * parse extra results
  265. */
  266. static int parse_reply_info_extra(void **p, void *end,
  267. struct ceph_mds_reply_info_parsed *info,
  268. u64 features)
  269. {
  270. u32 op = le32_to_cpu(info->head->op);
  271. if (op == CEPH_MDS_OP_GETFILELOCK)
  272. return parse_reply_info_filelock(p, end, info, features);
  273. else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
  274. return parse_reply_info_dir(p, end, info, features);
  275. else if (op == CEPH_MDS_OP_CREATE)
  276. return parse_reply_info_create(p, end, info, features);
  277. else
  278. return -EIO;
  279. }
  280. /*
  281. * parse entire mds reply
  282. */
  283. static int parse_reply_info(struct ceph_msg *msg,
  284. struct ceph_mds_reply_info_parsed *info,
  285. u64 features)
  286. {
  287. void *p, *end;
  288. u32 len;
  289. int err;
  290. info->head = msg->front.iov_base;
  291. p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
  292. end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
  293. /* trace */
  294. ceph_decode_32_safe(&p, end, len, bad);
  295. if (len > 0) {
  296. ceph_decode_need(&p, end, len, bad);
  297. err = parse_reply_info_trace(&p, p+len, info, features);
  298. if (err < 0)
  299. goto out_bad;
  300. }
  301. /* extra */
  302. ceph_decode_32_safe(&p, end, len, bad);
  303. if (len > 0) {
  304. ceph_decode_need(&p, end, len, bad);
  305. err = parse_reply_info_extra(&p, p+len, info, features);
  306. if (err < 0)
  307. goto out_bad;
  308. }
  309. /* snap blob */
  310. ceph_decode_32_safe(&p, end, len, bad);
  311. info->snapblob_len = len;
  312. info->snapblob = p;
  313. p += len;
  314. if (p != end)
  315. goto bad;
  316. return 0;
  317. bad:
  318. err = -EIO;
  319. out_bad:
  320. pr_err("mds parse_reply err %d\n", err);
  321. return err;
  322. }
  323. static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
  324. {
  325. if (!info->dir_entries)
  326. return;
  327. free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
  328. }
  329. /*
  330. * sessions
  331. */
  332. const char *ceph_session_state_name(int s)
  333. {
  334. switch (s) {
  335. case CEPH_MDS_SESSION_NEW: return "new";
  336. case CEPH_MDS_SESSION_OPENING: return "opening";
  337. case CEPH_MDS_SESSION_OPEN: return "open";
  338. case CEPH_MDS_SESSION_HUNG: return "hung";
  339. case CEPH_MDS_SESSION_CLOSING: return "closing";
  340. case CEPH_MDS_SESSION_RESTARTING: return "restarting";
  341. case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
  342. case CEPH_MDS_SESSION_REJECTED: return "rejected";
  343. default: return "???";
  344. }
  345. }
  346. static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
  347. {
  348. if (refcount_inc_not_zero(&s->s_ref)) {
  349. dout("mdsc get_session %p %d -> %d\n", s,
  350. refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
  351. return s;
  352. } else {
  353. dout("mdsc get_session %p 0 -- FAIL\n", s);
  354. return NULL;
  355. }
  356. }
  357. void ceph_put_mds_session(struct ceph_mds_session *s)
  358. {
  359. dout("mdsc put_session %p %d -> %d\n", s,
  360. refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
  361. if (refcount_dec_and_test(&s->s_ref)) {
  362. if (s->s_auth.authorizer)
  363. ceph_auth_destroy_authorizer(s->s_auth.authorizer);
  364. kfree(s);
  365. }
  366. }
  367. /*
  368. * called under mdsc->mutex
  369. */
  370. struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
  371. int mds)
  372. {
  373. struct ceph_mds_session *session;
  374. if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
  375. return NULL;
  376. session = mdsc->sessions[mds];
  377. dout("lookup_mds_session %p %d\n", session,
  378. refcount_read(&session->s_ref));
  379. get_session(session);
  380. return session;
  381. }
  382. static bool __have_session(struct ceph_mds_client *mdsc, int mds)
  383. {
  384. if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
  385. return false;
  386. else
  387. return true;
  388. }
  389. static int __verify_registered_session(struct ceph_mds_client *mdsc,
  390. struct ceph_mds_session *s)
  391. {
  392. if (s->s_mds >= mdsc->max_sessions ||
  393. mdsc->sessions[s->s_mds] != s)
  394. return -ENOENT;
  395. return 0;
  396. }
  397. /*
  398. * create+register a new session for given mds.
  399. * called under mdsc->mutex.
  400. */
  401. static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
  402. int mds)
  403. {
  404. struct ceph_mds_session *s;
  405. if (mds >= mdsc->mdsmap->m_num_mds)
  406. return ERR_PTR(-EINVAL);
  407. s = kzalloc(sizeof(*s), GFP_NOFS);
  408. if (!s)
  409. return ERR_PTR(-ENOMEM);
  410. if (mds >= mdsc->max_sessions) {
  411. int newmax = 1 << get_count_order(mds + 1);
  412. struct ceph_mds_session **sa;
  413. dout("%s: realloc to %d\n", __func__, newmax);
  414. sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
  415. if (!sa)
  416. goto fail_realloc;
  417. if (mdsc->sessions) {
  418. memcpy(sa, mdsc->sessions,
  419. mdsc->max_sessions * sizeof(void *));
  420. kfree(mdsc->sessions);
  421. }
  422. mdsc->sessions = sa;
  423. mdsc->max_sessions = newmax;
  424. }
  425. dout("%s: mds%d\n", __func__, mds);
  426. s->s_mdsc = mdsc;
  427. s->s_mds = mds;
  428. s->s_state = CEPH_MDS_SESSION_NEW;
  429. s->s_ttl = 0;
  430. s->s_seq = 0;
  431. mutex_init(&s->s_mutex);
  432. ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
  433. spin_lock_init(&s->s_gen_ttl_lock);
  434. s->s_cap_gen = 0;
  435. s->s_cap_ttl = jiffies - 1;
  436. spin_lock_init(&s->s_cap_lock);
  437. s->s_renew_requested = 0;
  438. s->s_renew_seq = 0;
  439. INIT_LIST_HEAD(&s->s_caps);
  440. s->s_nr_caps = 0;
  441. s->s_trim_caps = 0;
  442. refcount_set(&s->s_ref, 1);
  443. INIT_LIST_HEAD(&s->s_waiting);
  444. INIT_LIST_HEAD(&s->s_unsafe);
  445. s->s_num_cap_releases = 0;
  446. s->s_cap_reconnect = 0;
  447. s->s_cap_iterator = NULL;
  448. INIT_LIST_HEAD(&s->s_cap_releases);
  449. INIT_LIST_HEAD(&s->s_cap_flushing);
  450. mdsc->sessions[mds] = s;
  451. atomic_inc(&mdsc->num_sessions);
  452. refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
  453. ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
  454. ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
  455. return s;
  456. fail_realloc:
  457. kfree(s);
  458. return ERR_PTR(-ENOMEM);
  459. }
  460. /*
  461. * called under mdsc->mutex
  462. */
  463. static void __unregister_session(struct ceph_mds_client *mdsc,
  464. struct ceph_mds_session *s)
  465. {
  466. dout("__unregister_session mds%d %p\n", s->s_mds, s);
  467. BUG_ON(mdsc->sessions[s->s_mds] != s);
  468. mdsc->sessions[s->s_mds] = NULL;
  469. ceph_con_close(&s->s_con);
  470. ceph_put_mds_session(s);
  471. atomic_dec(&mdsc->num_sessions);
  472. }
  473. /*
  474. * drop session refs in request.
  475. *
  476. * should be last request ref, or hold mdsc->mutex
  477. */
  478. static void put_request_session(struct ceph_mds_request *req)
  479. {
  480. if (req->r_session) {
  481. ceph_put_mds_session(req->r_session);
  482. req->r_session = NULL;
  483. }
  484. }
  485. void ceph_mdsc_release_request(struct kref *kref)
  486. {
  487. struct ceph_mds_request *req = container_of(kref,
  488. struct ceph_mds_request,
  489. r_kref);
  490. destroy_reply_info(&req->r_reply_info);
  491. if (req->r_request)
  492. ceph_msg_put(req->r_request);
  493. if (req->r_reply)
  494. ceph_msg_put(req->r_reply);
  495. if (req->r_inode) {
  496. ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
  497. iput(req->r_inode);
  498. }
  499. if (req->r_parent)
  500. ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
  501. iput(req->r_target_inode);
  502. if (req->r_dentry)
  503. dput(req->r_dentry);
  504. if (req->r_old_dentry)
  505. dput(req->r_old_dentry);
  506. if (req->r_old_dentry_dir) {
  507. /*
  508. * track (and drop pins for) r_old_dentry_dir
  509. * separately, since r_old_dentry's d_parent may have
  510. * changed between the dir mutex being dropped and
  511. * this request being freed.
  512. */
  513. ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
  514. CEPH_CAP_PIN);
  515. iput(req->r_old_dentry_dir);
  516. }
  517. kfree(req->r_path1);
  518. kfree(req->r_path2);
  519. if (req->r_pagelist)
  520. ceph_pagelist_release(req->r_pagelist);
  521. put_request_session(req);
  522. ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
  523. kfree(req);
  524. }
  525. DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
  526. /*
  527. * lookup session, bump ref if found.
  528. *
  529. * called under mdsc->mutex.
  530. */
  531. static struct ceph_mds_request *
  532. lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
  533. {
  534. struct ceph_mds_request *req;
  535. req = lookup_request(&mdsc->request_tree, tid);
  536. if (req)
  537. ceph_mdsc_get_request(req);
  538. return req;
  539. }
  540. /*
  541. * Register an in-flight request, and assign a tid. Link to directory
  542. * are modifying (if any).
  543. *
  544. * Called under mdsc->mutex.
  545. */
  546. static void __register_request(struct ceph_mds_client *mdsc,
  547. struct ceph_mds_request *req,
  548. struct inode *dir)
  549. {
  550. int ret = 0;
  551. req->r_tid = ++mdsc->last_tid;
  552. if (req->r_num_caps) {
  553. ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
  554. req->r_num_caps);
  555. if (ret < 0) {
  556. pr_err("__register_request %p "
  557. "failed to reserve caps: %d\n", req, ret);
  558. /* set req->r_err to fail early from __do_request */
  559. req->r_err = ret;
  560. return;
  561. }
  562. }
  563. dout("__register_request %p tid %lld\n", req, req->r_tid);
  564. ceph_mdsc_get_request(req);
  565. insert_request(&mdsc->request_tree, req);
  566. req->r_uid = current_fsuid();
  567. req->r_gid = current_fsgid();
  568. if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
  569. mdsc->oldest_tid = req->r_tid;
  570. if (dir) {
  571. ihold(dir);
  572. req->r_unsafe_dir = dir;
  573. }
  574. }
  575. static void __unregister_request(struct ceph_mds_client *mdsc,
  576. struct ceph_mds_request *req)
  577. {
  578. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  579. /* Never leave an unregistered request on an unsafe list! */
  580. list_del_init(&req->r_unsafe_item);
  581. if (req->r_tid == mdsc->oldest_tid) {
  582. struct rb_node *p = rb_next(&req->r_node);
  583. mdsc->oldest_tid = 0;
  584. while (p) {
  585. struct ceph_mds_request *next_req =
  586. rb_entry(p, struct ceph_mds_request, r_node);
  587. if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
  588. mdsc->oldest_tid = next_req->r_tid;
  589. break;
  590. }
  591. p = rb_next(p);
  592. }
  593. }
  594. erase_request(&mdsc->request_tree, req);
  595. if (req->r_unsafe_dir &&
  596. test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
  597. struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
  598. spin_lock(&ci->i_unsafe_lock);
  599. list_del_init(&req->r_unsafe_dir_item);
  600. spin_unlock(&ci->i_unsafe_lock);
  601. }
  602. if (req->r_target_inode &&
  603. test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
  604. struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
  605. spin_lock(&ci->i_unsafe_lock);
  606. list_del_init(&req->r_unsafe_target_item);
  607. spin_unlock(&ci->i_unsafe_lock);
  608. }
  609. if (req->r_unsafe_dir) {
  610. iput(req->r_unsafe_dir);
  611. req->r_unsafe_dir = NULL;
  612. }
  613. complete_all(&req->r_safe_completion);
  614. ceph_mdsc_put_request(req);
  615. }
  616. /*
  617. * Walk back up the dentry tree until we hit a dentry representing a
  618. * non-snapshot inode. We do this using the rcu_read_lock (which must be held
  619. * when calling this) to ensure that the objects won't disappear while we're
  620. * working with them. Once we hit a candidate dentry, we attempt to take a
  621. * reference to it, and return that as the result.
  622. */
  623. static struct inode *get_nonsnap_parent(struct dentry *dentry)
  624. {
  625. struct inode *inode = NULL;
  626. while (dentry && !IS_ROOT(dentry)) {
  627. inode = d_inode_rcu(dentry);
  628. if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
  629. break;
  630. dentry = dentry->d_parent;
  631. }
  632. if (inode)
  633. inode = igrab(inode);
  634. return inode;
  635. }
  636. /*
  637. * Choose mds to send request to next. If there is a hint set in the
  638. * request (e.g., due to a prior forward hint from the mds), use that.
  639. * Otherwise, consult frag tree and/or caps to identify the
  640. * appropriate mds. If all else fails, choose randomly.
  641. *
  642. * Called under mdsc->mutex.
  643. */
  644. static int __choose_mds(struct ceph_mds_client *mdsc,
  645. struct ceph_mds_request *req)
  646. {
  647. struct inode *inode;
  648. struct ceph_inode_info *ci;
  649. struct ceph_cap *cap;
  650. int mode = req->r_direct_mode;
  651. int mds = -1;
  652. u32 hash = req->r_direct_hash;
  653. bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
  654. /*
  655. * is there a specific mds we should try? ignore hint if we have
  656. * no session and the mds is not up (active or recovering).
  657. */
  658. if (req->r_resend_mds >= 0 &&
  659. (__have_session(mdsc, req->r_resend_mds) ||
  660. ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
  661. dout("choose_mds using resend_mds mds%d\n",
  662. req->r_resend_mds);
  663. return req->r_resend_mds;
  664. }
  665. if (mode == USE_RANDOM_MDS)
  666. goto random;
  667. inode = NULL;
  668. if (req->r_inode) {
  669. if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
  670. inode = req->r_inode;
  671. ihold(inode);
  672. } else {
  673. /* req->r_dentry is non-null for LSSNAP request */
  674. rcu_read_lock();
  675. inode = get_nonsnap_parent(req->r_dentry);
  676. rcu_read_unlock();
  677. dout("__choose_mds using snapdir's parent %p\n", inode);
  678. }
  679. } else if (req->r_dentry) {
  680. /* ignore race with rename; old or new d_parent is okay */
  681. struct dentry *parent;
  682. struct inode *dir;
  683. rcu_read_lock();
  684. parent = req->r_dentry->d_parent;
  685. dir = req->r_parent ? : d_inode_rcu(parent);
  686. if (!dir || dir->i_sb != mdsc->fsc->sb) {
  687. /* not this fs or parent went negative */
  688. inode = d_inode(req->r_dentry);
  689. if (inode)
  690. ihold(inode);
  691. } else if (ceph_snap(dir) != CEPH_NOSNAP) {
  692. /* direct snapped/virtual snapdir requests
  693. * based on parent dir inode */
  694. inode = get_nonsnap_parent(parent);
  695. dout("__choose_mds using nonsnap parent %p\n", inode);
  696. } else {
  697. /* dentry target */
  698. inode = d_inode(req->r_dentry);
  699. if (!inode || mode == USE_AUTH_MDS) {
  700. /* dir + name */
  701. inode = igrab(dir);
  702. hash = ceph_dentry_hash(dir, req->r_dentry);
  703. is_hash = true;
  704. } else {
  705. ihold(inode);
  706. }
  707. }
  708. rcu_read_unlock();
  709. }
  710. dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
  711. (int)hash, mode);
  712. if (!inode)
  713. goto random;
  714. ci = ceph_inode(inode);
  715. if (is_hash && S_ISDIR(inode->i_mode)) {
  716. struct ceph_inode_frag frag;
  717. int found;
  718. ceph_choose_frag(ci, hash, &frag, &found);
  719. if (found) {
  720. if (mode == USE_ANY_MDS && frag.ndist > 0) {
  721. u8 r;
  722. /* choose a random replica */
  723. get_random_bytes(&r, 1);
  724. r %= frag.ndist;
  725. mds = frag.dist[r];
  726. dout("choose_mds %p %llx.%llx "
  727. "frag %u mds%d (%d/%d)\n",
  728. inode, ceph_vinop(inode),
  729. frag.frag, mds,
  730. (int)r, frag.ndist);
  731. if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
  732. CEPH_MDS_STATE_ACTIVE)
  733. goto out;
  734. }
  735. /* since this file/dir wasn't known to be
  736. * replicated, then we want to look for the
  737. * authoritative mds. */
  738. mode = USE_AUTH_MDS;
  739. if (frag.mds >= 0) {
  740. /* choose auth mds */
  741. mds = frag.mds;
  742. dout("choose_mds %p %llx.%llx "
  743. "frag %u mds%d (auth)\n",
  744. inode, ceph_vinop(inode), frag.frag, mds);
  745. if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
  746. CEPH_MDS_STATE_ACTIVE)
  747. goto out;
  748. }
  749. }
  750. }
  751. spin_lock(&ci->i_ceph_lock);
  752. cap = NULL;
  753. if (mode == USE_AUTH_MDS)
  754. cap = ci->i_auth_cap;
  755. if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
  756. cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
  757. if (!cap) {
  758. spin_unlock(&ci->i_ceph_lock);
  759. iput(inode);
  760. goto random;
  761. }
  762. mds = cap->session->s_mds;
  763. dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
  764. inode, ceph_vinop(inode), mds,
  765. cap == ci->i_auth_cap ? "auth " : "", cap);
  766. spin_unlock(&ci->i_ceph_lock);
  767. out:
  768. iput(inode);
  769. return mds;
  770. random:
  771. mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
  772. dout("choose_mds chose random mds%d\n", mds);
  773. return mds;
  774. }
  775. /*
  776. * session messages
  777. */
  778. static struct ceph_msg *create_session_msg(u32 op, u64 seq)
  779. {
  780. struct ceph_msg *msg;
  781. struct ceph_mds_session_head *h;
  782. msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
  783. false);
  784. if (!msg) {
  785. pr_err("create_session_msg ENOMEM creating msg\n");
  786. return NULL;
  787. }
  788. h = msg->front.iov_base;
  789. h->op = cpu_to_le32(op);
  790. h->seq = cpu_to_le64(seq);
  791. return msg;
  792. }
  793. static void encode_supported_features(void **p, void *end)
  794. {
  795. static const unsigned char bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
  796. static const size_t count = ARRAY_SIZE(bits);
  797. if (count > 0) {
  798. size_t i;
  799. size_t size = ((size_t)bits[count - 1] + 64) / 64 * 8;
  800. BUG_ON(*p + 4 + size > end);
  801. ceph_encode_32(p, size);
  802. memset(*p, 0, size);
  803. for (i = 0; i < count; i++)
  804. ((unsigned char*)(*p))[i / 8] |= 1 << (bits[i] % 8);
  805. *p += size;
  806. } else {
  807. BUG_ON(*p + 4 > end);
  808. ceph_encode_32(p, 0);
  809. }
  810. }
  811. /*
  812. * session message, specialization for CEPH_SESSION_REQUEST_OPEN
  813. * to include additional client metadata fields.
  814. */
  815. static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
  816. {
  817. struct ceph_msg *msg;
  818. struct ceph_mds_session_head *h;
  819. int i = -1;
  820. int extra_bytes = 0;
  821. int metadata_key_count = 0;
  822. struct ceph_options *opt = mdsc->fsc->client->options;
  823. struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
  824. void *p, *end;
  825. const char* metadata[][2] = {
  826. {"hostname", mdsc->nodename},
  827. {"kernel_version", init_utsname()->release},
  828. {"entity_id", opt->name ? : ""},
  829. {"root", fsopt->server_path ? : "/"},
  830. {NULL, NULL}
  831. };
  832. /* Calculate serialized length of metadata */
  833. extra_bytes = 4; /* map length */
  834. for (i = 0; metadata[i][0]; ++i) {
  835. extra_bytes += 8 + strlen(metadata[i][0]) +
  836. strlen(metadata[i][1]);
  837. metadata_key_count++;
  838. }
  839. /* supported feature */
  840. extra_bytes += 4 + 8;
  841. /* Allocate the message */
  842. msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
  843. GFP_NOFS, false);
  844. if (!msg) {
  845. pr_err("create_session_msg ENOMEM creating msg\n");
  846. return NULL;
  847. }
  848. p = msg->front.iov_base;
  849. end = p + msg->front.iov_len;
  850. h = p;
  851. h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
  852. h->seq = cpu_to_le64(seq);
  853. /*
  854. * Serialize client metadata into waiting buffer space, using
  855. * the format that userspace expects for map<string, string>
  856. *
  857. * ClientSession messages with metadata are v2
  858. */
  859. msg->hdr.version = cpu_to_le16(3);
  860. msg->hdr.compat_version = cpu_to_le16(1);
  861. /* The write pointer, following the session_head structure */
  862. p += sizeof(*h);
  863. /* Number of entries in the map */
  864. ceph_encode_32(&p, metadata_key_count);
  865. /* Two length-prefixed strings for each entry in the map */
  866. for (i = 0; metadata[i][0]; ++i) {
  867. size_t const key_len = strlen(metadata[i][0]);
  868. size_t const val_len = strlen(metadata[i][1]);
  869. ceph_encode_32(&p, key_len);
  870. memcpy(p, metadata[i][0], key_len);
  871. p += key_len;
  872. ceph_encode_32(&p, val_len);
  873. memcpy(p, metadata[i][1], val_len);
  874. p += val_len;
  875. }
  876. encode_supported_features(&p, end);
  877. msg->front.iov_len = p - msg->front.iov_base;
  878. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  879. return msg;
  880. }
  881. /*
  882. * send session open request.
  883. *
  884. * called under mdsc->mutex
  885. */
  886. static int __open_session(struct ceph_mds_client *mdsc,
  887. struct ceph_mds_session *session)
  888. {
  889. struct ceph_msg *msg;
  890. int mstate;
  891. int mds = session->s_mds;
  892. /* wait for mds to go active? */
  893. mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
  894. dout("open_session to mds%d (%s)\n", mds,
  895. ceph_mds_state_name(mstate));
  896. session->s_state = CEPH_MDS_SESSION_OPENING;
  897. session->s_renew_requested = jiffies;
  898. /* send connect message */
  899. msg = create_session_open_msg(mdsc, session->s_seq);
  900. if (!msg)
  901. return -ENOMEM;
  902. ceph_con_send(&session->s_con, msg);
  903. return 0;
  904. }
  905. /*
  906. * open sessions for any export targets for the given mds
  907. *
  908. * called under mdsc->mutex
  909. */
  910. static struct ceph_mds_session *
  911. __open_export_target_session(struct ceph_mds_client *mdsc, int target)
  912. {
  913. struct ceph_mds_session *session;
  914. session = __ceph_lookup_mds_session(mdsc, target);
  915. if (!session) {
  916. session = register_session(mdsc, target);
  917. if (IS_ERR(session))
  918. return session;
  919. }
  920. if (session->s_state == CEPH_MDS_SESSION_NEW ||
  921. session->s_state == CEPH_MDS_SESSION_CLOSING)
  922. __open_session(mdsc, session);
  923. return session;
  924. }
  925. struct ceph_mds_session *
  926. ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
  927. {
  928. struct ceph_mds_session *session;
  929. dout("open_export_target_session to mds%d\n", target);
  930. mutex_lock(&mdsc->mutex);
  931. session = __open_export_target_session(mdsc, target);
  932. mutex_unlock(&mdsc->mutex);
  933. return session;
  934. }
  935. static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
  936. struct ceph_mds_session *session)
  937. {
  938. struct ceph_mds_info *mi;
  939. struct ceph_mds_session *ts;
  940. int i, mds = session->s_mds;
  941. if (mds >= mdsc->mdsmap->m_num_mds)
  942. return;
  943. mi = &mdsc->mdsmap->m_info[mds];
  944. dout("open_export_target_sessions for mds%d (%d targets)\n",
  945. session->s_mds, mi->num_export_targets);
  946. for (i = 0; i < mi->num_export_targets; i++) {
  947. ts = __open_export_target_session(mdsc, mi->export_targets[i]);
  948. if (!IS_ERR(ts))
  949. ceph_put_mds_session(ts);
  950. }
  951. }
  952. void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
  953. struct ceph_mds_session *session)
  954. {
  955. mutex_lock(&mdsc->mutex);
  956. __open_export_target_sessions(mdsc, session);
  957. mutex_unlock(&mdsc->mutex);
  958. }
  959. /*
  960. * session caps
  961. */
  962. static void detach_cap_releases(struct ceph_mds_session *session,
  963. struct list_head *target)
  964. {
  965. lockdep_assert_held(&session->s_cap_lock);
  966. list_splice_init(&session->s_cap_releases, target);
  967. session->s_num_cap_releases = 0;
  968. dout("dispose_cap_releases mds%d\n", session->s_mds);
  969. }
  970. static void dispose_cap_releases(struct ceph_mds_client *mdsc,
  971. struct list_head *dispose)
  972. {
  973. while (!list_empty(dispose)) {
  974. struct ceph_cap *cap;
  975. /* zero out the in-progress message */
  976. cap = list_first_entry(dispose, struct ceph_cap, session_caps);
  977. list_del(&cap->session_caps);
  978. ceph_put_cap(mdsc, cap);
  979. }
  980. }
  981. static void cleanup_session_requests(struct ceph_mds_client *mdsc,
  982. struct ceph_mds_session *session)
  983. {
  984. struct ceph_mds_request *req;
  985. struct rb_node *p;
  986. dout("cleanup_session_requests mds%d\n", session->s_mds);
  987. mutex_lock(&mdsc->mutex);
  988. while (!list_empty(&session->s_unsafe)) {
  989. req = list_first_entry(&session->s_unsafe,
  990. struct ceph_mds_request, r_unsafe_item);
  991. pr_warn_ratelimited(" dropping unsafe request %llu\n",
  992. req->r_tid);
  993. __unregister_request(mdsc, req);
  994. }
  995. /* zero r_attempts, so kick_requests() will re-send requests */
  996. p = rb_first(&mdsc->request_tree);
  997. while (p) {
  998. req = rb_entry(p, struct ceph_mds_request, r_node);
  999. p = rb_next(p);
  1000. if (req->r_session &&
  1001. req->r_session->s_mds == session->s_mds)
  1002. req->r_attempts = 0;
  1003. }
  1004. mutex_unlock(&mdsc->mutex);
  1005. }
  1006. /*
  1007. * Helper to safely iterate over all caps associated with a session, with
  1008. * special care taken to handle a racing __ceph_remove_cap().
  1009. *
  1010. * Caller must hold session s_mutex.
  1011. */
  1012. static int iterate_session_caps(struct ceph_mds_session *session,
  1013. int (*cb)(struct inode *, struct ceph_cap *,
  1014. void *), void *arg)
  1015. {
  1016. struct list_head *p;
  1017. struct ceph_cap *cap;
  1018. struct inode *inode, *last_inode = NULL;
  1019. struct ceph_cap *old_cap = NULL;
  1020. int ret;
  1021. dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
  1022. spin_lock(&session->s_cap_lock);
  1023. p = session->s_caps.next;
  1024. while (p != &session->s_caps) {
  1025. cap = list_entry(p, struct ceph_cap, session_caps);
  1026. inode = igrab(&cap->ci->vfs_inode);
  1027. if (!inode) {
  1028. p = p->next;
  1029. continue;
  1030. }
  1031. session->s_cap_iterator = cap;
  1032. spin_unlock(&session->s_cap_lock);
  1033. if (last_inode) {
  1034. iput(last_inode);
  1035. last_inode = NULL;
  1036. }
  1037. if (old_cap) {
  1038. ceph_put_cap(session->s_mdsc, old_cap);
  1039. old_cap = NULL;
  1040. }
  1041. ret = cb(inode, cap, arg);
  1042. last_inode = inode;
  1043. spin_lock(&session->s_cap_lock);
  1044. p = p->next;
  1045. if (!cap->ci) {
  1046. dout("iterate_session_caps finishing cap %p removal\n",
  1047. cap);
  1048. BUG_ON(cap->session != session);
  1049. cap->session = NULL;
  1050. list_del_init(&cap->session_caps);
  1051. session->s_nr_caps--;
  1052. if (cap->queue_release) {
  1053. list_add_tail(&cap->session_caps,
  1054. &session->s_cap_releases);
  1055. session->s_num_cap_releases++;
  1056. } else {
  1057. old_cap = cap; /* put_cap it w/o locks held */
  1058. }
  1059. }
  1060. if (ret < 0)
  1061. goto out;
  1062. }
  1063. ret = 0;
  1064. out:
  1065. session->s_cap_iterator = NULL;
  1066. spin_unlock(&session->s_cap_lock);
  1067. iput(last_inode);
  1068. if (old_cap)
  1069. ceph_put_cap(session->s_mdsc, old_cap);
  1070. return ret;
  1071. }
  1072. static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
  1073. void *arg)
  1074. {
  1075. struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
  1076. struct ceph_inode_info *ci = ceph_inode(inode);
  1077. LIST_HEAD(to_remove);
  1078. bool drop = false;
  1079. bool invalidate = false;
  1080. dout("removing cap %p, ci is %p, inode is %p\n",
  1081. cap, ci, &ci->vfs_inode);
  1082. spin_lock(&ci->i_ceph_lock);
  1083. __ceph_remove_cap(cap, false);
  1084. if (!ci->i_auth_cap) {
  1085. struct ceph_cap_flush *cf;
  1086. struct ceph_mds_client *mdsc = fsc->mdsc;
  1087. ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
  1088. if (ci->i_wrbuffer_ref > 0 &&
  1089. READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
  1090. invalidate = true;
  1091. while (!list_empty(&ci->i_cap_flush_list)) {
  1092. cf = list_first_entry(&ci->i_cap_flush_list,
  1093. struct ceph_cap_flush, i_list);
  1094. list_move(&cf->i_list, &to_remove);
  1095. }
  1096. spin_lock(&mdsc->cap_dirty_lock);
  1097. list_for_each_entry(cf, &to_remove, i_list)
  1098. list_del(&cf->g_list);
  1099. if (!list_empty(&ci->i_dirty_item)) {
  1100. pr_warn_ratelimited(
  1101. " dropping dirty %s state for %p %lld\n",
  1102. ceph_cap_string(ci->i_dirty_caps),
  1103. inode, ceph_ino(inode));
  1104. ci->i_dirty_caps = 0;
  1105. list_del_init(&ci->i_dirty_item);
  1106. drop = true;
  1107. }
  1108. if (!list_empty(&ci->i_flushing_item)) {
  1109. pr_warn_ratelimited(
  1110. " dropping dirty+flushing %s state for %p %lld\n",
  1111. ceph_cap_string(ci->i_flushing_caps),
  1112. inode, ceph_ino(inode));
  1113. ci->i_flushing_caps = 0;
  1114. list_del_init(&ci->i_flushing_item);
  1115. mdsc->num_cap_flushing--;
  1116. drop = true;
  1117. }
  1118. spin_unlock(&mdsc->cap_dirty_lock);
  1119. if (atomic_read(&ci->i_filelock_ref) > 0) {
  1120. /* make further file lock syscall return -EIO */
  1121. ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
  1122. pr_warn_ratelimited(" dropping file locks for %p %lld\n",
  1123. inode, ceph_ino(inode));
  1124. }
  1125. if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
  1126. list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
  1127. ci->i_prealloc_cap_flush = NULL;
  1128. }
  1129. if (drop &&
  1130. ci->i_wrbuffer_ref_head == 0 &&
  1131. ci->i_wr_ref == 0 &&
  1132. ci->i_dirty_caps == 0 &&
  1133. ci->i_flushing_caps == 0) {
  1134. ceph_put_snap_context(ci->i_head_snapc);
  1135. ci->i_head_snapc = NULL;
  1136. }
  1137. }
  1138. spin_unlock(&ci->i_ceph_lock);
  1139. while (!list_empty(&to_remove)) {
  1140. struct ceph_cap_flush *cf;
  1141. cf = list_first_entry(&to_remove,
  1142. struct ceph_cap_flush, i_list);
  1143. list_del(&cf->i_list);
  1144. ceph_free_cap_flush(cf);
  1145. }
  1146. wake_up_all(&ci->i_cap_wq);
  1147. if (invalidate)
  1148. ceph_queue_invalidate(inode);
  1149. if (drop)
  1150. iput(inode);
  1151. return 0;
  1152. }
  1153. /*
  1154. * caller must hold session s_mutex
  1155. */
  1156. static void remove_session_caps(struct ceph_mds_session *session)
  1157. {
  1158. struct ceph_fs_client *fsc = session->s_mdsc->fsc;
  1159. struct super_block *sb = fsc->sb;
  1160. LIST_HEAD(dispose);
  1161. dout("remove_session_caps on %p\n", session);
  1162. iterate_session_caps(session, remove_session_caps_cb, fsc);
  1163. wake_up_all(&fsc->mdsc->cap_flushing_wq);
  1164. spin_lock(&session->s_cap_lock);
  1165. if (session->s_nr_caps > 0) {
  1166. struct inode *inode;
  1167. struct ceph_cap *cap, *prev = NULL;
  1168. struct ceph_vino vino;
  1169. /*
  1170. * iterate_session_caps() skips inodes that are being
  1171. * deleted, we need to wait until deletions are complete.
  1172. * __wait_on_freeing_inode() is designed for the job,
  1173. * but it is not exported, so use lookup inode function
  1174. * to access it.
  1175. */
  1176. while (!list_empty(&session->s_caps)) {
  1177. cap = list_entry(session->s_caps.next,
  1178. struct ceph_cap, session_caps);
  1179. if (cap == prev)
  1180. break;
  1181. prev = cap;
  1182. vino = cap->ci->i_vino;
  1183. spin_unlock(&session->s_cap_lock);
  1184. inode = ceph_find_inode(sb, vino);
  1185. iput(inode);
  1186. spin_lock(&session->s_cap_lock);
  1187. }
  1188. }
  1189. // drop cap expires and unlock s_cap_lock
  1190. detach_cap_releases(session, &dispose);
  1191. BUG_ON(session->s_nr_caps > 0);
  1192. BUG_ON(!list_empty(&session->s_cap_flushing));
  1193. spin_unlock(&session->s_cap_lock);
  1194. dispose_cap_releases(session->s_mdsc, &dispose);
  1195. }
  1196. /*
  1197. * wake up any threads waiting on this session's caps. if the cap is
  1198. * old (didn't get renewed on the client reconnect), remove it now.
  1199. *
  1200. * caller must hold s_mutex.
  1201. */
  1202. static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
  1203. void *arg)
  1204. {
  1205. struct ceph_inode_info *ci = ceph_inode(inode);
  1206. if (arg) {
  1207. spin_lock(&ci->i_ceph_lock);
  1208. ci->i_wanted_max_size = 0;
  1209. ci->i_requested_max_size = 0;
  1210. spin_unlock(&ci->i_ceph_lock);
  1211. }
  1212. wake_up_all(&ci->i_cap_wq);
  1213. return 0;
  1214. }
  1215. static void wake_up_session_caps(struct ceph_mds_session *session,
  1216. int reconnect)
  1217. {
  1218. dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
  1219. iterate_session_caps(session, wake_up_session_cb,
  1220. (void *)(unsigned long)reconnect);
  1221. }
  1222. /*
  1223. * Send periodic message to MDS renewing all currently held caps. The
  1224. * ack will reset the expiration for all caps from this session.
  1225. *
  1226. * caller holds s_mutex
  1227. */
  1228. static int send_renew_caps(struct ceph_mds_client *mdsc,
  1229. struct ceph_mds_session *session)
  1230. {
  1231. struct ceph_msg *msg;
  1232. int state;
  1233. if (time_after_eq(jiffies, session->s_cap_ttl) &&
  1234. time_after_eq(session->s_cap_ttl, session->s_renew_requested))
  1235. pr_info("mds%d caps stale\n", session->s_mds);
  1236. session->s_renew_requested = jiffies;
  1237. /* do not try to renew caps until a recovering mds has reconnected
  1238. * with its clients. */
  1239. state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
  1240. if (state < CEPH_MDS_STATE_RECONNECT) {
  1241. dout("send_renew_caps ignoring mds%d (%s)\n",
  1242. session->s_mds, ceph_mds_state_name(state));
  1243. return 0;
  1244. }
  1245. dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
  1246. ceph_mds_state_name(state));
  1247. msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
  1248. ++session->s_renew_seq);
  1249. if (!msg)
  1250. return -ENOMEM;
  1251. ceph_con_send(&session->s_con, msg);
  1252. return 0;
  1253. }
  1254. static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
  1255. struct ceph_mds_session *session, u64 seq)
  1256. {
  1257. struct ceph_msg *msg;
  1258. dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
  1259. session->s_mds, ceph_session_state_name(session->s_state), seq);
  1260. msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
  1261. if (!msg)
  1262. return -ENOMEM;
  1263. ceph_con_send(&session->s_con, msg);
  1264. return 0;
  1265. }
  1266. /*
  1267. * Note new cap ttl, and any transition from stale -> not stale (fresh?).
  1268. *
  1269. * Called under session->s_mutex
  1270. */
  1271. static void renewed_caps(struct ceph_mds_client *mdsc,
  1272. struct ceph_mds_session *session, int is_renew)
  1273. {
  1274. int was_stale;
  1275. int wake = 0;
  1276. spin_lock(&session->s_cap_lock);
  1277. was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
  1278. session->s_cap_ttl = session->s_renew_requested +
  1279. mdsc->mdsmap->m_session_timeout*HZ;
  1280. if (was_stale) {
  1281. if (time_before(jiffies, session->s_cap_ttl)) {
  1282. pr_info("mds%d caps renewed\n", session->s_mds);
  1283. wake = 1;
  1284. } else {
  1285. pr_info("mds%d caps still stale\n", session->s_mds);
  1286. }
  1287. }
  1288. dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
  1289. session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
  1290. time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
  1291. spin_unlock(&session->s_cap_lock);
  1292. if (wake)
  1293. wake_up_session_caps(session, 0);
  1294. }
  1295. /*
  1296. * send a session close request
  1297. */
  1298. static int request_close_session(struct ceph_mds_client *mdsc,
  1299. struct ceph_mds_session *session)
  1300. {
  1301. struct ceph_msg *msg;
  1302. dout("request_close_session mds%d state %s seq %lld\n",
  1303. session->s_mds, ceph_session_state_name(session->s_state),
  1304. session->s_seq);
  1305. msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
  1306. if (!msg)
  1307. return -ENOMEM;
  1308. ceph_con_send(&session->s_con, msg);
  1309. return 1;
  1310. }
  1311. /*
  1312. * Called with s_mutex held.
  1313. */
  1314. static int __close_session(struct ceph_mds_client *mdsc,
  1315. struct ceph_mds_session *session)
  1316. {
  1317. if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
  1318. return 0;
  1319. session->s_state = CEPH_MDS_SESSION_CLOSING;
  1320. return request_close_session(mdsc, session);
  1321. }
  1322. static bool drop_negative_children(struct dentry *dentry)
  1323. {
  1324. struct dentry *child;
  1325. bool all_negative = true;
  1326. if (!d_is_dir(dentry))
  1327. goto out;
  1328. spin_lock(&dentry->d_lock);
  1329. list_for_each_entry(child, &dentry->d_subdirs, d_child) {
  1330. if (d_really_is_positive(child)) {
  1331. all_negative = false;
  1332. break;
  1333. }
  1334. }
  1335. spin_unlock(&dentry->d_lock);
  1336. if (all_negative)
  1337. shrink_dcache_parent(dentry);
  1338. out:
  1339. return all_negative;
  1340. }
  1341. /*
  1342. * Trim old(er) caps.
  1343. *
  1344. * Because we can't cache an inode without one or more caps, we do
  1345. * this indirectly: if a cap is unused, we prune its aliases, at which
  1346. * point the inode will hopefully get dropped to.
  1347. *
  1348. * Yes, this is a bit sloppy. Our only real goal here is to respond to
  1349. * memory pressure from the MDS, though, so it needn't be perfect.
  1350. */
  1351. static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
  1352. {
  1353. struct ceph_mds_session *session = arg;
  1354. struct ceph_inode_info *ci = ceph_inode(inode);
  1355. int used, wanted, oissued, mine;
  1356. if (session->s_trim_caps <= 0)
  1357. return -1;
  1358. spin_lock(&ci->i_ceph_lock);
  1359. mine = cap->issued | cap->implemented;
  1360. used = __ceph_caps_used(ci);
  1361. wanted = __ceph_caps_file_wanted(ci);
  1362. oissued = __ceph_caps_issued_other(ci, cap);
  1363. dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
  1364. inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
  1365. ceph_cap_string(used), ceph_cap_string(wanted));
  1366. if (cap == ci->i_auth_cap) {
  1367. if (ci->i_dirty_caps || ci->i_flushing_caps ||
  1368. !list_empty(&ci->i_cap_snaps))
  1369. goto out;
  1370. if ((used | wanted) & CEPH_CAP_ANY_WR)
  1371. goto out;
  1372. /* Note: it's possible that i_filelock_ref becomes non-zero
  1373. * after dropping auth caps. It doesn't hurt because reply
  1374. * of lock mds request will re-add auth caps. */
  1375. if (atomic_read(&ci->i_filelock_ref) > 0)
  1376. goto out;
  1377. }
  1378. /* The inode has cached pages, but it's no longer used.
  1379. * we can safely drop it */
  1380. if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
  1381. !(oissued & CEPH_CAP_FILE_CACHE)) {
  1382. used = 0;
  1383. oissued = 0;
  1384. }
  1385. if ((used | wanted) & ~oissued & mine)
  1386. goto out; /* we need these caps */
  1387. if (oissued) {
  1388. /* we aren't the only cap.. just remove us */
  1389. __ceph_remove_cap(cap, true);
  1390. session->s_trim_caps--;
  1391. } else {
  1392. struct dentry *dentry;
  1393. /* try dropping referring dentries */
  1394. spin_unlock(&ci->i_ceph_lock);
  1395. dentry = d_find_any_alias(inode);
  1396. if (dentry && drop_negative_children(dentry)) {
  1397. int count;
  1398. dput(dentry);
  1399. d_prune_aliases(inode);
  1400. count = atomic_read(&inode->i_count);
  1401. if (count == 1)
  1402. session->s_trim_caps--;
  1403. dout("trim_caps_cb %p cap %p pruned, count now %d\n",
  1404. inode, cap, count);
  1405. } else {
  1406. dput(dentry);
  1407. }
  1408. return 0;
  1409. }
  1410. out:
  1411. spin_unlock(&ci->i_ceph_lock);
  1412. return 0;
  1413. }
  1414. /*
  1415. * Trim session cap count down to some max number.
  1416. */
  1417. int ceph_trim_caps(struct ceph_mds_client *mdsc,
  1418. struct ceph_mds_session *session,
  1419. int max_caps)
  1420. {
  1421. int trim_caps = session->s_nr_caps - max_caps;
  1422. dout("trim_caps mds%d start: %d / %d, trim %d\n",
  1423. session->s_mds, session->s_nr_caps, max_caps, trim_caps);
  1424. if (trim_caps > 0) {
  1425. session->s_trim_caps = trim_caps;
  1426. iterate_session_caps(session, trim_caps_cb, session);
  1427. dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
  1428. session->s_mds, session->s_nr_caps, max_caps,
  1429. trim_caps - session->s_trim_caps);
  1430. session->s_trim_caps = 0;
  1431. }
  1432. ceph_send_cap_releases(mdsc, session);
  1433. return 0;
  1434. }
  1435. static int check_caps_flush(struct ceph_mds_client *mdsc,
  1436. u64 want_flush_tid)
  1437. {
  1438. int ret = 1;
  1439. spin_lock(&mdsc->cap_dirty_lock);
  1440. if (!list_empty(&mdsc->cap_flush_list)) {
  1441. struct ceph_cap_flush *cf =
  1442. list_first_entry(&mdsc->cap_flush_list,
  1443. struct ceph_cap_flush, g_list);
  1444. if (cf->tid <= want_flush_tid) {
  1445. dout("check_caps_flush still flushing tid "
  1446. "%llu <= %llu\n", cf->tid, want_flush_tid);
  1447. ret = 0;
  1448. }
  1449. }
  1450. spin_unlock(&mdsc->cap_dirty_lock);
  1451. return ret;
  1452. }
  1453. /*
  1454. * flush all dirty inode data to disk.
  1455. *
  1456. * returns true if we've flushed through want_flush_tid
  1457. */
  1458. static void wait_caps_flush(struct ceph_mds_client *mdsc,
  1459. u64 want_flush_tid)
  1460. {
  1461. dout("check_caps_flush want %llu\n", want_flush_tid);
  1462. wait_event(mdsc->cap_flushing_wq,
  1463. check_caps_flush(mdsc, want_flush_tid));
  1464. dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
  1465. }
  1466. /*
  1467. * called under s_mutex
  1468. */
  1469. void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
  1470. struct ceph_mds_session *session)
  1471. {
  1472. struct ceph_msg *msg = NULL;
  1473. struct ceph_mds_cap_release *head;
  1474. struct ceph_mds_cap_item *item;
  1475. struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
  1476. struct ceph_cap *cap;
  1477. LIST_HEAD(tmp_list);
  1478. int num_cap_releases;
  1479. __le32 barrier, *cap_barrier;
  1480. down_read(&osdc->lock);
  1481. barrier = cpu_to_le32(osdc->epoch_barrier);
  1482. up_read(&osdc->lock);
  1483. spin_lock(&session->s_cap_lock);
  1484. again:
  1485. list_splice_init(&session->s_cap_releases, &tmp_list);
  1486. num_cap_releases = session->s_num_cap_releases;
  1487. session->s_num_cap_releases = 0;
  1488. spin_unlock(&session->s_cap_lock);
  1489. while (!list_empty(&tmp_list)) {
  1490. if (!msg) {
  1491. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
  1492. PAGE_SIZE, GFP_NOFS, false);
  1493. if (!msg)
  1494. goto out_err;
  1495. head = msg->front.iov_base;
  1496. head->num = cpu_to_le32(0);
  1497. msg->front.iov_len = sizeof(*head);
  1498. msg->hdr.version = cpu_to_le16(2);
  1499. msg->hdr.compat_version = cpu_to_le16(1);
  1500. }
  1501. cap = list_first_entry(&tmp_list, struct ceph_cap,
  1502. session_caps);
  1503. list_del(&cap->session_caps);
  1504. num_cap_releases--;
  1505. head = msg->front.iov_base;
  1506. le32_add_cpu(&head->num, 1);
  1507. item = msg->front.iov_base + msg->front.iov_len;
  1508. item->ino = cpu_to_le64(cap->cap_ino);
  1509. item->cap_id = cpu_to_le64(cap->cap_id);
  1510. item->migrate_seq = cpu_to_le32(cap->mseq);
  1511. item->seq = cpu_to_le32(cap->issue_seq);
  1512. msg->front.iov_len += sizeof(*item);
  1513. ceph_put_cap(mdsc, cap);
  1514. if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
  1515. // Append cap_barrier field
  1516. cap_barrier = msg->front.iov_base + msg->front.iov_len;
  1517. *cap_barrier = barrier;
  1518. msg->front.iov_len += sizeof(*cap_barrier);
  1519. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1520. dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
  1521. ceph_con_send(&session->s_con, msg);
  1522. msg = NULL;
  1523. }
  1524. }
  1525. BUG_ON(num_cap_releases != 0);
  1526. spin_lock(&session->s_cap_lock);
  1527. if (!list_empty(&session->s_cap_releases))
  1528. goto again;
  1529. spin_unlock(&session->s_cap_lock);
  1530. if (msg) {
  1531. // Append cap_barrier field
  1532. cap_barrier = msg->front.iov_base + msg->front.iov_len;
  1533. *cap_barrier = barrier;
  1534. msg->front.iov_len += sizeof(*cap_barrier);
  1535. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1536. dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
  1537. ceph_con_send(&session->s_con, msg);
  1538. }
  1539. return;
  1540. out_err:
  1541. pr_err("send_cap_releases mds%d, failed to allocate message\n",
  1542. session->s_mds);
  1543. spin_lock(&session->s_cap_lock);
  1544. list_splice(&tmp_list, &session->s_cap_releases);
  1545. session->s_num_cap_releases += num_cap_releases;
  1546. spin_unlock(&session->s_cap_lock);
  1547. }
  1548. /*
  1549. * requests
  1550. */
  1551. int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
  1552. struct inode *dir)
  1553. {
  1554. struct ceph_inode_info *ci = ceph_inode(dir);
  1555. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1556. struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
  1557. size_t size = sizeof(struct ceph_mds_reply_dir_entry);
  1558. int order, num_entries;
  1559. spin_lock(&ci->i_ceph_lock);
  1560. num_entries = ci->i_files + ci->i_subdirs;
  1561. spin_unlock(&ci->i_ceph_lock);
  1562. num_entries = max(num_entries, 1);
  1563. num_entries = min(num_entries, opt->max_readdir);
  1564. order = get_order(size * num_entries);
  1565. while (order >= 0) {
  1566. rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
  1567. __GFP_NOWARN,
  1568. order);
  1569. if (rinfo->dir_entries)
  1570. break;
  1571. order--;
  1572. }
  1573. if (!rinfo->dir_entries)
  1574. return -ENOMEM;
  1575. num_entries = (PAGE_SIZE << order) / size;
  1576. num_entries = min(num_entries, opt->max_readdir);
  1577. rinfo->dir_buf_size = PAGE_SIZE << order;
  1578. req->r_num_caps = num_entries + 1;
  1579. req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
  1580. req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
  1581. return 0;
  1582. }
  1583. /*
  1584. * Create an mds request.
  1585. */
  1586. struct ceph_mds_request *
  1587. ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
  1588. {
  1589. struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
  1590. struct timespec64 ts;
  1591. if (!req)
  1592. return ERR_PTR(-ENOMEM);
  1593. mutex_init(&req->r_fill_mutex);
  1594. req->r_mdsc = mdsc;
  1595. req->r_started = jiffies;
  1596. req->r_resend_mds = -1;
  1597. INIT_LIST_HEAD(&req->r_unsafe_dir_item);
  1598. INIT_LIST_HEAD(&req->r_unsafe_target_item);
  1599. req->r_fmode = -1;
  1600. kref_init(&req->r_kref);
  1601. RB_CLEAR_NODE(&req->r_node);
  1602. INIT_LIST_HEAD(&req->r_wait);
  1603. init_completion(&req->r_completion);
  1604. init_completion(&req->r_safe_completion);
  1605. INIT_LIST_HEAD(&req->r_unsafe_item);
  1606. ktime_get_coarse_real_ts64(&ts);
  1607. req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
  1608. req->r_op = op;
  1609. req->r_direct_mode = mode;
  1610. return req;
  1611. }
  1612. /*
  1613. * return oldest (lowest) request, tid in request tree, 0 if none.
  1614. *
  1615. * called under mdsc->mutex.
  1616. */
  1617. static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
  1618. {
  1619. if (RB_EMPTY_ROOT(&mdsc->request_tree))
  1620. return NULL;
  1621. return rb_entry(rb_first(&mdsc->request_tree),
  1622. struct ceph_mds_request, r_node);
  1623. }
  1624. static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
  1625. {
  1626. return mdsc->oldest_tid;
  1627. }
  1628. /*
  1629. * Build a dentry's path. Allocate on heap; caller must kfree. Based
  1630. * on build_path_from_dentry in fs/cifs/dir.c.
  1631. *
  1632. * If @stop_on_nosnap, generate path relative to the first non-snapped
  1633. * inode.
  1634. *
  1635. * Encode hidden .snap dirs as a double /, i.e.
  1636. * foo/.snap/bar -> foo//bar
  1637. */
  1638. char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
  1639. int stop_on_nosnap)
  1640. {
  1641. struct dentry *temp;
  1642. char *path;
  1643. int len, pos;
  1644. unsigned seq;
  1645. if (!dentry)
  1646. return ERR_PTR(-EINVAL);
  1647. retry:
  1648. len = 0;
  1649. seq = read_seqbegin(&rename_lock);
  1650. rcu_read_lock();
  1651. for (temp = dentry; !IS_ROOT(temp);) {
  1652. struct inode *inode = d_inode(temp);
  1653. if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
  1654. len++; /* slash only */
  1655. else if (stop_on_nosnap && inode &&
  1656. ceph_snap(inode) == CEPH_NOSNAP)
  1657. break;
  1658. else
  1659. len += 1 + temp->d_name.len;
  1660. temp = temp->d_parent;
  1661. }
  1662. rcu_read_unlock();
  1663. if (len)
  1664. len--; /* no leading '/' */
  1665. path = kmalloc(len+1, GFP_NOFS);
  1666. if (!path)
  1667. return ERR_PTR(-ENOMEM);
  1668. pos = len;
  1669. path[pos] = 0; /* trailing null */
  1670. rcu_read_lock();
  1671. for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
  1672. struct inode *inode;
  1673. spin_lock(&temp->d_lock);
  1674. inode = d_inode(temp);
  1675. if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
  1676. dout("build_path path+%d: %p SNAPDIR\n",
  1677. pos, temp);
  1678. } else if (stop_on_nosnap && inode &&
  1679. ceph_snap(inode) == CEPH_NOSNAP) {
  1680. spin_unlock(&temp->d_lock);
  1681. break;
  1682. } else {
  1683. pos -= temp->d_name.len;
  1684. if (pos < 0) {
  1685. spin_unlock(&temp->d_lock);
  1686. break;
  1687. }
  1688. strncpy(path + pos, temp->d_name.name,
  1689. temp->d_name.len);
  1690. }
  1691. spin_unlock(&temp->d_lock);
  1692. if (pos)
  1693. path[--pos] = '/';
  1694. temp = temp->d_parent;
  1695. }
  1696. rcu_read_unlock();
  1697. if (pos != 0 || read_seqretry(&rename_lock, seq)) {
  1698. pr_err("build_path did not end path lookup where "
  1699. "expected, namelen is %d, pos is %d\n", len, pos);
  1700. /* presumably this is only possible if racing with a
  1701. rename of one of the parent directories (we can not
  1702. lock the dentries above us to prevent this, but
  1703. retrying should be harmless) */
  1704. kfree(path);
  1705. goto retry;
  1706. }
  1707. *base = ceph_ino(d_inode(temp));
  1708. *plen = len;
  1709. dout("build_path on %p %d built %llx '%.*s'\n",
  1710. dentry, d_count(dentry), *base, len, path);
  1711. return path;
  1712. }
  1713. /* Duplicate the dentry->d_name.name safely */
  1714. static int clone_dentry_name(struct dentry *dentry, const char **ppath,
  1715. int *ppathlen)
  1716. {
  1717. u32 len;
  1718. char *name;
  1719. retry:
  1720. len = READ_ONCE(dentry->d_name.len);
  1721. name = kmalloc(len + 1, GFP_NOFS);
  1722. if (!name)
  1723. return -ENOMEM;
  1724. spin_lock(&dentry->d_lock);
  1725. if (dentry->d_name.len != len) {
  1726. spin_unlock(&dentry->d_lock);
  1727. kfree(name);
  1728. goto retry;
  1729. }
  1730. memcpy(name, dentry->d_name.name, len);
  1731. spin_unlock(&dentry->d_lock);
  1732. name[len] = '\0';
  1733. *ppath = name;
  1734. *ppathlen = len;
  1735. return 0;
  1736. }
  1737. static int build_dentry_path(struct dentry *dentry, struct inode *dir,
  1738. const char **ppath, int *ppathlen, u64 *pino,
  1739. bool *pfreepath, bool parent_locked)
  1740. {
  1741. int ret;
  1742. char *path;
  1743. rcu_read_lock();
  1744. if (!dir)
  1745. dir = d_inode_rcu(dentry->d_parent);
  1746. if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
  1747. *pino = ceph_ino(dir);
  1748. rcu_read_unlock();
  1749. if (parent_locked) {
  1750. *ppath = dentry->d_name.name;
  1751. *ppathlen = dentry->d_name.len;
  1752. } else {
  1753. ret = clone_dentry_name(dentry, ppath, ppathlen);
  1754. if (ret)
  1755. return ret;
  1756. *pfreepath = true;
  1757. }
  1758. return 0;
  1759. }
  1760. rcu_read_unlock();
  1761. path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
  1762. if (IS_ERR(path))
  1763. return PTR_ERR(path);
  1764. *ppath = path;
  1765. *pfreepath = true;
  1766. return 0;
  1767. }
  1768. static int build_inode_path(struct inode *inode,
  1769. const char **ppath, int *ppathlen, u64 *pino,
  1770. bool *pfreepath)
  1771. {
  1772. struct dentry *dentry;
  1773. char *path;
  1774. if (ceph_snap(inode) == CEPH_NOSNAP) {
  1775. *pino = ceph_ino(inode);
  1776. *ppathlen = 0;
  1777. return 0;
  1778. }
  1779. dentry = d_find_alias(inode);
  1780. path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
  1781. dput(dentry);
  1782. if (IS_ERR(path))
  1783. return PTR_ERR(path);
  1784. *ppath = path;
  1785. *pfreepath = true;
  1786. return 0;
  1787. }
  1788. /*
  1789. * request arguments may be specified via an inode *, a dentry *, or
  1790. * an explicit ino+path.
  1791. */
  1792. static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
  1793. struct inode *rdiri, const char *rpath,
  1794. u64 rino, const char **ppath, int *pathlen,
  1795. u64 *ino, bool *freepath, bool parent_locked)
  1796. {
  1797. int r = 0;
  1798. if (rinode) {
  1799. r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
  1800. dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
  1801. ceph_snap(rinode));
  1802. } else if (rdentry) {
  1803. r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
  1804. freepath, parent_locked);
  1805. dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
  1806. *ppath);
  1807. } else if (rpath || rino) {
  1808. *ino = rino;
  1809. *ppath = rpath;
  1810. *pathlen = rpath ? strlen(rpath) : 0;
  1811. dout(" path %.*s\n", *pathlen, rpath);
  1812. }
  1813. return r;
  1814. }
  1815. /*
  1816. * called under mdsc->mutex
  1817. */
  1818. static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
  1819. struct ceph_mds_request *req,
  1820. int mds, bool drop_cap_releases)
  1821. {
  1822. struct ceph_msg *msg;
  1823. struct ceph_mds_request_head *head;
  1824. const char *path1 = NULL;
  1825. const char *path2 = NULL;
  1826. u64 ino1 = 0, ino2 = 0;
  1827. int pathlen1 = 0, pathlen2 = 0;
  1828. bool freepath1 = false, freepath2 = false;
  1829. int len;
  1830. u16 releases;
  1831. void *p, *end;
  1832. int ret;
  1833. ret = set_request_path_attr(req->r_inode, req->r_dentry,
  1834. req->r_parent, req->r_path1, req->r_ino1.ino,
  1835. &path1, &pathlen1, &ino1, &freepath1,
  1836. test_bit(CEPH_MDS_R_PARENT_LOCKED,
  1837. &req->r_req_flags));
  1838. if (ret < 0) {
  1839. msg = ERR_PTR(ret);
  1840. goto out;
  1841. }
  1842. /* If r_old_dentry is set, then assume that its parent is locked */
  1843. ret = set_request_path_attr(NULL, req->r_old_dentry,
  1844. req->r_old_dentry_dir,
  1845. req->r_path2, req->r_ino2.ino,
  1846. &path2, &pathlen2, &ino2, &freepath2, true);
  1847. if (ret < 0) {
  1848. msg = ERR_PTR(ret);
  1849. goto out_free1;
  1850. }
  1851. len = sizeof(*head) +
  1852. pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
  1853. sizeof(struct ceph_timespec);
  1854. /* calculate (max) length for cap releases */
  1855. len += sizeof(struct ceph_mds_request_release) *
  1856. (!!req->r_inode_drop + !!req->r_dentry_drop +
  1857. !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
  1858. if (req->r_dentry_drop)
  1859. len += req->r_dentry->d_name.len;
  1860. if (req->r_old_dentry_drop)
  1861. len += req->r_old_dentry->d_name.len;
  1862. msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
  1863. if (!msg) {
  1864. msg = ERR_PTR(-ENOMEM);
  1865. goto out_free2;
  1866. }
  1867. msg->hdr.version = cpu_to_le16(2);
  1868. msg->hdr.tid = cpu_to_le64(req->r_tid);
  1869. head = msg->front.iov_base;
  1870. p = msg->front.iov_base + sizeof(*head);
  1871. end = msg->front.iov_base + msg->front.iov_len;
  1872. head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
  1873. head->op = cpu_to_le32(req->r_op);
  1874. head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
  1875. head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
  1876. head->args = req->r_args;
  1877. ceph_encode_filepath(&p, end, ino1, path1);
  1878. ceph_encode_filepath(&p, end, ino2, path2);
  1879. /* make note of release offset, in case we need to replay */
  1880. req->r_request_release_offset = p - msg->front.iov_base;
  1881. /* cap releases */
  1882. releases = 0;
  1883. if (req->r_inode_drop)
  1884. releases += ceph_encode_inode_release(&p,
  1885. req->r_inode ? req->r_inode : d_inode(req->r_dentry),
  1886. mds, req->r_inode_drop, req->r_inode_unless, 0);
  1887. if (req->r_dentry_drop)
  1888. releases += ceph_encode_dentry_release(&p, req->r_dentry,
  1889. req->r_parent, mds, req->r_dentry_drop,
  1890. req->r_dentry_unless);
  1891. if (req->r_old_dentry_drop)
  1892. releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
  1893. req->r_old_dentry_dir, mds,
  1894. req->r_old_dentry_drop,
  1895. req->r_old_dentry_unless);
  1896. if (req->r_old_inode_drop)
  1897. releases += ceph_encode_inode_release(&p,
  1898. d_inode(req->r_old_dentry),
  1899. mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
  1900. if (drop_cap_releases) {
  1901. releases = 0;
  1902. p = msg->front.iov_base + req->r_request_release_offset;
  1903. }
  1904. head->num_releases = cpu_to_le16(releases);
  1905. /* time stamp */
  1906. {
  1907. struct ceph_timespec ts;
  1908. ceph_encode_timespec64(&ts, &req->r_stamp);
  1909. ceph_encode_copy(&p, &ts, sizeof(ts));
  1910. }
  1911. BUG_ON(p > end);
  1912. msg->front.iov_len = p - msg->front.iov_base;
  1913. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1914. if (req->r_pagelist) {
  1915. struct ceph_pagelist *pagelist = req->r_pagelist;
  1916. refcount_inc(&pagelist->refcnt);
  1917. ceph_msg_data_add_pagelist(msg, pagelist);
  1918. msg->hdr.data_len = cpu_to_le32(pagelist->length);
  1919. } else {
  1920. msg->hdr.data_len = 0;
  1921. }
  1922. msg->hdr.data_off = cpu_to_le16(0);
  1923. out_free2:
  1924. if (freepath2)
  1925. kfree((char *)path2);
  1926. out_free1:
  1927. if (freepath1)
  1928. kfree((char *)path1);
  1929. out:
  1930. return msg;
  1931. }
  1932. /*
  1933. * called under mdsc->mutex if error, under no mutex if
  1934. * success.
  1935. */
  1936. static void complete_request(struct ceph_mds_client *mdsc,
  1937. struct ceph_mds_request *req)
  1938. {
  1939. if (req->r_callback)
  1940. req->r_callback(mdsc, req);
  1941. else
  1942. complete_all(&req->r_completion);
  1943. }
  1944. /*
  1945. * called under mdsc->mutex
  1946. */
  1947. static int __prepare_send_request(struct ceph_mds_client *mdsc,
  1948. struct ceph_mds_request *req,
  1949. int mds, bool drop_cap_releases)
  1950. {
  1951. struct ceph_mds_request_head *rhead;
  1952. struct ceph_msg *msg;
  1953. int flags = 0;
  1954. req->r_attempts++;
  1955. if (req->r_inode) {
  1956. struct ceph_cap *cap =
  1957. ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
  1958. if (cap)
  1959. req->r_sent_on_mseq = cap->mseq;
  1960. else
  1961. req->r_sent_on_mseq = -1;
  1962. }
  1963. dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
  1964. req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
  1965. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
  1966. void *p;
  1967. /*
  1968. * Replay. Do not regenerate message (and rebuild
  1969. * paths, etc.); just use the original message.
  1970. * Rebuilding paths will break for renames because
  1971. * d_move mangles the src name.
  1972. */
  1973. msg = req->r_request;
  1974. rhead = msg->front.iov_base;
  1975. flags = le32_to_cpu(rhead->flags);
  1976. flags |= CEPH_MDS_FLAG_REPLAY;
  1977. rhead->flags = cpu_to_le32(flags);
  1978. if (req->r_target_inode)
  1979. rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
  1980. rhead->num_retry = req->r_attempts - 1;
  1981. /* remove cap/dentry releases from message */
  1982. rhead->num_releases = 0;
  1983. /* time stamp */
  1984. p = msg->front.iov_base + req->r_request_release_offset;
  1985. {
  1986. struct ceph_timespec ts;
  1987. ceph_encode_timespec64(&ts, &req->r_stamp);
  1988. ceph_encode_copy(&p, &ts, sizeof(ts));
  1989. }
  1990. msg->front.iov_len = p - msg->front.iov_base;
  1991. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1992. return 0;
  1993. }
  1994. if (req->r_request) {
  1995. ceph_msg_put(req->r_request);
  1996. req->r_request = NULL;
  1997. }
  1998. msg = create_request_message(mdsc, req, mds, drop_cap_releases);
  1999. if (IS_ERR(msg)) {
  2000. req->r_err = PTR_ERR(msg);
  2001. return PTR_ERR(msg);
  2002. }
  2003. req->r_request = msg;
  2004. rhead = msg->front.iov_base;
  2005. rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
  2006. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
  2007. flags |= CEPH_MDS_FLAG_REPLAY;
  2008. if (req->r_parent)
  2009. flags |= CEPH_MDS_FLAG_WANT_DENTRY;
  2010. rhead->flags = cpu_to_le32(flags);
  2011. rhead->num_fwd = req->r_num_fwd;
  2012. rhead->num_retry = req->r_attempts - 1;
  2013. rhead->ino = 0;
  2014. dout(" r_parent = %p\n", req->r_parent);
  2015. return 0;
  2016. }
  2017. /*
  2018. * send request, or put it on the appropriate wait list.
  2019. */
  2020. static void __do_request(struct ceph_mds_client *mdsc,
  2021. struct ceph_mds_request *req)
  2022. {
  2023. struct ceph_mds_session *session = NULL;
  2024. int mds = -1;
  2025. int err = 0;
  2026. if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
  2027. if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
  2028. __unregister_request(mdsc, req);
  2029. return;
  2030. }
  2031. if (req->r_timeout &&
  2032. time_after_eq(jiffies, req->r_started + req->r_timeout)) {
  2033. dout("do_request timed out\n");
  2034. err = -EIO;
  2035. goto finish;
  2036. }
  2037. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  2038. dout("do_request forced umount\n");
  2039. err = -EIO;
  2040. goto finish;
  2041. }
  2042. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
  2043. if (mdsc->mdsmap_err) {
  2044. err = mdsc->mdsmap_err;
  2045. dout("do_request mdsmap err %d\n", err);
  2046. goto finish;
  2047. }
  2048. if (mdsc->mdsmap->m_epoch == 0) {
  2049. dout("do_request no mdsmap, waiting for map\n");
  2050. list_add(&req->r_wait, &mdsc->waiting_for_map);
  2051. return;
  2052. }
  2053. if (!(mdsc->fsc->mount_options->flags &
  2054. CEPH_MOUNT_OPT_MOUNTWAIT) &&
  2055. !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
  2056. err = -EHOSTUNREACH;
  2057. goto finish;
  2058. }
  2059. }
  2060. put_request_session(req);
  2061. mds = __choose_mds(mdsc, req);
  2062. if (mds < 0 ||
  2063. ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
  2064. dout("do_request no mds or not active, waiting for map\n");
  2065. list_add(&req->r_wait, &mdsc->waiting_for_map);
  2066. return;
  2067. }
  2068. /* get, open session */
  2069. session = __ceph_lookup_mds_session(mdsc, mds);
  2070. if (!session) {
  2071. session = register_session(mdsc, mds);
  2072. if (IS_ERR(session)) {
  2073. err = PTR_ERR(session);
  2074. goto finish;
  2075. }
  2076. }
  2077. req->r_session = get_session(session);
  2078. dout("do_request mds%d session %p state %s\n", mds, session,
  2079. ceph_session_state_name(session->s_state));
  2080. if (session->s_state != CEPH_MDS_SESSION_OPEN &&
  2081. session->s_state != CEPH_MDS_SESSION_HUNG) {
  2082. if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
  2083. err = -EACCES;
  2084. goto out_session;
  2085. }
  2086. if (session->s_state == CEPH_MDS_SESSION_NEW ||
  2087. session->s_state == CEPH_MDS_SESSION_CLOSING)
  2088. __open_session(mdsc, session);
  2089. list_add(&req->r_wait, &session->s_waiting);
  2090. goto out_session;
  2091. }
  2092. /* send request */
  2093. req->r_resend_mds = -1; /* forget any previous mds hint */
  2094. if (req->r_request_started == 0) /* note request start time */
  2095. req->r_request_started = jiffies;
  2096. err = __prepare_send_request(mdsc, req, mds, false);
  2097. if (!err) {
  2098. ceph_msg_get(req->r_request);
  2099. ceph_con_send(&session->s_con, req->r_request);
  2100. }
  2101. out_session:
  2102. ceph_put_mds_session(session);
  2103. finish:
  2104. if (err) {
  2105. dout("__do_request early error %d\n", err);
  2106. req->r_err = err;
  2107. complete_request(mdsc, req);
  2108. __unregister_request(mdsc, req);
  2109. }
  2110. return;
  2111. }
  2112. /*
  2113. * called under mdsc->mutex
  2114. */
  2115. static void __wake_requests(struct ceph_mds_client *mdsc,
  2116. struct list_head *head)
  2117. {
  2118. struct ceph_mds_request *req;
  2119. LIST_HEAD(tmp_list);
  2120. list_splice_init(head, &tmp_list);
  2121. while (!list_empty(&tmp_list)) {
  2122. req = list_entry(tmp_list.next,
  2123. struct ceph_mds_request, r_wait);
  2124. list_del_init(&req->r_wait);
  2125. dout(" wake request %p tid %llu\n", req, req->r_tid);
  2126. __do_request(mdsc, req);
  2127. }
  2128. }
  2129. /*
  2130. * Wake up threads with requests pending for @mds, so that they can
  2131. * resubmit their requests to a possibly different mds.
  2132. */
  2133. static void kick_requests(struct ceph_mds_client *mdsc, int mds)
  2134. {
  2135. struct ceph_mds_request *req;
  2136. struct rb_node *p = rb_first(&mdsc->request_tree);
  2137. dout("kick_requests mds%d\n", mds);
  2138. while (p) {
  2139. req = rb_entry(p, struct ceph_mds_request, r_node);
  2140. p = rb_next(p);
  2141. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
  2142. continue;
  2143. if (req->r_attempts > 0)
  2144. continue; /* only new requests */
  2145. if (req->r_session &&
  2146. req->r_session->s_mds == mds) {
  2147. dout(" kicking tid %llu\n", req->r_tid);
  2148. list_del_init(&req->r_wait);
  2149. __do_request(mdsc, req);
  2150. }
  2151. }
  2152. }
  2153. void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
  2154. struct ceph_mds_request *req)
  2155. {
  2156. dout("submit_request on %p\n", req);
  2157. mutex_lock(&mdsc->mutex);
  2158. __register_request(mdsc, req, NULL);
  2159. __do_request(mdsc, req);
  2160. mutex_unlock(&mdsc->mutex);
  2161. }
  2162. /*
  2163. * Synchrously perform an mds request. Take care of all of the
  2164. * session setup, forwarding, retry details.
  2165. */
  2166. int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
  2167. struct inode *dir,
  2168. struct ceph_mds_request *req)
  2169. {
  2170. int err;
  2171. dout("do_request on %p\n", req);
  2172. /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
  2173. if (req->r_inode)
  2174. ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
  2175. if (req->r_parent)
  2176. ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
  2177. if (req->r_old_dentry_dir)
  2178. ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
  2179. CEPH_CAP_PIN);
  2180. /* issue */
  2181. mutex_lock(&mdsc->mutex);
  2182. __register_request(mdsc, req, dir);
  2183. __do_request(mdsc, req);
  2184. if (req->r_err) {
  2185. err = req->r_err;
  2186. goto out;
  2187. }
  2188. /* wait */
  2189. mutex_unlock(&mdsc->mutex);
  2190. dout("do_request waiting\n");
  2191. if (!req->r_timeout && req->r_wait_for_completion) {
  2192. err = req->r_wait_for_completion(mdsc, req);
  2193. } else {
  2194. long timeleft = wait_for_completion_killable_timeout(
  2195. &req->r_completion,
  2196. ceph_timeout_jiffies(req->r_timeout));
  2197. if (timeleft > 0)
  2198. err = 0;
  2199. else if (!timeleft)
  2200. err = -EIO; /* timed out */
  2201. else
  2202. err = timeleft; /* killed */
  2203. }
  2204. dout("do_request waited, got %d\n", err);
  2205. mutex_lock(&mdsc->mutex);
  2206. /* only abort if we didn't race with a real reply */
  2207. if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
  2208. err = le32_to_cpu(req->r_reply_info.head->result);
  2209. } else if (err < 0) {
  2210. dout("aborted request %lld with %d\n", req->r_tid, err);
  2211. /*
  2212. * ensure we aren't running concurrently with
  2213. * ceph_fill_trace or ceph_readdir_prepopulate, which
  2214. * rely on locks (dir mutex) held by our caller.
  2215. */
  2216. mutex_lock(&req->r_fill_mutex);
  2217. req->r_err = err;
  2218. set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
  2219. mutex_unlock(&req->r_fill_mutex);
  2220. if (req->r_parent &&
  2221. (req->r_op & CEPH_MDS_OP_WRITE))
  2222. ceph_invalidate_dir_request(req);
  2223. } else {
  2224. err = req->r_err;
  2225. }
  2226. out:
  2227. mutex_unlock(&mdsc->mutex);
  2228. dout("do_request %p done, result %d\n", req, err);
  2229. return err;
  2230. }
  2231. /*
  2232. * Invalidate dir's completeness, dentry lease state on an aborted MDS
  2233. * namespace request.
  2234. */
  2235. void ceph_invalidate_dir_request(struct ceph_mds_request *req)
  2236. {
  2237. struct inode *dir = req->r_parent;
  2238. struct inode *old_dir = req->r_old_dentry_dir;
  2239. dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
  2240. ceph_dir_clear_complete(dir);
  2241. if (old_dir)
  2242. ceph_dir_clear_complete(old_dir);
  2243. if (req->r_dentry)
  2244. ceph_invalidate_dentry_lease(req->r_dentry);
  2245. if (req->r_old_dentry)
  2246. ceph_invalidate_dentry_lease(req->r_old_dentry);
  2247. }
  2248. /*
  2249. * Handle mds reply.
  2250. *
  2251. * We take the session mutex and parse and process the reply immediately.
  2252. * This preserves the logical ordering of replies, capabilities, etc., sent
  2253. * by the MDS as they are applied to our local cache.
  2254. */
  2255. static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
  2256. {
  2257. struct ceph_mds_client *mdsc = session->s_mdsc;
  2258. struct ceph_mds_request *req;
  2259. struct ceph_mds_reply_head *head = msg->front.iov_base;
  2260. struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
  2261. struct ceph_snap_realm *realm;
  2262. u64 tid;
  2263. int err, result;
  2264. int mds = session->s_mds;
  2265. if (msg->front.iov_len < sizeof(*head)) {
  2266. pr_err("mdsc_handle_reply got corrupt (short) reply\n");
  2267. ceph_msg_dump(msg);
  2268. return;
  2269. }
  2270. /* get request, session */
  2271. tid = le64_to_cpu(msg->hdr.tid);
  2272. mutex_lock(&mdsc->mutex);
  2273. req = lookup_get_request(mdsc, tid);
  2274. if (!req) {
  2275. dout("handle_reply on unknown tid %llu\n", tid);
  2276. mutex_unlock(&mdsc->mutex);
  2277. return;
  2278. }
  2279. dout("handle_reply %p\n", req);
  2280. /* correct session? */
  2281. if (req->r_session != session) {
  2282. pr_err("mdsc_handle_reply got %llu on session mds%d"
  2283. " not mds%d\n", tid, session->s_mds,
  2284. req->r_session ? req->r_session->s_mds : -1);
  2285. mutex_unlock(&mdsc->mutex);
  2286. goto out;
  2287. }
  2288. /* dup? */
  2289. if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
  2290. (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
  2291. pr_warn("got a dup %s reply on %llu from mds%d\n",
  2292. head->safe ? "safe" : "unsafe", tid, mds);
  2293. mutex_unlock(&mdsc->mutex);
  2294. goto out;
  2295. }
  2296. if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
  2297. pr_warn("got unsafe after safe on %llu from mds%d\n",
  2298. tid, mds);
  2299. mutex_unlock(&mdsc->mutex);
  2300. goto out;
  2301. }
  2302. result = le32_to_cpu(head->result);
  2303. /*
  2304. * Handle an ESTALE
  2305. * if we're not talking to the authority, send to them
  2306. * if the authority has changed while we weren't looking,
  2307. * send to new authority
  2308. * Otherwise we just have to return an ESTALE
  2309. */
  2310. if (result == -ESTALE) {
  2311. dout("got ESTALE on request %llu\n", req->r_tid);
  2312. req->r_resend_mds = -1;
  2313. if (req->r_direct_mode != USE_AUTH_MDS) {
  2314. dout("not using auth, setting for that now\n");
  2315. req->r_direct_mode = USE_AUTH_MDS;
  2316. __do_request(mdsc, req);
  2317. mutex_unlock(&mdsc->mutex);
  2318. goto out;
  2319. } else {
  2320. int mds = __choose_mds(mdsc, req);
  2321. if (mds >= 0 && mds != req->r_session->s_mds) {
  2322. dout("but auth changed, so resending\n");
  2323. __do_request(mdsc, req);
  2324. mutex_unlock(&mdsc->mutex);
  2325. goto out;
  2326. }
  2327. }
  2328. dout("have to return ESTALE on request %llu\n", req->r_tid);
  2329. }
  2330. if (head->safe) {
  2331. set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
  2332. __unregister_request(mdsc, req);
  2333. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
  2334. /*
  2335. * We already handled the unsafe response, now do the
  2336. * cleanup. No need to examine the response; the MDS
  2337. * doesn't include any result info in the safe
  2338. * response. And even if it did, there is nothing
  2339. * useful we could do with a revised return value.
  2340. */
  2341. dout("got safe reply %llu, mds%d\n", tid, mds);
  2342. /* last unsafe request during umount? */
  2343. if (mdsc->stopping && !__get_oldest_req(mdsc))
  2344. complete_all(&mdsc->safe_umount_waiters);
  2345. mutex_unlock(&mdsc->mutex);
  2346. goto out;
  2347. }
  2348. } else {
  2349. set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
  2350. list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
  2351. if (req->r_unsafe_dir) {
  2352. struct ceph_inode_info *ci =
  2353. ceph_inode(req->r_unsafe_dir);
  2354. spin_lock(&ci->i_unsafe_lock);
  2355. list_add_tail(&req->r_unsafe_dir_item,
  2356. &ci->i_unsafe_dirops);
  2357. spin_unlock(&ci->i_unsafe_lock);
  2358. }
  2359. }
  2360. dout("handle_reply tid %lld result %d\n", tid, result);
  2361. rinfo = &req->r_reply_info;
  2362. err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
  2363. mutex_unlock(&mdsc->mutex);
  2364. mutex_lock(&session->s_mutex);
  2365. if (err < 0) {
  2366. pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
  2367. ceph_msg_dump(msg);
  2368. goto out_err;
  2369. }
  2370. /* snap trace */
  2371. realm = NULL;
  2372. if (rinfo->snapblob_len) {
  2373. down_write(&mdsc->snap_rwsem);
  2374. ceph_update_snap_trace(mdsc, rinfo->snapblob,
  2375. rinfo->snapblob + rinfo->snapblob_len,
  2376. le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
  2377. &realm);
  2378. downgrade_write(&mdsc->snap_rwsem);
  2379. } else {
  2380. down_read(&mdsc->snap_rwsem);
  2381. }
  2382. /* insert trace into our cache */
  2383. mutex_lock(&req->r_fill_mutex);
  2384. current->journal_info = req;
  2385. err = ceph_fill_trace(mdsc->fsc->sb, req);
  2386. if (err == 0) {
  2387. if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
  2388. req->r_op == CEPH_MDS_OP_LSSNAP))
  2389. ceph_readdir_prepopulate(req, req->r_session);
  2390. ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
  2391. }
  2392. current->journal_info = NULL;
  2393. mutex_unlock(&req->r_fill_mutex);
  2394. up_read(&mdsc->snap_rwsem);
  2395. if (realm)
  2396. ceph_put_snap_realm(mdsc, realm);
  2397. if (err == 0 && req->r_target_inode &&
  2398. test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
  2399. struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
  2400. spin_lock(&ci->i_unsafe_lock);
  2401. list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
  2402. spin_unlock(&ci->i_unsafe_lock);
  2403. }
  2404. out_err:
  2405. mutex_lock(&mdsc->mutex);
  2406. if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
  2407. if (err) {
  2408. req->r_err = err;
  2409. } else {
  2410. req->r_reply = ceph_msg_get(msg);
  2411. set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
  2412. }
  2413. } else {
  2414. dout("reply arrived after request %lld was aborted\n", tid);
  2415. }
  2416. mutex_unlock(&mdsc->mutex);
  2417. mutex_unlock(&session->s_mutex);
  2418. /* kick calling process */
  2419. complete_request(mdsc, req);
  2420. out:
  2421. ceph_mdsc_put_request(req);
  2422. return;
  2423. }
  2424. /*
  2425. * handle mds notification that our request has been forwarded.
  2426. */
  2427. static void handle_forward(struct ceph_mds_client *mdsc,
  2428. struct ceph_mds_session *session,
  2429. struct ceph_msg *msg)
  2430. {
  2431. struct ceph_mds_request *req;
  2432. u64 tid = le64_to_cpu(msg->hdr.tid);
  2433. u32 next_mds;
  2434. u32 fwd_seq;
  2435. int err = -EINVAL;
  2436. void *p = msg->front.iov_base;
  2437. void *end = p + msg->front.iov_len;
  2438. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  2439. next_mds = ceph_decode_32(&p);
  2440. fwd_seq = ceph_decode_32(&p);
  2441. mutex_lock(&mdsc->mutex);
  2442. req = lookup_get_request(mdsc, tid);
  2443. if (!req) {
  2444. dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
  2445. goto out; /* dup reply? */
  2446. }
  2447. if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
  2448. dout("forward tid %llu aborted, unregistering\n", tid);
  2449. __unregister_request(mdsc, req);
  2450. } else if (fwd_seq <= req->r_num_fwd) {
  2451. dout("forward tid %llu to mds%d - old seq %d <= %d\n",
  2452. tid, next_mds, req->r_num_fwd, fwd_seq);
  2453. } else {
  2454. /* resend. forward race not possible; mds would drop */
  2455. dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
  2456. BUG_ON(req->r_err);
  2457. BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
  2458. req->r_attempts = 0;
  2459. req->r_num_fwd = fwd_seq;
  2460. req->r_resend_mds = next_mds;
  2461. put_request_session(req);
  2462. __do_request(mdsc, req);
  2463. }
  2464. ceph_mdsc_put_request(req);
  2465. out:
  2466. mutex_unlock(&mdsc->mutex);
  2467. return;
  2468. bad:
  2469. pr_err("mdsc_handle_forward decode error err=%d\n", err);
  2470. }
  2471. /*
  2472. * handle a mds session control message
  2473. */
  2474. static void handle_session(struct ceph_mds_session *session,
  2475. struct ceph_msg *msg)
  2476. {
  2477. struct ceph_mds_client *mdsc = session->s_mdsc;
  2478. u32 op;
  2479. u64 seq;
  2480. int mds = session->s_mds;
  2481. struct ceph_mds_session_head *h = msg->front.iov_base;
  2482. int wake = 0;
  2483. /* decode */
  2484. if (msg->front.iov_len < sizeof(*h))
  2485. goto bad;
  2486. op = le32_to_cpu(h->op);
  2487. seq = le64_to_cpu(h->seq);
  2488. mutex_lock(&mdsc->mutex);
  2489. if (op == CEPH_SESSION_CLOSE) {
  2490. get_session(session);
  2491. __unregister_session(mdsc, session);
  2492. }
  2493. /* FIXME: this ttl calculation is generous */
  2494. session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
  2495. mutex_unlock(&mdsc->mutex);
  2496. mutex_lock(&session->s_mutex);
  2497. dout("handle_session mds%d %s %p state %s seq %llu\n",
  2498. mds, ceph_session_op_name(op), session,
  2499. ceph_session_state_name(session->s_state), seq);
  2500. if (session->s_state == CEPH_MDS_SESSION_HUNG) {
  2501. session->s_state = CEPH_MDS_SESSION_OPEN;
  2502. pr_info("mds%d came back\n", session->s_mds);
  2503. }
  2504. switch (op) {
  2505. case CEPH_SESSION_OPEN:
  2506. if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
  2507. pr_info("mds%d reconnect success\n", session->s_mds);
  2508. session->s_state = CEPH_MDS_SESSION_OPEN;
  2509. renewed_caps(mdsc, session, 0);
  2510. wake = 1;
  2511. if (mdsc->stopping)
  2512. __close_session(mdsc, session);
  2513. break;
  2514. case CEPH_SESSION_RENEWCAPS:
  2515. if (session->s_renew_seq == seq)
  2516. renewed_caps(mdsc, session, 1);
  2517. break;
  2518. case CEPH_SESSION_CLOSE:
  2519. if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
  2520. pr_info("mds%d reconnect denied\n", session->s_mds);
  2521. cleanup_session_requests(mdsc, session);
  2522. remove_session_caps(session);
  2523. wake = 2; /* for good measure */
  2524. wake_up_all(&mdsc->session_close_wq);
  2525. break;
  2526. case CEPH_SESSION_STALE:
  2527. pr_info("mds%d caps went stale, renewing\n",
  2528. session->s_mds);
  2529. spin_lock(&session->s_gen_ttl_lock);
  2530. session->s_cap_gen++;
  2531. session->s_cap_ttl = jiffies - 1;
  2532. spin_unlock(&session->s_gen_ttl_lock);
  2533. send_renew_caps(mdsc, session);
  2534. break;
  2535. case CEPH_SESSION_RECALL_STATE:
  2536. ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
  2537. break;
  2538. case CEPH_SESSION_FLUSHMSG:
  2539. send_flushmsg_ack(mdsc, session, seq);
  2540. break;
  2541. case CEPH_SESSION_FORCE_RO:
  2542. dout("force_session_readonly %p\n", session);
  2543. spin_lock(&session->s_cap_lock);
  2544. session->s_readonly = true;
  2545. spin_unlock(&session->s_cap_lock);
  2546. wake_up_session_caps(session, 0);
  2547. break;
  2548. case CEPH_SESSION_REJECT:
  2549. WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
  2550. pr_info("mds%d rejected session\n", session->s_mds);
  2551. session->s_state = CEPH_MDS_SESSION_REJECTED;
  2552. cleanup_session_requests(mdsc, session);
  2553. remove_session_caps(session);
  2554. wake = 2; /* for good measure */
  2555. break;
  2556. default:
  2557. pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
  2558. WARN_ON(1);
  2559. }
  2560. mutex_unlock(&session->s_mutex);
  2561. if (wake) {
  2562. mutex_lock(&mdsc->mutex);
  2563. __wake_requests(mdsc, &session->s_waiting);
  2564. if (wake == 2)
  2565. kick_requests(mdsc, mds);
  2566. mutex_unlock(&mdsc->mutex);
  2567. }
  2568. if (op == CEPH_SESSION_CLOSE)
  2569. ceph_put_mds_session(session);
  2570. return;
  2571. bad:
  2572. pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
  2573. (int)msg->front.iov_len);
  2574. ceph_msg_dump(msg);
  2575. return;
  2576. }
  2577. /*
  2578. * called under session->mutex.
  2579. */
  2580. static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
  2581. struct ceph_mds_session *session)
  2582. {
  2583. struct ceph_mds_request *req, *nreq;
  2584. struct rb_node *p;
  2585. int err;
  2586. dout("replay_unsafe_requests mds%d\n", session->s_mds);
  2587. mutex_lock(&mdsc->mutex);
  2588. list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
  2589. err = __prepare_send_request(mdsc, req, session->s_mds, true);
  2590. if (!err) {
  2591. ceph_msg_get(req->r_request);
  2592. ceph_con_send(&session->s_con, req->r_request);
  2593. }
  2594. }
  2595. /*
  2596. * also re-send old requests when MDS enters reconnect stage. So that MDS
  2597. * can process completed request in clientreplay stage.
  2598. */
  2599. p = rb_first(&mdsc->request_tree);
  2600. while (p) {
  2601. req = rb_entry(p, struct ceph_mds_request, r_node);
  2602. p = rb_next(p);
  2603. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
  2604. continue;
  2605. if (req->r_attempts == 0)
  2606. continue; /* only old requests */
  2607. if (req->r_session &&
  2608. req->r_session->s_mds == session->s_mds) {
  2609. err = __prepare_send_request(mdsc, req,
  2610. session->s_mds, true);
  2611. if (!err) {
  2612. ceph_msg_get(req->r_request);
  2613. ceph_con_send(&session->s_con, req->r_request);
  2614. }
  2615. }
  2616. }
  2617. mutex_unlock(&mdsc->mutex);
  2618. }
  2619. /*
  2620. * Encode information about a cap for a reconnect with the MDS.
  2621. */
  2622. static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
  2623. void *arg)
  2624. {
  2625. union {
  2626. struct ceph_mds_cap_reconnect v2;
  2627. struct ceph_mds_cap_reconnect_v1 v1;
  2628. } rec;
  2629. struct ceph_inode_info *ci = cap->ci;
  2630. struct ceph_reconnect_state *recon_state = arg;
  2631. struct ceph_pagelist *pagelist = recon_state->pagelist;
  2632. char *path;
  2633. int pathlen, err;
  2634. u64 pathbase;
  2635. u64 snap_follows;
  2636. struct dentry *dentry;
  2637. dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
  2638. inode, ceph_vinop(inode), cap, cap->cap_id,
  2639. ceph_cap_string(cap->issued));
  2640. err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
  2641. if (err)
  2642. return err;
  2643. dentry = d_find_alias(inode);
  2644. if (dentry) {
  2645. path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
  2646. if (IS_ERR(path)) {
  2647. err = PTR_ERR(path);
  2648. goto out_dput;
  2649. }
  2650. } else {
  2651. path = NULL;
  2652. pathlen = 0;
  2653. pathbase = 0;
  2654. }
  2655. spin_lock(&ci->i_ceph_lock);
  2656. cap->seq = 0; /* reset cap seq */
  2657. cap->issue_seq = 0; /* and issue_seq */
  2658. cap->mseq = 0; /* and migrate_seq */
  2659. cap->cap_gen = cap->session->s_cap_gen;
  2660. if (recon_state->msg_version >= 2) {
  2661. rec.v2.cap_id = cpu_to_le64(cap->cap_id);
  2662. rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
  2663. rec.v2.issued = cpu_to_le32(cap->issued);
  2664. rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
  2665. rec.v2.pathbase = cpu_to_le64(pathbase);
  2666. rec.v2.flock_len = (__force __le32)
  2667. ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
  2668. } else {
  2669. rec.v1.cap_id = cpu_to_le64(cap->cap_id);
  2670. rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
  2671. rec.v1.issued = cpu_to_le32(cap->issued);
  2672. rec.v1.size = cpu_to_le64(inode->i_size);
  2673. ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
  2674. ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
  2675. rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
  2676. rec.v1.pathbase = cpu_to_le64(pathbase);
  2677. }
  2678. if (list_empty(&ci->i_cap_snaps)) {
  2679. snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
  2680. } else {
  2681. struct ceph_cap_snap *capsnap =
  2682. list_first_entry(&ci->i_cap_snaps,
  2683. struct ceph_cap_snap, ci_item);
  2684. snap_follows = capsnap->follows;
  2685. }
  2686. spin_unlock(&ci->i_ceph_lock);
  2687. if (recon_state->msg_version >= 2) {
  2688. int num_fcntl_locks, num_flock_locks;
  2689. struct ceph_filelock *flocks = NULL;
  2690. size_t struct_len, total_len = 0;
  2691. u8 struct_v = 0;
  2692. encode_again:
  2693. if (rec.v2.flock_len) {
  2694. ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
  2695. } else {
  2696. num_fcntl_locks = 0;
  2697. num_flock_locks = 0;
  2698. }
  2699. if (num_fcntl_locks + num_flock_locks > 0) {
  2700. flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
  2701. sizeof(struct ceph_filelock),
  2702. GFP_NOFS);
  2703. if (!flocks) {
  2704. err = -ENOMEM;
  2705. goto out_free;
  2706. }
  2707. err = ceph_encode_locks_to_buffer(inode, flocks,
  2708. num_fcntl_locks,
  2709. num_flock_locks);
  2710. if (err) {
  2711. kfree(flocks);
  2712. flocks = NULL;
  2713. if (err == -ENOSPC)
  2714. goto encode_again;
  2715. goto out_free;
  2716. }
  2717. } else {
  2718. kfree(flocks);
  2719. flocks = NULL;
  2720. }
  2721. if (recon_state->msg_version >= 3) {
  2722. /* version, compat_version and struct_len */
  2723. total_len = 2 * sizeof(u8) + sizeof(u32);
  2724. struct_v = 2;
  2725. }
  2726. /*
  2727. * number of encoded locks is stable, so copy to pagelist
  2728. */
  2729. struct_len = 2 * sizeof(u32) +
  2730. (num_fcntl_locks + num_flock_locks) *
  2731. sizeof(struct ceph_filelock);
  2732. rec.v2.flock_len = cpu_to_le32(struct_len);
  2733. struct_len += sizeof(rec.v2);
  2734. struct_len += sizeof(u32) + pathlen;
  2735. if (struct_v >= 2)
  2736. struct_len += sizeof(u64); /* snap_follows */
  2737. total_len += struct_len;
  2738. err = ceph_pagelist_reserve(pagelist, total_len);
  2739. if (!err) {
  2740. if (recon_state->msg_version >= 3) {
  2741. ceph_pagelist_encode_8(pagelist, struct_v);
  2742. ceph_pagelist_encode_8(pagelist, 1);
  2743. ceph_pagelist_encode_32(pagelist, struct_len);
  2744. }
  2745. ceph_pagelist_encode_string(pagelist, path, pathlen);
  2746. ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
  2747. ceph_locks_to_pagelist(flocks, pagelist,
  2748. num_fcntl_locks,
  2749. num_flock_locks);
  2750. if (struct_v >= 2)
  2751. ceph_pagelist_encode_64(pagelist, snap_follows);
  2752. }
  2753. kfree(flocks);
  2754. } else {
  2755. size_t size = sizeof(u32) + pathlen + sizeof(rec.v1);
  2756. err = ceph_pagelist_reserve(pagelist, size);
  2757. if (!err) {
  2758. ceph_pagelist_encode_string(pagelist, path, pathlen);
  2759. ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
  2760. }
  2761. }
  2762. recon_state->nr_caps++;
  2763. out_free:
  2764. kfree(path);
  2765. out_dput:
  2766. dput(dentry);
  2767. return err;
  2768. }
  2769. /*
  2770. * If an MDS fails and recovers, clients need to reconnect in order to
  2771. * reestablish shared state. This includes all caps issued through
  2772. * this session _and_ the snap_realm hierarchy. Because it's not
  2773. * clear which snap realms the mds cares about, we send everything we
  2774. * know about.. that ensures we'll then get any new info the
  2775. * recovering MDS might have.
  2776. *
  2777. * This is a relatively heavyweight operation, but it's rare.
  2778. *
  2779. * called with mdsc->mutex held.
  2780. */
  2781. static void send_mds_reconnect(struct ceph_mds_client *mdsc,
  2782. struct ceph_mds_session *session)
  2783. {
  2784. struct ceph_msg *reply;
  2785. struct rb_node *p;
  2786. int mds = session->s_mds;
  2787. int err = -ENOMEM;
  2788. int s_nr_caps;
  2789. struct ceph_pagelist *pagelist;
  2790. struct ceph_reconnect_state recon_state;
  2791. LIST_HEAD(dispose);
  2792. pr_info("mds%d reconnect start\n", mds);
  2793. pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
  2794. if (!pagelist)
  2795. goto fail_nopagelist;
  2796. ceph_pagelist_init(pagelist);
  2797. reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
  2798. if (!reply)
  2799. goto fail_nomsg;
  2800. mutex_lock(&session->s_mutex);
  2801. session->s_state = CEPH_MDS_SESSION_RECONNECTING;
  2802. session->s_seq = 0;
  2803. dout("session %p state %s\n", session,
  2804. ceph_session_state_name(session->s_state));
  2805. spin_lock(&session->s_gen_ttl_lock);
  2806. session->s_cap_gen++;
  2807. spin_unlock(&session->s_gen_ttl_lock);
  2808. spin_lock(&session->s_cap_lock);
  2809. /* don't know if session is readonly */
  2810. session->s_readonly = 0;
  2811. /*
  2812. * notify __ceph_remove_cap() that we are composing cap reconnect.
  2813. * If a cap get released before being added to the cap reconnect,
  2814. * __ceph_remove_cap() should skip queuing cap release.
  2815. */
  2816. session->s_cap_reconnect = 1;
  2817. /* drop old cap expires; we're about to reestablish that state */
  2818. detach_cap_releases(session, &dispose);
  2819. spin_unlock(&session->s_cap_lock);
  2820. dispose_cap_releases(mdsc, &dispose);
  2821. /* trim unused caps to reduce MDS's cache rejoin time */
  2822. if (mdsc->fsc->sb->s_root)
  2823. shrink_dcache_parent(mdsc->fsc->sb->s_root);
  2824. ceph_con_close(&session->s_con);
  2825. ceph_con_open(&session->s_con,
  2826. CEPH_ENTITY_TYPE_MDS, mds,
  2827. ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
  2828. /* replay unsafe requests */
  2829. replay_unsafe_requests(mdsc, session);
  2830. down_read(&mdsc->snap_rwsem);
  2831. /* traverse this session's caps */
  2832. s_nr_caps = session->s_nr_caps;
  2833. err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
  2834. if (err)
  2835. goto fail;
  2836. recon_state.nr_caps = 0;
  2837. recon_state.pagelist = pagelist;
  2838. if (session->s_con.peer_features & CEPH_FEATURE_MDSENC)
  2839. recon_state.msg_version = 3;
  2840. else if (session->s_con.peer_features & CEPH_FEATURE_FLOCK)
  2841. recon_state.msg_version = 2;
  2842. else
  2843. recon_state.msg_version = 1;
  2844. err = iterate_session_caps(session, encode_caps_cb, &recon_state);
  2845. if (err < 0)
  2846. goto fail;
  2847. spin_lock(&session->s_cap_lock);
  2848. session->s_cap_reconnect = 0;
  2849. spin_unlock(&session->s_cap_lock);
  2850. /*
  2851. * snaprealms. we provide mds with the ino, seq (version), and
  2852. * parent for all of our realms. If the mds has any newer info,
  2853. * it will tell us.
  2854. */
  2855. for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
  2856. struct ceph_snap_realm *realm =
  2857. rb_entry(p, struct ceph_snap_realm, node);
  2858. struct ceph_mds_snaprealm_reconnect sr_rec;
  2859. dout(" adding snap realm %llx seq %lld parent %llx\n",
  2860. realm->ino, realm->seq, realm->parent_ino);
  2861. sr_rec.ino = cpu_to_le64(realm->ino);
  2862. sr_rec.seq = cpu_to_le64(realm->seq);
  2863. sr_rec.parent = cpu_to_le64(realm->parent_ino);
  2864. err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
  2865. if (err)
  2866. goto fail;
  2867. }
  2868. reply->hdr.version = cpu_to_le16(recon_state.msg_version);
  2869. /* raced with cap release? */
  2870. if (s_nr_caps != recon_state.nr_caps) {
  2871. struct page *page = list_first_entry(&pagelist->head,
  2872. struct page, lru);
  2873. __le32 *addr = kmap_atomic(page);
  2874. *addr = cpu_to_le32(recon_state.nr_caps);
  2875. kunmap_atomic(addr);
  2876. }
  2877. reply->hdr.data_len = cpu_to_le32(pagelist->length);
  2878. ceph_msg_data_add_pagelist(reply, pagelist);
  2879. ceph_early_kick_flushing_caps(mdsc, session);
  2880. ceph_con_send(&session->s_con, reply);
  2881. mutex_unlock(&session->s_mutex);
  2882. mutex_lock(&mdsc->mutex);
  2883. __wake_requests(mdsc, &session->s_waiting);
  2884. mutex_unlock(&mdsc->mutex);
  2885. up_read(&mdsc->snap_rwsem);
  2886. return;
  2887. fail:
  2888. ceph_msg_put(reply);
  2889. up_read(&mdsc->snap_rwsem);
  2890. mutex_unlock(&session->s_mutex);
  2891. fail_nomsg:
  2892. ceph_pagelist_release(pagelist);
  2893. fail_nopagelist:
  2894. pr_err("error %d preparing reconnect for mds%d\n", err, mds);
  2895. return;
  2896. }
  2897. /*
  2898. * compare old and new mdsmaps, kicking requests
  2899. * and closing out old connections as necessary
  2900. *
  2901. * called under mdsc->mutex.
  2902. */
  2903. static void check_new_map(struct ceph_mds_client *mdsc,
  2904. struct ceph_mdsmap *newmap,
  2905. struct ceph_mdsmap *oldmap)
  2906. {
  2907. int i;
  2908. int oldstate, newstate;
  2909. struct ceph_mds_session *s;
  2910. dout("check_new_map new %u old %u\n",
  2911. newmap->m_epoch, oldmap->m_epoch);
  2912. for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
  2913. if (!mdsc->sessions[i])
  2914. continue;
  2915. s = mdsc->sessions[i];
  2916. oldstate = ceph_mdsmap_get_state(oldmap, i);
  2917. newstate = ceph_mdsmap_get_state(newmap, i);
  2918. dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
  2919. i, ceph_mds_state_name(oldstate),
  2920. ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
  2921. ceph_mds_state_name(newstate),
  2922. ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
  2923. ceph_session_state_name(s->s_state));
  2924. if (i >= newmap->m_num_mds ||
  2925. memcmp(ceph_mdsmap_get_addr(oldmap, i),
  2926. ceph_mdsmap_get_addr(newmap, i),
  2927. sizeof(struct ceph_entity_addr))) {
  2928. if (s->s_state == CEPH_MDS_SESSION_OPENING) {
  2929. /* the session never opened, just close it
  2930. * out now */
  2931. get_session(s);
  2932. __unregister_session(mdsc, s);
  2933. __wake_requests(mdsc, &s->s_waiting);
  2934. ceph_put_mds_session(s);
  2935. } else if (i >= newmap->m_num_mds) {
  2936. /* force close session for stopped mds */
  2937. get_session(s);
  2938. __unregister_session(mdsc, s);
  2939. __wake_requests(mdsc, &s->s_waiting);
  2940. kick_requests(mdsc, i);
  2941. mutex_unlock(&mdsc->mutex);
  2942. mutex_lock(&s->s_mutex);
  2943. cleanup_session_requests(mdsc, s);
  2944. remove_session_caps(s);
  2945. mutex_unlock(&s->s_mutex);
  2946. ceph_put_mds_session(s);
  2947. mutex_lock(&mdsc->mutex);
  2948. } else {
  2949. /* just close it */
  2950. mutex_unlock(&mdsc->mutex);
  2951. mutex_lock(&s->s_mutex);
  2952. mutex_lock(&mdsc->mutex);
  2953. ceph_con_close(&s->s_con);
  2954. mutex_unlock(&s->s_mutex);
  2955. s->s_state = CEPH_MDS_SESSION_RESTARTING;
  2956. }
  2957. } else if (oldstate == newstate) {
  2958. continue; /* nothing new with this mds */
  2959. }
  2960. /*
  2961. * send reconnect?
  2962. */
  2963. if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
  2964. newstate >= CEPH_MDS_STATE_RECONNECT) {
  2965. mutex_unlock(&mdsc->mutex);
  2966. send_mds_reconnect(mdsc, s);
  2967. mutex_lock(&mdsc->mutex);
  2968. }
  2969. /*
  2970. * kick request on any mds that has gone active.
  2971. */
  2972. if (oldstate < CEPH_MDS_STATE_ACTIVE &&
  2973. newstate >= CEPH_MDS_STATE_ACTIVE) {
  2974. if (oldstate != CEPH_MDS_STATE_CREATING &&
  2975. oldstate != CEPH_MDS_STATE_STARTING)
  2976. pr_info("mds%d recovery completed\n", s->s_mds);
  2977. kick_requests(mdsc, i);
  2978. ceph_kick_flushing_caps(mdsc, s);
  2979. wake_up_session_caps(s, 1);
  2980. }
  2981. }
  2982. for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
  2983. s = mdsc->sessions[i];
  2984. if (!s)
  2985. continue;
  2986. if (!ceph_mdsmap_is_laggy(newmap, i))
  2987. continue;
  2988. if (s->s_state == CEPH_MDS_SESSION_OPEN ||
  2989. s->s_state == CEPH_MDS_SESSION_HUNG ||
  2990. s->s_state == CEPH_MDS_SESSION_CLOSING) {
  2991. dout(" connecting to export targets of laggy mds%d\n",
  2992. i);
  2993. __open_export_target_sessions(mdsc, s);
  2994. }
  2995. }
  2996. }
  2997. /*
  2998. * leases
  2999. */
  3000. /*
  3001. * caller must hold session s_mutex, dentry->d_lock
  3002. */
  3003. void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
  3004. {
  3005. struct ceph_dentry_info *di = ceph_dentry(dentry);
  3006. ceph_put_mds_session(di->lease_session);
  3007. di->lease_session = NULL;
  3008. }
  3009. static void handle_lease(struct ceph_mds_client *mdsc,
  3010. struct ceph_mds_session *session,
  3011. struct ceph_msg *msg)
  3012. {
  3013. struct super_block *sb = mdsc->fsc->sb;
  3014. struct inode *inode;
  3015. struct dentry *parent, *dentry;
  3016. struct ceph_dentry_info *di;
  3017. int mds = session->s_mds;
  3018. struct ceph_mds_lease *h = msg->front.iov_base;
  3019. u32 seq;
  3020. struct ceph_vino vino;
  3021. struct qstr dname;
  3022. int release = 0;
  3023. dout("handle_lease from mds%d\n", mds);
  3024. /* decode */
  3025. if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
  3026. goto bad;
  3027. vino.ino = le64_to_cpu(h->ino);
  3028. vino.snap = CEPH_NOSNAP;
  3029. seq = le32_to_cpu(h->seq);
  3030. dname.len = get_unaligned_le32(h + 1);
  3031. if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
  3032. goto bad;
  3033. dname.name = (void *)(h + 1) + sizeof(u32);
  3034. /* lookup inode */
  3035. inode = ceph_find_inode(sb, vino);
  3036. dout("handle_lease %s, ino %llx %p %.*s\n",
  3037. ceph_lease_op_name(h->action), vino.ino, inode,
  3038. dname.len, dname.name);
  3039. mutex_lock(&session->s_mutex);
  3040. session->s_seq++;
  3041. if (!inode) {
  3042. dout("handle_lease no inode %llx\n", vino.ino);
  3043. goto release;
  3044. }
  3045. /* dentry */
  3046. parent = d_find_alias(inode);
  3047. if (!parent) {
  3048. dout("no parent dentry on inode %p\n", inode);
  3049. WARN_ON(1);
  3050. goto release; /* hrm... */
  3051. }
  3052. dname.hash = full_name_hash(parent, dname.name, dname.len);
  3053. dentry = d_lookup(parent, &dname);
  3054. dput(parent);
  3055. if (!dentry)
  3056. goto release;
  3057. spin_lock(&dentry->d_lock);
  3058. di = ceph_dentry(dentry);
  3059. switch (h->action) {
  3060. case CEPH_MDS_LEASE_REVOKE:
  3061. if (di->lease_session == session) {
  3062. if (ceph_seq_cmp(di->lease_seq, seq) > 0)
  3063. h->seq = cpu_to_le32(di->lease_seq);
  3064. __ceph_mdsc_drop_dentry_lease(dentry);
  3065. }
  3066. release = 1;
  3067. break;
  3068. case CEPH_MDS_LEASE_RENEW:
  3069. if (di->lease_session == session &&
  3070. di->lease_gen == session->s_cap_gen &&
  3071. di->lease_renew_from &&
  3072. di->lease_renew_after == 0) {
  3073. unsigned long duration =
  3074. msecs_to_jiffies(le32_to_cpu(h->duration_ms));
  3075. di->lease_seq = seq;
  3076. di->time = di->lease_renew_from + duration;
  3077. di->lease_renew_after = di->lease_renew_from +
  3078. (duration >> 1);
  3079. di->lease_renew_from = 0;
  3080. }
  3081. break;
  3082. }
  3083. spin_unlock(&dentry->d_lock);
  3084. dput(dentry);
  3085. if (!release)
  3086. goto out;
  3087. release:
  3088. /* let's just reuse the same message */
  3089. h->action = CEPH_MDS_LEASE_REVOKE_ACK;
  3090. ceph_msg_get(msg);
  3091. ceph_con_send(&session->s_con, msg);
  3092. out:
  3093. iput(inode);
  3094. mutex_unlock(&session->s_mutex);
  3095. return;
  3096. bad:
  3097. pr_err("corrupt lease message\n");
  3098. ceph_msg_dump(msg);
  3099. }
  3100. void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
  3101. struct inode *inode,
  3102. struct dentry *dentry, char action,
  3103. u32 seq)
  3104. {
  3105. struct ceph_msg *msg;
  3106. struct ceph_mds_lease *lease;
  3107. int len = sizeof(*lease) + sizeof(u32);
  3108. int dnamelen = 0;
  3109. dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
  3110. inode, dentry, ceph_lease_op_name(action), session->s_mds);
  3111. dnamelen = dentry->d_name.len;
  3112. len += dnamelen;
  3113. msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
  3114. if (!msg)
  3115. return;
  3116. lease = msg->front.iov_base;
  3117. lease->action = action;
  3118. lease->ino = cpu_to_le64(ceph_vino(inode).ino);
  3119. lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
  3120. lease->seq = cpu_to_le32(seq);
  3121. put_unaligned_le32(dnamelen, lease + 1);
  3122. memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
  3123. /*
  3124. * if this is a preemptive lease RELEASE, no need to
  3125. * flush request stream, since the actual request will
  3126. * soon follow.
  3127. */
  3128. msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
  3129. ceph_con_send(&session->s_con, msg);
  3130. }
  3131. /*
  3132. * lock unlock sessions, to wait ongoing session activities
  3133. */
  3134. static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
  3135. {
  3136. int i;
  3137. mutex_lock(&mdsc->mutex);
  3138. for (i = 0; i < mdsc->max_sessions; i++) {
  3139. struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
  3140. if (!s)
  3141. continue;
  3142. mutex_unlock(&mdsc->mutex);
  3143. mutex_lock(&s->s_mutex);
  3144. mutex_unlock(&s->s_mutex);
  3145. ceph_put_mds_session(s);
  3146. mutex_lock(&mdsc->mutex);
  3147. }
  3148. mutex_unlock(&mdsc->mutex);
  3149. }
  3150. /*
  3151. * delayed work -- periodically trim expired leases, renew caps with mds
  3152. */
  3153. static void schedule_delayed(struct ceph_mds_client *mdsc)
  3154. {
  3155. int delay = 5;
  3156. unsigned hz = round_jiffies_relative(HZ * delay);
  3157. schedule_delayed_work(&mdsc->delayed_work, hz);
  3158. }
  3159. static void delayed_work(struct work_struct *work)
  3160. {
  3161. int i;
  3162. struct ceph_mds_client *mdsc =
  3163. container_of(work, struct ceph_mds_client, delayed_work.work);
  3164. int renew_interval;
  3165. int renew_caps;
  3166. dout("mdsc delayed_work\n");
  3167. ceph_check_delayed_caps(mdsc);
  3168. mutex_lock(&mdsc->mutex);
  3169. renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
  3170. renew_caps = time_after_eq(jiffies, HZ*renew_interval +
  3171. mdsc->last_renew_caps);
  3172. if (renew_caps)
  3173. mdsc->last_renew_caps = jiffies;
  3174. for (i = 0; i < mdsc->max_sessions; i++) {
  3175. struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
  3176. if (!s)
  3177. continue;
  3178. if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
  3179. dout("resending session close request for mds%d\n",
  3180. s->s_mds);
  3181. request_close_session(mdsc, s);
  3182. ceph_put_mds_session(s);
  3183. continue;
  3184. }
  3185. if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
  3186. if (s->s_state == CEPH_MDS_SESSION_OPEN) {
  3187. s->s_state = CEPH_MDS_SESSION_HUNG;
  3188. pr_info("mds%d hung\n", s->s_mds);
  3189. }
  3190. }
  3191. if (s->s_state == CEPH_MDS_SESSION_NEW ||
  3192. s->s_state == CEPH_MDS_SESSION_RESTARTING ||
  3193. s->s_state == CEPH_MDS_SESSION_REJECTED) {
  3194. /* this mds is failed or recovering, just wait */
  3195. ceph_put_mds_session(s);
  3196. continue;
  3197. }
  3198. mutex_unlock(&mdsc->mutex);
  3199. mutex_lock(&s->s_mutex);
  3200. if (renew_caps)
  3201. send_renew_caps(mdsc, s);
  3202. else
  3203. ceph_con_keepalive(&s->s_con);
  3204. if (s->s_state == CEPH_MDS_SESSION_OPEN ||
  3205. s->s_state == CEPH_MDS_SESSION_HUNG)
  3206. ceph_send_cap_releases(mdsc, s);
  3207. mutex_unlock(&s->s_mutex);
  3208. ceph_put_mds_session(s);
  3209. mutex_lock(&mdsc->mutex);
  3210. }
  3211. mutex_unlock(&mdsc->mutex);
  3212. schedule_delayed(mdsc);
  3213. }
  3214. int ceph_mdsc_init(struct ceph_fs_client *fsc)
  3215. {
  3216. struct ceph_mds_client *mdsc;
  3217. mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
  3218. if (!mdsc)
  3219. return -ENOMEM;
  3220. mdsc->fsc = fsc;
  3221. mutex_init(&mdsc->mutex);
  3222. mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
  3223. if (!mdsc->mdsmap) {
  3224. kfree(mdsc);
  3225. return -ENOMEM;
  3226. }
  3227. fsc->mdsc = mdsc;
  3228. init_completion(&mdsc->safe_umount_waiters);
  3229. init_waitqueue_head(&mdsc->session_close_wq);
  3230. INIT_LIST_HEAD(&mdsc->waiting_for_map);
  3231. mdsc->sessions = NULL;
  3232. atomic_set(&mdsc->num_sessions, 0);
  3233. mdsc->max_sessions = 0;
  3234. mdsc->stopping = 0;
  3235. atomic64_set(&mdsc->quotarealms_count, 0);
  3236. mdsc->last_snap_seq = 0;
  3237. init_rwsem(&mdsc->snap_rwsem);
  3238. mdsc->snap_realms = RB_ROOT;
  3239. INIT_LIST_HEAD(&mdsc->snap_empty);
  3240. spin_lock_init(&mdsc->snap_empty_lock);
  3241. mdsc->last_tid = 0;
  3242. mdsc->oldest_tid = 0;
  3243. mdsc->request_tree = RB_ROOT;
  3244. INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
  3245. mdsc->last_renew_caps = jiffies;
  3246. INIT_LIST_HEAD(&mdsc->cap_delay_list);
  3247. spin_lock_init(&mdsc->cap_delay_lock);
  3248. INIT_LIST_HEAD(&mdsc->snap_flush_list);
  3249. spin_lock_init(&mdsc->snap_flush_lock);
  3250. mdsc->last_cap_flush_tid = 1;
  3251. INIT_LIST_HEAD(&mdsc->cap_flush_list);
  3252. INIT_LIST_HEAD(&mdsc->cap_dirty);
  3253. INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
  3254. mdsc->num_cap_flushing = 0;
  3255. spin_lock_init(&mdsc->cap_dirty_lock);
  3256. init_waitqueue_head(&mdsc->cap_flushing_wq);
  3257. spin_lock_init(&mdsc->dentry_lru_lock);
  3258. INIT_LIST_HEAD(&mdsc->dentry_lru);
  3259. ceph_caps_init(mdsc);
  3260. ceph_adjust_min_caps(mdsc, fsc->min_caps);
  3261. init_rwsem(&mdsc->pool_perm_rwsem);
  3262. mdsc->pool_perm_tree = RB_ROOT;
  3263. strscpy(mdsc->nodename, utsname()->nodename,
  3264. sizeof(mdsc->nodename));
  3265. return 0;
  3266. }
  3267. /*
  3268. * Wait for safe replies on open mds requests. If we time out, drop
  3269. * all requests from the tree to avoid dangling dentry refs.
  3270. */
  3271. static void wait_requests(struct ceph_mds_client *mdsc)
  3272. {
  3273. struct ceph_options *opts = mdsc->fsc->client->options;
  3274. struct ceph_mds_request *req;
  3275. mutex_lock(&mdsc->mutex);
  3276. if (__get_oldest_req(mdsc)) {
  3277. mutex_unlock(&mdsc->mutex);
  3278. dout("wait_requests waiting for requests\n");
  3279. wait_for_completion_timeout(&mdsc->safe_umount_waiters,
  3280. ceph_timeout_jiffies(opts->mount_timeout));
  3281. /* tear down remaining requests */
  3282. mutex_lock(&mdsc->mutex);
  3283. while ((req = __get_oldest_req(mdsc))) {
  3284. dout("wait_requests timed out on tid %llu\n",
  3285. req->r_tid);
  3286. __unregister_request(mdsc, req);
  3287. }
  3288. }
  3289. mutex_unlock(&mdsc->mutex);
  3290. dout("wait_requests done\n");
  3291. }
  3292. /*
  3293. * called before mount is ro, and before dentries are torn down.
  3294. * (hmm, does this still race with new lookups?)
  3295. */
  3296. void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
  3297. {
  3298. dout("pre_umount\n");
  3299. mdsc->stopping = 1;
  3300. lock_unlock_sessions(mdsc);
  3301. ceph_flush_dirty_caps(mdsc);
  3302. wait_requests(mdsc);
  3303. /*
  3304. * wait for reply handlers to drop their request refs and
  3305. * their inode/dcache refs
  3306. */
  3307. ceph_msgr_flush();
  3308. }
  3309. /*
  3310. * wait for all write mds requests to flush.
  3311. */
  3312. static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
  3313. {
  3314. struct ceph_mds_request *req = NULL, *nextreq;
  3315. struct rb_node *n;
  3316. mutex_lock(&mdsc->mutex);
  3317. dout("wait_unsafe_requests want %lld\n", want_tid);
  3318. restart:
  3319. req = __get_oldest_req(mdsc);
  3320. while (req && req->r_tid <= want_tid) {
  3321. /* find next request */
  3322. n = rb_next(&req->r_node);
  3323. if (n)
  3324. nextreq = rb_entry(n, struct ceph_mds_request, r_node);
  3325. else
  3326. nextreq = NULL;
  3327. if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
  3328. (req->r_op & CEPH_MDS_OP_WRITE)) {
  3329. /* write op */
  3330. ceph_mdsc_get_request(req);
  3331. if (nextreq)
  3332. ceph_mdsc_get_request(nextreq);
  3333. mutex_unlock(&mdsc->mutex);
  3334. dout("wait_unsafe_requests wait on %llu (want %llu)\n",
  3335. req->r_tid, want_tid);
  3336. wait_for_completion(&req->r_safe_completion);
  3337. mutex_lock(&mdsc->mutex);
  3338. ceph_mdsc_put_request(req);
  3339. if (!nextreq)
  3340. break; /* next dne before, so we're done! */
  3341. if (RB_EMPTY_NODE(&nextreq->r_node)) {
  3342. /* next request was removed from tree */
  3343. ceph_mdsc_put_request(nextreq);
  3344. goto restart;
  3345. }
  3346. ceph_mdsc_put_request(nextreq); /* won't go away */
  3347. }
  3348. req = nextreq;
  3349. }
  3350. mutex_unlock(&mdsc->mutex);
  3351. dout("wait_unsafe_requests done\n");
  3352. }
  3353. void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
  3354. {
  3355. u64 want_tid, want_flush;
  3356. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
  3357. return;
  3358. dout("sync\n");
  3359. mutex_lock(&mdsc->mutex);
  3360. want_tid = mdsc->last_tid;
  3361. mutex_unlock(&mdsc->mutex);
  3362. ceph_flush_dirty_caps(mdsc);
  3363. spin_lock(&mdsc->cap_dirty_lock);
  3364. want_flush = mdsc->last_cap_flush_tid;
  3365. if (!list_empty(&mdsc->cap_flush_list)) {
  3366. struct ceph_cap_flush *cf =
  3367. list_last_entry(&mdsc->cap_flush_list,
  3368. struct ceph_cap_flush, g_list);
  3369. cf->wake = true;
  3370. }
  3371. spin_unlock(&mdsc->cap_dirty_lock);
  3372. dout("sync want tid %lld flush_seq %lld\n",
  3373. want_tid, want_flush);
  3374. wait_unsafe_requests(mdsc, want_tid);
  3375. wait_caps_flush(mdsc, want_flush);
  3376. }
  3377. /*
  3378. * true if all sessions are closed, or we force unmount
  3379. */
  3380. static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
  3381. {
  3382. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
  3383. return true;
  3384. return atomic_read(&mdsc->num_sessions) <= skipped;
  3385. }
  3386. /*
  3387. * called after sb is ro.
  3388. */
  3389. void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
  3390. {
  3391. struct ceph_options *opts = mdsc->fsc->client->options;
  3392. struct ceph_mds_session *session;
  3393. int i;
  3394. int skipped = 0;
  3395. dout("close_sessions\n");
  3396. /* close sessions */
  3397. mutex_lock(&mdsc->mutex);
  3398. for (i = 0; i < mdsc->max_sessions; i++) {
  3399. session = __ceph_lookup_mds_session(mdsc, i);
  3400. if (!session)
  3401. continue;
  3402. mutex_unlock(&mdsc->mutex);
  3403. mutex_lock(&session->s_mutex);
  3404. if (__close_session(mdsc, session) <= 0)
  3405. skipped++;
  3406. mutex_unlock(&session->s_mutex);
  3407. ceph_put_mds_session(session);
  3408. mutex_lock(&mdsc->mutex);
  3409. }
  3410. mutex_unlock(&mdsc->mutex);
  3411. dout("waiting for sessions to close\n");
  3412. wait_event_timeout(mdsc->session_close_wq,
  3413. done_closing_sessions(mdsc, skipped),
  3414. ceph_timeout_jiffies(opts->mount_timeout));
  3415. /* tear down remaining sessions */
  3416. mutex_lock(&mdsc->mutex);
  3417. for (i = 0; i < mdsc->max_sessions; i++) {
  3418. if (mdsc->sessions[i]) {
  3419. session = get_session(mdsc->sessions[i]);
  3420. __unregister_session(mdsc, session);
  3421. mutex_unlock(&mdsc->mutex);
  3422. mutex_lock(&session->s_mutex);
  3423. remove_session_caps(session);
  3424. mutex_unlock(&session->s_mutex);
  3425. ceph_put_mds_session(session);
  3426. mutex_lock(&mdsc->mutex);
  3427. }
  3428. }
  3429. WARN_ON(!list_empty(&mdsc->cap_delay_list));
  3430. mutex_unlock(&mdsc->mutex);
  3431. ceph_cleanup_empty_realms(mdsc);
  3432. cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
  3433. dout("stopped\n");
  3434. }
  3435. void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
  3436. {
  3437. struct ceph_mds_session *session;
  3438. int mds;
  3439. dout("force umount\n");
  3440. mutex_lock(&mdsc->mutex);
  3441. for (mds = 0; mds < mdsc->max_sessions; mds++) {
  3442. session = __ceph_lookup_mds_session(mdsc, mds);
  3443. if (!session)
  3444. continue;
  3445. mutex_unlock(&mdsc->mutex);
  3446. mutex_lock(&session->s_mutex);
  3447. __close_session(mdsc, session);
  3448. if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
  3449. cleanup_session_requests(mdsc, session);
  3450. remove_session_caps(session);
  3451. }
  3452. mutex_unlock(&session->s_mutex);
  3453. ceph_put_mds_session(session);
  3454. mutex_lock(&mdsc->mutex);
  3455. kick_requests(mdsc, mds);
  3456. }
  3457. __wake_requests(mdsc, &mdsc->waiting_for_map);
  3458. mutex_unlock(&mdsc->mutex);
  3459. }
  3460. static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
  3461. {
  3462. dout("stop\n");
  3463. cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
  3464. if (mdsc->mdsmap)
  3465. ceph_mdsmap_destroy(mdsc->mdsmap);
  3466. kfree(mdsc->sessions);
  3467. ceph_caps_finalize(mdsc);
  3468. ceph_pool_perm_destroy(mdsc);
  3469. }
  3470. void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
  3471. {
  3472. struct ceph_mds_client *mdsc = fsc->mdsc;
  3473. dout("mdsc_destroy %p\n", mdsc);
  3474. if (!mdsc)
  3475. return;
  3476. /* flush out any connection work with references to us */
  3477. ceph_msgr_flush();
  3478. ceph_mdsc_stop(mdsc);
  3479. fsc->mdsc = NULL;
  3480. kfree(mdsc);
  3481. dout("mdsc_destroy %p done\n", mdsc);
  3482. }
  3483. void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
  3484. {
  3485. struct ceph_fs_client *fsc = mdsc->fsc;
  3486. const char *mds_namespace = fsc->mount_options->mds_namespace;
  3487. void *p = msg->front.iov_base;
  3488. void *end = p + msg->front.iov_len;
  3489. u32 epoch;
  3490. u32 map_len;
  3491. u32 num_fs;
  3492. u32 mount_fscid = (u32)-1;
  3493. u8 struct_v, struct_cv;
  3494. int err = -EINVAL;
  3495. ceph_decode_need(&p, end, sizeof(u32), bad);
  3496. epoch = ceph_decode_32(&p);
  3497. dout("handle_fsmap epoch %u\n", epoch);
  3498. ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
  3499. struct_v = ceph_decode_8(&p);
  3500. struct_cv = ceph_decode_8(&p);
  3501. map_len = ceph_decode_32(&p);
  3502. ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
  3503. p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
  3504. num_fs = ceph_decode_32(&p);
  3505. while (num_fs-- > 0) {
  3506. void *info_p, *info_end;
  3507. u32 info_len;
  3508. u8 info_v, info_cv;
  3509. u32 fscid, namelen;
  3510. ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
  3511. info_v = ceph_decode_8(&p);
  3512. info_cv = ceph_decode_8(&p);
  3513. info_len = ceph_decode_32(&p);
  3514. ceph_decode_need(&p, end, info_len, bad);
  3515. info_p = p;
  3516. info_end = p + info_len;
  3517. p = info_end;
  3518. ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
  3519. fscid = ceph_decode_32(&info_p);
  3520. namelen = ceph_decode_32(&info_p);
  3521. ceph_decode_need(&info_p, info_end, namelen, bad);
  3522. if (mds_namespace &&
  3523. strlen(mds_namespace) == namelen &&
  3524. !strncmp(mds_namespace, (char *)info_p, namelen)) {
  3525. mount_fscid = fscid;
  3526. break;
  3527. }
  3528. }
  3529. ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
  3530. if (mount_fscid != (u32)-1) {
  3531. fsc->client->monc.fs_cluster_id = mount_fscid;
  3532. ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
  3533. 0, true);
  3534. ceph_monc_renew_subs(&fsc->client->monc);
  3535. } else {
  3536. err = -ENOENT;
  3537. goto err_out;
  3538. }
  3539. return;
  3540. bad:
  3541. pr_err("error decoding fsmap\n");
  3542. err_out:
  3543. mutex_lock(&mdsc->mutex);
  3544. mdsc->mdsmap_err = err;
  3545. __wake_requests(mdsc, &mdsc->waiting_for_map);
  3546. mutex_unlock(&mdsc->mutex);
  3547. }
  3548. /*
  3549. * handle mds map update.
  3550. */
  3551. void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
  3552. {
  3553. u32 epoch;
  3554. u32 maplen;
  3555. void *p = msg->front.iov_base;
  3556. void *end = p + msg->front.iov_len;
  3557. struct ceph_mdsmap *newmap, *oldmap;
  3558. struct ceph_fsid fsid;
  3559. int err = -EINVAL;
  3560. ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
  3561. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  3562. if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
  3563. return;
  3564. epoch = ceph_decode_32(&p);
  3565. maplen = ceph_decode_32(&p);
  3566. dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
  3567. /* do we need it? */
  3568. mutex_lock(&mdsc->mutex);
  3569. if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
  3570. dout("handle_map epoch %u <= our %u\n",
  3571. epoch, mdsc->mdsmap->m_epoch);
  3572. mutex_unlock(&mdsc->mutex);
  3573. return;
  3574. }
  3575. newmap = ceph_mdsmap_decode(&p, end);
  3576. if (IS_ERR(newmap)) {
  3577. err = PTR_ERR(newmap);
  3578. goto bad_unlock;
  3579. }
  3580. /* swap into place */
  3581. if (mdsc->mdsmap) {
  3582. oldmap = mdsc->mdsmap;
  3583. mdsc->mdsmap = newmap;
  3584. check_new_map(mdsc, newmap, oldmap);
  3585. ceph_mdsmap_destroy(oldmap);
  3586. } else {
  3587. mdsc->mdsmap = newmap; /* first mds map */
  3588. }
  3589. mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
  3590. MAX_LFS_FILESIZE);
  3591. __wake_requests(mdsc, &mdsc->waiting_for_map);
  3592. ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
  3593. mdsc->mdsmap->m_epoch);
  3594. mutex_unlock(&mdsc->mutex);
  3595. schedule_delayed(mdsc);
  3596. return;
  3597. bad_unlock:
  3598. mutex_unlock(&mdsc->mutex);
  3599. bad:
  3600. pr_err("error decoding mdsmap %d\n", err);
  3601. return;
  3602. }
  3603. static struct ceph_connection *con_get(struct ceph_connection *con)
  3604. {
  3605. struct ceph_mds_session *s = con->private;
  3606. if (get_session(s)) {
  3607. dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
  3608. return con;
  3609. }
  3610. dout("mdsc con_get %p FAIL\n", s);
  3611. return NULL;
  3612. }
  3613. static void con_put(struct ceph_connection *con)
  3614. {
  3615. struct ceph_mds_session *s = con->private;
  3616. dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
  3617. ceph_put_mds_session(s);
  3618. }
  3619. /*
  3620. * if the client is unresponsive for long enough, the mds will kill
  3621. * the session entirely.
  3622. */
  3623. static void peer_reset(struct ceph_connection *con)
  3624. {
  3625. struct ceph_mds_session *s = con->private;
  3626. struct ceph_mds_client *mdsc = s->s_mdsc;
  3627. pr_warn("mds%d closed our session\n", s->s_mds);
  3628. send_mds_reconnect(mdsc, s);
  3629. }
  3630. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  3631. {
  3632. struct ceph_mds_session *s = con->private;
  3633. struct ceph_mds_client *mdsc = s->s_mdsc;
  3634. int type = le16_to_cpu(msg->hdr.type);
  3635. mutex_lock(&mdsc->mutex);
  3636. if (__verify_registered_session(mdsc, s) < 0) {
  3637. mutex_unlock(&mdsc->mutex);
  3638. goto out;
  3639. }
  3640. mutex_unlock(&mdsc->mutex);
  3641. switch (type) {
  3642. case CEPH_MSG_MDS_MAP:
  3643. ceph_mdsc_handle_mdsmap(mdsc, msg);
  3644. break;
  3645. case CEPH_MSG_FS_MAP_USER:
  3646. ceph_mdsc_handle_fsmap(mdsc, msg);
  3647. break;
  3648. case CEPH_MSG_CLIENT_SESSION:
  3649. handle_session(s, msg);
  3650. break;
  3651. case CEPH_MSG_CLIENT_REPLY:
  3652. handle_reply(s, msg);
  3653. break;
  3654. case CEPH_MSG_CLIENT_REQUEST_FORWARD:
  3655. handle_forward(mdsc, s, msg);
  3656. break;
  3657. case CEPH_MSG_CLIENT_CAPS:
  3658. ceph_handle_caps(s, msg);
  3659. break;
  3660. case CEPH_MSG_CLIENT_SNAP:
  3661. ceph_handle_snap(mdsc, s, msg);
  3662. break;
  3663. case CEPH_MSG_CLIENT_LEASE:
  3664. handle_lease(mdsc, s, msg);
  3665. break;
  3666. case CEPH_MSG_CLIENT_QUOTA:
  3667. ceph_handle_quota(mdsc, s, msg);
  3668. break;
  3669. default:
  3670. pr_err("received unknown message type %d %s\n", type,
  3671. ceph_msg_type_name(type));
  3672. }
  3673. out:
  3674. ceph_msg_put(msg);
  3675. }
  3676. /*
  3677. * authentication
  3678. */
  3679. /*
  3680. * Note: returned pointer is the address of a structure that's
  3681. * managed separately. Caller must *not* attempt to free it.
  3682. */
  3683. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  3684. int *proto, int force_new)
  3685. {
  3686. struct ceph_mds_session *s = con->private;
  3687. struct ceph_mds_client *mdsc = s->s_mdsc;
  3688. struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
  3689. struct ceph_auth_handshake *auth = &s->s_auth;
  3690. if (force_new && auth->authorizer) {
  3691. ceph_auth_destroy_authorizer(auth->authorizer);
  3692. auth->authorizer = NULL;
  3693. }
  3694. if (!auth->authorizer) {
  3695. int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
  3696. auth);
  3697. if (ret)
  3698. return ERR_PTR(ret);
  3699. } else {
  3700. int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
  3701. auth);
  3702. if (ret)
  3703. return ERR_PTR(ret);
  3704. }
  3705. *proto = ac->protocol;
  3706. return auth;
  3707. }
  3708. static int add_authorizer_challenge(struct ceph_connection *con,
  3709. void *challenge_buf, int challenge_buf_len)
  3710. {
  3711. struct ceph_mds_session *s = con->private;
  3712. struct ceph_mds_client *mdsc = s->s_mdsc;
  3713. struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
  3714. return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
  3715. challenge_buf, challenge_buf_len);
  3716. }
  3717. static int verify_authorizer_reply(struct ceph_connection *con)
  3718. {
  3719. struct ceph_mds_session *s = con->private;
  3720. struct ceph_mds_client *mdsc = s->s_mdsc;
  3721. struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
  3722. return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
  3723. }
  3724. static int invalidate_authorizer(struct ceph_connection *con)
  3725. {
  3726. struct ceph_mds_session *s = con->private;
  3727. struct ceph_mds_client *mdsc = s->s_mdsc;
  3728. struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
  3729. ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
  3730. return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
  3731. }
  3732. static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
  3733. struct ceph_msg_header *hdr, int *skip)
  3734. {
  3735. struct ceph_msg *msg;
  3736. int type = (int) le16_to_cpu(hdr->type);
  3737. int front_len = (int) le32_to_cpu(hdr->front_len);
  3738. if (con->in_msg)
  3739. return con->in_msg;
  3740. *skip = 0;
  3741. msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
  3742. if (!msg) {
  3743. pr_err("unable to allocate msg type %d len %d\n",
  3744. type, front_len);
  3745. return NULL;
  3746. }
  3747. return msg;
  3748. }
  3749. static int mds_sign_message(struct ceph_msg *msg)
  3750. {
  3751. struct ceph_mds_session *s = msg->con->private;
  3752. struct ceph_auth_handshake *auth = &s->s_auth;
  3753. return ceph_auth_sign_message(auth, msg);
  3754. }
  3755. static int mds_check_message_signature(struct ceph_msg *msg)
  3756. {
  3757. struct ceph_mds_session *s = msg->con->private;
  3758. struct ceph_auth_handshake *auth = &s->s_auth;
  3759. return ceph_auth_check_message_signature(auth, msg);
  3760. }
  3761. static const struct ceph_connection_operations mds_con_ops = {
  3762. .get = con_get,
  3763. .put = con_put,
  3764. .dispatch = dispatch,
  3765. .get_authorizer = get_authorizer,
  3766. .add_authorizer_challenge = add_authorizer_challenge,
  3767. .verify_authorizer_reply = verify_authorizer_reply,
  3768. .invalidate_authorizer = invalidate_authorizer,
  3769. .peer_reset = peer_reset,
  3770. .alloc_msg = mds_alloc_msg,
  3771. .sign_message = mds_sign_message,
  3772. .check_message_signature = mds_check_message_signature,
  3773. };
  3774. /* eof */