mds_client.c 108 KB

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