mds_client.c 83 KB

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