caps.c 84 KB

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