caps.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425
  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 || ci->vfs_inode.i_data.nrpages)
  748. used |= CEPH_CAP_FILE_CACHE;
  749. if (ci->i_wr_ref)
  750. used |= CEPH_CAP_FILE_WR;
  751. if (ci->i_wb_ref || ci->i_wrbuffer_ref)
  752. used |= CEPH_CAP_FILE_BUFFER;
  753. return used;
  754. }
  755. /*
  756. * wanted, by virtue of open file modes
  757. */
  758. int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
  759. {
  760. int want = 0;
  761. int mode;
  762. for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
  763. if (ci->i_nr_by_mode[mode])
  764. want |= ceph_caps_for_mode(mode);
  765. return want;
  766. }
  767. /*
  768. * Return caps we have registered with the MDS(s) as 'wanted'.
  769. */
  770. int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
  771. {
  772. struct ceph_cap *cap;
  773. struct rb_node *p;
  774. int mds_wanted = 0;
  775. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  776. cap = rb_entry(p, struct ceph_cap, ci_node);
  777. if (!__cap_is_valid(cap))
  778. continue;
  779. if (cap == ci->i_auth_cap)
  780. mds_wanted |= cap->mds_wanted;
  781. else
  782. mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
  783. }
  784. return mds_wanted;
  785. }
  786. /*
  787. * called under i_ceph_lock
  788. */
  789. static int __ceph_is_any_caps(struct ceph_inode_info *ci)
  790. {
  791. return !RB_EMPTY_ROOT(&ci->i_caps);
  792. }
  793. int ceph_is_any_caps(struct inode *inode)
  794. {
  795. struct ceph_inode_info *ci = ceph_inode(inode);
  796. int ret;
  797. spin_lock(&ci->i_ceph_lock);
  798. ret = __ceph_is_any_caps(ci);
  799. spin_unlock(&ci->i_ceph_lock);
  800. return ret;
  801. }
  802. /*
  803. * Remove a cap. Take steps to deal with a racing iterate_session_caps.
  804. *
  805. * caller should hold i_ceph_lock.
  806. * caller will not hold session s_mutex if called from destroy_inode.
  807. */
  808. void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
  809. {
  810. struct ceph_mds_session *session = cap->session;
  811. struct ceph_inode_info *ci = cap->ci;
  812. struct ceph_mds_client *mdsc =
  813. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  814. int removed = 0;
  815. dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
  816. /* remove from session list */
  817. spin_lock(&session->s_cap_lock);
  818. /*
  819. * s_cap_reconnect is protected by s_cap_lock. no one changes
  820. * s_cap_gen while session is in the reconnect state.
  821. */
  822. if (queue_release &&
  823. (!session->s_cap_reconnect ||
  824. cap->cap_gen == session->s_cap_gen))
  825. __queue_cap_release(session, ci->i_vino.ino, cap->cap_id,
  826. cap->mseq, cap->issue_seq);
  827. if (session->s_cap_iterator == cap) {
  828. /* not yet, we are iterating over this very cap */
  829. dout("__ceph_remove_cap delaying %p removal from session %p\n",
  830. cap, cap->session);
  831. } else {
  832. list_del_init(&cap->session_caps);
  833. session->s_nr_caps--;
  834. cap->session = NULL;
  835. removed = 1;
  836. }
  837. /* protect backpointer with s_cap_lock: see iterate_session_caps */
  838. cap->ci = NULL;
  839. spin_unlock(&session->s_cap_lock);
  840. /* remove from inode list */
  841. rb_erase(&cap->ci_node, &ci->i_caps);
  842. if (ci->i_auth_cap == cap)
  843. ci->i_auth_cap = NULL;
  844. if (removed)
  845. ceph_put_cap(mdsc, cap);
  846. if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
  847. struct ceph_snap_realm *realm = ci->i_snap_realm;
  848. spin_lock(&realm->inodes_with_caps_lock);
  849. list_del_init(&ci->i_snap_realm_item);
  850. ci->i_snap_realm_counter++;
  851. ci->i_snap_realm = NULL;
  852. spin_unlock(&realm->inodes_with_caps_lock);
  853. ceph_put_snap_realm(mdsc, realm);
  854. }
  855. if (!__ceph_is_any_real_caps(ci))
  856. __cap_delay_cancel(mdsc, ci);
  857. }
  858. /*
  859. * Build and send a cap message to the given MDS.
  860. *
  861. * Caller should be holding s_mutex.
  862. */
  863. static int send_cap_msg(struct ceph_mds_session *session,
  864. u64 ino, u64 cid, int op,
  865. int caps, int wanted, int dirty,
  866. u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
  867. u64 size, u64 max_size,
  868. struct timespec *mtime, struct timespec *atime,
  869. u64 time_warp_seq,
  870. kuid_t uid, kgid_t gid, umode_t mode,
  871. u64 xattr_version,
  872. struct ceph_buffer *xattrs_buf,
  873. u64 follows, bool inline_data)
  874. {
  875. struct ceph_mds_caps *fc;
  876. struct ceph_msg *msg;
  877. void *p;
  878. size_t extra_len;
  879. dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
  880. " seq %u/%u mseq %u follows %lld size %llu/%llu"
  881. " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
  882. cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
  883. ceph_cap_string(dirty),
  884. seq, issue_seq, mseq, follows, size, max_size,
  885. xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
  886. /* flock buffer size + inline version + inline data size */
  887. extra_len = 4 + 8 + 4;
  888. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
  889. GFP_NOFS, false);
  890. if (!msg)
  891. return -ENOMEM;
  892. msg->hdr.tid = cpu_to_le64(flush_tid);
  893. fc = msg->front.iov_base;
  894. memset(fc, 0, sizeof(*fc));
  895. fc->cap_id = cpu_to_le64(cid);
  896. fc->op = cpu_to_le32(op);
  897. fc->seq = cpu_to_le32(seq);
  898. fc->issue_seq = cpu_to_le32(issue_seq);
  899. fc->migrate_seq = cpu_to_le32(mseq);
  900. fc->caps = cpu_to_le32(caps);
  901. fc->wanted = cpu_to_le32(wanted);
  902. fc->dirty = cpu_to_le32(dirty);
  903. fc->ino = cpu_to_le64(ino);
  904. fc->snap_follows = cpu_to_le64(follows);
  905. fc->size = cpu_to_le64(size);
  906. fc->max_size = cpu_to_le64(max_size);
  907. if (mtime)
  908. ceph_encode_timespec(&fc->mtime, mtime);
  909. if (atime)
  910. ceph_encode_timespec(&fc->atime, atime);
  911. fc->time_warp_seq = cpu_to_le32(time_warp_seq);
  912. fc->uid = cpu_to_le32(from_kuid(&init_user_ns, uid));
  913. fc->gid = cpu_to_le32(from_kgid(&init_user_ns, gid));
  914. fc->mode = cpu_to_le32(mode);
  915. p = fc + 1;
  916. /* flock buffer size */
  917. ceph_encode_32(&p, 0);
  918. /* inline version */
  919. ceph_encode_64(&p, inline_data ? 0 : CEPH_INLINE_NONE);
  920. /* inline data size */
  921. ceph_encode_32(&p, 0);
  922. fc->xattr_version = cpu_to_le64(xattr_version);
  923. if (xattrs_buf) {
  924. msg->middle = ceph_buffer_get(xattrs_buf);
  925. fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
  926. msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
  927. }
  928. ceph_con_send(&session->s_con, msg);
  929. return 0;
  930. }
  931. void __queue_cap_release(struct ceph_mds_session *session,
  932. u64 ino, u64 cap_id, u32 migrate_seq,
  933. u32 issue_seq)
  934. {
  935. struct ceph_msg *msg;
  936. struct ceph_mds_cap_release *head;
  937. struct ceph_mds_cap_item *item;
  938. BUG_ON(!session->s_num_cap_releases);
  939. msg = list_first_entry(&session->s_cap_releases,
  940. struct ceph_msg, list_head);
  941. dout(" adding %llx release to mds%d msg %p (%d left)\n",
  942. ino, session->s_mds, msg, session->s_num_cap_releases);
  943. BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
  944. head = msg->front.iov_base;
  945. le32_add_cpu(&head->num, 1);
  946. item = msg->front.iov_base + msg->front.iov_len;
  947. item->ino = cpu_to_le64(ino);
  948. item->cap_id = cpu_to_le64(cap_id);
  949. item->migrate_seq = cpu_to_le32(migrate_seq);
  950. item->seq = cpu_to_le32(issue_seq);
  951. session->s_num_cap_releases--;
  952. msg->front.iov_len += sizeof(*item);
  953. if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
  954. dout(" release msg %p full\n", msg);
  955. list_move_tail(&msg->list_head, &session->s_cap_releases_done);
  956. } else {
  957. dout(" release msg %p at %d/%d (%d)\n", msg,
  958. (int)le32_to_cpu(head->num),
  959. (int)CEPH_CAPS_PER_RELEASE,
  960. (int)msg->front.iov_len);
  961. }
  962. }
  963. /*
  964. * Queue cap releases when an inode is dropped from our cache. Since
  965. * inode is about to be destroyed, there is no need for i_ceph_lock.
  966. */
  967. void ceph_queue_caps_release(struct inode *inode)
  968. {
  969. struct ceph_inode_info *ci = ceph_inode(inode);
  970. struct rb_node *p;
  971. p = rb_first(&ci->i_caps);
  972. while (p) {
  973. struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
  974. p = rb_next(p);
  975. __ceph_remove_cap(cap, true);
  976. }
  977. }
  978. /*
  979. * Send a cap msg on the given inode. Update our caps state, then
  980. * drop i_ceph_lock and send the message.
  981. *
  982. * Make note of max_size reported/requested from mds, revoked caps
  983. * that have now been implemented.
  984. *
  985. * Make half-hearted attempt ot to invalidate page cache if we are
  986. * dropping RDCACHE. Note that this will leave behind locked pages
  987. * that we'll then need to deal with elsewhere.
  988. *
  989. * Return non-zero if delayed release, or we experienced an error
  990. * such that the caller should requeue + retry later.
  991. *
  992. * called with i_ceph_lock, then drops it.
  993. * caller should hold snap_rwsem (read), s_mutex.
  994. */
  995. static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
  996. int op, int used, int want, int retain, int flushing,
  997. unsigned *pflush_tid)
  998. __releases(cap->ci->i_ceph_lock)
  999. {
  1000. struct ceph_inode_info *ci = cap->ci;
  1001. struct inode *inode = &ci->vfs_inode;
  1002. u64 cap_id = cap->cap_id;
  1003. int held, revoking, dropping, keep;
  1004. u64 seq, issue_seq, mseq, time_warp_seq, follows;
  1005. u64 size, max_size;
  1006. struct timespec mtime, atime;
  1007. int wake = 0;
  1008. umode_t mode;
  1009. kuid_t uid;
  1010. kgid_t gid;
  1011. struct ceph_mds_session *session;
  1012. u64 xattr_version = 0;
  1013. struct ceph_buffer *xattr_blob = NULL;
  1014. int delayed = 0;
  1015. u64 flush_tid = 0;
  1016. int i;
  1017. int ret;
  1018. bool inline_data;
  1019. held = cap->issued | cap->implemented;
  1020. revoking = cap->implemented & ~cap->issued;
  1021. retain &= ~revoking;
  1022. dropping = cap->issued & ~retain;
  1023. dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
  1024. inode, cap, cap->session,
  1025. ceph_cap_string(held), ceph_cap_string(held & retain),
  1026. ceph_cap_string(revoking));
  1027. BUG_ON((retain & CEPH_CAP_PIN) == 0);
  1028. session = cap->session;
  1029. /* don't release wanted unless we've waited a bit. */
  1030. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  1031. time_before(jiffies, ci->i_hold_caps_min)) {
  1032. dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
  1033. ceph_cap_string(cap->issued),
  1034. ceph_cap_string(cap->issued & retain),
  1035. ceph_cap_string(cap->mds_wanted),
  1036. ceph_cap_string(want));
  1037. want |= cap->mds_wanted;
  1038. retain |= cap->issued;
  1039. delayed = 1;
  1040. }
  1041. ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
  1042. cap->issued &= retain; /* drop bits we don't want */
  1043. if (cap->implemented & ~cap->issued) {
  1044. /*
  1045. * Wake up any waiters on wanted -> needed transition.
  1046. * This is due to the weird transition from buffered
  1047. * to sync IO... we need to flush dirty pages _before_
  1048. * allowing sync writes to avoid reordering.
  1049. */
  1050. wake = 1;
  1051. }
  1052. cap->implemented &= cap->issued | used;
  1053. cap->mds_wanted = want;
  1054. if (flushing) {
  1055. /*
  1056. * assign a tid for flush operations so we can avoid
  1057. * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
  1058. * clean type races. track latest tid for every bit
  1059. * so we can handle flush AxFw, flush Fw, and have the
  1060. * first ack clean Ax.
  1061. */
  1062. flush_tid = ++ci->i_cap_flush_last_tid;
  1063. if (pflush_tid)
  1064. *pflush_tid = flush_tid;
  1065. dout(" cap_flush_tid %d\n", (int)flush_tid);
  1066. for (i = 0; i < CEPH_CAP_BITS; i++)
  1067. if (flushing & (1 << i))
  1068. ci->i_cap_flush_tid[i] = flush_tid;
  1069. follows = ci->i_head_snapc->seq;
  1070. } else {
  1071. follows = 0;
  1072. }
  1073. keep = cap->implemented;
  1074. seq = cap->seq;
  1075. issue_seq = cap->issue_seq;
  1076. mseq = cap->mseq;
  1077. size = inode->i_size;
  1078. ci->i_reported_size = size;
  1079. max_size = ci->i_wanted_max_size;
  1080. ci->i_requested_max_size = max_size;
  1081. mtime = inode->i_mtime;
  1082. atime = inode->i_atime;
  1083. time_warp_seq = ci->i_time_warp_seq;
  1084. uid = inode->i_uid;
  1085. gid = inode->i_gid;
  1086. mode = inode->i_mode;
  1087. if (flushing & CEPH_CAP_XATTR_EXCL) {
  1088. __ceph_build_xattrs_blob(ci);
  1089. xattr_blob = ci->i_xattrs.blob;
  1090. xattr_version = ci->i_xattrs.version;
  1091. }
  1092. inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
  1093. spin_unlock(&ci->i_ceph_lock);
  1094. ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
  1095. op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
  1096. size, max_size, &mtime, &atime, time_warp_seq,
  1097. uid, gid, mode, xattr_version, xattr_blob,
  1098. follows, inline_data);
  1099. if (ret < 0) {
  1100. dout("error sending cap msg, must requeue %p\n", inode);
  1101. delayed = 1;
  1102. }
  1103. if (wake)
  1104. wake_up_all(&ci->i_cap_wq);
  1105. return delayed;
  1106. }
  1107. /*
  1108. * When a snapshot is taken, clients accumulate dirty metadata on
  1109. * inodes with capabilities in ceph_cap_snaps to describe the file
  1110. * state at the time the snapshot was taken. This must be flushed
  1111. * asynchronously back to the MDS once sync writes complete and dirty
  1112. * data is written out.
  1113. *
  1114. * Unless @again is true, skip cap_snaps that were already sent to
  1115. * the MDS (i.e., during this session).
  1116. *
  1117. * Called under i_ceph_lock. Takes s_mutex as needed.
  1118. */
  1119. void __ceph_flush_snaps(struct ceph_inode_info *ci,
  1120. struct ceph_mds_session **psession,
  1121. int again)
  1122. __releases(ci->i_ceph_lock)
  1123. __acquires(ci->i_ceph_lock)
  1124. {
  1125. struct inode *inode = &ci->vfs_inode;
  1126. int mds;
  1127. struct ceph_cap_snap *capsnap;
  1128. u32 mseq;
  1129. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  1130. struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
  1131. session->s_mutex */
  1132. u64 next_follows = 0; /* keep track of how far we've gotten through the
  1133. i_cap_snaps list, and skip these entries next time
  1134. around to avoid an infinite loop */
  1135. if (psession)
  1136. session = *psession;
  1137. dout("__flush_snaps %p\n", inode);
  1138. retry:
  1139. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1140. /* avoid an infiniute loop after retry */
  1141. if (capsnap->follows < next_follows)
  1142. continue;
  1143. /*
  1144. * we need to wait for sync writes to complete and for dirty
  1145. * pages to be written out.
  1146. */
  1147. if (capsnap->dirty_pages || capsnap->writing)
  1148. break;
  1149. /*
  1150. * if cap writeback already occurred, we should have dropped
  1151. * the capsnap in ceph_put_wrbuffer_cap_refs.
  1152. */
  1153. BUG_ON(capsnap->dirty == 0);
  1154. /* pick mds, take s_mutex */
  1155. if (ci->i_auth_cap == NULL) {
  1156. dout("no auth cap (migrating?), doing nothing\n");
  1157. goto out;
  1158. }
  1159. /* only flush each capsnap once */
  1160. if (!again && !list_empty(&capsnap->flushing_item)) {
  1161. dout("already flushed %p, skipping\n", capsnap);
  1162. continue;
  1163. }
  1164. mds = ci->i_auth_cap->session->s_mds;
  1165. mseq = ci->i_auth_cap->mseq;
  1166. if (session && session->s_mds != mds) {
  1167. dout("oops, wrong session %p mutex\n", session);
  1168. mutex_unlock(&session->s_mutex);
  1169. ceph_put_mds_session(session);
  1170. session = NULL;
  1171. }
  1172. if (!session) {
  1173. spin_unlock(&ci->i_ceph_lock);
  1174. mutex_lock(&mdsc->mutex);
  1175. session = __ceph_lookup_mds_session(mdsc, mds);
  1176. mutex_unlock(&mdsc->mutex);
  1177. if (session) {
  1178. dout("inverting session/ino locks on %p\n",
  1179. session);
  1180. mutex_lock(&session->s_mutex);
  1181. }
  1182. /*
  1183. * if session == NULL, we raced against a cap
  1184. * deletion or migration. retry, and we'll
  1185. * get a better @mds value next time.
  1186. */
  1187. spin_lock(&ci->i_ceph_lock);
  1188. goto retry;
  1189. }
  1190. capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
  1191. atomic_inc(&capsnap->nref);
  1192. if (!list_empty(&capsnap->flushing_item))
  1193. list_del_init(&capsnap->flushing_item);
  1194. list_add_tail(&capsnap->flushing_item,
  1195. &session->s_cap_snaps_flushing);
  1196. spin_unlock(&ci->i_ceph_lock);
  1197. dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
  1198. inode, capsnap, capsnap->follows, capsnap->flush_tid);
  1199. send_cap_msg(session, ceph_vino(inode).ino, 0,
  1200. CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
  1201. capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
  1202. capsnap->size, 0,
  1203. &capsnap->mtime, &capsnap->atime,
  1204. capsnap->time_warp_seq,
  1205. capsnap->uid, capsnap->gid, capsnap->mode,
  1206. capsnap->xattr_version, capsnap->xattr_blob,
  1207. capsnap->follows, capsnap->inline_data);
  1208. next_follows = capsnap->follows + 1;
  1209. ceph_put_cap_snap(capsnap);
  1210. spin_lock(&ci->i_ceph_lock);
  1211. goto retry;
  1212. }
  1213. /* we flushed them all; remove this inode from the queue */
  1214. spin_lock(&mdsc->snap_flush_lock);
  1215. list_del_init(&ci->i_snap_flush_item);
  1216. spin_unlock(&mdsc->snap_flush_lock);
  1217. out:
  1218. if (psession)
  1219. *psession = session;
  1220. else if (session) {
  1221. mutex_unlock(&session->s_mutex);
  1222. ceph_put_mds_session(session);
  1223. }
  1224. }
  1225. static void ceph_flush_snaps(struct ceph_inode_info *ci)
  1226. {
  1227. spin_lock(&ci->i_ceph_lock);
  1228. __ceph_flush_snaps(ci, NULL, 0);
  1229. spin_unlock(&ci->i_ceph_lock);
  1230. }
  1231. /*
  1232. * Mark caps dirty. If inode is newly dirty, return the dirty flags.
  1233. * Caller is then responsible for calling __mark_inode_dirty with the
  1234. * returned flags value.
  1235. */
  1236. int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
  1237. {
  1238. struct ceph_mds_client *mdsc =
  1239. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  1240. struct inode *inode = &ci->vfs_inode;
  1241. int was = ci->i_dirty_caps;
  1242. int dirty = 0;
  1243. dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
  1244. ceph_cap_string(mask), ceph_cap_string(was),
  1245. ceph_cap_string(was | mask));
  1246. ci->i_dirty_caps |= mask;
  1247. if (was == 0) {
  1248. if (!ci->i_head_snapc)
  1249. ci->i_head_snapc = ceph_get_snap_context(
  1250. ci->i_snap_realm->cached_context);
  1251. dout(" inode %p now dirty snapc %p auth cap %p\n",
  1252. &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
  1253. WARN_ON(!ci->i_auth_cap);
  1254. BUG_ON(!list_empty(&ci->i_dirty_item));
  1255. spin_lock(&mdsc->cap_dirty_lock);
  1256. list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
  1257. spin_unlock(&mdsc->cap_dirty_lock);
  1258. if (ci->i_flushing_caps == 0) {
  1259. ihold(inode);
  1260. dirty |= I_DIRTY_SYNC;
  1261. }
  1262. }
  1263. BUG_ON(list_empty(&ci->i_dirty_item));
  1264. if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
  1265. (mask & CEPH_CAP_FILE_BUFFER))
  1266. dirty |= I_DIRTY_DATASYNC;
  1267. __cap_delay_requeue(mdsc, ci);
  1268. return dirty;
  1269. }
  1270. /*
  1271. * Add dirty inode to the flushing list. Assigned a seq number so we
  1272. * can wait for caps to flush without starving.
  1273. *
  1274. * Called under i_ceph_lock.
  1275. */
  1276. static int __mark_caps_flushing(struct inode *inode,
  1277. struct ceph_mds_session *session)
  1278. {
  1279. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1280. struct ceph_inode_info *ci = ceph_inode(inode);
  1281. int flushing;
  1282. BUG_ON(ci->i_dirty_caps == 0);
  1283. BUG_ON(list_empty(&ci->i_dirty_item));
  1284. flushing = ci->i_dirty_caps;
  1285. dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
  1286. ceph_cap_string(flushing),
  1287. ceph_cap_string(ci->i_flushing_caps),
  1288. ceph_cap_string(ci->i_flushing_caps | flushing));
  1289. ci->i_flushing_caps |= flushing;
  1290. ci->i_dirty_caps = 0;
  1291. dout(" inode %p now !dirty\n", inode);
  1292. spin_lock(&mdsc->cap_dirty_lock);
  1293. list_del_init(&ci->i_dirty_item);
  1294. if (list_empty(&ci->i_flushing_item)) {
  1295. ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
  1296. list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1297. mdsc->num_cap_flushing++;
  1298. dout(" inode %p now flushing seq %lld\n", inode,
  1299. ci->i_cap_flush_seq);
  1300. } else {
  1301. list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1302. dout(" inode %p now flushing (more) seq %lld\n", inode,
  1303. ci->i_cap_flush_seq);
  1304. }
  1305. spin_unlock(&mdsc->cap_dirty_lock);
  1306. return flushing;
  1307. }
  1308. /*
  1309. * try to invalidate mapping pages without blocking.
  1310. */
  1311. static int try_nonblocking_invalidate(struct inode *inode)
  1312. {
  1313. struct ceph_inode_info *ci = ceph_inode(inode);
  1314. u32 invalidating_gen = ci->i_rdcache_gen;
  1315. spin_unlock(&ci->i_ceph_lock);
  1316. invalidate_mapping_pages(&inode->i_data, 0, -1);
  1317. spin_lock(&ci->i_ceph_lock);
  1318. if (inode->i_data.nrpages == 0 &&
  1319. invalidating_gen == ci->i_rdcache_gen) {
  1320. /* success. */
  1321. dout("try_nonblocking_invalidate %p success\n", inode);
  1322. /* save any racing async invalidate some trouble */
  1323. ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
  1324. return 0;
  1325. }
  1326. dout("try_nonblocking_invalidate %p failed\n", inode);
  1327. return -1;
  1328. }
  1329. /*
  1330. * Swiss army knife function to examine currently used and wanted
  1331. * versus held caps. Release, flush, ack revoked caps to mds as
  1332. * appropriate.
  1333. *
  1334. * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
  1335. * cap release further.
  1336. * CHECK_CAPS_AUTHONLY - we should only check the auth cap
  1337. * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
  1338. * further delay.
  1339. */
  1340. void ceph_check_caps(struct ceph_inode_info *ci, int flags,
  1341. struct ceph_mds_session *session)
  1342. {
  1343. struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
  1344. struct ceph_mds_client *mdsc = fsc->mdsc;
  1345. struct inode *inode = &ci->vfs_inode;
  1346. struct ceph_cap *cap;
  1347. int file_wanted, used, cap_used;
  1348. int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
  1349. int issued, implemented, want, retain, revoking, flushing = 0;
  1350. int mds = -1; /* keep track of how far we've gone through i_caps list
  1351. to avoid an infinite loop on retry */
  1352. struct rb_node *p;
  1353. int tried_invalidate = 0;
  1354. int delayed = 0, sent = 0, force_requeue = 0, num;
  1355. int queue_invalidate = 0;
  1356. int is_delayed = flags & CHECK_CAPS_NODELAY;
  1357. /* if we are unmounting, flush any unused caps immediately. */
  1358. if (mdsc->stopping)
  1359. is_delayed = 1;
  1360. spin_lock(&ci->i_ceph_lock);
  1361. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  1362. flags |= CHECK_CAPS_FLUSH;
  1363. /* flush snaps first time around only */
  1364. if (!list_empty(&ci->i_cap_snaps))
  1365. __ceph_flush_snaps(ci, &session, 0);
  1366. goto retry_locked;
  1367. retry:
  1368. spin_lock(&ci->i_ceph_lock);
  1369. retry_locked:
  1370. file_wanted = __ceph_caps_file_wanted(ci);
  1371. used = __ceph_caps_used(ci);
  1372. want = file_wanted | used;
  1373. issued = __ceph_caps_issued(ci, &implemented);
  1374. revoking = implemented & ~issued;
  1375. retain = want | CEPH_CAP_PIN;
  1376. if (!mdsc->stopping && inode->i_nlink > 0) {
  1377. if (want) {
  1378. retain |= CEPH_CAP_ANY; /* be greedy */
  1379. } else {
  1380. retain |= CEPH_CAP_ANY_SHARED;
  1381. /*
  1382. * keep RD only if we didn't have the file open RW,
  1383. * because then the mds would revoke it anyway to
  1384. * journal max_size=0.
  1385. */
  1386. if (ci->i_max_size == 0)
  1387. retain |= CEPH_CAP_ANY_RD;
  1388. }
  1389. }
  1390. dout("check_caps %p file_want %s used %s dirty %s flushing %s"
  1391. " issued %s revoking %s retain %s %s%s%s\n", inode,
  1392. ceph_cap_string(file_wanted),
  1393. ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
  1394. ceph_cap_string(ci->i_flushing_caps),
  1395. ceph_cap_string(issued), ceph_cap_string(revoking),
  1396. ceph_cap_string(retain),
  1397. (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
  1398. (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
  1399. (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
  1400. /*
  1401. * If we no longer need to hold onto old our caps, and we may
  1402. * have cached pages, but don't want them, then try to invalidate.
  1403. * If we fail, it's because pages are locked.... try again later.
  1404. */
  1405. if ((!is_delayed || mdsc->stopping) &&
  1406. ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
  1407. inode->i_data.nrpages && /* have cached pages */
  1408. (file_wanted == 0 || /* no open files */
  1409. (revoking & (CEPH_CAP_FILE_CACHE|
  1410. CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
  1411. !tried_invalidate) {
  1412. dout("check_caps trying to invalidate on %p\n", inode);
  1413. if (try_nonblocking_invalidate(inode) < 0) {
  1414. if (revoking & (CEPH_CAP_FILE_CACHE|
  1415. CEPH_CAP_FILE_LAZYIO)) {
  1416. dout("check_caps queuing invalidate\n");
  1417. queue_invalidate = 1;
  1418. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  1419. } else {
  1420. dout("check_caps failed to invalidate pages\n");
  1421. /* we failed to invalidate pages. check these
  1422. caps again later. */
  1423. force_requeue = 1;
  1424. __cap_set_timeouts(mdsc, ci);
  1425. }
  1426. }
  1427. tried_invalidate = 1;
  1428. goto retry_locked;
  1429. }
  1430. num = 0;
  1431. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  1432. cap = rb_entry(p, struct ceph_cap, ci_node);
  1433. num++;
  1434. /* avoid looping forever */
  1435. if (mds >= cap->mds ||
  1436. ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
  1437. continue;
  1438. /* NOTE: no side-effects allowed, until we take s_mutex */
  1439. cap_used = used;
  1440. if (ci->i_auth_cap && cap != ci->i_auth_cap)
  1441. cap_used &= ~ci->i_auth_cap->issued;
  1442. revoking = cap->implemented & ~cap->issued;
  1443. dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
  1444. cap->mds, cap, ceph_cap_string(cap->issued),
  1445. ceph_cap_string(cap_used),
  1446. ceph_cap_string(cap->implemented),
  1447. ceph_cap_string(revoking));
  1448. if (cap == ci->i_auth_cap &&
  1449. (cap->issued & CEPH_CAP_FILE_WR)) {
  1450. /* request larger max_size from MDS? */
  1451. if (ci->i_wanted_max_size > ci->i_max_size &&
  1452. ci->i_wanted_max_size > ci->i_requested_max_size) {
  1453. dout("requesting new max_size\n");
  1454. goto ack;
  1455. }
  1456. /* approaching file_max? */
  1457. if ((inode->i_size << 1) >= ci->i_max_size &&
  1458. (ci->i_reported_size << 1) < ci->i_max_size) {
  1459. dout("i_size approaching max_size\n");
  1460. goto ack;
  1461. }
  1462. }
  1463. /* flush anything dirty? */
  1464. if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
  1465. ci->i_dirty_caps) {
  1466. dout("flushing dirty caps\n");
  1467. goto ack;
  1468. }
  1469. /* completed revocation? going down and there are no caps? */
  1470. if (revoking && (revoking & cap_used) == 0) {
  1471. dout("completed revocation of %s\n",
  1472. ceph_cap_string(cap->implemented & ~cap->issued));
  1473. goto ack;
  1474. }
  1475. /* want more caps from mds? */
  1476. if (want & ~(cap->mds_wanted | cap->issued))
  1477. goto ack;
  1478. /* things we might delay */
  1479. if ((cap->issued & ~retain) == 0 &&
  1480. cap->mds_wanted == want)
  1481. continue; /* nope, all good */
  1482. if (is_delayed)
  1483. goto ack;
  1484. /* delay? */
  1485. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  1486. time_before(jiffies, ci->i_hold_caps_max)) {
  1487. dout(" delaying issued %s -> %s, wanted %s -> %s\n",
  1488. ceph_cap_string(cap->issued),
  1489. ceph_cap_string(cap->issued & retain),
  1490. ceph_cap_string(cap->mds_wanted),
  1491. ceph_cap_string(want));
  1492. delayed++;
  1493. continue;
  1494. }
  1495. ack:
  1496. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1497. dout(" skipping %p I_NOFLUSH set\n", inode);
  1498. continue;
  1499. }
  1500. if (session && session != cap->session) {
  1501. dout("oops, wrong session %p mutex\n", session);
  1502. mutex_unlock(&session->s_mutex);
  1503. session = NULL;
  1504. }
  1505. if (!session) {
  1506. session = cap->session;
  1507. if (mutex_trylock(&session->s_mutex) == 0) {
  1508. dout("inverting session/ino locks on %p\n",
  1509. session);
  1510. spin_unlock(&ci->i_ceph_lock);
  1511. if (took_snap_rwsem) {
  1512. up_read(&mdsc->snap_rwsem);
  1513. took_snap_rwsem = 0;
  1514. }
  1515. mutex_lock(&session->s_mutex);
  1516. goto retry;
  1517. }
  1518. }
  1519. /* take snap_rwsem after session mutex */
  1520. if (!took_snap_rwsem) {
  1521. if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
  1522. dout("inverting snap/in locks on %p\n",
  1523. inode);
  1524. spin_unlock(&ci->i_ceph_lock);
  1525. down_read(&mdsc->snap_rwsem);
  1526. took_snap_rwsem = 1;
  1527. goto retry;
  1528. }
  1529. took_snap_rwsem = 1;
  1530. }
  1531. if (cap == ci->i_auth_cap && ci->i_dirty_caps)
  1532. flushing = __mark_caps_flushing(inode, session);
  1533. else
  1534. flushing = 0;
  1535. mds = cap->mds; /* remember mds, so we don't repeat */
  1536. sent++;
  1537. /* __send_cap drops i_ceph_lock */
  1538. delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, cap_used,
  1539. want, retain, flushing, NULL);
  1540. goto retry; /* retake i_ceph_lock and restart our cap scan. */
  1541. }
  1542. /*
  1543. * Reschedule delayed caps release if we delayed anything,
  1544. * otherwise cancel.
  1545. */
  1546. if (delayed && is_delayed)
  1547. force_requeue = 1; /* __send_cap delayed release; requeue */
  1548. if (!delayed && !is_delayed)
  1549. __cap_delay_cancel(mdsc, ci);
  1550. else if (!is_delayed || force_requeue)
  1551. __cap_delay_requeue(mdsc, ci);
  1552. spin_unlock(&ci->i_ceph_lock);
  1553. if (queue_invalidate)
  1554. ceph_queue_invalidate(inode);
  1555. if (session)
  1556. mutex_unlock(&session->s_mutex);
  1557. if (took_snap_rwsem)
  1558. up_read(&mdsc->snap_rwsem);
  1559. }
  1560. /*
  1561. * Try to flush dirty caps back to the auth mds.
  1562. */
  1563. static int try_flush_caps(struct inode *inode, unsigned *flush_tid)
  1564. {
  1565. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1566. struct ceph_inode_info *ci = ceph_inode(inode);
  1567. int flushing = 0;
  1568. struct ceph_mds_session *session = NULL;
  1569. retry:
  1570. spin_lock(&ci->i_ceph_lock);
  1571. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1572. dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
  1573. goto out;
  1574. }
  1575. if (ci->i_dirty_caps && ci->i_auth_cap) {
  1576. struct ceph_cap *cap = ci->i_auth_cap;
  1577. int used = __ceph_caps_used(ci);
  1578. int want = __ceph_caps_wanted(ci);
  1579. int delayed;
  1580. if (!session || session != cap->session) {
  1581. spin_unlock(&ci->i_ceph_lock);
  1582. if (session)
  1583. mutex_unlock(&session->s_mutex);
  1584. session = cap->session;
  1585. mutex_lock(&session->s_mutex);
  1586. goto retry;
  1587. }
  1588. if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
  1589. goto out;
  1590. flushing = __mark_caps_flushing(inode, session);
  1591. /* __send_cap drops i_ceph_lock */
  1592. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
  1593. cap->issued | cap->implemented, flushing,
  1594. flush_tid);
  1595. if (!delayed)
  1596. goto out_unlocked;
  1597. spin_lock(&ci->i_ceph_lock);
  1598. __cap_delay_requeue(mdsc, ci);
  1599. }
  1600. out:
  1601. spin_unlock(&ci->i_ceph_lock);
  1602. out_unlocked:
  1603. if (session)
  1604. mutex_unlock(&session->s_mutex);
  1605. return flushing;
  1606. }
  1607. /*
  1608. * Return true if we've flushed caps through the given flush_tid.
  1609. */
  1610. static int caps_are_flushed(struct inode *inode, unsigned tid)
  1611. {
  1612. struct ceph_inode_info *ci = ceph_inode(inode);
  1613. int i, ret = 1;
  1614. spin_lock(&ci->i_ceph_lock);
  1615. for (i = 0; i < CEPH_CAP_BITS; i++)
  1616. if ((ci->i_flushing_caps & (1 << i)) &&
  1617. ci->i_cap_flush_tid[i] <= tid) {
  1618. /* still flushing this bit */
  1619. ret = 0;
  1620. break;
  1621. }
  1622. spin_unlock(&ci->i_ceph_lock);
  1623. return ret;
  1624. }
  1625. /*
  1626. * Wait on any unsafe replies for the given inode. First wait on the
  1627. * newest request, and make that the upper bound. Then, if there are
  1628. * more requests, keep waiting on the oldest as long as it is still older
  1629. * than the original request.
  1630. */
  1631. static void sync_write_wait(struct inode *inode)
  1632. {
  1633. struct ceph_inode_info *ci = ceph_inode(inode);
  1634. struct list_head *head = &ci->i_unsafe_writes;
  1635. struct ceph_osd_request *req;
  1636. u64 last_tid;
  1637. spin_lock(&ci->i_unsafe_lock);
  1638. if (list_empty(head))
  1639. goto out;
  1640. /* set upper bound as _last_ entry in chain */
  1641. req = list_entry(head->prev, struct ceph_osd_request,
  1642. r_unsafe_item);
  1643. last_tid = req->r_tid;
  1644. do {
  1645. ceph_osdc_get_request(req);
  1646. spin_unlock(&ci->i_unsafe_lock);
  1647. dout("sync_write_wait on tid %llu (until %llu)\n",
  1648. req->r_tid, last_tid);
  1649. wait_for_completion(&req->r_safe_completion);
  1650. spin_lock(&ci->i_unsafe_lock);
  1651. ceph_osdc_put_request(req);
  1652. /*
  1653. * from here on look at first entry in chain, since we
  1654. * only want to wait for anything older than last_tid
  1655. */
  1656. if (list_empty(head))
  1657. break;
  1658. req = list_entry(head->next, struct ceph_osd_request,
  1659. r_unsafe_item);
  1660. } while (req->r_tid < last_tid);
  1661. out:
  1662. spin_unlock(&ci->i_unsafe_lock);
  1663. }
  1664. int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  1665. {
  1666. struct inode *inode = file->f_mapping->host;
  1667. struct ceph_inode_info *ci = ceph_inode(inode);
  1668. unsigned flush_tid;
  1669. int ret;
  1670. int dirty;
  1671. dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
  1672. sync_write_wait(inode);
  1673. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1674. if (ret < 0)
  1675. return ret;
  1676. mutex_lock(&inode->i_mutex);
  1677. dirty = try_flush_caps(inode, &flush_tid);
  1678. dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
  1679. /*
  1680. * only wait on non-file metadata writeback (the mds
  1681. * can recover size and mtime, so we don't need to
  1682. * wait for that)
  1683. */
  1684. if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
  1685. dout("fsync waiting for flush_tid %u\n", flush_tid);
  1686. ret = wait_event_interruptible(ci->i_cap_wq,
  1687. caps_are_flushed(inode, flush_tid));
  1688. }
  1689. dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
  1690. mutex_unlock(&inode->i_mutex);
  1691. return ret;
  1692. }
  1693. /*
  1694. * Flush any dirty caps back to the mds. If we aren't asked to wait,
  1695. * queue inode for flush but don't do so immediately, because we can
  1696. * get by with fewer MDS messages if we wait for data writeback to
  1697. * complete first.
  1698. */
  1699. int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
  1700. {
  1701. struct ceph_inode_info *ci = ceph_inode(inode);
  1702. unsigned flush_tid;
  1703. int err = 0;
  1704. int dirty;
  1705. int wait = wbc->sync_mode == WB_SYNC_ALL;
  1706. dout("write_inode %p wait=%d\n", inode, wait);
  1707. if (wait) {
  1708. dirty = try_flush_caps(inode, &flush_tid);
  1709. if (dirty)
  1710. err = wait_event_interruptible(ci->i_cap_wq,
  1711. caps_are_flushed(inode, flush_tid));
  1712. } else {
  1713. struct ceph_mds_client *mdsc =
  1714. ceph_sb_to_client(inode->i_sb)->mdsc;
  1715. spin_lock(&ci->i_ceph_lock);
  1716. if (__ceph_caps_dirty(ci))
  1717. __cap_delay_requeue_front(mdsc, ci);
  1718. spin_unlock(&ci->i_ceph_lock);
  1719. }
  1720. return err;
  1721. }
  1722. /*
  1723. * After a recovering MDS goes active, we need to resend any caps
  1724. * we were flushing.
  1725. *
  1726. * Caller holds session->s_mutex.
  1727. */
  1728. static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
  1729. struct ceph_mds_session *session)
  1730. {
  1731. struct ceph_cap_snap *capsnap;
  1732. dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
  1733. list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
  1734. flushing_item) {
  1735. struct ceph_inode_info *ci = capsnap->ci;
  1736. struct inode *inode = &ci->vfs_inode;
  1737. struct ceph_cap *cap;
  1738. spin_lock(&ci->i_ceph_lock);
  1739. cap = ci->i_auth_cap;
  1740. if (cap && cap->session == session) {
  1741. dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
  1742. cap, capsnap);
  1743. __ceph_flush_snaps(ci, &session, 1);
  1744. } else {
  1745. pr_err("%p auth cap %p not mds%d ???\n", inode,
  1746. cap, session->s_mds);
  1747. }
  1748. spin_unlock(&ci->i_ceph_lock);
  1749. }
  1750. }
  1751. void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
  1752. struct ceph_mds_session *session)
  1753. {
  1754. struct ceph_inode_info *ci;
  1755. kick_flushing_capsnaps(mdsc, session);
  1756. dout("kick_flushing_caps mds%d\n", session->s_mds);
  1757. list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  1758. struct inode *inode = &ci->vfs_inode;
  1759. struct ceph_cap *cap;
  1760. int delayed = 0;
  1761. spin_lock(&ci->i_ceph_lock);
  1762. cap = ci->i_auth_cap;
  1763. if (cap && cap->session == session) {
  1764. dout("kick_flushing_caps %p cap %p %s\n", inode,
  1765. cap, ceph_cap_string(ci->i_flushing_caps));
  1766. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
  1767. __ceph_caps_used(ci),
  1768. __ceph_caps_wanted(ci),
  1769. cap->issued | cap->implemented,
  1770. ci->i_flushing_caps, NULL);
  1771. if (delayed) {
  1772. spin_lock(&ci->i_ceph_lock);
  1773. __cap_delay_requeue(mdsc, ci);
  1774. spin_unlock(&ci->i_ceph_lock);
  1775. }
  1776. } else {
  1777. pr_err("%p auth cap %p not mds%d ???\n", inode,
  1778. cap, session->s_mds);
  1779. spin_unlock(&ci->i_ceph_lock);
  1780. }
  1781. }
  1782. }
  1783. static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
  1784. struct ceph_mds_session *session,
  1785. struct inode *inode)
  1786. {
  1787. struct ceph_inode_info *ci = ceph_inode(inode);
  1788. struct ceph_cap *cap;
  1789. int delayed = 0;
  1790. spin_lock(&ci->i_ceph_lock);
  1791. cap = ci->i_auth_cap;
  1792. dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode,
  1793. ceph_cap_string(ci->i_flushing_caps), ci->i_cap_flush_seq);
  1794. __ceph_flush_snaps(ci, &session, 1);
  1795. if (ci->i_flushing_caps) {
  1796. spin_lock(&mdsc->cap_dirty_lock);
  1797. list_move_tail(&ci->i_flushing_item,
  1798. &cap->session->s_cap_flushing);
  1799. spin_unlock(&mdsc->cap_dirty_lock);
  1800. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
  1801. __ceph_caps_used(ci),
  1802. __ceph_caps_wanted(ci),
  1803. cap->issued | cap->implemented,
  1804. ci->i_flushing_caps, NULL);
  1805. if (delayed) {
  1806. spin_lock(&ci->i_ceph_lock);
  1807. __cap_delay_requeue(mdsc, ci);
  1808. spin_unlock(&ci->i_ceph_lock);
  1809. }
  1810. } else {
  1811. spin_unlock(&ci->i_ceph_lock);
  1812. }
  1813. }
  1814. /*
  1815. * Take references to capabilities we hold, so that we don't release
  1816. * them to the MDS prematurely.
  1817. *
  1818. * Protected by i_ceph_lock.
  1819. */
  1820. static void __take_cap_refs(struct ceph_inode_info *ci, int got)
  1821. {
  1822. if (got & CEPH_CAP_PIN)
  1823. ci->i_pin_ref++;
  1824. if (got & CEPH_CAP_FILE_RD)
  1825. ci->i_rd_ref++;
  1826. if (got & CEPH_CAP_FILE_CACHE)
  1827. ci->i_rdcache_ref++;
  1828. if (got & CEPH_CAP_FILE_WR)
  1829. ci->i_wr_ref++;
  1830. if (got & CEPH_CAP_FILE_BUFFER) {
  1831. if (ci->i_wb_ref == 0)
  1832. ihold(&ci->vfs_inode);
  1833. ci->i_wb_ref++;
  1834. dout("__take_cap_refs %p wb %d -> %d (?)\n",
  1835. &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
  1836. }
  1837. }
  1838. /*
  1839. * Try to grab cap references. Specify those refs we @want, and the
  1840. * minimal set we @need. Also include the larger offset we are writing
  1841. * to (when applicable), and check against max_size here as well.
  1842. * Note that caller is responsible for ensuring max_size increases are
  1843. * requested from the MDS.
  1844. */
  1845. static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
  1846. loff_t endoff, int *got, int *check_max, int *err)
  1847. {
  1848. struct inode *inode = &ci->vfs_inode;
  1849. int ret = 0;
  1850. int have, implemented;
  1851. int file_wanted;
  1852. dout("get_cap_refs %p need %s want %s\n", inode,
  1853. ceph_cap_string(need), ceph_cap_string(want));
  1854. spin_lock(&ci->i_ceph_lock);
  1855. /* make sure file is actually open */
  1856. file_wanted = __ceph_caps_file_wanted(ci);
  1857. if ((file_wanted & need) == 0) {
  1858. dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
  1859. ceph_cap_string(need), ceph_cap_string(file_wanted));
  1860. *err = -EBADF;
  1861. ret = 1;
  1862. goto out_unlock;
  1863. }
  1864. /* finish pending truncate */
  1865. while (ci->i_truncate_pending) {
  1866. spin_unlock(&ci->i_ceph_lock);
  1867. __ceph_do_pending_vmtruncate(inode);
  1868. spin_lock(&ci->i_ceph_lock);
  1869. }
  1870. have = __ceph_caps_issued(ci, &implemented);
  1871. if (have & need & CEPH_CAP_FILE_WR) {
  1872. if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
  1873. dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
  1874. inode, endoff, ci->i_max_size);
  1875. if (endoff > ci->i_requested_max_size) {
  1876. *check_max = 1;
  1877. ret = 1;
  1878. }
  1879. goto out_unlock;
  1880. }
  1881. /*
  1882. * If a sync write is in progress, we must wait, so that we
  1883. * can get a final snapshot value for size+mtime.
  1884. */
  1885. if (__ceph_have_pending_cap_snap(ci)) {
  1886. dout("get_cap_refs %p cap_snap_pending\n", inode);
  1887. goto out_unlock;
  1888. }
  1889. }
  1890. if ((have & need) == need) {
  1891. /*
  1892. * Look at (implemented & ~have & not) so that we keep waiting
  1893. * on transition from wanted -> needed caps. This is needed
  1894. * for WRBUFFER|WR -> WR to avoid a new WR sync write from
  1895. * going before a prior buffered writeback happens.
  1896. */
  1897. int not = want & ~(have & need);
  1898. int revoking = implemented & ~have;
  1899. dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
  1900. inode, ceph_cap_string(have), ceph_cap_string(not),
  1901. ceph_cap_string(revoking));
  1902. if ((revoking & not) == 0) {
  1903. *got = need | (have & want);
  1904. __take_cap_refs(ci, *got);
  1905. ret = 1;
  1906. }
  1907. } else {
  1908. int session_readonly = false;
  1909. if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
  1910. struct ceph_mds_session *s = ci->i_auth_cap->session;
  1911. spin_lock(&s->s_cap_lock);
  1912. session_readonly = s->s_readonly;
  1913. spin_unlock(&s->s_cap_lock);
  1914. }
  1915. if (session_readonly) {
  1916. dout("get_cap_refs %p needed %s but mds%d readonly\n",
  1917. inode, ceph_cap_string(need), ci->i_auth_cap->mds);
  1918. *err = -EROFS;
  1919. ret = 1;
  1920. goto out_unlock;
  1921. }
  1922. dout("get_cap_refs %p have %s needed %s\n", inode,
  1923. ceph_cap_string(have), ceph_cap_string(need));
  1924. }
  1925. out_unlock:
  1926. spin_unlock(&ci->i_ceph_lock);
  1927. dout("get_cap_refs %p ret %d got %s\n", inode,
  1928. ret, ceph_cap_string(*got));
  1929. return ret;
  1930. }
  1931. /*
  1932. * Check the offset we are writing up to against our current
  1933. * max_size. If necessary, tell the MDS we want to write to
  1934. * a larger offset.
  1935. */
  1936. static void check_max_size(struct inode *inode, loff_t endoff)
  1937. {
  1938. struct ceph_inode_info *ci = ceph_inode(inode);
  1939. int check = 0;
  1940. /* do we need to explicitly request a larger max_size? */
  1941. spin_lock(&ci->i_ceph_lock);
  1942. if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
  1943. dout("write %p at large endoff %llu, req max_size\n",
  1944. inode, endoff);
  1945. ci->i_wanted_max_size = endoff;
  1946. }
  1947. /* duplicate ceph_check_caps()'s logic */
  1948. if (ci->i_auth_cap &&
  1949. (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
  1950. ci->i_wanted_max_size > ci->i_max_size &&
  1951. ci->i_wanted_max_size > ci->i_requested_max_size)
  1952. check = 1;
  1953. spin_unlock(&ci->i_ceph_lock);
  1954. if (check)
  1955. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1956. }
  1957. /*
  1958. * Wait for caps, and take cap references. If we can't get a WR cap
  1959. * due to a small max_size, make sure we check_max_size (and possibly
  1960. * ask the mds) so we don't get hung up indefinitely.
  1961. */
  1962. int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
  1963. loff_t endoff, int *got, struct page **pinned_page)
  1964. {
  1965. int _got, check_max, ret, err = 0;
  1966. retry:
  1967. if (endoff > 0)
  1968. check_max_size(&ci->vfs_inode, endoff);
  1969. _got = 0;
  1970. check_max = 0;
  1971. ret = wait_event_interruptible(ci->i_cap_wq,
  1972. try_get_cap_refs(ci, need, want, endoff,
  1973. &_got, &check_max, &err));
  1974. if (err)
  1975. ret = err;
  1976. if (ret < 0)
  1977. return ret;
  1978. if (check_max)
  1979. goto retry;
  1980. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  1981. (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
  1982. i_size_read(&ci->vfs_inode) > 0) {
  1983. struct page *page = find_get_page(ci->vfs_inode.i_mapping, 0);
  1984. if (page) {
  1985. if (PageUptodate(page)) {
  1986. *pinned_page = page;
  1987. goto out;
  1988. }
  1989. page_cache_release(page);
  1990. }
  1991. /*
  1992. * drop cap refs first because getattr while holding
  1993. * caps refs can cause deadlock.
  1994. */
  1995. ceph_put_cap_refs(ci, _got);
  1996. _got = 0;
  1997. /* getattr request will bring inline data into page cache */
  1998. ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
  1999. CEPH_STAT_CAP_INLINE_DATA, true);
  2000. if (ret < 0)
  2001. return ret;
  2002. goto retry;
  2003. }
  2004. out:
  2005. *got = _got;
  2006. return 0;
  2007. }
  2008. /*
  2009. * Take cap refs. Caller must already know we hold at least one ref
  2010. * on the caps in question or we don't know this is safe.
  2011. */
  2012. void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
  2013. {
  2014. spin_lock(&ci->i_ceph_lock);
  2015. __take_cap_refs(ci, caps);
  2016. spin_unlock(&ci->i_ceph_lock);
  2017. }
  2018. /*
  2019. * Release cap refs.
  2020. *
  2021. * If we released the last ref on any given cap, call ceph_check_caps
  2022. * to release (or schedule a release).
  2023. *
  2024. * If we are releasing a WR cap (from a sync write), finalize any affected
  2025. * cap_snap, and wake up any waiters.
  2026. */
  2027. void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
  2028. {
  2029. struct inode *inode = &ci->vfs_inode;
  2030. int last = 0, put = 0, flushsnaps = 0, wake = 0;
  2031. struct ceph_cap_snap *capsnap;
  2032. spin_lock(&ci->i_ceph_lock);
  2033. if (had & CEPH_CAP_PIN)
  2034. --ci->i_pin_ref;
  2035. if (had & CEPH_CAP_FILE_RD)
  2036. if (--ci->i_rd_ref == 0)
  2037. last++;
  2038. if (had & CEPH_CAP_FILE_CACHE)
  2039. if (--ci->i_rdcache_ref == 0)
  2040. last++;
  2041. if (had & CEPH_CAP_FILE_BUFFER) {
  2042. if (--ci->i_wb_ref == 0) {
  2043. last++;
  2044. put++;
  2045. }
  2046. dout("put_cap_refs %p wb %d -> %d (?)\n",
  2047. inode, ci->i_wb_ref+1, ci->i_wb_ref);
  2048. }
  2049. if (had & CEPH_CAP_FILE_WR)
  2050. if (--ci->i_wr_ref == 0) {
  2051. last++;
  2052. if (!list_empty(&ci->i_cap_snaps)) {
  2053. capsnap = list_first_entry(&ci->i_cap_snaps,
  2054. struct ceph_cap_snap,
  2055. ci_item);
  2056. if (capsnap->writing) {
  2057. capsnap->writing = 0;
  2058. flushsnaps =
  2059. __ceph_finish_cap_snap(ci,
  2060. capsnap);
  2061. wake = 1;
  2062. }
  2063. }
  2064. }
  2065. spin_unlock(&ci->i_ceph_lock);
  2066. dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
  2067. last ? " last" : "", put ? " put" : "");
  2068. if (last && !flushsnaps)
  2069. ceph_check_caps(ci, 0, NULL);
  2070. else if (flushsnaps)
  2071. ceph_flush_snaps(ci);
  2072. if (wake)
  2073. wake_up_all(&ci->i_cap_wq);
  2074. if (put)
  2075. iput(inode);
  2076. }
  2077. /*
  2078. * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
  2079. * context. Adjust per-snap dirty page accounting as appropriate.
  2080. * Once all dirty data for a cap_snap is flushed, flush snapped file
  2081. * metadata back to the MDS. If we dropped the last ref, call
  2082. * ceph_check_caps.
  2083. */
  2084. void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
  2085. struct ceph_snap_context *snapc)
  2086. {
  2087. struct inode *inode = &ci->vfs_inode;
  2088. int last = 0;
  2089. int complete_capsnap = 0;
  2090. int drop_capsnap = 0;
  2091. int found = 0;
  2092. struct ceph_cap_snap *capsnap = NULL;
  2093. spin_lock(&ci->i_ceph_lock);
  2094. ci->i_wrbuffer_ref -= nr;
  2095. last = !ci->i_wrbuffer_ref;
  2096. if (ci->i_head_snapc == snapc) {
  2097. ci->i_wrbuffer_ref_head -= nr;
  2098. if (ci->i_wrbuffer_ref_head == 0 &&
  2099. ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) {
  2100. BUG_ON(!ci->i_head_snapc);
  2101. ceph_put_snap_context(ci->i_head_snapc);
  2102. ci->i_head_snapc = NULL;
  2103. }
  2104. dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
  2105. inode,
  2106. ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
  2107. ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  2108. last ? " LAST" : "");
  2109. } else {
  2110. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  2111. if (capsnap->context == snapc) {
  2112. found = 1;
  2113. break;
  2114. }
  2115. }
  2116. BUG_ON(!found);
  2117. capsnap->dirty_pages -= nr;
  2118. if (capsnap->dirty_pages == 0) {
  2119. complete_capsnap = 1;
  2120. if (capsnap->dirty == 0)
  2121. /* cap writeback completed before we created
  2122. * the cap_snap; no FLUSHSNAP is needed */
  2123. drop_capsnap = 1;
  2124. }
  2125. dout("put_wrbuffer_cap_refs on %p cap_snap %p "
  2126. " snap %lld %d/%d -> %d/%d %s%s%s\n",
  2127. inode, capsnap, capsnap->context->seq,
  2128. ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
  2129. ci->i_wrbuffer_ref, capsnap->dirty_pages,
  2130. last ? " (wrbuffer last)" : "",
  2131. complete_capsnap ? " (complete capsnap)" : "",
  2132. drop_capsnap ? " (drop capsnap)" : "");
  2133. if (drop_capsnap) {
  2134. ceph_put_snap_context(capsnap->context);
  2135. list_del(&capsnap->ci_item);
  2136. list_del(&capsnap->flushing_item);
  2137. ceph_put_cap_snap(capsnap);
  2138. }
  2139. }
  2140. spin_unlock(&ci->i_ceph_lock);
  2141. if (last) {
  2142. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  2143. iput(inode);
  2144. } else if (complete_capsnap) {
  2145. ceph_flush_snaps(ci);
  2146. wake_up_all(&ci->i_cap_wq);
  2147. }
  2148. if (drop_capsnap)
  2149. iput(inode);
  2150. }
  2151. /*
  2152. * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
  2153. */
  2154. static void invalidate_aliases(struct inode *inode)
  2155. {
  2156. struct dentry *dn, *prev = NULL;
  2157. dout("invalidate_aliases inode %p\n", inode);
  2158. d_prune_aliases(inode);
  2159. /*
  2160. * For non-directory inode, d_find_alias() only returns
  2161. * hashed dentry. After calling d_invalidate(), the
  2162. * dentry becomes unhashed.
  2163. *
  2164. * For directory inode, d_find_alias() can return
  2165. * unhashed dentry. But directory inode should have
  2166. * one alias at most.
  2167. */
  2168. while ((dn = d_find_alias(inode))) {
  2169. if (dn == prev) {
  2170. dput(dn);
  2171. break;
  2172. }
  2173. d_invalidate(dn);
  2174. if (prev)
  2175. dput(prev);
  2176. prev = dn;
  2177. }
  2178. if (prev)
  2179. dput(prev);
  2180. }
  2181. /*
  2182. * Handle a cap GRANT message from the MDS. (Note that a GRANT may
  2183. * actually be a revocation if it specifies a smaller cap set.)
  2184. *
  2185. * caller holds s_mutex and i_ceph_lock, we drop both.
  2186. */
  2187. static void handle_cap_grant(struct ceph_mds_client *mdsc,
  2188. struct inode *inode, struct ceph_mds_caps *grant,
  2189. u64 inline_version,
  2190. void *inline_data, int inline_len,
  2191. struct ceph_buffer *xattr_buf,
  2192. struct ceph_mds_session *session,
  2193. struct ceph_cap *cap, int issued)
  2194. __releases(ci->i_ceph_lock)
  2195. __releases(mdsc->snap_rwsem)
  2196. {
  2197. struct ceph_inode_info *ci = ceph_inode(inode);
  2198. int mds = session->s_mds;
  2199. int seq = le32_to_cpu(grant->seq);
  2200. int newcaps = le32_to_cpu(grant->caps);
  2201. int used, wanted, dirty;
  2202. u64 size = le64_to_cpu(grant->size);
  2203. u64 max_size = le64_to_cpu(grant->max_size);
  2204. struct timespec mtime, atime, ctime;
  2205. int check_caps = 0;
  2206. bool wake = false;
  2207. bool writeback = false;
  2208. bool queue_trunc = false;
  2209. bool queue_invalidate = false;
  2210. bool queue_revalidate = false;
  2211. bool deleted_inode = false;
  2212. bool fill_inline = false;
  2213. dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
  2214. inode, cap, mds, seq, ceph_cap_string(newcaps));
  2215. dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
  2216. inode->i_size);
  2217. /*
  2218. * auth mds of the inode changed. we received the cap export message,
  2219. * but still haven't received the cap import message. handle_cap_export
  2220. * updated the new auth MDS' cap.
  2221. *
  2222. * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
  2223. * that was sent before the cap import message. So don't remove caps.
  2224. */
  2225. if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  2226. WARN_ON(cap != ci->i_auth_cap);
  2227. WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
  2228. seq = cap->seq;
  2229. newcaps |= cap->issued;
  2230. }
  2231. /*
  2232. * If CACHE is being revoked, and we have no dirty buffers,
  2233. * try to invalidate (once). (If there are dirty buffers, we
  2234. * will invalidate _after_ writeback.)
  2235. */
  2236. if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
  2237. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  2238. !ci->i_wrbuffer_ref) {
  2239. if (try_nonblocking_invalidate(inode)) {
  2240. /* there were locked pages.. invalidate later
  2241. in a separate thread. */
  2242. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  2243. queue_invalidate = true;
  2244. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  2245. }
  2246. }
  2247. ceph_fscache_invalidate(inode);
  2248. }
  2249. /* side effects now are allowed */
  2250. cap->cap_gen = session->s_cap_gen;
  2251. cap->seq = seq;
  2252. __check_cap_issue(ci, cap, newcaps);
  2253. if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
  2254. (issued & CEPH_CAP_AUTH_EXCL) == 0) {
  2255. inode->i_mode = le32_to_cpu(grant->mode);
  2256. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
  2257. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
  2258. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  2259. from_kuid(&init_user_ns, inode->i_uid),
  2260. from_kgid(&init_user_ns, inode->i_gid));
  2261. }
  2262. if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
  2263. (issued & CEPH_CAP_LINK_EXCL) == 0) {
  2264. set_nlink(inode, le32_to_cpu(grant->nlink));
  2265. if (inode->i_nlink == 0 &&
  2266. (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
  2267. deleted_inode = true;
  2268. }
  2269. if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
  2270. int len = le32_to_cpu(grant->xattr_len);
  2271. u64 version = le64_to_cpu(grant->xattr_version);
  2272. if (version > ci->i_xattrs.version) {
  2273. dout(" got new xattrs v%llu on %p len %d\n",
  2274. version, inode, len);
  2275. if (ci->i_xattrs.blob)
  2276. ceph_buffer_put(ci->i_xattrs.blob);
  2277. ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
  2278. ci->i_xattrs.version = version;
  2279. ceph_forget_all_cached_acls(inode);
  2280. }
  2281. }
  2282. /* Do we need to revalidate our fscache cookie. Don't bother on the
  2283. * first cache cap as we already validate at cookie creation time. */
  2284. if ((issued & CEPH_CAP_FILE_CACHE) && ci->i_rdcache_gen > 1)
  2285. queue_revalidate = true;
  2286. if (newcaps & CEPH_CAP_ANY_RD) {
  2287. /* ctime/mtime/atime? */
  2288. ceph_decode_timespec(&mtime, &grant->mtime);
  2289. ceph_decode_timespec(&atime, &grant->atime);
  2290. ceph_decode_timespec(&ctime, &grant->ctime);
  2291. ceph_fill_file_time(inode, issued,
  2292. le32_to_cpu(grant->time_warp_seq),
  2293. &ctime, &mtime, &atime);
  2294. }
  2295. if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
  2296. /* file layout may have changed */
  2297. ci->i_layout = grant->layout;
  2298. /* size/truncate_seq? */
  2299. queue_trunc = ceph_fill_file_size(inode, issued,
  2300. le32_to_cpu(grant->truncate_seq),
  2301. le64_to_cpu(grant->truncate_size),
  2302. size);
  2303. /* max size increase? */
  2304. if (ci->i_auth_cap == cap && max_size != ci->i_max_size) {
  2305. dout("max_size %lld -> %llu\n",
  2306. ci->i_max_size, max_size);
  2307. ci->i_max_size = max_size;
  2308. if (max_size >= ci->i_wanted_max_size) {
  2309. ci->i_wanted_max_size = 0; /* reset */
  2310. ci->i_requested_max_size = 0;
  2311. }
  2312. wake = true;
  2313. }
  2314. }
  2315. /* check cap bits */
  2316. wanted = __ceph_caps_wanted(ci);
  2317. used = __ceph_caps_used(ci);
  2318. dirty = __ceph_caps_dirty(ci);
  2319. dout(" my wanted = %s, used = %s, dirty %s\n",
  2320. ceph_cap_string(wanted),
  2321. ceph_cap_string(used),
  2322. ceph_cap_string(dirty));
  2323. if (wanted != le32_to_cpu(grant->wanted)) {
  2324. dout("mds wanted %s -> %s\n",
  2325. ceph_cap_string(le32_to_cpu(grant->wanted)),
  2326. ceph_cap_string(wanted));
  2327. /* imported cap may not have correct mds_wanted */
  2328. if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
  2329. check_caps = 1;
  2330. }
  2331. /* revocation, grant, or no-op? */
  2332. if (cap->issued & ~newcaps) {
  2333. int revoking = cap->issued & ~newcaps;
  2334. dout("revocation: %s -> %s (revoking %s)\n",
  2335. ceph_cap_string(cap->issued),
  2336. ceph_cap_string(newcaps),
  2337. ceph_cap_string(revoking));
  2338. if (revoking & used & CEPH_CAP_FILE_BUFFER)
  2339. writeback = true; /* initiate writeback; will delay ack */
  2340. else if (revoking == CEPH_CAP_FILE_CACHE &&
  2341. (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
  2342. queue_invalidate)
  2343. ; /* do nothing yet, invalidation will be queued */
  2344. else if (cap == ci->i_auth_cap)
  2345. check_caps = 1; /* check auth cap only */
  2346. else
  2347. check_caps = 2; /* check all caps */
  2348. cap->issued = newcaps;
  2349. cap->implemented |= newcaps;
  2350. } else if (cap->issued == newcaps) {
  2351. dout("caps unchanged: %s -> %s\n",
  2352. ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
  2353. } else {
  2354. dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
  2355. ceph_cap_string(newcaps));
  2356. /* non-auth MDS is revoking the newly grant caps ? */
  2357. if (cap == ci->i_auth_cap &&
  2358. __ceph_caps_revoking_other(ci, cap, newcaps))
  2359. check_caps = 2;
  2360. cap->issued = newcaps;
  2361. cap->implemented |= newcaps; /* add bits only, to
  2362. * avoid stepping on a
  2363. * pending revocation */
  2364. wake = true;
  2365. }
  2366. BUG_ON(cap->issued & ~cap->implemented);
  2367. if (inline_version > 0 && inline_version >= ci->i_inline_version) {
  2368. ci->i_inline_version = inline_version;
  2369. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  2370. (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
  2371. fill_inline = true;
  2372. }
  2373. spin_unlock(&ci->i_ceph_lock);
  2374. if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
  2375. kick_flushing_inode_caps(mdsc, session, inode);
  2376. up_read(&mdsc->snap_rwsem);
  2377. if (newcaps & ~issued)
  2378. wake = true;
  2379. }
  2380. if (fill_inline)
  2381. ceph_fill_inline_data(inode, NULL, inline_data, inline_len);
  2382. if (queue_trunc) {
  2383. ceph_queue_vmtruncate(inode);
  2384. ceph_queue_revalidate(inode);
  2385. } else if (queue_revalidate)
  2386. ceph_queue_revalidate(inode);
  2387. if (writeback)
  2388. /*
  2389. * queue inode for writeback: we can't actually call
  2390. * filemap_write_and_wait, etc. from message handler
  2391. * context.
  2392. */
  2393. ceph_queue_writeback(inode);
  2394. if (queue_invalidate)
  2395. ceph_queue_invalidate(inode);
  2396. if (deleted_inode)
  2397. invalidate_aliases(inode);
  2398. if (wake)
  2399. wake_up_all(&ci->i_cap_wq);
  2400. if (check_caps == 1)
  2401. ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
  2402. session);
  2403. else if (check_caps == 2)
  2404. ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
  2405. else
  2406. mutex_unlock(&session->s_mutex);
  2407. }
  2408. /*
  2409. * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
  2410. * MDS has been safely committed.
  2411. */
  2412. static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
  2413. struct ceph_mds_caps *m,
  2414. struct ceph_mds_session *session,
  2415. struct ceph_cap *cap)
  2416. __releases(ci->i_ceph_lock)
  2417. {
  2418. struct ceph_inode_info *ci = ceph_inode(inode);
  2419. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  2420. unsigned seq = le32_to_cpu(m->seq);
  2421. int dirty = le32_to_cpu(m->dirty);
  2422. int cleaned = 0;
  2423. int drop = 0;
  2424. int i;
  2425. for (i = 0; i < CEPH_CAP_BITS; i++)
  2426. if ((dirty & (1 << i)) &&
  2427. (u16)flush_tid == ci->i_cap_flush_tid[i])
  2428. cleaned |= 1 << i;
  2429. dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
  2430. " flushing %s -> %s\n",
  2431. inode, session->s_mds, seq, ceph_cap_string(dirty),
  2432. ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
  2433. ceph_cap_string(ci->i_flushing_caps & ~cleaned));
  2434. if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
  2435. goto out;
  2436. ci->i_flushing_caps &= ~cleaned;
  2437. spin_lock(&mdsc->cap_dirty_lock);
  2438. if (ci->i_flushing_caps == 0) {
  2439. list_del_init(&ci->i_flushing_item);
  2440. if (!list_empty(&session->s_cap_flushing))
  2441. dout(" mds%d still flushing cap on %p\n",
  2442. session->s_mds,
  2443. &list_entry(session->s_cap_flushing.next,
  2444. struct ceph_inode_info,
  2445. i_flushing_item)->vfs_inode);
  2446. mdsc->num_cap_flushing--;
  2447. wake_up_all(&mdsc->cap_flushing_wq);
  2448. dout(" inode %p now !flushing\n", inode);
  2449. if (ci->i_dirty_caps == 0) {
  2450. dout(" inode %p now clean\n", inode);
  2451. BUG_ON(!list_empty(&ci->i_dirty_item));
  2452. drop = 1;
  2453. if (ci->i_wrbuffer_ref_head == 0) {
  2454. BUG_ON(!ci->i_head_snapc);
  2455. ceph_put_snap_context(ci->i_head_snapc);
  2456. ci->i_head_snapc = NULL;
  2457. }
  2458. } else {
  2459. BUG_ON(list_empty(&ci->i_dirty_item));
  2460. }
  2461. }
  2462. spin_unlock(&mdsc->cap_dirty_lock);
  2463. wake_up_all(&ci->i_cap_wq);
  2464. out:
  2465. spin_unlock(&ci->i_ceph_lock);
  2466. if (drop)
  2467. iput(inode);
  2468. }
  2469. /*
  2470. * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
  2471. * throw away our cap_snap.
  2472. *
  2473. * Caller hold s_mutex.
  2474. */
  2475. static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
  2476. struct ceph_mds_caps *m,
  2477. struct ceph_mds_session *session)
  2478. {
  2479. struct ceph_inode_info *ci = ceph_inode(inode);
  2480. u64 follows = le64_to_cpu(m->snap_follows);
  2481. struct ceph_cap_snap *capsnap;
  2482. int drop = 0;
  2483. dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
  2484. inode, ci, session->s_mds, follows);
  2485. spin_lock(&ci->i_ceph_lock);
  2486. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  2487. if (capsnap->follows == follows) {
  2488. if (capsnap->flush_tid != flush_tid) {
  2489. dout(" cap_snap %p follows %lld tid %lld !="
  2490. " %lld\n", capsnap, follows,
  2491. flush_tid, capsnap->flush_tid);
  2492. break;
  2493. }
  2494. WARN_ON(capsnap->dirty_pages || capsnap->writing);
  2495. dout(" removing %p cap_snap %p follows %lld\n",
  2496. inode, capsnap, follows);
  2497. ceph_put_snap_context(capsnap->context);
  2498. list_del(&capsnap->ci_item);
  2499. list_del(&capsnap->flushing_item);
  2500. ceph_put_cap_snap(capsnap);
  2501. drop = 1;
  2502. break;
  2503. } else {
  2504. dout(" skipping cap_snap %p follows %lld\n",
  2505. capsnap, capsnap->follows);
  2506. }
  2507. }
  2508. spin_unlock(&ci->i_ceph_lock);
  2509. if (drop)
  2510. iput(inode);
  2511. }
  2512. /*
  2513. * Handle TRUNC from MDS, indicating file truncation.
  2514. *
  2515. * caller hold s_mutex.
  2516. */
  2517. static void handle_cap_trunc(struct inode *inode,
  2518. struct ceph_mds_caps *trunc,
  2519. struct ceph_mds_session *session)
  2520. __releases(ci->i_ceph_lock)
  2521. {
  2522. struct ceph_inode_info *ci = ceph_inode(inode);
  2523. int mds = session->s_mds;
  2524. int seq = le32_to_cpu(trunc->seq);
  2525. u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
  2526. u64 truncate_size = le64_to_cpu(trunc->truncate_size);
  2527. u64 size = le64_to_cpu(trunc->size);
  2528. int implemented = 0;
  2529. int dirty = __ceph_caps_dirty(ci);
  2530. int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
  2531. int queue_trunc = 0;
  2532. issued |= implemented | dirty;
  2533. dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
  2534. inode, mds, seq, truncate_size, truncate_seq);
  2535. queue_trunc = ceph_fill_file_size(inode, issued,
  2536. truncate_seq, truncate_size, size);
  2537. spin_unlock(&ci->i_ceph_lock);
  2538. if (queue_trunc) {
  2539. ceph_queue_vmtruncate(inode);
  2540. ceph_fscache_invalidate(inode);
  2541. }
  2542. }
  2543. /*
  2544. * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
  2545. * different one. If we are the most recent migration we've seen (as
  2546. * indicated by mseq), make note of the migrating cap bits for the
  2547. * duration (until we see the corresponding IMPORT).
  2548. *
  2549. * caller holds s_mutex
  2550. */
  2551. static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
  2552. struct ceph_mds_cap_peer *ph,
  2553. struct ceph_mds_session *session)
  2554. {
  2555. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  2556. struct ceph_mds_session *tsession = NULL;
  2557. struct ceph_cap *cap, *tcap, *new_cap = NULL;
  2558. struct ceph_inode_info *ci = ceph_inode(inode);
  2559. u64 t_cap_id;
  2560. unsigned mseq = le32_to_cpu(ex->migrate_seq);
  2561. unsigned t_seq, t_mseq;
  2562. int target, issued;
  2563. int mds = session->s_mds;
  2564. if (ph) {
  2565. t_cap_id = le64_to_cpu(ph->cap_id);
  2566. t_seq = le32_to_cpu(ph->seq);
  2567. t_mseq = le32_to_cpu(ph->mseq);
  2568. target = le32_to_cpu(ph->mds);
  2569. } else {
  2570. t_cap_id = t_seq = t_mseq = 0;
  2571. target = -1;
  2572. }
  2573. dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
  2574. inode, ci, mds, mseq, target);
  2575. retry:
  2576. spin_lock(&ci->i_ceph_lock);
  2577. cap = __get_cap_for_mds(ci, mds);
  2578. if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
  2579. goto out_unlock;
  2580. if (target < 0) {
  2581. __ceph_remove_cap(cap, false);
  2582. goto out_unlock;
  2583. }
  2584. /*
  2585. * now we know we haven't received the cap import message yet
  2586. * because the exported cap still exist.
  2587. */
  2588. issued = cap->issued;
  2589. WARN_ON(issued != cap->implemented);
  2590. tcap = __get_cap_for_mds(ci, target);
  2591. if (tcap) {
  2592. /* already have caps from the target */
  2593. if (tcap->cap_id != t_cap_id ||
  2594. ceph_seq_cmp(tcap->seq, t_seq) < 0) {
  2595. dout(" updating import cap %p mds%d\n", tcap, target);
  2596. tcap->cap_id = t_cap_id;
  2597. tcap->seq = t_seq - 1;
  2598. tcap->issue_seq = t_seq - 1;
  2599. tcap->mseq = t_mseq;
  2600. tcap->issued |= issued;
  2601. tcap->implemented |= issued;
  2602. if (cap == ci->i_auth_cap)
  2603. ci->i_auth_cap = tcap;
  2604. if (ci->i_flushing_caps && ci->i_auth_cap == tcap) {
  2605. spin_lock(&mdsc->cap_dirty_lock);
  2606. list_move_tail(&ci->i_flushing_item,
  2607. &tcap->session->s_cap_flushing);
  2608. spin_unlock(&mdsc->cap_dirty_lock);
  2609. }
  2610. }
  2611. __ceph_remove_cap(cap, false);
  2612. goto out_unlock;
  2613. } else if (tsession) {
  2614. /* add placeholder for the export tagert */
  2615. int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
  2616. ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
  2617. t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
  2618. __ceph_remove_cap(cap, false);
  2619. goto out_unlock;
  2620. }
  2621. spin_unlock(&ci->i_ceph_lock);
  2622. mutex_unlock(&session->s_mutex);
  2623. /* open target session */
  2624. tsession = ceph_mdsc_open_export_target_session(mdsc, target);
  2625. if (!IS_ERR(tsession)) {
  2626. if (mds > target) {
  2627. mutex_lock(&session->s_mutex);
  2628. mutex_lock_nested(&tsession->s_mutex,
  2629. SINGLE_DEPTH_NESTING);
  2630. } else {
  2631. mutex_lock(&tsession->s_mutex);
  2632. mutex_lock_nested(&session->s_mutex,
  2633. SINGLE_DEPTH_NESTING);
  2634. }
  2635. ceph_add_cap_releases(mdsc, tsession);
  2636. new_cap = ceph_get_cap(mdsc, NULL);
  2637. } else {
  2638. WARN_ON(1);
  2639. tsession = NULL;
  2640. target = -1;
  2641. }
  2642. goto retry;
  2643. out_unlock:
  2644. spin_unlock(&ci->i_ceph_lock);
  2645. mutex_unlock(&session->s_mutex);
  2646. if (tsession) {
  2647. mutex_unlock(&tsession->s_mutex);
  2648. ceph_put_mds_session(tsession);
  2649. }
  2650. if (new_cap)
  2651. ceph_put_cap(mdsc, new_cap);
  2652. }
  2653. /*
  2654. * Handle cap IMPORT.
  2655. *
  2656. * caller holds s_mutex. acquires i_ceph_lock
  2657. */
  2658. static void handle_cap_import(struct ceph_mds_client *mdsc,
  2659. struct inode *inode, struct ceph_mds_caps *im,
  2660. struct ceph_mds_cap_peer *ph,
  2661. struct ceph_mds_session *session,
  2662. struct ceph_cap **target_cap, int *old_issued)
  2663. __acquires(ci->i_ceph_lock)
  2664. {
  2665. struct ceph_inode_info *ci = ceph_inode(inode);
  2666. struct ceph_cap *cap, *ocap, *new_cap = NULL;
  2667. int mds = session->s_mds;
  2668. int issued;
  2669. unsigned caps = le32_to_cpu(im->caps);
  2670. unsigned wanted = le32_to_cpu(im->wanted);
  2671. unsigned seq = le32_to_cpu(im->seq);
  2672. unsigned mseq = le32_to_cpu(im->migrate_seq);
  2673. u64 realmino = le64_to_cpu(im->realm);
  2674. u64 cap_id = le64_to_cpu(im->cap_id);
  2675. u64 p_cap_id;
  2676. int peer;
  2677. if (ph) {
  2678. p_cap_id = le64_to_cpu(ph->cap_id);
  2679. peer = le32_to_cpu(ph->mds);
  2680. } else {
  2681. p_cap_id = 0;
  2682. peer = -1;
  2683. }
  2684. dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
  2685. inode, ci, mds, mseq, peer);
  2686. retry:
  2687. spin_lock(&ci->i_ceph_lock);
  2688. cap = __get_cap_for_mds(ci, mds);
  2689. if (!cap) {
  2690. if (!new_cap) {
  2691. spin_unlock(&ci->i_ceph_lock);
  2692. new_cap = ceph_get_cap(mdsc, NULL);
  2693. goto retry;
  2694. }
  2695. cap = new_cap;
  2696. } else {
  2697. if (new_cap) {
  2698. ceph_put_cap(mdsc, new_cap);
  2699. new_cap = NULL;
  2700. }
  2701. }
  2702. __ceph_caps_issued(ci, &issued);
  2703. issued |= __ceph_caps_dirty(ci);
  2704. ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
  2705. realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
  2706. ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
  2707. if (ocap && ocap->cap_id == p_cap_id) {
  2708. dout(" remove export cap %p mds%d flags %d\n",
  2709. ocap, peer, ph->flags);
  2710. if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
  2711. (ocap->seq != le32_to_cpu(ph->seq) ||
  2712. ocap->mseq != le32_to_cpu(ph->mseq))) {
  2713. pr_err("handle_cap_import: mismatched seq/mseq: "
  2714. "ino (%llx.%llx) mds%d seq %d mseq %d "
  2715. "importer mds%d has peer seq %d mseq %d\n",
  2716. ceph_vinop(inode), peer, ocap->seq,
  2717. ocap->mseq, mds, le32_to_cpu(ph->seq),
  2718. le32_to_cpu(ph->mseq));
  2719. }
  2720. __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
  2721. }
  2722. /* make sure we re-request max_size, if necessary */
  2723. ci->i_wanted_max_size = 0;
  2724. ci->i_requested_max_size = 0;
  2725. *old_issued = issued;
  2726. *target_cap = cap;
  2727. }
  2728. /*
  2729. * Handle a caps message from the MDS.
  2730. *
  2731. * Identify the appropriate session, inode, and call the right handler
  2732. * based on the cap op.
  2733. */
  2734. void ceph_handle_caps(struct ceph_mds_session *session,
  2735. struct ceph_msg *msg)
  2736. {
  2737. struct ceph_mds_client *mdsc = session->s_mdsc;
  2738. struct super_block *sb = mdsc->fsc->sb;
  2739. struct inode *inode;
  2740. struct ceph_inode_info *ci;
  2741. struct ceph_cap *cap;
  2742. struct ceph_mds_caps *h;
  2743. struct ceph_mds_cap_peer *peer = NULL;
  2744. struct ceph_snap_realm *realm;
  2745. int mds = session->s_mds;
  2746. int op, issued;
  2747. u32 seq, mseq;
  2748. struct ceph_vino vino;
  2749. u64 cap_id;
  2750. u64 size, max_size;
  2751. u64 tid;
  2752. u64 inline_version = 0;
  2753. void *inline_data = NULL;
  2754. u32 inline_len = 0;
  2755. void *snaptrace;
  2756. size_t snaptrace_len;
  2757. void *p, *end;
  2758. dout("handle_caps from mds%d\n", mds);
  2759. /* decode */
  2760. end = msg->front.iov_base + msg->front.iov_len;
  2761. tid = le64_to_cpu(msg->hdr.tid);
  2762. if (msg->front.iov_len < sizeof(*h))
  2763. goto bad;
  2764. h = msg->front.iov_base;
  2765. op = le32_to_cpu(h->op);
  2766. vino.ino = le64_to_cpu(h->ino);
  2767. vino.snap = CEPH_NOSNAP;
  2768. cap_id = le64_to_cpu(h->cap_id);
  2769. seq = le32_to_cpu(h->seq);
  2770. mseq = le32_to_cpu(h->migrate_seq);
  2771. size = le64_to_cpu(h->size);
  2772. max_size = le64_to_cpu(h->max_size);
  2773. snaptrace = h + 1;
  2774. snaptrace_len = le32_to_cpu(h->snap_trace_len);
  2775. p = snaptrace + snaptrace_len;
  2776. if (le16_to_cpu(msg->hdr.version) >= 2) {
  2777. u32 flock_len;
  2778. ceph_decode_32_safe(&p, end, flock_len, bad);
  2779. if (p + flock_len > end)
  2780. goto bad;
  2781. p += flock_len;
  2782. }
  2783. if (le16_to_cpu(msg->hdr.version) >= 3) {
  2784. if (op == CEPH_CAP_OP_IMPORT) {
  2785. if (p + sizeof(*peer) > end)
  2786. goto bad;
  2787. peer = p;
  2788. p += sizeof(*peer);
  2789. } else if (op == CEPH_CAP_OP_EXPORT) {
  2790. /* recorded in unused fields */
  2791. peer = (void *)&h->size;
  2792. }
  2793. }
  2794. if (le16_to_cpu(msg->hdr.version) >= 4) {
  2795. ceph_decode_64_safe(&p, end, inline_version, bad);
  2796. ceph_decode_32_safe(&p, end, inline_len, bad);
  2797. if (p + inline_len > end)
  2798. goto bad;
  2799. inline_data = p;
  2800. p += inline_len;
  2801. }
  2802. /* lookup ino */
  2803. inode = ceph_find_inode(sb, vino);
  2804. ci = ceph_inode(inode);
  2805. dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
  2806. vino.snap, inode);
  2807. mutex_lock(&session->s_mutex);
  2808. session->s_seq++;
  2809. dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
  2810. (unsigned)seq);
  2811. if (op == CEPH_CAP_OP_IMPORT)
  2812. ceph_add_cap_releases(mdsc, session);
  2813. if (!inode) {
  2814. dout(" i don't have ino %llx\n", vino.ino);
  2815. if (op == CEPH_CAP_OP_IMPORT) {
  2816. spin_lock(&session->s_cap_lock);
  2817. __queue_cap_release(session, vino.ino, cap_id,
  2818. mseq, seq);
  2819. spin_unlock(&session->s_cap_lock);
  2820. }
  2821. goto flush_cap_releases;
  2822. }
  2823. /* these will work even if we don't have a cap yet */
  2824. switch (op) {
  2825. case CEPH_CAP_OP_FLUSHSNAP_ACK:
  2826. handle_cap_flushsnap_ack(inode, tid, h, session);
  2827. goto done;
  2828. case CEPH_CAP_OP_EXPORT:
  2829. handle_cap_export(inode, h, peer, session);
  2830. goto done_unlocked;
  2831. case CEPH_CAP_OP_IMPORT:
  2832. realm = NULL;
  2833. if (snaptrace_len) {
  2834. down_write(&mdsc->snap_rwsem);
  2835. ceph_update_snap_trace(mdsc, snaptrace,
  2836. snaptrace + snaptrace_len,
  2837. false, &realm);
  2838. downgrade_write(&mdsc->snap_rwsem);
  2839. } else {
  2840. down_read(&mdsc->snap_rwsem);
  2841. }
  2842. handle_cap_import(mdsc, inode, h, peer, session,
  2843. &cap, &issued);
  2844. handle_cap_grant(mdsc, inode, h,
  2845. inline_version, inline_data, inline_len,
  2846. msg->middle, session, cap, issued);
  2847. if (realm)
  2848. ceph_put_snap_realm(mdsc, realm);
  2849. goto done_unlocked;
  2850. }
  2851. /* the rest require a cap */
  2852. spin_lock(&ci->i_ceph_lock);
  2853. cap = __get_cap_for_mds(ceph_inode(inode), mds);
  2854. if (!cap) {
  2855. dout(" no cap on %p ino %llx.%llx from mds%d\n",
  2856. inode, ceph_ino(inode), ceph_snap(inode), mds);
  2857. spin_unlock(&ci->i_ceph_lock);
  2858. goto flush_cap_releases;
  2859. }
  2860. /* note that each of these drops i_ceph_lock for us */
  2861. switch (op) {
  2862. case CEPH_CAP_OP_REVOKE:
  2863. case CEPH_CAP_OP_GRANT:
  2864. __ceph_caps_issued(ci, &issued);
  2865. issued |= __ceph_caps_dirty(ci);
  2866. handle_cap_grant(mdsc, inode, h,
  2867. inline_version, inline_data, inline_len,
  2868. msg->middle, session, cap, issued);
  2869. goto done_unlocked;
  2870. case CEPH_CAP_OP_FLUSH_ACK:
  2871. handle_cap_flush_ack(inode, tid, h, session, cap);
  2872. break;
  2873. case CEPH_CAP_OP_TRUNC:
  2874. handle_cap_trunc(inode, h, session);
  2875. break;
  2876. default:
  2877. spin_unlock(&ci->i_ceph_lock);
  2878. pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
  2879. ceph_cap_op_name(op));
  2880. }
  2881. goto done;
  2882. flush_cap_releases:
  2883. /*
  2884. * send any full release message to try to move things
  2885. * along for the mds (who clearly thinks we still have this
  2886. * cap).
  2887. */
  2888. ceph_add_cap_releases(mdsc, session);
  2889. ceph_send_cap_releases(mdsc, session);
  2890. done:
  2891. mutex_unlock(&session->s_mutex);
  2892. done_unlocked:
  2893. iput(inode);
  2894. return;
  2895. bad:
  2896. pr_err("ceph_handle_caps: corrupt message\n");
  2897. ceph_msg_dump(msg);
  2898. return;
  2899. }
  2900. /*
  2901. * Delayed work handler to process end of delayed cap release LRU list.
  2902. */
  2903. void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
  2904. {
  2905. struct ceph_inode_info *ci;
  2906. int flags = CHECK_CAPS_NODELAY;
  2907. dout("check_delayed_caps\n");
  2908. while (1) {
  2909. spin_lock(&mdsc->cap_delay_lock);
  2910. if (list_empty(&mdsc->cap_delay_list))
  2911. break;
  2912. ci = list_first_entry(&mdsc->cap_delay_list,
  2913. struct ceph_inode_info,
  2914. i_cap_delay_list);
  2915. if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
  2916. time_before(jiffies, ci->i_hold_caps_max))
  2917. break;
  2918. list_del_init(&ci->i_cap_delay_list);
  2919. spin_unlock(&mdsc->cap_delay_lock);
  2920. dout("check_delayed_caps on %p\n", &ci->vfs_inode);
  2921. ceph_check_caps(ci, flags, NULL);
  2922. }
  2923. spin_unlock(&mdsc->cap_delay_lock);
  2924. }
  2925. /*
  2926. * Flush all dirty caps to the mds
  2927. */
  2928. void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
  2929. {
  2930. struct ceph_inode_info *ci;
  2931. struct inode *inode;
  2932. dout("flush_dirty_caps\n");
  2933. spin_lock(&mdsc->cap_dirty_lock);
  2934. while (!list_empty(&mdsc->cap_dirty)) {
  2935. ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
  2936. i_dirty_item);
  2937. inode = &ci->vfs_inode;
  2938. ihold(inode);
  2939. dout("flush_dirty_caps %p\n", inode);
  2940. spin_unlock(&mdsc->cap_dirty_lock);
  2941. ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
  2942. iput(inode);
  2943. spin_lock(&mdsc->cap_dirty_lock);
  2944. }
  2945. spin_unlock(&mdsc->cap_dirty_lock);
  2946. dout("flush_dirty_caps done\n");
  2947. }
  2948. /*
  2949. * Drop open file reference. If we were the last open file,
  2950. * we may need to release capabilities to the MDS (or schedule
  2951. * their delayed release).
  2952. */
  2953. void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
  2954. {
  2955. struct inode *inode = &ci->vfs_inode;
  2956. int last = 0;
  2957. spin_lock(&ci->i_ceph_lock);
  2958. dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
  2959. ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
  2960. BUG_ON(ci->i_nr_by_mode[fmode] == 0);
  2961. if (--ci->i_nr_by_mode[fmode] == 0)
  2962. last++;
  2963. spin_unlock(&ci->i_ceph_lock);
  2964. if (last && ci->i_vino.snap == CEPH_NOSNAP)
  2965. ceph_check_caps(ci, 0, NULL);
  2966. }
  2967. /*
  2968. * Helpers for embedding cap and dentry lease releases into mds
  2969. * requests.
  2970. *
  2971. * @force is used by dentry_release (below) to force inclusion of a
  2972. * record for the directory inode, even when there aren't any caps to
  2973. * drop.
  2974. */
  2975. int ceph_encode_inode_release(void **p, struct inode *inode,
  2976. int mds, int drop, int unless, int force)
  2977. {
  2978. struct ceph_inode_info *ci = ceph_inode(inode);
  2979. struct ceph_cap *cap;
  2980. struct ceph_mds_request_release *rel = *p;
  2981. int used, dirty;
  2982. int ret = 0;
  2983. spin_lock(&ci->i_ceph_lock);
  2984. used = __ceph_caps_used(ci);
  2985. dirty = __ceph_caps_dirty(ci);
  2986. dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
  2987. inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
  2988. ceph_cap_string(unless));
  2989. /* only drop unused, clean caps */
  2990. drop &= ~(used | dirty);
  2991. cap = __get_cap_for_mds(ci, mds);
  2992. if (cap && __cap_is_valid(cap)) {
  2993. if (force ||
  2994. ((cap->issued & drop) &&
  2995. (cap->issued & unless) == 0)) {
  2996. if ((cap->issued & drop) &&
  2997. (cap->issued & unless) == 0) {
  2998. int wanted = __ceph_caps_wanted(ci);
  2999. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
  3000. wanted |= cap->mds_wanted;
  3001. dout("encode_inode_release %p cap %p "
  3002. "%s -> %s, wanted %s -> %s\n", inode, cap,
  3003. ceph_cap_string(cap->issued),
  3004. ceph_cap_string(cap->issued & ~drop),
  3005. ceph_cap_string(cap->mds_wanted),
  3006. ceph_cap_string(wanted));
  3007. cap->issued &= ~drop;
  3008. cap->implemented &= ~drop;
  3009. cap->mds_wanted = wanted;
  3010. } else {
  3011. dout("encode_inode_release %p cap %p %s"
  3012. " (force)\n", inode, cap,
  3013. ceph_cap_string(cap->issued));
  3014. }
  3015. rel->ino = cpu_to_le64(ceph_ino(inode));
  3016. rel->cap_id = cpu_to_le64(cap->cap_id);
  3017. rel->seq = cpu_to_le32(cap->seq);
  3018. rel->issue_seq = cpu_to_le32(cap->issue_seq);
  3019. rel->mseq = cpu_to_le32(cap->mseq);
  3020. rel->caps = cpu_to_le32(cap->implemented);
  3021. rel->wanted = cpu_to_le32(cap->mds_wanted);
  3022. rel->dname_len = 0;
  3023. rel->dname_seq = 0;
  3024. *p += sizeof(*rel);
  3025. ret = 1;
  3026. } else {
  3027. dout("encode_inode_release %p cap %p %s\n",
  3028. inode, cap, ceph_cap_string(cap->issued));
  3029. }
  3030. }
  3031. spin_unlock(&ci->i_ceph_lock);
  3032. return ret;
  3033. }
  3034. int ceph_encode_dentry_release(void **p, struct dentry *dentry,
  3035. int mds, int drop, int unless)
  3036. {
  3037. struct inode *dir = dentry->d_parent->d_inode;
  3038. struct ceph_mds_request_release *rel = *p;
  3039. struct ceph_dentry_info *di = ceph_dentry(dentry);
  3040. int force = 0;
  3041. int ret;
  3042. /*
  3043. * force an record for the directory caps if we have a dentry lease.
  3044. * this is racy (can't take i_ceph_lock and d_lock together), but it
  3045. * doesn't have to be perfect; the mds will revoke anything we don't
  3046. * release.
  3047. */
  3048. spin_lock(&dentry->d_lock);
  3049. if (di->lease_session && di->lease_session->s_mds == mds)
  3050. force = 1;
  3051. spin_unlock(&dentry->d_lock);
  3052. ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
  3053. spin_lock(&dentry->d_lock);
  3054. if (ret && di->lease_session && di->lease_session->s_mds == mds) {
  3055. dout("encode_dentry_release %p mds%d seq %d\n",
  3056. dentry, mds, (int)di->lease_seq);
  3057. rel->dname_len = cpu_to_le32(dentry->d_name.len);
  3058. memcpy(*p, dentry->d_name.name, dentry->d_name.len);
  3059. *p += dentry->d_name.len;
  3060. rel->dname_seq = cpu_to_le32(di->lease_seq);
  3061. __ceph_mdsc_drop_dentry_lease(dentry);
  3062. }
  3063. spin_unlock(&dentry->d_lock);
  3064. return ret;
  3065. }