mds_client.c 91 KB

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