caps.c 105 KB

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