mds_client.c 87 KB

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