mds_client.c 90 KB

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