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; |
| 23 | char r; |
| 24 | |
| 25 | /* count */ |
| 26 | for (i = 0; i < m->m_max_mds; i++) |
| 27 | if (m->m_info[i].state > 0) |
| 28 | n++; |
| 29 | if (n == 0) |
| 30 | return -1; |
| 31 | |
| 32 | /* pick */ |
| 33 | get_random_bytes(&r, 1); |
| 34 | n = r % n; |
| 35 | i = 0; |
| 36 | for (i = 0; n > 0; i++, n--) |
| 37 | while (m->m_info[i].state <= 0) |
| 38 | i++; |
| 39 | |
| 40 | return i; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Decode an MDS map |
| 45 | * |
| 46 | * Ignore any fields we don't care about (there are quite a few of |
| 47 | * them). |
| 48 | */ |
| 49 | struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end) |
| 50 | { |
| 51 | struct ceph_mdsmap *m; |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 52 | const void *start = *p; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 53 | int i, j, n; |
| 54 | int err = -EINVAL; |
| 55 | u16 version; |
| 56 | |
| 57 | m = kzalloc(sizeof(*m), GFP_NOFS); |
| 58 | if (m == NULL) |
| 59 | return ERR_PTR(-ENOMEM); |
| 60 | |
| 61 | ceph_decode_16_safe(p, end, version, bad); |
| 62 | |
| 63 | ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 64 | m->m_epoch = ceph_decode_32(p); |
| 65 | m->m_client_epoch = ceph_decode_32(p); |
| 66 | m->m_last_failure = ceph_decode_32(p); |
| 67 | m->m_root = ceph_decode_32(p); |
| 68 | m->m_session_timeout = ceph_decode_32(p); |
| 69 | m->m_session_autoclose = ceph_decode_32(p); |
| 70 | m->m_max_file_size = ceph_decode_64(p); |
| 71 | m->m_max_mds = ceph_decode_32(p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 72 | |
| 73 | m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS); |
| 74 | if (m->m_info == NULL) |
| 75 | goto badmem; |
| 76 | |
| 77 | /* pick out active nodes from mds_info (state > 0) */ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 78 | n = ceph_decode_32(p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 79 | for (i = 0; i < n; i++) { |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 80 | u64 global_id; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 81 | u32 namelen; |
| 82 | s32 mds, inc, state; |
| 83 | u64 state_seq; |
| 84 | u8 infoversion; |
| 85 | struct ceph_entity_addr addr; |
| 86 | u32 num_export_targets; |
| 87 | void *pexport_targets = NULL; |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 88 | struct ceph_timespec laggy_since; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 89 | |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 90 | ceph_decode_need(p, end, sizeof(u64)*2 + 1 + sizeof(u32), bad); |
| 91 | global_id = ceph_decode_64(p); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 92 | infoversion = ceph_decode_8(p); |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 93 | *p += sizeof(u64); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 94 | namelen = ceph_decode_32(p); /* skip mds name */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 95 | *p += namelen; |
| 96 | |
| 97 | ceph_decode_need(p, end, |
Sage Weil | e251e28 | 2009-10-07 16:38:19 -0700 | [diff] [blame] | 98 | 4*sizeof(u32) + sizeof(u64) + |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 99 | sizeof(addr) + sizeof(struct ceph_timespec), |
| 100 | bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 101 | mds = ceph_decode_32(p); |
| 102 | inc = ceph_decode_32(p); |
| 103 | state = ceph_decode_32(p); |
| 104 | state_seq = ceph_decode_64(p); |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 105 | ceph_decode_copy(p, &addr, sizeof(addr)); |
| 106 | ceph_decode_addr(&addr); |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 107 | ceph_decode_copy(p, &laggy_since, sizeof(laggy_since)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 108 | *p += sizeof(u32); |
| 109 | ceph_decode_32_safe(p, end, namelen, bad); |
Sage Weil | e251e28 | 2009-10-07 16:38:19 -0700 | [diff] [blame] | 110 | *p += namelen; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 111 | if (infoversion >= 2) { |
| 112 | ceph_decode_32_safe(p, end, num_export_targets, bad); |
| 113 | pexport_targets = *p; |
Sage Weil | e251e28 | 2009-10-07 16:38:19 -0700 | [diff] [blame] | 114 | *p += num_export_targets * sizeof(u32); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 115 | } else { |
| 116 | num_export_targets = 0; |
| 117 | } |
| 118 | |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 119 | dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 120 | i+1, n, global_id, mds, inc, |
| 121 | ceph_pr_addr(&addr.in_addr), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 122 | ceph_mds_state_name(state)); |
| 123 | if (mds >= 0 && mds < m->m_max_mds && state > 0) { |
Sage Weil | 94045e1 | 2009-11-19 15:31:50 -0800 | [diff] [blame] | 124 | m->m_info[mds].global_id = global_id; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 125 | m->m_info[mds].state = state; |
| 126 | m->m_info[mds].addr = addr; |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 127 | m->m_info[mds].laggy = |
| 128 | (laggy_since.tv_sec != 0 || |
| 129 | laggy_since.tv_nsec != 0); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 130 | m->m_info[mds].num_export_targets = num_export_targets; |
| 131 | if (num_export_targets) { |
| 132 | m->m_info[mds].export_targets = |
| 133 | kcalloc(num_export_targets, sizeof(u32), |
| 134 | GFP_NOFS); |
| 135 | for (j = 0; j < num_export_targets; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 136 | m->m_info[mds].export_targets[j] = |
| 137 | ceph_decode_32(&pexport_targets); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 138 | } else { |
| 139 | m->m_info[mds].export_targets = NULL; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* pg_pools */ |
| 145 | ceph_decode_32_safe(p, end, n, bad); |
| 146 | m->m_num_data_pg_pools = n; |
| 147 | m->m_data_pg_pools = kcalloc(n, sizeof(u32), GFP_NOFS); |
| 148 | if (!m->m_data_pg_pools) |
| 149 | goto badmem; |
| 150 | ceph_decode_need(p, end, sizeof(u32)*(n+1), bad); |
| 151 | for (i = 0; i < n; i++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 152 | m->m_data_pg_pools[i] = ceph_decode_32(p); |
| 153 | m->m_cas_pg_pool = ceph_decode_32(p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 154 | |
| 155 | /* ok, we don't care about the rest. */ |
| 156 | dout("mdsmap_decode success epoch %u\n", m->m_epoch); |
| 157 | return m; |
| 158 | |
| 159 | badmem: |
| 160 | err = -ENOMEM; |
| 161 | bad: |
| 162 | pr_err("corrupt mdsmap\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 163 | print_hex_dump(KERN_DEBUG, "mdsmap: ", |
| 164 | DUMP_PREFIX_OFFSET, 16, 1, |
| 165 | start, end - start, true); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 166 | ceph_mdsmap_destroy(m); |
| 167 | return ERR_PTR(-EINVAL); |
| 168 | } |
| 169 | |
| 170 | void ceph_mdsmap_destroy(struct ceph_mdsmap *m) |
| 171 | { |
| 172 | int i; |
| 173 | |
| 174 | for (i = 0; i < m->m_max_mds; i++) |
| 175 | kfree(m->m_info[i].export_targets); |
| 176 | kfree(m->m_info); |
| 177 | kfree(m->m_data_pg_pools); |
| 178 | kfree(m); |
| 179 | } |