libceph: allocate ceph_osd with GFP_NOFAIL
[linux-2.6-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 866
0247a0cf
ID
867static bool osd_homeless(struct ceph_osd *osd)
868{
869 return osd->o_osd == CEPH_HOMELESS_OSD;
870}
871
f24e9980
SW
872static struct ceph_osd_request *
873__lookup_request_ge(struct ceph_osd_client *osdc,
874 u64 tid)
875{
876 struct ceph_osd_request *req;
877 struct rb_node *n = osdc->requests.rb_node;
878
879 while (n) {
880 req = rb_entry(n, struct ceph_osd_request, r_node);
881 if (tid < req->r_tid) {
882 if (!n->rb_left)
883 return req;
884 n = n->rb_left;
885 } else if (tid > req->r_tid) {
886 n = n->rb_right;
887 } else {
888 return req;
889 }
890 }
891 return NULL;
892}
893
2cc6128a
ID
894static void __kick_linger_request(struct ceph_osd_request *req)
895{
896 struct ceph_osd_client *osdc = req->r_osdc;
897 struct ceph_osd *osd = req->r_osd;
898
899 /*
900 * Linger requests need to be resent with a new tid to avoid
901 * the dup op detection logic on the OSDs. Achieve this with
902 * a re-register dance instead of open-coding.
903 */
904 ceph_osdc_get_request(req);
905 if (!list_empty(&req->r_linger_item))
906 __unregister_linger_request(osdc, req);
907 else
908 __unregister_request(osdc, req);
909 __register_request(osdc, req);
910 ceph_osdc_put_request(req);
911
912 /*
913 * Unless request has been registered as both normal and
914 * lingering, __unregister{,_linger}_request clears r_osd.
915 * However, here we need to preserve r_osd to make sure we
916 * requeue on the same OSD.
917 */
918 WARN_ON(req->r_osd || !osd);
919 req->r_osd = osd;
920
921 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
922 __enqueue_request(req);
923}
924
6f6c7006
SW
925/*
926 * Resubmit requests pending on the given osd.
927 */
928static void __kick_osd_requests(struct ceph_osd_client *osdc,
929 struct ceph_osd *osd)
930{
a40c4f10 931 struct ceph_osd_request *req, *nreq;
e02493c0 932 LIST_HEAD(resend);
2cc6128a 933 LIST_HEAD(resend_linger);
6f6c7006
SW
934 int err;
935
2cc6128a 936 dout("%s osd%d\n", __func__, osd->o_osd);
6f6c7006 937 err = __reset_osd(osdc, osd);
685a7555 938 if (err)
6f6c7006 939 return;
2cc6128a 940
e02493c0
AE
941 /*
942 * Build up a list of requests to resend by traversing the
943 * osd's list of requests. Requests for a given object are
944 * sent in tid order, and that is also the order they're
945 * kept on this list. Therefore all requests that are in
946 * flight will be found first, followed by all requests that
947 * have not yet been sent. And to resend requests while
948 * preserving this order we will want to put any sent
949 * requests back on the front of the osd client's unsent
950 * list.
951 *
952 * So we build a separate ordered list of already-sent
953 * requests for the affected osd and splice it onto the
954 * front of the osd client's unsent list. Once we've seen a
955 * request that has not yet been sent we're done. Those
956 * requests are already sitting right where they belong.
957 */
6f6c7006 958 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
e02493c0
AE
959 if (!req->r_sent)
960 break;
2cc6128a
ID
961
962 if (!req->r_linger) {
963 dout("%s requeueing %p tid %llu\n", __func__, req,
964 req->r_tid);
965 list_move_tail(&req->r_req_lru_item, &resend);
a40c4f10 966 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2cc6128a
ID
967 } else {
968 list_move_tail(&req->r_req_lru_item, &resend_linger);
969 }
a40c4f10 970 }
e02493c0 971 list_splice(&resend, &osdc->req_unsent);
a40c4f10 972
e02493c0 973 /*
2cc6128a
ID
974 * Both registered and not yet registered linger requests are
975 * enqueued with a new tid on the same OSD. We add/move them
976 * to req_unsent/o_requests at the end to keep things in tid
977 * order.
e02493c0 978 */
a40c4f10 979 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
1d0326b1 980 r_linger_osd_item) {
2cc6128a
ID
981 WARN_ON(!list_empty(&req->r_req_lru_item));
982 __kick_linger_request(req);
6f6c7006 983 }
2cc6128a
ID
984
985 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
986 __kick_linger_request(req);
6f6c7006
SW
987}
988
f24e9980 989/*
81b024e7 990 * If the osd connection drops, we need to resubmit all requests.
f24e9980
SW
991 */
992static void osd_reset(struct ceph_connection *con)
993{
994 struct ceph_osd *osd = con->private;
995 struct ceph_osd_client *osdc;
996
997 if (!osd)
998 return;
999 dout("osd_reset osd%d\n", osd->o_osd);
1000 osdc = osd->o_osdc;
f24e9980 1001 down_read(&osdc->map_sem);
83aff95e
SW
1002 mutex_lock(&osdc->request_mutex);
1003 __kick_osd_requests(osdc, osd);
f9d25199 1004 __send_queued(osdc);
83aff95e 1005 mutex_unlock(&osdc->request_mutex);
f24e9980
SW
1006 up_read(&osdc->map_sem);
1007}
1008
0247a0cf
ID
1009/*
1010 * Assumes @osd is zero-initialized.
1011 */
1012static void osd_init(struct ceph_osd *osd)
1013{
1014 atomic_set(&osd->o_ref, 1);
1015 RB_CLEAR_NODE(&osd->o_node);
1016 INIT_LIST_HEAD(&osd->o_requests);
1017 INIT_LIST_HEAD(&osd->o_linger_requests);
1018 INIT_LIST_HEAD(&osd->o_osd_lru);
1019 INIT_LIST_HEAD(&osd->o_keepalive_item);
1020 osd->o_incarnation = 1;
1021}
1022
1023static void osd_cleanup(struct ceph_osd *osd)
1024{
1025 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1026 WARN_ON(!list_empty(&osd->o_requests));
1027 WARN_ON(!list_empty(&osd->o_linger_requests));
1028 WARN_ON(!list_empty(&osd->o_osd_lru));
1029 WARN_ON(!list_empty(&osd->o_keepalive_item));
1030
1031 if (osd->o_auth.authorizer) {
1032 WARN_ON(osd_homeless(osd));
1033 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1034 }
1035}
1036
f24e9980
SW
1037/*
1038 * Track open sessions with osds.
1039 */
e10006f8 1040static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
f24e9980
SW
1041{
1042 struct ceph_osd *osd;
1043
0247a0cf
ID
1044 WARN_ON(onum == CEPH_HOMELESS_OSD);
1045
7a28f59b 1046 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
0247a0cf 1047 osd_init(osd);
f24e9980 1048 osd->o_osdc = osdc;
e10006f8 1049 osd->o_osd = onum;
f24e9980 1050
b7a9e5dd 1051 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
4e7a5dcd 1052
f24e9980
SW
1053 return osd;
1054}
1055
1056static struct ceph_osd *get_osd(struct ceph_osd *osd)
1057{
1058 if (atomic_inc_not_zero(&osd->o_ref)) {
1059 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1060 atomic_read(&osd->o_ref));
1061 return osd;
1062 } else {
1063 dout("get_osd %p FAIL\n", osd);
1064 return NULL;
1065 }
1066}
1067
1068static void put_osd(struct ceph_osd *osd)
1069{
1070 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1071 atomic_read(&osd->o_ref) - 1);
b28ec2f3 1072 if (atomic_dec_and_test(&osd->o_ref)) {
0247a0cf 1073 osd_cleanup(osd);
f24e9980 1074 kfree(osd);
79494d1b 1075 }
f24e9980
SW
1076}
1077
fcd00b68
ID
1078DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1079
f24e9980
SW
1080/*
1081 * remove an osd from our map
1082 */
f5a2041b 1083static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 1084{
7eb71e03 1085 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
cc9f1f51
ID
1086 WARN_ON(!list_empty(&osd->o_requests));
1087 WARN_ON(!list_empty(&osd->o_linger_requests));
7c6e6fc5 1088
f5a2041b 1089 list_del_init(&osd->o_osd_lru);
fcd00b68 1090 erase_osd(&osdc->osds, osd);
7eb71e03
ID
1091}
1092
1093static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1094{
1095 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1096
1097 if (!RB_EMPTY_NODE(&osd->o_node)) {
1098 ceph_con_close(&osd->o_con);
1099 __remove_osd(osdc, osd);
1100 put_osd(osd);
1101 }
f24e9980
SW
1102}
1103
f5a2041b
YS
1104static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1105 struct ceph_osd *osd)
1106{
bbf37ec3 1107 dout("%s %p\n", __func__, osd);
f5a2041b 1108 BUG_ON(!list_empty(&osd->o_osd_lru));
bbf37ec3 1109
f5a2041b 1110 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
a319bf56 1111 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
f5a2041b
YS
1112}
1113
bbf37ec3
ID
1114static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1115 struct ceph_osd *osd)
1116{
1117 dout("%s %p\n", __func__, osd);
1118
1119 if (list_empty(&osd->o_requests) &&
1120 list_empty(&osd->o_linger_requests))
1121 __move_osd_to_lru(osdc, osd);
1122}
1123
f5a2041b
YS
1124static void __remove_osd_from_lru(struct ceph_osd *osd)
1125{
1126 dout("__remove_osd_from_lru %p\n", osd);
1127 if (!list_empty(&osd->o_osd_lru))
1128 list_del_init(&osd->o_osd_lru);
1129}
1130
f24e9980
SW
1131/*
1132 * reset osd connect
1133 */
f5a2041b 1134static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 1135{
c3acb181 1136 struct ceph_entity_addr *peer_addr;
f24e9980 1137
f5a2041b 1138 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
a40c4f10
YS
1139 if (list_empty(&osd->o_requests) &&
1140 list_empty(&osd->o_linger_requests)) {
7eb71e03 1141 remove_osd(osdc, osd);
c3acb181
AE
1142 return -ENODEV;
1143 }
1144
1145 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1146 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1147 !ceph_con_opened(&osd->o_con)) {
1148 struct ceph_osd_request *req;
1149
0b4af2e8
ID
1150 dout("osd addr hasn't changed and connection never opened, "
1151 "letting msgr retry\n");
87b315a5
SW
1152 /* touch each r_stamp for handle_timeout()'s benfit */
1153 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1154 req->r_stamp = jiffies;
c3acb181
AE
1155
1156 return -EAGAIN;
f24e9980 1157 }
c3acb181
AE
1158
1159 ceph_con_close(&osd->o_con);
1160 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1161 osd->o_incarnation++;
1162
1163 return 0;
f24e9980
SW
1164}
1165
f24e9980
SW
1166/*
1167 * Register request, assign tid. If this is the first request, set up
1168 * the timeout event.
1169 */
a40c4f10
YS
1170static void __register_request(struct ceph_osd_client *osdc,
1171 struct ceph_osd_request *req)
f24e9980 1172{
f24e9980 1173 req->r_tid = ++osdc->last_tid;
6df058c0 1174 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
77f38e0e 1175 dout("__register_request %p tid %lld\n", req, req->r_tid);
fcd00b68 1176 insert_request(&osdc->requests, req);
f24e9980
SW
1177 ceph_osdc_get_request(req);
1178 osdc->num_requests++;
a40c4f10
YS
1179}
1180
f24e9980
SW
1181/*
1182 * called under osdc->request_mutex
1183 */
1184static void __unregister_request(struct ceph_osd_client *osdc,
1185 struct ceph_osd_request *req)
1186{
35f9f8a0
SW
1187 if (RB_EMPTY_NODE(&req->r_node)) {
1188 dout("__unregister_request %p tid %lld not registered\n",
1189 req, req->r_tid);
1190 return;
1191 }
1192
f24e9980 1193 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
fcd00b68 1194 erase_request(&osdc->requests, req);
f24e9980
SW
1195 osdc->num_requests--;
1196
0ba6478d
SW
1197 if (req->r_osd) {
1198 /* make sure the original request isn't in flight. */
6740a845 1199 ceph_msg_revoke(req->r_request);
0ba6478d
SW
1200
1201 list_del_init(&req->r_osd_item);
bbf37ec3 1202 maybe_move_osd_to_lru(osdc, req->r_osd);
4f23409e 1203 if (list_empty(&req->r_linger_osd_item))
a40c4f10 1204 req->r_osd = NULL;
0ba6478d 1205 }
f24e9980 1206
7d5f2481 1207 list_del_init(&req->r_req_lru_item);
f24e9980 1208 ceph_osdc_put_request(req);
f24e9980
SW
1209}
1210
1211/*
1212 * Cancel a previously queued request message
1213 */
1214static void __cancel_request(struct ceph_osd_request *req)
1215{
6bc18876 1216 if (req->r_sent && req->r_osd) {
6740a845 1217 ceph_msg_revoke(req->r_request);
f24e9980
SW
1218 req->r_sent = 0;
1219 }
1220}
1221
a40c4f10
YS
1222static void __register_linger_request(struct ceph_osd_client *osdc,
1223 struct ceph_osd_request *req)
1224{
af593064
ID
1225 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1226 WARN_ON(!req->r_linger);
1227
96e4dac6 1228 ceph_osdc_get_request(req);
a40c4f10 1229 list_add_tail(&req->r_linger_item, &osdc->req_linger);
6194ea89 1230 if (req->r_osd)
1d0326b1 1231 list_add_tail(&req->r_linger_osd_item,
6194ea89 1232 &req->r_osd->o_linger_requests);
a40c4f10
YS
1233}
1234
1235static void __unregister_linger_request(struct ceph_osd_client *osdc,
1236 struct ceph_osd_request *req)
1237{
af593064
ID
1238 WARN_ON(!req->r_linger);
1239
1240 if (list_empty(&req->r_linger_item)) {
1241 dout("%s %p tid %llu not registered\n", __func__, req,
1242 req->r_tid);
1243 return;
1244 }
1245
1246 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
61c74035 1247 list_del_init(&req->r_linger_item);
af593064 1248
a40c4f10 1249 if (req->r_osd) {
1d0326b1 1250 list_del_init(&req->r_linger_osd_item);
bbf37ec3 1251 maybe_move_osd_to_lru(osdc, req->r_osd);
fbdb9190
SW
1252 if (list_empty(&req->r_osd_item))
1253 req->r_osd = NULL;
a40c4f10 1254 }
96e4dac6 1255 ceph_osdc_put_request(req);
a40c4f10
YS
1256}
1257
a40c4f10
YS
1258void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1259 struct ceph_osd_request *req)
1260{
1261 if (!req->r_linger) {
1262 dout("set_request_linger %p\n", req);
1263 req->r_linger = 1;
a40c4f10
YS
1264 }
1265}
1266EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1267
63244fa1
ID
1268static bool __pool_full(struct ceph_pg_pool_info *pi)
1269{
1270 return pi->flags & CEPH_POOL_FLAG_FULL;
1271}
1272
42c1b124
ID
1273static bool have_pool_full(struct ceph_osd_client *osdc)
1274{
1275 struct rb_node *n;
1276
1277 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1278 struct ceph_pg_pool_info *pi =
1279 rb_entry(n, struct ceph_pg_pool_info, node);
1280
1281 if (__pool_full(pi))
1282 return true;
1283 }
1284
1285 return false;
1286}
1287
d29adb34
JD
1288/*
1289 * Returns whether a request should be blocked from being sent
1290 * based on the current osdmap and osd_client settings.
1291 *
1292 * Caller should hold map_sem for read.
1293 */
63244fa1
ID
1294static bool target_should_be_paused(struct ceph_osd_client *osdc,
1295 const struct ceph_osd_request_target *t,
1296 struct ceph_pg_pool_info *pi)
1297{
1298 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1299 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1300 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
1301 __pool_full(pi);
1302
1303 WARN_ON(pi->id != t->base_oloc.pool);
1304 return (t->flags & CEPH_OSD_FLAG_READ && pauserd) ||
1305 (t->flags & CEPH_OSD_FLAG_WRITE && pausewr);
1306}
1307
63244fa1
ID
1308enum calc_target_result {
1309 CALC_TARGET_NO_ACTION = 0,
1310 CALC_TARGET_NEED_RESEND,
1311 CALC_TARGET_POOL_DNE,
1312};
1313
1314static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1315 struct ceph_osd_request_target *t,
1316 u32 *last_force_resend,
1317 bool any_change)
1318{
1319 struct ceph_pg_pool_info *pi;
1320 struct ceph_pg pgid, last_pgid;
1321 struct ceph_osds up, acting;
1322 bool force_resend = false;
1323 bool need_check_tiering = false;
1324 bool need_resend = false;
1325 bool sort_bitwise = ceph_osdmap_flag(osdc->osdmap,
1326 CEPH_OSDMAP_SORTBITWISE);
1327 enum calc_target_result ct_res;
1328 int ret;
1329
1330 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1331 if (!pi) {
1332 t->osd = CEPH_HOMELESS_OSD;
1333 ct_res = CALC_TARGET_POOL_DNE;
1334 goto out;
1335 }
1336
1337 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1338 if (last_force_resend &&
1339 *last_force_resend < pi->last_force_request_resend) {
1340 *last_force_resend = pi->last_force_request_resend;
1341 force_resend = true;
1342 } else if (!last_force_resend) {
1343 force_resend = true;
1344 }
1345 }
1346 if (ceph_oid_empty(&t->target_oid) || force_resend) {
1347 ceph_oid_copy(&t->target_oid, &t->base_oid);
1348 need_check_tiering = true;
1349 }
1350 if (ceph_oloc_empty(&t->target_oloc) || force_resend) {
1351 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1352 need_check_tiering = true;
1353 }
1354
1355 if (need_check_tiering &&
1356 (t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1357 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1358 t->target_oloc.pool = pi->read_tier;
1359 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1360 t->target_oloc.pool = pi->write_tier;
1361 }
1362
1363 ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1364 &t->target_oloc, &pgid);
1365 if (ret) {
1366 WARN_ON(ret != -ENOENT);
1367 t->osd = CEPH_HOMELESS_OSD;
1368 ct_res = CALC_TARGET_POOL_DNE;
1369 goto out;
1370 }
1371 last_pgid.pool = pgid.pool;
1372 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1373
1374 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1375 if (any_change &&
1376 ceph_is_new_interval(&t->acting,
1377 &acting,
1378 &t->up,
1379 &up,
1380 t->size,
1381 pi->size,
1382 t->min_size,
1383 pi->min_size,
1384 t->pg_num,
1385 pi->pg_num,
1386 t->sort_bitwise,
1387 sort_bitwise,
1388 &last_pgid))
1389 force_resend = true;
1390
1391 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1392 t->paused = false;
1393 need_resend = true;
1394 }
1395
1396 if (ceph_pg_compare(&t->pgid, &pgid) ||
1397 ceph_osds_changed(&t->acting, &acting, any_change) ||
1398 force_resend) {
1399 t->pgid = pgid; /* struct */
1400 ceph_osds_copy(&t->acting, &acting);
1401 ceph_osds_copy(&t->up, &up);
1402 t->size = pi->size;
1403 t->min_size = pi->min_size;
1404 t->pg_num = pi->pg_num;
1405 t->pg_num_mask = pi->pg_num_mask;
1406 t->sort_bitwise = sort_bitwise;
1407
1408 t->osd = acting.primary;
1409 need_resend = true;
1410 }
1411
1412 ct_res = need_resend ? CALC_TARGET_NEED_RESEND : CALC_TARGET_NO_ACTION;
1413out:
1414 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1415 return ct_res;
1416}
1417
f671b581
ID
1418static void __enqueue_request(struct ceph_osd_request *req)
1419{
1420 struct ceph_osd_client *osdc = req->r_osdc;
1421
1422 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1423 req->r_osd ? req->r_osd->o_osd : -1);
1424
1425 if (req->r_osd) {
1426 __remove_osd_from_lru(req->r_osd);
1427 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1428 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1429 } else {
1430 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1431 }
1432}
1433
f24e9980
SW
1434/*
1435 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1436 * (as needed), and set the request r_osd appropriately. If there is
25985edc 1437 * no up osd, set r_osd to NULL. Move the request to the appropriate list
6f6c7006 1438 * (unsent, homeless) or leave on in-flight lru.
f24e9980
SW
1439 *
1440 * Return 0 if unchanged, 1 if changed, or negative on error.
1441 *
1442 * Caller should hold map_sem for read and request_mutex.
1443 */
6f6c7006 1444static int __map_request(struct ceph_osd_client *osdc,
38d6453c 1445 struct ceph_osd_request *req, int force_resend)
f24e9980 1446{
a66dd383 1447 enum calc_target_result ct_res;
f24e9980 1448 int err;
f24e9980 1449
6f6c7006 1450 dout("map_request %p tid %lld\n", req, req->r_tid);
17a13e40 1451
a66dd383
ID
1452 ct_res = calc_target(osdc, &req->r_t, NULL, force_resend);
1453 switch (ct_res) {
1454 case CALC_TARGET_POOL_DNE:
6f6c7006 1455 list_move(&req->r_req_lru_item, &osdc->req_notarget);
a66dd383
ID
1456 return -EIO;
1457 case CALC_TARGET_NO_ACTION:
f24e9980 1458 return 0; /* no change */
a66dd383
ID
1459 default:
1460 BUG_ON(ct_res != CALC_TARGET_NEED_RESEND);
1461 }
f24e9980 1462
5b191d99 1463 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
a66dd383 1464 req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed, req->r_t.osd,
f24e9980
SW
1465 req->r_osd ? req->r_osd->o_osd : -1);
1466
1467 if (req->r_osd) {
1468 __cancel_request(req);
1469 list_del_init(&req->r_osd_item);
a390de02 1470 list_del_init(&req->r_linger_osd_item);
f24e9980
SW
1471 req->r_osd = NULL;
1472 }
1473
a66dd383
ID
1474 req->r_osd = lookup_osd(&osdc->osds, req->r_t.osd);
1475 if (!req->r_osd && req->r_t.osd >= 0) {
c99eb1c7 1476 err = -ENOMEM;
a66dd383 1477 req->r_osd = create_osd(osdc, req->r_t.osd);
6f6c7006
SW
1478 if (!req->r_osd) {
1479 list_move(&req->r_req_lru_item, &osdc->req_notarget);
c99eb1c7 1480 goto out;
6f6c7006 1481 }
f24e9980 1482
6f3bfd45 1483 dout("map_request osd %p is osd%d\n", req->r_osd,
a66dd383 1484 req->r_osd->o_osd);
fcd00b68 1485 insert_osd(&osdc->osds, req->r_osd);
f24e9980 1486
b7a9e5dd 1487 ceph_con_open(&req->r_osd->o_con,
a66dd383
ID
1488 CEPH_ENTITY_TYPE_OSD, req->r_osd->o_osd,
1489 &osdc->osdmap->osd_addr[req->r_osd->o_osd]);
f24e9980
SW
1490 }
1491
f671b581 1492 __enqueue_request(req);
d85b7056 1493 err = 1; /* osd or pg changed */
f24e9980
SW
1494
1495out:
f24e9980
SW
1496 return err;
1497}
1498
bb873b53
ID
1499static void setup_request_data(struct ceph_osd_request *req,
1500 struct ceph_msg *msg)
f24e9980 1501{
bb873b53
ID
1502 u32 data_len = 0;
1503 int i;
1504
1505 if (!list_empty(&msg->data))
1506 return;
f24e9980 1507
bb873b53
ID
1508 WARN_ON(msg->data_length);
1509 for (i = 0; i < req->r_num_ops; i++) {
1510 struct ceph_osd_req_op *op = &req->r_ops[i];
1511
1512 switch (op->op) {
1513 /* request */
1514 case CEPH_OSD_OP_WRITE:
1515 case CEPH_OSD_OP_WRITEFULL:
1516 WARN_ON(op->indata_len != op->extent.length);
1517 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1518 break;
1519 case CEPH_OSD_OP_SETXATTR:
1520 case CEPH_OSD_OP_CMPXATTR:
1521 WARN_ON(op->indata_len != op->xattr.name_len +
1522 op->xattr.value_len);
1523 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1524 break;
1525
1526 /* reply */
1527 case CEPH_OSD_OP_STAT:
1528 ceph_osdc_msg_data_add(req->r_reply,
1529 &op->raw_data_in);
1530 break;
1531 case CEPH_OSD_OP_READ:
1532 ceph_osdc_msg_data_add(req->r_reply,
1533 &op->extent.osd_data);
1534 break;
1535
1536 /* both */
1537 case CEPH_OSD_OP_CALL:
1538 WARN_ON(op->indata_len != op->cls.class_len +
1539 op->cls.method_len +
1540 op->cls.indata_len);
1541 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1542 /* optional, can be NONE */
1543 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1544 /* optional, can be NONE */
1545 ceph_osdc_msg_data_add(req->r_reply,
1546 &op->cls.response_data);
1547 break;
1548 }
1549
1550 data_len += op->indata_len;
1551 }
1b83bef2 1552
bb873b53
ID
1553 WARN_ON(data_len != msg->data_length);
1554}
1555
1556static void encode_request(struct ceph_osd_request *req, struct ceph_msg *msg)
1557{
1558 void *p = msg->front.iov_base;
1559 void *const end = p + msg->front_alloc_len;
1560 u32 data_len = 0;
1561 int i;
1562
1563 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1564 /* snapshots aren't writeable */
1565 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1566 } else {
1567 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1568 req->r_data_offset || req->r_snapc);
1569 }
1570
1571 setup_request_data(req, msg);
1572
1573 ceph_encode_32(&p, 1); /* client_inc, always 1 */
1574 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1575 ceph_encode_32(&p, req->r_flags);
1576 ceph_encode_timespec(p, &req->r_mtime);
1577 p += sizeof(struct ceph_timespec);
1578 /* aka reassert_version */
1579 memcpy(p, &req->r_replay_version, sizeof(req->r_replay_version));
1580 p += sizeof(req->r_replay_version);
1581
1582 /* oloc */
1583 ceph_encode_8(&p, 4);
1584 ceph_encode_8(&p, 4);
1585 ceph_encode_32(&p, 8 + 4 + 4);
1586 ceph_encode_64(&p, req->r_t.target_oloc.pool);
1587 ceph_encode_32(&p, -1); /* preferred */
1588 ceph_encode_32(&p, 0); /* key len */
1589
1590 /* pgid */
1591 ceph_encode_8(&p, 1);
a66dd383
ID
1592 ceph_encode_64(&p, req->r_t.pgid.pool);
1593 ceph_encode_32(&p, req->r_t.pgid.seed);
bb873b53 1594 ceph_encode_32(&p, -1); /* preferred */
2169aea6 1595
bb873b53
ID
1596 /* oid */
1597 ceph_encode_32(&p, req->r_t.target_oid.name_len);
1598 memcpy(p, req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1599 p += req->r_t.target_oid.name_len;
f24e9980 1600
bb873b53
ID
1601 /* ops, can imply data */
1602 ceph_encode_16(&p, req->r_num_ops);
1603 for (i = 0; i < req->r_num_ops; i++) {
1604 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1605 p += sizeof(struct ceph_osd_op);
1606 }
26be8808 1607
bb873b53
ID
1608 ceph_encode_64(&p, req->r_snapid); /* snapid */
1609 if (req->r_snapc) {
1610 ceph_encode_64(&p, req->r_snapc->seq);
1611 ceph_encode_32(&p, req->r_snapc->num_snaps);
1612 for (i = 0; i < req->r_snapc->num_snaps; i++)
1613 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1614 } else {
1615 ceph_encode_64(&p, 0); /* snap_seq */
1616 ceph_encode_32(&p, 0); /* snaps len */
1617 }
1618
1619 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1620
1621 BUG_ON(p > end);
1622 msg->front.iov_len = p - msg->front.iov_base;
1623 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1624 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1625 msg->hdr.data_len = cpu_to_le32(data_len);
1626 /*
1627 * The header "data_off" is a hint to the receiver allowing it
1628 * to align received data into its buffers such that there's no
1629 * need to re-copy it before writing it to disk (direct I/O).
1630 */
1631 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
26be8808 1632
bb873b53
ID
1633 dout("%s req %p oid %*pE oid_len %d front %zu data %u\n", __func__,
1634 req, req->r_t.target_oid.name_len, req->r_t.target_oid.name,
1635 req->r_t.target_oid.name_len, msg->front.iov_len, data_len);
1636}
1637
1638/*
1639 * @req has to be assigned a tid and registered.
1640 */
1641static void send_request(struct ceph_osd_request *req)
1642{
1643 struct ceph_osd *osd = req->r_osd;
1644
1645 WARN_ON(osd->o_osd != req->r_t.osd);
1646
1647 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1648 if (req->r_attempts)
1649 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1650 else
1651 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1652
1653 encode_request(req, req->r_request);
1654
1655 dout("%s req %p tid %llu to pg %llu.%x osd%d flags 0x%x attempt %d\n",
1656 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
1657 req->r_t.osd, req->r_flags, req->r_attempts);
1658
1659 req->r_t.paused = false;
1660 req->r_stamp = jiffies;
1661 req->r_attempts++;
1662
1663 req->r_sent = osd->o_incarnation;
1664 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1665 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
f24e9980
SW
1666}
1667
6f6c7006
SW
1668/*
1669 * Send any requests in the queue (req_unsent).
1670 */
f9d25199 1671static void __send_queued(struct ceph_osd_client *osdc)
6f6c7006
SW
1672{
1673 struct ceph_osd_request *req, *tmp;
1674
f9d25199 1675 dout("__send_queued\n");
bb873b53
ID
1676 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1677 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1678 send_request(req);
1679 }
6f6c7006
SW
1680}
1681
42c1b124
ID
1682static void maybe_request_map(struct ceph_osd_client *osdc)
1683{
1684 bool continuous = false;
1685
1686 WARN_ON(!osdc->osdmap->epoch);
1687
1688 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
1689 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
1690 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR)) {
1691 dout("%s osdc %p continuous\n", __func__, osdc);
1692 continuous = true;
1693 } else {
1694 dout("%s osdc %p onetime\n", __func__, osdc);
1695 }
1696
1697 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
1698 osdc->osdmap->epoch + 1, continuous))
1699 ceph_monc_renew_subs(&osdc->client->monc);
1700}
1701
0bbfdfe8
ID
1702/*
1703 * Caller should hold map_sem for read and request_mutex.
1704 */
1705static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1706 struct ceph_osd_request *req,
1707 bool nofail)
1708{
1709 int rc;
1710
1711 __register_request(osdc, req);
1712 req->r_sent = 0;
1713 req->r_got_reply = 0;
1714 rc = __map_request(osdc, req, 0);
1715 if (rc < 0) {
1716 if (nofail) {
1717 dout("osdc_start_request failed map, "
1718 " will retry %lld\n", req->r_tid);
1719 rc = 0;
1720 } else {
1721 __unregister_request(osdc, req);
1722 }
1723 return rc;
1724 }
1725
1726 if (req->r_osd == NULL) {
1727 dout("send_request %p no up osds in pg\n", req);
1728 ceph_monc_request_next_osdmap(&osdc->client->monc);
1729 } else {
1730 __send_queued(osdc);
1731 }
1732
1733 return 0;
1734}
1735
fe5da05e
ID
1736static void __complete_request(struct ceph_osd_request *req)
1737{
1738 if (req->r_callback)
1739 req->r_callback(req);
1740 else
1741 complete_all(&req->r_completion);
1742}
1743
f24e9980 1744/*
fbca9635
ID
1745 * Timeout callback, called every N seconds. When 1 or more OSD
1746 * requests has been active for more than N seconds, we send a keepalive
1747 * (tag + timestamp) to its OSD to ensure any communications channel
1748 * reset is detected.
f24e9980
SW
1749 */
1750static void handle_timeout(struct work_struct *work)
1751{
1752 struct ceph_osd_client *osdc =
1753 container_of(work, struct ceph_osd_client, timeout_work.work);
a319bf56 1754 struct ceph_options *opts = osdc->client->options;
83aff95e 1755 struct ceph_osd_request *req;
f24e9980 1756 struct ceph_osd *osd;
422d2cb8 1757 struct list_head slow_osds;
f24e9980
SW
1758 dout("timeout\n");
1759 down_read(&osdc->map_sem);
1760
1761 ceph_monc_request_next_osdmap(&osdc->client->monc);
1762
1763 mutex_lock(&osdc->request_mutex);
f24e9980 1764
422d2cb8
YS
1765 /*
1766 * ping osds that are a bit slow. this ensures that if there
1767 * is a break in the TCP connection we will notice, and reopen
1768 * a connection with that osd (from the fault callback).
1769 */
1770 INIT_LIST_HEAD(&slow_osds);
1771 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
a319bf56
ID
1772 if (time_before(jiffies,
1773 req->r_stamp + opts->osd_keepalive_timeout))
422d2cb8
YS
1774 break;
1775
1776 osd = req->r_osd;
1777 BUG_ON(!osd);
1778 dout(" tid %llu is slow, will send keepalive on osd%d\n",
f24e9980 1779 req->r_tid, osd->o_osd);
422d2cb8
YS
1780 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1781 }
1782 while (!list_empty(&slow_osds)) {
1783 osd = list_entry(slow_osds.next, struct ceph_osd,
1784 o_keepalive_item);
1785 list_del_init(&osd->o_keepalive_item);
f24e9980
SW
1786 ceph_con_keepalive(&osd->o_con);
1787 }
1788
f9d25199 1789 __send_queued(osdc);
f24e9980 1790 mutex_unlock(&osdc->request_mutex);
f24e9980 1791 up_read(&osdc->map_sem);
fbca9635
ID
1792
1793 schedule_delayed_work(&osdc->timeout_work,
1794 osdc->client->options->osd_keepalive_timeout);
f24e9980
SW
1795}
1796
f5a2041b
YS
1797static void handle_osds_timeout(struct work_struct *work)
1798{
1799 struct ceph_osd_client *osdc =
1800 container_of(work, struct ceph_osd_client,
1801 osds_timeout_work.work);
a319bf56 1802 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
42a2c09f 1803 struct ceph_osd *osd, *nosd;
f5a2041b 1804
42a2c09f 1805 dout("%s osdc %p\n", __func__, osdc);
f5a2041b 1806 down_read(&osdc->map_sem);
42a2c09f
ID
1807 mutex_lock(&osdc->request_mutex);
1808
1809 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1810 if (time_before(jiffies, osd->lru_ttl))
1811 break;
1812
1813 remove_osd(osdc, osd);
1814 }
f5a2041b 1815
42a2c09f
ID
1816 mutex_unlock(&osdc->request_mutex);
1817 up_read(&osdc->map_sem);
f5a2041b
YS
1818 schedule_delayed_work(&osdc->osds_timeout_work,
1819 round_jiffies_relative(delay));
1820}
1821
205ee118
ID
1822static int ceph_oloc_decode(void **p, void *end,
1823 struct ceph_object_locator *oloc)
1824{
1825 u8 struct_v, struct_cv;
1826 u32 len;
1827 void *struct_end;
1828 int ret = 0;
1829
1830 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1831 struct_v = ceph_decode_8(p);
1832 struct_cv = ceph_decode_8(p);
1833 if (struct_v < 3) {
1834 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1835 struct_v, struct_cv);
1836 goto e_inval;
1837 }
1838 if (struct_cv > 6) {
1839 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1840 struct_v, struct_cv);
1841 goto e_inval;
1842 }
1843 len = ceph_decode_32(p);
1844 ceph_decode_need(p, end, len, e_inval);
1845 struct_end = *p + len;
1846
1847 oloc->pool = ceph_decode_64(p);
1848 *p += 4; /* skip preferred */
1849
1850 len = ceph_decode_32(p);
1851 if (len > 0) {
1852 pr_warn("ceph_object_locator::key is set\n");
1853 goto e_inval;
1854 }
1855
1856 if (struct_v >= 5) {
1857 len = ceph_decode_32(p);
1858 if (len > 0) {
1859 pr_warn("ceph_object_locator::nspace is set\n");
1860 goto e_inval;
1861 }
1862 }
1863
1864 if (struct_v >= 6) {
1865 s64 hash = ceph_decode_64(p);
1866 if (hash != -1) {
1867 pr_warn("ceph_object_locator::hash is set\n");
1868 goto e_inval;
1869 }
1870 }
1871
1872 /* skip the rest */
1873 *p = struct_end;
1874out:
1875 return ret;
1876
1877e_inval:
1878 ret = -EINVAL;
1879 goto out;
1880}
1881
1882static int ceph_redirect_decode(void **p, void *end,
1883 struct ceph_request_redirect *redir)
1884{
1885 u8 struct_v, struct_cv;
1886 u32 len;
1887 void *struct_end;
1888 int ret;
1889
1890 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1891 struct_v = ceph_decode_8(p);
1892 struct_cv = ceph_decode_8(p);
1893 if (struct_cv > 1) {
1894 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1895 struct_v, struct_cv);
1896 goto e_inval;
1897 }
1898 len = ceph_decode_32(p);
1899 ceph_decode_need(p, end, len, e_inval);
1900 struct_end = *p + len;
1901
1902 ret = ceph_oloc_decode(p, end, &redir->oloc);
1903 if (ret)
1904 goto out;
1905
1906 len = ceph_decode_32(p);
1907 if (len > 0) {
1908 pr_warn("ceph_request_redirect::object_name is set\n");
1909 goto e_inval;
1910 }
1911
1912 len = ceph_decode_32(p);
1913 *p += len; /* skip osd_instructions */
1914
1915 /* skip the rest */
1916 *p = struct_end;
1917out:
1918 return ret;
1919
1920e_inval:
1921 ret = -EINVAL;
1922 goto out;
1923}
1924
fe5da05e
ID
1925struct MOSDOpReply {
1926 struct ceph_pg pgid;
1927 u64 flags;
1928 int result;
1929 u32 epoch;
1930 int num_ops;
1931 u32 outdata_len[CEPH_OSD_MAX_OPS];
1932 s32 rval[CEPH_OSD_MAX_OPS];
1933 int retry_attempt;
1934 struct ceph_eversion replay_version;
1935 u64 user_version;
1936 struct ceph_request_redirect redirect;
1937};
25845472 1938
fe5da05e 1939static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
f24e9980 1940{
fe5da05e
ID
1941 void *p = msg->front.iov_base;
1942 void *const end = p + msg->front.iov_len;
1943 u16 version = le16_to_cpu(msg->hdr.version);
1944 struct ceph_eversion bad_replay_version;
b0b31a8f 1945 u8 decode_redir;
fe5da05e
ID
1946 u32 len;
1947 int ret;
1948 int i;
1b83bef2 1949
fe5da05e
ID
1950 ceph_decode_32_safe(&p, end, len, e_inval);
1951 ceph_decode_need(&p, end, len, e_inval);
1952 p += len; /* skip oid */
1b83bef2 1953
fe5da05e
ID
1954 ret = ceph_decode_pgid(&p, end, &m->pgid);
1955 if (ret)
1956 return ret;
1b83bef2 1957
fe5da05e
ID
1958 ceph_decode_64_safe(&p, end, m->flags, e_inval);
1959 ceph_decode_32_safe(&p, end, m->result, e_inval);
1960 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
1961 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
1962 p += sizeof(bad_replay_version);
1963 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
1b83bef2 1964
fe5da05e
ID
1965 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
1966 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
1967 goto e_inval;
1b83bef2 1968
fe5da05e
ID
1969 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
1970 e_inval);
1971 for (i = 0; i < m->num_ops; i++) {
1b83bef2 1972 struct ceph_osd_op *op = p;
1b83bef2 1973
fe5da05e 1974 m->outdata_len[i] = le32_to_cpu(op->payload_len);
1b83bef2
SW
1975 p += sizeof(*op);
1976 }
1b83bef2 1977
fe5da05e
ID
1978 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
1979 for (i = 0; i < m->num_ops; i++)
1980 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
f24e9980 1981
fe5da05e
ID
1982 if (version >= 5) {
1983 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
1984 memcpy(&m->replay_version, p, sizeof(m->replay_version));
1985 p += sizeof(m->replay_version);
1986 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
1987 } else {
1988 m->replay_version = bad_replay_version; /* struct */
1989 m->user_version = le64_to_cpu(m->replay_version.version);
1990 }
eb845ff1 1991
fe5da05e
ID
1992 if (version >= 6) {
1993 if (version >= 7)
1994 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
b0b31a8f
ID
1995 else
1996 decode_redir = 1;
1997 } else {
1998 decode_redir = 0;
1999 }
2000
2001 if (decode_redir) {
fe5da05e
ID
2002 ret = ceph_redirect_decode(&p, end, &m->redirect);
2003 if (ret)
2004 return ret;
205ee118 2005 } else {
fe5da05e 2006 ceph_oloc_init(&m->redirect.oloc);
205ee118 2007 }
f24e9980 2008
fe5da05e
ID
2009 return 0;
2010
2011e_inval:
2012 return -EINVAL;
2013}
2014
2015/*
2016 * We are done with @req if
2017 * - @m is a safe reply, or
2018 * - @m is an unsafe reply and we didn't want a safe one
2019 */
2020static bool done_request(const struct ceph_osd_request *req,
2021 const struct MOSDOpReply *m)
2022{
2023 return (m->result < 0 ||
2024 (m->flags & CEPH_OSD_FLAG_ONDISK) ||
2025 !(req->r_flags & CEPH_OSD_FLAG_ONDISK));
2026}
205ee118 2027
fe5da05e
ID
2028/*
2029 * handle osd op reply. either call the callback if it is specified,
2030 * or do the completion to wake up the waiting thread.
2031 *
2032 * ->r_unsafe_callback is set? yes no
2033 *
2034 * first reply is OK (needed r_cb/r_completion, r_cb/r_completion,
2035 * any or needed/got safe) r_safe_completion r_safe_completion
2036 *
2037 * first reply is unsafe r_unsafe_cb(true) (nothing)
2038 *
2039 * when we get the safe reply r_unsafe_cb(false), r_cb/r_completion,
2040 * r_safe_completion r_safe_completion
2041 */
2042static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2043{
2044 struct ceph_osd_request *req;
2045 struct MOSDOpReply m;
2046 u64 tid = le64_to_cpu(msg->hdr.tid);
2047 u32 data_len = 0;
2048 bool already_acked;
2049 int ret;
2050 int i;
2051
2052 dout("%s msg %p tid %llu\n", __func__, msg, tid);
2053
2054 down_read(&osdc->map_sem);
2055 mutex_lock(&osdc->request_mutex);
2056 req = lookup_request(&osdc->requests, tid);
2057 if (!req) {
2058 dout("%s no tid %llu\n", __func__, tid);
2059 goto out_unlock;
2060 }
2061 ceph_osdc_get_request(req);
2062
2063 ret = decode_MOSDOpReply(msg, &m);
2064 if (ret) {
2065 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
2066 req->r_tid, ret);
2067 ceph_msg_dump(msg);
2068 goto fail_request;
2069 }
2070 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
2071 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
2072 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
2073 le64_to_cpu(m.replay_version.version), m.user_version);
2074
2075 if (m.retry_attempt >= 0) {
2076 if (m.retry_attempt != req->r_attempts - 1) {
2077 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
2078 req, req->r_tid, m.retry_attempt,
2079 req->r_attempts - 1);
2080 goto out_put;
2081 }
2082 } else {
2083 WARN_ON(1); /* MOSDOpReply v4 is assumed */
2084 }
2085
2086 if (!ceph_oloc_empty(&m.redirect.oloc)) {
2087 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
2088 m.redirect.oloc.pool);
205ee118 2089 __unregister_request(osdc, req);
205ee118 2090
fe5da05e 2091 ceph_oloc_copy(&req->r_t.target_oloc, &m.redirect.oloc);
205ee118
ID
2092
2093 /*
2094 * Start redirect requests with nofail=true. If
2095 * mapping fails, request will end up on the notarget
2096 * list, waiting for the new osdmap (which can take
2097 * a while), even though the original request mapped
2098 * successfully. In the future we might want to follow
2099 * original request's nofail setting here.
2100 */
fe5da05e
ID
2101 ret = __ceph_osdc_start_request(osdc, req, true);
2102 BUG_ON(ret);
205ee118 2103
fe5da05e 2104 goto out_put;
205ee118
ID
2105 }
2106
fe5da05e
ID
2107 if (m.num_ops != req->r_num_ops) {
2108 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
2109 req->r_num_ops, req->r_tid);
2110 goto fail_request;
f24e9980 2111 }
fe5da05e
ID
2112 for (i = 0; i < req->r_num_ops; i++) {
2113 dout(" req %p tid %llu op %d rval %d len %u\n", req,
2114 req->r_tid, i, m.rval[i], m.outdata_len[i]);
2115 req->r_ops[i].rval = m.rval[i];
2116 req->r_ops[i].outdata_len = m.outdata_len[i];
2117 data_len += m.outdata_len[i];
2118 }
2119 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
2120 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
2121 le32_to_cpu(msg->hdr.data_len), req->r_tid);
2122 goto fail_request;
2123 }
2124 dout("%s req %p tid %llu acked %d result %d data_len %u\n", __func__,
2125 req, req->r_tid, req->r_got_reply, m.result, data_len);
2126
2127 already_acked = req->r_got_reply;
2128 if (!already_acked) {
2129 req->r_result = m.result ?: data_len;
2130 req->r_replay_version = m.replay_version; /* struct */
2131 req->r_got_reply = true;
2132 } else if (!(m.flags & CEPH_OSD_FLAG_ONDISK)) {
2133 dout("req %p tid %llu dup ack\n", req, req->r_tid);
2134 goto out_put;
2135 }
2136
2137 if (done_request(req, &m)) {
f24e9980 2138 __unregister_request(osdc, req);
fe5da05e
ID
2139 if (req->r_linger) {
2140 WARN_ON(req->r_unsafe_callback);
2141 __register_linger_request(osdc, req);
2142 }
2143 }
f24e9980
SW
2144
2145 mutex_unlock(&osdc->request_mutex);
ff513ace 2146 up_read(&osdc->map_sem);
f24e9980 2147
fe5da05e
ID
2148 if (done_request(req, &m)) {
2149 if (already_acked && req->r_unsafe_callback) {
2150 dout("req %p tid %llu safe-cb\n", req, req->r_tid);
61c5d6bf 2151 req->r_unsafe_callback(req, false);
fe5da05e
ID
2152 } else {
2153 dout("req %p tid %llu cb\n", req, req->r_tid);
2154 __complete_request(req);
2155 }
2156 } else {
2157 if (req->r_unsafe_callback) {
2158 dout("req %p tid %llu unsafe-cb\n", req, req->r_tid);
2159 req->r_unsafe_callback(req, true);
2160 } else {
2161 WARN_ON(1);
2162 }
61c5d6bf 2163 }
fe5da05e
ID
2164 if (m.flags & CEPH_OSD_FLAG_ONDISK)
2165 complete_all(&req->r_safe_completion);
f24e9980 2166
f24e9980
SW
2167 ceph_osdc_put_request(req);
2168 return;
2169
fe5da05e 2170fail_request:
37c89bde
LW
2171 req->r_result = -EIO;
2172 __unregister_request(osdc, req);
fe5da05e
ID
2173 __complete_request(req);
2174 complete_all(&req->r_safe_completion);
2175out_put:
1b83bef2 2176 ceph_osdc_put_request(req);
fe5da05e 2177out_unlock:
8058fd45 2178 mutex_unlock(&osdc->request_mutex);
ff513ace 2179 up_read(&osdc->map_sem);
f24e9980
SW
2180}
2181
42c1b124
ID
2182static void set_pool_was_full(struct ceph_osd_client *osdc)
2183{
2184 struct rb_node *n;
2185
2186 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
2187 struct ceph_pg_pool_info *pi =
2188 rb_entry(n, struct ceph_pg_pool_info, node);
2189
2190 pi->was_full = __pool_full(pi);
2191 }
2192}
2193
6f6c7006 2194static void reset_changed_osds(struct ceph_osd_client *osdc)
f24e9980 2195{
f24e9980 2196 struct rb_node *p, *n;
f24e9980 2197
7eb71e03 2198 dout("%s %p\n", __func__, osdc);
6f6c7006
SW
2199 for (p = rb_first(&osdc->osds); p; p = n) {
2200 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
f24e9980 2201
6f6c7006
SW
2202 n = rb_next(p);
2203 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
2204 memcmp(&osd->o_con.peer_addr,
2205 ceph_osd_addr(osdc->osdmap,
2206 osd->o_osd),
2207 sizeof(struct ceph_entity_addr)) != 0)
2208 __reset_osd(osdc, osd);
f24e9980 2209 }
422d2cb8
YS
2210}
2211
2212/*
6f6c7006
SW
2213 * Requeue requests whose mapping to an OSD has changed. If requests map to
2214 * no osd, request a new map.
422d2cb8 2215 *
e6d50f67 2216 * Caller should hold map_sem for read.
422d2cb8 2217 */
9a1ea2db
JD
2218static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
2219 bool force_resend_writes)
422d2cb8 2220{
a40c4f10 2221 struct ceph_osd_request *req, *nreq;
6f6c7006
SW
2222 struct rb_node *p;
2223 int needmap = 0;
2224 int err;
9a1ea2db 2225 bool force_resend_req;
422d2cb8 2226
9a1ea2db
JD
2227 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2228 force_resend_writes ? " (force resend writes)" : "");
422d2cb8 2229 mutex_lock(&osdc->request_mutex);
6194ea89 2230 for (p = rb_first(&osdc->requests); p; ) {
6f6c7006 2231 req = rb_entry(p, struct ceph_osd_request, r_node);
6194ea89 2232 p = rb_next(p);
ab60b16d
AE
2233
2234 /*
2235 * For linger requests that have not yet been
2236 * registered, move them to the linger list; they'll
2237 * be sent to the osd in the loop below. Unregister
2238 * the request before re-registering it as a linger
2239 * request to ensure the __map_request() below
2240 * will decide it needs to be sent.
2241 */
2242 if (req->r_linger && list_empty(&req->r_linger_item)) {
2243 dout("%p tid %llu restart on osd%d\n",
2244 req, req->r_tid,
2245 req->r_osd ? req->r_osd->o_osd : -1);
96e4dac6 2246 ceph_osdc_get_request(req);
ab60b16d
AE
2247 __unregister_request(osdc, req);
2248 __register_linger_request(osdc, req);
96e4dac6 2249 ceph_osdc_put_request(req);
ab60b16d
AE
2250 continue;
2251 }
2252
9a1ea2db
JD
2253 force_resend_req = force_resend ||
2254 (force_resend_writes &&
2255 req->r_flags & CEPH_OSD_FLAG_WRITE);
2256 err = __map_request(osdc, req, force_resend_req);
6f6c7006
SW
2257 if (err < 0)
2258 continue; /* error */
2259 if (req->r_osd == NULL) {
2260 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2261 needmap++; /* request a newer map */
2262 } else if (err > 0) {
6194ea89
SW
2263 if (!req->r_linger) {
2264 dout("%p tid %llu requeued on osd%d\n", req,
2265 req->r_tid,
2266 req->r_osd ? req->r_osd->o_osd : -1);
a40c4f10 2267 req->r_flags |= CEPH_OSD_FLAG_RETRY;
6194ea89
SW
2268 }
2269 }
a40c4f10
YS
2270 }
2271
2272 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2273 r_linger_item) {
2274 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2275
9a1ea2db
JD
2276 err = __map_request(osdc, req,
2277 force_resend || force_resend_writes);
ab60b16d 2278 dout("__map_request returned %d\n", err);
a40c4f10
YS
2279 if (err < 0)
2280 continue; /* hrm! */
b0494532
ID
2281 if (req->r_osd == NULL || err > 0) {
2282 if (req->r_osd == NULL) {
2283 dout("lingering %p tid %llu maps to no osd\n",
2284 req, req->r_tid);
2285 /*
2286 * A homeless lingering request makes
2287 * no sense, as it's job is to keep
2288 * a particular OSD connection open.
2289 * Request a newer map and kick the
2290 * request, knowing that it won't be
2291 * resent until we actually get a map
2292 * that can tell us where to send it.
2293 */
2294 needmap++;
2295 }
a40c4f10 2296
b0494532
ID
2297 dout("kicking lingering %p tid %llu osd%d\n", req,
2298 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2299 __register_request(osdc, req);
2300 __unregister_linger_request(osdc, req);
2301 }
6f6c7006 2302 }
14d2f38d 2303 reset_changed_osds(osdc);
f24e9980
SW
2304 mutex_unlock(&osdc->request_mutex);
2305
2306 if (needmap) {
2307 dout("%d requests for down osds, need new map\n", needmap);
2308 ceph_monc_request_next_osdmap(&osdc->client->monc);
2309 }
422d2cb8 2310}
6f6c7006 2311
42c1b124
ID
2312static int handle_one_map(struct ceph_osd_client *osdc,
2313 void *p, void *end, bool incremental)
2314{
2315 struct ceph_osdmap *newmap;
2316 struct rb_node *n;
2317 bool skipped_map = false;
2318 bool was_full;
2319
2320 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2321 set_pool_was_full(osdc);
2322
2323 if (incremental)
2324 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
2325 else
2326 newmap = ceph_osdmap_decode(&p, end);
2327 if (IS_ERR(newmap))
2328 return PTR_ERR(newmap);
2329
2330 if (newmap != osdc->osdmap) {
2331 /*
2332 * Preserve ->was_full before destroying the old map.
2333 * For pools that weren't in the old map, ->was_full
2334 * should be false.
2335 */
2336 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
2337 struct ceph_pg_pool_info *pi =
2338 rb_entry(n, struct ceph_pg_pool_info, node);
2339 struct ceph_pg_pool_info *old_pi;
2340
2341 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
2342 if (old_pi)
2343 pi->was_full = old_pi->was_full;
2344 else
2345 WARN_ON(pi->was_full);
2346 }
2347
2348 if (osdc->osdmap->epoch &&
2349 osdc->osdmap->epoch + 1 < newmap->epoch) {
2350 WARN_ON(incremental);
2351 skipped_map = true;
2352 }
2353
2354 ceph_osdmap_destroy(osdc->osdmap);
2355 osdc->osdmap = newmap;
2356 }
2357
2358 was_full &= !ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2359 kick_requests(osdc, skipped_map, was_full);
2360
2361 return 0;
2362}
6f6c7006 2363
f24e9980
SW
2364/*
2365 * Process updated osd map.
2366 *
2367 * The message contains any number of incremental and full maps, normally
2368 * indicating some sort of topology change in the cluster. Kick requests
2369 * off to different OSDs as needed.
2370 */
2371void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2372{
42c1b124
ID
2373 void *p = msg->front.iov_base;
2374 void *const end = p + msg->front.iov_len;
f24e9980
SW
2375 u32 nr_maps, maplen;
2376 u32 epoch;
f24e9980 2377 struct ceph_fsid fsid;
42c1b124
ID
2378 bool handled_incremental = false;
2379 bool was_pauserd, was_pausewr;
2380 bool pauserd, pausewr;
2381 int err;
f24e9980 2382
42c1b124
ID
2383 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
2384 down_write(&osdc->map_sem);
f24e9980
SW
2385
2386 /* verify fsid */
2387 ceph_decode_need(&p, end, sizeof(fsid), bad);
2388 ceph_decode_copy(&p, &fsid, sizeof(fsid));
0743304d 2389 if (ceph_check_fsid(osdc->client, &fsid) < 0)
42c1b124 2390 goto bad;
f24e9980 2391
42c1b124
ID
2392 was_pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
2393 was_pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
2394 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2395 have_pool_full(osdc);
9a1ea2db 2396
f24e9980
SW
2397 /* incremental maps */
2398 ceph_decode_32_safe(&p, end, nr_maps, bad);
2399 dout(" %d inc maps\n", nr_maps);
2400 while (nr_maps > 0) {
2401 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
2402 epoch = ceph_decode_32(&p);
2403 maplen = ceph_decode_32(&p);
f24e9980 2404 ceph_decode_need(&p, end, maplen, bad);
42c1b124
ID
2405 if (osdc->osdmap->epoch &&
2406 osdc->osdmap->epoch + 1 == epoch) {
f24e9980
SW
2407 dout("applying incremental map %u len %d\n",
2408 epoch, maplen);
42c1b124
ID
2409 err = handle_one_map(osdc, p, p + maplen, true);
2410 if (err)
f24e9980 2411 goto bad;
42c1b124 2412 handled_incremental = true;
f24e9980
SW
2413 } else {
2414 dout("ignoring incremental map %u len %d\n",
2415 epoch, maplen);
2416 }
42c1b124 2417 p += maplen;
f24e9980
SW
2418 nr_maps--;
2419 }
42c1b124 2420 if (handled_incremental)
f24e9980
SW
2421 goto done;
2422
2423 /* full maps */
2424 ceph_decode_32_safe(&p, end, nr_maps, bad);
2425 dout(" %d full maps\n", nr_maps);
2426 while (nr_maps) {
2427 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
2428 epoch = ceph_decode_32(&p);
2429 maplen = ceph_decode_32(&p);
f24e9980
SW
2430 ceph_decode_need(&p, end, maplen, bad);
2431 if (nr_maps > 1) {
2432 dout("skipping non-latest full map %u len %d\n",
2433 epoch, maplen);
e5253a7b 2434 } else if (osdc->osdmap->epoch >= epoch) {
f24e9980
SW
2435 dout("skipping full map %u len %d, "
2436 "older than our %u\n", epoch, maplen,
2437 osdc->osdmap->epoch);
2438 } else {
2439 dout("taking full map %u len %d\n", epoch, maplen);
42c1b124
ID
2440 err = handle_one_map(osdc, p, p + maplen, false);
2441 if (err)
f24e9980 2442 goto bad;
f24e9980
SW
2443 }
2444 p += maplen;
2445 nr_maps--;
2446 }
2447
2448done:
cd634fb6
SW
2449 /*
2450 * subscribe to subsequent osdmap updates if full to ensure
2451 * we find out when we are no longer full and stop returning
2452 * ENOSPC.
2453 */
42c1b124
ID
2454 pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
2455 pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
2456 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2457 have_pool_full(osdc);
2458 if (was_pauserd || was_pausewr || pauserd || pausewr)
2459 maybe_request_map(osdc);
cd634fb6 2460
f9d25199
AE
2461 mutex_lock(&osdc->request_mutex);
2462 __send_queued(osdc);
2463 mutex_unlock(&osdc->request_mutex);
42c1b124
ID
2464
2465 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2466 osdc->osdmap->epoch);
2467 up_write(&osdc->map_sem);
03066f23 2468 wake_up_all(&osdc->client->auth_wq);
f24e9980
SW
2469 return;
2470
2471bad:
2472 pr_err("osdc handle_map corrupt msg\n");
9ec7cab1 2473 ceph_msg_dump(msg);
f24e9980 2474 up_write(&osdc->map_sem);
f24e9980
SW
2475}
2476
a40c4f10
YS
2477/*
2478 * watch/notify callback event infrastructure
2479 *
2480 * These callbacks are used both for watch and notify operations.
2481 */
2482static void __release_event(struct kref *kref)
2483{
2484 struct ceph_osd_event *event =
2485 container_of(kref, struct ceph_osd_event, kref);
2486
2487 dout("__release_event %p\n", event);
2488 kfree(event);
2489}
2490
2491static void get_event(struct ceph_osd_event *event)
2492{
2493 kref_get(&event->kref);
2494}
2495
2496void ceph_osdc_put_event(struct ceph_osd_event *event)
2497{
2498 kref_put(&event->kref, __release_event);
2499}
2500EXPORT_SYMBOL(ceph_osdc_put_event);
2501
2502static void __insert_event(struct ceph_osd_client *osdc,
2503 struct ceph_osd_event *new)
2504{
2505 struct rb_node **p = &osdc->event_tree.rb_node;
2506 struct rb_node *parent = NULL;
2507 struct ceph_osd_event *event = NULL;
2508
2509 while (*p) {
2510 parent = *p;
2511 event = rb_entry(parent, struct ceph_osd_event, node);
2512 if (new->cookie < event->cookie)
2513 p = &(*p)->rb_left;
2514 else if (new->cookie > event->cookie)
2515 p = &(*p)->rb_right;
2516 else
2517 BUG();
2518 }
2519
2520 rb_link_node(&new->node, parent, p);
2521 rb_insert_color(&new->node, &osdc->event_tree);
2522}
2523
2524static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2525 u64 cookie)
2526{
2527 struct rb_node **p = &osdc->event_tree.rb_node;
2528 struct rb_node *parent = NULL;
2529 struct ceph_osd_event *event = NULL;
2530
2531 while (*p) {
2532 parent = *p;
2533 event = rb_entry(parent, struct ceph_osd_event, node);
2534 if (cookie < event->cookie)
2535 p = &(*p)->rb_left;
2536 else if (cookie > event->cookie)
2537 p = &(*p)->rb_right;
2538 else
2539 return event;
2540 }
2541 return NULL;
2542}
2543
2544static void __remove_event(struct ceph_osd_event *event)
2545{
2546 struct ceph_osd_client *osdc = event->osdc;
2547
2548 if (!RB_EMPTY_NODE(&event->node)) {
2549 dout("__remove_event removed %p\n", event);
2550 rb_erase(&event->node, &osdc->event_tree);
2551 ceph_osdc_put_event(event);
2552 } else {
2553 dout("__remove_event didn't remove %p\n", event);
2554 }
2555}
2556
2557int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2558 void (*event_cb)(u64, u64, u8, void *),
3c663bbd 2559 void *data, struct ceph_osd_event **pevent)
a40c4f10
YS
2560{
2561 struct ceph_osd_event *event;
2562
2563 event = kmalloc(sizeof(*event), GFP_NOIO);
2564 if (!event)
2565 return -ENOMEM;
2566
2567 dout("create_event %p\n", event);
2568 event->cb = event_cb;
3c663bbd 2569 event->one_shot = 0;
a40c4f10
YS
2570 event->data = data;
2571 event->osdc = osdc;
2572 INIT_LIST_HEAD(&event->osd_node);
3ee5234d 2573 RB_CLEAR_NODE(&event->node);
a40c4f10
YS
2574 kref_init(&event->kref); /* one ref for us */
2575 kref_get(&event->kref); /* one ref for the caller */
a40c4f10
YS
2576
2577 spin_lock(&osdc->event_lock);
2578 event->cookie = ++osdc->event_count;
2579 __insert_event(osdc, event);
2580 spin_unlock(&osdc->event_lock);
2581
2582 *pevent = event;
2583 return 0;
2584}
2585EXPORT_SYMBOL(ceph_osdc_create_event);
2586
2587void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2588{
2589 struct ceph_osd_client *osdc = event->osdc;
2590
2591 dout("cancel_event %p\n", event);
2592 spin_lock(&osdc->event_lock);
2593 __remove_event(event);
2594 spin_unlock(&osdc->event_lock);
2595 ceph_osdc_put_event(event); /* caller's */
2596}
2597EXPORT_SYMBOL(ceph_osdc_cancel_event);
2598
2599
2600static void do_event_work(struct work_struct *work)
2601{
2602 struct ceph_osd_event_work *event_work =
2603 container_of(work, struct ceph_osd_event_work, work);
2604 struct ceph_osd_event *event = event_work->event;
2605 u64 ver = event_work->ver;
2606 u64 notify_id = event_work->notify_id;
2607 u8 opcode = event_work->opcode;
2608
2609 dout("do_event_work completing %p\n", event);
2610 event->cb(ver, notify_id, opcode, event->data);
a40c4f10
YS
2611 dout("do_event_work completed %p\n", event);
2612 ceph_osdc_put_event(event);
2613 kfree(event_work);
2614}
2615
2616
2617/*
2618 * Process osd watch notifications
2619 */
3c663bbd
AE
2620static void handle_watch_notify(struct ceph_osd_client *osdc,
2621 struct ceph_msg *msg)
a40c4f10
YS
2622{
2623 void *p, *end;
2624 u8 proto_ver;
2625 u64 cookie, ver, notify_id;
2626 u8 opcode;
2627 struct ceph_osd_event *event;
2628 struct ceph_osd_event_work *event_work;
2629
2630 p = msg->front.iov_base;
2631 end = p + msg->front.iov_len;
2632
2633 ceph_decode_8_safe(&p, end, proto_ver, bad);
2634 ceph_decode_8_safe(&p, end, opcode, bad);
2635 ceph_decode_64_safe(&p, end, cookie, bad);
2636 ceph_decode_64_safe(&p, end, ver, bad);
2637 ceph_decode_64_safe(&p, end, notify_id, bad);
2638
2639 spin_lock(&osdc->event_lock);
2640 event = __find_event(osdc, cookie);
2641 if (event) {
3c663bbd 2642 BUG_ON(event->one_shot);
a40c4f10 2643 get_event(event);
a40c4f10
YS
2644 }
2645 spin_unlock(&osdc->event_lock);
2646 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2647 cookie, ver, event);
2648 if (event) {
2649 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
a40c4f10 2650 if (!event_work) {
91883cd2
ID
2651 pr_err("couldn't allocate event_work\n");
2652 ceph_osdc_put_event(event);
2653 return;
a40c4f10 2654 }
6b0ae409 2655 INIT_WORK(&event_work->work, do_event_work);
a40c4f10
YS
2656 event_work->event = event;
2657 event_work->ver = ver;
2658 event_work->notify_id = notify_id;
2659 event_work->opcode = opcode;
a40c4f10 2660
91883cd2
ID
2661 queue_work(osdc->notify_wq, &event_work->work);
2662 }
a40c4f10 2663
a40c4f10
YS
2664 return;
2665
2666bad:
2667 pr_err("osdc handle_watch_notify corrupt msg\n");
a40c4f10
YS
2668}
2669
70636773
AE
2670/*
2671 * Register request, send initial attempt.
2672 */
2673int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2674 struct ceph_osd_request *req,
2675 bool nofail)
2676{
0bbfdfe8 2677 int rc;
70636773 2678
f24e9980
SW
2679 down_read(&osdc->map_sem);
2680 mutex_lock(&osdc->request_mutex);
0bbfdfe8
ID
2681
2682 rc = __ceph_osdc_start_request(osdc, req, nofail);
2683
f24e9980
SW
2684 mutex_unlock(&osdc->request_mutex);
2685 up_read(&osdc->map_sem);
0bbfdfe8 2686
f24e9980
SW
2687 return rc;
2688}
3d14c5d2 2689EXPORT_SYMBOL(ceph_osdc_start_request);
f24e9980 2690
c9f9b93d
ID
2691/*
2692 * Unregister a registered request. The request is not completed (i.e.
2693 * no callbacks or wakeups) - higher layers are supposed to know what
2694 * they are canceling.
2695 */
2696void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2697{
2698 struct ceph_osd_client *osdc = req->r_osdc;
2699
2700 mutex_lock(&osdc->request_mutex);
2701 if (req->r_linger)
2702 __unregister_linger_request(osdc, req);
2703 __unregister_request(osdc, req);
2704 mutex_unlock(&osdc->request_mutex);
2705
2706 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2707}
2708EXPORT_SYMBOL(ceph_osdc_cancel_request);
2709
f24e9980
SW
2710/*
2711 * wait for a request to complete
2712 */
2713int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2714 struct ceph_osd_request *req)
2715{
2716 int rc;
2717
c9f9b93d
ID
2718 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2719
f24e9980
SW
2720 rc = wait_for_completion_interruptible(&req->r_completion);
2721 if (rc < 0) {
c9f9b93d
ID
2722 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2723 ceph_osdc_cancel_request(req);
fe5da05e
ID
2724
2725 /* kludge - need to to wake ceph_osdc_sync() */
2726 complete_all(&req->r_safe_completion);
f24e9980
SW
2727 return rc;
2728 }
2729
c9f9b93d
ID
2730 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2731 req->r_result);
f24e9980
SW
2732 return req->r_result;
2733}
3d14c5d2 2734EXPORT_SYMBOL(ceph_osdc_wait_request);
f24e9980
SW
2735
2736/*
2737 * sync - wait for all in-flight requests to flush. avoid starvation.
2738 */
2739void ceph_osdc_sync(struct ceph_osd_client *osdc)
2740{
2741 struct ceph_osd_request *req;
2742 u64 last_tid, next_tid = 0;
2743
2744 mutex_lock(&osdc->request_mutex);
2745 last_tid = osdc->last_tid;
2746 while (1) {
2747 req = __lookup_request_ge(osdc, next_tid);
2748 if (!req)
2749 break;
2750 if (req->r_tid > last_tid)
2751 break;
2752
2753 next_tid = req->r_tid + 1;
2754 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2755 continue;
2756
2757 ceph_osdc_get_request(req);
2758 mutex_unlock(&osdc->request_mutex);
2759 dout("sync waiting on tid %llu (last is %llu)\n",
2760 req->r_tid, last_tid);
2761 wait_for_completion(&req->r_safe_completion);
2762 mutex_lock(&osdc->request_mutex);
2763 ceph_osdc_put_request(req);
2764 }
2765 mutex_unlock(&osdc->request_mutex);
2766 dout("sync done (thru tid %llu)\n", last_tid);
2767}
3d14c5d2 2768EXPORT_SYMBOL(ceph_osdc_sync);
f24e9980 2769
dd935f44
JD
2770/*
2771 * Call all pending notify callbacks - for use after a watch is
2772 * unregistered, to make sure no more callbacks for it will be invoked
2773 */
f6479449 2774void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
dd935f44
JD
2775{
2776 flush_workqueue(osdc->notify_wq);
2777}
2778EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2779
2780
f24e9980
SW
2781/*
2782 * init, shutdown
2783 */
2784int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2785{
2786 int err;
2787
2788 dout("init\n");
2789 osdc->client = client;
f24e9980 2790 init_rwsem(&osdc->map_sem);
f24e9980 2791 mutex_init(&osdc->request_mutex);
f24e9980
SW
2792 osdc->last_tid = 0;
2793 osdc->osds = RB_ROOT;
f5a2041b 2794 INIT_LIST_HEAD(&osdc->osd_lru);
f24e9980 2795 osdc->requests = RB_ROOT;
422d2cb8 2796 INIT_LIST_HEAD(&osdc->req_lru);
6f6c7006
SW
2797 INIT_LIST_HEAD(&osdc->req_unsent);
2798 INIT_LIST_HEAD(&osdc->req_notarget);
a40c4f10 2799 INIT_LIST_HEAD(&osdc->req_linger);
f24e9980
SW
2800 osdc->num_requests = 0;
2801 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
f5a2041b 2802 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
a40c4f10
YS
2803 spin_lock_init(&osdc->event_lock);
2804 osdc->event_tree = RB_ROOT;
2805 osdc->event_count = 0;
f5a2041b 2806
5f44f142 2807 err = -ENOMEM;
e5253a7b
ID
2808 osdc->osdmap = ceph_osdmap_alloc();
2809 if (!osdc->osdmap)
2810 goto out;
2811
9e767adb
ID
2812 osdc->req_mempool = mempool_create_slab_pool(10,
2813 ceph_osd_request_cache);
f24e9980 2814 if (!osdc->req_mempool)
e5253a7b 2815 goto out_map;
f24e9980 2816
d50b409f 2817 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
711da55d 2818 PAGE_SIZE, 10, true, "osd_op");
f24e9980 2819 if (err < 0)
5f44f142 2820 goto out_mempool;
d50b409f 2821 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
711da55d 2822 PAGE_SIZE, 10, true, "osd_op_reply");
c16e7869
SW
2823 if (err < 0)
2824 goto out_msgpool;
a40c4f10 2825
dbcae088 2826 err = -ENOMEM;
a40c4f10 2827 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
dbcae088 2828 if (!osdc->notify_wq)
c172ec5c
ID
2829 goto out_msgpool_reply;
2830
fbca9635
ID
2831 schedule_delayed_work(&osdc->timeout_work,
2832 osdc->client->options->osd_keepalive_timeout);
b37ee1b9
ID
2833 schedule_delayed_work(&osdc->osds_timeout_work,
2834 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
2835
f24e9980 2836 return 0;
5f44f142 2837
c172ec5c
ID
2838out_msgpool_reply:
2839 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
c16e7869
SW
2840out_msgpool:
2841 ceph_msgpool_destroy(&osdc->msgpool_op);
5f44f142
SW
2842out_mempool:
2843 mempool_destroy(osdc->req_mempool);
e5253a7b
ID
2844out_map:
2845 ceph_osdmap_destroy(osdc->osdmap);
5f44f142
SW
2846out:
2847 return err;
f24e9980
SW
2848}
2849
2850void ceph_osdc_stop(struct ceph_osd_client *osdc)
2851{
a40c4f10
YS
2852 flush_workqueue(osdc->notify_wq);
2853 destroy_workqueue(osdc->notify_wq);
f24e9980 2854 cancel_delayed_work_sync(&osdc->timeout_work);
f5a2041b 2855 cancel_delayed_work_sync(&osdc->osds_timeout_work);
42a2c09f
ID
2856
2857 mutex_lock(&osdc->request_mutex);
2858 while (!RB_EMPTY_ROOT(&osdc->osds)) {
2859 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
2860 struct ceph_osd, o_node);
2861 remove_osd(osdc, osd);
2862 }
2863 mutex_unlock(&osdc->request_mutex);
2864
e5253a7b 2865 ceph_osdmap_destroy(osdc->osdmap);
f24e9980
SW
2866 mempool_destroy(osdc->req_mempool);
2867 ceph_msgpool_destroy(&osdc->msgpool_op);
c16e7869 2868 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
f24e9980
SW
2869}
2870
2871/*
2872 * Read some contiguous pages. If we cross a stripe boundary, shorten
2873 * *plen. Return number of bytes read, or error.
2874 */
2875int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2876 struct ceph_vino vino, struct ceph_file_layout *layout,
2877 u64 off, u64 *plen,
2878 u32 truncate_seq, u64 truncate_size,
b7495fc2 2879 struct page **pages, int num_pages, int page_align)
f24e9980
SW
2880{
2881 struct ceph_osd_request *req;
2882 int rc = 0;
2883
2884 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2885 vino.snap, off, *plen);
715e4cd4 2886 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
f24e9980 2887 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
acead002 2888 NULL, truncate_seq, truncate_size,
153e5167 2889 false);
6816282d
SW
2890 if (IS_ERR(req))
2891 return PTR_ERR(req);
f24e9980
SW
2892
2893 /* it may be a short read due to an object boundary */
406e2c9f 2894 osd_req_op_extent_osd_data_pages(req, 0,
a4ce40a9 2895 pages, *plen, page_align, false, false);
f24e9980 2896
e0c59487 2897 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
43bfe5de 2898 off, *plen, *plen, page_align);
f24e9980
SW
2899
2900 rc = ceph_osdc_start_request(osdc, req, false);
2901 if (!rc)
2902 rc = ceph_osdc_wait_request(osdc, req);
2903
2904 ceph_osdc_put_request(req);
2905 dout("readpages result %d\n", rc);
2906 return rc;
2907}
3d14c5d2 2908EXPORT_SYMBOL(ceph_osdc_readpages);
f24e9980
SW
2909
2910/*
2911 * do a synchronous write on N pages
2912 */
2913int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2914 struct ceph_file_layout *layout,
2915 struct ceph_snap_context *snapc,
2916 u64 off, u64 len,
2917 u32 truncate_seq, u64 truncate_size,
2918 struct timespec *mtime,
24808826 2919 struct page **pages, int num_pages)
f24e9980
SW
2920{
2921 struct ceph_osd_request *req;
2922 int rc = 0;
b7495fc2 2923 int page_align = off & ~PAGE_MASK;
f24e9980 2924
715e4cd4 2925 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
f24e9980 2926 CEPH_OSD_OP_WRITE,
24808826 2927 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
acead002 2928 snapc, truncate_seq, truncate_size,
153e5167 2929 true);
6816282d
SW
2930 if (IS_ERR(req))
2931 return PTR_ERR(req);
f24e9980
SW
2932
2933 /* it may be a short write due to an object boundary */
406e2c9f 2934 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
43bfe5de
AE
2935 false, false);
2936 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
f24e9980 2937
bb873b53 2938 req->r_mtime = *mtime;
87f979d3 2939 rc = ceph_osdc_start_request(osdc, req, true);
f24e9980
SW
2940 if (!rc)
2941 rc = ceph_osdc_wait_request(osdc, req);
2942
2943 ceph_osdc_put_request(req);
2944 if (rc == 0)
2945 rc = len;
2946 dout("writepages result %d\n", rc);
2947 return rc;
2948}
3d14c5d2 2949EXPORT_SYMBOL(ceph_osdc_writepages);
f24e9980 2950
5522ae0b
AE
2951int ceph_osdc_setup(void)
2952{
3f1af42a
ID
2953 size_t size = sizeof(struct ceph_osd_request) +
2954 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2955
5522ae0b 2956 BUG_ON(ceph_osd_request_cache);
3f1af42a
ID
2957 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2958 0, 0, NULL);
5522ae0b
AE
2959
2960 return ceph_osd_request_cache ? 0 : -ENOMEM;
2961}
2962EXPORT_SYMBOL(ceph_osdc_setup);
2963
2964void ceph_osdc_cleanup(void)
2965{
2966 BUG_ON(!ceph_osd_request_cache);
2967 kmem_cache_destroy(ceph_osd_request_cache);
2968 ceph_osd_request_cache = NULL;
2969}
2970EXPORT_SYMBOL(ceph_osdc_cleanup);
2971
f24e9980
SW
2972/*
2973 * handle incoming message
2974 */
2975static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2976{
2977 struct ceph_osd *osd = con->private;
32c895e7 2978 struct ceph_osd_client *osdc;
f24e9980
SW
2979 int type = le16_to_cpu(msg->hdr.type);
2980
2981 if (!osd)
4a32f93d 2982 goto out;
32c895e7 2983 osdc = osd->o_osdc;
f24e9980
SW
2984
2985 switch (type) {
2986 case CEPH_MSG_OSD_MAP:
2987 ceph_osdc_handle_map(osdc, msg);
2988 break;
2989 case CEPH_MSG_OSD_OPREPLY:
70cf052d 2990 handle_reply(osdc, msg);
f24e9980 2991 break;
a40c4f10
YS
2992 case CEPH_MSG_WATCH_NOTIFY:
2993 handle_watch_notify(osdc, msg);
2994 break;
f24e9980
SW
2995
2996 default:
2997 pr_err("received unknown message type %d %s\n", type,
2998 ceph_msg_type_name(type));
2999 }
4a32f93d 3000out:
f24e9980
SW
3001 ceph_msg_put(msg);
3002}
3003
5b3a4db3 3004/*
d15f9d69
ID
3005 * Lookup and return message for incoming reply. Don't try to do
3006 * anything about a larger than preallocated data portion of the
3007 * message at the moment - for now, just skip the message.
5b3a4db3
SW
3008 */
3009static struct ceph_msg *get_reply(struct ceph_connection *con,
2450418c
YS
3010 struct ceph_msg_header *hdr,
3011 int *skip)
f24e9980
SW
3012{
3013 struct ceph_osd *osd = con->private;
3014 struct ceph_osd_client *osdc = osd->o_osdc;
2450418c 3015 struct ceph_msg *m;
0547a9b3 3016 struct ceph_osd_request *req;
3f0a4ac5 3017 int front_len = le32_to_cpu(hdr->front_len);
5b3a4db3 3018 int data_len = le32_to_cpu(hdr->data_len);
0547a9b3 3019 u64 tid;
f24e9980 3020
0547a9b3
YS
3021 tid = le64_to_cpu(hdr->tid);
3022 mutex_lock(&osdc->request_mutex);
fcd00b68 3023 req = lookup_request(&osdc->requests, tid);
0547a9b3 3024 if (!req) {
cd8140c6
ID
3025 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
3026 osd->o_osd, tid);
0547a9b3 3027 m = NULL;
d15f9d69 3028 *skip = 1;
0547a9b3
YS
3029 goto out;
3030 }
c16e7869 3031
ace6d3a9 3032 ceph_msg_revoke_incoming(req->r_reply);
0547a9b3 3033
f2be82b0 3034 if (front_len > req->r_reply->front_alloc_len) {
d15f9d69
ID
3035 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
3036 __func__, osd->o_osd, req->r_tid, front_len,
3037 req->r_reply->front_alloc_len);
3f0a4ac5
ID
3038 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
3039 false);
a79832f2 3040 if (!m)
c16e7869
SW
3041 goto out;
3042 ceph_msg_put(req->r_reply);
3043 req->r_reply = m;
3044 }
0fff87ec 3045
d15f9d69
ID
3046 if (data_len > req->r_reply->data_length) {
3047 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
3048 __func__, osd->o_osd, req->r_tid, data_len,
3049 req->r_reply->data_length);
3050 m = NULL;
3051 *skip = 1;
3052 goto out;
0547a9b3 3053 }
d15f9d69
ID
3054
3055 m = ceph_msg_get(req->r_reply);
c16e7869 3056 dout("get_reply tid %lld %p\n", tid, m);
0547a9b3
YS
3057
3058out:
3059 mutex_unlock(&osdc->request_mutex);
2450418c 3060 return m;
5b3a4db3
SW
3061}
3062
3063static struct ceph_msg *alloc_msg(struct ceph_connection *con,
3064 struct ceph_msg_header *hdr,
3065 int *skip)
3066{
3067 struct ceph_osd *osd = con->private;
3068 int type = le16_to_cpu(hdr->type);
3069 int front = le32_to_cpu(hdr->front_len);
3070
1c20f2d2 3071 *skip = 0;
5b3a4db3
SW
3072 switch (type) {
3073 case CEPH_MSG_OSD_MAP:
a40c4f10 3074 case CEPH_MSG_WATCH_NOTIFY:
b61c2763 3075 return ceph_msg_new(type, front, GFP_NOFS, false);
5b3a4db3
SW
3076 case CEPH_MSG_OSD_OPREPLY:
3077 return get_reply(con, hdr, skip);
3078 default:
3079 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
3080 osd->o_osd);
3081 *skip = 1;
3082 return NULL;
3083 }
f24e9980
SW
3084}
3085
3086/*
3087 * Wrappers to refcount containing ceph_osd struct
3088 */
3089static struct ceph_connection *get_osd_con(struct ceph_connection *con)
3090{
3091 struct ceph_osd *osd = con->private;
3092 if (get_osd(osd))
3093 return con;
3094 return NULL;
3095}
3096
3097static void put_osd_con(struct ceph_connection *con)
3098{
3099 struct ceph_osd *osd = con->private;
3100 put_osd(osd);
3101}
3102
4e7a5dcd
SW
3103/*
3104 * authentication
3105 */
a3530df3
AE
3106/*
3107 * Note: returned pointer is the address of a structure that's
3108 * managed separately. Caller must *not* attempt to free it.
3109 */
3110static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 3111 int *proto, int force_new)
4e7a5dcd
SW
3112{
3113 struct ceph_osd *o = con->private;
3114 struct ceph_osd_client *osdc = o->o_osdc;
3115 struct ceph_auth_client *ac = osdc->client->monc.auth;
74f1869f 3116 struct ceph_auth_handshake *auth = &o->o_auth;
4e7a5dcd 3117
74f1869f 3118 if (force_new && auth->authorizer) {
6c1ea260 3119 ceph_auth_destroy_authorizer(auth->authorizer);
74f1869f
AE
3120 auth->authorizer = NULL;
3121 }
27859f97
SW
3122 if (!auth->authorizer) {
3123 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
3124 auth);
4e7a5dcd 3125 if (ret)
a3530df3 3126 return ERR_PTR(ret);
27859f97
SW
3127 } else {
3128 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
0bed9b5c
SW
3129 auth);
3130 if (ret)
3131 return ERR_PTR(ret);
4e7a5dcd 3132 }
4e7a5dcd 3133 *proto = ac->protocol;
74f1869f 3134
a3530df3 3135 return auth;
4e7a5dcd
SW
3136}
3137
3138
3139static int verify_authorizer_reply(struct ceph_connection *con, int len)
3140{
3141 struct ceph_osd *o = con->private;
3142 struct ceph_osd_client *osdc = o->o_osdc;
3143 struct ceph_auth_client *ac = osdc->client->monc.auth;
3144
27859f97 3145 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
4e7a5dcd
SW
3146}
3147
9bd2e6f8
SW
3148static int invalidate_authorizer(struct ceph_connection *con)
3149{
3150 struct ceph_osd *o = con->private;
3151 struct ceph_osd_client *osdc = o->o_osdc;
3152 struct ceph_auth_client *ac = osdc->client->monc.auth;
3153
27859f97 3154 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
9bd2e6f8
SW
3155 return ceph_monc_validate_auth(&osdc->client->monc);
3156}
4e7a5dcd 3157
79dbd1ba 3158static int osd_sign_message(struct ceph_msg *msg)
33d07337 3159{
79dbd1ba 3160 struct ceph_osd *o = msg->con->private;
33d07337 3161 struct ceph_auth_handshake *auth = &o->o_auth;
79dbd1ba 3162
33d07337
YZ
3163 return ceph_auth_sign_message(auth, msg);
3164}
3165
79dbd1ba 3166static int osd_check_message_signature(struct ceph_msg *msg)
33d07337 3167{
79dbd1ba 3168 struct ceph_osd *o = msg->con->private;
33d07337 3169 struct ceph_auth_handshake *auth = &o->o_auth;
79dbd1ba 3170
33d07337
YZ
3171 return ceph_auth_check_message_signature(auth, msg);
3172}
3173
9e32789f 3174static const struct ceph_connection_operations osd_con_ops = {
f24e9980
SW
3175 .get = get_osd_con,
3176 .put = put_osd_con,
3177 .dispatch = dispatch,
4e7a5dcd
SW
3178 .get_authorizer = get_authorizer,
3179 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 3180 .invalidate_authorizer = invalidate_authorizer,
f24e9980 3181 .alloc_msg = alloc_msg,
79dbd1ba
ID
3182 .sign_message = osd_sign_message,
3183 .check_message_signature = osd_check_message_signature,
81b024e7 3184 .fault = osd_reset,
f24e9980 3185};