caps.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048
  1. #include "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 "decode.h"
  11. #include "messenger.h"
  12. /*
  13. * Capability management
  14. *
  15. * The Ceph metadata servers control client access to inode metadata
  16. * and file data by issuing capabilities, granting clients permission
  17. * to read and/or write both inode field and file data to OSDs
  18. * (storage nodes). Each capability consists of a set of bits
  19. * indicating which operations are allowed.
  20. *
  21. * If the client holds a *_SHARED cap, the client has a coherent value
  22. * that can be safely read from the cached inode.
  23. *
  24. * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
  25. * client is allowed to change inode attributes (e.g., file size,
  26. * mtime), note its dirty state in the ceph_cap, and asynchronously
  27. * flush that metadata change to the MDS.
  28. *
  29. * In the event of a conflicting operation (perhaps by another
  30. * client), the MDS will revoke the conflicting client capabilities.
  31. *
  32. * In order for a client to cache an inode, it must hold a capability
  33. * with at least one MDS server. When inodes are released, release
  34. * notifications are batched and periodically sent en masse to the MDS
  35. * cluster to release server state.
  36. */
  37. /*
  38. * Generate readable cap strings for debugging output.
  39. */
  40. #define MAX_CAP_STR 20
  41. static char cap_str[MAX_CAP_STR][40];
  42. static DEFINE_SPINLOCK(cap_str_lock);
  43. static int last_cap_str;
  44. static char *gcap_string(char *s, int c)
  45. {
  46. if (c & CEPH_CAP_GSHARED)
  47. *s++ = 's';
  48. if (c & CEPH_CAP_GEXCL)
  49. *s++ = 'x';
  50. if (c & CEPH_CAP_GCACHE)
  51. *s++ = 'c';
  52. if (c & CEPH_CAP_GRD)
  53. *s++ = 'r';
  54. if (c & CEPH_CAP_GWR)
  55. *s++ = 'w';
  56. if (c & CEPH_CAP_GBUFFER)
  57. *s++ = 'b';
  58. if (c & CEPH_CAP_GLAZYIO)
  59. *s++ = 'l';
  60. return s;
  61. }
  62. const char *ceph_cap_string(int caps)
  63. {
  64. int i;
  65. char *s;
  66. int c;
  67. spin_lock(&cap_str_lock);
  68. i = last_cap_str++;
  69. if (last_cap_str == MAX_CAP_STR)
  70. last_cap_str = 0;
  71. spin_unlock(&cap_str_lock);
  72. s = cap_str[i];
  73. if (caps & CEPH_CAP_PIN)
  74. *s++ = 'p';
  75. c = (caps >> CEPH_CAP_SAUTH) & 3;
  76. if (c) {
  77. *s++ = 'A';
  78. s = gcap_string(s, c);
  79. }
  80. c = (caps >> CEPH_CAP_SLINK) & 3;
  81. if (c) {
  82. *s++ = 'L';
  83. s = gcap_string(s, c);
  84. }
  85. c = (caps >> CEPH_CAP_SXATTR) & 3;
  86. if (c) {
  87. *s++ = 'X';
  88. s = gcap_string(s, c);
  89. }
  90. c = caps >> CEPH_CAP_SFILE;
  91. if (c) {
  92. *s++ = 'F';
  93. s = gcap_string(s, c);
  94. }
  95. if (s == cap_str[i])
  96. *s++ = '-';
  97. *s = 0;
  98. return cap_str[i];
  99. }
  100. void ceph_caps_init(struct ceph_mds_client *mdsc)
  101. {
  102. INIT_LIST_HEAD(&mdsc->caps_list);
  103. spin_lock_init(&mdsc->caps_list_lock);
  104. }
  105. void ceph_caps_finalize(struct ceph_mds_client *mdsc)
  106. {
  107. struct ceph_cap *cap;
  108. spin_lock(&mdsc->caps_list_lock);
  109. while (!list_empty(&mdsc->caps_list)) {
  110. cap = list_first_entry(&mdsc->caps_list,
  111. struct ceph_cap, caps_item);
  112. list_del(&cap->caps_item);
  113. kmem_cache_free(ceph_cap_cachep, cap);
  114. }
  115. mdsc->caps_total_count = 0;
  116. mdsc->caps_avail_count = 0;
  117. mdsc->caps_use_count = 0;
  118. mdsc->caps_reserve_count = 0;
  119. mdsc->caps_min_count = 0;
  120. spin_unlock(&mdsc->caps_list_lock);
  121. }
  122. void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
  123. {
  124. spin_lock(&mdsc->caps_list_lock);
  125. mdsc->caps_min_count += delta;
  126. BUG_ON(mdsc->caps_min_count < 0);
  127. spin_unlock(&mdsc->caps_list_lock);
  128. }
  129. int ceph_reserve_caps(struct ceph_mds_client *mdsc,
  130. struct ceph_cap_reservation *ctx, int need)
  131. {
  132. int i;
  133. struct ceph_cap *cap;
  134. int have;
  135. int alloc = 0;
  136. LIST_HEAD(newcaps);
  137. int ret = 0;
  138. dout("reserve caps ctx=%p need=%d\n", ctx, need);
  139. /* first reserve any caps that are already allocated */
  140. spin_lock(&mdsc->caps_list_lock);
  141. if (mdsc->caps_avail_count >= need)
  142. have = need;
  143. else
  144. have = mdsc->caps_avail_count;
  145. mdsc->caps_avail_count -= have;
  146. mdsc->caps_reserve_count += have;
  147. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  148. mdsc->caps_reserve_count +
  149. mdsc->caps_avail_count);
  150. spin_unlock(&mdsc->caps_list_lock);
  151. for (i = have; i < need; i++) {
  152. cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
  153. if (!cap) {
  154. ret = -ENOMEM;
  155. goto out_alloc_count;
  156. }
  157. list_add(&cap->caps_item, &newcaps);
  158. alloc++;
  159. }
  160. BUG_ON(have + alloc != need);
  161. spin_lock(&mdsc->caps_list_lock);
  162. mdsc->caps_total_count += alloc;
  163. mdsc->caps_reserve_count += alloc;
  164. list_splice(&newcaps, &mdsc->caps_list);
  165. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  166. mdsc->caps_reserve_count +
  167. mdsc->caps_avail_count);
  168. spin_unlock(&mdsc->caps_list_lock);
  169. ctx->count = need;
  170. dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
  171. ctx, mdsc->caps_total_count, mdsc->caps_use_count,
  172. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  173. return 0;
  174. out_alloc_count:
  175. /* we didn't manage to reserve as much as we needed */
  176. pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
  177. ctx, need, have);
  178. return ret;
  179. }
  180. int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
  181. struct ceph_cap_reservation *ctx)
  182. {
  183. dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
  184. if (ctx->count) {
  185. spin_lock(&mdsc->caps_list_lock);
  186. BUG_ON(mdsc->caps_reserve_count < ctx->count);
  187. mdsc->caps_reserve_count -= ctx->count;
  188. mdsc->caps_avail_count += ctx->count;
  189. ctx->count = 0;
  190. dout("unreserve caps %d = %d used + %d resv + %d avail\n",
  191. mdsc->caps_total_count, mdsc->caps_use_count,
  192. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  193. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  194. mdsc->caps_reserve_count +
  195. mdsc->caps_avail_count);
  196. spin_unlock(&mdsc->caps_list_lock);
  197. }
  198. return 0;
  199. }
  200. static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc,
  201. struct ceph_cap_reservation *ctx)
  202. {
  203. struct ceph_cap *cap = NULL;
  204. /* temporary, until we do something about cap import/export */
  205. if (!ctx) {
  206. cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
  207. if (cap) {
  208. mdsc->caps_use_count++;
  209. mdsc->caps_total_count++;
  210. }
  211. return cap;
  212. }
  213. spin_lock(&mdsc->caps_list_lock);
  214. dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
  215. ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
  216. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  217. BUG_ON(!ctx->count);
  218. BUG_ON(ctx->count > mdsc->caps_reserve_count);
  219. BUG_ON(list_empty(&mdsc->caps_list));
  220. ctx->count--;
  221. mdsc->caps_reserve_count--;
  222. mdsc->caps_use_count++;
  223. cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
  224. list_del(&cap->caps_item);
  225. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  226. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  227. spin_unlock(&mdsc->caps_list_lock);
  228. return cap;
  229. }
  230. void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
  231. {
  232. spin_lock(&mdsc->caps_list_lock);
  233. dout("put_cap %p %d = %d used + %d resv + %d avail\n",
  234. cap, mdsc->caps_total_count, mdsc->caps_use_count,
  235. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  236. mdsc->caps_use_count--;
  237. /*
  238. * Keep some preallocated caps around (ceph_min_count), to
  239. * avoid lots of free/alloc churn.
  240. */
  241. if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
  242. mdsc->caps_min_count) {
  243. mdsc->caps_total_count--;
  244. kmem_cache_free(ceph_cap_cachep, cap);
  245. } else {
  246. mdsc->caps_avail_count++;
  247. list_add(&cap->caps_item, &mdsc->caps_list);
  248. }
  249. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  250. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  251. spin_unlock(&mdsc->caps_list_lock);
  252. }
  253. void ceph_reservation_status(struct ceph_client *client,
  254. int *total, int *avail, int *used, int *reserved,
  255. int *min)
  256. {
  257. struct ceph_mds_client *mdsc = &client->mdsc;
  258. if (total)
  259. *total = mdsc->caps_total_count;
  260. if (avail)
  261. *avail = mdsc->caps_avail_count;
  262. if (used)
  263. *used = mdsc->caps_use_count;
  264. if (reserved)
  265. *reserved = mdsc->caps_reserve_count;
  266. if (min)
  267. *min = mdsc->caps_min_count;
  268. }
  269. /*
  270. * Find ceph_cap for given mds, if any.
  271. *
  272. * Called with i_lock held.
  273. */
  274. static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  275. {
  276. struct ceph_cap *cap;
  277. struct rb_node *n = ci->i_caps.rb_node;
  278. while (n) {
  279. cap = rb_entry(n, struct ceph_cap, ci_node);
  280. if (mds < cap->mds)
  281. n = n->rb_left;
  282. else if (mds > cap->mds)
  283. n = n->rb_right;
  284. else
  285. return cap;
  286. }
  287. return NULL;
  288. }
  289. struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  290. {
  291. struct ceph_cap *cap;
  292. spin_lock(&ci->vfs_inode.i_lock);
  293. cap = __get_cap_for_mds(ci, mds);
  294. spin_unlock(&ci->vfs_inode.i_lock);
  295. return cap;
  296. }
  297. /*
  298. * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
  299. */
  300. static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
  301. {
  302. struct ceph_cap *cap;
  303. int mds = -1;
  304. struct rb_node *p;
  305. /* prefer mds with WR|BUFFER|EXCL caps */
  306. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  307. cap = rb_entry(p, struct ceph_cap, ci_node);
  308. mds = cap->mds;
  309. if (cap->issued & (CEPH_CAP_FILE_WR |
  310. CEPH_CAP_FILE_BUFFER |
  311. CEPH_CAP_FILE_EXCL))
  312. break;
  313. }
  314. return mds;
  315. }
  316. int ceph_get_cap_mds(struct inode *inode)
  317. {
  318. int mds;
  319. spin_lock(&inode->i_lock);
  320. mds = __ceph_get_cap_mds(ceph_inode(inode));
  321. spin_unlock(&inode->i_lock);
  322. return mds;
  323. }
  324. /*
  325. * Called under i_lock.
  326. */
  327. static void __insert_cap_node(struct ceph_inode_info *ci,
  328. struct ceph_cap *new)
  329. {
  330. struct rb_node **p = &ci->i_caps.rb_node;
  331. struct rb_node *parent = NULL;
  332. struct ceph_cap *cap = NULL;
  333. while (*p) {
  334. parent = *p;
  335. cap = rb_entry(parent, struct ceph_cap, ci_node);
  336. if (new->mds < cap->mds)
  337. p = &(*p)->rb_left;
  338. else if (new->mds > cap->mds)
  339. p = &(*p)->rb_right;
  340. else
  341. BUG();
  342. }
  343. rb_link_node(&new->ci_node, parent, p);
  344. rb_insert_color(&new->ci_node, &ci->i_caps);
  345. }
  346. /*
  347. * (re)set cap hold timeouts, which control the delayed release
  348. * of unused caps back to the MDS. Should be called on cap use.
  349. */
  350. static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
  351. struct ceph_inode_info *ci)
  352. {
  353. struct ceph_mount_args *ma = mdsc->client->mount_args;
  354. ci->i_hold_caps_min = round_jiffies(jiffies +
  355. ma->caps_wanted_delay_min * HZ);
  356. ci->i_hold_caps_max = round_jiffies(jiffies +
  357. ma->caps_wanted_delay_max * HZ);
  358. dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
  359. ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
  360. }
  361. /*
  362. * (Re)queue cap at the end of the delayed cap release list.
  363. *
  364. * If I_FLUSH is set, leave the inode at the front of the list.
  365. *
  366. * Caller holds i_lock
  367. * -> we take mdsc->cap_delay_lock
  368. */
  369. static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
  370. struct ceph_inode_info *ci)
  371. {
  372. __cap_set_timeouts(mdsc, ci);
  373. dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
  374. ci->i_ceph_flags, ci->i_hold_caps_max);
  375. if (!mdsc->stopping) {
  376. spin_lock(&mdsc->cap_delay_lock);
  377. if (!list_empty(&ci->i_cap_delay_list)) {
  378. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  379. goto no_change;
  380. list_del_init(&ci->i_cap_delay_list);
  381. }
  382. list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  383. no_change:
  384. spin_unlock(&mdsc->cap_delay_lock);
  385. }
  386. }
  387. /*
  388. * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
  389. * indicating we should send a cap message to flush dirty metadata
  390. * asap, and move to the front of the delayed cap list.
  391. */
  392. static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
  393. struct ceph_inode_info *ci)
  394. {
  395. dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
  396. spin_lock(&mdsc->cap_delay_lock);
  397. ci->i_ceph_flags |= CEPH_I_FLUSH;
  398. if (!list_empty(&ci->i_cap_delay_list))
  399. list_del_init(&ci->i_cap_delay_list);
  400. list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  401. spin_unlock(&mdsc->cap_delay_lock);
  402. }
  403. /*
  404. * Cancel delayed work on cap.
  405. *
  406. * Caller must hold i_lock.
  407. */
  408. static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
  409. struct ceph_inode_info *ci)
  410. {
  411. dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
  412. if (list_empty(&ci->i_cap_delay_list))
  413. return;
  414. spin_lock(&mdsc->cap_delay_lock);
  415. list_del_init(&ci->i_cap_delay_list);
  416. spin_unlock(&mdsc->cap_delay_lock);
  417. }
  418. /*
  419. * Common issue checks for add_cap, handle_cap_grant.
  420. */
  421. static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
  422. unsigned issued)
  423. {
  424. unsigned had = __ceph_caps_issued(ci, NULL);
  425. /*
  426. * Each time we receive FILE_CACHE anew, we increment
  427. * i_rdcache_gen.
  428. */
  429. if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
  430. (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
  431. ci->i_rdcache_gen++;
  432. /*
  433. * if we are newly issued FILE_SHARED, clear I_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. ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
  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. int 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_reservation *caps_reservation)
  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 *new_cap = NULL;
  464. struct ceph_cap *cap;
  465. int mds = session->s_mds;
  466. int actual_wanted;
  467. dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
  468. session->s_mds, cap_id, ceph_cap_string(issued), seq);
  469. /*
  470. * If we are opening the file, include file mode wanted bits
  471. * in wanted.
  472. */
  473. if (fmode >= 0)
  474. wanted |= ceph_caps_for_mode(fmode);
  475. retry:
  476. spin_lock(&inode->i_lock);
  477. cap = __get_cap_for_mds(ci, mds);
  478. if (!cap) {
  479. if (new_cap) {
  480. cap = new_cap;
  481. new_cap = NULL;
  482. } else {
  483. spin_unlock(&inode->i_lock);
  484. new_cap = get_cap(mdsc, caps_reservation);
  485. if (new_cap == NULL)
  486. return -ENOMEM;
  487. goto retry;
  488. }
  489. cap->issued = 0;
  490. cap->implemented = 0;
  491. cap->mds = mds;
  492. cap->mds_wanted = 0;
  493. cap->ci = ci;
  494. __insert_cap_node(ci, cap);
  495. /* clear out old exporting info? (i.e. on cap import) */
  496. if (ci->i_cap_exporting_mds == mds) {
  497. ci->i_cap_exporting_issued = 0;
  498. ci->i_cap_exporting_mseq = 0;
  499. ci->i_cap_exporting_mds = -1;
  500. }
  501. /* add to session cap list */
  502. cap->session = session;
  503. spin_lock(&session->s_cap_lock);
  504. list_add_tail(&cap->session_caps, &session->s_caps);
  505. session->s_nr_caps++;
  506. spin_unlock(&session->s_cap_lock);
  507. }
  508. if (!ci->i_snap_realm) {
  509. /*
  510. * add this inode to the appropriate snap realm
  511. */
  512. struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
  513. realmino);
  514. if (realm) {
  515. ceph_get_snap_realm(mdsc, realm);
  516. spin_lock(&realm->inodes_with_caps_lock);
  517. ci->i_snap_realm = realm;
  518. list_add(&ci->i_snap_realm_item,
  519. &realm->inodes_with_caps);
  520. spin_unlock(&realm->inodes_with_caps_lock);
  521. } else {
  522. pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
  523. realmino);
  524. WARN_ON(!realm);
  525. }
  526. }
  527. __check_cap_issue(ci, cap, issued);
  528. /*
  529. * If we are issued caps we don't want, or the mds' wanted
  530. * value appears to be off, queue a check so we'll release
  531. * later and/or update the mds wanted value.
  532. */
  533. actual_wanted = __ceph_caps_wanted(ci);
  534. if ((wanted & ~actual_wanted) ||
  535. (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
  536. dout(" issued %s, mds wanted %s, actual %s, queueing\n",
  537. ceph_cap_string(issued), ceph_cap_string(wanted),
  538. ceph_cap_string(actual_wanted));
  539. __cap_delay_requeue(mdsc, ci);
  540. }
  541. if (flags & CEPH_CAP_FLAG_AUTH)
  542. ci->i_auth_cap = cap;
  543. else if (ci->i_auth_cap == cap)
  544. ci->i_auth_cap = NULL;
  545. dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
  546. inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
  547. ceph_cap_string(issued|cap->issued), seq, mds);
  548. cap->cap_id = cap_id;
  549. cap->issued = issued;
  550. cap->implemented |= issued;
  551. cap->mds_wanted |= wanted;
  552. cap->seq = seq;
  553. cap->issue_seq = seq;
  554. cap->mseq = mseq;
  555. cap->cap_gen = session->s_cap_gen;
  556. if (fmode >= 0)
  557. __ceph_get_fmode(ci, fmode);
  558. spin_unlock(&inode->i_lock);
  559. wake_up_all(&ci->i_cap_wq);
  560. return 0;
  561. }
  562. /*
  563. * Return true if cap has not timed out and belongs to the current
  564. * generation of the MDS session (i.e. has not gone 'stale' due to
  565. * us losing touch with the mds).
  566. */
  567. static int __cap_is_valid(struct ceph_cap *cap)
  568. {
  569. unsigned long ttl;
  570. u32 gen;
  571. spin_lock(&cap->session->s_cap_lock);
  572. gen = cap->session->s_cap_gen;
  573. ttl = cap->session->s_cap_ttl;
  574. spin_unlock(&cap->session->s_cap_lock);
  575. if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
  576. dout("__cap_is_valid %p cap %p issued %s "
  577. "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
  578. cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
  579. return 0;
  580. }
  581. return 1;
  582. }
  583. /*
  584. * Return set of valid cap bits issued to us. Note that caps time
  585. * out, and may be invalidated in bulk if the client session times out
  586. * and session->s_cap_gen is bumped.
  587. */
  588. int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
  589. {
  590. int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
  591. struct ceph_cap *cap;
  592. struct rb_node *p;
  593. if (implemented)
  594. *implemented = 0;
  595. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  596. cap = rb_entry(p, struct ceph_cap, ci_node);
  597. if (!__cap_is_valid(cap))
  598. continue;
  599. dout("__ceph_caps_issued %p cap %p issued %s\n",
  600. &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
  601. have |= cap->issued;
  602. if (implemented)
  603. *implemented |= cap->implemented;
  604. }
  605. return have;
  606. }
  607. /*
  608. * Get cap bits issued by caps other than @ocap
  609. */
  610. int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
  611. {
  612. int have = ci->i_snap_caps;
  613. struct ceph_cap *cap;
  614. struct rb_node *p;
  615. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  616. cap = rb_entry(p, struct ceph_cap, ci_node);
  617. if (cap == ocap)
  618. continue;
  619. if (!__cap_is_valid(cap))
  620. continue;
  621. have |= cap->issued;
  622. }
  623. return have;
  624. }
  625. /*
  626. * Move a cap to the end of the LRU (oldest caps at list head, newest
  627. * at list tail).
  628. */
  629. static void __touch_cap(struct ceph_cap *cap)
  630. {
  631. struct ceph_mds_session *s = cap->session;
  632. spin_lock(&s->s_cap_lock);
  633. if (s->s_cap_iterator == NULL) {
  634. dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
  635. s->s_mds);
  636. list_move_tail(&cap->session_caps, &s->s_caps);
  637. } else {
  638. dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
  639. &cap->ci->vfs_inode, cap, s->s_mds);
  640. }
  641. spin_unlock(&s->s_cap_lock);
  642. }
  643. /*
  644. * Check if we hold the given mask. If so, move the cap(s) to the
  645. * front of their respective LRUs. (This is the preferred way for
  646. * callers to check for caps they want.)
  647. */
  648. int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
  649. {
  650. struct ceph_cap *cap;
  651. struct rb_node *p;
  652. int have = ci->i_snap_caps;
  653. if ((have & mask) == mask) {
  654. dout("__ceph_caps_issued_mask %p snap issued %s"
  655. " (mask %s)\n", &ci->vfs_inode,
  656. ceph_cap_string(have),
  657. ceph_cap_string(mask));
  658. return 1;
  659. }
  660. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  661. cap = rb_entry(p, struct ceph_cap, ci_node);
  662. if (!__cap_is_valid(cap))
  663. continue;
  664. if ((cap->issued & mask) == mask) {
  665. dout("__ceph_caps_issued_mask %p cap %p issued %s"
  666. " (mask %s)\n", &ci->vfs_inode, cap,
  667. ceph_cap_string(cap->issued),
  668. ceph_cap_string(mask));
  669. if (touch)
  670. __touch_cap(cap);
  671. return 1;
  672. }
  673. /* does a combination of caps satisfy mask? */
  674. have |= cap->issued;
  675. if ((have & mask) == mask) {
  676. dout("__ceph_caps_issued_mask %p combo issued %s"
  677. " (mask %s)\n", &ci->vfs_inode,
  678. ceph_cap_string(cap->issued),
  679. ceph_cap_string(mask));
  680. if (touch) {
  681. struct rb_node *q;
  682. /* touch this + preceeding caps */
  683. __touch_cap(cap);
  684. for (q = rb_first(&ci->i_caps); q != p;
  685. q = rb_next(q)) {
  686. cap = rb_entry(q, struct ceph_cap,
  687. ci_node);
  688. if (!__cap_is_valid(cap))
  689. continue;
  690. __touch_cap(cap);
  691. }
  692. }
  693. return 1;
  694. }
  695. }
  696. return 0;
  697. }
  698. /*
  699. * Return true if mask caps are currently being revoked by an MDS.
  700. */
  701. int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
  702. {
  703. struct inode *inode = &ci->vfs_inode;
  704. struct ceph_cap *cap;
  705. struct rb_node *p;
  706. int ret = 0;
  707. spin_lock(&inode->i_lock);
  708. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  709. cap = rb_entry(p, struct ceph_cap, ci_node);
  710. if (__cap_is_valid(cap) &&
  711. (cap->implemented & ~cap->issued & mask)) {
  712. ret = 1;
  713. break;
  714. }
  715. }
  716. spin_unlock(&inode->i_lock);
  717. dout("ceph_caps_revoking %p %s = %d\n", inode,
  718. ceph_cap_string(mask), ret);
  719. return ret;
  720. }
  721. int __ceph_caps_used(struct ceph_inode_info *ci)
  722. {
  723. int used = 0;
  724. if (ci->i_pin_ref)
  725. used |= CEPH_CAP_PIN;
  726. if (ci->i_rd_ref)
  727. used |= CEPH_CAP_FILE_RD;
  728. if (ci->i_rdcache_ref || ci->i_rdcache_gen)
  729. used |= CEPH_CAP_FILE_CACHE;
  730. if (ci->i_wr_ref)
  731. used |= CEPH_CAP_FILE_WR;
  732. if (ci->i_wrbuffer_ref)
  733. used |= CEPH_CAP_FILE_BUFFER;
  734. return used;
  735. }
  736. /*
  737. * wanted, by virtue of open file modes
  738. */
  739. int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
  740. {
  741. int want = 0;
  742. int mode;
  743. for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
  744. if (ci->i_nr_by_mode[mode])
  745. want |= ceph_caps_for_mode(mode);
  746. return want;
  747. }
  748. /*
  749. * Return caps we have registered with the MDS(s) as 'wanted'.
  750. */
  751. int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
  752. {
  753. struct ceph_cap *cap;
  754. struct rb_node *p;
  755. int mds_wanted = 0;
  756. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  757. cap = rb_entry(p, struct ceph_cap, ci_node);
  758. if (!__cap_is_valid(cap))
  759. continue;
  760. mds_wanted |= cap->mds_wanted;
  761. }
  762. return mds_wanted;
  763. }
  764. /*
  765. * called under i_lock
  766. */
  767. static int __ceph_is_any_caps(struct ceph_inode_info *ci)
  768. {
  769. return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
  770. }
  771. /*
  772. * Remove a cap. Take steps to deal with a racing iterate_session_caps.
  773. *
  774. * caller should hold i_lock.
  775. * caller will not hold session s_mutex if called from destroy_inode.
  776. */
  777. void __ceph_remove_cap(struct ceph_cap *cap)
  778. {
  779. struct ceph_mds_session *session = cap->session;
  780. struct ceph_inode_info *ci = cap->ci;
  781. struct ceph_mds_client *mdsc =
  782. &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  783. int removed = 0;
  784. dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
  785. /* remove from session list */
  786. spin_lock(&session->s_cap_lock);
  787. if (session->s_cap_iterator == cap) {
  788. /* not yet, we are iterating over this very cap */
  789. dout("__ceph_remove_cap delaying %p removal from session %p\n",
  790. cap, cap->session);
  791. } else {
  792. list_del_init(&cap->session_caps);
  793. session->s_nr_caps--;
  794. cap->session = NULL;
  795. removed = 1;
  796. }
  797. /* protect backpointer with s_cap_lock: see iterate_session_caps */
  798. cap->ci = NULL;
  799. spin_unlock(&session->s_cap_lock);
  800. /* remove from inode list */
  801. rb_erase(&cap->ci_node, &ci->i_caps);
  802. if (ci->i_auth_cap == cap)
  803. ci->i_auth_cap = NULL;
  804. if (removed)
  805. ceph_put_cap(mdsc, cap);
  806. if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
  807. struct ceph_snap_realm *realm = ci->i_snap_realm;
  808. spin_lock(&realm->inodes_with_caps_lock);
  809. list_del_init(&ci->i_snap_realm_item);
  810. ci->i_snap_realm_counter++;
  811. ci->i_snap_realm = NULL;
  812. spin_unlock(&realm->inodes_with_caps_lock);
  813. ceph_put_snap_realm(mdsc, realm);
  814. }
  815. if (!__ceph_is_any_real_caps(ci))
  816. __cap_delay_cancel(mdsc, ci);
  817. }
  818. /*
  819. * Build and send a cap message to the given MDS.
  820. *
  821. * Caller should be holding s_mutex.
  822. */
  823. static int send_cap_msg(struct ceph_mds_session *session,
  824. u64 ino, u64 cid, int op,
  825. int caps, int wanted, int dirty,
  826. u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
  827. u64 size, u64 max_size,
  828. struct timespec *mtime, struct timespec *atime,
  829. u64 time_warp_seq,
  830. uid_t uid, gid_t gid, mode_t mode,
  831. u64 xattr_version,
  832. struct ceph_buffer *xattrs_buf,
  833. u64 follows)
  834. {
  835. struct ceph_mds_caps *fc;
  836. struct ceph_msg *msg;
  837. dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
  838. " seq %u/%u mseq %u follows %lld size %llu/%llu"
  839. " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
  840. cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
  841. ceph_cap_string(dirty),
  842. seq, issue_seq, mseq, follows, size, max_size,
  843. xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
  844. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS);
  845. if (!msg)
  846. return -ENOMEM;
  847. msg->hdr.tid = cpu_to_le64(flush_tid);
  848. fc = msg->front.iov_base;
  849. memset(fc, 0, sizeof(*fc));
  850. fc->cap_id = cpu_to_le64(cid);
  851. fc->op = cpu_to_le32(op);
  852. fc->seq = cpu_to_le32(seq);
  853. fc->issue_seq = cpu_to_le32(issue_seq);
  854. fc->migrate_seq = cpu_to_le32(mseq);
  855. fc->caps = cpu_to_le32(caps);
  856. fc->wanted = cpu_to_le32(wanted);
  857. fc->dirty = cpu_to_le32(dirty);
  858. fc->ino = cpu_to_le64(ino);
  859. fc->snap_follows = cpu_to_le64(follows);
  860. fc->size = cpu_to_le64(size);
  861. fc->max_size = cpu_to_le64(max_size);
  862. if (mtime)
  863. ceph_encode_timespec(&fc->mtime, mtime);
  864. if (atime)
  865. ceph_encode_timespec(&fc->atime, atime);
  866. fc->time_warp_seq = cpu_to_le32(time_warp_seq);
  867. fc->uid = cpu_to_le32(uid);
  868. fc->gid = cpu_to_le32(gid);
  869. fc->mode = cpu_to_le32(mode);
  870. fc->xattr_version = cpu_to_le64(xattr_version);
  871. if (xattrs_buf) {
  872. msg->middle = ceph_buffer_get(xattrs_buf);
  873. fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
  874. msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
  875. }
  876. ceph_con_send(&session->s_con, msg);
  877. return 0;
  878. }
  879. static void __queue_cap_release(struct ceph_mds_session *session,
  880. u64 ino, u64 cap_id, u32 migrate_seq,
  881. u32 issue_seq)
  882. {
  883. struct ceph_msg *msg;
  884. struct ceph_mds_cap_release *head;
  885. struct ceph_mds_cap_item *item;
  886. spin_lock(&session->s_cap_lock);
  887. BUG_ON(!session->s_num_cap_releases);
  888. msg = list_first_entry(&session->s_cap_releases,
  889. struct ceph_msg, list_head);
  890. dout(" adding %llx release to mds%d msg %p (%d left)\n",
  891. ino, session->s_mds, msg, session->s_num_cap_releases);
  892. BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
  893. head = msg->front.iov_base;
  894. head->num = cpu_to_le32(le32_to_cpu(head->num) + 1);
  895. item = msg->front.iov_base + msg->front.iov_len;
  896. item->ino = cpu_to_le64(ino);
  897. item->cap_id = cpu_to_le64(cap_id);
  898. item->migrate_seq = cpu_to_le32(migrate_seq);
  899. item->seq = cpu_to_le32(issue_seq);
  900. session->s_num_cap_releases--;
  901. msg->front.iov_len += sizeof(*item);
  902. if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
  903. dout(" release msg %p full\n", msg);
  904. list_move_tail(&msg->list_head, &session->s_cap_releases_done);
  905. } else {
  906. dout(" release msg %p at %d/%d (%d)\n", msg,
  907. (int)le32_to_cpu(head->num),
  908. (int)CEPH_CAPS_PER_RELEASE,
  909. (int)msg->front.iov_len);
  910. }
  911. spin_unlock(&session->s_cap_lock);
  912. }
  913. /*
  914. * Queue cap releases when an inode is dropped from our cache. Since
  915. * inode is about to be destroyed, there is no need for i_lock.
  916. */
  917. void ceph_queue_caps_release(struct inode *inode)
  918. {
  919. struct ceph_inode_info *ci = ceph_inode(inode);
  920. struct rb_node *p;
  921. p = rb_first(&ci->i_caps);
  922. while (p) {
  923. struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
  924. struct ceph_mds_session *session = cap->session;
  925. __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
  926. cap->mseq, cap->issue_seq);
  927. p = rb_next(p);
  928. __ceph_remove_cap(cap);
  929. }
  930. }
  931. /*
  932. * Send a cap msg on the given inode. Update our caps state, then
  933. * drop i_lock and send the message.
  934. *
  935. * Make note of max_size reported/requested from mds, revoked caps
  936. * that have now been implemented.
  937. *
  938. * Make half-hearted attempt ot to invalidate page cache if we are
  939. * dropping RDCACHE. Note that this will leave behind locked pages
  940. * that we'll then need to deal with elsewhere.
  941. *
  942. * Return non-zero if delayed release, or we experienced an error
  943. * such that the caller should requeue + retry later.
  944. *
  945. * called with i_lock, then drops it.
  946. * caller should hold snap_rwsem (read), s_mutex.
  947. */
  948. static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
  949. int op, int used, int want, int retain, int flushing,
  950. unsigned *pflush_tid)
  951. __releases(cap->ci->vfs_inode->i_lock)
  952. {
  953. struct ceph_inode_info *ci = cap->ci;
  954. struct inode *inode = &ci->vfs_inode;
  955. u64 cap_id = cap->cap_id;
  956. int held, revoking, dropping, keep;
  957. u64 seq, issue_seq, mseq, time_warp_seq, follows;
  958. u64 size, max_size;
  959. struct timespec mtime, atime;
  960. int wake = 0;
  961. mode_t mode;
  962. uid_t uid;
  963. gid_t gid;
  964. struct ceph_mds_session *session;
  965. u64 xattr_version = 0;
  966. struct ceph_buffer *xattr_blob = NULL;
  967. int delayed = 0;
  968. u64 flush_tid = 0;
  969. int i;
  970. int ret;
  971. held = cap->issued | cap->implemented;
  972. revoking = cap->implemented & ~cap->issued;
  973. retain &= ~revoking;
  974. dropping = cap->issued & ~retain;
  975. dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
  976. inode, cap, cap->session,
  977. ceph_cap_string(held), ceph_cap_string(held & retain),
  978. ceph_cap_string(revoking));
  979. BUG_ON((retain & CEPH_CAP_PIN) == 0);
  980. session = cap->session;
  981. /* don't release wanted unless we've waited a bit. */
  982. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  983. time_before(jiffies, ci->i_hold_caps_min)) {
  984. dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
  985. ceph_cap_string(cap->issued),
  986. ceph_cap_string(cap->issued & retain),
  987. ceph_cap_string(cap->mds_wanted),
  988. ceph_cap_string(want));
  989. want |= cap->mds_wanted;
  990. retain |= cap->issued;
  991. delayed = 1;
  992. }
  993. ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
  994. cap->issued &= retain; /* drop bits we don't want */
  995. if (cap->implemented & ~cap->issued) {
  996. /*
  997. * Wake up any waiters on wanted -> needed transition.
  998. * This is due to the weird transition from buffered
  999. * to sync IO... we need to flush dirty pages _before_
  1000. * allowing sync writes to avoid reordering.
  1001. */
  1002. wake = 1;
  1003. }
  1004. cap->implemented &= cap->issued | used;
  1005. cap->mds_wanted = want;
  1006. if (flushing) {
  1007. /*
  1008. * assign a tid for flush operations so we can avoid
  1009. * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
  1010. * clean type races. track latest tid for every bit
  1011. * so we can handle flush AxFw, flush Fw, and have the
  1012. * first ack clean Ax.
  1013. */
  1014. flush_tid = ++ci->i_cap_flush_last_tid;
  1015. if (pflush_tid)
  1016. *pflush_tid = flush_tid;
  1017. dout(" cap_flush_tid %d\n", (int)flush_tid);
  1018. for (i = 0; i < CEPH_CAP_BITS; i++)
  1019. if (flushing & (1 << i))
  1020. ci->i_cap_flush_tid[i] = flush_tid;
  1021. follows = ci->i_head_snapc->seq;
  1022. } else {
  1023. follows = 0;
  1024. }
  1025. keep = cap->implemented;
  1026. seq = cap->seq;
  1027. issue_seq = cap->issue_seq;
  1028. mseq = cap->mseq;
  1029. size = inode->i_size;
  1030. ci->i_reported_size = size;
  1031. max_size = ci->i_wanted_max_size;
  1032. ci->i_requested_max_size = max_size;
  1033. mtime = inode->i_mtime;
  1034. atime = inode->i_atime;
  1035. time_warp_seq = ci->i_time_warp_seq;
  1036. uid = inode->i_uid;
  1037. gid = inode->i_gid;
  1038. mode = inode->i_mode;
  1039. if (flushing & CEPH_CAP_XATTR_EXCL) {
  1040. __ceph_build_xattrs_blob(ci);
  1041. xattr_blob = ci->i_xattrs.blob;
  1042. xattr_version = ci->i_xattrs.version;
  1043. }
  1044. spin_unlock(&inode->i_lock);
  1045. ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
  1046. op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
  1047. size, max_size, &mtime, &atime, time_warp_seq,
  1048. uid, gid, mode, xattr_version, xattr_blob,
  1049. follows);
  1050. if (ret < 0) {
  1051. dout("error sending cap msg, must requeue %p\n", inode);
  1052. delayed = 1;
  1053. }
  1054. if (wake)
  1055. wake_up_all(&ci->i_cap_wq);
  1056. return delayed;
  1057. }
  1058. /*
  1059. * When a snapshot is taken, clients accumulate dirty metadata on
  1060. * inodes with capabilities in ceph_cap_snaps to describe the file
  1061. * state at the time the snapshot was taken. This must be flushed
  1062. * asynchronously back to the MDS once sync writes complete and dirty
  1063. * data is written out.
  1064. *
  1065. * Called under i_lock. Takes s_mutex as needed.
  1066. */
  1067. void __ceph_flush_snaps(struct ceph_inode_info *ci,
  1068. struct ceph_mds_session **psession)
  1069. __releases(ci->vfs_inode->i_lock)
  1070. __acquires(ci->vfs_inode->i_lock)
  1071. {
  1072. struct inode *inode = &ci->vfs_inode;
  1073. int mds;
  1074. struct ceph_cap_snap *capsnap;
  1075. u32 mseq;
  1076. struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
  1077. struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
  1078. session->s_mutex */
  1079. u64 next_follows = 0; /* keep track of how far we've gotten through the
  1080. i_cap_snaps list, and skip these entries next time
  1081. around to avoid an infinite loop */
  1082. if (psession)
  1083. session = *psession;
  1084. dout("__flush_snaps %p\n", inode);
  1085. retry:
  1086. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1087. /* avoid an infiniute loop after retry */
  1088. if (capsnap->follows < next_follows)
  1089. continue;
  1090. /*
  1091. * we need to wait for sync writes to complete and for dirty
  1092. * pages to be written out.
  1093. */
  1094. if (capsnap->dirty_pages || capsnap->writing)
  1095. continue;
  1096. /*
  1097. * if cap writeback already occurred, we should have dropped
  1098. * the capsnap in ceph_put_wrbuffer_cap_refs.
  1099. */
  1100. BUG_ON(capsnap->dirty == 0);
  1101. /* pick mds, take s_mutex */
  1102. if (ci->i_auth_cap == NULL) {
  1103. dout("no auth cap (migrating?), doing nothing\n");
  1104. goto out;
  1105. }
  1106. mds = ci->i_auth_cap->session->s_mds;
  1107. mseq = ci->i_auth_cap->mseq;
  1108. if (session && session->s_mds != mds) {
  1109. dout("oops, wrong session %p mutex\n", session);
  1110. mutex_unlock(&session->s_mutex);
  1111. ceph_put_mds_session(session);
  1112. session = NULL;
  1113. }
  1114. if (!session) {
  1115. spin_unlock(&inode->i_lock);
  1116. mutex_lock(&mdsc->mutex);
  1117. session = __ceph_lookup_mds_session(mdsc, mds);
  1118. mutex_unlock(&mdsc->mutex);
  1119. if (session) {
  1120. dout("inverting session/ino locks on %p\n",
  1121. session);
  1122. mutex_lock(&session->s_mutex);
  1123. }
  1124. /*
  1125. * if session == NULL, we raced against a cap
  1126. * deletion or migration. retry, and we'll
  1127. * get a better @mds value next time.
  1128. */
  1129. spin_lock(&inode->i_lock);
  1130. goto retry;
  1131. }
  1132. capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
  1133. atomic_inc(&capsnap->nref);
  1134. if (!list_empty(&capsnap->flushing_item))
  1135. list_del_init(&capsnap->flushing_item);
  1136. list_add_tail(&capsnap->flushing_item,
  1137. &session->s_cap_snaps_flushing);
  1138. spin_unlock(&inode->i_lock);
  1139. dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
  1140. inode, capsnap, next_follows, capsnap->size);
  1141. send_cap_msg(session, ceph_vino(inode).ino, 0,
  1142. CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
  1143. capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
  1144. capsnap->size, 0,
  1145. &capsnap->mtime, &capsnap->atime,
  1146. capsnap->time_warp_seq,
  1147. capsnap->uid, capsnap->gid, capsnap->mode,
  1148. capsnap->xattr_version, capsnap->xattr_blob,
  1149. capsnap->follows);
  1150. next_follows = capsnap->follows + 1;
  1151. ceph_put_cap_snap(capsnap);
  1152. spin_lock(&inode->i_lock);
  1153. goto retry;
  1154. }
  1155. /* we flushed them all; remove this inode from the queue */
  1156. spin_lock(&mdsc->snap_flush_lock);
  1157. list_del_init(&ci->i_snap_flush_item);
  1158. spin_unlock(&mdsc->snap_flush_lock);
  1159. out:
  1160. if (psession)
  1161. *psession = session;
  1162. else if (session) {
  1163. mutex_unlock(&session->s_mutex);
  1164. ceph_put_mds_session(session);
  1165. }
  1166. }
  1167. static void ceph_flush_snaps(struct ceph_inode_info *ci)
  1168. {
  1169. struct inode *inode = &ci->vfs_inode;
  1170. spin_lock(&inode->i_lock);
  1171. __ceph_flush_snaps(ci, NULL);
  1172. spin_unlock(&inode->i_lock);
  1173. }
  1174. /*
  1175. * Mark caps dirty. If inode is newly dirty, add to the global dirty
  1176. * list.
  1177. */
  1178. void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
  1179. {
  1180. struct ceph_mds_client *mdsc =
  1181. &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  1182. struct inode *inode = &ci->vfs_inode;
  1183. int was = ci->i_dirty_caps;
  1184. int dirty = 0;
  1185. dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
  1186. ceph_cap_string(mask), ceph_cap_string(was),
  1187. ceph_cap_string(was | mask));
  1188. ci->i_dirty_caps |= mask;
  1189. if (was == 0) {
  1190. if (!ci->i_head_snapc)
  1191. ci->i_head_snapc = ceph_get_snap_context(
  1192. ci->i_snap_realm->cached_context);
  1193. dout(" inode %p now dirty snapc %p\n", &ci->vfs_inode,
  1194. ci->i_head_snapc);
  1195. BUG_ON(!list_empty(&ci->i_dirty_item));
  1196. spin_lock(&mdsc->cap_dirty_lock);
  1197. list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
  1198. spin_unlock(&mdsc->cap_dirty_lock);
  1199. if (ci->i_flushing_caps == 0) {
  1200. igrab(inode);
  1201. dirty |= I_DIRTY_SYNC;
  1202. }
  1203. }
  1204. BUG_ON(list_empty(&ci->i_dirty_item));
  1205. if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
  1206. (mask & CEPH_CAP_FILE_BUFFER))
  1207. dirty |= I_DIRTY_DATASYNC;
  1208. if (dirty)
  1209. __mark_inode_dirty(inode, dirty);
  1210. __cap_delay_requeue(mdsc, ci);
  1211. }
  1212. /*
  1213. * Add dirty inode to the flushing list. Assigned a seq number so we
  1214. * can wait for caps to flush without starving.
  1215. *
  1216. * Called under i_lock.
  1217. */
  1218. static int __mark_caps_flushing(struct inode *inode,
  1219. struct ceph_mds_session *session)
  1220. {
  1221. struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
  1222. struct ceph_inode_info *ci = ceph_inode(inode);
  1223. int flushing;
  1224. BUG_ON(ci->i_dirty_caps == 0);
  1225. BUG_ON(list_empty(&ci->i_dirty_item));
  1226. flushing = ci->i_dirty_caps;
  1227. dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
  1228. ceph_cap_string(flushing),
  1229. ceph_cap_string(ci->i_flushing_caps),
  1230. ceph_cap_string(ci->i_flushing_caps | flushing));
  1231. ci->i_flushing_caps |= flushing;
  1232. ci->i_dirty_caps = 0;
  1233. dout(" inode %p now !dirty\n", inode);
  1234. spin_lock(&mdsc->cap_dirty_lock);
  1235. list_del_init(&ci->i_dirty_item);
  1236. ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
  1237. if (list_empty(&ci->i_flushing_item)) {
  1238. list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1239. mdsc->num_cap_flushing++;
  1240. dout(" inode %p now flushing seq %lld\n", inode,
  1241. ci->i_cap_flush_seq);
  1242. } else {
  1243. list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1244. dout(" inode %p now flushing (more) seq %lld\n", inode,
  1245. ci->i_cap_flush_seq);
  1246. }
  1247. spin_unlock(&mdsc->cap_dirty_lock);
  1248. return flushing;
  1249. }
  1250. /*
  1251. * try to invalidate mapping pages without blocking.
  1252. */
  1253. static int mapping_is_empty(struct address_space *mapping)
  1254. {
  1255. struct page *page = find_get_page(mapping, 0);
  1256. if (!page)
  1257. return 1;
  1258. put_page(page);
  1259. return 0;
  1260. }
  1261. static int try_nonblocking_invalidate(struct inode *inode)
  1262. {
  1263. struct ceph_inode_info *ci = ceph_inode(inode);
  1264. u32 invalidating_gen = ci->i_rdcache_gen;
  1265. spin_unlock(&inode->i_lock);
  1266. invalidate_mapping_pages(&inode->i_data, 0, -1);
  1267. spin_lock(&inode->i_lock);
  1268. if (mapping_is_empty(&inode->i_data) &&
  1269. invalidating_gen == ci->i_rdcache_gen) {
  1270. /* success. */
  1271. dout("try_nonblocking_invalidate %p success\n", inode);
  1272. ci->i_rdcache_gen = 0;
  1273. ci->i_rdcache_revoking = 0;
  1274. return 0;
  1275. }
  1276. dout("try_nonblocking_invalidate %p failed\n", inode);
  1277. return -1;
  1278. }
  1279. /*
  1280. * Swiss army knife function to examine currently used and wanted
  1281. * versus held caps. Release, flush, ack revoked caps to mds as
  1282. * appropriate.
  1283. *
  1284. * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
  1285. * cap release further.
  1286. * CHECK_CAPS_AUTHONLY - we should only check the auth cap
  1287. * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
  1288. * further delay.
  1289. */
  1290. void ceph_check_caps(struct ceph_inode_info *ci, int flags,
  1291. struct ceph_mds_session *session)
  1292. {
  1293. struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode);
  1294. struct ceph_mds_client *mdsc = &client->mdsc;
  1295. struct inode *inode = &ci->vfs_inode;
  1296. struct ceph_cap *cap;
  1297. int file_wanted, used;
  1298. int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
  1299. int issued, implemented, want, retain, revoking, flushing = 0;
  1300. int mds = -1; /* keep track of how far we've gone through i_caps list
  1301. to avoid an infinite loop on retry */
  1302. struct rb_node *p;
  1303. int tried_invalidate = 0;
  1304. int delayed = 0, sent = 0, force_requeue = 0, num;
  1305. int queue_invalidate = 0;
  1306. int is_delayed = flags & CHECK_CAPS_NODELAY;
  1307. /* if we are unmounting, flush any unused caps immediately. */
  1308. if (mdsc->stopping)
  1309. is_delayed = 1;
  1310. spin_lock(&inode->i_lock);
  1311. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  1312. flags |= CHECK_CAPS_FLUSH;
  1313. /* flush snaps first time around only */
  1314. if (!list_empty(&ci->i_cap_snaps))
  1315. __ceph_flush_snaps(ci, &session);
  1316. goto retry_locked;
  1317. retry:
  1318. spin_lock(&inode->i_lock);
  1319. retry_locked:
  1320. file_wanted = __ceph_caps_file_wanted(ci);
  1321. used = __ceph_caps_used(ci);
  1322. want = file_wanted | used;
  1323. issued = __ceph_caps_issued(ci, &implemented);
  1324. revoking = implemented & ~issued;
  1325. retain = want | CEPH_CAP_PIN;
  1326. if (!mdsc->stopping && inode->i_nlink > 0) {
  1327. if (want) {
  1328. retain |= CEPH_CAP_ANY; /* be greedy */
  1329. } else {
  1330. retain |= CEPH_CAP_ANY_SHARED;
  1331. /*
  1332. * keep RD only if we didn't have the file open RW,
  1333. * because then the mds would revoke it anyway to
  1334. * journal max_size=0.
  1335. */
  1336. if (ci->i_max_size == 0)
  1337. retain |= CEPH_CAP_ANY_RD;
  1338. }
  1339. }
  1340. dout("check_caps %p file_want %s used %s dirty %s flushing %s"
  1341. " issued %s revoking %s retain %s %s%s%s\n", inode,
  1342. ceph_cap_string(file_wanted),
  1343. ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
  1344. ceph_cap_string(ci->i_flushing_caps),
  1345. ceph_cap_string(issued), ceph_cap_string(revoking),
  1346. ceph_cap_string(retain),
  1347. (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
  1348. (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
  1349. (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
  1350. /*
  1351. * If we no longer need to hold onto old our caps, and we may
  1352. * have cached pages, but don't want them, then try to invalidate.
  1353. * If we fail, it's because pages are locked.... try again later.
  1354. */
  1355. if ((!is_delayed || mdsc->stopping) &&
  1356. ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
  1357. ci->i_rdcache_gen && /* may have cached pages */
  1358. (file_wanted == 0 || /* no open files */
  1359. (revoking & (CEPH_CAP_FILE_CACHE|
  1360. CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
  1361. !tried_invalidate) {
  1362. dout("check_caps trying to invalidate on %p\n", inode);
  1363. if (try_nonblocking_invalidate(inode) < 0) {
  1364. if (revoking & (CEPH_CAP_FILE_CACHE|
  1365. CEPH_CAP_FILE_LAZYIO)) {
  1366. dout("check_caps queuing invalidate\n");
  1367. queue_invalidate = 1;
  1368. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  1369. } else {
  1370. dout("check_caps failed to invalidate pages\n");
  1371. /* we failed to invalidate pages. check these
  1372. caps again later. */
  1373. force_requeue = 1;
  1374. __cap_set_timeouts(mdsc, ci);
  1375. }
  1376. }
  1377. tried_invalidate = 1;
  1378. goto retry_locked;
  1379. }
  1380. num = 0;
  1381. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  1382. cap = rb_entry(p, struct ceph_cap, ci_node);
  1383. num++;
  1384. /* avoid looping forever */
  1385. if (mds >= cap->mds ||
  1386. ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
  1387. continue;
  1388. /* NOTE: no side-effects allowed, until we take s_mutex */
  1389. revoking = cap->implemented & ~cap->issued;
  1390. if (revoking)
  1391. dout(" mds%d revoking %s\n", cap->mds,
  1392. ceph_cap_string(revoking));
  1393. if (cap == ci->i_auth_cap &&
  1394. (cap->issued & CEPH_CAP_FILE_WR)) {
  1395. /* request larger max_size from MDS? */
  1396. if (ci->i_wanted_max_size > ci->i_max_size &&
  1397. ci->i_wanted_max_size > ci->i_requested_max_size) {
  1398. dout("requesting new max_size\n");
  1399. goto ack;
  1400. }
  1401. /* approaching file_max? */
  1402. if ((inode->i_size << 1) >= ci->i_max_size &&
  1403. (ci->i_reported_size << 1) < ci->i_max_size) {
  1404. dout("i_size approaching max_size\n");
  1405. goto ack;
  1406. }
  1407. }
  1408. /* flush anything dirty? */
  1409. if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
  1410. ci->i_dirty_caps) {
  1411. dout("flushing dirty caps\n");
  1412. goto ack;
  1413. }
  1414. /* completed revocation? going down and there are no caps? */
  1415. if (revoking && (revoking & used) == 0) {
  1416. dout("completed revocation of %s\n",
  1417. ceph_cap_string(cap->implemented & ~cap->issued));
  1418. goto ack;
  1419. }
  1420. /* want more caps from mds? */
  1421. if (want & ~(cap->mds_wanted | cap->issued))
  1422. goto ack;
  1423. /* things we might delay */
  1424. if ((cap->issued & ~retain) == 0 &&
  1425. cap->mds_wanted == want)
  1426. continue; /* nope, all good */
  1427. if (is_delayed)
  1428. goto ack;
  1429. /* delay? */
  1430. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  1431. time_before(jiffies, ci->i_hold_caps_max)) {
  1432. dout(" delaying issued %s -> %s, wanted %s -> %s\n",
  1433. ceph_cap_string(cap->issued),
  1434. ceph_cap_string(cap->issued & retain),
  1435. ceph_cap_string(cap->mds_wanted),
  1436. ceph_cap_string(want));
  1437. delayed++;
  1438. continue;
  1439. }
  1440. ack:
  1441. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1442. dout(" skipping %p I_NOFLUSH set\n", inode);
  1443. continue;
  1444. }
  1445. if (session && session != cap->session) {
  1446. dout("oops, wrong session %p mutex\n", session);
  1447. mutex_unlock(&session->s_mutex);
  1448. session = NULL;
  1449. }
  1450. if (!session) {
  1451. session = cap->session;
  1452. if (mutex_trylock(&session->s_mutex) == 0) {
  1453. dout("inverting session/ino locks on %p\n",
  1454. session);
  1455. spin_unlock(&inode->i_lock);
  1456. if (took_snap_rwsem) {
  1457. up_read(&mdsc->snap_rwsem);
  1458. took_snap_rwsem = 0;
  1459. }
  1460. mutex_lock(&session->s_mutex);
  1461. goto retry;
  1462. }
  1463. }
  1464. /* take snap_rwsem after session mutex */
  1465. if (!took_snap_rwsem) {
  1466. if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
  1467. dout("inverting snap/in locks on %p\n",
  1468. inode);
  1469. spin_unlock(&inode->i_lock);
  1470. down_read(&mdsc->snap_rwsem);
  1471. took_snap_rwsem = 1;
  1472. goto retry;
  1473. }
  1474. took_snap_rwsem = 1;
  1475. }
  1476. if (cap == ci->i_auth_cap && ci->i_dirty_caps)
  1477. flushing = __mark_caps_flushing(inode, session);
  1478. mds = cap->mds; /* remember mds, so we don't repeat */
  1479. sent++;
  1480. /* __send_cap drops i_lock */
  1481. delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
  1482. retain, flushing, NULL);
  1483. goto retry; /* retake i_lock and restart our cap scan. */
  1484. }
  1485. /*
  1486. * Reschedule delayed caps release if we delayed anything,
  1487. * otherwise cancel.
  1488. */
  1489. if (delayed && is_delayed)
  1490. force_requeue = 1; /* __send_cap delayed release; requeue */
  1491. if (!delayed && !is_delayed)
  1492. __cap_delay_cancel(mdsc, ci);
  1493. else if (!is_delayed || force_requeue)
  1494. __cap_delay_requeue(mdsc, ci);
  1495. spin_unlock(&inode->i_lock);
  1496. if (queue_invalidate)
  1497. ceph_queue_invalidate(inode);
  1498. if (session)
  1499. mutex_unlock(&session->s_mutex);
  1500. if (took_snap_rwsem)
  1501. up_read(&mdsc->snap_rwsem);
  1502. }
  1503. /*
  1504. * Try to flush dirty caps back to the auth mds.
  1505. */
  1506. static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
  1507. unsigned *flush_tid)
  1508. {
  1509. struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
  1510. struct ceph_inode_info *ci = ceph_inode(inode);
  1511. int unlock_session = session ? 0 : 1;
  1512. int flushing = 0;
  1513. retry:
  1514. spin_lock(&inode->i_lock);
  1515. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1516. dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
  1517. goto out;
  1518. }
  1519. if (ci->i_dirty_caps && ci->i_auth_cap) {
  1520. struct ceph_cap *cap = ci->i_auth_cap;
  1521. int used = __ceph_caps_used(ci);
  1522. int want = __ceph_caps_wanted(ci);
  1523. int delayed;
  1524. if (!session) {
  1525. spin_unlock(&inode->i_lock);
  1526. session = cap->session;
  1527. mutex_lock(&session->s_mutex);
  1528. goto retry;
  1529. }
  1530. BUG_ON(session != cap->session);
  1531. if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
  1532. goto out;
  1533. flushing = __mark_caps_flushing(inode, session);
  1534. /* __send_cap drops i_lock */
  1535. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
  1536. cap->issued | cap->implemented, flushing,
  1537. flush_tid);
  1538. if (!delayed)
  1539. goto out_unlocked;
  1540. spin_lock(&inode->i_lock);
  1541. __cap_delay_requeue(mdsc, ci);
  1542. }
  1543. out:
  1544. spin_unlock(&inode->i_lock);
  1545. out_unlocked:
  1546. if (session && unlock_session)
  1547. mutex_unlock(&session->s_mutex);
  1548. return flushing;
  1549. }
  1550. /*
  1551. * Return true if we've flushed caps through the given flush_tid.
  1552. */
  1553. static int caps_are_flushed(struct inode *inode, unsigned tid)
  1554. {
  1555. struct ceph_inode_info *ci = ceph_inode(inode);
  1556. int i, ret = 1;
  1557. spin_lock(&inode->i_lock);
  1558. for (i = 0; i < CEPH_CAP_BITS; i++)
  1559. if ((ci->i_flushing_caps & (1 << i)) &&
  1560. ci->i_cap_flush_tid[i] <= tid) {
  1561. /* still flushing this bit */
  1562. ret = 0;
  1563. break;
  1564. }
  1565. spin_unlock(&inode->i_lock);
  1566. return ret;
  1567. }
  1568. /*
  1569. * Wait on any unsafe replies for the given inode. First wait on the
  1570. * newest request, and make that the upper bound. Then, if there are
  1571. * more requests, keep waiting on the oldest as long as it is still older
  1572. * than the original request.
  1573. */
  1574. static void sync_write_wait(struct inode *inode)
  1575. {
  1576. struct ceph_inode_info *ci = ceph_inode(inode);
  1577. struct list_head *head = &ci->i_unsafe_writes;
  1578. struct ceph_osd_request *req;
  1579. u64 last_tid;
  1580. spin_lock(&ci->i_unsafe_lock);
  1581. if (list_empty(head))
  1582. goto out;
  1583. /* set upper bound as _last_ entry in chain */
  1584. req = list_entry(head->prev, struct ceph_osd_request,
  1585. r_unsafe_item);
  1586. last_tid = req->r_tid;
  1587. do {
  1588. ceph_osdc_get_request(req);
  1589. spin_unlock(&ci->i_unsafe_lock);
  1590. dout("sync_write_wait on tid %llu (until %llu)\n",
  1591. req->r_tid, last_tid);
  1592. wait_for_completion(&req->r_safe_completion);
  1593. spin_lock(&ci->i_unsafe_lock);
  1594. ceph_osdc_put_request(req);
  1595. /*
  1596. * from here on look at first entry in chain, since we
  1597. * only want to wait for anything older than last_tid
  1598. */
  1599. if (list_empty(head))
  1600. break;
  1601. req = list_entry(head->next, struct ceph_osd_request,
  1602. r_unsafe_item);
  1603. } while (req->r_tid < last_tid);
  1604. out:
  1605. spin_unlock(&ci->i_unsafe_lock);
  1606. }
  1607. int ceph_fsync(struct file *file, int datasync)
  1608. {
  1609. struct inode *inode = file->f_mapping->host;
  1610. struct ceph_inode_info *ci = ceph_inode(inode);
  1611. unsigned flush_tid;
  1612. int ret;
  1613. int dirty;
  1614. dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
  1615. sync_write_wait(inode);
  1616. ret = filemap_write_and_wait(inode->i_mapping);
  1617. if (ret < 0)
  1618. return ret;
  1619. dirty = try_flush_caps(inode, NULL, &flush_tid);
  1620. dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
  1621. /*
  1622. * only wait on non-file metadata writeback (the mds
  1623. * can recover size and mtime, so we don't need to
  1624. * wait for that)
  1625. */
  1626. if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
  1627. dout("fsync waiting for flush_tid %u\n", flush_tid);
  1628. ret = wait_event_interruptible(ci->i_cap_wq,
  1629. caps_are_flushed(inode, flush_tid));
  1630. }
  1631. dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
  1632. return ret;
  1633. }
  1634. /*
  1635. * Flush any dirty caps back to the mds. If we aren't asked to wait,
  1636. * queue inode for flush but don't do so immediately, because we can
  1637. * get by with fewer MDS messages if we wait for data writeback to
  1638. * complete first.
  1639. */
  1640. int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
  1641. {
  1642. struct ceph_inode_info *ci = ceph_inode(inode);
  1643. unsigned flush_tid;
  1644. int err = 0;
  1645. int dirty;
  1646. int wait = wbc->sync_mode == WB_SYNC_ALL;
  1647. dout("write_inode %p wait=%d\n", inode, wait);
  1648. if (wait) {
  1649. dirty = try_flush_caps(inode, NULL, &flush_tid);
  1650. if (dirty)
  1651. err = wait_event_interruptible(ci->i_cap_wq,
  1652. caps_are_flushed(inode, flush_tid));
  1653. } else {
  1654. struct ceph_mds_client *mdsc =
  1655. &ceph_sb_to_client(inode->i_sb)->mdsc;
  1656. spin_lock(&inode->i_lock);
  1657. if (__ceph_caps_dirty(ci))
  1658. __cap_delay_requeue_front(mdsc, ci);
  1659. spin_unlock(&inode->i_lock);
  1660. }
  1661. return err;
  1662. }
  1663. /*
  1664. * After a recovering MDS goes active, we need to resend any caps
  1665. * we were flushing.
  1666. *
  1667. * Caller holds session->s_mutex.
  1668. */
  1669. static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
  1670. struct ceph_mds_session *session)
  1671. {
  1672. struct ceph_cap_snap *capsnap;
  1673. dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
  1674. list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
  1675. flushing_item) {
  1676. struct ceph_inode_info *ci = capsnap->ci;
  1677. struct inode *inode = &ci->vfs_inode;
  1678. struct ceph_cap *cap;
  1679. spin_lock(&inode->i_lock);
  1680. cap = ci->i_auth_cap;
  1681. if (cap && cap->session == session) {
  1682. dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
  1683. cap, capsnap);
  1684. __ceph_flush_snaps(ci, &session);
  1685. } else {
  1686. pr_err("%p auth cap %p not mds%d ???\n", inode,
  1687. cap, session->s_mds);
  1688. }
  1689. spin_unlock(&inode->i_lock);
  1690. }
  1691. }
  1692. void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
  1693. struct ceph_mds_session *session)
  1694. {
  1695. struct ceph_inode_info *ci;
  1696. kick_flushing_capsnaps(mdsc, session);
  1697. dout("kick_flushing_caps mds%d\n", session->s_mds);
  1698. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  1699. struct inode *inode = &ci->vfs_inode;
  1700. struct ceph_cap *cap;
  1701. int delayed = 0;
  1702. spin_lock(&inode->i_lock);
  1703. cap = ci->i_auth_cap;
  1704. if (cap && cap->session == session) {
  1705. dout("kick_flushing_caps %p cap %p %s\n", inode,
  1706. cap, ceph_cap_string(ci->i_flushing_caps));
  1707. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
  1708. __ceph_caps_used(ci),
  1709. __ceph_caps_wanted(ci),
  1710. cap->issued | cap->implemented,
  1711. ci->i_flushing_caps, NULL);
  1712. if (delayed) {
  1713. spin_lock(&inode->i_lock);
  1714. __cap_delay_requeue(mdsc, ci);
  1715. spin_unlock(&inode->i_lock);
  1716. }
  1717. } else {
  1718. pr_err("%p auth cap %p not mds%d ???\n", inode,
  1719. cap, session->s_mds);
  1720. spin_unlock(&inode->i_lock);
  1721. }
  1722. }
  1723. }
  1724. /*
  1725. * Take references to capabilities we hold, so that we don't release
  1726. * them to the MDS prematurely.
  1727. *
  1728. * Protected by i_lock.
  1729. */
  1730. static void __take_cap_refs(struct ceph_inode_info *ci, int got)
  1731. {
  1732. if (got & CEPH_CAP_PIN)
  1733. ci->i_pin_ref++;
  1734. if (got & CEPH_CAP_FILE_RD)
  1735. ci->i_rd_ref++;
  1736. if (got & CEPH_CAP_FILE_CACHE)
  1737. ci->i_rdcache_ref++;
  1738. if (got & CEPH_CAP_FILE_WR)
  1739. ci->i_wr_ref++;
  1740. if (got & CEPH_CAP_FILE_BUFFER) {
  1741. if (ci->i_wrbuffer_ref == 0)
  1742. igrab(&ci->vfs_inode);
  1743. ci->i_wrbuffer_ref++;
  1744. dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
  1745. &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref);
  1746. }
  1747. }
  1748. /*
  1749. * Try to grab cap references. Specify those refs we @want, and the
  1750. * minimal set we @need. Also include the larger offset we are writing
  1751. * to (when applicable), and check against max_size here as well.
  1752. * Note that caller is responsible for ensuring max_size increases are
  1753. * requested from the MDS.
  1754. */
  1755. static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
  1756. int *got, loff_t endoff, int *check_max, int *err)
  1757. {
  1758. struct inode *inode = &ci->vfs_inode;
  1759. int ret = 0;
  1760. int have, implemented;
  1761. int file_wanted;
  1762. dout("get_cap_refs %p need %s want %s\n", inode,
  1763. ceph_cap_string(need), ceph_cap_string(want));
  1764. spin_lock(&inode->i_lock);
  1765. /* make sure file is actually open */
  1766. file_wanted = __ceph_caps_file_wanted(ci);
  1767. if ((file_wanted & need) == 0) {
  1768. dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
  1769. ceph_cap_string(need), ceph_cap_string(file_wanted));
  1770. *err = -EBADF;
  1771. ret = 1;
  1772. goto out;
  1773. }
  1774. if (need & CEPH_CAP_FILE_WR) {
  1775. if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
  1776. dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
  1777. inode, endoff, ci->i_max_size);
  1778. if (endoff > ci->i_wanted_max_size) {
  1779. *check_max = 1;
  1780. ret = 1;
  1781. }
  1782. goto out;
  1783. }
  1784. /*
  1785. * If a sync write is in progress, we must wait, so that we
  1786. * can get a final snapshot value for size+mtime.
  1787. */
  1788. if (__ceph_have_pending_cap_snap(ci)) {
  1789. dout("get_cap_refs %p cap_snap_pending\n", inode);
  1790. goto out;
  1791. }
  1792. }
  1793. have = __ceph_caps_issued(ci, &implemented);
  1794. /*
  1795. * disallow writes while a truncate is pending
  1796. */
  1797. if (ci->i_truncate_pending)
  1798. have &= ~CEPH_CAP_FILE_WR;
  1799. if ((have & need) == need) {
  1800. /*
  1801. * Look at (implemented & ~have & not) so that we keep waiting
  1802. * on transition from wanted -> needed caps. This is needed
  1803. * for WRBUFFER|WR -> WR to avoid a new WR sync write from
  1804. * going before a prior buffered writeback happens.
  1805. */
  1806. int not = want & ~(have & need);
  1807. int revoking = implemented & ~have;
  1808. dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
  1809. inode, ceph_cap_string(have), ceph_cap_string(not),
  1810. ceph_cap_string(revoking));
  1811. if ((revoking & not) == 0) {
  1812. *got = need | (have & want);
  1813. __take_cap_refs(ci, *got);
  1814. ret = 1;
  1815. }
  1816. } else {
  1817. dout("get_cap_refs %p have %s needed %s\n", inode,
  1818. ceph_cap_string(have), ceph_cap_string(need));
  1819. }
  1820. out:
  1821. spin_unlock(&inode->i_lock);
  1822. dout("get_cap_refs %p ret %d got %s\n", inode,
  1823. ret, ceph_cap_string(*got));
  1824. return ret;
  1825. }
  1826. /*
  1827. * Check the offset we are writing up to against our current
  1828. * max_size. If necessary, tell the MDS we want to write to
  1829. * a larger offset.
  1830. */
  1831. static void check_max_size(struct inode *inode, loff_t endoff)
  1832. {
  1833. struct ceph_inode_info *ci = ceph_inode(inode);
  1834. int check = 0;
  1835. /* do we need to explicitly request a larger max_size? */
  1836. spin_lock(&inode->i_lock);
  1837. if ((endoff >= ci->i_max_size ||
  1838. endoff > (inode->i_size << 1)) &&
  1839. endoff > ci->i_wanted_max_size) {
  1840. dout("write %p at large endoff %llu, req max_size\n",
  1841. inode, endoff);
  1842. ci->i_wanted_max_size = endoff;
  1843. check = 1;
  1844. }
  1845. spin_unlock(&inode->i_lock);
  1846. if (check)
  1847. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1848. }
  1849. /*
  1850. * Wait for caps, and take cap references. If we can't get a WR cap
  1851. * due to a small max_size, make sure we check_max_size (and possibly
  1852. * ask the mds) so we don't get hung up indefinitely.
  1853. */
  1854. int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
  1855. loff_t endoff)
  1856. {
  1857. int check_max, ret, err;
  1858. retry:
  1859. if (endoff > 0)
  1860. check_max_size(&ci->vfs_inode, endoff);
  1861. check_max = 0;
  1862. err = 0;
  1863. ret = wait_event_interruptible(ci->i_cap_wq,
  1864. try_get_cap_refs(ci, need, want,
  1865. got, endoff,
  1866. &check_max, &err));
  1867. if (err)
  1868. ret = err;
  1869. if (check_max)
  1870. goto retry;
  1871. return ret;
  1872. }
  1873. /*
  1874. * Take cap refs. Caller must already know we hold at least one ref
  1875. * on the caps in question or we don't know this is safe.
  1876. */
  1877. void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
  1878. {
  1879. spin_lock(&ci->vfs_inode.i_lock);
  1880. __take_cap_refs(ci, caps);
  1881. spin_unlock(&ci->vfs_inode.i_lock);
  1882. }
  1883. /*
  1884. * Release cap refs.
  1885. *
  1886. * If we released the last ref on any given cap, call ceph_check_caps
  1887. * to release (or schedule a release).
  1888. *
  1889. * If we are releasing a WR cap (from a sync write), finalize any affected
  1890. * cap_snap, and wake up any waiters.
  1891. */
  1892. void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
  1893. {
  1894. struct inode *inode = &ci->vfs_inode;
  1895. int last = 0, put = 0, flushsnaps = 0, wake = 0;
  1896. struct ceph_cap_snap *capsnap;
  1897. spin_lock(&inode->i_lock);
  1898. if (had & CEPH_CAP_PIN)
  1899. --ci->i_pin_ref;
  1900. if (had & CEPH_CAP_FILE_RD)
  1901. if (--ci->i_rd_ref == 0)
  1902. last++;
  1903. if (had & CEPH_CAP_FILE_CACHE)
  1904. if (--ci->i_rdcache_ref == 0)
  1905. last++;
  1906. if (had & CEPH_CAP_FILE_BUFFER) {
  1907. if (--ci->i_wrbuffer_ref == 0) {
  1908. last++;
  1909. put++;
  1910. }
  1911. dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
  1912. inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref);
  1913. }
  1914. if (had & CEPH_CAP_FILE_WR)
  1915. if (--ci->i_wr_ref == 0) {
  1916. last++;
  1917. if (!list_empty(&ci->i_cap_snaps)) {
  1918. capsnap = list_first_entry(&ci->i_cap_snaps,
  1919. struct ceph_cap_snap,
  1920. ci_item);
  1921. if (capsnap->writing) {
  1922. capsnap->writing = 0;
  1923. flushsnaps =
  1924. __ceph_finish_cap_snap(ci,
  1925. capsnap);
  1926. wake = 1;
  1927. }
  1928. }
  1929. }
  1930. spin_unlock(&inode->i_lock);
  1931. dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
  1932. last ? " last" : "", put ? " put" : "");
  1933. if (last && !flushsnaps)
  1934. ceph_check_caps(ci, 0, NULL);
  1935. else if (flushsnaps)
  1936. ceph_flush_snaps(ci);
  1937. if (wake)
  1938. wake_up_all(&ci->i_cap_wq);
  1939. if (put)
  1940. iput(inode);
  1941. }
  1942. /*
  1943. * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
  1944. * context. Adjust per-snap dirty page accounting as appropriate.
  1945. * Once all dirty data for a cap_snap is flushed, flush snapped file
  1946. * metadata back to the MDS. If we dropped the last ref, call
  1947. * ceph_check_caps.
  1948. */
  1949. void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
  1950. struct ceph_snap_context *snapc)
  1951. {
  1952. struct inode *inode = &ci->vfs_inode;
  1953. int last = 0;
  1954. int complete_capsnap = 0;
  1955. int drop_capsnap = 0;
  1956. int found = 0;
  1957. struct ceph_cap_snap *capsnap = NULL;
  1958. spin_lock(&inode->i_lock);
  1959. ci->i_wrbuffer_ref -= nr;
  1960. last = !ci->i_wrbuffer_ref;
  1961. if (ci->i_head_snapc == snapc) {
  1962. ci->i_wrbuffer_ref_head -= nr;
  1963. if (ci->i_wrbuffer_ref_head == 0 &&
  1964. ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) {
  1965. BUG_ON(!ci->i_head_snapc);
  1966. ceph_put_snap_context(ci->i_head_snapc);
  1967. ci->i_head_snapc = NULL;
  1968. }
  1969. dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
  1970. inode,
  1971. ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
  1972. ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  1973. last ? " LAST" : "");
  1974. } else {
  1975. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1976. if (capsnap->context == snapc) {
  1977. found = 1;
  1978. break;
  1979. }
  1980. }
  1981. BUG_ON(!found);
  1982. capsnap->dirty_pages -= nr;
  1983. if (capsnap->dirty_pages == 0) {
  1984. complete_capsnap = 1;
  1985. if (capsnap->dirty == 0)
  1986. /* cap writeback completed before we created
  1987. * the cap_snap; no FLUSHSNAP is needed */
  1988. drop_capsnap = 1;
  1989. }
  1990. dout("put_wrbuffer_cap_refs on %p cap_snap %p "
  1991. " snap %lld %d/%d -> %d/%d %s%s%s\n",
  1992. inode, capsnap, capsnap->context->seq,
  1993. ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
  1994. ci->i_wrbuffer_ref, capsnap->dirty_pages,
  1995. last ? " (wrbuffer last)" : "",
  1996. complete_capsnap ? " (complete capsnap)" : "",
  1997. drop_capsnap ? " (drop capsnap)" : "");
  1998. if (drop_capsnap) {
  1999. ceph_put_snap_context(capsnap->context);
  2000. list_del(&capsnap->ci_item);
  2001. list_del(&capsnap->flushing_item);
  2002. ceph_put_cap_snap(capsnap);
  2003. }
  2004. }
  2005. spin_unlock(&inode->i_lock);
  2006. if (last) {
  2007. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  2008. iput(inode);
  2009. } else if (complete_capsnap) {
  2010. ceph_flush_snaps(ci);
  2011. wake_up_all(&ci->i_cap_wq);
  2012. }
  2013. if (drop_capsnap)
  2014. iput(inode);
  2015. }
  2016. /*
  2017. * Handle a cap GRANT message from the MDS. (Note that a GRANT may
  2018. * actually be a revocation if it specifies a smaller cap set.)
  2019. *
  2020. * caller holds s_mutex and i_lock, we drop both.
  2021. *
  2022. * return value:
  2023. * 0 - ok
  2024. * 1 - check_caps on auth cap only (writeback)
  2025. * 2 - check_caps (ack revoke)
  2026. */
  2027. static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
  2028. struct ceph_mds_session *session,
  2029. struct ceph_cap *cap,
  2030. struct ceph_buffer *xattr_buf)
  2031. __releases(inode->i_lock)
  2032. {
  2033. struct ceph_inode_info *ci = ceph_inode(inode);
  2034. int mds = session->s_mds;
  2035. int seq = le32_to_cpu(grant->seq);
  2036. int newcaps = le32_to_cpu(grant->caps);
  2037. int issued, implemented, used, wanted, dirty;
  2038. u64 size = le64_to_cpu(grant->size);
  2039. u64 max_size = le64_to_cpu(grant->max_size);
  2040. struct timespec mtime, atime, ctime;
  2041. int check_caps = 0;
  2042. int wake = 0;
  2043. int writeback = 0;
  2044. int revoked_rdcache = 0;
  2045. int queue_invalidate = 0;
  2046. dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
  2047. inode, cap, mds, seq, ceph_cap_string(newcaps));
  2048. dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
  2049. inode->i_size);
  2050. /*
  2051. * If CACHE is being revoked, and we have no dirty buffers,
  2052. * try to invalidate (once). (If there are dirty buffers, we
  2053. * will invalidate _after_ writeback.)
  2054. */
  2055. if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
  2056. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  2057. !ci->i_wrbuffer_ref) {
  2058. if (try_nonblocking_invalidate(inode) == 0) {
  2059. revoked_rdcache = 1;
  2060. } else {
  2061. /* there were locked pages.. invalidate later
  2062. in a separate thread. */
  2063. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  2064. queue_invalidate = 1;
  2065. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  2066. }
  2067. }
  2068. }
  2069. /* side effects now are allowed */
  2070. issued = __ceph_caps_issued(ci, &implemented);
  2071. issued |= implemented | __ceph_caps_dirty(ci);
  2072. cap->cap_gen = session->s_cap_gen;
  2073. __check_cap_issue(ci, cap, newcaps);
  2074. if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
  2075. inode->i_mode = le32_to_cpu(grant->mode);
  2076. inode->i_uid = le32_to_cpu(grant->uid);
  2077. inode->i_gid = le32_to_cpu(grant->gid);
  2078. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  2079. inode->i_uid, inode->i_gid);
  2080. }
  2081. if ((issued & CEPH_CAP_LINK_EXCL) == 0)
  2082. inode->i_nlink = le32_to_cpu(grant->nlink);
  2083. if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
  2084. int len = le32_to_cpu(grant->xattr_len);
  2085. u64 version = le64_to_cpu(grant->xattr_version);
  2086. if (version > ci->i_xattrs.version) {
  2087. dout(" got new xattrs v%llu on %p len %d\n",
  2088. version, inode, len);
  2089. if (ci->i_xattrs.blob)
  2090. ceph_buffer_put(ci->i_xattrs.blob);
  2091. ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
  2092. ci->i_xattrs.version = version;
  2093. }
  2094. }
  2095. /* size/ctime/mtime/atime? */
  2096. ceph_fill_file_size(inode, issued,
  2097. le32_to_cpu(grant->truncate_seq),
  2098. le64_to_cpu(grant->truncate_size), size);
  2099. ceph_decode_timespec(&mtime, &grant->mtime);
  2100. ceph_decode_timespec(&atime, &grant->atime);
  2101. ceph_decode_timespec(&ctime, &grant->ctime);
  2102. ceph_fill_file_time(inode, issued,
  2103. le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
  2104. &atime);
  2105. /* max size increase? */
  2106. if (max_size != ci->i_max_size) {
  2107. dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
  2108. ci->i_max_size = max_size;
  2109. if (max_size >= ci->i_wanted_max_size) {
  2110. ci->i_wanted_max_size = 0; /* reset */
  2111. ci->i_requested_max_size = 0;
  2112. }
  2113. wake = 1;
  2114. }
  2115. /* check cap bits */
  2116. wanted = __ceph_caps_wanted(ci);
  2117. used = __ceph_caps_used(ci);
  2118. dirty = __ceph_caps_dirty(ci);
  2119. dout(" my wanted = %s, used = %s, dirty %s\n",
  2120. ceph_cap_string(wanted),
  2121. ceph_cap_string(used),
  2122. ceph_cap_string(dirty));
  2123. if (wanted != le32_to_cpu(grant->wanted)) {
  2124. dout("mds wanted %s -> %s\n",
  2125. ceph_cap_string(le32_to_cpu(grant->wanted)),
  2126. ceph_cap_string(wanted));
  2127. grant->wanted = cpu_to_le32(wanted);
  2128. }
  2129. cap->seq = seq;
  2130. /* file layout may have changed */
  2131. ci->i_layout = grant->layout;
  2132. /* revocation, grant, or no-op? */
  2133. if (cap->issued & ~newcaps) {
  2134. int revoking = cap->issued & ~newcaps;
  2135. dout("revocation: %s -> %s (revoking %s)\n",
  2136. ceph_cap_string(cap->issued),
  2137. ceph_cap_string(newcaps),
  2138. ceph_cap_string(revoking));
  2139. if (revoking & used & CEPH_CAP_FILE_BUFFER)
  2140. writeback = 1; /* initiate writeback; will delay ack */
  2141. else if (revoking == CEPH_CAP_FILE_CACHE &&
  2142. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  2143. queue_invalidate)
  2144. ; /* do nothing yet, invalidation will be queued */
  2145. else if (cap == ci->i_auth_cap)
  2146. check_caps = 1; /* check auth cap only */
  2147. else
  2148. check_caps = 2; /* check all caps */
  2149. cap->issued = newcaps;
  2150. cap->implemented |= newcaps;
  2151. } else if (cap->issued == newcaps) {
  2152. dout("caps unchanged: %s -> %s\n",
  2153. ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
  2154. } else {
  2155. dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
  2156. ceph_cap_string(newcaps));
  2157. cap->issued = newcaps;
  2158. cap->implemented |= newcaps; /* add bits only, to
  2159. * avoid stepping on a
  2160. * pending revocation */
  2161. wake = 1;
  2162. }
  2163. BUG_ON(cap->issued & ~cap->implemented);
  2164. spin_unlock(&inode->i_lock);
  2165. if (writeback)
  2166. /*
  2167. * queue inode for writeback: we can't actually call
  2168. * filemap_write_and_wait, etc. from message handler
  2169. * context.
  2170. */
  2171. ceph_queue_writeback(inode);
  2172. if (queue_invalidate)
  2173. ceph_queue_invalidate(inode);
  2174. if (wake)
  2175. wake_up_all(&ci->i_cap_wq);
  2176. if (check_caps == 1)
  2177. ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
  2178. session);
  2179. else if (check_caps == 2)
  2180. ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
  2181. else
  2182. mutex_unlock(&session->s_mutex);
  2183. }
  2184. /*
  2185. * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
  2186. * MDS has been safely committed.
  2187. */
  2188. static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
  2189. struct ceph_mds_caps *m,
  2190. struct ceph_mds_session *session,
  2191. struct ceph_cap *cap)
  2192. __releases(inode->i_lock)
  2193. {
  2194. struct ceph_inode_info *ci = ceph_inode(inode);
  2195. struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
  2196. unsigned seq = le32_to_cpu(m->seq);
  2197. int dirty = le32_to_cpu(m->dirty);
  2198. int cleaned = 0;
  2199. int drop = 0;
  2200. int i;
  2201. for (i = 0; i < CEPH_CAP_BITS; i++)
  2202. if ((dirty & (1 << i)) &&
  2203. flush_tid == ci->i_cap_flush_tid[i])
  2204. cleaned |= 1 << i;
  2205. dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
  2206. " flushing %s -> %s\n",
  2207. inode, session->s_mds, seq, ceph_cap_string(dirty),
  2208. ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
  2209. ceph_cap_string(ci->i_flushing_caps & ~cleaned));
  2210. if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
  2211. goto out;
  2212. ci->i_flushing_caps &= ~cleaned;
  2213. spin_lock(&mdsc->cap_dirty_lock);
  2214. if (ci->i_flushing_caps == 0) {
  2215. list_del_init(&ci->i_flushing_item);
  2216. if (!list_empty(&session->s_cap_flushing))
  2217. dout(" mds%d still flushing cap on %p\n",
  2218. session->s_mds,
  2219. &list_entry(session->s_cap_flushing.next,
  2220. struct ceph_inode_info,
  2221. i_flushing_item)->vfs_inode);
  2222. mdsc->num_cap_flushing--;
  2223. wake_up_all(&mdsc->cap_flushing_wq);
  2224. dout(" inode %p now !flushing\n", inode);
  2225. if (ci->i_dirty_caps == 0) {
  2226. dout(" inode %p now clean\n", inode);
  2227. BUG_ON(!list_empty(&ci->i_dirty_item));
  2228. drop = 1;
  2229. if (ci->i_wrbuffer_ref_head == 0) {
  2230. BUG_ON(!ci->i_head_snapc);
  2231. ceph_put_snap_context(ci->i_head_snapc);
  2232. ci->i_head_snapc = NULL;
  2233. }
  2234. } else {
  2235. BUG_ON(list_empty(&ci->i_dirty_item));
  2236. }
  2237. }
  2238. spin_unlock(&mdsc->cap_dirty_lock);
  2239. wake_up_all(&ci->i_cap_wq);
  2240. out:
  2241. spin_unlock(&inode->i_lock);
  2242. if (drop)
  2243. iput(inode);
  2244. }
  2245. /*
  2246. * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
  2247. * throw away our cap_snap.
  2248. *
  2249. * Caller hold s_mutex.
  2250. */
  2251. static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
  2252. struct ceph_mds_caps *m,
  2253. struct ceph_mds_session *session)
  2254. {
  2255. struct ceph_inode_info *ci = ceph_inode(inode);
  2256. u64 follows = le64_to_cpu(m->snap_follows);
  2257. struct ceph_cap_snap *capsnap;
  2258. int drop = 0;
  2259. dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
  2260. inode, ci, session->s_mds, follows);
  2261. spin_lock(&inode->i_lock);
  2262. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  2263. if (capsnap->follows == follows) {
  2264. if (capsnap->flush_tid != flush_tid) {
  2265. dout(" cap_snap %p follows %lld tid %lld !="
  2266. " %lld\n", capsnap, follows,
  2267. flush_tid, capsnap->flush_tid);
  2268. break;
  2269. }
  2270. WARN_ON(capsnap->dirty_pages || capsnap->writing);
  2271. dout(" removing %p cap_snap %p follows %lld\n",
  2272. inode, capsnap, follows);
  2273. ceph_put_snap_context(capsnap->context);
  2274. list_del(&capsnap->ci_item);
  2275. list_del(&capsnap->flushing_item);
  2276. ceph_put_cap_snap(capsnap);
  2277. drop = 1;
  2278. break;
  2279. } else {
  2280. dout(" skipping cap_snap %p follows %lld\n",
  2281. capsnap, capsnap->follows);
  2282. }
  2283. }
  2284. spin_unlock(&inode->i_lock);
  2285. if (drop)
  2286. iput(inode);
  2287. }
  2288. /*
  2289. * Handle TRUNC from MDS, indicating file truncation.
  2290. *
  2291. * caller hold s_mutex.
  2292. */
  2293. static void handle_cap_trunc(struct inode *inode,
  2294. struct ceph_mds_caps *trunc,
  2295. struct ceph_mds_session *session)
  2296. __releases(inode->i_lock)
  2297. {
  2298. struct ceph_inode_info *ci = ceph_inode(inode);
  2299. int mds = session->s_mds;
  2300. int seq = le32_to_cpu(trunc->seq);
  2301. u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
  2302. u64 truncate_size = le64_to_cpu(trunc->truncate_size);
  2303. u64 size = le64_to_cpu(trunc->size);
  2304. int implemented = 0;
  2305. int dirty = __ceph_caps_dirty(ci);
  2306. int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
  2307. int queue_trunc = 0;
  2308. issued |= implemented | dirty;
  2309. dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
  2310. inode, mds, seq, truncate_size, truncate_seq);
  2311. queue_trunc = ceph_fill_file_size(inode, issued,
  2312. truncate_seq, truncate_size, size);
  2313. spin_unlock(&inode->i_lock);
  2314. if (queue_trunc)
  2315. ceph_queue_vmtruncate(inode);
  2316. }
  2317. /*
  2318. * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
  2319. * different one. If we are the most recent migration we've seen (as
  2320. * indicated by mseq), make note of the migrating cap bits for the
  2321. * duration (until we see the corresponding IMPORT).
  2322. *
  2323. * caller holds s_mutex
  2324. */
  2325. static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
  2326. struct ceph_mds_session *session,
  2327. int *open_target_sessions)
  2328. {
  2329. struct ceph_inode_info *ci = ceph_inode(inode);
  2330. int mds = session->s_mds;
  2331. unsigned mseq = le32_to_cpu(ex->migrate_seq);
  2332. struct ceph_cap *cap = NULL, *t;
  2333. struct rb_node *p;
  2334. int remember = 1;
  2335. dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
  2336. inode, ci, mds, mseq);
  2337. spin_lock(&inode->i_lock);
  2338. /* make sure we haven't seen a higher mseq */
  2339. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  2340. t = rb_entry(p, struct ceph_cap, ci_node);
  2341. if (ceph_seq_cmp(t->mseq, mseq) > 0) {
  2342. dout(" higher mseq on cap from mds%d\n",
  2343. t->session->s_mds);
  2344. remember = 0;
  2345. }
  2346. if (t->session->s_mds == mds)
  2347. cap = t;
  2348. }
  2349. if (cap) {
  2350. if (remember) {
  2351. /* make note */
  2352. ci->i_cap_exporting_mds = mds;
  2353. ci->i_cap_exporting_mseq = mseq;
  2354. ci->i_cap_exporting_issued = cap->issued;
  2355. /*
  2356. * make sure we have open sessions with all possible
  2357. * export targets, so that we get the matching IMPORT
  2358. */
  2359. *open_target_sessions = 1;
  2360. }
  2361. __ceph_remove_cap(cap);
  2362. }
  2363. /* else, we already released it */
  2364. spin_unlock(&inode->i_lock);
  2365. }
  2366. /*
  2367. * Handle cap IMPORT. If there are temp bits from an older EXPORT,
  2368. * clean them up.
  2369. *
  2370. * caller holds s_mutex.
  2371. */
  2372. static void handle_cap_import(struct ceph_mds_client *mdsc,
  2373. struct inode *inode, struct ceph_mds_caps *im,
  2374. struct ceph_mds_session *session,
  2375. void *snaptrace, int snaptrace_len)
  2376. {
  2377. struct ceph_inode_info *ci = ceph_inode(inode);
  2378. int mds = session->s_mds;
  2379. unsigned issued = le32_to_cpu(im->caps);
  2380. unsigned wanted = le32_to_cpu(im->wanted);
  2381. unsigned seq = le32_to_cpu(im->seq);
  2382. unsigned mseq = le32_to_cpu(im->migrate_seq);
  2383. u64 realmino = le64_to_cpu(im->realm);
  2384. u64 cap_id = le64_to_cpu(im->cap_id);
  2385. if (ci->i_cap_exporting_mds >= 0 &&
  2386. ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
  2387. dout("handle_cap_import inode %p ci %p mds%d mseq %d"
  2388. " - cleared exporting from mds%d\n",
  2389. inode, ci, mds, mseq,
  2390. ci->i_cap_exporting_mds);
  2391. ci->i_cap_exporting_issued = 0;
  2392. ci->i_cap_exporting_mseq = 0;
  2393. ci->i_cap_exporting_mds = -1;
  2394. } else {
  2395. dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
  2396. inode, ci, mds, mseq);
  2397. }
  2398. down_write(&mdsc->snap_rwsem);
  2399. ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
  2400. false);
  2401. downgrade_write(&mdsc->snap_rwsem);
  2402. ceph_add_cap(inode, session, cap_id, -1,
  2403. issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
  2404. NULL /* no caps context */);
  2405. try_flush_caps(inode, session, NULL);
  2406. up_read(&mdsc->snap_rwsem);
  2407. }
  2408. /*
  2409. * Handle a caps message from the MDS.
  2410. *
  2411. * Identify the appropriate session, inode, and call the right handler
  2412. * based on the cap op.
  2413. */
  2414. void ceph_handle_caps(struct ceph_mds_session *session,
  2415. struct ceph_msg *msg)
  2416. {
  2417. struct ceph_mds_client *mdsc = session->s_mdsc;
  2418. struct super_block *sb = mdsc->client->sb;
  2419. struct inode *inode;
  2420. struct ceph_cap *cap;
  2421. struct ceph_mds_caps *h;
  2422. int mds = session->s_mds;
  2423. int op;
  2424. u32 seq, mseq;
  2425. struct ceph_vino vino;
  2426. u64 cap_id;
  2427. u64 size, max_size;
  2428. u64 tid;
  2429. void *snaptrace;
  2430. size_t snaptrace_len;
  2431. void *flock;
  2432. u32 flock_len;
  2433. int open_target_sessions = 0;
  2434. dout("handle_caps from mds%d\n", mds);
  2435. /* decode */
  2436. tid = le64_to_cpu(msg->hdr.tid);
  2437. if (msg->front.iov_len < sizeof(*h))
  2438. goto bad;
  2439. h = msg->front.iov_base;
  2440. op = le32_to_cpu(h->op);
  2441. vino.ino = le64_to_cpu(h->ino);
  2442. vino.snap = CEPH_NOSNAP;
  2443. cap_id = le64_to_cpu(h->cap_id);
  2444. seq = le32_to_cpu(h->seq);
  2445. mseq = le32_to_cpu(h->migrate_seq);
  2446. size = le64_to_cpu(h->size);
  2447. max_size = le64_to_cpu(h->max_size);
  2448. snaptrace = h + 1;
  2449. snaptrace_len = le32_to_cpu(h->snap_trace_len);
  2450. if (le16_to_cpu(msg->hdr.version) >= 2) {
  2451. void *p, *end;
  2452. p = snaptrace + snaptrace_len;
  2453. end = msg->front.iov_base + msg->front.iov_len;
  2454. ceph_decode_32_safe(&p, end, flock_len, bad);
  2455. flock = p;
  2456. } else {
  2457. flock = NULL;
  2458. flock_len = 0;
  2459. }
  2460. mutex_lock(&session->s_mutex);
  2461. session->s_seq++;
  2462. dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
  2463. (unsigned)seq);
  2464. /* lookup ino */
  2465. inode = ceph_find_inode(sb, vino);
  2466. dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
  2467. vino.snap, inode);
  2468. if (!inode) {
  2469. dout(" i don't have ino %llx\n", vino.ino);
  2470. if (op == CEPH_CAP_OP_IMPORT)
  2471. __queue_cap_release(session, vino.ino, cap_id,
  2472. mseq, seq);
  2473. /*
  2474. * send any full release message to try to move things
  2475. * along for the mds (who clearly thinks we still have this
  2476. * cap).
  2477. */
  2478. ceph_add_cap_releases(mdsc, session);
  2479. ceph_send_cap_releases(mdsc, session);
  2480. goto done;
  2481. }
  2482. /* these will work even if we don't have a cap yet */
  2483. switch (op) {
  2484. case CEPH_CAP_OP_FLUSHSNAP_ACK:
  2485. handle_cap_flushsnap_ack(inode, tid, h, session);
  2486. goto done;
  2487. case CEPH_CAP_OP_EXPORT:
  2488. handle_cap_export(inode, h, session, &open_target_sessions);
  2489. goto done;
  2490. case CEPH_CAP_OP_IMPORT:
  2491. handle_cap_import(mdsc, inode, h, session,
  2492. snaptrace, snaptrace_len);
  2493. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY,
  2494. session);
  2495. goto done_unlocked;
  2496. }
  2497. /* the rest require a cap */
  2498. spin_lock(&inode->i_lock);
  2499. cap = __get_cap_for_mds(ceph_inode(inode), mds);
  2500. if (!cap) {
  2501. dout(" no cap on %p ino %llx.%llx from mds%d\n",
  2502. inode, ceph_ino(inode), ceph_snap(inode), mds);
  2503. spin_unlock(&inode->i_lock);
  2504. goto done;
  2505. }
  2506. /* note that each of these drops i_lock for us */
  2507. switch (op) {
  2508. case CEPH_CAP_OP_REVOKE:
  2509. case CEPH_CAP_OP_GRANT:
  2510. handle_cap_grant(inode, h, session, cap, msg->middle);
  2511. goto done_unlocked;
  2512. case CEPH_CAP_OP_FLUSH_ACK:
  2513. handle_cap_flush_ack(inode, tid, h, session, cap);
  2514. break;
  2515. case CEPH_CAP_OP_TRUNC:
  2516. handle_cap_trunc(inode, h, session);
  2517. break;
  2518. default:
  2519. spin_unlock(&inode->i_lock);
  2520. pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
  2521. ceph_cap_op_name(op));
  2522. }
  2523. done:
  2524. mutex_unlock(&session->s_mutex);
  2525. done_unlocked:
  2526. if (inode)
  2527. iput(inode);
  2528. if (open_target_sessions)
  2529. ceph_mdsc_open_export_target_sessions(mdsc, session);
  2530. return;
  2531. bad:
  2532. pr_err("ceph_handle_caps: corrupt message\n");
  2533. ceph_msg_dump(msg);
  2534. return;
  2535. }
  2536. /*
  2537. * Delayed work handler to process end of delayed cap release LRU list.
  2538. */
  2539. void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
  2540. {
  2541. struct ceph_inode_info *ci;
  2542. int flags = CHECK_CAPS_NODELAY;
  2543. dout("check_delayed_caps\n");
  2544. while (1) {
  2545. spin_lock(&mdsc->cap_delay_lock);
  2546. if (list_empty(&mdsc->cap_delay_list))
  2547. break;
  2548. ci = list_first_entry(&mdsc->cap_delay_list,
  2549. struct ceph_inode_info,
  2550. i_cap_delay_list);
  2551. if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
  2552. time_before(jiffies, ci->i_hold_caps_max))
  2553. break;
  2554. list_del_init(&ci->i_cap_delay_list);
  2555. spin_unlock(&mdsc->cap_delay_lock);
  2556. dout("check_delayed_caps on %p\n", &ci->vfs_inode);
  2557. ceph_check_caps(ci, flags, NULL);
  2558. }
  2559. spin_unlock(&mdsc->cap_delay_lock);
  2560. }
  2561. /*
  2562. * Flush all dirty caps to the mds
  2563. */
  2564. void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
  2565. {
  2566. struct ceph_inode_info *ci, *nci = NULL;
  2567. struct inode *inode, *ninode = NULL;
  2568. struct list_head *p, *n;
  2569. dout("flush_dirty_caps\n");
  2570. spin_lock(&mdsc->cap_dirty_lock);
  2571. list_for_each_safe(p, n, &mdsc->cap_dirty) {
  2572. if (nci) {
  2573. ci = nci;
  2574. inode = ninode;
  2575. ci->i_ceph_flags &= ~CEPH_I_NOFLUSH;
  2576. dout("flush_dirty_caps inode %p (was next inode)\n",
  2577. inode);
  2578. } else {
  2579. ci = list_entry(p, struct ceph_inode_info,
  2580. i_dirty_item);
  2581. inode = igrab(&ci->vfs_inode);
  2582. BUG_ON(!inode);
  2583. dout("flush_dirty_caps inode %p\n", inode);
  2584. }
  2585. if (n != &mdsc->cap_dirty) {
  2586. nci = list_entry(n, struct ceph_inode_info,
  2587. i_dirty_item);
  2588. ninode = igrab(&nci->vfs_inode);
  2589. BUG_ON(!ninode);
  2590. nci->i_ceph_flags |= CEPH_I_NOFLUSH;
  2591. dout("flush_dirty_caps next inode %p, noflush\n",
  2592. ninode);
  2593. } else {
  2594. nci = NULL;
  2595. ninode = NULL;
  2596. }
  2597. spin_unlock(&mdsc->cap_dirty_lock);
  2598. if (inode) {
  2599. ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH,
  2600. NULL);
  2601. iput(inode);
  2602. }
  2603. spin_lock(&mdsc->cap_dirty_lock);
  2604. }
  2605. spin_unlock(&mdsc->cap_dirty_lock);
  2606. }
  2607. /*
  2608. * Drop open file reference. If we were the last open file,
  2609. * we may need to release capabilities to the MDS (or schedule
  2610. * their delayed release).
  2611. */
  2612. void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
  2613. {
  2614. struct inode *inode = &ci->vfs_inode;
  2615. int last = 0;
  2616. spin_lock(&inode->i_lock);
  2617. dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
  2618. ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
  2619. BUG_ON(ci->i_nr_by_mode[fmode] == 0);
  2620. if (--ci->i_nr_by_mode[fmode] == 0)
  2621. last++;
  2622. spin_unlock(&inode->i_lock);
  2623. if (last && ci->i_vino.snap == CEPH_NOSNAP)
  2624. ceph_check_caps(ci, 0, NULL);
  2625. }
  2626. /*
  2627. * Helpers for embedding cap and dentry lease releases into mds
  2628. * requests.
  2629. *
  2630. * @force is used by dentry_release (below) to force inclusion of a
  2631. * record for the directory inode, even when there aren't any caps to
  2632. * drop.
  2633. */
  2634. int ceph_encode_inode_release(void **p, struct inode *inode,
  2635. int mds, int drop, int unless, int force)
  2636. {
  2637. struct ceph_inode_info *ci = ceph_inode(inode);
  2638. struct ceph_cap *cap;
  2639. struct ceph_mds_request_release *rel = *p;
  2640. int used, dirty;
  2641. int ret = 0;
  2642. spin_lock(&inode->i_lock);
  2643. used = __ceph_caps_used(ci);
  2644. dirty = __ceph_caps_dirty(ci);
  2645. dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
  2646. inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
  2647. ceph_cap_string(unless));
  2648. /* only drop unused, clean caps */
  2649. drop &= ~(used | dirty);
  2650. cap = __get_cap_for_mds(ci, mds);
  2651. if (cap && __cap_is_valid(cap)) {
  2652. if (force ||
  2653. ((cap->issued & drop) &&
  2654. (cap->issued & unless) == 0)) {
  2655. if ((cap->issued & drop) &&
  2656. (cap->issued & unless) == 0) {
  2657. dout("encode_inode_release %p cap %p %s -> "
  2658. "%s\n", inode, cap,
  2659. ceph_cap_string(cap->issued),
  2660. ceph_cap_string(cap->issued & ~drop));
  2661. cap->issued &= ~drop;
  2662. cap->implemented &= ~drop;
  2663. if (ci->i_ceph_flags & CEPH_I_NODELAY) {
  2664. int wanted = __ceph_caps_wanted(ci);
  2665. dout(" wanted %s -> %s (act %s)\n",
  2666. ceph_cap_string(cap->mds_wanted),
  2667. ceph_cap_string(cap->mds_wanted &
  2668. ~wanted),
  2669. ceph_cap_string(wanted));
  2670. cap->mds_wanted &= wanted;
  2671. }
  2672. } else {
  2673. dout("encode_inode_release %p cap %p %s"
  2674. " (force)\n", inode, cap,
  2675. ceph_cap_string(cap->issued));
  2676. }
  2677. rel->ino = cpu_to_le64(ceph_ino(inode));
  2678. rel->cap_id = cpu_to_le64(cap->cap_id);
  2679. rel->seq = cpu_to_le32(cap->seq);
  2680. rel->issue_seq = cpu_to_le32(cap->issue_seq),
  2681. rel->mseq = cpu_to_le32(cap->mseq);
  2682. rel->caps = cpu_to_le32(cap->issued);
  2683. rel->wanted = cpu_to_le32(cap->mds_wanted);
  2684. rel->dname_len = 0;
  2685. rel->dname_seq = 0;
  2686. *p += sizeof(*rel);
  2687. ret = 1;
  2688. } else {
  2689. dout("encode_inode_release %p cap %p %s\n",
  2690. inode, cap, ceph_cap_string(cap->issued));
  2691. }
  2692. }
  2693. spin_unlock(&inode->i_lock);
  2694. return ret;
  2695. }
  2696. int ceph_encode_dentry_release(void **p, struct dentry *dentry,
  2697. int mds, int drop, int unless)
  2698. {
  2699. struct inode *dir = dentry->d_parent->d_inode;
  2700. struct ceph_mds_request_release *rel = *p;
  2701. struct ceph_dentry_info *di = ceph_dentry(dentry);
  2702. int force = 0;
  2703. int ret;
  2704. /*
  2705. * force an record for the directory caps if we have a dentry lease.
  2706. * this is racy (can't take i_lock and d_lock together), but it
  2707. * doesn't have to be perfect; the mds will revoke anything we don't
  2708. * release.
  2709. */
  2710. spin_lock(&dentry->d_lock);
  2711. if (di->lease_session && di->lease_session->s_mds == mds)
  2712. force = 1;
  2713. spin_unlock(&dentry->d_lock);
  2714. ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
  2715. spin_lock(&dentry->d_lock);
  2716. if (ret && di->lease_session && di->lease_session->s_mds == mds) {
  2717. dout("encode_dentry_release %p mds%d seq %d\n",
  2718. dentry, mds, (int)di->lease_seq);
  2719. rel->dname_len = cpu_to_le32(dentry->d_name.len);
  2720. memcpy(*p, dentry->d_name.name, dentry->d_name.len);
  2721. *p += dentry->d_name.len;
  2722. rel->dname_seq = cpu_to_le32(di->lease_seq);
  2723. __ceph_mdsc_drop_dentry_lease(dentry);
  2724. }
  2725. spin_unlock(&dentry->d_lock);
  2726. return ret;
  2727. }