mds_client.c 87 KB

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