caps.c 82 KB

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