caps.c 139 KB

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