Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 2 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3 | #include <linux/module.h> |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 4 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/slab.h> |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 6 | #include <linux/random.h> |
| 7 | #include <linux/sched.h> |
| 8 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 9 | #include <linux/ceph/mon_client.h> |
| 10 | #include <linux/ceph/libceph.h> |
Sage Weil | ab434b6 | 2012-01-13 22:22:03 -0800 | [diff] [blame] | 11 | #include <linux/ceph/debugfs.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 12 | #include <linux/ceph/decode.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 13 | #include <linux/ceph/auth.h> |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * Interact with Ceph monitor cluster. Handle requests for new map |
| 17 | * versions, and periodically resend as needed. Also implement |
| 18 | * statfs() and umount(). |
| 19 | * |
| 20 | * A small cluster of Ceph "monitors" are responsible for managing critical |
| 21 | * cluster configuration and state information. An odd number (e.g., 3, 5) |
| 22 | * of cmon daemons use a modified version of the Paxos part-time parliament |
| 23 | * algorithm to manage the MDS map (mds cluster membership), OSD map, and |
| 24 | * list of clients who have mounted the file system. |
| 25 | * |
| 26 | * We maintain an open, active session with a monitor at all times in order to |
| 27 | * receive timely MDSMap updates. We periodically send a keepalive byte on the |
| 28 | * TCP socket to ensure we detect a failure. If the connection does break, we |
| 29 | * randomly hunt for a new monitor. Once the connection is reestablished, we |
| 30 | * resend any outstanding requests. |
| 31 | */ |
| 32 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 33 | static const struct ceph_connection_operations mon_con_ops; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 34 | |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 35 | static int __validate_auth(struct ceph_mon_client *monc); |
| 36 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 37 | /* |
| 38 | * Decode a monmap blob (e.g., during mount). |
| 39 | */ |
| 40 | struct ceph_monmap *ceph_monmap_decode(void *p, void *end) |
| 41 | { |
| 42 | struct ceph_monmap *m = NULL; |
| 43 | int i, err = -EINVAL; |
| 44 | struct ceph_fsid fsid; |
| 45 | u32 epoch, num_mon; |
| 46 | u16 version; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 47 | u32 len; |
| 48 | |
| 49 | ceph_decode_32_safe(&p, end, len, bad); |
| 50 | ceph_decode_need(&p, end, len, bad); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 51 | |
| 52 | dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p)); |
| 53 | |
| 54 | ceph_decode_16_safe(&p, end, version, bad); |
| 55 | |
| 56 | ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad); |
| 57 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 58 | epoch = ceph_decode_32(&p); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 59 | |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 60 | num_mon = ceph_decode_32(&p); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 61 | ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad); |
| 62 | |
| 63 | if (num_mon >= CEPH_MAX_MON) |
| 64 | goto bad; |
| 65 | m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS); |
| 66 | if (m == NULL) |
| 67 | return ERR_PTR(-ENOMEM); |
| 68 | m->fsid = fsid; |
| 69 | m->epoch = epoch; |
| 70 | m->num_mon = num_mon; |
| 71 | ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0])); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 72 | for (i = 0; i < num_mon; i++) |
| 73 | ceph_decode_addr(&m->mon_inst[i].addr); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 74 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 75 | dout("monmap_decode epoch %d, num_mon %d\n", m->epoch, |
| 76 | m->num_mon); |
| 77 | for (i = 0; i < m->num_mon; i++) |
| 78 | dout("monmap_decode mon%d is %s\n", i, |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 79 | ceph_pr_addr(&m->mon_inst[i].addr.in_addr)); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 80 | return m; |
| 81 | |
| 82 | bad: |
| 83 | dout("monmap_decode failed with %d\n", err); |
| 84 | kfree(m); |
| 85 | return ERR_PTR(err); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * return true if *addr is included in the monmap. |
| 90 | */ |
| 91 | int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr) |
| 92 | { |
| 93 | int i; |
| 94 | |
| 95 | for (i = 0; i < m->num_mon; i++) |
Sage Weil | 103e2d3 | 2010-01-07 16:12:36 -0800 | [diff] [blame] | 96 | if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 97 | return 1; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /* |
Sage Weil | 5ce6e9d | 2010-02-15 16:22:28 -0800 | [diff] [blame] | 102 | * Send an auth request. |
| 103 | */ |
| 104 | static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len) |
| 105 | { |
| 106 | monc->pending_auth = 1; |
| 107 | monc->m_auth->front.iov_len = len; |
| 108 | monc->m_auth->hdr.front_len = cpu_to_le32(len); |
Alex Elder | 6740a84 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 109 | ceph_msg_revoke(monc->m_auth); |
Sage Weil | 5ce6e9d | 2010-02-15 16:22:28 -0800 | [diff] [blame] | 110 | ceph_msg_get(monc->m_auth); /* keep our ref */ |
Alex Elder | 6713093 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 111 | ceph_con_send(&monc->con, monc->m_auth); |
Sage Weil | 5ce6e9d | 2010-02-15 16:22:28 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 115 | * Close monitor session, if any. |
| 116 | */ |
| 117 | static void __close_session(struct ceph_mon_client *monc) |
| 118 | { |
Sage Weil | f6a2f5b | 2011-08-09 09:27:44 -0700 | [diff] [blame] | 119 | dout("__close_session closing mon%d\n", monc->cur_mon); |
Alex Elder | 6740a84 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 120 | ceph_msg_revoke(monc->m_auth); |
Sage Weil | 4f471e4 | 2012-07-30 18:16:40 -0700 | [diff] [blame] | 121 | ceph_msg_revoke_incoming(monc->m_auth_reply); |
| 122 | ceph_msg_revoke(monc->m_subscribe); |
| 123 | ceph_msg_revoke_incoming(monc->m_subscribe_ack); |
Alex Elder | 6713093 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 124 | ceph_con_close(&monc->con); |
Sage Weil | f6a2f5b | 2011-08-09 09:27:44 -0700 | [diff] [blame] | 125 | monc->cur_mon = -1; |
| 126 | monc->pending_auth = 0; |
| 127 | ceph_auth_reset(monc->auth); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Open a session with a (new) monitor. |
| 132 | */ |
| 133 | static int __open_session(struct ceph_mon_client *monc) |
| 134 | { |
| 135 | char r; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 136 | int ret; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 137 | |
| 138 | if (monc->cur_mon < 0) { |
| 139 | get_random_bytes(&r, 1); |
| 140 | monc->cur_mon = r % monc->monmap->num_mon; |
| 141 | dout("open_session num=%d r=%d -> mon%d\n", |
| 142 | monc->monmap->num_mon, r, monc->cur_mon); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 143 | monc->sub_renew_after = jiffies; /* i.e., expired */ |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 144 | monc->sub_renew_sent = 0; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 145 | |
Alex Elder | 20581c1 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 146 | dout("open_session mon%d opening\n", monc->cur_mon); |
Alex Elder | 6713093 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 147 | ceph_con_open(&monc->con, |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 148 | CEPH_ENTITY_TYPE_MON, monc->cur_mon, |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 149 | &monc->monmap->mon_inst[monc->cur_mon].addr); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 150 | |
Yan, Zheng | 8b9558a | 2015-09-01 17:19:38 +0800 | [diff] [blame] | 151 | /* send an initial keepalive to ensure our timestamp is |
| 152 | * valid by the time we are in an OPENED state */ |
| 153 | ceph_con_keepalive(&monc->con); |
| 154 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 155 | /* initiatiate authentication handshake */ |
| 156 | ret = ceph_auth_build_hello(monc->auth, |
| 157 | monc->m_auth->front.iov_base, |
Ilya Dryomov | 3cea4c3 | 2014-01-09 20:08:21 +0200 | [diff] [blame] | 158 | monc->m_auth->front_alloc_len); |
Sage Weil | 5ce6e9d | 2010-02-15 16:22:28 -0800 | [diff] [blame] | 159 | __send_prepared_auth_request(monc, ret); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 160 | } else { |
| 161 | dout("open_session mon%d already open\n", monc->cur_mon); |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static bool __sub_expired(struct ceph_mon_client *monc) |
| 167 | { |
| 168 | return time_after_eq(jiffies, monc->sub_renew_after); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Reschedule delayed work timer. |
| 173 | */ |
| 174 | static void __schedule_delayed(struct ceph_mon_client *monc) |
| 175 | { |
Yan, Zheng | 8b9558a | 2015-09-01 17:19:38 +0800 | [diff] [blame] | 176 | struct ceph_options *opt = monc->client->options; |
| 177 | unsigned long delay; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 178 | |
Yan, Zheng | 8b9558a | 2015-09-01 17:19:38 +0800 | [diff] [blame] | 179 | if (monc->cur_mon < 0 || __sub_expired(monc)) { |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 180 | delay = 10 * HZ; |
Yan, Zheng | 8b9558a | 2015-09-01 17:19:38 +0800 | [diff] [blame] | 181 | } else { |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 182 | delay = 20 * HZ; |
Yan, Zheng | 8b9558a | 2015-09-01 17:19:38 +0800 | [diff] [blame] | 183 | if (opt->monc_ping_timeout > 0) |
| 184 | delay = min(delay, opt->monc_ping_timeout / 3); |
| 185 | } |
| 186 | dout("__schedule_delayed after %lu\n", delay); |
| 187 | schedule_delayed_work(&monc->delayed_work, |
| 188 | round_jiffies_relative(delay)); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 191 | const char *ceph_sub_str[] = { |
| 192 | [CEPH_SUB_MDSMAP] = "mdsmap", |
| 193 | [CEPH_SUB_MONMAP] = "monmap", |
| 194 | [CEPH_SUB_OSDMAP] = "osdmap", |
| 195 | }; |
| 196 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 197 | /* |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 198 | * Send subscribe request for one or more maps, according to |
| 199 | * monc->subs. |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 200 | */ |
| 201 | static void __send_subscribe(struct ceph_mon_client *monc) |
| 202 | { |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 203 | struct ceph_msg *msg = monc->m_subscribe; |
| 204 | void *p = msg->front.iov_base; |
| 205 | void *const end = p + msg->front_alloc_len; |
| 206 | int num = 0; |
| 207 | int i; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 208 | |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 209 | dout("%s sent %lu\n", __func__, monc->sub_renew_sent); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 210 | |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 211 | BUG_ON(monc->cur_mon < 0); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 212 | |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 213 | if (!monc->sub_renew_sent) |
| 214 | monc->sub_renew_sent = jiffies | 1; /* never 0 */ |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 215 | |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 216 | msg->hdr.version = cpu_to_le16(2); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 217 | |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 218 | for (i = 0; i < ARRAY_SIZE(monc->subs); i++) { |
| 219 | if (monc->subs[i].want) |
| 220 | num++; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 221 | } |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 222 | BUG_ON(num < 1); /* monmap sub is always there */ |
| 223 | ceph_encode_32(&p, num); |
| 224 | for (i = 0; i < ARRAY_SIZE(monc->subs); i++) { |
| 225 | const char *s = ceph_sub_str[i]; |
| 226 | |
| 227 | if (!monc->subs[i].want) |
| 228 | continue; |
| 229 | |
| 230 | dout("%s %s start %llu flags 0x%x\n", __func__, s, |
| 231 | le64_to_cpu(monc->subs[i].item.start), |
| 232 | monc->subs[i].item.flags); |
| 233 | ceph_encode_string(&p, end, s, strlen(s)); |
| 234 | memcpy(p, &monc->subs[i].item, sizeof(monc->subs[i].item)); |
| 235 | p += sizeof(monc->subs[i].item); |
| 236 | } |
| 237 | |
| 238 | BUG_ON(p != (end - 35 - (ARRAY_SIZE(monc->subs) - num) * 19)); |
| 239 | msg->front.iov_len = p - msg->front.iov_base; |
| 240 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 241 | ceph_msg_revoke(msg); |
| 242 | ceph_con_send(&monc->con, ceph_msg_get(msg)); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static void handle_subscribe_ack(struct ceph_mon_client *monc, |
| 246 | struct ceph_msg *msg) |
| 247 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 248 | unsigned int seconds; |
Sage Weil | 07bd10f | 2009-10-14 17:26:40 -0700 | [diff] [blame] | 249 | struct ceph_mon_subscribe_ack *h = msg->front.iov_base; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 250 | |
Sage Weil | 07bd10f | 2009-10-14 17:26:40 -0700 | [diff] [blame] | 251 | if (msg->front.iov_len < sizeof(*h)) |
| 252 | goto bad; |
| 253 | seconds = le32_to_cpu(h->duration); |
| 254 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 255 | mutex_lock(&monc->mutex); |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 256 | if (monc->sub_renew_sent) { |
| 257 | monc->sub_renew_after = monc->sub_renew_sent + |
| 258 | (seconds >> 1) * HZ - 1; |
| 259 | dout("%s sent %lu duration %d renew after %lu\n", __func__, |
| 260 | monc->sub_renew_sent, seconds, monc->sub_renew_after); |
| 261 | monc->sub_renew_sent = 0; |
| 262 | } else { |
| 263 | dout("%s sent %lu renew after %lu, ignoring\n", __func__, |
| 264 | monc->sub_renew_sent, monc->sub_renew_after); |
| 265 | } |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 266 | mutex_unlock(&monc->mutex); |
| 267 | return; |
| 268 | bad: |
| 269 | pr_err("got corrupt subscribe-ack msg\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 270 | ceph_msg_dump(msg); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 274 | * Register interest in a map |
| 275 | * |
| 276 | * @sub: one of CEPH_SUB_* |
| 277 | * @epoch: X for "every map since X", or 0 for "just the latest" |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 278 | */ |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 279 | static bool __ceph_monc_want_map(struct ceph_mon_client *monc, int sub, |
| 280 | u32 epoch, bool continuous) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 281 | { |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 282 | __le64 start = cpu_to_le64(epoch); |
| 283 | u8 flags = !continuous ? CEPH_SUBSCRIBE_ONETIME : 0; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 284 | |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 285 | dout("%s %s epoch %u continuous %d\n", __func__, ceph_sub_str[sub], |
| 286 | epoch, continuous); |
| 287 | |
| 288 | if (monc->subs[sub].want && |
| 289 | monc->subs[sub].item.start == start && |
| 290 | monc->subs[sub].item.flags == flags) |
| 291 | return false; |
| 292 | |
| 293 | monc->subs[sub].item.start = start; |
| 294 | monc->subs[sub].item.flags = flags; |
| 295 | monc->subs[sub].want = true; |
| 296 | |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | bool ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch, |
| 301 | bool continuous) |
| 302 | { |
| 303 | bool need_request; |
| 304 | |
| 305 | mutex_lock(&monc->mutex); |
| 306 | need_request = __ceph_monc_want_map(monc, sub, epoch, continuous); |
| 307 | mutex_unlock(&monc->mutex); |
| 308 | |
| 309 | return need_request; |
| 310 | } |
| 311 | EXPORT_SYMBOL(ceph_monc_want_map); |
| 312 | |
| 313 | /* |
| 314 | * Keep track of which maps we have |
| 315 | * |
| 316 | * @sub: one of CEPH_SUB_* |
| 317 | */ |
| 318 | static void __ceph_monc_got_map(struct ceph_mon_client *monc, int sub, |
| 319 | u32 epoch) |
| 320 | { |
| 321 | dout("%s %s epoch %u\n", __func__, ceph_sub_str[sub], epoch); |
| 322 | |
| 323 | if (monc->subs[sub].want) { |
| 324 | if (monc->subs[sub].item.flags & CEPH_SUBSCRIBE_ONETIME) |
| 325 | monc->subs[sub].want = false; |
| 326 | else |
| 327 | monc->subs[sub].item.start = cpu_to_le64(epoch + 1); |
| 328 | } |
| 329 | |
| 330 | monc->subs[sub].have = epoch; |
| 331 | } |
| 332 | |
| 333 | void ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 334 | { |
| 335 | mutex_lock(&monc->mutex); |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 336 | __ceph_monc_got_map(monc, sub, epoch); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 337 | mutex_unlock(&monc->mutex); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 338 | } |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 339 | EXPORT_SYMBOL(ceph_monc_got_map); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | * Register interest in the next osdmap |
| 343 | */ |
| 344 | void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc) |
| 345 | { |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 346 | dout("%s have %u\n", __func__, monc->subs[CEPH_SUB_OSDMAP].have); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 347 | mutex_lock(&monc->mutex); |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 348 | if (__ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, |
| 349 | monc->subs[CEPH_SUB_OSDMAP].have + 1, false)) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 350 | __send_subscribe(monc); |
| 351 | mutex_unlock(&monc->mutex); |
| 352 | } |
Ilya Dryomov | 6044cde | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 353 | EXPORT_SYMBOL(ceph_monc_request_next_osdmap); |
| 354 | |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 355 | /* |
| 356 | * Wait for an osdmap with a given epoch. |
| 357 | * |
| 358 | * @epoch: epoch to wait for |
| 359 | * @timeout: in jiffies, 0 means "wait forever" |
| 360 | */ |
Ilya Dryomov | 6044cde | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 361 | int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch, |
| 362 | unsigned long timeout) |
| 363 | { |
| 364 | unsigned long started = jiffies; |
Ilya Dryomov | 216639d | 2015-05-19 12:03:33 +0300 | [diff] [blame] | 365 | long ret; |
Ilya Dryomov | 6044cde | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 366 | |
| 367 | mutex_lock(&monc->mutex); |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 368 | while (monc->subs[CEPH_SUB_OSDMAP].have < epoch) { |
Ilya Dryomov | 6044cde | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 369 | mutex_unlock(&monc->mutex); |
| 370 | |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 371 | if (timeout && time_after_eq(jiffies, started + timeout)) |
Ilya Dryomov | 6044cde | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 372 | return -ETIMEDOUT; |
| 373 | |
| 374 | ret = wait_event_interruptible_timeout(monc->client->auth_wq, |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 375 | monc->subs[CEPH_SUB_OSDMAP].have >= epoch, |
| 376 | ceph_timeout_jiffies(timeout)); |
Ilya Dryomov | 6044cde | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 377 | if (ret < 0) |
| 378 | return ret; |
| 379 | |
| 380 | mutex_lock(&monc->mutex); |
| 381 | } |
| 382 | |
| 383 | mutex_unlock(&monc->mutex); |
| 384 | return 0; |
| 385 | } |
| 386 | EXPORT_SYMBOL(ceph_monc_wait_osdmap); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 387 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 388 | /* |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 389 | * Open a session with a random monitor. Request monmap and osdmap, |
| 390 | * which are waited upon in __ceph_open_session(). |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 391 | */ |
| 392 | int ceph_monc_open_session(struct ceph_mon_client *monc) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 393 | { |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 394 | mutex_lock(&monc->mutex); |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 395 | __ceph_monc_want_map(monc, CEPH_SUB_MONMAP, 0, true); |
| 396 | __ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, 0, false); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 397 | __open_session(monc); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 398 | __schedule_delayed(monc); |
| 399 | mutex_unlock(&monc->mutex); |
| 400 | return 0; |
| 401 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 402 | EXPORT_SYMBOL(ceph_monc_open_session); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 403 | |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 404 | static void ceph_monc_handle_map(struct ceph_mon_client *monc, |
| 405 | struct ceph_msg *msg) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 406 | { |
| 407 | struct ceph_client *client = monc->client; |
| 408 | struct ceph_monmap *monmap = NULL, *old = monc->monmap; |
| 409 | void *p, *end; |
| 410 | |
| 411 | mutex_lock(&monc->mutex); |
| 412 | |
| 413 | dout("handle_monmap\n"); |
| 414 | p = msg->front.iov_base; |
| 415 | end = p + msg->front.iov_len; |
| 416 | |
| 417 | monmap = ceph_monmap_decode(p, end); |
| 418 | if (IS_ERR(monmap)) { |
| 419 | pr_err("problem decoding monmap, %d\n", |
| 420 | (int)PTR_ERR(monmap)); |
Sage Weil | d4a780c | 2009-12-11 08:55:23 -0800 | [diff] [blame] | 421 | goto out; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 422 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 423 | |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 424 | if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) { |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 425 | kfree(monmap); |
Sage Weil | d4a780c | 2009-12-11 08:55:23 -0800 | [diff] [blame] | 426 | goto out; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | client->monc.monmap = monmap; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 430 | kfree(old); |
| 431 | |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 432 | __ceph_monc_got_map(monc, CEPH_SUB_MONMAP, monc->monmap->epoch); |
Ilya Dryomov | 02ac956 | 2016-01-06 12:56:21 +0300 | [diff] [blame] | 433 | client->have_fsid = true; |
Sage Weil | d1c338a | 2012-08-19 12:29:16 -0700 | [diff] [blame] | 434 | |
Sage Weil | d4a780c | 2009-12-11 08:55:23 -0800 | [diff] [blame] | 435 | out: |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 436 | mutex_unlock(&monc->mutex); |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 437 | wake_up_all(&client->auth_wq); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 438 | } |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 439 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 440 | /* |
Ilya Dryomov | 7a6fdeb | 2014-12-22 19:14:26 +0300 | [diff] [blame] | 441 | * generic requests (currently statfs, mon_get_version) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 442 | */ |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 443 | static struct ceph_mon_generic_request *__lookup_generic_req( |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 444 | struct ceph_mon_client *monc, u64 tid) |
| 445 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 446 | struct ceph_mon_generic_request *req; |
| 447 | struct rb_node *n = monc->generic_request_tree.rb_node; |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 448 | |
| 449 | while (n) { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 450 | req = rb_entry(n, struct ceph_mon_generic_request, node); |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 451 | if (tid < req->tid) |
| 452 | n = n->rb_left; |
| 453 | else if (tid > req->tid) |
| 454 | n = n->rb_right; |
| 455 | else |
| 456 | return req; |
| 457 | } |
| 458 | return NULL; |
| 459 | } |
| 460 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 461 | static void __insert_generic_request(struct ceph_mon_client *monc, |
| 462 | struct ceph_mon_generic_request *new) |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 463 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 464 | struct rb_node **p = &monc->generic_request_tree.rb_node; |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 465 | struct rb_node *parent = NULL; |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 466 | struct ceph_mon_generic_request *req = NULL; |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 467 | |
| 468 | while (*p) { |
| 469 | parent = *p; |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 470 | req = rb_entry(parent, struct ceph_mon_generic_request, node); |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 471 | if (new->tid < req->tid) |
| 472 | p = &(*p)->rb_left; |
| 473 | else if (new->tid > req->tid) |
| 474 | p = &(*p)->rb_right; |
| 475 | else |
| 476 | BUG(); |
| 477 | } |
| 478 | |
| 479 | rb_link_node(&new->node, parent, p); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 480 | rb_insert_color(&new->node, &monc->generic_request_tree); |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 483 | static void release_generic_request(struct kref *kref) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 484 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 485 | struct ceph_mon_generic_request *req = |
| 486 | container_of(kref, struct ceph_mon_generic_request, kref); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 487 | |
| 488 | if (req->reply) |
| 489 | ceph_msg_put(req->reply); |
| 490 | if (req->request) |
| 491 | ceph_msg_put(req->request); |
Yehuda Sadeh | 2054756 | 2010-06-01 10:37:40 -0700 | [diff] [blame] | 492 | |
| 493 | kfree(req); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 496 | static void put_generic_request(struct ceph_mon_generic_request *req) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 497 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 498 | kref_put(&req->kref, release_generic_request); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 501 | static void get_generic_request(struct ceph_mon_generic_request *req) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 502 | { |
| 503 | kref_get(&req->kref); |
| 504 | } |
| 505 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 506 | static struct ceph_msg *get_generic_reply(struct ceph_connection *con, |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 507 | struct ceph_msg_header *hdr, |
| 508 | int *skip) |
| 509 | { |
| 510 | struct ceph_mon_client *monc = con->private; |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 511 | struct ceph_mon_generic_request *req; |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 512 | u64 tid = le64_to_cpu(hdr->tid); |
| 513 | struct ceph_msg *m; |
| 514 | |
| 515 | mutex_lock(&monc->mutex); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 516 | req = __lookup_generic_req(monc, tid); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 517 | if (!req) { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 518 | dout("get_generic_reply %lld dne\n", tid); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 519 | *skip = 1; |
| 520 | m = NULL; |
| 521 | } else { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 522 | dout("get_generic_reply %lld got %p\n", tid, req->reply); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 523 | *skip = 0; |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 524 | m = ceph_msg_get(req->reply); |
| 525 | /* |
| 526 | * we don't need to track the connection reading into |
| 527 | * this reply because we only have one open connection |
| 528 | * at a time, ever. |
| 529 | */ |
| 530 | } |
| 531 | mutex_unlock(&monc->mutex); |
| 532 | return m; |
| 533 | } |
| 534 | |
Ilya Dryomov | 513a824 | 2014-05-13 11:19:26 +0400 | [diff] [blame] | 535 | static int __do_generic_request(struct ceph_mon_client *monc, u64 tid, |
| 536 | struct ceph_mon_generic_request *req) |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 537 | { |
| 538 | int err; |
| 539 | |
| 540 | /* register request */ |
Ilya Dryomov | 513a824 | 2014-05-13 11:19:26 +0400 | [diff] [blame] | 541 | req->tid = tid != 0 ? tid : ++monc->last_tid; |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 542 | req->request->hdr.tid = cpu_to_le64(req->tid); |
| 543 | __insert_generic_request(monc, req); |
| 544 | monc->num_generic_requests++; |
Alex Elder | 6713093 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 545 | ceph_con_send(&monc->con, ceph_msg_get(req->request)); |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 546 | mutex_unlock(&monc->mutex); |
| 547 | |
| 548 | err = wait_for_completion_interruptible(&req->completion); |
| 549 | |
| 550 | mutex_lock(&monc->mutex); |
| 551 | rb_erase(&req->node, &monc->generic_request_tree); |
| 552 | monc->num_generic_requests--; |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 553 | |
| 554 | if (!err) |
| 555 | err = req->result; |
| 556 | return err; |
| 557 | } |
| 558 | |
Ilya Dryomov | 513a824 | 2014-05-13 11:19:26 +0400 | [diff] [blame] | 559 | static int do_generic_request(struct ceph_mon_client *monc, |
| 560 | struct ceph_mon_generic_request *req) |
| 561 | { |
| 562 | int err; |
| 563 | |
| 564 | mutex_lock(&monc->mutex); |
| 565 | err = __do_generic_request(monc, 0, req); |
| 566 | mutex_unlock(&monc->mutex); |
| 567 | |
| 568 | return err; |
| 569 | } |
| 570 | |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 571 | /* |
| 572 | * statfs |
| 573 | */ |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 574 | static void handle_statfs_reply(struct ceph_mon_client *monc, |
| 575 | struct ceph_msg *msg) |
| 576 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 577 | struct ceph_mon_generic_request *req; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 578 | struct ceph_mon_statfs_reply *reply = msg->front.iov_base; |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 579 | u64 tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 580 | |
| 581 | if (msg->front.iov_len != sizeof(*reply)) |
| 582 | goto bad; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 583 | dout("handle_statfs_reply %p tid %llu\n", msg, tid); |
| 584 | |
| 585 | mutex_lock(&monc->mutex); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 586 | req = __lookup_generic_req(monc, tid); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 587 | if (req) { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 588 | *(struct ceph_statfs *)req->buf = reply->st; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 589 | req->result = 0; |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 590 | get_generic_request(req); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 591 | } |
| 592 | mutex_unlock(&monc->mutex); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 593 | if (req) { |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 594 | complete_all(&req->completion); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 595 | put_generic_request(req); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 596 | } |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 597 | return; |
| 598 | |
| 599 | bad: |
Ilya Dryomov | 7a6fdeb | 2014-12-22 19:14:26 +0300 | [diff] [blame] | 600 | pr_err("corrupt statfs reply, tid %llu\n", tid); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 601 | ceph_msg_dump(msg); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 605 | * Do a synchronous statfs(). |
| 606 | */ |
| 607 | int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf) |
| 608 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 609 | struct ceph_mon_generic_request *req; |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 610 | struct ceph_mon_statfs *h; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 611 | int err; |
| 612 | |
Julia Lawall | cffe7b6 | 2010-05-13 22:07:29 +0200 | [diff] [blame] | 613 | req = kzalloc(sizeof(*req), GFP_NOFS); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 614 | if (!req) |
| 615 | return -ENOMEM; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 616 | |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 617 | kref_init(&req->kref); |
| 618 | req->buf = buf; |
| 619 | init_completion(&req->completion); |
| 620 | |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 621 | err = -ENOMEM; |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 622 | req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS, |
| 623 | true); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 624 | if (!req->request) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 625 | goto out; |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 626 | req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS, |
| 627 | true); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 628 | if (!req->reply) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 629 | goto out; |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 630 | |
| 631 | /* fill out request */ |
| 632 | h = req->request->front.iov_base; |
| 633 | h->monhdr.have_version = 0; |
| 634 | h->monhdr.session_mon = cpu_to_le16(-1); |
| 635 | h->monhdr.session_mon_tid = 0; |
| 636 | h->fsid = monc->monmap->fsid; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 637 | |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 638 | err = do_generic_request(monc, req); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 639 | |
| 640 | out: |
Ilya Dryomov | f646912 | 2014-12-22 19:35:17 +0300 | [diff] [blame] | 641 | put_generic_request(req); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 642 | return err; |
| 643 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 644 | EXPORT_SYMBOL(ceph_monc_do_statfs); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 645 | |
Ilya Dryomov | 513a824 | 2014-05-13 11:19:26 +0400 | [diff] [blame] | 646 | static void handle_get_version_reply(struct ceph_mon_client *monc, |
| 647 | struct ceph_msg *msg) |
| 648 | { |
| 649 | struct ceph_mon_generic_request *req; |
| 650 | u64 tid = le64_to_cpu(msg->hdr.tid); |
| 651 | void *p = msg->front.iov_base; |
| 652 | void *end = p + msg->front_alloc_len; |
| 653 | u64 handle; |
| 654 | |
| 655 | dout("%s %p tid %llu\n", __func__, msg, tid); |
| 656 | |
| 657 | ceph_decode_need(&p, end, 2*sizeof(u64), bad); |
| 658 | handle = ceph_decode_64(&p); |
| 659 | if (tid != 0 && tid != handle) |
| 660 | goto bad; |
| 661 | |
| 662 | mutex_lock(&monc->mutex); |
| 663 | req = __lookup_generic_req(monc, handle); |
| 664 | if (req) { |
| 665 | *(u64 *)req->buf = ceph_decode_64(&p); |
| 666 | req->result = 0; |
| 667 | get_generic_request(req); |
| 668 | } |
| 669 | mutex_unlock(&monc->mutex); |
| 670 | if (req) { |
| 671 | complete_all(&req->completion); |
| 672 | put_generic_request(req); |
| 673 | } |
| 674 | |
| 675 | return; |
| 676 | bad: |
Ilya Dryomov | 7a6fdeb | 2014-12-22 19:14:26 +0300 | [diff] [blame] | 677 | pr_err("corrupt mon_get_version reply, tid %llu\n", tid); |
Ilya Dryomov | 513a824 | 2014-05-13 11:19:26 +0400 | [diff] [blame] | 678 | ceph_msg_dump(msg); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Send MMonGetVersion and wait for the reply. |
| 683 | * |
| 684 | * @what: one of "mdsmap", "osdmap" or "monmap" |
| 685 | */ |
| 686 | int ceph_monc_do_get_version(struct ceph_mon_client *monc, const char *what, |
| 687 | u64 *newest) |
| 688 | { |
| 689 | struct ceph_mon_generic_request *req; |
| 690 | void *p, *end; |
| 691 | u64 tid; |
| 692 | int err; |
| 693 | |
| 694 | req = kzalloc(sizeof(*req), GFP_NOFS); |
| 695 | if (!req) |
| 696 | return -ENOMEM; |
| 697 | |
| 698 | kref_init(&req->kref); |
| 699 | req->buf = newest; |
Ilya Dryomov | 513a824 | 2014-05-13 11:19:26 +0400 | [diff] [blame] | 700 | init_completion(&req->completion); |
| 701 | |
| 702 | req->request = ceph_msg_new(CEPH_MSG_MON_GET_VERSION, |
| 703 | sizeof(u64) + sizeof(u32) + strlen(what), |
| 704 | GFP_NOFS, true); |
| 705 | if (!req->request) { |
| 706 | err = -ENOMEM; |
| 707 | goto out; |
| 708 | } |
| 709 | |
| 710 | req->reply = ceph_msg_new(CEPH_MSG_MON_GET_VERSION_REPLY, 1024, |
| 711 | GFP_NOFS, true); |
| 712 | if (!req->reply) { |
| 713 | err = -ENOMEM; |
| 714 | goto out; |
| 715 | } |
| 716 | |
| 717 | p = req->request->front.iov_base; |
| 718 | end = p + req->request->front_alloc_len; |
| 719 | |
| 720 | /* fill out request */ |
| 721 | mutex_lock(&monc->mutex); |
| 722 | tid = ++monc->last_tid; |
| 723 | ceph_encode_64(&p, tid); /* handle */ |
| 724 | ceph_encode_string(&p, end, what, strlen(what)); |
| 725 | |
| 726 | err = __do_generic_request(monc, tid, req); |
| 727 | |
| 728 | mutex_unlock(&monc->mutex); |
| 729 | out: |
Ilya Dryomov | f646912 | 2014-12-22 19:35:17 +0300 | [diff] [blame] | 730 | put_generic_request(req); |
Ilya Dryomov | 513a824 | 2014-05-13 11:19:26 +0400 | [diff] [blame] | 731 | return err; |
| 732 | } |
| 733 | EXPORT_SYMBOL(ceph_monc_do_get_version); |
| 734 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 735 | /* |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 736 | * Resend pending generic requests. |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 737 | */ |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 738 | static void __resend_generic_request(struct ceph_mon_client *monc) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 739 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 740 | struct ceph_mon_generic_request *req; |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 741 | struct rb_node *p; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 742 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 743 | for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) { |
| 744 | req = rb_entry(p, struct ceph_mon_generic_request, node); |
Alex Elder | 6740a84 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 745 | ceph_msg_revoke(req->request); |
Sage Weil | 4f471e4 | 2012-07-30 18:16:40 -0700 | [diff] [blame] | 746 | ceph_msg_revoke_incoming(req->reply); |
Alex Elder | 6713093 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 747 | ceph_con_send(&monc->con, ceph_msg_get(req->request)); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * Delayed work. If we haven't mounted yet, retry. Otherwise, |
| 753 | * renew/retry subscription as needed (in case it is timing out, or we |
| 754 | * got an ENOMEM). And keep the monitor connection alive. |
| 755 | */ |
| 756 | static void delayed_work(struct work_struct *work) |
| 757 | { |
| 758 | struct ceph_mon_client *monc = |
| 759 | container_of(work, struct ceph_mon_client, delayed_work.work); |
| 760 | |
| 761 | dout("monc delayed_work\n"); |
| 762 | mutex_lock(&monc->mutex); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 763 | if (monc->hunting) { |
| 764 | __close_session(monc); |
| 765 | __open_session(monc); /* continue hunting */ |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 766 | } else { |
Yan, Zheng | 8b9558a | 2015-09-01 17:19:38 +0800 | [diff] [blame] | 767 | struct ceph_options *opt = monc->client->options; |
| 768 | int is_auth = ceph_auth_is_authenticated(monc->auth); |
| 769 | if (ceph_con_keepalive_expired(&monc->con, |
| 770 | opt->monc_ping_timeout)) { |
| 771 | dout("monc keepalive timeout\n"); |
| 772 | is_auth = 0; |
| 773 | __close_session(monc); |
| 774 | monc->hunting = true; |
| 775 | __open_session(monc); |
| 776 | } |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 777 | |
Yan, Zheng | 8b9558a | 2015-09-01 17:19:38 +0800 | [diff] [blame] | 778 | if (!monc->hunting) { |
| 779 | ceph_con_keepalive(&monc->con); |
| 780 | __validate_auth(monc); |
| 781 | } |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 782 | |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 783 | if (is_auth) { |
| 784 | unsigned long now = jiffies; |
| 785 | |
| 786 | dout("%s renew subs? now %lu renew after %lu\n", |
| 787 | __func__, now, monc->sub_renew_after); |
| 788 | if (time_after_eq(now, monc->sub_renew_after)) |
| 789 | __send_subscribe(monc); |
| 790 | } |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 791 | } |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 792 | __schedule_delayed(monc); |
| 793 | mutex_unlock(&monc->mutex); |
| 794 | } |
| 795 | |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 796 | /* |
| 797 | * On startup, we build a temporary monmap populated with the IPs |
| 798 | * provided by mount(2). |
| 799 | */ |
| 800 | static int build_initial_monmap(struct ceph_mon_client *monc) |
| 801 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 802 | struct ceph_options *opt = monc->client->options; |
| 803 | struct ceph_entity_addr *mon_addr = opt->mon_addr; |
| 804 | int num_mon = opt->num_mon; |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 805 | int i; |
| 806 | |
| 807 | /* build initial monmap */ |
| 808 | monc->monmap = kzalloc(sizeof(*monc->monmap) + |
| 809 | num_mon*sizeof(monc->monmap->mon_inst[0]), |
| 810 | GFP_KERNEL); |
| 811 | if (!monc->monmap) |
| 812 | return -ENOMEM; |
| 813 | for (i = 0; i < num_mon; i++) { |
| 814 | monc->monmap->mon_inst[i].addr = mon_addr[i]; |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 815 | monc->monmap->mon_inst[i].addr.nonce = 0; |
| 816 | monc->monmap->mon_inst[i].name.type = |
| 817 | CEPH_ENTITY_TYPE_MON; |
| 818 | monc->monmap->mon_inst[i].name.num = cpu_to_le64(i); |
| 819 | } |
| 820 | monc->monmap->num_mon = num_mon; |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 824 | int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl) |
| 825 | { |
| 826 | int err = 0; |
| 827 | |
| 828 | dout("init\n"); |
| 829 | memset(monc, 0, sizeof(*monc)); |
| 830 | monc->client = cl; |
| 831 | monc->monmap = NULL; |
| 832 | mutex_init(&monc->mutex); |
| 833 | |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 834 | err = build_initial_monmap(monc); |
| 835 | if (err) |
| 836 | goto out; |
| 837 | |
Sage Weil | f6a2f5b | 2011-08-09 09:27:44 -0700 | [diff] [blame] | 838 | /* connection */ |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 839 | /* authentication */ |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 840 | monc->auth = ceph_auth_init(cl->options->name, |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 841 | cl->options->key); |
Noah Watkins | 49d9224 | 2011-09-12 14:51:58 -0700 | [diff] [blame] | 842 | if (IS_ERR(monc->auth)) { |
| 843 | err = PTR_ERR(monc->auth); |
Alex Elder | 6713093 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 844 | goto out_monmap; |
Noah Watkins | 49d9224 | 2011-09-12 14:51:58 -0700 | [diff] [blame] | 845 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 846 | monc->auth->want_keys = |
| 847 | CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON | |
| 848 | CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS; |
| 849 | |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 850 | /* msgs */ |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 851 | err = -ENOMEM; |
Sage Weil | 7c315c5 | 2010-03-24 21:52:30 -0700 | [diff] [blame] | 852 | monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK, |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 853 | sizeof(struct ceph_mon_subscribe_ack), |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 854 | GFP_NOFS, true); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 855 | if (!monc->m_subscribe_ack) |
Noah Watkins | 49d9224 | 2011-09-12 14:51:58 -0700 | [diff] [blame] | 856 | goto out_auth; |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 857 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 858 | monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS, |
| 859 | true); |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 860 | if (!monc->m_subscribe) |
| 861 | goto out_subscribe_ack; |
| 862 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 863 | monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS, |
| 864 | true); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 865 | if (!monc->m_auth_reply) |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 866 | goto out_subscribe; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 867 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 868 | monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS, true); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 869 | monc->pending_auth = 0; |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 870 | if (!monc->m_auth) |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 871 | goto out_auth_reply; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 872 | |
Sage Weil | 735a72e | 2012-06-27 12:24:34 -0700 | [diff] [blame] | 873 | ceph_con_init(&monc->con, monc, &mon_con_ops, |
| 874 | &monc->client->msgr); |
| 875 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 876 | monc->cur_mon = -1; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 877 | monc->hunting = true; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 878 | monc->sub_renew_after = jiffies; |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame^] | 879 | monc->sub_renew_sent = 0; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 880 | |
| 881 | INIT_DELAYED_WORK(&monc->delayed_work, delayed_work); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 882 | monc->generic_request_tree = RB_ROOT; |
| 883 | monc->num_generic_requests = 0; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 884 | monc->last_tid = 0; |
| 885 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 886 | return 0; |
| 887 | |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 888 | out_auth_reply: |
| 889 | ceph_msg_put(monc->m_auth_reply); |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 890 | out_subscribe: |
| 891 | ceph_msg_put(monc->m_subscribe); |
Sage Weil | 7c315c5 | 2010-03-24 21:52:30 -0700 | [diff] [blame] | 892 | out_subscribe_ack: |
| 893 | ceph_msg_put(monc->m_subscribe_ack); |
Noah Watkins | 49d9224 | 2011-09-12 14:51:58 -0700 | [diff] [blame] | 894 | out_auth: |
| 895 | ceph_auth_destroy(monc->auth); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 896 | out_monmap: |
| 897 | kfree(monc->monmap); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 898 | out: |
| 899 | return err; |
| 900 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 901 | EXPORT_SYMBOL(ceph_monc_init); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 902 | |
| 903 | void ceph_monc_stop(struct ceph_mon_client *monc) |
| 904 | { |
| 905 | dout("stop\n"); |
| 906 | cancel_delayed_work_sync(&monc->delayed_work); |
| 907 | |
| 908 | mutex_lock(&monc->mutex); |
| 909 | __close_session(monc); |
Sage Weil | f6a2f5b | 2011-08-09 09:27:44 -0700 | [diff] [blame] | 910 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 911 | mutex_unlock(&monc->mutex); |
| 912 | |
Sage Weil | f3dea7e | 2012-06-10 20:43:56 -0700 | [diff] [blame] | 913 | /* |
| 914 | * flush msgr queue before we destroy ourselves to ensure that: |
| 915 | * - any work that references our embedded con is finished. |
| 916 | * - any osd_client or other work that may reference an authorizer |
| 917 | * finishes before we shut down the auth subsystem. |
| 918 | */ |
| 919 | ceph_msgr_flush(); |
| 920 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 921 | ceph_auth_destroy(monc->auth); |
| 922 | |
| 923 | ceph_msg_put(monc->m_auth); |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 924 | ceph_msg_put(monc->m_auth_reply); |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 925 | ceph_msg_put(monc->m_subscribe); |
Sage Weil | 7c315c5 | 2010-03-24 21:52:30 -0700 | [diff] [blame] | 926 | ceph_msg_put(monc->m_subscribe_ack); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 927 | |
| 928 | kfree(monc->monmap); |
| 929 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 930 | EXPORT_SYMBOL(ceph_monc_stop); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 931 | |
Ilya Dryomov | 0f9af16 | 2016-01-08 21:17:22 +0300 | [diff] [blame] | 932 | static void finish_hunting(struct ceph_mon_client *monc) |
| 933 | { |
| 934 | if (monc->hunting) { |
| 935 | dout("%s found mon%d\n", __func__, monc->cur_mon); |
| 936 | monc->hunting = false; |
| 937 | } |
| 938 | } |
| 939 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 940 | static void handle_auth_reply(struct ceph_mon_client *monc, |
| 941 | struct ceph_msg *msg) |
| 942 | { |
| 943 | int ret; |
Sage Weil | 09c4d6a | 2010-05-25 15:38:06 -0700 | [diff] [blame] | 944 | int was_auth = 0; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 945 | |
| 946 | mutex_lock(&monc->mutex); |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 947 | was_auth = ceph_auth_is_authenticated(monc->auth); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 948 | monc->pending_auth = 0; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 949 | ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base, |
| 950 | msg->front.iov_len, |
| 951 | monc->m_auth->front.iov_base, |
Ilya Dryomov | 3cea4c3 | 2014-01-09 20:08:21 +0200 | [diff] [blame] | 952 | monc->m_auth->front_alloc_len); |
Ilya Dryomov | 0f9af16 | 2016-01-08 21:17:22 +0300 | [diff] [blame] | 953 | if (ret > 0) { |
| 954 | __send_prepared_auth_request(monc, ret); |
| 955 | goto out; |
| 956 | } |
| 957 | |
| 958 | finish_hunting(monc); |
| 959 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 960 | if (ret < 0) { |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 961 | monc->client->auth_err = ret; |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 962 | } else if (!was_auth && ceph_auth_is_authenticated(monc->auth)) { |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 963 | dout("authenticated, starting session\n"); |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 964 | |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 965 | monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT; |
| 966 | monc->client->msgr.inst.name.num = |
Yehuda Sadeh | 0cf5537 | 2010-06-11 15:57:06 -0700 | [diff] [blame] | 967 | cpu_to_le64(monc->auth->global_id); |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 968 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 969 | __send_subscribe(monc); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 970 | __resend_generic_request(monc); |
Ilya Dryomov | 0f9af16 | 2016-01-08 21:17:22 +0300 | [diff] [blame] | 971 | |
| 972 | pr_info("mon%d %s session established\n", monc->cur_mon, |
| 973 | ceph_pr_addr(&monc->con.peer_addr.in_addr)); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 974 | } |
Ilya Dryomov | 0f9af16 | 2016-01-08 21:17:22 +0300 | [diff] [blame] | 975 | |
| 976 | out: |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 977 | mutex_unlock(&monc->mutex); |
Ilya Dryomov | 0f9af16 | 2016-01-08 21:17:22 +0300 | [diff] [blame] | 978 | if (monc->client->auth_err < 0) |
| 979 | wake_up_all(&monc->client->auth_wq); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 980 | } |
| 981 | |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 982 | static int __validate_auth(struct ceph_mon_client *monc) |
| 983 | { |
| 984 | int ret; |
| 985 | |
| 986 | if (monc->pending_auth) |
| 987 | return 0; |
| 988 | |
| 989 | ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base, |
Ilya Dryomov | 3cea4c3 | 2014-01-09 20:08:21 +0200 | [diff] [blame] | 990 | monc->m_auth->front_alloc_len); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 991 | if (ret <= 0) |
| 992 | return ret; /* either an error, or no need to authenticate */ |
| 993 | __send_prepared_auth_request(monc, ret); |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | int ceph_monc_validate_auth(struct ceph_mon_client *monc) |
| 998 | { |
| 999 | int ret; |
| 1000 | |
| 1001 | mutex_lock(&monc->mutex); |
| 1002 | ret = __validate_auth(monc); |
| 1003 | mutex_unlock(&monc->mutex); |
| 1004 | return ret; |
| 1005 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1006 | EXPORT_SYMBOL(ceph_monc_validate_auth); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 1007 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1008 | /* |
| 1009 | * handle incoming message |
| 1010 | */ |
| 1011 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) |
| 1012 | { |
| 1013 | struct ceph_mon_client *monc = con->private; |
| 1014 | int type = le16_to_cpu(msg->hdr.type); |
| 1015 | |
| 1016 | if (!monc) |
| 1017 | return; |
| 1018 | |
| 1019 | switch (type) { |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1020 | case CEPH_MSG_AUTH_REPLY: |
| 1021 | handle_auth_reply(monc, msg); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1022 | break; |
| 1023 | |
| 1024 | case CEPH_MSG_MON_SUBSCRIBE_ACK: |
| 1025 | handle_subscribe_ack(monc, msg); |
| 1026 | break; |
| 1027 | |
| 1028 | case CEPH_MSG_STATFS_REPLY: |
| 1029 | handle_statfs_reply(monc, msg); |
| 1030 | break; |
| 1031 | |
Ilya Dryomov | 513a824 | 2014-05-13 11:19:26 +0400 | [diff] [blame] | 1032 | case CEPH_MSG_MON_GET_VERSION_REPLY: |
| 1033 | handle_get_version_reply(monc, msg); |
| 1034 | break; |
| 1035 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1036 | case CEPH_MSG_MON_MAP: |
| 1037 | ceph_monc_handle_map(monc, msg); |
| 1038 | break; |
| 1039 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1040 | case CEPH_MSG_OSD_MAP: |
| 1041 | ceph_osdc_handle_map(&monc->client->osdc, msg); |
| 1042 | break; |
| 1043 | |
| 1044 | default: |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1045 | /* can the chained handler handle it? */ |
| 1046 | if (monc->client->extra_mon_dispatch && |
| 1047 | monc->client->extra_mon_dispatch(monc->client, msg) == 0) |
| 1048 | break; |
| 1049 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1050 | pr_err("received unknown message type %d %s\n", type, |
| 1051 | ceph_msg_type_name(type)); |
| 1052 | } |
| 1053 | ceph_msg_put(msg); |
| 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | * Allocate memory for incoming message |
| 1058 | */ |
| 1059 | static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con, |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1060 | struct ceph_msg_header *hdr, |
| 1061 | int *skip) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1062 | { |
| 1063 | struct ceph_mon_client *monc = con->private; |
| 1064 | int type = le16_to_cpu(hdr->type); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1065 | int front_len = le32_to_cpu(hdr->front_len); |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 1066 | struct ceph_msg *m = NULL; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1067 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1068 | *skip = 0; |
Yehuda Sadeh | 0547a9b | 2010-01-11 14:47:13 -0800 | [diff] [blame] | 1069 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1070 | switch (type) { |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1071 | case CEPH_MSG_MON_SUBSCRIBE_ACK: |
Sage Weil | 7c315c5 | 2010-03-24 21:52:30 -0700 | [diff] [blame] | 1072 | m = ceph_msg_get(monc->m_subscribe_ack); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1073 | break; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1074 | case CEPH_MSG_STATFS_REPLY: |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 1075 | return get_generic_reply(con, hdr, skip); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1076 | case CEPH_MSG_AUTH_REPLY: |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 1077 | m = ceph_msg_get(monc->m_auth_reply); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1078 | break; |
Ilya Dryomov | 513a824 | 2014-05-13 11:19:26 +0400 | [diff] [blame] | 1079 | case CEPH_MSG_MON_GET_VERSION_REPLY: |
| 1080 | if (le64_to_cpu(hdr->tid) != 0) |
| 1081 | return get_generic_reply(con, hdr, skip); |
| 1082 | |
| 1083 | /* |
| 1084 | * Older OSDs don't set reply tid even if the orignal |
| 1085 | * request had a non-zero tid. Workaround this weirdness |
| 1086 | * by falling through to the allocate case. |
| 1087 | */ |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 1088 | case CEPH_MSG_MON_MAP: |
| 1089 | case CEPH_MSG_MDS_MAP: |
| 1090 | case CEPH_MSG_OSD_MAP: |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 1091 | m = ceph_msg_new(type, front_len, GFP_NOFS, false); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 1092 | if (!m) |
| 1093 | return NULL; /* ENOMEM--return skip == 0 */ |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 1094 | break; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1095 | } |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1096 | |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 1097 | if (!m) { |
| 1098 | pr_info("alloc_msg unknown type %d\n", type); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1099 | *skip = 1; |
Sage Weil | 73c3d48 | 2014-08-04 07:01:54 -0700 | [diff] [blame] | 1100 | } else if (front_len > m->front_alloc_len) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 1101 | pr_warn("mon_alloc_msg front %d > prealloc %d (%u#%llu)\n", |
| 1102 | front_len, m->front_alloc_len, |
| 1103 | (unsigned int)con->peer_name.type, |
| 1104 | le64_to_cpu(con->peer_name.num)); |
Sage Weil | 73c3d48 | 2014-08-04 07:01:54 -0700 | [diff] [blame] | 1105 | ceph_msg_put(m); |
| 1106 | m = ceph_msg_new(type, front_len, GFP_NOFS, false); |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 1107 | } |
Sage Weil | 73c3d48 | 2014-08-04 07:01:54 -0700 | [diff] [blame] | 1108 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1109 | return m; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * If the monitor connection resets, pick a new monitor and resubmit |
| 1114 | * any pending requests. |
| 1115 | */ |
| 1116 | static void mon_fault(struct ceph_connection *con) |
| 1117 | { |
| 1118 | struct ceph_mon_client *monc = con->private; |
| 1119 | |
| 1120 | if (!monc) |
| 1121 | return; |
| 1122 | |
| 1123 | dout("mon_fault\n"); |
| 1124 | mutex_lock(&monc->mutex); |
| 1125 | if (!con->private) |
| 1126 | goto out; |
| 1127 | |
Sage Weil | f6a2f5b | 2011-08-09 09:27:44 -0700 | [diff] [blame] | 1128 | if (!monc->hunting) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1129 | pr_info("mon%d %s session lost, " |
| 1130 | "hunting for new mon\n", monc->cur_mon, |
Alex Elder | 6713093 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 1131 | ceph_pr_addr(&monc->con.peer_addr.in_addr)); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1132 | |
| 1133 | __close_session(monc); |
| 1134 | if (!monc->hunting) { |
| 1135 | /* start hunting */ |
| 1136 | monc->hunting = true; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1137 | __open_session(monc); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1138 | } else { |
| 1139 | /* already hunting, let's wait a bit */ |
| 1140 | __schedule_delayed(monc); |
| 1141 | } |
| 1142 | out: |
| 1143 | mutex_unlock(&monc->mutex); |
| 1144 | } |
| 1145 | |
Sage Weil | ec87ef4 | 2012-05-31 20:27:50 -0700 | [diff] [blame] | 1146 | /* |
| 1147 | * We can ignore refcounting on the connection struct, as all references |
| 1148 | * will come from the messenger workqueue, which is drained prior to |
| 1149 | * mon_client destruction. |
| 1150 | */ |
| 1151 | static struct ceph_connection *con_get(struct ceph_connection *con) |
| 1152 | { |
| 1153 | return con; |
| 1154 | } |
| 1155 | |
| 1156 | static void con_put(struct ceph_connection *con) |
| 1157 | { |
| 1158 | } |
| 1159 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 1160 | static const struct ceph_connection_operations mon_con_ops = { |
Sage Weil | ec87ef4 | 2012-05-31 20:27:50 -0700 | [diff] [blame] | 1161 | .get = con_get, |
| 1162 | .put = con_put, |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1163 | .dispatch = dispatch, |
| 1164 | .fault = mon_fault, |
| 1165 | .alloc_msg = mon_alloc_msg, |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1166 | }; |