caps.c 82 KB

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