Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1 | #include "ceph_debug.h" |
| 2 | |
| 3 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 4 | #include <linux/slab.h> |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 5 | #include <linux/random.h> |
| 6 | #include <linux/sched.h> |
| 7 | |
| 8 | #include "mon_client.h" |
| 9 | #include "super.h" |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 10 | #include "auth.h" |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 11 | #include "decode.h" |
| 12 | |
| 13 | /* |
| 14 | * Interact with Ceph monitor cluster. Handle requests for new map |
| 15 | * versions, and periodically resend as needed. Also implement |
| 16 | * statfs() and umount(). |
| 17 | * |
| 18 | * A small cluster of Ceph "monitors" are responsible for managing critical |
| 19 | * cluster configuration and state information. An odd number (e.g., 3, 5) |
| 20 | * of cmon daemons use a modified version of the Paxos part-time parliament |
| 21 | * algorithm to manage the MDS map (mds cluster membership), OSD map, and |
| 22 | * list of clients who have mounted the file system. |
| 23 | * |
| 24 | * We maintain an open, active session with a monitor at all times in order to |
| 25 | * receive timely MDSMap updates. We periodically send a keepalive byte on the |
| 26 | * TCP socket to ensure we detect a failure. If the connection does break, we |
| 27 | * randomly hunt for a new monitor. Once the connection is reestablished, we |
| 28 | * resend any outstanding requests. |
| 29 | */ |
| 30 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 31 | static const struct ceph_connection_operations mon_con_ops; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 32 | |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 33 | static int __validate_auth(struct ceph_mon_client *monc); |
| 34 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Decode a monmap blob (e.g., during mount). |
| 37 | */ |
| 38 | struct ceph_monmap *ceph_monmap_decode(void *p, void *end) |
| 39 | { |
| 40 | struct ceph_monmap *m = NULL; |
| 41 | int i, err = -EINVAL; |
| 42 | struct ceph_fsid fsid; |
| 43 | u32 epoch, num_mon; |
| 44 | u16 version; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 45 | u32 len; |
| 46 | |
| 47 | ceph_decode_32_safe(&p, end, len, bad); |
| 48 | ceph_decode_need(&p, end, len, bad); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 49 | |
| 50 | dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p)); |
| 51 | |
| 52 | ceph_decode_16_safe(&p, end, version, bad); |
| 53 | |
| 54 | ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad); |
| 55 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 56 | epoch = ceph_decode_32(&p); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 57 | |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 58 | num_mon = ceph_decode_32(&p); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 59 | ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad); |
| 60 | |
| 61 | if (num_mon >= CEPH_MAX_MON) |
| 62 | goto bad; |
| 63 | m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS); |
| 64 | if (m == NULL) |
| 65 | return ERR_PTR(-ENOMEM); |
| 66 | m->fsid = fsid; |
| 67 | m->epoch = epoch; |
| 68 | m->num_mon = num_mon; |
| 69 | 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] | 70 | for (i = 0; i < num_mon; i++) |
| 71 | ceph_decode_addr(&m->mon_inst[i].addr); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 72 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 73 | dout("monmap_decode epoch %d, num_mon %d\n", m->epoch, |
| 74 | m->num_mon); |
| 75 | for (i = 0; i < m->num_mon; i++) |
| 76 | dout("monmap_decode mon%d is %s\n", i, |
| 77 | pr_addr(&m->mon_inst[i].addr.in_addr)); |
| 78 | return m; |
| 79 | |
| 80 | bad: |
| 81 | dout("monmap_decode failed with %d\n", err); |
| 82 | kfree(m); |
| 83 | return ERR_PTR(err); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * return true if *addr is included in the monmap. |
| 88 | */ |
| 89 | int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr) |
| 90 | { |
| 91 | int i; |
| 92 | |
| 93 | for (i = 0; i < m->num_mon; i++) |
Sage Weil | 103e2d3 | 2010-01-07 16:12:36 -0800 | [diff] [blame] | 94 | if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 95 | return 1; |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* |
Sage Weil | 5ce6e9d | 2010-02-15 16:22:28 -0800 | [diff] [blame] | 100 | * Send an auth request. |
| 101 | */ |
| 102 | static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len) |
| 103 | { |
| 104 | monc->pending_auth = 1; |
| 105 | monc->m_auth->front.iov_len = len; |
| 106 | monc->m_auth->hdr.front_len = cpu_to_le32(len); |
Sage Weil | 9706900 | 2010-05-21 12:31:49 -0700 | [diff] [blame] | 107 | ceph_con_revoke(monc->con, monc->m_auth); |
Sage Weil | 5ce6e9d | 2010-02-15 16:22:28 -0800 | [diff] [blame] | 108 | ceph_msg_get(monc->m_auth); /* keep our ref */ |
| 109 | ceph_con_send(monc->con, monc->m_auth); |
| 110 | } |
| 111 | |
| 112 | /* |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 113 | * Close monitor session, if any. |
| 114 | */ |
| 115 | static void __close_session(struct ceph_mon_client *monc) |
| 116 | { |
| 117 | if (monc->con) { |
| 118 | dout("__close_session closing mon%d\n", monc->cur_mon); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 119 | ceph_con_revoke(monc->con, monc->m_auth); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 120 | ceph_con_close(monc->con); |
| 121 | monc->cur_mon = -1; |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 122 | monc->pending_auth = 0; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 123 | ceph_auth_reset(monc->auth); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Open a session with a (new) monitor. |
| 129 | */ |
| 130 | static int __open_session(struct ceph_mon_client *monc) |
| 131 | { |
| 132 | char r; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 133 | int ret; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 134 | |
| 135 | if (monc->cur_mon < 0) { |
| 136 | get_random_bytes(&r, 1); |
| 137 | monc->cur_mon = r % monc->monmap->num_mon; |
| 138 | dout("open_session num=%d r=%d -> mon%d\n", |
| 139 | monc->monmap->num_mon, r, monc->cur_mon); |
| 140 | monc->sub_sent = 0; |
| 141 | monc->sub_renew_after = jiffies; /* i.e., expired */ |
| 142 | monc->want_next_osdmap = !!monc->want_next_osdmap; |
| 143 | |
| 144 | dout("open_session mon%d opening\n", monc->cur_mon); |
| 145 | monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON; |
| 146 | monc->con->peer_name.num = cpu_to_le64(monc->cur_mon); |
| 147 | ceph_con_open(monc->con, |
| 148 | &monc->monmap->mon_inst[monc->cur_mon].addr); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 149 | |
| 150 | /* initiatiate authentication handshake */ |
| 151 | ret = ceph_auth_build_hello(monc->auth, |
| 152 | monc->m_auth->front.iov_base, |
| 153 | monc->m_auth->front_max); |
Sage Weil | 5ce6e9d | 2010-02-15 16:22:28 -0800 | [diff] [blame] | 154 | __send_prepared_auth_request(monc, ret); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 155 | } else { |
| 156 | dout("open_session mon%d already open\n", monc->cur_mon); |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static bool __sub_expired(struct ceph_mon_client *monc) |
| 162 | { |
| 163 | return time_after_eq(jiffies, monc->sub_renew_after); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Reschedule delayed work timer. |
| 168 | */ |
| 169 | static void __schedule_delayed(struct ceph_mon_client *monc) |
| 170 | { |
| 171 | unsigned delay; |
| 172 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 173 | if (monc->cur_mon < 0 || __sub_expired(monc)) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 174 | delay = 10 * HZ; |
| 175 | else |
| 176 | delay = 20 * HZ; |
| 177 | dout("__schedule_delayed after %u\n", delay); |
| 178 | schedule_delayed_work(&monc->delayed_work, delay); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Send subscribe request for mdsmap and/or osdmap. |
| 183 | */ |
| 184 | static void __send_subscribe(struct ceph_mon_client *monc) |
| 185 | { |
| 186 | dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n", |
| 187 | (unsigned)monc->sub_sent, __sub_expired(monc), |
| 188 | monc->want_next_osdmap); |
| 189 | if ((__sub_expired(monc) && !monc->sub_sent) || |
| 190 | monc->want_next_osdmap == 1) { |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 191 | struct ceph_msg *msg = monc->m_subscribe; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 192 | struct ceph_mon_subscribe_item *i; |
| 193 | void *p, *end; |
| 194 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 195 | p = msg->front.iov_base; |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 196 | end = p + msg->front_max; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 197 | |
| 198 | dout("__send_subscribe to 'mdsmap' %u+\n", |
| 199 | (unsigned)monc->have_mdsmap); |
| 200 | if (monc->want_next_osdmap) { |
| 201 | dout("__send_subscribe to 'osdmap' %u\n", |
| 202 | (unsigned)monc->have_osdmap); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 203 | ceph_encode_32(&p, 3); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 204 | ceph_encode_string(&p, end, "osdmap", 6); |
| 205 | i = p; |
| 206 | i->have = cpu_to_le64(monc->have_osdmap); |
| 207 | i->onetime = 1; |
| 208 | p += sizeof(*i); |
| 209 | monc->want_next_osdmap = 2; /* requested */ |
| 210 | } else { |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 211 | ceph_encode_32(&p, 2); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 212 | } |
| 213 | ceph_encode_string(&p, end, "mdsmap", 6); |
| 214 | i = p; |
| 215 | i->have = cpu_to_le64(monc->have_mdsmap); |
| 216 | i->onetime = 0; |
| 217 | p += sizeof(*i); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 218 | ceph_encode_string(&p, end, "monmap", 6); |
| 219 | i = p; |
| 220 | i->have = 0; |
| 221 | i->onetime = 0; |
| 222 | p += sizeof(*i); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 223 | |
| 224 | msg->front.iov_len = p - msg->front.iov_base; |
| 225 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 226 | ceph_con_revoke(monc->con, msg); |
| 227 | ceph_con_send(monc->con, ceph_msg_get(msg)); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 228 | |
| 229 | monc->sub_sent = jiffies | 1; /* never 0 */ |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static void handle_subscribe_ack(struct ceph_mon_client *monc, |
| 234 | struct ceph_msg *msg) |
| 235 | { |
| 236 | unsigned seconds; |
Sage Weil | 07bd10f | 2009-10-14 17:26:40 -0700 | [diff] [blame] | 237 | struct ceph_mon_subscribe_ack *h = msg->front.iov_base; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 238 | |
Sage Weil | 07bd10f | 2009-10-14 17:26:40 -0700 | [diff] [blame] | 239 | if (msg->front.iov_len < sizeof(*h)) |
| 240 | goto bad; |
| 241 | seconds = le32_to_cpu(h->duration); |
| 242 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 243 | mutex_lock(&monc->mutex); |
| 244 | if (monc->hunting) { |
| 245 | pr_info("mon%d %s session established\n", |
| 246 | monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr)); |
| 247 | monc->hunting = false; |
| 248 | } |
| 249 | dout("handle_subscribe_ack after %d seconds\n", seconds); |
Sage Weil | 0656d11 | 2009-10-08 10:25:46 -0700 | [diff] [blame] | 250 | monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 251 | monc->sub_sent = 0; |
| 252 | mutex_unlock(&monc->mutex); |
| 253 | return; |
| 254 | bad: |
| 255 | pr_err("got corrupt subscribe-ack msg\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 256 | ceph_msg_dump(msg); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Keep track of which maps we have |
| 261 | */ |
| 262 | int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got) |
| 263 | { |
| 264 | mutex_lock(&monc->mutex); |
| 265 | monc->have_mdsmap = got; |
| 266 | mutex_unlock(&monc->mutex); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got) |
| 271 | { |
| 272 | mutex_lock(&monc->mutex); |
| 273 | monc->have_osdmap = got; |
| 274 | monc->want_next_osdmap = 0; |
| 275 | mutex_unlock(&monc->mutex); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Register interest in the next osdmap |
| 281 | */ |
| 282 | void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc) |
| 283 | { |
| 284 | dout("request_next_osdmap have %u\n", monc->have_osdmap); |
| 285 | mutex_lock(&monc->mutex); |
| 286 | if (!monc->want_next_osdmap) |
| 287 | monc->want_next_osdmap = 1; |
| 288 | if (monc->want_next_osdmap < 2) |
| 289 | __send_subscribe(monc); |
| 290 | mutex_unlock(&monc->mutex); |
| 291 | } |
| 292 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 293 | /* |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 294 | * |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 295 | */ |
| 296 | int ceph_monc_open_session(struct ceph_mon_client *monc) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 297 | { |
| 298 | if (!monc->con) { |
| 299 | monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL); |
| 300 | if (!monc->con) |
| 301 | return -ENOMEM; |
| 302 | ceph_con_init(monc->client->msgr, monc->con); |
| 303 | monc->con->private = monc; |
| 304 | monc->con->ops = &mon_con_ops; |
| 305 | } |
| 306 | |
| 307 | mutex_lock(&monc->mutex); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 308 | __open_session(monc); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 309 | __schedule_delayed(monc); |
| 310 | mutex_unlock(&monc->mutex); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * The monitor responds with mount ack indicate mount success. The |
| 316 | * included client ticket allows the client to talk to MDSs and OSDs. |
| 317 | */ |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 318 | static void ceph_monc_handle_map(struct ceph_mon_client *monc, |
| 319 | struct ceph_msg *msg) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 320 | { |
| 321 | struct ceph_client *client = monc->client; |
| 322 | struct ceph_monmap *monmap = NULL, *old = monc->monmap; |
| 323 | void *p, *end; |
| 324 | |
| 325 | mutex_lock(&monc->mutex); |
| 326 | |
| 327 | dout("handle_monmap\n"); |
| 328 | p = msg->front.iov_base; |
| 329 | end = p + msg->front.iov_len; |
| 330 | |
| 331 | monmap = ceph_monmap_decode(p, end); |
| 332 | if (IS_ERR(monmap)) { |
| 333 | pr_err("problem decoding monmap, %d\n", |
| 334 | (int)PTR_ERR(monmap)); |
Sage Weil | d4a780c | 2009-12-11 08:55:23 -0800 | [diff] [blame] | 335 | goto out; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 336 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 337 | |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 338 | if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) { |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 339 | kfree(monmap); |
Sage Weil | d4a780c | 2009-12-11 08:55:23 -0800 | [diff] [blame] | 340 | goto out; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | client->monc.monmap = monmap; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 344 | kfree(old); |
| 345 | |
Sage Weil | d4a780c | 2009-12-11 08:55:23 -0800 | [diff] [blame] | 346 | out: |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 347 | mutex_unlock(&monc->mutex); |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 348 | wake_up_all(&client->auth_wq); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 349 | } |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 350 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 351 | /* |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 352 | * generic requests (e.g., statfs, poolop) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 353 | */ |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 354 | static struct ceph_mon_generic_request *__lookup_generic_req( |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 355 | struct ceph_mon_client *monc, u64 tid) |
| 356 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 357 | struct ceph_mon_generic_request *req; |
| 358 | struct rb_node *n = monc->generic_request_tree.rb_node; |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 359 | |
| 360 | while (n) { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 361 | req = rb_entry(n, struct ceph_mon_generic_request, node); |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 362 | if (tid < req->tid) |
| 363 | n = n->rb_left; |
| 364 | else if (tid > req->tid) |
| 365 | n = n->rb_right; |
| 366 | else |
| 367 | return req; |
| 368 | } |
| 369 | return NULL; |
| 370 | } |
| 371 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 372 | static void __insert_generic_request(struct ceph_mon_client *monc, |
| 373 | struct ceph_mon_generic_request *new) |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 374 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 375 | struct rb_node **p = &monc->generic_request_tree.rb_node; |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 376 | struct rb_node *parent = NULL; |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 377 | struct ceph_mon_generic_request *req = NULL; |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 378 | |
| 379 | while (*p) { |
| 380 | parent = *p; |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 381 | req = rb_entry(parent, struct ceph_mon_generic_request, node); |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 382 | if (new->tid < req->tid) |
| 383 | p = &(*p)->rb_left; |
| 384 | else if (new->tid > req->tid) |
| 385 | p = &(*p)->rb_right; |
| 386 | else |
| 387 | BUG(); |
| 388 | } |
| 389 | |
| 390 | rb_link_node(&new->node, parent, p); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 391 | rb_insert_color(&new->node, &monc->generic_request_tree); |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 394 | static void release_generic_request(struct kref *kref) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 395 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 396 | struct ceph_mon_generic_request *req = |
| 397 | container_of(kref, struct ceph_mon_generic_request, kref); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 398 | |
| 399 | if (req->reply) |
| 400 | ceph_msg_put(req->reply); |
| 401 | if (req->request) |
| 402 | ceph_msg_put(req->request); |
Yehuda Sadeh | 2054756 | 2010-06-01 10:37:40 -0700 | [diff] [blame] | 403 | |
| 404 | kfree(req); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 407 | static void put_generic_request(struct ceph_mon_generic_request *req) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 408 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 409 | kref_put(&req->kref, release_generic_request); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 412 | static void get_generic_request(struct ceph_mon_generic_request *req) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 413 | { |
| 414 | kref_get(&req->kref); |
| 415 | } |
| 416 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 417 | static struct ceph_msg *get_generic_reply(struct ceph_connection *con, |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 418 | struct ceph_msg_header *hdr, |
| 419 | int *skip) |
| 420 | { |
| 421 | struct ceph_mon_client *monc = con->private; |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 422 | struct ceph_mon_generic_request *req; |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 423 | u64 tid = le64_to_cpu(hdr->tid); |
| 424 | struct ceph_msg *m; |
| 425 | |
| 426 | mutex_lock(&monc->mutex); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 427 | req = __lookup_generic_req(monc, tid); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 428 | if (!req) { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 429 | dout("get_generic_reply %lld dne\n", tid); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 430 | *skip = 1; |
| 431 | m = NULL; |
| 432 | } else { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 433 | dout("get_generic_reply %lld got %p\n", tid, req->reply); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 434 | m = ceph_msg_get(req->reply); |
| 435 | /* |
| 436 | * we don't need to track the connection reading into |
| 437 | * this reply because we only have one open connection |
| 438 | * at a time, ever. |
| 439 | */ |
| 440 | } |
| 441 | mutex_unlock(&monc->mutex); |
| 442 | return m; |
| 443 | } |
| 444 | |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 445 | static int do_generic_request(struct ceph_mon_client *monc, |
| 446 | struct ceph_mon_generic_request *req) |
| 447 | { |
| 448 | int err; |
| 449 | |
| 450 | /* register request */ |
| 451 | mutex_lock(&monc->mutex); |
| 452 | req->tid = ++monc->last_tid; |
| 453 | req->request->hdr.tid = cpu_to_le64(req->tid); |
| 454 | __insert_generic_request(monc, req); |
| 455 | monc->num_generic_requests++; |
| 456 | ceph_con_send(monc->con, ceph_msg_get(req->request)); |
| 457 | mutex_unlock(&monc->mutex); |
| 458 | |
| 459 | err = wait_for_completion_interruptible(&req->completion); |
| 460 | |
| 461 | mutex_lock(&monc->mutex); |
| 462 | rb_erase(&req->node, &monc->generic_request_tree); |
| 463 | monc->num_generic_requests--; |
| 464 | mutex_unlock(&monc->mutex); |
| 465 | |
| 466 | if (!err) |
| 467 | err = req->result; |
| 468 | return err; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * statfs |
| 473 | */ |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 474 | static void handle_statfs_reply(struct ceph_mon_client *monc, |
| 475 | struct ceph_msg *msg) |
| 476 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 477 | struct ceph_mon_generic_request *req; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 478 | struct ceph_mon_statfs_reply *reply = msg->front.iov_base; |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 479 | u64 tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 480 | |
| 481 | if (msg->front.iov_len != sizeof(*reply)) |
| 482 | goto bad; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 483 | dout("handle_statfs_reply %p tid %llu\n", msg, tid); |
| 484 | |
| 485 | mutex_lock(&monc->mutex); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 486 | req = __lookup_generic_req(monc, tid); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 487 | if (req) { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 488 | *(struct ceph_statfs *)req->buf = reply->st; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 489 | req->result = 0; |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 490 | get_generic_request(req); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 491 | } |
| 492 | mutex_unlock(&monc->mutex); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 493 | if (req) { |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 494 | complete_all(&req->completion); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 495 | put_generic_request(req); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 496 | } |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 497 | return; |
| 498 | |
| 499 | bad: |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 500 | pr_err("corrupt generic reply, tid %llu\n", tid); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 501 | ceph_msg_dump(msg); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 505 | * Do a synchronous statfs(). |
| 506 | */ |
| 507 | int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf) |
| 508 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 509 | struct ceph_mon_generic_request *req; |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 510 | struct ceph_mon_statfs *h; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 511 | int err; |
| 512 | |
Julia Lawall | cffe7b6 | 2010-05-13 22:07:29 +0200 | [diff] [blame] | 513 | req = kzalloc(sizeof(*req), GFP_NOFS); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 514 | if (!req) |
| 515 | return -ENOMEM; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 516 | |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 517 | kref_init(&req->kref); |
| 518 | req->buf = buf; |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 519 | req->buf_len = sizeof(*buf); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 520 | init_completion(&req->completion); |
| 521 | |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 522 | err = -ENOMEM; |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 523 | req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 524 | if (!req->request) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 525 | goto out; |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 526 | req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 527 | if (!req->reply) |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 528 | goto out; |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 529 | |
| 530 | /* fill out request */ |
| 531 | h = req->request->front.iov_base; |
| 532 | h->monhdr.have_version = 0; |
| 533 | h->monhdr.session_mon = cpu_to_le16(-1); |
| 534 | h->monhdr.session_mon_tid = 0; |
| 535 | h->fsid = monc->monmap->fsid; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 536 | |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 537 | err = do_generic_request(monc, req); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 538 | |
| 539 | out: |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 540 | kref_put(&req->kref, release_generic_request); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 541 | return err; |
| 542 | } |
| 543 | |
| 544 | /* |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 545 | * pool ops |
| 546 | */ |
| 547 | static int get_poolop_reply_buf(const char *src, size_t src_len, |
| 548 | char *dst, size_t dst_len) |
| 549 | { |
| 550 | u32 buf_len; |
| 551 | |
| 552 | if (src_len != sizeof(u32) + dst_len) |
| 553 | return -EINVAL; |
| 554 | |
| 555 | buf_len = le32_to_cpu(*(u32 *)src); |
| 556 | if (buf_len != dst_len) |
| 557 | return -EINVAL; |
| 558 | |
| 559 | memcpy(dst, src + sizeof(u32), dst_len); |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static void handle_poolop_reply(struct ceph_mon_client *monc, |
| 564 | struct ceph_msg *msg) |
| 565 | { |
| 566 | struct ceph_mon_generic_request *req; |
| 567 | struct ceph_mon_poolop_reply *reply = msg->front.iov_base; |
| 568 | u64 tid = le64_to_cpu(msg->hdr.tid); |
| 569 | |
| 570 | if (msg->front.iov_len < sizeof(*reply)) |
| 571 | goto bad; |
| 572 | dout("handle_poolop_reply %p tid %llu\n", msg, tid); |
| 573 | |
| 574 | mutex_lock(&monc->mutex); |
| 575 | req = __lookup_generic_req(monc, tid); |
| 576 | if (req) { |
| 577 | if (req->buf_len && |
| 578 | get_poolop_reply_buf(msg->front.iov_base + sizeof(*reply), |
| 579 | msg->front.iov_len - sizeof(*reply), |
| 580 | req->buf, req->buf_len) < 0) { |
| 581 | mutex_unlock(&monc->mutex); |
| 582 | goto bad; |
| 583 | } |
| 584 | req->result = le32_to_cpu(reply->reply_code); |
| 585 | get_generic_request(req); |
| 586 | } |
| 587 | mutex_unlock(&monc->mutex); |
| 588 | if (req) { |
| 589 | complete(&req->completion); |
| 590 | put_generic_request(req); |
| 591 | } |
| 592 | return; |
| 593 | |
| 594 | bad: |
| 595 | pr_err("corrupt generic reply, tid %llu\n", tid); |
| 596 | ceph_msg_dump(msg); |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Do a synchronous pool op. |
| 601 | */ |
| 602 | int ceph_monc_do_poolop(struct ceph_mon_client *monc, u32 op, |
| 603 | u32 pool, u64 snapid, |
| 604 | char *buf, int len) |
| 605 | { |
| 606 | struct ceph_mon_generic_request *req; |
| 607 | struct ceph_mon_poolop *h; |
| 608 | int err; |
| 609 | |
| 610 | req = kzalloc(sizeof(*req), GFP_NOFS); |
| 611 | if (!req) |
| 612 | return -ENOMEM; |
| 613 | |
| 614 | kref_init(&req->kref); |
| 615 | req->buf = buf; |
| 616 | req->buf_len = len; |
| 617 | init_completion(&req->completion); |
| 618 | |
| 619 | err = -ENOMEM; |
| 620 | req->request = ceph_msg_new(CEPH_MSG_POOLOP, sizeof(*h), GFP_NOFS); |
| 621 | if (!req->request) |
| 622 | goto out; |
| 623 | req->reply = ceph_msg_new(CEPH_MSG_POOLOP_REPLY, 1024, GFP_NOFS); |
| 624 | if (!req->reply) |
| 625 | goto out; |
| 626 | |
| 627 | /* fill out request */ |
| 628 | req->request->hdr.version = cpu_to_le16(2); |
| 629 | h = req->request->front.iov_base; |
| 630 | h->monhdr.have_version = 0; |
| 631 | h->monhdr.session_mon = cpu_to_le16(-1); |
| 632 | h->monhdr.session_mon_tid = 0; |
| 633 | h->fsid = monc->monmap->fsid; |
| 634 | h->pool = cpu_to_le32(pool); |
| 635 | h->op = cpu_to_le32(op); |
| 636 | h->auid = 0; |
| 637 | h->snapid = cpu_to_le64(snapid); |
| 638 | h->name_len = 0; |
| 639 | |
| 640 | err = do_generic_request(monc, req); |
| 641 | |
| 642 | out: |
| 643 | kref_put(&req->kref, release_generic_request); |
| 644 | return err; |
| 645 | } |
| 646 | |
| 647 | int ceph_monc_create_snapid(struct ceph_mon_client *monc, |
| 648 | u32 pool, u64 *snapid) |
| 649 | { |
| 650 | return ceph_monc_do_poolop(monc, POOL_OP_CREATE_UNMANAGED_SNAP, |
| 651 | pool, 0, (char *)snapid, sizeof(*snapid)); |
| 652 | |
| 653 | } |
| 654 | |
| 655 | int ceph_monc_delete_snapid(struct ceph_mon_client *monc, |
| 656 | u32 pool, u64 snapid) |
| 657 | { |
| 658 | return ceph_monc_do_poolop(monc, POOL_OP_CREATE_UNMANAGED_SNAP, |
| 659 | pool, snapid, 0, 0); |
| 660 | |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Resend pending generic requests. |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 665 | */ |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 666 | static void __resend_generic_request(struct ceph_mon_client *monc) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 667 | { |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 668 | struct ceph_mon_generic_request *req; |
Sage Weil | 85ff03f | 2010-02-15 14:47:28 -0800 | [diff] [blame] | 669 | struct rb_node *p; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 670 | |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 671 | for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) { |
| 672 | req = rb_entry(p, struct ceph_mon_generic_request, node); |
Sage Weil | 9706900 | 2010-05-21 12:31:49 -0700 | [diff] [blame] | 673 | ceph_con_revoke(monc->con, req->request); |
Sage Weil | 3143edd | 2010-03-24 21:43:33 -0700 | [diff] [blame] | 674 | ceph_con_send(monc->con, ceph_msg_get(req->request)); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Delayed work. If we haven't mounted yet, retry. Otherwise, |
| 680 | * renew/retry subscription as needed (in case it is timing out, or we |
| 681 | * got an ENOMEM). And keep the monitor connection alive. |
| 682 | */ |
| 683 | static void delayed_work(struct work_struct *work) |
| 684 | { |
| 685 | struct ceph_mon_client *monc = |
| 686 | container_of(work, struct ceph_mon_client, delayed_work.work); |
| 687 | |
| 688 | dout("monc delayed_work\n"); |
| 689 | mutex_lock(&monc->mutex); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 690 | if (monc->hunting) { |
| 691 | __close_session(monc); |
| 692 | __open_session(monc); /* continue hunting */ |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 693 | } else { |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 694 | ceph_con_keepalive(monc->con); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 695 | |
| 696 | __validate_auth(monc); |
| 697 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 698 | if (monc->auth->ops->is_authenticated(monc->auth)) |
| 699 | __send_subscribe(monc); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 700 | } |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 701 | __schedule_delayed(monc); |
| 702 | mutex_unlock(&monc->mutex); |
| 703 | } |
| 704 | |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 705 | /* |
| 706 | * On startup, we build a temporary monmap populated with the IPs |
| 707 | * provided by mount(2). |
| 708 | */ |
| 709 | static int build_initial_monmap(struct ceph_mon_client *monc) |
| 710 | { |
| 711 | struct ceph_mount_args *args = monc->client->mount_args; |
| 712 | struct ceph_entity_addr *mon_addr = args->mon_addr; |
| 713 | int num_mon = args->num_mon; |
| 714 | int i; |
| 715 | |
| 716 | /* build initial monmap */ |
| 717 | monc->monmap = kzalloc(sizeof(*monc->monmap) + |
| 718 | num_mon*sizeof(monc->monmap->mon_inst[0]), |
| 719 | GFP_KERNEL); |
| 720 | if (!monc->monmap) |
| 721 | return -ENOMEM; |
| 722 | for (i = 0; i < num_mon; i++) { |
| 723 | monc->monmap->mon_inst[i].addr = mon_addr[i]; |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 724 | monc->monmap->mon_inst[i].addr.nonce = 0; |
| 725 | monc->monmap->mon_inst[i].name.type = |
| 726 | CEPH_ENTITY_TYPE_MON; |
| 727 | monc->monmap->mon_inst[i].name.num = cpu_to_le64(i); |
| 728 | } |
| 729 | monc->monmap->num_mon = num_mon; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 730 | monc->have_fsid = false; |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 731 | |
| 732 | /* release addr memory */ |
| 733 | kfree(args->mon_addr); |
| 734 | args->mon_addr = NULL; |
| 735 | args->num_mon = 0; |
| 736 | return 0; |
| 737 | } |
| 738 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 739 | int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl) |
| 740 | { |
| 741 | int err = 0; |
| 742 | |
| 743 | dout("init\n"); |
| 744 | memset(monc, 0, sizeof(*monc)); |
| 745 | monc->client = cl; |
| 746 | monc->monmap = NULL; |
| 747 | mutex_init(&monc->mutex); |
| 748 | |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 749 | err = build_initial_monmap(monc); |
| 750 | if (err) |
| 751 | goto out; |
| 752 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 753 | monc->con = NULL; |
| 754 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 755 | /* authentication */ |
| 756 | monc->auth = ceph_auth_init(cl->mount_args->name, |
| 757 | cl->mount_args->secret); |
| 758 | if (IS_ERR(monc->auth)) |
| 759 | return PTR_ERR(monc->auth); |
| 760 | monc->auth->want_keys = |
| 761 | CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON | |
| 762 | CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS; |
| 763 | |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 764 | /* msgs */ |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 765 | err = -ENOMEM; |
Sage Weil | 7c315c5 | 2010-03-24 21:52:30 -0700 | [diff] [blame] | 766 | monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK, |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 767 | sizeof(struct ceph_mon_subscribe_ack), |
| 768 | GFP_NOFS); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 769 | if (!monc->m_subscribe_ack) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 770 | goto out_monmap; |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 771 | |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 772 | monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS); |
| 773 | if (!monc->m_subscribe) |
| 774 | goto out_subscribe_ack; |
| 775 | |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 776 | monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 777 | if (!monc->m_auth_reply) |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 778 | goto out_subscribe; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 779 | |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 780 | monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 781 | monc->pending_auth = 0; |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 782 | if (!monc->m_auth) |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 783 | goto out_auth_reply; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 784 | |
| 785 | monc->cur_mon = -1; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 786 | monc->hunting = true; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 787 | monc->sub_renew_after = jiffies; |
| 788 | monc->sub_sent = 0; |
| 789 | |
| 790 | INIT_DELAYED_WORK(&monc->delayed_work, delayed_work); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 791 | monc->generic_request_tree = RB_ROOT; |
| 792 | monc->num_generic_requests = 0; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 793 | monc->last_tid = 0; |
| 794 | |
| 795 | monc->have_mdsmap = 0; |
| 796 | monc->have_osdmap = 0; |
| 797 | monc->want_next_osdmap = 1; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 798 | return 0; |
| 799 | |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 800 | out_auth_reply: |
| 801 | ceph_msg_put(monc->m_auth_reply); |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 802 | out_subscribe: |
| 803 | ceph_msg_put(monc->m_subscribe); |
Sage Weil | 7c315c5 | 2010-03-24 21:52:30 -0700 | [diff] [blame] | 804 | out_subscribe_ack: |
| 805 | ceph_msg_put(monc->m_subscribe_ack); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 806 | out_monmap: |
| 807 | kfree(monc->monmap); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 808 | out: |
| 809 | return err; |
| 810 | } |
| 811 | |
| 812 | void ceph_monc_stop(struct ceph_mon_client *monc) |
| 813 | { |
| 814 | dout("stop\n"); |
| 815 | cancel_delayed_work_sync(&monc->delayed_work); |
| 816 | |
| 817 | mutex_lock(&monc->mutex); |
| 818 | __close_session(monc); |
| 819 | if (monc->con) { |
| 820 | monc->con->private = NULL; |
| 821 | monc->con->ops->put(monc->con); |
| 822 | monc->con = NULL; |
| 823 | } |
| 824 | mutex_unlock(&monc->mutex); |
| 825 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 826 | ceph_auth_destroy(monc->auth); |
| 827 | |
| 828 | ceph_msg_put(monc->m_auth); |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 829 | ceph_msg_put(monc->m_auth_reply); |
Sage Weil | 240ed68 | 2010-05-21 14:57:25 -0700 | [diff] [blame] | 830 | ceph_msg_put(monc->m_subscribe); |
Sage Weil | 7c315c5 | 2010-03-24 21:52:30 -0700 | [diff] [blame] | 831 | ceph_msg_put(monc->m_subscribe_ack); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 832 | |
| 833 | kfree(monc->monmap); |
| 834 | } |
| 835 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 836 | static void handle_auth_reply(struct ceph_mon_client *monc, |
| 837 | struct ceph_msg *msg) |
| 838 | { |
| 839 | int ret; |
Sage Weil | 09c4d6a | 2010-05-25 15:38:06 -0700 | [diff] [blame] | 840 | int was_auth = 0; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 841 | |
| 842 | mutex_lock(&monc->mutex); |
Sage Weil | 09c4d6a | 2010-05-25 15:38:06 -0700 | [diff] [blame] | 843 | if (monc->auth->ops) |
| 844 | was_auth = monc->auth->ops->is_authenticated(monc->auth); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 845 | monc->pending_auth = 0; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 846 | ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base, |
| 847 | msg->front.iov_len, |
| 848 | monc->m_auth->front.iov_base, |
| 849 | monc->m_auth->front_max); |
| 850 | if (ret < 0) { |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 851 | monc->client->auth_err = ret; |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 852 | wake_up_all(&monc->client->auth_wq); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 853 | } else if (ret > 0) { |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 854 | __send_prepared_auth_request(monc, ret); |
Sage Weil | 09c4d6a | 2010-05-25 15:38:06 -0700 | [diff] [blame] | 855 | } else if (!was_auth && monc->auth->ops->is_authenticated(monc->auth)) { |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 856 | dout("authenticated, starting session\n"); |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 857 | |
| 858 | monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT; |
Yehuda Sadeh | 0cf5537 | 2010-06-11 15:57:06 -0700 | [diff] [blame] | 859 | monc->client->msgr->inst.name.num = |
| 860 | cpu_to_le64(monc->auth->global_id); |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 861 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 862 | __send_subscribe(monc); |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 863 | __resend_generic_request(monc); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 864 | } |
| 865 | mutex_unlock(&monc->mutex); |
| 866 | } |
| 867 | |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 868 | static int __validate_auth(struct ceph_mon_client *monc) |
| 869 | { |
| 870 | int ret; |
| 871 | |
| 872 | if (monc->pending_auth) |
| 873 | return 0; |
| 874 | |
| 875 | ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base, |
| 876 | monc->m_auth->front_max); |
| 877 | if (ret <= 0) |
| 878 | return ret; /* either an error, or no need to authenticate */ |
| 879 | __send_prepared_auth_request(monc, ret); |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | int ceph_monc_validate_auth(struct ceph_mon_client *monc) |
| 884 | { |
| 885 | int ret; |
| 886 | |
| 887 | mutex_lock(&monc->mutex); |
| 888 | ret = __validate_auth(monc); |
| 889 | mutex_unlock(&monc->mutex); |
| 890 | return ret; |
| 891 | } |
| 892 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 893 | /* |
| 894 | * handle incoming message |
| 895 | */ |
| 896 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) |
| 897 | { |
| 898 | struct ceph_mon_client *monc = con->private; |
| 899 | int type = le16_to_cpu(msg->hdr.type); |
| 900 | |
| 901 | if (!monc) |
| 902 | return; |
| 903 | |
| 904 | switch (type) { |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 905 | case CEPH_MSG_AUTH_REPLY: |
| 906 | handle_auth_reply(monc, msg); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 907 | break; |
| 908 | |
| 909 | case CEPH_MSG_MON_SUBSCRIBE_ACK: |
| 910 | handle_subscribe_ack(monc, msg); |
| 911 | break; |
| 912 | |
| 913 | case CEPH_MSG_STATFS_REPLY: |
| 914 | handle_statfs_reply(monc, msg); |
| 915 | break; |
| 916 | |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 917 | case CEPH_MSG_POOLOP_REPLY: |
| 918 | handle_poolop_reply(monc, msg); |
| 919 | break; |
| 920 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 921 | case CEPH_MSG_MON_MAP: |
| 922 | ceph_monc_handle_map(monc, msg); |
| 923 | break; |
| 924 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 925 | case CEPH_MSG_MDS_MAP: |
| 926 | ceph_mdsc_handle_map(&monc->client->mdsc, msg); |
| 927 | break; |
| 928 | |
| 929 | case CEPH_MSG_OSD_MAP: |
| 930 | ceph_osdc_handle_map(&monc->client->osdc, msg); |
| 931 | break; |
| 932 | |
| 933 | default: |
| 934 | pr_err("received unknown message type %d %s\n", type, |
| 935 | ceph_msg_type_name(type)); |
| 936 | } |
| 937 | ceph_msg_put(msg); |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Allocate memory for incoming message |
| 942 | */ |
| 943 | static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con, |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 944 | struct ceph_msg_header *hdr, |
| 945 | int *skip) |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 946 | { |
| 947 | struct ceph_mon_client *monc = con->private; |
| 948 | int type = le16_to_cpu(hdr->type); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 949 | int front_len = le32_to_cpu(hdr->front_len); |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 950 | struct ceph_msg *m = NULL; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 951 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 952 | *skip = 0; |
Yehuda Sadeh | 0547a9b | 2010-01-11 14:47:13 -0800 | [diff] [blame] | 953 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 954 | switch (type) { |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 955 | case CEPH_MSG_MON_SUBSCRIBE_ACK: |
Sage Weil | 7c315c5 | 2010-03-24 21:52:30 -0700 | [diff] [blame] | 956 | m = ceph_msg_get(monc->m_subscribe_ack); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 957 | break; |
Yehuda Sadeh | e56fa10 | 2010-05-17 12:40:28 -0700 | [diff] [blame] | 958 | case CEPH_MSG_POOLOP_REPLY: |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 959 | case CEPH_MSG_STATFS_REPLY: |
Yehuda Sadeh | f8c76f6 | 2010-04-22 15:40:37 -0700 | [diff] [blame] | 960 | return get_generic_reply(con, hdr, skip); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 961 | case CEPH_MSG_AUTH_REPLY: |
Sage Weil | 6694d6b | 2010-03-24 21:48:05 -0700 | [diff] [blame] | 962 | m = ceph_msg_get(monc->m_auth_reply); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 963 | break; |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 964 | case CEPH_MSG_MON_MAP: |
| 965 | case CEPH_MSG_MDS_MAP: |
| 966 | case CEPH_MSG_OSD_MAP: |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 967 | m = ceph_msg_new(type, front_len, GFP_NOFS); |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 968 | break; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 969 | } |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 970 | |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 971 | if (!m) { |
| 972 | pr_info("alloc_msg unknown type %d\n", type); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 973 | *skip = 1; |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 974 | } |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 975 | return m; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /* |
| 979 | * If the monitor connection resets, pick a new monitor and resubmit |
| 980 | * any pending requests. |
| 981 | */ |
| 982 | static void mon_fault(struct ceph_connection *con) |
| 983 | { |
| 984 | struct ceph_mon_client *monc = con->private; |
| 985 | |
| 986 | if (!monc) |
| 987 | return; |
| 988 | |
| 989 | dout("mon_fault\n"); |
| 990 | mutex_lock(&monc->mutex); |
| 991 | if (!con->private) |
| 992 | goto out; |
| 993 | |
| 994 | if (monc->con && !monc->hunting) |
| 995 | pr_info("mon%d %s session lost, " |
| 996 | "hunting for new mon\n", monc->cur_mon, |
| 997 | pr_addr(&monc->con->peer_addr.in_addr)); |
| 998 | |
| 999 | __close_session(monc); |
| 1000 | if (!monc->hunting) { |
| 1001 | /* start hunting */ |
| 1002 | monc->hunting = true; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1003 | __open_session(monc); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1004 | } else { |
| 1005 | /* already hunting, let's wait a bit */ |
| 1006 | __schedule_delayed(monc); |
| 1007 | } |
| 1008 | out: |
| 1009 | mutex_unlock(&monc->mutex); |
| 1010 | } |
| 1011 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 1012 | static const struct ceph_connection_operations mon_con_ops = { |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1013 | .get = ceph_con_get, |
| 1014 | .put = ceph_con_put, |
| 1015 | .dispatch = dispatch, |
| 1016 | .fault = mon_fault, |
| 1017 | .alloc_msg = mon_alloc_msg, |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1018 | }; |