caps.c 108 KB

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