mds_client.c 85 KB

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