mds_client.c 98 KB

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