mds_client.c 102 KB

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