mds_client.c 96 KB

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