caps.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214
  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. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  932. int removed = 0;
  933. dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
  934. /* remove from inode's cap rbtree, and clear auth cap */
  935. rb_erase(&cap->ci_node, &ci->i_caps);
  936. if (ci->i_auth_cap == cap)
  937. ci->i_auth_cap = NULL;
  938. /* remove from session list */
  939. spin_lock(&session->s_cap_lock);
  940. if (session->s_cap_iterator == cap) {
  941. /* not yet, we are iterating over this very cap */
  942. dout("__ceph_remove_cap delaying %p removal from session %p\n",
  943. cap, cap->session);
  944. } else {
  945. list_del_init(&cap->session_caps);
  946. session->s_nr_caps--;
  947. cap->session = NULL;
  948. removed = 1;
  949. }
  950. /* protect backpointer with s_cap_lock: see iterate_session_caps */
  951. cap->ci = NULL;
  952. /*
  953. * s_cap_reconnect is protected by s_cap_lock. no one changes
  954. * s_cap_gen while session is in the reconnect state.
  955. */
  956. if (queue_release &&
  957. (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
  958. cap->queue_release = 1;
  959. if (removed) {
  960. list_add_tail(&cap->session_caps,
  961. &session->s_cap_releases);
  962. session->s_num_cap_releases++;
  963. removed = 0;
  964. }
  965. } else {
  966. cap->queue_release = 0;
  967. }
  968. cap->cap_ino = ci->i_vino.ino;
  969. spin_unlock(&session->s_cap_lock);
  970. if (removed)
  971. ceph_put_cap(mdsc, cap);
  972. /* when reconnect denied, we remove session caps forcibly,
  973. * i_wr_ref can be non-zero. If there are ongoing write,
  974. * keep i_snap_realm.
  975. */
  976. if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
  977. drop_inode_snap_realm(ci);
  978. if (!__ceph_is_any_real_caps(ci))
  979. __cap_delay_cancel(mdsc, ci);
  980. }
  981. struct cap_msg_args {
  982. struct ceph_mds_session *session;
  983. u64 ino, cid, follows;
  984. u64 flush_tid, oldest_flush_tid, size, max_size;
  985. u64 xattr_version;
  986. struct ceph_buffer *xattr_buf;
  987. struct timespec64 atime, mtime, ctime;
  988. int op, caps, wanted, dirty;
  989. u32 seq, issue_seq, mseq, time_warp_seq;
  990. u32 flags;
  991. kuid_t uid;
  992. kgid_t gid;
  993. umode_t mode;
  994. bool inline_data;
  995. };
  996. /*
  997. * Build and send a cap message to the given MDS.
  998. *
  999. * Caller should be holding s_mutex.
  1000. */
  1001. static int send_cap_msg(struct cap_msg_args *arg)
  1002. {
  1003. struct ceph_mds_caps *fc;
  1004. struct ceph_msg *msg;
  1005. void *p;
  1006. size_t extra_len;
  1007. struct timespec64 zerotime = {0};
  1008. struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
  1009. dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
  1010. " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
  1011. " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
  1012. arg->cid, arg->ino, ceph_cap_string(arg->caps),
  1013. ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
  1014. arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
  1015. arg->mseq, arg->follows, arg->size, arg->max_size,
  1016. arg->xattr_version,
  1017. arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
  1018. /* flock buffer size + inline version + inline data size +
  1019. * osd_epoch_barrier + oldest_flush_tid */
  1020. extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
  1021. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
  1022. GFP_NOFS, false);
  1023. if (!msg)
  1024. return -ENOMEM;
  1025. msg->hdr.version = cpu_to_le16(10);
  1026. msg->hdr.tid = cpu_to_le64(arg->flush_tid);
  1027. fc = msg->front.iov_base;
  1028. memset(fc, 0, sizeof(*fc));
  1029. fc->cap_id = cpu_to_le64(arg->cid);
  1030. fc->op = cpu_to_le32(arg->op);
  1031. fc->seq = cpu_to_le32(arg->seq);
  1032. fc->issue_seq = cpu_to_le32(arg->issue_seq);
  1033. fc->migrate_seq = cpu_to_le32(arg->mseq);
  1034. fc->caps = cpu_to_le32(arg->caps);
  1035. fc->wanted = cpu_to_le32(arg->wanted);
  1036. fc->dirty = cpu_to_le32(arg->dirty);
  1037. fc->ino = cpu_to_le64(arg->ino);
  1038. fc->snap_follows = cpu_to_le64(arg->follows);
  1039. fc->size = cpu_to_le64(arg->size);
  1040. fc->max_size = cpu_to_le64(arg->max_size);
  1041. ceph_encode_timespec64(&fc->mtime, &arg->mtime);
  1042. ceph_encode_timespec64(&fc->atime, &arg->atime);
  1043. ceph_encode_timespec64(&fc->ctime, &arg->ctime);
  1044. fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
  1045. fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
  1046. fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
  1047. fc->mode = cpu_to_le32(arg->mode);
  1048. fc->xattr_version = cpu_to_le64(arg->xattr_version);
  1049. if (arg->xattr_buf) {
  1050. msg->middle = ceph_buffer_get(arg->xattr_buf);
  1051. fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
  1052. msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
  1053. }
  1054. p = fc + 1;
  1055. /* flock buffer size (version 2) */
  1056. ceph_encode_32(&p, 0);
  1057. /* inline version (version 4) */
  1058. ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
  1059. /* inline data size */
  1060. ceph_encode_32(&p, 0);
  1061. /*
  1062. * osd_epoch_barrier (version 5)
  1063. * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
  1064. * case it was recently changed
  1065. */
  1066. ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
  1067. /* oldest_flush_tid (version 6) */
  1068. ceph_encode_64(&p, arg->oldest_flush_tid);
  1069. /*
  1070. * caller_uid/caller_gid (version 7)
  1071. *
  1072. * Currently, we don't properly track which caller dirtied the caps
  1073. * last, and force a flush of them when there is a conflict. For now,
  1074. * just set this to 0:0, to emulate how the MDS has worked up to now.
  1075. */
  1076. ceph_encode_32(&p, 0);
  1077. ceph_encode_32(&p, 0);
  1078. /* pool namespace (version 8) (mds always ignores this) */
  1079. ceph_encode_32(&p, 0);
  1080. /*
  1081. * btime and change_attr (version 9)
  1082. *
  1083. * We just zero these out for now, as the MDS ignores them unless
  1084. * the requisite feature flags are set (which we don't do yet).
  1085. */
  1086. ceph_encode_timespec64(p, &zerotime);
  1087. p += sizeof(struct ceph_timespec);
  1088. ceph_encode_64(&p, 0);
  1089. /* Advisory flags (version 10) */
  1090. ceph_encode_32(&p, arg->flags);
  1091. ceph_con_send(&arg->session->s_con, msg);
  1092. return 0;
  1093. }
  1094. /*
  1095. * Queue cap releases when an inode is dropped from our cache.
  1096. */
  1097. void ceph_queue_caps_release(struct inode *inode)
  1098. {
  1099. struct ceph_inode_info *ci = ceph_inode(inode);
  1100. struct rb_node *p;
  1101. /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
  1102. * may call __ceph_caps_issued_mask() on a freeing inode. */
  1103. spin_lock(&ci->i_ceph_lock);
  1104. p = rb_first(&ci->i_caps);
  1105. while (p) {
  1106. struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
  1107. p = rb_next(p);
  1108. __ceph_remove_cap(cap, true);
  1109. }
  1110. spin_unlock(&ci->i_ceph_lock);
  1111. }
  1112. /*
  1113. * Send a cap msg on the given inode. Update our caps state, then
  1114. * drop i_ceph_lock and send the message.
  1115. *
  1116. * Make note of max_size reported/requested from mds, revoked caps
  1117. * that have now been implemented.
  1118. *
  1119. * Make half-hearted attempt ot to invalidate page cache if we are
  1120. * dropping RDCACHE. Note that this will leave behind locked pages
  1121. * that we'll then need to deal with elsewhere.
  1122. *
  1123. * Return non-zero if delayed release, or we experienced an error
  1124. * such that the caller should requeue + retry later.
  1125. *
  1126. * called with i_ceph_lock, then drops it.
  1127. * caller should hold snap_rwsem (read), s_mutex.
  1128. */
  1129. static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
  1130. int op, bool sync, int used, int want, int retain,
  1131. int flushing, u64 flush_tid, u64 oldest_flush_tid)
  1132. __releases(cap->ci->i_ceph_lock)
  1133. {
  1134. struct ceph_inode_info *ci = cap->ci;
  1135. struct inode *inode = &ci->vfs_inode;
  1136. struct ceph_buffer *old_blob = NULL;
  1137. struct cap_msg_args arg;
  1138. int held, revoking;
  1139. int wake = 0;
  1140. int delayed = 0;
  1141. int ret;
  1142. held = cap->issued | cap->implemented;
  1143. revoking = cap->implemented & ~cap->issued;
  1144. retain &= ~revoking;
  1145. dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
  1146. inode, cap, cap->session,
  1147. ceph_cap_string(held), ceph_cap_string(held & retain),
  1148. ceph_cap_string(revoking));
  1149. BUG_ON((retain & CEPH_CAP_PIN) == 0);
  1150. arg.session = cap->session;
  1151. /* don't release wanted unless we've waited a bit. */
  1152. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  1153. time_before(jiffies, ci->i_hold_caps_min)) {
  1154. dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
  1155. ceph_cap_string(cap->issued),
  1156. ceph_cap_string(cap->issued & retain),
  1157. ceph_cap_string(cap->mds_wanted),
  1158. ceph_cap_string(want));
  1159. want |= cap->mds_wanted;
  1160. retain |= cap->issued;
  1161. delayed = 1;
  1162. }
  1163. ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
  1164. if (want & ~cap->mds_wanted) {
  1165. /* user space may open/close single file frequently.
  1166. * This avoids droping mds_wanted immediately after
  1167. * requesting new mds_wanted.
  1168. */
  1169. __cap_set_timeouts(mdsc, ci);
  1170. }
  1171. cap->issued &= retain; /* drop bits we don't want */
  1172. if (cap->implemented & ~cap->issued) {
  1173. /*
  1174. * Wake up any waiters on wanted -> needed transition.
  1175. * This is due to the weird transition from buffered
  1176. * to sync IO... we need to flush dirty pages _before_
  1177. * allowing sync writes to avoid reordering.
  1178. */
  1179. wake = 1;
  1180. }
  1181. cap->implemented &= cap->issued | used;
  1182. cap->mds_wanted = want;
  1183. arg.ino = ceph_vino(inode).ino;
  1184. arg.cid = cap->cap_id;
  1185. arg.follows = flushing ? ci->i_head_snapc->seq : 0;
  1186. arg.flush_tid = flush_tid;
  1187. arg.oldest_flush_tid = oldest_flush_tid;
  1188. arg.size = inode->i_size;
  1189. ci->i_reported_size = arg.size;
  1190. arg.max_size = ci->i_wanted_max_size;
  1191. ci->i_requested_max_size = arg.max_size;
  1192. if (flushing & CEPH_CAP_XATTR_EXCL) {
  1193. old_blob = __ceph_build_xattrs_blob(ci);
  1194. arg.xattr_version = ci->i_xattrs.version;
  1195. arg.xattr_buf = ci->i_xattrs.blob;
  1196. } else {
  1197. arg.xattr_buf = NULL;
  1198. }
  1199. arg.mtime = inode->i_mtime;
  1200. arg.atime = inode->i_atime;
  1201. arg.ctime = inode->i_ctime;
  1202. arg.op = op;
  1203. arg.caps = cap->implemented;
  1204. arg.wanted = want;
  1205. arg.dirty = flushing;
  1206. arg.seq = cap->seq;
  1207. arg.issue_seq = cap->issue_seq;
  1208. arg.mseq = cap->mseq;
  1209. arg.time_warp_seq = ci->i_time_warp_seq;
  1210. arg.uid = inode->i_uid;
  1211. arg.gid = inode->i_gid;
  1212. arg.mode = inode->i_mode;
  1213. arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
  1214. if (list_empty(&ci->i_cap_snaps))
  1215. arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
  1216. else
  1217. arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
  1218. if (sync)
  1219. arg.flags |= CEPH_CLIENT_CAPS_SYNC;
  1220. spin_unlock(&ci->i_ceph_lock);
  1221. ceph_buffer_put(old_blob);
  1222. ret = send_cap_msg(&arg);
  1223. if (ret < 0) {
  1224. dout("error sending cap msg, must requeue %p\n", inode);
  1225. delayed = 1;
  1226. }
  1227. if (wake)
  1228. wake_up_all(&ci->i_cap_wq);
  1229. return delayed;
  1230. }
  1231. static inline int __send_flush_snap(struct inode *inode,
  1232. struct ceph_mds_session *session,
  1233. struct ceph_cap_snap *capsnap,
  1234. u32 mseq, u64 oldest_flush_tid)
  1235. {
  1236. struct cap_msg_args arg;
  1237. arg.session = session;
  1238. arg.ino = ceph_vino(inode).ino;
  1239. arg.cid = 0;
  1240. arg.follows = capsnap->follows;
  1241. arg.flush_tid = capsnap->cap_flush.tid;
  1242. arg.oldest_flush_tid = oldest_flush_tid;
  1243. arg.size = capsnap->size;
  1244. arg.max_size = 0;
  1245. arg.xattr_version = capsnap->xattr_version;
  1246. arg.xattr_buf = capsnap->xattr_blob;
  1247. arg.atime = capsnap->atime;
  1248. arg.mtime = capsnap->mtime;
  1249. arg.ctime = capsnap->ctime;
  1250. arg.op = CEPH_CAP_OP_FLUSHSNAP;
  1251. arg.caps = capsnap->issued;
  1252. arg.wanted = 0;
  1253. arg.dirty = capsnap->dirty;
  1254. arg.seq = 0;
  1255. arg.issue_seq = 0;
  1256. arg.mseq = mseq;
  1257. arg.time_warp_seq = capsnap->time_warp_seq;
  1258. arg.uid = capsnap->uid;
  1259. arg.gid = capsnap->gid;
  1260. arg.mode = capsnap->mode;
  1261. arg.inline_data = capsnap->inline_data;
  1262. arg.flags = 0;
  1263. return send_cap_msg(&arg);
  1264. }
  1265. /*
  1266. * When a snapshot is taken, clients accumulate dirty metadata on
  1267. * inodes with capabilities in ceph_cap_snaps to describe the file
  1268. * state at the time the snapshot was taken. This must be flushed
  1269. * asynchronously back to the MDS once sync writes complete and dirty
  1270. * data is written out.
  1271. *
  1272. * Called under i_ceph_lock. Takes s_mutex as needed.
  1273. */
  1274. static void __ceph_flush_snaps(struct ceph_inode_info *ci,
  1275. struct ceph_mds_session *session)
  1276. __releases(ci->i_ceph_lock)
  1277. __acquires(ci->i_ceph_lock)
  1278. {
  1279. struct inode *inode = &ci->vfs_inode;
  1280. struct ceph_mds_client *mdsc = session->s_mdsc;
  1281. struct ceph_cap_snap *capsnap;
  1282. u64 oldest_flush_tid = 0;
  1283. u64 first_tid = 1, last_tid = 0;
  1284. dout("__flush_snaps %p session %p\n", inode, session);
  1285. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1286. /*
  1287. * we need to wait for sync writes to complete and for dirty
  1288. * pages to be written out.
  1289. */
  1290. if (capsnap->dirty_pages || capsnap->writing)
  1291. break;
  1292. /* should be removed by ceph_try_drop_cap_snap() */
  1293. BUG_ON(!capsnap->need_flush);
  1294. /* only flush each capsnap once */
  1295. if (capsnap->cap_flush.tid > 0) {
  1296. dout(" already flushed %p, skipping\n", capsnap);
  1297. continue;
  1298. }
  1299. spin_lock(&mdsc->cap_dirty_lock);
  1300. capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
  1301. list_add_tail(&capsnap->cap_flush.g_list,
  1302. &mdsc->cap_flush_list);
  1303. if (oldest_flush_tid == 0)
  1304. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1305. if (list_empty(&ci->i_flushing_item)) {
  1306. list_add_tail(&ci->i_flushing_item,
  1307. &session->s_cap_flushing);
  1308. }
  1309. spin_unlock(&mdsc->cap_dirty_lock);
  1310. list_add_tail(&capsnap->cap_flush.i_list,
  1311. &ci->i_cap_flush_list);
  1312. if (first_tid == 1)
  1313. first_tid = capsnap->cap_flush.tid;
  1314. last_tid = capsnap->cap_flush.tid;
  1315. }
  1316. ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
  1317. while (first_tid <= last_tid) {
  1318. struct ceph_cap *cap = ci->i_auth_cap;
  1319. struct ceph_cap_flush *cf;
  1320. int ret;
  1321. if (!(cap && cap->session == session)) {
  1322. dout("__flush_snaps %p auth cap %p not mds%d, "
  1323. "stop\n", inode, cap, session->s_mds);
  1324. break;
  1325. }
  1326. ret = -ENOENT;
  1327. list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
  1328. if (cf->tid >= first_tid) {
  1329. ret = 0;
  1330. break;
  1331. }
  1332. }
  1333. if (ret < 0)
  1334. break;
  1335. first_tid = cf->tid + 1;
  1336. capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
  1337. refcount_inc(&capsnap->nref);
  1338. spin_unlock(&ci->i_ceph_lock);
  1339. dout("__flush_snaps %p capsnap %p tid %llu %s\n",
  1340. inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
  1341. ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
  1342. oldest_flush_tid);
  1343. if (ret < 0) {
  1344. pr_err("__flush_snaps: error sending cap flushsnap, "
  1345. "ino (%llx.%llx) tid %llu follows %llu\n",
  1346. ceph_vinop(inode), cf->tid, capsnap->follows);
  1347. }
  1348. ceph_put_cap_snap(capsnap);
  1349. spin_lock(&ci->i_ceph_lock);
  1350. }
  1351. }
  1352. void ceph_flush_snaps(struct ceph_inode_info *ci,
  1353. struct ceph_mds_session **psession)
  1354. {
  1355. struct inode *inode = &ci->vfs_inode;
  1356. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  1357. struct ceph_mds_session *session = NULL;
  1358. int mds;
  1359. dout("ceph_flush_snaps %p\n", inode);
  1360. if (psession)
  1361. session = *psession;
  1362. retry:
  1363. spin_lock(&ci->i_ceph_lock);
  1364. if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
  1365. dout(" no capsnap needs flush, doing nothing\n");
  1366. goto out;
  1367. }
  1368. if (!ci->i_auth_cap) {
  1369. dout(" no auth cap (migrating?), doing nothing\n");
  1370. goto out;
  1371. }
  1372. mds = ci->i_auth_cap->session->s_mds;
  1373. if (session && session->s_mds != mds) {
  1374. dout(" oops, wrong session %p mutex\n", session);
  1375. mutex_unlock(&session->s_mutex);
  1376. ceph_put_mds_session(session);
  1377. session = NULL;
  1378. }
  1379. if (!session) {
  1380. spin_unlock(&ci->i_ceph_lock);
  1381. mutex_lock(&mdsc->mutex);
  1382. session = __ceph_lookup_mds_session(mdsc, mds);
  1383. mutex_unlock(&mdsc->mutex);
  1384. if (session) {
  1385. dout(" inverting session/ino locks on %p\n", session);
  1386. mutex_lock(&session->s_mutex);
  1387. }
  1388. goto retry;
  1389. }
  1390. // make sure flushsnap messages are sent in proper order.
  1391. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
  1392. __kick_flushing_caps(mdsc, session, ci, 0);
  1393. ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
  1394. }
  1395. __ceph_flush_snaps(ci, session);
  1396. out:
  1397. spin_unlock(&ci->i_ceph_lock);
  1398. if (psession) {
  1399. *psession = session;
  1400. } else if (session) {
  1401. mutex_unlock(&session->s_mutex);
  1402. ceph_put_mds_session(session);
  1403. }
  1404. /* we flushed them all; remove this inode from the queue */
  1405. spin_lock(&mdsc->snap_flush_lock);
  1406. list_del_init(&ci->i_snap_flush_item);
  1407. spin_unlock(&mdsc->snap_flush_lock);
  1408. }
  1409. /*
  1410. * Mark caps dirty. If inode is newly dirty, return the dirty flags.
  1411. * Caller is then responsible for calling __mark_inode_dirty with the
  1412. * returned flags value.
  1413. */
  1414. int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
  1415. struct ceph_cap_flush **pcf)
  1416. {
  1417. struct ceph_mds_client *mdsc =
  1418. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  1419. struct inode *inode = &ci->vfs_inode;
  1420. int was = ci->i_dirty_caps;
  1421. int dirty = 0;
  1422. if (!ci->i_auth_cap) {
  1423. pr_warn("__mark_dirty_caps %p %llx mask %s, "
  1424. "but no auth cap (session was closed?)\n",
  1425. inode, ceph_ino(inode), ceph_cap_string(mask));
  1426. return 0;
  1427. }
  1428. dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
  1429. ceph_cap_string(mask), ceph_cap_string(was),
  1430. ceph_cap_string(was | mask));
  1431. ci->i_dirty_caps |= mask;
  1432. if (was == 0) {
  1433. WARN_ON_ONCE(ci->i_prealloc_cap_flush);
  1434. swap(ci->i_prealloc_cap_flush, *pcf);
  1435. if (!ci->i_head_snapc) {
  1436. WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
  1437. ci->i_head_snapc = ceph_get_snap_context(
  1438. ci->i_snap_realm->cached_context);
  1439. }
  1440. dout(" inode %p now dirty snapc %p auth cap %p\n",
  1441. &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
  1442. BUG_ON(!list_empty(&ci->i_dirty_item));
  1443. spin_lock(&mdsc->cap_dirty_lock);
  1444. list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
  1445. spin_unlock(&mdsc->cap_dirty_lock);
  1446. if (ci->i_flushing_caps == 0) {
  1447. ihold(inode);
  1448. dirty |= I_DIRTY_SYNC;
  1449. }
  1450. } else {
  1451. WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
  1452. }
  1453. BUG_ON(list_empty(&ci->i_dirty_item));
  1454. if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
  1455. (mask & CEPH_CAP_FILE_BUFFER))
  1456. dirty |= I_DIRTY_DATASYNC;
  1457. __cap_delay_requeue(mdsc, ci);
  1458. return dirty;
  1459. }
  1460. struct ceph_cap_flush *ceph_alloc_cap_flush(void)
  1461. {
  1462. return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
  1463. }
  1464. void ceph_free_cap_flush(struct ceph_cap_flush *cf)
  1465. {
  1466. if (cf)
  1467. kmem_cache_free(ceph_cap_flush_cachep, cf);
  1468. }
  1469. static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
  1470. {
  1471. if (!list_empty(&mdsc->cap_flush_list)) {
  1472. struct ceph_cap_flush *cf =
  1473. list_first_entry(&mdsc->cap_flush_list,
  1474. struct ceph_cap_flush, g_list);
  1475. return cf->tid;
  1476. }
  1477. return 0;
  1478. }
  1479. /*
  1480. * Remove cap_flush from the mdsc's or inode's flushing cap list.
  1481. * Return true if caller needs to wake up flush waiters.
  1482. */
  1483. static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
  1484. struct ceph_inode_info *ci,
  1485. struct ceph_cap_flush *cf)
  1486. {
  1487. struct ceph_cap_flush *prev;
  1488. bool wake = cf->wake;
  1489. if (mdsc) {
  1490. /* are there older pending cap flushes? */
  1491. if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
  1492. prev = list_prev_entry(cf, g_list);
  1493. prev->wake = true;
  1494. wake = false;
  1495. }
  1496. list_del(&cf->g_list);
  1497. } else if (ci) {
  1498. if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
  1499. prev = list_prev_entry(cf, i_list);
  1500. prev->wake = true;
  1501. wake = false;
  1502. }
  1503. list_del(&cf->i_list);
  1504. } else {
  1505. BUG_ON(1);
  1506. }
  1507. return wake;
  1508. }
  1509. /*
  1510. * Add dirty inode to the flushing list. Assigned a seq number so we
  1511. * can wait for caps to flush without starving.
  1512. *
  1513. * Called under i_ceph_lock.
  1514. */
  1515. static int __mark_caps_flushing(struct inode *inode,
  1516. struct ceph_mds_session *session, bool wake,
  1517. u64 *flush_tid, u64 *oldest_flush_tid)
  1518. {
  1519. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1520. struct ceph_inode_info *ci = ceph_inode(inode);
  1521. struct ceph_cap_flush *cf = NULL;
  1522. int flushing;
  1523. BUG_ON(ci->i_dirty_caps == 0);
  1524. BUG_ON(list_empty(&ci->i_dirty_item));
  1525. BUG_ON(!ci->i_prealloc_cap_flush);
  1526. flushing = ci->i_dirty_caps;
  1527. dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
  1528. ceph_cap_string(flushing),
  1529. ceph_cap_string(ci->i_flushing_caps),
  1530. ceph_cap_string(ci->i_flushing_caps | flushing));
  1531. ci->i_flushing_caps |= flushing;
  1532. ci->i_dirty_caps = 0;
  1533. dout(" inode %p now !dirty\n", inode);
  1534. swap(cf, ci->i_prealloc_cap_flush);
  1535. cf->caps = flushing;
  1536. cf->wake = wake;
  1537. spin_lock(&mdsc->cap_dirty_lock);
  1538. list_del_init(&ci->i_dirty_item);
  1539. cf->tid = ++mdsc->last_cap_flush_tid;
  1540. list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
  1541. *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1542. if (list_empty(&ci->i_flushing_item)) {
  1543. list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1544. mdsc->num_cap_flushing++;
  1545. }
  1546. spin_unlock(&mdsc->cap_dirty_lock);
  1547. list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
  1548. *flush_tid = cf->tid;
  1549. return flushing;
  1550. }
  1551. /*
  1552. * try to invalidate mapping pages without blocking.
  1553. */
  1554. static int try_nonblocking_invalidate(struct inode *inode)
  1555. {
  1556. struct ceph_inode_info *ci = ceph_inode(inode);
  1557. u32 invalidating_gen = ci->i_rdcache_gen;
  1558. spin_unlock(&ci->i_ceph_lock);
  1559. invalidate_mapping_pages(&inode->i_data, 0, -1);
  1560. spin_lock(&ci->i_ceph_lock);
  1561. if (inode->i_data.nrpages == 0 &&
  1562. invalidating_gen == ci->i_rdcache_gen) {
  1563. /* success. */
  1564. dout("try_nonblocking_invalidate %p success\n", inode);
  1565. /* save any racing async invalidate some trouble */
  1566. ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
  1567. return 0;
  1568. }
  1569. dout("try_nonblocking_invalidate %p failed\n", inode);
  1570. return -1;
  1571. }
  1572. bool __ceph_should_report_size(struct ceph_inode_info *ci)
  1573. {
  1574. loff_t size = ci->vfs_inode.i_size;
  1575. /* mds will adjust max size according to the reported size */
  1576. if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
  1577. return false;
  1578. if (size >= ci->i_max_size)
  1579. return true;
  1580. /* half of previous max_size increment has been used */
  1581. if (ci->i_max_size > ci->i_reported_size &&
  1582. (size << 1) >= ci->i_max_size + ci->i_reported_size)
  1583. return true;
  1584. return false;
  1585. }
  1586. /*
  1587. * Swiss army knife function to examine currently used and wanted
  1588. * versus held caps. Release, flush, ack revoked caps to mds as
  1589. * appropriate.
  1590. *
  1591. * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
  1592. * cap release further.
  1593. * CHECK_CAPS_AUTHONLY - we should only check the auth cap
  1594. * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
  1595. * further delay.
  1596. */
  1597. void ceph_check_caps(struct ceph_inode_info *ci, int flags,
  1598. struct ceph_mds_session *session)
  1599. {
  1600. struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
  1601. struct ceph_mds_client *mdsc = fsc->mdsc;
  1602. struct inode *inode = &ci->vfs_inode;
  1603. struct ceph_cap *cap;
  1604. u64 flush_tid, oldest_flush_tid;
  1605. int file_wanted, used, cap_used;
  1606. int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
  1607. int issued, implemented, want, retain, revoking, flushing = 0;
  1608. int mds = -1; /* keep track of how far we've gone through i_caps list
  1609. to avoid an infinite loop on retry */
  1610. struct rb_node *p;
  1611. int delayed = 0, sent = 0;
  1612. bool no_delay = flags & CHECK_CAPS_NODELAY;
  1613. bool queue_invalidate = false;
  1614. bool tried_invalidate = false;
  1615. /* if we are unmounting, flush any unused caps immediately. */
  1616. if (mdsc->stopping)
  1617. no_delay = true;
  1618. spin_lock(&ci->i_ceph_lock);
  1619. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  1620. flags |= CHECK_CAPS_FLUSH;
  1621. if (!(flags & CHECK_CAPS_AUTHONLY) ||
  1622. (ci->i_auth_cap && __ceph_is_single_caps(ci)))
  1623. __cap_delay_cancel(mdsc, ci);
  1624. goto retry_locked;
  1625. retry:
  1626. spin_lock(&ci->i_ceph_lock);
  1627. retry_locked:
  1628. file_wanted = __ceph_caps_file_wanted(ci);
  1629. used = __ceph_caps_used(ci);
  1630. issued = __ceph_caps_issued(ci, &implemented);
  1631. revoking = implemented & ~issued;
  1632. want = file_wanted;
  1633. retain = file_wanted | used | CEPH_CAP_PIN;
  1634. if (!mdsc->stopping && inode->i_nlink > 0) {
  1635. if (file_wanted) {
  1636. retain |= CEPH_CAP_ANY; /* be greedy */
  1637. } else if (S_ISDIR(inode->i_mode) &&
  1638. (issued & CEPH_CAP_FILE_SHARED) &&
  1639. __ceph_dir_is_complete(ci)) {
  1640. /*
  1641. * If a directory is complete, we want to keep
  1642. * the exclusive cap. So that MDS does not end up
  1643. * revoking the shared cap on every create/unlink
  1644. * operation.
  1645. */
  1646. want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
  1647. retain |= want;
  1648. } else {
  1649. retain |= CEPH_CAP_ANY_SHARED;
  1650. /*
  1651. * keep RD only if we didn't have the file open RW,
  1652. * because then the mds would revoke it anyway to
  1653. * journal max_size=0.
  1654. */
  1655. if (ci->i_max_size == 0)
  1656. retain |= CEPH_CAP_ANY_RD;
  1657. }
  1658. }
  1659. dout("check_caps %p file_want %s used %s dirty %s flushing %s"
  1660. " issued %s revoking %s retain %s %s%s%s\n", inode,
  1661. ceph_cap_string(file_wanted),
  1662. ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
  1663. ceph_cap_string(ci->i_flushing_caps),
  1664. ceph_cap_string(issued), ceph_cap_string(revoking),
  1665. ceph_cap_string(retain),
  1666. (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
  1667. (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
  1668. (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
  1669. /*
  1670. * If we no longer need to hold onto old our caps, and we may
  1671. * have cached pages, but don't want them, then try to invalidate.
  1672. * If we fail, it's because pages are locked.... try again later.
  1673. */
  1674. if ((!no_delay || mdsc->stopping) &&
  1675. !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
  1676. !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
  1677. inode->i_data.nrpages && /* have cached pages */
  1678. (revoking & (CEPH_CAP_FILE_CACHE|
  1679. CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
  1680. !tried_invalidate) {
  1681. dout("check_caps trying to invalidate on %p\n", inode);
  1682. if (try_nonblocking_invalidate(inode) < 0) {
  1683. dout("check_caps queuing invalidate\n");
  1684. queue_invalidate = true;
  1685. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  1686. }
  1687. tried_invalidate = true;
  1688. goto retry_locked;
  1689. }
  1690. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  1691. cap = rb_entry(p, struct ceph_cap, ci_node);
  1692. /* avoid looping forever */
  1693. if (mds >= cap->mds ||
  1694. ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
  1695. continue;
  1696. /* NOTE: no side-effects allowed, until we take s_mutex */
  1697. cap_used = used;
  1698. if (ci->i_auth_cap && cap != ci->i_auth_cap)
  1699. cap_used &= ~ci->i_auth_cap->issued;
  1700. revoking = cap->implemented & ~cap->issued;
  1701. dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
  1702. cap->mds, cap, ceph_cap_string(cap_used),
  1703. ceph_cap_string(cap->issued),
  1704. ceph_cap_string(cap->implemented),
  1705. ceph_cap_string(revoking));
  1706. if (cap == ci->i_auth_cap &&
  1707. (cap->issued & CEPH_CAP_FILE_WR)) {
  1708. /* request larger max_size from MDS? */
  1709. if (ci->i_wanted_max_size > ci->i_max_size &&
  1710. ci->i_wanted_max_size > ci->i_requested_max_size) {
  1711. dout("requesting new max_size\n");
  1712. goto ack;
  1713. }
  1714. /* approaching file_max? */
  1715. if (__ceph_should_report_size(ci)) {
  1716. dout("i_size approaching max_size\n");
  1717. goto ack;
  1718. }
  1719. }
  1720. /* flush anything dirty? */
  1721. if (cap == ci->i_auth_cap) {
  1722. if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
  1723. dout("flushing dirty caps\n");
  1724. goto ack;
  1725. }
  1726. if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
  1727. dout("flushing snap caps\n");
  1728. goto ack;
  1729. }
  1730. }
  1731. /* completed revocation? going down and there are no caps? */
  1732. if (revoking && (revoking & cap_used) == 0) {
  1733. dout("completed revocation of %s\n",
  1734. ceph_cap_string(cap->implemented & ~cap->issued));
  1735. goto ack;
  1736. }
  1737. /* want more caps from mds? */
  1738. if (want & ~(cap->mds_wanted | cap->issued))
  1739. goto ack;
  1740. /* things we might delay */
  1741. if ((cap->issued & ~retain) == 0 &&
  1742. cap->mds_wanted == want)
  1743. continue; /* nope, all good */
  1744. if (no_delay)
  1745. goto ack;
  1746. /* delay? */
  1747. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  1748. time_before(jiffies, ci->i_hold_caps_max)) {
  1749. dout(" delaying issued %s -> %s, wanted %s -> %s\n",
  1750. ceph_cap_string(cap->issued),
  1751. ceph_cap_string(cap->issued & retain),
  1752. ceph_cap_string(cap->mds_wanted),
  1753. ceph_cap_string(want));
  1754. delayed++;
  1755. continue;
  1756. }
  1757. ack:
  1758. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1759. dout(" skipping %p I_NOFLUSH set\n", inode);
  1760. continue;
  1761. }
  1762. if (session && session != cap->session) {
  1763. dout("oops, wrong session %p mutex\n", session);
  1764. mutex_unlock(&session->s_mutex);
  1765. session = NULL;
  1766. }
  1767. if (!session) {
  1768. session = cap->session;
  1769. if (mutex_trylock(&session->s_mutex) == 0) {
  1770. dout("inverting session/ino locks on %p\n",
  1771. session);
  1772. spin_unlock(&ci->i_ceph_lock);
  1773. if (took_snap_rwsem) {
  1774. up_read(&mdsc->snap_rwsem);
  1775. took_snap_rwsem = 0;
  1776. }
  1777. mutex_lock(&session->s_mutex);
  1778. goto retry;
  1779. }
  1780. }
  1781. /* kick flushing and flush snaps before sending normal
  1782. * cap message */
  1783. if (cap == ci->i_auth_cap &&
  1784. (ci->i_ceph_flags &
  1785. (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
  1786. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
  1787. __kick_flushing_caps(mdsc, session, ci, 0);
  1788. ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
  1789. }
  1790. if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
  1791. __ceph_flush_snaps(ci, session);
  1792. goto retry_locked;
  1793. }
  1794. /* take snap_rwsem after session mutex */
  1795. if (!took_snap_rwsem) {
  1796. if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
  1797. dout("inverting snap/in locks on %p\n",
  1798. inode);
  1799. spin_unlock(&ci->i_ceph_lock);
  1800. down_read(&mdsc->snap_rwsem);
  1801. took_snap_rwsem = 1;
  1802. goto retry;
  1803. }
  1804. took_snap_rwsem = 1;
  1805. }
  1806. if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
  1807. flushing = __mark_caps_flushing(inode, session, false,
  1808. &flush_tid,
  1809. &oldest_flush_tid);
  1810. } else {
  1811. flushing = 0;
  1812. flush_tid = 0;
  1813. spin_lock(&mdsc->cap_dirty_lock);
  1814. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  1815. spin_unlock(&mdsc->cap_dirty_lock);
  1816. }
  1817. mds = cap->mds; /* remember mds, so we don't repeat */
  1818. sent++;
  1819. /* __send_cap drops i_ceph_lock */
  1820. delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
  1821. cap_used, want, retain, flushing,
  1822. flush_tid, oldest_flush_tid);
  1823. goto retry; /* retake i_ceph_lock and restart our cap scan. */
  1824. }
  1825. /* Reschedule delayed caps release if we delayed anything */
  1826. if (delayed)
  1827. __cap_delay_requeue(mdsc, ci);
  1828. spin_unlock(&ci->i_ceph_lock);
  1829. if (queue_invalidate)
  1830. ceph_queue_invalidate(inode);
  1831. if (session)
  1832. mutex_unlock(&session->s_mutex);
  1833. if (took_snap_rwsem)
  1834. up_read(&mdsc->snap_rwsem);
  1835. }
  1836. /*
  1837. * Try to flush dirty caps back to the auth mds.
  1838. */
  1839. static int try_flush_caps(struct inode *inode, u64 *ptid)
  1840. {
  1841. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1842. struct ceph_inode_info *ci = ceph_inode(inode);
  1843. struct ceph_mds_session *session = NULL;
  1844. int flushing = 0;
  1845. u64 flush_tid = 0, oldest_flush_tid = 0;
  1846. retry:
  1847. spin_lock(&ci->i_ceph_lock);
  1848. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1849. spin_unlock(&ci->i_ceph_lock);
  1850. dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
  1851. goto out;
  1852. }
  1853. if (ci->i_dirty_caps && ci->i_auth_cap) {
  1854. struct ceph_cap *cap = ci->i_auth_cap;
  1855. int used = __ceph_caps_used(ci);
  1856. int want = __ceph_caps_wanted(ci);
  1857. int delayed;
  1858. if (!session || session != cap->session) {
  1859. spin_unlock(&ci->i_ceph_lock);
  1860. if (session)
  1861. mutex_unlock(&session->s_mutex);
  1862. session = cap->session;
  1863. mutex_lock(&session->s_mutex);
  1864. goto retry;
  1865. }
  1866. if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
  1867. spin_unlock(&ci->i_ceph_lock);
  1868. goto out;
  1869. }
  1870. flushing = __mark_caps_flushing(inode, session, true,
  1871. &flush_tid, &oldest_flush_tid);
  1872. /* __send_cap drops i_ceph_lock */
  1873. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
  1874. used, want, (cap->issued | cap->implemented),
  1875. flushing, flush_tid, oldest_flush_tid);
  1876. if (delayed) {
  1877. spin_lock(&ci->i_ceph_lock);
  1878. __cap_delay_requeue(mdsc, ci);
  1879. spin_unlock(&ci->i_ceph_lock);
  1880. }
  1881. } else {
  1882. if (!list_empty(&ci->i_cap_flush_list)) {
  1883. struct ceph_cap_flush *cf =
  1884. list_last_entry(&ci->i_cap_flush_list,
  1885. struct ceph_cap_flush, i_list);
  1886. cf->wake = true;
  1887. flush_tid = cf->tid;
  1888. }
  1889. flushing = ci->i_flushing_caps;
  1890. spin_unlock(&ci->i_ceph_lock);
  1891. }
  1892. out:
  1893. if (session)
  1894. mutex_unlock(&session->s_mutex);
  1895. *ptid = flush_tid;
  1896. return flushing;
  1897. }
  1898. /*
  1899. * Return true if we've flushed caps through the given flush_tid.
  1900. */
  1901. static int caps_are_flushed(struct inode *inode, u64 flush_tid)
  1902. {
  1903. struct ceph_inode_info *ci = ceph_inode(inode);
  1904. int ret = 1;
  1905. spin_lock(&ci->i_ceph_lock);
  1906. if (!list_empty(&ci->i_cap_flush_list)) {
  1907. struct ceph_cap_flush * cf =
  1908. list_first_entry(&ci->i_cap_flush_list,
  1909. struct ceph_cap_flush, i_list);
  1910. if (cf->tid <= flush_tid)
  1911. ret = 0;
  1912. }
  1913. spin_unlock(&ci->i_ceph_lock);
  1914. return ret;
  1915. }
  1916. /*
  1917. * wait for any unsafe requests to complete.
  1918. */
  1919. static int unsafe_request_wait(struct inode *inode)
  1920. {
  1921. struct ceph_inode_info *ci = ceph_inode(inode);
  1922. struct ceph_mds_request *req1 = NULL, *req2 = NULL;
  1923. int ret, err = 0;
  1924. spin_lock(&ci->i_unsafe_lock);
  1925. if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
  1926. req1 = list_last_entry(&ci->i_unsafe_dirops,
  1927. struct ceph_mds_request,
  1928. r_unsafe_dir_item);
  1929. ceph_mdsc_get_request(req1);
  1930. }
  1931. if (!list_empty(&ci->i_unsafe_iops)) {
  1932. req2 = list_last_entry(&ci->i_unsafe_iops,
  1933. struct ceph_mds_request,
  1934. r_unsafe_target_item);
  1935. ceph_mdsc_get_request(req2);
  1936. }
  1937. spin_unlock(&ci->i_unsafe_lock);
  1938. dout("unsafe_request_wait %p wait on tid %llu %llu\n",
  1939. inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
  1940. if (req1) {
  1941. ret = !wait_for_completion_timeout(&req1->r_safe_completion,
  1942. ceph_timeout_jiffies(req1->r_timeout));
  1943. if (ret)
  1944. err = -EIO;
  1945. ceph_mdsc_put_request(req1);
  1946. }
  1947. if (req2) {
  1948. ret = !wait_for_completion_timeout(&req2->r_safe_completion,
  1949. ceph_timeout_jiffies(req2->r_timeout));
  1950. if (ret)
  1951. err = -EIO;
  1952. ceph_mdsc_put_request(req2);
  1953. }
  1954. return err;
  1955. }
  1956. int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  1957. {
  1958. struct inode *inode = file->f_mapping->host;
  1959. struct ceph_inode_info *ci = ceph_inode(inode);
  1960. u64 flush_tid;
  1961. int ret;
  1962. int dirty;
  1963. dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
  1964. ret = file_write_and_wait_range(file, start, end);
  1965. if (ret < 0)
  1966. goto out;
  1967. if (datasync)
  1968. goto out;
  1969. inode_lock(inode);
  1970. dirty = try_flush_caps(inode, &flush_tid);
  1971. dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
  1972. ret = unsafe_request_wait(inode);
  1973. /*
  1974. * only wait on non-file metadata writeback (the mds
  1975. * can recover size and mtime, so we don't need to
  1976. * wait for that)
  1977. */
  1978. if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
  1979. ret = wait_event_interruptible(ci->i_cap_wq,
  1980. caps_are_flushed(inode, flush_tid));
  1981. }
  1982. inode_unlock(inode);
  1983. out:
  1984. dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
  1985. return ret;
  1986. }
  1987. /*
  1988. * Flush any dirty caps back to the mds. If we aren't asked to wait,
  1989. * queue inode for flush but don't do so immediately, because we can
  1990. * get by with fewer MDS messages if we wait for data writeback to
  1991. * complete first.
  1992. */
  1993. int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
  1994. {
  1995. struct ceph_inode_info *ci = ceph_inode(inode);
  1996. u64 flush_tid;
  1997. int err = 0;
  1998. int dirty;
  1999. int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
  2000. dout("write_inode %p wait=%d\n", inode, wait);
  2001. if (wait) {
  2002. dirty = try_flush_caps(inode, &flush_tid);
  2003. if (dirty)
  2004. err = wait_event_interruptible(ci->i_cap_wq,
  2005. caps_are_flushed(inode, flush_tid));
  2006. } else {
  2007. struct ceph_mds_client *mdsc =
  2008. ceph_sb_to_client(inode->i_sb)->mdsc;
  2009. spin_lock(&ci->i_ceph_lock);
  2010. if (__ceph_caps_dirty(ci))
  2011. __cap_delay_requeue_front(mdsc, ci);
  2012. spin_unlock(&ci->i_ceph_lock);
  2013. }
  2014. return err;
  2015. }
  2016. static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
  2017. struct ceph_mds_session *session,
  2018. struct ceph_inode_info *ci,
  2019. u64 oldest_flush_tid)
  2020. __releases(ci->i_ceph_lock)
  2021. __acquires(ci->i_ceph_lock)
  2022. {
  2023. struct inode *inode = &ci->vfs_inode;
  2024. struct ceph_cap *cap;
  2025. struct ceph_cap_flush *cf;
  2026. int ret;
  2027. u64 first_tid = 0;
  2028. list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
  2029. if (cf->tid < first_tid)
  2030. continue;
  2031. cap = ci->i_auth_cap;
  2032. if (!(cap && cap->session == session)) {
  2033. pr_err("%p auth cap %p not mds%d ???\n",
  2034. inode, cap, session->s_mds);
  2035. break;
  2036. }
  2037. first_tid = cf->tid + 1;
  2038. if (cf->caps) {
  2039. dout("kick_flushing_caps %p cap %p tid %llu %s\n",
  2040. inode, cap, cf->tid, ceph_cap_string(cf->caps));
  2041. ci->i_ceph_flags |= CEPH_I_NODELAY;
  2042. ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
  2043. false, __ceph_caps_used(ci),
  2044. __ceph_caps_wanted(ci),
  2045. cap->issued | cap->implemented,
  2046. cf->caps, cf->tid, oldest_flush_tid);
  2047. if (ret) {
  2048. pr_err("kick_flushing_caps: error sending "
  2049. "cap flush, ino (%llx.%llx) "
  2050. "tid %llu flushing %s\n",
  2051. ceph_vinop(inode), cf->tid,
  2052. ceph_cap_string(cf->caps));
  2053. }
  2054. } else {
  2055. struct ceph_cap_snap *capsnap =
  2056. container_of(cf, struct ceph_cap_snap,
  2057. cap_flush);
  2058. dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
  2059. inode, capsnap, cf->tid,
  2060. ceph_cap_string(capsnap->dirty));
  2061. refcount_inc(&capsnap->nref);
  2062. spin_unlock(&ci->i_ceph_lock);
  2063. ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
  2064. oldest_flush_tid);
  2065. if (ret < 0) {
  2066. pr_err("kick_flushing_caps: error sending "
  2067. "cap flushsnap, ino (%llx.%llx) "
  2068. "tid %llu follows %llu\n",
  2069. ceph_vinop(inode), cf->tid,
  2070. capsnap->follows);
  2071. }
  2072. ceph_put_cap_snap(capsnap);
  2073. }
  2074. spin_lock(&ci->i_ceph_lock);
  2075. }
  2076. }
  2077. void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
  2078. struct ceph_mds_session *session)
  2079. {
  2080. struct ceph_inode_info *ci;
  2081. struct ceph_cap *cap;
  2082. u64 oldest_flush_tid;
  2083. dout("early_kick_flushing_caps mds%d\n", session->s_mds);
  2084. spin_lock(&mdsc->cap_dirty_lock);
  2085. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  2086. spin_unlock(&mdsc->cap_dirty_lock);
  2087. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  2088. spin_lock(&ci->i_ceph_lock);
  2089. cap = ci->i_auth_cap;
  2090. if (!(cap && cap->session == session)) {
  2091. pr_err("%p auth cap %p not mds%d ???\n",
  2092. &ci->vfs_inode, cap, session->s_mds);
  2093. spin_unlock(&ci->i_ceph_lock);
  2094. continue;
  2095. }
  2096. /*
  2097. * if flushing caps were revoked, we re-send the cap flush
  2098. * in client reconnect stage. This guarantees MDS * processes
  2099. * the cap flush message before issuing the flushing caps to
  2100. * other client.
  2101. */
  2102. if ((cap->issued & ci->i_flushing_caps) !=
  2103. ci->i_flushing_caps) {
  2104. ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
  2105. __kick_flushing_caps(mdsc, session, ci,
  2106. oldest_flush_tid);
  2107. } else {
  2108. ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
  2109. }
  2110. spin_unlock(&ci->i_ceph_lock);
  2111. }
  2112. }
  2113. void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
  2114. struct ceph_mds_session *session)
  2115. {
  2116. struct ceph_inode_info *ci;
  2117. struct ceph_cap *cap;
  2118. u64 oldest_flush_tid;
  2119. dout("kick_flushing_caps mds%d\n", session->s_mds);
  2120. spin_lock(&mdsc->cap_dirty_lock);
  2121. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  2122. spin_unlock(&mdsc->cap_dirty_lock);
  2123. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  2124. spin_lock(&ci->i_ceph_lock);
  2125. cap = ci->i_auth_cap;
  2126. if (!(cap && cap->session == session)) {
  2127. pr_err("%p auth cap %p not mds%d ???\n",
  2128. &ci->vfs_inode, cap, session->s_mds);
  2129. spin_unlock(&ci->i_ceph_lock);
  2130. continue;
  2131. }
  2132. if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
  2133. ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
  2134. __kick_flushing_caps(mdsc, session, ci,
  2135. oldest_flush_tid);
  2136. }
  2137. spin_unlock(&ci->i_ceph_lock);
  2138. }
  2139. }
  2140. static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
  2141. struct ceph_mds_session *session,
  2142. struct inode *inode)
  2143. __releases(ci->i_ceph_lock)
  2144. {
  2145. struct ceph_inode_info *ci = ceph_inode(inode);
  2146. struct ceph_cap *cap;
  2147. cap = ci->i_auth_cap;
  2148. dout("kick_flushing_inode_caps %p flushing %s\n", inode,
  2149. ceph_cap_string(ci->i_flushing_caps));
  2150. if (!list_empty(&ci->i_cap_flush_list)) {
  2151. u64 oldest_flush_tid;
  2152. spin_lock(&mdsc->cap_dirty_lock);
  2153. list_move_tail(&ci->i_flushing_item,
  2154. &cap->session->s_cap_flushing);
  2155. oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  2156. spin_unlock(&mdsc->cap_dirty_lock);
  2157. ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
  2158. __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
  2159. spin_unlock(&ci->i_ceph_lock);
  2160. } else {
  2161. spin_unlock(&ci->i_ceph_lock);
  2162. }
  2163. }
  2164. /*
  2165. * Take references to capabilities we hold, so that we don't release
  2166. * them to the MDS prematurely.
  2167. *
  2168. * Protected by i_ceph_lock.
  2169. */
  2170. static void __take_cap_refs(struct ceph_inode_info *ci, int got,
  2171. bool snap_rwsem_locked)
  2172. {
  2173. if (got & CEPH_CAP_PIN)
  2174. ci->i_pin_ref++;
  2175. if (got & CEPH_CAP_FILE_RD)
  2176. ci->i_rd_ref++;
  2177. if (got & CEPH_CAP_FILE_CACHE)
  2178. ci->i_rdcache_ref++;
  2179. if (got & CEPH_CAP_FILE_WR) {
  2180. if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
  2181. BUG_ON(!snap_rwsem_locked);
  2182. ci->i_head_snapc = ceph_get_snap_context(
  2183. ci->i_snap_realm->cached_context);
  2184. }
  2185. ci->i_wr_ref++;
  2186. }
  2187. if (got & CEPH_CAP_FILE_BUFFER) {
  2188. if (ci->i_wb_ref == 0)
  2189. ihold(&ci->vfs_inode);
  2190. ci->i_wb_ref++;
  2191. dout("__take_cap_refs %p wb %d -> %d (?)\n",
  2192. &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
  2193. }
  2194. }
  2195. /*
  2196. * Try to grab cap references. Specify those refs we @want, and the
  2197. * minimal set we @need. Also include the larger offset we are writing
  2198. * to (when applicable), and check against max_size here as well.
  2199. * Note that caller is responsible for ensuring max_size increases are
  2200. * requested from the MDS.
  2201. */
  2202. static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
  2203. loff_t endoff, bool nonblock, int *got, int *err)
  2204. {
  2205. struct inode *inode = &ci->vfs_inode;
  2206. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  2207. int ret = 0;
  2208. int have, implemented;
  2209. int file_wanted;
  2210. bool snap_rwsem_locked = false;
  2211. dout("get_cap_refs %p need %s want %s\n", inode,
  2212. ceph_cap_string(need), ceph_cap_string(want));
  2213. again:
  2214. spin_lock(&ci->i_ceph_lock);
  2215. /* make sure file is actually open */
  2216. file_wanted = __ceph_caps_file_wanted(ci);
  2217. if ((file_wanted & need) != need) {
  2218. dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
  2219. ceph_cap_string(need), ceph_cap_string(file_wanted));
  2220. *err = -EBADF;
  2221. ret = 1;
  2222. goto out_unlock;
  2223. }
  2224. /* finish pending truncate */
  2225. while (ci->i_truncate_pending) {
  2226. spin_unlock(&ci->i_ceph_lock);
  2227. if (snap_rwsem_locked) {
  2228. up_read(&mdsc->snap_rwsem);
  2229. snap_rwsem_locked = false;
  2230. }
  2231. __ceph_do_pending_vmtruncate(inode);
  2232. spin_lock(&ci->i_ceph_lock);
  2233. }
  2234. have = __ceph_caps_issued(ci, &implemented);
  2235. if (have & need & CEPH_CAP_FILE_WR) {
  2236. if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
  2237. dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
  2238. inode, endoff, ci->i_max_size);
  2239. if (endoff > ci->i_requested_max_size) {
  2240. *err = -EAGAIN;
  2241. ret = 1;
  2242. }
  2243. goto out_unlock;
  2244. }
  2245. /*
  2246. * If a sync write is in progress, we must wait, so that we
  2247. * can get a final snapshot value for size+mtime.
  2248. */
  2249. if (__ceph_have_pending_cap_snap(ci)) {
  2250. dout("get_cap_refs %p cap_snap_pending\n", inode);
  2251. goto out_unlock;
  2252. }
  2253. }
  2254. if ((have & need) == need) {
  2255. /*
  2256. * Look at (implemented & ~have & not) so that we keep waiting
  2257. * on transition from wanted -> needed caps. This is needed
  2258. * for WRBUFFER|WR -> WR to avoid a new WR sync write from
  2259. * going before a prior buffered writeback happens.
  2260. */
  2261. int not = want & ~(have & need);
  2262. int revoking = implemented & ~have;
  2263. dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
  2264. inode, ceph_cap_string(have), ceph_cap_string(not),
  2265. ceph_cap_string(revoking));
  2266. if ((revoking & not) == 0) {
  2267. if (!snap_rwsem_locked &&
  2268. !ci->i_head_snapc &&
  2269. (need & CEPH_CAP_FILE_WR)) {
  2270. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  2271. /*
  2272. * we can not call down_read() when
  2273. * task isn't in TASK_RUNNING state
  2274. */
  2275. if (nonblock) {
  2276. *err = -EAGAIN;
  2277. ret = 1;
  2278. goto out_unlock;
  2279. }
  2280. spin_unlock(&ci->i_ceph_lock);
  2281. down_read(&mdsc->snap_rwsem);
  2282. snap_rwsem_locked = true;
  2283. goto again;
  2284. }
  2285. snap_rwsem_locked = true;
  2286. }
  2287. *got = need | (have & want);
  2288. if ((need & CEPH_CAP_FILE_RD) &&
  2289. !(*got & CEPH_CAP_FILE_CACHE))
  2290. ceph_disable_fscache_readpage(ci);
  2291. __take_cap_refs(ci, *got, true);
  2292. ret = 1;
  2293. }
  2294. } else {
  2295. int session_readonly = false;
  2296. if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
  2297. struct ceph_mds_session *s = ci->i_auth_cap->session;
  2298. spin_lock(&s->s_cap_lock);
  2299. session_readonly = s->s_readonly;
  2300. spin_unlock(&s->s_cap_lock);
  2301. }
  2302. if (session_readonly) {
  2303. dout("get_cap_refs %p needed %s but mds%d readonly\n",
  2304. inode, ceph_cap_string(need), ci->i_auth_cap->mds);
  2305. *err = -EROFS;
  2306. ret = 1;
  2307. goto out_unlock;
  2308. }
  2309. if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
  2310. int mds_wanted;
  2311. if (READ_ONCE(mdsc->fsc->mount_state) ==
  2312. CEPH_MOUNT_SHUTDOWN) {
  2313. dout("get_cap_refs %p forced umount\n", inode);
  2314. *err = -EIO;
  2315. ret = 1;
  2316. goto out_unlock;
  2317. }
  2318. mds_wanted = __ceph_caps_mds_wanted(ci, false);
  2319. if (need & ~(mds_wanted & need)) {
  2320. dout("get_cap_refs %p caps were dropped"
  2321. " (session killed?)\n", inode);
  2322. *err = -ESTALE;
  2323. ret = 1;
  2324. goto out_unlock;
  2325. }
  2326. if (!(file_wanted & ~mds_wanted))
  2327. ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
  2328. }
  2329. dout("get_cap_refs %p have %s needed %s\n", inode,
  2330. ceph_cap_string(have), ceph_cap_string(need));
  2331. }
  2332. out_unlock:
  2333. spin_unlock(&ci->i_ceph_lock);
  2334. if (snap_rwsem_locked)
  2335. up_read(&mdsc->snap_rwsem);
  2336. dout("get_cap_refs %p ret %d got %s\n", inode,
  2337. ret, ceph_cap_string(*got));
  2338. return ret;
  2339. }
  2340. /*
  2341. * Check the offset we are writing up to against our current
  2342. * max_size. If necessary, tell the MDS we want to write to
  2343. * a larger offset.
  2344. */
  2345. static void check_max_size(struct inode *inode, loff_t endoff)
  2346. {
  2347. struct ceph_inode_info *ci = ceph_inode(inode);
  2348. int check = 0;
  2349. /* do we need to explicitly request a larger max_size? */
  2350. spin_lock(&ci->i_ceph_lock);
  2351. if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
  2352. dout("write %p at large endoff %llu, req max_size\n",
  2353. inode, endoff);
  2354. ci->i_wanted_max_size = endoff;
  2355. }
  2356. /* duplicate ceph_check_caps()'s logic */
  2357. if (ci->i_auth_cap &&
  2358. (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
  2359. ci->i_wanted_max_size > ci->i_max_size &&
  2360. ci->i_wanted_max_size > ci->i_requested_max_size)
  2361. check = 1;
  2362. spin_unlock(&ci->i_ceph_lock);
  2363. if (check)
  2364. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  2365. }
  2366. int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, int *got)
  2367. {
  2368. int ret, err = 0;
  2369. BUG_ON(need & ~CEPH_CAP_FILE_RD);
  2370. BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
  2371. ret = ceph_pool_perm_check(ci, need);
  2372. if (ret < 0)
  2373. return ret;
  2374. ret = try_get_cap_refs(ci, need, want, 0, true, got, &err);
  2375. if (ret) {
  2376. if (err == -EAGAIN) {
  2377. ret = 0;
  2378. } else if (err < 0) {
  2379. ret = err;
  2380. }
  2381. }
  2382. return ret;
  2383. }
  2384. /*
  2385. * Wait for caps, and take cap references. If we can't get a WR cap
  2386. * due to a small max_size, make sure we check_max_size (and possibly
  2387. * ask the mds) so we don't get hung up indefinitely.
  2388. */
  2389. int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
  2390. loff_t endoff, int *got, struct page **pinned_page)
  2391. {
  2392. int _got, ret, err = 0;
  2393. ret = ceph_pool_perm_check(ci, need);
  2394. if (ret < 0)
  2395. return ret;
  2396. while (true) {
  2397. if (endoff > 0)
  2398. check_max_size(&ci->vfs_inode, endoff);
  2399. err = 0;
  2400. _got = 0;
  2401. ret = try_get_cap_refs(ci, need, want, endoff,
  2402. false, &_got, &err);
  2403. if (ret) {
  2404. if (err == -EAGAIN)
  2405. continue;
  2406. if (err < 0)
  2407. ret = err;
  2408. } else {
  2409. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2410. add_wait_queue(&ci->i_cap_wq, &wait);
  2411. while (!try_get_cap_refs(ci, need, want, endoff,
  2412. true, &_got, &err)) {
  2413. if (signal_pending(current)) {
  2414. ret = -ERESTARTSYS;
  2415. break;
  2416. }
  2417. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  2418. }
  2419. remove_wait_queue(&ci->i_cap_wq, &wait);
  2420. if (err == -EAGAIN)
  2421. continue;
  2422. if (err < 0)
  2423. ret = err;
  2424. }
  2425. if (ret < 0) {
  2426. if (err == -ESTALE) {
  2427. /* session was killed, try renew caps */
  2428. ret = ceph_renew_caps(&ci->vfs_inode);
  2429. if (ret == 0)
  2430. continue;
  2431. }
  2432. return ret;
  2433. }
  2434. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  2435. (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
  2436. i_size_read(&ci->vfs_inode) > 0) {
  2437. struct page *page =
  2438. find_get_page(ci->vfs_inode.i_mapping, 0);
  2439. if (page) {
  2440. if (PageUptodate(page)) {
  2441. *pinned_page = page;
  2442. break;
  2443. }
  2444. put_page(page);
  2445. }
  2446. /*
  2447. * drop cap refs first because getattr while
  2448. * holding * caps refs can cause deadlock.
  2449. */
  2450. ceph_put_cap_refs(ci, _got);
  2451. _got = 0;
  2452. /*
  2453. * getattr request will bring inline data into
  2454. * page cache
  2455. */
  2456. ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
  2457. CEPH_STAT_CAP_INLINE_DATA,
  2458. true);
  2459. if (ret < 0)
  2460. return ret;
  2461. continue;
  2462. }
  2463. break;
  2464. }
  2465. if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
  2466. ceph_fscache_revalidate_cookie(ci);
  2467. *got = _got;
  2468. return 0;
  2469. }
  2470. /*
  2471. * Take cap refs. Caller must already know we hold at least one ref
  2472. * on the caps in question or we don't know this is safe.
  2473. */
  2474. void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
  2475. {
  2476. spin_lock(&ci->i_ceph_lock);
  2477. __take_cap_refs(ci, caps, false);
  2478. spin_unlock(&ci->i_ceph_lock);
  2479. }
  2480. /*
  2481. * drop cap_snap that is not associated with any snapshot.
  2482. * we don't need to send FLUSHSNAP message for it.
  2483. */
  2484. static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
  2485. struct ceph_cap_snap *capsnap)
  2486. {
  2487. if (!capsnap->need_flush &&
  2488. !capsnap->writing && !capsnap->dirty_pages) {
  2489. dout("dropping cap_snap %p follows %llu\n",
  2490. capsnap, capsnap->follows);
  2491. BUG_ON(capsnap->cap_flush.tid > 0);
  2492. ceph_put_snap_context(capsnap->context);
  2493. if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
  2494. ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  2495. list_del(&capsnap->ci_item);
  2496. ceph_put_cap_snap(capsnap);
  2497. return 1;
  2498. }
  2499. return 0;
  2500. }
  2501. /*
  2502. * Release cap refs.
  2503. *
  2504. * If we released the last ref on any given cap, call ceph_check_caps
  2505. * to release (or schedule a release).
  2506. *
  2507. * If we are releasing a WR cap (from a sync write), finalize any affected
  2508. * cap_snap, and wake up any waiters.
  2509. */
  2510. void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
  2511. {
  2512. struct inode *inode = &ci->vfs_inode;
  2513. int last = 0, put = 0, flushsnaps = 0, wake = 0;
  2514. spin_lock(&ci->i_ceph_lock);
  2515. if (had & CEPH_CAP_PIN)
  2516. --ci->i_pin_ref;
  2517. if (had & CEPH_CAP_FILE_RD)
  2518. if (--ci->i_rd_ref == 0)
  2519. last++;
  2520. if (had & CEPH_CAP_FILE_CACHE)
  2521. if (--ci->i_rdcache_ref == 0)
  2522. last++;
  2523. if (had & CEPH_CAP_FILE_BUFFER) {
  2524. if (--ci->i_wb_ref == 0) {
  2525. last++;
  2526. put++;
  2527. }
  2528. dout("put_cap_refs %p wb %d -> %d (?)\n",
  2529. inode, ci->i_wb_ref+1, ci->i_wb_ref);
  2530. }
  2531. if (had & CEPH_CAP_FILE_WR)
  2532. if (--ci->i_wr_ref == 0) {
  2533. last++;
  2534. if (__ceph_have_pending_cap_snap(ci)) {
  2535. struct ceph_cap_snap *capsnap =
  2536. list_last_entry(&ci->i_cap_snaps,
  2537. struct ceph_cap_snap,
  2538. ci_item);
  2539. capsnap->writing = 0;
  2540. if (ceph_try_drop_cap_snap(ci, capsnap))
  2541. put++;
  2542. else if (__ceph_finish_cap_snap(ci, capsnap))
  2543. flushsnaps = 1;
  2544. wake = 1;
  2545. }
  2546. if (ci->i_wrbuffer_ref_head == 0 &&
  2547. ci->i_dirty_caps == 0 &&
  2548. ci->i_flushing_caps == 0) {
  2549. BUG_ON(!ci->i_head_snapc);
  2550. ceph_put_snap_context(ci->i_head_snapc);
  2551. ci->i_head_snapc = NULL;
  2552. }
  2553. /* see comment in __ceph_remove_cap() */
  2554. if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
  2555. drop_inode_snap_realm(ci);
  2556. }
  2557. spin_unlock(&ci->i_ceph_lock);
  2558. dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
  2559. last ? " last" : "", put ? " put" : "");
  2560. if (last && !flushsnaps)
  2561. ceph_check_caps(ci, 0, NULL);
  2562. else if (flushsnaps)
  2563. ceph_flush_snaps(ci, NULL);
  2564. if (wake)
  2565. wake_up_all(&ci->i_cap_wq);
  2566. while (put-- > 0)
  2567. iput(inode);
  2568. }
  2569. /*
  2570. * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
  2571. * context. Adjust per-snap dirty page accounting as appropriate.
  2572. * Once all dirty data for a cap_snap is flushed, flush snapped file
  2573. * metadata back to the MDS. If we dropped the last ref, call
  2574. * ceph_check_caps.
  2575. */
  2576. void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
  2577. struct ceph_snap_context *snapc)
  2578. {
  2579. struct inode *inode = &ci->vfs_inode;
  2580. struct ceph_cap_snap *capsnap = NULL;
  2581. int put = 0;
  2582. bool last = false;
  2583. bool found = false;
  2584. bool flush_snaps = false;
  2585. bool complete_capsnap = false;
  2586. spin_lock(&ci->i_ceph_lock);
  2587. ci->i_wrbuffer_ref -= nr;
  2588. if (ci->i_wrbuffer_ref == 0) {
  2589. last = true;
  2590. put++;
  2591. }
  2592. if (ci->i_head_snapc == snapc) {
  2593. ci->i_wrbuffer_ref_head -= nr;
  2594. if (ci->i_wrbuffer_ref_head == 0 &&
  2595. ci->i_wr_ref == 0 &&
  2596. ci->i_dirty_caps == 0 &&
  2597. ci->i_flushing_caps == 0) {
  2598. BUG_ON(!ci->i_head_snapc);
  2599. ceph_put_snap_context(ci->i_head_snapc);
  2600. ci->i_head_snapc = NULL;
  2601. }
  2602. dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
  2603. inode,
  2604. ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
  2605. ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  2606. last ? " LAST" : "");
  2607. } else {
  2608. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  2609. if (capsnap->context == snapc) {
  2610. found = true;
  2611. break;
  2612. }
  2613. }
  2614. BUG_ON(!found);
  2615. capsnap->dirty_pages -= nr;
  2616. if (capsnap->dirty_pages == 0) {
  2617. complete_capsnap = true;
  2618. if (!capsnap->writing) {
  2619. if (ceph_try_drop_cap_snap(ci, capsnap)) {
  2620. put++;
  2621. } else {
  2622. ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  2623. flush_snaps = true;
  2624. }
  2625. }
  2626. }
  2627. dout("put_wrbuffer_cap_refs on %p cap_snap %p "
  2628. " snap %lld %d/%d -> %d/%d %s%s\n",
  2629. inode, capsnap, capsnap->context->seq,
  2630. ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
  2631. ci->i_wrbuffer_ref, capsnap->dirty_pages,
  2632. last ? " (wrbuffer last)" : "",
  2633. complete_capsnap ? " (complete capsnap)" : "");
  2634. }
  2635. spin_unlock(&ci->i_ceph_lock);
  2636. if (last) {
  2637. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  2638. } else if (flush_snaps) {
  2639. ceph_flush_snaps(ci, NULL);
  2640. }
  2641. if (complete_capsnap)
  2642. wake_up_all(&ci->i_cap_wq);
  2643. while (put-- > 0)
  2644. iput(inode);
  2645. }
  2646. /*
  2647. * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
  2648. */
  2649. static void invalidate_aliases(struct inode *inode)
  2650. {
  2651. struct dentry *dn, *prev = NULL;
  2652. dout("invalidate_aliases inode %p\n", inode);
  2653. d_prune_aliases(inode);
  2654. /*
  2655. * For non-directory inode, d_find_alias() only returns
  2656. * hashed dentry. After calling d_invalidate(), the
  2657. * dentry becomes unhashed.
  2658. *
  2659. * For directory inode, d_find_alias() can return
  2660. * unhashed dentry. But directory inode should have
  2661. * one alias at most.
  2662. */
  2663. while ((dn = d_find_alias(inode))) {
  2664. if (dn == prev) {
  2665. dput(dn);
  2666. break;
  2667. }
  2668. d_invalidate(dn);
  2669. if (prev)
  2670. dput(prev);
  2671. prev = dn;
  2672. }
  2673. if (prev)
  2674. dput(prev);
  2675. }
  2676. struct cap_extra_info {
  2677. struct ceph_string *pool_ns;
  2678. /* inline data */
  2679. u64 inline_version;
  2680. void *inline_data;
  2681. u32 inline_len;
  2682. /* dirstat */
  2683. bool dirstat_valid;
  2684. u64 nfiles;
  2685. u64 nsubdirs;
  2686. /* currently issued */
  2687. int issued;
  2688. };
  2689. /*
  2690. * Handle a cap GRANT message from the MDS. (Note that a GRANT may
  2691. * actually be a revocation if it specifies a smaller cap set.)
  2692. *
  2693. * caller holds s_mutex and i_ceph_lock, we drop both.
  2694. */
  2695. static void handle_cap_grant(struct inode *inode,
  2696. struct ceph_mds_session *session,
  2697. struct ceph_cap *cap,
  2698. struct ceph_mds_caps *grant,
  2699. struct ceph_buffer *xattr_buf,
  2700. struct cap_extra_info *extra_info)
  2701. __releases(ci->i_ceph_lock)
  2702. __releases(session->s_mdsc->snap_rwsem)
  2703. {
  2704. struct ceph_inode_info *ci = ceph_inode(inode);
  2705. int seq = le32_to_cpu(grant->seq);
  2706. int newcaps = le32_to_cpu(grant->caps);
  2707. int used, wanted, dirty;
  2708. u64 size = le64_to_cpu(grant->size);
  2709. u64 max_size = le64_to_cpu(grant->max_size);
  2710. int check_caps = 0;
  2711. bool wake = false;
  2712. bool writeback = false;
  2713. bool queue_trunc = false;
  2714. bool queue_invalidate = false;
  2715. bool deleted_inode = false;
  2716. bool fill_inline = false;
  2717. dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
  2718. inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
  2719. dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
  2720. inode->i_size);
  2721. /*
  2722. * auth mds of the inode changed. we received the cap export message,
  2723. * but still haven't received the cap import message. handle_cap_export
  2724. * updated the new auth MDS' cap.
  2725. *
  2726. * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
  2727. * that was sent before the cap import message. So don't remove caps.
  2728. */
  2729. if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  2730. WARN_ON(cap != ci->i_auth_cap);
  2731. WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
  2732. seq = cap->seq;
  2733. newcaps |= cap->issued;
  2734. }
  2735. /*
  2736. * If CACHE is being revoked, and we have no dirty buffers,
  2737. * try to invalidate (once). (If there are dirty buffers, we
  2738. * will invalidate _after_ writeback.)
  2739. */
  2740. if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
  2741. ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
  2742. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  2743. !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
  2744. if (try_nonblocking_invalidate(inode)) {
  2745. /* there were locked pages.. invalidate later
  2746. in a separate thread. */
  2747. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  2748. queue_invalidate = true;
  2749. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  2750. }
  2751. }
  2752. }
  2753. /* side effects now are allowed */
  2754. cap->cap_gen = session->s_cap_gen;
  2755. cap->seq = seq;
  2756. __check_cap_issue(ci, cap, newcaps);
  2757. if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
  2758. (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
  2759. inode->i_mode = le32_to_cpu(grant->mode);
  2760. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
  2761. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
  2762. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  2763. from_kuid(&init_user_ns, inode->i_uid),
  2764. from_kgid(&init_user_ns, inode->i_gid));
  2765. }
  2766. if ((newcaps & CEPH_CAP_LINK_SHARED) &&
  2767. (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
  2768. set_nlink(inode, le32_to_cpu(grant->nlink));
  2769. if (inode->i_nlink == 0 &&
  2770. (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
  2771. deleted_inode = true;
  2772. }
  2773. if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
  2774. grant->xattr_len) {
  2775. int len = le32_to_cpu(grant->xattr_len);
  2776. u64 version = le64_to_cpu(grant->xattr_version);
  2777. if (version > ci->i_xattrs.version) {
  2778. dout(" got new xattrs v%llu on %p len %d\n",
  2779. version, inode, len);
  2780. if (ci->i_xattrs.blob)
  2781. ceph_buffer_put(ci->i_xattrs.blob);
  2782. ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
  2783. ci->i_xattrs.version = version;
  2784. ceph_forget_all_cached_acls(inode);
  2785. }
  2786. }
  2787. if (newcaps & CEPH_CAP_ANY_RD) {
  2788. struct timespec64 mtime, atime, ctime;
  2789. /* ctime/mtime/atime? */
  2790. ceph_decode_timespec64(&mtime, &grant->mtime);
  2791. ceph_decode_timespec64(&atime, &grant->atime);
  2792. ceph_decode_timespec64(&ctime, &grant->ctime);
  2793. ceph_fill_file_time(inode, extra_info->issued,
  2794. le32_to_cpu(grant->time_warp_seq),
  2795. &ctime, &mtime, &atime);
  2796. }
  2797. if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
  2798. ci->i_files = extra_info->nfiles;
  2799. ci->i_subdirs = extra_info->nsubdirs;
  2800. }
  2801. if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
  2802. /* file layout may have changed */
  2803. s64 old_pool = ci->i_layout.pool_id;
  2804. struct ceph_string *old_ns;
  2805. ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
  2806. old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
  2807. lockdep_is_held(&ci->i_ceph_lock));
  2808. rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
  2809. if (ci->i_layout.pool_id != old_pool ||
  2810. extra_info->pool_ns != old_ns)
  2811. ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
  2812. extra_info->pool_ns = old_ns;
  2813. /* size/truncate_seq? */
  2814. queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
  2815. le32_to_cpu(grant->truncate_seq),
  2816. le64_to_cpu(grant->truncate_size),
  2817. size);
  2818. }
  2819. if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
  2820. if (max_size != ci->i_max_size) {
  2821. dout("max_size %lld -> %llu\n",
  2822. ci->i_max_size, max_size);
  2823. ci->i_max_size = max_size;
  2824. if (max_size >= ci->i_wanted_max_size) {
  2825. ci->i_wanted_max_size = 0; /* reset */
  2826. ci->i_requested_max_size = 0;
  2827. }
  2828. wake = true;
  2829. } else if (ci->i_wanted_max_size > ci->i_max_size &&
  2830. ci->i_wanted_max_size > ci->i_requested_max_size) {
  2831. /* CEPH_CAP_OP_IMPORT */
  2832. wake = true;
  2833. }
  2834. }
  2835. /* check cap bits */
  2836. wanted = __ceph_caps_wanted(ci);
  2837. used = __ceph_caps_used(ci);
  2838. dirty = __ceph_caps_dirty(ci);
  2839. dout(" my wanted = %s, used = %s, dirty %s\n",
  2840. ceph_cap_string(wanted),
  2841. ceph_cap_string(used),
  2842. ceph_cap_string(dirty));
  2843. if (wanted != le32_to_cpu(grant->wanted)) {
  2844. dout("mds wanted %s -> %s\n",
  2845. ceph_cap_string(le32_to_cpu(grant->wanted)),
  2846. ceph_cap_string(wanted));
  2847. /* imported cap may not have correct mds_wanted */
  2848. if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
  2849. check_caps = 1;
  2850. }
  2851. /* revocation, grant, or no-op? */
  2852. if (cap->issued & ~newcaps) {
  2853. int revoking = cap->issued & ~newcaps;
  2854. dout("revocation: %s -> %s (revoking %s)\n",
  2855. ceph_cap_string(cap->issued),
  2856. ceph_cap_string(newcaps),
  2857. ceph_cap_string(revoking));
  2858. if (revoking & used & CEPH_CAP_FILE_BUFFER)
  2859. writeback = true; /* initiate writeback; will delay ack */
  2860. else if (revoking == CEPH_CAP_FILE_CACHE &&
  2861. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  2862. queue_invalidate)
  2863. ; /* do nothing yet, invalidation will be queued */
  2864. else if (cap == ci->i_auth_cap)
  2865. check_caps = 1; /* check auth cap only */
  2866. else
  2867. check_caps = 2; /* check all caps */
  2868. cap->issued = newcaps;
  2869. cap->implemented |= newcaps;
  2870. } else if (cap->issued == newcaps) {
  2871. dout("caps unchanged: %s -> %s\n",
  2872. ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
  2873. } else {
  2874. dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
  2875. ceph_cap_string(newcaps));
  2876. /* non-auth MDS is revoking the newly grant caps ? */
  2877. if (cap == ci->i_auth_cap &&
  2878. __ceph_caps_revoking_other(ci, cap, newcaps))
  2879. check_caps = 2;
  2880. cap->issued = newcaps;
  2881. cap->implemented |= newcaps; /* add bits only, to
  2882. * avoid stepping on a
  2883. * pending revocation */
  2884. wake = true;
  2885. }
  2886. BUG_ON(cap->issued & ~cap->implemented);
  2887. if (extra_info->inline_version > 0 &&
  2888. extra_info->inline_version >= ci->i_inline_version) {
  2889. ci->i_inline_version = extra_info->inline_version;
  2890. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  2891. (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
  2892. fill_inline = true;
  2893. }
  2894. if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
  2895. if (newcaps & ~extra_info->issued)
  2896. wake = true;
  2897. kick_flushing_inode_caps(session->s_mdsc, session, inode);
  2898. up_read(&session->s_mdsc->snap_rwsem);
  2899. } else {
  2900. spin_unlock(&ci->i_ceph_lock);
  2901. }
  2902. if (fill_inline)
  2903. ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
  2904. extra_info->inline_len);
  2905. if (queue_trunc)
  2906. ceph_queue_vmtruncate(inode);
  2907. if (writeback)
  2908. /*
  2909. * queue inode for writeback: we can't actually call
  2910. * filemap_write_and_wait, etc. from message handler
  2911. * context.
  2912. */
  2913. ceph_queue_writeback(inode);
  2914. if (queue_invalidate)
  2915. ceph_queue_invalidate(inode);
  2916. if (deleted_inode)
  2917. invalidate_aliases(inode);
  2918. if (wake)
  2919. wake_up_all(&ci->i_cap_wq);
  2920. if (check_caps == 1)
  2921. ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
  2922. session);
  2923. else if (check_caps == 2)
  2924. ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
  2925. else
  2926. mutex_unlock(&session->s_mutex);
  2927. }
  2928. /*
  2929. * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
  2930. * MDS has been safely committed.
  2931. */
  2932. static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
  2933. struct ceph_mds_caps *m,
  2934. struct ceph_mds_session *session,
  2935. struct ceph_cap *cap)
  2936. __releases(ci->i_ceph_lock)
  2937. {
  2938. struct ceph_inode_info *ci = ceph_inode(inode);
  2939. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  2940. struct ceph_cap_flush *cf, *tmp_cf;
  2941. LIST_HEAD(to_remove);
  2942. unsigned seq = le32_to_cpu(m->seq);
  2943. int dirty = le32_to_cpu(m->dirty);
  2944. int cleaned = 0;
  2945. bool drop = false;
  2946. bool wake_ci = false;
  2947. bool wake_mdsc = false;
  2948. list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
  2949. if (cf->tid == flush_tid)
  2950. cleaned = cf->caps;
  2951. if (cf->caps == 0) /* capsnap */
  2952. continue;
  2953. if (cf->tid <= flush_tid) {
  2954. if (__finish_cap_flush(NULL, ci, cf))
  2955. wake_ci = true;
  2956. list_add_tail(&cf->i_list, &to_remove);
  2957. } else {
  2958. cleaned &= ~cf->caps;
  2959. if (!cleaned)
  2960. break;
  2961. }
  2962. }
  2963. dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
  2964. " flushing %s -> %s\n",
  2965. inode, session->s_mds, seq, ceph_cap_string(dirty),
  2966. ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
  2967. ceph_cap_string(ci->i_flushing_caps & ~cleaned));
  2968. if (list_empty(&to_remove) && !cleaned)
  2969. goto out;
  2970. ci->i_flushing_caps &= ~cleaned;
  2971. spin_lock(&mdsc->cap_dirty_lock);
  2972. list_for_each_entry(cf, &to_remove, i_list) {
  2973. if (__finish_cap_flush(mdsc, NULL, cf))
  2974. wake_mdsc = true;
  2975. }
  2976. if (ci->i_flushing_caps == 0) {
  2977. if (list_empty(&ci->i_cap_flush_list)) {
  2978. list_del_init(&ci->i_flushing_item);
  2979. if (!list_empty(&session->s_cap_flushing)) {
  2980. dout(" mds%d still flushing cap on %p\n",
  2981. session->s_mds,
  2982. &list_first_entry(&session->s_cap_flushing,
  2983. struct ceph_inode_info,
  2984. i_flushing_item)->vfs_inode);
  2985. }
  2986. }
  2987. mdsc->num_cap_flushing--;
  2988. dout(" inode %p now !flushing\n", inode);
  2989. if (ci->i_dirty_caps == 0) {
  2990. dout(" inode %p now clean\n", inode);
  2991. BUG_ON(!list_empty(&ci->i_dirty_item));
  2992. drop = true;
  2993. if (ci->i_wr_ref == 0 &&
  2994. ci->i_wrbuffer_ref_head == 0) {
  2995. BUG_ON(!ci->i_head_snapc);
  2996. ceph_put_snap_context(ci->i_head_snapc);
  2997. ci->i_head_snapc = NULL;
  2998. }
  2999. } else {
  3000. BUG_ON(list_empty(&ci->i_dirty_item));
  3001. }
  3002. }
  3003. spin_unlock(&mdsc->cap_dirty_lock);
  3004. out:
  3005. spin_unlock(&ci->i_ceph_lock);
  3006. while (!list_empty(&to_remove)) {
  3007. cf = list_first_entry(&to_remove,
  3008. struct ceph_cap_flush, i_list);
  3009. list_del(&cf->i_list);
  3010. ceph_free_cap_flush(cf);
  3011. }
  3012. if (wake_ci)
  3013. wake_up_all(&ci->i_cap_wq);
  3014. if (wake_mdsc)
  3015. wake_up_all(&mdsc->cap_flushing_wq);
  3016. if (drop)
  3017. iput(inode);
  3018. }
  3019. /*
  3020. * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
  3021. * throw away our cap_snap.
  3022. *
  3023. * Caller hold s_mutex.
  3024. */
  3025. static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
  3026. struct ceph_mds_caps *m,
  3027. struct ceph_mds_session *session)
  3028. {
  3029. struct ceph_inode_info *ci = ceph_inode(inode);
  3030. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  3031. u64 follows = le64_to_cpu(m->snap_follows);
  3032. struct ceph_cap_snap *capsnap;
  3033. bool flushed = false;
  3034. bool wake_ci = false;
  3035. bool wake_mdsc = false;
  3036. dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
  3037. inode, ci, session->s_mds, follows);
  3038. spin_lock(&ci->i_ceph_lock);
  3039. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  3040. if (capsnap->follows == follows) {
  3041. if (capsnap->cap_flush.tid != flush_tid) {
  3042. dout(" cap_snap %p follows %lld tid %lld !="
  3043. " %lld\n", capsnap, follows,
  3044. flush_tid, capsnap->cap_flush.tid);
  3045. break;
  3046. }
  3047. flushed = true;
  3048. break;
  3049. } else {
  3050. dout(" skipping cap_snap %p follows %lld\n",
  3051. capsnap, capsnap->follows);
  3052. }
  3053. }
  3054. if (flushed) {
  3055. WARN_ON(capsnap->dirty_pages || capsnap->writing);
  3056. dout(" removing %p cap_snap %p follows %lld\n",
  3057. inode, capsnap, follows);
  3058. list_del(&capsnap->ci_item);
  3059. if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
  3060. wake_ci = true;
  3061. spin_lock(&mdsc->cap_dirty_lock);
  3062. if (list_empty(&ci->i_cap_flush_list))
  3063. list_del_init(&ci->i_flushing_item);
  3064. if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
  3065. wake_mdsc = true;
  3066. spin_unlock(&mdsc->cap_dirty_lock);
  3067. }
  3068. spin_unlock(&ci->i_ceph_lock);
  3069. if (flushed) {
  3070. ceph_put_snap_context(capsnap->context);
  3071. ceph_put_cap_snap(capsnap);
  3072. if (wake_ci)
  3073. wake_up_all(&ci->i_cap_wq);
  3074. if (wake_mdsc)
  3075. wake_up_all(&mdsc->cap_flushing_wq);
  3076. iput(inode);
  3077. }
  3078. }
  3079. /*
  3080. * Handle TRUNC from MDS, indicating file truncation.
  3081. *
  3082. * caller hold s_mutex.
  3083. */
  3084. static void handle_cap_trunc(struct inode *inode,
  3085. struct ceph_mds_caps *trunc,
  3086. struct ceph_mds_session *session)
  3087. __releases(ci->i_ceph_lock)
  3088. {
  3089. struct ceph_inode_info *ci = ceph_inode(inode);
  3090. int mds = session->s_mds;
  3091. int seq = le32_to_cpu(trunc->seq);
  3092. u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
  3093. u64 truncate_size = le64_to_cpu(trunc->truncate_size);
  3094. u64 size = le64_to_cpu(trunc->size);
  3095. int implemented = 0;
  3096. int dirty = __ceph_caps_dirty(ci);
  3097. int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
  3098. int queue_trunc = 0;
  3099. issued |= implemented | dirty;
  3100. dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
  3101. inode, mds, seq, truncate_size, truncate_seq);
  3102. queue_trunc = ceph_fill_file_size(inode, issued,
  3103. truncate_seq, truncate_size, size);
  3104. spin_unlock(&ci->i_ceph_lock);
  3105. if (queue_trunc)
  3106. ceph_queue_vmtruncate(inode);
  3107. }
  3108. /*
  3109. * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
  3110. * different one. If we are the most recent migration we've seen (as
  3111. * indicated by mseq), make note of the migrating cap bits for the
  3112. * duration (until we see the corresponding IMPORT).
  3113. *
  3114. * caller holds s_mutex
  3115. */
  3116. static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
  3117. struct ceph_mds_cap_peer *ph,
  3118. struct ceph_mds_session *session)
  3119. {
  3120. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  3121. struct ceph_mds_session *tsession = NULL;
  3122. struct ceph_cap *cap, *tcap, *new_cap = NULL;
  3123. struct ceph_inode_info *ci = ceph_inode(inode);
  3124. u64 t_cap_id;
  3125. unsigned mseq = le32_to_cpu(ex->migrate_seq);
  3126. unsigned t_seq, t_mseq;
  3127. int target, issued;
  3128. int mds = session->s_mds;
  3129. if (ph) {
  3130. t_cap_id = le64_to_cpu(ph->cap_id);
  3131. t_seq = le32_to_cpu(ph->seq);
  3132. t_mseq = le32_to_cpu(ph->mseq);
  3133. target = le32_to_cpu(ph->mds);
  3134. } else {
  3135. t_cap_id = t_seq = t_mseq = 0;
  3136. target = -1;
  3137. }
  3138. dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
  3139. inode, ci, mds, mseq, target);
  3140. retry:
  3141. spin_lock(&ci->i_ceph_lock);
  3142. cap = __get_cap_for_mds(ci, mds);
  3143. if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
  3144. goto out_unlock;
  3145. if (target < 0) {
  3146. __ceph_remove_cap(cap, false);
  3147. if (!ci->i_auth_cap)
  3148. ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
  3149. goto out_unlock;
  3150. }
  3151. /*
  3152. * now we know we haven't received the cap import message yet
  3153. * because the exported cap still exist.
  3154. */
  3155. issued = cap->issued;
  3156. if (issued != cap->implemented)
  3157. pr_err_ratelimited("handle_cap_export: issued != implemented: "
  3158. "ino (%llx.%llx) mds%d seq %d mseq %d "
  3159. "issued %s implemented %s\n",
  3160. ceph_vinop(inode), mds, cap->seq, cap->mseq,
  3161. ceph_cap_string(issued),
  3162. ceph_cap_string(cap->implemented));
  3163. tcap = __get_cap_for_mds(ci, target);
  3164. if (tcap) {
  3165. /* already have caps from the target */
  3166. if (tcap->cap_id == t_cap_id &&
  3167. ceph_seq_cmp(tcap->seq, t_seq) < 0) {
  3168. dout(" updating import cap %p mds%d\n", tcap, target);
  3169. tcap->cap_id = t_cap_id;
  3170. tcap->seq = t_seq - 1;
  3171. tcap->issue_seq = t_seq - 1;
  3172. tcap->issued |= issued;
  3173. tcap->implemented |= issued;
  3174. if (cap == ci->i_auth_cap)
  3175. ci->i_auth_cap = tcap;
  3176. if (!list_empty(&ci->i_cap_flush_list) &&
  3177. ci->i_auth_cap == tcap) {
  3178. spin_lock(&mdsc->cap_dirty_lock);
  3179. list_move_tail(&ci->i_flushing_item,
  3180. &tcap->session->s_cap_flushing);
  3181. spin_unlock(&mdsc->cap_dirty_lock);
  3182. }
  3183. }
  3184. __ceph_remove_cap(cap, false);
  3185. goto out_unlock;
  3186. } else if (tsession) {
  3187. /* add placeholder for the export tagert */
  3188. int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
  3189. tcap = new_cap;
  3190. ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
  3191. t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
  3192. if (!list_empty(&ci->i_cap_flush_list) &&
  3193. ci->i_auth_cap == tcap) {
  3194. spin_lock(&mdsc->cap_dirty_lock);
  3195. list_move_tail(&ci->i_flushing_item,
  3196. &tcap->session->s_cap_flushing);
  3197. spin_unlock(&mdsc->cap_dirty_lock);
  3198. }
  3199. __ceph_remove_cap(cap, false);
  3200. goto out_unlock;
  3201. }
  3202. spin_unlock(&ci->i_ceph_lock);
  3203. mutex_unlock(&session->s_mutex);
  3204. /* open target session */
  3205. tsession = ceph_mdsc_open_export_target_session(mdsc, target);
  3206. if (!IS_ERR(tsession)) {
  3207. if (mds > target) {
  3208. mutex_lock(&session->s_mutex);
  3209. mutex_lock_nested(&tsession->s_mutex,
  3210. SINGLE_DEPTH_NESTING);
  3211. } else {
  3212. mutex_lock(&tsession->s_mutex);
  3213. mutex_lock_nested(&session->s_mutex,
  3214. SINGLE_DEPTH_NESTING);
  3215. }
  3216. new_cap = ceph_get_cap(mdsc, NULL);
  3217. } else {
  3218. WARN_ON(1);
  3219. tsession = NULL;
  3220. target = -1;
  3221. }
  3222. goto retry;
  3223. out_unlock:
  3224. spin_unlock(&ci->i_ceph_lock);
  3225. mutex_unlock(&session->s_mutex);
  3226. if (tsession) {
  3227. mutex_unlock(&tsession->s_mutex);
  3228. ceph_put_mds_session(tsession);
  3229. }
  3230. if (new_cap)
  3231. ceph_put_cap(mdsc, new_cap);
  3232. }
  3233. /*
  3234. * Handle cap IMPORT.
  3235. *
  3236. * caller holds s_mutex. acquires i_ceph_lock
  3237. */
  3238. static void handle_cap_import(struct ceph_mds_client *mdsc,
  3239. struct inode *inode, struct ceph_mds_caps *im,
  3240. struct ceph_mds_cap_peer *ph,
  3241. struct ceph_mds_session *session,
  3242. struct ceph_cap **target_cap, int *old_issued)
  3243. __acquires(ci->i_ceph_lock)
  3244. {
  3245. struct ceph_inode_info *ci = ceph_inode(inode);
  3246. struct ceph_cap *cap, *ocap, *new_cap = NULL;
  3247. int mds = session->s_mds;
  3248. int issued;
  3249. unsigned caps = le32_to_cpu(im->caps);
  3250. unsigned wanted = le32_to_cpu(im->wanted);
  3251. unsigned seq = le32_to_cpu(im->seq);
  3252. unsigned mseq = le32_to_cpu(im->migrate_seq);
  3253. u64 realmino = le64_to_cpu(im->realm);
  3254. u64 cap_id = le64_to_cpu(im->cap_id);
  3255. u64 p_cap_id;
  3256. int peer;
  3257. if (ph) {
  3258. p_cap_id = le64_to_cpu(ph->cap_id);
  3259. peer = le32_to_cpu(ph->mds);
  3260. } else {
  3261. p_cap_id = 0;
  3262. peer = -1;
  3263. }
  3264. dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
  3265. inode, ci, mds, mseq, peer);
  3266. retry:
  3267. spin_lock(&ci->i_ceph_lock);
  3268. cap = __get_cap_for_mds(ci, mds);
  3269. if (!cap) {
  3270. if (!new_cap) {
  3271. spin_unlock(&ci->i_ceph_lock);
  3272. new_cap = ceph_get_cap(mdsc, NULL);
  3273. goto retry;
  3274. }
  3275. cap = new_cap;
  3276. } else {
  3277. if (new_cap) {
  3278. ceph_put_cap(mdsc, new_cap);
  3279. new_cap = NULL;
  3280. }
  3281. }
  3282. __ceph_caps_issued(ci, &issued);
  3283. issued |= __ceph_caps_dirty(ci);
  3284. ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
  3285. realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
  3286. ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
  3287. if (ocap && ocap->cap_id == p_cap_id) {
  3288. dout(" remove export cap %p mds%d flags %d\n",
  3289. ocap, peer, ph->flags);
  3290. if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
  3291. (ocap->seq != le32_to_cpu(ph->seq) ||
  3292. ocap->mseq != le32_to_cpu(ph->mseq))) {
  3293. pr_err_ratelimited("handle_cap_import: "
  3294. "mismatched seq/mseq: ino (%llx.%llx) "
  3295. "mds%d seq %d mseq %d importer mds%d "
  3296. "has peer seq %d mseq %d\n",
  3297. ceph_vinop(inode), peer, ocap->seq,
  3298. ocap->mseq, mds, le32_to_cpu(ph->seq),
  3299. le32_to_cpu(ph->mseq));
  3300. }
  3301. __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
  3302. }
  3303. /* make sure we re-request max_size, if necessary */
  3304. ci->i_requested_max_size = 0;
  3305. *old_issued = issued;
  3306. *target_cap = cap;
  3307. }
  3308. /*
  3309. * Handle a caps message from the MDS.
  3310. *
  3311. * Identify the appropriate session, inode, and call the right handler
  3312. * based on the cap op.
  3313. */
  3314. void ceph_handle_caps(struct ceph_mds_session *session,
  3315. struct ceph_msg *msg)
  3316. {
  3317. struct ceph_mds_client *mdsc = session->s_mdsc;
  3318. struct inode *inode;
  3319. struct ceph_inode_info *ci;
  3320. struct ceph_cap *cap;
  3321. struct ceph_mds_caps *h;
  3322. struct ceph_mds_cap_peer *peer = NULL;
  3323. struct ceph_snap_realm *realm = NULL;
  3324. int op;
  3325. int msg_version = le16_to_cpu(msg->hdr.version);
  3326. u32 seq, mseq;
  3327. struct ceph_vino vino;
  3328. void *snaptrace;
  3329. size_t snaptrace_len;
  3330. void *p, *end;
  3331. struct cap_extra_info extra_info = {};
  3332. dout("handle_caps from mds%d\n", session->s_mds);
  3333. /* decode */
  3334. end = msg->front.iov_base + msg->front.iov_len;
  3335. if (msg->front.iov_len < sizeof(*h))
  3336. goto bad;
  3337. h = msg->front.iov_base;
  3338. op = le32_to_cpu(h->op);
  3339. vino.ino = le64_to_cpu(h->ino);
  3340. vino.snap = CEPH_NOSNAP;
  3341. seq = le32_to_cpu(h->seq);
  3342. mseq = le32_to_cpu(h->migrate_seq);
  3343. snaptrace = h + 1;
  3344. snaptrace_len = le32_to_cpu(h->snap_trace_len);
  3345. p = snaptrace + snaptrace_len;
  3346. if (msg_version >= 2) {
  3347. u32 flock_len;
  3348. ceph_decode_32_safe(&p, end, flock_len, bad);
  3349. if (p + flock_len > end)
  3350. goto bad;
  3351. p += flock_len;
  3352. }
  3353. if (msg_version >= 3) {
  3354. if (op == CEPH_CAP_OP_IMPORT) {
  3355. if (p + sizeof(*peer) > end)
  3356. goto bad;
  3357. peer = p;
  3358. p += sizeof(*peer);
  3359. } else if (op == CEPH_CAP_OP_EXPORT) {
  3360. /* recorded in unused fields */
  3361. peer = (void *)&h->size;
  3362. }
  3363. }
  3364. if (msg_version >= 4) {
  3365. ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
  3366. ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
  3367. if (p + extra_info.inline_len > end)
  3368. goto bad;
  3369. extra_info.inline_data = p;
  3370. p += extra_info.inline_len;
  3371. }
  3372. if (msg_version >= 5) {
  3373. struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
  3374. u32 epoch_barrier;
  3375. ceph_decode_32_safe(&p, end, epoch_barrier, bad);
  3376. ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
  3377. }
  3378. if (msg_version >= 8) {
  3379. u64 flush_tid;
  3380. u32 caller_uid, caller_gid;
  3381. u32 pool_ns_len;
  3382. /* version >= 6 */
  3383. ceph_decode_64_safe(&p, end, flush_tid, bad);
  3384. /* version >= 7 */
  3385. ceph_decode_32_safe(&p, end, caller_uid, bad);
  3386. ceph_decode_32_safe(&p, end, caller_gid, bad);
  3387. /* version >= 8 */
  3388. ceph_decode_32_safe(&p, end, pool_ns_len, bad);
  3389. if (pool_ns_len > 0) {
  3390. ceph_decode_need(&p, end, pool_ns_len, bad);
  3391. extra_info.pool_ns =
  3392. ceph_find_or_create_string(p, pool_ns_len);
  3393. p += pool_ns_len;
  3394. }
  3395. }
  3396. if (msg_version >= 11) {
  3397. struct ceph_timespec *btime;
  3398. u64 change_attr;
  3399. u32 flags;
  3400. /* version >= 9 */
  3401. if (p + sizeof(*btime) > end)
  3402. goto bad;
  3403. btime = p;
  3404. p += sizeof(*btime);
  3405. ceph_decode_64_safe(&p, end, change_attr, bad);
  3406. /* version >= 10 */
  3407. ceph_decode_32_safe(&p, end, flags, bad);
  3408. /* version >= 11 */
  3409. extra_info.dirstat_valid = true;
  3410. ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
  3411. ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
  3412. }
  3413. /* lookup ino */
  3414. inode = ceph_find_inode(mdsc->fsc->sb, vino);
  3415. ci = ceph_inode(inode);
  3416. dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
  3417. vino.snap, inode);
  3418. mutex_lock(&session->s_mutex);
  3419. session->s_seq++;
  3420. dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
  3421. (unsigned)seq);
  3422. if (!inode) {
  3423. dout(" i don't have ino %llx\n", vino.ino);
  3424. if (op == CEPH_CAP_OP_IMPORT) {
  3425. cap = ceph_get_cap(mdsc, NULL);
  3426. cap->cap_ino = vino.ino;
  3427. cap->queue_release = 1;
  3428. cap->cap_id = le64_to_cpu(h->cap_id);
  3429. cap->mseq = mseq;
  3430. cap->seq = seq;
  3431. cap->issue_seq = seq;
  3432. spin_lock(&session->s_cap_lock);
  3433. list_add_tail(&cap->session_caps,
  3434. &session->s_cap_releases);
  3435. session->s_num_cap_releases++;
  3436. spin_unlock(&session->s_cap_lock);
  3437. }
  3438. goto flush_cap_releases;
  3439. }
  3440. /* these will work even if we don't have a cap yet */
  3441. switch (op) {
  3442. case CEPH_CAP_OP_FLUSHSNAP_ACK:
  3443. handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
  3444. h, session);
  3445. goto done;
  3446. case CEPH_CAP_OP_EXPORT:
  3447. handle_cap_export(inode, h, peer, session);
  3448. goto done_unlocked;
  3449. case CEPH_CAP_OP_IMPORT:
  3450. realm = NULL;
  3451. if (snaptrace_len) {
  3452. down_write(&mdsc->snap_rwsem);
  3453. ceph_update_snap_trace(mdsc, snaptrace,
  3454. snaptrace + snaptrace_len,
  3455. false, &realm);
  3456. downgrade_write(&mdsc->snap_rwsem);
  3457. } else {
  3458. down_read(&mdsc->snap_rwsem);
  3459. }
  3460. handle_cap_import(mdsc, inode, h, peer, session,
  3461. &cap, &extra_info.issued);
  3462. handle_cap_grant(inode, session, cap,
  3463. h, msg->middle, &extra_info);
  3464. if (realm)
  3465. ceph_put_snap_realm(mdsc, realm);
  3466. goto done_unlocked;
  3467. }
  3468. /* the rest require a cap */
  3469. spin_lock(&ci->i_ceph_lock);
  3470. cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
  3471. if (!cap) {
  3472. dout(" no cap on %p ino %llx.%llx from mds%d\n",
  3473. inode, ceph_ino(inode), ceph_snap(inode),
  3474. session->s_mds);
  3475. spin_unlock(&ci->i_ceph_lock);
  3476. goto flush_cap_releases;
  3477. }
  3478. /* note that each of these drops i_ceph_lock for us */
  3479. switch (op) {
  3480. case CEPH_CAP_OP_REVOKE:
  3481. case CEPH_CAP_OP_GRANT:
  3482. __ceph_caps_issued(ci, &extra_info.issued);
  3483. extra_info.issued |= __ceph_caps_dirty(ci);
  3484. handle_cap_grant(inode, session, cap,
  3485. h, msg->middle, &extra_info);
  3486. goto done_unlocked;
  3487. case CEPH_CAP_OP_FLUSH_ACK:
  3488. handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
  3489. h, session, cap);
  3490. break;
  3491. case CEPH_CAP_OP_TRUNC:
  3492. handle_cap_trunc(inode, h, session);
  3493. break;
  3494. default:
  3495. spin_unlock(&ci->i_ceph_lock);
  3496. pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
  3497. ceph_cap_op_name(op));
  3498. }
  3499. goto done;
  3500. flush_cap_releases:
  3501. /*
  3502. * send any cap release message to try to move things
  3503. * along for the mds (who clearly thinks we still have this
  3504. * cap).
  3505. */
  3506. ceph_send_cap_releases(mdsc, session);
  3507. done:
  3508. mutex_unlock(&session->s_mutex);
  3509. done_unlocked:
  3510. iput(inode);
  3511. ceph_put_string(extra_info.pool_ns);
  3512. return;
  3513. bad:
  3514. pr_err("ceph_handle_caps: corrupt message\n");
  3515. ceph_msg_dump(msg);
  3516. return;
  3517. }
  3518. /*
  3519. * Delayed work handler to process end of delayed cap release LRU list.
  3520. */
  3521. void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
  3522. {
  3523. struct inode *inode;
  3524. struct ceph_inode_info *ci;
  3525. int flags = CHECK_CAPS_NODELAY;
  3526. dout("check_delayed_caps\n");
  3527. while (1) {
  3528. spin_lock(&mdsc->cap_delay_lock);
  3529. if (list_empty(&mdsc->cap_delay_list))
  3530. break;
  3531. ci = list_first_entry(&mdsc->cap_delay_list,
  3532. struct ceph_inode_info,
  3533. i_cap_delay_list);
  3534. if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
  3535. time_before(jiffies, ci->i_hold_caps_max))
  3536. break;
  3537. list_del_init(&ci->i_cap_delay_list);
  3538. inode = igrab(&ci->vfs_inode);
  3539. spin_unlock(&mdsc->cap_delay_lock);
  3540. if (inode) {
  3541. dout("check_delayed_caps on %p\n", inode);
  3542. ceph_check_caps(ci, flags, NULL);
  3543. iput(inode);
  3544. }
  3545. }
  3546. spin_unlock(&mdsc->cap_delay_lock);
  3547. }
  3548. /*
  3549. * Flush all dirty caps to the mds
  3550. */
  3551. void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
  3552. {
  3553. struct ceph_inode_info *ci;
  3554. struct inode *inode;
  3555. dout("flush_dirty_caps\n");
  3556. spin_lock(&mdsc->cap_dirty_lock);
  3557. while (!list_empty(&mdsc->cap_dirty)) {
  3558. ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
  3559. i_dirty_item);
  3560. inode = &ci->vfs_inode;
  3561. ihold(inode);
  3562. dout("flush_dirty_caps %p\n", inode);
  3563. spin_unlock(&mdsc->cap_dirty_lock);
  3564. ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
  3565. iput(inode);
  3566. spin_lock(&mdsc->cap_dirty_lock);
  3567. }
  3568. spin_unlock(&mdsc->cap_dirty_lock);
  3569. dout("flush_dirty_caps done\n");
  3570. }
  3571. void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
  3572. {
  3573. int i;
  3574. int bits = (fmode << 1) | 1;
  3575. for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
  3576. if (bits & (1 << i))
  3577. ci->i_nr_by_mode[i]++;
  3578. }
  3579. }
  3580. /*
  3581. * Drop open file reference. If we were the last open file,
  3582. * we may need to release capabilities to the MDS (or schedule
  3583. * their delayed release).
  3584. */
  3585. void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
  3586. {
  3587. int i, last = 0;
  3588. int bits = (fmode << 1) | 1;
  3589. spin_lock(&ci->i_ceph_lock);
  3590. for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
  3591. if (bits & (1 << i)) {
  3592. BUG_ON(ci->i_nr_by_mode[i] == 0);
  3593. if (--ci->i_nr_by_mode[i] == 0)
  3594. last++;
  3595. }
  3596. }
  3597. dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
  3598. &ci->vfs_inode, fmode,
  3599. ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
  3600. ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
  3601. spin_unlock(&ci->i_ceph_lock);
  3602. if (last && ci->i_vino.snap == CEPH_NOSNAP)
  3603. ceph_check_caps(ci, 0, NULL);
  3604. }
  3605. /*
  3606. * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
  3607. * looks like the link count will hit 0, drop any other caps (other
  3608. * than PIN) we don't specifically want (due to the file still being
  3609. * open).
  3610. */
  3611. int ceph_drop_caps_for_unlink(struct inode *inode)
  3612. {
  3613. struct ceph_inode_info *ci = ceph_inode(inode);
  3614. int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
  3615. spin_lock(&ci->i_ceph_lock);
  3616. if (inode->i_nlink == 1) {
  3617. drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
  3618. ci->i_ceph_flags |= CEPH_I_NODELAY;
  3619. if (__ceph_caps_dirty(ci)) {
  3620. struct ceph_mds_client *mdsc =
  3621. ceph_inode_to_client(inode)->mdsc;
  3622. __cap_delay_requeue_front(mdsc, ci);
  3623. }
  3624. }
  3625. spin_unlock(&ci->i_ceph_lock);
  3626. return drop;
  3627. }
  3628. /*
  3629. * Helpers for embedding cap and dentry lease releases into mds
  3630. * requests.
  3631. *
  3632. * @force is used by dentry_release (below) to force inclusion of a
  3633. * record for the directory inode, even when there aren't any caps to
  3634. * drop.
  3635. */
  3636. int ceph_encode_inode_release(void **p, struct inode *inode,
  3637. int mds, int drop, int unless, int force)
  3638. {
  3639. struct ceph_inode_info *ci = ceph_inode(inode);
  3640. struct ceph_cap *cap;
  3641. struct ceph_mds_request_release *rel = *p;
  3642. int used, dirty;
  3643. int ret = 0;
  3644. spin_lock(&ci->i_ceph_lock);
  3645. used = __ceph_caps_used(ci);
  3646. dirty = __ceph_caps_dirty(ci);
  3647. dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
  3648. inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
  3649. ceph_cap_string(unless));
  3650. /* only drop unused, clean caps */
  3651. drop &= ~(used | dirty);
  3652. cap = __get_cap_for_mds(ci, mds);
  3653. if (cap && __cap_is_valid(cap)) {
  3654. unless &= cap->issued;
  3655. if (unless) {
  3656. if (unless & CEPH_CAP_AUTH_EXCL)
  3657. drop &= ~CEPH_CAP_AUTH_SHARED;
  3658. if (unless & CEPH_CAP_LINK_EXCL)
  3659. drop &= ~CEPH_CAP_LINK_SHARED;
  3660. if (unless & CEPH_CAP_XATTR_EXCL)
  3661. drop &= ~CEPH_CAP_XATTR_SHARED;
  3662. if (unless & CEPH_CAP_FILE_EXCL)
  3663. drop &= ~CEPH_CAP_FILE_SHARED;
  3664. }
  3665. if (force || (cap->issued & drop)) {
  3666. if (cap->issued & drop) {
  3667. int wanted = __ceph_caps_wanted(ci);
  3668. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
  3669. wanted |= cap->mds_wanted;
  3670. dout("encode_inode_release %p cap %p "
  3671. "%s -> %s, wanted %s -> %s\n", inode, cap,
  3672. ceph_cap_string(cap->issued),
  3673. ceph_cap_string(cap->issued & ~drop),
  3674. ceph_cap_string(cap->mds_wanted),
  3675. ceph_cap_string(wanted));
  3676. cap->issued &= ~drop;
  3677. cap->implemented &= ~drop;
  3678. cap->mds_wanted = wanted;
  3679. } else {
  3680. dout("encode_inode_release %p cap %p %s"
  3681. " (force)\n", inode, cap,
  3682. ceph_cap_string(cap->issued));
  3683. }
  3684. rel->ino = cpu_to_le64(ceph_ino(inode));
  3685. rel->cap_id = cpu_to_le64(cap->cap_id);
  3686. rel->seq = cpu_to_le32(cap->seq);
  3687. rel->issue_seq = cpu_to_le32(cap->issue_seq);
  3688. rel->mseq = cpu_to_le32(cap->mseq);
  3689. rel->caps = cpu_to_le32(cap->implemented);
  3690. rel->wanted = cpu_to_le32(cap->mds_wanted);
  3691. rel->dname_len = 0;
  3692. rel->dname_seq = 0;
  3693. *p += sizeof(*rel);
  3694. ret = 1;
  3695. } else {
  3696. dout("encode_inode_release %p cap %p %s (noop)\n",
  3697. inode, cap, ceph_cap_string(cap->issued));
  3698. }
  3699. }
  3700. spin_unlock(&ci->i_ceph_lock);
  3701. return ret;
  3702. }
  3703. int ceph_encode_dentry_release(void **p, struct dentry *dentry,
  3704. struct inode *dir,
  3705. int mds, int drop, int unless)
  3706. {
  3707. struct dentry *parent = NULL;
  3708. struct ceph_mds_request_release *rel = *p;
  3709. struct ceph_dentry_info *di = ceph_dentry(dentry);
  3710. int force = 0;
  3711. int ret;
  3712. /*
  3713. * force an record for the directory caps if we have a dentry lease.
  3714. * this is racy (can't take i_ceph_lock and d_lock together), but it
  3715. * doesn't have to be perfect; the mds will revoke anything we don't
  3716. * release.
  3717. */
  3718. spin_lock(&dentry->d_lock);
  3719. if (di->lease_session && di->lease_session->s_mds == mds)
  3720. force = 1;
  3721. if (!dir) {
  3722. parent = dget(dentry->d_parent);
  3723. dir = d_inode(parent);
  3724. }
  3725. spin_unlock(&dentry->d_lock);
  3726. ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
  3727. dput(parent);
  3728. spin_lock(&dentry->d_lock);
  3729. if (ret && di->lease_session && di->lease_session->s_mds == mds) {
  3730. dout("encode_dentry_release %p mds%d seq %d\n",
  3731. dentry, mds, (int)di->lease_seq);
  3732. rel->dname_len = cpu_to_le32(dentry->d_name.len);
  3733. memcpy(*p, dentry->d_name.name, dentry->d_name.len);
  3734. *p += dentry->d_name.len;
  3735. rel->dname_seq = cpu_to_le32(di->lease_seq);
  3736. __ceph_mdsc_drop_dentry_lease(dentry);
  3737. }
  3738. spin_unlock(&dentry->d_lock);
  3739. return ret;
  3740. }