mds_client.c 89 KB

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