caps.c 113 KB

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