caps.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766
  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. struct ceph_cap *ceph_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. void 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 **new_cap)
  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 *cap;
  464. int mds = session->s_mds;
  465. int actual_wanted;
  466. dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
  467. session->s_mds, cap_id, ceph_cap_string(issued), seq);
  468. /*
  469. * If we are opening the file, include file mode wanted bits
  470. * in wanted.
  471. */
  472. if (fmode >= 0)
  473. wanted |= ceph_caps_for_mode(fmode);
  474. cap = __get_cap_for_mds(ci, mds);
  475. if (!cap) {
  476. cap = *new_cap;
  477. *new_cap = NULL;
  478. cap->issued = 0;
  479. cap->implemented = 0;
  480. cap->mds = mds;
  481. cap->mds_wanted = 0;
  482. cap->mseq = 0;
  483. cap->ci = ci;
  484. __insert_cap_node(ci, cap);
  485. /* add to session cap list */
  486. cap->session = session;
  487. spin_lock(&session->s_cap_lock);
  488. list_add_tail(&cap->session_caps, &session->s_caps);
  489. session->s_nr_caps++;
  490. spin_unlock(&session->s_cap_lock);
  491. } else {
  492. /*
  493. * auth mds of the inode changed. we received the cap export
  494. * message, but still haven't received the cap import message.
  495. * handle_cap_export() updated the new auth MDS' cap.
  496. *
  497. * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
  498. * a message that was send before the cap import message. So
  499. * don't remove caps.
  500. */
  501. if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  502. WARN_ON(cap != ci->i_auth_cap);
  503. WARN_ON(cap->cap_id != cap_id);
  504. seq = cap->seq;
  505. mseq = cap->mseq;
  506. issued |= cap->issued;
  507. flags |= CEPH_CAP_FLAG_AUTH;
  508. }
  509. }
  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. spin_lock(&realm->inodes_with_caps_lock);
  518. ci->i_snap_realm = realm;
  519. list_add(&ci->i_snap_realm_item,
  520. &realm->inodes_with_caps);
  521. spin_unlock(&realm->inodes_with_caps_lock);
  522. } else {
  523. pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
  524. realmino);
  525. WARN_ON(!realm);
  526. }
  527. }
  528. __check_cap_issue(ci, cap, issued);
  529. /*
  530. * If we are issued caps we don't want, or the mds' wanted
  531. * value appears to be off, queue a check so we'll release
  532. * later and/or update the mds wanted value.
  533. */
  534. actual_wanted = __ceph_caps_wanted(ci);
  535. if ((wanted & ~actual_wanted) ||
  536. (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
  537. dout(" issued %s, mds wanted %s, actual %s, queueing\n",
  538. ceph_cap_string(issued), ceph_cap_string(wanted),
  539. ceph_cap_string(actual_wanted));
  540. __cap_delay_requeue(mdsc, ci);
  541. }
  542. if (flags & CEPH_CAP_FLAG_AUTH) {
  543. if (ci->i_auth_cap == NULL ||
  544. ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
  545. ci->i_auth_cap = cap;
  546. cap->mds_wanted = wanted;
  547. }
  548. } else {
  549. WARN_ON(ci->i_auth_cap == cap);
  550. }
  551. dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
  552. inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
  553. ceph_cap_string(issued|cap->issued), seq, mds);
  554. cap->cap_id = cap_id;
  555. cap->issued = issued;
  556. cap->implemented |= issued;
  557. if (ceph_seq_cmp(mseq, cap->mseq) > 0)
  558. cap->mds_wanted = wanted;
  559. else
  560. cap->mds_wanted |= wanted;
  561. cap->seq = seq;
  562. cap->issue_seq = seq;
  563. cap->mseq = mseq;
  564. cap->cap_gen = session->s_cap_gen;
  565. if (fmode >= 0)
  566. __ceph_get_fmode(ci, fmode);
  567. }
  568. /*
  569. * Return true if cap has not timed out and belongs to the current
  570. * generation of the MDS session (i.e. has not gone 'stale' due to
  571. * us losing touch with the mds).
  572. */
  573. static int __cap_is_valid(struct ceph_cap *cap)
  574. {
  575. unsigned long ttl;
  576. u32 gen;
  577. spin_lock(&cap->session->s_gen_ttl_lock);
  578. gen = cap->session->s_cap_gen;
  579. ttl = cap->session->s_cap_ttl;
  580. spin_unlock(&cap->session->s_gen_ttl_lock);
  581. if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
  582. dout("__cap_is_valid %p cap %p issued %s "
  583. "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
  584. cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
  585. return 0;
  586. }
  587. return 1;
  588. }
  589. /*
  590. * Return set of valid cap bits issued to us. Note that caps time
  591. * out, and may be invalidated in bulk if the client session times out
  592. * and session->s_cap_gen is bumped.
  593. */
  594. int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
  595. {
  596. int have = ci->i_snap_caps;
  597. struct ceph_cap *cap;
  598. struct rb_node *p;
  599. if (implemented)
  600. *implemented = 0;
  601. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  602. cap = rb_entry(p, struct ceph_cap, ci_node);
  603. if (!__cap_is_valid(cap))
  604. continue;
  605. dout("__ceph_caps_issued %p cap %p issued %s\n",
  606. &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
  607. have |= cap->issued;
  608. if (implemented)
  609. *implemented |= cap->implemented;
  610. }
  611. /*
  612. * exclude caps issued by non-auth MDS, but are been revoking
  613. * by the auth MDS. The non-auth MDS should be revoking/exporting
  614. * these caps, but the message is delayed.
  615. */
  616. if (ci->i_auth_cap) {
  617. cap = ci->i_auth_cap;
  618. have &= ~cap->implemented | cap->issued;
  619. }
  620. return have;
  621. }
  622. /*
  623. * Get cap bits issued by caps other than @ocap
  624. */
  625. int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
  626. {
  627. int have = ci->i_snap_caps;
  628. struct ceph_cap *cap;
  629. struct rb_node *p;
  630. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  631. cap = rb_entry(p, struct ceph_cap, ci_node);
  632. if (cap == ocap)
  633. continue;
  634. if (!__cap_is_valid(cap))
  635. continue;
  636. have |= cap->issued;
  637. }
  638. return have;
  639. }
  640. /*
  641. * Move a cap to the end of the LRU (oldest caps at list head, newest
  642. * at list tail).
  643. */
  644. static void __touch_cap(struct ceph_cap *cap)
  645. {
  646. struct ceph_mds_session *s = cap->session;
  647. spin_lock(&s->s_cap_lock);
  648. if (s->s_cap_iterator == NULL) {
  649. dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
  650. s->s_mds);
  651. list_move_tail(&cap->session_caps, &s->s_caps);
  652. } else {
  653. dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
  654. &cap->ci->vfs_inode, cap, s->s_mds);
  655. }
  656. spin_unlock(&s->s_cap_lock);
  657. }
  658. /*
  659. * Check if we hold the given mask. If so, move the cap(s) to the
  660. * front of their respective LRUs. (This is the preferred way for
  661. * callers to check for caps they want.)
  662. */
  663. int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
  664. {
  665. struct ceph_cap *cap;
  666. struct rb_node *p;
  667. int have = ci->i_snap_caps;
  668. if ((have & mask) == mask) {
  669. dout("__ceph_caps_issued_mask %p snap issued %s"
  670. " (mask %s)\n", &ci->vfs_inode,
  671. ceph_cap_string(have),
  672. ceph_cap_string(mask));
  673. return 1;
  674. }
  675. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  676. cap = rb_entry(p, struct ceph_cap, ci_node);
  677. if (!__cap_is_valid(cap))
  678. continue;
  679. if ((cap->issued & mask) == mask) {
  680. dout("__ceph_caps_issued_mask %p cap %p issued %s"
  681. " (mask %s)\n", &ci->vfs_inode, cap,
  682. ceph_cap_string(cap->issued),
  683. ceph_cap_string(mask));
  684. if (touch)
  685. __touch_cap(cap);
  686. return 1;
  687. }
  688. /* does a combination of caps satisfy mask? */
  689. have |= cap->issued;
  690. if ((have & mask) == mask) {
  691. dout("__ceph_caps_issued_mask %p combo issued %s"
  692. " (mask %s)\n", &ci->vfs_inode,
  693. ceph_cap_string(cap->issued),
  694. ceph_cap_string(mask));
  695. if (touch) {
  696. struct rb_node *q;
  697. /* touch this + preceding caps */
  698. __touch_cap(cap);
  699. for (q = rb_first(&ci->i_caps); q != p;
  700. q = rb_next(q)) {
  701. cap = rb_entry(q, struct ceph_cap,
  702. ci_node);
  703. if (!__cap_is_valid(cap))
  704. continue;
  705. __touch_cap(cap);
  706. }
  707. }
  708. return 1;
  709. }
  710. }
  711. return 0;
  712. }
  713. /*
  714. * Return true if mask caps are currently being revoked by an MDS.
  715. */
  716. int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
  717. struct ceph_cap *ocap, int mask)
  718. {
  719. struct ceph_cap *cap;
  720. struct rb_node *p;
  721. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  722. cap = rb_entry(p, struct ceph_cap, ci_node);
  723. if (cap != ocap &&
  724. (cap->implemented & ~cap->issued & mask))
  725. return 1;
  726. }
  727. return 0;
  728. }
  729. int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
  730. {
  731. struct inode *inode = &ci->vfs_inode;
  732. int ret;
  733. spin_lock(&ci->i_ceph_lock);
  734. ret = __ceph_caps_revoking_other(ci, NULL, mask);
  735. spin_unlock(&ci->i_ceph_lock);
  736. dout("ceph_caps_revoking %p %s = %d\n", inode,
  737. ceph_cap_string(mask), ret);
  738. return ret;
  739. }
  740. int __ceph_caps_used(struct ceph_inode_info *ci)
  741. {
  742. int used = 0;
  743. if (ci->i_pin_ref)
  744. used |= CEPH_CAP_PIN;
  745. if (ci->i_rd_ref)
  746. used |= CEPH_CAP_FILE_RD;
  747. if (ci->i_rdcache_ref ||
  748. (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
  749. ci->vfs_inode.i_data.nrpages))
  750. used |= CEPH_CAP_FILE_CACHE;
  751. if (ci->i_wr_ref)
  752. used |= CEPH_CAP_FILE_WR;
  753. if (ci->i_wb_ref || ci->i_wrbuffer_ref)
  754. used |= CEPH_CAP_FILE_BUFFER;
  755. return used;
  756. }
  757. /*
  758. * wanted, by virtue of open file modes
  759. */
  760. int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
  761. {
  762. int want = 0;
  763. int mode;
  764. for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
  765. if (ci->i_nr_by_mode[mode])
  766. want |= ceph_caps_for_mode(mode);
  767. return want;
  768. }
  769. /*
  770. * Return caps we have registered with the MDS(s) as 'wanted'.
  771. */
  772. int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
  773. {
  774. struct ceph_cap *cap;
  775. struct rb_node *p;
  776. int mds_wanted = 0;
  777. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  778. cap = rb_entry(p, struct ceph_cap, ci_node);
  779. if (!__cap_is_valid(cap))
  780. continue;
  781. if (cap == ci->i_auth_cap)
  782. mds_wanted |= cap->mds_wanted;
  783. else
  784. mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
  785. }
  786. return mds_wanted;
  787. }
  788. /*
  789. * called under i_ceph_lock
  790. */
  791. static int __ceph_is_any_caps(struct ceph_inode_info *ci)
  792. {
  793. return !RB_EMPTY_ROOT(&ci->i_caps);
  794. }
  795. int ceph_is_any_caps(struct inode *inode)
  796. {
  797. struct ceph_inode_info *ci = ceph_inode(inode);
  798. int ret;
  799. spin_lock(&ci->i_ceph_lock);
  800. ret = __ceph_is_any_caps(ci);
  801. spin_unlock(&ci->i_ceph_lock);
  802. return ret;
  803. }
  804. static void drop_inode_snap_realm(struct ceph_inode_info *ci)
  805. {
  806. struct ceph_snap_realm *realm = ci->i_snap_realm;
  807. spin_lock(&realm->inodes_with_caps_lock);
  808. list_del_init(&ci->i_snap_realm_item);
  809. ci->i_snap_realm_counter++;
  810. ci->i_snap_realm = NULL;
  811. spin_unlock(&realm->inodes_with_caps_lock);
  812. ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
  813. realm);
  814. }
  815. /*
  816. * Remove a cap. Take steps to deal with a racing iterate_session_caps.
  817. *
  818. * caller should hold i_ceph_lock.
  819. * caller will not hold session s_mutex if called from destroy_inode.
  820. */
  821. void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
  822. {
  823. struct ceph_mds_session *session = cap->session;
  824. struct ceph_inode_info *ci = cap->ci;
  825. struct ceph_mds_client *mdsc =
  826. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  827. int removed = 0;
  828. dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
  829. /* remove from session list */
  830. spin_lock(&session->s_cap_lock);
  831. if (session->s_cap_iterator == cap) {
  832. /* not yet, we are iterating over this very cap */
  833. dout("__ceph_remove_cap delaying %p removal from session %p\n",
  834. cap, cap->session);
  835. } else {
  836. list_del_init(&cap->session_caps);
  837. session->s_nr_caps--;
  838. cap->session = NULL;
  839. removed = 1;
  840. }
  841. /* protect backpointer with s_cap_lock: see iterate_session_caps */
  842. cap->ci = NULL;
  843. /*
  844. * s_cap_reconnect is protected by s_cap_lock. no one changes
  845. * s_cap_gen while session is in the reconnect state.
  846. */
  847. if (queue_release &&
  848. (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
  849. cap->queue_release = 1;
  850. if (removed) {
  851. list_add_tail(&cap->session_caps,
  852. &session->s_cap_releases);
  853. session->s_num_cap_releases++;
  854. removed = 0;
  855. }
  856. } else {
  857. cap->queue_release = 0;
  858. }
  859. cap->cap_ino = ci->i_vino.ino;
  860. spin_unlock(&session->s_cap_lock);
  861. /* remove from inode list */
  862. rb_erase(&cap->ci_node, &ci->i_caps);
  863. if (ci->i_auth_cap == cap)
  864. ci->i_auth_cap = NULL;
  865. if (removed)
  866. ceph_put_cap(mdsc, cap);
  867. /* when reconnect denied, we remove session caps forcibly,
  868. * i_wr_ref can be non-zero. If there are ongoing write,
  869. * keep i_snap_realm.
  870. */
  871. if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
  872. drop_inode_snap_realm(ci);
  873. if (!__ceph_is_any_real_caps(ci))
  874. __cap_delay_cancel(mdsc, ci);
  875. }
  876. /*
  877. * Build and send a cap message to the given MDS.
  878. *
  879. * Caller should be holding s_mutex.
  880. */
  881. static int send_cap_msg(struct ceph_mds_session *session,
  882. u64 ino, u64 cid, int op,
  883. int caps, int wanted, int dirty,
  884. u32 seq, u64 flush_tid, u64 oldest_flush_tid,
  885. u32 issue_seq, u32 mseq, u64 size, u64 max_size,
  886. struct timespec *mtime, struct timespec *atime,
  887. u64 time_warp_seq,
  888. kuid_t uid, kgid_t gid, umode_t mode,
  889. u64 xattr_version,
  890. struct ceph_buffer *xattrs_buf,
  891. u64 follows, bool inline_data)
  892. {
  893. struct ceph_mds_caps *fc;
  894. struct ceph_msg *msg;
  895. void *p;
  896. size_t extra_len;
  897. dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
  898. " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
  899. " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
  900. cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
  901. ceph_cap_string(dirty),
  902. seq, issue_seq, flush_tid, oldest_flush_tid,
  903. mseq, follows, size, max_size,
  904. xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
  905. /* flock buffer size + inline version + inline data size +
  906. * osd_epoch_barrier + oldest_flush_tid */
  907. extra_len = 4 + 8 + 4 + 4 + 8;
  908. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
  909. GFP_NOFS, false);
  910. if (!msg)
  911. return -ENOMEM;
  912. msg->hdr.version = cpu_to_le16(6);
  913. msg->hdr.tid = cpu_to_le64(flush_tid);
  914. fc = msg->front.iov_base;
  915. memset(fc, 0, sizeof(*fc));
  916. fc->cap_id = cpu_to_le64(cid);
  917. fc->op = cpu_to_le32(op);
  918. fc->seq = cpu_to_le32(seq);
  919. fc->issue_seq = cpu_to_le32(issue_seq);
  920. fc->migrate_seq = cpu_to_le32(mseq);
  921. fc->caps = cpu_to_le32(caps);
  922. fc->wanted = cpu_to_le32(wanted);
  923. fc->dirty = cpu_to_le32(dirty);
  924. fc->ino = cpu_to_le64(ino);
  925. fc->snap_follows = cpu_to_le64(follows);
  926. fc->size = cpu_to_le64(size);
  927. fc->max_size = cpu_to_le64(max_size);
  928. if (mtime)
  929. ceph_encode_timespec(&fc->mtime, mtime);
  930. if (atime)
  931. ceph_encode_timespec(&fc->atime, atime);
  932. fc->time_warp_seq = cpu_to_le32(time_warp_seq);
  933. fc->uid = cpu_to_le32(from_kuid(&init_user_ns, uid));
  934. fc->gid = cpu_to_le32(from_kgid(&init_user_ns, gid));
  935. fc->mode = cpu_to_le32(mode);
  936. p = fc + 1;
  937. /* flock buffer size */
  938. ceph_encode_32(&p, 0);
  939. /* inline version */
  940. ceph_encode_64(&p, inline_data ? 0 : CEPH_INLINE_NONE);
  941. /* inline data size */
  942. ceph_encode_32(&p, 0);
  943. /* osd_epoch_barrier */
  944. ceph_encode_32(&p, 0);
  945. /* oldest_flush_tid */
  946. ceph_encode_64(&p, oldest_flush_tid);
  947. fc->xattr_version = cpu_to_le64(xattr_version);
  948. if (xattrs_buf) {
  949. msg->middle = ceph_buffer_get(xattrs_buf);
  950. fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
  951. msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
  952. }
  953. ceph_con_send(&session->s_con, msg);
  954. return 0;
  955. }
  956. /*
  957. * Queue cap releases when an inode is dropped from our cache. Since
  958. * inode is about to be destroyed, there is no need for i_ceph_lock.
  959. */
  960. void ceph_queue_caps_release(struct inode *inode)
  961. {
  962. struct ceph_inode_info *ci = ceph_inode(inode);
  963. struct rb_node *p;
  964. p = rb_first(&ci->i_caps);
  965. while (p) {
  966. struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
  967. p = rb_next(p);
  968. __ceph_remove_cap(cap, true);
  969. }
  970. }
  971. /*
  972. * Send a cap msg on the given inode. Update our caps state, then
  973. * drop i_ceph_lock and send the message.
  974. *
  975. * Make note of max_size reported/requested from mds, revoked caps
  976. * that have now been implemented.
  977. *
  978. * Make half-hearted attempt ot to invalidate page cache if we are
  979. * dropping RDCACHE. Note that this will leave behind locked pages
  980. * that we'll then need to deal with elsewhere.
  981. *
  982. * Return non-zero if delayed release, or we experienced an error
  983. * such that the caller should requeue + retry later.
  984. *
  985. * called with i_ceph_lock, then drops it.
  986. * caller should hold snap_rwsem (read), s_mutex.
  987. */
  988. static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
  989. int op, int used, int want, int retain, int flushing,
  990. u64 flush_tid, u64 oldest_flush_tid)
  991. __releases(cap->ci->i_ceph_lock)
  992. {
  993. struct ceph_inode_info *ci = cap->ci;
  994. struct inode *inode = &ci->vfs_inode;
  995. u64 cap_id = cap->cap_id;
  996. int held, revoking, dropping, keep;
  997. u64 seq, issue_seq, mseq, time_warp_seq, follows;
  998. u64 size, max_size;
  999. struct timespec mtime, atime;
  1000. int wake = 0;
  1001. umode_t mode;
  1002. kuid_t uid;
  1003. kgid_t gid;
  1004. struct ceph_mds_session *session;
  1005. u64 xattr_version = 0;
  1006. struct ceph_buffer *xattr_blob = NULL;
  1007. int delayed = 0;
  1008. int ret;
  1009. bool inline_data;
  1010. held = cap->issued | cap->implemented;
  1011. revoking = cap->implemented & ~cap->issued;
  1012. retain &= ~revoking;
  1013. dropping = cap->issued & ~retain;
  1014. dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
  1015. inode, cap, cap->session,
  1016. ceph_cap_string(held), ceph_cap_string(held & retain),
  1017. ceph_cap_string(revoking));
  1018. BUG_ON((retain & CEPH_CAP_PIN) == 0);
  1019. session = cap->session;
  1020. /* don't release wanted unless we've waited a bit. */
  1021. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  1022. time_before(jiffies, ci->i_hold_caps_min)) {
  1023. dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
  1024. ceph_cap_string(cap->issued),
  1025. ceph_cap_string(cap->issued & retain),
  1026. ceph_cap_string(cap->mds_wanted),
  1027. ceph_cap_string(want));
  1028. want |= cap->mds_wanted;
  1029. retain |= cap->issued;
  1030. delayed = 1;
  1031. }
  1032. ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
  1033. cap->issued &= retain; /* drop bits we don't want */
  1034. if (cap->implemented & ~cap->issued) {
  1035. /*
  1036. * Wake up any waiters on wanted -> needed transition.
  1037. * This is due to the weird transition from buffered
  1038. * to sync IO... we need to flush dirty pages _before_
  1039. * allowing sync writes to avoid reordering.
  1040. */
  1041. wake = 1;
  1042. }
  1043. cap->implemented &= cap->issued | used;
  1044. cap->mds_wanted = want;
  1045. follows = flushing ? ci->i_head_snapc->seq : 0;
  1046. keep = cap->implemented;
  1047. seq = cap->seq;
  1048. issue_seq = cap->issue_seq;
  1049. mseq = cap->mseq;
  1050. size = inode->i_size;
  1051. ci->i_reported_size = size;
  1052. max_size = ci->i_wanted_max_size;
  1053. ci->i_requested_max_size = max_size;
  1054. mtime = inode->i_mtime;
  1055. atime = inode->i_atime;
  1056. time_warp_seq = ci->i_time_warp_seq;
  1057. uid = inode->i_uid;
  1058. gid = inode->i_gid;
  1059. mode = inode->i_mode;
  1060. if (flushing & CEPH_CAP_XATTR_EXCL) {
  1061. __ceph_build_xattrs_blob(ci);
  1062. xattr_blob = ci->i_xattrs.blob;
  1063. xattr_version = ci->i_xattrs.version;
  1064. }
  1065. inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
  1066. spin_unlock(&ci->i_ceph_lock);
  1067. ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
  1068. op, keep, want, flushing, seq,
  1069. flush_tid, oldest_flush_tid, issue_seq, mseq,
  1070. size, max_size, &mtime, &atime, time_warp_seq,
  1071. uid, gid, mode, xattr_version, xattr_blob,
  1072. follows, inline_data);
  1073. if (ret < 0) {
  1074. dout("error sending cap msg, must requeue %p\n", inode);
  1075. delayed = 1;
  1076. }
  1077. if (wake)
  1078. wake_up_all(&ci->i_cap_wq);
  1079. return delayed;
  1080. }
  1081. /*
  1082. * When a snapshot is taken, clients accumulate dirty metadata on
  1083. * inodes with capabilities in ceph_cap_snaps to describe the file
  1084. * state at the time the snapshot was taken. This must be flushed
  1085. * asynchronously back to the MDS once sync writes complete and dirty
  1086. * data is written out.
  1087. *
  1088. * Unless @kick is true, skip cap_snaps that were already sent to
  1089. * the MDS (i.e., during this session).
  1090. *
  1091. * Called under i_ceph_lock. Takes s_mutex as needed.
  1092. */
  1093. void __ceph_flush_snaps(struct ceph_inode_info *ci,
  1094. struct ceph_mds_session **psession,
  1095. int kick)
  1096. __releases(ci->i_ceph_lock)
  1097. __acquires(ci->i_ceph_lock)
  1098. {
  1099. struct inode *inode = &ci->vfs_inode;
  1100. int mds;
  1101. struct ceph_cap_snap *capsnap;
  1102. u32 mseq;
  1103. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  1104. struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
  1105. session->s_mutex */
  1106. u64 next_follows = 0; /* keep track of how far we've gotten through the
  1107. i_cap_snaps list, and skip these entries next time
  1108. around to avoid an infinite loop */
  1109. if (psession)
  1110. session = *psession;
  1111. dout("__flush_snaps %p\n", inode);
  1112. retry:
  1113. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1114. /* avoid an infiniute loop after retry */
  1115. if (capsnap->follows < next_follows)
  1116. continue;
  1117. /*
  1118. * we need to wait for sync writes to complete and for dirty
  1119. * pages to be written out.
  1120. */
  1121. if (capsnap->dirty_pages || capsnap->writing)
  1122. break;
  1123. /* should be removed by ceph_try_drop_cap_snap() */
  1124. BUG_ON(!capsnap->need_flush);
  1125. /* pick mds, take s_mutex */
  1126. if (ci->i_auth_cap == NULL) {
  1127. dout("no auth cap (migrating?), doing nothing\n");
  1128. goto out;
  1129. }
  1130. /* only flush each capsnap once */
  1131. if (!kick && !list_empty(&capsnap->flushing_item)) {
  1132. dout("already flushed %p, skipping\n", capsnap);
  1133. continue;
  1134. }
  1135. mds = ci->i_auth_cap->session->s_mds;
  1136. mseq = ci->i_auth_cap->mseq;
  1137. if (session && session->s_mds != mds) {
  1138. dout("oops, wrong session %p mutex\n", session);
  1139. if (kick)
  1140. goto out;
  1141. mutex_unlock(&session->s_mutex);
  1142. ceph_put_mds_session(session);
  1143. session = NULL;
  1144. }
  1145. if (!session) {
  1146. spin_unlock(&ci->i_ceph_lock);
  1147. mutex_lock(&mdsc->mutex);
  1148. session = __ceph_lookup_mds_session(mdsc, mds);
  1149. mutex_unlock(&mdsc->mutex);
  1150. if (session) {
  1151. dout("inverting session/ino locks on %p\n",
  1152. session);
  1153. mutex_lock(&session->s_mutex);
  1154. }
  1155. /*
  1156. * if session == NULL, we raced against a cap
  1157. * deletion or migration. retry, and we'll
  1158. * get a better @mds value next time.
  1159. */
  1160. spin_lock(&ci->i_ceph_lock);
  1161. goto retry;
  1162. }
  1163. spin_lock(&mdsc->cap_dirty_lock);
  1164. capsnap->flush_tid = ++mdsc->last_cap_flush_tid;
  1165. spin_unlock(&mdsc->cap_dirty_lock);
  1166. atomic_inc(&capsnap->nref);
  1167. if (list_empty(&capsnap->flushing_item))
  1168. list_add_tail(&capsnap->flushing_item,
  1169. &session->s_cap_snaps_flushing);
  1170. spin_unlock(&ci->i_ceph_lock);
  1171. dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
  1172. inode, capsnap, capsnap->follows, capsnap->flush_tid);
  1173. send_cap_msg(session, ceph_vino(inode).ino, 0,
  1174. CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
  1175. capsnap->dirty, 0, capsnap->flush_tid, 0,
  1176. 0, mseq, capsnap->size, 0,
  1177. &capsnap->mtime, &capsnap->atime,
  1178. capsnap->time_warp_seq,
  1179. capsnap->uid, capsnap->gid, capsnap->mode,
  1180. capsnap->xattr_version, capsnap->xattr_blob,
  1181. capsnap->follows, capsnap->inline_data);
  1182. next_follows = capsnap->follows + 1;
  1183. ceph_put_cap_snap(capsnap);
  1184. spin_lock(&ci->i_ceph_lock);
  1185. goto retry;
  1186. }
  1187. /* we flushed them all; remove this inode from the queue */
  1188. spin_lock(&mdsc->snap_flush_lock);
  1189. list_del_init(&ci->i_snap_flush_item);
  1190. spin_unlock(&mdsc->snap_flush_lock);
  1191. out:
  1192. if (psession)
  1193. *psession = session;
  1194. else if (session) {
  1195. mutex_unlock(&session->s_mutex);
  1196. ceph_put_mds_session(session);
  1197. }
  1198. }
  1199. static void ceph_flush_snaps(struct ceph_inode_info *ci)
  1200. {
  1201. spin_lock(&ci->i_ceph_lock);
  1202. __ceph_flush_snaps(ci, NULL, 0);
  1203. spin_unlock(&ci->i_ceph_lock);
  1204. }
  1205. /*
  1206. * Mark caps dirty. If inode is newly dirty, return the dirty flags.
  1207. * Caller is then responsible for calling __mark_inode_dirty with the
  1208. * returned flags value.
  1209. */
  1210. int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
  1211. struct ceph_cap_flush **pcf)
  1212. {
  1213. struct ceph_mds_client *mdsc =
  1214. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  1215. struct inode *inode = &ci->vfs_inode;
  1216. int was = ci->i_dirty_caps;
  1217. int dirty = 0;
  1218. if (!ci->i_auth_cap) {
  1219. pr_warn("__mark_dirty_caps %p %llx mask %s, "
  1220. "but no auth cap (session was closed?)\n",
  1221. inode, ceph_ino(inode), ceph_cap_string(mask));
  1222. return 0;
  1223. }
  1224. dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
  1225. ceph_cap_string(mask), ceph_cap_string(was),
  1226. ceph_cap_string(was | mask));
  1227. ci->i_dirty_caps |= mask;
  1228. if (was == 0) {
  1229. WARN_ON_ONCE(ci->i_prealloc_cap_flush);
  1230. swap(ci->i_prealloc_cap_flush, *pcf);
  1231. if (!ci->i_head_snapc) {
  1232. WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
  1233. ci->i_head_snapc = ceph_get_snap_context(
  1234. ci->i_snap_realm->cached_context);
  1235. }
  1236. dout(" inode %p now dirty snapc %p auth cap %p\n",
  1237. &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
  1238. BUG_ON(!list_empty(&ci->i_dirty_item));
  1239. spin_lock(&mdsc->cap_dirty_lock);
  1240. list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
  1241. spin_unlock(&mdsc->cap_dirty_lock);
  1242. if (ci->i_flushing_caps == 0) {
  1243. ihold(inode);
  1244. dirty |= I_DIRTY_SYNC;
  1245. }
  1246. } else {
  1247. WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
  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. static void __add_cap_flushing_to_inode(struct ceph_inode_info *ci,
  1257. struct ceph_cap_flush *cf)
  1258. {
  1259. struct rb_node **p = &ci->i_cap_flush_tree.rb_node;
  1260. struct rb_node *parent = NULL;
  1261. struct ceph_cap_flush *other = NULL;
  1262. while (*p) {
  1263. parent = *p;
  1264. other = rb_entry(parent, struct ceph_cap_flush, i_node);
  1265. if (cf->tid < other->tid)
  1266. p = &(*p)->rb_left;
  1267. else if (cf->tid > other->tid)
  1268. p = &(*p)->rb_right;
  1269. else
  1270. BUG();
  1271. }
  1272. rb_link_node(&cf->i_node, parent, p);
  1273. rb_insert_color(&cf->i_node, &ci->i_cap_flush_tree);
  1274. }
  1275. static void __add_cap_flushing_to_mdsc(struct ceph_mds_client *mdsc,
  1276. struct ceph_cap_flush *cf)
  1277. {
  1278. struct rb_node **p = &mdsc->cap_flush_tree.rb_node;
  1279. struct rb_node *parent = NULL;
  1280. struct ceph_cap_flush *other = NULL;
  1281. while (*p) {
  1282. parent = *p;
  1283. other = rb_entry(parent, struct ceph_cap_flush, g_node);
  1284. if (cf->tid < other->tid)
  1285. p = &(*p)->rb_left;
  1286. else if (cf->tid > other->tid)
  1287. p = &(*p)->rb_right;
  1288. else
  1289. BUG();
  1290. }
  1291. rb_link_node(&cf->g_node, parent, p);
  1292. rb_insert_color(&cf->g_node, &mdsc->cap_flush_tree);
  1293. }
  1294. struct ceph_cap_flush *ceph_alloc_cap_flush(void)
  1295. {
  1296. return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
  1297. }
  1298. void ceph_free_cap_flush(struct ceph_cap_flush *cf)
  1299. {
  1300. if (cf)
  1301. kmem_cache_free(ceph_cap_flush_cachep, cf);
  1302. }
  1303. static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
  1304. {
  1305. struct rb_node *n = rb_first(&mdsc->cap_flush_tree);
  1306. if (n) {
  1307. struct ceph_cap_flush *cf =
  1308. rb_entry(n, struct ceph_cap_flush, g_node);
  1309. return cf->tid;
  1310. }
  1311. return 0;
  1312. }
  1313. /*
  1314. * Add dirty inode to the flushing list. Assigned a seq number so we
  1315. * can wait for caps to flush without starving.
  1316. *
  1317. * Called under i_ceph_lock.
  1318. */
  1319. static int __mark_caps_flushing(struct inode *inode,
  1320. struct ceph_mds_session *session,
  1321. u64 *flush_tid, u64 *oldest_flush_tid)
  1322. {
  1323. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1324. struct ceph_inode_info *ci = ceph_inode(inode);
  1325. struct ceph_cap_flush *cf = NULL;
  1326. int flushing;
  1327. BUG_ON(ci->i_dirty_caps == 0);
  1328. BUG_ON(list_empty(&ci->i_dirty_item));
  1329. BUG_ON(!ci->i_prealloc_cap_flush);
  1330. flushing = ci->i_dirty_caps;
  1331. dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
  1332. ceph_cap_string(flushing),
  1333. ceph_cap_string(ci->i_flushing_caps),
  1334. ceph_cap_string(ci->i_flushing_caps | flushing));
  1335. ci->i_flushing_caps |= flushing;
  1336. ci->i_dirty_caps = 0;
  1337. dout(" inode %p now !dirty\n", inode);
  1338. swap(cf, ci->i_prealloc_cap_flush);
  1339. cf->caps = flushing;
  1340. spin_lock(&mdsc->cap_dirty_lock);
  1341. list_del_init(&ci->i_dirty_item);
  1342. cf->tid = ++mdsc->last_cap_flush_tid;
  1343. __add_cap_flushing_to_mdsc(mdsc, cf);
  1344. *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1345. if (list_empty(&ci->i_flushing_item)) {
  1346. list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1347. mdsc->num_cap_flushing++;
  1348. dout(" inode %p now flushing tid %llu\n", inode, cf->tid);
  1349. } else {
  1350. list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1351. dout(" inode %p now flushing (more) tid %llu\n",
  1352. inode, cf->tid);
  1353. }
  1354. spin_unlock(&mdsc->cap_dirty_lock);
  1355. __add_cap_flushing_to_inode(ci, cf);
  1356. *flush_tid = cf->tid;
  1357. return flushing;
  1358. }
  1359. /*
  1360. * try to invalidate mapping pages without blocking.
  1361. */
  1362. static int try_nonblocking_invalidate(struct inode *inode)
  1363. {
  1364. struct ceph_inode_info *ci = ceph_inode(inode);
  1365. u32 invalidating_gen = ci->i_rdcache_gen;
  1366. spin_unlock(&ci->i_ceph_lock);
  1367. invalidate_mapping_pages(&inode->i_data, 0, -1);
  1368. spin_lock(&ci->i_ceph_lock);
  1369. if (inode->i_data.nrpages == 0 &&
  1370. invalidating_gen == ci->i_rdcache_gen) {
  1371. /* success. */
  1372. dout("try_nonblocking_invalidate %p success\n", inode);
  1373. /* save any racing async invalidate some trouble */
  1374. ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
  1375. return 0;
  1376. }
  1377. dout("try_nonblocking_invalidate %p failed\n", inode);
  1378. return -1;
  1379. }
  1380. /*
  1381. * Swiss army knife function to examine currently used and wanted
  1382. * versus held caps. Release, flush, ack revoked caps to mds as
  1383. * appropriate.
  1384. *
  1385. * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
  1386. * cap release further.
  1387. * CHECK_CAPS_AUTHONLY - we should only check the auth cap
  1388. * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
  1389. * further delay.
  1390. */
  1391. void ceph_check_caps(struct ceph_inode_info *ci, int flags,
  1392. struct ceph_mds_session *session)
  1393. {
  1394. struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
  1395. struct ceph_mds_client *mdsc = fsc->mdsc;
  1396. struct inode *inode = &ci->vfs_inode;
  1397. struct ceph_cap *cap;
  1398. u64 flush_tid, oldest_flush_tid;
  1399. int file_wanted, used, cap_used;
  1400. int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
  1401. int issued, implemented, want, retain, revoking, flushing = 0;
  1402. int mds = -1; /* keep track of how far we've gone through i_caps list
  1403. to avoid an infinite loop on retry */
  1404. struct rb_node *p;
  1405. int tried_invalidate = 0;
  1406. int delayed = 0, sent = 0, force_requeue = 0, num;
  1407. int queue_invalidate = 0;
  1408. int is_delayed = flags & CHECK_CAPS_NODELAY;
  1409. /* if we are unmounting, flush any unused caps immediately. */
  1410. if (mdsc->stopping)
  1411. is_delayed = 1;
  1412. spin_lock(&ci->i_ceph_lock);
  1413. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  1414. flags |= CHECK_CAPS_FLUSH;
  1415. /* flush snaps first time around only */
  1416. if (!list_empty(&ci->i_cap_snaps))
  1417. __ceph_flush_snaps(ci, &session, 0);
  1418. goto retry_locked;
  1419. retry:
  1420. spin_lock(&ci->i_ceph_lock);
  1421. retry_locked:
  1422. file_wanted = __ceph_caps_file_wanted(ci);
  1423. used = __ceph_caps_used(ci);
  1424. issued = __ceph_caps_issued(ci, &implemented);
  1425. revoking = implemented & ~issued;
  1426. want = file_wanted;
  1427. retain = file_wanted | used | CEPH_CAP_PIN;
  1428. if (!mdsc->stopping && inode->i_nlink > 0) {
  1429. if (file_wanted) {
  1430. retain |= CEPH_CAP_ANY; /* be greedy */
  1431. } else if (S_ISDIR(inode->i_mode) &&
  1432. (issued & CEPH_CAP_FILE_SHARED) &&
  1433. __ceph_dir_is_complete(ci)) {
  1434. /*
  1435. * If a directory is complete, we want to keep
  1436. * the exclusive cap. So that MDS does not end up
  1437. * revoking the shared cap on every create/unlink
  1438. * operation.
  1439. */
  1440. want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
  1441. retain |= want;
  1442. } else {
  1443. retain |= CEPH_CAP_ANY_SHARED;
  1444. /*
  1445. * keep RD only if we didn't have the file open RW,
  1446. * because then the mds would revoke it anyway to
  1447. * journal max_size=0.
  1448. */
  1449. if (ci->i_max_size == 0)
  1450. retain |= CEPH_CAP_ANY_RD;
  1451. }
  1452. }
  1453. dout("check_caps %p file_want %s used %s dirty %s flushing %s"
  1454. " issued %s revoking %s retain %s %s%s%s\n", inode,
  1455. ceph_cap_string(file_wanted),
  1456. ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
  1457. ceph_cap_string(ci->i_flushing_caps),
  1458. ceph_cap_string(issued), ceph_cap_string(revoking),
  1459. ceph_cap_string(retain),
  1460. (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
  1461. (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
  1462. (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
  1463. /*
  1464. * If we no longer need to hold onto old our caps, and we may
  1465. * have cached pages, but don't want them, then try to invalidate.
  1466. * If we fail, it's because pages are locked.... try again later.
  1467. */
  1468. if ((!is_delayed || mdsc->stopping) &&
  1469. !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
  1470. ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
  1471. inode->i_data.nrpages && /* have cached pages */
  1472. (revoking & (CEPH_CAP_FILE_CACHE|
  1473. CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
  1474. !tried_invalidate) {
  1475. dout("check_caps trying to invalidate on %p\n", inode);
  1476. if (try_nonblocking_invalidate(inode) < 0) {
  1477. if (revoking & (CEPH_CAP_FILE_CACHE|
  1478. CEPH_CAP_FILE_LAZYIO)) {
  1479. dout("check_caps queuing invalidate\n");
  1480. queue_invalidate = 1;
  1481. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  1482. } else {
  1483. dout("check_caps failed to invalidate pages\n");
  1484. /* we failed to invalidate pages. check these
  1485. caps again later. */
  1486. force_requeue = 1;
  1487. __cap_set_timeouts(mdsc, ci);
  1488. }
  1489. }
  1490. tried_invalidate = 1;
  1491. goto retry_locked;
  1492. }
  1493. num = 0;
  1494. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  1495. cap = rb_entry(p, struct ceph_cap, ci_node);
  1496. num++;
  1497. /* avoid looping forever */
  1498. if (mds >= cap->mds ||
  1499. ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
  1500. continue;
  1501. /* NOTE: no side-effects allowed, until we take s_mutex */
  1502. cap_used = used;
  1503. if (ci->i_auth_cap && cap != ci->i_auth_cap)
  1504. cap_used &= ~ci->i_auth_cap->issued;
  1505. revoking = cap->implemented & ~cap->issued;
  1506. dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
  1507. cap->mds, cap, ceph_cap_string(cap->issued),
  1508. ceph_cap_string(cap_used),
  1509. ceph_cap_string(cap->implemented),
  1510. ceph_cap_string(revoking));
  1511. if (cap == ci->i_auth_cap &&
  1512. (cap->issued & CEPH_CAP_FILE_WR)) {
  1513. /* request larger max_size from MDS? */
  1514. if (ci->i_wanted_max_size > ci->i_max_size &&
  1515. ci->i_wanted_max_size > ci->i_requested_max_size) {
  1516. dout("requesting new max_size\n");
  1517. goto ack;
  1518. }
  1519. /* approaching file_max? */
  1520. if ((inode->i_size << 1) >= ci->i_max_size &&
  1521. (ci->i_reported_size << 1) < ci->i_max_size) {
  1522. dout("i_size approaching max_size\n");
  1523. goto ack;
  1524. }
  1525. }
  1526. /* flush anything dirty? */
  1527. if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
  1528. ci->i_dirty_caps) {
  1529. dout("flushing dirty caps\n");
  1530. goto ack;
  1531. }
  1532. /* completed revocation? going down and there are no caps? */
  1533. if (revoking && (revoking & cap_used) == 0) {
  1534. dout("completed revocation of %s\n",
  1535. ceph_cap_string(cap->implemented & ~cap->issued));
  1536. goto ack;
  1537. }
  1538. /* want more caps from mds? */
  1539. if (want & ~(cap->mds_wanted | cap->issued))
  1540. goto ack;
  1541. /* things we might delay */
  1542. if ((cap->issued & ~retain) == 0 &&
  1543. cap->mds_wanted == want)
  1544. continue; /* nope, all good */
  1545. if (is_delayed)
  1546. goto ack;
  1547. /* delay? */
  1548. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  1549. time_before(jiffies, ci->i_hold_caps_max)) {
  1550. dout(" delaying issued %s -> %s, wanted %s -> %s\n",
  1551. ceph_cap_string(cap->issued),
  1552. ceph_cap_string(cap->issued & retain),
  1553. ceph_cap_string(cap->mds_wanted),
  1554. ceph_cap_string(want));
  1555. delayed++;
  1556. continue;
  1557. }
  1558. ack:
  1559. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1560. dout(" skipping %p I_NOFLUSH set\n", inode);
  1561. continue;
  1562. }
  1563. if (session && session != cap->session) {
  1564. dout("oops, wrong session %p mutex\n", session);
  1565. mutex_unlock(&session->s_mutex);
  1566. session = NULL;
  1567. }
  1568. if (!session) {
  1569. session = cap->session;
  1570. if (mutex_trylock(&session->s_mutex) == 0) {
  1571. dout("inverting session/ino locks on %p\n",
  1572. session);
  1573. spin_unlock(&ci->i_ceph_lock);
  1574. if (took_snap_rwsem) {
  1575. up_read(&mdsc->snap_rwsem);
  1576. took_snap_rwsem = 0;
  1577. }
  1578. mutex_lock(&session->s_mutex);
  1579. goto retry;
  1580. }
  1581. }
  1582. /* take snap_rwsem after session mutex */
  1583. if (!took_snap_rwsem) {
  1584. if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
  1585. dout("inverting snap/in locks on %p\n",
  1586. inode);
  1587. spin_unlock(&ci->i_ceph_lock);
  1588. down_read(&mdsc->snap_rwsem);
  1589. took_snap_rwsem = 1;
  1590. goto retry;
  1591. }
  1592. took_snap_rwsem = 1;
  1593. }
  1594. if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
  1595. flushing = __mark_caps_flushing(inode, session,
  1596. &flush_tid,
  1597. &oldest_flush_tid);
  1598. } else {
  1599. flushing = 0;
  1600. flush_tid = 0;
  1601. spin_lock(&mdsc->cap_dirty_lock);
  1602. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1603. spin_unlock(&mdsc->cap_dirty_lock);
  1604. }
  1605. mds = cap->mds; /* remember mds, so we don't repeat */
  1606. sent++;
  1607. /* __send_cap drops i_ceph_lock */
  1608. delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, cap_used,
  1609. want, retain, flushing,
  1610. flush_tid, oldest_flush_tid);
  1611. goto retry; /* retake i_ceph_lock and restart our cap scan. */
  1612. }
  1613. /*
  1614. * Reschedule delayed caps release if we delayed anything,
  1615. * otherwise cancel.
  1616. */
  1617. if (delayed && is_delayed)
  1618. force_requeue = 1; /* __send_cap delayed release; requeue */
  1619. if (!delayed && !is_delayed)
  1620. __cap_delay_cancel(mdsc, ci);
  1621. else if (!is_delayed || force_requeue)
  1622. __cap_delay_requeue(mdsc, ci);
  1623. spin_unlock(&ci->i_ceph_lock);
  1624. if (queue_invalidate)
  1625. ceph_queue_invalidate(inode);
  1626. if (session)
  1627. mutex_unlock(&session->s_mutex);
  1628. if (took_snap_rwsem)
  1629. up_read(&mdsc->snap_rwsem);
  1630. }
  1631. /*
  1632. * Try to flush dirty caps back to the auth mds.
  1633. */
  1634. static int try_flush_caps(struct inode *inode, u64 *ptid)
  1635. {
  1636. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1637. struct ceph_inode_info *ci = ceph_inode(inode);
  1638. struct ceph_mds_session *session = NULL;
  1639. int flushing = 0;
  1640. u64 flush_tid = 0, oldest_flush_tid = 0;
  1641. retry:
  1642. spin_lock(&ci->i_ceph_lock);
  1643. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1644. dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
  1645. goto out;
  1646. }
  1647. if (ci->i_dirty_caps && ci->i_auth_cap) {
  1648. struct ceph_cap *cap = ci->i_auth_cap;
  1649. int used = __ceph_caps_used(ci);
  1650. int want = __ceph_caps_wanted(ci);
  1651. int delayed;
  1652. if (!session || session != cap->session) {
  1653. spin_unlock(&ci->i_ceph_lock);
  1654. if (session)
  1655. mutex_unlock(&session->s_mutex);
  1656. session = cap->session;
  1657. mutex_lock(&session->s_mutex);
  1658. goto retry;
  1659. }
  1660. if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
  1661. goto out;
  1662. flushing = __mark_caps_flushing(inode, session, &flush_tid,
  1663. &oldest_flush_tid);
  1664. /* __send_cap drops i_ceph_lock */
  1665. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
  1666. (cap->issued | cap->implemented),
  1667. flushing, flush_tid, oldest_flush_tid);
  1668. if (delayed) {
  1669. spin_lock(&ci->i_ceph_lock);
  1670. __cap_delay_requeue(mdsc, ci);
  1671. spin_unlock(&ci->i_ceph_lock);
  1672. }
  1673. } else {
  1674. struct rb_node *n = rb_last(&ci->i_cap_flush_tree);
  1675. if (n) {
  1676. struct ceph_cap_flush *cf =
  1677. rb_entry(n, struct ceph_cap_flush, i_node);
  1678. flush_tid = cf->tid;
  1679. }
  1680. flushing = ci->i_flushing_caps;
  1681. spin_unlock(&ci->i_ceph_lock);
  1682. }
  1683. out:
  1684. if (session)
  1685. mutex_unlock(&session->s_mutex);
  1686. *ptid = flush_tid;
  1687. return flushing;
  1688. }
  1689. /*
  1690. * Return true if we've flushed caps through the given flush_tid.
  1691. */
  1692. static int caps_are_flushed(struct inode *inode, u64 flush_tid)
  1693. {
  1694. struct ceph_inode_info *ci = ceph_inode(inode);
  1695. struct ceph_cap_flush *cf;
  1696. struct rb_node *n;
  1697. int ret = 1;
  1698. spin_lock(&ci->i_ceph_lock);
  1699. n = rb_first(&ci->i_cap_flush_tree);
  1700. if (n) {
  1701. cf = rb_entry(n, struct ceph_cap_flush, i_node);
  1702. if (cf->tid <= flush_tid)
  1703. ret = 0;
  1704. }
  1705. spin_unlock(&ci->i_ceph_lock);
  1706. return ret;
  1707. }
  1708. /*
  1709. * Wait on any unsafe replies for the given inode. First wait on the
  1710. * newest request, and make that the upper bound. Then, if there are
  1711. * more requests, keep waiting on the oldest as long as it is still older
  1712. * than the original request.
  1713. */
  1714. static void sync_write_wait(struct inode *inode)
  1715. {
  1716. struct ceph_inode_info *ci = ceph_inode(inode);
  1717. struct list_head *head = &ci->i_unsafe_writes;
  1718. struct ceph_osd_request *req;
  1719. u64 last_tid;
  1720. if (!S_ISREG(inode->i_mode))
  1721. return;
  1722. spin_lock(&ci->i_unsafe_lock);
  1723. if (list_empty(head))
  1724. goto out;
  1725. /* set upper bound as _last_ entry in chain */
  1726. req = list_last_entry(head, struct ceph_osd_request,
  1727. r_unsafe_item);
  1728. last_tid = req->r_tid;
  1729. do {
  1730. ceph_osdc_get_request(req);
  1731. spin_unlock(&ci->i_unsafe_lock);
  1732. dout("sync_write_wait on tid %llu (until %llu)\n",
  1733. req->r_tid, last_tid);
  1734. wait_for_completion(&req->r_safe_completion);
  1735. spin_lock(&ci->i_unsafe_lock);
  1736. ceph_osdc_put_request(req);
  1737. /*
  1738. * from here on look at first entry in chain, since we
  1739. * only want to wait for anything older than last_tid
  1740. */
  1741. if (list_empty(head))
  1742. break;
  1743. req = list_first_entry(head, struct ceph_osd_request,
  1744. r_unsafe_item);
  1745. } while (req->r_tid < last_tid);
  1746. out:
  1747. spin_unlock(&ci->i_unsafe_lock);
  1748. }
  1749. /*
  1750. * wait for any unsafe requests to complete.
  1751. */
  1752. static int unsafe_request_wait(struct inode *inode)
  1753. {
  1754. struct ceph_inode_info *ci = ceph_inode(inode);
  1755. struct ceph_mds_request *req1 = NULL, *req2 = NULL;
  1756. int ret, err = 0;
  1757. spin_lock(&ci->i_unsafe_lock);
  1758. if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
  1759. req1 = list_last_entry(&ci->i_unsafe_dirops,
  1760. struct ceph_mds_request,
  1761. r_unsafe_dir_item);
  1762. ceph_mdsc_get_request(req1);
  1763. }
  1764. if (!list_empty(&ci->i_unsafe_iops)) {
  1765. req2 = list_last_entry(&ci->i_unsafe_iops,
  1766. struct ceph_mds_request,
  1767. r_unsafe_target_item);
  1768. ceph_mdsc_get_request(req2);
  1769. }
  1770. spin_unlock(&ci->i_unsafe_lock);
  1771. dout("unsafe_requeset_wait %p wait on tid %llu %llu\n",
  1772. inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
  1773. if (req1) {
  1774. ret = !wait_for_completion_timeout(&req1->r_safe_completion,
  1775. ceph_timeout_jiffies(req1->r_timeout));
  1776. if (ret)
  1777. err = -EIO;
  1778. ceph_mdsc_put_request(req1);
  1779. }
  1780. if (req2) {
  1781. ret = !wait_for_completion_timeout(&req2->r_safe_completion,
  1782. ceph_timeout_jiffies(req2->r_timeout));
  1783. if (ret)
  1784. err = -EIO;
  1785. ceph_mdsc_put_request(req2);
  1786. }
  1787. return err;
  1788. }
  1789. int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  1790. {
  1791. struct inode *inode = file->f_mapping->host;
  1792. struct ceph_inode_info *ci = ceph_inode(inode);
  1793. u64 flush_tid;
  1794. int ret;
  1795. int dirty;
  1796. dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
  1797. sync_write_wait(inode);
  1798. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1799. if (ret < 0)
  1800. goto out;
  1801. if (datasync)
  1802. goto out;
  1803. inode_lock(inode);
  1804. dirty = try_flush_caps(inode, &flush_tid);
  1805. dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
  1806. ret = unsafe_request_wait(inode);
  1807. /*
  1808. * only wait on non-file metadata writeback (the mds
  1809. * can recover size and mtime, so we don't need to
  1810. * wait for that)
  1811. */
  1812. if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
  1813. ret = wait_event_interruptible(ci->i_cap_wq,
  1814. caps_are_flushed(inode, flush_tid));
  1815. }
  1816. inode_unlock(inode);
  1817. out:
  1818. dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
  1819. return ret;
  1820. }
  1821. /*
  1822. * Flush any dirty caps back to the mds. If we aren't asked to wait,
  1823. * queue inode for flush but don't do so immediately, because we can
  1824. * get by with fewer MDS messages if we wait for data writeback to
  1825. * complete first.
  1826. */
  1827. int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
  1828. {
  1829. struct ceph_inode_info *ci = ceph_inode(inode);
  1830. u64 flush_tid;
  1831. int err = 0;
  1832. int dirty;
  1833. int wait = wbc->sync_mode == WB_SYNC_ALL;
  1834. dout("write_inode %p wait=%d\n", inode, wait);
  1835. if (wait) {
  1836. dirty = try_flush_caps(inode, &flush_tid);
  1837. if (dirty)
  1838. err = wait_event_interruptible(ci->i_cap_wq,
  1839. caps_are_flushed(inode, flush_tid));
  1840. } else {
  1841. struct ceph_mds_client *mdsc =
  1842. ceph_sb_to_client(inode->i_sb)->mdsc;
  1843. spin_lock(&ci->i_ceph_lock);
  1844. if (__ceph_caps_dirty(ci))
  1845. __cap_delay_requeue_front(mdsc, ci);
  1846. spin_unlock(&ci->i_ceph_lock);
  1847. }
  1848. return err;
  1849. }
  1850. /*
  1851. * After a recovering MDS goes active, we need to resend any caps
  1852. * we were flushing.
  1853. *
  1854. * Caller holds session->s_mutex.
  1855. */
  1856. static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
  1857. struct ceph_mds_session *session)
  1858. {
  1859. struct ceph_cap_snap *capsnap;
  1860. dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
  1861. list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
  1862. flushing_item) {
  1863. struct ceph_inode_info *ci = capsnap->ci;
  1864. struct inode *inode = &ci->vfs_inode;
  1865. struct ceph_cap *cap;
  1866. spin_lock(&ci->i_ceph_lock);
  1867. cap = ci->i_auth_cap;
  1868. if (cap && cap->session == session) {
  1869. dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
  1870. cap, capsnap);
  1871. __ceph_flush_snaps(ci, &session, 1);
  1872. } else {
  1873. pr_err("%p auth cap %p not mds%d ???\n", inode,
  1874. cap, session->s_mds);
  1875. }
  1876. spin_unlock(&ci->i_ceph_lock);
  1877. }
  1878. }
  1879. static int __kick_flushing_caps(struct ceph_mds_client *mdsc,
  1880. struct ceph_mds_session *session,
  1881. struct ceph_inode_info *ci)
  1882. {
  1883. struct inode *inode = &ci->vfs_inode;
  1884. struct ceph_cap *cap;
  1885. struct ceph_cap_flush *cf;
  1886. struct rb_node *n;
  1887. int delayed = 0;
  1888. u64 first_tid = 0;
  1889. u64 oldest_flush_tid;
  1890. spin_lock(&mdsc->cap_dirty_lock);
  1891. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1892. spin_unlock(&mdsc->cap_dirty_lock);
  1893. while (true) {
  1894. spin_lock(&ci->i_ceph_lock);
  1895. cap = ci->i_auth_cap;
  1896. if (!(cap && cap->session == session)) {
  1897. pr_err("%p auth cap %p not mds%d ???\n", inode,
  1898. cap, session->s_mds);
  1899. spin_unlock(&ci->i_ceph_lock);
  1900. break;
  1901. }
  1902. for (n = rb_first(&ci->i_cap_flush_tree); n; n = rb_next(n)) {
  1903. cf = rb_entry(n, struct ceph_cap_flush, i_node);
  1904. if (cf->tid >= first_tid)
  1905. break;
  1906. }
  1907. if (!n) {
  1908. spin_unlock(&ci->i_ceph_lock);
  1909. break;
  1910. }
  1911. cf = rb_entry(n, struct ceph_cap_flush, i_node);
  1912. first_tid = cf->tid + 1;
  1913. dout("kick_flushing_caps %p cap %p tid %llu %s\n", inode,
  1914. cap, cf->tid, ceph_cap_string(cf->caps));
  1915. delayed |= __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
  1916. __ceph_caps_used(ci),
  1917. __ceph_caps_wanted(ci),
  1918. cap->issued | cap->implemented,
  1919. cf->caps, cf->tid, oldest_flush_tid);
  1920. }
  1921. return delayed;
  1922. }
  1923. void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
  1924. struct ceph_mds_session *session)
  1925. {
  1926. struct ceph_inode_info *ci;
  1927. struct ceph_cap *cap;
  1928. dout("early_kick_flushing_caps mds%d\n", session->s_mds);
  1929. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  1930. spin_lock(&ci->i_ceph_lock);
  1931. cap = ci->i_auth_cap;
  1932. if (!(cap && cap->session == session)) {
  1933. pr_err("%p auth cap %p not mds%d ???\n",
  1934. &ci->vfs_inode, cap, session->s_mds);
  1935. spin_unlock(&ci->i_ceph_lock);
  1936. continue;
  1937. }
  1938. /*
  1939. * if flushing caps were revoked, we re-send the cap flush
  1940. * in client reconnect stage. This guarantees MDS * processes
  1941. * the cap flush message before issuing the flushing caps to
  1942. * other client.
  1943. */
  1944. if ((cap->issued & ci->i_flushing_caps) !=
  1945. ci->i_flushing_caps) {
  1946. spin_unlock(&ci->i_ceph_lock);
  1947. if (!__kick_flushing_caps(mdsc, session, ci))
  1948. continue;
  1949. spin_lock(&ci->i_ceph_lock);
  1950. }
  1951. spin_unlock(&ci->i_ceph_lock);
  1952. }
  1953. }
  1954. void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
  1955. struct ceph_mds_session *session)
  1956. {
  1957. struct ceph_inode_info *ci;
  1958. kick_flushing_capsnaps(mdsc, session);
  1959. dout("kick_flushing_caps mds%d\n", session->s_mds);
  1960. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  1961. int delayed = __kick_flushing_caps(mdsc, session, ci);
  1962. if (delayed) {
  1963. spin_lock(&ci->i_ceph_lock);
  1964. __cap_delay_requeue(mdsc, ci);
  1965. spin_unlock(&ci->i_ceph_lock);
  1966. }
  1967. }
  1968. }
  1969. static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
  1970. struct ceph_mds_session *session,
  1971. struct inode *inode)
  1972. {
  1973. struct ceph_inode_info *ci = ceph_inode(inode);
  1974. struct ceph_cap *cap;
  1975. spin_lock(&ci->i_ceph_lock);
  1976. cap = ci->i_auth_cap;
  1977. dout("kick_flushing_inode_caps %p flushing %s\n", inode,
  1978. ceph_cap_string(ci->i_flushing_caps));
  1979. __ceph_flush_snaps(ci, &session, 1);
  1980. if (ci->i_flushing_caps) {
  1981. int delayed;
  1982. spin_lock(&mdsc->cap_dirty_lock);
  1983. list_move_tail(&ci->i_flushing_item,
  1984. &cap->session->s_cap_flushing);
  1985. spin_unlock(&mdsc->cap_dirty_lock);
  1986. spin_unlock(&ci->i_ceph_lock);
  1987. delayed = __kick_flushing_caps(mdsc, session, ci);
  1988. if (delayed) {
  1989. spin_lock(&ci->i_ceph_lock);
  1990. __cap_delay_requeue(mdsc, ci);
  1991. spin_unlock(&ci->i_ceph_lock);
  1992. }
  1993. } else {
  1994. spin_unlock(&ci->i_ceph_lock);
  1995. }
  1996. }
  1997. /*
  1998. * Take references to capabilities we hold, so that we don't release
  1999. * them to the MDS prematurely.
  2000. *
  2001. * Protected by i_ceph_lock.
  2002. */
  2003. static void __take_cap_refs(struct ceph_inode_info *ci, int got,
  2004. bool snap_rwsem_locked)
  2005. {
  2006. if (got & CEPH_CAP_PIN)
  2007. ci->i_pin_ref++;
  2008. if (got & CEPH_CAP_FILE_RD)
  2009. ci->i_rd_ref++;
  2010. if (got & CEPH_CAP_FILE_CACHE)
  2011. ci->i_rdcache_ref++;
  2012. if (got & CEPH_CAP_FILE_WR) {
  2013. if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
  2014. BUG_ON(!snap_rwsem_locked);
  2015. ci->i_head_snapc = ceph_get_snap_context(
  2016. ci->i_snap_realm->cached_context);
  2017. }
  2018. ci->i_wr_ref++;
  2019. }
  2020. if (got & CEPH_CAP_FILE_BUFFER) {
  2021. if (ci->i_wb_ref == 0)
  2022. ihold(&ci->vfs_inode);
  2023. ci->i_wb_ref++;
  2024. dout("__take_cap_refs %p wb %d -> %d (?)\n",
  2025. &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
  2026. }
  2027. }
  2028. /*
  2029. * Try to grab cap references. Specify those refs we @want, and the
  2030. * minimal set we @need. Also include the larger offset we are writing
  2031. * to (when applicable), and check against max_size here as well.
  2032. * Note that caller is responsible for ensuring max_size increases are
  2033. * requested from the MDS.
  2034. */
  2035. static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
  2036. loff_t endoff, bool nonblock, int *got, int *err)
  2037. {
  2038. struct inode *inode = &ci->vfs_inode;
  2039. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  2040. int ret = 0;
  2041. int have, implemented;
  2042. int file_wanted;
  2043. bool snap_rwsem_locked = false;
  2044. dout("get_cap_refs %p need %s want %s\n", inode,
  2045. ceph_cap_string(need), ceph_cap_string(want));
  2046. again:
  2047. spin_lock(&ci->i_ceph_lock);
  2048. /* make sure file is actually open */
  2049. file_wanted = __ceph_caps_file_wanted(ci);
  2050. if ((file_wanted & need) == 0) {
  2051. dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
  2052. ceph_cap_string(need), ceph_cap_string(file_wanted));
  2053. *err = -EBADF;
  2054. ret = 1;
  2055. goto out_unlock;
  2056. }
  2057. /* finish pending truncate */
  2058. while (ci->i_truncate_pending) {
  2059. spin_unlock(&ci->i_ceph_lock);
  2060. if (snap_rwsem_locked) {
  2061. up_read(&mdsc->snap_rwsem);
  2062. snap_rwsem_locked = false;
  2063. }
  2064. __ceph_do_pending_vmtruncate(inode);
  2065. spin_lock(&ci->i_ceph_lock);
  2066. }
  2067. have = __ceph_caps_issued(ci, &implemented);
  2068. if (have & need & CEPH_CAP_FILE_WR) {
  2069. if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
  2070. dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
  2071. inode, endoff, ci->i_max_size);
  2072. if (endoff > ci->i_requested_max_size) {
  2073. *err = -EAGAIN;
  2074. ret = 1;
  2075. }
  2076. goto out_unlock;
  2077. }
  2078. /*
  2079. * If a sync write is in progress, we must wait, so that we
  2080. * can get a final snapshot value for size+mtime.
  2081. */
  2082. if (__ceph_have_pending_cap_snap(ci)) {
  2083. dout("get_cap_refs %p cap_snap_pending\n", inode);
  2084. goto out_unlock;
  2085. }
  2086. }
  2087. if ((have & need) == need) {
  2088. /*
  2089. * Look at (implemented & ~have & not) so that we keep waiting
  2090. * on transition from wanted -> needed caps. This is needed
  2091. * for WRBUFFER|WR -> WR to avoid a new WR sync write from
  2092. * going before a prior buffered writeback happens.
  2093. */
  2094. int not = want & ~(have & need);
  2095. int revoking = implemented & ~have;
  2096. dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
  2097. inode, ceph_cap_string(have), ceph_cap_string(not),
  2098. ceph_cap_string(revoking));
  2099. if ((revoking & not) == 0) {
  2100. if (!snap_rwsem_locked &&
  2101. !ci->i_head_snapc &&
  2102. (need & CEPH_CAP_FILE_WR)) {
  2103. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  2104. /*
  2105. * we can not call down_read() when
  2106. * task isn't in TASK_RUNNING state
  2107. */
  2108. if (nonblock) {
  2109. *err = -EAGAIN;
  2110. ret = 1;
  2111. goto out_unlock;
  2112. }
  2113. spin_unlock(&ci->i_ceph_lock);
  2114. down_read(&mdsc->snap_rwsem);
  2115. snap_rwsem_locked = true;
  2116. goto again;
  2117. }
  2118. snap_rwsem_locked = true;
  2119. }
  2120. *got = need | (have & want);
  2121. __take_cap_refs(ci, *got, true);
  2122. ret = 1;
  2123. }
  2124. } else {
  2125. int session_readonly = false;
  2126. if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
  2127. struct ceph_mds_session *s = ci->i_auth_cap->session;
  2128. spin_lock(&s->s_cap_lock);
  2129. session_readonly = s->s_readonly;
  2130. spin_unlock(&s->s_cap_lock);
  2131. }
  2132. if (session_readonly) {
  2133. dout("get_cap_refs %p needed %s but mds%d readonly\n",
  2134. inode, ceph_cap_string(need), ci->i_auth_cap->mds);
  2135. *err = -EROFS;
  2136. ret = 1;
  2137. goto out_unlock;
  2138. }
  2139. if (!__ceph_is_any_caps(ci) &&
  2140. ACCESS_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  2141. dout("get_cap_refs %p forced umount\n", inode);
  2142. *err = -EIO;
  2143. ret = 1;
  2144. goto out_unlock;
  2145. }
  2146. dout("get_cap_refs %p have %s needed %s\n", inode,
  2147. ceph_cap_string(have), ceph_cap_string(need));
  2148. }
  2149. out_unlock:
  2150. spin_unlock(&ci->i_ceph_lock);
  2151. if (snap_rwsem_locked)
  2152. up_read(&mdsc->snap_rwsem);
  2153. dout("get_cap_refs %p ret %d got %s\n", inode,
  2154. ret, ceph_cap_string(*got));
  2155. return ret;
  2156. }
  2157. /*
  2158. * Check the offset we are writing up to against our current
  2159. * max_size. If necessary, tell the MDS we want to write to
  2160. * a larger offset.
  2161. */
  2162. static void check_max_size(struct inode *inode, loff_t endoff)
  2163. {
  2164. struct ceph_inode_info *ci = ceph_inode(inode);
  2165. int check = 0;
  2166. /* do we need to explicitly request a larger max_size? */
  2167. spin_lock(&ci->i_ceph_lock);
  2168. if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
  2169. dout("write %p at large endoff %llu, req max_size\n",
  2170. inode, endoff);
  2171. ci->i_wanted_max_size = endoff;
  2172. }
  2173. /* duplicate ceph_check_caps()'s logic */
  2174. if (ci->i_auth_cap &&
  2175. (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
  2176. ci->i_wanted_max_size > ci->i_max_size &&
  2177. ci->i_wanted_max_size > ci->i_requested_max_size)
  2178. check = 1;
  2179. spin_unlock(&ci->i_ceph_lock);
  2180. if (check)
  2181. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  2182. }
  2183. /*
  2184. * Wait for caps, and take cap references. If we can't get a WR cap
  2185. * due to a small max_size, make sure we check_max_size (and possibly
  2186. * ask the mds) so we don't get hung up indefinitely.
  2187. */
  2188. int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
  2189. loff_t endoff, int *got, struct page **pinned_page)
  2190. {
  2191. int _got, ret, err = 0;
  2192. ret = ceph_pool_perm_check(ci, need);
  2193. if (ret < 0)
  2194. return ret;
  2195. while (true) {
  2196. if (endoff > 0)
  2197. check_max_size(&ci->vfs_inode, endoff);
  2198. err = 0;
  2199. _got = 0;
  2200. ret = try_get_cap_refs(ci, need, want, endoff,
  2201. false, &_got, &err);
  2202. if (ret) {
  2203. if (err == -EAGAIN)
  2204. continue;
  2205. if (err < 0)
  2206. return err;
  2207. } else {
  2208. ret = wait_event_interruptible(ci->i_cap_wq,
  2209. try_get_cap_refs(ci, need, want, endoff,
  2210. true, &_got, &err));
  2211. if (err == -EAGAIN)
  2212. continue;
  2213. if (err < 0)
  2214. ret = err;
  2215. if (ret < 0)
  2216. return ret;
  2217. }
  2218. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  2219. (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
  2220. i_size_read(&ci->vfs_inode) > 0) {
  2221. struct page *page =
  2222. find_get_page(ci->vfs_inode.i_mapping, 0);
  2223. if (page) {
  2224. if (PageUptodate(page)) {
  2225. *pinned_page = page;
  2226. break;
  2227. }
  2228. page_cache_release(page);
  2229. }
  2230. /*
  2231. * drop cap refs first because getattr while
  2232. * holding * caps refs can cause deadlock.
  2233. */
  2234. ceph_put_cap_refs(ci, _got);
  2235. _got = 0;
  2236. /*
  2237. * getattr request will bring inline data into
  2238. * page cache
  2239. */
  2240. ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
  2241. CEPH_STAT_CAP_INLINE_DATA,
  2242. true);
  2243. if (ret < 0)
  2244. return ret;
  2245. continue;
  2246. }
  2247. break;
  2248. }
  2249. *got = _got;
  2250. return 0;
  2251. }
  2252. /*
  2253. * Take cap refs. Caller must already know we hold at least one ref
  2254. * on the caps in question or we don't know this is safe.
  2255. */
  2256. void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
  2257. {
  2258. spin_lock(&ci->i_ceph_lock);
  2259. __take_cap_refs(ci, caps, false);
  2260. spin_unlock(&ci->i_ceph_lock);
  2261. }
  2262. /*
  2263. * drop cap_snap that is not associated with any snapshot.
  2264. * we don't need to send FLUSHSNAP message for it.
  2265. */
  2266. static int ceph_try_drop_cap_snap(struct ceph_cap_snap *capsnap)
  2267. {
  2268. if (!capsnap->need_flush &&
  2269. !capsnap->writing && !capsnap->dirty_pages) {
  2270. dout("dropping cap_snap %p follows %llu\n",
  2271. capsnap, capsnap->follows);
  2272. ceph_put_snap_context(capsnap->context);
  2273. list_del(&capsnap->ci_item);
  2274. list_del(&capsnap->flushing_item);
  2275. ceph_put_cap_snap(capsnap);
  2276. return 1;
  2277. }
  2278. return 0;
  2279. }
  2280. /*
  2281. * Release cap refs.
  2282. *
  2283. * If we released the last ref on any given cap, call ceph_check_caps
  2284. * to release (or schedule a release).
  2285. *
  2286. * If we are releasing a WR cap (from a sync write), finalize any affected
  2287. * cap_snap, and wake up any waiters.
  2288. */
  2289. void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
  2290. {
  2291. struct inode *inode = &ci->vfs_inode;
  2292. int last = 0, put = 0, flushsnaps = 0, wake = 0;
  2293. spin_lock(&ci->i_ceph_lock);
  2294. if (had & CEPH_CAP_PIN)
  2295. --ci->i_pin_ref;
  2296. if (had & CEPH_CAP_FILE_RD)
  2297. if (--ci->i_rd_ref == 0)
  2298. last++;
  2299. if (had & CEPH_CAP_FILE_CACHE)
  2300. if (--ci->i_rdcache_ref == 0)
  2301. last++;
  2302. if (had & CEPH_CAP_FILE_BUFFER) {
  2303. if (--ci->i_wb_ref == 0) {
  2304. last++;
  2305. put++;
  2306. }
  2307. dout("put_cap_refs %p wb %d -> %d (?)\n",
  2308. inode, ci->i_wb_ref+1, ci->i_wb_ref);
  2309. }
  2310. if (had & CEPH_CAP_FILE_WR)
  2311. if (--ci->i_wr_ref == 0) {
  2312. last++;
  2313. if (__ceph_have_pending_cap_snap(ci)) {
  2314. struct ceph_cap_snap *capsnap =
  2315. list_last_entry(&ci->i_cap_snaps,
  2316. struct ceph_cap_snap,
  2317. ci_item);
  2318. capsnap->writing = 0;
  2319. if (ceph_try_drop_cap_snap(capsnap))
  2320. put++;
  2321. else if (__ceph_finish_cap_snap(ci, capsnap))
  2322. flushsnaps = 1;
  2323. wake = 1;
  2324. }
  2325. if (ci->i_wrbuffer_ref_head == 0 &&
  2326. ci->i_dirty_caps == 0 &&
  2327. ci->i_flushing_caps == 0) {
  2328. BUG_ON(!ci->i_head_snapc);
  2329. ceph_put_snap_context(ci->i_head_snapc);
  2330. ci->i_head_snapc = NULL;
  2331. }
  2332. /* see comment in __ceph_remove_cap() */
  2333. if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
  2334. drop_inode_snap_realm(ci);
  2335. }
  2336. spin_unlock(&ci->i_ceph_lock);
  2337. dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
  2338. last ? " last" : "", put ? " put" : "");
  2339. if (last && !flushsnaps)
  2340. ceph_check_caps(ci, 0, NULL);
  2341. else if (flushsnaps)
  2342. ceph_flush_snaps(ci);
  2343. if (wake)
  2344. wake_up_all(&ci->i_cap_wq);
  2345. while (put-- > 0)
  2346. iput(inode);
  2347. }
  2348. /*
  2349. * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
  2350. * context. Adjust per-snap dirty page accounting as appropriate.
  2351. * Once all dirty data for a cap_snap is flushed, flush snapped file
  2352. * metadata back to the MDS. If we dropped the last ref, call
  2353. * ceph_check_caps.
  2354. */
  2355. void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
  2356. struct ceph_snap_context *snapc)
  2357. {
  2358. struct inode *inode = &ci->vfs_inode;
  2359. int last = 0;
  2360. int complete_capsnap = 0;
  2361. int drop_capsnap = 0;
  2362. int found = 0;
  2363. struct ceph_cap_snap *capsnap = NULL;
  2364. spin_lock(&ci->i_ceph_lock);
  2365. ci->i_wrbuffer_ref -= nr;
  2366. last = !ci->i_wrbuffer_ref;
  2367. if (ci->i_head_snapc == snapc) {
  2368. ci->i_wrbuffer_ref_head -= nr;
  2369. if (ci->i_wrbuffer_ref_head == 0 &&
  2370. ci->i_wr_ref == 0 &&
  2371. ci->i_dirty_caps == 0 &&
  2372. ci->i_flushing_caps == 0) {
  2373. BUG_ON(!ci->i_head_snapc);
  2374. ceph_put_snap_context(ci->i_head_snapc);
  2375. ci->i_head_snapc = NULL;
  2376. }
  2377. dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
  2378. inode,
  2379. ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
  2380. ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  2381. last ? " LAST" : "");
  2382. } else {
  2383. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  2384. if (capsnap->context == snapc) {
  2385. found = 1;
  2386. break;
  2387. }
  2388. }
  2389. BUG_ON(!found);
  2390. capsnap->dirty_pages -= nr;
  2391. if (capsnap->dirty_pages == 0) {
  2392. complete_capsnap = 1;
  2393. drop_capsnap = ceph_try_drop_cap_snap(capsnap);
  2394. }
  2395. dout("put_wrbuffer_cap_refs on %p cap_snap %p "
  2396. " snap %lld %d/%d -> %d/%d %s%s\n",
  2397. inode, capsnap, capsnap->context->seq,
  2398. ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
  2399. ci->i_wrbuffer_ref, capsnap->dirty_pages,
  2400. last ? " (wrbuffer last)" : "",
  2401. complete_capsnap ? " (complete capsnap)" : "");
  2402. }
  2403. spin_unlock(&ci->i_ceph_lock);
  2404. if (last) {
  2405. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  2406. iput(inode);
  2407. } else if (complete_capsnap) {
  2408. ceph_flush_snaps(ci);
  2409. wake_up_all(&ci->i_cap_wq);
  2410. }
  2411. if (drop_capsnap)
  2412. iput(inode);
  2413. }
  2414. /*
  2415. * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
  2416. */
  2417. static void invalidate_aliases(struct inode *inode)
  2418. {
  2419. struct dentry *dn, *prev = NULL;
  2420. dout("invalidate_aliases inode %p\n", inode);
  2421. d_prune_aliases(inode);
  2422. /*
  2423. * For non-directory inode, d_find_alias() only returns
  2424. * hashed dentry. After calling d_invalidate(), the
  2425. * dentry becomes unhashed.
  2426. *
  2427. * For directory inode, d_find_alias() can return
  2428. * unhashed dentry. But directory inode should have
  2429. * one alias at most.
  2430. */
  2431. while ((dn = d_find_alias(inode))) {
  2432. if (dn == prev) {
  2433. dput(dn);
  2434. break;
  2435. }
  2436. d_invalidate(dn);
  2437. if (prev)
  2438. dput(prev);
  2439. prev = dn;
  2440. }
  2441. if (prev)
  2442. dput(prev);
  2443. }
  2444. /*
  2445. * Handle a cap GRANT message from the MDS. (Note that a GRANT may
  2446. * actually be a revocation if it specifies a smaller cap set.)
  2447. *
  2448. * caller holds s_mutex and i_ceph_lock, we drop both.
  2449. */
  2450. static void handle_cap_grant(struct ceph_mds_client *mdsc,
  2451. struct inode *inode, struct ceph_mds_caps *grant,
  2452. u64 inline_version,
  2453. void *inline_data, int inline_len,
  2454. struct ceph_buffer *xattr_buf,
  2455. struct ceph_mds_session *session,
  2456. struct ceph_cap *cap, int issued)
  2457. __releases(ci->i_ceph_lock)
  2458. __releases(mdsc->snap_rwsem)
  2459. {
  2460. struct ceph_inode_info *ci = ceph_inode(inode);
  2461. int mds = session->s_mds;
  2462. int seq = le32_to_cpu(grant->seq);
  2463. int newcaps = le32_to_cpu(grant->caps);
  2464. int used, wanted, dirty;
  2465. u64 size = le64_to_cpu(grant->size);
  2466. u64 max_size = le64_to_cpu(grant->max_size);
  2467. struct timespec mtime, atime, ctime;
  2468. int check_caps = 0;
  2469. bool wake = false;
  2470. bool writeback = false;
  2471. bool queue_trunc = false;
  2472. bool queue_invalidate = false;
  2473. bool queue_revalidate = false;
  2474. bool deleted_inode = false;
  2475. bool fill_inline = false;
  2476. dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
  2477. inode, cap, mds, seq, ceph_cap_string(newcaps));
  2478. dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
  2479. inode->i_size);
  2480. /*
  2481. * auth mds of the inode changed. we received the cap export message,
  2482. * but still haven't received the cap import message. handle_cap_export
  2483. * updated the new auth MDS' cap.
  2484. *
  2485. * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
  2486. * that was sent before the cap import message. So don't remove caps.
  2487. */
  2488. if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  2489. WARN_ON(cap != ci->i_auth_cap);
  2490. WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
  2491. seq = cap->seq;
  2492. newcaps |= cap->issued;
  2493. }
  2494. /*
  2495. * If CACHE is being revoked, and we have no dirty buffers,
  2496. * try to invalidate (once). (If there are dirty buffers, we
  2497. * will invalidate _after_ writeback.)
  2498. */
  2499. if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
  2500. ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
  2501. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  2502. !ci->i_wrbuffer_ref) {
  2503. if (try_nonblocking_invalidate(inode)) {
  2504. /* there were locked pages.. invalidate later
  2505. in a separate thread. */
  2506. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  2507. queue_invalidate = true;
  2508. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  2509. }
  2510. }
  2511. ceph_fscache_invalidate(inode);
  2512. }
  2513. /* side effects now are allowed */
  2514. cap->cap_gen = session->s_cap_gen;
  2515. cap->seq = seq;
  2516. __check_cap_issue(ci, cap, newcaps);
  2517. if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
  2518. (issued & CEPH_CAP_AUTH_EXCL) == 0) {
  2519. inode->i_mode = le32_to_cpu(grant->mode);
  2520. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
  2521. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
  2522. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  2523. from_kuid(&init_user_ns, inode->i_uid),
  2524. from_kgid(&init_user_ns, inode->i_gid));
  2525. }
  2526. if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
  2527. (issued & CEPH_CAP_LINK_EXCL) == 0) {
  2528. set_nlink(inode, le32_to_cpu(grant->nlink));
  2529. if (inode->i_nlink == 0 &&
  2530. (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
  2531. deleted_inode = true;
  2532. }
  2533. if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
  2534. int len = le32_to_cpu(grant->xattr_len);
  2535. u64 version = le64_to_cpu(grant->xattr_version);
  2536. if (version > ci->i_xattrs.version) {
  2537. dout(" got new xattrs v%llu on %p len %d\n",
  2538. version, inode, len);
  2539. if (ci->i_xattrs.blob)
  2540. ceph_buffer_put(ci->i_xattrs.blob);
  2541. ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
  2542. ci->i_xattrs.version = version;
  2543. ceph_forget_all_cached_acls(inode);
  2544. }
  2545. }
  2546. /* Do we need to revalidate our fscache cookie. Don't bother on the
  2547. * first cache cap as we already validate at cookie creation time. */
  2548. if ((issued & CEPH_CAP_FILE_CACHE) && ci->i_rdcache_gen > 1)
  2549. queue_revalidate = true;
  2550. if (newcaps & CEPH_CAP_ANY_RD) {
  2551. /* ctime/mtime/atime? */
  2552. ceph_decode_timespec(&mtime, &grant->mtime);
  2553. ceph_decode_timespec(&atime, &grant->atime);
  2554. ceph_decode_timespec(&ctime, &grant->ctime);
  2555. ceph_fill_file_time(inode, issued,
  2556. le32_to_cpu(grant->time_warp_seq),
  2557. &ctime, &mtime, &atime);
  2558. }
  2559. if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
  2560. /* file layout may have changed */
  2561. ci->i_layout = grant->layout;
  2562. /* size/truncate_seq? */
  2563. queue_trunc = ceph_fill_file_size(inode, issued,
  2564. le32_to_cpu(grant->truncate_seq),
  2565. le64_to_cpu(grant->truncate_size),
  2566. size);
  2567. /* max size increase? */
  2568. if (ci->i_auth_cap == cap && max_size != ci->i_max_size) {
  2569. dout("max_size %lld -> %llu\n",
  2570. ci->i_max_size, max_size);
  2571. ci->i_max_size = max_size;
  2572. if (max_size >= ci->i_wanted_max_size) {
  2573. ci->i_wanted_max_size = 0; /* reset */
  2574. ci->i_requested_max_size = 0;
  2575. }
  2576. wake = true;
  2577. }
  2578. }
  2579. /* check cap bits */
  2580. wanted = __ceph_caps_wanted(ci);
  2581. used = __ceph_caps_used(ci);
  2582. dirty = __ceph_caps_dirty(ci);
  2583. dout(" my wanted = %s, used = %s, dirty %s\n",
  2584. ceph_cap_string(wanted),
  2585. ceph_cap_string(used),
  2586. ceph_cap_string(dirty));
  2587. if (wanted != le32_to_cpu(grant->wanted)) {
  2588. dout("mds wanted %s -> %s\n",
  2589. ceph_cap_string(le32_to_cpu(grant->wanted)),
  2590. ceph_cap_string(wanted));
  2591. /* imported cap may not have correct mds_wanted */
  2592. if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
  2593. check_caps = 1;
  2594. }
  2595. /* revocation, grant, or no-op? */
  2596. if (cap->issued & ~newcaps) {
  2597. int revoking = cap->issued & ~newcaps;
  2598. dout("revocation: %s -> %s (revoking %s)\n",
  2599. ceph_cap_string(cap->issued),
  2600. ceph_cap_string(newcaps),
  2601. ceph_cap_string(revoking));
  2602. if (revoking & used & CEPH_CAP_FILE_BUFFER)
  2603. writeback = true; /* initiate writeback; will delay ack */
  2604. else if (revoking == CEPH_CAP_FILE_CACHE &&
  2605. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  2606. queue_invalidate)
  2607. ; /* do nothing yet, invalidation will be queued */
  2608. else if (cap == ci->i_auth_cap)
  2609. check_caps = 1; /* check auth cap only */
  2610. else
  2611. check_caps = 2; /* check all caps */
  2612. cap->issued = newcaps;
  2613. cap->implemented |= newcaps;
  2614. } else if (cap->issued == newcaps) {
  2615. dout("caps unchanged: %s -> %s\n",
  2616. ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
  2617. } else {
  2618. dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
  2619. ceph_cap_string(newcaps));
  2620. /* non-auth MDS is revoking the newly grant caps ? */
  2621. if (cap == ci->i_auth_cap &&
  2622. __ceph_caps_revoking_other(ci, cap, newcaps))
  2623. check_caps = 2;
  2624. cap->issued = newcaps;
  2625. cap->implemented |= newcaps; /* add bits only, to
  2626. * avoid stepping on a
  2627. * pending revocation */
  2628. wake = true;
  2629. }
  2630. BUG_ON(cap->issued & ~cap->implemented);
  2631. if (inline_version > 0 && inline_version >= ci->i_inline_version) {
  2632. ci->i_inline_version = inline_version;
  2633. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  2634. (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
  2635. fill_inline = true;
  2636. }
  2637. spin_unlock(&ci->i_ceph_lock);
  2638. if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
  2639. kick_flushing_inode_caps(mdsc, session, inode);
  2640. up_read(&mdsc->snap_rwsem);
  2641. if (newcaps & ~issued)
  2642. wake = true;
  2643. }
  2644. if (fill_inline)
  2645. ceph_fill_inline_data(inode, NULL, inline_data, inline_len);
  2646. if (queue_trunc) {
  2647. ceph_queue_vmtruncate(inode);
  2648. ceph_queue_revalidate(inode);
  2649. } else if (queue_revalidate)
  2650. ceph_queue_revalidate(inode);
  2651. if (writeback)
  2652. /*
  2653. * queue inode for writeback: we can't actually call
  2654. * filemap_write_and_wait, etc. from message handler
  2655. * context.
  2656. */
  2657. ceph_queue_writeback(inode);
  2658. if (queue_invalidate)
  2659. ceph_queue_invalidate(inode);
  2660. if (deleted_inode)
  2661. invalidate_aliases(inode);
  2662. if (wake)
  2663. wake_up_all(&ci->i_cap_wq);
  2664. if (check_caps == 1)
  2665. ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
  2666. session);
  2667. else if (check_caps == 2)
  2668. ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
  2669. else
  2670. mutex_unlock(&session->s_mutex);
  2671. }
  2672. /*
  2673. * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
  2674. * MDS has been safely committed.
  2675. */
  2676. static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
  2677. struct ceph_mds_caps *m,
  2678. struct ceph_mds_session *session,
  2679. struct ceph_cap *cap)
  2680. __releases(ci->i_ceph_lock)
  2681. {
  2682. struct ceph_inode_info *ci = ceph_inode(inode);
  2683. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  2684. struct ceph_cap_flush *cf;
  2685. struct rb_node *n;
  2686. LIST_HEAD(to_remove);
  2687. unsigned seq = le32_to_cpu(m->seq);
  2688. int dirty = le32_to_cpu(m->dirty);
  2689. int cleaned = 0;
  2690. int drop = 0;
  2691. n = rb_first(&ci->i_cap_flush_tree);
  2692. while (n) {
  2693. cf = rb_entry(n, struct ceph_cap_flush, i_node);
  2694. n = rb_next(&cf->i_node);
  2695. if (cf->tid == flush_tid)
  2696. cleaned = cf->caps;
  2697. if (cf->tid <= flush_tid) {
  2698. rb_erase(&cf->i_node, &ci->i_cap_flush_tree);
  2699. list_add_tail(&cf->list, &to_remove);
  2700. } else {
  2701. cleaned &= ~cf->caps;
  2702. if (!cleaned)
  2703. break;
  2704. }
  2705. }
  2706. dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
  2707. " flushing %s -> %s\n",
  2708. inode, session->s_mds, seq, ceph_cap_string(dirty),
  2709. ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
  2710. ceph_cap_string(ci->i_flushing_caps & ~cleaned));
  2711. if (list_empty(&to_remove) && !cleaned)
  2712. goto out;
  2713. ci->i_flushing_caps &= ~cleaned;
  2714. spin_lock(&mdsc->cap_dirty_lock);
  2715. if (!list_empty(&to_remove)) {
  2716. list_for_each_entry(cf, &to_remove, list)
  2717. rb_erase(&cf->g_node, &mdsc->cap_flush_tree);
  2718. n = rb_first(&mdsc->cap_flush_tree);
  2719. cf = n ? rb_entry(n, struct ceph_cap_flush, g_node) : NULL;
  2720. if (!cf || cf->tid > flush_tid)
  2721. wake_up_all(&mdsc->cap_flushing_wq);
  2722. }
  2723. if (ci->i_flushing_caps == 0) {
  2724. list_del_init(&ci->i_flushing_item);
  2725. if (!list_empty(&session->s_cap_flushing))
  2726. dout(" mds%d still flushing cap on %p\n",
  2727. session->s_mds,
  2728. &list_entry(session->s_cap_flushing.next,
  2729. struct ceph_inode_info,
  2730. i_flushing_item)->vfs_inode);
  2731. mdsc->num_cap_flushing--;
  2732. dout(" inode %p now !flushing\n", inode);
  2733. if (ci->i_dirty_caps == 0) {
  2734. dout(" inode %p now clean\n", inode);
  2735. BUG_ON(!list_empty(&ci->i_dirty_item));
  2736. drop = 1;
  2737. if (ci->i_wr_ref == 0 &&
  2738. ci->i_wrbuffer_ref_head == 0) {
  2739. BUG_ON(!ci->i_head_snapc);
  2740. ceph_put_snap_context(ci->i_head_snapc);
  2741. ci->i_head_snapc = NULL;
  2742. }
  2743. } else {
  2744. BUG_ON(list_empty(&ci->i_dirty_item));
  2745. }
  2746. }
  2747. spin_unlock(&mdsc->cap_dirty_lock);
  2748. wake_up_all(&ci->i_cap_wq);
  2749. out:
  2750. spin_unlock(&ci->i_ceph_lock);
  2751. while (!list_empty(&to_remove)) {
  2752. cf = list_first_entry(&to_remove,
  2753. struct ceph_cap_flush, list);
  2754. list_del(&cf->list);
  2755. ceph_free_cap_flush(cf);
  2756. }
  2757. if (drop)
  2758. iput(inode);
  2759. }
  2760. /*
  2761. * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
  2762. * throw away our cap_snap.
  2763. *
  2764. * Caller hold s_mutex.
  2765. */
  2766. static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
  2767. struct ceph_mds_caps *m,
  2768. struct ceph_mds_session *session)
  2769. {
  2770. struct ceph_inode_info *ci = ceph_inode(inode);
  2771. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  2772. u64 follows = le64_to_cpu(m->snap_follows);
  2773. struct ceph_cap_snap *capsnap;
  2774. int drop = 0;
  2775. dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
  2776. inode, ci, session->s_mds, follows);
  2777. spin_lock(&ci->i_ceph_lock);
  2778. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  2779. if (capsnap->follows == follows) {
  2780. if (capsnap->flush_tid != flush_tid) {
  2781. dout(" cap_snap %p follows %lld tid %lld !="
  2782. " %lld\n", capsnap, follows,
  2783. flush_tid, capsnap->flush_tid);
  2784. break;
  2785. }
  2786. WARN_ON(capsnap->dirty_pages || capsnap->writing);
  2787. dout(" removing %p cap_snap %p follows %lld\n",
  2788. inode, capsnap, follows);
  2789. ceph_put_snap_context(capsnap->context);
  2790. list_del(&capsnap->ci_item);
  2791. list_del(&capsnap->flushing_item);
  2792. ceph_put_cap_snap(capsnap);
  2793. wake_up_all(&mdsc->cap_flushing_wq);
  2794. drop = 1;
  2795. break;
  2796. } else {
  2797. dout(" skipping cap_snap %p follows %lld\n",
  2798. capsnap, capsnap->follows);
  2799. }
  2800. }
  2801. spin_unlock(&ci->i_ceph_lock);
  2802. if (drop)
  2803. iput(inode);
  2804. }
  2805. /*
  2806. * Handle TRUNC from MDS, indicating file truncation.
  2807. *
  2808. * caller hold s_mutex.
  2809. */
  2810. static void handle_cap_trunc(struct inode *inode,
  2811. struct ceph_mds_caps *trunc,
  2812. struct ceph_mds_session *session)
  2813. __releases(ci->i_ceph_lock)
  2814. {
  2815. struct ceph_inode_info *ci = ceph_inode(inode);
  2816. int mds = session->s_mds;
  2817. int seq = le32_to_cpu(trunc->seq);
  2818. u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
  2819. u64 truncate_size = le64_to_cpu(trunc->truncate_size);
  2820. u64 size = le64_to_cpu(trunc->size);
  2821. int implemented = 0;
  2822. int dirty = __ceph_caps_dirty(ci);
  2823. int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
  2824. int queue_trunc = 0;
  2825. issued |= implemented | dirty;
  2826. dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
  2827. inode, mds, seq, truncate_size, truncate_seq);
  2828. queue_trunc = ceph_fill_file_size(inode, issued,
  2829. truncate_seq, truncate_size, size);
  2830. spin_unlock(&ci->i_ceph_lock);
  2831. if (queue_trunc) {
  2832. ceph_queue_vmtruncate(inode);
  2833. ceph_fscache_invalidate(inode);
  2834. }
  2835. }
  2836. /*
  2837. * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
  2838. * different one. If we are the most recent migration we've seen (as
  2839. * indicated by mseq), make note of the migrating cap bits for the
  2840. * duration (until we see the corresponding IMPORT).
  2841. *
  2842. * caller holds s_mutex
  2843. */
  2844. static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
  2845. struct ceph_mds_cap_peer *ph,
  2846. struct ceph_mds_session *session)
  2847. {
  2848. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  2849. struct ceph_mds_session *tsession = NULL;
  2850. struct ceph_cap *cap, *tcap, *new_cap = NULL;
  2851. struct ceph_inode_info *ci = ceph_inode(inode);
  2852. u64 t_cap_id;
  2853. unsigned mseq = le32_to_cpu(ex->migrate_seq);
  2854. unsigned t_seq, t_mseq;
  2855. int target, issued;
  2856. int mds = session->s_mds;
  2857. if (ph) {
  2858. t_cap_id = le64_to_cpu(ph->cap_id);
  2859. t_seq = le32_to_cpu(ph->seq);
  2860. t_mseq = le32_to_cpu(ph->mseq);
  2861. target = le32_to_cpu(ph->mds);
  2862. } else {
  2863. t_cap_id = t_seq = t_mseq = 0;
  2864. target = -1;
  2865. }
  2866. dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
  2867. inode, ci, mds, mseq, target);
  2868. retry:
  2869. spin_lock(&ci->i_ceph_lock);
  2870. cap = __get_cap_for_mds(ci, mds);
  2871. if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
  2872. goto out_unlock;
  2873. if (target < 0) {
  2874. __ceph_remove_cap(cap, false);
  2875. goto out_unlock;
  2876. }
  2877. /*
  2878. * now we know we haven't received the cap import message yet
  2879. * because the exported cap still exist.
  2880. */
  2881. issued = cap->issued;
  2882. WARN_ON(issued != cap->implemented);
  2883. tcap = __get_cap_for_mds(ci, target);
  2884. if (tcap) {
  2885. /* already have caps from the target */
  2886. if (tcap->cap_id != t_cap_id ||
  2887. ceph_seq_cmp(tcap->seq, t_seq) < 0) {
  2888. dout(" updating import cap %p mds%d\n", tcap, target);
  2889. tcap->cap_id = t_cap_id;
  2890. tcap->seq = t_seq - 1;
  2891. tcap->issue_seq = t_seq - 1;
  2892. tcap->mseq = t_mseq;
  2893. tcap->issued |= issued;
  2894. tcap->implemented |= issued;
  2895. if (cap == ci->i_auth_cap)
  2896. ci->i_auth_cap = tcap;
  2897. if (ci->i_flushing_caps && ci->i_auth_cap == tcap) {
  2898. spin_lock(&mdsc->cap_dirty_lock);
  2899. list_move_tail(&ci->i_flushing_item,
  2900. &tcap->session->s_cap_flushing);
  2901. spin_unlock(&mdsc->cap_dirty_lock);
  2902. }
  2903. }
  2904. __ceph_remove_cap(cap, false);
  2905. goto out_unlock;
  2906. } else if (tsession) {
  2907. /* add placeholder for the export tagert */
  2908. int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
  2909. ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
  2910. t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
  2911. __ceph_remove_cap(cap, false);
  2912. goto out_unlock;
  2913. }
  2914. spin_unlock(&ci->i_ceph_lock);
  2915. mutex_unlock(&session->s_mutex);
  2916. /* open target session */
  2917. tsession = ceph_mdsc_open_export_target_session(mdsc, target);
  2918. if (!IS_ERR(tsession)) {
  2919. if (mds > target) {
  2920. mutex_lock(&session->s_mutex);
  2921. mutex_lock_nested(&tsession->s_mutex,
  2922. SINGLE_DEPTH_NESTING);
  2923. } else {
  2924. mutex_lock(&tsession->s_mutex);
  2925. mutex_lock_nested(&session->s_mutex,
  2926. SINGLE_DEPTH_NESTING);
  2927. }
  2928. new_cap = ceph_get_cap(mdsc, NULL);
  2929. } else {
  2930. WARN_ON(1);
  2931. tsession = NULL;
  2932. target = -1;
  2933. }
  2934. goto retry;
  2935. out_unlock:
  2936. spin_unlock(&ci->i_ceph_lock);
  2937. mutex_unlock(&session->s_mutex);
  2938. if (tsession) {
  2939. mutex_unlock(&tsession->s_mutex);
  2940. ceph_put_mds_session(tsession);
  2941. }
  2942. if (new_cap)
  2943. ceph_put_cap(mdsc, new_cap);
  2944. }
  2945. /*
  2946. * Handle cap IMPORT.
  2947. *
  2948. * caller holds s_mutex. acquires i_ceph_lock
  2949. */
  2950. static void handle_cap_import(struct ceph_mds_client *mdsc,
  2951. struct inode *inode, struct ceph_mds_caps *im,
  2952. struct ceph_mds_cap_peer *ph,
  2953. struct ceph_mds_session *session,
  2954. struct ceph_cap **target_cap, int *old_issued)
  2955. __acquires(ci->i_ceph_lock)
  2956. {
  2957. struct ceph_inode_info *ci = ceph_inode(inode);
  2958. struct ceph_cap *cap, *ocap, *new_cap = NULL;
  2959. int mds = session->s_mds;
  2960. int issued;
  2961. unsigned caps = le32_to_cpu(im->caps);
  2962. unsigned wanted = le32_to_cpu(im->wanted);
  2963. unsigned seq = le32_to_cpu(im->seq);
  2964. unsigned mseq = le32_to_cpu(im->migrate_seq);
  2965. u64 realmino = le64_to_cpu(im->realm);
  2966. u64 cap_id = le64_to_cpu(im->cap_id);
  2967. u64 p_cap_id;
  2968. int peer;
  2969. if (ph) {
  2970. p_cap_id = le64_to_cpu(ph->cap_id);
  2971. peer = le32_to_cpu(ph->mds);
  2972. } else {
  2973. p_cap_id = 0;
  2974. peer = -1;
  2975. }
  2976. dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
  2977. inode, ci, mds, mseq, peer);
  2978. retry:
  2979. spin_lock(&ci->i_ceph_lock);
  2980. cap = __get_cap_for_mds(ci, mds);
  2981. if (!cap) {
  2982. if (!new_cap) {
  2983. spin_unlock(&ci->i_ceph_lock);
  2984. new_cap = ceph_get_cap(mdsc, NULL);
  2985. goto retry;
  2986. }
  2987. cap = new_cap;
  2988. } else {
  2989. if (new_cap) {
  2990. ceph_put_cap(mdsc, new_cap);
  2991. new_cap = NULL;
  2992. }
  2993. }
  2994. __ceph_caps_issued(ci, &issued);
  2995. issued |= __ceph_caps_dirty(ci);
  2996. ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
  2997. realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
  2998. ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
  2999. if (ocap && ocap->cap_id == p_cap_id) {
  3000. dout(" remove export cap %p mds%d flags %d\n",
  3001. ocap, peer, ph->flags);
  3002. if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
  3003. (ocap->seq != le32_to_cpu(ph->seq) ||
  3004. ocap->mseq != le32_to_cpu(ph->mseq))) {
  3005. pr_err("handle_cap_import: mismatched seq/mseq: "
  3006. "ino (%llx.%llx) mds%d seq %d mseq %d "
  3007. "importer mds%d has peer seq %d mseq %d\n",
  3008. ceph_vinop(inode), peer, ocap->seq,
  3009. ocap->mseq, mds, le32_to_cpu(ph->seq),
  3010. le32_to_cpu(ph->mseq));
  3011. }
  3012. __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
  3013. }
  3014. /* make sure we re-request max_size, if necessary */
  3015. ci->i_wanted_max_size = 0;
  3016. ci->i_requested_max_size = 0;
  3017. *old_issued = issued;
  3018. *target_cap = cap;
  3019. }
  3020. /*
  3021. * Handle a caps message from the MDS.
  3022. *
  3023. * Identify the appropriate session, inode, and call the right handler
  3024. * based on the cap op.
  3025. */
  3026. void ceph_handle_caps(struct ceph_mds_session *session,
  3027. struct ceph_msg *msg)
  3028. {
  3029. struct ceph_mds_client *mdsc = session->s_mdsc;
  3030. struct super_block *sb = mdsc->fsc->sb;
  3031. struct inode *inode;
  3032. struct ceph_inode_info *ci;
  3033. struct ceph_cap *cap;
  3034. struct ceph_mds_caps *h;
  3035. struct ceph_mds_cap_peer *peer = NULL;
  3036. struct ceph_snap_realm *realm;
  3037. int mds = session->s_mds;
  3038. int op, issued;
  3039. u32 seq, mseq;
  3040. struct ceph_vino vino;
  3041. u64 cap_id;
  3042. u64 size, max_size;
  3043. u64 tid;
  3044. u64 inline_version = 0;
  3045. void *inline_data = NULL;
  3046. u32 inline_len = 0;
  3047. void *snaptrace;
  3048. size_t snaptrace_len;
  3049. void *p, *end;
  3050. dout("handle_caps from mds%d\n", mds);
  3051. /* decode */
  3052. end = msg->front.iov_base + msg->front.iov_len;
  3053. tid = le64_to_cpu(msg->hdr.tid);
  3054. if (msg->front.iov_len < sizeof(*h))
  3055. goto bad;
  3056. h = msg->front.iov_base;
  3057. op = le32_to_cpu(h->op);
  3058. vino.ino = le64_to_cpu(h->ino);
  3059. vino.snap = CEPH_NOSNAP;
  3060. cap_id = le64_to_cpu(h->cap_id);
  3061. seq = le32_to_cpu(h->seq);
  3062. mseq = le32_to_cpu(h->migrate_seq);
  3063. size = le64_to_cpu(h->size);
  3064. max_size = le64_to_cpu(h->max_size);
  3065. snaptrace = h + 1;
  3066. snaptrace_len = le32_to_cpu(h->snap_trace_len);
  3067. p = snaptrace + snaptrace_len;
  3068. if (le16_to_cpu(msg->hdr.version) >= 2) {
  3069. u32 flock_len;
  3070. ceph_decode_32_safe(&p, end, flock_len, bad);
  3071. if (p + flock_len > end)
  3072. goto bad;
  3073. p += flock_len;
  3074. }
  3075. if (le16_to_cpu(msg->hdr.version) >= 3) {
  3076. if (op == CEPH_CAP_OP_IMPORT) {
  3077. if (p + sizeof(*peer) > end)
  3078. goto bad;
  3079. peer = p;
  3080. p += sizeof(*peer);
  3081. } else if (op == CEPH_CAP_OP_EXPORT) {
  3082. /* recorded in unused fields */
  3083. peer = (void *)&h->size;
  3084. }
  3085. }
  3086. if (le16_to_cpu(msg->hdr.version) >= 4) {
  3087. ceph_decode_64_safe(&p, end, inline_version, bad);
  3088. ceph_decode_32_safe(&p, end, inline_len, bad);
  3089. if (p + inline_len > end)
  3090. goto bad;
  3091. inline_data = p;
  3092. p += inline_len;
  3093. }
  3094. /* lookup ino */
  3095. inode = ceph_find_inode(sb, vino);
  3096. ci = ceph_inode(inode);
  3097. dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
  3098. vino.snap, inode);
  3099. mutex_lock(&session->s_mutex);
  3100. session->s_seq++;
  3101. dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
  3102. (unsigned)seq);
  3103. if (!inode) {
  3104. dout(" i don't have ino %llx\n", vino.ino);
  3105. if (op == CEPH_CAP_OP_IMPORT) {
  3106. cap = ceph_get_cap(mdsc, NULL);
  3107. cap->cap_ino = vino.ino;
  3108. cap->queue_release = 1;
  3109. cap->cap_id = cap_id;
  3110. cap->mseq = mseq;
  3111. cap->seq = seq;
  3112. spin_lock(&session->s_cap_lock);
  3113. list_add_tail(&cap->session_caps,
  3114. &session->s_cap_releases);
  3115. session->s_num_cap_releases++;
  3116. spin_unlock(&session->s_cap_lock);
  3117. }
  3118. goto flush_cap_releases;
  3119. }
  3120. /* these will work even if we don't have a cap yet */
  3121. switch (op) {
  3122. case CEPH_CAP_OP_FLUSHSNAP_ACK:
  3123. handle_cap_flushsnap_ack(inode, tid, h, session);
  3124. goto done;
  3125. case CEPH_CAP_OP_EXPORT:
  3126. handle_cap_export(inode, h, peer, session);
  3127. goto done_unlocked;
  3128. case CEPH_CAP_OP_IMPORT:
  3129. realm = NULL;
  3130. if (snaptrace_len) {
  3131. down_write(&mdsc->snap_rwsem);
  3132. ceph_update_snap_trace(mdsc, snaptrace,
  3133. snaptrace + snaptrace_len,
  3134. false, &realm);
  3135. downgrade_write(&mdsc->snap_rwsem);
  3136. } else {
  3137. down_read(&mdsc->snap_rwsem);
  3138. }
  3139. handle_cap_import(mdsc, inode, h, peer, session,
  3140. &cap, &issued);
  3141. handle_cap_grant(mdsc, inode, h,
  3142. inline_version, inline_data, inline_len,
  3143. msg->middle, session, cap, issued);
  3144. if (realm)
  3145. ceph_put_snap_realm(mdsc, realm);
  3146. goto done_unlocked;
  3147. }
  3148. /* the rest require a cap */
  3149. spin_lock(&ci->i_ceph_lock);
  3150. cap = __get_cap_for_mds(ceph_inode(inode), mds);
  3151. if (!cap) {
  3152. dout(" no cap on %p ino %llx.%llx from mds%d\n",
  3153. inode, ceph_ino(inode), ceph_snap(inode), mds);
  3154. spin_unlock(&ci->i_ceph_lock);
  3155. goto flush_cap_releases;
  3156. }
  3157. /* note that each of these drops i_ceph_lock for us */
  3158. switch (op) {
  3159. case CEPH_CAP_OP_REVOKE:
  3160. case CEPH_CAP_OP_GRANT:
  3161. __ceph_caps_issued(ci, &issued);
  3162. issued |= __ceph_caps_dirty(ci);
  3163. handle_cap_grant(mdsc, inode, h,
  3164. inline_version, inline_data, inline_len,
  3165. msg->middle, session, cap, issued);
  3166. goto done_unlocked;
  3167. case CEPH_CAP_OP_FLUSH_ACK:
  3168. handle_cap_flush_ack(inode, tid, h, session, cap);
  3169. break;
  3170. case CEPH_CAP_OP_TRUNC:
  3171. handle_cap_trunc(inode, h, session);
  3172. break;
  3173. default:
  3174. spin_unlock(&ci->i_ceph_lock);
  3175. pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
  3176. ceph_cap_op_name(op));
  3177. }
  3178. goto done;
  3179. flush_cap_releases:
  3180. /*
  3181. * send any cap release message to try to move things
  3182. * along for the mds (who clearly thinks we still have this
  3183. * cap).
  3184. */
  3185. ceph_send_cap_releases(mdsc, session);
  3186. done:
  3187. mutex_unlock(&session->s_mutex);
  3188. done_unlocked:
  3189. iput(inode);
  3190. return;
  3191. bad:
  3192. pr_err("ceph_handle_caps: corrupt message\n");
  3193. ceph_msg_dump(msg);
  3194. return;
  3195. }
  3196. /*
  3197. * Delayed work handler to process end of delayed cap release LRU list.
  3198. */
  3199. void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
  3200. {
  3201. struct ceph_inode_info *ci;
  3202. int flags = CHECK_CAPS_NODELAY;
  3203. dout("check_delayed_caps\n");
  3204. while (1) {
  3205. spin_lock(&mdsc->cap_delay_lock);
  3206. if (list_empty(&mdsc->cap_delay_list))
  3207. break;
  3208. ci = list_first_entry(&mdsc->cap_delay_list,
  3209. struct ceph_inode_info,
  3210. i_cap_delay_list);
  3211. if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
  3212. time_before(jiffies, ci->i_hold_caps_max))
  3213. break;
  3214. list_del_init(&ci->i_cap_delay_list);
  3215. spin_unlock(&mdsc->cap_delay_lock);
  3216. dout("check_delayed_caps on %p\n", &ci->vfs_inode);
  3217. ceph_check_caps(ci, flags, NULL);
  3218. }
  3219. spin_unlock(&mdsc->cap_delay_lock);
  3220. }
  3221. /*
  3222. * Flush all dirty caps to the mds
  3223. */
  3224. void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
  3225. {
  3226. struct ceph_inode_info *ci;
  3227. struct inode *inode;
  3228. dout("flush_dirty_caps\n");
  3229. spin_lock(&mdsc->cap_dirty_lock);
  3230. while (!list_empty(&mdsc->cap_dirty)) {
  3231. ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
  3232. i_dirty_item);
  3233. inode = &ci->vfs_inode;
  3234. ihold(inode);
  3235. dout("flush_dirty_caps %p\n", inode);
  3236. spin_unlock(&mdsc->cap_dirty_lock);
  3237. ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
  3238. iput(inode);
  3239. spin_lock(&mdsc->cap_dirty_lock);
  3240. }
  3241. spin_unlock(&mdsc->cap_dirty_lock);
  3242. dout("flush_dirty_caps done\n");
  3243. }
  3244. /*
  3245. * Drop open file reference. If we were the last open file,
  3246. * we may need to release capabilities to the MDS (or schedule
  3247. * their delayed release).
  3248. */
  3249. void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
  3250. {
  3251. struct inode *inode = &ci->vfs_inode;
  3252. int last = 0;
  3253. spin_lock(&ci->i_ceph_lock);
  3254. dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
  3255. ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
  3256. BUG_ON(ci->i_nr_by_mode[fmode] == 0);
  3257. if (--ci->i_nr_by_mode[fmode] == 0)
  3258. last++;
  3259. spin_unlock(&ci->i_ceph_lock);
  3260. if (last && ci->i_vino.snap == CEPH_NOSNAP)
  3261. ceph_check_caps(ci, 0, NULL);
  3262. }
  3263. /*
  3264. * Helpers for embedding cap and dentry lease releases into mds
  3265. * requests.
  3266. *
  3267. * @force is used by dentry_release (below) to force inclusion of a
  3268. * record for the directory inode, even when there aren't any caps to
  3269. * drop.
  3270. */
  3271. int ceph_encode_inode_release(void **p, struct inode *inode,
  3272. int mds, int drop, int unless, int force)
  3273. {
  3274. struct ceph_inode_info *ci = ceph_inode(inode);
  3275. struct ceph_cap *cap;
  3276. struct ceph_mds_request_release *rel = *p;
  3277. int used, dirty;
  3278. int ret = 0;
  3279. spin_lock(&ci->i_ceph_lock);
  3280. used = __ceph_caps_used(ci);
  3281. dirty = __ceph_caps_dirty(ci);
  3282. dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
  3283. inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
  3284. ceph_cap_string(unless));
  3285. /* only drop unused, clean caps */
  3286. drop &= ~(used | dirty);
  3287. cap = __get_cap_for_mds(ci, mds);
  3288. if (cap && __cap_is_valid(cap)) {
  3289. if (force ||
  3290. ((cap->issued & drop) &&
  3291. (cap->issued & unless) == 0)) {
  3292. if ((cap->issued & drop) &&
  3293. (cap->issued & unless) == 0) {
  3294. int wanted = __ceph_caps_wanted(ci);
  3295. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
  3296. wanted |= cap->mds_wanted;
  3297. dout("encode_inode_release %p cap %p "
  3298. "%s -> %s, wanted %s -> %s\n", inode, cap,
  3299. ceph_cap_string(cap->issued),
  3300. ceph_cap_string(cap->issued & ~drop),
  3301. ceph_cap_string(cap->mds_wanted),
  3302. ceph_cap_string(wanted));
  3303. cap->issued &= ~drop;
  3304. cap->implemented &= ~drop;
  3305. cap->mds_wanted = wanted;
  3306. } else {
  3307. dout("encode_inode_release %p cap %p %s"
  3308. " (force)\n", inode, cap,
  3309. ceph_cap_string(cap->issued));
  3310. }
  3311. rel->ino = cpu_to_le64(ceph_ino(inode));
  3312. rel->cap_id = cpu_to_le64(cap->cap_id);
  3313. rel->seq = cpu_to_le32(cap->seq);
  3314. rel->issue_seq = cpu_to_le32(cap->issue_seq);
  3315. rel->mseq = cpu_to_le32(cap->mseq);
  3316. rel->caps = cpu_to_le32(cap->implemented);
  3317. rel->wanted = cpu_to_le32(cap->mds_wanted);
  3318. rel->dname_len = 0;
  3319. rel->dname_seq = 0;
  3320. *p += sizeof(*rel);
  3321. ret = 1;
  3322. } else {
  3323. dout("encode_inode_release %p cap %p %s\n",
  3324. inode, cap, ceph_cap_string(cap->issued));
  3325. }
  3326. }
  3327. spin_unlock(&ci->i_ceph_lock);
  3328. return ret;
  3329. }
  3330. int ceph_encode_dentry_release(void **p, struct dentry *dentry,
  3331. int mds, int drop, int unless)
  3332. {
  3333. struct inode *dir = d_inode(dentry->d_parent);
  3334. struct ceph_mds_request_release *rel = *p;
  3335. struct ceph_dentry_info *di = ceph_dentry(dentry);
  3336. int force = 0;
  3337. int ret;
  3338. /*
  3339. * force an record for the directory caps if we have a dentry lease.
  3340. * this is racy (can't take i_ceph_lock and d_lock together), but it
  3341. * doesn't have to be perfect; the mds will revoke anything we don't
  3342. * release.
  3343. */
  3344. spin_lock(&dentry->d_lock);
  3345. if (di->lease_session && di->lease_session->s_mds == mds)
  3346. force = 1;
  3347. spin_unlock(&dentry->d_lock);
  3348. ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
  3349. spin_lock(&dentry->d_lock);
  3350. if (ret && di->lease_session && di->lease_session->s_mds == mds) {
  3351. dout("encode_dentry_release %p mds%d seq %d\n",
  3352. dentry, mds, (int)di->lease_seq);
  3353. rel->dname_len = cpu_to_le32(dentry->d_name.len);
  3354. memcpy(*p, dentry->d_name.name, dentry->d_name.len);
  3355. *p += dentry->d_name.len;
  3356. rel->dname_seq = cpu_to_le32(di->lease_seq);
  3357. __ceph_mdsc_drop_dentry_lease(dentry);
  3358. }
  3359. spin_unlock(&dentry->d_lock);
  3360. return ret;
  3361. }