mds_client.c 86 KB

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