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