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_vino vino,
67 struct ceph_file_layout *layout,
69 struct ceph_osd_request *req,
70 struct ceph_osd_req_op *op)
79 r = ceph_calc_file_object_mapping(layout, off, orig_len, &bno,
83 if (objlen < orig_len) {
85 dout(" skipping last %llu, final file extent %llu~%llu\n",
86 orig_len - *plen, off, *plen);
89 if (op_has_extent(op->op)) {
90 u32 osize = le32_to_cpu(layout->fl_object_size);
91 op->extent.offset = objoff;
92 op->extent.length = objlen;
93 if (op->extent.truncate_size <= off - objoff) {
94 op->extent.truncate_size = 0;
96 op->extent.truncate_size -= off - objoff;
97 if (op->extent.truncate_size > osize)
98 op->extent.truncate_size = osize;
101 req->r_num_pages = calc_pages_for(off, *plen);
102 req->r_page_alignment = off & ~PAGE_MASK;
103 if (op->op == CEPH_OSD_OP_WRITE)
104 op->payload_len = *plen;
106 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
107 bno, objoff, objlen, req->r_num_pages);
109 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
110 req->r_oid_len = strlen(req->r_oid);
118 void ceph_osdc_release_request(struct kref *kref)
120 struct ceph_osd_request *req = container_of(kref,
121 struct ceph_osd_request,
125 ceph_msg_put(req->r_request);
126 if (req->r_con_filling_msg) {
127 dout("%s revoking msg %p from con %p\n", __func__,
128 req->r_reply, req->r_con_filling_msg);
129 ceph_msg_revoke_incoming(req->r_reply);
130 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
131 req->r_con_filling_msg = NULL;
134 ceph_msg_put(req->r_reply);
135 if (req->r_own_pages)
136 ceph_release_page_vector(req->r_pages,
138 ceph_put_snap_context(req->r_snapc);
139 ceph_pagelist_release(&req->r_trail);
141 mempool_free(req, req->r_osdc->req_mempool);
145 EXPORT_SYMBOL(ceph_osdc_release_request);
147 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
148 struct ceph_snap_context *snapc,
153 struct ceph_osd_request *req;
154 struct ceph_msg *msg;
155 size_t msg_size = sizeof(struct ceph_osd_request_head);
157 msg_size += num_op*sizeof(struct ceph_osd_op);
160 req = mempool_alloc(osdc->req_mempool, gfp_flags);
161 memset(req, 0, sizeof(*req));
163 req = kzalloc(sizeof(*req), gfp_flags);
169 req->r_mempool = use_mempool;
171 kref_init(&req->r_kref);
172 init_completion(&req->r_completion);
173 init_completion(&req->r_safe_completion);
174 RB_CLEAR_NODE(&req->r_node);
175 INIT_LIST_HEAD(&req->r_unsafe_item);
176 INIT_LIST_HEAD(&req->r_linger_item);
177 INIT_LIST_HEAD(&req->r_linger_osd);
178 INIT_LIST_HEAD(&req->r_req_lru_item);
179 INIT_LIST_HEAD(&req->r_osd_item);
181 /* create reply message */
183 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
185 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
186 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
188 ceph_osdc_put_request(req);
193 ceph_pagelist_init(&req->r_trail);
195 /* create request message; allow space for oid */
196 msg_size += MAX_OBJ_NAME_SIZE;
198 msg_size += sizeof(u64) * snapc->num_snaps;
200 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
202 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
204 ceph_osdc_put_request(req);
208 memset(msg->front.iov_base, 0, msg->front.iov_len);
210 req->r_request = msg;
214 EXPORT_SYMBOL(ceph_osdc_alloc_request);
216 static void osd_req_encode_op(struct ceph_osd_request *req,
217 struct ceph_osd_op *dst,
218 struct ceph_osd_req_op *src)
220 dst->op = cpu_to_le16(src->op);
223 case CEPH_OSD_OP_READ:
224 case CEPH_OSD_OP_WRITE:
226 cpu_to_le64(src->extent.offset);
228 cpu_to_le64(src->extent.length);
229 dst->extent.truncate_size =
230 cpu_to_le64(src->extent.truncate_size);
231 dst->extent.truncate_seq =
232 cpu_to_le32(src->extent.truncate_seq);
234 case CEPH_OSD_OP_CALL:
235 dst->cls.class_len = src->cls.class_len;
236 dst->cls.method_len = src->cls.method_len;
237 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
239 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
241 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
242 src->cls.method_len);
243 ceph_pagelist_append(&req->r_trail, src->cls.indata,
244 src->cls.indata_len);
246 case CEPH_OSD_OP_STARTSYNC:
248 case CEPH_OSD_OP_NOTIFY_ACK:
249 case CEPH_OSD_OP_WATCH:
250 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
251 dst->watch.ver = cpu_to_le64(src->watch.ver);
252 dst->watch.flag = src->watch.flag;
255 pr_err("unrecognized osd opcode %d\n", dst->op);
258 case CEPH_OSD_OP_STAT:
259 case CEPH_OSD_OP_MAPEXT:
260 case CEPH_OSD_OP_MASKTRUNC:
261 case CEPH_OSD_OP_SPARSE_READ:
262 case CEPH_OSD_OP_NOTIFY:
263 case CEPH_OSD_OP_ASSERT_VER:
264 case CEPH_OSD_OP_WRITEFULL:
265 case CEPH_OSD_OP_TRUNCATE:
266 case CEPH_OSD_OP_ZERO:
267 case CEPH_OSD_OP_DELETE:
268 case CEPH_OSD_OP_APPEND:
269 case CEPH_OSD_OP_SETTRUNC:
270 case CEPH_OSD_OP_TRIMTRUNC:
271 case CEPH_OSD_OP_TMAPUP:
272 case CEPH_OSD_OP_TMAPPUT:
273 case CEPH_OSD_OP_TMAPGET:
274 case CEPH_OSD_OP_CREATE:
275 case CEPH_OSD_OP_ROLLBACK:
276 case CEPH_OSD_OP_OMAPGETKEYS:
277 case CEPH_OSD_OP_OMAPGETVALS:
278 case CEPH_OSD_OP_OMAPGETHEADER:
279 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
280 case CEPH_OSD_OP_MODE_RD:
281 case CEPH_OSD_OP_OMAPSETVALS:
282 case CEPH_OSD_OP_OMAPSETHEADER:
283 case CEPH_OSD_OP_OMAPCLEAR:
284 case CEPH_OSD_OP_OMAPRMKEYS:
285 case CEPH_OSD_OP_OMAP_CMP:
286 case CEPH_OSD_OP_CLONERANGE:
287 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
288 case CEPH_OSD_OP_SRC_CMPXATTR:
289 case CEPH_OSD_OP_GETXATTR:
290 case CEPH_OSD_OP_GETXATTRS:
291 case CEPH_OSD_OP_CMPXATTR:
292 case CEPH_OSD_OP_SETXATTR:
293 case CEPH_OSD_OP_SETXATTRS:
294 case CEPH_OSD_OP_RESETXATTRS:
295 case CEPH_OSD_OP_RMXATTR:
296 case CEPH_OSD_OP_PULL:
297 case CEPH_OSD_OP_PUSH:
298 case CEPH_OSD_OP_BALANCEREADS:
299 case CEPH_OSD_OP_UNBALANCEREADS:
300 case CEPH_OSD_OP_SCRUB:
301 case CEPH_OSD_OP_SCRUB_RESERVE:
302 case CEPH_OSD_OP_SCRUB_UNRESERVE:
303 case CEPH_OSD_OP_SCRUB_STOP:
304 case CEPH_OSD_OP_SCRUB_MAP:
305 case CEPH_OSD_OP_WRLOCK:
306 case CEPH_OSD_OP_WRUNLOCK:
307 case CEPH_OSD_OP_RDLOCK:
308 case CEPH_OSD_OP_RDUNLOCK:
309 case CEPH_OSD_OP_UPLOCK:
310 case CEPH_OSD_OP_DNLOCK:
311 case CEPH_OSD_OP_PGLS:
312 case CEPH_OSD_OP_PGLS_FILTER:
313 pr_err("unsupported osd opcode %s\n",
314 ceph_osd_op_name(dst->op));
318 dst->payload_len = cpu_to_le32(src->payload_len);
322 * build new request AND message
325 void ceph_osdc_build_request(struct ceph_osd_request *req,
326 u64 off, u64 len, unsigned int num_op,
327 struct ceph_osd_req_op *src_ops,
328 struct ceph_snap_context *snapc, u64 snap_id,
329 struct timespec *mtime)
331 struct ceph_msg *msg = req->r_request;
332 struct ceph_osd_request_head *head;
333 struct ceph_osd_req_op *src_op;
334 struct ceph_osd_op *op;
336 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
337 int flags = req->r_flags;
341 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
343 head = msg->front.iov_base;
344 head->snapid = cpu_to_le64(snap_id);
345 op = (void *)(head + 1);
346 p = (void *)(op + num_op);
348 req->r_snapc = ceph_get_snap_context(snapc);
350 head->client_inc = cpu_to_le32(1); /* always, for now. */
351 head->flags = cpu_to_le32(flags);
352 if (flags & CEPH_OSD_FLAG_WRITE)
353 ceph_encode_timespec(&head->mtime, mtime);
354 BUG_ON(num_op > (unsigned int) ((u16) -1));
355 head->num_ops = cpu_to_le16(num_op);
358 head->object_len = cpu_to_le32(req->r_oid_len);
359 memcpy(p, req->r_oid, req->r_oid_len);
364 osd_req_encode_op(req, op++, src_op++);
366 data_len += req->r_trail.length;
369 head->snap_seq = cpu_to_le64(snapc->seq);
370 head->num_snaps = cpu_to_le32(snapc->num_snaps);
371 for (i = 0; i < snapc->num_snaps; i++) {
372 put_unaligned_le64(snapc->snaps[i], p);
377 if (flags & CEPH_OSD_FLAG_WRITE) {
378 req->r_request->hdr.data_off = cpu_to_le16(off);
379 req->r_request->hdr.data_len = cpu_to_le32(len + data_len);
380 } else if (data_len) {
381 req->r_request->hdr.data_off = 0;
382 req->r_request->hdr.data_len = cpu_to_le32(data_len);
385 req->r_request->page_alignment = req->r_page_alignment;
387 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
388 msg_size = p - msg->front.iov_base;
389 msg->front.iov_len = msg_size;
390 msg->hdr.front_len = cpu_to_le32(msg_size);
393 EXPORT_SYMBOL(ceph_osdc_build_request);
396 * build new request AND message, calculate layout, and adjust file
399 * if the file was recently truncated, we include information about its
400 * old and new size so that the object can be updated appropriately. (we
401 * avoid synchronously deleting truncated objects because it's slow.)
403 * if @do_sync, include a 'startsync' command so that the osd will flush
406 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
407 struct ceph_file_layout *layout,
408 struct ceph_vino vino,
410 int opcode, int flags,
411 struct ceph_snap_context *snapc,
415 struct timespec *mtime,
419 struct ceph_osd_req_op ops[2];
420 struct ceph_osd_request *req;
421 unsigned int num_op = 1;
424 memset(&ops, 0, sizeof ops);
427 ops[0].extent.truncate_seq = truncate_seq;
428 ops[0].extent.truncate_size = truncate_size;
431 ops[1].op = CEPH_OSD_OP_STARTSYNC;
435 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
438 return ERR_PTR(-ENOMEM);
439 req->r_flags = flags;
441 /* calculate max write size */
442 r = calc_layout(vino, layout, off, plen, req, ops);
445 req->r_file_layout = *layout; /* keep a copy */
447 /* in case it differs from natural (file) alignment that
448 calc_layout filled in for us */
449 req->r_num_pages = calc_pages_for(page_align, *plen);
450 req->r_page_alignment = page_align;
452 ceph_osdc_build_request(req, off, *plen, num_op, ops,
453 snapc, vino.snap, mtime);
457 EXPORT_SYMBOL(ceph_osdc_new_request);
460 * We keep osd requests in an rbtree, sorted by ->r_tid.
462 static void __insert_request(struct ceph_osd_client *osdc,
463 struct ceph_osd_request *new)
465 struct rb_node **p = &osdc->requests.rb_node;
466 struct rb_node *parent = NULL;
467 struct ceph_osd_request *req = NULL;
471 req = rb_entry(parent, struct ceph_osd_request, r_node);
472 if (new->r_tid < req->r_tid)
474 else if (new->r_tid > req->r_tid)
480 rb_link_node(&new->r_node, parent, p);
481 rb_insert_color(&new->r_node, &osdc->requests);
484 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
487 struct ceph_osd_request *req;
488 struct rb_node *n = osdc->requests.rb_node;
491 req = rb_entry(n, struct ceph_osd_request, r_node);
492 if (tid < req->r_tid)
494 else if (tid > req->r_tid)
502 static struct ceph_osd_request *
503 __lookup_request_ge(struct ceph_osd_client *osdc,
506 struct ceph_osd_request *req;
507 struct rb_node *n = osdc->requests.rb_node;
510 req = rb_entry(n, struct ceph_osd_request, r_node);
511 if (tid < req->r_tid) {
515 } else if (tid > req->r_tid) {
525 * Resubmit requests pending on the given osd.
527 static void __kick_osd_requests(struct ceph_osd_client *osdc,
528 struct ceph_osd *osd)
530 struct ceph_osd_request *req, *nreq;
533 dout("__kick_osd_requests osd%d\n", osd->o_osd);
534 err = __reset_osd(osdc, osd);
538 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
539 list_move(&req->r_req_lru_item, &osdc->req_unsent);
540 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
543 req->r_flags |= CEPH_OSD_FLAG_RETRY;
546 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
549 * reregister request prior to unregistering linger so
550 * that r_osd is preserved.
552 BUG_ON(!list_empty(&req->r_req_lru_item));
553 __register_request(osdc, req);
554 list_add(&req->r_req_lru_item, &osdc->req_unsent);
555 list_add(&req->r_osd_item, &req->r_osd->o_requests);
556 __unregister_linger_request(osdc, req);
557 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
563 * If the osd connection drops, we need to resubmit all requests.
565 static void osd_reset(struct ceph_connection *con)
567 struct ceph_osd *osd = con->private;
568 struct ceph_osd_client *osdc;
572 dout("osd_reset osd%d\n", osd->o_osd);
574 down_read(&osdc->map_sem);
575 mutex_lock(&osdc->request_mutex);
576 __kick_osd_requests(osdc, osd);
578 mutex_unlock(&osdc->request_mutex);
579 up_read(&osdc->map_sem);
583 * Track open sessions with osds.
585 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
587 struct ceph_osd *osd;
589 osd = kzalloc(sizeof(*osd), GFP_NOFS);
593 atomic_set(&osd->o_ref, 1);
596 RB_CLEAR_NODE(&osd->o_node);
597 INIT_LIST_HEAD(&osd->o_requests);
598 INIT_LIST_HEAD(&osd->o_linger_requests);
599 INIT_LIST_HEAD(&osd->o_osd_lru);
600 osd->o_incarnation = 1;
602 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
604 INIT_LIST_HEAD(&osd->o_keepalive_item);
608 static struct ceph_osd *get_osd(struct ceph_osd *osd)
610 if (atomic_inc_not_zero(&osd->o_ref)) {
611 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
612 atomic_read(&osd->o_ref));
615 dout("get_osd %p FAIL\n", osd);
620 static void put_osd(struct ceph_osd *osd)
622 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
623 atomic_read(&osd->o_ref) - 1);
624 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
625 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
627 if (ac->ops && ac->ops->destroy_authorizer)
628 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
634 * remove an osd from our map
636 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
638 dout("__remove_osd %p\n", osd);
639 BUG_ON(!list_empty(&osd->o_requests));
640 rb_erase(&osd->o_node, &osdc->osds);
641 list_del_init(&osd->o_osd_lru);
642 ceph_con_close(&osd->o_con);
646 static void remove_all_osds(struct ceph_osd_client *osdc)
648 dout("%s %p\n", __func__, osdc);
649 mutex_lock(&osdc->request_mutex);
650 while (!RB_EMPTY_ROOT(&osdc->osds)) {
651 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
652 struct ceph_osd, o_node);
653 __remove_osd(osdc, osd);
655 mutex_unlock(&osdc->request_mutex);
658 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
659 struct ceph_osd *osd)
661 dout("__move_osd_to_lru %p\n", osd);
662 BUG_ON(!list_empty(&osd->o_osd_lru));
663 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
664 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
667 static void __remove_osd_from_lru(struct ceph_osd *osd)
669 dout("__remove_osd_from_lru %p\n", osd);
670 if (!list_empty(&osd->o_osd_lru))
671 list_del_init(&osd->o_osd_lru);
674 static void remove_old_osds(struct ceph_osd_client *osdc)
676 struct ceph_osd *osd, *nosd;
678 dout("__remove_old_osds %p\n", osdc);
679 mutex_lock(&osdc->request_mutex);
680 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
681 if (time_before(jiffies, osd->lru_ttl))
683 __remove_osd(osdc, osd);
685 mutex_unlock(&osdc->request_mutex);
691 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
693 struct ceph_entity_addr *peer_addr;
695 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
696 if (list_empty(&osd->o_requests) &&
697 list_empty(&osd->o_linger_requests)) {
698 __remove_osd(osdc, osd);
703 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
704 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
705 !ceph_con_opened(&osd->o_con)) {
706 struct ceph_osd_request *req;
708 dout(" osd addr hasn't changed and connection never opened,"
709 " letting msgr retry");
710 /* touch each r_stamp for handle_timeout()'s benfit */
711 list_for_each_entry(req, &osd->o_requests, r_osd_item)
712 req->r_stamp = jiffies;
717 ceph_con_close(&osd->o_con);
718 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
719 osd->o_incarnation++;
724 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
726 struct rb_node **p = &osdc->osds.rb_node;
727 struct rb_node *parent = NULL;
728 struct ceph_osd *osd = NULL;
730 dout("__insert_osd %p osd%d\n", new, new->o_osd);
733 osd = rb_entry(parent, struct ceph_osd, o_node);
734 if (new->o_osd < osd->o_osd)
736 else if (new->o_osd > osd->o_osd)
742 rb_link_node(&new->o_node, parent, p);
743 rb_insert_color(&new->o_node, &osdc->osds);
746 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
748 struct ceph_osd *osd;
749 struct rb_node *n = osdc->osds.rb_node;
752 osd = rb_entry(n, struct ceph_osd, o_node);
755 else if (o > osd->o_osd)
763 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
765 schedule_delayed_work(&osdc->timeout_work,
766 osdc->client->options->osd_keepalive_timeout * HZ);
769 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
771 cancel_delayed_work(&osdc->timeout_work);
775 * Register request, assign tid. If this is the first request, set up
778 static void __register_request(struct ceph_osd_client *osdc,
779 struct ceph_osd_request *req)
781 req->r_tid = ++osdc->last_tid;
782 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
783 dout("__register_request %p tid %lld\n", req, req->r_tid);
784 __insert_request(osdc, req);
785 ceph_osdc_get_request(req);
786 osdc->num_requests++;
787 if (osdc->num_requests == 1) {
788 dout(" first request, scheduling timeout\n");
789 __schedule_osd_timeout(osdc);
793 static void register_request(struct ceph_osd_client *osdc,
794 struct ceph_osd_request *req)
796 mutex_lock(&osdc->request_mutex);
797 __register_request(osdc, req);
798 mutex_unlock(&osdc->request_mutex);
802 * called under osdc->request_mutex
804 static void __unregister_request(struct ceph_osd_client *osdc,
805 struct ceph_osd_request *req)
807 if (RB_EMPTY_NODE(&req->r_node)) {
808 dout("__unregister_request %p tid %lld not registered\n",
813 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
814 rb_erase(&req->r_node, &osdc->requests);
815 osdc->num_requests--;
818 /* make sure the original request isn't in flight. */
819 ceph_msg_revoke(req->r_request);
821 list_del_init(&req->r_osd_item);
822 if (list_empty(&req->r_osd->o_requests) &&
823 list_empty(&req->r_osd->o_linger_requests)) {
824 dout("moving osd to %p lru\n", req->r_osd);
825 __move_osd_to_lru(osdc, req->r_osd);
827 if (list_empty(&req->r_linger_item))
831 list_del_init(&req->r_req_lru_item);
832 ceph_osdc_put_request(req);
834 if (osdc->num_requests == 0) {
835 dout(" no requests, canceling timeout\n");
836 __cancel_osd_timeout(osdc);
841 * Cancel a previously queued request message
843 static void __cancel_request(struct ceph_osd_request *req)
845 if (req->r_sent && req->r_osd) {
846 ceph_msg_revoke(req->r_request);
851 static void __register_linger_request(struct ceph_osd_client *osdc,
852 struct ceph_osd_request *req)
854 dout("__register_linger_request %p\n", req);
855 list_add_tail(&req->r_linger_item, &osdc->req_linger);
857 list_add_tail(&req->r_linger_osd,
858 &req->r_osd->o_linger_requests);
861 static void __unregister_linger_request(struct ceph_osd_client *osdc,
862 struct ceph_osd_request *req)
864 dout("__unregister_linger_request %p\n", req);
865 list_del_init(&req->r_linger_item);
867 list_del_init(&req->r_linger_osd);
869 if (list_empty(&req->r_osd->o_requests) &&
870 list_empty(&req->r_osd->o_linger_requests)) {
871 dout("moving osd to %p lru\n", req->r_osd);
872 __move_osd_to_lru(osdc, req->r_osd);
874 if (list_empty(&req->r_osd_item))
879 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
880 struct ceph_osd_request *req)
882 mutex_lock(&osdc->request_mutex);
884 __unregister_linger_request(osdc, req);
885 ceph_osdc_put_request(req);
887 mutex_unlock(&osdc->request_mutex);
889 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
891 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
892 struct ceph_osd_request *req)
894 if (!req->r_linger) {
895 dout("set_request_linger %p\n", req);
898 * caller is now responsible for calling
899 * unregister_linger_request
901 ceph_osdc_get_request(req);
904 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
907 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
908 * (as needed), and set the request r_osd appropriately. If there is
909 * no up osd, set r_osd to NULL. Move the request to the appropriate list
910 * (unsent, homeless) or leave on in-flight lru.
912 * Return 0 if unchanged, 1 if changed, or negative on error.
914 * Caller should hold map_sem for read and request_mutex.
916 static int __map_request(struct ceph_osd_client *osdc,
917 struct ceph_osd_request *req, int force_resend)
919 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
921 int acting[CEPH_PG_MAX_SIZE];
925 dout("map_request %p tid %lld\n", req, req->r_tid);
926 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
927 &req->r_file_layout, osdc->osdmap);
929 list_move(&req->r_req_lru_item, &osdc->req_notarget);
932 pgid = reqhead->layout.ol_pgid;
935 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
941 if ((!force_resend &&
942 req->r_osd && req->r_osd->o_osd == o &&
943 req->r_sent >= req->r_osd->o_incarnation &&
944 req->r_num_pg_osds == num &&
945 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
946 (req->r_osd == NULL && o == -1))
947 return 0; /* no change */
949 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
950 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
951 req->r_osd ? req->r_osd->o_osd : -1);
953 /* record full pg acting set */
954 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
955 req->r_num_pg_osds = num;
958 __cancel_request(req);
959 list_del_init(&req->r_osd_item);
963 req->r_osd = __lookup_osd(osdc, o);
964 if (!req->r_osd && o >= 0) {
966 req->r_osd = create_osd(osdc, o);
968 list_move(&req->r_req_lru_item, &osdc->req_notarget);
972 dout("map_request osd %p is osd%d\n", req->r_osd, o);
973 __insert_osd(osdc, req->r_osd);
975 ceph_con_open(&req->r_osd->o_con,
976 CEPH_ENTITY_TYPE_OSD, o,
977 &osdc->osdmap->osd_addr[o]);
981 __remove_osd_from_lru(req->r_osd);
982 list_add(&req->r_osd_item, &req->r_osd->o_requests);
983 list_move(&req->r_req_lru_item, &osdc->req_unsent);
985 list_move(&req->r_req_lru_item, &osdc->req_notarget);
987 err = 1; /* osd or pg changed */
994 * caller should hold map_sem (for read) and request_mutex
996 static void __send_request(struct ceph_osd_client *osdc,
997 struct ceph_osd_request *req)
999 struct ceph_osd_request_head *reqhead;
1001 dout("send_request %p tid %llu to osd%d flags %d\n",
1002 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1004 reqhead = req->r_request->front.iov_base;
1005 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1006 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1007 reqhead->reassert_version = req->r_reassert_version;
1009 req->r_stamp = jiffies;
1010 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1012 ceph_msg_get(req->r_request); /* send consumes a ref */
1013 ceph_con_send(&req->r_osd->o_con, req->r_request);
1014 req->r_sent = req->r_osd->o_incarnation;
1018 * Send any requests in the queue (req_unsent).
1020 static void __send_queued(struct ceph_osd_client *osdc)
1022 struct ceph_osd_request *req, *tmp;
1024 dout("__send_queued\n");
1025 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1026 __send_request(osdc, req);
1030 * Timeout callback, called every N seconds when 1 or more osd
1031 * requests has been active for more than N seconds. When this
1032 * happens, we ping all OSDs with requests who have timed out to
1033 * ensure any communications channel reset is detected. Reset the
1034 * request timeouts another N seconds in the future as we go.
1035 * Reschedule the timeout event another N seconds in future (unless
1036 * there are no open requests).
1038 static void handle_timeout(struct work_struct *work)
1040 struct ceph_osd_client *osdc =
1041 container_of(work, struct ceph_osd_client, timeout_work.work);
1042 struct ceph_osd_request *req;
1043 struct ceph_osd *osd;
1044 unsigned long keepalive =
1045 osdc->client->options->osd_keepalive_timeout * HZ;
1046 struct list_head slow_osds;
1048 down_read(&osdc->map_sem);
1050 ceph_monc_request_next_osdmap(&osdc->client->monc);
1052 mutex_lock(&osdc->request_mutex);
1055 * ping osds that are a bit slow. this ensures that if there
1056 * is a break in the TCP connection we will notice, and reopen
1057 * a connection with that osd (from the fault callback).
1059 INIT_LIST_HEAD(&slow_osds);
1060 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1061 if (time_before(jiffies, req->r_stamp + keepalive))
1066 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1067 req->r_tid, osd->o_osd);
1068 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1070 while (!list_empty(&slow_osds)) {
1071 osd = list_entry(slow_osds.next, struct ceph_osd,
1073 list_del_init(&osd->o_keepalive_item);
1074 ceph_con_keepalive(&osd->o_con);
1077 __schedule_osd_timeout(osdc);
1078 __send_queued(osdc);
1079 mutex_unlock(&osdc->request_mutex);
1080 up_read(&osdc->map_sem);
1083 static void handle_osds_timeout(struct work_struct *work)
1085 struct ceph_osd_client *osdc =
1086 container_of(work, struct ceph_osd_client,
1087 osds_timeout_work.work);
1088 unsigned long delay =
1089 osdc->client->options->osd_idle_ttl * HZ >> 2;
1091 dout("osds timeout\n");
1092 down_read(&osdc->map_sem);
1093 remove_old_osds(osdc);
1094 up_read(&osdc->map_sem);
1096 schedule_delayed_work(&osdc->osds_timeout_work,
1097 round_jiffies_relative(delay));
1100 static void complete_request(struct ceph_osd_request *req)
1102 if (req->r_safe_callback)
1103 req->r_safe_callback(req, NULL);
1104 complete_all(&req->r_safe_completion); /* fsync waiter */
1108 * handle osd op reply. either call the callback if it is specified,
1109 * or do the completion to wake up the waiting thread.
1111 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1112 struct ceph_connection *con)
1114 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1115 struct ceph_osd_request *req;
1117 int numops, object_len, flags;
1120 tid = le64_to_cpu(msg->hdr.tid);
1121 if (msg->front.iov_len < sizeof(*rhead))
1123 numops = le32_to_cpu(rhead->num_ops);
1124 object_len = le32_to_cpu(rhead->object_len);
1125 result = le32_to_cpu(rhead->result);
1126 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1127 numops * sizeof(struct ceph_osd_op))
1129 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1131 mutex_lock(&osdc->request_mutex);
1132 req = __lookup_request(osdc, tid);
1134 dout("handle_reply tid %llu dne\n", tid);
1135 mutex_unlock(&osdc->request_mutex);
1138 ceph_osdc_get_request(req);
1139 flags = le32_to_cpu(rhead->flags);
1142 * if this connection filled our message, drop our reference now, to
1143 * avoid a (safe but slower) revoke later.
1145 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1146 dout(" dropping con_filling_msg ref %p\n", con);
1147 req->r_con_filling_msg = NULL;
1151 if (!req->r_got_reply) {
1154 req->r_result = le32_to_cpu(rhead->result);
1155 bytes = le32_to_cpu(msg->hdr.data_len);
1156 dout("handle_reply result %d bytes %d\n", req->r_result,
1158 if (req->r_result == 0)
1159 req->r_result = bytes;
1161 /* in case this is a write and we need to replay, */
1162 req->r_reassert_version = rhead->reassert_version;
1164 req->r_got_reply = 1;
1165 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1166 dout("handle_reply tid %llu dup ack\n", tid);
1167 mutex_unlock(&osdc->request_mutex);
1171 dout("handle_reply tid %llu flags %d\n", tid, flags);
1173 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1174 __register_linger_request(osdc, req);
1176 /* either this is a read, or we got the safe response */
1178 (flags & CEPH_OSD_FLAG_ONDISK) ||
1179 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1180 __unregister_request(osdc, req);
1182 mutex_unlock(&osdc->request_mutex);
1184 if (req->r_callback)
1185 req->r_callback(req, msg);
1187 complete_all(&req->r_completion);
1189 if (flags & CEPH_OSD_FLAG_ONDISK)
1190 complete_request(req);
1193 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1194 ceph_osdc_put_request(req);
1198 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1199 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1200 (int)sizeof(*rhead));
1204 static void reset_changed_osds(struct ceph_osd_client *osdc)
1206 struct rb_node *p, *n;
1208 for (p = rb_first(&osdc->osds); p; p = n) {
1209 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1212 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1213 memcmp(&osd->o_con.peer_addr,
1214 ceph_osd_addr(osdc->osdmap,
1216 sizeof(struct ceph_entity_addr)) != 0)
1217 __reset_osd(osdc, osd);
1222 * Requeue requests whose mapping to an OSD has changed. If requests map to
1223 * no osd, request a new map.
1225 * Caller should hold map_sem for read.
1227 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1229 struct ceph_osd_request *req, *nreq;
1234 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1235 mutex_lock(&osdc->request_mutex);
1236 for (p = rb_first(&osdc->requests); p; ) {
1237 req = rb_entry(p, struct ceph_osd_request, r_node);
1241 * For linger requests that have not yet been
1242 * registered, move them to the linger list; they'll
1243 * be sent to the osd in the loop below. Unregister
1244 * the request before re-registering it as a linger
1245 * request to ensure the __map_request() below
1246 * will decide it needs to be sent.
1248 if (req->r_linger && list_empty(&req->r_linger_item)) {
1249 dout("%p tid %llu restart on osd%d\n",
1251 req->r_osd ? req->r_osd->o_osd : -1);
1252 __unregister_request(osdc, req);
1253 __register_linger_request(osdc, req);
1257 err = __map_request(osdc, req, force_resend);
1259 continue; /* error */
1260 if (req->r_osd == NULL) {
1261 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1262 needmap++; /* request a newer map */
1263 } else if (err > 0) {
1264 if (!req->r_linger) {
1265 dout("%p tid %llu requeued on osd%d\n", req,
1267 req->r_osd ? req->r_osd->o_osd : -1);
1268 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1273 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1275 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1277 err = __map_request(osdc, req, force_resend);
1278 dout("__map_request returned %d\n", err);
1280 continue; /* no change and no osd was specified */
1282 continue; /* hrm! */
1283 if (req->r_osd == NULL) {
1284 dout("tid %llu maps to no valid osd\n", req->r_tid);
1285 needmap++; /* request a newer map */
1289 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1290 req->r_osd ? req->r_osd->o_osd : -1);
1291 __register_request(osdc, req);
1292 __unregister_linger_request(osdc, req);
1294 mutex_unlock(&osdc->request_mutex);
1297 dout("%d requests for down osds, need new map\n", needmap);
1298 ceph_monc_request_next_osdmap(&osdc->client->monc);
1300 reset_changed_osds(osdc);
1305 * Process updated osd map.
1307 * The message contains any number of incremental and full maps, normally
1308 * indicating some sort of topology change in the cluster. Kick requests
1309 * off to different OSDs as needed.
1311 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1313 void *p, *end, *next;
1314 u32 nr_maps, maplen;
1316 struct ceph_osdmap *newmap = NULL, *oldmap;
1318 struct ceph_fsid fsid;
1320 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1321 p = msg->front.iov_base;
1322 end = p + msg->front.iov_len;
1325 ceph_decode_need(&p, end, sizeof(fsid), bad);
1326 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1327 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1330 down_write(&osdc->map_sem);
1332 /* incremental maps */
1333 ceph_decode_32_safe(&p, end, nr_maps, bad);
1334 dout(" %d inc maps\n", nr_maps);
1335 while (nr_maps > 0) {
1336 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1337 epoch = ceph_decode_32(&p);
1338 maplen = ceph_decode_32(&p);
1339 ceph_decode_need(&p, end, maplen, bad);
1341 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1342 dout("applying incremental map %u len %d\n",
1344 newmap = osdmap_apply_incremental(&p, next,
1346 &osdc->client->msgr);
1347 if (IS_ERR(newmap)) {
1348 err = PTR_ERR(newmap);
1352 if (newmap != osdc->osdmap) {
1353 ceph_osdmap_destroy(osdc->osdmap);
1354 osdc->osdmap = newmap;
1356 kick_requests(osdc, 0);
1358 dout("ignoring incremental map %u len %d\n",
1368 ceph_decode_32_safe(&p, end, nr_maps, bad);
1369 dout(" %d full maps\n", nr_maps);
1371 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1372 epoch = ceph_decode_32(&p);
1373 maplen = ceph_decode_32(&p);
1374 ceph_decode_need(&p, end, maplen, bad);
1376 dout("skipping non-latest full map %u len %d\n",
1378 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1379 dout("skipping full map %u len %d, "
1380 "older than our %u\n", epoch, maplen,
1381 osdc->osdmap->epoch);
1383 int skipped_map = 0;
1385 dout("taking full map %u len %d\n", epoch, maplen);
1386 newmap = osdmap_decode(&p, p+maplen);
1387 if (IS_ERR(newmap)) {
1388 err = PTR_ERR(newmap);
1392 oldmap = osdc->osdmap;
1393 osdc->osdmap = newmap;
1395 if (oldmap->epoch + 1 < newmap->epoch)
1397 ceph_osdmap_destroy(oldmap);
1399 kick_requests(osdc, skipped_map);
1406 downgrade_write(&osdc->map_sem);
1407 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1410 * subscribe to subsequent osdmap updates if full to ensure
1411 * we find out when we are no longer full and stop returning
1414 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1415 ceph_monc_request_next_osdmap(&osdc->client->monc);
1417 mutex_lock(&osdc->request_mutex);
1418 __send_queued(osdc);
1419 mutex_unlock(&osdc->request_mutex);
1420 up_read(&osdc->map_sem);
1421 wake_up_all(&osdc->client->auth_wq);
1425 pr_err("osdc handle_map corrupt msg\n");
1427 up_write(&osdc->map_sem);
1432 * watch/notify callback event infrastructure
1434 * These callbacks are used both for watch and notify operations.
1436 static void __release_event(struct kref *kref)
1438 struct ceph_osd_event *event =
1439 container_of(kref, struct ceph_osd_event, kref);
1441 dout("__release_event %p\n", event);
1445 static void get_event(struct ceph_osd_event *event)
1447 kref_get(&event->kref);
1450 void ceph_osdc_put_event(struct ceph_osd_event *event)
1452 kref_put(&event->kref, __release_event);
1454 EXPORT_SYMBOL(ceph_osdc_put_event);
1456 static void __insert_event(struct ceph_osd_client *osdc,
1457 struct ceph_osd_event *new)
1459 struct rb_node **p = &osdc->event_tree.rb_node;
1460 struct rb_node *parent = NULL;
1461 struct ceph_osd_event *event = NULL;
1465 event = rb_entry(parent, struct ceph_osd_event, node);
1466 if (new->cookie < event->cookie)
1468 else if (new->cookie > event->cookie)
1469 p = &(*p)->rb_right;
1474 rb_link_node(&new->node, parent, p);
1475 rb_insert_color(&new->node, &osdc->event_tree);
1478 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1481 struct rb_node **p = &osdc->event_tree.rb_node;
1482 struct rb_node *parent = NULL;
1483 struct ceph_osd_event *event = NULL;
1487 event = rb_entry(parent, struct ceph_osd_event, node);
1488 if (cookie < event->cookie)
1490 else if (cookie > event->cookie)
1491 p = &(*p)->rb_right;
1498 static void __remove_event(struct ceph_osd_event *event)
1500 struct ceph_osd_client *osdc = event->osdc;
1502 if (!RB_EMPTY_NODE(&event->node)) {
1503 dout("__remove_event removed %p\n", event);
1504 rb_erase(&event->node, &osdc->event_tree);
1505 ceph_osdc_put_event(event);
1507 dout("__remove_event didn't remove %p\n", event);
1511 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1512 void (*event_cb)(u64, u64, u8, void *),
1513 void *data, struct ceph_osd_event **pevent)
1515 struct ceph_osd_event *event;
1517 event = kmalloc(sizeof(*event), GFP_NOIO);
1521 dout("create_event %p\n", event);
1522 event->cb = event_cb;
1523 event->one_shot = 0;
1526 INIT_LIST_HEAD(&event->osd_node);
1527 RB_CLEAR_NODE(&event->node);
1528 kref_init(&event->kref); /* one ref for us */
1529 kref_get(&event->kref); /* one ref for the caller */
1531 spin_lock(&osdc->event_lock);
1532 event->cookie = ++osdc->event_count;
1533 __insert_event(osdc, event);
1534 spin_unlock(&osdc->event_lock);
1539 EXPORT_SYMBOL(ceph_osdc_create_event);
1541 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1543 struct ceph_osd_client *osdc = event->osdc;
1545 dout("cancel_event %p\n", event);
1546 spin_lock(&osdc->event_lock);
1547 __remove_event(event);
1548 spin_unlock(&osdc->event_lock);
1549 ceph_osdc_put_event(event); /* caller's */
1551 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1554 static void do_event_work(struct work_struct *work)
1556 struct ceph_osd_event_work *event_work =
1557 container_of(work, struct ceph_osd_event_work, work);
1558 struct ceph_osd_event *event = event_work->event;
1559 u64 ver = event_work->ver;
1560 u64 notify_id = event_work->notify_id;
1561 u8 opcode = event_work->opcode;
1563 dout("do_event_work completing %p\n", event);
1564 event->cb(ver, notify_id, opcode, event->data);
1565 dout("do_event_work completed %p\n", event);
1566 ceph_osdc_put_event(event);
1572 * Process osd watch notifications
1574 static void handle_watch_notify(struct ceph_osd_client *osdc,
1575 struct ceph_msg *msg)
1579 u64 cookie, ver, notify_id;
1581 struct ceph_osd_event *event;
1582 struct ceph_osd_event_work *event_work;
1584 p = msg->front.iov_base;
1585 end = p + msg->front.iov_len;
1587 ceph_decode_8_safe(&p, end, proto_ver, bad);
1588 ceph_decode_8_safe(&p, end, opcode, bad);
1589 ceph_decode_64_safe(&p, end, cookie, bad);
1590 ceph_decode_64_safe(&p, end, ver, bad);
1591 ceph_decode_64_safe(&p, end, notify_id, bad);
1593 spin_lock(&osdc->event_lock);
1594 event = __find_event(osdc, cookie);
1596 BUG_ON(event->one_shot);
1599 spin_unlock(&osdc->event_lock);
1600 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1601 cookie, ver, event);
1603 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1605 dout("ERROR: could not allocate event_work\n");
1608 INIT_WORK(&event_work->work, do_event_work);
1609 event_work->event = event;
1610 event_work->ver = ver;
1611 event_work->notify_id = notify_id;
1612 event_work->opcode = opcode;
1613 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1614 dout("WARNING: failed to queue notify event work\n");
1622 ceph_osdc_put_event(event);
1626 pr_err("osdc handle_watch_notify corrupt msg\n");
1631 * Register request, send initial attempt.
1633 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1634 struct ceph_osd_request *req,
1639 req->r_request->pages = req->r_pages;
1640 req->r_request->nr_pages = req->r_num_pages;
1642 req->r_request->bio = req->r_bio;
1644 req->r_request->trail = &req->r_trail;
1646 register_request(osdc, req);
1648 down_read(&osdc->map_sem);
1649 mutex_lock(&osdc->request_mutex);
1651 * a racing kick_requests() may have sent the message for us
1652 * while we dropped request_mutex above, so only send now if
1653 * the request still han't been touched yet.
1655 if (req->r_sent == 0) {
1656 rc = __map_request(osdc, req, 0);
1659 dout("osdc_start_request failed map, "
1660 " will retry %lld\n", req->r_tid);
1665 if (req->r_osd == NULL) {
1666 dout("send_request %p no up osds in pg\n", req);
1667 ceph_monc_request_next_osdmap(&osdc->client->monc);
1669 __send_request(osdc, req);
1675 mutex_unlock(&osdc->request_mutex);
1676 up_read(&osdc->map_sem);
1679 EXPORT_SYMBOL(ceph_osdc_start_request);
1682 * wait for a request to complete
1684 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1685 struct ceph_osd_request *req)
1689 rc = wait_for_completion_interruptible(&req->r_completion);
1691 mutex_lock(&osdc->request_mutex);
1692 __cancel_request(req);
1693 __unregister_request(osdc, req);
1694 mutex_unlock(&osdc->request_mutex);
1695 complete_request(req);
1696 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1700 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1701 return req->r_result;
1703 EXPORT_SYMBOL(ceph_osdc_wait_request);
1706 * sync - wait for all in-flight requests to flush. avoid starvation.
1708 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1710 struct ceph_osd_request *req;
1711 u64 last_tid, next_tid = 0;
1713 mutex_lock(&osdc->request_mutex);
1714 last_tid = osdc->last_tid;
1716 req = __lookup_request_ge(osdc, next_tid);
1719 if (req->r_tid > last_tid)
1722 next_tid = req->r_tid + 1;
1723 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1726 ceph_osdc_get_request(req);
1727 mutex_unlock(&osdc->request_mutex);
1728 dout("sync waiting on tid %llu (last is %llu)\n",
1729 req->r_tid, last_tid);
1730 wait_for_completion(&req->r_safe_completion);
1731 mutex_lock(&osdc->request_mutex);
1732 ceph_osdc_put_request(req);
1734 mutex_unlock(&osdc->request_mutex);
1735 dout("sync done (thru tid %llu)\n", last_tid);
1737 EXPORT_SYMBOL(ceph_osdc_sync);
1742 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1747 osdc->client = client;
1748 osdc->osdmap = NULL;
1749 init_rwsem(&osdc->map_sem);
1750 init_completion(&osdc->map_waiters);
1751 osdc->last_requested_map = 0;
1752 mutex_init(&osdc->request_mutex);
1754 osdc->osds = RB_ROOT;
1755 INIT_LIST_HEAD(&osdc->osd_lru);
1756 osdc->requests = RB_ROOT;
1757 INIT_LIST_HEAD(&osdc->req_lru);
1758 INIT_LIST_HEAD(&osdc->req_unsent);
1759 INIT_LIST_HEAD(&osdc->req_notarget);
1760 INIT_LIST_HEAD(&osdc->req_linger);
1761 osdc->num_requests = 0;
1762 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1763 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1764 spin_lock_init(&osdc->event_lock);
1765 osdc->event_tree = RB_ROOT;
1766 osdc->event_count = 0;
1768 schedule_delayed_work(&osdc->osds_timeout_work,
1769 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1772 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1773 sizeof(struct ceph_osd_request));
1774 if (!osdc->req_mempool)
1777 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1778 OSD_OP_FRONT_LEN, 10, true,
1782 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1783 OSD_OPREPLY_FRONT_LEN, 10, true,
1788 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1789 if (IS_ERR(osdc->notify_wq)) {
1790 err = PTR_ERR(osdc->notify_wq);
1791 osdc->notify_wq = NULL;
1797 ceph_msgpool_destroy(&osdc->msgpool_op);
1799 mempool_destroy(osdc->req_mempool);
1804 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1806 flush_workqueue(osdc->notify_wq);
1807 destroy_workqueue(osdc->notify_wq);
1808 cancel_delayed_work_sync(&osdc->timeout_work);
1809 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1811 ceph_osdmap_destroy(osdc->osdmap);
1812 osdc->osdmap = NULL;
1814 remove_all_osds(osdc);
1815 mempool_destroy(osdc->req_mempool);
1816 ceph_msgpool_destroy(&osdc->msgpool_op);
1817 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1821 * Read some contiguous pages. If we cross a stripe boundary, shorten
1822 * *plen. Return number of bytes read, or error.
1824 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1825 struct ceph_vino vino, struct ceph_file_layout *layout,
1827 u32 truncate_seq, u64 truncate_size,
1828 struct page **pages, int num_pages, int page_align)
1830 struct ceph_osd_request *req;
1833 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1834 vino.snap, off, *plen);
1835 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1836 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1837 NULL, 0, truncate_seq, truncate_size, NULL,
1840 return PTR_ERR(req);
1842 /* it may be a short read due to an object boundary */
1843 req->r_pages = pages;
1845 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1846 off, *plen, req->r_num_pages, page_align);
1848 rc = ceph_osdc_start_request(osdc, req, false);
1850 rc = ceph_osdc_wait_request(osdc, req);
1852 ceph_osdc_put_request(req);
1853 dout("readpages result %d\n", rc);
1856 EXPORT_SYMBOL(ceph_osdc_readpages);
1859 * do a synchronous write on N pages
1861 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1862 struct ceph_file_layout *layout,
1863 struct ceph_snap_context *snapc,
1865 u32 truncate_seq, u64 truncate_size,
1866 struct timespec *mtime,
1867 struct page **pages, int num_pages)
1869 struct ceph_osd_request *req;
1871 int page_align = off & ~PAGE_MASK;
1873 BUG_ON(vino.snap != CEPH_NOSNAP);
1874 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1876 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1878 truncate_seq, truncate_size, mtime,
1881 return PTR_ERR(req);
1883 /* it may be a short write due to an object boundary */
1884 req->r_pages = pages;
1885 dout("writepages %llu~%llu (%d pages)\n", off, len,
1888 rc = ceph_osdc_start_request(osdc, req, true);
1890 rc = ceph_osdc_wait_request(osdc, req);
1892 ceph_osdc_put_request(req);
1895 dout("writepages result %d\n", rc);
1898 EXPORT_SYMBOL(ceph_osdc_writepages);
1901 * handle incoming message
1903 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1905 struct ceph_osd *osd = con->private;
1906 struct ceph_osd_client *osdc;
1907 int type = le16_to_cpu(msg->hdr.type);
1914 case CEPH_MSG_OSD_MAP:
1915 ceph_osdc_handle_map(osdc, msg);
1917 case CEPH_MSG_OSD_OPREPLY:
1918 handle_reply(osdc, msg, con);
1920 case CEPH_MSG_WATCH_NOTIFY:
1921 handle_watch_notify(osdc, msg);
1925 pr_err("received unknown message type %d %s\n", type,
1926 ceph_msg_type_name(type));
1933 * lookup and return message for incoming reply. set up reply message
1936 static struct ceph_msg *get_reply(struct ceph_connection *con,
1937 struct ceph_msg_header *hdr,
1940 struct ceph_osd *osd = con->private;
1941 struct ceph_osd_client *osdc = osd->o_osdc;
1943 struct ceph_osd_request *req;
1944 int front = le32_to_cpu(hdr->front_len);
1945 int data_len = le32_to_cpu(hdr->data_len);
1948 tid = le64_to_cpu(hdr->tid);
1949 mutex_lock(&osdc->request_mutex);
1950 req = __lookup_request(osdc, tid);
1954 dout("get_reply unknown tid %llu from osd%d\n", tid,
1959 if (req->r_con_filling_msg) {
1960 dout("%s revoking msg %p from old con %p\n", __func__,
1961 req->r_reply, req->r_con_filling_msg);
1962 ceph_msg_revoke_incoming(req->r_reply);
1963 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
1964 req->r_con_filling_msg = NULL;
1967 if (front > req->r_reply->front.iov_len) {
1968 pr_warning("get_reply front %d > preallocated %d\n",
1969 front, (int)req->r_reply->front.iov_len);
1970 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
1973 ceph_msg_put(req->r_reply);
1976 m = ceph_msg_get(req->r_reply);
1979 int want = calc_pages_for(req->r_page_alignment, data_len);
1981 if (req->r_pages && unlikely(req->r_num_pages < want)) {
1982 pr_warning("tid %lld reply has %d bytes %d pages, we"
1983 " had only %d pages ready\n", tid, data_len,
1984 want, req->r_num_pages);
1990 m->pages = req->r_pages;
1991 m->nr_pages = req->r_num_pages;
1992 m->page_alignment = req->r_page_alignment;
1994 m->bio = req->r_bio;
1998 req->r_con_filling_msg = con->ops->get(con);
1999 dout("get_reply tid %lld %p\n", tid, m);
2002 mutex_unlock(&osdc->request_mutex);
2007 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2008 struct ceph_msg_header *hdr,
2011 struct ceph_osd *osd = con->private;
2012 int type = le16_to_cpu(hdr->type);
2013 int front = le32_to_cpu(hdr->front_len);
2017 case CEPH_MSG_OSD_MAP:
2018 case CEPH_MSG_WATCH_NOTIFY:
2019 return ceph_msg_new(type, front, GFP_NOFS, false);
2020 case CEPH_MSG_OSD_OPREPLY:
2021 return get_reply(con, hdr, skip);
2023 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2031 * Wrappers to refcount containing ceph_osd struct
2033 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2035 struct ceph_osd *osd = con->private;
2041 static void put_osd_con(struct ceph_connection *con)
2043 struct ceph_osd *osd = con->private;
2051 * Note: returned pointer is the address of a structure that's
2052 * managed separately. Caller must *not* attempt to free it.
2054 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2055 int *proto, int force_new)
2057 struct ceph_osd *o = con->private;
2058 struct ceph_osd_client *osdc = o->o_osdc;
2059 struct ceph_auth_client *ac = osdc->client->monc.auth;
2060 struct ceph_auth_handshake *auth = &o->o_auth;
2062 if (force_new && auth->authorizer) {
2063 if (ac->ops && ac->ops->destroy_authorizer)
2064 ac->ops->destroy_authorizer(ac, auth->authorizer);
2065 auth->authorizer = NULL;
2067 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2068 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2071 return ERR_PTR(ret);
2073 *proto = ac->protocol;
2079 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2081 struct ceph_osd *o = con->private;
2082 struct ceph_osd_client *osdc = o->o_osdc;
2083 struct ceph_auth_client *ac = osdc->client->monc.auth;
2086 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2087 * XXX which do we do: succeed or fail?
2089 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2092 static int invalidate_authorizer(struct ceph_connection *con)
2094 struct ceph_osd *o = con->private;
2095 struct ceph_osd_client *osdc = o->o_osdc;
2096 struct ceph_auth_client *ac = osdc->client->monc.auth;
2098 if (ac->ops && ac->ops->invalidate_authorizer)
2099 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2101 return ceph_monc_validate_auth(&osdc->client->monc);
2104 static const struct ceph_connection_operations osd_con_ops = {
2107 .dispatch = dispatch,
2108 .get_authorizer = get_authorizer,
2109 .verify_authorizer_reply = verify_authorizer_reply,
2110 .invalidate_authorizer = invalidate_authorizer,
2111 .alloc_msg = alloc_msg,