mds_client.c 105 KB

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