mds_client.c 74 KB

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