mds_client.c 80 KB

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