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