mds_client.c 105 KB

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