mds_client.c 88 KB

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