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