mds_client.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141
  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. static const 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), GFP_NOFS);
  590. if (!msg) {
  591. pr_err("create_session_msg ENOMEM creating msg\n");
  592. return NULL;
  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. /* wait for mds to go active? */
  611. mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
  612. dout("open_session to mds%d (%s)\n", mds,
  613. ceph_mds_state_name(mstate));
  614. session->s_state = CEPH_MDS_SESSION_OPENING;
  615. session->s_renew_requested = jiffies;
  616. /* send connect message */
  617. msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
  618. if (!msg)
  619. return -ENOMEM;
  620. ceph_con_send(&session->s_con, msg);
  621. return 0;
  622. }
  623. /*
  624. * session caps
  625. */
  626. /*
  627. * Free preallocated cap messages assigned to this session
  628. */
  629. static void cleanup_cap_releases(struct ceph_mds_session *session)
  630. {
  631. struct ceph_msg *msg;
  632. spin_lock(&session->s_cap_lock);
  633. while (!list_empty(&session->s_cap_releases)) {
  634. msg = list_first_entry(&session->s_cap_releases,
  635. struct ceph_msg, list_head);
  636. list_del_init(&msg->list_head);
  637. ceph_msg_put(msg);
  638. }
  639. while (!list_empty(&session->s_cap_releases_done)) {
  640. msg = list_first_entry(&session->s_cap_releases_done,
  641. struct ceph_msg, list_head);
  642. list_del_init(&msg->list_head);
  643. ceph_msg_put(msg);
  644. }
  645. spin_unlock(&session->s_cap_lock);
  646. }
  647. /*
  648. * Helper to safely iterate over all caps associated with a session, with
  649. * special care taken to handle a racing __ceph_remove_cap().
  650. *
  651. * Caller must hold session s_mutex.
  652. */
  653. static int iterate_session_caps(struct ceph_mds_session *session,
  654. int (*cb)(struct inode *, struct ceph_cap *,
  655. void *), void *arg)
  656. {
  657. struct list_head *p;
  658. struct ceph_cap *cap;
  659. struct inode *inode, *last_inode = NULL;
  660. struct ceph_cap *old_cap = NULL;
  661. int ret;
  662. dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
  663. spin_lock(&session->s_cap_lock);
  664. p = session->s_caps.next;
  665. while (p != &session->s_caps) {
  666. cap = list_entry(p, struct ceph_cap, session_caps);
  667. inode = igrab(&cap->ci->vfs_inode);
  668. if (!inode) {
  669. p = p->next;
  670. continue;
  671. }
  672. session->s_cap_iterator = cap;
  673. spin_unlock(&session->s_cap_lock);
  674. if (last_inode) {
  675. iput(last_inode);
  676. last_inode = NULL;
  677. }
  678. if (old_cap) {
  679. ceph_put_cap(old_cap);
  680. old_cap = NULL;
  681. }
  682. ret = cb(inode, cap, arg);
  683. last_inode = inode;
  684. spin_lock(&session->s_cap_lock);
  685. p = p->next;
  686. if (cap->ci == NULL) {
  687. dout("iterate_session_caps finishing cap %p removal\n",
  688. cap);
  689. BUG_ON(cap->session != session);
  690. list_del_init(&cap->session_caps);
  691. session->s_nr_caps--;
  692. cap->session = NULL;
  693. old_cap = cap; /* put_cap it w/o locks held */
  694. }
  695. if (ret < 0)
  696. goto out;
  697. }
  698. ret = 0;
  699. out:
  700. session->s_cap_iterator = NULL;
  701. spin_unlock(&session->s_cap_lock);
  702. if (last_inode)
  703. iput(last_inode);
  704. if (old_cap)
  705. ceph_put_cap(old_cap);
  706. return ret;
  707. }
  708. static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
  709. void *arg)
  710. {
  711. struct ceph_inode_info *ci = ceph_inode(inode);
  712. int drop = 0;
  713. dout("removing cap %p, ci is %p, inode is %p\n",
  714. cap, ci, &ci->vfs_inode);
  715. spin_lock(&inode->i_lock);
  716. __ceph_remove_cap(cap);
  717. if (!__ceph_is_any_real_caps(ci)) {
  718. struct ceph_mds_client *mdsc =
  719. &ceph_sb_to_client(inode->i_sb)->mdsc;
  720. spin_lock(&mdsc->cap_dirty_lock);
  721. if (!list_empty(&ci->i_dirty_item)) {
  722. pr_info(" dropping dirty %s state for %p %lld\n",
  723. ceph_cap_string(ci->i_dirty_caps),
  724. inode, ceph_ino(inode));
  725. ci->i_dirty_caps = 0;
  726. list_del_init(&ci->i_dirty_item);
  727. drop = 1;
  728. }
  729. if (!list_empty(&ci->i_flushing_item)) {
  730. pr_info(" dropping dirty+flushing %s state for %p %lld\n",
  731. ceph_cap_string(ci->i_flushing_caps),
  732. inode, ceph_ino(inode));
  733. ci->i_flushing_caps = 0;
  734. list_del_init(&ci->i_flushing_item);
  735. mdsc->num_cap_flushing--;
  736. drop = 1;
  737. }
  738. if (drop && ci->i_wrbuffer_ref) {
  739. pr_info(" dropping dirty data for %p %lld\n",
  740. inode, ceph_ino(inode));
  741. ci->i_wrbuffer_ref = 0;
  742. ci->i_wrbuffer_ref_head = 0;
  743. drop++;
  744. }
  745. spin_unlock(&mdsc->cap_dirty_lock);
  746. }
  747. spin_unlock(&inode->i_lock);
  748. while (drop--)
  749. iput(inode);
  750. return 0;
  751. }
  752. /*
  753. * caller must hold session s_mutex
  754. */
  755. static void remove_session_caps(struct ceph_mds_session *session)
  756. {
  757. dout("remove_session_caps on %p\n", session);
  758. iterate_session_caps(session, remove_session_caps_cb, NULL);
  759. BUG_ON(session->s_nr_caps > 0);
  760. BUG_ON(!list_empty(&session->s_cap_flushing));
  761. cleanup_cap_releases(session);
  762. }
  763. /*
  764. * wake up any threads waiting on this session's caps. if the cap is
  765. * old (didn't get renewed on the client reconnect), remove it now.
  766. *
  767. * caller must hold s_mutex.
  768. */
  769. static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
  770. void *arg)
  771. {
  772. struct ceph_inode_info *ci = ceph_inode(inode);
  773. wake_up(&ci->i_cap_wq);
  774. if (arg) {
  775. spin_lock(&inode->i_lock);
  776. ci->i_wanted_max_size = 0;
  777. ci->i_requested_max_size = 0;
  778. spin_unlock(&inode->i_lock);
  779. }
  780. return 0;
  781. }
  782. static void wake_up_session_caps(struct ceph_mds_session *session,
  783. int reconnect)
  784. {
  785. dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
  786. iterate_session_caps(session, wake_up_session_cb,
  787. (void *)(unsigned long)reconnect);
  788. }
  789. /*
  790. * Send periodic message to MDS renewing all currently held caps. The
  791. * ack will reset the expiration for all caps from this session.
  792. *
  793. * caller holds s_mutex
  794. */
  795. static int send_renew_caps(struct ceph_mds_client *mdsc,
  796. struct ceph_mds_session *session)
  797. {
  798. struct ceph_msg *msg;
  799. int state;
  800. if (time_after_eq(jiffies, session->s_cap_ttl) &&
  801. time_after_eq(session->s_cap_ttl, session->s_renew_requested))
  802. pr_info("mds%d caps stale\n", session->s_mds);
  803. session->s_renew_requested = jiffies;
  804. /* do not try to renew caps until a recovering mds has reconnected
  805. * with its clients. */
  806. state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
  807. if (state < CEPH_MDS_STATE_RECONNECT) {
  808. dout("send_renew_caps ignoring mds%d (%s)\n",
  809. session->s_mds, ceph_mds_state_name(state));
  810. return 0;
  811. }
  812. dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
  813. ceph_mds_state_name(state));
  814. msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
  815. ++session->s_renew_seq);
  816. if (!msg)
  817. return -ENOMEM;
  818. ceph_con_send(&session->s_con, msg);
  819. return 0;
  820. }
  821. /*
  822. * Note new cap ttl, and any transition from stale -> not stale (fresh?).
  823. *
  824. * Called under session->s_mutex
  825. */
  826. static void renewed_caps(struct ceph_mds_client *mdsc,
  827. struct ceph_mds_session *session, int is_renew)
  828. {
  829. int was_stale;
  830. int wake = 0;
  831. spin_lock(&session->s_cap_lock);
  832. was_stale = is_renew && (session->s_cap_ttl == 0 ||
  833. time_after_eq(jiffies, session->s_cap_ttl));
  834. session->s_cap_ttl = session->s_renew_requested +
  835. mdsc->mdsmap->m_session_timeout*HZ;
  836. if (was_stale) {
  837. if (time_before(jiffies, session->s_cap_ttl)) {
  838. pr_info("mds%d caps renewed\n", session->s_mds);
  839. wake = 1;
  840. } else {
  841. pr_info("mds%d caps still stale\n", session->s_mds);
  842. }
  843. }
  844. dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
  845. session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
  846. time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
  847. spin_unlock(&session->s_cap_lock);
  848. if (wake)
  849. wake_up_session_caps(session, 0);
  850. }
  851. /*
  852. * send a session close request
  853. */
  854. static int request_close_session(struct ceph_mds_client *mdsc,
  855. struct ceph_mds_session *session)
  856. {
  857. struct ceph_msg *msg;
  858. dout("request_close_session mds%d state %s seq %lld\n",
  859. session->s_mds, session_state_name(session->s_state),
  860. session->s_seq);
  861. msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
  862. if (!msg)
  863. return -ENOMEM;
  864. ceph_con_send(&session->s_con, msg);
  865. return 0;
  866. }
  867. /*
  868. * Called with s_mutex held.
  869. */
  870. static int __close_session(struct ceph_mds_client *mdsc,
  871. struct ceph_mds_session *session)
  872. {
  873. if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
  874. return 0;
  875. session->s_state = CEPH_MDS_SESSION_CLOSING;
  876. return request_close_session(mdsc, session);
  877. }
  878. /*
  879. * Trim old(er) caps.
  880. *
  881. * Because we can't cache an inode without one or more caps, we do
  882. * this indirectly: if a cap is unused, we prune its aliases, at which
  883. * point the inode will hopefully get dropped to.
  884. *
  885. * Yes, this is a bit sloppy. Our only real goal here is to respond to
  886. * memory pressure from the MDS, though, so it needn't be perfect.
  887. */
  888. static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
  889. {
  890. struct ceph_mds_session *session = arg;
  891. struct ceph_inode_info *ci = ceph_inode(inode);
  892. int used, oissued, mine;
  893. if (session->s_trim_caps <= 0)
  894. return -1;
  895. spin_lock(&inode->i_lock);
  896. mine = cap->issued | cap->implemented;
  897. used = __ceph_caps_used(ci);
  898. oissued = __ceph_caps_issued_other(ci, cap);
  899. dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
  900. inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
  901. ceph_cap_string(used));
  902. if (ci->i_dirty_caps)
  903. goto out; /* dirty caps */
  904. if ((used & ~oissued) & mine)
  905. goto out; /* we need these caps */
  906. session->s_trim_caps--;
  907. if (oissued) {
  908. /* we aren't the only cap.. just remove us */
  909. __ceph_remove_cap(cap);
  910. } else {
  911. /* try to drop referring dentries */
  912. spin_unlock(&inode->i_lock);
  913. d_prune_aliases(inode);
  914. dout("trim_caps_cb %p cap %p pruned, count now %d\n",
  915. inode, cap, atomic_read(&inode->i_count));
  916. return 0;
  917. }
  918. out:
  919. spin_unlock(&inode->i_lock);
  920. return 0;
  921. }
  922. /*
  923. * Trim session cap count down to some max number.
  924. */
  925. static int trim_caps(struct ceph_mds_client *mdsc,
  926. struct ceph_mds_session *session,
  927. int max_caps)
  928. {
  929. int trim_caps = session->s_nr_caps - max_caps;
  930. dout("trim_caps mds%d start: %d / %d, trim %d\n",
  931. session->s_mds, session->s_nr_caps, max_caps, trim_caps);
  932. if (trim_caps > 0) {
  933. session->s_trim_caps = trim_caps;
  934. iterate_session_caps(session, trim_caps_cb, session);
  935. dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
  936. session->s_mds, session->s_nr_caps, max_caps,
  937. trim_caps - session->s_trim_caps);
  938. session->s_trim_caps = 0;
  939. }
  940. return 0;
  941. }
  942. /*
  943. * Allocate cap_release messages. If there is a partially full message
  944. * in the queue, try to allocate enough to cover it's remainder, so that
  945. * we can send it immediately.
  946. *
  947. * Called under s_mutex.
  948. */
  949. static int add_cap_releases(struct ceph_mds_client *mdsc,
  950. struct ceph_mds_session *session,
  951. int extra)
  952. {
  953. struct ceph_msg *msg;
  954. struct ceph_mds_cap_release *head;
  955. int err = -ENOMEM;
  956. if (extra < 0)
  957. extra = mdsc->client->mount_args->cap_release_safety;
  958. spin_lock(&session->s_cap_lock);
  959. if (!list_empty(&session->s_cap_releases)) {
  960. msg = list_first_entry(&session->s_cap_releases,
  961. struct ceph_msg,
  962. list_head);
  963. head = msg->front.iov_base;
  964. extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
  965. }
  966. while (session->s_num_cap_releases < session->s_nr_caps + extra) {
  967. spin_unlock(&session->s_cap_lock);
  968. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
  969. GFP_NOFS);
  970. if (!msg)
  971. goto out_unlocked;
  972. dout("add_cap_releases %p msg %p now %d\n", session, msg,
  973. (int)msg->front.iov_len);
  974. head = msg->front.iov_base;
  975. head->num = cpu_to_le32(0);
  976. msg->front.iov_len = sizeof(*head);
  977. spin_lock(&session->s_cap_lock);
  978. list_add(&msg->list_head, &session->s_cap_releases);
  979. session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
  980. }
  981. if (!list_empty(&session->s_cap_releases)) {
  982. msg = list_first_entry(&session->s_cap_releases,
  983. struct ceph_msg,
  984. list_head);
  985. head = msg->front.iov_base;
  986. if (head->num) {
  987. dout(" queueing non-full %p (%d)\n", msg,
  988. le32_to_cpu(head->num));
  989. list_move_tail(&msg->list_head,
  990. &session->s_cap_releases_done);
  991. session->s_num_cap_releases -=
  992. CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
  993. }
  994. }
  995. err = 0;
  996. spin_unlock(&session->s_cap_lock);
  997. out_unlocked:
  998. return err;
  999. }
  1000. /*
  1001. * flush all dirty inode data to disk.
  1002. *
  1003. * returns true if we've flushed through want_flush_seq
  1004. */
  1005. static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
  1006. {
  1007. int mds, ret = 1;
  1008. dout("check_cap_flush want %lld\n", want_flush_seq);
  1009. mutex_lock(&mdsc->mutex);
  1010. for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
  1011. struct ceph_mds_session *session = mdsc->sessions[mds];
  1012. if (!session)
  1013. continue;
  1014. get_session(session);
  1015. mutex_unlock(&mdsc->mutex);
  1016. mutex_lock(&session->s_mutex);
  1017. if (!list_empty(&session->s_cap_flushing)) {
  1018. struct ceph_inode_info *ci =
  1019. list_entry(session->s_cap_flushing.next,
  1020. struct ceph_inode_info,
  1021. i_flushing_item);
  1022. struct inode *inode = &ci->vfs_inode;
  1023. spin_lock(&inode->i_lock);
  1024. if (ci->i_cap_flush_seq <= want_flush_seq) {
  1025. dout("check_cap_flush still flushing %p "
  1026. "seq %lld <= %lld to mds%d\n", inode,
  1027. ci->i_cap_flush_seq, want_flush_seq,
  1028. session->s_mds);
  1029. ret = 0;
  1030. }
  1031. spin_unlock(&inode->i_lock);
  1032. }
  1033. mutex_unlock(&session->s_mutex);
  1034. ceph_put_mds_session(session);
  1035. if (!ret)
  1036. return ret;
  1037. mutex_lock(&mdsc->mutex);
  1038. }
  1039. mutex_unlock(&mdsc->mutex);
  1040. dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
  1041. return ret;
  1042. }
  1043. /*
  1044. * called under s_mutex
  1045. */
  1046. static void send_cap_releases(struct ceph_mds_client *mdsc,
  1047. struct ceph_mds_session *session)
  1048. {
  1049. struct ceph_msg *msg;
  1050. dout("send_cap_releases mds%d\n", session->s_mds);
  1051. spin_lock(&session->s_cap_lock);
  1052. while (!list_empty(&session->s_cap_releases_done)) {
  1053. msg = list_first_entry(&session->s_cap_releases_done,
  1054. struct ceph_msg, list_head);
  1055. list_del_init(&msg->list_head);
  1056. spin_unlock(&session->s_cap_lock);
  1057. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1058. dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
  1059. ceph_con_send(&session->s_con, msg);
  1060. spin_lock(&session->s_cap_lock);
  1061. }
  1062. spin_unlock(&session->s_cap_lock);
  1063. }
  1064. static void discard_cap_releases(struct ceph_mds_client *mdsc,
  1065. struct ceph_mds_session *session)
  1066. {
  1067. struct ceph_msg *msg;
  1068. struct ceph_mds_cap_release *head;
  1069. unsigned num;
  1070. dout("discard_cap_releases mds%d\n", session->s_mds);
  1071. spin_lock(&session->s_cap_lock);
  1072. /* zero out the in-progress message */
  1073. msg = list_first_entry(&session->s_cap_releases,
  1074. struct ceph_msg, list_head);
  1075. head = msg->front.iov_base;
  1076. num = le32_to_cpu(head->num);
  1077. dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
  1078. head->num = cpu_to_le32(0);
  1079. session->s_num_cap_releases += num;
  1080. /* requeue completed messages */
  1081. while (!list_empty(&session->s_cap_releases_done)) {
  1082. msg = list_first_entry(&session->s_cap_releases_done,
  1083. struct ceph_msg, list_head);
  1084. list_del_init(&msg->list_head);
  1085. head = msg->front.iov_base;
  1086. num = le32_to_cpu(head->num);
  1087. dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
  1088. num);
  1089. session->s_num_cap_releases += num;
  1090. head->num = cpu_to_le32(0);
  1091. msg->front.iov_len = sizeof(*head);
  1092. list_add(&msg->list_head, &session->s_cap_releases);
  1093. }
  1094. spin_unlock(&session->s_cap_lock);
  1095. }
  1096. /*
  1097. * requests
  1098. */
  1099. /*
  1100. * Create an mds request.
  1101. */
  1102. struct ceph_mds_request *
  1103. ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
  1104. {
  1105. struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
  1106. if (!req)
  1107. return ERR_PTR(-ENOMEM);
  1108. mutex_init(&req->r_fill_mutex);
  1109. req->r_started = jiffies;
  1110. req->r_resend_mds = -1;
  1111. INIT_LIST_HEAD(&req->r_unsafe_dir_item);
  1112. req->r_fmode = -1;
  1113. kref_init(&req->r_kref);
  1114. INIT_LIST_HEAD(&req->r_wait);
  1115. init_completion(&req->r_completion);
  1116. init_completion(&req->r_safe_completion);
  1117. INIT_LIST_HEAD(&req->r_unsafe_item);
  1118. req->r_op = op;
  1119. req->r_direct_mode = mode;
  1120. return req;
  1121. }
  1122. /*
  1123. * return oldest (lowest) request, tid in request tree, 0 if none.
  1124. *
  1125. * called under mdsc->mutex.
  1126. */
  1127. static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
  1128. {
  1129. if (RB_EMPTY_ROOT(&mdsc->request_tree))
  1130. return NULL;
  1131. return rb_entry(rb_first(&mdsc->request_tree),
  1132. struct ceph_mds_request, r_node);
  1133. }
  1134. static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
  1135. {
  1136. struct ceph_mds_request *req = __get_oldest_req(mdsc);
  1137. if (req)
  1138. return req->r_tid;
  1139. return 0;
  1140. }
  1141. /*
  1142. * Build a dentry's path. Allocate on heap; caller must kfree. Based
  1143. * on build_path_from_dentry in fs/cifs/dir.c.
  1144. *
  1145. * If @stop_on_nosnap, generate path relative to the first non-snapped
  1146. * inode.
  1147. *
  1148. * Encode hidden .snap dirs as a double /, i.e.
  1149. * foo/.snap/bar -> foo//bar
  1150. */
  1151. char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
  1152. int stop_on_nosnap)
  1153. {
  1154. struct dentry *temp;
  1155. char *path;
  1156. int len, pos;
  1157. if (dentry == NULL)
  1158. return ERR_PTR(-EINVAL);
  1159. retry:
  1160. len = 0;
  1161. for (temp = dentry; !IS_ROOT(temp);) {
  1162. struct inode *inode = temp->d_inode;
  1163. if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
  1164. len++; /* slash only */
  1165. else if (stop_on_nosnap && inode &&
  1166. ceph_snap(inode) == CEPH_NOSNAP)
  1167. break;
  1168. else
  1169. len += 1 + temp->d_name.len;
  1170. temp = temp->d_parent;
  1171. if (temp == NULL) {
  1172. pr_err("build_path corrupt dentry %p\n", dentry);
  1173. return ERR_PTR(-EINVAL);
  1174. }
  1175. }
  1176. if (len)
  1177. len--; /* no leading '/' */
  1178. path = kmalloc(len+1, GFP_NOFS);
  1179. if (path == NULL)
  1180. return ERR_PTR(-ENOMEM);
  1181. pos = len;
  1182. path[pos] = 0; /* trailing null */
  1183. for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
  1184. struct inode *inode = temp->d_inode;
  1185. if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
  1186. dout("build_path path+%d: %p SNAPDIR\n",
  1187. pos, temp);
  1188. } else if (stop_on_nosnap && inode &&
  1189. ceph_snap(inode) == CEPH_NOSNAP) {
  1190. break;
  1191. } else {
  1192. pos -= temp->d_name.len;
  1193. if (pos < 0)
  1194. break;
  1195. strncpy(path + pos, temp->d_name.name,
  1196. temp->d_name.len);
  1197. }
  1198. if (pos)
  1199. path[--pos] = '/';
  1200. temp = temp->d_parent;
  1201. if (temp == NULL) {
  1202. pr_err("build_path corrupt dentry\n");
  1203. kfree(path);
  1204. return ERR_PTR(-EINVAL);
  1205. }
  1206. }
  1207. if (pos != 0) {
  1208. pr_err("build_path did not end path lookup where "
  1209. "expected, namelen is %d, pos is %d\n", len, pos);
  1210. /* presumably this is only possible if racing with a
  1211. rename of one of the parent directories (we can not
  1212. lock the dentries above us to prevent this, but
  1213. retrying should be harmless) */
  1214. kfree(path);
  1215. goto retry;
  1216. }
  1217. *base = ceph_ino(temp->d_inode);
  1218. *plen = len;
  1219. dout("build_path on %p %d built %llx '%.*s'\n",
  1220. dentry, atomic_read(&dentry->d_count), *base, len, path);
  1221. return path;
  1222. }
  1223. static int build_dentry_path(struct dentry *dentry,
  1224. const char **ppath, int *ppathlen, u64 *pino,
  1225. int *pfreepath)
  1226. {
  1227. char *path;
  1228. if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
  1229. *pino = ceph_ino(dentry->d_parent->d_inode);
  1230. *ppath = dentry->d_name.name;
  1231. *ppathlen = dentry->d_name.len;
  1232. return 0;
  1233. }
  1234. path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
  1235. if (IS_ERR(path))
  1236. return PTR_ERR(path);
  1237. *ppath = path;
  1238. *pfreepath = 1;
  1239. return 0;
  1240. }
  1241. static int build_inode_path(struct inode *inode,
  1242. const char **ppath, int *ppathlen, u64 *pino,
  1243. int *pfreepath)
  1244. {
  1245. struct dentry *dentry;
  1246. char *path;
  1247. if (ceph_snap(inode) == CEPH_NOSNAP) {
  1248. *pino = ceph_ino(inode);
  1249. *ppathlen = 0;
  1250. return 0;
  1251. }
  1252. dentry = d_find_alias(inode);
  1253. path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
  1254. dput(dentry);
  1255. if (IS_ERR(path))
  1256. return PTR_ERR(path);
  1257. *ppath = path;
  1258. *pfreepath = 1;
  1259. return 0;
  1260. }
  1261. /*
  1262. * request arguments may be specified via an inode *, a dentry *, or
  1263. * an explicit ino+path.
  1264. */
  1265. static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
  1266. const char *rpath, u64 rino,
  1267. const char **ppath, int *pathlen,
  1268. u64 *ino, int *freepath)
  1269. {
  1270. int r = 0;
  1271. if (rinode) {
  1272. r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
  1273. dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
  1274. ceph_snap(rinode));
  1275. } else if (rdentry) {
  1276. r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
  1277. dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
  1278. *ppath);
  1279. } else if (rpath) {
  1280. *ino = rino;
  1281. *ppath = rpath;
  1282. *pathlen = strlen(rpath);
  1283. dout(" path %.*s\n", *pathlen, rpath);
  1284. }
  1285. return r;
  1286. }
  1287. /*
  1288. * called under mdsc->mutex
  1289. */
  1290. static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
  1291. struct ceph_mds_request *req,
  1292. int mds)
  1293. {
  1294. struct ceph_msg *msg;
  1295. struct ceph_mds_request_head *head;
  1296. const char *path1 = NULL;
  1297. const char *path2 = NULL;
  1298. u64 ino1 = 0, ino2 = 0;
  1299. int pathlen1 = 0, pathlen2 = 0;
  1300. int freepath1 = 0, freepath2 = 0;
  1301. int len;
  1302. u16 releases;
  1303. void *p, *end;
  1304. int ret;
  1305. ret = set_request_path_attr(req->r_inode, req->r_dentry,
  1306. req->r_path1, req->r_ino1.ino,
  1307. &path1, &pathlen1, &ino1, &freepath1);
  1308. if (ret < 0) {
  1309. msg = ERR_PTR(ret);
  1310. goto out;
  1311. }
  1312. ret = set_request_path_attr(NULL, req->r_old_dentry,
  1313. req->r_path2, req->r_ino2.ino,
  1314. &path2, &pathlen2, &ino2, &freepath2);
  1315. if (ret < 0) {
  1316. msg = ERR_PTR(ret);
  1317. goto out_free1;
  1318. }
  1319. len = sizeof(*head) +
  1320. pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
  1321. /* calculate (max) length for cap releases */
  1322. len += sizeof(struct ceph_mds_request_release) *
  1323. (!!req->r_inode_drop + !!req->r_dentry_drop +
  1324. !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
  1325. if (req->r_dentry_drop)
  1326. len += req->r_dentry->d_name.len;
  1327. if (req->r_old_dentry_drop)
  1328. len += req->r_old_dentry->d_name.len;
  1329. msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
  1330. if (!msg) {
  1331. msg = ERR_PTR(-ENOMEM);
  1332. goto out_free2;
  1333. }
  1334. msg->hdr.tid = cpu_to_le64(req->r_tid);
  1335. head = msg->front.iov_base;
  1336. p = msg->front.iov_base + sizeof(*head);
  1337. end = msg->front.iov_base + msg->front.iov_len;
  1338. head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
  1339. head->op = cpu_to_le32(req->r_op);
  1340. head->caller_uid = cpu_to_le32(current_fsuid());
  1341. head->caller_gid = cpu_to_le32(current_fsgid());
  1342. head->args = req->r_args;
  1343. ceph_encode_filepath(&p, end, ino1, path1);
  1344. ceph_encode_filepath(&p, end, ino2, path2);
  1345. /* cap releases */
  1346. releases = 0;
  1347. if (req->r_inode_drop)
  1348. releases += ceph_encode_inode_release(&p,
  1349. req->r_inode ? req->r_inode : req->r_dentry->d_inode,
  1350. mds, req->r_inode_drop, req->r_inode_unless, 0);
  1351. if (req->r_dentry_drop)
  1352. releases += ceph_encode_dentry_release(&p, req->r_dentry,
  1353. mds, req->r_dentry_drop, req->r_dentry_unless);
  1354. if (req->r_old_dentry_drop)
  1355. releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
  1356. mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
  1357. if (req->r_old_inode_drop)
  1358. releases += ceph_encode_inode_release(&p,
  1359. req->r_old_dentry->d_inode,
  1360. mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
  1361. head->num_releases = cpu_to_le16(releases);
  1362. BUG_ON(p > end);
  1363. msg->front.iov_len = p - msg->front.iov_base;
  1364. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1365. msg->pages = req->r_pages;
  1366. msg->nr_pages = req->r_num_pages;
  1367. msg->hdr.data_len = cpu_to_le32(req->r_data_len);
  1368. msg->hdr.data_off = cpu_to_le16(0);
  1369. out_free2:
  1370. if (freepath2)
  1371. kfree((char *)path2);
  1372. out_free1:
  1373. if (freepath1)
  1374. kfree((char *)path1);
  1375. out:
  1376. return msg;
  1377. }
  1378. /*
  1379. * called under mdsc->mutex if error, under no mutex if
  1380. * success.
  1381. */
  1382. static void complete_request(struct ceph_mds_client *mdsc,
  1383. struct ceph_mds_request *req)
  1384. {
  1385. if (req->r_callback)
  1386. req->r_callback(mdsc, req);
  1387. else
  1388. complete(&req->r_completion);
  1389. }
  1390. /*
  1391. * called under mdsc->mutex
  1392. */
  1393. static int __prepare_send_request(struct ceph_mds_client *mdsc,
  1394. struct ceph_mds_request *req,
  1395. int mds)
  1396. {
  1397. struct ceph_mds_request_head *rhead;
  1398. struct ceph_msg *msg;
  1399. int flags = 0;
  1400. req->r_mds = mds;
  1401. req->r_attempts++;
  1402. dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
  1403. req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
  1404. if (req->r_request) {
  1405. ceph_msg_put(req->r_request);
  1406. req->r_request = NULL;
  1407. }
  1408. msg = create_request_message(mdsc, req, mds);
  1409. if (IS_ERR(msg)) {
  1410. req->r_err = PTR_ERR(msg);
  1411. complete_request(mdsc, req);
  1412. return PTR_ERR(msg);
  1413. }
  1414. req->r_request = msg;
  1415. rhead = msg->front.iov_base;
  1416. rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
  1417. if (req->r_got_unsafe)
  1418. flags |= CEPH_MDS_FLAG_REPLAY;
  1419. if (req->r_locked_dir)
  1420. flags |= CEPH_MDS_FLAG_WANT_DENTRY;
  1421. rhead->flags = cpu_to_le32(flags);
  1422. rhead->num_fwd = req->r_num_fwd;
  1423. rhead->num_retry = req->r_attempts - 1;
  1424. dout(" r_locked_dir = %p\n", req->r_locked_dir);
  1425. if (req->r_target_inode && req->r_got_unsafe)
  1426. rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
  1427. else
  1428. rhead->ino = 0;
  1429. return 0;
  1430. }
  1431. /*
  1432. * send request, or put it on the appropriate wait list.
  1433. */
  1434. static int __do_request(struct ceph_mds_client *mdsc,
  1435. struct ceph_mds_request *req)
  1436. {
  1437. struct ceph_mds_session *session = NULL;
  1438. int mds = -1;
  1439. int err = -EAGAIN;
  1440. if (req->r_err || req->r_got_result)
  1441. goto out;
  1442. if (req->r_timeout &&
  1443. time_after_eq(jiffies, req->r_started + req->r_timeout)) {
  1444. dout("do_request timed out\n");
  1445. err = -EIO;
  1446. goto finish;
  1447. }
  1448. mds = __choose_mds(mdsc, req);
  1449. if (mds < 0 ||
  1450. ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
  1451. dout("do_request no mds or not active, waiting for map\n");
  1452. list_add(&req->r_wait, &mdsc->waiting_for_map);
  1453. goto out;
  1454. }
  1455. /* get, open session */
  1456. session = __ceph_lookup_mds_session(mdsc, mds);
  1457. if (!session) {
  1458. session = register_session(mdsc, mds);
  1459. if (IS_ERR(session)) {
  1460. err = PTR_ERR(session);
  1461. goto finish;
  1462. }
  1463. }
  1464. dout("do_request mds%d session %p state %s\n", mds, session,
  1465. session_state_name(session->s_state));
  1466. if (session->s_state != CEPH_MDS_SESSION_OPEN &&
  1467. session->s_state != CEPH_MDS_SESSION_HUNG) {
  1468. if (session->s_state == CEPH_MDS_SESSION_NEW ||
  1469. session->s_state == CEPH_MDS_SESSION_CLOSING)
  1470. __open_session(mdsc, session);
  1471. list_add(&req->r_wait, &session->s_waiting);
  1472. goto out_session;
  1473. }
  1474. /* send request */
  1475. req->r_session = get_session(session);
  1476. req->r_resend_mds = -1; /* forget any previous mds hint */
  1477. if (req->r_request_started == 0) /* note request start time */
  1478. req->r_request_started = jiffies;
  1479. err = __prepare_send_request(mdsc, req, mds);
  1480. if (!err) {
  1481. ceph_msg_get(req->r_request);
  1482. ceph_con_send(&session->s_con, req->r_request);
  1483. }
  1484. out_session:
  1485. ceph_put_mds_session(session);
  1486. out:
  1487. return err;
  1488. finish:
  1489. req->r_err = err;
  1490. complete_request(mdsc, req);
  1491. goto out;
  1492. }
  1493. /*
  1494. * called under mdsc->mutex
  1495. */
  1496. static void __wake_requests(struct ceph_mds_client *mdsc,
  1497. struct list_head *head)
  1498. {
  1499. struct ceph_mds_request *req, *nreq;
  1500. list_for_each_entry_safe(req, nreq, head, r_wait) {
  1501. list_del_init(&req->r_wait);
  1502. __do_request(mdsc, req);
  1503. }
  1504. }
  1505. /*
  1506. * Wake up threads with requests pending for @mds, so that they can
  1507. * resubmit their requests to a possibly different mds.
  1508. */
  1509. static void kick_requests(struct ceph_mds_client *mdsc, int mds)
  1510. {
  1511. struct ceph_mds_request *req;
  1512. struct rb_node *p;
  1513. dout("kick_requests mds%d\n", mds);
  1514. for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
  1515. req = rb_entry(p, struct ceph_mds_request, r_node);
  1516. if (req->r_got_unsafe)
  1517. continue;
  1518. if (req->r_session &&
  1519. req->r_session->s_mds == mds) {
  1520. dout(" kicking tid %llu\n", req->r_tid);
  1521. put_request_session(req);
  1522. __do_request(mdsc, req);
  1523. }
  1524. }
  1525. }
  1526. void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
  1527. struct ceph_mds_request *req)
  1528. {
  1529. dout("submit_request on %p\n", req);
  1530. mutex_lock(&mdsc->mutex);
  1531. __register_request(mdsc, req, NULL);
  1532. __do_request(mdsc, req);
  1533. mutex_unlock(&mdsc->mutex);
  1534. }
  1535. /*
  1536. * Synchrously perform an mds request. Take care of all of the
  1537. * session setup, forwarding, retry details.
  1538. */
  1539. int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
  1540. struct inode *dir,
  1541. struct ceph_mds_request *req)
  1542. {
  1543. int err;
  1544. dout("do_request on %p\n", req);
  1545. /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
  1546. if (req->r_inode)
  1547. ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
  1548. if (req->r_locked_dir)
  1549. ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
  1550. if (req->r_old_dentry)
  1551. ceph_get_cap_refs(
  1552. ceph_inode(req->r_old_dentry->d_parent->d_inode),
  1553. CEPH_CAP_PIN);
  1554. /* issue */
  1555. mutex_lock(&mdsc->mutex);
  1556. __register_request(mdsc, req, dir);
  1557. __do_request(mdsc, req);
  1558. if (req->r_err) {
  1559. err = req->r_err;
  1560. __unregister_request(mdsc, req);
  1561. dout("do_request early error %d\n", err);
  1562. goto out;
  1563. }
  1564. /* wait */
  1565. mutex_unlock(&mdsc->mutex);
  1566. dout("do_request waiting\n");
  1567. if (req->r_timeout) {
  1568. err = (long)wait_for_completion_killable_timeout(
  1569. &req->r_completion, req->r_timeout);
  1570. if (err == 0)
  1571. err = -EIO;
  1572. } else {
  1573. err = wait_for_completion_killable(&req->r_completion);
  1574. }
  1575. dout("do_request waited, got %d\n", err);
  1576. mutex_lock(&mdsc->mutex);
  1577. /* only abort if we didn't race with a real reply */
  1578. if (req->r_got_result) {
  1579. err = le32_to_cpu(req->r_reply_info.head->result);
  1580. } else if (err < 0) {
  1581. dout("aborted request %lld with %d\n", req->r_tid, err);
  1582. /*
  1583. * ensure we aren't running concurrently with
  1584. * ceph_fill_trace or ceph_readdir_prepopulate, which
  1585. * rely on locks (dir mutex) held by our caller.
  1586. */
  1587. mutex_lock(&req->r_fill_mutex);
  1588. req->r_err = err;
  1589. req->r_aborted = true;
  1590. mutex_unlock(&req->r_fill_mutex);
  1591. if (req->r_locked_dir &&
  1592. (req->r_op & CEPH_MDS_OP_WRITE))
  1593. ceph_invalidate_dir_request(req);
  1594. } else {
  1595. err = req->r_err;
  1596. }
  1597. out:
  1598. mutex_unlock(&mdsc->mutex);
  1599. dout("do_request %p done, result %d\n", req, err);
  1600. return err;
  1601. }
  1602. /*
  1603. * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
  1604. * namespace request.
  1605. */
  1606. void ceph_invalidate_dir_request(struct ceph_mds_request *req)
  1607. {
  1608. struct inode *inode = req->r_locked_dir;
  1609. struct ceph_inode_info *ci = ceph_inode(inode);
  1610. dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
  1611. spin_lock(&inode->i_lock);
  1612. ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
  1613. ci->i_release_count++;
  1614. spin_unlock(&inode->i_lock);
  1615. if (req->r_dentry)
  1616. ceph_invalidate_dentry_lease(req->r_dentry);
  1617. if (req->r_old_dentry)
  1618. ceph_invalidate_dentry_lease(req->r_old_dentry);
  1619. }
  1620. /*
  1621. * Handle mds reply.
  1622. *
  1623. * We take the session mutex and parse and process the reply immediately.
  1624. * This preserves the logical ordering of replies, capabilities, etc., sent
  1625. * by the MDS as they are applied to our local cache.
  1626. */
  1627. static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
  1628. {
  1629. struct ceph_mds_client *mdsc = session->s_mdsc;
  1630. struct ceph_mds_request *req;
  1631. struct ceph_mds_reply_head *head = msg->front.iov_base;
  1632. struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
  1633. u64 tid;
  1634. int err, result;
  1635. int mds = session->s_mds;
  1636. if (msg->front.iov_len < sizeof(*head)) {
  1637. pr_err("mdsc_handle_reply got corrupt (short) reply\n");
  1638. ceph_msg_dump(msg);
  1639. return;
  1640. }
  1641. /* get request, session */
  1642. tid = le64_to_cpu(msg->hdr.tid);
  1643. mutex_lock(&mdsc->mutex);
  1644. req = __lookup_request(mdsc, tid);
  1645. if (!req) {
  1646. dout("handle_reply on unknown tid %llu\n", tid);
  1647. mutex_unlock(&mdsc->mutex);
  1648. return;
  1649. }
  1650. dout("handle_reply %p\n", req);
  1651. /* correct session? */
  1652. if (req->r_session != session) {
  1653. pr_err("mdsc_handle_reply got %llu on session mds%d"
  1654. " not mds%d\n", tid, session->s_mds,
  1655. req->r_session ? req->r_session->s_mds : -1);
  1656. mutex_unlock(&mdsc->mutex);
  1657. goto out;
  1658. }
  1659. /* dup? */
  1660. if ((req->r_got_unsafe && !head->safe) ||
  1661. (req->r_got_safe && head->safe)) {
  1662. pr_warning("got a dup %s reply on %llu from mds%d\n",
  1663. head->safe ? "safe" : "unsafe", tid, mds);
  1664. mutex_unlock(&mdsc->mutex);
  1665. goto out;
  1666. }
  1667. if (req->r_got_safe && !head->safe) {
  1668. pr_warning("got unsafe after safe on %llu from mds%d\n",
  1669. tid, mds);
  1670. mutex_unlock(&mdsc->mutex);
  1671. goto out;
  1672. }
  1673. result = le32_to_cpu(head->result);
  1674. /*
  1675. * Tolerate 2 consecutive ESTALEs from the same mds.
  1676. * FIXME: we should be looking at the cap migrate_seq.
  1677. */
  1678. if (result == -ESTALE) {
  1679. req->r_direct_mode = USE_AUTH_MDS;
  1680. req->r_num_stale++;
  1681. if (req->r_num_stale <= 2) {
  1682. __do_request(mdsc, req);
  1683. mutex_unlock(&mdsc->mutex);
  1684. goto out;
  1685. }
  1686. } else {
  1687. req->r_num_stale = 0;
  1688. }
  1689. if (head->safe) {
  1690. req->r_got_safe = true;
  1691. __unregister_request(mdsc, req);
  1692. complete(&req->r_safe_completion);
  1693. if (req->r_got_unsafe) {
  1694. /*
  1695. * We already handled the unsafe response, now do the
  1696. * cleanup. No need to examine the response; the MDS
  1697. * doesn't include any result info in the safe
  1698. * response. And even if it did, there is nothing
  1699. * useful we could do with a revised return value.
  1700. */
  1701. dout("got safe reply %llu, mds%d\n", tid, mds);
  1702. list_del_init(&req->r_unsafe_item);
  1703. /* last unsafe request during umount? */
  1704. if (mdsc->stopping && !__get_oldest_req(mdsc))
  1705. complete(&mdsc->safe_umount_waiters);
  1706. mutex_unlock(&mdsc->mutex);
  1707. goto out;
  1708. }
  1709. } else {
  1710. req->r_got_unsafe = true;
  1711. list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
  1712. }
  1713. dout("handle_reply tid %lld result %d\n", tid, result);
  1714. rinfo = &req->r_reply_info;
  1715. err = parse_reply_info(msg, rinfo);
  1716. mutex_unlock(&mdsc->mutex);
  1717. mutex_lock(&session->s_mutex);
  1718. if (err < 0) {
  1719. pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
  1720. ceph_msg_dump(msg);
  1721. goto out_err;
  1722. }
  1723. /* snap trace */
  1724. if (rinfo->snapblob_len) {
  1725. down_write(&mdsc->snap_rwsem);
  1726. ceph_update_snap_trace(mdsc, rinfo->snapblob,
  1727. rinfo->snapblob + rinfo->snapblob_len,
  1728. le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
  1729. downgrade_write(&mdsc->snap_rwsem);
  1730. } else {
  1731. down_read(&mdsc->snap_rwsem);
  1732. }
  1733. /* insert trace into our cache */
  1734. mutex_lock(&req->r_fill_mutex);
  1735. err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
  1736. if (err == 0) {
  1737. if (result == 0 && rinfo->dir_nr)
  1738. ceph_readdir_prepopulate(req, req->r_session);
  1739. ceph_unreserve_caps(&req->r_caps_reservation);
  1740. }
  1741. mutex_unlock(&req->r_fill_mutex);
  1742. up_read(&mdsc->snap_rwsem);
  1743. out_err:
  1744. mutex_lock(&mdsc->mutex);
  1745. if (!req->r_aborted) {
  1746. if (err) {
  1747. req->r_err = err;
  1748. } else {
  1749. req->r_reply = msg;
  1750. ceph_msg_get(msg);
  1751. req->r_got_result = true;
  1752. }
  1753. } else {
  1754. dout("reply arrived after request %lld was aborted\n", tid);
  1755. }
  1756. mutex_unlock(&mdsc->mutex);
  1757. add_cap_releases(mdsc, req->r_session, -1);
  1758. mutex_unlock(&session->s_mutex);
  1759. /* kick calling process */
  1760. complete_request(mdsc, req);
  1761. out:
  1762. ceph_mdsc_put_request(req);
  1763. return;
  1764. }
  1765. /*
  1766. * handle mds notification that our request has been forwarded.
  1767. */
  1768. static void handle_forward(struct ceph_mds_client *mdsc,
  1769. struct ceph_mds_session *session,
  1770. struct ceph_msg *msg)
  1771. {
  1772. struct ceph_mds_request *req;
  1773. u64 tid = le64_to_cpu(msg->hdr.tid);
  1774. u32 next_mds;
  1775. u32 fwd_seq;
  1776. int err = -EINVAL;
  1777. void *p = msg->front.iov_base;
  1778. void *end = p + msg->front.iov_len;
  1779. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1780. next_mds = ceph_decode_32(&p);
  1781. fwd_seq = ceph_decode_32(&p);
  1782. mutex_lock(&mdsc->mutex);
  1783. req = __lookup_request(mdsc, tid);
  1784. if (!req) {
  1785. dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
  1786. goto out; /* dup reply? */
  1787. }
  1788. if (req->r_aborted) {
  1789. dout("forward tid %llu aborted, unregistering\n", tid);
  1790. __unregister_request(mdsc, req);
  1791. } else if (fwd_seq <= req->r_num_fwd) {
  1792. dout("forward tid %llu to mds%d - old seq %d <= %d\n",
  1793. tid, next_mds, req->r_num_fwd, fwd_seq);
  1794. } else {
  1795. /* resend. forward race not possible; mds would drop */
  1796. dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
  1797. BUG_ON(req->r_err);
  1798. BUG_ON(req->r_got_result);
  1799. req->r_num_fwd = fwd_seq;
  1800. req->r_resend_mds = next_mds;
  1801. put_request_session(req);
  1802. __do_request(mdsc, req);
  1803. }
  1804. ceph_mdsc_put_request(req);
  1805. out:
  1806. mutex_unlock(&mdsc->mutex);
  1807. return;
  1808. bad:
  1809. pr_err("mdsc_handle_forward decode error err=%d\n", err);
  1810. }
  1811. /*
  1812. * handle a mds session control message
  1813. */
  1814. static void handle_session(struct ceph_mds_session *session,
  1815. struct ceph_msg *msg)
  1816. {
  1817. struct ceph_mds_client *mdsc = session->s_mdsc;
  1818. u32 op;
  1819. u64 seq;
  1820. int mds = session->s_mds;
  1821. struct ceph_mds_session_head *h = msg->front.iov_base;
  1822. int wake = 0;
  1823. /* decode */
  1824. if (msg->front.iov_len != sizeof(*h))
  1825. goto bad;
  1826. op = le32_to_cpu(h->op);
  1827. seq = le64_to_cpu(h->seq);
  1828. mutex_lock(&mdsc->mutex);
  1829. if (op == CEPH_SESSION_CLOSE)
  1830. __unregister_session(mdsc, session);
  1831. /* FIXME: this ttl calculation is generous */
  1832. session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
  1833. mutex_unlock(&mdsc->mutex);
  1834. mutex_lock(&session->s_mutex);
  1835. dout("handle_session mds%d %s %p state %s seq %llu\n",
  1836. mds, ceph_session_op_name(op), session,
  1837. session_state_name(session->s_state), seq);
  1838. if (session->s_state == CEPH_MDS_SESSION_HUNG) {
  1839. session->s_state = CEPH_MDS_SESSION_OPEN;
  1840. pr_info("mds%d came back\n", session->s_mds);
  1841. }
  1842. switch (op) {
  1843. case CEPH_SESSION_OPEN:
  1844. if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
  1845. pr_info("mds%d reconnect success\n", session->s_mds);
  1846. session->s_state = CEPH_MDS_SESSION_OPEN;
  1847. renewed_caps(mdsc, session, 0);
  1848. wake = 1;
  1849. if (mdsc->stopping)
  1850. __close_session(mdsc, session);
  1851. break;
  1852. case CEPH_SESSION_RENEWCAPS:
  1853. if (session->s_renew_seq == seq)
  1854. renewed_caps(mdsc, session, 1);
  1855. break;
  1856. case CEPH_SESSION_CLOSE:
  1857. if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
  1858. pr_info("mds%d reconnect denied\n", session->s_mds);
  1859. remove_session_caps(session);
  1860. wake = 1; /* for good measure */
  1861. complete(&mdsc->session_close_waiters);
  1862. kick_requests(mdsc, mds);
  1863. break;
  1864. case CEPH_SESSION_STALE:
  1865. pr_info("mds%d caps went stale, renewing\n",
  1866. session->s_mds);
  1867. spin_lock(&session->s_cap_lock);
  1868. session->s_cap_gen++;
  1869. session->s_cap_ttl = 0;
  1870. spin_unlock(&session->s_cap_lock);
  1871. send_renew_caps(mdsc, session);
  1872. break;
  1873. case CEPH_SESSION_RECALL_STATE:
  1874. trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
  1875. break;
  1876. default:
  1877. pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
  1878. WARN_ON(1);
  1879. }
  1880. mutex_unlock(&session->s_mutex);
  1881. if (wake) {
  1882. mutex_lock(&mdsc->mutex);
  1883. __wake_requests(mdsc, &session->s_waiting);
  1884. mutex_unlock(&mdsc->mutex);
  1885. }
  1886. return;
  1887. bad:
  1888. pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
  1889. (int)msg->front.iov_len);
  1890. ceph_msg_dump(msg);
  1891. return;
  1892. }
  1893. /*
  1894. * called under session->mutex.
  1895. */
  1896. static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
  1897. struct ceph_mds_session *session)
  1898. {
  1899. struct ceph_mds_request *req, *nreq;
  1900. int err;
  1901. dout("replay_unsafe_requests mds%d\n", session->s_mds);
  1902. mutex_lock(&mdsc->mutex);
  1903. list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
  1904. err = __prepare_send_request(mdsc, req, session->s_mds);
  1905. if (!err) {
  1906. ceph_msg_get(req->r_request);
  1907. ceph_con_send(&session->s_con, req->r_request);
  1908. }
  1909. }
  1910. mutex_unlock(&mdsc->mutex);
  1911. }
  1912. /*
  1913. * Encode information about a cap for a reconnect with the MDS.
  1914. */
  1915. static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
  1916. void *arg)
  1917. {
  1918. struct ceph_mds_cap_reconnect rec;
  1919. struct ceph_inode_info *ci;
  1920. struct ceph_pagelist *pagelist = arg;
  1921. char *path;
  1922. int pathlen, err;
  1923. u64 pathbase;
  1924. struct dentry *dentry;
  1925. ci = cap->ci;
  1926. dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
  1927. inode, ceph_vinop(inode), cap, cap->cap_id,
  1928. ceph_cap_string(cap->issued));
  1929. err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
  1930. if (err)
  1931. return err;
  1932. dentry = d_find_alias(inode);
  1933. if (dentry) {
  1934. path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
  1935. if (IS_ERR(path)) {
  1936. err = PTR_ERR(path);
  1937. BUG_ON(err);
  1938. }
  1939. } else {
  1940. path = NULL;
  1941. pathlen = 0;
  1942. }
  1943. err = ceph_pagelist_encode_string(pagelist, path, pathlen);
  1944. if (err)
  1945. goto out;
  1946. spin_lock(&inode->i_lock);
  1947. cap->seq = 0; /* reset cap seq */
  1948. cap->issue_seq = 0; /* and issue_seq */
  1949. rec.cap_id = cpu_to_le64(cap->cap_id);
  1950. rec.pathbase = cpu_to_le64(pathbase);
  1951. rec.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
  1952. rec.issued = cpu_to_le32(cap->issued);
  1953. rec.size = cpu_to_le64(inode->i_size);
  1954. ceph_encode_timespec(&rec.mtime, &inode->i_mtime);
  1955. ceph_encode_timespec(&rec.atime, &inode->i_atime);
  1956. rec.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
  1957. spin_unlock(&inode->i_lock);
  1958. err = ceph_pagelist_append(pagelist, &rec, sizeof(rec));
  1959. out:
  1960. kfree(path);
  1961. dput(dentry);
  1962. return err;
  1963. }
  1964. /*
  1965. * If an MDS fails and recovers, clients need to reconnect in order to
  1966. * reestablish shared state. This includes all caps issued through
  1967. * this session _and_ the snap_realm hierarchy. Because it's not
  1968. * clear which snap realms the mds cares about, we send everything we
  1969. * know about.. that ensures we'll then get any new info the
  1970. * recovering MDS might have.
  1971. *
  1972. * This is a relatively heavyweight operation, but it's rare.
  1973. *
  1974. * called with mdsc->mutex held.
  1975. */
  1976. static void send_mds_reconnect(struct ceph_mds_client *mdsc,
  1977. struct ceph_mds_session *session)
  1978. {
  1979. struct ceph_msg *reply;
  1980. struct rb_node *p;
  1981. int mds = session->s_mds;
  1982. int err = -ENOMEM;
  1983. struct ceph_pagelist *pagelist;
  1984. pr_info("mds%d reconnect start\n", mds);
  1985. pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
  1986. if (!pagelist)
  1987. goto fail_nopagelist;
  1988. ceph_pagelist_init(pagelist);
  1989. reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
  1990. if (!reply)
  1991. goto fail_nomsg;
  1992. mutex_lock(&session->s_mutex);
  1993. session->s_state = CEPH_MDS_SESSION_RECONNECTING;
  1994. session->s_seq = 0;
  1995. ceph_con_open(&session->s_con,
  1996. ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
  1997. /* replay unsafe requests */
  1998. replay_unsafe_requests(mdsc, session);
  1999. down_read(&mdsc->snap_rwsem);
  2000. dout("session %p state %s\n", session,
  2001. session_state_name(session->s_state));
  2002. /* drop old cap expires; we're about to reestablish that state */
  2003. discard_cap_releases(mdsc, session);
  2004. /* traverse this session's caps */
  2005. err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
  2006. if (err)
  2007. goto fail;
  2008. err = iterate_session_caps(session, encode_caps_cb, pagelist);
  2009. if (err < 0)
  2010. goto fail;
  2011. /*
  2012. * snaprealms. we provide mds with the ino, seq (version), and
  2013. * parent for all of our realms. If the mds has any newer info,
  2014. * it will tell us.
  2015. */
  2016. for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
  2017. struct ceph_snap_realm *realm =
  2018. rb_entry(p, struct ceph_snap_realm, node);
  2019. struct ceph_mds_snaprealm_reconnect sr_rec;
  2020. dout(" adding snap realm %llx seq %lld parent %llx\n",
  2021. realm->ino, realm->seq, realm->parent_ino);
  2022. sr_rec.ino = cpu_to_le64(realm->ino);
  2023. sr_rec.seq = cpu_to_le64(realm->seq);
  2024. sr_rec.parent = cpu_to_le64(realm->parent_ino);
  2025. err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
  2026. if (err)
  2027. goto fail;
  2028. }
  2029. reply->pagelist = pagelist;
  2030. reply->hdr.data_len = cpu_to_le32(pagelist->length);
  2031. reply->nr_pages = calc_pages_for(0, pagelist->length);
  2032. ceph_con_send(&session->s_con, reply);
  2033. mutex_unlock(&session->s_mutex);
  2034. mutex_lock(&mdsc->mutex);
  2035. __wake_requests(mdsc, &session->s_waiting);
  2036. mutex_unlock(&mdsc->mutex);
  2037. up_read(&mdsc->snap_rwsem);
  2038. return;
  2039. fail:
  2040. ceph_msg_put(reply);
  2041. up_read(&mdsc->snap_rwsem);
  2042. mutex_unlock(&session->s_mutex);
  2043. fail_nomsg:
  2044. ceph_pagelist_release(pagelist);
  2045. kfree(pagelist);
  2046. fail_nopagelist:
  2047. pr_err("error %d preparing reconnect for mds%d\n", err, mds);
  2048. return;
  2049. }
  2050. /*
  2051. * compare old and new mdsmaps, kicking requests
  2052. * and closing out old connections as necessary
  2053. *
  2054. * called under mdsc->mutex.
  2055. */
  2056. static void check_new_map(struct ceph_mds_client *mdsc,
  2057. struct ceph_mdsmap *newmap,
  2058. struct ceph_mdsmap *oldmap)
  2059. {
  2060. int i;
  2061. int oldstate, newstate;
  2062. struct ceph_mds_session *s;
  2063. dout("check_new_map new %u old %u\n",
  2064. newmap->m_epoch, oldmap->m_epoch);
  2065. for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
  2066. if (mdsc->sessions[i] == NULL)
  2067. continue;
  2068. s = mdsc->sessions[i];
  2069. oldstate = ceph_mdsmap_get_state(oldmap, i);
  2070. newstate = ceph_mdsmap_get_state(newmap, i);
  2071. dout("check_new_map mds%d state %s -> %s (session %s)\n",
  2072. i, ceph_mds_state_name(oldstate),
  2073. ceph_mds_state_name(newstate),
  2074. session_state_name(s->s_state));
  2075. if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
  2076. ceph_mdsmap_get_addr(newmap, i),
  2077. sizeof(struct ceph_entity_addr))) {
  2078. if (s->s_state == CEPH_MDS_SESSION_OPENING) {
  2079. /* the session never opened, just close it
  2080. * out now */
  2081. __wake_requests(mdsc, &s->s_waiting);
  2082. __unregister_session(mdsc, s);
  2083. } else {
  2084. /* just close it */
  2085. mutex_unlock(&mdsc->mutex);
  2086. mutex_lock(&s->s_mutex);
  2087. mutex_lock(&mdsc->mutex);
  2088. ceph_con_close(&s->s_con);
  2089. mutex_unlock(&s->s_mutex);
  2090. s->s_state = CEPH_MDS_SESSION_RESTARTING;
  2091. }
  2092. /* kick any requests waiting on the recovering mds */
  2093. kick_requests(mdsc, i);
  2094. } else if (oldstate == newstate) {
  2095. continue; /* nothing new with this mds */
  2096. }
  2097. /*
  2098. * send reconnect?
  2099. */
  2100. if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
  2101. newstate >= CEPH_MDS_STATE_RECONNECT) {
  2102. mutex_unlock(&mdsc->mutex);
  2103. send_mds_reconnect(mdsc, s);
  2104. mutex_lock(&mdsc->mutex);
  2105. }
  2106. /*
  2107. * kick request on any mds that has gone active.
  2108. */
  2109. if (oldstate < CEPH_MDS_STATE_ACTIVE &&
  2110. newstate >= CEPH_MDS_STATE_ACTIVE) {
  2111. if (oldstate != CEPH_MDS_STATE_CREATING &&
  2112. oldstate != CEPH_MDS_STATE_STARTING)
  2113. pr_info("mds%d recovery completed\n", s->s_mds);
  2114. kick_requests(mdsc, i);
  2115. ceph_kick_flushing_caps(mdsc, s);
  2116. wake_up_session_caps(s, 1);
  2117. }
  2118. }
  2119. }
  2120. /*
  2121. * leases
  2122. */
  2123. /*
  2124. * caller must hold session s_mutex, dentry->d_lock
  2125. */
  2126. void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
  2127. {
  2128. struct ceph_dentry_info *di = ceph_dentry(dentry);
  2129. ceph_put_mds_session(di->lease_session);
  2130. di->lease_session = NULL;
  2131. }
  2132. static void handle_lease(struct ceph_mds_client *mdsc,
  2133. struct ceph_mds_session *session,
  2134. struct ceph_msg *msg)
  2135. {
  2136. struct super_block *sb = mdsc->client->sb;
  2137. struct inode *inode;
  2138. struct ceph_inode_info *ci;
  2139. struct dentry *parent, *dentry;
  2140. struct ceph_dentry_info *di;
  2141. int mds = session->s_mds;
  2142. struct ceph_mds_lease *h = msg->front.iov_base;
  2143. struct ceph_vino vino;
  2144. int mask;
  2145. struct qstr dname;
  2146. int release = 0;
  2147. dout("handle_lease from mds%d\n", mds);
  2148. /* decode */
  2149. if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
  2150. goto bad;
  2151. vino.ino = le64_to_cpu(h->ino);
  2152. vino.snap = CEPH_NOSNAP;
  2153. mask = le16_to_cpu(h->mask);
  2154. dname.name = (void *)h + sizeof(*h) + sizeof(u32);
  2155. dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
  2156. if (dname.len != get_unaligned_le32(h+1))
  2157. goto bad;
  2158. mutex_lock(&session->s_mutex);
  2159. session->s_seq++;
  2160. /* lookup inode */
  2161. inode = ceph_find_inode(sb, vino);
  2162. dout("handle_lease '%s', mask %d, ino %llx %p\n",
  2163. ceph_lease_op_name(h->action), mask, vino.ino, inode);
  2164. if (inode == NULL) {
  2165. dout("handle_lease no inode %llx\n", vino.ino);
  2166. goto release;
  2167. }
  2168. ci = ceph_inode(inode);
  2169. /* dentry */
  2170. parent = d_find_alias(inode);
  2171. if (!parent) {
  2172. dout("no parent dentry on inode %p\n", inode);
  2173. WARN_ON(1);
  2174. goto release; /* hrm... */
  2175. }
  2176. dname.hash = full_name_hash(dname.name, dname.len);
  2177. dentry = d_lookup(parent, &dname);
  2178. dput(parent);
  2179. if (!dentry)
  2180. goto release;
  2181. spin_lock(&dentry->d_lock);
  2182. di = ceph_dentry(dentry);
  2183. switch (h->action) {
  2184. case CEPH_MDS_LEASE_REVOKE:
  2185. if (di && di->lease_session == session) {
  2186. h->seq = cpu_to_le32(di->lease_seq);
  2187. __ceph_mdsc_drop_dentry_lease(dentry);
  2188. }
  2189. release = 1;
  2190. break;
  2191. case CEPH_MDS_LEASE_RENEW:
  2192. if (di && di->lease_session == session &&
  2193. di->lease_gen == session->s_cap_gen &&
  2194. di->lease_renew_from &&
  2195. di->lease_renew_after == 0) {
  2196. unsigned long duration =
  2197. le32_to_cpu(h->duration_ms) * HZ / 1000;
  2198. di->lease_seq = le32_to_cpu(h->seq);
  2199. dentry->d_time = di->lease_renew_from + duration;
  2200. di->lease_renew_after = di->lease_renew_from +
  2201. (duration >> 1);
  2202. di->lease_renew_from = 0;
  2203. }
  2204. break;
  2205. }
  2206. spin_unlock(&dentry->d_lock);
  2207. dput(dentry);
  2208. if (!release)
  2209. goto out;
  2210. release:
  2211. /* let's just reuse the same message */
  2212. h->action = CEPH_MDS_LEASE_REVOKE_ACK;
  2213. ceph_msg_get(msg);
  2214. ceph_con_send(&session->s_con, msg);
  2215. out:
  2216. iput(inode);
  2217. mutex_unlock(&session->s_mutex);
  2218. return;
  2219. bad:
  2220. pr_err("corrupt lease message\n");
  2221. ceph_msg_dump(msg);
  2222. }
  2223. void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
  2224. struct inode *inode,
  2225. struct dentry *dentry, char action,
  2226. u32 seq)
  2227. {
  2228. struct ceph_msg *msg;
  2229. struct ceph_mds_lease *lease;
  2230. int len = sizeof(*lease) + sizeof(u32);
  2231. int dnamelen = 0;
  2232. dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
  2233. inode, dentry, ceph_lease_op_name(action), session->s_mds);
  2234. dnamelen = dentry->d_name.len;
  2235. len += dnamelen;
  2236. msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
  2237. if (!msg)
  2238. return;
  2239. lease = msg->front.iov_base;
  2240. lease->action = action;
  2241. lease->mask = cpu_to_le16(1);
  2242. lease->ino = cpu_to_le64(ceph_vino(inode).ino);
  2243. lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
  2244. lease->seq = cpu_to_le32(seq);
  2245. put_unaligned_le32(dnamelen, lease + 1);
  2246. memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
  2247. /*
  2248. * if this is a preemptive lease RELEASE, no need to
  2249. * flush request stream, since the actual request will
  2250. * soon follow.
  2251. */
  2252. msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
  2253. ceph_con_send(&session->s_con, msg);
  2254. }
  2255. /*
  2256. * Preemptively release a lease we expect to invalidate anyway.
  2257. * Pass @inode always, @dentry is optional.
  2258. */
  2259. void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
  2260. struct dentry *dentry, int mask)
  2261. {
  2262. struct ceph_dentry_info *di;
  2263. struct ceph_mds_session *session;
  2264. u32 seq;
  2265. BUG_ON(inode == NULL);
  2266. BUG_ON(dentry == NULL);
  2267. BUG_ON(mask == 0);
  2268. /* is dentry lease valid? */
  2269. spin_lock(&dentry->d_lock);
  2270. di = ceph_dentry(dentry);
  2271. if (!di || !di->lease_session ||
  2272. di->lease_session->s_mds < 0 ||
  2273. di->lease_gen != di->lease_session->s_cap_gen ||
  2274. !time_before(jiffies, dentry->d_time)) {
  2275. dout("lease_release inode %p dentry %p -- "
  2276. "no lease on %d\n",
  2277. inode, dentry, mask);
  2278. spin_unlock(&dentry->d_lock);
  2279. return;
  2280. }
  2281. /* we do have a lease on this dentry; note mds and seq */
  2282. session = ceph_get_mds_session(di->lease_session);
  2283. seq = di->lease_seq;
  2284. __ceph_mdsc_drop_dentry_lease(dentry);
  2285. spin_unlock(&dentry->d_lock);
  2286. dout("lease_release inode %p dentry %p mask %d to mds%d\n",
  2287. inode, dentry, mask, session->s_mds);
  2288. ceph_mdsc_lease_send_msg(session, inode, dentry,
  2289. CEPH_MDS_LEASE_RELEASE, seq);
  2290. ceph_put_mds_session(session);
  2291. }
  2292. /*
  2293. * drop all leases (and dentry refs) in preparation for umount
  2294. */
  2295. static void drop_leases(struct ceph_mds_client *mdsc)
  2296. {
  2297. int i;
  2298. dout("drop_leases\n");
  2299. mutex_lock(&mdsc->mutex);
  2300. for (i = 0; i < mdsc->max_sessions; i++) {
  2301. struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
  2302. if (!s)
  2303. continue;
  2304. mutex_unlock(&mdsc->mutex);
  2305. mutex_lock(&s->s_mutex);
  2306. mutex_unlock(&s->s_mutex);
  2307. ceph_put_mds_session(s);
  2308. mutex_lock(&mdsc->mutex);
  2309. }
  2310. mutex_unlock(&mdsc->mutex);
  2311. }
  2312. /*
  2313. * delayed work -- periodically trim expired leases, renew caps with mds
  2314. */
  2315. static void schedule_delayed(struct ceph_mds_client *mdsc)
  2316. {
  2317. int delay = 5;
  2318. unsigned hz = round_jiffies_relative(HZ * delay);
  2319. schedule_delayed_work(&mdsc->delayed_work, hz);
  2320. }
  2321. static void delayed_work(struct work_struct *work)
  2322. {
  2323. int i;
  2324. struct ceph_mds_client *mdsc =
  2325. container_of(work, struct ceph_mds_client, delayed_work.work);
  2326. int renew_interval;
  2327. int renew_caps;
  2328. dout("mdsc delayed_work\n");
  2329. ceph_check_delayed_caps(mdsc);
  2330. mutex_lock(&mdsc->mutex);
  2331. renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
  2332. renew_caps = time_after_eq(jiffies, HZ*renew_interval +
  2333. mdsc->last_renew_caps);
  2334. if (renew_caps)
  2335. mdsc->last_renew_caps = jiffies;
  2336. for (i = 0; i < mdsc->max_sessions; i++) {
  2337. struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
  2338. if (s == NULL)
  2339. continue;
  2340. if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
  2341. dout("resending session close request for mds%d\n",
  2342. s->s_mds);
  2343. request_close_session(mdsc, s);
  2344. ceph_put_mds_session(s);
  2345. continue;
  2346. }
  2347. if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
  2348. if (s->s_state == CEPH_MDS_SESSION_OPEN) {
  2349. s->s_state = CEPH_MDS_SESSION_HUNG;
  2350. pr_info("mds%d hung\n", s->s_mds);
  2351. }
  2352. }
  2353. if (s->s_state < CEPH_MDS_SESSION_OPEN) {
  2354. /* this mds is failed or recovering, just wait */
  2355. ceph_put_mds_session(s);
  2356. continue;
  2357. }
  2358. mutex_unlock(&mdsc->mutex);
  2359. mutex_lock(&s->s_mutex);
  2360. if (renew_caps)
  2361. send_renew_caps(mdsc, s);
  2362. else
  2363. ceph_con_keepalive(&s->s_con);
  2364. add_cap_releases(mdsc, s, -1);
  2365. if (s->s_state == CEPH_MDS_SESSION_OPEN ||
  2366. s->s_state == CEPH_MDS_SESSION_HUNG)
  2367. send_cap_releases(mdsc, s);
  2368. mutex_unlock(&s->s_mutex);
  2369. ceph_put_mds_session(s);
  2370. mutex_lock(&mdsc->mutex);
  2371. }
  2372. mutex_unlock(&mdsc->mutex);
  2373. schedule_delayed(mdsc);
  2374. }
  2375. int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
  2376. {
  2377. mdsc->client = client;
  2378. mutex_init(&mdsc->mutex);
  2379. mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
  2380. if (mdsc->mdsmap == NULL)
  2381. return -ENOMEM;
  2382. init_completion(&mdsc->safe_umount_waiters);
  2383. init_completion(&mdsc->session_close_waiters);
  2384. INIT_LIST_HEAD(&mdsc->waiting_for_map);
  2385. mdsc->sessions = NULL;
  2386. mdsc->max_sessions = 0;
  2387. mdsc->stopping = 0;
  2388. init_rwsem(&mdsc->snap_rwsem);
  2389. mdsc->snap_realms = RB_ROOT;
  2390. INIT_LIST_HEAD(&mdsc->snap_empty);
  2391. spin_lock_init(&mdsc->snap_empty_lock);
  2392. mdsc->last_tid = 0;
  2393. mdsc->request_tree = RB_ROOT;
  2394. INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
  2395. mdsc->last_renew_caps = jiffies;
  2396. INIT_LIST_HEAD(&mdsc->cap_delay_list);
  2397. spin_lock_init(&mdsc->cap_delay_lock);
  2398. INIT_LIST_HEAD(&mdsc->snap_flush_list);
  2399. spin_lock_init(&mdsc->snap_flush_lock);
  2400. mdsc->cap_flush_seq = 0;
  2401. INIT_LIST_HEAD(&mdsc->cap_dirty);
  2402. mdsc->num_cap_flushing = 0;
  2403. spin_lock_init(&mdsc->cap_dirty_lock);
  2404. init_waitqueue_head(&mdsc->cap_flushing_wq);
  2405. spin_lock_init(&mdsc->dentry_lru_lock);
  2406. INIT_LIST_HEAD(&mdsc->dentry_lru);
  2407. return 0;
  2408. }
  2409. /*
  2410. * Wait for safe replies on open mds requests. If we time out, drop
  2411. * all requests from the tree to avoid dangling dentry refs.
  2412. */
  2413. static void wait_requests(struct ceph_mds_client *mdsc)
  2414. {
  2415. struct ceph_mds_request *req;
  2416. struct ceph_client *client = mdsc->client;
  2417. mutex_lock(&mdsc->mutex);
  2418. if (__get_oldest_req(mdsc)) {
  2419. mutex_unlock(&mdsc->mutex);
  2420. dout("wait_requests waiting for requests\n");
  2421. wait_for_completion_timeout(&mdsc->safe_umount_waiters,
  2422. client->mount_args->mount_timeout * HZ);
  2423. /* tear down remaining requests */
  2424. mutex_lock(&mdsc->mutex);
  2425. while ((req = __get_oldest_req(mdsc))) {
  2426. dout("wait_requests timed out on tid %llu\n",
  2427. req->r_tid);
  2428. __unregister_request(mdsc, req);
  2429. }
  2430. }
  2431. mutex_unlock(&mdsc->mutex);
  2432. dout("wait_requests done\n");
  2433. }
  2434. /*
  2435. * called before mount is ro, and before dentries are torn down.
  2436. * (hmm, does this still race with new lookups?)
  2437. */
  2438. void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
  2439. {
  2440. dout("pre_umount\n");
  2441. mdsc->stopping = 1;
  2442. drop_leases(mdsc);
  2443. ceph_flush_dirty_caps(mdsc);
  2444. wait_requests(mdsc);
  2445. }
  2446. /*
  2447. * wait for all write mds requests to flush.
  2448. */
  2449. static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
  2450. {
  2451. struct ceph_mds_request *req = NULL, *nextreq;
  2452. struct rb_node *n;
  2453. mutex_lock(&mdsc->mutex);
  2454. dout("wait_unsafe_requests want %lld\n", want_tid);
  2455. restart:
  2456. req = __get_oldest_req(mdsc);
  2457. while (req && req->r_tid <= want_tid) {
  2458. /* find next request */
  2459. n = rb_next(&req->r_node);
  2460. if (n)
  2461. nextreq = rb_entry(n, struct ceph_mds_request, r_node);
  2462. else
  2463. nextreq = NULL;
  2464. if ((req->r_op & CEPH_MDS_OP_WRITE)) {
  2465. /* write op */
  2466. ceph_mdsc_get_request(req);
  2467. if (nextreq)
  2468. ceph_mdsc_get_request(nextreq);
  2469. mutex_unlock(&mdsc->mutex);
  2470. dout("wait_unsafe_requests wait on %llu (want %llu)\n",
  2471. req->r_tid, want_tid);
  2472. wait_for_completion(&req->r_safe_completion);
  2473. mutex_lock(&mdsc->mutex);
  2474. ceph_mdsc_put_request(req);
  2475. if (!nextreq)
  2476. break; /* next dne before, so we're done! */
  2477. if (RB_EMPTY_NODE(&nextreq->r_node)) {
  2478. /* next request was removed from tree */
  2479. ceph_mdsc_put_request(nextreq);
  2480. goto restart;
  2481. }
  2482. ceph_mdsc_put_request(nextreq); /* won't go away */
  2483. }
  2484. req = nextreq;
  2485. }
  2486. mutex_unlock(&mdsc->mutex);
  2487. dout("wait_unsafe_requests done\n");
  2488. }
  2489. void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
  2490. {
  2491. u64 want_tid, want_flush;
  2492. if (mdsc->client->mount_state == CEPH_MOUNT_SHUTDOWN)
  2493. return;
  2494. dout("sync\n");
  2495. mutex_lock(&mdsc->mutex);
  2496. want_tid = mdsc->last_tid;
  2497. want_flush = mdsc->cap_flush_seq;
  2498. mutex_unlock(&mdsc->mutex);
  2499. dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
  2500. ceph_flush_dirty_caps(mdsc);
  2501. wait_unsafe_requests(mdsc, want_tid);
  2502. wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
  2503. }
  2504. /*
  2505. * called after sb is ro.
  2506. */
  2507. void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
  2508. {
  2509. struct ceph_mds_session *session;
  2510. int i;
  2511. int n;
  2512. struct ceph_client *client = mdsc->client;
  2513. unsigned long started, timeout = client->mount_args->mount_timeout * HZ;
  2514. dout("close_sessions\n");
  2515. mutex_lock(&mdsc->mutex);
  2516. /* close sessions */
  2517. started = jiffies;
  2518. while (time_before(jiffies, started + timeout)) {
  2519. dout("closing sessions\n");
  2520. n = 0;
  2521. for (i = 0; i < mdsc->max_sessions; i++) {
  2522. session = __ceph_lookup_mds_session(mdsc, i);
  2523. if (!session)
  2524. continue;
  2525. mutex_unlock(&mdsc->mutex);
  2526. mutex_lock(&session->s_mutex);
  2527. __close_session(mdsc, session);
  2528. mutex_unlock(&session->s_mutex);
  2529. ceph_put_mds_session(session);
  2530. mutex_lock(&mdsc->mutex);
  2531. n++;
  2532. }
  2533. if (n == 0)
  2534. break;
  2535. if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
  2536. break;
  2537. dout("waiting for sessions to close\n");
  2538. mutex_unlock(&mdsc->mutex);
  2539. wait_for_completion_timeout(&mdsc->session_close_waiters,
  2540. timeout);
  2541. mutex_lock(&mdsc->mutex);
  2542. }
  2543. /* tear down remaining sessions */
  2544. for (i = 0; i < mdsc->max_sessions; i++) {
  2545. if (mdsc->sessions[i]) {
  2546. session = get_session(mdsc->sessions[i]);
  2547. __unregister_session(mdsc, session);
  2548. mutex_unlock(&mdsc->mutex);
  2549. mutex_lock(&session->s_mutex);
  2550. remove_session_caps(session);
  2551. mutex_unlock(&session->s_mutex);
  2552. ceph_put_mds_session(session);
  2553. mutex_lock(&mdsc->mutex);
  2554. }
  2555. }
  2556. WARN_ON(!list_empty(&mdsc->cap_delay_list));
  2557. mutex_unlock(&mdsc->mutex);
  2558. ceph_cleanup_empty_realms(mdsc);
  2559. cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
  2560. dout("stopped\n");
  2561. }
  2562. void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
  2563. {
  2564. dout("stop\n");
  2565. cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
  2566. if (mdsc->mdsmap)
  2567. ceph_mdsmap_destroy(mdsc->mdsmap);
  2568. kfree(mdsc->sessions);
  2569. }
  2570. /*
  2571. * handle mds map update.
  2572. */
  2573. void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
  2574. {
  2575. u32 epoch;
  2576. u32 maplen;
  2577. void *p = msg->front.iov_base;
  2578. void *end = p + msg->front.iov_len;
  2579. struct ceph_mdsmap *newmap, *oldmap;
  2580. struct ceph_fsid fsid;
  2581. int err = -EINVAL;
  2582. ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
  2583. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  2584. if (ceph_check_fsid(mdsc->client, &fsid) < 0)
  2585. return;
  2586. epoch = ceph_decode_32(&p);
  2587. maplen = ceph_decode_32(&p);
  2588. dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
  2589. /* do we need it? */
  2590. ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
  2591. mutex_lock(&mdsc->mutex);
  2592. if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
  2593. dout("handle_map epoch %u <= our %u\n",
  2594. epoch, mdsc->mdsmap->m_epoch);
  2595. mutex_unlock(&mdsc->mutex);
  2596. return;
  2597. }
  2598. newmap = ceph_mdsmap_decode(&p, end);
  2599. if (IS_ERR(newmap)) {
  2600. err = PTR_ERR(newmap);
  2601. goto bad_unlock;
  2602. }
  2603. /* swap into place */
  2604. if (mdsc->mdsmap) {
  2605. oldmap = mdsc->mdsmap;
  2606. mdsc->mdsmap = newmap;
  2607. check_new_map(mdsc, newmap, oldmap);
  2608. ceph_mdsmap_destroy(oldmap);
  2609. } else {
  2610. mdsc->mdsmap = newmap; /* first mds map */
  2611. }
  2612. mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
  2613. __wake_requests(mdsc, &mdsc->waiting_for_map);
  2614. mutex_unlock(&mdsc->mutex);
  2615. schedule_delayed(mdsc);
  2616. return;
  2617. bad_unlock:
  2618. mutex_unlock(&mdsc->mutex);
  2619. bad:
  2620. pr_err("error decoding mdsmap %d\n", err);
  2621. return;
  2622. }
  2623. static struct ceph_connection *con_get(struct ceph_connection *con)
  2624. {
  2625. struct ceph_mds_session *s = con->private;
  2626. if (get_session(s)) {
  2627. dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
  2628. return con;
  2629. }
  2630. dout("mdsc con_get %p FAIL\n", s);
  2631. return NULL;
  2632. }
  2633. static void con_put(struct ceph_connection *con)
  2634. {
  2635. struct ceph_mds_session *s = con->private;
  2636. ceph_put_mds_session(s);
  2637. dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
  2638. }
  2639. /*
  2640. * if the client is unresponsive for long enough, the mds will kill
  2641. * the session entirely.
  2642. */
  2643. static void peer_reset(struct ceph_connection *con)
  2644. {
  2645. struct ceph_mds_session *s = con->private;
  2646. struct ceph_mds_client *mdsc = s->s_mdsc;
  2647. pr_warning("mds%d closed our session\n", s->s_mds);
  2648. send_mds_reconnect(mdsc, s);
  2649. }
  2650. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  2651. {
  2652. struct ceph_mds_session *s = con->private;
  2653. struct ceph_mds_client *mdsc = s->s_mdsc;
  2654. int type = le16_to_cpu(msg->hdr.type);
  2655. mutex_lock(&mdsc->mutex);
  2656. if (__verify_registered_session(mdsc, s) < 0) {
  2657. mutex_unlock(&mdsc->mutex);
  2658. goto out;
  2659. }
  2660. mutex_unlock(&mdsc->mutex);
  2661. switch (type) {
  2662. case CEPH_MSG_MDS_MAP:
  2663. ceph_mdsc_handle_map(mdsc, msg);
  2664. break;
  2665. case CEPH_MSG_CLIENT_SESSION:
  2666. handle_session(s, msg);
  2667. break;
  2668. case CEPH_MSG_CLIENT_REPLY:
  2669. handle_reply(s, msg);
  2670. break;
  2671. case CEPH_MSG_CLIENT_REQUEST_FORWARD:
  2672. handle_forward(mdsc, s, msg);
  2673. break;
  2674. case CEPH_MSG_CLIENT_CAPS:
  2675. ceph_handle_caps(s, msg);
  2676. break;
  2677. case CEPH_MSG_CLIENT_SNAP:
  2678. ceph_handle_snap(mdsc, s, msg);
  2679. break;
  2680. case CEPH_MSG_CLIENT_LEASE:
  2681. handle_lease(mdsc, s, msg);
  2682. break;
  2683. default:
  2684. pr_err("received unknown message type %d %s\n", type,
  2685. ceph_msg_type_name(type));
  2686. }
  2687. out:
  2688. ceph_msg_put(msg);
  2689. }
  2690. /*
  2691. * authentication
  2692. */
  2693. static int get_authorizer(struct ceph_connection *con,
  2694. void **buf, int *len, int *proto,
  2695. void **reply_buf, int *reply_len, int force_new)
  2696. {
  2697. struct ceph_mds_session *s = con->private;
  2698. struct ceph_mds_client *mdsc = s->s_mdsc;
  2699. struct ceph_auth_client *ac = mdsc->client->monc.auth;
  2700. int ret = 0;
  2701. if (force_new && s->s_authorizer) {
  2702. ac->ops->destroy_authorizer(ac, s->s_authorizer);
  2703. s->s_authorizer = NULL;
  2704. }
  2705. if (s->s_authorizer == NULL) {
  2706. if (ac->ops->create_authorizer) {
  2707. ret = ac->ops->create_authorizer(
  2708. ac, CEPH_ENTITY_TYPE_MDS,
  2709. &s->s_authorizer,
  2710. &s->s_authorizer_buf,
  2711. &s->s_authorizer_buf_len,
  2712. &s->s_authorizer_reply_buf,
  2713. &s->s_authorizer_reply_buf_len);
  2714. if (ret)
  2715. return ret;
  2716. }
  2717. }
  2718. *proto = ac->protocol;
  2719. *buf = s->s_authorizer_buf;
  2720. *len = s->s_authorizer_buf_len;
  2721. *reply_buf = s->s_authorizer_reply_buf;
  2722. *reply_len = s->s_authorizer_reply_buf_len;
  2723. return 0;
  2724. }
  2725. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  2726. {
  2727. struct ceph_mds_session *s = con->private;
  2728. struct ceph_mds_client *mdsc = s->s_mdsc;
  2729. struct ceph_auth_client *ac = mdsc->client->monc.auth;
  2730. return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
  2731. }
  2732. static int invalidate_authorizer(struct ceph_connection *con)
  2733. {
  2734. struct ceph_mds_session *s = con->private;
  2735. struct ceph_mds_client *mdsc = s->s_mdsc;
  2736. struct ceph_auth_client *ac = mdsc->client->monc.auth;
  2737. if (ac->ops->invalidate_authorizer)
  2738. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
  2739. return ceph_monc_validate_auth(&mdsc->client->monc);
  2740. }
  2741. static const struct ceph_connection_operations mds_con_ops = {
  2742. .get = con_get,
  2743. .put = con_put,
  2744. .dispatch = dispatch,
  2745. .get_authorizer = get_authorizer,
  2746. .verify_authorizer_reply = verify_authorizer_reply,
  2747. .invalidate_authorizer = invalidate_authorizer,
  2748. .peer_reset = peer_reset,
  2749. };
  2750. /* eof */