caps.c 88 KB

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