caps.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859
  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. dout(" inode %p now !dirty\n", inode);
  1154. spin_lock(&mdsc->cap_dirty_lock);
  1155. list_del_init(&ci->i_dirty_item);
  1156. ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
  1157. if (list_empty(&ci->i_flushing_item)) {
  1158. list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1159. mdsc->num_cap_flushing++;
  1160. dout(" inode %p now flushing seq %lld\n", inode,
  1161. ci->i_cap_flush_seq);
  1162. } else {
  1163. list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1164. dout(" inode %p now flushing (more) seq %lld\n", inode,
  1165. ci->i_cap_flush_seq);
  1166. }
  1167. spin_unlock(&mdsc->cap_dirty_lock);
  1168. return flushing;
  1169. }
  1170. /*
  1171. * Swiss army knife function to examine currently used and wanted
  1172. * versus held caps. Release, flush, ack revoked caps to mds as
  1173. * appropriate.
  1174. *
  1175. * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
  1176. * cap release further.
  1177. * CHECK_CAPS_AUTHONLY - we should only check the auth cap
  1178. * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
  1179. * further delay.
  1180. */
  1181. void ceph_check_caps(struct ceph_inode_info *ci, int flags,
  1182. struct ceph_mds_session *session)
  1183. {
  1184. struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode);
  1185. struct ceph_mds_client *mdsc = &client->mdsc;
  1186. struct inode *inode = &ci->vfs_inode;
  1187. struct ceph_cap *cap;
  1188. int file_wanted, used;
  1189. int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
  1190. int drop_session_lock = session ? 0 : 1;
  1191. int want, retain, revoking, flushing = 0;
  1192. int mds = -1; /* keep track of how far we've gone through i_caps list
  1193. to avoid an infinite loop on retry */
  1194. struct rb_node *p;
  1195. int tried_invalidate = 0;
  1196. int delayed = 0, sent = 0, force_requeue = 0, num;
  1197. int is_delayed = flags & CHECK_CAPS_NODELAY;
  1198. /* if we are unmounting, flush any unused caps immediately. */
  1199. if (mdsc->stopping)
  1200. is_delayed = 1;
  1201. spin_lock(&inode->i_lock);
  1202. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  1203. flags |= CHECK_CAPS_FLUSH;
  1204. /* flush snaps first time around only */
  1205. if (!list_empty(&ci->i_cap_snaps))
  1206. __ceph_flush_snaps(ci, &session);
  1207. goto retry_locked;
  1208. retry:
  1209. spin_lock(&inode->i_lock);
  1210. retry_locked:
  1211. file_wanted = __ceph_caps_file_wanted(ci);
  1212. used = __ceph_caps_used(ci);
  1213. want = file_wanted | used;
  1214. retain = want | CEPH_CAP_PIN;
  1215. if (!mdsc->stopping && inode->i_nlink > 0) {
  1216. if (want) {
  1217. retain |= CEPH_CAP_ANY; /* be greedy */
  1218. } else {
  1219. retain |= CEPH_CAP_ANY_SHARED;
  1220. /*
  1221. * keep RD only if we didn't have the file open RW,
  1222. * because then the mds would revoke it anyway to
  1223. * journal max_size=0.
  1224. */
  1225. if (ci->i_max_size == 0)
  1226. retain |= CEPH_CAP_ANY_RD;
  1227. }
  1228. }
  1229. dout("check_caps %p file_want %s used %s dirty %s flushing %s"
  1230. " issued %s retain %s %s%s%s\n", inode,
  1231. ceph_cap_string(file_wanted),
  1232. ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
  1233. ceph_cap_string(ci->i_flushing_caps),
  1234. ceph_cap_string(__ceph_caps_issued(ci, NULL)),
  1235. ceph_cap_string(retain),
  1236. (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
  1237. (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
  1238. (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
  1239. /*
  1240. * If we no longer need to hold onto old our caps, and we may
  1241. * have cached pages, but don't want them, then try to invalidate.
  1242. * If we fail, it's because pages are locked.... try again later.
  1243. */
  1244. if ((!is_delayed || mdsc->stopping) &&
  1245. ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
  1246. ci->i_rdcache_gen && /* may have cached pages */
  1247. file_wanted == 0 && /* no open files */
  1248. !ci->i_truncate_pending &&
  1249. !tried_invalidate) {
  1250. u32 invalidating_gen = ci->i_rdcache_gen;
  1251. int ret;
  1252. dout("check_caps trying to invalidate on %p\n", inode);
  1253. spin_unlock(&inode->i_lock);
  1254. ret = invalidate_inode_pages2(&inode->i_data);
  1255. spin_lock(&inode->i_lock);
  1256. if (ret == 0 && invalidating_gen == ci->i_rdcache_gen) {
  1257. /* success. */
  1258. ci->i_rdcache_gen = 0;
  1259. ci->i_rdcache_revoking = 0;
  1260. } else {
  1261. dout("check_caps failed to invalidate pages\n");
  1262. /* we failed to invalidate pages. check these
  1263. caps again later. */
  1264. force_requeue = 1;
  1265. __cap_set_timeouts(mdsc, ci);
  1266. }
  1267. tried_invalidate = 1;
  1268. goto retry_locked;
  1269. }
  1270. num = 0;
  1271. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  1272. cap = rb_entry(p, struct ceph_cap, ci_node);
  1273. num++;
  1274. /* avoid looping forever */
  1275. if (mds >= cap->mds ||
  1276. ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
  1277. continue;
  1278. /* NOTE: no side-effects allowed, until we take s_mutex */
  1279. revoking = cap->implemented & ~cap->issued;
  1280. if (revoking)
  1281. dout("mds%d revoking %s\n", cap->mds,
  1282. ceph_cap_string(revoking));
  1283. if (cap == ci->i_auth_cap &&
  1284. (cap->issued & CEPH_CAP_FILE_WR)) {
  1285. /* request larger max_size from MDS? */
  1286. if (ci->i_wanted_max_size > ci->i_max_size &&
  1287. ci->i_wanted_max_size > ci->i_requested_max_size) {
  1288. dout("requesting new max_size\n");
  1289. goto ack;
  1290. }
  1291. /* approaching file_max? */
  1292. if ((inode->i_size << 1) >= ci->i_max_size &&
  1293. (ci->i_reported_size << 1) < ci->i_max_size) {
  1294. dout("i_size approaching max_size\n");
  1295. goto ack;
  1296. }
  1297. }
  1298. /* flush anything dirty? */
  1299. if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
  1300. ci->i_dirty_caps) {
  1301. dout("flushing dirty caps\n");
  1302. goto ack;
  1303. }
  1304. /* completed revocation? going down and there are no caps? */
  1305. if (revoking && (revoking & used) == 0) {
  1306. dout("completed revocation of %s\n",
  1307. ceph_cap_string(cap->implemented & ~cap->issued));
  1308. goto ack;
  1309. }
  1310. /* want more caps from mds? */
  1311. if (want & ~(cap->mds_wanted | cap->issued))
  1312. goto ack;
  1313. /* things we might delay */
  1314. if ((cap->issued & ~retain) == 0 &&
  1315. cap->mds_wanted == want)
  1316. continue; /* nope, all good */
  1317. if (is_delayed)
  1318. goto ack;
  1319. /* delay? */
  1320. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  1321. time_before(jiffies, ci->i_hold_caps_max)) {
  1322. dout(" delaying issued %s -> %s, wanted %s -> %s\n",
  1323. ceph_cap_string(cap->issued),
  1324. ceph_cap_string(cap->issued & retain),
  1325. ceph_cap_string(cap->mds_wanted),
  1326. ceph_cap_string(want));
  1327. delayed++;
  1328. continue;
  1329. }
  1330. ack:
  1331. if (session && session != cap->session) {
  1332. dout("oops, wrong session %p mutex\n", session);
  1333. mutex_unlock(&session->s_mutex);
  1334. session = NULL;
  1335. }
  1336. if (!session) {
  1337. session = cap->session;
  1338. if (mutex_trylock(&session->s_mutex) == 0) {
  1339. dout("inverting session/ino locks on %p\n",
  1340. session);
  1341. spin_unlock(&inode->i_lock);
  1342. if (took_snap_rwsem) {
  1343. up_read(&mdsc->snap_rwsem);
  1344. took_snap_rwsem = 0;
  1345. }
  1346. mutex_lock(&session->s_mutex);
  1347. goto retry;
  1348. }
  1349. }
  1350. /* take snap_rwsem after session mutex */
  1351. if (!took_snap_rwsem) {
  1352. if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
  1353. dout("inverting snap/in locks on %p\n",
  1354. inode);
  1355. spin_unlock(&inode->i_lock);
  1356. down_read(&mdsc->snap_rwsem);
  1357. took_snap_rwsem = 1;
  1358. goto retry;
  1359. }
  1360. took_snap_rwsem = 1;
  1361. }
  1362. if (cap == ci->i_auth_cap && ci->i_dirty_caps)
  1363. flushing = __mark_caps_flushing(inode, session);
  1364. mds = cap->mds; /* remember mds, so we don't repeat */
  1365. sent++;
  1366. /* __send_cap drops i_lock */
  1367. delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
  1368. retain, flushing, NULL);
  1369. goto retry; /* retake i_lock and restart our cap scan. */
  1370. }
  1371. /*
  1372. * Reschedule delayed caps release if we delayed anything,
  1373. * otherwise cancel.
  1374. */
  1375. if (delayed && is_delayed)
  1376. force_requeue = 1; /* __send_cap delayed release; requeue */
  1377. if (!delayed && !is_delayed)
  1378. __cap_delay_cancel(mdsc, ci);
  1379. else if (!is_delayed || force_requeue)
  1380. __cap_delay_requeue(mdsc, ci);
  1381. spin_unlock(&inode->i_lock);
  1382. if (session && drop_session_lock)
  1383. mutex_unlock(&session->s_mutex);
  1384. if (took_snap_rwsem)
  1385. up_read(&mdsc->snap_rwsem);
  1386. }
  1387. /*
  1388. * Mark caps dirty. If inode is newly dirty, add to the global dirty
  1389. * list.
  1390. */
  1391. void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
  1392. {
  1393. struct ceph_mds_client *mdsc = &ceph_client(ci->vfs_inode.i_sb)->mdsc;
  1394. struct inode *inode = &ci->vfs_inode;
  1395. int was_dirty = ci->i_dirty_caps;
  1396. int dirty = 0;
  1397. dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
  1398. ceph_cap_string(mask), ceph_cap_string(ci->i_dirty_caps),
  1399. ceph_cap_string(ci->i_dirty_caps | mask));
  1400. ci->i_dirty_caps |= mask;
  1401. if (!was_dirty) {
  1402. dout(" inode %p now dirty\n", &ci->vfs_inode);
  1403. spin_lock(&mdsc->cap_dirty_lock);
  1404. list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
  1405. spin_unlock(&mdsc->cap_dirty_lock);
  1406. if (ci->i_flushing_caps == 0) {
  1407. igrab(inode);
  1408. dirty |= I_DIRTY_SYNC;
  1409. }
  1410. }
  1411. if (((was_dirty | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
  1412. (mask & CEPH_CAP_FILE_BUFFER))
  1413. dirty |= I_DIRTY_DATASYNC;
  1414. if (dirty)
  1415. __mark_inode_dirty(inode, dirty);
  1416. __cap_delay_requeue(mdsc, ci);
  1417. }
  1418. /*
  1419. * Try to flush dirty caps back to the auth mds.
  1420. */
  1421. static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
  1422. unsigned *flush_tid)
  1423. {
  1424. struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc;
  1425. struct ceph_inode_info *ci = ceph_inode(inode);
  1426. int unlock_session = session ? 0 : 1;
  1427. int flushing = 0;
  1428. retry:
  1429. spin_lock(&inode->i_lock);
  1430. if (ci->i_dirty_caps && ci->i_auth_cap) {
  1431. struct ceph_cap *cap = ci->i_auth_cap;
  1432. int used = __ceph_caps_used(ci);
  1433. int want = __ceph_caps_wanted(ci);
  1434. int delayed;
  1435. if (!session) {
  1436. spin_unlock(&inode->i_lock);
  1437. session = cap->session;
  1438. mutex_lock(&session->s_mutex);
  1439. goto retry;
  1440. }
  1441. BUG_ON(session != cap->session);
  1442. if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
  1443. goto out;
  1444. flushing = __mark_caps_flushing(inode, session);
  1445. /* __send_cap drops i_lock */
  1446. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
  1447. cap->issued | cap->implemented, flushing,
  1448. flush_tid);
  1449. if (!delayed)
  1450. goto out_unlocked;
  1451. spin_lock(&inode->i_lock);
  1452. __cap_delay_requeue(mdsc, ci);
  1453. }
  1454. out:
  1455. spin_unlock(&inode->i_lock);
  1456. out_unlocked:
  1457. if (session && unlock_session)
  1458. mutex_unlock(&session->s_mutex);
  1459. return flushing;
  1460. }
  1461. /*
  1462. * Return true if we've flushed caps through the given flush_tid.
  1463. */
  1464. static int caps_are_flushed(struct inode *inode, unsigned tid)
  1465. {
  1466. struct ceph_inode_info *ci = ceph_inode(inode);
  1467. int dirty, i, ret = 1;
  1468. spin_lock(&inode->i_lock);
  1469. dirty = __ceph_caps_dirty(ci);
  1470. for (i = 0; i < CEPH_CAP_BITS; i++)
  1471. if ((ci->i_flushing_caps & (1 << i)) &&
  1472. ci->i_cap_flush_tid[i] <= tid) {
  1473. /* still flushing this bit */
  1474. ret = 0;
  1475. break;
  1476. }
  1477. spin_unlock(&inode->i_lock);
  1478. return ret;
  1479. }
  1480. /*
  1481. * Wait on any unsafe replies for the given inode. First wait on the
  1482. * newest request, and make that the upper bound. Then, if there are
  1483. * more requests, keep waiting on the oldest as long as it is still older
  1484. * than the original request.
  1485. */
  1486. static void sync_write_wait(struct inode *inode)
  1487. {
  1488. struct ceph_inode_info *ci = ceph_inode(inode);
  1489. struct list_head *head = &ci->i_unsafe_writes;
  1490. struct ceph_osd_request *req;
  1491. u64 last_tid;
  1492. spin_lock(&ci->i_unsafe_lock);
  1493. if (list_empty(head))
  1494. goto out;
  1495. /* set upper bound as _last_ entry in chain */
  1496. req = list_entry(head->prev, struct ceph_osd_request,
  1497. r_unsafe_item);
  1498. last_tid = req->r_tid;
  1499. do {
  1500. ceph_osdc_get_request(req);
  1501. spin_unlock(&ci->i_unsafe_lock);
  1502. dout("sync_write_wait on tid %llu (until %llu)\n",
  1503. req->r_tid, last_tid);
  1504. wait_for_completion(&req->r_safe_completion);
  1505. spin_lock(&ci->i_unsafe_lock);
  1506. ceph_osdc_put_request(req);
  1507. /*
  1508. * from here on look at first entry in chain, since we
  1509. * only want to wait for anything older than last_tid
  1510. */
  1511. if (list_empty(head))
  1512. break;
  1513. req = list_entry(head->next, struct ceph_osd_request,
  1514. r_unsafe_item);
  1515. } while (req->r_tid < last_tid);
  1516. out:
  1517. spin_unlock(&ci->i_unsafe_lock);
  1518. }
  1519. int ceph_fsync(struct file *file, struct dentry *dentry, int datasync)
  1520. {
  1521. struct inode *inode = dentry->d_inode;
  1522. struct ceph_inode_info *ci = ceph_inode(inode);
  1523. unsigned flush_tid;
  1524. int ret;
  1525. int dirty;
  1526. dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
  1527. sync_write_wait(inode);
  1528. ret = filemap_write_and_wait(inode->i_mapping);
  1529. if (ret < 0)
  1530. return ret;
  1531. dirty = try_flush_caps(inode, NULL, &flush_tid);
  1532. dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
  1533. /*
  1534. * only wait on non-file metadata writeback (the mds
  1535. * can recover size and mtime, so we don't need to
  1536. * wait for that)
  1537. */
  1538. if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
  1539. dout("fsync waiting for flush_tid %u\n", flush_tid);
  1540. ret = wait_event_interruptible(ci->i_cap_wq,
  1541. caps_are_flushed(inode, flush_tid));
  1542. }
  1543. dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
  1544. return ret;
  1545. }
  1546. /*
  1547. * Flush any dirty caps back to the mds. If we aren't asked to wait,
  1548. * queue inode for flush but don't do so immediately, because we can
  1549. * get by with fewer MDS messages if we wait for data writeback to
  1550. * complete first.
  1551. */
  1552. int ceph_write_inode(struct inode *inode, int wait)
  1553. {
  1554. struct ceph_inode_info *ci = ceph_inode(inode);
  1555. unsigned flush_tid;
  1556. int err = 0;
  1557. int dirty;
  1558. dout("write_inode %p wait=%d\n", inode, wait);
  1559. if (wait) {
  1560. dirty = try_flush_caps(inode, NULL, &flush_tid);
  1561. if (dirty)
  1562. err = wait_event_interruptible(ci->i_cap_wq,
  1563. caps_are_flushed(inode, flush_tid));
  1564. } else {
  1565. struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc;
  1566. spin_lock(&inode->i_lock);
  1567. if (__ceph_caps_dirty(ci))
  1568. __cap_delay_requeue_front(mdsc, ci);
  1569. spin_unlock(&inode->i_lock);
  1570. }
  1571. return err;
  1572. }
  1573. /*
  1574. * After a recovering MDS goes active, we need to resend any caps
  1575. * we were flushing.
  1576. *
  1577. * Caller holds session->s_mutex.
  1578. */
  1579. static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
  1580. struct ceph_mds_session *session)
  1581. {
  1582. struct ceph_cap_snap *capsnap;
  1583. dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
  1584. list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
  1585. flushing_item) {
  1586. struct ceph_inode_info *ci = capsnap->ci;
  1587. struct inode *inode = &ci->vfs_inode;
  1588. struct ceph_cap *cap;
  1589. spin_lock(&inode->i_lock);
  1590. cap = ci->i_auth_cap;
  1591. if (cap && cap->session == session) {
  1592. dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
  1593. cap, capsnap);
  1594. __ceph_flush_snaps(ci, &session);
  1595. } else {
  1596. pr_err("%p auth cap %p not mds%d ???\n", inode,
  1597. cap, session->s_mds);
  1598. spin_unlock(&inode->i_lock);
  1599. }
  1600. }
  1601. }
  1602. void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
  1603. struct ceph_mds_session *session)
  1604. {
  1605. struct ceph_inode_info *ci;
  1606. kick_flushing_capsnaps(mdsc, session);
  1607. dout("kick_flushing_caps mds%d\n", session->s_mds);
  1608. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  1609. struct inode *inode = &ci->vfs_inode;
  1610. struct ceph_cap *cap;
  1611. int delayed = 0;
  1612. spin_lock(&inode->i_lock);
  1613. cap = ci->i_auth_cap;
  1614. if (cap && cap->session == session) {
  1615. dout("kick_flushing_caps %p cap %p %s\n", inode,
  1616. cap, ceph_cap_string(ci->i_flushing_caps));
  1617. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
  1618. __ceph_caps_used(ci),
  1619. __ceph_caps_wanted(ci),
  1620. cap->issued | cap->implemented,
  1621. ci->i_flushing_caps, NULL);
  1622. if (delayed) {
  1623. spin_lock(&inode->i_lock);
  1624. __cap_delay_requeue(mdsc, ci);
  1625. spin_unlock(&inode->i_lock);
  1626. }
  1627. } else {
  1628. pr_err("%p auth cap %p not mds%d ???\n", inode,
  1629. cap, session->s_mds);
  1630. spin_unlock(&inode->i_lock);
  1631. }
  1632. }
  1633. }
  1634. /*
  1635. * Take references to capabilities we hold, so that we don't release
  1636. * them to the MDS prematurely.
  1637. *
  1638. * Protected by i_lock.
  1639. */
  1640. static void __take_cap_refs(struct ceph_inode_info *ci, int got)
  1641. {
  1642. if (got & CEPH_CAP_PIN)
  1643. ci->i_pin_ref++;
  1644. if (got & CEPH_CAP_FILE_RD)
  1645. ci->i_rd_ref++;
  1646. if (got & CEPH_CAP_FILE_CACHE)
  1647. ci->i_rdcache_ref++;
  1648. if (got & CEPH_CAP_FILE_WR)
  1649. ci->i_wr_ref++;
  1650. if (got & CEPH_CAP_FILE_BUFFER) {
  1651. if (ci->i_wrbuffer_ref == 0)
  1652. igrab(&ci->vfs_inode);
  1653. ci->i_wrbuffer_ref++;
  1654. dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
  1655. &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref);
  1656. }
  1657. }
  1658. /*
  1659. * Try to grab cap references. Specify those refs we @want, and the
  1660. * minimal set we @need. Also include the larger offset we are writing
  1661. * to (when applicable), and check against max_size here as well.
  1662. * Note that caller is responsible for ensuring max_size increases are
  1663. * requested from the MDS.
  1664. */
  1665. static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
  1666. int *got, loff_t endoff, int *check_max, int *err)
  1667. {
  1668. struct inode *inode = &ci->vfs_inode;
  1669. int ret = 0;
  1670. int have, implemented;
  1671. dout("get_cap_refs %p need %s want %s\n", inode,
  1672. ceph_cap_string(need), ceph_cap_string(want));
  1673. spin_lock(&inode->i_lock);
  1674. /* make sure we _have_ some caps! */
  1675. if (!__ceph_is_any_caps(ci)) {
  1676. dout("get_cap_refs %p no real caps\n", inode);
  1677. *err = -EBADF;
  1678. ret = 1;
  1679. goto out;
  1680. }
  1681. if (need & CEPH_CAP_FILE_WR) {
  1682. if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
  1683. dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
  1684. inode, endoff, ci->i_max_size);
  1685. if (endoff > ci->i_wanted_max_size) {
  1686. *check_max = 1;
  1687. ret = 1;
  1688. }
  1689. goto out;
  1690. }
  1691. /*
  1692. * If a sync write is in progress, we must wait, so that we
  1693. * can get a final snapshot value for size+mtime.
  1694. */
  1695. if (__ceph_have_pending_cap_snap(ci)) {
  1696. dout("get_cap_refs %p cap_snap_pending\n", inode);
  1697. goto out;
  1698. }
  1699. }
  1700. have = __ceph_caps_issued(ci, &implemented);
  1701. /*
  1702. * disallow writes while a truncate is pending
  1703. */
  1704. if (ci->i_truncate_pending)
  1705. have &= ~CEPH_CAP_FILE_WR;
  1706. if ((have & need) == need) {
  1707. /*
  1708. * Look at (implemented & ~have & not) so that we keep waiting
  1709. * on transition from wanted -> needed caps. This is needed
  1710. * for WRBUFFER|WR -> WR to avoid a new WR sync write from
  1711. * going before a prior buffered writeback happens.
  1712. */
  1713. int not = want & ~(have & need);
  1714. int revoking = implemented & ~have;
  1715. dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
  1716. inode, ceph_cap_string(have), ceph_cap_string(not),
  1717. ceph_cap_string(revoking));
  1718. if ((revoking & not) == 0) {
  1719. *got = need | (have & want);
  1720. __take_cap_refs(ci, *got);
  1721. ret = 1;
  1722. }
  1723. } else {
  1724. dout("get_cap_refs %p have %s needed %s\n", inode,
  1725. ceph_cap_string(have), ceph_cap_string(need));
  1726. }
  1727. out:
  1728. spin_unlock(&inode->i_lock);
  1729. dout("get_cap_refs %p ret %d got %s\n", inode,
  1730. ret, ceph_cap_string(*got));
  1731. return ret;
  1732. }
  1733. /*
  1734. * Check the offset we are writing up to against our current
  1735. * max_size. If necessary, tell the MDS we want to write to
  1736. * a larger offset.
  1737. */
  1738. static void check_max_size(struct inode *inode, loff_t endoff)
  1739. {
  1740. struct ceph_inode_info *ci = ceph_inode(inode);
  1741. int check = 0;
  1742. /* do we need to explicitly request a larger max_size? */
  1743. spin_lock(&inode->i_lock);
  1744. if ((endoff >= ci->i_max_size ||
  1745. endoff > (inode->i_size << 1)) &&
  1746. endoff > ci->i_wanted_max_size) {
  1747. dout("write %p at large endoff %llu, req max_size\n",
  1748. inode, endoff);
  1749. ci->i_wanted_max_size = endoff;
  1750. check = 1;
  1751. }
  1752. spin_unlock(&inode->i_lock);
  1753. if (check)
  1754. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1755. }
  1756. /*
  1757. * Wait for caps, and take cap references. If we can't get a WR cap
  1758. * due to a small max_size, make sure we check_max_size (and possibly
  1759. * ask the mds) so we don't get hung up indefinitely.
  1760. */
  1761. int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
  1762. loff_t endoff)
  1763. {
  1764. int check_max, ret, err;
  1765. retry:
  1766. if (endoff > 0)
  1767. check_max_size(&ci->vfs_inode, endoff);
  1768. check_max = 0;
  1769. err = 0;
  1770. ret = wait_event_interruptible(ci->i_cap_wq,
  1771. try_get_cap_refs(ci, need, want,
  1772. got, endoff,
  1773. &check_max, &err));
  1774. if (err)
  1775. ret = err;
  1776. if (check_max)
  1777. goto retry;
  1778. return ret;
  1779. }
  1780. /*
  1781. * Take cap refs. Caller must already know we hold at least one ref
  1782. * on the caps in question or we don't know this is safe.
  1783. */
  1784. void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
  1785. {
  1786. spin_lock(&ci->vfs_inode.i_lock);
  1787. __take_cap_refs(ci, caps);
  1788. spin_unlock(&ci->vfs_inode.i_lock);
  1789. }
  1790. /*
  1791. * Release cap refs.
  1792. *
  1793. * If we released the last ref on any given cap, call ceph_check_caps
  1794. * to release (or schedule a release).
  1795. *
  1796. * If we are releasing a WR cap (from a sync write), finalize any affected
  1797. * cap_snap, and wake up any waiters.
  1798. */
  1799. void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
  1800. {
  1801. struct inode *inode = &ci->vfs_inode;
  1802. int last = 0, put = 0, flushsnaps = 0, wake = 0;
  1803. struct ceph_cap_snap *capsnap;
  1804. spin_lock(&inode->i_lock);
  1805. if (had & CEPH_CAP_PIN)
  1806. --ci->i_pin_ref;
  1807. if (had & CEPH_CAP_FILE_RD)
  1808. if (--ci->i_rd_ref == 0)
  1809. last++;
  1810. if (had & CEPH_CAP_FILE_CACHE)
  1811. if (--ci->i_rdcache_ref == 0)
  1812. last++;
  1813. if (had & CEPH_CAP_FILE_BUFFER) {
  1814. if (--ci->i_wrbuffer_ref == 0) {
  1815. last++;
  1816. put++;
  1817. }
  1818. dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
  1819. inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref);
  1820. }
  1821. if (had & CEPH_CAP_FILE_WR)
  1822. if (--ci->i_wr_ref == 0) {
  1823. last++;
  1824. if (!list_empty(&ci->i_cap_snaps)) {
  1825. capsnap = list_first_entry(&ci->i_cap_snaps,
  1826. struct ceph_cap_snap,
  1827. ci_item);
  1828. if (capsnap->writing) {
  1829. capsnap->writing = 0;
  1830. flushsnaps =
  1831. __ceph_finish_cap_snap(ci,
  1832. capsnap);
  1833. wake = 1;
  1834. }
  1835. }
  1836. }
  1837. spin_unlock(&inode->i_lock);
  1838. dout("put_cap_refs %p had %s %s\n", inode, ceph_cap_string(had),
  1839. last ? "last" : "");
  1840. if (last && !flushsnaps)
  1841. ceph_check_caps(ci, 0, NULL);
  1842. else if (flushsnaps)
  1843. ceph_flush_snaps(ci);
  1844. if (wake)
  1845. wake_up(&ci->i_cap_wq);
  1846. if (put)
  1847. iput(inode);
  1848. }
  1849. /*
  1850. * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
  1851. * context. Adjust per-snap dirty page accounting as appropriate.
  1852. * Once all dirty data for a cap_snap is flushed, flush snapped file
  1853. * metadata back to the MDS. If we dropped the last ref, call
  1854. * ceph_check_caps.
  1855. */
  1856. void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
  1857. struct ceph_snap_context *snapc)
  1858. {
  1859. struct inode *inode = &ci->vfs_inode;
  1860. int last = 0;
  1861. int last_snap = 0;
  1862. int found = 0;
  1863. struct ceph_cap_snap *capsnap = NULL;
  1864. spin_lock(&inode->i_lock);
  1865. ci->i_wrbuffer_ref -= nr;
  1866. last = !ci->i_wrbuffer_ref;
  1867. if (ci->i_head_snapc == snapc) {
  1868. ci->i_wrbuffer_ref_head -= nr;
  1869. if (!ci->i_wrbuffer_ref_head) {
  1870. ceph_put_snap_context(ci->i_head_snapc);
  1871. ci->i_head_snapc = NULL;
  1872. }
  1873. dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
  1874. inode,
  1875. ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
  1876. ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  1877. last ? " LAST" : "");
  1878. } else {
  1879. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1880. if (capsnap->context == snapc) {
  1881. found = 1;
  1882. capsnap->dirty_pages -= nr;
  1883. last_snap = !capsnap->dirty_pages;
  1884. break;
  1885. }
  1886. }
  1887. BUG_ON(!found);
  1888. dout("put_wrbuffer_cap_refs on %p cap_snap %p "
  1889. " snap %lld %d/%d -> %d/%d %s%s\n",
  1890. inode, capsnap, capsnap->context->seq,
  1891. ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
  1892. ci->i_wrbuffer_ref, capsnap->dirty_pages,
  1893. last ? " (wrbuffer last)" : "",
  1894. last_snap ? " (capsnap last)" : "");
  1895. }
  1896. spin_unlock(&inode->i_lock);
  1897. if (last) {
  1898. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1899. iput(inode);
  1900. } else if (last_snap) {
  1901. ceph_flush_snaps(ci);
  1902. wake_up(&ci->i_cap_wq);
  1903. }
  1904. }
  1905. /*
  1906. * Handle a cap GRANT message from the MDS. (Note that a GRANT may
  1907. * actually be a revocation if it specifies a smaller cap set.)
  1908. *
  1909. * caller holds s_mutex.
  1910. * return value:
  1911. * 0 - ok
  1912. * 1 - check_caps on auth cap only (writeback)
  1913. * 2 - check_caps (ack revoke)
  1914. */
  1915. static int handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
  1916. struct ceph_mds_session *session,
  1917. struct ceph_cap *cap,
  1918. struct ceph_buffer *xattr_buf)
  1919. __releases(inode->i_lock)
  1920. {
  1921. struct ceph_inode_info *ci = ceph_inode(inode);
  1922. int mds = session->s_mds;
  1923. int seq = le32_to_cpu(grant->seq);
  1924. int newcaps = le32_to_cpu(grant->caps);
  1925. int issued, implemented, used, wanted, dirty;
  1926. u64 size = le64_to_cpu(grant->size);
  1927. u64 max_size = le64_to_cpu(grant->max_size);
  1928. struct timespec mtime, atime, ctime;
  1929. int reply = 0;
  1930. int wake = 0;
  1931. int writeback = 0;
  1932. int revoked_rdcache = 0;
  1933. int invalidate_async = 0;
  1934. int tried_invalidate = 0;
  1935. int ret;
  1936. dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
  1937. inode, cap, mds, seq, ceph_cap_string(newcaps));
  1938. dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
  1939. inode->i_size);
  1940. /*
  1941. * If CACHE is being revoked, and we have no dirty buffers,
  1942. * try to invalidate (once). (If there are dirty buffers, we
  1943. * will invalidate _after_ writeback.)
  1944. */
  1945. restart:
  1946. if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
  1947. !ci->i_wrbuffer_ref && !tried_invalidate) {
  1948. dout("CACHE invalidation\n");
  1949. spin_unlock(&inode->i_lock);
  1950. tried_invalidate = 1;
  1951. ret = invalidate_inode_pages2(&inode->i_data);
  1952. spin_lock(&inode->i_lock);
  1953. if (ret < 0) {
  1954. /* there were locked pages.. invalidate later
  1955. in a separate thread. */
  1956. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1957. invalidate_async = 1;
  1958. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  1959. }
  1960. } else {
  1961. /* we successfully invalidated those pages */
  1962. revoked_rdcache = 1;
  1963. ci->i_rdcache_gen = 0;
  1964. ci->i_rdcache_revoking = 0;
  1965. }
  1966. goto restart;
  1967. }
  1968. /* side effects now are allowed */
  1969. issued = __ceph_caps_issued(ci, &implemented);
  1970. issued |= implemented | __ceph_caps_dirty(ci);
  1971. cap->gen = session->s_cap_gen;
  1972. __check_cap_issue(ci, cap, newcaps);
  1973. if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
  1974. inode->i_mode = le32_to_cpu(grant->mode);
  1975. inode->i_uid = le32_to_cpu(grant->uid);
  1976. inode->i_gid = le32_to_cpu(grant->gid);
  1977. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  1978. inode->i_uid, inode->i_gid);
  1979. }
  1980. if ((issued & CEPH_CAP_LINK_EXCL) == 0)
  1981. inode->i_nlink = le32_to_cpu(grant->nlink);
  1982. if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
  1983. int len = le32_to_cpu(grant->xattr_len);
  1984. u64 version = le64_to_cpu(grant->xattr_version);
  1985. if (version > ci->i_xattrs.version) {
  1986. dout(" got new xattrs v%llu on %p len %d\n",
  1987. version, inode, len);
  1988. if (ci->i_xattrs.blob)
  1989. ceph_buffer_put(ci->i_xattrs.blob);
  1990. ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
  1991. ci->i_xattrs.version = version;
  1992. }
  1993. }
  1994. /* size/ctime/mtime/atime? */
  1995. ceph_fill_file_size(inode, issued,
  1996. le32_to_cpu(grant->truncate_seq),
  1997. le64_to_cpu(grant->truncate_size), size);
  1998. ceph_decode_timespec(&mtime, &grant->mtime);
  1999. ceph_decode_timespec(&atime, &grant->atime);
  2000. ceph_decode_timespec(&ctime, &grant->ctime);
  2001. ceph_fill_file_time(inode, issued,
  2002. le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
  2003. &atime);
  2004. /* max size increase? */
  2005. if (max_size != ci->i_max_size) {
  2006. dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
  2007. ci->i_max_size = max_size;
  2008. if (max_size >= ci->i_wanted_max_size) {
  2009. ci->i_wanted_max_size = 0; /* reset */
  2010. ci->i_requested_max_size = 0;
  2011. }
  2012. wake = 1;
  2013. }
  2014. /* check cap bits */
  2015. wanted = __ceph_caps_wanted(ci);
  2016. used = __ceph_caps_used(ci);
  2017. dirty = __ceph_caps_dirty(ci);
  2018. dout(" my wanted = %s, used = %s, dirty %s\n",
  2019. ceph_cap_string(wanted),
  2020. ceph_cap_string(used),
  2021. ceph_cap_string(dirty));
  2022. if (wanted != le32_to_cpu(grant->wanted)) {
  2023. dout("mds wanted %s -> %s\n",
  2024. ceph_cap_string(le32_to_cpu(grant->wanted)),
  2025. ceph_cap_string(wanted));
  2026. grant->wanted = cpu_to_le32(wanted);
  2027. }
  2028. cap->seq = seq;
  2029. /* file layout may have changed */
  2030. ci->i_layout = grant->layout;
  2031. /* revocation, grant, or no-op? */
  2032. if (cap->issued & ~newcaps) {
  2033. dout("revocation: %s -> %s\n", ceph_cap_string(cap->issued),
  2034. ceph_cap_string(newcaps));
  2035. if ((used & ~newcaps) & CEPH_CAP_FILE_BUFFER)
  2036. writeback = 1; /* will delay ack */
  2037. else if (dirty & ~newcaps)
  2038. reply = 1; /* initiate writeback in check_caps */
  2039. else if (((used & ~newcaps) & CEPH_CAP_FILE_CACHE) == 0 ||
  2040. revoked_rdcache)
  2041. reply = 2; /* send revoke ack in check_caps */
  2042. cap->issued = newcaps;
  2043. } else if (cap->issued == newcaps) {
  2044. dout("caps unchanged: %s -> %s\n",
  2045. ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
  2046. } else {
  2047. dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
  2048. ceph_cap_string(newcaps));
  2049. cap->issued = newcaps;
  2050. cap->implemented |= newcaps; /* add bits only, to
  2051. * avoid stepping on a
  2052. * pending revocation */
  2053. wake = 1;
  2054. }
  2055. spin_unlock(&inode->i_lock);
  2056. if (writeback) {
  2057. /*
  2058. * queue inode for writeback: we can't actually call
  2059. * filemap_write_and_wait, etc. from message handler
  2060. * context.
  2061. */
  2062. dout("queueing %p for writeback\n", inode);
  2063. if (ceph_queue_writeback(inode))
  2064. igrab(inode);
  2065. }
  2066. if (invalidate_async) {
  2067. dout("queueing %p for page invalidation\n", inode);
  2068. if (ceph_queue_page_invalidation(inode))
  2069. igrab(inode);
  2070. }
  2071. if (wake)
  2072. wake_up(&ci->i_cap_wq);
  2073. return reply;
  2074. }
  2075. /*
  2076. * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
  2077. * MDS has been safely committed.
  2078. */
  2079. static void handle_cap_flush_ack(struct inode *inode,
  2080. struct ceph_mds_caps *m,
  2081. struct ceph_mds_session *session,
  2082. struct ceph_cap *cap)
  2083. __releases(inode->i_lock)
  2084. {
  2085. struct ceph_inode_info *ci = ceph_inode(inode);
  2086. struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc;
  2087. unsigned seq = le32_to_cpu(m->seq);
  2088. int dirty = le32_to_cpu(m->dirty);
  2089. int cleaned = 0;
  2090. u64 flush_tid = le64_to_cpu(m->client_tid);
  2091. int drop = 0;
  2092. int i;
  2093. for (i = 0; i < CEPH_CAP_BITS; i++)
  2094. if ((dirty & (1 << i)) &&
  2095. flush_tid == ci->i_cap_flush_tid[i])
  2096. cleaned |= 1 << i;
  2097. dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
  2098. " flushing %s -> %s\n",
  2099. inode, session->s_mds, seq, ceph_cap_string(dirty),
  2100. ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
  2101. ceph_cap_string(ci->i_flushing_caps & ~cleaned));
  2102. if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
  2103. goto out;
  2104. ci->i_flushing_caps &= ~cleaned;
  2105. spin_lock(&mdsc->cap_dirty_lock);
  2106. if (ci->i_flushing_caps == 0) {
  2107. list_del_init(&ci->i_flushing_item);
  2108. if (!list_empty(&session->s_cap_flushing))
  2109. dout(" mds%d still flushing cap on %p\n",
  2110. session->s_mds,
  2111. &list_entry(session->s_cap_flushing.next,
  2112. struct ceph_inode_info,
  2113. i_flushing_item)->vfs_inode);
  2114. mdsc->num_cap_flushing--;
  2115. wake_up(&mdsc->cap_flushing_wq);
  2116. dout(" inode %p now !flushing\n", inode);
  2117. if (ci->i_dirty_caps == 0) {
  2118. dout(" inode %p now clean\n", inode);
  2119. BUG_ON(!list_empty(&ci->i_dirty_item));
  2120. drop = 1;
  2121. }
  2122. }
  2123. spin_unlock(&mdsc->cap_dirty_lock);
  2124. wake_up(&ci->i_cap_wq);
  2125. out:
  2126. spin_unlock(&inode->i_lock);
  2127. if (drop)
  2128. iput(inode);
  2129. }
  2130. /*
  2131. * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
  2132. * throw away our cap_snap.
  2133. *
  2134. * Caller hold s_mutex.
  2135. */
  2136. static void handle_cap_flushsnap_ack(struct inode *inode,
  2137. struct ceph_mds_caps *m,
  2138. struct ceph_mds_session *session)
  2139. {
  2140. struct ceph_inode_info *ci = ceph_inode(inode);
  2141. u64 follows = le64_to_cpu(m->snap_follows);
  2142. u64 flush_tid = le64_to_cpu(m->client_tid);
  2143. struct ceph_cap_snap *capsnap;
  2144. int drop = 0;
  2145. dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
  2146. inode, ci, session->s_mds, follows);
  2147. spin_lock(&inode->i_lock);
  2148. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  2149. if (capsnap->follows == follows) {
  2150. if (capsnap->flush_tid != flush_tid) {
  2151. dout(" cap_snap %p follows %lld tid %lld !="
  2152. " %lld\n", capsnap, follows,
  2153. flush_tid, capsnap->flush_tid);
  2154. break;
  2155. }
  2156. WARN_ON(capsnap->dirty_pages || capsnap->writing);
  2157. dout(" removing cap_snap %p follows %lld\n",
  2158. capsnap, follows);
  2159. ceph_put_snap_context(capsnap->context);
  2160. list_del(&capsnap->ci_item);
  2161. list_del(&capsnap->flushing_item);
  2162. ceph_put_cap_snap(capsnap);
  2163. drop = 1;
  2164. break;
  2165. } else {
  2166. dout(" skipping cap_snap %p follows %lld\n",
  2167. capsnap, capsnap->follows);
  2168. }
  2169. }
  2170. spin_unlock(&inode->i_lock);
  2171. if (drop)
  2172. iput(inode);
  2173. }
  2174. /*
  2175. * Handle TRUNC from MDS, indicating file truncation.
  2176. *
  2177. * caller hold s_mutex.
  2178. */
  2179. static void handle_cap_trunc(struct inode *inode,
  2180. struct ceph_mds_caps *trunc,
  2181. struct ceph_mds_session *session)
  2182. __releases(inode->i_lock)
  2183. {
  2184. struct ceph_inode_info *ci = ceph_inode(inode);
  2185. int mds = session->s_mds;
  2186. int seq = le32_to_cpu(trunc->seq);
  2187. u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
  2188. u64 truncate_size = le64_to_cpu(trunc->truncate_size);
  2189. u64 size = le64_to_cpu(trunc->size);
  2190. int implemented = 0;
  2191. int dirty = __ceph_caps_dirty(ci);
  2192. int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
  2193. int queue_trunc = 0;
  2194. issued |= implemented | dirty;
  2195. dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
  2196. inode, mds, seq, truncate_size, truncate_seq);
  2197. queue_trunc = ceph_fill_file_size(inode, issued,
  2198. truncate_seq, truncate_size, size);
  2199. spin_unlock(&inode->i_lock);
  2200. if (queue_trunc)
  2201. if (queue_work(ceph_client(inode->i_sb)->trunc_wq,
  2202. &ci->i_vmtruncate_work))
  2203. igrab(inode);
  2204. }
  2205. /*
  2206. * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
  2207. * different one. If we are the most recent migration we've seen (as
  2208. * indicated by mseq), make note of the migrating cap bits for the
  2209. * duration (until we see the corresponding IMPORT).
  2210. *
  2211. * caller holds s_mutex
  2212. */
  2213. static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
  2214. struct ceph_mds_session *session)
  2215. {
  2216. struct ceph_inode_info *ci = ceph_inode(inode);
  2217. int mds = session->s_mds;
  2218. unsigned mseq = le32_to_cpu(ex->migrate_seq);
  2219. struct ceph_cap *cap = NULL, *t;
  2220. struct rb_node *p;
  2221. int remember = 1;
  2222. dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
  2223. inode, ci, mds, mseq);
  2224. spin_lock(&inode->i_lock);
  2225. /* make sure we haven't seen a higher mseq */
  2226. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  2227. t = rb_entry(p, struct ceph_cap, ci_node);
  2228. if (ceph_seq_cmp(t->mseq, mseq) > 0) {
  2229. dout(" higher mseq on cap from mds%d\n",
  2230. t->session->s_mds);
  2231. remember = 0;
  2232. }
  2233. if (t->session->s_mds == mds)
  2234. cap = t;
  2235. }
  2236. if (cap) {
  2237. if (remember) {
  2238. /* make note */
  2239. ci->i_cap_exporting_mds = mds;
  2240. ci->i_cap_exporting_mseq = mseq;
  2241. ci->i_cap_exporting_issued = cap->issued;
  2242. }
  2243. __ceph_remove_cap(cap, NULL);
  2244. } else {
  2245. WARN_ON(!cap);
  2246. }
  2247. spin_unlock(&inode->i_lock);
  2248. }
  2249. /*
  2250. * Handle cap IMPORT. If there are temp bits from an older EXPORT,
  2251. * clean them up.
  2252. *
  2253. * caller holds s_mutex.
  2254. */
  2255. static void handle_cap_import(struct ceph_mds_client *mdsc,
  2256. struct inode *inode, struct ceph_mds_caps *im,
  2257. struct ceph_mds_session *session,
  2258. void *snaptrace, int snaptrace_len)
  2259. {
  2260. struct ceph_inode_info *ci = ceph_inode(inode);
  2261. int mds = session->s_mds;
  2262. unsigned issued = le32_to_cpu(im->caps);
  2263. unsigned wanted = le32_to_cpu(im->wanted);
  2264. unsigned seq = le32_to_cpu(im->seq);
  2265. unsigned mseq = le32_to_cpu(im->migrate_seq);
  2266. u64 realmino = le64_to_cpu(im->realm);
  2267. u64 cap_id = le64_to_cpu(im->cap_id);
  2268. if (ci->i_cap_exporting_mds >= 0 &&
  2269. ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
  2270. dout("handle_cap_import inode %p ci %p mds%d mseq %d"
  2271. " - cleared exporting from mds%d\n",
  2272. inode, ci, mds, mseq,
  2273. ci->i_cap_exporting_mds);
  2274. ci->i_cap_exporting_issued = 0;
  2275. ci->i_cap_exporting_mseq = 0;
  2276. ci->i_cap_exporting_mds = -1;
  2277. } else {
  2278. dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
  2279. inode, ci, mds, mseq);
  2280. }
  2281. down_write(&mdsc->snap_rwsem);
  2282. ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
  2283. false);
  2284. downgrade_write(&mdsc->snap_rwsem);
  2285. ceph_add_cap(inode, session, cap_id, -1,
  2286. issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
  2287. NULL /* no caps context */);
  2288. try_flush_caps(inode, session, NULL);
  2289. up_read(&mdsc->snap_rwsem);
  2290. }
  2291. /*
  2292. * Handle a caps message from the MDS.
  2293. *
  2294. * Identify the appropriate session, inode, and call the right handler
  2295. * based on the cap op.
  2296. */
  2297. void ceph_handle_caps(struct ceph_mds_session *session,
  2298. struct ceph_msg *msg)
  2299. {
  2300. struct ceph_mds_client *mdsc = session->s_mdsc;
  2301. struct super_block *sb = mdsc->client->sb;
  2302. struct inode *inode;
  2303. struct ceph_cap *cap;
  2304. struct ceph_mds_caps *h;
  2305. int mds = le64_to_cpu(msg->hdr.src.name.num);
  2306. int op;
  2307. u32 seq;
  2308. struct ceph_vino vino;
  2309. u64 cap_id;
  2310. u64 size, max_size;
  2311. int check_caps = 0;
  2312. int r;
  2313. dout("handle_caps from mds%d\n", mds);
  2314. /* decode */
  2315. if (msg->front.iov_len < sizeof(*h))
  2316. goto bad;
  2317. h = msg->front.iov_base;
  2318. op = le32_to_cpu(h->op);
  2319. vino.ino = le64_to_cpu(h->ino);
  2320. vino.snap = CEPH_NOSNAP;
  2321. cap_id = le64_to_cpu(h->cap_id);
  2322. seq = le32_to_cpu(h->seq);
  2323. size = le64_to_cpu(h->size);
  2324. max_size = le64_to_cpu(h->max_size);
  2325. mutex_lock(&session->s_mutex);
  2326. session->s_seq++;
  2327. dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
  2328. (unsigned)seq);
  2329. /* lookup ino */
  2330. inode = ceph_find_inode(sb, vino);
  2331. dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
  2332. vino.snap, inode);
  2333. if (!inode) {
  2334. dout(" i don't have ino %llx\n", vino.ino);
  2335. goto done;
  2336. }
  2337. /* these will work even if we don't have a cap yet */
  2338. switch (op) {
  2339. case CEPH_CAP_OP_FLUSHSNAP_ACK:
  2340. handle_cap_flushsnap_ack(inode, h, session);
  2341. goto done;
  2342. case CEPH_CAP_OP_EXPORT:
  2343. handle_cap_export(inode, h, session);
  2344. goto done;
  2345. case CEPH_CAP_OP_IMPORT:
  2346. handle_cap_import(mdsc, inode, h, session,
  2347. msg->middle,
  2348. le32_to_cpu(h->snap_trace_len));
  2349. check_caps = 1; /* we may have sent a RELEASE to the old auth */
  2350. goto done;
  2351. }
  2352. /* the rest require a cap */
  2353. spin_lock(&inode->i_lock);
  2354. cap = __get_cap_for_mds(ceph_inode(inode), mds);
  2355. if (!cap) {
  2356. dout("no cap on %p ino %llx.%llx from mds%d, releasing\n",
  2357. inode, ceph_ino(inode), ceph_snap(inode), mds);
  2358. spin_unlock(&inode->i_lock);
  2359. goto done;
  2360. }
  2361. /* note that each of these drops i_lock for us */
  2362. switch (op) {
  2363. case CEPH_CAP_OP_REVOKE:
  2364. case CEPH_CAP_OP_GRANT:
  2365. r = handle_cap_grant(inode, h, session, cap, msg->middle);
  2366. if (r == 1)
  2367. ceph_check_caps(ceph_inode(inode),
  2368. CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
  2369. session);
  2370. else if (r == 2)
  2371. ceph_check_caps(ceph_inode(inode),
  2372. CHECK_CAPS_NODELAY,
  2373. session);
  2374. break;
  2375. case CEPH_CAP_OP_FLUSH_ACK:
  2376. handle_cap_flush_ack(inode, h, session, cap);
  2377. break;
  2378. case CEPH_CAP_OP_TRUNC:
  2379. handle_cap_trunc(inode, h, session);
  2380. break;
  2381. default:
  2382. spin_unlock(&inode->i_lock);
  2383. pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
  2384. ceph_cap_op_name(op));
  2385. }
  2386. done:
  2387. mutex_unlock(&session->s_mutex);
  2388. if (check_caps)
  2389. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY, NULL);
  2390. if (inode)
  2391. iput(inode);
  2392. return;
  2393. bad:
  2394. pr_err("ceph_handle_caps: corrupt message\n");
  2395. return;
  2396. }
  2397. /*
  2398. * Delayed work handler to process end of delayed cap release LRU list.
  2399. */
  2400. void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
  2401. {
  2402. struct ceph_inode_info *ci;
  2403. int flags = CHECK_CAPS_NODELAY;
  2404. dout("check_delayed_caps\n");
  2405. while (1) {
  2406. spin_lock(&mdsc->cap_delay_lock);
  2407. if (list_empty(&mdsc->cap_delay_list))
  2408. break;
  2409. ci = list_first_entry(&mdsc->cap_delay_list,
  2410. struct ceph_inode_info,
  2411. i_cap_delay_list);
  2412. if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
  2413. time_before(jiffies, ci->i_hold_caps_max))
  2414. break;
  2415. list_del_init(&ci->i_cap_delay_list);
  2416. spin_unlock(&mdsc->cap_delay_lock);
  2417. dout("check_delayed_caps on %p\n", &ci->vfs_inode);
  2418. ceph_check_caps(ci, flags, NULL);
  2419. }
  2420. spin_unlock(&mdsc->cap_delay_lock);
  2421. }
  2422. /*
  2423. * Flush all dirty caps to the mds
  2424. */
  2425. void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
  2426. {
  2427. struct ceph_inode_info *ci;
  2428. struct inode *inode;
  2429. dout("flush_dirty_caps\n");
  2430. spin_lock(&mdsc->cap_dirty_lock);
  2431. while (!list_empty(&mdsc->cap_dirty)) {
  2432. ci = list_first_entry(&mdsc->cap_dirty,
  2433. struct ceph_inode_info,
  2434. i_dirty_item);
  2435. inode = igrab(&ci->vfs_inode);
  2436. spin_unlock(&mdsc->cap_dirty_lock);
  2437. if (inode) {
  2438. ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH,
  2439. NULL);
  2440. iput(inode);
  2441. }
  2442. spin_lock(&mdsc->cap_dirty_lock);
  2443. }
  2444. spin_unlock(&mdsc->cap_dirty_lock);
  2445. }
  2446. /*
  2447. * Drop open file reference. If we were the last open file,
  2448. * we may need to release capabilities to the MDS (or schedule
  2449. * their delayed release).
  2450. */
  2451. void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
  2452. {
  2453. struct inode *inode = &ci->vfs_inode;
  2454. int last = 0;
  2455. spin_lock(&inode->i_lock);
  2456. dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
  2457. ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
  2458. BUG_ON(ci->i_nr_by_mode[fmode] == 0);
  2459. if (--ci->i_nr_by_mode[fmode] == 0)
  2460. last++;
  2461. spin_unlock(&inode->i_lock);
  2462. if (last && ci->i_vino.snap == CEPH_NOSNAP)
  2463. ceph_check_caps(ci, 0, NULL);
  2464. }
  2465. /*
  2466. * Helpers for embedding cap and dentry lease releases into mds
  2467. * requests.
  2468. *
  2469. * @force is used by dentry_release (below) to force inclusion of a
  2470. * record for the directory inode, even when there aren't any caps to
  2471. * drop.
  2472. */
  2473. int ceph_encode_inode_release(void **p, struct inode *inode,
  2474. int mds, int drop, int unless, int force)
  2475. {
  2476. struct ceph_inode_info *ci = ceph_inode(inode);
  2477. struct ceph_cap *cap;
  2478. struct ceph_mds_request_release *rel = *p;
  2479. int ret = 0;
  2480. dout("encode_inode_release %p mds%d drop %s unless %s\n", inode,
  2481. mds, ceph_cap_string(drop), ceph_cap_string(unless));
  2482. spin_lock(&inode->i_lock);
  2483. cap = __get_cap_for_mds(ci, mds);
  2484. if (cap && __cap_is_valid(cap)) {
  2485. if (force ||
  2486. ((cap->issued & drop) &&
  2487. (cap->issued & unless) == 0)) {
  2488. if ((cap->issued & drop) &&
  2489. (cap->issued & unless) == 0) {
  2490. dout("encode_inode_release %p cap %p %s -> "
  2491. "%s\n", inode, cap,
  2492. ceph_cap_string(cap->issued),
  2493. ceph_cap_string(cap->issued & ~drop));
  2494. cap->issued &= ~drop;
  2495. cap->implemented &= ~drop;
  2496. if (ci->i_ceph_flags & CEPH_I_NODELAY) {
  2497. int wanted = __ceph_caps_wanted(ci);
  2498. dout(" wanted %s -> %s (act %s)\n",
  2499. ceph_cap_string(cap->mds_wanted),
  2500. ceph_cap_string(cap->mds_wanted &
  2501. ~wanted),
  2502. ceph_cap_string(wanted));
  2503. cap->mds_wanted &= wanted;
  2504. }
  2505. } else {
  2506. dout("encode_inode_release %p cap %p %s"
  2507. " (force)\n", inode, cap,
  2508. ceph_cap_string(cap->issued));
  2509. }
  2510. rel->ino = cpu_to_le64(ceph_ino(inode));
  2511. rel->cap_id = cpu_to_le64(cap->cap_id);
  2512. rel->seq = cpu_to_le32(cap->seq);
  2513. rel->issue_seq = cpu_to_le32(cap->issue_seq),
  2514. rel->mseq = cpu_to_le32(cap->mseq);
  2515. rel->caps = cpu_to_le32(cap->issued);
  2516. rel->wanted = cpu_to_le32(cap->mds_wanted);
  2517. rel->dname_len = 0;
  2518. rel->dname_seq = 0;
  2519. *p += sizeof(*rel);
  2520. ret = 1;
  2521. } else {
  2522. dout("encode_inode_release %p cap %p %s\n",
  2523. inode, cap, ceph_cap_string(cap->issued));
  2524. }
  2525. }
  2526. spin_unlock(&inode->i_lock);
  2527. return ret;
  2528. }
  2529. int ceph_encode_dentry_release(void **p, struct dentry *dentry,
  2530. int mds, int drop, int unless)
  2531. {
  2532. struct inode *dir = dentry->d_parent->d_inode;
  2533. struct ceph_mds_request_release *rel = *p;
  2534. struct ceph_dentry_info *di = ceph_dentry(dentry);
  2535. int force = 0;
  2536. int ret;
  2537. /*
  2538. * force an record for the directory caps if we have a dentry lease.
  2539. * this is racy (can't take i_lock and d_lock together), but it
  2540. * doesn't have to be perfect; the mds will revoke anything we don't
  2541. * release.
  2542. */
  2543. spin_lock(&dentry->d_lock);
  2544. if (di->lease_session && di->lease_session->s_mds == mds)
  2545. force = 1;
  2546. spin_unlock(&dentry->d_lock);
  2547. ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
  2548. spin_lock(&dentry->d_lock);
  2549. if (ret && di->lease_session && di->lease_session->s_mds == mds) {
  2550. dout("encode_dentry_release %p mds%d seq %d\n",
  2551. dentry, mds, (int)di->lease_seq);
  2552. rel->dname_len = cpu_to_le32(dentry->d_name.len);
  2553. memcpy(*p, dentry->d_name.name, dentry->d_name.len);
  2554. *p += dentry->d_name.len;
  2555. rel->dname_seq = cpu_to_le32(di->lease_seq);
  2556. }
  2557. spin_unlock(&dentry->d_lock);
  2558. return ret;
  2559. }