caps.c 103 KB

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