mds_client.c 75 KB

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