mds_client.c 82 KB

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