Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1 | #include "ceph_debug.h" |
| 2 | |
| 3 | #include <linux/types.h> |
| 4 | #include <linux/random.h> |
| 5 | #include <linux/sched.h> |
| 6 | |
| 7 | #include "mon_client.h" |
| 8 | #include "super.h" |
| 9 | #include "decode.h" |
| 10 | |
| 11 | /* |
| 12 | * Interact with Ceph monitor cluster. Handle requests for new map |
| 13 | * versions, and periodically resend as needed. Also implement |
| 14 | * statfs() and umount(). |
| 15 | * |
| 16 | * A small cluster of Ceph "monitors" are responsible for managing critical |
| 17 | * cluster configuration and state information. An odd number (e.g., 3, 5) |
| 18 | * of cmon daemons use a modified version of the Paxos part-time parliament |
| 19 | * algorithm to manage the MDS map (mds cluster membership), OSD map, and |
| 20 | * list of clients who have mounted the file system. |
| 21 | * |
| 22 | * We maintain an open, active session with a monitor at all times in order to |
| 23 | * receive timely MDSMap updates. We periodically send a keepalive byte on the |
| 24 | * TCP socket to ensure we detect a failure. If the connection does break, we |
| 25 | * randomly hunt for a new monitor. Once the connection is reestablished, we |
| 26 | * resend any outstanding requests. |
| 27 | */ |
| 28 | |
| 29 | const static struct ceph_connection_operations mon_con_ops; |
| 30 | |
| 31 | /* |
| 32 | * Decode a monmap blob (e.g., during mount). |
| 33 | */ |
| 34 | struct ceph_monmap *ceph_monmap_decode(void *p, void *end) |
| 35 | { |
| 36 | struct ceph_monmap *m = NULL; |
| 37 | int i, err = -EINVAL; |
| 38 | struct ceph_fsid fsid; |
| 39 | u32 epoch, num_mon; |
| 40 | u16 version; |
| 41 | |
| 42 | dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p)); |
| 43 | |
| 44 | ceph_decode_16_safe(&p, end, version, bad); |
| 45 | |
| 46 | ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad); |
| 47 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 48 | epoch = ceph_decode_32(&p); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 49 | |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 50 | num_mon = ceph_decode_32(&p); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 51 | ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad); |
| 52 | |
| 53 | if (num_mon >= CEPH_MAX_MON) |
| 54 | goto bad; |
| 55 | m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS); |
| 56 | if (m == NULL) |
| 57 | return ERR_PTR(-ENOMEM); |
| 58 | m->fsid = fsid; |
| 59 | m->epoch = epoch; |
| 60 | m->num_mon = num_mon; |
| 61 | ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0])); |
| 62 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 63 | dout("monmap_decode epoch %d, num_mon %d\n", m->epoch, |
| 64 | m->num_mon); |
| 65 | for (i = 0; i < m->num_mon; i++) |
| 66 | dout("monmap_decode mon%d is %s\n", i, |
| 67 | pr_addr(&m->mon_inst[i].addr.in_addr)); |
| 68 | return m; |
| 69 | |
| 70 | bad: |
| 71 | dout("monmap_decode failed with %d\n", err); |
| 72 | kfree(m); |
| 73 | return ERR_PTR(err); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * return true if *addr is included in the monmap. |
| 78 | */ |
| 79 | int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr) |
| 80 | { |
| 81 | int i; |
| 82 | |
| 83 | for (i = 0; i < m->num_mon; i++) |
| 84 | if (ceph_entity_addr_equal(addr, &m->mon_inst[i].addr)) |
| 85 | return 1; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Close monitor session, if any. |
| 91 | */ |
| 92 | static void __close_session(struct ceph_mon_client *monc) |
| 93 | { |
| 94 | if (monc->con) { |
| 95 | dout("__close_session closing mon%d\n", monc->cur_mon); |
| 96 | ceph_con_close(monc->con); |
| 97 | monc->cur_mon = -1; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Open a session with a (new) monitor. |
| 103 | */ |
| 104 | static int __open_session(struct ceph_mon_client *monc) |
| 105 | { |
| 106 | char r; |
| 107 | |
| 108 | if (monc->cur_mon < 0) { |
| 109 | get_random_bytes(&r, 1); |
| 110 | monc->cur_mon = r % monc->monmap->num_mon; |
| 111 | dout("open_session num=%d r=%d -> mon%d\n", |
| 112 | monc->monmap->num_mon, r, monc->cur_mon); |
| 113 | monc->sub_sent = 0; |
| 114 | monc->sub_renew_after = jiffies; /* i.e., expired */ |
| 115 | monc->want_next_osdmap = !!monc->want_next_osdmap; |
| 116 | |
| 117 | dout("open_session mon%d opening\n", monc->cur_mon); |
| 118 | monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON; |
| 119 | monc->con->peer_name.num = cpu_to_le64(monc->cur_mon); |
| 120 | ceph_con_open(monc->con, |
| 121 | &monc->monmap->mon_inst[monc->cur_mon].addr); |
| 122 | } else { |
| 123 | dout("open_session mon%d already open\n", monc->cur_mon); |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static bool __sub_expired(struct ceph_mon_client *monc) |
| 129 | { |
| 130 | return time_after_eq(jiffies, monc->sub_renew_after); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Reschedule delayed work timer. |
| 135 | */ |
| 136 | static void __schedule_delayed(struct ceph_mon_client *monc) |
| 137 | { |
| 138 | unsigned delay; |
| 139 | |
| 140 | if (monc->cur_mon < 0 || monc->want_mount || __sub_expired(monc)) |
| 141 | delay = 10 * HZ; |
| 142 | else |
| 143 | delay = 20 * HZ; |
| 144 | dout("__schedule_delayed after %u\n", delay); |
| 145 | schedule_delayed_work(&monc->delayed_work, delay); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Send subscribe request for mdsmap and/or osdmap. |
| 150 | */ |
| 151 | static void __send_subscribe(struct ceph_mon_client *monc) |
| 152 | { |
| 153 | dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n", |
| 154 | (unsigned)monc->sub_sent, __sub_expired(monc), |
| 155 | monc->want_next_osdmap); |
| 156 | if ((__sub_expired(monc) && !monc->sub_sent) || |
| 157 | monc->want_next_osdmap == 1) { |
| 158 | struct ceph_msg *msg; |
| 159 | struct ceph_mon_subscribe_item *i; |
| 160 | void *p, *end; |
| 161 | |
| 162 | msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 64, 0, 0, NULL); |
| 163 | if (!msg) |
| 164 | return; |
| 165 | |
| 166 | p = msg->front.iov_base; |
| 167 | end = p + msg->front.iov_len; |
| 168 | |
| 169 | dout("__send_subscribe to 'mdsmap' %u+\n", |
| 170 | (unsigned)monc->have_mdsmap); |
| 171 | if (monc->want_next_osdmap) { |
| 172 | dout("__send_subscribe to 'osdmap' %u\n", |
| 173 | (unsigned)monc->have_osdmap); |
| 174 | ceph_encode_32(&p, 2); |
| 175 | ceph_encode_string(&p, end, "osdmap", 6); |
| 176 | i = p; |
| 177 | i->have = cpu_to_le64(monc->have_osdmap); |
| 178 | i->onetime = 1; |
| 179 | p += sizeof(*i); |
| 180 | monc->want_next_osdmap = 2; /* requested */ |
| 181 | } else { |
| 182 | ceph_encode_32(&p, 1); |
| 183 | } |
| 184 | ceph_encode_string(&p, end, "mdsmap", 6); |
| 185 | i = p; |
| 186 | i->have = cpu_to_le64(monc->have_mdsmap); |
| 187 | i->onetime = 0; |
| 188 | p += sizeof(*i); |
| 189 | |
| 190 | msg->front.iov_len = p - msg->front.iov_base; |
| 191 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 192 | ceph_con_send(monc->con, msg); |
| 193 | |
| 194 | monc->sub_sent = jiffies | 1; /* never 0 */ |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static void handle_subscribe_ack(struct ceph_mon_client *monc, |
| 199 | struct ceph_msg *msg) |
| 200 | { |
| 201 | unsigned seconds; |
Sage Weil | 07bd10f | 2009-10-14 17:26:40 -0700 | [diff] [blame] | 202 | struct ceph_mon_subscribe_ack *h = msg->front.iov_base; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 203 | |
Sage Weil | 07bd10f | 2009-10-14 17:26:40 -0700 | [diff] [blame] | 204 | if (msg->front.iov_len < sizeof(*h)) |
| 205 | goto bad; |
| 206 | seconds = le32_to_cpu(h->duration); |
| 207 | |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 208 | mutex_lock(&monc->mutex); |
| 209 | if (monc->hunting) { |
| 210 | pr_info("mon%d %s session established\n", |
| 211 | monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr)); |
| 212 | monc->hunting = false; |
| 213 | } |
| 214 | dout("handle_subscribe_ack after %d seconds\n", seconds); |
Sage Weil | 0656d11 | 2009-10-08 10:25:46 -0700 | [diff] [blame] | 215 | monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 216 | monc->sub_sent = 0; |
| 217 | mutex_unlock(&monc->mutex); |
| 218 | return; |
| 219 | bad: |
| 220 | pr_err("got corrupt subscribe-ack msg\n"); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Keep track of which maps we have |
| 225 | */ |
| 226 | int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got) |
| 227 | { |
| 228 | mutex_lock(&monc->mutex); |
| 229 | monc->have_mdsmap = got; |
| 230 | mutex_unlock(&monc->mutex); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got) |
| 235 | { |
| 236 | mutex_lock(&monc->mutex); |
| 237 | monc->have_osdmap = got; |
| 238 | monc->want_next_osdmap = 0; |
| 239 | mutex_unlock(&monc->mutex); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Register interest in the next osdmap |
| 245 | */ |
| 246 | void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc) |
| 247 | { |
| 248 | dout("request_next_osdmap have %u\n", monc->have_osdmap); |
| 249 | mutex_lock(&monc->mutex); |
| 250 | if (!monc->want_next_osdmap) |
| 251 | monc->want_next_osdmap = 1; |
| 252 | if (monc->want_next_osdmap < 2) |
| 253 | __send_subscribe(monc); |
| 254 | mutex_unlock(&monc->mutex); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /* |
| 259 | * mount |
| 260 | */ |
| 261 | static void __request_mount(struct ceph_mon_client *monc) |
| 262 | { |
| 263 | struct ceph_msg *msg; |
| 264 | struct ceph_client_mount *h; |
| 265 | int err; |
| 266 | |
| 267 | dout("__request_mount\n"); |
| 268 | err = __open_session(monc); |
| 269 | if (err) |
| 270 | return; |
| 271 | msg = ceph_msg_new(CEPH_MSG_CLIENT_MOUNT, sizeof(*h), 0, 0, NULL); |
| 272 | if (IS_ERR(msg)) |
| 273 | return; |
| 274 | h = msg->front.iov_base; |
Sage Weil | 13e38c8 | 2009-10-09 16:36:34 -0700 | [diff] [blame] | 275 | h->monhdr.have_version = 0; |
| 276 | h->monhdr.session_mon = cpu_to_le16(-1); |
| 277 | h->monhdr.session_mon_tid = 0; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 278 | ceph_con_send(monc->con, msg); |
| 279 | } |
| 280 | |
| 281 | int ceph_monc_request_mount(struct ceph_mon_client *monc) |
| 282 | { |
| 283 | if (!monc->con) { |
| 284 | monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL); |
| 285 | if (!monc->con) |
| 286 | return -ENOMEM; |
| 287 | ceph_con_init(monc->client->msgr, monc->con); |
| 288 | monc->con->private = monc; |
| 289 | monc->con->ops = &mon_con_ops; |
| 290 | } |
| 291 | |
| 292 | mutex_lock(&monc->mutex); |
| 293 | __request_mount(monc); |
| 294 | __schedule_delayed(monc); |
| 295 | mutex_unlock(&monc->mutex); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * The monitor responds with mount ack indicate mount success. The |
| 301 | * included client ticket allows the client to talk to MDSs and OSDs. |
| 302 | */ |
| 303 | static void handle_mount_ack(struct ceph_mon_client *monc, struct ceph_msg *msg) |
| 304 | { |
| 305 | struct ceph_client *client = monc->client; |
| 306 | struct ceph_monmap *monmap = NULL, *old = monc->monmap; |
| 307 | void *p, *end; |
| 308 | s32 result; |
| 309 | u32 len; |
| 310 | s64 cnum; |
| 311 | int err = -EINVAL; |
| 312 | |
| 313 | if (client->whoami >= 0) { |
| 314 | dout("handle_mount_ack - already mounted\n"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | mutex_lock(&monc->mutex); |
| 319 | |
| 320 | dout("handle_mount_ack\n"); |
| 321 | p = msg->front.iov_base; |
| 322 | end = p + msg->front.iov_len; |
| 323 | |
| 324 | ceph_decode_64_safe(&p, end, cnum, bad); |
| 325 | ceph_decode_32_safe(&p, end, result, bad); |
| 326 | ceph_decode_32_safe(&p, end, len, bad); |
| 327 | if (result) { |
| 328 | pr_err("mount denied: %.*s (%d)\n", len, (char *)p, |
| 329 | result); |
| 330 | err = result; |
| 331 | goto out; |
| 332 | } |
| 333 | p += len; |
| 334 | |
| 335 | ceph_decode_32_safe(&p, end, len, bad); |
| 336 | ceph_decode_need(&p, end, len, bad); |
| 337 | monmap = ceph_monmap_decode(p, p + len); |
| 338 | if (IS_ERR(monmap)) { |
| 339 | pr_err("problem decoding monmap, %d\n", |
| 340 | (int)PTR_ERR(monmap)); |
| 341 | err = -EINVAL; |
| 342 | goto out; |
| 343 | } |
| 344 | p += len; |
| 345 | |
| 346 | client->monc.monmap = monmap; |
| 347 | kfree(old); |
| 348 | |
| 349 | client->signed_ticket = NULL; |
| 350 | client->signed_ticket_len = 0; |
| 351 | |
| 352 | monc->want_mount = false; |
| 353 | |
| 354 | client->whoami = cnum; |
| 355 | client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT; |
| 356 | client->msgr->inst.name.num = cpu_to_le64(cnum); |
| 357 | pr_info("client%lld fsid " FSID_FORMAT "\n", |
| 358 | client->whoami, PR_FSID(&client->monc.monmap->fsid)); |
| 359 | |
| 360 | ceph_debugfs_client_init(client); |
| 361 | __send_subscribe(monc); |
| 362 | |
| 363 | err = 0; |
| 364 | goto out; |
| 365 | |
| 366 | bad: |
| 367 | pr_err("error decoding mount_ack message\n"); |
| 368 | out: |
| 369 | client->mount_err = err; |
| 370 | mutex_unlock(&monc->mutex); |
| 371 | wake_up(&client->mount_wq); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | /* |
| 378 | * statfs |
| 379 | */ |
| 380 | static void handle_statfs_reply(struct ceph_mon_client *monc, |
| 381 | struct ceph_msg *msg) |
| 382 | { |
| 383 | struct ceph_mon_statfs_request *req; |
| 384 | struct ceph_mon_statfs_reply *reply = msg->front.iov_base; |
| 385 | u64 tid; |
| 386 | |
| 387 | if (msg->front.iov_len != sizeof(*reply)) |
| 388 | goto bad; |
| 389 | tid = le64_to_cpu(reply->tid); |
| 390 | dout("handle_statfs_reply %p tid %llu\n", msg, tid); |
| 391 | |
| 392 | mutex_lock(&monc->mutex); |
| 393 | req = radix_tree_lookup(&monc->statfs_request_tree, tid); |
| 394 | if (req) { |
| 395 | *req->buf = reply->st; |
| 396 | req->result = 0; |
| 397 | } |
| 398 | mutex_unlock(&monc->mutex); |
| 399 | if (req) |
| 400 | complete(&req->completion); |
| 401 | return; |
| 402 | |
| 403 | bad: |
| 404 | pr_err("corrupt statfs reply, no tid\n"); |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * (re)send a statfs request |
| 409 | */ |
| 410 | static int send_statfs(struct ceph_mon_client *monc, |
| 411 | struct ceph_mon_statfs_request *req) |
| 412 | { |
| 413 | struct ceph_msg *msg; |
| 414 | struct ceph_mon_statfs *h; |
| 415 | int err; |
| 416 | |
| 417 | dout("send_statfs tid %llu\n", req->tid); |
| 418 | err = __open_session(monc); |
| 419 | if (err) |
| 420 | return err; |
| 421 | msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL); |
| 422 | if (IS_ERR(msg)) |
| 423 | return PTR_ERR(msg); |
| 424 | req->request = msg; |
| 425 | h = msg->front.iov_base; |
Sage Weil | 13e38c8 | 2009-10-09 16:36:34 -0700 | [diff] [blame] | 426 | h->monhdr.have_version = 0; |
| 427 | h->monhdr.session_mon = cpu_to_le16(-1); |
| 428 | h->monhdr.session_mon_tid = 0; |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 429 | h->fsid = monc->monmap->fsid; |
| 430 | h->tid = cpu_to_le64(req->tid); |
| 431 | ceph_con_send(monc->con, msg); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Do a synchronous statfs(). |
| 437 | */ |
| 438 | int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf) |
| 439 | { |
| 440 | struct ceph_mon_statfs_request req; |
| 441 | int err; |
| 442 | |
| 443 | req.buf = buf; |
| 444 | init_completion(&req.completion); |
| 445 | |
| 446 | /* allocate memory for reply */ |
| 447 | err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1); |
| 448 | if (err) |
| 449 | return err; |
| 450 | |
| 451 | /* register request */ |
| 452 | mutex_lock(&monc->mutex); |
| 453 | req.tid = ++monc->last_tid; |
| 454 | req.last_attempt = jiffies; |
| 455 | req.delay = BASE_DELAY_INTERVAL; |
| 456 | if (radix_tree_insert(&monc->statfs_request_tree, req.tid, &req) < 0) { |
| 457 | mutex_unlock(&monc->mutex); |
| 458 | pr_err("ENOMEM in do_statfs\n"); |
| 459 | return -ENOMEM; |
| 460 | } |
| 461 | monc->num_statfs_requests++; |
| 462 | mutex_unlock(&monc->mutex); |
| 463 | |
| 464 | /* send request and wait */ |
| 465 | err = send_statfs(monc, &req); |
| 466 | if (!err) |
| 467 | err = wait_for_completion_interruptible(&req.completion); |
| 468 | |
| 469 | mutex_lock(&monc->mutex); |
| 470 | radix_tree_delete(&monc->statfs_request_tree, req.tid); |
| 471 | monc->num_statfs_requests--; |
| 472 | ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1); |
| 473 | mutex_unlock(&monc->mutex); |
| 474 | |
| 475 | if (!err) |
| 476 | err = req.result; |
| 477 | return err; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Resend pending statfs requests. |
| 482 | */ |
| 483 | static void __resend_statfs(struct ceph_mon_client *monc) |
| 484 | { |
| 485 | u64 next_tid = 0; |
| 486 | int got; |
| 487 | int did = 0; |
| 488 | struct ceph_mon_statfs_request *req; |
| 489 | |
| 490 | while (1) { |
| 491 | got = radix_tree_gang_lookup(&monc->statfs_request_tree, |
| 492 | (void **)&req, |
| 493 | next_tid, 1); |
| 494 | if (got == 0) |
| 495 | break; |
| 496 | did++; |
| 497 | next_tid = req->tid + 1; |
| 498 | |
| 499 | send_statfs(monc, req); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Delayed work. If we haven't mounted yet, retry. Otherwise, |
| 505 | * renew/retry subscription as needed (in case it is timing out, or we |
| 506 | * got an ENOMEM). And keep the monitor connection alive. |
| 507 | */ |
| 508 | static void delayed_work(struct work_struct *work) |
| 509 | { |
| 510 | struct ceph_mon_client *monc = |
| 511 | container_of(work, struct ceph_mon_client, delayed_work.work); |
| 512 | |
| 513 | dout("monc delayed_work\n"); |
| 514 | mutex_lock(&monc->mutex); |
| 515 | if (monc->want_mount) { |
| 516 | __request_mount(monc); |
| 517 | } else { |
Sage Weil | 0656d11 | 2009-10-08 10:25:46 -0700 | [diff] [blame] | 518 | if (monc->hunting) { |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 519 | __close_session(monc); |
| 520 | __open_session(monc); /* continue hunting */ |
| 521 | } else { |
| 522 | ceph_con_keepalive(monc->con); |
| 523 | } |
| 524 | } |
| 525 | __send_subscribe(monc); |
| 526 | __schedule_delayed(monc); |
| 527 | mutex_unlock(&monc->mutex); |
| 528 | } |
| 529 | |
| 530 | int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl) |
| 531 | { |
| 532 | int err = 0; |
| 533 | |
| 534 | dout("init\n"); |
| 535 | memset(monc, 0, sizeof(*monc)); |
| 536 | monc->client = cl; |
| 537 | monc->monmap = NULL; |
| 538 | mutex_init(&monc->mutex); |
| 539 | |
| 540 | monc->con = NULL; |
| 541 | |
| 542 | /* msg pools */ |
| 543 | err = ceph_msgpool_init(&monc->msgpool_mount_ack, 4096, 1, false); |
| 544 | if (err < 0) |
| 545 | goto out; |
Sage Weil | 07bd10f | 2009-10-14 17:26:40 -0700 | [diff] [blame] | 546 | err = ceph_msgpool_init(&monc->msgpool_subscribe_ack, |
| 547 | sizeof(struct ceph_mon_subscribe_ack), 1, false); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 548 | if (err < 0) |
| 549 | goto out; |
| 550 | err = ceph_msgpool_init(&monc->msgpool_statfs_reply, |
| 551 | sizeof(struct ceph_mon_statfs_reply), 0, false); |
| 552 | if (err < 0) |
| 553 | goto out; |
| 554 | |
| 555 | monc->cur_mon = -1; |
| 556 | monc->hunting = false; /* not really */ |
| 557 | monc->sub_renew_after = jiffies; |
| 558 | monc->sub_sent = 0; |
| 559 | |
| 560 | INIT_DELAYED_WORK(&monc->delayed_work, delayed_work); |
| 561 | INIT_RADIX_TREE(&monc->statfs_request_tree, GFP_NOFS); |
| 562 | monc->num_statfs_requests = 0; |
| 563 | monc->last_tid = 0; |
| 564 | |
| 565 | monc->have_mdsmap = 0; |
| 566 | monc->have_osdmap = 0; |
| 567 | monc->want_next_osdmap = 1; |
| 568 | monc->want_mount = true; |
| 569 | out: |
| 570 | return err; |
| 571 | } |
| 572 | |
| 573 | void ceph_monc_stop(struct ceph_mon_client *monc) |
| 574 | { |
| 575 | dout("stop\n"); |
| 576 | cancel_delayed_work_sync(&monc->delayed_work); |
| 577 | |
| 578 | mutex_lock(&monc->mutex); |
| 579 | __close_session(monc); |
| 580 | if (monc->con) { |
| 581 | monc->con->private = NULL; |
| 582 | monc->con->ops->put(monc->con); |
| 583 | monc->con = NULL; |
| 584 | } |
| 585 | mutex_unlock(&monc->mutex); |
| 586 | |
| 587 | ceph_msgpool_destroy(&monc->msgpool_mount_ack); |
| 588 | ceph_msgpool_destroy(&monc->msgpool_subscribe_ack); |
| 589 | ceph_msgpool_destroy(&monc->msgpool_statfs_reply); |
| 590 | |
| 591 | kfree(monc->monmap); |
| 592 | } |
| 593 | |
| 594 | |
| 595 | /* |
| 596 | * handle incoming message |
| 597 | */ |
| 598 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) |
| 599 | { |
| 600 | struct ceph_mon_client *monc = con->private; |
| 601 | int type = le16_to_cpu(msg->hdr.type); |
| 602 | |
| 603 | if (!monc) |
| 604 | return; |
| 605 | |
| 606 | switch (type) { |
| 607 | case CEPH_MSG_CLIENT_MOUNT_ACK: |
| 608 | handle_mount_ack(monc, msg); |
| 609 | break; |
| 610 | |
| 611 | case CEPH_MSG_MON_SUBSCRIBE_ACK: |
| 612 | handle_subscribe_ack(monc, msg); |
| 613 | break; |
| 614 | |
| 615 | case CEPH_MSG_STATFS_REPLY: |
| 616 | handle_statfs_reply(monc, msg); |
| 617 | break; |
| 618 | |
| 619 | case CEPH_MSG_MDS_MAP: |
| 620 | ceph_mdsc_handle_map(&monc->client->mdsc, msg); |
| 621 | break; |
| 622 | |
| 623 | case CEPH_MSG_OSD_MAP: |
| 624 | ceph_osdc_handle_map(&monc->client->osdc, msg); |
| 625 | break; |
| 626 | |
| 627 | default: |
| 628 | pr_err("received unknown message type %d %s\n", type, |
| 629 | ceph_msg_type_name(type)); |
| 630 | } |
| 631 | ceph_msg_put(msg); |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Allocate memory for incoming message |
| 636 | */ |
| 637 | static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con, |
| 638 | struct ceph_msg_header *hdr) |
| 639 | { |
| 640 | struct ceph_mon_client *monc = con->private; |
| 641 | int type = le16_to_cpu(hdr->type); |
Sage Weil | 8f3bc05 | 2009-10-14 17:36:07 -0700 | [diff] [blame^] | 642 | int front = le32_to_cpu(hdr->front_len); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 643 | |
| 644 | switch (type) { |
| 645 | case CEPH_MSG_CLIENT_MOUNT_ACK: |
Sage Weil | 8f3bc05 | 2009-10-14 17:36:07 -0700 | [diff] [blame^] | 646 | return ceph_msgpool_get(&monc->msgpool_mount_ack, front); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 647 | case CEPH_MSG_MON_SUBSCRIBE_ACK: |
Sage Weil | 8f3bc05 | 2009-10-14 17:36:07 -0700 | [diff] [blame^] | 648 | return ceph_msgpool_get(&monc->msgpool_subscribe_ack, front); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 649 | case CEPH_MSG_STATFS_REPLY: |
Sage Weil | 8f3bc05 | 2009-10-14 17:36:07 -0700 | [diff] [blame^] | 650 | return ceph_msgpool_get(&monc->msgpool_statfs_reply, front); |
Sage Weil | ba75bb9 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 651 | } |
| 652 | return ceph_alloc_msg(con, hdr); |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * If the monitor connection resets, pick a new monitor and resubmit |
| 657 | * any pending requests. |
| 658 | */ |
| 659 | static void mon_fault(struct ceph_connection *con) |
| 660 | { |
| 661 | struct ceph_mon_client *monc = con->private; |
| 662 | |
| 663 | if (!monc) |
| 664 | return; |
| 665 | |
| 666 | dout("mon_fault\n"); |
| 667 | mutex_lock(&monc->mutex); |
| 668 | if (!con->private) |
| 669 | goto out; |
| 670 | |
| 671 | if (monc->con && !monc->hunting) |
| 672 | pr_info("mon%d %s session lost, " |
| 673 | "hunting for new mon\n", monc->cur_mon, |
| 674 | pr_addr(&monc->con->peer_addr.in_addr)); |
| 675 | |
| 676 | __close_session(monc); |
| 677 | if (!monc->hunting) { |
| 678 | /* start hunting */ |
| 679 | monc->hunting = true; |
| 680 | if (__open_session(monc) == 0) { |
| 681 | __send_subscribe(monc); |
| 682 | __resend_statfs(monc); |
| 683 | } |
| 684 | } else { |
| 685 | /* already hunting, let's wait a bit */ |
| 686 | __schedule_delayed(monc); |
| 687 | } |
| 688 | out: |
| 689 | mutex_unlock(&monc->mutex); |
| 690 | } |
| 691 | |
| 692 | const static struct ceph_connection_operations mon_con_ops = { |
| 693 | .get = ceph_con_get, |
| 694 | .put = ceph_con_put, |
| 695 | .dispatch = dispatch, |
| 696 | .fault = mon_fault, |
| 697 | .alloc_msg = mon_alloc_msg, |
| 698 | .alloc_middle = ceph_alloc_middle, |
| 699 | }; |