]> Git Repo - linux.git/blame - net/ceph/osd_client.c
libceph: enable fallback to ceph_msg_new() in ceph_msgpool_get()
[linux.git] / net / ceph / osd_client.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
a4ce40a9 2
3d14c5d2 3#include <linux/ceph/ceph_debug.h>
f24e9980 4
3d14c5d2 5#include <linux/module.h>
f24e9980
SW
6#include <linux/err.h>
7#include <linux/highmem.h>
8#include <linux/mm.h>
9#include <linux/pagemap.h>
10#include <linux/slab.h>
11#include <linux/uaccess.h>
68b4476b
YS
12#ifdef CONFIG_BLOCK
13#include <linux/bio.h>
14#endif
f24e9980 15
8cb441c0 16#include <linux/ceph/ceph_features.h>
3d14c5d2
YS
17#include <linux/ceph/libceph.h>
18#include <linux/ceph/osd_client.h>
19#include <linux/ceph/messenger.h>
20#include <linux/ceph/decode.h>
21#include <linux/ceph/auth.h>
22#include <linux/ceph/pagelist.h>
08c1ac50 23#include <linux/ceph/striper.h>
f24e9980 24
c16e7869 25#define OSD_OPREPLY_FRONT_LEN 512
0d59ab81 26
5522ae0b
AE
27static struct kmem_cache *ceph_osd_request_cache;
28
9e32789f 29static const struct ceph_connection_operations osd_con_ops;
f24e9980 30
f24e9980
SW
31/*
32 * Implement client access to distributed object storage cluster.
33 *
34 * All data objects are stored within a cluster/cloud of OSDs, or
35 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
36 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
37 * remote daemons serving up and coordinating consistent and safe
38 * access to storage.
39 *
40 * Cluster membership and the mapping of data objects onto storage devices
41 * are described by the osd map.
42 *
43 * We keep track of pending OSD requests (read, write), resubmit
44 * requests to different OSDs when the cluster topology/data layout
45 * change, or retry the affected requests when the communications
46 * channel with an OSD is reset.
47 */
48
5aea3dcd
ID
49static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
50static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
922dab61
ID
51static void link_linger(struct ceph_osd *osd,
52 struct ceph_osd_linger_request *lreq);
53static void unlink_linger(struct ceph_osd *osd,
54 struct ceph_osd_linger_request *lreq);
a02a946d 55static void clear_backoffs(struct ceph_osd *osd);
5aea3dcd
ID
56
57#if 1
58static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
59{
60 bool wrlocked = true;
61
62 if (unlikely(down_read_trylock(sem))) {
63 wrlocked = false;
64 up_read(sem);
65 }
66
67 return wrlocked;
68}
69static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
70{
71 WARN_ON(!rwsem_is_locked(&osdc->lock));
72}
73static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
74{
75 WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
76}
77static inline void verify_osd_locked(struct ceph_osd *osd)
78{
79 struct ceph_osd_client *osdc = osd->o_osdc;
80
81 WARN_ON(!(mutex_is_locked(&osd->lock) &&
82 rwsem_is_locked(&osdc->lock)) &&
83 !rwsem_is_wrlocked(&osdc->lock));
84}
922dab61
ID
85static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
86{
87 WARN_ON(!mutex_is_locked(&lreq->lock));
88}
5aea3dcd
ID
89#else
90static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
91static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
92static inline void verify_osd_locked(struct ceph_osd *osd) { }
922dab61 93static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
5aea3dcd
ID
94#endif
95
f24e9980
SW
96/*
97 * calculate the mapping of a file extent onto an object, and fill out the
98 * request accordingly. shorten extent as necessary if it crosses an
99 * object boundary.
100 *
101 * fill osd op in request message.
102 */
dbe0fc41 103static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
a19dadfb 104 u64 *objnum, u64 *objoff, u64 *objlen)
f24e9980 105{
60e56f13 106 u64 orig_len = *plen;
dccbf080 107 u32 xlen;
f24e9980 108
60e56f13 109 /* object extent? */
dccbf080
ID
110 ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
111 objoff, &xlen);
112 *objlen = xlen;
75d1c941
AE
113 if (*objlen < orig_len) {
114 *plen = *objlen;
60e56f13
AE
115 dout(" skipping last %llu, final file extent %llu~%llu\n",
116 orig_len - *plen, off, *plen);
117 }
118
75d1c941 119 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
3ff5f385 120 return 0;
f24e9980
SW
121}
122
c54d47bf
AE
123static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124{
125 memset(osd_data, 0, sizeof (*osd_data));
126 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127}
128
89486833
ID
129/*
130 * Consumes @pages if @own_pages is true.
131 */
a4ce40a9 132static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
43bfe5de
AE
133 struct page **pages, u64 length, u32 alignment,
134 bool pages_from_pool, bool own_pages)
135{
136 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
137 osd_data->pages = pages;
138 osd_data->length = length;
139 osd_data->alignment = alignment;
140 osd_data->pages_from_pool = pages_from_pool;
141 osd_data->own_pages = own_pages;
142}
43bfe5de 143
89486833
ID
144/*
145 * Consumes a ref on @pagelist.
146 */
a4ce40a9 147static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
43bfe5de
AE
148 struct ceph_pagelist *pagelist)
149{
150 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
151 osd_data->pagelist = pagelist;
152}
43bfe5de
AE
153
154#ifdef CONFIG_BLOCK
a4ce40a9 155static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
5359a17d
ID
156 struct ceph_bio_iter *bio_pos,
157 u32 bio_length)
43bfe5de
AE
158{
159 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
5359a17d 160 osd_data->bio_pos = *bio_pos;
43bfe5de
AE
161 osd_data->bio_length = bio_length;
162}
43bfe5de
AE
163#endif /* CONFIG_BLOCK */
164
b9e281c2 165static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
0010f705
ID
166 struct ceph_bvec_iter *bvec_pos,
167 u32 num_bvecs)
b9e281c2
ID
168{
169 osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
170 osd_data->bvec_pos = *bvec_pos;
0010f705 171 osd_data->num_bvecs = num_bvecs;
b9e281c2
ID
172}
173
8a703a38
IC
174#define osd_req_op_data(oreq, whch, typ, fld) \
175({ \
176 struct ceph_osd_request *__oreq = (oreq); \
177 unsigned int __whch = (whch); \
178 BUG_ON(__whch >= __oreq->r_num_ops); \
179 &__oreq->r_ops[__whch].typ.fld; \
180})
863c7eb5 181
49719778
AE
182static struct ceph_osd_data *
183osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
184{
185 BUG_ON(which >= osd_req->r_num_ops);
186
187 return &osd_req->r_ops[which].raw_data_in;
188}
189
a4ce40a9
AE
190struct ceph_osd_data *
191osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
406e2c9f 192 unsigned int which)
a4ce40a9 193{
863c7eb5 194 return osd_req_op_data(osd_req, which, extent, osd_data);
a4ce40a9
AE
195}
196EXPORT_SYMBOL(osd_req_op_extent_osd_data);
197
49719778
AE
198void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
199 unsigned int which, struct page **pages,
200 u64 length, u32 alignment,
201 bool pages_from_pool, bool own_pages)
202{
203 struct ceph_osd_data *osd_data;
204
205 osd_data = osd_req_op_raw_data_in(osd_req, which);
206 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
207 pages_from_pool, own_pages);
208}
209EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
210
a4ce40a9 211void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
406e2c9f
AE
212 unsigned int which, struct page **pages,
213 u64 length, u32 alignment,
a4ce40a9
AE
214 bool pages_from_pool, bool own_pages)
215{
216 struct ceph_osd_data *osd_data;
217
863c7eb5 218 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
a4ce40a9
AE
219 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
220 pages_from_pool, own_pages);
a4ce40a9
AE
221}
222EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
223
224void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
406e2c9f 225 unsigned int which, struct ceph_pagelist *pagelist)
a4ce40a9
AE
226{
227 struct ceph_osd_data *osd_data;
228
863c7eb5 229 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
a4ce40a9 230 ceph_osd_data_pagelist_init(osd_data, pagelist);
a4ce40a9
AE
231}
232EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
233
234#ifdef CONFIG_BLOCK
235void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
5359a17d
ID
236 unsigned int which,
237 struct ceph_bio_iter *bio_pos,
238 u32 bio_length)
a4ce40a9
AE
239{
240 struct ceph_osd_data *osd_data;
863c7eb5
AE
241
242 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
5359a17d 243 ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
a4ce40a9
AE
244}
245EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
246#endif /* CONFIG_BLOCK */
247
0010f705
ID
248void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
249 unsigned int which,
250 struct bio_vec *bvecs, u32 num_bvecs,
251 u32 bytes)
252{
253 struct ceph_osd_data *osd_data;
254 struct ceph_bvec_iter it = {
255 .bvecs = bvecs,
256 .iter = { .bi_size = bytes },
257 };
258
259 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
260 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
261}
262EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
263
b9e281c2
ID
264void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
265 unsigned int which,
266 struct ceph_bvec_iter *bvec_pos)
267{
268 struct ceph_osd_data *osd_data;
269
270 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
0010f705 271 ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
b9e281c2
ID
272}
273EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
274
a4ce40a9
AE
275static void osd_req_op_cls_request_info_pagelist(
276 struct ceph_osd_request *osd_req,
277 unsigned int which, struct ceph_pagelist *pagelist)
278{
279 struct ceph_osd_data *osd_data;
280
863c7eb5 281 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
a4ce40a9 282 ceph_osd_data_pagelist_init(osd_data, pagelist);
a4ce40a9
AE
283}
284
04017e29
AE
285void osd_req_op_cls_request_data_pagelist(
286 struct ceph_osd_request *osd_req,
287 unsigned int which, struct ceph_pagelist *pagelist)
288{
289 struct ceph_osd_data *osd_data;
290
863c7eb5 291 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
04017e29 292 ceph_osd_data_pagelist_init(osd_data, pagelist);
bb873b53
ID
293 osd_req->r_ops[which].cls.indata_len += pagelist->length;
294 osd_req->r_ops[which].indata_len += pagelist->length;
04017e29
AE
295}
296EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
297
6c57b554
AE
298void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
299 unsigned int which, struct page **pages, u64 length,
300 u32 alignment, bool pages_from_pool, bool own_pages)
301{
302 struct ceph_osd_data *osd_data;
303
304 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
305 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
306 pages_from_pool, own_pages);
bb873b53
ID
307 osd_req->r_ops[which].cls.indata_len += length;
308 osd_req->r_ops[which].indata_len += length;
6c57b554
AE
309}
310EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
311
b9e281c2
ID
312void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
313 unsigned int which,
0010f705
ID
314 struct bio_vec *bvecs, u32 num_bvecs,
315 u32 bytes)
b9e281c2
ID
316{
317 struct ceph_osd_data *osd_data;
318 struct ceph_bvec_iter it = {
319 .bvecs = bvecs,
320 .iter = { .bi_size = bytes },
321 };
322
323 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
0010f705 324 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
b9e281c2
ID
325 osd_req->r_ops[which].cls.indata_len += bytes;
326 osd_req->r_ops[which].indata_len += bytes;
327}
328EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
329
a4ce40a9
AE
330void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
331 unsigned int which, struct page **pages, u64 length,
332 u32 alignment, bool pages_from_pool, bool own_pages)
333{
334 struct ceph_osd_data *osd_data;
335
863c7eb5 336 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
a4ce40a9
AE
337 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
338 pages_from_pool, own_pages);
a4ce40a9
AE
339}
340EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
341
23c08a9c
AE
342static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
343{
344 switch (osd_data->type) {
345 case CEPH_OSD_DATA_TYPE_NONE:
346 return 0;
347 case CEPH_OSD_DATA_TYPE_PAGES:
348 return osd_data->length;
349 case CEPH_OSD_DATA_TYPE_PAGELIST:
350 return (u64)osd_data->pagelist->length;
351#ifdef CONFIG_BLOCK
352 case CEPH_OSD_DATA_TYPE_BIO:
353 return (u64)osd_data->bio_length;
354#endif /* CONFIG_BLOCK */
b9e281c2
ID
355 case CEPH_OSD_DATA_TYPE_BVECS:
356 return osd_data->bvec_pos.iter.bi_size;
23c08a9c
AE
357 default:
358 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
359 return 0;
360 }
361}
362
c54d47bf
AE
363static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
364{
5476492f 365 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
c54d47bf
AE
366 int num_pages;
367
368 num_pages = calc_pages_for((u64)osd_data->alignment,
369 (u64)osd_data->length);
370 ceph_release_page_vector(osd_data->pages, num_pages);
89486833
ID
371 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
372 ceph_pagelist_release(osd_data->pagelist);
c54d47bf 373 }
5476492f
AE
374 ceph_osd_data_init(osd_data);
375}
376
377static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
378 unsigned int which)
379{
380 struct ceph_osd_req_op *op;
381
382 BUG_ON(which >= osd_req->r_num_ops);
383 op = &osd_req->r_ops[which];
384
385 switch (op->op) {
386 case CEPH_OSD_OP_READ:
387 case CEPH_OSD_OP_WRITE:
e30b7577 388 case CEPH_OSD_OP_WRITEFULL:
5476492f
AE
389 ceph_osd_data_release(&op->extent.osd_data);
390 break;
391 case CEPH_OSD_OP_CALL:
392 ceph_osd_data_release(&op->cls.request_info);
04017e29 393 ceph_osd_data_release(&op->cls.request_data);
5476492f
AE
394 ceph_osd_data_release(&op->cls.response_data);
395 break;
d74b50be
YZ
396 case CEPH_OSD_OP_SETXATTR:
397 case CEPH_OSD_OP_CMPXATTR:
398 ceph_osd_data_release(&op->xattr.osd_data);
399 break;
66ba609f
YZ
400 case CEPH_OSD_OP_STAT:
401 ceph_osd_data_release(&op->raw_data_in);
402 break;
922dab61
ID
403 case CEPH_OSD_OP_NOTIFY_ACK:
404 ceph_osd_data_release(&op->notify_ack.request_data);
405 break;
19079203
ID
406 case CEPH_OSD_OP_NOTIFY:
407 ceph_osd_data_release(&op->notify.request_data);
408 ceph_osd_data_release(&op->notify.response_data);
409 break;
a4ed38d7
DF
410 case CEPH_OSD_OP_LIST_WATCHERS:
411 ceph_osd_data_release(&op->list_watchers.response_data);
412 break;
5476492f
AE
413 default:
414 break;
415 }
c54d47bf
AE
416}
417
63244fa1
ID
418/*
419 * Assumes @t is zero-initialized.
420 */
421static void target_init(struct ceph_osd_request_target *t)
422{
423 ceph_oid_init(&t->base_oid);
424 ceph_oloc_init(&t->base_oloc);
425 ceph_oid_init(&t->target_oid);
426 ceph_oloc_init(&t->target_oloc);
427
428 ceph_osds_init(&t->acting);
429 ceph_osds_init(&t->up);
430 t->size = -1;
431 t->min_size = -1;
432
433 t->osd = CEPH_HOMELESS_OSD;
434}
435
922dab61
ID
436static void target_copy(struct ceph_osd_request_target *dest,
437 const struct ceph_osd_request_target *src)
438{
439 ceph_oid_copy(&dest->base_oid, &src->base_oid);
440 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
441 ceph_oid_copy(&dest->target_oid, &src->target_oid);
442 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
443
444 dest->pgid = src->pgid; /* struct */
dc98ff72 445 dest->spgid = src->spgid; /* struct */
922dab61
ID
446 dest->pg_num = src->pg_num;
447 dest->pg_num_mask = src->pg_num_mask;
448 ceph_osds_copy(&dest->acting, &src->acting);
449 ceph_osds_copy(&dest->up, &src->up);
450 dest->size = src->size;
451 dest->min_size = src->min_size;
452 dest->sort_bitwise = src->sort_bitwise;
453
454 dest->flags = src->flags;
455 dest->paused = src->paused;
456
04c7d789 457 dest->epoch = src->epoch;
dc93e0e2
ID
458 dest->last_force_resend = src->last_force_resend;
459
922dab61
ID
460 dest->osd = src->osd;
461}
462
63244fa1
ID
463static void target_destroy(struct ceph_osd_request_target *t)
464{
465 ceph_oid_destroy(&t->base_oid);
30c156d9 466 ceph_oloc_destroy(&t->base_oloc);
63244fa1 467 ceph_oid_destroy(&t->target_oid);
30c156d9 468 ceph_oloc_destroy(&t->target_oloc);
63244fa1
ID
469}
470
f24e9980
SW
471/*
472 * requests
473 */
3540bfdb
ID
474static void request_release_checks(struct ceph_osd_request *req)
475{
476 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4609245e 477 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
3540bfdb
ID
478 WARN_ON(!list_empty(&req->r_unsafe_item));
479 WARN_ON(req->r_osd);
480}
481
9e94af20 482static void ceph_osdc_release_request(struct kref *kref)
f24e9980 483{
9e94af20
ID
484 struct ceph_osd_request *req = container_of(kref,
485 struct ceph_osd_request, r_kref);
5476492f 486 unsigned int which;
415e49a9 487
9e94af20
ID
488 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
489 req->r_request, req->r_reply);
3540bfdb 490 request_release_checks(req);
9e94af20 491
415e49a9
SW
492 if (req->r_request)
493 ceph_msg_put(req->r_request);
5aea3dcd 494 if (req->r_reply)
ab8cb34a 495 ceph_msg_put(req->r_reply);
0fff87ec 496
5476492f
AE
497 for (which = 0; which < req->r_num_ops; which++)
498 osd_req_op_data_release(req, which);
0fff87ec 499
a66dd383 500 target_destroy(&req->r_t);
415e49a9 501 ceph_put_snap_context(req->r_snapc);
d30291b9 502
415e49a9
SW
503 if (req->r_mempool)
504 mempool_free(req, req->r_osdc->req_mempool);
3f1af42a 505 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
5522ae0b 506 kmem_cache_free(ceph_osd_request_cache, req);
3f1af42a
ID
507 else
508 kfree(req);
f24e9980 509}
9e94af20
ID
510
511void ceph_osdc_get_request(struct ceph_osd_request *req)
512{
513 dout("%s %p (was %d)\n", __func__, req,
2c935bc5 514 kref_read(&req->r_kref));
9e94af20
ID
515 kref_get(&req->r_kref);
516}
517EXPORT_SYMBOL(ceph_osdc_get_request);
518
519void ceph_osdc_put_request(struct ceph_osd_request *req)
520{
3ed97d63
ID
521 if (req) {
522 dout("%s %p (was %d)\n", __func__, req,
2c935bc5 523 kref_read(&req->r_kref));
3ed97d63
ID
524 kref_put(&req->r_kref, ceph_osdc_release_request);
525 }
9e94af20
ID
526}
527EXPORT_SYMBOL(ceph_osdc_put_request);
68b4476b 528
3540bfdb
ID
529static void request_init(struct ceph_osd_request *req)
530{
531 /* req only, each op is zeroed in _osd_req_op_init() */
532 memset(req, 0, sizeof(*req));
533
534 kref_init(&req->r_kref);
535 init_completion(&req->r_completion);
3540bfdb 536 RB_CLEAR_NODE(&req->r_node);
4609245e 537 RB_CLEAR_NODE(&req->r_mc_node);
3540bfdb
ID
538 INIT_LIST_HEAD(&req->r_unsafe_item);
539
540 target_init(&req->r_t);
541}
542
922dab61
ID
543/*
544 * This is ugly, but it allows us to reuse linger registration and ping
545 * requests, keeping the structure of the code around send_linger{_ping}()
546 * reasonable. Setting up a min_nr=2 mempool for each linger request
547 * and dealing with copying ops (this blasts req only, watch op remains
548 * intact) isn't any better.
549 */
550static void request_reinit(struct ceph_osd_request *req)
551{
552 struct ceph_osd_client *osdc = req->r_osdc;
553 bool mempool = req->r_mempool;
554 unsigned int num_ops = req->r_num_ops;
555 u64 snapid = req->r_snapid;
556 struct ceph_snap_context *snapc = req->r_snapc;
557 bool linger = req->r_linger;
558 struct ceph_msg *request_msg = req->r_request;
559 struct ceph_msg *reply_msg = req->r_reply;
560
561 dout("%s req %p\n", __func__, req);
2c935bc5 562 WARN_ON(kref_read(&req->r_kref) != 1);
922dab61
ID
563 request_release_checks(req);
564
2c935bc5
PZ
565 WARN_ON(kref_read(&request_msg->kref) != 1);
566 WARN_ON(kref_read(&reply_msg->kref) != 1);
922dab61
ID
567 target_destroy(&req->r_t);
568
569 request_init(req);
570 req->r_osdc = osdc;
571 req->r_mempool = mempool;
572 req->r_num_ops = num_ops;
573 req->r_snapid = snapid;
574 req->r_snapc = snapc;
575 req->r_linger = linger;
576 req->r_request = request_msg;
577 req->r_reply = reply_msg;
578}
579
3499e8a5 580struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
f24e9980 581 struct ceph_snap_context *snapc,
1b83bef2 582 unsigned int num_ops,
3499e8a5 583 bool use_mempool,
54a54007 584 gfp_t gfp_flags)
f24e9980
SW
585{
586 struct ceph_osd_request *req;
1b83bef2 587
f24e9980 588 if (use_mempool) {
3f1af42a 589 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
3499e8a5 590 req = mempool_alloc(osdc->req_mempool, gfp_flags);
3f1af42a
ID
591 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
592 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
f24e9980 593 } else {
3f1af42a 594 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
acafe7e3 595 req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
f24e9980 596 }
3f1af42a 597 if (unlikely(!req))
a79832f2 598 return NULL;
f24e9980 599
3540bfdb 600 request_init(req);
f24e9980
SW
601 req->r_osdc = osdc;
602 req->r_mempool = use_mempool;
79528734 603 req->r_num_ops = num_ops;
84127282
ID
604 req->r_snapid = CEPH_NOSNAP;
605 req->r_snapc = ceph_get_snap_context(snapc);
68b4476b 606
13d1ad16
ID
607 dout("%s req %p\n", __func__, req);
608 return req;
609}
610EXPORT_SYMBOL(ceph_osdc_alloc_request);
3f1af42a 611
2e59ffd1 612static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
30c156d9
YZ
613{
614 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
615}
616
13d1ad16
ID
617int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
618{
619 struct ceph_osd_client *osdc = req->r_osdc;
620 struct ceph_msg *msg;
621 int msg_size;
c16e7869 622
d30291b9 623 WARN_ON(ceph_oid_empty(&req->r_base_oid));
30c156d9 624 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
d30291b9 625
13d1ad16 626 /* create request message */
8cb441c0
ID
627 msg_size = CEPH_ENCODING_START_BLK_LEN +
628 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
629 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
630 msg_size += CEPH_ENCODING_START_BLK_LEN +
631 sizeof(struct ceph_osd_reqid); /* reqid */
632 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
633 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
30c156d9
YZ
634 msg_size += CEPH_ENCODING_START_BLK_LEN +
635 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
13d1ad16
ID
636 msg_size += 4 + req->r_base_oid.name_len; /* oid */
637 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
ae458f5a
ID
638 msg_size += 8; /* snapid */
639 msg_size += 8; /* snap_seq */
13d1ad16 640 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
8cb441c0 641 msg_size += 4 + 8; /* retry_attempt, features */
ae458f5a 642
13d1ad16 643 if (req->r_mempool)
3b83f60d 644 msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size);
f24e9980 645 else
13d1ad16
ID
646 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
647 if (!msg)
648 return -ENOMEM;
68b4476b 649
f24e9980 650 memset(msg->front.iov_base, 0, msg->front.iov_len);
3499e8a5 651 req->r_request = msg;
3499e8a5 652
13d1ad16
ID
653 /* create reply message */
654 msg_size = OSD_OPREPLY_FRONT_LEN;
711da55d
ID
655 msg_size += req->r_base_oid.name_len;
656 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
13d1ad16
ID
657
658 if (req->r_mempool)
3b83f60d 659 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size);
13d1ad16
ID
660 else
661 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
662 if (!msg)
663 return -ENOMEM;
664
665 req->r_reply = msg;
666
667 return 0;
3499e8a5 668}
13d1ad16 669EXPORT_SYMBOL(ceph_osdc_alloc_messages);
3499e8a5 670
a8dd0a37 671static bool osd_req_opcode_valid(u16 opcode)
68b4476b 672{
a8dd0a37 673 switch (opcode) {
70b5bfa3
ID
674#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
675__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
676#undef GENERATE_CASE
a8dd0a37
AE
677 default:
678 return false;
679 }
680}
681
33803f33
AE
682/*
683 * This is an osd op init function for opcodes that have no data or
684 * other information associated with them. It also serves as a
685 * common init routine for all the other init functions, below.
686 */
c99d2d4a 687static struct ceph_osd_req_op *
49719778 688_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
144cba14 689 u16 opcode, u32 flags)
33803f33 690{
c99d2d4a
AE
691 struct ceph_osd_req_op *op;
692
693 BUG_ON(which >= osd_req->r_num_ops);
33803f33
AE
694 BUG_ON(!osd_req_opcode_valid(opcode));
695
c99d2d4a 696 op = &osd_req->r_ops[which];
33803f33 697 memset(op, 0, sizeof (*op));
33803f33 698 op->op = opcode;
144cba14 699 op->flags = flags;
c99d2d4a
AE
700
701 return op;
33803f33
AE
702}
703
49719778 704void osd_req_op_init(struct ceph_osd_request *osd_req,
144cba14 705 unsigned int which, u16 opcode, u32 flags)
49719778 706{
144cba14 707 (void)_osd_req_op_init(osd_req, which, opcode, flags);
49719778
AE
708}
709EXPORT_SYMBOL(osd_req_op_init);
710
c99d2d4a
AE
711void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
712 unsigned int which, u16 opcode,
33803f33
AE
713 u64 offset, u64 length,
714 u64 truncate_size, u32 truncate_seq)
715{
144cba14
YZ
716 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
717 opcode, 0);
33803f33
AE
718 size_t payload_len = 0;
719
ad7a60de 720 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
e30b7577
ID
721 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
722 opcode != CEPH_OSD_OP_TRUNCATE);
33803f33 723
33803f33
AE
724 op->extent.offset = offset;
725 op->extent.length = length;
726 op->extent.truncate_size = truncate_size;
727 op->extent.truncate_seq = truncate_seq;
e30b7577 728 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
33803f33
AE
729 payload_len += length;
730
de2aa102 731 op->indata_len = payload_len;
33803f33
AE
732}
733EXPORT_SYMBOL(osd_req_op_extent_init);
734
c99d2d4a
AE
735void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
736 unsigned int which, u64 length)
e5975c7c 737{
c99d2d4a
AE
738 struct ceph_osd_req_op *op;
739 u64 previous;
740
741 BUG_ON(which >= osd_req->r_num_ops);
742 op = &osd_req->r_ops[which];
743 previous = op->extent.length;
e5975c7c
AE
744
745 if (length == previous)
746 return; /* Nothing to do */
747 BUG_ON(length > previous);
748
749 op->extent.length = length;
d641df81
YZ
750 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
751 op->indata_len -= previous - length;
e5975c7c
AE
752}
753EXPORT_SYMBOL(osd_req_op_extent_update);
754
2c63f49a
YZ
755void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
756 unsigned int which, u64 offset_inc)
757{
758 struct ceph_osd_req_op *op, *prev_op;
759
760 BUG_ON(which + 1 >= osd_req->r_num_ops);
761
762 prev_op = &osd_req->r_ops[which];
763 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
764 /* dup previous one */
765 op->indata_len = prev_op->indata_len;
766 op->outdata_len = prev_op->outdata_len;
767 op->extent = prev_op->extent;
768 /* adjust offset */
769 op->extent.offset += offset_inc;
770 op->extent.length -= offset_inc;
771
772 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
773 op->indata_len -= offset_inc;
774}
775EXPORT_SYMBOL(osd_req_op_extent_dup_last);
776
fe943d50 777int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
24639ce5 778 const char *class, const char *method)
33803f33 779{
24639ce5 780 struct ceph_osd_req_op *op;
5f562df5 781 struct ceph_pagelist *pagelist;
33803f33
AE
782 size_t payload_len = 0;
783 size_t size;
784
24639ce5 785 op = _osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
33803f33 786
33165d47 787 pagelist = ceph_pagelist_alloc(GFP_NOFS);
fe943d50
CX
788 if (!pagelist)
789 return -ENOMEM;
790
33803f33
AE
791 op->cls.class_name = class;
792 size = strlen(class);
793 BUG_ON(size > (size_t) U8_MAX);
794 op->cls.class_len = size;
5f562df5 795 ceph_pagelist_append(pagelist, class, size);
33803f33
AE
796 payload_len += size;
797
798 op->cls.method_name = method;
799 size = strlen(method);
800 BUG_ON(size > (size_t) U8_MAX);
801 op->cls.method_len = size;
5f562df5 802 ceph_pagelist_append(pagelist, method, size);
33803f33
AE
803 payload_len += size;
804
a4ce40a9 805 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
5f562df5 806
de2aa102 807 op->indata_len = payload_len;
fe943d50 808 return 0;
33803f33
AE
809}
810EXPORT_SYMBOL(osd_req_op_cls_init);
8c042b0d 811
d74b50be
YZ
812int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
813 u16 opcode, const char *name, const void *value,
814 size_t size, u8 cmp_op, u8 cmp_mode)
815{
144cba14
YZ
816 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
817 opcode, 0);
d74b50be
YZ
818 struct ceph_pagelist *pagelist;
819 size_t payload_len;
820
821 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
822
33165d47 823 pagelist = ceph_pagelist_alloc(GFP_NOFS);
d74b50be
YZ
824 if (!pagelist)
825 return -ENOMEM;
826
d74b50be
YZ
827 payload_len = strlen(name);
828 op->xattr.name_len = payload_len;
829 ceph_pagelist_append(pagelist, name, payload_len);
830
831 op->xattr.value_len = size;
832 ceph_pagelist_append(pagelist, value, size);
833 payload_len += size;
834
835 op->xattr.cmp_op = cmp_op;
836 op->xattr.cmp_mode = cmp_mode;
837
838 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
de2aa102 839 op->indata_len = payload_len;
d74b50be
YZ
840 return 0;
841}
842EXPORT_SYMBOL(osd_req_op_xattr_init);
843
922dab61
ID
844/*
845 * @watch_opcode: CEPH_OSD_WATCH_OP_*
846 */
847static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
848 u64 cookie, u8 watch_opcode)
33803f33 849{
922dab61 850 struct ceph_osd_req_op *op;
33803f33 851
922dab61 852 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
33803f33 853 op->watch.cookie = cookie;
922dab61
ID
854 op->watch.op = watch_opcode;
855 op->watch.gen = 0;
33803f33 856}
33803f33 857
c647b8a8
ID
858void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
859 unsigned int which,
860 u64 expected_object_size,
861 u64 expected_write_size)
862{
863 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
144cba14
YZ
864 CEPH_OSD_OP_SETALLOCHINT,
865 0);
c647b8a8
ID
866
867 op->alloc_hint.expected_object_size = expected_object_size;
868 op->alloc_hint.expected_write_size = expected_write_size;
869
870 /*
871 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
872 * not worth a feature bit. Set FAILOK per-op flag to make
873 * sure older osds don't trip over an unsupported opcode.
874 */
875 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
876}
877EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
878
90af3602 879static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
ec9123c5
AE
880 struct ceph_osd_data *osd_data)
881{
882 u64 length = ceph_osd_data_length(osd_data);
883
884 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
885 BUG_ON(length > (u64) SIZE_MAX);
886 if (length)
90af3602 887 ceph_msg_data_add_pages(msg, osd_data->pages,
ec9123c5
AE
888 length, osd_data->alignment);
889 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
890 BUG_ON(!length);
90af3602 891 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
ec9123c5
AE
892#ifdef CONFIG_BLOCK
893 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
5359a17d 894 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
ec9123c5 895#endif
b9e281c2
ID
896 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
897 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
ec9123c5
AE
898 } else {
899 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
900 }
901}
902
bb873b53
ID
903static u32 osd_req_encode_op(struct ceph_osd_op *dst,
904 const struct ceph_osd_req_op *src)
a8dd0a37 905{
a8dd0a37
AE
906 switch (src->op) {
907 case CEPH_OSD_OP_STAT:
908 break;
909 case CEPH_OSD_OP_READ:
910 case CEPH_OSD_OP_WRITE:
e30b7577 911 case CEPH_OSD_OP_WRITEFULL:
ad7a60de 912 case CEPH_OSD_OP_ZERO:
ad7a60de 913 case CEPH_OSD_OP_TRUNCATE:
a8dd0a37
AE
914 dst->extent.offset = cpu_to_le64(src->extent.offset);
915 dst->extent.length = cpu_to_le64(src->extent.length);
916 dst->extent.truncate_size =
917 cpu_to_le64(src->extent.truncate_size);
918 dst->extent.truncate_seq =
919 cpu_to_le32(src->extent.truncate_seq);
920 break;
921 case CEPH_OSD_OP_CALL:
a8dd0a37
AE
922 dst->cls.class_len = src->cls.class_len;
923 dst->cls.method_len = src->cls.method_len;
bb873b53 924 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
a8dd0a37 925 break;
a8dd0a37
AE
926 case CEPH_OSD_OP_WATCH:
927 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
922dab61
ID
928 dst->watch.ver = cpu_to_le64(0);
929 dst->watch.op = src->watch.op;
930 dst->watch.gen = cpu_to_le32(src->watch.gen);
931 break;
932 case CEPH_OSD_OP_NOTIFY_ACK:
a8dd0a37 933 break;
19079203
ID
934 case CEPH_OSD_OP_NOTIFY:
935 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
936 break;
a4ed38d7
DF
937 case CEPH_OSD_OP_LIST_WATCHERS:
938 break;
c647b8a8
ID
939 case CEPH_OSD_OP_SETALLOCHINT:
940 dst->alloc_hint.expected_object_size =
941 cpu_to_le64(src->alloc_hint.expected_object_size);
942 dst->alloc_hint.expected_write_size =
943 cpu_to_le64(src->alloc_hint.expected_write_size);
944 break;
d74b50be
YZ
945 case CEPH_OSD_OP_SETXATTR:
946 case CEPH_OSD_OP_CMPXATTR:
947 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
948 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
949 dst->xattr.cmp_op = src->xattr.cmp_op;
950 dst->xattr.cmp_mode = src->xattr.cmp_mode;
d74b50be 951 break;
864e9197
YZ
952 case CEPH_OSD_OP_CREATE:
953 case CEPH_OSD_OP_DELETE:
954 break;
a8dd0a37 955 default:
4c46459c 956 pr_err("unsupported osd opcode %s\n",
8f63ca2d 957 ceph_osd_op_name(src->op));
4c46459c 958 WARN_ON(1);
a8dd0a37
AE
959
960 return 0;
68b4476b 961 }
7b25bf5f 962
a8dd0a37 963 dst->op = cpu_to_le16(src->op);
7b25bf5f 964 dst->flags = cpu_to_le32(src->flags);
de2aa102 965 dst->payload_len = cpu_to_le32(src->indata_len);
175face2 966
bb873b53 967 return src->indata_len;
68b4476b
YS
968}
969
3499e8a5
YS
970/*
971 * build new request AND message, calculate layout, and adjust file
972 * extent as needed.
973 *
974 * if the file was recently truncated, we include information about its
975 * old and new size so that the object can be updated appropriately. (we
976 * avoid synchronously deleting truncated objects because it's slow.)
3499e8a5
YS
977 */
978struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
979 struct ceph_file_layout *layout,
980 struct ceph_vino vino,
715e4cd4
YZ
981 u64 off, u64 *plen,
982 unsigned int which, int num_ops,
3499e8a5
YS
983 int opcode, int flags,
984 struct ceph_snap_context *snapc,
3499e8a5
YS
985 u32 truncate_seq,
986 u64 truncate_size,
153e5167 987 bool use_mempool)
3499e8a5 988{
68b4476b 989 struct ceph_osd_request *req;
75d1c941
AE
990 u64 objnum = 0;
991 u64 objoff = 0;
992 u64 objlen = 0;
6816282d 993 int r;
68b4476b 994
ad7a60de 995 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
864e9197
YZ
996 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
997 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
68b4476b 998
acead002 999 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
ae7ca4a3 1000 GFP_NOFS);
13d1ad16
ID
1001 if (!req) {
1002 r = -ENOMEM;
1003 goto fail;
1004 }
79528734 1005
3499e8a5 1006 /* calculate max write size */
a19dadfb 1007 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
13d1ad16
ID
1008 if (r)
1009 goto fail;
a19dadfb 1010
864e9197 1011 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
144cba14 1012 osd_req_op_init(req, which, opcode, 0);
864e9197 1013 } else {
7627151e 1014 u32 object_size = layout->object_size;
864e9197
YZ
1015 u32 object_base = off - objoff;
1016 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1017 if (truncate_size <= object_base) {
1018 truncate_size = 0;
1019 } else {
1020 truncate_size -= object_base;
1021 if (truncate_size > object_size)
1022 truncate_size = object_size;
1023 }
ccca4e37 1024 }
715e4cd4 1025 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
864e9197
YZ
1026 truncate_size, truncate_seq);
1027 }
d18d1e28 1028
bb873b53 1029 req->r_flags = flags;
7627151e 1030 req->r_base_oloc.pool = layout->pool_id;
30c156d9 1031 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
d30291b9 1032 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
dbe0fc41 1033
bb873b53
ID
1034 req->r_snapid = vino.snap;
1035 if (flags & CEPH_OSD_FLAG_WRITE)
1036 req->r_data_offset = off;
1037
13d1ad16
ID
1038 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1039 if (r)
1040 goto fail;
1041
f24e9980 1042 return req;
13d1ad16
ID
1043
1044fail:
1045 ceph_osdc_put_request(req);
1046 return ERR_PTR(r);
f24e9980 1047}
3d14c5d2 1048EXPORT_SYMBOL(ceph_osdc_new_request);
f24e9980
SW
1049
1050/*
1051 * We keep osd requests in an rbtree, sorted by ->r_tid.
1052 */
fcd00b68 1053DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
4609245e 1054DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
f24e9980 1055
66850df5
ID
1056/*
1057 * Call @fn on each OSD request as long as @fn returns 0.
1058 */
1059static void for_each_request(struct ceph_osd_client *osdc,
1060 int (*fn)(struct ceph_osd_request *req, void *arg),
1061 void *arg)
1062{
1063 struct rb_node *n, *p;
1064
1065 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1066 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1067
1068 for (p = rb_first(&osd->o_requests); p; ) {
1069 struct ceph_osd_request *req =
1070 rb_entry(p, struct ceph_osd_request, r_node);
1071
1072 p = rb_next(p);
1073 if (fn(req, arg))
1074 return;
1075 }
1076 }
1077
1078 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1079 struct ceph_osd_request *req =
1080 rb_entry(p, struct ceph_osd_request, r_node);
1081
1082 p = rb_next(p);
1083 if (fn(req, arg))
1084 return;
1085 }
1086}
1087
0247a0cf
ID
1088static bool osd_homeless(struct ceph_osd *osd)
1089{
1090 return osd->o_osd == CEPH_HOMELESS_OSD;
1091}
1092
5aea3dcd 1093static bool osd_registered(struct ceph_osd *osd)
f24e9980 1094{
5aea3dcd 1095 verify_osdc_locked(osd->o_osdc);
f24e9980 1096
5aea3dcd 1097 return !RB_EMPTY_NODE(&osd->o_node);
f24e9980
SW
1098}
1099
0247a0cf
ID
1100/*
1101 * Assumes @osd is zero-initialized.
1102 */
1103static void osd_init(struct ceph_osd *osd)
1104{
02113a0f 1105 refcount_set(&osd->o_ref, 1);
0247a0cf 1106 RB_CLEAR_NODE(&osd->o_node);
5aea3dcd 1107 osd->o_requests = RB_ROOT;
922dab61 1108 osd->o_linger_requests = RB_ROOT;
a02a946d
ID
1109 osd->o_backoff_mappings = RB_ROOT;
1110 osd->o_backoffs_by_id = RB_ROOT;
0247a0cf
ID
1111 INIT_LIST_HEAD(&osd->o_osd_lru);
1112 INIT_LIST_HEAD(&osd->o_keepalive_item);
1113 osd->o_incarnation = 1;
5aea3dcd 1114 mutex_init(&osd->lock);
0247a0cf
ID
1115}
1116
1117static void osd_cleanup(struct ceph_osd *osd)
1118{
1119 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
5aea3dcd 1120 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
922dab61 1121 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
a02a946d
ID
1122 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1123 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
0247a0cf
ID
1124 WARN_ON(!list_empty(&osd->o_osd_lru));
1125 WARN_ON(!list_empty(&osd->o_keepalive_item));
1126
1127 if (osd->o_auth.authorizer) {
1128 WARN_ON(osd_homeless(osd));
1129 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1130 }
1131}
1132
f24e9980
SW
1133/*
1134 * Track open sessions with osds.
1135 */
e10006f8 1136static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
f24e9980
SW
1137{
1138 struct ceph_osd *osd;
1139
0247a0cf
ID
1140 WARN_ON(onum == CEPH_HOMELESS_OSD);
1141
7a28f59b 1142 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
0247a0cf 1143 osd_init(osd);
f24e9980 1144 osd->o_osdc = osdc;
e10006f8 1145 osd->o_osd = onum;
f24e9980 1146
b7a9e5dd 1147 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
4e7a5dcd 1148
f24e9980
SW
1149 return osd;
1150}
1151
1152static struct ceph_osd *get_osd(struct ceph_osd *osd)
1153{
02113a0f
ER
1154 if (refcount_inc_not_zero(&osd->o_ref)) {
1155 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1156 refcount_read(&osd->o_ref));
f24e9980
SW
1157 return osd;
1158 } else {
1159 dout("get_osd %p FAIL\n", osd);
1160 return NULL;
1161 }
1162}
1163
1164static void put_osd(struct ceph_osd *osd)
1165{
02113a0f
ER
1166 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1167 refcount_read(&osd->o_ref) - 1);
1168 if (refcount_dec_and_test(&osd->o_ref)) {
0247a0cf 1169 osd_cleanup(osd);
f24e9980 1170 kfree(osd);
79494d1b 1171 }
f24e9980
SW
1172}
1173
fcd00b68
ID
1174DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1175
9dd2845c 1176static void __move_osd_to_lru(struct ceph_osd *osd)
f5a2041b 1177{
9dd2845c
ID
1178 struct ceph_osd_client *osdc = osd->o_osdc;
1179
1180 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
f5a2041b 1181 BUG_ON(!list_empty(&osd->o_osd_lru));
bbf37ec3 1182
9dd2845c 1183 spin_lock(&osdc->osd_lru_lock);
f5a2041b 1184 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
9dd2845c
ID
1185 spin_unlock(&osdc->osd_lru_lock);
1186
a319bf56 1187 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
f5a2041b
YS
1188}
1189
9dd2845c 1190static void maybe_move_osd_to_lru(struct ceph_osd *osd)
bbf37ec3 1191{
5aea3dcd 1192 if (RB_EMPTY_ROOT(&osd->o_requests) &&
922dab61 1193 RB_EMPTY_ROOT(&osd->o_linger_requests))
9dd2845c 1194 __move_osd_to_lru(osd);
bbf37ec3
ID
1195}
1196
f5a2041b
YS
1197static void __remove_osd_from_lru(struct ceph_osd *osd)
1198{
9dd2845c
ID
1199 struct ceph_osd_client *osdc = osd->o_osdc;
1200
1201 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1202
1203 spin_lock(&osdc->osd_lru_lock);
f5a2041b
YS
1204 if (!list_empty(&osd->o_osd_lru))
1205 list_del_init(&osd->o_osd_lru);
9dd2845c 1206 spin_unlock(&osdc->osd_lru_lock);
f5a2041b
YS
1207}
1208
5aea3dcd
ID
1209/*
1210 * Close the connection and assign any leftover requests to the
1211 * homeless session.
1212 */
1213static void close_osd(struct ceph_osd *osd)
1214{
1215 struct ceph_osd_client *osdc = osd->o_osdc;
1216 struct rb_node *n;
1217
1218 verify_osdc_wrlocked(osdc);
1219 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1220
1221 ceph_con_close(&osd->o_con);
1222
1223 for (n = rb_first(&osd->o_requests); n; ) {
1224 struct ceph_osd_request *req =
1225 rb_entry(n, struct ceph_osd_request, r_node);
1226
1227 n = rb_next(n); /* unlink_request() */
1228
1229 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1230 unlink_request(osd, req);
1231 link_request(&osdc->homeless_osd, req);
1232 }
922dab61
ID
1233 for (n = rb_first(&osd->o_linger_requests); n; ) {
1234 struct ceph_osd_linger_request *lreq =
1235 rb_entry(n, struct ceph_osd_linger_request, node);
1236
1237 n = rb_next(n); /* unlink_linger() */
1238
1239 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1240 lreq->linger_id);
1241 unlink_linger(osd, lreq);
1242 link_linger(&osdc->homeless_osd, lreq);
1243 }
a02a946d 1244 clear_backoffs(osd);
5aea3dcd
ID
1245
1246 __remove_osd_from_lru(osd);
1247 erase_osd(&osdc->osds, osd);
1248 put_osd(osd);
1249}
1250
f24e9980
SW
1251/*
1252 * reset osd connect
1253 */
5aea3dcd 1254static int reopen_osd(struct ceph_osd *osd)
f24e9980 1255{
c3acb181 1256 struct ceph_entity_addr *peer_addr;
f24e9980 1257
5aea3dcd
ID
1258 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1259
1260 if (RB_EMPTY_ROOT(&osd->o_requests) &&
922dab61 1261 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
5aea3dcd 1262 close_osd(osd);
c3acb181
AE
1263 return -ENODEV;
1264 }
1265
5aea3dcd 1266 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
c3acb181
AE
1267 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1268 !ceph_con_opened(&osd->o_con)) {
5aea3dcd 1269 struct rb_node *n;
c3acb181 1270
0b4af2e8
ID
1271 dout("osd addr hasn't changed and connection never opened, "
1272 "letting msgr retry\n");
87b315a5 1273 /* touch each r_stamp for handle_timeout()'s benfit */
5aea3dcd
ID
1274 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1275 struct ceph_osd_request *req =
1276 rb_entry(n, struct ceph_osd_request, r_node);
87b315a5 1277 req->r_stamp = jiffies;
5aea3dcd 1278 }
c3acb181
AE
1279
1280 return -EAGAIN;
f24e9980 1281 }
c3acb181
AE
1282
1283 ceph_con_close(&osd->o_con);
1284 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1285 osd->o_incarnation++;
1286
1287 return 0;
f24e9980
SW
1288}
1289
5aea3dcd
ID
1290static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1291 bool wrlocked)
f24e9980 1292{
5aea3dcd 1293 struct ceph_osd *osd;
35f9f8a0 1294
5aea3dcd
ID
1295 if (wrlocked)
1296 verify_osdc_wrlocked(osdc);
1297 else
1298 verify_osdc_locked(osdc);
f24e9980 1299
5aea3dcd
ID
1300 if (o != CEPH_HOMELESS_OSD)
1301 osd = lookup_osd(&osdc->osds, o);
1302 else
1303 osd = &osdc->homeless_osd;
1304 if (!osd) {
1305 if (!wrlocked)
1306 return ERR_PTR(-EAGAIN);
0ba6478d 1307
5aea3dcd
ID
1308 osd = create_osd(osdc, o);
1309 insert_osd(&osdc->osds, osd);
1310 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1311 &osdc->osdmap->osd_addr[osd->o_osd]);
0ba6478d 1312 }
f24e9980 1313
5aea3dcd
ID
1314 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1315 return osd;
f24e9980
SW
1316}
1317
1318/*
5aea3dcd
ID
1319 * Create request <-> OSD session relation.
1320 *
1321 * @req has to be assigned a tid, @osd may be homeless.
f24e9980 1322 */
5aea3dcd 1323static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
f24e9980 1324{
5aea3dcd
ID
1325 verify_osd_locked(osd);
1326 WARN_ON(!req->r_tid || req->r_osd);
1327 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1328 req, req->r_tid);
1329
1330 if (!osd_homeless(osd))
1331 __remove_osd_from_lru(osd);
1332 else
1333 atomic_inc(&osd->o_osdc->num_homeless);
1334
1335 get_osd(osd);
1336 insert_request(&osd->o_requests, req);
1337 req->r_osd = osd;
f24e9980
SW
1338}
1339
5aea3dcd
ID
1340static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1341{
1342 verify_osd_locked(osd);
1343 WARN_ON(req->r_osd != osd);
1344 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1345 req, req->r_tid);
1346
1347 req->r_osd = NULL;
1348 erase_request(&osd->o_requests, req);
1349 put_osd(osd);
1350
1351 if (!osd_homeless(osd))
1352 maybe_move_osd_to_lru(osd);
1353 else
1354 atomic_dec(&osd->o_osdc->num_homeless);
1355}
1356
63244fa1
ID
1357static bool __pool_full(struct ceph_pg_pool_info *pi)
1358{
1359 return pi->flags & CEPH_POOL_FLAG_FULL;
1360}
1361
42c1b124
ID
1362static bool have_pool_full(struct ceph_osd_client *osdc)
1363{
1364 struct rb_node *n;
1365
1366 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1367 struct ceph_pg_pool_info *pi =
1368 rb_entry(n, struct ceph_pg_pool_info, node);
1369
1370 if (__pool_full(pi))
1371 return true;
1372 }
1373
1374 return false;
1375}
1376
5aea3dcd
ID
1377static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1378{
1379 struct ceph_pg_pool_info *pi;
1380
1381 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1382 if (!pi)
1383 return false;
1384
1385 return __pool_full(pi);
1386}
1387
d29adb34
JD
1388/*
1389 * Returns whether a request should be blocked from being sent
1390 * based on the current osdmap and osd_client settings.
d29adb34 1391 */
63244fa1
ID
1392static bool target_should_be_paused(struct ceph_osd_client *osdc,
1393 const struct ceph_osd_request_target *t,
1394 struct ceph_pg_pool_info *pi)
1395{
b7ec35b3
ID
1396 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1397 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1398 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
63244fa1
ID
1399 __pool_full(pi);
1400
6d637a54 1401 WARN_ON(pi->id != t->target_oloc.pool);
58eb7932
JL
1402 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1403 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1404 (osdc->osdmap->epoch < osdc->epoch_barrier);
63244fa1
ID
1405}
1406
63244fa1
ID
1407enum calc_target_result {
1408 CALC_TARGET_NO_ACTION = 0,
1409 CALC_TARGET_NEED_RESEND,
1410 CALC_TARGET_POOL_DNE,
1411};
1412
1413static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1414 struct ceph_osd_request_target *t,
7de030d6 1415 struct ceph_connection *con,
63244fa1
ID
1416 bool any_change)
1417{
1418 struct ceph_pg_pool_info *pi;
1419 struct ceph_pg pgid, last_pgid;
1420 struct ceph_osds up, acting;
1421 bool force_resend = false;
84ed45df
ID
1422 bool unpaused = false;
1423 bool legacy_change;
7de030d6 1424 bool split = false;
b7ec35b3 1425 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
ae78dd81
ID
1426 bool recovery_deletes = ceph_osdmap_flag(osdc,
1427 CEPH_OSDMAP_RECOVERY_DELETES);
63244fa1 1428 enum calc_target_result ct_res;
63244fa1 1429
04c7d789 1430 t->epoch = osdc->osdmap->epoch;
63244fa1
ID
1431 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1432 if (!pi) {
1433 t->osd = CEPH_HOMELESS_OSD;
1434 ct_res = CALC_TARGET_POOL_DNE;
1435 goto out;
1436 }
1437
1438 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
dc93e0e2
ID
1439 if (t->last_force_resend < pi->last_force_request_resend) {
1440 t->last_force_resend = pi->last_force_request_resend;
63244fa1 1441 force_resend = true;
dc93e0e2 1442 } else if (t->last_force_resend == 0) {
63244fa1
ID
1443 force_resend = true;
1444 }
1445 }
63244fa1 1446
db098ec4
ID
1447 /* apply tiering */
1448 ceph_oid_copy(&t->target_oid, &t->base_oid);
1449 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1450 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
63244fa1
ID
1451 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1452 t->target_oloc.pool = pi->read_tier;
1453 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1454 t->target_oloc.pool = pi->write_tier;
6d637a54
ID
1455
1456 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1457 if (!pi) {
1458 t->osd = CEPH_HOMELESS_OSD;
1459 ct_res = CALC_TARGET_POOL_DNE;
1460 goto out;
1461 }
63244fa1
ID
1462 }
1463
a86f009f 1464 __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
63244fa1
ID
1465 last_pgid.pool = pgid.pool;
1466 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1467
df28152d 1468 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
63244fa1
ID
1469 if (any_change &&
1470 ceph_is_new_interval(&t->acting,
1471 &acting,
1472 &t->up,
1473 &up,
1474 t->size,
1475 pi->size,
1476 t->min_size,
1477 pi->min_size,
1478 t->pg_num,
1479 pi->pg_num,
1480 t->sort_bitwise,
1481 sort_bitwise,
ae78dd81
ID
1482 t->recovery_deletes,
1483 recovery_deletes,
63244fa1
ID
1484 &last_pgid))
1485 force_resend = true;
1486
1487 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1488 t->paused = false;
84ed45df 1489 unpaused = true;
63244fa1 1490 }
84ed45df
ID
1491 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1492 ceph_osds_changed(&t->acting, &acting, any_change);
7de030d6
ID
1493 if (t->pg_num)
1494 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
63244fa1 1495
7de030d6 1496 if (legacy_change || force_resend || split) {
63244fa1 1497 t->pgid = pgid; /* struct */
df28152d 1498 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
63244fa1
ID
1499 ceph_osds_copy(&t->acting, &acting);
1500 ceph_osds_copy(&t->up, &up);
1501 t->size = pi->size;
1502 t->min_size = pi->min_size;
1503 t->pg_num = pi->pg_num;
1504 t->pg_num_mask = pi->pg_num_mask;
1505 t->sort_bitwise = sort_bitwise;
ae78dd81 1506 t->recovery_deletes = recovery_deletes;
63244fa1
ID
1507
1508 t->osd = acting.primary;
63244fa1
ID
1509 }
1510
7de030d6
ID
1511 if (unpaused || legacy_change || force_resend ||
1512 (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1513 RESEND_ON_SPLIT)))
84ed45df
ID
1514 ct_res = CALC_TARGET_NEED_RESEND;
1515 else
1516 ct_res = CALC_TARGET_NO_ACTION;
1517
63244fa1
ID
1518out:
1519 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1520 return ct_res;
1521}
1522
a02a946d
ID
1523static struct ceph_spg_mapping *alloc_spg_mapping(void)
1524{
1525 struct ceph_spg_mapping *spg;
1526
1527 spg = kmalloc(sizeof(*spg), GFP_NOIO);
1528 if (!spg)
1529 return NULL;
1530
1531 RB_CLEAR_NODE(&spg->node);
1532 spg->backoffs = RB_ROOT;
1533 return spg;
1534}
1535
1536static void free_spg_mapping(struct ceph_spg_mapping *spg)
1537{
1538 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1539 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1540
1541 kfree(spg);
1542}
1543
1544/*
1545 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1546 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1547 * defined only within a specific spgid; it does not pass anything to
1548 * children on split, or to another primary.
1549 */
1550DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1551 RB_BYPTR, const struct ceph_spg *, node)
1552
1553static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1554{
1555 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1556}
1557
1558static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1559 void **pkey, size_t *pkey_len)
1560{
1561 if (hoid->key_len) {
1562 *pkey = hoid->key;
1563 *pkey_len = hoid->key_len;
1564 } else {
1565 *pkey = hoid->oid;
1566 *pkey_len = hoid->oid_len;
1567 }
1568}
1569
1570static int compare_names(const void *name1, size_t name1_len,
1571 const void *name2, size_t name2_len)
1572{
1573 int ret;
1574
1575 ret = memcmp(name1, name2, min(name1_len, name2_len));
1576 if (!ret) {
1577 if (name1_len < name2_len)
1578 ret = -1;
1579 else if (name1_len > name2_len)
1580 ret = 1;
1581 }
1582 return ret;
1583}
1584
1585static int hoid_compare(const struct ceph_hobject_id *lhs,
1586 const struct ceph_hobject_id *rhs)
1587{
1588 void *effective_key1, *effective_key2;
1589 size_t effective_key1_len, effective_key2_len;
1590 int ret;
1591
1592 if (lhs->is_max < rhs->is_max)
1593 return -1;
1594 if (lhs->is_max > rhs->is_max)
1595 return 1;
1596
1597 if (lhs->pool < rhs->pool)
1598 return -1;
1599 if (lhs->pool > rhs->pool)
1600 return 1;
1601
1602 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1603 return -1;
1604 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1605 return 1;
1606
1607 ret = compare_names(lhs->nspace, lhs->nspace_len,
1608 rhs->nspace, rhs->nspace_len);
1609 if (ret)
1610 return ret;
1611
1612 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1613 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1614 ret = compare_names(effective_key1, effective_key1_len,
1615 effective_key2, effective_key2_len);
1616 if (ret)
1617 return ret;
1618
1619 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1620 if (ret)
1621 return ret;
1622
1623 if (lhs->snapid < rhs->snapid)
1624 return -1;
1625 if (lhs->snapid > rhs->snapid)
1626 return 1;
1627
1628 return 0;
1629}
1630
1631/*
1632 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1633 * compat stuff here.
1634 *
1635 * Assumes @hoid is zero-initialized.
1636 */
1637static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1638{
1639 u8 struct_v;
1640 u32 struct_len;
1641 int ret;
1642
1643 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1644 &struct_len);
1645 if (ret)
1646 return ret;
1647
1648 if (struct_v < 4) {
1649 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1650 goto e_inval;
1651 }
1652
1653 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1654 GFP_NOIO);
1655 if (IS_ERR(hoid->key)) {
1656 ret = PTR_ERR(hoid->key);
1657 hoid->key = NULL;
1658 return ret;
1659 }
1660
1661 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1662 GFP_NOIO);
1663 if (IS_ERR(hoid->oid)) {
1664 ret = PTR_ERR(hoid->oid);
1665 hoid->oid = NULL;
1666 return ret;
1667 }
1668
1669 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1670 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1671 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1672
1673 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1674 GFP_NOIO);
1675 if (IS_ERR(hoid->nspace)) {
1676 ret = PTR_ERR(hoid->nspace);
1677 hoid->nspace = NULL;
1678 return ret;
1679 }
1680
1681 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1682
1683 ceph_hoid_build_hash_cache(hoid);
1684 return 0;
1685
1686e_inval:
1687 return -EINVAL;
1688}
1689
1690static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1691{
1692 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1693 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1694}
1695
1696static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1697{
1698 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1699 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1700 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1701 ceph_encode_64(p, hoid->snapid);
1702 ceph_encode_32(p, hoid->hash);
1703 ceph_encode_8(p, hoid->is_max);
1704 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1705 ceph_encode_64(p, hoid->pool);
1706}
1707
1708static void free_hoid(struct ceph_hobject_id *hoid)
1709{
1710 if (hoid) {
1711 kfree(hoid->key);
1712 kfree(hoid->oid);
1713 kfree(hoid->nspace);
1714 kfree(hoid);
1715 }
1716}
1717
1718static struct ceph_osd_backoff *alloc_backoff(void)
1719{
1720 struct ceph_osd_backoff *backoff;
1721
1722 backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1723 if (!backoff)
1724 return NULL;
1725
1726 RB_CLEAR_NODE(&backoff->spg_node);
1727 RB_CLEAR_NODE(&backoff->id_node);
1728 return backoff;
1729}
1730
1731static void free_backoff(struct ceph_osd_backoff *backoff)
1732{
1733 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1734 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1735
1736 free_hoid(backoff->begin);
1737 free_hoid(backoff->end);
1738 kfree(backoff);
1739}
1740
1741/*
1742 * Within a specific spgid, backoffs are managed by ->begin hoid.
1743 */
1744DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1745 RB_BYVAL, spg_node);
1746
1747static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1748 const struct ceph_hobject_id *hoid)
1749{
1750 struct rb_node *n = root->rb_node;
1751
1752 while (n) {
1753 struct ceph_osd_backoff *cur =
1754 rb_entry(n, struct ceph_osd_backoff, spg_node);
1755 int cmp;
1756
1757 cmp = hoid_compare(hoid, cur->begin);
1758 if (cmp < 0) {
1759 n = n->rb_left;
1760 } else if (cmp > 0) {
1761 if (hoid_compare(hoid, cur->end) < 0)
1762 return cur;
1763
1764 n = n->rb_right;
1765 } else {
1766 return cur;
1767 }
1768 }
1769
1770 return NULL;
1771}
1772
1773/*
1774 * Each backoff has a unique id within its OSD session.
1775 */
1776DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1777
1778static void clear_backoffs(struct ceph_osd *osd)
1779{
1780 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1781 struct ceph_spg_mapping *spg =
1782 rb_entry(rb_first(&osd->o_backoff_mappings),
1783 struct ceph_spg_mapping, node);
1784
1785 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1786 struct ceph_osd_backoff *backoff =
1787 rb_entry(rb_first(&spg->backoffs),
1788 struct ceph_osd_backoff, spg_node);
1789
1790 erase_backoff(&spg->backoffs, backoff);
1791 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1792 free_backoff(backoff);
1793 }
1794 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1795 free_spg_mapping(spg);
1796 }
1797}
1798
1799/*
1800 * Set up a temporary, non-owning view into @t.
1801 */
1802static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1803 const struct ceph_osd_request_target *t)
1804{
1805 hoid->key = NULL;
1806 hoid->key_len = 0;
1807 hoid->oid = t->target_oid.name;
1808 hoid->oid_len = t->target_oid.name_len;
1809 hoid->snapid = CEPH_NOSNAP;
1810 hoid->hash = t->pgid.seed;
1811 hoid->is_max = false;
1812 if (t->target_oloc.pool_ns) {
1813 hoid->nspace = t->target_oloc.pool_ns->str;
1814 hoid->nspace_len = t->target_oloc.pool_ns->len;
1815 } else {
1816 hoid->nspace = NULL;
1817 hoid->nspace_len = 0;
1818 }
1819 hoid->pool = t->target_oloc.pool;
1820 ceph_hoid_build_hash_cache(hoid);
1821}
1822
1823static bool should_plug_request(struct ceph_osd_request *req)
1824{
1825 struct ceph_osd *osd = req->r_osd;
1826 struct ceph_spg_mapping *spg;
1827 struct ceph_osd_backoff *backoff;
1828 struct ceph_hobject_id hoid;
1829
1830 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1831 if (!spg)
1832 return false;
1833
1834 hoid_fill_from_target(&hoid, &req->r_t);
1835 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1836 if (!backoff)
1837 return false;
1838
1839 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1840 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1841 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1842 return true;
1843}
1844
bb873b53
ID
1845static void setup_request_data(struct ceph_osd_request *req,
1846 struct ceph_msg *msg)
f24e9980 1847{
bb873b53
ID
1848 u32 data_len = 0;
1849 int i;
1850
1851 if (!list_empty(&msg->data))
1852 return;
f24e9980 1853
bb873b53
ID
1854 WARN_ON(msg->data_length);
1855 for (i = 0; i < req->r_num_ops; i++) {
1856 struct ceph_osd_req_op *op = &req->r_ops[i];
1857
1858 switch (op->op) {
1859 /* request */
1860 case CEPH_OSD_OP_WRITE:
1861 case CEPH_OSD_OP_WRITEFULL:
1862 WARN_ON(op->indata_len != op->extent.length);
1863 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1864 break;
1865 case CEPH_OSD_OP_SETXATTR:
1866 case CEPH_OSD_OP_CMPXATTR:
1867 WARN_ON(op->indata_len != op->xattr.name_len +
1868 op->xattr.value_len);
1869 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1870 break;
922dab61
ID
1871 case CEPH_OSD_OP_NOTIFY_ACK:
1872 ceph_osdc_msg_data_add(msg,
1873 &op->notify_ack.request_data);
1874 break;
bb873b53
ID
1875
1876 /* reply */
1877 case CEPH_OSD_OP_STAT:
1878 ceph_osdc_msg_data_add(req->r_reply,
1879 &op->raw_data_in);
1880 break;
1881 case CEPH_OSD_OP_READ:
1882 ceph_osdc_msg_data_add(req->r_reply,
1883 &op->extent.osd_data);
1884 break;
a4ed38d7
DF
1885 case CEPH_OSD_OP_LIST_WATCHERS:
1886 ceph_osdc_msg_data_add(req->r_reply,
1887 &op->list_watchers.response_data);
1888 break;
bb873b53
ID
1889
1890 /* both */
1891 case CEPH_OSD_OP_CALL:
1892 WARN_ON(op->indata_len != op->cls.class_len +
1893 op->cls.method_len +
1894 op->cls.indata_len);
1895 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1896 /* optional, can be NONE */
1897 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1898 /* optional, can be NONE */
1899 ceph_osdc_msg_data_add(req->r_reply,
1900 &op->cls.response_data);
1901 break;
19079203
ID
1902 case CEPH_OSD_OP_NOTIFY:
1903 ceph_osdc_msg_data_add(msg,
1904 &op->notify.request_data);
1905 ceph_osdc_msg_data_add(req->r_reply,
1906 &op->notify.response_data);
1907 break;
bb873b53
ID
1908 }
1909
1910 data_len += op->indata_len;
1911 }
1b83bef2 1912
bb873b53
ID
1913 WARN_ON(data_len != msg->data_length);
1914}
1915
2e59ffd1
ID
1916static void encode_pgid(void **p, const struct ceph_pg *pgid)
1917{
1918 ceph_encode_8(p, 1);
1919 ceph_encode_64(p, pgid->pool);
1920 ceph_encode_32(p, pgid->seed);
1921 ceph_encode_32(p, -1); /* preferred */
1922}
1923
8cb441c0
ID
1924static void encode_spgid(void **p, const struct ceph_spg *spgid)
1925{
1926 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1927 encode_pgid(p, &spgid->pgid);
1928 ceph_encode_8(p, spgid->shard);
1929}
1930
2e59ffd1
ID
1931static void encode_oloc(void **p, void *end,
1932 const struct ceph_object_locator *oloc)
1933{
1934 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1935 ceph_encode_64(p, oloc->pool);
1936 ceph_encode_32(p, -1); /* preferred */
1937 ceph_encode_32(p, 0); /* key len */
1938 if (oloc->pool_ns)
1939 ceph_encode_string(p, end, oloc->pool_ns->str,
1940 oloc->pool_ns->len);
1941 else
1942 ceph_encode_32(p, 0);
1943}
1944
8cb441c0
ID
1945static void encode_request_partial(struct ceph_osd_request *req,
1946 struct ceph_msg *msg)
bb873b53
ID
1947{
1948 void *p = msg->front.iov_base;
1949 void *const end = p + msg->front_alloc_len;
1950 u32 data_len = 0;
1951 int i;
1952
1953 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1954 /* snapshots aren't writeable */
1955 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1956 } else {
1957 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1958 req->r_data_offset || req->r_snapc);
1959 }
1960
1961 setup_request_data(req, msg);
1962
8cb441c0
ID
1963 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1964 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
bb873b53
ID
1965 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1966 ceph_encode_32(&p, req->r_flags);
8cb441c0
ID
1967
1968 /* reqid */
1969 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1970 memset(p, 0, sizeof(struct ceph_osd_reqid));
1971 p += sizeof(struct ceph_osd_reqid);
1972
1973 /* trace */
1974 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1975 p += sizeof(struct ceph_blkin_trace_info);
1976
1977 ceph_encode_32(&p, 0); /* client_inc, always 0 */
fac02ddf 1978 ceph_encode_timespec64(p, &req->r_mtime);
bb873b53 1979 p += sizeof(struct ceph_timespec);
aa26d662 1980
2e59ffd1 1981 encode_oloc(&p, end, &req->r_t.target_oloc);
2e59ffd1
ID
1982 ceph_encode_string(&p, end, req->r_t.target_oid.name,
1983 req->r_t.target_oid.name_len);
f24e9980 1984
bb873b53
ID
1985 /* ops, can imply data */
1986 ceph_encode_16(&p, req->r_num_ops);
1987 for (i = 0; i < req->r_num_ops; i++) {
1988 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1989 p += sizeof(struct ceph_osd_op);
1990 }
26be8808 1991
bb873b53
ID
1992 ceph_encode_64(&p, req->r_snapid); /* snapid */
1993 if (req->r_snapc) {
1994 ceph_encode_64(&p, req->r_snapc->seq);
1995 ceph_encode_32(&p, req->r_snapc->num_snaps);
1996 for (i = 0; i < req->r_snapc->num_snaps; i++)
1997 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1998 } else {
1999 ceph_encode_64(&p, 0); /* snap_seq */
2000 ceph_encode_32(&p, 0); /* snaps len */
2001 }
2002
2003 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
986e8989 2004 BUG_ON(p > end - 8); /* space for features */
bb873b53 2005
8cb441c0
ID
2006 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2007 /* front_len is finalized in encode_request_finish() */
986e8989
ID
2008 msg->front.iov_len = p - msg->front.iov_base;
2009 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
bb873b53
ID
2010 msg->hdr.data_len = cpu_to_le32(data_len);
2011 /*
2012 * The header "data_off" is a hint to the receiver allowing it
2013 * to align received data into its buffers such that there's no
2014 * need to re-copy it before writing it to disk (direct I/O).
2015 */
2016 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
26be8808 2017
8cb441c0
ID
2018 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2019 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2020}
2021
2022static void encode_request_finish(struct ceph_msg *msg)
2023{
2024 void *p = msg->front.iov_base;
986e8989 2025 void *const partial_end = p + msg->front.iov_len;
8cb441c0
ID
2026 void *const end = p + msg->front_alloc_len;
2027
2028 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2029 /* luminous OSD -- encode features and be done */
986e8989 2030 p = partial_end;
8cb441c0
ID
2031 ceph_encode_64(&p, msg->con->peer_features);
2032 } else {
2033 struct {
2034 char spgid[CEPH_ENCODING_START_BLK_LEN +
2035 CEPH_PGID_ENCODING_LEN + 1];
2036 __le32 hash;
2037 __le32 epoch;
2038 __le32 flags;
2039 char reqid[CEPH_ENCODING_START_BLK_LEN +
2040 sizeof(struct ceph_osd_reqid)];
2041 char trace[sizeof(struct ceph_blkin_trace_info)];
2042 __le32 client_inc;
2043 struct ceph_timespec mtime;
2044 } __packed head;
2045 struct ceph_pg pgid;
2046 void *oloc, *oid, *tail;
2047 int oloc_len, oid_len, tail_len;
2048 int len;
2049
2050 /*
2051 * Pre-luminous OSD -- reencode v8 into v4 using @head
2052 * as a temporary buffer. Encode the raw PG; the rest
2053 * is just a matter of moving oloc, oid and tail blobs
2054 * around.
2055 */
2056 memcpy(&head, p, sizeof(head));
2057 p += sizeof(head);
2058
2059 oloc = p;
2060 p += CEPH_ENCODING_START_BLK_LEN;
2061 pgid.pool = ceph_decode_64(&p);
2062 p += 4 + 4; /* preferred, key len */
2063 len = ceph_decode_32(&p);
2064 p += len; /* nspace */
2065 oloc_len = p - oloc;
2066
2067 oid = p;
2068 len = ceph_decode_32(&p);
2069 p += len;
2070 oid_len = p - oid;
2071
2072 tail = p;
986e8989 2073 tail_len = partial_end - p;
8cb441c0
ID
2074
2075 p = msg->front.iov_base;
2076 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2077 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2078 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2079 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2080
2081 /* reassert_version */
2082 memset(p, 0, sizeof(struct ceph_eversion));
2083 p += sizeof(struct ceph_eversion);
2084
2085 BUG_ON(p >= oloc);
2086 memmove(p, oloc, oloc_len);
2087 p += oloc_len;
2088
2089 pgid.seed = le32_to_cpu(head.hash);
2090 encode_pgid(&p, &pgid); /* raw pg */
2091
2092 BUG_ON(p >= oid);
2093 memmove(p, oid, oid_len);
2094 p += oid_len;
2095
2096 /* tail -- ops, snapid, snapc, retry_attempt */
2097 BUG_ON(p >= tail);
2098 memmove(p, tail, tail_len);
2099 p += tail_len;
2100
2101 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2102 }
2103
2104 BUG_ON(p > end);
2105 msg->front.iov_len = p - msg->front.iov_base;
2106 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2107
2108 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2109 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2110 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2111 le16_to_cpu(msg->hdr.version));
bb873b53
ID
2112}
2113
2114/*
2115 * @req has to be assigned a tid and registered.
2116 */
2117static void send_request(struct ceph_osd_request *req)
2118{
2119 struct ceph_osd *osd = req->r_osd;
2120
5aea3dcd 2121 verify_osd_locked(osd);
bb873b53
ID
2122 WARN_ON(osd->o_osd != req->r_t.osd);
2123
a02a946d
ID
2124 /* backoff? */
2125 if (should_plug_request(req))
2126 return;
2127
5aea3dcd
ID
2128 /*
2129 * We may have a previously queued request message hanging
2130 * around. Cancel it to avoid corrupting the msgr.
2131 */
2132 if (req->r_sent)
2133 ceph_msg_revoke(req->r_request);
2134
bb873b53
ID
2135 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2136 if (req->r_attempts)
2137 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2138 else
2139 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2140
8cb441c0 2141 encode_request_partial(req, req->r_request);
bb873b53 2142
04c7d789 2143 dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
bb873b53 2144 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
dc98ff72 2145 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
04c7d789
ID
2146 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2147 req->r_attempts);
bb873b53
ID
2148
2149 req->r_t.paused = false;
2150 req->r_stamp = jiffies;
2151 req->r_attempts++;
2152
2153 req->r_sent = osd->o_incarnation;
2154 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2155 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
f24e9980
SW
2156}
2157
42c1b124
ID
2158static void maybe_request_map(struct ceph_osd_client *osdc)
2159{
2160 bool continuous = false;
2161
5aea3dcd 2162 verify_osdc_locked(osdc);
42c1b124
ID
2163 WARN_ON(!osdc->osdmap->epoch);
2164
b7ec35b3
ID
2165 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2166 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2167 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
42c1b124
ID
2168 dout("%s osdc %p continuous\n", __func__, osdc);
2169 continuous = true;
2170 } else {
2171 dout("%s osdc %p onetime\n", __func__, osdc);
2172 }
2173
2174 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2175 osdc->osdmap->epoch + 1, continuous))
2176 ceph_monc_renew_subs(&osdc->client->monc);
2177}
2178
a1f4020a 2179static void complete_request(struct ceph_osd_request *req, int err);
4609245e
ID
2180static void send_map_check(struct ceph_osd_request *req);
2181
5aea3dcd 2182static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
0bbfdfe8 2183{
5aea3dcd
ID
2184 struct ceph_osd_client *osdc = req->r_osdc;
2185 struct ceph_osd *osd;
4609245e 2186 enum calc_target_result ct_res;
66850df5 2187 int err = 0;
5aea3dcd
ID
2188 bool need_send = false;
2189 bool promoted = false;
0bbfdfe8 2190
b18b9550 2191 WARN_ON(req->r_tid);
5aea3dcd
ID
2192 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2193
2194again:
7de030d6 2195 ct_res = calc_target(osdc, &req->r_t, NULL, false);
4609245e
ID
2196 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2197 goto promote;
2198
5aea3dcd
ID
2199 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2200 if (IS_ERR(osd)) {
2201 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2202 goto promote;
0bbfdfe8
ID
2203 }
2204
66850df5
ID
2205 if (osdc->abort_err) {
2206 dout("req %p abort_err %d\n", req, osdc->abort_err);
2207 err = osdc->abort_err;
2208 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
58eb7932
JL
2209 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2210 osdc->epoch_barrier);
2211 req->r_t.paused = true;
2212 maybe_request_map(osdc);
2213 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2214 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
5aea3dcd
ID
2215 dout("req %p pausewr\n", req);
2216 req->r_t.paused = true;
2217 maybe_request_map(osdc);
2218 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
b7ec35b3 2219 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
5aea3dcd
ID
2220 dout("req %p pauserd\n", req);
2221 req->r_t.paused = true;
2222 maybe_request_map(osdc);
2223 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2224 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2225 CEPH_OSD_FLAG_FULL_FORCE)) &&
b7ec35b3 2226 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
5aea3dcd
ID
2227 pool_full(osdc, req->r_t.base_oloc.pool))) {
2228 dout("req %p full/pool_full\n", req);
c843d13c 2229 if (osdc->abort_on_full) {
66850df5 2230 err = -ENOSPC;
29e87820
ID
2231 } else {
2232 pr_warn_ratelimited("FULL or reached pool quota\n");
2233 req->r_t.paused = true;
2234 maybe_request_map(osdc);
2235 }
5aea3dcd
ID
2236 } else if (!osd_homeless(osd)) {
2237 need_send = true;
0bbfdfe8 2238 } else {
5aea3dcd 2239 maybe_request_map(osdc);
0bbfdfe8
ID
2240 }
2241
5aea3dcd
ID
2242 mutex_lock(&osd->lock);
2243 /*
2244 * Assign the tid atomically with send_request() to protect
2245 * multiple writes to the same object from racing with each
2246 * other, resulting in out of order ops on the OSDs.
2247 */
2248 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2249 link_request(osd, req);
2250 if (need_send)
2251 send_request(req);
66850df5
ID
2252 else if (err)
2253 complete_request(req, err);
5aea3dcd
ID
2254 mutex_unlock(&osd->lock);
2255
6001567c 2256 if (!err && ct_res == CALC_TARGET_POOL_DNE)
4609245e
ID
2257 send_map_check(req);
2258
5aea3dcd
ID
2259 if (promoted)
2260 downgrade_write(&osdc->lock);
2261 return;
2262
2263promote:
2264 up_read(&osdc->lock);
2265 down_write(&osdc->lock);
2266 wrlocked = true;
2267 promoted = true;
2268 goto again;
2269}
2270
2271static void account_request(struct ceph_osd_request *req)
2272{
54ea0046 2273 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
b18b9550 2274 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
5aea3dcd 2275
b18b9550 2276 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
5aea3dcd 2277 atomic_inc(&req->r_osdc->num_requests);
7cc5e38f
ID
2278
2279 req->r_start_stamp = jiffies;
5aea3dcd
ID
2280}
2281
2282static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2283{
2284 ceph_osdc_get_request(req);
2285 account_request(req);
2286 __submit_request(req, wrlocked);
2287}
2288
45ee2c1d 2289static void finish_request(struct ceph_osd_request *req)
5aea3dcd
ID
2290{
2291 struct ceph_osd_client *osdc = req->r_osdc;
5aea3dcd 2292
04c7d789 2293 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
5aea3dcd
ID
2294 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2295
04c7d789
ID
2296 if (req->r_osd)
2297 unlink_request(req->r_osd, req);
5aea3dcd
ID
2298 atomic_dec(&osdc->num_requests);
2299
2300 /*
2301 * If an OSD has failed or returned and a request has been sent
2302 * twice, it's possible to get a reply and end up here while the
2303 * request message is queued for delivery. We will ignore the
2304 * reply, so not a big deal, but better to try and catch it.
2305 */
2306 ceph_msg_revoke(req->r_request);
2307 ceph_msg_revoke_incoming(req->r_reply);
2308}
2309
fe5da05e
ID
2310static void __complete_request(struct ceph_osd_request *req)
2311{
26df726b
ID
2312 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2313 req->r_tid, req->r_callback, req->r_result);
2314
2315 if (req->r_callback)
fe5da05e 2316 req->r_callback(req);
26df726b
ID
2317 complete_all(&req->r_completion);
2318 ceph_osdc_put_request(req);
fe5da05e
ID
2319}
2320
88bc1922
ID
2321static void complete_request_workfn(struct work_struct *work)
2322{
2323 struct ceph_osd_request *req =
2324 container_of(work, struct ceph_osd_request, r_complete_work);
2325
2326 __complete_request(req);
fe5da05e
ID
2327}
2328
4609245e 2329/*
b18b9550 2330 * This is open-coded in handle_reply().
4609245e
ID
2331 */
2332static void complete_request(struct ceph_osd_request *req, int err)
2333{
2334 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2335
2336 req->r_result = err;
45ee2c1d 2337 finish_request(req);
88bc1922
ID
2338
2339 INIT_WORK(&req->r_complete_work, complete_request_workfn);
2340 queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
4609245e
ID
2341}
2342
2343static void cancel_map_check(struct ceph_osd_request *req)
2344{
2345 struct ceph_osd_client *osdc = req->r_osdc;
2346 struct ceph_osd_request *lookup_req;
2347
2348 verify_osdc_wrlocked(osdc);
2349
2350 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2351 if (!lookup_req)
2352 return;
2353
2354 WARN_ON(lookup_req != req);
2355 erase_request_mc(&osdc->map_checks, req);
2356 ceph_osdc_put_request(req);
2357}
2358
5aea3dcd
ID
2359static void cancel_request(struct ceph_osd_request *req)
2360{
2361 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2362
4609245e 2363 cancel_map_check(req);
45ee2c1d 2364 finish_request(req);
b18b9550 2365 complete_all(&req->r_completion);
c297eb42 2366 ceph_osdc_put_request(req);
5aea3dcd
ID
2367}
2368
7cc5e38f
ID
2369static void abort_request(struct ceph_osd_request *req, int err)
2370{
2371 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2372
2373 cancel_map_check(req);
2374 complete_request(req, err);
2375}
2376
66850df5
ID
2377static int abort_fn(struct ceph_osd_request *req, void *arg)
2378{
2379 int err = *(int *)arg;
2380
2381 abort_request(req, err);
2382 return 0; /* continue iteration */
2383}
2384
2385/*
2386 * Abort all in-flight requests with @err and arrange for all future
2387 * requests to be failed immediately.
2388 */
2389void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2390{
2391 dout("%s osdc %p err %d\n", __func__, osdc, err);
2392 down_write(&osdc->lock);
2393 for_each_request(osdc, abort_fn, &err);
2394 osdc->abort_err = err;
2395 up_write(&osdc->lock);
2396}
2397EXPORT_SYMBOL(ceph_osdc_abort_requests);
2398
58eb7932
JL
2399static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2400{
2401 if (likely(eb > osdc->epoch_barrier)) {
2402 dout("updating epoch_barrier from %u to %u\n",
2403 osdc->epoch_barrier, eb);
2404 osdc->epoch_barrier = eb;
2405 /* Request map if we're not to the barrier yet */
2406 if (eb > osdc->osdmap->epoch)
2407 maybe_request_map(osdc);
2408 }
2409}
2410
2411void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2412{
2413 down_read(&osdc->lock);
2414 if (unlikely(eb > osdc->epoch_barrier)) {
2415 up_read(&osdc->lock);
2416 down_write(&osdc->lock);
2417 update_epoch_barrier(osdc, eb);
2418 up_write(&osdc->lock);
2419 } else {
2420 up_read(&osdc->lock);
2421 }
2422}
2423EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2424
4eea0fef
ID
2425/*
2426 * We can end up releasing caps as a result of abort_request().
2427 * In that case, we probably want to ensure that the cap release message
2428 * has an updated epoch barrier in it, so set the epoch barrier prior to
2429 * aborting the first request.
2430 */
2431static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
2432{
2433 struct ceph_osd_client *osdc = req->r_osdc;
2434 bool *victims = arg;
2435
c843d13c 2436 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
4eea0fef 2437 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
690f951d 2438 pool_full(osdc, req->r_t.base_oloc.pool))) {
4eea0fef
ID
2439 if (!*victims) {
2440 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2441 *victims = true;
2442 }
2443 abort_request(req, -ENOSPC);
2444 }
2445
2446 return 0; /* continue iteration */
2447}
2448
fc36d0a4
JL
2449/*
2450 * Drop all pending requests that are stalled waiting on a full condition to
58eb7932
JL
2451 * clear, and complete them with ENOSPC as the return code. Set the
2452 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2453 * cancelled.
fc36d0a4
JL
2454 */
2455static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2456{
58eb7932 2457 bool victims = false;
fc36d0a4 2458
c843d13c
ID
2459 if (osdc->abort_on_full &&
2460 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
4eea0fef 2461 for_each_request(osdc, abort_on_full_fn, &victims);
fc36d0a4
JL
2462}
2463
4609245e
ID
2464static void check_pool_dne(struct ceph_osd_request *req)
2465{
2466 struct ceph_osd_client *osdc = req->r_osdc;
2467 struct ceph_osdmap *map = osdc->osdmap;
2468
2469 verify_osdc_wrlocked(osdc);
2470 WARN_ON(!map->epoch);
2471
2472 if (req->r_attempts) {
2473 /*
2474 * We sent a request earlier, which means that
2475 * previously the pool existed, and now it does not
2476 * (i.e., it was deleted).
2477 */
2478 req->r_map_dne_bound = map->epoch;
2479 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2480 req->r_tid);
2481 } else {
2482 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2483 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2484 }
2485
2486 if (req->r_map_dne_bound) {
2487 if (map->epoch >= req->r_map_dne_bound) {
2488 /* we had a new enough map */
2489 pr_info_ratelimited("tid %llu pool does not exist\n",
2490 req->r_tid);
2491 complete_request(req, -ENOENT);
2492 }
2493 } else {
2494 send_map_check(req);
2495 }
2496}
2497
2498static void map_check_cb(struct ceph_mon_generic_request *greq)
2499{
2500 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2501 struct ceph_osd_request *req;
2502 u64 tid = greq->private_data;
2503
2504 WARN_ON(greq->result || !greq->u.newest);
2505
2506 down_write(&osdc->lock);
2507 req = lookup_request_mc(&osdc->map_checks, tid);
2508 if (!req) {
2509 dout("%s tid %llu dne\n", __func__, tid);
2510 goto out_unlock;
2511 }
2512
2513 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2514 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2515 if (!req->r_map_dne_bound)
2516 req->r_map_dne_bound = greq->u.newest;
2517 erase_request_mc(&osdc->map_checks, req);
2518 check_pool_dne(req);
2519
2520 ceph_osdc_put_request(req);
2521out_unlock:
2522 up_write(&osdc->lock);
2523}
2524
2525static void send_map_check(struct ceph_osd_request *req)
2526{
2527 struct ceph_osd_client *osdc = req->r_osdc;
2528 struct ceph_osd_request *lookup_req;
2529 int ret;
2530
2531 verify_osdc_wrlocked(osdc);
2532
2533 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2534 if (lookup_req) {
2535 WARN_ON(lookup_req != req);
2536 return;
2537 }
2538
2539 ceph_osdc_get_request(req);
2540 insert_request_mc(&osdc->map_checks, req);
2541 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2542 map_check_cb, req->r_tid);
2543 WARN_ON(ret);
2544}
2545
922dab61
ID
2546/*
2547 * lingering requests, watch/notify v2 infrastructure
2548 */
2549static void linger_release(struct kref *kref)
2550{
2551 struct ceph_osd_linger_request *lreq =
2552 container_of(kref, struct ceph_osd_linger_request, kref);
2553
2554 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2555 lreq->reg_req, lreq->ping_req);
2556 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2557 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
4609245e 2558 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
922dab61 2559 WARN_ON(!list_empty(&lreq->scan_item));
b07d3c4b 2560 WARN_ON(!list_empty(&lreq->pending_lworks));
922dab61
ID
2561 WARN_ON(lreq->osd);
2562
2563 if (lreq->reg_req)
2564 ceph_osdc_put_request(lreq->reg_req);
2565 if (lreq->ping_req)
2566 ceph_osdc_put_request(lreq->ping_req);
2567 target_destroy(&lreq->t);
2568 kfree(lreq);
2569}
2570
2571static void linger_put(struct ceph_osd_linger_request *lreq)
2572{
2573 if (lreq)
2574 kref_put(&lreq->kref, linger_release);
2575}
2576
2577static struct ceph_osd_linger_request *
2578linger_get(struct ceph_osd_linger_request *lreq)
2579{
2580 kref_get(&lreq->kref);
2581 return lreq;
2582}
2583
2584static struct ceph_osd_linger_request *
2585linger_alloc(struct ceph_osd_client *osdc)
2586{
2587 struct ceph_osd_linger_request *lreq;
2588
2589 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2590 if (!lreq)
2591 return NULL;
2592
2593 kref_init(&lreq->kref);
2594 mutex_init(&lreq->lock);
2595 RB_CLEAR_NODE(&lreq->node);
2596 RB_CLEAR_NODE(&lreq->osdc_node);
4609245e 2597 RB_CLEAR_NODE(&lreq->mc_node);
922dab61 2598 INIT_LIST_HEAD(&lreq->scan_item);
b07d3c4b 2599 INIT_LIST_HEAD(&lreq->pending_lworks);
922dab61 2600 init_completion(&lreq->reg_commit_wait);
19079203 2601 init_completion(&lreq->notify_finish_wait);
922dab61
ID
2602
2603 lreq->osdc = osdc;
2604 target_init(&lreq->t);
2605
2606 dout("%s lreq %p\n", __func__, lreq);
2607 return lreq;
2608}
2609
2610DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2611DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
4609245e 2612DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
922dab61
ID
2613
2614/*
2615 * Create linger request <-> OSD session relation.
2616 *
2617 * @lreq has to be registered, @osd may be homeless.
2618 */
2619static void link_linger(struct ceph_osd *osd,
2620 struct ceph_osd_linger_request *lreq)
2621{
2622 verify_osd_locked(osd);
2623 WARN_ON(!lreq->linger_id || lreq->osd);
2624 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2625 osd->o_osd, lreq, lreq->linger_id);
2626
2627 if (!osd_homeless(osd))
2628 __remove_osd_from_lru(osd);
2629 else
2630 atomic_inc(&osd->o_osdc->num_homeless);
2631
2632 get_osd(osd);
2633 insert_linger(&osd->o_linger_requests, lreq);
2634 lreq->osd = osd;
2635}
2636
2637static void unlink_linger(struct ceph_osd *osd,
2638 struct ceph_osd_linger_request *lreq)
2639{
2640 verify_osd_locked(osd);
2641 WARN_ON(lreq->osd != osd);
2642 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2643 osd->o_osd, lreq, lreq->linger_id);
2644
2645 lreq->osd = NULL;
2646 erase_linger(&osd->o_linger_requests, lreq);
2647 put_osd(osd);
2648
2649 if (!osd_homeless(osd))
2650 maybe_move_osd_to_lru(osd);
2651 else
2652 atomic_dec(&osd->o_osdc->num_homeless);
2653}
2654
2655static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2656{
2657 verify_osdc_locked(lreq->osdc);
2658
2659 return !RB_EMPTY_NODE(&lreq->osdc_node);
2660}
2661
2662static bool linger_registered(struct ceph_osd_linger_request *lreq)
2663{
2664 struct ceph_osd_client *osdc = lreq->osdc;
2665 bool registered;
2666
2667 down_read(&osdc->lock);
2668 registered = __linger_registered(lreq);
2669 up_read(&osdc->lock);
2670
2671 return registered;
2672}
2673
2674static void linger_register(struct ceph_osd_linger_request *lreq)
2675{
2676 struct ceph_osd_client *osdc = lreq->osdc;
2677
2678 verify_osdc_wrlocked(osdc);
2679 WARN_ON(lreq->linger_id);
2680
2681 linger_get(lreq);
2682 lreq->linger_id = ++osdc->last_linger_id;
2683 insert_linger_osdc(&osdc->linger_requests, lreq);
2684}
2685
2686static void linger_unregister(struct ceph_osd_linger_request *lreq)
2687{
2688 struct ceph_osd_client *osdc = lreq->osdc;
2689
2690 verify_osdc_wrlocked(osdc);
2691
2692 erase_linger_osdc(&osdc->linger_requests, lreq);
2693 linger_put(lreq);
2694}
2695
2696static void cancel_linger_request(struct ceph_osd_request *req)
2697{
2698 struct ceph_osd_linger_request *lreq = req->r_priv;
2699
2700 WARN_ON(!req->r_linger);
2701 cancel_request(req);
2702 linger_put(lreq);
2703}
2704
2705struct linger_work {
2706 struct work_struct work;
2707 struct ceph_osd_linger_request *lreq;
b07d3c4b
ID
2708 struct list_head pending_item;
2709 unsigned long queued_stamp;
922dab61
ID
2710
2711 union {
2712 struct {
2713 u64 notify_id;
2714 u64 notifier_id;
2715 void *payload; /* points into @msg front */
2716 size_t payload_len;
2717
2718 struct ceph_msg *msg; /* for ceph_msg_put() */
2719 } notify;
2720 struct {
2721 int err;
2722 } error;
2723 };
2724};
2725
2726static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2727 work_func_t workfn)
2728{
2729 struct linger_work *lwork;
2730
2731 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2732 if (!lwork)
2733 return NULL;
2734
2735 INIT_WORK(&lwork->work, workfn);
b07d3c4b 2736 INIT_LIST_HEAD(&lwork->pending_item);
922dab61
ID
2737 lwork->lreq = linger_get(lreq);
2738
2739 return lwork;
2740}
2741
2742static void lwork_free(struct linger_work *lwork)
2743{
2744 struct ceph_osd_linger_request *lreq = lwork->lreq;
2745
b07d3c4b
ID
2746 mutex_lock(&lreq->lock);
2747 list_del(&lwork->pending_item);
2748 mutex_unlock(&lreq->lock);
2749
922dab61
ID
2750 linger_put(lreq);
2751 kfree(lwork);
2752}
2753
2754static void lwork_queue(struct linger_work *lwork)
2755{
2756 struct ceph_osd_linger_request *lreq = lwork->lreq;
2757 struct ceph_osd_client *osdc = lreq->osdc;
2758
2759 verify_lreq_locked(lreq);
b07d3c4b
ID
2760 WARN_ON(!list_empty(&lwork->pending_item));
2761
2762 lwork->queued_stamp = jiffies;
2763 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
922dab61
ID
2764 queue_work(osdc->notify_wq, &lwork->work);
2765}
2766
2767static void do_watch_notify(struct work_struct *w)
2768{
2769 struct linger_work *lwork = container_of(w, struct linger_work, work);
2770 struct ceph_osd_linger_request *lreq = lwork->lreq;
2771
2772 if (!linger_registered(lreq)) {
2773 dout("%s lreq %p not registered\n", __func__, lreq);
2774 goto out;
2775 }
2776
19079203 2777 WARN_ON(!lreq->is_watch);
922dab61
ID
2778 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2779 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2780 lwork->notify.payload_len);
2781 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2782 lwork->notify.notifier_id, lwork->notify.payload,
2783 lwork->notify.payload_len);
2784
2785out:
2786 ceph_msg_put(lwork->notify.msg);
2787 lwork_free(lwork);
2788}
2789
2790static void do_watch_error(struct work_struct *w)
2791{
2792 struct linger_work *lwork = container_of(w, struct linger_work, work);
2793 struct ceph_osd_linger_request *lreq = lwork->lreq;
2794
2795 if (!linger_registered(lreq)) {
2796 dout("%s lreq %p not registered\n", __func__, lreq);
2797 goto out;
2798 }
2799
2800 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2801 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2802
2803out:
2804 lwork_free(lwork);
2805}
2806
2807static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2808{
2809 struct linger_work *lwork;
2810
2811 lwork = lwork_alloc(lreq, do_watch_error);
2812 if (!lwork) {
2813 pr_err("failed to allocate error-lwork\n");
2814 return;
2815 }
2816
2817 lwork->error.err = lreq->last_error;
2818 lwork_queue(lwork);
2819}
2820
2821static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2822 int result)
2823{
2824 if (!completion_done(&lreq->reg_commit_wait)) {
2825 lreq->reg_commit_error = (result <= 0 ? result : 0);
2826 complete_all(&lreq->reg_commit_wait);
2827 }
2828}
2829
2830static void linger_commit_cb(struct ceph_osd_request *req)
2831{
2832 struct ceph_osd_linger_request *lreq = req->r_priv;
2833
2834 mutex_lock(&lreq->lock);
2835 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2836 lreq->linger_id, req->r_result);
922dab61
ID
2837 linger_reg_commit_complete(lreq, req->r_result);
2838 lreq->committed = true;
2839
19079203
ID
2840 if (!lreq->is_watch) {
2841 struct ceph_osd_data *osd_data =
2842 osd_req_op_data(req, 0, notify, response_data);
2843 void *p = page_address(osd_data->pages[0]);
2844
2845 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2846 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2847
2848 /* make note of the notify_id */
2849 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2850 lreq->notify_id = ceph_decode_64(&p);
2851 dout("lreq %p notify_id %llu\n", lreq,
2852 lreq->notify_id);
2853 } else {
2854 dout("lreq %p no notify_id\n", lreq);
2855 }
2856 }
2857
922dab61
ID
2858 mutex_unlock(&lreq->lock);
2859 linger_put(lreq);
2860}
2861
2862static int normalize_watch_error(int err)
2863{
2864 /*
2865 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2866 * notification and a failure to reconnect because we raced with
2867 * the delete appear the same to the user.
2868 */
2869 if (err == -ENOENT)
2870 err = -ENOTCONN;
2871
2872 return err;
2873}
2874
2875static void linger_reconnect_cb(struct ceph_osd_request *req)
2876{
2877 struct ceph_osd_linger_request *lreq = req->r_priv;
2878
2879 mutex_lock(&lreq->lock);
2880 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2881 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2882 if (req->r_result < 0) {
2883 if (!lreq->last_error) {
2884 lreq->last_error = normalize_watch_error(req->r_result);
2885 queue_watch_error(lreq);
2886 }
2887 }
2888
2889 mutex_unlock(&lreq->lock);
2890 linger_put(lreq);
2891}
2892
2893static void send_linger(struct ceph_osd_linger_request *lreq)
2894{
2895 struct ceph_osd_request *req = lreq->reg_req;
2896 struct ceph_osd_req_op *op = &req->r_ops[0];
2897
2898 verify_osdc_wrlocked(req->r_osdc);
2899 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2900
2901 if (req->r_osd)
2902 cancel_linger_request(req);
2903
2904 request_reinit(req);
2905 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2906 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2907 req->r_flags = lreq->t.flags;
2908 req->r_mtime = lreq->mtime;
2909
2910 mutex_lock(&lreq->lock);
19079203 2911 if (lreq->is_watch && lreq->committed) {
922dab61
ID
2912 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2913 op->watch.cookie != lreq->linger_id);
2914 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2915 op->watch.gen = ++lreq->register_gen;
2916 dout("lreq %p reconnect register_gen %u\n", lreq,
2917 op->watch.gen);
2918 req->r_callback = linger_reconnect_cb;
2919 } else {
19079203
ID
2920 if (!lreq->is_watch)
2921 lreq->notify_id = 0;
2922 else
2923 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
922dab61
ID
2924 dout("lreq %p register\n", lreq);
2925 req->r_callback = linger_commit_cb;
2926 }
2927 mutex_unlock(&lreq->lock);
2928
2929 req->r_priv = linger_get(lreq);
2930 req->r_linger = true;
2931
2932 submit_request(req, true);
2933}
2934
2935static void linger_ping_cb(struct ceph_osd_request *req)
2936{
2937 struct ceph_osd_linger_request *lreq = req->r_priv;
2938
2939 mutex_lock(&lreq->lock);
2940 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2941 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2942 lreq->last_error);
2943 if (lreq->register_gen == req->r_ops[0].watch.gen) {
b07d3c4b
ID
2944 if (!req->r_result) {
2945 lreq->watch_valid_thru = lreq->ping_sent;
2946 } else if (!lreq->last_error) {
922dab61
ID
2947 lreq->last_error = normalize_watch_error(req->r_result);
2948 queue_watch_error(lreq);
2949 }
2950 } else {
2951 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2952 lreq->register_gen, req->r_ops[0].watch.gen);
2953 }
2954
2955 mutex_unlock(&lreq->lock);
2956 linger_put(lreq);
2957}
2958
2959static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2960{
2961 struct ceph_osd_client *osdc = lreq->osdc;
2962 struct ceph_osd_request *req = lreq->ping_req;
2963 struct ceph_osd_req_op *op = &req->r_ops[0];
2964
b7ec35b3 2965 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
922dab61
ID
2966 dout("%s PAUSERD\n", __func__);
2967 return;
2968 }
2969
2970 lreq->ping_sent = jiffies;
2971 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2972 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2973 lreq->register_gen);
2974
2975 if (req->r_osd)
2976 cancel_linger_request(req);
2977
2978 request_reinit(req);
2979 target_copy(&req->r_t, &lreq->t);
2980
2981 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2982 op->watch.cookie != lreq->linger_id ||
2983 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2984 op->watch.gen = lreq->register_gen;
2985 req->r_callback = linger_ping_cb;
2986 req->r_priv = linger_get(lreq);
2987 req->r_linger = true;
2988
2989 ceph_osdc_get_request(req);
2990 account_request(req);
2991 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2992 link_request(lreq->osd, req);
2993 send_request(req);
2994}
2995
2996static void linger_submit(struct ceph_osd_linger_request *lreq)
2997{
2998 struct ceph_osd_client *osdc = lreq->osdc;
2999 struct ceph_osd *osd;
3000
7de030d6 3001 calc_target(osdc, &lreq->t, NULL, false);
922dab61
ID
3002 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3003 link_linger(osd, lreq);
3004
3005 send_linger(lreq);
3006}
3007
4609245e
ID
3008static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3009{
3010 struct ceph_osd_client *osdc = lreq->osdc;
3011 struct ceph_osd_linger_request *lookup_lreq;
3012
3013 verify_osdc_wrlocked(osdc);
3014
3015 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3016 lreq->linger_id);
3017 if (!lookup_lreq)
3018 return;
3019
3020 WARN_ON(lookup_lreq != lreq);
3021 erase_linger_mc(&osdc->linger_map_checks, lreq);
3022 linger_put(lreq);
3023}
3024
922dab61
ID
3025/*
3026 * @lreq has to be both registered and linked.
3027 */
3028static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3029{
19079203 3030 if (lreq->is_watch && lreq->ping_req->r_osd)
922dab61
ID
3031 cancel_linger_request(lreq->ping_req);
3032 if (lreq->reg_req->r_osd)
3033 cancel_linger_request(lreq->reg_req);
4609245e 3034 cancel_linger_map_check(lreq);
922dab61
ID
3035 unlink_linger(lreq->osd, lreq);
3036 linger_unregister(lreq);
3037}
3038
3039static void linger_cancel(struct ceph_osd_linger_request *lreq)
3040{
3041 struct ceph_osd_client *osdc = lreq->osdc;
3042
3043 down_write(&osdc->lock);
3044 if (__linger_registered(lreq))
3045 __linger_cancel(lreq);
3046 up_write(&osdc->lock);
3047}
3048
4609245e
ID
3049static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3050
3051static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3052{
3053 struct ceph_osd_client *osdc = lreq->osdc;
3054 struct ceph_osdmap *map = osdc->osdmap;
3055
3056 verify_osdc_wrlocked(osdc);
3057 WARN_ON(!map->epoch);
3058
3059 if (lreq->register_gen) {
3060 lreq->map_dne_bound = map->epoch;
3061 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3062 lreq, lreq->linger_id);
3063 } else {
3064 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3065 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3066 map->epoch);
3067 }
3068
3069 if (lreq->map_dne_bound) {
3070 if (map->epoch >= lreq->map_dne_bound) {
3071 /* we had a new enough map */
3072 pr_info("linger_id %llu pool does not exist\n",
3073 lreq->linger_id);
3074 linger_reg_commit_complete(lreq, -ENOENT);
3075 __linger_cancel(lreq);
3076 }
3077 } else {
3078 send_linger_map_check(lreq);
3079 }
3080}
3081
3082static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3083{
3084 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3085 struct ceph_osd_linger_request *lreq;
3086 u64 linger_id = greq->private_data;
3087
3088 WARN_ON(greq->result || !greq->u.newest);
3089
3090 down_write(&osdc->lock);
3091 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3092 if (!lreq) {
3093 dout("%s linger_id %llu dne\n", __func__, linger_id);
3094 goto out_unlock;
3095 }
3096
3097 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3098 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3099 greq->u.newest);
3100 if (!lreq->map_dne_bound)
3101 lreq->map_dne_bound = greq->u.newest;
3102 erase_linger_mc(&osdc->linger_map_checks, lreq);
3103 check_linger_pool_dne(lreq);
3104
3105 linger_put(lreq);
3106out_unlock:
3107 up_write(&osdc->lock);
3108}
3109
3110static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3111{
3112 struct ceph_osd_client *osdc = lreq->osdc;
3113 struct ceph_osd_linger_request *lookup_lreq;
3114 int ret;
3115
3116 verify_osdc_wrlocked(osdc);
3117
3118 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3119 lreq->linger_id);
3120 if (lookup_lreq) {
3121 WARN_ON(lookup_lreq != lreq);
3122 return;
3123 }
3124
3125 linger_get(lreq);
3126 insert_linger_mc(&osdc->linger_map_checks, lreq);
3127 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3128 linger_map_check_cb, lreq->linger_id);
3129 WARN_ON(ret);
3130}
3131
922dab61
ID
3132static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3133{
3134 int ret;
3135
3136 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3137 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3138 return ret ?: lreq->reg_commit_error;
3139}
3140
19079203
ID
3141static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3142{
3143 int ret;
3144
3145 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3146 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3147 return ret ?: lreq->notify_finish_error;
3148}
3149
f24e9980 3150/*
fbca9635
ID
3151 * Timeout callback, called every N seconds. When 1 or more OSD
3152 * requests has been active for more than N seconds, we send a keepalive
3153 * (tag + timestamp) to its OSD to ensure any communications channel
3154 * reset is detected.
f24e9980
SW
3155 */
3156static void handle_timeout(struct work_struct *work)
3157{
3158 struct ceph_osd_client *osdc =
3159 container_of(work, struct ceph_osd_client, timeout_work.work);
a319bf56 3160 struct ceph_options *opts = osdc->client->options;
5aea3dcd 3161 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
7cc5e38f 3162 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
5aea3dcd
ID
3163 LIST_HEAD(slow_osds);
3164 struct rb_node *n, *p;
f24e9980 3165
5aea3dcd
ID
3166 dout("%s osdc %p\n", __func__, osdc);
3167 down_write(&osdc->lock);
f24e9980 3168
422d2cb8
YS
3169 /*
3170 * ping osds that are a bit slow. this ensures that if there
3171 * is a break in the TCP connection we will notice, and reopen
3172 * a connection with that osd (from the fault callback).
3173 */
5aea3dcd
ID
3174 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3175 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3176 bool found = false;
3177
7cc5e38f 3178 for (p = rb_first(&osd->o_requests); p; ) {
5aea3dcd
ID
3179 struct ceph_osd_request *req =
3180 rb_entry(p, struct ceph_osd_request, r_node);
3181
7cc5e38f
ID
3182 p = rb_next(p); /* abort_request() */
3183
5aea3dcd
ID
3184 if (time_before(req->r_stamp, cutoff)) {
3185 dout(" req %p tid %llu on osd%d is laggy\n",
3186 req, req->r_tid, osd->o_osd);
3187 found = true;
3188 }
7cc5e38f
ID
3189 if (opts->osd_request_timeout &&
3190 time_before(req->r_start_stamp, expiry_cutoff)) {
3191 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3192 req->r_tid, osd->o_osd);
3193 abort_request(req, -ETIMEDOUT);
3194 }
5aea3dcd 3195 }
922dab61
ID
3196 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3197 struct ceph_osd_linger_request *lreq =
3198 rb_entry(p, struct ceph_osd_linger_request, node);
3199
3200 dout(" lreq %p linger_id %llu is served by osd%d\n",
3201 lreq, lreq->linger_id, osd->o_osd);
3202 found = true;
3203
3204 mutex_lock(&lreq->lock);
19079203 3205 if (lreq->is_watch && lreq->committed && !lreq->last_error)
922dab61
ID
3206 send_linger_ping(lreq);
3207 mutex_unlock(&lreq->lock);
3208 }
422d2cb8 3209
5aea3dcd
ID
3210 if (found)
3211 list_move_tail(&osd->o_keepalive_item, &slow_osds);
422d2cb8 3212 }
5aea3dcd 3213
7cc5e38f
ID
3214 if (opts->osd_request_timeout) {
3215 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3216 struct ceph_osd_request *req =
3217 rb_entry(p, struct ceph_osd_request, r_node);
3218
3219 p = rb_next(p); /* abort_request() */
3220
3221 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3222 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3223 req->r_tid, osdc->homeless_osd.o_osd);
3224 abort_request(req, -ETIMEDOUT);
3225 }
3226 }
3227 }
3228
5aea3dcd
ID
3229 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3230 maybe_request_map(osdc);
3231
422d2cb8 3232 while (!list_empty(&slow_osds)) {
5aea3dcd
ID
3233 struct ceph_osd *osd = list_first_entry(&slow_osds,
3234 struct ceph_osd,
3235 o_keepalive_item);
422d2cb8 3236 list_del_init(&osd->o_keepalive_item);
f24e9980
SW
3237 ceph_con_keepalive(&osd->o_con);
3238 }
3239
5aea3dcd 3240 up_write(&osdc->lock);
fbca9635
ID
3241 schedule_delayed_work(&osdc->timeout_work,
3242 osdc->client->options->osd_keepalive_timeout);
f24e9980
SW
3243}
3244
f5a2041b
YS
3245static void handle_osds_timeout(struct work_struct *work)
3246{
3247 struct ceph_osd_client *osdc =
3248 container_of(work, struct ceph_osd_client,
3249 osds_timeout_work.work);
a319bf56 3250 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
42a2c09f 3251 struct ceph_osd *osd, *nosd;
f5a2041b 3252
42a2c09f 3253 dout("%s osdc %p\n", __func__, osdc);
5aea3dcd 3254 down_write(&osdc->lock);
42a2c09f
ID
3255 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3256 if (time_before(jiffies, osd->lru_ttl))
3257 break;
3258
5aea3dcd 3259 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
922dab61 3260 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
5aea3dcd 3261 close_osd(osd);
42a2c09f 3262 }
f5a2041b 3263
5aea3dcd 3264 up_write(&osdc->lock);
f5a2041b
YS
3265 schedule_delayed_work(&osdc->osds_timeout_work,
3266 round_jiffies_relative(delay));
3267}
3268
205ee118
ID
3269static int ceph_oloc_decode(void **p, void *end,
3270 struct ceph_object_locator *oloc)
3271{
3272 u8 struct_v, struct_cv;
3273 u32 len;
3274 void *struct_end;
3275 int ret = 0;
3276
3277 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3278 struct_v = ceph_decode_8(p);
3279 struct_cv = ceph_decode_8(p);
3280 if (struct_v < 3) {
3281 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3282 struct_v, struct_cv);
3283 goto e_inval;
3284 }
3285 if (struct_cv > 6) {
3286 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3287 struct_v, struct_cv);
3288 goto e_inval;
3289 }
3290 len = ceph_decode_32(p);
3291 ceph_decode_need(p, end, len, e_inval);
3292 struct_end = *p + len;
3293
3294 oloc->pool = ceph_decode_64(p);
3295 *p += 4; /* skip preferred */
3296
3297 len = ceph_decode_32(p);
3298 if (len > 0) {
3299 pr_warn("ceph_object_locator::key is set\n");
3300 goto e_inval;
3301 }
3302
3303 if (struct_v >= 5) {
cd08e0a2
YZ
3304 bool changed = false;
3305
205ee118
ID
3306 len = ceph_decode_32(p);
3307 if (len > 0) {
30c156d9 3308 ceph_decode_need(p, end, len, e_inval);
cd08e0a2
YZ
3309 if (!oloc->pool_ns ||
3310 ceph_compare_string(oloc->pool_ns, *p, len))
3311 changed = true;
30c156d9 3312 *p += len;
cd08e0a2
YZ
3313 } else {
3314 if (oloc->pool_ns)
3315 changed = true;
3316 }
3317 if (changed) {
3318 /* redirect changes namespace */
3319 pr_warn("ceph_object_locator::nspace is changed\n");
3320 goto e_inval;
205ee118
ID
3321 }
3322 }
3323
3324 if (struct_v >= 6) {
3325 s64 hash = ceph_decode_64(p);
3326 if (hash != -1) {
3327 pr_warn("ceph_object_locator::hash is set\n");
3328 goto e_inval;
3329 }
3330 }
3331
3332 /* skip the rest */
3333 *p = struct_end;
3334out:
3335 return ret;
3336
3337e_inval:
3338 ret = -EINVAL;
3339 goto out;
3340}
3341
3342static int ceph_redirect_decode(void **p, void *end,
3343 struct ceph_request_redirect *redir)
3344{
3345 u8 struct_v, struct_cv;
3346 u32 len;
3347 void *struct_end;
3348 int ret;
3349
3350 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3351 struct_v = ceph_decode_8(p);
3352 struct_cv = ceph_decode_8(p);
3353 if (struct_cv > 1) {
3354 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3355 struct_v, struct_cv);
3356 goto e_inval;
3357 }
3358 len = ceph_decode_32(p);
3359 ceph_decode_need(p, end, len, e_inval);
3360 struct_end = *p + len;
3361
3362 ret = ceph_oloc_decode(p, end, &redir->oloc);
3363 if (ret)
3364 goto out;
3365
3366 len = ceph_decode_32(p);
3367 if (len > 0) {
3368 pr_warn("ceph_request_redirect::object_name is set\n");
3369 goto e_inval;
3370 }
3371
3372 len = ceph_decode_32(p);
3373 *p += len; /* skip osd_instructions */
3374
3375 /* skip the rest */
3376 *p = struct_end;
3377out:
3378 return ret;
3379
3380e_inval:
3381 ret = -EINVAL;
3382 goto out;
3383}
3384
fe5da05e
ID
3385struct MOSDOpReply {
3386 struct ceph_pg pgid;
3387 u64 flags;
3388 int result;
3389 u32 epoch;
3390 int num_ops;
3391 u32 outdata_len[CEPH_OSD_MAX_OPS];
3392 s32 rval[CEPH_OSD_MAX_OPS];
3393 int retry_attempt;
3394 struct ceph_eversion replay_version;
3395 u64 user_version;
3396 struct ceph_request_redirect redirect;
3397};
25845472 3398
fe5da05e 3399static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
f24e9980 3400{
fe5da05e
ID
3401 void *p = msg->front.iov_base;
3402 void *const end = p + msg->front.iov_len;
3403 u16 version = le16_to_cpu(msg->hdr.version);
3404 struct ceph_eversion bad_replay_version;
b0b31a8f 3405 u8 decode_redir;
fe5da05e
ID
3406 u32 len;
3407 int ret;
3408 int i;
1b83bef2 3409
fe5da05e
ID
3410 ceph_decode_32_safe(&p, end, len, e_inval);
3411 ceph_decode_need(&p, end, len, e_inval);
3412 p += len; /* skip oid */
1b83bef2 3413
fe5da05e
ID
3414 ret = ceph_decode_pgid(&p, end, &m->pgid);
3415 if (ret)
3416 return ret;
1b83bef2 3417
fe5da05e
ID
3418 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3419 ceph_decode_32_safe(&p, end, m->result, e_inval);
3420 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3421 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3422 p += sizeof(bad_replay_version);
3423 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
1b83bef2 3424
fe5da05e
ID
3425 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3426 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3427 goto e_inval;
1b83bef2 3428
fe5da05e
ID
3429 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3430 e_inval);
3431 for (i = 0; i < m->num_ops; i++) {
1b83bef2 3432 struct ceph_osd_op *op = p;
1b83bef2 3433
fe5da05e 3434 m->outdata_len[i] = le32_to_cpu(op->payload_len);
1b83bef2
SW
3435 p += sizeof(*op);
3436 }
1b83bef2 3437
fe5da05e
ID
3438 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3439 for (i = 0; i < m->num_ops; i++)
3440 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
f24e9980 3441
fe5da05e
ID
3442 if (version >= 5) {
3443 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3444 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3445 p += sizeof(m->replay_version);
3446 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3447 } else {
3448 m->replay_version = bad_replay_version; /* struct */
3449 m->user_version = le64_to_cpu(m->replay_version.version);
3450 }
eb845ff1 3451
fe5da05e
ID
3452 if (version >= 6) {
3453 if (version >= 7)
3454 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
b0b31a8f
ID
3455 else
3456 decode_redir = 1;
3457 } else {
3458 decode_redir = 0;
3459 }
3460
3461 if (decode_redir) {
fe5da05e
ID
3462 ret = ceph_redirect_decode(&p, end, &m->redirect);
3463 if (ret)
3464 return ret;
205ee118 3465 } else {
fe5da05e 3466 ceph_oloc_init(&m->redirect.oloc);
205ee118 3467 }
f24e9980 3468
fe5da05e
ID
3469 return 0;
3470
3471e_inval:
3472 return -EINVAL;
3473}
3474
3475/*
b18b9550
ID
3476 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3477 * specified.
fe5da05e 3478 */
5aea3dcd 3479static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
fe5da05e 3480{
5aea3dcd 3481 struct ceph_osd_client *osdc = osd->o_osdc;
fe5da05e
ID
3482 struct ceph_osd_request *req;
3483 struct MOSDOpReply m;
3484 u64 tid = le64_to_cpu(msg->hdr.tid);
3485 u32 data_len = 0;
fe5da05e
ID
3486 int ret;
3487 int i;
3488
3489 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3490
5aea3dcd
ID
3491 down_read(&osdc->lock);
3492 if (!osd_registered(osd)) {
3493 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3494 goto out_unlock_osdc;
3495 }
3496 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3497
3498 mutex_lock(&osd->lock);
3499 req = lookup_request(&osd->o_requests, tid);
fe5da05e 3500 if (!req) {
5aea3dcd
ID
3501 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3502 goto out_unlock_session;
fe5da05e 3503 }
fe5da05e 3504
cd08e0a2 3505 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
fe5da05e 3506 ret = decode_MOSDOpReply(msg, &m);
cd08e0a2 3507 m.redirect.oloc.pool_ns = NULL;
fe5da05e
ID
3508 if (ret) {
3509 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3510 req->r_tid, ret);
3511 ceph_msg_dump(msg);
3512 goto fail_request;
3513 }
3514 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3515 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3516 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3517 le64_to_cpu(m.replay_version.version), m.user_version);
3518
3519 if (m.retry_attempt >= 0) {
3520 if (m.retry_attempt != req->r_attempts - 1) {
3521 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3522 req, req->r_tid, m.retry_attempt,
3523 req->r_attempts - 1);
5aea3dcd 3524 goto out_unlock_session;
fe5da05e
ID
3525 }
3526 } else {
3527 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3528 }
3529
3530 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3531 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3532 m.redirect.oloc.pool);
5aea3dcd
ID
3533 unlink_request(osd, req);
3534 mutex_unlock(&osd->lock);
205ee118 3535
30c156d9
YZ
3536 /*
3537 * Not ceph_oloc_copy() - changing pool_ns is not
3538 * supported.
3539 */
3540 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
5aea3dcd
ID
3541 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3542 req->r_tid = 0;
3543 __submit_request(req, false);
3544 goto out_unlock_osdc;
205ee118
ID
3545 }
3546
fe5da05e
ID
3547 if (m.num_ops != req->r_num_ops) {
3548 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3549 req->r_num_ops, req->r_tid);
3550 goto fail_request;
f24e9980 3551 }
fe5da05e
ID
3552 for (i = 0; i < req->r_num_ops; i++) {
3553 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3554 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3555 req->r_ops[i].rval = m.rval[i];
3556 req->r_ops[i].outdata_len = m.outdata_len[i];
3557 data_len += m.outdata_len[i];
3558 }
3559 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3560 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3561 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3562 goto fail_request;
3563 }
b18b9550
ID
3564 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3565 req, req->r_tid, m.result, data_len);
f24e9980 3566
b18b9550
ID
3567 /*
3568 * Since we only ever request ONDISK, we should only ever get
3569 * one (type of) reply back.
3570 */
3571 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3572 req->r_result = m.result ?: data_len;
3573 finish_request(req);
5aea3dcd
ID
3574 mutex_unlock(&osd->lock);
3575 up_read(&osdc->lock);
f24e9980 3576
b18b9550 3577 __complete_request(req);
f24e9980
SW
3578 return;
3579
fe5da05e 3580fail_request:
4609245e 3581 complete_request(req, -EIO);
5aea3dcd
ID
3582out_unlock_session:
3583 mutex_unlock(&osd->lock);
3584out_unlock_osdc:
3585 up_read(&osdc->lock);
f24e9980
SW
3586}
3587
42c1b124
ID
3588static void set_pool_was_full(struct ceph_osd_client *osdc)
3589{
3590 struct rb_node *n;
3591
3592 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3593 struct ceph_pg_pool_info *pi =
3594 rb_entry(n, struct ceph_pg_pool_info, node);
3595
3596 pi->was_full = __pool_full(pi);
3597 }
3598}
3599
5aea3dcd 3600static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
f24e9980 3601{
5aea3dcd 3602 struct ceph_pg_pool_info *pi;
f24e9980 3603
5aea3dcd
ID
3604 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3605 if (!pi)
3606 return false;
f24e9980 3607
5aea3dcd 3608 return pi->was_full && !__pool_full(pi);
422d2cb8
YS
3609}
3610
922dab61
ID
3611static enum calc_target_result
3612recalc_linger_target(struct ceph_osd_linger_request *lreq)
3613{
3614 struct ceph_osd_client *osdc = lreq->osdc;
3615 enum calc_target_result ct_res;
3616
7de030d6 3617 ct_res = calc_target(osdc, &lreq->t, NULL, true);
922dab61
ID
3618 if (ct_res == CALC_TARGET_NEED_RESEND) {
3619 struct ceph_osd *osd;
3620
3621 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3622 if (osd != lreq->osd) {
3623 unlink_linger(lreq->osd, lreq);
3624 link_linger(osd, lreq);
3625 }
3626 }
3627
3628 return ct_res;
3629}
3630
422d2cb8 3631/*
5aea3dcd 3632 * Requeue requests whose mapping to an OSD has changed.
422d2cb8 3633 */
5aea3dcd
ID
3634static void scan_requests(struct ceph_osd *osd,
3635 bool force_resend,
3636 bool cleared_full,
3637 bool check_pool_cleared_full,
3638 struct rb_root *need_resend,
3639 struct list_head *need_resend_linger)
422d2cb8 3640{
5aea3dcd
ID
3641 struct ceph_osd_client *osdc = osd->o_osdc;
3642 struct rb_node *n;
3643 bool force_resend_writes;
3644
922dab61
ID
3645 for (n = rb_first(&osd->o_linger_requests); n; ) {
3646 struct ceph_osd_linger_request *lreq =
3647 rb_entry(n, struct ceph_osd_linger_request, node);
3648 enum calc_target_result ct_res;
3649
3650 n = rb_next(n); /* recalc_linger_target() */
3651
3652 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3653 lreq->linger_id);
3654 ct_res = recalc_linger_target(lreq);
3655 switch (ct_res) {
3656 case CALC_TARGET_NO_ACTION:
3657 force_resend_writes = cleared_full ||
3658 (check_pool_cleared_full &&
3659 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3660 if (!force_resend && !force_resend_writes)
3661 break;
3662
3663 /* fall through */
3664 case CALC_TARGET_NEED_RESEND:
4609245e 3665 cancel_linger_map_check(lreq);
922dab61
ID
3666 /*
3667 * scan_requests() for the previous epoch(s)
3668 * may have already added it to the list, since
3669 * it's not unlinked here.
3670 */
3671 if (list_empty(&lreq->scan_item))
3672 list_add_tail(&lreq->scan_item, need_resend_linger);
3673 break;
3674 case CALC_TARGET_POOL_DNE:
a10bcb19 3675 list_del_init(&lreq->scan_item);
4609245e 3676 check_linger_pool_dne(lreq);
922dab61
ID
3677 break;
3678 }
3679 }
3680
5aea3dcd
ID
3681 for (n = rb_first(&osd->o_requests); n; ) {
3682 struct ceph_osd_request *req =
3683 rb_entry(n, struct ceph_osd_request, r_node);
3684 enum calc_target_result ct_res;
3685
4609245e 3686 n = rb_next(n); /* unlink_request(), check_pool_dne() */
5aea3dcd
ID
3687
3688 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
7de030d6
ID
3689 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3690 false);
5aea3dcd
ID
3691 switch (ct_res) {
3692 case CALC_TARGET_NO_ACTION:
3693 force_resend_writes = cleared_full ||
3694 (check_pool_cleared_full &&
3695 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3696 if (!force_resend &&
3697 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3698 !force_resend_writes))
3699 break;
3700
3701 /* fall through */
3702 case CALC_TARGET_NEED_RESEND:
4609245e 3703 cancel_map_check(req);
5aea3dcd
ID
3704 unlink_request(osd, req);
3705 insert_request(need_resend, req);
3706 break;
3707 case CALC_TARGET_POOL_DNE:
4609245e 3708 check_pool_dne(req);
5aea3dcd 3709 break;
b0494532 3710 }
6f6c7006 3711 }
422d2cb8 3712}
6f6c7006 3713
42c1b124 3714static int handle_one_map(struct ceph_osd_client *osdc,
5aea3dcd
ID
3715 void *p, void *end, bool incremental,
3716 struct rb_root *need_resend,
3717 struct list_head *need_resend_linger)
42c1b124
ID
3718{
3719 struct ceph_osdmap *newmap;
3720 struct rb_node *n;
3721 bool skipped_map = false;
3722 bool was_full;
3723
b7ec35b3 3724 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
42c1b124
ID
3725 set_pool_was_full(osdc);
3726
3727 if (incremental)
3728 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3729 else
3730 newmap = ceph_osdmap_decode(&p, end);
3731 if (IS_ERR(newmap))
3732 return PTR_ERR(newmap);
3733
3734 if (newmap != osdc->osdmap) {
3735 /*
3736 * Preserve ->was_full before destroying the old map.
3737 * For pools that weren't in the old map, ->was_full
3738 * should be false.
3739 */
3740 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3741 struct ceph_pg_pool_info *pi =
3742 rb_entry(n, struct ceph_pg_pool_info, node);
3743 struct ceph_pg_pool_info *old_pi;
3744
3745 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3746 if (old_pi)
3747 pi->was_full = old_pi->was_full;
3748 else
3749 WARN_ON(pi->was_full);
3750 }
3751
3752 if (osdc->osdmap->epoch &&
3753 osdc->osdmap->epoch + 1 < newmap->epoch) {
3754 WARN_ON(incremental);
3755 skipped_map = true;
3756 }
3757
3758 ceph_osdmap_destroy(osdc->osdmap);
3759 osdc->osdmap = newmap;
3760 }
3761
b7ec35b3 3762 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
5aea3dcd
ID
3763 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3764 need_resend, need_resend_linger);
3765
3766 for (n = rb_first(&osdc->osds); n; ) {
3767 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3768
3769 n = rb_next(n); /* close_osd() */
3770
3771 scan_requests(osd, skipped_map, was_full, true, need_resend,
3772 need_resend_linger);
3773 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3774 memcmp(&osd->o_con.peer_addr,
3775 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3776 sizeof(struct ceph_entity_addr)))
3777 close_osd(osd);
3778 }
42c1b124
ID
3779
3780 return 0;
3781}
6f6c7006 3782
5aea3dcd
ID
3783static void kick_requests(struct ceph_osd_client *osdc,
3784 struct rb_root *need_resend,
3785 struct list_head *need_resend_linger)
3786{
922dab61 3787 struct ceph_osd_linger_request *lreq, *nlreq;
04c7d789 3788 enum calc_target_result ct_res;
5aea3dcd
ID
3789 struct rb_node *n;
3790
04c7d789
ID
3791 /* make sure need_resend targets reflect latest map */
3792 for (n = rb_first(need_resend); n; ) {
3793 struct ceph_osd_request *req =
3794 rb_entry(n, struct ceph_osd_request, r_node);
3795
3796 n = rb_next(n);
3797
3798 if (req->r_t.epoch < osdc->osdmap->epoch) {
3799 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3800 if (ct_res == CALC_TARGET_POOL_DNE) {
3801 erase_request(need_resend, req);
3802 check_pool_dne(req);
3803 }
3804 }
3805 }
3806
5aea3dcd
ID
3807 for (n = rb_first(need_resend); n; ) {
3808 struct ceph_osd_request *req =
3809 rb_entry(n, struct ceph_osd_request, r_node);
3810 struct ceph_osd *osd;
3811
3812 n = rb_next(n);
3813 erase_request(need_resend, req); /* before link_request() */
3814
5aea3dcd
ID
3815 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3816 link_request(osd, req);
3817 if (!req->r_linger) {
3818 if (!osd_homeless(osd) && !req->r_t.paused)
3819 send_request(req);
922dab61
ID
3820 } else {
3821 cancel_linger_request(req);
5aea3dcd
ID
3822 }
3823 }
922dab61
ID
3824
3825 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3826 if (!osd_homeless(lreq->osd))
3827 send_linger(lreq);
3828
3829 list_del_init(&lreq->scan_item);
3830 }
5aea3dcd
ID
3831}
3832
f24e9980
SW
3833/*
3834 * Process updated osd map.
3835 *
3836 * The message contains any number of incremental and full maps, normally
3837 * indicating some sort of topology change in the cluster. Kick requests
3838 * off to different OSDs as needed.
3839 */
3840void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3841{
42c1b124
ID
3842 void *p = msg->front.iov_base;
3843 void *const end = p + msg->front.iov_len;
f24e9980
SW
3844 u32 nr_maps, maplen;
3845 u32 epoch;
f24e9980 3846 struct ceph_fsid fsid;
5aea3dcd
ID
3847 struct rb_root need_resend = RB_ROOT;
3848 LIST_HEAD(need_resend_linger);
42c1b124
ID
3849 bool handled_incremental = false;
3850 bool was_pauserd, was_pausewr;
3851 bool pauserd, pausewr;
3852 int err;
f24e9980 3853
42c1b124 3854 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
5aea3dcd 3855 down_write(&osdc->lock);
f24e9980
SW
3856
3857 /* verify fsid */
3858 ceph_decode_need(&p, end, sizeof(fsid), bad);
3859 ceph_decode_copy(&p, &fsid, sizeof(fsid));
0743304d 3860 if (ceph_check_fsid(osdc->client, &fsid) < 0)
42c1b124 3861 goto bad;
f24e9980 3862
b7ec35b3
ID
3863 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3864 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3865 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
42c1b124 3866 have_pool_full(osdc);
9a1ea2db 3867
f24e9980
SW
3868 /* incremental maps */
3869 ceph_decode_32_safe(&p, end, nr_maps, bad);
3870 dout(" %d inc maps\n", nr_maps);
3871 while (nr_maps > 0) {
3872 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
3873 epoch = ceph_decode_32(&p);
3874 maplen = ceph_decode_32(&p);
f24e9980 3875 ceph_decode_need(&p, end, maplen, bad);
42c1b124
ID
3876 if (osdc->osdmap->epoch &&
3877 osdc->osdmap->epoch + 1 == epoch) {
f24e9980
SW
3878 dout("applying incremental map %u len %d\n",
3879 epoch, maplen);
5aea3dcd
ID
3880 err = handle_one_map(osdc, p, p + maplen, true,
3881 &need_resend, &need_resend_linger);
42c1b124 3882 if (err)
f24e9980 3883 goto bad;
42c1b124 3884 handled_incremental = true;
f24e9980
SW
3885 } else {
3886 dout("ignoring incremental map %u len %d\n",
3887 epoch, maplen);
3888 }
42c1b124 3889 p += maplen;
f24e9980
SW
3890 nr_maps--;
3891 }
42c1b124 3892 if (handled_incremental)
f24e9980
SW
3893 goto done;
3894
3895 /* full maps */
3896 ceph_decode_32_safe(&p, end, nr_maps, bad);
3897 dout(" %d full maps\n", nr_maps);
3898 while (nr_maps) {
3899 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
3900 epoch = ceph_decode_32(&p);
3901 maplen = ceph_decode_32(&p);
f24e9980
SW
3902 ceph_decode_need(&p, end, maplen, bad);
3903 if (nr_maps > 1) {
3904 dout("skipping non-latest full map %u len %d\n",
3905 epoch, maplen);
e5253a7b 3906 } else if (osdc->osdmap->epoch >= epoch) {
f24e9980
SW
3907 dout("skipping full map %u len %d, "
3908 "older than our %u\n", epoch, maplen,
3909 osdc->osdmap->epoch);
3910 } else {
3911 dout("taking full map %u len %d\n", epoch, maplen);
5aea3dcd
ID
3912 err = handle_one_map(osdc, p, p + maplen, false,
3913 &need_resend, &need_resend_linger);
42c1b124 3914 if (err)
f24e9980 3915 goto bad;
f24e9980
SW
3916 }
3917 p += maplen;
3918 nr_maps--;
3919 }
3920
3921done:
cd634fb6
SW
3922 /*
3923 * subscribe to subsequent osdmap updates if full to ensure
3924 * we find out when we are no longer full and stop returning
3925 * ENOSPC.
3926 */
b7ec35b3
ID
3927 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3928 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3929 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
42c1b124 3930 have_pool_full(osdc);
58eb7932
JL
3931 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3932 osdc->osdmap->epoch < osdc->epoch_barrier)
42c1b124 3933 maybe_request_map(osdc);
cd634fb6 3934
5aea3dcd 3935 kick_requests(osdc, &need_resend, &need_resend_linger);
42c1b124 3936
fc36d0a4 3937 ceph_osdc_abort_on_full(osdc);
42c1b124
ID
3938 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3939 osdc->osdmap->epoch);
5aea3dcd 3940 up_write(&osdc->lock);
03066f23 3941 wake_up_all(&osdc->client->auth_wq);
f24e9980
SW
3942 return;
3943
3944bad:
3945 pr_err("osdc handle_map corrupt msg\n");
9ec7cab1 3946 ceph_msg_dump(msg);
5aea3dcd
ID
3947 up_write(&osdc->lock);
3948}
3949
3950/*
3951 * Resubmit requests pending on the given osd.
3952 */
3953static void kick_osd_requests(struct ceph_osd *osd)
3954{
3955 struct rb_node *n;
3956
a02a946d
ID
3957 clear_backoffs(osd);
3958
922dab61 3959 for (n = rb_first(&osd->o_requests); n; ) {
5aea3dcd
ID
3960 struct ceph_osd_request *req =
3961 rb_entry(n, struct ceph_osd_request, r_node);
3962
922dab61
ID
3963 n = rb_next(n); /* cancel_linger_request() */
3964
5aea3dcd
ID
3965 if (!req->r_linger) {
3966 if (!req->r_t.paused)
3967 send_request(req);
922dab61
ID
3968 } else {
3969 cancel_linger_request(req);
5aea3dcd
ID
3970 }
3971 }
922dab61
ID
3972 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3973 struct ceph_osd_linger_request *lreq =
3974 rb_entry(n, struct ceph_osd_linger_request, node);
3975
3976 send_linger(lreq);
3977 }
5aea3dcd
ID
3978}
3979
3980/*
3981 * If the osd connection drops, we need to resubmit all requests.
3982 */
3983static void osd_fault(struct ceph_connection *con)
3984{
3985 struct ceph_osd *osd = con->private;
3986 struct ceph_osd_client *osdc = osd->o_osdc;
3987
3988 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3989
3990 down_write(&osdc->lock);
3991 if (!osd_registered(osd)) {
3992 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3993 goto out_unlock;
3994 }
3995
3996 if (!reopen_osd(osd))
3997 kick_osd_requests(osd);
3998 maybe_request_map(osdc);
3999
4000out_unlock:
4001 up_write(&osdc->lock);
f24e9980
SW
4002}
4003
a02a946d
ID
4004struct MOSDBackoff {
4005 struct ceph_spg spgid;
4006 u32 map_epoch;
4007 u8 op;
4008 u64 id;
4009 struct ceph_hobject_id *begin;
4010 struct ceph_hobject_id *end;
4011};
4012
4013static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4014{
4015 void *p = msg->front.iov_base;
4016 void *const end = p + msg->front.iov_len;
4017 u8 struct_v;
4018 u32 struct_len;
4019 int ret;
4020
4021 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4022 if (ret)
4023 return ret;
4024
4025 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4026 if (ret)
4027 return ret;
4028
4029 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4030 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4031 ceph_decode_8_safe(&p, end, m->op, e_inval);
4032 ceph_decode_64_safe(&p, end, m->id, e_inval);
4033
4034 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4035 if (!m->begin)
4036 return -ENOMEM;
4037
4038 ret = decode_hoid(&p, end, m->begin);
4039 if (ret) {
4040 free_hoid(m->begin);
4041 return ret;
4042 }
4043
4044 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4045 if (!m->end) {
4046 free_hoid(m->begin);
4047 return -ENOMEM;
4048 }
4049
4050 ret = decode_hoid(&p, end, m->end);
4051 if (ret) {
4052 free_hoid(m->begin);
4053 free_hoid(m->end);
4054 return ret;
4055 }
4056
4057 return 0;
4058
4059e_inval:
4060 return -EINVAL;
4061}
4062
4063static struct ceph_msg *create_backoff_message(
4064 const struct ceph_osd_backoff *backoff,
4065 u32 map_epoch)
4066{
4067 struct ceph_msg *msg;
4068 void *p, *end;
4069 int msg_size;
4070
4071 msg_size = CEPH_ENCODING_START_BLK_LEN +
4072 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4073 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4074 msg_size += CEPH_ENCODING_START_BLK_LEN +
4075 hoid_encoding_size(backoff->begin);
4076 msg_size += CEPH_ENCODING_START_BLK_LEN +
4077 hoid_encoding_size(backoff->end);
4078
4079 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4080 if (!msg)
4081 return NULL;
4082
4083 p = msg->front.iov_base;
4084 end = p + msg->front_alloc_len;
4085
4086 encode_spgid(&p, &backoff->spgid);
4087 ceph_encode_32(&p, map_epoch);
4088 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4089 ceph_encode_64(&p, backoff->id);
4090 encode_hoid(&p, end, backoff->begin);
4091 encode_hoid(&p, end, backoff->end);
4092 BUG_ON(p != end);
4093
4094 msg->front.iov_len = p - msg->front.iov_base;
4095 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4096 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4097
4098 return msg;
4099}
4100
4101static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4102{
4103 struct ceph_spg_mapping *spg;
4104 struct ceph_osd_backoff *backoff;
4105 struct ceph_msg *msg;
4106
4107 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4108 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4109
4110 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4111 if (!spg) {
4112 spg = alloc_spg_mapping();
4113 if (!spg) {
4114 pr_err("%s failed to allocate spg\n", __func__);
4115 return;
4116 }
4117 spg->spgid = m->spgid; /* struct */
4118 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4119 }
4120
4121 backoff = alloc_backoff();
4122 if (!backoff) {
4123 pr_err("%s failed to allocate backoff\n", __func__);
4124 return;
4125 }
4126 backoff->spgid = m->spgid; /* struct */
4127 backoff->id = m->id;
4128 backoff->begin = m->begin;
4129 m->begin = NULL; /* backoff now owns this */
4130 backoff->end = m->end;
4131 m->end = NULL; /* ditto */
4132
4133 insert_backoff(&spg->backoffs, backoff);
4134 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4135
4136 /*
4137 * Ack with original backoff's epoch so that the OSD can
4138 * discard this if there was a PG split.
4139 */
4140 msg = create_backoff_message(backoff, m->map_epoch);
4141 if (!msg) {
4142 pr_err("%s failed to allocate msg\n", __func__);
4143 return;
4144 }
4145 ceph_con_send(&osd->o_con, msg);
4146}
4147
4148static bool target_contained_by(const struct ceph_osd_request_target *t,
4149 const struct ceph_hobject_id *begin,
4150 const struct ceph_hobject_id *end)
4151{
4152 struct ceph_hobject_id hoid;
4153 int cmp;
4154
4155 hoid_fill_from_target(&hoid, t);
4156 cmp = hoid_compare(&hoid, begin);
4157 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4158}
4159
4160static void handle_backoff_unblock(struct ceph_osd *osd,
4161 const struct MOSDBackoff *m)
4162{
4163 struct ceph_spg_mapping *spg;
4164 struct ceph_osd_backoff *backoff;
4165 struct rb_node *n;
4166
4167 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4168 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4169
4170 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4171 if (!backoff) {
4172 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4173 __func__, osd->o_osd, m->spgid.pgid.pool,
4174 m->spgid.pgid.seed, m->spgid.shard, m->id);
4175 return;
4176 }
4177
4178 if (hoid_compare(backoff->begin, m->begin) &&
4179 hoid_compare(backoff->end, m->end)) {
4180 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4181 __func__, osd->o_osd, m->spgid.pgid.pool,
4182 m->spgid.pgid.seed, m->spgid.shard, m->id);
4183 /* unblock it anyway... */
4184 }
4185
4186 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4187 BUG_ON(!spg);
4188
4189 erase_backoff(&spg->backoffs, backoff);
4190 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4191 free_backoff(backoff);
4192
4193 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4194 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4195 free_spg_mapping(spg);
4196 }
4197
4198 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4199 struct ceph_osd_request *req =
4200 rb_entry(n, struct ceph_osd_request, r_node);
4201
4202 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4203 /*
4204 * Match against @m, not @backoff -- the PG may
4205 * have split on the OSD.
4206 */
4207 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4208 /*
4209 * If no other installed backoff applies,
4210 * resend.
4211 */
4212 send_request(req);
4213 }
4214 }
4215 }
4216}
4217
4218static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4219{
4220 struct ceph_osd_client *osdc = osd->o_osdc;
4221 struct MOSDBackoff m;
4222 int ret;
4223
4224 down_read(&osdc->lock);
4225 if (!osd_registered(osd)) {
4226 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4227 up_read(&osdc->lock);
4228 return;
4229 }
4230 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4231
4232 mutex_lock(&osd->lock);
4233 ret = decode_MOSDBackoff(msg, &m);
4234 if (ret) {
4235 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4236 ceph_msg_dump(msg);
4237 goto out_unlock;
4238 }
4239
4240 switch (m.op) {
4241 case CEPH_OSD_BACKOFF_OP_BLOCK:
4242 handle_backoff_block(osd, &m);
4243 break;
4244 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4245 handle_backoff_unblock(osd, &m);
4246 break;
4247 default:
4248 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4249 }
4250
4251 free_hoid(m.begin);
4252 free_hoid(m.end);
4253
4254out_unlock:
4255 mutex_unlock(&osd->lock);
4256 up_read(&osdc->lock);
4257}
4258
a40c4f10
YS
4259/*
4260 * Process osd watch notifications
4261 */
3c663bbd
AE
4262static void handle_watch_notify(struct ceph_osd_client *osdc,
4263 struct ceph_msg *msg)
a40c4f10 4264{
922dab61
ID
4265 void *p = msg->front.iov_base;
4266 void *const end = p + msg->front.iov_len;
4267 struct ceph_osd_linger_request *lreq;
4268 struct linger_work *lwork;
4269 u8 proto_ver, opcode;
4270 u64 cookie, notify_id;
4271 u64 notifier_id = 0;
19079203 4272 s32 return_code = 0;
922dab61
ID
4273 void *payload = NULL;
4274 u32 payload_len = 0;
a40c4f10
YS
4275
4276 ceph_decode_8_safe(&p, end, proto_ver, bad);
4277 ceph_decode_8_safe(&p, end, opcode, bad);
4278 ceph_decode_64_safe(&p, end, cookie, bad);
922dab61 4279 p += 8; /* skip ver */
a40c4f10
YS
4280 ceph_decode_64_safe(&p, end, notify_id, bad);
4281
922dab61
ID
4282 if (proto_ver >= 1) {
4283 ceph_decode_32_safe(&p, end, payload_len, bad);
4284 ceph_decode_need(&p, end, payload_len, bad);
4285 payload = p;
4286 p += payload_len;
4287 }
4288
4289 if (le16_to_cpu(msg->hdr.version) >= 2)
19079203 4290 ceph_decode_32_safe(&p, end, return_code, bad);
922dab61
ID
4291
4292 if (le16_to_cpu(msg->hdr.version) >= 3)
4293 ceph_decode_64_safe(&p, end, notifier_id, bad);
4294
4295 down_read(&osdc->lock);
4296 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4297 if (!lreq) {
4298 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4299 cookie);
4300 goto out_unlock_osdc;
4301 }
4302
4303 mutex_lock(&lreq->lock);
19079203
ID
4304 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4305 opcode, cookie, lreq, lreq->is_watch);
922dab61
ID
4306 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4307 if (!lreq->last_error) {
4308 lreq->last_error = -ENOTCONN;
4309 queue_watch_error(lreq);
4310 }
19079203
ID
4311 } else if (!lreq->is_watch) {
4312 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4313 if (lreq->notify_id && lreq->notify_id != notify_id) {
4314 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4315 lreq->notify_id, notify_id);
4316 } else if (!completion_done(&lreq->notify_finish_wait)) {
4317 struct ceph_msg_data *data =
4318 list_first_entry_or_null(&msg->data,
4319 struct ceph_msg_data,
4320 links);
4321
4322 if (data) {
4323 if (lreq->preply_pages) {
4324 WARN_ON(data->type !=
4325 CEPH_MSG_DATA_PAGES);
4326 *lreq->preply_pages = data->pages;
4327 *lreq->preply_len = data->length;
4328 } else {
4329 ceph_release_page_vector(data->pages,
4330 calc_pages_for(0, data->length));
4331 }
4332 }
4333 lreq->notify_finish_error = return_code;
4334 complete_all(&lreq->notify_finish_wait);
4335 }
922dab61
ID
4336 } else {
4337 /* CEPH_WATCH_EVENT_NOTIFY */
4338 lwork = lwork_alloc(lreq, do_watch_notify);
4339 if (!lwork) {
4340 pr_err("failed to allocate notify-lwork\n");
4341 goto out_unlock_lreq;
a40c4f10 4342 }
a40c4f10 4343
922dab61
ID
4344 lwork->notify.notify_id = notify_id;
4345 lwork->notify.notifier_id = notifier_id;
4346 lwork->notify.payload = payload;
4347 lwork->notify.payload_len = payload_len;
4348 lwork->notify.msg = ceph_msg_get(msg);
4349 lwork_queue(lwork);
91883cd2 4350 }
a40c4f10 4351
922dab61
ID
4352out_unlock_lreq:
4353 mutex_unlock(&lreq->lock);
4354out_unlock_osdc:
4355 up_read(&osdc->lock);
a40c4f10
YS
4356 return;
4357
4358bad:
4359 pr_err("osdc handle_watch_notify corrupt msg\n");
a40c4f10
YS
4360}
4361
70636773
AE
4362/*
4363 * Register request, send initial attempt.
4364 */
4365int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4366 struct ceph_osd_request *req,
4367 bool nofail)
4368{
5aea3dcd
ID
4369 down_read(&osdc->lock);
4370 submit_request(req, false);
4371 up_read(&osdc->lock);
0bbfdfe8 4372
5aea3dcd 4373 return 0;
f24e9980 4374}
3d14c5d2 4375EXPORT_SYMBOL(ceph_osdc_start_request);
f24e9980 4376
c9f9b93d 4377/*
c297eb42
ID
4378 * Unregister a registered request. The request is not completed:
4379 * ->r_result isn't set and __complete_request() isn't called.
c9f9b93d
ID
4380 */
4381void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4382{
4383 struct ceph_osd_client *osdc = req->r_osdc;
4384
5aea3dcd 4385 down_write(&osdc->lock);
5aea3dcd
ID
4386 if (req->r_osd)
4387 cancel_request(req);
4388 up_write(&osdc->lock);
c9f9b93d
ID
4389}
4390EXPORT_SYMBOL(ceph_osdc_cancel_request);
4391
f24e9980 4392/*
42b06965 4393 * @timeout: in jiffies, 0 means "wait forever"
f24e9980 4394 */
42b06965
ID
4395static int wait_request_timeout(struct ceph_osd_request *req,
4396 unsigned long timeout)
f24e9980 4397{
42b06965 4398 long left;
c9f9b93d 4399
42b06965 4400 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
0e76abf2 4401 left = wait_for_completion_killable_timeout(&req->r_completion,
42b06965
ID
4402 ceph_timeout_jiffies(timeout));
4403 if (left <= 0) {
4404 left = left ?: -ETIMEDOUT;
c9f9b93d 4405 ceph_osdc_cancel_request(req);
42b06965
ID
4406 } else {
4407 left = req->r_result; /* completed */
f24e9980
SW
4408 }
4409
42b06965
ID
4410 return left;
4411}
4412
4413/*
4414 * wait for a request to complete
4415 */
4416int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4417 struct ceph_osd_request *req)
4418{
4419 return wait_request_timeout(req, 0);
f24e9980 4420}
3d14c5d2 4421EXPORT_SYMBOL(ceph_osdc_wait_request);
f24e9980
SW
4422
4423/*
4424 * sync - wait for all in-flight requests to flush. avoid starvation.
4425 */
4426void ceph_osdc_sync(struct ceph_osd_client *osdc)
4427{
5aea3dcd
ID
4428 struct rb_node *n, *p;
4429 u64 last_tid = atomic64_read(&osdc->last_tid);
f24e9980 4430
5aea3dcd
ID
4431again:
4432 down_read(&osdc->lock);
4433 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4434 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4435
4436 mutex_lock(&osd->lock);
4437 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4438 struct ceph_osd_request *req =
4439 rb_entry(p, struct ceph_osd_request, r_node);
4440
4441 if (req->r_tid > last_tid)
4442 break;
4443
4444 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4445 continue;
f24e9980 4446
5aea3dcd
ID
4447 ceph_osdc_get_request(req);
4448 mutex_unlock(&osd->lock);
4449 up_read(&osdc->lock);
4450 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4451 __func__, req, req->r_tid, last_tid);
b18b9550 4452 wait_for_completion(&req->r_completion);
5aea3dcd
ID
4453 ceph_osdc_put_request(req);
4454 goto again;
4455 }
f24e9980 4456
5aea3dcd 4457 mutex_unlock(&osd->lock);
f24e9980 4458 }
5aea3dcd
ID
4459
4460 up_read(&osdc->lock);
4461 dout("%s done last_tid %llu\n", __func__, last_tid);
f24e9980 4462}
3d14c5d2 4463EXPORT_SYMBOL(ceph_osdc_sync);
f24e9980 4464
922dab61
ID
4465static struct ceph_osd_request *
4466alloc_linger_request(struct ceph_osd_linger_request *lreq)
4467{
4468 struct ceph_osd_request *req;
4469
4470 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4471 if (!req)
4472 return NULL;
4473
4474 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4475 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4476
4477 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4478 ceph_osdc_put_request(req);
4479 return NULL;
4480 }
4481
4482 return req;
4483}
4484
4485/*
4486 * Returns a handle, caller owns a ref.
4487 */
4488struct ceph_osd_linger_request *
4489ceph_osdc_watch(struct ceph_osd_client *osdc,
4490 struct ceph_object_id *oid,
4491 struct ceph_object_locator *oloc,
4492 rados_watchcb2_t wcb,
4493 rados_watcherrcb_t errcb,
4494 void *data)
4495{
4496 struct ceph_osd_linger_request *lreq;
4497 int ret;
4498
4499 lreq = linger_alloc(osdc);
4500 if (!lreq)
4501 return ERR_PTR(-ENOMEM);
4502
19079203 4503 lreq->is_watch = true;
922dab61
ID
4504 lreq->wcb = wcb;
4505 lreq->errcb = errcb;
4506 lreq->data = data;
b07d3c4b 4507 lreq->watch_valid_thru = jiffies;
922dab61
ID
4508
4509 ceph_oid_copy(&lreq->t.base_oid, oid);
4510 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
54ea0046 4511 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
fac02ddf 4512 ktime_get_real_ts64(&lreq->mtime);
922dab61
ID
4513
4514 lreq->reg_req = alloc_linger_request(lreq);
4515 if (!lreq->reg_req) {
4516 ret = -ENOMEM;
4517 goto err_put_lreq;
4518 }
4519
4520 lreq->ping_req = alloc_linger_request(lreq);
4521 if (!lreq->ping_req) {
4522 ret = -ENOMEM;
4523 goto err_put_lreq;
4524 }
4525
4526 down_write(&osdc->lock);
4527 linger_register(lreq); /* before osd_req_op_* */
4528 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4529 CEPH_OSD_WATCH_OP_WATCH);
4530 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4531 CEPH_OSD_WATCH_OP_PING);
4532 linger_submit(lreq);
4533 up_write(&osdc->lock);
4534
4535 ret = linger_reg_commit_wait(lreq);
4536 if (ret) {
4537 linger_cancel(lreq);
4538 goto err_put_lreq;
4539 }
4540
4541 return lreq;
4542
4543err_put_lreq:
4544 linger_put(lreq);
4545 return ERR_PTR(ret);
4546}
4547EXPORT_SYMBOL(ceph_osdc_watch);
4548
4549/*
4550 * Releases a ref.
4551 *
4552 * Times out after mount_timeout to preserve rbd unmap behaviour
4553 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4554 * with mount_timeout").
4555 */
4556int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4557 struct ceph_osd_linger_request *lreq)
4558{
4559 struct ceph_options *opts = osdc->client->options;
4560 struct ceph_osd_request *req;
4561 int ret;
4562
4563 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4564 if (!req)
4565 return -ENOMEM;
4566
4567 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4568 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
54ea0046 4569 req->r_flags = CEPH_OSD_FLAG_WRITE;
fac02ddf 4570 ktime_get_real_ts64(&req->r_mtime);
922dab61
ID
4571 osd_req_op_watch_init(req, 0, lreq->linger_id,
4572 CEPH_OSD_WATCH_OP_UNWATCH);
4573
4574 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4575 if (ret)
4576 goto out_put_req;
4577
4578 ceph_osdc_start_request(osdc, req, false);
4579 linger_cancel(lreq);
4580 linger_put(lreq);
4581 ret = wait_request_timeout(req, opts->mount_timeout);
4582
4583out_put_req:
4584 ceph_osdc_put_request(req);
4585 return ret;
4586}
4587EXPORT_SYMBOL(ceph_osdc_unwatch);
4588
4589static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4590 u64 notify_id, u64 cookie, void *payload,
6d54228f 4591 u32 payload_len)
922dab61
ID
4592{
4593 struct ceph_osd_req_op *op;
4594 struct ceph_pagelist *pl;
4595 int ret;
4596
4597 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4598
33165d47 4599 pl = ceph_pagelist_alloc(GFP_NOIO);
922dab61
ID
4600 if (!pl)
4601 return -ENOMEM;
4602
922dab61
ID
4603 ret = ceph_pagelist_encode_64(pl, notify_id);
4604 ret |= ceph_pagelist_encode_64(pl, cookie);
4605 if (payload) {
4606 ret |= ceph_pagelist_encode_32(pl, payload_len);
4607 ret |= ceph_pagelist_append(pl, payload, payload_len);
4608 } else {
4609 ret |= ceph_pagelist_encode_32(pl, 0);
4610 }
4611 if (ret) {
4612 ceph_pagelist_release(pl);
4613 return -ENOMEM;
4614 }
4615
4616 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4617 op->indata_len = pl->length;
4618 return 0;
4619}
4620
4621int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4622 struct ceph_object_id *oid,
4623 struct ceph_object_locator *oloc,
4624 u64 notify_id,
4625 u64 cookie,
4626 void *payload,
6d54228f 4627 u32 payload_len)
922dab61
ID
4628{
4629 struct ceph_osd_request *req;
4630 int ret;
4631
4632 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4633 if (!req)
4634 return -ENOMEM;
4635
4636 ceph_oid_copy(&req->r_base_oid, oid);
4637 ceph_oloc_copy(&req->r_base_oloc, oloc);
4638 req->r_flags = CEPH_OSD_FLAG_READ;
4639
4640 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4641 if (ret)
4642 goto out_put_req;
4643
4644 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4645 payload_len);
4646 if (ret)
4647 goto out_put_req;
4648
4649 ceph_osdc_start_request(osdc, req, false);
4650 ret = ceph_osdc_wait_request(osdc, req);
4651
4652out_put_req:
4653 ceph_osdc_put_request(req);
4654 return ret;
4655}
4656EXPORT_SYMBOL(ceph_osdc_notify_ack);
4657
19079203
ID
4658static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4659 u64 cookie, u32 prot_ver, u32 timeout,
6d54228f 4660 void *payload, u32 payload_len)
19079203
ID
4661{
4662 struct ceph_osd_req_op *op;
4663 struct ceph_pagelist *pl;
4664 int ret;
4665
4666 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4667 op->notify.cookie = cookie;
4668
33165d47 4669 pl = ceph_pagelist_alloc(GFP_NOIO);
19079203
ID
4670 if (!pl)
4671 return -ENOMEM;
4672
19079203
ID
4673 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4674 ret |= ceph_pagelist_encode_32(pl, timeout);
4675 ret |= ceph_pagelist_encode_32(pl, payload_len);
4676 ret |= ceph_pagelist_append(pl, payload, payload_len);
4677 if (ret) {
4678 ceph_pagelist_release(pl);
4679 return -ENOMEM;
4680 }
4681
4682 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4683 op->indata_len = pl->length;
4684 return 0;
4685}
4686
4687/*
4688 * @timeout: in seconds
4689 *
4690 * @preply_{pages,len} are initialized both on success and error.
4691 * The caller is responsible for:
4692 *
4693 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4694 */
4695int ceph_osdc_notify(struct ceph_osd_client *osdc,
4696 struct ceph_object_id *oid,
4697 struct ceph_object_locator *oloc,
4698 void *payload,
6d54228f 4699 u32 payload_len,
19079203
ID
4700 u32 timeout,
4701 struct page ***preply_pages,
4702 size_t *preply_len)
4703{
4704 struct ceph_osd_linger_request *lreq;
4705 struct page **pages;
4706 int ret;
4707
4708 WARN_ON(!timeout);
4709 if (preply_pages) {
4710 *preply_pages = NULL;
4711 *preply_len = 0;
4712 }
4713
4714 lreq = linger_alloc(osdc);
4715 if (!lreq)
4716 return -ENOMEM;
4717
4718 lreq->preply_pages = preply_pages;
4719 lreq->preply_len = preply_len;
4720
4721 ceph_oid_copy(&lreq->t.base_oid, oid);
4722 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4723 lreq->t.flags = CEPH_OSD_FLAG_READ;
4724
4725 lreq->reg_req = alloc_linger_request(lreq);
4726 if (!lreq->reg_req) {
4727 ret = -ENOMEM;
4728 goto out_put_lreq;
4729 }
4730
4731 /* for notify_id */
4732 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4733 if (IS_ERR(pages)) {
4734 ret = PTR_ERR(pages);
4735 goto out_put_lreq;
4736 }
4737
4738 down_write(&osdc->lock);
4739 linger_register(lreq); /* before osd_req_op_* */
4740 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
4741 timeout, payload, payload_len);
4742 if (ret) {
4743 linger_unregister(lreq);
4744 up_write(&osdc->lock);
4745 ceph_release_page_vector(pages, 1);
4746 goto out_put_lreq;
4747 }
4748 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4749 response_data),
4750 pages, PAGE_SIZE, 0, false, true);
4751 linger_submit(lreq);
4752 up_write(&osdc->lock);
4753
4754 ret = linger_reg_commit_wait(lreq);
4755 if (!ret)
4756 ret = linger_notify_finish_wait(lreq);
4757 else
4758 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4759
4760 linger_cancel(lreq);
4761out_put_lreq:
4762 linger_put(lreq);
4763 return ret;
4764}
4765EXPORT_SYMBOL(ceph_osdc_notify);
4766
b07d3c4b
ID
4767/*
4768 * Return the number of milliseconds since the watch was last
4769 * confirmed, or an error. If there is an error, the watch is no
4770 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4771 */
4772int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4773 struct ceph_osd_linger_request *lreq)
4774{
4775 unsigned long stamp, age;
4776 int ret;
4777
4778 down_read(&osdc->lock);
4779 mutex_lock(&lreq->lock);
4780 stamp = lreq->watch_valid_thru;
4781 if (!list_empty(&lreq->pending_lworks)) {
4782 struct linger_work *lwork =
4783 list_first_entry(&lreq->pending_lworks,
4784 struct linger_work,
4785 pending_item);
4786
4787 if (time_before(lwork->queued_stamp, stamp))
4788 stamp = lwork->queued_stamp;
4789 }
4790 age = jiffies - stamp;
4791 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4792 lreq, lreq->linger_id, age, lreq->last_error);
4793 /* we are truncating to msecs, so return a safe upper bound */
4794 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4795
4796 mutex_unlock(&lreq->lock);
4797 up_read(&osdc->lock);
4798 return ret;
4799}
4800
a4ed38d7
DF
4801static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4802{
4803 u8 struct_v;
4804 u32 struct_len;
4805 int ret;
4806
4807 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4808 &struct_v, &struct_len);
4809 if (ret)
4810 return ret;
4811
4812 ceph_decode_copy(p, &item->name, sizeof(item->name));
4813 item->cookie = ceph_decode_64(p);
4814 *p += 4; /* skip timeout_seconds */
4815 if (struct_v >= 2) {
4816 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4817 ceph_decode_addr(&item->addr);
4818 }
4819
4820 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4821 ENTITY_NAME(item->name), item->cookie,
4822 ceph_pr_addr(&item->addr.in_addr));
4823 return 0;
4824}
4825
4826static int decode_watchers(void **p, void *end,
4827 struct ceph_watch_item **watchers,
4828 u32 *num_watchers)
4829{
4830 u8 struct_v;
4831 u32 struct_len;
4832 int i;
4833 int ret;
4834
4835 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4836 &struct_v, &struct_len);
4837 if (ret)
4838 return ret;
4839
4840 *num_watchers = ceph_decode_32(p);
4841 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4842 if (!*watchers)
4843 return -ENOMEM;
4844
4845 for (i = 0; i < *num_watchers; i++) {
4846 ret = decode_watcher(p, end, *watchers + i);
4847 if (ret) {
4848 kfree(*watchers);
4849 return ret;
4850 }
4851 }
4852
4853 return 0;
4854}
4855
4856/*
4857 * On success, the caller is responsible for:
4858 *
4859 * kfree(watchers);
4860 */
4861int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4862 struct ceph_object_id *oid,
4863 struct ceph_object_locator *oloc,
4864 struct ceph_watch_item **watchers,
4865 u32 *num_watchers)
4866{
4867 struct ceph_osd_request *req;
4868 struct page **pages;
4869 int ret;
4870
4871 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4872 if (!req)
4873 return -ENOMEM;
4874
4875 ceph_oid_copy(&req->r_base_oid, oid);
4876 ceph_oloc_copy(&req->r_base_oloc, oloc);
4877 req->r_flags = CEPH_OSD_FLAG_READ;
4878
4879 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4880 if (ret)
4881 goto out_put_req;
4882
4883 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4884 if (IS_ERR(pages)) {
4885 ret = PTR_ERR(pages);
4886 goto out_put_req;
4887 }
4888
4889 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4890 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4891 response_data),
4892 pages, PAGE_SIZE, 0, false, true);
4893
4894 ceph_osdc_start_request(osdc, req, false);
4895 ret = ceph_osdc_wait_request(osdc, req);
4896 if (ret >= 0) {
4897 void *p = page_address(pages[0]);
4898 void *const end = p + req->r_ops[0].outdata_len;
4899
4900 ret = decode_watchers(&p, end, watchers, num_watchers);
4901 }
4902
4903out_put_req:
4904 ceph_osdc_put_request(req);
4905 return ret;
4906}
4907EXPORT_SYMBOL(ceph_osdc_list_watchers);
4908
dd935f44
JD
4909/*
4910 * Call all pending notify callbacks - for use after a watch is
4911 * unregistered, to make sure no more callbacks for it will be invoked
4912 */
f6479449 4913void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
dd935f44 4914{
99d16943 4915 dout("%s osdc %p\n", __func__, osdc);
dd935f44
JD
4916 flush_workqueue(osdc->notify_wq);
4917}
4918EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4919
7cca78c9
ID
4920void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4921{
4922 down_read(&osdc->lock);
4923 maybe_request_map(osdc);
4924 up_read(&osdc->lock);
4925}
4926EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
dd935f44 4927
428a7158
DF
4928/*
4929 * Execute an OSD class method on an object.
4930 *
4931 * @flags: CEPH_OSD_FLAG_*
2544a020 4932 * @resp_len: in/out param for reply length
428a7158
DF
4933 */
4934int ceph_osdc_call(struct ceph_osd_client *osdc,
4935 struct ceph_object_id *oid,
4936 struct ceph_object_locator *oloc,
4937 const char *class, const char *method,
4938 unsigned int flags,
4939 struct page *req_page, size_t req_len,
4940 struct page *resp_page, size_t *resp_len)
4941{
4942 struct ceph_osd_request *req;
4943 int ret;
4944
2544a020
ID
4945 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4946 return -E2BIG;
4947
428a7158
DF
4948 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4949 if (!req)
4950 return -ENOMEM;
4951
4952 ceph_oid_copy(&req->r_base_oid, oid);
4953 ceph_oloc_copy(&req->r_base_oloc, oloc);
4954 req->r_flags = flags;
4955
4956 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4957 if (ret)
4958 goto out_put_req;
4959
24639ce5 4960 ret = osd_req_op_cls_init(req, 0, class, method);
fe943d50
CX
4961 if (ret)
4962 goto out_put_req;
4963
428a7158
DF
4964 if (req_page)
4965 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4966 0, false, false);
4967 if (resp_page)
4968 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
2544a020 4969 *resp_len, 0, false, false);
428a7158
DF
4970
4971 ceph_osdc_start_request(osdc, req, false);
4972 ret = ceph_osdc_wait_request(osdc, req);
4973 if (ret >= 0) {
4974 ret = req->r_ops[0].rval;
4975 if (resp_page)
4976 *resp_len = req->r_ops[0].outdata_len;
4977 }
4978
4979out_put_req:
4980 ceph_osdc_put_request(req);
4981 return ret;
4982}
4983EXPORT_SYMBOL(ceph_osdc_call);
4984
f24e9980
SW
4985/*
4986 * init, shutdown
4987 */
4988int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4989{
4990 int err;
4991
4992 dout("init\n");
4993 osdc->client = client;
5aea3dcd 4994 init_rwsem(&osdc->lock);
f24e9980 4995 osdc->osds = RB_ROOT;
f5a2041b 4996 INIT_LIST_HEAD(&osdc->osd_lru);
9dd2845c 4997 spin_lock_init(&osdc->osd_lru_lock);
5aea3dcd
ID
4998 osd_init(&osdc->homeless_osd);
4999 osdc->homeless_osd.o_osdc = osdc;
5000 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
264048af 5001 osdc->last_linger_id = CEPH_LINGER_ID_START;
922dab61 5002 osdc->linger_requests = RB_ROOT;
4609245e
ID
5003 osdc->map_checks = RB_ROOT;
5004 osdc->linger_map_checks = RB_ROOT;
f24e9980 5005 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
f5a2041b
YS
5006 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5007
5f44f142 5008 err = -ENOMEM;
e5253a7b
ID
5009 osdc->osdmap = ceph_osdmap_alloc();
5010 if (!osdc->osdmap)
5011 goto out;
5012
9e767adb
ID
5013 osdc->req_mempool = mempool_create_slab_pool(10,
5014 ceph_osd_request_cache);
f24e9980 5015 if (!osdc->req_mempool)
e5253a7b 5016 goto out_map;
f24e9980 5017
d50b409f 5018 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
711da55d 5019 PAGE_SIZE, 10, true, "osd_op");
f24e9980 5020 if (err < 0)
5f44f142 5021 goto out_mempool;
d50b409f 5022 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
711da55d 5023 PAGE_SIZE, 10, true, "osd_op_reply");
c16e7869
SW
5024 if (err < 0)
5025 goto out_msgpool;
a40c4f10 5026
dbcae088 5027 err = -ENOMEM;
a40c4f10 5028 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
dbcae088 5029 if (!osdc->notify_wq)
c172ec5c
ID
5030 goto out_msgpool_reply;
5031
88bc1922
ID
5032 osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5033 if (!osdc->completion_wq)
5034 goto out_notify_wq;
5035
fbca9635
ID
5036 schedule_delayed_work(&osdc->timeout_work,
5037 osdc->client->options->osd_keepalive_timeout);
b37ee1b9
ID
5038 schedule_delayed_work(&osdc->osds_timeout_work,
5039 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5040
f24e9980 5041 return 0;
5f44f142 5042
88bc1922
ID
5043out_notify_wq:
5044 destroy_workqueue(osdc->notify_wq);
c172ec5c
ID
5045out_msgpool_reply:
5046 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
c16e7869
SW
5047out_msgpool:
5048 ceph_msgpool_destroy(&osdc->msgpool_op);
5f44f142
SW
5049out_mempool:
5050 mempool_destroy(osdc->req_mempool);
e5253a7b
ID
5051out_map:
5052 ceph_osdmap_destroy(osdc->osdmap);
5f44f142
SW
5053out:
5054 return err;
f24e9980
SW
5055}
5056
5057void ceph_osdc_stop(struct ceph_osd_client *osdc)
5058{
88bc1922 5059 destroy_workqueue(osdc->completion_wq);
a40c4f10 5060 destroy_workqueue(osdc->notify_wq);
f24e9980 5061 cancel_delayed_work_sync(&osdc->timeout_work);
f5a2041b 5062 cancel_delayed_work_sync(&osdc->osds_timeout_work);
42a2c09f 5063
5aea3dcd 5064 down_write(&osdc->lock);
42a2c09f
ID
5065 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5066 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5067 struct ceph_osd, o_node);
5aea3dcd 5068 close_osd(osd);
42a2c09f 5069 }
5aea3dcd 5070 up_write(&osdc->lock);
02113a0f 5071 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
5aea3dcd
ID
5072 osd_cleanup(&osdc->homeless_osd);
5073
5074 WARN_ON(!list_empty(&osdc->osd_lru));
922dab61 5075 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
4609245e
ID
5076 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5077 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
5aea3dcd
ID
5078 WARN_ON(atomic_read(&osdc->num_requests));
5079 WARN_ON(atomic_read(&osdc->num_homeless));
42a2c09f 5080
e5253a7b 5081 ceph_osdmap_destroy(osdc->osdmap);
f24e9980
SW
5082 mempool_destroy(osdc->req_mempool);
5083 ceph_msgpool_destroy(&osdc->msgpool_op);
c16e7869 5084 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
f24e9980
SW
5085}
5086
5087/*
5088 * Read some contiguous pages. If we cross a stripe boundary, shorten
5089 * *plen. Return number of bytes read, or error.
5090 */
5091int ceph_osdc_readpages(struct ceph_osd_client *osdc,
5092 struct ceph_vino vino, struct ceph_file_layout *layout,
5093 u64 off, u64 *plen,
5094 u32 truncate_seq, u64 truncate_size,
b7495fc2 5095 struct page **pages, int num_pages, int page_align)
f24e9980
SW
5096{
5097 struct ceph_osd_request *req;
5098 int rc = 0;
5099
5100 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
5101 vino.snap, off, *plen);
715e4cd4 5102 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
f24e9980 5103 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
acead002 5104 NULL, truncate_seq, truncate_size,
153e5167 5105 false);
6816282d
SW
5106 if (IS_ERR(req))
5107 return PTR_ERR(req);
f24e9980
SW
5108
5109 /* it may be a short read due to an object boundary */
406e2c9f 5110 osd_req_op_extent_osd_data_pages(req, 0,
a4ce40a9 5111 pages, *plen, page_align, false, false);
f24e9980 5112
e0c59487 5113 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
43bfe5de 5114 off, *plen, *plen, page_align);
f24e9980
SW
5115
5116 rc = ceph_osdc_start_request(osdc, req, false);
5117 if (!rc)
5118 rc = ceph_osdc_wait_request(osdc, req);
5119
5120 ceph_osdc_put_request(req);
5121 dout("readpages result %d\n", rc);
5122 return rc;
5123}
3d14c5d2 5124EXPORT_SYMBOL(ceph_osdc_readpages);
f24e9980
SW
5125
5126/*
5127 * do a synchronous write on N pages
5128 */
5129int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
5130 struct ceph_file_layout *layout,
5131 struct ceph_snap_context *snapc,
5132 u64 off, u64 len,
5133 u32 truncate_seq, u64 truncate_size,
fac02ddf 5134 struct timespec64 *mtime,
24808826 5135 struct page **pages, int num_pages)
f24e9980
SW
5136{
5137 struct ceph_osd_request *req;
5138 int rc = 0;
b7495fc2 5139 int page_align = off & ~PAGE_MASK;
f24e9980 5140
715e4cd4 5141 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
54ea0046 5142 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
acead002 5143 snapc, truncate_seq, truncate_size,
153e5167 5144 true);
6816282d
SW
5145 if (IS_ERR(req))
5146 return PTR_ERR(req);
f24e9980
SW
5147
5148 /* it may be a short write due to an object boundary */
406e2c9f 5149 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
43bfe5de
AE
5150 false, false);
5151 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
f24e9980 5152
bb873b53 5153 req->r_mtime = *mtime;
87f979d3 5154 rc = ceph_osdc_start_request(osdc, req, true);
f24e9980
SW
5155 if (!rc)
5156 rc = ceph_osdc_wait_request(osdc, req);
5157
5158 ceph_osdc_put_request(req);
5159 if (rc == 0)
5160 rc = len;
5161 dout("writepages result %d\n", rc);
5162 return rc;
5163}
3d14c5d2 5164EXPORT_SYMBOL(ceph_osdc_writepages);
f24e9980 5165
57a35dfb 5166int __init ceph_osdc_setup(void)
5522ae0b 5167{
3f1af42a
ID
5168 size_t size = sizeof(struct ceph_osd_request) +
5169 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5170
5522ae0b 5171 BUG_ON(ceph_osd_request_cache);
3f1af42a
ID
5172 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5173 0, 0, NULL);
5522ae0b
AE
5174
5175 return ceph_osd_request_cache ? 0 : -ENOMEM;
5176}
5522ae0b
AE
5177
5178void ceph_osdc_cleanup(void)
5179{
5180 BUG_ON(!ceph_osd_request_cache);
5181 kmem_cache_destroy(ceph_osd_request_cache);
5182 ceph_osd_request_cache = NULL;
5183}
5522ae0b 5184
f24e9980
SW
5185/*
5186 * handle incoming message
5187 */
5188static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5189{
5190 struct ceph_osd *osd = con->private;
5aea3dcd 5191 struct ceph_osd_client *osdc = osd->o_osdc;
f24e9980
SW
5192 int type = le16_to_cpu(msg->hdr.type);
5193
f24e9980
SW
5194 switch (type) {
5195 case CEPH_MSG_OSD_MAP:
5196 ceph_osdc_handle_map(osdc, msg);
5197 break;
5198 case CEPH_MSG_OSD_OPREPLY:
5aea3dcd 5199 handle_reply(osd, msg);
f24e9980 5200 break;
a02a946d
ID
5201 case CEPH_MSG_OSD_BACKOFF:
5202 handle_backoff(osd, msg);
5203 break;
a40c4f10
YS
5204 case CEPH_MSG_WATCH_NOTIFY:
5205 handle_watch_notify(osdc, msg);
5206 break;
f24e9980
SW
5207
5208 default:
5209 pr_err("received unknown message type %d %s\n", type,
5210 ceph_msg_type_name(type));
5211 }
5aea3dcd 5212
f24e9980
SW
5213 ceph_msg_put(msg);
5214}
5215
5b3a4db3 5216/*
d15f9d69
ID
5217 * Lookup and return message for incoming reply. Don't try to do
5218 * anything about a larger than preallocated data portion of the
5219 * message at the moment - for now, just skip the message.
5b3a4db3
SW
5220 */
5221static struct ceph_msg *get_reply(struct ceph_connection *con,
2450418c
YS
5222 struct ceph_msg_header *hdr,
5223 int *skip)
f24e9980
SW
5224{
5225 struct ceph_osd *osd = con->private;
5226 struct ceph_osd_client *osdc = osd->o_osdc;
5aea3dcd 5227 struct ceph_msg *m = NULL;
0547a9b3 5228 struct ceph_osd_request *req;
3f0a4ac5 5229 int front_len = le32_to_cpu(hdr->front_len);
5b3a4db3 5230 int data_len = le32_to_cpu(hdr->data_len);
5aea3dcd 5231 u64 tid = le64_to_cpu(hdr->tid);
f24e9980 5232
5aea3dcd
ID
5233 down_read(&osdc->lock);
5234 if (!osd_registered(osd)) {
5235 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5236 *skip = 1;
5237 goto out_unlock_osdc;
5238 }
5239 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5240
5241 mutex_lock(&osd->lock);
5242 req = lookup_request(&osd->o_requests, tid);
0547a9b3 5243 if (!req) {
cd8140c6
ID
5244 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5245 osd->o_osd, tid);
d15f9d69 5246 *skip = 1;
5aea3dcd 5247 goto out_unlock_session;
0547a9b3 5248 }
c16e7869 5249
ace6d3a9 5250 ceph_msg_revoke_incoming(req->r_reply);
0547a9b3 5251
f2be82b0 5252 if (front_len > req->r_reply->front_alloc_len) {
d15f9d69
ID
5253 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5254 __func__, osd->o_osd, req->r_tid, front_len,
5255 req->r_reply->front_alloc_len);
3f0a4ac5
ID
5256 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5257 false);
a79832f2 5258 if (!m)
5aea3dcd 5259 goto out_unlock_session;
c16e7869
SW
5260 ceph_msg_put(req->r_reply);
5261 req->r_reply = m;
5262 }
0fff87ec 5263
d15f9d69
ID
5264 if (data_len > req->r_reply->data_length) {
5265 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5266 __func__, osd->o_osd, req->r_tid, data_len,
5267 req->r_reply->data_length);
5268 m = NULL;
5269 *skip = 1;
5aea3dcd 5270 goto out_unlock_session;
0547a9b3 5271 }
d15f9d69
ID
5272
5273 m = ceph_msg_get(req->r_reply);
c16e7869 5274 dout("get_reply tid %lld %p\n", tid, m);
0547a9b3 5275
5aea3dcd
ID
5276out_unlock_session:
5277 mutex_unlock(&osd->lock);
5278out_unlock_osdc:
5279 up_read(&osdc->lock);
2450418c 5280 return m;
5b3a4db3
SW
5281}
5282
19079203
ID
5283/*
5284 * TODO: switch to a msg-owned pagelist
5285 */
5286static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5287{
5288 struct ceph_msg *m;
5289 int type = le16_to_cpu(hdr->type);
5290 u32 front_len = le32_to_cpu(hdr->front_len);
5291 u32 data_len = le32_to_cpu(hdr->data_len);
5292
5293 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
5294 if (!m)
5295 return NULL;
5296
5297 if (data_len) {
5298 struct page **pages;
5299 struct ceph_osd_data osd_data;
5300
5301 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5302 GFP_NOIO);
c22e853a 5303 if (IS_ERR(pages)) {
19079203
ID
5304 ceph_msg_put(m);
5305 return NULL;
5306 }
5307
5308 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
5309 false);
5310 ceph_osdc_msg_data_add(m, &osd_data);
5311 }
5312
5313 return m;
5314}
5315
5b3a4db3
SW
5316static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5317 struct ceph_msg_header *hdr,
5318 int *skip)
5319{
5320 struct ceph_osd *osd = con->private;
5321 int type = le16_to_cpu(hdr->type);
5b3a4db3 5322
1c20f2d2 5323 *skip = 0;
5b3a4db3
SW
5324 switch (type) {
5325 case CEPH_MSG_OSD_MAP:
a02a946d 5326 case CEPH_MSG_OSD_BACKOFF:
a40c4f10 5327 case CEPH_MSG_WATCH_NOTIFY:
19079203 5328 return alloc_msg_with_page_vector(hdr);
5b3a4db3
SW
5329 case CEPH_MSG_OSD_OPREPLY:
5330 return get_reply(con, hdr, skip);
5331 default:
5aea3dcd
ID
5332 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5333 osd->o_osd, type);
5b3a4db3
SW
5334 *skip = 1;
5335 return NULL;
5336 }
f24e9980
SW
5337}
5338
5339/*
5340 * Wrappers to refcount containing ceph_osd struct
5341 */
5342static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5343{
5344 struct ceph_osd *osd = con->private;
5345 if (get_osd(osd))
5346 return con;
5347 return NULL;
5348}
5349
5350static void put_osd_con(struct ceph_connection *con)
5351{
5352 struct ceph_osd *osd = con->private;
5353 put_osd(osd);
5354}
5355
4e7a5dcd
SW
5356/*
5357 * authentication
5358 */
a3530df3
AE
5359/*
5360 * Note: returned pointer is the address of a structure that's
5361 * managed separately. Caller must *not* attempt to free it.
5362 */
5363static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 5364 int *proto, int force_new)
4e7a5dcd
SW
5365{
5366 struct ceph_osd *o = con->private;
5367 struct ceph_osd_client *osdc = o->o_osdc;
5368 struct ceph_auth_client *ac = osdc->client->monc.auth;
74f1869f 5369 struct ceph_auth_handshake *auth = &o->o_auth;
4e7a5dcd 5370
74f1869f 5371 if (force_new && auth->authorizer) {
6c1ea260 5372 ceph_auth_destroy_authorizer(auth->authorizer);
74f1869f
AE
5373 auth->authorizer = NULL;
5374 }
27859f97
SW
5375 if (!auth->authorizer) {
5376 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5377 auth);
4e7a5dcd 5378 if (ret)
a3530df3 5379 return ERR_PTR(ret);
27859f97
SW
5380 } else {
5381 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
0bed9b5c
SW
5382 auth);
5383 if (ret)
5384 return ERR_PTR(ret);
4e7a5dcd 5385 }
4e7a5dcd 5386 *proto = ac->protocol;
74f1869f 5387
a3530df3 5388 return auth;
4e7a5dcd
SW
5389}
5390
6daca13d
ID
5391static int add_authorizer_challenge(struct ceph_connection *con,
5392 void *challenge_buf, int challenge_buf_len)
5393{
5394 struct ceph_osd *o = con->private;
5395 struct ceph_osd_client *osdc = o->o_osdc;
5396 struct ceph_auth_client *ac = osdc->client->monc.auth;
5397
5398 return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
5399 challenge_buf, challenge_buf_len);
5400}
4e7a5dcd 5401
0dde5848 5402static int verify_authorizer_reply(struct ceph_connection *con)
4e7a5dcd
SW
5403{
5404 struct ceph_osd *o = con->private;
5405 struct ceph_osd_client *osdc = o->o_osdc;
5406 struct ceph_auth_client *ac = osdc->client->monc.auth;
5407
0dde5848 5408 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
4e7a5dcd
SW
5409}
5410
9bd2e6f8
SW
5411static int invalidate_authorizer(struct ceph_connection *con)
5412{
5413 struct ceph_osd *o = con->private;
5414 struct ceph_osd_client *osdc = o->o_osdc;
5415 struct ceph_auth_client *ac = osdc->client->monc.auth;
5416
27859f97 5417 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
9bd2e6f8
SW
5418 return ceph_monc_validate_auth(&osdc->client->monc);
5419}
4e7a5dcd 5420
8cb441c0
ID
5421static void osd_reencode_message(struct ceph_msg *msg)
5422{
914902af
ID
5423 int type = le16_to_cpu(msg->hdr.type);
5424
5425 if (type == CEPH_MSG_OSD_OP)
5426 encode_request_finish(msg);
8cb441c0
ID
5427}
5428
79dbd1ba 5429static int osd_sign_message(struct ceph_msg *msg)
33d07337 5430{
79dbd1ba 5431 struct ceph_osd *o = msg->con->private;
33d07337 5432 struct ceph_auth_handshake *auth = &o->o_auth;
79dbd1ba 5433
33d07337
YZ
5434 return ceph_auth_sign_message(auth, msg);
5435}
5436
79dbd1ba 5437static int osd_check_message_signature(struct ceph_msg *msg)
33d07337 5438{
79dbd1ba 5439 struct ceph_osd *o = msg->con->private;
33d07337 5440 struct ceph_auth_handshake *auth = &o->o_auth;
79dbd1ba 5441
33d07337
YZ
5442 return ceph_auth_check_message_signature(auth, msg);
5443}
5444
9e32789f 5445static const struct ceph_connection_operations osd_con_ops = {
f24e9980
SW
5446 .get = get_osd_con,
5447 .put = put_osd_con,
5448 .dispatch = dispatch,
4e7a5dcd 5449 .get_authorizer = get_authorizer,
6daca13d 5450 .add_authorizer_challenge = add_authorizer_challenge,
4e7a5dcd 5451 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 5452 .invalidate_authorizer = invalidate_authorizer,
f24e9980 5453 .alloc_msg = alloc_msg,
8cb441c0 5454 .reencode_message = osd_reencode_message,
79dbd1ba
ID
5455 .sign_message = osd_sign_message,
5456 .check_message_signature = osd_check_message_signature,
5aea3dcd 5457 .fault = osd_fault,
f24e9980 5458};
This page took 1.166376 seconds and 4 git commands to generate.