caps.c 78 KB

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