cregit-Linux how code gets into the kernel

Release 4.11 net/ceph/mon_client.c

Directory: net/ceph
#include <linux/ceph/ceph_debug.h>

#include <linux/module.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/random.h>
#include <linux/sched.h>

#include <linux/ceph/mon_client.h>
#include <linux/ceph/libceph.h>
#include <linux/ceph/debugfs.h>
#include <linux/ceph/decode.h>
#include <linux/ceph/auth.h>

/*
 * Interact with Ceph monitor cluster.  Handle requests for new map
 * versions, and periodically resend as needed.  Also implement
 * statfs() and umount().
 *
 * A small cluster of Ceph "monitors" are responsible for managing critical
 * cluster configuration and state information.  An odd number (e.g., 3, 5)
 * of cmon daemons use a modified version of the Paxos part-time parliament
 * algorithm to manage the MDS map (mds cluster membership), OSD map, and
 * list of clients who have mounted the file system.
 *
 * We maintain an open, active session with a monitor at all times in order to
 * receive timely MDSMap updates.  We periodically send a keepalive byte on the
 * TCP socket to ensure we detect a failure.  If the connection does break, we
 * randomly hunt for a new monitor.  Once the connection is reestablished, we
 * resend any outstanding requests.
 */


static const struct ceph_connection_operations mon_con_ops;

static int __validate_auth(struct ceph_mon_client *monc);

/*
 * Decode a monmap blob (e.g., during mount).
 */

struct ceph_monmap *ceph_monmap_decode(void *p, void *end) { struct ceph_monmap *m = NULL; int i, err = -EINVAL; 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)); ceph_decode_16_safe(&p, end, version, bad); ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad); ceph_decode_copy(&p, &fsid, sizeof(fsid)); epoch = ceph_decode_32(&p); num_mon = ceph_decode_32(&p); ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad); if (num_mon >= CEPH_MAX_MON) goto bad; m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS); if (m == NULL) return ERR_PTR(-ENOMEM); m->fsid = fsid; 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); for (i = 0; i < m->num_mon; i++) dout("monmap_decode mon%d is %s\n", i, ceph_pr_addr(&m->mon_inst[i].addr.in_addr)); return m; bad: dout("monmap_decode failed with %d\n", err); kfree(m); return ERR_PTR(err); }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil35899.72%480.00%
Yehuda Sadeh Weinraub10.28%120.00%
Total359100.00%5100.00%

/* * return true if *addr is included in the monmap. */
int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr) { int i; for (i = 0; i < m->num_mon; i++) if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0) return 1; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil64100.00%2100.00%
Total64100.00%2100.00%

/* * Send an auth request. */
static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len) { monc->pending_auth = 1; monc->m_auth->front.iov_len = len; monc->m_auth->hdr.front_len = cpu_to_le32(len); ceph_msg_revoke(monc->m_auth); ceph_msg_get(monc->m_auth); /* keep our ref */ ceph_con_send(&monc->con, monc->m_auth); }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil6897.14%250.00%
Alex Elder22.86%250.00%
Total70100.00%4100.00%

/* * Close monitor session, if any. */
static void __close_session(struct ceph_mon_client *monc) { dout("__close_session closing mon%d\n", monc->cur_mon); ceph_msg_revoke(monc->m_auth); ceph_msg_revoke_incoming(monc->m_auth_reply); ceph_msg_revoke(monc->m_subscribe); ceph_msg_revoke_incoming(monc->m_subscribe_ack); ceph_con_close(&monc->con); monc->pending_auth = 0; ceph_auth_reset(monc->auth); }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil6797.10%466.67%
Alex Elder22.90%233.33%
Total69100.00%6100.00%

/* * Pick a new monitor at random and set cur_mon. If we are repicking * (i.e. cur_mon is already set), be sure to pick a different one. */
static void pick_new_mon(struct ceph_mon_client *monc) { int old_mon = monc->cur_mon; BUG_ON(monc->monmap->num_mon < 1); if (monc->monmap->num_mon == 1) { monc->cur_mon = 0; } else { int max = monc->monmap->num_mon; int o = -1; int n; if (monc->cur_mon >= 0) { if (monc->cur_mon < monc->monmap->num_mon) o = monc->cur_mon; if (o >= 0) max--; } n = prandom_u32() % max; if (o >= 0 && n >= o) n++; monc->cur_mon = n; } dout("%s mon%d -> mon%d out of %d mons\n", __func__, old_mon, monc->cur_mon, monc->monmap->num_mon); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov11476.00%133.33%
Sage Weil3624.00%266.67%
Total150100.00%3100.00%

/* * Open a session with a new monitor. */
static void __open_session(struct ceph_mon_client *monc) { int ret; pick_new_mon(monc); monc->hunting = true; if (monc->had_a_connection) { monc->hunt_mult *= CEPH_MONC_HUNT_BACKOFF; if (monc->hunt_mult > CEPH_MONC_HUNT_MAX_MULT) monc->hunt_mult = CEPH_MONC_HUNT_MAX_MULT; } monc->sub_renew_after = jiffies; /* i.e., expired */ monc->sub_renew_sent = 0; dout("%s opening mon%d\n", __func__, monc->cur_mon); ceph_con_open(&monc->con, CEPH_ENTITY_TYPE_MON, monc->cur_mon, &monc->monmap->mon_inst[monc->cur_mon].addr); /* * send an initial keepalive to ensure our timestamp is valid * by the time we are in an OPENED state */ ceph_con_keepalive(&monc->con); /* initiate authentication handshake */ ret = ceph_auth_build_hello(monc->auth, monc->m_auth->front.iov_base, monc->m_auth->front_alloc_len); BUG_ON(ret <= 0); __send_prepared_auth_request(monc, ret); }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil7247.37%433.33%
Ilya Dryomov6341.45%541.67%
Alex Elder95.92%216.67%
Yan, Zheng85.26%18.33%
Total152100.00%12100.00%


static void reopen_session(struct ceph_mon_client *monc) { if (!monc->hunting) pr_info("mon%d %s session lost, hunting for new mon\n", monc->cur_mon, ceph_pr_addr(&monc->con.peer_addr.in_addr)); __close_session(monc); __open_session(monc); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov49100.00%1100.00%
Total49100.00%1100.00%

/* * Reschedule delayed work timer. */
static void __schedule_delayed(struct ceph_mon_client *monc) { unsigned long delay; if (monc->hunting) delay = CEPH_MONC_HUNT_INTERVAL * monc->hunt_mult; else delay = CEPH_MONC_PING_INTERVAL; dout("__schedule_delayed after %lu\n", delay); mod_delayed_work(system_wq, &monc->delayed_work, round_jiffies_relative(delay)); }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil4173.21%120.00%
Ilya Dryomov916.07%360.00%
Yan, Zheng610.71%120.00%
Total56100.00%5100.00%

const char *ceph_sub_str[] = { [CEPH_SUB_MONMAP] = "monmap", [CEPH_SUB_OSDMAP] = "osdmap", [CEPH_SUB_FSMAP] = "fsmap.user", [CEPH_SUB_MDSMAP] = "mdsmap", }; /* * Send subscribe request for one or more maps, according to * monc->subs. */
static void __send_subscribe(struct ceph_mon_client *monc) { struct ceph_msg *msg = monc->m_subscribe; void *p = msg->front.iov_base; void *const end = p + msg->front_alloc_len; int num = 0; int i; dout("%s sent %lu\n", __func__, monc->sub_renew_sent); BUG_ON(monc->cur_mon < 0); if (!monc->sub_renew_sent) monc->sub_renew_sent = jiffies | 1; /* never 0 */ msg->hdr.version = cpu_to_le16(2); for (i = 0; i < ARRAY_SIZE(monc->subs); i++) { if (monc->subs[i].want) num++; } BUG_ON(num < 1); /* monmap sub is always there */ ceph_encode_32(&p, num); for (i = 0; i < ARRAY_SIZE(monc->subs); i++) { char buf[32]; int len; if (!monc->subs[i].want) continue; len = sprintf(buf, "%s", ceph_sub_str[i]); if (i == CEPH_SUB_MDSMAP && monc->fs_cluster_id != CEPH_FS_CLUSTER_ID_NONE) len += sprintf(buf + len, ".%d", monc->fs_cluster_id); dout("%s %s start %llu flags 0x%x\n", __func__, buf, le64_to_cpu(monc->subs[i].item.start), monc->subs[i].item.flags); ceph_encode_string(&p, end, buf, len); memcpy(p, &monc->subs[i].item, sizeof(monc->subs[i].item)); p += sizeof(monc->subs[i].item); } BUG_ON(p > end); msg->front.iov_len = p - msg->front.iov_base; msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); ceph_msg_revoke(msg); ceph_con_send(&monc->con, ceph_msg_get(msg)); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov21558.11%333.33%
Sage Weil13937.57%333.33%
Yehuda Sadeh Weinraub143.78%111.11%
Alex Elder20.54%222.22%
Total370100.00%9100.00%


static void handle_subscribe_ack(struct ceph_mon_client *monc, struct ceph_msg *msg) { unsigned int seconds; struct ceph_mon_subscribe_ack *h = msg->front.iov_base; if (msg->front.iov_len < sizeof(*h)) goto bad; seconds = le32_to_cpu(h->duration); mutex_lock(&monc->mutex); if (monc->sub_renew_sent) { monc->sub_renew_after = monc->sub_renew_sent + (seconds >> 1) * HZ - 1; dout("%s sent %lu duration %d renew after %lu\n", __func__, monc->sub_renew_sent, seconds, monc->sub_renew_after); monc->sub_renew_sent = 0; } else { dout("%s sent %lu renew after %lu, ignoring\n", __func__, monc->sub_renew_sent, monc->sub_renew_after); } mutex_unlock(&monc->mutex); return; bad: pr_err("got corrupt subscribe-ack msg\n"); ceph_msg_dump(msg); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov8958.17%125.00%
Sage Weil6341.18%250.00%
Eric Dumazet10.65%125.00%
Total153100.00%4100.00%

/* * Register interest in a map * * @sub: one of CEPH_SUB_* * @epoch: X for "every map since X", or 0 for "just the latest" */
static bool __ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch, bool continuous) { __le64 start = cpu_to_le64(epoch); u8 flags = !continuous ? CEPH_SUBSCRIBE_ONETIME : 0; dout("%s %s epoch %u continuous %d\n", __func__, ceph_sub_str[sub], epoch, continuous); if (monc->subs[sub].want && monc->subs[sub].item.start == start && monc->subs[sub].item.flags == flags) return false; monc->subs[sub].item.start = start; monc->subs[sub].item.flags = flags; monc->subs[sub].want = true; return true; }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov134100.00%1100.00%
Total134100.00%1100.00%


bool ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch, bool continuous) { bool need_request; mutex_lock(&monc->mutex); need_request = __ceph_monc_want_map(monc, sub, epoch, continuous); mutex_unlock(&monc->mutex); return need_request; }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov4277.78%150.00%
Sage Weil1222.22%150.00%
Total54100.00%2100.00%

EXPORT_SYMBOL(ceph_monc_want_map); /* * Keep track of which maps we have * * @sub: one of CEPH_SUB_* */
static void __ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch) { dout("%s %s epoch %u\n", __func__, ceph_sub_str[sub], epoch); if (monc->subs[sub].want) { if (monc->subs[sub].item.flags & CEPH_SUBSCRIBE_ONETIME) monc->subs[sub].want = false; else monc->subs[sub].item.start = cpu_to_le64(epoch + 1); } monc->subs[sub].have = epoch; }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov7878.00%150.00%
Sage Weil2222.00%150.00%
Total100100.00%2100.00%


void ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch) { mutex_lock(&monc->mutex); __ceph_monc_got_map(monc, sub, epoch); mutex_unlock(&monc->mutex); }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil2868.29%150.00%
Ilya Dryomov1331.71%150.00%
Total41100.00%2100.00%

EXPORT_SYMBOL(ceph_monc_got_map);
void ceph_monc_renew_subs(struct ceph_mon_client *monc) { mutex_lock(&monc->mutex); __send_subscribe(monc); mutex_unlock(&monc->mutex); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov31100.00%1100.00%
Total31100.00%1100.00%

EXPORT_SYMBOL(ceph_monc_renew_subs); /* * Wait for an osdmap with a given epoch. * * @epoch: epoch to wait for * @timeout: in jiffies, 0 means "wait forever" */
int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch, unsigned long timeout) { unsigned long started = jiffies; long ret; mutex_lock(&monc->mutex); while (monc->subs[CEPH_SUB_OSDMAP].have < epoch) { mutex_unlock(&monc->mutex); if (timeout && time_after_eq(jiffies, started + timeout)) return -ETIMEDOUT; ret = wait_event_interruptible_timeout(monc->client->auth_wq, monc->subs[CEPH_SUB_OSDMAP].have >= epoch, ceph_timeout_jiffies(timeout)); if (ret < 0) return ret; mutex_lock(&monc->mutex); } mutex_unlock(&monc->mutex); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov129100.00%4100.00%
Total129100.00%4100.00%

EXPORT_SYMBOL(ceph_monc_wait_osdmap); /* * Open a session with a random monitor. Request monmap and osdmap, * which are waited upon in __ceph_open_session(). */
int ceph_monc_open_session(struct ceph_mon_client *monc) { mutex_lock(&monc->mutex); __ceph_monc_want_map(monc, CEPH_SUB_MONMAP, 0, true); __ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, 0, false); __open_session(monc); __schedule_delayed(monc); mutex_unlock(&monc->mutex); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil3963.93%266.67%
Ilya Dryomov2236.07%133.33%
Total61100.00%3100.00%

EXPORT_SYMBOL(ceph_monc_open_session);
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; mutex_lock(&monc->mutex); dout("handle_monmap\n"); p = msg->front.iov_base; end = p + msg->front.iov_len; monmap = ceph_monmap_decode(p, end); if (IS_ERR(monmap)) { pr_err("problem decoding monmap, %d\n", (int)PTR_ERR(monmap)); goto out; } if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) { kfree(monmap); goto out; } client->monc.monmap = monmap; kfree(old); __ceph_monc_got_map(monc, CEPH_SUB_MONMAP, monc->monmap->epoch); client->have_fsid = true; out: mutex_unlock(&monc->mutex); wake_up_all(&client->auth_wq); }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil17392.51%675.00%
Ilya Dryomov136.95%112.50%
Yehuda Sadeh Weinraub10.53%112.50%
Total187100.00%8100.00%

/* * generic requests (currently statfs, mon_get_version) */ DEFINE_RB_FUNCS(generic_request, struct ceph_mon_generic_request, tid, node)
static void release_generic_request(struct kref *kref) { struct ceph_mon_generic_request *req = container_of(kref, struct ceph_mon_generic_request, kref); dout("%s greq %p request %p reply %p\n", __func__, req, req->request, req->reply); WARN_ON(!RB_EMPTY_NODE(&req->node)); if (req->reply) ceph_msg_put(req->reply); if (req->request) ceph_msg_put(req->request); kfree(req); }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil4956.98%125.00%
Ilya Dryomov2933.72%125.00%
Yehuda Sadeh Weinraub89.30%250.00%
Total86100.00%4100.00%


static void put_generic_request(struct ceph_mon_generic_request *req) { if (req) kref_put(&req->kref, release_generic_request); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov25100.00%1100.00%
Total25100.00%1100.00%


static void get_generic_request(struct ceph_mon_generic_request *req) { kref_get(&req->kref); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov19100.00%1100.00%
Total19100.00%1100.00%


static struct ceph_mon_generic_request * alloc_generic_request(struct ceph_mon_client *monc, gfp_t gfp) { struct ceph_mon_generic_request *req; req = kzalloc(sizeof(*req), gfp); if (!req) return NULL; req->monc = monc; kref_init(&req->kref); RB_CLEAR_NODE(&req->node); init_completion(&req->completion); dout("%s greq %p\n", __func__, req); return req; }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov84100.00%1100.00%
Total84100.00%1100.00%


static void register_generic_request(struct ceph_mon_generic_request *req) { struct ceph_mon_client *monc = req->monc; WARN_ON(req->tid); get_generic_request(req); req->tid = ++monc->last_tid; insert_generic_request(&monc->generic_request_tree, req); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov51100.00%1100.00%
Total51100.00%1100.00%


static void send_generic_request(struct ceph_mon_client *monc, struct ceph_mon_generic_request *req) { WARN_ON(!req->tid); dout("%s greq %p tid %llu\n", __func__, req, req->tid); req->request->hdr.tid = cpu_to_le64(req->tid); ceph_con_send(&monc->con, ceph_msg_get(req->request)); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov67100.00%1100.00%
Total67100.00%1100.00%


static void __finish_generic_request(struct ceph_mon_generic_request *req) { struct ceph_mon_client *monc = req->monc; dout("%s greq %p tid %llu\n", __func__, req, req->tid); erase_generic_request(&monc->generic_request_tree, req); ceph_msg_revoke(req->request); ceph_msg_revoke_incoming(req->reply); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov57100.00%1100.00%
Total57100.00%1100.00%


static void finish_generic_request(struct ceph_mon_generic_request *req) { __finish_generic_request(req); put_generic_request(req); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov21100.00%1100.00%
Total21100.00%1100.00%


static void complete_generic_request(struct ceph_mon_generic_request *req) { if (req->complete_cb) req->complete_cb(req); else complete_all(&req->completion); put_generic_request(req); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov38100.00%1100.00%
Total38100.00%1100.00%


static void cancel_generic_request(struct ceph_mon_generic_request *req) { struct ceph_mon_client *monc = req->monc; struct ceph_mon_generic_request *lookup_req; dout("%s greq %p tid %llu\n", __func__, req, req->tid); mutex_lock(&monc->mutex); lookup_req = lookup_generic_request(&monc->generic_request_tree, req->tid); if (lookup_req) { WARN_ON(lookup_req != req); finish_generic_request(req); } mutex_unlock(&monc->mutex); }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov8598.84%150.00%
Wei Yongjun11.16%150.00%
Total86100.00%2100.00%


static int wait_generic_request(struct ceph_mon_generic_request *req) { int ret; dout("%s greq %p tid %llu\n", __func__, req, req->tid); ret = wait_for_completion_interruptible(&req->completion); if (ret) cancel_generic_request(req); else ret = req->result; /* completed */ return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Ilya Dryomov3764.91%133.33%
Sage Weil1933.33%133.33%
Yehuda Sadeh Weinraub11.75%133.33%
Total57100.00%3100.00%


static struct ceph_msg *get_generic_reply(struct ceph_connection *con, struct ceph_msg_header *hdr, int *skip) { struct ceph_mon_client *monc = con->private; struct ceph_mon_generic_request *req; u64 tid = le64_to_cpu(hdr->tid); struct ceph_msg *m; mutex_lock(&monc->mutex); req = lookup_generic_request(&monc->generic_request_tree, tid); if (!req) { dout("get_generic_reply %lld dne\n", tid); *skip = 1; m = NULL; } else { dout("get_generic_reply %lld got %p\n", tid, req->reply); *skip = 0; m = ceph_msg_get(req->reply); /* * we don't need to track the connection reading into * this reply because we only have one open connection * at a time, ever. */ } mutex_unlock(&monc->mutex); return m; }

Contributors

PersonTokensPropCommitsCommitProp
Sage Weil12190.30%125.00%
Alex Elder53.73%125.00%
Ilya Dryomov42.99%125.00%
Yehuda Sadeh Weinraub42.99%125.00%
Total134100.00%4100.00%

/* * statfs */