caps.c 79 KB

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