mds_client.c 85 KB

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