ceph: include transaction id in ceph_msg_header (protocol change)
[safe/jmp/linux-2.6] / fs / ceph / mon_client.c
index 95b76e7..bb94006 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "mon_client.h"
 #include "super.h"
+#include "auth.h"
 #include "decode.h"
 
 /*
@@ -38,6 +39,10 @@ struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
        struct ceph_fsid fsid;
        u32 epoch, num_mon;
        u16 version;
+       u32 len;
+
+       ceph_decode_32_safe(&p, end, len, bad);
+       ceph_decode_need(&p, end, len, bad);
 
        dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
 
@@ -95,8 +100,10 @@ static void __close_session(struct ceph_mon_client *monc)
 {
        if (monc->con) {
                dout("__close_session closing mon%d\n", monc->cur_mon);
+               ceph_con_revoke(monc->con, monc->m_auth);
                ceph_con_close(monc->con);
                monc->cur_mon = -1;
+               ceph_auth_reset(monc->auth);
        }
 }
 
@@ -106,6 +113,7 @@ static void __close_session(struct ceph_mon_client *monc)
 static int __open_session(struct ceph_mon_client *monc)
 {
        char r;
+       int ret;
 
        if (monc->cur_mon < 0) {
                get_random_bytes(&r, 1);
@@ -121,6 +129,15 @@ static int __open_session(struct ceph_mon_client *monc)
                monc->con->peer_name.num = cpu_to_le64(monc->cur_mon);
                ceph_con_open(monc->con,
                              &monc->monmap->mon_inst[monc->cur_mon].addr);
+
+               /* initiatiate authentication handshake */
+               ret = ceph_auth_build_hello(monc->auth,
+                                           monc->m_auth->front.iov_base,
+                                           monc->m_auth->front_max);
+               monc->m_auth->front.iov_len = ret;
+               monc->m_auth->hdr.front_len = cpu_to_le32(ret);
+               ceph_msg_get(monc->m_auth);  /* keep our ref */
+               ceph_con_send(monc->con, monc->m_auth);
        } else {
                dout("open_session mon%d already open\n", monc->cur_mon);
        }
@@ -139,7 +156,7 @@ static void __schedule_delayed(struct ceph_mon_client *monc)
 {
        unsigned delay;
 
-       if (monc->cur_mon < 0 || monc->want_mount || __sub_expired(monc))
+       if (monc->cur_mon < 0 || __sub_expired(monc))
                delay = 10 * HZ;
        else
                delay = 20 * HZ;
@@ -161,7 +178,7 @@ static void __send_subscribe(struct ceph_mon_client *monc)
                struct ceph_mon_subscribe_item *i;
                void *p, *end;
 
-               msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 64, 0, 0, NULL);
+               msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, 0, 0, NULL);
                if (!msg)
                        return;
 
@@ -173,7 +190,7 @@ static void __send_subscribe(struct ceph_mon_client *monc)
                if (monc->want_next_osdmap) {
                        dout("__send_subscribe to 'osdmap' %u\n",
                             (unsigned)monc->have_osdmap);
-                       ceph_encode_32(&p, 2);
+                       ceph_encode_32(&p, 3);
                        ceph_encode_string(&p, end, "osdmap", 6);
                        i = p;
                        i->have = cpu_to_le64(monc->have_osdmap);
@@ -181,13 +198,18 @@ static void __send_subscribe(struct ceph_mon_client *monc)
                        p += sizeof(*i);
                        monc->want_next_osdmap = 2;  /* requested */
                } else {
-                       ceph_encode_32(&p, 1);
+                       ceph_encode_32(&p, 2);
                }
                ceph_encode_string(&p, end, "mdsmap", 6);
                i = p;
                i->have = cpu_to_le64(monc->have_mdsmap);
                i->onetime = 0;
                p += sizeof(*i);
+               ceph_encode_string(&p, end, "monmap", 6);
+               i = p;
+               i->have = 0;
+               i->onetime = 0;
+               p += sizeof(*i);
 
                msg->front.iov_len = p - msg->front.iov_base;
                msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
@@ -220,6 +242,7 @@ static void handle_subscribe_ack(struct ceph_mon_client *monc,
        return;
 bad:
        pr_err("got corrupt subscribe-ack msg\n");
+       ceph_msg_dump(msg);
 }
 
 /*
@@ -256,31 +279,10 @@ void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
        mutex_unlock(&monc->mutex);
 }
 
-
 /*
- * mount
+ *
  */
-static void __request_mount(struct ceph_mon_client *monc)
-{
-       struct ceph_msg *msg;
-       struct ceph_client_mount *h;
-       int err;
-
-       dout("__request_mount\n");
-       err = __open_session(monc);
-       if (err)
-               return;
-       msg = ceph_msg_new(CEPH_MSG_CLIENT_MOUNT, sizeof(*h), 0, 0, NULL);
-       if (IS_ERR(msg))
-               return;
-       h = msg->front.iov_base;
-       h->monhdr.have_version = 0;
-       h->monhdr.session_mon = cpu_to_le16(-1);
-       h->monhdr.session_mon_tid = 0;
-       ceph_con_send(monc->con, msg);
-}
-
-int ceph_monc_request_mount(struct ceph_mon_client *monc)
+int ceph_monc_open_session(struct ceph_mon_client *monc)
 {
        if (!monc->con) {
                monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
@@ -292,7 +294,7 @@ int ceph_monc_request_mount(struct ceph_mon_client *monc)
        }
 
        mutex_lock(&monc->mutex);
-       __request_mount(monc);
+       __open_session(monc);
        __schedule_delayed(monc);
        mutex_unlock(&monc->mutex);
        return 0;
@@ -302,80 +304,39 @@ int ceph_monc_request_mount(struct ceph_mon_client *monc)
  * The monitor responds with mount ack indicate mount success.  The
  * included client ticket allows the client to talk to MDSs and OSDs.
  */
-static void handle_mount_ack(struct ceph_mon_client *monc, struct ceph_msg *msg)
+static void ceph_monc_handle_map(struct ceph_mon_client *monc,
+                                struct ceph_msg *msg)
 {
        struct ceph_client *client = monc->client;
        struct ceph_monmap *monmap = NULL, *old = monc->monmap;
        void *p, *end;
-       s32 result;
-       u32 len;
-       s64 cnum;
-       int err = -EINVAL;
-
-       if (client->whoami >= 0) {
-               dout("handle_mount_ack - already mounted\n");
-               return;
-       }
 
        mutex_lock(&monc->mutex);
 
-       dout("handle_mount_ack\n");
+       dout("handle_monmap\n");
        p = msg->front.iov_base;
        end = p + msg->front.iov_len;
 
-       ceph_decode_64_safe(&p, end, cnum, bad);
-       ceph_decode_32_safe(&p, end, result, bad);
-       ceph_decode_32_safe(&p, end, len, bad);
-       if (result) {
-               pr_err("mount denied: %.*s (%d)\n", len, (char *)p,
-                      result);
-               err = result;
-               goto out;
-       }
-       p += len;
-
-       ceph_decode_32_safe(&p, end, len, bad);
-       ceph_decode_need(&p, end, len, bad);
-       monmap = ceph_monmap_decode(p, p + len);
+       monmap = ceph_monmap_decode(p, end);
        if (IS_ERR(monmap)) {
                pr_err("problem decoding monmap, %d\n",
                       (int)PTR_ERR(monmap));
-               err = -EINVAL;
                goto out;
        }
-       p += len;
+
+       if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
+               kfree(monmap);
+               goto out;
+       }
 
        client->monc.monmap = monmap;
        kfree(old);
 
-       client->signed_ticket = NULL;
-       client->signed_ticket_len = 0;
-
-       monc->want_mount = false;
-
-       client->whoami = cnum;
-       client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
-       client->msgr->inst.name.num = cpu_to_le64(cnum);
-       pr_info("client%lld fsid " FSID_FORMAT "\n",
-               client->whoami, PR_FSID(&client->monc.monmap->fsid));
-
-       ceph_debugfs_client_init(client);
-       __send_subscribe(monc);
-
-       err = 0;
-       goto out;
-
-bad:
-       pr_err("error decoding mount_ack message\n");
 out:
-       client->mount_err = err;
        mutex_unlock(&monc->mutex);
        wake_up(&client->mount_wq);
 }
 
-
-
-
 /*
  * statfs
  */
@@ -388,7 +349,7 @@ static void handle_statfs_reply(struct ceph_mon_client *monc,
 
        if (msg->front.iov_len != sizeof(*reply))
                goto bad;
-       tid = le64_to_cpu(reply->tid);
+       tid = le64_to_cpu(msg->hdr.tid);
        dout("handle_statfs_reply %p tid %llu\n", msg, tid);
 
        mutex_lock(&monc->mutex);
@@ -404,6 +365,7 @@ static void handle_statfs_reply(struct ceph_mon_client *monc,
 
 bad:
        pr_err("corrupt statfs reply, no tid\n");
+       ceph_msg_dump(msg);
 }
 
 /*
@@ -414,22 +376,18 @@ static int send_statfs(struct ceph_mon_client *monc,
 {
        struct ceph_msg *msg;
        struct ceph_mon_statfs *h;
-       int err;
 
        dout("send_statfs tid %llu\n", req->tid);
-       err = __open_session(monc);
-       if (err)
-               return err;
        msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL);
        if (IS_ERR(msg))
                return PTR_ERR(msg);
        req->request = msg;
+       msg->hdr.tid = cpu_to_le64(req->tid);
        h = msg->front.iov_base;
        h->monhdr.have_version = 0;
        h->monhdr.session_mon = cpu_to_le16(-1);
        h->monhdr.session_mon_tid = 0;
        h->fsid = monc->monmap->fsid;
-       h->tid = cpu_to_le64(req->tid);
        ceph_con_send(monc->con, msg);
        return 0;
 }
@@ -514,17 +472,14 @@ static void delayed_work(struct work_struct *work)
 
        dout("monc delayed_work\n");
        mutex_lock(&monc->mutex);
-       if (monc->want_mount) {
-               __request_mount(monc);
+       if (monc->hunting) {
+               __close_session(monc);
+               __open_session(monc);  /* continue hunting */
        } else {
-               if (monc->hunting) {
-                       __close_session(monc);
-                       __open_session(monc);  /* continue hunting */
-               } else {
-                       ceph_con_keepalive(monc->con);
-               }
+               ceph_con_keepalive(monc->con);
+               if (monc->auth->ops->is_authenticated(monc->auth))
+                       __send_subscribe(monc);
        }
-       __send_subscribe(monc);
        __schedule_delayed(monc);
        mutex_unlock(&monc->mutex);
 }
@@ -555,6 +510,7 @@ static int build_initial_monmap(struct ceph_mon_client *monc)
                monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
        }
        monc->monmap->num_mon = num_mon;
+       monc->have_fsid = false;
 
        /* release addr memory */
        kfree(args->mon_addr);
@@ -579,21 +535,37 @@ int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
 
        monc->con = NULL;
 
+       /* authentication */
+       monc->auth = ceph_auth_init(cl->mount_args->name,
+                                   cl->mount_args->secret);
+       if (IS_ERR(monc->auth))
+               return PTR_ERR(monc->auth);
+       monc->auth->want_keys =
+               CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
+               CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
+
        /* msg pools */
-       err = ceph_msgpool_init(&monc->msgpool_mount_ack, 4096, 1, false);
-       if (err < 0)
-               goto out;
        err = ceph_msgpool_init(&monc->msgpool_subscribe_ack,
                               sizeof(struct ceph_mon_subscribe_ack), 1, false);
        if (err < 0)
-               goto out;
+               goto out_monmap;
        err = ceph_msgpool_init(&monc->msgpool_statfs_reply,
                                sizeof(struct ceph_mon_statfs_reply), 0, false);
        if (err < 0)
-               goto out;
+               goto out_pool1;
+       err = ceph_msgpool_init(&monc->msgpool_auth_reply, 4096, 1, false);
+       if (err < 0)
+               goto out_pool2;
+
+       monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, 0, 0, NULL);
+       if (IS_ERR(monc->m_auth)) {
+               err = PTR_ERR(monc->m_auth);
+               monc->m_auth = NULL;
+               goto out_pool3;
+       }
 
        monc->cur_mon = -1;
-       monc->hunting = false;  /* not really */
+       monc->hunting = true;
        monc->sub_renew_after = jiffies;
        monc->sub_sent = 0;
 
@@ -605,7 +577,16 @@ int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
        monc->have_mdsmap = 0;
        monc->have_osdmap = 0;
        monc->want_next_osdmap = 1;
-       monc->want_mount = true;
+       return 0;
+
+out_pool3:
+       ceph_msgpool_destroy(&monc->msgpool_auth_reply);
+out_pool2:
+       ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
+out_pool1:
+       ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
+out_monmap:
+       kfree(monc->monmap);
 out:
        return err;
 }
@@ -624,14 +605,47 @@ void ceph_monc_stop(struct ceph_mon_client *monc)
        }
        mutex_unlock(&monc->mutex);
 
-       ceph_msgpool_destroy(&monc->msgpool_mount_ack);
+       ceph_auth_destroy(monc->auth);
+
+       ceph_msg_put(monc->m_auth);
        ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
        ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
+       ceph_msgpool_destroy(&monc->msgpool_auth_reply);
 
        kfree(monc->monmap);
 }
 
 
+static void handle_auth_reply(struct ceph_mon_client *monc,
+                             struct ceph_msg *msg)
+{
+       int ret;
+
+       mutex_lock(&monc->mutex);
+       ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
+                                    msg->front.iov_len,
+                                    monc->m_auth->front.iov_base,
+                                    monc->m_auth->front_max);
+       if (ret < 0) {
+               monc->client->mount_err = ret;
+               wake_up(&monc->client->mount_wq);
+       } else if (ret > 0) {
+               monc->m_auth->front.iov_len = ret;
+               monc->m_auth->hdr.front_len = cpu_to_le32(ret);
+               ceph_msg_get(monc->m_auth);  /* keep our ref */
+               ceph_con_send(monc->con, monc->m_auth);
+       } else if (monc->auth->ops->is_authenticated(monc->auth)) {
+               dout("authenticated, starting session\n");
+
+               monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
+               monc->client->msgr->inst.name.num = monc->auth->global_id;
+
+               __send_subscribe(monc);
+               __resend_statfs(monc);
+       }
+       mutex_unlock(&monc->mutex);
+}
+
 /*
  * handle incoming message
  */
@@ -644,8 +658,8 @@ static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
                return;
 
        switch (type) {
-       case CEPH_MSG_CLIENT_MOUNT_ACK:
-               handle_mount_ack(monc, msg);
+       case CEPH_MSG_AUTH_REPLY:
+               handle_auth_reply(monc, msg);
                break;
 
        case CEPH_MSG_MON_SUBSCRIBE_ACK:
@@ -656,6 +670,10 @@ static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
                handle_statfs_reply(monc, msg);
                break;
 
+       case CEPH_MSG_MON_MAP:
+               ceph_monc_handle_map(monc, msg);
+               break;
+
        case CEPH_MSG_MDS_MAP:
                ceph_mdsc_handle_map(&monc->client->mdsc, msg);
                break;
@@ -682,12 +700,12 @@ static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
        int front = le32_to_cpu(hdr->front_len);
 
        switch (type) {
-       case CEPH_MSG_CLIENT_MOUNT_ACK:
-               return ceph_msgpool_get(&monc->msgpool_mount_ack, front);
        case CEPH_MSG_MON_SUBSCRIBE_ACK:
                return ceph_msgpool_get(&monc->msgpool_subscribe_ack, front);
        case CEPH_MSG_STATFS_REPLY:
                return ceph_msgpool_get(&monc->msgpool_statfs_reply, front);
+       case CEPH_MSG_AUTH_REPLY:
+               return ceph_msgpool_get(&monc->msgpool_auth_reply, front);
        }
        return ceph_alloc_msg(con, hdr);
 }
@@ -717,10 +735,7 @@ static void mon_fault(struct ceph_connection *con)
        if (!monc->hunting) {
                /* start hunting */
                monc->hunting = true;
-               if (__open_session(monc) == 0) {
-                       __send_subscribe(monc);
-                       __resend_statfs(monc);
-               }
+               __open_session(monc);
        } else {
                /* already hunting, let's wait a bit */
                __schedule_delayed(monc);