mds_client.c 77 KB

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