mds_client.c 88 KB

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