mds_client.c 74 KB

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