mdsmap.c 9.22 KB
Newer Older
1
#include <linux/ceph/ceph_debug.h>
Sage Weil's avatar
Sage Weil committed
2 3 4 5 6 7 8

#include <linux/bug.h>
#include <linux/err.h>
#include <linux/random.h>
#include <linux/slab.h>
#include <linux/types.h>

9 10 11
#include <linux/ceph/mdsmap.h>
#include <linux/ceph/messenger.h>
#include <linux/ceph/decode.h>
Sage Weil's avatar
Sage Weil committed
12 13 14 15 16 17 18 19 20 21 22

#include "super.h"


/*
 * choose a random mds that is "up" (i.e. has a state > 0), or -1.
 */
int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
{
	int n = 0;
	int i;
23 24

	/* special case for one mds */
25
	if (1 == m->m_num_mds && m->m_info[0].state > 0)
26
		return 0;
Sage Weil's avatar
Sage Weil committed
27 28

	/* count */
29
	for (i = 0; i < m->m_num_mds; i++)
Sage Weil's avatar
Sage Weil committed
30 31 32 33 34 35
		if (m->m_info[i].state > 0)
			n++;
	if (n == 0)
		return -1;

	/* pick */
36
	n = prandom_u32() % n;
Sage Weil's avatar
Sage Weil committed
37 38 39 40 41 42 43 44
	i = 0;
	for (i = 0; n > 0; i++, n--)
		while (m->m_info[i].state <= 0)
			i++;

	return i;
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#define __decode_and_drop_type(p, end, type, bad)		\
	do {							\
		if (*p + sizeof(type) > end)			\
			goto bad;				\
		*p += sizeof(type);				\
	} while (0)

#define __decode_and_drop_set(p, end, type, bad)		\
	do {							\
		u32 n;						\
		size_t need;					\
		ceph_decode_32_safe(p, end, n, bad);		\
		need = sizeof(type) * n;			\
		ceph_decode_need(p, end, need, bad);		\
		*p += need;					\
	} while (0)

#define __decode_and_drop_map(p, end, ktype, vtype, bad)	\
	do {							\
		u32 n;						\
		size_t need;					\
		ceph_decode_32_safe(p, end, n, bad);		\
		need = (sizeof(ktype) + sizeof(vtype)) * n;	\
		ceph_decode_need(p, end, need, bad);		\
		*p += need;					\
	} while (0)


static int __decode_and_drop_compat_set(void **p, void* end)
{
	int i;
	/* compat, ro_compat, incompat*/
	for (i = 0; i < 3; i++) {
		u32 n;
		ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
		/* mask */
		*p += sizeof(u64);
		/* names (map<u64, string>) */
		n = ceph_decode_32(p);
		while (n-- > 0) {
			u32 len;
			ceph_decode_need(p, end, sizeof(u64) + sizeof(u32),
					 bad);
			*p += sizeof(u64);
			len = ceph_decode_32(p);
			ceph_decode_need(p, end, len, bad);
			*p += len;
		}
	}
	return 0;
bad:
	return -1;
}

Sage Weil's avatar
Sage Weil committed
99 100 101 102 103 104 105 106 107
/*
 * Decode an MDS map
 *
 * Ignore any fields we don't care about (there are quite a few of
 * them).
 */
struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end)
{
	struct ceph_mdsmap *m;
108
	const void *start = *p;
Sage Weil's avatar
Sage Weil committed
109 110
	int i, j, n;
	int err = -EINVAL;
111
	u8 mdsmap_v, mdsmap_cv;
112
	u16 mdsmap_ev;
Sage Weil's avatar
Sage Weil committed
113 114 115 116 117

	m = kzalloc(sizeof(*m), GFP_NOFS);
	if (m == NULL)
		return ERR_PTR(-ENOMEM);

118 119 120 121 122 123 124 125 126
	ceph_decode_need(p, end, 1 + 1, bad);
	mdsmap_v = ceph_decode_8(p);
	mdsmap_cv = ceph_decode_8(p);
	if (mdsmap_v >= 4) {
	       u32 mdsmap_len;
	       ceph_decode_32_safe(p, end, mdsmap_len, bad);
	       if (end < *p + mdsmap_len)
		       goto bad;
	       end = *p + mdsmap_len;
127
	}
Sage Weil's avatar
Sage Weil committed
128 129

	ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
130 131 132 133 134 135 136 137
	m->m_epoch = ceph_decode_32(p);
	m->m_client_epoch = ceph_decode_32(p);
	m->m_last_failure = ceph_decode_32(p);
	m->m_root = ceph_decode_32(p);
	m->m_session_timeout = ceph_decode_32(p);
	m->m_session_autoclose = ceph_decode_32(p);
	m->m_max_file_size = ceph_decode_64(p);
	m->m_max_mds = ceph_decode_32(p);
138
	m->m_num_mds = m->m_max_mds;
Sage Weil's avatar
Sage Weil committed
139

140
	m->m_info = kcalloc(m->m_num_mds, sizeof(*m->m_info), GFP_NOFS);
Sage Weil's avatar
Sage Weil committed
141
	if (m->m_info == NULL)
142
		goto nomem;
Sage Weil's avatar
Sage Weil committed
143 144

	/* pick out active nodes from mds_info (state > 0) */
145
	n = ceph_decode_32(p);
Sage Weil's avatar
Sage Weil committed
146
	for (i = 0; i < n; i++) {
147
		u64 global_id;
Sage Weil's avatar
Sage Weil committed
148 149 150
		u32 namelen;
		s32 mds, inc, state;
		u64 state_seq;
151 152
		u8 info_v;
		void *info_end = NULL;
Sage Weil's avatar
Sage Weil committed
153 154 155
		struct ceph_entity_addr addr;
		u32 num_export_targets;
		void *pexport_targets = NULL;
156
		struct ceph_timespec laggy_since;
157
		struct ceph_mds_info *info;
Sage Weil's avatar
Sage Weil committed
158

159
		ceph_decode_need(p, end, sizeof(u64) + 1, bad);
160
		global_id = ceph_decode_64(p);
161 162 163 164 165 166 167 168 169 170 171 172 173
		info_v= ceph_decode_8(p);
		if (info_v >= 4) {
			u32 info_len;
			u8 info_cv;
			ceph_decode_need(p, end, 1 + sizeof(u32), bad);
			info_cv = ceph_decode_8(p);
			info_len = ceph_decode_32(p);
			info_end = *p + info_len;
			if (info_end > end)
				goto bad;
		}

		ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
174
		*p += sizeof(u64);
175
		namelen = ceph_decode_32(p);  /* skip mds name */
Sage Weil's avatar
Sage Weil committed
176 177 178
		*p += namelen;

		ceph_decode_need(p, end,
179
				 4*sizeof(u32) + sizeof(u64) +
Sage Weil's avatar
Sage Weil committed
180 181
				 sizeof(addr) + sizeof(struct ceph_timespec),
				 bad);
182 183 184 185
		mds = ceph_decode_32(p);
		inc = ceph_decode_32(p);
		state = ceph_decode_32(p);
		state_seq = ceph_decode_64(p);
186 187
		ceph_decode_copy(p, &addr, sizeof(addr));
		ceph_decode_addr(&addr);
188
		ceph_decode_copy(p, &laggy_since, sizeof(laggy_since));
Sage Weil's avatar
Sage Weil committed
189 190
		*p += sizeof(u32);
		ceph_decode_32_safe(p, end, namelen, bad);
191
		*p += namelen;
192
		if (info_v >= 2) {
Sage Weil's avatar
Sage Weil committed
193 194
			ceph_decode_32_safe(p, end, num_export_targets, bad);
			pexport_targets = *p;
195
			*p += num_export_targets * sizeof(u32);
Sage Weil's avatar
Sage Weil committed
196 197 198 199
		} else {
			num_export_targets = 0;
		}

200 201 202 203 204 205
		if (info_end && *p != info_end) {
			if (*p > info_end)
				goto bad;
			*p = info_end;
		}

206
		dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n",
207 208
		     i+1, n, global_id, mds, inc,
		     ceph_pr_addr(&addr.in_addr),
Sage Weil's avatar
Sage Weil committed
209
		     ceph_mds_state_name(state));
210

211
		if (mds < 0 || state <= 0)
212 213
			continue;

214 215 216 217 218 219 220 221 222 223 224
		if (mds >= m->m_num_mds) {
			int new_num = max(mds + 1, m->m_num_mds * 2);
			void *new_m_info = krealloc(m->m_info,
						new_num * sizeof(*m->m_info),
						GFP_NOFS | __GFP_ZERO);
			if (!new_m_info)
				goto nomem;
			m->m_info = new_m_info;
			m->m_num_mds = new_num;
		}

225 226 227 228 229 230 231 232 233 234 235
		info = &m->m_info[mds];
		info->global_id = global_id;
		info->state = state;
		info->addr = addr;
		info->laggy = (laggy_since.tv_sec != 0 ||
			       laggy_since.tv_nsec != 0);
		info->num_export_targets = num_export_targets;
		if (num_export_targets) {
			info->export_targets = kcalloc(num_export_targets,
						       sizeof(u32), GFP_NOFS);
			if (info->export_targets == NULL)
236
				goto nomem;
237 238 239 240 241
			for (j = 0; j < num_export_targets; j++)
				info->export_targets[j] =
				       ceph_decode_32(&pexport_targets);
		} else {
			info->export_targets = NULL;
Sage Weil's avatar
Sage Weil committed
242 243
		}
	}
244 245 246 247 248 249 250 251
	if (m->m_num_mds > m->m_max_mds) {
		/* find max up mds */
		for (i = m->m_num_mds; i >= m->m_max_mds; i--) {
			if (i == 0 || m->m_info[i-1].state > 0)
				break;
		}
		m->m_num_mds = i;
	}
Sage Weil's avatar
Sage Weil committed
252 253 254 255

	/* pg_pools */
	ceph_decode_32_safe(p, end, n, bad);
	m->m_num_data_pg_pools = n;
256
	m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
Sage Weil's avatar
Sage Weil committed
257
	if (!m->m_data_pg_pools)
258
		goto nomem;
259
	ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
Sage Weil's avatar
Sage Weil committed
260
	for (i = 0; i < n; i++)
261 262
		m->m_data_pg_pools[i] = ceph_decode_64(p);
	m->m_cas_pg_pool = ceph_decode_64(p);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	m->m_enabled = m->m_epoch > 1;

	mdsmap_ev = 1;
	if (mdsmap_v >= 2) {
		ceph_decode_16_safe(p, end, mdsmap_ev, bad_ext);
	}
	if (mdsmap_ev >= 3) {
		if (__decode_and_drop_compat_set(p, end) < 0)
			goto bad_ext;
	}
	/* metadata_pool */
	if (mdsmap_ev < 5) {
		__decode_and_drop_type(p, end, u32, bad_ext);
	} else {
		__decode_and_drop_type(p, end, u64, bad_ext);
	}
Sage Weil's avatar
Sage Weil committed
279

280 281 282 283 284 285 286 287 288 289 290 291 292
	/* created + modified + tableserver */
	__decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
	__decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
	__decode_and_drop_type(p, end, u32, bad_ext);

	/* in */
	{
		int num_laggy = 0;
		ceph_decode_32_safe(p, end, n, bad_ext);
		ceph_decode_need(p, end, sizeof(u32) * n, bad_ext);

		for (i = 0; i < n; i++) {
			s32 mds = ceph_decode_32(p);
293
			if (mds >= 0 && mds < m->m_num_mds) {
294 295 296 297 298
				if (m->m_info[mds].laggy)
					num_laggy++;
			}
		}
		m->m_num_laggy = num_laggy;
299 300 301 302 303 304 305 306 307 308

		if (n > m->m_num_mds) {
			void *new_m_info = krealloc(m->m_info,
						    n * sizeof(*m->m_info),
						    GFP_NOFS | __GFP_ZERO);
			if (!new_m_info)
				goto nomem;
			m->m_info = new_m_info;
		}
		m->m_num_mds = n;
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	}

	/* inc */
	__decode_and_drop_map(p, end, u32, u32, bad_ext);
	/* up */
	__decode_and_drop_map(p, end, u32, u64, bad_ext);
	/* failed */
	__decode_and_drop_set(p, end, u32, bad_ext);
	/* stopped */
	__decode_and_drop_set(p, end, u32, bad_ext);

	if (mdsmap_ev >= 4) {
		/* last_failure_osd_epoch */
		__decode_and_drop_type(p, end, u32, bad_ext);
	}
	if (mdsmap_ev >= 6) {
		/* ever_allowed_snaps */
		__decode_and_drop_type(p, end, u8, bad_ext);
		/* explicitly_allowed_snaps */
		__decode_and_drop_type(p, end, u8, bad_ext);
	}
	if (mdsmap_ev >= 7) {
		/* inline_data_enabled */
		__decode_and_drop_type(p, end, u8, bad_ext);
	}
	if (mdsmap_ev >= 8) {
		u32 name_len;
		/* enabled */
		ceph_decode_8_safe(p, end, m->m_enabled, bad_ext);
		ceph_decode_32_safe(p, end, name_len, bad_ext);
		ceph_decode_need(p, end, name_len, bad_ext);
		*p += name_len;
	}
	/* damaged */
	if (mdsmap_ev >= 9) {
		size_t need;
		ceph_decode_32_safe(p, end, n, bad_ext);
		need = sizeof(u32) * n;
		ceph_decode_need(p, end, need, bad_ext);
		*p += need;
		m->m_damaged = n > 0;
	} else {
		m->m_damaged = false;
	}
bad_ext:
354
	*p = end;
Sage Weil's avatar
Sage Weil committed
355 356
	dout("mdsmap_decode success epoch %u\n", m->m_epoch);
	return m;
357
nomem:
Sage Weil's avatar
Sage Weil committed
358
	err = -ENOMEM;
359
	goto out_err;
Sage Weil's avatar
Sage Weil committed
360 361
bad:
	pr_err("corrupt mdsmap\n");
362 363 364
	print_hex_dump(KERN_DEBUG, "mdsmap: ",
		       DUMP_PREFIX_OFFSET, 16, 1,
		       start, end - start, true);
365
out_err:
Sage Weil's avatar
Sage Weil committed
366
	ceph_mdsmap_destroy(m);
367
	return ERR_PTR(err);
Sage Weil's avatar
Sage Weil committed
368 369 370 371 372 373
}

void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
{
	int i;

374
	for (i = 0; i < m->m_num_mds; i++)
Sage Weil's avatar
Sage Weil committed
375 376 377 378 379
		kfree(m->m_info[i].export_targets);
	kfree(m->m_info);
	kfree(m->m_data_pg_pools);
	kfree(m);
}
380 381 382 383 384 385 386 387 388 389

bool ceph_mdsmap_is_cluster_available(struct ceph_mdsmap *m)
{
	int i, nr_active = 0;
	if (!m->m_enabled)
		return false;
	if (m->m_damaged)
		return false;
	if (m->m_num_laggy > 0)
		return false;
390
	for (i = 0; i < m->m_num_mds; i++) {
391 392 393 394 395
		if (m->m_info[i].state == CEPH_MDS_STATE_ACTIVE)
			nr_active++;
	}
	return nr_active > 0;
}