mds_client.c 102 KB

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