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_abort_on_full = true;
965 req->r_flags = flags;
966 req->r_base_oloc.pool = layout->pool_id;
967 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
968 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
970 req->r_snapid = vino.snap;
971 if (flags & CEPH_OSD_FLAG_WRITE)
972 req->r_data_offset = off;
974 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
981 ceph_osdc_put_request(req);
984 EXPORT_SYMBOL(ceph_osdc_new_request);
987 * We keep osd requests in an rbtree, sorted by ->r_tid.
989 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
990 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
992 static bool osd_homeless(struct ceph_osd *osd)
994 return osd->o_osd == CEPH_HOMELESS_OSD;
997 static bool osd_registered(struct ceph_osd *osd)
999 verify_osdc_locked(osd->o_osdc);
1001 return !RB_EMPTY_NODE(&osd->o_node);
1005 * Assumes @osd is zero-initialized.
1007 static void osd_init(struct ceph_osd *osd)
1009 refcount_set(&osd->o_ref, 1);
1010 RB_CLEAR_NODE(&osd->o_node);
1011 osd->o_requests = RB_ROOT;
1012 osd->o_linger_requests = RB_ROOT;
1013 INIT_LIST_HEAD(&osd->o_osd_lru);
1014 INIT_LIST_HEAD(&osd->o_keepalive_item);
1015 osd->o_incarnation = 1;
1016 mutex_init(&osd->lock);
1019 static void osd_cleanup(struct ceph_osd *osd)
1021 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1022 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1023 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1024 WARN_ON(!list_empty(&osd->o_osd_lru));
1025 WARN_ON(!list_empty(&osd->o_keepalive_item));
1027 if (osd->o_auth.authorizer) {
1028 WARN_ON(osd_homeless(osd));
1029 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1034 * Track open sessions with osds.
1036 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1038 struct ceph_osd *osd;
1040 WARN_ON(onum == CEPH_HOMELESS_OSD);
1042 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1047 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1052 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1054 if (refcount_inc_not_zero(&osd->o_ref)) {
1055 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1056 refcount_read(&osd->o_ref));
1059 dout("get_osd %p FAIL\n", osd);
1064 static void put_osd(struct ceph_osd *osd)
1066 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1067 refcount_read(&osd->o_ref) - 1);
1068 if (refcount_dec_and_test(&osd->o_ref)) {
1074 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1076 static void __move_osd_to_lru(struct ceph_osd *osd)
1078 struct ceph_osd_client *osdc = osd->o_osdc;
1080 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1081 BUG_ON(!list_empty(&osd->o_osd_lru));
1083 spin_lock(&osdc->osd_lru_lock);
1084 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1085 spin_unlock(&osdc->osd_lru_lock);
1087 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1090 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1092 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1093 RB_EMPTY_ROOT(&osd->o_linger_requests))
1094 __move_osd_to_lru(osd);
1097 static void __remove_osd_from_lru(struct ceph_osd *osd)
1099 struct ceph_osd_client *osdc = osd->o_osdc;
1101 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1103 spin_lock(&osdc->osd_lru_lock);
1104 if (!list_empty(&osd->o_osd_lru))
1105 list_del_init(&osd->o_osd_lru);
1106 spin_unlock(&osdc->osd_lru_lock);
1110 * Close the connection and assign any leftover requests to the
1113 static void close_osd(struct ceph_osd *osd)
1115 struct ceph_osd_client *osdc = osd->o_osdc;
1118 verify_osdc_wrlocked(osdc);
1119 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1121 ceph_con_close(&osd->o_con);
1123 for (n = rb_first(&osd->o_requests); n; ) {
1124 struct ceph_osd_request *req =
1125 rb_entry(n, struct ceph_osd_request, r_node);
1127 n = rb_next(n); /* unlink_request() */
1129 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1130 unlink_request(osd, req);
1131 link_request(&osdc->homeless_osd, req);
1133 for (n = rb_first(&osd->o_linger_requests); n; ) {
1134 struct ceph_osd_linger_request *lreq =
1135 rb_entry(n, struct ceph_osd_linger_request, node);
1137 n = rb_next(n); /* unlink_linger() */
1139 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1141 unlink_linger(osd, lreq);
1142 link_linger(&osdc->homeless_osd, lreq);
1145 __remove_osd_from_lru(osd);
1146 erase_osd(&osdc->osds, osd);
1153 static int reopen_osd(struct ceph_osd *osd)
1155 struct ceph_entity_addr *peer_addr;
1157 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1159 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1160 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1165 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1166 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1167 !ceph_con_opened(&osd->o_con)) {
1170 dout("osd addr hasn't changed and connection never opened, "
1171 "letting msgr retry\n");
1172 /* touch each r_stamp for handle_timeout()'s benfit */
1173 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1174 struct ceph_osd_request *req =
1175 rb_entry(n, struct ceph_osd_request, r_node);
1176 req->r_stamp = jiffies;
1182 ceph_con_close(&osd->o_con);
1183 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1184 osd->o_incarnation++;
1189 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1192 struct ceph_osd *osd;
1195 verify_osdc_wrlocked(osdc);
1197 verify_osdc_locked(osdc);
1199 if (o != CEPH_HOMELESS_OSD)
1200 osd = lookup_osd(&osdc->osds, o);
1202 osd = &osdc->homeless_osd;
1205 return ERR_PTR(-EAGAIN);
1207 osd = create_osd(osdc, o);
1208 insert_osd(&osdc->osds, osd);
1209 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1210 &osdc->osdmap->osd_addr[osd->o_osd]);
1213 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1218 * Create request <-> OSD session relation.
1220 * @req has to be assigned a tid, @osd may be homeless.
1222 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1224 verify_osd_locked(osd);
1225 WARN_ON(!req->r_tid || req->r_osd);
1226 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1229 if (!osd_homeless(osd))
1230 __remove_osd_from_lru(osd);
1232 atomic_inc(&osd->o_osdc->num_homeless);
1235 insert_request(&osd->o_requests, req);
1239 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1241 verify_osd_locked(osd);
1242 WARN_ON(req->r_osd != osd);
1243 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1247 erase_request(&osd->o_requests, req);
1250 if (!osd_homeless(osd))
1251 maybe_move_osd_to_lru(osd);
1253 atomic_dec(&osd->o_osdc->num_homeless);
1256 static bool __pool_full(struct ceph_pg_pool_info *pi)
1258 return pi->flags & CEPH_POOL_FLAG_FULL;
1261 static bool have_pool_full(struct ceph_osd_client *osdc)
1265 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1266 struct ceph_pg_pool_info *pi =
1267 rb_entry(n, struct ceph_pg_pool_info, node);
1269 if (__pool_full(pi))
1276 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1278 struct ceph_pg_pool_info *pi;
1280 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1284 return __pool_full(pi);
1288 * Returns whether a request should be blocked from being sent
1289 * based on the current osdmap and osd_client settings.
1291 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1292 const struct ceph_osd_request_target *t,
1293 struct ceph_pg_pool_info *pi)
1295 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1296 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1297 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1300 WARN_ON(pi->id != t->base_oloc.pool);
1301 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1302 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1303 (osdc->osdmap->epoch < osdc->epoch_barrier);
1306 enum calc_target_result {
1307 CALC_TARGET_NO_ACTION = 0,
1308 CALC_TARGET_NEED_RESEND,
1309 CALC_TARGET_POOL_DNE,
1312 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1313 struct ceph_osd_request_target *t,
1314 u32 *last_force_resend,
1317 struct ceph_pg_pool_info *pi;
1318 struct ceph_pg pgid, last_pgid;
1319 struct ceph_osds up, acting;
1320 bool force_resend = false;
1321 bool need_check_tiering = false;
1322 bool need_resend = false;
1323 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1324 enum calc_target_result ct_res;
1327 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1329 t->osd = CEPH_HOMELESS_OSD;
1330 ct_res = CALC_TARGET_POOL_DNE;
1334 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1335 if (last_force_resend &&
1336 *last_force_resend < pi->last_force_request_resend) {
1337 *last_force_resend = pi->last_force_request_resend;
1338 force_resend = true;
1339 } else if (!last_force_resend) {
1340 force_resend = true;
1343 if (ceph_oid_empty(&t->target_oid) || force_resend) {
1344 ceph_oid_copy(&t->target_oid, &t->base_oid);
1345 need_check_tiering = true;
1347 if (ceph_oloc_empty(&t->target_oloc) || force_resend) {
1348 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1349 need_check_tiering = true;
1352 if (need_check_tiering &&
1353 (t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1354 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1355 t->target_oloc.pool = pi->read_tier;
1356 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1357 t->target_oloc.pool = pi->write_tier;
1360 ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1361 &t->target_oloc, &pgid);
1363 WARN_ON(ret != -ENOENT);
1364 t->osd = CEPH_HOMELESS_OSD;
1365 ct_res = CALC_TARGET_POOL_DNE;
1368 last_pgid.pool = pgid.pool;
1369 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1371 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1373 ceph_is_new_interval(&t->acting,
1386 force_resend = true;
1388 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1393 if (ceph_pg_compare(&t->pgid, &pgid) ||
1394 ceph_osds_changed(&t->acting, &acting, any_change) ||
1396 t->pgid = pgid; /* struct */
1397 ceph_osds_copy(&t->acting, &acting);
1398 ceph_osds_copy(&t->up, &up);
1400 t->min_size = pi->min_size;
1401 t->pg_num = pi->pg_num;
1402 t->pg_num_mask = pi->pg_num_mask;
1403 t->sort_bitwise = sort_bitwise;
1405 t->osd = acting.primary;
1409 ct_res = need_resend ? CALC_TARGET_NEED_RESEND : CALC_TARGET_NO_ACTION;
1411 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1415 static void setup_request_data(struct ceph_osd_request *req,
1416 struct ceph_msg *msg)
1421 if (!list_empty(&msg->data))
1424 WARN_ON(msg->data_length);
1425 for (i = 0; i < req->r_num_ops; i++) {
1426 struct ceph_osd_req_op *op = &req->r_ops[i];
1430 case CEPH_OSD_OP_WRITE:
1431 case CEPH_OSD_OP_WRITEFULL:
1432 WARN_ON(op->indata_len != op->extent.length);
1433 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1435 case CEPH_OSD_OP_SETXATTR:
1436 case CEPH_OSD_OP_CMPXATTR:
1437 WARN_ON(op->indata_len != op->xattr.name_len +
1438 op->xattr.value_len);
1439 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1441 case CEPH_OSD_OP_NOTIFY_ACK:
1442 ceph_osdc_msg_data_add(msg,
1443 &op->notify_ack.request_data);
1447 case CEPH_OSD_OP_STAT:
1448 ceph_osdc_msg_data_add(req->r_reply,
1451 case CEPH_OSD_OP_READ:
1452 ceph_osdc_msg_data_add(req->r_reply,
1453 &op->extent.osd_data);
1455 case CEPH_OSD_OP_LIST_WATCHERS:
1456 ceph_osdc_msg_data_add(req->r_reply,
1457 &op->list_watchers.response_data);
1461 case CEPH_OSD_OP_CALL:
1462 WARN_ON(op->indata_len != op->cls.class_len +
1463 op->cls.method_len +
1464 op->cls.indata_len);
1465 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1466 /* optional, can be NONE */
1467 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1468 /* optional, can be NONE */
1469 ceph_osdc_msg_data_add(req->r_reply,
1470 &op->cls.response_data);
1472 case CEPH_OSD_OP_NOTIFY:
1473 ceph_osdc_msg_data_add(msg,
1474 &op->notify.request_data);
1475 ceph_osdc_msg_data_add(req->r_reply,
1476 &op->notify.response_data);
1480 data_len += op->indata_len;
1483 WARN_ON(data_len != msg->data_length);
1486 static void encode_request(struct ceph_osd_request *req, struct ceph_msg *msg)
1488 void *p = msg->front.iov_base;
1489 void *const end = p + msg->front_alloc_len;
1493 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1494 /* snapshots aren't writeable */
1495 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1497 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1498 req->r_data_offset || req->r_snapc);
1501 setup_request_data(req, msg);
1503 ceph_encode_32(&p, 1); /* client_inc, always 1 */
1504 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1505 ceph_encode_32(&p, req->r_flags);
1506 ceph_encode_timespec(p, &req->r_mtime);
1507 p += sizeof(struct ceph_timespec);
1509 /* reassert_version */
1510 memset(p, 0, sizeof(struct ceph_eversion));
1511 p += sizeof(struct ceph_eversion);
1514 ceph_start_encoding(&p, 5, 4,
1515 ceph_oloc_encoding_size(&req->r_t.target_oloc));
1516 ceph_encode_64(&p, req->r_t.target_oloc.pool);
1517 ceph_encode_32(&p, -1); /* preferred */
1518 ceph_encode_32(&p, 0); /* key len */
1519 if (req->r_t.target_oloc.pool_ns)
1520 ceph_encode_string(&p, end, req->r_t.target_oloc.pool_ns->str,
1521 req->r_t.target_oloc.pool_ns->len);
1523 ceph_encode_32(&p, 0);
1526 ceph_encode_8(&p, 1);
1527 ceph_encode_64(&p, req->r_t.pgid.pool);
1528 ceph_encode_32(&p, req->r_t.pgid.seed);
1529 ceph_encode_32(&p, -1); /* preferred */
1532 ceph_encode_32(&p, req->r_t.target_oid.name_len);
1533 memcpy(p, req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1534 p += req->r_t.target_oid.name_len;
1536 /* ops, can imply data */
1537 ceph_encode_16(&p, req->r_num_ops);
1538 for (i = 0; i < req->r_num_ops; i++) {
1539 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1540 p += sizeof(struct ceph_osd_op);
1543 ceph_encode_64(&p, req->r_snapid); /* snapid */
1545 ceph_encode_64(&p, req->r_snapc->seq);
1546 ceph_encode_32(&p, req->r_snapc->num_snaps);
1547 for (i = 0; i < req->r_snapc->num_snaps; i++)
1548 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1550 ceph_encode_64(&p, 0); /* snap_seq */
1551 ceph_encode_32(&p, 0); /* snaps len */
1554 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1557 msg->front.iov_len = p - msg->front.iov_base;
1558 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1559 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1560 msg->hdr.data_len = cpu_to_le32(data_len);
1562 * The header "data_off" is a hint to the receiver allowing it
1563 * to align received data into its buffers such that there's no
1564 * need to re-copy it before writing it to disk (direct I/O).
1566 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1568 dout("%s req %p oid %s oid_len %d front %zu data %u\n", __func__,
1569 req, req->r_t.target_oid.name, req->r_t.target_oid.name_len,
1570 msg->front.iov_len, data_len);
1574 * @req has to be assigned a tid and registered.
1576 static void send_request(struct ceph_osd_request *req)
1578 struct ceph_osd *osd = req->r_osd;
1580 verify_osd_locked(osd);
1581 WARN_ON(osd->o_osd != req->r_t.osd);
1584 * We may have a previously queued request message hanging
1585 * around. Cancel it to avoid corrupting the msgr.
1588 ceph_msg_revoke(req->r_request);
1590 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1591 if (req->r_attempts)
1592 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1594 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1596 encode_request(req, req->r_request);
1598 dout("%s req %p tid %llu to pg %llu.%x osd%d flags 0x%x attempt %d\n",
1599 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
1600 req->r_t.osd, req->r_flags, req->r_attempts);
1602 req->r_t.paused = false;
1603 req->r_stamp = jiffies;
1606 req->r_sent = osd->o_incarnation;
1607 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1608 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
1611 static void maybe_request_map(struct ceph_osd_client *osdc)
1613 bool continuous = false;
1615 verify_osdc_locked(osdc);
1616 WARN_ON(!osdc->osdmap->epoch);
1618 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1619 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
1620 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
1621 dout("%s osdc %p continuous\n", __func__, osdc);
1624 dout("%s osdc %p onetime\n", __func__, osdc);
1627 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
1628 osdc->osdmap->epoch + 1, continuous))
1629 ceph_monc_renew_subs(&osdc->client->monc);
1632 static void complete_request(struct ceph_osd_request *req, int err);
1633 static void send_map_check(struct ceph_osd_request *req);
1635 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
1637 struct ceph_osd_client *osdc = req->r_osdc;
1638 struct ceph_osd *osd;
1639 enum calc_target_result ct_res;
1640 bool need_send = false;
1641 bool promoted = false;
1642 bool need_abort = false;
1644 WARN_ON(req->r_tid);
1645 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
1648 ct_res = calc_target(osdc, &req->r_t, &req->r_last_force_resend, false);
1649 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
1652 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
1654 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
1658 if (osdc->osdmap->epoch < osdc->epoch_barrier) {
1659 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
1660 osdc->epoch_barrier);
1661 req->r_t.paused = true;
1662 maybe_request_map(osdc);
1663 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1664 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
1665 dout("req %p pausewr\n", req);
1666 req->r_t.paused = true;
1667 maybe_request_map(osdc);
1668 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1669 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
1670 dout("req %p pauserd\n", req);
1671 req->r_t.paused = true;
1672 maybe_request_map(osdc);
1673 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1674 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
1675 CEPH_OSD_FLAG_FULL_FORCE)) &&
1676 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1677 pool_full(osdc, req->r_t.base_oloc.pool))) {
1678 dout("req %p full/pool_full\n", req);
1679 pr_warn_ratelimited("FULL or reached pool quota\n");
1680 req->r_t.paused = true;
1681 maybe_request_map(osdc);
1682 if (req->r_abort_on_full)
1684 } else if (!osd_homeless(osd)) {
1687 maybe_request_map(osdc);
1690 mutex_lock(&osd->lock);
1692 * Assign the tid atomically with send_request() to protect
1693 * multiple writes to the same object from racing with each
1694 * other, resulting in out of order ops on the OSDs.
1696 req->r_tid = atomic64_inc_return(&osdc->last_tid);
1697 link_request(osd, req);
1700 else if (need_abort)
1701 complete_request(req, -ENOSPC);
1702 mutex_unlock(&osd->lock);
1704 if (ct_res == CALC_TARGET_POOL_DNE)
1705 send_map_check(req);
1708 downgrade_write(&osdc->lock);
1712 up_read(&osdc->lock);
1713 down_write(&osdc->lock);
1719 static void account_request(struct ceph_osd_request *req)
1721 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
1722 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
1724 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
1725 atomic_inc(&req->r_osdc->num_requests);
1727 req->r_start_stamp = jiffies;
1730 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
1732 ceph_osdc_get_request(req);
1733 account_request(req);
1734 __submit_request(req, wrlocked);
1737 static void finish_request(struct ceph_osd_request *req)
1739 struct ceph_osd_client *osdc = req->r_osdc;
1740 struct ceph_osd *osd = req->r_osd;
1742 verify_osd_locked(osd);
1743 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1745 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
1746 unlink_request(osd, req);
1747 atomic_dec(&osdc->num_requests);
1750 * If an OSD has failed or returned and a request has been sent
1751 * twice, it's possible to get a reply and end up here while the
1752 * request message is queued for delivery. We will ignore the
1753 * reply, so not a big deal, but better to try and catch it.
1755 ceph_msg_revoke(req->r_request);
1756 ceph_msg_revoke_incoming(req->r_reply);
1759 static void __complete_request(struct ceph_osd_request *req)
1761 if (req->r_callback) {
1762 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
1763 req->r_tid, req->r_callback, req->r_result);
1764 req->r_callback(req);
1769 * This is open-coded in handle_reply().
1771 static void complete_request(struct ceph_osd_request *req, int err)
1773 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1775 req->r_result = err;
1776 finish_request(req);
1777 __complete_request(req);
1778 complete_all(&req->r_completion);
1779 ceph_osdc_put_request(req);
1782 static void cancel_map_check(struct ceph_osd_request *req)
1784 struct ceph_osd_client *osdc = req->r_osdc;
1785 struct ceph_osd_request *lookup_req;
1787 verify_osdc_wrlocked(osdc);
1789 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1793 WARN_ON(lookup_req != req);
1794 erase_request_mc(&osdc->map_checks, req);
1795 ceph_osdc_put_request(req);
1798 static void cancel_request(struct ceph_osd_request *req)
1800 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1802 cancel_map_check(req);
1803 finish_request(req);
1804 complete_all(&req->r_completion);
1805 ceph_osdc_put_request(req);
1808 static void abort_request(struct ceph_osd_request *req, int err)
1810 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1812 cancel_map_check(req);
1813 complete_request(req, err);
1816 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
1818 if (likely(eb > osdc->epoch_barrier)) {
1819 dout("updating epoch_barrier from %u to %u\n",
1820 osdc->epoch_barrier, eb);
1821 osdc->epoch_barrier = eb;
1822 /* Request map if we're not to the barrier yet */
1823 if (eb > osdc->osdmap->epoch)
1824 maybe_request_map(osdc);
1828 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
1830 down_read(&osdc->lock);
1831 if (unlikely(eb > osdc->epoch_barrier)) {
1832 up_read(&osdc->lock);
1833 down_write(&osdc->lock);
1834 update_epoch_barrier(osdc, eb);
1835 up_write(&osdc->lock);
1837 up_read(&osdc->lock);
1840 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
1843 * Drop all pending requests that are stalled waiting on a full condition to
1844 * clear, and complete them with ENOSPC as the return code. Set the
1845 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
1848 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
1851 bool victims = false;
1853 dout("enter abort_on_full\n");
1855 if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
1858 /* Scan list and see if there is anything to abort */
1859 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1860 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1863 m = rb_first(&osd->o_requests);
1865 struct ceph_osd_request *req = rb_entry(m,
1866 struct ceph_osd_request, r_node);
1869 if (req->r_abort_on_full) {
1882 * Update the barrier to current epoch if it's behind that point,
1883 * since we know we have some calls to be aborted in the tree.
1885 update_epoch_barrier(osdc, osdc->osdmap->epoch);
1887 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1888 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1891 m = rb_first(&osd->o_requests);
1893 struct ceph_osd_request *req = rb_entry(m,
1894 struct ceph_osd_request, r_node);
1897 if (req->r_abort_on_full &&
1898 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1899 pool_full(osdc, req->r_t.target_oloc.pool)))
1900 abort_request(req, -ENOSPC);
1904 dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
1907 static void check_pool_dne(struct ceph_osd_request *req)
1909 struct ceph_osd_client *osdc = req->r_osdc;
1910 struct ceph_osdmap *map = osdc->osdmap;
1912 verify_osdc_wrlocked(osdc);
1913 WARN_ON(!map->epoch);
1915 if (req->r_attempts) {
1917 * We sent a request earlier, which means that
1918 * previously the pool existed, and now it does not
1919 * (i.e., it was deleted).
1921 req->r_map_dne_bound = map->epoch;
1922 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
1925 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
1926 req, req->r_tid, req->r_map_dne_bound, map->epoch);
1929 if (req->r_map_dne_bound) {
1930 if (map->epoch >= req->r_map_dne_bound) {
1931 /* we had a new enough map */
1932 pr_info_ratelimited("tid %llu pool does not exist\n",
1934 complete_request(req, -ENOENT);
1937 send_map_check(req);
1941 static void map_check_cb(struct ceph_mon_generic_request *greq)
1943 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
1944 struct ceph_osd_request *req;
1945 u64 tid = greq->private_data;
1947 WARN_ON(greq->result || !greq->u.newest);
1949 down_write(&osdc->lock);
1950 req = lookup_request_mc(&osdc->map_checks, tid);
1952 dout("%s tid %llu dne\n", __func__, tid);
1956 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
1957 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
1958 if (!req->r_map_dne_bound)
1959 req->r_map_dne_bound = greq->u.newest;
1960 erase_request_mc(&osdc->map_checks, req);
1961 check_pool_dne(req);
1963 ceph_osdc_put_request(req);
1965 up_write(&osdc->lock);
1968 static void send_map_check(struct ceph_osd_request *req)
1970 struct ceph_osd_client *osdc = req->r_osdc;
1971 struct ceph_osd_request *lookup_req;
1974 verify_osdc_wrlocked(osdc);
1976 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1978 WARN_ON(lookup_req != req);
1982 ceph_osdc_get_request(req);
1983 insert_request_mc(&osdc->map_checks, req);
1984 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
1985 map_check_cb, req->r_tid);
1990 * lingering requests, watch/notify v2 infrastructure
1992 static void linger_release(struct kref *kref)
1994 struct ceph_osd_linger_request *lreq =
1995 container_of(kref, struct ceph_osd_linger_request, kref);
1997 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
1998 lreq->reg_req, lreq->ping_req);
1999 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2000 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2001 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2002 WARN_ON(!list_empty(&lreq->scan_item));
2003 WARN_ON(!list_empty(&lreq->pending_lworks));
2007 ceph_osdc_put_request(lreq->reg_req);
2009 ceph_osdc_put_request(lreq->ping_req);
2010 target_destroy(&lreq->t);
2014 static void linger_put(struct ceph_osd_linger_request *lreq)
2017 kref_put(&lreq->kref, linger_release);
2020 static struct ceph_osd_linger_request *
2021 linger_get(struct ceph_osd_linger_request *lreq)
2023 kref_get(&lreq->kref);
2027 static struct ceph_osd_linger_request *
2028 linger_alloc(struct ceph_osd_client *osdc)
2030 struct ceph_osd_linger_request *lreq;
2032 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2036 kref_init(&lreq->kref);
2037 mutex_init(&lreq->lock);
2038 RB_CLEAR_NODE(&lreq->node);
2039 RB_CLEAR_NODE(&lreq->osdc_node);
2040 RB_CLEAR_NODE(&lreq->mc_node);
2041 INIT_LIST_HEAD(&lreq->scan_item);
2042 INIT_LIST_HEAD(&lreq->pending_lworks);
2043 init_completion(&lreq->reg_commit_wait);
2044 init_completion(&lreq->notify_finish_wait);
2047 target_init(&lreq->t);
2049 dout("%s lreq %p\n", __func__, lreq);
2053 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2054 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2055 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2058 * Create linger request <-> OSD session relation.
2060 * @lreq has to be registered, @osd may be homeless.
2062 static void link_linger(struct ceph_osd *osd,
2063 struct ceph_osd_linger_request *lreq)
2065 verify_osd_locked(osd);
2066 WARN_ON(!lreq->linger_id || lreq->osd);
2067 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2068 osd->o_osd, lreq, lreq->linger_id);
2070 if (!osd_homeless(osd))
2071 __remove_osd_from_lru(osd);
2073 atomic_inc(&osd->o_osdc->num_homeless);
2076 insert_linger(&osd->o_linger_requests, lreq);
2080 static void unlink_linger(struct ceph_osd *osd,
2081 struct ceph_osd_linger_request *lreq)
2083 verify_osd_locked(osd);
2084 WARN_ON(lreq->osd != osd);
2085 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2086 osd->o_osd, lreq, lreq->linger_id);
2089 erase_linger(&osd->o_linger_requests, lreq);
2092 if (!osd_homeless(osd))
2093 maybe_move_osd_to_lru(osd);
2095 atomic_dec(&osd->o_osdc->num_homeless);
2098 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2100 verify_osdc_locked(lreq->osdc);
2102 return !RB_EMPTY_NODE(&lreq->osdc_node);
2105 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2107 struct ceph_osd_client *osdc = lreq->osdc;
2110 down_read(&osdc->lock);
2111 registered = __linger_registered(lreq);
2112 up_read(&osdc->lock);
2117 static void linger_register(struct ceph_osd_linger_request *lreq)
2119 struct ceph_osd_client *osdc = lreq->osdc;
2121 verify_osdc_wrlocked(osdc);
2122 WARN_ON(lreq->linger_id);
2125 lreq->linger_id = ++osdc->last_linger_id;
2126 insert_linger_osdc(&osdc->linger_requests, lreq);
2129 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2131 struct ceph_osd_client *osdc = lreq->osdc;
2133 verify_osdc_wrlocked(osdc);
2135 erase_linger_osdc(&osdc->linger_requests, lreq);
2139 static void cancel_linger_request(struct ceph_osd_request *req)
2141 struct ceph_osd_linger_request *lreq = req->r_priv;
2143 WARN_ON(!req->r_linger);
2144 cancel_request(req);
2148 struct linger_work {
2149 struct work_struct work;
2150 struct ceph_osd_linger_request *lreq;
2151 struct list_head pending_item;
2152 unsigned long queued_stamp;
2158 void *payload; /* points into @msg front */
2161 struct ceph_msg *msg; /* for ceph_msg_put() */
2169 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2172 struct linger_work *lwork;
2174 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2178 INIT_WORK(&lwork->work, workfn);
2179 INIT_LIST_HEAD(&lwork->pending_item);
2180 lwork->lreq = linger_get(lreq);
2185 static void lwork_free(struct linger_work *lwork)
2187 struct ceph_osd_linger_request *lreq = lwork->lreq;
2189 mutex_lock(&lreq->lock);
2190 list_del(&lwork->pending_item);
2191 mutex_unlock(&lreq->lock);
2197 static void lwork_queue(struct linger_work *lwork)
2199 struct ceph_osd_linger_request *lreq = lwork->lreq;
2200 struct ceph_osd_client *osdc = lreq->osdc;
2202 verify_lreq_locked(lreq);
2203 WARN_ON(!list_empty(&lwork->pending_item));
2205 lwork->queued_stamp = jiffies;
2206 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2207 queue_work(osdc->notify_wq, &lwork->work);
2210 static void do_watch_notify(struct work_struct *w)
2212 struct linger_work *lwork = container_of(w, struct linger_work, work);
2213 struct ceph_osd_linger_request *lreq = lwork->lreq;
2215 if (!linger_registered(lreq)) {
2216 dout("%s lreq %p not registered\n", __func__, lreq);
2220 WARN_ON(!lreq->is_watch);
2221 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2222 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2223 lwork->notify.payload_len);
2224 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2225 lwork->notify.notifier_id, lwork->notify.payload,
2226 lwork->notify.payload_len);
2229 ceph_msg_put(lwork->notify.msg);
2233 static void do_watch_error(struct work_struct *w)
2235 struct linger_work *lwork = container_of(w, struct linger_work, work);
2236 struct ceph_osd_linger_request *lreq = lwork->lreq;
2238 if (!linger_registered(lreq)) {
2239 dout("%s lreq %p not registered\n", __func__, lreq);
2243 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2244 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2250 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2252 struct linger_work *lwork;
2254 lwork = lwork_alloc(lreq, do_watch_error);
2256 pr_err("failed to allocate error-lwork\n");
2260 lwork->error.err = lreq->last_error;
2264 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2267 if (!completion_done(&lreq->reg_commit_wait)) {
2268 lreq->reg_commit_error = (result <= 0 ? result : 0);
2269 complete_all(&lreq->reg_commit_wait);
2273 static void linger_commit_cb(struct ceph_osd_request *req)
2275 struct ceph_osd_linger_request *lreq = req->r_priv;
2277 mutex_lock(&lreq->lock);
2278 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2279 lreq->linger_id, req->r_result);
2280 linger_reg_commit_complete(lreq, req->r_result);
2281 lreq->committed = true;
2283 if (!lreq->is_watch) {
2284 struct ceph_osd_data *osd_data =
2285 osd_req_op_data(req, 0, notify, response_data);
2286 void *p = page_address(osd_data->pages[0]);
2288 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2289 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2291 /* make note of the notify_id */
2292 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2293 lreq->notify_id = ceph_decode_64(&p);
2294 dout("lreq %p notify_id %llu\n", lreq,
2297 dout("lreq %p no notify_id\n", lreq);
2301 mutex_unlock(&lreq->lock);
2305 static int normalize_watch_error(int err)
2308 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2309 * notification and a failure to reconnect because we raced with
2310 * the delete appear the same to the user.
2318 static void linger_reconnect_cb(struct ceph_osd_request *req)
2320 struct ceph_osd_linger_request *lreq = req->r_priv;
2322 mutex_lock(&lreq->lock);
2323 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2324 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2325 if (req->r_result < 0) {
2326 if (!lreq->last_error) {
2327 lreq->last_error = normalize_watch_error(req->r_result);
2328 queue_watch_error(lreq);
2332 mutex_unlock(&lreq->lock);
2336 static void send_linger(struct ceph_osd_linger_request *lreq)
2338 struct ceph_osd_request *req = lreq->reg_req;
2339 struct ceph_osd_req_op *op = &req->r_ops[0];
2341 verify_osdc_wrlocked(req->r_osdc);
2342 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2345 cancel_linger_request(req);
2347 request_reinit(req);
2348 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2349 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2350 req->r_flags = lreq->t.flags;
2351 req->r_mtime = lreq->mtime;
2353 mutex_lock(&lreq->lock);
2354 if (lreq->is_watch && lreq->committed) {
2355 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2356 op->watch.cookie != lreq->linger_id);
2357 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2358 op->watch.gen = ++lreq->register_gen;
2359 dout("lreq %p reconnect register_gen %u\n", lreq,
2361 req->r_callback = linger_reconnect_cb;
2363 if (!lreq->is_watch)
2364 lreq->notify_id = 0;
2366 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2367 dout("lreq %p register\n", lreq);
2368 req->r_callback = linger_commit_cb;
2370 mutex_unlock(&lreq->lock);
2372 req->r_priv = linger_get(lreq);
2373 req->r_linger = true;
2375 submit_request(req, true);
2378 static void linger_ping_cb(struct ceph_osd_request *req)
2380 struct ceph_osd_linger_request *lreq = req->r_priv;
2382 mutex_lock(&lreq->lock);
2383 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2384 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2386 if (lreq->register_gen == req->r_ops[0].watch.gen) {
2387 if (!req->r_result) {
2388 lreq->watch_valid_thru = lreq->ping_sent;
2389 } else if (!lreq->last_error) {
2390 lreq->last_error = normalize_watch_error(req->r_result);
2391 queue_watch_error(lreq);
2394 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2395 lreq->register_gen, req->r_ops[0].watch.gen);
2398 mutex_unlock(&lreq->lock);
2402 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2404 struct ceph_osd_client *osdc = lreq->osdc;
2405 struct ceph_osd_request *req = lreq->ping_req;
2406 struct ceph_osd_req_op *op = &req->r_ops[0];
2408 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2409 dout("%s PAUSERD\n", __func__);
2413 lreq->ping_sent = jiffies;
2414 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2415 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2416 lreq->register_gen);
2419 cancel_linger_request(req);
2421 request_reinit(req);
2422 target_copy(&req->r_t, &lreq->t);
2424 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2425 op->watch.cookie != lreq->linger_id ||
2426 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2427 op->watch.gen = lreq->register_gen;
2428 req->r_callback = linger_ping_cb;
2429 req->r_priv = linger_get(lreq);
2430 req->r_linger = true;
2432 ceph_osdc_get_request(req);
2433 account_request(req);
2434 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2435 link_request(lreq->osd, req);
2439 static void linger_submit(struct ceph_osd_linger_request *lreq)
2441 struct ceph_osd_client *osdc = lreq->osdc;
2442 struct ceph_osd *osd;
2444 calc_target(osdc, &lreq->t, &lreq->last_force_resend, false);
2445 osd = lookup_create_osd(osdc, lreq->t.osd, true);
2446 link_linger(osd, lreq);
2451 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2453 struct ceph_osd_client *osdc = lreq->osdc;
2454 struct ceph_osd_linger_request *lookup_lreq;
2456 verify_osdc_wrlocked(osdc);
2458 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2463 WARN_ON(lookup_lreq != lreq);
2464 erase_linger_mc(&osdc->linger_map_checks, lreq);
2469 * @lreq has to be both registered and linked.
2471 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2473 if (lreq->is_watch && lreq->ping_req->r_osd)
2474 cancel_linger_request(lreq->ping_req);
2475 if (lreq->reg_req->r_osd)
2476 cancel_linger_request(lreq->reg_req);
2477 cancel_linger_map_check(lreq);
2478 unlink_linger(lreq->osd, lreq);
2479 linger_unregister(lreq);
2482 static void linger_cancel(struct ceph_osd_linger_request *lreq)
2484 struct ceph_osd_client *osdc = lreq->osdc;
2486 down_write(&osdc->lock);
2487 if (__linger_registered(lreq))
2488 __linger_cancel(lreq);
2489 up_write(&osdc->lock);
2492 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2494 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2496 struct ceph_osd_client *osdc = lreq->osdc;
2497 struct ceph_osdmap *map = osdc->osdmap;
2499 verify_osdc_wrlocked(osdc);
2500 WARN_ON(!map->epoch);
2502 if (lreq->register_gen) {
2503 lreq->map_dne_bound = map->epoch;
2504 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2505 lreq, lreq->linger_id);
2507 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2508 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2512 if (lreq->map_dne_bound) {
2513 if (map->epoch >= lreq->map_dne_bound) {
2514 /* we had a new enough map */
2515 pr_info("linger_id %llu pool does not exist\n",
2517 linger_reg_commit_complete(lreq, -ENOENT);
2518 __linger_cancel(lreq);
2521 send_linger_map_check(lreq);
2525 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2527 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2528 struct ceph_osd_linger_request *lreq;
2529 u64 linger_id = greq->private_data;
2531 WARN_ON(greq->result || !greq->u.newest);
2533 down_write(&osdc->lock);
2534 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2536 dout("%s linger_id %llu dne\n", __func__, linger_id);
2540 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
2541 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2543 if (!lreq->map_dne_bound)
2544 lreq->map_dne_bound = greq->u.newest;
2545 erase_linger_mc(&osdc->linger_map_checks, lreq);
2546 check_linger_pool_dne(lreq);
2550 up_write(&osdc->lock);
2553 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
2555 struct ceph_osd_client *osdc = lreq->osdc;
2556 struct ceph_osd_linger_request *lookup_lreq;
2559 verify_osdc_wrlocked(osdc);
2561 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2564 WARN_ON(lookup_lreq != lreq);
2569 insert_linger_mc(&osdc->linger_map_checks, lreq);
2570 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2571 linger_map_check_cb, lreq->linger_id);
2575 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
2579 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2580 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
2581 return ret ?: lreq->reg_commit_error;
2584 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
2588 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2589 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
2590 return ret ?: lreq->notify_finish_error;
2594 * Timeout callback, called every N seconds. When 1 or more OSD
2595 * requests has been active for more than N seconds, we send a keepalive
2596 * (tag + timestamp) to its OSD to ensure any communications channel
2597 * reset is detected.
2599 static void handle_timeout(struct work_struct *work)
2601 struct ceph_osd_client *osdc =
2602 container_of(work, struct ceph_osd_client, timeout_work.work);
2603 struct ceph_options *opts = osdc->client->options;
2604 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
2605 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
2606 LIST_HEAD(slow_osds);
2607 struct rb_node *n, *p;
2609 dout("%s osdc %p\n", __func__, osdc);
2610 down_write(&osdc->lock);
2613 * ping osds that are a bit slow. this ensures that if there
2614 * is a break in the TCP connection we will notice, and reopen
2615 * a connection with that osd (from the fault callback).
2617 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2618 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2621 for (p = rb_first(&osd->o_requests); p; ) {
2622 struct ceph_osd_request *req =
2623 rb_entry(p, struct ceph_osd_request, r_node);
2625 p = rb_next(p); /* abort_request() */
2627 if (time_before(req->r_stamp, cutoff)) {
2628 dout(" req %p tid %llu on osd%d is laggy\n",
2629 req, req->r_tid, osd->o_osd);
2632 if (opts->osd_request_timeout &&
2633 time_before(req->r_start_stamp, expiry_cutoff)) {
2634 pr_err_ratelimited("tid %llu on osd%d timeout\n",
2635 req->r_tid, osd->o_osd);
2636 abort_request(req, -ETIMEDOUT);
2639 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
2640 struct ceph_osd_linger_request *lreq =
2641 rb_entry(p, struct ceph_osd_linger_request, node);
2643 dout(" lreq %p linger_id %llu is served by osd%d\n",
2644 lreq, lreq->linger_id, osd->o_osd);
2647 mutex_lock(&lreq->lock);
2648 if (lreq->is_watch && lreq->committed && !lreq->last_error)
2649 send_linger_ping(lreq);
2650 mutex_unlock(&lreq->lock);
2654 list_move_tail(&osd->o_keepalive_item, &slow_osds);
2657 if (opts->osd_request_timeout) {
2658 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
2659 struct ceph_osd_request *req =
2660 rb_entry(p, struct ceph_osd_request, r_node);
2662 p = rb_next(p); /* abort_request() */
2664 if (time_before(req->r_start_stamp, expiry_cutoff)) {
2665 pr_err_ratelimited("tid %llu on osd%d timeout\n",
2666 req->r_tid, osdc->homeless_osd.o_osd);
2667 abort_request(req, -ETIMEDOUT);
2672 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
2673 maybe_request_map(osdc);
2675 while (!list_empty(&slow_osds)) {
2676 struct ceph_osd *osd = list_first_entry(&slow_osds,
2679 list_del_init(&osd->o_keepalive_item);
2680 ceph_con_keepalive(&osd->o_con);
2683 up_write(&osdc->lock);
2684 schedule_delayed_work(&osdc->timeout_work,
2685 osdc->client->options->osd_keepalive_timeout);
2688 static void handle_osds_timeout(struct work_struct *work)
2690 struct ceph_osd_client *osdc =
2691 container_of(work, struct ceph_osd_client,
2692 osds_timeout_work.work);
2693 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
2694 struct ceph_osd *osd, *nosd;
2696 dout("%s osdc %p\n", __func__, osdc);
2697 down_write(&osdc->lock);
2698 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
2699 if (time_before(jiffies, osd->lru_ttl))
2702 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
2703 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
2707 up_write(&osdc->lock);
2708 schedule_delayed_work(&osdc->osds_timeout_work,
2709 round_jiffies_relative(delay));
2712 static int ceph_oloc_decode(void **p, void *end,
2713 struct ceph_object_locator *oloc)
2715 u8 struct_v, struct_cv;
2720 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2721 struct_v = ceph_decode_8(p);
2722 struct_cv = ceph_decode_8(p);
2724 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
2725 struct_v, struct_cv);
2728 if (struct_cv > 6) {
2729 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
2730 struct_v, struct_cv);
2733 len = ceph_decode_32(p);
2734 ceph_decode_need(p, end, len, e_inval);
2735 struct_end = *p + len;
2737 oloc->pool = ceph_decode_64(p);
2738 *p += 4; /* skip preferred */
2740 len = ceph_decode_32(p);
2742 pr_warn("ceph_object_locator::key is set\n");
2746 if (struct_v >= 5) {
2747 bool changed = false;
2749 len = ceph_decode_32(p);
2751 ceph_decode_need(p, end, len, e_inval);
2752 if (!oloc->pool_ns ||
2753 ceph_compare_string(oloc->pool_ns, *p, len))
2761 /* redirect changes namespace */
2762 pr_warn("ceph_object_locator::nspace is changed\n");
2767 if (struct_v >= 6) {
2768 s64 hash = ceph_decode_64(p);
2770 pr_warn("ceph_object_locator::hash is set\n");
2785 static int ceph_redirect_decode(void **p, void *end,
2786 struct ceph_request_redirect *redir)
2788 u8 struct_v, struct_cv;
2793 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2794 struct_v = ceph_decode_8(p);
2795 struct_cv = ceph_decode_8(p);
2796 if (struct_cv > 1) {
2797 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
2798 struct_v, struct_cv);
2801 len = ceph_decode_32(p);
2802 ceph_decode_need(p, end, len, e_inval);
2803 struct_end = *p + len;
2805 ret = ceph_oloc_decode(p, end, &redir->oloc);
2809 len = ceph_decode_32(p);
2811 pr_warn("ceph_request_redirect::object_name is set\n");
2815 len = ceph_decode_32(p);
2816 *p += len; /* skip osd_instructions */
2828 struct MOSDOpReply {
2829 struct ceph_pg pgid;
2834 u32 outdata_len[CEPH_OSD_MAX_OPS];
2835 s32 rval[CEPH_OSD_MAX_OPS];
2837 struct ceph_eversion replay_version;
2839 struct ceph_request_redirect redirect;
2842 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
2844 void *p = msg->front.iov_base;
2845 void *const end = p + msg->front.iov_len;
2846 u16 version = le16_to_cpu(msg->hdr.version);
2847 struct ceph_eversion bad_replay_version;
2853 ceph_decode_32_safe(&p, end, len, e_inval);
2854 ceph_decode_need(&p, end, len, e_inval);
2855 p += len; /* skip oid */
2857 ret = ceph_decode_pgid(&p, end, &m->pgid);
2861 ceph_decode_64_safe(&p, end, m->flags, e_inval);
2862 ceph_decode_32_safe(&p, end, m->result, e_inval);
2863 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
2864 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
2865 p += sizeof(bad_replay_version);
2866 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
2868 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
2869 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
2872 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
2874 for (i = 0; i < m->num_ops; i++) {
2875 struct ceph_osd_op *op = p;
2877 m->outdata_len[i] = le32_to_cpu(op->payload_len);
2881 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
2882 for (i = 0; i < m->num_ops; i++)
2883 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
2886 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
2887 memcpy(&m->replay_version, p, sizeof(m->replay_version));
2888 p += sizeof(m->replay_version);
2889 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
2891 m->replay_version = bad_replay_version; /* struct */
2892 m->user_version = le64_to_cpu(m->replay_version.version);
2897 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
2905 ret = ceph_redirect_decode(&p, end, &m->redirect);
2909 ceph_oloc_init(&m->redirect.oloc);
2919 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
2922 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
2924 struct ceph_osd_client *osdc = osd->o_osdc;
2925 struct ceph_osd_request *req;
2926 struct MOSDOpReply m;
2927 u64 tid = le64_to_cpu(msg->hdr.tid);
2932 dout("%s msg %p tid %llu\n", __func__, msg, tid);
2934 down_read(&osdc->lock);
2935 if (!osd_registered(osd)) {
2936 dout("%s osd%d unknown\n", __func__, osd->o_osd);
2937 goto out_unlock_osdc;
2939 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
2941 mutex_lock(&osd->lock);
2942 req = lookup_request(&osd->o_requests, tid);
2944 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
2945 goto out_unlock_session;
2948 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
2949 ret = decode_MOSDOpReply(msg, &m);
2950 m.redirect.oloc.pool_ns = NULL;
2952 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
2957 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
2958 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
2959 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
2960 le64_to_cpu(m.replay_version.version), m.user_version);
2962 if (m.retry_attempt >= 0) {
2963 if (m.retry_attempt != req->r_attempts - 1) {
2964 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
2965 req, req->r_tid, m.retry_attempt,
2966 req->r_attempts - 1);
2967 goto out_unlock_session;
2970 WARN_ON(1); /* MOSDOpReply v4 is assumed */
2973 if (!ceph_oloc_empty(&m.redirect.oloc)) {
2974 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
2975 m.redirect.oloc.pool);
2976 unlink_request(osd, req);
2977 mutex_unlock(&osd->lock);
2980 * Not ceph_oloc_copy() - changing pool_ns is not
2983 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
2984 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
2986 __submit_request(req, false);
2987 goto out_unlock_osdc;
2990 if (m.num_ops != req->r_num_ops) {
2991 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
2992 req->r_num_ops, req->r_tid);
2995 for (i = 0; i < req->r_num_ops; i++) {
2996 dout(" req %p tid %llu op %d rval %d len %u\n", req,
2997 req->r_tid, i, m.rval[i], m.outdata_len[i]);
2998 req->r_ops[i].rval = m.rval[i];
2999 req->r_ops[i].outdata_len = m.outdata_len[i];
3000 data_len += m.outdata_len[i];
3002 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3003 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3004 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3007 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3008 req, req->r_tid, m.result, data_len);
3011 * Since we only ever request ONDISK, we should only ever get
3012 * one (type of) reply back.
3014 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3015 req->r_result = m.result ?: data_len;
3016 finish_request(req);
3017 mutex_unlock(&osd->lock);
3018 up_read(&osdc->lock);
3020 __complete_request(req);
3021 complete_all(&req->r_completion);
3022 ceph_osdc_put_request(req);
3026 complete_request(req, -EIO);
3028 mutex_unlock(&osd->lock);
3030 up_read(&osdc->lock);
3033 static void set_pool_was_full(struct ceph_osd_client *osdc)
3037 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3038 struct ceph_pg_pool_info *pi =
3039 rb_entry(n, struct ceph_pg_pool_info, node);
3041 pi->was_full = __pool_full(pi);
3045 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
3047 struct ceph_pg_pool_info *pi;
3049 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3053 return pi->was_full && !__pool_full(pi);
3056 static enum calc_target_result
3057 recalc_linger_target(struct ceph_osd_linger_request *lreq)
3059 struct ceph_osd_client *osdc = lreq->osdc;
3060 enum calc_target_result ct_res;
3062 ct_res = calc_target(osdc, &lreq->t, &lreq->last_force_resend, true);
3063 if (ct_res == CALC_TARGET_NEED_RESEND) {
3064 struct ceph_osd *osd;
3066 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3067 if (osd != lreq->osd) {
3068 unlink_linger(lreq->osd, lreq);
3069 link_linger(osd, lreq);
3077 * Requeue requests whose mapping to an OSD has changed.
3079 static void scan_requests(struct ceph_osd *osd,
3082 bool check_pool_cleared_full,
3083 struct rb_root *need_resend,
3084 struct list_head *need_resend_linger)
3086 struct ceph_osd_client *osdc = osd->o_osdc;
3088 bool force_resend_writes;
3090 for (n = rb_first(&osd->o_linger_requests); n; ) {
3091 struct ceph_osd_linger_request *lreq =
3092 rb_entry(n, struct ceph_osd_linger_request, node);
3093 enum calc_target_result ct_res;
3095 n = rb_next(n); /* recalc_linger_target() */
3097 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3099 ct_res = recalc_linger_target(lreq);
3101 case CALC_TARGET_NO_ACTION:
3102 force_resend_writes = cleared_full ||
3103 (check_pool_cleared_full &&
3104 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3105 if (!force_resend && !force_resend_writes)
3109 case CALC_TARGET_NEED_RESEND:
3110 cancel_linger_map_check(lreq);
3112 * scan_requests() for the previous epoch(s)
3113 * may have already added it to the list, since
3114 * it's not unlinked here.
3116 if (list_empty(&lreq->scan_item))
3117 list_add_tail(&lreq->scan_item, need_resend_linger);
3119 case CALC_TARGET_POOL_DNE:
3120 check_linger_pool_dne(lreq);
3125 for (n = rb_first(&osd->o_requests); n; ) {
3126 struct ceph_osd_request *req =
3127 rb_entry(n, struct ceph_osd_request, r_node);
3128 enum calc_target_result ct_res;
3130 n = rb_next(n); /* unlink_request(), check_pool_dne() */
3132 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3133 ct_res = calc_target(osdc, &req->r_t,
3134 &req->r_last_force_resend, false);
3136 case CALC_TARGET_NO_ACTION:
3137 force_resend_writes = cleared_full ||
3138 (check_pool_cleared_full &&
3139 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3140 if (!force_resend &&
3141 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3142 !force_resend_writes))
3146 case CALC_TARGET_NEED_RESEND:
3147 cancel_map_check(req);
3148 unlink_request(osd, req);
3149 insert_request(need_resend, req);
3151 case CALC_TARGET_POOL_DNE:
3152 check_pool_dne(req);
3158 static int handle_one_map(struct ceph_osd_client *osdc,
3159 void *p, void *end, bool incremental,
3160 struct rb_root *need_resend,
3161 struct list_head *need_resend_linger)
3163 struct ceph_osdmap *newmap;
3165 bool skipped_map = false;
3168 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3169 set_pool_was_full(osdc);
3172 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3174 newmap = ceph_osdmap_decode(&p, end);
3176 return PTR_ERR(newmap);
3178 if (newmap != osdc->osdmap) {
3180 * Preserve ->was_full before destroying the old map.
3181 * For pools that weren't in the old map, ->was_full
3184 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3185 struct ceph_pg_pool_info *pi =
3186 rb_entry(n, struct ceph_pg_pool_info, node);
3187 struct ceph_pg_pool_info *old_pi;
3189 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3191 pi->was_full = old_pi->was_full;
3193 WARN_ON(pi->was_full);
3196 if (osdc->osdmap->epoch &&
3197 osdc->osdmap->epoch + 1 < newmap->epoch) {
3198 WARN_ON(incremental);
3202 ceph_osdmap_destroy(osdc->osdmap);
3203 osdc->osdmap = newmap;
3206 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3207 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3208 need_resend, need_resend_linger);
3210 for (n = rb_first(&osdc->osds); n; ) {
3211 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3213 n = rb_next(n); /* close_osd() */
3215 scan_requests(osd, skipped_map, was_full, true, need_resend,
3216 need_resend_linger);
3217 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3218 memcmp(&osd->o_con.peer_addr,
3219 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3220 sizeof(struct ceph_entity_addr)))
3227 static void kick_requests(struct ceph_osd_client *osdc,
3228 struct rb_root *need_resend,
3229 struct list_head *need_resend_linger)
3231 struct ceph_osd_linger_request *lreq, *nlreq;
3234 for (n = rb_first(need_resend); n; ) {
3235 struct ceph_osd_request *req =
3236 rb_entry(n, struct ceph_osd_request, r_node);
3237 struct ceph_osd *osd;
3240 erase_request(need_resend, req); /* before link_request() */
3242 WARN_ON(req->r_osd);
3243 calc_target(osdc, &req->r_t, NULL, false);
3244 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3245 link_request(osd, req);
3246 if (!req->r_linger) {
3247 if (!osd_homeless(osd) && !req->r_t.paused)
3250 cancel_linger_request(req);
3254 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3255 if (!osd_homeless(lreq->osd))
3258 list_del_init(&lreq->scan_item);
3263 * Process updated osd map.
3265 * The message contains any number of incremental and full maps, normally
3266 * indicating some sort of topology change in the cluster. Kick requests
3267 * off to different OSDs as needed.
3269 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3271 void *p = msg->front.iov_base;
3272 void *const end = p + msg->front.iov_len;
3273 u32 nr_maps, maplen;
3275 struct ceph_fsid fsid;
3276 struct rb_root need_resend = RB_ROOT;
3277 LIST_HEAD(need_resend_linger);
3278 bool handled_incremental = false;
3279 bool was_pauserd, was_pausewr;
3280 bool pauserd, pausewr;
3283 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
3284 down_write(&osdc->lock);
3287 ceph_decode_need(&p, end, sizeof(fsid), bad);
3288 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3289 if (ceph_check_fsid(osdc->client, &fsid) < 0)
3292 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3293 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3294 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3295 have_pool_full(osdc);
3297 /* incremental maps */
3298 ceph_decode_32_safe(&p, end, nr_maps, bad);
3299 dout(" %d inc maps\n", nr_maps);
3300 while (nr_maps > 0) {
3301 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3302 epoch = ceph_decode_32(&p);
3303 maplen = ceph_decode_32(&p);
3304 ceph_decode_need(&p, end, maplen, bad);
3305 if (osdc->osdmap->epoch &&
3306 osdc->osdmap->epoch + 1 == epoch) {
3307 dout("applying incremental map %u len %d\n",
3309 err = handle_one_map(osdc, p, p + maplen, true,
3310 &need_resend, &need_resend_linger);
3313 handled_incremental = true;
3315 dout("ignoring incremental map %u len %d\n",
3321 if (handled_incremental)
3325 ceph_decode_32_safe(&p, end, nr_maps, bad);
3326 dout(" %d full maps\n", nr_maps);
3328 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3329 epoch = ceph_decode_32(&p);
3330 maplen = ceph_decode_32(&p);
3331 ceph_decode_need(&p, end, maplen, bad);
3333 dout("skipping non-latest full map %u len %d\n",
3335 } else if (osdc->osdmap->epoch >= epoch) {
3336 dout("skipping full map %u len %d, "
3337 "older than our %u\n", epoch, maplen,
3338 osdc->osdmap->epoch);
3340 dout("taking full map %u len %d\n", epoch, maplen);
3341 err = handle_one_map(osdc, p, p + maplen, false,
3342 &need_resend, &need_resend_linger);
3352 * subscribe to subsequent osdmap updates if full to ensure
3353 * we find out when we are no longer full and stop returning
3356 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3357 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3358 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3359 have_pool_full(osdc);
3360 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3361 osdc->osdmap->epoch < osdc->epoch_barrier)
3362 maybe_request_map(osdc);
3364 kick_requests(osdc, &need_resend, &need_resend_linger);
3366 ceph_osdc_abort_on_full(osdc);
3367 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3368 osdc->osdmap->epoch);
3369 up_write(&osdc->lock);
3370 wake_up_all(&osdc->client->auth_wq);
3374 pr_err("osdc handle_map corrupt msg\n");
3376 up_write(&osdc->lock);
3380 * Resubmit requests pending on the given osd.
3382 static void kick_osd_requests(struct ceph_osd *osd)
3386 for (n = rb_first(&osd->o_requests); n; ) {
3387 struct ceph_osd_request *req =
3388 rb_entry(n, struct ceph_osd_request, r_node);
3390 n = rb_next(n); /* cancel_linger_request() */
3392 if (!req->r_linger) {
3393 if (!req->r_t.paused)
3396 cancel_linger_request(req);
3399 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3400 struct ceph_osd_linger_request *lreq =
3401 rb_entry(n, struct ceph_osd_linger_request, node);
3408 * If the osd connection drops, we need to resubmit all requests.
3410 static void osd_fault(struct ceph_connection *con)
3412 struct ceph_osd *osd = con->private;
3413 struct ceph_osd_client *osdc = osd->o_osdc;
3415 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3417 down_write(&osdc->lock);
3418 if (!osd_registered(osd)) {
3419 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3423 if (!reopen_osd(osd))
3424 kick_osd_requests(osd);
3425 maybe_request_map(osdc);
3428 up_write(&osdc->lock);
3432 * Process osd watch notifications
3434 static void handle_watch_notify(struct ceph_osd_client *osdc,
3435 struct ceph_msg *msg)
3437 void *p = msg->front.iov_base;
3438 void *const end = p + msg->front.iov_len;
3439 struct ceph_osd_linger_request *lreq;
3440 struct linger_work *lwork;
3441 u8 proto_ver, opcode;
3442 u64 cookie, notify_id;
3443 u64 notifier_id = 0;
3444 s32 return_code = 0;
3445 void *payload = NULL;
3446 u32 payload_len = 0;
3448 ceph_decode_8_safe(&p, end, proto_ver, bad);
3449 ceph_decode_8_safe(&p, end, opcode, bad);
3450 ceph_decode_64_safe(&p, end, cookie, bad);
3451 p += 8; /* skip ver */
3452 ceph_decode_64_safe(&p, end, notify_id, bad);
3454 if (proto_ver >= 1) {
3455 ceph_decode_32_safe(&p, end, payload_len, bad);
3456 ceph_decode_need(&p, end, payload_len, bad);
3461 if (le16_to_cpu(msg->hdr.version) >= 2)
3462 ceph_decode_32_safe(&p, end, return_code, bad);
3464 if (le16_to_cpu(msg->hdr.version) >= 3)
3465 ceph_decode_64_safe(&p, end, notifier_id, bad);
3467 down_read(&osdc->lock);
3468 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
3470 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
3472 goto out_unlock_osdc;
3475 mutex_lock(&lreq->lock);
3476 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
3477 opcode, cookie, lreq, lreq->is_watch);
3478 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
3479 if (!lreq->last_error) {
3480 lreq->last_error = -ENOTCONN;
3481 queue_watch_error(lreq);
3483 } else if (!lreq->is_watch) {
3484 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
3485 if (lreq->notify_id && lreq->notify_id != notify_id) {
3486 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
3487 lreq->notify_id, notify_id);
3488 } else if (!completion_done(&lreq->notify_finish_wait)) {
3489 struct ceph_msg_data *data =
3490 list_first_entry_or_null(&msg->data,
3491 struct ceph_msg_data,
3495 if (lreq->preply_pages) {
3496 WARN_ON(data->type !=
3497 CEPH_MSG_DATA_PAGES);
3498 *lreq->preply_pages = data->pages;
3499 *lreq->preply_len = data->length;
3501 ceph_release_page_vector(data->pages,
3502 calc_pages_for(0, data->length));
3505 lreq->notify_finish_error = return_code;
3506 complete_all(&lreq->notify_finish_wait);
3509 /* CEPH_WATCH_EVENT_NOTIFY */
3510 lwork = lwork_alloc(lreq, do_watch_notify);
3512 pr_err("failed to allocate notify-lwork\n");
3513 goto out_unlock_lreq;
3516 lwork->notify.notify_id = notify_id;
3517 lwork->notify.notifier_id = notifier_id;
3518 lwork->notify.payload = payload;
3519 lwork->notify.payload_len = payload_len;
3520 lwork->notify.msg = ceph_msg_get(msg);
3525 mutex_unlock(&lreq->lock);
3527 up_read(&osdc->lock);
3531 pr_err("osdc handle_watch_notify corrupt msg\n");
3535 * Register request, send initial attempt.
3537 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
3538 struct ceph_osd_request *req,
3541 down_read(&osdc->lock);
3542 submit_request(req, false);
3543 up_read(&osdc->lock);
3547 EXPORT_SYMBOL(ceph_osdc_start_request);
3550 * Unregister a registered request. The request is not completed:
3551 * ->r_result isn't set and __complete_request() isn't called.
3553 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
3555 struct ceph_osd_client *osdc = req->r_osdc;
3557 down_write(&osdc->lock);
3559 cancel_request(req);
3560 up_write(&osdc->lock);
3562 EXPORT_SYMBOL(ceph_osdc_cancel_request);
3565 * @timeout: in jiffies, 0 means "wait forever"
3567 static int wait_request_timeout(struct ceph_osd_request *req,
3568 unsigned long timeout)
3572 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3573 left = wait_for_completion_killable_timeout(&req->r_completion,
3574 ceph_timeout_jiffies(timeout));
3576 left = left ?: -ETIMEDOUT;
3577 ceph_osdc_cancel_request(req);
3579 left = req->r_result; /* completed */
3586 * wait for a request to complete
3588 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
3589 struct ceph_osd_request *req)
3591 return wait_request_timeout(req, 0);
3593 EXPORT_SYMBOL(ceph_osdc_wait_request);
3596 * sync - wait for all in-flight requests to flush. avoid starvation.
3598 void ceph_osdc_sync(struct ceph_osd_client *osdc)
3600 struct rb_node *n, *p;
3601 u64 last_tid = atomic64_read(&osdc->last_tid);
3604 down_read(&osdc->lock);
3605 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3606 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3608 mutex_lock(&osd->lock);
3609 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
3610 struct ceph_osd_request *req =
3611 rb_entry(p, struct ceph_osd_request, r_node);
3613 if (req->r_tid > last_tid)
3616 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
3619 ceph_osdc_get_request(req);
3620 mutex_unlock(&osd->lock);
3621 up_read(&osdc->lock);
3622 dout("%s waiting on req %p tid %llu last_tid %llu\n",
3623 __func__, req, req->r_tid, last_tid);
3624 wait_for_completion(&req->r_completion);
3625 ceph_osdc_put_request(req);
3629 mutex_unlock(&osd->lock);
3632 up_read(&osdc->lock);
3633 dout("%s done last_tid %llu\n", __func__, last_tid);
3635 EXPORT_SYMBOL(ceph_osdc_sync);
3637 static struct ceph_osd_request *
3638 alloc_linger_request(struct ceph_osd_linger_request *lreq)
3640 struct ceph_osd_request *req;
3642 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
3646 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3647 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3649 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
3650 ceph_osdc_put_request(req);
3658 * Returns a handle, caller owns a ref.
3660 struct ceph_osd_linger_request *
3661 ceph_osdc_watch(struct ceph_osd_client *osdc,
3662 struct ceph_object_id *oid,
3663 struct ceph_object_locator *oloc,
3664 rados_watchcb2_t wcb,
3665 rados_watcherrcb_t errcb,
3668 struct ceph_osd_linger_request *lreq;
3671 lreq = linger_alloc(osdc);
3673 return ERR_PTR(-ENOMEM);
3675 lreq->is_watch = true;
3677 lreq->errcb = errcb;
3679 lreq->watch_valid_thru = jiffies;
3681 ceph_oid_copy(&lreq->t.base_oid, oid);
3682 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3683 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
3684 ktime_get_real_ts(&lreq->mtime);
3686 lreq->reg_req = alloc_linger_request(lreq);
3687 if (!lreq->reg_req) {
3692 lreq->ping_req = alloc_linger_request(lreq);
3693 if (!lreq->ping_req) {
3698 down_write(&osdc->lock);
3699 linger_register(lreq); /* before osd_req_op_* */
3700 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
3701 CEPH_OSD_WATCH_OP_WATCH);
3702 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
3703 CEPH_OSD_WATCH_OP_PING);
3704 linger_submit(lreq);
3705 up_write(&osdc->lock);
3707 ret = linger_reg_commit_wait(lreq);
3709 linger_cancel(lreq);
3717 return ERR_PTR(ret);
3719 EXPORT_SYMBOL(ceph_osdc_watch);
3724 * Times out after mount_timeout to preserve rbd unmap behaviour
3725 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
3726 * with mount_timeout").
3728 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
3729 struct ceph_osd_linger_request *lreq)
3731 struct ceph_options *opts = osdc->client->options;
3732 struct ceph_osd_request *req;
3735 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3739 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3740 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3741 req->r_flags = CEPH_OSD_FLAG_WRITE;
3742 ktime_get_real_ts(&req->r_mtime);
3743 osd_req_op_watch_init(req, 0, lreq->linger_id,
3744 CEPH_OSD_WATCH_OP_UNWATCH);
3746 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3750 ceph_osdc_start_request(osdc, req, false);
3751 linger_cancel(lreq);
3753 ret = wait_request_timeout(req, opts->mount_timeout);
3756 ceph_osdc_put_request(req);
3759 EXPORT_SYMBOL(ceph_osdc_unwatch);
3761 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
3762 u64 notify_id, u64 cookie, void *payload,
3765 struct ceph_osd_req_op *op;
3766 struct ceph_pagelist *pl;
3769 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
3771 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3775 ceph_pagelist_init(pl);
3776 ret = ceph_pagelist_encode_64(pl, notify_id);
3777 ret |= ceph_pagelist_encode_64(pl, cookie);
3779 ret |= ceph_pagelist_encode_32(pl, payload_len);
3780 ret |= ceph_pagelist_append(pl, payload, payload_len);
3782 ret |= ceph_pagelist_encode_32(pl, 0);
3785 ceph_pagelist_release(pl);
3789 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
3790 op->indata_len = pl->length;
3794 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
3795 struct ceph_object_id *oid,
3796 struct ceph_object_locator *oloc,
3802 struct ceph_osd_request *req;
3805 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3809 ceph_oid_copy(&req->r_base_oid, oid);
3810 ceph_oloc_copy(&req->r_base_oloc, oloc);
3811 req->r_flags = CEPH_OSD_FLAG_READ;
3813 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3817 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
3822 ceph_osdc_start_request(osdc, req, false);
3823 ret = ceph_osdc_wait_request(osdc, req);
3826 ceph_osdc_put_request(req);
3829 EXPORT_SYMBOL(ceph_osdc_notify_ack);
3831 static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
3832 u64 cookie, u32 prot_ver, u32 timeout,
3833 void *payload, size_t payload_len)
3835 struct ceph_osd_req_op *op;
3836 struct ceph_pagelist *pl;
3839 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
3840 op->notify.cookie = cookie;
3842 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3846 ceph_pagelist_init(pl);
3847 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
3848 ret |= ceph_pagelist_encode_32(pl, timeout);
3849 ret |= ceph_pagelist_encode_32(pl, payload_len);
3850 ret |= ceph_pagelist_append(pl, payload, payload_len);
3852 ceph_pagelist_release(pl);
3856 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
3857 op->indata_len = pl->length;
3862 * @timeout: in seconds
3864 * @preply_{pages,len} are initialized both on success and error.
3865 * The caller is responsible for:
3867 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
3869 int ceph_osdc_notify(struct ceph_osd_client *osdc,
3870 struct ceph_object_id *oid,
3871 struct ceph_object_locator *oloc,
3875 struct page ***preply_pages,
3878 struct ceph_osd_linger_request *lreq;
3879 struct page **pages;
3884 *preply_pages = NULL;
3888 lreq = linger_alloc(osdc);
3892 lreq->preply_pages = preply_pages;
3893 lreq->preply_len = preply_len;
3895 ceph_oid_copy(&lreq->t.base_oid, oid);
3896 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3897 lreq->t.flags = CEPH_OSD_FLAG_READ;
3899 lreq->reg_req = alloc_linger_request(lreq);
3900 if (!lreq->reg_req) {
3906 pages = ceph_alloc_page_vector(1, GFP_NOIO);
3907 if (IS_ERR(pages)) {
3908 ret = PTR_ERR(pages);
3912 down_write(&osdc->lock);
3913 linger_register(lreq); /* before osd_req_op_* */
3914 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
3915 timeout, payload, payload_len);
3917 linger_unregister(lreq);
3918 up_write(&osdc->lock);
3919 ceph_release_page_vector(pages, 1);
3922 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
3924 pages, PAGE_SIZE, 0, false, true);
3925 linger_submit(lreq);
3926 up_write(&osdc->lock);
3928 ret = linger_reg_commit_wait(lreq);
3930 ret = linger_notify_finish_wait(lreq);
3932 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
3934 linger_cancel(lreq);
3939 EXPORT_SYMBOL(ceph_osdc_notify);
3942 * Return the number of milliseconds since the watch was last
3943 * confirmed, or an error. If there is an error, the watch is no
3944 * longer valid, and should be destroyed with ceph_osdc_unwatch().
3946 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
3947 struct ceph_osd_linger_request *lreq)
3949 unsigned long stamp, age;
3952 down_read(&osdc->lock);
3953 mutex_lock(&lreq->lock);
3954 stamp = lreq->watch_valid_thru;
3955 if (!list_empty(&lreq->pending_lworks)) {
3956 struct linger_work *lwork =
3957 list_first_entry(&lreq->pending_lworks,
3961 if (time_before(lwork->queued_stamp, stamp))
3962 stamp = lwork->queued_stamp;
3964 age = jiffies - stamp;
3965 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
3966 lreq, lreq->linger_id, age, lreq->last_error);
3967 /* we are truncating to msecs, so return a safe upper bound */
3968 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
3970 mutex_unlock(&lreq->lock);
3971 up_read(&osdc->lock);
3975 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
3981 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
3982 &struct_v, &struct_len);
3986 ceph_decode_copy(p, &item->name, sizeof(item->name));
3987 item->cookie = ceph_decode_64(p);
3988 *p += 4; /* skip timeout_seconds */
3989 if (struct_v >= 2) {
3990 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
3991 ceph_decode_addr(&item->addr);
3994 dout("%s %s%llu cookie %llu addr %s\n", __func__,
3995 ENTITY_NAME(item->name), item->cookie,
3996 ceph_pr_addr(&item->addr.in_addr));
4000 static int decode_watchers(void **p, void *end,
4001 struct ceph_watch_item **watchers,
4009 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4010 &struct_v, &struct_len);
4014 *num_watchers = ceph_decode_32(p);
4015 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4019 for (i = 0; i < *num_watchers; i++) {
4020 ret = decode_watcher(p, end, *watchers + i);
4031 * On success, the caller is responsible for:
4035 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4036 struct ceph_object_id *oid,
4037 struct ceph_object_locator *oloc,
4038 struct ceph_watch_item **watchers,
4041 struct ceph_osd_request *req;
4042 struct page **pages;
4045 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4049 ceph_oid_copy(&req->r_base_oid, oid);
4050 ceph_oloc_copy(&req->r_base_oloc, oloc);
4051 req->r_flags = CEPH_OSD_FLAG_READ;
4053 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4057 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4058 if (IS_ERR(pages)) {
4059 ret = PTR_ERR(pages);
4063 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4064 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4066 pages, PAGE_SIZE, 0, false, true);
4068 ceph_osdc_start_request(osdc, req, false);
4069 ret = ceph_osdc_wait_request(osdc, req);
4071 void *p = page_address(pages[0]);
4072 void *const end = p + req->r_ops[0].outdata_len;
4074 ret = decode_watchers(&p, end, watchers, num_watchers);
4078 ceph_osdc_put_request(req);
4081 EXPORT_SYMBOL(ceph_osdc_list_watchers);
4084 * Call all pending notify callbacks - for use after a watch is
4085 * unregistered, to make sure no more callbacks for it will be invoked
4087 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4089 dout("%s osdc %p\n", __func__, osdc);
4090 flush_workqueue(osdc->notify_wq);
4092 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4094 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4096 down_read(&osdc->lock);
4097 maybe_request_map(osdc);
4098 up_read(&osdc->lock);
4100 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4103 * Execute an OSD class method on an object.
4105 * @flags: CEPH_OSD_FLAG_*
4106 * @resp_len: in/out param for reply length
4108 int ceph_osdc_call(struct ceph_osd_client *osdc,
4109 struct ceph_object_id *oid,
4110 struct ceph_object_locator *oloc,
4111 const char *class, const char *method,
4113 struct page *req_page, size_t req_len,
4114 struct page *resp_page, size_t *resp_len)
4116 struct ceph_osd_request *req;
4119 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4122 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4126 ceph_oid_copy(&req->r_base_oid, oid);
4127 ceph_oloc_copy(&req->r_base_oloc, oloc);
4128 req->r_flags = flags;
4130 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4134 osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4136 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4139 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
4140 *resp_len, 0, false, false);
4142 ceph_osdc_start_request(osdc, req, false);
4143 ret = ceph_osdc_wait_request(osdc, req);
4145 ret = req->r_ops[0].rval;
4147 *resp_len = req->r_ops[0].outdata_len;
4151 ceph_osdc_put_request(req);
4154 EXPORT_SYMBOL(ceph_osdc_call);
4159 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4164 osdc->client = client;
4165 init_rwsem(&osdc->lock);
4166 osdc->osds = RB_ROOT;
4167 INIT_LIST_HEAD(&osdc->osd_lru);
4168 spin_lock_init(&osdc->osd_lru_lock);
4169 osd_init(&osdc->homeless_osd);
4170 osdc->homeless_osd.o_osdc = osdc;
4171 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
4172 osdc->last_linger_id = CEPH_LINGER_ID_START;
4173 osdc->linger_requests = RB_ROOT;
4174 osdc->map_checks = RB_ROOT;
4175 osdc->linger_map_checks = RB_ROOT;
4176 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
4177 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
4180 osdc->osdmap = ceph_osdmap_alloc();
4184 osdc->req_mempool = mempool_create_slab_pool(10,
4185 ceph_osd_request_cache);
4186 if (!osdc->req_mempool)
4189 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
4190 PAGE_SIZE, 10, true, "osd_op");
4193 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4194 PAGE_SIZE, 10, true, "osd_op_reply");
4199 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
4200 if (!osdc->notify_wq)
4201 goto out_msgpool_reply;
4203 schedule_delayed_work(&osdc->timeout_work,
4204 osdc->client->options->osd_keepalive_timeout);
4205 schedule_delayed_work(&osdc->osds_timeout_work,
4206 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4211 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
4213 ceph_msgpool_destroy(&osdc->msgpool_op);
4215 mempool_destroy(osdc->req_mempool);
4217 ceph_osdmap_destroy(osdc->osdmap);
4222 void ceph_osdc_stop(struct ceph_osd_client *osdc)
4224 flush_workqueue(osdc->notify_wq);
4225 destroy_workqueue(osdc->notify_wq);
4226 cancel_delayed_work_sync(&osdc->timeout_work);
4227 cancel_delayed_work_sync(&osdc->osds_timeout_work);
4229 down_write(&osdc->lock);
4230 while (!RB_EMPTY_ROOT(&osdc->osds)) {
4231 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
4232 struct ceph_osd, o_node);
4235 up_write(&osdc->lock);
4236 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
4237 osd_cleanup(&osdc->homeless_osd);
4239 WARN_ON(!list_empty(&osdc->osd_lru));
4240 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
4241 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
4242 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
4243 WARN_ON(atomic_read(&osdc->num_requests));
4244 WARN_ON(atomic_read(&osdc->num_homeless));
4246 ceph_osdmap_destroy(osdc->osdmap);
4247 mempool_destroy(osdc->req_mempool);
4248 ceph_msgpool_destroy(&osdc->msgpool_op);
4249 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
4253 * Read some contiguous pages. If we cross a stripe boundary, shorten
4254 * *plen. Return number of bytes read, or error.
4256 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
4257 struct ceph_vino vino, struct ceph_file_layout *layout,
4259 u32 truncate_seq, u64 truncate_size,
4260 struct page **pages, int num_pages, int page_align)
4262 struct ceph_osd_request *req;
4265 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
4266 vino.snap, off, *plen);
4267 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
4268 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
4269 NULL, truncate_seq, truncate_size,
4272 return PTR_ERR(req);
4274 /* it may be a short read due to an object boundary */
4275 osd_req_op_extent_osd_data_pages(req, 0,
4276 pages, *plen, page_align, false, false);
4278 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
4279 off, *plen, *plen, page_align);
4281 rc = ceph_osdc_start_request(osdc, req, false);
4283 rc = ceph_osdc_wait_request(osdc, req);
4285 ceph_osdc_put_request(req);
4286 dout("readpages result %d\n", rc);
4289 EXPORT_SYMBOL(ceph_osdc_readpages);
4292 * do a synchronous write on N pages
4294 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
4295 struct ceph_file_layout *layout,
4296 struct ceph_snap_context *snapc,
4298 u32 truncate_seq, u64 truncate_size,
4299 struct timespec *mtime,
4300 struct page **pages, int num_pages)
4302 struct ceph_osd_request *req;
4304 int page_align = off & ~PAGE_MASK;
4306 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
4307 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
4308 snapc, truncate_seq, truncate_size,
4311 return PTR_ERR(req);
4313 /* it may be a short write due to an object boundary */
4314 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
4316 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
4318 req->r_mtime = *mtime;
4319 rc = ceph_osdc_start_request(osdc, req, true);
4321 rc = ceph_osdc_wait_request(osdc, req);
4323 ceph_osdc_put_request(req);
4326 dout("writepages result %d\n", rc);
4329 EXPORT_SYMBOL(ceph_osdc_writepages);
4331 int ceph_osdc_setup(void)
4333 size_t size = sizeof(struct ceph_osd_request) +
4334 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
4336 BUG_ON(ceph_osd_request_cache);
4337 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
4340 return ceph_osd_request_cache ? 0 : -ENOMEM;
4342 EXPORT_SYMBOL(ceph_osdc_setup);
4344 void ceph_osdc_cleanup(void)
4346 BUG_ON(!ceph_osd_request_cache);
4347 kmem_cache_destroy(ceph_osd_request_cache);
4348 ceph_osd_request_cache = NULL;
4350 EXPORT_SYMBOL(ceph_osdc_cleanup);
4353 * handle incoming message
4355 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4357 struct ceph_osd *osd = con->private;
4358 struct ceph_osd_client *osdc = osd->o_osdc;
4359 int type = le16_to_cpu(msg->hdr.type);
4362 case CEPH_MSG_OSD_MAP:
4363 ceph_osdc_handle_map(osdc, msg);
4365 case CEPH_MSG_OSD_OPREPLY:
4366 handle_reply(osd, msg);
4368 case CEPH_MSG_WATCH_NOTIFY:
4369 handle_watch_notify(osdc, msg);
4373 pr_err("received unknown message type %d %s\n", type,
4374 ceph_msg_type_name(type));
4381 * Lookup and return message for incoming reply. Don't try to do
4382 * anything about a larger than preallocated data portion of the
4383 * message at the moment - for now, just skip the message.
4385 static struct ceph_msg *get_reply(struct ceph_connection *con,
4386 struct ceph_msg_header *hdr,
4389 struct ceph_osd *osd = con->private;
4390 struct ceph_osd_client *osdc = osd->o_osdc;
4391 struct ceph_msg *m = NULL;
4392 struct ceph_osd_request *req;
4393 int front_len = le32_to_cpu(hdr->front_len);
4394 int data_len = le32_to_cpu(hdr->data_len);
4395 u64 tid = le64_to_cpu(hdr->tid);
4397 down_read(&osdc->lock);
4398 if (!osd_registered(osd)) {
4399 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
4401 goto out_unlock_osdc;
4403 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
4405 mutex_lock(&osd->lock);
4406 req = lookup_request(&osd->o_requests, tid);
4408 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
4411 goto out_unlock_session;
4414 ceph_msg_revoke_incoming(req->r_reply);
4416 if (front_len > req->r_reply->front_alloc_len) {
4417 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
4418 __func__, osd->o_osd, req->r_tid, front_len,
4419 req->r_reply->front_alloc_len);
4420 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
4423 goto out_unlock_session;
4424 ceph_msg_put(req->r_reply);
4428 if (data_len > req->r_reply->data_length) {
4429 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
4430 __func__, osd->o_osd, req->r_tid, data_len,
4431 req->r_reply->data_length);
4434 goto out_unlock_session;
4437 m = ceph_msg_get(req->r_reply);
4438 dout("get_reply tid %lld %p\n", tid, m);
4441 mutex_unlock(&osd->lock);
4443 up_read(&osdc->lock);
4448 * TODO: switch to a msg-owned pagelist
4450 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
4453 int type = le16_to_cpu(hdr->type);
4454 u32 front_len = le32_to_cpu(hdr->front_len);
4455 u32 data_len = le32_to_cpu(hdr->data_len);
4457 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
4462 struct page **pages;
4463 struct ceph_osd_data osd_data;
4465 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
4467 if (IS_ERR(pages)) {
4472 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
4474 ceph_osdc_msg_data_add(m, &osd_data);
4480 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
4481 struct ceph_msg_header *hdr,
4484 struct ceph_osd *osd = con->private;
4485 int type = le16_to_cpu(hdr->type);
4489 case CEPH_MSG_OSD_MAP:
4490 case CEPH_MSG_WATCH_NOTIFY:
4491 return alloc_msg_with_page_vector(hdr);
4492 case CEPH_MSG_OSD_OPREPLY:
4493 return get_reply(con, hdr, skip);
4495 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
4503 * Wrappers to refcount containing ceph_osd struct
4505 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
4507 struct ceph_osd *osd = con->private;
4513 static void put_osd_con(struct ceph_connection *con)
4515 struct ceph_osd *osd = con->private;
4523 * Note: returned pointer is the address of a structure that's
4524 * managed separately. Caller must *not* attempt to free it.
4526 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
4527 int *proto, int force_new)
4529 struct ceph_osd *o = con->private;
4530 struct ceph_osd_client *osdc = o->o_osdc;
4531 struct ceph_auth_client *ac = osdc->client->monc.auth;
4532 struct ceph_auth_handshake *auth = &o->o_auth;
4534 if (force_new && auth->authorizer) {
4535 ceph_auth_destroy_authorizer(auth->authorizer);
4536 auth->authorizer = NULL;
4538 if (!auth->authorizer) {
4539 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4542 return ERR_PTR(ret);
4544 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4547 return ERR_PTR(ret);
4549 *proto = ac->protocol;
4555 static int verify_authorizer_reply(struct ceph_connection *con)
4557 struct ceph_osd *o = con->private;
4558 struct ceph_osd_client *osdc = o->o_osdc;
4559 struct ceph_auth_client *ac = osdc->client->monc.auth;
4561 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
4564 static int invalidate_authorizer(struct ceph_connection *con)
4566 struct ceph_osd *o = con->private;
4567 struct ceph_osd_client *osdc = o->o_osdc;
4568 struct ceph_auth_client *ac = osdc->client->monc.auth;
4570 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
4571 return ceph_monc_validate_auth(&osdc->client->monc);
4574 static int osd_sign_message(struct ceph_msg *msg)
4576 struct ceph_osd *o = msg->con->private;
4577 struct ceph_auth_handshake *auth = &o->o_auth;
4579 return ceph_auth_sign_message(auth, msg);
4582 static int osd_check_message_signature(struct ceph_msg *msg)
4584 struct ceph_osd *o = msg->con->private;
4585 struct ceph_auth_handshake *auth = &o->o_auth;
4587 return ceph_auth_check_message_signature(auth, msg);
4590 static const struct ceph_connection_operations osd_con_ops = {
4593 .dispatch = dispatch,
4594 .get_authorizer = get_authorizer,
4595 .verify_authorizer_reply = verify_authorizer_reply,
4596 .invalidate_authorizer = invalidate_authorizer,
4597 .alloc_msg = alloc_msg,
4598 .sign_message = osd_sign_message,
4599 .check_message_signature = osd_check_message_signature,