ceph: fix debugfs entry, simplify fsid checks
[linux-3.10.git] / fs / ceph / mon_client.c
index 61263c9..b742b3b 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));
 
@@ -59,6 +64,8 @@ struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
        m->epoch = epoch;
        m->num_mon = num_mon;
        ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
+       for (i = 0; i < num_mon; i++)
+               ceph_decode_addr(&m->mon_inst[i].addr);
 
        dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
             m->num_mon);
@@ -93,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);
        }
 }
 
@@ -104,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);
@@ -119,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);
        }
@@ -137,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;
@@ -159,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;
 
@@ -171,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);
@@ -179,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);
@@ -254,7 +278,7 @@ void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
        mutex_unlock(&monc->mutex);
 }
 
-
+#if 0
 /*
  * mount
  */
@@ -262,12 +286,8 @@ 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;
@@ -277,8 +297,12 @@ static void __request_mount(struct ceph_mon_client *monc)
        h->monhdr.session_mon_tid = 0;
        ceph_con_send(monc->con, msg);
 }
+#endif
 
-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);
@@ -290,7 +314,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;
@@ -300,80 +324,38 @@ 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;
+               return;
+       }
+
+       if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
+               kfree(monmap);
+               return;
        }
-       p += len;
 
        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
  */
@@ -412,12 +394,8 @@ 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);
@@ -512,17 +490,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);
 }
@@ -553,6 +528,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);
@@ -577,21 +553,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;
 
@@ -603,7 +595,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;
 }
@@ -622,14 +623,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
  */
@@ -642,8 +676,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:
@@ -654,6 +688,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;
@@ -680,12 +718,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);
 }
@@ -715,10 +753,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);