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