caps.c 83 KB

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