mds_client.c 102 KB

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