caps.c 94 KB

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