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