caps.c 84 KB

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