caps.c 93 KB

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