mds_client.c 73 KB

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