caps.c 105 KB

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