caps.c 81 KB

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