blob: 013101598c4109ef1bed5daccbf738f832d9b8f8 [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
Alex Elderf9d25192013-02-15 11:42:29 -060028static void __send_queued(struct ceph_osd_client *osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -080029static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070030static void __register_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040032static void __unregister_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070034static void __unregister_linger_request(struct ceph_osd_client *osdc,
35 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040036static void __enqueue_request(struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080037static void __send_request(struct ceph_osd_client *osdc,
38 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070039
40/*
41 * Implement client access to distributed object storage cluster.
42 *
43 * All data objects are stored within a cluster/cloud of OSDs, or
44 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
45 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
46 * remote daemons serving up and coordinating consistent and safe
47 * access to storage.
48 *
49 * Cluster membership and the mapping of data objects onto storage devices
50 * are described by the osd map.
51 *
52 * We keep track of pending OSD requests (read, write), resubmit
53 * requests to different OSDs when the cluster topology/data layout
54 * change, or retry the affected requests when the communications
55 * channel with an OSD is reset.
56 */
57
58/*
59 * calculate the mapping of a file extent onto an object, and fill out the
60 * request accordingly. shorten extent as necessary if it crosses an
61 * object boundary.
62 *
63 * fill osd op in request message.
64 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060065static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050066 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070067{
Alex Elder60e56f12013-02-15 11:42:29 -060068 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070069 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070070
Alex Elder60e56f12013-02-15 11:42:29 -060071 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050072 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
73 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070074 if (r < 0)
75 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050076 if (*objlen < orig_len) {
77 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060078 dout(" skipping last %llu, final file extent %llu~%llu\n",
79 orig_len - *plen, off, *plen);
80 }
81
Alex Elder75d1c942013-03-13 20:50:00 -050082 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070083
Alex Elder3ff5f382013-02-15 22:10:17 -060084 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070085}
86
Alex Elderc54d47b2013-04-03 01:28:57 -050087static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
88{
89 memset(osd_data, 0, sizeof (*osd_data));
90 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
91}
92
Alex Eldera4ce40a2013-04-05 01:27:12 -050093static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -050094 struct page **pages, u64 length, u32 alignment,
95 bool pages_from_pool, bool own_pages)
96{
97 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
98 osd_data->pages = pages;
99 osd_data->length = length;
100 osd_data->alignment = alignment;
101 osd_data->pages_from_pool = pages_from_pool;
102 osd_data->own_pages = own_pages;
103}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500104
Alex Eldera4ce40a2013-04-05 01:27:12 -0500105static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500106 struct ceph_pagelist *pagelist)
107{
108 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
109 osd_data->pagelist = pagelist;
110}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500111
112#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500113static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500114 struct bio *bio, size_t bio_length)
115{
116 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
117 osd_data->bio = bio;
118 osd_data->bio_length = bio_length;
119}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500120#endif /* CONFIG_BLOCK */
121
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300122#define osd_req_op_data(oreq, whch, typ, fld) \
123({ \
124 struct ceph_osd_request *__oreq = (oreq); \
125 unsigned int __whch = (whch); \
126 BUG_ON(__whch >= __oreq->r_num_ops); \
127 &__oreq->r_ops[__whch].typ.fld; \
128})
Alex Elder863c7eb2013-04-15 14:50:36 -0500129
Alex Elder49719772013-02-11 12:33:24 -0600130static struct ceph_osd_data *
131osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
132{
133 BUG_ON(which >= osd_req->r_num_ops);
134
135 return &osd_req->r_ops[which].raw_data_in;
136}
137
Alex Eldera4ce40a2013-04-05 01:27:12 -0500138struct ceph_osd_data *
139osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500140 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500141{
Alex Elder863c7eb2013-04-15 14:50:36 -0500142 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500143}
144EXPORT_SYMBOL(osd_req_op_extent_osd_data);
145
Alex Elder49719772013-02-11 12:33:24 -0600146void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
147 unsigned int which, struct page **pages,
148 u64 length, u32 alignment,
149 bool pages_from_pool, bool own_pages)
150{
151 struct ceph_osd_data *osd_data;
152
153 osd_data = osd_req_op_raw_data_in(osd_req, which);
154 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
155 pages_from_pool, own_pages);
156}
157EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
158
Alex Eldera4ce40a2013-04-05 01:27:12 -0500159void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500160 unsigned int which, struct page **pages,
161 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500162 bool pages_from_pool, bool own_pages)
163{
164 struct ceph_osd_data *osd_data;
165
Alex Elder863c7eb2013-04-15 14:50:36 -0500166 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500167 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
168 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500169}
170EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
171
172void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500173 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500174{
175 struct ceph_osd_data *osd_data;
176
Alex Elder863c7eb2013-04-15 14:50:36 -0500177 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500178 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500179}
180EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
181
182#ifdef CONFIG_BLOCK
183void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500184 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500185{
186 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500187
188 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500189 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500190}
191EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
192#endif /* CONFIG_BLOCK */
193
194static void osd_req_op_cls_request_info_pagelist(
195 struct ceph_osd_request *osd_req,
196 unsigned int which, struct ceph_pagelist *pagelist)
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, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500201 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500202}
203
Alex Elder04017e22013-04-05 14:46:02 -0500204void osd_req_op_cls_request_data_pagelist(
205 struct ceph_osd_request *osd_req,
206 unsigned int which, struct ceph_pagelist *pagelist)
207{
208 struct ceph_osd_data *osd_data;
209
Alex Elder863c7eb2013-04-15 14:50:36 -0500210 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500211 ceph_osd_data_pagelist_init(osd_data, pagelist);
212}
213EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
214
Alex Elder6c57b552013-04-19 15:34:49 -0500215void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
216 unsigned int which, struct page **pages, u64 length,
217 u32 alignment, bool pages_from_pool, bool own_pages)
218{
219 struct ceph_osd_data *osd_data;
220
221 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
222 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
223 pages_from_pool, own_pages);
224}
225EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
226
Alex Eldera4ce40a2013-04-05 01:27:12 -0500227void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
228 unsigned int which, struct page **pages, u64 length,
229 u32 alignment, bool pages_from_pool, bool own_pages)
230{
231 struct ceph_osd_data *osd_data;
232
Alex Elder863c7eb2013-04-15 14:50:36 -0500233 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500234 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
235 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500236}
237EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
238
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500239static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
240{
241 switch (osd_data->type) {
242 case CEPH_OSD_DATA_TYPE_NONE:
243 return 0;
244 case CEPH_OSD_DATA_TYPE_PAGES:
245 return osd_data->length;
246 case CEPH_OSD_DATA_TYPE_PAGELIST:
247 return (u64)osd_data->pagelist->length;
248#ifdef CONFIG_BLOCK
249 case CEPH_OSD_DATA_TYPE_BIO:
250 return (u64)osd_data->bio_length;
251#endif /* CONFIG_BLOCK */
252 default:
253 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
254 return 0;
255 }
256}
257
Alex Elderc54d47b2013-04-03 01:28:57 -0500258static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
259{
Alex Elder54764922013-04-05 01:27:12 -0500260 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500261 int num_pages;
262
263 num_pages = calc_pages_for((u64)osd_data->alignment,
264 (u64)osd_data->length);
265 ceph_release_page_vector(osd_data->pages, num_pages);
266 }
Alex Elder54764922013-04-05 01:27:12 -0500267 ceph_osd_data_init(osd_data);
268}
269
270static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
271 unsigned int which)
272{
273 struct ceph_osd_req_op *op;
274
275 BUG_ON(which >= osd_req->r_num_ops);
276 op = &osd_req->r_ops[which];
277
278 switch (op->op) {
279 case CEPH_OSD_OP_READ:
280 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200281 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500282 ceph_osd_data_release(&op->extent.osd_data);
283 break;
284 case CEPH_OSD_OP_CALL:
285 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500286 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500287 ceph_osd_data_release(&op->cls.response_data);
288 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800289 case CEPH_OSD_OP_SETXATTR:
290 case CEPH_OSD_OP_CMPXATTR:
291 ceph_osd_data_release(&op->xattr.osd_data);
292 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800293 case CEPH_OSD_OP_STAT:
294 ceph_osd_data_release(&op->raw_data_in);
295 break;
Alex Elder54764922013-04-05 01:27:12 -0500296 default:
297 break;
298 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500299}
300
Sage Weilf24e9982009-10-06 11:31:10 -0700301/*
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200302 * Assumes @t is zero-initialized.
303 */
304static void target_init(struct ceph_osd_request_target *t)
305{
306 ceph_oid_init(&t->base_oid);
307 ceph_oloc_init(&t->base_oloc);
308 ceph_oid_init(&t->target_oid);
309 ceph_oloc_init(&t->target_oloc);
310
311 ceph_osds_init(&t->acting);
312 ceph_osds_init(&t->up);
313 t->size = -1;
314 t->min_size = -1;
315
316 t->osd = CEPH_HOMELESS_OSD;
317}
318
319static void target_destroy(struct ceph_osd_request_target *t)
320{
321 ceph_oid_destroy(&t->base_oid);
322 ceph_oid_destroy(&t->target_oid);
323}
324
325/*
Sage Weilf24e9982009-10-06 11:31:10 -0700326 * requests
327 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400328static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700329{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400330 struct ceph_osd_request *req = container_of(kref,
331 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500332 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800333
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400334 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
335 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400336 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
337 WARN_ON(!list_empty(&req->r_req_lru_item));
338 WARN_ON(!list_empty(&req->r_osd_item));
339 WARN_ON(!list_empty(&req->r_linger_item));
340 WARN_ON(!list_empty(&req->r_linger_osd_item));
341 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400342
Sage Weil415e49a2009-12-07 13:37:03 -0800343 if (req->r_request)
344 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500345 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500346 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500347 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500348 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600349
Alex Elder54764922013-04-05 01:27:12 -0500350 for (which = 0; which < req->r_num_ops; which++)
351 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600352
Ilya Dryomova66dd382016-04-28 16:07:23 +0200353 target_destroy(&req->r_t);
Sage Weil415e49a2009-12-07 13:37:03 -0800354 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200355
Sage Weil415e49a2009-12-07 13:37:03 -0800356 if (req->r_mempool)
357 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100358 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500359 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100360 else
361 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700362}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400363
364void ceph_osdc_get_request(struct ceph_osd_request *req)
365{
366 dout("%s %p (was %d)\n", __func__, req,
367 atomic_read(&req->r_kref.refcount));
368 kref_get(&req->r_kref);
369}
370EXPORT_SYMBOL(ceph_osdc_get_request);
371
372void ceph_osdc_put_request(struct ceph_osd_request *req)
373{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200374 if (req) {
375 dout("%s %p (was %d)\n", __func__, req,
376 atomic_read(&req->r_kref.refcount));
377 kref_put(&req->r_kref, ceph_osdc_release_request);
378 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400379}
380EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700381
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700382struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700383 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800384 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700385 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600386 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700387{
388 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700389
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700390 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100391 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700392 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100393 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
394 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700395 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100396 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
397 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
398 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700399 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100400 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700401 return NULL;
402
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100403 /* req only, each op is zeroed in _osd_req_op_init() */
404 memset(req, 0, sizeof(*req));
405
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700406 req->r_osdc = osdc;
407 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500408 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200409 req->r_snapid = CEPH_NOSNAP;
410 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700411
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700412 kref_init(&req->r_kref);
413 init_completion(&req->r_completion);
414 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600415 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700416 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700417 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400418 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700419 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700420 INIT_LIST_HEAD(&req->r_osd_item);
421
Ilya Dryomova66dd382016-04-28 16:07:23 +0200422 target_init(&req->r_t);
Ilya Dryomov221165252014-01-27 17:40:18 +0200423
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200424 dout("%s req %p\n", __func__, req);
425 return req;
426}
427EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100428
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200429int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
430{
431 struct ceph_osd_client *osdc = req->r_osdc;
432 struct ceph_msg *msg;
433 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700434
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200435 WARN_ON(ceph_oid_empty(&req->r_base_oid));
436
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200437 /* create request message */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100438 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
439 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
440 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
441 msg_size += 1 + 8 + 4 + 4; /* pgid */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200442 msg_size += 4 + req->r_base_oid.name_len; /* oid */
443 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100444 msg_size += 8; /* snapid */
445 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200446 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100447 msg_size += 4; /* retry_attempt */
448
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200449 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700450 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
451 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200452 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
453 if (!msg)
454 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700455
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700456 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700457 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700458
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200459 /* create reply message */
460 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200461 msg_size += req->r_base_oid.name_len;
462 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200463
464 if (req->r_mempool)
465 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
466 else
467 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
468 if (!msg)
469 return -ENOMEM;
470
471 req->r_reply = msg;
472
473 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700474}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200475EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700476
Alex Eldera8dd0a32013-03-13 20:50:00 -0500477static bool osd_req_opcode_valid(u16 opcode)
478{
479 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400480#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
481__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
482#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500483 default:
484 return false;
485 }
486}
487
Alex Elder33803f32013-03-13 20:50:00 -0500488/*
489 * This is an osd op init function for opcodes that have no data or
490 * other information associated with them. It also serves as a
491 * common init routine for all the other init functions, below.
492 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500493static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600494_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800495 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500496{
Alex Elderc99d2d42013-04-05 01:27:11 -0500497 struct ceph_osd_req_op *op;
498
499 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500500 BUG_ON(!osd_req_opcode_valid(opcode));
501
Alex Elderc99d2d42013-04-05 01:27:11 -0500502 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500503 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500504 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800505 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500506
507 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500508}
509
Alex Elder49719772013-02-11 12:33:24 -0600510void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800511 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600512{
Yan, Zheng144cba12015-04-27 11:09:54 +0800513 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600514}
515EXPORT_SYMBOL(osd_req_op_init);
516
Alex Elderc99d2d42013-04-05 01:27:11 -0500517void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
518 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500519 u64 offset, u64 length,
520 u64 truncate_size, u32 truncate_seq)
521{
Yan, Zheng144cba12015-04-27 11:09:54 +0800522 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
523 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500524 size_t payload_len = 0;
525
Li Wangad7a60d2013-08-15 11:51:44 +0800526 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200527 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
528 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500529
Alex Elder33803f32013-03-13 20:50:00 -0500530 op->extent.offset = offset;
531 op->extent.length = length;
532 op->extent.truncate_size = truncate_size;
533 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200534 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500535 payload_len += length;
536
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100537 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500538}
539EXPORT_SYMBOL(osd_req_op_extent_init);
540
Alex Elderc99d2d42013-04-05 01:27:11 -0500541void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
542 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500543{
Alex Elderc99d2d42013-04-05 01:27:11 -0500544 struct ceph_osd_req_op *op;
545 u64 previous;
546
547 BUG_ON(which >= osd_req->r_num_ops);
548 op = &osd_req->r_ops[which];
549 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500550
551 if (length == previous)
552 return; /* Nothing to do */
553 BUG_ON(length > previous);
554
555 op->extent.length = length;
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100556 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500557}
558EXPORT_SYMBOL(osd_req_op_extent_update);
559
Yan, Zheng2c63f492016-01-07 17:32:54 +0800560void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
561 unsigned int which, u64 offset_inc)
562{
563 struct ceph_osd_req_op *op, *prev_op;
564
565 BUG_ON(which + 1 >= osd_req->r_num_ops);
566
567 prev_op = &osd_req->r_ops[which];
568 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
569 /* dup previous one */
570 op->indata_len = prev_op->indata_len;
571 op->outdata_len = prev_op->outdata_len;
572 op->extent = prev_op->extent;
573 /* adjust offset */
574 op->extent.offset += offset_inc;
575 op->extent.length -= offset_inc;
576
577 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
578 op->indata_len -= offset_inc;
579}
580EXPORT_SYMBOL(osd_req_op_extent_dup_last);
581
Alex Elderc99d2d42013-04-05 01:27:11 -0500582void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500583 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500584{
Yan, Zheng144cba12015-04-27 11:09:54 +0800585 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
586 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500587 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500588 size_t payload_len = 0;
589 size_t size;
590
591 BUG_ON(opcode != CEPH_OSD_OP_CALL);
592
Alex Elder5f562df2013-04-05 01:27:12 -0500593 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
594 BUG_ON(!pagelist);
595 ceph_pagelist_init(pagelist);
596
Alex Elder33803f32013-03-13 20:50:00 -0500597 op->cls.class_name = class;
598 size = strlen(class);
599 BUG_ON(size > (size_t) U8_MAX);
600 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500601 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500602 payload_len += size;
603
604 op->cls.method_name = method;
605 size = strlen(method);
606 BUG_ON(size > (size_t) U8_MAX);
607 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500608 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500609 payload_len += size;
610
Alex Eldera4ce40a2013-04-05 01:27:12 -0500611 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500612
Alex Elder33803f32013-03-13 20:50:00 -0500613 op->cls.argc = 0; /* currently unused */
614
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100615 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500616}
617EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500618
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800619int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
620 u16 opcode, const char *name, const void *value,
621 size_t size, u8 cmp_op, u8 cmp_mode)
622{
Yan, Zheng144cba12015-04-27 11:09:54 +0800623 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
624 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800625 struct ceph_pagelist *pagelist;
626 size_t payload_len;
627
628 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
629
630 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
631 if (!pagelist)
632 return -ENOMEM;
633
634 ceph_pagelist_init(pagelist);
635
636 payload_len = strlen(name);
637 op->xattr.name_len = payload_len;
638 ceph_pagelist_append(pagelist, name, payload_len);
639
640 op->xattr.value_len = size;
641 ceph_pagelist_append(pagelist, value, size);
642 payload_len += size;
643
644 op->xattr.cmp_op = cmp_op;
645 op->xattr.cmp_mode = cmp_mode;
646
647 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100648 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800649 return 0;
650}
651EXPORT_SYMBOL(osd_req_op_xattr_init);
652
Alex Elderc99d2d42013-04-05 01:27:11 -0500653void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
654 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500655 u64 cookie, u64 version, int flag)
656{
Yan, Zheng144cba12015-04-27 11:09:54 +0800657 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
658 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500659
Alex Elderc99d2d42013-04-05 01:27:11 -0500660 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500661
662 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500663 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500664 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500665 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500666}
667EXPORT_SYMBOL(osd_req_op_watch_init);
668
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200669void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
670 unsigned int which,
671 u64 expected_object_size,
672 u64 expected_write_size)
673{
674 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800675 CEPH_OSD_OP_SETALLOCHINT,
676 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200677
678 op->alloc_hint.expected_object_size = expected_object_size;
679 op->alloc_hint.expected_write_size = expected_write_size;
680
681 /*
682 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
683 * not worth a feature bit. Set FAILOK per-op flag to make
684 * sure older osds don't trip over an unsupported opcode.
685 */
686 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
687}
688EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
689
Alex Elder90af3602013-04-05 14:46:01 -0500690static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500691 struct ceph_osd_data *osd_data)
692{
693 u64 length = ceph_osd_data_length(osd_data);
694
695 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
696 BUG_ON(length > (u64) SIZE_MAX);
697 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500698 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500699 length, osd_data->alignment);
700 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
701 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500702 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500703#ifdef CONFIG_BLOCK
704 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500705 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500706#endif
707 } else {
708 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
709 }
710}
711
Alex Elder175face2013-03-08 13:35:36 -0600712static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500713 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700714{
Alex Elder79528732013-04-03 21:32:51 -0500715 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500716 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500717 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500718 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600719
Alex Elder79528732013-04-03 21:32:51 -0500720 BUG_ON(which >= req->r_num_ops);
721 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500722 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
723 pr_err("unrecognized osd opcode %d\n", src->op);
724
725 return 0;
726 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700727
Alex Elder065a68f2012-04-20 15:49:43 -0500728 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600729 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600730 osd_data = &src->raw_data_in;
731 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600732 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700733 case CEPH_OSD_OP_READ:
734 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200735 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800736 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800737 case CEPH_OSD_OP_TRUNCATE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200738 if (src->op == CEPH_OSD_OP_WRITE ||
739 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder54d50642013-04-03 01:28:58 -0500740 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600741 dst->extent.offset = cpu_to_le64(src->extent.offset);
742 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700743 dst->extent.truncate_size =
744 cpu_to_le64(src->extent.truncate_size);
745 dst->extent.truncate_seq =
746 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500747 osd_data = &src->extent.osd_data;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200748 if (src->op == CEPH_OSD_OP_WRITE ||
749 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder04017e22013-04-05 14:46:02 -0500750 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500751 else
Alex Elder04017e22013-04-05 14:46:02 -0500752 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700753 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700754 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700755 dst->cls.class_len = src->cls.class_len;
756 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500757 osd_data = &src->cls.request_info;
758 ceph_osdc_msg_data_add(req->r_request, osd_data);
759 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
760 request_data_len = osd_data->pagelist->length;
761
762 osd_data = &src->cls.request_data;
763 data_length = ceph_osd_data_length(osd_data);
764 if (data_length) {
765 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
766 dst->cls.indata_len = cpu_to_le32(data_length);
767 ceph_osdc_msg_data_add(req->r_request, osd_data);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100768 src->indata_len += data_length;
Alex Elder04017e22013-04-05 14:46:02 -0500769 request_data_len += data_length;
770 }
771 osd_data = &src->cls.response_data;
772 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700773 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700774 case CEPH_OSD_OP_STARTSYNC:
775 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700776 case CEPH_OSD_OP_NOTIFY_ACK:
777 case CEPH_OSD_OP_WATCH:
778 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
779 dst->watch.ver = cpu_to_le64(src->watch.ver);
780 dst->watch.flag = src->watch.flag;
781 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200782 case CEPH_OSD_OP_SETALLOCHINT:
783 dst->alloc_hint.expected_object_size =
784 cpu_to_le64(src->alloc_hint.expected_object_size);
785 dst->alloc_hint.expected_write_size =
786 cpu_to_le64(src->alloc_hint.expected_write_size);
787 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800788 case CEPH_OSD_OP_SETXATTR:
789 case CEPH_OSD_OP_CMPXATTR:
790 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
791 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
792 dst->xattr.cmp_op = src->xattr.cmp_op;
793 dst->xattr.cmp_mode = src->xattr.cmp_mode;
794 osd_data = &src->xattr.osd_data;
795 ceph_osdc_msg_data_add(req->r_request, osd_data);
796 request_data_len = osd_data->pagelist->length;
797 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800798 case CEPH_OSD_OP_CREATE:
799 case CEPH_OSD_OP_DELETE:
800 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700801 default:
Alex Elder4c464592013-02-15 11:42:30 -0600802 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600803 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600804 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500805
806 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700807 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200808
Alex Eldera8dd0a32013-03-13 20:50:00 -0500809 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200810 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100811 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600812
Alex Elder54d50642013-04-03 01:28:58 -0500813 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700814}
815
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700816/*
Sage Weilf24e9982009-10-06 11:31:10 -0700817 * build new request AND message, calculate layout, and adjust file
818 * extent as needed.
819 *
820 * if the file was recently truncated, we include information about its
821 * old and new size so that the object can be updated appropriately. (we
822 * avoid synchronously deleting truncated objects because it's slow.)
823 *
824 * if @do_sync, include a 'startsync' command so that the osd will flush
825 * data quickly.
826 */
827struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
828 struct ceph_file_layout *layout,
829 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800830 u64 off, u64 *plen,
831 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700832 int opcode, int flags,
833 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700834 u32 truncate_seq,
835 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600836 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700837{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700838 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500839 u64 objnum = 0;
840 u64 objoff = 0;
841 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700842 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700843
Li Wangad7a60d2013-08-15 11:51:44 +0800844 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800845 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
846 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700847
Alex Elderacead002013-03-14 14:09:05 -0500848 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600849 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200850 if (!req) {
851 r = -ENOMEM;
852 goto fail;
853 }
Alex Elder79528732013-04-03 21:32:51 -0500854
Alex Elderd178a9e2012-11-13 21:11:15 -0600855 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700856
857 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500858 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200859 if (r)
860 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -0500861
Yan, Zheng864e9192014-11-13 10:47:25 +0800862 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800863 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800864 } else {
865 u32 object_size = le32_to_cpu(layout->fl_object_size);
866 u32 object_base = off - objoff;
867 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
868 if (truncate_size <= object_base) {
869 truncate_size = 0;
870 } else {
871 truncate_size -= object_base;
872 if (truncate_size > object_size)
873 truncate_size = object_size;
874 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800875 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800876 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800877 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500878 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500879
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200880 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200881 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600882
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200883 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
884 if (r)
885 goto fail;
886
Sage Weilf24e9982009-10-06 11:31:10 -0700887 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200888
889fail:
890 ceph_osdc_put_request(req);
891 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700892}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700893EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700894
895/*
896 * We keep osd requests in an rbtree, sorted by ->r_tid.
897 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200898DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Sage Weilf24e9982009-10-06 11:31:10 -0700899
900static struct ceph_osd_request *
901__lookup_request_ge(struct ceph_osd_client *osdc,
902 u64 tid)
903{
904 struct ceph_osd_request *req;
905 struct rb_node *n = osdc->requests.rb_node;
906
907 while (n) {
908 req = rb_entry(n, struct ceph_osd_request, r_node);
909 if (tid < req->r_tid) {
910 if (!n->rb_left)
911 return req;
912 n = n->rb_left;
913 } else if (tid > req->r_tid) {
914 n = n->rb_right;
915 } else {
916 return req;
917 }
918 }
919 return NULL;
920}
921
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400922static void __kick_linger_request(struct ceph_osd_request *req)
923{
924 struct ceph_osd_client *osdc = req->r_osdc;
925 struct ceph_osd *osd = req->r_osd;
926
927 /*
928 * Linger requests need to be resent with a new tid to avoid
929 * the dup op detection logic on the OSDs. Achieve this with
930 * a re-register dance instead of open-coding.
931 */
932 ceph_osdc_get_request(req);
933 if (!list_empty(&req->r_linger_item))
934 __unregister_linger_request(osdc, req);
935 else
936 __unregister_request(osdc, req);
937 __register_request(osdc, req);
938 ceph_osdc_put_request(req);
939
940 /*
941 * Unless request has been registered as both normal and
942 * lingering, __unregister{,_linger}_request clears r_osd.
943 * However, here we need to preserve r_osd to make sure we
944 * requeue on the same OSD.
945 */
946 WARN_ON(req->r_osd || !osd);
947 req->r_osd = osd;
948
949 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
950 __enqueue_request(req);
951}
952
Sage Weil6f6c7002011-01-17 20:34:08 -0800953/*
954 * Resubmit requests pending on the given osd.
955 */
956static void __kick_osd_requests(struct ceph_osd_client *osdc,
957 struct ceph_osd *osd)
958{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700959 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500960 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400961 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800962 int err;
963
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400964 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800965 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600966 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800967 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400968
Alex Eldere02493c2013-03-25 18:16:11 -0500969 /*
970 * Build up a list of requests to resend by traversing the
971 * osd's list of requests. Requests for a given object are
972 * sent in tid order, and that is also the order they're
973 * kept on this list. Therefore all requests that are in
974 * flight will be found first, followed by all requests that
975 * have not yet been sent. And to resend requests while
976 * preserving this order we will want to put any sent
977 * requests back on the front of the osd client's unsent
978 * list.
979 *
980 * So we build a separate ordered list of already-sent
981 * requests for the affected osd and splice it onto the
982 * front of the osd client's unsent list. Once we've seen a
983 * request that has not yet been sent we're done. Those
984 * requests are already sitting right where they belong.
985 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800986 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500987 if (!req->r_sent)
988 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400989
990 if (!req->r_linger) {
991 dout("%s requeueing %p tid %llu\n", __func__, req,
992 req->r_tid);
993 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700994 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400995 } else {
996 list_move_tail(&req->r_req_lru_item, &resend_linger);
997 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700998 }
Alex Eldere02493c2013-03-25 18:16:11 -0500999 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001000
Alex Eldere02493c2013-03-25 18:16:11 -05001001 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001002 * Both registered and not yet registered linger requests are
1003 * enqueued with a new tid on the same OSD. We add/move them
1004 * to req_unsent/o_requests at the end to keep things in tid
1005 * order.
Alex Eldere02493c2013-03-25 18:16:11 -05001006 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001007 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001008 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001009 WARN_ON(!list_empty(&req->r_req_lru_item));
1010 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001011 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001012
1013 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
1014 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001015}
1016
Sage Weilf24e9982009-10-06 11:31:10 -07001017/*
Sage Weil81b024e2009-10-09 10:29:18 -07001018 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -07001019 */
1020static void osd_reset(struct ceph_connection *con)
1021{
1022 struct ceph_osd *osd = con->private;
1023 struct ceph_osd_client *osdc;
1024
1025 if (!osd)
1026 return;
1027 dout("osd_reset osd%d\n", osd->o_osd);
1028 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001029 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001030 mutex_lock(&osdc->request_mutex);
1031 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001032 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001033 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001034 up_read(&osdc->map_sem);
1035}
1036
1037/*
1038 * Track open sessions with osds.
1039 */
Alex Eldere10006f2012-05-26 23:26:43 -05001040static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001041{
1042 struct ceph_osd *osd;
1043
1044 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1045 if (!osd)
1046 return NULL;
1047
1048 atomic_set(&osd->o_ref, 1);
1049 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001050 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001051 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001052 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001053 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001054 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001055 osd->o_incarnation = 1;
1056
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001057 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001058
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001059 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001060 return osd;
1061}
1062
1063static struct ceph_osd *get_osd(struct ceph_osd *osd)
1064{
1065 if (atomic_inc_not_zero(&osd->o_ref)) {
1066 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1067 atomic_read(&osd->o_ref));
1068 return osd;
1069 } else {
1070 dout("get_osd %p FAIL\n", osd);
1071 return NULL;
1072 }
1073}
1074
1075static void put_osd(struct ceph_osd *osd)
1076{
1077 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1078 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001079 if (atomic_dec_and_test(&osd->o_ref)) {
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001080 if (osd->o_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02001081 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001082 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001083 }
Sage Weilf24e9982009-10-06 11:31:10 -07001084}
1085
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001086DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1087
Sage Weilf24e9982009-10-06 11:31:10 -07001088/*
1089 * remove an osd from our map
1090 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001091static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001092{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001093 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001094 WARN_ON(!list_empty(&osd->o_requests));
1095 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001096
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001097 list_del_init(&osd->o_osd_lru);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001098 erase_osd(&osdc->osds, osd);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001099}
1100
1101static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1102{
1103 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1104
1105 if (!RB_EMPTY_NODE(&osd->o_node)) {
1106 ceph_con_close(&osd->o_con);
1107 __remove_osd(osdc, osd);
1108 put_osd(osd);
1109 }
Sage Weilf24e9982009-10-06 11:31:10 -07001110}
1111
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001112static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1113 struct ceph_osd *osd)
1114{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001115 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001116 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001117
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001118 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001119 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001120}
1121
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001122static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1123 struct ceph_osd *osd)
1124{
1125 dout("%s %p\n", __func__, osd);
1126
1127 if (list_empty(&osd->o_requests) &&
1128 list_empty(&osd->o_linger_requests))
1129 __move_osd_to_lru(osdc, osd);
1130}
1131
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001132static void __remove_osd_from_lru(struct ceph_osd *osd)
1133{
1134 dout("__remove_osd_from_lru %p\n", osd);
1135 if (!list_empty(&osd->o_osd_lru))
1136 list_del_init(&osd->o_osd_lru);
1137}
1138
Sage Weilf24e9982009-10-06 11:31:10 -07001139/*
1140 * reset osd connect
1141 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001142static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001143{
Alex Elderc3acb182012-12-07 09:57:58 -06001144 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001145
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001146 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001147 if (list_empty(&osd->o_requests) &&
1148 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001149 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001150 return -ENODEV;
1151 }
1152
1153 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1154 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1155 !ceph_con_opened(&osd->o_con)) {
1156 struct ceph_osd_request *req;
1157
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001158 dout("osd addr hasn't changed and connection never opened, "
1159 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001160 /* touch each r_stamp for handle_timeout()'s benfit */
1161 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1162 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001163
1164 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001165 }
Alex Elderc3acb182012-12-07 09:57:58 -06001166
1167 ceph_con_close(&osd->o_con);
1168 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1169 osd->o_incarnation++;
1170
1171 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001172}
1173
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001174static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1175{
1176 schedule_delayed_work(&osdc->timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03001177 osdc->client->options->osd_keepalive_timeout);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001178}
1179
1180static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1181{
1182 cancel_delayed_work(&osdc->timeout_work);
1183}
Sage Weilf24e9982009-10-06 11:31:10 -07001184
1185/*
1186 * Register request, assign tid. If this is the first request, set up
1187 * the timeout event.
1188 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001189static void __register_request(struct ceph_osd_client *osdc,
1190 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001191{
Sage Weilf24e9982009-10-06 11:31:10 -07001192 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001193 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001194 dout("__register_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001195 insert_request(&osdc->requests, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001196 ceph_osdc_get_request(req);
1197 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001198 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001199 dout(" first request, scheduling timeout\n");
1200 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001201 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001202}
1203
Sage Weilf24e9982009-10-06 11:31:10 -07001204/*
1205 * called under osdc->request_mutex
1206 */
1207static void __unregister_request(struct ceph_osd_client *osdc,
1208 struct ceph_osd_request *req)
1209{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001210 if (RB_EMPTY_NODE(&req->r_node)) {
1211 dout("__unregister_request %p tid %lld not registered\n",
1212 req, req->r_tid);
1213 return;
1214 }
1215
Sage Weilf24e9982009-10-06 11:31:10 -07001216 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001217 erase_request(&osdc->requests, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001218 osdc->num_requests--;
1219
Sage Weil0ba64782009-10-08 16:57:16 -07001220 if (req->r_osd) {
1221 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001222 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001223
1224 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001225 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001226 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001227 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001228 }
Sage Weilf24e9982009-10-06 11:31:10 -07001229
Alex Elder7d5f2482012-11-29 08:37:03 -06001230 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001231 ceph_osdc_put_request(req);
1232
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001233 if (osdc->num_requests == 0) {
1234 dout(" no requests, canceling timeout\n");
1235 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001236 }
1237}
1238
1239/*
1240 * Cancel a previously queued request message
1241 */
1242static void __cancel_request(struct ceph_osd_request *req)
1243{
Sage Weil6bc18872010-09-27 10:18:52 -07001244 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001245 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001246 req->r_sent = 0;
1247 }
1248}
1249
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001250static void __register_linger_request(struct ceph_osd_client *osdc,
1251 struct ceph_osd_request *req)
1252{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001253 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1254 WARN_ON(!req->r_linger);
1255
Alex Elder96e4dac2013-05-22 20:54:25 -05001256 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001257 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001258 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001259 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001260 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001261}
1262
1263static void __unregister_linger_request(struct ceph_osd_client *osdc,
1264 struct ceph_osd_request *req)
1265{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001266 WARN_ON(!req->r_linger);
1267
1268 if (list_empty(&req->r_linger_item)) {
1269 dout("%s %p tid %llu not registered\n", __func__, req,
1270 req->r_tid);
1271 return;
1272 }
1273
1274 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001275 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001276
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001277 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001278 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001279 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001280 if (list_empty(&req->r_osd_item))
1281 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001282 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001283 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001284}
1285
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001286void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1287 struct ceph_osd_request *req)
1288{
1289 if (!req->r_linger) {
1290 dout("set_request_linger %p\n", req);
1291 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001292 }
1293}
1294EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1295
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001296static bool __pool_full(struct ceph_pg_pool_info *pi)
1297{
1298 return pi->flags & CEPH_POOL_FLAG_FULL;
1299}
1300
Sage Weilf24e9982009-10-06 11:31:10 -07001301/*
Josh Durgind29adb32013-12-02 19:11:48 -08001302 * Returns whether a request should be blocked from being sent
1303 * based on the current osdmap and osd_client settings.
1304 *
1305 * Caller should hold map_sem for read.
1306 */
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001307static bool target_should_be_paused(struct ceph_osd_client *osdc,
1308 const struct ceph_osd_request_target *t,
1309 struct ceph_pg_pool_info *pi)
1310{
1311 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1312 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1313 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
1314 __pool_full(pi);
1315
1316 WARN_ON(pi->id != t->base_oloc.pool);
1317 return (t->flags & CEPH_OSD_FLAG_READ && pauserd) ||
1318 (t->flags & CEPH_OSD_FLAG_WRITE && pausewr);
1319}
1320
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001321enum calc_target_result {
1322 CALC_TARGET_NO_ACTION = 0,
1323 CALC_TARGET_NEED_RESEND,
1324 CALC_TARGET_POOL_DNE,
1325};
1326
1327static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1328 struct ceph_osd_request_target *t,
1329 u32 *last_force_resend,
1330 bool any_change)
1331{
1332 struct ceph_pg_pool_info *pi;
1333 struct ceph_pg pgid, last_pgid;
1334 struct ceph_osds up, acting;
1335 bool force_resend = false;
1336 bool need_check_tiering = false;
1337 bool need_resend = false;
1338 bool sort_bitwise = ceph_osdmap_flag(osdc->osdmap,
1339 CEPH_OSDMAP_SORTBITWISE);
1340 enum calc_target_result ct_res;
1341 int ret;
1342
1343 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1344 if (!pi) {
1345 t->osd = CEPH_HOMELESS_OSD;
1346 ct_res = CALC_TARGET_POOL_DNE;
1347 goto out;
1348 }
1349
1350 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1351 if (last_force_resend &&
1352 *last_force_resend < pi->last_force_request_resend) {
1353 *last_force_resend = pi->last_force_request_resend;
1354 force_resend = true;
1355 } else if (!last_force_resend) {
1356 force_resend = true;
1357 }
1358 }
1359 if (ceph_oid_empty(&t->target_oid) || force_resend) {
1360 ceph_oid_copy(&t->target_oid, &t->base_oid);
1361 need_check_tiering = true;
1362 }
1363 if (ceph_oloc_empty(&t->target_oloc) || force_resend) {
1364 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1365 need_check_tiering = true;
1366 }
1367
1368 if (need_check_tiering &&
1369 (t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1370 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1371 t->target_oloc.pool = pi->read_tier;
1372 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1373 t->target_oloc.pool = pi->write_tier;
1374 }
1375
1376 ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1377 &t->target_oloc, &pgid);
1378 if (ret) {
1379 WARN_ON(ret != -ENOENT);
1380 t->osd = CEPH_HOMELESS_OSD;
1381 ct_res = CALC_TARGET_POOL_DNE;
1382 goto out;
1383 }
1384 last_pgid.pool = pgid.pool;
1385 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1386
1387 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1388 if (any_change &&
1389 ceph_is_new_interval(&t->acting,
1390 &acting,
1391 &t->up,
1392 &up,
1393 t->size,
1394 pi->size,
1395 t->min_size,
1396 pi->min_size,
1397 t->pg_num,
1398 pi->pg_num,
1399 t->sort_bitwise,
1400 sort_bitwise,
1401 &last_pgid))
1402 force_resend = true;
1403
1404 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1405 t->paused = false;
1406 need_resend = true;
1407 }
1408
1409 if (ceph_pg_compare(&t->pgid, &pgid) ||
1410 ceph_osds_changed(&t->acting, &acting, any_change) ||
1411 force_resend) {
1412 t->pgid = pgid; /* struct */
1413 ceph_osds_copy(&t->acting, &acting);
1414 ceph_osds_copy(&t->up, &up);
1415 t->size = pi->size;
1416 t->min_size = pi->min_size;
1417 t->pg_num = pi->pg_num;
1418 t->pg_num_mask = pi->pg_num_mask;
1419 t->sort_bitwise = sort_bitwise;
1420
1421 t->osd = acting.primary;
1422 need_resend = true;
1423 }
1424
1425 ct_res = need_resend ? CALC_TARGET_NEED_RESEND : CALC_TARGET_NO_ACTION;
1426out:
1427 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1428 return ct_res;
1429}
1430
Ilya Dryomovf671b582014-09-02 13:40:33 +04001431static void __enqueue_request(struct ceph_osd_request *req)
1432{
1433 struct ceph_osd_client *osdc = req->r_osdc;
1434
1435 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1436 req->r_osd ? req->r_osd->o_osd : -1);
1437
1438 if (req->r_osd) {
1439 __remove_osd_from_lru(req->r_osd);
1440 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1441 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1442 } else {
1443 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1444 }
1445}
1446
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001447/*
Sage Weilf24e9982009-10-06 11:31:10 -07001448 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1449 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001450 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001451 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001452 *
1453 * Return 0 if unchanged, 1 if changed, or negative on error.
1454 *
1455 * Caller should hold map_sem for read and request_mutex.
1456 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001457static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001458 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001459{
Ilya Dryomova66dd382016-04-28 16:07:23 +02001460 enum calc_target_result ct_res;
Sage Weilf24e9982009-10-06 11:31:10 -07001461 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001462
Sage Weil6f6c7002011-01-17 20:34:08 -08001463 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001464
Ilya Dryomova66dd382016-04-28 16:07:23 +02001465 ct_res = calc_target(osdc, &req->r_t, NULL, force_resend);
1466 switch (ct_res) {
1467 case CALC_TARGET_POOL_DNE:
Sage Weil6f6c7002011-01-17 20:34:08 -08001468 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Ilya Dryomova66dd382016-04-28 16:07:23 +02001469 return -EIO;
1470 case CALC_TARGET_NO_ACTION:
Sage Weilf24e9982009-10-06 11:31:10 -07001471 return 0; /* no change */
Ilya Dryomova66dd382016-04-28 16:07:23 +02001472 default:
1473 BUG_ON(ct_res != CALC_TARGET_NEED_RESEND);
1474 }
Sage Weilf24e9982009-10-06 11:31:10 -07001475
Sage Weil5b191d92013-02-23 10:38:16 -08001476 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
Ilya Dryomova66dd382016-04-28 16:07:23 +02001477 req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed, req->r_t.osd,
Sage Weilf24e9982009-10-06 11:31:10 -07001478 req->r_osd ? req->r_osd->o_osd : -1);
1479
1480 if (req->r_osd) {
1481 __cancel_request(req);
1482 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001483 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001484 req->r_osd = NULL;
1485 }
1486
Ilya Dryomova66dd382016-04-28 16:07:23 +02001487 req->r_osd = lookup_osd(&osdc->osds, req->r_t.osd);
1488 if (!req->r_osd && req->r_t.osd >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001489 err = -ENOMEM;
Ilya Dryomova66dd382016-04-28 16:07:23 +02001490 req->r_osd = create_osd(osdc, req->r_t.osd);
Sage Weil6f6c7002011-01-17 20:34:08 -08001491 if (!req->r_osd) {
1492 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001493 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001494 }
Sage Weilf24e9982009-10-06 11:31:10 -07001495
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001496 dout("map_request osd %p is osd%d\n", req->r_osd,
Ilya Dryomova66dd382016-04-28 16:07:23 +02001497 req->r_osd->o_osd);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001498 insert_osd(&osdc->osds, req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001499
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001500 ceph_con_open(&req->r_osd->o_con,
Ilya Dryomova66dd382016-04-28 16:07:23 +02001501 CEPH_ENTITY_TYPE_OSD, req->r_osd->o_osd,
1502 &osdc->osdmap->osd_addr[req->r_osd->o_osd]);
Sage Weilf24e9982009-10-06 11:31:10 -07001503 }
1504
Ilya Dryomovf671b582014-09-02 13:40:33 +04001505 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001506 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001507
1508out:
Sage Weilf24e9982009-10-06 11:31:10 -07001509 return err;
1510}
1511
1512/*
1513 * caller should hold map_sem (for read) and request_mutex
1514 */
Sage Weil56e925b2012-01-03 12:34:34 -08001515static void __send_request(struct ceph_osd_client *osdc,
1516 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001517{
Sage Weil1b83bef2013-02-25 16:11:12 -08001518 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001519
Sage Weil1b83bef2013-02-25 16:11:12 -08001520 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1521 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
Ilya Dryomova66dd382016-04-28 16:07:23 +02001522 req->r_t.pgid.pool, req->r_t.pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001523
Sage Weil1b83bef2013-02-25 16:11:12 -08001524 /* fill in message content that changes each time we send it */
1525 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1526 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomova66dd382016-04-28 16:07:23 +02001527 put_unaligned_le64(req->r_t.target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001528 p = req->r_request_pgid;
Ilya Dryomova66dd382016-04-28 16:07:23 +02001529 ceph_encode_64(&p, req->r_t.pgid.pool);
1530 ceph_encode_32(&p, req->r_t.pgid.seed);
Sage Weil1b83bef2013-02-25 16:11:12 -08001531 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1532 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1533 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001534
Sage Weil3dd72fc2010-03-22 14:42:30 -07001535 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001536 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001537
1538 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001539
Sage Weilf24e9982009-10-06 11:31:10 -07001540 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001541
1542 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001543}
1544
1545/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001546 * Send any requests in the queue (req_unsent).
1547 */
Alex Elderf9d25192013-02-15 11:42:29 -06001548static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001549{
1550 struct ceph_osd_request *req, *tmp;
1551
Alex Elderf9d25192013-02-15 11:42:29 -06001552 dout("__send_queued\n");
1553 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001554 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001555}
1556
1557/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001558 * Caller should hold map_sem for read and request_mutex.
1559 */
1560static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1561 struct ceph_osd_request *req,
1562 bool nofail)
1563{
1564 int rc;
1565
1566 __register_request(osdc, req);
1567 req->r_sent = 0;
1568 req->r_got_reply = 0;
1569 rc = __map_request(osdc, req, 0);
1570 if (rc < 0) {
1571 if (nofail) {
1572 dout("osdc_start_request failed map, "
1573 " will retry %lld\n", req->r_tid);
1574 rc = 0;
1575 } else {
1576 __unregister_request(osdc, req);
1577 }
1578 return rc;
1579 }
1580
1581 if (req->r_osd == NULL) {
1582 dout("send_request %p no up osds in pg\n", req);
1583 ceph_monc_request_next_osdmap(&osdc->client->monc);
1584 } else {
1585 __send_queued(osdc);
1586 }
1587
1588 return 0;
1589}
1590
1591/*
Sage Weilf24e9982009-10-06 11:31:10 -07001592 * Timeout callback, called every N seconds when 1 or more osd
1593 * requests has been active for more than N seconds. When this
1594 * happens, we ping all OSDs with requests who have timed out to
1595 * ensure any communications channel reset is detected. Reset the
1596 * request timeouts another N seconds in the future as we go.
1597 * Reschedule the timeout event another N seconds in future (unless
1598 * there are no open requests).
1599 */
1600static void handle_timeout(struct work_struct *work)
1601{
1602 struct ceph_osd_client *osdc =
1603 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001604 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001605 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001606 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001607 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001608 dout("timeout\n");
1609 down_read(&osdc->map_sem);
1610
1611 ceph_monc_request_next_osdmap(&osdc->client->monc);
1612
1613 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001614
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001615 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001616 * ping osds that are a bit slow. this ensures that if there
1617 * is a break in the TCP connection we will notice, and reopen
1618 * a connection with that osd (from the fault callback).
1619 */
1620 INIT_LIST_HEAD(&slow_osds);
1621 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001622 if (time_before(jiffies,
1623 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001624 break;
1625
1626 osd = req->r_osd;
1627 BUG_ON(!osd);
1628 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001629 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001630 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1631 }
1632 while (!list_empty(&slow_osds)) {
1633 osd = list_entry(slow_osds.next, struct ceph_osd,
1634 o_keepalive_item);
1635 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001636 ceph_con_keepalive(&osd->o_con);
1637 }
1638
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001639 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001640 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001641 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001642 up_read(&osdc->map_sem);
1643}
1644
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001645static void handle_osds_timeout(struct work_struct *work)
1646{
1647 struct ceph_osd_client *osdc =
1648 container_of(work, struct ceph_osd_client,
1649 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001650 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001651 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001652
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001653 dout("%s osdc %p\n", __func__, osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001654 down_read(&osdc->map_sem);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001655 mutex_lock(&osdc->request_mutex);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001656
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001657 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1658 if (time_before(jiffies, osd->lru_ttl))
1659 break;
1660
1661 remove_osd(osdc, osd);
1662 }
1663
1664 mutex_unlock(&osdc->request_mutex);
1665 up_read(&osdc->map_sem);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001666 schedule_delayed_work(&osdc->osds_timeout_work,
1667 round_jiffies_relative(delay));
1668}
1669
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001670static int ceph_oloc_decode(void **p, void *end,
1671 struct ceph_object_locator *oloc)
1672{
1673 u8 struct_v, struct_cv;
1674 u32 len;
1675 void *struct_end;
1676 int ret = 0;
1677
1678 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1679 struct_v = ceph_decode_8(p);
1680 struct_cv = ceph_decode_8(p);
1681 if (struct_v < 3) {
1682 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1683 struct_v, struct_cv);
1684 goto e_inval;
1685 }
1686 if (struct_cv > 6) {
1687 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1688 struct_v, struct_cv);
1689 goto e_inval;
1690 }
1691 len = ceph_decode_32(p);
1692 ceph_decode_need(p, end, len, e_inval);
1693 struct_end = *p + len;
1694
1695 oloc->pool = ceph_decode_64(p);
1696 *p += 4; /* skip preferred */
1697
1698 len = ceph_decode_32(p);
1699 if (len > 0) {
1700 pr_warn("ceph_object_locator::key is set\n");
1701 goto e_inval;
1702 }
1703
1704 if (struct_v >= 5) {
1705 len = ceph_decode_32(p);
1706 if (len > 0) {
1707 pr_warn("ceph_object_locator::nspace is set\n");
1708 goto e_inval;
1709 }
1710 }
1711
1712 if (struct_v >= 6) {
1713 s64 hash = ceph_decode_64(p);
1714 if (hash != -1) {
1715 pr_warn("ceph_object_locator::hash is set\n");
1716 goto e_inval;
1717 }
1718 }
1719
1720 /* skip the rest */
1721 *p = struct_end;
1722out:
1723 return ret;
1724
1725e_inval:
1726 ret = -EINVAL;
1727 goto out;
1728}
1729
1730static int ceph_redirect_decode(void **p, void *end,
1731 struct ceph_request_redirect *redir)
1732{
1733 u8 struct_v, struct_cv;
1734 u32 len;
1735 void *struct_end;
1736 int ret;
1737
1738 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1739 struct_v = ceph_decode_8(p);
1740 struct_cv = ceph_decode_8(p);
1741 if (struct_cv > 1) {
1742 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1743 struct_v, struct_cv);
1744 goto e_inval;
1745 }
1746 len = ceph_decode_32(p);
1747 ceph_decode_need(p, end, len, e_inval);
1748 struct_end = *p + len;
1749
1750 ret = ceph_oloc_decode(p, end, &redir->oloc);
1751 if (ret)
1752 goto out;
1753
1754 len = ceph_decode_32(p);
1755 if (len > 0) {
1756 pr_warn("ceph_request_redirect::object_name is set\n");
1757 goto e_inval;
1758 }
1759
1760 len = ceph_decode_32(p);
1761 *p += len; /* skip osd_instructions */
1762
1763 /* skip the rest */
1764 *p = struct_end;
1765out:
1766 return ret;
1767
1768e_inval:
1769 ret = -EINVAL;
1770 goto out;
1771}
1772
Sage Weil25845472011-06-03 09:37:09 -07001773static void complete_request(struct ceph_osd_request *req)
1774{
Sage Weil25845472011-06-03 09:37:09 -07001775 complete_all(&req->r_safe_completion); /* fsync waiter */
1776}
1777
Sage Weilf24e9982009-10-06 11:31:10 -07001778/*
1779 * handle osd op reply. either call the callback if it is specified,
1780 * or do the completion to wake up the waiting thread.
1781 */
Shraddha Barke70cf0522015-10-18 13:55:38 +05301782static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001783{
Sage Weil1b83bef2013-02-25 16:11:12 -08001784 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001785 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001786 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001787 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001788 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001789 unsigned int numops;
1790 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001791 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001792 s32 retry_attempt;
1793 struct ceph_pg pg;
1794 int err;
1795 u32 reassert_epoch;
1796 u64 reassert_version;
1797 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001798 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001799 u32 bytes;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001800 u8 decode_redir;
Alex Elder79528732013-04-03 21:32:51 -05001801 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001802
Sage Weil6df058c2009-12-22 11:24:33 -08001803 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001804 dout("handle_reply %p tid %llu\n", msg, tid);
1805
1806 p = msg->front.iov_base;
1807 end = p + msg->front.iov_len;
1808
1809 ceph_decode_need(&p, end, 4, bad);
1810 object_len = ceph_decode_32(&p);
1811 ceph_decode_need(&p, end, object_len, bad);
1812 p += object_len;
1813
Alex Elderef4859d2013-04-01 18:58:26 -05001814 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001815 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001816 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001817
1818 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1819 flags = ceph_decode_64(&p);
1820 result = ceph_decode_32(&p);
1821 reassert_epoch = ceph_decode_32(&p);
1822 reassert_version = ceph_decode_64(&p);
1823 osdmap_epoch = ceph_decode_32(&p);
1824
Sage Weilf24e9982009-10-06 11:31:10 -07001825 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001826 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001827 mutex_lock(&osdc->request_mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001828 req = lookup_request(&osdc->requests, tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001829 if (req == NULL) {
1830 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001831 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001832 }
1833 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001834
1835 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1836 req, result);
1837
Dan Carpenter18741192013-08-15 08:51:58 +03001838 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001839 numops = ceph_decode_32(&p);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01001840 if (numops > CEPH_OSD_MAX_OPS)
Sage Weil1b83bef2013-02-25 16:11:12 -08001841 goto bad_put;
1842 if (numops != req->r_num_ops)
1843 goto bad_put;
1844 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001845 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001846 for (i = 0; i < numops; i++) {
1847 struct ceph_osd_op *op = p;
1848 int len;
1849
1850 len = le32_to_cpu(op->payload_len);
Yan, Zheng7665d852016-01-07 16:48:57 +08001851 req->r_ops[i].outdata_len = len;
Sage Weil1b83bef2013-02-25 16:11:12 -08001852 dout(" op %d has %d bytes\n", i, len);
1853 payload_len += len;
1854 p += sizeof(*op);
1855 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001856 bytes = le32_to_cpu(msg->hdr.data_len);
1857 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001858 pr_warn("sum of op payload lens %d != data_len %d\n",
1859 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001860 goto bad_put;
1861 }
1862
Dan Carpenter18741192013-08-15 08:51:58 +03001863 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001864 retry_attempt = ceph_decode_32(&p);
1865 for (i = 0; i < numops; i++)
Yan, Zheng7665d852016-01-07 16:48:57 +08001866 req->r_ops[i].rval = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001867
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001868 if (le16_to_cpu(msg->hdr.version) >= 6) {
1869 p += 8 + 4; /* skip replay_version */
1870 p += 8; /* skip user_version */
1871
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001872 if (le16_to_cpu(msg->hdr.version) >= 7)
1873 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1874 else
1875 decode_redir = 1;
1876 } else {
1877 decode_redir = 0;
1878 }
1879
1880 if (decode_redir) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001881 err = ceph_redirect_decode(&p, end, &redir);
1882 if (err)
1883 goto bad_put;
1884 } else {
1885 redir.oloc.pool = -1;
1886 }
1887
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001888 if (!ceph_oloc_empty(&redir.oloc)) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001889 dout("redirect pool %lld\n", redir.oloc.pool);
1890
1891 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001892
Ilya Dryomova66dd382016-04-28 16:07:23 +02001893 ceph_oloc_copy(&req->r_t.target_oloc, &redir.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001894
1895 /*
1896 * Start redirect requests with nofail=true. If
1897 * mapping fails, request will end up on the notarget
1898 * list, waiting for the new osdmap (which can take
1899 * a while), even though the original request mapped
1900 * successfully. In the future we might want to follow
1901 * original request's nofail setting here.
1902 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001903 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001904 BUG_ON(err);
1905
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001906 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001907 }
1908
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001909 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001910 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001911 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001912 dout("handle_reply result %d bytes %d\n", req->r_result,
1913 bytes);
1914 if (req->r_result == 0)
1915 req->r_result = bytes;
1916
1917 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001918 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1919 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001920
1921 req->r_got_reply = 1;
1922 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1923 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001924 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001925 }
1926
1927 dout("handle_reply tid %llu flags %d\n", tid, flags);
1928
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001929 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1930 __register_linger_request(osdc, req);
1931
Sage Weilf24e9982009-10-06 11:31:10 -07001932 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001933 if (result < 0 ||
1934 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001935 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1936 __unregister_request(osdc, req);
1937
1938 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001939 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001940
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001941 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001942 if (req->r_unsafe_callback &&
1943 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1944 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001945 if (req->r_callback)
1946 req->r_callback(req, msg);
1947 else
1948 complete_all(&req->r_completion);
1949 }
Sage Weilf24e9982009-10-06 11:31:10 -07001950
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001951 if (flags & CEPH_OSD_FLAG_ONDISK) {
1952 if (req->r_unsafe_callback && already_completed)
1953 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001954 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001955 }
Sage Weilf24e9982009-10-06 11:31:10 -07001956
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001957out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001958 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001959 ceph_osdc_put_request(req);
1960 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001961out_unlock:
1962 mutex_unlock(&osdc->request_mutex);
1963 up_read(&osdc->map_sem);
1964 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001965
Sage Weil1b83bef2013-02-25 16:11:12 -08001966bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001967 req->r_result = -EIO;
1968 __unregister_request(osdc, req);
1969 if (req->r_callback)
1970 req->r_callback(req, msg);
1971 else
1972 complete_all(&req->r_completion);
1973 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001974 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001975bad_mutex:
1976 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001977 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001978bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001979 pr_err("corrupt osd_op_reply got %d %d\n",
1980 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001981 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001982}
1983
Sage Weil6f6c7002011-01-17 20:34:08 -08001984static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001985{
Sage Weilf24e9982009-10-06 11:31:10 -07001986 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001987
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001988 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08001989 for (p = rb_first(&osdc->osds); p; p = n) {
1990 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001991
Sage Weil6f6c7002011-01-17 20:34:08 -08001992 n = rb_next(p);
1993 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1994 memcmp(&osd->o_con.peer_addr,
1995 ceph_osd_addr(osdc->osdmap,
1996 osd->o_osd),
1997 sizeof(struct ceph_entity_addr)) != 0)
1998 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001999 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002000}
2001
2002/*
Sage Weil6f6c7002011-01-17 20:34:08 -08002003 * Requeue requests whose mapping to an OSD has changed. If requests map to
2004 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002005 *
Alex Eldere6d50f62012-12-26 14:31:40 -06002006 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002007 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002008static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
2009 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002010{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002011 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08002012 struct rb_node *p;
2013 int needmap = 0;
2014 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002015 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002016
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002017 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2018 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002019 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07002020 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08002021 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07002022 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06002023
2024 /*
2025 * For linger requests that have not yet been
2026 * registered, move them to the linger list; they'll
2027 * be sent to the osd in the loop below. Unregister
2028 * the request before re-registering it as a linger
2029 * request to ensure the __map_request() below
2030 * will decide it needs to be sent.
2031 */
2032 if (req->r_linger && list_empty(&req->r_linger_item)) {
2033 dout("%p tid %llu restart on osd%d\n",
2034 req, req->r_tid,
2035 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05002036 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002037 __unregister_request(osdc, req);
2038 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002039 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002040 continue;
2041 }
2042
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002043 force_resend_req = force_resend ||
2044 (force_resend_writes &&
2045 req->r_flags & CEPH_OSD_FLAG_WRITE);
2046 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002047 if (err < 0)
2048 continue; /* error */
2049 if (req->r_osd == NULL) {
2050 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2051 needmap++; /* request a newer map */
2052 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002053 if (!req->r_linger) {
2054 dout("%p tid %llu requeued on osd%d\n", req,
2055 req->r_tid,
2056 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002057 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002058 }
2059 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002060 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002061
2062 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2063 r_linger_item) {
2064 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2065
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002066 err = __map_request(osdc, req,
2067 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002068 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002069 if (err < 0)
2070 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002071 if (req->r_osd == NULL || err > 0) {
2072 if (req->r_osd == NULL) {
2073 dout("lingering %p tid %llu maps to no osd\n",
2074 req, req->r_tid);
2075 /*
2076 * A homeless lingering request makes
2077 * no sense, as it's job is to keep
2078 * a particular OSD connection open.
2079 * Request a newer map and kick the
2080 * request, knowing that it won't be
2081 * resent until we actually get a map
2082 * that can tell us where to send it.
2083 */
2084 needmap++;
2085 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002086
Ilya Dryomovb0494532015-05-11 17:53:10 +03002087 dout("kicking lingering %p tid %llu osd%d\n", req,
2088 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2089 __register_request(osdc, req);
2090 __unregister_linger_request(osdc, req);
2091 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002092 }
Alex Elder14d2f382013-05-15 16:28:33 -05002093 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002094 mutex_unlock(&osdc->request_mutex);
2095
2096 if (needmap) {
2097 dout("%d requests for down osds, need new map\n", needmap);
2098 ceph_monc_request_next_osdmap(&osdc->client->monc);
2099 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002100}
Sage Weil6f6c7002011-01-17 20:34:08 -08002101
2102
Sage Weilf24e9982009-10-06 11:31:10 -07002103/*
2104 * Process updated osd map.
2105 *
2106 * The message contains any number of incremental and full maps, normally
2107 * indicating some sort of topology change in the cluster. Kick requests
2108 * off to different OSDs as needed.
2109 */
2110void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2111{
2112 void *p, *end, *next;
2113 u32 nr_maps, maplen;
2114 u32 epoch;
2115 struct ceph_osdmap *newmap = NULL, *oldmap;
2116 int err;
2117 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002118 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002119
2120 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2121 p = msg->front.iov_base;
2122 end = p + msg->front.iov_len;
2123
2124 /* verify fsid */
2125 ceph_decode_need(&p, end, sizeof(fsid), bad);
2126 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002127 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2128 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002129
2130 down_write(&osdc->map_sem);
2131
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002132 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2133
Sage Weilf24e9982009-10-06 11:31:10 -07002134 /* incremental maps */
2135 ceph_decode_32_safe(&p, end, nr_maps, bad);
2136 dout(" %d inc maps\n", nr_maps);
2137 while (nr_maps > 0) {
2138 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002139 epoch = ceph_decode_32(&p);
2140 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002141 ceph_decode_need(&p, end, maplen, bad);
2142 next = p + maplen;
2143 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2144 dout("applying incremental map %u len %d\n",
2145 epoch, maplen);
2146 newmap = osdmap_apply_incremental(&p, next,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02002147 osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002148 if (IS_ERR(newmap)) {
2149 err = PTR_ERR(newmap);
2150 goto bad;
2151 }
Sage Weil30dc6382009-12-21 14:49:37 -08002152 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002153 if (newmap != osdc->osdmap) {
2154 ceph_osdmap_destroy(osdc->osdmap);
2155 osdc->osdmap = newmap;
2156 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002157 was_full = was_full ||
2158 ceph_osdmap_flag(osdc->osdmap,
2159 CEPH_OSDMAP_FULL);
2160 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002161 } else {
2162 dout("ignoring incremental map %u len %d\n",
2163 epoch, maplen);
2164 }
2165 p = next;
2166 nr_maps--;
2167 }
2168 if (newmap)
2169 goto done;
2170
2171 /* full maps */
2172 ceph_decode_32_safe(&p, end, nr_maps, bad);
2173 dout(" %d full maps\n", nr_maps);
2174 while (nr_maps) {
2175 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002176 epoch = ceph_decode_32(&p);
2177 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002178 ceph_decode_need(&p, end, maplen, bad);
2179 if (nr_maps > 1) {
2180 dout("skipping non-latest full map %u len %d\n",
2181 epoch, maplen);
2182 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2183 dout("skipping full map %u len %d, "
2184 "older than our %u\n", epoch, maplen,
2185 osdc->osdmap->epoch);
2186 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002187 int skipped_map = 0;
2188
Sage Weilf24e9982009-10-06 11:31:10 -07002189 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002190 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002191 if (IS_ERR(newmap)) {
2192 err = PTR_ERR(newmap);
2193 goto bad;
2194 }
Sage Weil30dc6382009-12-21 14:49:37 -08002195 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002196 oldmap = osdc->osdmap;
2197 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002198 if (oldmap) {
2199 if (oldmap->epoch + 1 < newmap->epoch)
2200 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002201 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002202 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002203 was_full = was_full ||
2204 ceph_osdmap_flag(osdc->osdmap,
2205 CEPH_OSDMAP_FULL);
2206 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002207 }
2208 p += maplen;
2209 nr_maps--;
2210 }
2211
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002212 if (!osdc->osdmap)
2213 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002214done:
2215 downgrade_write(&osdc->map_sem);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01002216 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2217 osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002218
2219 /*
2220 * subscribe to subsequent osdmap updates if full to ensure
2221 * we find out when we are no longer full and stop returning
2222 * ENOSPC.
2223 */
Josh Durgind29adb32013-12-02 19:11:48 -08002224 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2225 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2226 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002227 ceph_monc_request_next_osdmap(&osdc->client->monc);
2228
Alex Elderf9d25192013-02-15 11:42:29 -06002229 mutex_lock(&osdc->request_mutex);
2230 __send_queued(osdc);
2231 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002232 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002233 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002234 return;
2235
2236bad:
2237 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002238 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002239 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002240}
2241
Sage Weilf24e9982009-10-06 11:31:10 -07002242/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002243 * watch/notify callback event infrastructure
2244 *
2245 * These callbacks are used both for watch and notify operations.
2246 */
2247static void __release_event(struct kref *kref)
2248{
2249 struct ceph_osd_event *event =
2250 container_of(kref, struct ceph_osd_event, kref);
2251
2252 dout("__release_event %p\n", event);
2253 kfree(event);
2254}
2255
2256static void get_event(struct ceph_osd_event *event)
2257{
2258 kref_get(&event->kref);
2259}
2260
2261void ceph_osdc_put_event(struct ceph_osd_event *event)
2262{
2263 kref_put(&event->kref, __release_event);
2264}
2265EXPORT_SYMBOL(ceph_osdc_put_event);
2266
2267static void __insert_event(struct ceph_osd_client *osdc,
2268 struct ceph_osd_event *new)
2269{
2270 struct rb_node **p = &osdc->event_tree.rb_node;
2271 struct rb_node *parent = NULL;
2272 struct ceph_osd_event *event = NULL;
2273
2274 while (*p) {
2275 parent = *p;
2276 event = rb_entry(parent, struct ceph_osd_event, node);
2277 if (new->cookie < event->cookie)
2278 p = &(*p)->rb_left;
2279 else if (new->cookie > event->cookie)
2280 p = &(*p)->rb_right;
2281 else
2282 BUG();
2283 }
2284
2285 rb_link_node(&new->node, parent, p);
2286 rb_insert_color(&new->node, &osdc->event_tree);
2287}
2288
2289static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2290 u64 cookie)
2291{
2292 struct rb_node **p = &osdc->event_tree.rb_node;
2293 struct rb_node *parent = NULL;
2294 struct ceph_osd_event *event = NULL;
2295
2296 while (*p) {
2297 parent = *p;
2298 event = rb_entry(parent, struct ceph_osd_event, node);
2299 if (cookie < event->cookie)
2300 p = &(*p)->rb_left;
2301 else if (cookie > event->cookie)
2302 p = &(*p)->rb_right;
2303 else
2304 return event;
2305 }
2306 return NULL;
2307}
2308
2309static void __remove_event(struct ceph_osd_event *event)
2310{
2311 struct ceph_osd_client *osdc = event->osdc;
2312
2313 if (!RB_EMPTY_NODE(&event->node)) {
2314 dout("__remove_event removed %p\n", event);
2315 rb_erase(&event->node, &osdc->event_tree);
2316 ceph_osdc_put_event(event);
2317 } else {
2318 dout("__remove_event didn't remove %p\n", event);
2319 }
2320}
2321
2322int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2323 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002324 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002325{
2326 struct ceph_osd_event *event;
2327
2328 event = kmalloc(sizeof(*event), GFP_NOIO);
2329 if (!event)
2330 return -ENOMEM;
2331
2332 dout("create_event %p\n", event);
2333 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002334 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002335 event->data = data;
2336 event->osdc = osdc;
2337 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002338 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002339 kref_init(&event->kref); /* one ref for us */
2340 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002341
2342 spin_lock(&osdc->event_lock);
2343 event->cookie = ++osdc->event_count;
2344 __insert_event(osdc, event);
2345 spin_unlock(&osdc->event_lock);
2346
2347 *pevent = event;
2348 return 0;
2349}
2350EXPORT_SYMBOL(ceph_osdc_create_event);
2351
2352void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2353{
2354 struct ceph_osd_client *osdc = event->osdc;
2355
2356 dout("cancel_event %p\n", event);
2357 spin_lock(&osdc->event_lock);
2358 __remove_event(event);
2359 spin_unlock(&osdc->event_lock);
2360 ceph_osdc_put_event(event); /* caller's */
2361}
2362EXPORT_SYMBOL(ceph_osdc_cancel_event);
2363
2364
2365static void do_event_work(struct work_struct *work)
2366{
2367 struct ceph_osd_event_work *event_work =
2368 container_of(work, struct ceph_osd_event_work, work);
2369 struct ceph_osd_event *event = event_work->event;
2370 u64 ver = event_work->ver;
2371 u64 notify_id = event_work->notify_id;
2372 u8 opcode = event_work->opcode;
2373
2374 dout("do_event_work completing %p\n", event);
2375 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002376 dout("do_event_work completed %p\n", event);
2377 ceph_osdc_put_event(event);
2378 kfree(event_work);
2379}
2380
2381
2382/*
2383 * Process osd watch notifications
2384 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002385static void handle_watch_notify(struct ceph_osd_client *osdc,
2386 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002387{
2388 void *p, *end;
2389 u8 proto_ver;
2390 u64 cookie, ver, notify_id;
2391 u8 opcode;
2392 struct ceph_osd_event *event;
2393 struct ceph_osd_event_work *event_work;
2394
2395 p = msg->front.iov_base;
2396 end = p + msg->front.iov_len;
2397
2398 ceph_decode_8_safe(&p, end, proto_ver, bad);
2399 ceph_decode_8_safe(&p, end, opcode, bad);
2400 ceph_decode_64_safe(&p, end, cookie, bad);
2401 ceph_decode_64_safe(&p, end, ver, bad);
2402 ceph_decode_64_safe(&p, end, notify_id, bad);
2403
2404 spin_lock(&osdc->event_lock);
2405 event = __find_event(osdc, cookie);
2406 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002407 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002408 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002409 }
2410 spin_unlock(&osdc->event_lock);
2411 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2412 cookie, ver, event);
2413 if (event) {
2414 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002415 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002416 pr_err("couldn't allocate event_work\n");
2417 ceph_osdc_put_event(event);
2418 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002419 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002420 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002421 event_work->event = event;
2422 event_work->ver = ver;
2423 event_work->notify_id = notify_id;
2424 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002425
2426 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002427 }
2428
2429 return;
2430
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002431bad:
2432 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002433}
2434
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002435/*
Alex Eldere65550f2013-04-05 01:27:12 -05002436 * build new request AND message
2437 *
2438 */
2439void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2440 struct ceph_snap_context *snapc, u64 snap_id,
2441 struct timespec *mtime)
2442{
2443 struct ceph_msg *msg = req->r_request;
2444 void *p;
2445 size_t msg_size;
2446 int flags = req->r_flags;
2447 u64 data_len;
2448 unsigned int i;
2449
2450 req->r_snapid = snap_id;
Ilya Dryomov84127282016-04-26 15:39:47 +02002451 WARN_ON(snapc != req->r_snapc);
Alex Eldere65550f2013-04-05 01:27:12 -05002452
2453 /* encode request */
2454 msg->hdr.version = cpu_to_le16(4);
2455
2456 p = msg->front.iov_base;
2457 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2458 req->r_request_osdmap_epoch = p;
2459 p += 4;
2460 req->r_request_flags = p;
2461 p += 4;
2462 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2463 ceph_encode_timespec(p, mtime);
2464 p += sizeof(struct ceph_timespec);
2465 req->r_request_reassert_version = p;
2466 p += sizeof(struct ceph_eversion); /* will get filled in */
2467
2468 /* oloc */
2469 ceph_encode_8(&p, 4);
2470 ceph_encode_8(&p, 4);
2471 ceph_encode_32(&p, 8 + 4 + 4);
2472 req->r_request_pool = p;
2473 p += 8;
2474 ceph_encode_32(&p, -1); /* preferred */
2475 ceph_encode_32(&p, 0); /* key len */
2476
2477 ceph_encode_8(&p, 1);
2478 req->r_request_pgid = p;
2479 p += 8 + 4;
2480 ceph_encode_32(&p, -1); /* preferred */
2481
2482 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002483 ceph_encode_32(&p, req->r_base_oid.name_len);
2484 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002485 dout("oid %*pE len %d\n", req->r_base_oid.name_len,
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002486 req->r_base_oid.name, req->r_base_oid.name_len);
2487 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002488
2489 /* ops--can imply data */
2490 ceph_encode_16(&p, (u16)req->r_num_ops);
2491 data_len = 0;
2492 for (i = 0; i < req->r_num_ops; i++) {
2493 data_len += osd_req_encode_op(req, p, i);
2494 p += sizeof(struct ceph_osd_op);
2495 }
2496
2497 /* snaps */
2498 ceph_encode_64(&p, req->r_snapid);
2499 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2500 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2501 if (req->r_snapc) {
Ilya Dryomov84127282016-04-26 15:39:47 +02002502 for (i = 0; i < req->r_snapc->num_snaps; i++) {
Alex Eldere65550f2013-04-05 01:27:12 -05002503 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2504 }
2505 }
2506
2507 req->r_request_attempts = p;
2508 p += 4;
2509
2510 /* data */
2511 if (flags & CEPH_OSD_FLAG_WRITE) {
2512 u16 data_off;
2513
2514 /*
2515 * The header "data_off" is a hint to the receiver
2516 * allowing it to align received data into its
2517 * buffers such that there's no need to re-copy
2518 * it before writing it to disk (direct I/O).
2519 */
2520 data_off = (u16) (off & 0xffff);
2521 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2522 }
2523 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2524
2525 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2526 msg_size = p - msg->front.iov_base;
2527 msg->front.iov_len = msg_size;
2528 msg->hdr.front_len = cpu_to_le32(msg_size);
2529
2530 dout("build_request msg_size was %d\n", (int)msg_size);
2531}
2532EXPORT_SYMBOL(ceph_osdc_build_request);
2533
2534/*
Sage Weilf24e9982009-10-06 11:31:10 -07002535 * Register request, send initial attempt.
2536 */
2537int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2538 struct ceph_osd_request *req,
2539 bool nofail)
2540{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002541 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002542
Sage Weilf24e9982009-10-06 11:31:10 -07002543 down_read(&osdc->map_sem);
2544 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002545
2546 rc = __ceph_osdc_start_request(osdc, req, nofail);
2547
Sage Weilf24e9982009-10-06 11:31:10 -07002548 mutex_unlock(&osdc->request_mutex);
2549 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002550
Sage Weilf24e9982009-10-06 11:31:10 -07002551 return rc;
2552}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002553EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002554
2555/*
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002556 * Unregister a registered request. The request is not completed (i.e.
2557 * no callbacks or wakeups) - higher layers are supposed to know what
2558 * they are canceling.
2559 */
2560void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2561{
2562 struct ceph_osd_client *osdc = req->r_osdc;
2563
2564 mutex_lock(&osdc->request_mutex);
2565 if (req->r_linger)
2566 __unregister_linger_request(osdc, req);
2567 __unregister_request(osdc, req);
2568 mutex_unlock(&osdc->request_mutex);
2569
2570 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2571}
2572EXPORT_SYMBOL(ceph_osdc_cancel_request);
2573
2574/*
Sage Weilf24e9982009-10-06 11:31:10 -07002575 * wait for a request to complete
2576 */
2577int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2578 struct ceph_osd_request *req)
2579{
2580 int rc;
2581
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002582 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2583
Sage Weilf24e9982009-10-06 11:31:10 -07002584 rc = wait_for_completion_interruptible(&req->r_completion);
2585 if (rc < 0) {
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002586 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2587 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002588 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002589 return rc;
2590 }
2591
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002592 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2593 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002594 return req->r_result;
2595}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002596EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002597
2598/*
2599 * sync - wait for all in-flight requests to flush. avoid starvation.
2600 */
2601void ceph_osdc_sync(struct ceph_osd_client *osdc)
2602{
2603 struct ceph_osd_request *req;
2604 u64 last_tid, next_tid = 0;
2605
2606 mutex_lock(&osdc->request_mutex);
2607 last_tid = osdc->last_tid;
2608 while (1) {
2609 req = __lookup_request_ge(osdc, next_tid);
2610 if (!req)
2611 break;
2612 if (req->r_tid > last_tid)
2613 break;
2614
2615 next_tid = req->r_tid + 1;
2616 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2617 continue;
2618
2619 ceph_osdc_get_request(req);
2620 mutex_unlock(&osdc->request_mutex);
2621 dout("sync waiting on tid %llu (last is %llu)\n",
2622 req->r_tid, last_tid);
2623 wait_for_completion(&req->r_safe_completion);
2624 mutex_lock(&osdc->request_mutex);
2625 ceph_osdc_put_request(req);
2626 }
2627 mutex_unlock(&osdc->request_mutex);
2628 dout("sync done (thru tid %llu)\n", last_tid);
2629}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002630EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002631
2632/*
Josh Durgindd935f42013-08-28 21:43:09 -07002633 * Call all pending notify callbacks - for use after a watch is
2634 * unregistered, to make sure no more callbacks for it will be invoked
2635 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002636void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002637{
2638 flush_workqueue(osdc->notify_wq);
2639}
2640EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2641
2642
2643/*
Sage Weilf24e9982009-10-06 11:31:10 -07002644 * init, shutdown
2645 */
2646int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2647{
2648 int err;
2649
2650 dout("init\n");
2651 osdc->client = client;
2652 osdc->osdmap = NULL;
2653 init_rwsem(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002654 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002655 osdc->last_tid = 0;
2656 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002657 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002658 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002659 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002660 INIT_LIST_HEAD(&osdc->req_unsent);
2661 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002662 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002663 osdc->num_requests = 0;
2664 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002665 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002666 spin_lock_init(&osdc->event_lock);
2667 osdc->event_tree = RB_ROOT;
2668 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002669
2670 schedule_delayed_work(&osdc->osds_timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03002671 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
Sage Weilf24e9982009-10-06 11:31:10 -07002672
Sage Weil5f44f142009-11-18 14:52:18 -08002673 err = -ENOMEM;
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01002674 osdc->req_mempool = mempool_create_slab_pool(10,
2675 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07002676 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002677 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002678
Sage Weild50b4092012-07-09 14:22:34 -07002679 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02002680 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002681 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002682 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002683 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02002684 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002685 if (err < 0)
2686 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002687
Dan Carpenterdbcae082013-08-15 08:58:59 +03002688 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002689 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002690 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002691 goto out_msgpool_reply;
2692
Sage Weilf24e9982009-10-06 11:31:10 -07002693 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002694
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002695out_msgpool_reply:
2696 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002697out_msgpool:
2698 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002699out_mempool:
2700 mempool_destroy(osdc->req_mempool);
2701out:
2702 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002703}
2704
2705void ceph_osdc_stop(struct ceph_osd_client *osdc)
2706{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002707 flush_workqueue(osdc->notify_wq);
2708 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002709 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002710 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002711
2712 mutex_lock(&osdc->request_mutex);
2713 while (!RB_EMPTY_ROOT(&osdc->osds)) {
2714 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
2715 struct ceph_osd, o_node);
2716 remove_osd(osdc, osd);
2717 }
2718 mutex_unlock(&osdc->request_mutex);
2719
Sage Weilf24e9982009-10-06 11:31:10 -07002720 if (osdc->osdmap) {
2721 ceph_osdmap_destroy(osdc->osdmap);
2722 osdc->osdmap = NULL;
2723 }
2724 mempool_destroy(osdc->req_mempool);
2725 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002726 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002727}
2728
2729/*
2730 * Read some contiguous pages. If we cross a stripe boundary, shorten
2731 * *plen. Return number of bytes read, or error.
2732 */
2733int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2734 struct ceph_vino vino, struct ceph_file_layout *layout,
2735 u64 off, u64 *plen,
2736 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002737 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002738{
2739 struct ceph_osd_request *req;
2740 int rc = 0;
2741
2742 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2743 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002744 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002745 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002746 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002747 false);
Sage Weil68162822012-09-24 21:01:02 -07002748 if (IS_ERR(req))
2749 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002750
2751 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002752
Alex Elder406e2c92013-04-15 14:50:36 -05002753 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002754 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002755
Alex Eldere0c59482013-03-07 15:38:25 -06002756 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002757 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002758
Alex Elder79528732013-04-03 21:32:51 -05002759 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002760
Sage Weilf24e9982009-10-06 11:31:10 -07002761 rc = ceph_osdc_start_request(osdc, req, false);
2762 if (!rc)
2763 rc = ceph_osdc_wait_request(osdc, req);
2764
2765 ceph_osdc_put_request(req);
2766 dout("readpages result %d\n", rc);
2767 return rc;
2768}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002769EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002770
2771/*
2772 * do a synchronous write on N pages
2773 */
2774int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2775 struct ceph_file_layout *layout,
2776 struct ceph_snap_context *snapc,
2777 u64 off, u64 len,
2778 u32 truncate_seq, u64 truncate_size,
2779 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002780 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002781{
2782 struct ceph_osd_request *req;
2783 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002784 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002785
Alex Elderacead002013-03-14 14:09:05 -05002786 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002787 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002788 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002789 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002790 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002791 true);
Sage Weil68162822012-09-24 21:01:02 -07002792 if (IS_ERR(req))
2793 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002794
2795 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002796 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002797 false, false);
2798 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002799
Alex Elder79528732013-04-03 21:32:51 -05002800 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002801
Alex Elder87f979d2013-02-15 11:42:29 -06002802 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002803 if (!rc)
2804 rc = ceph_osdc_wait_request(osdc, req);
2805
2806 ceph_osdc_put_request(req);
2807 if (rc == 0)
2808 rc = len;
2809 dout("writepages result %d\n", rc);
2810 return rc;
2811}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002812EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002813
Alex Elder5522ae02013-05-01 12:43:04 -05002814int ceph_osdc_setup(void)
2815{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002816 size_t size = sizeof(struct ceph_osd_request) +
2817 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2818
Alex Elder5522ae02013-05-01 12:43:04 -05002819 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002820 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2821 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05002822
2823 return ceph_osd_request_cache ? 0 : -ENOMEM;
2824}
2825EXPORT_SYMBOL(ceph_osdc_setup);
2826
2827void ceph_osdc_cleanup(void)
2828{
2829 BUG_ON(!ceph_osd_request_cache);
2830 kmem_cache_destroy(ceph_osd_request_cache);
2831 ceph_osd_request_cache = NULL;
2832}
2833EXPORT_SYMBOL(ceph_osdc_cleanup);
2834
Sage Weilf24e9982009-10-06 11:31:10 -07002835/*
2836 * handle incoming message
2837 */
2838static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2839{
2840 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002841 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002842 int type = le16_to_cpu(msg->hdr.type);
2843
2844 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002845 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002846 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002847
2848 switch (type) {
2849 case CEPH_MSG_OSD_MAP:
2850 ceph_osdc_handle_map(osdc, msg);
2851 break;
2852 case CEPH_MSG_OSD_OPREPLY:
Shraddha Barke70cf0522015-10-18 13:55:38 +05302853 handle_reply(osdc, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002854 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002855 case CEPH_MSG_WATCH_NOTIFY:
2856 handle_watch_notify(osdc, msg);
2857 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002858
2859 default:
2860 pr_err("received unknown message type %d %s\n", type,
2861 ceph_msg_type_name(type));
2862 }
Sage Weil4a32f932010-06-13 10:27:53 -07002863out:
Sage Weilf24e9982009-10-06 11:31:10 -07002864 ceph_msg_put(msg);
2865}
2866
Sage Weil5b3a4db2010-02-19 21:43:23 -08002867/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002868 * Lookup and return message for incoming reply. Don't try to do
2869 * anything about a larger than preallocated data portion of the
2870 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002871 */
2872static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002873 struct ceph_msg_header *hdr,
2874 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002875{
2876 struct ceph_osd *osd = con->private;
2877 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002878 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002879 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002880 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002881 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002882 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002883
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002884 tid = le64_to_cpu(hdr->tid);
2885 mutex_lock(&osdc->request_mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002886 req = lookup_request(&osdc->requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002887 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01002888 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2889 osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002890 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002891 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002892 goto out;
2893 }
Sage Weilc16e7862010-03-01 13:02:00 -08002894
Alex Elderace6d3a2013-04-01 16:12:14 -05002895 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002896
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002897 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002898 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2899 __func__, osd->o_osd, req->r_tid, front_len,
2900 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002901 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2902 false);
Sage Weila79832f2010-04-01 16:06:19 -07002903 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002904 goto out;
2905 ceph_msg_put(req->r_reply);
2906 req->r_reply = m;
2907 }
Sage Weilc16e7862010-03-01 13:02:00 -08002908
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002909 if (data_len > req->r_reply->data_length) {
2910 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2911 __func__, osd->o_osd, req->r_tid, data_len,
2912 req->r_reply->data_length);
2913 m = NULL;
2914 *skip = 1;
2915 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002916 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002917
2918 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002919 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002920
2921out:
2922 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002923 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002924}
2925
2926static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2927 struct ceph_msg_header *hdr,
2928 int *skip)
2929{
2930 struct ceph_osd *osd = con->private;
2931 int type = le16_to_cpu(hdr->type);
2932 int front = le32_to_cpu(hdr->front_len);
2933
Alex Elder1c20f2d2012-06-04 14:43:32 -05002934 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002935 switch (type) {
2936 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002937 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002938 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002939 case CEPH_MSG_OSD_OPREPLY:
2940 return get_reply(con, hdr, skip);
2941 default:
2942 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2943 osd->o_osd);
2944 *skip = 1;
2945 return NULL;
2946 }
Sage Weilf24e9982009-10-06 11:31:10 -07002947}
2948
2949/*
2950 * Wrappers to refcount containing ceph_osd struct
2951 */
2952static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2953{
2954 struct ceph_osd *osd = con->private;
2955 if (get_osd(osd))
2956 return con;
2957 return NULL;
2958}
2959
2960static void put_osd_con(struct ceph_connection *con)
2961{
2962 struct ceph_osd *osd = con->private;
2963 put_osd(osd);
2964}
2965
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002966/*
2967 * authentication
2968 */
Alex Eldera3530df2012-05-16 15:16:39 -05002969/*
2970 * Note: returned pointer is the address of a structure that's
2971 * managed separately. Caller must *not* attempt to free it.
2972 */
2973static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002974 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002975{
2976 struct ceph_osd *o = con->private;
2977 struct ceph_osd_client *osdc = o->o_osdc;
2978 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002979 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002980
Alex Elder74f18692012-05-16 15:16:39 -05002981 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02002982 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002983 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002984 }
Sage Weil27859f92013-03-25 10:26:14 -07002985 if (!auth->authorizer) {
2986 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2987 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002988 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002989 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002990 } else {
2991 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002992 auth);
2993 if (ret)
2994 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002995 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002996 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002997
Alex Eldera3530df2012-05-16 15:16:39 -05002998 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002999}
3000
3001
3002static int verify_authorizer_reply(struct ceph_connection *con, int len)
3003{
3004 struct ceph_osd *o = con->private;
3005 struct ceph_osd_client *osdc = o->o_osdc;
3006 struct ceph_auth_client *ac = osdc->client->monc.auth;
3007
Sage Weil27859f92013-03-25 10:26:14 -07003008 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003009}
3010
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003011static int invalidate_authorizer(struct ceph_connection *con)
3012{
3013 struct ceph_osd *o = con->private;
3014 struct ceph_osd_client *osdc = o->o_osdc;
3015 struct ceph_auth_client *ac = osdc->client->monc.auth;
3016
Sage Weil27859f92013-03-25 10:26:14 -07003017 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003018 return ceph_monc_validate_auth(&osdc->client->monc);
3019}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003020
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003021static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003022{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003023 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003024 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003025
Yan, Zheng33d07332014-11-04 16:33:37 +08003026 return ceph_auth_sign_message(auth, msg);
3027}
3028
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003029static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003030{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003031 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003032 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003033
Yan, Zheng33d07332014-11-04 16:33:37 +08003034 return ceph_auth_check_message_signature(auth, msg);
3035}
3036
Tobias Klauser9e327892010-05-20 10:40:19 +02003037static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07003038 .get = get_osd_con,
3039 .put = put_osd_con,
3040 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003041 .get_authorizer = get_authorizer,
3042 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003043 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003044 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003045 .sign_message = osd_sign_message,
3046 .check_message_signature = osd_check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003047 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003048};