caps.c 112 KB

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