caps.c 91 KB

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