caps.c 77 KB

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