mds_client.c 73 KB

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