caps.c 81 KB

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