]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blame - net/ceph/osd_client.c
libceph: always populate t->target_{oid,oloc} in calc_target()
[mirror_ubuntu-disco-kernel.git] / net / ceph / osd_client.c
CommitLineData
a4ce40a9 1
3d14c5d2 2#include <linux/ceph/ceph_debug.h>
f24e9980 3
3d14c5d2 4#include <linux/module.h>
f24e9980
SW
5#include <linux/err.h>
6#include <linux/highmem.h>
7#include <linux/mm.h>
8#include <linux/pagemap.h>
9#include <linux/slab.h>
10#include <linux/uaccess.h>
68b4476b
YS
11#ifdef CONFIG_BLOCK
12#include <linux/bio.h>
13#endif
f24e9980 14
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;
b7ec35b3 1333 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
63244fa1
ID
1334 enum calc_target_result ct_res;
1335 int ret;
1336
04c7d789 1337 t->epoch = osdc->osdmap->epoch;
63244fa1
ID
1338 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1339 if (!pi) {
1340 t->osd = CEPH_HOMELESS_OSD;
1341 ct_res = CALC_TARGET_POOL_DNE;
1342 goto out;
1343 }
1344
1345 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
dc93e0e2
ID
1346 if (t->last_force_resend < pi->last_force_request_resend) {
1347 t->last_force_resend = pi->last_force_request_resend;
63244fa1 1348 force_resend = true;
dc93e0e2 1349 } else if (t->last_force_resend == 0) {
63244fa1
ID
1350 force_resend = true;
1351 }
1352 }
63244fa1 1353
db098ec4
ID
1354 /* apply tiering */
1355 ceph_oid_copy(&t->target_oid, &t->base_oid);
1356 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1357 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
63244fa1
ID
1358 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1359 t->target_oloc.pool = pi->read_tier;
1360 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1361 t->target_oloc.pool = pi->write_tier;
1362 }
1363
1364 ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1365 &t->target_oloc, &pgid);
1366 if (ret) {
1367 WARN_ON(ret != -ENOENT);
1368 t->osd = CEPH_HOMELESS_OSD;
1369 ct_res = CALC_TARGET_POOL_DNE;
1370 goto out;
1371 }
1372 last_pgid.pool = pgid.pool;
1373 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1374
1375 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1376 if (any_change &&
1377 ceph_is_new_interval(&t->acting,
1378 &acting,
1379 &t->up,
1380 &up,
1381 t->size,
1382 pi->size,
1383 t->min_size,
1384 pi->min_size,
1385 t->pg_num,
1386 pi->pg_num,
1387 t->sort_bitwise,
1388 sort_bitwise,
1389 &last_pgid))
1390 force_resend = true;
1391
1392 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1393 t->paused = false;
84ed45df 1394 unpaused = true;
63244fa1 1395 }
84ed45df
ID
1396 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1397 ceph_osds_changed(&t->acting, &acting, any_change);
7de030d6
ID
1398 if (t->pg_num)
1399 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
63244fa1 1400
7de030d6 1401 if (legacy_change || force_resend || split) {
63244fa1 1402 t->pgid = pgid; /* struct */
dc98ff72 1403 ceph_pg_to_primary_shard(osdc->osdmap, &pgid, &t->spgid);
63244fa1
ID
1404 ceph_osds_copy(&t->acting, &acting);
1405 ceph_osds_copy(&t->up, &up);
1406 t->size = pi->size;
1407 t->min_size = pi->min_size;
1408 t->pg_num = pi->pg_num;
1409 t->pg_num_mask = pi->pg_num_mask;
1410 t->sort_bitwise = sort_bitwise;
1411
1412 t->osd = acting.primary;
63244fa1
ID
1413 }
1414
7de030d6
ID
1415 if (unpaused || legacy_change || force_resend ||
1416 (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1417 RESEND_ON_SPLIT)))
84ed45df
ID
1418 ct_res = CALC_TARGET_NEED_RESEND;
1419 else
1420 ct_res = CALC_TARGET_NO_ACTION;
1421
63244fa1
ID
1422out:
1423 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1424 return ct_res;
1425}
1426
bb873b53
ID
1427static void setup_request_data(struct ceph_osd_request *req,
1428 struct ceph_msg *msg)
f24e9980 1429{
bb873b53
ID
1430 u32 data_len = 0;
1431 int i;
1432
1433 if (!list_empty(&msg->data))
1434 return;
f24e9980 1435
bb873b53
ID
1436 WARN_ON(msg->data_length);
1437 for (i = 0; i < req->r_num_ops; i++) {
1438 struct ceph_osd_req_op *op = &req->r_ops[i];
1439
1440 switch (op->op) {
1441 /* request */
1442 case CEPH_OSD_OP_WRITE:
1443 case CEPH_OSD_OP_WRITEFULL:
1444 WARN_ON(op->indata_len != op->extent.length);
1445 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1446 break;
1447 case CEPH_OSD_OP_SETXATTR:
1448 case CEPH_OSD_OP_CMPXATTR:
1449 WARN_ON(op->indata_len != op->xattr.name_len +
1450 op->xattr.value_len);
1451 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1452 break;
922dab61
ID
1453 case CEPH_OSD_OP_NOTIFY_ACK:
1454 ceph_osdc_msg_data_add(msg,
1455 &op->notify_ack.request_data);
1456 break;
bb873b53
ID
1457
1458 /* reply */
1459 case CEPH_OSD_OP_STAT:
1460 ceph_osdc_msg_data_add(req->r_reply,
1461 &op->raw_data_in);
1462 break;
1463 case CEPH_OSD_OP_READ:
1464 ceph_osdc_msg_data_add(req->r_reply,
1465 &op->extent.osd_data);
1466 break;
a4ed38d7
DF
1467 case CEPH_OSD_OP_LIST_WATCHERS:
1468 ceph_osdc_msg_data_add(req->r_reply,
1469 &op->list_watchers.response_data);
1470 break;
bb873b53
ID
1471
1472 /* both */
1473 case CEPH_OSD_OP_CALL:
1474 WARN_ON(op->indata_len != op->cls.class_len +
1475 op->cls.method_len +
1476 op->cls.indata_len);
1477 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1478 /* optional, can be NONE */
1479 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1480 /* optional, can be NONE */
1481 ceph_osdc_msg_data_add(req->r_reply,
1482 &op->cls.response_data);
1483 break;
19079203
ID
1484 case CEPH_OSD_OP_NOTIFY:
1485 ceph_osdc_msg_data_add(msg,
1486 &op->notify.request_data);
1487 ceph_osdc_msg_data_add(req->r_reply,
1488 &op->notify.response_data);
1489 break;
bb873b53
ID
1490 }
1491
1492 data_len += op->indata_len;
1493 }
1b83bef2 1494
bb873b53
ID
1495 WARN_ON(data_len != msg->data_length);
1496}
1497
2e59ffd1
ID
1498static void encode_pgid(void **p, const struct ceph_pg *pgid)
1499{
1500 ceph_encode_8(p, 1);
1501 ceph_encode_64(p, pgid->pool);
1502 ceph_encode_32(p, pgid->seed);
1503 ceph_encode_32(p, -1); /* preferred */
1504}
1505
8cb441c0
ID
1506static void encode_spgid(void **p, const struct ceph_spg *spgid)
1507{
1508 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1509 encode_pgid(p, &spgid->pgid);
1510 ceph_encode_8(p, spgid->shard);
1511}
1512
2e59ffd1
ID
1513static void encode_oloc(void **p, void *end,
1514 const struct ceph_object_locator *oloc)
1515{
1516 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1517 ceph_encode_64(p, oloc->pool);
1518 ceph_encode_32(p, -1); /* preferred */
1519 ceph_encode_32(p, 0); /* key len */
1520 if (oloc->pool_ns)
1521 ceph_encode_string(p, end, oloc->pool_ns->str,
1522 oloc->pool_ns->len);
1523 else
1524 ceph_encode_32(p, 0);
1525}
1526
8cb441c0
ID
1527static void encode_request_partial(struct ceph_osd_request *req,
1528 struct ceph_msg *msg)
bb873b53
ID
1529{
1530 void *p = msg->front.iov_base;
1531 void *const end = p + msg->front_alloc_len;
1532 u32 data_len = 0;
1533 int i;
1534
1535 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1536 /* snapshots aren't writeable */
1537 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1538 } else {
1539 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1540 req->r_data_offset || req->r_snapc);
1541 }
1542
1543 setup_request_data(req, msg);
1544
8cb441c0
ID
1545 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1546 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
bb873b53
ID
1547 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1548 ceph_encode_32(&p, req->r_flags);
8cb441c0
ID
1549
1550 /* reqid */
1551 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1552 memset(p, 0, sizeof(struct ceph_osd_reqid));
1553 p += sizeof(struct ceph_osd_reqid);
1554
1555 /* trace */
1556 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1557 p += sizeof(struct ceph_blkin_trace_info);
1558
1559 ceph_encode_32(&p, 0); /* client_inc, always 0 */
bb873b53
ID
1560 ceph_encode_timespec(p, &req->r_mtime);
1561 p += sizeof(struct ceph_timespec);
aa26d662 1562
2e59ffd1 1563 encode_oloc(&p, end, &req->r_t.target_oloc);
2e59ffd1
ID
1564 ceph_encode_string(&p, end, req->r_t.target_oid.name,
1565 req->r_t.target_oid.name_len);
f24e9980 1566
bb873b53
ID
1567 /* ops, can imply data */
1568 ceph_encode_16(&p, req->r_num_ops);
1569 for (i = 0; i < req->r_num_ops; i++) {
1570 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1571 p += sizeof(struct ceph_osd_op);
1572 }
26be8808 1573
bb873b53
ID
1574 ceph_encode_64(&p, req->r_snapid); /* snapid */
1575 if (req->r_snapc) {
1576 ceph_encode_64(&p, req->r_snapc->seq);
1577 ceph_encode_32(&p, req->r_snapc->num_snaps);
1578 for (i = 0; i < req->r_snapc->num_snaps; i++)
1579 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1580 } else {
1581 ceph_encode_64(&p, 0); /* snap_seq */
1582 ceph_encode_32(&p, 0); /* snaps len */
1583 }
1584
1585 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
8cb441c0 1586 BUG_ON(p != end - 8); /* space for features */
bb873b53 1587
8cb441c0
ID
1588 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
1589 /* front_len is finalized in encode_request_finish() */
bb873b53
ID
1590 msg->hdr.data_len = cpu_to_le32(data_len);
1591 /*
1592 * The header "data_off" is a hint to the receiver allowing it
1593 * to align received data into its buffers such that there's no
1594 * need to re-copy it before writing it to disk (direct I/O).
1595 */
1596 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
26be8808 1597
8cb441c0
ID
1598 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
1599 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1600}
1601
1602static void encode_request_finish(struct ceph_msg *msg)
1603{
1604 void *p = msg->front.iov_base;
1605 void *const end = p + msg->front_alloc_len;
1606
1607 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
1608 /* luminous OSD -- encode features and be done */
1609 p = end - 8;
1610 ceph_encode_64(&p, msg->con->peer_features);
1611 } else {
1612 struct {
1613 char spgid[CEPH_ENCODING_START_BLK_LEN +
1614 CEPH_PGID_ENCODING_LEN + 1];
1615 __le32 hash;
1616 __le32 epoch;
1617 __le32 flags;
1618 char reqid[CEPH_ENCODING_START_BLK_LEN +
1619 sizeof(struct ceph_osd_reqid)];
1620 char trace[sizeof(struct ceph_blkin_trace_info)];
1621 __le32 client_inc;
1622 struct ceph_timespec mtime;
1623 } __packed head;
1624 struct ceph_pg pgid;
1625 void *oloc, *oid, *tail;
1626 int oloc_len, oid_len, tail_len;
1627 int len;
1628
1629 /*
1630 * Pre-luminous OSD -- reencode v8 into v4 using @head
1631 * as a temporary buffer. Encode the raw PG; the rest
1632 * is just a matter of moving oloc, oid and tail blobs
1633 * around.
1634 */
1635 memcpy(&head, p, sizeof(head));
1636 p += sizeof(head);
1637
1638 oloc = p;
1639 p += CEPH_ENCODING_START_BLK_LEN;
1640 pgid.pool = ceph_decode_64(&p);
1641 p += 4 + 4; /* preferred, key len */
1642 len = ceph_decode_32(&p);
1643 p += len; /* nspace */
1644 oloc_len = p - oloc;
1645
1646 oid = p;
1647 len = ceph_decode_32(&p);
1648 p += len;
1649 oid_len = p - oid;
1650
1651 tail = p;
1652 tail_len = (end - p) - 8;
1653
1654 p = msg->front.iov_base;
1655 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
1656 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
1657 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
1658 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
1659
1660 /* reassert_version */
1661 memset(p, 0, sizeof(struct ceph_eversion));
1662 p += sizeof(struct ceph_eversion);
1663
1664 BUG_ON(p >= oloc);
1665 memmove(p, oloc, oloc_len);
1666 p += oloc_len;
1667
1668 pgid.seed = le32_to_cpu(head.hash);
1669 encode_pgid(&p, &pgid); /* raw pg */
1670
1671 BUG_ON(p >= oid);
1672 memmove(p, oid, oid_len);
1673 p += oid_len;
1674
1675 /* tail -- ops, snapid, snapc, retry_attempt */
1676 BUG_ON(p >= tail);
1677 memmove(p, tail, tail_len);
1678 p += tail_len;
1679
1680 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1681 }
1682
1683 BUG_ON(p > end);
1684 msg->front.iov_len = p - msg->front.iov_base;
1685 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1686
1687 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
1688 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
1689 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
1690 le16_to_cpu(msg->hdr.version));
bb873b53
ID
1691}
1692
1693/*
1694 * @req has to be assigned a tid and registered.
1695 */
1696static void send_request(struct ceph_osd_request *req)
1697{
1698 struct ceph_osd *osd = req->r_osd;
1699
5aea3dcd 1700 verify_osd_locked(osd);
bb873b53
ID
1701 WARN_ON(osd->o_osd != req->r_t.osd);
1702
5aea3dcd
ID
1703 /*
1704 * We may have a previously queued request message hanging
1705 * around. Cancel it to avoid corrupting the msgr.
1706 */
1707 if (req->r_sent)
1708 ceph_msg_revoke(req->r_request);
1709
bb873b53
ID
1710 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1711 if (req->r_attempts)
1712 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1713 else
1714 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1715
8cb441c0 1716 encode_request_partial(req, req->r_request);
bb873b53 1717
04c7d789 1718 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 1719 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
dc98ff72 1720 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
04c7d789
ID
1721 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
1722 req->r_attempts);
bb873b53
ID
1723
1724 req->r_t.paused = false;
1725 req->r_stamp = jiffies;
1726 req->r_attempts++;
1727
1728 req->r_sent = osd->o_incarnation;
1729 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1730 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
f24e9980
SW
1731}
1732
42c1b124
ID
1733static void maybe_request_map(struct ceph_osd_client *osdc)
1734{
1735 bool continuous = false;
1736
5aea3dcd 1737 verify_osdc_locked(osdc);
42c1b124
ID
1738 WARN_ON(!osdc->osdmap->epoch);
1739
b7ec35b3
ID
1740 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1741 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
1742 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
42c1b124
ID
1743 dout("%s osdc %p continuous\n", __func__, osdc);
1744 continuous = true;
1745 } else {
1746 dout("%s osdc %p onetime\n", __func__, osdc);
1747 }
1748
1749 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
1750 osdc->osdmap->epoch + 1, continuous))
1751 ceph_monc_renew_subs(&osdc->client->monc);
1752}
1753
a1f4020a 1754static void complete_request(struct ceph_osd_request *req, int err);
4609245e
ID
1755static void send_map_check(struct ceph_osd_request *req);
1756
5aea3dcd 1757static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
0bbfdfe8 1758{
5aea3dcd
ID
1759 struct ceph_osd_client *osdc = req->r_osdc;
1760 struct ceph_osd *osd;
4609245e 1761 enum calc_target_result ct_res;
5aea3dcd
ID
1762 bool need_send = false;
1763 bool promoted = false;
a1f4020a 1764 bool need_abort = false;
0bbfdfe8 1765
b18b9550 1766 WARN_ON(req->r_tid);
5aea3dcd
ID
1767 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
1768
1769again:
7de030d6 1770 ct_res = calc_target(osdc, &req->r_t, NULL, false);
4609245e
ID
1771 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
1772 goto promote;
1773
5aea3dcd
ID
1774 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
1775 if (IS_ERR(osd)) {
1776 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
1777 goto promote;
0bbfdfe8
ID
1778 }
1779
58eb7932
JL
1780 if (osdc->osdmap->epoch < osdc->epoch_barrier) {
1781 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
1782 osdc->epoch_barrier);
1783 req->r_t.paused = true;
1784 maybe_request_map(osdc);
1785 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1786 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
5aea3dcd
ID
1787 dout("req %p pausewr\n", req);
1788 req->r_t.paused = true;
1789 maybe_request_map(osdc);
1790 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
b7ec35b3 1791 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
5aea3dcd
ID
1792 dout("req %p pauserd\n", req);
1793 req->r_t.paused = true;
1794 maybe_request_map(osdc);
1795 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1796 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
1797 CEPH_OSD_FLAG_FULL_FORCE)) &&
b7ec35b3 1798 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
5aea3dcd
ID
1799 pool_full(osdc, req->r_t.base_oloc.pool))) {
1800 dout("req %p full/pool_full\n", req);
1801 pr_warn_ratelimited("FULL or reached pool quota\n");
1802 req->r_t.paused = true;
1803 maybe_request_map(osdc);
a1f4020a
JL
1804 if (req->r_abort_on_full)
1805 need_abort = true;
5aea3dcd
ID
1806 } else if (!osd_homeless(osd)) {
1807 need_send = true;
0bbfdfe8 1808 } else {
5aea3dcd 1809 maybe_request_map(osdc);
0bbfdfe8
ID
1810 }
1811
5aea3dcd
ID
1812 mutex_lock(&osd->lock);
1813 /*
1814 * Assign the tid atomically with send_request() to protect
1815 * multiple writes to the same object from racing with each
1816 * other, resulting in out of order ops on the OSDs.
1817 */
1818 req->r_tid = atomic64_inc_return(&osdc->last_tid);
1819 link_request(osd, req);
1820 if (need_send)
1821 send_request(req);
a1f4020a
JL
1822 else if (need_abort)
1823 complete_request(req, -ENOSPC);
5aea3dcd
ID
1824 mutex_unlock(&osd->lock);
1825
4609245e
ID
1826 if (ct_res == CALC_TARGET_POOL_DNE)
1827 send_map_check(req);
1828
5aea3dcd
ID
1829 if (promoted)
1830 downgrade_write(&osdc->lock);
1831 return;
1832
1833promote:
1834 up_read(&osdc->lock);
1835 down_write(&osdc->lock);
1836 wrlocked = true;
1837 promoted = true;
1838 goto again;
1839}
1840
1841static void account_request(struct ceph_osd_request *req)
1842{
54ea0046 1843 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
b18b9550 1844 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
5aea3dcd 1845
b18b9550 1846 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
5aea3dcd 1847 atomic_inc(&req->r_osdc->num_requests);
7cc5e38f
ID
1848
1849 req->r_start_stamp = jiffies;
5aea3dcd
ID
1850}
1851
1852static void submit_request(struct ceph_osd_request *req, bool wrlocked)
1853{
1854 ceph_osdc_get_request(req);
1855 account_request(req);
1856 __submit_request(req, wrlocked);
1857}
1858
45ee2c1d 1859static void finish_request(struct ceph_osd_request *req)
5aea3dcd
ID
1860{
1861 struct ceph_osd_client *osdc = req->r_osdc;
5aea3dcd 1862
04c7d789 1863 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
5aea3dcd
ID
1864 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1865
04c7d789
ID
1866 if (req->r_osd)
1867 unlink_request(req->r_osd, req);
5aea3dcd
ID
1868 atomic_dec(&osdc->num_requests);
1869
1870 /*
1871 * If an OSD has failed or returned and a request has been sent
1872 * twice, it's possible to get a reply and end up here while the
1873 * request message is queued for delivery. We will ignore the
1874 * reply, so not a big deal, but better to try and catch it.
1875 */
1876 ceph_msg_revoke(req->r_request);
1877 ceph_msg_revoke_incoming(req->r_reply);
1878}
1879
fe5da05e
ID
1880static void __complete_request(struct ceph_osd_request *req)
1881{
b18b9550
ID
1882 if (req->r_callback) {
1883 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
1884 req->r_tid, req->r_callback, req->r_result);
fe5da05e 1885 req->r_callback(req);
b18b9550 1886 }
fe5da05e
ID
1887}
1888
4609245e 1889/*
b18b9550 1890 * This is open-coded in handle_reply().
4609245e
ID
1891 */
1892static void complete_request(struct ceph_osd_request *req, int err)
1893{
1894 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1895
1896 req->r_result = err;
45ee2c1d 1897 finish_request(req);
4609245e 1898 __complete_request(req);
b18b9550 1899 complete_all(&req->r_completion);
4609245e
ID
1900 ceph_osdc_put_request(req);
1901}
1902
1903static void cancel_map_check(struct ceph_osd_request *req)
1904{
1905 struct ceph_osd_client *osdc = req->r_osdc;
1906 struct ceph_osd_request *lookup_req;
1907
1908 verify_osdc_wrlocked(osdc);
1909
1910 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1911 if (!lookup_req)
1912 return;
1913
1914 WARN_ON(lookup_req != req);
1915 erase_request_mc(&osdc->map_checks, req);
1916 ceph_osdc_put_request(req);
1917}
1918
5aea3dcd
ID
1919static void cancel_request(struct ceph_osd_request *req)
1920{
1921 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1922
4609245e 1923 cancel_map_check(req);
45ee2c1d 1924 finish_request(req);
b18b9550 1925 complete_all(&req->r_completion);
c297eb42 1926 ceph_osdc_put_request(req);
5aea3dcd
ID
1927}
1928
7cc5e38f
ID
1929static void abort_request(struct ceph_osd_request *req, int err)
1930{
1931 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1932
1933 cancel_map_check(req);
1934 complete_request(req, err);
1935}
1936
58eb7932
JL
1937static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
1938{
1939 if (likely(eb > osdc->epoch_barrier)) {
1940 dout("updating epoch_barrier from %u to %u\n",
1941 osdc->epoch_barrier, eb);
1942 osdc->epoch_barrier = eb;
1943 /* Request map if we're not to the barrier yet */
1944 if (eb > osdc->osdmap->epoch)
1945 maybe_request_map(osdc);
1946 }
1947}
1948
1949void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
1950{
1951 down_read(&osdc->lock);
1952 if (unlikely(eb > osdc->epoch_barrier)) {
1953 up_read(&osdc->lock);
1954 down_write(&osdc->lock);
1955 update_epoch_barrier(osdc, eb);
1956 up_write(&osdc->lock);
1957 } else {
1958 up_read(&osdc->lock);
1959 }
1960}
1961EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
1962
fc36d0a4
JL
1963/*
1964 * Drop all pending requests that are stalled waiting on a full condition to
58eb7932
JL
1965 * clear, and complete them with ENOSPC as the return code. Set the
1966 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
1967 * cancelled.
fc36d0a4
JL
1968 */
1969static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
1970{
1971 struct rb_node *n;
58eb7932 1972 bool victims = false;
fc36d0a4
JL
1973
1974 dout("enter abort_on_full\n");
1975
1976 if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
1977 goto out;
1978
58eb7932
JL
1979 /* Scan list and see if there is anything to abort */
1980 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1981 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1982 struct rb_node *m;
1983
1984 m = rb_first(&osd->o_requests);
1985 while (m) {
1986 struct ceph_osd_request *req = rb_entry(m,
1987 struct ceph_osd_request, r_node);
1988 m = rb_next(m);
1989
1990 if (req->r_abort_on_full) {
1991 victims = true;
1992 break;
1993 }
1994 }
1995 if (victims)
1996 break;
1997 }
1998
1999 if (!victims)
2000 goto out;
2001
2002 /*
2003 * Update the barrier to current epoch if it's behind that point,
2004 * since we know we have some calls to be aborted in the tree.
2005 */
2006 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2007
fc36d0a4
JL
2008 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2009 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2010 struct rb_node *m;
2011
2012 m = rb_first(&osd->o_requests);
2013 while (m) {
2014 struct ceph_osd_request *req = rb_entry(m,
2015 struct ceph_osd_request, r_node);
2016 m = rb_next(m);
2017
2018 if (req->r_abort_on_full &&
2019 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2020 pool_full(osdc, req->r_t.target_oloc.pool)))
2021 abort_request(req, -ENOSPC);
2022 }
2023 }
2024out:
58eb7932 2025 dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
fc36d0a4
JL
2026}
2027
4609245e
ID
2028static void check_pool_dne(struct ceph_osd_request *req)
2029{
2030 struct ceph_osd_client *osdc = req->r_osdc;
2031 struct ceph_osdmap *map = osdc->osdmap;
2032
2033 verify_osdc_wrlocked(osdc);
2034 WARN_ON(!map->epoch);
2035
2036 if (req->r_attempts) {
2037 /*
2038 * We sent a request earlier, which means that
2039 * previously the pool existed, and now it does not
2040 * (i.e., it was deleted).
2041 */
2042 req->r_map_dne_bound = map->epoch;
2043 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2044 req->r_tid);
2045 } else {
2046 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2047 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2048 }
2049
2050 if (req->r_map_dne_bound) {
2051 if (map->epoch >= req->r_map_dne_bound) {
2052 /* we had a new enough map */
2053 pr_info_ratelimited("tid %llu pool does not exist\n",
2054 req->r_tid);
2055 complete_request(req, -ENOENT);
2056 }
2057 } else {
2058 send_map_check(req);
2059 }
2060}
2061
2062static void map_check_cb(struct ceph_mon_generic_request *greq)
2063{
2064 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2065 struct ceph_osd_request *req;
2066 u64 tid = greq->private_data;
2067
2068 WARN_ON(greq->result || !greq->u.newest);
2069
2070 down_write(&osdc->lock);
2071 req = lookup_request_mc(&osdc->map_checks, tid);
2072 if (!req) {
2073 dout("%s tid %llu dne\n", __func__, tid);
2074 goto out_unlock;
2075 }
2076
2077 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2078 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2079 if (!req->r_map_dne_bound)
2080 req->r_map_dne_bound = greq->u.newest;
2081 erase_request_mc(&osdc->map_checks, req);
2082 check_pool_dne(req);
2083
2084 ceph_osdc_put_request(req);
2085out_unlock:
2086 up_write(&osdc->lock);
2087}
2088
2089static void send_map_check(struct ceph_osd_request *req)
2090{
2091 struct ceph_osd_client *osdc = req->r_osdc;
2092 struct ceph_osd_request *lookup_req;
2093 int ret;
2094
2095 verify_osdc_wrlocked(osdc);
2096
2097 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2098 if (lookup_req) {
2099 WARN_ON(lookup_req != req);
2100 return;
2101 }
2102
2103 ceph_osdc_get_request(req);
2104 insert_request_mc(&osdc->map_checks, req);
2105 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2106 map_check_cb, req->r_tid);
2107 WARN_ON(ret);
2108}
2109
922dab61
ID
2110/*
2111 * lingering requests, watch/notify v2 infrastructure
2112 */
2113static void linger_release(struct kref *kref)
2114{
2115 struct ceph_osd_linger_request *lreq =
2116 container_of(kref, struct ceph_osd_linger_request, kref);
2117
2118 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2119 lreq->reg_req, lreq->ping_req);
2120 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2121 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
4609245e 2122 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
922dab61 2123 WARN_ON(!list_empty(&lreq->scan_item));
b07d3c4b 2124 WARN_ON(!list_empty(&lreq->pending_lworks));
922dab61
ID
2125 WARN_ON(lreq->osd);
2126
2127 if (lreq->reg_req)
2128 ceph_osdc_put_request(lreq->reg_req);
2129 if (lreq->ping_req)
2130 ceph_osdc_put_request(lreq->ping_req);
2131 target_destroy(&lreq->t);
2132 kfree(lreq);
2133}
2134
2135static void linger_put(struct ceph_osd_linger_request *lreq)
2136{
2137 if (lreq)
2138 kref_put(&lreq->kref, linger_release);
2139}
2140
2141static struct ceph_osd_linger_request *
2142linger_get(struct ceph_osd_linger_request *lreq)
2143{
2144 kref_get(&lreq->kref);
2145 return lreq;
2146}
2147
2148static struct ceph_osd_linger_request *
2149linger_alloc(struct ceph_osd_client *osdc)
2150{
2151 struct ceph_osd_linger_request *lreq;
2152
2153 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2154 if (!lreq)
2155 return NULL;
2156
2157 kref_init(&lreq->kref);
2158 mutex_init(&lreq->lock);
2159 RB_CLEAR_NODE(&lreq->node);
2160 RB_CLEAR_NODE(&lreq->osdc_node);
4609245e 2161 RB_CLEAR_NODE(&lreq->mc_node);
922dab61 2162 INIT_LIST_HEAD(&lreq->scan_item);
b07d3c4b 2163 INIT_LIST_HEAD(&lreq->pending_lworks);
922dab61 2164 init_completion(&lreq->reg_commit_wait);
19079203 2165 init_completion(&lreq->notify_finish_wait);
922dab61
ID
2166
2167 lreq->osdc = osdc;
2168 target_init(&lreq->t);
2169
2170 dout("%s lreq %p\n", __func__, lreq);
2171 return lreq;
2172}
2173
2174DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2175DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
4609245e 2176DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
922dab61
ID
2177
2178/*
2179 * Create linger request <-> OSD session relation.
2180 *
2181 * @lreq has to be registered, @osd may be homeless.
2182 */
2183static void link_linger(struct ceph_osd *osd,
2184 struct ceph_osd_linger_request *lreq)
2185{
2186 verify_osd_locked(osd);
2187 WARN_ON(!lreq->linger_id || lreq->osd);
2188 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2189 osd->o_osd, lreq, lreq->linger_id);
2190
2191 if (!osd_homeless(osd))
2192 __remove_osd_from_lru(osd);
2193 else
2194 atomic_inc(&osd->o_osdc->num_homeless);
2195
2196 get_osd(osd);
2197 insert_linger(&osd->o_linger_requests, lreq);
2198 lreq->osd = osd;
2199}
2200
2201static void unlink_linger(struct ceph_osd *osd,
2202 struct ceph_osd_linger_request *lreq)
2203{
2204 verify_osd_locked(osd);
2205 WARN_ON(lreq->osd != osd);
2206 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2207 osd->o_osd, lreq, lreq->linger_id);
2208
2209 lreq->osd = NULL;
2210 erase_linger(&osd->o_linger_requests, lreq);
2211 put_osd(osd);
2212
2213 if (!osd_homeless(osd))
2214 maybe_move_osd_to_lru(osd);
2215 else
2216 atomic_dec(&osd->o_osdc->num_homeless);
2217}
2218
2219static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2220{
2221 verify_osdc_locked(lreq->osdc);
2222
2223 return !RB_EMPTY_NODE(&lreq->osdc_node);
2224}
2225
2226static bool linger_registered(struct ceph_osd_linger_request *lreq)
2227{
2228 struct ceph_osd_client *osdc = lreq->osdc;
2229 bool registered;
2230
2231 down_read(&osdc->lock);
2232 registered = __linger_registered(lreq);
2233 up_read(&osdc->lock);
2234
2235 return registered;
2236}
2237
2238static void linger_register(struct ceph_osd_linger_request *lreq)
2239{
2240 struct ceph_osd_client *osdc = lreq->osdc;
2241
2242 verify_osdc_wrlocked(osdc);
2243 WARN_ON(lreq->linger_id);
2244
2245 linger_get(lreq);
2246 lreq->linger_id = ++osdc->last_linger_id;
2247 insert_linger_osdc(&osdc->linger_requests, lreq);
2248}
2249
2250static void linger_unregister(struct ceph_osd_linger_request *lreq)
2251{
2252 struct ceph_osd_client *osdc = lreq->osdc;
2253
2254 verify_osdc_wrlocked(osdc);
2255
2256 erase_linger_osdc(&osdc->linger_requests, lreq);
2257 linger_put(lreq);
2258}
2259
2260static void cancel_linger_request(struct ceph_osd_request *req)
2261{
2262 struct ceph_osd_linger_request *lreq = req->r_priv;
2263
2264 WARN_ON(!req->r_linger);
2265 cancel_request(req);
2266 linger_put(lreq);
2267}
2268
2269struct linger_work {
2270 struct work_struct work;
2271 struct ceph_osd_linger_request *lreq;
b07d3c4b
ID
2272 struct list_head pending_item;
2273 unsigned long queued_stamp;
922dab61
ID
2274
2275 union {
2276 struct {
2277 u64 notify_id;
2278 u64 notifier_id;
2279 void *payload; /* points into @msg front */
2280 size_t payload_len;
2281
2282 struct ceph_msg *msg; /* for ceph_msg_put() */
2283 } notify;
2284 struct {
2285 int err;
2286 } error;
2287 };
2288};
2289
2290static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2291 work_func_t workfn)
2292{
2293 struct linger_work *lwork;
2294
2295 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2296 if (!lwork)
2297 return NULL;
2298
2299 INIT_WORK(&lwork->work, workfn);
b07d3c4b 2300 INIT_LIST_HEAD(&lwork->pending_item);
922dab61
ID
2301 lwork->lreq = linger_get(lreq);
2302
2303 return lwork;
2304}
2305
2306static void lwork_free(struct linger_work *lwork)
2307{
2308 struct ceph_osd_linger_request *lreq = lwork->lreq;
2309
b07d3c4b
ID
2310 mutex_lock(&lreq->lock);
2311 list_del(&lwork->pending_item);
2312 mutex_unlock(&lreq->lock);
2313
922dab61
ID
2314 linger_put(lreq);
2315 kfree(lwork);
2316}
2317
2318static void lwork_queue(struct linger_work *lwork)
2319{
2320 struct ceph_osd_linger_request *lreq = lwork->lreq;
2321 struct ceph_osd_client *osdc = lreq->osdc;
2322
2323 verify_lreq_locked(lreq);
b07d3c4b
ID
2324 WARN_ON(!list_empty(&lwork->pending_item));
2325
2326 lwork->queued_stamp = jiffies;
2327 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
922dab61
ID
2328 queue_work(osdc->notify_wq, &lwork->work);
2329}
2330
2331static void do_watch_notify(struct work_struct *w)
2332{
2333 struct linger_work *lwork = container_of(w, struct linger_work, work);
2334 struct ceph_osd_linger_request *lreq = lwork->lreq;
2335
2336 if (!linger_registered(lreq)) {
2337 dout("%s lreq %p not registered\n", __func__, lreq);
2338 goto out;
2339 }
2340
19079203 2341 WARN_ON(!lreq->is_watch);
922dab61
ID
2342 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2343 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2344 lwork->notify.payload_len);
2345 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2346 lwork->notify.notifier_id, lwork->notify.payload,
2347 lwork->notify.payload_len);
2348
2349out:
2350 ceph_msg_put(lwork->notify.msg);
2351 lwork_free(lwork);
2352}
2353
2354static void do_watch_error(struct work_struct *w)
2355{
2356 struct linger_work *lwork = container_of(w, struct linger_work, work);
2357 struct ceph_osd_linger_request *lreq = lwork->lreq;
2358
2359 if (!linger_registered(lreq)) {
2360 dout("%s lreq %p not registered\n", __func__, lreq);
2361 goto out;
2362 }
2363
2364 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2365 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2366
2367out:
2368 lwork_free(lwork);
2369}
2370
2371static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2372{
2373 struct linger_work *lwork;
2374
2375 lwork = lwork_alloc(lreq, do_watch_error);
2376 if (!lwork) {
2377 pr_err("failed to allocate error-lwork\n");
2378 return;
2379 }
2380
2381 lwork->error.err = lreq->last_error;
2382 lwork_queue(lwork);
2383}
2384
2385static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2386 int result)
2387{
2388 if (!completion_done(&lreq->reg_commit_wait)) {
2389 lreq->reg_commit_error = (result <= 0 ? result : 0);
2390 complete_all(&lreq->reg_commit_wait);
2391 }
2392}
2393
2394static void linger_commit_cb(struct ceph_osd_request *req)
2395{
2396 struct ceph_osd_linger_request *lreq = req->r_priv;
2397
2398 mutex_lock(&lreq->lock);
2399 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2400 lreq->linger_id, req->r_result);
922dab61
ID
2401 linger_reg_commit_complete(lreq, req->r_result);
2402 lreq->committed = true;
2403
19079203
ID
2404 if (!lreq->is_watch) {
2405 struct ceph_osd_data *osd_data =
2406 osd_req_op_data(req, 0, notify, response_data);
2407 void *p = page_address(osd_data->pages[0]);
2408
2409 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2410 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2411
2412 /* make note of the notify_id */
2413 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2414 lreq->notify_id = ceph_decode_64(&p);
2415 dout("lreq %p notify_id %llu\n", lreq,
2416 lreq->notify_id);
2417 } else {
2418 dout("lreq %p no notify_id\n", lreq);
2419 }
2420 }
2421
922dab61
ID
2422 mutex_unlock(&lreq->lock);
2423 linger_put(lreq);
2424}
2425
2426static int normalize_watch_error(int err)
2427{
2428 /*
2429 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2430 * notification and a failure to reconnect because we raced with
2431 * the delete appear the same to the user.
2432 */
2433 if (err == -ENOENT)
2434 err = -ENOTCONN;
2435
2436 return err;
2437}
2438
2439static void linger_reconnect_cb(struct ceph_osd_request *req)
2440{
2441 struct ceph_osd_linger_request *lreq = req->r_priv;
2442
2443 mutex_lock(&lreq->lock);
2444 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2445 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2446 if (req->r_result < 0) {
2447 if (!lreq->last_error) {
2448 lreq->last_error = normalize_watch_error(req->r_result);
2449 queue_watch_error(lreq);
2450 }
2451 }
2452
2453 mutex_unlock(&lreq->lock);
2454 linger_put(lreq);
2455}
2456
2457static void send_linger(struct ceph_osd_linger_request *lreq)
2458{
2459 struct ceph_osd_request *req = lreq->reg_req;
2460 struct ceph_osd_req_op *op = &req->r_ops[0];
2461
2462 verify_osdc_wrlocked(req->r_osdc);
2463 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2464
2465 if (req->r_osd)
2466 cancel_linger_request(req);
2467
2468 request_reinit(req);
2469 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2470 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2471 req->r_flags = lreq->t.flags;
2472 req->r_mtime = lreq->mtime;
2473
2474 mutex_lock(&lreq->lock);
19079203 2475 if (lreq->is_watch && lreq->committed) {
922dab61
ID
2476 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2477 op->watch.cookie != lreq->linger_id);
2478 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2479 op->watch.gen = ++lreq->register_gen;
2480 dout("lreq %p reconnect register_gen %u\n", lreq,
2481 op->watch.gen);
2482 req->r_callback = linger_reconnect_cb;
2483 } else {
19079203
ID
2484 if (!lreq->is_watch)
2485 lreq->notify_id = 0;
2486 else
2487 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
922dab61
ID
2488 dout("lreq %p register\n", lreq);
2489 req->r_callback = linger_commit_cb;
2490 }
2491 mutex_unlock(&lreq->lock);
2492
2493 req->r_priv = linger_get(lreq);
2494 req->r_linger = true;
2495
2496 submit_request(req, true);
2497}
2498
2499static void linger_ping_cb(struct ceph_osd_request *req)
2500{
2501 struct ceph_osd_linger_request *lreq = req->r_priv;
2502
2503 mutex_lock(&lreq->lock);
2504 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2505 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2506 lreq->last_error);
2507 if (lreq->register_gen == req->r_ops[0].watch.gen) {
b07d3c4b
ID
2508 if (!req->r_result) {
2509 lreq->watch_valid_thru = lreq->ping_sent;
2510 } else if (!lreq->last_error) {
922dab61
ID
2511 lreq->last_error = normalize_watch_error(req->r_result);
2512 queue_watch_error(lreq);
2513 }
2514 } else {
2515 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2516 lreq->register_gen, req->r_ops[0].watch.gen);
2517 }
2518
2519 mutex_unlock(&lreq->lock);
2520 linger_put(lreq);
2521}
2522
2523static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2524{
2525 struct ceph_osd_client *osdc = lreq->osdc;
2526 struct ceph_osd_request *req = lreq->ping_req;
2527 struct ceph_osd_req_op *op = &req->r_ops[0];
2528
b7ec35b3 2529 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
922dab61
ID
2530 dout("%s PAUSERD\n", __func__);
2531 return;
2532 }
2533
2534 lreq->ping_sent = jiffies;
2535 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2536 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2537 lreq->register_gen);
2538
2539 if (req->r_osd)
2540 cancel_linger_request(req);
2541
2542 request_reinit(req);
2543 target_copy(&req->r_t, &lreq->t);
2544
2545 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2546 op->watch.cookie != lreq->linger_id ||
2547 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2548 op->watch.gen = lreq->register_gen;
2549 req->r_callback = linger_ping_cb;
2550 req->r_priv = linger_get(lreq);
2551 req->r_linger = true;
2552
2553 ceph_osdc_get_request(req);
2554 account_request(req);
2555 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2556 link_request(lreq->osd, req);
2557 send_request(req);
2558}
2559
2560static void linger_submit(struct ceph_osd_linger_request *lreq)
2561{
2562 struct ceph_osd_client *osdc = lreq->osdc;
2563 struct ceph_osd *osd;
2564
7de030d6 2565 calc_target(osdc, &lreq->t, NULL, false);
922dab61
ID
2566 osd = lookup_create_osd(osdc, lreq->t.osd, true);
2567 link_linger(osd, lreq);
2568
2569 send_linger(lreq);
2570}
2571
4609245e
ID
2572static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2573{
2574 struct ceph_osd_client *osdc = lreq->osdc;
2575 struct ceph_osd_linger_request *lookup_lreq;
2576
2577 verify_osdc_wrlocked(osdc);
2578
2579 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2580 lreq->linger_id);
2581 if (!lookup_lreq)
2582 return;
2583
2584 WARN_ON(lookup_lreq != lreq);
2585 erase_linger_mc(&osdc->linger_map_checks, lreq);
2586 linger_put(lreq);
2587}
2588
922dab61
ID
2589/*
2590 * @lreq has to be both registered and linked.
2591 */
2592static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2593{
19079203 2594 if (lreq->is_watch && lreq->ping_req->r_osd)
922dab61
ID
2595 cancel_linger_request(lreq->ping_req);
2596 if (lreq->reg_req->r_osd)
2597 cancel_linger_request(lreq->reg_req);
4609245e 2598 cancel_linger_map_check(lreq);
922dab61
ID
2599 unlink_linger(lreq->osd, lreq);
2600 linger_unregister(lreq);
2601}
2602
2603static void linger_cancel(struct ceph_osd_linger_request *lreq)
2604{
2605 struct ceph_osd_client *osdc = lreq->osdc;
2606
2607 down_write(&osdc->lock);
2608 if (__linger_registered(lreq))
2609 __linger_cancel(lreq);
2610 up_write(&osdc->lock);
2611}
2612
4609245e
ID
2613static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2614
2615static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2616{
2617 struct ceph_osd_client *osdc = lreq->osdc;
2618 struct ceph_osdmap *map = osdc->osdmap;
2619
2620 verify_osdc_wrlocked(osdc);
2621 WARN_ON(!map->epoch);
2622
2623 if (lreq->register_gen) {
2624 lreq->map_dne_bound = map->epoch;
2625 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2626 lreq, lreq->linger_id);
2627 } else {
2628 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2629 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2630 map->epoch);
2631 }
2632
2633 if (lreq->map_dne_bound) {
2634 if (map->epoch >= lreq->map_dne_bound) {
2635 /* we had a new enough map */
2636 pr_info("linger_id %llu pool does not exist\n",
2637 lreq->linger_id);
2638 linger_reg_commit_complete(lreq, -ENOENT);
2639 __linger_cancel(lreq);
2640 }
2641 } else {
2642 send_linger_map_check(lreq);
2643 }
2644}
2645
2646static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2647{
2648 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2649 struct ceph_osd_linger_request *lreq;
2650 u64 linger_id = greq->private_data;
2651
2652 WARN_ON(greq->result || !greq->u.newest);
2653
2654 down_write(&osdc->lock);
2655 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2656 if (!lreq) {
2657 dout("%s linger_id %llu dne\n", __func__, linger_id);
2658 goto out_unlock;
2659 }
2660
2661 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
2662 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2663 greq->u.newest);
2664 if (!lreq->map_dne_bound)
2665 lreq->map_dne_bound = greq->u.newest;
2666 erase_linger_mc(&osdc->linger_map_checks, lreq);
2667 check_linger_pool_dne(lreq);
2668
2669 linger_put(lreq);
2670out_unlock:
2671 up_write(&osdc->lock);
2672}
2673
2674static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
2675{
2676 struct ceph_osd_client *osdc = lreq->osdc;
2677 struct ceph_osd_linger_request *lookup_lreq;
2678 int ret;
2679
2680 verify_osdc_wrlocked(osdc);
2681
2682 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2683 lreq->linger_id);
2684 if (lookup_lreq) {
2685 WARN_ON(lookup_lreq != lreq);
2686 return;
2687 }
2688
2689 linger_get(lreq);
2690 insert_linger_mc(&osdc->linger_map_checks, lreq);
2691 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2692 linger_map_check_cb, lreq->linger_id);
2693 WARN_ON(ret);
2694}
2695
922dab61
ID
2696static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
2697{
2698 int ret;
2699
2700 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2701 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
2702 return ret ?: lreq->reg_commit_error;
2703}
2704
19079203
ID
2705static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
2706{
2707 int ret;
2708
2709 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2710 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
2711 return ret ?: lreq->notify_finish_error;
2712}
2713
f24e9980 2714/*
fbca9635
ID
2715 * Timeout callback, called every N seconds. When 1 or more OSD
2716 * requests has been active for more than N seconds, we send a keepalive
2717 * (tag + timestamp) to its OSD to ensure any communications channel
2718 * reset is detected.
f24e9980
SW
2719 */
2720static void handle_timeout(struct work_struct *work)
2721{
2722 struct ceph_osd_client *osdc =
2723 container_of(work, struct ceph_osd_client, timeout_work.work);
a319bf56 2724 struct ceph_options *opts = osdc->client->options;
5aea3dcd 2725 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
7cc5e38f 2726 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
5aea3dcd
ID
2727 LIST_HEAD(slow_osds);
2728 struct rb_node *n, *p;
f24e9980 2729
5aea3dcd
ID
2730 dout("%s osdc %p\n", __func__, osdc);
2731 down_write(&osdc->lock);
f24e9980 2732
422d2cb8
YS
2733 /*
2734 * ping osds that are a bit slow. this ensures that if there
2735 * is a break in the TCP connection we will notice, and reopen
2736 * a connection with that osd (from the fault callback).
2737 */
5aea3dcd
ID
2738 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2739 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2740 bool found = false;
2741
7cc5e38f 2742 for (p = rb_first(&osd->o_requests); p; ) {
5aea3dcd
ID
2743 struct ceph_osd_request *req =
2744 rb_entry(p, struct ceph_osd_request, r_node);
2745
7cc5e38f
ID
2746 p = rb_next(p); /* abort_request() */
2747
5aea3dcd
ID
2748 if (time_before(req->r_stamp, cutoff)) {
2749 dout(" req %p tid %llu on osd%d is laggy\n",
2750 req, req->r_tid, osd->o_osd);
2751 found = true;
2752 }
7cc5e38f
ID
2753 if (opts->osd_request_timeout &&
2754 time_before(req->r_start_stamp, expiry_cutoff)) {
2755 pr_err_ratelimited("tid %llu on osd%d timeout\n",
2756 req->r_tid, osd->o_osd);
2757 abort_request(req, -ETIMEDOUT);
2758 }
5aea3dcd 2759 }
922dab61
ID
2760 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
2761 struct ceph_osd_linger_request *lreq =
2762 rb_entry(p, struct ceph_osd_linger_request, node);
2763
2764 dout(" lreq %p linger_id %llu is served by osd%d\n",
2765 lreq, lreq->linger_id, osd->o_osd);
2766 found = true;
2767
2768 mutex_lock(&lreq->lock);
19079203 2769 if (lreq->is_watch && lreq->committed && !lreq->last_error)
922dab61
ID
2770 send_linger_ping(lreq);
2771 mutex_unlock(&lreq->lock);
2772 }
422d2cb8 2773
5aea3dcd
ID
2774 if (found)
2775 list_move_tail(&osd->o_keepalive_item, &slow_osds);
422d2cb8 2776 }
5aea3dcd 2777
7cc5e38f
ID
2778 if (opts->osd_request_timeout) {
2779 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
2780 struct ceph_osd_request *req =
2781 rb_entry(p, struct ceph_osd_request, r_node);
2782
2783 p = rb_next(p); /* abort_request() */
2784
2785 if (time_before(req->r_start_stamp, expiry_cutoff)) {
2786 pr_err_ratelimited("tid %llu on osd%d timeout\n",
2787 req->r_tid, osdc->homeless_osd.o_osd);
2788 abort_request(req, -ETIMEDOUT);
2789 }
2790 }
2791 }
2792
5aea3dcd
ID
2793 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
2794 maybe_request_map(osdc);
2795
422d2cb8 2796 while (!list_empty(&slow_osds)) {
5aea3dcd
ID
2797 struct ceph_osd *osd = list_first_entry(&slow_osds,
2798 struct ceph_osd,
2799 o_keepalive_item);
422d2cb8 2800 list_del_init(&osd->o_keepalive_item);
f24e9980
SW
2801 ceph_con_keepalive(&osd->o_con);
2802 }
2803
5aea3dcd 2804 up_write(&osdc->lock);
fbca9635
ID
2805 schedule_delayed_work(&osdc->timeout_work,
2806 osdc->client->options->osd_keepalive_timeout);
f24e9980
SW
2807}
2808
f5a2041b
YS
2809static void handle_osds_timeout(struct work_struct *work)
2810{
2811 struct ceph_osd_client *osdc =
2812 container_of(work, struct ceph_osd_client,
2813 osds_timeout_work.work);
a319bf56 2814 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
42a2c09f 2815 struct ceph_osd *osd, *nosd;
f5a2041b 2816
42a2c09f 2817 dout("%s osdc %p\n", __func__, osdc);
5aea3dcd 2818 down_write(&osdc->lock);
42a2c09f
ID
2819 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
2820 if (time_before(jiffies, osd->lru_ttl))
2821 break;
2822
5aea3dcd 2823 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
922dab61 2824 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
5aea3dcd 2825 close_osd(osd);
42a2c09f 2826 }
f5a2041b 2827
5aea3dcd 2828 up_write(&osdc->lock);
f5a2041b
YS
2829 schedule_delayed_work(&osdc->osds_timeout_work,
2830 round_jiffies_relative(delay));
2831}
2832
205ee118
ID
2833static int ceph_oloc_decode(void **p, void *end,
2834 struct ceph_object_locator *oloc)
2835{
2836 u8 struct_v, struct_cv;
2837 u32 len;
2838 void *struct_end;
2839 int ret = 0;
2840
2841 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2842 struct_v = ceph_decode_8(p);
2843 struct_cv = ceph_decode_8(p);
2844 if (struct_v < 3) {
2845 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
2846 struct_v, struct_cv);
2847 goto e_inval;
2848 }
2849 if (struct_cv > 6) {
2850 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
2851 struct_v, struct_cv);
2852 goto e_inval;
2853 }
2854 len = ceph_decode_32(p);
2855 ceph_decode_need(p, end, len, e_inval);
2856 struct_end = *p + len;
2857
2858 oloc->pool = ceph_decode_64(p);
2859 *p += 4; /* skip preferred */
2860
2861 len = ceph_decode_32(p);
2862 if (len > 0) {
2863 pr_warn("ceph_object_locator::key is set\n");
2864 goto e_inval;
2865 }
2866
2867 if (struct_v >= 5) {
cd08e0a2
YZ
2868 bool changed = false;
2869
205ee118
ID
2870 len = ceph_decode_32(p);
2871 if (len > 0) {
30c156d9 2872 ceph_decode_need(p, end, len, e_inval);
cd08e0a2
YZ
2873 if (!oloc->pool_ns ||
2874 ceph_compare_string(oloc->pool_ns, *p, len))
2875 changed = true;
30c156d9 2876 *p += len;
cd08e0a2
YZ
2877 } else {
2878 if (oloc->pool_ns)
2879 changed = true;
2880 }
2881 if (changed) {
2882 /* redirect changes namespace */
2883 pr_warn("ceph_object_locator::nspace is changed\n");
2884 goto e_inval;
205ee118
ID
2885 }
2886 }
2887
2888 if (struct_v >= 6) {
2889 s64 hash = ceph_decode_64(p);
2890 if (hash != -1) {
2891 pr_warn("ceph_object_locator::hash is set\n");
2892 goto e_inval;
2893 }
2894 }
2895
2896 /* skip the rest */
2897 *p = struct_end;
2898out:
2899 return ret;
2900
2901e_inval:
2902 ret = -EINVAL;
2903 goto out;
2904}
2905
2906static int ceph_redirect_decode(void **p, void *end,
2907 struct ceph_request_redirect *redir)
2908{
2909 u8 struct_v, struct_cv;
2910 u32 len;
2911 void *struct_end;
2912 int ret;
2913
2914 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2915 struct_v = ceph_decode_8(p);
2916 struct_cv = ceph_decode_8(p);
2917 if (struct_cv > 1) {
2918 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
2919 struct_v, struct_cv);
2920 goto e_inval;
2921 }
2922 len = ceph_decode_32(p);
2923 ceph_decode_need(p, end, len, e_inval);
2924 struct_end = *p + len;
2925
2926 ret = ceph_oloc_decode(p, end, &redir->oloc);
2927 if (ret)
2928 goto out;
2929
2930 len = ceph_decode_32(p);
2931 if (len > 0) {
2932 pr_warn("ceph_request_redirect::object_name is set\n");
2933 goto e_inval;
2934 }
2935
2936 len = ceph_decode_32(p);
2937 *p += len; /* skip osd_instructions */
2938
2939 /* skip the rest */
2940 *p = struct_end;
2941out:
2942 return ret;
2943
2944e_inval:
2945 ret = -EINVAL;
2946 goto out;
2947}
2948
fe5da05e
ID
2949struct MOSDOpReply {
2950 struct ceph_pg pgid;
2951 u64 flags;
2952 int result;
2953 u32 epoch;
2954 int num_ops;
2955 u32 outdata_len[CEPH_OSD_MAX_OPS];
2956 s32 rval[CEPH_OSD_MAX_OPS];
2957 int retry_attempt;
2958 struct ceph_eversion replay_version;
2959 u64 user_version;
2960 struct ceph_request_redirect redirect;
2961};
25845472 2962
fe5da05e 2963static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
f24e9980 2964{
fe5da05e
ID
2965 void *p = msg->front.iov_base;
2966 void *const end = p + msg->front.iov_len;
2967 u16 version = le16_to_cpu(msg->hdr.version);
2968 struct ceph_eversion bad_replay_version;
b0b31a8f 2969 u8 decode_redir;
fe5da05e
ID
2970 u32 len;
2971 int ret;
2972 int i;
1b83bef2 2973
fe5da05e
ID
2974 ceph_decode_32_safe(&p, end, len, e_inval);
2975 ceph_decode_need(&p, end, len, e_inval);
2976 p += len; /* skip oid */
1b83bef2 2977
fe5da05e
ID
2978 ret = ceph_decode_pgid(&p, end, &m->pgid);
2979 if (ret)
2980 return ret;
1b83bef2 2981
fe5da05e
ID
2982 ceph_decode_64_safe(&p, end, m->flags, e_inval);
2983 ceph_decode_32_safe(&p, end, m->result, e_inval);
2984 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
2985 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
2986 p += sizeof(bad_replay_version);
2987 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
1b83bef2 2988
fe5da05e
ID
2989 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
2990 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
2991 goto e_inval;
1b83bef2 2992
fe5da05e
ID
2993 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
2994 e_inval);
2995 for (i = 0; i < m->num_ops; i++) {
1b83bef2 2996 struct ceph_osd_op *op = p;
1b83bef2 2997
fe5da05e 2998 m->outdata_len[i] = le32_to_cpu(op->payload_len);
1b83bef2
SW
2999 p += sizeof(*op);
3000 }
1b83bef2 3001
fe5da05e
ID
3002 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3003 for (i = 0; i < m->num_ops; i++)
3004 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
f24e9980 3005
fe5da05e
ID
3006 if (version >= 5) {
3007 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3008 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3009 p += sizeof(m->replay_version);
3010 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3011 } else {
3012 m->replay_version = bad_replay_version; /* struct */
3013 m->user_version = le64_to_cpu(m->replay_version.version);
3014 }
eb845ff1 3015
fe5da05e
ID
3016 if (version >= 6) {
3017 if (version >= 7)
3018 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
b0b31a8f
ID
3019 else
3020 decode_redir = 1;
3021 } else {
3022 decode_redir = 0;
3023 }
3024
3025 if (decode_redir) {
fe5da05e
ID
3026 ret = ceph_redirect_decode(&p, end, &m->redirect);
3027 if (ret)
3028 return ret;
205ee118 3029 } else {
fe5da05e 3030 ceph_oloc_init(&m->redirect.oloc);
205ee118 3031 }
f24e9980 3032
fe5da05e
ID
3033 return 0;
3034
3035e_inval:
3036 return -EINVAL;
3037}
3038
3039/*
b18b9550
ID
3040 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3041 * specified.
fe5da05e 3042 */
5aea3dcd 3043static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
fe5da05e 3044{
5aea3dcd 3045 struct ceph_osd_client *osdc = osd->o_osdc;
fe5da05e
ID
3046 struct ceph_osd_request *req;
3047 struct MOSDOpReply m;
3048 u64 tid = le64_to_cpu(msg->hdr.tid);
3049 u32 data_len = 0;
fe5da05e
ID
3050 int ret;
3051 int i;
3052
3053 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3054
5aea3dcd
ID
3055 down_read(&osdc->lock);
3056 if (!osd_registered(osd)) {
3057 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3058 goto out_unlock_osdc;
3059 }
3060 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3061
3062 mutex_lock(&osd->lock);
3063 req = lookup_request(&osd->o_requests, tid);
fe5da05e 3064 if (!req) {
5aea3dcd
ID
3065 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3066 goto out_unlock_session;
fe5da05e 3067 }
fe5da05e 3068
cd08e0a2 3069 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
fe5da05e 3070 ret = decode_MOSDOpReply(msg, &m);
cd08e0a2 3071 m.redirect.oloc.pool_ns = NULL;
fe5da05e
ID
3072 if (ret) {
3073 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3074 req->r_tid, ret);
3075 ceph_msg_dump(msg);
3076 goto fail_request;
3077 }
3078 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3079 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3080 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3081 le64_to_cpu(m.replay_version.version), m.user_version);
3082
3083 if (m.retry_attempt >= 0) {
3084 if (m.retry_attempt != req->r_attempts - 1) {
3085 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3086 req, req->r_tid, m.retry_attempt,
3087 req->r_attempts - 1);
5aea3dcd 3088 goto out_unlock_session;
fe5da05e
ID
3089 }
3090 } else {
3091 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3092 }
3093
3094 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3095 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3096 m.redirect.oloc.pool);
5aea3dcd
ID
3097 unlink_request(osd, req);
3098 mutex_unlock(&osd->lock);
205ee118 3099
30c156d9
YZ
3100 /*
3101 * Not ceph_oloc_copy() - changing pool_ns is not
3102 * supported.
3103 */
3104 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
5aea3dcd
ID
3105 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3106 req->r_tid = 0;
3107 __submit_request(req, false);
3108 goto out_unlock_osdc;
205ee118
ID
3109 }
3110
fe5da05e
ID
3111 if (m.num_ops != req->r_num_ops) {
3112 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3113 req->r_num_ops, req->r_tid);
3114 goto fail_request;
f24e9980 3115 }
fe5da05e
ID
3116 for (i = 0; i < req->r_num_ops; i++) {
3117 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3118 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3119 req->r_ops[i].rval = m.rval[i];
3120 req->r_ops[i].outdata_len = m.outdata_len[i];
3121 data_len += m.outdata_len[i];
3122 }
3123 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3124 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3125 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3126 goto fail_request;
3127 }
b18b9550
ID
3128 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3129 req, req->r_tid, m.result, data_len);
f24e9980 3130
b18b9550
ID
3131 /*
3132 * Since we only ever request ONDISK, we should only ever get
3133 * one (type of) reply back.
3134 */
3135 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3136 req->r_result = m.result ?: data_len;
3137 finish_request(req);
5aea3dcd
ID
3138 mutex_unlock(&osd->lock);
3139 up_read(&osdc->lock);
f24e9980 3140
b18b9550
ID
3141 __complete_request(req);
3142 complete_all(&req->r_completion);
3143 ceph_osdc_put_request(req);
f24e9980
SW
3144 return;
3145
fe5da05e 3146fail_request:
4609245e 3147 complete_request(req, -EIO);
5aea3dcd
ID
3148out_unlock_session:
3149 mutex_unlock(&osd->lock);
3150out_unlock_osdc:
3151 up_read(&osdc->lock);
f24e9980
SW
3152}
3153
42c1b124
ID
3154static void set_pool_was_full(struct ceph_osd_client *osdc)
3155{
3156 struct rb_node *n;
3157
3158 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3159 struct ceph_pg_pool_info *pi =
3160 rb_entry(n, struct ceph_pg_pool_info, node);
3161
3162 pi->was_full = __pool_full(pi);
3163 }
3164}
3165
5aea3dcd 3166static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
f24e9980 3167{
5aea3dcd 3168 struct ceph_pg_pool_info *pi;
f24e9980 3169
5aea3dcd
ID
3170 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3171 if (!pi)
3172 return false;
f24e9980 3173
5aea3dcd 3174 return pi->was_full && !__pool_full(pi);
422d2cb8
YS
3175}
3176
922dab61
ID
3177static enum calc_target_result
3178recalc_linger_target(struct ceph_osd_linger_request *lreq)
3179{
3180 struct ceph_osd_client *osdc = lreq->osdc;
3181 enum calc_target_result ct_res;
3182
7de030d6 3183 ct_res = calc_target(osdc, &lreq->t, NULL, true);
922dab61
ID
3184 if (ct_res == CALC_TARGET_NEED_RESEND) {
3185 struct ceph_osd *osd;
3186
3187 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3188 if (osd != lreq->osd) {
3189 unlink_linger(lreq->osd, lreq);
3190 link_linger(osd, lreq);
3191 }
3192 }
3193
3194 return ct_res;
3195}
3196
422d2cb8 3197/*
5aea3dcd 3198 * Requeue requests whose mapping to an OSD has changed.
422d2cb8 3199 */
5aea3dcd
ID
3200static void scan_requests(struct ceph_osd *osd,
3201 bool force_resend,
3202 bool cleared_full,
3203 bool check_pool_cleared_full,
3204 struct rb_root *need_resend,
3205 struct list_head *need_resend_linger)
422d2cb8 3206{
5aea3dcd
ID
3207 struct ceph_osd_client *osdc = osd->o_osdc;
3208 struct rb_node *n;
3209 bool force_resend_writes;
3210
922dab61
ID
3211 for (n = rb_first(&osd->o_linger_requests); n; ) {
3212 struct ceph_osd_linger_request *lreq =
3213 rb_entry(n, struct ceph_osd_linger_request, node);
3214 enum calc_target_result ct_res;
3215
3216 n = rb_next(n); /* recalc_linger_target() */
3217
3218 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3219 lreq->linger_id);
3220 ct_res = recalc_linger_target(lreq);
3221 switch (ct_res) {
3222 case CALC_TARGET_NO_ACTION:
3223 force_resend_writes = cleared_full ||
3224 (check_pool_cleared_full &&
3225 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3226 if (!force_resend && !force_resend_writes)
3227 break;
3228
3229 /* fall through */
3230 case CALC_TARGET_NEED_RESEND:
4609245e 3231 cancel_linger_map_check(lreq);
922dab61
ID
3232 /*
3233 * scan_requests() for the previous epoch(s)
3234 * may have already added it to the list, since
3235 * it's not unlinked here.
3236 */
3237 if (list_empty(&lreq->scan_item))
3238 list_add_tail(&lreq->scan_item, need_resend_linger);
3239 break;
3240 case CALC_TARGET_POOL_DNE:
a10bcb19 3241 list_del_init(&lreq->scan_item);
4609245e 3242 check_linger_pool_dne(lreq);
922dab61
ID
3243 break;
3244 }
3245 }
3246
5aea3dcd
ID
3247 for (n = rb_first(&osd->o_requests); n; ) {
3248 struct ceph_osd_request *req =
3249 rb_entry(n, struct ceph_osd_request, r_node);
3250 enum calc_target_result ct_res;
3251
4609245e 3252 n = rb_next(n); /* unlink_request(), check_pool_dne() */
5aea3dcd
ID
3253
3254 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
7de030d6
ID
3255 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3256 false);
5aea3dcd
ID
3257 switch (ct_res) {
3258 case CALC_TARGET_NO_ACTION:
3259 force_resend_writes = cleared_full ||
3260 (check_pool_cleared_full &&
3261 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3262 if (!force_resend &&
3263 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3264 !force_resend_writes))
3265 break;
3266
3267 /* fall through */
3268 case CALC_TARGET_NEED_RESEND:
4609245e 3269 cancel_map_check(req);
5aea3dcd
ID
3270 unlink_request(osd, req);
3271 insert_request(need_resend, req);
3272 break;
3273 case CALC_TARGET_POOL_DNE:
4609245e 3274 check_pool_dne(req);
5aea3dcd 3275 break;
b0494532 3276 }
6f6c7006 3277 }
422d2cb8 3278}
6f6c7006 3279
42c1b124 3280static int handle_one_map(struct ceph_osd_client *osdc,
5aea3dcd
ID
3281 void *p, void *end, bool incremental,
3282 struct rb_root *need_resend,
3283 struct list_head *need_resend_linger)
42c1b124
ID
3284{
3285 struct ceph_osdmap *newmap;
3286 struct rb_node *n;
3287 bool skipped_map = false;
3288 bool was_full;
3289
b7ec35b3 3290 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
42c1b124
ID
3291 set_pool_was_full(osdc);
3292
3293 if (incremental)
3294 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3295 else
3296 newmap = ceph_osdmap_decode(&p, end);
3297 if (IS_ERR(newmap))
3298 return PTR_ERR(newmap);
3299
3300 if (newmap != osdc->osdmap) {
3301 /*
3302 * Preserve ->was_full before destroying the old map.
3303 * For pools that weren't in the old map, ->was_full
3304 * should be false.
3305 */
3306 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3307 struct ceph_pg_pool_info *pi =
3308 rb_entry(n, struct ceph_pg_pool_info, node);
3309 struct ceph_pg_pool_info *old_pi;
3310
3311 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3312 if (old_pi)
3313 pi->was_full = old_pi->was_full;
3314 else
3315 WARN_ON(pi->was_full);
3316 }
3317
3318 if (osdc->osdmap->epoch &&
3319 osdc->osdmap->epoch + 1 < newmap->epoch) {
3320 WARN_ON(incremental);
3321 skipped_map = true;
3322 }
3323
3324 ceph_osdmap_destroy(osdc->osdmap);
3325 osdc->osdmap = newmap;
3326 }
3327
b7ec35b3 3328 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
5aea3dcd
ID
3329 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3330 need_resend, need_resend_linger);
3331
3332 for (n = rb_first(&osdc->osds); n; ) {
3333 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3334
3335 n = rb_next(n); /* close_osd() */
3336
3337 scan_requests(osd, skipped_map, was_full, true, need_resend,
3338 need_resend_linger);
3339 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3340 memcmp(&osd->o_con.peer_addr,
3341 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3342 sizeof(struct ceph_entity_addr)))
3343 close_osd(osd);
3344 }
42c1b124
ID
3345
3346 return 0;
3347}
6f6c7006 3348
5aea3dcd
ID
3349static void kick_requests(struct ceph_osd_client *osdc,
3350 struct rb_root *need_resend,
3351 struct list_head *need_resend_linger)
3352{
922dab61 3353 struct ceph_osd_linger_request *lreq, *nlreq;
04c7d789 3354 enum calc_target_result ct_res;
5aea3dcd
ID
3355 struct rb_node *n;
3356
04c7d789
ID
3357 /* make sure need_resend targets reflect latest map */
3358 for (n = rb_first(need_resend); n; ) {
3359 struct ceph_osd_request *req =
3360 rb_entry(n, struct ceph_osd_request, r_node);
3361
3362 n = rb_next(n);
3363
3364 if (req->r_t.epoch < osdc->osdmap->epoch) {
3365 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3366 if (ct_res == CALC_TARGET_POOL_DNE) {
3367 erase_request(need_resend, req);
3368 check_pool_dne(req);
3369 }
3370 }
3371 }
3372
5aea3dcd
ID
3373 for (n = rb_first(need_resend); n; ) {
3374 struct ceph_osd_request *req =
3375 rb_entry(n, struct ceph_osd_request, r_node);
3376 struct ceph_osd *osd;
3377
3378 n = rb_next(n);
3379 erase_request(need_resend, req); /* before link_request() */
3380
5aea3dcd
ID
3381 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3382 link_request(osd, req);
3383 if (!req->r_linger) {
3384 if (!osd_homeless(osd) && !req->r_t.paused)
3385 send_request(req);
922dab61
ID
3386 } else {
3387 cancel_linger_request(req);
5aea3dcd
ID
3388 }
3389 }
922dab61
ID
3390
3391 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3392 if (!osd_homeless(lreq->osd))
3393 send_linger(lreq);
3394
3395 list_del_init(&lreq->scan_item);
3396 }
5aea3dcd
ID
3397}
3398
f24e9980
SW
3399/*
3400 * Process updated osd map.
3401 *
3402 * The message contains any number of incremental and full maps, normally
3403 * indicating some sort of topology change in the cluster. Kick requests
3404 * off to different OSDs as needed.
3405 */
3406void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3407{
42c1b124
ID
3408 void *p = msg->front.iov_base;
3409 void *const end = p + msg->front.iov_len;
f24e9980
SW
3410 u32 nr_maps, maplen;
3411 u32 epoch;
f24e9980 3412 struct ceph_fsid fsid;
5aea3dcd
ID
3413 struct rb_root need_resend = RB_ROOT;
3414 LIST_HEAD(need_resend_linger);
42c1b124
ID
3415 bool handled_incremental = false;
3416 bool was_pauserd, was_pausewr;
3417 bool pauserd, pausewr;
3418 int err;
f24e9980 3419
42c1b124 3420 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
5aea3dcd 3421 down_write(&osdc->lock);
f24e9980
SW
3422
3423 /* verify fsid */
3424 ceph_decode_need(&p, end, sizeof(fsid), bad);
3425 ceph_decode_copy(&p, &fsid, sizeof(fsid));
0743304d 3426 if (ceph_check_fsid(osdc->client, &fsid) < 0)
42c1b124 3427 goto bad;
f24e9980 3428
b7ec35b3
ID
3429 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3430 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3431 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
42c1b124 3432 have_pool_full(osdc);
9a1ea2db 3433
f24e9980
SW
3434 /* incremental maps */
3435 ceph_decode_32_safe(&p, end, nr_maps, bad);
3436 dout(" %d inc maps\n", nr_maps);
3437 while (nr_maps > 0) {
3438 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
3439 epoch = ceph_decode_32(&p);
3440 maplen = ceph_decode_32(&p);
f24e9980 3441 ceph_decode_need(&p, end, maplen, bad);
42c1b124
ID
3442 if (osdc->osdmap->epoch &&
3443 osdc->osdmap->epoch + 1 == epoch) {
f24e9980
SW
3444 dout("applying incremental map %u len %d\n",
3445 epoch, maplen);
5aea3dcd
ID
3446 err = handle_one_map(osdc, p, p + maplen, true,
3447 &need_resend, &need_resend_linger);
42c1b124 3448 if (err)
f24e9980 3449 goto bad;
42c1b124 3450 handled_incremental = true;
f24e9980
SW
3451 } else {
3452 dout("ignoring incremental map %u len %d\n",
3453 epoch, maplen);
3454 }
42c1b124 3455 p += maplen;
f24e9980
SW
3456 nr_maps--;
3457 }
42c1b124 3458 if (handled_incremental)
f24e9980
SW
3459 goto done;
3460
3461 /* full maps */
3462 ceph_decode_32_safe(&p, end, nr_maps, bad);
3463 dout(" %d full maps\n", nr_maps);
3464 while (nr_maps) {
3465 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
3466 epoch = ceph_decode_32(&p);
3467 maplen = ceph_decode_32(&p);
f24e9980
SW
3468 ceph_decode_need(&p, end, maplen, bad);
3469 if (nr_maps > 1) {
3470 dout("skipping non-latest full map %u len %d\n",
3471 epoch, maplen);
e5253a7b 3472 } else if (osdc->osdmap->epoch >= epoch) {
f24e9980
SW
3473 dout("skipping full map %u len %d, "
3474 "older than our %u\n", epoch, maplen,
3475 osdc->osdmap->epoch);
3476 } else {
3477 dout("taking full map %u len %d\n", epoch, maplen);
5aea3dcd
ID
3478 err = handle_one_map(osdc, p, p + maplen, false,
3479 &need_resend, &need_resend_linger);
42c1b124 3480 if (err)
f24e9980 3481 goto bad;
f24e9980
SW
3482 }
3483 p += maplen;
3484 nr_maps--;
3485 }
3486
3487done:
cd634fb6
SW
3488 /*
3489 * subscribe to subsequent osdmap updates if full to ensure
3490 * we find out when we are no longer full and stop returning
3491 * ENOSPC.
3492 */
b7ec35b3
ID
3493 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3494 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3495 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
42c1b124 3496 have_pool_full(osdc);
58eb7932
JL
3497 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3498 osdc->osdmap->epoch < osdc->epoch_barrier)
42c1b124 3499 maybe_request_map(osdc);
cd634fb6 3500
5aea3dcd 3501 kick_requests(osdc, &need_resend, &need_resend_linger);
42c1b124 3502
fc36d0a4 3503 ceph_osdc_abort_on_full(osdc);
42c1b124
ID
3504 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3505 osdc->osdmap->epoch);
5aea3dcd 3506 up_write(&osdc->lock);
03066f23 3507 wake_up_all(&osdc->client->auth_wq);
f24e9980
SW
3508 return;
3509
3510bad:
3511 pr_err("osdc handle_map corrupt msg\n");
9ec7cab1 3512 ceph_msg_dump(msg);
5aea3dcd
ID
3513 up_write(&osdc->lock);
3514}
3515
3516/*
3517 * Resubmit requests pending on the given osd.
3518 */
3519static void kick_osd_requests(struct ceph_osd *osd)
3520{
3521 struct rb_node *n;
3522
922dab61 3523 for (n = rb_first(&osd->o_requests); n; ) {
5aea3dcd
ID
3524 struct ceph_osd_request *req =
3525 rb_entry(n, struct ceph_osd_request, r_node);
3526
922dab61
ID
3527 n = rb_next(n); /* cancel_linger_request() */
3528
5aea3dcd
ID
3529 if (!req->r_linger) {
3530 if (!req->r_t.paused)
3531 send_request(req);
922dab61
ID
3532 } else {
3533 cancel_linger_request(req);
5aea3dcd
ID
3534 }
3535 }
922dab61
ID
3536 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3537 struct ceph_osd_linger_request *lreq =
3538 rb_entry(n, struct ceph_osd_linger_request, node);
3539
3540 send_linger(lreq);
3541 }
5aea3dcd
ID
3542}
3543
3544/*
3545 * If the osd connection drops, we need to resubmit all requests.
3546 */
3547static void osd_fault(struct ceph_connection *con)
3548{
3549 struct ceph_osd *osd = con->private;
3550 struct ceph_osd_client *osdc = osd->o_osdc;
3551
3552 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3553
3554 down_write(&osdc->lock);
3555 if (!osd_registered(osd)) {
3556 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3557 goto out_unlock;
3558 }
3559
3560 if (!reopen_osd(osd))
3561 kick_osd_requests(osd);
3562 maybe_request_map(osdc);
3563
3564out_unlock:
3565 up_write(&osdc->lock);
f24e9980
SW
3566}
3567
a40c4f10
YS
3568/*
3569 * Process osd watch notifications
3570 */
3c663bbd
AE
3571static void handle_watch_notify(struct ceph_osd_client *osdc,
3572 struct ceph_msg *msg)
a40c4f10 3573{
922dab61
ID
3574 void *p = msg->front.iov_base;
3575 void *const end = p + msg->front.iov_len;
3576 struct ceph_osd_linger_request *lreq;
3577 struct linger_work *lwork;
3578 u8 proto_ver, opcode;
3579 u64 cookie, notify_id;
3580 u64 notifier_id = 0;
19079203 3581 s32 return_code = 0;
922dab61
ID
3582 void *payload = NULL;
3583 u32 payload_len = 0;
a40c4f10
YS
3584
3585 ceph_decode_8_safe(&p, end, proto_ver, bad);
3586 ceph_decode_8_safe(&p, end, opcode, bad);
3587 ceph_decode_64_safe(&p, end, cookie, bad);
922dab61 3588 p += 8; /* skip ver */
a40c4f10
YS
3589 ceph_decode_64_safe(&p, end, notify_id, bad);
3590
922dab61
ID
3591 if (proto_ver >= 1) {
3592 ceph_decode_32_safe(&p, end, payload_len, bad);
3593 ceph_decode_need(&p, end, payload_len, bad);
3594 payload = p;
3595 p += payload_len;
3596 }
3597
3598 if (le16_to_cpu(msg->hdr.version) >= 2)
19079203 3599 ceph_decode_32_safe(&p, end, return_code, bad);
922dab61
ID
3600
3601 if (le16_to_cpu(msg->hdr.version) >= 3)
3602 ceph_decode_64_safe(&p, end, notifier_id, bad);
3603
3604 down_read(&osdc->lock);
3605 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
3606 if (!lreq) {
3607 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
3608 cookie);
3609 goto out_unlock_osdc;
3610 }
3611
3612 mutex_lock(&lreq->lock);
19079203
ID
3613 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
3614 opcode, cookie, lreq, lreq->is_watch);
922dab61
ID
3615 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
3616 if (!lreq->last_error) {
3617 lreq->last_error = -ENOTCONN;
3618 queue_watch_error(lreq);
3619 }
19079203
ID
3620 } else if (!lreq->is_watch) {
3621 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
3622 if (lreq->notify_id && lreq->notify_id != notify_id) {
3623 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
3624 lreq->notify_id, notify_id);
3625 } else if (!completion_done(&lreq->notify_finish_wait)) {
3626 struct ceph_msg_data *data =
3627 list_first_entry_or_null(&msg->data,
3628 struct ceph_msg_data,
3629 links);
3630
3631 if (data) {
3632 if (lreq->preply_pages) {
3633 WARN_ON(data->type !=
3634 CEPH_MSG_DATA_PAGES);
3635 *lreq->preply_pages = data->pages;
3636 *lreq->preply_len = data->length;
3637 } else {
3638 ceph_release_page_vector(data->pages,
3639 calc_pages_for(0, data->length));
3640 }
3641 }
3642 lreq->notify_finish_error = return_code;
3643 complete_all(&lreq->notify_finish_wait);
3644 }
922dab61
ID
3645 } else {
3646 /* CEPH_WATCH_EVENT_NOTIFY */
3647 lwork = lwork_alloc(lreq, do_watch_notify);
3648 if (!lwork) {
3649 pr_err("failed to allocate notify-lwork\n");
3650 goto out_unlock_lreq;
a40c4f10 3651 }
a40c4f10 3652
922dab61
ID
3653 lwork->notify.notify_id = notify_id;
3654 lwork->notify.notifier_id = notifier_id;
3655 lwork->notify.payload = payload;
3656 lwork->notify.payload_len = payload_len;
3657 lwork->notify.msg = ceph_msg_get(msg);
3658 lwork_queue(lwork);
91883cd2 3659 }
a40c4f10 3660
922dab61
ID
3661out_unlock_lreq:
3662 mutex_unlock(&lreq->lock);
3663out_unlock_osdc:
3664 up_read(&osdc->lock);
a40c4f10
YS
3665 return;
3666
3667bad:
3668 pr_err("osdc handle_watch_notify corrupt msg\n");
a40c4f10
YS
3669}
3670
70636773
AE
3671/*
3672 * Register request, send initial attempt.
3673 */
3674int ceph_osdc_start_request(struct ceph_osd_client *osdc,
3675 struct ceph_osd_request *req,
3676 bool nofail)
3677{
5aea3dcd
ID
3678 down_read(&osdc->lock);
3679 submit_request(req, false);
3680 up_read(&osdc->lock);
0bbfdfe8 3681
5aea3dcd 3682 return 0;
f24e9980 3683}
3d14c5d2 3684EXPORT_SYMBOL(ceph_osdc_start_request);
f24e9980 3685
c9f9b93d 3686/*
c297eb42
ID
3687 * Unregister a registered request. The request is not completed:
3688 * ->r_result isn't set and __complete_request() isn't called.
c9f9b93d
ID
3689 */
3690void ceph_osdc_cancel_request(struct ceph_osd_request *req)
3691{
3692 struct ceph_osd_client *osdc = req->r_osdc;
3693
5aea3dcd 3694 down_write(&osdc->lock);
5aea3dcd
ID
3695 if (req->r_osd)
3696 cancel_request(req);
3697 up_write(&osdc->lock);
c9f9b93d
ID
3698}
3699EXPORT_SYMBOL(ceph_osdc_cancel_request);
3700
f24e9980 3701/*
42b06965 3702 * @timeout: in jiffies, 0 means "wait forever"
f24e9980 3703 */
42b06965
ID
3704static int wait_request_timeout(struct ceph_osd_request *req,
3705 unsigned long timeout)
f24e9980 3706{
42b06965 3707 long left;
c9f9b93d 3708
42b06965 3709 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
0e76abf2 3710 left = wait_for_completion_killable_timeout(&req->r_completion,
42b06965
ID
3711 ceph_timeout_jiffies(timeout));
3712 if (left <= 0) {
3713 left = left ?: -ETIMEDOUT;
c9f9b93d 3714 ceph_osdc_cancel_request(req);
42b06965
ID
3715 } else {
3716 left = req->r_result; /* completed */
f24e9980
SW
3717 }
3718
42b06965
ID
3719 return left;
3720}
3721
3722/*
3723 * wait for a request to complete
3724 */
3725int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
3726 struct ceph_osd_request *req)
3727{
3728 return wait_request_timeout(req, 0);
f24e9980 3729}
3d14c5d2 3730EXPORT_SYMBOL(ceph_osdc_wait_request);
f24e9980
SW
3731
3732/*
3733 * sync - wait for all in-flight requests to flush. avoid starvation.
3734 */
3735void ceph_osdc_sync(struct ceph_osd_client *osdc)
3736{
5aea3dcd
ID
3737 struct rb_node *n, *p;
3738 u64 last_tid = atomic64_read(&osdc->last_tid);
f24e9980 3739
5aea3dcd
ID
3740again:
3741 down_read(&osdc->lock);
3742 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3743 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3744
3745 mutex_lock(&osd->lock);
3746 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
3747 struct ceph_osd_request *req =
3748 rb_entry(p, struct ceph_osd_request, r_node);
3749
3750 if (req->r_tid > last_tid)
3751 break;
3752
3753 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
3754 continue;
f24e9980 3755
5aea3dcd
ID
3756 ceph_osdc_get_request(req);
3757 mutex_unlock(&osd->lock);
3758 up_read(&osdc->lock);
3759 dout("%s waiting on req %p tid %llu last_tid %llu\n",
3760 __func__, req, req->r_tid, last_tid);
b18b9550 3761 wait_for_completion(&req->r_completion);
5aea3dcd
ID
3762 ceph_osdc_put_request(req);
3763 goto again;
3764 }
f24e9980 3765
5aea3dcd 3766 mutex_unlock(&osd->lock);
f24e9980 3767 }
5aea3dcd
ID
3768
3769 up_read(&osdc->lock);
3770 dout("%s done last_tid %llu\n", __func__, last_tid);
f24e9980 3771}
3d14c5d2 3772EXPORT_SYMBOL(ceph_osdc_sync);
f24e9980 3773
922dab61
ID
3774static struct ceph_osd_request *
3775alloc_linger_request(struct ceph_osd_linger_request *lreq)
3776{
3777 struct ceph_osd_request *req;
3778
3779 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
3780 if (!req)
3781 return NULL;
3782
3783 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3784 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3785
3786 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
3787 ceph_osdc_put_request(req);
3788 return NULL;
3789 }
3790
3791 return req;
3792}
3793
3794/*
3795 * Returns a handle, caller owns a ref.
3796 */
3797struct ceph_osd_linger_request *
3798ceph_osdc_watch(struct ceph_osd_client *osdc,
3799 struct ceph_object_id *oid,
3800 struct ceph_object_locator *oloc,
3801 rados_watchcb2_t wcb,
3802 rados_watcherrcb_t errcb,
3803 void *data)
3804{
3805 struct ceph_osd_linger_request *lreq;
3806 int ret;
3807
3808 lreq = linger_alloc(osdc);
3809 if (!lreq)
3810 return ERR_PTR(-ENOMEM);
3811
19079203 3812 lreq->is_watch = true;
922dab61
ID
3813 lreq->wcb = wcb;
3814 lreq->errcb = errcb;
3815 lreq->data = data;
b07d3c4b 3816 lreq->watch_valid_thru = jiffies;
922dab61
ID
3817
3818 ceph_oid_copy(&lreq->t.base_oid, oid);
3819 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
54ea0046 3820 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
1134e091 3821 ktime_get_real_ts(&lreq->mtime);
922dab61
ID
3822
3823 lreq->reg_req = alloc_linger_request(lreq);
3824 if (!lreq->reg_req) {
3825 ret = -ENOMEM;
3826 goto err_put_lreq;
3827 }
3828
3829 lreq->ping_req = alloc_linger_request(lreq);
3830 if (!lreq->ping_req) {
3831 ret = -ENOMEM;
3832 goto err_put_lreq;
3833 }
3834
3835 down_write(&osdc->lock);
3836 linger_register(lreq); /* before osd_req_op_* */
3837 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
3838 CEPH_OSD_WATCH_OP_WATCH);
3839 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
3840 CEPH_OSD_WATCH_OP_PING);
3841 linger_submit(lreq);
3842 up_write(&osdc->lock);
3843
3844 ret = linger_reg_commit_wait(lreq);
3845 if (ret) {
3846 linger_cancel(lreq);
3847 goto err_put_lreq;
3848 }
3849
3850 return lreq;
3851
3852err_put_lreq:
3853 linger_put(lreq);
3854 return ERR_PTR(ret);
3855}
3856EXPORT_SYMBOL(ceph_osdc_watch);
3857
3858/*
3859 * Releases a ref.
3860 *
3861 * Times out after mount_timeout to preserve rbd unmap behaviour
3862 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
3863 * with mount_timeout").
3864 */
3865int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
3866 struct ceph_osd_linger_request *lreq)
3867{
3868 struct ceph_options *opts = osdc->client->options;
3869 struct ceph_osd_request *req;
3870 int ret;
3871
3872 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3873 if (!req)
3874 return -ENOMEM;
3875
3876 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3877 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
54ea0046 3878 req->r_flags = CEPH_OSD_FLAG_WRITE;
1134e091 3879 ktime_get_real_ts(&req->r_mtime);
922dab61
ID
3880 osd_req_op_watch_init(req, 0, lreq->linger_id,
3881 CEPH_OSD_WATCH_OP_UNWATCH);
3882
3883 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3884 if (ret)
3885 goto out_put_req;
3886
3887 ceph_osdc_start_request(osdc, req, false);
3888 linger_cancel(lreq);
3889 linger_put(lreq);
3890 ret = wait_request_timeout(req, opts->mount_timeout);
3891
3892out_put_req:
3893 ceph_osdc_put_request(req);
3894 return ret;
3895}
3896EXPORT_SYMBOL(ceph_osdc_unwatch);
3897
3898static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
3899 u64 notify_id, u64 cookie, void *payload,
3900 size_t payload_len)
3901{
3902 struct ceph_osd_req_op *op;
3903 struct ceph_pagelist *pl;
3904 int ret;
3905
3906 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
3907
3908 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3909 if (!pl)
3910 return -ENOMEM;
3911
3912 ceph_pagelist_init(pl);
3913 ret = ceph_pagelist_encode_64(pl, notify_id);
3914 ret |= ceph_pagelist_encode_64(pl, cookie);
3915 if (payload) {
3916 ret |= ceph_pagelist_encode_32(pl, payload_len);
3917 ret |= ceph_pagelist_append(pl, payload, payload_len);
3918 } else {
3919 ret |= ceph_pagelist_encode_32(pl, 0);
3920 }
3921 if (ret) {
3922 ceph_pagelist_release(pl);
3923 return -ENOMEM;
3924 }
3925
3926 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
3927 op->indata_len = pl->length;
3928 return 0;
3929}
3930
3931int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
3932 struct ceph_object_id *oid,
3933 struct ceph_object_locator *oloc,
3934 u64 notify_id,
3935 u64 cookie,
3936 void *payload,
3937 size_t payload_len)
3938{
3939 struct ceph_osd_request *req;
3940 int ret;
3941
3942 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3943 if (!req)
3944 return -ENOMEM;
3945
3946 ceph_oid_copy(&req->r_base_oid, oid);
3947 ceph_oloc_copy(&req->r_base_oloc, oloc);
3948 req->r_flags = CEPH_OSD_FLAG_READ;
3949
3950 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3951 if (ret)
3952 goto out_put_req;
3953
3954 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
3955 payload_len);
3956 if (ret)
3957 goto out_put_req;
3958
3959 ceph_osdc_start_request(osdc, req, false);
3960 ret = ceph_osdc_wait_request(osdc, req);
3961
3962out_put_req:
3963 ceph_osdc_put_request(req);
3964 return ret;
3965}
3966EXPORT_SYMBOL(ceph_osdc_notify_ack);
3967
19079203
ID
3968static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
3969 u64 cookie, u32 prot_ver, u32 timeout,
3970 void *payload, size_t payload_len)
3971{
3972 struct ceph_osd_req_op *op;
3973 struct ceph_pagelist *pl;
3974 int ret;
3975
3976 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
3977 op->notify.cookie = cookie;
3978
3979 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3980 if (!pl)
3981 return -ENOMEM;
3982
3983 ceph_pagelist_init(pl);
3984 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
3985 ret |= ceph_pagelist_encode_32(pl, timeout);
3986 ret |= ceph_pagelist_encode_32(pl, payload_len);
3987 ret |= ceph_pagelist_append(pl, payload, payload_len);
3988 if (ret) {
3989 ceph_pagelist_release(pl);
3990 return -ENOMEM;
3991 }
3992
3993 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
3994 op->indata_len = pl->length;
3995 return 0;
3996}
3997
3998/*
3999 * @timeout: in seconds
4000 *
4001 * @preply_{pages,len} are initialized both on success and error.
4002 * The caller is responsible for:
4003 *
4004 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4005 */
4006int ceph_osdc_notify(struct ceph_osd_client *osdc,
4007 struct ceph_object_id *oid,
4008 struct ceph_object_locator *oloc,
4009 void *payload,
4010 size_t payload_len,
4011 u32 timeout,
4012 struct page ***preply_pages,
4013 size_t *preply_len)
4014{
4015 struct ceph_osd_linger_request *lreq;
4016 struct page **pages;
4017 int ret;
4018
4019 WARN_ON(!timeout);
4020 if (preply_pages) {
4021 *preply_pages = NULL;
4022 *preply_len = 0;
4023 }
4024
4025 lreq = linger_alloc(osdc);
4026 if (!lreq)
4027 return -ENOMEM;
4028
4029 lreq->preply_pages = preply_pages;
4030 lreq->preply_len = preply_len;
4031
4032 ceph_oid_copy(&lreq->t.base_oid, oid);
4033 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4034 lreq->t.flags = CEPH_OSD_FLAG_READ;
4035
4036 lreq->reg_req = alloc_linger_request(lreq);
4037 if (!lreq->reg_req) {
4038 ret = -ENOMEM;
4039 goto out_put_lreq;
4040 }
4041
4042 /* for notify_id */
4043 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4044 if (IS_ERR(pages)) {
4045 ret = PTR_ERR(pages);
4046 goto out_put_lreq;
4047 }
4048
4049 down_write(&osdc->lock);
4050 linger_register(lreq); /* before osd_req_op_* */
4051 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
4052 timeout, payload, payload_len);
4053 if (ret) {
4054 linger_unregister(lreq);
4055 up_write(&osdc->lock);
4056 ceph_release_page_vector(pages, 1);
4057 goto out_put_lreq;
4058 }
4059 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4060 response_data),
4061 pages, PAGE_SIZE, 0, false, true);
4062 linger_submit(lreq);
4063 up_write(&osdc->lock);
4064
4065 ret = linger_reg_commit_wait(lreq);
4066 if (!ret)
4067 ret = linger_notify_finish_wait(lreq);
4068 else
4069 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4070
4071 linger_cancel(lreq);
4072out_put_lreq:
4073 linger_put(lreq);
4074 return ret;
4075}
4076EXPORT_SYMBOL(ceph_osdc_notify);
4077
b07d3c4b
ID
4078/*
4079 * Return the number of milliseconds since the watch was last
4080 * confirmed, or an error. If there is an error, the watch is no
4081 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4082 */
4083int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4084 struct ceph_osd_linger_request *lreq)
4085{
4086 unsigned long stamp, age;
4087 int ret;
4088
4089 down_read(&osdc->lock);
4090 mutex_lock(&lreq->lock);
4091 stamp = lreq->watch_valid_thru;
4092 if (!list_empty(&lreq->pending_lworks)) {
4093 struct linger_work *lwork =
4094 list_first_entry(&lreq->pending_lworks,
4095 struct linger_work,
4096 pending_item);
4097
4098 if (time_before(lwork->queued_stamp, stamp))
4099 stamp = lwork->queued_stamp;
4100 }
4101 age = jiffies - stamp;
4102 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4103 lreq, lreq->linger_id, age, lreq->last_error);
4104 /* we are truncating to msecs, so return a safe upper bound */
4105 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4106
4107 mutex_unlock(&lreq->lock);
4108 up_read(&osdc->lock);
4109 return ret;
4110}
4111
a4ed38d7
DF
4112static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4113{
4114 u8 struct_v;
4115 u32 struct_len;
4116 int ret;
4117
4118 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4119 &struct_v, &struct_len);
4120 if (ret)
4121 return ret;
4122
4123 ceph_decode_copy(p, &item->name, sizeof(item->name));
4124 item->cookie = ceph_decode_64(p);
4125 *p += 4; /* skip timeout_seconds */
4126 if (struct_v >= 2) {
4127 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4128 ceph_decode_addr(&item->addr);
4129 }
4130
4131 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4132 ENTITY_NAME(item->name), item->cookie,
4133 ceph_pr_addr(&item->addr.in_addr));
4134 return 0;
4135}
4136
4137static int decode_watchers(void **p, void *end,
4138 struct ceph_watch_item **watchers,
4139 u32 *num_watchers)
4140{
4141 u8 struct_v;
4142 u32 struct_len;
4143 int i;
4144 int ret;
4145
4146 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4147 &struct_v, &struct_len);
4148 if (ret)
4149 return ret;
4150
4151 *num_watchers = ceph_decode_32(p);
4152 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4153 if (!*watchers)
4154 return -ENOMEM;
4155
4156 for (i = 0; i < *num_watchers; i++) {
4157 ret = decode_watcher(p, end, *watchers + i);
4158 if (ret) {
4159 kfree(*watchers);
4160 return ret;
4161 }
4162 }
4163
4164 return 0;
4165}
4166
4167/*
4168 * On success, the caller is responsible for:
4169 *
4170 * kfree(watchers);
4171 */
4172int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4173 struct ceph_object_id *oid,
4174 struct ceph_object_locator *oloc,
4175 struct ceph_watch_item **watchers,
4176 u32 *num_watchers)
4177{
4178 struct ceph_osd_request *req;
4179 struct page **pages;
4180 int ret;
4181
4182 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4183 if (!req)
4184 return -ENOMEM;
4185
4186 ceph_oid_copy(&req->r_base_oid, oid);
4187 ceph_oloc_copy(&req->r_base_oloc, oloc);
4188 req->r_flags = CEPH_OSD_FLAG_READ;
4189
4190 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4191 if (ret)
4192 goto out_put_req;
4193
4194 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4195 if (IS_ERR(pages)) {
4196 ret = PTR_ERR(pages);
4197 goto out_put_req;
4198 }
4199
4200 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4201 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4202 response_data),
4203 pages, PAGE_SIZE, 0, false, true);
4204
4205 ceph_osdc_start_request(osdc, req, false);
4206 ret = ceph_osdc_wait_request(osdc, req);
4207 if (ret >= 0) {
4208 void *p = page_address(pages[0]);
4209 void *const end = p + req->r_ops[0].outdata_len;
4210
4211 ret = decode_watchers(&p, end, watchers, num_watchers);
4212 }
4213
4214out_put_req:
4215 ceph_osdc_put_request(req);
4216 return ret;
4217}
4218EXPORT_SYMBOL(ceph_osdc_list_watchers);
4219
dd935f44
JD
4220/*
4221 * Call all pending notify callbacks - for use after a watch is
4222 * unregistered, to make sure no more callbacks for it will be invoked
4223 */
f6479449 4224void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
dd935f44 4225{
99d16943 4226 dout("%s osdc %p\n", __func__, osdc);
dd935f44
JD
4227 flush_workqueue(osdc->notify_wq);
4228}
4229EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4230
7cca78c9
ID
4231void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4232{
4233 down_read(&osdc->lock);
4234 maybe_request_map(osdc);
4235 up_read(&osdc->lock);
4236}
4237EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
dd935f44 4238
428a7158
DF
4239/*
4240 * Execute an OSD class method on an object.
4241 *
4242 * @flags: CEPH_OSD_FLAG_*
2544a020 4243 * @resp_len: in/out param for reply length
428a7158
DF
4244 */
4245int ceph_osdc_call(struct ceph_osd_client *osdc,
4246 struct ceph_object_id *oid,
4247 struct ceph_object_locator *oloc,
4248 const char *class, const char *method,
4249 unsigned int flags,
4250 struct page *req_page, size_t req_len,
4251 struct page *resp_page, size_t *resp_len)
4252{
4253 struct ceph_osd_request *req;
4254 int ret;
4255
2544a020
ID
4256 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4257 return -E2BIG;
4258
428a7158
DF
4259 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4260 if (!req)
4261 return -ENOMEM;
4262
4263 ceph_oid_copy(&req->r_base_oid, oid);
4264 ceph_oloc_copy(&req->r_base_oloc, oloc);
4265 req->r_flags = flags;
4266
4267 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4268 if (ret)
4269 goto out_put_req;
4270
4271 osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4272 if (req_page)
4273 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4274 0, false, false);
4275 if (resp_page)
4276 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
2544a020 4277 *resp_len, 0, false, false);
428a7158
DF
4278
4279 ceph_osdc_start_request(osdc, req, false);
4280 ret = ceph_osdc_wait_request(osdc, req);
4281 if (ret >= 0) {
4282 ret = req->r_ops[0].rval;
4283 if (resp_page)
4284 *resp_len = req->r_ops[0].outdata_len;
4285 }
4286
4287out_put_req:
4288 ceph_osdc_put_request(req);
4289 return ret;
4290}
4291EXPORT_SYMBOL(ceph_osdc_call);
4292
f24e9980
SW
4293/*
4294 * init, shutdown
4295 */
4296int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4297{
4298 int err;
4299
4300 dout("init\n");
4301 osdc->client = client;
5aea3dcd 4302 init_rwsem(&osdc->lock);
f24e9980 4303 osdc->osds = RB_ROOT;
f5a2041b 4304 INIT_LIST_HEAD(&osdc->osd_lru);
9dd2845c 4305 spin_lock_init(&osdc->osd_lru_lock);
5aea3dcd
ID
4306 osd_init(&osdc->homeless_osd);
4307 osdc->homeless_osd.o_osdc = osdc;
4308 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
264048af 4309 osdc->last_linger_id = CEPH_LINGER_ID_START;
922dab61 4310 osdc->linger_requests = RB_ROOT;
4609245e
ID
4311 osdc->map_checks = RB_ROOT;
4312 osdc->linger_map_checks = RB_ROOT;
f24e9980 4313 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
f5a2041b
YS
4314 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
4315
5f44f142 4316 err = -ENOMEM;
e5253a7b
ID
4317 osdc->osdmap = ceph_osdmap_alloc();
4318 if (!osdc->osdmap)
4319 goto out;
4320
9e767adb
ID
4321 osdc->req_mempool = mempool_create_slab_pool(10,
4322 ceph_osd_request_cache);
f24e9980 4323 if (!osdc->req_mempool)
e5253a7b 4324 goto out_map;
f24e9980 4325
d50b409f 4326 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
711da55d 4327 PAGE_SIZE, 10, true, "osd_op");
f24e9980 4328 if (err < 0)
5f44f142 4329 goto out_mempool;
d50b409f 4330 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
711da55d 4331 PAGE_SIZE, 10, true, "osd_op_reply");
c16e7869
SW
4332 if (err < 0)
4333 goto out_msgpool;
a40c4f10 4334
dbcae088 4335 err = -ENOMEM;
a40c4f10 4336 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
dbcae088 4337 if (!osdc->notify_wq)
c172ec5c
ID
4338 goto out_msgpool_reply;
4339
fbca9635
ID
4340 schedule_delayed_work(&osdc->timeout_work,
4341 osdc->client->options->osd_keepalive_timeout);
b37ee1b9
ID
4342 schedule_delayed_work(&osdc->osds_timeout_work,
4343 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4344
f24e9980 4345 return 0;
5f44f142 4346
c172ec5c
ID
4347out_msgpool_reply:
4348 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
c16e7869
SW
4349out_msgpool:
4350 ceph_msgpool_destroy(&osdc->msgpool_op);
5f44f142
SW
4351out_mempool:
4352 mempool_destroy(osdc->req_mempool);
e5253a7b
ID
4353out_map:
4354 ceph_osdmap_destroy(osdc->osdmap);
5f44f142
SW
4355out:
4356 return err;
f24e9980
SW
4357}
4358
4359void ceph_osdc_stop(struct ceph_osd_client *osdc)
4360{
a40c4f10
YS
4361 flush_workqueue(osdc->notify_wq);
4362 destroy_workqueue(osdc->notify_wq);
f24e9980 4363 cancel_delayed_work_sync(&osdc->timeout_work);
f5a2041b 4364 cancel_delayed_work_sync(&osdc->osds_timeout_work);
42a2c09f 4365
5aea3dcd 4366 down_write(&osdc->lock);
42a2c09f
ID
4367 while (!RB_EMPTY_ROOT(&osdc->osds)) {
4368 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
4369 struct ceph_osd, o_node);
5aea3dcd 4370 close_osd(osd);
42a2c09f 4371 }
5aea3dcd 4372 up_write(&osdc->lock);
02113a0f 4373 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
5aea3dcd
ID
4374 osd_cleanup(&osdc->homeless_osd);
4375
4376 WARN_ON(!list_empty(&osdc->osd_lru));
922dab61 4377 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
4609245e
ID
4378 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
4379 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
5aea3dcd
ID
4380 WARN_ON(atomic_read(&osdc->num_requests));
4381 WARN_ON(atomic_read(&osdc->num_homeless));
42a2c09f 4382
e5253a7b 4383 ceph_osdmap_destroy(osdc->osdmap);
f24e9980
SW
4384 mempool_destroy(osdc->req_mempool);
4385 ceph_msgpool_destroy(&osdc->msgpool_op);
c16e7869 4386 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
f24e9980
SW
4387}
4388
4389/*
4390 * Read some contiguous pages. If we cross a stripe boundary, shorten
4391 * *plen. Return number of bytes read, or error.
4392 */
4393int ceph_osdc_readpages(struct ceph_osd_client *osdc,
4394 struct ceph_vino vino, struct ceph_file_layout *layout,
4395 u64 off, u64 *plen,
4396 u32 truncate_seq, u64 truncate_size,
b7495fc2 4397 struct page **pages, int num_pages, int page_align)
f24e9980
SW
4398{
4399 struct ceph_osd_request *req;
4400 int rc = 0;
4401
4402 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
4403 vino.snap, off, *plen);
715e4cd4 4404 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
f24e9980 4405 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
acead002 4406 NULL, truncate_seq, truncate_size,
153e5167 4407 false);
6816282d
SW
4408 if (IS_ERR(req))
4409 return PTR_ERR(req);
f24e9980
SW
4410
4411 /* it may be a short read due to an object boundary */
406e2c9f 4412 osd_req_op_extent_osd_data_pages(req, 0,
a4ce40a9 4413 pages, *plen, page_align, false, false);
f24e9980 4414
e0c59487 4415 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
43bfe5de 4416 off, *plen, *plen, page_align);
f24e9980
SW
4417
4418 rc = ceph_osdc_start_request(osdc, req, false);
4419 if (!rc)
4420 rc = ceph_osdc_wait_request(osdc, req);
4421
4422 ceph_osdc_put_request(req);
4423 dout("readpages result %d\n", rc);
4424 return rc;
4425}
3d14c5d2 4426EXPORT_SYMBOL(ceph_osdc_readpages);
f24e9980
SW
4427
4428/*
4429 * do a synchronous write on N pages
4430 */
4431int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
4432 struct ceph_file_layout *layout,
4433 struct ceph_snap_context *snapc,
4434 u64 off, u64 len,
4435 u32 truncate_seq, u64 truncate_size,
4436 struct timespec *mtime,
24808826 4437 struct page **pages, int num_pages)
f24e9980
SW
4438{
4439 struct ceph_osd_request *req;
4440 int rc = 0;
b7495fc2 4441 int page_align = off & ~PAGE_MASK;
f24e9980 4442
715e4cd4 4443 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
54ea0046 4444 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
acead002 4445 snapc, truncate_seq, truncate_size,
153e5167 4446 true);
6816282d
SW
4447 if (IS_ERR(req))
4448 return PTR_ERR(req);
f24e9980
SW
4449
4450 /* it may be a short write due to an object boundary */
406e2c9f 4451 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
43bfe5de
AE
4452 false, false);
4453 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
f24e9980 4454
bb873b53 4455 req->r_mtime = *mtime;
87f979d3 4456 rc = ceph_osdc_start_request(osdc, req, true);
f24e9980
SW
4457 if (!rc)
4458 rc = ceph_osdc_wait_request(osdc, req);
4459
4460 ceph_osdc_put_request(req);
4461 if (rc == 0)
4462 rc = len;
4463 dout("writepages result %d\n", rc);
4464 return rc;
4465}
3d14c5d2 4466EXPORT_SYMBOL(ceph_osdc_writepages);
f24e9980 4467
5522ae0b
AE
4468int ceph_osdc_setup(void)
4469{
3f1af42a
ID
4470 size_t size = sizeof(struct ceph_osd_request) +
4471 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
4472
5522ae0b 4473 BUG_ON(ceph_osd_request_cache);
3f1af42a
ID
4474 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
4475 0, 0, NULL);
5522ae0b
AE
4476
4477 return ceph_osd_request_cache ? 0 : -ENOMEM;
4478}
4479EXPORT_SYMBOL(ceph_osdc_setup);
4480
4481void ceph_osdc_cleanup(void)
4482{
4483 BUG_ON(!ceph_osd_request_cache);
4484 kmem_cache_destroy(ceph_osd_request_cache);
4485 ceph_osd_request_cache = NULL;
4486}
4487EXPORT_SYMBOL(ceph_osdc_cleanup);
4488
f24e9980
SW
4489/*
4490 * handle incoming message
4491 */
4492static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4493{
4494 struct ceph_osd *osd = con->private;
5aea3dcd 4495 struct ceph_osd_client *osdc = osd->o_osdc;
f24e9980
SW
4496 int type = le16_to_cpu(msg->hdr.type);
4497
f24e9980
SW
4498 switch (type) {
4499 case CEPH_MSG_OSD_MAP:
4500 ceph_osdc_handle_map(osdc, msg);
4501 break;
4502 case CEPH_MSG_OSD_OPREPLY:
5aea3dcd 4503 handle_reply(osd, msg);
f24e9980 4504 break;
a40c4f10
YS
4505 case CEPH_MSG_WATCH_NOTIFY:
4506 handle_watch_notify(osdc, msg);
4507 break;
f24e9980
SW
4508
4509 default:
4510 pr_err("received unknown message type %d %s\n", type,
4511 ceph_msg_type_name(type));
4512 }
5aea3dcd 4513
f24e9980
SW
4514 ceph_msg_put(msg);
4515}
4516
5b3a4db3 4517/*
d15f9d69
ID
4518 * Lookup and return message for incoming reply. Don't try to do
4519 * anything about a larger than preallocated data portion of the
4520 * message at the moment - for now, just skip the message.
5b3a4db3
SW
4521 */
4522static struct ceph_msg *get_reply(struct ceph_connection *con,
2450418c
YS
4523 struct ceph_msg_header *hdr,
4524 int *skip)
f24e9980
SW
4525{
4526 struct ceph_osd *osd = con->private;
4527 struct ceph_osd_client *osdc = osd->o_osdc;
5aea3dcd 4528 struct ceph_msg *m = NULL;
0547a9b3 4529 struct ceph_osd_request *req;
3f0a4ac5 4530 int front_len = le32_to_cpu(hdr->front_len);
5b3a4db3 4531 int data_len = le32_to_cpu(hdr->data_len);
5aea3dcd 4532 u64 tid = le64_to_cpu(hdr->tid);
f24e9980 4533
5aea3dcd
ID
4534 down_read(&osdc->lock);
4535 if (!osd_registered(osd)) {
4536 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
4537 *skip = 1;
4538 goto out_unlock_osdc;
4539 }
4540 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
4541
4542 mutex_lock(&osd->lock);
4543 req = lookup_request(&osd->o_requests, tid);
0547a9b3 4544 if (!req) {
cd8140c6
ID
4545 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
4546 osd->o_osd, tid);
d15f9d69 4547 *skip = 1;
5aea3dcd 4548 goto out_unlock_session;
0547a9b3 4549 }
c16e7869 4550
ace6d3a9 4551 ceph_msg_revoke_incoming(req->r_reply);
0547a9b3 4552
f2be82b0 4553 if (front_len > req->r_reply->front_alloc_len) {
d15f9d69
ID
4554 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
4555 __func__, osd->o_osd, req->r_tid, front_len,
4556 req->r_reply->front_alloc_len);
3f0a4ac5
ID
4557 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
4558 false);
a79832f2 4559 if (!m)
5aea3dcd 4560 goto out_unlock_session;
c16e7869
SW
4561 ceph_msg_put(req->r_reply);
4562 req->r_reply = m;
4563 }
0fff87ec 4564
d15f9d69
ID
4565 if (data_len > req->r_reply->data_length) {
4566 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
4567 __func__, osd->o_osd, req->r_tid, data_len,
4568 req->r_reply->data_length);
4569 m = NULL;
4570 *skip = 1;
5aea3dcd 4571 goto out_unlock_session;
0547a9b3 4572 }
d15f9d69
ID
4573
4574 m = ceph_msg_get(req->r_reply);
c16e7869 4575 dout("get_reply tid %lld %p\n", tid, m);
0547a9b3 4576
5aea3dcd
ID
4577out_unlock_session:
4578 mutex_unlock(&osd->lock);
4579out_unlock_osdc:
4580 up_read(&osdc->lock);
2450418c 4581 return m;
5b3a4db3
SW
4582}
4583
19079203
ID
4584/*
4585 * TODO: switch to a msg-owned pagelist
4586 */
4587static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
4588{
4589 struct ceph_msg *m;
4590 int type = le16_to_cpu(hdr->type);
4591 u32 front_len = le32_to_cpu(hdr->front_len);
4592 u32 data_len = le32_to_cpu(hdr->data_len);
4593
4594 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
4595 if (!m)
4596 return NULL;
4597
4598 if (data_len) {
4599 struct page **pages;
4600 struct ceph_osd_data osd_data;
4601
4602 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
4603 GFP_NOIO);
c22e853a 4604 if (IS_ERR(pages)) {
19079203
ID
4605 ceph_msg_put(m);
4606 return NULL;
4607 }
4608
4609 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
4610 false);
4611 ceph_osdc_msg_data_add(m, &osd_data);
4612 }
4613
4614 return m;
4615}
4616
5b3a4db3
SW
4617static struct ceph_msg *alloc_msg(struct ceph_connection *con,
4618 struct ceph_msg_header *hdr,
4619 int *skip)
4620{
4621 struct ceph_osd *osd = con->private;
4622 int type = le16_to_cpu(hdr->type);
5b3a4db3 4623
1c20f2d2 4624 *skip = 0;
5b3a4db3
SW
4625 switch (type) {
4626 case CEPH_MSG_OSD_MAP:
a40c4f10 4627 case CEPH_MSG_WATCH_NOTIFY:
19079203 4628 return alloc_msg_with_page_vector(hdr);
5b3a4db3
SW
4629 case CEPH_MSG_OSD_OPREPLY:
4630 return get_reply(con, hdr, skip);
4631 default:
5aea3dcd
ID
4632 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
4633 osd->o_osd, type);
5b3a4db3
SW
4634 *skip = 1;
4635 return NULL;
4636 }
f24e9980
SW
4637}
4638
4639/*
4640 * Wrappers to refcount containing ceph_osd struct
4641 */
4642static struct ceph_connection *get_osd_con(struct ceph_connection *con)
4643{
4644 struct ceph_osd *osd = con->private;
4645 if (get_osd(osd))
4646 return con;
4647 return NULL;
4648}
4649
4650static void put_osd_con(struct ceph_connection *con)
4651{
4652 struct ceph_osd *osd = con->private;
4653 put_osd(osd);
4654}
4655
4e7a5dcd
SW
4656/*
4657 * authentication
4658 */
a3530df3
AE
4659/*
4660 * Note: returned pointer is the address of a structure that's
4661 * managed separately. Caller must *not* attempt to free it.
4662 */
4663static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 4664 int *proto, int force_new)
4e7a5dcd
SW
4665{
4666 struct ceph_osd *o = con->private;
4667 struct ceph_osd_client *osdc = o->o_osdc;
4668 struct ceph_auth_client *ac = osdc->client->monc.auth;
74f1869f 4669 struct ceph_auth_handshake *auth = &o->o_auth;
4e7a5dcd 4670
74f1869f 4671 if (force_new && auth->authorizer) {
6c1ea260 4672 ceph_auth_destroy_authorizer(auth->authorizer);
74f1869f
AE
4673 auth->authorizer = NULL;
4674 }
27859f97
SW
4675 if (!auth->authorizer) {
4676 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4677 auth);
4e7a5dcd 4678 if (ret)
a3530df3 4679 return ERR_PTR(ret);
27859f97
SW
4680 } else {
4681 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
0bed9b5c
SW
4682 auth);
4683 if (ret)
4684 return ERR_PTR(ret);
4e7a5dcd 4685 }
4e7a5dcd 4686 *proto = ac->protocol;
74f1869f 4687
a3530df3 4688 return auth;
4e7a5dcd
SW
4689}
4690
4691
0dde5848 4692static int verify_authorizer_reply(struct ceph_connection *con)
4e7a5dcd
SW
4693{
4694 struct ceph_osd *o = con->private;
4695 struct ceph_osd_client *osdc = o->o_osdc;
4696 struct ceph_auth_client *ac = osdc->client->monc.auth;
4697
0dde5848 4698 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
4e7a5dcd
SW
4699}
4700
9bd2e6f8
SW
4701static int invalidate_authorizer(struct ceph_connection *con)
4702{
4703 struct ceph_osd *o = con->private;
4704 struct ceph_osd_client *osdc = o->o_osdc;
4705 struct ceph_auth_client *ac = osdc->client->monc.auth;
4706
27859f97 4707 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
9bd2e6f8
SW
4708 return ceph_monc_validate_auth(&osdc->client->monc);
4709}
4e7a5dcd 4710
8cb441c0
ID
4711static void osd_reencode_message(struct ceph_msg *msg)
4712{
4713 encode_request_finish(msg);
4714}
4715
79dbd1ba 4716static int osd_sign_message(struct ceph_msg *msg)
33d07337 4717{
79dbd1ba 4718 struct ceph_osd *o = msg->con->private;
33d07337 4719 struct ceph_auth_handshake *auth = &o->o_auth;
79dbd1ba 4720
33d07337
YZ
4721 return ceph_auth_sign_message(auth, msg);
4722}
4723
79dbd1ba 4724static int osd_check_message_signature(struct ceph_msg *msg)
33d07337 4725{
79dbd1ba 4726 struct ceph_osd *o = msg->con->private;
33d07337 4727 struct ceph_auth_handshake *auth = &o->o_auth;
79dbd1ba 4728
33d07337
YZ
4729 return ceph_auth_check_message_signature(auth, msg);
4730}
4731
9e32789f 4732static const struct ceph_connection_operations osd_con_ops = {
f24e9980
SW
4733 .get = get_osd_con,
4734 .put = put_osd_con,
4735 .dispatch = dispatch,
4e7a5dcd
SW
4736 .get_authorizer = get_authorizer,
4737 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 4738 .invalidate_authorizer = invalidate_authorizer,
f24e9980 4739 .alloc_msg = alloc_msg,
8cb441c0 4740 .reencode_message = osd_reencode_message,
79dbd1ba
ID
4741 .sign_message = osd_sign_message,
4742 .check_message_signature = osd_check_message_signature,
5aea3dcd 4743 .fault = osd_fault,
f24e9980 4744};