caps.c 77 KB

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