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