caps.c 85 KB

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