mds_client.c 103 KB

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