blob: ac4753421d0ca80ab84749b371d0f37c82542bb1 [file] [log] [blame]
Alex Eldera4ce40a2013-04-05 01:27:12 -05001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07005#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>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070011#ifdef CONFIG_BLOCK
12#include <linux/bio.h>
13#endif
Sage Weilf24e9982009-10-06 11:31:10 -070014
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#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>
Sage Weilf24e9982009-10-06 11:31:10 -070021
Sage Weilc16e7862010-03-01 13:02:00 -080022#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080023
Alex Elder5522ae02013-05-01 12:43:04 -050024static struct kmem_cache *ceph_osd_request_cache;
25
Tobias Klauser9e327892010-05-20 10:40:19 +020026static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070027
Sage Weilf24e9982009-10-06 11:31:10 -070028/*
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
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020046static 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);
Ilya Dryomov922dab62016-05-26 01:15:02 +020048static 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);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020052
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}
Ilya Dryomov922dab62016-05-26 01:15:02 +020081static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
82{
83 WARN_ON(!mutex_is_locked(&lreq->lock));
84}
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020085#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) { }
Ilya Dryomov922dab62016-05-26 01:15:02 +020089static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020090#endif
91
Sage Weilf24e9982009-10-06 11:31:10 -070092/*
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 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060099static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -0500100 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -0700101{
Alex Elder60e56f12013-02-15 11:42:29 -0600102 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -0700103 int r;
Sage Weilf24e9982009-10-06 11:31:10 -0700104
Alex Elder60e56f12013-02-15 11:42:29 -0600105 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -0500106 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
107 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -0700108 if (r < 0)
109 return r;
Alex Elder75d1c942013-03-13 20:50:00 -0500110 if (*objlen < orig_len) {
111 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -0600112 dout(" skipping last %llu, final file extent %llu~%llu\n",
113 orig_len - *plen, off, *plen);
114 }
115
Alex Elder75d1c942013-03-13 20:50:00 -0500116 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -0700117
Alex Elder3ff5f382013-02-15 22:10:17 -0600118 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700119}
120
Alex Elderc54d47b2013-04-03 01:28:57 -0500121static 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
Alex Eldera4ce40a2013-04-05 01:27:12 -0500127static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500128 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}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500138
Alex Eldera4ce40a2013-04-05 01:27:12 -0500139static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500140 struct ceph_pagelist *pagelist)
141{
142 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
143 osd_data->pagelist = pagelist;
144}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500145
146#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500147static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500148 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}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500154#endif /* CONFIG_BLOCK */
155
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300156#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})
Alex Elder863c7eb2013-04-15 14:50:36 -0500163
Alex Elder49719772013-02-11 12:33:24 -0600164static 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
Alex Eldera4ce40a2013-04-05 01:27:12 -0500172struct ceph_osd_data *
173osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500174 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500175{
Alex Elder863c7eb2013-04-15 14:50:36 -0500176 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500177}
178EXPORT_SYMBOL(osd_req_op_extent_osd_data);
179
Alex Elder49719772013-02-11 12:33:24 -0600180void 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
Alex Eldera4ce40a2013-04-05 01:27:12 -0500193void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500194 unsigned int which, struct page **pages,
195 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500196 bool pages_from_pool, bool own_pages)
197{
198 struct ceph_osd_data *osd_data;
199
Alex Elder863c7eb2013-04-15 14:50:36 -0500200 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500201 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
202 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500203}
204EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
205
206void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500207 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500208{
209 struct ceph_osd_data *osd_data;
210
Alex Elder863c7eb2013-04-15 14:50:36 -0500211 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500212 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500213}
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,
Alex Elder406e2c92013-04-15 14:50:36 -0500218 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500219{
220 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500221
222 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500223 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500224}
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
Alex Elder863c7eb2013-04-15 14:50:36 -0500234 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500235 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500236}
237
Alex Elder04017e22013-04-05 14:46:02 -0500238void 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
Alex Elder863c7eb2013-04-15 14:50:36 -0500244 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500245 ceph_osd_data_pagelist_init(osd_data, pagelist);
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200246 osd_req->r_ops[which].cls.indata_len += pagelist->length;
247 osd_req->r_ops[which].indata_len += pagelist->length;
Alex Elder04017e22013-04-05 14:46:02 -0500248}
249EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
250
Alex Elder6c57b552013-04-19 15:34:49 -0500251void 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);
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200260 osd_req->r_ops[which].cls.indata_len += length;
261 osd_req->r_ops[which].indata_len += length;
Alex Elder6c57b552013-04-19 15:34:49 -0500262}
263EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
264
Alex Eldera4ce40a2013-04-05 01:27:12 -0500265void 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
Alex Elder863c7eb2013-04-15 14:50:36 -0500271 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500272 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
273 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500274}
275EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
276
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500277static 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
Alex Elderc54d47b2013-04-03 01:28:57 -0500296static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
297{
Alex Elder54764922013-04-05 01:27:12 -0500298 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500299 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 }
Alex Elder54764922013-04-05 01:27:12 -0500305 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:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200319 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500320 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);
Alex Elder04017e22013-04-05 14:46:02 -0500324 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500325 ceph_osd_data_release(&op->cls.response_data);
326 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800327 case CEPH_OSD_OP_SETXATTR:
328 case CEPH_OSD_OP_CMPXATTR:
329 ceph_osd_data_release(&op->xattr.osd_data);
330 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800331 case CEPH_OSD_OP_STAT:
332 ceph_osd_data_release(&op->raw_data_in);
333 break;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200334 case CEPH_OSD_OP_NOTIFY_ACK:
335 ceph_osd_data_release(&op->notify_ack.request_data);
336 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200337 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;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700341 case CEPH_OSD_OP_LIST_WATCHERS:
342 ceph_osd_data_release(&op->list_watchers.response_data);
343 break;
Alex Elder54764922013-04-05 01:27:12 -0500344 default:
345 break;
346 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500347}
348
Sage Weilf24e9982009-10-06 11:31:10 -0700349/*
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200350 * 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
Ilya Dryomov922dab62016-05-26 01:15:02 +0200367static 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
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200390static void target_destroy(struct ceph_osd_request_target *t)
391{
392 ceph_oid_destroy(&t->base_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800393 ceph_oloc_destroy(&t->base_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200394 ceph_oid_destroy(&t->target_oid);
Yan, Zheng30c156d2016-02-14 11:24:31 +0800395 ceph_oloc_destroy(&t->target_oloc);
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200396}
397
398/*
Sage Weilf24e9982009-10-06 11:31:10 -0700399 * requests
400 */
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200401static void request_release_checks(struct ceph_osd_request *req)
402{
403 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
Ilya Dryomov46092452016-04-28 16:07:27 +0200404 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200405 WARN_ON(!list_empty(&req->r_unsafe_item));
406 WARN_ON(req->r_osd);
407}
408
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400409static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700410{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400411 struct ceph_osd_request *req = container_of(kref,
412 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500413 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800414
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400415 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
416 req->r_request, req->r_reply);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200417 request_release_checks(req);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400418
Sage Weil415e49a2009-12-07 13:37:03 -0800419 if (req->r_request)
420 ceph_msg_put(req->r_request);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200421 if (req->r_reply)
Alex Elderab8cb342012-06-04 14:43:32 -0500422 ceph_msg_put(req->r_reply);
Alex Elder0fff87e2013-02-14 12:16:43 -0600423
Alex Elder54764922013-04-05 01:27:12 -0500424 for (which = 0; which < req->r_num_ops; which++)
425 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600426
Ilya Dryomova66dd382016-04-28 16:07:23 +0200427 target_destroy(&req->r_t);
Sage Weil415e49a2009-12-07 13:37:03 -0800428 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200429
Sage Weil415e49a2009-12-07 13:37:03 -0800430 if (req->r_mempool)
431 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100432 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500433 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100434 else
435 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700436}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400437
438void ceph_osdc_get_request(struct ceph_osd_request *req)
439{
440 dout("%s %p (was %d)\n", __func__, req,
441 atomic_read(&req->r_kref.refcount));
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{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200448 if (req) {
449 dout("%s %p (was %d)\n", __func__, req,
450 atomic_read(&req->r_kref.refcount));
451 kref_put(&req->r_kref, ceph_osdc_release_request);
452 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400453}
454EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700455
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200456static 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);
Ilya Dryomovc297eb422016-12-02 14:01:55 +0100463 init_completion(&req->r_done_completion);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200464 RB_CLEAR_NODE(&req->r_node);
Ilya Dryomov46092452016-04-28 16:07:27 +0200465 RB_CLEAR_NODE(&req->r_mc_node);
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200466 INIT_LIST_HEAD(&req->r_unsafe_item);
467
468 target_init(&req->r_t);
469}
470
Ilya Dryomov922dab62016-05-26 01:15:02 +0200471/*
472 * This is ugly, but it allows us to reuse linger registration and ping
473 * requests, keeping the structure of the code around send_linger{_ping}()
474 * reasonable. Setting up a min_nr=2 mempool for each linger request
475 * and dealing with copying ops (this blasts req only, watch op remains
476 * intact) isn't any better.
477 */
478static void request_reinit(struct ceph_osd_request *req)
479{
480 struct ceph_osd_client *osdc = req->r_osdc;
481 bool mempool = req->r_mempool;
482 unsigned int num_ops = req->r_num_ops;
483 u64 snapid = req->r_snapid;
484 struct ceph_snap_context *snapc = req->r_snapc;
485 bool linger = req->r_linger;
486 struct ceph_msg *request_msg = req->r_request;
487 struct ceph_msg *reply_msg = req->r_reply;
488
489 dout("%s req %p\n", __func__, req);
490 WARN_ON(atomic_read(&req->r_kref.refcount) != 1);
491 request_release_checks(req);
492
493 WARN_ON(atomic_read(&request_msg->kref.refcount) != 1);
494 WARN_ON(atomic_read(&reply_msg->kref.refcount) != 1);
495 target_destroy(&req->r_t);
496
497 request_init(req);
498 req->r_osdc = osdc;
499 req->r_mempool = mempool;
500 req->r_num_ops = num_ops;
501 req->r_snapid = snapid;
502 req->r_snapc = snapc;
503 req->r_linger = linger;
504 req->r_request = request_msg;
505 req->r_reply = reply_msg;
506}
507
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700508struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700509 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800510 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700511 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600512 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700513{
514 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700515
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700516 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100517 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700518 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100519 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
520 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700521 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100522 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
523 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
524 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700525 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100526 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700527 return NULL;
528
Ilya Dryomov3540bfd2016-04-28 16:07:26 +0200529 request_init(req);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700530 req->r_osdc = osdc;
531 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500532 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200533 req->r_snapid = CEPH_NOSNAP;
534 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700535
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200536 dout("%s req %p\n", __func__, req);
537 return req;
538}
539EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100540
Yan, Zheng30c156d2016-02-14 11:24:31 +0800541static int ceph_oloc_encoding_size(struct ceph_object_locator *oloc)
542{
543 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
544}
545
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200546int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
547{
548 struct ceph_osd_client *osdc = req->r_osdc;
549 struct ceph_msg *msg;
550 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700551
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200552 WARN_ON(ceph_oid_empty(&req->r_base_oid));
Yan, Zheng30c156d2016-02-14 11:24:31 +0800553 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200554
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200555 /* create request message */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100556 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
557 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
Yan, Zheng30c156d2016-02-14 11:24:31 +0800558 msg_size += CEPH_ENCODING_START_BLK_LEN +
559 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100560 msg_size += 1 + 8 + 4 + 4; /* pgid */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200561 msg_size += 4 + req->r_base_oid.name_len; /* oid */
562 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100563 msg_size += 8; /* snapid */
564 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200565 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100566 msg_size += 4; /* retry_attempt */
567
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200568 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700569 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
570 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200571 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
572 if (!msg)
573 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700574
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700575 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700576 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700577
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200578 /* create reply message */
579 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200580 msg_size += req->r_base_oid.name_len;
581 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200582
583 if (req->r_mempool)
584 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
585 else
586 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
587 if (!msg)
588 return -ENOMEM;
589
590 req->r_reply = msg;
591
592 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700593}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200594EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700595
Alex Eldera8dd0a32013-03-13 20:50:00 -0500596static bool osd_req_opcode_valid(u16 opcode)
597{
598 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400599#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
600__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
601#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500602 default:
603 return false;
604 }
605}
606
Alex Elder33803f32013-03-13 20:50:00 -0500607/*
608 * This is an osd op init function for opcodes that have no data or
609 * other information associated with them. It also serves as a
610 * common init routine for all the other init functions, below.
611 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500612static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600613_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800614 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500615{
Alex Elderc99d2d42013-04-05 01:27:11 -0500616 struct ceph_osd_req_op *op;
617
618 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500619 BUG_ON(!osd_req_opcode_valid(opcode));
620
Alex Elderc99d2d42013-04-05 01:27:11 -0500621 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500622 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500623 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800624 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500625
626 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500627}
628
Alex Elder49719772013-02-11 12:33:24 -0600629void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800630 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600631{
Yan, Zheng144cba12015-04-27 11:09:54 +0800632 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600633}
634EXPORT_SYMBOL(osd_req_op_init);
635
Alex Elderc99d2d42013-04-05 01:27:11 -0500636void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
637 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500638 u64 offset, u64 length,
639 u64 truncate_size, u32 truncate_seq)
640{
Yan, Zheng144cba12015-04-27 11:09:54 +0800641 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
642 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500643 size_t payload_len = 0;
644
Li Wangad7a60d2013-08-15 11:51:44 +0800645 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200646 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
647 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500648
Alex Elder33803f32013-03-13 20:50:00 -0500649 op->extent.offset = offset;
650 op->extent.length = length;
651 op->extent.truncate_size = truncate_size;
652 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200653 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500654 payload_len += length;
655
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100656 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500657}
658EXPORT_SYMBOL(osd_req_op_extent_init);
659
Alex Elderc99d2d42013-04-05 01:27:11 -0500660void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
661 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500662{
Alex Elderc99d2d42013-04-05 01:27:11 -0500663 struct ceph_osd_req_op *op;
664 u64 previous;
665
666 BUG_ON(which >= osd_req->r_num_ops);
667 op = &osd_req->r_ops[which];
668 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500669
670 if (length == previous)
671 return; /* Nothing to do */
672 BUG_ON(length > previous);
673
674 op->extent.length = length;
Yan, Zhengd641df82017-01-19 11:21:29 +0800675 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
676 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500677}
678EXPORT_SYMBOL(osd_req_op_extent_update);
679
Yan, Zheng2c63f492016-01-07 17:32:54 +0800680void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
681 unsigned int which, u64 offset_inc)
682{
683 struct ceph_osd_req_op *op, *prev_op;
684
685 BUG_ON(which + 1 >= osd_req->r_num_ops);
686
687 prev_op = &osd_req->r_ops[which];
688 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
689 /* dup previous one */
690 op->indata_len = prev_op->indata_len;
691 op->outdata_len = prev_op->outdata_len;
692 op->extent = prev_op->extent;
693 /* adjust offset */
694 op->extent.offset += offset_inc;
695 op->extent.length -= offset_inc;
696
697 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
698 op->indata_len -= offset_inc;
699}
700EXPORT_SYMBOL(osd_req_op_extent_dup_last);
701
Alex Elderc99d2d42013-04-05 01:27:11 -0500702void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500703 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500704{
Yan, Zheng144cba12015-04-27 11:09:54 +0800705 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
706 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500707 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500708 size_t payload_len = 0;
709 size_t size;
710
711 BUG_ON(opcode != CEPH_OSD_OP_CALL);
712
Alex Elder5f562df2013-04-05 01:27:12 -0500713 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
714 BUG_ON(!pagelist);
715 ceph_pagelist_init(pagelist);
716
Alex Elder33803f32013-03-13 20:50:00 -0500717 op->cls.class_name = class;
718 size = strlen(class);
719 BUG_ON(size > (size_t) U8_MAX);
720 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500721 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500722 payload_len += size;
723
724 op->cls.method_name = method;
725 size = strlen(method);
726 BUG_ON(size > (size_t) U8_MAX);
727 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500728 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500729 payload_len += size;
730
Alex Eldera4ce40a2013-04-05 01:27:12 -0500731 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500732
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100733 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500734}
735EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500736
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800737int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
738 u16 opcode, const char *name, const void *value,
739 size_t size, u8 cmp_op, u8 cmp_mode)
740{
Yan, Zheng144cba12015-04-27 11:09:54 +0800741 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
742 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800743 struct ceph_pagelist *pagelist;
744 size_t payload_len;
745
746 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
747
748 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
749 if (!pagelist)
750 return -ENOMEM;
751
752 ceph_pagelist_init(pagelist);
753
754 payload_len = strlen(name);
755 op->xattr.name_len = payload_len;
756 ceph_pagelist_append(pagelist, name, payload_len);
757
758 op->xattr.value_len = size;
759 ceph_pagelist_append(pagelist, value, size);
760 payload_len += size;
761
762 op->xattr.cmp_op = cmp_op;
763 op->xattr.cmp_mode = cmp_mode;
764
765 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100766 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800767 return 0;
768}
769EXPORT_SYMBOL(osd_req_op_xattr_init);
770
Ilya Dryomov922dab62016-05-26 01:15:02 +0200771/*
772 * @watch_opcode: CEPH_OSD_WATCH_OP_*
773 */
774static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
775 u64 cookie, u8 watch_opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500776{
Ilya Dryomov922dab62016-05-26 01:15:02 +0200777 struct ceph_osd_req_op *op;
Alex Elder33803f32013-03-13 20:50:00 -0500778
Ilya Dryomov922dab62016-05-26 01:15:02 +0200779 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500780 op->watch.cookie = cookie;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200781 op->watch.op = watch_opcode;
782 op->watch.gen = 0;
Alex Elder33803f32013-03-13 20:50:00 -0500783}
Alex Elder33803f32013-03-13 20:50:00 -0500784
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200785void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
786 unsigned int which,
787 u64 expected_object_size,
788 u64 expected_write_size)
789{
790 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800791 CEPH_OSD_OP_SETALLOCHINT,
792 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200793
794 op->alloc_hint.expected_object_size = expected_object_size;
795 op->alloc_hint.expected_write_size = expected_write_size;
796
797 /*
798 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
799 * not worth a feature bit. Set FAILOK per-op flag to make
800 * sure older osds don't trip over an unsupported opcode.
801 */
802 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
803}
804EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
805
Alex Elder90af3602013-04-05 14:46:01 -0500806static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500807 struct ceph_osd_data *osd_data)
808{
809 u64 length = ceph_osd_data_length(osd_data);
810
811 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
812 BUG_ON(length > (u64) SIZE_MAX);
813 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500814 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500815 length, osd_data->alignment);
816 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
817 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500818 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500819#ifdef CONFIG_BLOCK
820 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500821 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500822#endif
823 } else {
824 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
825 }
826}
827
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200828static u32 osd_req_encode_op(struct ceph_osd_op *dst,
829 const struct ceph_osd_req_op *src)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700830{
Alex Eldera8dd0a32013-03-13 20:50:00 -0500831 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
832 pr_err("unrecognized osd opcode %d\n", src->op);
833
834 return 0;
835 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700836
Alex Elder065a68f2012-04-20 15:49:43 -0500837 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600838 case CEPH_OSD_OP_STAT:
839 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700840 case CEPH_OSD_OP_READ:
841 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200842 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800843 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800844 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600845 dst->extent.offset = cpu_to_le64(src->extent.offset);
846 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700847 dst->extent.truncate_size =
848 cpu_to_le64(src->extent.truncate_size);
849 dst->extent.truncate_seq =
850 cpu_to_le32(src->extent.truncate_seq);
851 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700852 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700853 dst->cls.class_len = src->cls.class_len;
854 dst->cls.method_len = src->cls.method_len;
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200855 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700856 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700857 case CEPH_OSD_OP_STARTSYNC:
858 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700859 case CEPH_OSD_OP_WATCH:
860 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
Ilya Dryomov922dab62016-05-26 01:15:02 +0200861 dst->watch.ver = cpu_to_le64(0);
862 dst->watch.op = src->watch.op;
863 dst->watch.gen = cpu_to_le32(src->watch.gen);
864 break;
865 case CEPH_OSD_OP_NOTIFY_ACK:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700866 break;
Ilya Dryomov19079202016-04-28 16:07:27 +0200867 case CEPH_OSD_OP_NOTIFY:
868 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
869 break;
Douglas Fullera4ed38d2015-07-17 13:18:07 -0700870 case CEPH_OSD_OP_LIST_WATCHERS:
871 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200872 case CEPH_OSD_OP_SETALLOCHINT:
873 dst->alloc_hint.expected_object_size =
874 cpu_to_le64(src->alloc_hint.expected_object_size);
875 dst->alloc_hint.expected_write_size =
876 cpu_to_le64(src->alloc_hint.expected_write_size);
877 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800878 case CEPH_OSD_OP_SETXATTR:
879 case CEPH_OSD_OP_CMPXATTR:
880 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
881 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
882 dst->xattr.cmp_op = src->xattr.cmp_op;
883 dst->xattr.cmp_mode = src->xattr.cmp_mode;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800884 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800885 case CEPH_OSD_OP_CREATE:
886 case CEPH_OSD_OP_DELETE:
887 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700888 default:
Alex Elder4c464592013-02-15 11:42:30 -0600889 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600890 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600891 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500892
893 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700894 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200895
Alex Eldera8dd0a32013-03-13 20:50:00 -0500896 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200897 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100898 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600899
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200900 return src->indata_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700901}
902
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700903/*
Sage Weilf24e9982009-10-06 11:31:10 -0700904 * build new request AND message, calculate layout, and adjust file
905 * extent as needed.
906 *
907 * if the file was recently truncated, we include information about its
908 * old and new size so that the object can be updated appropriately. (we
909 * avoid synchronously deleting truncated objects because it's slow.)
910 *
911 * if @do_sync, include a 'startsync' command so that the osd will flush
912 * data quickly.
913 */
914struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
915 struct ceph_file_layout *layout,
916 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800917 u64 off, u64 *plen,
918 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700919 int opcode, int flags,
920 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700921 u32 truncate_seq,
922 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600923 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700924{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700925 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500926 u64 objnum = 0;
927 u64 objoff = 0;
928 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700929 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700930
Li Wangad7a60d2013-08-15 11:51:44 +0800931 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800932 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
933 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700934
Alex Elderacead002013-03-14 14:09:05 -0500935 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600936 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200937 if (!req) {
938 r = -ENOMEM;
939 goto fail;
940 }
Alex Elder79528732013-04-03 21:32:51 -0500941
Sage Weilf24e9982009-10-06 11:31:10 -0700942 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500943 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200944 if (r)
945 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -0500946
Yan, Zheng864e9192014-11-13 10:47:25 +0800947 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800948 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800949 } else {
Yan, Zheng76271512016-02-03 21:24:49 +0800950 u32 object_size = layout->object_size;
Yan, Zheng864e9192014-11-13 10:47:25 +0800951 u32 object_base = off - objoff;
952 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
953 if (truncate_size <= object_base) {
954 truncate_size = 0;
955 } else {
956 truncate_size -= object_base;
957 if (truncate_size > object_size)
958 truncate_size = object_size;
959 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800960 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800961 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800962 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500963 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500964
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200965 req->r_flags = flags;
Yan, Zheng76271512016-02-03 21:24:49 +0800966 req->r_base_oloc.pool = layout->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +0800967 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200968 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600969
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200970 req->r_snapid = vino.snap;
971 if (flags & CEPH_OSD_FLAG_WRITE)
972 req->r_data_offset = off;
973
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200974 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
975 if (r)
976 goto fail;
977
Sage Weilf24e9982009-10-06 11:31:10 -0700978 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200979
980fail:
981 ceph_osdc_put_request(req);
982 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700983}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700984EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700985
986/*
987 * We keep osd requests in an rbtree, sorted by ->r_tid.
988 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200989DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Ilya Dryomov46092452016-04-28 16:07:27 +0200990DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
Sage Weilf24e9982009-10-06 11:31:10 -0700991
Ilya Dryomov0247a0c2016-04-28 16:07:25 +0200992static bool osd_homeless(struct ceph_osd *osd)
993{
994 return osd->o_osd == CEPH_HOMELESS_OSD;
995}
996
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200997static bool osd_registered(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700998{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200999 verify_osdc_locked(osd->o_osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001000
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001001 return !RB_EMPTY_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001002}
1003
1004/*
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001005 * Assumes @osd is zero-initialized.
1006 */
1007static void osd_init(struct ceph_osd *osd)
1008{
1009 atomic_set(&osd->o_ref, 1);
1010 RB_CLEAR_NODE(&osd->o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001011 osd->o_requests = RB_ROOT;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001012 osd->o_linger_requests = RB_ROOT;
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001013 INIT_LIST_HEAD(&osd->o_osd_lru);
1014 INIT_LIST_HEAD(&osd->o_keepalive_item);
1015 osd->o_incarnation = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001016 mutex_init(&osd->lock);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001017}
1018
1019static void osd_cleanup(struct ceph_osd *osd)
1020{
1021 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001022 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02001023 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001024 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
1033/*
Sage Weilf24e9982009-10-06 11:31:10 -07001034 * Track open sessions with osds.
1035 */
Alex Eldere10006f2012-05-26 23:26:43 -05001036static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001037{
1038 struct ceph_osd *osd;
1039
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001040 WARN_ON(onum == CEPH_HOMELESS_OSD);
1041
Ilya Dryomov7a28f592016-04-28 16:07:25 +02001042 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001043 osd_init(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001044 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001045 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -07001046
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001047 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001048
Sage Weilf24e9982009-10-06 11:31:10 -07001049 return osd;
1050}
1051
1052static struct ceph_osd *get_osd(struct ceph_osd *osd)
1053{
1054 if (atomic_inc_not_zero(&osd->o_ref)) {
1055 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1056 atomic_read(&osd->o_ref));
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{
1066 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1067 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001068 if (atomic_dec_and_test(&osd->o_ref)) {
Ilya Dryomov0247a0c2016-04-28 16:07:25 +02001069 osd_cleanup(osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001070 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001071 }
Sage Weilf24e9982009-10-06 11:31:10 -07001072}
1073
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001074DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1075
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001076static void __move_osd_to_lru(struct ceph_osd *osd)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001077{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001078 struct ceph_osd_client *osdc = osd->o_osdc;
1079
1080 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001081 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001082
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001083 spin_lock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001084 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001085 spin_unlock(&osdc->osd_lru_lock);
1086
Ilya Dryomova319bf52015-05-15 12:02:17 +03001087 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001088}
1089
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001090static void maybe_move_osd_to_lru(struct ceph_osd *osd)
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001091{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001092 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001093 RB_EMPTY_ROOT(&osd->o_linger_requests))
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001094 __move_osd_to_lru(osd);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001095}
1096
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001097static void __remove_osd_from_lru(struct ceph_osd *osd)
1098{
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001099 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);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001104 if (!list_empty(&osd->o_osd_lru))
1105 list_del_init(&osd->o_osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02001106 spin_unlock(&osdc->osd_lru_lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001107}
1108
Sage Weilf24e9982009-10-06 11:31:10 -07001109/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001110 * 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 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02001133 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 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001144
1145 __remove_osd_from_lru(osd);
1146 erase_osd(&osdc->osds, osd);
1147 put_osd(osd);
1148}
1149
1150/*
Sage Weilf24e9982009-10-06 11:31:10 -07001151 * reset osd connect
1152 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001153static int reopen_osd(struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001154{
Alex Elderc3acb182012-12-07 09:57:58 -06001155 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001156
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001157 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1158
1159 if (RB_EMPTY_ROOT(&osd->o_requests) &&
Ilya Dryomov922dab62016-05-26 01:15:02 +02001160 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001161 close_osd(osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001162 return -ENODEV;
1163 }
1164
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001165 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
Alex Elderc3acb182012-12-07 09:57:58 -06001166 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1167 !ceph_con_opened(&osd->o_con)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001168 struct rb_node *n;
Alex Elderc3acb182012-12-07 09:57:58 -06001169
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001170 dout("osd addr hasn't changed and connection never opened, "
1171 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001172 /* touch each r_stamp for handle_timeout()'s benfit */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001173 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);
Sage Weil87b315a2010-03-22 14:51:18 -07001176 req->r_stamp = jiffies;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001177 }
Alex Elderc3acb182012-12-07 09:57:58 -06001178
1179 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001180 }
Alex Elderc3acb182012-12-07 09:57:58 -06001181
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;
Sage Weilf24e9982009-10-06 11:31:10 -07001187}
1188
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001189static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1190 bool wrlocked)
Sage Weilf24e9982009-10-06 11:31:10 -07001191{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001192 struct ceph_osd *osd;
1193
1194 if (wrlocked)
1195 verify_osdc_wrlocked(osdc);
1196 else
1197 verify_osdc_locked(osdc);
1198
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);
1206
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]);
1211 }
1212
1213 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1214 return osd;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001215}
1216
Sage Weilf24e9982009-10-06 11:31:10 -07001217/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001218 * Create request <-> OSD session relation.
1219 *
1220 * @req has to be assigned a tid, @osd may be homeless.
Sage Weilf24e9982009-10-06 11:31:10 -07001221 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001222static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001223{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001224 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);
Sage Weil35f9f8a2012-05-16 15:16:38 -05001228
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001229 if (!osd_homeless(osd))
1230 __remove_osd_from_lru(osd);
1231 else
1232 atomic_inc(&osd->o_osdc->num_homeless);
Sage Weilf24e9982009-10-06 11:31:10 -07001233
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001234 get_osd(osd);
1235 insert_request(&osd->o_requests, req);
1236 req->r_osd = osd;
Sage Weilf24e9982009-10-06 11:31:10 -07001237}
1238
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001239static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001240{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001241 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);
Sage Weilf24e9982009-10-06 11:31:10 -07001254}
1255
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001256static bool __pool_full(struct ceph_pg_pool_info *pi)
1257{
1258 return pi->flags & CEPH_POOL_FLAG_FULL;
1259}
1260
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001261static 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
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001276static 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
Sage Weilf24e9982009-10-06 11:31:10 -07001287/*
Josh Durgind29adb32013-12-02 19:11:48 -08001288 * Returns whether a request should be blocked from being sent
1289 * based on the current osdmap and osd_client settings.
Josh Durgind29adb32013-12-02 19:11:48 -08001290 */
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001291static 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{
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001295 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) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001298 __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
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001305enum 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;
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001322 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001323 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
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001414static void setup_request_data(struct ceph_osd_request *req,
1415 struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001416{
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001417 u32 data_len = 0;
1418 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001419
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001420 if (!list_empty(&msg->data))
1421 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001422
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001423 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;
Ilya Dryomov922dab62016-05-26 01:15:02 +02001440 case CEPH_OSD_OP_NOTIFY_ACK:
1441 ceph_osdc_msg_data_add(msg,
1442 &op->notify_ack.request_data);
1443 break;
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001444
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;
Douglas Fullera4ed38d2015-07-17 13:18:07 -07001454 case CEPH_OSD_OP_LIST_WATCHERS:
1455 ceph_osdc_msg_data_add(req->r_reply,
1456 &op->list_watchers.response_data);
1457 break;
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001458
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;
Ilya Dryomov19079202016-04-28 16:07:27 +02001471 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;
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001477 }
1478
1479 data_len += op->indata_len;
1480 }
1481
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);
1507 /* aka reassert_version */
1508 memcpy(p, &req->r_replay_version, sizeof(req->r_replay_version));
1509 p += sizeof(req->r_replay_version);
1510
1511 /* oloc */
Yan, Zheng30c156d2016-02-14 11:24:31 +08001512 ceph_start_encoding(&p, 5, 4,
1513 ceph_oloc_encoding_size(&req->r_t.target_oloc));
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001514 ceph_encode_64(&p, req->r_t.target_oloc.pool);
1515 ceph_encode_32(&p, -1); /* preferred */
1516 ceph_encode_32(&p, 0); /* key len */
Yan, Zheng30c156d2016-02-14 11:24:31 +08001517 if (req->r_t.target_oloc.pool_ns)
1518 ceph_encode_string(&p, end, req->r_t.target_oloc.pool_ns->str,
1519 req->r_t.target_oloc.pool_ns->len);
1520 else
1521 ceph_encode_32(&p, 0);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001522
1523 /* pgid */
1524 ceph_encode_8(&p, 1);
Ilya Dryomova66dd382016-04-28 16:07:23 +02001525 ceph_encode_64(&p, req->r_t.pgid.pool);
1526 ceph_encode_32(&p, req->r_t.pgid.seed);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001527 ceph_encode_32(&p, -1); /* preferred */
Sage Weil2169aea2013-02-25 16:13:08 -08001528
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001529 /* oid */
1530 ceph_encode_32(&p, req->r_t.target_oid.name_len);
1531 memcpy(p, req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1532 p += req->r_t.target_oid.name_len;
1533
1534 /* ops, can imply data */
1535 ceph_encode_16(&p, req->r_num_ops);
1536 for (i = 0; i < req->r_num_ops; i++) {
1537 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1538 p += sizeof(struct ceph_osd_op);
1539 }
1540
1541 ceph_encode_64(&p, req->r_snapid); /* snapid */
1542 if (req->r_snapc) {
1543 ceph_encode_64(&p, req->r_snapc->seq);
1544 ceph_encode_32(&p, req->r_snapc->num_snaps);
1545 for (i = 0; i < req->r_snapc->num_snaps; i++)
1546 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1547 } else {
1548 ceph_encode_64(&p, 0); /* snap_seq */
1549 ceph_encode_32(&p, 0); /* snaps len */
1550 }
1551
1552 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1553
1554 BUG_ON(p > end);
1555 msg->front.iov_len = p - msg->front.iov_base;
1556 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1557 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1558 msg->hdr.data_len = cpu_to_le32(data_len);
1559 /*
1560 * The header "data_off" is a hint to the receiver allowing it
1561 * to align received data into its buffers such that there's no
1562 * need to re-copy it before writing it to disk (direct I/O).
1563 */
1564 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1565
Ilya Dryomov4a3262b2016-05-30 18:33:32 +02001566 dout("%s req %p oid %s oid_len %d front %zu data %u\n", __func__,
1567 req, req->r_t.target_oid.name, req->r_t.target_oid.name_len,
1568 msg->front.iov_len, data_len);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001569}
1570
1571/*
1572 * @req has to be assigned a tid and registered.
1573 */
1574static void send_request(struct ceph_osd_request *req)
1575{
1576 struct ceph_osd *osd = req->r_osd;
1577
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001578 verify_osd_locked(osd);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001579 WARN_ON(osd->o_osd != req->r_t.osd);
1580
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001581 /*
1582 * We may have a previously queued request message hanging
1583 * around. Cancel it to avoid corrupting the msgr.
1584 */
1585 if (req->r_sent)
1586 ceph_msg_revoke(req->r_request);
1587
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001588 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1589 if (req->r_attempts)
1590 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1591 else
1592 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1593
1594 encode_request(req, req->r_request);
1595
1596 dout("%s req %p tid %llu to pg %llu.%x osd%d flags 0x%x attempt %d\n",
1597 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
1598 req->r_t.osd, req->r_flags, req->r_attempts);
1599
1600 req->r_t.paused = false;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001601 req->r_stamp = jiffies;
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001602 req->r_attempts++;
Sage Weilf24e9982009-10-06 11:31:10 -07001603
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001604 req->r_sent = osd->o_incarnation;
1605 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1606 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
Sage Weilf24e9982009-10-06 11:31:10 -07001607}
1608
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001609static void maybe_request_map(struct ceph_osd_client *osdc)
1610{
1611 bool continuous = false;
1612
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001613 verify_osdc_locked(osdc);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001614 WARN_ON(!osdc->osdmap->epoch);
1615
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001616 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1617 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
1618 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov42c1b122016-04-28 16:07:25 +02001619 dout("%s osdc %p continuous\n", __func__, osdc);
1620 continuous = true;
1621 } else {
1622 dout("%s osdc %p onetime\n", __func__, osdc);
1623 }
1624
1625 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
1626 osdc->osdmap->epoch + 1, continuous))
1627 ceph_monc_renew_subs(&osdc->client->monc);
1628}
1629
Ilya Dryomov46092452016-04-28 16:07:27 +02001630static void send_map_check(struct ceph_osd_request *req);
1631
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001632static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001633{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001634 struct ceph_osd_client *osdc = req->r_osdc;
1635 struct ceph_osd *osd;
Ilya Dryomov46092452016-04-28 16:07:27 +02001636 enum calc_target_result ct_res;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001637 bool need_send = false;
1638 bool promoted = false;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001639
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001640 WARN_ON(req->r_tid || req->r_got_reply);
1641 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
1642
1643again:
Ilya Dryomov46092452016-04-28 16:07:27 +02001644 ct_res = calc_target(osdc, &req->r_t, &req->r_last_force_resend, false);
1645 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
1646 goto promote;
1647
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001648 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
1649 if (IS_ERR(osd)) {
1650 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
1651 goto promote;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001652 }
1653
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001654 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001655 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001656 dout("req %p pausewr\n", req);
1657 req->r_t.paused = true;
1658 maybe_request_map(osdc);
1659 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001660 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001661 dout("req %p pauserd\n", req);
1662 req->r_t.paused = true;
1663 maybe_request_map(osdc);
1664 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1665 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
1666 CEPH_OSD_FLAG_FULL_FORCE)) &&
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02001667 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001668 pool_full(osdc, req->r_t.base_oloc.pool))) {
1669 dout("req %p full/pool_full\n", req);
1670 pr_warn_ratelimited("FULL or reached pool quota\n");
1671 req->r_t.paused = true;
1672 maybe_request_map(osdc);
1673 } else if (!osd_homeless(osd)) {
1674 need_send = true;
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001675 } else {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001676 maybe_request_map(osdc);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001677 }
1678
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001679 mutex_lock(&osd->lock);
1680 /*
1681 * Assign the tid atomically with send_request() to protect
1682 * multiple writes to the same object from racing with each
1683 * other, resulting in out of order ops on the OSDs.
1684 */
1685 req->r_tid = atomic64_inc_return(&osdc->last_tid);
1686 link_request(osd, req);
1687 if (need_send)
1688 send_request(req);
1689 mutex_unlock(&osd->lock);
1690
Ilya Dryomov46092452016-04-28 16:07:27 +02001691 if (ct_res == CALC_TARGET_POOL_DNE)
1692 send_map_check(req);
1693
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001694 if (promoted)
1695 downgrade_write(&osdc->lock);
1696 return;
1697
1698promote:
1699 up_read(&osdc->lock);
1700 down_write(&osdc->lock);
1701 wrlocked = true;
1702 promoted = true;
1703 goto again;
1704}
1705
1706static void account_request(struct ceph_osd_request *req)
1707{
1708 unsigned int mask = CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK;
1709
1710 if (req->r_flags & CEPH_OSD_FLAG_READ) {
1711 WARN_ON(req->r_flags & mask);
1712 req->r_flags |= CEPH_OSD_FLAG_ACK;
1713 } else if (req->r_flags & CEPH_OSD_FLAG_WRITE)
1714 WARN_ON(!(req->r_flags & mask));
1715 else
1716 WARN_ON(1);
1717
1718 WARN_ON(req->r_unsafe_callback && (req->r_flags & mask) != mask);
1719 atomic_inc(&req->r_osdc->num_requests);
1720}
1721
1722static void submit_request(struct ceph_osd_request *req, bool wrlocked)
1723{
1724 ceph_osdc_get_request(req);
1725 account_request(req);
1726 __submit_request(req, wrlocked);
1727}
1728
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01001729static void finish_request(struct ceph_osd_request *req)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001730{
1731 struct ceph_osd_client *osdc = req->r_osdc;
1732 struct ceph_osd *osd = req->r_osd;
1733
1734 verify_osd_locked(osd);
1735 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1736
Ilya Dryomov46092452016-04-28 16:07:27 +02001737 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001738 unlink_request(osd, req);
1739 atomic_dec(&osdc->num_requests);
1740
1741 /*
1742 * If an OSD has failed or returned and a request has been sent
1743 * twice, it's possible to get a reply and end up here while the
1744 * request message is queued for delivery. We will ignore the
1745 * reply, so not a big deal, but better to try and catch it.
1746 */
1747 ceph_msg_revoke(req->r_request);
1748 ceph_msg_revoke_incoming(req->r_reply);
1749}
1750
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001751static void __complete_request(struct ceph_osd_request *req)
1752{
1753 if (req->r_callback)
1754 req->r_callback(req);
1755 else
1756 complete_all(&req->r_completion);
1757}
1758
Ilya Dryomov46092452016-04-28 16:07:27 +02001759/*
1760 * Note that this is open-coded in handle_reply(), which has to deal
1761 * with ack vs commit, dup acks, etc.
1762 */
1763static void complete_request(struct ceph_osd_request *req, int err)
1764{
1765 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1766
1767 req->r_result = err;
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01001768 finish_request(req);
Ilya Dryomov46092452016-04-28 16:07:27 +02001769 __complete_request(req);
Ilya Dryomovc297eb422016-12-02 14:01:55 +01001770 complete_all(&req->r_done_completion);
Ilya Dryomov46092452016-04-28 16:07:27 +02001771 ceph_osdc_put_request(req);
1772}
1773
1774static void cancel_map_check(struct ceph_osd_request *req)
1775{
1776 struct ceph_osd_client *osdc = req->r_osdc;
1777 struct ceph_osd_request *lookup_req;
1778
1779 verify_osdc_wrlocked(osdc);
1780
1781 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1782 if (!lookup_req)
1783 return;
1784
1785 WARN_ON(lookup_req != req);
1786 erase_request_mc(&osdc->map_checks, req);
1787 ceph_osdc_put_request(req);
1788}
1789
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001790static void cancel_request(struct ceph_osd_request *req)
1791{
1792 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1793
Ilya Dryomov46092452016-04-28 16:07:27 +02001794 cancel_map_check(req);
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01001795 finish_request(req);
Ilya Dryomovc297eb422016-12-02 14:01:55 +01001796 complete_all(&req->r_done_completion);
1797 ceph_osdc_put_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02001798}
1799
Ilya Dryomov46092452016-04-28 16:07:27 +02001800static void check_pool_dne(struct ceph_osd_request *req)
1801{
1802 struct ceph_osd_client *osdc = req->r_osdc;
1803 struct ceph_osdmap *map = osdc->osdmap;
1804
1805 verify_osdc_wrlocked(osdc);
1806 WARN_ON(!map->epoch);
1807
1808 if (req->r_attempts) {
1809 /*
1810 * We sent a request earlier, which means that
1811 * previously the pool existed, and now it does not
1812 * (i.e., it was deleted).
1813 */
1814 req->r_map_dne_bound = map->epoch;
1815 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
1816 req->r_tid);
1817 } else {
1818 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
1819 req, req->r_tid, req->r_map_dne_bound, map->epoch);
1820 }
1821
1822 if (req->r_map_dne_bound) {
1823 if (map->epoch >= req->r_map_dne_bound) {
1824 /* we had a new enough map */
1825 pr_info_ratelimited("tid %llu pool does not exist\n",
1826 req->r_tid);
1827 complete_request(req, -ENOENT);
1828 }
1829 } else {
1830 send_map_check(req);
1831 }
1832}
1833
1834static void map_check_cb(struct ceph_mon_generic_request *greq)
1835{
1836 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
1837 struct ceph_osd_request *req;
1838 u64 tid = greq->private_data;
1839
1840 WARN_ON(greq->result || !greq->u.newest);
1841
1842 down_write(&osdc->lock);
1843 req = lookup_request_mc(&osdc->map_checks, tid);
1844 if (!req) {
1845 dout("%s tid %llu dne\n", __func__, tid);
1846 goto out_unlock;
1847 }
1848
1849 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
1850 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
1851 if (!req->r_map_dne_bound)
1852 req->r_map_dne_bound = greq->u.newest;
1853 erase_request_mc(&osdc->map_checks, req);
1854 check_pool_dne(req);
1855
1856 ceph_osdc_put_request(req);
1857out_unlock:
1858 up_write(&osdc->lock);
1859}
1860
1861static void send_map_check(struct ceph_osd_request *req)
1862{
1863 struct ceph_osd_client *osdc = req->r_osdc;
1864 struct ceph_osd_request *lookup_req;
1865 int ret;
1866
1867 verify_osdc_wrlocked(osdc);
1868
1869 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1870 if (lookup_req) {
1871 WARN_ON(lookup_req != req);
1872 return;
1873 }
1874
1875 ceph_osdc_get_request(req);
1876 insert_request_mc(&osdc->map_checks, req);
1877 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
1878 map_check_cb, req->r_tid);
1879 WARN_ON(ret);
1880}
1881
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001882/*
Ilya Dryomov922dab62016-05-26 01:15:02 +02001883 * lingering requests, watch/notify v2 infrastructure
1884 */
1885static void linger_release(struct kref *kref)
1886{
1887 struct ceph_osd_linger_request *lreq =
1888 container_of(kref, struct ceph_osd_linger_request, kref);
1889
1890 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
1891 lreq->reg_req, lreq->ping_req);
1892 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
1893 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
Ilya Dryomov46092452016-04-28 16:07:27 +02001894 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
Ilya Dryomov922dab62016-05-26 01:15:02 +02001895 WARN_ON(!list_empty(&lreq->scan_item));
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02001896 WARN_ON(!list_empty(&lreq->pending_lworks));
Ilya Dryomov922dab62016-05-26 01:15:02 +02001897 WARN_ON(lreq->osd);
1898
1899 if (lreq->reg_req)
1900 ceph_osdc_put_request(lreq->reg_req);
1901 if (lreq->ping_req)
1902 ceph_osdc_put_request(lreq->ping_req);
1903 target_destroy(&lreq->t);
1904 kfree(lreq);
1905}
1906
1907static void linger_put(struct ceph_osd_linger_request *lreq)
1908{
1909 if (lreq)
1910 kref_put(&lreq->kref, linger_release);
1911}
1912
1913static struct ceph_osd_linger_request *
1914linger_get(struct ceph_osd_linger_request *lreq)
1915{
1916 kref_get(&lreq->kref);
1917 return lreq;
1918}
1919
1920static struct ceph_osd_linger_request *
1921linger_alloc(struct ceph_osd_client *osdc)
1922{
1923 struct ceph_osd_linger_request *lreq;
1924
1925 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
1926 if (!lreq)
1927 return NULL;
1928
1929 kref_init(&lreq->kref);
1930 mutex_init(&lreq->lock);
1931 RB_CLEAR_NODE(&lreq->node);
1932 RB_CLEAR_NODE(&lreq->osdc_node);
Ilya Dryomov46092452016-04-28 16:07:27 +02001933 RB_CLEAR_NODE(&lreq->mc_node);
Ilya Dryomov922dab62016-05-26 01:15:02 +02001934 INIT_LIST_HEAD(&lreq->scan_item);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02001935 INIT_LIST_HEAD(&lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02001936 init_completion(&lreq->reg_commit_wait);
Ilya Dryomov19079202016-04-28 16:07:27 +02001937 init_completion(&lreq->notify_finish_wait);
Ilya Dryomov922dab62016-05-26 01:15:02 +02001938
1939 lreq->osdc = osdc;
1940 target_init(&lreq->t);
1941
1942 dout("%s lreq %p\n", __func__, lreq);
1943 return lreq;
1944}
1945
1946DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
1947DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
Ilya Dryomov46092452016-04-28 16:07:27 +02001948DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
Ilya Dryomov922dab62016-05-26 01:15:02 +02001949
1950/*
1951 * Create linger request <-> OSD session relation.
1952 *
1953 * @lreq has to be registered, @osd may be homeless.
1954 */
1955static void link_linger(struct ceph_osd *osd,
1956 struct ceph_osd_linger_request *lreq)
1957{
1958 verify_osd_locked(osd);
1959 WARN_ON(!lreq->linger_id || lreq->osd);
1960 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
1961 osd->o_osd, lreq, lreq->linger_id);
1962
1963 if (!osd_homeless(osd))
1964 __remove_osd_from_lru(osd);
1965 else
1966 atomic_inc(&osd->o_osdc->num_homeless);
1967
1968 get_osd(osd);
1969 insert_linger(&osd->o_linger_requests, lreq);
1970 lreq->osd = osd;
1971}
1972
1973static void unlink_linger(struct ceph_osd *osd,
1974 struct ceph_osd_linger_request *lreq)
1975{
1976 verify_osd_locked(osd);
1977 WARN_ON(lreq->osd != osd);
1978 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
1979 osd->o_osd, lreq, lreq->linger_id);
1980
1981 lreq->osd = NULL;
1982 erase_linger(&osd->o_linger_requests, lreq);
1983 put_osd(osd);
1984
1985 if (!osd_homeless(osd))
1986 maybe_move_osd_to_lru(osd);
1987 else
1988 atomic_dec(&osd->o_osdc->num_homeless);
1989}
1990
1991static bool __linger_registered(struct ceph_osd_linger_request *lreq)
1992{
1993 verify_osdc_locked(lreq->osdc);
1994
1995 return !RB_EMPTY_NODE(&lreq->osdc_node);
1996}
1997
1998static bool linger_registered(struct ceph_osd_linger_request *lreq)
1999{
2000 struct ceph_osd_client *osdc = lreq->osdc;
2001 bool registered;
2002
2003 down_read(&osdc->lock);
2004 registered = __linger_registered(lreq);
2005 up_read(&osdc->lock);
2006
2007 return registered;
2008}
2009
2010static void linger_register(struct ceph_osd_linger_request *lreq)
2011{
2012 struct ceph_osd_client *osdc = lreq->osdc;
2013
2014 verify_osdc_wrlocked(osdc);
2015 WARN_ON(lreq->linger_id);
2016
2017 linger_get(lreq);
2018 lreq->linger_id = ++osdc->last_linger_id;
2019 insert_linger_osdc(&osdc->linger_requests, lreq);
2020}
2021
2022static void linger_unregister(struct ceph_osd_linger_request *lreq)
2023{
2024 struct ceph_osd_client *osdc = lreq->osdc;
2025
2026 verify_osdc_wrlocked(osdc);
2027
2028 erase_linger_osdc(&osdc->linger_requests, lreq);
2029 linger_put(lreq);
2030}
2031
2032static void cancel_linger_request(struct ceph_osd_request *req)
2033{
2034 struct ceph_osd_linger_request *lreq = req->r_priv;
2035
2036 WARN_ON(!req->r_linger);
2037 cancel_request(req);
2038 linger_put(lreq);
2039}
2040
2041struct linger_work {
2042 struct work_struct work;
2043 struct ceph_osd_linger_request *lreq;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002044 struct list_head pending_item;
2045 unsigned long queued_stamp;
Ilya Dryomov922dab62016-05-26 01:15:02 +02002046
2047 union {
2048 struct {
2049 u64 notify_id;
2050 u64 notifier_id;
2051 void *payload; /* points into @msg front */
2052 size_t payload_len;
2053
2054 struct ceph_msg *msg; /* for ceph_msg_put() */
2055 } notify;
2056 struct {
2057 int err;
2058 } error;
2059 };
2060};
2061
2062static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2063 work_func_t workfn)
2064{
2065 struct linger_work *lwork;
2066
2067 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2068 if (!lwork)
2069 return NULL;
2070
2071 INIT_WORK(&lwork->work, workfn);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002072 INIT_LIST_HEAD(&lwork->pending_item);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002073 lwork->lreq = linger_get(lreq);
2074
2075 return lwork;
2076}
2077
2078static void lwork_free(struct linger_work *lwork)
2079{
2080 struct ceph_osd_linger_request *lreq = lwork->lreq;
2081
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002082 mutex_lock(&lreq->lock);
2083 list_del(&lwork->pending_item);
2084 mutex_unlock(&lreq->lock);
2085
Ilya Dryomov922dab62016-05-26 01:15:02 +02002086 linger_put(lreq);
2087 kfree(lwork);
2088}
2089
2090static void lwork_queue(struct linger_work *lwork)
2091{
2092 struct ceph_osd_linger_request *lreq = lwork->lreq;
2093 struct ceph_osd_client *osdc = lreq->osdc;
2094
2095 verify_lreq_locked(lreq);
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002096 WARN_ON(!list_empty(&lwork->pending_item));
2097
2098 lwork->queued_stamp = jiffies;
2099 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002100 queue_work(osdc->notify_wq, &lwork->work);
2101}
2102
2103static void do_watch_notify(struct work_struct *w)
2104{
2105 struct linger_work *lwork = container_of(w, struct linger_work, work);
2106 struct ceph_osd_linger_request *lreq = lwork->lreq;
2107
2108 if (!linger_registered(lreq)) {
2109 dout("%s lreq %p not registered\n", __func__, lreq);
2110 goto out;
2111 }
2112
Ilya Dryomov19079202016-04-28 16:07:27 +02002113 WARN_ON(!lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002114 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2115 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2116 lwork->notify.payload_len);
2117 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2118 lwork->notify.notifier_id, lwork->notify.payload,
2119 lwork->notify.payload_len);
2120
2121out:
2122 ceph_msg_put(lwork->notify.msg);
2123 lwork_free(lwork);
2124}
2125
2126static void do_watch_error(struct work_struct *w)
2127{
2128 struct linger_work *lwork = container_of(w, struct linger_work, work);
2129 struct ceph_osd_linger_request *lreq = lwork->lreq;
2130
2131 if (!linger_registered(lreq)) {
2132 dout("%s lreq %p not registered\n", __func__, lreq);
2133 goto out;
2134 }
2135
2136 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2137 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2138
2139out:
2140 lwork_free(lwork);
2141}
2142
2143static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2144{
2145 struct linger_work *lwork;
2146
2147 lwork = lwork_alloc(lreq, do_watch_error);
2148 if (!lwork) {
2149 pr_err("failed to allocate error-lwork\n");
2150 return;
2151 }
2152
2153 lwork->error.err = lreq->last_error;
2154 lwork_queue(lwork);
2155}
2156
2157static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2158 int result)
2159{
2160 if (!completion_done(&lreq->reg_commit_wait)) {
2161 lreq->reg_commit_error = (result <= 0 ? result : 0);
2162 complete_all(&lreq->reg_commit_wait);
2163 }
2164}
2165
2166static void linger_commit_cb(struct ceph_osd_request *req)
2167{
2168 struct ceph_osd_linger_request *lreq = req->r_priv;
2169
2170 mutex_lock(&lreq->lock);
2171 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2172 lreq->linger_id, req->r_result);
2173 WARN_ON(!__linger_registered(lreq));
2174 linger_reg_commit_complete(lreq, req->r_result);
2175 lreq->committed = true;
2176
Ilya Dryomov19079202016-04-28 16:07:27 +02002177 if (!lreq->is_watch) {
2178 struct ceph_osd_data *osd_data =
2179 osd_req_op_data(req, 0, notify, response_data);
2180 void *p = page_address(osd_data->pages[0]);
2181
2182 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2183 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2184
2185 /* make note of the notify_id */
2186 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2187 lreq->notify_id = ceph_decode_64(&p);
2188 dout("lreq %p notify_id %llu\n", lreq,
2189 lreq->notify_id);
2190 } else {
2191 dout("lreq %p no notify_id\n", lreq);
2192 }
2193 }
2194
Ilya Dryomov922dab62016-05-26 01:15:02 +02002195 mutex_unlock(&lreq->lock);
2196 linger_put(lreq);
2197}
2198
2199static int normalize_watch_error(int err)
2200{
2201 /*
2202 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2203 * notification and a failure to reconnect because we raced with
2204 * the delete appear the same to the user.
2205 */
2206 if (err == -ENOENT)
2207 err = -ENOTCONN;
2208
2209 return err;
2210}
2211
2212static void linger_reconnect_cb(struct ceph_osd_request *req)
2213{
2214 struct ceph_osd_linger_request *lreq = req->r_priv;
2215
2216 mutex_lock(&lreq->lock);
2217 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2218 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2219 if (req->r_result < 0) {
2220 if (!lreq->last_error) {
2221 lreq->last_error = normalize_watch_error(req->r_result);
2222 queue_watch_error(lreq);
2223 }
2224 }
2225
2226 mutex_unlock(&lreq->lock);
2227 linger_put(lreq);
2228}
2229
2230static void send_linger(struct ceph_osd_linger_request *lreq)
2231{
2232 struct ceph_osd_request *req = lreq->reg_req;
2233 struct ceph_osd_req_op *op = &req->r_ops[0];
2234
2235 verify_osdc_wrlocked(req->r_osdc);
2236 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2237
2238 if (req->r_osd)
2239 cancel_linger_request(req);
2240
2241 request_reinit(req);
2242 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2243 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2244 req->r_flags = lreq->t.flags;
2245 req->r_mtime = lreq->mtime;
2246
2247 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02002248 if (lreq->is_watch && lreq->committed) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002249 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2250 op->watch.cookie != lreq->linger_id);
2251 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2252 op->watch.gen = ++lreq->register_gen;
2253 dout("lreq %p reconnect register_gen %u\n", lreq,
2254 op->watch.gen);
2255 req->r_callback = linger_reconnect_cb;
2256 } else {
Ilya Dryomov19079202016-04-28 16:07:27 +02002257 if (!lreq->is_watch)
2258 lreq->notify_id = 0;
2259 else
2260 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002261 dout("lreq %p register\n", lreq);
2262 req->r_callback = linger_commit_cb;
2263 }
2264 mutex_unlock(&lreq->lock);
2265
2266 req->r_priv = linger_get(lreq);
2267 req->r_linger = true;
2268
2269 submit_request(req, true);
2270}
2271
2272static void linger_ping_cb(struct ceph_osd_request *req)
2273{
2274 struct ceph_osd_linger_request *lreq = req->r_priv;
2275
2276 mutex_lock(&lreq->lock);
2277 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2278 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2279 lreq->last_error);
2280 if (lreq->register_gen == req->r_ops[0].watch.gen) {
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02002281 if (!req->r_result) {
2282 lreq->watch_valid_thru = lreq->ping_sent;
2283 } else if (!lreq->last_error) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002284 lreq->last_error = normalize_watch_error(req->r_result);
2285 queue_watch_error(lreq);
2286 }
2287 } else {
2288 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2289 lreq->register_gen, req->r_ops[0].watch.gen);
2290 }
2291
2292 mutex_unlock(&lreq->lock);
2293 linger_put(lreq);
2294}
2295
2296static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2297{
2298 struct ceph_osd_client *osdc = lreq->osdc;
2299 struct ceph_osd_request *req = lreq->ping_req;
2300 struct ceph_osd_req_op *op = &req->r_ops[0];
2301
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02002302 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
Ilya Dryomov922dab62016-05-26 01:15:02 +02002303 dout("%s PAUSERD\n", __func__);
2304 return;
2305 }
2306
2307 lreq->ping_sent = jiffies;
2308 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2309 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2310 lreq->register_gen);
2311
2312 if (req->r_osd)
2313 cancel_linger_request(req);
2314
2315 request_reinit(req);
2316 target_copy(&req->r_t, &lreq->t);
2317
2318 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2319 op->watch.cookie != lreq->linger_id ||
2320 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2321 op->watch.gen = lreq->register_gen;
2322 req->r_callback = linger_ping_cb;
2323 req->r_priv = linger_get(lreq);
2324 req->r_linger = true;
2325
2326 ceph_osdc_get_request(req);
2327 account_request(req);
2328 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2329 link_request(lreq->osd, req);
2330 send_request(req);
2331}
2332
2333static void linger_submit(struct ceph_osd_linger_request *lreq)
2334{
2335 struct ceph_osd_client *osdc = lreq->osdc;
2336 struct ceph_osd *osd;
2337
2338 calc_target(osdc, &lreq->t, &lreq->last_force_resend, false);
2339 osd = lookup_create_osd(osdc, lreq->t.osd, true);
2340 link_linger(osd, lreq);
2341
2342 send_linger(lreq);
2343}
2344
Ilya Dryomov46092452016-04-28 16:07:27 +02002345static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2346{
2347 struct ceph_osd_client *osdc = lreq->osdc;
2348 struct ceph_osd_linger_request *lookup_lreq;
2349
2350 verify_osdc_wrlocked(osdc);
2351
2352 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2353 lreq->linger_id);
2354 if (!lookup_lreq)
2355 return;
2356
2357 WARN_ON(lookup_lreq != lreq);
2358 erase_linger_mc(&osdc->linger_map_checks, lreq);
2359 linger_put(lreq);
2360}
2361
Ilya Dryomov922dab62016-05-26 01:15:02 +02002362/*
2363 * @lreq has to be both registered and linked.
2364 */
2365static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2366{
Ilya Dryomov19079202016-04-28 16:07:27 +02002367 if (lreq->is_watch && lreq->ping_req->r_osd)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002368 cancel_linger_request(lreq->ping_req);
2369 if (lreq->reg_req->r_osd)
2370 cancel_linger_request(lreq->reg_req);
Ilya Dryomov46092452016-04-28 16:07:27 +02002371 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002372 unlink_linger(lreq->osd, lreq);
2373 linger_unregister(lreq);
2374}
2375
2376static void linger_cancel(struct ceph_osd_linger_request *lreq)
2377{
2378 struct ceph_osd_client *osdc = lreq->osdc;
2379
2380 down_write(&osdc->lock);
2381 if (__linger_registered(lreq))
2382 __linger_cancel(lreq);
2383 up_write(&osdc->lock);
2384}
2385
Ilya Dryomov46092452016-04-28 16:07:27 +02002386static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2387
2388static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2389{
2390 struct ceph_osd_client *osdc = lreq->osdc;
2391 struct ceph_osdmap *map = osdc->osdmap;
2392
2393 verify_osdc_wrlocked(osdc);
2394 WARN_ON(!map->epoch);
2395
2396 if (lreq->register_gen) {
2397 lreq->map_dne_bound = map->epoch;
2398 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2399 lreq, lreq->linger_id);
2400 } else {
2401 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2402 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2403 map->epoch);
2404 }
2405
2406 if (lreq->map_dne_bound) {
2407 if (map->epoch >= lreq->map_dne_bound) {
2408 /* we had a new enough map */
2409 pr_info("linger_id %llu pool does not exist\n",
2410 lreq->linger_id);
2411 linger_reg_commit_complete(lreq, -ENOENT);
2412 __linger_cancel(lreq);
2413 }
2414 } else {
2415 send_linger_map_check(lreq);
2416 }
2417}
2418
2419static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2420{
2421 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2422 struct ceph_osd_linger_request *lreq;
2423 u64 linger_id = greq->private_data;
2424
2425 WARN_ON(greq->result || !greq->u.newest);
2426
2427 down_write(&osdc->lock);
2428 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2429 if (!lreq) {
2430 dout("%s linger_id %llu dne\n", __func__, linger_id);
2431 goto out_unlock;
2432 }
2433
2434 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
2435 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2436 greq->u.newest);
2437 if (!lreq->map_dne_bound)
2438 lreq->map_dne_bound = greq->u.newest;
2439 erase_linger_mc(&osdc->linger_map_checks, lreq);
2440 check_linger_pool_dne(lreq);
2441
2442 linger_put(lreq);
2443out_unlock:
2444 up_write(&osdc->lock);
2445}
2446
2447static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
2448{
2449 struct ceph_osd_client *osdc = lreq->osdc;
2450 struct ceph_osd_linger_request *lookup_lreq;
2451 int ret;
2452
2453 verify_osdc_wrlocked(osdc);
2454
2455 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2456 lreq->linger_id);
2457 if (lookup_lreq) {
2458 WARN_ON(lookup_lreq != lreq);
2459 return;
2460 }
2461
2462 linger_get(lreq);
2463 insert_linger_mc(&osdc->linger_map_checks, lreq);
2464 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2465 linger_map_check_cb, lreq->linger_id);
2466 WARN_ON(ret);
2467}
2468
Ilya Dryomov922dab62016-05-26 01:15:02 +02002469static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
2470{
2471 int ret;
2472
2473 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2474 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
2475 return ret ?: lreq->reg_commit_error;
2476}
2477
Ilya Dryomov19079202016-04-28 16:07:27 +02002478static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
2479{
2480 int ret;
2481
2482 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2483 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
2484 return ret ?: lreq->notify_finish_error;
2485}
2486
Ilya Dryomov922dab62016-05-26 01:15:02 +02002487/*
Ilya Dryomovfbca9632016-04-28 16:07:24 +02002488 * Timeout callback, called every N seconds. When 1 or more OSD
2489 * requests has been active for more than N seconds, we send a keepalive
2490 * (tag + timestamp) to its OSD to ensure any communications channel
2491 * reset is detected.
Sage Weilf24e9982009-10-06 11:31:10 -07002492 */
2493static void handle_timeout(struct work_struct *work)
2494{
2495 struct ceph_osd_client *osdc =
2496 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03002497 struct ceph_options *opts = osdc->client->options;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002498 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
2499 LIST_HEAD(slow_osds);
2500 struct rb_node *n, *p;
Sage Weilf24e9982009-10-06 11:31:10 -07002501
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002502 dout("%s osdc %p\n", __func__, osdc);
2503 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07002504
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002505 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002506 * ping osds that are a bit slow. this ensures that if there
2507 * is a break in the TCP connection we will notice, and reopen
2508 * a connection with that osd (from the fault callback).
2509 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002510 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2511 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2512 bool found = false;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002513
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002514 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
2515 struct ceph_osd_request *req =
2516 rb_entry(p, struct ceph_osd_request, r_node);
2517
2518 if (time_before(req->r_stamp, cutoff)) {
2519 dout(" req %p tid %llu on osd%d is laggy\n",
2520 req, req->r_tid, osd->o_osd);
2521 found = true;
2522 }
2523 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02002524 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
2525 struct ceph_osd_linger_request *lreq =
2526 rb_entry(p, struct ceph_osd_linger_request, node);
2527
2528 dout(" lreq %p linger_id %llu is served by osd%d\n",
2529 lreq, lreq->linger_id, osd->o_osd);
2530 found = true;
2531
2532 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02002533 if (lreq->is_watch && lreq->committed && !lreq->last_error)
Ilya Dryomov922dab62016-05-26 01:15:02 +02002534 send_linger_ping(lreq);
2535 mutex_unlock(&lreq->lock);
2536 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002537
2538 if (found)
2539 list_move_tail(&osd->o_keepalive_item, &slow_osds);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002540 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002541
2542 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
2543 maybe_request_map(osdc);
2544
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002545 while (!list_empty(&slow_osds)) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002546 struct ceph_osd *osd = list_first_entry(&slow_osds,
2547 struct ceph_osd,
2548 o_keepalive_item);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002549 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07002550 ceph_con_keepalive(&osd->o_con);
2551 }
2552
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002553 up_write(&osdc->lock);
Ilya Dryomovfbca9632016-04-28 16:07:24 +02002554 schedule_delayed_work(&osdc->timeout_work,
2555 osdc->client->options->osd_keepalive_timeout);
Sage Weilf24e9982009-10-06 11:31:10 -07002556}
2557
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002558static void handle_osds_timeout(struct work_struct *work)
2559{
2560 struct ceph_osd_client *osdc =
2561 container_of(work, struct ceph_osd_client,
2562 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03002563 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002564 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002565
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002566 dout("%s osdc %p\n", __func__, osdc);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002567 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002568 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
2569 if (time_before(jiffies, osd->lru_ttl))
2570 break;
2571
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002572 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
Ilya Dryomov922dab62016-05-26 01:15:02 +02002573 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002574 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002575 }
2576
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002577 up_write(&osdc->lock);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002578 schedule_delayed_work(&osdc->osds_timeout_work,
2579 round_jiffies_relative(delay));
2580}
2581
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002582static int ceph_oloc_decode(void **p, void *end,
2583 struct ceph_object_locator *oloc)
2584{
2585 u8 struct_v, struct_cv;
2586 u32 len;
2587 void *struct_end;
2588 int ret = 0;
2589
2590 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2591 struct_v = ceph_decode_8(p);
2592 struct_cv = ceph_decode_8(p);
2593 if (struct_v < 3) {
2594 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
2595 struct_v, struct_cv);
2596 goto e_inval;
2597 }
2598 if (struct_cv > 6) {
2599 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
2600 struct_v, struct_cv);
2601 goto e_inval;
2602 }
2603 len = ceph_decode_32(p);
2604 ceph_decode_need(p, end, len, e_inval);
2605 struct_end = *p + len;
2606
2607 oloc->pool = ceph_decode_64(p);
2608 *p += 4; /* skip preferred */
2609
2610 len = ceph_decode_32(p);
2611 if (len > 0) {
2612 pr_warn("ceph_object_locator::key is set\n");
2613 goto e_inval;
2614 }
2615
2616 if (struct_v >= 5) {
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002617 bool changed = false;
2618
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002619 len = ceph_decode_32(p);
2620 if (len > 0) {
Yan, Zheng30c156d2016-02-14 11:24:31 +08002621 ceph_decode_need(p, end, len, e_inval);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002622 if (!oloc->pool_ns ||
2623 ceph_compare_string(oloc->pool_ns, *p, len))
2624 changed = true;
Yan, Zheng30c156d2016-02-14 11:24:31 +08002625 *p += len;
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002626 } else {
2627 if (oloc->pool_ns)
2628 changed = true;
2629 }
2630 if (changed) {
2631 /* redirect changes namespace */
2632 pr_warn("ceph_object_locator::nspace is changed\n");
2633 goto e_inval;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002634 }
2635 }
2636
2637 if (struct_v >= 6) {
2638 s64 hash = ceph_decode_64(p);
2639 if (hash != -1) {
2640 pr_warn("ceph_object_locator::hash is set\n");
2641 goto e_inval;
2642 }
2643 }
2644
2645 /* skip the rest */
2646 *p = struct_end;
2647out:
2648 return ret;
2649
2650e_inval:
2651 ret = -EINVAL;
2652 goto out;
2653}
2654
2655static int ceph_redirect_decode(void **p, void *end,
2656 struct ceph_request_redirect *redir)
2657{
2658 u8 struct_v, struct_cv;
2659 u32 len;
2660 void *struct_end;
2661 int ret;
2662
2663 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2664 struct_v = ceph_decode_8(p);
2665 struct_cv = ceph_decode_8(p);
2666 if (struct_cv > 1) {
2667 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
2668 struct_v, struct_cv);
2669 goto e_inval;
2670 }
2671 len = ceph_decode_32(p);
2672 ceph_decode_need(p, end, len, e_inval);
2673 struct_end = *p + len;
2674
2675 ret = ceph_oloc_decode(p, end, &redir->oloc);
2676 if (ret)
2677 goto out;
2678
2679 len = ceph_decode_32(p);
2680 if (len > 0) {
2681 pr_warn("ceph_request_redirect::object_name is set\n");
2682 goto e_inval;
2683 }
2684
2685 len = ceph_decode_32(p);
2686 *p += len; /* skip osd_instructions */
2687
2688 /* skip the rest */
2689 *p = struct_end;
2690out:
2691 return ret;
2692
2693e_inval:
2694 ret = -EINVAL;
2695 goto out;
2696}
2697
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002698struct MOSDOpReply {
2699 struct ceph_pg pgid;
2700 u64 flags;
2701 int result;
2702 u32 epoch;
2703 int num_ops;
2704 u32 outdata_len[CEPH_OSD_MAX_OPS];
2705 s32 rval[CEPH_OSD_MAX_OPS];
2706 int retry_attempt;
2707 struct ceph_eversion replay_version;
2708 u64 user_version;
2709 struct ceph_request_redirect redirect;
2710};
Sage Weil25845472011-06-03 09:37:09 -07002711
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002712static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
Sage Weilf24e9982009-10-06 11:31:10 -07002713{
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002714 void *p = msg->front.iov_base;
2715 void *const end = p + msg->front.iov_len;
2716 u16 version = le16_to_cpu(msg->hdr.version);
2717 struct ceph_eversion bad_replay_version;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01002718 u8 decode_redir;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002719 u32 len;
2720 int ret;
2721 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07002722
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002723 ceph_decode_32_safe(&p, end, len, e_inval);
2724 ceph_decode_need(&p, end, len, e_inval);
2725 p += len; /* skip oid */
Sage Weil1b83bef2013-02-25 16:11:12 -08002726
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002727 ret = ceph_decode_pgid(&p, end, &m->pgid);
2728 if (ret)
2729 return ret;
Sage Weil1b83bef2013-02-25 16:11:12 -08002730
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002731 ceph_decode_64_safe(&p, end, m->flags, e_inval);
2732 ceph_decode_32_safe(&p, end, m->result, e_inval);
2733 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
2734 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
2735 p += sizeof(bad_replay_version);
2736 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
Sage Weil1b83bef2013-02-25 16:11:12 -08002737
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002738 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
2739 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
2740 goto e_inval;
Sage Weil1b83bef2013-02-25 16:11:12 -08002741
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002742 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
2743 e_inval);
2744 for (i = 0; i < m->num_ops; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -08002745 struct ceph_osd_op *op = p;
Sage Weil1b83bef2013-02-25 16:11:12 -08002746
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002747 m->outdata_len[i] = le32_to_cpu(op->payload_len);
Sage Weil1b83bef2013-02-25 16:11:12 -08002748 p += sizeof(*op);
2749 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002750
2751 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
2752 for (i = 0; i < m->num_ops; i++)
2753 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
2754
2755 if (version >= 5) {
2756 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
2757 memcpy(&m->replay_version, p, sizeof(m->replay_version));
2758 p += sizeof(m->replay_version);
2759 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
2760 } else {
2761 m->replay_version = bad_replay_version; /* struct */
2762 m->user_version = le64_to_cpu(m->replay_version.version);
Sage Weil1b83bef2013-02-25 16:11:12 -08002763 }
2764
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002765 if (version >= 6) {
2766 if (version >= 7)
2767 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01002768 else
2769 decode_redir = 1;
2770 } else {
2771 decode_redir = 0;
2772 }
2773
2774 if (decode_redir) {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002775 ret = ceph_redirect_decode(&p, end, &m->redirect);
2776 if (ret)
2777 return ret;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002778 } else {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002779 ceph_oloc_init(&m->redirect.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002780 }
2781
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002782 return 0;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002783
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002784e_inval:
2785 return -EINVAL;
2786}
2787
2788/*
2789 * We are done with @req if
2790 * - @m is a safe reply, or
2791 * - @m is an unsafe reply and we didn't want a safe one
2792 */
2793static bool done_request(const struct ceph_osd_request *req,
2794 const struct MOSDOpReply *m)
2795{
2796 return (m->result < 0 ||
2797 (m->flags & CEPH_OSD_FLAG_ONDISK) ||
2798 !(req->r_flags & CEPH_OSD_FLAG_ONDISK));
2799}
2800
2801/*
2802 * handle osd op reply. either call the callback if it is specified,
2803 * or do the completion to wake up the waiting thread.
2804 *
2805 * ->r_unsafe_callback is set? yes no
2806 *
2807 * first reply is OK (needed r_cb/r_completion, r_cb/r_completion,
Ilya Dryomovc297eb422016-12-02 14:01:55 +01002808 * any or needed/got safe) r_done_completion r_done_completion
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002809 *
2810 * first reply is unsafe r_unsafe_cb(true) (nothing)
2811 *
2812 * when we get the safe reply r_unsafe_cb(false), r_cb/r_completion,
Ilya Dryomovc297eb422016-12-02 14:01:55 +01002813 * r_done_completion r_done_completion
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002814 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002815static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002816{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002817 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002818 struct ceph_osd_request *req;
2819 struct MOSDOpReply m;
2820 u64 tid = le64_to_cpu(msg->hdr.tid);
2821 u32 data_len = 0;
2822 bool already_acked;
2823 int ret;
2824 int i;
2825
2826 dout("%s msg %p tid %llu\n", __func__, msg, tid);
2827
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002828 down_read(&osdc->lock);
2829 if (!osd_registered(osd)) {
2830 dout("%s osd%d unknown\n", __func__, osd->o_osd);
2831 goto out_unlock_osdc;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002832 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002833 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
2834
2835 mutex_lock(&osd->lock);
2836 req = lookup_request(&osd->o_requests, tid);
2837 if (!req) {
2838 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
2839 goto out_unlock_session;
2840 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002841
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002842 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002843 ret = decode_MOSDOpReply(msg, &m);
Yan, Zhengcd08e0a2016-06-13 19:05:13 +08002844 m.redirect.oloc.pool_ns = NULL;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002845 if (ret) {
2846 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
2847 req->r_tid, ret);
2848 ceph_msg_dump(msg);
2849 goto fail_request;
2850 }
2851 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
2852 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
2853 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
2854 le64_to_cpu(m.replay_version.version), m.user_version);
2855
2856 if (m.retry_attempt >= 0) {
2857 if (m.retry_attempt != req->r_attempts - 1) {
2858 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
2859 req, req->r_tid, m.retry_attempt,
2860 req->r_attempts - 1);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002861 goto out_unlock_session;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002862 }
2863 } else {
2864 WARN_ON(1); /* MOSDOpReply v4 is assumed */
2865 }
2866
2867 if (!ceph_oloc_empty(&m.redirect.oloc)) {
2868 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
2869 m.redirect.oloc.pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002870 unlink_request(osd, req);
2871 mutex_unlock(&osd->lock);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002872
Yan, Zheng30c156d2016-02-14 11:24:31 +08002873 /*
2874 * Not ceph_oloc_copy() - changing pool_ns is not
2875 * supported.
2876 */
2877 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002878 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
2879 req->r_tid = 0;
2880 __submit_request(req, false);
2881 goto out_unlock_osdc;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002882 }
2883
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002884 if (m.num_ops != req->r_num_ops) {
2885 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
2886 req->r_num_ops, req->r_tid);
2887 goto fail_request;
2888 }
2889 for (i = 0; i < req->r_num_ops; i++) {
2890 dout(" req %p tid %llu op %d rval %d len %u\n", req,
2891 req->r_tid, i, m.rval[i], m.outdata_len[i]);
2892 req->r_ops[i].rval = m.rval[i];
2893 req->r_ops[i].outdata_len = m.outdata_len[i];
2894 data_len += m.outdata_len[i];
2895 }
2896 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
2897 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
2898 le32_to_cpu(msg->hdr.data_len), req->r_tid);
2899 goto fail_request;
2900 }
2901 dout("%s req %p tid %llu acked %d result %d data_len %u\n", __func__,
2902 req, req->r_tid, req->r_got_reply, m.result, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07002903
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002904 already_acked = req->r_got_reply;
2905 if (!already_acked) {
2906 req->r_result = m.result ?: data_len;
2907 req->r_replay_version = m.replay_version; /* struct */
2908 req->r_got_reply = true;
2909 } else if (!(m.flags & CEPH_OSD_FLAG_ONDISK)) {
2910 dout("req %p tid %llu dup ack\n", req, req->r_tid);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002911 goto out_unlock_session;
Sage Weilf24e9982009-10-06 11:31:10 -07002912 }
2913
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002914 if (done_request(req, &m)) {
Ilya Dryomov45ee2c12016-12-02 14:01:55 +01002915 finish_request(req);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002916 if (req->r_linger) {
2917 WARN_ON(req->r_unsafe_callback);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002918 dout("req %p tid %llu cb (locked)\n", req, req->r_tid);
2919 __complete_request(req);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002920 }
2921 }
Sage Weilf24e9982009-10-06 11:31:10 -07002922
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002923 mutex_unlock(&osd->lock);
2924 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07002925
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002926 if (done_request(req, &m)) {
2927 if (already_acked && req->r_unsafe_callback) {
2928 dout("req %p tid %llu safe-cb\n", req, req->r_tid);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08002929 req->r_unsafe_callback(req, false);
Ilya Dryomov922dab62016-05-26 01:15:02 +02002930 } else if (!req->r_linger) {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002931 dout("req %p tid %llu cb\n", req, req->r_tid);
2932 __complete_request(req);
2933 }
Ilya Dryomovc297eb422016-12-02 14:01:55 +01002934 complete_all(&req->r_done_completion);
Ilya Dryomovdc045a92016-05-27 15:18:34 +02002935 ceph_osdc_put_request(req);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002936 } else {
2937 if (req->r_unsafe_callback) {
2938 dout("req %p tid %llu unsafe-cb\n", req, req->r_tid);
2939 req->r_unsafe_callback(req, true);
2940 } else {
2941 WARN_ON(1);
2942 }
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08002943 }
Sage Weilf24e9982009-10-06 11:31:10 -07002944
Sage Weilf24e9982009-10-06 11:31:10 -07002945 return;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002946
2947fail_request:
Ilya Dryomov46092452016-04-28 16:07:27 +02002948 complete_request(req, -EIO);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002949out_unlock_session:
2950 mutex_unlock(&osd->lock);
2951out_unlock_osdc:
2952 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07002953}
2954
Ilya Dryomov42c1b122016-04-28 16:07:25 +02002955static void set_pool_was_full(struct ceph_osd_client *osdc)
2956{
2957 struct rb_node *n;
2958
2959 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
2960 struct ceph_pg_pool_info *pi =
2961 rb_entry(n, struct ceph_pg_pool_info, node);
2962
2963 pi->was_full = __pool_full(pi);
2964 }
2965}
2966
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002967static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
Sage Weilf24e9982009-10-06 11:31:10 -07002968{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002969 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07002970
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002971 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
2972 if (!pi)
2973 return false;
Sage Weilf24e9982009-10-06 11:31:10 -07002974
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002975 return pi->was_full && !__pool_full(pi);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002976}
2977
Ilya Dryomov922dab62016-05-26 01:15:02 +02002978static enum calc_target_result
2979recalc_linger_target(struct ceph_osd_linger_request *lreq)
2980{
2981 struct ceph_osd_client *osdc = lreq->osdc;
2982 enum calc_target_result ct_res;
2983
2984 ct_res = calc_target(osdc, &lreq->t, &lreq->last_force_resend, true);
2985 if (ct_res == CALC_TARGET_NEED_RESEND) {
2986 struct ceph_osd *osd;
2987
2988 osd = lookup_create_osd(osdc, lreq->t.osd, true);
2989 if (osd != lreq->osd) {
2990 unlink_linger(lreq->osd, lreq);
2991 link_linger(osd, lreq);
2992 }
2993 }
2994
2995 return ct_res;
2996}
2997
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002998/*
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02002999 * Requeue requests whose mapping to an OSD has changed.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003000 */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003001static void scan_requests(struct ceph_osd *osd,
3002 bool force_resend,
3003 bool cleared_full,
3004 bool check_pool_cleared_full,
3005 struct rb_root *need_resend,
3006 struct list_head *need_resend_linger)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003007{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003008 struct ceph_osd_client *osdc = osd->o_osdc;
3009 struct rb_node *n;
3010 bool force_resend_writes;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003011
Ilya Dryomov922dab62016-05-26 01:15:02 +02003012 for (n = rb_first(&osd->o_linger_requests); n; ) {
3013 struct ceph_osd_linger_request *lreq =
3014 rb_entry(n, struct ceph_osd_linger_request, node);
3015 enum calc_target_result ct_res;
3016
3017 n = rb_next(n); /* recalc_linger_target() */
3018
3019 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3020 lreq->linger_id);
3021 ct_res = recalc_linger_target(lreq);
3022 switch (ct_res) {
3023 case CALC_TARGET_NO_ACTION:
3024 force_resend_writes = cleared_full ||
3025 (check_pool_cleared_full &&
3026 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3027 if (!force_resend && !force_resend_writes)
3028 break;
3029
3030 /* fall through */
3031 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003032 cancel_linger_map_check(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003033 /*
3034 * scan_requests() for the previous epoch(s)
3035 * may have already added it to the list, since
3036 * it's not unlinked here.
3037 */
3038 if (list_empty(&lreq->scan_item))
3039 list_add_tail(&lreq->scan_item, need_resend_linger);
3040 break;
3041 case CALC_TARGET_POOL_DNE:
Ilya Dryomov46092452016-04-28 16:07:27 +02003042 check_linger_pool_dne(lreq);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003043 break;
3044 }
3045 }
3046
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003047 for (n = rb_first(&osd->o_requests); n; ) {
3048 struct ceph_osd_request *req =
3049 rb_entry(n, struct ceph_osd_request, r_node);
3050 enum calc_target_result ct_res;
Alex Elderab60b162012-12-19 15:52:36 -06003051
Ilya Dryomov46092452016-04-28 16:07:27 +02003052 n = rb_next(n); /* unlink_request(), check_pool_dne() */
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003053
3054 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3055 ct_res = calc_target(osdc, &req->r_t,
3056 &req->r_last_force_resend, false);
3057 switch (ct_res) {
3058 case CALC_TARGET_NO_ACTION:
3059 force_resend_writes = cleared_full ||
3060 (check_pool_cleared_full &&
3061 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3062 if (!force_resend &&
3063 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3064 !force_resend_writes))
3065 break;
3066
3067 /* fall through */
3068 case CALC_TARGET_NEED_RESEND:
Ilya Dryomov46092452016-04-28 16:07:27 +02003069 cancel_map_check(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003070 unlink_request(osd, req);
3071 insert_request(need_resend, req);
3072 break;
3073 case CALC_TARGET_POOL_DNE:
Ilya Dryomov46092452016-04-28 16:07:27 +02003074 check_pool_dne(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003075 break;
Alex Elderab60b162012-12-19 15:52:36 -06003076 }
Sage Weilf24e9982009-10-06 11:31:10 -07003077 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08003078}
Sage Weil6f6c7002011-01-17 20:34:08 -08003079
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003080static int handle_one_map(struct ceph_osd_client *osdc,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003081 void *p, void *end, bool incremental,
3082 struct rb_root *need_resend,
3083 struct list_head *need_resend_linger)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003084{
3085 struct ceph_osdmap *newmap;
3086 struct rb_node *n;
3087 bool skipped_map = false;
3088 bool was_full;
3089
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003090 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003091 set_pool_was_full(osdc);
3092
3093 if (incremental)
3094 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3095 else
3096 newmap = ceph_osdmap_decode(&p, end);
3097 if (IS_ERR(newmap))
3098 return PTR_ERR(newmap);
3099
3100 if (newmap != osdc->osdmap) {
3101 /*
3102 * Preserve ->was_full before destroying the old map.
3103 * For pools that weren't in the old map, ->was_full
3104 * should be false.
3105 */
3106 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3107 struct ceph_pg_pool_info *pi =
3108 rb_entry(n, struct ceph_pg_pool_info, node);
3109 struct ceph_pg_pool_info *old_pi;
3110
3111 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3112 if (old_pi)
3113 pi->was_full = old_pi->was_full;
3114 else
3115 WARN_ON(pi->was_full);
3116 }
3117
3118 if (osdc->osdmap->epoch &&
3119 osdc->osdmap->epoch + 1 < newmap->epoch) {
3120 WARN_ON(incremental);
3121 skipped_map = true;
3122 }
3123
3124 ceph_osdmap_destroy(osdc->osdmap);
3125 osdc->osdmap = newmap;
3126 }
3127
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003128 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003129 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3130 need_resend, need_resend_linger);
3131
3132 for (n = rb_first(&osdc->osds); n; ) {
3133 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3134
3135 n = rb_next(n); /* close_osd() */
3136
3137 scan_requests(osd, skipped_map, was_full, true, need_resend,
3138 need_resend_linger);
3139 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3140 memcmp(&osd->o_con.peer_addr,
3141 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3142 sizeof(struct ceph_entity_addr)))
3143 close_osd(osd);
3144 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003145
3146 return 0;
3147}
Sage Weil6f6c7002011-01-17 20:34:08 -08003148
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003149static void kick_requests(struct ceph_osd_client *osdc,
3150 struct rb_root *need_resend,
3151 struct list_head *need_resend_linger)
3152{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003153 struct ceph_osd_linger_request *lreq, *nlreq;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003154 struct rb_node *n;
3155
3156 for (n = rb_first(need_resend); n; ) {
3157 struct ceph_osd_request *req =
3158 rb_entry(n, struct ceph_osd_request, r_node);
3159 struct ceph_osd *osd;
3160
3161 n = rb_next(n);
3162 erase_request(need_resend, req); /* before link_request() */
3163
3164 WARN_ON(req->r_osd);
3165 calc_target(osdc, &req->r_t, NULL, false);
3166 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3167 link_request(osd, req);
3168 if (!req->r_linger) {
3169 if (!osd_homeless(osd) && !req->r_t.paused)
3170 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003171 } else {
3172 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003173 }
3174 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003175
3176 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3177 if (!osd_homeless(lreq->osd))
3178 send_linger(lreq);
3179
3180 list_del_init(&lreq->scan_item);
3181 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003182}
3183
Sage Weilf24e9982009-10-06 11:31:10 -07003184/*
3185 * Process updated osd map.
3186 *
3187 * The message contains any number of incremental and full maps, normally
3188 * indicating some sort of topology change in the cluster. Kick requests
3189 * off to different OSDs as needed.
3190 */
3191void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3192{
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003193 void *p = msg->front.iov_base;
3194 void *const end = p + msg->front.iov_len;
Sage Weilf24e9982009-10-06 11:31:10 -07003195 u32 nr_maps, maplen;
3196 u32 epoch;
Sage Weilf24e9982009-10-06 11:31:10 -07003197 struct ceph_fsid fsid;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003198 struct rb_root need_resend = RB_ROOT;
3199 LIST_HEAD(need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003200 bool handled_incremental = false;
3201 bool was_pauserd, was_pausewr;
3202 bool pauserd, pausewr;
3203 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07003204
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003205 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003206 down_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003207
3208 /* verify fsid */
3209 ceph_decode_need(&p, end, sizeof(fsid), bad);
3210 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08003211 if (ceph_check_fsid(osdc->client, &fsid) < 0)
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003212 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003213
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003214 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3215 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3216 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003217 have_pool_full(osdc);
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08003218
Sage Weilf24e9982009-10-06 11:31:10 -07003219 /* incremental maps */
3220 ceph_decode_32_safe(&p, end, nr_maps, bad);
3221 dout(" %d inc maps\n", nr_maps);
3222 while (nr_maps > 0) {
3223 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003224 epoch = ceph_decode_32(&p);
3225 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003226 ceph_decode_need(&p, end, maplen, bad);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003227 if (osdc->osdmap->epoch &&
3228 osdc->osdmap->epoch + 1 == epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003229 dout("applying incremental map %u len %d\n",
3230 epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003231 err = handle_one_map(osdc, p, p + maplen, true,
3232 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003233 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003234 goto bad;
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003235 handled_incremental = true;
Sage Weilf24e9982009-10-06 11:31:10 -07003236 } else {
3237 dout("ignoring incremental map %u len %d\n",
3238 epoch, maplen);
3239 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003240 p += maplen;
Sage Weilf24e9982009-10-06 11:31:10 -07003241 nr_maps--;
3242 }
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003243 if (handled_incremental)
Sage Weilf24e9982009-10-06 11:31:10 -07003244 goto done;
3245
3246 /* full maps */
3247 ceph_decode_32_safe(&p, end, nr_maps, bad);
3248 dout(" %d full maps\n", nr_maps);
3249 while (nr_maps) {
3250 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003251 epoch = ceph_decode_32(&p);
3252 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07003253 ceph_decode_need(&p, end, maplen, bad);
3254 if (nr_maps > 1) {
3255 dout("skipping non-latest full map %u len %d\n",
3256 epoch, maplen);
Ilya Dryomove5253a72016-04-28 16:07:25 +02003257 } else if (osdc->osdmap->epoch >= epoch) {
Sage Weilf24e9982009-10-06 11:31:10 -07003258 dout("skipping full map %u len %d, "
3259 "older than our %u\n", epoch, maplen,
3260 osdc->osdmap->epoch);
3261 } else {
3262 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003263 err = handle_one_map(osdc, p, p + maplen, false,
3264 &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003265 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07003266 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07003267 }
3268 p += maplen;
3269 nr_maps--;
3270 }
3271
3272done:
Sage Weilcd634fb2011-05-12 09:29:18 -07003273 /*
3274 * subscribe to subsequent osdmap updates if full to ensure
3275 * we find out when we are no longer full and stop returning
3276 * ENOSPC.
3277 */
Ilya Dryomovb7ec35b2016-04-28 16:07:25 +02003278 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3279 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3280 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003281 have_pool_full(osdc);
3282 if (was_pauserd || was_pausewr || pauserd || pausewr)
3283 maybe_request_map(osdc);
Sage Weilcd634fb2011-05-12 09:29:18 -07003284
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003285 kick_requests(osdc, &need_resend, &need_resend_linger);
Ilya Dryomov42c1b122016-04-28 16:07:25 +02003286
3287 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3288 osdc->osdmap->epoch);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003289 up_write(&osdc->lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07003290 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07003291 return;
3292
3293bad:
3294 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003295 ceph_msg_dump(msg);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003296 up_write(&osdc->lock);
3297}
3298
3299/*
3300 * Resubmit requests pending on the given osd.
3301 */
3302static void kick_osd_requests(struct ceph_osd *osd)
3303{
3304 struct rb_node *n;
3305
Ilya Dryomov922dab62016-05-26 01:15:02 +02003306 for (n = rb_first(&osd->o_requests); n; ) {
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003307 struct ceph_osd_request *req =
3308 rb_entry(n, struct ceph_osd_request, r_node);
3309
Ilya Dryomov922dab62016-05-26 01:15:02 +02003310 n = rb_next(n); /* cancel_linger_request() */
3311
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003312 if (!req->r_linger) {
3313 if (!req->r_t.paused)
3314 send_request(req);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003315 } else {
3316 cancel_linger_request(req);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003317 }
3318 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003319 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3320 struct ceph_osd_linger_request *lreq =
3321 rb_entry(n, struct ceph_osd_linger_request, node);
3322
3323 send_linger(lreq);
3324 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003325}
3326
3327/*
3328 * If the osd connection drops, we need to resubmit all requests.
3329 */
3330static void osd_fault(struct ceph_connection *con)
3331{
3332 struct ceph_osd *osd = con->private;
3333 struct ceph_osd_client *osdc = osd->o_osdc;
3334
3335 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3336
3337 down_write(&osdc->lock);
3338 if (!osd_registered(osd)) {
3339 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3340 goto out_unlock;
3341 }
3342
3343 if (!reopen_osd(osd))
3344 kick_osd_requests(osd);
3345 maybe_request_map(osdc);
3346
3347out_unlock:
3348 up_write(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003349}
3350
Sage Weilf24e9982009-10-06 11:31:10 -07003351/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003352 * Process osd watch notifications
3353 */
Alex Elder3c663bb2013-02-15 11:42:30 -06003354static void handle_watch_notify(struct ceph_osd_client *osdc,
3355 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003356{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003357 void *p = msg->front.iov_base;
3358 void *const end = p + msg->front.iov_len;
3359 struct ceph_osd_linger_request *lreq;
3360 struct linger_work *lwork;
3361 u8 proto_ver, opcode;
3362 u64 cookie, notify_id;
3363 u64 notifier_id = 0;
Ilya Dryomov19079202016-04-28 16:07:27 +02003364 s32 return_code = 0;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003365 void *payload = NULL;
3366 u32 payload_len = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003367
3368 ceph_decode_8_safe(&p, end, proto_ver, bad);
3369 ceph_decode_8_safe(&p, end, opcode, bad);
3370 ceph_decode_64_safe(&p, end, cookie, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003371 p += 8; /* skip ver */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003372 ceph_decode_64_safe(&p, end, notify_id, bad);
3373
Ilya Dryomov922dab62016-05-26 01:15:02 +02003374 if (proto_ver >= 1) {
3375 ceph_decode_32_safe(&p, end, payload_len, bad);
3376 ceph_decode_need(&p, end, payload_len, bad);
3377 payload = p;
3378 p += payload_len;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003379 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003380
3381 if (le16_to_cpu(msg->hdr.version) >= 2)
Ilya Dryomov19079202016-04-28 16:07:27 +02003382 ceph_decode_32_safe(&p, end, return_code, bad);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003383
3384 if (le16_to_cpu(msg->hdr.version) >= 3)
3385 ceph_decode_64_safe(&p, end, notifier_id, bad);
3386
3387 down_read(&osdc->lock);
3388 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
3389 if (!lreq) {
3390 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
3391 cookie);
3392 goto out_unlock_osdc;
3393 }
3394
3395 mutex_lock(&lreq->lock);
Ilya Dryomov19079202016-04-28 16:07:27 +02003396 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
3397 opcode, cookie, lreq, lreq->is_watch);
Ilya Dryomov922dab62016-05-26 01:15:02 +02003398 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
3399 if (!lreq->last_error) {
3400 lreq->last_error = -ENOTCONN;
3401 queue_watch_error(lreq);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003402 }
Ilya Dryomov19079202016-04-28 16:07:27 +02003403 } else if (!lreq->is_watch) {
3404 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
3405 if (lreq->notify_id && lreq->notify_id != notify_id) {
3406 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
3407 lreq->notify_id, notify_id);
3408 } else if (!completion_done(&lreq->notify_finish_wait)) {
3409 struct ceph_msg_data *data =
3410 list_first_entry_or_null(&msg->data,
3411 struct ceph_msg_data,
3412 links);
3413
3414 if (data) {
3415 if (lreq->preply_pages) {
3416 WARN_ON(data->type !=
3417 CEPH_MSG_DATA_PAGES);
3418 *lreq->preply_pages = data->pages;
3419 *lreq->preply_len = data->length;
3420 } else {
3421 ceph_release_page_vector(data->pages,
3422 calc_pages_for(0, data->length));
3423 }
3424 }
3425 lreq->notify_finish_error = return_code;
3426 complete_all(&lreq->notify_finish_wait);
3427 }
Ilya Dryomov922dab62016-05-26 01:15:02 +02003428 } else {
3429 /* CEPH_WATCH_EVENT_NOTIFY */
3430 lwork = lwork_alloc(lreq, do_watch_notify);
3431 if (!lwork) {
3432 pr_err("failed to allocate notify-lwork\n");
3433 goto out_unlock_lreq;
3434 }
Ilya Dryomov91883cd2014-09-11 12:18:53 +04003435
Ilya Dryomov922dab62016-05-26 01:15:02 +02003436 lwork->notify.notify_id = notify_id;
3437 lwork->notify.notifier_id = notifier_id;
3438 lwork->notify.payload = payload;
3439 lwork->notify.payload_len = payload_len;
3440 lwork->notify.msg = ceph_msg_get(msg);
3441 lwork_queue(lwork);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003442 }
3443
Ilya Dryomov922dab62016-05-26 01:15:02 +02003444out_unlock_lreq:
3445 mutex_unlock(&lreq->lock);
3446out_unlock_osdc:
3447 up_read(&osdc->lock);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003448 return;
3449
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003450bad:
3451 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003452}
3453
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003454/*
Sage Weilf24e9982009-10-06 11:31:10 -07003455 * Register request, send initial attempt.
3456 */
3457int ceph_osdc_start_request(struct ceph_osd_client *osdc,
3458 struct ceph_osd_request *req,
3459 bool nofail)
3460{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003461 down_read(&osdc->lock);
3462 submit_request(req, false);
3463 up_read(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003464
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003465 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07003466}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003467EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07003468
3469/*
Ilya Dryomovc297eb422016-12-02 14:01:55 +01003470 * Unregister a registered request. The request is not completed:
3471 * ->r_result isn't set and __complete_request() isn't called.
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04003472 */
3473void ceph_osdc_cancel_request(struct ceph_osd_request *req)
3474{
3475 struct ceph_osd_client *osdc = req->r_osdc;
3476
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003477 down_write(&osdc->lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003478 if (req->r_osd)
3479 cancel_request(req);
3480 up_write(&osdc->lock);
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04003481}
3482EXPORT_SYMBOL(ceph_osdc_cancel_request);
3483
3484/*
Ilya Dryomov42b06962016-04-28 16:07:26 +02003485 * @timeout: in jiffies, 0 means "wait forever"
3486 */
3487static int wait_request_timeout(struct ceph_osd_request *req,
3488 unsigned long timeout)
3489{
3490 long left;
3491
3492 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
Yan, Zheng0e76abf2016-05-13 11:04:33 +08003493 left = wait_for_completion_killable_timeout(&req->r_completion,
Ilya Dryomov42b06962016-04-28 16:07:26 +02003494 ceph_timeout_jiffies(timeout));
3495 if (left <= 0) {
3496 left = left ?: -ETIMEDOUT;
3497 ceph_osdc_cancel_request(req);
Ilya Dryomov42b06962016-04-28 16:07:26 +02003498 } else {
3499 left = req->r_result; /* completed */
3500 }
3501
3502 return left;
3503}
3504
3505/*
Sage Weilf24e9982009-10-06 11:31:10 -07003506 * wait for a request to complete
3507 */
3508int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
3509 struct ceph_osd_request *req)
3510{
Ilya Dryomov42b06962016-04-28 16:07:26 +02003511 return wait_request_timeout(req, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07003512}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003513EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07003514
3515/*
3516 * sync - wait for all in-flight requests to flush. avoid starvation.
3517 */
3518void ceph_osdc_sync(struct ceph_osd_client *osdc)
3519{
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003520 struct rb_node *n, *p;
3521 u64 last_tid = atomic64_read(&osdc->last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07003522
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003523again:
3524 down_read(&osdc->lock);
3525 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3526 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07003527
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003528 mutex_lock(&osd->lock);
3529 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
3530 struct ceph_osd_request *req =
3531 rb_entry(p, struct ceph_osd_request, r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07003532
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003533 if (req->r_tid > last_tid)
3534 break;
3535
3536 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
3537 continue;
3538
3539 ceph_osdc_get_request(req);
3540 mutex_unlock(&osd->lock);
3541 up_read(&osdc->lock);
3542 dout("%s waiting on req %p tid %llu last_tid %llu\n",
3543 __func__, req, req->r_tid, last_tid);
Ilya Dryomovc297eb422016-12-02 14:01:55 +01003544 wait_for_completion(&req->r_done_completion);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003545 ceph_osdc_put_request(req);
3546 goto again;
3547 }
3548
3549 mutex_unlock(&osd->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07003550 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02003551
3552 up_read(&osdc->lock);
3553 dout("%s done last_tid %llu\n", __func__, last_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07003554}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003555EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07003556
Ilya Dryomov922dab62016-05-26 01:15:02 +02003557static struct ceph_osd_request *
3558alloc_linger_request(struct ceph_osd_linger_request *lreq)
3559{
3560 struct ceph_osd_request *req;
3561
3562 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
3563 if (!req)
3564 return NULL;
3565
3566 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3567 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3568
3569 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
3570 ceph_osdc_put_request(req);
3571 return NULL;
3572 }
3573
3574 return req;
3575}
3576
3577/*
3578 * Returns a handle, caller owns a ref.
3579 */
3580struct ceph_osd_linger_request *
3581ceph_osdc_watch(struct ceph_osd_client *osdc,
3582 struct ceph_object_id *oid,
3583 struct ceph_object_locator *oloc,
3584 rados_watchcb2_t wcb,
3585 rados_watcherrcb_t errcb,
3586 void *data)
3587{
3588 struct ceph_osd_linger_request *lreq;
3589 int ret;
3590
3591 lreq = linger_alloc(osdc);
3592 if (!lreq)
3593 return ERR_PTR(-ENOMEM);
3594
Ilya Dryomov19079202016-04-28 16:07:27 +02003595 lreq->is_watch = true;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003596 lreq->wcb = wcb;
3597 lreq->errcb = errcb;
3598 lreq->data = data;
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02003599 lreq->watch_valid_thru = jiffies;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003600
3601 ceph_oid_copy(&lreq->t.base_oid, oid);
3602 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3603 lreq->t.flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
3604 lreq->mtime = CURRENT_TIME;
3605
3606 lreq->reg_req = alloc_linger_request(lreq);
3607 if (!lreq->reg_req) {
3608 ret = -ENOMEM;
3609 goto err_put_lreq;
3610 }
3611
3612 lreq->ping_req = alloc_linger_request(lreq);
3613 if (!lreq->ping_req) {
3614 ret = -ENOMEM;
3615 goto err_put_lreq;
3616 }
3617
3618 down_write(&osdc->lock);
3619 linger_register(lreq); /* before osd_req_op_* */
3620 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
3621 CEPH_OSD_WATCH_OP_WATCH);
3622 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
3623 CEPH_OSD_WATCH_OP_PING);
3624 linger_submit(lreq);
3625 up_write(&osdc->lock);
3626
3627 ret = linger_reg_commit_wait(lreq);
3628 if (ret) {
3629 linger_cancel(lreq);
3630 goto err_put_lreq;
3631 }
3632
3633 return lreq;
3634
3635err_put_lreq:
3636 linger_put(lreq);
3637 return ERR_PTR(ret);
3638}
3639EXPORT_SYMBOL(ceph_osdc_watch);
3640
3641/*
3642 * Releases a ref.
3643 *
3644 * Times out after mount_timeout to preserve rbd unmap behaviour
3645 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
3646 * with mount_timeout").
3647 */
3648int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
3649 struct ceph_osd_linger_request *lreq)
3650{
3651 struct ceph_options *opts = osdc->client->options;
3652 struct ceph_osd_request *req;
3653 int ret;
3654
3655 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3656 if (!req)
3657 return -ENOMEM;
3658
3659 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3660 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3661 req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
3662 req->r_mtime = CURRENT_TIME;
3663 osd_req_op_watch_init(req, 0, lreq->linger_id,
3664 CEPH_OSD_WATCH_OP_UNWATCH);
3665
3666 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3667 if (ret)
3668 goto out_put_req;
3669
3670 ceph_osdc_start_request(osdc, req, false);
3671 linger_cancel(lreq);
3672 linger_put(lreq);
3673 ret = wait_request_timeout(req, opts->mount_timeout);
3674
3675out_put_req:
3676 ceph_osdc_put_request(req);
3677 return ret;
3678}
3679EXPORT_SYMBOL(ceph_osdc_unwatch);
3680
3681static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
3682 u64 notify_id, u64 cookie, void *payload,
3683 size_t payload_len)
3684{
3685 struct ceph_osd_req_op *op;
3686 struct ceph_pagelist *pl;
3687 int ret;
3688
3689 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
3690
3691 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3692 if (!pl)
3693 return -ENOMEM;
3694
3695 ceph_pagelist_init(pl);
3696 ret = ceph_pagelist_encode_64(pl, notify_id);
3697 ret |= ceph_pagelist_encode_64(pl, cookie);
3698 if (payload) {
3699 ret |= ceph_pagelist_encode_32(pl, payload_len);
3700 ret |= ceph_pagelist_append(pl, payload, payload_len);
3701 } else {
3702 ret |= ceph_pagelist_encode_32(pl, 0);
3703 }
3704 if (ret) {
3705 ceph_pagelist_release(pl);
3706 return -ENOMEM;
3707 }
3708
3709 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
3710 op->indata_len = pl->length;
3711 return 0;
3712}
3713
3714int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
3715 struct ceph_object_id *oid,
3716 struct ceph_object_locator *oloc,
3717 u64 notify_id,
3718 u64 cookie,
3719 void *payload,
3720 size_t payload_len)
3721{
3722 struct ceph_osd_request *req;
3723 int ret;
3724
3725 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3726 if (!req)
3727 return -ENOMEM;
3728
3729 ceph_oid_copy(&req->r_base_oid, oid);
3730 ceph_oloc_copy(&req->r_base_oloc, oloc);
3731 req->r_flags = CEPH_OSD_FLAG_READ;
3732
3733 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3734 if (ret)
3735 goto out_put_req;
3736
3737 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
3738 payload_len);
3739 if (ret)
3740 goto out_put_req;
3741
3742 ceph_osdc_start_request(osdc, req, false);
3743 ret = ceph_osdc_wait_request(osdc, req);
3744
3745out_put_req:
3746 ceph_osdc_put_request(req);
3747 return ret;
3748}
3749EXPORT_SYMBOL(ceph_osdc_notify_ack);
3750
Ilya Dryomov19079202016-04-28 16:07:27 +02003751static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
3752 u64 cookie, u32 prot_ver, u32 timeout,
3753 void *payload, size_t payload_len)
3754{
3755 struct ceph_osd_req_op *op;
3756 struct ceph_pagelist *pl;
3757 int ret;
3758
3759 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
3760 op->notify.cookie = cookie;
3761
3762 pl = kmalloc(sizeof(*pl), GFP_NOIO);
3763 if (!pl)
3764 return -ENOMEM;
3765
3766 ceph_pagelist_init(pl);
3767 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
3768 ret |= ceph_pagelist_encode_32(pl, timeout);
3769 ret |= ceph_pagelist_encode_32(pl, payload_len);
3770 ret |= ceph_pagelist_append(pl, payload, payload_len);
3771 if (ret) {
3772 ceph_pagelist_release(pl);
3773 return -ENOMEM;
3774 }
3775
3776 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
3777 op->indata_len = pl->length;
3778 return 0;
3779}
3780
3781/*
3782 * @timeout: in seconds
3783 *
3784 * @preply_{pages,len} are initialized both on success and error.
3785 * The caller is responsible for:
3786 *
3787 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
3788 */
3789int ceph_osdc_notify(struct ceph_osd_client *osdc,
3790 struct ceph_object_id *oid,
3791 struct ceph_object_locator *oloc,
3792 void *payload,
3793 size_t payload_len,
3794 u32 timeout,
3795 struct page ***preply_pages,
3796 size_t *preply_len)
3797{
3798 struct ceph_osd_linger_request *lreq;
3799 struct page **pages;
3800 int ret;
3801
3802 WARN_ON(!timeout);
3803 if (preply_pages) {
3804 *preply_pages = NULL;
3805 *preply_len = 0;
3806 }
3807
3808 lreq = linger_alloc(osdc);
3809 if (!lreq)
3810 return -ENOMEM;
3811
3812 lreq->preply_pages = preply_pages;
3813 lreq->preply_len = preply_len;
3814
3815 ceph_oid_copy(&lreq->t.base_oid, oid);
3816 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3817 lreq->t.flags = CEPH_OSD_FLAG_READ;
3818
3819 lreq->reg_req = alloc_linger_request(lreq);
3820 if (!lreq->reg_req) {
3821 ret = -ENOMEM;
3822 goto out_put_lreq;
3823 }
3824
3825 /* for notify_id */
3826 pages = ceph_alloc_page_vector(1, GFP_NOIO);
3827 if (IS_ERR(pages)) {
3828 ret = PTR_ERR(pages);
3829 goto out_put_lreq;
3830 }
3831
3832 down_write(&osdc->lock);
3833 linger_register(lreq); /* before osd_req_op_* */
3834 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
3835 timeout, payload, payload_len);
3836 if (ret) {
3837 linger_unregister(lreq);
3838 up_write(&osdc->lock);
3839 ceph_release_page_vector(pages, 1);
3840 goto out_put_lreq;
3841 }
3842 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
3843 response_data),
3844 pages, PAGE_SIZE, 0, false, true);
3845 linger_submit(lreq);
3846 up_write(&osdc->lock);
3847
3848 ret = linger_reg_commit_wait(lreq);
3849 if (!ret)
3850 ret = linger_notify_finish_wait(lreq);
3851 else
3852 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
3853
3854 linger_cancel(lreq);
3855out_put_lreq:
3856 linger_put(lreq);
3857 return ret;
3858}
3859EXPORT_SYMBOL(ceph_osdc_notify);
3860
Sage Weilf24e9982009-10-06 11:31:10 -07003861/*
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02003862 * Return the number of milliseconds since the watch was last
3863 * confirmed, or an error. If there is an error, the watch is no
3864 * longer valid, and should be destroyed with ceph_osdc_unwatch().
3865 */
3866int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
3867 struct ceph_osd_linger_request *lreq)
3868{
3869 unsigned long stamp, age;
3870 int ret;
3871
3872 down_read(&osdc->lock);
3873 mutex_lock(&lreq->lock);
3874 stamp = lreq->watch_valid_thru;
3875 if (!list_empty(&lreq->pending_lworks)) {
3876 struct linger_work *lwork =
3877 list_first_entry(&lreq->pending_lworks,
3878 struct linger_work,
3879 pending_item);
3880
3881 if (time_before(lwork->queued_stamp, stamp))
3882 stamp = lwork->queued_stamp;
3883 }
3884 age = jiffies - stamp;
3885 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
3886 lreq, lreq->linger_id, age, lreq->last_error);
3887 /* we are truncating to msecs, so return a safe upper bound */
3888 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
3889
3890 mutex_unlock(&lreq->lock);
3891 up_read(&osdc->lock);
3892 return ret;
3893}
3894
Douglas Fullera4ed38d2015-07-17 13:18:07 -07003895static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
3896{
3897 u8 struct_v;
3898 u32 struct_len;
3899 int ret;
3900
3901 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
3902 &struct_v, &struct_len);
3903 if (ret)
3904 return ret;
3905
3906 ceph_decode_copy(p, &item->name, sizeof(item->name));
3907 item->cookie = ceph_decode_64(p);
3908 *p += 4; /* skip timeout_seconds */
3909 if (struct_v >= 2) {
3910 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
3911 ceph_decode_addr(&item->addr);
3912 }
3913
3914 dout("%s %s%llu cookie %llu addr %s\n", __func__,
3915 ENTITY_NAME(item->name), item->cookie,
3916 ceph_pr_addr(&item->addr.in_addr));
3917 return 0;
3918}
3919
3920static int decode_watchers(void **p, void *end,
3921 struct ceph_watch_item **watchers,
3922 u32 *num_watchers)
3923{
3924 u8 struct_v;
3925 u32 struct_len;
3926 int i;
3927 int ret;
3928
3929 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
3930 &struct_v, &struct_len);
3931 if (ret)
3932 return ret;
3933
3934 *num_watchers = ceph_decode_32(p);
3935 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
3936 if (!*watchers)
3937 return -ENOMEM;
3938
3939 for (i = 0; i < *num_watchers; i++) {
3940 ret = decode_watcher(p, end, *watchers + i);
3941 if (ret) {
3942 kfree(*watchers);
3943 return ret;
3944 }
3945 }
3946
3947 return 0;
3948}
3949
3950/*
3951 * On success, the caller is responsible for:
3952 *
3953 * kfree(watchers);
3954 */
3955int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
3956 struct ceph_object_id *oid,
3957 struct ceph_object_locator *oloc,
3958 struct ceph_watch_item **watchers,
3959 u32 *num_watchers)
3960{
3961 struct ceph_osd_request *req;
3962 struct page **pages;
3963 int ret;
3964
3965 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3966 if (!req)
3967 return -ENOMEM;
3968
3969 ceph_oid_copy(&req->r_base_oid, oid);
3970 ceph_oloc_copy(&req->r_base_oloc, oloc);
3971 req->r_flags = CEPH_OSD_FLAG_READ;
3972
3973 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3974 if (ret)
3975 goto out_put_req;
3976
3977 pages = ceph_alloc_page_vector(1, GFP_NOIO);
3978 if (IS_ERR(pages)) {
3979 ret = PTR_ERR(pages);
3980 goto out_put_req;
3981 }
3982
3983 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
3984 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
3985 response_data),
3986 pages, PAGE_SIZE, 0, false, true);
3987
3988 ceph_osdc_start_request(osdc, req, false);
3989 ret = ceph_osdc_wait_request(osdc, req);
3990 if (ret >= 0) {
3991 void *p = page_address(pages[0]);
3992 void *const end = p + req->r_ops[0].outdata_len;
3993
3994 ret = decode_watchers(&p, end, watchers, num_watchers);
3995 }
3996
3997out_put_req:
3998 ceph_osdc_put_request(req);
3999 return ret;
4000}
4001EXPORT_SYMBOL(ceph_osdc_list_watchers);
4002
Ilya Dryomovb07d3c42016-04-28 16:07:27 +02004003/*
Josh Durgindd935f42013-08-28 21:43:09 -07004004 * Call all pending notify callbacks - for use after a watch is
4005 * unregistered, to make sure no more callbacks for it will be invoked
4006 */
stephen hemmingerf64794492014-06-10 20:30:13 -07004007void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07004008{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004009 dout("%s osdc %p\n", __func__, osdc);
Josh Durgindd935f42013-08-28 21:43:09 -07004010 flush_workqueue(osdc->notify_wq);
4011}
4012EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4013
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02004014void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4015{
4016 down_read(&osdc->lock);
4017 maybe_request_map(osdc);
4018 up_read(&osdc->lock);
4019}
4020EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
Josh Durgindd935f42013-08-28 21:43:09 -07004021
4022/*
Douglas Fuller428a7152015-06-17 14:49:45 -04004023 * Execute an OSD class method on an object.
4024 *
4025 * @flags: CEPH_OSD_FLAG_*
Ilya Dryomov2544a022017-01-25 18:16:21 +01004026 * @resp_len: in/out param for reply length
Douglas Fuller428a7152015-06-17 14:49:45 -04004027 */
4028int ceph_osdc_call(struct ceph_osd_client *osdc,
4029 struct ceph_object_id *oid,
4030 struct ceph_object_locator *oloc,
4031 const char *class, const char *method,
4032 unsigned int flags,
4033 struct page *req_page, size_t req_len,
4034 struct page *resp_page, size_t *resp_len)
4035{
4036 struct ceph_osd_request *req;
4037 int ret;
4038
Ilya Dryomov2544a022017-01-25 18:16:21 +01004039 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4040 return -E2BIG;
4041
Douglas Fuller428a7152015-06-17 14:49:45 -04004042 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4043 if (!req)
4044 return -ENOMEM;
4045
4046 ceph_oid_copy(&req->r_base_oid, oid);
4047 ceph_oloc_copy(&req->r_base_oloc, oloc);
4048 req->r_flags = flags;
4049
4050 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4051 if (ret)
4052 goto out_put_req;
4053
4054 osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4055 if (req_page)
4056 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4057 0, false, false);
4058 if (resp_page)
4059 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
Ilya Dryomov2544a022017-01-25 18:16:21 +01004060 *resp_len, 0, false, false);
Douglas Fuller428a7152015-06-17 14:49:45 -04004061
4062 ceph_osdc_start_request(osdc, req, false);
4063 ret = ceph_osdc_wait_request(osdc, req);
4064 if (ret >= 0) {
4065 ret = req->r_ops[0].rval;
4066 if (resp_page)
4067 *resp_len = req->r_ops[0].outdata_len;
4068 }
4069
4070out_put_req:
4071 ceph_osdc_put_request(req);
4072 return ret;
4073}
4074EXPORT_SYMBOL(ceph_osdc_call);
4075
4076/*
Sage Weilf24e9982009-10-06 11:31:10 -07004077 * init, shutdown
4078 */
4079int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4080{
4081 int err;
4082
4083 dout("init\n");
4084 osdc->client = client;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004085 init_rwsem(&osdc->lock);
Sage Weilf24e9982009-10-06 11:31:10 -07004086 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004087 INIT_LIST_HEAD(&osdc->osd_lru);
Ilya Dryomov9dd28452016-04-28 16:07:26 +02004088 spin_lock_init(&osdc->osd_lru_lock);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004089 osd_init(&osdc->homeless_osd);
4090 osdc->homeless_osd.o_osdc = osdc;
4091 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
Ilya Dryomov264048a2016-11-08 15:15:24 +01004092 osdc->last_linger_id = CEPH_LINGER_ID_START;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004093 osdc->linger_requests = RB_ROOT;
Ilya Dryomov46092452016-04-28 16:07:27 +02004094 osdc->map_checks = RB_ROOT;
4095 osdc->linger_map_checks = RB_ROOT;
Sage Weilf24e9982009-10-06 11:31:10 -07004096 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004097 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
4098
Sage Weil5f44f142009-11-18 14:52:18 -08004099 err = -ENOMEM;
Ilya Dryomove5253a72016-04-28 16:07:25 +02004100 osdc->osdmap = ceph_osdmap_alloc();
4101 if (!osdc->osdmap)
4102 goto out;
4103
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01004104 osdc->req_mempool = mempool_create_slab_pool(10,
4105 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07004106 if (!osdc->req_mempool)
Ilya Dryomove5253a72016-04-28 16:07:25 +02004107 goto out_map;
Sage Weilf24e9982009-10-06 11:31:10 -07004108
Sage Weild50b4092012-07-09 14:22:34 -07004109 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02004110 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07004111 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08004112 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07004113 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02004114 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08004115 if (err < 0)
4116 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004117
Dan Carpenterdbcae082013-08-15 08:58:59 +03004118 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004119 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03004120 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02004121 goto out_msgpool_reply;
4122
Ilya Dryomovfbca9632016-04-28 16:07:24 +02004123 schedule_delayed_work(&osdc->timeout_work,
4124 osdc->client->options->osd_keepalive_timeout);
Ilya Dryomovb37ee1b2016-04-28 16:07:24 +02004125 schedule_delayed_work(&osdc->osds_timeout_work,
4126 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4127
Sage Weilf24e9982009-10-06 11:31:10 -07004128 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08004129
Ilya Dryomovc172ec52014-01-31 17:49:22 +02004130out_msgpool_reply:
4131 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08004132out_msgpool:
4133 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08004134out_mempool:
4135 mempool_destroy(osdc->req_mempool);
Ilya Dryomove5253a72016-04-28 16:07:25 +02004136out_map:
4137 ceph_osdmap_destroy(osdc->osdmap);
Sage Weil5f44f142009-11-18 14:52:18 -08004138out:
4139 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07004140}
4141
4142void ceph_osdc_stop(struct ceph_osd_client *osdc)
4143{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004144 flush_workqueue(osdc->notify_wq);
4145 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07004146 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08004147 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004148
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004149 down_write(&osdc->lock);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004150 while (!RB_EMPTY_ROOT(&osdc->osds)) {
4151 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
4152 struct ceph_osd, o_node);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004153 close_osd(osd);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004154 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004155 up_write(&osdc->lock);
4156 WARN_ON(atomic_read(&osdc->homeless_osd.o_ref) != 1);
4157 osd_cleanup(&osdc->homeless_osd);
4158
4159 WARN_ON(!list_empty(&osdc->osd_lru));
Ilya Dryomov922dab62016-05-26 01:15:02 +02004160 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
Ilya Dryomov46092452016-04-28 16:07:27 +02004161 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
4162 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004163 WARN_ON(atomic_read(&osdc->num_requests));
4164 WARN_ON(atomic_read(&osdc->num_homeless));
Ilya Dryomov42a2c092016-04-28 16:07:22 +02004165
Ilya Dryomove5253a72016-04-28 16:07:25 +02004166 ceph_osdmap_destroy(osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07004167 mempool_destroy(osdc->req_mempool);
4168 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08004169 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07004170}
4171
4172/*
4173 * Read some contiguous pages. If we cross a stripe boundary, shorten
4174 * *plen. Return number of bytes read, or error.
4175 */
4176int ceph_osdc_readpages(struct ceph_osd_client *osdc,
4177 struct ceph_vino vino, struct ceph_file_layout *layout,
4178 u64 off, u64 *plen,
4179 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08004180 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07004181{
4182 struct ceph_osd_request *req;
4183 int rc = 0;
4184
4185 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
4186 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08004187 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07004188 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05004189 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06004190 false);
Sage Weil68162822012-09-24 21:01:02 -07004191 if (IS_ERR(req))
4192 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07004193
4194 /* it may be a short read due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05004195 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05004196 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07004197
Alex Eldere0c59482013-03-07 15:38:25 -06004198 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05004199 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07004200
4201 rc = ceph_osdc_start_request(osdc, req, false);
4202 if (!rc)
4203 rc = ceph_osdc_wait_request(osdc, req);
4204
4205 ceph_osdc_put_request(req);
4206 dout("readpages result %d\n", rc);
4207 return rc;
4208}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004209EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07004210
4211/*
4212 * do a synchronous write on N pages
4213 */
4214int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
4215 struct ceph_file_layout *layout,
4216 struct ceph_snap_context *snapc,
4217 u64 off, u64 len,
4218 u32 truncate_seq, u64 truncate_size,
4219 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06004220 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07004221{
4222 struct ceph_osd_request *req;
4223 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08004224 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07004225
Yan, Zheng715e4cd2014-11-13 14:40:37 +08004226 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07004227 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06004228 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05004229 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06004230 true);
Sage Weil68162822012-09-24 21:01:02 -07004231 if (IS_ERR(req))
4232 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07004233
4234 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05004235 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05004236 false, false);
4237 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07004238
Ilya Dryomovbb873b52016-05-26 00:29:52 +02004239 req->r_mtime = *mtime;
Alex Elder87f979d2013-02-15 11:42:29 -06004240 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07004241 if (!rc)
4242 rc = ceph_osdc_wait_request(osdc, req);
4243
4244 ceph_osdc_put_request(req);
4245 if (rc == 0)
4246 rc = len;
4247 dout("writepages result %d\n", rc);
4248 return rc;
4249}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004250EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07004251
Alex Elder5522ae02013-05-01 12:43:04 -05004252int ceph_osdc_setup(void)
4253{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01004254 size_t size = sizeof(struct ceph_osd_request) +
4255 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
4256
Alex Elder5522ae02013-05-01 12:43:04 -05004257 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01004258 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
4259 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05004260
4261 return ceph_osd_request_cache ? 0 : -ENOMEM;
4262}
4263EXPORT_SYMBOL(ceph_osdc_setup);
4264
4265void ceph_osdc_cleanup(void)
4266{
4267 BUG_ON(!ceph_osd_request_cache);
4268 kmem_cache_destroy(ceph_osd_request_cache);
4269 ceph_osd_request_cache = NULL;
4270}
4271EXPORT_SYMBOL(ceph_osdc_cleanup);
4272
Sage Weilf24e9982009-10-06 11:31:10 -07004273/*
4274 * handle incoming message
4275 */
4276static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4277{
4278 struct ceph_osd *osd = con->private;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004279 struct ceph_osd_client *osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07004280 int type = le16_to_cpu(msg->hdr.type);
4281
Sage Weilf24e9982009-10-06 11:31:10 -07004282 switch (type) {
4283 case CEPH_MSG_OSD_MAP:
4284 ceph_osdc_handle_map(osdc, msg);
4285 break;
4286 case CEPH_MSG_OSD_OPREPLY:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004287 handle_reply(osd, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07004288 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004289 case CEPH_MSG_WATCH_NOTIFY:
4290 handle_watch_notify(osdc, msg);
4291 break;
Sage Weilf24e9982009-10-06 11:31:10 -07004292
4293 default:
4294 pr_err("received unknown message type %d %s\n", type,
4295 ceph_msg_type_name(type));
4296 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004297
Sage Weilf24e9982009-10-06 11:31:10 -07004298 ceph_msg_put(msg);
4299}
4300
Sage Weil5b3a4db2010-02-19 21:43:23 -08004301/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004302 * Lookup and return message for incoming reply. Don't try to do
4303 * anything about a larger than preallocated data portion of the
4304 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08004305 */
4306static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08004307 struct ceph_msg_header *hdr,
4308 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07004309{
4310 struct ceph_osd *osd = con->private;
4311 struct ceph_osd_client *osdc = osd->o_osdc;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004312 struct ceph_msg *m = NULL;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004313 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02004314 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004315 int data_len = le32_to_cpu(hdr->data_len);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004316 u64 tid = le64_to_cpu(hdr->tid);
Sage Weilf24e9982009-10-06 11:31:10 -07004317
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004318 down_read(&osdc->lock);
4319 if (!osd_registered(osd)) {
4320 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
4321 *skip = 1;
4322 goto out_unlock_osdc;
4323 }
4324 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
4325
4326 mutex_lock(&osd->lock);
4327 req = lookup_request(&osd->o_requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004328 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01004329 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
4330 osd->o_osd, tid);
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004331 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004332 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004333 }
Sage Weilc16e7862010-03-01 13:02:00 -08004334
Alex Elderace6d3a2013-04-01 16:12:14 -05004335 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08004336
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02004337 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004338 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
4339 __func__, osd->o_osd, req->r_tid, front_len,
4340 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02004341 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
4342 false);
Sage Weila79832f2010-04-01 16:06:19 -07004343 if (!m)
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004344 goto out_unlock_session;
Sage Weilc16e7862010-03-01 13:02:00 -08004345 ceph_msg_put(req->r_reply);
4346 req->r_reply = m;
4347 }
Sage Weilc16e7862010-03-01 13:02:00 -08004348
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004349 if (data_len > req->r_reply->data_length) {
4350 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
4351 __func__, osd->o_osd, req->r_tid, data_len,
4352 req->r_reply->data_length);
4353 m = NULL;
4354 *skip = 1;
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004355 goto out_unlock_session;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004356 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03004357
4358 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08004359 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08004360
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004361out_unlock_session:
4362 mutex_unlock(&osd->lock);
4363out_unlock_osdc:
4364 up_read(&osdc->lock);
Yehuda Sadeh24504182010-01-08 13:58:34 -08004365 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08004366}
4367
Ilya Dryomov19079202016-04-28 16:07:27 +02004368/*
4369 * TODO: switch to a msg-owned pagelist
4370 */
4371static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
4372{
4373 struct ceph_msg *m;
4374 int type = le16_to_cpu(hdr->type);
4375 u32 front_len = le32_to_cpu(hdr->front_len);
4376 u32 data_len = le32_to_cpu(hdr->data_len);
4377
4378 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
4379 if (!m)
4380 return NULL;
4381
4382 if (data_len) {
4383 struct page **pages;
4384 struct ceph_osd_data osd_data;
4385
4386 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
4387 GFP_NOIO);
Wei Yongjunc22e8532016-07-30 00:37:57 +00004388 if (IS_ERR(pages)) {
Ilya Dryomov19079202016-04-28 16:07:27 +02004389 ceph_msg_put(m);
4390 return NULL;
4391 }
4392
4393 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
4394 false);
4395 ceph_osdc_msg_data_add(m, &osd_data);
4396 }
4397
4398 return m;
4399}
4400
Sage Weil5b3a4db2010-02-19 21:43:23 -08004401static struct ceph_msg *alloc_msg(struct ceph_connection *con,
4402 struct ceph_msg_header *hdr,
4403 int *skip)
4404{
4405 struct ceph_osd *osd = con->private;
4406 int type = le16_to_cpu(hdr->type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004407
Alex Elder1c20f2d2012-06-04 14:43:32 -05004408 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08004409 switch (type) {
4410 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07004411 case CEPH_MSG_WATCH_NOTIFY:
Ilya Dryomov19079202016-04-28 16:07:27 +02004412 return alloc_msg_with_page_vector(hdr);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004413 case CEPH_MSG_OSD_OPREPLY:
4414 return get_reply(con, hdr, skip);
4415 default:
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004416 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
4417 osd->o_osd, type);
Sage Weil5b3a4db2010-02-19 21:43:23 -08004418 *skip = 1;
4419 return NULL;
4420 }
Sage Weilf24e9982009-10-06 11:31:10 -07004421}
4422
4423/*
4424 * Wrappers to refcount containing ceph_osd struct
4425 */
4426static struct ceph_connection *get_osd_con(struct ceph_connection *con)
4427{
4428 struct ceph_osd *osd = con->private;
4429 if (get_osd(osd))
4430 return con;
4431 return NULL;
4432}
4433
4434static void put_osd_con(struct ceph_connection *con)
4435{
4436 struct ceph_osd *osd = con->private;
4437 put_osd(osd);
4438}
4439
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004440/*
4441 * authentication
4442 */
Alex Eldera3530df2012-05-16 15:16:39 -05004443/*
4444 * Note: returned pointer is the address of a structure that's
4445 * managed separately. Caller must *not* attempt to free it.
4446 */
4447static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004448 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004449{
4450 struct ceph_osd *o = con->private;
4451 struct ceph_osd_client *osdc = o->o_osdc;
4452 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004453 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004454
Alex Elder74f18692012-05-16 15:16:39 -05004455 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004456 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004457 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004458 }
Sage Weil27859f92013-03-25 10:26:14 -07004459 if (!auth->authorizer) {
4460 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4461 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004462 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004463 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004464 } else {
4465 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07004466 auth);
4467 if (ret)
4468 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004469 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004470 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004471
Alex Eldera3530df2012-05-16 15:16:39 -05004472 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004473}
4474
4475
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004476static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004477{
4478 struct ceph_osd *o = con->private;
4479 struct ceph_osd_client *osdc = o->o_osdc;
4480 struct ceph_auth_client *ac = osdc->client->monc.auth;
4481
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004482 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004483}
4484
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004485static int invalidate_authorizer(struct ceph_connection *con)
4486{
4487 struct ceph_osd *o = con->private;
4488 struct ceph_osd_client *osdc = o->o_osdc;
4489 struct ceph_auth_client *ac = osdc->client->monc.auth;
4490
Sage Weil27859f92013-03-25 10:26:14 -07004491 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004492 return ceph_monc_validate_auth(&osdc->client->monc);
4493}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004494
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004495static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004496{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004497 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004498 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004499
Yan, Zheng33d07332014-11-04 16:33:37 +08004500 return ceph_auth_sign_message(auth, msg);
4501}
4502
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004503static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004504{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004505 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004506 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004507
Yan, Zheng33d07332014-11-04 16:33:37 +08004508 return ceph_auth_check_message_signature(auth, msg);
4509}
4510
Tobias Klauser9e327892010-05-20 10:40:19 +02004511static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07004512 .get = get_osd_con,
4513 .put = put_osd_con,
4514 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004515 .get_authorizer = get_authorizer,
4516 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004517 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07004518 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004519 .sign_message = osd_sign_message,
4520 .check_message_signature = osd_check_message_signature,
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +02004521 .fault = osd_fault,
Sage Weilf24e9982009-10-06 11:31:10 -07004522};