caps.c 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/fs.h>
  4. #include <linux/kernel.h>
  5. #include <linux/sched/signal.h>
  6. #include <linux/slab.h>
  7. #include <linux/vmalloc.h>
  8. #include <linux/wait.h>
  9. #include <linux/writeback.h>
  10. #include <linux/iversion.h>
  11. #include <linux/filelock.h>
  12. #include <linux/jiffies.h>
  13. #include "super.h"
  14. #include "mds_client.h"
  15. #include "cache.h"
  16. #include "crypto.h"
  17. #include <linux/ceph/decode.h>
  18. #include <linux/ceph/messenger.h>
  19. /*
  20. * Capability management
  21. *
  22. * The Ceph metadata servers control client access to inode metadata
  23. * and file data by issuing capabilities, granting clients permission
  24. * to read and/or write both inode field and file data to OSDs
  25. * (storage nodes). Each capability consists of a set of bits
  26. * indicating which operations are allowed.
  27. *
  28. * If the client holds a *_SHARED cap, the client has a coherent value
  29. * that can be safely read from the cached inode.
  30. *
  31. * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
  32. * client is allowed to change inode attributes (e.g., file size,
  33. * mtime), note its dirty state in the ceph_cap, and asynchronously
  34. * flush that metadata change to the MDS.
  35. *
  36. * In the event of a conflicting operation (perhaps by another
  37. * client), the MDS will revoke the conflicting client capabilities.
  38. *
  39. * In order for a client to cache an inode, it must hold a capability
  40. * with at least one MDS server. When inodes are released, release
  41. * notifications are batched and periodically sent en masse to the MDS
  42. * cluster to release server state.
  43. */
  44. static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
  45. static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
  46. struct ceph_mds_session *session,
  47. struct ceph_inode_info *ci,
  48. u64 oldest_flush_tid);
  49. /*
  50. * Generate readable cap strings for debugging output.
  51. */
  52. #define MAX_CAP_STR 20
  53. static char cap_str[MAX_CAP_STR][40];
  54. static DEFINE_SPINLOCK(cap_str_lock);
  55. static int last_cap_str;
  56. static char *gcap_string(char *s, int c)
  57. {
  58. if (c & CEPH_CAP_GSHARED)
  59. *s++ = 's';
  60. if (c & CEPH_CAP_GEXCL)
  61. *s++ = 'x';
  62. if (c & CEPH_CAP_GCACHE)
  63. *s++ = 'c';
  64. if (c & CEPH_CAP_GRD)
  65. *s++ = 'r';
  66. if (c & CEPH_CAP_GWR)
  67. *s++ = 'w';
  68. if (c & CEPH_CAP_GBUFFER)
  69. *s++ = 'b';
  70. if (c & CEPH_CAP_GWREXTEND)
  71. *s++ = 'a';
  72. if (c & CEPH_CAP_GLAZYIO)
  73. *s++ = 'l';
  74. return s;
  75. }
  76. const char *ceph_cap_string(int caps)
  77. {
  78. int i;
  79. char *s;
  80. int c;
  81. spin_lock(&cap_str_lock);
  82. i = last_cap_str++;
  83. if (last_cap_str == MAX_CAP_STR)
  84. last_cap_str = 0;
  85. spin_unlock(&cap_str_lock);
  86. s = cap_str[i];
  87. if (caps & CEPH_CAP_PIN)
  88. *s++ = 'p';
  89. c = (caps >> CEPH_CAP_SAUTH) & 3;
  90. if (c) {
  91. *s++ = 'A';
  92. s = gcap_string(s, c);
  93. }
  94. c = (caps >> CEPH_CAP_SLINK) & 3;
  95. if (c) {
  96. *s++ = 'L';
  97. s = gcap_string(s, c);
  98. }
  99. c = (caps >> CEPH_CAP_SXATTR) & 3;
  100. if (c) {
  101. *s++ = 'X';
  102. s = gcap_string(s, c);
  103. }
  104. c = caps >> CEPH_CAP_SFILE;
  105. if (c) {
  106. *s++ = 'F';
  107. s = gcap_string(s, c);
  108. }
  109. if (s == cap_str[i])
  110. *s++ = '-';
  111. *s = 0;
  112. return cap_str[i];
  113. }
  114. void ceph_caps_init(struct ceph_mds_client *mdsc)
  115. {
  116. INIT_LIST_HEAD(&mdsc->caps_list);
  117. spin_lock_init(&mdsc->caps_list_lock);
  118. }
  119. void ceph_caps_finalize(struct ceph_mds_client *mdsc)
  120. {
  121. struct ceph_cap *cap;
  122. spin_lock(&mdsc->caps_list_lock);
  123. while (!list_empty(&mdsc->caps_list)) {
  124. cap = list_first_entry(&mdsc->caps_list,
  125. struct ceph_cap, caps_item);
  126. list_del(&cap->caps_item);
  127. kmem_cache_free(ceph_cap_cachep, cap);
  128. }
  129. mdsc->caps_total_count = 0;
  130. mdsc->caps_avail_count = 0;
  131. mdsc->caps_use_count = 0;
  132. mdsc->caps_reserve_count = 0;
  133. mdsc->caps_min_count = 0;
  134. spin_unlock(&mdsc->caps_list_lock);
  135. }
  136. void ceph_adjust_caps_max_min(struct ceph_mds_client *mdsc,
  137. struct ceph_mount_options *fsopt)
  138. {
  139. spin_lock(&mdsc->caps_list_lock);
  140. mdsc->caps_min_count = fsopt->max_readdir;
  141. if (mdsc->caps_min_count < 1024)
  142. mdsc->caps_min_count = 1024;
  143. mdsc->caps_use_max = fsopt->caps_max;
  144. if (mdsc->caps_use_max > 0 &&
  145. mdsc->caps_use_max < mdsc->caps_min_count)
  146. mdsc->caps_use_max = mdsc->caps_min_count;
  147. spin_unlock(&mdsc->caps_list_lock);
  148. }
  149. static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps)
  150. {
  151. struct ceph_cap *cap;
  152. int i;
  153. if (nr_caps) {
  154. BUG_ON(mdsc->caps_reserve_count < nr_caps);
  155. mdsc->caps_reserve_count -= nr_caps;
  156. if (mdsc->caps_avail_count >=
  157. mdsc->caps_reserve_count + mdsc->caps_min_count) {
  158. mdsc->caps_total_count -= nr_caps;
  159. for (i = 0; i < nr_caps; i++) {
  160. cap = list_first_entry(&mdsc->caps_list,
  161. struct ceph_cap, caps_item);
  162. list_del(&cap->caps_item);
  163. kmem_cache_free(ceph_cap_cachep, cap);
  164. }
  165. } else {
  166. mdsc->caps_avail_count += nr_caps;
  167. }
  168. doutc(mdsc->fsc->client,
  169. "caps %d = %d used + %d resv + %d avail\n",
  170. mdsc->caps_total_count, mdsc->caps_use_count,
  171. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  172. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  173. mdsc->caps_reserve_count +
  174. mdsc->caps_avail_count);
  175. }
  176. }
  177. /*
  178. * Called under mdsc->mutex.
  179. */
  180. int ceph_reserve_caps(struct ceph_mds_client *mdsc,
  181. struct ceph_cap_reservation *ctx, int need)
  182. {
  183. struct ceph_client *cl = mdsc->fsc->client;
  184. int i, j;
  185. struct ceph_cap *cap;
  186. int have;
  187. int alloc = 0;
  188. int max_caps;
  189. int err = 0;
  190. bool trimmed = false;
  191. struct ceph_mds_session *s;
  192. LIST_HEAD(newcaps);
  193. doutc(cl, "ctx=%p need=%d\n", ctx, need);
  194. /* first reserve any caps that are already allocated */
  195. spin_lock(&mdsc->caps_list_lock);
  196. if (mdsc->caps_avail_count >= need)
  197. have = need;
  198. else
  199. have = mdsc->caps_avail_count;
  200. mdsc->caps_avail_count -= have;
  201. mdsc->caps_reserve_count += have;
  202. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  203. mdsc->caps_reserve_count +
  204. mdsc->caps_avail_count);
  205. spin_unlock(&mdsc->caps_list_lock);
  206. for (i = have; i < need; ) {
  207. cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
  208. if (cap) {
  209. list_add(&cap->caps_item, &newcaps);
  210. alloc++;
  211. i++;
  212. continue;
  213. }
  214. if (!trimmed) {
  215. for (j = 0; j < mdsc->max_sessions; j++) {
  216. s = __ceph_lookup_mds_session(mdsc, j);
  217. if (!s)
  218. continue;
  219. mutex_unlock(&mdsc->mutex);
  220. mutex_lock(&s->s_mutex);
  221. max_caps = s->s_nr_caps - (need - i);
  222. ceph_trim_caps(mdsc, s, max_caps);
  223. mutex_unlock(&s->s_mutex);
  224. ceph_put_mds_session(s);
  225. mutex_lock(&mdsc->mutex);
  226. }
  227. trimmed = true;
  228. spin_lock(&mdsc->caps_list_lock);
  229. if (mdsc->caps_avail_count) {
  230. int more_have;
  231. if (mdsc->caps_avail_count >= need - i)
  232. more_have = need - i;
  233. else
  234. more_have = mdsc->caps_avail_count;
  235. i += more_have;
  236. have += more_have;
  237. mdsc->caps_avail_count -= more_have;
  238. mdsc->caps_reserve_count += more_have;
  239. }
  240. spin_unlock(&mdsc->caps_list_lock);
  241. continue;
  242. }
  243. pr_warn_client(cl, "ctx=%p ENOMEM need=%d got=%d\n", ctx, need,
  244. have + alloc);
  245. err = -ENOMEM;
  246. break;
  247. }
  248. if (!err) {
  249. BUG_ON(have + alloc != need);
  250. ctx->count = need;
  251. ctx->used = 0;
  252. }
  253. spin_lock(&mdsc->caps_list_lock);
  254. mdsc->caps_total_count += alloc;
  255. mdsc->caps_reserve_count += alloc;
  256. list_splice(&newcaps, &mdsc->caps_list);
  257. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  258. mdsc->caps_reserve_count +
  259. mdsc->caps_avail_count);
  260. if (err)
  261. __ceph_unreserve_caps(mdsc, have + alloc);
  262. spin_unlock(&mdsc->caps_list_lock);
  263. doutc(cl, "ctx=%p %d = %d used + %d resv + %d avail\n", ctx,
  264. mdsc->caps_total_count, mdsc->caps_use_count,
  265. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  266. return err;
  267. }
  268. void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
  269. struct ceph_cap_reservation *ctx)
  270. {
  271. struct ceph_client *cl = mdsc->fsc->client;
  272. bool reclaim = false;
  273. if (!ctx->count)
  274. return;
  275. doutc(cl, "ctx=%p count=%d\n", ctx, ctx->count);
  276. spin_lock(&mdsc->caps_list_lock);
  277. __ceph_unreserve_caps(mdsc, ctx->count);
  278. ctx->count = 0;
  279. if (mdsc->caps_use_max > 0 &&
  280. mdsc->caps_use_count > mdsc->caps_use_max)
  281. reclaim = true;
  282. spin_unlock(&mdsc->caps_list_lock);
  283. if (reclaim)
  284. ceph_reclaim_caps_nr(mdsc, ctx->used);
  285. }
  286. struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
  287. struct ceph_cap_reservation *ctx)
  288. {
  289. struct ceph_client *cl = mdsc->fsc->client;
  290. struct ceph_cap *cap = NULL;
  291. /* temporary, until we do something about cap import/export */
  292. if (!ctx) {
  293. cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
  294. if (cap) {
  295. spin_lock(&mdsc->caps_list_lock);
  296. mdsc->caps_use_count++;
  297. mdsc->caps_total_count++;
  298. spin_unlock(&mdsc->caps_list_lock);
  299. } else {
  300. spin_lock(&mdsc->caps_list_lock);
  301. if (mdsc->caps_avail_count) {
  302. BUG_ON(list_empty(&mdsc->caps_list));
  303. mdsc->caps_avail_count--;
  304. mdsc->caps_use_count++;
  305. cap = list_first_entry(&mdsc->caps_list,
  306. struct ceph_cap, caps_item);
  307. list_del(&cap->caps_item);
  308. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  309. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  310. }
  311. spin_unlock(&mdsc->caps_list_lock);
  312. }
  313. return cap;
  314. }
  315. spin_lock(&mdsc->caps_list_lock);
  316. doutc(cl, "ctx=%p (%d) %d = %d used + %d resv + %d avail\n", ctx,
  317. ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
  318. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  319. BUG_ON(!ctx->count);
  320. BUG_ON(ctx->count > mdsc->caps_reserve_count);
  321. BUG_ON(list_empty(&mdsc->caps_list));
  322. ctx->count--;
  323. ctx->used++;
  324. mdsc->caps_reserve_count--;
  325. mdsc->caps_use_count++;
  326. cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
  327. list_del(&cap->caps_item);
  328. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  329. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  330. spin_unlock(&mdsc->caps_list_lock);
  331. return cap;
  332. }
  333. void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
  334. {
  335. struct ceph_client *cl = mdsc->fsc->client;
  336. spin_lock(&mdsc->caps_list_lock);
  337. doutc(cl, "%p %d = %d used + %d resv + %d avail\n", cap,
  338. mdsc->caps_total_count, mdsc->caps_use_count,
  339. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  340. mdsc->caps_use_count--;
  341. /*
  342. * Keep some preallocated caps around (ceph_min_count), to
  343. * avoid lots of free/alloc churn.
  344. */
  345. if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
  346. mdsc->caps_min_count) {
  347. mdsc->caps_total_count--;
  348. kmem_cache_free(ceph_cap_cachep, cap);
  349. } else {
  350. mdsc->caps_avail_count++;
  351. list_add(&cap->caps_item, &mdsc->caps_list);
  352. }
  353. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  354. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  355. spin_unlock(&mdsc->caps_list_lock);
  356. }
  357. void ceph_reservation_status(struct ceph_fs_client *fsc,
  358. int *total, int *avail, int *used, int *reserved,
  359. int *min)
  360. {
  361. struct ceph_mds_client *mdsc = fsc->mdsc;
  362. spin_lock(&mdsc->caps_list_lock);
  363. if (total)
  364. *total = mdsc->caps_total_count;
  365. if (avail)
  366. *avail = mdsc->caps_avail_count;
  367. if (used)
  368. *used = mdsc->caps_use_count;
  369. if (reserved)
  370. *reserved = mdsc->caps_reserve_count;
  371. if (min)
  372. *min = mdsc->caps_min_count;
  373. spin_unlock(&mdsc->caps_list_lock);
  374. }
  375. /*
  376. * Find ceph_cap for given mds, if any.
  377. *
  378. * Called with i_ceph_lock held.
  379. */
  380. struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  381. {
  382. struct ceph_cap *cap;
  383. struct rb_node *n = ci->i_caps.rb_node;
  384. while (n) {
  385. cap = rb_entry(n, struct ceph_cap, ci_node);
  386. if (mds < cap->mds)
  387. n = n->rb_left;
  388. else if (mds > cap->mds)
  389. n = n->rb_right;
  390. else
  391. return cap;
  392. }
  393. return NULL;
  394. }
  395. struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  396. {
  397. struct ceph_cap *cap;
  398. spin_lock(&ci->i_ceph_lock);
  399. cap = __get_cap_for_mds(ci, mds);
  400. spin_unlock(&ci->i_ceph_lock);
  401. return cap;
  402. }
  403. /*
  404. * Called under i_ceph_lock.
  405. */
  406. static void __insert_cap_node(struct ceph_inode_info *ci,
  407. struct ceph_cap *new)
  408. {
  409. struct rb_node **p = &ci->i_caps.rb_node;
  410. struct rb_node *parent = NULL;
  411. struct ceph_cap *cap = NULL;
  412. while (*p) {
  413. parent = *p;
  414. cap = rb_entry(parent, struct ceph_cap, ci_node);
  415. if (new->mds < cap->mds)
  416. p = &(*p)->rb_left;
  417. else if (new->mds > cap->mds)
  418. p = &(*p)->rb_right;
  419. else
  420. BUG();
  421. }
  422. rb_link_node(&new->ci_node, parent, p);
  423. rb_insert_color(&new->ci_node, &ci->i_caps);
  424. }
  425. /*
  426. * (re)set cap hold timeouts, which control the delayed release
  427. * of unused caps back to the MDS. Should be called on cap use.
  428. */
  429. static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
  430. struct ceph_inode_info *ci)
  431. {
  432. struct inode *inode = &ci->netfs.inode;
  433. struct ceph_mount_options *opt = mdsc->fsc->mount_options;
  434. ci->i_hold_caps_max = round_jiffies(jiffies +
  435. opt->caps_wanted_delay_max * HZ);
  436. doutc(mdsc->fsc->client, "%p %llx.%llx %lu\n", inode,
  437. ceph_vinop(inode), ci->i_hold_caps_max - jiffies);
  438. }
  439. /*
  440. * (Re)queue cap at the end of the delayed cap release list.
  441. *
  442. * If I_FLUSH is set, leave the inode at the front of the list.
  443. *
  444. * Caller holds i_ceph_lock
  445. * -> we take mdsc->cap_delay_lock
  446. */
  447. static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
  448. struct ceph_inode_info *ci)
  449. {
  450. struct inode *inode = &ci->netfs.inode;
  451. doutc(mdsc->fsc->client, "%p %llx.%llx flags 0x%lx at %lu\n",
  452. inode, ceph_vinop(inode), ci->i_ceph_flags,
  453. ci->i_hold_caps_max);
  454. if (!mdsc->stopping) {
  455. spin_lock(&mdsc->cap_delay_lock);
  456. if (!list_empty(&ci->i_cap_delay_list)) {
  457. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  458. goto no_change;
  459. list_del_init(&ci->i_cap_delay_list);
  460. }
  461. __cap_set_timeouts(mdsc, ci);
  462. list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  463. no_change:
  464. spin_unlock(&mdsc->cap_delay_lock);
  465. }
  466. }
  467. /*
  468. * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
  469. * indicating we should send a cap message to flush dirty metadata
  470. * asap, and move to the front of the delayed cap list.
  471. */
  472. static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
  473. struct ceph_inode_info *ci)
  474. {
  475. struct inode *inode = &ci->netfs.inode;
  476. doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode, ceph_vinop(inode));
  477. spin_lock(&mdsc->cap_delay_lock);
  478. ci->i_ceph_flags |= CEPH_I_FLUSH;
  479. if (!list_empty(&ci->i_cap_delay_list))
  480. list_del_init(&ci->i_cap_delay_list);
  481. list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  482. spin_unlock(&mdsc->cap_delay_lock);
  483. }
  484. /*
  485. * Cancel delayed work on cap.
  486. *
  487. * Caller must hold i_ceph_lock.
  488. */
  489. static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
  490. struct ceph_inode_info *ci)
  491. {
  492. struct inode *inode = &ci->netfs.inode;
  493. doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode, ceph_vinop(inode));
  494. if (list_empty(&ci->i_cap_delay_list))
  495. return;
  496. spin_lock(&mdsc->cap_delay_lock);
  497. list_del_init(&ci->i_cap_delay_list);
  498. spin_unlock(&mdsc->cap_delay_lock);
  499. }
  500. /* Common issue checks for add_cap, handle_cap_grant. */
  501. static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
  502. unsigned issued)
  503. {
  504. struct inode *inode = &ci->netfs.inode;
  505. struct ceph_client *cl = ceph_inode_to_client(inode);
  506. unsigned had = __ceph_caps_issued(ci, NULL);
  507. lockdep_assert_held(&ci->i_ceph_lock);
  508. /*
  509. * Each time we receive FILE_CACHE anew, we increment
  510. * i_rdcache_gen.
  511. */
  512. if (S_ISREG(ci->netfs.inode.i_mode) &&
  513. (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
  514. (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
  515. ci->i_rdcache_gen++;
  516. }
  517. /*
  518. * If FILE_SHARED is newly issued, mark dir not complete. We don't
  519. * know what happened to this directory while we didn't have the cap.
  520. * If FILE_SHARED is being revoked, also mark dir not complete. It
  521. * stops on-going cached readdir.
  522. */
  523. if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
  524. if (issued & CEPH_CAP_FILE_SHARED)
  525. atomic_inc(&ci->i_shared_gen);
  526. if (S_ISDIR(ci->netfs.inode.i_mode)) {
  527. doutc(cl, " marking %p NOT complete\n", inode);
  528. __ceph_dir_clear_complete(ci);
  529. }
  530. }
  531. /* Wipe saved layout if we're losing DIR_CREATE caps */
  532. if (S_ISDIR(ci->netfs.inode.i_mode) && (had & CEPH_CAP_DIR_CREATE) &&
  533. !(issued & CEPH_CAP_DIR_CREATE)) {
  534. ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
  535. memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
  536. }
  537. }
  538. /**
  539. * change_auth_cap_ses - move inode to appropriate lists when auth caps change
  540. * @ci: inode to be moved
  541. * @session: new auth caps session
  542. */
  543. void change_auth_cap_ses(struct ceph_inode_info *ci,
  544. struct ceph_mds_session *session)
  545. {
  546. lockdep_assert_held(&ci->i_ceph_lock);
  547. if (list_empty(&ci->i_dirty_item) && list_empty(&ci->i_flushing_item))
  548. return;
  549. spin_lock(&session->s_mdsc->cap_dirty_lock);
  550. if (!list_empty(&ci->i_dirty_item))
  551. list_move(&ci->i_dirty_item, &session->s_cap_dirty);
  552. if (!list_empty(&ci->i_flushing_item))
  553. list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  554. spin_unlock(&session->s_mdsc->cap_dirty_lock);
  555. }
  556. /*
  557. * Add a capability under the given MDS session.
  558. *
  559. * Caller should hold session snap_rwsem (read) and ci->i_ceph_lock
  560. *
  561. * @fmode is the open file mode, if we are opening a file, otherwise
  562. * it is < 0. (This is so we can atomically add the cap and add an
  563. * open file reference to it.)
  564. */
  565. void ceph_add_cap(struct inode *inode,
  566. struct ceph_mds_session *session, u64 cap_id,
  567. unsigned issued, unsigned wanted,
  568. unsigned seq, unsigned mseq, u64 realmino, int flags,
  569. struct ceph_cap **new_cap)
  570. {
  571. struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
  572. struct ceph_client *cl = ceph_inode_to_client(inode);
  573. struct ceph_inode_info *ci = ceph_inode(inode);
  574. struct ceph_cap *cap;
  575. int mds = session->s_mds;
  576. int actual_wanted;
  577. u32 gen;
  578. lockdep_assert_held(&ci->i_ceph_lock);
  579. doutc(cl, "%p %llx.%llx mds%d cap %llx %s seq %d\n", inode,
  580. ceph_vinop(inode), session->s_mds, cap_id,
  581. ceph_cap_string(issued), seq);
  582. gen = atomic_read(&session->s_cap_gen);
  583. cap = __get_cap_for_mds(ci, mds);
  584. if (!cap) {
  585. cap = *new_cap;
  586. *new_cap = NULL;
  587. cap->issued = 0;
  588. cap->implemented = 0;
  589. cap->mds = mds;
  590. cap->mds_wanted = 0;
  591. cap->mseq = 0;
  592. cap->ci = ci;
  593. __insert_cap_node(ci, cap);
  594. /* add to session cap list */
  595. cap->session = session;
  596. spin_lock(&session->s_cap_lock);
  597. list_add_tail(&cap->session_caps, &session->s_caps);
  598. session->s_nr_caps++;
  599. atomic64_inc(&mdsc->metric.total_caps);
  600. spin_unlock(&session->s_cap_lock);
  601. } else {
  602. spin_lock(&session->s_cap_lock);
  603. list_move_tail(&cap->session_caps, &session->s_caps);
  604. spin_unlock(&session->s_cap_lock);
  605. if (cap->cap_gen < gen)
  606. cap->issued = cap->implemented = CEPH_CAP_PIN;
  607. /*
  608. * auth mds of the inode changed. we received the cap export
  609. * message, but still haven't received the cap import message.
  610. * handle_cap_export() updated the new auth MDS' cap.
  611. *
  612. * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
  613. * a message that was send before the cap import message. So
  614. * don't remove caps.
  615. */
  616. if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  617. WARN_ON(cap != ci->i_auth_cap);
  618. WARN_ON(cap->cap_id != cap_id);
  619. seq = cap->seq;
  620. mseq = cap->mseq;
  621. issued |= cap->issued;
  622. flags |= CEPH_CAP_FLAG_AUTH;
  623. }
  624. }
  625. if (!ci->i_snap_realm ||
  626. ((flags & CEPH_CAP_FLAG_AUTH) &&
  627. realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
  628. /*
  629. * add this inode to the appropriate snap realm
  630. */
  631. struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
  632. realmino);
  633. if (realm)
  634. ceph_change_snap_realm(inode, realm);
  635. else
  636. WARN(1, "%s: couldn't find snap realm 0x%llx (ino 0x%llx oldrealm 0x%llx)\n",
  637. __func__, realmino, ci->i_vino.ino,
  638. ci->i_snap_realm ? ci->i_snap_realm->ino : 0);
  639. }
  640. __check_cap_issue(ci, cap, issued);
  641. /*
  642. * If we are issued caps we don't want, or the mds' wanted
  643. * value appears to be off, queue a check so we'll release
  644. * later and/or update the mds wanted value.
  645. */
  646. actual_wanted = __ceph_caps_wanted(ci);
  647. if ((wanted & ~actual_wanted) ||
  648. (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
  649. doutc(cl, "issued %s, mds wanted %s, actual %s, queueing\n",
  650. ceph_cap_string(issued), ceph_cap_string(wanted),
  651. ceph_cap_string(actual_wanted));
  652. __cap_delay_requeue(mdsc, ci);
  653. }
  654. if (flags & CEPH_CAP_FLAG_AUTH) {
  655. if (!ci->i_auth_cap ||
  656. ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
  657. if (ci->i_auth_cap &&
  658. ci->i_auth_cap->session != cap->session)
  659. change_auth_cap_ses(ci, cap->session);
  660. ci->i_auth_cap = cap;
  661. cap->mds_wanted = wanted;
  662. }
  663. } else {
  664. WARN_ON(ci->i_auth_cap == cap);
  665. }
  666. doutc(cl, "inode %p %llx.%llx cap %p %s now %s seq %d mds%d\n",
  667. inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
  668. ceph_cap_string(issued|cap->issued), seq, mds);
  669. cap->cap_id = cap_id;
  670. cap->issued = issued;
  671. cap->implemented |= issued;
  672. if (ceph_seq_cmp(mseq, cap->mseq) > 0)
  673. cap->mds_wanted = wanted;
  674. else
  675. cap->mds_wanted |= wanted;
  676. cap->seq = seq;
  677. cap->issue_seq = seq;
  678. cap->mseq = mseq;
  679. cap->cap_gen = gen;
  680. wake_up_all(&ci->i_cap_wq);
  681. }
  682. /*
  683. * Return true if cap has not timed out and belongs to the current
  684. * generation of the MDS session (i.e. has not gone 'stale' due to
  685. * us losing touch with the mds).
  686. */
  687. static int __cap_is_valid(struct ceph_cap *cap)
  688. {
  689. struct inode *inode = &cap->ci->netfs.inode;
  690. struct ceph_client *cl = cap->session->s_mdsc->fsc->client;
  691. unsigned long ttl;
  692. u32 gen;
  693. gen = atomic_read(&cap->session->s_cap_gen);
  694. ttl = cap->session->s_cap_ttl;
  695. if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
  696. doutc(cl, "%p %llx.%llx cap %p issued %s but STALE (gen %u vs %u)\n",
  697. inode, ceph_vinop(inode), cap,
  698. ceph_cap_string(cap->issued), cap->cap_gen, gen);
  699. return 0;
  700. }
  701. return 1;
  702. }
  703. /*
  704. * Return set of valid cap bits issued to us. Note that caps time
  705. * out, and may be invalidated in bulk if the client session times out
  706. * and session->s_cap_gen is bumped.
  707. */
  708. int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
  709. {
  710. struct inode *inode = &ci->netfs.inode;
  711. struct ceph_client *cl = ceph_inode_to_client(inode);
  712. int have = ci->i_snap_caps;
  713. struct ceph_cap *cap;
  714. struct rb_node *p;
  715. if (implemented)
  716. *implemented = 0;
  717. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  718. cap = rb_entry(p, struct ceph_cap, ci_node);
  719. if (!__cap_is_valid(cap))
  720. continue;
  721. doutc(cl, "%p %llx.%llx cap %p issued %s\n", inode,
  722. ceph_vinop(inode), cap, ceph_cap_string(cap->issued));
  723. have |= cap->issued;
  724. if (implemented)
  725. *implemented |= cap->implemented;
  726. }
  727. /*
  728. * exclude caps issued by non-auth MDS, but are been revoking
  729. * by the auth MDS. The non-auth MDS should be revoking/exporting
  730. * these caps, but the message is delayed.
  731. */
  732. if (ci->i_auth_cap) {
  733. cap = ci->i_auth_cap;
  734. have &= ~cap->implemented | cap->issued;
  735. }
  736. return have;
  737. }
  738. /*
  739. * Get cap bits issued by caps other than @ocap
  740. */
  741. int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
  742. {
  743. int have = ci->i_snap_caps;
  744. struct ceph_cap *cap;
  745. struct rb_node *p;
  746. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  747. cap = rb_entry(p, struct ceph_cap, ci_node);
  748. if (cap == ocap)
  749. continue;
  750. if (!__cap_is_valid(cap))
  751. continue;
  752. have |= cap->issued;
  753. }
  754. return have;
  755. }
  756. /*
  757. * Move a cap to the end of the LRU (oldest caps at list head, newest
  758. * at list tail).
  759. */
  760. static void __touch_cap(struct ceph_cap *cap)
  761. {
  762. struct inode *inode = &cap->ci->netfs.inode;
  763. struct ceph_mds_session *s = cap->session;
  764. struct ceph_client *cl = s->s_mdsc->fsc->client;
  765. spin_lock(&s->s_cap_lock);
  766. if (!s->s_cap_iterator) {
  767. doutc(cl, "%p %llx.%llx cap %p mds%d\n", inode,
  768. ceph_vinop(inode), cap, s->s_mds);
  769. list_move_tail(&cap->session_caps, &s->s_caps);
  770. } else {
  771. doutc(cl, "%p %llx.%llx cap %p mds%d NOP, iterating over caps\n",
  772. inode, ceph_vinop(inode), cap, s->s_mds);
  773. }
  774. spin_unlock(&s->s_cap_lock);
  775. }
  776. /*
  777. * Check if we hold the given mask. If so, move the cap(s) to the
  778. * front of their respective LRUs. (This is the preferred way for
  779. * callers to check for caps they want.)
  780. */
  781. int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
  782. {
  783. struct inode *inode = &ci->netfs.inode;
  784. struct ceph_client *cl = ceph_inode_to_client(inode);
  785. struct ceph_cap *cap;
  786. struct rb_node *p;
  787. int have = ci->i_snap_caps;
  788. if ((have & mask) == mask) {
  789. doutc(cl, "mask %p %llx.%llx snap issued %s (mask %s)\n",
  790. inode, ceph_vinop(inode), ceph_cap_string(have),
  791. ceph_cap_string(mask));
  792. return 1;
  793. }
  794. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  795. cap = rb_entry(p, struct ceph_cap, ci_node);
  796. if (!__cap_is_valid(cap))
  797. continue;
  798. if ((cap->issued & mask) == mask) {
  799. doutc(cl, "mask %p %llx.%llx cap %p issued %s (mask %s)\n",
  800. inode, ceph_vinop(inode), cap,
  801. ceph_cap_string(cap->issued),
  802. ceph_cap_string(mask));
  803. if (touch)
  804. __touch_cap(cap);
  805. return 1;
  806. }
  807. /* does a combination of caps satisfy mask? */
  808. have |= cap->issued;
  809. if ((have & mask) == mask) {
  810. doutc(cl, "mask %p %llx.%llx combo issued %s (mask %s)\n",
  811. inode, ceph_vinop(inode),
  812. ceph_cap_string(cap->issued),
  813. ceph_cap_string(mask));
  814. if (touch) {
  815. struct rb_node *q;
  816. /* touch this + preceding caps */
  817. __touch_cap(cap);
  818. for (q = rb_first(&ci->i_caps); q != p;
  819. q = rb_next(q)) {
  820. cap = rb_entry(q, struct ceph_cap,
  821. ci_node);
  822. if (!__cap_is_valid(cap))
  823. continue;
  824. if (cap->issued & mask)
  825. __touch_cap(cap);
  826. }
  827. }
  828. return 1;
  829. }
  830. }
  831. return 0;
  832. }
  833. int __ceph_caps_issued_mask_metric(struct ceph_inode_info *ci, int mask,
  834. int touch)
  835. {
  836. struct ceph_fs_client *fsc = ceph_sb_to_fs_client(ci->netfs.inode.i_sb);
  837. int r;
  838. r = __ceph_caps_issued_mask(ci, mask, touch);
  839. if (r)
  840. ceph_update_cap_hit(&fsc->mdsc->metric);
  841. else
  842. ceph_update_cap_mis(&fsc->mdsc->metric);
  843. return r;
  844. }
  845. /*
  846. * Return true if mask caps are currently being revoked by an MDS.
  847. */
  848. int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
  849. struct ceph_cap *ocap, int mask)
  850. {
  851. struct ceph_cap *cap;
  852. struct rb_node *p;
  853. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  854. cap = rb_entry(p, struct ceph_cap, ci_node);
  855. if (cap != ocap &&
  856. (cap->implemented & ~cap->issued & mask))
  857. return 1;
  858. }
  859. return 0;
  860. }
  861. int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
  862. {
  863. struct inode *inode = &ci->netfs.inode;
  864. struct ceph_client *cl = ceph_inode_to_client(inode);
  865. int ret;
  866. spin_lock(&ci->i_ceph_lock);
  867. ret = __ceph_caps_revoking_other(ci, NULL, mask);
  868. spin_unlock(&ci->i_ceph_lock);
  869. doutc(cl, "%p %llx.%llx %s = %d\n", inode, ceph_vinop(inode),
  870. ceph_cap_string(mask), ret);
  871. return ret;
  872. }
  873. int __ceph_caps_used(struct ceph_inode_info *ci)
  874. {
  875. int used = 0;
  876. if (ci->i_pin_ref)
  877. used |= CEPH_CAP_PIN;
  878. if (ci->i_rd_ref)
  879. used |= CEPH_CAP_FILE_RD;
  880. if (ci->i_rdcache_ref ||
  881. (S_ISREG(ci->netfs.inode.i_mode) &&
  882. ci->netfs.inode.i_data.nrpages))
  883. used |= CEPH_CAP_FILE_CACHE;
  884. if (ci->i_wr_ref)
  885. used |= CEPH_CAP_FILE_WR;
  886. if (ci->i_wb_ref || ci->i_wrbuffer_ref)
  887. used |= CEPH_CAP_FILE_BUFFER;
  888. if (ci->i_fx_ref)
  889. used |= CEPH_CAP_FILE_EXCL;
  890. return used;
  891. }
  892. #define FMODE_WAIT_BIAS 1000
  893. /*
  894. * wanted, by virtue of open file modes
  895. */
  896. int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
  897. {
  898. const int PIN_SHIFT = ffs(CEPH_FILE_MODE_PIN);
  899. const int RD_SHIFT = ffs(CEPH_FILE_MODE_RD);
  900. const int WR_SHIFT = ffs(CEPH_FILE_MODE_WR);
  901. const int LAZY_SHIFT = ffs(CEPH_FILE_MODE_LAZY);
  902. struct ceph_mount_options *opt =
  903. ceph_inode_to_fs_client(&ci->netfs.inode)->mount_options;
  904. unsigned long used_cutoff = jiffies - opt->caps_wanted_delay_max * HZ;
  905. unsigned long idle_cutoff = jiffies - opt->caps_wanted_delay_min * HZ;
  906. if (S_ISDIR(ci->netfs.inode.i_mode)) {
  907. int want = 0;
  908. /* use used_cutoff here, to keep dir's wanted caps longer */
  909. if (ci->i_nr_by_mode[RD_SHIFT] > 0 ||
  910. time_after(ci->i_last_rd, used_cutoff))
  911. want |= CEPH_CAP_ANY_SHARED;
  912. if (ci->i_nr_by_mode[WR_SHIFT] > 0 ||
  913. time_after(ci->i_last_wr, used_cutoff)) {
  914. want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
  915. if (opt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS)
  916. want |= CEPH_CAP_ANY_DIR_OPS;
  917. }
  918. if (want || ci->i_nr_by_mode[PIN_SHIFT] > 0)
  919. want |= CEPH_CAP_PIN;
  920. return want;
  921. } else {
  922. int bits = 0;
  923. if (ci->i_nr_by_mode[RD_SHIFT] > 0) {
  924. if (ci->i_nr_by_mode[RD_SHIFT] >= FMODE_WAIT_BIAS ||
  925. time_after(ci->i_last_rd, used_cutoff))
  926. bits |= 1 << RD_SHIFT;
  927. } else if (time_after(ci->i_last_rd, idle_cutoff)) {
  928. bits |= 1 << RD_SHIFT;
  929. }
  930. if (ci->i_nr_by_mode[WR_SHIFT] > 0) {
  931. if (ci->i_nr_by_mode[WR_SHIFT] >= FMODE_WAIT_BIAS ||
  932. time_after(ci->i_last_wr, used_cutoff))
  933. bits |= 1 << WR_SHIFT;
  934. } else if (time_after(ci->i_last_wr, idle_cutoff)) {
  935. bits |= 1 << WR_SHIFT;
  936. }
  937. /* check lazyio only when read/write is wanted */
  938. if ((bits & (CEPH_FILE_MODE_RDWR << 1)) &&
  939. ci->i_nr_by_mode[LAZY_SHIFT] > 0)
  940. bits |= 1 << LAZY_SHIFT;
  941. return bits ? ceph_caps_for_mode(bits >> 1) : 0;
  942. }
  943. }
  944. /*
  945. * wanted, by virtue of open file modes AND cap refs (buffered/cached data)
  946. */
  947. int __ceph_caps_wanted(struct ceph_inode_info *ci)
  948. {
  949. int w = __ceph_caps_file_wanted(ci) | __ceph_caps_used(ci);
  950. if (S_ISDIR(ci->netfs.inode.i_mode)) {
  951. /* we want EXCL if holding caps of dir ops */
  952. if (w & CEPH_CAP_ANY_DIR_OPS)
  953. w |= CEPH_CAP_FILE_EXCL;
  954. } else {
  955. /* we want EXCL if dirty data */
  956. if (w & CEPH_CAP_FILE_BUFFER)
  957. w |= CEPH_CAP_FILE_EXCL;
  958. }
  959. return w;
  960. }
  961. /*
  962. * Return caps we have registered with the MDS(s) as 'wanted'.
  963. */
  964. int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
  965. {
  966. struct ceph_cap *cap;
  967. struct rb_node *p;
  968. int mds_wanted = 0;
  969. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  970. cap = rb_entry(p, struct ceph_cap, ci_node);
  971. if (check && !__cap_is_valid(cap))
  972. continue;
  973. if (cap == ci->i_auth_cap)
  974. mds_wanted |= cap->mds_wanted;
  975. else
  976. mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
  977. }
  978. return mds_wanted;
  979. }
  980. int ceph_is_any_caps(struct inode *inode)
  981. {
  982. struct ceph_inode_info *ci = ceph_inode(inode);
  983. int ret;
  984. spin_lock(&ci->i_ceph_lock);
  985. ret = __ceph_is_any_real_caps(ci);
  986. spin_unlock(&ci->i_ceph_lock);
  987. return ret;
  988. }
  989. /*
  990. * Remove a cap. Take steps to deal with a racing iterate_session_caps.
  991. *
  992. * caller should hold i_ceph_lock.
  993. * caller will not hold session s_mutex if called from destroy_inode.
  994. */
  995. void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
  996. {
  997. struct ceph_mds_session *session = cap->session;
  998. struct ceph_client *cl = session->s_mdsc->fsc->client;
  999. struct ceph_inode_info *ci = cap->ci;
  1000. struct inode *inode = &ci->netfs.inode;
  1001. struct ceph_mds_client *mdsc;
  1002. int removed = 0;
  1003. /* 'ci' being NULL means the remove have already occurred */
  1004. if (!ci) {
  1005. doutc(cl, "inode is NULL\n");
  1006. return;
  1007. }
  1008. lockdep_assert_held(&ci->i_ceph_lock);
  1009. doutc(cl, "%p from %p %llx.%llx\n", cap, inode, ceph_vinop(inode));
  1010. mdsc = ceph_inode_to_fs_client(&ci->netfs.inode)->mdsc;
  1011. /* remove from inode's cap rbtree, and clear auth cap */
  1012. rb_erase(&cap->ci_node, &ci->i_caps);
  1013. if (ci->i_auth_cap == cap)
  1014. ci->i_auth_cap = NULL;
  1015. /* remove from session list */
  1016. spin_lock(&session->s_cap_lock);
  1017. if (session->s_cap_iterator == cap) {
  1018. /* not yet, we are iterating over this very cap */
  1019. doutc(cl, "delaying %p removal from session %p\n", cap,
  1020. cap->session);
  1021. } else {
  1022. list_del_init(&cap->session_caps);
  1023. session->s_nr_caps--;
  1024. atomic64_dec(&mdsc->metric.total_caps);
  1025. cap->session = NULL;
  1026. removed = 1;
  1027. }
  1028. /* protect backpointer with s_cap_lock: see iterate_session_caps */
  1029. cap->ci = NULL;
  1030. /*
  1031. * s_cap_reconnect is protected by s_cap_lock. no one changes
  1032. * s_cap_gen while session is in the reconnect state.
  1033. */
  1034. if (queue_release &&
  1035. (!session->s_cap_reconnect ||
  1036. cap->cap_gen == atomic_read(&session->s_cap_gen))) {
  1037. cap->queue_release = 1;
  1038. if (removed) {
  1039. __ceph_queue_cap_release(session, cap);
  1040. removed = 0;
  1041. }
  1042. } else {
  1043. cap->queue_release = 0;
  1044. }
  1045. cap->cap_ino = ci->i_vino.ino;
  1046. spin_unlock(&session->s_cap_lock);
  1047. if (removed)
  1048. ceph_put_cap(mdsc, cap);
  1049. if (!__ceph_is_any_real_caps(ci)) {
  1050. /* when reconnect denied, we remove session caps forcibly,
  1051. * i_wr_ref can be non-zero. If there are ongoing write,
  1052. * keep i_snap_realm.
  1053. */
  1054. if (ci->i_wr_ref == 0 && ci->i_snap_realm)
  1055. ceph_change_snap_realm(&ci->netfs.inode, NULL);
  1056. __cap_delay_cancel(mdsc, ci);
  1057. }
  1058. }
  1059. void ceph_remove_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
  1060. bool queue_release)
  1061. {
  1062. struct ceph_inode_info *ci = cap->ci;
  1063. struct ceph_fs_client *fsc;
  1064. /* 'ci' being NULL means the remove have already occurred */
  1065. if (!ci) {
  1066. doutc(mdsc->fsc->client, "inode is NULL\n");
  1067. return;
  1068. }
  1069. lockdep_assert_held(&ci->i_ceph_lock);
  1070. fsc = ceph_inode_to_fs_client(&ci->netfs.inode);
  1071. WARN_ON_ONCE(ci->i_auth_cap == cap &&
  1072. !list_empty(&ci->i_dirty_item) &&
  1073. !fsc->blocklisted &&
  1074. !ceph_inode_is_shutdown(&ci->netfs.inode));
  1075. __ceph_remove_cap(cap, queue_release);
  1076. }
  1077. struct cap_msg_args {
  1078. struct ceph_mds_session *session;
  1079. u64 ino, cid, follows;
  1080. u64 flush_tid, oldest_flush_tid, size, max_size;
  1081. u64 xattr_version;
  1082. u64 change_attr;
  1083. struct ceph_buffer *xattr_buf;
  1084. struct ceph_buffer *old_xattr_buf;
  1085. struct timespec64 atime, mtime, ctime, btime;
  1086. int op, caps, wanted, dirty;
  1087. u32 seq, issue_seq, mseq, time_warp_seq;
  1088. u32 flags;
  1089. kuid_t uid;
  1090. kgid_t gid;
  1091. umode_t mode;
  1092. bool inline_data;
  1093. bool wake;
  1094. bool encrypted;
  1095. u32 fscrypt_auth_len;
  1096. u8 fscrypt_auth[sizeof(struct ceph_fscrypt_auth)]; // for context
  1097. };
  1098. /* Marshal up the cap msg to the MDS */
  1099. static void encode_cap_msg(struct ceph_msg *msg, struct cap_msg_args *arg)
  1100. {
  1101. struct ceph_mds_caps *fc;
  1102. void *p;
  1103. struct ceph_mds_client *mdsc = arg->session->s_mdsc;
  1104. struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
  1105. doutc(mdsc->fsc->client,
  1106. "%s %llx %llx caps %s wanted %s dirty %s seq %u/%u"
  1107. " tid %llu/%llu mseq %u follows %lld size %llu/%llu"
  1108. " xattr_ver %llu xattr_len %d\n",
  1109. ceph_cap_op_name(arg->op), arg->cid, arg->ino,
  1110. ceph_cap_string(arg->caps), ceph_cap_string(arg->wanted),
  1111. ceph_cap_string(arg->dirty), arg->seq, arg->issue_seq,
  1112. arg->flush_tid, arg->oldest_flush_tid, arg->mseq, arg->follows,
  1113. arg->size, arg->max_size, arg->xattr_version,
  1114. arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
  1115. msg->hdr.version = cpu_to_le16(12);
  1116. msg->hdr.tid = cpu_to_le64(arg->flush_tid);
  1117. fc = msg->front.iov_base;
  1118. memset(fc, 0, sizeof(*fc));
  1119. fc->cap_id = cpu_to_le64(arg->cid);
  1120. fc->op = cpu_to_le32(arg->op);
  1121. fc->seq = cpu_to_le32(arg->seq);
  1122. fc->issue_seq = cpu_to_le32(arg->issue_seq);
  1123. fc->migrate_seq = cpu_to_le32(arg->mseq);
  1124. fc->caps = cpu_to_le32(arg->caps);
  1125. fc->wanted = cpu_to_le32(arg->wanted);
  1126. fc->dirty = cpu_to_le32(arg->dirty);
  1127. fc->ino = cpu_to_le64(arg->ino);
  1128. fc->snap_follows = cpu_to_le64(arg->follows);
  1129. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  1130. if (arg->encrypted)
  1131. fc->size = cpu_to_le64(round_up(arg->size,
  1132. CEPH_FSCRYPT_BLOCK_SIZE));
  1133. else
  1134. #endif
  1135. fc->size = cpu_to_le64(arg->size);
  1136. fc->max_size = cpu_to_le64(arg->max_size);
  1137. ceph_encode_timespec64(&fc->mtime, &arg->mtime);
  1138. ceph_encode_timespec64(&fc->atime, &arg->atime);
  1139. ceph_encode_timespec64(&fc->ctime, &arg->ctime);
  1140. fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
  1141. fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
  1142. fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
  1143. fc->mode = cpu_to_le32(arg->mode);
  1144. fc->xattr_version = cpu_to_le64(arg->xattr_version);
  1145. if (arg->xattr_buf) {
  1146. msg->middle = ceph_buffer_get(arg->xattr_buf);
  1147. fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
  1148. msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
  1149. }
  1150. p = fc + 1;
  1151. /* flock buffer size (version 2) */
  1152. ceph_encode_32(&p, 0);
  1153. /* inline version (version 4) */
  1154. ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
  1155. /* inline data size */
  1156. ceph_encode_32(&p, 0);
  1157. /*
  1158. * osd_epoch_barrier (version 5)
  1159. * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
  1160. * case it was recently changed
  1161. */
  1162. ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
  1163. /* oldest_flush_tid (version 6) */
  1164. ceph_encode_64(&p, arg->oldest_flush_tid);
  1165. /*
  1166. * caller_uid/caller_gid (version 7)
  1167. *
  1168. * Currently, we don't properly track which caller dirtied the caps
  1169. * last, and force a flush of them when there is a conflict. For now,
  1170. * just set this to 0:0, to emulate how the MDS has worked up to now.
  1171. */
  1172. ceph_encode_32(&p, 0);
  1173. ceph_encode_32(&p, 0);
  1174. /* pool namespace (version 8) (mds always ignores this) */
  1175. ceph_encode_32(&p, 0);
  1176. /* btime and change_attr (version 9) */
  1177. ceph_encode_timespec64(p, &arg->btime);
  1178. p += sizeof(struct ceph_timespec);
  1179. ceph_encode_64(&p, arg->change_attr);
  1180. /* Advisory flags (version 10) */
  1181. ceph_encode_32(&p, arg->flags);
  1182. /* dirstats (version 11) - these are r/o on the client */
  1183. ceph_encode_64(&p, 0);
  1184. ceph_encode_64(&p, 0);
  1185. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  1186. /*
  1187. * fscrypt_auth and fscrypt_file (version 12)
  1188. *
  1189. * fscrypt_auth holds the crypto context (if any). fscrypt_file
  1190. * tracks the real i_size as an __le64 field (and we use a rounded-up
  1191. * i_size in the traditional size field).
  1192. */
  1193. ceph_encode_32(&p, arg->fscrypt_auth_len);
  1194. ceph_encode_copy(&p, arg->fscrypt_auth, arg->fscrypt_auth_len);
  1195. ceph_encode_32(&p, sizeof(__le64));
  1196. ceph_encode_64(&p, arg->size);
  1197. #else /* CONFIG_FS_ENCRYPTION */
  1198. ceph_encode_32(&p, 0);
  1199. ceph_encode_32(&p, 0);
  1200. #endif /* CONFIG_FS_ENCRYPTION */
  1201. }
  1202. /*
  1203. * Queue cap releases when an inode is dropped from our cache.
  1204. */
  1205. void __ceph_remove_caps(struct ceph_inode_info *ci)
  1206. {
  1207. struct inode *inode = &ci->netfs.inode;
  1208. struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
  1209. struct rb_node *p;
  1210. /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
  1211. * may call __ceph_caps_issued_mask() on a freeing inode. */
  1212. spin_lock(&ci->i_ceph_lock);
  1213. p = rb_first(&ci->i_caps);
  1214. while (p) {
  1215. struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
  1216. p = rb_next(p);
  1217. ceph_remove_cap(mdsc, cap, true);
  1218. }
  1219. spin_unlock(&ci->i_ceph_lock);
  1220. }
  1221. /*
  1222. * Prepare to send a cap message to an MDS. Update the cap state, and populate
  1223. * the arg struct with the parameters that will need to be sent. This should
  1224. * be done under the i_ceph_lock to guard against changes to cap state.
  1225. *
  1226. * Make note of max_size reported/requested from mds, revoked caps
  1227. * that have now been implemented.
  1228. */
  1229. static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,
  1230. int op, int flags, int used, int want, int retain,
  1231. int flushing, u64 flush_tid, u64 oldest_flush_tid)
  1232. {
  1233. struct ceph_inode_info *ci = cap->ci;
  1234. struct inode *inode = &ci->netfs.inode;
  1235. struct ceph_client *cl = ceph_inode_to_client(inode);
  1236. int held, revoking;
  1237. lockdep_assert_held(&ci->i_ceph_lock);
  1238. held = cap->issued | cap->implemented;
  1239. revoking = cap->implemented & ~cap->issued;
  1240. retain &= ~revoking;
  1241. doutc(cl, "%p %llx.%llx cap %p session %p %s -> %s (revoking %s)\n",
  1242. inode, ceph_vinop(inode), cap, cap->session,
  1243. ceph_cap_string(held), ceph_cap_string(held & retain),
  1244. ceph_cap_string(revoking));
  1245. BUG_ON((retain & CEPH_CAP_PIN) == 0);
  1246. ci->i_ceph_flags &= ~CEPH_I_FLUSH;
  1247. cap->issued &= retain; /* drop bits we don't want */
  1248. /*
  1249. * Wake up any waiters on wanted -> needed transition. This is due to
  1250. * the weird transition from buffered to sync IO... we need to flush
  1251. * dirty pages _before_ allowing sync writes to avoid reordering.
  1252. */
  1253. arg->wake = cap->implemented & ~cap->issued;
  1254. cap->implemented &= cap->issued | used;
  1255. cap->mds_wanted = want;
  1256. arg->session = cap->session;
  1257. arg->ino = ceph_vino(inode).ino;
  1258. arg->cid = cap->cap_id;
  1259. arg->follows = flushing ? ci->i_head_snapc->seq : 0;
  1260. arg->flush_tid = flush_tid;
  1261. arg->oldest_flush_tid = oldest_flush_tid;
  1262. arg->size = i_size_read(inode);
  1263. ci->i_reported_size = arg->size;
  1264. arg->max_size = ci->i_wanted_max_size;
  1265. if (cap == ci->i_auth_cap) {
  1266. if (want & CEPH_CAP_ANY_FILE_WR)
  1267. ci->i_requested_max_size = arg->max_size;
  1268. else
  1269. ci->i_requested_max_size = 0;
  1270. }
  1271. if (flushing & CEPH_CAP_XATTR_EXCL) {
  1272. arg->old_xattr_buf = __ceph_build_xattrs_blob(ci);
  1273. arg->xattr_version = ci->i_xattrs.version;
  1274. arg->xattr_buf = ceph_buffer_get(ci->i_xattrs.blob);
  1275. } else {
  1276. arg->xattr_buf = NULL;
  1277. arg->old_xattr_buf = NULL;
  1278. }
  1279. arg->mtime = inode_get_mtime(inode);
  1280. arg->atime = inode_get_atime(inode);
  1281. arg->ctime = inode_get_ctime(inode);
  1282. arg->btime = ci->i_btime;
  1283. arg->change_attr = inode_peek_iversion_raw(inode);
  1284. arg->op = op;
  1285. arg->caps = cap->implemented;
  1286. arg->wanted = want;
  1287. arg->dirty = flushing;
  1288. arg->seq = cap->seq;
  1289. arg->issue_seq = cap->issue_seq;
  1290. arg->mseq = cap->mseq;
  1291. arg->time_warp_seq = ci->i_time_warp_seq;
  1292. arg->uid = inode->i_uid;
  1293. arg->gid = inode->i_gid;
  1294. arg->mode = inode->i_mode;
  1295. arg->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
  1296. if (!(flags & CEPH_CLIENT_CAPS_PENDING_CAPSNAP) &&
  1297. !list_empty(&ci->i_cap_snaps)) {
  1298. struct ceph_cap_snap *capsnap;
  1299. list_for_each_entry_reverse(capsnap, &ci->i_cap_snaps, ci_item) {
  1300. if (capsnap->cap_flush.tid)
  1301. break;
  1302. if (capsnap->need_flush) {
  1303. flags |= CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
  1304. break;
  1305. }
  1306. }
  1307. }
  1308. arg->flags = flags;
  1309. arg->encrypted = IS_ENCRYPTED(inode);
  1310. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  1311. if (ci->fscrypt_auth_len &&
  1312. WARN_ON_ONCE(ci->fscrypt_auth_len > sizeof(struct ceph_fscrypt_auth))) {
  1313. /* Don't set this if it's too big */
  1314. arg->fscrypt_auth_len = 0;
  1315. } else {
  1316. arg->fscrypt_auth_len = ci->fscrypt_auth_len;
  1317. memcpy(arg->fscrypt_auth, ci->fscrypt_auth,
  1318. min_t(size_t, ci->fscrypt_auth_len,
  1319. sizeof(arg->fscrypt_auth)));
  1320. }
  1321. #endif /* CONFIG_FS_ENCRYPTION */
  1322. }
  1323. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  1324. #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \
  1325. 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4 + 8)
  1326. static inline int cap_msg_size(struct cap_msg_args *arg)
  1327. {
  1328. return CAP_MSG_FIXED_FIELDS + arg->fscrypt_auth_len;
  1329. }
  1330. #else
  1331. #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \
  1332. 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4)
  1333. static inline int cap_msg_size(struct cap_msg_args *arg)
  1334. {
  1335. return CAP_MSG_FIXED_FIELDS;
  1336. }
  1337. #endif /* CONFIG_FS_ENCRYPTION */
  1338. /*
  1339. * Send a cap msg on the given inode.
  1340. *
  1341. * Caller should hold snap_rwsem (read), s_mutex.
  1342. */
  1343. static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci)
  1344. {
  1345. struct ceph_msg *msg;
  1346. struct inode *inode = &ci->netfs.inode;
  1347. struct ceph_client *cl = ceph_inode_to_client(inode);
  1348. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(arg), GFP_NOFS,
  1349. false);
  1350. if (!msg) {
  1351. pr_err_client(cl,
  1352. "error allocating cap msg: ino (%llx.%llx)"
  1353. " flushing %s tid %llu, requeuing cap.\n",
  1354. ceph_vinop(inode), ceph_cap_string(arg->dirty),
  1355. arg->flush_tid);
  1356. spin_lock(&ci->i_ceph_lock);
  1357. __cap_delay_requeue(arg->session->s_mdsc, ci);
  1358. spin_unlock(&ci->i_ceph_lock);
  1359. return;
  1360. }
  1361. encode_cap_msg(msg, arg);
  1362. ceph_con_send(&arg->session->s_con, msg);
  1363. ceph_buffer_put(arg->old_xattr_buf);
  1364. ceph_buffer_put(arg->xattr_buf);
  1365. if (arg->wake)
  1366. wake_up_all(&ci->i_cap_wq);
  1367. }
  1368. static inline int __send_flush_snap(struct inode *inode,
  1369. struct ceph_mds_session *session,
  1370. struct ceph_cap_snap *capsnap,
  1371. u32 mseq, u64 oldest_flush_tid)
  1372. {
  1373. struct cap_msg_args arg;
  1374. struct ceph_msg *msg;
  1375. arg.session = session;
  1376. arg.ino = ceph_vino(inode).ino;
  1377. arg.cid = 0;
  1378. arg.follows = capsnap->follows;
  1379. arg.flush_tid = capsnap->cap_flush.tid;
  1380. arg.oldest_flush_tid = oldest_flush_tid;
  1381. arg.size = capsnap->size;
  1382. arg.max_size = 0;
  1383. arg.xattr_version = capsnap->xattr_version;
  1384. arg.xattr_buf = capsnap->xattr_blob;
  1385. arg.old_xattr_buf = NULL;
  1386. arg.atime = capsnap->atime;
  1387. arg.mtime = capsnap->mtime;
  1388. arg.ctime = capsnap->ctime;
  1389. arg.btime = capsnap->btime;
  1390. arg.change_attr = capsnap->change_attr;
  1391. arg.op = CEPH_CAP_OP_FLUSHSNAP;
  1392. arg.caps = capsnap->issued;
  1393. arg.wanted = 0;
  1394. arg.dirty = capsnap->dirty;
  1395. arg.seq = 0;
  1396. arg.issue_seq = 0;
  1397. arg.mseq = mseq;
  1398. arg.time_warp_seq = capsnap->time_warp_seq;
  1399. arg.uid = capsnap->uid;
  1400. arg.gid = capsnap->gid;
  1401. arg.mode = capsnap->mode;
  1402. arg.inline_data = capsnap->inline_data;
  1403. arg.flags = 0;
  1404. arg.wake = false;
  1405. arg.encrypted = IS_ENCRYPTED(inode);
  1406. /* No fscrypt_auth changes from a capsnap.*/
  1407. arg.fscrypt_auth_len = 0;
  1408. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(&arg),
  1409. GFP_NOFS, false);
  1410. if (!msg)
  1411. return -ENOMEM;
  1412. encode_cap_msg(msg, &arg);
  1413. ceph_con_send(&arg.session->s_con, msg);
  1414. return 0;
  1415. }
  1416. /*
  1417. * When a snapshot is taken, clients accumulate dirty metadata on
  1418. * inodes with capabilities in ceph_cap_snaps to describe the file
  1419. * state at the time the snapshot was taken. This must be flushed
  1420. * asynchronously back to the MDS once sync writes complete and dirty
  1421. * data is written out.
  1422. *
  1423. * Called under i_ceph_lock.
  1424. */
  1425. static void __ceph_flush_snaps(struct ceph_inode_info *ci,
  1426. struct ceph_mds_session *session)
  1427. __releases(ci->i_ceph_lock)
  1428. __acquires(ci->i_ceph_lock)
  1429. {
  1430. struct inode *inode = &ci->netfs.inode;
  1431. struct ceph_mds_client *mdsc = session->s_mdsc;
  1432. struct ceph_client *cl = mdsc->fsc->client;
  1433. struct ceph_cap_snap *capsnap;
  1434. u64 oldest_flush_tid = 0;
  1435. u64 first_tid = 1, last_tid = 0;
  1436. doutc(cl, "%p %llx.%llx session %p\n", inode, ceph_vinop(inode),
  1437. session);
  1438. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1439. /*
  1440. * we need to wait for sync writes to complete and for dirty
  1441. * pages to be written out.
  1442. */
  1443. if (capsnap->dirty_pages || capsnap->writing)
  1444. break;
  1445. /* should be removed by ceph_try_drop_cap_snap() */
  1446. BUG_ON(!capsnap->need_flush);
  1447. /* only flush each capsnap once */
  1448. if (capsnap->cap_flush.tid > 0) {
  1449. doutc(cl, "already flushed %p, skipping\n", capsnap);
  1450. continue;
  1451. }
  1452. spin_lock(&mdsc->cap_dirty_lock);
  1453. capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
  1454. list_add_tail(&capsnap->cap_flush.g_list,
  1455. &mdsc->cap_flush_list);
  1456. if (oldest_flush_tid == 0)
  1457. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1458. if (list_empty(&ci->i_flushing_item)) {
  1459. list_add_tail(&ci->i_flushing_item,
  1460. &session->s_cap_flushing);
  1461. }
  1462. spin_unlock(&mdsc->cap_dirty_lock);
  1463. list_add_tail(&capsnap->cap_flush.i_list,
  1464. &ci->i_cap_flush_list);
  1465. if (first_tid == 1)
  1466. first_tid = capsnap->cap_flush.tid;
  1467. last_tid = capsnap->cap_flush.tid;
  1468. }
  1469. ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
  1470. while (first_tid <= last_tid) {
  1471. struct ceph_cap *cap = ci->i_auth_cap;
  1472. struct ceph_cap_flush *cf = NULL, *iter;
  1473. int ret;
  1474. if (!(cap && cap->session == session)) {
  1475. doutc(cl, "%p %llx.%llx auth cap %p not mds%d, stop\n",
  1476. inode, ceph_vinop(inode), cap, session->s_mds);
  1477. break;
  1478. }
  1479. ret = -ENOENT;
  1480. list_for_each_entry(iter, &ci->i_cap_flush_list, i_list) {
  1481. if (iter->tid >= first_tid) {
  1482. cf = iter;
  1483. ret = 0;
  1484. break;
  1485. }
  1486. }
  1487. if (ret < 0)
  1488. break;
  1489. first_tid = cf->tid + 1;
  1490. capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
  1491. refcount_inc(&capsnap->nref);
  1492. spin_unlock(&ci->i_ceph_lock);
  1493. doutc(cl, "%p %llx.%llx capsnap %p tid %llu %s\n", inode,
  1494. ceph_vinop(inode), capsnap, cf->tid,
  1495. ceph_cap_string(capsnap->dirty));
  1496. ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
  1497. oldest_flush_tid);
  1498. if (ret < 0) {
  1499. pr_err_client(cl, "error sending cap flushsnap, "
  1500. "ino (%llx.%llx) tid %llu follows %llu\n",
  1501. ceph_vinop(inode), cf->tid,
  1502. capsnap->follows);
  1503. }
  1504. ceph_put_cap_snap(capsnap);
  1505. spin_lock(&ci->i_ceph_lock);
  1506. }
  1507. }
  1508. void ceph_flush_snaps(struct ceph_inode_info *ci,
  1509. struct ceph_mds_session **psession)
  1510. {
  1511. struct inode *inode = &ci->netfs.inode;
  1512. struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
  1513. struct ceph_client *cl = ceph_inode_to_client(inode);
  1514. struct ceph_mds_session *session = NULL;
  1515. bool need_put = false;
  1516. int mds;
  1517. doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
  1518. if (psession)
  1519. session = *psession;
  1520. retry:
  1521. spin_lock(&ci->i_ceph_lock);
  1522. if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
  1523. doutc(cl, " no capsnap needs flush, doing nothing\n");
  1524. goto out;
  1525. }
  1526. if (!ci->i_auth_cap) {
  1527. doutc(cl, " no auth cap (migrating?), doing nothing\n");
  1528. goto out;
  1529. }
  1530. mds = ci->i_auth_cap->session->s_mds;
  1531. if (session && session->s_mds != mds) {
  1532. doutc(cl, " oops, wrong session %p mutex\n", session);
  1533. ceph_put_mds_session(session);
  1534. session = NULL;
  1535. }
  1536. if (!session) {
  1537. spin_unlock(&ci->i_ceph_lock);
  1538. mutex_lock(&mdsc->mutex);
  1539. session = __ceph_lookup_mds_session(mdsc, mds);
  1540. mutex_unlock(&mdsc->mutex);
  1541. goto retry;
  1542. }
  1543. // make sure flushsnap messages are sent in proper order.
  1544. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
  1545. __kick_flushing_caps(mdsc, session, ci, 0);
  1546. __ceph_flush_snaps(ci, session);
  1547. out:
  1548. spin_unlock(&ci->i_ceph_lock);
  1549. if (psession)
  1550. *psession = session;
  1551. else
  1552. ceph_put_mds_session(session);
  1553. /* we flushed them all; remove this inode from the queue */
  1554. spin_lock(&mdsc->snap_flush_lock);
  1555. if (!list_empty(&ci->i_snap_flush_item))
  1556. need_put = true;
  1557. list_del_init(&ci->i_snap_flush_item);
  1558. spin_unlock(&mdsc->snap_flush_lock);
  1559. if (need_put)
  1560. iput(inode);
  1561. }
  1562. /*
  1563. * Mark caps dirty. If inode is newly dirty, return the dirty flags.
  1564. * Caller is then responsible for calling __mark_inode_dirty with the
  1565. * returned flags value.
  1566. */
  1567. int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
  1568. struct ceph_cap_flush **pcf)
  1569. {
  1570. struct ceph_mds_client *mdsc =
  1571. ceph_sb_to_fs_client(ci->netfs.inode.i_sb)->mdsc;
  1572. struct inode *inode = &ci->netfs.inode;
  1573. struct ceph_client *cl = ceph_inode_to_client(inode);
  1574. int was = ci->i_dirty_caps;
  1575. int dirty = 0;
  1576. lockdep_assert_held(&ci->i_ceph_lock);
  1577. if (!ci->i_auth_cap) {
  1578. pr_warn_client(cl, "%p %llx.%llx mask %s, "
  1579. "but no auth cap (session was closed?)\n",
  1580. inode, ceph_vinop(inode),
  1581. ceph_cap_string(mask));
  1582. return 0;
  1583. }
  1584. doutc(cl, "%p %llx.%llx %s dirty %s -> %s\n", inode,
  1585. ceph_vinop(inode), ceph_cap_string(mask),
  1586. ceph_cap_string(was), ceph_cap_string(was | mask));
  1587. ci->i_dirty_caps |= mask;
  1588. if (was == 0) {
  1589. struct ceph_mds_session *session = ci->i_auth_cap->session;
  1590. WARN_ON_ONCE(ci->i_prealloc_cap_flush);
  1591. swap(ci->i_prealloc_cap_flush, *pcf);
  1592. if (!ci->i_head_snapc) {
  1593. WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
  1594. ci->i_head_snapc = ceph_get_snap_context(
  1595. ci->i_snap_realm->cached_context);
  1596. }
  1597. doutc(cl, "%p %llx.%llx now dirty snapc %p auth cap %p\n",
  1598. inode, ceph_vinop(inode), ci->i_head_snapc,
  1599. ci->i_auth_cap);
  1600. BUG_ON(!list_empty(&ci->i_dirty_item));
  1601. spin_lock(&mdsc->cap_dirty_lock);
  1602. list_add(&ci->i_dirty_item, &session->s_cap_dirty);
  1603. spin_unlock(&mdsc->cap_dirty_lock);
  1604. if (ci->i_flushing_caps == 0) {
  1605. ihold(inode);
  1606. dirty |= I_DIRTY_SYNC;
  1607. }
  1608. } else {
  1609. WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
  1610. }
  1611. BUG_ON(list_empty(&ci->i_dirty_item));
  1612. if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
  1613. (mask & CEPH_CAP_FILE_BUFFER))
  1614. dirty |= I_DIRTY_DATASYNC;
  1615. __cap_delay_requeue(mdsc, ci);
  1616. return dirty;
  1617. }
  1618. struct ceph_cap_flush *ceph_alloc_cap_flush(void)
  1619. {
  1620. struct ceph_cap_flush *cf;
  1621. cf = kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
  1622. if (!cf)
  1623. return NULL;
  1624. cf->is_capsnap = false;
  1625. return cf;
  1626. }
  1627. void ceph_free_cap_flush(struct ceph_cap_flush *cf)
  1628. {
  1629. if (cf)
  1630. kmem_cache_free(ceph_cap_flush_cachep, cf);
  1631. }
  1632. static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
  1633. {
  1634. if (!list_empty(&mdsc->cap_flush_list)) {
  1635. struct ceph_cap_flush *cf =
  1636. list_first_entry(&mdsc->cap_flush_list,
  1637. struct ceph_cap_flush, g_list);
  1638. return cf->tid;
  1639. }
  1640. return 0;
  1641. }
  1642. /*
  1643. * Remove cap_flush from the mdsc's or inode's flushing cap list.
  1644. * Return true if caller needs to wake up flush waiters.
  1645. */
  1646. static bool __detach_cap_flush_from_mdsc(struct ceph_mds_client *mdsc,
  1647. struct ceph_cap_flush *cf)
  1648. {
  1649. struct ceph_cap_flush *prev;
  1650. bool wake = cf->wake;
  1651. if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
  1652. prev = list_prev_entry(cf, g_list);
  1653. prev->wake = true;
  1654. wake = false;
  1655. }
  1656. list_del_init(&cf->g_list);
  1657. return wake;
  1658. }
  1659. static bool __detach_cap_flush_from_ci(struct ceph_inode_info *ci,
  1660. struct ceph_cap_flush *cf)
  1661. {
  1662. struct ceph_cap_flush *prev;
  1663. bool wake = cf->wake;
  1664. if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
  1665. prev = list_prev_entry(cf, i_list);
  1666. prev->wake = true;
  1667. wake = false;
  1668. }
  1669. list_del_init(&cf->i_list);
  1670. return wake;
  1671. }
  1672. /*
  1673. * Add dirty inode to the flushing list. Assigned a seq number so we
  1674. * can wait for caps to flush without starving.
  1675. *
  1676. * Called under i_ceph_lock. Returns the flush tid.
  1677. */
  1678. static u64 __mark_caps_flushing(struct inode *inode,
  1679. struct ceph_mds_session *session, bool wake,
  1680. u64 *oldest_flush_tid)
  1681. {
  1682. struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  1683. struct ceph_client *cl = ceph_inode_to_client(inode);
  1684. struct ceph_inode_info *ci = ceph_inode(inode);
  1685. struct ceph_cap_flush *cf = NULL;
  1686. int flushing;
  1687. lockdep_assert_held(&ci->i_ceph_lock);
  1688. BUG_ON(ci->i_dirty_caps == 0);
  1689. BUG_ON(list_empty(&ci->i_dirty_item));
  1690. BUG_ON(!ci->i_prealloc_cap_flush);
  1691. flushing = ci->i_dirty_caps;
  1692. doutc(cl, "flushing %s, flushing_caps %s -> %s\n",
  1693. ceph_cap_string(flushing),
  1694. ceph_cap_string(ci->i_flushing_caps),
  1695. ceph_cap_string(ci->i_flushing_caps | flushing));
  1696. ci->i_flushing_caps |= flushing;
  1697. ci->i_dirty_caps = 0;
  1698. doutc(cl, "%p %llx.%llx now !dirty\n", inode, ceph_vinop(inode));
  1699. swap(cf, ci->i_prealloc_cap_flush);
  1700. cf->caps = flushing;
  1701. cf->wake = wake;
  1702. spin_lock(&mdsc->cap_dirty_lock);
  1703. list_del_init(&ci->i_dirty_item);
  1704. cf->tid = ++mdsc->last_cap_flush_tid;
  1705. list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
  1706. *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1707. if (list_empty(&ci->i_flushing_item)) {
  1708. list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1709. mdsc->num_cap_flushing++;
  1710. }
  1711. spin_unlock(&mdsc->cap_dirty_lock);
  1712. list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
  1713. return cf->tid;
  1714. }
  1715. /*
  1716. * try to invalidate mapping pages without blocking.
  1717. */
  1718. static int try_nonblocking_invalidate(struct inode *inode)
  1719. __releases(ci->i_ceph_lock)
  1720. __acquires(ci->i_ceph_lock)
  1721. {
  1722. struct ceph_client *cl = ceph_inode_to_client(inode);
  1723. struct ceph_inode_info *ci = ceph_inode(inode);
  1724. u32 invalidating_gen = ci->i_rdcache_gen;
  1725. spin_unlock(&ci->i_ceph_lock);
  1726. ceph_fscache_invalidate(inode, false);
  1727. invalidate_mapping_pages(&inode->i_data, 0, -1);
  1728. spin_lock(&ci->i_ceph_lock);
  1729. if (inode->i_data.nrpages == 0 &&
  1730. invalidating_gen == ci->i_rdcache_gen) {
  1731. /* success. */
  1732. doutc(cl, "%p %llx.%llx success\n", inode,
  1733. ceph_vinop(inode));
  1734. /* save any racing async invalidate some trouble */
  1735. ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
  1736. return 0;
  1737. }
  1738. doutc(cl, "%p %llx.%llx failed\n", inode, ceph_vinop(inode));
  1739. return -1;
  1740. }
  1741. bool __ceph_should_report_size(struct ceph_inode_info *ci)
  1742. {
  1743. loff_t size = i_size_read(&ci->netfs.inode);
  1744. /* mds will adjust max size according to the reported size */
  1745. if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
  1746. return false;
  1747. if (size >= ci->i_max_size)
  1748. return true;
  1749. /* half of previous max_size increment has been used */
  1750. if (ci->i_max_size > ci->i_reported_size &&
  1751. (size << 1) >= ci->i_max_size + ci->i_reported_size)
  1752. return true;
  1753. return false;
  1754. }
  1755. /*
  1756. * Swiss army knife function to examine currently used and wanted
  1757. * versus held caps. Release, flush, ack revoked caps to mds as
  1758. * appropriate.
  1759. *
  1760. * CHECK_CAPS_AUTHONLY - we should only check the auth cap
  1761. * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
  1762. * further delay.
  1763. * CHECK_CAPS_FLUSH_FORCE - we should flush any caps immediately, without
  1764. * further delay.
  1765. */
  1766. void ceph_check_caps(struct ceph_inode_info *ci, int flags)
  1767. {
  1768. struct inode *inode = &ci->netfs.inode;
  1769. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  1770. struct ceph_client *cl = ceph_inode_to_client(inode);
  1771. struct ceph_cap *cap;
  1772. u64 flush_tid, oldest_flush_tid;
  1773. int file_wanted, used, cap_used;
  1774. int issued, implemented, want, retain, revoking, flushing = 0;
  1775. int mds = -1; /* keep track of how far we've gone through i_caps list
  1776. to avoid an infinite loop on retry */
  1777. struct rb_node *p;
  1778. bool queue_invalidate = false;
  1779. bool tried_invalidate = false;
  1780. bool queue_writeback = false;
  1781. struct ceph_mds_session *session = NULL;
  1782. spin_lock(&ci->i_ceph_lock);
  1783. if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
  1784. ci->i_ceph_flags |= CEPH_I_ASYNC_CHECK_CAPS;
  1785. /* Don't send messages until we get async create reply */
  1786. spin_unlock(&ci->i_ceph_lock);
  1787. return;
  1788. }
  1789. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  1790. flags |= CHECK_CAPS_FLUSH;
  1791. retry:
  1792. /* Caps wanted by virtue of active open files. */
  1793. file_wanted = __ceph_caps_file_wanted(ci);
  1794. /* Caps which have active references against them */
  1795. used = __ceph_caps_used(ci);
  1796. /*
  1797. * "issued" represents the current caps that the MDS wants us to have.
  1798. * "implemented" is the set that we have been granted, and includes the
  1799. * ones that have not yet been returned to the MDS (the "revoking" set,
  1800. * usually because they have outstanding references).
  1801. */
  1802. issued = __ceph_caps_issued(ci, &implemented);
  1803. revoking = implemented & ~issued;
  1804. want = file_wanted;
  1805. /* The ones we currently want to retain (may be adjusted below) */
  1806. retain = file_wanted | used | CEPH_CAP_PIN;
  1807. if (!mdsc->stopping && inode->i_nlink > 0) {
  1808. if (file_wanted) {
  1809. retain |= CEPH_CAP_ANY; /* be greedy */
  1810. } else if (S_ISDIR(inode->i_mode) &&
  1811. (issued & CEPH_CAP_FILE_SHARED) &&
  1812. __ceph_dir_is_complete(ci)) {
  1813. /*
  1814. * If a directory is complete, we want to keep
  1815. * the exclusive cap. So that MDS does not end up
  1816. * revoking the shared cap on every create/unlink
  1817. * operation.
  1818. */
  1819. if (IS_RDONLY(inode)) {
  1820. want = CEPH_CAP_ANY_SHARED;
  1821. } else {
  1822. want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
  1823. }
  1824. retain |= want;
  1825. } else {
  1826. retain |= CEPH_CAP_ANY_SHARED;
  1827. /*
  1828. * keep RD only if we didn't have the file open RW,
  1829. * because then the mds would revoke it anyway to
  1830. * journal max_size=0.
  1831. */
  1832. if (ci->i_max_size == 0)
  1833. retain |= CEPH_CAP_ANY_RD;
  1834. }
  1835. }
  1836. doutc(cl, "%p %llx.%llx file_want %s used %s dirty %s "
  1837. "flushing %s issued %s revoking %s retain %s %s%s%s%s\n",
  1838. inode, ceph_vinop(inode), ceph_cap_string(file_wanted),
  1839. ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
  1840. ceph_cap_string(ci->i_flushing_caps),
  1841. ceph_cap_string(issued), ceph_cap_string(revoking),
  1842. ceph_cap_string(retain),
  1843. (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
  1844. (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "",
  1845. (flags & CHECK_CAPS_NOINVAL) ? " NOINVAL" : "",
  1846. (flags & CHECK_CAPS_FLUSH_FORCE) ? " FLUSH_FORCE" : "");
  1847. /*
  1848. * If we no longer need to hold onto old our caps, and we may
  1849. * have cached pages, but don't want them, then try to invalidate.
  1850. * If we fail, it's because pages are locked.... try again later.
  1851. */
  1852. if ((!(flags & CHECK_CAPS_NOINVAL) || mdsc->stopping) &&
  1853. S_ISREG(inode->i_mode) &&
  1854. !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
  1855. inode->i_data.nrpages && /* have cached pages */
  1856. (revoking & (CEPH_CAP_FILE_CACHE|
  1857. CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
  1858. !tried_invalidate) {
  1859. doutc(cl, "trying to invalidate on %p %llx.%llx\n",
  1860. inode, ceph_vinop(inode));
  1861. if (try_nonblocking_invalidate(inode) < 0) {
  1862. doutc(cl, "queuing invalidate\n");
  1863. queue_invalidate = true;
  1864. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  1865. }
  1866. tried_invalidate = true;
  1867. goto retry;
  1868. }
  1869. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  1870. int mflags = 0;
  1871. struct cap_msg_args arg;
  1872. cap = rb_entry(p, struct ceph_cap, ci_node);
  1873. /* avoid looping forever */
  1874. if (mds >= cap->mds ||
  1875. ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
  1876. continue;
  1877. /*
  1878. * If we have an auth cap, we don't need to consider any
  1879. * overlapping caps as used.
  1880. */
  1881. cap_used = used;
  1882. if (ci->i_auth_cap && cap != ci->i_auth_cap)
  1883. cap_used &= ~ci->i_auth_cap->issued;
  1884. revoking = cap->implemented & ~cap->issued;
  1885. doutc(cl, " mds%d cap %p used %s issued %s implemented %s revoking %s\n",
  1886. cap->mds, cap, ceph_cap_string(cap_used),
  1887. ceph_cap_string(cap->issued),
  1888. ceph_cap_string(cap->implemented),
  1889. ceph_cap_string(revoking));
  1890. /* completed revocation? going down and there are no caps? */
  1891. if (revoking) {
  1892. if ((revoking & cap_used) == 0) {
  1893. doutc(cl, "completed revocation of %s\n",
  1894. ceph_cap_string(cap->implemented & ~cap->issued));
  1895. goto ack;
  1896. }
  1897. /*
  1898. * If the "i_wrbuffer_ref" was increased by mmap or generic
  1899. * cache write just before the ceph_check_caps() is called,
  1900. * the Fb capability revoking will fail this time. Then we
  1901. * must wait for the BDI's delayed work to flush the dirty
  1902. * pages and to release the "i_wrbuffer_ref", which will cost
  1903. * at most 5 seconds. That means the MDS needs to wait at
  1904. * most 5 seconds to finished the Fb capability's revocation.
  1905. *
  1906. * Let's queue a writeback for it.
  1907. */
  1908. if (S_ISREG(inode->i_mode) && ci->i_wrbuffer_ref &&
  1909. (revoking & CEPH_CAP_FILE_BUFFER))
  1910. queue_writeback = true;
  1911. }
  1912. if (flags & CHECK_CAPS_FLUSH_FORCE) {
  1913. doutc(cl, "force to flush caps\n");
  1914. goto ack;
  1915. }
  1916. if (cap == ci->i_auth_cap &&
  1917. (cap->issued & CEPH_CAP_FILE_WR)) {
  1918. /* request larger max_size from MDS? */
  1919. if (ci->i_wanted_max_size > ci->i_max_size &&
  1920. ci->i_wanted_max_size > ci->i_requested_max_size) {
  1921. doutc(cl, "requesting new max_size\n");
  1922. goto ack;
  1923. }
  1924. /* approaching file_max? */
  1925. if (__ceph_should_report_size(ci)) {
  1926. doutc(cl, "i_size approaching max_size\n");
  1927. goto ack;
  1928. }
  1929. }
  1930. /* flush anything dirty? */
  1931. if (cap == ci->i_auth_cap) {
  1932. if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
  1933. doutc(cl, "flushing dirty caps\n");
  1934. goto ack;
  1935. }
  1936. if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
  1937. doutc(cl, "flushing snap caps\n");
  1938. goto ack;
  1939. }
  1940. }
  1941. /* want more caps from mds? */
  1942. if (want & ~cap->mds_wanted) {
  1943. if (want & ~(cap->mds_wanted | cap->issued))
  1944. goto ack;
  1945. if (!__cap_is_valid(cap))
  1946. goto ack;
  1947. }
  1948. /* things we might delay */
  1949. if ((cap->issued & ~retain) == 0)
  1950. continue; /* nope, all good */
  1951. ack:
  1952. ceph_put_mds_session(session);
  1953. session = ceph_get_mds_session(cap->session);
  1954. /* kick flushing and flush snaps before sending normal
  1955. * cap message */
  1956. if (cap == ci->i_auth_cap &&
  1957. (ci->i_ceph_flags &
  1958. (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
  1959. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
  1960. __kick_flushing_caps(mdsc, session, ci, 0);
  1961. if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
  1962. __ceph_flush_snaps(ci, session);
  1963. goto retry;
  1964. }
  1965. if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
  1966. flushing = ci->i_dirty_caps;
  1967. flush_tid = __mark_caps_flushing(inode, session, false,
  1968. &oldest_flush_tid);
  1969. if (flags & CHECK_CAPS_FLUSH &&
  1970. list_empty(&session->s_cap_dirty))
  1971. mflags |= CEPH_CLIENT_CAPS_SYNC;
  1972. } else {
  1973. flushing = 0;
  1974. flush_tid = 0;
  1975. spin_lock(&mdsc->cap_dirty_lock);
  1976. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1977. spin_unlock(&mdsc->cap_dirty_lock);
  1978. }
  1979. mds = cap->mds; /* remember mds, so we don't repeat */
  1980. __prep_cap(&arg, cap, CEPH_CAP_OP_UPDATE, mflags, cap_used,
  1981. want, retain, flushing, flush_tid, oldest_flush_tid);
  1982. spin_unlock(&ci->i_ceph_lock);
  1983. __send_cap(&arg, ci);
  1984. spin_lock(&ci->i_ceph_lock);
  1985. goto retry; /* retake i_ceph_lock and restart our cap scan. */
  1986. }
  1987. /* periodically re-calculate caps wanted by open files */
  1988. if (__ceph_is_any_real_caps(ci) &&
  1989. list_empty(&ci->i_cap_delay_list) &&
  1990. (file_wanted & ~CEPH_CAP_PIN) &&
  1991. !(used & (CEPH_CAP_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
  1992. __cap_delay_requeue(mdsc, ci);
  1993. }
  1994. spin_unlock(&ci->i_ceph_lock);
  1995. ceph_put_mds_session(session);
  1996. if (queue_writeback)
  1997. ceph_queue_writeback(inode);
  1998. if (queue_invalidate)
  1999. ceph_queue_invalidate(inode);
  2000. }
  2001. /*
  2002. * Try to flush dirty caps back to the auth mds.
  2003. */
  2004. static int try_flush_caps(struct inode *inode, u64 *ptid)
  2005. {
  2006. struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  2007. struct ceph_inode_info *ci = ceph_inode(inode);
  2008. int flushing = 0;
  2009. u64 flush_tid = 0, oldest_flush_tid = 0;
  2010. spin_lock(&ci->i_ceph_lock);
  2011. retry_locked:
  2012. if (ci->i_dirty_caps && ci->i_auth_cap) {
  2013. struct ceph_cap *cap = ci->i_auth_cap;
  2014. struct cap_msg_args arg;
  2015. struct ceph_mds_session *session = cap->session;
  2016. if (session->s_state < CEPH_MDS_SESSION_OPEN) {
  2017. spin_unlock(&ci->i_ceph_lock);
  2018. goto out;
  2019. }
  2020. if (ci->i_ceph_flags &
  2021. (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS)) {
  2022. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
  2023. __kick_flushing_caps(mdsc, session, ci, 0);
  2024. if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
  2025. __ceph_flush_snaps(ci, session);
  2026. goto retry_locked;
  2027. }
  2028. flushing = ci->i_dirty_caps;
  2029. flush_tid = __mark_caps_flushing(inode, session, true,
  2030. &oldest_flush_tid);
  2031. __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH, CEPH_CLIENT_CAPS_SYNC,
  2032. __ceph_caps_used(ci), __ceph_caps_wanted(ci),
  2033. (cap->issued | cap->implemented),
  2034. flushing, flush_tid, oldest_flush_tid);
  2035. spin_unlock(&ci->i_ceph_lock);
  2036. __send_cap(&arg, ci);
  2037. } else {
  2038. if (!list_empty(&ci->i_cap_flush_list)) {
  2039. struct ceph_cap_flush *cf =
  2040. list_last_entry(&ci->i_cap_flush_list,
  2041. struct ceph_cap_flush, i_list);
  2042. cf->wake = true;
  2043. flush_tid = cf->tid;
  2044. }
  2045. flushing = ci->i_flushing_caps;
  2046. spin_unlock(&ci->i_ceph_lock);
  2047. }
  2048. out:
  2049. *ptid = flush_tid;
  2050. return flushing;
  2051. }
  2052. /*
  2053. * Return true if we've flushed caps through the given flush_tid.
  2054. */
  2055. static int caps_are_flushed(struct inode *inode, u64 flush_tid)
  2056. {
  2057. struct ceph_inode_info *ci = ceph_inode(inode);
  2058. int ret = 1;
  2059. spin_lock(&ci->i_ceph_lock);
  2060. if (!list_empty(&ci->i_cap_flush_list)) {
  2061. struct ceph_cap_flush * cf =
  2062. list_first_entry(&ci->i_cap_flush_list,
  2063. struct ceph_cap_flush, i_list);
  2064. if (cf->tid <= flush_tid)
  2065. ret = 0;
  2066. }
  2067. spin_unlock(&ci->i_ceph_lock);
  2068. return ret;
  2069. }
  2070. /*
  2071. * flush the mdlog and wait for any unsafe requests to complete.
  2072. */
  2073. static int flush_mdlog_and_wait_inode_unsafe_requests(struct inode *inode)
  2074. {
  2075. struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  2076. struct ceph_client *cl = ceph_inode_to_client(inode);
  2077. struct ceph_inode_info *ci = ceph_inode(inode);
  2078. struct ceph_mds_request *req1 = NULL, *req2 = NULL;
  2079. int ret, err = 0;
  2080. spin_lock(&ci->i_unsafe_lock);
  2081. if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
  2082. req1 = list_last_entry(&ci->i_unsafe_dirops,
  2083. struct ceph_mds_request,
  2084. r_unsafe_dir_item);
  2085. ceph_mdsc_get_request(req1);
  2086. }
  2087. if (!list_empty(&ci->i_unsafe_iops)) {
  2088. req2 = list_last_entry(&ci->i_unsafe_iops,
  2089. struct ceph_mds_request,
  2090. r_unsafe_target_item);
  2091. ceph_mdsc_get_request(req2);
  2092. }
  2093. spin_unlock(&ci->i_unsafe_lock);
  2094. /*
  2095. * Trigger to flush the journal logs in all the relevant MDSes
  2096. * manually, or in the worst case we must wait at most 5 seconds
  2097. * to wait the journal logs to be flushed by the MDSes periodically.
  2098. */
  2099. if (req1 || req2) {
  2100. struct ceph_mds_request *req;
  2101. struct ceph_mds_session **sessions;
  2102. struct ceph_mds_session *s;
  2103. unsigned int max_sessions;
  2104. int i;
  2105. mutex_lock(&mdsc->mutex);
  2106. max_sessions = mdsc->max_sessions;
  2107. sessions = kcalloc(max_sessions, sizeof(s), GFP_KERNEL);
  2108. if (!sessions) {
  2109. mutex_unlock(&mdsc->mutex);
  2110. err = -ENOMEM;
  2111. goto out;
  2112. }
  2113. spin_lock(&ci->i_unsafe_lock);
  2114. if (req1) {
  2115. list_for_each_entry(req, &ci->i_unsafe_dirops,
  2116. r_unsafe_dir_item) {
  2117. s = req->r_session;
  2118. if (!s)
  2119. continue;
  2120. if (!sessions[s->s_mds]) {
  2121. s = ceph_get_mds_session(s);
  2122. sessions[s->s_mds] = s;
  2123. }
  2124. }
  2125. }
  2126. if (req2) {
  2127. list_for_each_entry(req, &ci->i_unsafe_iops,
  2128. r_unsafe_target_item) {
  2129. s = req->r_session;
  2130. if (!s)
  2131. continue;
  2132. if (!sessions[s->s_mds]) {
  2133. s = ceph_get_mds_session(s);
  2134. sessions[s->s_mds] = s;
  2135. }
  2136. }
  2137. }
  2138. spin_unlock(&ci->i_unsafe_lock);
  2139. /* the auth MDS */
  2140. spin_lock(&ci->i_ceph_lock);
  2141. if (ci->i_auth_cap) {
  2142. s = ci->i_auth_cap->session;
  2143. if (!sessions[s->s_mds])
  2144. sessions[s->s_mds] = ceph_get_mds_session(s);
  2145. }
  2146. spin_unlock(&ci->i_ceph_lock);
  2147. mutex_unlock(&mdsc->mutex);
  2148. /* send flush mdlog request to MDSes */
  2149. for (i = 0; i < max_sessions; i++) {
  2150. s = sessions[i];
  2151. if (s) {
  2152. send_flush_mdlog(s);
  2153. ceph_put_mds_session(s);
  2154. }
  2155. }
  2156. kfree(sessions);
  2157. }
  2158. doutc(cl, "%p %llx.%llx wait on tid %llu %llu\n", inode,
  2159. ceph_vinop(inode), req1 ? req1->r_tid : 0ULL,
  2160. req2 ? req2->r_tid : 0ULL);
  2161. if (req1) {
  2162. ret = !wait_for_completion_timeout(&req1->r_safe_completion,
  2163. ceph_timeout_jiffies(req1->r_timeout));
  2164. if (ret)
  2165. err = -EIO;
  2166. }
  2167. if (req2) {
  2168. ret = !wait_for_completion_timeout(&req2->r_safe_completion,
  2169. ceph_timeout_jiffies(req2->r_timeout));
  2170. if (ret)
  2171. err = -EIO;
  2172. }
  2173. out:
  2174. if (req1)
  2175. ceph_mdsc_put_request(req1);
  2176. if (req2)
  2177. ceph_mdsc_put_request(req2);
  2178. return err;
  2179. }
  2180. int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  2181. {
  2182. struct inode *inode = file->f_mapping->host;
  2183. struct ceph_inode_info *ci = ceph_inode(inode);
  2184. struct ceph_client *cl = ceph_inode_to_client(inode);
  2185. u64 flush_tid;
  2186. int ret, err;
  2187. int dirty;
  2188. doutc(cl, "%p %llx.%llx%s\n", inode, ceph_vinop(inode),
  2189. datasync ? " datasync" : "");
  2190. ret = file_write_and_wait_range(file, start, end);
  2191. if (datasync)
  2192. goto out;
  2193. ret = ceph_wait_on_async_create(inode);
  2194. if (ret)
  2195. goto out;
  2196. dirty = try_flush_caps(inode, &flush_tid);
  2197. doutc(cl, "dirty caps are %s\n", ceph_cap_string(dirty));
  2198. err = flush_mdlog_and_wait_inode_unsafe_requests(inode);
  2199. /*
  2200. * only wait on non-file metadata writeback (the mds
  2201. * can recover size and mtime, so we don't need to
  2202. * wait for that)
  2203. */
  2204. if (!err && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
  2205. err = wait_event_interruptible(ci->i_cap_wq,
  2206. caps_are_flushed(inode, flush_tid));
  2207. }
  2208. if (err < 0)
  2209. ret = err;
  2210. err = file_check_and_advance_wb_err(file);
  2211. if (err < 0)
  2212. ret = err;
  2213. out:
  2214. doutc(cl, "%p %llx.%llx%s result=%d\n", inode, ceph_vinop(inode),
  2215. datasync ? " datasync" : "", ret);
  2216. return ret;
  2217. }
  2218. /*
  2219. * Flush any dirty caps back to the mds. If we aren't asked to wait,
  2220. * queue inode for flush but don't do so immediately, because we can
  2221. * get by with fewer MDS messages if we wait for data writeback to
  2222. * complete first.
  2223. */
  2224. int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
  2225. {
  2226. struct ceph_inode_info *ci = ceph_inode(inode);
  2227. struct ceph_client *cl = ceph_inode_to_client(inode);
  2228. u64 flush_tid;
  2229. int err = 0;
  2230. int dirty;
  2231. int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
  2232. doutc(cl, "%p %llx.%llx wait=%d\n", inode, ceph_vinop(inode), wait);
  2233. ceph_fscache_unpin_writeback(inode, wbc);
  2234. if (wait) {
  2235. err = ceph_wait_on_async_create(inode);
  2236. if (err)
  2237. return err;
  2238. dirty = try_flush_caps(inode, &flush_tid);
  2239. if (dirty)
  2240. err = wait_event_interruptible(ci->i_cap_wq,
  2241. caps_are_flushed(inode, flush_tid));
  2242. } else {
  2243. struct ceph_mds_client *mdsc =
  2244. ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  2245. spin_lock(&ci->i_ceph_lock);
  2246. if (__ceph_caps_dirty(ci))
  2247. __cap_delay_requeue_front(mdsc, ci);
  2248. spin_unlock(&ci->i_ceph_lock);
  2249. }
  2250. return err;
  2251. }
  2252. static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
  2253. struct ceph_mds_session *session,
  2254. struct ceph_inode_info *ci,
  2255. u64 oldest_flush_tid)
  2256. __releases(ci->i_ceph_lock)
  2257. __acquires(ci->i_ceph_lock)
  2258. {
  2259. struct inode *inode = &ci->netfs.inode;
  2260. struct ceph_client *cl = mdsc->fsc->client;
  2261. struct ceph_cap *cap;
  2262. struct ceph_cap_flush *cf;
  2263. int ret;
  2264. u64 first_tid = 0;
  2265. u64 last_snap_flush = 0;
  2266. /* Don't do anything until create reply comes in */
  2267. if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE)
  2268. return;
  2269. ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
  2270. list_for_each_entry_reverse(cf, &ci->i_cap_flush_list, i_list) {
  2271. if (cf->is_capsnap) {
  2272. last_snap_flush = cf->tid;
  2273. break;
  2274. }
  2275. }
  2276. list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
  2277. if (cf->tid < first_tid)
  2278. continue;
  2279. cap = ci->i_auth_cap;
  2280. if (!(cap && cap->session == session)) {
  2281. pr_err_client(cl, "%p auth cap %p not mds%d ???\n",
  2282. inode, cap, session->s_mds);
  2283. break;
  2284. }
  2285. first_tid = cf->tid + 1;
  2286. if (!cf->is_capsnap) {
  2287. struct cap_msg_args arg;
  2288. doutc(cl, "%p %llx.%llx cap %p tid %llu %s\n",
  2289. inode, ceph_vinop(inode), cap, cf->tid,
  2290. ceph_cap_string(cf->caps));
  2291. __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH,
  2292. (cf->tid < last_snap_flush ?
  2293. CEPH_CLIENT_CAPS_PENDING_CAPSNAP : 0),
  2294. __ceph_caps_used(ci),
  2295. __ceph_caps_wanted(ci),
  2296. (cap->issued | cap->implemented),
  2297. cf->caps, cf->tid, oldest_flush_tid);
  2298. spin_unlock(&ci->i_ceph_lock);
  2299. __send_cap(&arg, ci);
  2300. } else {
  2301. struct ceph_cap_snap *capsnap =
  2302. container_of(cf, struct ceph_cap_snap,
  2303. cap_flush);
  2304. doutc(cl, "%p %llx.%llx capsnap %p tid %llu %s\n",
  2305. inode, ceph_vinop(inode), capsnap, cf->tid,
  2306. ceph_cap_string(capsnap->dirty));
  2307. refcount_inc(&capsnap->nref);
  2308. spin_unlock(&ci->i_ceph_lock);
  2309. ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
  2310. oldest_flush_tid);
  2311. if (ret < 0) {
  2312. pr_err_client(cl, "error sending cap flushsnap,"
  2313. " %p %llx.%llx tid %llu follows %llu\n",
  2314. inode, ceph_vinop(inode), cf->tid,
  2315. capsnap->follows);
  2316. }
  2317. ceph_put_cap_snap(capsnap);
  2318. }
  2319. spin_lock(&ci->i_ceph_lock);
  2320. }
  2321. }
  2322. void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
  2323. struct ceph_mds_session *session)
  2324. {
  2325. struct ceph_client *cl = mdsc->fsc->client;
  2326. struct ceph_inode_info *ci;
  2327. struct ceph_cap *cap;
  2328. u64 oldest_flush_tid;
  2329. doutc(cl, "mds%d\n", session->s_mds);
  2330. spin_lock(&mdsc->cap_dirty_lock);
  2331. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  2332. spin_unlock(&mdsc->cap_dirty_lock);
  2333. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  2334. struct inode *inode = &ci->netfs.inode;
  2335. spin_lock(&ci->i_ceph_lock);
  2336. cap = ci->i_auth_cap;
  2337. if (!(cap && cap->session == session)) {
  2338. pr_err_client(cl, "%p %llx.%llx auth cap %p not mds%d ???\n",
  2339. inode, ceph_vinop(inode), cap,
  2340. session->s_mds);
  2341. spin_unlock(&ci->i_ceph_lock);
  2342. continue;
  2343. }
  2344. /*
  2345. * if flushing caps were revoked, we re-send the cap flush
  2346. * in client reconnect stage. This guarantees MDS * processes
  2347. * the cap flush message before issuing the flushing caps to
  2348. * other client.
  2349. */
  2350. if ((cap->issued & ci->i_flushing_caps) !=
  2351. ci->i_flushing_caps) {
  2352. /* encode_caps_cb() also will reset these sequence
  2353. * numbers. make sure sequence numbers in cap flush
  2354. * message match later reconnect message */
  2355. cap->seq = 0;
  2356. cap->issue_seq = 0;
  2357. cap->mseq = 0;
  2358. __kick_flushing_caps(mdsc, session, ci,
  2359. oldest_flush_tid);
  2360. } else {
  2361. ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
  2362. }
  2363. spin_unlock(&ci->i_ceph_lock);
  2364. }
  2365. }
  2366. void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
  2367. struct ceph_mds_session *session)
  2368. {
  2369. struct ceph_client *cl = mdsc->fsc->client;
  2370. struct ceph_inode_info *ci;
  2371. struct ceph_cap *cap;
  2372. u64 oldest_flush_tid;
  2373. lockdep_assert_held(&session->s_mutex);
  2374. doutc(cl, "mds%d\n", session->s_mds);
  2375. spin_lock(&mdsc->cap_dirty_lock);
  2376. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  2377. spin_unlock(&mdsc->cap_dirty_lock);
  2378. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  2379. struct inode *inode = &ci->netfs.inode;
  2380. spin_lock(&ci->i_ceph_lock);
  2381. cap = ci->i_auth_cap;
  2382. if (!(cap && cap->session == session)) {
  2383. pr_err_client(cl, "%p %llx.%llx auth cap %p not mds%d ???\n",
  2384. inode, ceph_vinop(inode), cap,
  2385. session->s_mds);
  2386. spin_unlock(&ci->i_ceph_lock);
  2387. continue;
  2388. }
  2389. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
  2390. __kick_flushing_caps(mdsc, session, ci,
  2391. oldest_flush_tid);
  2392. }
  2393. spin_unlock(&ci->i_ceph_lock);
  2394. }
  2395. }
  2396. void ceph_kick_flushing_inode_caps(struct ceph_mds_session *session,
  2397. struct ceph_inode_info *ci)
  2398. {
  2399. struct ceph_mds_client *mdsc = session->s_mdsc;
  2400. struct ceph_cap *cap = ci->i_auth_cap;
  2401. struct inode *inode = &ci->netfs.inode;
  2402. lockdep_assert_held(&ci->i_ceph_lock);
  2403. doutc(mdsc->fsc->client, "%p %llx.%llx flushing %s\n",
  2404. inode, ceph_vinop(inode),
  2405. ceph_cap_string(ci->i_flushing_caps));
  2406. if (!list_empty(&ci->i_cap_flush_list)) {
  2407. u64 oldest_flush_tid;
  2408. spin_lock(&mdsc->cap_dirty_lock);
  2409. list_move_tail(&ci->i_flushing_item,
  2410. &cap->session->s_cap_flushing);
  2411. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  2412. spin_unlock(&mdsc->cap_dirty_lock);
  2413. __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
  2414. }
  2415. }
  2416. /*
  2417. * Take references to capabilities we hold, so that we don't release
  2418. * them to the MDS prematurely.
  2419. */
  2420. void ceph_take_cap_refs(struct ceph_inode_info *ci, int got,
  2421. bool snap_rwsem_locked)
  2422. {
  2423. struct inode *inode = &ci->netfs.inode;
  2424. struct ceph_client *cl = ceph_inode_to_client(inode);
  2425. lockdep_assert_held(&ci->i_ceph_lock);
  2426. if (got & CEPH_CAP_PIN)
  2427. ci->i_pin_ref++;
  2428. if (got & CEPH_CAP_FILE_RD)
  2429. ci->i_rd_ref++;
  2430. if (got & CEPH_CAP_FILE_CACHE)
  2431. ci->i_rdcache_ref++;
  2432. if (got & CEPH_CAP_FILE_EXCL)
  2433. ci->i_fx_ref++;
  2434. if (got & CEPH_CAP_FILE_WR) {
  2435. if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
  2436. BUG_ON(!snap_rwsem_locked);
  2437. ci->i_head_snapc = ceph_get_snap_context(
  2438. ci->i_snap_realm->cached_context);
  2439. }
  2440. ci->i_wr_ref++;
  2441. }
  2442. if (got & CEPH_CAP_FILE_BUFFER) {
  2443. if (ci->i_wb_ref == 0)
  2444. ihold(inode);
  2445. ci->i_wb_ref++;
  2446. doutc(cl, "%p %llx.%llx wb %d -> %d (?)\n", inode,
  2447. ceph_vinop(inode), ci->i_wb_ref-1, ci->i_wb_ref);
  2448. }
  2449. }
  2450. /*
  2451. * Try to grab cap references. Specify those refs we @want, and the
  2452. * minimal set we @need. Also include the larger offset we are writing
  2453. * to (when applicable), and check against max_size here as well.
  2454. * Note that caller is responsible for ensuring max_size increases are
  2455. * requested from the MDS.
  2456. *
  2457. * Returns 0 if caps were not able to be acquired (yet), 1 if succeed,
  2458. * or a negative error code. There are 3 speical error codes:
  2459. * -EAGAIN: need to sleep but non-blocking is specified
  2460. * -EFBIG: ask caller to call check_max_size() and try again.
  2461. * -EUCLEAN: ask caller to call ceph_renew_caps() and try again.
  2462. */
  2463. enum {
  2464. /* first 8 bits are reserved for CEPH_FILE_MODE_FOO */
  2465. NON_BLOCKING = (1 << 8),
  2466. CHECK_FILELOCK = (1 << 9),
  2467. };
  2468. static int try_get_cap_refs(struct inode *inode, int need, int want,
  2469. loff_t endoff, int flags, int *got)
  2470. {
  2471. struct ceph_inode_info *ci = ceph_inode(inode);
  2472. struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
  2473. struct ceph_client *cl = ceph_inode_to_client(inode);
  2474. int ret = 0;
  2475. int have, implemented;
  2476. bool snap_rwsem_locked = false;
  2477. doutc(cl, "%p %llx.%llx need %s want %s\n", inode,
  2478. ceph_vinop(inode), ceph_cap_string(need),
  2479. ceph_cap_string(want));
  2480. again:
  2481. spin_lock(&ci->i_ceph_lock);
  2482. if ((flags & CHECK_FILELOCK) &&
  2483. (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK)) {
  2484. doutc(cl, "%p %llx.%llx error filelock\n", inode,
  2485. ceph_vinop(inode));
  2486. ret = -EIO;
  2487. goto out_unlock;
  2488. }
  2489. /* finish pending truncate */
  2490. while (ci->i_truncate_pending) {
  2491. spin_unlock(&ci->i_ceph_lock);
  2492. if (snap_rwsem_locked) {
  2493. up_read(&mdsc->snap_rwsem);
  2494. snap_rwsem_locked = false;
  2495. }
  2496. __ceph_do_pending_vmtruncate(inode);
  2497. spin_lock(&ci->i_ceph_lock);
  2498. }
  2499. have = __ceph_caps_issued(ci, &implemented);
  2500. if (have & need & CEPH_CAP_FILE_WR) {
  2501. if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
  2502. doutc(cl, "%p %llx.%llx endoff %llu > maxsize %llu\n",
  2503. inode, ceph_vinop(inode), endoff, ci->i_max_size);
  2504. if (endoff > ci->i_requested_max_size)
  2505. ret = ci->i_auth_cap ? -EFBIG : -EUCLEAN;
  2506. goto out_unlock;
  2507. }
  2508. /*
  2509. * If a sync write is in progress, we must wait, so that we
  2510. * can get a final snapshot value for size+mtime.
  2511. */
  2512. if (__ceph_have_pending_cap_snap(ci)) {
  2513. doutc(cl, "%p %llx.%llx cap_snap_pending\n", inode,
  2514. ceph_vinop(inode));
  2515. goto out_unlock;
  2516. }
  2517. }
  2518. if ((have & need) == need) {
  2519. /*
  2520. * Look at (implemented & ~have & not) so that we keep waiting
  2521. * on transition from wanted -> needed caps. This is needed
  2522. * for WRBUFFER|WR -> WR to avoid a new WR sync write from
  2523. * going before a prior buffered writeback happens.
  2524. *
  2525. * For RDCACHE|RD -> RD, there is not need to wait and we can
  2526. * just exclude the revoking caps and force to sync read.
  2527. */
  2528. int not = want & ~(have & need);
  2529. int revoking = implemented & ~have;
  2530. int exclude = revoking & not;
  2531. doutc(cl, "%p %llx.%llx have %s but not %s (revoking %s)\n",
  2532. inode, ceph_vinop(inode), ceph_cap_string(have),
  2533. ceph_cap_string(not), ceph_cap_string(revoking));
  2534. if (!exclude || !(exclude & CEPH_CAP_FILE_BUFFER)) {
  2535. if (!snap_rwsem_locked &&
  2536. !ci->i_head_snapc &&
  2537. (need & CEPH_CAP_FILE_WR)) {
  2538. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  2539. /*
  2540. * we can not call down_read() when
  2541. * task isn't in TASK_RUNNING state
  2542. */
  2543. if (flags & NON_BLOCKING) {
  2544. ret = -EAGAIN;
  2545. goto out_unlock;
  2546. }
  2547. spin_unlock(&ci->i_ceph_lock);
  2548. down_read(&mdsc->snap_rwsem);
  2549. snap_rwsem_locked = true;
  2550. goto again;
  2551. }
  2552. snap_rwsem_locked = true;
  2553. }
  2554. if ((have & want) == want)
  2555. *got = need | (want & ~exclude);
  2556. else
  2557. *got = need;
  2558. ceph_take_cap_refs(ci, *got, true);
  2559. ret = 1;
  2560. }
  2561. } else {
  2562. int session_readonly = false;
  2563. int mds_wanted;
  2564. if (ci->i_auth_cap &&
  2565. (need & (CEPH_CAP_FILE_WR | CEPH_CAP_FILE_EXCL))) {
  2566. struct ceph_mds_session *s = ci->i_auth_cap->session;
  2567. spin_lock(&s->s_cap_lock);
  2568. session_readonly = s->s_readonly;
  2569. spin_unlock(&s->s_cap_lock);
  2570. }
  2571. if (session_readonly) {
  2572. doutc(cl, "%p %llx.%llx need %s but mds%d readonly\n",
  2573. inode, ceph_vinop(inode), ceph_cap_string(need),
  2574. ci->i_auth_cap->mds);
  2575. ret = -EROFS;
  2576. goto out_unlock;
  2577. }
  2578. if (ceph_inode_is_shutdown(inode)) {
  2579. doutc(cl, "%p %llx.%llx inode is shutdown\n",
  2580. inode, ceph_vinop(inode));
  2581. ret = -ESTALE;
  2582. goto out_unlock;
  2583. }
  2584. mds_wanted = __ceph_caps_mds_wanted(ci, false);
  2585. if (need & ~mds_wanted) {
  2586. doutc(cl, "%p %llx.%llx need %s > mds_wanted %s\n",
  2587. inode, ceph_vinop(inode), ceph_cap_string(need),
  2588. ceph_cap_string(mds_wanted));
  2589. ret = -EUCLEAN;
  2590. goto out_unlock;
  2591. }
  2592. doutc(cl, "%p %llx.%llx have %s need %s\n", inode,
  2593. ceph_vinop(inode), ceph_cap_string(have),
  2594. ceph_cap_string(need));
  2595. }
  2596. out_unlock:
  2597. __ceph_touch_fmode(ci, mdsc, flags);
  2598. spin_unlock(&ci->i_ceph_lock);
  2599. if (snap_rwsem_locked)
  2600. up_read(&mdsc->snap_rwsem);
  2601. if (!ret)
  2602. ceph_update_cap_mis(&mdsc->metric);
  2603. else if (ret == 1)
  2604. ceph_update_cap_hit(&mdsc->metric);
  2605. doutc(cl, "%p %llx.%llx ret %d got %s\n", inode,
  2606. ceph_vinop(inode), ret, ceph_cap_string(*got));
  2607. return ret;
  2608. }
  2609. /*
  2610. * Check the offset we are writing up to against our current
  2611. * max_size. If necessary, tell the MDS we want to write to
  2612. * a larger offset.
  2613. */
  2614. static void check_max_size(struct inode *inode, loff_t endoff)
  2615. {
  2616. struct ceph_inode_info *ci = ceph_inode(inode);
  2617. struct ceph_client *cl = ceph_inode_to_client(inode);
  2618. int check = 0;
  2619. /* do we need to explicitly request a larger max_size? */
  2620. spin_lock(&ci->i_ceph_lock);
  2621. if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
  2622. doutc(cl, "write %p %llx.%llx at large endoff %llu, req max_size\n",
  2623. inode, ceph_vinop(inode), endoff);
  2624. ci->i_wanted_max_size = endoff;
  2625. }
  2626. /* duplicate ceph_check_caps()'s logic */
  2627. if (ci->i_auth_cap &&
  2628. (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
  2629. ci->i_wanted_max_size > ci->i_max_size &&
  2630. ci->i_wanted_max_size > ci->i_requested_max_size)
  2631. check = 1;
  2632. spin_unlock(&ci->i_ceph_lock);
  2633. if (check)
  2634. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY);
  2635. }
  2636. static inline int get_used_fmode(int caps)
  2637. {
  2638. int fmode = 0;
  2639. if (caps & CEPH_CAP_FILE_RD)
  2640. fmode |= CEPH_FILE_MODE_RD;
  2641. if (caps & CEPH_CAP_FILE_WR)
  2642. fmode |= CEPH_FILE_MODE_WR;
  2643. return fmode;
  2644. }
  2645. int ceph_try_get_caps(struct inode *inode, int need, int want,
  2646. bool nonblock, int *got)
  2647. {
  2648. int ret, flags;
  2649. BUG_ON(need & ~CEPH_CAP_FILE_RD);
  2650. BUG_ON(want & ~(CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO |
  2651. CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
  2652. CEPH_CAP_ANY_DIR_OPS));
  2653. if (need) {
  2654. ret = ceph_pool_perm_check(inode, need);
  2655. if (ret < 0)
  2656. return ret;
  2657. }
  2658. flags = get_used_fmode(need | want);
  2659. if (nonblock)
  2660. flags |= NON_BLOCKING;
  2661. ret = try_get_cap_refs(inode, need, want, 0, flags, got);
  2662. /* three special error codes */
  2663. if (ret == -EAGAIN || ret == -EFBIG || ret == -EUCLEAN)
  2664. ret = 0;
  2665. return ret;
  2666. }
  2667. /*
  2668. * Wait for caps, and take cap references. If we can't get a WR cap
  2669. * due to a small max_size, make sure we check_max_size (and possibly
  2670. * ask the mds) so we don't get hung up indefinitely.
  2671. */
  2672. int __ceph_get_caps(struct inode *inode, struct ceph_file_info *fi, int need,
  2673. int want, loff_t endoff, int *got)
  2674. {
  2675. struct ceph_inode_info *ci = ceph_inode(inode);
  2676. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  2677. int ret, _got, flags;
  2678. ret = ceph_pool_perm_check(inode, need);
  2679. if (ret < 0)
  2680. return ret;
  2681. if (fi && (fi->fmode & CEPH_FILE_MODE_WR) &&
  2682. fi->filp_gen != READ_ONCE(fsc->filp_gen))
  2683. return -EBADF;
  2684. flags = get_used_fmode(need | want);
  2685. while (true) {
  2686. flags &= CEPH_FILE_MODE_MASK;
  2687. if (vfs_inode_has_locks(inode))
  2688. flags |= CHECK_FILELOCK;
  2689. _got = 0;
  2690. ret = try_get_cap_refs(inode, need, want, endoff,
  2691. flags, &_got);
  2692. WARN_ON_ONCE(ret == -EAGAIN);
  2693. if (!ret) {
  2694. #ifdef CONFIG_DEBUG_FS
  2695. struct ceph_mds_client *mdsc = fsc->mdsc;
  2696. struct cap_wait cw;
  2697. #endif
  2698. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2699. #ifdef CONFIG_DEBUG_FS
  2700. cw.ino = ceph_ino(inode);
  2701. cw.tgid = current->tgid;
  2702. cw.need = need;
  2703. cw.want = want;
  2704. spin_lock(&mdsc->caps_list_lock);
  2705. list_add(&cw.list, &mdsc->cap_wait_list);
  2706. spin_unlock(&mdsc->caps_list_lock);
  2707. #endif
  2708. /* make sure used fmode not timeout */
  2709. ceph_get_fmode(ci, flags, FMODE_WAIT_BIAS);
  2710. add_wait_queue(&ci->i_cap_wq, &wait);
  2711. flags |= NON_BLOCKING;
  2712. while (!(ret = try_get_cap_refs(inode, need, want,
  2713. endoff, flags, &_got))) {
  2714. if (signal_pending(current)) {
  2715. ret = -ERESTARTSYS;
  2716. break;
  2717. }
  2718. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  2719. }
  2720. remove_wait_queue(&ci->i_cap_wq, &wait);
  2721. ceph_put_fmode(ci, flags, FMODE_WAIT_BIAS);
  2722. #ifdef CONFIG_DEBUG_FS
  2723. spin_lock(&mdsc->caps_list_lock);
  2724. list_del(&cw.list);
  2725. spin_unlock(&mdsc->caps_list_lock);
  2726. #endif
  2727. if (ret == -EAGAIN)
  2728. continue;
  2729. }
  2730. if (fi && (fi->fmode & CEPH_FILE_MODE_WR) &&
  2731. fi->filp_gen != READ_ONCE(fsc->filp_gen)) {
  2732. if (ret >= 0 && _got)
  2733. ceph_put_cap_refs(ci, _got);
  2734. return -EBADF;
  2735. }
  2736. if (ret < 0) {
  2737. if (ret == -EFBIG || ret == -EUCLEAN) {
  2738. int ret2 = ceph_wait_on_async_create(inode);
  2739. if (ret2 < 0)
  2740. return ret2;
  2741. }
  2742. if (ret == -EFBIG) {
  2743. check_max_size(inode, endoff);
  2744. continue;
  2745. }
  2746. if (ret == -EUCLEAN) {
  2747. /* session was killed, try renew caps */
  2748. ret = ceph_renew_caps(inode, flags);
  2749. if (ret == 0)
  2750. continue;
  2751. }
  2752. return ret;
  2753. }
  2754. if (S_ISREG(ci->netfs.inode.i_mode) &&
  2755. ceph_has_inline_data(ci) &&
  2756. (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
  2757. i_size_read(inode) > 0) {
  2758. struct page *page =
  2759. find_get_page(inode->i_mapping, 0);
  2760. if (page) {
  2761. bool uptodate = PageUptodate(page);
  2762. put_page(page);
  2763. if (uptodate)
  2764. break;
  2765. }
  2766. /*
  2767. * drop cap refs first because getattr while
  2768. * holding * caps refs can cause deadlock.
  2769. */
  2770. ceph_put_cap_refs(ci, _got);
  2771. _got = 0;
  2772. /*
  2773. * getattr request will bring inline data into
  2774. * page cache
  2775. */
  2776. ret = __ceph_do_getattr(inode, NULL,
  2777. CEPH_STAT_CAP_INLINE_DATA,
  2778. true);
  2779. if (ret < 0)
  2780. return ret;
  2781. continue;
  2782. }
  2783. break;
  2784. }
  2785. *got = _got;
  2786. return 0;
  2787. }
  2788. int ceph_get_caps(struct file *filp, int need, int want, loff_t endoff,
  2789. int *got)
  2790. {
  2791. struct ceph_file_info *fi = filp->private_data;
  2792. struct inode *inode = file_inode(filp);
  2793. return __ceph_get_caps(inode, fi, need, want, endoff, got);
  2794. }
  2795. /*
  2796. * Take cap refs. Caller must already know we hold at least one ref
  2797. * on the caps in question or we don't know this is safe.
  2798. */
  2799. void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
  2800. {
  2801. spin_lock(&ci->i_ceph_lock);
  2802. ceph_take_cap_refs(ci, caps, false);
  2803. spin_unlock(&ci->i_ceph_lock);
  2804. }
  2805. /*
  2806. * drop cap_snap that is not associated with any snapshot.
  2807. * we don't need to send FLUSHSNAP message for it.
  2808. */
  2809. static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
  2810. struct ceph_cap_snap *capsnap)
  2811. {
  2812. struct inode *inode = &ci->netfs.inode;
  2813. struct ceph_client *cl = ceph_inode_to_client(inode);
  2814. if (!capsnap->need_flush &&
  2815. !capsnap->writing && !capsnap->dirty_pages) {
  2816. doutc(cl, "%p follows %llu\n", capsnap, capsnap->follows);
  2817. BUG_ON(capsnap->cap_flush.tid > 0);
  2818. ceph_put_snap_context(capsnap->context);
  2819. if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
  2820. ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  2821. list_del(&capsnap->ci_item);
  2822. ceph_put_cap_snap(capsnap);
  2823. return 1;
  2824. }
  2825. return 0;
  2826. }
  2827. enum put_cap_refs_mode {
  2828. PUT_CAP_REFS_SYNC = 0,
  2829. PUT_CAP_REFS_ASYNC,
  2830. };
  2831. /*
  2832. * Release cap refs.
  2833. *
  2834. * If we released the last ref on any given cap, call ceph_check_caps
  2835. * to release (or schedule a release).
  2836. *
  2837. * If we are releasing a WR cap (from a sync write), finalize any affected
  2838. * cap_snap, and wake up any waiters.
  2839. */
  2840. static void __ceph_put_cap_refs(struct ceph_inode_info *ci, int had,
  2841. enum put_cap_refs_mode mode)
  2842. {
  2843. struct inode *inode = &ci->netfs.inode;
  2844. struct ceph_client *cl = ceph_inode_to_client(inode);
  2845. int last = 0, put = 0, flushsnaps = 0, wake = 0;
  2846. bool check_flushsnaps = false;
  2847. spin_lock(&ci->i_ceph_lock);
  2848. if (had & CEPH_CAP_PIN)
  2849. --ci->i_pin_ref;
  2850. if (had & CEPH_CAP_FILE_RD)
  2851. if (--ci->i_rd_ref == 0)
  2852. last++;
  2853. if (had & CEPH_CAP_FILE_CACHE)
  2854. if (--ci->i_rdcache_ref == 0)
  2855. last++;
  2856. if (had & CEPH_CAP_FILE_EXCL)
  2857. if (--ci->i_fx_ref == 0)
  2858. last++;
  2859. if (had & CEPH_CAP_FILE_BUFFER) {
  2860. if (--ci->i_wb_ref == 0) {
  2861. last++;
  2862. /* put the ref held by ceph_take_cap_refs() */
  2863. put++;
  2864. check_flushsnaps = true;
  2865. }
  2866. doutc(cl, "%p %llx.%llx wb %d -> %d (?)\n", inode,
  2867. ceph_vinop(inode), ci->i_wb_ref+1, ci->i_wb_ref);
  2868. }
  2869. if (had & CEPH_CAP_FILE_WR) {
  2870. if (--ci->i_wr_ref == 0) {
  2871. /*
  2872. * The Fb caps will always be took and released
  2873. * together with the Fw caps.
  2874. */
  2875. WARN_ON_ONCE(ci->i_wb_ref);
  2876. last++;
  2877. check_flushsnaps = true;
  2878. if (ci->i_wrbuffer_ref_head == 0 &&
  2879. ci->i_dirty_caps == 0 &&
  2880. ci->i_flushing_caps == 0) {
  2881. BUG_ON(!ci->i_head_snapc);
  2882. ceph_put_snap_context(ci->i_head_snapc);
  2883. ci->i_head_snapc = NULL;
  2884. }
  2885. /* see comment in __ceph_remove_cap() */
  2886. if (!__ceph_is_any_real_caps(ci) && ci->i_snap_realm)
  2887. ceph_change_snap_realm(inode, NULL);
  2888. }
  2889. }
  2890. if (check_flushsnaps && __ceph_have_pending_cap_snap(ci)) {
  2891. struct ceph_cap_snap *capsnap =
  2892. list_last_entry(&ci->i_cap_snaps,
  2893. struct ceph_cap_snap,
  2894. ci_item);
  2895. capsnap->writing = 0;
  2896. if (ceph_try_drop_cap_snap(ci, capsnap))
  2897. /* put the ref held by ceph_queue_cap_snap() */
  2898. put++;
  2899. else if (__ceph_finish_cap_snap(ci, capsnap))
  2900. flushsnaps = 1;
  2901. wake = 1;
  2902. }
  2903. spin_unlock(&ci->i_ceph_lock);
  2904. doutc(cl, "%p %llx.%llx had %s%s%s\n", inode, ceph_vinop(inode),
  2905. ceph_cap_string(had), last ? " last" : "", put ? " put" : "");
  2906. switch (mode) {
  2907. case PUT_CAP_REFS_SYNC:
  2908. if (last)
  2909. ceph_check_caps(ci, 0);
  2910. else if (flushsnaps)
  2911. ceph_flush_snaps(ci, NULL);
  2912. break;
  2913. case PUT_CAP_REFS_ASYNC:
  2914. if (last)
  2915. ceph_queue_check_caps(inode);
  2916. else if (flushsnaps)
  2917. ceph_queue_flush_snaps(inode);
  2918. break;
  2919. default:
  2920. break;
  2921. }
  2922. if (wake)
  2923. wake_up_all(&ci->i_cap_wq);
  2924. while (put-- > 0)
  2925. iput(inode);
  2926. }
  2927. void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
  2928. {
  2929. __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_SYNC);
  2930. }
  2931. void ceph_put_cap_refs_async(struct ceph_inode_info *ci, int had)
  2932. {
  2933. __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_ASYNC);
  2934. }
  2935. /*
  2936. * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
  2937. * context. Adjust per-snap dirty page accounting as appropriate.
  2938. * Once all dirty data for a cap_snap is flushed, flush snapped file
  2939. * metadata back to the MDS. If we dropped the last ref, call
  2940. * ceph_check_caps.
  2941. */
  2942. void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
  2943. struct ceph_snap_context *snapc)
  2944. {
  2945. struct inode *inode = &ci->netfs.inode;
  2946. struct ceph_client *cl = ceph_inode_to_client(inode);
  2947. struct ceph_cap_snap *capsnap = NULL, *iter;
  2948. int put = 0;
  2949. bool last = false;
  2950. bool flush_snaps = false;
  2951. bool complete_capsnap = false;
  2952. spin_lock(&ci->i_ceph_lock);
  2953. ci->i_wrbuffer_ref -= nr;
  2954. if (ci->i_wrbuffer_ref == 0) {
  2955. last = true;
  2956. put++;
  2957. }
  2958. if (ci->i_head_snapc == snapc) {
  2959. ci->i_wrbuffer_ref_head -= nr;
  2960. if (ci->i_wrbuffer_ref_head == 0 &&
  2961. ci->i_wr_ref == 0 &&
  2962. ci->i_dirty_caps == 0 &&
  2963. ci->i_flushing_caps == 0) {
  2964. BUG_ON(!ci->i_head_snapc);
  2965. ceph_put_snap_context(ci->i_head_snapc);
  2966. ci->i_head_snapc = NULL;
  2967. }
  2968. doutc(cl, "on %p %llx.%llx head %d/%d -> %d/%d %s\n",
  2969. inode, ceph_vinop(inode), ci->i_wrbuffer_ref+nr,
  2970. ci->i_wrbuffer_ref_head+nr, ci->i_wrbuffer_ref,
  2971. ci->i_wrbuffer_ref_head, last ? " LAST" : "");
  2972. } else {
  2973. list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) {
  2974. if (iter->context == snapc) {
  2975. capsnap = iter;
  2976. break;
  2977. }
  2978. }
  2979. if (!capsnap) {
  2980. /*
  2981. * The capsnap should already be removed when removing
  2982. * auth cap in the case of a forced unmount.
  2983. */
  2984. WARN_ON_ONCE(ci->i_auth_cap);
  2985. goto unlock;
  2986. }
  2987. capsnap->dirty_pages -= nr;
  2988. if (capsnap->dirty_pages == 0) {
  2989. complete_capsnap = true;
  2990. if (!capsnap->writing) {
  2991. if (ceph_try_drop_cap_snap(ci, capsnap)) {
  2992. put++;
  2993. } else {
  2994. ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  2995. flush_snaps = true;
  2996. }
  2997. }
  2998. }
  2999. doutc(cl, "%p %llx.%llx cap_snap %p snap %lld %d/%d -> %d/%d %s%s\n",
  3000. inode, ceph_vinop(inode), capsnap, capsnap->context->seq,
  3001. ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
  3002. ci->i_wrbuffer_ref, capsnap->dirty_pages,
  3003. last ? " (wrbuffer last)" : "",
  3004. complete_capsnap ? " (complete capsnap)" : "");
  3005. }
  3006. unlock:
  3007. spin_unlock(&ci->i_ceph_lock);
  3008. if (last) {
  3009. ceph_check_caps(ci, 0);
  3010. } else if (flush_snaps) {
  3011. ceph_flush_snaps(ci, NULL);
  3012. }
  3013. if (complete_capsnap)
  3014. wake_up_all(&ci->i_cap_wq);
  3015. while (put-- > 0) {
  3016. iput(inode);
  3017. }
  3018. }
  3019. /*
  3020. * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
  3021. */
  3022. static void invalidate_aliases(struct inode *inode)
  3023. {
  3024. struct ceph_client *cl = ceph_inode_to_client(inode);
  3025. struct dentry *dn, *prev = NULL;
  3026. doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
  3027. d_prune_aliases(inode);
  3028. /*
  3029. * For non-directory inode, d_find_alias() only returns
  3030. * hashed dentry. After calling d_invalidate(), the
  3031. * dentry becomes unhashed.
  3032. *
  3033. * For directory inode, d_find_alias() can return
  3034. * unhashed dentry. But directory inode should have
  3035. * one alias at most.
  3036. */
  3037. while ((dn = d_find_alias(inode))) {
  3038. if (dn == prev) {
  3039. dput(dn);
  3040. break;
  3041. }
  3042. d_invalidate(dn);
  3043. if (prev)
  3044. dput(prev);
  3045. prev = dn;
  3046. }
  3047. if (prev)
  3048. dput(prev);
  3049. }
  3050. struct cap_extra_info {
  3051. struct ceph_string *pool_ns;
  3052. /* inline data */
  3053. u64 inline_version;
  3054. void *inline_data;
  3055. u32 inline_len;
  3056. /* dirstat */
  3057. bool dirstat_valid;
  3058. u64 nfiles;
  3059. u64 nsubdirs;
  3060. u64 change_attr;
  3061. /* currently issued */
  3062. int issued;
  3063. struct timespec64 btime;
  3064. u8 *fscrypt_auth;
  3065. u32 fscrypt_auth_len;
  3066. u64 fscrypt_file_size;
  3067. };
  3068. /*
  3069. * Handle a cap GRANT message from the MDS. (Note that a GRANT may
  3070. * actually be a revocation if it specifies a smaller cap set.)
  3071. *
  3072. * caller holds s_mutex and i_ceph_lock, we drop both.
  3073. */
  3074. static void handle_cap_grant(struct inode *inode,
  3075. struct ceph_mds_session *session,
  3076. struct ceph_cap *cap,
  3077. struct ceph_mds_caps *grant,
  3078. struct ceph_buffer *xattr_buf,
  3079. struct cap_extra_info *extra_info)
  3080. __releases(ci->i_ceph_lock)
  3081. __releases(session->s_mdsc->snap_rwsem)
  3082. {
  3083. struct ceph_client *cl = ceph_inode_to_client(inode);
  3084. struct ceph_inode_info *ci = ceph_inode(inode);
  3085. int seq = le32_to_cpu(grant->seq);
  3086. int newcaps = le32_to_cpu(grant->caps);
  3087. int used, wanted, dirty;
  3088. u64 size = le64_to_cpu(grant->size);
  3089. u64 max_size = le64_to_cpu(grant->max_size);
  3090. unsigned char check_caps = 0;
  3091. bool was_stale = cap->cap_gen < atomic_read(&session->s_cap_gen);
  3092. bool wake = false;
  3093. bool writeback = false;
  3094. bool queue_trunc = false;
  3095. bool queue_invalidate = false;
  3096. bool deleted_inode = false;
  3097. bool fill_inline = false;
  3098. bool revoke_wait = false;
  3099. int flags = 0;
  3100. /*
  3101. * If there is at least one crypto block then we'll trust
  3102. * fscrypt_file_size. If the real length of the file is 0, then
  3103. * ignore it (it has probably been truncated down to 0 by the MDS).
  3104. */
  3105. if (IS_ENCRYPTED(inode) && size)
  3106. size = extra_info->fscrypt_file_size;
  3107. doutc(cl, "%p %llx.%llx cap %p mds%d seq %d %s\n", inode,
  3108. ceph_vinop(inode), cap, session->s_mds, seq,
  3109. ceph_cap_string(newcaps));
  3110. doutc(cl, " size %llu max_size %llu, i_size %llu\n", size,
  3111. max_size, i_size_read(inode));
  3112. /*
  3113. * If CACHE is being revoked, and we have no dirty buffers,
  3114. * try to invalidate (once). (If there are dirty buffers, we
  3115. * will invalidate _after_ writeback.)
  3116. */
  3117. if (S_ISREG(inode->i_mode) && /* don't invalidate readdir cache */
  3118. ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
  3119. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  3120. !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
  3121. if (try_nonblocking_invalidate(inode)) {
  3122. /* there were locked pages.. invalidate later
  3123. in a separate thread. */
  3124. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  3125. queue_invalidate = true;
  3126. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  3127. }
  3128. }
  3129. }
  3130. if (was_stale)
  3131. cap->issued = cap->implemented = CEPH_CAP_PIN;
  3132. /*
  3133. * auth mds of the inode changed. we received the cap export message,
  3134. * but still haven't received the cap import message. handle_cap_export
  3135. * updated the new auth MDS' cap.
  3136. *
  3137. * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
  3138. * that was sent before the cap import message. So don't remove caps.
  3139. */
  3140. if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  3141. WARN_ON(cap != ci->i_auth_cap);
  3142. WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
  3143. seq = cap->seq;
  3144. newcaps |= cap->issued;
  3145. }
  3146. /* side effects now are allowed */
  3147. cap->cap_gen = atomic_read(&session->s_cap_gen);
  3148. cap->seq = seq;
  3149. __check_cap_issue(ci, cap, newcaps);
  3150. inode_set_max_iversion_raw(inode, extra_info->change_attr);
  3151. if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
  3152. (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
  3153. umode_t mode = le32_to_cpu(grant->mode);
  3154. if (inode_wrong_type(inode, mode))
  3155. pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
  3156. ceph_vinop(inode), inode->i_mode, mode);
  3157. else
  3158. inode->i_mode = mode;
  3159. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
  3160. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
  3161. ci->i_btime = extra_info->btime;
  3162. doutc(cl, "%p %llx.%llx mode 0%o uid.gid %d.%d\n", inode,
  3163. ceph_vinop(inode), inode->i_mode,
  3164. from_kuid(&init_user_ns, inode->i_uid),
  3165. from_kgid(&init_user_ns, inode->i_gid));
  3166. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  3167. if (ci->fscrypt_auth_len != extra_info->fscrypt_auth_len ||
  3168. memcmp(ci->fscrypt_auth, extra_info->fscrypt_auth,
  3169. ci->fscrypt_auth_len))
  3170. pr_warn_ratelimited_client(cl,
  3171. "cap grant attempt to change fscrypt_auth on non-I_NEW inode (old len %d new len %d)\n",
  3172. ci->fscrypt_auth_len,
  3173. extra_info->fscrypt_auth_len);
  3174. #endif
  3175. }
  3176. if ((newcaps & CEPH_CAP_LINK_SHARED) &&
  3177. (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
  3178. set_nlink(inode, le32_to_cpu(grant->nlink));
  3179. if (inode->i_nlink == 0)
  3180. deleted_inode = true;
  3181. }
  3182. if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
  3183. grant->xattr_len) {
  3184. int len = le32_to_cpu(grant->xattr_len);
  3185. u64 version = le64_to_cpu(grant->xattr_version);
  3186. if (version > ci->i_xattrs.version) {
  3187. doutc(cl, " got new xattrs v%llu on %p %llx.%llx len %d\n",
  3188. version, inode, ceph_vinop(inode), len);
  3189. if (ci->i_xattrs.blob)
  3190. ceph_buffer_put(ci->i_xattrs.blob);
  3191. ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
  3192. ci->i_xattrs.version = version;
  3193. ceph_forget_all_cached_acls(inode);
  3194. ceph_security_invalidate_secctx(inode);
  3195. }
  3196. }
  3197. if (newcaps & CEPH_CAP_ANY_RD) {
  3198. struct timespec64 mtime, atime, ctime;
  3199. /* ctime/mtime/atime? */
  3200. ceph_decode_timespec64(&mtime, &grant->mtime);
  3201. ceph_decode_timespec64(&atime, &grant->atime);
  3202. ceph_decode_timespec64(&ctime, &grant->ctime);
  3203. ceph_fill_file_time(inode, extra_info->issued,
  3204. le32_to_cpu(grant->time_warp_seq),
  3205. &ctime, &mtime, &atime);
  3206. }
  3207. if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
  3208. ci->i_files = extra_info->nfiles;
  3209. ci->i_subdirs = extra_info->nsubdirs;
  3210. }
  3211. if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
  3212. /* file layout may have changed */
  3213. s64 old_pool = ci->i_layout.pool_id;
  3214. struct ceph_string *old_ns;
  3215. ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
  3216. old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
  3217. lockdep_is_held(&ci->i_ceph_lock));
  3218. rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
  3219. if (ci->i_layout.pool_id != old_pool ||
  3220. extra_info->pool_ns != old_ns)
  3221. ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
  3222. extra_info->pool_ns = old_ns;
  3223. /* size/truncate_seq? */
  3224. queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
  3225. le32_to_cpu(grant->truncate_seq),
  3226. le64_to_cpu(grant->truncate_size),
  3227. size);
  3228. }
  3229. if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
  3230. if (max_size != ci->i_max_size) {
  3231. doutc(cl, "max_size %lld -> %llu\n", ci->i_max_size,
  3232. max_size);
  3233. ci->i_max_size = max_size;
  3234. if (max_size >= ci->i_wanted_max_size) {
  3235. ci->i_wanted_max_size = 0; /* reset */
  3236. ci->i_requested_max_size = 0;
  3237. }
  3238. wake = true;
  3239. }
  3240. }
  3241. /* check cap bits */
  3242. wanted = __ceph_caps_wanted(ci);
  3243. used = __ceph_caps_used(ci);
  3244. dirty = __ceph_caps_dirty(ci);
  3245. doutc(cl, " my wanted = %s, used = %s, dirty %s\n",
  3246. ceph_cap_string(wanted), ceph_cap_string(used),
  3247. ceph_cap_string(dirty));
  3248. if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) &&
  3249. (wanted & ~(cap->mds_wanted | newcaps))) {
  3250. /*
  3251. * If mds is importing cap, prior cap messages that update
  3252. * 'wanted' may get dropped by mds (migrate seq mismatch).
  3253. *
  3254. * We don't send cap message to update 'wanted' if what we
  3255. * want are already issued. If mds revokes caps, cap message
  3256. * that releases caps also tells mds what we want. But if
  3257. * caps got revoked by mds forcedly (session stale). We may
  3258. * haven't told mds what we want.
  3259. */
  3260. check_caps = 1;
  3261. }
  3262. /* revocation, grant, or no-op? */
  3263. if (cap->issued & ~newcaps) {
  3264. int revoking = cap->issued & ~newcaps;
  3265. doutc(cl, "revocation: %s -> %s (revoking %s)\n",
  3266. ceph_cap_string(cap->issued), ceph_cap_string(newcaps),
  3267. ceph_cap_string(revoking));
  3268. if (S_ISREG(inode->i_mode) &&
  3269. (revoking & used & CEPH_CAP_FILE_BUFFER)) {
  3270. writeback = true; /* initiate writeback; will delay ack */
  3271. revoke_wait = true;
  3272. } else if (queue_invalidate &&
  3273. revoking == CEPH_CAP_FILE_CACHE &&
  3274. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0) {
  3275. revoke_wait = true; /* do nothing yet, invalidation will be queued */
  3276. } else if (cap == ci->i_auth_cap) {
  3277. check_caps = 1; /* check auth cap only */
  3278. } else {
  3279. check_caps = 2; /* check all caps */
  3280. }
  3281. /* If there is new caps, try to wake up the waiters */
  3282. if (~cap->issued & newcaps)
  3283. wake = true;
  3284. cap->issued = newcaps;
  3285. cap->implemented |= newcaps;
  3286. } else if (cap->issued == newcaps) {
  3287. doutc(cl, "caps unchanged: %s -> %s\n",
  3288. ceph_cap_string(cap->issued),
  3289. ceph_cap_string(newcaps));
  3290. } else {
  3291. doutc(cl, "grant: %s -> %s\n", ceph_cap_string(cap->issued),
  3292. ceph_cap_string(newcaps));
  3293. /* non-auth MDS is revoking the newly grant caps ? */
  3294. if (cap == ci->i_auth_cap &&
  3295. __ceph_caps_revoking_other(ci, cap, newcaps))
  3296. check_caps = 2;
  3297. cap->issued = newcaps;
  3298. cap->implemented |= newcaps; /* add bits only, to
  3299. * avoid stepping on a
  3300. * pending revocation */
  3301. wake = true;
  3302. }
  3303. BUG_ON(cap->issued & ~cap->implemented);
  3304. /* don't let check_caps skip sending a response to MDS for revoke msgs */
  3305. if (!revoke_wait && le32_to_cpu(grant->op) == CEPH_CAP_OP_REVOKE) {
  3306. cap->mds_wanted = 0;
  3307. flags |= CHECK_CAPS_FLUSH_FORCE;
  3308. if (cap == ci->i_auth_cap)
  3309. check_caps = 1; /* check auth cap only */
  3310. else
  3311. check_caps = 2; /* check all caps */
  3312. }
  3313. if (extra_info->inline_version > 0 &&
  3314. extra_info->inline_version >= ci->i_inline_version) {
  3315. ci->i_inline_version = extra_info->inline_version;
  3316. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  3317. (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
  3318. fill_inline = true;
  3319. }
  3320. if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
  3321. if (ci->i_auth_cap == cap) {
  3322. if (newcaps & ~extra_info->issued)
  3323. wake = true;
  3324. if (ci->i_requested_max_size > max_size ||
  3325. !(le32_to_cpu(grant->wanted) & CEPH_CAP_ANY_FILE_WR)) {
  3326. /* re-request max_size if necessary */
  3327. ci->i_requested_max_size = 0;
  3328. wake = true;
  3329. }
  3330. ceph_kick_flushing_inode_caps(session, ci);
  3331. }
  3332. up_read(&session->s_mdsc->snap_rwsem);
  3333. }
  3334. spin_unlock(&ci->i_ceph_lock);
  3335. if (fill_inline)
  3336. ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
  3337. extra_info->inline_len);
  3338. if (queue_trunc)
  3339. ceph_queue_vmtruncate(inode);
  3340. if (writeback)
  3341. /*
  3342. * queue inode for writeback: we can't actually call
  3343. * filemap_write_and_wait, etc. from message handler
  3344. * context.
  3345. */
  3346. ceph_queue_writeback(inode);
  3347. if (queue_invalidate)
  3348. ceph_queue_invalidate(inode);
  3349. if (deleted_inode)
  3350. invalidate_aliases(inode);
  3351. if (wake)
  3352. wake_up_all(&ci->i_cap_wq);
  3353. mutex_unlock(&session->s_mutex);
  3354. if (check_caps == 1)
  3355. ceph_check_caps(ci, flags | CHECK_CAPS_AUTHONLY | CHECK_CAPS_NOINVAL);
  3356. else if (check_caps == 2)
  3357. ceph_check_caps(ci, flags | CHECK_CAPS_NOINVAL);
  3358. }
  3359. /*
  3360. * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
  3361. * MDS has been safely committed.
  3362. */
  3363. static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
  3364. struct ceph_mds_caps *m,
  3365. struct ceph_mds_session *session,
  3366. struct ceph_cap *cap)
  3367. __releases(ci->i_ceph_lock)
  3368. {
  3369. struct ceph_inode_info *ci = ceph_inode(inode);
  3370. struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  3371. struct ceph_client *cl = mdsc->fsc->client;
  3372. struct ceph_cap_flush *cf, *tmp_cf;
  3373. LIST_HEAD(to_remove);
  3374. unsigned seq = le32_to_cpu(m->seq);
  3375. int dirty = le32_to_cpu(m->dirty);
  3376. int cleaned = 0;
  3377. bool drop = false;
  3378. bool wake_ci = false;
  3379. bool wake_mdsc = false;
  3380. list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
  3381. /* Is this the one that was flushed? */
  3382. if (cf->tid == flush_tid)
  3383. cleaned = cf->caps;
  3384. /* Is this a capsnap? */
  3385. if (cf->is_capsnap)
  3386. continue;
  3387. if (cf->tid <= flush_tid) {
  3388. /*
  3389. * An earlier or current tid. The FLUSH_ACK should
  3390. * represent a superset of this flush's caps.
  3391. */
  3392. wake_ci |= __detach_cap_flush_from_ci(ci, cf);
  3393. list_add_tail(&cf->i_list, &to_remove);
  3394. } else {
  3395. /*
  3396. * This is a later one. Any caps in it are still dirty
  3397. * so don't count them as cleaned.
  3398. */
  3399. cleaned &= ~cf->caps;
  3400. if (!cleaned)
  3401. break;
  3402. }
  3403. }
  3404. doutc(cl, "%p %llx.%llx mds%d seq %d on %s cleaned %s, flushing %s -> %s\n",
  3405. inode, ceph_vinop(inode), session->s_mds, seq,
  3406. ceph_cap_string(dirty), ceph_cap_string(cleaned),
  3407. ceph_cap_string(ci->i_flushing_caps),
  3408. ceph_cap_string(ci->i_flushing_caps & ~cleaned));
  3409. if (list_empty(&to_remove) && !cleaned)
  3410. goto out;
  3411. ci->i_flushing_caps &= ~cleaned;
  3412. spin_lock(&mdsc->cap_dirty_lock);
  3413. list_for_each_entry(cf, &to_remove, i_list)
  3414. wake_mdsc |= __detach_cap_flush_from_mdsc(mdsc, cf);
  3415. if (ci->i_flushing_caps == 0) {
  3416. if (list_empty(&ci->i_cap_flush_list)) {
  3417. list_del_init(&ci->i_flushing_item);
  3418. if (!list_empty(&session->s_cap_flushing)) {
  3419. struct inode *inode =
  3420. &list_first_entry(&session->s_cap_flushing,
  3421. struct ceph_inode_info,
  3422. i_flushing_item)->netfs.inode;
  3423. doutc(cl, " mds%d still flushing cap on %p %llx.%llx\n",
  3424. session->s_mds, inode, ceph_vinop(inode));
  3425. }
  3426. }
  3427. mdsc->num_cap_flushing--;
  3428. doutc(cl, " %p %llx.%llx now !flushing\n", inode,
  3429. ceph_vinop(inode));
  3430. if (ci->i_dirty_caps == 0) {
  3431. doutc(cl, " %p %llx.%llx now clean\n", inode,
  3432. ceph_vinop(inode));
  3433. BUG_ON(!list_empty(&ci->i_dirty_item));
  3434. drop = true;
  3435. if (ci->i_wr_ref == 0 &&
  3436. ci->i_wrbuffer_ref_head == 0) {
  3437. BUG_ON(!ci->i_head_snapc);
  3438. ceph_put_snap_context(ci->i_head_snapc);
  3439. ci->i_head_snapc = NULL;
  3440. }
  3441. } else {
  3442. BUG_ON(list_empty(&ci->i_dirty_item));
  3443. }
  3444. }
  3445. spin_unlock(&mdsc->cap_dirty_lock);
  3446. out:
  3447. spin_unlock(&ci->i_ceph_lock);
  3448. while (!list_empty(&to_remove)) {
  3449. cf = list_first_entry(&to_remove,
  3450. struct ceph_cap_flush, i_list);
  3451. list_del_init(&cf->i_list);
  3452. if (!cf->is_capsnap)
  3453. ceph_free_cap_flush(cf);
  3454. }
  3455. if (wake_ci)
  3456. wake_up_all(&ci->i_cap_wq);
  3457. if (wake_mdsc)
  3458. wake_up_all(&mdsc->cap_flushing_wq);
  3459. if (drop)
  3460. iput(inode);
  3461. }
  3462. void __ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap,
  3463. bool *wake_ci, bool *wake_mdsc)
  3464. {
  3465. struct ceph_inode_info *ci = ceph_inode(inode);
  3466. struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  3467. struct ceph_client *cl = mdsc->fsc->client;
  3468. bool ret;
  3469. lockdep_assert_held(&ci->i_ceph_lock);
  3470. doutc(cl, "removing capsnap %p, %p %llx.%llx ci %p\n", capsnap,
  3471. inode, ceph_vinop(inode), ci);
  3472. list_del_init(&capsnap->ci_item);
  3473. ret = __detach_cap_flush_from_ci(ci, &capsnap->cap_flush);
  3474. if (wake_ci)
  3475. *wake_ci = ret;
  3476. spin_lock(&mdsc->cap_dirty_lock);
  3477. if (list_empty(&ci->i_cap_flush_list))
  3478. list_del_init(&ci->i_flushing_item);
  3479. ret = __detach_cap_flush_from_mdsc(mdsc, &capsnap->cap_flush);
  3480. if (wake_mdsc)
  3481. *wake_mdsc = ret;
  3482. spin_unlock(&mdsc->cap_dirty_lock);
  3483. }
  3484. void ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap,
  3485. bool *wake_ci, bool *wake_mdsc)
  3486. {
  3487. struct ceph_inode_info *ci = ceph_inode(inode);
  3488. lockdep_assert_held(&ci->i_ceph_lock);
  3489. WARN_ON_ONCE(capsnap->dirty_pages || capsnap->writing);
  3490. __ceph_remove_capsnap(inode, capsnap, wake_ci, wake_mdsc);
  3491. }
  3492. /*
  3493. * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
  3494. * throw away our cap_snap.
  3495. *
  3496. * Caller hold s_mutex.
  3497. */
  3498. static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
  3499. struct ceph_mds_caps *m,
  3500. struct ceph_mds_session *session)
  3501. {
  3502. struct ceph_inode_info *ci = ceph_inode(inode);
  3503. struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  3504. struct ceph_client *cl = mdsc->fsc->client;
  3505. u64 follows = le64_to_cpu(m->snap_follows);
  3506. struct ceph_cap_snap *capsnap = NULL, *iter;
  3507. bool wake_ci = false;
  3508. bool wake_mdsc = false;
  3509. doutc(cl, "%p %llx.%llx ci %p mds%d follows %lld\n", inode,
  3510. ceph_vinop(inode), ci, session->s_mds, follows);
  3511. spin_lock(&ci->i_ceph_lock);
  3512. list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) {
  3513. if (iter->follows == follows) {
  3514. if (iter->cap_flush.tid != flush_tid) {
  3515. doutc(cl, " cap_snap %p follows %lld "
  3516. "tid %lld != %lld\n", iter,
  3517. follows, flush_tid,
  3518. iter->cap_flush.tid);
  3519. break;
  3520. }
  3521. capsnap = iter;
  3522. break;
  3523. } else {
  3524. doutc(cl, " skipping cap_snap %p follows %lld\n",
  3525. iter, iter->follows);
  3526. }
  3527. }
  3528. if (capsnap)
  3529. ceph_remove_capsnap(inode, capsnap, &wake_ci, &wake_mdsc);
  3530. spin_unlock(&ci->i_ceph_lock);
  3531. if (capsnap) {
  3532. ceph_put_snap_context(capsnap->context);
  3533. ceph_put_cap_snap(capsnap);
  3534. if (wake_ci)
  3535. wake_up_all(&ci->i_cap_wq);
  3536. if (wake_mdsc)
  3537. wake_up_all(&mdsc->cap_flushing_wq);
  3538. iput(inode);
  3539. }
  3540. }
  3541. /*
  3542. * Handle TRUNC from MDS, indicating file truncation.
  3543. *
  3544. * caller hold s_mutex.
  3545. */
  3546. static bool handle_cap_trunc(struct inode *inode,
  3547. struct ceph_mds_caps *trunc,
  3548. struct ceph_mds_session *session,
  3549. struct cap_extra_info *extra_info)
  3550. {
  3551. struct ceph_inode_info *ci = ceph_inode(inode);
  3552. struct ceph_client *cl = ceph_inode_to_client(inode);
  3553. int mds = session->s_mds;
  3554. int seq = le32_to_cpu(trunc->seq);
  3555. u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
  3556. u64 truncate_size = le64_to_cpu(trunc->truncate_size);
  3557. u64 size = le64_to_cpu(trunc->size);
  3558. int implemented = 0;
  3559. int dirty = __ceph_caps_dirty(ci);
  3560. int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
  3561. bool queue_trunc = false;
  3562. lockdep_assert_held(&ci->i_ceph_lock);
  3563. issued |= implemented | dirty;
  3564. /*
  3565. * If there is at least one crypto block then we'll trust
  3566. * fscrypt_file_size. If the real length of the file is 0, then
  3567. * ignore it (it has probably been truncated down to 0 by the MDS).
  3568. */
  3569. if (IS_ENCRYPTED(inode) && size)
  3570. size = extra_info->fscrypt_file_size;
  3571. doutc(cl, "%p %llx.%llx mds%d seq %d to %lld truncate seq %d\n",
  3572. inode, ceph_vinop(inode), mds, seq, truncate_size, truncate_seq);
  3573. queue_trunc = ceph_fill_file_size(inode, issued,
  3574. truncate_seq, truncate_size, size);
  3575. return queue_trunc;
  3576. }
  3577. /*
  3578. * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
  3579. * different one. If we are the most recent migration we've seen (as
  3580. * indicated by mseq), make note of the migrating cap bits for the
  3581. * duration (until we see the corresponding IMPORT).
  3582. *
  3583. * caller holds s_mutex
  3584. */
  3585. static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
  3586. struct ceph_mds_cap_peer *ph,
  3587. struct ceph_mds_session *session)
  3588. {
  3589. struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
  3590. struct ceph_client *cl = mdsc->fsc->client;
  3591. struct ceph_mds_session *tsession = NULL;
  3592. struct ceph_cap *cap, *tcap, *new_cap = NULL;
  3593. struct ceph_inode_info *ci = ceph_inode(inode);
  3594. u64 t_cap_id;
  3595. unsigned mseq = le32_to_cpu(ex->migrate_seq);
  3596. unsigned t_seq, t_mseq;
  3597. int target, issued;
  3598. int mds = session->s_mds;
  3599. if (ph) {
  3600. t_cap_id = le64_to_cpu(ph->cap_id);
  3601. t_seq = le32_to_cpu(ph->seq);
  3602. t_mseq = le32_to_cpu(ph->mseq);
  3603. target = le32_to_cpu(ph->mds);
  3604. } else {
  3605. t_cap_id = t_seq = t_mseq = 0;
  3606. target = -1;
  3607. }
  3608. doutc(cl, "%p %llx.%llx ci %p mds%d mseq %d target %d\n",
  3609. inode, ceph_vinop(inode), ci, mds, mseq, target);
  3610. retry:
  3611. down_read(&mdsc->snap_rwsem);
  3612. spin_lock(&ci->i_ceph_lock);
  3613. cap = __get_cap_for_mds(ci, mds);
  3614. if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
  3615. goto out_unlock;
  3616. if (target < 0) {
  3617. ceph_remove_cap(mdsc, cap, false);
  3618. goto out_unlock;
  3619. }
  3620. /*
  3621. * now we know we haven't received the cap import message yet
  3622. * because the exported cap still exist.
  3623. */
  3624. issued = cap->issued;
  3625. if (issued != cap->implemented)
  3626. pr_err_ratelimited_client(cl, "issued != implemented: "
  3627. "%p %llx.%llx mds%d seq %d mseq %d"
  3628. " issued %s implemented %s\n",
  3629. inode, ceph_vinop(inode), mds,
  3630. cap->seq, cap->mseq,
  3631. ceph_cap_string(issued),
  3632. ceph_cap_string(cap->implemented));
  3633. tcap = __get_cap_for_mds(ci, target);
  3634. if (tcap) {
  3635. /* already have caps from the target */
  3636. if (tcap->cap_id == t_cap_id &&
  3637. ceph_seq_cmp(tcap->seq, t_seq) < 0) {
  3638. doutc(cl, " updating import cap %p mds%d\n", tcap,
  3639. target);
  3640. tcap->cap_id = t_cap_id;
  3641. tcap->seq = t_seq - 1;
  3642. tcap->issue_seq = t_seq - 1;
  3643. tcap->issued |= issued;
  3644. tcap->implemented |= issued;
  3645. if (cap == ci->i_auth_cap) {
  3646. ci->i_auth_cap = tcap;
  3647. change_auth_cap_ses(ci, tcap->session);
  3648. }
  3649. }
  3650. ceph_remove_cap(mdsc, cap, false);
  3651. goto out_unlock;
  3652. } else if (tsession) {
  3653. /* add placeholder for the export target */
  3654. int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
  3655. tcap = new_cap;
  3656. ceph_add_cap(inode, tsession, t_cap_id, issued, 0,
  3657. t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
  3658. if (!list_empty(&ci->i_cap_flush_list) &&
  3659. ci->i_auth_cap == tcap) {
  3660. spin_lock(&mdsc->cap_dirty_lock);
  3661. list_move_tail(&ci->i_flushing_item,
  3662. &tcap->session->s_cap_flushing);
  3663. spin_unlock(&mdsc->cap_dirty_lock);
  3664. }
  3665. ceph_remove_cap(mdsc, cap, false);
  3666. goto out_unlock;
  3667. }
  3668. spin_unlock(&ci->i_ceph_lock);
  3669. up_read(&mdsc->snap_rwsem);
  3670. mutex_unlock(&session->s_mutex);
  3671. /* open target session */
  3672. tsession = ceph_mdsc_open_export_target_session(mdsc, target);
  3673. if (!IS_ERR(tsession)) {
  3674. if (mds > target) {
  3675. mutex_lock(&session->s_mutex);
  3676. mutex_lock_nested(&tsession->s_mutex,
  3677. SINGLE_DEPTH_NESTING);
  3678. } else {
  3679. mutex_lock(&tsession->s_mutex);
  3680. mutex_lock_nested(&session->s_mutex,
  3681. SINGLE_DEPTH_NESTING);
  3682. }
  3683. new_cap = ceph_get_cap(mdsc, NULL);
  3684. } else {
  3685. WARN_ON(1);
  3686. tsession = NULL;
  3687. target = -1;
  3688. mutex_lock(&session->s_mutex);
  3689. }
  3690. goto retry;
  3691. out_unlock:
  3692. spin_unlock(&ci->i_ceph_lock);
  3693. up_read(&mdsc->snap_rwsem);
  3694. mutex_unlock(&session->s_mutex);
  3695. if (tsession) {
  3696. mutex_unlock(&tsession->s_mutex);
  3697. ceph_put_mds_session(tsession);
  3698. }
  3699. if (new_cap)
  3700. ceph_put_cap(mdsc, new_cap);
  3701. }
  3702. /*
  3703. * Handle cap IMPORT.
  3704. *
  3705. * caller holds s_mutex. acquires i_ceph_lock
  3706. */
  3707. static void handle_cap_import(struct ceph_mds_client *mdsc,
  3708. struct inode *inode, struct ceph_mds_caps *im,
  3709. struct ceph_mds_cap_peer *ph,
  3710. struct ceph_mds_session *session,
  3711. struct ceph_cap **target_cap, int *old_issued)
  3712. {
  3713. struct ceph_inode_info *ci = ceph_inode(inode);
  3714. struct ceph_client *cl = mdsc->fsc->client;
  3715. struct ceph_cap *cap, *ocap, *new_cap = NULL;
  3716. int mds = session->s_mds;
  3717. int issued;
  3718. unsigned caps = le32_to_cpu(im->caps);
  3719. unsigned wanted = le32_to_cpu(im->wanted);
  3720. unsigned seq = le32_to_cpu(im->seq);
  3721. unsigned mseq = le32_to_cpu(im->migrate_seq);
  3722. u64 realmino = le64_to_cpu(im->realm);
  3723. u64 cap_id = le64_to_cpu(im->cap_id);
  3724. u64 p_cap_id;
  3725. int peer;
  3726. if (ph) {
  3727. p_cap_id = le64_to_cpu(ph->cap_id);
  3728. peer = le32_to_cpu(ph->mds);
  3729. } else {
  3730. p_cap_id = 0;
  3731. peer = -1;
  3732. }
  3733. doutc(cl, "%p %llx.%llx ci %p mds%d mseq %d peer %d\n",
  3734. inode, ceph_vinop(inode), ci, mds, mseq, peer);
  3735. retry:
  3736. cap = __get_cap_for_mds(ci, mds);
  3737. if (!cap) {
  3738. if (!new_cap) {
  3739. spin_unlock(&ci->i_ceph_lock);
  3740. new_cap = ceph_get_cap(mdsc, NULL);
  3741. spin_lock(&ci->i_ceph_lock);
  3742. goto retry;
  3743. }
  3744. cap = new_cap;
  3745. } else {
  3746. if (new_cap) {
  3747. ceph_put_cap(mdsc, new_cap);
  3748. new_cap = NULL;
  3749. }
  3750. }
  3751. __ceph_caps_issued(ci, &issued);
  3752. issued |= __ceph_caps_dirty(ci);
  3753. ceph_add_cap(inode, session, cap_id, caps, wanted, seq, mseq,
  3754. realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
  3755. ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
  3756. if (ocap && ocap->cap_id == p_cap_id) {
  3757. doutc(cl, " remove export cap %p mds%d flags %d\n",
  3758. ocap, peer, ph->flags);
  3759. if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
  3760. (ocap->seq != le32_to_cpu(ph->seq) ||
  3761. ocap->mseq != le32_to_cpu(ph->mseq))) {
  3762. pr_err_ratelimited_client(cl, "mismatched seq/mseq: "
  3763. "%p %llx.%llx mds%d seq %d mseq %d"
  3764. " importer mds%d has peer seq %d mseq %d\n",
  3765. inode, ceph_vinop(inode), peer,
  3766. ocap->seq, ocap->mseq, mds,
  3767. le32_to_cpu(ph->seq),
  3768. le32_to_cpu(ph->mseq));
  3769. }
  3770. ceph_remove_cap(mdsc, ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
  3771. }
  3772. *old_issued = issued;
  3773. *target_cap = cap;
  3774. }
  3775. #ifdef CONFIG_FS_ENCRYPTION
  3776. static int parse_fscrypt_fields(void **p, void *end,
  3777. struct cap_extra_info *extra)
  3778. {
  3779. u32 len;
  3780. ceph_decode_32_safe(p, end, extra->fscrypt_auth_len, bad);
  3781. if (extra->fscrypt_auth_len) {
  3782. ceph_decode_need(p, end, extra->fscrypt_auth_len, bad);
  3783. extra->fscrypt_auth = kmalloc(extra->fscrypt_auth_len,
  3784. GFP_KERNEL);
  3785. if (!extra->fscrypt_auth)
  3786. return -ENOMEM;
  3787. ceph_decode_copy_safe(p, end, extra->fscrypt_auth,
  3788. extra->fscrypt_auth_len, bad);
  3789. }
  3790. ceph_decode_32_safe(p, end, len, bad);
  3791. if (len >= sizeof(u64)) {
  3792. ceph_decode_64_safe(p, end, extra->fscrypt_file_size, bad);
  3793. len -= sizeof(u64);
  3794. }
  3795. ceph_decode_skip_n(p, end, len, bad);
  3796. return 0;
  3797. bad:
  3798. return -EIO;
  3799. }
  3800. #else
  3801. static int parse_fscrypt_fields(void **p, void *end,
  3802. struct cap_extra_info *extra)
  3803. {
  3804. u32 len;
  3805. /* Don't care about these fields unless we're encryption-capable */
  3806. ceph_decode_32_safe(p, end, len, bad);
  3807. if (len)
  3808. ceph_decode_skip_n(p, end, len, bad);
  3809. ceph_decode_32_safe(p, end, len, bad);
  3810. if (len)
  3811. ceph_decode_skip_n(p, end, len, bad);
  3812. return 0;
  3813. bad:
  3814. return -EIO;
  3815. }
  3816. #endif
  3817. /*
  3818. * Handle a caps message from the MDS.
  3819. *
  3820. * Identify the appropriate session, inode, and call the right handler
  3821. * based on the cap op.
  3822. */
  3823. void ceph_handle_caps(struct ceph_mds_session *session,
  3824. struct ceph_msg *msg)
  3825. {
  3826. struct ceph_mds_client *mdsc = session->s_mdsc;
  3827. struct ceph_client *cl = mdsc->fsc->client;
  3828. struct inode *inode;
  3829. struct ceph_inode_info *ci;
  3830. struct ceph_cap *cap;
  3831. struct ceph_mds_caps *h;
  3832. struct ceph_mds_cap_peer *peer = NULL;
  3833. struct ceph_snap_realm *realm = NULL;
  3834. int op;
  3835. int msg_version = le16_to_cpu(msg->hdr.version);
  3836. u32 seq, mseq;
  3837. struct ceph_vino vino;
  3838. void *snaptrace;
  3839. size_t snaptrace_len;
  3840. void *p, *end;
  3841. struct cap_extra_info extra_info = {};
  3842. bool queue_trunc;
  3843. bool close_sessions = false;
  3844. bool do_cap_release = false;
  3845. doutc(cl, "from mds%d\n", session->s_mds);
  3846. if (!ceph_inc_mds_stopping_blocker(mdsc, session))
  3847. return;
  3848. /* decode */
  3849. end = msg->front.iov_base + msg->front.iov_len;
  3850. if (msg->front.iov_len < sizeof(*h))
  3851. goto bad;
  3852. h = msg->front.iov_base;
  3853. op = le32_to_cpu(h->op);
  3854. vino.ino = le64_to_cpu(h->ino);
  3855. vino.snap = CEPH_NOSNAP;
  3856. seq = le32_to_cpu(h->seq);
  3857. mseq = le32_to_cpu(h->migrate_seq);
  3858. snaptrace = h + 1;
  3859. snaptrace_len = le32_to_cpu(h->snap_trace_len);
  3860. p = snaptrace + snaptrace_len;
  3861. if (msg_version >= 2) {
  3862. u32 flock_len;
  3863. ceph_decode_32_safe(&p, end, flock_len, bad);
  3864. if (p + flock_len > end)
  3865. goto bad;
  3866. p += flock_len;
  3867. }
  3868. if (msg_version >= 3) {
  3869. if (op == CEPH_CAP_OP_IMPORT) {
  3870. if (p + sizeof(*peer) > end)
  3871. goto bad;
  3872. peer = p;
  3873. p += sizeof(*peer);
  3874. } else if (op == CEPH_CAP_OP_EXPORT) {
  3875. /* recorded in unused fields */
  3876. peer = (void *)&h->size;
  3877. }
  3878. }
  3879. if (msg_version >= 4) {
  3880. ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
  3881. ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
  3882. if (p + extra_info.inline_len > end)
  3883. goto bad;
  3884. extra_info.inline_data = p;
  3885. p += extra_info.inline_len;
  3886. }
  3887. if (msg_version >= 5) {
  3888. struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
  3889. u32 epoch_barrier;
  3890. ceph_decode_32_safe(&p, end, epoch_barrier, bad);
  3891. ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
  3892. }
  3893. if (msg_version >= 8) {
  3894. u32 pool_ns_len;
  3895. /* version >= 6 */
  3896. ceph_decode_skip_64(&p, end, bad); // flush_tid
  3897. /* version >= 7 */
  3898. ceph_decode_skip_32(&p, end, bad); // caller_uid
  3899. ceph_decode_skip_32(&p, end, bad); // caller_gid
  3900. /* version >= 8 */
  3901. ceph_decode_32_safe(&p, end, pool_ns_len, bad);
  3902. if (pool_ns_len > 0) {
  3903. ceph_decode_need(&p, end, pool_ns_len, bad);
  3904. extra_info.pool_ns =
  3905. ceph_find_or_create_string(p, pool_ns_len);
  3906. p += pool_ns_len;
  3907. }
  3908. }
  3909. if (msg_version >= 9) {
  3910. struct ceph_timespec *btime;
  3911. if (p + sizeof(*btime) > end)
  3912. goto bad;
  3913. btime = p;
  3914. ceph_decode_timespec64(&extra_info.btime, btime);
  3915. p += sizeof(*btime);
  3916. ceph_decode_64_safe(&p, end, extra_info.change_attr, bad);
  3917. }
  3918. if (msg_version >= 11) {
  3919. /* version >= 10 */
  3920. ceph_decode_skip_32(&p, end, bad); // flags
  3921. /* version >= 11 */
  3922. extra_info.dirstat_valid = true;
  3923. ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
  3924. ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
  3925. }
  3926. if (msg_version >= 12) {
  3927. if (parse_fscrypt_fields(&p, end, &extra_info))
  3928. goto bad;
  3929. }
  3930. /* lookup ino */
  3931. inode = ceph_find_inode(mdsc->fsc->sb, vino);
  3932. doutc(cl, " op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op),
  3933. vino.ino, vino.snap, inode);
  3934. mutex_lock(&session->s_mutex);
  3935. doutc(cl, " mds%d seq %lld cap seq %u\n", session->s_mds,
  3936. session->s_seq, (unsigned)seq);
  3937. if (!inode) {
  3938. doutc(cl, " i don't have ino %llx\n", vino.ino);
  3939. switch (op) {
  3940. case CEPH_CAP_OP_IMPORT:
  3941. case CEPH_CAP_OP_REVOKE:
  3942. case CEPH_CAP_OP_GRANT:
  3943. do_cap_release = true;
  3944. break;
  3945. default:
  3946. break;
  3947. }
  3948. goto flush_cap_releases;
  3949. }
  3950. ci = ceph_inode(inode);
  3951. /* these will work even if we don't have a cap yet */
  3952. switch (op) {
  3953. case CEPH_CAP_OP_FLUSHSNAP_ACK:
  3954. handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
  3955. h, session);
  3956. goto done;
  3957. case CEPH_CAP_OP_EXPORT:
  3958. handle_cap_export(inode, h, peer, session);
  3959. goto done_unlocked;
  3960. case CEPH_CAP_OP_IMPORT:
  3961. realm = NULL;
  3962. if (snaptrace_len) {
  3963. down_write(&mdsc->snap_rwsem);
  3964. if (ceph_update_snap_trace(mdsc, snaptrace,
  3965. snaptrace + snaptrace_len,
  3966. false, &realm)) {
  3967. up_write(&mdsc->snap_rwsem);
  3968. close_sessions = true;
  3969. goto done;
  3970. }
  3971. downgrade_write(&mdsc->snap_rwsem);
  3972. } else {
  3973. down_read(&mdsc->snap_rwsem);
  3974. }
  3975. spin_lock(&ci->i_ceph_lock);
  3976. handle_cap_import(mdsc, inode, h, peer, session,
  3977. &cap, &extra_info.issued);
  3978. handle_cap_grant(inode, session, cap,
  3979. h, msg->middle, &extra_info);
  3980. if (realm)
  3981. ceph_put_snap_realm(mdsc, realm);
  3982. goto done_unlocked;
  3983. }
  3984. /* the rest require a cap */
  3985. spin_lock(&ci->i_ceph_lock);
  3986. cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
  3987. if (!cap) {
  3988. doutc(cl, " no cap on %p ino %llx.%llx from mds%d\n",
  3989. inode, ceph_ino(inode), ceph_snap(inode),
  3990. session->s_mds);
  3991. spin_unlock(&ci->i_ceph_lock);
  3992. switch (op) {
  3993. case CEPH_CAP_OP_REVOKE:
  3994. case CEPH_CAP_OP_GRANT:
  3995. do_cap_release = true;
  3996. break;
  3997. default:
  3998. break;
  3999. }
  4000. goto flush_cap_releases;
  4001. }
  4002. /* note that each of these drops i_ceph_lock for us */
  4003. switch (op) {
  4004. case CEPH_CAP_OP_REVOKE:
  4005. case CEPH_CAP_OP_GRANT:
  4006. __ceph_caps_issued(ci, &extra_info.issued);
  4007. extra_info.issued |= __ceph_caps_dirty(ci);
  4008. handle_cap_grant(inode, session, cap,
  4009. h, msg->middle, &extra_info);
  4010. goto done_unlocked;
  4011. case CEPH_CAP_OP_FLUSH_ACK:
  4012. handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
  4013. h, session, cap);
  4014. break;
  4015. case CEPH_CAP_OP_TRUNC:
  4016. queue_trunc = handle_cap_trunc(inode, h, session,
  4017. &extra_info);
  4018. spin_unlock(&ci->i_ceph_lock);
  4019. if (queue_trunc)
  4020. ceph_queue_vmtruncate(inode);
  4021. break;
  4022. default:
  4023. spin_unlock(&ci->i_ceph_lock);
  4024. pr_err_client(cl, "unknown cap op %d %s\n", op,
  4025. ceph_cap_op_name(op));
  4026. }
  4027. done:
  4028. mutex_unlock(&session->s_mutex);
  4029. done_unlocked:
  4030. iput(inode);
  4031. out:
  4032. ceph_dec_mds_stopping_blocker(mdsc);
  4033. ceph_put_string(extra_info.pool_ns);
  4034. /* Defer closing the sessions after s_mutex lock being released */
  4035. if (close_sessions)
  4036. ceph_mdsc_close_sessions(mdsc);
  4037. kfree(extra_info.fscrypt_auth);
  4038. return;
  4039. flush_cap_releases:
  4040. /*
  4041. * send any cap release message to try to move things
  4042. * along for the mds (who clearly thinks we still have this
  4043. * cap).
  4044. */
  4045. if (do_cap_release) {
  4046. cap = ceph_get_cap(mdsc, NULL);
  4047. cap->cap_ino = vino.ino;
  4048. cap->queue_release = 1;
  4049. cap->cap_id = le64_to_cpu(h->cap_id);
  4050. cap->mseq = mseq;
  4051. cap->seq = seq;
  4052. cap->issue_seq = seq;
  4053. spin_lock(&session->s_cap_lock);
  4054. __ceph_queue_cap_release(session, cap);
  4055. spin_unlock(&session->s_cap_lock);
  4056. }
  4057. ceph_flush_session_cap_releases(mdsc, session);
  4058. goto done;
  4059. bad:
  4060. pr_err_client(cl, "corrupt message\n");
  4061. ceph_msg_dump(msg);
  4062. goto out;
  4063. }
  4064. /*
  4065. * Delayed work handler to process end of delayed cap release LRU list.
  4066. *
  4067. * If new caps are added to the list while processing it, these won't get
  4068. * processed in this run. In this case, the ci->i_hold_caps_max will be
  4069. * returned so that the work can be scheduled accordingly.
  4070. */
  4071. unsigned long ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
  4072. {
  4073. struct ceph_client *cl = mdsc->fsc->client;
  4074. struct inode *inode;
  4075. struct ceph_inode_info *ci;
  4076. struct ceph_mount_options *opt = mdsc->fsc->mount_options;
  4077. unsigned long delay_max = opt->caps_wanted_delay_max * HZ;
  4078. unsigned long loop_start = jiffies;
  4079. unsigned long delay = 0;
  4080. doutc(cl, "begin\n");
  4081. spin_lock(&mdsc->cap_delay_lock);
  4082. while (!list_empty(&mdsc->cap_delay_list)) {
  4083. ci = list_first_entry(&mdsc->cap_delay_list,
  4084. struct ceph_inode_info,
  4085. i_cap_delay_list);
  4086. if (time_before(loop_start, ci->i_hold_caps_max - delay_max)) {
  4087. doutc(cl, "caps added recently. Exiting loop");
  4088. delay = ci->i_hold_caps_max;
  4089. break;
  4090. }
  4091. if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
  4092. time_before(jiffies, ci->i_hold_caps_max))
  4093. break;
  4094. list_del_init(&ci->i_cap_delay_list);
  4095. inode = igrab(&ci->netfs.inode);
  4096. if (inode) {
  4097. spin_unlock(&mdsc->cap_delay_lock);
  4098. doutc(cl, "on %p %llx.%llx\n", inode,
  4099. ceph_vinop(inode));
  4100. ceph_check_caps(ci, 0);
  4101. iput(inode);
  4102. spin_lock(&mdsc->cap_delay_lock);
  4103. }
  4104. /*
  4105. * Make sure too many dirty caps or general
  4106. * slowness doesn't block mdsc delayed work,
  4107. * preventing send_renew_caps() from running.
  4108. */
  4109. if (time_after_eq(jiffies, loop_start + 5 * HZ))
  4110. break;
  4111. }
  4112. spin_unlock(&mdsc->cap_delay_lock);
  4113. doutc(cl, "done\n");
  4114. return delay;
  4115. }
  4116. /*
  4117. * Flush all dirty caps to the mds
  4118. */
  4119. static void flush_dirty_session_caps(struct ceph_mds_session *s)
  4120. {
  4121. struct ceph_mds_client *mdsc = s->s_mdsc;
  4122. struct ceph_client *cl = mdsc->fsc->client;
  4123. struct ceph_inode_info *ci;
  4124. struct inode *inode;
  4125. doutc(cl, "begin\n");
  4126. spin_lock(&mdsc->cap_dirty_lock);
  4127. while (!list_empty(&s->s_cap_dirty)) {
  4128. ci = list_first_entry(&s->s_cap_dirty, struct ceph_inode_info,
  4129. i_dirty_item);
  4130. inode = &ci->netfs.inode;
  4131. ihold(inode);
  4132. doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
  4133. spin_unlock(&mdsc->cap_dirty_lock);
  4134. ceph_wait_on_async_create(inode);
  4135. ceph_check_caps(ci, CHECK_CAPS_FLUSH);
  4136. iput(inode);
  4137. spin_lock(&mdsc->cap_dirty_lock);
  4138. }
  4139. spin_unlock(&mdsc->cap_dirty_lock);
  4140. doutc(cl, "done\n");
  4141. }
  4142. void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
  4143. {
  4144. ceph_mdsc_iterate_sessions(mdsc, flush_dirty_session_caps, true);
  4145. }
  4146. /*
  4147. * Flush all cap releases to the mds
  4148. */
  4149. static void flush_cap_releases(struct ceph_mds_session *s)
  4150. {
  4151. struct ceph_mds_client *mdsc = s->s_mdsc;
  4152. struct ceph_client *cl = mdsc->fsc->client;
  4153. doutc(cl, "begin\n");
  4154. spin_lock(&s->s_cap_lock);
  4155. if (s->s_num_cap_releases)
  4156. ceph_flush_session_cap_releases(mdsc, s);
  4157. spin_unlock(&s->s_cap_lock);
  4158. doutc(cl, "done\n");
  4159. }
  4160. void ceph_flush_cap_releases(struct ceph_mds_client *mdsc)
  4161. {
  4162. ceph_mdsc_iterate_sessions(mdsc, flush_cap_releases, true);
  4163. }
  4164. void __ceph_touch_fmode(struct ceph_inode_info *ci,
  4165. struct ceph_mds_client *mdsc, int fmode)
  4166. {
  4167. unsigned long now = jiffies;
  4168. if (fmode & CEPH_FILE_MODE_RD)
  4169. ci->i_last_rd = now;
  4170. if (fmode & CEPH_FILE_MODE_WR)
  4171. ci->i_last_wr = now;
  4172. /* queue periodic check */
  4173. if (fmode &&
  4174. __ceph_is_any_real_caps(ci) &&
  4175. list_empty(&ci->i_cap_delay_list))
  4176. __cap_delay_requeue(mdsc, ci);
  4177. }
  4178. void ceph_get_fmode(struct ceph_inode_info *ci, int fmode, int count)
  4179. {
  4180. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb);
  4181. int bits = (fmode << 1) | 1;
  4182. bool already_opened = false;
  4183. int i;
  4184. if (count == 1)
  4185. atomic64_inc(&mdsc->metric.opened_files);
  4186. spin_lock(&ci->i_ceph_lock);
  4187. for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
  4188. /*
  4189. * If any of the mode ref is larger than 0,
  4190. * that means it has been already opened by
  4191. * others. Just skip checking the PIN ref.
  4192. */
  4193. if (i && ci->i_nr_by_mode[i])
  4194. already_opened = true;
  4195. if (bits & (1 << i))
  4196. ci->i_nr_by_mode[i] += count;
  4197. }
  4198. if (!already_opened)
  4199. percpu_counter_inc(&mdsc->metric.opened_inodes);
  4200. spin_unlock(&ci->i_ceph_lock);
  4201. }
  4202. /*
  4203. * Drop open file reference. If we were the last open file,
  4204. * we may need to release capabilities to the MDS (or schedule
  4205. * their delayed release).
  4206. */
  4207. void ceph_put_fmode(struct ceph_inode_info *ci, int fmode, int count)
  4208. {
  4209. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb);
  4210. int bits = (fmode << 1) | 1;
  4211. bool is_closed = true;
  4212. int i;
  4213. if (count == 1)
  4214. atomic64_dec(&mdsc->metric.opened_files);
  4215. spin_lock(&ci->i_ceph_lock);
  4216. for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
  4217. if (bits & (1 << i)) {
  4218. BUG_ON(ci->i_nr_by_mode[i] < count);
  4219. ci->i_nr_by_mode[i] -= count;
  4220. }
  4221. /*
  4222. * If any of the mode ref is not 0 after
  4223. * decreased, that means it is still opened
  4224. * by others. Just skip checking the PIN ref.
  4225. */
  4226. if (i && ci->i_nr_by_mode[i])
  4227. is_closed = false;
  4228. }
  4229. if (is_closed)
  4230. percpu_counter_dec(&mdsc->metric.opened_inodes);
  4231. spin_unlock(&ci->i_ceph_lock);
  4232. }
  4233. /*
  4234. * For a soon-to-be unlinked file, drop the LINK caps. If it
  4235. * looks like the link count will hit 0, drop any other caps (other
  4236. * than PIN) we don't specifically want (due to the file still being
  4237. * open).
  4238. */
  4239. int ceph_drop_caps_for_unlink(struct inode *inode)
  4240. {
  4241. struct ceph_inode_info *ci = ceph_inode(inode);
  4242. int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
  4243. spin_lock(&ci->i_ceph_lock);
  4244. if (inode->i_nlink == 1) {
  4245. drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
  4246. if (__ceph_caps_dirty(ci)) {
  4247. struct ceph_mds_client *mdsc =
  4248. ceph_inode_to_fs_client(inode)->mdsc;
  4249. doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode,
  4250. ceph_vinop(inode));
  4251. spin_lock(&mdsc->cap_delay_lock);
  4252. ci->i_ceph_flags |= CEPH_I_FLUSH;
  4253. if (!list_empty(&ci->i_cap_delay_list))
  4254. list_del_init(&ci->i_cap_delay_list);
  4255. list_add_tail(&ci->i_cap_delay_list,
  4256. &mdsc->cap_unlink_delay_list);
  4257. spin_unlock(&mdsc->cap_delay_lock);
  4258. /*
  4259. * Fire the work immediately, because the MDS maybe
  4260. * waiting for caps release.
  4261. */
  4262. ceph_queue_cap_unlink_work(mdsc);
  4263. }
  4264. }
  4265. spin_unlock(&ci->i_ceph_lock);
  4266. return drop;
  4267. }
  4268. /*
  4269. * Helpers for embedding cap and dentry lease releases into mds
  4270. * requests.
  4271. *
  4272. * @force is used by dentry_release (below) to force inclusion of a
  4273. * record for the directory inode, even when there aren't any caps to
  4274. * drop.
  4275. */
  4276. int ceph_encode_inode_release(void **p, struct inode *inode,
  4277. int mds, int drop, int unless, int force)
  4278. {
  4279. struct ceph_inode_info *ci = ceph_inode(inode);
  4280. struct ceph_client *cl = ceph_inode_to_client(inode);
  4281. struct ceph_cap *cap;
  4282. struct ceph_mds_request_release *rel = *p;
  4283. int used, dirty;
  4284. int ret = 0;
  4285. spin_lock(&ci->i_ceph_lock);
  4286. used = __ceph_caps_used(ci);
  4287. dirty = __ceph_caps_dirty(ci);
  4288. doutc(cl, "%p %llx.%llx mds%d used|dirty %s drop %s unless %s\n",
  4289. inode, ceph_vinop(inode), mds, ceph_cap_string(used|dirty),
  4290. ceph_cap_string(drop), ceph_cap_string(unless));
  4291. /* only drop unused, clean caps */
  4292. drop &= ~(used | dirty);
  4293. cap = __get_cap_for_mds(ci, mds);
  4294. if (cap && __cap_is_valid(cap)) {
  4295. unless &= cap->issued;
  4296. if (unless) {
  4297. if (unless & CEPH_CAP_AUTH_EXCL)
  4298. drop &= ~CEPH_CAP_AUTH_SHARED;
  4299. if (unless & CEPH_CAP_LINK_EXCL)
  4300. drop &= ~CEPH_CAP_LINK_SHARED;
  4301. if (unless & CEPH_CAP_XATTR_EXCL)
  4302. drop &= ~CEPH_CAP_XATTR_SHARED;
  4303. if (unless & CEPH_CAP_FILE_EXCL)
  4304. drop &= ~CEPH_CAP_FILE_SHARED;
  4305. }
  4306. if (force || (cap->issued & drop)) {
  4307. if (cap->issued & drop) {
  4308. int wanted = __ceph_caps_wanted(ci);
  4309. doutc(cl, "%p %llx.%llx cap %p %s -> %s, "
  4310. "wanted %s -> %s\n", inode,
  4311. ceph_vinop(inode), cap,
  4312. ceph_cap_string(cap->issued),
  4313. ceph_cap_string(cap->issued & ~drop),
  4314. ceph_cap_string(cap->mds_wanted),
  4315. ceph_cap_string(wanted));
  4316. cap->issued &= ~drop;
  4317. cap->implemented &= ~drop;
  4318. cap->mds_wanted = wanted;
  4319. if (cap == ci->i_auth_cap &&
  4320. !(wanted & CEPH_CAP_ANY_FILE_WR))
  4321. ci->i_requested_max_size = 0;
  4322. } else {
  4323. doutc(cl, "%p %llx.%llx cap %p %s (force)\n",
  4324. inode, ceph_vinop(inode), cap,
  4325. ceph_cap_string(cap->issued));
  4326. }
  4327. rel->ino = cpu_to_le64(ceph_ino(inode));
  4328. rel->cap_id = cpu_to_le64(cap->cap_id);
  4329. rel->seq = cpu_to_le32(cap->seq);
  4330. rel->issue_seq = cpu_to_le32(cap->issue_seq);
  4331. rel->mseq = cpu_to_le32(cap->mseq);
  4332. rel->caps = cpu_to_le32(cap->implemented);
  4333. rel->wanted = cpu_to_le32(cap->mds_wanted);
  4334. rel->dname_len = 0;
  4335. rel->dname_seq = 0;
  4336. *p += sizeof(*rel);
  4337. ret = 1;
  4338. } else {
  4339. doutc(cl, "%p %llx.%llx cap %p %s (noop)\n",
  4340. inode, ceph_vinop(inode), cap,
  4341. ceph_cap_string(cap->issued));
  4342. }
  4343. }
  4344. spin_unlock(&ci->i_ceph_lock);
  4345. return ret;
  4346. }
  4347. /**
  4348. * ceph_encode_dentry_release - encode a dentry release into an outgoing request
  4349. * @p: outgoing request buffer
  4350. * @dentry: dentry to release
  4351. * @dir: dir to release it from
  4352. * @mds: mds that we're speaking to
  4353. * @drop: caps being dropped
  4354. * @unless: unless we have these caps
  4355. *
  4356. * Encode a dentry release into an outgoing request buffer. Returns 1 if the
  4357. * thing was released, or a negative error code otherwise.
  4358. */
  4359. int ceph_encode_dentry_release(void **p, struct dentry *dentry,
  4360. struct inode *dir,
  4361. int mds, int drop, int unless)
  4362. {
  4363. struct ceph_mds_request_release *rel = *p;
  4364. struct ceph_dentry_info *di = ceph_dentry(dentry);
  4365. struct ceph_client *cl;
  4366. int force = 0;
  4367. int ret;
  4368. /* This shouldn't happen */
  4369. BUG_ON(!dir);
  4370. /*
  4371. * force an record for the directory caps if we have a dentry lease.
  4372. * this is racy (can't take i_ceph_lock and d_lock together), but it
  4373. * doesn't have to be perfect; the mds will revoke anything we don't
  4374. * release.
  4375. */
  4376. spin_lock(&dentry->d_lock);
  4377. if (di->lease_session && di->lease_session->s_mds == mds)
  4378. force = 1;
  4379. spin_unlock(&dentry->d_lock);
  4380. ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
  4381. cl = ceph_inode_to_client(dir);
  4382. spin_lock(&dentry->d_lock);
  4383. if (ret && di->lease_session && di->lease_session->s_mds == mds) {
  4384. doutc(cl, "%p mds%d seq %d\n", dentry, mds,
  4385. (int)di->lease_seq);
  4386. rel->dname_seq = cpu_to_le32(di->lease_seq);
  4387. __ceph_mdsc_drop_dentry_lease(dentry);
  4388. spin_unlock(&dentry->d_lock);
  4389. if (IS_ENCRYPTED(dir) && fscrypt_has_encryption_key(dir)) {
  4390. int ret2 = ceph_encode_encrypted_fname(dir, dentry, *p);
  4391. if (ret2 < 0)
  4392. return ret2;
  4393. rel->dname_len = cpu_to_le32(ret2);
  4394. *p += ret2;
  4395. } else {
  4396. rel->dname_len = cpu_to_le32(dentry->d_name.len);
  4397. memcpy(*p, dentry->d_name.name, dentry->d_name.len);
  4398. *p += dentry->d_name.len;
  4399. }
  4400. } else {
  4401. spin_unlock(&dentry->d_lock);
  4402. }
  4403. return ret;
  4404. }
  4405. static int remove_capsnaps(struct ceph_mds_client *mdsc, struct inode *inode)
  4406. {
  4407. struct ceph_inode_info *ci = ceph_inode(inode);
  4408. struct ceph_client *cl = mdsc->fsc->client;
  4409. struct ceph_cap_snap *capsnap;
  4410. int capsnap_release = 0;
  4411. lockdep_assert_held(&ci->i_ceph_lock);
  4412. doutc(cl, "removing capsnaps, ci is %p, %p %llx.%llx\n",
  4413. ci, inode, ceph_vinop(inode));
  4414. while (!list_empty(&ci->i_cap_snaps)) {
  4415. capsnap = list_first_entry(&ci->i_cap_snaps,
  4416. struct ceph_cap_snap, ci_item);
  4417. __ceph_remove_capsnap(inode, capsnap, NULL, NULL);
  4418. ceph_put_snap_context(capsnap->context);
  4419. ceph_put_cap_snap(capsnap);
  4420. capsnap_release++;
  4421. }
  4422. wake_up_all(&ci->i_cap_wq);
  4423. wake_up_all(&mdsc->cap_flushing_wq);
  4424. return capsnap_release;
  4425. }
  4426. int ceph_purge_inode_cap(struct inode *inode, struct ceph_cap *cap, bool *invalidate)
  4427. {
  4428. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  4429. struct ceph_mds_client *mdsc = fsc->mdsc;
  4430. struct ceph_client *cl = fsc->client;
  4431. struct ceph_inode_info *ci = ceph_inode(inode);
  4432. bool is_auth;
  4433. bool dirty_dropped = false;
  4434. int iputs = 0;
  4435. lockdep_assert_held(&ci->i_ceph_lock);
  4436. doutc(cl, "removing cap %p, ci is %p, %p %llx.%llx\n",
  4437. cap, ci, inode, ceph_vinop(inode));
  4438. is_auth = (cap == ci->i_auth_cap);
  4439. __ceph_remove_cap(cap, false);
  4440. if (is_auth) {
  4441. struct ceph_cap_flush *cf;
  4442. if (ceph_inode_is_shutdown(inode)) {
  4443. if (inode->i_data.nrpages > 0)
  4444. *invalidate = true;
  4445. if (ci->i_wrbuffer_ref > 0)
  4446. mapping_set_error(&inode->i_data, -EIO);
  4447. }
  4448. spin_lock(&mdsc->cap_dirty_lock);
  4449. /* trash all of the cap flushes for this inode */
  4450. while (!list_empty(&ci->i_cap_flush_list)) {
  4451. cf = list_first_entry(&ci->i_cap_flush_list,
  4452. struct ceph_cap_flush, i_list);
  4453. list_del_init(&cf->g_list);
  4454. list_del_init(&cf->i_list);
  4455. if (!cf->is_capsnap)
  4456. ceph_free_cap_flush(cf);
  4457. }
  4458. if (!list_empty(&ci->i_dirty_item)) {
  4459. pr_warn_ratelimited_client(cl,
  4460. " dropping dirty %s state for %p %llx.%llx\n",
  4461. ceph_cap_string(ci->i_dirty_caps),
  4462. inode, ceph_vinop(inode));
  4463. ci->i_dirty_caps = 0;
  4464. list_del_init(&ci->i_dirty_item);
  4465. dirty_dropped = true;
  4466. }
  4467. if (!list_empty(&ci->i_flushing_item)) {
  4468. pr_warn_ratelimited_client(cl,
  4469. " dropping dirty+flushing %s state for %p %llx.%llx\n",
  4470. ceph_cap_string(ci->i_flushing_caps),
  4471. inode, ceph_vinop(inode));
  4472. ci->i_flushing_caps = 0;
  4473. list_del_init(&ci->i_flushing_item);
  4474. mdsc->num_cap_flushing--;
  4475. dirty_dropped = true;
  4476. }
  4477. spin_unlock(&mdsc->cap_dirty_lock);
  4478. if (dirty_dropped) {
  4479. mapping_set_error(inode->i_mapping, -EIO);
  4480. if (ci->i_wrbuffer_ref_head == 0 &&
  4481. ci->i_wr_ref == 0 &&
  4482. ci->i_dirty_caps == 0 &&
  4483. ci->i_flushing_caps == 0) {
  4484. ceph_put_snap_context(ci->i_head_snapc);
  4485. ci->i_head_snapc = NULL;
  4486. }
  4487. }
  4488. if (atomic_read(&ci->i_filelock_ref) > 0) {
  4489. /* make further file lock syscall return -EIO */
  4490. ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
  4491. pr_warn_ratelimited_client(cl,
  4492. " dropping file locks for %p %llx.%llx\n",
  4493. inode, ceph_vinop(inode));
  4494. }
  4495. if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
  4496. cf = ci->i_prealloc_cap_flush;
  4497. ci->i_prealloc_cap_flush = NULL;
  4498. if (!cf->is_capsnap)
  4499. ceph_free_cap_flush(cf);
  4500. }
  4501. if (!list_empty(&ci->i_cap_snaps))
  4502. iputs = remove_capsnaps(mdsc, inode);
  4503. }
  4504. if (dirty_dropped)
  4505. ++iputs;
  4506. return iputs;
  4507. }