mds_client.c 87 KB

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