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