mds_client.c 89 KB

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