mds_client.c 87 KB

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