caps.c 85 KB

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