blob: 41dabce9c9c37c4d721ec6a81df66509ce74793a [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 Weilf24e9982009-10-06 11:31:10 -070037
38/*
39 * Implement client access to distributed object storage cluster.
40 *
41 * All data objects are stored within a cluster/cloud of OSDs, or
42 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
43 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
44 * remote daemons serving up and coordinating consistent and safe
45 * access to storage.
46 *
47 * Cluster membership and the mapping of data objects onto storage devices
48 * are described by the osd map.
49 *
50 * We keep track of pending OSD requests (read, write), resubmit
51 * requests to different OSDs when the cluster topology/data layout
52 * change, or retry the affected requests when the communications
53 * channel with an OSD is reset.
54 */
55
56/*
57 * calculate the mapping of a file extent onto an object, and fill out the
58 * request accordingly. shorten extent as necessary if it crosses an
59 * object boundary.
60 *
61 * fill osd op in request message.
62 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060063static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050064 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070065{
Alex Elder60e56f12013-02-15 11:42:29 -060066 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070067 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070068
Alex Elder60e56f12013-02-15 11:42:29 -060069 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050070 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
71 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070072 if (r < 0)
73 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050074 if (*objlen < orig_len) {
75 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060076 dout(" skipping last %llu, final file extent %llu~%llu\n",
77 orig_len - *plen, off, *plen);
78 }
79
Alex Elder75d1c942013-03-13 20:50:00 -050080 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070081
Alex Elder3ff5f382013-02-15 22:10:17 -060082 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070083}
84
Alex Elderc54d47b2013-04-03 01:28:57 -050085static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
86{
87 memset(osd_data, 0, sizeof (*osd_data));
88 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
89}
90
Alex Eldera4ce40a2013-04-05 01:27:12 -050091static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -050092 struct page **pages, u64 length, u32 alignment,
93 bool pages_from_pool, bool own_pages)
94{
95 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
96 osd_data->pages = pages;
97 osd_data->length = length;
98 osd_data->alignment = alignment;
99 osd_data->pages_from_pool = pages_from_pool;
100 osd_data->own_pages = own_pages;
101}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500102
Alex Eldera4ce40a2013-04-05 01:27:12 -0500103static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500104 struct ceph_pagelist *pagelist)
105{
106 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
107 osd_data->pagelist = pagelist;
108}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500109
110#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500111static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500112 struct bio *bio, size_t bio_length)
113{
114 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
115 osd_data->bio = bio;
116 osd_data->bio_length = bio_length;
117}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500118#endif /* CONFIG_BLOCK */
119
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300120#define osd_req_op_data(oreq, whch, typ, fld) \
121({ \
122 struct ceph_osd_request *__oreq = (oreq); \
123 unsigned int __whch = (whch); \
124 BUG_ON(__whch >= __oreq->r_num_ops); \
125 &__oreq->r_ops[__whch].typ.fld; \
126})
Alex Elder863c7eb2013-04-15 14:50:36 -0500127
Alex Elder49719772013-02-11 12:33:24 -0600128static struct ceph_osd_data *
129osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
130{
131 BUG_ON(which >= osd_req->r_num_ops);
132
133 return &osd_req->r_ops[which].raw_data_in;
134}
135
Alex Eldera4ce40a2013-04-05 01:27:12 -0500136struct ceph_osd_data *
137osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500138 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500139{
Alex Elder863c7eb2013-04-15 14:50:36 -0500140 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500141}
142EXPORT_SYMBOL(osd_req_op_extent_osd_data);
143
Alex Elder49719772013-02-11 12:33:24 -0600144void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
145 unsigned int which, struct page **pages,
146 u64 length, u32 alignment,
147 bool pages_from_pool, bool own_pages)
148{
149 struct ceph_osd_data *osd_data;
150
151 osd_data = osd_req_op_raw_data_in(osd_req, which);
152 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
153 pages_from_pool, own_pages);
154}
155EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
156
Alex Eldera4ce40a2013-04-05 01:27:12 -0500157void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500158 unsigned int which, struct page **pages,
159 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500160 bool pages_from_pool, bool own_pages)
161{
162 struct ceph_osd_data *osd_data;
163
Alex Elder863c7eb2013-04-15 14:50:36 -0500164 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500165 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
166 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500167}
168EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
169
170void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500171 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500172{
173 struct ceph_osd_data *osd_data;
174
Alex Elder863c7eb2013-04-15 14:50:36 -0500175 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500176 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500177}
178EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
179
180#ifdef CONFIG_BLOCK
181void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500182 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500183{
184 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500185
186 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500187 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500188}
189EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
190#endif /* CONFIG_BLOCK */
191
192static void osd_req_op_cls_request_info_pagelist(
193 struct ceph_osd_request *osd_req,
194 unsigned int which, struct ceph_pagelist *pagelist)
195{
196 struct ceph_osd_data *osd_data;
197
Alex Elder863c7eb2013-04-15 14:50:36 -0500198 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500199 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500200}
201
Alex Elder04017e22013-04-05 14:46:02 -0500202void osd_req_op_cls_request_data_pagelist(
203 struct ceph_osd_request *osd_req,
204 unsigned int which, struct ceph_pagelist *pagelist)
205{
206 struct ceph_osd_data *osd_data;
207
Alex Elder863c7eb2013-04-15 14:50:36 -0500208 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500209 ceph_osd_data_pagelist_init(osd_data, pagelist);
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200210 osd_req->r_ops[which].cls.indata_len += pagelist->length;
211 osd_req->r_ops[which].indata_len += pagelist->length;
Alex Elder04017e22013-04-05 14:46:02 -0500212}
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);
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200224 osd_req->r_ops[which].cls.indata_len += length;
225 osd_req->r_ops[which].indata_len += length;
Alex Elder6c57b552013-04-19 15:34:49 -0500226}
227EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
228
Alex Eldera4ce40a2013-04-05 01:27:12 -0500229void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
230 unsigned int which, struct page **pages, u64 length,
231 u32 alignment, bool pages_from_pool, bool own_pages)
232{
233 struct ceph_osd_data *osd_data;
234
Alex Elder863c7eb2013-04-15 14:50:36 -0500235 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500236 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
237 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500238}
239EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
240
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500241static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
242{
243 switch (osd_data->type) {
244 case CEPH_OSD_DATA_TYPE_NONE:
245 return 0;
246 case CEPH_OSD_DATA_TYPE_PAGES:
247 return osd_data->length;
248 case CEPH_OSD_DATA_TYPE_PAGELIST:
249 return (u64)osd_data->pagelist->length;
250#ifdef CONFIG_BLOCK
251 case CEPH_OSD_DATA_TYPE_BIO:
252 return (u64)osd_data->bio_length;
253#endif /* CONFIG_BLOCK */
254 default:
255 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
256 return 0;
257 }
258}
259
Alex Elderc54d47b2013-04-03 01:28:57 -0500260static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
261{
Alex Elder54764922013-04-05 01:27:12 -0500262 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500263 int num_pages;
264
265 num_pages = calc_pages_for((u64)osd_data->alignment,
266 (u64)osd_data->length);
267 ceph_release_page_vector(osd_data->pages, num_pages);
268 }
Alex Elder54764922013-04-05 01:27:12 -0500269 ceph_osd_data_init(osd_data);
270}
271
272static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
273 unsigned int which)
274{
275 struct ceph_osd_req_op *op;
276
277 BUG_ON(which >= osd_req->r_num_ops);
278 op = &osd_req->r_ops[which];
279
280 switch (op->op) {
281 case CEPH_OSD_OP_READ:
282 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200283 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500284 ceph_osd_data_release(&op->extent.osd_data);
285 break;
286 case CEPH_OSD_OP_CALL:
287 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500288 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500289 ceph_osd_data_release(&op->cls.response_data);
290 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800291 case CEPH_OSD_OP_SETXATTR:
292 case CEPH_OSD_OP_CMPXATTR:
293 ceph_osd_data_release(&op->xattr.osd_data);
294 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800295 case CEPH_OSD_OP_STAT:
296 ceph_osd_data_release(&op->raw_data_in);
297 break;
Alex Elder54764922013-04-05 01:27:12 -0500298 default:
299 break;
300 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500301}
302
Sage Weilf24e9982009-10-06 11:31:10 -0700303/*
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200304 * Assumes @t is zero-initialized.
305 */
306static void target_init(struct ceph_osd_request_target *t)
307{
308 ceph_oid_init(&t->base_oid);
309 ceph_oloc_init(&t->base_oloc);
310 ceph_oid_init(&t->target_oid);
311 ceph_oloc_init(&t->target_oloc);
312
313 ceph_osds_init(&t->acting);
314 ceph_osds_init(&t->up);
315 t->size = -1;
316 t->min_size = -1;
317
318 t->osd = CEPH_HOMELESS_OSD;
319}
320
321static void target_destroy(struct ceph_osd_request_target *t)
322{
323 ceph_oid_destroy(&t->base_oid);
324 ceph_oid_destroy(&t->target_oid);
325}
326
327/*
Sage Weilf24e9982009-10-06 11:31:10 -0700328 * requests
329 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400330static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700331{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400332 struct ceph_osd_request *req = container_of(kref,
333 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500334 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800335
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400336 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
337 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400338 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
339 WARN_ON(!list_empty(&req->r_req_lru_item));
340 WARN_ON(!list_empty(&req->r_osd_item));
341 WARN_ON(!list_empty(&req->r_linger_item));
342 WARN_ON(!list_empty(&req->r_linger_osd_item));
343 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400344
Sage Weil415e49a2009-12-07 13:37:03 -0800345 if (req->r_request)
346 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500347 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500348 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500349 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500350 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600351
Alex Elder54764922013-04-05 01:27:12 -0500352 for (which = 0; which < req->r_num_ops; which++)
353 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600354
Ilya Dryomova66dd382016-04-28 16:07:23 +0200355 target_destroy(&req->r_t);
Sage Weil415e49a2009-12-07 13:37:03 -0800356 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200357
Sage Weil415e49a2009-12-07 13:37:03 -0800358 if (req->r_mempool)
359 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100360 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500361 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100362 else
363 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700364}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400365
366void ceph_osdc_get_request(struct ceph_osd_request *req)
367{
368 dout("%s %p (was %d)\n", __func__, req,
369 atomic_read(&req->r_kref.refcount));
370 kref_get(&req->r_kref);
371}
372EXPORT_SYMBOL(ceph_osdc_get_request);
373
374void ceph_osdc_put_request(struct ceph_osd_request *req)
375{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200376 if (req) {
377 dout("%s %p (was %d)\n", __func__, req,
378 atomic_read(&req->r_kref.refcount));
379 kref_put(&req->r_kref, ceph_osdc_release_request);
380 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400381}
382EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700383
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700384struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700385 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800386 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700387 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600388 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700389{
390 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700391
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700392 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100393 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700394 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100395 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
396 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700397 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100398 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
399 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
400 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700401 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100402 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700403 return NULL;
404
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100405 /* req only, each op is zeroed in _osd_req_op_init() */
406 memset(req, 0, sizeof(*req));
407
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700408 req->r_osdc = osdc;
409 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500410 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200411 req->r_snapid = CEPH_NOSNAP;
412 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700413
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700414 kref_init(&req->r_kref);
415 init_completion(&req->r_completion);
416 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600417 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700418 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700419 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400420 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700421 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700422 INIT_LIST_HEAD(&req->r_osd_item);
423
Ilya Dryomova66dd382016-04-28 16:07:23 +0200424 target_init(&req->r_t);
Ilya Dryomov221165252014-01-27 17:40:18 +0200425
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200426 dout("%s req %p\n", __func__, req);
427 return req;
428}
429EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100430
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200431int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
432{
433 struct ceph_osd_client *osdc = req->r_osdc;
434 struct ceph_msg *msg;
435 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700436
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200437 WARN_ON(ceph_oid_empty(&req->r_base_oid));
438
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200439 /* create request message */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100440 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
441 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
442 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
443 msg_size += 1 + 8 + 4 + 4; /* pgid */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200444 msg_size += 4 + req->r_base_oid.name_len; /* oid */
445 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100446 msg_size += 8; /* snapid */
447 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200448 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100449 msg_size += 4; /* retry_attempt */
450
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200451 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700452 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
453 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200454 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
455 if (!msg)
456 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700457
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700458 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700459 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700460
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200461 /* create reply message */
462 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200463 msg_size += req->r_base_oid.name_len;
464 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200465
466 if (req->r_mempool)
467 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
468 else
469 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
470 if (!msg)
471 return -ENOMEM;
472
473 req->r_reply = msg;
474
475 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700476}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200477EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700478
Alex Eldera8dd0a32013-03-13 20:50:00 -0500479static bool osd_req_opcode_valid(u16 opcode)
480{
481 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400482#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
483__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
484#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500485 default:
486 return false;
487 }
488}
489
Alex Elder33803f32013-03-13 20:50:00 -0500490/*
491 * This is an osd op init function for opcodes that have no data or
492 * other information associated with them. It also serves as a
493 * common init routine for all the other init functions, below.
494 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500495static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600496_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800497 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500498{
Alex Elderc99d2d42013-04-05 01:27:11 -0500499 struct ceph_osd_req_op *op;
500
501 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500502 BUG_ON(!osd_req_opcode_valid(opcode));
503
Alex Elderc99d2d42013-04-05 01:27:11 -0500504 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500505 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500506 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800507 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500508
509 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500510}
511
Alex Elder49719772013-02-11 12:33:24 -0600512void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800513 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600514{
Yan, Zheng144cba12015-04-27 11:09:54 +0800515 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600516}
517EXPORT_SYMBOL(osd_req_op_init);
518
Alex Elderc99d2d42013-04-05 01:27:11 -0500519void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
520 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500521 u64 offset, u64 length,
522 u64 truncate_size, u32 truncate_seq)
523{
Yan, Zheng144cba12015-04-27 11:09:54 +0800524 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
525 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500526 size_t payload_len = 0;
527
Li Wangad7a60d2013-08-15 11:51:44 +0800528 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200529 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
530 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500531
Alex Elder33803f32013-03-13 20:50:00 -0500532 op->extent.offset = offset;
533 op->extent.length = length;
534 op->extent.truncate_size = truncate_size;
535 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200536 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500537 payload_len += length;
538
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100539 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500540}
541EXPORT_SYMBOL(osd_req_op_extent_init);
542
Alex Elderc99d2d42013-04-05 01:27:11 -0500543void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
544 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500545{
Alex Elderc99d2d42013-04-05 01:27:11 -0500546 struct ceph_osd_req_op *op;
547 u64 previous;
548
549 BUG_ON(which >= osd_req->r_num_ops);
550 op = &osd_req->r_ops[which];
551 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500552
553 if (length == previous)
554 return; /* Nothing to do */
555 BUG_ON(length > previous);
556
557 op->extent.length = length;
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100558 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500559}
560EXPORT_SYMBOL(osd_req_op_extent_update);
561
Yan, Zheng2c63f492016-01-07 17:32:54 +0800562void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
563 unsigned int which, u64 offset_inc)
564{
565 struct ceph_osd_req_op *op, *prev_op;
566
567 BUG_ON(which + 1 >= osd_req->r_num_ops);
568
569 prev_op = &osd_req->r_ops[which];
570 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
571 /* dup previous one */
572 op->indata_len = prev_op->indata_len;
573 op->outdata_len = prev_op->outdata_len;
574 op->extent = prev_op->extent;
575 /* adjust offset */
576 op->extent.offset += offset_inc;
577 op->extent.length -= offset_inc;
578
579 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
580 op->indata_len -= offset_inc;
581}
582EXPORT_SYMBOL(osd_req_op_extent_dup_last);
583
Alex Elderc99d2d42013-04-05 01:27:11 -0500584void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500585 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500586{
Yan, Zheng144cba12015-04-27 11:09:54 +0800587 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
588 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500589 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500590 size_t payload_len = 0;
591 size_t size;
592
593 BUG_ON(opcode != CEPH_OSD_OP_CALL);
594
Alex Elder5f562df2013-04-05 01:27:12 -0500595 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
596 BUG_ON(!pagelist);
597 ceph_pagelist_init(pagelist);
598
Alex Elder33803f32013-03-13 20:50:00 -0500599 op->cls.class_name = class;
600 size = strlen(class);
601 BUG_ON(size > (size_t) U8_MAX);
602 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500603 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500604 payload_len += size;
605
606 op->cls.method_name = method;
607 size = strlen(method);
608 BUG_ON(size > (size_t) U8_MAX);
609 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500610 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500611 payload_len += size;
612
Alex Eldera4ce40a2013-04-05 01:27:12 -0500613 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500614
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
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200712static u32 osd_req_encode_op(struct ceph_osd_op *dst,
713 const struct ceph_osd_req_op *src)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700714{
Alex Eldera8dd0a32013-03-13 20:50:00 -0500715 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
716 pr_err("unrecognized osd opcode %d\n", src->op);
717
718 return 0;
719 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700720
Alex Elder065a68f2012-04-20 15:49:43 -0500721 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600722 case CEPH_OSD_OP_STAT:
723 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700724 case CEPH_OSD_OP_READ:
725 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200726 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800727 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800728 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600729 dst->extent.offset = cpu_to_le64(src->extent.offset);
730 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700731 dst->extent.truncate_size =
732 cpu_to_le64(src->extent.truncate_size);
733 dst->extent.truncate_seq =
734 cpu_to_le32(src->extent.truncate_seq);
735 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700736 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700737 dst->cls.class_len = src->cls.class_len;
738 dst->cls.method_len = src->cls.method_len;
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200739 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700740 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700741 case CEPH_OSD_OP_STARTSYNC:
742 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700743 case CEPH_OSD_OP_NOTIFY_ACK:
744 case CEPH_OSD_OP_WATCH:
745 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
746 dst->watch.ver = cpu_to_le64(src->watch.ver);
747 dst->watch.flag = src->watch.flag;
748 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200749 case CEPH_OSD_OP_SETALLOCHINT:
750 dst->alloc_hint.expected_object_size =
751 cpu_to_le64(src->alloc_hint.expected_object_size);
752 dst->alloc_hint.expected_write_size =
753 cpu_to_le64(src->alloc_hint.expected_write_size);
754 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800755 case CEPH_OSD_OP_SETXATTR:
756 case CEPH_OSD_OP_CMPXATTR:
757 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
758 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
759 dst->xattr.cmp_op = src->xattr.cmp_op;
760 dst->xattr.cmp_mode = src->xattr.cmp_mode;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800761 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800762 case CEPH_OSD_OP_CREATE:
763 case CEPH_OSD_OP_DELETE:
764 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700765 default:
Alex Elder4c464592013-02-15 11:42:30 -0600766 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600767 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600768 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500769
770 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700771 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200772
Alex Eldera8dd0a32013-03-13 20:50:00 -0500773 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200774 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100775 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600776
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200777 return src->indata_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700778}
779
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700780/*
Sage Weilf24e9982009-10-06 11:31:10 -0700781 * build new request AND message, calculate layout, and adjust file
782 * extent as needed.
783 *
784 * if the file was recently truncated, we include information about its
785 * old and new size so that the object can be updated appropriately. (we
786 * avoid synchronously deleting truncated objects because it's slow.)
787 *
788 * if @do_sync, include a 'startsync' command so that the osd will flush
789 * data quickly.
790 */
791struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
792 struct ceph_file_layout *layout,
793 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800794 u64 off, u64 *plen,
795 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700796 int opcode, int flags,
797 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700798 u32 truncate_seq,
799 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600800 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700801{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700802 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500803 u64 objnum = 0;
804 u64 objoff = 0;
805 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700806 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700807
Li Wangad7a60d2013-08-15 11:51:44 +0800808 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800809 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
810 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700811
Alex Elderacead002013-03-14 14:09:05 -0500812 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600813 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200814 if (!req) {
815 r = -ENOMEM;
816 goto fail;
817 }
Alex Elder79528732013-04-03 21:32:51 -0500818
Sage Weilf24e9982009-10-06 11:31:10 -0700819 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500820 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200821 if (r)
822 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -0500823
Yan, Zheng864e9192014-11-13 10:47:25 +0800824 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800825 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800826 } else {
827 u32 object_size = le32_to_cpu(layout->fl_object_size);
828 u32 object_base = off - objoff;
829 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
830 if (truncate_size <= object_base) {
831 truncate_size = 0;
832 } else {
833 truncate_size -= object_base;
834 if (truncate_size > object_size)
835 truncate_size = object_size;
836 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800837 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800838 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800839 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500840 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500841
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200842 req->r_flags = flags;
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200843 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200844 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600845
Ilya Dryomovbb873b52016-05-26 00:29:52 +0200846 req->r_snapid = vino.snap;
847 if (flags & CEPH_OSD_FLAG_WRITE)
848 req->r_data_offset = off;
849
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200850 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
851 if (r)
852 goto fail;
853
Sage Weilf24e9982009-10-06 11:31:10 -0700854 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200855
856fail:
857 ceph_osdc_put_request(req);
858 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700859}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700860EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700861
862/*
863 * We keep osd requests in an rbtree, sorted by ->r_tid.
864 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200865DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Sage Weilf24e9982009-10-06 11:31:10 -0700866
867static struct ceph_osd_request *
868__lookup_request_ge(struct ceph_osd_client *osdc,
869 u64 tid)
870{
871 struct ceph_osd_request *req;
872 struct rb_node *n = osdc->requests.rb_node;
873
874 while (n) {
875 req = rb_entry(n, struct ceph_osd_request, r_node);
876 if (tid < req->r_tid) {
877 if (!n->rb_left)
878 return req;
879 n = n->rb_left;
880 } else if (tid > req->r_tid) {
881 n = n->rb_right;
882 } else {
883 return req;
884 }
885 }
886 return NULL;
887}
888
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400889static void __kick_linger_request(struct ceph_osd_request *req)
890{
891 struct ceph_osd_client *osdc = req->r_osdc;
892 struct ceph_osd *osd = req->r_osd;
893
894 /*
895 * Linger requests need to be resent with a new tid to avoid
896 * the dup op detection logic on the OSDs. Achieve this with
897 * a re-register dance instead of open-coding.
898 */
899 ceph_osdc_get_request(req);
900 if (!list_empty(&req->r_linger_item))
901 __unregister_linger_request(osdc, req);
902 else
903 __unregister_request(osdc, req);
904 __register_request(osdc, req);
905 ceph_osdc_put_request(req);
906
907 /*
908 * Unless request has been registered as both normal and
909 * lingering, __unregister{,_linger}_request clears r_osd.
910 * However, here we need to preserve r_osd to make sure we
911 * requeue on the same OSD.
912 */
913 WARN_ON(req->r_osd || !osd);
914 req->r_osd = osd;
915
916 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
917 __enqueue_request(req);
918}
919
Sage Weil6f6c7002011-01-17 20:34:08 -0800920/*
921 * Resubmit requests pending on the given osd.
922 */
923static void __kick_osd_requests(struct ceph_osd_client *osdc,
924 struct ceph_osd *osd)
925{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700926 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500927 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400928 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800929 int err;
930
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400931 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800932 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600933 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800934 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400935
Alex Eldere02493c2013-03-25 18:16:11 -0500936 /*
937 * Build up a list of requests to resend by traversing the
938 * osd's list of requests. Requests for a given object are
939 * sent in tid order, and that is also the order they're
940 * kept on this list. Therefore all requests that are in
941 * flight will be found first, followed by all requests that
942 * have not yet been sent. And to resend requests while
943 * preserving this order we will want to put any sent
944 * requests back on the front of the osd client's unsent
945 * list.
946 *
947 * So we build a separate ordered list of already-sent
948 * requests for the affected osd and splice it onto the
949 * front of the osd client's unsent list. Once we've seen a
950 * request that has not yet been sent we're done. Those
951 * requests are already sitting right where they belong.
952 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800953 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500954 if (!req->r_sent)
955 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400956
957 if (!req->r_linger) {
958 dout("%s requeueing %p tid %llu\n", __func__, req,
959 req->r_tid);
960 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700961 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400962 } else {
963 list_move_tail(&req->r_req_lru_item, &resend_linger);
964 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700965 }
Alex Eldere02493c2013-03-25 18:16:11 -0500966 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700967
Alex Eldere02493c2013-03-25 18:16:11 -0500968 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400969 * Both registered and not yet registered linger requests are
970 * enqueued with a new tid on the same OSD. We add/move them
971 * to req_unsent/o_requests at the end to keep things in tid
972 * order.
Alex Eldere02493c2013-03-25 18:16:11 -0500973 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700974 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400975 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400976 WARN_ON(!list_empty(&req->r_req_lru_item));
977 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800978 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400979
980 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
981 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800982}
983
Sage Weilf24e9982009-10-06 11:31:10 -0700984/*
Sage Weil81b024e2009-10-09 10:29:18 -0700985 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700986 */
987static void osd_reset(struct ceph_connection *con)
988{
989 struct ceph_osd *osd = con->private;
990 struct ceph_osd_client *osdc;
991
992 if (!osd)
993 return;
994 dout("osd_reset osd%d\n", osd->o_osd);
995 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700996 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800997 mutex_lock(&osdc->request_mutex);
998 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600999 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001000 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001001 up_read(&osdc->map_sem);
1002}
1003
1004/*
1005 * Track open sessions with osds.
1006 */
Alex Eldere10006f2012-05-26 23:26:43 -05001007static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001008{
1009 struct ceph_osd *osd;
1010
1011 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1012 if (!osd)
1013 return NULL;
1014
1015 atomic_set(&osd->o_ref, 1);
1016 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001017 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001018 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001019 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001020 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001021 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001022 osd->o_incarnation = 1;
1023
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001024 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001025
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001026 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001027 return osd;
1028}
1029
1030static struct ceph_osd *get_osd(struct ceph_osd *osd)
1031{
1032 if (atomic_inc_not_zero(&osd->o_ref)) {
1033 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1034 atomic_read(&osd->o_ref));
1035 return osd;
1036 } else {
1037 dout("get_osd %p FAIL\n", osd);
1038 return NULL;
1039 }
1040}
1041
1042static void put_osd(struct ceph_osd *osd)
1043{
1044 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1045 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001046 if (atomic_dec_and_test(&osd->o_ref)) {
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001047 if (osd->o_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02001048 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001049 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001050 }
Sage Weilf24e9982009-10-06 11:31:10 -07001051}
1052
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001053DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1054
Sage Weilf24e9982009-10-06 11:31:10 -07001055/*
1056 * remove an osd from our map
1057 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001058static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001059{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001060 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001061 WARN_ON(!list_empty(&osd->o_requests));
1062 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001063
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001064 list_del_init(&osd->o_osd_lru);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001065 erase_osd(&osdc->osds, osd);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001066}
1067
1068static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1069{
1070 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1071
1072 if (!RB_EMPTY_NODE(&osd->o_node)) {
1073 ceph_con_close(&osd->o_con);
1074 __remove_osd(osdc, osd);
1075 put_osd(osd);
1076 }
Sage Weilf24e9982009-10-06 11:31:10 -07001077}
1078
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001079static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1080 struct ceph_osd *osd)
1081{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001082 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001083 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001084
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001085 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001086 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001087}
1088
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001089static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1090 struct ceph_osd *osd)
1091{
1092 dout("%s %p\n", __func__, osd);
1093
1094 if (list_empty(&osd->o_requests) &&
1095 list_empty(&osd->o_linger_requests))
1096 __move_osd_to_lru(osdc, osd);
1097}
1098
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001099static void __remove_osd_from_lru(struct ceph_osd *osd)
1100{
1101 dout("__remove_osd_from_lru %p\n", osd);
1102 if (!list_empty(&osd->o_osd_lru))
1103 list_del_init(&osd->o_osd_lru);
1104}
1105
Sage Weilf24e9982009-10-06 11:31:10 -07001106/*
1107 * reset osd connect
1108 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001109static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001110{
Alex Elderc3acb182012-12-07 09:57:58 -06001111 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001112
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001113 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001114 if (list_empty(&osd->o_requests) &&
1115 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001116 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001117 return -ENODEV;
1118 }
1119
1120 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1121 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1122 !ceph_con_opened(&osd->o_con)) {
1123 struct ceph_osd_request *req;
1124
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001125 dout("osd addr hasn't changed and connection never opened, "
1126 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001127 /* touch each r_stamp for handle_timeout()'s benfit */
1128 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1129 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001130
1131 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001132 }
Alex Elderc3acb182012-12-07 09:57:58 -06001133
1134 ceph_con_close(&osd->o_con);
1135 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1136 osd->o_incarnation++;
1137
1138 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001139}
1140
Sage Weilf24e9982009-10-06 11:31:10 -07001141/*
1142 * Register request, assign tid. If this is the first request, set up
1143 * the timeout event.
1144 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001145static void __register_request(struct ceph_osd_client *osdc,
1146 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001147{
Sage Weilf24e9982009-10-06 11:31:10 -07001148 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001149 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001150 dout("__register_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001151 insert_request(&osdc->requests, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001152 ceph_osdc_get_request(req);
1153 osdc->num_requests++;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001154}
1155
Sage Weilf24e9982009-10-06 11:31:10 -07001156/*
1157 * called under osdc->request_mutex
1158 */
1159static void __unregister_request(struct ceph_osd_client *osdc,
1160 struct ceph_osd_request *req)
1161{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001162 if (RB_EMPTY_NODE(&req->r_node)) {
1163 dout("__unregister_request %p tid %lld not registered\n",
1164 req, req->r_tid);
1165 return;
1166 }
1167
Sage Weilf24e9982009-10-06 11:31:10 -07001168 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001169 erase_request(&osdc->requests, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001170 osdc->num_requests--;
1171
Sage Weil0ba64782009-10-08 16:57:16 -07001172 if (req->r_osd) {
1173 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001174 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001175
1176 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001177 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001178 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001179 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001180 }
Sage Weilf24e9982009-10-06 11:31:10 -07001181
Alex Elder7d5f2482012-11-29 08:37:03 -06001182 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001183 ceph_osdc_put_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001184}
1185
1186/*
1187 * Cancel a previously queued request message
1188 */
1189static void __cancel_request(struct ceph_osd_request *req)
1190{
Sage Weil6bc18872010-09-27 10:18:52 -07001191 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001192 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001193 req->r_sent = 0;
1194 }
1195}
1196
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001197static void __register_linger_request(struct ceph_osd_client *osdc,
1198 struct ceph_osd_request *req)
1199{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001200 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1201 WARN_ON(!req->r_linger);
1202
Alex Elder96e4dac2013-05-22 20:54:25 -05001203 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001204 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001205 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001206 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001207 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001208}
1209
1210static void __unregister_linger_request(struct ceph_osd_client *osdc,
1211 struct ceph_osd_request *req)
1212{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001213 WARN_ON(!req->r_linger);
1214
1215 if (list_empty(&req->r_linger_item)) {
1216 dout("%s %p tid %llu not registered\n", __func__, req,
1217 req->r_tid);
1218 return;
1219 }
1220
1221 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001222 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001223
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001224 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001225 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001226 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001227 if (list_empty(&req->r_osd_item))
1228 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001229 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001230 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001231}
1232
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001233void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1234 struct ceph_osd_request *req)
1235{
1236 if (!req->r_linger) {
1237 dout("set_request_linger %p\n", req);
1238 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001239 }
1240}
1241EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1242
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001243static bool __pool_full(struct ceph_pg_pool_info *pi)
1244{
1245 return pi->flags & CEPH_POOL_FLAG_FULL;
1246}
1247
Sage Weilf24e9982009-10-06 11:31:10 -07001248/*
Josh Durgind29adb32013-12-02 19:11:48 -08001249 * Returns whether a request should be blocked from being sent
1250 * based on the current osdmap and osd_client settings.
1251 *
1252 * Caller should hold map_sem for read.
1253 */
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001254static bool target_should_be_paused(struct ceph_osd_client *osdc,
1255 const struct ceph_osd_request_target *t,
1256 struct ceph_pg_pool_info *pi)
1257{
1258 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1259 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1260 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
1261 __pool_full(pi);
1262
1263 WARN_ON(pi->id != t->base_oloc.pool);
1264 return (t->flags & CEPH_OSD_FLAG_READ && pauserd) ||
1265 (t->flags & CEPH_OSD_FLAG_WRITE && pausewr);
1266}
1267
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001268enum calc_target_result {
1269 CALC_TARGET_NO_ACTION = 0,
1270 CALC_TARGET_NEED_RESEND,
1271 CALC_TARGET_POOL_DNE,
1272};
1273
1274static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1275 struct ceph_osd_request_target *t,
1276 u32 *last_force_resend,
1277 bool any_change)
1278{
1279 struct ceph_pg_pool_info *pi;
1280 struct ceph_pg pgid, last_pgid;
1281 struct ceph_osds up, acting;
1282 bool force_resend = false;
1283 bool need_check_tiering = false;
1284 bool need_resend = false;
1285 bool sort_bitwise = ceph_osdmap_flag(osdc->osdmap,
1286 CEPH_OSDMAP_SORTBITWISE);
1287 enum calc_target_result ct_res;
1288 int ret;
1289
1290 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1291 if (!pi) {
1292 t->osd = CEPH_HOMELESS_OSD;
1293 ct_res = CALC_TARGET_POOL_DNE;
1294 goto out;
1295 }
1296
1297 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1298 if (last_force_resend &&
1299 *last_force_resend < pi->last_force_request_resend) {
1300 *last_force_resend = pi->last_force_request_resend;
1301 force_resend = true;
1302 } else if (!last_force_resend) {
1303 force_resend = true;
1304 }
1305 }
1306 if (ceph_oid_empty(&t->target_oid) || force_resend) {
1307 ceph_oid_copy(&t->target_oid, &t->base_oid);
1308 need_check_tiering = true;
1309 }
1310 if (ceph_oloc_empty(&t->target_oloc) || force_resend) {
1311 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1312 need_check_tiering = true;
1313 }
1314
1315 if (need_check_tiering &&
1316 (t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1317 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1318 t->target_oloc.pool = pi->read_tier;
1319 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1320 t->target_oloc.pool = pi->write_tier;
1321 }
1322
1323 ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1324 &t->target_oloc, &pgid);
1325 if (ret) {
1326 WARN_ON(ret != -ENOENT);
1327 t->osd = CEPH_HOMELESS_OSD;
1328 ct_res = CALC_TARGET_POOL_DNE;
1329 goto out;
1330 }
1331 last_pgid.pool = pgid.pool;
1332 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1333
1334 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1335 if (any_change &&
1336 ceph_is_new_interval(&t->acting,
1337 &acting,
1338 &t->up,
1339 &up,
1340 t->size,
1341 pi->size,
1342 t->min_size,
1343 pi->min_size,
1344 t->pg_num,
1345 pi->pg_num,
1346 t->sort_bitwise,
1347 sort_bitwise,
1348 &last_pgid))
1349 force_resend = true;
1350
1351 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1352 t->paused = false;
1353 need_resend = true;
1354 }
1355
1356 if (ceph_pg_compare(&t->pgid, &pgid) ||
1357 ceph_osds_changed(&t->acting, &acting, any_change) ||
1358 force_resend) {
1359 t->pgid = pgid; /* struct */
1360 ceph_osds_copy(&t->acting, &acting);
1361 ceph_osds_copy(&t->up, &up);
1362 t->size = pi->size;
1363 t->min_size = pi->min_size;
1364 t->pg_num = pi->pg_num;
1365 t->pg_num_mask = pi->pg_num_mask;
1366 t->sort_bitwise = sort_bitwise;
1367
1368 t->osd = acting.primary;
1369 need_resend = true;
1370 }
1371
1372 ct_res = need_resend ? CALC_TARGET_NEED_RESEND : CALC_TARGET_NO_ACTION;
1373out:
1374 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1375 return ct_res;
1376}
1377
Ilya Dryomovf671b582014-09-02 13:40:33 +04001378static void __enqueue_request(struct ceph_osd_request *req)
1379{
1380 struct ceph_osd_client *osdc = req->r_osdc;
1381
1382 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1383 req->r_osd ? req->r_osd->o_osd : -1);
1384
1385 if (req->r_osd) {
1386 __remove_osd_from_lru(req->r_osd);
1387 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1388 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1389 } else {
1390 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1391 }
1392}
1393
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001394/*
Sage Weilf24e9982009-10-06 11:31:10 -07001395 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1396 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001397 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001398 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001399 *
1400 * Return 0 if unchanged, 1 if changed, or negative on error.
1401 *
1402 * Caller should hold map_sem for read and request_mutex.
1403 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001404static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001405 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001406{
Ilya Dryomova66dd382016-04-28 16:07:23 +02001407 enum calc_target_result ct_res;
Sage Weilf24e9982009-10-06 11:31:10 -07001408 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001409
Sage Weil6f6c7002011-01-17 20:34:08 -08001410 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001411
Ilya Dryomova66dd382016-04-28 16:07:23 +02001412 ct_res = calc_target(osdc, &req->r_t, NULL, force_resend);
1413 switch (ct_res) {
1414 case CALC_TARGET_POOL_DNE:
Sage Weil6f6c7002011-01-17 20:34:08 -08001415 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Ilya Dryomova66dd382016-04-28 16:07:23 +02001416 return -EIO;
1417 case CALC_TARGET_NO_ACTION:
Sage Weilf24e9982009-10-06 11:31:10 -07001418 return 0; /* no change */
Ilya Dryomova66dd382016-04-28 16:07:23 +02001419 default:
1420 BUG_ON(ct_res != CALC_TARGET_NEED_RESEND);
1421 }
Sage Weilf24e9982009-10-06 11:31:10 -07001422
Sage Weil5b191d92013-02-23 10:38:16 -08001423 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
Ilya Dryomova66dd382016-04-28 16:07:23 +02001424 req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed, req->r_t.osd,
Sage Weilf24e9982009-10-06 11:31:10 -07001425 req->r_osd ? req->r_osd->o_osd : -1);
1426
1427 if (req->r_osd) {
1428 __cancel_request(req);
1429 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001430 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001431 req->r_osd = NULL;
1432 }
1433
Ilya Dryomova66dd382016-04-28 16:07:23 +02001434 req->r_osd = lookup_osd(&osdc->osds, req->r_t.osd);
1435 if (!req->r_osd && req->r_t.osd >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001436 err = -ENOMEM;
Ilya Dryomova66dd382016-04-28 16:07:23 +02001437 req->r_osd = create_osd(osdc, req->r_t.osd);
Sage Weil6f6c7002011-01-17 20:34:08 -08001438 if (!req->r_osd) {
1439 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001440 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001441 }
Sage Weilf24e9982009-10-06 11:31:10 -07001442
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001443 dout("map_request osd %p is osd%d\n", req->r_osd,
Ilya Dryomova66dd382016-04-28 16:07:23 +02001444 req->r_osd->o_osd);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001445 insert_osd(&osdc->osds, req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001446
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001447 ceph_con_open(&req->r_osd->o_con,
Ilya Dryomova66dd382016-04-28 16:07:23 +02001448 CEPH_ENTITY_TYPE_OSD, req->r_osd->o_osd,
1449 &osdc->osdmap->osd_addr[req->r_osd->o_osd]);
Sage Weilf24e9982009-10-06 11:31:10 -07001450 }
1451
Ilya Dryomovf671b582014-09-02 13:40:33 +04001452 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001453 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001454
1455out:
Sage Weilf24e9982009-10-06 11:31:10 -07001456 return err;
1457}
1458
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001459static void setup_request_data(struct ceph_osd_request *req,
1460 struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001461{
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001462 u32 data_len = 0;
1463 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001464
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001465 if (!list_empty(&msg->data))
1466 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001467
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001468 WARN_ON(msg->data_length);
1469 for (i = 0; i < req->r_num_ops; i++) {
1470 struct ceph_osd_req_op *op = &req->r_ops[i];
1471
1472 switch (op->op) {
1473 /* request */
1474 case CEPH_OSD_OP_WRITE:
1475 case CEPH_OSD_OP_WRITEFULL:
1476 WARN_ON(op->indata_len != op->extent.length);
1477 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1478 break;
1479 case CEPH_OSD_OP_SETXATTR:
1480 case CEPH_OSD_OP_CMPXATTR:
1481 WARN_ON(op->indata_len != op->xattr.name_len +
1482 op->xattr.value_len);
1483 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1484 break;
1485
1486 /* reply */
1487 case CEPH_OSD_OP_STAT:
1488 ceph_osdc_msg_data_add(req->r_reply,
1489 &op->raw_data_in);
1490 break;
1491 case CEPH_OSD_OP_READ:
1492 ceph_osdc_msg_data_add(req->r_reply,
1493 &op->extent.osd_data);
1494 break;
1495
1496 /* both */
1497 case CEPH_OSD_OP_CALL:
1498 WARN_ON(op->indata_len != op->cls.class_len +
1499 op->cls.method_len +
1500 op->cls.indata_len);
1501 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1502 /* optional, can be NONE */
1503 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1504 /* optional, can be NONE */
1505 ceph_osdc_msg_data_add(req->r_reply,
1506 &op->cls.response_data);
1507 break;
1508 }
1509
1510 data_len += op->indata_len;
1511 }
1512
1513 WARN_ON(data_len != msg->data_length);
1514}
1515
1516static void encode_request(struct ceph_osd_request *req, struct ceph_msg *msg)
1517{
1518 void *p = msg->front.iov_base;
1519 void *const end = p + msg->front_alloc_len;
1520 u32 data_len = 0;
1521 int i;
1522
1523 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1524 /* snapshots aren't writeable */
1525 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1526 } else {
1527 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1528 req->r_data_offset || req->r_snapc);
1529 }
1530
1531 setup_request_data(req, msg);
1532
1533 ceph_encode_32(&p, 1); /* client_inc, always 1 */
1534 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1535 ceph_encode_32(&p, req->r_flags);
1536 ceph_encode_timespec(p, &req->r_mtime);
1537 p += sizeof(struct ceph_timespec);
1538 /* aka reassert_version */
1539 memcpy(p, &req->r_replay_version, sizeof(req->r_replay_version));
1540 p += sizeof(req->r_replay_version);
1541
1542 /* oloc */
1543 ceph_encode_8(&p, 4);
1544 ceph_encode_8(&p, 4);
1545 ceph_encode_32(&p, 8 + 4 + 4);
1546 ceph_encode_64(&p, req->r_t.target_oloc.pool);
1547 ceph_encode_32(&p, -1); /* preferred */
1548 ceph_encode_32(&p, 0); /* key len */
1549
1550 /* pgid */
1551 ceph_encode_8(&p, 1);
Ilya Dryomova66dd382016-04-28 16:07:23 +02001552 ceph_encode_64(&p, req->r_t.pgid.pool);
1553 ceph_encode_32(&p, req->r_t.pgid.seed);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001554 ceph_encode_32(&p, -1); /* preferred */
Sage Weil2169aea2013-02-25 16:13:08 -08001555
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001556 /* oid */
1557 ceph_encode_32(&p, req->r_t.target_oid.name_len);
1558 memcpy(p, req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1559 p += req->r_t.target_oid.name_len;
1560
1561 /* ops, can imply data */
1562 ceph_encode_16(&p, req->r_num_ops);
1563 for (i = 0; i < req->r_num_ops; i++) {
1564 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1565 p += sizeof(struct ceph_osd_op);
1566 }
1567
1568 ceph_encode_64(&p, req->r_snapid); /* snapid */
1569 if (req->r_snapc) {
1570 ceph_encode_64(&p, req->r_snapc->seq);
1571 ceph_encode_32(&p, req->r_snapc->num_snaps);
1572 for (i = 0; i < req->r_snapc->num_snaps; i++)
1573 ceph_encode_64(&p, req->r_snapc->snaps[i]);
1574 } else {
1575 ceph_encode_64(&p, 0); /* snap_seq */
1576 ceph_encode_32(&p, 0); /* snaps len */
1577 }
1578
1579 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1580
1581 BUG_ON(p > end);
1582 msg->front.iov_len = p - msg->front.iov_base;
1583 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1584 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1585 msg->hdr.data_len = cpu_to_le32(data_len);
1586 /*
1587 * The header "data_off" is a hint to the receiver allowing it
1588 * to align received data into its buffers such that there's no
1589 * need to re-copy it before writing it to disk (direct I/O).
1590 */
1591 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1592
1593 dout("%s req %p oid %*pE oid_len %d front %zu data %u\n", __func__,
1594 req, req->r_t.target_oid.name_len, req->r_t.target_oid.name,
1595 req->r_t.target_oid.name_len, msg->front.iov_len, data_len);
1596}
1597
1598/*
1599 * @req has to be assigned a tid and registered.
1600 */
1601static void send_request(struct ceph_osd_request *req)
1602{
1603 struct ceph_osd *osd = req->r_osd;
1604
1605 WARN_ON(osd->o_osd != req->r_t.osd);
1606
1607 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1608 if (req->r_attempts)
1609 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1610 else
1611 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1612
1613 encode_request(req, req->r_request);
1614
1615 dout("%s req %p tid %llu to pg %llu.%x osd%d flags 0x%x attempt %d\n",
1616 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
1617 req->r_t.osd, req->r_flags, req->r_attempts);
1618
1619 req->r_t.paused = false;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001620 req->r_stamp = jiffies;
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001621 req->r_attempts++;
Sage Weilf24e9982009-10-06 11:31:10 -07001622
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001623 req->r_sent = osd->o_incarnation;
1624 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1625 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
Sage Weilf24e9982009-10-06 11:31:10 -07001626}
1627
1628/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001629 * Send any requests in the queue (req_unsent).
1630 */
Alex Elderf9d25192013-02-15 11:42:29 -06001631static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001632{
1633 struct ceph_osd_request *req, *tmp;
1634
Alex Elderf9d25192013-02-15 11:42:29 -06001635 dout("__send_queued\n");
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001636 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1637 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1638 send_request(req);
1639 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001640}
1641
1642/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001643 * Caller should hold map_sem for read and request_mutex.
1644 */
1645static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1646 struct ceph_osd_request *req,
1647 bool nofail)
1648{
1649 int rc;
1650
1651 __register_request(osdc, req);
1652 req->r_sent = 0;
1653 req->r_got_reply = 0;
1654 rc = __map_request(osdc, req, 0);
1655 if (rc < 0) {
1656 if (nofail) {
1657 dout("osdc_start_request failed map, "
1658 " will retry %lld\n", req->r_tid);
1659 rc = 0;
1660 } else {
1661 __unregister_request(osdc, req);
1662 }
1663 return rc;
1664 }
1665
1666 if (req->r_osd == NULL) {
1667 dout("send_request %p no up osds in pg\n", req);
1668 ceph_monc_request_next_osdmap(&osdc->client->monc);
1669 } else {
1670 __send_queued(osdc);
1671 }
1672
1673 return 0;
1674}
1675
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001676static void __complete_request(struct ceph_osd_request *req)
1677{
1678 if (req->r_callback)
1679 req->r_callback(req);
1680 else
1681 complete_all(&req->r_completion);
1682}
1683
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001684/*
Ilya Dryomovfbca9632016-04-28 16:07:24 +02001685 * Timeout callback, called every N seconds. When 1 or more OSD
1686 * requests has been active for more than N seconds, we send a keepalive
1687 * (tag + timestamp) to its OSD to ensure any communications channel
1688 * reset is detected.
Sage Weilf24e9982009-10-06 11:31:10 -07001689 */
1690static void handle_timeout(struct work_struct *work)
1691{
1692 struct ceph_osd_client *osdc =
1693 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001694 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001695 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001696 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001697 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001698 dout("timeout\n");
1699 down_read(&osdc->map_sem);
1700
1701 ceph_monc_request_next_osdmap(&osdc->client->monc);
1702
1703 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001704
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001705 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001706 * ping osds that are a bit slow. this ensures that if there
1707 * is a break in the TCP connection we will notice, and reopen
1708 * a connection with that osd (from the fault callback).
1709 */
1710 INIT_LIST_HEAD(&slow_osds);
1711 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001712 if (time_before(jiffies,
1713 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001714 break;
1715
1716 osd = req->r_osd;
1717 BUG_ON(!osd);
1718 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001719 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001720 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1721 }
1722 while (!list_empty(&slow_osds)) {
1723 osd = list_entry(slow_osds.next, struct ceph_osd,
1724 o_keepalive_item);
1725 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001726 ceph_con_keepalive(&osd->o_con);
1727 }
1728
Alex Elderf9d25192013-02-15 11:42:29 -06001729 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001730 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001731 up_read(&osdc->map_sem);
Ilya Dryomovfbca9632016-04-28 16:07:24 +02001732
1733 schedule_delayed_work(&osdc->timeout_work,
1734 osdc->client->options->osd_keepalive_timeout);
Sage Weilf24e9982009-10-06 11:31:10 -07001735}
1736
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001737static void handle_osds_timeout(struct work_struct *work)
1738{
1739 struct ceph_osd_client *osdc =
1740 container_of(work, struct ceph_osd_client,
1741 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001742 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001743 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001744
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001745 dout("%s osdc %p\n", __func__, osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001746 down_read(&osdc->map_sem);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001747 mutex_lock(&osdc->request_mutex);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001748
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001749 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1750 if (time_before(jiffies, osd->lru_ttl))
1751 break;
1752
1753 remove_osd(osdc, osd);
1754 }
1755
1756 mutex_unlock(&osdc->request_mutex);
1757 up_read(&osdc->map_sem);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001758 schedule_delayed_work(&osdc->osds_timeout_work,
1759 round_jiffies_relative(delay));
1760}
1761
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001762static int ceph_oloc_decode(void **p, void *end,
1763 struct ceph_object_locator *oloc)
1764{
1765 u8 struct_v, struct_cv;
1766 u32 len;
1767 void *struct_end;
1768 int ret = 0;
1769
1770 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1771 struct_v = ceph_decode_8(p);
1772 struct_cv = ceph_decode_8(p);
1773 if (struct_v < 3) {
1774 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1775 struct_v, struct_cv);
1776 goto e_inval;
1777 }
1778 if (struct_cv > 6) {
1779 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1780 struct_v, struct_cv);
1781 goto e_inval;
1782 }
1783 len = ceph_decode_32(p);
1784 ceph_decode_need(p, end, len, e_inval);
1785 struct_end = *p + len;
1786
1787 oloc->pool = ceph_decode_64(p);
1788 *p += 4; /* skip preferred */
1789
1790 len = ceph_decode_32(p);
1791 if (len > 0) {
1792 pr_warn("ceph_object_locator::key is set\n");
1793 goto e_inval;
1794 }
1795
1796 if (struct_v >= 5) {
1797 len = ceph_decode_32(p);
1798 if (len > 0) {
1799 pr_warn("ceph_object_locator::nspace is set\n");
1800 goto e_inval;
1801 }
1802 }
1803
1804 if (struct_v >= 6) {
1805 s64 hash = ceph_decode_64(p);
1806 if (hash != -1) {
1807 pr_warn("ceph_object_locator::hash is set\n");
1808 goto e_inval;
1809 }
1810 }
1811
1812 /* skip the rest */
1813 *p = struct_end;
1814out:
1815 return ret;
1816
1817e_inval:
1818 ret = -EINVAL;
1819 goto out;
1820}
1821
1822static int ceph_redirect_decode(void **p, void *end,
1823 struct ceph_request_redirect *redir)
1824{
1825 u8 struct_v, struct_cv;
1826 u32 len;
1827 void *struct_end;
1828 int ret;
1829
1830 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1831 struct_v = ceph_decode_8(p);
1832 struct_cv = ceph_decode_8(p);
1833 if (struct_cv > 1) {
1834 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1835 struct_v, struct_cv);
1836 goto e_inval;
1837 }
1838 len = ceph_decode_32(p);
1839 ceph_decode_need(p, end, len, e_inval);
1840 struct_end = *p + len;
1841
1842 ret = ceph_oloc_decode(p, end, &redir->oloc);
1843 if (ret)
1844 goto out;
1845
1846 len = ceph_decode_32(p);
1847 if (len > 0) {
1848 pr_warn("ceph_request_redirect::object_name is set\n");
1849 goto e_inval;
1850 }
1851
1852 len = ceph_decode_32(p);
1853 *p += len; /* skip osd_instructions */
1854
1855 /* skip the rest */
1856 *p = struct_end;
1857out:
1858 return ret;
1859
1860e_inval:
1861 ret = -EINVAL;
1862 goto out;
1863}
1864
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001865struct MOSDOpReply {
1866 struct ceph_pg pgid;
1867 u64 flags;
1868 int result;
1869 u32 epoch;
1870 int num_ops;
1871 u32 outdata_len[CEPH_OSD_MAX_OPS];
1872 s32 rval[CEPH_OSD_MAX_OPS];
1873 int retry_attempt;
1874 struct ceph_eversion replay_version;
1875 u64 user_version;
1876 struct ceph_request_redirect redirect;
1877};
Sage Weil25845472011-06-03 09:37:09 -07001878
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001879static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
Sage Weilf24e9982009-10-06 11:31:10 -07001880{
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001881 void *p = msg->front.iov_base;
1882 void *const end = p + msg->front.iov_len;
1883 u16 version = le16_to_cpu(msg->hdr.version);
1884 struct ceph_eversion bad_replay_version;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001885 u8 decode_redir;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001886 u32 len;
1887 int ret;
1888 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001889
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001890 ceph_decode_32_safe(&p, end, len, e_inval);
1891 ceph_decode_need(&p, end, len, e_inval);
1892 p += len; /* skip oid */
Sage Weil1b83bef2013-02-25 16:11:12 -08001893
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001894 ret = ceph_decode_pgid(&p, end, &m->pgid);
1895 if (ret)
1896 return ret;
Sage Weil1b83bef2013-02-25 16:11:12 -08001897
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001898 ceph_decode_64_safe(&p, end, m->flags, e_inval);
1899 ceph_decode_32_safe(&p, end, m->result, e_inval);
1900 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
1901 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
1902 p += sizeof(bad_replay_version);
1903 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
Sage Weil1b83bef2013-02-25 16:11:12 -08001904
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001905 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
1906 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
1907 goto e_inval;
Sage Weil1b83bef2013-02-25 16:11:12 -08001908
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001909 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
1910 e_inval);
1911 for (i = 0; i < m->num_ops; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001912 struct ceph_osd_op *op = p;
Sage Weil1b83bef2013-02-25 16:11:12 -08001913
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001914 m->outdata_len[i] = le32_to_cpu(op->payload_len);
Sage Weil1b83bef2013-02-25 16:11:12 -08001915 p += sizeof(*op);
1916 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001917
1918 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
1919 for (i = 0; i < m->num_ops; i++)
1920 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
1921
1922 if (version >= 5) {
1923 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
1924 memcpy(&m->replay_version, p, sizeof(m->replay_version));
1925 p += sizeof(m->replay_version);
1926 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
1927 } else {
1928 m->replay_version = bad_replay_version; /* struct */
1929 m->user_version = le64_to_cpu(m->replay_version.version);
Sage Weil1b83bef2013-02-25 16:11:12 -08001930 }
1931
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001932 if (version >= 6) {
1933 if (version >= 7)
1934 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001935 else
1936 decode_redir = 1;
1937 } else {
1938 decode_redir = 0;
1939 }
1940
1941 if (decode_redir) {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001942 ret = ceph_redirect_decode(&p, end, &m->redirect);
1943 if (ret)
1944 return ret;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001945 } else {
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001946 ceph_oloc_init(&m->redirect.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001947 }
1948
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001949 return 0;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001950
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001951e_inval:
1952 return -EINVAL;
1953}
1954
1955/*
1956 * We are done with @req if
1957 * - @m is a safe reply, or
1958 * - @m is an unsafe reply and we didn't want a safe one
1959 */
1960static bool done_request(const struct ceph_osd_request *req,
1961 const struct MOSDOpReply *m)
1962{
1963 return (m->result < 0 ||
1964 (m->flags & CEPH_OSD_FLAG_ONDISK) ||
1965 !(req->r_flags & CEPH_OSD_FLAG_ONDISK));
1966}
1967
1968/*
1969 * handle osd op reply. either call the callback if it is specified,
1970 * or do the completion to wake up the waiting thread.
1971 *
1972 * ->r_unsafe_callback is set? yes no
1973 *
1974 * first reply is OK (needed r_cb/r_completion, r_cb/r_completion,
1975 * any or needed/got safe) r_safe_completion r_safe_completion
1976 *
1977 * first reply is unsafe r_unsafe_cb(true) (nothing)
1978 *
1979 * when we get the safe reply r_unsafe_cb(false), r_cb/r_completion,
1980 * r_safe_completion r_safe_completion
1981 */
1982static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1983{
1984 struct ceph_osd_request *req;
1985 struct MOSDOpReply m;
1986 u64 tid = le64_to_cpu(msg->hdr.tid);
1987 u32 data_len = 0;
1988 bool already_acked;
1989 int ret;
1990 int i;
1991
1992 dout("%s msg %p tid %llu\n", __func__, msg, tid);
1993
1994 down_read(&osdc->map_sem);
1995 mutex_lock(&osdc->request_mutex);
1996 req = lookup_request(&osdc->requests, tid);
1997 if (!req) {
1998 dout("%s no tid %llu\n", __func__, tid);
1999 goto out_unlock;
2000 }
2001 ceph_osdc_get_request(req);
2002
2003 ret = decode_MOSDOpReply(msg, &m);
2004 if (ret) {
2005 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
2006 req->r_tid, ret);
2007 ceph_msg_dump(msg);
2008 goto fail_request;
2009 }
2010 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
2011 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
2012 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
2013 le64_to_cpu(m.replay_version.version), m.user_version);
2014
2015 if (m.retry_attempt >= 0) {
2016 if (m.retry_attempt != req->r_attempts - 1) {
2017 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
2018 req, req->r_tid, m.retry_attempt,
2019 req->r_attempts - 1);
2020 goto out_put;
2021 }
2022 } else {
2023 WARN_ON(1); /* MOSDOpReply v4 is assumed */
2024 }
2025
2026 if (!ceph_oloc_empty(&m.redirect.oloc)) {
2027 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
2028 m.redirect.oloc.pool);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002029 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002030
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002031 ceph_oloc_copy(&req->r_t.target_oloc, &m.redirect.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002032
2033 /*
2034 * Start redirect requests with nofail=true. If
2035 * mapping fails, request will end up on the notarget
2036 * list, waiting for the new osdmap (which can take
2037 * a while), even though the original request mapped
2038 * successfully. In the future we might want to follow
2039 * original request's nofail setting here.
2040 */
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002041 ret = __ceph_osdc_start_request(osdc, req, true);
2042 BUG_ON(ret);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002043
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002044 goto out_put;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02002045 }
2046
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002047 if (m.num_ops != req->r_num_ops) {
2048 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
2049 req->r_num_ops, req->r_tid);
2050 goto fail_request;
2051 }
2052 for (i = 0; i < req->r_num_ops; i++) {
2053 dout(" req %p tid %llu op %d rval %d len %u\n", req,
2054 req->r_tid, i, m.rval[i], m.outdata_len[i]);
2055 req->r_ops[i].rval = m.rval[i];
2056 req->r_ops[i].outdata_len = m.outdata_len[i];
2057 data_len += m.outdata_len[i];
2058 }
2059 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
2060 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
2061 le32_to_cpu(msg->hdr.data_len), req->r_tid);
2062 goto fail_request;
2063 }
2064 dout("%s req %p tid %llu acked %d result %d data_len %u\n", __func__,
2065 req, req->r_tid, req->r_got_reply, m.result, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07002066
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002067 already_acked = req->r_got_reply;
2068 if (!already_acked) {
2069 req->r_result = m.result ?: data_len;
2070 req->r_replay_version = m.replay_version; /* struct */
2071 req->r_got_reply = true;
2072 } else if (!(m.flags & CEPH_OSD_FLAG_ONDISK)) {
2073 dout("req %p tid %llu dup ack\n", req, req->r_tid);
2074 goto out_put;
Sage Weilf24e9982009-10-06 11:31:10 -07002075 }
2076
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002077 if (done_request(req, &m)) {
Sage Weilf24e9982009-10-06 11:31:10 -07002078 __unregister_request(osdc, req);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002079 if (req->r_linger) {
2080 WARN_ON(req->r_unsafe_callback);
2081 __register_linger_request(osdc, req);
2082 }
2083 }
Sage Weilf24e9982009-10-06 11:31:10 -07002084
2085 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02002086 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002087
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002088 if (done_request(req, &m)) {
2089 if (already_acked && req->r_unsafe_callback) {
2090 dout("req %p tid %llu safe-cb\n", req, req->r_tid);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08002091 req->r_unsafe_callback(req, false);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002092 } else {
2093 dout("req %p tid %llu cb\n", req, req->r_tid);
2094 __complete_request(req);
2095 }
2096 } else {
2097 if (req->r_unsafe_callback) {
2098 dout("req %p tid %llu unsafe-cb\n", req, req->r_tid);
2099 req->r_unsafe_callback(req, true);
2100 } else {
2101 WARN_ON(1);
2102 }
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08002103 }
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002104 if (m.flags & CEPH_OSD_FLAG_ONDISK)
2105 complete_all(&req->r_safe_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07002106
Sage Weilf24e9982009-10-06 11:31:10 -07002107 ceph_osdc_put_request(req);
2108 return;
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002109
2110fail_request:
2111 req->r_result = -EIO;
2112 __unregister_request(osdc, req);
2113 __complete_request(req);
2114 complete_all(&req->r_safe_completion);
2115out_put:
2116 ceph_osdc_put_request(req);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02002117out_unlock:
2118 mutex_unlock(&osdc->request_mutex);
2119 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002120}
2121
Sage Weil6f6c7002011-01-17 20:34:08 -08002122static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07002123{
Sage Weilf24e9982009-10-06 11:31:10 -07002124 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07002125
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03002126 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08002127 for (p = rb_first(&osdc->osds); p; p = n) {
2128 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07002129
Sage Weil6f6c7002011-01-17 20:34:08 -08002130 n = rb_next(p);
2131 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
2132 memcmp(&osd->o_con.peer_addr,
2133 ceph_osd_addr(osdc->osdmap,
2134 osd->o_osd),
2135 sizeof(struct ceph_entity_addr)) != 0)
2136 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07002137 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002138}
2139
2140/*
Sage Weil6f6c7002011-01-17 20:34:08 -08002141 * Requeue requests whose mapping to an OSD has changed. If requests map to
2142 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002143 *
Alex Eldere6d50f62012-12-26 14:31:40 -06002144 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002145 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002146static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
2147 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002148{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002149 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08002150 struct rb_node *p;
2151 int needmap = 0;
2152 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002153 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002154
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002155 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2156 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002157 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07002158 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08002159 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07002160 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06002161
2162 /*
2163 * For linger requests that have not yet been
2164 * registered, move them to the linger list; they'll
2165 * be sent to the osd in the loop below. Unregister
2166 * the request before re-registering it as a linger
2167 * request to ensure the __map_request() below
2168 * will decide it needs to be sent.
2169 */
2170 if (req->r_linger && list_empty(&req->r_linger_item)) {
2171 dout("%p tid %llu restart on osd%d\n",
2172 req, req->r_tid,
2173 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05002174 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002175 __unregister_request(osdc, req);
2176 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002177 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002178 continue;
2179 }
2180
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002181 force_resend_req = force_resend ||
2182 (force_resend_writes &&
2183 req->r_flags & CEPH_OSD_FLAG_WRITE);
2184 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002185 if (err < 0)
2186 continue; /* error */
2187 if (req->r_osd == NULL) {
2188 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2189 needmap++; /* request a newer map */
2190 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002191 if (!req->r_linger) {
2192 dout("%p tid %llu requeued on osd%d\n", req,
2193 req->r_tid,
2194 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002195 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002196 }
2197 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002198 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002199
2200 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2201 r_linger_item) {
2202 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2203
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002204 err = __map_request(osdc, req,
2205 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002206 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002207 if (err < 0)
2208 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002209 if (req->r_osd == NULL || err > 0) {
2210 if (req->r_osd == NULL) {
2211 dout("lingering %p tid %llu maps to no osd\n",
2212 req, req->r_tid);
2213 /*
2214 * A homeless lingering request makes
2215 * no sense, as it's job is to keep
2216 * a particular OSD connection open.
2217 * Request a newer map and kick the
2218 * request, knowing that it won't be
2219 * resent until we actually get a map
2220 * that can tell us where to send it.
2221 */
2222 needmap++;
2223 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002224
Ilya Dryomovb0494532015-05-11 17:53:10 +03002225 dout("kicking lingering %p tid %llu osd%d\n", req,
2226 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2227 __register_request(osdc, req);
2228 __unregister_linger_request(osdc, req);
2229 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002230 }
Alex Elder14d2f382013-05-15 16:28:33 -05002231 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002232 mutex_unlock(&osdc->request_mutex);
2233
2234 if (needmap) {
2235 dout("%d requests for down osds, need new map\n", needmap);
2236 ceph_monc_request_next_osdmap(&osdc->client->monc);
2237 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002238}
Sage Weil6f6c7002011-01-17 20:34:08 -08002239
2240
Sage Weilf24e9982009-10-06 11:31:10 -07002241/*
2242 * Process updated osd map.
2243 *
2244 * The message contains any number of incremental and full maps, normally
2245 * indicating some sort of topology change in the cluster. Kick requests
2246 * off to different OSDs as needed.
2247 */
2248void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2249{
2250 void *p, *end, *next;
2251 u32 nr_maps, maplen;
2252 u32 epoch;
2253 struct ceph_osdmap *newmap = NULL, *oldmap;
2254 int err;
2255 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002256 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002257
2258 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2259 p = msg->front.iov_base;
2260 end = p + msg->front.iov_len;
2261
2262 /* verify fsid */
2263 ceph_decode_need(&p, end, sizeof(fsid), bad);
2264 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002265 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2266 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002267
2268 down_write(&osdc->map_sem);
2269
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002270 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2271
Sage Weilf24e9982009-10-06 11:31:10 -07002272 /* incremental maps */
2273 ceph_decode_32_safe(&p, end, nr_maps, bad);
2274 dout(" %d inc maps\n", nr_maps);
2275 while (nr_maps > 0) {
2276 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002277 epoch = ceph_decode_32(&p);
2278 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002279 ceph_decode_need(&p, end, maplen, bad);
2280 next = p + maplen;
2281 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2282 dout("applying incremental map %u len %d\n",
2283 epoch, maplen);
2284 newmap = osdmap_apply_incremental(&p, next,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02002285 osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002286 if (IS_ERR(newmap)) {
2287 err = PTR_ERR(newmap);
2288 goto bad;
2289 }
Sage Weil30dc6382009-12-21 14:49:37 -08002290 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002291 if (newmap != osdc->osdmap) {
2292 ceph_osdmap_destroy(osdc->osdmap);
2293 osdc->osdmap = newmap;
2294 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002295 was_full = was_full ||
2296 ceph_osdmap_flag(osdc->osdmap,
2297 CEPH_OSDMAP_FULL);
2298 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002299 } else {
2300 dout("ignoring incremental map %u len %d\n",
2301 epoch, maplen);
2302 }
2303 p = next;
2304 nr_maps--;
2305 }
2306 if (newmap)
2307 goto done;
2308
2309 /* full maps */
2310 ceph_decode_32_safe(&p, end, nr_maps, bad);
2311 dout(" %d full maps\n", nr_maps);
2312 while (nr_maps) {
2313 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002314 epoch = ceph_decode_32(&p);
2315 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002316 ceph_decode_need(&p, end, maplen, bad);
2317 if (nr_maps > 1) {
2318 dout("skipping non-latest full map %u len %d\n",
2319 epoch, maplen);
2320 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2321 dout("skipping full map %u len %d, "
2322 "older than our %u\n", epoch, maplen,
2323 osdc->osdmap->epoch);
2324 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002325 int skipped_map = 0;
2326
Sage Weilf24e9982009-10-06 11:31:10 -07002327 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002328 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002329 if (IS_ERR(newmap)) {
2330 err = PTR_ERR(newmap);
2331 goto bad;
2332 }
Sage Weil30dc6382009-12-21 14:49:37 -08002333 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002334 oldmap = osdc->osdmap;
2335 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002336 if (oldmap) {
2337 if (oldmap->epoch + 1 < newmap->epoch)
2338 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002339 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002340 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002341 was_full = was_full ||
2342 ceph_osdmap_flag(osdc->osdmap,
2343 CEPH_OSDMAP_FULL);
2344 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002345 }
2346 p += maplen;
2347 nr_maps--;
2348 }
2349
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002350 if (!osdc->osdmap)
2351 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002352done:
2353 downgrade_write(&osdc->map_sem);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01002354 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2355 osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002356
2357 /*
2358 * subscribe to subsequent osdmap updates if full to ensure
2359 * we find out when we are no longer full and stop returning
2360 * ENOSPC.
2361 */
Josh Durgind29adb32013-12-02 19:11:48 -08002362 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2363 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2364 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002365 ceph_monc_request_next_osdmap(&osdc->client->monc);
2366
Alex Elderf9d25192013-02-15 11:42:29 -06002367 mutex_lock(&osdc->request_mutex);
2368 __send_queued(osdc);
2369 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002370 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002371 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002372 return;
2373
2374bad:
2375 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002376 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002377 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002378}
2379
Sage Weilf24e9982009-10-06 11:31:10 -07002380/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002381 * watch/notify callback event infrastructure
2382 *
2383 * These callbacks are used both for watch and notify operations.
2384 */
2385static void __release_event(struct kref *kref)
2386{
2387 struct ceph_osd_event *event =
2388 container_of(kref, struct ceph_osd_event, kref);
2389
2390 dout("__release_event %p\n", event);
2391 kfree(event);
2392}
2393
2394static void get_event(struct ceph_osd_event *event)
2395{
2396 kref_get(&event->kref);
2397}
2398
2399void ceph_osdc_put_event(struct ceph_osd_event *event)
2400{
2401 kref_put(&event->kref, __release_event);
2402}
2403EXPORT_SYMBOL(ceph_osdc_put_event);
2404
2405static void __insert_event(struct ceph_osd_client *osdc,
2406 struct ceph_osd_event *new)
2407{
2408 struct rb_node **p = &osdc->event_tree.rb_node;
2409 struct rb_node *parent = NULL;
2410 struct ceph_osd_event *event = NULL;
2411
2412 while (*p) {
2413 parent = *p;
2414 event = rb_entry(parent, struct ceph_osd_event, node);
2415 if (new->cookie < event->cookie)
2416 p = &(*p)->rb_left;
2417 else if (new->cookie > event->cookie)
2418 p = &(*p)->rb_right;
2419 else
2420 BUG();
2421 }
2422
2423 rb_link_node(&new->node, parent, p);
2424 rb_insert_color(&new->node, &osdc->event_tree);
2425}
2426
2427static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2428 u64 cookie)
2429{
2430 struct rb_node **p = &osdc->event_tree.rb_node;
2431 struct rb_node *parent = NULL;
2432 struct ceph_osd_event *event = NULL;
2433
2434 while (*p) {
2435 parent = *p;
2436 event = rb_entry(parent, struct ceph_osd_event, node);
2437 if (cookie < event->cookie)
2438 p = &(*p)->rb_left;
2439 else if (cookie > event->cookie)
2440 p = &(*p)->rb_right;
2441 else
2442 return event;
2443 }
2444 return NULL;
2445}
2446
2447static void __remove_event(struct ceph_osd_event *event)
2448{
2449 struct ceph_osd_client *osdc = event->osdc;
2450
2451 if (!RB_EMPTY_NODE(&event->node)) {
2452 dout("__remove_event removed %p\n", event);
2453 rb_erase(&event->node, &osdc->event_tree);
2454 ceph_osdc_put_event(event);
2455 } else {
2456 dout("__remove_event didn't remove %p\n", event);
2457 }
2458}
2459
2460int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2461 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002462 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002463{
2464 struct ceph_osd_event *event;
2465
2466 event = kmalloc(sizeof(*event), GFP_NOIO);
2467 if (!event)
2468 return -ENOMEM;
2469
2470 dout("create_event %p\n", event);
2471 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002472 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002473 event->data = data;
2474 event->osdc = osdc;
2475 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002476 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002477 kref_init(&event->kref); /* one ref for us */
2478 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002479
2480 spin_lock(&osdc->event_lock);
2481 event->cookie = ++osdc->event_count;
2482 __insert_event(osdc, event);
2483 spin_unlock(&osdc->event_lock);
2484
2485 *pevent = event;
2486 return 0;
2487}
2488EXPORT_SYMBOL(ceph_osdc_create_event);
2489
2490void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2491{
2492 struct ceph_osd_client *osdc = event->osdc;
2493
2494 dout("cancel_event %p\n", event);
2495 spin_lock(&osdc->event_lock);
2496 __remove_event(event);
2497 spin_unlock(&osdc->event_lock);
2498 ceph_osdc_put_event(event); /* caller's */
2499}
2500EXPORT_SYMBOL(ceph_osdc_cancel_event);
2501
2502
2503static void do_event_work(struct work_struct *work)
2504{
2505 struct ceph_osd_event_work *event_work =
2506 container_of(work, struct ceph_osd_event_work, work);
2507 struct ceph_osd_event *event = event_work->event;
2508 u64 ver = event_work->ver;
2509 u64 notify_id = event_work->notify_id;
2510 u8 opcode = event_work->opcode;
2511
2512 dout("do_event_work completing %p\n", event);
2513 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002514 dout("do_event_work completed %p\n", event);
2515 ceph_osdc_put_event(event);
2516 kfree(event_work);
2517}
2518
2519
2520/*
2521 * Process osd watch notifications
2522 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002523static void handle_watch_notify(struct ceph_osd_client *osdc,
2524 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002525{
2526 void *p, *end;
2527 u8 proto_ver;
2528 u64 cookie, ver, notify_id;
2529 u8 opcode;
2530 struct ceph_osd_event *event;
2531 struct ceph_osd_event_work *event_work;
2532
2533 p = msg->front.iov_base;
2534 end = p + msg->front.iov_len;
2535
2536 ceph_decode_8_safe(&p, end, proto_ver, bad);
2537 ceph_decode_8_safe(&p, end, opcode, bad);
2538 ceph_decode_64_safe(&p, end, cookie, bad);
2539 ceph_decode_64_safe(&p, end, ver, bad);
2540 ceph_decode_64_safe(&p, end, notify_id, bad);
2541
2542 spin_lock(&osdc->event_lock);
2543 event = __find_event(osdc, cookie);
2544 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002545 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002546 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002547 }
2548 spin_unlock(&osdc->event_lock);
2549 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2550 cookie, ver, event);
2551 if (event) {
2552 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002553 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002554 pr_err("couldn't allocate event_work\n");
2555 ceph_osdc_put_event(event);
2556 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002557 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002558 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002559 event_work->event = event;
2560 event_work->ver = ver;
2561 event_work->notify_id = notify_id;
2562 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002563
2564 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002565 }
2566
2567 return;
2568
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002569bad:
2570 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002571}
2572
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002573/*
Sage Weilf24e9982009-10-06 11:31:10 -07002574 * Register request, send initial attempt.
2575 */
2576int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2577 struct ceph_osd_request *req,
2578 bool nofail)
2579{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002580 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002581
Sage Weilf24e9982009-10-06 11:31:10 -07002582 down_read(&osdc->map_sem);
2583 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002584
2585 rc = __ceph_osdc_start_request(osdc, req, nofail);
2586
Sage Weilf24e9982009-10-06 11:31:10 -07002587 mutex_unlock(&osdc->request_mutex);
2588 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002589
Sage Weilf24e9982009-10-06 11:31:10 -07002590 return rc;
2591}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002592EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002593
2594/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002595 * Unregister a registered request. The request is not completed (i.e.
2596 * no callbacks or wakeups) - higher layers are supposed to know what
2597 * they are canceling.
2598 */
2599void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2600{
2601 struct ceph_osd_client *osdc = req->r_osdc;
2602
2603 mutex_lock(&osdc->request_mutex);
2604 if (req->r_linger)
2605 __unregister_linger_request(osdc, req);
2606 __unregister_request(osdc, req);
2607 mutex_unlock(&osdc->request_mutex);
2608
2609 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2610}
2611EXPORT_SYMBOL(ceph_osdc_cancel_request);
2612
2613/*
Sage Weilf24e9982009-10-06 11:31:10 -07002614 * wait for a request to complete
2615 */
2616int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2617 struct ceph_osd_request *req)
2618{
2619 int rc;
2620
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002621 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2622
Sage Weilf24e9982009-10-06 11:31:10 -07002623 rc = wait_for_completion_interruptible(&req->r_completion);
2624 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002625 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2626 ceph_osdc_cancel_request(req);
Ilya Dryomovfe5da052016-04-28 16:07:24 +02002627
2628 /* kludge - need to to wake ceph_osdc_sync() */
2629 complete_all(&req->r_safe_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07002630 return rc;
2631 }
2632
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002633 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2634 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002635 return req->r_result;
2636}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002637EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002638
2639/*
2640 * sync - wait for all in-flight requests to flush. avoid starvation.
2641 */
2642void ceph_osdc_sync(struct ceph_osd_client *osdc)
2643{
2644 struct ceph_osd_request *req;
2645 u64 last_tid, next_tid = 0;
2646
2647 mutex_lock(&osdc->request_mutex);
2648 last_tid = osdc->last_tid;
2649 while (1) {
2650 req = __lookup_request_ge(osdc, next_tid);
2651 if (!req)
2652 break;
2653 if (req->r_tid > last_tid)
2654 break;
2655
2656 next_tid = req->r_tid + 1;
2657 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2658 continue;
2659
2660 ceph_osdc_get_request(req);
2661 mutex_unlock(&osdc->request_mutex);
2662 dout("sync waiting on tid %llu (last is %llu)\n",
2663 req->r_tid, last_tid);
2664 wait_for_completion(&req->r_safe_completion);
2665 mutex_lock(&osdc->request_mutex);
2666 ceph_osdc_put_request(req);
2667 }
2668 mutex_unlock(&osdc->request_mutex);
2669 dout("sync done (thru tid %llu)\n", last_tid);
2670}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002671EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002672
2673/*
Josh Durgindd935f42013-08-28 21:43:09 -07002674 * Call all pending notify callbacks - for use after a watch is
2675 * unregistered, to make sure no more callbacks for it will be invoked
2676 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002677void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002678{
2679 flush_workqueue(osdc->notify_wq);
2680}
2681EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2682
2683
2684/*
Sage Weilf24e9982009-10-06 11:31:10 -07002685 * init, shutdown
2686 */
2687int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2688{
2689 int err;
2690
2691 dout("init\n");
2692 osdc->client = client;
2693 osdc->osdmap = NULL;
2694 init_rwsem(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002695 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002696 osdc->last_tid = 0;
2697 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002698 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002699 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002700 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002701 INIT_LIST_HEAD(&osdc->req_unsent);
2702 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002703 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002704 osdc->num_requests = 0;
2705 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002706 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002707 spin_lock_init(&osdc->event_lock);
2708 osdc->event_tree = RB_ROOT;
2709 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002710
Sage Weil5f44f142009-11-18 14:52:18 -08002711 err = -ENOMEM;
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01002712 osdc->req_mempool = mempool_create_slab_pool(10,
2713 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07002714 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002715 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002716
Sage Weild50b4092012-07-09 14:22:34 -07002717 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02002718 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002719 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002720 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002721 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02002722 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002723 if (err < 0)
2724 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002725
Dan Carpenterdbcae082013-08-15 08:58:59 +03002726 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002727 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002728 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002729 goto out_msgpool_reply;
2730
Ilya Dryomovfbca9632016-04-28 16:07:24 +02002731 schedule_delayed_work(&osdc->timeout_work,
2732 osdc->client->options->osd_keepalive_timeout);
Ilya Dryomovb37ee1b2016-04-28 16:07:24 +02002733 schedule_delayed_work(&osdc->osds_timeout_work,
2734 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
2735
Sage Weilf24e9982009-10-06 11:31:10 -07002736 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002737
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002738out_msgpool_reply:
2739 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002740out_msgpool:
2741 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002742out_mempool:
2743 mempool_destroy(osdc->req_mempool);
2744out:
2745 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002746}
2747
2748void ceph_osdc_stop(struct ceph_osd_client *osdc)
2749{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002750 flush_workqueue(osdc->notify_wq);
2751 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002752 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002753 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002754
2755 mutex_lock(&osdc->request_mutex);
2756 while (!RB_EMPTY_ROOT(&osdc->osds)) {
2757 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
2758 struct ceph_osd, o_node);
2759 remove_osd(osdc, osd);
2760 }
2761 mutex_unlock(&osdc->request_mutex);
2762
Sage Weilf24e9982009-10-06 11:31:10 -07002763 if (osdc->osdmap) {
2764 ceph_osdmap_destroy(osdc->osdmap);
2765 osdc->osdmap = NULL;
2766 }
2767 mempool_destroy(osdc->req_mempool);
2768 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002769 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002770}
2771
2772/*
2773 * Read some contiguous pages. If we cross a stripe boundary, shorten
2774 * *plen. Return number of bytes read, or error.
2775 */
2776int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2777 struct ceph_vino vino, struct ceph_file_layout *layout,
2778 u64 off, u64 *plen,
2779 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002780 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002781{
2782 struct ceph_osd_request *req;
2783 int rc = 0;
2784
2785 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2786 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002787 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002788 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002789 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002790 false);
Sage Weil68162822012-09-24 21:01:02 -07002791 if (IS_ERR(req))
2792 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002793
2794 /* it may be a short read due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002795 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002796 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002797
Alex Eldere0c59482013-03-07 15:38:25 -06002798 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002799 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002800
2801 rc = ceph_osdc_start_request(osdc, req, false);
2802 if (!rc)
2803 rc = ceph_osdc_wait_request(osdc, req);
2804
2805 ceph_osdc_put_request(req);
2806 dout("readpages result %d\n", rc);
2807 return rc;
2808}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002809EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002810
2811/*
2812 * do a synchronous write on N pages
2813 */
2814int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2815 struct ceph_file_layout *layout,
2816 struct ceph_snap_context *snapc,
2817 u64 off, u64 len,
2818 u32 truncate_seq, u64 truncate_size,
2819 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002820 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002821{
2822 struct ceph_osd_request *req;
2823 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002824 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002825
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002826 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002827 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002828 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002829 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002830 true);
Sage Weil68162822012-09-24 21:01:02 -07002831 if (IS_ERR(req))
2832 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002833
2834 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002835 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002836 false, false);
2837 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002838
Ilya Dryomovbb873b52016-05-26 00:29:52 +02002839 req->r_mtime = *mtime;
Alex Elder87f979d2013-02-15 11:42:29 -06002840 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002841 if (!rc)
2842 rc = ceph_osdc_wait_request(osdc, req);
2843
2844 ceph_osdc_put_request(req);
2845 if (rc == 0)
2846 rc = len;
2847 dout("writepages result %d\n", rc);
2848 return rc;
2849}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002850EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002851
Alex Elder5522ae02013-05-01 12:43:04 -05002852int ceph_osdc_setup(void)
2853{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002854 size_t size = sizeof(struct ceph_osd_request) +
2855 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2856
Alex Elder5522ae02013-05-01 12:43:04 -05002857 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002858 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2859 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05002860
2861 return ceph_osd_request_cache ? 0 : -ENOMEM;
2862}
2863EXPORT_SYMBOL(ceph_osdc_setup);
2864
2865void ceph_osdc_cleanup(void)
2866{
2867 BUG_ON(!ceph_osd_request_cache);
2868 kmem_cache_destroy(ceph_osd_request_cache);
2869 ceph_osd_request_cache = NULL;
2870}
2871EXPORT_SYMBOL(ceph_osdc_cleanup);
2872
Sage Weilf24e9982009-10-06 11:31:10 -07002873/*
2874 * handle incoming message
2875 */
2876static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2877{
2878 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002879 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002880 int type = le16_to_cpu(msg->hdr.type);
2881
2882 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002883 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002884 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002885
2886 switch (type) {
2887 case CEPH_MSG_OSD_MAP:
2888 ceph_osdc_handle_map(osdc, msg);
2889 break;
2890 case CEPH_MSG_OSD_OPREPLY:
Shraddha Barke70cf0522015-10-18 13:55:38 +05302891 handle_reply(osdc, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002892 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002893 case CEPH_MSG_WATCH_NOTIFY:
2894 handle_watch_notify(osdc, msg);
2895 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002896
2897 default:
2898 pr_err("received unknown message type %d %s\n", type,
2899 ceph_msg_type_name(type));
2900 }
Sage Weil4a32f932010-06-13 10:27:53 -07002901out:
Sage Weilf24e9982009-10-06 11:31:10 -07002902 ceph_msg_put(msg);
2903}
2904
Sage Weil5b3a4db2010-02-19 21:43:23 -08002905/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002906 * Lookup and return message for incoming reply. Don't try to do
2907 * anything about a larger than preallocated data portion of the
2908 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002909 */
2910static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002911 struct ceph_msg_header *hdr,
2912 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002913{
2914 struct ceph_osd *osd = con->private;
2915 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002916 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002917 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002918 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002919 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002920 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002921
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002922 tid = le64_to_cpu(hdr->tid);
2923 mutex_lock(&osdc->request_mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002924 req = lookup_request(&osdc->requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002925 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01002926 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2927 osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002928 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002929 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002930 goto out;
2931 }
Sage Weilc16e7862010-03-01 13:02:00 -08002932
Alex Elderace6d3a2013-04-01 16:12:14 -05002933 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002934
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002935 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002936 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2937 __func__, osd->o_osd, req->r_tid, front_len,
2938 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002939 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2940 false);
Sage Weila79832f2010-04-01 16:06:19 -07002941 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002942 goto out;
2943 ceph_msg_put(req->r_reply);
2944 req->r_reply = m;
2945 }
Sage Weilc16e7862010-03-01 13:02:00 -08002946
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002947 if (data_len > req->r_reply->data_length) {
2948 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2949 __func__, osd->o_osd, req->r_tid, data_len,
2950 req->r_reply->data_length);
2951 m = NULL;
2952 *skip = 1;
2953 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002954 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002955
2956 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002957 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002958
2959out:
2960 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002961 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002962}
2963
2964static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2965 struct ceph_msg_header *hdr,
2966 int *skip)
2967{
2968 struct ceph_osd *osd = con->private;
2969 int type = le16_to_cpu(hdr->type);
2970 int front = le32_to_cpu(hdr->front_len);
2971
Alex Elder1c20f2d2012-06-04 14:43:32 -05002972 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002973 switch (type) {
2974 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002975 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002976 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002977 case CEPH_MSG_OSD_OPREPLY:
2978 return get_reply(con, hdr, skip);
2979 default:
2980 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2981 osd->o_osd);
2982 *skip = 1;
2983 return NULL;
2984 }
Sage Weilf24e9982009-10-06 11:31:10 -07002985}
2986
2987/*
2988 * Wrappers to refcount containing ceph_osd struct
2989 */
2990static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2991{
2992 struct ceph_osd *osd = con->private;
2993 if (get_osd(osd))
2994 return con;
2995 return NULL;
2996}
2997
2998static void put_osd_con(struct ceph_connection *con)
2999{
3000 struct ceph_osd *osd = con->private;
3001 put_osd(osd);
3002}
3003
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003004/*
3005 * authentication
3006 */
Alex Eldera3530df2012-05-16 15:16:39 -05003007/*
3008 * Note: returned pointer is the address of a structure that's
3009 * managed separately. Caller must *not* attempt to free it.
3010 */
3011static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05003012 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003013{
3014 struct ceph_osd *o = con->private;
3015 struct ceph_osd_client *osdc = o->o_osdc;
3016 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05003017 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003018
Alex Elder74f18692012-05-16 15:16:39 -05003019 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02003020 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05003021 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003022 }
Sage Weil27859f92013-03-25 10:26:14 -07003023 if (!auth->authorizer) {
3024 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
3025 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003026 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05003027 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07003028 } else {
3029 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07003030 auth);
3031 if (ret)
3032 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003033 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003034 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05003035
Alex Eldera3530df2012-05-16 15:16:39 -05003036 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003037}
3038
3039
3040static int verify_authorizer_reply(struct ceph_connection *con, int len)
3041{
3042 struct ceph_osd *o = con->private;
3043 struct ceph_osd_client *osdc = o->o_osdc;
3044 struct ceph_auth_client *ac = osdc->client->monc.auth;
3045
Sage Weil27859f92013-03-25 10:26:14 -07003046 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003047}
3048
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003049static int invalidate_authorizer(struct ceph_connection *con)
3050{
3051 struct ceph_osd *o = con->private;
3052 struct ceph_osd_client *osdc = o->o_osdc;
3053 struct ceph_auth_client *ac = osdc->client->monc.auth;
3054
Sage Weil27859f92013-03-25 10:26:14 -07003055 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003056 return ceph_monc_validate_auth(&osdc->client->monc);
3057}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003058
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003059static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003060{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003061 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003062 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003063
Yan, Zheng33d07332014-11-04 16:33:37 +08003064 return ceph_auth_sign_message(auth, msg);
3065}
3066
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003067static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003068{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003069 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003070 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003071
Yan, Zheng33d07332014-11-04 16:33:37 +08003072 return ceph_auth_check_message_signature(auth, msg);
3073}
3074
Tobias Klauser9e327892010-05-20 10:40:19 +02003075static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07003076 .get = get_osd_con,
3077 .put = put_osd_con,
3078 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003079 .get_authorizer = get_authorizer,
3080 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003081 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003082 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003083 .sign_message = osd_sign_message,
3084 .check_message_signature = osd_check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003085 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003086};