caps.c 106 KB

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