caps.c 85 KB

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