mds_client.c 84 KB

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