caps.c 79 KB

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