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