mds_client.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060
  1. #include "ceph_debug.h"
  2. #include <linux/wait.h>
  3. #include <linux/slab.h>
  4. #include <linux/sched.h>
  5. #include "mds_client.h"
  6. #include "mon_client.h"
  7. #include "super.h"
  8. #include "messenger.h"
  9. #include "decode.h"
  10. #include "auth.h"
  11. #include "pagelist.h"
  12. /*
  13. * A cluster of MDS (metadata server) daemons is responsible for
  14. * managing the file system namespace (the directory hierarchy and
  15. * inodes) and for coordinating shared access to storage. Metadata is
  16. * partitioning hierarchically across a number of servers, and that
  17. * partition varies over time as the cluster adjusts the distribution
  18. * in order to balance load.
  19. *
  20. * The MDS client is primarily responsible to managing synchronous
  21. * metadata requests for operations like open, unlink, and so forth.
  22. * If there is a MDS failure, we find out about it when we (possibly
  23. * request and) receive a new MDS map, and can resubmit affected
  24. * requests.
  25. *
  26. * For the most part, though, we take advantage of a lossless
  27. * communications channel to the MDS, and do not need to worry about
  28. * timing out or resubmitting requests.
  29. *
  30. * We maintain a stateful "session" with each MDS we interact with.
  31. * Within each session, we sent periodic heartbeat messages to ensure
  32. * any capabilities or leases we have been issues remain valid. If
  33. * the session times out and goes stale, our leases and capabilities
  34. * are no longer valid.
  35. */
  36. static void __wake_requests(struct ceph_mds_client *mdsc,
  37. struct list_head *head);
  38. const static struct ceph_connection_operations mds_con_ops;
  39. /*
  40. * mds reply parsing
  41. */
  42. /*
  43. * parse individual inode info
  44. */
  45. static int parse_reply_info_in(void **p, void *end,
  46. struct ceph_mds_reply_info_in *info)
  47. {
  48. int err = -EIO;
  49. info->in = *p;
  50. *p += sizeof(struct ceph_mds_reply_inode) +
  51. sizeof(*info->in->fragtree.splits) *
  52. le32_to_cpu(info->in->fragtree.nsplits);
  53. ceph_decode_32_safe(p, end, info->symlink_len, bad);
  54. ceph_decode_need(p, end, info->symlink_len, bad);
  55. info->symlink = *p;
  56. *p += info->symlink_len;
  57. ceph_decode_32_safe(p, end, info->xattr_len, bad);
  58. ceph_decode_need(p, end, info->xattr_len, bad);
  59. info->xattr_data = *p;
  60. *p += info->xattr_len;
  61. return 0;
  62. bad:
  63. return err;
  64. }
  65. /*
  66. * parse a normal reply, which may contain a (dir+)dentry and/or a
  67. * target inode.
  68. */
  69. static int parse_reply_info_trace(void **p, void *end,
  70. struct ceph_mds_reply_info_parsed *info)
  71. {
  72. int err;
  73. if (info->head->is_dentry) {
  74. err = parse_reply_info_in(p, end, &info->diri);
  75. if (err < 0)
  76. goto out_bad;
  77. if (unlikely(*p + sizeof(*info->dirfrag) > end))
  78. goto bad;
  79. info->dirfrag = *p;
  80. *p += sizeof(*info->dirfrag) +
  81. sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
  82. if (unlikely(*p > end))
  83. goto bad;
  84. ceph_decode_32_safe(p, end, info->dname_len, bad);
  85. ceph_decode_need(p, end, info->dname_len, bad);
  86. info->dname = *p;
  87. *p += info->dname_len;
  88. info->dlease = *p;
  89. *p += sizeof(*info->dlease);
  90. }
  91. if (info->head->is_target) {
  92. err = parse_reply_info_in(p, end, &info->targeti);
  93. if (err < 0)
  94. goto out_bad;
  95. }
  96. if (unlikely(*p != end))
  97. goto bad;
  98. return 0;
  99. bad:
  100. err = -EIO;
  101. out_bad:
  102. pr_err("problem parsing mds trace %d\n", err);
  103. return err;
  104. }
  105. /*
  106. * parse readdir results
  107. */
  108. static int parse_reply_info_dir(void **p, void *end,
  109. struct ceph_mds_reply_info_parsed *info)
  110. {
  111. u32 num, i = 0;
  112. int err;
  113. info->dir_dir = *p;
  114. if (*p + sizeof(*info->dir_dir) > end)
  115. goto bad;
  116. *p += sizeof(*info->dir_dir) +
  117. sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
  118. if (*p > end)
  119. goto bad;
  120. ceph_decode_need(p, end, sizeof(num) + 2, bad);
  121. num = ceph_decode_32(p);
  122. info->dir_end = ceph_decode_8(p);
  123. info->dir_complete = ceph_decode_8(p);
  124. if (num == 0)
  125. goto done;
  126. /* alloc large array */
  127. info->dir_nr = num;
  128. info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
  129. sizeof(*info->dir_dname) +
  130. sizeof(*info->dir_dname_len) +
  131. sizeof(*info->dir_dlease),
  132. GFP_NOFS);
  133. if (info->dir_in == NULL) {
  134. err = -ENOMEM;
  135. goto out_bad;
  136. }
  137. info->dir_dname = (void *)(info->dir_in + num);
  138. info->dir_dname_len = (void *)(info->dir_dname + num);
  139. info->dir_dlease = (void *)(info->dir_dname_len + num);
  140. while (num) {
  141. /* dentry */
  142. ceph_decode_need(p, end, sizeof(u32)*2, bad);
  143. info->dir_dname_len[i] = ceph_decode_32(p);
  144. ceph_decode_need(p, end, info->dir_dname_len[i], bad);
  145. info->dir_dname[i] = *p;
  146. *p += info->dir_dname_len[i];
  147. dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
  148. info->dir_dname[i]);
  149. info->dir_dlease[i] = *p;
  150. *p += sizeof(struct ceph_mds_reply_lease);
  151. /* inode */
  152. err = parse_reply_info_in(p, end, &info->dir_in[i]);
  153. if (err < 0)
  154. goto out_bad;
  155. i++;
  156. num--;
  157. }
  158. done:
  159. if (*p != end)
  160. goto bad;
  161. return 0;
  162. bad:
  163. err = -EIO;
  164. out_bad:
  165. pr_err("problem parsing dir contents %d\n", err);
  166. return err;
  167. }
  168. /*
  169. * parse entire mds reply
  170. */
  171. static int parse_reply_info(struct ceph_msg *msg,
  172. struct ceph_mds_reply_info_parsed *info)
  173. {
  174. void *p, *end;
  175. u32 len;
  176. int err;
  177. info->head = msg->front.iov_base;
  178. p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
  179. end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
  180. /* trace */
  181. ceph_decode_32_safe(&p, end, len, bad);
  182. if (len > 0) {
  183. err = parse_reply_info_trace(&p, p+len, info);
  184. if (err < 0)
  185. goto out_bad;
  186. }
  187. /* dir content */
  188. ceph_decode_32_safe(&p, end, len, bad);
  189. if (len > 0) {
  190. err = parse_reply_info_dir(&p, p+len, info);
  191. if (err < 0)
  192. goto out_bad;
  193. }
  194. /* snap blob */
  195. ceph_decode_32_safe(&p, end, len, bad);
  196. info->snapblob_len = len;
  197. info->snapblob = p;
  198. p += len;
  199. if (p != end)
  200. goto bad;
  201. return 0;
  202. bad:
  203. err = -EIO;
  204. out_bad:
  205. pr_err("mds parse_reply err %d\n", err);
  206. return err;
  207. }
  208. static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
  209. {
  210. kfree(info->dir_in);
  211. }
  212. /*
  213. * sessions
  214. */
  215. static const char *session_state_name(int s)
  216. {
  217. switch (s) {
  218. case CEPH_MDS_SESSION_NEW: return "new";
  219. case CEPH_MDS_SESSION_OPENING: return "opening";
  220. case CEPH_MDS_SESSION_OPEN: return "open";
  221. case CEPH_MDS_SESSION_HUNG: return "hung";
  222. case CEPH_MDS_SESSION_CLOSING: return "closing";
  223. case CEPH_MDS_SESSION_RESTARTING: return "restarting";
  224. case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
  225. default: return "???";
  226. }
  227. }
  228. static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
  229. {
  230. if (atomic_inc_not_zero(&s->s_ref)) {
  231. dout("mdsc get_session %p %d -> %d\n", s,
  232. atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
  233. return s;
  234. } else {
  235. dout("mdsc get_session %p 0 -- FAIL", s);
  236. return NULL;
  237. }
  238. }
  239. void ceph_put_mds_session(struct ceph_mds_session *s)
  240. {
  241. dout("mdsc put_session %p %d -> %d\n", s,
  242. atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
  243. if (atomic_dec_and_test(&s->s_ref)) {
  244. if (s->s_authorizer)
  245. s->s_mdsc->client->monc.auth->ops->destroy_authorizer(
  246. s->s_mdsc->client->monc.auth, s->s_authorizer);
  247. kfree(s);
  248. }
  249. }
  250. /*
  251. * called under mdsc->mutex
  252. */
  253. struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
  254. int mds)
  255. {
  256. struct ceph_mds_session *session;
  257. if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
  258. return NULL;
  259. session = mdsc->sessions[mds];
  260. dout("lookup_mds_session %p %d\n", session,
  261. atomic_read(&session->s_ref));
  262. get_session(session);
  263. return session;
  264. }
  265. static bool __have_session(struct ceph_mds_client *mdsc, int mds)
  266. {
  267. if (mds >= mdsc->max_sessions)
  268. return false;
  269. return mdsc->sessions[mds];
  270. }
  271. static int __verify_registered_session(struct ceph_mds_client *mdsc,
  272. struct ceph_mds_session *s)
  273. {
  274. if (s->s_mds >= mdsc->max_sessions ||
  275. mdsc->sessions[s->s_mds] != s)
  276. return -ENOENT;
  277. return 0;
  278. }
  279. /*
  280. * create+register a new session for given mds.
  281. * called under mdsc->mutex.
  282. */
  283. static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
  284. int mds)
  285. {
  286. struct ceph_mds_session *s;
  287. s = kzalloc(sizeof(*s), GFP_NOFS);
  288. if (!s)
  289. return ERR_PTR(-ENOMEM);
  290. s->s_mdsc = mdsc;
  291. s->s_mds = mds;
  292. s->s_state = CEPH_MDS_SESSION_NEW;
  293. s->s_ttl = 0;
  294. s->s_seq = 0;
  295. mutex_init(&s->s_mutex);
  296. ceph_con_init(mdsc->client->msgr, &s->s_con);
  297. s->s_con.private = s;
  298. s->s_con.ops = &mds_con_ops;
  299. s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
  300. s->s_con.peer_name.num = cpu_to_le64(mds);
  301. spin_lock_init(&s->s_cap_lock);
  302. s->s_cap_gen = 0;
  303. s->s_cap_ttl = 0;
  304. s->s_renew_requested = 0;
  305. s->s_renew_seq = 0;
  306. INIT_LIST_HEAD(&s->s_caps);
  307. s->s_nr_caps = 0;
  308. s->s_trim_caps = 0;
  309. atomic_set(&s->s_ref, 1);
  310. INIT_LIST_HEAD(&s->s_waiting);
  311. INIT_LIST_HEAD(&s->s_unsafe);
  312. s->s_num_cap_releases = 0;
  313. s->s_cap_iterator = NULL;
  314. INIT_LIST_HEAD(&s->s_cap_releases);
  315. INIT_LIST_HEAD(&s->s_cap_releases_done);
  316. INIT_LIST_HEAD(&s->s_cap_flushing);
  317. INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
  318. dout("register_session mds%d\n", mds);
  319. if (mds >= mdsc->max_sessions) {
  320. int newmax = 1 << get_count_order(mds+1);
  321. struct ceph_mds_session **sa;
  322. dout("register_session realloc to %d\n", newmax);
  323. sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
  324. if (sa == NULL)
  325. goto fail_realloc;
  326. if (mdsc->sessions) {
  327. memcpy(sa, mdsc->sessions,
  328. mdsc->max_sessions * sizeof(void *));
  329. kfree(mdsc->sessions);
  330. }
  331. mdsc->sessions = sa;
  332. mdsc->max_sessions = newmax;
  333. }
  334. mdsc->sessions[mds] = s;
  335. atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
  336. ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
  337. return s;
  338. fail_realloc:
  339. kfree(s);
  340. return ERR_PTR(-ENOMEM);
  341. }
  342. /*
  343. * called under mdsc->mutex
  344. */
  345. static void __unregister_session(struct ceph_mds_client *mdsc,
  346. struct ceph_mds_session *s)
  347. {
  348. dout("__unregister_session mds%d %p\n", s->s_mds, s);
  349. BUG_ON(mdsc->sessions[s->s_mds] != s);
  350. mdsc->sessions[s->s_mds] = NULL;
  351. ceph_con_close(&s->s_con);
  352. ceph_put_mds_session(s);
  353. }
  354. /*
  355. * drop session refs in request.
  356. *
  357. * should be last request ref, or hold mdsc->mutex
  358. */
  359. static void put_request_session(struct ceph_mds_request *req)
  360. {
  361. if (req->r_session) {
  362. ceph_put_mds_session(req->r_session);
  363. req->r_session = NULL;
  364. }
  365. }
  366. void ceph_mdsc_release_request(struct kref *kref)
  367. {
  368. struct ceph_mds_request *req = container_of(kref,
  369. struct ceph_mds_request,
  370. r_kref);
  371. if (req->r_request)
  372. ceph_msg_put(req->r_request);
  373. if (req->r_reply) {
  374. ceph_msg_put(req->r_reply);
  375. destroy_reply_info(&req->r_reply_info);
  376. }
  377. if (req->r_inode) {
  378. ceph_put_cap_refs(ceph_inode(req->r_inode),
  379. CEPH_CAP_PIN);
  380. iput(req->r_inode);
  381. }
  382. if (req->r_locked_dir)
  383. ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
  384. CEPH_CAP_PIN);
  385. if (req->r_target_inode)
  386. iput(req->r_target_inode);
  387. if (req->r_dentry)
  388. dput(req->r_dentry);
  389. if (req->r_old_dentry) {
  390. ceph_put_cap_refs(
  391. ceph_inode(req->r_old_dentry->d_parent->d_inode),
  392. CEPH_CAP_PIN);
  393. dput(req->r_old_dentry);
  394. }
  395. kfree(req->r_path1);
  396. kfree(req->r_path2);
  397. put_request_session(req);
  398. ceph_unreserve_caps(&req->r_caps_reservation);
  399. kfree(req);
  400. }
  401. /*
  402. * lookup session, bump ref if found.
  403. *
  404. * called under mdsc->mutex.
  405. */
  406. static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
  407. u64 tid)
  408. {
  409. struct ceph_mds_request *req;
  410. struct rb_node *n = mdsc->request_tree.rb_node;
  411. while (n) {
  412. req = rb_entry(n, struct ceph_mds_request, r_node);
  413. if (tid < req->r_tid)
  414. n = n->rb_left;
  415. else if (tid > req->r_tid)
  416. n = n->rb_right;
  417. else {
  418. ceph_mdsc_get_request(req);
  419. return req;
  420. }
  421. }
  422. return NULL;
  423. }
  424. static void __insert_request(struct ceph_mds_client *mdsc,
  425. struct ceph_mds_request *new)
  426. {
  427. struct rb_node **p = &mdsc->request_tree.rb_node;
  428. struct rb_node *parent = NULL;
  429. struct ceph_mds_request *req = NULL;
  430. while (*p) {
  431. parent = *p;
  432. req = rb_entry(parent, struct ceph_mds_request, r_node);
  433. if (new->r_tid < req->r_tid)
  434. p = &(*p)->rb_left;
  435. else if (new->r_tid > req->r_tid)
  436. p = &(*p)->rb_right;
  437. else
  438. BUG();
  439. }
  440. rb_link_node(&new->r_node, parent, p);
  441. rb_insert_color(&new->r_node, &mdsc->request_tree);
  442. }
  443. /*
  444. * Register an in-flight request, and assign a tid. Link to directory
  445. * are modifying (if any).
  446. *
  447. * Called under mdsc->mutex.
  448. */
  449. static void __register_request(struct ceph_mds_client *mdsc,
  450. struct ceph_mds_request *req,
  451. struct inode *dir)
  452. {
  453. req->r_tid = ++mdsc->last_tid;
  454. if (req->r_num_caps)
  455. ceph_reserve_caps(&req->r_caps_reservation, req->r_num_caps);
  456. dout("__register_request %p tid %lld\n", req, req->r_tid);
  457. ceph_mdsc_get_request(req);
  458. __insert_request(mdsc, req);
  459. if (dir) {
  460. struct ceph_inode_info *ci = ceph_inode(dir);
  461. spin_lock(&ci->i_unsafe_lock);
  462. req->r_unsafe_dir = dir;
  463. list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
  464. spin_unlock(&ci->i_unsafe_lock);
  465. }
  466. }
  467. static void __unregister_request(struct ceph_mds_client *mdsc,
  468. struct ceph_mds_request *req)
  469. {
  470. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  471. rb_erase(&req->r_node, &mdsc->request_tree);
  472. RB_CLEAR_NODE(&req->r_node);
  473. if (req->r_unsafe_dir) {
  474. struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
  475. spin_lock(&ci->i_unsafe_lock);
  476. list_del_init(&req->r_unsafe_dir_item);
  477. spin_unlock(&ci->i_unsafe_lock);
  478. }
  479. ceph_mdsc_put_request(req);
  480. }
  481. /*
  482. * Choose mds to send request to next. If there is a hint set in the
  483. * request (e.g., due to a prior forward hint from the mds), use that.
  484. * Otherwise, consult frag tree and/or caps to identify the
  485. * appropriate mds. If all else fails, choose randomly.
  486. *
  487. * Called under mdsc->mutex.
  488. */
  489. static int __choose_mds(struct ceph_mds_client *mdsc,
  490. struct ceph_mds_request *req)
  491. {
  492. struct inode *inode;
  493. struct ceph_inode_info *ci;
  494. struct ceph_cap *cap;
  495. int mode = req->r_direct_mode;
  496. int mds = -1;
  497. u32 hash = req->r_direct_hash;
  498. bool is_hash = req->r_direct_is_hash;
  499. /*
  500. * is there a specific mds we should try? ignore hint if we have
  501. * no session and the mds is not up (active or recovering).
  502. */
  503. if (req->r_resend_mds >= 0 &&
  504. (__have_session(mdsc, req->r_resend_mds) ||
  505. ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
  506. dout("choose_mds using resend_mds mds%d\n",
  507. req->r_resend_mds);
  508. return req->r_resend_mds;
  509. }
  510. if (mode == USE_RANDOM_MDS)
  511. goto random;
  512. inode = NULL;
  513. if (req->r_inode) {
  514. inode = req->r_inode;
  515. } else if (req->r_dentry) {
  516. if (req->r_dentry->d_inode) {
  517. inode = req->r_dentry->d_inode;
  518. } else {
  519. inode = req->r_dentry->d_parent->d_inode;
  520. hash = req->r_dentry->d_name.hash;
  521. is_hash = true;
  522. }
  523. }
  524. dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
  525. (int)hash, mode);
  526. if (!inode)
  527. goto random;
  528. ci = ceph_inode(inode);
  529. if (is_hash && S_ISDIR(inode->i_mode)) {
  530. struct ceph_inode_frag frag;
  531. int found;
  532. ceph_choose_frag(ci, hash, &frag, &found);
  533. if (found) {
  534. if (mode == USE_ANY_MDS && frag.ndist > 0) {
  535. u8 r;
  536. /* choose a random replica */
  537. get_random_bytes(&r, 1);
  538. r %= frag.ndist;
  539. mds = frag.dist[r];
  540. dout("choose_mds %p %llx.%llx "
  541. "frag %u mds%d (%d/%d)\n",
  542. inode, ceph_vinop(inode),
  543. frag.frag, frag.mds,
  544. (int)r, frag.ndist);
  545. return mds;
  546. }
  547. /* since this file/dir wasn't known to be
  548. * replicated, then we want to look for the
  549. * authoritative mds. */
  550. mode = USE_AUTH_MDS;
  551. if (frag.mds >= 0) {
  552. /* choose auth mds */
  553. mds = frag.mds;
  554. dout("choose_mds %p %llx.%llx "
  555. "frag %u mds%d (auth)\n",
  556. inode, ceph_vinop(inode), frag.frag, mds);
  557. return mds;
  558. }
  559. }
  560. }
  561. spin_lock(&inode->i_lock);
  562. cap = NULL;
  563. if (mode == USE_AUTH_MDS)
  564. cap = ci->i_auth_cap;
  565. if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
  566. cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
  567. if (!cap) {
  568. spin_unlock(&inode->i_lock);
  569. goto random;
  570. }
  571. mds = cap->session->s_mds;
  572. dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
  573. inode, ceph_vinop(inode), mds,
  574. cap == ci->i_auth_cap ? "auth " : "", cap);
  575. spin_unlock(&inode->i_lock);
  576. return mds;
  577. random:
  578. mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
  579. dout("choose_mds chose random mds%d\n", mds);
  580. return mds;
  581. }
  582. /*
  583. * session messages
  584. */
  585. static struct ceph_msg *create_session_msg(u32 op, u64 seq)
  586. {
  587. struct ceph_msg *msg;
  588. struct ceph_mds_session_head *h;
  589. msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), 0, 0, NULL);
  590. if (IS_ERR(msg)) {
  591. pr_err("create_session_msg ENOMEM creating msg\n");
  592. return ERR_PTR(PTR_ERR(msg));
  593. }
  594. h = msg->front.iov_base;
  595. h->op = cpu_to_le32(op);
  596. h->seq = cpu_to_le64(seq);
  597. return msg;
  598. }
  599. /*
  600. * send session open request.
  601. *
  602. * called under mdsc->mutex
  603. */
  604. static int __open_session(struct ceph_mds_client *mdsc,
  605. struct ceph_mds_session *session)
  606. {
  607. struct ceph_msg *msg;
  608. int mstate;
  609. int mds = session->s_mds;
  610. int err = 0;
  611. /* wait for mds to go active? */
  612. mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
  613. dout("open_session to mds%d (%s)\n", mds,
  614. ceph_mds_state_name(mstate));
  615. session->s_state = CEPH_MDS_SESSION_OPENING;
  616. session->s_renew_requested = jiffies;
  617. /* send connect message */
  618. msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
  619. if (IS_ERR(msg)) {
  620. err = PTR_ERR(msg);
  621. goto out;
  622. }
  623. ceph_con_send(&session->s_con, msg);
  624. out:
  625. return 0;
  626. }
  627. /*
  628. * session caps
  629. */
  630. /*
  631. * Free preallocated cap messages assigned to this session
  632. */
  633. static void cleanup_cap_releases(struct ceph_mds_session *session)
  634. {
  635. struct ceph_msg *msg;
  636. spin_lock(&session->s_cap_lock);
  637. while (!list_empty(&session->s_cap_releases)) {
  638. msg = list_first_entry(&session->s_cap_releases,
  639. struct ceph_msg, list_head);
  640. list_del_init(&msg->list_head);
  641. ceph_msg_put(msg);
  642. }
  643. while (!list_empty(&session->s_cap_releases_done)) {
  644. msg = list_first_entry(&session->s_cap_releases_done,
  645. struct ceph_msg, list_head);
  646. list_del_init(&msg->list_head);
  647. ceph_msg_put(msg);
  648. }
  649. spin_unlock(&session->s_cap_lock);
  650. }
  651. /*
  652. * Helper to safely iterate over all caps associated with a session, with
  653. * special care taken to handle a racing __ceph_remove_cap().
  654. *
  655. * Caller must hold session s_mutex.
  656. */
  657. static int iterate_session_caps(struct ceph_mds_session *session,
  658. int (*cb)(struct inode *, struct ceph_cap *,
  659. void *), void *arg)
  660. {
  661. struct list_head *p;
  662. struct ceph_cap *cap;
  663. struct inode *inode, *last_inode = NULL;
  664. struct ceph_cap *old_cap = NULL;
  665. int ret;
  666. dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
  667. spin_lock(&session->s_cap_lock);
  668. p = session->s_caps.next;
  669. while (p != &session->s_caps) {
  670. cap = list_entry(p, struct ceph_cap, session_caps);
  671. inode = igrab(&cap->ci->vfs_inode);
  672. if (!inode) {
  673. p = p->next;
  674. continue;
  675. }
  676. session->s_cap_iterator = cap;
  677. spin_unlock(&session->s_cap_lock);
  678. if (last_inode) {
  679. iput(last_inode);
  680. last_inode = NULL;
  681. }
  682. if (old_cap) {
  683. ceph_put_cap(old_cap);
  684. old_cap = NULL;
  685. }
  686. ret = cb(inode, cap, arg);
  687. last_inode = inode;
  688. spin_lock(&session->s_cap_lock);
  689. p = p->next;
  690. if (cap->ci == NULL) {
  691. dout("iterate_session_caps finishing cap %p removal\n",
  692. cap);
  693. BUG_ON(cap->session != session);
  694. list_del_init(&cap->session_caps);
  695. session->s_nr_caps--;
  696. cap->session = NULL;
  697. old_cap = cap; /* put_cap it w/o locks held */
  698. }
  699. if (ret < 0)
  700. goto out;
  701. }
  702. ret = 0;
  703. out:
  704. session->s_cap_iterator = NULL;
  705. spin_unlock(&session->s_cap_lock);
  706. if (last_inode)
  707. iput(last_inode);
  708. if (old_cap)
  709. ceph_put_cap(old_cap);
  710. return ret;
  711. }
  712. static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
  713. void *arg)
  714. {
  715. struct ceph_inode_info *ci = ceph_inode(inode);
  716. dout("removing cap %p, ci is %p, inode is %p\n",
  717. cap, ci, &ci->vfs_inode);
  718. ceph_remove_cap(cap);
  719. return 0;
  720. }
  721. /*
  722. * caller must hold session s_mutex
  723. */
  724. static void remove_session_caps(struct ceph_mds_session *session)
  725. {
  726. dout("remove_session_caps on %p\n", session);
  727. iterate_session_caps(session, remove_session_caps_cb, NULL);
  728. BUG_ON(session->s_nr_caps > 0);
  729. cleanup_cap_releases(session);
  730. }
  731. /*
  732. * wake up any threads waiting on this session's caps. if the cap is
  733. * old (didn't get renewed on the client reconnect), remove it now.
  734. *
  735. * caller must hold s_mutex.
  736. */
  737. static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
  738. void *arg)
  739. {
  740. struct ceph_inode_info *ci = ceph_inode(inode);
  741. wake_up(&ci->i_cap_wq);
  742. if (arg) {
  743. spin_lock(&inode->i_lock);
  744. ci->i_wanted_max_size = 0;
  745. ci->i_requested_max_size = 0;
  746. spin_unlock(&inode->i_lock);
  747. }
  748. return 0;
  749. }
  750. static void wake_up_session_caps(struct ceph_mds_session *session,
  751. int reconnect)
  752. {
  753. dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
  754. iterate_session_caps(session, wake_up_session_cb,
  755. (void *)(unsigned long)reconnect);
  756. }
  757. /*
  758. * Send periodic message to MDS renewing all currently held caps. The
  759. * ack will reset the expiration for all caps from this session.
  760. *
  761. * caller holds s_mutex
  762. */
  763. static int send_renew_caps(struct ceph_mds_client *mdsc,
  764. struct ceph_mds_session *session)
  765. {
  766. struct ceph_msg *msg;
  767. int state;
  768. if (time_after_eq(jiffies, session->s_cap_ttl) &&
  769. time_after_eq(session->s_cap_ttl, session->s_renew_requested))
  770. pr_info("mds%d caps stale\n", session->s_mds);
  771. session->s_renew_requested = jiffies;
  772. /* do not try to renew caps until a recovering mds has reconnected
  773. * with its clients. */
  774. state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
  775. if (state < CEPH_MDS_STATE_RECONNECT) {
  776. dout("send_renew_caps ignoring mds%d (%s)\n",
  777. session->s_mds, ceph_mds_state_name(state));
  778. return 0;
  779. }
  780. dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
  781. ceph_mds_state_name(state));
  782. msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
  783. ++session->s_renew_seq);
  784. if (IS_ERR(msg))
  785. return PTR_ERR(msg);
  786. ceph_con_send(&session->s_con, msg);
  787. return 0;
  788. }
  789. /*
  790. * Note new cap ttl, and any transition from stale -> not stale (fresh?).
  791. *
  792. * Called under session->s_mutex
  793. */
  794. static void renewed_caps(struct ceph_mds_client *mdsc,
  795. struct ceph_mds_session *session, int is_renew)
  796. {
  797. int was_stale;
  798. int wake = 0;
  799. spin_lock(&session->s_cap_lock);
  800. was_stale = is_renew && (session->s_cap_ttl == 0 ||
  801. time_after_eq(jiffies, session->s_cap_ttl));
  802. session->s_cap_ttl = session->s_renew_requested +
  803. mdsc->mdsmap->m_session_timeout*HZ;
  804. if (was_stale) {
  805. if (time_before(jiffies, session->s_cap_ttl)) {
  806. pr_info("mds%d caps renewed\n", session->s_mds);
  807. wake = 1;
  808. } else {
  809. pr_info("mds%d caps still stale\n", session->s_mds);
  810. }
  811. }
  812. dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
  813. session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
  814. time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
  815. spin_unlock(&session->s_cap_lock);
  816. if (wake)
  817. wake_up_session_caps(session, 0);
  818. }
  819. /*
  820. * send a session close request
  821. */
  822. static int request_close_session(struct ceph_mds_client *mdsc,
  823. struct ceph_mds_session *session)
  824. {
  825. struct ceph_msg *msg;
  826. int err = 0;
  827. dout("request_close_session mds%d state %s seq %lld\n",
  828. session->s_mds, session_state_name(session->s_state),
  829. session->s_seq);
  830. msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
  831. if (IS_ERR(msg))
  832. err = PTR_ERR(msg);
  833. else
  834. ceph_con_send(&session->s_con, msg);
  835. return err;
  836. }
  837. /*
  838. * Called with s_mutex held.
  839. */
  840. static int __close_session(struct ceph_mds_client *mdsc,
  841. struct ceph_mds_session *session)
  842. {
  843. if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
  844. return 0;
  845. session->s_state = CEPH_MDS_SESSION_CLOSING;
  846. return request_close_session(mdsc, session);
  847. }
  848. /*
  849. * Trim old(er) caps.
  850. *
  851. * Because we can't cache an inode without one or more caps, we do
  852. * this indirectly: if a cap is unused, we prune its aliases, at which
  853. * point the inode will hopefully get dropped to.
  854. *
  855. * Yes, this is a bit sloppy. Our only real goal here is to respond to
  856. * memory pressure from the MDS, though, so it needn't be perfect.
  857. */
  858. static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
  859. {
  860. struct ceph_mds_session *session = arg;
  861. struct ceph_inode_info *ci = ceph_inode(inode);
  862. int used, oissued, mine;
  863. if (session->s_trim_caps <= 0)
  864. return -1;
  865. spin_lock(&inode->i_lock);
  866. mine = cap->issued | cap->implemented;
  867. used = __ceph_caps_used(ci);
  868. oissued = __ceph_caps_issued_other(ci, cap);
  869. dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
  870. inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
  871. ceph_cap_string(used));
  872. if (ci->i_dirty_caps)
  873. goto out; /* dirty caps */
  874. if ((used & ~oissued) & mine)
  875. goto out; /* we need these caps */
  876. session->s_trim_caps--;
  877. if (oissued) {
  878. /* we aren't the only cap.. just remove us */
  879. __ceph_remove_cap(cap);
  880. } else {
  881. /* try to drop referring dentries */
  882. spin_unlock(&inode->i_lock);
  883. d_prune_aliases(inode);
  884. dout("trim_caps_cb %p cap %p pruned, count now %d\n",
  885. inode, cap, atomic_read(&inode->i_count));
  886. return 0;
  887. }
  888. out:
  889. spin_unlock(&inode->i_lock);
  890. return 0;
  891. }
  892. /*
  893. * Trim session cap count down to some max number.
  894. */
  895. static int trim_caps(struct ceph_mds_client *mdsc,
  896. struct ceph_mds_session *session,
  897. int max_caps)
  898. {
  899. int trim_caps = session->s_nr_caps - max_caps;
  900. dout("trim_caps mds%d start: %d / %d, trim %d\n",
  901. session->s_mds, session->s_nr_caps, max_caps, trim_caps);
  902. if (trim_caps > 0) {
  903. session->s_trim_caps = trim_caps;
  904. iterate_session_caps(session, trim_caps_cb, session);
  905. dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
  906. session->s_mds, session->s_nr_caps, max_caps,
  907. trim_caps - session->s_trim_caps);
  908. session->s_trim_caps = 0;
  909. }
  910. return 0;
  911. }
  912. /*
  913. * Allocate cap_release messages. If there is a partially full message
  914. * in the queue, try to allocate enough to cover it's remainder, so that
  915. * we can send it immediately.
  916. *
  917. * Called under s_mutex.
  918. */
  919. static int add_cap_releases(struct ceph_mds_client *mdsc,
  920. struct ceph_mds_session *session,
  921. int extra)
  922. {
  923. struct ceph_msg *msg;
  924. struct ceph_mds_cap_release *head;
  925. int err = -ENOMEM;
  926. if (extra < 0)
  927. extra = mdsc->client->mount_args->cap_release_safety;
  928. spin_lock(&session->s_cap_lock);
  929. if (!list_empty(&session->s_cap_releases)) {
  930. msg = list_first_entry(&session->s_cap_releases,
  931. struct ceph_msg,
  932. list_head);
  933. head = msg->front.iov_base;
  934. extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
  935. }
  936. while (session->s_num_cap_releases < session->s_nr_caps + extra) {
  937. spin_unlock(&session->s_cap_lock);
  938. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
  939. 0, 0, NULL);
  940. if (!msg)
  941. goto out_unlocked;
  942. dout("add_cap_releases %p msg %p now %d\n", session, msg,
  943. (int)msg->front.iov_len);
  944. head = msg->front.iov_base;
  945. head->num = cpu_to_le32(0);
  946. msg->front.iov_len = sizeof(*head);
  947. spin_lock(&session->s_cap_lock);
  948. list_add(&msg->list_head, &session->s_cap_releases);
  949. session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
  950. }
  951. if (!list_empty(&session->s_cap_releases)) {
  952. msg = list_first_entry(&session->s_cap_releases,
  953. struct ceph_msg,
  954. list_head);
  955. head = msg->front.iov_base;
  956. if (head->num) {
  957. dout(" queueing non-full %p (%d)\n", msg,
  958. le32_to_cpu(head->num));
  959. list_move_tail(&msg->list_head,
  960. &session->s_cap_releases_done);
  961. session->s_num_cap_releases -=
  962. CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
  963. }
  964. }
  965. err = 0;
  966. spin_unlock(&session->s_cap_lock);
  967. out_unlocked:
  968. return err;
  969. }
  970. /*
  971. * flush all dirty inode data to disk.
  972. *
  973. * returns true if we've flushed through want_flush_seq
  974. */
  975. static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
  976. {
  977. int mds, ret = 1;
  978. dout("check_cap_flush want %lld\n", want_flush_seq);
  979. mutex_lock(&mdsc->mutex);
  980. for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
  981. struct ceph_mds_session *session = mdsc->sessions[mds];
  982. if (!session)
  983. continue;
  984. get_session(session);
  985. mutex_unlock(&mdsc->mutex);
  986. mutex_lock(&session->s_mutex);
  987. if (!list_empty(&session->s_cap_flushing)) {
  988. struct ceph_inode_info *ci =
  989. list_entry(session->s_cap_flushing.next,
  990. struct ceph_inode_info,
  991. i_flushing_item);
  992. struct inode *inode = &ci->vfs_inode;
  993. spin_lock(&inode->i_lock);
  994. if (ci->i_cap_flush_seq <= want_flush_seq) {
  995. dout("check_cap_flush still flushing %p "
  996. "seq %lld <= %lld to mds%d\n", inode,
  997. ci->i_cap_flush_seq, want_flush_seq,
  998. session->s_mds);
  999. ret = 0;
  1000. }
  1001. spin_unlock(&inode->i_lock);
  1002. }
  1003. mutex_unlock(&session->s_mutex);
  1004. ceph_put_mds_session(session);
  1005. if (!ret)
  1006. return ret;
  1007. mutex_lock(&mdsc->mutex);
  1008. }
  1009. mutex_unlock(&mdsc->mutex);
  1010. dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
  1011. return ret;
  1012. }
  1013. /*
  1014. * called under s_mutex
  1015. */
  1016. static void send_cap_releases(struct ceph_mds_client *mdsc,
  1017. struct ceph_mds_session *session)
  1018. {
  1019. struct ceph_msg *msg;
  1020. dout("send_cap_releases mds%d\n", session->s_mds);
  1021. while (1) {
  1022. spin_lock(&session->s_cap_lock);
  1023. if (list_empty(&session->s_cap_releases_done))
  1024. break;
  1025. msg = list_first_entry(&session->s_cap_releases_done,
  1026. struct ceph_msg, list_head);
  1027. list_del_init(&msg->list_head);
  1028. spin_unlock(&session->s_cap_lock);
  1029. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1030. dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
  1031. ceph_con_send(&session->s_con, msg);
  1032. }
  1033. spin_unlock(&session->s_cap_lock);
  1034. }
  1035. /*
  1036. * requests
  1037. */
  1038. /*
  1039. * Create an mds request.
  1040. */
  1041. struct ceph_mds_request *
  1042. ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
  1043. {
  1044. struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
  1045. if (!req)
  1046. return ERR_PTR(-ENOMEM);
  1047. mutex_init(&req->r_fill_mutex);
  1048. req->r_started = jiffies;
  1049. req->r_resend_mds = -1;
  1050. INIT_LIST_HEAD(&req->r_unsafe_dir_item);
  1051. req->r_fmode = -1;
  1052. kref_init(&req->r_kref);
  1053. INIT_LIST_HEAD(&req->r_wait);
  1054. init_completion(&req->r_completion);
  1055. init_completion(&req->r_safe_completion);
  1056. INIT_LIST_HEAD(&req->r_unsafe_item);
  1057. req->r_op = op;
  1058. req->r_direct_mode = mode;
  1059. return req;
  1060. }
  1061. /*
  1062. * return oldest (lowest) request, tid in request tree, 0 if none.
  1063. *
  1064. * called under mdsc->mutex.
  1065. */
  1066. static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
  1067. {
  1068. if (RB_EMPTY_ROOT(&mdsc->request_tree))
  1069. return NULL;
  1070. return rb_entry(rb_first(&mdsc->request_tree),
  1071. struct ceph_mds_request, r_node);
  1072. }
  1073. static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
  1074. {
  1075. struct ceph_mds_request *req = __get_oldest_req(mdsc);
  1076. if (req)
  1077. return req->r_tid;
  1078. return 0;
  1079. }
  1080. /*
  1081. * Build a dentry's path. Allocate on heap; caller must kfree. Based
  1082. * on build_path_from_dentry in fs/cifs/dir.c.
  1083. *
  1084. * If @stop_on_nosnap, generate path relative to the first non-snapped
  1085. * inode.
  1086. *
  1087. * Encode hidden .snap dirs as a double /, i.e.
  1088. * foo/.snap/bar -> foo//bar
  1089. */
  1090. char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
  1091. int stop_on_nosnap)
  1092. {
  1093. struct dentry *temp;
  1094. char *path;
  1095. int len, pos;
  1096. if (dentry == NULL)
  1097. return ERR_PTR(-EINVAL);
  1098. retry:
  1099. len = 0;
  1100. for (temp = dentry; !IS_ROOT(temp);) {
  1101. struct inode *inode = temp->d_inode;
  1102. if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
  1103. len++; /* slash only */
  1104. else if (stop_on_nosnap && inode &&
  1105. ceph_snap(inode) == CEPH_NOSNAP)
  1106. break;
  1107. else
  1108. len += 1 + temp->d_name.len;
  1109. temp = temp->d_parent;
  1110. if (temp == NULL) {
  1111. pr_err("build_path_dentry corrupt dentry %p\n", dentry);
  1112. return ERR_PTR(-EINVAL);
  1113. }
  1114. }
  1115. if (len)
  1116. len--; /* no leading '/' */
  1117. path = kmalloc(len+1, GFP_NOFS);
  1118. if (path == NULL)
  1119. return ERR_PTR(-ENOMEM);
  1120. pos = len;
  1121. path[pos] = 0; /* trailing null */
  1122. for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
  1123. struct inode *inode = temp->d_inode;
  1124. if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
  1125. dout("build_path_dentry path+%d: %p SNAPDIR\n",
  1126. pos, temp);
  1127. } else if (stop_on_nosnap && inode &&
  1128. ceph_snap(inode) == CEPH_NOSNAP) {
  1129. break;
  1130. } else {
  1131. pos -= temp->d_name.len;
  1132. if (pos < 0)
  1133. break;
  1134. strncpy(path + pos, temp->d_name.name,
  1135. temp->d_name.len);
  1136. dout("build_path_dentry path+%d: %p '%.*s'\n",
  1137. pos, temp, temp->d_name.len, path + pos);
  1138. }
  1139. if (pos)
  1140. path[--pos] = '/';
  1141. temp = temp->d_parent;
  1142. if (temp == NULL) {
  1143. pr_err("build_path_dentry corrupt dentry\n");
  1144. kfree(path);
  1145. return ERR_PTR(-EINVAL);
  1146. }
  1147. }
  1148. if (pos != 0) {
  1149. pr_err("build_path_dentry did not end path lookup where "
  1150. "expected, namelen is %d, pos is %d\n", len, pos);
  1151. /* presumably this is only possible if racing with a
  1152. rename of one of the parent directories (we can not
  1153. lock the dentries above us to prevent this, but
  1154. retrying should be harmless) */
  1155. kfree(path);
  1156. goto retry;
  1157. }
  1158. *base = ceph_ino(temp->d_inode);
  1159. *plen = len;
  1160. dout("build_path_dentry on %p %d built %llx '%.*s'\n",
  1161. dentry, atomic_read(&dentry->d_count), *base, len, path);
  1162. return path;
  1163. }
  1164. static int build_dentry_path(struct dentry *dentry,
  1165. const char **ppath, int *ppathlen, u64 *pino,
  1166. int *pfreepath)
  1167. {
  1168. char *path;
  1169. if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
  1170. *pino = ceph_ino(dentry->d_parent->d_inode);
  1171. *ppath = dentry->d_name.name;
  1172. *ppathlen = dentry->d_name.len;
  1173. return 0;
  1174. }
  1175. path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
  1176. if (IS_ERR(path))
  1177. return PTR_ERR(path);
  1178. *ppath = path;
  1179. *pfreepath = 1;
  1180. return 0;
  1181. }
  1182. static int build_inode_path(struct inode *inode,
  1183. const char **ppath, int *ppathlen, u64 *pino,
  1184. int *pfreepath)
  1185. {
  1186. struct dentry *dentry;
  1187. char *path;
  1188. if (ceph_snap(inode) == CEPH_NOSNAP) {
  1189. *pino = ceph_ino(inode);
  1190. *ppathlen = 0;
  1191. return 0;
  1192. }
  1193. dentry = d_find_alias(inode);
  1194. path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
  1195. dput(dentry);
  1196. if (IS_ERR(path))
  1197. return PTR_ERR(path);
  1198. *ppath = path;
  1199. *pfreepath = 1;
  1200. return 0;
  1201. }
  1202. /*
  1203. * request arguments may be specified via an inode *, a dentry *, or
  1204. * an explicit ino+path.
  1205. */
  1206. static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
  1207. const char *rpath, u64 rino,
  1208. const char **ppath, int *pathlen,
  1209. u64 *ino, int *freepath)
  1210. {
  1211. int r = 0;
  1212. if (rinode) {
  1213. r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
  1214. dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
  1215. ceph_snap(rinode));
  1216. } else if (rdentry) {
  1217. r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
  1218. dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
  1219. *ppath);
  1220. } else if (rpath) {
  1221. *ino = rino;
  1222. *ppath = rpath;
  1223. *pathlen = strlen(rpath);
  1224. dout(" path %.*s\n", *pathlen, rpath);
  1225. }
  1226. return r;
  1227. }
  1228. /*
  1229. * called under mdsc->mutex
  1230. */
  1231. static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
  1232. struct ceph_mds_request *req,
  1233. int mds)
  1234. {
  1235. struct ceph_msg *msg;
  1236. struct ceph_mds_request_head *head;
  1237. const char *path1 = NULL;
  1238. const char *path2 = NULL;
  1239. u64 ino1 = 0, ino2 = 0;
  1240. int pathlen1 = 0, pathlen2 = 0;
  1241. int freepath1 = 0, freepath2 = 0;
  1242. int len;
  1243. u16 releases;
  1244. void *p, *end;
  1245. int ret;
  1246. ret = set_request_path_attr(req->r_inode, req->r_dentry,
  1247. req->r_path1, req->r_ino1.ino,
  1248. &path1, &pathlen1, &ino1, &freepath1);
  1249. if (ret < 0) {
  1250. msg = ERR_PTR(ret);
  1251. goto out;
  1252. }
  1253. ret = set_request_path_attr(NULL, req->r_old_dentry,
  1254. req->r_path2, req->r_ino2.ino,
  1255. &path2, &pathlen2, &ino2, &freepath2);
  1256. if (ret < 0) {
  1257. msg = ERR_PTR(ret);
  1258. goto out_free1;
  1259. }
  1260. len = sizeof(*head) +
  1261. pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
  1262. /* calculate (max) length for cap releases */
  1263. len += sizeof(struct ceph_mds_request_release) *
  1264. (!!req->r_inode_drop + !!req->r_dentry_drop +
  1265. !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
  1266. if (req->r_dentry_drop)
  1267. len += req->r_dentry->d_name.len;
  1268. if (req->r_old_dentry_drop)
  1269. len += req->r_old_dentry->d_name.len;
  1270. msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, 0, 0, NULL);
  1271. if (IS_ERR(msg))
  1272. goto out_free2;
  1273. msg->hdr.tid = cpu_to_le64(req->r_tid);
  1274. head = msg->front.iov_base;
  1275. p = msg->front.iov_base + sizeof(*head);
  1276. end = msg->front.iov_base + msg->front.iov_len;
  1277. head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
  1278. head->op = cpu_to_le32(req->r_op);
  1279. head->caller_uid = cpu_to_le32(current_fsuid());
  1280. head->caller_gid = cpu_to_le32(current_fsgid());
  1281. head->args = req->r_args;
  1282. ceph_encode_filepath(&p, end, ino1, path1);
  1283. ceph_encode_filepath(&p, end, ino2, path2);
  1284. /* cap releases */
  1285. releases = 0;
  1286. if (req->r_inode_drop)
  1287. releases += ceph_encode_inode_release(&p,
  1288. req->r_inode ? req->r_inode : req->r_dentry->d_inode,
  1289. mds, req->r_inode_drop, req->r_inode_unless, 0);
  1290. if (req->r_dentry_drop)
  1291. releases += ceph_encode_dentry_release(&p, req->r_dentry,
  1292. mds, req->r_dentry_drop, req->r_dentry_unless);
  1293. if (req->r_old_dentry_drop)
  1294. releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
  1295. mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
  1296. if (req->r_old_inode_drop)
  1297. releases += ceph_encode_inode_release(&p,
  1298. req->r_old_dentry->d_inode,
  1299. mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
  1300. head->num_releases = cpu_to_le16(releases);
  1301. BUG_ON(p > end);
  1302. msg->front.iov_len = p - msg->front.iov_base;
  1303. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1304. msg->pages = req->r_pages;
  1305. msg->nr_pages = req->r_num_pages;
  1306. msg->hdr.data_len = cpu_to_le32(req->r_data_len);
  1307. msg->hdr.data_off = cpu_to_le16(0);
  1308. out_free2:
  1309. if (freepath2)
  1310. kfree((char *)path2);
  1311. out_free1:
  1312. if (freepath1)
  1313. kfree((char *)path1);
  1314. out:
  1315. return msg;
  1316. }
  1317. /*
  1318. * called under mdsc->mutex if error, under no mutex if
  1319. * success.
  1320. */
  1321. static void complete_request(struct ceph_mds_client *mdsc,
  1322. struct ceph_mds_request *req)
  1323. {
  1324. if (req->r_callback)
  1325. req->r_callback(mdsc, req);
  1326. else
  1327. complete(&req->r_completion);
  1328. }
  1329. /*
  1330. * called under mdsc->mutex
  1331. */
  1332. static int __prepare_send_request(struct ceph_mds_client *mdsc,
  1333. struct ceph_mds_request *req,
  1334. int mds)
  1335. {
  1336. struct ceph_mds_request_head *rhead;
  1337. struct ceph_msg *msg;
  1338. int flags = 0;
  1339. req->r_mds = mds;
  1340. req->r_attempts++;
  1341. dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
  1342. req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
  1343. if (req->r_request) {
  1344. ceph_msg_put(req->r_request);
  1345. req->r_request = NULL;
  1346. }
  1347. msg = create_request_message(mdsc, req, mds);
  1348. if (IS_ERR(msg)) {
  1349. req->r_err = PTR_ERR(msg);
  1350. complete_request(mdsc, req);
  1351. return -PTR_ERR(msg);
  1352. }
  1353. req->r_request = msg;
  1354. rhead = msg->front.iov_base;
  1355. rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
  1356. if (req->r_got_unsafe)
  1357. flags |= CEPH_MDS_FLAG_REPLAY;
  1358. if (req->r_locked_dir)
  1359. flags |= CEPH_MDS_FLAG_WANT_DENTRY;
  1360. rhead->flags = cpu_to_le32(flags);
  1361. rhead->num_fwd = req->r_num_fwd;
  1362. rhead->num_retry = req->r_attempts - 1;
  1363. dout(" r_locked_dir = %p\n", req->r_locked_dir);
  1364. if (req->r_target_inode && req->r_got_unsafe)
  1365. rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
  1366. else
  1367. rhead->ino = 0;
  1368. return 0;
  1369. }
  1370. /*
  1371. * send request, or put it on the appropriate wait list.
  1372. */
  1373. static int __do_request(struct ceph_mds_client *mdsc,
  1374. struct ceph_mds_request *req)
  1375. {
  1376. struct ceph_mds_session *session = NULL;
  1377. int mds = -1;
  1378. int err = -EAGAIN;
  1379. if (req->r_err || req->r_got_result)
  1380. goto out;
  1381. if (req->r_timeout &&
  1382. time_after_eq(jiffies, req->r_started + req->r_timeout)) {
  1383. dout("do_request timed out\n");
  1384. err = -EIO;
  1385. goto finish;
  1386. }
  1387. mds = __choose_mds(mdsc, req);
  1388. if (mds < 0 ||
  1389. ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
  1390. dout("do_request no mds or not active, waiting for map\n");
  1391. list_add(&req->r_wait, &mdsc->waiting_for_map);
  1392. goto out;
  1393. }
  1394. /* get, open session */
  1395. session = __ceph_lookup_mds_session(mdsc, mds);
  1396. if (!session) {
  1397. session = register_session(mdsc, mds);
  1398. if (IS_ERR(session)) {
  1399. err = PTR_ERR(session);
  1400. goto finish;
  1401. }
  1402. }
  1403. dout("do_request mds%d session %p state %s\n", mds, session,
  1404. session_state_name(session->s_state));
  1405. if (session->s_state != CEPH_MDS_SESSION_OPEN &&
  1406. session->s_state != CEPH_MDS_SESSION_HUNG) {
  1407. if (session->s_state == CEPH_MDS_SESSION_NEW ||
  1408. session->s_state == CEPH_MDS_SESSION_CLOSING)
  1409. __open_session(mdsc, session);
  1410. list_add(&req->r_wait, &session->s_waiting);
  1411. goto out_session;
  1412. }
  1413. /* send request */
  1414. req->r_session = get_session(session);
  1415. req->r_resend_mds = -1; /* forget any previous mds hint */
  1416. if (req->r_request_started == 0) /* note request start time */
  1417. req->r_request_started = jiffies;
  1418. err = __prepare_send_request(mdsc, req, mds);
  1419. if (!err) {
  1420. ceph_msg_get(req->r_request);
  1421. ceph_con_send(&session->s_con, req->r_request);
  1422. }
  1423. out_session:
  1424. ceph_put_mds_session(session);
  1425. out:
  1426. return err;
  1427. finish:
  1428. req->r_err = err;
  1429. complete_request(mdsc, req);
  1430. goto out;
  1431. }
  1432. /*
  1433. * called under mdsc->mutex
  1434. */
  1435. static void __wake_requests(struct ceph_mds_client *mdsc,
  1436. struct list_head *head)
  1437. {
  1438. struct ceph_mds_request *req, *nreq;
  1439. list_for_each_entry_safe(req, nreq, head, r_wait) {
  1440. list_del_init(&req->r_wait);
  1441. __do_request(mdsc, req);
  1442. }
  1443. }
  1444. /*
  1445. * Wake up threads with requests pending for @mds, so that they can
  1446. * resubmit their requests to a possibly different mds. If @all is set,
  1447. * wake up if their requests has been forwarded to @mds, too.
  1448. */
  1449. static void kick_requests(struct ceph_mds_client *mdsc, int mds, int all)
  1450. {
  1451. struct ceph_mds_request *req;
  1452. struct rb_node *p;
  1453. dout("kick_requests mds%d\n", mds);
  1454. for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
  1455. req = rb_entry(p, struct ceph_mds_request, r_node);
  1456. if (req->r_got_unsafe)
  1457. continue;
  1458. if (req->r_session &&
  1459. req->r_session->s_mds == mds) {
  1460. dout(" kicking tid %llu\n", req->r_tid);
  1461. put_request_session(req);
  1462. __do_request(mdsc, req);
  1463. }
  1464. }
  1465. }
  1466. void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
  1467. struct ceph_mds_request *req)
  1468. {
  1469. dout("submit_request on %p\n", req);
  1470. mutex_lock(&mdsc->mutex);
  1471. __register_request(mdsc, req, NULL);
  1472. __do_request(mdsc, req);
  1473. mutex_unlock(&mdsc->mutex);
  1474. }
  1475. /*
  1476. * Synchrously perform an mds request. Take care of all of the
  1477. * session setup, forwarding, retry details.
  1478. */
  1479. int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
  1480. struct inode *dir,
  1481. struct ceph_mds_request *req)
  1482. {
  1483. int err;
  1484. dout("do_request on %p\n", req);
  1485. /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
  1486. if (req->r_inode)
  1487. ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
  1488. if (req->r_locked_dir)
  1489. ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
  1490. if (req->r_old_dentry)
  1491. ceph_get_cap_refs(
  1492. ceph_inode(req->r_old_dentry->d_parent->d_inode),
  1493. CEPH_CAP_PIN);
  1494. /* issue */
  1495. mutex_lock(&mdsc->mutex);
  1496. __register_request(mdsc, req, dir);
  1497. __do_request(mdsc, req);
  1498. if (req->r_err) {
  1499. err = req->r_err;
  1500. __unregister_request(mdsc, req);
  1501. dout("do_request early error %d\n", err);
  1502. goto out;
  1503. }
  1504. /* wait */
  1505. mutex_unlock(&mdsc->mutex);
  1506. dout("do_request waiting\n");
  1507. if (req->r_timeout) {
  1508. err = (long)wait_for_completion_interruptible_timeout(
  1509. &req->r_completion, req->r_timeout);
  1510. if (err == 0)
  1511. err = -EIO;
  1512. } else {
  1513. err = wait_for_completion_interruptible(&req->r_completion);
  1514. }
  1515. dout("do_request waited, got %d\n", err);
  1516. mutex_lock(&mdsc->mutex);
  1517. /* only abort if we didn't race with a real reply */
  1518. if (req->r_got_result) {
  1519. err = le32_to_cpu(req->r_reply_info.head->result);
  1520. } else if (err < 0) {
  1521. dout("aborted request %lld with %d\n", req->r_tid, err);
  1522. /*
  1523. * ensure we aren't running concurrently with
  1524. * ceph_fill_trace or ceph_readdir_prepopulate, which
  1525. * rely on locks (dir mutex) held by our caller.
  1526. */
  1527. mutex_lock(&req->r_fill_mutex);
  1528. req->r_err = err;
  1529. req->r_aborted = true;
  1530. mutex_unlock(&req->r_fill_mutex);
  1531. if (req->r_locked_dir &&
  1532. (req->r_op & CEPH_MDS_OP_WRITE)) {
  1533. struct ceph_inode_info *ci =
  1534. ceph_inode(req->r_locked_dir);
  1535. dout("aborted, clearing I_COMPLETE on %p, leases\n",
  1536. req->r_locked_dir);
  1537. spin_lock(&req->r_locked_dir->i_lock);
  1538. ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
  1539. ci->i_release_count++;
  1540. spin_unlock(&req->r_locked_dir->i_lock);
  1541. if (req->r_dentry)
  1542. ceph_invalidate_dentry_lease(req->r_dentry);
  1543. if (req->r_old_dentry)
  1544. ceph_invalidate_dentry_lease(req->r_old_dentry);
  1545. }
  1546. } else {
  1547. err = req->r_err;
  1548. }
  1549. out:
  1550. mutex_unlock(&mdsc->mutex);
  1551. dout("do_request %p done, result %d\n", req, err);
  1552. return err;
  1553. }
  1554. /*
  1555. * Handle mds reply.
  1556. *
  1557. * We take the session mutex and parse and process the reply immediately.
  1558. * This preserves the logical ordering of replies, capabilities, etc., sent
  1559. * by the MDS as they are applied to our local cache.
  1560. */
  1561. static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
  1562. {
  1563. struct ceph_mds_client *mdsc = session->s_mdsc;
  1564. struct ceph_mds_request *req;
  1565. struct ceph_mds_reply_head *head = msg->front.iov_base;
  1566. struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
  1567. u64 tid;
  1568. int err, result;
  1569. int mds = session->s_mds;
  1570. if (msg->front.iov_len < sizeof(*head)) {
  1571. pr_err("mdsc_handle_reply got corrupt (short) reply\n");
  1572. ceph_msg_dump(msg);
  1573. return;
  1574. }
  1575. /* get request, session */
  1576. tid = le64_to_cpu(msg->hdr.tid);
  1577. mutex_lock(&mdsc->mutex);
  1578. req = __lookup_request(mdsc, tid);
  1579. if (!req) {
  1580. dout("handle_reply on unknown tid %llu\n", tid);
  1581. mutex_unlock(&mdsc->mutex);
  1582. return;
  1583. }
  1584. dout("handle_reply %p\n", req);
  1585. /* correct session? */
  1586. if (req->r_session != session) {
  1587. pr_err("mdsc_handle_reply got %llu on session mds%d"
  1588. " not mds%d\n", tid, session->s_mds,
  1589. req->r_session ? req->r_session->s_mds : -1);
  1590. mutex_unlock(&mdsc->mutex);
  1591. goto out;
  1592. }
  1593. /* dup? */
  1594. if ((req->r_got_unsafe && !head->safe) ||
  1595. (req->r_got_safe && head->safe)) {
  1596. pr_warning("got a dup %s reply on %llu from mds%d\n",
  1597. head->safe ? "safe" : "unsafe", tid, mds);
  1598. mutex_unlock(&mdsc->mutex);
  1599. goto out;
  1600. }
  1601. result = le32_to_cpu(head->result);
  1602. /*
  1603. * Tolerate 2 consecutive ESTALEs from the same mds.
  1604. * FIXME: we should be looking at the cap migrate_seq.
  1605. */
  1606. if (result == -ESTALE) {
  1607. req->r_direct_mode = USE_AUTH_MDS;
  1608. req->r_num_stale++;
  1609. if (req->r_num_stale <= 2) {
  1610. __do_request(mdsc, req);
  1611. mutex_unlock(&mdsc->mutex);
  1612. goto out;
  1613. }
  1614. } else {
  1615. req->r_num_stale = 0;
  1616. }
  1617. if (head->safe) {
  1618. req->r_got_safe = true;
  1619. __unregister_request(mdsc, req);
  1620. complete(&req->r_safe_completion);
  1621. if (req->r_got_unsafe) {
  1622. /*
  1623. * We already handled the unsafe response, now do the
  1624. * cleanup. No need to examine the response; the MDS
  1625. * doesn't include any result info in the safe
  1626. * response. And even if it did, there is nothing
  1627. * useful we could do with a revised return value.
  1628. */
  1629. dout("got safe reply %llu, mds%d\n", tid, mds);
  1630. list_del_init(&req->r_unsafe_item);
  1631. /* last unsafe request during umount? */
  1632. if (mdsc->stopping && !__get_oldest_req(mdsc))
  1633. complete(&mdsc->safe_umount_waiters);
  1634. mutex_unlock(&mdsc->mutex);
  1635. goto out;
  1636. }
  1637. } else {
  1638. req->r_got_unsafe = true;
  1639. list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
  1640. }
  1641. dout("handle_reply tid %lld result %d\n", tid, result);
  1642. rinfo = &req->r_reply_info;
  1643. err = parse_reply_info(msg, rinfo);
  1644. mutex_unlock(&mdsc->mutex);
  1645. mutex_lock(&session->s_mutex);
  1646. if (err < 0) {
  1647. pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
  1648. ceph_msg_dump(msg);
  1649. goto out_err;
  1650. }
  1651. /* snap trace */
  1652. if (rinfo->snapblob_len) {
  1653. down_write(&mdsc->snap_rwsem);
  1654. ceph_update_snap_trace(mdsc, rinfo->snapblob,
  1655. rinfo->snapblob + rinfo->snapblob_len,
  1656. le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
  1657. downgrade_write(&mdsc->snap_rwsem);
  1658. } else {
  1659. down_read(&mdsc->snap_rwsem);
  1660. }
  1661. /* insert trace into our cache */
  1662. mutex_lock(&req->r_fill_mutex);
  1663. err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
  1664. if (err == 0) {
  1665. if (result == 0 && rinfo->dir_nr)
  1666. ceph_readdir_prepopulate(req, req->r_session);
  1667. ceph_unreserve_caps(&req->r_caps_reservation);
  1668. }
  1669. mutex_unlock(&req->r_fill_mutex);
  1670. up_read(&mdsc->snap_rwsem);
  1671. out_err:
  1672. mutex_lock(&mdsc->mutex);
  1673. if (!req->r_aborted) {
  1674. if (err) {
  1675. req->r_err = err;
  1676. } else {
  1677. req->r_reply = msg;
  1678. ceph_msg_get(msg);
  1679. req->r_got_result = true;
  1680. }
  1681. } else {
  1682. dout("reply arrived after request %lld was aborted\n", tid);
  1683. }
  1684. mutex_unlock(&mdsc->mutex);
  1685. add_cap_releases(mdsc, req->r_session, -1);
  1686. mutex_unlock(&session->s_mutex);
  1687. /* kick calling process */
  1688. complete_request(mdsc, req);
  1689. out:
  1690. ceph_mdsc_put_request(req);
  1691. return;
  1692. }
  1693. /*
  1694. * handle mds notification that our request has been forwarded.
  1695. */
  1696. static void handle_forward(struct ceph_mds_client *mdsc,
  1697. struct ceph_mds_session *session,
  1698. struct ceph_msg *msg)
  1699. {
  1700. struct ceph_mds_request *req;
  1701. u64 tid = le64_to_cpu(msg->hdr.tid);
  1702. u32 next_mds;
  1703. u32 fwd_seq;
  1704. int err = -EINVAL;
  1705. void *p = msg->front.iov_base;
  1706. void *end = p + msg->front.iov_len;
  1707. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1708. next_mds = ceph_decode_32(&p);
  1709. fwd_seq = ceph_decode_32(&p);
  1710. mutex_lock(&mdsc->mutex);
  1711. req = __lookup_request(mdsc, tid);
  1712. if (!req) {
  1713. dout("forward %llu to mds%d - req dne\n", tid, next_mds);
  1714. goto out; /* dup reply? */
  1715. }
  1716. if (fwd_seq <= req->r_num_fwd) {
  1717. dout("forward %llu to mds%d - old seq %d <= %d\n",
  1718. tid, next_mds, req->r_num_fwd, fwd_seq);
  1719. } else {
  1720. /* resend. forward race not possible; mds would drop */
  1721. dout("forward %llu to mds%d (we resend)\n", tid, next_mds);
  1722. req->r_num_fwd = fwd_seq;
  1723. req->r_resend_mds = next_mds;
  1724. put_request_session(req);
  1725. __do_request(mdsc, req);
  1726. }
  1727. ceph_mdsc_put_request(req);
  1728. out:
  1729. mutex_unlock(&mdsc->mutex);
  1730. return;
  1731. bad:
  1732. pr_err("mdsc_handle_forward decode error err=%d\n", err);
  1733. }
  1734. /*
  1735. * handle a mds session control message
  1736. */
  1737. static void handle_session(struct ceph_mds_session *session,
  1738. struct ceph_msg *msg)
  1739. {
  1740. struct ceph_mds_client *mdsc = session->s_mdsc;
  1741. u32 op;
  1742. u64 seq;
  1743. int mds = session->s_mds;
  1744. struct ceph_mds_session_head *h = msg->front.iov_base;
  1745. int wake = 0;
  1746. /* decode */
  1747. if (msg->front.iov_len != sizeof(*h))
  1748. goto bad;
  1749. op = le32_to_cpu(h->op);
  1750. seq = le64_to_cpu(h->seq);
  1751. mutex_lock(&mdsc->mutex);
  1752. if (op == CEPH_SESSION_CLOSE)
  1753. __unregister_session(mdsc, session);
  1754. /* FIXME: this ttl calculation is generous */
  1755. session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
  1756. mutex_unlock(&mdsc->mutex);
  1757. mutex_lock(&session->s_mutex);
  1758. dout("handle_session mds%d %s %p state %s seq %llu\n",
  1759. mds, ceph_session_op_name(op), session,
  1760. session_state_name(session->s_state), seq);
  1761. if (session->s_state == CEPH_MDS_SESSION_HUNG) {
  1762. session->s_state = CEPH_MDS_SESSION_OPEN;
  1763. pr_info("mds%d came back\n", session->s_mds);
  1764. }
  1765. switch (op) {
  1766. case CEPH_SESSION_OPEN:
  1767. session->s_state = CEPH_MDS_SESSION_OPEN;
  1768. renewed_caps(mdsc, session, 0);
  1769. wake = 1;
  1770. if (mdsc->stopping)
  1771. __close_session(mdsc, session);
  1772. break;
  1773. case CEPH_SESSION_RENEWCAPS:
  1774. if (session->s_renew_seq == seq)
  1775. renewed_caps(mdsc, session, 1);
  1776. break;
  1777. case CEPH_SESSION_CLOSE:
  1778. remove_session_caps(session);
  1779. wake = 1; /* for good measure */
  1780. complete(&mdsc->session_close_waiters);
  1781. kick_requests(mdsc, mds, 0); /* cur only */
  1782. break;
  1783. case CEPH_SESSION_STALE:
  1784. pr_info("mds%d caps went stale, renewing\n",
  1785. session->s_mds);
  1786. spin_lock(&session->s_cap_lock);
  1787. session->s_cap_gen++;
  1788. session->s_cap_ttl = 0;
  1789. spin_unlock(&session->s_cap_lock);
  1790. send_renew_caps(mdsc, session);
  1791. break;
  1792. case CEPH_SESSION_RECALL_STATE:
  1793. trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
  1794. break;
  1795. default:
  1796. pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
  1797. WARN_ON(1);
  1798. }
  1799. mutex_unlock(&session->s_mutex);
  1800. if (wake) {
  1801. mutex_lock(&mdsc->mutex);
  1802. __wake_requests(mdsc, &session->s_waiting);
  1803. mutex_unlock(&mdsc->mutex);
  1804. }
  1805. return;
  1806. bad:
  1807. pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
  1808. (int)msg->front.iov_len);
  1809. ceph_msg_dump(msg);
  1810. return;
  1811. }
  1812. /*
  1813. * called under session->mutex.
  1814. */
  1815. static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
  1816. struct ceph_mds_session *session)
  1817. {
  1818. struct ceph_mds_request *req, *nreq;
  1819. int err;
  1820. dout("replay_unsafe_requests mds%d\n", session->s_mds);
  1821. mutex_lock(&mdsc->mutex);
  1822. list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
  1823. err = __prepare_send_request(mdsc, req, session->s_mds);
  1824. if (!err) {
  1825. ceph_msg_get(req->r_request);
  1826. ceph_con_send(&session->s_con, req->r_request);
  1827. }
  1828. }
  1829. mutex_unlock(&mdsc->mutex);
  1830. }
  1831. /*
  1832. * Encode information about a cap for a reconnect with the MDS.
  1833. */
  1834. static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
  1835. void *arg)
  1836. {
  1837. struct ceph_mds_cap_reconnect rec;
  1838. struct ceph_inode_info *ci;
  1839. struct ceph_pagelist *pagelist = arg;
  1840. char *path;
  1841. int pathlen, err;
  1842. u64 pathbase;
  1843. struct dentry *dentry;
  1844. ci = cap->ci;
  1845. dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
  1846. inode, ceph_vinop(inode), cap, cap->cap_id,
  1847. ceph_cap_string(cap->issued));
  1848. err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
  1849. if (err)
  1850. return err;
  1851. dentry = d_find_alias(inode);
  1852. if (dentry) {
  1853. path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
  1854. if (IS_ERR(path)) {
  1855. err = PTR_ERR(path);
  1856. BUG_ON(err);
  1857. }
  1858. } else {
  1859. path = NULL;
  1860. pathlen = 0;
  1861. }
  1862. err = ceph_pagelist_encode_string(pagelist, path, pathlen);
  1863. if (err)
  1864. goto out;
  1865. spin_lock(&inode->i_lock);
  1866. cap->seq = 0; /* reset cap seq */
  1867. cap->issue_seq = 0; /* and issue_seq */
  1868. rec.cap_id = cpu_to_le64(cap->cap_id);
  1869. rec.pathbase = cpu_to_le64(pathbase);
  1870. rec.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
  1871. rec.issued = cpu_to_le32(cap->issued);
  1872. rec.size = cpu_to_le64(inode->i_size);
  1873. ceph_encode_timespec(&rec.mtime, &inode->i_mtime);
  1874. ceph_encode_timespec(&rec.atime, &inode->i_atime);
  1875. rec.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
  1876. spin_unlock(&inode->i_lock);
  1877. err = ceph_pagelist_append(pagelist, &rec, sizeof(rec));
  1878. out:
  1879. kfree(path);
  1880. dput(dentry);
  1881. return err;
  1882. }
  1883. /*
  1884. * If an MDS fails and recovers, clients need to reconnect in order to
  1885. * reestablish shared state. This includes all caps issued through
  1886. * this session _and_ the snap_realm hierarchy. Because it's not
  1887. * clear which snap realms the mds cares about, we send everything we
  1888. * know about.. that ensures we'll then get any new info the
  1889. * recovering MDS might have.
  1890. *
  1891. * This is a relatively heavyweight operation, but it's rare.
  1892. *
  1893. * called with mdsc->mutex held.
  1894. */
  1895. static void send_mds_reconnect(struct ceph_mds_client *mdsc, int mds)
  1896. {
  1897. struct ceph_mds_session *session = NULL;
  1898. struct ceph_msg *reply;
  1899. struct rb_node *p;
  1900. int err = -ENOMEM;
  1901. struct ceph_pagelist *pagelist;
  1902. pr_info("reconnect to recovering mds%d\n", mds);
  1903. pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
  1904. if (!pagelist)
  1905. goto fail_nopagelist;
  1906. ceph_pagelist_init(pagelist);
  1907. reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, 0, 0, NULL);
  1908. if (IS_ERR(reply)) {
  1909. err = PTR_ERR(reply);
  1910. goto fail_nomsg;
  1911. }
  1912. /* find session */
  1913. session = __ceph_lookup_mds_session(mdsc, mds);
  1914. mutex_unlock(&mdsc->mutex); /* drop lock for duration */
  1915. if (session) {
  1916. mutex_lock(&session->s_mutex);
  1917. session->s_state = CEPH_MDS_SESSION_RECONNECTING;
  1918. session->s_seq = 0;
  1919. ceph_con_open(&session->s_con,
  1920. ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
  1921. /* replay unsafe requests */
  1922. replay_unsafe_requests(mdsc, session);
  1923. } else {
  1924. dout("no session for mds%d, will send short reconnect\n",
  1925. mds);
  1926. }
  1927. down_read(&mdsc->snap_rwsem);
  1928. if (!session)
  1929. goto send;
  1930. dout("session %p state %s\n", session,
  1931. session_state_name(session->s_state));
  1932. /* traverse this session's caps */
  1933. err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
  1934. if (err)
  1935. goto fail;
  1936. err = iterate_session_caps(session, encode_caps_cb, pagelist);
  1937. if (err < 0)
  1938. goto fail;
  1939. /*
  1940. * snaprealms. we provide mds with the ino, seq (version), and
  1941. * parent for all of our realms. If the mds has any newer info,
  1942. * it will tell us.
  1943. */
  1944. for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
  1945. struct ceph_snap_realm *realm =
  1946. rb_entry(p, struct ceph_snap_realm, node);
  1947. struct ceph_mds_snaprealm_reconnect sr_rec;
  1948. dout(" adding snap realm %llx seq %lld parent %llx\n",
  1949. realm->ino, realm->seq, realm->parent_ino);
  1950. sr_rec.ino = cpu_to_le64(realm->ino);
  1951. sr_rec.seq = cpu_to_le64(realm->seq);
  1952. sr_rec.parent = cpu_to_le64(realm->parent_ino);
  1953. err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
  1954. if (err)
  1955. goto fail;
  1956. }
  1957. send:
  1958. reply->pagelist = pagelist;
  1959. reply->hdr.data_len = cpu_to_le32(pagelist->length);
  1960. reply->nr_pages = calc_pages_for(0, pagelist->length);
  1961. ceph_con_send(&session->s_con, reply);
  1962. session->s_state = CEPH_MDS_SESSION_OPEN;
  1963. mutex_unlock(&session->s_mutex);
  1964. mutex_lock(&mdsc->mutex);
  1965. __wake_requests(mdsc, &session->s_waiting);
  1966. mutex_unlock(&mdsc->mutex);
  1967. ceph_put_mds_session(session);
  1968. up_read(&mdsc->snap_rwsem);
  1969. mutex_lock(&mdsc->mutex);
  1970. return;
  1971. fail:
  1972. ceph_msg_put(reply);
  1973. up_read(&mdsc->snap_rwsem);
  1974. mutex_unlock(&session->s_mutex);
  1975. ceph_put_mds_session(session);
  1976. fail_nomsg:
  1977. ceph_pagelist_release(pagelist);
  1978. kfree(pagelist);
  1979. fail_nopagelist:
  1980. pr_err("error %d preparing reconnect for mds%d\n", err, mds);
  1981. mutex_lock(&mdsc->mutex);
  1982. return;
  1983. }
  1984. /*
  1985. * compare old and new mdsmaps, kicking requests
  1986. * and closing out old connections as necessary
  1987. *
  1988. * called under mdsc->mutex.
  1989. */
  1990. static void check_new_map(struct ceph_mds_client *mdsc,
  1991. struct ceph_mdsmap *newmap,
  1992. struct ceph_mdsmap *oldmap)
  1993. {
  1994. int i;
  1995. int oldstate, newstate;
  1996. struct ceph_mds_session *s;
  1997. dout("check_new_map new %u old %u\n",
  1998. newmap->m_epoch, oldmap->m_epoch);
  1999. for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
  2000. if (mdsc->sessions[i] == NULL)
  2001. continue;
  2002. s = mdsc->sessions[i];
  2003. oldstate = ceph_mdsmap_get_state(oldmap, i);
  2004. newstate = ceph_mdsmap_get_state(newmap, i);
  2005. dout("check_new_map mds%d state %s -> %s (session %s)\n",
  2006. i, ceph_mds_state_name(oldstate),
  2007. ceph_mds_state_name(newstate),
  2008. session_state_name(s->s_state));
  2009. if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
  2010. ceph_mdsmap_get_addr(newmap, i),
  2011. sizeof(struct ceph_entity_addr))) {
  2012. if (s->s_state == CEPH_MDS_SESSION_OPENING) {
  2013. /* the session never opened, just close it
  2014. * out now */
  2015. __wake_requests(mdsc, &s->s_waiting);
  2016. __unregister_session(mdsc, s);
  2017. } else {
  2018. /* just close it */
  2019. mutex_unlock(&mdsc->mutex);
  2020. mutex_lock(&s->s_mutex);
  2021. mutex_lock(&mdsc->mutex);
  2022. ceph_con_close(&s->s_con);
  2023. mutex_unlock(&s->s_mutex);
  2024. s->s_state = CEPH_MDS_SESSION_RESTARTING;
  2025. }
  2026. /* kick any requests waiting on the recovering mds */
  2027. kick_requests(mdsc, i, 1);
  2028. } else if (oldstate == newstate) {
  2029. continue; /* nothing new with this mds */
  2030. }
  2031. /*
  2032. * send reconnect?
  2033. */
  2034. if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
  2035. newstate >= CEPH_MDS_STATE_RECONNECT)
  2036. send_mds_reconnect(mdsc, i);
  2037. /*
  2038. * kick requests on any mds that has gone active.
  2039. *
  2040. * kick requests on cur or forwarder: we may have sent
  2041. * the request to mds1, mds1 told us it forwarded it
  2042. * to mds2, but then we learn mds1 failed and can't be
  2043. * sure it successfully forwarded our request before
  2044. * it died.
  2045. */
  2046. if (oldstate < CEPH_MDS_STATE_ACTIVE &&
  2047. newstate >= CEPH_MDS_STATE_ACTIVE) {
  2048. pr_info("mds%d reconnect completed\n", s->s_mds);
  2049. kick_requests(mdsc, i, 1);
  2050. ceph_kick_flushing_caps(mdsc, s);
  2051. wake_up_session_caps(s, 1);
  2052. }
  2053. }
  2054. }
  2055. /*
  2056. * leases
  2057. */
  2058. /*
  2059. * caller must hold session s_mutex, dentry->d_lock
  2060. */
  2061. void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
  2062. {
  2063. struct ceph_dentry_info *di = ceph_dentry(dentry);
  2064. ceph_put_mds_session(di->lease_session);
  2065. di->lease_session = NULL;
  2066. }
  2067. static void handle_lease(struct ceph_mds_client *mdsc,
  2068. struct ceph_mds_session *session,
  2069. struct ceph_msg *msg)
  2070. {
  2071. struct super_block *sb = mdsc->client->sb;
  2072. struct inode *inode;
  2073. struct ceph_inode_info *ci;
  2074. struct dentry *parent, *dentry;
  2075. struct ceph_dentry_info *di;
  2076. int mds = session->s_mds;
  2077. struct ceph_mds_lease *h = msg->front.iov_base;
  2078. struct ceph_vino vino;
  2079. int mask;
  2080. struct qstr dname;
  2081. int release = 0;
  2082. dout("handle_lease from mds%d\n", mds);
  2083. /* decode */
  2084. if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
  2085. goto bad;
  2086. vino.ino = le64_to_cpu(h->ino);
  2087. vino.snap = CEPH_NOSNAP;
  2088. mask = le16_to_cpu(h->mask);
  2089. dname.name = (void *)h + sizeof(*h) + sizeof(u32);
  2090. dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
  2091. if (dname.len != get_unaligned_le32(h+1))
  2092. goto bad;
  2093. mutex_lock(&session->s_mutex);
  2094. session->s_seq++;
  2095. /* lookup inode */
  2096. inode = ceph_find_inode(sb, vino);
  2097. dout("handle_lease '%s', mask %d, ino %llx %p\n",
  2098. ceph_lease_op_name(h->action), mask, vino.ino, inode);
  2099. if (inode == NULL) {
  2100. dout("handle_lease no inode %llx\n", vino.ino);
  2101. goto release;
  2102. }
  2103. ci = ceph_inode(inode);
  2104. /* dentry */
  2105. parent = d_find_alias(inode);
  2106. if (!parent) {
  2107. dout("no parent dentry on inode %p\n", inode);
  2108. WARN_ON(1);
  2109. goto release; /* hrm... */
  2110. }
  2111. dname.hash = full_name_hash(dname.name, dname.len);
  2112. dentry = d_lookup(parent, &dname);
  2113. dput(parent);
  2114. if (!dentry)
  2115. goto release;
  2116. spin_lock(&dentry->d_lock);
  2117. di = ceph_dentry(dentry);
  2118. switch (h->action) {
  2119. case CEPH_MDS_LEASE_REVOKE:
  2120. if (di && di->lease_session == session) {
  2121. h->seq = cpu_to_le32(di->lease_seq);
  2122. __ceph_mdsc_drop_dentry_lease(dentry);
  2123. }
  2124. release = 1;
  2125. break;
  2126. case CEPH_MDS_LEASE_RENEW:
  2127. if (di && di->lease_session == session &&
  2128. di->lease_gen == session->s_cap_gen &&
  2129. di->lease_renew_from &&
  2130. di->lease_renew_after == 0) {
  2131. unsigned long duration =
  2132. le32_to_cpu(h->duration_ms) * HZ / 1000;
  2133. di->lease_seq = le32_to_cpu(h->seq);
  2134. dentry->d_time = di->lease_renew_from + duration;
  2135. di->lease_renew_after = di->lease_renew_from +
  2136. (duration >> 1);
  2137. di->lease_renew_from = 0;
  2138. }
  2139. break;
  2140. }
  2141. spin_unlock(&dentry->d_lock);
  2142. dput(dentry);
  2143. if (!release)
  2144. goto out;
  2145. release:
  2146. /* let's just reuse the same message */
  2147. h->action = CEPH_MDS_LEASE_REVOKE_ACK;
  2148. ceph_msg_get(msg);
  2149. ceph_con_send(&session->s_con, msg);
  2150. out:
  2151. iput(inode);
  2152. mutex_unlock(&session->s_mutex);
  2153. return;
  2154. bad:
  2155. pr_err("corrupt lease message\n");
  2156. ceph_msg_dump(msg);
  2157. }
  2158. void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
  2159. struct inode *inode,
  2160. struct dentry *dentry, char action,
  2161. u32 seq)
  2162. {
  2163. struct ceph_msg *msg;
  2164. struct ceph_mds_lease *lease;
  2165. int len = sizeof(*lease) + sizeof(u32);
  2166. int dnamelen = 0;
  2167. dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
  2168. inode, dentry, ceph_lease_op_name(action), session->s_mds);
  2169. dnamelen = dentry->d_name.len;
  2170. len += dnamelen;
  2171. msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, 0, 0, NULL);
  2172. if (IS_ERR(msg))
  2173. return;
  2174. lease = msg->front.iov_base;
  2175. lease->action = action;
  2176. lease->mask = cpu_to_le16(CEPH_LOCK_DN);
  2177. lease->ino = cpu_to_le64(ceph_vino(inode).ino);
  2178. lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
  2179. lease->seq = cpu_to_le32(seq);
  2180. put_unaligned_le32(dnamelen, lease + 1);
  2181. memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
  2182. /*
  2183. * if this is a preemptive lease RELEASE, no need to
  2184. * flush request stream, since the actual request will
  2185. * soon follow.
  2186. */
  2187. msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
  2188. ceph_con_send(&session->s_con, msg);
  2189. }
  2190. /*
  2191. * Preemptively release a lease we expect to invalidate anyway.
  2192. * Pass @inode always, @dentry is optional.
  2193. */
  2194. void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
  2195. struct dentry *dentry, int mask)
  2196. {
  2197. struct ceph_dentry_info *di;
  2198. struct ceph_mds_session *session;
  2199. u32 seq;
  2200. BUG_ON(inode == NULL);
  2201. BUG_ON(dentry == NULL);
  2202. BUG_ON(mask != CEPH_LOCK_DN);
  2203. /* is dentry lease valid? */
  2204. spin_lock(&dentry->d_lock);
  2205. di = ceph_dentry(dentry);
  2206. if (!di || !di->lease_session ||
  2207. di->lease_session->s_mds < 0 ||
  2208. di->lease_gen != di->lease_session->s_cap_gen ||
  2209. !time_before(jiffies, dentry->d_time)) {
  2210. dout("lease_release inode %p dentry %p -- "
  2211. "no lease on %d\n",
  2212. inode, dentry, mask);
  2213. spin_unlock(&dentry->d_lock);
  2214. return;
  2215. }
  2216. /* we do have a lease on this dentry; note mds and seq */
  2217. session = ceph_get_mds_session(di->lease_session);
  2218. seq = di->lease_seq;
  2219. __ceph_mdsc_drop_dentry_lease(dentry);
  2220. spin_unlock(&dentry->d_lock);
  2221. dout("lease_release inode %p dentry %p mask %d to mds%d\n",
  2222. inode, dentry, mask, session->s_mds);
  2223. ceph_mdsc_lease_send_msg(session, inode, dentry,
  2224. CEPH_MDS_LEASE_RELEASE, seq);
  2225. ceph_put_mds_session(session);
  2226. }
  2227. /*
  2228. * drop all leases (and dentry refs) in preparation for umount
  2229. */
  2230. static void drop_leases(struct ceph_mds_client *mdsc)
  2231. {
  2232. int i;
  2233. dout("drop_leases\n");
  2234. mutex_lock(&mdsc->mutex);
  2235. for (i = 0; i < mdsc->max_sessions; i++) {
  2236. struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
  2237. if (!s)
  2238. continue;
  2239. mutex_unlock(&mdsc->mutex);
  2240. mutex_lock(&s->s_mutex);
  2241. mutex_unlock(&s->s_mutex);
  2242. ceph_put_mds_session(s);
  2243. mutex_lock(&mdsc->mutex);
  2244. }
  2245. mutex_unlock(&mdsc->mutex);
  2246. }
  2247. /*
  2248. * delayed work -- periodically trim expired leases, renew caps with mds
  2249. */
  2250. static void schedule_delayed(struct ceph_mds_client *mdsc)
  2251. {
  2252. int delay = 5;
  2253. unsigned hz = round_jiffies_relative(HZ * delay);
  2254. schedule_delayed_work(&mdsc->delayed_work, hz);
  2255. }
  2256. static void delayed_work(struct work_struct *work)
  2257. {
  2258. int i;
  2259. struct ceph_mds_client *mdsc =
  2260. container_of(work, struct ceph_mds_client, delayed_work.work);
  2261. int renew_interval;
  2262. int renew_caps;
  2263. dout("mdsc delayed_work\n");
  2264. ceph_check_delayed_caps(mdsc);
  2265. mutex_lock(&mdsc->mutex);
  2266. renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
  2267. renew_caps = time_after_eq(jiffies, HZ*renew_interval +
  2268. mdsc->last_renew_caps);
  2269. if (renew_caps)
  2270. mdsc->last_renew_caps = jiffies;
  2271. for (i = 0; i < mdsc->max_sessions; i++) {
  2272. struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
  2273. if (s == NULL)
  2274. continue;
  2275. if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
  2276. dout("resending session close request for mds%d\n",
  2277. s->s_mds);
  2278. request_close_session(mdsc, s);
  2279. ceph_put_mds_session(s);
  2280. continue;
  2281. }
  2282. if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
  2283. if (s->s_state == CEPH_MDS_SESSION_OPEN) {
  2284. s->s_state = CEPH_MDS_SESSION_HUNG;
  2285. pr_info("mds%d hung\n", s->s_mds);
  2286. }
  2287. }
  2288. if (s->s_state < CEPH_MDS_SESSION_OPEN) {
  2289. /* this mds is failed or recovering, just wait */
  2290. ceph_put_mds_session(s);
  2291. continue;
  2292. }
  2293. mutex_unlock(&mdsc->mutex);
  2294. mutex_lock(&s->s_mutex);
  2295. if (renew_caps)
  2296. send_renew_caps(mdsc, s);
  2297. else
  2298. ceph_con_keepalive(&s->s_con);
  2299. add_cap_releases(mdsc, s, -1);
  2300. send_cap_releases(mdsc, s);
  2301. mutex_unlock(&s->s_mutex);
  2302. ceph_put_mds_session(s);
  2303. mutex_lock(&mdsc->mutex);
  2304. }
  2305. mutex_unlock(&mdsc->mutex);
  2306. schedule_delayed(mdsc);
  2307. }
  2308. int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
  2309. {
  2310. mdsc->client = client;
  2311. mutex_init(&mdsc->mutex);
  2312. mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
  2313. init_completion(&mdsc->safe_umount_waiters);
  2314. init_completion(&mdsc->session_close_waiters);
  2315. INIT_LIST_HEAD(&mdsc->waiting_for_map);
  2316. mdsc->sessions = NULL;
  2317. mdsc->max_sessions = 0;
  2318. mdsc->stopping = 0;
  2319. init_rwsem(&mdsc->snap_rwsem);
  2320. mdsc->snap_realms = RB_ROOT;
  2321. INIT_LIST_HEAD(&mdsc->snap_empty);
  2322. spin_lock_init(&mdsc->snap_empty_lock);
  2323. mdsc->last_tid = 0;
  2324. mdsc->request_tree = RB_ROOT;
  2325. INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
  2326. mdsc->last_renew_caps = jiffies;
  2327. INIT_LIST_HEAD(&mdsc->cap_delay_list);
  2328. spin_lock_init(&mdsc->cap_delay_lock);
  2329. INIT_LIST_HEAD(&mdsc->snap_flush_list);
  2330. spin_lock_init(&mdsc->snap_flush_lock);
  2331. mdsc->cap_flush_seq = 0;
  2332. INIT_LIST_HEAD(&mdsc->cap_dirty);
  2333. mdsc->num_cap_flushing = 0;
  2334. spin_lock_init(&mdsc->cap_dirty_lock);
  2335. init_waitqueue_head(&mdsc->cap_flushing_wq);
  2336. spin_lock_init(&mdsc->dentry_lru_lock);
  2337. INIT_LIST_HEAD(&mdsc->dentry_lru);
  2338. return 0;
  2339. }
  2340. /*
  2341. * Wait for safe replies on open mds requests. If we time out, drop
  2342. * all requests from the tree to avoid dangling dentry refs.
  2343. */
  2344. static void wait_requests(struct ceph_mds_client *mdsc)
  2345. {
  2346. struct ceph_mds_request *req;
  2347. struct ceph_client *client = mdsc->client;
  2348. mutex_lock(&mdsc->mutex);
  2349. if (__get_oldest_req(mdsc)) {
  2350. mutex_unlock(&mdsc->mutex);
  2351. dout("wait_requests waiting for requests\n");
  2352. wait_for_completion_timeout(&mdsc->safe_umount_waiters,
  2353. client->mount_args->mount_timeout * HZ);
  2354. /* tear down remaining requests */
  2355. mutex_lock(&mdsc->mutex);
  2356. while ((req = __get_oldest_req(mdsc))) {
  2357. dout("wait_requests timed out on tid %llu\n",
  2358. req->r_tid);
  2359. __unregister_request(mdsc, req);
  2360. }
  2361. }
  2362. mutex_unlock(&mdsc->mutex);
  2363. dout("wait_requests done\n");
  2364. }
  2365. /*
  2366. * called before mount is ro, and before dentries are torn down.
  2367. * (hmm, does this still race with new lookups?)
  2368. */
  2369. void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
  2370. {
  2371. dout("pre_umount\n");
  2372. mdsc->stopping = 1;
  2373. drop_leases(mdsc);
  2374. ceph_flush_dirty_caps(mdsc);
  2375. wait_requests(mdsc);
  2376. }
  2377. /*
  2378. * wait for all write mds requests to flush.
  2379. */
  2380. static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
  2381. {
  2382. struct ceph_mds_request *req = NULL, *nextreq;
  2383. struct rb_node *n;
  2384. mutex_lock(&mdsc->mutex);
  2385. dout("wait_unsafe_requests want %lld\n", want_tid);
  2386. restart:
  2387. req = __get_oldest_req(mdsc);
  2388. while (req && req->r_tid <= want_tid) {
  2389. /* find next request */
  2390. n = rb_next(&req->r_node);
  2391. if (n)
  2392. nextreq = rb_entry(n, struct ceph_mds_request, r_node);
  2393. else
  2394. nextreq = NULL;
  2395. if ((req->r_op & CEPH_MDS_OP_WRITE)) {
  2396. /* write op */
  2397. ceph_mdsc_get_request(req);
  2398. if (nextreq)
  2399. ceph_mdsc_get_request(nextreq);
  2400. mutex_unlock(&mdsc->mutex);
  2401. dout("wait_unsafe_requests wait on %llu (want %llu)\n",
  2402. req->r_tid, want_tid);
  2403. wait_for_completion(&req->r_safe_completion);
  2404. mutex_lock(&mdsc->mutex);
  2405. ceph_mdsc_put_request(req);
  2406. if (!nextreq)
  2407. break; /* next dne before, so we're done! */
  2408. if (RB_EMPTY_NODE(&nextreq->r_node)) {
  2409. /* next request was removed from tree */
  2410. ceph_mdsc_put_request(nextreq);
  2411. goto restart;
  2412. }
  2413. ceph_mdsc_put_request(nextreq); /* won't go away */
  2414. }
  2415. req = nextreq;
  2416. }
  2417. mutex_unlock(&mdsc->mutex);
  2418. dout("wait_unsafe_requests done\n");
  2419. }
  2420. void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
  2421. {
  2422. u64 want_tid, want_flush;
  2423. dout("sync\n");
  2424. mutex_lock(&mdsc->mutex);
  2425. want_tid = mdsc->last_tid;
  2426. want_flush = mdsc->cap_flush_seq;
  2427. mutex_unlock(&mdsc->mutex);
  2428. dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
  2429. ceph_flush_dirty_caps(mdsc);
  2430. wait_unsafe_requests(mdsc, want_tid);
  2431. wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
  2432. }
  2433. /*
  2434. * called after sb is ro.
  2435. */
  2436. void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
  2437. {
  2438. struct ceph_mds_session *session;
  2439. int i;
  2440. int n;
  2441. struct ceph_client *client = mdsc->client;
  2442. unsigned long started, timeout = client->mount_args->mount_timeout * HZ;
  2443. dout("close_sessions\n");
  2444. mutex_lock(&mdsc->mutex);
  2445. /* close sessions */
  2446. started = jiffies;
  2447. while (time_before(jiffies, started + timeout)) {
  2448. dout("closing sessions\n");
  2449. n = 0;
  2450. for (i = 0; i < mdsc->max_sessions; i++) {
  2451. session = __ceph_lookup_mds_session(mdsc, i);
  2452. if (!session)
  2453. continue;
  2454. mutex_unlock(&mdsc->mutex);
  2455. mutex_lock(&session->s_mutex);
  2456. __close_session(mdsc, session);
  2457. mutex_unlock(&session->s_mutex);
  2458. ceph_put_mds_session(session);
  2459. mutex_lock(&mdsc->mutex);
  2460. n++;
  2461. }
  2462. if (n == 0)
  2463. break;
  2464. if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
  2465. break;
  2466. dout("waiting for sessions to close\n");
  2467. mutex_unlock(&mdsc->mutex);
  2468. wait_for_completion_timeout(&mdsc->session_close_waiters,
  2469. timeout);
  2470. mutex_lock(&mdsc->mutex);
  2471. }
  2472. /* tear down remaining sessions */
  2473. for (i = 0; i < mdsc->max_sessions; i++) {
  2474. if (mdsc->sessions[i]) {
  2475. session = get_session(mdsc->sessions[i]);
  2476. __unregister_session(mdsc, session);
  2477. mutex_unlock(&mdsc->mutex);
  2478. mutex_lock(&session->s_mutex);
  2479. remove_session_caps(session);
  2480. mutex_unlock(&session->s_mutex);
  2481. ceph_put_mds_session(session);
  2482. mutex_lock(&mdsc->mutex);
  2483. }
  2484. }
  2485. WARN_ON(!list_empty(&mdsc->cap_delay_list));
  2486. mutex_unlock(&mdsc->mutex);
  2487. ceph_cleanup_empty_realms(mdsc);
  2488. cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
  2489. dout("stopped\n");
  2490. }
  2491. void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
  2492. {
  2493. dout("stop\n");
  2494. cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
  2495. if (mdsc->mdsmap)
  2496. ceph_mdsmap_destroy(mdsc->mdsmap);
  2497. kfree(mdsc->sessions);
  2498. }
  2499. /*
  2500. * handle mds map update.
  2501. */
  2502. void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
  2503. {
  2504. u32 epoch;
  2505. u32 maplen;
  2506. void *p = msg->front.iov_base;
  2507. void *end = p + msg->front.iov_len;
  2508. struct ceph_mdsmap *newmap, *oldmap;
  2509. struct ceph_fsid fsid;
  2510. int err = -EINVAL;
  2511. ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
  2512. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  2513. if (ceph_check_fsid(mdsc->client, &fsid) < 0)
  2514. return;
  2515. epoch = ceph_decode_32(&p);
  2516. maplen = ceph_decode_32(&p);
  2517. dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
  2518. /* do we need it? */
  2519. ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
  2520. mutex_lock(&mdsc->mutex);
  2521. if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
  2522. dout("handle_map epoch %u <= our %u\n",
  2523. epoch, mdsc->mdsmap->m_epoch);
  2524. mutex_unlock(&mdsc->mutex);
  2525. return;
  2526. }
  2527. newmap = ceph_mdsmap_decode(&p, end);
  2528. if (IS_ERR(newmap)) {
  2529. err = PTR_ERR(newmap);
  2530. goto bad_unlock;
  2531. }
  2532. /* swap into place */
  2533. if (mdsc->mdsmap) {
  2534. oldmap = mdsc->mdsmap;
  2535. mdsc->mdsmap = newmap;
  2536. check_new_map(mdsc, newmap, oldmap);
  2537. ceph_mdsmap_destroy(oldmap);
  2538. } else {
  2539. mdsc->mdsmap = newmap; /* first mds map */
  2540. }
  2541. mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
  2542. __wake_requests(mdsc, &mdsc->waiting_for_map);
  2543. mutex_unlock(&mdsc->mutex);
  2544. schedule_delayed(mdsc);
  2545. return;
  2546. bad_unlock:
  2547. mutex_unlock(&mdsc->mutex);
  2548. bad:
  2549. pr_err("error decoding mdsmap %d\n", err);
  2550. return;
  2551. }
  2552. static struct ceph_connection *con_get(struct ceph_connection *con)
  2553. {
  2554. struct ceph_mds_session *s = con->private;
  2555. if (get_session(s)) {
  2556. dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
  2557. return con;
  2558. }
  2559. dout("mdsc con_get %p FAIL\n", s);
  2560. return NULL;
  2561. }
  2562. static void con_put(struct ceph_connection *con)
  2563. {
  2564. struct ceph_mds_session *s = con->private;
  2565. ceph_put_mds_session(s);
  2566. dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
  2567. }
  2568. /*
  2569. * if the client is unresponsive for long enough, the mds will kill
  2570. * the session entirely.
  2571. */
  2572. static void peer_reset(struct ceph_connection *con)
  2573. {
  2574. struct ceph_mds_session *s = con->private;
  2575. pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n",
  2576. s->s_mds);
  2577. }
  2578. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  2579. {
  2580. struct ceph_mds_session *s = con->private;
  2581. struct ceph_mds_client *mdsc = s->s_mdsc;
  2582. int type = le16_to_cpu(msg->hdr.type);
  2583. mutex_lock(&mdsc->mutex);
  2584. if (__verify_registered_session(mdsc, s) < 0) {
  2585. mutex_unlock(&mdsc->mutex);
  2586. goto out;
  2587. }
  2588. mutex_unlock(&mdsc->mutex);
  2589. switch (type) {
  2590. case CEPH_MSG_MDS_MAP:
  2591. ceph_mdsc_handle_map(mdsc, msg);
  2592. break;
  2593. case CEPH_MSG_CLIENT_SESSION:
  2594. handle_session(s, msg);
  2595. break;
  2596. case CEPH_MSG_CLIENT_REPLY:
  2597. handle_reply(s, msg);
  2598. break;
  2599. case CEPH_MSG_CLIENT_REQUEST_FORWARD:
  2600. handle_forward(mdsc, s, msg);
  2601. break;
  2602. case CEPH_MSG_CLIENT_CAPS:
  2603. ceph_handle_caps(s, msg);
  2604. break;
  2605. case CEPH_MSG_CLIENT_SNAP:
  2606. ceph_handle_snap(mdsc, s, msg);
  2607. break;
  2608. case CEPH_MSG_CLIENT_LEASE:
  2609. handle_lease(mdsc, s, msg);
  2610. break;
  2611. default:
  2612. pr_err("received unknown message type %d %s\n", type,
  2613. ceph_msg_type_name(type));
  2614. }
  2615. out:
  2616. ceph_msg_put(msg);
  2617. }
  2618. /*
  2619. * authentication
  2620. */
  2621. static int get_authorizer(struct ceph_connection *con,
  2622. void **buf, int *len, int *proto,
  2623. void **reply_buf, int *reply_len, int force_new)
  2624. {
  2625. struct ceph_mds_session *s = con->private;
  2626. struct ceph_mds_client *mdsc = s->s_mdsc;
  2627. struct ceph_auth_client *ac = mdsc->client->monc.auth;
  2628. int ret = 0;
  2629. if (force_new && s->s_authorizer) {
  2630. ac->ops->destroy_authorizer(ac, s->s_authorizer);
  2631. s->s_authorizer = NULL;
  2632. }
  2633. if (s->s_authorizer == NULL) {
  2634. if (ac->ops->create_authorizer) {
  2635. ret = ac->ops->create_authorizer(
  2636. ac, CEPH_ENTITY_TYPE_MDS,
  2637. &s->s_authorizer,
  2638. &s->s_authorizer_buf,
  2639. &s->s_authorizer_buf_len,
  2640. &s->s_authorizer_reply_buf,
  2641. &s->s_authorizer_reply_buf_len);
  2642. if (ret)
  2643. return ret;
  2644. }
  2645. }
  2646. *proto = ac->protocol;
  2647. *buf = s->s_authorizer_buf;
  2648. *len = s->s_authorizer_buf_len;
  2649. *reply_buf = s->s_authorizer_reply_buf;
  2650. *reply_len = s->s_authorizer_reply_buf_len;
  2651. return 0;
  2652. }
  2653. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  2654. {
  2655. struct ceph_mds_session *s = con->private;
  2656. struct ceph_mds_client *mdsc = s->s_mdsc;
  2657. struct ceph_auth_client *ac = mdsc->client->monc.auth;
  2658. return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
  2659. }
  2660. static int invalidate_authorizer(struct ceph_connection *con)
  2661. {
  2662. struct ceph_mds_session *s = con->private;
  2663. struct ceph_mds_client *mdsc = s->s_mdsc;
  2664. struct ceph_auth_client *ac = mdsc->client->monc.auth;
  2665. if (ac->ops->invalidate_authorizer)
  2666. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
  2667. return ceph_monc_validate_auth(&mdsc->client->monc);
  2668. }
  2669. const static struct ceph_connection_operations mds_con_ops = {
  2670. .get = con_get,
  2671. .put = con_put,
  2672. .dispatch = dispatch,
  2673. .get_authorizer = get_authorizer,
  2674. .verify_authorizer_reply = verify_authorizer_reply,
  2675. .invalidate_authorizer = invalidate_authorizer,
  2676. .peer_reset = peer_reset,
  2677. };
  2678. /* eof */