mds_client.c 74 KB

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