caps.c 87 KB

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