caps.c 80 KB

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