caps.c 78 KB

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