mds_client.c 96 KB

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