caps.c 80 KB

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