1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/highmem.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
11 #include <linux/bio.h>
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
21 #define OSD_OP_FRONT_LEN 4096
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static const struct ceph_connection_operations osd_con_ops;
26 static void __send_queued(struct ceph_osd_client *osdc);
27 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28 static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30 static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
32 static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
35 static int op_has_extent(int op)
37 return (op == CEPH_OSD_OP_READ ||
38 op == CEPH_OSD_OP_WRITE);
42 * Implement client access to distributed object storage cluster.
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
64 * fill osd op in request message.
66 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
67 struct ceph_osd_req_op *op, u64 *bno)
75 r = ceph_calc_file_object_mapping(layout, off, orig_len, bno,
79 if (objlen < orig_len) {
81 dout(" skipping last %llu, final file extent %llu~%llu\n",
82 orig_len - *plen, off, *plen);
85 if (op_has_extent(op->op)) {
86 u32 osize = le32_to_cpu(layout->fl_object_size);
87 op->extent.offset = objoff;
88 op->extent.length = objlen;
89 if (op->extent.truncate_size <= off - objoff) {
90 op->extent.truncate_size = 0;
92 op->extent.truncate_size -= off - objoff;
93 if (op->extent.truncate_size > osize)
94 op->extent.truncate_size = osize;
97 if (op->op == CEPH_OSD_OP_WRITE)
98 op->payload_len = *plen;
100 dout("calc_layout bno=%llx %llu~%llu\n", *bno, objoff, objlen);
108 void ceph_osdc_release_request(struct kref *kref)
111 struct ceph_osd_request *req = container_of(kref,
112 struct ceph_osd_request,
116 ceph_msg_put(req->r_request);
117 if (req->r_con_filling_msg) {
118 dout("%s revoking msg %p from con %p\n", __func__,
119 req->r_reply, req->r_con_filling_msg);
120 ceph_msg_revoke_incoming(req->r_reply);
121 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
122 req->r_con_filling_msg = NULL;
125 ceph_msg_put(req->r_reply);
127 if (req->r_data_in.type == CEPH_OSD_DATA_TYPE_PAGES &&
128 req->r_data_in.own_pages) {
129 num_pages = calc_pages_for((u64)req->r_data_in.alignment,
130 (u64)req->r_data_in.length);
131 ceph_release_page_vector(req->r_data_in.pages, num_pages);
133 if (req->r_data_out.type == CEPH_OSD_DATA_TYPE_PAGES &&
134 req->r_data_out.own_pages) {
135 num_pages = calc_pages_for((u64)req->r_data_out.alignment,
136 (u64)req->r_data_out.length);
137 ceph_release_page_vector(req->r_data_out.pages, num_pages);
140 ceph_put_snap_context(req->r_snapc);
142 mempool_free(req, req->r_osdc->req_mempool);
146 EXPORT_SYMBOL(ceph_osdc_release_request);
148 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
149 struct ceph_snap_context *snapc,
150 unsigned int num_ops,
154 struct ceph_osd_request *req;
155 struct ceph_msg *msg;
158 msg_size = 4 + 4 + 8 + 8 + 4+8;
159 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
160 msg_size += 1 + 8 + 4 + 4; /* pg_t */
161 msg_size += 4 + MAX_OBJ_NAME_SIZE;
162 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
163 msg_size += 8; /* snapid */
164 msg_size += 8; /* snap_seq */
165 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
169 req = mempool_alloc(osdc->req_mempool, gfp_flags);
170 memset(req, 0, sizeof(*req));
172 req = kzalloc(sizeof(*req), gfp_flags);
178 req->r_mempool = use_mempool;
180 kref_init(&req->r_kref);
181 init_completion(&req->r_completion);
182 init_completion(&req->r_safe_completion);
183 RB_CLEAR_NODE(&req->r_node);
184 INIT_LIST_HEAD(&req->r_unsafe_item);
185 INIT_LIST_HEAD(&req->r_linger_item);
186 INIT_LIST_HEAD(&req->r_linger_osd);
187 INIT_LIST_HEAD(&req->r_req_lru_item);
188 INIT_LIST_HEAD(&req->r_osd_item);
190 /* create reply message */
192 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
194 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
195 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
197 ceph_osdc_put_request(req);
202 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
203 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
205 /* create request message; allow space for oid */
207 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
209 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
211 ceph_osdc_put_request(req);
215 memset(msg->front.iov_base, 0, msg->front.iov_len);
217 req->r_request = msg;
221 EXPORT_SYMBOL(ceph_osdc_alloc_request);
223 static u64 osd_req_encode_op(struct ceph_osd_request *req,
224 struct ceph_osd_op *dst,
225 struct ceph_osd_req_op *src)
227 u64 out_data_len = 0;
228 struct ceph_pagelist *pagelist;
230 dst->op = cpu_to_le16(src->op);
233 case CEPH_OSD_OP_STAT:
235 case CEPH_OSD_OP_READ:
236 case CEPH_OSD_OP_WRITE:
237 if (src->op == CEPH_OSD_OP_WRITE)
238 out_data_len = src->extent.length;
239 dst->extent.offset = cpu_to_le64(src->extent.offset);
240 dst->extent.length = cpu_to_le64(src->extent.length);
241 dst->extent.truncate_size =
242 cpu_to_le64(src->extent.truncate_size);
243 dst->extent.truncate_seq =
244 cpu_to_le32(src->extent.truncate_seq);
246 case CEPH_OSD_OP_CALL:
247 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
249 ceph_pagelist_init(pagelist);
251 dst->cls.class_len = src->cls.class_len;
252 dst->cls.method_len = src->cls.method_len;
253 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
254 ceph_pagelist_append(pagelist, src->cls.class_name,
256 ceph_pagelist_append(pagelist, src->cls.method_name,
257 src->cls.method_len);
258 ceph_pagelist_append(pagelist, src->cls.indata,
259 src->cls.indata_len);
261 req->r_data_out.type = CEPH_OSD_DATA_TYPE_PAGELIST;
262 req->r_data_out.pagelist = pagelist;
263 out_data_len = pagelist->length;
265 case CEPH_OSD_OP_STARTSYNC:
267 case CEPH_OSD_OP_NOTIFY_ACK:
268 case CEPH_OSD_OP_WATCH:
269 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
270 dst->watch.ver = cpu_to_le64(src->watch.ver);
271 dst->watch.flag = src->watch.flag;
274 pr_err("unrecognized osd opcode %d\n", src->op);
277 case CEPH_OSD_OP_MAPEXT:
278 case CEPH_OSD_OP_MASKTRUNC:
279 case CEPH_OSD_OP_SPARSE_READ:
280 case CEPH_OSD_OP_NOTIFY:
281 case CEPH_OSD_OP_ASSERT_VER:
282 case CEPH_OSD_OP_WRITEFULL:
283 case CEPH_OSD_OP_TRUNCATE:
284 case CEPH_OSD_OP_ZERO:
285 case CEPH_OSD_OP_DELETE:
286 case CEPH_OSD_OP_APPEND:
287 case CEPH_OSD_OP_SETTRUNC:
288 case CEPH_OSD_OP_TRIMTRUNC:
289 case CEPH_OSD_OP_TMAPUP:
290 case CEPH_OSD_OP_TMAPPUT:
291 case CEPH_OSD_OP_TMAPGET:
292 case CEPH_OSD_OP_CREATE:
293 case CEPH_OSD_OP_ROLLBACK:
294 case CEPH_OSD_OP_OMAPGETKEYS:
295 case CEPH_OSD_OP_OMAPGETVALS:
296 case CEPH_OSD_OP_OMAPGETHEADER:
297 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
298 case CEPH_OSD_OP_MODE_RD:
299 case CEPH_OSD_OP_OMAPSETVALS:
300 case CEPH_OSD_OP_OMAPSETHEADER:
301 case CEPH_OSD_OP_OMAPCLEAR:
302 case CEPH_OSD_OP_OMAPRMKEYS:
303 case CEPH_OSD_OP_OMAP_CMP:
304 case CEPH_OSD_OP_CLONERANGE:
305 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
306 case CEPH_OSD_OP_SRC_CMPXATTR:
307 case CEPH_OSD_OP_GETXATTR:
308 case CEPH_OSD_OP_GETXATTRS:
309 case CEPH_OSD_OP_CMPXATTR:
310 case CEPH_OSD_OP_SETXATTR:
311 case CEPH_OSD_OP_SETXATTRS:
312 case CEPH_OSD_OP_RESETXATTRS:
313 case CEPH_OSD_OP_RMXATTR:
314 case CEPH_OSD_OP_PULL:
315 case CEPH_OSD_OP_PUSH:
316 case CEPH_OSD_OP_BALANCEREADS:
317 case CEPH_OSD_OP_UNBALANCEREADS:
318 case CEPH_OSD_OP_SCRUB:
319 case CEPH_OSD_OP_SCRUB_RESERVE:
320 case CEPH_OSD_OP_SCRUB_UNRESERVE:
321 case CEPH_OSD_OP_SCRUB_STOP:
322 case CEPH_OSD_OP_SCRUB_MAP:
323 case CEPH_OSD_OP_WRLOCK:
324 case CEPH_OSD_OP_WRUNLOCK:
325 case CEPH_OSD_OP_RDLOCK:
326 case CEPH_OSD_OP_RDUNLOCK:
327 case CEPH_OSD_OP_UPLOCK:
328 case CEPH_OSD_OP_DNLOCK:
329 case CEPH_OSD_OP_PGLS:
330 case CEPH_OSD_OP_PGLS_FILTER:
331 pr_err("unsupported osd opcode %s\n",
332 ceph_osd_op_name(src->op));
336 dst->payload_len = cpu_to_le32(src->payload_len);
342 * build new request AND message
345 void ceph_osdc_build_request(struct ceph_osd_request *req,
346 u64 off, unsigned int num_ops,
347 struct ceph_osd_req_op *src_ops,
348 struct ceph_snap_context *snapc, u64 snap_id,
349 struct timespec *mtime)
351 struct ceph_msg *msg = req->r_request;
352 struct ceph_osd_req_op *src_op;
355 int flags = req->r_flags;
359 req->r_num_ops = num_ops;
360 req->r_snapid = snap_id;
361 req->r_snapc = ceph_get_snap_context(snapc);
364 msg->hdr.version = cpu_to_le16(4);
366 p = msg->front.iov_base;
367 ceph_encode_32(&p, 1); /* client_inc is always 1 */
368 req->r_request_osdmap_epoch = p;
370 req->r_request_flags = p;
372 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
373 ceph_encode_timespec(p, mtime);
374 p += sizeof(struct ceph_timespec);
375 req->r_request_reassert_version = p;
376 p += sizeof(struct ceph_eversion); /* will get filled in */
379 ceph_encode_8(&p, 4);
380 ceph_encode_8(&p, 4);
381 ceph_encode_32(&p, 8 + 4 + 4);
382 req->r_request_pool = p;
384 ceph_encode_32(&p, -1); /* preferred */
385 ceph_encode_32(&p, 0); /* key len */
387 ceph_encode_8(&p, 1);
388 req->r_request_pgid = p;
390 ceph_encode_32(&p, -1); /* preferred */
393 ceph_encode_32(&p, req->r_oid_len);
394 memcpy(p, req->r_oid, req->r_oid_len);
395 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
398 /* ops--can imply data */
399 ceph_encode_16(&p, num_ops);
401 req->r_request_ops = p;
403 for (i = 0; i < num_ops; i++, src_op++) {
404 data_len += osd_req_encode_op(req, p, src_op);
405 p += sizeof(struct ceph_osd_op);
409 ceph_encode_64(&p, req->r_snapid);
410 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
411 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
413 for (i = 0; i < snapc->num_snaps; i++) {
414 ceph_encode_64(&p, req->r_snapc->snaps[i]);
418 req->r_request_attempts = p;
422 if (flags & CEPH_OSD_FLAG_WRITE)
423 req->r_request->hdr.data_off = cpu_to_le16(off);
424 req->r_request->hdr.data_len = cpu_to_le32(data_len);
426 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
427 msg_size = p - msg->front.iov_base;
428 msg->front.iov_len = msg_size;
429 msg->hdr.front_len = cpu_to_le32(msg_size);
431 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
435 EXPORT_SYMBOL(ceph_osdc_build_request);
438 * build new request AND message, calculate layout, and adjust file
441 * if the file was recently truncated, we include information about its
442 * old and new size so that the object can be updated appropriately. (we
443 * avoid synchronously deleting truncated objects because it's slow.)
445 * if @do_sync, include a 'startsync' command so that the osd will flush
448 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
449 struct ceph_file_layout *layout,
450 struct ceph_vino vino,
452 int opcode, int flags,
453 struct ceph_snap_context *snapc,
457 struct timespec *mtime,
460 struct ceph_osd_req_op ops[2];
461 struct ceph_osd_request *req;
462 unsigned int num_op = 1;
466 memset(&ops, 0, sizeof ops);
469 ops[0].extent.truncate_seq = truncate_seq;
470 ops[0].extent.truncate_size = truncate_size;
473 ops[1].op = CEPH_OSD_OP_STARTSYNC;
477 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
480 return ERR_PTR(-ENOMEM);
481 req->r_flags = flags;
483 /* calculate max write size */
484 r = calc_layout(layout, off, plen, ops, &bno);
486 ceph_osdc_put_request(req);
489 req->r_file_layout = *layout; /* keep a copy */
491 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
492 req->r_oid_len = strlen(req->r_oid);
494 ceph_osdc_build_request(req, off, num_op, ops,
495 snapc, vino.snap, mtime);
499 EXPORT_SYMBOL(ceph_osdc_new_request);
502 * We keep osd requests in an rbtree, sorted by ->r_tid.
504 static void __insert_request(struct ceph_osd_client *osdc,
505 struct ceph_osd_request *new)
507 struct rb_node **p = &osdc->requests.rb_node;
508 struct rb_node *parent = NULL;
509 struct ceph_osd_request *req = NULL;
513 req = rb_entry(parent, struct ceph_osd_request, r_node);
514 if (new->r_tid < req->r_tid)
516 else if (new->r_tid > req->r_tid)
522 rb_link_node(&new->r_node, parent, p);
523 rb_insert_color(&new->r_node, &osdc->requests);
526 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
529 struct ceph_osd_request *req;
530 struct rb_node *n = osdc->requests.rb_node;
533 req = rb_entry(n, struct ceph_osd_request, r_node);
534 if (tid < req->r_tid)
536 else if (tid > req->r_tid)
544 static struct ceph_osd_request *
545 __lookup_request_ge(struct ceph_osd_client *osdc,
548 struct ceph_osd_request *req;
549 struct rb_node *n = osdc->requests.rb_node;
552 req = rb_entry(n, struct ceph_osd_request, r_node);
553 if (tid < req->r_tid) {
557 } else if (tid > req->r_tid) {
567 * Resubmit requests pending on the given osd.
569 static void __kick_osd_requests(struct ceph_osd_client *osdc,
570 struct ceph_osd *osd)
572 struct ceph_osd_request *req, *nreq;
575 dout("__kick_osd_requests osd%d\n", osd->o_osd);
576 err = __reset_osd(osdc, osd);
580 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
581 list_move(&req->r_req_lru_item, &osdc->req_unsent);
582 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
585 req->r_flags |= CEPH_OSD_FLAG_RETRY;
588 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
591 * reregister request prior to unregistering linger so
592 * that r_osd is preserved.
594 BUG_ON(!list_empty(&req->r_req_lru_item));
595 __register_request(osdc, req);
596 list_add(&req->r_req_lru_item, &osdc->req_unsent);
597 list_add(&req->r_osd_item, &req->r_osd->o_requests);
598 __unregister_linger_request(osdc, req);
599 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
605 * If the osd connection drops, we need to resubmit all requests.
607 static void osd_reset(struct ceph_connection *con)
609 struct ceph_osd *osd = con->private;
610 struct ceph_osd_client *osdc;
614 dout("osd_reset osd%d\n", osd->o_osd);
616 down_read(&osdc->map_sem);
617 mutex_lock(&osdc->request_mutex);
618 __kick_osd_requests(osdc, osd);
620 mutex_unlock(&osdc->request_mutex);
621 up_read(&osdc->map_sem);
625 * Track open sessions with osds.
627 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
629 struct ceph_osd *osd;
631 osd = kzalloc(sizeof(*osd), GFP_NOFS);
635 atomic_set(&osd->o_ref, 1);
638 RB_CLEAR_NODE(&osd->o_node);
639 INIT_LIST_HEAD(&osd->o_requests);
640 INIT_LIST_HEAD(&osd->o_linger_requests);
641 INIT_LIST_HEAD(&osd->o_osd_lru);
642 osd->o_incarnation = 1;
644 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
646 INIT_LIST_HEAD(&osd->o_keepalive_item);
650 static struct ceph_osd *get_osd(struct ceph_osd *osd)
652 if (atomic_inc_not_zero(&osd->o_ref)) {
653 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
654 atomic_read(&osd->o_ref));
657 dout("get_osd %p FAIL\n", osd);
662 static void put_osd(struct ceph_osd *osd)
664 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
665 atomic_read(&osd->o_ref) - 1);
666 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
667 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
669 if (ac->ops && ac->ops->destroy_authorizer)
670 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
676 * remove an osd from our map
678 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
680 dout("__remove_osd %p\n", osd);
681 BUG_ON(!list_empty(&osd->o_requests));
682 rb_erase(&osd->o_node, &osdc->osds);
683 list_del_init(&osd->o_osd_lru);
684 ceph_con_close(&osd->o_con);
688 static void remove_all_osds(struct ceph_osd_client *osdc)
690 dout("%s %p\n", __func__, osdc);
691 mutex_lock(&osdc->request_mutex);
692 while (!RB_EMPTY_ROOT(&osdc->osds)) {
693 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
694 struct ceph_osd, o_node);
695 __remove_osd(osdc, osd);
697 mutex_unlock(&osdc->request_mutex);
700 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
701 struct ceph_osd *osd)
703 dout("__move_osd_to_lru %p\n", osd);
704 BUG_ON(!list_empty(&osd->o_osd_lru));
705 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
706 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
709 static void __remove_osd_from_lru(struct ceph_osd *osd)
711 dout("__remove_osd_from_lru %p\n", osd);
712 if (!list_empty(&osd->o_osd_lru))
713 list_del_init(&osd->o_osd_lru);
716 static void remove_old_osds(struct ceph_osd_client *osdc)
718 struct ceph_osd *osd, *nosd;
720 dout("__remove_old_osds %p\n", osdc);
721 mutex_lock(&osdc->request_mutex);
722 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
723 if (time_before(jiffies, osd->lru_ttl))
725 __remove_osd(osdc, osd);
727 mutex_unlock(&osdc->request_mutex);
733 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
735 struct ceph_entity_addr *peer_addr;
737 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
738 if (list_empty(&osd->o_requests) &&
739 list_empty(&osd->o_linger_requests)) {
740 __remove_osd(osdc, osd);
745 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
746 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
747 !ceph_con_opened(&osd->o_con)) {
748 struct ceph_osd_request *req;
750 dout(" osd addr hasn't changed and connection never opened,"
751 " letting msgr retry");
752 /* touch each r_stamp for handle_timeout()'s benfit */
753 list_for_each_entry(req, &osd->o_requests, r_osd_item)
754 req->r_stamp = jiffies;
759 ceph_con_close(&osd->o_con);
760 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
761 osd->o_incarnation++;
766 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
768 struct rb_node **p = &osdc->osds.rb_node;
769 struct rb_node *parent = NULL;
770 struct ceph_osd *osd = NULL;
772 dout("__insert_osd %p osd%d\n", new, new->o_osd);
775 osd = rb_entry(parent, struct ceph_osd, o_node);
776 if (new->o_osd < osd->o_osd)
778 else if (new->o_osd > osd->o_osd)
784 rb_link_node(&new->o_node, parent, p);
785 rb_insert_color(&new->o_node, &osdc->osds);
788 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
790 struct ceph_osd *osd;
791 struct rb_node *n = osdc->osds.rb_node;
794 osd = rb_entry(n, struct ceph_osd, o_node);
797 else if (o > osd->o_osd)
805 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
807 schedule_delayed_work(&osdc->timeout_work,
808 osdc->client->options->osd_keepalive_timeout * HZ);
811 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
813 cancel_delayed_work(&osdc->timeout_work);
817 * Register request, assign tid. If this is the first request, set up
820 static void __register_request(struct ceph_osd_client *osdc,
821 struct ceph_osd_request *req)
823 req->r_tid = ++osdc->last_tid;
824 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
825 dout("__register_request %p tid %lld\n", req, req->r_tid);
826 __insert_request(osdc, req);
827 ceph_osdc_get_request(req);
828 osdc->num_requests++;
829 if (osdc->num_requests == 1) {
830 dout(" first request, scheduling timeout\n");
831 __schedule_osd_timeout(osdc);
835 static void register_request(struct ceph_osd_client *osdc,
836 struct ceph_osd_request *req)
838 mutex_lock(&osdc->request_mutex);
839 __register_request(osdc, req);
840 mutex_unlock(&osdc->request_mutex);
844 * called under osdc->request_mutex
846 static void __unregister_request(struct ceph_osd_client *osdc,
847 struct ceph_osd_request *req)
849 if (RB_EMPTY_NODE(&req->r_node)) {
850 dout("__unregister_request %p tid %lld not registered\n",
855 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
856 rb_erase(&req->r_node, &osdc->requests);
857 osdc->num_requests--;
860 /* make sure the original request isn't in flight. */
861 ceph_msg_revoke(req->r_request);
863 list_del_init(&req->r_osd_item);
864 if (list_empty(&req->r_osd->o_requests) &&
865 list_empty(&req->r_osd->o_linger_requests)) {
866 dout("moving osd to %p lru\n", req->r_osd);
867 __move_osd_to_lru(osdc, req->r_osd);
869 if (list_empty(&req->r_linger_item))
873 list_del_init(&req->r_req_lru_item);
874 ceph_osdc_put_request(req);
876 if (osdc->num_requests == 0) {
877 dout(" no requests, canceling timeout\n");
878 __cancel_osd_timeout(osdc);
883 * Cancel a previously queued request message
885 static void __cancel_request(struct ceph_osd_request *req)
887 if (req->r_sent && req->r_osd) {
888 ceph_msg_revoke(req->r_request);
893 static void __register_linger_request(struct ceph_osd_client *osdc,
894 struct ceph_osd_request *req)
896 dout("__register_linger_request %p\n", req);
897 list_add_tail(&req->r_linger_item, &osdc->req_linger);
899 list_add_tail(&req->r_linger_osd,
900 &req->r_osd->o_linger_requests);
903 static void __unregister_linger_request(struct ceph_osd_client *osdc,
904 struct ceph_osd_request *req)
906 dout("__unregister_linger_request %p\n", req);
907 list_del_init(&req->r_linger_item);
909 list_del_init(&req->r_linger_osd);
911 if (list_empty(&req->r_osd->o_requests) &&
912 list_empty(&req->r_osd->o_linger_requests)) {
913 dout("moving osd to %p lru\n", req->r_osd);
914 __move_osd_to_lru(osdc, req->r_osd);
916 if (list_empty(&req->r_osd_item))
921 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
922 struct ceph_osd_request *req)
924 mutex_lock(&osdc->request_mutex);
926 __unregister_linger_request(osdc, req);
927 ceph_osdc_put_request(req);
929 mutex_unlock(&osdc->request_mutex);
931 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
933 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
934 struct ceph_osd_request *req)
936 if (!req->r_linger) {
937 dout("set_request_linger %p\n", req);
940 * caller is now responsible for calling
941 * unregister_linger_request
943 ceph_osdc_get_request(req);
946 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
949 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
950 * (as needed), and set the request r_osd appropriately. If there is
951 * no up osd, set r_osd to NULL. Move the request to the appropriate list
952 * (unsent, homeless) or leave on in-flight lru.
954 * Return 0 if unchanged, 1 if changed, or negative on error.
956 * Caller should hold map_sem for read and request_mutex.
958 static int __map_request(struct ceph_osd_client *osdc,
959 struct ceph_osd_request *req, int force_resend)
962 int acting[CEPH_PG_MAX_SIZE];
966 dout("map_request %p tid %lld\n", req, req->r_tid);
967 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
968 ceph_file_layout_pg_pool(req->r_file_layout));
970 list_move(&req->r_req_lru_item, &osdc->req_notarget);
975 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
981 if ((!force_resend &&
982 req->r_osd && req->r_osd->o_osd == o &&
983 req->r_sent >= req->r_osd->o_incarnation &&
984 req->r_num_pg_osds == num &&
985 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
986 (req->r_osd == NULL && o == -1))
987 return 0; /* no change */
989 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
990 req->r_tid, pgid.pool, pgid.seed, o,
991 req->r_osd ? req->r_osd->o_osd : -1);
993 /* record full pg acting set */
994 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
995 req->r_num_pg_osds = num;
998 __cancel_request(req);
999 list_del_init(&req->r_osd_item);
1003 req->r_osd = __lookup_osd(osdc, o);
1004 if (!req->r_osd && o >= 0) {
1006 req->r_osd = create_osd(osdc, o);
1008 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1012 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1013 __insert_osd(osdc, req->r_osd);
1015 ceph_con_open(&req->r_osd->o_con,
1016 CEPH_ENTITY_TYPE_OSD, o,
1017 &osdc->osdmap->osd_addr[o]);
1021 __remove_osd_from_lru(req->r_osd);
1022 list_add(&req->r_osd_item, &req->r_osd->o_requests);
1023 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1025 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1027 err = 1; /* osd or pg changed */
1034 * caller should hold map_sem (for read) and request_mutex
1036 static void __send_request(struct ceph_osd_client *osdc,
1037 struct ceph_osd_request *req)
1041 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1042 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1043 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1045 /* fill in message content that changes each time we send it */
1046 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1047 put_unaligned_le32(req->r_flags, req->r_request_flags);
1048 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1049 p = req->r_request_pgid;
1050 ceph_encode_64(&p, req->r_pgid.pool);
1051 ceph_encode_32(&p, req->r_pgid.seed);
1052 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1053 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1054 sizeof(req->r_reassert_version));
1056 req->r_stamp = jiffies;
1057 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1059 ceph_msg_get(req->r_request); /* send consumes a ref */
1060 ceph_con_send(&req->r_osd->o_con, req->r_request);
1061 req->r_sent = req->r_osd->o_incarnation;
1065 * Send any requests in the queue (req_unsent).
1067 static void __send_queued(struct ceph_osd_client *osdc)
1069 struct ceph_osd_request *req, *tmp;
1071 dout("__send_queued\n");
1072 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1073 __send_request(osdc, req);
1077 * Timeout callback, called every N seconds when 1 or more osd
1078 * requests has been active for more than N seconds. When this
1079 * happens, we ping all OSDs with requests who have timed out to
1080 * ensure any communications channel reset is detected. Reset the
1081 * request timeouts another N seconds in the future as we go.
1082 * Reschedule the timeout event another N seconds in future (unless
1083 * there are no open requests).
1085 static void handle_timeout(struct work_struct *work)
1087 struct ceph_osd_client *osdc =
1088 container_of(work, struct ceph_osd_client, timeout_work.work);
1089 struct ceph_osd_request *req;
1090 struct ceph_osd *osd;
1091 unsigned long keepalive =
1092 osdc->client->options->osd_keepalive_timeout * HZ;
1093 struct list_head slow_osds;
1095 down_read(&osdc->map_sem);
1097 ceph_monc_request_next_osdmap(&osdc->client->monc);
1099 mutex_lock(&osdc->request_mutex);
1102 * ping osds that are a bit slow. this ensures that if there
1103 * is a break in the TCP connection we will notice, and reopen
1104 * a connection with that osd (from the fault callback).
1106 INIT_LIST_HEAD(&slow_osds);
1107 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1108 if (time_before(jiffies, req->r_stamp + keepalive))
1113 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1114 req->r_tid, osd->o_osd);
1115 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1117 while (!list_empty(&slow_osds)) {
1118 osd = list_entry(slow_osds.next, struct ceph_osd,
1120 list_del_init(&osd->o_keepalive_item);
1121 ceph_con_keepalive(&osd->o_con);
1124 __schedule_osd_timeout(osdc);
1125 __send_queued(osdc);
1126 mutex_unlock(&osdc->request_mutex);
1127 up_read(&osdc->map_sem);
1130 static void handle_osds_timeout(struct work_struct *work)
1132 struct ceph_osd_client *osdc =
1133 container_of(work, struct ceph_osd_client,
1134 osds_timeout_work.work);
1135 unsigned long delay =
1136 osdc->client->options->osd_idle_ttl * HZ >> 2;
1138 dout("osds timeout\n");
1139 down_read(&osdc->map_sem);
1140 remove_old_osds(osdc);
1141 up_read(&osdc->map_sem);
1143 schedule_delayed_work(&osdc->osds_timeout_work,
1144 round_jiffies_relative(delay));
1147 static void complete_request(struct ceph_osd_request *req)
1149 if (req->r_safe_callback)
1150 req->r_safe_callback(req, NULL);
1151 complete_all(&req->r_safe_completion); /* fsync waiter */
1154 static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid)
1158 ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad);
1159 v = ceph_decode_8(p);
1161 pr_warning("do not understand pg encoding %d > 1", v);
1164 pgid->pool = ceph_decode_64(p);
1165 pgid->seed = ceph_decode_32(p);
1170 pr_warning("incomplete pg encoding");
1175 * handle osd op reply. either call the callback if it is specified,
1176 * or do the completion to wake up the waiting thread.
1178 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1179 struct ceph_connection *con)
1182 struct ceph_osd_request *req;
1185 int numops, payload_len, flags;
1191 u64 reassert_version;
1193 int already_completed;
1196 tid = le64_to_cpu(msg->hdr.tid);
1197 dout("handle_reply %p tid %llu\n", msg, tid);
1199 p = msg->front.iov_base;
1200 end = p + msg->front.iov_len;
1202 ceph_decode_need(&p, end, 4, bad);
1203 object_len = ceph_decode_32(&p);
1204 ceph_decode_need(&p, end, object_len, bad);
1207 err = __decode_pgid(&p, end, &pg);
1211 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1212 flags = ceph_decode_64(&p);
1213 result = ceph_decode_32(&p);
1214 reassert_epoch = ceph_decode_32(&p);
1215 reassert_version = ceph_decode_64(&p);
1216 osdmap_epoch = ceph_decode_32(&p);
1219 mutex_lock(&osdc->request_mutex);
1220 req = __lookup_request(osdc, tid);
1222 dout("handle_reply tid %llu dne\n", tid);
1223 mutex_unlock(&osdc->request_mutex);
1226 ceph_osdc_get_request(req);
1228 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1231 ceph_decode_need(&p, end, 4, bad);
1232 numops = ceph_decode_32(&p);
1233 if (numops > CEPH_OSD_MAX_OP)
1235 if (numops != req->r_num_ops)
1238 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1239 for (i = 0; i < numops; i++) {
1240 struct ceph_osd_op *op = p;
1243 len = le32_to_cpu(op->payload_len);
1244 req->r_reply_op_len[i] = len;
1245 dout(" op %d has %d bytes\n", i, len);
1249 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1250 pr_warning("sum of op payload lens %d != data_len %d",
1251 payload_len, le32_to_cpu(msg->hdr.data_len));
1255 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1256 retry_attempt = ceph_decode_32(&p);
1257 for (i = 0; i < numops; i++)
1258 req->r_reply_op_result[i] = ceph_decode_32(&p);
1261 * if this connection filled our message, drop our reference now, to
1262 * avoid a (safe but slower) revoke later.
1264 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1265 dout(" dropping con_filling_msg ref %p\n", con);
1266 req->r_con_filling_msg = NULL;
1270 if (!req->r_got_reply) {
1273 req->r_result = result;
1274 bytes = le32_to_cpu(msg->hdr.data_len);
1275 dout("handle_reply result %d bytes %d\n", req->r_result,
1277 if (req->r_result == 0)
1278 req->r_result = bytes;
1280 /* in case this is a write and we need to replay, */
1281 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1282 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1284 req->r_got_reply = 1;
1285 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1286 dout("handle_reply tid %llu dup ack\n", tid);
1287 mutex_unlock(&osdc->request_mutex);
1291 dout("handle_reply tid %llu flags %d\n", tid, flags);
1293 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1294 __register_linger_request(osdc, req);
1296 /* either this is a read, or we got the safe response */
1298 (flags & CEPH_OSD_FLAG_ONDISK) ||
1299 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1300 __unregister_request(osdc, req);
1302 already_completed = req->r_completed;
1303 req->r_completed = 1;
1304 mutex_unlock(&osdc->request_mutex);
1305 if (already_completed)
1308 if (req->r_callback)
1309 req->r_callback(req, msg);
1311 complete_all(&req->r_completion);
1313 if (flags & CEPH_OSD_FLAG_ONDISK)
1314 complete_request(req);
1317 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1318 ceph_osdc_put_request(req);
1322 ceph_osdc_put_request(req);
1324 pr_err("corrupt osd_op_reply got %d %d\n",
1325 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1329 static void reset_changed_osds(struct ceph_osd_client *osdc)
1331 struct rb_node *p, *n;
1333 for (p = rb_first(&osdc->osds); p; p = n) {
1334 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1337 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1338 memcmp(&osd->o_con.peer_addr,
1339 ceph_osd_addr(osdc->osdmap,
1341 sizeof(struct ceph_entity_addr)) != 0)
1342 __reset_osd(osdc, osd);
1347 * Requeue requests whose mapping to an OSD has changed. If requests map to
1348 * no osd, request a new map.
1350 * Caller should hold map_sem for read.
1352 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1354 struct ceph_osd_request *req, *nreq;
1359 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1360 mutex_lock(&osdc->request_mutex);
1361 for (p = rb_first(&osdc->requests); p; ) {
1362 req = rb_entry(p, struct ceph_osd_request, r_node);
1366 * For linger requests that have not yet been
1367 * registered, move them to the linger list; they'll
1368 * be sent to the osd in the loop below. Unregister
1369 * the request before re-registering it as a linger
1370 * request to ensure the __map_request() below
1371 * will decide it needs to be sent.
1373 if (req->r_linger && list_empty(&req->r_linger_item)) {
1374 dout("%p tid %llu restart on osd%d\n",
1376 req->r_osd ? req->r_osd->o_osd : -1);
1377 __unregister_request(osdc, req);
1378 __register_linger_request(osdc, req);
1382 err = __map_request(osdc, req, force_resend);
1384 continue; /* error */
1385 if (req->r_osd == NULL) {
1386 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1387 needmap++; /* request a newer map */
1388 } else if (err > 0) {
1389 if (!req->r_linger) {
1390 dout("%p tid %llu requeued on osd%d\n", req,
1392 req->r_osd ? req->r_osd->o_osd : -1);
1393 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1398 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1400 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1402 err = __map_request(osdc, req, force_resend);
1403 dout("__map_request returned %d\n", err);
1405 continue; /* no change and no osd was specified */
1407 continue; /* hrm! */
1408 if (req->r_osd == NULL) {
1409 dout("tid %llu maps to no valid osd\n", req->r_tid);
1410 needmap++; /* request a newer map */
1414 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1415 req->r_osd ? req->r_osd->o_osd : -1);
1416 __register_request(osdc, req);
1417 __unregister_linger_request(osdc, req);
1419 mutex_unlock(&osdc->request_mutex);
1422 dout("%d requests for down osds, need new map\n", needmap);
1423 ceph_monc_request_next_osdmap(&osdc->client->monc);
1425 reset_changed_osds(osdc);
1430 * Process updated osd map.
1432 * The message contains any number of incremental and full maps, normally
1433 * indicating some sort of topology change in the cluster. Kick requests
1434 * off to different OSDs as needed.
1436 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1438 void *p, *end, *next;
1439 u32 nr_maps, maplen;
1441 struct ceph_osdmap *newmap = NULL, *oldmap;
1443 struct ceph_fsid fsid;
1445 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1446 p = msg->front.iov_base;
1447 end = p + msg->front.iov_len;
1450 ceph_decode_need(&p, end, sizeof(fsid), bad);
1451 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1452 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1455 down_write(&osdc->map_sem);
1457 /* incremental maps */
1458 ceph_decode_32_safe(&p, end, nr_maps, bad);
1459 dout(" %d inc maps\n", nr_maps);
1460 while (nr_maps > 0) {
1461 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1462 epoch = ceph_decode_32(&p);
1463 maplen = ceph_decode_32(&p);
1464 ceph_decode_need(&p, end, maplen, bad);
1466 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1467 dout("applying incremental map %u len %d\n",
1469 newmap = osdmap_apply_incremental(&p, next,
1471 &osdc->client->msgr);
1472 if (IS_ERR(newmap)) {
1473 err = PTR_ERR(newmap);
1477 if (newmap != osdc->osdmap) {
1478 ceph_osdmap_destroy(osdc->osdmap);
1479 osdc->osdmap = newmap;
1481 kick_requests(osdc, 0);
1483 dout("ignoring incremental map %u len %d\n",
1493 ceph_decode_32_safe(&p, end, nr_maps, bad);
1494 dout(" %d full maps\n", nr_maps);
1496 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1497 epoch = ceph_decode_32(&p);
1498 maplen = ceph_decode_32(&p);
1499 ceph_decode_need(&p, end, maplen, bad);
1501 dout("skipping non-latest full map %u len %d\n",
1503 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1504 dout("skipping full map %u len %d, "
1505 "older than our %u\n", epoch, maplen,
1506 osdc->osdmap->epoch);
1508 int skipped_map = 0;
1510 dout("taking full map %u len %d\n", epoch, maplen);
1511 newmap = osdmap_decode(&p, p+maplen);
1512 if (IS_ERR(newmap)) {
1513 err = PTR_ERR(newmap);
1517 oldmap = osdc->osdmap;
1518 osdc->osdmap = newmap;
1520 if (oldmap->epoch + 1 < newmap->epoch)
1522 ceph_osdmap_destroy(oldmap);
1524 kick_requests(osdc, skipped_map);
1531 downgrade_write(&osdc->map_sem);
1532 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1535 * subscribe to subsequent osdmap updates if full to ensure
1536 * we find out when we are no longer full and stop returning
1539 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1540 ceph_monc_request_next_osdmap(&osdc->client->monc);
1542 mutex_lock(&osdc->request_mutex);
1543 __send_queued(osdc);
1544 mutex_unlock(&osdc->request_mutex);
1545 up_read(&osdc->map_sem);
1546 wake_up_all(&osdc->client->auth_wq);
1550 pr_err("osdc handle_map corrupt msg\n");
1552 up_write(&osdc->map_sem);
1557 * watch/notify callback event infrastructure
1559 * These callbacks are used both for watch and notify operations.
1561 static void __release_event(struct kref *kref)
1563 struct ceph_osd_event *event =
1564 container_of(kref, struct ceph_osd_event, kref);
1566 dout("__release_event %p\n", event);
1570 static void get_event(struct ceph_osd_event *event)
1572 kref_get(&event->kref);
1575 void ceph_osdc_put_event(struct ceph_osd_event *event)
1577 kref_put(&event->kref, __release_event);
1579 EXPORT_SYMBOL(ceph_osdc_put_event);
1581 static void __insert_event(struct ceph_osd_client *osdc,
1582 struct ceph_osd_event *new)
1584 struct rb_node **p = &osdc->event_tree.rb_node;
1585 struct rb_node *parent = NULL;
1586 struct ceph_osd_event *event = NULL;
1590 event = rb_entry(parent, struct ceph_osd_event, node);
1591 if (new->cookie < event->cookie)
1593 else if (new->cookie > event->cookie)
1594 p = &(*p)->rb_right;
1599 rb_link_node(&new->node, parent, p);
1600 rb_insert_color(&new->node, &osdc->event_tree);
1603 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1606 struct rb_node **p = &osdc->event_tree.rb_node;
1607 struct rb_node *parent = NULL;
1608 struct ceph_osd_event *event = NULL;
1612 event = rb_entry(parent, struct ceph_osd_event, node);
1613 if (cookie < event->cookie)
1615 else if (cookie > event->cookie)
1616 p = &(*p)->rb_right;
1623 static void __remove_event(struct ceph_osd_event *event)
1625 struct ceph_osd_client *osdc = event->osdc;
1627 if (!RB_EMPTY_NODE(&event->node)) {
1628 dout("__remove_event removed %p\n", event);
1629 rb_erase(&event->node, &osdc->event_tree);
1630 ceph_osdc_put_event(event);
1632 dout("__remove_event didn't remove %p\n", event);
1636 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1637 void (*event_cb)(u64, u64, u8, void *),
1638 void *data, struct ceph_osd_event **pevent)
1640 struct ceph_osd_event *event;
1642 event = kmalloc(sizeof(*event), GFP_NOIO);
1646 dout("create_event %p\n", event);
1647 event->cb = event_cb;
1648 event->one_shot = 0;
1651 INIT_LIST_HEAD(&event->osd_node);
1652 RB_CLEAR_NODE(&event->node);
1653 kref_init(&event->kref); /* one ref for us */
1654 kref_get(&event->kref); /* one ref for the caller */
1656 spin_lock(&osdc->event_lock);
1657 event->cookie = ++osdc->event_count;
1658 __insert_event(osdc, event);
1659 spin_unlock(&osdc->event_lock);
1664 EXPORT_SYMBOL(ceph_osdc_create_event);
1666 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1668 struct ceph_osd_client *osdc = event->osdc;
1670 dout("cancel_event %p\n", event);
1671 spin_lock(&osdc->event_lock);
1672 __remove_event(event);
1673 spin_unlock(&osdc->event_lock);
1674 ceph_osdc_put_event(event); /* caller's */
1676 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1679 static void do_event_work(struct work_struct *work)
1681 struct ceph_osd_event_work *event_work =
1682 container_of(work, struct ceph_osd_event_work, work);
1683 struct ceph_osd_event *event = event_work->event;
1684 u64 ver = event_work->ver;
1685 u64 notify_id = event_work->notify_id;
1686 u8 opcode = event_work->opcode;
1688 dout("do_event_work completing %p\n", event);
1689 event->cb(ver, notify_id, opcode, event->data);
1690 dout("do_event_work completed %p\n", event);
1691 ceph_osdc_put_event(event);
1697 * Process osd watch notifications
1699 static void handle_watch_notify(struct ceph_osd_client *osdc,
1700 struct ceph_msg *msg)
1704 u64 cookie, ver, notify_id;
1706 struct ceph_osd_event *event;
1707 struct ceph_osd_event_work *event_work;
1709 p = msg->front.iov_base;
1710 end = p + msg->front.iov_len;
1712 ceph_decode_8_safe(&p, end, proto_ver, bad);
1713 ceph_decode_8_safe(&p, end, opcode, bad);
1714 ceph_decode_64_safe(&p, end, cookie, bad);
1715 ceph_decode_64_safe(&p, end, ver, bad);
1716 ceph_decode_64_safe(&p, end, notify_id, bad);
1718 spin_lock(&osdc->event_lock);
1719 event = __find_event(osdc, cookie);
1721 BUG_ON(event->one_shot);
1724 spin_unlock(&osdc->event_lock);
1725 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1726 cookie, ver, event);
1728 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1730 dout("ERROR: could not allocate event_work\n");
1733 INIT_WORK(&event_work->work, do_event_work);
1734 event_work->event = event;
1735 event_work->ver = ver;
1736 event_work->notify_id = notify_id;
1737 event_work->opcode = opcode;
1738 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1739 dout("WARNING: failed to queue notify event work\n");
1747 ceph_osdc_put_event(event);
1751 pr_err("osdc handle_watch_notify corrupt msg\n");
1755 static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1756 struct ceph_osd_data *osd_data)
1758 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1759 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1760 if (osd_data->length)
1761 ceph_msg_data_set_pages(msg, osd_data->pages,
1762 osd_data->length, osd_data->alignment);
1763 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
1764 BUG_ON(!osd_data->pagelist->length);
1765 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
1767 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
1768 ceph_msg_data_set_bio(msg, osd_data->bio);
1771 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1776 * Register request, send initial attempt.
1778 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1779 struct ceph_osd_request *req,
1784 /* Set up response incoming data and request outgoing data fields */
1786 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1787 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
1789 register_request(osdc, req);
1791 down_read(&osdc->map_sem);
1792 mutex_lock(&osdc->request_mutex);
1794 * a racing kick_requests() may have sent the message for us
1795 * while we dropped request_mutex above, so only send now if
1796 * the request still han't been touched yet.
1798 if (req->r_sent == 0) {
1799 rc = __map_request(osdc, req, 0);
1802 dout("osdc_start_request failed map, "
1803 " will retry %lld\n", req->r_tid);
1808 if (req->r_osd == NULL) {
1809 dout("send_request %p no up osds in pg\n", req);
1810 ceph_monc_request_next_osdmap(&osdc->client->monc);
1812 __send_request(osdc, req);
1818 mutex_unlock(&osdc->request_mutex);
1819 up_read(&osdc->map_sem);
1822 EXPORT_SYMBOL(ceph_osdc_start_request);
1825 * wait for a request to complete
1827 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1828 struct ceph_osd_request *req)
1832 rc = wait_for_completion_interruptible(&req->r_completion);
1834 mutex_lock(&osdc->request_mutex);
1835 __cancel_request(req);
1836 __unregister_request(osdc, req);
1837 mutex_unlock(&osdc->request_mutex);
1838 complete_request(req);
1839 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1843 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1844 return req->r_result;
1846 EXPORT_SYMBOL(ceph_osdc_wait_request);
1849 * sync - wait for all in-flight requests to flush. avoid starvation.
1851 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1853 struct ceph_osd_request *req;
1854 u64 last_tid, next_tid = 0;
1856 mutex_lock(&osdc->request_mutex);
1857 last_tid = osdc->last_tid;
1859 req = __lookup_request_ge(osdc, next_tid);
1862 if (req->r_tid > last_tid)
1865 next_tid = req->r_tid + 1;
1866 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1869 ceph_osdc_get_request(req);
1870 mutex_unlock(&osdc->request_mutex);
1871 dout("sync waiting on tid %llu (last is %llu)\n",
1872 req->r_tid, last_tid);
1873 wait_for_completion(&req->r_safe_completion);
1874 mutex_lock(&osdc->request_mutex);
1875 ceph_osdc_put_request(req);
1877 mutex_unlock(&osdc->request_mutex);
1878 dout("sync done (thru tid %llu)\n", last_tid);
1880 EXPORT_SYMBOL(ceph_osdc_sync);
1885 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1890 osdc->client = client;
1891 osdc->osdmap = NULL;
1892 init_rwsem(&osdc->map_sem);
1893 init_completion(&osdc->map_waiters);
1894 osdc->last_requested_map = 0;
1895 mutex_init(&osdc->request_mutex);
1897 osdc->osds = RB_ROOT;
1898 INIT_LIST_HEAD(&osdc->osd_lru);
1899 osdc->requests = RB_ROOT;
1900 INIT_LIST_HEAD(&osdc->req_lru);
1901 INIT_LIST_HEAD(&osdc->req_unsent);
1902 INIT_LIST_HEAD(&osdc->req_notarget);
1903 INIT_LIST_HEAD(&osdc->req_linger);
1904 osdc->num_requests = 0;
1905 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1906 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1907 spin_lock_init(&osdc->event_lock);
1908 osdc->event_tree = RB_ROOT;
1909 osdc->event_count = 0;
1911 schedule_delayed_work(&osdc->osds_timeout_work,
1912 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1915 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1916 sizeof(struct ceph_osd_request));
1917 if (!osdc->req_mempool)
1920 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1921 OSD_OP_FRONT_LEN, 10, true,
1925 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1926 OSD_OPREPLY_FRONT_LEN, 10, true,
1931 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1932 if (IS_ERR(osdc->notify_wq)) {
1933 err = PTR_ERR(osdc->notify_wq);
1934 osdc->notify_wq = NULL;
1940 ceph_msgpool_destroy(&osdc->msgpool_op);
1942 mempool_destroy(osdc->req_mempool);
1947 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1949 flush_workqueue(osdc->notify_wq);
1950 destroy_workqueue(osdc->notify_wq);
1951 cancel_delayed_work_sync(&osdc->timeout_work);
1952 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1954 ceph_osdmap_destroy(osdc->osdmap);
1955 osdc->osdmap = NULL;
1957 remove_all_osds(osdc);
1958 mempool_destroy(osdc->req_mempool);
1959 ceph_msgpool_destroy(&osdc->msgpool_op);
1960 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1964 * Read some contiguous pages. If we cross a stripe boundary, shorten
1965 * *plen. Return number of bytes read, or error.
1967 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1968 struct ceph_vino vino, struct ceph_file_layout *layout,
1970 u32 truncate_seq, u64 truncate_size,
1971 struct page **pages, int num_pages, int page_align)
1973 struct ceph_osd_request *req;
1974 struct ceph_osd_data *osd_data;
1977 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1978 vino.snap, off, *plen);
1979 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1980 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1981 NULL, 0, truncate_seq, truncate_size, NULL,
1984 return PTR_ERR(req);
1986 /* it may be a short read due to an object boundary */
1988 osd_data = &req->r_data_in;
1989 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
1990 osd_data->pages = pages;
1991 osd_data->length = *plen;
1992 osd_data->alignment = page_align;
1994 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
1995 off, *plen, osd_data->length, page_align);
1997 rc = ceph_osdc_start_request(osdc, req, false);
1999 rc = ceph_osdc_wait_request(osdc, req);
2001 ceph_osdc_put_request(req);
2002 dout("readpages result %d\n", rc);
2005 EXPORT_SYMBOL(ceph_osdc_readpages);
2008 * do a synchronous write on N pages
2010 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2011 struct ceph_file_layout *layout,
2012 struct ceph_snap_context *snapc,
2014 u32 truncate_seq, u64 truncate_size,
2015 struct timespec *mtime,
2016 struct page **pages, int num_pages)
2018 struct ceph_osd_request *req;
2019 struct ceph_osd_data *osd_data;
2021 int page_align = off & ~PAGE_MASK;
2023 BUG_ON(vino.snap != CEPH_NOSNAP);
2024 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
2026 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2028 truncate_seq, truncate_size, mtime,
2031 return PTR_ERR(req);
2033 /* it may be a short write due to an object boundary */
2034 osd_data = &req->r_data_out;
2035 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2036 osd_data->pages = pages;
2037 osd_data->length = len;
2038 osd_data->alignment = page_align;
2039 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
2041 rc = ceph_osdc_start_request(osdc, req, true);
2043 rc = ceph_osdc_wait_request(osdc, req);
2045 ceph_osdc_put_request(req);
2048 dout("writepages result %d\n", rc);
2051 EXPORT_SYMBOL(ceph_osdc_writepages);
2054 * handle incoming message
2056 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2058 struct ceph_osd *osd = con->private;
2059 struct ceph_osd_client *osdc;
2060 int type = le16_to_cpu(msg->hdr.type);
2067 case CEPH_MSG_OSD_MAP:
2068 ceph_osdc_handle_map(osdc, msg);
2070 case CEPH_MSG_OSD_OPREPLY:
2071 handle_reply(osdc, msg, con);
2073 case CEPH_MSG_WATCH_NOTIFY:
2074 handle_watch_notify(osdc, msg);
2078 pr_err("received unknown message type %d %s\n", type,
2079 ceph_msg_type_name(type));
2086 * lookup and return message for incoming reply. set up reply message
2089 static struct ceph_msg *get_reply(struct ceph_connection *con,
2090 struct ceph_msg_header *hdr,
2093 struct ceph_osd *osd = con->private;
2094 struct ceph_osd_client *osdc = osd->o_osdc;
2096 struct ceph_osd_request *req;
2097 int front = le32_to_cpu(hdr->front_len);
2098 int data_len = le32_to_cpu(hdr->data_len);
2101 tid = le64_to_cpu(hdr->tid);
2102 mutex_lock(&osdc->request_mutex);
2103 req = __lookup_request(osdc, tid);
2107 dout("get_reply unknown tid %llu from osd%d\n", tid,
2112 if (req->r_con_filling_msg) {
2113 dout("%s revoking msg %p from old con %p\n", __func__,
2114 req->r_reply, req->r_con_filling_msg);
2115 ceph_msg_revoke_incoming(req->r_reply);
2116 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2117 req->r_con_filling_msg = NULL;
2120 if (front > req->r_reply->front.iov_len) {
2121 pr_warning("get_reply front %d > preallocated %d\n",
2122 front, (int)req->r_reply->front.iov_len);
2123 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2126 ceph_msg_put(req->r_reply);
2129 m = ceph_msg_get(req->r_reply);
2132 struct ceph_osd_data *osd_data = &req->r_data_in;
2134 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2135 if (osd_data->pages &&
2136 unlikely(osd_data->length < data_len)) {
2138 pr_warning("tid %lld reply has %d bytes "
2139 "we had only %llu bytes ready\n",
2140 tid, data_len, osd_data->length);
2149 req->r_con_filling_msg = con->ops->get(con);
2150 dout("get_reply tid %lld %p\n", tid, m);
2153 mutex_unlock(&osdc->request_mutex);
2158 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2159 struct ceph_msg_header *hdr,
2162 struct ceph_osd *osd = con->private;
2163 int type = le16_to_cpu(hdr->type);
2164 int front = le32_to_cpu(hdr->front_len);
2168 case CEPH_MSG_OSD_MAP:
2169 case CEPH_MSG_WATCH_NOTIFY:
2170 return ceph_msg_new(type, front, GFP_NOFS, false);
2171 case CEPH_MSG_OSD_OPREPLY:
2172 return get_reply(con, hdr, skip);
2174 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2182 * Wrappers to refcount containing ceph_osd struct
2184 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2186 struct ceph_osd *osd = con->private;
2192 static void put_osd_con(struct ceph_connection *con)
2194 struct ceph_osd *osd = con->private;
2202 * Note: returned pointer is the address of a structure that's
2203 * managed separately. Caller must *not* attempt to free it.
2205 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2206 int *proto, int force_new)
2208 struct ceph_osd *o = con->private;
2209 struct ceph_osd_client *osdc = o->o_osdc;
2210 struct ceph_auth_client *ac = osdc->client->monc.auth;
2211 struct ceph_auth_handshake *auth = &o->o_auth;
2213 if (force_new && auth->authorizer) {
2214 if (ac->ops && ac->ops->destroy_authorizer)
2215 ac->ops->destroy_authorizer(ac, auth->authorizer);
2216 auth->authorizer = NULL;
2218 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2219 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2222 return ERR_PTR(ret);
2223 } else if (ac->ops && ac->ops->update_authorizer) {
2224 int ret = ac->ops->update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2227 return ERR_PTR(ret);
2229 *proto = ac->protocol;
2235 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2237 struct ceph_osd *o = con->private;
2238 struct ceph_osd_client *osdc = o->o_osdc;
2239 struct ceph_auth_client *ac = osdc->client->monc.auth;
2242 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2243 * XXX which do we do: succeed or fail?
2245 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2248 static int invalidate_authorizer(struct ceph_connection *con)
2250 struct ceph_osd *o = con->private;
2251 struct ceph_osd_client *osdc = o->o_osdc;
2252 struct ceph_auth_client *ac = osdc->client->monc.auth;
2254 if (ac->ops && ac->ops->invalidate_authorizer)
2255 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2257 return ceph_monc_validate_auth(&osdc->client->monc);
2260 static const struct ceph_connection_operations osd_con_ops = {
2263 .dispatch = dispatch,
2264 .get_authorizer = get_authorizer,
2265 .verify_authorizer_reply = verify_authorizer_reply,
2266 .invalidate_authorizer = invalidate_authorizer,
2267 .alloc_msg = alloc_msg,