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_OPREPLY_FRONT_LEN 512
24 static struct kmem_cache *ceph_osd_request_cache;
26 static const struct ceph_connection_operations osd_con_ops;
29 * Implement client access to distributed object storage cluster.
31 * All data objects are stored within a cluster/cloud of OSDs, or
32 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
33 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
34 * remote daemons serving up and coordinating consistent and safe
37 * Cluster membership and the mapping of data objects onto storage devices
38 * are described by the osd map.
40 * We keep track of pending OSD requests (read, write), resubmit
41 * requests to different OSDs when the cluster topology/data layout
42 * change, or retry the affected requests when the communications
43 * channel with an OSD is reset.
46 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
47 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
48 static void link_linger(struct ceph_osd *osd,
49 struct ceph_osd_linger_request *lreq);
50 static void unlink_linger(struct ceph_osd *osd,
51 struct ceph_osd_linger_request *lreq);
54 static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
58 if (unlikely(down_read_trylock(sem))) {
65 static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
67 WARN_ON(!rwsem_is_locked(&osdc->lock));
69 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
71 WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
73 static inline void verify_osd_locked(struct ceph_osd *osd)
75 struct ceph_osd_client *osdc = osd->o_osdc;
77 WARN_ON(!(mutex_is_locked(&osd->lock) &&
78 rwsem_is_locked(&osdc->lock)) &&
79 !rwsem_is_wrlocked(&osdc->lock));
81 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
83 WARN_ON(!mutex_is_locked(&lreq->lock));
86 static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
87 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
88 static inline void verify_osd_locked(struct ceph_osd *osd) { }
89 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
93 * calculate the mapping of a file extent onto an object, and fill out the
94 * request accordingly. shorten extent as necessary if it crosses an
97 * fill osd op in request message.
99 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
100 u64 *objnum, u64 *objoff, u64 *objlen)
102 u64 orig_len = *plen;
106 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
110 if (*objlen < orig_len) {
112 dout(" skipping last %llu, final file extent %llu~%llu\n",
113 orig_len - *plen, off, *plen);
116 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
121 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
123 memset(osd_data, 0, sizeof (*osd_data));
124 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
128 struct page **pages, u64 length, u32 alignment,
129 bool pages_from_pool, bool own_pages)
131 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
132 osd_data->pages = pages;
133 osd_data->length = length;
134 osd_data->alignment = alignment;
135 osd_data->pages_from_pool = pages_from_pool;
136 osd_data->own_pages = own_pages;
139 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
140 struct ceph_pagelist *pagelist)
142 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
143 osd_data->pagelist = pagelist;
147 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
148 struct bio *bio, size_t bio_length)
150 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
152 osd_data->bio_length = bio_length;
154 #endif /* CONFIG_BLOCK */
156 #define osd_req_op_data(oreq, whch, typ, fld) \
158 struct ceph_osd_request *__oreq = (oreq); \
159 unsigned int __whch = (whch); \
160 BUG_ON(__whch >= __oreq->r_num_ops); \
161 &__oreq->r_ops[__whch].typ.fld; \
164 static struct ceph_osd_data *
165 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
167 BUG_ON(which >= osd_req->r_num_ops);
169 return &osd_req->r_ops[which].raw_data_in;
172 struct ceph_osd_data *
173 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
176 return osd_req_op_data(osd_req, which, extent, osd_data);
178 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
180 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
181 unsigned int which, struct page **pages,
182 u64 length, u32 alignment,
183 bool pages_from_pool, bool own_pages)
185 struct ceph_osd_data *osd_data;
187 osd_data = osd_req_op_raw_data_in(osd_req, which);
188 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
189 pages_from_pool, own_pages);
191 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
193 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
194 unsigned int which, struct page **pages,
195 u64 length, u32 alignment,
196 bool pages_from_pool, bool own_pages)
198 struct ceph_osd_data *osd_data;
200 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
201 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
202 pages_from_pool, own_pages);
204 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
206 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
207 unsigned int which, struct ceph_pagelist *pagelist)
209 struct ceph_osd_data *osd_data;
211 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
212 ceph_osd_data_pagelist_init(osd_data, pagelist);
214 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
217 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
218 unsigned int which, struct bio *bio, size_t bio_length)
220 struct ceph_osd_data *osd_data;
222 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
223 ceph_osd_data_bio_init(osd_data, bio, bio_length);
225 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
226 #endif /* CONFIG_BLOCK */
228 static void osd_req_op_cls_request_info_pagelist(
229 struct ceph_osd_request *osd_req,
230 unsigned int which, struct ceph_pagelist *pagelist)
232 struct ceph_osd_data *osd_data;
234 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
235 ceph_osd_data_pagelist_init(osd_data, pagelist);
238 void osd_req_op_cls_request_data_pagelist(
239 struct ceph_osd_request *osd_req,
240 unsigned int which, struct ceph_pagelist *pagelist)
242 struct ceph_osd_data *osd_data;
244 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
245 ceph_osd_data_pagelist_init(osd_data, pagelist);
246 osd_req->r_ops[which].cls.indata_len += pagelist->length;
247 osd_req->r_ops[which].indata_len += pagelist->length;
249 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
251 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
252 unsigned int which, struct page **pages, u64 length,
253 u32 alignment, bool pages_from_pool, bool own_pages)
255 struct ceph_osd_data *osd_data;
257 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
258 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
259 pages_from_pool, own_pages);
260 osd_req->r_ops[which].cls.indata_len += length;
261 osd_req->r_ops[which].indata_len += length;
263 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
265 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
266 unsigned int which, struct page **pages, u64 length,
267 u32 alignment, bool pages_from_pool, bool own_pages)
269 struct ceph_osd_data *osd_data;
271 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
272 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
273 pages_from_pool, own_pages);
275 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
277 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
279 switch (osd_data->type) {
280 case CEPH_OSD_DATA_TYPE_NONE:
282 case CEPH_OSD_DATA_TYPE_PAGES:
283 return osd_data->length;
284 case CEPH_OSD_DATA_TYPE_PAGELIST:
285 return (u64)osd_data->pagelist->length;
287 case CEPH_OSD_DATA_TYPE_BIO:
288 return (u64)osd_data->bio_length;
289 #endif /* CONFIG_BLOCK */
291 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
296 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
298 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
301 num_pages = calc_pages_for((u64)osd_data->alignment,
302 (u64)osd_data->length);
303 ceph_release_page_vector(osd_data->pages, num_pages);
305 ceph_osd_data_init(osd_data);
308 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
311 struct ceph_osd_req_op *op;
313 BUG_ON(which >= osd_req->r_num_ops);
314 op = &osd_req->r_ops[which];
317 case CEPH_OSD_OP_READ:
318 case CEPH_OSD_OP_WRITE:
319 case CEPH_OSD_OP_WRITEFULL:
320 ceph_osd_data_release(&op->extent.osd_data);
322 case CEPH_OSD_OP_CALL:
323 ceph_osd_data_release(&op->cls.request_info);
324 ceph_osd_data_release(&op->cls.request_data);
325 ceph_osd_data_release(&op->cls.response_data);
327 case CEPH_OSD_OP_SETXATTR:
328 case CEPH_OSD_OP_CMPXATTR:
329 ceph_osd_data_release(&op->xattr.osd_data);
331 case CEPH_OSD_OP_STAT:
332 ceph_osd_data_release(&op->raw_data_in);
334 case CEPH_OSD_OP_NOTIFY_ACK:
335 ceph_osd_data_release(&op->notify_ack.request_data);
337 case CEPH_OSD_OP_NOTIFY:
338 ceph_osd_data_release(&op->notify.request_data);
339 ceph_osd_data_release(&op->notify.response_data);
341 case CEPH_OSD_OP_LIST_WATCHERS:
342 ceph_osd_data_release(&op->list_watchers.response_data);
350 * Assumes @t is zero-initialized.
352 static void target_init(struct ceph_osd_request_target *t)
354 ceph_oid_init(&t->base_oid);
355 ceph_oloc_init(&t->base_oloc);
356 ceph_oid_init(&t->target_oid);
357 ceph_oloc_init(&t->target_oloc);
359 ceph_osds_init(&t->acting);
360 ceph_osds_init(&t->up);
364 t->osd = CEPH_HOMELESS_OSD;
367 static void target_copy(struct ceph_osd_request_target *dest,
368 const struct ceph_osd_request_target *src)
370 ceph_oid_copy(&dest->base_oid, &src->base_oid);
371 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
372 ceph_oid_copy(&dest->target_oid, &src->target_oid);
373 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
375 dest->pgid = src->pgid; /* struct */
376 dest->pg_num = src->pg_num;
377 dest->pg_num_mask = src->pg_num_mask;
378 ceph_osds_copy(&dest->acting, &src->acting);
379 ceph_osds_copy(&dest->up, &src->up);
380 dest->size = src->size;
381 dest->min_size = src->min_size;
382 dest->sort_bitwise = src->sort_bitwise;
384 dest->flags = src->flags;
385 dest->paused = src->paused;
387 dest->osd = src->osd;
390 static void target_destroy(struct ceph_osd_request_target *t)
392 ceph_oid_destroy(&t->base_oid);
393 ceph_oloc_destroy(&t->base_oloc);
394 ceph_oid_destroy(&t->target_oid);
395 ceph_oloc_destroy(&t->target_oloc);
401 static void request_release_checks(struct ceph_osd_request *req)
403 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
404 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
405 WARN_ON(!list_empty(&req->r_unsafe_item));
409 static void ceph_osdc_release_request(struct kref *kref)
411 struct ceph_osd_request *req = container_of(kref,
412 struct ceph_osd_request, r_kref);
415 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
416 req->r_request, req->r_reply);
417 request_release_checks(req);
420 ceph_msg_put(req->r_request);
422 ceph_msg_put(req->r_reply);
424 for (which = 0; which < req->r_num_ops; which++)
425 osd_req_op_data_release(req, which);
427 target_destroy(&req->r_t);
428 ceph_put_snap_context(req->r_snapc);
431 mempool_free(req, req->r_osdc->req_mempool);
432 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
433 kmem_cache_free(ceph_osd_request_cache, req);
438 void ceph_osdc_get_request(struct ceph_osd_request *req)
440 dout("%s %p (was %d)\n", __func__, req,
441 kref_read(&req->r_kref));
442 kref_get(&req->r_kref);
444 EXPORT_SYMBOL(ceph_osdc_get_request);
446 void ceph_osdc_put_request(struct ceph_osd_request *req)
449 dout("%s %p (was %d)\n", __func__, req,
450 kref_read(&req->r_kref));
451 kref_put(&req->r_kref, ceph_osdc_release_request);
454 EXPORT_SYMBOL(ceph_osdc_put_request);
456 static void request_init(struct ceph_osd_request *req)
458 /* req only, each op is zeroed in _osd_req_op_init() */
459 memset(req, 0, sizeof(*req));
461 kref_init(&req->r_kref);
462 init_completion(&req->r_completion);
463 RB_CLEAR_NODE(&req->r_node);
464 RB_CLEAR_NODE(&req->r_mc_node);
465 INIT_LIST_HEAD(&req->r_unsafe_item);
467 target_init(&req->r_t);
471 * This is ugly, but it allows us to reuse linger registration and ping
472 * requests, keeping the structure of the code around send_linger{_ping}()
473 * reasonable. Setting up a min_nr=2 mempool for each linger request
474 * and dealing with copying ops (this blasts req only, watch op remains
475 * intact) isn't any better.
477 static void request_reinit(struct ceph_osd_request *req)
479 struct ceph_osd_client *osdc = req->r_osdc;
480 bool mempool = req->r_mempool;
481 unsigned int num_ops = req->r_num_ops;
482 u64 snapid = req->r_snapid;
483 struct ceph_snap_context *snapc = req->r_snapc;
484 bool linger = req->r_linger;
485 struct ceph_msg *request_msg = req->r_request;
486 struct ceph_msg *reply_msg = req->r_reply;
488 dout("%s req %p\n", __func__, req);
489 WARN_ON(kref_read(&req->r_kref) != 1);
490 request_release_checks(req);
492 WARN_ON(kref_read(&request_msg->kref) != 1);
493 WARN_ON(kref_read(&reply_msg->kref) != 1);
494 target_destroy(&req->r_t);
498 req->r_mempool = mempool;
499 req->r_num_ops = num_ops;
500 req->r_snapid = snapid;
501 req->r_snapc = snapc;
502 req->r_linger = linger;
503 req->r_request = request_msg;
504 req->r_reply = reply_msg;
507 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
508 struct ceph_snap_context *snapc,
509 unsigned int num_ops,
513 struct ceph_osd_request *req;
516 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
517 req = mempool_alloc(osdc->req_mempool, gfp_flags);
518 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
519 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
521 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
522 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
530 req->r_mempool = use_mempool;
531 req->r_num_ops = num_ops;
532 req->r_snapid = CEPH_NOSNAP;
533 req->r_snapc = ceph_get_snap_context(snapc);
535 dout("%s req %p\n", __func__, req);
538 EXPORT_SYMBOL(ceph_osdc_alloc_request);
540 static int ceph_oloc_encoding_size(struct ceph_object_locator *oloc)
542 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
545 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
547 struct ceph_osd_client *osdc = req->r_osdc;
548 struct ceph_msg *msg;
551 WARN_ON(ceph_oid_empty(&req->r_base_oid));
552 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
554 /* create request message */
555 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
556 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
557 msg_size += CEPH_ENCODING_START_BLK_LEN +
558 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
559 msg_size += 1 + 8 + 4 + 4; /* pgid */
560 msg_size += 4 + req->r_base_oid.name_len; /* oid */
561 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
562 msg_size += 8; /* snapid */
563 msg_size += 8; /* snap_seq */
564 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
565 msg_size += 4; /* retry_attempt */
568 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
570 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
574 memset(msg->front.iov_base, 0, msg->front.iov_len);
575 req->r_request = msg;
577 /* create reply message */
578 msg_size = OSD_OPREPLY_FRONT_LEN;
579 msg_size += req->r_base_oid.name_len;
580 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
583 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
585 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
593 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
595 static bool osd_req_opcode_valid(u16 opcode)
598 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
599 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
607 * This is an osd op init function for opcodes that have no data or
608 * other information associated with them. It also serves as a
609 * common init routine for all the other init functions, below.
611 static struct ceph_osd_req_op *
612 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
613 u16 opcode, u32 flags)
615 struct ceph_osd_req_op *op;
617 BUG_ON(which >= osd_req->r_num_ops);
618 BUG_ON(!osd_req_opcode_valid(opcode));
620 op = &osd_req->r_ops[which];
621 memset(op, 0, sizeof (*op));
628 void osd_req_op_init(struct ceph_osd_request *osd_req,
629 unsigned int which, u16 opcode, u32 flags)
631 (void)_osd_req_op_init(osd_req, which, opcode, flags);
633 EXPORT_SYMBOL(osd_req_op_init);
635 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
636 unsigned int which, u16 opcode,
637 u64 offset, u64 length,
638 u64 truncate_size, u32 truncate_seq)
640 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
642 size_t payload_len = 0;
644 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
645 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
646 opcode != CEPH_OSD_OP_TRUNCATE);
648 op->extent.offset = offset;
649 op->extent.length = length;
650 op->extent.truncate_size = truncate_size;
651 op->extent.truncate_seq = truncate_seq;
652 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
653 payload_len += length;
655 op->indata_len = payload_len;
657 EXPORT_SYMBOL(osd_req_op_extent_init);
659 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
660 unsigned int which, u64 length)
662 struct ceph_osd_req_op *op;
665 BUG_ON(which >= osd_req->r_num_ops);
666 op = &osd_req->r_ops[which];
667 previous = op->extent.length;
669 if (length == previous)
670 return; /* Nothing to do */
671 BUG_ON(length > previous);
673 op->extent.length = length;
674 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
675 op->indata_len -= previous - length;
677 EXPORT_SYMBOL(osd_req_op_extent_update);
679 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
680 unsigned int which, u64 offset_inc)
682 struct ceph_osd_req_op *op, *prev_op;
684 BUG_ON(which + 1 >= osd_req->r_num_ops);
686 prev_op = &osd_req->r_ops[which];
687 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
688 /* dup previous one */
689 op->indata_len = prev_op->indata_len;
690 op->outdata_len = prev_op->outdata_len;
691 op->extent = prev_op->extent;
693 op->extent.offset += offset_inc;
694 op->extent.length -= offset_inc;
696 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
697 op->indata_len -= offset_inc;
699 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
701 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
702 u16 opcode, const char *class, const char *method)
704 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
706 struct ceph_pagelist *pagelist;
707 size_t payload_len = 0;
710 BUG_ON(opcode != CEPH_OSD_OP_CALL);
712 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
714 ceph_pagelist_init(pagelist);
716 op->cls.class_name = class;
717 size = strlen(class);
718 BUG_ON(size > (size_t) U8_MAX);
719 op->cls.class_len = size;
720 ceph_pagelist_append(pagelist, class, size);
723 op->cls.method_name = method;
724 size = strlen(method);
725 BUG_ON(size > (size_t) U8_MAX);
726 op->cls.method_len = size;
727 ceph_pagelist_append(pagelist, method, size);
730 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
732 op->indata_len = payload_len;
734 EXPORT_SYMBOL(osd_req_op_cls_init);
736 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
737 u16 opcode, const char *name, const void *value,
738 size_t size, u8 cmp_op, u8 cmp_mode)
740 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
742 struct ceph_pagelist *pagelist;
745 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
747 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
751 ceph_pagelist_init(pagelist);
753 payload_len = strlen(name);
754 op->xattr.name_len = payload_len;
755 ceph_pagelist_append(pagelist, name, payload_len);
757 op->xattr.value_len = size;
758 ceph_pagelist_append(pagelist, value, size);
761 op->xattr.cmp_op = cmp_op;
762 op->xattr.cmp_mode = cmp_mode;
764 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
765 op->indata_len = payload_len;
768 EXPORT_SYMBOL(osd_req_op_xattr_init);
771 * @watch_opcode: CEPH_OSD_WATCH_OP_*
773 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
774 u64 cookie, u8 watch_opcode)
776 struct ceph_osd_req_op *op;
778 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
779 op->watch.cookie = cookie;
780 op->watch.op = watch_opcode;
784 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
786 u64 expected_object_size,
787 u64 expected_write_size)
789 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
790 CEPH_OSD_OP_SETALLOCHINT,
793 op->alloc_hint.expected_object_size = expected_object_size;
794 op->alloc_hint.expected_write_size = expected_write_size;
797 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
798 * not worth a feature bit. Set FAILOK per-op flag to make
799 * sure older osds don't trip over an unsupported opcode.
801 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
803 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
805 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
806 struct ceph_osd_data *osd_data)
808 u64 length = ceph_osd_data_length(osd_data);
810 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
811 BUG_ON(length > (u64) SIZE_MAX);
813 ceph_msg_data_add_pages(msg, osd_data->pages,
814 length, osd_data->alignment);
815 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
817 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
819 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
820 ceph_msg_data_add_bio(msg, osd_data->bio, length);
823 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
827 static u32 osd_req_encode_op(struct ceph_osd_op *dst,
828 const struct ceph_osd_req_op *src)
830 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
831 pr_err("unrecognized osd opcode %d\n", src->op);
837 case CEPH_OSD_OP_STAT:
839 case CEPH_OSD_OP_READ:
840 case CEPH_OSD_OP_WRITE:
841 case CEPH_OSD_OP_WRITEFULL:
842 case CEPH_OSD_OP_ZERO:
843 case CEPH_OSD_OP_TRUNCATE:
844 dst->extent.offset = cpu_to_le64(src->extent.offset);
845 dst->extent.length = cpu_to_le64(src->extent.length);
846 dst->extent.truncate_size =
847 cpu_to_le64(src->extent.truncate_size);
848 dst->extent.truncate_seq =
849 cpu_to_le32(src->extent.truncate_seq);
851 case CEPH_OSD_OP_CALL:
852 dst->cls.class_len = src->cls.class_len;
853 dst->cls.method_len = src->cls.method_len;
854 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
856 case CEPH_OSD_OP_STARTSYNC:
858 case CEPH_OSD_OP_WATCH:
859 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
860 dst->watch.ver = cpu_to_le64(0);
861 dst->watch.op = src->watch.op;
862 dst->watch.gen = cpu_to_le32(src->watch.gen);
864 case CEPH_OSD_OP_NOTIFY_ACK:
866 case CEPH_OSD_OP_NOTIFY:
867 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
869 case CEPH_OSD_OP_LIST_WATCHERS:
871 case CEPH_OSD_OP_SETALLOCHINT:
872 dst->alloc_hint.expected_object_size =
873 cpu_to_le64(src->alloc_hint.expected_object_size);
874 dst->alloc_hint.expected_write_size =
875 cpu_to_le64(src->alloc_hint.expected_write_size);
877 case CEPH_OSD_OP_SETXATTR:
878 case CEPH_OSD_OP_CMPXATTR:
879 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
880 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
881 dst->xattr.cmp_op = src->xattr.cmp_op;
882 dst->xattr.cmp_mode = src->xattr.cmp_mode;
884 case CEPH_OSD_OP_CREATE:
885 case CEPH_OSD_OP_DELETE:
888 pr_err("unsupported osd opcode %s\n",
889 ceph_osd_op_name(src->op));
895 dst->op = cpu_to_le16(src->op);
896 dst->flags = cpu_to_le32(src->flags);
897 dst->payload_len = cpu_to_le32(src->indata_len);
899 return src->indata_len;
903 * build new request AND message, calculate layout, and adjust file
906 * if the file was recently truncated, we include information about its
907 * old and new size so that the object can be updated appropriately. (we
908 * avoid synchronously deleting truncated objects because it's slow.)
910 * if @do_sync, include a 'startsync' command so that the osd will flush
913 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
914 struct ceph_file_layout *layout,
915 struct ceph_vino vino,
917 unsigned int which, int num_ops,
918 int opcode, int flags,
919 struct ceph_snap_context *snapc,
924 struct ceph_osd_request *req;
930 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
931 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
932 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
934 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
941 /* calculate max write size */
942 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
946 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
947 osd_req_op_init(req, which, opcode, 0);
949 u32 object_size = layout->object_size;
950 u32 object_base = off - objoff;
951 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
952 if (truncate_size <= object_base) {
955 truncate_size -= object_base;
956 if (truncate_size > object_size)
957 truncate_size = object_size;
960 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
961 truncate_size, truncate_seq);
964 req->r_flags = flags;
965 req->r_base_oloc.pool = layout->pool_id;
966 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
967 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
969 req->r_snapid = vino.snap;
970 if (flags & CEPH_OSD_FLAG_WRITE)
971 req->r_data_offset = off;
973 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
980 ceph_osdc_put_request(req);
983 EXPORT_SYMBOL(ceph_osdc_new_request);
986 * We keep osd requests in an rbtree, sorted by ->r_tid.
988 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
989 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
991 static bool osd_homeless(struct ceph_osd *osd)
993 return osd->o_osd == CEPH_HOMELESS_OSD;
996 static bool osd_registered(struct ceph_osd *osd)
998 verify_osdc_locked(osd->o_osdc);
1000 return !RB_EMPTY_NODE(&osd->o_node);
1004 * Assumes @osd is zero-initialized.
1006 static void osd_init(struct ceph_osd *osd)
1008 atomic_set(&osd->o_ref, 1);
1009 RB_CLEAR_NODE(&osd->o_node);
1010 osd->o_requests = RB_ROOT;
1011 osd->o_linger_requests = RB_ROOT;
1012 INIT_LIST_HEAD(&osd->o_osd_lru);
1013 INIT_LIST_HEAD(&osd->o_keepalive_item);
1014 osd->o_incarnation = 1;
1015 mutex_init(&osd->lock);
1018 static void osd_cleanup(struct ceph_osd *osd)
1020 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1021 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1022 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1023 WARN_ON(!list_empty(&osd->o_osd_lru));
1024 WARN_ON(!list_empty(&osd->o_keepalive_item));
1026 if (osd->o_auth.authorizer) {
1027 WARN_ON(osd_homeless(osd));
1028 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1033 * Track open sessions with osds.
1035 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1037 struct ceph_osd *osd;
1039 WARN_ON(onum == CEPH_HOMELESS_OSD);
1041 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1046 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1051 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1053 if (atomic_inc_not_zero(&osd->o_ref)) {
1054 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1055 atomic_read(&osd->o_ref));
1058 dout("get_osd %p FAIL\n", osd);
1063 static void put_osd(struct ceph_osd *osd)
1065 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1066 atomic_read(&osd->o_ref) - 1);
1067 if (atomic_dec_and_test(&osd->o_ref)) {
1073 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1075 static void __move_osd_to_lru(struct ceph_osd *osd)
1077 struct ceph_osd_client *osdc = osd->o_osdc;
1079 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1080 BUG_ON(!list_empty(&osd->o_osd_lru));
1082 spin_lock(&osdc->osd_lru_lock);
1083 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1084 spin_unlock(&osdc->osd_lru_lock);
1086 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1089 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1091 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1092 RB_EMPTY_ROOT(&osd->o_linger_requests))
1093 __move_osd_to_lru(osd);
1096 static void __remove_osd_from_lru(struct ceph_osd *osd)
1098 struct ceph_osd_client *osdc = osd->o_osdc;
1100 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1102 spin_lock(&osdc->osd_lru_lock);
1103 if (!list_empty(&osd->o_osd_lru))
1104 list_del_init(&osd->o_osd_lru);
1105 spin_unlock(&osdc->osd_lru_lock);
1109 * Close the connection and assign any leftover requests to the
1112 static void close_osd(struct ceph_osd *osd)
1114 struct ceph_osd_client *osdc = osd->o_osdc;
1117 verify_osdc_wrlocked(osdc);
1118 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1120 ceph_con_close(&osd->o_con);
1122 for (n = rb_first(&osd->o_requests); n; ) {
1123 struct ceph_osd_request *req =
1124 rb_entry(n, struct ceph_osd_request, r_node);
1126 n = rb_next(n); /* unlink_request() */
1128 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1129 unlink_request(osd, req);
1130 link_request(&osdc->homeless_osd, req);
1132 for (n = rb_first(&osd->o_linger_requests); n; ) {
1133 struct ceph_osd_linger_request *lreq =
1134 rb_entry(n, struct ceph_osd_linger_request, node);
1136 n = rb_next(n); /* unlink_linger() */
1138 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1140 unlink_linger(osd, lreq);
1141 link_linger(&osdc->homeless_osd, lreq);
1144 __remove_osd_from_lru(osd);
1145 erase_osd(&osdc->osds, osd);
1152 static int reopen_osd(struct ceph_osd *osd)
1154 struct ceph_entity_addr *peer_addr;
1156 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1158 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1159 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1164 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1165 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1166 !ceph_con_opened(&osd->o_con)) {
1169 dout("osd addr hasn't changed and connection never opened, "
1170 "letting msgr retry\n");
1171 /* touch each r_stamp for handle_timeout()'s benfit */
1172 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1173 struct ceph_osd_request *req =
1174 rb_entry(n, struct ceph_osd_request, r_node);
1175 req->r_stamp = jiffies;
1181 ceph_con_close(&osd->o_con);
1182 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1183 osd->o_incarnation++;
1188 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1191 struct ceph_osd *osd;
1194 verify_osdc_wrlocked(osdc);
1196 verify_osdc_locked(osdc);
1198 if (o != CEPH_HOMELESS_OSD)
1199 osd = lookup_osd(&osdc->osds, o);
1201 osd = &osdc->homeless_osd;
1204 return ERR_PTR(-EAGAIN);
1206 osd = create_osd(osdc, o);
1207 insert_osd(&osdc->osds, osd);
1208 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1209 &osdc->osdmap->osd_addr[osd->o_osd]);
1212 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1217 * Create request <-> OSD session relation.
1219 * @req has to be assigned a tid, @osd may be homeless.
1221 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1223 verify_osd_locked(osd);
1224 WARN_ON(!req->r_tid || req->r_osd);
1225 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1228 if (!osd_homeless(osd))
1229 __remove_osd_from_lru(osd);
1231 atomic_inc(&osd->o_osdc->num_homeless);
1234 insert_request(&osd->o_requests, req);
1238 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1240 verify_osd_locked(osd);
1241 WARN_ON(req->r_osd != osd);
1242 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1246 erase_request(&osd->o_requests, req);
1249 if (!osd_homeless(osd))
1250 maybe_move_osd_to_lru(osd);
1252 atomic_dec(&osd->o_osdc->num_homeless);
1255 static bool __pool_full(struct ceph_pg_pool_info *pi)
1257 return pi->flags & CEPH_POOL_FLAG_FULL;
1260 static bool have_pool_full(struct ceph_osd_client *osdc)
1264 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1265 struct ceph_pg_pool_info *pi =
1266 rb_entry(n, struct ceph_pg_pool_info, node);
1268 if (__pool_full(pi))
1275 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1277 struct ceph_pg_pool_info *pi;
1279 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1283 return __pool_full(pi);
1287 * Returns whether a request should be blocked from being sent
1288 * based on the current osdmap and osd_client settings.
1290 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1291 const struct ceph_osd_request_target *t,
1292 struct ceph_pg_pool_info *pi)
1294 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1295 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1296 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1299 WARN_ON(pi->id != t->base_oloc.pool);
1300 return (t->flags & CEPH_OSD_FLAG_READ && pauserd) ||
1301 (t->flags & CEPH_OSD_FLAG_WRITE && pausewr);
1304 enum calc_target_result {
1305 CALC_TARGET_NO_ACTION = 0,
1306 CALC_TARGET_NEED_RESEND,
1307 CALC_TARGET_POOL_DNE,
1310 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1311 struct ceph_osd_request_target *t,
1312 u32 *last_force_resend,
1315 struct ceph_pg_pool_info *pi;
1316 struct ceph_pg pgid, last_pgid;
1317 struct ceph_osds up, acting;
1318 bool force_resend = false;
1319 bool need_check_tiering = false;
1320 bool need_resend = false;
1321 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1322 enum calc_target_result ct_res;
1325 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1327 t->osd = CEPH_HOMELESS_OSD;
1328 ct_res = CALC_TARGET_POOL_DNE;
1332 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1333 if (last_force_resend &&
1334 *last_force_resend < pi->last_force_request_resend) {
1335 *last_force_resend = pi->last_force_request_resend;
1336 force_resend = true;
1337 } else if (!last_force_resend) {
1338 force_resend = true;
1341 if (ceph_oid_empty(&t->target_oid) || force_resend) {
1342 ceph_oid_copy(&t->target_oid, &t->base_oid);
1343 need_check_tiering = true;
1345 if (ceph_oloc_empty(&t->target_oloc) || force_resend) {
1346 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1347 need_check_tiering = true;
1350 if (need_check_tiering &&
1351 (t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1352 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1353 t->target_oloc.pool = pi->read_tier;
1354 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1355 t->target_oloc.pool = pi->write_tier;
1358 ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1359 &t->target_oloc, &pgid);
1361 WARN_ON(ret != -ENOENT);
1362 t->osd = CEPH_HOMELESS_OSD;
1363 ct_res = CALC_TARGET_POOL_DNE;
1366 last_pgid.pool = pgid.pool;
1367 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1369 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1371 ceph_is_new_interval(&t->acting,
1384 force_resend = true;
1386 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1391 if (ceph_pg_compare(&t->pgid, &pgid) ||
1392 ceph_osds_changed(&t->acting, &acting, any_change) ||
1394 t->pgid = pgid; /* struct */
1395 ceph_osds_copy(&t->acting, &acting);
1396 ceph_osds_copy(&t->up, &up);
1398 t->min_size = pi->min_size;
1399 t->pg_num = pi->pg_num;
1400 t->pg_num_mask = pi->pg_num_mask;
1401 t->sort_bitwise = sort_bitwise;
1403 t->osd = acting.primary;
1407 ct_res = need_resend ? CALC_TARGET_NEED_RESEND : CALC_TARGET_NO_ACTION;
1409 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1413 static void setup_request_data(struct ceph_osd_request *req,
1414 struct ceph_msg *msg)
1419 if (!list_empty(&msg->data))
1422 WARN_ON(msg->data_length);
1423 for (i = 0; i < req->r_num_ops; i++) {
1424 struct ceph_osd_req_op *op = &req->r_ops[i];
1428 case CEPH_OSD_OP_WRITE:
1429 case CEPH_OSD_OP_WRITEFULL:
1430 WARN_ON(op->indata_len != op->extent.length);
1431 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1433 case CEPH_OSD_OP_SETXATTR:
1434 case CEPH_OSD_OP_CMPXATTR:
1435 WARN_ON(op->indata_len != op->xattr.name_len +
1436 op->xattr.value_len);
1437 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1439 case CEPH_OSD_OP_NOTIFY_ACK:
1440 ceph_osdc_msg_data_add(msg,
1441 &op->notify_ack.request_data);
1445 case CEPH_OSD_OP_STAT:
1446 ceph_osdc_msg_data_add(req->r_reply,
1449 case CEPH_OSD_OP_READ:
1450 ceph_osdc_msg_data_add(req->r_reply,
1451 &op->extent.osd_data);
1453 case CEPH_OSD_OP_LIST_WATCHERS:
1454 ceph_osdc_msg_data_add(req->r_reply,
1455 &op->list_watchers.response_data);
1459 case CEPH_OSD_OP_CALL:
1460 WARN_ON(op->indata_len != op->cls.class_len +
1461 op->cls.method_len +
1462 op->cls.indata_len);
1463 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1464 /* optional, can be NONE */
1465 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1466 /* optional, can be NONE */
1467 ceph_osdc_msg_data_add(req->r_reply,
1468 &op->cls.response_data);
1470 case CEPH_OSD_OP_NOTIFY:
1471 ceph_osdc_msg_data_add(msg,
1472 &op->notify.request_data);
1473 ceph_osdc_msg_data_add(req->r_reply,
1474 &op->notify.response_data);
1478 data_len += op->indata_len;
1481 WARN_ON(data_len != msg->data_length);
1484 static void encode_request(struct ceph_osd_request *req, struct ceph_msg *msg)
1486 void *p = msg->front.iov_base;
1487 void *const end = p + msg->front_alloc_len;
1491 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1492 /* snapshots aren't writeable */
1493 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1495 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1496 req->r_data_offset || req->r_snapc);
1499 setup_request_data(req, msg);
1501 ceph_encode_32(&p, 1); /* client_inc, always 1 */
1502 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1503 ceph_encode_32(&p, req->r_flags);
1504 ceph_encode_timespec(p, &req->r_mtime);
1505 p += sizeof(struct ceph_timespec);
1506 /* aka reassert_version */
1507 memcpy(p, &req->r_replay_version, sizeof(req->r_replay_version));
1508 p += sizeof(req->r_replay_version);
1511 ceph_start_encoding(&p, 5, 4,
1512 ceph_oloc_encoding_size(&req->r_t.target_oloc));
1513 ceph_encode_64(&p, req->r_t.target_oloc.pool);
1514 ceph_encode_32(&p, -1); /* preferred */
1515 ceph_encode_32(&p, 0); /* key len */
1516 if (req->r_t.target_oloc.pool_ns)
1517 ceph_encode_string(&p, end, req->r_t.target_oloc.pool_ns->str,
1518 req->r_t.target_oloc.pool_ns->len);
1520 ceph_encode_32(&p, 0);
1523 ceph_encode_8(&p, 1);
1524 ceph_encode_64(&p, req->r_t.pgid.pool);
1525 ceph_encode_32(&p, req->r_t.pgid.seed);
1526 ceph_encode_32(&p, -1); /* preferred */
1529 ceph_encode_32(&p, req->r_t.target_oid.name_len);
1530 memcpy(p, req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1531 p += req->r_t.target_oid.name_len;
1533 /* ops, can imply data */
1534 ceph_encode_16(&p, req->r_num_ops);
1535 for (i = 0; i < req->r_num_ops; i++) {
1536 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1537 p += sizeof(struct ceph_osd_op);
1540 ceph_encode_64(&p, req->r_snapid); /* snapid */
1542 ceph_encode_64(&p, req->r_snapc->seq);
1543 ceph_encode_32(&p, req->r_snapc->num_snaps);
1544 for (i = 0; i < req->r_snapc->num_snaps; i++)
1545 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1547 ceph_encode_64(&p, 0); /* snap_seq */
1548 ceph_encode_32(&p, 0); /* snaps len */
1551 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1554 msg->front.iov_len = p - msg->front.iov_base;
1555 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1556 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1557 msg->hdr.data_len = cpu_to_le32(data_len);
1559 * The header "data_off" is a hint to the receiver allowing it
1560 * to align received data into its buffers such that there's no
1561 * need to re-copy it before writing it to disk (direct I/O).
1563 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1565 dout("%s req %p oid %s oid_len %d front %zu data %u\n", __func__,
1566 req, req->r_t.target_oid.name, req->r_t.target_oid.name_len,
1567 msg->front.iov_len, data_len);
1571 * @req has to be assigned a tid and registered.
1573 static void send_request(struct ceph_osd_request *req)
1575 struct ceph_osd *osd = req->r_osd;
1577 verify_osd_locked(osd);
1578 WARN_ON(osd->o_osd != req->r_t.osd);
1581 * We may have a previously queued request message hanging
1582 * around. Cancel it to avoid corrupting the msgr.
1585 ceph_msg_revoke(req->r_request);
1587 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1588 if (req->r_attempts)
1589 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1591 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1593 encode_request(req, req->r_request);
1595 dout("%s req %p tid %llu to pg %llu.%x osd%d flags 0x%x attempt %d\n",
1596 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
1597 req->r_t.osd, req->r_flags, req->r_attempts);
1599 req->r_t.paused = false;
1600 req->r_stamp = jiffies;
1603 req->r_sent = osd->o_incarnation;
1604 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1605 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
1608 static void maybe_request_map(struct ceph_osd_client *osdc)
1610 bool continuous = false;
1612 verify_osdc_locked(osdc);
1613 WARN_ON(!osdc->osdmap->epoch);
1615 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1616 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
1617 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
1618 dout("%s osdc %p continuous\n", __func__, osdc);
1621 dout("%s osdc %p onetime\n", __func__, osdc);
1624 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
1625 osdc->osdmap->epoch + 1, continuous))
1626 ceph_monc_renew_subs(&osdc->client->monc);
1629 static void send_map_check(struct ceph_osd_request *req);
1631 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
1633 struct ceph_osd_client *osdc = req->r_osdc;
1634 struct ceph_osd *osd;
1635 enum calc_target_result ct_res;
1636 bool need_send = false;
1637 bool promoted = false;
1639 WARN_ON(req->r_tid);
1640 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
1643 ct_res = calc_target(osdc, &req->r_t, &req->r_last_force_resend, false);
1644 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
1647 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
1649 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
1653 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1654 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
1655 dout("req %p pausewr\n", req);
1656 req->r_t.paused = true;
1657 maybe_request_map(osdc);
1658 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1659 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
1660 dout("req %p pauserd\n", req);
1661 req->r_t.paused = true;
1662 maybe_request_map(osdc);
1663 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1664 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
1665 CEPH_OSD_FLAG_FULL_FORCE)) &&
1666 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1667 pool_full(osdc, req->r_t.base_oloc.pool))) {
1668 dout("req %p full/pool_full\n", req);
1669 pr_warn_ratelimited("FULL or reached pool quota\n");
1670 req->r_t.paused = true;
1671 maybe_request_map(osdc);
1672 } else if (!osd_homeless(osd)) {
1675 maybe_request_map(osdc);
1678 mutex_lock(&osd->lock);
1680 * Assign the tid atomically with send_request() to protect
1681 * multiple writes to the same object from racing with each
1682 * other, resulting in out of order ops on the OSDs.
1684 req->r_tid = atomic64_inc_return(&osdc->last_tid);
1685 link_request(osd, req);
1688 mutex_unlock(&osd->lock);
1690 if (ct_res == CALC_TARGET_POOL_DNE)
1691 send_map_check(req);
1694 downgrade_write(&osdc->lock);
1698 up_read(&osdc->lock);
1699 down_write(&osdc->lock);
1705 static void account_request(struct ceph_osd_request *req)
1707 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
1708 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
1710 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
1711 atomic_inc(&req->r_osdc->num_requests);
1714 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
1716 ceph_osdc_get_request(req);
1717 account_request(req);
1718 __submit_request(req, wrlocked);
1721 static void finish_request(struct ceph_osd_request *req)
1723 struct ceph_osd_client *osdc = req->r_osdc;
1724 struct ceph_osd *osd = req->r_osd;
1726 verify_osd_locked(osd);
1727 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1729 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
1730 unlink_request(osd, req);
1731 atomic_dec(&osdc->num_requests);
1734 * If an OSD has failed or returned and a request has been sent
1735 * twice, it's possible to get a reply and end up here while the
1736 * request message is queued for delivery. We will ignore the
1737 * reply, so not a big deal, but better to try and catch it.
1739 ceph_msg_revoke(req->r_request);
1740 ceph_msg_revoke_incoming(req->r_reply);
1743 static void __complete_request(struct ceph_osd_request *req)
1745 if (req->r_callback) {
1746 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
1747 req->r_tid, req->r_callback, req->r_result);
1748 req->r_callback(req);
1753 * This is open-coded in handle_reply().
1755 static void complete_request(struct ceph_osd_request *req, int err)
1757 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1759 req->r_result = err;
1760 finish_request(req);
1761 __complete_request(req);
1762 complete_all(&req->r_completion);
1763 ceph_osdc_put_request(req);
1766 static void cancel_map_check(struct ceph_osd_request *req)
1768 struct ceph_osd_client *osdc = req->r_osdc;
1769 struct ceph_osd_request *lookup_req;
1771 verify_osdc_wrlocked(osdc);
1773 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1777 WARN_ON(lookup_req != req);
1778 erase_request_mc(&osdc->map_checks, req);
1779 ceph_osdc_put_request(req);
1782 static void cancel_request(struct ceph_osd_request *req)
1784 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1786 cancel_map_check(req);
1787 finish_request(req);
1788 complete_all(&req->r_completion);
1789 ceph_osdc_put_request(req);
1792 static void check_pool_dne(struct ceph_osd_request *req)
1794 struct ceph_osd_client *osdc = req->r_osdc;
1795 struct ceph_osdmap *map = osdc->osdmap;
1797 verify_osdc_wrlocked(osdc);
1798 WARN_ON(!map->epoch);
1800 if (req->r_attempts) {
1802 * We sent a request earlier, which means that
1803 * previously the pool existed, and now it does not
1804 * (i.e., it was deleted).
1806 req->r_map_dne_bound = map->epoch;
1807 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
1810 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
1811 req, req->r_tid, req->r_map_dne_bound, map->epoch);
1814 if (req->r_map_dne_bound) {
1815 if (map->epoch >= req->r_map_dne_bound) {
1816 /* we had a new enough map */
1817 pr_info_ratelimited("tid %llu pool does not exist\n",
1819 complete_request(req, -ENOENT);
1822 send_map_check(req);
1826 static void map_check_cb(struct ceph_mon_generic_request *greq)
1828 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
1829 struct ceph_osd_request *req;
1830 u64 tid = greq->private_data;
1832 WARN_ON(greq->result || !greq->u.newest);
1834 down_write(&osdc->lock);
1835 req = lookup_request_mc(&osdc->map_checks, tid);
1837 dout("%s tid %llu dne\n", __func__, tid);
1841 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
1842 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
1843 if (!req->r_map_dne_bound)
1844 req->r_map_dne_bound = greq->u.newest;
1845 erase_request_mc(&osdc->map_checks, req);
1846 check_pool_dne(req);
1848 ceph_osdc_put_request(req);
1850 up_write(&osdc->lock);
1853 static void send_map_check(struct ceph_osd_request *req)
1855 struct ceph_osd_client *osdc = req->r_osdc;
1856 struct ceph_osd_request *lookup_req;
1859 verify_osdc_wrlocked(osdc);
1861 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1863 WARN_ON(lookup_req != req);
1867 ceph_osdc_get_request(req);
1868 insert_request_mc(&osdc->map_checks, req);
1869 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
1870 map_check_cb, req->r_tid);
1875 * lingering requests, watch/notify v2 infrastructure
1877 static void linger_release(struct kref *kref)
1879 struct ceph_osd_linger_request *lreq =
1880 container_of(kref, struct ceph_osd_linger_request, kref);
1882 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
1883 lreq->reg_req, lreq->ping_req);
1884 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
1885 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
1886 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
1887 WARN_ON(!list_empty(&lreq->scan_item));
1888 WARN_ON(!list_empty(&lreq->pending_lworks));
1892 ceph_osdc_put_request(lreq->reg_req);
1894 ceph_osdc_put_request(lreq->ping_req);
1895 target_destroy(&lreq->t);
1899 static void linger_put(struct ceph_osd_linger_request *lreq)
1902 kref_put(&lreq->kref, linger_release);
1905 static struct ceph_osd_linger_request *
1906 linger_get(struct ceph_osd_linger_request *lreq)
1908 kref_get(&lreq->kref);
1912 static struct ceph_osd_linger_request *
1913 linger_alloc(struct ceph_osd_client *osdc)
1915 struct ceph_osd_linger_request *lreq;
1917 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
1921 kref_init(&lreq->kref);
1922 mutex_init(&lreq->lock);
1923 RB_CLEAR_NODE(&lreq->node);
1924 RB_CLEAR_NODE(&lreq->osdc_node);
1925 RB_CLEAR_NODE(&lreq->mc_node);
1926 INIT_LIST_HEAD(&lreq->scan_item);
1927 INIT_LIST_HEAD(&lreq->pending_lworks);
1928 init_completion(&lreq->reg_commit_wait);
1929 init_completion(&lreq->notify_finish_wait);
1932 target_init(&lreq->t);
1934 dout("%s lreq %p\n", __func__, lreq);
1938 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
1939 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
1940 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
1943 * Create linger request <-> OSD session relation.
1945 * @lreq has to be registered, @osd may be homeless.
1947 static void link_linger(struct ceph_osd *osd,
1948 struct ceph_osd_linger_request *lreq)
1950 verify_osd_locked(osd);
1951 WARN_ON(!lreq->linger_id || lreq->osd);
1952 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
1953 osd->o_osd, lreq, lreq->linger_id);
1955 if (!osd_homeless(osd))
1956 __remove_osd_from_lru(osd);
1958 atomic_inc(&osd->o_osdc->num_homeless);
1961 insert_linger(&osd->o_linger_requests, lreq);
1965 static void unlink_linger(struct ceph_osd *osd,
1966 struct ceph_osd_linger_request *lreq)
1968 verify_osd_locked(osd);
1969 WARN_ON(lreq->osd != osd);
1970 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
1971 osd->o_osd, lreq, lreq->linger_id);
1974 erase_linger(&osd->o_linger_requests, lreq);
1977 if (!osd_homeless(osd))
1978 maybe_move_osd_to_lru(osd);
1980 atomic_dec(&osd->o_osdc->num_homeless);
1983 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
1985 verify_osdc_locked(lreq->osdc);
1987 return !RB_EMPTY_NODE(&lreq->osdc_node);
1990 static bool linger_registered(struct ceph_osd_linger_request *lreq)
1992 struct ceph_osd_client *osdc = lreq->osdc;
1995 down_read(&osdc->lock);
1996 registered = __linger_registered(lreq);
1997 up_read(&osdc->lock);
2002 static void linger_register(struct ceph_osd_linger_request *lreq)
2004 struct ceph_osd_client *osdc = lreq->osdc;
2006 verify_osdc_wrlocked(osdc);
2007 WARN_ON(lreq->linger_id);
2010 lreq->linger_id = ++osdc->last_linger_id;
2011 insert_linger_osdc(&osdc->linger_requests, lreq);
2014 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2016 struct ceph_osd_client *osdc = lreq->osdc;
2018 verify_osdc_wrlocked(osdc);
2020 erase_linger_osdc(&osdc->linger_requests, lreq);
2024 static void cancel_linger_request(struct ceph_osd_request *req)
2026 struct ceph_osd_linger_request *lreq = req->r_priv;
2028 WARN_ON(!req->r_linger);
2029 cancel_request(req);
2033 struct linger_work {
2034 struct work_struct work;
2035 struct ceph_osd_linger_request *lreq;
2036 struct list_head pending_item;
2037 unsigned long queued_stamp;
2043 void *payload; /* points into @msg front */
2046 struct ceph_msg *msg; /* for ceph_msg_put() */
2054 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2057 struct linger_work *lwork;
2059 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2063 INIT_WORK(&lwork->work, workfn);
2064 INIT_LIST_HEAD(&lwork->pending_item);
2065 lwork->lreq = linger_get(lreq);
2070 static void lwork_free(struct linger_work *lwork)
2072 struct ceph_osd_linger_request *lreq = lwork->lreq;
2074 mutex_lock(&lreq->lock);
2075 list_del(&lwork->pending_item);
2076 mutex_unlock(&lreq->lock);
2082 static void lwork_queue(struct linger_work *lwork)
2084 struct ceph_osd_linger_request *lreq = lwork->lreq;
2085 struct ceph_osd_client *osdc = lreq->osdc;
2087 verify_lreq_locked(lreq);
2088 WARN_ON(!list_empty(&lwork->pending_item));
2090 lwork->queued_stamp = jiffies;
2091 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2092 queue_work(osdc->notify_wq, &lwork->work);
2095 static void do_watch_notify(struct work_struct *w)
2097 struct linger_work *lwork = container_of(w, struct linger_work, work);
2098 struct ceph_osd_linger_request *lreq = lwork->lreq;
2100 if (!linger_registered(lreq)) {
2101 dout("%s lreq %p not registered\n", __func__, lreq);
2105 WARN_ON(!lreq->is_watch);
2106 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2107 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2108 lwork->notify.payload_len);
2109 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2110 lwork->notify.notifier_id, lwork->notify.payload,
2111 lwork->notify.payload_len);
2114 ceph_msg_put(lwork->notify.msg);
2118 static void do_watch_error(struct work_struct *w)
2120 struct linger_work *lwork = container_of(w, struct linger_work, work);
2121 struct ceph_osd_linger_request *lreq = lwork->lreq;
2123 if (!linger_registered(lreq)) {
2124 dout("%s lreq %p not registered\n", __func__, lreq);
2128 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2129 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2135 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2137 struct linger_work *lwork;
2139 lwork = lwork_alloc(lreq, do_watch_error);
2141 pr_err("failed to allocate error-lwork\n");
2145 lwork->error.err = lreq->last_error;
2149 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2152 if (!completion_done(&lreq->reg_commit_wait)) {
2153 lreq->reg_commit_error = (result <= 0 ? result : 0);
2154 complete_all(&lreq->reg_commit_wait);
2158 static void linger_commit_cb(struct ceph_osd_request *req)
2160 struct ceph_osd_linger_request *lreq = req->r_priv;
2162 mutex_lock(&lreq->lock);
2163 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2164 lreq->linger_id, req->r_result);
2165 linger_reg_commit_complete(lreq, req->r_result);
2166 lreq->committed = true;
2168 if (!lreq->is_watch) {
2169 struct ceph_osd_data *osd_data =
2170 osd_req_op_data(req, 0, notify, response_data);
2171 void *p = page_address(osd_data->pages[0]);
2173 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2174 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2176 /* make note of the notify_id */
2177 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2178 lreq->notify_id = ceph_decode_64(&p);
2179 dout("lreq %p notify_id %llu\n", lreq,
2182 dout("lreq %p no notify_id\n", lreq);
2186 mutex_unlock(&lreq->lock);
2190 static int normalize_watch_error(int err)
2193 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2194 * notification and a failure to reconnect because we raced with
2195 * the delete appear the same to the user.
2203 static void linger_reconnect_cb(struct ceph_osd_request *req)
2205 struct ceph_osd_linger_request *lreq = req->r_priv;
2207 mutex_lock(&lreq->lock);
2208 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2209 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2210 if (req->r_result < 0) {
2211 if (!lreq->last_error) {
2212 lreq->last_error = normalize_watch_error(req->r_result);
2213 queue_watch_error(lreq);
2217 mutex_unlock(&lreq->lock);
2221 static void send_linger(struct ceph_osd_linger_request *lreq)
2223 struct ceph_osd_request *req = lreq->reg_req;
2224 struct ceph_osd_req_op *op = &req->r_ops[0];
2226 verify_osdc_wrlocked(req->r_osdc);
2227 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2230 cancel_linger_request(req);
2232 request_reinit(req);
2233 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2234 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2235 req->r_flags = lreq->t.flags;
2236 req->r_mtime = lreq->mtime;
2238 mutex_lock(&lreq->lock);
2239 if (lreq->is_watch && lreq->committed) {
2240 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2241 op->watch.cookie != lreq->linger_id);
2242 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2243 op->watch.gen = ++lreq->register_gen;
2244 dout("lreq %p reconnect register_gen %u\n", lreq,
2246 req->r_callback = linger_reconnect_cb;
2248 if (!lreq->is_watch)
2249 lreq->notify_id = 0;
2251 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2252 dout("lreq %p register\n", lreq);
2253 req->r_callback = linger_commit_cb;
2255 mutex_unlock(&lreq->lock);
2257 req->r_priv = linger_get(lreq);
2258 req->r_linger = true;
2260 submit_request(req, true);
2263 static void linger_ping_cb(struct ceph_osd_request *req)
2265 struct ceph_osd_linger_request *lreq = req->r_priv;
2267 mutex_lock(&lreq->lock);
2268 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2269 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2271 if (lreq->register_gen == req->r_ops[0].watch.gen) {
2272 if (!req->r_result) {
2273 lreq->watch_valid_thru = lreq->ping_sent;
2274 } else if (!lreq->last_error) {
2275 lreq->last_error = normalize_watch_error(req->r_result);
2276 queue_watch_error(lreq);
2279 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2280 lreq->register_gen, req->r_ops[0].watch.gen);
2283 mutex_unlock(&lreq->lock);
2287 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2289 struct ceph_osd_client *osdc = lreq->osdc;
2290 struct ceph_osd_request *req = lreq->ping_req;
2291 struct ceph_osd_req_op *op = &req->r_ops[0];
2293 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2294 dout("%s PAUSERD\n", __func__);
2298 lreq->ping_sent = jiffies;
2299 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2300 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2301 lreq->register_gen);
2304 cancel_linger_request(req);
2306 request_reinit(req);
2307 target_copy(&req->r_t, &lreq->t);
2309 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2310 op->watch.cookie != lreq->linger_id ||
2311 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2312 op->watch.gen = lreq->register_gen;
2313 req->r_callback = linger_ping_cb;
2314 req->r_priv = linger_get(lreq);
2315 req->r_linger = true;
2317 ceph_osdc_get_request(req);
2318 account_request(req);
2319 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2320 link_request(lreq->osd, req);
2324 static void linger_submit(struct ceph_osd_linger_request *lreq)
2326 struct ceph_osd_client *osdc = lreq->osdc;
2327 struct ceph_osd *osd;
2329 calc_target(osdc, &lreq->t, &lreq->last_force_resend, false);
2330 osd = lookup_create_osd(osdc, lreq->t.osd, true);
2331 link_linger(osd, lreq);
2336 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2338 struct ceph_osd_client *osdc = lreq->osdc;
2339 struct ceph_osd_linger_request *lookup_lreq;
2341 verify_osdc_wrlocked(osdc);
2343 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2348 WARN_ON(lookup_lreq != lreq);
2349 erase_linger_mc(&osdc->linger_map_checks, lreq);
2354 * @lreq has to be both registered and linked.
2356 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2358 if (lreq->is_watch && lreq->ping_req->r_osd)
2359 cancel_linger_request(lreq->ping_req);
2360 if (lreq->reg_req->r_osd)
2361 cancel_linger_request(lreq->reg_req);
2362 cancel_linger_map_check(lreq);
2363 unlink_linger(lreq->osd, lreq);
2364 linger_unregister(lreq);
2367 static void linger_cancel(struct ceph_osd_linger_request *lreq)
2369 struct ceph_osd_client *osdc = lreq->osdc;
2371 down_write(&osdc->lock);
2372 if (__linger_registered(lreq))
2373 __linger_cancel(lreq);
2374 up_write(&osdc->lock);
2377 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2379 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2381 struct ceph_osd_client *osdc = lreq->osdc;
2382 struct ceph_osdmap *map = osdc->osdmap;
2384 verify_osdc_wrlocked(osdc);
2385 WARN_ON(!map->epoch);
2387 if (lreq->register_gen) {
2388 lreq->map_dne_bound = map->epoch;
2389 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2390 lreq, lreq->linger_id);
2392 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2393 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2397 if (lreq->map_dne_bound) {
2398 if (map->epoch >= lreq->map_dne_bound) {
2399 /* we had a new enough map */
2400 pr_info("linger_id %llu pool does not exist\n",
2402 linger_reg_commit_complete(lreq, -ENOENT);
2403 __linger_cancel(lreq);
2406 send_linger_map_check(lreq);
2410 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2412 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2413 struct ceph_osd_linger_request *lreq;
2414 u64 linger_id = greq->private_data;
2416 WARN_ON(greq->result || !greq->u.newest);
2418 down_write(&osdc->lock);
2419 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2421 dout("%s linger_id %llu dne\n", __func__, linger_id);
2425 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
2426 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2428 if (!lreq->map_dne_bound)
2429 lreq->map_dne_bound = greq->u.newest;
2430 erase_linger_mc(&osdc->linger_map_checks, lreq);
2431 check_linger_pool_dne(lreq);
2435 up_write(&osdc->lock);
2438 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
2440 struct ceph_osd_client *osdc = lreq->osdc;
2441 struct ceph_osd_linger_request *lookup_lreq;
2444 verify_osdc_wrlocked(osdc);
2446 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2449 WARN_ON(lookup_lreq != lreq);
2454 insert_linger_mc(&osdc->linger_map_checks, lreq);
2455 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2456 linger_map_check_cb, lreq->linger_id);
2460 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
2464 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2465 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
2466 return ret ?: lreq->reg_commit_error;
2469 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
2473 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2474 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
2475 return ret ?: lreq->notify_finish_error;
2479 * Timeout callback, called every N seconds. When 1 or more OSD
2480 * requests has been active for more than N seconds, we send a keepalive
2481 * (tag + timestamp) to its OSD to ensure any communications channel
2482 * reset is detected.
2484 static void handle_timeout(struct work_struct *work)
2486 struct ceph_osd_client *osdc =
2487 container_of(work, struct ceph_osd_client, timeout_work.work);
2488 struct ceph_options *opts = osdc->client->options;
2489 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
2490 LIST_HEAD(slow_osds);
2491 struct rb_node *n, *p;
2493 dout("%s osdc %p\n", __func__, osdc);
2494 down_write(&osdc->lock);
2497 * ping osds that are a bit slow. this ensures that if there
2498 * is a break in the TCP connection we will notice, and reopen
2499 * a connection with that osd (from the fault callback).
2501 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2502 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2505 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
2506 struct ceph_osd_request *req =
2507 rb_entry(p, struct ceph_osd_request, r_node);
2509 if (time_before(req->r_stamp, cutoff)) {
2510 dout(" req %p tid %llu on osd%d is laggy\n",
2511 req, req->r_tid, osd->o_osd);
2515 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
2516 struct ceph_osd_linger_request *lreq =
2517 rb_entry(p, struct ceph_osd_linger_request, node);
2519 dout(" lreq %p linger_id %llu is served by osd%d\n",
2520 lreq, lreq->linger_id, osd->o_osd);
2523 mutex_lock(&lreq->lock);
2524 if (lreq->is_watch && lreq->committed && !lreq->last_error)
2525 send_linger_ping(lreq);
2526 mutex_unlock(&lreq->lock);
2530 list_move_tail(&osd->o_keepalive_item, &slow_osds);
2533 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
2534 maybe_request_map(osdc);
2536 while (!list_empty(&slow_osds)) {
2537 struct ceph_osd *osd = list_first_entry(&slow_osds,
2540 list_del_init(&osd->o_keepalive_item);
2541 ceph_con_keepalive(&osd->o_con);
2544 up_write(&osdc->lock);
2545 schedule_delayed_work(&osdc->timeout_work,
2546 osdc->client->options->osd_keepalive_timeout);
2549 static void handle_osds_timeout(struct work_struct *work)
2551 struct ceph_osd_client *osdc =
2552 container_of(work, struct ceph_osd_client,
2553 osds_timeout_work.work);
2554 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
2555 struct ceph_osd *osd, *nosd;
2557 dout("%s osdc %p\n", __func__, osdc);
2558 down_write(&osdc->lock);
2559 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
2560 if (time_before(jiffies, osd->lru_ttl))
2563 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
2564 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
2568 up_write(&osdc->lock);
2569 schedule_delayed_work(&osdc->osds_timeout_work,
2570 round_jiffies_relative(delay));
2573 static int ceph_oloc_decode(void **p, void *end,
2574 struct ceph_object_locator *oloc)
2576 u8 struct_v, struct_cv;
2581 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2582 struct_v = ceph_decode_8(p);
2583 struct_cv = ceph_decode_8(p);
2585 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
2586 struct_v, struct_cv);
2589 if (struct_cv > 6) {
2590 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
2591 struct_v, struct_cv);
2594 len = ceph_decode_32(p);
2595 ceph_decode_need(p, end, len, e_inval);
2596 struct_end = *p + len;
2598 oloc->pool = ceph_decode_64(p);
2599 *p += 4; /* skip preferred */
2601 len = ceph_decode_32(p);
2603 pr_warn("ceph_object_locator::key is set\n");
2607 if (struct_v >= 5) {
2608 bool changed = false;
2610 len = ceph_decode_32(p);
2612 ceph_decode_need(p, end, len, e_inval);
2613 if (!oloc->pool_ns ||
2614 ceph_compare_string(oloc->pool_ns, *p, len))
2622 /* redirect changes namespace */
2623 pr_warn("ceph_object_locator::nspace is changed\n");
2628 if (struct_v >= 6) {
2629 s64 hash = ceph_decode_64(p);
2631 pr_warn("ceph_object_locator::hash is set\n");
2646 static int ceph_redirect_decode(void **p, void *end,
2647 struct ceph_request_redirect *redir)
2649 u8 struct_v, struct_cv;
2654 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2655 struct_v = ceph_decode_8(p);
2656 struct_cv = ceph_decode_8(p);
2657 if (struct_cv > 1) {
2658 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
2659 struct_v, struct_cv);
2662 len = ceph_decode_32(p);
2663 ceph_decode_need(p, end, len, e_inval);
2664 struct_end = *p + len;
2666 ret = ceph_oloc_decode(p, end, &redir->oloc);
2670 len = ceph_decode_32(p);
2672 pr_warn("ceph_request_redirect::object_name is set\n");
2676 len = ceph_decode_32(p);
2677 *p += len; /* skip osd_instructions */
2689 struct MOSDOpReply {
2690 struct ceph_pg pgid;
2695 u32 outdata_len[CEPH_OSD_MAX_OPS];
2696 s32 rval[CEPH_OSD_MAX_OPS];
2698 struct ceph_eversion replay_version;
2700 struct ceph_request_redirect redirect;
2703 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
2705 void *p = msg->front.iov_base;
2706 void *const end = p + msg->front.iov_len;
2707 u16 version = le16_to_cpu(msg->hdr.version);
2708 struct ceph_eversion bad_replay_version;
2714 ceph_decode_32_safe(&p, end, len, e_inval);
2715 ceph_decode_need(&p, end, len, e_inval);
2716 p += len; /* skip oid */
2718 ret = ceph_decode_pgid(&p, end, &m->pgid);
2722 ceph_decode_64_safe(&p, end, m->flags, e_inval);
2723 ceph_decode_32_safe(&p, end, m->result, e_inval);
2724 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
2725 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
2726 p += sizeof(bad_replay_version);
2727 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
2729 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
2730 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
2733 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
2735 for (i = 0; i < m->num_ops; i++) {
2736 struct ceph_osd_op *op = p;
2738 m->outdata_len[i] = le32_to_cpu(op->payload_len);
2742 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
2743 for (i = 0; i < m->num_ops; i++)
2744 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
2747 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
2748 memcpy(&m->replay_version, p, sizeof(m->replay_version));
2749 p += sizeof(m->replay_version);
2750 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
2752 m->replay_version = bad_replay_version; /* struct */
2753 m->user_version = le64_to_cpu(m->replay_version.version);
2758 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
2766 ret = ceph_redirect_decode(&p, end, &m->redirect);
2770 ceph_oloc_init(&m->redirect.oloc);
2780 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
2783 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
2785 struct ceph_osd_client *osdc = osd->o_osdc;
2786 struct ceph_osd_request *req;
2787 struct MOSDOpReply m;
2788 u64 tid = le64_to_cpu(msg->hdr.tid);
2793 dout("%s msg %p tid %llu\n", __func__, msg, tid);
2795 down_read(&osdc->lock);
2796 if (!osd_registered(osd)) {
2797 dout("%s osd%d unknown\n", __func__, osd->o_osd);
2798 goto out_unlock_osdc;
2800 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
2802 mutex_lock(&osd->lock);
2803 req = lookup_request(&osd->o_requests, tid);
2805 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
2806 goto out_unlock_session;
2809 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
2810 ret = decode_MOSDOpReply(msg, &m);
2811 m.redirect.oloc.pool_ns = NULL;
2813 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
2818 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
2819 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
2820 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
2821 le64_to_cpu(m.replay_version.version), m.user_version);
2823 if (m.retry_attempt >= 0) {
2824 if (m.retry_attempt != req->r_attempts - 1) {
2825 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
2826 req, req->r_tid, m.retry_attempt,
2827 req->r_attempts - 1);
2828 goto out_unlock_session;
2831 WARN_ON(1); /* MOSDOpReply v4 is assumed */
2834 if (!ceph_oloc_empty(&m.redirect.oloc)) {
2835 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
2836 m.redirect.oloc.pool);
2837 unlink_request(osd, req);
2838 mutex_unlock(&osd->lock);
2841 * Not ceph_oloc_copy() - changing pool_ns is not
2844 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
2845 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
2847 __submit_request(req, false);
2848 goto out_unlock_osdc;
2851 if (m.num_ops != req->r_num_ops) {
2852 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
2853 req->r_num_ops, req->r_tid);
2856 for (i = 0; i < req->r_num_ops; i++) {
2857 dout(" req %p tid %llu op %d rval %d len %u\n", req,
2858 req->r_tid, i, m.rval[i], m.outdata_len[i]);
2859 req->r_ops[i].rval = m.rval[i];
2860 req->r_ops[i].outdata_len = m.outdata_len[i];
2861 data_len += m.outdata_len[i];
2863 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
2864 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
2865 le32_to_cpu(msg->hdr.data_len), req->r_tid);
2868 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
2869 req, req->r_tid, m.result, data_len);
2872 * Since we only ever request ONDISK, we should only ever get
2873 * one (type of) reply back.
2875 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
2876 req->r_result = m.result ?: data_len;
2877 finish_request(req);
2878 mutex_unlock(&osd->lock);
2879 up_read(&osdc->lock);
2881 __complete_request(req);
2882 complete_all(&req->r_completion);
2883 ceph_osdc_put_request(req);
2887 complete_request(req, -EIO);
2889 mutex_unlock(&osd->lock);
2891 up_read(&osdc->lock);
2894 static void set_pool_was_full(struct ceph_osd_client *osdc)
2898 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
2899 struct ceph_pg_pool_info *pi =
2900 rb_entry(n, struct ceph_pg_pool_info, node);
2902 pi->was_full = __pool_full(pi);
2906 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
2908 struct ceph_pg_pool_info *pi;
2910 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
2914 return pi->was_full && !__pool_full(pi);
2917 static enum calc_target_result
2918 recalc_linger_target(struct ceph_osd_linger_request *lreq)
2920 struct ceph_osd_client *osdc = lreq->osdc;
2921 enum calc_target_result ct_res;
2923 ct_res = calc_target(osdc, &lreq->t, &lreq->last_force_resend, true);
2924 if (ct_res == CALC_TARGET_NEED_RESEND) {
2925 struct ceph_osd *osd;
2927 osd = lookup_create_osd(osdc, lreq->t.osd, true);
2928 if (osd != lreq->osd) {
2929 unlink_linger(lreq->osd, lreq);
2930 link_linger(osd, lreq);
2938 * Requeue requests whose mapping to an OSD has changed.
2940 static void scan_requests(struct ceph_osd *osd,
2943 bool check_pool_cleared_full,
2944 struct rb_root *need_resend,
2945 struct list_head *need_resend_linger)
2947 struct ceph_osd_client *osdc = osd->o_osdc;
2949 bool force_resend_writes;
2951 for (n = rb_first(&osd->o_linger_requests); n; ) {
2952 struct ceph_osd_linger_request *lreq =
2953 rb_entry(n, struct ceph_osd_linger_request, node);
2954 enum calc_target_result ct_res;
2956 n = rb_next(n); /* recalc_linger_target() */
2958 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
2960 ct_res = recalc_linger_target(lreq);
2962 case CALC_TARGET_NO_ACTION:
2963 force_resend_writes = cleared_full ||
2964 (check_pool_cleared_full &&
2965 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
2966 if (!force_resend && !force_resend_writes)
2970 case CALC_TARGET_NEED_RESEND:
2971 cancel_linger_map_check(lreq);
2973 * scan_requests() for the previous epoch(s)
2974 * may have already added it to the list, since
2975 * it's not unlinked here.
2977 if (list_empty(&lreq->scan_item))
2978 list_add_tail(&lreq->scan_item, need_resend_linger);
2980 case CALC_TARGET_POOL_DNE:
2981 check_linger_pool_dne(lreq);
2986 for (n = rb_first(&osd->o_requests); n; ) {
2987 struct ceph_osd_request *req =
2988 rb_entry(n, struct ceph_osd_request, r_node);
2989 enum calc_target_result ct_res;
2991 n = rb_next(n); /* unlink_request(), check_pool_dne() */
2993 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2994 ct_res = calc_target(osdc, &req->r_t,
2995 &req->r_last_force_resend, false);
2997 case CALC_TARGET_NO_ACTION:
2998 force_resend_writes = cleared_full ||
2999 (check_pool_cleared_full &&
3000 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3001 if (!force_resend &&
3002 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3003 !force_resend_writes))
3007 case CALC_TARGET_NEED_RESEND:
3008 cancel_map_check(req);
3009 unlink_request(osd, req);
3010 insert_request(need_resend, req);
3012 case CALC_TARGET_POOL_DNE:
3013 check_pool_dne(req);
3019 static int handle_one_map(struct ceph_osd_client *osdc,
3020 void *p, void *end, bool incremental,
3021 struct rb_root *need_resend,
3022 struct list_head *need_resend_linger)
3024 struct ceph_osdmap *newmap;
3026 bool skipped_map = false;
3029 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3030 set_pool_was_full(osdc);
3033 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3035 newmap = ceph_osdmap_decode(&p, end);
3037 return PTR_ERR(newmap);
3039 if (newmap != osdc->osdmap) {
3041 * Preserve ->was_full before destroying the old map.
3042 * For pools that weren't in the old map, ->was_full
3045 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3046 struct ceph_pg_pool_info *pi =
3047 rb_entry(n, struct ceph_pg_pool_info, node);
3048 struct ceph_pg_pool_info *old_pi;
3050 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3052 pi->was_full = old_pi->was_full;
3054 WARN_ON(pi->was_full);
3057 if (osdc->osdmap->epoch &&
3058 osdc->osdmap->epoch + 1 < newmap->epoch) {
3059 WARN_ON(incremental);
3063 ceph_osdmap_destroy(osdc->osdmap);
3064 osdc->osdmap = newmap;
3067 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3068 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3069 need_resend, need_resend_linger);
3071 for (n = rb_first(&osdc->osds); n; ) {
3072 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3074 n = rb_next(n); /* close_osd() */
3076 scan_requests(osd, skipped_map, was_full, true, need_resend,
3077 need_resend_linger);
3078 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3079 memcmp(&osd->o_con.peer_addr,
3080 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3081 sizeof(struct ceph_entity_addr)))
3088 static void kick_requests(struct ceph_osd_client *osdc,
3089 struct rb_root *need_resend,
3090 struct list_head *need_resend_linger)
3092 struct ceph_osd_linger_request *lreq, *nlreq;
3095 for (n = rb_first(need_resend); n; ) {
3096 struct ceph_osd_request *req =
3097 rb_entry(n, struct ceph_osd_request, r_node);
3098 struct ceph_osd *osd;
3101 erase_request(need_resend, req); /* before link_request() */
3103 WARN_ON(req->r_osd);
3104 calc_target(osdc, &req->r_t, NULL, false);
3105 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3106 link_request(osd, req);
3107 if (!req->r_linger) {
3108 if (!osd_homeless(osd) && !req->r_t.paused)
3111 cancel_linger_request(req);
3115 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3116 if (!osd_homeless(lreq->osd))
3119 list_del_init(&lreq->scan_item);
3124 * Process updated osd map.
3126 * The message contains any number of incremental and full maps, normally
3127 * indicating some sort of topology change in the cluster. Kick requests
3128 * off to different OSDs as needed.
3130 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3132 void *p = msg->front.iov_base;
3133 void *const end = p + msg->front.iov_len;
3134 u32 nr_maps, maplen;
3136 struct ceph_fsid fsid;
3137 struct rb_root need_resend = RB_ROOT;
3138 LIST_HEAD(need_resend_linger);
3139 bool handled_incremental = false;
3140 bool was_pauserd, was_pausewr;
3141 bool pauserd, pausewr;
3144 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
3145 down_write(&osdc->lock);
3148 ceph_decode_need(&p, end, sizeof(fsid), bad);
3149 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3150 if (ceph_check_fsid(osdc->client, &fsid) < 0)
3153 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3154 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3155 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3156 have_pool_full(osdc);
3158 /* incremental maps */
3159 ceph_decode_32_safe(&p, end, nr_maps, bad);
3160 dout(" %d inc maps\n", nr_maps);
3161 while (nr_maps > 0) {
3162 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3163 epoch = ceph_decode_32(&p);
3164 maplen = ceph_decode_32(&p);
3165 ceph_decode_need(&p, end, maplen, bad);
3166 if (osdc->osdmap->epoch &&
3167 osdc->osdmap->epoch + 1 == epoch) {
3168 dout("applying incremental map %u len %d\n",
3170 err = handle_one_map(osdc, p, p + maplen, true,
3171 &need_resend, &need_resend_linger);
3174 handled_incremental = true;
3176 dout("ignoring incremental map %u len %d\n",
3182 if (handled_incremental)
3186 ceph_decode_32_safe(&p, end, nr_maps, bad);
3187 dout(" %d full maps\n", nr_maps);
3189 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3190 epoch = ceph_decode_32(&p);
3191 maplen = ceph_decode_32(&p);
3192 ceph_decode_need(&p, end, maplen, bad);
3194 dout("skipping non-latest full map %u len %d\n",
3196 } else if (osdc->osdmap->epoch >= epoch) {
3197 dout("skipping full map %u len %d, "
3198 "older than our %u\n", epoch, maplen,
3199 osdc->osdmap->epoch);
3201 dout("taking full map %u len %d\n", epoch, maplen);
3202 err = handle_one_map(osdc, p, p + maplen, false,
3203 &need_resend, &need_resend_linger);
3213 * subscribe to subsequent osdmap updates if full to ensure
3214 * we find out when we are no longer full and stop returning
3217 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3218 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3219 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3220 have_pool_full(osdc);
3221 if (was_pauserd || was_pausewr || pauserd || pausewr)
3222 maybe_request_map(osdc);
3224 kick_requests(osdc, &need_resend, &need_resend_linger);
3226 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3227 osdc->osdmap->epoch);
3228 up_write(&osdc->lock);
3229 wake_up_all(&osdc->client->auth_wq);
3233 pr_err("osdc handle_map corrupt msg\n");
3235 up_write(&osdc->lock);
3239 * Resubmit requests pending on the given osd.
3241 static void kick_osd_requests(struct ceph_osd *osd)
3245 for (n = rb_first(&osd->o_requests); n; ) {
3246 struct ceph_osd_request *req =
3247 rb_entry(n, struct ceph_osd_request, r_node);
3249 n = rb_next(n); /* cancel_linger_request() */
3251 if (!req->r_linger) {
3252 if (!req->r_t.paused)
3255 cancel_linger_request(req);
3258 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3259 struct ceph_osd_linger_request *lreq =
3260 rb_entry(n, struct ceph_osd_linger_request, node);
3267 * If the osd connection drops, we need to resubmit all requests.
3269 static void osd_fault(struct ceph_connection *con)
3271 struct ceph_osd *osd = con->private;
3272 struct ceph_osd_client *osdc = osd->o_osdc;
3274 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3276 down_write(&osdc->lock);
3277 if (!osd_registered(osd)) {
3278 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3282 if (!reopen_osd(osd))
3283 kick_osd_requests(osd);
3284 maybe_request_map(osdc);
3287 up_write(&osdc->lock);
3291 * Process osd watch notifications
3293 static void handle_watch_notify(struct ceph_osd_client *osdc,
3294 struct ceph_msg *msg)
3296 void *p = msg->front.iov_base;
3297 void *const end = p + msg->front.iov_len;
3298 struct ceph_osd_linger_request *lreq;
3299 struct linger_work *lwork;
3300 u8 proto_ver, opcode;
3301 u64 cookie, notify_id;
3302 u64 notifier_id = 0;
3303 s32 return_code = 0;
3304 void *payload = NULL;
3305 u32 payload_len = 0;
3307 ceph_decode_8_safe(&p, end, proto_ver, bad);
3308 ceph_decode_8_safe(&p, end, opcode, bad);
3309 ceph_decode_64_safe(&p, end, cookie, bad);
3310 p += 8; /* skip ver */
3311 ceph_decode_64_safe(&p, end, notify_id, bad);
3313 if (proto_ver >= 1) {
3314 ceph_decode_32_safe(&p, end, payload_len, bad);
3315 ceph_decode_need(&p, end, payload_len, bad);
3320 if (le16_to_cpu(msg->hdr.version) >= 2)
3321 ceph_decode_32_safe(&p, end, return_code, bad);
3323 if (le16_to_cpu(msg->hdr.version) >= 3)
3324 ceph_decode_64_safe(&p, end, notifier_id, bad);
3326 down_read(&osdc->lock);
3327 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
3329 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
3331 goto out_unlock_osdc;
3334 mutex_lock(&lreq->lock);
3335 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
3336 opcode, cookie, lreq, lreq->is_watch);
3337 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
3338 if (!lreq->last_error) {
3339 lreq->last_error = -ENOTCONN;
3340 queue_watch_error(lreq);
3342 } else if (!lreq->is_watch) {
3343 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
3344 if (lreq->notify_id && lreq->notify_id != notify_id) {
3345 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
3346 lreq->notify_id, notify_id);
3347 } else if (!completion_done(&lreq->notify_finish_wait)) {
3348 struct ceph_msg_data *data =
3349 list_first_entry_or_null(&msg->data,
3350 struct ceph_msg_data,
3354 if (lreq->preply_pages) {
3355 WARN_ON(data->type !=
3356 CEPH_MSG_DATA_PAGES);
3357 *lreq->preply_pages = data->pages;
3358 *lreq->preply_len = data->length;
3360 ceph_release_page_vector(data->pages,
3361 calc_pages_for(0, data->length));
3364 lreq->notify_finish_error = return_code;
3365 complete_all(&lreq->notify_finish_wait);
3368 /* CEPH_WATCH_EVENT_NOTIFY */
3369 lwork = lwork_alloc(lreq, do_watch_notify);
3371 pr_err("failed to allocate notify-lwork\n");
3372 goto out_unlock_lreq;
3375 lwork->notify.notify_id = notify_id;
3376 lwork->notify.notifier_id = notifier_id;
3377 lwork->notify.payload = payload;
3378 lwork->notify.payload_len = payload_len;
3379 lwork->notify.msg = ceph_msg_get(msg);
3384 mutex_unlock(&lreq->lock);
3386 up_read(&osdc->lock);
3390 pr_err("osdc handle_watch_notify corrupt msg\n");
3394 * Register request, send initial attempt.
3396 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
3397 struct ceph_osd_request *req,
3400 down_read(&osdc->lock);
3401 submit_request(req, false);
3402 up_read(&osdc->lock);
3406 EXPORT_SYMBOL(ceph_osdc_start_request);
3409 * Unregister a registered request. The request is not completed:
3410 * ->r_result isn't set and __complete_request() isn't called.
3412 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
3414 struct ceph_osd_client *osdc = req->r_osdc;
3416 down_write(&osdc->lock);
3418 cancel_request(req);
3419 up_write(&osdc->lock);
3421 EXPORT_SYMBOL(ceph_osdc_cancel_request);
3424 * @timeout: in jiffies, 0 means "wait forever"
3426 static int wait_request_timeout(struct ceph_osd_request *req,
3427 unsigned long timeout)
3431 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3432 left = wait_for_completion_killable_timeout(&req->r_completion,
3433 ceph_timeout_jiffies(timeout));
3435 left = left ?: -ETIMEDOUT;
3436 ceph_osdc_cancel_request(req);
3438 left = req->r_result; /* completed */
3445 * wait for a request to complete
3447 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
3448 struct ceph_osd_request *req)
3450 return wait_request_timeout(req, 0);
3452 EXPORT_SYMBOL(ceph_osdc_wait_request);
3455 * sync - wait for all in-flight requests to flush. avoid starvation.
3457 void ceph_osdc_sync(struct ceph_osd_client *osdc)
3459 struct rb_node *n, *p;
3460 u64 last_tid = atomic64_read(&osdc->last_tid);
3463 down_read(&osdc->lock);
3464 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3465 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3467 mutex_lock(&osd->lock);
3468 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
3469 struct ceph_osd_request *req =
3470 rb_entry(p, struct ceph_osd_request, r_node);
3472 if (req->r_tid > last_tid)
3475 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
3478 ceph_osdc_get_request(req);
3479 mutex_unlock(&osd->lock);
3480 up_read(&osdc->lock);
3481 dout("%s waiting on req %p tid %llu last_tid %llu\n",
3482 __func__, req, req->r_tid, last_tid);
3483 wait_for_completion(&req->r_completion);
3484 ceph_osdc_put_request(req);
3488 mutex_unlock(&osd->lock);
3491 up_read(&osdc->lock);
3492 dout("%s done last_tid %llu\n", __func__, last_tid);
3494 EXPORT_SYMBOL(ceph_osdc_sync);
3496 static struct ceph_osd_request *
3497 alloc_linger_request(struct ceph_osd_linger_request *lreq)
3499 struct ceph_osd_request *req;
3501 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
3505 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3506 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3508 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
3509 ceph_osdc_put_request(req);
3517 * Returns a handle, caller owns a ref.
3519 struct ceph_osd_linger_request *
3520 ceph_osdc_watch(struct ceph_osd_client *osdc,
3521 struct ceph_object_id *oid,
3522 struct ceph_object_locator *oloc,
3523 rados_watchcb2_t wcb,
3524 rados_watcherrcb_t errcb,
3527 struct ceph_osd_linger_request *lreq;
3530 lreq = linger_alloc(osdc);
3532 return ERR_PTR(-ENOMEM);
3534 lreq->is_watch = true;
3536 lreq->errcb = errcb;
3538 lreq->watch_valid_thru = jiffies;
3540 ceph_oid_copy(&lreq->t.base_oid, oid);
3541 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3542 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
3543 lreq->mtime = CURRENT_TIME;
3545 lreq->reg_req = alloc_linger_request(lreq);
3546 if (!lreq->reg_req) {
3551 lreq->ping_req = alloc_linger_request(lreq);
3552 if (!lreq->ping_req) {
3557 down_write(&osdc->lock);
3558 linger_register(lreq); /* before osd_req_op_* */
3559 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
3560 CEPH_OSD_WATCH_OP_WATCH);
3561 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
3562 CEPH_OSD_WATCH_OP_PING);
3563 linger_submit(lreq);
3564 up_write(&osdc->lock);
3566 ret = linger_reg_commit_wait(lreq);
3568 linger_cancel(lreq);
3576 return ERR_PTR(ret);
3578 EXPORT_SYMBOL(ceph_osdc_watch);
3583 * Times out after mount_timeout to preserve rbd unmap behaviour
3584 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
3585 * with mount_timeout").
3587 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
3588 struct ceph_osd_linger_request *lreq)
3590 struct ceph_options *opts = osdc->client->options;
3591 struct ceph_osd_request *req;
3594 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3598 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3599 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3600 req->r_flags = CEPH_OSD_FLAG_WRITE;
3601 req->r_mtime = CURRENT_TIME;
3602 osd_req_op_watch_init(req, 0, lreq->linger_id,
3603 CEPH_OSD_WATCH_OP_UNWATCH);
3605 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3609 ceph_osdc_start_request(osdc, req, false);
3610 linger_cancel(lreq);
3612 ret = wait_request_timeout(req, opts->mount_timeout);
3615 ceph_osdc_put_request(req);
3618 EXPORT_SYMBOL(ceph_osdc_unwatch);
3620 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
3621 u64 notify_id, u64 cookie, void *payload,
3624 struct ceph_osd_req_op *op;
3625 struct ceph_pagelist *pl;
3628 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
3630 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3634 ceph_pagelist_init(pl);
3635 ret = ceph_pagelist_encode_64(pl, notify_id);
3636 ret |= ceph_pagelist_encode_64(pl, cookie);
3638 ret |= ceph_pagelist_encode_32(pl, payload_len);
3639 ret |= ceph_pagelist_append(pl, payload, payload_len);
3641 ret |= ceph_pagelist_encode_32(pl, 0);
3644 ceph_pagelist_release(pl);
3648 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
3649 op->indata_len = pl->length;
3653 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
3654 struct ceph_object_id *oid,
3655 struct ceph_object_locator *oloc,
3661 struct ceph_osd_request *req;
3664 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3668 ceph_oid_copy(&req->r_base_oid, oid);
3669 ceph_oloc_copy(&req->r_base_oloc, oloc);
3670 req->r_flags = CEPH_OSD_FLAG_READ;
3672 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3676 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
3681 ceph_osdc_start_request(osdc, req, false);
3682 ret = ceph_osdc_wait_request(osdc, req);
3685 ceph_osdc_put_request(req);
3688 EXPORT_SYMBOL(ceph_osdc_notify_ack);
3690 static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
3691 u64 cookie, u32 prot_ver, u32 timeout,
3692 void *payload, size_t payload_len)
3694 struct ceph_osd_req_op *op;
3695 struct ceph_pagelist *pl;
3698 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
3699 op->notify.cookie = cookie;
3701 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3705 ceph_pagelist_init(pl);
3706 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
3707 ret |= ceph_pagelist_encode_32(pl, timeout);
3708 ret |= ceph_pagelist_encode_32(pl, payload_len);
3709 ret |= ceph_pagelist_append(pl, payload, payload_len);
3711 ceph_pagelist_release(pl);
3715 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
3716 op->indata_len = pl->length;
3721 * @timeout: in seconds
3723 * @preply_{pages,len} are initialized both on success and error.
3724 * The caller is responsible for:
3726 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
3728 int ceph_osdc_notify(struct ceph_osd_client *osdc,
3729 struct ceph_object_id *oid,
3730 struct ceph_object_locator *oloc,
3734 struct page ***preply_pages,
3737 struct ceph_osd_linger_request *lreq;
3738 struct page **pages;
3743 *preply_pages = NULL;
3747 lreq = linger_alloc(osdc);
3751 lreq->preply_pages = preply_pages;
3752 lreq->preply_len = preply_len;
3754 ceph_oid_copy(&lreq->t.base_oid, oid);
3755 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3756 lreq->t.flags = CEPH_OSD_FLAG_READ;
3758 lreq->reg_req = alloc_linger_request(lreq);
3759 if (!lreq->reg_req) {
3765 pages = ceph_alloc_page_vector(1, GFP_NOIO);
3766 if (IS_ERR(pages)) {
3767 ret = PTR_ERR(pages);
3771 down_write(&osdc->lock);
3772 linger_register(lreq); /* before osd_req_op_* */
3773 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
3774 timeout, payload, payload_len);
3776 linger_unregister(lreq);
3777 up_write(&osdc->lock);
3778 ceph_release_page_vector(pages, 1);
3781 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
3783 pages, PAGE_SIZE, 0, false, true);
3784 linger_submit(lreq);
3785 up_write(&osdc->lock);
3787 ret = linger_reg_commit_wait(lreq);
3789 ret = linger_notify_finish_wait(lreq);
3791 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
3793 linger_cancel(lreq);
3798 EXPORT_SYMBOL(ceph_osdc_notify);
3801 * Return the number of milliseconds since the watch was last
3802 * confirmed, or an error. If there is an error, the watch is no
3803 * longer valid, and should be destroyed with ceph_osdc_unwatch().
3805 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
3806 struct ceph_osd_linger_request *lreq)
3808 unsigned long stamp, age;
3811 down_read(&osdc->lock);
3812 mutex_lock(&lreq->lock);
3813 stamp = lreq->watch_valid_thru;
3814 if (!list_empty(&lreq->pending_lworks)) {
3815 struct linger_work *lwork =
3816 list_first_entry(&lreq->pending_lworks,
3820 if (time_before(lwork->queued_stamp, stamp))
3821 stamp = lwork->queued_stamp;
3823 age = jiffies - stamp;
3824 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
3825 lreq, lreq->linger_id, age, lreq->last_error);
3826 /* we are truncating to msecs, so return a safe upper bound */
3827 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
3829 mutex_unlock(&lreq->lock);
3830 up_read(&osdc->lock);
3834 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
3840 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
3841 &struct_v, &struct_len);
3845 ceph_decode_copy(p, &item->name, sizeof(item->name));
3846 item->cookie = ceph_decode_64(p);
3847 *p += 4; /* skip timeout_seconds */
3848 if (struct_v >= 2) {
3849 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
3850 ceph_decode_addr(&item->addr);
3853 dout("%s %s%llu cookie %llu addr %s\n", __func__,
3854 ENTITY_NAME(item->name), item->cookie,
3855 ceph_pr_addr(&item->addr.in_addr));
3859 static int decode_watchers(void **p, void *end,
3860 struct ceph_watch_item **watchers,
3868 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
3869 &struct_v, &struct_len);
3873 *num_watchers = ceph_decode_32(p);
3874 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
3878 for (i = 0; i < *num_watchers; i++) {
3879 ret = decode_watcher(p, end, *watchers + i);
3890 * On success, the caller is responsible for:
3894 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
3895 struct ceph_object_id *oid,
3896 struct ceph_object_locator *oloc,
3897 struct ceph_watch_item **watchers,
3900 struct ceph_osd_request *req;
3901 struct page **pages;
3904 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3908 ceph_oid_copy(&req->r_base_oid, oid);
3909 ceph_oloc_copy(&req->r_base_oloc, oloc);
3910 req->r_flags = CEPH_OSD_FLAG_READ;
3912 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3916 pages = ceph_alloc_page_vector(1, GFP_NOIO);
3917 if (IS_ERR(pages)) {
3918 ret = PTR_ERR(pages);
3922 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
3923 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
3925 pages, PAGE_SIZE, 0, false, true);
3927 ceph_osdc_start_request(osdc, req, false);
3928 ret = ceph_osdc_wait_request(osdc, req);
3930 void *p = page_address(pages[0]);
3931 void *const end = p + req->r_ops[0].outdata_len;
3933 ret = decode_watchers(&p, end, watchers, num_watchers);
3937 ceph_osdc_put_request(req);
3940 EXPORT_SYMBOL(ceph_osdc_list_watchers);
3943 * Call all pending notify callbacks - for use after a watch is
3944 * unregistered, to make sure no more callbacks for it will be invoked
3946 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
3948 dout("%s osdc %p\n", __func__, osdc);
3949 flush_workqueue(osdc->notify_wq);
3951 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
3953 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
3955 down_read(&osdc->lock);
3956 maybe_request_map(osdc);
3957 up_read(&osdc->lock);
3959 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
3962 * Execute an OSD class method on an object.
3964 * @flags: CEPH_OSD_FLAG_*
3965 * @resp_len: in/out param for reply length
3967 int ceph_osdc_call(struct ceph_osd_client *osdc,
3968 struct ceph_object_id *oid,
3969 struct ceph_object_locator *oloc,
3970 const char *class, const char *method,
3972 struct page *req_page, size_t req_len,
3973 struct page *resp_page, size_t *resp_len)
3975 struct ceph_osd_request *req;
3978 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
3981 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3985 ceph_oid_copy(&req->r_base_oid, oid);
3986 ceph_oloc_copy(&req->r_base_oloc, oloc);
3987 req->r_flags = flags;
3989 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3993 osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
3995 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
3998 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
3999 *resp_len, 0, false, false);
4001 ceph_osdc_start_request(osdc, req, false);
4002 ret = ceph_osdc_wait_request(osdc, req);
4004 ret = req->r_ops[0].rval;
4006 *resp_len = req->r_ops[0].outdata_len;
4010 ceph_osdc_put_request(req);
4013 EXPORT_SYMBOL(ceph_osdc_call);
4018 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4023 osdc->client = client;
4024 init_rwsem(&osdc->lock);
4025 osdc->osds = RB_ROOT;
4026 INIT_LIST_HEAD(&osdc->osd_lru);
4027 spin_lock_init(&osdc->osd_lru_lock);
4028 osd_init(&osdc->homeless_osd);
4029 osdc->homeless_osd.o_osdc = osdc;
4030 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
4031 osdc->last_linger_id = CEPH_LINGER_ID_START;
4032 osdc->linger_requests = RB_ROOT;
4033 osdc->map_checks = RB_ROOT;
4034 osdc->linger_map_checks = RB_ROOT;
4035 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
4036 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
4039 osdc->osdmap = ceph_osdmap_alloc();
4043 osdc->req_mempool = mempool_create_slab_pool(10,
4044 ceph_osd_request_cache);
4045 if (!osdc->req_mempool)
4048 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
4049 PAGE_SIZE, 10, true, "osd_op");
4052 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4053 PAGE_SIZE, 10, true, "osd_op_reply");
4058 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
4059 if (!osdc->notify_wq)
4060 goto out_msgpool_reply;
4062 schedule_delayed_work(&osdc->timeout_work,
4063 osdc->client->options->osd_keepalive_timeout);
4064 schedule_delayed_work(&osdc->osds_timeout_work,
4065 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4070 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
4072 ceph_msgpool_destroy(&osdc->msgpool_op);
4074 mempool_destroy(osdc->req_mempool);
4076 ceph_osdmap_destroy(osdc->osdmap);
4081 void ceph_osdc_stop(struct ceph_osd_client *osdc)
4083 flush_workqueue(osdc->notify_wq);
4084 destroy_workqueue(osdc->notify_wq);
4085 cancel_delayed_work_sync(&osdc->timeout_work);
4086 cancel_delayed_work_sync(&osdc->osds_timeout_work);
4088 down_write(&osdc->lock);
4089 while (!RB_EMPTY_ROOT(&osdc->osds)) {
4090 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
4091 struct ceph_osd, o_node);
4094 up_write(&osdc->lock);
4095 WARN_ON(atomic_read(&osdc->homeless_osd.o_ref) != 1);
4096 osd_cleanup(&osdc->homeless_osd);
4098 WARN_ON(!list_empty(&osdc->osd_lru));
4099 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
4100 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
4101 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
4102 WARN_ON(atomic_read(&osdc->num_requests));
4103 WARN_ON(atomic_read(&osdc->num_homeless));
4105 ceph_osdmap_destroy(osdc->osdmap);
4106 mempool_destroy(osdc->req_mempool);
4107 ceph_msgpool_destroy(&osdc->msgpool_op);
4108 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
4112 * Read some contiguous pages. If we cross a stripe boundary, shorten
4113 * *plen. Return number of bytes read, or error.
4115 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
4116 struct ceph_vino vino, struct ceph_file_layout *layout,
4118 u32 truncate_seq, u64 truncate_size,
4119 struct page **pages, int num_pages, int page_align)
4121 struct ceph_osd_request *req;
4124 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
4125 vino.snap, off, *plen);
4126 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
4127 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
4128 NULL, truncate_seq, truncate_size,
4131 return PTR_ERR(req);
4133 /* it may be a short read due to an object boundary */
4134 osd_req_op_extent_osd_data_pages(req, 0,
4135 pages, *plen, page_align, false, false);
4137 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
4138 off, *plen, *plen, page_align);
4140 rc = ceph_osdc_start_request(osdc, req, false);
4142 rc = ceph_osdc_wait_request(osdc, req);
4144 ceph_osdc_put_request(req);
4145 dout("readpages result %d\n", rc);
4148 EXPORT_SYMBOL(ceph_osdc_readpages);
4151 * do a synchronous write on N pages
4153 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
4154 struct ceph_file_layout *layout,
4155 struct ceph_snap_context *snapc,
4157 u32 truncate_seq, u64 truncate_size,
4158 struct timespec *mtime,
4159 struct page **pages, int num_pages)
4161 struct ceph_osd_request *req;
4163 int page_align = off & ~PAGE_MASK;
4165 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
4166 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
4167 snapc, truncate_seq, truncate_size,
4170 return PTR_ERR(req);
4172 /* it may be a short write due to an object boundary */
4173 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
4175 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
4177 req->r_mtime = *mtime;
4178 rc = ceph_osdc_start_request(osdc, req, true);
4180 rc = ceph_osdc_wait_request(osdc, req);
4182 ceph_osdc_put_request(req);
4185 dout("writepages result %d\n", rc);
4188 EXPORT_SYMBOL(ceph_osdc_writepages);
4190 int ceph_osdc_setup(void)
4192 size_t size = sizeof(struct ceph_osd_request) +
4193 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
4195 BUG_ON(ceph_osd_request_cache);
4196 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
4199 return ceph_osd_request_cache ? 0 : -ENOMEM;
4201 EXPORT_SYMBOL(ceph_osdc_setup);
4203 void ceph_osdc_cleanup(void)
4205 BUG_ON(!ceph_osd_request_cache);
4206 kmem_cache_destroy(ceph_osd_request_cache);
4207 ceph_osd_request_cache = NULL;
4209 EXPORT_SYMBOL(ceph_osdc_cleanup);
4212 * handle incoming message
4214 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4216 struct ceph_osd *osd = con->private;
4217 struct ceph_osd_client *osdc = osd->o_osdc;
4218 int type = le16_to_cpu(msg->hdr.type);
4221 case CEPH_MSG_OSD_MAP:
4222 ceph_osdc_handle_map(osdc, msg);
4224 case CEPH_MSG_OSD_OPREPLY:
4225 handle_reply(osd, msg);
4227 case CEPH_MSG_WATCH_NOTIFY:
4228 handle_watch_notify(osdc, msg);
4232 pr_err("received unknown message type %d %s\n", type,
4233 ceph_msg_type_name(type));
4240 * Lookup and return message for incoming reply. Don't try to do
4241 * anything about a larger than preallocated data portion of the
4242 * message at the moment - for now, just skip the message.
4244 static struct ceph_msg *get_reply(struct ceph_connection *con,
4245 struct ceph_msg_header *hdr,
4248 struct ceph_osd *osd = con->private;
4249 struct ceph_osd_client *osdc = osd->o_osdc;
4250 struct ceph_msg *m = NULL;
4251 struct ceph_osd_request *req;
4252 int front_len = le32_to_cpu(hdr->front_len);
4253 int data_len = le32_to_cpu(hdr->data_len);
4254 u64 tid = le64_to_cpu(hdr->tid);
4256 down_read(&osdc->lock);
4257 if (!osd_registered(osd)) {
4258 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
4260 goto out_unlock_osdc;
4262 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
4264 mutex_lock(&osd->lock);
4265 req = lookup_request(&osd->o_requests, tid);
4267 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
4270 goto out_unlock_session;
4273 ceph_msg_revoke_incoming(req->r_reply);
4275 if (front_len > req->r_reply->front_alloc_len) {
4276 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
4277 __func__, osd->o_osd, req->r_tid, front_len,
4278 req->r_reply->front_alloc_len);
4279 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
4282 goto out_unlock_session;
4283 ceph_msg_put(req->r_reply);
4287 if (data_len > req->r_reply->data_length) {
4288 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
4289 __func__, osd->o_osd, req->r_tid, data_len,
4290 req->r_reply->data_length);
4293 goto out_unlock_session;
4296 m = ceph_msg_get(req->r_reply);
4297 dout("get_reply tid %lld %p\n", tid, m);
4300 mutex_unlock(&osd->lock);
4302 up_read(&osdc->lock);
4307 * TODO: switch to a msg-owned pagelist
4309 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
4312 int type = le16_to_cpu(hdr->type);
4313 u32 front_len = le32_to_cpu(hdr->front_len);
4314 u32 data_len = le32_to_cpu(hdr->data_len);
4316 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
4321 struct page **pages;
4322 struct ceph_osd_data osd_data;
4324 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
4326 if (IS_ERR(pages)) {
4331 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
4333 ceph_osdc_msg_data_add(m, &osd_data);
4339 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
4340 struct ceph_msg_header *hdr,
4343 struct ceph_osd *osd = con->private;
4344 int type = le16_to_cpu(hdr->type);
4348 case CEPH_MSG_OSD_MAP:
4349 case CEPH_MSG_WATCH_NOTIFY:
4350 return alloc_msg_with_page_vector(hdr);
4351 case CEPH_MSG_OSD_OPREPLY:
4352 return get_reply(con, hdr, skip);
4354 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
4362 * Wrappers to refcount containing ceph_osd struct
4364 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
4366 struct ceph_osd *osd = con->private;
4372 static void put_osd_con(struct ceph_connection *con)
4374 struct ceph_osd *osd = con->private;
4382 * Note: returned pointer is the address of a structure that's
4383 * managed separately. Caller must *not* attempt to free it.
4385 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
4386 int *proto, int force_new)
4388 struct ceph_osd *o = con->private;
4389 struct ceph_osd_client *osdc = o->o_osdc;
4390 struct ceph_auth_client *ac = osdc->client->monc.auth;
4391 struct ceph_auth_handshake *auth = &o->o_auth;
4393 if (force_new && auth->authorizer) {
4394 ceph_auth_destroy_authorizer(auth->authorizer);
4395 auth->authorizer = NULL;
4397 if (!auth->authorizer) {
4398 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4401 return ERR_PTR(ret);
4403 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4406 return ERR_PTR(ret);
4408 *proto = ac->protocol;
4414 static int verify_authorizer_reply(struct ceph_connection *con)
4416 struct ceph_osd *o = con->private;
4417 struct ceph_osd_client *osdc = o->o_osdc;
4418 struct ceph_auth_client *ac = osdc->client->monc.auth;
4420 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
4423 static int invalidate_authorizer(struct ceph_connection *con)
4425 struct ceph_osd *o = con->private;
4426 struct ceph_osd_client *osdc = o->o_osdc;
4427 struct ceph_auth_client *ac = osdc->client->monc.auth;
4429 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
4430 return ceph_monc_validate_auth(&osdc->client->monc);
4433 static int osd_sign_message(struct ceph_msg *msg)
4435 struct ceph_osd *o = msg->con->private;
4436 struct ceph_auth_handshake *auth = &o->o_auth;
4438 return ceph_auth_sign_message(auth, msg);
4441 static int osd_check_message_signature(struct ceph_msg *msg)
4443 struct ceph_osd *o = msg->con->private;
4444 struct ceph_auth_handshake *auth = &o->o_auth;
4446 return ceph_auth_check_message_signature(auth, msg);
4449 static const struct ceph_connection_operations osd_con_ops = {
4452 .dispatch = dispatch,
4453 .get_authorizer = get_authorizer,
4454 .verify_authorizer_reply = verify_authorizer_reply,
4455 .invalidate_authorizer = invalidate_authorizer,
4456 .alloc_msg = alloc_msg,
4457 .sign_message = osd_sign_message,
4458 .check_message_signature = osd_check_message_signature,