mds_client.c 86 KB

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