caps.c 80 KB

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