2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.h>
6 #include <linux/highmem.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
12 #include <linux/bio.h>
15 #include <linux/ceph/libceph.h>
16 #include <linux/ceph/osd_client.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/pagelist.h>
22 #define OSD_OP_FRONT_LEN 4096
23 #define OSD_OPREPLY_FRONT_LEN 512
25 static struct kmem_cache *ceph_osd_request_cache;
27 static const struct ceph_connection_operations osd_con_ops;
29 static void __send_queued(struct ceph_osd_client *osdc);
30 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
31 static void __register_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
33 static void __unregister_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
35 static void __unregister_linger_request(struct ceph_osd_client *osdc,
36 struct ceph_osd_request *req);
37 static void __enqueue_request(struct ceph_osd_request *req);
38 static void __send_request(struct ceph_osd_client *osdc,
39 struct ceph_osd_request *req);
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 u64 *objnum, u64 *objoff, u64 *objlen)
73 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
77 if (*objlen < orig_len) {
79 dout(" skipping last %llu, final file extent %llu~%llu\n",
80 orig_len - *plen, off, *plen);
83 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
88 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
90 memset(osd_data, 0, sizeof (*osd_data));
91 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
94 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
95 struct page **pages, u64 length, u32 alignment,
96 bool pages_from_pool, bool own_pages)
98 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
99 osd_data->pages = pages;
100 osd_data->length = length;
101 osd_data->alignment = alignment;
102 osd_data->pages_from_pool = pages_from_pool;
103 osd_data->own_pages = own_pages;
106 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
107 struct ceph_pagelist *pagelist)
109 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
110 osd_data->pagelist = pagelist;
114 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
115 struct bio *bio, size_t bio_length)
117 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
119 osd_data->bio_length = bio_length;
121 #endif /* CONFIG_BLOCK */
123 #define osd_req_op_data(oreq, whch, typ, fld) \
125 BUG_ON(whch >= (oreq)->r_num_ops); \
126 &(oreq)->r_ops[whch].typ.fld; \
129 static struct ceph_osd_data *
130 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
132 BUG_ON(which >= osd_req->r_num_ops);
134 return &osd_req->r_ops[which].raw_data_in;
137 struct ceph_osd_data *
138 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
141 return osd_req_op_data(osd_req, which, extent, osd_data);
143 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
145 struct ceph_osd_data *
146 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
149 return osd_req_op_data(osd_req, which, cls, response_data);
151 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
153 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
154 unsigned int which, struct page **pages,
155 u64 length, u32 alignment,
156 bool pages_from_pool, bool own_pages)
158 struct ceph_osd_data *osd_data;
160 osd_data = osd_req_op_raw_data_in(osd_req, which);
161 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
162 pages_from_pool, own_pages);
164 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
166 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
167 unsigned int which, struct page **pages,
168 u64 length, u32 alignment,
169 bool pages_from_pool, bool own_pages)
171 struct ceph_osd_data *osd_data;
173 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
174 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
175 pages_from_pool, own_pages);
177 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
179 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
180 unsigned int which, struct ceph_pagelist *pagelist)
182 struct ceph_osd_data *osd_data;
184 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
185 ceph_osd_data_pagelist_init(osd_data, pagelist);
187 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
190 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
191 unsigned int which, struct bio *bio, size_t bio_length)
193 struct ceph_osd_data *osd_data;
195 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
196 ceph_osd_data_bio_init(osd_data, bio, bio_length);
198 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
199 #endif /* CONFIG_BLOCK */
201 static void osd_req_op_cls_request_info_pagelist(
202 struct ceph_osd_request *osd_req,
203 unsigned int which, struct ceph_pagelist *pagelist)
205 struct ceph_osd_data *osd_data;
207 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
208 ceph_osd_data_pagelist_init(osd_data, pagelist);
211 void osd_req_op_cls_request_data_pagelist(
212 struct ceph_osd_request *osd_req,
213 unsigned int which, struct ceph_pagelist *pagelist)
215 struct ceph_osd_data *osd_data;
217 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
218 ceph_osd_data_pagelist_init(osd_data, pagelist);
220 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
222 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
223 unsigned int which, struct page **pages, u64 length,
224 u32 alignment, bool pages_from_pool, bool own_pages)
226 struct ceph_osd_data *osd_data;
228 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
229 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
230 pages_from_pool, own_pages);
232 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
234 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
235 unsigned int which, struct page **pages, u64 length,
236 u32 alignment, bool pages_from_pool, bool own_pages)
238 struct ceph_osd_data *osd_data;
240 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
241 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
242 pages_from_pool, own_pages);
244 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
246 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
248 switch (osd_data->type) {
249 case CEPH_OSD_DATA_TYPE_NONE:
251 case CEPH_OSD_DATA_TYPE_PAGES:
252 return osd_data->length;
253 case CEPH_OSD_DATA_TYPE_PAGELIST:
254 return (u64)osd_data->pagelist->length;
256 case CEPH_OSD_DATA_TYPE_BIO:
257 return (u64)osd_data->bio_length;
258 #endif /* CONFIG_BLOCK */
260 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
265 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
267 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
270 num_pages = calc_pages_for((u64)osd_data->alignment,
271 (u64)osd_data->length);
272 ceph_release_page_vector(osd_data->pages, num_pages);
274 ceph_osd_data_init(osd_data);
277 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
280 struct ceph_osd_req_op *op;
282 BUG_ON(which >= osd_req->r_num_ops);
283 op = &osd_req->r_ops[which];
286 case CEPH_OSD_OP_READ:
287 case CEPH_OSD_OP_WRITE:
288 ceph_osd_data_release(&op->extent.osd_data);
290 case CEPH_OSD_OP_CALL:
291 ceph_osd_data_release(&op->cls.request_info);
292 ceph_osd_data_release(&op->cls.request_data);
293 ceph_osd_data_release(&op->cls.response_data);
303 static void ceph_osdc_release_request(struct kref *kref)
305 struct ceph_osd_request *req = container_of(kref,
306 struct ceph_osd_request, r_kref);
309 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
310 req->r_request, req->r_reply);
311 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
312 WARN_ON(!list_empty(&req->r_req_lru_item));
313 WARN_ON(!list_empty(&req->r_osd_item));
314 WARN_ON(!list_empty(&req->r_linger_item));
315 WARN_ON(!list_empty(&req->r_linger_osd_item));
319 ceph_msg_put(req->r_request);
321 ceph_msg_revoke_incoming(req->r_reply);
322 ceph_msg_put(req->r_reply);
325 for (which = 0; which < req->r_num_ops; which++)
326 osd_req_op_data_release(req, which);
328 ceph_put_snap_context(req->r_snapc);
330 mempool_free(req, req->r_osdc->req_mempool);
332 kmem_cache_free(ceph_osd_request_cache, req);
336 void ceph_osdc_get_request(struct ceph_osd_request *req)
338 dout("%s %p (was %d)\n", __func__, req,
339 atomic_read(&req->r_kref.refcount));
340 kref_get(&req->r_kref);
342 EXPORT_SYMBOL(ceph_osdc_get_request);
344 void ceph_osdc_put_request(struct ceph_osd_request *req)
346 dout("%s %p (was %d)\n", __func__, req,
347 atomic_read(&req->r_kref.refcount));
348 kref_put(&req->r_kref, ceph_osdc_release_request);
350 EXPORT_SYMBOL(ceph_osdc_put_request);
352 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
353 struct ceph_snap_context *snapc,
354 unsigned int num_ops,
358 struct ceph_osd_request *req;
359 struct ceph_msg *msg;
362 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
363 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
365 msg_size = 4 + 4 + 8 + 8 + 4+8;
366 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
367 msg_size += 1 + 8 + 4 + 4; /* pg_t */
368 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
369 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
370 msg_size += 8; /* snapid */
371 msg_size += 8; /* snap_seq */
372 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
376 req = mempool_alloc(osdc->req_mempool, gfp_flags);
377 memset(req, 0, sizeof(*req));
379 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
385 req->r_mempool = use_mempool;
386 req->r_num_ops = num_ops;
388 kref_init(&req->r_kref);
389 init_completion(&req->r_completion);
390 init_completion(&req->r_safe_completion);
391 RB_CLEAR_NODE(&req->r_node);
392 INIT_LIST_HEAD(&req->r_unsafe_item);
393 INIT_LIST_HEAD(&req->r_linger_item);
394 INIT_LIST_HEAD(&req->r_linger_osd_item);
395 INIT_LIST_HEAD(&req->r_req_lru_item);
396 INIT_LIST_HEAD(&req->r_osd_item);
398 req->r_base_oloc.pool = -1;
399 req->r_target_oloc.pool = -1;
401 /* create reply message */
403 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
405 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
406 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
408 ceph_osdc_put_request(req);
413 /* create request message; allow space for oid */
415 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
417 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
419 ceph_osdc_put_request(req);
423 memset(msg->front.iov_base, 0, msg->front.iov_len);
425 req->r_request = msg;
429 EXPORT_SYMBOL(ceph_osdc_alloc_request);
431 static bool osd_req_opcode_valid(u16 opcode)
434 case CEPH_OSD_OP_READ:
435 case CEPH_OSD_OP_STAT:
436 case CEPH_OSD_OP_MAPEXT:
437 case CEPH_OSD_OP_MASKTRUNC:
438 case CEPH_OSD_OP_SPARSE_READ:
439 case CEPH_OSD_OP_NOTIFY:
440 case CEPH_OSD_OP_NOTIFY_ACK:
441 case CEPH_OSD_OP_ASSERT_VER:
442 case CEPH_OSD_OP_WRITE:
443 case CEPH_OSD_OP_WRITEFULL:
444 case CEPH_OSD_OP_TRUNCATE:
445 case CEPH_OSD_OP_ZERO:
446 case CEPH_OSD_OP_DELETE:
447 case CEPH_OSD_OP_APPEND:
448 case CEPH_OSD_OP_STARTSYNC:
449 case CEPH_OSD_OP_SETTRUNC:
450 case CEPH_OSD_OP_TRIMTRUNC:
451 case CEPH_OSD_OP_TMAPUP:
452 case CEPH_OSD_OP_TMAPPUT:
453 case CEPH_OSD_OP_TMAPGET:
454 case CEPH_OSD_OP_CREATE:
455 case CEPH_OSD_OP_ROLLBACK:
456 case CEPH_OSD_OP_WATCH:
457 case CEPH_OSD_OP_OMAPGETKEYS:
458 case CEPH_OSD_OP_OMAPGETVALS:
459 case CEPH_OSD_OP_OMAPGETHEADER:
460 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
461 case CEPH_OSD_OP_OMAPSETVALS:
462 case CEPH_OSD_OP_OMAPSETHEADER:
463 case CEPH_OSD_OP_OMAPCLEAR:
464 case CEPH_OSD_OP_OMAPRMKEYS:
465 case CEPH_OSD_OP_OMAP_CMP:
466 case CEPH_OSD_OP_SETALLOCHINT:
467 case CEPH_OSD_OP_CLONERANGE:
468 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
469 case CEPH_OSD_OP_SRC_CMPXATTR:
470 case CEPH_OSD_OP_GETXATTR:
471 case CEPH_OSD_OP_GETXATTRS:
472 case CEPH_OSD_OP_CMPXATTR:
473 case CEPH_OSD_OP_SETXATTR:
474 case CEPH_OSD_OP_SETXATTRS:
475 case CEPH_OSD_OP_RESETXATTRS:
476 case CEPH_OSD_OP_RMXATTR:
477 case CEPH_OSD_OP_PULL:
478 case CEPH_OSD_OP_PUSH:
479 case CEPH_OSD_OP_BALANCEREADS:
480 case CEPH_OSD_OP_UNBALANCEREADS:
481 case CEPH_OSD_OP_SCRUB:
482 case CEPH_OSD_OP_SCRUB_RESERVE:
483 case CEPH_OSD_OP_SCRUB_UNRESERVE:
484 case CEPH_OSD_OP_SCRUB_STOP:
485 case CEPH_OSD_OP_SCRUB_MAP:
486 case CEPH_OSD_OP_WRLOCK:
487 case CEPH_OSD_OP_WRUNLOCK:
488 case CEPH_OSD_OP_RDLOCK:
489 case CEPH_OSD_OP_RDUNLOCK:
490 case CEPH_OSD_OP_UPLOCK:
491 case CEPH_OSD_OP_DNLOCK:
492 case CEPH_OSD_OP_CALL:
493 case CEPH_OSD_OP_PGLS:
494 case CEPH_OSD_OP_PGLS_FILTER:
502 * This is an osd op init function for opcodes that have no data or
503 * other information associated with them. It also serves as a
504 * common init routine for all the other init functions, below.
506 static struct ceph_osd_req_op *
507 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
510 struct ceph_osd_req_op *op;
512 BUG_ON(which >= osd_req->r_num_ops);
513 BUG_ON(!osd_req_opcode_valid(opcode));
515 op = &osd_req->r_ops[which];
516 memset(op, 0, sizeof (*op));
522 void osd_req_op_init(struct ceph_osd_request *osd_req,
523 unsigned int which, u16 opcode)
525 (void)_osd_req_op_init(osd_req, which, opcode);
527 EXPORT_SYMBOL(osd_req_op_init);
529 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
530 unsigned int which, u16 opcode,
531 u64 offset, u64 length,
532 u64 truncate_size, u32 truncate_seq)
534 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
535 size_t payload_len = 0;
537 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
538 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
539 opcode != CEPH_OSD_OP_TRUNCATE);
541 op->extent.offset = offset;
542 op->extent.length = length;
543 op->extent.truncate_size = truncate_size;
544 op->extent.truncate_seq = truncate_seq;
545 if (opcode == CEPH_OSD_OP_WRITE)
546 payload_len += length;
548 op->payload_len = payload_len;
550 EXPORT_SYMBOL(osd_req_op_extent_init);
552 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
553 unsigned int which, u64 length)
555 struct ceph_osd_req_op *op;
558 BUG_ON(which >= osd_req->r_num_ops);
559 op = &osd_req->r_ops[which];
560 previous = op->extent.length;
562 if (length == previous)
563 return; /* Nothing to do */
564 BUG_ON(length > previous);
566 op->extent.length = length;
567 op->payload_len -= previous - length;
569 EXPORT_SYMBOL(osd_req_op_extent_update);
571 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
572 u16 opcode, const char *class, const char *method)
574 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
575 struct ceph_pagelist *pagelist;
576 size_t payload_len = 0;
579 BUG_ON(opcode != CEPH_OSD_OP_CALL);
581 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
583 ceph_pagelist_init(pagelist);
585 op->cls.class_name = class;
586 size = strlen(class);
587 BUG_ON(size > (size_t) U8_MAX);
588 op->cls.class_len = size;
589 ceph_pagelist_append(pagelist, class, size);
592 op->cls.method_name = method;
593 size = strlen(method);
594 BUG_ON(size > (size_t) U8_MAX);
595 op->cls.method_len = size;
596 ceph_pagelist_append(pagelist, method, size);
599 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
601 op->cls.argc = 0; /* currently unused */
603 op->payload_len = payload_len;
605 EXPORT_SYMBOL(osd_req_op_cls_init);
607 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
608 unsigned int which, u16 opcode,
609 u64 cookie, u64 version, int flag)
611 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
613 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
615 op->watch.cookie = cookie;
616 op->watch.ver = version;
617 if (opcode == CEPH_OSD_OP_WATCH && flag)
618 op->watch.flag = (u8)1;
620 EXPORT_SYMBOL(osd_req_op_watch_init);
622 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
624 u64 expected_object_size,
625 u64 expected_write_size)
627 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
628 CEPH_OSD_OP_SETALLOCHINT);
630 op->alloc_hint.expected_object_size = expected_object_size;
631 op->alloc_hint.expected_write_size = expected_write_size;
634 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
635 * not worth a feature bit. Set FAILOK per-op flag to make
636 * sure older osds don't trip over an unsupported opcode.
638 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
640 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
642 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
643 struct ceph_osd_data *osd_data)
645 u64 length = ceph_osd_data_length(osd_data);
647 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
648 BUG_ON(length > (u64) SIZE_MAX);
650 ceph_msg_data_add_pages(msg, osd_data->pages,
651 length, osd_data->alignment);
652 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
654 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
656 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
657 ceph_msg_data_add_bio(msg, osd_data->bio, length);
660 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
664 static u64 osd_req_encode_op(struct ceph_osd_request *req,
665 struct ceph_osd_op *dst, unsigned int which)
667 struct ceph_osd_req_op *src;
668 struct ceph_osd_data *osd_data;
669 u64 request_data_len = 0;
672 BUG_ON(which >= req->r_num_ops);
673 src = &req->r_ops[which];
674 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
675 pr_err("unrecognized osd opcode %d\n", src->op);
681 case CEPH_OSD_OP_STAT:
682 osd_data = &src->raw_data_in;
683 ceph_osdc_msg_data_add(req->r_reply, osd_data);
685 case CEPH_OSD_OP_READ:
686 case CEPH_OSD_OP_WRITE:
687 case CEPH_OSD_OP_ZERO:
688 case CEPH_OSD_OP_DELETE:
689 case CEPH_OSD_OP_TRUNCATE:
690 if (src->op == CEPH_OSD_OP_WRITE)
691 request_data_len = src->extent.length;
692 dst->extent.offset = cpu_to_le64(src->extent.offset);
693 dst->extent.length = cpu_to_le64(src->extent.length);
694 dst->extent.truncate_size =
695 cpu_to_le64(src->extent.truncate_size);
696 dst->extent.truncate_seq =
697 cpu_to_le32(src->extent.truncate_seq);
698 osd_data = &src->extent.osd_data;
699 if (src->op == CEPH_OSD_OP_WRITE)
700 ceph_osdc_msg_data_add(req->r_request, osd_data);
702 ceph_osdc_msg_data_add(req->r_reply, osd_data);
704 case CEPH_OSD_OP_CALL:
705 dst->cls.class_len = src->cls.class_len;
706 dst->cls.method_len = src->cls.method_len;
707 osd_data = &src->cls.request_info;
708 ceph_osdc_msg_data_add(req->r_request, osd_data);
709 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
710 request_data_len = osd_data->pagelist->length;
712 osd_data = &src->cls.request_data;
713 data_length = ceph_osd_data_length(osd_data);
715 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
716 dst->cls.indata_len = cpu_to_le32(data_length);
717 ceph_osdc_msg_data_add(req->r_request, osd_data);
718 src->payload_len += data_length;
719 request_data_len += data_length;
721 osd_data = &src->cls.response_data;
722 ceph_osdc_msg_data_add(req->r_reply, osd_data);
724 case CEPH_OSD_OP_STARTSYNC:
726 case CEPH_OSD_OP_NOTIFY_ACK:
727 case CEPH_OSD_OP_WATCH:
728 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
729 dst->watch.ver = cpu_to_le64(src->watch.ver);
730 dst->watch.flag = src->watch.flag;
732 case CEPH_OSD_OP_SETALLOCHINT:
733 dst->alloc_hint.expected_object_size =
734 cpu_to_le64(src->alloc_hint.expected_object_size);
735 dst->alloc_hint.expected_write_size =
736 cpu_to_le64(src->alloc_hint.expected_write_size);
739 pr_err("unsupported osd opcode %s\n",
740 ceph_osd_op_name(src->op));
746 dst->op = cpu_to_le16(src->op);
747 dst->flags = cpu_to_le32(src->flags);
748 dst->payload_len = cpu_to_le32(src->payload_len);
750 return request_data_len;
754 * build new request AND message, calculate layout, and adjust file
757 * if the file was recently truncated, we include information about its
758 * old and new size so that the object can be updated appropriately. (we
759 * avoid synchronously deleting truncated objects because it's slow.)
761 * if @do_sync, include a 'startsync' command so that the osd will flush
764 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
765 struct ceph_file_layout *layout,
766 struct ceph_vino vino,
767 u64 off, u64 *plen, int num_ops,
768 int opcode, int flags,
769 struct ceph_snap_context *snapc,
774 struct ceph_osd_request *req;
782 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
783 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
784 opcode != CEPH_OSD_OP_TRUNCATE);
786 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
789 return ERR_PTR(-ENOMEM);
791 req->r_flags = flags;
793 /* calculate max write size */
794 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
796 ceph_osdc_put_request(req);
800 object_size = le32_to_cpu(layout->fl_object_size);
801 object_base = off - objoff;
802 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
803 if (truncate_size <= object_base) {
806 truncate_size -= object_base;
807 if (truncate_size > object_size)
808 truncate_size = object_size;
812 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
813 truncate_size, truncate_seq);
816 * A second op in the ops array means the caller wants to
817 * also issue a include a 'startsync' command so that the
818 * osd will flush data quickly.
821 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
823 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
825 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
826 "%llx.%08llx", vino.ino, objnum);
827 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
831 EXPORT_SYMBOL(ceph_osdc_new_request);
834 * We keep osd requests in an rbtree, sorted by ->r_tid.
836 static void __insert_request(struct ceph_osd_client *osdc,
837 struct ceph_osd_request *new)
839 struct rb_node **p = &osdc->requests.rb_node;
840 struct rb_node *parent = NULL;
841 struct ceph_osd_request *req = NULL;
845 req = rb_entry(parent, struct ceph_osd_request, r_node);
846 if (new->r_tid < req->r_tid)
848 else if (new->r_tid > req->r_tid)
854 rb_link_node(&new->r_node, parent, p);
855 rb_insert_color(&new->r_node, &osdc->requests);
858 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
861 struct ceph_osd_request *req;
862 struct rb_node *n = osdc->requests.rb_node;
865 req = rb_entry(n, struct ceph_osd_request, r_node);
866 if (tid < req->r_tid)
868 else if (tid > req->r_tid)
876 static struct ceph_osd_request *
877 __lookup_request_ge(struct ceph_osd_client *osdc,
880 struct ceph_osd_request *req;
881 struct rb_node *n = osdc->requests.rb_node;
884 req = rb_entry(n, struct ceph_osd_request, r_node);
885 if (tid < req->r_tid) {
889 } else if (tid > req->r_tid) {
898 static void __kick_linger_request(struct ceph_osd_request *req)
900 struct ceph_osd_client *osdc = req->r_osdc;
901 struct ceph_osd *osd = req->r_osd;
904 * Linger requests need to be resent with a new tid to avoid
905 * the dup op detection logic on the OSDs. Achieve this with
906 * a re-register dance instead of open-coding.
908 ceph_osdc_get_request(req);
909 if (!list_empty(&req->r_linger_item))
910 __unregister_linger_request(osdc, req);
912 __unregister_request(osdc, req);
913 __register_request(osdc, req);
914 ceph_osdc_put_request(req);
917 * Unless request has been registered as both normal and
918 * lingering, __unregister{,_linger}_request clears r_osd.
919 * However, here we need to preserve r_osd to make sure we
920 * requeue on the same OSD.
922 WARN_ON(req->r_osd || !osd);
925 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
926 __enqueue_request(req);
930 * Resubmit requests pending on the given osd.
932 static void __kick_osd_requests(struct ceph_osd_client *osdc,
933 struct ceph_osd *osd)
935 struct ceph_osd_request *req, *nreq;
937 LIST_HEAD(resend_linger);
940 dout("%s osd%d\n", __func__, osd->o_osd);
941 err = __reset_osd(osdc, osd);
946 * Build up a list of requests to resend by traversing the
947 * osd's list of requests. Requests for a given object are
948 * sent in tid order, and that is also the order they're
949 * kept on this list. Therefore all requests that are in
950 * flight will be found first, followed by all requests that
951 * have not yet been sent. And to resend requests while
952 * preserving this order we will want to put any sent
953 * requests back on the front of the osd client's unsent
956 * So we build a separate ordered list of already-sent
957 * requests for the affected osd and splice it onto the
958 * front of the osd client's unsent list. Once we've seen a
959 * request that has not yet been sent we're done. Those
960 * requests are already sitting right where they belong.
962 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
966 if (!req->r_linger) {
967 dout("%s requeueing %p tid %llu\n", __func__, req,
969 list_move_tail(&req->r_req_lru_item, &resend);
970 req->r_flags |= CEPH_OSD_FLAG_RETRY;
972 list_move_tail(&req->r_req_lru_item, &resend_linger);
975 list_splice(&resend, &osdc->req_unsent);
978 * Both registered and not yet registered linger requests are
979 * enqueued with a new tid on the same OSD. We add/move them
980 * to req_unsent/o_requests at the end to keep things in tid
983 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
985 WARN_ON(!list_empty(&req->r_req_lru_item));
986 __kick_linger_request(req);
989 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
990 __kick_linger_request(req);
994 * If the osd connection drops, we need to resubmit all requests.
996 static void osd_reset(struct ceph_connection *con)
998 struct ceph_osd *osd = con->private;
999 struct ceph_osd_client *osdc;
1003 dout("osd_reset osd%d\n", osd->o_osd);
1005 down_read(&osdc->map_sem);
1006 mutex_lock(&osdc->request_mutex);
1007 __kick_osd_requests(osdc, osd);
1008 __send_queued(osdc);
1009 mutex_unlock(&osdc->request_mutex);
1010 up_read(&osdc->map_sem);
1014 * Track open sessions with osds.
1016 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1018 struct ceph_osd *osd;
1020 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1024 atomic_set(&osd->o_ref, 1);
1027 RB_CLEAR_NODE(&osd->o_node);
1028 INIT_LIST_HEAD(&osd->o_requests);
1029 INIT_LIST_HEAD(&osd->o_linger_requests);
1030 INIT_LIST_HEAD(&osd->o_osd_lru);
1031 osd->o_incarnation = 1;
1033 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1035 INIT_LIST_HEAD(&osd->o_keepalive_item);
1039 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1041 if (atomic_inc_not_zero(&osd->o_ref)) {
1042 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1043 atomic_read(&osd->o_ref));
1046 dout("get_osd %p FAIL\n", osd);
1051 static void put_osd(struct ceph_osd *osd)
1053 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1054 atomic_read(&osd->o_ref) - 1);
1055 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
1056 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1058 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
1064 * remove an osd from our map
1066 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1068 dout("__remove_osd %p\n", osd);
1069 BUG_ON(!list_empty(&osd->o_requests));
1070 BUG_ON(!list_empty(&osd->o_linger_requests));
1072 rb_erase(&osd->o_node, &osdc->osds);
1073 list_del_init(&osd->o_osd_lru);
1074 ceph_con_close(&osd->o_con);
1078 static void remove_all_osds(struct ceph_osd_client *osdc)
1080 dout("%s %p\n", __func__, osdc);
1081 mutex_lock(&osdc->request_mutex);
1082 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1083 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1084 struct ceph_osd, o_node);
1085 __remove_osd(osdc, osd);
1087 mutex_unlock(&osdc->request_mutex);
1090 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1091 struct ceph_osd *osd)
1093 dout("%s %p\n", __func__, osd);
1094 BUG_ON(!list_empty(&osd->o_osd_lru));
1096 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1097 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
1100 static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1101 struct ceph_osd *osd)
1103 dout("%s %p\n", __func__, osd);
1105 if (list_empty(&osd->o_requests) &&
1106 list_empty(&osd->o_linger_requests))
1107 __move_osd_to_lru(osdc, osd);
1110 static void __remove_osd_from_lru(struct ceph_osd *osd)
1112 dout("__remove_osd_from_lru %p\n", osd);
1113 if (!list_empty(&osd->o_osd_lru))
1114 list_del_init(&osd->o_osd_lru);
1117 static void remove_old_osds(struct ceph_osd_client *osdc)
1119 struct ceph_osd *osd, *nosd;
1121 dout("__remove_old_osds %p\n", osdc);
1122 mutex_lock(&osdc->request_mutex);
1123 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1124 if (time_before(jiffies, osd->lru_ttl))
1126 __remove_osd(osdc, osd);
1128 mutex_unlock(&osdc->request_mutex);
1134 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1136 struct ceph_entity_addr *peer_addr;
1138 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1139 if (list_empty(&osd->o_requests) &&
1140 list_empty(&osd->o_linger_requests)) {
1141 __remove_osd(osdc, osd);
1146 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1147 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1148 !ceph_con_opened(&osd->o_con)) {
1149 struct ceph_osd_request *req;
1151 dout("osd addr hasn't changed and connection never opened, "
1152 "letting msgr retry\n");
1153 /* touch each r_stamp for handle_timeout()'s benfit */
1154 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1155 req->r_stamp = jiffies;
1160 ceph_con_close(&osd->o_con);
1161 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1162 osd->o_incarnation++;
1167 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1169 struct rb_node **p = &osdc->osds.rb_node;
1170 struct rb_node *parent = NULL;
1171 struct ceph_osd *osd = NULL;
1173 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1176 osd = rb_entry(parent, struct ceph_osd, o_node);
1177 if (new->o_osd < osd->o_osd)
1179 else if (new->o_osd > osd->o_osd)
1180 p = &(*p)->rb_right;
1185 rb_link_node(&new->o_node, parent, p);
1186 rb_insert_color(&new->o_node, &osdc->osds);
1189 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1191 struct ceph_osd *osd;
1192 struct rb_node *n = osdc->osds.rb_node;
1195 osd = rb_entry(n, struct ceph_osd, o_node);
1198 else if (o > osd->o_osd)
1206 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1208 schedule_delayed_work(&osdc->timeout_work,
1209 osdc->client->options->osd_keepalive_timeout * HZ);
1212 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1214 cancel_delayed_work(&osdc->timeout_work);
1218 * Register request, assign tid. If this is the first request, set up
1219 * the timeout event.
1221 static void __register_request(struct ceph_osd_client *osdc,
1222 struct ceph_osd_request *req)
1224 req->r_tid = ++osdc->last_tid;
1225 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1226 dout("__register_request %p tid %lld\n", req, req->r_tid);
1227 __insert_request(osdc, req);
1228 ceph_osdc_get_request(req);
1229 osdc->num_requests++;
1230 if (osdc->num_requests == 1) {
1231 dout(" first request, scheduling timeout\n");
1232 __schedule_osd_timeout(osdc);
1237 * called under osdc->request_mutex
1239 static void __unregister_request(struct ceph_osd_client *osdc,
1240 struct ceph_osd_request *req)
1242 if (RB_EMPTY_NODE(&req->r_node)) {
1243 dout("__unregister_request %p tid %lld not registered\n",
1248 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1249 rb_erase(&req->r_node, &osdc->requests);
1250 RB_CLEAR_NODE(&req->r_node);
1251 osdc->num_requests--;
1254 /* make sure the original request isn't in flight. */
1255 ceph_msg_revoke(req->r_request);
1257 list_del_init(&req->r_osd_item);
1258 maybe_move_osd_to_lru(osdc, req->r_osd);
1259 if (list_empty(&req->r_linger_osd_item))
1263 list_del_init(&req->r_req_lru_item);
1264 ceph_osdc_put_request(req);
1266 if (osdc->num_requests == 0) {
1267 dout(" no requests, canceling timeout\n");
1268 __cancel_osd_timeout(osdc);
1273 * Cancel a previously queued request message
1275 static void __cancel_request(struct ceph_osd_request *req)
1277 if (req->r_sent && req->r_osd) {
1278 ceph_msg_revoke(req->r_request);
1283 static void __register_linger_request(struct ceph_osd_client *osdc,
1284 struct ceph_osd_request *req)
1286 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1287 WARN_ON(!req->r_linger);
1289 ceph_osdc_get_request(req);
1290 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1292 list_add_tail(&req->r_linger_osd_item,
1293 &req->r_osd->o_linger_requests);
1296 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1297 struct ceph_osd_request *req)
1299 WARN_ON(!req->r_linger);
1301 if (list_empty(&req->r_linger_item)) {
1302 dout("%s %p tid %llu not registered\n", __func__, req,
1307 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1308 list_del_init(&req->r_linger_item);
1311 list_del_init(&req->r_linger_osd_item);
1312 maybe_move_osd_to_lru(osdc, req->r_osd);
1313 if (list_empty(&req->r_osd_item))
1316 ceph_osdc_put_request(req);
1319 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1320 struct ceph_osd_request *req)
1322 if (!req->r_linger) {
1323 dout("set_request_linger %p\n", req);
1327 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1330 * Returns whether a request should be blocked from being sent
1331 * based on the current osdmap and osd_client settings.
1333 * Caller should hold map_sem for read.
1335 static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1336 struct ceph_osd_request *req)
1338 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1339 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1340 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1341 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1342 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1346 * Calculate mapping of a request to a PG. Takes tiering into account.
1348 static int __calc_request_pg(struct ceph_osdmap *osdmap,
1349 struct ceph_osd_request *req,
1350 struct ceph_pg *pg_out)
1352 bool need_check_tiering;
1354 need_check_tiering = false;
1355 if (req->r_target_oloc.pool == -1) {
1356 req->r_target_oloc = req->r_base_oloc; /* struct */
1357 need_check_tiering = true;
1359 if (req->r_target_oid.name_len == 0) {
1360 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1361 need_check_tiering = true;
1364 if (need_check_tiering &&
1365 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1366 struct ceph_pg_pool_info *pi;
1368 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
1370 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1372 req->r_target_oloc.pool = pi->read_tier;
1373 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1374 pi->write_tier >= 0)
1375 req->r_target_oloc.pool = pi->write_tier;
1377 /* !pi is caught in ceph_oloc_oid_to_pg() */
1380 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1381 &req->r_target_oid, pg_out);
1384 static void __enqueue_request(struct ceph_osd_request *req)
1386 struct ceph_osd_client *osdc = req->r_osdc;
1388 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1389 req->r_osd ? req->r_osd->o_osd : -1);
1392 __remove_osd_from_lru(req->r_osd);
1393 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1394 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1396 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1401 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1402 * (as needed), and set the request r_osd appropriately. If there is
1403 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1404 * (unsent, homeless) or leave on in-flight lru.
1406 * Return 0 if unchanged, 1 if changed, or negative on error.
1408 * Caller should hold map_sem for read and request_mutex.
1410 static int __map_request(struct ceph_osd_client *osdc,
1411 struct ceph_osd_request *req, int force_resend)
1413 struct ceph_pg pgid;
1414 int acting[CEPH_PG_MAX_SIZE];
1419 dout("map_request %p tid %lld\n", req, req->r_tid);
1421 err = __calc_request_pg(osdc->osdmap, req, &pgid);
1423 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1428 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1432 was_paused = req->r_paused;
1433 req->r_paused = __req_should_be_paused(osdc, req);
1434 if (was_paused && !req->r_paused)
1437 if ((!force_resend &&
1438 req->r_osd && req->r_osd->o_osd == o &&
1439 req->r_sent >= req->r_osd->o_incarnation &&
1440 req->r_num_pg_osds == num &&
1441 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1442 (req->r_osd == NULL && o == -1) ||
1444 return 0; /* no change */
1446 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1447 req->r_tid, pgid.pool, pgid.seed, o,
1448 req->r_osd ? req->r_osd->o_osd : -1);
1450 /* record full pg acting set */
1451 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1452 req->r_num_pg_osds = num;
1455 __cancel_request(req);
1456 list_del_init(&req->r_osd_item);
1460 req->r_osd = __lookup_osd(osdc, o);
1461 if (!req->r_osd && o >= 0) {
1463 req->r_osd = create_osd(osdc, o);
1465 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1469 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1470 __insert_osd(osdc, req->r_osd);
1472 ceph_con_open(&req->r_osd->o_con,
1473 CEPH_ENTITY_TYPE_OSD, o,
1474 &osdc->osdmap->osd_addr[o]);
1477 __enqueue_request(req);
1478 err = 1; /* osd or pg changed */
1485 * caller should hold map_sem (for read) and request_mutex
1487 static void __send_request(struct ceph_osd_client *osdc,
1488 struct ceph_osd_request *req)
1492 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1493 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1494 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1496 /* fill in message content that changes each time we send it */
1497 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1498 put_unaligned_le32(req->r_flags, req->r_request_flags);
1499 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
1500 p = req->r_request_pgid;
1501 ceph_encode_64(&p, req->r_pgid.pool);
1502 ceph_encode_32(&p, req->r_pgid.seed);
1503 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1504 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1505 sizeof(req->r_reassert_version));
1507 req->r_stamp = jiffies;
1508 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1510 ceph_msg_get(req->r_request); /* send consumes a ref */
1512 req->r_sent = req->r_osd->o_incarnation;
1514 ceph_con_send(&req->r_osd->o_con, req->r_request);
1518 * Send any requests in the queue (req_unsent).
1520 static void __send_queued(struct ceph_osd_client *osdc)
1522 struct ceph_osd_request *req, *tmp;
1524 dout("__send_queued\n");
1525 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1526 __send_request(osdc, req);
1530 * Caller should hold map_sem for read and request_mutex.
1532 static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1533 struct ceph_osd_request *req,
1538 __register_request(osdc, req);
1540 req->r_got_reply = 0;
1541 rc = __map_request(osdc, req, 0);
1544 dout("osdc_start_request failed map, "
1545 " will retry %lld\n", req->r_tid);
1548 __unregister_request(osdc, req);
1553 if (req->r_osd == NULL) {
1554 dout("send_request %p no up osds in pg\n", req);
1555 ceph_monc_request_next_osdmap(&osdc->client->monc);
1557 __send_queued(osdc);
1564 * Timeout callback, called every N seconds when 1 or more osd
1565 * requests has been active for more than N seconds. When this
1566 * happens, we ping all OSDs with requests who have timed out to
1567 * ensure any communications channel reset is detected. Reset the
1568 * request timeouts another N seconds in the future as we go.
1569 * Reschedule the timeout event another N seconds in future (unless
1570 * there are no open requests).
1572 static void handle_timeout(struct work_struct *work)
1574 struct ceph_osd_client *osdc =
1575 container_of(work, struct ceph_osd_client, timeout_work.work);
1576 struct ceph_osd_request *req;
1577 struct ceph_osd *osd;
1578 unsigned long keepalive =
1579 osdc->client->options->osd_keepalive_timeout * HZ;
1580 struct list_head slow_osds;
1582 down_read(&osdc->map_sem);
1584 ceph_monc_request_next_osdmap(&osdc->client->monc);
1586 mutex_lock(&osdc->request_mutex);
1589 * ping osds that are a bit slow. this ensures that if there
1590 * is a break in the TCP connection we will notice, and reopen
1591 * a connection with that osd (from the fault callback).
1593 INIT_LIST_HEAD(&slow_osds);
1594 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1595 if (time_before(jiffies, req->r_stamp + keepalive))
1600 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1601 req->r_tid, osd->o_osd);
1602 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1604 while (!list_empty(&slow_osds)) {
1605 osd = list_entry(slow_osds.next, struct ceph_osd,
1607 list_del_init(&osd->o_keepalive_item);
1608 ceph_con_keepalive(&osd->o_con);
1611 __schedule_osd_timeout(osdc);
1612 __send_queued(osdc);
1613 mutex_unlock(&osdc->request_mutex);
1614 up_read(&osdc->map_sem);
1617 static void handle_osds_timeout(struct work_struct *work)
1619 struct ceph_osd_client *osdc =
1620 container_of(work, struct ceph_osd_client,
1621 osds_timeout_work.work);
1622 unsigned long delay =
1623 osdc->client->options->osd_idle_ttl * HZ >> 2;
1625 dout("osds timeout\n");
1626 down_read(&osdc->map_sem);
1627 remove_old_osds(osdc);
1628 up_read(&osdc->map_sem);
1630 schedule_delayed_work(&osdc->osds_timeout_work,
1631 round_jiffies_relative(delay));
1634 static int ceph_oloc_decode(void **p, void *end,
1635 struct ceph_object_locator *oloc)
1637 u8 struct_v, struct_cv;
1642 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1643 struct_v = ceph_decode_8(p);
1644 struct_cv = ceph_decode_8(p);
1646 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1647 struct_v, struct_cv);
1650 if (struct_cv > 6) {
1651 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1652 struct_v, struct_cv);
1655 len = ceph_decode_32(p);
1656 ceph_decode_need(p, end, len, e_inval);
1657 struct_end = *p + len;
1659 oloc->pool = ceph_decode_64(p);
1660 *p += 4; /* skip preferred */
1662 len = ceph_decode_32(p);
1664 pr_warn("ceph_object_locator::key is set\n");
1668 if (struct_v >= 5) {
1669 len = ceph_decode_32(p);
1671 pr_warn("ceph_object_locator::nspace is set\n");
1676 if (struct_v >= 6) {
1677 s64 hash = ceph_decode_64(p);
1679 pr_warn("ceph_object_locator::hash is set\n");
1694 static int ceph_redirect_decode(void **p, void *end,
1695 struct ceph_request_redirect *redir)
1697 u8 struct_v, struct_cv;
1702 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1703 struct_v = ceph_decode_8(p);
1704 struct_cv = ceph_decode_8(p);
1705 if (struct_cv > 1) {
1706 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1707 struct_v, struct_cv);
1710 len = ceph_decode_32(p);
1711 ceph_decode_need(p, end, len, e_inval);
1712 struct_end = *p + len;
1714 ret = ceph_oloc_decode(p, end, &redir->oloc);
1718 len = ceph_decode_32(p);
1720 pr_warn("ceph_request_redirect::object_name is set\n");
1724 len = ceph_decode_32(p);
1725 *p += len; /* skip osd_instructions */
1737 static void complete_request(struct ceph_osd_request *req)
1739 complete_all(&req->r_safe_completion); /* fsync waiter */
1743 * handle osd op reply. either call the callback if it is specified,
1744 * or do the completion to wake up the waiting thread.
1746 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1747 struct ceph_connection *con)
1750 struct ceph_osd_request *req;
1751 struct ceph_request_redirect redir;
1754 unsigned int numops;
1755 int payload_len, flags;
1761 u64 reassert_version;
1763 int already_completed;
1767 tid = le64_to_cpu(msg->hdr.tid);
1768 dout("handle_reply %p tid %llu\n", msg, tid);
1770 p = msg->front.iov_base;
1771 end = p + msg->front.iov_len;
1773 ceph_decode_need(&p, end, 4, bad);
1774 object_len = ceph_decode_32(&p);
1775 ceph_decode_need(&p, end, object_len, bad);
1778 err = ceph_decode_pgid(&p, end, &pg);
1782 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1783 flags = ceph_decode_64(&p);
1784 result = ceph_decode_32(&p);
1785 reassert_epoch = ceph_decode_32(&p);
1786 reassert_version = ceph_decode_64(&p);
1787 osdmap_epoch = ceph_decode_32(&p);
1790 down_read(&osdc->map_sem);
1791 mutex_lock(&osdc->request_mutex);
1792 req = __lookup_request(osdc, tid);
1794 dout("handle_reply tid %llu dne\n", tid);
1797 ceph_osdc_get_request(req);
1799 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1802 ceph_decode_need(&p, end, 4, bad_put);
1803 numops = ceph_decode_32(&p);
1804 if (numops > CEPH_OSD_MAX_OP)
1806 if (numops != req->r_num_ops)
1809 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
1810 for (i = 0; i < numops; i++) {
1811 struct ceph_osd_op *op = p;
1814 len = le32_to_cpu(op->payload_len);
1815 req->r_reply_op_len[i] = len;
1816 dout(" op %d has %d bytes\n", i, len);
1820 bytes = le32_to_cpu(msg->hdr.data_len);
1821 if (payload_len != bytes) {
1822 pr_warning("sum of op payload lens %d != data_len %d",
1823 payload_len, bytes);
1827 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
1828 retry_attempt = ceph_decode_32(&p);
1829 for (i = 0; i < numops; i++)
1830 req->r_reply_op_result[i] = ceph_decode_32(&p);
1832 if (le16_to_cpu(msg->hdr.version) >= 6) {
1833 p += 8 + 4; /* skip replay_version */
1834 p += 8; /* skip user_version */
1836 err = ceph_redirect_decode(&p, end, &redir);
1840 redir.oloc.pool = -1;
1843 if (redir.oloc.pool != -1) {
1844 dout("redirect pool %lld\n", redir.oloc.pool);
1846 __unregister_request(osdc, req);
1848 req->r_target_oloc = redir.oloc; /* struct */
1851 * Start redirect requests with nofail=true. If
1852 * mapping fails, request will end up on the notarget
1853 * list, waiting for the new osdmap (which can take
1854 * a while), even though the original request mapped
1855 * successfully. In the future we might want to follow
1856 * original request's nofail setting here.
1858 err = __ceph_osdc_start_request(osdc, req, true);
1864 already_completed = req->r_got_reply;
1865 if (!req->r_got_reply) {
1866 req->r_result = result;
1867 dout("handle_reply result %d bytes %d\n", req->r_result,
1869 if (req->r_result == 0)
1870 req->r_result = bytes;
1872 /* in case this is a write and we need to replay, */
1873 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1874 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1876 req->r_got_reply = 1;
1877 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1878 dout("handle_reply tid %llu dup ack\n", tid);
1882 dout("handle_reply tid %llu flags %d\n", tid, flags);
1884 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1885 __register_linger_request(osdc, req);
1887 /* either this is a read, or we got the safe response */
1889 (flags & CEPH_OSD_FLAG_ONDISK) ||
1890 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1891 __unregister_request(osdc, req);
1893 mutex_unlock(&osdc->request_mutex);
1894 up_read(&osdc->map_sem);
1896 if (!already_completed) {
1897 if (req->r_unsafe_callback &&
1898 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1899 req->r_unsafe_callback(req, true);
1900 if (req->r_callback)
1901 req->r_callback(req, msg);
1903 complete_all(&req->r_completion);
1906 if (flags & CEPH_OSD_FLAG_ONDISK) {
1907 if (req->r_unsafe_callback && already_completed)
1908 req->r_unsafe_callback(req, false);
1909 complete_request(req);
1913 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1914 ceph_osdc_put_request(req);
1917 mutex_unlock(&osdc->request_mutex);
1918 up_read(&osdc->map_sem);
1922 req->r_result = -EIO;
1923 __unregister_request(osdc, req);
1924 if (req->r_callback)
1925 req->r_callback(req, msg);
1927 complete_all(&req->r_completion);
1928 complete_request(req);
1929 ceph_osdc_put_request(req);
1931 mutex_unlock(&osdc->request_mutex);
1932 up_read(&osdc->map_sem);
1934 pr_err("corrupt osd_op_reply got %d %d\n",
1935 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1939 static void reset_changed_osds(struct ceph_osd_client *osdc)
1941 struct rb_node *p, *n;
1943 for (p = rb_first(&osdc->osds); p; p = n) {
1944 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1947 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1948 memcmp(&osd->o_con.peer_addr,
1949 ceph_osd_addr(osdc->osdmap,
1951 sizeof(struct ceph_entity_addr)) != 0)
1952 __reset_osd(osdc, osd);
1957 * Requeue requests whose mapping to an OSD has changed. If requests map to
1958 * no osd, request a new map.
1960 * Caller should hold map_sem for read.
1962 static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1963 bool force_resend_writes)
1965 struct ceph_osd_request *req, *nreq;
1969 bool force_resend_req;
1971 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1972 force_resend_writes ? " (force resend writes)" : "");
1973 mutex_lock(&osdc->request_mutex);
1974 for (p = rb_first(&osdc->requests); p; ) {
1975 req = rb_entry(p, struct ceph_osd_request, r_node);
1979 * For linger requests that have not yet been
1980 * registered, move them to the linger list; they'll
1981 * be sent to the osd in the loop below. Unregister
1982 * the request before re-registering it as a linger
1983 * request to ensure the __map_request() below
1984 * will decide it needs to be sent.
1986 if (req->r_linger && list_empty(&req->r_linger_item)) {
1987 dout("%p tid %llu restart on osd%d\n",
1989 req->r_osd ? req->r_osd->o_osd : -1);
1990 ceph_osdc_get_request(req);
1991 __unregister_request(osdc, req);
1992 __register_linger_request(osdc, req);
1993 ceph_osdc_put_request(req);
1997 force_resend_req = force_resend ||
1998 (force_resend_writes &&
1999 req->r_flags & CEPH_OSD_FLAG_WRITE);
2000 err = __map_request(osdc, req, force_resend_req);
2002 continue; /* error */
2003 if (req->r_osd == NULL) {
2004 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2005 needmap++; /* request a newer map */
2006 } else if (err > 0) {
2007 if (!req->r_linger) {
2008 dout("%p tid %llu requeued on osd%d\n", req,
2010 req->r_osd ? req->r_osd->o_osd : -1);
2011 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2016 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2018 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2020 err = __map_request(osdc, req,
2021 force_resend || force_resend_writes);
2022 dout("__map_request returned %d\n", err);
2024 continue; /* no change and no osd was specified */
2026 continue; /* hrm! */
2027 if (req->r_osd == NULL) {
2028 dout("tid %llu maps to no valid osd\n", req->r_tid);
2029 needmap++; /* request a newer map */
2033 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
2034 req->r_osd ? req->r_osd->o_osd : -1);
2035 __register_request(osdc, req);
2036 __unregister_linger_request(osdc, req);
2038 reset_changed_osds(osdc);
2039 mutex_unlock(&osdc->request_mutex);
2042 dout("%d requests for down osds, need new map\n", needmap);
2043 ceph_monc_request_next_osdmap(&osdc->client->monc);
2049 * Process updated osd map.
2051 * The message contains any number of incremental and full maps, normally
2052 * indicating some sort of topology change in the cluster. Kick requests
2053 * off to different OSDs as needed.
2055 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2057 void *p, *end, *next;
2058 u32 nr_maps, maplen;
2060 struct ceph_osdmap *newmap = NULL, *oldmap;
2062 struct ceph_fsid fsid;
2065 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2066 p = msg->front.iov_base;
2067 end = p + msg->front.iov_len;
2070 ceph_decode_need(&p, end, sizeof(fsid), bad);
2071 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2072 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2075 down_write(&osdc->map_sem);
2077 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2079 /* incremental maps */
2080 ceph_decode_32_safe(&p, end, nr_maps, bad);
2081 dout(" %d inc maps\n", nr_maps);
2082 while (nr_maps > 0) {
2083 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2084 epoch = ceph_decode_32(&p);
2085 maplen = ceph_decode_32(&p);
2086 ceph_decode_need(&p, end, maplen, bad);
2088 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2089 dout("applying incremental map %u len %d\n",
2091 newmap = osdmap_apply_incremental(&p, next,
2093 &osdc->client->msgr);
2094 if (IS_ERR(newmap)) {
2095 err = PTR_ERR(newmap);
2099 if (newmap != osdc->osdmap) {
2100 ceph_osdmap_destroy(osdc->osdmap);
2101 osdc->osdmap = newmap;
2103 was_full = was_full ||
2104 ceph_osdmap_flag(osdc->osdmap,
2106 kick_requests(osdc, 0, was_full);
2108 dout("ignoring incremental map %u len %d\n",
2118 ceph_decode_32_safe(&p, end, nr_maps, bad);
2119 dout(" %d full maps\n", nr_maps);
2121 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2122 epoch = ceph_decode_32(&p);
2123 maplen = ceph_decode_32(&p);
2124 ceph_decode_need(&p, end, maplen, bad);
2126 dout("skipping non-latest full map %u len %d\n",
2128 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2129 dout("skipping full map %u len %d, "
2130 "older than our %u\n", epoch, maplen,
2131 osdc->osdmap->epoch);
2133 int skipped_map = 0;
2135 dout("taking full map %u len %d\n", epoch, maplen);
2136 newmap = ceph_osdmap_decode(&p, p+maplen);
2137 if (IS_ERR(newmap)) {
2138 err = PTR_ERR(newmap);
2142 oldmap = osdc->osdmap;
2143 osdc->osdmap = newmap;
2145 if (oldmap->epoch + 1 < newmap->epoch)
2147 ceph_osdmap_destroy(oldmap);
2149 was_full = was_full ||
2150 ceph_osdmap_flag(osdc->osdmap,
2152 kick_requests(osdc, skipped_map, was_full);
2161 downgrade_write(&osdc->map_sem);
2162 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
2165 * subscribe to subsequent osdmap updates if full to ensure
2166 * we find out when we are no longer full and stop returning
2169 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2170 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2171 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
2172 ceph_monc_request_next_osdmap(&osdc->client->monc);
2174 mutex_lock(&osdc->request_mutex);
2175 __send_queued(osdc);
2176 mutex_unlock(&osdc->request_mutex);
2177 up_read(&osdc->map_sem);
2178 wake_up_all(&osdc->client->auth_wq);
2182 pr_err("osdc handle_map corrupt msg\n");
2184 up_write(&osdc->map_sem);
2188 * watch/notify callback event infrastructure
2190 * These callbacks are used both for watch and notify operations.
2192 static void __release_event(struct kref *kref)
2194 struct ceph_osd_event *event =
2195 container_of(kref, struct ceph_osd_event, kref);
2197 dout("__release_event %p\n", event);
2201 static void get_event(struct ceph_osd_event *event)
2203 kref_get(&event->kref);
2206 void ceph_osdc_put_event(struct ceph_osd_event *event)
2208 kref_put(&event->kref, __release_event);
2210 EXPORT_SYMBOL(ceph_osdc_put_event);
2212 static void __insert_event(struct ceph_osd_client *osdc,
2213 struct ceph_osd_event *new)
2215 struct rb_node **p = &osdc->event_tree.rb_node;
2216 struct rb_node *parent = NULL;
2217 struct ceph_osd_event *event = NULL;
2221 event = rb_entry(parent, struct ceph_osd_event, node);
2222 if (new->cookie < event->cookie)
2224 else if (new->cookie > event->cookie)
2225 p = &(*p)->rb_right;
2230 rb_link_node(&new->node, parent, p);
2231 rb_insert_color(&new->node, &osdc->event_tree);
2234 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2237 struct rb_node **p = &osdc->event_tree.rb_node;
2238 struct rb_node *parent = NULL;
2239 struct ceph_osd_event *event = NULL;
2243 event = rb_entry(parent, struct ceph_osd_event, node);
2244 if (cookie < event->cookie)
2246 else if (cookie > event->cookie)
2247 p = &(*p)->rb_right;
2254 static void __remove_event(struct ceph_osd_event *event)
2256 struct ceph_osd_client *osdc = event->osdc;
2258 if (!RB_EMPTY_NODE(&event->node)) {
2259 dout("__remove_event removed %p\n", event);
2260 rb_erase(&event->node, &osdc->event_tree);
2261 ceph_osdc_put_event(event);
2263 dout("__remove_event didn't remove %p\n", event);
2267 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2268 void (*event_cb)(u64, u64, u8, void *),
2269 void *data, struct ceph_osd_event **pevent)
2271 struct ceph_osd_event *event;
2273 event = kmalloc(sizeof(*event), GFP_NOIO);
2277 dout("create_event %p\n", event);
2278 event->cb = event_cb;
2279 event->one_shot = 0;
2282 INIT_LIST_HEAD(&event->osd_node);
2283 RB_CLEAR_NODE(&event->node);
2284 kref_init(&event->kref); /* one ref for us */
2285 kref_get(&event->kref); /* one ref for the caller */
2287 spin_lock(&osdc->event_lock);
2288 event->cookie = ++osdc->event_count;
2289 __insert_event(osdc, event);
2290 spin_unlock(&osdc->event_lock);
2295 EXPORT_SYMBOL(ceph_osdc_create_event);
2297 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2299 struct ceph_osd_client *osdc = event->osdc;
2301 dout("cancel_event %p\n", event);
2302 spin_lock(&osdc->event_lock);
2303 __remove_event(event);
2304 spin_unlock(&osdc->event_lock);
2305 ceph_osdc_put_event(event); /* caller's */
2307 EXPORT_SYMBOL(ceph_osdc_cancel_event);
2310 static void do_event_work(struct work_struct *work)
2312 struct ceph_osd_event_work *event_work =
2313 container_of(work, struct ceph_osd_event_work, work);
2314 struct ceph_osd_event *event = event_work->event;
2315 u64 ver = event_work->ver;
2316 u64 notify_id = event_work->notify_id;
2317 u8 opcode = event_work->opcode;
2319 dout("do_event_work completing %p\n", event);
2320 event->cb(ver, notify_id, opcode, event->data);
2321 dout("do_event_work completed %p\n", event);
2322 ceph_osdc_put_event(event);
2328 * Process osd watch notifications
2330 static void handle_watch_notify(struct ceph_osd_client *osdc,
2331 struct ceph_msg *msg)
2335 u64 cookie, ver, notify_id;
2337 struct ceph_osd_event *event;
2338 struct ceph_osd_event_work *event_work;
2340 p = msg->front.iov_base;
2341 end = p + msg->front.iov_len;
2343 ceph_decode_8_safe(&p, end, proto_ver, bad);
2344 ceph_decode_8_safe(&p, end, opcode, bad);
2345 ceph_decode_64_safe(&p, end, cookie, bad);
2346 ceph_decode_64_safe(&p, end, ver, bad);
2347 ceph_decode_64_safe(&p, end, notify_id, bad);
2349 spin_lock(&osdc->event_lock);
2350 event = __find_event(osdc, cookie);
2352 BUG_ON(event->one_shot);
2355 spin_unlock(&osdc->event_lock);
2356 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2357 cookie, ver, event);
2359 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
2361 dout("ERROR: could not allocate event_work\n");
2364 INIT_WORK(&event_work->work, do_event_work);
2365 event_work->event = event;
2366 event_work->ver = ver;
2367 event_work->notify_id = notify_id;
2368 event_work->opcode = opcode;
2369 if (!queue_work(osdc->notify_wq, &event_work->work)) {
2370 dout("WARNING: failed to queue notify event work\n");
2378 ceph_osdc_put_event(event);
2382 pr_err("osdc handle_watch_notify corrupt msg\n");
2386 * build new request AND message
2389 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2390 struct ceph_snap_context *snapc, u64 snap_id,
2391 struct timespec *mtime)
2393 struct ceph_msg *msg = req->r_request;
2396 int flags = req->r_flags;
2400 req->r_snapid = snap_id;
2401 req->r_snapc = ceph_get_snap_context(snapc);
2403 /* encode request */
2404 msg->hdr.version = cpu_to_le16(4);
2406 p = msg->front.iov_base;
2407 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2408 req->r_request_osdmap_epoch = p;
2410 req->r_request_flags = p;
2412 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2413 ceph_encode_timespec(p, mtime);
2414 p += sizeof(struct ceph_timespec);
2415 req->r_request_reassert_version = p;
2416 p += sizeof(struct ceph_eversion); /* will get filled in */
2419 ceph_encode_8(&p, 4);
2420 ceph_encode_8(&p, 4);
2421 ceph_encode_32(&p, 8 + 4 + 4);
2422 req->r_request_pool = p;
2424 ceph_encode_32(&p, -1); /* preferred */
2425 ceph_encode_32(&p, 0); /* key len */
2427 ceph_encode_8(&p, 1);
2428 req->r_request_pgid = p;
2430 ceph_encode_32(&p, -1); /* preferred */
2433 ceph_encode_32(&p, req->r_base_oid.name_len);
2434 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2435 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2436 req->r_base_oid.name, req->r_base_oid.name_len);
2437 p += req->r_base_oid.name_len;
2439 /* ops--can imply data */
2440 ceph_encode_16(&p, (u16)req->r_num_ops);
2442 for (i = 0; i < req->r_num_ops; i++) {
2443 data_len += osd_req_encode_op(req, p, i);
2444 p += sizeof(struct ceph_osd_op);
2448 ceph_encode_64(&p, req->r_snapid);
2449 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2450 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2452 for (i = 0; i < snapc->num_snaps; i++) {
2453 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2457 req->r_request_attempts = p;
2461 if (flags & CEPH_OSD_FLAG_WRITE) {
2465 * The header "data_off" is a hint to the receiver
2466 * allowing it to align received data into its
2467 * buffers such that there's no need to re-copy
2468 * it before writing it to disk (direct I/O).
2470 data_off = (u16) (off & 0xffff);
2471 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2473 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2475 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2476 msg_size = p - msg->front.iov_base;
2477 msg->front.iov_len = msg_size;
2478 msg->hdr.front_len = cpu_to_le32(msg_size);
2480 dout("build_request msg_size was %d\n", (int)msg_size);
2482 EXPORT_SYMBOL(ceph_osdc_build_request);
2485 * Register request, send initial attempt.
2487 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2488 struct ceph_osd_request *req,
2493 down_read(&osdc->map_sem);
2494 mutex_lock(&osdc->request_mutex);
2496 rc = __ceph_osdc_start_request(osdc, req, nofail);
2498 mutex_unlock(&osdc->request_mutex);
2499 up_read(&osdc->map_sem);
2503 EXPORT_SYMBOL(ceph_osdc_start_request);
2506 * Unregister a registered request. The request is not completed (i.e.
2507 * no callbacks or wakeups) - higher layers are supposed to know what
2508 * they are canceling.
2510 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2512 struct ceph_osd_client *osdc = req->r_osdc;
2514 mutex_lock(&osdc->request_mutex);
2516 __unregister_linger_request(osdc, req);
2517 __unregister_request(osdc, req);
2518 mutex_unlock(&osdc->request_mutex);
2520 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2522 EXPORT_SYMBOL(ceph_osdc_cancel_request);
2525 * wait for a request to complete
2527 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2528 struct ceph_osd_request *req)
2532 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2534 rc = wait_for_completion_interruptible(&req->r_completion);
2536 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2537 ceph_osdc_cancel_request(req);
2538 complete_request(req);
2542 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2544 return req->r_result;
2546 EXPORT_SYMBOL(ceph_osdc_wait_request);
2549 * sync - wait for all in-flight requests to flush. avoid starvation.
2551 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2553 struct ceph_osd_request *req;
2554 u64 last_tid, next_tid = 0;
2556 mutex_lock(&osdc->request_mutex);
2557 last_tid = osdc->last_tid;
2559 req = __lookup_request_ge(osdc, next_tid);
2562 if (req->r_tid > last_tid)
2565 next_tid = req->r_tid + 1;
2566 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2569 ceph_osdc_get_request(req);
2570 mutex_unlock(&osdc->request_mutex);
2571 dout("sync waiting on tid %llu (last is %llu)\n",
2572 req->r_tid, last_tid);
2573 wait_for_completion(&req->r_safe_completion);
2574 mutex_lock(&osdc->request_mutex);
2575 ceph_osdc_put_request(req);
2577 mutex_unlock(&osdc->request_mutex);
2578 dout("sync done (thru tid %llu)\n", last_tid);
2580 EXPORT_SYMBOL(ceph_osdc_sync);
2583 * Call all pending notify callbacks - for use after a watch is
2584 * unregistered, to make sure no more callbacks for it will be invoked
2586 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2588 flush_workqueue(osdc->notify_wq);
2590 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2596 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2601 osdc->client = client;
2602 osdc->osdmap = NULL;
2603 init_rwsem(&osdc->map_sem);
2604 init_completion(&osdc->map_waiters);
2605 osdc->last_requested_map = 0;
2606 mutex_init(&osdc->request_mutex);
2608 osdc->osds = RB_ROOT;
2609 INIT_LIST_HEAD(&osdc->osd_lru);
2610 osdc->requests = RB_ROOT;
2611 INIT_LIST_HEAD(&osdc->req_lru);
2612 INIT_LIST_HEAD(&osdc->req_unsent);
2613 INIT_LIST_HEAD(&osdc->req_notarget);
2614 INIT_LIST_HEAD(&osdc->req_linger);
2615 osdc->num_requests = 0;
2616 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2617 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2618 spin_lock_init(&osdc->event_lock);
2619 osdc->event_tree = RB_ROOT;
2620 osdc->event_count = 0;
2622 schedule_delayed_work(&osdc->osds_timeout_work,
2623 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2626 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2627 sizeof(struct ceph_osd_request));
2628 if (!osdc->req_mempool)
2631 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2632 OSD_OP_FRONT_LEN, 10, true,
2636 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2637 OSD_OPREPLY_FRONT_LEN, 10, true,
2643 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2644 if (!osdc->notify_wq)
2645 goto out_msgpool_reply;
2650 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2652 ceph_msgpool_destroy(&osdc->msgpool_op);
2654 mempool_destroy(osdc->req_mempool);
2659 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2661 flush_workqueue(osdc->notify_wq);
2662 destroy_workqueue(osdc->notify_wq);
2663 cancel_delayed_work_sync(&osdc->timeout_work);
2664 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2666 ceph_osdmap_destroy(osdc->osdmap);
2667 osdc->osdmap = NULL;
2669 remove_all_osds(osdc);
2670 mempool_destroy(osdc->req_mempool);
2671 ceph_msgpool_destroy(&osdc->msgpool_op);
2672 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2676 * Read some contiguous pages. If we cross a stripe boundary, shorten
2677 * *plen. Return number of bytes read, or error.
2679 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2680 struct ceph_vino vino, struct ceph_file_layout *layout,
2682 u32 truncate_seq, u64 truncate_size,
2683 struct page **pages, int num_pages, int page_align)
2685 struct ceph_osd_request *req;
2688 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2689 vino.snap, off, *plen);
2690 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
2691 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2692 NULL, truncate_seq, truncate_size,
2695 return PTR_ERR(req);
2697 /* it may be a short read due to an object boundary */
2699 osd_req_op_extent_osd_data_pages(req, 0,
2700 pages, *plen, page_align, false, false);
2702 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2703 off, *plen, *plen, page_align);
2705 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2707 rc = ceph_osdc_start_request(osdc, req, false);
2709 rc = ceph_osdc_wait_request(osdc, req);
2711 ceph_osdc_put_request(req);
2712 dout("readpages result %d\n", rc);
2715 EXPORT_SYMBOL(ceph_osdc_readpages);
2718 * do a synchronous write on N pages
2720 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2721 struct ceph_file_layout *layout,
2722 struct ceph_snap_context *snapc,
2724 u32 truncate_seq, u64 truncate_size,
2725 struct timespec *mtime,
2726 struct page **pages, int num_pages)
2728 struct ceph_osd_request *req;
2730 int page_align = off & ~PAGE_MASK;
2732 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2733 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
2735 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2736 snapc, truncate_seq, truncate_size,
2739 return PTR_ERR(req);
2741 /* it may be a short write due to an object boundary */
2742 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2744 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2746 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2748 rc = ceph_osdc_start_request(osdc, req, true);
2750 rc = ceph_osdc_wait_request(osdc, req);
2752 ceph_osdc_put_request(req);
2755 dout("writepages result %d\n", rc);
2758 EXPORT_SYMBOL(ceph_osdc_writepages);
2760 int ceph_osdc_setup(void)
2762 BUG_ON(ceph_osd_request_cache);
2763 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2764 sizeof (struct ceph_osd_request),
2765 __alignof__(struct ceph_osd_request),
2768 return ceph_osd_request_cache ? 0 : -ENOMEM;
2770 EXPORT_SYMBOL(ceph_osdc_setup);
2772 void ceph_osdc_cleanup(void)
2774 BUG_ON(!ceph_osd_request_cache);
2775 kmem_cache_destroy(ceph_osd_request_cache);
2776 ceph_osd_request_cache = NULL;
2778 EXPORT_SYMBOL(ceph_osdc_cleanup);
2781 * handle incoming message
2783 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2785 struct ceph_osd *osd = con->private;
2786 struct ceph_osd_client *osdc;
2787 int type = le16_to_cpu(msg->hdr.type);
2794 case CEPH_MSG_OSD_MAP:
2795 ceph_osdc_handle_map(osdc, msg);
2797 case CEPH_MSG_OSD_OPREPLY:
2798 handle_reply(osdc, msg, con);
2800 case CEPH_MSG_WATCH_NOTIFY:
2801 handle_watch_notify(osdc, msg);
2805 pr_err("received unknown message type %d %s\n", type,
2806 ceph_msg_type_name(type));
2813 * lookup and return message for incoming reply. set up reply message
2816 static struct ceph_msg *get_reply(struct ceph_connection *con,
2817 struct ceph_msg_header *hdr,
2820 struct ceph_osd *osd = con->private;
2821 struct ceph_osd_client *osdc = osd->o_osdc;
2823 struct ceph_osd_request *req;
2824 int front_len = le32_to_cpu(hdr->front_len);
2825 int data_len = le32_to_cpu(hdr->data_len);
2828 tid = le64_to_cpu(hdr->tid);
2829 mutex_lock(&osdc->request_mutex);
2830 req = __lookup_request(osdc, tid);
2834 dout("get_reply unknown tid %llu from osd%d\n", tid,
2839 if (req->r_reply->con)
2840 dout("%s revoking msg %p from old con %p\n", __func__,
2841 req->r_reply, req->r_reply->con);
2842 ceph_msg_revoke_incoming(req->r_reply);
2844 if (front_len > req->r_reply->front_alloc_len) {
2845 pr_warning("get_reply front %d > preallocated %d (%u#%llu)\n",
2846 front_len, req->r_reply->front_alloc_len,
2847 (unsigned int)con->peer_name.type,
2848 le64_to_cpu(con->peer_name.num));
2849 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2853 ceph_msg_put(req->r_reply);
2856 m = ceph_msg_get(req->r_reply);
2859 struct ceph_osd_data *osd_data;
2862 * XXX This is assuming there is only one op containing
2863 * XXX page data. Probably OK for reads, but this
2864 * XXX ought to be done more generally.
2866 osd_data = osd_req_op_extent_osd_data(req, 0);
2867 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2868 if (osd_data->pages &&
2869 unlikely(osd_data->length < data_len)) {
2871 pr_warning("tid %lld reply has %d bytes "
2872 "we had only %llu bytes ready\n",
2873 tid, data_len, osd_data->length);
2882 dout("get_reply tid %lld %p\n", tid, m);
2885 mutex_unlock(&osdc->request_mutex);
2890 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2891 struct ceph_msg_header *hdr,
2894 struct ceph_osd *osd = con->private;
2895 int type = le16_to_cpu(hdr->type);
2896 int front = le32_to_cpu(hdr->front_len);
2900 case CEPH_MSG_OSD_MAP:
2901 case CEPH_MSG_WATCH_NOTIFY:
2902 return ceph_msg_new(type, front, GFP_NOFS, false);
2903 case CEPH_MSG_OSD_OPREPLY:
2904 return get_reply(con, hdr, skip);
2906 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2914 * Wrappers to refcount containing ceph_osd struct
2916 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2918 struct ceph_osd *osd = con->private;
2924 static void put_osd_con(struct ceph_connection *con)
2926 struct ceph_osd *osd = con->private;
2934 * Note: returned pointer is the address of a structure that's
2935 * managed separately. Caller must *not* attempt to free it.
2937 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2938 int *proto, int force_new)
2940 struct ceph_osd *o = con->private;
2941 struct ceph_osd_client *osdc = o->o_osdc;
2942 struct ceph_auth_client *ac = osdc->client->monc.auth;
2943 struct ceph_auth_handshake *auth = &o->o_auth;
2945 if (force_new && auth->authorizer) {
2946 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2947 auth->authorizer = NULL;
2949 if (!auth->authorizer) {
2950 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2953 return ERR_PTR(ret);
2955 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2958 return ERR_PTR(ret);
2960 *proto = ac->protocol;
2966 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2968 struct ceph_osd *o = con->private;
2969 struct ceph_osd_client *osdc = o->o_osdc;
2970 struct ceph_auth_client *ac = osdc->client->monc.auth;
2972 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2975 static int invalidate_authorizer(struct ceph_connection *con)
2977 struct ceph_osd *o = con->private;
2978 struct ceph_osd_client *osdc = o->o_osdc;
2979 struct ceph_auth_client *ac = osdc->client->monc.auth;
2981 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2982 return ceph_monc_validate_auth(&osdc->client->monc);
2985 static const struct ceph_connection_operations osd_con_ops = {
2988 .dispatch = dispatch,
2989 .get_authorizer = get_authorizer,
2990 .verify_authorizer_reply = verify_authorizer_reply,
2991 .invalidate_authorizer = invalidate_authorizer,
2992 .alloc_msg = alloc_msg,