caps.c 78 KB

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