caps.c 81 KB

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