libceph: change how osd_op_reply message size is calculated
[deliverable/linux.git] / net / ceph / osd_client.c
1
2 #include <linux/ceph/ceph_debug.h>
3
4 #include <linux/module.h>
5 #include <linux/err.h>
6 #include <linux/highmem.h>
7 #include <linux/mm.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
11 #ifdef CONFIG_BLOCK
12 #include <linux/bio.h>
13 #endif
14
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>
21
22 #define OSD_OPREPLY_FRONT_LEN 512
23
24 static struct kmem_cache *ceph_osd_request_cache;
25
26 static const struct ceph_connection_operations osd_con_ops;
27
28 static void __send_queued(struct ceph_osd_client *osdc);
29 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
30 static void __register_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
32 static void __unregister_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
34 static void __unregister_linger_request(struct ceph_osd_client *osdc,
35 struct ceph_osd_request *req);
36 static void __enqueue_request(struct ceph_osd_request *req);
37 static void __send_request(struct ceph_osd_client *osdc,
38 struct ceph_osd_request *req);
39
40 /*
41 * Implement client access to distributed object storage cluster.
42 *
43 * All data objects are stored within a cluster/cloud of OSDs, or
44 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
45 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
46 * remote daemons serving up and coordinating consistent and safe
47 * access to storage.
48 *
49 * Cluster membership and the mapping of data objects onto storage devices
50 * are described by the osd map.
51 *
52 * We keep track of pending OSD requests (read, write), resubmit
53 * requests to different OSDs when the cluster topology/data layout
54 * change, or retry the affected requests when the communications
55 * channel with an OSD is reset.
56 */
57
58 /*
59 * calculate the mapping of a file extent onto an object, and fill out the
60 * request accordingly. shorten extent as necessary if it crosses an
61 * object boundary.
62 *
63 * fill osd op in request message.
64 */
65 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
66 u64 *objnum, u64 *objoff, u64 *objlen)
67 {
68 u64 orig_len = *plen;
69 int r;
70
71 /* object extent? */
72 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
73 objoff, objlen);
74 if (r < 0)
75 return r;
76 if (*objlen < orig_len) {
77 *plen = *objlen;
78 dout(" skipping last %llu, final file extent %llu~%llu\n",
79 orig_len - *plen, off, *plen);
80 }
81
82 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
83
84 return 0;
85 }
86
87 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
88 {
89 memset(osd_data, 0, sizeof (*osd_data));
90 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
91 }
92
93 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
94 struct page **pages, u64 length, u32 alignment,
95 bool pages_from_pool, bool own_pages)
96 {
97 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
98 osd_data->pages = pages;
99 osd_data->length = length;
100 osd_data->alignment = alignment;
101 osd_data->pages_from_pool = pages_from_pool;
102 osd_data->own_pages = own_pages;
103 }
104
105 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
106 struct ceph_pagelist *pagelist)
107 {
108 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
109 osd_data->pagelist = pagelist;
110 }
111
112 #ifdef CONFIG_BLOCK
113 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
114 struct bio *bio, size_t bio_length)
115 {
116 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
117 osd_data->bio = bio;
118 osd_data->bio_length = bio_length;
119 }
120 #endif /* CONFIG_BLOCK */
121
122 #define osd_req_op_data(oreq, whch, typ, fld) \
123 ({ \
124 struct ceph_osd_request *__oreq = (oreq); \
125 unsigned int __whch = (whch); \
126 BUG_ON(__whch >= __oreq->r_num_ops); \
127 &__oreq->r_ops[__whch].typ.fld; \
128 })
129
130 static struct ceph_osd_data *
131 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
132 {
133 BUG_ON(which >= osd_req->r_num_ops);
134
135 return &osd_req->r_ops[which].raw_data_in;
136 }
137
138 struct ceph_osd_data *
139 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
140 unsigned int which)
141 {
142 return osd_req_op_data(osd_req, which, extent, osd_data);
143 }
144 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
145
146 struct ceph_osd_data *
147 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
148 unsigned int which)
149 {
150 return osd_req_op_data(osd_req, which, cls, response_data);
151 }
152 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
153
154 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
155 unsigned int which, struct page **pages,
156 u64 length, u32 alignment,
157 bool pages_from_pool, bool own_pages)
158 {
159 struct ceph_osd_data *osd_data;
160
161 osd_data = osd_req_op_raw_data_in(osd_req, which);
162 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
163 pages_from_pool, own_pages);
164 }
165 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
166
167 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
168 unsigned int which, struct page **pages,
169 u64 length, u32 alignment,
170 bool pages_from_pool, bool own_pages)
171 {
172 struct ceph_osd_data *osd_data;
173
174 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
175 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
176 pages_from_pool, own_pages);
177 }
178 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
179
180 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
181 unsigned int which, struct ceph_pagelist *pagelist)
182 {
183 struct ceph_osd_data *osd_data;
184
185 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
186 ceph_osd_data_pagelist_init(osd_data, pagelist);
187 }
188 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
189
190 #ifdef CONFIG_BLOCK
191 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
192 unsigned int which, struct bio *bio, size_t bio_length)
193 {
194 struct ceph_osd_data *osd_data;
195
196 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
197 ceph_osd_data_bio_init(osd_data, bio, bio_length);
198 }
199 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
200 #endif /* CONFIG_BLOCK */
201
202 static void osd_req_op_cls_request_info_pagelist(
203 struct ceph_osd_request *osd_req,
204 unsigned int which, struct ceph_pagelist *pagelist)
205 {
206 struct ceph_osd_data *osd_data;
207
208 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
209 ceph_osd_data_pagelist_init(osd_data, pagelist);
210 }
211
212 void osd_req_op_cls_request_data_pagelist(
213 struct ceph_osd_request *osd_req,
214 unsigned int which, struct ceph_pagelist *pagelist)
215 {
216 struct ceph_osd_data *osd_data;
217
218 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
219 ceph_osd_data_pagelist_init(osd_data, pagelist);
220 }
221 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
222
223 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
224 unsigned int which, struct page **pages, u64 length,
225 u32 alignment, bool pages_from_pool, bool own_pages)
226 {
227 struct ceph_osd_data *osd_data;
228
229 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
230 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
231 pages_from_pool, own_pages);
232 }
233 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
234
235 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
236 unsigned int which, struct page **pages, u64 length,
237 u32 alignment, bool pages_from_pool, bool own_pages)
238 {
239 struct ceph_osd_data *osd_data;
240
241 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
242 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
243 pages_from_pool, own_pages);
244 }
245 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
246
247 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
248 {
249 switch (osd_data->type) {
250 case CEPH_OSD_DATA_TYPE_NONE:
251 return 0;
252 case CEPH_OSD_DATA_TYPE_PAGES:
253 return osd_data->length;
254 case CEPH_OSD_DATA_TYPE_PAGELIST:
255 return (u64)osd_data->pagelist->length;
256 #ifdef CONFIG_BLOCK
257 case CEPH_OSD_DATA_TYPE_BIO:
258 return (u64)osd_data->bio_length;
259 #endif /* CONFIG_BLOCK */
260 default:
261 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
262 return 0;
263 }
264 }
265
266 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
267 {
268 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
269 int num_pages;
270
271 num_pages = calc_pages_for((u64)osd_data->alignment,
272 (u64)osd_data->length);
273 ceph_release_page_vector(osd_data->pages, num_pages);
274 }
275 ceph_osd_data_init(osd_data);
276 }
277
278 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
279 unsigned int which)
280 {
281 struct ceph_osd_req_op *op;
282
283 BUG_ON(which >= osd_req->r_num_ops);
284 op = &osd_req->r_ops[which];
285
286 switch (op->op) {
287 case CEPH_OSD_OP_READ:
288 case CEPH_OSD_OP_WRITE:
289 case CEPH_OSD_OP_WRITEFULL:
290 ceph_osd_data_release(&op->extent.osd_data);
291 break;
292 case CEPH_OSD_OP_CALL:
293 ceph_osd_data_release(&op->cls.request_info);
294 ceph_osd_data_release(&op->cls.request_data);
295 ceph_osd_data_release(&op->cls.response_data);
296 break;
297 case CEPH_OSD_OP_SETXATTR:
298 case CEPH_OSD_OP_CMPXATTR:
299 ceph_osd_data_release(&op->xattr.osd_data);
300 break;
301 case CEPH_OSD_OP_STAT:
302 ceph_osd_data_release(&op->raw_data_in);
303 break;
304 default:
305 break;
306 }
307 }
308
309 /*
310 * requests
311 */
312 static void ceph_osdc_release_request(struct kref *kref)
313 {
314 struct ceph_osd_request *req = container_of(kref,
315 struct ceph_osd_request, r_kref);
316 unsigned int which;
317
318 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
319 req->r_request, req->r_reply);
320 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
321 WARN_ON(!list_empty(&req->r_req_lru_item));
322 WARN_ON(!list_empty(&req->r_osd_item));
323 WARN_ON(!list_empty(&req->r_linger_item));
324 WARN_ON(!list_empty(&req->r_linger_osd_item));
325 WARN_ON(req->r_osd);
326
327 if (req->r_request)
328 ceph_msg_put(req->r_request);
329 if (req->r_reply) {
330 ceph_msg_revoke_incoming(req->r_reply);
331 ceph_msg_put(req->r_reply);
332 }
333
334 for (which = 0; which < req->r_num_ops; which++)
335 osd_req_op_data_release(req, which);
336
337 ceph_put_snap_context(req->r_snapc);
338 if (req->r_mempool)
339 mempool_free(req, req->r_osdc->req_mempool);
340 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
341 kmem_cache_free(ceph_osd_request_cache, req);
342 else
343 kfree(req);
344 }
345
346 void ceph_osdc_get_request(struct ceph_osd_request *req)
347 {
348 dout("%s %p (was %d)\n", __func__, req,
349 atomic_read(&req->r_kref.refcount));
350 kref_get(&req->r_kref);
351 }
352 EXPORT_SYMBOL(ceph_osdc_get_request);
353
354 void ceph_osdc_put_request(struct ceph_osd_request *req)
355 {
356 if (req) {
357 dout("%s %p (was %d)\n", __func__, req,
358 atomic_read(&req->r_kref.refcount));
359 kref_put(&req->r_kref, ceph_osdc_release_request);
360 }
361 }
362 EXPORT_SYMBOL(ceph_osdc_put_request);
363
364 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
365 struct ceph_snap_context *snapc,
366 unsigned int num_ops,
367 bool use_mempool,
368 gfp_t gfp_flags)
369 {
370 struct ceph_osd_request *req;
371
372 if (use_mempool) {
373 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
374 req = mempool_alloc(osdc->req_mempool, gfp_flags);
375 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
376 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
377 } else {
378 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
379 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
380 gfp_flags);
381 }
382 if (unlikely(!req))
383 return NULL;
384
385 /* req only, each op is zeroed in _osd_req_op_init() */
386 memset(req, 0, sizeof(*req));
387
388 req->r_osdc = osdc;
389 req->r_mempool = use_mempool;
390 req->r_num_ops = num_ops;
391 req->r_snapid = CEPH_NOSNAP;
392 req->r_snapc = ceph_get_snap_context(snapc);
393
394 kref_init(&req->r_kref);
395 init_completion(&req->r_completion);
396 init_completion(&req->r_safe_completion);
397 RB_CLEAR_NODE(&req->r_node);
398 INIT_LIST_HEAD(&req->r_unsafe_item);
399 INIT_LIST_HEAD(&req->r_linger_item);
400 INIT_LIST_HEAD(&req->r_linger_osd_item);
401 INIT_LIST_HEAD(&req->r_req_lru_item);
402 INIT_LIST_HEAD(&req->r_osd_item);
403
404 req->r_base_oloc.pool = -1;
405 req->r_target_oloc.pool = -1;
406
407 dout("%s req %p\n", __func__, req);
408 return req;
409 }
410 EXPORT_SYMBOL(ceph_osdc_alloc_request);
411
412 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
413 {
414 struct ceph_osd_client *osdc = req->r_osdc;
415 struct ceph_msg *msg;
416 int msg_size;
417
418 /* create request message */
419 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
420 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
421 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
422 msg_size += 1 + 8 + 4 + 4; /* pgid */
423 msg_size += 4 + req->r_base_oid.name_len; /* oid */
424 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
425 msg_size += 8; /* snapid */
426 msg_size += 8; /* snap_seq */
427 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
428 msg_size += 4; /* retry_attempt */
429
430 if (req->r_mempool)
431 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
432 else
433 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
434 if (!msg)
435 return -ENOMEM;
436
437 memset(msg->front.iov_base, 0, msg->front.iov_len);
438 req->r_request = msg;
439
440 /* create reply message */
441 msg_size = OSD_OPREPLY_FRONT_LEN;
442 msg_size += req->r_base_oid.name_len;
443 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
444
445 if (req->r_mempool)
446 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
447 else
448 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
449 if (!msg)
450 return -ENOMEM;
451
452 req->r_reply = msg;
453
454 return 0;
455 }
456 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
457
458 static bool osd_req_opcode_valid(u16 opcode)
459 {
460 switch (opcode) {
461 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
462 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
463 #undef GENERATE_CASE
464 default:
465 return false;
466 }
467 }
468
469 /*
470 * This is an osd op init function for opcodes that have no data or
471 * other information associated with them. It also serves as a
472 * common init routine for all the other init functions, below.
473 */
474 static struct ceph_osd_req_op *
475 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
476 u16 opcode, u32 flags)
477 {
478 struct ceph_osd_req_op *op;
479
480 BUG_ON(which >= osd_req->r_num_ops);
481 BUG_ON(!osd_req_opcode_valid(opcode));
482
483 op = &osd_req->r_ops[which];
484 memset(op, 0, sizeof (*op));
485 op->op = opcode;
486 op->flags = flags;
487
488 return op;
489 }
490
491 void osd_req_op_init(struct ceph_osd_request *osd_req,
492 unsigned int which, u16 opcode, u32 flags)
493 {
494 (void)_osd_req_op_init(osd_req, which, opcode, flags);
495 }
496 EXPORT_SYMBOL(osd_req_op_init);
497
498 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
499 unsigned int which, u16 opcode,
500 u64 offset, u64 length,
501 u64 truncate_size, u32 truncate_seq)
502 {
503 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
504 opcode, 0);
505 size_t payload_len = 0;
506
507 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
508 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
509 opcode != CEPH_OSD_OP_TRUNCATE);
510
511 op->extent.offset = offset;
512 op->extent.length = length;
513 op->extent.truncate_size = truncate_size;
514 op->extent.truncate_seq = truncate_seq;
515 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
516 payload_len += length;
517
518 op->indata_len = payload_len;
519 }
520 EXPORT_SYMBOL(osd_req_op_extent_init);
521
522 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
523 unsigned int which, u64 length)
524 {
525 struct ceph_osd_req_op *op;
526 u64 previous;
527
528 BUG_ON(which >= osd_req->r_num_ops);
529 op = &osd_req->r_ops[which];
530 previous = op->extent.length;
531
532 if (length == previous)
533 return; /* Nothing to do */
534 BUG_ON(length > previous);
535
536 op->extent.length = length;
537 op->indata_len -= previous - length;
538 }
539 EXPORT_SYMBOL(osd_req_op_extent_update);
540
541 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
542 unsigned int which, u64 offset_inc)
543 {
544 struct ceph_osd_req_op *op, *prev_op;
545
546 BUG_ON(which + 1 >= osd_req->r_num_ops);
547
548 prev_op = &osd_req->r_ops[which];
549 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
550 /* dup previous one */
551 op->indata_len = prev_op->indata_len;
552 op->outdata_len = prev_op->outdata_len;
553 op->extent = prev_op->extent;
554 /* adjust offset */
555 op->extent.offset += offset_inc;
556 op->extent.length -= offset_inc;
557
558 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
559 op->indata_len -= offset_inc;
560 }
561 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
562
563 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
564 u16 opcode, const char *class, const char *method)
565 {
566 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
567 opcode, 0);
568 struct ceph_pagelist *pagelist;
569 size_t payload_len = 0;
570 size_t size;
571
572 BUG_ON(opcode != CEPH_OSD_OP_CALL);
573
574 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
575 BUG_ON(!pagelist);
576 ceph_pagelist_init(pagelist);
577
578 op->cls.class_name = class;
579 size = strlen(class);
580 BUG_ON(size > (size_t) U8_MAX);
581 op->cls.class_len = size;
582 ceph_pagelist_append(pagelist, class, size);
583 payload_len += size;
584
585 op->cls.method_name = method;
586 size = strlen(method);
587 BUG_ON(size > (size_t) U8_MAX);
588 op->cls.method_len = size;
589 ceph_pagelist_append(pagelist, method, size);
590 payload_len += size;
591
592 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
593
594 op->cls.argc = 0; /* currently unused */
595
596 op->indata_len = payload_len;
597 }
598 EXPORT_SYMBOL(osd_req_op_cls_init);
599
600 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
601 u16 opcode, const char *name, const void *value,
602 size_t size, u8 cmp_op, u8 cmp_mode)
603 {
604 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
605 opcode, 0);
606 struct ceph_pagelist *pagelist;
607 size_t payload_len;
608
609 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
610
611 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
612 if (!pagelist)
613 return -ENOMEM;
614
615 ceph_pagelist_init(pagelist);
616
617 payload_len = strlen(name);
618 op->xattr.name_len = payload_len;
619 ceph_pagelist_append(pagelist, name, payload_len);
620
621 op->xattr.value_len = size;
622 ceph_pagelist_append(pagelist, value, size);
623 payload_len += size;
624
625 op->xattr.cmp_op = cmp_op;
626 op->xattr.cmp_mode = cmp_mode;
627
628 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
629 op->indata_len = payload_len;
630 return 0;
631 }
632 EXPORT_SYMBOL(osd_req_op_xattr_init);
633
634 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
635 unsigned int which, u16 opcode,
636 u64 cookie, u64 version, int flag)
637 {
638 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
639 opcode, 0);
640
641 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
642
643 op->watch.cookie = cookie;
644 op->watch.ver = version;
645 if (opcode == CEPH_OSD_OP_WATCH && flag)
646 op->watch.flag = (u8)1;
647 }
648 EXPORT_SYMBOL(osd_req_op_watch_init);
649
650 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
651 unsigned int which,
652 u64 expected_object_size,
653 u64 expected_write_size)
654 {
655 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
656 CEPH_OSD_OP_SETALLOCHINT,
657 0);
658
659 op->alloc_hint.expected_object_size = expected_object_size;
660 op->alloc_hint.expected_write_size = expected_write_size;
661
662 /*
663 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
664 * not worth a feature bit. Set FAILOK per-op flag to make
665 * sure older osds don't trip over an unsupported opcode.
666 */
667 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
668 }
669 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
670
671 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
672 struct ceph_osd_data *osd_data)
673 {
674 u64 length = ceph_osd_data_length(osd_data);
675
676 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
677 BUG_ON(length > (u64) SIZE_MAX);
678 if (length)
679 ceph_msg_data_add_pages(msg, osd_data->pages,
680 length, osd_data->alignment);
681 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
682 BUG_ON(!length);
683 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
684 #ifdef CONFIG_BLOCK
685 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
686 ceph_msg_data_add_bio(msg, osd_data->bio, length);
687 #endif
688 } else {
689 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
690 }
691 }
692
693 static u64 osd_req_encode_op(struct ceph_osd_request *req,
694 struct ceph_osd_op *dst, unsigned int which)
695 {
696 struct ceph_osd_req_op *src;
697 struct ceph_osd_data *osd_data;
698 u64 request_data_len = 0;
699 u64 data_length;
700
701 BUG_ON(which >= req->r_num_ops);
702 src = &req->r_ops[which];
703 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
704 pr_err("unrecognized osd opcode %d\n", src->op);
705
706 return 0;
707 }
708
709 switch (src->op) {
710 case CEPH_OSD_OP_STAT:
711 osd_data = &src->raw_data_in;
712 ceph_osdc_msg_data_add(req->r_reply, osd_data);
713 break;
714 case CEPH_OSD_OP_READ:
715 case CEPH_OSD_OP_WRITE:
716 case CEPH_OSD_OP_WRITEFULL:
717 case CEPH_OSD_OP_ZERO:
718 case CEPH_OSD_OP_TRUNCATE:
719 if (src->op == CEPH_OSD_OP_WRITE ||
720 src->op == CEPH_OSD_OP_WRITEFULL)
721 request_data_len = src->extent.length;
722 dst->extent.offset = cpu_to_le64(src->extent.offset);
723 dst->extent.length = cpu_to_le64(src->extent.length);
724 dst->extent.truncate_size =
725 cpu_to_le64(src->extent.truncate_size);
726 dst->extent.truncate_seq =
727 cpu_to_le32(src->extent.truncate_seq);
728 osd_data = &src->extent.osd_data;
729 if (src->op == CEPH_OSD_OP_WRITE ||
730 src->op == CEPH_OSD_OP_WRITEFULL)
731 ceph_osdc_msg_data_add(req->r_request, osd_data);
732 else
733 ceph_osdc_msg_data_add(req->r_reply, osd_data);
734 break;
735 case CEPH_OSD_OP_CALL:
736 dst->cls.class_len = src->cls.class_len;
737 dst->cls.method_len = src->cls.method_len;
738 osd_data = &src->cls.request_info;
739 ceph_osdc_msg_data_add(req->r_request, osd_data);
740 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
741 request_data_len = osd_data->pagelist->length;
742
743 osd_data = &src->cls.request_data;
744 data_length = ceph_osd_data_length(osd_data);
745 if (data_length) {
746 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
747 dst->cls.indata_len = cpu_to_le32(data_length);
748 ceph_osdc_msg_data_add(req->r_request, osd_data);
749 src->indata_len += data_length;
750 request_data_len += data_length;
751 }
752 osd_data = &src->cls.response_data;
753 ceph_osdc_msg_data_add(req->r_reply, osd_data);
754 break;
755 case CEPH_OSD_OP_STARTSYNC:
756 break;
757 case CEPH_OSD_OP_NOTIFY_ACK:
758 case CEPH_OSD_OP_WATCH:
759 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
760 dst->watch.ver = cpu_to_le64(src->watch.ver);
761 dst->watch.flag = src->watch.flag;
762 break;
763 case CEPH_OSD_OP_SETALLOCHINT:
764 dst->alloc_hint.expected_object_size =
765 cpu_to_le64(src->alloc_hint.expected_object_size);
766 dst->alloc_hint.expected_write_size =
767 cpu_to_le64(src->alloc_hint.expected_write_size);
768 break;
769 case CEPH_OSD_OP_SETXATTR:
770 case CEPH_OSD_OP_CMPXATTR:
771 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
772 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
773 dst->xattr.cmp_op = src->xattr.cmp_op;
774 dst->xattr.cmp_mode = src->xattr.cmp_mode;
775 osd_data = &src->xattr.osd_data;
776 ceph_osdc_msg_data_add(req->r_request, osd_data);
777 request_data_len = osd_data->pagelist->length;
778 break;
779 case CEPH_OSD_OP_CREATE:
780 case CEPH_OSD_OP_DELETE:
781 break;
782 default:
783 pr_err("unsupported osd opcode %s\n",
784 ceph_osd_op_name(src->op));
785 WARN_ON(1);
786
787 return 0;
788 }
789
790 dst->op = cpu_to_le16(src->op);
791 dst->flags = cpu_to_le32(src->flags);
792 dst->payload_len = cpu_to_le32(src->indata_len);
793
794 return request_data_len;
795 }
796
797 /*
798 * build new request AND message, calculate layout, and adjust file
799 * extent as needed.
800 *
801 * if the file was recently truncated, we include information about its
802 * old and new size so that the object can be updated appropriately. (we
803 * avoid synchronously deleting truncated objects because it's slow.)
804 *
805 * if @do_sync, include a 'startsync' command so that the osd will flush
806 * data quickly.
807 */
808 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
809 struct ceph_file_layout *layout,
810 struct ceph_vino vino,
811 u64 off, u64 *plen,
812 unsigned int which, int num_ops,
813 int opcode, int flags,
814 struct ceph_snap_context *snapc,
815 u32 truncate_seq,
816 u64 truncate_size,
817 bool use_mempool)
818 {
819 struct ceph_osd_request *req;
820 u64 objnum = 0;
821 u64 objoff = 0;
822 u64 objlen = 0;
823 int r;
824
825 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
826 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
827 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
828
829 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
830 GFP_NOFS);
831 if (!req) {
832 r = -ENOMEM;
833 goto fail;
834 }
835
836 req->r_flags = flags;
837
838 /* calculate max write size */
839 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
840 if (r)
841 goto fail;
842
843 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
844 osd_req_op_init(req, which, opcode, 0);
845 } else {
846 u32 object_size = le32_to_cpu(layout->fl_object_size);
847 u32 object_base = off - objoff;
848 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
849 if (truncate_size <= object_base) {
850 truncate_size = 0;
851 } else {
852 truncate_size -= object_base;
853 if (truncate_size > object_size)
854 truncate_size = object_size;
855 }
856 }
857 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
858 truncate_size, truncate_seq);
859 }
860
861 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
862
863 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
864 "%llx.%08llx", vino.ino, objnum);
865 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
866
867 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
868 if (r)
869 goto fail;
870
871 return req;
872
873 fail:
874 ceph_osdc_put_request(req);
875 return ERR_PTR(r);
876 }
877 EXPORT_SYMBOL(ceph_osdc_new_request);
878
879 /*
880 * We keep osd requests in an rbtree, sorted by ->r_tid.
881 */
882 static void __insert_request(struct ceph_osd_client *osdc,
883 struct ceph_osd_request *new)
884 {
885 struct rb_node **p = &osdc->requests.rb_node;
886 struct rb_node *parent = NULL;
887 struct ceph_osd_request *req = NULL;
888
889 while (*p) {
890 parent = *p;
891 req = rb_entry(parent, struct ceph_osd_request, r_node);
892 if (new->r_tid < req->r_tid)
893 p = &(*p)->rb_left;
894 else if (new->r_tid > req->r_tid)
895 p = &(*p)->rb_right;
896 else
897 BUG();
898 }
899
900 rb_link_node(&new->r_node, parent, p);
901 rb_insert_color(&new->r_node, &osdc->requests);
902 }
903
904 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
905 u64 tid)
906 {
907 struct ceph_osd_request *req;
908 struct rb_node *n = osdc->requests.rb_node;
909
910 while (n) {
911 req = rb_entry(n, struct ceph_osd_request, r_node);
912 if (tid < req->r_tid)
913 n = n->rb_left;
914 else if (tid > req->r_tid)
915 n = n->rb_right;
916 else
917 return req;
918 }
919 return NULL;
920 }
921
922 static struct ceph_osd_request *
923 __lookup_request_ge(struct ceph_osd_client *osdc,
924 u64 tid)
925 {
926 struct ceph_osd_request *req;
927 struct rb_node *n = osdc->requests.rb_node;
928
929 while (n) {
930 req = rb_entry(n, struct ceph_osd_request, r_node);
931 if (tid < req->r_tid) {
932 if (!n->rb_left)
933 return req;
934 n = n->rb_left;
935 } else if (tid > req->r_tid) {
936 n = n->rb_right;
937 } else {
938 return req;
939 }
940 }
941 return NULL;
942 }
943
944 static void __kick_linger_request(struct ceph_osd_request *req)
945 {
946 struct ceph_osd_client *osdc = req->r_osdc;
947 struct ceph_osd *osd = req->r_osd;
948
949 /*
950 * Linger requests need to be resent with a new tid to avoid
951 * the dup op detection logic on the OSDs. Achieve this with
952 * a re-register dance instead of open-coding.
953 */
954 ceph_osdc_get_request(req);
955 if (!list_empty(&req->r_linger_item))
956 __unregister_linger_request(osdc, req);
957 else
958 __unregister_request(osdc, req);
959 __register_request(osdc, req);
960 ceph_osdc_put_request(req);
961
962 /*
963 * Unless request has been registered as both normal and
964 * lingering, __unregister{,_linger}_request clears r_osd.
965 * However, here we need to preserve r_osd to make sure we
966 * requeue on the same OSD.
967 */
968 WARN_ON(req->r_osd || !osd);
969 req->r_osd = osd;
970
971 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
972 __enqueue_request(req);
973 }
974
975 /*
976 * Resubmit requests pending on the given osd.
977 */
978 static void __kick_osd_requests(struct ceph_osd_client *osdc,
979 struct ceph_osd *osd)
980 {
981 struct ceph_osd_request *req, *nreq;
982 LIST_HEAD(resend);
983 LIST_HEAD(resend_linger);
984 int err;
985
986 dout("%s osd%d\n", __func__, osd->o_osd);
987 err = __reset_osd(osdc, osd);
988 if (err)
989 return;
990
991 /*
992 * Build up a list of requests to resend by traversing the
993 * osd's list of requests. Requests for a given object are
994 * sent in tid order, and that is also the order they're
995 * kept on this list. Therefore all requests that are in
996 * flight will be found first, followed by all requests that
997 * have not yet been sent. And to resend requests while
998 * preserving this order we will want to put any sent
999 * requests back on the front of the osd client's unsent
1000 * list.
1001 *
1002 * So we build a separate ordered list of already-sent
1003 * requests for the affected osd and splice it onto the
1004 * front of the osd client's unsent list. Once we've seen a
1005 * request that has not yet been sent we're done. Those
1006 * requests are already sitting right where they belong.
1007 */
1008 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
1009 if (!req->r_sent)
1010 break;
1011
1012 if (!req->r_linger) {
1013 dout("%s requeueing %p tid %llu\n", __func__, req,
1014 req->r_tid);
1015 list_move_tail(&req->r_req_lru_item, &resend);
1016 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1017 } else {
1018 list_move_tail(&req->r_req_lru_item, &resend_linger);
1019 }
1020 }
1021 list_splice(&resend, &osdc->req_unsent);
1022
1023 /*
1024 * Both registered and not yet registered linger requests are
1025 * enqueued with a new tid on the same OSD. We add/move them
1026 * to req_unsent/o_requests at the end to keep things in tid
1027 * order.
1028 */
1029 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
1030 r_linger_osd_item) {
1031 WARN_ON(!list_empty(&req->r_req_lru_item));
1032 __kick_linger_request(req);
1033 }
1034
1035 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
1036 __kick_linger_request(req);
1037 }
1038
1039 /*
1040 * If the osd connection drops, we need to resubmit all requests.
1041 */
1042 static void osd_reset(struct ceph_connection *con)
1043 {
1044 struct ceph_osd *osd = con->private;
1045 struct ceph_osd_client *osdc;
1046
1047 if (!osd)
1048 return;
1049 dout("osd_reset osd%d\n", osd->o_osd);
1050 osdc = osd->o_osdc;
1051 down_read(&osdc->map_sem);
1052 mutex_lock(&osdc->request_mutex);
1053 __kick_osd_requests(osdc, osd);
1054 __send_queued(osdc);
1055 mutex_unlock(&osdc->request_mutex);
1056 up_read(&osdc->map_sem);
1057 }
1058
1059 /*
1060 * Track open sessions with osds.
1061 */
1062 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1063 {
1064 struct ceph_osd *osd;
1065
1066 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1067 if (!osd)
1068 return NULL;
1069
1070 atomic_set(&osd->o_ref, 1);
1071 osd->o_osdc = osdc;
1072 osd->o_osd = onum;
1073 RB_CLEAR_NODE(&osd->o_node);
1074 INIT_LIST_HEAD(&osd->o_requests);
1075 INIT_LIST_HEAD(&osd->o_linger_requests);
1076 INIT_LIST_HEAD(&osd->o_osd_lru);
1077 osd->o_incarnation = 1;
1078
1079 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1080
1081 INIT_LIST_HEAD(&osd->o_keepalive_item);
1082 return osd;
1083 }
1084
1085 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1086 {
1087 if (atomic_inc_not_zero(&osd->o_ref)) {
1088 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1089 atomic_read(&osd->o_ref));
1090 return osd;
1091 } else {
1092 dout("get_osd %p FAIL\n", osd);
1093 return NULL;
1094 }
1095 }
1096
1097 static void put_osd(struct ceph_osd *osd)
1098 {
1099 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1100 atomic_read(&osd->o_ref) - 1);
1101 if (atomic_dec_and_test(&osd->o_ref)) {
1102 if (osd->o_auth.authorizer)
1103 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1104 kfree(osd);
1105 }
1106 }
1107
1108 /*
1109 * remove an osd from our map
1110 */
1111 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1112 {
1113 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1114 WARN_ON(!list_empty(&osd->o_requests));
1115 WARN_ON(!list_empty(&osd->o_linger_requests));
1116
1117 list_del_init(&osd->o_osd_lru);
1118 rb_erase(&osd->o_node, &osdc->osds);
1119 RB_CLEAR_NODE(&osd->o_node);
1120 }
1121
1122 static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1123 {
1124 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1125
1126 if (!RB_EMPTY_NODE(&osd->o_node)) {
1127 ceph_con_close(&osd->o_con);
1128 __remove_osd(osdc, osd);
1129 put_osd(osd);
1130 }
1131 }
1132
1133 static void remove_all_osds(struct ceph_osd_client *osdc)
1134 {
1135 dout("%s %p\n", __func__, osdc);
1136 mutex_lock(&osdc->request_mutex);
1137 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1138 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1139 struct ceph_osd, o_node);
1140 remove_osd(osdc, osd);
1141 }
1142 mutex_unlock(&osdc->request_mutex);
1143 }
1144
1145 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1146 struct ceph_osd *osd)
1147 {
1148 dout("%s %p\n", __func__, osd);
1149 BUG_ON(!list_empty(&osd->o_osd_lru));
1150
1151 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1152 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1153 }
1154
1155 static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1156 struct ceph_osd *osd)
1157 {
1158 dout("%s %p\n", __func__, osd);
1159
1160 if (list_empty(&osd->o_requests) &&
1161 list_empty(&osd->o_linger_requests))
1162 __move_osd_to_lru(osdc, osd);
1163 }
1164
1165 static void __remove_osd_from_lru(struct ceph_osd *osd)
1166 {
1167 dout("__remove_osd_from_lru %p\n", osd);
1168 if (!list_empty(&osd->o_osd_lru))
1169 list_del_init(&osd->o_osd_lru);
1170 }
1171
1172 static void remove_old_osds(struct ceph_osd_client *osdc)
1173 {
1174 struct ceph_osd *osd, *nosd;
1175
1176 dout("__remove_old_osds %p\n", osdc);
1177 mutex_lock(&osdc->request_mutex);
1178 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1179 if (time_before(jiffies, osd->lru_ttl))
1180 break;
1181 remove_osd(osdc, osd);
1182 }
1183 mutex_unlock(&osdc->request_mutex);
1184 }
1185
1186 /*
1187 * reset osd connect
1188 */
1189 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1190 {
1191 struct ceph_entity_addr *peer_addr;
1192
1193 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1194 if (list_empty(&osd->o_requests) &&
1195 list_empty(&osd->o_linger_requests)) {
1196 remove_osd(osdc, osd);
1197 return -ENODEV;
1198 }
1199
1200 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1201 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1202 !ceph_con_opened(&osd->o_con)) {
1203 struct ceph_osd_request *req;
1204
1205 dout("osd addr hasn't changed and connection never opened, "
1206 "letting msgr retry\n");
1207 /* touch each r_stamp for handle_timeout()'s benfit */
1208 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1209 req->r_stamp = jiffies;
1210
1211 return -EAGAIN;
1212 }
1213
1214 ceph_con_close(&osd->o_con);
1215 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1216 osd->o_incarnation++;
1217
1218 return 0;
1219 }
1220
1221 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1222 {
1223 struct rb_node **p = &osdc->osds.rb_node;
1224 struct rb_node *parent = NULL;
1225 struct ceph_osd *osd = NULL;
1226
1227 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1228 while (*p) {
1229 parent = *p;
1230 osd = rb_entry(parent, struct ceph_osd, o_node);
1231 if (new->o_osd < osd->o_osd)
1232 p = &(*p)->rb_left;
1233 else if (new->o_osd > osd->o_osd)
1234 p = &(*p)->rb_right;
1235 else
1236 BUG();
1237 }
1238
1239 rb_link_node(&new->o_node, parent, p);
1240 rb_insert_color(&new->o_node, &osdc->osds);
1241 }
1242
1243 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1244 {
1245 struct ceph_osd *osd;
1246 struct rb_node *n = osdc->osds.rb_node;
1247
1248 while (n) {
1249 osd = rb_entry(n, struct ceph_osd, o_node);
1250 if (o < osd->o_osd)
1251 n = n->rb_left;
1252 else if (o > osd->o_osd)
1253 n = n->rb_right;
1254 else
1255 return osd;
1256 }
1257 return NULL;
1258 }
1259
1260 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1261 {
1262 schedule_delayed_work(&osdc->timeout_work,
1263 osdc->client->options->osd_keepalive_timeout);
1264 }
1265
1266 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1267 {
1268 cancel_delayed_work(&osdc->timeout_work);
1269 }
1270
1271 /*
1272 * Register request, assign tid. If this is the first request, set up
1273 * the timeout event.
1274 */
1275 static void __register_request(struct ceph_osd_client *osdc,
1276 struct ceph_osd_request *req)
1277 {
1278 req->r_tid = ++osdc->last_tid;
1279 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1280 dout("__register_request %p tid %lld\n", req, req->r_tid);
1281 __insert_request(osdc, req);
1282 ceph_osdc_get_request(req);
1283 osdc->num_requests++;
1284 if (osdc->num_requests == 1) {
1285 dout(" first request, scheduling timeout\n");
1286 __schedule_osd_timeout(osdc);
1287 }
1288 }
1289
1290 /*
1291 * called under osdc->request_mutex
1292 */
1293 static void __unregister_request(struct ceph_osd_client *osdc,
1294 struct ceph_osd_request *req)
1295 {
1296 if (RB_EMPTY_NODE(&req->r_node)) {
1297 dout("__unregister_request %p tid %lld not registered\n",
1298 req, req->r_tid);
1299 return;
1300 }
1301
1302 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1303 rb_erase(&req->r_node, &osdc->requests);
1304 RB_CLEAR_NODE(&req->r_node);
1305 osdc->num_requests--;
1306
1307 if (req->r_osd) {
1308 /* make sure the original request isn't in flight. */
1309 ceph_msg_revoke(req->r_request);
1310
1311 list_del_init(&req->r_osd_item);
1312 maybe_move_osd_to_lru(osdc, req->r_osd);
1313 if (list_empty(&req->r_linger_osd_item))
1314 req->r_osd = NULL;
1315 }
1316
1317 list_del_init(&req->r_req_lru_item);
1318 ceph_osdc_put_request(req);
1319
1320 if (osdc->num_requests == 0) {
1321 dout(" no requests, canceling timeout\n");
1322 __cancel_osd_timeout(osdc);
1323 }
1324 }
1325
1326 /*
1327 * Cancel a previously queued request message
1328 */
1329 static void __cancel_request(struct ceph_osd_request *req)
1330 {
1331 if (req->r_sent && req->r_osd) {
1332 ceph_msg_revoke(req->r_request);
1333 req->r_sent = 0;
1334 }
1335 }
1336
1337 static void __register_linger_request(struct ceph_osd_client *osdc,
1338 struct ceph_osd_request *req)
1339 {
1340 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1341 WARN_ON(!req->r_linger);
1342
1343 ceph_osdc_get_request(req);
1344 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1345 if (req->r_osd)
1346 list_add_tail(&req->r_linger_osd_item,
1347 &req->r_osd->o_linger_requests);
1348 }
1349
1350 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1351 struct ceph_osd_request *req)
1352 {
1353 WARN_ON(!req->r_linger);
1354
1355 if (list_empty(&req->r_linger_item)) {
1356 dout("%s %p tid %llu not registered\n", __func__, req,
1357 req->r_tid);
1358 return;
1359 }
1360
1361 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1362 list_del_init(&req->r_linger_item);
1363
1364 if (req->r_osd) {
1365 list_del_init(&req->r_linger_osd_item);
1366 maybe_move_osd_to_lru(osdc, req->r_osd);
1367 if (list_empty(&req->r_osd_item))
1368 req->r_osd = NULL;
1369 }
1370 ceph_osdc_put_request(req);
1371 }
1372
1373 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1374 struct ceph_osd_request *req)
1375 {
1376 if (!req->r_linger) {
1377 dout("set_request_linger %p\n", req);
1378 req->r_linger = 1;
1379 }
1380 }
1381 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1382
1383 /*
1384 * Returns whether a request should be blocked from being sent
1385 * based on the current osdmap and osd_client settings.
1386 *
1387 * Caller should hold map_sem for read.
1388 */
1389 static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1390 struct ceph_osd_request *req)
1391 {
1392 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1393 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1394 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1395 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1396 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1397 }
1398
1399 /*
1400 * Calculate mapping of a request to a PG. Takes tiering into account.
1401 */
1402 static int __calc_request_pg(struct ceph_osdmap *osdmap,
1403 struct ceph_osd_request *req,
1404 struct ceph_pg *pg_out)
1405 {
1406 bool need_check_tiering;
1407
1408 need_check_tiering = false;
1409 if (req->r_target_oloc.pool == -1) {
1410 req->r_target_oloc = req->r_base_oloc; /* struct */
1411 need_check_tiering = true;
1412 }
1413 if (req->r_target_oid.name_len == 0) {
1414 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1415 need_check_tiering = true;
1416 }
1417
1418 if (need_check_tiering &&
1419 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1420 struct ceph_pg_pool_info *pi;
1421
1422 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
1423 if (pi) {
1424 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1425 pi->read_tier >= 0)
1426 req->r_target_oloc.pool = pi->read_tier;
1427 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1428 pi->write_tier >= 0)
1429 req->r_target_oloc.pool = pi->write_tier;
1430 }
1431 /* !pi is caught in ceph_oloc_oid_to_pg() */
1432 }
1433
1434 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1435 &req->r_target_oid, pg_out);
1436 }
1437
1438 static void __enqueue_request(struct ceph_osd_request *req)
1439 {
1440 struct ceph_osd_client *osdc = req->r_osdc;
1441
1442 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1443 req->r_osd ? req->r_osd->o_osd : -1);
1444
1445 if (req->r_osd) {
1446 __remove_osd_from_lru(req->r_osd);
1447 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1448 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1449 } else {
1450 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1451 }
1452 }
1453
1454 /*
1455 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1456 * (as needed), and set the request r_osd appropriately. If there is
1457 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1458 * (unsent, homeless) or leave on in-flight lru.
1459 *
1460 * Return 0 if unchanged, 1 if changed, or negative on error.
1461 *
1462 * Caller should hold map_sem for read and request_mutex.
1463 */
1464 static int __map_request(struct ceph_osd_client *osdc,
1465 struct ceph_osd_request *req, int force_resend)
1466 {
1467 struct ceph_pg pgid;
1468 int acting[CEPH_PG_MAX_SIZE];
1469 int num, o;
1470 int err;
1471 bool was_paused;
1472
1473 dout("map_request %p tid %lld\n", req, req->r_tid);
1474
1475 err = __calc_request_pg(osdc->osdmap, req, &pgid);
1476 if (err) {
1477 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1478 return err;
1479 }
1480 req->r_pgid = pgid;
1481
1482 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1483 if (num < 0)
1484 num = 0;
1485
1486 was_paused = req->r_paused;
1487 req->r_paused = __req_should_be_paused(osdc, req);
1488 if (was_paused && !req->r_paused)
1489 force_resend = 1;
1490
1491 if ((!force_resend &&
1492 req->r_osd && req->r_osd->o_osd == o &&
1493 req->r_sent >= req->r_osd->o_incarnation &&
1494 req->r_num_pg_osds == num &&
1495 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1496 (req->r_osd == NULL && o == -1) ||
1497 req->r_paused)
1498 return 0; /* no change */
1499
1500 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1501 req->r_tid, pgid.pool, pgid.seed, o,
1502 req->r_osd ? req->r_osd->o_osd : -1);
1503
1504 /* record full pg acting set */
1505 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1506 req->r_num_pg_osds = num;
1507
1508 if (req->r_osd) {
1509 __cancel_request(req);
1510 list_del_init(&req->r_osd_item);
1511 list_del_init(&req->r_linger_osd_item);
1512 req->r_osd = NULL;
1513 }
1514
1515 req->r_osd = __lookup_osd(osdc, o);
1516 if (!req->r_osd && o >= 0) {
1517 err = -ENOMEM;
1518 req->r_osd = create_osd(osdc, o);
1519 if (!req->r_osd) {
1520 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1521 goto out;
1522 }
1523
1524 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1525 __insert_osd(osdc, req->r_osd);
1526
1527 ceph_con_open(&req->r_osd->o_con,
1528 CEPH_ENTITY_TYPE_OSD, o,
1529 &osdc->osdmap->osd_addr[o]);
1530 }
1531
1532 __enqueue_request(req);
1533 err = 1; /* osd or pg changed */
1534
1535 out:
1536 return err;
1537 }
1538
1539 /*
1540 * caller should hold map_sem (for read) and request_mutex
1541 */
1542 static void __send_request(struct ceph_osd_client *osdc,
1543 struct ceph_osd_request *req)
1544 {
1545 void *p;
1546
1547 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1548 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1549 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1550
1551 /* fill in message content that changes each time we send it */
1552 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1553 put_unaligned_le32(req->r_flags, req->r_request_flags);
1554 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
1555 p = req->r_request_pgid;
1556 ceph_encode_64(&p, req->r_pgid.pool);
1557 ceph_encode_32(&p, req->r_pgid.seed);
1558 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1559 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1560 sizeof(req->r_reassert_version));
1561
1562 req->r_stamp = jiffies;
1563 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1564
1565 ceph_msg_get(req->r_request); /* send consumes a ref */
1566
1567 req->r_sent = req->r_osd->o_incarnation;
1568
1569 ceph_con_send(&req->r_osd->o_con, req->r_request);
1570 }
1571
1572 /*
1573 * Send any requests in the queue (req_unsent).
1574 */
1575 static void __send_queued(struct ceph_osd_client *osdc)
1576 {
1577 struct ceph_osd_request *req, *tmp;
1578
1579 dout("__send_queued\n");
1580 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1581 __send_request(osdc, req);
1582 }
1583
1584 /*
1585 * Caller should hold map_sem for read and request_mutex.
1586 */
1587 static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1588 struct ceph_osd_request *req,
1589 bool nofail)
1590 {
1591 int rc;
1592
1593 __register_request(osdc, req);
1594 req->r_sent = 0;
1595 req->r_got_reply = 0;
1596 rc = __map_request(osdc, req, 0);
1597 if (rc < 0) {
1598 if (nofail) {
1599 dout("osdc_start_request failed map, "
1600 " will retry %lld\n", req->r_tid);
1601 rc = 0;
1602 } else {
1603 __unregister_request(osdc, req);
1604 }
1605 return rc;
1606 }
1607
1608 if (req->r_osd == NULL) {
1609 dout("send_request %p no up osds in pg\n", req);
1610 ceph_monc_request_next_osdmap(&osdc->client->monc);
1611 } else {
1612 __send_queued(osdc);
1613 }
1614
1615 return 0;
1616 }
1617
1618 /*
1619 * Timeout callback, called every N seconds when 1 or more osd
1620 * requests has been active for more than N seconds. When this
1621 * happens, we ping all OSDs with requests who have timed out to
1622 * ensure any communications channel reset is detected. Reset the
1623 * request timeouts another N seconds in the future as we go.
1624 * Reschedule the timeout event another N seconds in future (unless
1625 * there are no open requests).
1626 */
1627 static void handle_timeout(struct work_struct *work)
1628 {
1629 struct ceph_osd_client *osdc =
1630 container_of(work, struct ceph_osd_client, timeout_work.work);
1631 struct ceph_options *opts = osdc->client->options;
1632 struct ceph_osd_request *req;
1633 struct ceph_osd *osd;
1634 struct list_head slow_osds;
1635 dout("timeout\n");
1636 down_read(&osdc->map_sem);
1637
1638 ceph_monc_request_next_osdmap(&osdc->client->monc);
1639
1640 mutex_lock(&osdc->request_mutex);
1641
1642 /*
1643 * ping osds that are a bit slow. this ensures that if there
1644 * is a break in the TCP connection we will notice, and reopen
1645 * a connection with that osd (from the fault callback).
1646 */
1647 INIT_LIST_HEAD(&slow_osds);
1648 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1649 if (time_before(jiffies,
1650 req->r_stamp + opts->osd_keepalive_timeout))
1651 break;
1652
1653 osd = req->r_osd;
1654 BUG_ON(!osd);
1655 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1656 req->r_tid, osd->o_osd);
1657 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1658 }
1659 while (!list_empty(&slow_osds)) {
1660 osd = list_entry(slow_osds.next, struct ceph_osd,
1661 o_keepalive_item);
1662 list_del_init(&osd->o_keepalive_item);
1663 ceph_con_keepalive(&osd->o_con);
1664 }
1665
1666 __schedule_osd_timeout(osdc);
1667 __send_queued(osdc);
1668 mutex_unlock(&osdc->request_mutex);
1669 up_read(&osdc->map_sem);
1670 }
1671
1672 static void handle_osds_timeout(struct work_struct *work)
1673 {
1674 struct ceph_osd_client *osdc =
1675 container_of(work, struct ceph_osd_client,
1676 osds_timeout_work.work);
1677 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
1678
1679 dout("osds timeout\n");
1680 down_read(&osdc->map_sem);
1681 remove_old_osds(osdc);
1682 up_read(&osdc->map_sem);
1683
1684 schedule_delayed_work(&osdc->osds_timeout_work,
1685 round_jiffies_relative(delay));
1686 }
1687
1688 static int ceph_oloc_decode(void **p, void *end,
1689 struct ceph_object_locator *oloc)
1690 {
1691 u8 struct_v, struct_cv;
1692 u32 len;
1693 void *struct_end;
1694 int ret = 0;
1695
1696 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1697 struct_v = ceph_decode_8(p);
1698 struct_cv = ceph_decode_8(p);
1699 if (struct_v < 3) {
1700 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1701 struct_v, struct_cv);
1702 goto e_inval;
1703 }
1704 if (struct_cv > 6) {
1705 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1706 struct_v, struct_cv);
1707 goto e_inval;
1708 }
1709 len = ceph_decode_32(p);
1710 ceph_decode_need(p, end, len, e_inval);
1711 struct_end = *p + len;
1712
1713 oloc->pool = ceph_decode_64(p);
1714 *p += 4; /* skip preferred */
1715
1716 len = ceph_decode_32(p);
1717 if (len > 0) {
1718 pr_warn("ceph_object_locator::key is set\n");
1719 goto e_inval;
1720 }
1721
1722 if (struct_v >= 5) {
1723 len = ceph_decode_32(p);
1724 if (len > 0) {
1725 pr_warn("ceph_object_locator::nspace is set\n");
1726 goto e_inval;
1727 }
1728 }
1729
1730 if (struct_v >= 6) {
1731 s64 hash = ceph_decode_64(p);
1732 if (hash != -1) {
1733 pr_warn("ceph_object_locator::hash is set\n");
1734 goto e_inval;
1735 }
1736 }
1737
1738 /* skip the rest */
1739 *p = struct_end;
1740 out:
1741 return ret;
1742
1743 e_inval:
1744 ret = -EINVAL;
1745 goto out;
1746 }
1747
1748 static int ceph_redirect_decode(void **p, void *end,
1749 struct ceph_request_redirect *redir)
1750 {
1751 u8 struct_v, struct_cv;
1752 u32 len;
1753 void *struct_end;
1754 int ret;
1755
1756 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1757 struct_v = ceph_decode_8(p);
1758 struct_cv = ceph_decode_8(p);
1759 if (struct_cv > 1) {
1760 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1761 struct_v, struct_cv);
1762 goto e_inval;
1763 }
1764 len = ceph_decode_32(p);
1765 ceph_decode_need(p, end, len, e_inval);
1766 struct_end = *p + len;
1767
1768 ret = ceph_oloc_decode(p, end, &redir->oloc);
1769 if (ret)
1770 goto out;
1771
1772 len = ceph_decode_32(p);
1773 if (len > 0) {
1774 pr_warn("ceph_request_redirect::object_name is set\n");
1775 goto e_inval;
1776 }
1777
1778 len = ceph_decode_32(p);
1779 *p += len; /* skip osd_instructions */
1780
1781 /* skip the rest */
1782 *p = struct_end;
1783 out:
1784 return ret;
1785
1786 e_inval:
1787 ret = -EINVAL;
1788 goto out;
1789 }
1790
1791 static void complete_request(struct ceph_osd_request *req)
1792 {
1793 complete_all(&req->r_safe_completion); /* fsync waiter */
1794 }
1795
1796 /*
1797 * handle osd op reply. either call the callback if it is specified,
1798 * or do the completion to wake up the waiting thread.
1799 */
1800 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1801 {
1802 void *p, *end;
1803 struct ceph_osd_request *req;
1804 struct ceph_request_redirect redir;
1805 u64 tid;
1806 int object_len;
1807 unsigned int numops;
1808 int payload_len, flags;
1809 s32 result;
1810 s32 retry_attempt;
1811 struct ceph_pg pg;
1812 int err;
1813 u32 reassert_epoch;
1814 u64 reassert_version;
1815 u32 osdmap_epoch;
1816 int already_completed;
1817 u32 bytes;
1818 u8 decode_redir;
1819 unsigned int i;
1820
1821 tid = le64_to_cpu(msg->hdr.tid);
1822 dout("handle_reply %p tid %llu\n", msg, tid);
1823
1824 p = msg->front.iov_base;
1825 end = p + msg->front.iov_len;
1826
1827 ceph_decode_need(&p, end, 4, bad);
1828 object_len = ceph_decode_32(&p);
1829 ceph_decode_need(&p, end, object_len, bad);
1830 p += object_len;
1831
1832 err = ceph_decode_pgid(&p, end, &pg);
1833 if (err)
1834 goto bad;
1835
1836 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1837 flags = ceph_decode_64(&p);
1838 result = ceph_decode_32(&p);
1839 reassert_epoch = ceph_decode_32(&p);
1840 reassert_version = ceph_decode_64(&p);
1841 osdmap_epoch = ceph_decode_32(&p);
1842
1843 /* lookup */
1844 down_read(&osdc->map_sem);
1845 mutex_lock(&osdc->request_mutex);
1846 req = __lookup_request(osdc, tid);
1847 if (req == NULL) {
1848 dout("handle_reply tid %llu dne\n", tid);
1849 goto bad_mutex;
1850 }
1851 ceph_osdc_get_request(req);
1852
1853 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1854 req, result);
1855
1856 ceph_decode_need(&p, end, 4, bad_put);
1857 numops = ceph_decode_32(&p);
1858 if (numops > CEPH_OSD_MAX_OPS)
1859 goto bad_put;
1860 if (numops != req->r_num_ops)
1861 goto bad_put;
1862 payload_len = 0;
1863 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
1864 for (i = 0; i < numops; i++) {
1865 struct ceph_osd_op *op = p;
1866 int len;
1867
1868 len = le32_to_cpu(op->payload_len);
1869 req->r_ops[i].outdata_len = len;
1870 dout(" op %d has %d bytes\n", i, len);
1871 payload_len += len;
1872 p += sizeof(*op);
1873 }
1874 bytes = le32_to_cpu(msg->hdr.data_len);
1875 if (payload_len != bytes) {
1876 pr_warn("sum of op payload lens %d != data_len %d\n",
1877 payload_len, bytes);
1878 goto bad_put;
1879 }
1880
1881 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
1882 retry_attempt = ceph_decode_32(&p);
1883 for (i = 0; i < numops; i++)
1884 req->r_ops[i].rval = ceph_decode_32(&p);
1885
1886 if (le16_to_cpu(msg->hdr.version) >= 6) {
1887 p += 8 + 4; /* skip replay_version */
1888 p += 8; /* skip user_version */
1889
1890 if (le16_to_cpu(msg->hdr.version) >= 7)
1891 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1892 else
1893 decode_redir = 1;
1894 } else {
1895 decode_redir = 0;
1896 }
1897
1898 if (decode_redir) {
1899 err = ceph_redirect_decode(&p, end, &redir);
1900 if (err)
1901 goto bad_put;
1902 } else {
1903 redir.oloc.pool = -1;
1904 }
1905
1906 if (redir.oloc.pool != -1) {
1907 dout("redirect pool %lld\n", redir.oloc.pool);
1908
1909 __unregister_request(osdc, req);
1910
1911 req->r_target_oloc = redir.oloc; /* struct */
1912
1913 /*
1914 * Start redirect requests with nofail=true. If
1915 * mapping fails, request will end up on the notarget
1916 * list, waiting for the new osdmap (which can take
1917 * a while), even though the original request mapped
1918 * successfully. In the future we might want to follow
1919 * original request's nofail setting here.
1920 */
1921 err = __ceph_osdc_start_request(osdc, req, true);
1922 BUG_ON(err);
1923
1924 goto out_unlock;
1925 }
1926
1927 already_completed = req->r_got_reply;
1928 if (!req->r_got_reply) {
1929 req->r_result = result;
1930 dout("handle_reply result %d bytes %d\n", req->r_result,
1931 bytes);
1932 if (req->r_result == 0)
1933 req->r_result = bytes;
1934
1935 /* in case this is a write and we need to replay, */
1936 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1937 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1938
1939 req->r_got_reply = 1;
1940 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1941 dout("handle_reply tid %llu dup ack\n", tid);
1942 goto out_unlock;
1943 }
1944
1945 dout("handle_reply tid %llu flags %d\n", tid, flags);
1946
1947 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1948 __register_linger_request(osdc, req);
1949
1950 /* either this is a read, or we got the safe response */
1951 if (result < 0 ||
1952 (flags & CEPH_OSD_FLAG_ONDISK) ||
1953 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1954 __unregister_request(osdc, req);
1955
1956 mutex_unlock(&osdc->request_mutex);
1957 up_read(&osdc->map_sem);
1958
1959 if (!already_completed) {
1960 if (req->r_unsafe_callback &&
1961 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1962 req->r_unsafe_callback(req, true);
1963 if (req->r_callback)
1964 req->r_callback(req, msg);
1965 else
1966 complete_all(&req->r_completion);
1967 }
1968
1969 if (flags & CEPH_OSD_FLAG_ONDISK) {
1970 if (req->r_unsafe_callback && already_completed)
1971 req->r_unsafe_callback(req, false);
1972 complete_request(req);
1973 }
1974
1975 out:
1976 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1977 ceph_osdc_put_request(req);
1978 return;
1979 out_unlock:
1980 mutex_unlock(&osdc->request_mutex);
1981 up_read(&osdc->map_sem);
1982 goto out;
1983
1984 bad_put:
1985 req->r_result = -EIO;
1986 __unregister_request(osdc, req);
1987 if (req->r_callback)
1988 req->r_callback(req, msg);
1989 else
1990 complete_all(&req->r_completion);
1991 complete_request(req);
1992 ceph_osdc_put_request(req);
1993 bad_mutex:
1994 mutex_unlock(&osdc->request_mutex);
1995 up_read(&osdc->map_sem);
1996 bad:
1997 pr_err("corrupt osd_op_reply got %d %d\n",
1998 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1999 ceph_msg_dump(msg);
2000 }
2001
2002 static void reset_changed_osds(struct ceph_osd_client *osdc)
2003 {
2004 struct rb_node *p, *n;
2005
2006 dout("%s %p\n", __func__, osdc);
2007 for (p = rb_first(&osdc->osds); p; p = n) {
2008 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
2009
2010 n = rb_next(p);
2011 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
2012 memcmp(&osd->o_con.peer_addr,
2013 ceph_osd_addr(osdc->osdmap,
2014 osd->o_osd),
2015 sizeof(struct ceph_entity_addr)) != 0)
2016 __reset_osd(osdc, osd);
2017 }
2018 }
2019
2020 /*
2021 * Requeue requests whose mapping to an OSD has changed. If requests map to
2022 * no osd, request a new map.
2023 *
2024 * Caller should hold map_sem for read.
2025 */
2026 static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
2027 bool force_resend_writes)
2028 {
2029 struct ceph_osd_request *req, *nreq;
2030 struct rb_node *p;
2031 int needmap = 0;
2032 int err;
2033 bool force_resend_req;
2034
2035 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2036 force_resend_writes ? " (force resend writes)" : "");
2037 mutex_lock(&osdc->request_mutex);
2038 for (p = rb_first(&osdc->requests); p; ) {
2039 req = rb_entry(p, struct ceph_osd_request, r_node);
2040 p = rb_next(p);
2041
2042 /*
2043 * For linger requests that have not yet been
2044 * registered, move them to the linger list; they'll
2045 * be sent to the osd in the loop below. Unregister
2046 * the request before re-registering it as a linger
2047 * request to ensure the __map_request() below
2048 * will decide it needs to be sent.
2049 */
2050 if (req->r_linger && list_empty(&req->r_linger_item)) {
2051 dout("%p tid %llu restart on osd%d\n",
2052 req, req->r_tid,
2053 req->r_osd ? req->r_osd->o_osd : -1);
2054 ceph_osdc_get_request(req);
2055 __unregister_request(osdc, req);
2056 __register_linger_request(osdc, req);
2057 ceph_osdc_put_request(req);
2058 continue;
2059 }
2060
2061 force_resend_req = force_resend ||
2062 (force_resend_writes &&
2063 req->r_flags & CEPH_OSD_FLAG_WRITE);
2064 err = __map_request(osdc, req, force_resend_req);
2065 if (err < 0)
2066 continue; /* error */
2067 if (req->r_osd == NULL) {
2068 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2069 needmap++; /* request a newer map */
2070 } else if (err > 0) {
2071 if (!req->r_linger) {
2072 dout("%p tid %llu requeued on osd%d\n", req,
2073 req->r_tid,
2074 req->r_osd ? req->r_osd->o_osd : -1);
2075 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2076 }
2077 }
2078 }
2079
2080 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2081 r_linger_item) {
2082 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2083
2084 err = __map_request(osdc, req,
2085 force_resend || force_resend_writes);
2086 dout("__map_request returned %d\n", err);
2087 if (err < 0)
2088 continue; /* hrm! */
2089 if (req->r_osd == NULL || err > 0) {
2090 if (req->r_osd == NULL) {
2091 dout("lingering %p tid %llu maps to no osd\n",
2092 req, req->r_tid);
2093 /*
2094 * A homeless lingering request makes
2095 * no sense, as it's job is to keep
2096 * a particular OSD connection open.
2097 * Request a newer map and kick the
2098 * request, knowing that it won't be
2099 * resent until we actually get a map
2100 * that can tell us where to send it.
2101 */
2102 needmap++;
2103 }
2104
2105 dout("kicking lingering %p tid %llu osd%d\n", req,
2106 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2107 __register_request(osdc, req);
2108 __unregister_linger_request(osdc, req);
2109 }
2110 }
2111 reset_changed_osds(osdc);
2112 mutex_unlock(&osdc->request_mutex);
2113
2114 if (needmap) {
2115 dout("%d requests for down osds, need new map\n", needmap);
2116 ceph_monc_request_next_osdmap(&osdc->client->monc);
2117 }
2118 }
2119
2120
2121 /*
2122 * Process updated osd map.
2123 *
2124 * The message contains any number of incremental and full maps, normally
2125 * indicating some sort of topology change in the cluster. Kick requests
2126 * off to different OSDs as needed.
2127 */
2128 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2129 {
2130 void *p, *end, *next;
2131 u32 nr_maps, maplen;
2132 u32 epoch;
2133 struct ceph_osdmap *newmap = NULL, *oldmap;
2134 int err;
2135 struct ceph_fsid fsid;
2136 bool was_full;
2137
2138 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2139 p = msg->front.iov_base;
2140 end = p + msg->front.iov_len;
2141
2142 /* verify fsid */
2143 ceph_decode_need(&p, end, sizeof(fsid), bad);
2144 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2145 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2146 return;
2147
2148 down_write(&osdc->map_sem);
2149
2150 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2151
2152 /* incremental maps */
2153 ceph_decode_32_safe(&p, end, nr_maps, bad);
2154 dout(" %d inc maps\n", nr_maps);
2155 while (nr_maps > 0) {
2156 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2157 epoch = ceph_decode_32(&p);
2158 maplen = ceph_decode_32(&p);
2159 ceph_decode_need(&p, end, maplen, bad);
2160 next = p + maplen;
2161 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2162 dout("applying incremental map %u len %d\n",
2163 epoch, maplen);
2164 newmap = osdmap_apply_incremental(&p, next,
2165 osdc->osdmap,
2166 &osdc->client->msgr);
2167 if (IS_ERR(newmap)) {
2168 err = PTR_ERR(newmap);
2169 goto bad;
2170 }
2171 BUG_ON(!newmap);
2172 if (newmap != osdc->osdmap) {
2173 ceph_osdmap_destroy(osdc->osdmap);
2174 osdc->osdmap = newmap;
2175 }
2176 was_full = was_full ||
2177 ceph_osdmap_flag(osdc->osdmap,
2178 CEPH_OSDMAP_FULL);
2179 kick_requests(osdc, 0, was_full);
2180 } else {
2181 dout("ignoring incremental map %u len %d\n",
2182 epoch, maplen);
2183 }
2184 p = next;
2185 nr_maps--;
2186 }
2187 if (newmap)
2188 goto done;
2189
2190 /* full maps */
2191 ceph_decode_32_safe(&p, end, nr_maps, bad);
2192 dout(" %d full maps\n", nr_maps);
2193 while (nr_maps) {
2194 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2195 epoch = ceph_decode_32(&p);
2196 maplen = ceph_decode_32(&p);
2197 ceph_decode_need(&p, end, maplen, bad);
2198 if (nr_maps > 1) {
2199 dout("skipping non-latest full map %u len %d\n",
2200 epoch, maplen);
2201 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2202 dout("skipping full map %u len %d, "
2203 "older than our %u\n", epoch, maplen,
2204 osdc->osdmap->epoch);
2205 } else {
2206 int skipped_map = 0;
2207
2208 dout("taking full map %u len %d\n", epoch, maplen);
2209 newmap = ceph_osdmap_decode(&p, p+maplen);
2210 if (IS_ERR(newmap)) {
2211 err = PTR_ERR(newmap);
2212 goto bad;
2213 }
2214 BUG_ON(!newmap);
2215 oldmap = osdc->osdmap;
2216 osdc->osdmap = newmap;
2217 if (oldmap) {
2218 if (oldmap->epoch + 1 < newmap->epoch)
2219 skipped_map = 1;
2220 ceph_osdmap_destroy(oldmap);
2221 }
2222 was_full = was_full ||
2223 ceph_osdmap_flag(osdc->osdmap,
2224 CEPH_OSDMAP_FULL);
2225 kick_requests(osdc, skipped_map, was_full);
2226 }
2227 p += maplen;
2228 nr_maps--;
2229 }
2230
2231 if (!osdc->osdmap)
2232 goto bad;
2233 done:
2234 downgrade_write(&osdc->map_sem);
2235 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2236 osdc->osdmap->epoch);
2237
2238 /*
2239 * subscribe to subsequent osdmap updates if full to ensure
2240 * we find out when we are no longer full and stop returning
2241 * ENOSPC.
2242 */
2243 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2244 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2245 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
2246 ceph_monc_request_next_osdmap(&osdc->client->monc);
2247
2248 mutex_lock(&osdc->request_mutex);
2249 __send_queued(osdc);
2250 mutex_unlock(&osdc->request_mutex);
2251 up_read(&osdc->map_sem);
2252 wake_up_all(&osdc->client->auth_wq);
2253 return;
2254
2255 bad:
2256 pr_err("osdc handle_map corrupt msg\n");
2257 ceph_msg_dump(msg);
2258 up_write(&osdc->map_sem);
2259 }
2260
2261 /*
2262 * watch/notify callback event infrastructure
2263 *
2264 * These callbacks are used both for watch and notify operations.
2265 */
2266 static void __release_event(struct kref *kref)
2267 {
2268 struct ceph_osd_event *event =
2269 container_of(kref, struct ceph_osd_event, kref);
2270
2271 dout("__release_event %p\n", event);
2272 kfree(event);
2273 }
2274
2275 static void get_event(struct ceph_osd_event *event)
2276 {
2277 kref_get(&event->kref);
2278 }
2279
2280 void ceph_osdc_put_event(struct ceph_osd_event *event)
2281 {
2282 kref_put(&event->kref, __release_event);
2283 }
2284 EXPORT_SYMBOL(ceph_osdc_put_event);
2285
2286 static void __insert_event(struct ceph_osd_client *osdc,
2287 struct ceph_osd_event *new)
2288 {
2289 struct rb_node **p = &osdc->event_tree.rb_node;
2290 struct rb_node *parent = NULL;
2291 struct ceph_osd_event *event = NULL;
2292
2293 while (*p) {
2294 parent = *p;
2295 event = rb_entry(parent, struct ceph_osd_event, node);
2296 if (new->cookie < event->cookie)
2297 p = &(*p)->rb_left;
2298 else if (new->cookie > event->cookie)
2299 p = &(*p)->rb_right;
2300 else
2301 BUG();
2302 }
2303
2304 rb_link_node(&new->node, parent, p);
2305 rb_insert_color(&new->node, &osdc->event_tree);
2306 }
2307
2308 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2309 u64 cookie)
2310 {
2311 struct rb_node **p = &osdc->event_tree.rb_node;
2312 struct rb_node *parent = NULL;
2313 struct ceph_osd_event *event = NULL;
2314
2315 while (*p) {
2316 parent = *p;
2317 event = rb_entry(parent, struct ceph_osd_event, node);
2318 if (cookie < event->cookie)
2319 p = &(*p)->rb_left;
2320 else if (cookie > event->cookie)
2321 p = &(*p)->rb_right;
2322 else
2323 return event;
2324 }
2325 return NULL;
2326 }
2327
2328 static void __remove_event(struct ceph_osd_event *event)
2329 {
2330 struct ceph_osd_client *osdc = event->osdc;
2331
2332 if (!RB_EMPTY_NODE(&event->node)) {
2333 dout("__remove_event removed %p\n", event);
2334 rb_erase(&event->node, &osdc->event_tree);
2335 ceph_osdc_put_event(event);
2336 } else {
2337 dout("__remove_event didn't remove %p\n", event);
2338 }
2339 }
2340
2341 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2342 void (*event_cb)(u64, u64, u8, void *),
2343 void *data, struct ceph_osd_event **pevent)
2344 {
2345 struct ceph_osd_event *event;
2346
2347 event = kmalloc(sizeof(*event), GFP_NOIO);
2348 if (!event)
2349 return -ENOMEM;
2350
2351 dout("create_event %p\n", event);
2352 event->cb = event_cb;
2353 event->one_shot = 0;
2354 event->data = data;
2355 event->osdc = osdc;
2356 INIT_LIST_HEAD(&event->osd_node);
2357 RB_CLEAR_NODE(&event->node);
2358 kref_init(&event->kref); /* one ref for us */
2359 kref_get(&event->kref); /* one ref for the caller */
2360
2361 spin_lock(&osdc->event_lock);
2362 event->cookie = ++osdc->event_count;
2363 __insert_event(osdc, event);
2364 spin_unlock(&osdc->event_lock);
2365
2366 *pevent = event;
2367 return 0;
2368 }
2369 EXPORT_SYMBOL(ceph_osdc_create_event);
2370
2371 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2372 {
2373 struct ceph_osd_client *osdc = event->osdc;
2374
2375 dout("cancel_event %p\n", event);
2376 spin_lock(&osdc->event_lock);
2377 __remove_event(event);
2378 spin_unlock(&osdc->event_lock);
2379 ceph_osdc_put_event(event); /* caller's */
2380 }
2381 EXPORT_SYMBOL(ceph_osdc_cancel_event);
2382
2383
2384 static void do_event_work(struct work_struct *work)
2385 {
2386 struct ceph_osd_event_work *event_work =
2387 container_of(work, struct ceph_osd_event_work, work);
2388 struct ceph_osd_event *event = event_work->event;
2389 u64 ver = event_work->ver;
2390 u64 notify_id = event_work->notify_id;
2391 u8 opcode = event_work->opcode;
2392
2393 dout("do_event_work completing %p\n", event);
2394 event->cb(ver, notify_id, opcode, event->data);
2395 dout("do_event_work completed %p\n", event);
2396 ceph_osdc_put_event(event);
2397 kfree(event_work);
2398 }
2399
2400
2401 /*
2402 * Process osd watch notifications
2403 */
2404 static void handle_watch_notify(struct ceph_osd_client *osdc,
2405 struct ceph_msg *msg)
2406 {
2407 void *p, *end;
2408 u8 proto_ver;
2409 u64 cookie, ver, notify_id;
2410 u8 opcode;
2411 struct ceph_osd_event *event;
2412 struct ceph_osd_event_work *event_work;
2413
2414 p = msg->front.iov_base;
2415 end = p + msg->front.iov_len;
2416
2417 ceph_decode_8_safe(&p, end, proto_ver, bad);
2418 ceph_decode_8_safe(&p, end, opcode, bad);
2419 ceph_decode_64_safe(&p, end, cookie, bad);
2420 ceph_decode_64_safe(&p, end, ver, bad);
2421 ceph_decode_64_safe(&p, end, notify_id, bad);
2422
2423 spin_lock(&osdc->event_lock);
2424 event = __find_event(osdc, cookie);
2425 if (event) {
2426 BUG_ON(event->one_shot);
2427 get_event(event);
2428 }
2429 spin_unlock(&osdc->event_lock);
2430 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2431 cookie, ver, event);
2432 if (event) {
2433 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
2434 if (!event_work) {
2435 pr_err("couldn't allocate event_work\n");
2436 ceph_osdc_put_event(event);
2437 return;
2438 }
2439 INIT_WORK(&event_work->work, do_event_work);
2440 event_work->event = event;
2441 event_work->ver = ver;
2442 event_work->notify_id = notify_id;
2443 event_work->opcode = opcode;
2444
2445 queue_work(osdc->notify_wq, &event_work->work);
2446 }
2447
2448 return;
2449
2450 bad:
2451 pr_err("osdc handle_watch_notify corrupt msg\n");
2452 }
2453
2454 /*
2455 * build new request AND message
2456 *
2457 */
2458 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2459 struct ceph_snap_context *snapc, u64 snap_id,
2460 struct timespec *mtime)
2461 {
2462 struct ceph_msg *msg = req->r_request;
2463 void *p;
2464 size_t msg_size;
2465 int flags = req->r_flags;
2466 u64 data_len;
2467 unsigned int i;
2468
2469 req->r_snapid = snap_id;
2470 WARN_ON(snapc != req->r_snapc);
2471
2472 /* encode request */
2473 msg->hdr.version = cpu_to_le16(4);
2474
2475 p = msg->front.iov_base;
2476 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2477 req->r_request_osdmap_epoch = p;
2478 p += 4;
2479 req->r_request_flags = p;
2480 p += 4;
2481 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2482 ceph_encode_timespec(p, mtime);
2483 p += sizeof(struct ceph_timespec);
2484 req->r_request_reassert_version = p;
2485 p += sizeof(struct ceph_eversion); /* will get filled in */
2486
2487 /* oloc */
2488 ceph_encode_8(&p, 4);
2489 ceph_encode_8(&p, 4);
2490 ceph_encode_32(&p, 8 + 4 + 4);
2491 req->r_request_pool = p;
2492 p += 8;
2493 ceph_encode_32(&p, -1); /* preferred */
2494 ceph_encode_32(&p, 0); /* key len */
2495
2496 ceph_encode_8(&p, 1);
2497 req->r_request_pgid = p;
2498 p += 8 + 4;
2499 ceph_encode_32(&p, -1); /* preferred */
2500
2501 /* oid */
2502 ceph_encode_32(&p, req->r_base_oid.name_len);
2503 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2504 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2505 req->r_base_oid.name, req->r_base_oid.name_len);
2506 p += req->r_base_oid.name_len;
2507
2508 /* ops--can imply data */
2509 ceph_encode_16(&p, (u16)req->r_num_ops);
2510 data_len = 0;
2511 for (i = 0; i < req->r_num_ops; i++) {
2512 data_len += osd_req_encode_op(req, p, i);
2513 p += sizeof(struct ceph_osd_op);
2514 }
2515
2516 /* snaps */
2517 ceph_encode_64(&p, req->r_snapid);
2518 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2519 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2520 if (req->r_snapc) {
2521 for (i = 0; i < req->r_snapc->num_snaps; i++) {
2522 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2523 }
2524 }
2525
2526 req->r_request_attempts = p;
2527 p += 4;
2528
2529 /* data */
2530 if (flags & CEPH_OSD_FLAG_WRITE) {
2531 u16 data_off;
2532
2533 /*
2534 * The header "data_off" is a hint to the receiver
2535 * allowing it to align received data into its
2536 * buffers such that there's no need to re-copy
2537 * it before writing it to disk (direct I/O).
2538 */
2539 data_off = (u16) (off & 0xffff);
2540 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2541 }
2542 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2543
2544 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2545 msg_size = p - msg->front.iov_base;
2546 msg->front.iov_len = msg_size;
2547 msg->hdr.front_len = cpu_to_le32(msg_size);
2548
2549 dout("build_request msg_size was %d\n", (int)msg_size);
2550 }
2551 EXPORT_SYMBOL(ceph_osdc_build_request);
2552
2553 /*
2554 * Register request, send initial attempt.
2555 */
2556 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2557 struct ceph_osd_request *req,
2558 bool nofail)
2559 {
2560 int rc;
2561
2562 down_read(&osdc->map_sem);
2563 mutex_lock(&osdc->request_mutex);
2564
2565 rc = __ceph_osdc_start_request(osdc, req, nofail);
2566
2567 mutex_unlock(&osdc->request_mutex);
2568 up_read(&osdc->map_sem);
2569
2570 return rc;
2571 }
2572 EXPORT_SYMBOL(ceph_osdc_start_request);
2573
2574 /*
2575 * Unregister a registered request. The request is not completed (i.e.
2576 * no callbacks or wakeups) - higher layers are supposed to know what
2577 * they are canceling.
2578 */
2579 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2580 {
2581 struct ceph_osd_client *osdc = req->r_osdc;
2582
2583 mutex_lock(&osdc->request_mutex);
2584 if (req->r_linger)
2585 __unregister_linger_request(osdc, req);
2586 __unregister_request(osdc, req);
2587 mutex_unlock(&osdc->request_mutex);
2588
2589 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2590 }
2591 EXPORT_SYMBOL(ceph_osdc_cancel_request);
2592
2593 /*
2594 * wait for a request to complete
2595 */
2596 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2597 struct ceph_osd_request *req)
2598 {
2599 int rc;
2600
2601 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2602
2603 rc = wait_for_completion_interruptible(&req->r_completion);
2604 if (rc < 0) {
2605 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2606 ceph_osdc_cancel_request(req);
2607 complete_request(req);
2608 return rc;
2609 }
2610
2611 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2612 req->r_result);
2613 return req->r_result;
2614 }
2615 EXPORT_SYMBOL(ceph_osdc_wait_request);
2616
2617 /*
2618 * sync - wait for all in-flight requests to flush. avoid starvation.
2619 */
2620 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2621 {
2622 struct ceph_osd_request *req;
2623 u64 last_tid, next_tid = 0;
2624
2625 mutex_lock(&osdc->request_mutex);
2626 last_tid = osdc->last_tid;
2627 while (1) {
2628 req = __lookup_request_ge(osdc, next_tid);
2629 if (!req)
2630 break;
2631 if (req->r_tid > last_tid)
2632 break;
2633
2634 next_tid = req->r_tid + 1;
2635 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2636 continue;
2637
2638 ceph_osdc_get_request(req);
2639 mutex_unlock(&osdc->request_mutex);
2640 dout("sync waiting on tid %llu (last is %llu)\n",
2641 req->r_tid, last_tid);
2642 wait_for_completion(&req->r_safe_completion);
2643 mutex_lock(&osdc->request_mutex);
2644 ceph_osdc_put_request(req);
2645 }
2646 mutex_unlock(&osdc->request_mutex);
2647 dout("sync done (thru tid %llu)\n", last_tid);
2648 }
2649 EXPORT_SYMBOL(ceph_osdc_sync);
2650
2651 /*
2652 * Call all pending notify callbacks - for use after a watch is
2653 * unregistered, to make sure no more callbacks for it will be invoked
2654 */
2655 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2656 {
2657 flush_workqueue(osdc->notify_wq);
2658 }
2659 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2660
2661
2662 /*
2663 * init, shutdown
2664 */
2665 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2666 {
2667 int err;
2668
2669 dout("init\n");
2670 osdc->client = client;
2671 osdc->osdmap = NULL;
2672 init_rwsem(&osdc->map_sem);
2673 init_completion(&osdc->map_waiters);
2674 osdc->last_requested_map = 0;
2675 mutex_init(&osdc->request_mutex);
2676 osdc->last_tid = 0;
2677 osdc->osds = RB_ROOT;
2678 INIT_LIST_HEAD(&osdc->osd_lru);
2679 osdc->requests = RB_ROOT;
2680 INIT_LIST_HEAD(&osdc->req_lru);
2681 INIT_LIST_HEAD(&osdc->req_unsent);
2682 INIT_LIST_HEAD(&osdc->req_notarget);
2683 INIT_LIST_HEAD(&osdc->req_linger);
2684 osdc->num_requests = 0;
2685 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2686 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2687 spin_lock_init(&osdc->event_lock);
2688 osdc->event_tree = RB_ROOT;
2689 osdc->event_count = 0;
2690
2691 schedule_delayed_work(&osdc->osds_timeout_work,
2692 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
2693
2694 err = -ENOMEM;
2695 osdc->req_mempool = mempool_create_slab_pool(10,
2696 ceph_osd_request_cache);
2697 if (!osdc->req_mempool)
2698 goto out;
2699
2700 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2701 PAGE_SIZE, 10, true, "osd_op");
2702 if (err < 0)
2703 goto out_mempool;
2704 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2705 PAGE_SIZE, 10, true, "osd_op_reply");
2706 if (err < 0)
2707 goto out_msgpool;
2708
2709 err = -ENOMEM;
2710 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2711 if (!osdc->notify_wq)
2712 goto out_msgpool_reply;
2713
2714 return 0;
2715
2716 out_msgpool_reply:
2717 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2718 out_msgpool:
2719 ceph_msgpool_destroy(&osdc->msgpool_op);
2720 out_mempool:
2721 mempool_destroy(osdc->req_mempool);
2722 out:
2723 return err;
2724 }
2725
2726 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2727 {
2728 flush_workqueue(osdc->notify_wq);
2729 destroy_workqueue(osdc->notify_wq);
2730 cancel_delayed_work_sync(&osdc->timeout_work);
2731 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2732 if (osdc->osdmap) {
2733 ceph_osdmap_destroy(osdc->osdmap);
2734 osdc->osdmap = NULL;
2735 }
2736 remove_all_osds(osdc);
2737 mempool_destroy(osdc->req_mempool);
2738 ceph_msgpool_destroy(&osdc->msgpool_op);
2739 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2740 }
2741
2742 /*
2743 * Read some contiguous pages. If we cross a stripe boundary, shorten
2744 * *plen. Return number of bytes read, or error.
2745 */
2746 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2747 struct ceph_vino vino, struct ceph_file_layout *layout,
2748 u64 off, u64 *plen,
2749 u32 truncate_seq, u64 truncate_size,
2750 struct page **pages, int num_pages, int page_align)
2751 {
2752 struct ceph_osd_request *req;
2753 int rc = 0;
2754
2755 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2756 vino.snap, off, *plen);
2757 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
2758 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2759 NULL, truncate_seq, truncate_size,
2760 false);
2761 if (IS_ERR(req))
2762 return PTR_ERR(req);
2763
2764 /* it may be a short read due to an object boundary */
2765
2766 osd_req_op_extent_osd_data_pages(req, 0,
2767 pages, *plen, page_align, false, false);
2768
2769 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2770 off, *plen, *plen, page_align);
2771
2772 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2773
2774 rc = ceph_osdc_start_request(osdc, req, false);
2775 if (!rc)
2776 rc = ceph_osdc_wait_request(osdc, req);
2777
2778 ceph_osdc_put_request(req);
2779 dout("readpages result %d\n", rc);
2780 return rc;
2781 }
2782 EXPORT_SYMBOL(ceph_osdc_readpages);
2783
2784 /*
2785 * do a synchronous write on N pages
2786 */
2787 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2788 struct ceph_file_layout *layout,
2789 struct ceph_snap_context *snapc,
2790 u64 off, u64 len,
2791 u32 truncate_seq, u64 truncate_size,
2792 struct timespec *mtime,
2793 struct page **pages, int num_pages)
2794 {
2795 struct ceph_osd_request *req;
2796 int rc = 0;
2797 int page_align = off & ~PAGE_MASK;
2798
2799 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2800 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
2801 CEPH_OSD_OP_WRITE,
2802 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2803 snapc, truncate_seq, truncate_size,
2804 true);
2805 if (IS_ERR(req))
2806 return PTR_ERR(req);
2807
2808 /* it may be a short write due to an object boundary */
2809 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2810 false, false);
2811 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2812
2813 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2814
2815 rc = ceph_osdc_start_request(osdc, req, true);
2816 if (!rc)
2817 rc = ceph_osdc_wait_request(osdc, req);
2818
2819 ceph_osdc_put_request(req);
2820 if (rc == 0)
2821 rc = len;
2822 dout("writepages result %d\n", rc);
2823 return rc;
2824 }
2825 EXPORT_SYMBOL(ceph_osdc_writepages);
2826
2827 int ceph_osdc_setup(void)
2828 {
2829 size_t size = sizeof(struct ceph_osd_request) +
2830 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2831
2832 BUG_ON(ceph_osd_request_cache);
2833 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2834 0, 0, NULL);
2835
2836 return ceph_osd_request_cache ? 0 : -ENOMEM;
2837 }
2838 EXPORT_SYMBOL(ceph_osdc_setup);
2839
2840 void ceph_osdc_cleanup(void)
2841 {
2842 BUG_ON(!ceph_osd_request_cache);
2843 kmem_cache_destroy(ceph_osd_request_cache);
2844 ceph_osd_request_cache = NULL;
2845 }
2846 EXPORT_SYMBOL(ceph_osdc_cleanup);
2847
2848 /*
2849 * handle incoming message
2850 */
2851 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2852 {
2853 struct ceph_osd *osd = con->private;
2854 struct ceph_osd_client *osdc;
2855 int type = le16_to_cpu(msg->hdr.type);
2856
2857 if (!osd)
2858 goto out;
2859 osdc = osd->o_osdc;
2860
2861 switch (type) {
2862 case CEPH_MSG_OSD_MAP:
2863 ceph_osdc_handle_map(osdc, msg);
2864 break;
2865 case CEPH_MSG_OSD_OPREPLY:
2866 handle_reply(osdc, msg);
2867 break;
2868 case CEPH_MSG_WATCH_NOTIFY:
2869 handle_watch_notify(osdc, msg);
2870 break;
2871
2872 default:
2873 pr_err("received unknown message type %d %s\n", type,
2874 ceph_msg_type_name(type));
2875 }
2876 out:
2877 ceph_msg_put(msg);
2878 }
2879
2880 /*
2881 * Lookup and return message for incoming reply. Don't try to do
2882 * anything about a larger than preallocated data portion of the
2883 * message at the moment - for now, just skip the message.
2884 */
2885 static struct ceph_msg *get_reply(struct ceph_connection *con,
2886 struct ceph_msg_header *hdr,
2887 int *skip)
2888 {
2889 struct ceph_osd *osd = con->private;
2890 struct ceph_osd_client *osdc = osd->o_osdc;
2891 struct ceph_msg *m;
2892 struct ceph_osd_request *req;
2893 int front_len = le32_to_cpu(hdr->front_len);
2894 int data_len = le32_to_cpu(hdr->data_len);
2895 u64 tid;
2896
2897 tid = le64_to_cpu(hdr->tid);
2898 mutex_lock(&osdc->request_mutex);
2899 req = __lookup_request(osdc, tid);
2900 if (!req) {
2901 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2902 osd->o_osd, tid);
2903 m = NULL;
2904 *skip = 1;
2905 goto out;
2906 }
2907
2908 ceph_msg_revoke_incoming(req->r_reply);
2909
2910 if (front_len > req->r_reply->front_alloc_len) {
2911 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2912 __func__, osd->o_osd, req->r_tid, front_len,
2913 req->r_reply->front_alloc_len);
2914 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2915 false);
2916 if (!m)
2917 goto out;
2918 ceph_msg_put(req->r_reply);
2919 req->r_reply = m;
2920 }
2921
2922 if (data_len > req->r_reply->data_length) {
2923 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2924 __func__, osd->o_osd, req->r_tid, data_len,
2925 req->r_reply->data_length);
2926 m = NULL;
2927 *skip = 1;
2928 goto out;
2929 }
2930
2931 m = ceph_msg_get(req->r_reply);
2932 dout("get_reply tid %lld %p\n", tid, m);
2933
2934 out:
2935 mutex_unlock(&osdc->request_mutex);
2936 return m;
2937 }
2938
2939 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2940 struct ceph_msg_header *hdr,
2941 int *skip)
2942 {
2943 struct ceph_osd *osd = con->private;
2944 int type = le16_to_cpu(hdr->type);
2945 int front = le32_to_cpu(hdr->front_len);
2946
2947 *skip = 0;
2948 switch (type) {
2949 case CEPH_MSG_OSD_MAP:
2950 case CEPH_MSG_WATCH_NOTIFY:
2951 return ceph_msg_new(type, front, GFP_NOFS, false);
2952 case CEPH_MSG_OSD_OPREPLY:
2953 return get_reply(con, hdr, skip);
2954 default:
2955 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2956 osd->o_osd);
2957 *skip = 1;
2958 return NULL;
2959 }
2960 }
2961
2962 /*
2963 * Wrappers to refcount containing ceph_osd struct
2964 */
2965 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2966 {
2967 struct ceph_osd *osd = con->private;
2968 if (get_osd(osd))
2969 return con;
2970 return NULL;
2971 }
2972
2973 static void put_osd_con(struct ceph_connection *con)
2974 {
2975 struct ceph_osd *osd = con->private;
2976 put_osd(osd);
2977 }
2978
2979 /*
2980 * authentication
2981 */
2982 /*
2983 * Note: returned pointer is the address of a structure that's
2984 * managed separately. Caller must *not* attempt to free it.
2985 */
2986 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2987 int *proto, int force_new)
2988 {
2989 struct ceph_osd *o = con->private;
2990 struct ceph_osd_client *osdc = o->o_osdc;
2991 struct ceph_auth_client *ac = osdc->client->monc.auth;
2992 struct ceph_auth_handshake *auth = &o->o_auth;
2993
2994 if (force_new && auth->authorizer) {
2995 ceph_auth_destroy_authorizer(auth->authorizer);
2996 auth->authorizer = NULL;
2997 }
2998 if (!auth->authorizer) {
2999 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
3000 auth);
3001 if (ret)
3002 return ERR_PTR(ret);
3003 } else {
3004 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
3005 auth);
3006 if (ret)
3007 return ERR_PTR(ret);
3008 }
3009 *proto = ac->protocol;
3010
3011 return auth;
3012 }
3013
3014
3015 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3016 {
3017 struct ceph_osd *o = con->private;
3018 struct ceph_osd_client *osdc = o->o_osdc;
3019 struct ceph_auth_client *ac = osdc->client->monc.auth;
3020
3021 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
3022 }
3023
3024 static int invalidate_authorizer(struct ceph_connection *con)
3025 {
3026 struct ceph_osd *o = con->private;
3027 struct ceph_osd_client *osdc = o->o_osdc;
3028 struct ceph_auth_client *ac = osdc->client->monc.auth;
3029
3030 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
3031 return ceph_monc_validate_auth(&osdc->client->monc);
3032 }
3033
3034 static int osd_sign_message(struct ceph_msg *msg)
3035 {
3036 struct ceph_osd *o = msg->con->private;
3037 struct ceph_auth_handshake *auth = &o->o_auth;
3038
3039 return ceph_auth_sign_message(auth, msg);
3040 }
3041
3042 static int osd_check_message_signature(struct ceph_msg *msg)
3043 {
3044 struct ceph_osd *o = msg->con->private;
3045 struct ceph_auth_handshake *auth = &o->o_auth;
3046
3047 return ceph_auth_check_message_signature(auth, msg);
3048 }
3049
3050 static const struct ceph_connection_operations osd_con_ops = {
3051 .get = get_osd_con,
3052 .put = put_osd_con,
3053 .dispatch = dispatch,
3054 .get_authorizer = get_authorizer,
3055 .verify_authorizer_reply = verify_authorizer_reply,
3056 .invalidate_authorizer = invalidate_authorizer,
3057 .alloc_msg = alloc_msg,
3058 .sign_message = osd_sign_message,
3059 .check_message_signature = osd_check_message_signature,
3060 .fault = osd_reset,
3061 };
This page took 0.171282 seconds and 5 git commands to generate.