mds_client.c 87 KB

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