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