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