mds_client.c 88 KB

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