caps.c 101 KB

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