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)
110 struct ceph_osd_request *req = container_of(kref,
111 struct ceph_osd_request,
115 ceph_msg_put(req->r_request);
116 if (req->r_con_filling_msg) {
117 dout("%s revoking msg %p from con %p\n", __func__,
118 req->r_reply, req->r_con_filling_msg);
119 ceph_msg_revoke_incoming(req->r_reply);
120 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
121 req->r_con_filling_msg = NULL;
124 ceph_msg_put(req->r_reply);
125 if (req->r_own_pages)
126 ceph_release_page_vector(req->r_pages,
128 ceph_put_snap_context(req->r_snapc);
129 ceph_pagelist_release(&req->r_trail);
131 mempool_free(req, req->r_osdc->req_mempool);
135 EXPORT_SYMBOL(ceph_osdc_release_request);
137 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
138 struct ceph_snap_context *snapc,
139 unsigned int num_ops,
143 struct ceph_osd_request *req;
144 struct ceph_msg *msg;
147 msg_size = 4 + 4 + 8 + 8 + 4+8;
148 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
149 msg_size += 1 + 8 + 4 + 4; /* pg_t */
150 msg_size += 4 + MAX_OBJ_NAME_SIZE;
151 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
152 msg_size += 8; /* snapid */
153 msg_size += 8; /* snap_seq */
154 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
158 req = mempool_alloc(osdc->req_mempool, gfp_flags);
159 memset(req, 0, sizeof(*req));
161 req = kzalloc(sizeof(*req), gfp_flags);
167 req->r_mempool = use_mempool;
169 kref_init(&req->r_kref);
170 init_completion(&req->r_completion);
171 init_completion(&req->r_safe_completion);
172 RB_CLEAR_NODE(&req->r_node);
173 INIT_LIST_HEAD(&req->r_unsafe_item);
174 INIT_LIST_HEAD(&req->r_linger_item);
175 INIT_LIST_HEAD(&req->r_linger_osd);
176 INIT_LIST_HEAD(&req->r_req_lru_item);
177 INIT_LIST_HEAD(&req->r_osd_item);
179 /* create reply message */
181 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
183 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
184 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
186 ceph_osdc_put_request(req);
191 ceph_pagelist_init(&req->r_trail);
193 /* create request message; allow space for oid */
195 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
197 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
199 ceph_osdc_put_request(req);
203 memset(msg->front.iov_base, 0, msg->front.iov_len);
205 req->r_request = msg;
209 EXPORT_SYMBOL(ceph_osdc_alloc_request);
211 static void osd_req_encode_op(struct ceph_osd_request *req,
212 struct ceph_osd_op *dst,
213 struct ceph_osd_req_op *src)
215 dst->op = cpu_to_le16(src->op);
218 case CEPH_OSD_OP_STAT:
220 case CEPH_OSD_OP_READ:
221 case CEPH_OSD_OP_WRITE:
223 cpu_to_le64(src->extent.offset);
225 cpu_to_le64(src->extent.length);
226 dst->extent.truncate_size =
227 cpu_to_le64(src->extent.truncate_size);
228 dst->extent.truncate_seq =
229 cpu_to_le32(src->extent.truncate_seq);
231 case CEPH_OSD_OP_CALL:
232 dst->cls.class_len = src->cls.class_len;
233 dst->cls.method_len = src->cls.method_len;
234 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
236 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
238 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
239 src->cls.method_len);
240 ceph_pagelist_append(&req->r_trail, src->cls.indata,
241 src->cls.indata_len);
243 case CEPH_OSD_OP_STARTSYNC:
245 case CEPH_OSD_OP_NOTIFY_ACK:
246 case CEPH_OSD_OP_WATCH:
247 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
248 dst->watch.ver = cpu_to_le64(src->watch.ver);
249 dst->watch.flag = src->watch.flag;
252 pr_err("unrecognized osd opcode %d\n", src->op);
255 case CEPH_OSD_OP_MAPEXT:
256 case CEPH_OSD_OP_MASKTRUNC:
257 case CEPH_OSD_OP_SPARSE_READ:
258 case CEPH_OSD_OP_NOTIFY:
259 case CEPH_OSD_OP_ASSERT_VER:
260 case CEPH_OSD_OP_WRITEFULL:
261 case CEPH_OSD_OP_TRUNCATE:
262 case CEPH_OSD_OP_ZERO:
263 case CEPH_OSD_OP_DELETE:
264 case CEPH_OSD_OP_APPEND:
265 case CEPH_OSD_OP_SETTRUNC:
266 case CEPH_OSD_OP_TRIMTRUNC:
267 case CEPH_OSD_OP_TMAPUP:
268 case CEPH_OSD_OP_TMAPPUT:
269 case CEPH_OSD_OP_TMAPGET:
270 case CEPH_OSD_OP_CREATE:
271 case CEPH_OSD_OP_ROLLBACK:
272 case CEPH_OSD_OP_OMAPGETKEYS:
273 case CEPH_OSD_OP_OMAPGETVALS:
274 case CEPH_OSD_OP_OMAPGETHEADER:
275 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
276 case CEPH_OSD_OP_MODE_RD:
277 case CEPH_OSD_OP_OMAPSETVALS:
278 case CEPH_OSD_OP_OMAPSETHEADER:
279 case CEPH_OSD_OP_OMAPCLEAR:
280 case CEPH_OSD_OP_OMAPRMKEYS:
281 case CEPH_OSD_OP_OMAP_CMP:
282 case CEPH_OSD_OP_CLONERANGE:
283 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
284 case CEPH_OSD_OP_SRC_CMPXATTR:
285 case CEPH_OSD_OP_GETXATTR:
286 case CEPH_OSD_OP_GETXATTRS:
287 case CEPH_OSD_OP_CMPXATTR:
288 case CEPH_OSD_OP_SETXATTR:
289 case CEPH_OSD_OP_SETXATTRS:
290 case CEPH_OSD_OP_RESETXATTRS:
291 case CEPH_OSD_OP_RMXATTR:
292 case CEPH_OSD_OP_PULL:
293 case CEPH_OSD_OP_PUSH:
294 case CEPH_OSD_OP_BALANCEREADS:
295 case CEPH_OSD_OP_UNBALANCEREADS:
296 case CEPH_OSD_OP_SCRUB:
297 case CEPH_OSD_OP_SCRUB_RESERVE:
298 case CEPH_OSD_OP_SCRUB_UNRESERVE:
299 case CEPH_OSD_OP_SCRUB_STOP:
300 case CEPH_OSD_OP_SCRUB_MAP:
301 case CEPH_OSD_OP_WRLOCK:
302 case CEPH_OSD_OP_WRUNLOCK:
303 case CEPH_OSD_OP_RDLOCK:
304 case CEPH_OSD_OP_RDUNLOCK:
305 case CEPH_OSD_OP_UPLOCK:
306 case CEPH_OSD_OP_DNLOCK:
307 case CEPH_OSD_OP_PGLS:
308 case CEPH_OSD_OP_PGLS_FILTER:
309 pr_err("unsupported osd opcode %s\n",
310 ceph_osd_op_name(src->op));
314 dst->payload_len = cpu_to_le32(src->payload_len);
318 * build new request AND message
321 void ceph_osdc_build_request(struct ceph_osd_request *req,
322 u64 off, u64 len, unsigned int num_ops,
323 struct ceph_osd_req_op *src_ops,
324 struct ceph_snap_context *snapc, u64 snap_id,
325 struct timespec *mtime)
327 struct ceph_msg *msg = req->r_request;
328 struct ceph_osd_req_op *src_op;
331 int flags = req->r_flags;
335 req->r_num_ops = num_ops;
336 req->r_snapid = snap_id;
337 req->r_snapc = ceph_get_snap_context(snapc);
340 msg->hdr.version = cpu_to_le16(4);
342 p = msg->front.iov_base;
343 ceph_encode_32(&p, 1); /* client_inc is always 1 */
344 req->r_request_osdmap_epoch = p;
346 req->r_request_flags = p;
348 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
349 ceph_encode_timespec(p, mtime);
350 p += sizeof(struct ceph_timespec);
351 req->r_request_reassert_version = p;
352 p += sizeof(struct ceph_eversion); /* will get filled in */
355 ceph_encode_8(&p, 4);
356 ceph_encode_8(&p, 4);
357 ceph_encode_32(&p, 8 + 4 + 4);
358 req->r_request_pool = p;
360 ceph_encode_32(&p, -1); /* preferred */
361 ceph_encode_32(&p, 0); /* key len */
363 ceph_encode_8(&p, 1);
364 req->r_request_pgid = p;
366 ceph_encode_32(&p, -1); /* preferred */
369 ceph_encode_32(&p, req->r_oid_len);
370 memcpy(p, req->r_oid, req->r_oid_len);
371 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
375 ceph_encode_16(&p, num_ops);
377 req->r_request_ops = p;
378 for (i = 0; i < num_ops; i++, src_op++) {
379 osd_req_encode_op(req, p, src_op);
380 p += sizeof(struct ceph_osd_op);
384 ceph_encode_64(&p, req->r_snapid);
385 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
386 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
388 for (i = 0; i < snapc->num_snaps; i++) {
389 ceph_encode_64(&p, req->r_snapc->snaps[i]);
393 req->r_request_attempts = p;
396 data_len = req->r_trail.length;
397 if (flags & CEPH_OSD_FLAG_WRITE) {
398 req->r_request->hdr.data_off = cpu_to_le16(off);
401 req->r_request->hdr.data_len = cpu_to_le32(data_len);
403 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
404 msg_size = p - msg->front.iov_base;
405 msg->front.iov_len = msg_size;
406 msg->hdr.front_len = cpu_to_le32(msg_size);
408 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
412 EXPORT_SYMBOL(ceph_osdc_build_request);
415 * build new request AND message, calculate layout, and adjust file
418 * if the file was recently truncated, we include information about its
419 * old and new size so that the object can be updated appropriately. (we
420 * avoid synchronously deleting truncated objects because it's slow.)
422 * if @do_sync, include a 'startsync' command so that the osd will flush
425 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
426 struct ceph_file_layout *layout,
427 struct ceph_vino vino,
429 int opcode, int flags,
430 struct ceph_snap_context *snapc,
434 struct timespec *mtime,
438 struct ceph_osd_req_op ops[2];
439 struct ceph_osd_request *req;
440 unsigned int num_op = 1;
444 memset(&ops, 0, sizeof ops);
447 ops[0].extent.truncate_seq = truncate_seq;
448 ops[0].extent.truncate_size = truncate_size;
451 ops[1].op = CEPH_OSD_OP_STARTSYNC;
455 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
458 return ERR_PTR(-ENOMEM);
459 req->r_flags = flags;
461 /* calculate max write size */
462 r = calc_layout(layout, off, plen, ops, &bno);
464 ceph_osdc_put_request(req);
468 req->r_file_layout = *layout; /* keep a copy */
470 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
471 req->r_oid_len = strlen(req->r_oid);
473 /* The alignment may differ from the natural (file) alignment */
475 req->r_num_pages = calc_pages_for(page_align, *plen);
476 req->r_page_alignment = page_align;
478 ceph_osdc_build_request(req, off, *plen, num_op, ops,
479 snapc, vino.snap, mtime);
483 EXPORT_SYMBOL(ceph_osdc_new_request);
486 * We keep osd requests in an rbtree, sorted by ->r_tid.
488 static void __insert_request(struct ceph_osd_client *osdc,
489 struct ceph_osd_request *new)
491 struct rb_node **p = &osdc->requests.rb_node;
492 struct rb_node *parent = NULL;
493 struct ceph_osd_request *req = NULL;
497 req = rb_entry(parent, struct ceph_osd_request, r_node);
498 if (new->r_tid < req->r_tid)
500 else if (new->r_tid > req->r_tid)
506 rb_link_node(&new->r_node, parent, p);
507 rb_insert_color(&new->r_node, &osdc->requests);
510 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
513 struct ceph_osd_request *req;
514 struct rb_node *n = osdc->requests.rb_node;
517 req = rb_entry(n, struct ceph_osd_request, r_node);
518 if (tid < req->r_tid)
520 else if (tid > req->r_tid)
528 static struct ceph_osd_request *
529 __lookup_request_ge(struct ceph_osd_client *osdc,
532 struct ceph_osd_request *req;
533 struct rb_node *n = osdc->requests.rb_node;
536 req = rb_entry(n, struct ceph_osd_request, r_node);
537 if (tid < req->r_tid) {
541 } else if (tid > req->r_tid) {
551 * Resubmit requests pending on the given osd.
553 static void __kick_osd_requests(struct ceph_osd_client *osdc,
554 struct ceph_osd *osd)
556 struct ceph_osd_request *req, *nreq;
559 dout("__kick_osd_requests osd%d\n", osd->o_osd);
560 err = __reset_osd(osdc, osd);
564 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
565 list_move(&req->r_req_lru_item, &osdc->req_unsent);
566 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
569 req->r_flags |= CEPH_OSD_FLAG_RETRY;
572 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
575 * reregister request prior to unregistering linger so
576 * that r_osd is preserved.
578 BUG_ON(!list_empty(&req->r_req_lru_item));
579 __register_request(osdc, req);
580 list_add(&req->r_req_lru_item, &osdc->req_unsent);
581 list_add(&req->r_osd_item, &req->r_osd->o_requests);
582 __unregister_linger_request(osdc, req);
583 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
589 * If the osd connection drops, we need to resubmit all requests.
591 static void osd_reset(struct ceph_connection *con)
593 struct ceph_osd *osd = con->private;
594 struct ceph_osd_client *osdc;
598 dout("osd_reset osd%d\n", osd->o_osd);
600 down_read(&osdc->map_sem);
601 mutex_lock(&osdc->request_mutex);
602 __kick_osd_requests(osdc, osd);
604 mutex_unlock(&osdc->request_mutex);
605 up_read(&osdc->map_sem);
609 * Track open sessions with osds.
611 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
613 struct ceph_osd *osd;
615 osd = kzalloc(sizeof(*osd), GFP_NOFS);
619 atomic_set(&osd->o_ref, 1);
622 RB_CLEAR_NODE(&osd->o_node);
623 INIT_LIST_HEAD(&osd->o_requests);
624 INIT_LIST_HEAD(&osd->o_linger_requests);
625 INIT_LIST_HEAD(&osd->o_osd_lru);
626 osd->o_incarnation = 1;
628 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
630 INIT_LIST_HEAD(&osd->o_keepalive_item);
634 static struct ceph_osd *get_osd(struct ceph_osd *osd)
636 if (atomic_inc_not_zero(&osd->o_ref)) {
637 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
638 atomic_read(&osd->o_ref));
641 dout("get_osd %p FAIL\n", osd);
646 static void put_osd(struct ceph_osd *osd)
648 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
649 atomic_read(&osd->o_ref) - 1);
650 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
651 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
653 if (ac->ops && ac->ops->destroy_authorizer)
654 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
660 * remove an osd from our map
662 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
664 dout("__remove_osd %p\n", osd);
665 BUG_ON(!list_empty(&osd->o_requests));
666 rb_erase(&osd->o_node, &osdc->osds);
667 list_del_init(&osd->o_osd_lru);
668 ceph_con_close(&osd->o_con);
672 static void remove_all_osds(struct ceph_osd_client *osdc)
674 dout("%s %p\n", __func__, osdc);
675 mutex_lock(&osdc->request_mutex);
676 while (!RB_EMPTY_ROOT(&osdc->osds)) {
677 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
678 struct ceph_osd, o_node);
679 __remove_osd(osdc, osd);
681 mutex_unlock(&osdc->request_mutex);
684 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
685 struct ceph_osd *osd)
687 dout("__move_osd_to_lru %p\n", osd);
688 BUG_ON(!list_empty(&osd->o_osd_lru));
689 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
690 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
693 static void __remove_osd_from_lru(struct ceph_osd *osd)
695 dout("__remove_osd_from_lru %p\n", osd);
696 if (!list_empty(&osd->o_osd_lru))
697 list_del_init(&osd->o_osd_lru);
700 static void remove_old_osds(struct ceph_osd_client *osdc)
702 struct ceph_osd *osd, *nosd;
704 dout("__remove_old_osds %p\n", osdc);
705 mutex_lock(&osdc->request_mutex);
706 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
707 if (time_before(jiffies, osd->lru_ttl))
709 __remove_osd(osdc, osd);
711 mutex_unlock(&osdc->request_mutex);
717 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
719 struct ceph_entity_addr *peer_addr;
721 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
722 if (list_empty(&osd->o_requests) &&
723 list_empty(&osd->o_linger_requests)) {
724 __remove_osd(osdc, osd);
729 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
730 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
731 !ceph_con_opened(&osd->o_con)) {
732 struct ceph_osd_request *req;
734 dout(" osd addr hasn't changed and connection never opened,"
735 " letting msgr retry");
736 /* touch each r_stamp for handle_timeout()'s benfit */
737 list_for_each_entry(req, &osd->o_requests, r_osd_item)
738 req->r_stamp = jiffies;
743 ceph_con_close(&osd->o_con);
744 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
745 osd->o_incarnation++;
750 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
752 struct rb_node **p = &osdc->osds.rb_node;
753 struct rb_node *parent = NULL;
754 struct ceph_osd *osd = NULL;
756 dout("__insert_osd %p osd%d\n", new, new->o_osd);
759 osd = rb_entry(parent, struct ceph_osd, o_node);
760 if (new->o_osd < osd->o_osd)
762 else if (new->o_osd > osd->o_osd)
768 rb_link_node(&new->o_node, parent, p);
769 rb_insert_color(&new->o_node, &osdc->osds);
772 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
774 struct ceph_osd *osd;
775 struct rb_node *n = osdc->osds.rb_node;
778 osd = rb_entry(n, struct ceph_osd, o_node);
781 else if (o > osd->o_osd)
789 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
791 schedule_delayed_work(&osdc->timeout_work,
792 osdc->client->options->osd_keepalive_timeout * HZ);
795 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
797 cancel_delayed_work(&osdc->timeout_work);
801 * Register request, assign tid. If this is the first request, set up
804 static void __register_request(struct ceph_osd_client *osdc,
805 struct ceph_osd_request *req)
807 req->r_tid = ++osdc->last_tid;
808 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
809 dout("__register_request %p tid %lld\n", req, req->r_tid);
810 __insert_request(osdc, req);
811 ceph_osdc_get_request(req);
812 osdc->num_requests++;
813 if (osdc->num_requests == 1) {
814 dout(" first request, scheduling timeout\n");
815 __schedule_osd_timeout(osdc);
819 static void register_request(struct ceph_osd_client *osdc,
820 struct ceph_osd_request *req)
822 mutex_lock(&osdc->request_mutex);
823 __register_request(osdc, req);
824 mutex_unlock(&osdc->request_mutex);
828 * called under osdc->request_mutex
830 static void __unregister_request(struct ceph_osd_client *osdc,
831 struct ceph_osd_request *req)
833 if (RB_EMPTY_NODE(&req->r_node)) {
834 dout("__unregister_request %p tid %lld not registered\n",
839 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
840 rb_erase(&req->r_node, &osdc->requests);
841 osdc->num_requests--;
844 /* make sure the original request isn't in flight. */
845 ceph_msg_revoke(req->r_request);
847 list_del_init(&req->r_osd_item);
848 if (list_empty(&req->r_osd->o_requests) &&
849 list_empty(&req->r_osd->o_linger_requests)) {
850 dout("moving osd to %p lru\n", req->r_osd);
851 __move_osd_to_lru(osdc, req->r_osd);
853 if (list_empty(&req->r_linger_item))
857 list_del_init(&req->r_req_lru_item);
858 ceph_osdc_put_request(req);
860 if (osdc->num_requests == 0) {
861 dout(" no requests, canceling timeout\n");
862 __cancel_osd_timeout(osdc);
867 * Cancel a previously queued request message
869 static void __cancel_request(struct ceph_osd_request *req)
871 if (req->r_sent && req->r_osd) {
872 ceph_msg_revoke(req->r_request);
877 static void __register_linger_request(struct ceph_osd_client *osdc,
878 struct ceph_osd_request *req)
880 dout("__register_linger_request %p\n", req);
881 list_add_tail(&req->r_linger_item, &osdc->req_linger);
883 list_add_tail(&req->r_linger_osd,
884 &req->r_osd->o_linger_requests);
887 static void __unregister_linger_request(struct ceph_osd_client *osdc,
888 struct ceph_osd_request *req)
890 dout("__unregister_linger_request %p\n", req);
891 list_del_init(&req->r_linger_item);
893 list_del_init(&req->r_linger_osd);
895 if (list_empty(&req->r_osd->o_requests) &&
896 list_empty(&req->r_osd->o_linger_requests)) {
897 dout("moving osd to %p lru\n", req->r_osd);
898 __move_osd_to_lru(osdc, req->r_osd);
900 if (list_empty(&req->r_osd_item))
905 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
906 struct ceph_osd_request *req)
908 mutex_lock(&osdc->request_mutex);
910 __unregister_linger_request(osdc, req);
911 ceph_osdc_put_request(req);
913 mutex_unlock(&osdc->request_mutex);
915 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
917 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
918 struct ceph_osd_request *req)
920 if (!req->r_linger) {
921 dout("set_request_linger %p\n", req);
924 * caller is now responsible for calling
925 * unregister_linger_request
927 ceph_osdc_get_request(req);
930 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
933 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
934 * (as needed), and set the request r_osd appropriately. If there is
935 * no up osd, set r_osd to NULL. Move the request to the appropriate list
936 * (unsent, homeless) or leave on in-flight lru.
938 * Return 0 if unchanged, 1 if changed, or negative on error.
940 * Caller should hold map_sem for read and request_mutex.
942 static int __map_request(struct ceph_osd_client *osdc,
943 struct ceph_osd_request *req, int force_resend)
946 int acting[CEPH_PG_MAX_SIZE];
950 dout("map_request %p tid %lld\n", req, req->r_tid);
951 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
952 ceph_file_layout_pg_pool(req->r_file_layout));
954 list_move(&req->r_req_lru_item, &osdc->req_notarget);
959 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
965 if ((!force_resend &&
966 req->r_osd && req->r_osd->o_osd == o &&
967 req->r_sent >= req->r_osd->o_incarnation &&
968 req->r_num_pg_osds == num &&
969 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
970 (req->r_osd == NULL && o == -1))
971 return 0; /* no change */
973 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
974 req->r_tid, pgid.pool, pgid.seed, o,
975 req->r_osd ? req->r_osd->o_osd : -1);
977 /* record full pg acting set */
978 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
979 req->r_num_pg_osds = num;
982 __cancel_request(req);
983 list_del_init(&req->r_osd_item);
987 req->r_osd = __lookup_osd(osdc, o);
988 if (!req->r_osd && o >= 0) {
990 req->r_osd = create_osd(osdc, o);
992 list_move(&req->r_req_lru_item, &osdc->req_notarget);
996 dout("map_request osd %p is osd%d\n", req->r_osd, o);
997 __insert_osd(osdc, req->r_osd);
999 ceph_con_open(&req->r_osd->o_con,
1000 CEPH_ENTITY_TYPE_OSD, o,
1001 &osdc->osdmap->osd_addr[o]);
1005 __remove_osd_from_lru(req->r_osd);
1006 list_add(&req->r_osd_item, &req->r_osd->o_requests);
1007 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1009 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1011 err = 1; /* osd or pg changed */
1018 * caller should hold map_sem (for read) and request_mutex
1020 static void __send_request(struct ceph_osd_client *osdc,
1021 struct ceph_osd_request *req)
1025 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1026 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1027 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1029 /* fill in message content that changes each time we send it */
1030 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1031 put_unaligned_le32(req->r_flags, req->r_request_flags);
1032 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1033 p = req->r_request_pgid;
1034 ceph_encode_64(&p, req->r_pgid.pool);
1035 ceph_encode_32(&p, req->r_pgid.seed);
1036 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1037 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1038 sizeof(req->r_reassert_version));
1040 req->r_stamp = jiffies;
1041 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1043 ceph_msg_get(req->r_request); /* send consumes a ref */
1044 ceph_con_send(&req->r_osd->o_con, req->r_request);
1045 req->r_sent = req->r_osd->o_incarnation;
1049 * Send any requests in the queue (req_unsent).
1051 static void __send_queued(struct ceph_osd_client *osdc)
1053 struct ceph_osd_request *req, *tmp;
1055 dout("__send_queued\n");
1056 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1057 __send_request(osdc, req);
1061 * Timeout callback, called every N seconds when 1 or more osd
1062 * requests has been active for more than N seconds. When this
1063 * happens, we ping all OSDs with requests who have timed out to
1064 * ensure any communications channel reset is detected. Reset the
1065 * request timeouts another N seconds in the future as we go.
1066 * Reschedule the timeout event another N seconds in future (unless
1067 * there are no open requests).
1069 static void handle_timeout(struct work_struct *work)
1071 struct ceph_osd_client *osdc =
1072 container_of(work, struct ceph_osd_client, timeout_work.work);
1073 struct ceph_osd_request *req;
1074 struct ceph_osd *osd;
1075 unsigned long keepalive =
1076 osdc->client->options->osd_keepalive_timeout * HZ;
1077 struct list_head slow_osds;
1079 down_read(&osdc->map_sem);
1081 ceph_monc_request_next_osdmap(&osdc->client->monc);
1083 mutex_lock(&osdc->request_mutex);
1086 * ping osds that are a bit slow. this ensures that if there
1087 * is a break in the TCP connection we will notice, and reopen
1088 * a connection with that osd (from the fault callback).
1090 INIT_LIST_HEAD(&slow_osds);
1091 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1092 if (time_before(jiffies, req->r_stamp + keepalive))
1097 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1098 req->r_tid, osd->o_osd);
1099 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1101 while (!list_empty(&slow_osds)) {
1102 osd = list_entry(slow_osds.next, struct ceph_osd,
1104 list_del_init(&osd->o_keepalive_item);
1105 ceph_con_keepalive(&osd->o_con);
1108 __schedule_osd_timeout(osdc);
1109 __send_queued(osdc);
1110 mutex_unlock(&osdc->request_mutex);
1111 up_read(&osdc->map_sem);
1114 static void handle_osds_timeout(struct work_struct *work)
1116 struct ceph_osd_client *osdc =
1117 container_of(work, struct ceph_osd_client,
1118 osds_timeout_work.work);
1119 unsigned long delay =
1120 osdc->client->options->osd_idle_ttl * HZ >> 2;
1122 dout("osds timeout\n");
1123 down_read(&osdc->map_sem);
1124 remove_old_osds(osdc);
1125 up_read(&osdc->map_sem);
1127 schedule_delayed_work(&osdc->osds_timeout_work,
1128 round_jiffies_relative(delay));
1131 static void complete_request(struct ceph_osd_request *req)
1133 if (req->r_safe_callback)
1134 req->r_safe_callback(req, NULL);
1135 complete_all(&req->r_safe_completion); /* fsync waiter */
1138 static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid)
1142 ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad);
1143 v = ceph_decode_8(p);
1145 pr_warning("do not understand pg encoding %d > 1", v);
1148 pgid->pool = ceph_decode_64(p);
1149 pgid->seed = ceph_decode_32(p);
1154 pr_warning("incomplete pg encoding");
1159 * handle osd op reply. either call the callback if it is specified,
1160 * or do the completion to wake up the waiting thread.
1162 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1163 struct ceph_connection *con)
1166 struct ceph_osd_request *req;
1169 int numops, payload_len, flags;
1175 u64 reassert_version;
1177 int already_completed;
1180 tid = le64_to_cpu(msg->hdr.tid);
1181 dout("handle_reply %p tid %llu\n", msg, tid);
1183 p = msg->front.iov_base;
1184 end = p + msg->front.iov_len;
1186 ceph_decode_need(&p, end, 4, bad);
1187 object_len = ceph_decode_32(&p);
1188 ceph_decode_need(&p, end, object_len, bad);
1191 err = __decode_pgid(&p, end, &pg);
1195 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1196 flags = ceph_decode_64(&p);
1197 result = ceph_decode_32(&p);
1198 reassert_epoch = ceph_decode_32(&p);
1199 reassert_version = ceph_decode_64(&p);
1200 osdmap_epoch = ceph_decode_32(&p);
1203 mutex_lock(&osdc->request_mutex);
1204 req = __lookup_request(osdc, tid);
1206 dout("handle_reply tid %llu dne\n", tid);
1207 mutex_unlock(&osdc->request_mutex);
1210 ceph_osdc_get_request(req);
1212 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1215 ceph_decode_need(&p, end, 4, bad);
1216 numops = ceph_decode_32(&p);
1217 if (numops > CEPH_OSD_MAX_OP)
1219 if (numops != req->r_num_ops)
1222 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1223 for (i = 0; i < numops; i++) {
1224 struct ceph_osd_op *op = p;
1227 len = le32_to_cpu(op->payload_len);
1228 req->r_reply_op_len[i] = len;
1229 dout(" op %d has %d bytes\n", i, len);
1233 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1234 pr_warning("sum of op payload lens %d != data_len %d",
1235 payload_len, le32_to_cpu(msg->hdr.data_len));
1239 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1240 retry_attempt = ceph_decode_32(&p);
1241 for (i = 0; i < numops; i++)
1242 req->r_reply_op_result[i] = ceph_decode_32(&p);
1245 * if this connection filled our message, drop our reference now, to
1246 * avoid a (safe but slower) revoke later.
1248 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1249 dout(" dropping con_filling_msg ref %p\n", con);
1250 req->r_con_filling_msg = NULL;
1254 if (!req->r_got_reply) {
1257 req->r_result = result;
1258 bytes = le32_to_cpu(msg->hdr.data_len);
1259 dout("handle_reply result %d bytes %d\n", req->r_result,
1261 if (req->r_result == 0)
1262 req->r_result = bytes;
1264 /* in case this is a write and we need to replay, */
1265 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1266 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1268 req->r_got_reply = 1;
1269 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1270 dout("handle_reply tid %llu dup ack\n", tid);
1271 mutex_unlock(&osdc->request_mutex);
1275 dout("handle_reply tid %llu flags %d\n", tid, flags);
1277 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1278 __register_linger_request(osdc, req);
1280 /* either this is a read, or we got the safe response */
1282 (flags & CEPH_OSD_FLAG_ONDISK) ||
1283 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1284 __unregister_request(osdc, req);
1286 already_completed = req->r_completed;
1287 req->r_completed = 1;
1288 mutex_unlock(&osdc->request_mutex);
1289 if (already_completed)
1292 if (req->r_callback)
1293 req->r_callback(req, msg);
1295 complete_all(&req->r_completion);
1297 if (flags & CEPH_OSD_FLAG_ONDISK)
1298 complete_request(req);
1301 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1302 ceph_osdc_put_request(req);
1306 ceph_osdc_put_request(req);
1308 pr_err("corrupt osd_op_reply got %d %d\n",
1309 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1313 static void reset_changed_osds(struct ceph_osd_client *osdc)
1315 struct rb_node *p, *n;
1317 for (p = rb_first(&osdc->osds); p; p = n) {
1318 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1321 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1322 memcmp(&osd->o_con.peer_addr,
1323 ceph_osd_addr(osdc->osdmap,
1325 sizeof(struct ceph_entity_addr)) != 0)
1326 __reset_osd(osdc, osd);
1331 * Requeue requests whose mapping to an OSD has changed. If requests map to
1332 * no osd, request a new map.
1334 * Caller should hold map_sem for read.
1336 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1338 struct ceph_osd_request *req, *nreq;
1343 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1344 mutex_lock(&osdc->request_mutex);
1345 for (p = rb_first(&osdc->requests); p; ) {
1346 req = rb_entry(p, struct ceph_osd_request, r_node);
1350 * For linger requests that have not yet been
1351 * registered, move them to the linger list; they'll
1352 * be sent to the osd in the loop below. Unregister
1353 * the request before re-registering it as a linger
1354 * request to ensure the __map_request() below
1355 * will decide it needs to be sent.
1357 if (req->r_linger && list_empty(&req->r_linger_item)) {
1358 dout("%p tid %llu restart on osd%d\n",
1360 req->r_osd ? req->r_osd->o_osd : -1);
1361 __unregister_request(osdc, req);
1362 __register_linger_request(osdc, req);
1366 err = __map_request(osdc, req, force_resend);
1368 continue; /* error */
1369 if (req->r_osd == NULL) {
1370 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1371 needmap++; /* request a newer map */
1372 } else if (err > 0) {
1373 if (!req->r_linger) {
1374 dout("%p tid %llu requeued on osd%d\n", req,
1376 req->r_osd ? req->r_osd->o_osd : -1);
1377 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1382 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1384 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1386 err = __map_request(osdc, req, force_resend);
1387 dout("__map_request returned %d\n", err);
1389 continue; /* no change and no osd was specified */
1391 continue; /* hrm! */
1392 if (req->r_osd == NULL) {
1393 dout("tid %llu maps to no valid osd\n", req->r_tid);
1394 needmap++; /* request a newer map */
1398 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1399 req->r_osd ? req->r_osd->o_osd : -1);
1400 __register_request(osdc, req);
1401 __unregister_linger_request(osdc, req);
1403 mutex_unlock(&osdc->request_mutex);
1406 dout("%d requests for down osds, need new map\n", needmap);
1407 ceph_monc_request_next_osdmap(&osdc->client->monc);
1409 reset_changed_osds(osdc);
1414 * Process updated osd map.
1416 * The message contains any number of incremental and full maps, normally
1417 * indicating some sort of topology change in the cluster. Kick requests
1418 * off to different OSDs as needed.
1420 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1422 void *p, *end, *next;
1423 u32 nr_maps, maplen;
1425 struct ceph_osdmap *newmap = NULL, *oldmap;
1427 struct ceph_fsid fsid;
1429 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1430 p = msg->front.iov_base;
1431 end = p + msg->front.iov_len;
1434 ceph_decode_need(&p, end, sizeof(fsid), bad);
1435 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1436 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1439 down_write(&osdc->map_sem);
1441 /* incremental maps */
1442 ceph_decode_32_safe(&p, end, nr_maps, bad);
1443 dout(" %d inc maps\n", nr_maps);
1444 while (nr_maps > 0) {
1445 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1446 epoch = ceph_decode_32(&p);
1447 maplen = ceph_decode_32(&p);
1448 ceph_decode_need(&p, end, maplen, bad);
1450 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1451 dout("applying incremental map %u len %d\n",
1453 newmap = osdmap_apply_incremental(&p, next,
1455 &osdc->client->msgr);
1456 if (IS_ERR(newmap)) {
1457 err = PTR_ERR(newmap);
1461 if (newmap != osdc->osdmap) {
1462 ceph_osdmap_destroy(osdc->osdmap);
1463 osdc->osdmap = newmap;
1465 kick_requests(osdc, 0);
1467 dout("ignoring incremental map %u len %d\n",
1477 ceph_decode_32_safe(&p, end, nr_maps, bad);
1478 dout(" %d full maps\n", nr_maps);
1480 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1481 epoch = ceph_decode_32(&p);
1482 maplen = ceph_decode_32(&p);
1483 ceph_decode_need(&p, end, maplen, bad);
1485 dout("skipping non-latest full map %u len %d\n",
1487 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1488 dout("skipping full map %u len %d, "
1489 "older than our %u\n", epoch, maplen,
1490 osdc->osdmap->epoch);
1492 int skipped_map = 0;
1494 dout("taking full map %u len %d\n", epoch, maplen);
1495 newmap = osdmap_decode(&p, p+maplen);
1496 if (IS_ERR(newmap)) {
1497 err = PTR_ERR(newmap);
1501 oldmap = osdc->osdmap;
1502 osdc->osdmap = newmap;
1504 if (oldmap->epoch + 1 < newmap->epoch)
1506 ceph_osdmap_destroy(oldmap);
1508 kick_requests(osdc, skipped_map);
1515 downgrade_write(&osdc->map_sem);
1516 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1519 * subscribe to subsequent osdmap updates if full to ensure
1520 * we find out when we are no longer full and stop returning
1523 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1524 ceph_monc_request_next_osdmap(&osdc->client->monc);
1526 mutex_lock(&osdc->request_mutex);
1527 __send_queued(osdc);
1528 mutex_unlock(&osdc->request_mutex);
1529 up_read(&osdc->map_sem);
1530 wake_up_all(&osdc->client->auth_wq);
1534 pr_err("osdc handle_map corrupt msg\n");
1536 up_write(&osdc->map_sem);
1541 * watch/notify callback event infrastructure
1543 * These callbacks are used both for watch and notify operations.
1545 static void __release_event(struct kref *kref)
1547 struct ceph_osd_event *event =
1548 container_of(kref, struct ceph_osd_event, kref);
1550 dout("__release_event %p\n", event);
1554 static void get_event(struct ceph_osd_event *event)
1556 kref_get(&event->kref);
1559 void ceph_osdc_put_event(struct ceph_osd_event *event)
1561 kref_put(&event->kref, __release_event);
1563 EXPORT_SYMBOL(ceph_osdc_put_event);
1565 static void __insert_event(struct ceph_osd_client *osdc,
1566 struct ceph_osd_event *new)
1568 struct rb_node **p = &osdc->event_tree.rb_node;
1569 struct rb_node *parent = NULL;
1570 struct ceph_osd_event *event = NULL;
1574 event = rb_entry(parent, struct ceph_osd_event, node);
1575 if (new->cookie < event->cookie)
1577 else if (new->cookie > event->cookie)
1578 p = &(*p)->rb_right;
1583 rb_link_node(&new->node, parent, p);
1584 rb_insert_color(&new->node, &osdc->event_tree);
1587 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1590 struct rb_node **p = &osdc->event_tree.rb_node;
1591 struct rb_node *parent = NULL;
1592 struct ceph_osd_event *event = NULL;
1596 event = rb_entry(parent, struct ceph_osd_event, node);
1597 if (cookie < event->cookie)
1599 else if (cookie > event->cookie)
1600 p = &(*p)->rb_right;
1607 static void __remove_event(struct ceph_osd_event *event)
1609 struct ceph_osd_client *osdc = event->osdc;
1611 if (!RB_EMPTY_NODE(&event->node)) {
1612 dout("__remove_event removed %p\n", event);
1613 rb_erase(&event->node, &osdc->event_tree);
1614 ceph_osdc_put_event(event);
1616 dout("__remove_event didn't remove %p\n", event);
1620 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1621 void (*event_cb)(u64, u64, u8, void *),
1622 void *data, struct ceph_osd_event **pevent)
1624 struct ceph_osd_event *event;
1626 event = kmalloc(sizeof(*event), GFP_NOIO);
1630 dout("create_event %p\n", event);
1631 event->cb = event_cb;
1632 event->one_shot = 0;
1635 INIT_LIST_HEAD(&event->osd_node);
1636 RB_CLEAR_NODE(&event->node);
1637 kref_init(&event->kref); /* one ref for us */
1638 kref_get(&event->kref); /* one ref for the caller */
1640 spin_lock(&osdc->event_lock);
1641 event->cookie = ++osdc->event_count;
1642 __insert_event(osdc, event);
1643 spin_unlock(&osdc->event_lock);
1648 EXPORT_SYMBOL(ceph_osdc_create_event);
1650 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1652 struct ceph_osd_client *osdc = event->osdc;
1654 dout("cancel_event %p\n", event);
1655 spin_lock(&osdc->event_lock);
1656 __remove_event(event);
1657 spin_unlock(&osdc->event_lock);
1658 ceph_osdc_put_event(event); /* caller's */
1660 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1663 static void do_event_work(struct work_struct *work)
1665 struct ceph_osd_event_work *event_work =
1666 container_of(work, struct ceph_osd_event_work, work);
1667 struct ceph_osd_event *event = event_work->event;
1668 u64 ver = event_work->ver;
1669 u64 notify_id = event_work->notify_id;
1670 u8 opcode = event_work->opcode;
1672 dout("do_event_work completing %p\n", event);
1673 event->cb(ver, notify_id, opcode, event->data);
1674 dout("do_event_work completed %p\n", event);
1675 ceph_osdc_put_event(event);
1681 * Process osd watch notifications
1683 static void handle_watch_notify(struct ceph_osd_client *osdc,
1684 struct ceph_msg *msg)
1688 u64 cookie, ver, notify_id;
1690 struct ceph_osd_event *event;
1691 struct ceph_osd_event_work *event_work;
1693 p = msg->front.iov_base;
1694 end = p + msg->front.iov_len;
1696 ceph_decode_8_safe(&p, end, proto_ver, bad);
1697 ceph_decode_8_safe(&p, end, opcode, bad);
1698 ceph_decode_64_safe(&p, end, cookie, bad);
1699 ceph_decode_64_safe(&p, end, ver, bad);
1700 ceph_decode_64_safe(&p, end, notify_id, bad);
1702 spin_lock(&osdc->event_lock);
1703 event = __find_event(osdc, cookie);
1705 BUG_ON(event->one_shot);
1708 spin_unlock(&osdc->event_lock);
1709 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1710 cookie, ver, event);
1712 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1714 dout("ERROR: could not allocate event_work\n");
1717 INIT_WORK(&event_work->work, do_event_work);
1718 event_work->event = event;
1719 event_work->ver = ver;
1720 event_work->notify_id = notify_id;
1721 event_work->opcode = opcode;
1722 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1723 dout("WARNING: failed to queue notify event work\n");
1731 ceph_osdc_put_event(event);
1735 pr_err("osdc handle_watch_notify corrupt msg\n");
1740 * Register request, send initial attempt.
1742 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1743 struct ceph_osd_request *req,
1748 req->r_request->pages = req->r_pages;
1749 req->r_request->page_count = req->r_num_pages;
1750 req->r_request->page_alignment = req->r_page_alignment;
1752 req->r_request->bio = req->r_bio;
1754 req->r_request->trail = &req->r_trail;
1756 register_request(osdc, req);
1758 down_read(&osdc->map_sem);
1759 mutex_lock(&osdc->request_mutex);
1761 * a racing kick_requests() may have sent the message for us
1762 * while we dropped request_mutex above, so only send now if
1763 * the request still han't been touched yet.
1765 if (req->r_sent == 0) {
1766 rc = __map_request(osdc, req, 0);
1769 dout("osdc_start_request failed map, "
1770 " will retry %lld\n", req->r_tid);
1775 if (req->r_osd == NULL) {
1776 dout("send_request %p no up osds in pg\n", req);
1777 ceph_monc_request_next_osdmap(&osdc->client->monc);
1779 __send_request(osdc, req);
1785 mutex_unlock(&osdc->request_mutex);
1786 up_read(&osdc->map_sem);
1789 EXPORT_SYMBOL(ceph_osdc_start_request);
1792 * wait for a request to complete
1794 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1795 struct ceph_osd_request *req)
1799 rc = wait_for_completion_interruptible(&req->r_completion);
1801 mutex_lock(&osdc->request_mutex);
1802 __cancel_request(req);
1803 __unregister_request(osdc, req);
1804 mutex_unlock(&osdc->request_mutex);
1805 complete_request(req);
1806 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1810 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1811 return req->r_result;
1813 EXPORT_SYMBOL(ceph_osdc_wait_request);
1816 * sync - wait for all in-flight requests to flush. avoid starvation.
1818 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1820 struct ceph_osd_request *req;
1821 u64 last_tid, next_tid = 0;
1823 mutex_lock(&osdc->request_mutex);
1824 last_tid = osdc->last_tid;
1826 req = __lookup_request_ge(osdc, next_tid);
1829 if (req->r_tid > last_tid)
1832 next_tid = req->r_tid + 1;
1833 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1836 ceph_osdc_get_request(req);
1837 mutex_unlock(&osdc->request_mutex);
1838 dout("sync waiting on tid %llu (last is %llu)\n",
1839 req->r_tid, last_tid);
1840 wait_for_completion(&req->r_safe_completion);
1841 mutex_lock(&osdc->request_mutex);
1842 ceph_osdc_put_request(req);
1844 mutex_unlock(&osdc->request_mutex);
1845 dout("sync done (thru tid %llu)\n", last_tid);
1847 EXPORT_SYMBOL(ceph_osdc_sync);
1852 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1857 osdc->client = client;
1858 osdc->osdmap = NULL;
1859 init_rwsem(&osdc->map_sem);
1860 init_completion(&osdc->map_waiters);
1861 osdc->last_requested_map = 0;
1862 mutex_init(&osdc->request_mutex);
1864 osdc->osds = RB_ROOT;
1865 INIT_LIST_HEAD(&osdc->osd_lru);
1866 osdc->requests = RB_ROOT;
1867 INIT_LIST_HEAD(&osdc->req_lru);
1868 INIT_LIST_HEAD(&osdc->req_unsent);
1869 INIT_LIST_HEAD(&osdc->req_notarget);
1870 INIT_LIST_HEAD(&osdc->req_linger);
1871 osdc->num_requests = 0;
1872 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1873 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1874 spin_lock_init(&osdc->event_lock);
1875 osdc->event_tree = RB_ROOT;
1876 osdc->event_count = 0;
1878 schedule_delayed_work(&osdc->osds_timeout_work,
1879 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1882 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1883 sizeof(struct ceph_osd_request));
1884 if (!osdc->req_mempool)
1887 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1888 OSD_OP_FRONT_LEN, 10, true,
1892 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1893 OSD_OPREPLY_FRONT_LEN, 10, true,
1898 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1899 if (IS_ERR(osdc->notify_wq)) {
1900 err = PTR_ERR(osdc->notify_wq);
1901 osdc->notify_wq = NULL;
1907 ceph_msgpool_destroy(&osdc->msgpool_op);
1909 mempool_destroy(osdc->req_mempool);
1914 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1916 flush_workqueue(osdc->notify_wq);
1917 destroy_workqueue(osdc->notify_wq);
1918 cancel_delayed_work_sync(&osdc->timeout_work);
1919 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1921 ceph_osdmap_destroy(osdc->osdmap);
1922 osdc->osdmap = NULL;
1924 remove_all_osds(osdc);
1925 mempool_destroy(osdc->req_mempool);
1926 ceph_msgpool_destroy(&osdc->msgpool_op);
1927 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1931 * Read some contiguous pages. If we cross a stripe boundary, shorten
1932 * *plen. Return number of bytes read, or error.
1934 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1935 struct ceph_vino vino, struct ceph_file_layout *layout,
1937 u32 truncate_seq, u64 truncate_size,
1938 struct page **pages, int num_pages, int page_align)
1940 struct ceph_osd_request *req;
1943 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1944 vino.snap, off, *plen);
1945 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1946 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1947 NULL, 0, truncate_seq, truncate_size, NULL,
1950 return PTR_ERR(req);
1952 /* it may be a short read due to an object boundary */
1953 req->r_pages = pages;
1955 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1956 off, *plen, req->r_num_pages, page_align);
1958 rc = ceph_osdc_start_request(osdc, req, false);
1960 rc = ceph_osdc_wait_request(osdc, req);
1962 ceph_osdc_put_request(req);
1963 dout("readpages result %d\n", rc);
1966 EXPORT_SYMBOL(ceph_osdc_readpages);
1969 * do a synchronous write on N pages
1971 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1972 struct ceph_file_layout *layout,
1973 struct ceph_snap_context *snapc,
1975 u32 truncate_seq, u64 truncate_size,
1976 struct timespec *mtime,
1977 struct page **pages, int num_pages)
1979 struct ceph_osd_request *req;
1981 int page_align = off & ~PAGE_MASK;
1983 BUG_ON(vino.snap != CEPH_NOSNAP);
1984 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1986 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1988 truncate_seq, truncate_size, mtime,
1991 return PTR_ERR(req);
1993 /* it may be a short write due to an object boundary */
1994 req->r_pages = pages;
1995 dout("writepages %llu~%llu (%d pages)\n", off, len,
1998 rc = ceph_osdc_start_request(osdc, req, true);
2000 rc = ceph_osdc_wait_request(osdc, req);
2002 ceph_osdc_put_request(req);
2005 dout("writepages result %d\n", rc);
2008 EXPORT_SYMBOL(ceph_osdc_writepages);
2011 * handle incoming message
2013 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2015 struct ceph_osd *osd = con->private;
2016 struct ceph_osd_client *osdc;
2017 int type = le16_to_cpu(msg->hdr.type);
2024 case CEPH_MSG_OSD_MAP:
2025 ceph_osdc_handle_map(osdc, msg);
2027 case CEPH_MSG_OSD_OPREPLY:
2028 handle_reply(osdc, msg, con);
2030 case CEPH_MSG_WATCH_NOTIFY:
2031 handle_watch_notify(osdc, msg);
2035 pr_err("received unknown message type %d %s\n", type,
2036 ceph_msg_type_name(type));
2043 * lookup and return message for incoming reply. set up reply message
2046 static struct ceph_msg *get_reply(struct ceph_connection *con,
2047 struct ceph_msg_header *hdr,
2050 struct ceph_osd *osd = con->private;
2051 struct ceph_osd_client *osdc = osd->o_osdc;
2053 struct ceph_osd_request *req;
2054 int front = le32_to_cpu(hdr->front_len);
2055 int data_len = le32_to_cpu(hdr->data_len);
2058 tid = le64_to_cpu(hdr->tid);
2059 mutex_lock(&osdc->request_mutex);
2060 req = __lookup_request(osdc, tid);
2064 dout("get_reply unknown tid %llu from osd%d\n", tid,
2069 if (req->r_con_filling_msg) {
2070 dout("%s revoking msg %p from old con %p\n", __func__,
2071 req->r_reply, req->r_con_filling_msg);
2072 ceph_msg_revoke_incoming(req->r_reply);
2073 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2074 req->r_con_filling_msg = NULL;
2077 if (front > req->r_reply->front.iov_len) {
2078 pr_warning("get_reply front %d > preallocated %d\n",
2079 front, (int)req->r_reply->front.iov_len);
2080 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2083 ceph_msg_put(req->r_reply);
2086 m = ceph_msg_get(req->r_reply);
2089 int want = calc_pages_for(req->r_page_alignment, data_len);
2091 if (req->r_pages && unlikely(req->r_num_pages < want)) {
2092 pr_warning("tid %lld reply has %d bytes %d pages, we"
2093 " had only %d pages ready\n", tid, data_len,
2094 want, req->r_num_pages);
2100 m->pages = req->r_pages;
2101 m->page_count = req->r_num_pages;
2102 m->page_alignment = req->r_page_alignment;
2104 m->bio = req->r_bio;
2108 req->r_con_filling_msg = con->ops->get(con);
2109 dout("get_reply tid %lld %p\n", tid, m);
2112 mutex_unlock(&osdc->request_mutex);
2117 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2118 struct ceph_msg_header *hdr,
2121 struct ceph_osd *osd = con->private;
2122 int type = le16_to_cpu(hdr->type);
2123 int front = le32_to_cpu(hdr->front_len);
2127 case CEPH_MSG_OSD_MAP:
2128 case CEPH_MSG_WATCH_NOTIFY:
2129 return ceph_msg_new(type, front, GFP_NOFS, false);
2130 case CEPH_MSG_OSD_OPREPLY:
2131 return get_reply(con, hdr, skip);
2133 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2141 * Wrappers to refcount containing ceph_osd struct
2143 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2145 struct ceph_osd *osd = con->private;
2151 static void put_osd_con(struct ceph_connection *con)
2153 struct ceph_osd *osd = con->private;
2161 * Note: returned pointer is the address of a structure that's
2162 * managed separately. Caller must *not* attempt to free it.
2164 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2165 int *proto, int force_new)
2167 struct ceph_osd *o = con->private;
2168 struct ceph_osd_client *osdc = o->o_osdc;
2169 struct ceph_auth_client *ac = osdc->client->monc.auth;
2170 struct ceph_auth_handshake *auth = &o->o_auth;
2172 if (force_new && auth->authorizer) {
2173 if (ac->ops && ac->ops->destroy_authorizer)
2174 ac->ops->destroy_authorizer(ac, auth->authorizer);
2175 auth->authorizer = NULL;
2177 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2178 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2181 return ERR_PTR(ret);
2183 *proto = ac->protocol;
2189 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2191 struct ceph_osd *o = con->private;
2192 struct ceph_osd_client *osdc = o->o_osdc;
2193 struct ceph_auth_client *ac = osdc->client->monc.auth;
2196 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2197 * XXX which do we do: succeed or fail?
2199 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2202 static int invalidate_authorizer(struct ceph_connection *con)
2204 struct ceph_osd *o = con->private;
2205 struct ceph_osd_client *osdc = o->o_osdc;
2206 struct ceph_auth_client *ac = osdc->client->monc.auth;
2208 if (ac->ops && ac->ops->invalidate_authorizer)
2209 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2211 return ceph_monc_validate_auth(&osdc->client->monc);
2214 static const struct ceph_connection_operations osd_con_ops = {
2217 .dispatch = dispatch,
2218 .get_authorizer = get_authorizer,
2219 .verify_authorizer_reply = verify_authorizer_reply,
2220 .invalidate_authorizer = invalidate_authorizer,
2221 .alloc_msg = alloc_msg,