caps.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/fs.h>
  4. #include <linux/kernel.h>
  5. #include <linux/sched/signal.h>
  6. #include <linux/slab.h>
  7. #include <linux/vmalloc.h>
  8. #include <linux/wait.h>
  9. #include <linux/writeback.h>
  10. #include <linux/iversion.h>
  11. #include "super.h"
  12. #include "mds_client.h"
  13. #include "cache.h"
  14. #include <linux/ceph/decode.h>
  15. #include <linux/ceph/messenger.h>
  16. /*
  17. * Capability management
  18. *
  19. * The Ceph metadata servers control client access to inode metadata
  20. * and file data by issuing capabilities, granting clients permission
  21. * to read and/or write both inode field and file data to OSDs
  22. * (storage nodes). Each capability consists of a set of bits
  23. * indicating which operations are allowed.
  24. *
  25. * If the client holds a *_SHARED cap, the client has a coherent value
  26. * that can be safely read from the cached inode.
  27. *
  28. * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
  29. * client is allowed to change inode attributes (e.g., file size,
  30. * mtime), note its dirty state in the ceph_cap, and asynchronously
  31. * flush that metadata change to the MDS.
  32. *
  33. * In the event of a conflicting operation (perhaps by another
  34. * client), the MDS will revoke the conflicting client capabilities.
  35. *
  36. * In order for a client to cache an inode, it must hold a capability
  37. * with at least one MDS server. When inodes are released, release
  38. * notifications are batched and periodically sent en masse to the MDS
  39. * cluster to release server state.
  40. */
  41. static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
  42. static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
  43. struct ceph_mds_session *session,
  44. struct ceph_inode_info *ci,
  45. u64 oldest_flush_tid);
  46. /*
  47. * Generate readable cap strings for debugging output.
  48. */
  49. #define MAX_CAP_STR 20
  50. static char cap_str[MAX_CAP_STR][40];
  51. static DEFINE_SPINLOCK(cap_str_lock);
  52. static int last_cap_str;
  53. static char *gcap_string(char *s, int c)
  54. {
  55. if (c & CEPH_CAP_GSHARED)
  56. *s++ = 's';
  57. if (c & CEPH_CAP_GEXCL)
  58. *s++ = 'x';
  59. if (c & CEPH_CAP_GCACHE)
  60. *s++ = 'c';
  61. if (c & CEPH_CAP_GRD)
  62. *s++ = 'r';
  63. if (c & CEPH_CAP_GWR)
  64. *s++ = 'w';
  65. if (c & CEPH_CAP_GBUFFER)
  66. *s++ = 'b';
  67. if (c & CEPH_CAP_GWREXTEND)
  68. *s++ = 'a';
  69. if (c & CEPH_CAP_GLAZYIO)
  70. *s++ = 'l';
  71. return s;
  72. }
  73. const char *ceph_cap_string(int caps)
  74. {
  75. int i;
  76. char *s;
  77. int c;
  78. spin_lock(&cap_str_lock);
  79. i = last_cap_str++;
  80. if (last_cap_str == MAX_CAP_STR)
  81. last_cap_str = 0;
  82. spin_unlock(&cap_str_lock);
  83. s = cap_str[i];
  84. if (caps & CEPH_CAP_PIN)
  85. *s++ = 'p';
  86. c = (caps >> CEPH_CAP_SAUTH) & 3;
  87. if (c) {
  88. *s++ = 'A';
  89. s = gcap_string(s, c);
  90. }
  91. c = (caps >> CEPH_CAP_SLINK) & 3;
  92. if (c) {
  93. *s++ = 'L';
  94. s = gcap_string(s, c);
  95. }
  96. c = (caps >> CEPH_CAP_SXATTR) & 3;
  97. if (c) {
  98. *s++ = 'X';
  99. s = gcap_string(s, c);
  100. }
  101. c = caps >> CEPH_CAP_SFILE;
  102. if (c) {
  103. *s++ = 'F';
  104. s = gcap_string(s, c);
  105. }
  106. if (s == cap_str[i])
  107. *s++ = '-';
  108. *s = 0;
  109. return cap_str[i];
  110. }
  111. void ceph_caps_init(struct ceph_mds_client *mdsc)
  112. {
  113. INIT_LIST_HEAD(&mdsc->caps_list);
  114. spin_lock_init(&mdsc->caps_list_lock);
  115. }
  116. void ceph_caps_finalize(struct ceph_mds_client *mdsc)
  117. {
  118. struct ceph_cap *cap;
  119. spin_lock(&mdsc->caps_list_lock);
  120. while (!list_empty(&mdsc->caps_list)) {
  121. cap = list_first_entry(&mdsc->caps_list,
  122. struct ceph_cap, caps_item);
  123. list_del(&cap->caps_item);
  124. kmem_cache_free(ceph_cap_cachep, cap);
  125. }
  126. mdsc->caps_total_count = 0;
  127. mdsc->caps_avail_count = 0;
  128. mdsc->caps_use_count = 0;
  129. mdsc->caps_reserve_count = 0;
  130. mdsc->caps_min_count = 0;
  131. spin_unlock(&mdsc->caps_list_lock);
  132. }
  133. void ceph_adjust_caps_max_min(struct ceph_mds_client *mdsc,
  134. struct ceph_mount_options *fsopt)
  135. {
  136. spin_lock(&mdsc->caps_list_lock);
  137. mdsc->caps_min_count = fsopt->max_readdir;
  138. if (mdsc->caps_min_count < 1024)
  139. mdsc->caps_min_count = 1024;
  140. mdsc->caps_use_max = fsopt->caps_max;
  141. if (mdsc->caps_use_max > 0 &&
  142. mdsc->caps_use_max < mdsc->caps_min_count)
  143. mdsc->caps_use_max = mdsc->caps_min_count;
  144. spin_unlock(&mdsc->caps_list_lock);
  145. }
  146. static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps)
  147. {
  148. struct ceph_cap *cap;
  149. int i;
  150. if (nr_caps) {
  151. BUG_ON(mdsc->caps_reserve_count < nr_caps);
  152. mdsc->caps_reserve_count -= nr_caps;
  153. if (mdsc->caps_avail_count >=
  154. mdsc->caps_reserve_count + mdsc->caps_min_count) {
  155. mdsc->caps_total_count -= nr_caps;
  156. for (i = 0; i < nr_caps; i++) {
  157. cap = list_first_entry(&mdsc->caps_list,
  158. struct ceph_cap, caps_item);
  159. list_del(&cap->caps_item);
  160. kmem_cache_free(ceph_cap_cachep, cap);
  161. }
  162. } else {
  163. mdsc->caps_avail_count += nr_caps;
  164. }
  165. dout("%s: caps %d = %d used + %d resv + %d avail\n",
  166. __func__,
  167. mdsc->caps_total_count, mdsc->caps_use_count,
  168. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  169. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  170. mdsc->caps_reserve_count +
  171. mdsc->caps_avail_count);
  172. }
  173. }
  174. /*
  175. * Called under mdsc->mutex.
  176. */
  177. int ceph_reserve_caps(struct ceph_mds_client *mdsc,
  178. struct ceph_cap_reservation *ctx, int need)
  179. {
  180. int i, j;
  181. struct ceph_cap *cap;
  182. int have;
  183. int alloc = 0;
  184. int max_caps;
  185. int err = 0;
  186. bool trimmed = false;
  187. struct ceph_mds_session *s;
  188. LIST_HEAD(newcaps);
  189. dout("reserve caps ctx=%p need=%d\n", ctx, need);
  190. /* first reserve any caps that are already allocated */
  191. spin_lock(&mdsc->caps_list_lock);
  192. if (mdsc->caps_avail_count >= need)
  193. have = need;
  194. else
  195. have = mdsc->caps_avail_count;
  196. mdsc->caps_avail_count -= have;
  197. mdsc->caps_reserve_count += have;
  198. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  199. mdsc->caps_reserve_count +
  200. mdsc->caps_avail_count);
  201. spin_unlock(&mdsc->caps_list_lock);
  202. for (i = have; i < need; ) {
  203. cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
  204. if (cap) {
  205. list_add(&cap->caps_item, &newcaps);
  206. alloc++;
  207. i++;
  208. continue;
  209. }
  210. if (!trimmed) {
  211. for (j = 0; j < mdsc->max_sessions; j++) {
  212. s = __ceph_lookup_mds_session(mdsc, j);
  213. if (!s)
  214. continue;
  215. mutex_unlock(&mdsc->mutex);
  216. mutex_lock(&s->s_mutex);
  217. max_caps = s->s_nr_caps - (need - i);
  218. ceph_trim_caps(mdsc, s, max_caps);
  219. mutex_unlock(&s->s_mutex);
  220. ceph_put_mds_session(s);
  221. mutex_lock(&mdsc->mutex);
  222. }
  223. trimmed = true;
  224. spin_lock(&mdsc->caps_list_lock);
  225. if (mdsc->caps_avail_count) {
  226. int more_have;
  227. if (mdsc->caps_avail_count >= need - i)
  228. more_have = need - i;
  229. else
  230. more_have = mdsc->caps_avail_count;
  231. i += more_have;
  232. have += more_have;
  233. mdsc->caps_avail_count -= more_have;
  234. mdsc->caps_reserve_count += more_have;
  235. }
  236. spin_unlock(&mdsc->caps_list_lock);
  237. continue;
  238. }
  239. pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
  240. ctx, need, have + alloc);
  241. err = -ENOMEM;
  242. break;
  243. }
  244. if (!err) {
  245. BUG_ON(have + alloc != need);
  246. ctx->count = need;
  247. ctx->used = 0;
  248. }
  249. spin_lock(&mdsc->caps_list_lock);
  250. mdsc->caps_total_count += alloc;
  251. mdsc->caps_reserve_count += alloc;
  252. list_splice(&newcaps, &mdsc->caps_list);
  253. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  254. mdsc->caps_reserve_count +
  255. mdsc->caps_avail_count);
  256. if (err)
  257. __ceph_unreserve_caps(mdsc, have + alloc);
  258. spin_unlock(&mdsc->caps_list_lock);
  259. dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
  260. ctx, mdsc->caps_total_count, mdsc->caps_use_count,
  261. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  262. return err;
  263. }
  264. void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
  265. struct ceph_cap_reservation *ctx)
  266. {
  267. bool reclaim = false;
  268. if (!ctx->count)
  269. return;
  270. dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
  271. spin_lock(&mdsc->caps_list_lock);
  272. __ceph_unreserve_caps(mdsc, ctx->count);
  273. ctx->count = 0;
  274. if (mdsc->caps_use_max > 0 &&
  275. mdsc->caps_use_count > mdsc->caps_use_max)
  276. reclaim = true;
  277. spin_unlock(&mdsc->caps_list_lock);
  278. if (reclaim)
  279. ceph_reclaim_caps_nr(mdsc, ctx->used);
  280. }
  281. struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
  282. struct ceph_cap_reservation *ctx)
  283. {
  284. struct ceph_cap *cap = NULL;
  285. /* temporary, until we do something about cap import/export */
  286. if (!ctx) {
  287. cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
  288. if (cap) {
  289. spin_lock(&mdsc->caps_list_lock);
  290. mdsc->caps_use_count++;
  291. mdsc->caps_total_count++;
  292. spin_unlock(&mdsc->caps_list_lock);
  293. } else {
  294. spin_lock(&mdsc->caps_list_lock);
  295. if (mdsc->caps_avail_count) {
  296. BUG_ON(list_empty(&mdsc->caps_list));
  297. mdsc->caps_avail_count--;
  298. mdsc->caps_use_count++;
  299. cap = list_first_entry(&mdsc->caps_list,
  300. struct ceph_cap, caps_item);
  301. list_del(&cap->caps_item);
  302. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  303. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  304. }
  305. spin_unlock(&mdsc->caps_list_lock);
  306. }
  307. return cap;
  308. }
  309. spin_lock(&mdsc->caps_list_lock);
  310. dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
  311. ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
  312. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  313. BUG_ON(!ctx->count);
  314. BUG_ON(ctx->count > mdsc->caps_reserve_count);
  315. BUG_ON(list_empty(&mdsc->caps_list));
  316. ctx->count--;
  317. ctx->used++;
  318. mdsc->caps_reserve_count--;
  319. mdsc->caps_use_count++;
  320. cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
  321. list_del(&cap->caps_item);
  322. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  323. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  324. spin_unlock(&mdsc->caps_list_lock);
  325. return cap;
  326. }
  327. void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
  328. {
  329. spin_lock(&mdsc->caps_list_lock);
  330. dout("put_cap %p %d = %d used + %d resv + %d avail\n",
  331. cap, mdsc->caps_total_count, mdsc->caps_use_count,
  332. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  333. mdsc->caps_use_count--;
  334. /*
  335. * Keep some preallocated caps around (ceph_min_count), to
  336. * avoid lots of free/alloc churn.
  337. */
  338. if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
  339. mdsc->caps_min_count) {
  340. mdsc->caps_total_count--;
  341. kmem_cache_free(ceph_cap_cachep, cap);
  342. } else {
  343. mdsc->caps_avail_count++;
  344. list_add(&cap->caps_item, &mdsc->caps_list);
  345. }
  346. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  347. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  348. spin_unlock(&mdsc->caps_list_lock);
  349. }
  350. void ceph_reservation_status(struct ceph_fs_client *fsc,
  351. int *total, int *avail, int *used, int *reserved,
  352. int *min)
  353. {
  354. struct ceph_mds_client *mdsc = fsc->mdsc;
  355. spin_lock(&mdsc->caps_list_lock);
  356. if (total)
  357. *total = mdsc->caps_total_count;
  358. if (avail)
  359. *avail = mdsc->caps_avail_count;
  360. if (used)
  361. *used = mdsc->caps_use_count;
  362. if (reserved)
  363. *reserved = mdsc->caps_reserve_count;
  364. if (min)
  365. *min = mdsc->caps_min_count;
  366. spin_unlock(&mdsc->caps_list_lock);
  367. }
  368. /*
  369. * Find ceph_cap for given mds, if any.
  370. *
  371. * Called with i_ceph_lock held.
  372. */
  373. static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  374. {
  375. struct ceph_cap *cap;
  376. struct rb_node *n = ci->i_caps.rb_node;
  377. while (n) {
  378. cap = rb_entry(n, struct ceph_cap, ci_node);
  379. if (mds < cap->mds)
  380. n = n->rb_left;
  381. else if (mds > cap->mds)
  382. n = n->rb_right;
  383. else
  384. return cap;
  385. }
  386. return NULL;
  387. }
  388. struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  389. {
  390. struct ceph_cap *cap;
  391. spin_lock(&ci->i_ceph_lock);
  392. cap = __get_cap_for_mds(ci, mds);
  393. spin_unlock(&ci->i_ceph_lock);
  394. return cap;
  395. }
  396. /*
  397. * Called under i_ceph_lock.
  398. */
  399. static void __insert_cap_node(struct ceph_inode_info *ci,
  400. struct ceph_cap *new)
  401. {
  402. struct rb_node **p = &ci->i_caps.rb_node;
  403. struct rb_node *parent = NULL;
  404. struct ceph_cap *cap = NULL;
  405. while (*p) {
  406. parent = *p;
  407. cap = rb_entry(parent, struct ceph_cap, ci_node);
  408. if (new->mds < cap->mds)
  409. p = &(*p)->rb_left;
  410. else if (new->mds > cap->mds)
  411. p = &(*p)->rb_right;
  412. else
  413. BUG();
  414. }
  415. rb_link_node(&new->ci_node, parent, p);
  416. rb_insert_color(&new->ci_node, &ci->i_caps);
  417. }
  418. /*
  419. * (re)set cap hold timeouts, which control the delayed release
  420. * of unused caps back to the MDS. Should be called on cap use.
  421. */
  422. static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
  423. struct ceph_inode_info *ci)
  424. {
  425. struct ceph_mount_options *opt = mdsc->fsc->mount_options;
  426. ci->i_hold_caps_max = round_jiffies(jiffies +
  427. opt->caps_wanted_delay_max * HZ);
  428. dout("__cap_set_timeouts %p %lu\n", &ci->vfs_inode,
  429. ci->i_hold_caps_max - jiffies);
  430. }
  431. /*
  432. * (Re)queue cap at the end of the delayed cap release list.
  433. *
  434. * If I_FLUSH is set, leave the inode at the front of the list.
  435. *
  436. * Caller holds i_ceph_lock
  437. * -> we take mdsc->cap_delay_lock
  438. */
  439. static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
  440. struct ceph_inode_info *ci)
  441. {
  442. dout("__cap_delay_requeue %p flags 0x%lx at %lu\n", &ci->vfs_inode,
  443. ci->i_ceph_flags, ci->i_hold_caps_max);
  444. if (!mdsc->stopping) {
  445. spin_lock(&mdsc->cap_delay_lock);
  446. if (!list_empty(&ci->i_cap_delay_list)) {
  447. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  448. goto no_change;
  449. list_del_init(&ci->i_cap_delay_list);
  450. }
  451. __cap_set_timeouts(mdsc, ci);
  452. list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  453. no_change:
  454. spin_unlock(&mdsc->cap_delay_lock);
  455. }
  456. }
  457. /*
  458. * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
  459. * indicating we should send a cap message to flush dirty metadata
  460. * asap, and move to the front of the delayed cap list.
  461. */
  462. static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
  463. struct ceph_inode_info *ci)
  464. {
  465. dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
  466. spin_lock(&mdsc->cap_delay_lock);
  467. ci->i_ceph_flags |= CEPH_I_FLUSH;
  468. if (!list_empty(&ci->i_cap_delay_list))
  469. list_del_init(&ci->i_cap_delay_list);
  470. list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  471. spin_unlock(&mdsc->cap_delay_lock);
  472. }
  473. /*
  474. * Cancel delayed work on cap.
  475. *
  476. * Caller must hold i_ceph_lock.
  477. */
  478. static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
  479. struct ceph_inode_info *ci)
  480. {
  481. dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
  482. if (list_empty(&ci->i_cap_delay_list))
  483. return;
  484. spin_lock(&mdsc->cap_delay_lock);
  485. list_del_init(&ci->i_cap_delay_list);
  486. spin_unlock(&mdsc->cap_delay_lock);
  487. }
  488. /* Common issue checks for add_cap, handle_cap_grant. */
  489. static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
  490. unsigned issued)
  491. {
  492. unsigned had = __ceph_caps_issued(ci, NULL);
  493. lockdep_assert_held(&ci->i_ceph_lock);
  494. /*
  495. * Each time we receive FILE_CACHE anew, we increment
  496. * i_rdcache_gen.
  497. */
  498. if (S_ISREG(ci->vfs_inode.i_mode) &&
  499. (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
  500. (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
  501. ci->i_rdcache_gen++;
  502. }
  503. /*
  504. * If FILE_SHARED is newly issued, mark dir not complete. We don't
  505. * know what happened to this directory while we didn't have the cap.
  506. * If FILE_SHARED is being revoked, also mark dir not complete. It
  507. * stops on-going cached readdir.
  508. */
  509. if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
  510. if (issued & CEPH_CAP_FILE_SHARED)
  511. atomic_inc(&ci->i_shared_gen);
  512. if (S_ISDIR(ci->vfs_inode.i_mode)) {
  513. dout(" marking %p NOT complete\n", &ci->vfs_inode);
  514. __ceph_dir_clear_complete(ci);
  515. }
  516. }
  517. /* Wipe saved layout if we're losing DIR_CREATE caps */
  518. if (S_ISDIR(ci->vfs_inode.i_mode) && (had & CEPH_CAP_DIR_CREATE) &&
  519. !(issued & CEPH_CAP_DIR_CREATE)) {
  520. ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
  521. memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
  522. }
  523. }
  524. /**
  525. * change_auth_cap_ses - move inode to appropriate lists when auth caps change
  526. * @ci: inode to be moved
  527. * @session: new auth caps session
  528. */
  529. static void change_auth_cap_ses(struct ceph_inode_info *ci,
  530. struct ceph_mds_session *session)
  531. {
  532. lockdep_assert_held(&ci->i_ceph_lock);
  533. if (list_empty(&ci->i_dirty_item) && list_empty(&ci->i_flushing_item))
  534. return;
  535. spin_lock(&session->s_mdsc->cap_dirty_lock);
  536. if (!list_empty(&ci->i_dirty_item))
  537. list_move(&ci->i_dirty_item, &session->s_cap_dirty);
  538. if (!list_empty(&ci->i_flushing_item))
  539. list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  540. spin_unlock(&session->s_mdsc->cap_dirty_lock);
  541. }
  542. /*
  543. * Add a capability under the given MDS session.
  544. *
  545. * Caller should hold session snap_rwsem (read) and ci->i_ceph_lock
  546. *
  547. * @fmode is the open file mode, if we are opening a file, otherwise
  548. * it is < 0. (This is so we can atomically add the cap and add an
  549. * open file reference to it.)
  550. */
  551. void ceph_add_cap(struct inode *inode,
  552. struct ceph_mds_session *session, u64 cap_id,
  553. unsigned issued, unsigned wanted,
  554. unsigned seq, unsigned mseq, u64 realmino, int flags,
  555. struct ceph_cap **new_cap)
  556. {
  557. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  558. struct ceph_inode_info *ci = ceph_inode(inode);
  559. struct ceph_cap *cap;
  560. int mds = session->s_mds;
  561. int actual_wanted;
  562. u32 gen;
  563. lockdep_assert_held(&ci->i_ceph_lock);
  564. dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
  565. session->s_mds, cap_id, ceph_cap_string(issued), seq);
  566. spin_lock(&session->s_gen_ttl_lock);
  567. gen = session->s_cap_gen;
  568. spin_unlock(&session->s_gen_ttl_lock);
  569. cap = __get_cap_for_mds(ci, mds);
  570. if (!cap) {
  571. cap = *new_cap;
  572. *new_cap = NULL;
  573. cap->issued = 0;
  574. cap->implemented = 0;
  575. cap->mds = mds;
  576. cap->mds_wanted = 0;
  577. cap->mseq = 0;
  578. cap->ci = ci;
  579. __insert_cap_node(ci, cap);
  580. /* add to session cap list */
  581. cap->session = session;
  582. spin_lock(&session->s_cap_lock);
  583. list_add_tail(&cap->session_caps, &session->s_caps);
  584. session->s_nr_caps++;
  585. atomic64_inc(&mdsc->metric.total_caps);
  586. spin_unlock(&session->s_cap_lock);
  587. } else {
  588. spin_lock(&session->s_cap_lock);
  589. list_move_tail(&cap->session_caps, &session->s_caps);
  590. spin_unlock(&session->s_cap_lock);
  591. if (cap->cap_gen < gen)
  592. cap->issued = cap->implemented = CEPH_CAP_PIN;
  593. /*
  594. * auth mds of the inode changed. we received the cap export
  595. * message, but still haven't received the cap import message.
  596. * handle_cap_export() updated the new auth MDS' cap.
  597. *
  598. * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
  599. * a message that was send before the cap import message. So
  600. * don't remove caps.
  601. */
  602. if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  603. WARN_ON(cap != ci->i_auth_cap);
  604. WARN_ON(cap->cap_id != cap_id);
  605. seq = cap->seq;
  606. mseq = cap->mseq;
  607. issued |= cap->issued;
  608. flags |= CEPH_CAP_FLAG_AUTH;
  609. }
  610. }
  611. if (!ci->i_snap_realm ||
  612. ((flags & CEPH_CAP_FLAG_AUTH) &&
  613. realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
  614. /*
  615. * add this inode to the appropriate snap realm
  616. */
  617. struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
  618. realmino);
  619. if (realm) {
  620. struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
  621. if (oldrealm) {
  622. spin_lock(&oldrealm->inodes_with_caps_lock);
  623. list_del_init(&ci->i_snap_realm_item);
  624. spin_unlock(&oldrealm->inodes_with_caps_lock);
  625. }
  626. spin_lock(&realm->inodes_with_caps_lock);
  627. list_add(&ci->i_snap_realm_item,
  628. &realm->inodes_with_caps);
  629. ci->i_snap_realm = realm;
  630. if (realm->ino == ci->i_vino.ino)
  631. realm->inode = inode;
  632. spin_unlock(&realm->inodes_with_caps_lock);
  633. if (oldrealm)
  634. ceph_put_snap_realm(mdsc, oldrealm);
  635. } else {
  636. pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
  637. realmino);
  638. WARN_ON(!realm);
  639. }
  640. }
  641. __check_cap_issue(ci, cap, issued);
  642. /*
  643. * If we are issued caps we don't want, or the mds' wanted
  644. * value appears to be off, queue a check so we'll release
  645. * later and/or update the mds wanted value.
  646. */
  647. actual_wanted = __ceph_caps_wanted(ci);
  648. if ((wanted & ~actual_wanted) ||
  649. (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
  650. dout(" issued %s, mds wanted %s, actual %s, queueing\n",
  651. ceph_cap_string(issued), ceph_cap_string(wanted),
  652. ceph_cap_string(actual_wanted));
  653. __cap_delay_requeue(mdsc, ci);
  654. }
  655. if (flags & CEPH_CAP_FLAG_AUTH) {
  656. if (!ci->i_auth_cap ||
  657. ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
  658. if (ci->i_auth_cap &&
  659. ci->i_auth_cap->session != cap->session)
  660. change_auth_cap_ses(ci, cap->session);
  661. ci->i_auth_cap = cap;
  662. cap->mds_wanted = wanted;
  663. }
  664. } else {
  665. WARN_ON(ci->i_auth_cap == cap);
  666. }
  667. dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
  668. inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
  669. ceph_cap_string(issued|cap->issued), seq, mds);
  670. cap->cap_id = cap_id;
  671. cap->issued = issued;
  672. cap->implemented |= issued;
  673. if (ceph_seq_cmp(mseq, cap->mseq) > 0)
  674. cap->mds_wanted = wanted;
  675. else
  676. cap->mds_wanted |= wanted;
  677. cap->seq = seq;
  678. cap->issue_seq = seq;
  679. cap->mseq = mseq;
  680. cap->cap_gen = gen;
  681. }
  682. /*
  683. * Return true if cap has not timed out and belongs to the current
  684. * generation of the MDS session (i.e. has not gone 'stale' due to
  685. * us losing touch with the mds).
  686. */
  687. static int __cap_is_valid(struct ceph_cap *cap)
  688. {
  689. unsigned long ttl;
  690. u32 gen;
  691. spin_lock(&cap->session->s_gen_ttl_lock);
  692. gen = cap->session->s_cap_gen;
  693. ttl = cap->session->s_cap_ttl;
  694. spin_unlock(&cap->session->s_gen_ttl_lock);
  695. if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
  696. dout("__cap_is_valid %p cap %p issued %s "
  697. "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
  698. cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
  699. return 0;
  700. }
  701. return 1;
  702. }
  703. /*
  704. * Return set of valid cap bits issued to us. Note that caps time
  705. * out, and may be invalidated in bulk if the client session times out
  706. * and session->s_cap_gen is bumped.
  707. */
  708. int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
  709. {
  710. int have = ci->i_snap_caps;
  711. struct ceph_cap *cap;
  712. struct rb_node *p;
  713. if (implemented)
  714. *implemented = 0;
  715. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  716. cap = rb_entry(p, struct ceph_cap, ci_node);
  717. if (!__cap_is_valid(cap))
  718. continue;
  719. dout("__ceph_caps_issued %p cap %p issued %s\n",
  720. &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
  721. have |= cap->issued;
  722. if (implemented)
  723. *implemented |= cap->implemented;
  724. }
  725. /*
  726. * exclude caps issued by non-auth MDS, but are been revoking
  727. * by the auth MDS. The non-auth MDS should be revoking/exporting
  728. * these caps, but the message is delayed.
  729. */
  730. if (ci->i_auth_cap) {
  731. cap = ci->i_auth_cap;
  732. have &= ~cap->implemented | cap->issued;
  733. }
  734. return have;
  735. }
  736. /*
  737. * Get cap bits issued by caps other than @ocap
  738. */
  739. int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
  740. {
  741. int have = ci->i_snap_caps;
  742. struct ceph_cap *cap;
  743. struct rb_node *p;
  744. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  745. cap = rb_entry(p, struct ceph_cap, ci_node);
  746. if (cap == ocap)
  747. continue;
  748. if (!__cap_is_valid(cap))
  749. continue;
  750. have |= cap->issued;
  751. }
  752. return have;
  753. }
  754. /*
  755. * Move a cap to the end of the LRU (oldest caps at list head, newest
  756. * at list tail).
  757. */
  758. static void __touch_cap(struct ceph_cap *cap)
  759. {
  760. struct ceph_mds_session *s = cap->session;
  761. spin_lock(&s->s_cap_lock);
  762. if (!s->s_cap_iterator) {
  763. dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
  764. s->s_mds);
  765. list_move_tail(&cap->session_caps, &s->s_caps);
  766. } else {
  767. dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
  768. &cap->ci->vfs_inode, cap, s->s_mds);
  769. }
  770. spin_unlock(&s->s_cap_lock);
  771. }
  772. /*
  773. * Check if we hold the given mask. If so, move the cap(s) to the
  774. * front of their respective LRUs. (This is the preferred way for
  775. * callers to check for caps they want.)
  776. */
  777. int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
  778. {
  779. struct ceph_cap *cap;
  780. struct rb_node *p;
  781. int have = ci->i_snap_caps;
  782. if ((have & mask) == mask) {
  783. dout("__ceph_caps_issued_mask ino 0x%llx snap issued %s"
  784. " (mask %s)\n", ceph_ino(&ci->vfs_inode),
  785. ceph_cap_string(have),
  786. ceph_cap_string(mask));
  787. return 1;
  788. }
  789. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  790. cap = rb_entry(p, struct ceph_cap, ci_node);
  791. if (!__cap_is_valid(cap))
  792. continue;
  793. if ((cap->issued & mask) == mask) {
  794. dout("__ceph_caps_issued_mask ino 0x%llx cap %p issued %s"
  795. " (mask %s)\n", ceph_ino(&ci->vfs_inode), cap,
  796. ceph_cap_string(cap->issued),
  797. ceph_cap_string(mask));
  798. if (touch)
  799. __touch_cap(cap);
  800. return 1;
  801. }
  802. /* does a combination of caps satisfy mask? */
  803. have |= cap->issued;
  804. if ((have & mask) == mask) {
  805. dout("__ceph_caps_issued_mask ino 0x%llx combo issued %s"
  806. " (mask %s)\n", ceph_ino(&ci->vfs_inode),
  807. ceph_cap_string(cap->issued),
  808. ceph_cap_string(mask));
  809. if (touch) {
  810. struct rb_node *q;
  811. /* touch this + preceding caps */
  812. __touch_cap(cap);
  813. for (q = rb_first(&ci->i_caps); q != p;
  814. q = rb_next(q)) {
  815. cap = rb_entry(q, struct ceph_cap,
  816. ci_node);
  817. if (!__cap_is_valid(cap))
  818. continue;
  819. if (cap->issued & mask)
  820. __touch_cap(cap);
  821. }
  822. }
  823. return 1;
  824. }
  825. }
  826. return 0;
  827. }
  828. int __ceph_caps_issued_mask_metric(struct ceph_inode_info *ci, int mask,
  829. int touch)
  830. {
  831. struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
  832. int r;
  833. r = __ceph_caps_issued_mask(ci, mask, touch);
  834. if (r)
  835. ceph_update_cap_hit(&fsc->mdsc->metric);
  836. else
  837. ceph_update_cap_mis(&fsc->mdsc->metric);
  838. return r;
  839. }
  840. /*
  841. * Return true if mask caps are currently being revoked by an MDS.
  842. */
  843. int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
  844. struct ceph_cap *ocap, int mask)
  845. {
  846. struct ceph_cap *cap;
  847. struct rb_node *p;
  848. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  849. cap = rb_entry(p, struct ceph_cap, ci_node);
  850. if (cap != ocap &&
  851. (cap->implemented & ~cap->issued & mask))
  852. return 1;
  853. }
  854. return 0;
  855. }
  856. int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
  857. {
  858. struct inode *inode = &ci->vfs_inode;
  859. int ret;
  860. spin_lock(&ci->i_ceph_lock);
  861. ret = __ceph_caps_revoking_other(ci, NULL, mask);
  862. spin_unlock(&ci->i_ceph_lock);
  863. dout("ceph_caps_revoking %p %s = %d\n", inode,
  864. ceph_cap_string(mask), ret);
  865. return ret;
  866. }
  867. int __ceph_caps_used(struct ceph_inode_info *ci)
  868. {
  869. int used = 0;
  870. if (ci->i_pin_ref)
  871. used |= CEPH_CAP_PIN;
  872. if (ci->i_rd_ref)
  873. used |= CEPH_CAP_FILE_RD;
  874. if (ci->i_rdcache_ref ||
  875. (S_ISREG(ci->vfs_inode.i_mode) &&
  876. ci->vfs_inode.i_data.nrpages))
  877. used |= CEPH_CAP_FILE_CACHE;
  878. if (ci->i_wr_ref)
  879. used |= CEPH_CAP_FILE_WR;
  880. if (ci->i_wb_ref || ci->i_wrbuffer_ref)
  881. used |= CEPH_CAP_FILE_BUFFER;
  882. if (ci->i_fx_ref)
  883. used |= CEPH_CAP_FILE_EXCL;
  884. return used;
  885. }
  886. #define FMODE_WAIT_BIAS 1000
  887. /*
  888. * wanted, by virtue of open file modes
  889. */
  890. int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
  891. {
  892. const int PIN_SHIFT = ffs(CEPH_FILE_MODE_PIN);
  893. const int RD_SHIFT = ffs(CEPH_FILE_MODE_RD);
  894. const int WR_SHIFT = ffs(CEPH_FILE_MODE_WR);
  895. const int LAZY_SHIFT = ffs(CEPH_FILE_MODE_LAZY);
  896. struct ceph_mount_options *opt =
  897. ceph_inode_to_client(&ci->vfs_inode)->mount_options;
  898. unsigned long used_cutoff = jiffies - opt->caps_wanted_delay_max * HZ;
  899. unsigned long idle_cutoff = jiffies - opt->caps_wanted_delay_min * HZ;
  900. if (S_ISDIR(ci->vfs_inode.i_mode)) {
  901. int want = 0;
  902. /* use used_cutoff here, to keep dir's wanted caps longer */
  903. if (ci->i_nr_by_mode[RD_SHIFT] > 0 ||
  904. time_after(ci->i_last_rd, used_cutoff))
  905. want |= CEPH_CAP_ANY_SHARED;
  906. if (ci->i_nr_by_mode[WR_SHIFT] > 0 ||
  907. time_after(ci->i_last_wr, used_cutoff)) {
  908. want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
  909. if (opt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS)
  910. want |= CEPH_CAP_ANY_DIR_OPS;
  911. }
  912. if (want || ci->i_nr_by_mode[PIN_SHIFT] > 0)
  913. want |= CEPH_CAP_PIN;
  914. return want;
  915. } else {
  916. int bits = 0;
  917. if (ci->i_nr_by_mode[RD_SHIFT] > 0) {
  918. if (ci->i_nr_by_mode[RD_SHIFT] >= FMODE_WAIT_BIAS ||
  919. time_after(ci->i_last_rd, used_cutoff))
  920. bits |= 1 << RD_SHIFT;
  921. } else if (time_after(ci->i_last_rd, idle_cutoff)) {
  922. bits |= 1 << RD_SHIFT;
  923. }
  924. if (ci->i_nr_by_mode[WR_SHIFT] > 0) {
  925. if (ci->i_nr_by_mode[WR_SHIFT] >= FMODE_WAIT_BIAS ||
  926. time_after(ci->i_last_wr, used_cutoff))
  927. bits |= 1 << WR_SHIFT;
  928. } else if (time_after(ci->i_last_wr, idle_cutoff)) {
  929. bits |= 1 << WR_SHIFT;
  930. }
  931. /* check lazyio only when read/write is wanted */
  932. if ((bits & (CEPH_FILE_MODE_RDWR << 1)) &&
  933. ci->i_nr_by_mode[LAZY_SHIFT] > 0)
  934. bits |= 1 << LAZY_SHIFT;
  935. return bits ? ceph_caps_for_mode(bits >> 1) : 0;
  936. }
  937. }
  938. /*
  939. * wanted, by virtue of open file modes AND cap refs (buffered/cached data)
  940. */
  941. int __ceph_caps_wanted(struct ceph_inode_info *ci)
  942. {
  943. int w = __ceph_caps_file_wanted(ci) | __ceph_caps_used(ci);
  944. if (S_ISDIR(ci->vfs_inode.i_mode)) {
  945. /* we want EXCL if holding caps of dir ops */
  946. if (w & CEPH_CAP_ANY_DIR_OPS)
  947. w |= CEPH_CAP_FILE_EXCL;
  948. } else {
  949. /* we want EXCL if dirty data */
  950. if (w & CEPH_CAP_FILE_BUFFER)
  951. w |= CEPH_CAP_FILE_EXCL;
  952. }
  953. return w;
  954. }
  955. /*
  956. * Return caps we have registered with the MDS(s) as 'wanted'.
  957. */
  958. int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
  959. {
  960. struct ceph_cap *cap;
  961. struct rb_node *p;
  962. int mds_wanted = 0;
  963. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  964. cap = rb_entry(p, struct ceph_cap, ci_node);
  965. if (check && !__cap_is_valid(cap))
  966. continue;
  967. if (cap == ci->i_auth_cap)
  968. mds_wanted |= cap->mds_wanted;
  969. else
  970. mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
  971. }
  972. return mds_wanted;
  973. }
  974. int ceph_is_any_caps(struct inode *inode)
  975. {
  976. struct ceph_inode_info *ci = ceph_inode(inode);
  977. int ret;
  978. spin_lock(&ci->i_ceph_lock);
  979. ret = __ceph_is_any_real_caps(ci);
  980. spin_unlock(&ci->i_ceph_lock);
  981. return ret;
  982. }
  983. static void drop_inode_snap_realm(struct ceph_inode_info *ci)
  984. {
  985. struct ceph_snap_realm *realm = ci->i_snap_realm;
  986. spin_lock(&realm->inodes_with_caps_lock);
  987. list_del_init(&ci->i_snap_realm_item);
  988. ci->i_snap_realm_counter++;
  989. ci->i_snap_realm = NULL;
  990. if (realm->ino == ci->i_vino.ino)
  991. realm->inode = NULL;
  992. spin_unlock(&realm->inodes_with_caps_lock);
  993. ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
  994. realm);
  995. }
  996. /*
  997. * Remove a cap. Take steps to deal with a racing iterate_session_caps.
  998. *
  999. * caller should hold i_ceph_lock.
  1000. * caller will not hold session s_mutex if called from destroy_inode.
  1001. */
  1002. void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
  1003. {
  1004. struct ceph_mds_session *session = cap->session;
  1005. struct ceph_inode_info *ci = cap->ci;
  1006. struct ceph_mds_client *mdsc;
  1007. int removed = 0;
  1008. /* 'ci' being NULL means the remove have already occurred */
  1009. if (!ci) {
  1010. dout("%s: cap inode is NULL\n", __func__);
  1011. return;
  1012. }
  1013. dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
  1014. mdsc = ceph_inode_to_client(&ci->vfs_inode)->mdsc;
  1015. /* remove from inode's cap rbtree, and clear auth cap */
  1016. rb_erase(&cap->ci_node, &ci->i_caps);
  1017. if (ci->i_auth_cap == cap) {
  1018. WARN_ON_ONCE(!list_empty(&ci->i_dirty_item));
  1019. ci->i_auth_cap = NULL;
  1020. }
  1021. /* remove from session list */
  1022. spin_lock(&session->s_cap_lock);
  1023. if (session->s_cap_iterator == cap) {
  1024. /* not yet, we are iterating over this very cap */
  1025. dout("__ceph_remove_cap delaying %p removal from session %p\n",
  1026. cap, cap->session);
  1027. } else {
  1028. list_del_init(&cap->session_caps);
  1029. session->s_nr_caps--;
  1030. atomic64_dec(&mdsc->metric.total_caps);
  1031. cap->session = NULL;
  1032. removed = 1;
  1033. }
  1034. /* protect backpointer with s_cap_lock: see iterate_session_caps */
  1035. cap->ci = NULL;
  1036. /*
  1037. * s_cap_reconnect is protected by s_cap_lock. no one changes
  1038. * s_cap_gen while session is in the reconnect state.
  1039. */
  1040. if (queue_release &&
  1041. (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
  1042. cap->queue_release = 1;
  1043. if (removed) {
  1044. __ceph_queue_cap_release(session, cap);
  1045. removed = 0;
  1046. }
  1047. } else {
  1048. cap->queue_release = 0;
  1049. }
  1050. cap->cap_ino = ci->i_vino.ino;
  1051. spin_unlock(&session->s_cap_lock);
  1052. if (removed)
  1053. ceph_put_cap(mdsc, cap);
  1054. if (!__ceph_is_any_real_caps(ci)) {
  1055. /* when reconnect denied, we remove session caps forcibly,
  1056. * i_wr_ref can be non-zero. If there are ongoing write,
  1057. * keep i_snap_realm.
  1058. */
  1059. if (ci->i_wr_ref == 0 && ci->i_snap_realm)
  1060. drop_inode_snap_realm(ci);
  1061. __cap_delay_cancel(mdsc, ci);
  1062. }
  1063. }
  1064. struct cap_msg_args {
  1065. struct ceph_mds_session *session;
  1066. u64 ino, cid, follows;
  1067. u64 flush_tid, oldest_flush_tid, size, max_size;
  1068. u64 xattr_version;
  1069. u64 change_attr;
  1070. struct ceph_buffer *xattr_buf;
  1071. struct ceph_buffer *old_xattr_buf;
  1072. struct timespec64 atime, mtime, ctime, btime;
  1073. int op, caps, wanted, dirty;
  1074. u32 seq, issue_seq, mseq, time_warp_seq;
  1075. u32 flags;
  1076. kuid_t uid;
  1077. kgid_t gid;
  1078. umode_t mode;
  1079. bool inline_data;
  1080. bool wake;
  1081. };
  1082. /*
  1083. * cap struct size + flock buffer size + inline version + inline data size +
  1084. * osd_epoch_barrier + oldest_flush_tid
  1085. */
  1086. #define CAP_MSG_SIZE (sizeof(struct ceph_mds_caps) + \
  1087. 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4)
  1088. /* Marshal up the cap msg to the MDS */
  1089. static void encode_cap_msg(struct ceph_msg *msg, struct cap_msg_args *arg)
  1090. {
  1091. struct ceph_mds_caps *fc;
  1092. void *p;
  1093. struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
  1094. dout("%s %s %llx %llx caps %s wanted %s dirty %s seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu xattr_ver %llu xattr_len %d\n",
  1095. __func__, ceph_cap_op_name(arg->op), arg->cid, arg->ino,
  1096. ceph_cap_string(arg->caps), ceph_cap_string(arg->wanted),
  1097. ceph_cap_string(arg->dirty), arg->seq, arg->issue_seq,
  1098. arg->flush_tid, arg->oldest_flush_tid, arg->mseq, arg->follows,
  1099. arg->size, arg->max_size, arg->xattr_version,
  1100. arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
  1101. msg->hdr.version = cpu_to_le16(10);
  1102. msg->hdr.tid = cpu_to_le64(arg->flush_tid);
  1103. fc = msg->front.iov_base;
  1104. memset(fc, 0, sizeof(*fc));
  1105. fc->cap_id = cpu_to_le64(arg->cid);
  1106. fc->op = cpu_to_le32(arg->op);
  1107. fc->seq = cpu_to_le32(arg->seq);
  1108. fc->issue_seq = cpu_to_le32(arg->issue_seq);
  1109. fc->migrate_seq = cpu_to_le32(arg->mseq);
  1110. fc->caps = cpu_to_le32(arg->caps);
  1111. fc->wanted = cpu_to_le32(arg->wanted);
  1112. fc->dirty = cpu_to_le32(arg->dirty);
  1113. fc->ino = cpu_to_le64(arg->ino);
  1114. fc->snap_follows = cpu_to_le64(arg->follows);
  1115. fc->size = cpu_to_le64(arg->size);
  1116. fc->max_size = cpu_to_le64(arg->max_size);
  1117. ceph_encode_timespec64(&fc->mtime, &arg->mtime);
  1118. ceph_encode_timespec64(&fc->atime, &arg->atime);
  1119. ceph_encode_timespec64(&fc->ctime, &arg->ctime);
  1120. fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
  1121. fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
  1122. fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
  1123. fc->mode = cpu_to_le32(arg->mode);
  1124. fc->xattr_version = cpu_to_le64(arg->xattr_version);
  1125. if (arg->xattr_buf) {
  1126. msg->middle = ceph_buffer_get(arg->xattr_buf);
  1127. fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
  1128. msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
  1129. }
  1130. p = fc + 1;
  1131. /* flock buffer size (version 2) */
  1132. ceph_encode_32(&p, 0);
  1133. /* inline version (version 4) */
  1134. ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
  1135. /* inline data size */
  1136. ceph_encode_32(&p, 0);
  1137. /*
  1138. * osd_epoch_barrier (version 5)
  1139. * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
  1140. * case it was recently changed
  1141. */
  1142. ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
  1143. /* oldest_flush_tid (version 6) */
  1144. ceph_encode_64(&p, arg->oldest_flush_tid);
  1145. /*
  1146. * caller_uid/caller_gid (version 7)
  1147. *
  1148. * Currently, we don't properly track which caller dirtied the caps
  1149. * last, and force a flush of them when there is a conflict. For now,
  1150. * just set this to 0:0, to emulate how the MDS has worked up to now.
  1151. */
  1152. ceph_encode_32(&p, 0);
  1153. ceph_encode_32(&p, 0);
  1154. /* pool namespace (version 8) (mds always ignores this) */
  1155. ceph_encode_32(&p, 0);
  1156. /* btime and change_attr (version 9) */
  1157. ceph_encode_timespec64(p, &arg->btime);
  1158. p += sizeof(struct ceph_timespec);
  1159. ceph_encode_64(&p, arg->change_attr);
  1160. /* Advisory flags (version 10) */
  1161. ceph_encode_32(&p, arg->flags);
  1162. }
  1163. /*
  1164. * Queue cap releases when an inode is dropped from our cache.
  1165. */
  1166. void __ceph_remove_caps(struct ceph_inode_info *ci)
  1167. {
  1168. struct rb_node *p;
  1169. /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
  1170. * may call __ceph_caps_issued_mask() on a freeing inode. */
  1171. spin_lock(&ci->i_ceph_lock);
  1172. p = rb_first(&ci->i_caps);
  1173. while (p) {
  1174. struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
  1175. p = rb_next(p);
  1176. __ceph_remove_cap(cap, true);
  1177. }
  1178. spin_unlock(&ci->i_ceph_lock);
  1179. }
  1180. /*
  1181. * Prepare to send a cap message to an MDS. Update the cap state, and populate
  1182. * the arg struct with the parameters that will need to be sent. This should
  1183. * be done under the i_ceph_lock to guard against changes to cap state.
  1184. *
  1185. * Make note of max_size reported/requested from mds, revoked caps
  1186. * that have now been implemented.
  1187. */
  1188. static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,
  1189. int op, int flags, int used, int want, int retain,
  1190. int flushing, u64 flush_tid, u64 oldest_flush_tid)
  1191. {
  1192. struct ceph_inode_info *ci = cap->ci;
  1193. struct inode *inode = &ci->vfs_inode;
  1194. int held, revoking;
  1195. lockdep_assert_held(&ci->i_ceph_lock);
  1196. held = cap->issued | cap->implemented;
  1197. revoking = cap->implemented & ~cap->issued;
  1198. retain &= ~revoking;
  1199. dout("%s %p cap %p session %p %s -> %s (revoking %s)\n",
  1200. __func__, inode, cap, cap->session,
  1201. ceph_cap_string(held), ceph_cap_string(held & retain),
  1202. ceph_cap_string(revoking));
  1203. BUG_ON((retain & CEPH_CAP_PIN) == 0);
  1204. ci->i_ceph_flags &= ~CEPH_I_FLUSH;
  1205. cap->issued &= retain; /* drop bits we don't want */
  1206. /*
  1207. * Wake up any waiters on wanted -> needed transition. This is due to
  1208. * the weird transition from buffered to sync IO... we need to flush
  1209. * dirty pages _before_ allowing sync writes to avoid reordering.
  1210. */
  1211. arg->wake = cap->implemented & ~cap->issued;
  1212. cap->implemented &= cap->issued | used;
  1213. cap->mds_wanted = want;
  1214. arg->session = cap->session;
  1215. arg->ino = ceph_vino(inode).ino;
  1216. arg->cid = cap->cap_id;
  1217. arg->follows = flushing ? ci->i_head_snapc->seq : 0;
  1218. arg->flush_tid = flush_tid;
  1219. arg->oldest_flush_tid = oldest_flush_tid;
  1220. arg->size = inode->i_size;
  1221. ci->i_reported_size = arg->size;
  1222. arg->max_size = ci->i_wanted_max_size;
  1223. if (cap == ci->i_auth_cap) {
  1224. if (want & CEPH_CAP_ANY_FILE_WR)
  1225. ci->i_requested_max_size = arg->max_size;
  1226. else
  1227. ci->i_requested_max_size = 0;
  1228. }
  1229. if (flushing & CEPH_CAP_XATTR_EXCL) {
  1230. arg->old_xattr_buf = __ceph_build_xattrs_blob(ci);
  1231. arg->xattr_version = ci->i_xattrs.version;
  1232. arg->xattr_buf = ci->i_xattrs.blob;
  1233. } else {
  1234. arg->xattr_buf = NULL;
  1235. arg->old_xattr_buf = NULL;
  1236. }
  1237. arg->mtime = inode->i_mtime;
  1238. arg->atime = inode->i_atime;
  1239. arg->ctime = inode->i_ctime;
  1240. arg->btime = ci->i_btime;
  1241. arg->change_attr = inode_peek_iversion_raw(inode);
  1242. arg->op = op;
  1243. arg->caps = cap->implemented;
  1244. arg->wanted = want;
  1245. arg->dirty = flushing;
  1246. arg->seq = cap->seq;
  1247. arg->issue_seq = cap->issue_seq;
  1248. arg->mseq = cap->mseq;
  1249. arg->time_warp_seq = ci->i_time_warp_seq;
  1250. arg->uid = inode->i_uid;
  1251. arg->gid = inode->i_gid;
  1252. arg->mode = inode->i_mode;
  1253. arg->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
  1254. if (!(flags & CEPH_CLIENT_CAPS_PENDING_CAPSNAP) &&
  1255. !list_empty(&ci->i_cap_snaps)) {
  1256. struct ceph_cap_snap *capsnap;
  1257. list_for_each_entry_reverse(capsnap, &ci->i_cap_snaps, ci_item) {
  1258. if (capsnap->cap_flush.tid)
  1259. break;
  1260. if (capsnap->need_flush) {
  1261. flags |= CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. arg->flags = flags;
  1267. }
  1268. /*
  1269. * Send a cap msg on the given inode.
  1270. *
  1271. * Caller should hold snap_rwsem (read), s_mutex.
  1272. */
  1273. static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci)
  1274. {
  1275. struct ceph_msg *msg;
  1276. struct inode *inode = &ci->vfs_inode;
  1277. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, CAP_MSG_SIZE, GFP_NOFS, false);
  1278. if (!msg) {
  1279. pr_err("error allocating cap msg: ino (%llx.%llx) flushing %s tid %llu, requeuing cap.\n",
  1280. ceph_vinop(inode), ceph_cap_string(arg->dirty),
  1281. arg->flush_tid);
  1282. spin_lock(&ci->i_ceph_lock);
  1283. __cap_delay_requeue(arg->session->s_mdsc, ci);
  1284. spin_unlock(&ci->i_ceph_lock);
  1285. return;
  1286. }
  1287. encode_cap_msg(msg, arg);
  1288. ceph_con_send(&arg->session->s_con, msg);
  1289. ceph_buffer_put(arg->old_xattr_buf);
  1290. if (arg->wake)
  1291. wake_up_all(&ci->i_cap_wq);
  1292. }
  1293. static inline int __send_flush_snap(struct inode *inode,
  1294. struct ceph_mds_session *session,
  1295. struct ceph_cap_snap *capsnap,
  1296. u32 mseq, u64 oldest_flush_tid)
  1297. {
  1298. struct cap_msg_args arg;
  1299. struct ceph_msg *msg;
  1300. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, CAP_MSG_SIZE, GFP_NOFS, false);
  1301. if (!msg)
  1302. return -ENOMEM;
  1303. arg.session = session;
  1304. arg.ino = ceph_vino(inode).ino;
  1305. arg.cid = 0;
  1306. arg.follows = capsnap->follows;
  1307. arg.flush_tid = capsnap->cap_flush.tid;
  1308. arg.oldest_flush_tid = oldest_flush_tid;
  1309. arg.size = capsnap->size;
  1310. arg.max_size = 0;
  1311. arg.xattr_version = capsnap->xattr_version;
  1312. arg.xattr_buf = capsnap->xattr_blob;
  1313. arg.old_xattr_buf = NULL;
  1314. arg.atime = capsnap->atime;
  1315. arg.mtime = capsnap->mtime;
  1316. arg.ctime = capsnap->ctime;
  1317. arg.btime = capsnap->btime;
  1318. arg.change_attr = capsnap->change_attr;
  1319. arg.op = CEPH_CAP_OP_FLUSHSNAP;
  1320. arg.caps = capsnap->issued;
  1321. arg.wanted = 0;
  1322. arg.dirty = capsnap->dirty;
  1323. arg.seq = 0;
  1324. arg.issue_seq = 0;
  1325. arg.mseq = mseq;
  1326. arg.time_warp_seq = capsnap->time_warp_seq;
  1327. arg.uid = capsnap->uid;
  1328. arg.gid = capsnap->gid;
  1329. arg.mode = capsnap->mode;
  1330. arg.inline_data = capsnap->inline_data;
  1331. arg.flags = 0;
  1332. arg.wake = false;
  1333. encode_cap_msg(msg, &arg);
  1334. ceph_con_send(&arg.session->s_con, msg);
  1335. return 0;
  1336. }
  1337. /*
  1338. * When a snapshot is taken, clients accumulate dirty metadata on
  1339. * inodes with capabilities in ceph_cap_snaps to describe the file
  1340. * state at the time the snapshot was taken. This must be flushed
  1341. * asynchronously back to the MDS once sync writes complete and dirty
  1342. * data is written out.
  1343. *
  1344. * Called under i_ceph_lock. Takes s_mutex as needed.
  1345. */
  1346. static void __ceph_flush_snaps(struct ceph_inode_info *ci,
  1347. struct ceph_mds_session *session)
  1348. __releases(ci->i_ceph_lock)
  1349. __acquires(ci->i_ceph_lock)
  1350. {
  1351. struct inode *inode = &ci->vfs_inode;
  1352. struct ceph_mds_client *mdsc = session->s_mdsc;
  1353. struct ceph_cap_snap *capsnap;
  1354. u64 oldest_flush_tid = 0;
  1355. u64 first_tid = 1, last_tid = 0;
  1356. dout("__flush_snaps %p session %p\n", inode, session);
  1357. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1358. /*
  1359. * we need to wait for sync writes to complete and for dirty
  1360. * pages to be written out.
  1361. */
  1362. if (capsnap->dirty_pages || capsnap->writing)
  1363. break;
  1364. /* should be removed by ceph_try_drop_cap_snap() */
  1365. BUG_ON(!capsnap->need_flush);
  1366. /* only flush each capsnap once */
  1367. if (capsnap->cap_flush.tid > 0) {
  1368. dout(" already flushed %p, skipping\n", capsnap);
  1369. continue;
  1370. }
  1371. spin_lock(&mdsc->cap_dirty_lock);
  1372. capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
  1373. list_add_tail(&capsnap->cap_flush.g_list,
  1374. &mdsc->cap_flush_list);
  1375. if (oldest_flush_tid == 0)
  1376. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1377. if (list_empty(&ci->i_flushing_item)) {
  1378. list_add_tail(&ci->i_flushing_item,
  1379. &session->s_cap_flushing);
  1380. }
  1381. spin_unlock(&mdsc->cap_dirty_lock);
  1382. list_add_tail(&capsnap->cap_flush.i_list,
  1383. &ci->i_cap_flush_list);
  1384. if (first_tid == 1)
  1385. first_tid = capsnap->cap_flush.tid;
  1386. last_tid = capsnap->cap_flush.tid;
  1387. }
  1388. ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
  1389. while (first_tid <= last_tid) {
  1390. struct ceph_cap *cap = ci->i_auth_cap;
  1391. struct ceph_cap_flush *cf;
  1392. int ret;
  1393. if (!(cap && cap->session == session)) {
  1394. dout("__flush_snaps %p auth cap %p not mds%d, "
  1395. "stop\n", inode, cap, session->s_mds);
  1396. break;
  1397. }
  1398. ret = -ENOENT;
  1399. list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
  1400. if (cf->tid >= first_tid) {
  1401. ret = 0;
  1402. break;
  1403. }
  1404. }
  1405. if (ret < 0)
  1406. break;
  1407. first_tid = cf->tid + 1;
  1408. capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
  1409. refcount_inc(&capsnap->nref);
  1410. spin_unlock(&ci->i_ceph_lock);
  1411. dout("__flush_snaps %p capsnap %p tid %llu %s\n",
  1412. inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
  1413. ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
  1414. oldest_flush_tid);
  1415. if (ret < 0) {
  1416. pr_err("__flush_snaps: error sending cap flushsnap, "
  1417. "ino (%llx.%llx) tid %llu follows %llu\n",
  1418. ceph_vinop(inode), cf->tid, capsnap->follows);
  1419. }
  1420. ceph_put_cap_snap(capsnap);
  1421. spin_lock(&ci->i_ceph_lock);
  1422. }
  1423. }
  1424. void ceph_flush_snaps(struct ceph_inode_info *ci,
  1425. struct ceph_mds_session **psession)
  1426. {
  1427. struct inode *inode = &ci->vfs_inode;
  1428. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  1429. struct ceph_mds_session *session = NULL;
  1430. int mds;
  1431. dout("ceph_flush_snaps %p\n", inode);
  1432. if (psession)
  1433. session = *psession;
  1434. retry:
  1435. spin_lock(&ci->i_ceph_lock);
  1436. if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
  1437. dout(" no capsnap needs flush, doing nothing\n");
  1438. goto out;
  1439. }
  1440. if (!ci->i_auth_cap) {
  1441. dout(" no auth cap (migrating?), doing nothing\n");
  1442. goto out;
  1443. }
  1444. mds = ci->i_auth_cap->session->s_mds;
  1445. if (session && session->s_mds != mds) {
  1446. dout(" oops, wrong session %p mutex\n", session);
  1447. mutex_unlock(&session->s_mutex);
  1448. ceph_put_mds_session(session);
  1449. session = NULL;
  1450. }
  1451. if (!session) {
  1452. spin_unlock(&ci->i_ceph_lock);
  1453. mutex_lock(&mdsc->mutex);
  1454. session = __ceph_lookup_mds_session(mdsc, mds);
  1455. mutex_unlock(&mdsc->mutex);
  1456. if (session) {
  1457. dout(" inverting session/ino locks on %p\n", session);
  1458. mutex_lock(&session->s_mutex);
  1459. }
  1460. goto retry;
  1461. }
  1462. // make sure flushsnap messages are sent in proper order.
  1463. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
  1464. __kick_flushing_caps(mdsc, session, ci, 0);
  1465. __ceph_flush_snaps(ci, session);
  1466. out:
  1467. spin_unlock(&ci->i_ceph_lock);
  1468. if (psession) {
  1469. *psession = session;
  1470. } else if (session) {
  1471. mutex_unlock(&session->s_mutex);
  1472. ceph_put_mds_session(session);
  1473. }
  1474. /* we flushed them all; remove this inode from the queue */
  1475. spin_lock(&mdsc->snap_flush_lock);
  1476. list_del_init(&ci->i_snap_flush_item);
  1477. spin_unlock(&mdsc->snap_flush_lock);
  1478. }
  1479. /*
  1480. * Mark caps dirty. If inode is newly dirty, return the dirty flags.
  1481. * Caller is then responsible for calling __mark_inode_dirty with the
  1482. * returned flags value.
  1483. */
  1484. int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
  1485. struct ceph_cap_flush **pcf)
  1486. {
  1487. struct ceph_mds_client *mdsc =
  1488. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  1489. struct inode *inode = &ci->vfs_inode;
  1490. int was = ci->i_dirty_caps;
  1491. int dirty = 0;
  1492. lockdep_assert_held(&ci->i_ceph_lock);
  1493. if (!ci->i_auth_cap) {
  1494. pr_warn("__mark_dirty_caps %p %llx mask %s, "
  1495. "but no auth cap (session was closed?)\n",
  1496. inode, ceph_ino(inode), ceph_cap_string(mask));
  1497. return 0;
  1498. }
  1499. dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
  1500. ceph_cap_string(mask), ceph_cap_string(was),
  1501. ceph_cap_string(was | mask));
  1502. ci->i_dirty_caps |= mask;
  1503. if (was == 0) {
  1504. struct ceph_mds_session *session = ci->i_auth_cap->session;
  1505. WARN_ON_ONCE(ci->i_prealloc_cap_flush);
  1506. swap(ci->i_prealloc_cap_flush, *pcf);
  1507. if (!ci->i_head_snapc) {
  1508. WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
  1509. ci->i_head_snapc = ceph_get_snap_context(
  1510. ci->i_snap_realm->cached_context);
  1511. }
  1512. dout(" inode %p now dirty snapc %p auth cap %p\n",
  1513. &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
  1514. BUG_ON(!list_empty(&ci->i_dirty_item));
  1515. spin_lock(&mdsc->cap_dirty_lock);
  1516. list_add(&ci->i_dirty_item, &session->s_cap_dirty);
  1517. spin_unlock(&mdsc->cap_dirty_lock);
  1518. if (ci->i_flushing_caps == 0) {
  1519. ihold(inode);
  1520. dirty |= I_DIRTY_SYNC;
  1521. }
  1522. } else {
  1523. WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
  1524. }
  1525. BUG_ON(list_empty(&ci->i_dirty_item));
  1526. if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
  1527. (mask & CEPH_CAP_FILE_BUFFER))
  1528. dirty |= I_DIRTY_DATASYNC;
  1529. __cap_delay_requeue(mdsc, ci);
  1530. return dirty;
  1531. }
  1532. struct ceph_cap_flush *ceph_alloc_cap_flush(void)
  1533. {
  1534. struct ceph_cap_flush *cf;
  1535. cf = kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
  1536. if (!cf)
  1537. return NULL;
  1538. cf->is_capsnap = false;
  1539. return cf;
  1540. }
  1541. void ceph_free_cap_flush(struct ceph_cap_flush *cf)
  1542. {
  1543. if (cf)
  1544. kmem_cache_free(ceph_cap_flush_cachep, cf);
  1545. }
  1546. static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
  1547. {
  1548. if (!list_empty(&mdsc->cap_flush_list)) {
  1549. struct ceph_cap_flush *cf =
  1550. list_first_entry(&mdsc->cap_flush_list,
  1551. struct ceph_cap_flush, g_list);
  1552. return cf->tid;
  1553. }
  1554. return 0;
  1555. }
  1556. /*
  1557. * Remove cap_flush from the mdsc's or inode's flushing cap list.
  1558. * Return true if caller needs to wake up flush waiters.
  1559. */
  1560. static bool __detach_cap_flush_from_mdsc(struct ceph_mds_client *mdsc,
  1561. struct ceph_cap_flush *cf)
  1562. {
  1563. struct ceph_cap_flush *prev;
  1564. bool wake = cf->wake;
  1565. if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
  1566. prev = list_prev_entry(cf, g_list);
  1567. prev->wake = true;
  1568. wake = false;
  1569. }
  1570. list_del_init(&cf->g_list);
  1571. return wake;
  1572. }
  1573. static bool __detach_cap_flush_from_ci(struct ceph_inode_info *ci,
  1574. struct ceph_cap_flush *cf)
  1575. {
  1576. struct ceph_cap_flush *prev;
  1577. bool wake = cf->wake;
  1578. if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
  1579. prev = list_prev_entry(cf, i_list);
  1580. prev->wake = true;
  1581. wake = false;
  1582. }
  1583. list_del_init(&cf->i_list);
  1584. return wake;
  1585. }
  1586. /*
  1587. * Add dirty inode to the flushing list. Assigned a seq number so we
  1588. * can wait for caps to flush without starving.
  1589. *
  1590. * Called under i_ceph_lock. Returns the flush tid.
  1591. */
  1592. static u64 __mark_caps_flushing(struct inode *inode,
  1593. struct ceph_mds_session *session, bool wake,
  1594. u64 *oldest_flush_tid)
  1595. {
  1596. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1597. struct ceph_inode_info *ci = ceph_inode(inode);
  1598. struct ceph_cap_flush *cf = NULL;
  1599. int flushing;
  1600. lockdep_assert_held(&ci->i_ceph_lock);
  1601. BUG_ON(ci->i_dirty_caps == 0);
  1602. BUG_ON(list_empty(&ci->i_dirty_item));
  1603. BUG_ON(!ci->i_prealloc_cap_flush);
  1604. flushing = ci->i_dirty_caps;
  1605. dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
  1606. ceph_cap_string(flushing),
  1607. ceph_cap_string(ci->i_flushing_caps),
  1608. ceph_cap_string(ci->i_flushing_caps | flushing));
  1609. ci->i_flushing_caps |= flushing;
  1610. ci->i_dirty_caps = 0;
  1611. dout(" inode %p now !dirty\n", inode);
  1612. swap(cf, ci->i_prealloc_cap_flush);
  1613. cf->caps = flushing;
  1614. cf->wake = wake;
  1615. spin_lock(&mdsc->cap_dirty_lock);
  1616. list_del_init(&ci->i_dirty_item);
  1617. cf->tid = ++mdsc->last_cap_flush_tid;
  1618. list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
  1619. *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1620. if (list_empty(&ci->i_flushing_item)) {
  1621. list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1622. mdsc->num_cap_flushing++;
  1623. }
  1624. spin_unlock(&mdsc->cap_dirty_lock);
  1625. list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
  1626. return cf->tid;
  1627. }
  1628. /*
  1629. * try to invalidate mapping pages without blocking.
  1630. */
  1631. static int try_nonblocking_invalidate(struct inode *inode)
  1632. __releases(ci->i_ceph_lock)
  1633. __acquires(ci->i_ceph_lock)
  1634. {
  1635. struct ceph_inode_info *ci = ceph_inode(inode);
  1636. u32 invalidating_gen = ci->i_rdcache_gen;
  1637. spin_unlock(&ci->i_ceph_lock);
  1638. ceph_fscache_invalidate(inode);
  1639. invalidate_mapping_pages(&inode->i_data, 0, -1);
  1640. spin_lock(&ci->i_ceph_lock);
  1641. if (inode->i_data.nrpages == 0 &&
  1642. invalidating_gen == ci->i_rdcache_gen) {
  1643. /* success. */
  1644. dout("try_nonblocking_invalidate %p success\n", inode);
  1645. /* save any racing async invalidate some trouble */
  1646. ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
  1647. return 0;
  1648. }
  1649. dout("try_nonblocking_invalidate %p failed\n", inode);
  1650. return -1;
  1651. }
  1652. bool __ceph_should_report_size(struct ceph_inode_info *ci)
  1653. {
  1654. loff_t size = ci->vfs_inode.i_size;
  1655. /* mds will adjust max size according to the reported size */
  1656. if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
  1657. return false;
  1658. if (size >= ci->i_max_size)
  1659. return true;
  1660. /* half of previous max_size increment has been used */
  1661. if (ci->i_max_size > ci->i_reported_size &&
  1662. (size << 1) >= ci->i_max_size + ci->i_reported_size)
  1663. return true;
  1664. return false;
  1665. }
  1666. /*
  1667. * Swiss army knife function to examine currently used and wanted
  1668. * versus held caps. Release, flush, ack revoked caps to mds as
  1669. * appropriate.
  1670. *
  1671. * CHECK_CAPS_AUTHONLY - we should only check the auth cap
  1672. * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
  1673. * further delay.
  1674. */
  1675. void ceph_check_caps(struct ceph_inode_info *ci, int flags,
  1676. struct ceph_mds_session *session)
  1677. {
  1678. struct inode *inode = &ci->vfs_inode;
  1679. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  1680. struct ceph_cap *cap;
  1681. u64 flush_tid, oldest_flush_tid;
  1682. int file_wanted, used, cap_used;
  1683. int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
  1684. int issued, implemented, want, retain, revoking, flushing = 0;
  1685. int mds = -1; /* keep track of how far we've gone through i_caps list
  1686. to avoid an infinite loop on retry */
  1687. struct rb_node *p;
  1688. bool queue_invalidate = false;
  1689. bool tried_invalidate = false;
  1690. spin_lock(&ci->i_ceph_lock);
  1691. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  1692. flags |= CHECK_CAPS_FLUSH;
  1693. goto retry_locked;
  1694. retry:
  1695. spin_lock(&ci->i_ceph_lock);
  1696. retry_locked:
  1697. /* Caps wanted by virtue of active open files. */
  1698. file_wanted = __ceph_caps_file_wanted(ci);
  1699. /* Caps which have active references against them */
  1700. used = __ceph_caps_used(ci);
  1701. /*
  1702. * "issued" represents the current caps that the MDS wants us to have.
  1703. * "implemented" is the set that we have been granted, and includes the
  1704. * ones that have not yet been returned to the MDS (the "revoking" set,
  1705. * usually because they have outstanding references).
  1706. */
  1707. issued = __ceph_caps_issued(ci, &implemented);
  1708. revoking = implemented & ~issued;
  1709. want = file_wanted;
  1710. /* The ones we currently want to retain (may be adjusted below) */
  1711. retain = file_wanted | used | CEPH_CAP_PIN;
  1712. if (!mdsc->stopping && inode->i_nlink > 0) {
  1713. if (file_wanted) {
  1714. retain |= CEPH_CAP_ANY; /* be greedy */
  1715. } else if (S_ISDIR(inode->i_mode) &&
  1716. (issued & CEPH_CAP_FILE_SHARED) &&
  1717. __ceph_dir_is_complete(ci)) {
  1718. /*
  1719. * If a directory is complete, we want to keep
  1720. * the exclusive cap. So that MDS does not end up
  1721. * revoking the shared cap on every create/unlink
  1722. * operation.
  1723. */
  1724. if (IS_RDONLY(inode)) {
  1725. want = CEPH_CAP_ANY_SHARED;
  1726. } else {
  1727. want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
  1728. }
  1729. retain |= want;
  1730. } else {
  1731. retain |= CEPH_CAP_ANY_SHARED;
  1732. /*
  1733. * keep RD only if we didn't have the file open RW,
  1734. * because then the mds would revoke it anyway to
  1735. * journal max_size=0.
  1736. */
  1737. if (ci->i_max_size == 0)
  1738. retain |= CEPH_CAP_ANY_RD;
  1739. }
  1740. }
  1741. dout("check_caps %p file_want %s used %s dirty %s flushing %s"
  1742. " issued %s revoking %s retain %s %s%s\n", inode,
  1743. ceph_cap_string(file_wanted),
  1744. ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
  1745. ceph_cap_string(ci->i_flushing_caps),
  1746. ceph_cap_string(issued), ceph_cap_string(revoking),
  1747. ceph_cap_string(retain),
  1748. (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
  1749. (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
  1750. /*
  1751. * If we no longer need to hold onto old our caps, and we may
  1752. * have cached pages, but don't want them, then try to invalidate.
  1753. * If we fail, it's because pages are locked.... try again later.
  1754. */
  1755. if ((!(flags & CHECK_CAPS_NOINVAL) || mdsc->stopping) &&
  1756. S_ISREG(inode->i_mode) &&
  1757. !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
  1758. inode->i_data.nrpages && /* have cached pages */
  1759. (revoking & (CEPH_CAP_FILE_CACHE|
  1760. CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
  1761. !tried_invalidate) {
  1762. dout("check_caps trying to invalidate on %p\n", inode);
  1763. if (try_nonblocking_invalidate(inode) < 0) {
  1764. dout("check_caps queuing invalidate\n");
  1765. queue_invalidate = true;
  1766. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  1767. }
  1768. tried_invalidate = true;
  1769. goto retry_locked;
  1770. }
  1771. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  1772. int mflags = 0;
  1773. struct cap_msg_args arg;
  1774. cap = rb_entry(p, struct ceph_cap, ci_node);
  1775. /* avoid looping forever */
  1776. if (mds >= cap->mds ||
  1777. ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
  1778. continue;
  1779. /* NOTE: no side-effects allowed, until we take s_mutex */
  1780. /*
  1781. * If we have an auth cap, we don't need to consider any
  1782. * overlapping caps as used.
  1783. */
  1784. cap_used = used;
  1785. if (ci->i_auth_cap && cap != ci->i_auth_cap)
  1786. cap_used &= ~ci->i_auth_cap->issued;
  1787. revoking = cap->implemented & ~cap->issued;
  1788. dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
  1789. cap->mds, cap, ceph_cap_string(cap_used),
  1790. ceph_cap_string(cap->issued),
  1791. ceph_cap_string(cap->implemented),
  1792. ceph_cap_string(revoking));
  1793. if (cap == ci->i_auth_cap &&
  1794. (cap->issued & CEPH_CAP_FILE_WR)) {
  1795. /* request larger max_size from MDS? */
  1796. if (ci->i_wanted_max_size > ci->i_max_size &&
  1797. ci->i_wanted_max_size > ci->i_requested_max_size) {
  1798. dout("requesting new max_size\n");
  1799. goto ack;
  1800. }
  1801. /* approaching file_max? */
  1802. if (__ceph_should_report_size(ci)) {
  1803. dout("i_size approaching max_size\n");
  1804. goto ack;
  1805. }
  1806. }
  1807. /* flush anything dirty? */
  1808. if (cap == ci->i_auth_cap) {
  1809. if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
  1810. dout("flushing dirty caps\n");
  1811. goto ack;
  1812. }
  1813. if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
  1814. dout("flushing snap caps\n");
  1815. goto ack;
  1816. }
  1817. }
  1818. /* completed revocation? going down and there are no caps? */
  1819. if (revoking && (revoking & cap_used) == 0) {
  1820. dout("completed revocation of %s\n",
  1821. ceph_cap_string(cap->implemented & ~cap->issued));
  1822. goto ack;
  1823. }
  1824. /* want more caps from mds? */
  1825. if (want & ~cap->mds_wanted) {
  1826. if (want & ~(cap->mds_wanted | cap->issued))
  1827. goto ack;
  1828. if (!__cap_is_valid(cap))
  1829. goto ack;
  1830. }
  1831. /* things we might delay */
  1832. if ((cap->issued & ~retain) == 0)
  1833. continue; /* nope, all good */
  1834. ack:
  1835. if (session && session != cap->session) {
  1836. dout("oops, wrong session %p mutex\n", session);
  1837. mutex_unlock(&session->s_mutex);
  1838. session = NULL;
  1839. }
  1840. if (!session) {
  1841. session = cap->session;
  1842. if (mutex_trylock(&session->s_mutex) == 0) {
  1843. dout("inverting session/ino locks on %p\n",
  1844. session);
  1845. session = ceph_get_mds_session(session);
  1846. spin_unlock(&ci->i_ceph_lock);
  1847. if (took_snap_rwsem) {
  1848. up_read(&mdsc->snap_rwsem);
  1849. took_snap_rwsem = 0;
  1850. }
  1851. if (session) {
  1852. mutex_lock(&session->s_mutex);
  1853. ceph_put_mds_session(session);
  1854. } else {
  1855. /*
  1856. * Because we take the reference while
  1857. * holding the i_ceph_lock, it should
  1858. * never be NULL. Throw a warning if it
  1859. * ever is.
  1860. */
  1861. WARN_ON_ONCE(true);
  1862. }
  1863. goto retry;
  1864. }
  1865. }
  1866. /* kick flushing and flush snaps before sending normal
  1867. * cap message */
  1868. if (cap == ci->i_auth_cap &&
  1869. (ci->i_ceph_flags &
  1870. (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
  1871. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
  1872. __kick_flushing_caps(mdsc, session, ci, 0);
  1873. if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
  1874. __ceph_flush_snaps(ci, session);
  1875. goto retry_locked;
  1876. }
  1877. /* take snap_rwsem after session mutex */
  1878. if (!took_snap_rwsem) {
  1879. if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
  1880. dout("inverting snap/in locks on %p\n",
  1881. inode);
  1882. spin_unlock(&ci->i_ceph_lock);
  1883. down_read(&mdsc->snap_rwsem);
  1884. took_snap_rwsem = 1;
  1885. goto retry;
  1886. }
  1887. took_snap_rwsem = 1;
  1888. }
  1889. if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
  1890. flushing = ci->i_dirty_caps;
  1891. flush_tid = __mark_caps_flushing(inode, session, false,
  1892. &oldest_flush_tid);
  1893. if (flags & CHECK_CAPS_FLUSH &&
  1894. list_empty(&session->s_cap_dirty))
  1895. mflags |= CEPH_CLIENT_CAPS_SYNC;
  1896. } else {
  1897. flushing = 0;
  1898. flush_tid = 0;
  1899. spin_lock(&mdsc->cap_dirty_lock);
  1900. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1901. spin_unlock(&mdsc->cap_dirty_lock);
  1902. }
  1903. mds = cap->mds; /* remember mds, so we don't repeat */
  1904. __prep_cap(&arg, cap, CEPH_CAP_OP_UPDATE, mflags, cap_used,
  1905. want, retain, flushing, flush_tid, oldest_flush_tid);
  1906. spin_unlock(&ci->i_ceph_lock);
  1907. __send_cap(&arg, ci);
  1908. goto retry; /* retake i_ceph_lock and restart our cap scan. */
  1909. }
  1910. /* periodically re-calculate caps wanted by open files */
  1911. if (__ceph_is_any_real_caps(ci) &&
  1912. list_empty(&ci->i_cap_delay_list) &&
  1913. (file_wanted & ~CEPH_CAP_PIN) &&
  1914. !(used & (CEPH_CAP_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
  1915. __cap_delay_requeue(mdsc, ci);
  1916. }
  1917. spin_unlock(&ci->i_ceph_lock);
  1918. if (queue_invalidate)
  1919. ceph_queue_invalidate(inode);
  1920. if (session)
  1921. mutex_unlock(&session->s_mutex);
  1922. if (took_snap_rwsem)
  1923. up_read(&mdsc->snap_rwsem);
  1924. }
  1925. /*
  1926. * Try to flush dirty caps back to the auth mds.
  1927. */
  1928. static int try_flush_caps(struct inode *inode, u64 *ptid)
  1929. {
  1930. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1931. struct ceph_inode_info *ci = ceph_inode(inode);
  1932. struct ceph_mds_session *session = NULL;
  1933. int flushing = 0;
  1934. u64 flush_tid = 0, oldest_flush_tid = 0;
  1935. retry:
  1936. spin_lock(&ci->i_ceph_lock);
  1937. retry_locked:
  1938. if (ci->i_dirty_caps && ci->i_auth_cap) {
  1939. struct ceph_cap *cap = ci->i_auth_cap;
  1940. struct cap_msg_args arg;
  1941. if (session != cap->session) {
  1942. spin_unlock(&ci->i_ceph_lock);
  1943. if (session)
  1944. mutex_unlock(&session->s_mutex);
  1945. session = cap->session;
  1946. mutex_lock(&session->s_mutex);
  1947. goto retry;
  1948. }
  1949. if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
  1950. spin_unlock(&ci->i_ceph_lock);
  1951. goto out;
  1952. }
  1953. if (ci->i_ceph_flags &
  1954. (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS)) {
  1955. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
  1956. __kick_flushing_caps(mdsc, session, ci, 0);
  1957. if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
  1958. __ceph_flush_snaps(ci, session);
  1959. goto retry_locked;
  1960. }
  1961. flushing = ci->i_dirty_caps;
  1962. flush_tid = __mark_caps_flushing(inode, session, true,
  1963. &oldest_flush_tid);
  1964. __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH, CEPH_CLIENT_CAPS_SYNC,
  1965. __ceph_caps_used(ci), __ceph_caps_wanted(ci),
  1966. (cap->issued | cap->implemented),
  1967. flushing, flush_tid, oldest_flush_tid);
  1968. spin_unlock(&ci->i_ceph_lock);
  1969. __send_cap(&arg, ci);
  1970. } else {
  1971. if (!list_empty(&ci->i_cap_flush_list)) {
  1972. struct ceph_cap_flush *cf =
  1973. list_last_entry(&ci->i_cap_flush_list,
  1974. struct ceph_cap_flush, i_list);
  1975. cf->wake = true;
  1976. flush_tid = cf->tid;
  1977. }
  1978. flushing = ci->i_flushing_caps;
  1979. spin_unlock(&ci->i_ceph_lock);
  1980. }
  1981. out:
  1982. if (session)
  1983. mutex_unlock(&session->s_mutex);
  1984. *ptid = flush_tid;
  1985. return flushing;
  1986. }
  1987. /*
  1988. * Return true if we've flushed caps through the given flush_tid.
  1989. */
  1990. static int caps_are_flushed(struct inode *inode, u64 flush_tid)
  1991. {
  1992. struct ceph_inode_info *ci = ceph_inode(inode);
  1993. int ret = 1;
  1994. spin_lock(&ci->i_ceph_lock);
  1995. if (!list_empty(&ci->i_cap_flush_list)) {
  1996. struct ceph_cap_flush * cf =
  1997. list_first_entry(&ci->i_cap_flush_list,
  1998. struct ceph_cap_flush, i_list);
  1999. if (cf->tid <= flush_tid)
  2000. ret = 0;
  2001. }
  2002. spin_unlock(&ci->i_ceph_lock);
  2003. return ret;
  2004. }
  2005. /*
  2006. * wait for any unsafe requests to complete.
  2007. */
  2008. static int unsafe_request_wait(struct inode *inode)
  2009. {
  2010. struct ceph_inode_info *ci = ceph_inode(inode);
  2011. struct ceph_mds_request *req1 = NULL, *req2 = NULL;
  2012. int ret, err = 0;
  2013. spin_lock(&ci->i_unsafe_lock);
  2014. if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
  2015. req1 = list_last_entry(&ci->i_unsafe_dirops,
  2016. struct ceph_mds_request,
  2017. r_unsafe_dir_item);
  2018. ceph_mdsc_get_request(req1);
  2019. }
  2020. if (!list_empty(&ci->i_unsafe_iops)) {
  2021. req2 = list_last_entry(&ci->i_unsafe_iops,
  2022. struct ceph_mds_request,
  2023. r_unsafe_target_item);
  2024. ceph_mdsc_get_request(req2);
  2025. }
  2026. spin_unlock(&ci->i_unsafe_lock);
  2027. dout("unsafe_request_wait %p wait on tid %llu %llu\n",
  2028. inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
  2029. if (req1) {
  2030. ret = !wait_for_completion_timeout(&req1->r_safe_completion,
  2031. ceph_timeout_jiffies(req1->r_timeout));
  2032. if (ret)
  2033. err = -EIO;
  2034. ceph_mdsc_put_request(req1);
  2035. }
  2036. if (req2) {
  2037. ret = !wait_for_completion_timeout(&req2->r_safe_completion,
  2038. ceph_timeout_jiffies(req2->r_timeout));
  2039. if (ret)
  2040. err = -EIO;
  2041. ceph_mdsc_put_request(req2);
  2042. }
  2043. return err;
  2044. }
  2045. int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  2046. {
  2047. struct inode *inode = file->f_mapping->host;
  2048. struct ceph_inode_info *ci = ceph_inode(inode);
  2049. u64 flush_tid;
  2050. int ret, err;
  2051. int dirty;
  2052. dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
  2053. ret = file_write_and_wait_range(file, start, end);
  2054. if (datasync)
  2055. goto out;
  2056. ret = ceph_wait_on_async_create(inode);
  2057. if (ret)
  2058. goto out;
  2059. dirty = try_flush_caps(inode, &flush_tid);
  2060. dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
  2061. err = unsafe_request_wait(inode);
  2062. /*
  2063. * only wait on non-file metadata writeback (the mds
  2064. * can recover size and mtime, so we don't need to
  2065. * wait for that)
  2066. */
  2067. if (!err && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
  2068. err = wait_event_interruptible(ci->i_cap_wq,
  2069. caps_are_flushed(inode, flush_tid));
  2070. }
  2071. if (err < 0)
  2072. ret = err;
  2073. err = file_check_and_advance_wb_err(file);
  2074. if (err < 0)
  2075. ret = err;
  2076. out:
  2077. dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
  2078. return ret;
  2079. }
  2080. /*
  2081. * Flush any dirty caps back to the mds. If we aren't asked to wait,
  2082. * queue inode for flush but don't do so immediately, because we can
  2083. * get by with fewer MDS messages if we wait for data writeback to
  2084. * complete first.
  2085. */
  2086. int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
  2087. {
  2088. struct ceph_inode_info *ci = ceph_inode(inode);
  2089. u64 flush_tid;
  2090. int err = 0;
  2091. int dirty;
  2092. int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
  2093. dout("write_inode %p wait=%d\n", inode, wait);
  2094. if (wait) {
  2095. dirty = try_flush_caps(inode, &flush_tid);
  2096. if (dirty)
  2097. err = wait_event_interruptible(ci->i_cap_wq,
  2098. caps_are_flushed(inode, flush_tid));
  2099. } else {
  2100. struct ceph_mds_client *mdsc =
  2101. ceph_sb_to_client(inode->i_sb)->mdsc;
  2102. spin_lock(&ci->i_ceph_lock);
  2103. if (__ceph_caps_dirty(ci))
  2104. __cap_delay_requeue_front(mdsc, ci);
  2105. spin_unlock(&ci->i_ceph_lock);
  2106. }
  2107. return err;
  2108. }
  2109. static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
  2110. struct ceph_mds_session *session,
  2111. struct ceph_inode_info *ci,
  2112. u64 oldest_flush_tid)
  2113. __releases(ci->i_ceph_lock)
  2114. __acquires(ci->i_ceph_lock)
  2115. {
  2116. struct inode *inode = &ci->vfs_inode;
  2117. struct ceph_cap *cap;
  2118. struct ceph_cap_flush *cf;
  2119. int ret;
  2120. u64 first_tid = 0;
  2121. u64 last_snap_flush = 0;
  2122. ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
  2123. list_for_each_entry_reverse(cf, &ci->i_cap_flush_list, i_list) {
  2124. if (cf->is_capsnap) {
  2125. last_snap_flush = cf->tid;
  2126. break;
  2127. }
  2128. }
  2129. list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
  2130. if (cf->tid < first_tid)
  2131. continue;
  2132. cap = ci->i_auth_cap;
  2133. if (!(cap && cap->session == session)) {
  2134. pr_err("%p auth cap %p not mds%d ???\n",
  2135. inode, cap, session->s_mds);
  2136. break;
  2137. }
  2138. first_tid = cf->tid + 1;
  2139. if (!cf->is_capsnap) {
  2140. struct cap_msg_args arg;
  2141. dout("kick_flushing_caps %p cap %p tid %llu %s\n",
  2142. inode, cap, cf->tid, ceph_cap_string(cf->caps));
  2143. __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH,
  2144. (cf->tid < last_snap_flush ?
  2145. CEPH_CLIENT_CAPS_PENDING_CAPSNAP : 0),
  2146. __ceph_caps_used(ci),
  2147. __ceph_caps_wanted(ci),
  2148. (cap->issued | cap->implemented),
  2149. cf->caps, cf->tid, oldest_flush_tid);
  2150. spin_unlock(&ci->i_ceph_lock);
  2151. __send_cap(&arg, ci);
  2152. } else {
  2153. struct ceph_cap_snap *capsnap =
  2154. container_of(cf, struct ceph_cap_snap,
  2155. cap_flush);
  2156. dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
  2157. inode, capsnap, cf->tid,
  2158. ceph_cap_string(capsnap->dirty));
  2159. refcount_inc(&capsnap->nref);
  2160. spin_unlock(&ci->i_ceph_lock);
  2161. ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
  2162. oldest_flush_tid);
  2163. if (ret < 0) {
  2164. pr_err("kick_flushing_caps: error sending "
  2165. "cap flushsnap, ino (%llx.%llx) "
  2166. "tid %llu follows %llu\n",
  2167. ceph_vinop(inode), cf->tid,
  2168. capsnap->follows);
  2169. }
  2170. ceph_put_cap_snap(capsnap);
  2171. }
  2172. spin_lock(&ci->i_ceph_lock);
  2173. }
  2174. }
  2175. void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
  2176. struct ceph_mds_session *session)
  2177. {
  2178. struct ceph_inode_info *ci;
  2179. struct ceph_cap *cap;
  2180. u64 oldest_flush_tid;
  2181. dout("early_kick_flushing_caps mds%d\n", session->s_mds);
  2182. spin_lock(&mdsc->cap_dirty_lock);
  2183. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  2184. spin_unlock(&mdsc->cap_dirty_lock);
  2185. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  2186. spin_lock(&ci->i_ceph_lock);
  2187. cap = ci->i_auth_cap;
  2188. if (!(cap && cap->session == session)) {
  2189. pr_err("%p auth cap %p not mds%d ???\n",
  2190. &ci->vfs_inode, cap, session->s_mds);
  2191. spin_unlock(&ci->i_ceph_lock);
  2192. continue;
  2193. }
  2194. /*
  2195. * if flushing caps were revoked, we re-send the cap flush
  2196. * in client reconnect stage. This guarantees MDS * processes
  2197. * the cap flush message before issuing the flushing caps to
  2198. * other client.
  2199. */
  2200. if ((cap->issued & ci->i_flushing_caps) !=
  2201. ci->i_flushing_caps) {
  2202. /* encode_caps_cb() also will reset these sequence
  2203. * numbers. make sure sequence numbers in cap flush
  2204. * message match later reconnect message */
  2205. cap->seq = 0;
  2206. cap->issue_seq = 0;
  2207. cap->mseq = 0;
  2208. __kick_flushing_caps(mdsc, session, ci,
  2209. oldest_flush_tid);
  2210. } else {
  2211. ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
  2212. }
  2213. spin_unlock(&ci->i_ceph_lock);
  2214. }
  2215. }
  2216. void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
  2217. struct ceph_mds_session *session)
  2218. {
  2219. struct ceph_inode_info *ci;
  2220. struct ceph_cap *cap;
  2221. u64 oldest_flush_tid;
  2222. lockdep_assert_held(&session->s_mutex);
  2223. dout("kick_flushing_caps mds%d\n", session->s_mds);
  2224. spin_lock(&mdsc->cap_dirty_lock);
  2225. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  2226. spin_unlock(&mdsc->cap_dirty_lock);
  2227. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  2228. spin_lock(&ci->i_ceph_lock);
  2229. cap = ci->i_auth_cap;
  2230. if (!(cap && cap->session == session)) {
  2231. pr_err("%p auth cap %p not mds%d ???\n",
  2232. &ci->vfs_inode, cap, session->s_mds);
  2233. spin_unlock(&ci->i_ceph_lock);
  2234. continue;
  2235. }
  2236. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
  2237. __kick_flushing_caps(mdsc, session, ci,
  2238. oldest_flush_tid);
  2239. }
  2240. spin_unlock(&ci->i_ceph_lock);
  2241. }
  2242. }
  2243. void ceph_kick_flushing_inode_caps(struct ceph_mds_session *session,
  2244. struct ceph_inode_info *ci)
  2245. {
  2246. struct ceph_mds_client *mdsc = session->s_mdsc;
  2247. struct ceph_cap *cap = ci->i_auth_cap;
  2248. lockdep_assert_held(&ci->i_ceph_lock);
  2249. dout("%s %p flushing %s\n", __func__, &ci->vfs_inode,
  2250. ceph_cap_string(ci->i_flushing_caps));
  2251. if (!list_empty(&ci->i_cap_flush_list)) {
  2252. u64 oldest_flush_tid;
  2253. spin_lock(&mdsc->cap_dirty_lock);
  2254. list_move_tail(&ci->i_flushing_item,
  2255. &cap->session->s_cap_flushing);
  2256. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  2257. spin_unlock(&mdsc->cap_dirty_lock);
  2258. __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
  2259. }
  2260. }
  2261. /*
  2262. * Take references to capabilities we hold, so that we don't release
  2263. * them to the MDS prematurely.
  2264. */
  2265. void ceph_take_cap_refs(struct ceph_inode_info *ci, int got,
  2266. bool snap_rwsem_locked)
  2267. {
  2268. lockdep_assert_held(&ci->i_ceph_lock);
  2269. if (got & CEPH_CAP_PIN)
  2270. ci->i_pin_ref++;
  2271. if (got & CEPH_CAP_FILE_RD)
  2272. ci->i_rd_ref++;
  2273. if (got & CEPH_CAP_FILE_CACHE)
  2274. ci->i_rdcache_ref++;
  2275. if (got & CEPH_CAP_FILE_EXCL)
  2276. ci->i_fx_ref++;
  2277. if (got & CEPH_CAP_FILE_WR) {
  2278. if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
  2279. BUG_ON(!snap_rwsem_locked);
  2280. ci->i_head_snapc = ceph_get_snap_context(
  2281. ci->i_snap_realm->cached_context);
  2282. }
  2283. ci->i_wr_ref++;
  2284. }
  2285. if (got & CEPH_CAP_FILE_BUFFER) {
  2286. if (ci->i_wb_ref == 0)
  2287. ihold(&ci->vfs_inode);
  2288. ci->i_wb_ref++;
  2289. dout("%s %p wb %d -> %d (?)\n", __func__,
  2290. &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
  2291. }
  2292. }
  2293. /*
  2294. * Try to grab cap references. Specify those refs we @want, and the
  2295. * minimal set we @need. Also include the larger offset we are writing
  2296. * to (when applicable), and check against max_size here as well.
  2297. * Note that caller is responsible for ensuring max_size increases are
  2298. * requested from the MDS.
  2299. *
  2300. * Returns 0 if caps were not able to be acquired (yet), 1 if succeed,
  2301. * or a negative error code. There are 3 speical error codes:
  2302. * -EAGAIN: need to sleep but non-blocking is specified
  2303. * -EFBIG: ask caller to call check_max_size() and try again.
  2304. * -ESTALE: ask caller to call ceph_renew_caps() and try again.
  2305. */
  2306. enum {
  2307. /* first 8 bits are reserved for CEPH_FILE_MODE_FOO */
  2308. NON_BLOCKING = (1 << 8),
  2309. CHECK_FILELOCK = (1 << 9),
  2310. };
  2311. static int try_get_cap_refs(struct inode *inode, int need, int want,
  2312. loff_t endoff, int flags, int *got)
  2313. {
  2314. struct ceph_inode_info *ci = ceph_inode(inode);
  2315. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  2316. int ret = 0;
  2317. int have, implemented;
  2318. bool snap_rwsem_locked = false;
  2319. dout("get_cap_refs %p need %s want %s\n", inode,
  2320. ceph_cap_string(need), ceph_cap_string(want));
  2321. again:
  2322. spin_lock(&ci->i_ceph_lock);
  2323. if ((flags & CHECK_FILELOCK) &&
  2324. (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK)) {
  2325. dout("try_get_cap_refs %p error filelock\n", inode);
  2326. ret = -EIO;
  2327. goto out_unlock;
  2328. }
  2329. /* finish pending truncate */
  2330. while (ci->i_truncate_pending) {
  2331. spin_unlock(&ci->i_ceph_lock);
  2332. if (snap_rwsem_locked) {
  2333. up_read(&mdsc->snap_rwsem);
  2334. snap_rwsem_locked = false;
  2335. }
  2336. __ceph_do_pending_vmtruncate(inode);
  2337. spin_lock(&ci->i_ceph_lock);
  2338. }
  2339. have = __ceph_caps_issued(ci, &implemented);
  2340. if (have & need & CEPH_CAP_FILE_WR) {
  2341. if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
  2342. dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
  2343. inode, endoff, ci->i_max_size);
  2344. if (endoff > ci->i_requested_max_size)
  2345. ret = ci->i_auth_cap ? -EFBIG : -ESTALE;
  2346. goto out_unlock;
  2347. }
  2348. /*
  2349. * If a sync write is in progress, we must wait, so that we
  2350. * can get a final snapshot value for size+mtime.
  2351. */
  2352. if (__ceph_have_pending_cap_snap(ci)) {
  2353. dout("get_cap_refs %p cap_snap_pending\n", inode);
  2354. goto out_unlock;
  2355. }
  2356. }
  2357. if ((have & need) == need) {
  2358. /*
  2359. * Look at (implemented & ~have & not) so that we keep waiting
  2360. * on transition from wanted -> needed caps. This is needed
  2361. * for WRBUFFER|WR -> WR to avoid a new WR sync write from
  2362. * going before a prior buffered writeback happens.
  2363. */
  2364. int not = want & ~(have & need);
  2365. int revoking = implemented & ~have;
  2366. dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
  2367. inode, ceph_cap_string(have), ceph_cap_string(not),
  2368. ceph_cap_string(revoking));
  2369. if ((revoking & not) == 0) {
  2370. if (!snap_rwsem_locked &&
  2371. !ci->i_head_snapc &&
  2372. (need & CEPH_CAP_FILE_WR)) {
  2373. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  2374. /*
  2375. * we can not call down_read() when
  2376. * task isn't in TASK_RUNNING state
  2377. */
  2378. if (flags & NON_BLOCKING) {
  2379. ret = -EAGAIN;
  2380. goto out_unlock;
  2381. }
  2382. spin_unlock(&ci->i_ceph_lock);
  2383. down_read(&mdsc->snap_rwsem);
  2384. snap_rwsem_locked = true;
  2385. goto again;
  2386. }
  2387. snap_rwsem_locked = true;
  2388. }
  2389. if ((have & want) == want)
  2390. *got = need | want;
  2391. else
  2392. *got = need;
  2393. if (S_ISREG(inode->i_mode) &&
  2394. (need & CEPH_CAP_FILE_RD) &&
  2395. !(*got & CEPH_CAP_FILE_CACHE))
  2396. ceph_disable_fscache_readpage(ci);
  2397. ceph_take_cap_refs(ci, *got, true);
  2398. ret = 1;
  2399. }
  2400. } else {
  2401. int session_readonly = false;
  2402. int mds_wanted;
  2403. if (ci->i_auth_cap &&
  2404. (need & (CEPH_CAP_FILE_WR | CEPH_CAP_FILE_EXCL))) {
  2405. struct ceph_mds_session *s = ci->i_auth_cap->session;
  2406. spin_lock(&s->s_cap_lock);
  2407. session_readonly = s->s_readonly;
  2408. spin_unlock(&s->s_cap_lock);
  2409. }
  2410. if (session_readonly) {
  2411. dout("get_cap_refs %p need %s but mds%d readonly\n",
  2412. inode, ceph_cap_string(need), ci->i_auth_cap->mds);
  2413. ret = -EROFS;
  2414. goto out_unlock;
  2415. }
  2416. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  2417. dout("get_cap_refs %p forced umount\n", inode);
  2418. ret = -EIO;
  2419. goto out_unlock;
  2420. }
  2421. mds_wanted = __ceph_caps_mds_wanted(ci, false);
  2422. if (need & ~mds_wanted) {
  2423. dout("get_cap_refs %p need %s > mds_wanted %s\n",
  2424. inode, ceph_cap_string(need),
  2425. ceph_cap_string(mds_wanted));
  2426. ret = -ESTALE;
  2427. goto out_unlock;
  2428. }
  2429. dout("get_cap_refs %p have %s need %s\n", inode,
  2430. ceph_cap_string(have), ceph_cap_string(need));
  2431. }
  2432. out_unlock:
  2433. __ceph_touch_fmode(ci, mdsc, flags);
  2434. spin_unlock(&ci->i_ceph_lock);
  2435. if (snap_rwsem_locked)
  2436. up_read(&mdsc->snap_rwsem);
  2437. if (!ret)
  2438. ceph_update_cap_mis(&mdsc->metric);
  2439. else if (ret == 1)
  2440. ceph_update_cap_hit(&mdsc->metric);
  2441. dout("get_cap_refs %p ret %d got %s\n", inode,
  2442. ret, ceph_cap_string(*got));
  2443. return ret;
  2444. }
  2445. /*
  2446. * Check the offset we are writing up to against our current
  2447. * max_size. If necessary, tell the MDS we want to write to
  2448. * a larger offset.
  2449. */
  2450. static void check_max_size(struct inode *inode, loff_t endoff)
  2451. {
  2452. struct ceph_inode_info *ci = ceph_inode(inode);
  2453. int check = 0;
  2454. /* do we need to explicitly request a larger max_size? */
  2455. spin_lock(&ci->i_ceph_lock);
  2456. if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
  2457. dout("write %p at large endoff %llu, req max_size\n",
  2458. inode, endoff);
  2459. ci->i_wanted_max_size = endoff;
  2460. }
  2461. /* duplicate ceph_check_caps()'s logic */
  2462. if (ci->i_auth_cap &&
  2463. (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
  2464. ci->i_wanted_max_size > ci->i_max_size &&
  2465. ci->i_wanted_max_size > ci->i_requested_max_size)
  2466. check = 1;
  2467. spin_unlock(&ci->i_ceph_lock);
  2468. if (check)
  2469. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  2470. }
  2471. static inline int get_used_fmode(int caps)
  2472. {
  2473. int fmode = 0;
  2474. if (caps & CEPH_CAP_FILE_RD)
  2475. fmode |= CEPH_FILE_MODE_RD;
  2476. if (caps & CEPH_CAP_FILE_WR)
  2477. fmode |= CEPH_FILE_MODE_WR;
  2478. return fmode;
  2479. }
  2480. int ceph_try_get_caps(struct inode *inode, int need, int want,
  2481. bool nonblock, int *got)
  2482. {
  2483. int ret, flags;
  2484. BUG_ON(need & ~CEPH_CAP_FILE_RD);
  2485. BUG_ON(want & ~(CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO |
  2486. CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
  2487. CEPH_CAP_ANY_DIR_OPS));
  2488. if (need) {
  2489. ret = ceph_pool_perm_check(inode, need);
  2490. if (ret < 0)
  2491. return ret;
  2492. }
  2493. flags = get_used_fmode(need | want);
  2494. if (nonblock)
  2495. flags |= NON_BLOCKING;
  2496. ret = try_get_cap_refs(inode, need, want, 0, flags, got);
  2497. /* three special error codes */
  2498. if (ret == -EAGAIN || ret == -EFBIG || ret == -ESTALE)
  2499. ret = 0;
  2500. return ret;
  2501. }
  2502. /*
  2503. * Wait for caps, and take cap references. If we can't get a WR cap
  2504. * due to a small max_size, make sure we check_max_size (and possibly
  2505. * ask the mds) so we don't get hung up indefinitely.
  2506. */
  2507. int ceph_get_caps(struct file *filp, int need, int want,
  2508. loff_t endoff, int *got, struct page **pinned_page)
  2509. {
  2510. struct ceph_file_info *fi = filp->private_data;
  2511. struct inode *inode = file_inode(filp);
  2512. struct ceph_inode_info *ci = ceph_inode(inode);
  2513. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  2514. int ret, _got, flags;
  2515. ret = ceph_pool_perm_check(inode, need);
  2516. if (ret < 0)
  2517. return ret;
  2518. if ((fi->fmode & CEPH_FILE_MODE_WR) &&
  2519. fi->filp_gen != READ_ONCE(fsc->filp_gen))
  2520. return -EBADF;
  2521. flags = get_used_fmode(need | want);
  2522. while (true) {
  2523. flags &= CEPH_FILE_MODE_MASK;
  2524. if (atomic_read(&fi->num_locks))
  2525. flags |= CHECK_FILELOCK;
  2526. _got = 0;
  2527. ret = try_get_cap_refs(inode, need, want, endoff,
  2528. flags, &_got);
  2529. WARN_ON_ONCE(ret == -EAGAIN);
  2530. if (!ret) {
  2531. struct ceph_mds_client *mdsc = fsc->mdsc;
  2532. struct cap_wait cw;
  2533. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2534. cw.ino = ceph_ino(inode);
  2535. cw.tgid = current->tgid;
  2536. cw.need = need;
  2537. cw.want = want;
  2538. spin_lock(&mdsc->caps_list_lock);
  2539. list_add(&cw.list, &mdsc->cap_wait_list);
  2540. spin_unlock(&mdsc->caps_list_lock);
  2541. /* make sure used fmode not timeout */
  2542. ceph_get_fmode(ci, flags, FMODE_WAIT_BIAS);
  2543. add_wait_queue(&ci->i_cap_wq, &wait);
  2544. flags |= NON_BLOCKING;
  2545. while (!(ret = try_get_cap_refs(inode, need, want,
  2546. endoff, flags, &_got))) {
  2547. if (signal_pending(current)) {
  2548. ret = -ERESTARTSYS;
  2549. break;
  2550. }
  2551. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  2552. }
  2553. remove_wait_queue(&ci->i_cap_wq, &wait);
  2554. ceph_put_fmode(ci, flags, FMODE_WAIT_BIAS);
  2555. spin_lock(&mdsc->caps_list_lock);
  2556. list_del(&cw.list);
  2557. spin_unlock(&mdsc->caps_list_lock);
  2558. if (ret == -EAGAIN)
  2559. continue;
  2560. }
  2561. if ((fi->fmode & CEPH_FILE_MODE_WR) &&
  2562. fi->filp_gen != READ_ONCE(fsc->filp_gen)) {
  2563. if (ret >= 0 && _got)
  2564. ceph_put_cap_refs(ci, _got);
  2565. return -EBADF;
  2566. }
  2567. if (ret < 0) {
  2568. if (ret == -EFBIG || ret == -ESTALE) {
  2569. int ret2 = ceph_wait_on_async_create(inode);
  2570. if (ret2 < 0)
  2571. return ret2;
  2572. }
  2573. if (ret == -EFBIG) {
  2574. check_max_size(inode, endoff);
  2575. continue;
  2576. }
  2577. if (ret == -ESTALE) {
  2578. /* session was killed, try renew caps */
  2579. ret = ceph_renew_caps(inode, flags);
  2580. if (ret == 0)
  2581. continue;
  2582. }
  2583. return ret;
  2584. }
  2585. if (S_ISREG(ci->vfs_inode.i_mode) &&
  2586. ci->i_inline_version != CEPH_INLINE_NONE &&
  2587. (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
  2588. i_size_read(inode) > 0) {
  2589. struct page *page =
  2590. find_get_page(inode->i_mapping, 0);
  2591. if (page) {
  2592. if (PageUptodate(page)) {
  2593. *pinned_page = page;
  2594. break;
  2595. }
  2596. put_page(page);
  2597. }
  2598. /*
  2599. * drop cap refs first because getattr while
  2600. * holding * caps refs can cause deadlock.
  2601. */
  2602. ceph_put_cap_refs(ci, _got);
  2603. _got = 0;
  2604. /*
  2605. * getattr request will bring inline data into
  2606. * page cache
  2607. */
  2608. ret = __ceph_do_getattr(inode, NULL,
  2609. CEPH_STAT_CAP_INLINE_DATA,
  2610. true);
  2611. if (ret < 0)
  2612. return ret;
  2613. continue;
  2614. }
  2615. break;
  2616. }
  2617. if (S_ISREG(ci->vfs_inode.i_mode) &&
  2618. (_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
  2619. ceph_fscache_revalidate_cookie(ci);
  2620. *got = _got;
  2621. return 0;
  2622. }
  2623. /*
  2624. * Take cap refs. Caller must already know we hold at least one ref
  2625. * on the caps in question or we don't know this is safe.
  2626. */
  2627. void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
  2628. {
  2629. spin_lock(&ci->i_ceph_lock);
  2630. ceph_take_cap_refs(ci, caps, false);
  2631. spin_unlock(&ci->i_ceph_lock);
  2632. }
  2633. /*
  2634. * drop cap_snap that is not associated with any snapshot.
  2635. * we don't need to send FLUSHSNAP message for it.
  2636. */
  2637. static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
  2638. struct ceph_cap_snap *capsnap)
  2639. {
  2640. if (!capsnap->need_flush &&
  2641. !capsnap->writing && !capsnap->dirty_pages) {
  2642. dout("dropping cap_snap %p follows %llu\n",
  2643. capsnap, capsnap->follows);
  2644. BUG_ON(capsnap->cap_flush.tid > 0);
  2645. ceph_put_snap_context(capsnap->context);
  2646. if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
  2647. ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  2648. list_del(&capsnap->ci_item);
  2649. ceph_put_cap_snap(capsnap);
  2650. return 1;
  2651. }
  2652. return 0;
  2653. }
  2654. /*
  2655. * Release cap refs.
  2656. *
  2657. * If we released the last ref on any given cap, call ceph_check_caps
  2658. * to release (or schedule a release).
  2659. *
  2660. * If we are releasing a WR cap (from a sync write), finalize any affected
  2661. * cap_snap, and wake up any waiters.
  2662. */
  2663. static void __ceph_put_cap_refs(struct ceph_inode_info *ci, int had,
  2664. bool skip_checking_caps)
  2665. {
  2666. struct inode *inode = &ci->vfs_inode;
  2667. int last = 0, put = 0, flushsnaps = 0, wake = 0;
  2668. spin_lock(&ci->i_ceph_lock);
  2669. if (had & CEPH_CAP_PIN)
  2670. --ci->i_pin_ref;
  2671. if (had & CEPH_CAP_FILE_RD)
  2672. if (--ci->i_rd_ref == 0)
  2673. last++;
  2674. if (had & CEPH_CAP_FILE_CACHE)
  2675. if (--ci->i_rdcache_ref == 0)
  2676. last++;
  2677. if (had & CEPH_CAP_FILE_EXCL)
  2678. if (--ci->i_fx_ref == 0)
  2679. last++;
  2680. if (had & CEPH_CAP_FILE_BUFFER) {
  2681. if (--ci->i_wb_ref == 0) {
  2682. last++;
  2683. put++;
  2684. }
  2685. dout("put_cap_refs %p wb %d -> %d (?)\n",
  2686. inode, ci->i_wb_ref+1, ci->i_wb_ref);
  2687. }
  2688. if (had & CEPH_CAP_FILE_WR)
  2689. if (--ci->i_wr_ref == 0) {
  2690. last++;
  2691. if (__ceph_have_pending_cap_snap(ci)) {
  2692. struct ceph_cap_snap *capsnap =
  2693. list_last_entry(&ci->i_cap_snaps,
  2694. struct ceph_cap_snap,
  2695. ci_item);
  2696. capsnap->writing = 0;
  2697. if (ceph_try_drop_cap_snap(ci, capsnap))
  2698. put++;
  2699. else if (__ceph_finish_cap_snap(ci, capsnap))
  2700. flushsnaps = 1;
  2701. wake = 1;
  2702. }
  2703. if (ci->i_wrbuffer_ref_head == 0 &&
  2704. ci->i_dirty_caps == 0 &&
  2705. ci->i_flushing_caps == 0) {
  2706. BUG_ON(!ci->i_head_snapc);
  2707. ceph_put_snap_context(ci->i_head_snapc);
  2708. ci->i_head_snapc = NULL;
  2709. }
  2710. /* see comment in __ceph_remove_cap() */
  2711. if (!__ceph_is_any_real_caps(ci) && ci->i_snap_realm)
  2712. drop_inode_snap_realm(ci);
  2713. }
  2714. spin_unlock(&ci->i_ceph_lock);
  2715. dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
  2716. last ? " last" : "", put ? " put" : "");
  2717. if (!skip_checking_caps) {
  2718. if (last)
  2719. ceph_check_caps(ci, 0, NULL);
  2720. else if (flushsnaps)
  2721. ceph_flush_snaps(ci, NULL);
  2722. }
  2723. if (wake)
  2724. wake_up_all(&ci->i_cap_wq);
  2725. while (put-- > 0)
  2726. iput(inode);
  2727. }
  2728. void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
  2729. {
  2730. __ceph_put_cap_refs(ci, had, false);
  2731. }
  2732. void ceph_put_cap_refs_no_check_caps(struct ceph_inode_info *ci, int had)
  2733. {
  2734. __ceph_put_cap_refs(ci, had, true);
  2735. }
  2736. /*
  2737. * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
  2738. * context. Adjust per-snap dirty page accounting as appropriate.
  2739. * Once all dirty data for a cap_snap is flushed, flush snapped file
  2740. * metadata back to the MDS. If we dropped the last ref, call
  2741. * ceph_check_caps.
  2742. */
  2743. void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
  2744. struct ceph_snap_context *snapc)
  2745. {
  2746. struct inode *inode = &ci->vfs_inode;
  2747. struct ceph_cap_snap *capsnap = NULL;
  2748. int put = 0;
  2749. bool last = false;
  2750. bool found = false;
  2751. bool flush_snaps = false;
  2752. bool complete_capsnap = false;
  2753. spin_lock(&ci->i_ceph_lock);
  2754. ci->i_wrbuffer_ref -= nr;
  2755. if (ci->i_wrbuffer_ref == 0) {
  2756. last = true;
  2757. put++;
  2758. }
  2759. if (ci->i_head_snapc == snapc) {
  2760. ci->i_wrbuffer_ref_head -= nr;
  2761. if (ci->i_wrbuffer_ref_head == 0 &&
  2762. ci->i_wr_ref == 0 &&
  2763. ci->i_dirty_caps == 0 &&
  2764. ci->i_flushing_caps == 0) {
  2765. BUG_ON(!ci->i_head_snapc);
  2766. ceph_put_snap_context(ci->i_head_snapc);
  2767. ci->i_head_snapc = NULL;
  2768. }
  2769. dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
  2770. inode,
  2771. ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
  2772. ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  2773. last ? " LAST" : "");
  2774. } else {
  2775. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  2776. if (capsnap->context == snapc) {
  2777. found = true;
  2778. break;
  2779. }
  2780. }
  2781. if (!found) {
  2782. /*
  2783. * The capsnap should already be removed when removing
  2784. * auth cap in the case of a forced unmount.
  2785. */
  2786. WARN_ON_ONCE(ci->i_auth_cap);
  2787. goto unlock;
  2788. }
  2789. capsnap->dirty_pages -= nr;
  2790. if (capsnap->dirty_pages == 0) {
  2791. complete_capsnap = true;
  2792. if (!capsnap->writing) {
  2793. if (ceph_try_drop_cap_snap(ci, capsnap)) {
  2794. put++;
  2795. } else {
  2796. ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  2797. flush_snaps = true;
  2798. }
  2799. }
  2800. }
  2801. dout("put_wrbuffer_cap_refs on %p cap_snap %p "
  2802. " snap %lld %d/%d -> %d/%d %s%s\n",
  2803. inode, capsnap, capsnap->context->seq,
  2804. ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
  2805. ci->i_wrbuffer_ref, capsnap->dirty_pages,
  2806. last ? " (wrbuffer last)" : "",
  2807. complete_capsnap ? " (complete capsnap)" : "");
  2808. }
  2809. unlock:
  2810. spin_unlock(&ci->i_ceph_lock);
  2811. if (last) {
  2812. ceph_check_caps(ci, 0, NULL);
  2813. } else if (flush_snaps) {
  2814. ceph_flush_snaps(ci, NULL);
  2815. }
  2816. if (complete_capsnap)
  2817. wake_up_all(&ci->i_cap_wq);
  2818. while (put-- > 0) {
  2819. /* avoid calling iput_final() in osd dispatch threads */
  2820. ceph_async_iput(inode);
  2821. }
  2822. }
  2823. /*
  2824. * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
  2825. */
  2826. static void invalidate_aliases(struct inode *inode)
  2827. {
  2828. struct dentry *dn, *prev = NULL;
  2829. dout("invalidate_aliases inode %p\n", inode);
  2830. d_prune_aliases(inode);
  2831. /*
  2832. * For non-directory inode, d_find_alias() only returns
  2833. * hashed dentry. After calling d_invalidate(), the
  2834. * dentry becomes unhashed.
  2835. *
  2836. * For directory inode, d_find_alias() can return
  2837. * unhashed dentry. But directory inode should have
  2838. * one alias at most.
  2839. */
  2840. while ((dn = d_find_alias(inode))) {
  2841. if (dn == prev) {
  2842. dput(dn);
  2843. break;
  2844. }
  2845. d_invalidate(dn);
  2846. if (prev)
  2847. dput(prev);
  2848. prev = dn;
  2849. }
  2850. if (prev)
  2851. dput(prev);
  2852. }
  2853. struct cap_extra_info {
  2854. struct ceph_string *pool_ns;
  2855. /* inline data */
  2856. u64 inline_version;
  2857. void *inline_data;
  2858. u32 inline_len;
  2859. /* dirstat */
  2860. bool dirstat_valid;
  2861. u64 nfiles;
  2862. u64 nsubdirs;
  2863. u64 change_attr;
  2864. /* currently issued */
  2865. int issued;
  2866. struct timespec64 btime;
  2867. };
  2868. /*
  2869. * Handle a cap GRANT message from the MDS. (Note that a GRANT may
  2870. * actually be a revocation if it specifies a smaller cap set.)
  2871. *
  2872. * caller holds s_mutex and i_ceph_lock, we drop both.
  2873. */
  2874. static void handle_cap_grant(struct inode *inode,
  2875. struct ceph_mds_session *session,
  2876. struct ceph_cap *cap,
  2877. struct ceph_mds_caps *grant,
  2878. struct ceph_buffer *xattr_buf,
  2879. struct cap_extra_info *extra_info)
  2880. __releases(ci->i_ceph_lock)
  2881. __releases(session->s_mdsc->snap_rwsem)
  2882. {
  2883. struct ceph_inode_info *ci = ceph_inode(inode);
  2884. int seq = le32_to_cpu(grant->seq);
  2885. int newcaps = le32_to_cpu(grant->caps);
  2886. int used, wanted, dirty;
  2887. u64 size = le64_to_cpu(grant->size);
  2888. u64 max_size = le64_to_cpu(grant->max_size);
  2889. unsigned char check_caps = 0;
  2890. bool was_stale = cap->cap_gen < session->s_cap_gen;
  2891. bool wake = false;
  2892. bool writeback = false;
  2893. bool queue_trunc = false;
  2894. bool queue_invalidate = false;
  2895. bool deleted_inode = false;
  2896. bool fill_inline = false;
  2897. dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
  2898. inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
  2899. dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
  2900. inode->i_size);
  2901. /*
  2902. * If CACHE is being revoked, and we have no dirty buffers,
  2903. * try to invalidate (once). (If there are dirty buffers, we
  2904. * will invalidate _after_ writeback.)
  2905. */
  2906. if (S_ISREG(inode->i_mode) && /* don't invalidate readdir cache */
  2907. ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
  2908. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  2909. !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
  2910. if (try_nonblocking_invalidate(inode)) {
  2911. /* there were locked pages.. invalidate later
  2912. in a separate thread. */
  2913. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  2914. queue_invalidate = true;
  2915. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  2916. }
  2917. }
  2918. }
  2919. if (was_stale)
  2920. cap->issued = cap->implemented = CEPH_CAP_PIN;
  2921. /*
  2922. * auth mds of the inode changed. we received the cap export message,
  2923. * but still haven't received the cap import message. handle_cap_export
  2924. * updated the new auth MDS' cap.
  2925. *
  2926. * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
  2927. * that was sent before the cap import message. So don't remove caps.
  2928. */
  2929. if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  2930. WARN_ON(cap != ci->i_auth_cap);
  2931. WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
  2932. seq = cap->seq;
  2933. newcaps |= cap->issued;
  2934. }
  2935. /* side effects now are allowed */
  2936. cap->cap_gen = session->s_cap_gen;
  2937. cap->seq = seq;
  2938. __check_cap_issue(ci, cap, newcaps);
  2939. inode_set_max_iversion_raw(inode, extra_info->change_attr);
  2940. if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
  2941. (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
  2942. inode->i_mode = le32_to_cpu(grant->mode);
  2943. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
  2944. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
  2945. ci->i_btime = extra_info->btime;
  2946. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  2947. from_kuid(&init_user_ns, inode->i_uid),
  2948. from_kgid(&init_user_ns, inode->i_gid));
  2949. }
  2950. if ((newcaps & CEPH_CAP_LINK_SHARED) &&
  2951. (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
  2952. set_nlink(inode, le32_to_cpu(grant->nlink));
  2953. if (inode->i_nlink == 0 &&
  2954. (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
  2955. deleted_inode = true;
  2956. }
  2957. if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
  2958. grant->xattr_len) {
  2959. int len = le32_to_cpu(grant->xattr_len);
  2960. u64 version = le64_to_cpu(grant->xattr_version);
  2961. if (version > ci->i_xattrs.version) {
  2962. dout(" got new xattrs v%llu on %p len %d\n",
  2963. version, inode, len);
  2964. if (ci->i_xattrs.blob)
  2965. ceph_buffer_put(ci->i_xattrs.blob);
  2966. ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
  2967. ci->i_xattrs.version = version;
  2968. ceph_forget_all_cached_acls(inode);
  2969. ceph_security_invalidate_secctx(inode);
  2970. }
  2971. }
  2972. if (newcaps & CEPH_CAP_ANY_RD) {
  2973. struct timespec64 mtime, atime, ctime;
  2974. /* ctime/mtime/atime? */
  2975. ceph_decode_timespec64(&mtime, &grant->mtime);
  2976. ceph_decode_timespec64(&atime, &grant->atime);
  2977. ceph_decode_timespec64(&ctime, &grant->ctime);
  2978. ceph_fill_file_time(inode, extra_info->issued,
  2979. le32_to_cpu(grant->time_warp_seq),
  2980. &ctime, &mtime, &atime);
  2981. }
  2982. if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
  2983. ci->i_files = extra_info->nfiles;
  2984. ci->i_subdirs = extra_info->nsubdirs;
  2985. }
  2986. if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
  2987. /* file layout may have changed */
  2988. s64 old_pool = ci->i_layout.pool_id;
  2989. struct ceph_string *old_ns;
  2990. ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
  2991. old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
  2992. lockdep_is_held(&ci->i_ceph_lock));
  2993. rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
  2994. if (ci->i_layout.pool_id != old_pool ||
  2995. extra_info->pool_ns != old_ns)
  2996. ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
  2997. extra_info->pool_ns = old_ns;
  2998. /* size/truncate_seq? */
  2999. queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
  3000. le32_to_cpu(grant->truncate_seq),
  3001. le64_to_cpu(grant->truncate_size),
  3002. size);
  3003. }
  3004. if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
  3005. if (max_size != ci->i_max_size) {
  3006. dout("max_size %lld -> %llu\n",
  3007. ci->i_max_size, max_size);
  3008. ci->i_max_size = max_size;
  3009. if (max_size >= ci->i_wanted_max_size) {
  3010. ci->i_wanted_max_size = 0; /* reset */
  3011. ci->i_requested_max_size = 0;
  3012. }
  3013. wake = true;
  3014. }
  3015. }
  3016. /* check cap bits */
  3017. wanted = __ceph_caps_wanted(ci);
  3018. used = __ceph_caps_used(ci);
  3019. dirty = __ceph_caps_dirty(ci);
  3020. dout(" my wanted = %s, used = %s, dirty %s\n",
  3021. ceph_cap_string(wanted),
  3022. ceph_cap_string(used),
  3023. ceph_cap_string(dirty));
  3024. if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) &&
  3025. (wanted & ~(cap->mds_wanted | newcaps))) {
  3026. /*
  3027. * If mds is importing cap, prior cap messages that update
  3028. * 'wanted' may get dropped by mds (migrate seq mismatch).
  3029. *
  3030. * We don't send cap message to update 'wanted' if what we
  3031. * want are already issued. If mds revokes caps, cap message
  3032. * that releases caps also tells mds what we want. But if
  3033. * caps got revoked by mds forcedly (session stale). We may
  3034. * haven't told mds what we want.
  3035. */
  3036. check_caps = 1;
  3037. }
  3038. /* revocation, grant, or no-op? */
  3039. if (cap->issued & ~newcaps) {
  3040. int revoking = cap->issued & ~newcaps;
  3041. dout("revocation: %s -> %s (revoking %s)\n",
  3042. ceph_cap_string(cap->issued),
  3043. ceph_cap_string(newcaps),
  3044. ceph_cap_string(revoking));
  3045. if (S_ISREG(inode->i_mode) &&
  3046. (revoking & used & CEPH_CAP_FILE_BUFFER))
  3047. writeback = true; /* initiate writeback; will delay ack */
  3048. else if (queue_invalidate &&
  3049. revoking == CEPH_CAP_FILE_CACHE &&
  3050. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0)
  3051. ; /* do nothing yet, invalidation will be queued */
  3052. else if (cap == ci->i_auth_cap)
  3053. check_caps = 1; /* check auth cap only */
  3054. else
  3055. check_caps = 2; /* check all caps */
  3056. cap->issued = newcaps;
  3057. cap->implemented |= newcaps;
  3058. } else if (cap->issued == newcaps) {
  3059. dout("caps unchanged: %s -> %s\n",
  3060. ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
  3061. } else {
  3062. dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
  3063. ceph_cap_string(newcaps));
  3064. /* non-auth MDS is revoking the newly grant caps ? */
  3065. if (cap == ci->i_auth_cap &&
  3066. __ceph_caps_revoking_other(ci, cap, newcaps))
  3067. check_caps = 2;
  3068. cap->issued = newcaps;
  3069. cap->implemented |= newcaps; /* add bits only, to
  3070. * avoid stepping on a
  3071. * pending revocation */
  3072. wake = true;
  3073. }
  3074. BUG_ON(cap->issued & ~cap->implemented);
  3075. if (extra_info->inline_version > 0 &&
  3076. extra_info->inline_version >= ci->i_inline_version) {
  3077. ci->i_inline_version = extra_info->inline_version;
  3078. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  3079. (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
  3080. fill_inline = true;
  3081. }
  3082. if (ci->i_auth_cap == cap &&
  3083. le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
  3084. if (newcaps & ~extra_info->issued)
  3085. wake = true;
  3086. if (ci->i_requested_max_size > max_size ||
  3087. !(le32_to_cpu(grant->wanted) & CEPH_CAP_ANY_FILE_WR)) {
  3088. /* re-request max_size if necessary */
  3089. ci->i_requested_max_size = 0;
  3090. wake = true;
  3091. }
  3092. ceph_kick_flushing_inode_caps(session, ci);
  3093. spin_unlock(&ci->i_ceph_lock);
  3094. up_read(&session->s_mdsc->snap_rwsem);
  3095. } else {
  3096. spin_unlock(&ci->i_ceph_lock);
  3097. }
  3098. if (fill_inline)
  3099. ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
  3100. extra_info->inline_len);
  3101. if (queue_trunc)
  3102. ceph_queue_vmtruncate(inode);
  3103. if (writeback)
  3104. /*
  3105. * queue inode for writeback: we can't actually call
  3106. * filemap_write_and_wait, etc. from message handler
  3107. * context.
  3108. */
  3109. ceph_queue_writeback(inode);
  3110. if (queue_invalidate)
  3111. ceph_queue_invalidate(inode);
  3112. if (deleted_inode)
  3113. invalidate_aliases(inode);
  3114. if (wake)
  3115. wake_up_all(&ci->i_cap_wq);
  3116. if (check_caps == 1)
  3117. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY | CHECK_CAPS_NOINVAL,
  3118. session);
  3119. else if (check_caps == 2)
  3120. ceph_check_caps(ci, CHECK_CAPS_NOINVAL, session);
  3121. else
  3122. mutex_unlock(&session->s_mutex);
  3123. }
  3124. /*
  3125. * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
  3126. * MDS has been safely committed.
  3127. */
  3128. static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
  3129. struct ceph_mds_caps *m,
  3130. struct ceph_mds_session *session,
  3131. struct ceph_cap *cap)
  3132. __releases(ci->i_ceph_lock)
  3133. {
  3134. struct ceph_inode_info *ci = ceph_inode(inode);
  3135. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  3136. struct ceph_cap_flush *cf, *tmp_cf;
  3137. LIST_HEAD(to_remove);
  3138. unsigned seq = le32_to_cpu(m->seq);
  3139. int dirty = le32_to_cpu(m->dirty);
  3140. int cleaned = 0;
  3141. bool drop = false;
  3142. bool wake_ci = false;
  3143. bool wake_mdsc = false;
  3144. list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
  3145. /* Is this the one that was flushed? */
  3146. if (cf->tid == flush_tid)
  3147. cleaned = cf->caps;
  3148. /* Is this a capsnap? */
  3149. if (cf->is_capsnap)
  3150. continue;
  3151. if (cf->tid <= flush_tid) {
  3152. /*
  3153. * An earlier or current tid. The FLUSH_ACK should
  3154. * represent a superset of this flush's caps.
  3155. */
  3156. wake_ci |= __detach_cap_flush_from_ci(ci, cf);
  3157. list_add_tail(&cf->i_list, &to_remove);
  3158. } else {
  3159. /*
  3160. * This is a later one. Any caps in it are still dirty
  3161. * so don't count them as cleaned.
  3162. */
  3163. cleaned &= ~cf->caps;
  3164. if (!cleaned)
  3165. break;
  3166. }
  3167. }
  3168. dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
  3169. " flushing %s -> %s\n",
  3170. inode, session->s_mds, seq, ceph_cap_string(dirty),
  3171. ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
  3172. ceph_cap_string(ci->i_flushing_caps & ~cleaned));
  3173. if (list_empty(&to_remove) && !cleaned)
  3174. goto out;
  3175. ci->i_flushing_caps &= ~cleaned;
  3176. spin_lock(&mdsc->cap_dirty_lock);
  3177. list_for_each_entry(cf, &to_remove, i_list)
  3178. wake_mdsc |= __detach_cap_flush_from_mdsc(mdsc, cf);
  3179. if (ci->i_flushing_caps == 0) {
  3180. if (list_empty(&ci->i_cap_flush_list)) {
  3181. list_del_init(&ci->i_flushing_item);
  3182. if (!list_empty(&session->s_cap_flushing)) {
  3183. dout(" mds%d still flushing cap on %p\n",
  3184. session->s_mds,
  3185. &list_first_entry(&session->s_cap_flushing,
  3186. struct ceph_inode_info,
  3187. i_flushing_item)->vfs_inode);
  3188. }
  3189. }
  3190. mdsc->num_cap_flushing--;
  3191. dout(" inode %p now !flushing\n", inode);
  3192. if (ci->i_dirty_caps == 0) {
  3193. dout(" inode %p now clean\n", inode);
  3194. BUG_ON(!list_empty(&ci->i_dirty_item));
  3195. drop = true;
  3196. if (ci->i_wr_ref == 0 &&
  3197. ci->i_wrbuffer_ref_head == 0) {
  3198. BUG_ON(!ci->i_head_snapc);
  3199. ceph_put_snap_context(ci->i_head_snapc);
  3200. ci->i_head_snapc = NULL;
  3201. }
  3202. } else {
  3203. BUG_ON(list_empty(&ci->i_dirty_item));
  3204. }
  3205. }
  3206. spin_unlock(&mdsc->cap_dirty_lock);
  3207. out:
  3208. spin_unlock(&ci->i_ceph_lock);
  3209. while (!list_empty(&to_remove)) {
  3210. cf = list_first_entry(&to_remove,
  3211. struct ceph_cap_flush, i_list);
  3212. list_del_init(&cf->i_list);
  3213. if (!cf->is_capsnap)
  3214. ceph_free_cap_flush(cf);
  3215. }
  3216. if (wake_ci)
  3217. wake_up_all(&ci->i_cap_wq);
  3218. if (wake_mdsc)
  3219. wake_up_all(&mdsc->cap_flushing_wq);
  3220. if (drop)
  3221. iput(inode);
  3222. }
  3223. void __ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap,
  3224. bool *wake_ci, bool *wake_mdsc)
  3225. {
  3226. struct ceph_inode_info *ci = ceph_inode(inode);
  3227. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  3228. bool ret;
  3229. lockdep_assert_held(&ci->i_ceph_lock);
  3230. dout("removing capsnap %p, inode %p ci %p\n", capsnap, inode, ci);
  3231. list_del_init(&capsnap->ci_item);
  3232. ret = __detach_cap_flush_from_ci(ci, &capsnap->cap_flush);
  3233. if (wake_ci)
  3234. *wake_ci = ret;
  3235. spin_lock(&mdsc->cap_dirty_lock);
  3236. if (list_empty(&ci->i_cap_flush_list))
  3237. list_del_init(&ci->i_flushing_item);
  3238. ret = __detach_cap_flush_from_mdsc(mdsc, &capsnap->cap_flush);
  3239. if (wake_mdsc)
  3240. *wake_mdsc = ret;
  3241. spin_unlock(&mdsc->cap_dirty_lock);
  3242. }
  3243. void ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap,
  3244. bool *wake_ci, bool *wake_mdsc)
  3245. {
  3246. struct ceph_inode_info *ci = ceph_inode(inode);
  3247. lockdep_assert_held(&ci->i_ceph_lock);
  3248. WARN_ON_ONCE(capsnap->dirty_pages || capsnap->writing);
  3249. __ceph_remove_capsnap(inode, capsnap, wake_ci, wake_mdsc);
  3250. }
  3251. /*
  3252. * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
  3253. * throw away our cap_snap.
  3254. *
  3255. * Caller hold s_mutex.
  3256. */
  3257. static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
  3258. struct ceph_mds_caps *m,
  3259. struct ceph_mds_session *session)
  3260. {
  3261. struct ceph_inode_info *ci = ceph_inode(inode);
  3262. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  3263. u64 follows = le64_to_cpu(m->snap_follows);
  3264. struct ceph_cap_snap *capsnap;
  3265. bool flushed = false;
  3266. bool wake_ci = false;
  3267. bool wake_mdsc = false;
  3268. dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
  3269. inode, ci, session->s_mds, follows);
  3270. spin_lock(&ci->i_ceph_lock);
  3271. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  3272. if (capsnap->follows == follows) {
  3273. if (capsnap->cap_flush.tid != flush_tid) {
  3274. dout(" cap_snap %p follows %lld tid %lld !="
  3275. " %lld\n", capsnap, follows,
  3276. flush_tid, capsnap->cap_flush.tid);
  3277. break;
  3278. }
  3279. flushed = true;
  3280. break;
  3281. } else {
  3282. dout(" skipping cap_snap %p follows %lld\n",
  3283. capsnap, capsnap->follows);
  3284. }
  3285. }
  3286. if (flushed)
  3287. ceph_remove_capsnap(inode, capsnap, &wake_ci, &wake_mdsc);
  3288. spin_unlock(&ci->i_ceph_lock);
  3289. if (flushed) {
  3290. ceph_put_snap_context(capsnap->context);
  3291. ceph_put_cap_snap(capsnap);
  3292. if (wake_ci)
  3293. wake_up_all(&ci->i_cap_wq);
  3294. if (wake_mdsc)
  3295. wake_up_all(&mdsc->cap_flushing_wq);
  3296. iput(inode);
  3297. }
  3298. }
  3299. /*
  3300. * Handle TRUNC from MDS, indicating file truncation.
  3301. *
  3302. * caller hold s_mutex.
  3303. */
  3304. static bool handle_cap_trunc(struct inode *inode,
  3305. struct ceph_mds_caps *trunc,
  3306. struct ceph_mds_session *session)
  3307. {
  3308. struct ceph_inode_info *ci = ceph_inode(inode);
  3309. int mds = session->s_mds;
  3310. int seq = le32_to_cpu(trunc->seq);
  3311. u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
  3312. u64 truncate_size = le64_to_cpu(trunc->truncate_size);
  3313. u64 size = le64_to_cpu(trunc->size);
  3314. int implemented = 0;
  3315. int dirty = __ceph_caps_dirty(ci);
  3316. int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
  3317. bool queue_trunc = false;
  3318. lockdep_assert_held(&ci->i_ceph_lock);
  3319. issued |= implemented | dirty;
  3320. dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
  3321. inode, mds, seq, truncate_size, truncate_seq);
  3322. queue_trunc = ceph_fill_file_size(inode, issued,
  3323. truncate_seq, truncate_size, size);
  3324. return queue_trunc;
  3325. }
  3326. /*
  3327. * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
  3328. * different one. If we are the most recent migration we've seen (as
  3329. * indicated by mseq), make note of the migrating cap bits for the
  3330. * duration (until we see the corresponding IMPORT).
  3331. *
  3332. * caller holds s_mutex
  3333. */
  3334. static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
  3335. struct ceph_mds_cap_peer *ph,
  3336. struct ceph_mds_session *session)
  3337. {
  3338. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  3339. struct ceph_mds_session *tsession = NULL;
  3340. struct ceph_cap *cap, *tcap, *new_cap = NULL;
  3341. struct ceph_inode_info *ci = ceph_inode(inode);
  3342. u64 t_cap_id;
  3343. unsigned mseq = le32_to_cpu(ex->migrate_seq);
  3344. unsigned t_seq, t_mseq;
  3345. int target, issued;
  3346. int mds = session->s_mds;
  3347. if (ph) {
  3348. t_cap_id = le64_to_cpu(ph->cap_id);
  3349. t_seq = le32_to_cpu(ph->seq);
  3350. t_mseq = le32_to_cpu(ph->mseq);
  3351. target = le32_to_cpu(ph->mds);
  3352. } else {
  3353. t_cap_id = t_seq = t_mseq = 0;
  3354. target = -1;
  3355. }
  3356. dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
  3357. inode, ci, mds, mseq, target);
  3358. retry:
  3359. spin_lock(&ci->i_ceph_lock);
  3360. cap = __get_cap_for_mds(ci, mds);
  3361. if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
  3362. goto out_unlock;
  3363. if (target < 0) {
  3364. __ceph_remove_cap(cap, false);
  3365. goto out_unlock;
  3366. }
  3367. /*
  3368. * now we know we haven't received the cap import message yet
  3369. * because the exported cap still exist.
  3370. */
  3371. issued = cap->issued;
  3372. if (issued != cap->implemented)
  3373. pr_err_ratelimited("handle_cap_export: issued != implemented: "
  3374. "ino (%llx.%llx) mds%d seq %d mseq %d "
  3375. "issued %s implemented %s\n",
  3376. ceph_vinop(inode), mds, cap->seq, cap->mseq,
  3377. ceph_cap_string(issued),
  3378. ceph_cap_string(cap->implemented));
  3379. tcap = __get_cap_for_mds(ci, target);
  3380. if (tcap) {
  3381. /* already have caps from the target */
  3382. if (tcap->cap_id == t_cap_id &&
  3383. ceph_seq_cmp(tcap->seq, t_seq) < 0) {
  3384. dout(" updating import cap %p mds%d\n", tcap, target);
  3385. tcap->cap_id = t_cap_id;
  3386. tcap->seq = t_seq - 1;
  3387. tcap->issue_seq = t_seq - 1;
  3388. tcap->issued |= issued;
  3389. tcap->implemented |= issued;
  3390. if (cap == ci->i_auth_cap) {
  3391. ci->i_auth_cap = tcap;
  3392. change_auth_cap_ses(ci, tcap->session);
  3393. }
  3394. }
  3395. __ceph_remove_cap(cap, false);
  3396. goto out_unlock;
  3397. } else if (tsession) {
  3398. /* add placeholder for the export tagert */
  3399. int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
  3400. tcap = new_cap;
  3401. ceph_add_cap(inode, tsession, t_cap_id, issued, 0,
  3402. t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
  3403. if (!list_empty(&ci->i_cap_flush_list) &&
  3404. ci->i_auth_cap == tcap) {
  3405. spin_lock(&mdsc->cap_dirty_lock);
  3406. list_move_tail(&ci->i_flushing_item,
  3407. &tcap->session->s_cap_flushing);
  3408. spin_unlock(&mdsc->cap_dirty_lock);
  3409. }
  3410. __ceph_remove_cap(cap, false);
  3411. goto out_unlock;
  3412. }
  3413. spin_unlock(&ci->i_ceph_lock);
  3414. mutex_unlock(&session->s_mutex);
  3415. /* open target session */
  3416. tsession = ceph_mdsc_open_export_target_session(mdsc, target);
  3417. if (!IS_ERR(tsession)) {
  3418. if (mds > target) {
  3419. mutex_lock(&session->s_mutex);
  3420. mutex_lock_nested(&tsession->s_mutex,
  3421. SINGLE_DEPTH_NESTING);
  3422. } else {
  3423. mutex_lock(&tsession->s_mutex);
  3424. mutex_lock_nested(&session->s_mutex,
  3425. SINGLE_DEPTH_NESTING);
  3426. }
  3427. new_cap = ceph_get_cap(mdsc, NULL);
  3428. } else {
  3429. WARN_ON(1);
  3430. tsession = NULL;
  3431. target = -1;
  3432. mutex_lock(&session->s_mutex);
  3433. }
  3434. goto retry;
  3435. out_unlock:
  3436. spin_unlock(&ci->i_ceph_lock);
  3437. mutex_unlock(&session->s_mutex);
  3438. if (tsession) {
  3439. mutex_unlock(&tsession->s_mutex);
  3440. ceph_put_mds_session(tsession);
  3441. }
  3442. if (new_cap)
  3443. ceph_put_cap(mdsc, new_cap);
  3444. }
  3445. /*
  3446. * Handle cap IMPORT.
  3447. *
  3448. * caller holds s_mutex. acquires i_ceph_lock
  3449. */
  3450. static void handle_cap_import(struct ceph_mds_client *mdsc,
  3451. struct inode *inode, struct ceph_mds_caps *im,
  3452. struct ceph_mds_cap_peer *ph,
  3453. struct ceph_mds_session *session,
  3454. struct ceph_cap **target_cap, int *old_issued)
  3455. {
  3456. struct ceph_inode_info *ci = ceph_inode(inode);
  3457. struct ceph_cap *cap, *ocap, *new_cap = NULL;
  3458. int mds = session->s_mds;
  3459. int issued;
  3460. unsigned caps = le32_to_cpu(im->caps);
  3461. unsigned wanted = le32_to_cpu(im->wanted);
  3462. unsigned seq = le32_to_cpu(im->seq);
  3463. unsigned mseq = le32_to_cpu(im->migrate_seq);
  3464. u64 realmino = le64_to_cpu(im->realm);
  3465. u64 cap_id = le64_to_cpu(im->cap_id);
  3466. u64 p_cap_id;
  3467. int peer;
  3468. if (ph) {
  3469. p_cap_id = le64_to_cpu(ph->cap_id);
  3470. peer = le32_to_cpu(ph->mds);
  3471. } else {
  3472. p_cap_id = 0;
  3473. peer = -1;
  3474. }
  3475. dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
  3476. inode, ci, mds, mseq, peer);
  3477. retry:
  3478. cap = __get_cap_for_mds(ci, mds);
  3479. if (!cap) {
  3480. if (!new_cap) {
  3481. spin_unlock(&ci->i_ceph_lock);
  3482. new_cap = ceph_get_cap(mdsc, NULL);
  3483. spin_lock(&ci->i_ceph_lock);
  3484. goto retry;
  3485. }
  3486. cap = new_cap;
  3487. } else {
  3488. if (new_cap) {
  3489. ceph_put_cap(mdsc, new_cap);
  3490. new_cap = NULL;
  3491. }
  3492. }
  3493. __ceph_caps_issued(ci, &issued);
  3494. issued |= __ceph_caps_dirty(ci);
  3495. ceph_add_cap(inode, session, cap_id, caps, wanted, seq, mseq,
  3496. realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
  3497. ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
  3498. if (ocap && ocap->cap_id == p_cap_id) {
  3499. dout(" remove export cap %p mds%d flags %d\n",
  3500. ocap, peer, ph->flags);
  3501. if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
  3502. (ocap->seq != le32_to_cpu(ph->seq) ||
  3503. ocap->mseq != le32_to_cpu(ph->mseq))) {
  3504. pr_err_ratelimited("handle_cap_import: "
  3505. "mismatched seq/mseq: ino (%llx.%llx) "
  3506. "mds%d seq %d mseq %d importer mds%d "
  3507. "has peer seq %d mseq %d\n",
  3508. ceph_vinop(inode), peer, ocap->seq,
  3509. ocap->mseq, mds, le32_to_cpu(ph->seq),
  3510. le32_to_cpu(ph->mseq));
  3511. }
  3512. __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
  3513. }
  3514. *old_issued = issued;
  3515. *target_cap = cap;
  3516. }
  3517. /*
  3518. * Handle a caps message from the MDS.
  3519. *
  3520. * Identify the appropriate session, inode, and call the right handler
  3521. * based on the cap op.
  3522. */
  3523. void ceph_handle_caps(struct ceph_mds_session *session,
  3524. struct ceph_msg *msg)
  3525. {
  3526. struct ceph_mds_client *mdsc = session->s_mdsc;
  3527. struct inode *inode;
  3528. struct ceph_inode_info *ci;
  3529. struct ceph_cap *cap;
  3530. struct ceph_mds_caps *h;
  3531. struct ceph_mds_cap_peer *peer = NULL;
  3532. struct ceph_snap_realm *realm = NULL;
  3533. int op;
  3534. int msg_version = le16_to_cpu(msg->hdr.version);
  3535. u32 seq, mseq;
  3536. struct ceph_vino vino;
  3537. void *snaptrace;
  3538. size_t snaptrace_len;
  3539. void *p, *end;
  3540. struct cap_extra_info extra_info = {};
  3541. bool queue_trunc;
  3542. dout("handle_caps from mds%d\n", session->s_mds);
  3543. /* decode */
  3544. end = msg->front.iov_base + msg->front.iov_len;
  3545. if (msg->front.iov_len < sizeof(*h))
  3546. goto bad;
  3547. h = msg->front.iov_base;
  3548. op = le32_to_cpu(h->op);
  3549. vino.ino = le64_to_cpu(h->ino);
  3550. vino.snap = CEPH_NOSNAP;
  3551. seq = le32_to_cpu(h->seq);
  3552. mseq = le32_to_cpu(h->migrate_seq);
  3553. snaptrace = h + 1;
  3554. snaptrace_len = le32_to_cpu(h->snap_trace_len);
  3555. p = snaptrace + snaptrace_len;
  3556. if (msg_version >= 2) {
  3557. u32 flock_len;
  3558. ceph_decode_32_safe(&p, end, flock_len, bad);
  3559. if (p + flock_len > end)
  3560. goto bad;
  3561. p += flock_len;
  3562. }
  3563. if (msg_version >= 3) {
  3564. if (op == CEPH_CAP_OP_IMPORT) {
  3565. if (p + sizeof(*peer) > end)
  3566. goto bad;
  3567. peer = p;
  3568. p += sizeof(*peer);
  3569. } else if (op == CEPH_CAP_OP_EXPORT) {
  3570. /* recorded in unused fields */
  3571. peer = (void *)&h->size;
  3572. }
  3573. }
  3574. if (msg_version >= 4) {
  3575. ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
  3576. ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
  3577. if (p + extra_info.inline_len > end)
  3578. goto bad;
  3579. extra_info.inline_data = p;
  3580. p += extra_info.inline_len;
  3581. }
  3582. if (msg_version >= 5) {
  3583. struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
  3584. u32 epoch_barrier;
  3585. ceph_decode_32_safe(&p, end, epoch_barrier, bad);
  3586. ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
  3587. }
  3588. if (msg_version >= 8) {
  3589. u64 flush_tid;
  3590. u32 caller_uid, caller_gid;
  3591. u32 pool_ns_len;
  3592. /* version >= 6 */
  3593. ceph_decode_64_safe(&p, end, flush_tid, bad);
  3594. /* version >= 7 */
  3595. ceph_decode_32_safe(&p, end, caller_uid, bad);
  3596. ceph_decode_32_safe(&p, end, caller_gid, bad);
  3597. /* version >= 8 */
  3598. ceph_decode_32_safe(&p, end, pool_ns_len, bad);
  3599. if (pool_ns_len > 0) {
  3600. ceph_decode_need(&p, end, pool_ns_len, bad);
  3601. extra_info.pool_ns =
  3602. ceph_find_or_create_string(p, pool_ns_len);
  3603. p += pool_ns_len;
  3604. }
  3605. }
  3606. if (msg_version >= 9) {
  3607. struct ceph_timespec *btime;
  3608. if (p + sizeof(*btime) > end)
  3609. goto bad;
  3610. btime = p;
  3611. ceph_decode_timespec64(&extra_info.btime, btime);
  3612. p += sizeof(*btime);
  3613. ceph_decode_64_safe(&p, end, extra_info.change_attr, bad);
  3614. }
  3615. if (msg_version >= 11) {
  3616. u32 flags;
  3617. /* version >= 10 */
  3618. ceph_decode_32_safe(&p, end, flags, bad);
  3619. /* version >= 11 */
  3620. extra_info.dirstat_valid = true;
  3621. ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
  3622. ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
  3623. }
  3624. /* lookup ino */
  3625. inode = ceph_find_inode(mdsc->fsc->sb, vino);
  3626. ci = ceph_inode(inode);
  3627. dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
  3628. vino.snap, inode);
  3629. mutex_lock(&session->s_mutex);
  3630. inc_session_sequence(session);
  3631. dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
  3632. (unsigned)seq);
  3633. if (!inode) {
  3634. dout(" i don't have ino %llx\n", vino.ino);
  3635. if (op == CEPH_CAP_OP_IMPORT) {
  3636. cap = ceph_get_cap(mdsc, NULL);
  3637. cap->cap_ino = vino.ino;
  3638. cap->queue_release = 1;
  3639. cap->cap_id = le64_to_cpu(h->cap_id);
  3640. cap->mseq = mseq;
  3641. cap->seq = seq;
  3642. cap->issue_seq = seq;
  3643. spin_lock(&session->s_cap_lock);
  3644. __ceph_queue_cap_release(session, cap);
  3645. spin_unlock(&session->s_cap_lock);
  3646. }
  3647. goto flush_cap_releases;
  3648. }
  3649. /* these will work even if we don't have a cap yet */
  3650. switch (op) {
  3651. case CEPH_CAP_OP_FLUSHSNAP_ACK:
  3652. handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
  3653. h, session);
  3654. goto done;
  3655. case CEPH_CAP_OP_EXPORT:
  3656. handle_cap_export(inode, h, peer, session);
  3657. goto done_unlocked;
  3658. case CEPH_CAP_OP_IMPORT:
  3659. realm = NULL;
  3660. if (snaptrace_len) {
  3661. down_write(&mdsc->snap_rwsem);
  3662. ceph_update_snap_trace(mdsc, snaptrace,
  3663. snaptrace + snaptrace_len,
  3664. false, &realm);
  3665. downgrade_write(&mdsc->snap_rwsem);
  3666. } else {
  3667. down_read(&mdsc->snap_rwsem);
  3668. }
  3669. spin_lock(&ci->i_ceph_lock);
  3670. handle_cap_import(mdsc, inode, h, peer, session,
  3671. &cap, &extra_info.issued);
  3672. handle_cap_grant(inode, session, cap,
  3673. h, msg->middle, &extra_info);
  3674. if (realm)
  3675. ceph_put_snap_realm(mdsc, realm);
  3676. goto done_unlocked;
  3677. }
  3678. /* the rest require a cap */
  3679. spin_lock(&ci->i_ceph_lock);
  3680. cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
  3681. if (!cap) {
  3682. dout(" no cap on %p ino %llx.%llx from mds%d\n",
  3683. inode, ceph_ino(inode), ceph_snap(inode),
  3684. session->s_mds);
  3685. spin_unlock(&ci->i_ceph_lock);
  3686. goto flush_cap_releases;
  3687. }
  3688. /* note that each of these drops i_ceph_lock for us */
  3689. switch (op) {
  3690. case CEPH_CAP_OP_REVOKE:
  3691. case CEPH_CAP_OP_GRANT:
  3692. __ceph_caps_issued(ci, &extra_info.issued);
  3693. extra_info.issued |= __ceph_caps_dirty(ci);
  3694. handle_cap_grant(inode, session, cap,
  3695. h, msg->middle, &extra_info);
  3696. goto done_unlocked;
  3697. case CEPH_CAP_OP_FLUSH_ACK:
  3698. handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
  3699. h, session, cap);
  3700. break;
  3701. case CEPH_CAP_OP_TRUNC:
  3702. queue_trunc = handle_cap_trunc(inode, h, session);
  3703. spin_unlock(&ci->i_ceph_lock);
  3704. if (queue_trunc)
  3705. ceph_queue_vmtruncate(inode);
  3706. break;
  3707. default:
  3708. spin_unlock(&ci->i_ceph_lock);
  3709. pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
  3710. ceph_cap_op_name(op));
  3711. }
  3712. done:
  3713. mutex_unlock(&session->s_mutex);
  3714. done_unlocked:
  3715. ceph_put_string(extra_info.pool_ns);
  3716. /* avoid calling iput_final() in mds dispatch threads */
  3717. ceph_async_iput(inode);
  3718. return;
  3719. flush_cap_releases:
  3720. /*
  3721. * send any cap release message to try to move things
  3722. * along for the mds (who clearly thinks we still have this
  3723. * cap).
  3724. */
  3725. ceph_flush_cap_releases(mdsc, session);
  3726. goto done;
  3727. bad:
  3728. pr_err("ceph_handle_caps: corrupt message\n");
  3729. ceph_msg_dump(msg);
  3730. return;
  3731. }
  3732. /*
  3733. * Delayed work handler to process end of delayed cap release LRU list.
  3734. *
  3735. * If new caps are added to the list while processing it, these won't get
  3736. * processed in this run. In this case, the ci->i_hold_caps_max will be
  3737. * returned so that the work can be scheduled accordingly.
  3738. */
  3739. unsigned long ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
  3740. {
  3741. struct inode *inode;
  3742. struct ceph_inode_info *ci;
  3743. struct ceph_mount_options *opt = mdsc->fsc->mount_options;
  3744. unsigned long delay_max = opt->caps_wanted_delay_max * HZ;
  3745. unsigned long loop_start = jiffies;
  3746. unsigned long delay = 0;
  3747. dout("check_delayed_caps\n");
  3748. spin_lock(&mdsc->cap_delay_lock);
  3749. while (!list_empty(&mdsc->cap_delay_list)) {
  3750. ci = list_first_entry(&mdsc->cap_delay_list,
  3751. struct ceph_inode_info,
  3752. i_cap_delay_list);
  3753. if (time_before(loop_start, ci->i_hold_caps_max - delay_max)) {
  3754. dout("%s caps added recently. Exiting loop", __func__);
  3755. delay = ci->i_hold_caps_max;
  3756. break;
  3757. }
  3758. if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
  3759. time_before(jiffies, ci->i_hold_caps_max))
  3760. break;
  3761. list_del_init(&ci->i_cap_delay_list);
  3762. inode = igrab(&ci->vfs_inode);
  3763. if (inode) {
  3764. spin_unlock(&mdsc->cap_delay_lock);
  3765. dout("check_delayed_caps on %p\n", inode);
  3766. ceph_check_caps(ci, 0, NULL);
  3767. /* avoid calling iput_final() in tick thread */
  3768. ceph_async_iput(inode);
  3769. spin_lock(&mdsc->cap_delay_lock);
  3770. }
  3771. }
  3772. spin_unlock(&mdsc->cap_delay_lock);
  3773. return delay;
  3774. }
  3775. /*
  3776. * Flush all dirty caps to the mds
  3777. */
  3778. static void flush_dirty_session_caps(struct ceph_mds_session *s)
  3779. {
  3780. struct ceph_mds_client *mdsc = s->s_mdsc;
  3781. struct ceph_inode_info *ci;
  3782. struct inode *inode;
  3783. dout("flush_dirty_caps\n");
  3784. spin_lock(&mdsc->cap_dirty_lock);
  3785. while (!list_empty(&s->s_cap_dirty)) {
  3786. ci = list_first_entry(&s->s_cap_dirty, struct ceph_inode_info,
  3787. i_dirty_item);
  3788. inode = &ci->vfs_inode;
  3789. ihold(inode);
  3790. dout("flush_dirty_caps %p\n", inode);
  3791. spin_unlock(&mdsc->cap_dirty_lock);
  3792. ceph_check_caps(ci, CHECK_CAPS_FLUSH, NULL);
  3793. iput(inode);
  3794. spin_lock(&mdsc->cap_dirty_lock);
  3795. }
  3796. spin_unlock(&mdsc->cap_dirty_lock);
  3797. dout("flush_dirty_caps done\n");
  3798. }
  3799. static void iterate_sessions(struct ceph_mds_client *mdsc,
  3800. void (*cb)(struct ceph_mds_session *))
  3801. {
  3802. int mds;
  3803. mutex_lock(&mdsc->mutex);
  3804. for (mds = 0; mds < mdsc->max_sessions; ++mds) {
  3805. struct ceph_mds_session *s;
  3806. if (!mdsc->sessions[mds])
  3807. continue;
  3808. s = ceph_get_mds_session(mdsc->sessions[mds]);
  3809. if (!s)
  3810. continue;
  3811. mutex_unlock(&mdsc->mutex);
  3812. cb(s);
  3813. ceph_put_mds_session(s);
  3814. mutex_lock(&mdsc->mutex);
  3815. }
  3816. mutex_unlock(&mdsc->mutex);
  3817. }
  3818. void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
  3819. {
  3820. iterate_sessions(mdsc, flush_dirty_session_caps);
  3821. }
  3822. void __ceph_touch_fmode(struct ceph_inode_info *ci,
  3823. struct ceph_mds_client *mdsc, int fmode)
  3824. {
  3825. unsigned long now = jiffies;
  3826. if (fmode & CEPH_FILE_MODE_RD)
  3827. ci->i_last_rd = now;
  3828. if (fmode & CEPH_FILE_MODE_WR)
  3829. ci->i_last_wr = now;
  3830. /* queue periodic check */
  3831. if (fmode &&
  3832. __ceph_is_any_real_caps(ci) &&
  3833. list_empty(&ci->i_cap_delay_list))
  3834. __cap_delay_requeue(mdsc, ci);
  3835. }
  3836. void ceph_get_fmode(struct ceph_inode_info *ci, int fmode, int count)
  3837. {
  3838. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->vfs_inode.i_sb);
  3839. int bits = (fmode << 1) | 1;
  3840. bool already_opened = false;
  3841. int i;
  3842. if (count == 1)
  3843. atomic64_inc(&mdsc->metric.opened_files);
  3844. spin_lock(&ci->i_ceph_lock);
  3845. for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
  3846. /*
  3847. * If any of the mode ref is larger than 0,
  3848. * that means it has been already opened by
  3849. * others. Just skip checking the PIN ref.
  3850. */
  3851. if (i && ci->i_nr_by_mode[i])
  3852. already_opened = true;
  3853. if (bits & (1 << i))
  3854. ci->i_nr_by_mode[i] += count;
  3855. }
  3856. if (!already_opened)
  3857. percpu_counter_inc(&mdsc->metric.opened_inodes);
  3858. spin_unlock(&ci->i_ceph_lock);
  3859. }
  3860. /*
  3861. * Drop open file reference. If we were the last open file,
  3862. * we may need to release capabilities to the MDS (or schedule
  3863. * their delayed release).
  3864. */
  3865. void ceph_put_fmode(struct ceph_inode_info *ci, int fmode, int count)
  3866. {
  3867. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->vfs_inode.i_sb);
  3868. int bits = (fmode << 1) | 1;
  3869. bool is_closed = true;
  3870. int i;
  3871. if (count == 1)
  3872. atomic64_dec(&mdsc->metric.opened_files);
  3873. spin_lock(&ci->i_ceph_lock);
  3874. for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
  3875. if (bits & (1 << i)) {
  3876. BUG_ON(ci->i_nr_by_mode[i] < count);
  3877. ci->i_nr_by_mode[i] -= count;
  3878. }
  3879. /*
  3880. * If any of the mode ref is not 0 after
  3881. * decreased, that means it is still opened
  3882. * by others. Just skip checking the PIN ref.
  3883. */
  3884. if (i && ci->i_nr_by_mode[i])
  3885. is_closed = false;
  3886. }
  3887. if (is_closed)
  3888. percpu_counter_dec(&mdsc->metric.opened_inodes);
  3889. spin_unlock(&ci->i_ceph_lock);
  3890. }
  3891. /*
  3892. * For a soon-to-be unlinked file, drop the LINK caps. If it
  3893. * looks like the link count will hit 0, drop any other caps (other
  3894. * than PIN) we don't specifically want (due to the file still being
  3895. * open).
  3896. */
  3897. int ceph_drop_caps_for_unlink(struct inode *inode)
  3898. {
  3899. struct ceph_inode_info *ci = ceph_inode(inode);
  3900. int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
  3901. spin_lock(&ci->i_ceph_lock);
  3902. if (inode->i_nlink == 1) {
  3903. drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
  3904. if (__ceph_caps_dirty(ci)) {
  3905. struct ceph_mds_client *mdsc =
  3906. ceph_inode_to_client(inode)->mdsc;
  3907. __cap_delay_requeue_front(mdsc, ci);
  3908. }
  3909. }
  3910. spin_unlock(&ci->i_ceph_lock);
  3911. return drop;
  3912. }
  3913. /*
  3914. * Helpers for embedding cap and dentry lease releases into mds
  3915. * requests.
  3916. *
  3917. * @force is used by dentry_release (below) to force inclusion of a
  3918. * record for the directory inode, even when there aren't any caps to
  3919. * drop.
  3920. */
  3921. int ceph_encode_inode_release(void **p, struct inode *inode,
  3922. int mds, int drop, int unless, int force)
  3923. {
  3924. struct ceph_inode_info *ci = ceph_inode(inode);
  3925. struct ceph_cap *cap;
  3926. struct ceph_mds_request_release *rel = *p;
  3927. int used, dirty;
  3928. int ret = 0;
  3929. spin_lock(&ci->i_ceph_lock);
  3930. used = __ceph_caps_used(ci);
  3931. dirty = __ceph_caps_dirty(ci);
  3932. dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
  3933. inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
  3934. ceph_cap_string(unless));
  3935. /* only drop unused, clean caps */
  3936. drop &= ~(used | dirty);
  3937. cap = __get_cap_for_mds(ci, mds);
  3938. if (cap && __cap_is_valid(cap)) {
  3939. unless &= cap->issued;
  3940. if (unless) {
  3941. if (unless & CEPH_CAP_AUTH_EXCL)
  3942. drop &= ~CEPH_CAP_AUTH_SHARED;
  3943. if (unless & CEPH_CAP_LINK_EXCL)
  3944. drop &= ~CEPH_CAP_LINK_SHARED;
  3945. if (unless & CEPH_CAP_XATTR_EXCL)
  3946. drop &= ~CEPH_CAP_XATTR_SHARED;
  3947. if (unless & CEPH_CAP_FILE_EXCL)
  3948. drop &= ~CEPH_CAP_FILE_SHARED;
  3949. }
  3950. if (force || (cap->issued & drop)) {
  3951. if (cap->issued & drop) {
  3952. int wanted = __ceph_caps_wanted(ci);
  3953. dout("encode_inode_release %p cap %p "
  3954. "%s -> %s, wanted %s -> %s\n", inode, cap,
  3955. ceph_cap_string(cap->issued),
  3956. ceph_cap_string(cap->issued & ~drop),
  3957. ceph_cap_string(cap->mds_wanted),
  3958. ceph_cap_string(wanted));
  3959. cap->issued &= ~drop;
  3960. cap->implemented &= ~drop;
  3961. cap->mds_wanted = wanted;
  3962. if (cap == ci->i_auth_cap &&
  3963. !(wanted & CEPH_CAP_ANY_FILE_WR))
  3964. ci->i_requested_max_size = 0;
  3965. } else {
  3966. dout("encode_inode_release %p cap %p %s"
  3967. " (force)\n", inode, cap,
  3968. ceph_cap_string(cap->issued));
  3969. }
  3970. rel->ino = cpu_to_le64(ceph_ino(inode));
  3971. rel->cap_id = cpu_to_le64(cap->cap_id);
  3972. rel->seq = cpu_to_le32(cap->seq);
  3973. rel->issue_seq = cpu_to_le32(cap->issue_seq);
  3974. rel->mseq = cpu_to_le32(cap->mseq);
  3975. rel->caps = cpu_to_le32(cap->implemented);
  3976. rel->wanted = cpu_to_le32(cap->mds_wanted);
  3977. rel->dname_len = 0;
  3978. rel->dname_seq = 0;
  3979. *p += sizeof(*rel);
  3980. ret = 1;
  3981. } else {
  3982. dout("encode_inode_release %p cap %p %s (noop)\n",
  3983. inode, cap, ceph_cap_string(cap->issued));
  3984. }
  3985. }
  3986. spin_unlock(&ci->i_ceph_lock);
  3987. return ret;
  3988. }
  3989. int ceph_encode_dentry_release(void **p, struct dentry *dentry,
  3990. struct inode *dir,
  3991. int mds, int drop, int unless)
  3992. {
  3993. struct dentry *parent = NULL;
  3994. struct ceph_mds_request_release *rel = *p;
  3995. struct ceph_dentry_info *di = ceph_dentry(dentry);
  3996. int force = 0;
  3997. int ret;
  3998. /*
  3999. * force an record for the directory caps if we have a dentry lease.
  4000. * this is racy (can't take i_ceph_lock and d_lock together), but it
  4001. * doesn't have to be perfect; the mds will revoke anything we don't
  4002. * release.
  4003. */
  4004. spin_lock(&dentry->d_lock);
  4005. if (di->lease_session && di->lease_session->s_mds == mds)
  4006. force = 1;
  4007. if (!dir) {
  4008. parent = dget(dentry->d_parent);
  4009. dir = d_inode(parent);
  4010. }
  4011. spin_unlock(&dentry->d_lock);
  4012. ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
  4013. dput(parent);
  4014. spin_lock(&dentry->d_lock);
  4015. if (ret && di->lease_session && di->lease_session->s_mds == mds) {
  4016. dout("encode_dentry_release %p mds%d seq %d\n",
  4017. dentry, mds, (int)di->lease_seq);
  4018. rel->dname_len = cpu_to_le32(dentry->d_name.len);
  4019. memcpy(*p, dentry->d_name.name, dentry->d_name.len);
  4020. *p += dentry->d_name.len;
  4021. rel->dname_seq = cpu_to_le32(di->lease_seq);
  4022. __ceph_mdsc_drop_dentry_lease(dentry);
  4023. }
  4024. spin_unlock(&dentry->d_lock);
  4025. return ret;
  4026. }