mds_client.c 90 KB

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