caps.c 90 KB

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