mds_client.c 78 KB

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