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);
141 ceph_pagelist_release(&req->r_trail);
143 mempool_free(req, req->r_osdc->req_mempool);
147 EXPORT_SYMBOL(ceph_osdc_release_request);
149 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
150 struct ceph_snap_context *snapc,
151 unsigned int num_ops,
155 struct ceph_osd_request *req;
156 struct ceph_msg *msg;
159 msg_size = 4 + 4 + 8 + 8 + 4+8;
160 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
161 msg_size += 1 + 8 + 4 + 4; /* pg_t */
162 msg_size += 4 + MAX_OBJ_NAME_SIZE;
163 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
164 msg_size += 8; /* snapid */
165 msg_size += 8; /* snap_seq */
166 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
170 req = mempool_alloc(osdc->req_mempool, gfp_flags);
171 memset(req, 0, sizeof(*req));
173 req = kzalloc(sizeof(*req), gfp_flags);
179 req->r_mempool = use_mempool;
181 kref_init(&req->r_kref);
182 init_completion(&req->r_completion);
183 init_completion(&req->r_safe_completion);
184 RB_CLEAR_NODE(&req->r_node);
185 INIT_LIST_HEAD(&req->r_unsafe_item);
186 INIT_LIST_HEAD(&req->r_linger_item);
187 INIT_LIST_HEAD(&req->r_linger_osd);
188 INIT_LIST_HEAD(&req->r_req_lru_item);
189 INIT_LIST_HEAD(&req->r_osd_item);
191 /* create reply message */
193 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
195 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
196 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
198 ceph_osdc_put_request(req);
203 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
204 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
205 ceph_pagelist_init(&req->r_trail);
207 /* create request message; allow space for oid */
209 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
211 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
213 ceph_osdc_put_request(req);
217 memset(msg->front.iov_base, 0, msg->front.iov_len);
219 req->r_request = msg;
223 EXPORT_SYMBOL(ceph_osdc_alloc_request);
225 static void osd_req_encode_op(struct ceph_osd_request *req,
226 struct ceph_osd_op *dst,
227 struct ceph_osd_req_op *src)
229 dst->op = cpu_to_le16(src->op);
232 case CEPH_OSD_OP_STAT:
234 case CEPH_OSD_OP_READ:
235 case CEPH_OSD_OP_WRITE:
237 cpu_to_le64(src->extent.offset);
239 cpu_to_le64(src->extent.length);
240 dst->extent.truncate_size =
241 cpu_to_le64(src->extent.truncate_size);
242 dst->extent.truncate_seq =
243 cpu_to_le32(src->extent.truncate_seq);
245 case CEPH_OSD_OP_CALL:
246 dst->cls.class_len = src->cls.class_len;
247 dst->cls.method_len = src->cls.method_len;
248 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
250 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
252 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
253 src->cls.method_len);
254 ceph_pagelist_append(&req->r_trail, src->cls.indata,
255 src->cls.indata_len);
257 case CEPH_OSD_OP_STARTSYNC:
259 case CEPH_OSD_OP_NOTIFY_ACK:
260 case CEPH_OSD_OP_WATCH:
261 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
262 dst->watch.ver = cpu_to_le64(src->watch.ver);
263 dst->watch.flag = src->watch.flag;
266 pr_err("unrecognized osd opcode %d\n", src->op);
269 case CEPH_OSD_OP_MAPEXT:
270 case CEPH_OSD_OP_MASKTRUNC:
271 case CEPH_OSD_OP_SPARSE_READ:
272 case CEPH_OSD_OP_NOTIFY:
273 case CEPH_OSD_OP_ASSERT_VER:
274 case CEPH_OSD_OP_WRITEFULL:
275 case CEPH_OSD_OP_TRUNCATE:
276 case CEPH_OSD_OP_ZERO:
277 case CEPH_OSD_OP_DELETE:
278 case CEPH_OSD_OP_APPEND:
279 case CEPH_OSD_OP_SETTRUNC:
280 case CEPH_OSD_OP_TRIMTRUNC:
281 case CEPH_OSD_OP_TMAPUP:
282 case CEPH_OSD_OP_TMAPPUT:
283 case CEPH_OSD_OP_TMAPGET:
284 case CEPH_OSD_OP_CREATE:
285 case CEPH_OSD_OP_ROLLBACK:
286 case CEPH_OSD_OP_OMAPGETKEYS:
287 case CEPH_OSD_OP_OMAPGETVALS:
288 case CEPH_OSD_OP_OMAPGETHEADER:
289 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
290 case CEPH_OSD_OP_MODE_RD:
291 case CEPH_OSD_OP_OMAPSETVALS:
292 case CEPH_OSD_OP_OMAPSETHEADER:
293 case CEPH_OSD_OP_OMAPCLEAR:
294 case CEPH_OSD_OP_OMAPRMKEYS:
295 case CEPH_OSD_OP_OMAP_CMP:
296 case CEPH_OSD_OP_CLONERANGE:
297 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
298 case CEPH_OSD_OP_SRC_CMPXATTR:
299 case CEPH_OSD_OP_GETXATTR:
300 case CEPH_OSD_OP_GETXATTRS:
301 case CEPH_OSD_OP_CMPXATTR:
302 case CEPH_OSD_OP_SETXATTR:
303 case CEPH_OSD_OP_SETXATTRS:
304 case CEPH_OSD_OP_RESETXATTRS:
305 case CEPH_OSD_OP_RMXATTR:
306 case CEPH_OSD_OP_PULL:
307 case CEPH_OSD_OP_PUSH:
308 case CEPH_OSD_OP_BALANCEREADS:
309 case CEPH_OSD_OP_UNBALANCEREADS:
310 case CEPH_OSD_OP_SCRUB:
311 case CEPH_OSD_OP_SCRUB_RESERVE:
312 case CEPH_OSD_OP_SCRUB_UNRESERVE:
313 case CEPH_OSD_OP_SCRUB_STOP:
314 case CEPH_OSD_OP_SCRUB_MAP:
315 case CEPH_OSD_OP_WRLOCK:
316 case CEPH_OSD_OP_WRUNLOCK:
317 case CEPH_OSD_OP_RDLOCK:
318 case CEPH_OSD_OP_RDUNLOCK:
319 case CEPH_OSD_OP_UPLOCK:
320 case CEPH_OSD_OP_DNLOCK:
321 case CEPH_OSD_OP_PGLS:
322 case CEPH_OSD_OP_PGLS_FILTER:
323 pr_err("unsupported osd opcode %s\n",
324 ceph_osd_op_name(src->op));
328 dst->payload_len = cpu_to_le32(src->payload_len);
332 * build new request AND message
335 void ceph_osdc_build_request(struct ceph_osd_request *req,
336 u64 off, u64 len, unsigned int num_ops,
337 struct ceph_osd_req_op *src_ops,
338 struct ceph_snap_context *snapc, u64 snap_id,
339 struct timespec *mtime)
341 struct ceph_msg *msg = req->r_request;
342 struct ceph_osd_req_op *src_op;
345 int flags = req->r_flags;
349 req->r_num_ops = num_ops;
350 req->r_snapid = snap_id;
351 req->r_snapc = ceph_get_snap_context(snapc);
354 msg->hdr.version = cpu_to_le16(4);
356 p = msg->front.iov_base;
357 ceph_encode_32(&p, 1); /* client_inc is always 1 */
358 req->r_request_osdmap_epoch = p;
360 req->r_request_flags = p;
362 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
363 ceph_encode_timespec(p, mtime);
364 p += sizeof(struct ceph_timespec);
365 req->r_request_reassert_version = p;
366 p += sizeof(struct ceph_eversion); /* will get filled in */
369 ceph_encode_8(&p, 4);
370 ceph_encode_8(&p, 4);
371 ceph_encode_32(&p, 8 + 4 + 4);
372 req->r_request_pool = p;
374 ceph_encode_32(&p, -1); /* preferred */
375 ceph_encode_32(&p, 0); /* key len */
377 ceph_encode_8(&p, 1);
378 req->r_request_pgid = p;
380 ceph_encode_32(&p, -1); /* preferred */
383 ceph_encode_32(&p, req->r_oid_len);
384 memcpy(p, req->r_oid, req->r_oid_len);
385 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
389 ceph_encode_16(&p, num_ops);
391 req->r_request_ops = p;
392 for (i = 0; i < num_ops; i++, src_op++) {
393 osd_req_encode_op(req, p, src_op);
394 p += sizeof(struct ceph_osd_op);
398 ceph_encode_64(&p, req->r_snapid);
399 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
400 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
402 for (i = 0; i < snapc->num_snaps; i++) {
403 ceph_encode_64(&p, req->r_snapc->snaps[i]);
407 req->r_request_attempts = p;
410 data_len = req->r_trail.length;
411 if (flags & CEPH_OSD_FLAG_WRITE) {
412 req->r_request->hdr.data_off = cpu_to_le16(off);
415 req->r_request->hdr.data_len = cpu_to_le32(data_len);
417 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
418 msg_size = p - msg->front.iov_base;
419 msg->front.iov_len = msg_size;
420 msg->hdr.front_len = cpu_to_le32(msg_size);
422 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
426 EXPORT_SYMBOL(ceph_osdc_build_request);
429 * build new request AND message, calculate layout, and adjust file
432 * if the file was recently truncated, we include information about its
433 * old and new size so that the object can be updated appropriately. (we
434 * avoid synchronously deleting truncated objects because it's slow.)
436 * if @do_sync, include a 'startsync' command so that the osd will flush
439 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
440 struct ceph_file_layout *layout,
441 struct ceph_vino vino,
443 int opcode, int flags,
444 struct ceph_snap_context *snapc,
448 struct timespec *mtime,
451 struct ceph_osd_req_op ops[2];
452 struct ceph_osd_request *req;
453 unsigned int num_op = 1;
457 memset(&ops, 0, sizeof ops);
460 ops[0].extent.truncate_seq = truncate_seq;
461 ops[0].extent.truncate_size = truncate_size;
464 ops[1].op = CEPH_OSD_OP_STARTSYNC;
468 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
471 return ERR_PTR(-ENOMEM);
472 req->r_flags = flags;
474 /* calculate max write size */
475 r = calc_layout(layout, off, plen, ops, &bno);
477 ceph_osdc_put_request(req);
481 req->r_file_layout = *layout; /* keep a copy */
483 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
484 req->r_oid_len = strlen(req->r_oid);
486 ceph_osdc_build_request(req, off, *plen, num_op, ops,
487 snapc, vino.snap, mtime);
491 EXPORT_SYMBOL(ceph_osdc_new_request);
494 * We keep osd requests in an rbtree, sorted by ->r_tid.
496 static void __insert_request(struct ceph_osd_client *osdc,
497 struct ceph_osd_request *new)
499 struct rb_node **p = &osdc->requests.rb_node;
500 struct rb_node *parent = NULL;
501 struct ceph_osd_request *req = NULL;
505 req = rb_entry(parent, struct ceph_osd_request, r_node);
506 if (new->r_tid < req->r_tid)
508 else if (new->r_tid > req->r_tid)
514 rb_link_node(&new->r_node, parent, p);
515 rb_insert_color(&new->r_node, &osdc->requests);
518 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
521 struct ceph_osd_request *req;
522 struct rb_node *n = osdc->requests.rb_node;
525 req = rb_entry(n, struct ceph_osd_request, r_node);
526 if (tid < req->r_tid)
528 else if (tid > req->r_tid)
536 static struct ceph_osd_request *
537 __lookup_request_ge(struct ceph_osd_client *osdc,
540 struct ceph_osd_request *req;
541 struct rb_node *n = osdc->requests.rb_node;
544 req = rb_entry(n, struct ceph_osd_request, r_node);
545 if (tid < req->r_tid) {
549 } else if (tid > req->r_tid) {
559 * Resubmit requests pending on the given osd.
561 static void __kick_osd_requests(struct ceph_osd_client *osdc,
562 struct ceph_osd *osd)
564 struct ceph_osd_request *req, *nreq;
567 dout("__kick_osd_requests osd%d\n", osd->o_osd);
568 err = __reset_osd(osdc, osd);
572 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
573 list_move(&req->r_req_lru_item, &osdc->req_unsent);
574 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
577 req->r_flags |= CEPH_OSD_FLAG_RETRY;
580 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
583 * reregister request prior to unregistering linger so
584 * that r_osd is preserved.
586 BUG_ON(!list_empty(&req->r_req_lru_item));
587 __register_request(osdc, req);
588 list_add(&req->r_req_lru_item, &osdc->req_unsent);
589 list_add(&req->r_osd_item, &req->r_osd->o_requests);
590 __unregister_linger_request(osdc, req);
591 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
597 * If the osd connection drops, we need to resubmit all requests.
599 static void osd_reset(struct ceph_connection *con)
601 struct ceph_osd *osd = con->private;
602 struct ceph_osd_client *osdc;
606 dout("osd_reset osd%d\n", osd->o_osd);
608 down_read(&osdc->map_sem);
609 mutex_lock(&osdc->request_mutex);
610 __kick_osd_requests(osdc, osd);
612 mutex_unlock(&osdc->request_mutex);
613 up_read(&osdc->map_sem);
617 * Track open sessions with osds.
619 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
621 struct ceph_osd *osd;
623 osd = kzalloc(sizeof(*osd), GFP_NOFS);
627 atomic_set(&osd->o_ref, 1);
630 RB_CLEAR_NODE(&osd->o_node);
631 INIT_LIST_HEAD(&osd->o_requests);
632 INIT_LIST_HEAD(&osd->o_linger_requests);
633 INIT_LIST_HEAD(&osd->o_osd_lru);
634 osd->o_incarnation = 1;
636 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
638 INIT_LIST_HEAD(&osd->o_keepalive_item);
642 static struct ceph_osd *get_osd(struct ceph_osd *osd)
644 if (atomic_inc_not_zero(&osd->o_ref)) {
645 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
646 atomic_read(&osd->o_ref));
649 dout("get_osd %p FAIL\n", osd);
654 static void put_osd(struct ceph_osd *osd)
656 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
657 atomic_read(&osd->o_ref) - 1);
658 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
659 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
661 if (ac->ops && ac->ops->destroy_authorizer)
662 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
668 * remove an osd from our map
670 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
672 dout("__remove_osd %p\n", osd);
673 BUG_ON(!list_empty(&osd->o_requests));
674 rb_erase(&osd->o_node, &osdc->osds);
675 list_del_init(&osd->o_osd_lru);
676 ceph_con_close(&osd->o_con);
680 static void remove_all_osds(struct ceph_osd_client *osdc)
682 dout("%s %p\n", __func__, osdc);
683 mutex_lock(&osdc->request_mutex);
684 while (!RB_EMPTY_ROOT(&osdc->osds)) {
685 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
686 struct ceph_osd, o_node);
687 __remove_osd(osdc, osd);
689 mutex_unlock(&osdc->request_mutex);
692 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
693 struct ceph_osd *osd)
695 dout("__move_osd_to_lru %p\n", osd);
696 BUG_ON(!list_empty(&osd->o_osd_lru));
697 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
698 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
701 static void __remove_osd_from_lru(struct ceph_osd *osd)
703 dout("__remove_osd_from_lru %p\n", osd);
704 if (!list_empty(&osd->o_osd_lru))
705 list_del_init(&osd->o_osd_lru);
708 static void remove_old_osds(struct ceph_osd_client *osdc)
710 struct ceph_osd *osd, *nosd;
712 dout("__remove_old_osds %p\n", osdc);
713 mutex_lock(&osdc->request_mutex);
714 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
715 if (time_before(jiffies, osd->lru_ttl))
717 __remove_osd(osdc, osd);
719 mutex_unlock(&osdc->request_mutex);
725 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
727 struct ceph_entity_addr *peer_addr;
729 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
730 if (list_empty(&osd->o_requests) &&
731 list_empty(&osd->o_linger_requests)) {
732 __remove_osd(osdc, osd);
737 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
738 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
739 !ceph_con_opened(&osd->o_con)) {
740 struct ceph_osd_request *req;
742 dout(" osd addr hasn't changed and connection never opened,"
743 " letting msgr retry");
744 /* touch each r_stamp for handle_timeout()'s benfit */
745 list_for_each_entry(req, &osd->o_requests, r_osd_item)
746 req->r_stamp = jiffies;
751 ceph_con_close(&osd->o_con);
752 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
753 osd->o_incarnation++;
758 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
760 struct rb_node **p = &osdc->osds.rb_node;
761 struct rb_node *parent = NULL;
762 struct ceph_osd *osd = NULL;
764 dout("__insert_osd %p osd%d\n", new, new->o_osd);
767 osd = rb_entry(parent, struct ceph_osd, o_node);
768 if (new->o_osd < osd->o_osd)
770 else if (new->o_osd > osd->o_osd)
776 rb_link_node(&new->o_node, parent, p);
777 rb_insert_color(&new->o_node, &osdc->osds);
780 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
782 struct ceph_osd *osd;
783 struct rb_node *n = osdc->osds.rb_node;
786 osd = rb_entry(n, struct ceph_osd, o_node);
789 else if (o > osd->o_osd)
797 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
799 schedule_delayed_work(&osdc->timeout_work,
800 osdc->client->options->osd_keepalive_timeout * HZ);
803 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
805 cancel_delayed_work(&osdc->timeout_work);
809 * Register request, assign tid. If this is the first request, set up
812 static void __register_request(struct ceph_osd_client *osdc,
813 struct ceph_osd_request *req)
815 req->r_tid = ++osdc->last_tid;
816 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
817 dout("__register_request %p tid %lld\n", req, req->r_tid);
818 __insert_request(osdc, req);
819 ceph_osdc_get_request(req);
820 osdc->num_requests++;
821 if (osdc->num_requests == 1) {
822 dout(" first request, scheduling timeout\n");
823 __schedule_osd_timeout(osdc);
827 static void register_request(struct ceph_osd_client *osdc,
828 struct ceph_osd_request *req)
830 mutex_lock(&osdc->request_mutex);
831 __register_request(osdc, req);
832 mutex_unlock(&osdc->request_mutex);
836 * called under osdc->request_mutex
838 static void __unregister_request(struct ceph_osd_client *osdc,
839 struct ceph_osd_request *req)
841 if (RB_EMPTY_NODE(&req->r_node)) {
842 dout("__unregister_request %p tid %lld not registered\n",
847 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
848 rb_erase(&req->r_node, &osdc->requests);
849 osdc->num_requests--;
852 /* make sure the original request isn't in flight. */
853 ceph_msg_revoke(req->r_request);
855 list_del_init(&req->r_osd_item);
856 if (list_empty(&req->r_osd->o_requests) &&
857 list_empty(&req->r_osd->o_linger_requests)) {
858 dout("moving osd to %p lru\n", req->r_osd);
859 __move_osd_to_lru(osdc, req->r_osd);
861 if (list_empty(&req->r_linger_item))
865 list_del_init(&req->r_req_lru_item);
866 ceph_osdc_put_request(req);
868 if (osdc->num_requests == 0) {
869 dout(" no requests, canceling timeout\n");
870 __cancel_osd_timeout(osdc);
875 * Cancel a previously queued request message
877 static void __cancel_request(struct ceph_osd_request *req)
879 if (req->r_sent && req->r_osd) {
880 ceph_msg_revoke(req->r_request);
885 static void __register_linger_request(struct ceph_osd_client *osdc,
886 struct ceph_osd_request *req)
888 dout("__register_linger_request %p\n", req);
889 list_add_tail(&req->r_linger_item, &osdc->req_linger);
891 list_add_tail(&req->r_linger_osd,
892 &req->r_osd->o_linger_requests);
895 static void __unregister_linger_request(struct ceph_osd_client *osdc,
896 struct ceph_osd_request *req)
898 dout("__unregister_linger_request %p\n", req);
899 list_del_init(&req->r_linger_item);
901 list_del_init(&req->r_linger_osd);
903 if (list_empty(&req->r_osd->o_requests) &&
904 list_empty(&req->r_osd->o_linger_requests)) {
905 dout("moving osd to %p lru\n", req->r_osd);
906 __move_osd_to_lru(osdc, req->r_osd);
908 if (list_empty(&req->r_osd_item))
913 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
914 struct ceph_osd_request *req)
916 mutex_lock(&osdc->request_mutex);
918 __unregister_linger_request(osdc, req);
919 ceph_osdc_put_request(req);
921 mutex_unlock(&osdc->request_mutex);
923 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
925 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
926 struct ceph_osd_request *req)
928 if (!req->r_linger) {
929 dout("set_request_linger %p\n", req);
932 * caller is now responsible for calling
933 * unregister_linger_request
935 ceph_osdc_get_request(req);
938 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
941 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
942 * (as needed), and set the request r_osd appropriately. If there is
943 * no up osd, set r_osd to NULL. Move the request to the appropriate list
944 * (unsent, homeless) or leave on in-flight lru.
946 * Return 0 if unchanged, 1 if changed, or negative on error.
948 * Caller should hold map_sem for read and request_mutex.
950 static int __map_request(struct ceph_osd_client *osdc,
951 struct ceph_osd_request *req, int force_resend)
954 int acting[CEPH_PG_MAX_SIZE];
958 dout("map_request %p tid %lld\n", req, req->r_tid);
959 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
960 ceph_file_layout_pg_pool(req->r_file_layout));
962 list_move(&req->r_req_lru_item, &osdc->req_notarget);
967 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
973 if ((!force_resend &&
974 req->r_osd && req->r_osd->o_osd == o &&
975 req->r_sent >= req->r_osd->o_incarnation &&
976 req->r_num_pg_osds == num &&
977 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
978 (req->r_osd == NULL && o == -1))
979 return 0; /* no change */
981 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
982 req->r_tid, pgid.pool, pgid.seed, o,
983 req->r_osd ? req->r_osd->o_osd : -1);
985 /* record full pg acting set */
986 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
987 req->r_num_pg_osds = num;
990 __cancel_request(req);
991 list_del_init(&req->r_osd_item);
995 req->r_osd = __lookup_osd(osdc, o);
996 if (!req->r_osd && o >= 0) {
998 req->r_osd = create_osd(osdc, o);
1000 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1004 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1005 __insert_osd(osdc, req->r_osd);
1007 ceph_con_open(&req->r_osd->o_con,
1008 CEPH_ENTITY_TYPE_OSD, o,
1009 &osdc->osdmap->osd_addr[o]);
1013 __remove_osd_from_lru(req->r_osd);
1014 list_add(&req->r_osd_item, &req->r_osd->o_requests);
1015 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1017 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1019 err = 1; /* osd or pg changed */
1026 * caller should hold map_sem (for read) and request_mutex
1028 static void __send_request(struct ceph_osd_client *osdc,
1029 struct ceph_osd_request *req)
1033 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1034 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1035 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1037 /* fill in message content that changes each time we send it */
1038 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1039 put_unaligned_le32(req->r_flags, req->r_request_flags);
1040 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1041 p = req->r_request_pgid;
1042 ceph_encode_64(&p, req->r_pgid.pool);
1043 ceph_encode_32(&p, req->r_pgid.seed);
1044 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1045 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1046 sizeof(req->r_reassert_version));
1048 req->r_stamp = jiffies;
1049 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1051 ceph_msg_get(req->r_request); /* send consumes a ref */
1052 ceph_con_send(&req->r_osd->o_con, req->r_request);
1053 req->r_sent = req->r_osd->o_incarnation;
1057 * Send any requests in the queue (req_unsent).
1059 static void __send_queued(struct ceph_osd_client *osdc)
1061 struct ceph_osd_request *req, *tmp;
1063 dout("__send_queued\n");
1064 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1065 __send_request(osdc, req);
1069 * Timeout callback, called every N seconds when 1 or more osd
1070 * requests has been active for more than N seconds. When this
1071 * happens, we ping all OSDs with requests who have timed out to
1072 * ensure any communications channel reset is detected. Reset the
1073 * request timeouts another N seconds in the future as we go.
1074 * Reschedule the timeout event another N seconds in future (unless
1075 * there are no open requests).
1077 static void handle_timeout(struct work_struct *work)
1079 struct ceph_osd_client *osdc =
1080 container_of(work, struct ceph_osd_client, timeout_work.work);
1081 struct ceph_osd_request *req;
1082 struct ceph_osd *osd;
1083 unsigned long keepalive =
1084 osdc->client->options->osd_keepalive_timeout * HZ;
1085 struct list_head slow_osds;
1087 down_read(&osdc->map_sem);
1089 ceph_monc_request_next_osdmap(&osdc->client->monc);
1091 mutex_lock(&osdc->request_mutex);
1094 * ping osds that are a bit slow. this ensures that if there
1095 * is a break in the TCP connection we will notice, and reopen
1096 * a connection with that osd (from the fault callback).
1098 INIT_LIST_HEAD(&slow_osds);
1099 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1100 if (time_before(jiffies, req->r_stamp + keepalive))
1105 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1106 req->r_tid, osd->o_osd);
1107 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1109 while (!list_empty(&slow_osds)) {
1110 osd = list_entry(slow_osds.next, struct ceph_osd,
1112 list_del_init(&osd->o_keepalive_item);
1113 ceph_con_keepalive(&osd->o_con);
1116 __schedule_osd_timeout(osdc);
1117 __send_queued(osdc);
1118 mutex_unlock(&osdc->request_mutex);
1119 up_read(&osdc->map_sem);
1122 static void handle_osds_timeout(struct work_struct *work)
1124 struct ceph_osd_client *osdc =
1125 container_of(work, struct ceph_osd_client,
1126 osds_timeout_work.work);
1127 unsigned long delay =
1128 osdc->client->options->osd_idle_ttl * HZ >> 2;
1130 dout("osds timeout\n");
1131 down_read(&osdc->map_sem);
1132 remove_old_osds(osdc);
1133 up_read(&osdc->map_sem);
1135 schedule_delayed_work(&osdc->osds_timeout_work,
1136 round_jiffies_relative(delay));
1139 static void complete_request(struct ceph_osd_request *req)
1141 if (req->r_safe_callback)
1142 req->r_safe_callback(req, NULL);
1143 complete_all(&req->r_safe_completion); /* fsync waiter */
1146 static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid)
1150 ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad);
1151 v = ceph_decode_8(p);
1153 pr_warning("do not understand pg encoding %d > 1", v);
1156 pgid->pool = ceph_decode_64(p);
1157 pgid->seed = ceph_decode_32(p);
1162 pr_warning("incomplete pg encoding");
1167 * handle osd op reply. either call the callback if it is specified,
1168 * or do the completion to wake up the waiting thread.
1170 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1171 struct ceph_connection *con)
1174 struct ceph_osd_request *req;
1177 int numops, payload_len, flags;
1183 u64 reassert_version;
1185 int already_completed;
1188 tid = le64_to_cpu(msg->hdr.tid);
1189 dout("handle_reply %p tid %llu\n", msg, tid);
1191 p = msg->front.iov_base;
1192 end = p + msg->front.iov_len;
1194 ceph_decode_need(&p, end, 4, bad);
1195 object_len = ceph_decode_32(&p);
1196 ceph_decode_need(&p, end, object_len, bad);
1199 err = __decode_pgid(&p, end, &pg);
1203 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1204 flags = ceph_decode_64(&p);
1205 result = ceph_decode_32(&p);
1206 reassert_epoch = ceph_decode_32(&p);
1207 reassert_version = ceph_decode_64(&p);
1208 osdmap_epoch = ceph_decode_32(&p);
1211 mutex_lock(&osdc->request_mutex);
1212 req = __lookup_request(osdc, tid);
1214 dout("handle_reply tid %llu dne\n", tid);
1215 mutex_unlock(&osdc->request_mutex);
1218 ceph_osdc_get_request(req);
1220 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1223 ceph_decode_need(&p, end, 4, bad);
1224 numops = ceph_decode_32(&p);
1225 if (numops > CEPH_OSD_MAX_OP)
1227 if (numops != req->r_num_ops)
1230 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1231 for (i = 0; i < numops; i++) {
1232 struct ceph_osd_op *op = p;
1235 len = le32_to_cpu(op->payload_len);
1236 req->r_reply_op_len[i] = len;
1237 dout(" op %d has %d bytes\n", i, len);
1241 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1242 pr_warning("sum of op payload lens %d != data_len %d",
1243 payload_len, le32_to_cpu(msg->hdr.data_len));
1247 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1248 retry_attempt = ceph_decode_32(&p);
1249 for (i = 0; i < numops; i++)
1250 req->r_reply_op_result[i] = ceph_decode_32(&p);
1253 * if this connection filled our message, drop our reference now, to
1254 * avoid a (safe but slower) revoke later.
1256 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1257 dout(" dropping con_filling_msg ref %p\n", con);
1258 req->r_con_filling_msg = NULL;
1262 if (!req->r_got_reply) {
1265 req->r_result = result;
1266 bytes = le32_to_cpu(msg->hdr.data_len);
1267 dout("handle_reply result %d bytes %d\n", req->r_result,
1269 if (req->r_result == 0)
1270 req->r_result = bytes;
1272 /* in case this is a write and we need to replay, */
1273 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1274 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1276 req->r_got_reply = 1;
1277 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1278 dout("handle_reply tid %llu dup ack\n", tid);
1279 mutex_unlock(&osdc->request_mutex);
1283 dout("handle_reply tid %llu flags %d\n", tid, flags);
1285 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1286 __register_linger_request(osdc, req);
1288 /* either this is a read, or we got the safe response */
1290 (flags & CEPH_OSD_FLAG_ONDISK) ||
1291 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1292 __unregister_request(osdc, req);
1294 already_completed = req->r_completed;
1295 req->r_completed = 1;
1296 mutex_unlock(&osdc->request_mutex);
1297 if (already_completed)
1300 if (req->r_callback)
1301 req->r_callback(req, msg);
1303 complete_all(&req->r_completion);
1305 if (flags & CEPH_OSD_FLAG_ONDISK)
1306 complete_request(req);
1309 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1310 ceph_osdc_put_request(req);
1314 ceph_osdc_put_request(req);
1316 pr_err("corrupt osd_op_reply got %d %d\n",
1317 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1321 static void reset_changed_osds(struct ceph_osd_client *osdc)
1323 struct rb_node *p, *n;
1325 for (p = rb_first(&osdc->osds); p; p = n) {
1326 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1329 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1330 memcmp(&osd->o_con.peer_addr,
1331 ceph_osd_addr(osdc->osdmap,
1333 sizeof(struct ceph_entity_addr)) != 0)
1334 __reset_osd(osdc, osd);
1339 * Requeue requests whose mapping to an OSD has changed. If requests map to
1340 * no osd, request a new map.
1342 * Caller should hold map_sem for read.
1344 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1346 struct ceph_osd_request *req, *nreq;
1351 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1352 mutex_lock(&osdc->request_mutex);
1353 for (p = rb_first(&osdc->requests); p; ) {
1354 req = rb_entry(p, struct ceph_osd_request, r_node);
1358 * For linger requests that have not yet been
1359 * registered, move them to the linger list; they'll
1360 * be sent to the osd in the loop below. Unregister
1361 * the request before re-registering it as a linger
1362 * request to ensure the __map_request() below
1363 * will decide it needs to be sent.
1365 if (req->r_linger && list_empty(&req->r_linger_item)) {
1366 dout("%p tid %llu restart on osd%d\n",
1368 req->r_osd ? req->r_osd->o_osd : -1);
1369 __unregister_request(osdc, req);
1370 __register_linger_request(osdc, req);
1374 err = __map_request(osdc, req, force_resend);
1376 continue; /* error */
1377 if (req->r_osd == NULL) {
1378 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1379 needmap++; /* request a newer map */
1380 } else if (err > 0) {
1381 if (!req->r_linger) {
1382 dout("%p tid %llu requeued on osd%d\n", req,
1384 req->r_osd ? req->r_osd->o_osd : -1);
1385 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1390 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1392 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1394 err = __map_request(osdc, req, force_resend);
1395 dout("__map_request returned %d\n", err);
1397 continue; /* no change and no osd was specified */
1399 continue; /* hrm! */
1400 if (req->r_osd == NULL) {
1401 dout("tid %llu maps to no valid osd\n", req->r_tid);
1402 needmap++; /* request a newer map */
1406 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1407 req->r_osd ? req->r_osd->o_osd : -1);
1408 __register_request(osdc, req);
1409 __unregister_linger_request(osdc, req);
1411 mutex_unlock(&osdc->request_mutex);
1414 dout("%d requests for down osds, need new map\n", needmap);
1415 ceph_monc_request_next_osdmap(&osdc->client->monc);
1417 reset_changed_osds(osdc);
1422 * Process updated osd map.
1424 * The message contains any number of incremental and full maps, normally
1425 * indicating some sort of topology change in the cluster. Kick requests
1426 * off to different OSDs as needed.
1428 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1430 void *p, *end, *next;
1431 u32 nr_maps, maplen;
1433 struct ceph_osdmap *newmap = NULL, *oldmap;
1435 struct ceph_fsid fsid;
1437 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1438 p = msg->front.iov_base;
1439 end = p + msg->front.iov_len;
1442 ceph_decode_need(&p, end, sizeof(fsid), bad);
1443 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1444 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1447 down_write(&osdc->map_sem);
1449 /* incremental maps */
1450 ceph_decode_32_safe(&p, end, nr_maps, bad);
1451 dout(" %d inc maps\n", nr_maps);
1452 while (nr_maps > 0) {
1453 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1454 epoch = ceph_decode_32(&p);
1455 maplen = ceph_decode_32(&p);
1456 ceph_decode_need(&p, end, maplen, bad);
1458 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1459 dout("applying incremental map %u len %d\n",
1461 newmap = osdmap_apply_incremental(&p, next,
1463 &osdc->client->msgr);
1464 if (IS_ERR(newmap)) {
1465 err = PTR_ERR(newmap);
1469 if (newmap != osdc->osdmap) {
1470 ceph_osdmap_destroy(osdc->osdmap);
1471 osdc->osdmap = newmap;
1473 kick_requests(osdc, 0);
1475 dout("ignoring incremental map %u len %d\n",
1485 ceph_decode_32_safe(&p, end, nr_maps, bad);
1486 dout(" %d full maps\n", nr_maps);
1488 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1489 epoch = ceph_decode_32(&p);
1490 maplen = ceph_decode_32(&p);
1491 ceph_decode_need(&p, end, maplen, bad);
1493 dout("skipping non-latest full map %u len %d\n",
1495 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1496 dout("skipping full map %u len %d, "
1497 "older than our %u\n", epoch, maplen,
1498 osdc->osdmap->epoch);
1500 int skipped_map = 0;
1502 dout("taking full map %u len %d\n", epoch, maplen);
1503 newmap = osdmap_decode(&p, p+maplen);
1504 if (IS_ERR(newmap)) {
1505 err = PTR_ERR(newmap);
1509 oldmap = osdc->osdmap;
1510 osdc->osdmap = newmap;
1512 if (oldmap->epoch + 1 < newmap->epoch)
1514 ceph_osdmap_destroy(oldmap);
1516 kick_requests(osdc, skipped_map);
1523 downgrade_write(&osdc->map_sem);
1524 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1527 * subscribe to subsequent osdmap updates if full to ensure
1528 * we find out when we are no longer full and stop returning
1531 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1532 ceph_monc_request_next_osdmap(&osdc->client->monc);
1534 mutex_lock(&osdc->request_mutex);
1535 __send_queued(osdc);
1536 mutex_unlock(&osdc->request_mutex);
1537 up_read(&osdc->map_sem);
1538 wake_up_all(&osdc->client->auth_wq);
1542 pr_err("osdc handle_map corrupt msg\n");
1544 up_write(&osdc->map_sem);
1549 * watch/notify callback event infrastructure
1551 * These callbacks are used both for watch and notify operations.
1553 static void __release_event(struct kref *kref)
1555 struct ceph_osd_event *event =
1556 container_of(kref, struct ceph_osd_event, kref);
1558 dout("__release_event %p\n", event);
1562 static void get_event(struct ceph_osd_event *event)
1564 kref_get(&event->kref);
1567 void ceph_osdc_put_event(struct ceph_osd_event *event)
1569 kref_put(&event->kref, __release_event);
1571 EXPORT_SYMBOL(ceph_osdc_put_event);
1573 static void __insert_event(struct ceph_osd_client *osdc,
1574 struct ceph_osd_event *new)
1576 struct rb_node **p = &osdc->event_tree.rb_node;
1577 struct rb_node *parent = NULL;
1578 struct ceph_osd_event *event = NULL;
1582 event = rb_entry(parent, struct ceph_osd_event, node);
1583 if (new->cookie < event->cookie)
1585 else if (new->cookie > event->cookie)
1586 p = &(*p)->rb_right;
1591 rb_link_node(&new->node, parent, p);
1592 rb_insert_color(&new->node, &osdc->event_tree);
1595 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1598 struct rb_node **p = &osdc->event_tree.rb_node;
1599 struct rb_node *parent = NULL;
1600 struct ceph_osd_event *event = NULL;
1604 event = rb_entry(parent, struct ceph_osd_event, node);
1605 if (cookie < event->cookie)
1607 else if (cookie > event->cookie)
1608 p = &(*p)->rb_right;
1615 static void __remove_event(struct ceph_osd_event *event)
1617 struct ceph_osd_client *osdc = event->osdc;
1619 if (!RB_EMPTY_NODE(&event->node)) {
1620 dout("__remove_event removed %p\n", event);
1621 rb_erase(&event->node, &osdc->event_tree);
1622 ceph_osdc_put_event(event);
1624 dout("__remove_event didn't remove %p\n", event);
1628 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1629 void (*event_cb)(u64, u64, u8, void *),
1630 void *data, struct ceph_osd_event **pevent)
1632 struct ceph_osd_event *event;
1634 event = kmalloc(sizeof(*event), GFP_NOIO);
1638 dout("create_event %p\n", event);
1639 event->cb = event_cb;
1640 event->one_shot = 0;
1643 INIT_LIST_HEAD(&event->osd_node);
1644 RB_CLEAR_NODE(&event->node);
1645 kref_init(&event->kref); /* one ref for us */
1646 kref_get(&event->kref); /* one ref for the caller */
1648 spin_lock(&osdc->event_lock);
1649 event->cookie = ++osdc->event_count;
1650 __insert_event(osdc, event);
1651 spin_unlock(&osdc->event_lock);
1656 EXPORT_SYMBOL(ceph_osdc_create_event);
1658 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1660 struct ceph_osd_client *osdc = event->osdc;
1662 dout("cancel_event %p\n", event);
1663 spin_lock(&osdc->event_lock);
1664 __remove_event(event);
1665 spin_unlock(&osdc->event_lock);
1666 ceph_osdc_put_event(event); /* caller's */
1668 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1671 static void do_event_work(struct work_struct *work)
1673 struct ceph_osd_event_work *event_work =
1674 container_of(work, struct ceph_osd_event_work, work);
1675 struct ceph_osd_event *event = event_work->event;
1676 u64 ver = event_work->ver;
1677 u64 notify_id = event_work->notify_id;
1678 u8 opcode = event_work->opcode;
1680 dout("do_event_work completing %p\n", event);
1681 event->cb(ver, notify_id, opcode, event->data);
1682 dout("do_event_work completed %p\n", event);
1683 ceph_osdc_put_event(event);
1689 * Process osd watch notifications
1691 static void handle_watch_notify(struct ceph_osd_client *osdc,
1692 struct ceph_msg *msg)
1696 u64 cookie, ver, notify_id;
1698 struct ceph_osd_event *event;
1699 struct ceph_osd_event_work *event_work;
1701 p = msg->front.iov_base;
1702 end = p + msg->front.iov_len;
1704 ceph_decode_8_safe(&p, end, proto_ver, bad);
1705 ceph_decode_8_safe(&p, end, opcode, bad);
1706 ceph_decode_64_safe(&p, end, cookie, bad);
1707 ceph_decode_64_safe(&p, end, ver, bad);
1708 ceph_decode_64_safe(&p, end, notify_id, bad);
1710 spin_lock(&osdc->event_lock);
1711 event = __find_event(osdc, cookie);
1713 BUG_ON(event->one_shot);
1716 spin_unlock(&osdc->event_lock);
1717 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1718 cookie, ver, event);
1720 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1722 dout("ERROR: could not allocate event_work\n");
1725 INIT_WORK(&event_work->work, do_event_work);
1726 event_work->event = event;
1727 event_work->ver = ver;
1728 event_work->notify_id = notify_id;
1729 event_work->opcode = opcode;
1730 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1731 dout("WARNING: failed to queue notify event work\n");
1739 ceph_osdc_put_event(event);
1743 pr_err("osdc handle_watch_notify corrupt msg\n");
1747 static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1748 struct ceph_osd_data *osd_data)
1750 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1751 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1752 if (osd_data->length)
1753 ceph_msg_data_set_pages(msg, osd_data->pages,
1754 osd_data->length, osd_data->alignment);
1756 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
1757 ceph_msg_data_set_bio(msg, osd_data->bio);
1760 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1765 * Register request, send initial attempt.
1767 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1768 struct ceph_osd_request *req,
1773 /* Set up response incoming data and request outgoing data fields */
1775 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1776 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
1777 if (req->r_trail.length)
1778 ceph_msg_data_set_trail(req->r_request, &req->r_trail);
1780 register_request(osdc, req);
1782 down_read(&osdc->map_sem);
1783 mutex_lock(&osdc->request_mutex);
1785 * a racing kick_requests() may have sent the message for us
1786 * while we dropped request_mutex above, so only send now if
1787 * the request still han't been touched yet.
1789 if (req->r_sent == 0) {
1790 rc = __map_request(osdc, req, 0);
1793 dout("osdc_start_request failed map, "
1794 " will retry %lld\n", req->r_tid);
1799 if (req->r_osd == NULL) {
1800 dout("send_request %p no up osds in pg\n", req);
1801 ceph_monc_request_next_osdmap(&osdc->client->monc);
1803 __send_request(osdc, req);
1809 mutex_unlock(&osdc->request_mutex);
1810 up_read(&osdc->map_sem);
1813 EXPORT_SYMBOL(ceph_osdc_start_request);
1816 * wait for a request to complete
1818 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1819 struct ceph_osd_request *req)
1823 rc = wait_for_completion_interruptible(&req->r_completion);
1825 mutex_lock(&osdc->request_mutex);
1826 __cancel_request(req);
1827 __unregister_request(osdc, req);
1828 mutex_unlock(&osdc->request_mutex);
1829 complete_request(req);
1830 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1834 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1835 return req->r_result;
1837 EXPORT_SYMBOL(ceph_osdc_wait_request);
1840 * sync - wait for all in-flight requests to flush. avoid starvation.
1842 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1844 struct ceph_osd_request *req;
1845 u64 last_tid, next_tid = 0;
1847 mutex_lock(&osdc->request_mutex);
1848 last_tid = osdc->last_tid;
1850 req = __lookup_request_ge(osdc, next_tid);
1853 if (req->r_tid > last_tid)
1856 next_tid = req->r_tid + 1;
1857 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1860 ceph_osdc_get_request(req);
1861 mutex_unlock(&osdc->request_mutex);
1862 dout("sync waiting on tid %llu (last is %llu)\n",
1863 req->r_tid, last_tid);
1864 wait_for_completion(&req->r_safe_completion);
1865 mutex_lock(&osdc->request_mutex);
1866 ceph_osdc_put_request(req);
1868 mutex_unlock(&osdc->request_mutex);
1869 dout("sync done (thru tid %llu)\n", last_tid);
1871 EXPORT_SYMBOL(ceph_osdc_sync);
1876 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1881 osdc->client = client;
1882 osdc->osdmap = NULL;
1883 init_rwsem(&osdc->map_sem);
1884 init_completion(&osdc->map_waiters);
1885 osdc->last_requested_map = 0;
1886 mutex_init(&osdc->request_mutex);
1888 osdc->osds = RB_ROOT;
1889 INIT_LIST_HEAD(&osdc->osd_lru);
1890 osdc->requests = RB_ROOT;
1891 INIT_LIST_HEAD(&osdc->req_lru);
1892 INIT_LIST_HEAD(&osdc->req_unsent);
1893 INIT_LIST_HEAD(&osdc->req_notarget);
1894 INIT_LIST_HEAD(&osdc->req_linger);
1895 osdc->num_requests = 0;
1896 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1897 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1898 spin_lock_init(&osdc->event_lock);
1899 osdc->event_tree = RB_ROOT;
1900 osdc->event_count = 0;
1902 schedule_delayed_work(&osdc->osds_timeout_work,
1903 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1906 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1907 sizeof(struct ceph_osd_request));
1908 if (!osdc->req_mempool)
1911 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1912 OSD_OP_FRONT_LEN, 10, true,
1916 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1917 OSD_OPREPLY_FRONT_LEN, 10, true,
1922 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1923 if (IS_ERR(osdc->notify_wq)) {
1924 err = PTR_ERR(osdc->notify_wq);
1925 osdc->notify_wq = NULL;
1931 ceph_msgpool_destroy(&osdc->msgpool_op);
1933 mempool_destroy(osdc->req_mempool);
1938 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1940 flush_workqueue(osdc->notify_wq);
1941 destroy_workqueue(osdc->notify_wq);
1942 cancel_delayed_work_sync(&osdc->timeout_work);
1943 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1945 ceph_osdmap_destroy(osdc->osdmap);
1946 osdc->osdmap = NULL;
1948 remove_all_osds(osdc);
1949 mempool_destroy(osdc->req_mempool);
1950 ceph_msgpool_destroy(&osdc->msgpool_op);
1951 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1955 * Read some contiguous pages. If we cross a stripe boundary, shorten
1956 * *plen. Return number of bytes read, or error.
1958 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1959 struct ceph_vino vino, struct ceph_file_layout *layout,
1961 u32 truncate_seq, u64 truncate_size,
1962 struct page **pages, int num_pages, int page_align)
1964 struct ceph_osd_request *req;
1965 struct ceph_osd_data *osd_data;
1968 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1969 vino.snap, off, *plen);
1970 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1971 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1972 NULL, 0, truncate_seq, truncate_size, NULL,
1975 return PTR_ERR(req);
1977 /* it may be a short read due to an object boundary */
1979 osd_data = &req->r_data_in;
1980 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
1981 osd_data->pages = pages;
1982 osd_data->length = *plen;
1983 osd_data->alignment = page_align;
1985 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
1986 off, *plen, osd_data->length, page_align);
1988 rc = ceph_osdc_start_request(osdc, req, false);
1990 rc = ceph_osdc_wait_request(osdc, req);
1992 ceph_osdc_put_request(req);
1993 dout("readpages result %d\n", rc);
1996 EXPORT_SYMBOL(ceph_osdc_readpages);
1999 * do a synchronous write on N pages
2001 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2002 struct ceph_file_layout *layout,
2003 struct ceph_snap_context *snapc,
2005 u32 truncate_seq, u64 truncate_size,
2006 struct timespec *mtime,
2007 struct page **pages, int num_pages)
2009 struct ceph_osd_request *req;
2010 struct ceph_osd_data *osd_data;
2012 int page_align = off & ~PAGE_MASK;
2014 BUG_ON(vino.snap != CEPH_NOSNAP);
2015 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
2017 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2019 truncate_seq, truncate_size, mtime,
2022 return PTR_ERR(req);
2024 /* it may be a short write due to an object boundary */
2025 osd_data = &req->r_data_out;
2026 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2027 osd_data->pages = pages;
2028 osd_data->length = len;
2029 osd_data->alignment = page_align;
2030 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
2032 rc = ceph_osdc_start_request(osdc, req, true);
2034 rc = ceph_osdc_wait_request(osdc, req);
2036 ceph_osdc_put_request(req);
2039 dout("writepages result %d\n", rc);
2042 EXPORT_SYMBOL(ceph_osdc_writepages);
2045 * handle incoming message
2047 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2049 struct ceph_osd *osd = con->private;
2050 struct ceph_osd_client *osdc;
2051 int type = le16_to_cpu(msg->hdr.type);
2058 case CEPH_MSG_OSD_MAP:
2059 ceph_osdc_handle_map(osdc, msg);
2061 case CEPH_MSG_OSD_OPREPLY:
2062 handle_reply(osdc, msg, con);
2064 case CEPH_MSG_WATCH_NOTIFY:
2065 handle_watch_notify(osdc, msg);
2069 pr_err("received unknown message type %d %s\n", type,
2070 ceph_msg_type_name(type));
2077 * lookup and return message for incoming reply. set up reply message
2080 static struct ceph_msg *get_reply(struct ceph_connection *con,
2081 struct ceph_msg_header *hdr,
2084 struct ceph_osd *osd = con->private;
2085 struct ceph_osd_client *osdc = osd->o_osdc;
2087 struct ceph_osd_request *req;
2088 int front = le32_to_cpu(hdr->front_len);
2089 int data_len = le32_to_cpu(hdr->data_len);
2092 tid = le64_to_cpu(hdr->tid);
2093 mutex_lock(&osdc->request_mutex);
2094 req = __lookup_request(osdc, tid);
2098 dout("get_reply unknown tid %llu from osd%d\n", tid,
2103 if (req->r_con_filling_msg) {
2104 dout("%s revoking msg %p from old con %p\n", __func__,
2105 req->r_reply, req->r_con_filling_msg);
2106 ceph_msg_revoke_incoming(req->r_reply);
2107 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2108 req->r_con_filling_msg = NULL;
2111 if (front > req->r_reply->front.iov_len) {
2112 pr_warning("get_reply front %d > preallocated %d\n",
2113 front, (int)req->r_reply->front.iov_len);
2114 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2117 ceph_msg_put(req->r_reply);
2120 m = ceph_msg_get(req->r_reply);
2123 struct ceph_osd_data *osd_data = &req->r_data_in;
2125 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2126 if (osd_data->pages &&
2127 unlikely(osd_data->length < data_len)) {
2129 pr_warning("tid %lld reply has %d bytes "
2130 "we had only %llu bytes ready\n",
2131 tid, data_len, osd_data->length);
2140 req->r_con_filling_msg = con->ops->get(con);
2141 dout("get_reply tid %lld %p\n", tid, m);
2144 mutex_unlock(&osdc->request_mutex);
2149 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2150 struct ceph_msg_header *hdr,
2153 struct ceph_osd *osd = con->private;
2154 int type = le16_to_cpu(hdr->type);
2155 int front = le32_to_cpu(hdr->front_len);
2159 case CEPH_MSG_OSD_MAP:
2160 case CEPH_MSG_WATCH_NOTIFY:
2161 return ceph_msg_new(type, front, GFP_NOFS, false);
2162 case CEPH_MSG_OSD_OPREPLY:
2163 return get_reply(con, hdr, skip);
2165 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2173 * Wrappers to refcount containing ceph_osd struct
2175 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2177 struct ceph_osd *osd = con->private;
2183 static void put_osd_con(struct ceph_connection *con)
2185 struct ceph_osd *osd = con->private;
2193 * Note: returned pointer is the address of a structure that's
2194 * managed separately. Caller must *not* attempt to free it.
2196 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2197 int *proto, int force_new)
2199 struct ceph_osd *o = con->private;
2200 struct ceph_osd_client *osdc = o->o_osdc;
2201 struct ceph_auth_client *ac = osdc->client->monc.auth;
2202 struct ceph_auth_handshake *auth = &o->o_auth;
2204 if (force_new && auth->authorizer) {
2205 if (ac->ops && ac->ops->destroy_authorizer)
2206 ac->ops->destroy_authorizer(ac, auth->authorizer);
2207 auth->authorizer = NULL;
2209 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2210 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2213 return ERR_PTR(ret);
2215 *proto = ac->protocol;
2221 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2223 struct ceph_osd *o = con->private;
2224 struct ceph_osd_client *osdc = o->o_osdc;
2225 struct ceph_auth_client *ac = osdc->client->monc.auth;
2228 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2229 * XXX which do we do: succeed or fail?
2231 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2234 static int invalidate_authorizer(struct ceph_connection *con)
2236 struct ceph_osd *o = con->private;
2237 struct ceph_osd_client *osdc = o->o_osdc;
2238 struct ceph_auth_client *ac = osdc->client->monc.auth;
2240 if (ac->ops && ac->ops->invalidate_authorizer)
2241 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2243 return ceph_monc_validate_auth(&osdc->client->monc);
2246 static const struct ceph_connection_operations osd_con_ops = {
2249 .dispatch = dispatch,
2250 .get_authorizer = get_authorizer,
2251 .verify_authorizer_reply = verify_authorizer_reply,
2252 .invalidate_authorizer = invalidate_authorizer,
2253 .alloc_msg = alloc_msg,