mds_client.c 80 KB

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