mds_client.c 84 KB

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