caps.c 84 KB

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