mds_client.c 109 KB

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