mds_client.c 85 KB

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