Commit a84cd29335f4ca38ca8405c1636ee3876bb292b5
Committed by
Sage Weil
1 parent
8b3e1a5698
Exists in
smarc-l5.0.0_1.0.0-ga
and in
5 other branches
ceph: Use pseudo-random numbers to choose mds
We don't need to use up entropy to choose an mds, so use prandom_u32() to get a pseudo-random number. Also, we don't need to choose a random mds if only one mds is available, so add special casing for the common case. Fixes http://tracker.ceph.com/issues/3579 Signed-off-by: Sam Lang <sam.lang@inktank.com> Reviewed-by: Greg Farnum <greg@inktank.com> Reviewed-by: Alex Elder <elder@inktank.com>
Showing 1 changed file with 5 additions and 3 deletions Inline Diff
fs/ceph/mdsmap.c
1 | #include <linux/ceph/ceph_debug.h> | 1 | #include <linux/ceph/ceph_debug.h> |
2 | 2 | ||
3 | #include <linux/bug.h> | 3 | #include <linux/bug.h> |
4 | #include <linux/err.h> | 4 | #include <linux/err.h> |
5 | #include <linux/random.h> | 5 | #include <linux/random.h> |
6 | #include <linux/slab.h> | 6 | #include <linux/slab.h> |
7 | #include <linux/types.h> | 7 | #include <linux/types.h> |
8 | 8 | ||
9 | #include <linux/ceph/mdsmap.h> | 9 | #include <linux/ceph/mdsmap.h> |
10 | #include <linux/ceph/messenger.h> | 10 | #include <linux/ceph/messenger.h> |
11 | #include <linux/ceph/decode.h> | 11 | #include <linux/ceph/decode.h> |
12 | 12 | ||
13 | #include "super.h" | 13 | #include "super.h" |
14 | 14 | ||
15 | 15 | ||
16 | /* | 16 | /* |
17 | * choose a random mds that is "up" (i.e. has a state > 0), or -1. | 17 | * choose a random mds that is "up" (i.e. has a state > 0), or -1. |
18 | */ | 18 | */ |
19 | int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m) | 19 | int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m) |
20 | { | 20 | { |
21 | int n = 0; | 21 | int n = 0; |
22 | int i; | 22 | int i; |
23 | char r; | ||
24 | 23 | ||
24 | /* special case for one mds */ | ||
25 | if (1 == m->m_max_mds && m->m_info[0].state > 0) | ||
26 | return 0; | ||
27 | |||
25 | /* count */ | 28 | /* count */ |
26 | for (i = 0; i < m->m_max_mds; i++) | 29 | for (i = 0; i < m->m_max_mds; i++) |
27 | if (m->m_info[i].state > 0) | 30 | if (m->m_info[i].state > 0) |
28 | n++; | 31 | n++; |
29 | if (n == 0) | 32 | if (n == 0) |
30 | return -1; | 33 | return -1; |
31 | 34 | ||
32 | /* pick */ | 35 | /* pick */ |
33 | get_random_bytes(&r, 1); | 36 | n = prandom_u32() % n; |
34 | n = r % n; | ||
35 | i = 0; | 37 | i = 0; |
36 | for (i = 0; n > 0; i++, n--) | 38 | for (i = 0; n > 0; i++, n--) |
37 | while (m->m_info[i].state <= 0) | 39 | while (m->m_info[i].state <= 0) |
38 | i++; | 40 | i++; |
39 | 41 | ||
40 | return i; | 42 | return i; |
41 | } | 43 | } |
42 | 44 | ||
43 | /* | 45 | /* |
44 | * Decode an MDS map | 46 | * Decode an MDS map |
45 | * | 47 | * |
46 | * Ignore any fields we don't care about (there are quite a few of | 48 | * Ignore any fields we don't care about (there are quite a few of |
47 | * them). | 49 | * them). |
48 | */ | 50 | */ |
49 | struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end) | 51 | struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end) |
50 | { | 52 | { |
51 | struct ceph_mdsmap *m; | 53 | struct ceph_mdsmap *m; |
52 | const void *start = *p; | 54 | const void *start = *p; |
53 | int i, j, n; | 55 | int i, j, n; |
54 | int err = -EINVAL; | 56 | int err = -EINVAL; |
55 | u16 version; | 57 | u16 version; |
56 | 58 | ||
57 | m = kzalloc(sizeof(*m), GFP_NOFS); | 59 | m = kzalloc(sizeof(*m), GFP_NOFS); |
58 | if (m == NULL) | 60 | if (m == NULL) |
59 | return ERR_PTR(-ENOMEM); | 61 | return ERR_PTR(-ENOMEM); |
60 | 62 | ||
61 | ceph_decode_16_safe(p, end, version, bad); | 63 | ceph_decode_16_safe(p, end, version, bad); |
62 | if (version > 3) { | 64 | if (version > 3) { |
63 | pr_warning("got mdsmap version %d > 3, failing", version); | 65 | pr_warning("got mdsmap version %d > 3, failing", version); |
64 | goto bad; | 66 | goto bad; |
65 | } | 67 | } |
66 | 68 | ||
67 | ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad); | 69 | ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad); |
68 | m->m_epoch = ceph_decode_32(p); | 70 | m->m_epoch = ceph_decode_32(p); |
69 | m->m_client_epoch = ceph_decode_32(p); | 71 | m->m_client_epoch = ceph_decode_32(p); |
70 | m->m_last_failure = ceph_decode_32(p); | 72 | m->m_last_failure = ceph_decode_32(p); |
71 | m->m_root = ceph_decode_32(p); | 73 | m->m_root = ceph_decode_32(p); |
72 | m->m_session_timeout = ceph_decode_32(p); | 74 | m->m_session_timeout = ceph_decode_32(p); |
73 | m->m_session_autoclose = ceph_decode_32(p); | 75 | m->m_session_autoclose = ceph_decode_32(p); |
74 | m->m_max_file_size = ceph_decode_64(p); | 76 | m->m_max_file_size = ceph_decode_64(p); |
75 | m->m_max_mds = ceph_decode_32(p); | 77 | m->m_max_mds = ceph_decode_32(p); |
76 | 78 | ||
77 | m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS); | 79 | m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS); |
78 | if (m->m_info == NULL) | 80 | if (m->m_info == NULL) |
79 | goto badmem; | 81 | goto badmem; |
80 | 82 | ||
81 | /* pick out active nodes from mds_info (state > 0) */ | 83 | /* pick out active nodes from mds_info (state > 0) */ |
82 | n = ceph_decode_32(p); | 84 | n = ceph_decode_32(p); |
83 | for (i = 0; i < n; i++) { | 85 | for (i = 0; i < n; i++) { |
84 | u64 global_id; | 86 | u64 global_id; |
85 | u32 namelen; | 87 | u32 namelen; |
86 | s32 mds, inc, state; | 88 | s32 mds, inc, state; |
87 | u64 state_seq; | 89 | u64 state_seq; |
88 | u8 infoversion; | 90 | u8 infoversion; |
89 | struct ceph_entity_addr addr; | 91 | struct ceph_entity_addr addr; |
90 | u32 num_export_targets; | 92 | u32 num_export_targets; |
91 | void *pexport_targets = NULL; | 93 | void *pexport_targets = NULL; |
92 | struct ceph_timespec laggy_since; | 94 | struct ceph_timespec laggy_since; |
93 | 95 | ||
94 | ceph_decode_need(p, end, sizeof(u64)*2 + 1 + sizeof(u32), bad); | 96 | ceph_decode_need(p, end, sizeof(u64)*2 + 1 + sizeof(u32), bad); |
95 | global_id = ceph_decode_64(p); | 97 | global_id = ceph_decode_64(p); |
96 | infoversion = ceph_decode_8(p); | 98 | infoversion = ceph_decode_8(p); |
97 | *p += sizeof(u64); | 99 | *p += sizeof(u64); |
98 | namelen = ceph_decode_32(p); /* skip mds name */ | 100 | namelen = ceph_decode_32(p); /* skip mds name */ |
99 | *p += namelen; | 101 | *p += namelen; |
100 | 102 | ||
101 | ceph_decode_need(p, end, | 103 | ceph_decode_need(p, end, |
102 | 4*sizeof(u32) + sizeof(u64) + | 104 | 4*sizeof(u32) + sizeof(u64) + |
103 | sizeof(addr) + sizeof(struct ceph_timespec), | 105 | sizeof(addr) + sizeof(struct ceph_timespec), |
104 | bad); | 106 | bad); |
105 | mds = ceph_decode_32(p); | 107 | mds = ceph_decode_32(p); |
106 | inc = ceph_decode_32(p); | 108 | inc = ceph_decode_32(p); |
107 | state = ceph_decode_32(p); | 109 | state = ceph_decode_32(p); |
108 | state_seq = ceph_decode_64(p); | 110 | state_seq = ceph_decode_64(p); |
109 | ceph_decode_copy(p, &addr, sizeof(addr)); | 111 | ceph_decode_copy(p, &addr, sizeof(addr)); |
110 | ceph_decode_addr(&addr); | 112 | ceph_decode_addr(&addr); |
111 | ceph_decode_copy(p, &laggy_since, sizeof(laggy_since)); | 113 | ceph_decode_copy(p, &laggy_since, sizeof(laggy_since)); |
112 | *p += sizeof(u32); | 114 | *p += sizeof(u32); |
113 | ceph_decode_32_safe(p, end, namelen, bad); | 115 | ceph_decode_32_safe(p, end, namelen, bad); |
114 | *p += namelen; | 116 | *p += namelen; |
115 | if (infoversion >= 2) { | 117 | if (infoversion >= 2) { |
116 | ceph_decode_32_safe(p, end, num_export_targets, bad); | 118 | ceph_decode_32_safe(p, end, num_export_targets, bad); |
117 | pexport_targets = *p; | 119 | pexport_targets = *p; |
118 | *p += num_export_targets * sizeof(u32); | 120 | *p += num_export_targets * sizeof(u32); |
119 | } else { | 121 | } else { |
120 | num_export_targets = 0; | 122 | num_export_targets = 0; |
121 | } | 123 | } |
122 | 124 | ||
123 | dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n", | 125 | dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n", |
124 | i+1, n, global_id, mds, inc, | 126 | i+1, n, global_id, mds, inc, |
125 | ceph_pr_addr(&addr.in_addr), | 127 | ceph_pr_addr(&addr.in_addr), |
126 | ceph_mds_state_name(state)); | 128 | ceph_mds_state_name(state)); |
127 | if (mds >= 0 && mds < m->m_max_mds && state > 0) { | 129 | if (mds >= 0 && mds < m->m_max_mds && state > 0) { |
128 | m->m_info[mds].global_id = global_id; | 130 | m->m_info[mds].global_id = global_id; |
129 | m->m_info[mds].state = state; | 131 | m->m_info[mds].state = state; |
130 | m->m_info[mds].addr = addr; | 132 | m->m_info[mds].addr = addr; |
131 | m->m_info[mds].laggy = | 133 | m->m_info[mds].laggy = |
132 | (laggy_since.tv_sec != 0 || | 134 | (laggy_since.tv_sec != 0 || |
133 | laggy_since.tv_nsec != 0); | 135 | laggy_since.tv_nsec != 0); |
134 | m->m_info[mds].num_export_targets = num_export_targets; | 136 | m->m_info[mds].num_export_targets = num_export_targets; |
135 | if (num_export_targets) { | 137 | if (num_export_targets) { |
136 | m->m_info[mds].export_targets = | 138 | m->m_info[mds].export_targets = |
137 | kcalloc(num_export_targets, sizeof(u32), | 139 | kcalloc(num_export_targets, sizeof(u32), |
138 | GFP_NOFS); | 140 | GFP_NOFS); |
139 | for (j = 0; j < num_export_targets; j++) | 141 | for (j = 0; j < num_export_targets; j++) |
140 | m->m_info[mds].export_targets[j] = | 142 | m->m_info[mds].export_targets[j] = |
141 | ceph_decode_32(&pexport_targets); | 143 | ceph_decode_32(&pexport_targets); |
142 | } else { | 144 | } else { |
143 | m->m_info[mds].export_targets = NULL; | 145 | m->m_info[mds].export_targets = NULL; |
144 | } | 146 | } |
145 | } | 147 | } |
146 | } | 148 | } |
147 | 149 | ||
148 | /* pg_pools */ | 150 | /* pg_pools */ |
149 | ceph_decode_32_safe(p, end, n, bad); | 151 | ceph_decode_32_safe(p, end, n, bad); |
150 | m->m_num_data_pg_pools = n; | 152 | m->m_num_data_pg_pools = n; |
151 | m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS); | 153 | m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS); |
152 | if (!m->m_data_pg_pools) | 154 | if (!m->m_data_pg_pools) |
153 | goto badmem; | 155 | goto badmem; |
154 | ceph_decode_need(p, end, sizeof(u64)*(n+1), bad); | 156 | ceph_decode_need(p, end, sizeof(u64)*(n+1), bad); |
155 | for (i = 0; i < n; i++) | 157 | for (i = 0; i < n; i++) |
156 | m->m_data_pg_pools[i] = ceph_decode_64(p); | 158 | m->m_data_pg_pools[i] = ceph_decode_64(p); |
157 | m->m_cas_pg_pool = ceph_decode_64(p); | 159 | m->m_cas_pg_pool = ceph_decode_64(p); |
158 | 160 | ||
159 | /* ok, we don't care about the rest. */ | 161 | /* ok, we don't care about the rest. */ |
160 | dout("mdsmap_decode success epoch %u\n", m->m_epoch); | 162 | dout("mdsmap_decode success epoch %u\n", m->m_epoch); |
161 | return m; | 163 | return m; |
162 | 164 | ||
163 | badmem: | 165 | badmem: |
164 | err = -ENOMEM; | 166 | err = -ENOMEM; |
165 | bad: | 167 | bad: |
166 | pr_err("corrupt mdsmap\n"); | 168 | pr_err("corrupt mdsmap\n"); |
167 | print_hex_dump(KERN_DEBUG, "mdsmap: ", | 169 | print_hex_dump(KERN_DEBUG, "mdsmap: ", |
168 | DUMP_PREFIX_OFFSET, 16, 1, | 170 | DUMP_PREFIX_OFFSET, 16, 1, |
169 | start, end - start, true); | 171 | start, end - start, true); |
170 | ceph_mdsmap_destroy(m); | 172 | ceph_mdsmap_destroy(m); |
171 | return ERR_PTR(-EINVAL); | 173 | return ERR_PTR(-EINVAL); |
172 | } | 174 | } |
173 | 175 | ||
174 | void ceph_mdsmap_destroy(struct ceph_mdsmap *m) | 176 | void ceph_mdsmap_destroy(struct ceph_mdsmap *m) |
175 | { | 177 | { |
176 | int i; | 178 | int i; |
177 | 179 | ||
178 | for (i = 0; i < m->m_max_mds; i++) | 180 | for (i = 0; i < m->m_max_mds; i++) |
179 | kfree(m->m_info[i].export_targets); | 181 | kfree(m->m_info[i].export_targets); |
180 | kfree(m->m_info); | 182 | kfree(m->m_info); |
181 | kfree(m->m_data_pg_pools); | 183 | kfree(m->m_data_pg_pools); |
182 | kfree(m); | 184 | kfree(m); |