mds_client.c 99 KB

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