mds_client.c 84 KB

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