mds_client.c 86 KB

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