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