Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2 | |
| 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 Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 9 | #include <linux/ceph/mdsmap.h> |
| 10 | #include <linux/ceph/messenger.h> |
| 11 | #include <linux/ceph/decode.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 12 | |
| 13 | #include "super.h" |
| 14 | |
| 15 | |
| 16 | /* |
| 17 | * choose a random mds that is "up" (i.e. has a state > 0), or -1. |
| 18 | */ |
| 19 | int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m) |
| 20 | { |
| 21 | int n = 0; |
| 22 | int i; |
Sam Lang | a84cd29 | 2013-04-09 16:49:11 -0500 | [diff] [blame] | 23 | |
| 24 | /* special case for one mds */ |
| 25 | if (1 == m->m_max_mds && m->m_info[0].state > 0) |
| 26 | return 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 27 | |
| 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 Lang | a84cd29 | 2013-04-09 16:49:11 -0500 | [diff] [blame] | 36 | n = prandom_u32() % n; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 37 | 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 | */ |
| 51 | struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end) |
| 52 | { |
| 53 | struct ceph_mdsmap *m; |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 54 | const void *start = *p; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 55 | int i, j, n; |
| 56 | int err = -EINVAL; |
Yan, Zheng | d463a43 | 2016-03-31 15:53:01 +0800 | [diff] [blame] | 57 | u8 mdsmap_v, mdsmap_cv; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 58 | |
| 59 | m = kzalloc(sizeof(*m), GFP_NOFS); |
| 60 | if (m == NULL) |
| 61 | return ERR_PTR(-ENOMEM); |
| 62 | |
Yan, Zheng | d463a43 | 2016-03-31 15:53:01 +0800 | [diff] [blame] | 63 | ceph_decode_need(p, end, 1 + 1, bad); |
| 64 | mdsmap_v = ceph_decode_8(p); |
| 65 | mdsmap_cv = ceph_decode_8(p); |
| 66 | if (mdsmap_v >= 4) { |
| 67 | u32 mdsmap_len; |
| 68 | ceph_decode_32_safe(p, end, mdsmap_len, bad); |
| 69 | if (end < *p + mdsmap_len) |
| 70 | goto bad; |
| 71 | end = *p + mdsmap_len; |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 72 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 73 | |
| 74 | ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 75 | m->m_epoch = ceph_decode_32(p); |
| 76 | m->m_client_epoch = ceph_decode_32(p); |
| 77 | m->m_last_failure = ceph_decode_32(p); |
| 78 | m->m_root = ceph_decode_32(p); |
| 79 | m->m_session_timeout = ceph_decode_32(p); |
| 80 | m->m_session_autoclose = ceph_decode_32(p); |
| 81 | m->m_max_file_size = ceph_decode_64(p); |
| 82 | m->m_max_mds = ceph_decode_32(p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 83 | |
| 84 | m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS); |
| 85 | if (m->m_info == NULL) |
| 86 | goto badmem; |
| 87 | |
| 88 | /* pick out active nodes from mds_info (state > 0) */ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 89 | n = ceph_decode_32(p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 90 | for (i = 0; i < n; i++) { |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 91 | u64 global_id; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 92 | u32 namelen; |
| 93 | s32 mds, inc, state; |
| 94 | u64 state_seq; |
Yan, Zheng | d463a43 | 2016-03-31 15:53:01 +0800 | [diff] [blame] | 95 | u8 info_v; |
| 96 | void *info_end = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 97 | struct ceph_entity_addr addr; |
| 98 | u32 num_export_targets; |
| 99 | void *pexport_targets = NULL; |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 100 | struct ceph_timespec laggy_since; |
Dan Carpenter | 6af8652 | 2013-05-29 06:46:56 -0500 | [diff] [blame] | 101 | struct ceph_mds_info *info; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 102 | |
Yan, Zheng | d463a43 | 2016-03-31 15:53:01 +0800 | [diff] [blame] | 103 | ceph_decode_need(p, end, sizeof(u64) + 1, bad); |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 104 | global_id = ceph_decode_64(p); |
Yan, Zheng | d463a43 | 2016-03-31 15:53:01 +0800 | [diff] [blame] | 105 | info_v= ceph_decode_8(p); |
| 106 | if (info_v >= 4) { |
| 107 | u32 info_len; |
| 108 | u8 info_cv; |
| 109 | ceph_decode_need(p, end, 1 + sizeof(u32), bad); |
| 110 | info_cv = ceph_decode_8(p); |
| 111 | info_len = ceph_decode_32(p); |
| 112 | info_end = *p + info_len; |
| 113 | if (info_end > end) |
| 114 | goto bad; |
| 115 | } |
| 116 | |
| 117 | ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad); |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 118 | *p += sizeof(u64); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 119 | namelen = ceph_decode_32(p); /* skip mds name */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 120 | *p += namelen; |
| 121 | |
| 122 | ceph_decode_need(p, end, |
Sage Weil | e251e28 | 2009-10-07 16:38:19 -0700 | [diff] [blame] | 123 | 4*sizeof(u32) + sizeof(u64) + |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 124 | sizeof(addr) + sizeof(struct ceph_timespec), |
| 125 | bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 126 | mds = ceph_decode_32(p); |
| 127 | inc = ceph_decode_32(p); |
| 128 | state = ceph_decode_32(p); |
| 129 | state_seq = ceph_decode_64(p); |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 130 | ceph_decode_copy(p, &addr, sizeof(addr)); |
| 131 | ceph_decode_addr(&addr); |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 132 | ceph_decode_copy(p, &laggy_since, sizeof(laggy_since)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 133 | *p += sizeof(u32); |
| 134 | ceph_decode_32_safe(p, end, namelen, bad); |
Sage Weil | e251e28 | 2009-10-07 16:38:19 -0700 | [diff] [blame] | 135 | *p += namelen; |
Yan, Zheng | d463a43 | 2016-03-31 15:53:01 +0800 | [diff] [blame] | 136 | if (info_v >= 2) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 137 | ceph_decode_32_safe(p, end, num_export_targets, bad); |
| 138 | pexport_targets = *p; |
Sage Weil | e251e28 | 2009-10-07 16:38:19 -0700 | [diff] [blame] | 139 | *p += num_export_targets * sizeof(u32); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 140 | } else { |
| 141 | num_export_targets = 0; |
| 142 | } |
| 143 | |
Yan, Zheng | d463a43 | 2016-03-31 15:53:01 +0800 | [diff] [blame] | 144 | if (info_end && *p != info_end) { |
| 145 | if (*p > info_end) |
| 146 | goto bad; |
| 147 | *p = info_end; |
| 148 | } |
| 149 | |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 150 | dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 151 | i+1, n, global_id, mds, inc, |
| 152 | ceph_pr_addr(&addr.in_addr), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 153 | ceph_mds_state_name(state)); |
Dan Carpenter | 6af8652 | 2013-05-29 06:46:56 -0500 | [diff] [blame] | 154 | |
| 155 | if (mds < 0 || mds >= m->m_max_mds || state <= 0) |
| 156 | continue; |
| 157 | |
| 158 | info = &m->m_info[mds]; |
| 159 | info->global_id = global_id; |
| 160 | info->state = state; |
| 161 | info->addr = addr; |
| 162 | info->laggy = (laggy_since.tv_sec != 0 || |
| 163 | laggy_since.tv_nsec != 0); |
| 164 | info->num_export_targets = num_export_targets; |
| 165 | if (num_export_targets) { |
| 166 | info->export_targets = kcalloc(num_export_targets, |
| 167 | sizeof(u32), GFP_NOFS); |
| 168 | if (info->export_targets == NULL) |
| 169 | goto badmem; |
| 170 | for (j = 0; j < num_export_targets; j++) |
| 171 | info->export_targets[j] = |
| 172 | ceph_decode_32(&pexport_targets); |
| 173 | } else { |
| 174 | info->export_targets = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | /* pg_pools */ |
| 179 | ceph_decode_32_safe(p, end, n, bad); |
| 180 | m->m_num_data_pg_pools = n; |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 181 | m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 182 | if (!m->m_data_pg_pools) |
| 183 | goto badmem; |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 184 | ceph_decode_need(p, end, sizeof(u64)*(n+1), bad); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 185 | for (i = 0; i < n; i++) |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 186 | m->m_data_pg_pools[i] = ceph_decode_64(p); |
| 187 | m->m_cas_pg_pool = ceph_decode_64(p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 188 | |
| 189 | /* ok, we don't care about the rest. */ |
Yan, Zheng | d463a43 | 2016-03-31 15:53:01 +0800 | [diff] [blame] | 190 | *p = end; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 191 | dout("mdsmap_decode success epoch %u\n", m->m_epoch); |
| 192 | return m; |
| 193 | |
| 194 | badmem: |
| 195 | err = -ENOMEM; |
| 196 | bad: |
| 197 | pr_err("corrupt mdsmap\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 198 | print_hex_dump(KERN_DEBUG, "mdsmap: ", |
| 199 | DUMP_PREFIX_OFFSET, 16, 1, |
| 200 | start, end - start, true); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 201 | ceph_mdsmap_destroy(m); |
Emil Goode | c213b50 | 2013-05-28 16:59:00 +0200 | [diff] [blame] | 202 | return ERR_PTR(err); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void ceph_mdsmap_destroy(struct ceph_mdsmap *m) |
| 206 | { |
| 207 | int i; |
| 208 | |
| 209 | for (i = 0; i < m->m_max_mds; i++) |
| 210 | kfree(m->m_info[i].export_targets); |
| 211 | kfree(m->m_info); |
| 212 | kfree(m->m_data_pg_pools); |
| 213 | kfree(m); |
| 214 | } |