mds_client.c 87 KB

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