mds_client.c 75 KB

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