libceph: define source request op functions
[linux-2.6/btrfs-unstable.git] / net / ceph / osd_client.c
blob02ed7282047931caeafeb91d89a9682f59ed78c1
1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
4 #include <linux/err.h>
5 #include <linux/highmem.h>
6 #include <linux/mm.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
10 #ifdef CONFIG_BLOCK
11 #include <linux/bio.h>
12 #endif
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
21 #define OSD_OP_FRONT_LEN 4096
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static const struct ceph_connection_operations osd_con_ops;
26 static void __send_queued(struct ceph_osd_client *osdc);
27 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28 static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30 static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
32 static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
35 static int op_has_extent(int op)
37 return (op == CEPH_OSD_OP_READ ||
38 op == CEPH_OSD_OP_WRITE);
42 * Implement client access to distributed object storage cluster.
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
48 * access to storage.
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
62 * object boundary.
64 * fill osd op in request message.
66 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
67 struct ceph_osd_req_op *op, u64 *bno)
69 u64 orig_len = *plen;
70 u64 objoff = 0;
71 u64 objlen = 0;
72 int r;
74 /* object extent? */
75 r = ceph_calc_file_object_mapping(layout, off, orig_len, bno,
76 &objoff, &objlen);
77 if (r < 0)
78 return r;
79 if (objlen < orig_len) {
80 *plen = objlen;
81 dout(" skipping last %llu, final file extent %llu~%llu\n",
82 orig_len - *plen, off, *plen);
85 if (op_has_extent(op->op)) {
86 u32 osize = le32_to_cpu(layout->fl_object_size);
87 op->extent.offset = objoff;
88 op->extent.length = objlen;
89 if (op->extent.truncate_size <= off - objoff) {
90 op->extent.truncate_size = 0;
91 } else {
92 op->extent.truncate_size -= off - objoff;
93 if (op->extent.truncate_size > osize)
94 op->extent.truncate_size = osize;
97 if (op->op == CEPH_OSD_OP_WRITE)
98 op->payload_len = *plen;
100 dout("calc_layout bno=%llx %llu~%llu\n", *bno, objoff, objlen);
102 return 0;
106 * requests
108 void ceph_osdc_release_request(struct kref *kref)
110 int num_pages;
111 struct ceph_osd_request *req = container_of(kref,
112 struct ceph_osd_request,
113 r_kref);
115 if (req->r_request)
116 ceph_msg_put(req->r_request);
117 if (req->r_con_filling_msg) {
118 dout("%s revoking msg %p from con %p\n", __func__,
119 req->r_reply, req->r_con_filling_msg);
120 ceph_msg_revoke_incoming(req->r_reply);
121 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
122 req->r_con_filling_msg = NULL;
124 if (req->r_reply)
125 ceph_msg_put(req->r_reply);
127 if (req->r_data_in.type == CEPH_OSD_DATA_TYPE_PAGES &&
128 req->r_data_in.own_pages) {
129 num_pages = calc_pages_for((u64)req->r_data_in.alignment,
130 (u64)req->r_data_in.length);
131 ceph_release_page_vector(req->r_data_in.pages, num_pages);
133 if (req->r_data_out.type == CEPH_OSD_DATA_TYPE_PAGES &&
134 req->r_data_out.own_pages) {
135 num_pages = calc_pages_for((u64)req->r_data_out.alignment,
136 (u64)req->r_data_out.length);
137 ceph_release_page_vector(req->r_data_out.pages, num_pages);
140 ceph_put_snap_context(req->r_snapc);
141 if (req->r_mempool)
142 mempool_free(req, req->r_osdc->req_mempool);
143 else
144 kfree(req);
146 EXPORT_SYMBOL(ceph_osdc_release_request);
148 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
149 struct ceph_snap_context *snapc,
150 unsigned int num_ops,
151 bool use_mempool,
152 gfp_t gfp_flags)
154 struct ceph_osd_request *req;
155 struct ceph_msg *msg;
156 size_t msg_size;
158 msg_size = 4 + 4 + 8 + 8 + 4+8;
159 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
160 msg_size += 1 + 8 + 4 + 4; /* pg_t */
161 msg_size += 4 + MAX_OBJ_NAME_SIZE;
162 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
163 msg_size += 8; /* snapid */
164 msg_size += 8; /* snap_seq */
165 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
166 msg_size += 4;
168 if (use_mempool) {
169 req = mempool_alloc(osdc->req_mempool, gfp_flags);
170 memset(req, 0, sizeof(*req));
171 } else {
172 req = kzalloc(sizeof(*req), gfp_flags);
174 if (req == NULL)
175 return NULL;
177 req->r_osdc = osdc;
178 req->r_mempool = use_mempool;
180 kref_init(&req->r_kref);
181 init_completion(&req->r_completion);
182 init_completion(&req->r_safe_completion);
183 RB_CLEAR_NODE(&req->r_node);
184 INIT_LIST_HEAD(&req->r_unsafe_item);
185 INIT_LIST_HEAD(&req->r_linger_item);
186 INIT_LIST_HEAD(&req->r_linger_osd);
187 INIT_LIST_HEAD(&req->r_req_lru_item);
188 INIT_LIST_HEAD(&req->r_osd_item);
190 /* create reply message */
191 if (use_mempool)
192 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
193 else
194 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
195 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
196 if (!msg) {
197 ceph_osdc_put_request(req);
198 return NULL;
200 req->r_reply = msg;
202 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
203 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
205 /* create request message; allow space for oid */
206 if (use_mempool)
207 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
208 else
209 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
210 if (!msg) {
211 ceph_osdc_put_request(req);
212 return NULL;
215 memset(msg->front.iov_base, 0, msg->front.iov_len);
217 req->r_request = msg;
219 return req;
221 EXPORT_SYMBOL(ceph_osdc_alloc_request);
223 static bool osd_req_opcode_valid(u16 opcode)
225 switch (opcode) {
226 case CEPH_OSD_OP_READ:
227 case CEPH_OSD_OP_STAT:
228 case CEPH_OSD_OP_MAPEXT:
229 case CEPH_OSD_OP_MASKTRUNC:
230 case CEPH_OSD_OP_SPARSE_READ:
231 case CEPH_OSD_OP_NOTIFY:
232 case CEPH_OSD_OP_NOTIFY_ACK:
233 case CEPH_OSD_OP_ASSERT_VER:
234 case CEPH_OSD_OP_WRITE:
235 case CEPH_OSD_OP_WRITEFULL:
236 case CEPH_OSD_OP_TRUNCATE:
237 case CEPH_OSD_OP_ZERO:
238 case CEPH_OSD_OP_DELETE:
239 case CEPH_OSD_OP_APPEND:
240 case CEPH_OSD_OP_STARTSYNC:
241 case CEPH_OSD_OP_SETTRUNC:
242 case CEPH_OSD_OP_TRIMTRUNC:
243 case CEPH_OSD_OP_TMAPUP:
244 case CEPH_OSD_OP_TMAPPUT:
245 case CEPH_OSD_OP_TMAPGET:
246 case CEPH_OSD_OP_CREATE:
247 case CEPH_OSD_OP_ROLLBACK:
248 case CEPH_OSD_OP_WATCH:
249 case CEPH_OSD_OP_OMAPGETKEYS:
250 case CEPH_OSD_OP_OMAPGETVALS:
251 case CEPH_OSD_OP_OMAPGETHEADER:
252 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
253 case CEPH_OSD_OP_OMAPSETVALS:
254 case CEPH_OSD_OP_OMAPSETHEADER:
255 case CEPH_OSD_OP_OMAPCLEAR:
256 case CEPH_OSD_OP_OMAPRMKEYS:
257 case CEPH_OSD_OP_OMAP_CMP:
258 case CEPH_OSD_OP_CLONERANGE:
259 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
260 case CEPH_OSD_OP_SRC_CMPXATTR:
261 case CEPH_OSD_OP_GETXATTR:
262 case CEPH_OSD_OP_GETXATTRS:
263 case CEPH_OSD_OP_CMPXATTR:
264 case CEPH_OSD_OP_SETXATTR:
265 case CEPH_OSD_OP_SETXATTRS:
266 case CEPH_OSD_OP_RESETXATTRS:
267 case CEPH_OSD_OP_RMXATTR:
268 case CEPH_OSD_OP_PULL:
269 case CEPH_OSD_OP_PUSH:
270 case CEPH_OSD_OP_BALANCEREADS:
271 case CEPH_OSD_OP_UNBALANCEREADS:
272 case CEPH_OSD_OP_SCRUB:
273 case CEPH_OSD_OP_SCRUB_RESERVE:
274 case CEPH_OSD_OP_SCRUB_UNRESERVE:
275 case CEPH_OSD_OP_SCRUB_STOP:
276 case CEPH_OSD_OP_SCRUB_MAP:
277 case CEPH_OSD_OP_WRLOCK:
278 case CEPH_OSD_OP_WRUNLOCK:
279 case CEPH_OSD_OP_RDLOCK:
280 case CEPH_OSD_OP_RDUNLOCK:
281 case CEPH_OSD_OP_UPLOCK:
282 case CEPH_OSD_OP_DNLOCK:
283 case CEPH_OSD_OP_CALL:
284 case CEPH_OSD_OP_PGLS:
285 case CEPH_OSD_OP_PGLS_FILTER:
286 return true;
287 default:
288 return false;
293 * This is an osd op init function for opcodes that have no data or
294 * other information associated with them. It also serves as a
295 * common init routine for all the other init functions, below.
297 void osd_req_op_init(struct ceph_osd_req_op *op, u16 opcode)
299 BUG_ON(!osd_req_opcode_valid(opcode));
301 memset(op, 0, sizeof (*op));
303 op->op = opcode;
306 void osd_req_op_extent_init(struct ceph_osd_req_op *op, u16 opcode,
307 u64 offset, u64 length,
308 u64 truncate_size, u32 truncate_seq)
310 size_t payload_len = 0;
312 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
314 osd_req_op_init(op, opcode);
316 op->extent.offset = offset;
317 op->extent.length = length;
318 op->extent.truncate_size = truncate_size;
319 op->extent.truncate_seq = truncate_seq;
320 if (opcode == CEPH_OSD_OP_WRITE)
321 payload_len += length;
323 op->payload_len = payload_len;
325 EXPORT_SYMBOL(osd_req_op_extent_init);
327 void osd_req_op_cls_init(struct ceph_osd_req_op *op, u16 opcode,
328 const char *class, const char *method,
329 const void *request_data, size_t request_data_size)
331 size_t payload_len = 0;
332 size_t size;
334 BUG_ON(opcode != CEPH_OSD_OP_CALL);
336 osd_req_op_init(op, opcode);
338 op->cls.class_name = class;
339 size = strlen(class);
340 BUG_ON(size > (size_t) U8_MAX);
341 op->cls.class_len = size;
342 payload_len += size;
344 op->cls.method_name = method;
345 size = strlen(method);
346 BUG_ON(size > (size_t) U8_MAX);
347 op->cls.method_len = size;
348 payload_len += size;
350 op->cls.indata = request_data;
351 BUG_ON(request_data_size > (size_t) U32_MAX);
352 op->cls.indata_len = (u32) request_data_size;
353 payload_len += request_data_size;
355 op->cls.argc = 0; /* currently unused */
357 op->payload_len = payload_len;
359 EXPORT_SYMBOL(osd_req_op_cls_init);
361 void osd_req_op_watch_init(struct ceph_osd_req_op *op, u16 opcode,
362 u64 cookie, u64 version, int flag)
364 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
366 osd_req_op_init(op, opcode);
368 op->watch.cookie = cookie;
369 /* op->watch.ver = version; */ /* XXX 3847 */
370 op->watch.ver = cpu_to_le64(version);
371 if (opcode == CEPH_OSD_OP_WATCH && flag)
372 op->watch.flag = (u8) 1;
374 EXPORT_SYMBOL(osd_req_op_watch_init);
376 static u64 osd_req_encode_op(struct ceph_osd_request *req,
377 struct ceph_osd_op *dst,
378 struct ceph_osd_req_op *src)
380 u64 out_data_len = 0;
381 struct ceph_pagelist *pagelist;
383 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
384 pr_err("unrecognized osd opcode %d\n", src->op);
386 return 0;
389 switch (src->op) {
390 case CEPH_OSD_OP_STAT:
391 break;
392 case CEPH_OSD_OP_READ:
393 case CEPH_OSD_OP_WRITE:
394 if (src->op == CEPH_OSD_OP_WRITE)
395 out_data_len = src->extent.length;
396 dst->extent.offset = cpu_to_le64(src->extent.offset);
397 dst->extent.length = cpu_to_le64(src->extent.length);
398 dst->extent.truncate_size =
399 cpu_to_le64(src->extent.truncate_size);
400 dst->extent.truncate_seq =
401 cpu_to_le32(src->extent.truncate_seq);
402 break;
403 case CEPH_OSD_OP_CALL:
404 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
405 BUG_ON(!pagelist);
406 ceph_pagelist_init(pagelist);
408 dst->cls.class_len = src->cls.class_len;
409 dst->cls.method_len = src->cls.method_len;
410 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
411 ceph_pagelist_append(pagelist, src->cls.class_name,
412 src->cls.class_len);
413 ceph_pagelist_append(pagelist, src->cls.method_name,
414 src->cls.method_len);
415 ceph_pagelist_append(pagelist, src->cls.indata,
416 src->cls.indata_len);
418 req->r_data_out.type = CEPH_OSD_DATA_TYPE_PAGELIST;
419 req->r_data_out.pagelist = pagelist;
420 out_data_len = pagelist->length;
421 break;
422 case CEPH_OSD_OP_STARTSYNC:
423 break;
424 case CEPH_OSD_OP_NOTIFY_ACK:
425 case CEPH_OSD_OP_WATCH:
426 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
427 dst->watch.ver = cpu_to_le64(src->watch.ver);
428 dst->watch.flag = src->watch.flag;
429 break;
430 default:
431 pr_err("unsupported osd opcode %s\n",
432 ceph_osd_op_name(src->op));
433 WARN_ON(1);
435 return 0;
437 dst->op = cpu_to_le16(src->op);
438 dst->payload_len = cpu_to_le32(src->payload_len);
440 return out_data_len;
444 * build new request AND message
447 void ceph_osdc_build_request(struct ceph_osd_request *req,
448 u64 off, unsigned int num_ops,
449 struct ceph_osd_req_op *src_ops,
450 struct ceph_snap_context *snapc, u64 snap_id,
451 struct timespec *mtime)
453 struct ceph_msg *msg = req->r_request;
454 struct ceph_osd_req_op *src_op;
455 void *p;
456 size_t msg_size;
457 int flags = req->r_flags;
458 u64 data_len;
459 int i;
461 req->r_num_ops = num_ops;
462 req->r_snapid = snap_id;
463 req->r_snapc = ceph_get_snap_context(snapc);
465 /* encode request */
466 msg->hdr.version = cpu_to_le16(4);
468 p = msg->front.iov_base;
469 ceph_encode_32(&p, 1); /* client_inc is always 1 */
470 req->r_request_osdmap_epoch = p;
471 p += 4;
472 req->r_request_flags = p;
473 p += 4;
474 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
475 ceph_encode_timespec(p, mtime);
476 p += sizeof(struct ceph_timespec);
477 req->r_request_reassert_version = p;
478 p += sizeof(struct ceph_eversion); /* will get filled in */
480 /* oloc */
481 ceph_encode_8(&p, 4);
482 ceph_encode_8(&p, 4);
483 ceph_encode_32(&p, 8 + 4 + 4);
484 req->r_request_pool = p;
485 p += 8;
486 ceph_encode_32(&p, -1); /* preferred */
487 ceph_encode_32(&p, 0); /* key len */
489 ceph_encode_8(&p, 1);
490 req->r_request_pgid = p;
491 p += 8 + 4;
492 ceph_encode_32(&p, -1); /* preferred */
494 /* oid */
495 ceph_encode_32(&p, req->r_oid_len);
496 memcpy(p, req->r_oid, req->r_oid_len);
497 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
498 p += req->r_oid_len;
500 /* ops--can imply data */
501 ceph_encode_16(&p, num_ops);
502 src_op = src_ops;
503 req->r_request_ops = p;
504 data_len = 0;
505 for (i = 0; i < num_ops; i++, src_op++) {
506 data_len += osd_req_encode_op(req, p, src_op);
507 p += sizeof(struct ceph_osd_op);
510 /* snaps */
511 ceph_encode_64(&p, req->r_snapid);
512 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
513 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
514 if (req->r_snapc) {
515 for (i = 0; i < snapc->num_snaps; i++) {
516 ceph_encode_64(&p, req->r_snapc->snaps[i]);
520 req->r_request_attempts = p;
521 p += 4;
523 /* data */
524 if (flags & CEPH_OSD_FLAG_WRITE) {
525 u16 data_off;
528 * The header "data_off" is a hint to the receiver
529 * allowing it to align received data into its
530 * buffers such that there's no need to re-copy
531 * it before writing it to disk (direct I/O).
533 data_off = (u16) (off & 0xffff);
534 req->r_request->hdr.data_off = cpu_to_le16(data_off);
536 req->r_request->hdr.data_len = cpu_to_le32(data_len);
538 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
539 msg_size = p - msg->front.iov_base;
540 msg->front.iov_len = msg_size;
541 msg->hdr.front_len = cpu_to_le32(msg_size);
543 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
544 num_ops);
545 return;
547 EXPORT_SYMBOL(ceph_osdc_build_request);
550 * build new request AND message, calculate layout, and adjust file
551 * extent as needed.
553 * if the file was recently truncated, we include information about its
554 * old and new size so that the object can be updated appropriately. (we
555 * avoid synchronously deleting truncated objects because it's slow.)
557 * if @do_sync, include a 'startsync' command so that the osd will flush
558 * data quickly.
560 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
561 struct ceph_file_layout *layout,
562 struct ceph_vino vino,
563 u64 off, u64 *plen,
564 int opcode, int flags,
565 struct ceph_snap_context *snapc,
566 int do_sync,
567 u32 truncate_seq,
568 u64 truncate_size,
569 struct timespec *mtime,
570 bool use_mempool)
572 struct ceph_osd_req_op ops[2];
573 struct ceph_osd_request *req;
574 unsigned int num_op = 1;
575 u64 bno = 0;
576 int r;
578 memset(&ops, 0, sizeof ops);
580 ops[0].op = opcode;
581 ops[0].extent.truncate_seq = truncate_seq;
582 ops[0].extent.truncate_size = truncate_size;
584 if (do_sync) {
585 ops[1].op = CEPH_OSD_OP_STARTSYNC;
586 num_op++;
589 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
590 GFP_NOFS);
591 if (!req)
592 return ERR_PTR(-ENOMEM);
593 req->r_flags = flags;
595 /* calculate max write size */
596 r = calc_layout(layout, off, plen, ops, &bno);
597 if (r < 0) {
598 ceph_osdc_put_request(req);
599 return ERR_PTR(r);
601 req->r_file_layout = *layout; /* keep a copy */
603 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
604 req->r_oid_len = strlen(req->r_oid);
606 ceph_osdc_build_request(req, off, num_op, ops,
607 snapc, vino.snap, mtime);
609 return req;
611 EXPORT_SYMBOL(ceph_osdc_new_request);
614 * We keep osd requests in an rbtree, sorted by ->r_tid.
616 static void __insert_request(struct ceph_osd_client *osdc,
617 struct ceph_osd_request *new)
619 struct rb_node **p = &osdc->requests.rb_node;
620 struct rb_node *parent = NULL;
621 struct ceph_osd_request *req = NULL;
623 while (*p) {
624 parent = *p;
625 req = rb_entry(parent, struct ceph_osd_request, r_node);
626 if (new->r_tid < req->r_tid)
627 p = &(*p)->rb_left;
628 else if (new->r_tid > req->r_tid)
629 p = &(*p)->rb_right;
630 else
631 BUG();
634 rb_link_node(&new->r_node, parent, p);
635 rb_insert_color(&new->r_node, &osdc->requests);
638 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
639 u64 tid)
641 struct ceph_osd_request *req;
642 struct rb_node *n = osdc->requests.rb_node;
644 while (n) {
645 req = rb_entry(n, struct ceph_osd_request, r_node);
646 if (tid < req->r_tid)
647 n = n->rb_left;
648 else if (tid > req->r_tid)
649 n = n->rb_right;
650 else
651 return req;
653 return NULL;
656 static struct ceph_osd_request *
657 __lookup_request_ge(struct ceph_osd_client *osdc,
658 u64 tid)
660 struct ceph_osd_request *req;
661 struct rb_node *n = osdc->requests.rb_node;
663 while (n) {
664 req = rb_entry(n, struct ceph_osd_request, r_node);
665 if (tid < req->r_tid) {
666 if (!n->rb_left)
667 return req;
668 n = n->rb_left;
669 } else if (tid > req->r_tid) {
670 n = n->rb_right;
671 } else {
672 return req;
675 return NULL;
679 * Resubmit requests pending on the given osd.
681 static void __kick_osd_requests(struct ceph_osd_client *osdc,
682 struct ceph_osd *osd)
684 struct ceph_osd_request *req, *nreq;
685 LIST_HEAD(resend);
686 int err;
688 dout("__kick_osd_requests osd%d\n", osd->o_osd);
689 err = __reset_osd(osdc, osd);
690 if (err)
691 return;
693 * Build up a list of requests to resend by traversing the
694 * osd's list of requests. Requests for a given object are
695 * sent in tid order, and that is also the order they're
696 * kept on this list. Therefore all requests that are in
697 * flight will be found first, followed by all requests that
698 * have not yet been sent. And to resend requests while
699 * preserving this order we will want to put any sent
700 * requests back on the front of the osd client's unsent
701 * list.
703 * So we build a separate ordered list of already-sent
704 * requests for the affected osd and splice it onto the
705 * front of the osd client's unsent list. Once we've seen a
706 * request that has not yet been sent we're done. Those
707 * requests are already sitting right where they belong.
709 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
710 if (!req->r_sent)
711 break;
712 list_move_tail(&req->r_req_lru_item, &resend);
713 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
714 osd->o_osd);
715 if (!req->r_linger)
716 req->r_flags |= CEPH_OSD_FLAG_RETRY;
718 list_splice(&resend, &osdc->req_unsent);
721 * Linger requests are re-registered before sending, which
722 * sets up a new tid for each. We add them to the unsent
723 * list at the end to keep things in tid order.
725 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
726 r_linger_osd) {
728 * reregister request prior to unregistering linger so
729 * that r_osd is preserved.
731 BUG_ON(!list_empty(&req->r_req_lru_item));
732 __register_request(osdc, req);
733 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
734 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
735 __unregister_linger_request(osdc, req);
736 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
737 osd->o_osd);
742 * If the osd connection drops, we need to resubmit all requests.
744 static void osd_reset(struct ceph_connection *con)
746 struct ceph_osd *osd = con->private;
747 struct ceph_osd_client *osdc;
749 if (!osd)
750 return;
751 dout("osd_reset osd%d\n", osd->o_osd);
752 osdc = osd->o_osdc;
753 down_read(&osdc->map_sem);
754 mutex_lock(&osdc->request_mutex);
755 __kick_osd_requests(osdc, osd);
756 __send_queued(osdc);
757 mutex_unlock(&osdc->request_mutex);
758 up_read(&osdc->map_sem);
762 * Track open sessions with osds.
764 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
766 struct ceph_osd *osd;
768 osd = kzalloc(sizeof(*osd), GFP_NOFS);
769 if (!osd)
770 return NULL;
772 atomic_set(&osd->o_ref, 1);
773 osd->o_osdc = osdc;
774 osd->o_osd = onum;
775 RB_CLEAR_NODE(&osd->o_node);
776 INIT_LIST_HEAD(&osd->o_requests);
777 INIT_LIST_HEAD(&osd->o_linger_requests);
778 INIT_LIST_HEAD(&osd->o_osd_lru);
779 osd->o_incarnation = 1;
781 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
783 INIT_LIST_HEAD(&osd->o_keepalive_item);
784 return osd;
787 static struct ceph_osd *get_osd(struct ceph_osd *osd)
789 if (atomic_inc_not_zero(&osd->o_ref)) {
790 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
791 atomic_read(&osd->o_ref));
792 return osd;
793 } else {
794 dout("get_osd %p FAIL\n", osd);
795 return NULL;
799 static void put_osd(struct ceph_osd *osd)
801 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
802 atomic_read(&osd->o_ref) - 1);
803 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
804 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
806 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
807 kfree(osd);
812 * remove an osd from our map
814 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
816 dout("__remove_osd %p\n", osd);
817 BUG_ON(!list_empty(&osd->o_requests));
818 rb_erase(&osd->o_node, &osdc->osds);
819 list_del_init(&osd->o_osd_lru);
820 ceph_con_close(&osd->o_con);
821 put_osd(osd);
824 static void remove_all_osds(struct ceph_osd_client *osdc)
826 dout("%s %p\n", __func__, osdc);
827 mutex_lock(&osdc->request_mutex);
828 while (!RB_EMPTY_ROOT(&osdc->osds)) {
829 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
830 struct ceph_osd, o_node);
831 __remove_osd(osdc, osd);
833 mutex_unlock(&osdc->request_mutex);
836 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
837 struct ceph_osd *osd)
839 dout("__move_osd_to_lru %p\n", osd);
840 BUG_ON(!list_empty(&osd->o_osd_lru));
841 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
842 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
845 static void __remove_osd_from_lru(struct ceph_osd *osd)
847 dout("__remove_osd_from_lru %p\n", osd);
848 if (!list_empty(&osd->o_osd_lru))
849 list_del_init(&osd->o_osd_lru);
852 static void remove_old_osds(struct ceph_osd_client *osdc)
854 struct ceph_osd *osd, *nosd;
856 dout("__remove_old_osds %p\n", osdc);
857 mutex_lock(&osdc->request_mutex);
858 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
859 if (time_before(jiffies, osd->lru_ttl))
860 break;
861 __remove_osd(osdc, osd);
863 mutex_unlock(&osdc->request_mutex);
867 * reset osd connect
869 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
871 struct ceph_entity_addr *peer_addr;
873 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
874 if (list_empty(&osd->o_requests) &&
875 list_empty(&osd->o_linger_requests)) {
876 __remove_osd(osdc, osd);
878 return -ENODEV;
881 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
882 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
883 !ceph_con_opened(&osd->o_con)) {
884 struct ceph_osd_request *req;
886 dout(" osd addr hasn't changed and connection never opened,"
887 " letting msgr retry");
888 /* touch each r_stamp for handle_timeout()'s benfit */
889 list_for_each_entry(req, &osd->o_requests, r_osd_item)
890 req->r_stamp = jiffies;
892 return -EAGAIN;
895 ceph_con_close(&osd->o_con);
896 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
897 osd->o_incarnation++;
899 return 0;
902 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
904 struct rb_node **p = &osdc->osds.rb_node;
905 struct rb_node *parent = NULL;
906 struct ceph_osd *osd = NULL;
908 dout("__insert_osd %p osd%d\n", new, new->o_osd);
909 while (*p) {
910 parent = *p;
911 osd = rb_entry(parent, struct ceph_osd, o_node);
912 if (new->o_osd < osd->o_osd)
913 p = &(*p)->rb_left;
914 else if (new->o_osd > osd->o_osd)
915 p = &(*p)->rb_right;
916 else
917 BUG();
920 rb_link_node(&new->o_node, parent, p);
921 rb_insert_color(&new->o_node, &osdc->osds);
924 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
926 struct ceph_osd *osd;
927 struct rb_node *n = osdc->osds.rb_node;
929 while (n) {
930 osd = rb_entry(n, struct ceph_osd, o_node);
931 if (o < osd->o_osd)
932 n = n->rb_left;
933 else if (o > osd->o_osd)
934 n = n->rb_right;
935 else
936 return osd;
938 return NULL;
941 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
943 schedule_delayed_work(&osdc->timeout_work,
944 osdc->client->options->osd_keepalive_timeout * HZ);
947 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
949 cancel_delayed_work(&osdc->timeout_work);
953 * Register request, assign tid. If this is the first request, set up
954 * the timeout event.
956 static void __register_request(struct ceph_osd_client *osdc,
957 struct ceph_osd_request *req)
959 req->r_tid = ++osdc->last_tid;
960 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
961 dout("__register_request %p tid %lld\n", req, req->r_tid);
962 __insert_request(osdc, req);
963 ceph_osdc_get_request(req);
964 osdc->num_requests++;
965 if (osdc->num_requests == 1) {
966 dout(" first request, scheduling timeout\n");
967 __schedule_osd_timeout(osdc);
972 * called under osdc->request_mutex
974 static void __unregister_request(struct ceph_osd_client *osdc,
975 struct ceph_osd_request *req)
977 if (RB_EMPTY_NODE(&req->r_node)) {
978 dout("__unregister_request %p tid %lld not registered\n",
979 req, req->r_tid);
980 return;
983 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
984 rb_erase(&req->r_node, &osdc->requests);
985 osdc->num_requests--;
987 if (req->r_osd) {
988 /* make sure the original request isn't in flight. */
989 ceph_msg_revoke(req->r_request);
991 list_del_init(&req->r_osd_item);
992 if (list_empty(&req->r_osd->o_requests) &&
993 list_empty(&req->r_osd->o_linger_requests)) {
994 dout("moving osd to %p lru\n", req->r_osd);
995 __move_osd_to_lru(osdc, req->r_osd);
997 if (list_empty(&req->r_linger_item))
998 req->r_osd = NULL;
1001 list_del_init(&req->r_req_lru_item);
1002 ceph_osdc_put_request(req);
1004 if (osdc->num_requests == 0) {
1005 dout(" no requests, canceling timeout\n");
1006 __cancel_osd_timeout(osdc);
1011 * Cancel a previously queued request message
1013 static void __cancel_request(struct ceph_osd_request *req)
1015 if (req->r_sent && req->r_osd) {
1016 ceph_msg_revoke(req->r_request);
1017 req->r_sent = 0;
1021 static void __register_linger_request(struct ceph_osd_client *osdc,
1022 struct ceph_osd_request *req)
1024 dout("__register_linger_request %p\n", req);
1025 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1026 if (req->r_osd)
1027 list_add_tail(&req->r_linger_osd,
1028 &req->r_osd->o_linger_requests);
1031 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1032 struct ceph_osd_request *req)
1034 dout("__unregister_linger_request %p\n", req);
1035 list_del_init(&req->r_linger_item);
1036 if (req->r_osd) {
1037 list_del_init(&req->r_linger_osd);
1039 if (list_empty(&req->r_osd->o_requests) &&
1040 list_empty(&req->r_osd->o_linger_requests)) {
1041 dout("moving osd to %p lru\n", req->r_osd);
1042 __move_osd_to_lru(osdc, req->r_osd);
1044 if (list_empty(&req->r_osd_item))
1045 req->r_osd = NULL;
1049 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1050 struct ceph_osd_request *req)
1052 mutex_lock(&osdc->request_mutex);
1053 if (req->r_linger) {
1054 __unregister_linger_request(osdc, req);
1055 ceph_osdc_put_request(req);
1057 mutex_unlock(&osdc->request_mutex);
1059 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1061 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1062 struct ceph_osd_request *req)
1064 if (!req->r_linger) {
1065 dout("set_request_linger %p\n", req);
1066 req->r_linger = 1;
1068 * caller is now responsible for calling
1069 * unregister_linger_request
1071 ceph_osdc_get_request(req);
1074 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1077 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1078 * (as needed), and set the request r_osd appropriately. If there is
1079 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1080 * (unsent, homeless) or leave on in-flight lru.
1082 * Return 0 if unchanged, 1 if changed, or negative on error.
1084 * Caller should hold map_sem for read and request_mutex.
1086 static int __map_request(struct ceph_osd_client *osdc,
1087 struct ceph_osd_request *req, int force_resend)
1089 struct ceph_pg pgid;
1090 int acting[CEPH_PG_MAX_SIZE];
1091 int o = -1, num = 0;
1092 int err;
1094 dout("map_request %p tid %lld\n", req, req->r_tid);
1095 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1096 ceph_file_layout_pg_pool(req->r_file_layout));
1097 if (err) {
1098 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1099 return err;
1101 req->r_pgid = pgid;
1103 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1104 if (err > 0) {
1105 o = acting[0];
1106 num = err;
1109 if ((!force_resend &&
1110 req->r_osd && req->r_osd->o_osd == o &&
1111 req->r_sent >= req->r_osd->o_incarnation &&
1112 req->r_num_pg_osds == num &&
1113 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1114 (req->r_osd == NULL && o == -1))
1115 return 0; /* no change */
1117 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1118 req->r_tid, pgid.pool, pgid.seed, o,
1119 req->r_osd ? req->r_osd->o_osd : -1);
1121 /* record full pg acting set */
1122 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1123 req->r_num_pg_osds = num;
1125 if (req->r_osd) {
1126 __cancel_request(req);
1127 list_del_init(&req->r_osd_item);
1128 req->r_osd = NULL;
1131 req->r_osd = __lookup_osd(osdc, o);
1132 if (!req->r_osd && o >= 0) {
1133 err = -ENOMEM;
1134 req->r_osd = create_osd(osdc, o);
1135 if (!req->r_osd) {
1136 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1137 goto out;
1140 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1141 __insert_osd(osdc, req->r_osd);
1143 ceph_con_open(&req->r_osd->o_con,
1144 CEPH_ENTITY_TYPE_OSD, o,
1145 &osdc->osdmap->osd_addr[o]);
1148 if (req->r_osd) {
1149 __remove_osd_from_lru(req->r_osd);
1150 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1151 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1152 } else {
1153 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1155 err = 1; /* osd or pg changed */
1157 out:
1158 return err;
1162 * caller should hold map_sem (for read) and request_mutex
1164 static void __send_request(struct ceph_osd_client *osdc,
1165 struct ceph_osd_request *req)
1167 void *p;
1169 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1170 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1171 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1173 /* fill in message content that changes each time we send it */
1174 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1175 put_unaligned_le32(req->r_flags, req->r_request_flags);
1176 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1177 p = req->r_request_pgid;
1178 ceph_encode_64(&p, req->r_pgid.pool);
1179 ceph_encode_32(&p, req->r_pgid.seed);
1180 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1181 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1182 sizeof(req->r_reassert_version));
1184 req->r_stamp = jiffies;
1185 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1187 ceph_msg_get(req->r_request); /* send consumes a ref */
1188 ceph_con_send(&req->r_osd->o_con, req->r_request);
1189 req->r_sent = req->r_osd->o_incarnation;
1193 * Send any requests in the queue (req_unsent).
1195 static void __send_queued(struct ceph_osd_client *osdc)
1197 struct ceph_osd_request *req, *tmp;
1199 dout("__send_queued\n");
1200 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1201 __send_request(osdc, req);
1205 * Timeout callback, called every N seconds when 1 or more osd
1206 * requests has been active for more than N seconds. When this
1207 * happens, we ping all OSDs with requests who have timed out to
1208 * ensure any communications channel reset is detected. Reset the
1209 * request timeouts another N seconds in the future as we go.
1210 * Reschedule the timeout event another N seconds in future (unless
1211 * there are no open requests).
1213 static void handle_timeout(struct work_struct *work)
1215 struct ceph_osd_client *osdc =
1216 container_of(work, struct ceph_osd_client, timeout_work.work);
1217 struct ceph_osd_request *req;
1218 struct ceph_osd *osd;
1219 unsigned long keepalive =
1220 osdc->client->options->osd_keepalive_timeout * HZ;
1221 struct list_head slow_osds;
1222 dout("timeout\n");
1223 down_read(&osdc->map_sem);
1225 ceph_monc_request_next_osdmap(&osdc->client->monc);
1227 mutex_lock(&osdc->request_mutex);
1230 * ping osds that are a bit slow. this ensures that if there
1231 * is a break in the TCP connection we will notice, and reopen
1232 * a connection with that osd (from the fault callback).
1234 INIT_LIST_HEAD(&slow_osds);
1235 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1236 if (time_before(jiffies, req->r_stamp + keepalive))
1237 break;
1239 osd = req->r_osd;
1240 BUG_ON(!osd);
1241 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1242 req->r_tid, osd->o_osd);
1243 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1245 while (!list_empty(&slow_osds)) {
1246 osd = list_entry(slow_osds.next, struct ceph_osd,
1247 o_keepalive_item);
1248 list_del_init(&osd->o_keepalive_item);
1249 ceph_con_keepalive(&osd->o_con);
1252 __schedule_osd_timeout(osdc);
1253 __send_queued(osdc);
1254 mutex_unlock(&osdc->request_mutex);
1255 up_read(&osdc->map_sem);
1258 static void handle_osds_timeout(struct work_struct *work)
1260 struct ceph_osd_client *osdc =
1261 container_of(work, struct ceph_osd_client,
1262 osds_timeout_work.work);
1263 unsigned long delay =
1264 osdc->client->options->osd_idle_ttl * HZ >> 2;
1266 dout("osds timeout\n");
1267 down_read(&osdc->map_sem);
1268 remove_old_osds(osdc);
1269 up_read(&osdc->map_sem);
1271 schedule_delayed_work(&osdc->osds_timeout_work,
1272 round_jiffies_relative(delay));
1275 static void complete_request(struct ceph_osd_request *req)
1277 if (req->r_safe_callback)
1278 req->r_safe_callback(req, NULL);
1279 complete_all(&req->r_safe_completion); /* fsync waiter */
1282 static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid)
1284 __u8 v;
1286 ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad);
1287 v = ceph_decode_8(p);
1288 if (v > 1) {
1289 pr_warning("do not understand pg encoding %d > 1", v);
1290 return -EINVAL;
1292 pgid->pool = ceph_decode_64(p);
1293 pgid->seed = ceph_decode_32(p);
1294 *p += 4;
1295 return 0;
1297 bad:
1298 pr_warning("incomplete pg encoding");
1299 return -EINVAL;
1303 * handle osd op reply. either call the callback if it is specified,
1304 * or do the completion to wake up the waiting thread.
1306 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1307 struct ceph_connection *con)
1309 void *p, *end;
1310 struct ceph_osd_request *req;
1311 u64 tid;
1312 int object_len;
1313 int numops, payload_len, flags;
1314 s32 result;
1315 s32 retry_attempt;
1316 struct ceph_pg pg;
1317 int err;
1318 u32 reassert_epoch;
1319 u64 reassert_version;
1320 u32 osdmap_epoch;
1321 int already_completed;
1322 int i;
1324 tid = le64_to_cpu(msg->hdr.tid);
1325 dout("handle_reply %p tid %llu\n", msg, tid);
1327 p = msg->front.iov_base;
1328 end = p + msg->front.iov_len;
1330 ceph_decode_need(&p, end, 4, bad);
1331 object_len = ceph_decode_32(&p);
1332 ceph_decode_need(&p, end, object_len, bad);
1333 p += object_len;
1335 err = __decode_pgid(&p, end, &pg);
1336 if (err)
1337 goto bad;
1339 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1340 flags = ceph_decode_64(&p);
1341 result = ceph_decode_32(&p);
1342 reassert_epoch = ceph_decode_32(&p);
1343 reassert_version = ceph_decode_64(&p);
1344 osdmap_epoch = ceph_decode_32(&p);
1346 /* lookup */
1347 mutex_lock(&osdc->request_mutex);
1348 req = __lookup_request(osdc, tid);
1349 if (req == NULL) {
1350 dout("handle_reply tid %llu dne\n", tid);
1351 mutex_unlock(&osdc->request_mutex);
1352 return;
1354 ceph_osdc_get_request(req);
1356 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1357 req, result);
1359 ceph_decode_need(&p, end, 4, bad);
1360 numops = ceph_decode_32(&p);
1361 if (numops > CEPH_OSD_MAX_OP)
1362 goto bad_put;
1363 if (numops != req->r_num_ops)
1364 goto bad_put;
1365 payload_len = 0;
1366 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1367 for (i = 0; i < numops; i++) {
1368 struct ceph_osd_op *op = p;
1369 int len;
1371 len = le32_to_cpu(op->payload_len);
1372 req->r_reply_op_len[i] = len;
1373 dout(" op %d has %d bytes\n", i, len);
1374 payload_len += len;
1375 p += sizeof(*op);
1377 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1378 pr_warning("sum of op payload lens %d != data_len %d",
1379 payload_len, le32_to_cpu(msg->hdr.data_len));
1380 goto bad_put;
1383 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1384 retry_attempt = ceph_decode_32(&p);
1385 for (i = 0; i < numops; i++)
1386 req->r_reply_op_result[i] = ceph_decode_32(&p);
1389 * if this connection filled our message, drop our reference now, to
1390 * avoid a (safe but slower) revoke later.
1392 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1393 dout(" dropping con_filling_msg ref %p\n", con);
1394 req->r_con_filling_msg = NULL;
1395 con->ops->put(con);
1398 if (!req->r_got_reply) {
1399 unsigned int bytes;
1401 req->r_result = result;
1402 bytes = le32_to_cpu(msg->hdr.data_len);
1403 dout("handle_reply result %d bytes %d\n", req->r_result,
1404 bytes);
1405 if (req->r_result == 0)
1406 req->r_result = bytes;
1408 /* in case this is a write and we need to replay, */
1409 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1410 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1412 req->r_got_reply = 1;
1413 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1414 dout("handle_reply tid %llu dup ack\n", tid);
1415 mutex_unlock(&osdc->request_mutex);
1416 goto done;
1419 dout("handle_reply tid %llu flags %d\n", tid, flags);
1421 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1422 __register_linger_request(osdc, req);
1424 /* either this is a read, or we got the safe response */
1425 if (result < 0 ||
1426 (flags & CEPH_OSD_FLAG_ONDISK) ||
1427 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1428 __unregister_request(osdc, req);
1430 already_completed = req->r_completed;
1431 req->r_completed = 1;
1432 mutex_unlock(&osdc->request_mutex);
1433 if (already_completed)
1434 goto done;
1436 if (req->r_callback)
1437 req->r_callback(req, msg);
1438 else
1439 complete_all(&req->r_completion);
1441 if (flags & CEPH_OSD_FLAG_ONDISK)
1442 complete_request(req);
1444 done:
1445 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1446 ceph_osdc_put_request(req);
1447 return;
1449 bad_put:
1450 ceph_osdc_put_request(req);
1451 bad:
1452 pr_err("corrupt osd_op_reply got %d %d\n",
1453 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1454 ceph_msg_dump(msg);
1457 static void reset_changed_osds(struct ceph_osd_client *osdc)
1459 struct rb_node *p, *n;
1461 for (p = rb_first(&osdc->osds); p; p = n) {
1462 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1464 n = rb_next(p);
1465 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1466 memcmp(&osd->o_con.peer_addr,
1467 ceph_osd_addr(osdc->osdmap,
1468 osd->o_osd),
1469 sizeof(struct ceph_entity_addr)) != 0)
1470 __reset_osd(osdc, osd);
1475 * Requeue requests whose mapping to an OSD has changed. If requests map to
1476 * no osd, request a new map.
1478 * Caller should hold map_sem for read.
1480 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1482 struct ceph_osd_request *req, *nreq;
1483 struct rb_node *p;
1484 int needmap = 0;
1485 int err;
1487 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1488 mutex_lock(&osdc->request_mutex);
1489 for (p = rb_first(&osdc->requests); p; ) {
1490 req = rb_entry(p, struct ceph_osd_request, r_node);
1491 p = rb_next(p);
1494 * For linger requests that have not yet been
1495 * registered, move them to the linger list; they'll
1496 * be sent to the osd in the loop below. Unregister
1497 * the request before re-registering it as a linger
1498 * request to ensure the __map_request() below
1499 * will decide it needs to be sent.
1501 if (req->r_linger && list_empty(&req->r_linger_item)) {
1502 dout("%p tid %llu restart on osd%d\n",
1503 req, req->r_tid,
1504 req->r_osd ? req->r_osd->o_osd : -1);
1505 __unregister_request(osdc, req);
1506 __register_linger_request(osdc, req);
1507 continue;
1510 err = __map_request(osdc, req, force_resend);
1511 if (err < 0)
1512 continue; /* error */
1513 if (req->r_osd == NULL) {
1514 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1515 needmap++; /* request a newer map */
1516 } else if (err > 0) {
1517 if (!req->r_linger) {
1518 dout("%p tid %llu requeued on osd%d\n", req,
1519 req->r_tid,
1520 req->r_osd ? req->r_osd->o_osd : -1);
1521 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1526 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1527 r_linger_item) {
1528 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1530 err = __map_request(osdc, req, force_resend);
1531 dout("__map_request returned %d\n", err);
1532 if (err == 0)
1533 continue; /* no change and no osd was specified */
1534 if (err < 0)
1535 continue; /* hrm! */
1536 if (req->r_osd == NULL) {
1537 dout("tid %llu maps to no valid osd\n", req->r_tid);
1538 needmap++; /* request a newer map */
1539 continue;
1542 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1543 req->r_osd ? req->r_osd->o_osd : -1);
1544 __register_request(osdc, req);
1545 __unregister_linger_request(osdc, req);
1547 mutex_unlock(&osdc->request_mutex);
1549 if (needmap) {
1550 dout("%d requests for down osds, need new map\n", needmap);
1551 ceph_monc_request_next_osdmap(&osdc->client->monc);
1553 reset_changed_osds(osdc);
1558 * Process updated osd map.
1560 * The message contains any number of incremental and full maps, normally
1561 * indicating some sort of topology change in the cluster. Kick requests
1562 * off to different OSDs as needed.
1564 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1566 void *p, *end, *next;
1567 u32 nr_maps, maplen;
1568 u32 epoch;
1569 struct ceph_osdmap *newmap = NULL, *oldmap;
1570 int err;
1571 struct ceph_fsid fsid;
1573 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1574 p = msg->front.iov_base;
1575 end = p + msg->front.iov_len;
1577 /* verify fsid */
1578 ceph_decode_need(&p, end, sizeof(fsid), bad);
1579 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1580 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1581 return;
1583 down_write(&osdc->map_sem);
1585 /* incremental maps */
1586 ceph_decode_32_safe(&p, end, nr_maps, bad);
1587 dout(" %d inc maps\n", nr_maps);
1588 while (nr_maps > 0) {
1589 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1590 epoch = ceph_decode_32(&p);
1591 maplen = ceph_decode_32(&p);
1592 ceph_decode_need(&p, end, maplen, bad);
1593 next = p + maplen;
1594 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1595 dout("applying incremental map %u len %d\n",
1596 epoch, maplen);
1597 newmap = osdmap_apply_incremental(&p, next,
1598 osdc->osdmap,
1599 &osdc->client->msgr);
1600 if (IS_ERR(newmap)) {
1601 err = PTR_ERR(newmap);
1602 goto bad;
1604 BUG_ON(!newmap);
1605 if (newmap != osdc->osdmap) {
1606 ceph_osdmap_destroy(osdc->osdmap);
1607 osdc->osdmap = newmap;
1609 kick_requests(osdc, 0);
1610 } else {
1611 dout("ignoring incremental map %u len %d\n",
1612 epoch, maplen);
1614 p = next;
1615 nr_maps--;
1617 if (newmap)
1618 goto done;
1620 /* full maps */
1621 ceph_decode_32_safe(&p, end, nr_maps, bad);
1622 dout(" %d full maps\n", nr_maps);
1623 while (nr_maps) {
1624 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1625 epoch = ceph_decode_32(&p);
1626 maplen = ceph_decode_32(&p);
1627 ceph_decode_need(&p, end, maplen, bad);
1628 if (nr_maps > 1) {
1629 dout("skipping non-latest full map %u len %d\n",
1630 epoch, maplen);
1631 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1632 dout("skipping full map %u len %d, "
1633 "older than our %u\n", epoch, maplen,
1634 osdc->osdmap->epoch);
1635 } else {
1636 int skipped_map = 0;
1638 dout("taking full map %u len %d\n", epoch, maplen);
1639 newmap = osdmap_decode(&p, p+maplen);
1640 if (IS_ERR(newmap)) {
1641 err = PTR_ERR(newmap);
1642 goto bad;
1644 BUG_ON(!newmap);
1645 oldmap = osdc->osdmap;
1646 osdc->osdmap = newmap;
1647 if (oldmap) {
1648 if (oldmap->epoch + 1 < newmap->epoch)
1649 skipped_map = 1;
1650 ceph_osdmap_destroy(oldmap);
1652 kick_requests(osdc, skipped_map);
1654 p += maplen;
1655 nr_maps--;
1658 done:
1659 downgrade_write(&osdc->map_sem);
1660 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1663 * subscribe to subsequent osdmap updates if full to ensure
1664 * we find out when we are no longer full and stop returning
1665 * ENOSPC.
1667 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1668 ceph_monc_request_next_osdmap(&osdc->client->monc);
1670 mutex_lock(&osdc->request_mutex);
1671 __send_queued(osdc);
1672 mutex_unlock(&osdc->request_mutex);
1673 up_read(&osdc->map_sem);
1674 wake_up_all(&osdc->client->auth_wq);
1675 return;
1677 bad:
1678 pr_err("osdc handle_map corrupt msg\n");
1679 ceph_msg_dump(msg);
1680 up_write(&osdc->map_sem);
1681 return;
1685 * watch/notify callback event infrastructure
1687 * These callbacks are used both for watch and notify operations.
1689 static void __release_event(struct kref *kref)
1691 struct ceph_osd_event *event =
1692 container_of(kref, struct ceph_osd_event, kref);
1694 dout("__release_event %p\n", event);
1695 kfree(event);
1698 static void get_event(struct ceph_osd_event *event)
1700 kref_get(&event->kref);
1703 void ceph_osdc_put_event(struct ceph_osd_event *event)
1705 kref_put(&event->kref, __release_event);
1707 EXPORT_SYMBOL(ceph_osdc_put_event);
1709 static void __insert_event(struct ceph_osd_client *osdc,
1710 struct ceph_osd_event *new)
1712 struct rb_node **p = &osdc->event_tree.rb_node;
1713 struct rb_node *parent = NULL;
1714 struct ceph_osd_event *event = NULL;
1716 while (*p) {
1717 parent = *p;
1718 event = rb_entry(parent, struct ceph_osd_event, node);
1719 if (new->cookie < event->cookie)
1720 p = &(*p)->rb_left;
1721 else if (new->cookie > event->cookie)
1722 p = &(*p)->rb_right;
1723 else
1724 BUG();
1727 rb_link_node(&new->node, parent, p);
1728 rb_insert_color(&new->node, &osdc->event_tree);
1731 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1732 u64 cookie)
1734 struct rb_node **p = &osdc->event_tree.rb_node;
1735 struct rb_node *parent = NULL;
1736 struct ceph_osd_event *event = NULL;
1738 while (*p) {
1739 parent = *p;
1740 event = rb_entry(parent, struct ceph_osd_event, node);
1741 if (cookie < event->cookie)
1742 p = &(*p)->rb_left;
1743 else if (cookie > event->cookie)
1744 p = &(*p)->rb_right;
1745 else
1746 return event;
1748 return NULL;
1751 static void __remove_event(struct ceph_osd_event *event)
1753 struct ceph_osd_client *osdc = event->osdc;
1755 if (!RB_EMPTY_NODE(&event->node)) {
1756 dout("__remove_event removed %p\n", event);
1757 rb_erase(&event->node, &osdc->event_tree);
1758 ceph_osdc_put_event(event);
1759 } else {
1760 dout("__remove_event didn't remove %p\n", event);
1764 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1765 void (*event_cb)(u64, u64, u8, void *),
1766 void *data, struct ceph_osd_event **pevent)
1768 struct ceph_osd_event *event;
1770 event = kmalloc(sizeof(*event), GFP_NOIO);
1771 if (!event)
1772 return -ENOMEM;
1774 dout("create_event %p\n", event);
1775 event->cb = event_cb;
1776 event->one_shot = 0;
1777 event->data = data;
1778 event->osdc = osdc;
1779 INIT_LIST_HEAD(&event->osd_node);
1780 RB_CLEAR_NODE(&event->node);
1781 kref_init(&event->kref); /* one ref for us */
1782 kref_get(&event->kref); /* one ref for the caller */
1784 spin_lock(&osdc->event_lock);
1785 event->cookie = ++osdc->event_count;
1786 __insert_event(osdc, event);
1787 spin_unlock(&osdc->event_lock);
1789 *pevent = event;
1790 return 0;
1792 EXPORT_SYMBOL(ceph_osdc_create_event);
1794 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1796 struct ceph_osd_client *osdc = event->osdc;
1798 dout("cancel_event %p\n", event);
1799 spin_lock(&osdc->event_lock);
1800 __remove_event(event);
1801 spin_unlock(&osdc->event_lock);
1802 ceph_osdc_put_event(event); /* caller's */
1804 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1807 static void do_event_work(struct work_struct *work)
1809 struct ceph_osd_event_work *event_work =
1810 container_of(work, struct ceph_osd_event_work, work);
1811 struct ceph_osd_event *event = event_work->event;
1812 u64 ver = event_work->ver;
1813 u64 notify_id = event_work->notify_id;
1814 u8 opcode = event_work->opcode;
1816 dout("do_event_work completing %p\n", event);
1817 event->cb(ver, notify_id, opcode, event->data);
1818 dout("do_event_work completed %p\n", event);
1819 ceph_osdc_put_event(event);
1820 kfree(event_work);
1825 * Process osd watch notifications
1827 static void handle_watch_notify(struct ceph_osd_client *osdc,
1828 struct ceph_msg *msg)
1830 void *p, *end;
1831 u8 proto_ver;
1832 u64 cookie, ver, notify_id;
1833 u8 opcode;
1834 struct ceph_osd_event *event;
1835 struct ceph_osd_event_work *event_work;
1837 p = msg->front.iov_base;
1838 end = p + msg->front.iov_len;
1840 ceph_decode_8_safe(&p, end, proto_ver, bad);
1841 ceph_decode_8_safe(&p, end, opcode, bad);
1842 ceph_decode_64_safe(&p, end, cookie, bad);
1843 ceph_decode_64_safe(&p, end, ver, bad);
1844 ceph_decode_64_safe(&p, end, notify_id, bad);
1846 spin_lock(&osdc->event_lock);
1847 event = __find_event(osdc, cookie);
1848 if (event) {
1849 BUG_ON(event->one_shot);
1850 get_event(event);
1852 spin_unlock(&osdc->event_lock);
1853 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1854 cookie, ver, event);
1855 if (event) {
1856 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1857 if (!event_work) {
1858 dout("ERROR: could not allocate event_work\n");
1859 goto done_err;
1861 INIT_WORK(&event_work->work, do_event_work);
1862 event_work->event = event;
1863 event_work->ver = ver;
1864 event_work->notify_id = notify_id;
1865 event_work->opcode = opcode;
1866 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1867 dout("WARNING: failed to queue notify event work\n");
1868 goto done_err;
1872 return;
1874 done_err:
1875 ceph_osdc_put_event(event);
1876 return;
1878 bad:
1879 pr_err("osdc handle_watch_notify corrupt msg\n");
1880 return;
1883 static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1884 struct ceph_osd_data *osd_data)
1886 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1887 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1888 if (osd_data->length)
1889 ceph_msg_data_set_pages(msg, osd_data->pages,
1890 osd_data->length, osd_data->alignment);
1891 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
1892 BUG_ON(!osd_data->pagelist->length);
1893 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
1894 #ifdef CONFIG_BLOCK
1895 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
1896 ceph_msg_data_set_bio(msg, osd_data->bio);
1897 #endif
1898 } else {
1899 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1904 * Register request, send initial attempt.
1906 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1907 struct ceph_osd_request *req,
1908 bool nofail)
1910 int rc = 0;
1912 /* Set up response incoming data and request outgoing data fields */
1914 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1915 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
1917 down_read(&osdc->map_sem);
1918 mutex_lock(&osdc->request_mutex);
1919 __register_request(osdc, req);
1920 WARN_ON(req->r_sent);
1921 rc = __map_request(osdc, req, 0);
1922 if (rc < 0) {
1923 if (nofail) {
1924 dout("osdc_start_request failed map, "
1925 " will retry %lld\n", req->r_tid);
1926 rc = 0;
1928 goto out_unlock;
1930 if (req->r_osd == NULL) {
1931 dout("send_request %p no up osds in pg\n", req);
1932 ceph_monc_request_next_osdmap(&osdc->client->monc);
1933 } else {
1934 __send_queued(osdc);
1936 rc = 0;
1937 out_unlock:
1938 mutex_unlock(&osdc->request_mutex);
1939 up_read(&osdc->map_sem);
1940 return rc;
1942 EXPORT_SYMBOL(ceph_osdc_start_request);
1945 * wait for a request to complete
1947 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1948 struct ceph_osd_request *req)
1950 int rc;
1952 rc = wait_for_completion_interruptible(&req->r_completion);
1953 if (rc < 0) {
1954 mutex_lock(&osdc->request_mutex);
1955 __cancel_request(req);
1956 __unregister_request(osdc, req);
1957 mutex_unlock(&osdc->request_mutex);
1958 complete_request(req);
1959 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1960 return rc;
1963 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1964 return req->r_result;
1966 EXPORT_SYMBOL(ceph_osdc_wait_request);
1969 * sync - wait for all in-flight requests to flush. avoid starvation.
1971 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1973 struct ceph_osd_request *req;
1974 u64 last_tid, next_tid = 0;
1976 mutex_lock(&osdc->request_mutex);
1977 last_tid = osdc->last_tid;
1978 while (1) {
1979 req = __lookup_request_ge(osdc, next_tid);
1980 if (!req)
1981 break;
1982 if (req->r_tid > last_tid)
1983 break;
1985 next_tid = req->r_tid + 1;
1986 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1987 continue;
1989 ceph_osdc_get_request(req);
1990 mutex_unlock(&osdc->request_mutex);
1991 dout("sync waiting on tid %llu (last is %llu)\n",
1992 req->r_tid, last_tid);
1993 wait_for_completion(&req->r_safe_completion);
1994 mutex_lock(&osdc->request_mutex);
1995 ceph_osdc_put_request(req);
1997 mutex_unlock(&osdc->request_mutex);
1998 dout("sync done (thru tid %llu)\n", last_tid);
2000 EXPORT_SYMBOL(ceph_osdc_sync);
2003 * init, shutdown
2005 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2007 int err;
2009 dout("init\n");
2010 osdc->client = client;
2011 osdc->osdmap = NULL;
2012 init_rwsem(&osdc->map_sem);
2013 init_completion(&osdc->map_waiters);
2014 osdc->last_requested_map = 0;
2015 mutex_init(&osdc->request_mutex);
2016 osdc->last_tid = 0;
2017 osdc->osds = RB_ROOT;
2018 INIT_LIST_HEAD(&osdc->osd_lru);
2019 osdc->requests = RB_ROOT;
2020 INIT_LIST_HEAD(&osdc->req_lru);
2021 INIT_LIST_HEAD(&osdc->req_unsent);
2022 INIT_LIST_HEAD(&osdc->req_notarget);
2023 INIT_LIST_HEAD(&osdc->req_linger);
2024 osdc->num_requests = 0;
2025 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2026 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2027 spin_lock_init(&osdc->event_lock);
2028 osdc->event_tree = RB_ROOT;
2029 osdc->event_count = 0;
2031 schedule_delayed_work(&osdc->osds_timeout_work,
2032 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2034 err = -ENOMEM;
2035 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2036 sizeof(struct ceph_osd_request));
2037 if (!osdc->req_mempool)
2038 goto out;
2040 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2041 OSD_OP_FRONT_LEN, 10, true,
2042 "osd_op");
2043 if (err < 0)
2044 goto out_mempool;
2045 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2046 OSD_OPREPLY_FRONT_LEN, 10, true,
2047 "osd_op_reply");
2048 if (err < 0)
2049 goto out_msgpool;
2051 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2052 if (IS_ERR(osdc->notify_wq)) {
2053 err = PTR_ERR(osdc->notify_wq);
2054 osdc->notify_wq = NULL;
2055 goto out_msgpool;
2057 return 0;
2059 out_msgpool:
2060 ceph_msgpool_destroy(&osdc->msgpool_op);
2061 out_mempool:
2062 mempool_destroy(osdc->req_mempool);
2063 out:
2064 return err;
2067 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2069 flush_workqueue(osdc->notify_wq);
2070 destroy_workqueue(osdc->notify_wq);
2071 cancel_delayed_work_sync(&osdc->timeout_work);
2072 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2073 if (osdc->osdmap) {
2074 ceph_osdmap_destroy(osdc->osdmap);
2075 osdc->osdmap = NULL;
2077 remove_all_osds(osdc);
2078 mempool_destroy(osdc->req_mempool);
2079 ceph_msgpool_destroy(&osdc->msgpool_op);
2080 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2084 * Read some contiguous pages. If we cross a stripe boundary, shorten
2085 * *plen. Return number of bytes read, or error.
2087 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2088 struct ceph_vino vino, struct ceph_file_layout *layout,
2089 u64 off, u64 *plen,
2090 u32 truncate_seq, u64 truncate_size,
2091 struct page **pages, int num_pages, int page_align)
2093 struct ceph_osd_request *req;
2094 struct ceph_osd_data *osd_data;
2095 int rc = 0;
2097 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2098 vino.snap, off, *plen);
2099 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
2100 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2101 NULL, 0, truncate_seq, truncate_size, NULL,
2102 false);
2103 if (IS_ERR(req))
2104 return PTR_ERR(req);
2106 /* it may be a short read due to an object boundary */
2108 osd_data = &req->r_data_in;
2109 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2110 osd_data->pages = pages;
2111 osd_data->length = *plen;
2112 osd_data->alignment = page_align;
2114 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2115 off, *plen, osd_data->length, page_align);
2117 rc = ceph_osdc_start_request(osdc, req, false);
2118 if (!rc)
2119 rc = ceph_osdc_wait_request(osdc, req);
2121 ceph_osdc_put_request(req);
2122 dout("readpages result %d\n", rc);
2123 return rc;
2125 EXPORT_SYMBOL(ceph_osdc_readpages);
2128 * do a synchronous write on N pages
2130 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2131 struct ceph_file_layout *layout,
2132 struct ceph_snap_context *snapc,
2133 u64 off, u64 len,
2134 u32 truncate_seq, u64 truncate_size,
2135 struct timespec *mtime,
2136 struct page **pages, int num_pages)
2138 struct ceph_osd_request *req;
2139 struct ceph_osd_data *osd_data;
2140 int rc = 0;
2141 int page_align = off & ~PAGE_MASK;
2143 BUG_ON(vino.snap != CEPH_NOSNAP);
2144 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
2145 CEPH_OSD_OP_WRITE,
2146 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2147 snapc, 0,
2148 truncate_seq, truncate_size, mtime,
2149 true);
2150 if (IS_ERR(req))
2151 return PTR_ERR(req);
2153 /* it may be a short write due to an object boundary */
2154 osd_data = &req->r_data_out;
2155 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2156 osd_data->pages = pages;
2157 osd_data->length = len;
2158 osd_data->alignment = page_align;
2159 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
2161 rc = ceph_osdc_start_request(osdc, req, true);
2162 if (!rc)
2163 rc = ceph_osdc_wait_request(osdc, req);
2165 ceph_osdc_put_request(req);
2166 if (rc == 0)
2167 rc = len;
2168 dout("writepages result %d\n", rc);
2169 return rc;
2171 EXPORT_SYMBOL(ceph_osdc_writepages);
2174 * handle incoming message
2176 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2178 struct ceph_osd *osd = con->private;
2179 struct ceph_osd_client *osdc;
2180 int type = le16_to_cpu(msg->hdr.type);
2182 if (!osd)
2183 goto out;
2184 osdc = osd->o_osdc;
2186 switch (type) {
2187 case CEPH_MSG_OSD_MAP:
2188 ceph_osdc_handle_map(osdc, msg);
2189 break;
2190 case CEPH_MSG_OSD_OPREPLY:
2191 handle_reply(osdc, msg, con);
2192 break;
2193 case CEPH_MSG_WATCH_NOTIFY:
2194 handle_watch_notify(osdc, msg);
2195 break;
2197 default:
2198 pr_err("received unknown message type %d %s\n", type,
2199 ceph_msg_type_name(type));
2201 out:
2202 ceph_msg_put(msg);
2206 * lookup and return message for incoming reply. set up reply message
2207 * pages.
2209 static struct ceph_msg *get_reply(struct ceph_connection *con,
2210 struct ceph_msg_header *hdr,
2211 int *skip)
2213 struct ceph_osd *osd = con->private;
2214 struct ceph_osd_client *osdc = osd->o_osdc;
2215 struct ceph_msg *m;
2216 struct ceph_osd_request *req;
2217 int front = le32_to_cpu(hdr->front_len);
2218 int data_len = le32_to_cpu(hdr->data_len);
2219 u64 tid;
2221 tid = le64_to_cpu(hdr->tid);
2222 mutex_lock(&osdc->request_mutex);
2223 req = __lookup_request(osdc, tid);
2224 if (!req) {
2225 *skip = 1;
2226 m = NULL;
2227 dout("get_reply unknown tid %llu from osd%d\n", tid,
2228 osd->o_osd);
2229 goto out;
2232 if (req->r_con_filling_msg) {
2233 dout("%s revoking msg %p from old con %p\n", __func__,
2234 req->r_reply, req->r_con_filling_msg);
2235 ceph_msg_revoke_incoming(req->r_reply);
2236 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2237 req->r_con_filling_msg = NULL;
2240 if (front > req->r_reply->front.iov_len) {
2241 pr_warning("get_reply front %d > preallocated %d\n",
2242 front, (int)req->r_reply->front.iov_len);
2243 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2244 if (!m)
2245 goto out;
2246 ceph_msg_put(req->r_reply);
2247 req->r_reply = m;
2249 m = ceph_msg_get(req->r_reply);
2251 if (data_len > 0) {
2252 struct ceph_osd_data *osd_data = &req->r_data_in;
2254 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2255 if (osd_data->pages &&
2256 unlikely(osd_data->length < data_len)) {
2258 pr_warning("tid %lld reply has %d bytes "
2259 "we had only %llu bytes ready\n",
2260 tid, data_len, osd_data->length);
2261 *skip = 1;
2262 ceph_msg_put(m);
2263 m = NULL;
2264 goto out;
2268 *skip = 0;
2269 req->r_con_filling_msg = con->ops->get(con);
2270 dout("get_reply tid %lld %p\n", tid, m);
2272 out:
2273 mutex_unlock(&osdc->request_mutex);
2274 return m;
2278 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2279 struct ceph_msg_header *hdr,
2280 int *skip)
2282 struct ceph_osd *osd = con->private;
2283 int type = le16_to_cpu(hdr->type);
2284 int front = le32_to_cpu(hdr->front_len);
2286 *skip = 0;
2287 switch (type) {
2288 case CEPH_MSG_OSD_MAP:
2289 case CEPH_MSG_WATCH_NOTIFY:
2290 return ceph_msg_new(type, front, GFP_NOFS, false);
2291 case CEPH_MSG_OSD_OPREPLY:
2292 return get_reply(con, hdr, skip);
2293 default:
2294 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2295 osd->o_osd);
2296 *skip = 1;
2297 return NULL;
2302 * Wrappers to refcount containing ceph_osd struct
2304 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2306 struct ceph_osd *osd = con->private;
2307 if (get_osd(osd))
2308 return con;
2309 return NULL;
2312 static void put_osd_con(struct ceph_connection *con)
2314 struct ceph_osd *osd = con->private;
2315 put_osd(osd);
2319 * authentication
2322 * Note: returned pointer is the address of a structure that's
2323 * managed separately. Caller must *not* attempt to free it.
2325 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2326 int *proto, int force_new)
2328 struct ceph_osd *o = con->private;
2329 struct ceph_osd_client *osdc = o->o_osdc;
2330 struct ceph_auth_client *ac = osdc->client->monc.auth;
2331 struct ceph_auth_handshake *auth = &o->o_auth;
2333 if (force_new && auth->authorizer) {
2334 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2335 auth->authorizer = NULL;
2337 if (!auth->authorizer) {
2338 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2339 auth);
2340 if (ret)
2341 return ERR_PTR(ret);
2342 } else {
2343 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2344 auth);
2345 if (ret)
2346 return ERR_PTR(ret);
2348 *proto = ac->protocol;
2350 return auth;
2354 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2356 struct ceph_osd *o = con->private;
2357 struct ceph_osd_client *osdc = o->o_osdc;
2358 struct ceph_auth_client *ac = osdc->client->monc.auth;
2360 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2363 static int invalidate_authorizer(struct ceph_connection *con)
2365 struct ceph_osd *o = con->private;
2366 struct ceph_osd_client *osdc = o->o_osdc;
2367 struct ceph_auth_client *ac = osdc->client->monc.auth;
2369 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2370 return ceph_monc_validate_auth(&osdc->client->monc);
2373 static const struct ceph_connection_operations osd_con_ops = {
2374 .get = get_osd_con,
2375 .put = put_osd_con,
2376 .dispatch = dispatch,
2377 .get_authorizer = get_authorizer,
2378 .verify_authorizer_reply = verify_authorizer_reply,
2379 .invalidate_authorizer = invalidate_authorizer,
2380 .alloc_msg = alloc_msg,
2381 .fault = osd_reset,