mds_client.c 84 KB

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