blob: 261531e55e9d0f9af3707f937d6efe7cfe612cf6 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07002
3#include <linux/bug.h>
4#include <linux/err.h>
5#include <linux/random.h>
6#include <linux/slab.h>
7#include <linux/types.h>
8
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07009#include <linux/ceph/mdsmap.h>
10#include <linux/ceph/messenger.h>
11#include <linux/ceph/decode.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070012
13#include "super.h"
14
15
16/*
17 * choose a random mds that is "up" (i.e. has a state > 0), or -1.
18 */
19int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
20{
21 int n = 0;
22 int i;
Sam Langa84cd292013-04-09 16:49:11 -050023
24 /* special case for one mds */
25 if (1 == m->m_max_mds && m->m_info[0].state > 0)
26 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -070027
28 /* count */
29 for (i = 0; i < m->m_max_mds; i++)
30 if (m->m_info[i].state > 0)
31 n++;
32 if (n == 0)
33 return -1;
34
35 /* pick */
Sam Langa84cd292013-04-09 16:49:11 -050036 n = prandom_u32() % n;
Sage Weil2f2dc052009-10-06 11:31:09 -070037 i = 0;
38 for (i = 0; n > 0; i++, n--)
39 while (m->m_info[i].state <= 0)
40 i++;
41
42 return i;
43}
44
45/*
46 * Decode an MDS map
47 *
48 * Ignore any fields we don't care about (there are quite a few of
49 * them).
50 */
51struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end)
52{
53 struct ceph_mdsmap *m;
Sage Weil9ec7cab2009-12-14 15:13:47 -080054 const void *start = *p;
Sage Weil2f2dc052009-10-06 11:31:09 -070055 int i, j, n;
56 int err = -EINVAL;
57 u16 version;
58
59 m = kzalloc(sizeof(*m), GFP_NOFS);
60 if (m == NULL)
61 return ERR_PTR(-ENOMEM);
62
63 ceph_decode_16_safe(p, end, version, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -080064 if (version > 3) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -070065 pr_warn("got mdsmap version %d > 3, failing", version);
Sage Weil4f6a7e52013-02-23 10:41:09 -080066 goto bad;
67 }
Sage Weil2f2dc052009-10-06 11:31:09 -070068
69 ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
Sage Weilc89136e2009-10-14 09:59:09 -070070 m->m_epoch = ceph_decode_32(p);
71 m->m_client_epoch = ceph_decode_32(p);
72 m->m_last_failure = ceph_decode_32(p);
73 m->m_root = ceph_decode_32(p);
74 m->m_session_timeout = ceph_decode_32(p);
75 m->m_session_autoclose = ceph_decode_32(p);
76 m->m_max_file_size = ceph_decode_64(p);
77 m->m_max_mds = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -070078
79 m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS);
80 if (m->m_info == NULL)
81 goto badmem;
82
83 /* pick out active nodes from mds_info (state > 0) */
Sage Weilc89136e2009-10-14 09:59:09 -070084 n = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -070085 for (i = 0; i < n; i++) {
Sage Weil94045e12009-11-19 15:31:50 -080086 u64 global_id;
Sage Weil2f2dc052009-10-06 11:31:09 -070087 u32 namelen;
88 s32 mds, inc, state;
89 u64 state_seq;
90 u8 infoversion;
91 struct ceph_entity_addr addr;
92 u32 num_export_targets;
93 void *pexport_targets = NULL;
Sage Weil0deb01c2010-06-17 14:19:01 -070094 struct ceph_timespec laggy_since;
Dan Carpenter6af86522013-05-29 06:46:56 -050095 struct ceph_mds_info *info;
Sage Weil2f2dc052009-10-06 11:31:09 -070096
Sage Weil94045e12009-11-19 15:31:50 -080097 ceph_decode_need(p, end, sizeof(u64)*2 + 1 + sizeof(u32), bad);
98 global_id = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -070099 infoversion = ceph_decode_8(p);
Sage Weil94045e12009-11-19 15:31:50 -0800100 *p += sizeof(u64);
Sage Weilc89136e2009-10-14 09:59:09 -0700101 namelen = ceph_decode_32(p); /* skip mds name */
Sage Weil2f2dc052009-10-06 11:31:09 -0700102 *p += namelen;
103
104 ceph_decode_need(p, end,
Sage Weile251e282009-10-07 16:38:19 -0700105 4*sizeof(u32) + sizeof(u64) +
Sage Weil2f2dc052009-10-06 11:31:09 -0700106 sizeof(addr) + sizeof(struct ceph_timespec),
107 bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700108 mds = ceph_decode_32(p);
109 inc = ceph_decode_32(p);
110 state = ceph_decode_32(p);
111 state_seq = ceph_decode_64(p);
Sage Weil94045e12009-11-19 15:31:50 -0800112 ceph_decode_copy(p, &addr, sizeof(addr));
113 ceph_decode_addr(&addr);
Sage Weil0deb01c2010-06-17 14:19:01 -0700114 ceph_decode_copy(p, &laggy_since, sizeof(laggy_since));
Sage Weil2f2dc052009-10-06 11:31:09 -0700115 *p += sizeof(u32);
116 ceph_decode_32_safe(p, end, namelen, bad);
Sage Weile251e282009-10-07 16:38:19 -0700117 *p += namelen;
Sage Weil2f2dc052009-10-06 11:31:09 -0700118 if (infoversion >= 2) {
119 ceph_decode_32_safe(p, end, num_export_targets, bad);
120 pexport_targets = *p;
Sage Weile251e282009-10-07 16:38:19 -0700121 *p += num_export_targets * sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -0700122 } else {
123 num_export_targets = 0;
124 }
125
Sage Weil94045e12009-11-19 15:31:50 -0800126 dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700127 i+1, n, global_id, mds, inc,
128 ceph_pr_addr(&addr.in_addr),
Sage Weil2f2dc052009-10-06 11:31:09 -0700129 ceph_mds_state_name(state));
Dan Carpenter6af86522013-05-29 06:46:56 -0500130
131 if (mds < 0 || mds >= m->m_max_mds || state <= 0)
132 continue;
133
134 info = &m->m_info[mds];
135 info->global_id = global_id;
136 info->state = state;
137 info->addr = addr;
138 info->laggy = (laggy_since.tv_sec != 0 ||
139 laggy_since.tv_nsec != 0);
140 info->num_export_targets = num_export_targets;
141 if (num_export_targets) {
142 info->export_targets = kcalloc(num_export_targets,
143 sizeof(u32), GFP_NOFS);
144 if (info->export_targets == NULL)
145 goto badmem;
146 for (j = 0; j < num_export_targets; j++)
147 info->export_targets[j] =
148 ceph_decode_32(&pexport_targets);
149 } else {
150 info->export_targets = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700151 }
152 }
153
154 /* pg_pools */
155 ceph_decode_32_safe(p, end, n, bad);
156 m->m_num_data_pg_pools = n;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800157 m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -0700158 if (!m->m_data_pg_pools)
159 goto badmem;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800160 ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
Sage Weil2f2dc052009-10-06 11:31:09 -0700161 for (i = 0; i < n; i++)
Sage Weil4f6a7e52013-02-23 10:41:09 -0800162 m->m_data_pg_pools[i] = ceph_decode_64(p);
163 m->m_cas_pg_pool = ceph_decode_64(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700164
165 /* ok, we don't care about the rest. */
166 dout("mdsmap_decode success epoch %u\n", m->m_epoch);
167 return m;
168
169badmem:
170 err = -ENOMEM;
171bad:
172 pr_err("corrupt mdsmap\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800173 print_hex_dump(KERN_DEBUG, "mdsmap: ",
174 DUMP_PREFIX_OFFSET, 16, 1,
175 start, end - start, true);
Sage Weil2f2dc052009-10-06 11:31:09 -0700176 ceph_mdsmap_destroy(m);
Emil Goodec213b502013-05-28 16:59:00 +0200177 return ERR_PTR(err);
Sage Weil2f2dc052009-10-06 11:31:09 -0700178}
179
180void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
181{
182 int i;
183
184 for (i = 0; i < m->m_max_mds; i++)
185 kfree(m->m_info[i].export_targets);
186 kfree(m->m_info);
187 kfree(m->m_data_pg_pools);
188 kfree(m);
189}