mds_client.c 95 KB

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