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