mds_client.c 103 KB

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