caps.c 87 KB

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