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