caps.c 81 KB

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