mdsmap.c 4.5 KB
Newer Older
1
#include <linux/ceph/ceph_debug.h>
S
Sage Weil 已提交
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>
S
Sage Weil 已提交
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 25 26

	/* special case for one mds */
	if (1 == m->m_max_mds && m->m_info[0].state > 0)
		return 0;
S
Sage Weil 已提交
27 28 29 30 31 32 33 34 35

	/* count */
	for (i = 0; i < m->m_max_mds; i++)
		if (m->m_info[i].state > 0)
			n++;
	if (n == 0)
		return -1;

	/* pick */
36
	n = prandom_u32() % n;
S
Sage Weil 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	i = 0;
	for (i = 0; n > 0; i++, n--)
		while (m->m_info[i].state <= 0)
			i++;

	return i;
}

/*
 * 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;
54
	const void *start = *p;
S
Sage Weil 已提交
55 56 57 58 59 60 61 62 63
	int i, j, n;
	int err = -EINVAL;
	u16 version;

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

	ceph_decode_16_safe(p, end, version, bad);
64 65 66 67
	if (version > 3) {
		pr_warning("got mdsmap version %d > 3, failing", version);
		goto bad;
	}
S
Sage Weil 已提交
68 69

	ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
70 71 72 73 74 75 76 77
	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);
S
Sage Weil 已提交
78 79 80 81 82 83

	m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS);
	if (m->m_info == NULL)
		goto badmem;

	/* pick out active nodes from mds_info (state > 0) */
84
	n = ceph_decode_32(p);
S
Sage Weil 已提交
85
	for (i = 0; i < n; i++) {
S
Sage Weil 已提交
86
		u64 global_id;
S
Sage Weil 已提交
87 88 89 90 91 92 93
		u32 namelen;
		s32 mds, inc, state;
		u64 state_seq;
		u8 infoversion;
		struct ceph_entity_addr addr;
		u32 num_export_targets;
		void *pexport_targets = NULL;
94
		struct ceph_timespec laggy_since;
S
Sage Weil 已提交
95

S
Sage Weil 已提交
96 97
		ceph_decode_need(p, end, sizeof(u64)*2 + 1 + sizeof(u32), bad);
		global_id = ceph_decode_64(p);
98
		infoversion = ceph_decode_8(p);
S
Sage Weil 已提交
99
		*p += sizeof(u64);
100
		namelen = ceph_decode_32(p);  /* skip mds name */
S
Sage Weil 已提交
101 102 103
		*p += namelen;

		ceph_decode_need(p, end,
104
				 4*sizeof(u32) + sizeof(u64) +
S
Sage Weil 已提交
105 106
				 sizeof(addr) + sizeof(struct ceph_timespec),
				 bad);
107 108 109 110
		mds = ceph_decode_32(p);
		inc = ceph_decode_32(p);
		state = ceph_decode_32(p);
		state_seq = ceph_decode_64(p);
S
Sage Weil 已提交
111 112
		ceph_decode_copy(p, &addr, sizeof(addr));
		ceph_decode_addr(&addr);
113
		ceph_decode_copy(p, &laggy_since, sizeof(laggy_since));
S
Sage Weil 已提交
114 115
		*p += sizeof(u32);
		ceph_decode_32_safe(p, end, namelen, bad);
116
		*p += namelen;
S
Sage Weil 已提交
117 118 119
		if (infoversion >= 2) {
			ceph_decode_32_safe(p, end, num_export_targets, bad);
			pexport_targets = *p;
120
			*p += num_export_targets * sizeof(u32);
S
Sage Weil 已提交
121 122 123 124
		} else {
			num_export_targets = 0;
		}

S
Sage Weil 已提交
125
		dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n",
126 127
		     i+1, n, global_id, mds, inc,
		     ceph_pr_addr(&addr.in_addr),
S
Sage Weil 已提交
128 129
		     ceph_mds_state_name(state));
		if (mds >= 0 && mds < m->m_max_mds && state > 0) {
S
Sage Weil 已提交
130
			m->m_info[mds].global_id = global_id;
S
Sage Weil 已提交
131 132
			m->m_info[mds].state = state;
			m->m_info[mds].addr = addr;
133 134 135
			m->m_info[mds].laggy =
				(laggy_since.tv_sec != 0 ||
				 laggy_since.tv_nsec != 0);
S
Sage Weil 已提交
136 137 138 139 140
			m->m_info[mds].num_export_targets = num_export_targets;
			if (num_export_targets) {
				m->m_info[mds].export_targets =
					kcalloc(num_export_targets, sizeof(u32),
						GFP_NOFS);
141 142
				if (m->m_info[mds].export_targets == NULL)
					goto badmem;
S
Sage Weil 已提交
143
				for (j = 0; j < num_export_targets; j++)
144 145
					m->m_info[mds].export_targets[j] =
					       ceph_decode_32(&pexport_targets);
S
Sage Weil 已提交
146 147 148 149 150 151 152 153 154
			} else {
				m->m_info[mds].export_targets = NULL;
			}
		}
	}

	/* pg_pools */
	ceph_decode_32_safe(p, end, n, bad);
	m->m_num_data_pg_pools = n;
155
	m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
S
Sage Weil 已提交
156 157
	if (!m->m_data_pg_pools)
		goto badmem;
158
	ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
S
Sage Weil 已提交
159
	for (i = 0; i < n; i++)
160 161
		m->m_data_pg_pools[i] = ceph_decode_64(p);
	m->m_cas_pg_pool = ceph_decode_64(p);
S
Sage Weil 已提交
162 163 164 165 166 167 168 169 170

	/* ok, we don't care about the rest. */
	dout("mdsmap_decode success epoch %u\n", m->m_epoch);
	return m;

badmem:
	err = -ENOMEM;
bad:
	pr_err("corrupt mdsmap\n");
171 172 173
	print_hex_dump(KERN_DEBUG, "mdsmap: ",
		       DUMP_PREFIX_OFFSET, 16, 1,
		       start, end - start, true);
S
Sage Weil 已提交
174
	ceph_mdsmap_destroy(m);
175
	return ERR_PTR(err);
S
Sage Weil 已提交
176 177 178 179 180 181 182 183 184 185 186 187
}

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

	for (i = 0; i < m->m_max_mds; i++)
		kfree(m->m_info[i].export_targets);
	kfree(m->m_info);
	kfree(m->m_data_pg_pools);
	kfree(m);
}