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