mds_client.c 101 KB

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