blob: cff3a7e29233db8e038df72f52e0afbfff09d576 [file] [log] [blame]
Alex Eldera4ce40a2013-04-05 01:27:12 -05001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07005#include <linux/err.h>
6#include <linux/highmem.h>
7#include <linux/mm.h>
8#include <linux/pagemap.h>
9#include <linux/slab.h>
10#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070011#ifdef CONFIG_BLOCK
12#include <linux/bio.h>
13#endif
Sage Weilf24e9982009-10-06 11:31:10 -070014
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include <linux/ceph/libceph.h>
16#include <linux/ceph/osd_client.h>
17#include <linux/ceph/messenger.h>
18#include <linux/ceph/decode.h>
19#include <linux/ceph/auth.h>
20#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070021
Sage Weilc16e7862010-03-01 13:02:00 -080022#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080023
Alex Elder5522ae02013-05-01 12:43:04 -050024static struct kmem_cache *ceph_osd_request_cache;
25
Tobias Klauser9e327892010-05-20 10:40:19 +020026static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070027
Alex Elderf9d25192013-02-15 11:42:29 -060028static void __send_queued(struct ceph_osd_client *osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -080029static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070030static void __register_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040032static void __unregister_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070034static void __unregister_linger_request(struct ceph_osd_client *osdc,
35 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040036static void __enqueue_request(struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080037static void __send_request(struct ceph_osd_client *osdc,
38 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070039
40/*
41 * Implement client access to distributed object storage cluster.
42 *
43 * All data objects are stored within a cluster/cloud of OSDs, or
44 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
45 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
46 * remote daemons serving up and coordinating consistent and safe
47 * access to storage.
48 *
49 * Cluster membership and the mapping of data objects onto storage devices
50 * are described by the osd map.
51 *
52 * We keep track of pending OSD requests (read, write), resubmit
53 * requests to different OSDs when the cluster topology/data layout
54 * change, or retry the affected requests when the communications
55 * channel with an OSD is reset.
56 */
57
58/*
59 * calculate the mapping of a file extent onto an object, and fill out the
60 * request accordingly. shorten extent as necessary if it crosses an
61 * object boundary.
62 *
63 * fill osd op in request message.
64 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060065static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050066 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070067{
Alex Elder60e56f12013-02-15 11:42:29 -060068 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070069 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070070
Alex Elder60e56f12013-02-15 11:42:29 -060071 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050072 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
73 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070074 if (r < 0)
75 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050076 if (*objlen < orig_len) {
77 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060078 dout(" skipping last %llu, final file extent %llu~%llu\n",
79 orig_len - *plen, off, *plen);
80 }
81
Alex Elder75d1c942013-03-13 20:50:00 -050082 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070083
Alex Elder3ff5f382013-02-15 22:10:17 -060084 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070085}
86
Alex Elderc54d47b2013-04-03 01:28:57 -050087static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
88{
89 memset(osd_data, 0, sizeof (*osd_data));
90 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
91}
92
Alex Eldera4ce40a2013-04-05 01:27:12 -050093static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -050094 struct page **pages, u64 length, u32 alignment,
95 bool pages_from_pool, bool own_pages)
96{
97 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
98 osd_data->pages = pages;
99 osd_data->length = length;
100 osd_data->alignment = alignment;
101 osd_data->pages_from_pool = pages_from_pool;
102 osd_data->own_pages = own_pages;
103}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500104
Alex Eldera4ce40a2013-04-05 01:27:12 -0500105static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500106 struct ceph_pagelist *pagelist)
107{
108 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
109 osd_data->pagelist = pagelist;
110}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500111
112#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500113static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500114 struct bio *bio, size_t bio_length)
115{
116 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
117 osd_data->bio = bio;
118 osd_data->bio_length = bio_length;
119}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500120#endif /* CONFIG_BLOCK */
121
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300122#define osd_req_op_data(oreq, whch, typ, fld) \
123({ \
124 struct ceph_osd_request *__oreq = (oreq); \
125 unsigned int __whch = (whch); \
126 BUG_ON(__whch >= __oreq->r_num_ops); \
127 &__oreq->r_ops[__whch].typ.fld; \
128})
Alex Elder863c7eb2013-04-15 14:50:36 -0500129
Alex Elder49719772013-02-11 12:33:24 -0600130static struct ceph_osd_data *
131osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
132{
133 BUG_ON(which >= osd_req->r_num_ops);
134
135 return &osd_req->r_ops[which].raw_data_in;
136}
137
Alex Eldera4ce40a2013-04-05 01:27:12 -0500138struct ceph_osd_data *
139osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500140 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500141{
Alex Elder863c7eb2013-04-15 14:50:36 -0500142 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500143}
144EXPORT_SYMBOL(osd_req_op_extent_osd_data);
145
Alex Elder49719772013-02-11 12:33:24 -0600146void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
147 unsigned int which, struct page **pages,
148 u64 length, u32 alignment,
149 bool pages_from_pool, bool own_pages)
150{
151 struct ceph_osd_data *osd_data;
152
153 osd_data = osd_req_op_raw_data_in(osd_req, which);
154 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
155 pages_from_pool, own_pages);
156}
157EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
158
Alex Eldera4ce40a2013-04-05 01:27:12 -0500159void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500160 unsigned int which, struct page **pages,
161 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500162 bool pages_from_pool, bool own_pages)
163{
164 struct ceph_osd_data *osd_data;
165
Alex Elder863c7eb2013-04-15 14:50:36 -0500166 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500167 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
168 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500169}
170EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
171
172void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500173 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500174{
175 struct ceph_osd_data *osd_data;
176
Alex Elder863c7eb2013-04-15 14:50:36 -0500177 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500178 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500179}
180EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
181
182#ifdef CONFIG_BLOCK
183void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500184 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500185{
186 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500187
188 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500189 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500190}
191EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
192#endif /* CONFIG_BLOCK */
193
194static void osd_req_op_cls_request_info_pagelist(
195 struct ceph_osd_request *osd_req,
196 unsigned int which, struct ceph_pagelist *pagelist)
197{
198 struct ceph_osd_data *osd_data;
199
Alex Elder863c7eb2013-04-15 14:50:36 -0500200 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500201 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500202}
203
Alex Elder04017e22013-04-05 14:46:02 -0500204void osd_req_op_cls_request_data_pagelist(
205 struct ceph_osd_request *osd_req,
206 unsigned int which, struct ceph_pagelist *pagelist)
207{
208 struct ceph_osd_data *osd_data;
209
Alex Elder863c7eb2013-04-15 14:50:36 -0500210 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500211 ceph_osd_data_pagelist_init(osd_data, pagelist);
212}
213EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
214
Alex Elder6c57b552013-04-19 15:34:49 -0500215void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
216 unsigned int which, struct page **pages, u64 length,
217 u32 alignment, bool pages_from_pool, bool own_pages)
218{
219 struct ceph_osd_data *osd_data;
220
221 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
222 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
223 pages_from_pool, own_pages);
224}
225EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
226
Alex Eldera4ce40a2013-04-05 01:27:12 -0500227void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
228 unsigned int which, struct page **pages, u64 length,
229 u32 alignment, bool pages_from_pool, bool own_pages)
230{
231 struct ceph_osd_data *osd_data;
232
Alex Elder863c7eb2013-04-15 14:50:36 -0500233 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500234 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
235 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500236}
237EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
238
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500239static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
240{
241 switch (osd_data->type) {
242 case CEPH_OSD_DATA_TYPE_NONE:
243 return 0;
244 case CEPH_OSD_DATA_TYPE_PAGES:
245 return osd_data->length;
246 case CEPH_OSD_DATA_TYPE_PAGELIST:
247 return (u64)osd_data->pagelist->length;
248#ifdef CONFIG_BLOCK
249 case CEPH_OSD_DATA_TYPE_BIO:
250 return (u64)osd_data->bio_length;
251#endif /* CONFIG_BLOCK */
252 default:
253 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
254 return 0;
255 }
256}
257
Alex Elderc54d47b2013-04-03 01:28:57 -0500258static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
259{
Alex Elder54764922013-04-05 01:27:12 -0500260 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500261 int num_pages;
262
263 num_pages = calc_pages_for((u64)osd_data->alignment,
264 (u64)osd_data->length);
265 ceph_release_page_vector(osd_data->pages, num_pages);
266 }
Alex Elder54764922013-04-05 01:27:12 -0500267 ceph_osd_data_init(osd_data);
268}
269
270static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
271 unsigned int which)
272{
273 struct ceph_osd_req_op *op;
274
275 BUG_ON(which >= osd_req->r_num_ops);
276 op = &osd_req->r_ops[which];
277
278 switch (op->op) {
279 case CEPH_OSD_OP_READ:
280 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200281 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500282 ceph_osd_data_release(&op->extent.osd_data);
283 break;
284 case CEPH_OSD_OP_CALL:
285 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500286 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500287 ceph_osd_data_release(&op->cls.response_data);
288 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800289 case CEPH_OSD_OP_SETXATTR:
290 case CEPH_OSD_OP_CMPXATTR:
291 ceph_osd_data_release(&op->xattr.osd_data);
292 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800293 case CEPH_OSD_OP_STAT:
294 ceph_osd_data_release(&op->raw_data_in);
295 break;
Alex Elder54764922013-04-05 01:27:12 -0500296 default:
297 break;
298 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500299}
300
Sage Weilf24e9982009-10-06 11:31:10 -0700301/*
Ilya Dryomov63244fa2016-04-28 16:07:23 +0200302 * Assumes @t is zero-initialized.
303 */
304static void target_init(struct ceph_osd_request_target *t)
305{
306 ceph_oid_init(&t->base_oid);
307 ceph_oloc_init(&t->base_oloc);
308 ceph_oid_init(&t->target_oid);
309 ceph_oloc_init(&t->target_oloc);
310
311 ceph_osds_init(&t->acting);
312 ceph_osds_init(&t->up);
313 t->size = -1;
314 t->min_size = -1;
315
316 t->osd = CEPH_HOMELESS_OSD;
317}
318
319static void target_destroy(struct ceph_osd_request_target *t)
320{
321 ceph_oid_destroy(&t->base_oid);
322 ceph_oid_destroy(&t->target_oid);
323}
324
325/*
Sage Weilf24e9982009-10-06 11:31:10 -0700326 * requests
327 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400328static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700329{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400330 struct ceph_osd_request *req = container_of(kref,
331 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500332 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800333
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400334 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
335 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400336 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
337 WARN_ON(!list_empty(&req->r_req_lru_item));
338 WARN_ON(!list_empty(&req->r_osd_item));
339 WARN_ON(!list_empty(&req->r_linger_item));
340 WARN_ON(!list_empty(&req->r_linger_osd_item));
341 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400342
Sage Weil415e49a2009-12-07 13:37:03 -0800343 if (req->r_request)
344 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500345 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500346 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500347 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500348 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600349
Alex Elder54764922013-04-05 01:27:12 -0500350 for (which = 0; which < req->r_num_ops; which++)
351 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600352
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200353 ceph_oid_destroy(&req->r_base_oid);
354 ceph_oid_destroy(&req->r_target_oid);
Sage Weil415e49a2009-12-07 13:37:03 -0800355 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200356
Sage Weil415e49a2009-12-07 13:37:03 -0800357 if (req->r_mempool)
358 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100359 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500360 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100361 else
362 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700363}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400364
365void ceph_osdc_get_request(struct ceph_osd_request *req)
366{
367 dout("%s %p (was %d)\n", __func__, req,
368 atomic_read(&req->r_kref.refcount));
369 kref_get(&req->r_kref);
370}
371EXPORT_SYMBOL(ceph_osdc_get_request);
372
373void ceph_osdc_put_request(struct ceph_osd_request *req)
374{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200375 if (req) {
376 dout("%s %p (was %d)\n", __func__, req,
377 atomic_read(&req->r_kref.refcount));
378 kref_put(&req->r_kref, ceph_osdc_release_request);
379 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400380}
381EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700382
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700383struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700384 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800385 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700386 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600387 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700388{
389 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700390
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700391 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100392 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700393 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100394 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
395 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700396 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100397 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
398 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
399 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700400 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100401 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700402 return NULL;
403
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100404 /* req only, each op is zeroed in _osd_req_op_init() */
405 memset(req, 0, sizeof(*req));
406
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700407 req->r_osdc = osdc;
408 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500409 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200410 req->r_snapid = CEPH_NOSNAP;
411 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700412
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700413 kref_init(&req->r_kref);
414 init_completion(&req->r_completion);
415 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600416 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700417 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700418 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400419 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b639a2011-09-16 11:13:17 -0700420 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700421 INIT_LIST_HEAD(&req->r_osd_item);
422
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200423 ceph_oid_init(&req->r_base_oid);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200424 req->r_base_oloc.pool = -1;
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200425 ceph_oid_init(&req->r_target_oid);
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200426 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200427
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200428 dout("%s req %p\n", __func__, req);
429 return req;
430}
431EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100432
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200433int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
434{
435 struct ceph_osd_client *osdc = req->r_osdc;
436 struct ceph_msg *msg;
437 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700438
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200439 WARN_ON(ceph_oid_empty(&req->r_base_oid));
440
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200441 /* create request message */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100442 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
443 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
444 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
445 msg_size += 1 + 8 + 4 + 4; /* pgid */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200446 msg_size += 4 + req->r_base_oid.name_len; /* oid */
447 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100448 msg_size += 8; /* snapid */
449 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200450 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100451 msg_size += 4; /* retry_attempt */
452
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200453 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700454 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
455 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200456 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
457 if (!msg)
458 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700459
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700460 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700461 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700462
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200463 /* create reply message */
464 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200465 msg_size += req->r_base_oid.name_len;
466 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200467
468 if (req->r_mempool)
469 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
470 else
471 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
472 if (!msg)
473 return -ENOMEM;
474
475 req->r_reply = msg;
476
477 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700478}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200479EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700480
Alex Eldera8dd0a32013-03-13 20:50:00 -0500481static bool osd_req_opcode_valid(u16 opcode)
482{
483 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400484#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
485__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
486#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500487 default:
488 return false;
489 }
490}
491
Alex Elder33803f32013-03-13 20:50:00 -0500492/*
493 * This is an osd op init function for opcodes that have no data or
494 * other information associated with them. It also serves as a
495 * common init routine for all the other init functions, below.
496 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500497static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600498_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800499 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500500{
Alex Elderc99d2d42013-04-05 01:27:11 -0500501 struct ceph_osd_req_op *op;
502
503 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500504 BUG_ON(!osd_req_opcode_valid(opcode));
505
Alex Elderc99d2d42013-04-05 01:27:11 -0500506 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500507 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500508 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800509 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500510
511 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500512}
513
Alex Elder49719772013-02-11 12:33:24 -0600514void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800515 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600516{
Yan, Zheng144cba12015-04-27 11:09:54 +0800517 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600518}
519EXPORT_SYMBOL(osd_req_op_init);
520
Alex Elderc99d2d42013-04-05 01:27:11 -0500521void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
522 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500523 u64 offset, u64 length,
524 u64 truncate_size, u32 truncate_seq)
525{
Yan, Zheng144cba12015-04-27 11:09:54 +0800526 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
527 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500528 size_t payload_len = 0;
529
Li Wangad7a60d2013-08-15 11:51:44 +0800530 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200531 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
532 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500533
Alex Elder33803f32013-03-13 20:50:00 -0500534 op->extent.offset = offset;
535 op->extent.length = length;
536 op->extent.truncate_size = truncate_size;
537 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200538 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500539 payload_len += length;
540
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100541 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500542}
543EXPORT_SYMBOL(osd_req_op_extent_init);
544
Alex Elderc99d2d42013-04-05 01:27:11 -0500545void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
546 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500547{
Alex Elderc99d2d42013-04-05 01:27:11 -0500548 struct ceph_osd_req_op *op;
549 u64 previous;
550
551 BUG_ON(which >= osd_req->r_num_ops);
552 op = &osd_req->r_ops[which];
553 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500554
555 if (length == previous)
556 return; /* Nothing to do */
557 BUG_ON(length > previous);
558
559 op->extent.length = length;
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100560 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500561}
562EXPORT_SYMBOL(osd_req_op_extent_update);
563
Yan, Zheng2c63f492016-01-07 17:32:54 +0800564void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
565 unsigned int which, u64 offset_inc)
566{
567 struct ceph_osd_req_op *op, *prev_op;
568
569 BUG_ON(which + 1 >= osd_req->r_num_ops);
570
571 prev_op = &osd_req->r_ops[which];
572 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
573 /* dup previous one */
574 op->indata_len = prev_op->indata_len;
575 op->outdata_len = prev_op->outdata_len;
576 op->extent = prev_op->extent;
577 /* adjust offset */
578 op->extent.offset += offset_inc;
579 op->extent.length -= offset_inc;
580
581 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
582 op->indata_len -= offset_inc;
583}
584EXPORT_SYMBOL(osd_req_op_extent_dup_last);
585
Alex Elderc99d2d42013-04-05 01:27:11 -0500586void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500587 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500588{
Yan, Zheng144cba12015-04-27 11:09:54 +0800589 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
590 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500591 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500592 size_t payload_len = 0;
593 size_t size;
594
595 BUG_ON(opcode != CEPH_OSD_OP_CALL);
596
Alex Elder5f562df2013-04-05 01:27:12 -0500597 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
598 BUG_ON(!pagelist);
599 ceph_pagelist_init(pagelist);
600
Alex Elder33803f32013-03-13 20:50:00 -0500601 op->cls.class_name = class;
602 size = strlen(class);
603 BUG_ON(size > (size_t) U8_MAX);
604 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500605 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500606 payload_len += size;
607
608 op->cls.method_name = method;
609 size = strlen(method);
610 BUG_ON(size > (size_t) U8_MAX);
611 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500612 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500613 payload_len += size;
614
Alex Eldera4ce40a2013-04-05 01:27:12 -0500615 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500616
Alex Elder33803f32013-03-13 20:50:00 -0500617 op->cls.argc = 0; /* currently unused */
618
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100619 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500620}
621EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500622
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800623int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
624 u16 opcode, const char *name, const void *value,
625 size_t size, u8 cmp_op, u8 cmp_mode)
626{
Yan, Zheng144cba12015-04-27 11:09:54 +0800627 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
628 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800629 struct ceph_pagelist *pagelist;
630 size_t payload_len;
631
632 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
633
634 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
635 if (!pagelist)
636 return -ENOMEM;
637
638 ceph_pagelist_init(pagelist);
639
640 payload_len = strlen(name);
641 op->xattr.name_len = payload_len;
642 ceph_pagelist_append(pagelist, name, payload_len);
643
644 op->xattr.value_len = size;
645 ceph_pagelist_append(pagelist, value, size);
646 payload_len += size;
647
648 op->xattr.cmp_op = cmp_op;
649 op->xattr.cmp_mode = cmp_mode;
650
651 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100652 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800653 return 0;
654}
655EXPORT_SYMBOL(osd_req_op_xattr_init);
656
Alex Elderc99d2d42013-04-05 01:27:11 -0500657void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
658 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500659 u64 cookie, u64 version, int flag)
660{
Yan, Zheng144cba12015-04-27 11:09:54 +0800661 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
662 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500663
Alex Elderc99d2d42013-04-05 01:27:11 -0500664 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500665
666 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500667 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500668 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500669 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500670}
671EXPORT_SYMBOL(osd_req_op_watch_init);
672
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200673void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
674 unsigned int which,
675 u64 expected_object_size,
676 u64 expected_write_size)
677{
678 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800679 CEPH_OSD_OP_SETALLOCHINT,
680 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200681
682 op->alloc_hint.expected_object_size = expected_object_size;
683 op->alloc_hint.expected_write_size = expected_write_size;
684
685 /*
686 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
687 * not worth a feature bit. Set FAILOK per-op flag to make
688 * sure older osds don't trip over an unsupported opcode.
689 */
690 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
691}
692EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
693
Alex Elder90af3602013-04-05 14:46:01 -0500694static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500695 struct ceph_osd_data *osd_data)
696{
697 u64 length = ceph_osd_data_length(osd_data);
698
699 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
700 BUG_ON(length > (u64) SIZE_MAX);
701 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500702 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500703 length, osd_data->alignment);
704 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
705 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500706 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500707#ifdef CONFIG_BLOCK
708 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500709 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500710#endif
711 } else {
712 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
713 }
714}
715
Alex Elder175face2013-03-08 13:35:36 -0600716static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500717 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700718{
Alex Elder79528732013-04-03 21:32:51 -0500719 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500720 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500721 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500722 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600723
Alex Elder79528732013-04-03 21:32:51 -0500724 BUG_ON(which >= req->r_num_ops);
725 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500726 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
727 pr_err("unrecognized osd opcode %d\n", src->op);
728
729 return 0;
730 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700731
Alex Elder065a68f2012-04-20 15:49:43 -0500732 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600733 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600734 osd_data = &src->raw_data_in;
735 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600736 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700737 case CEPH_OSD_OP_READ:
738 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200739 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800740 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800741 case CEPH_OSD_OP_TRUNCATE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200742 if (src->op == CEPH_OSD_OP_WRITE ||
743 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder54d50642013-04-03 01:28:58 -0500744 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600745 dst->extent.offset = cpu_to_le64(src->extent.offset);
746 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700747 dst->extent.truncate_size =
748 cpu_to_le64(src->extent.truncate_size);
749 dst->extent.truncate_seq =
750 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500751 osd_data = &src->extent.osd_data;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200752 if (src->op == CEPH_OSD_OP_WRITE ||
753 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder04017e22013-04-05 14:46:02 -0500754 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500755 else
Alex Elder04017e22013-04-05 14:46:02 -0500756 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700757 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700758 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700759 dst->cls.class_len = src->cls.class_len;
760 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500761 osd_data = &src->cls.request_info;
762 ceph_osdc_msg_data_add(req->r_request, osd_data);
763 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
764 request_data_len = osd_data->pagelist->length;
765
766 osd_data = &src->cls.request_data;
767 data_length = ceph_osd_data_length(osd_data);
768 if (data_length) {
769 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
770 dst->cls.indata_len = cpu_to_le32(data_length);
771 ceph_osdc_msg_data_add(req->r_request, osd_data);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100772 src->indata_len += data_length;
Alex Elder04017e22013-04-05 14:46:02 -0500773 request_data_len += data_length;
774 }
775 osd_data = &src->cls.response_data;
776 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700777 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700778 case CEPH_OSD_OP_STARTSYNC:
779 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700780 case CEPH_OSD_OP_NOTIFY_ACK:
781 case CEPH_OSD_OP_WATCH:
782 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
783 dst->watch.ver = cpu_to_le64(src->watch.ver);
784 dst->watch.flag = src->watch.flag;
785 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200786 case CEPH_OSD_OP_SETALLOCHINT:
787 dst->alloc_hint.expected_object_size =
788 cpu_to_le64(src->alloc_hint.expected_object_size);
789 dst->alloc_hint.expected_write_size =
790 cpu_to_le64(src->alloc_hint.expected_write_size);
791 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800792 case CEPH_OSD_OP_SETXATTR:
793 case CEPH_OSD_OP_CMPXATTR:
794 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
795 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
796 dst->xattr.cmp_op = src->xattr.cmp_op;
797 dst->xattr.cmp_mode = src->xattr.cmp_mode;
798 osd_data = &src->xattr.osd_data;
799 ceph_osdc_msg_data_add(req->r_request, osd_data);
800 request_data_len = osd_data->pagelist->length;
801 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800802 case CEPH_OSD_OP_CREATE:
803 case CEPH_OSD_OP_DELETE:
804 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700805 default:
Alex Elder4c464592013-02-15 11:42:30 -0600806 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600807 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600808 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500809
810 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700811 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200812
Alex Eldera8dd0a32013-03-13 20:50:00 -0500813 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200814 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100815 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600816
Alex Elder54d50642013-04-03 01:28:58 -0500817 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700818}
819
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700820/*
Sage Weilf24e9982009-10-06 11:31:10 -0700821 * build new request AND message, calculate layout, and adjust file
822 * extent as needed.
823 *
824 * if the file was recently truncated, we include information about its
825 * old and new size so that the object can be updated appropriately. (we
826 * avoid synchronously deleting truncated objects because it's slow.)
827 *
828 * if @do_sync, include a 'startsync' command so that the osd will flush
829 * data quickly.
830 */
831struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
832 struct ceph_file_layout *layout,
833 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800834 u64 off, u64 *plen,
835 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700836 int opcode, int flags,
837 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700838 u32 truncate_seq,
839 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600840 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700841{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700842 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500843 u64 objnum = 0;
844 u64 objoff = 0;
845 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700846 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700847
Li Wangad7a60d2013-08-15 11:51:44 +0800848 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800849 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
850 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700851
Alex Elderacead002013-03-14 14:09:05 -0500852 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600853 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200854 if (!req) {
855 r = -ENOMEM;
856 goto fail;
857 }
Alex Elder79528732013-04-03 21:32:51 -0500858
Alex Elderd178a9e2012-11-13 21:11:15 -0600859 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700860
861 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500862 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200863 if (r)
864 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -0500865
Yan, Zheng864e9192014-11-13 10:47:25 +0800866 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800867 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800868 } else {
869 u32 object_size = le32_to_cpu(layout->fl_object_size);
870 u32 object_base = off - objoff;
871 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
872 if (truncate_size <= object_base) {
873 truncate_size = 0;
874 } else {
875 truncate_size -= object_base;
876 if (truncate_size > object_size)
877 truncate_size = object_size;
878 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800879 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800880 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800881 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500882 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500883
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200884 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200885 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600886
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200887 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
888 if (r)
889 goto fail;
890
Sage Weilf24e9982009-10-06 11:31:10 -0700891 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200892
893fail:
894 ceph_osdc_put_request(req);
895 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700896}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700897EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700898
899/*
900 * We keep osd requests in an rbtree, sorted by ->r_tid.
901 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200902DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
Sage Weilf24e9982009-10-06 11:31:10 -0700903
904static struct ceph_osd_request *
905__lookup_request_ge(struct ceph_osd_client *osdc,
906 u64 tid)
907{
908 struct ceph_osd_request *req;
909 struct rb_node *n = osdc->requests.rb_node;
910
911 while (n) {
912 req = rb_entry(n, struct ceph_osd_request, r_node);
913 if (tid < req->r_tid) {
914 if (!n->rb_left)
915 return req;
916 n = n->rb_left;
917 } else if (tid > req->r_tid) {
918 n = n->rb_right;
919 } else {
920 return req;
921 }
922 }
923 return NULL;
924}
925
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400926static void __kick_linger_request(struct ceph_osd_request *req)
927{
928 struct ceph_osd_client *osdc = req->r_osdc;
929 struct ceph_osd *osd = req->r_osd;
930
931 /*
932 * Linger requests need to be resent with a new tid to avoid
933 * the dup op detection logic on the OSDs. Achieve this with
934 * a re-register dance instead of open-coding.
935 */
936 ceph_osdc_get_request(req);
937 if (!list_empty(&req->r_linger_item))
938 __unregister_linger_request(osdc, req);
939 else
940 __unregister_request(osdc, req);
941 __register_request(osdc, req);
942 ceph_osdc_put_request(req);
943
944 /*
945 * Unless request has been registered as both normal and
946 * lingering, __unregister{,_linger}_request clears r_osd.
947 * However, here we need to preserve r_osd to make sure we
948 * requeue on the same OSD.
949 */
950 WARN_ON(req->r_osd || !osd);
951 req->r_osd = osd;
952
953 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
954 __enqueue_request(req);
955}
956
Sage Weil6f6c7002011-01-17 20:34:08 -0800957/*
958 * Resubmit requests pending on the given osd.
959 */
960static void __kick_osd_requests(struct ceph_osd_client *osdc,
961 struct ceph_osd *osd)
962{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700963 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500964 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400965 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800966 int err;
967
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400968 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800969 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600970 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800971 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400972
Alex Eldere02493c2013-03-25 18:16:11 -0500973 /*
974 * Build up a list of requests to resend by traversing the
975 * osd's list of requests. Requests for a given object are
976 * sent in tid order, and that is also the order they're
977 * kept on this list. Therefore all requests that are in
978 * flight will be found first, followed by all requests that
979 * have not yet been sent. And to resend requests while
980 * preserving this order we will want to put any sent
981 * requests back on the front of the osd client's unsent
982 * list.
983 *
984 * So we build a separate ordered list of already-sent
985 * requests for the affected osd and splice it onto the
986 * front of the osd client's unsent list. Once we've seen a
987 * request that has not yet been sent we're done. Those
988 * requests are already sitting right where they belong.
989 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800990 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500991 if (!req->r_sent)
992 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400993
994 if (!req->r_linger) {
995 dout("%s requeueing %p tid %llu\n", __func__, req,
996 req->r_tid);
997 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700998 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400999 } else {
1000 list_move_tail(&req->r_req_lru_item, &resend_linger);
1001 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001002 }
Alex Eldere02493c2013-03-25 18:16:11 -05001003 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001004
Alex Eldere02493c2013-03-25 18:16:11 -05001005 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001006 * Both registered and not yet registered linger requests are
1007 * enqueued with a new tid on the same OSD. We add/move them
1008 * to req_unsent/o_requests at the end to keep things in tid
1009 * order.
Alex Eldere02493c2013-03-25 18:16:11 -05001010 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001011 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001012 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001013 WARN_ON(!list_empty(&req->r_req_lru_item));
1014 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001015 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001016
1017 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
1018 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001019}
1020
Sage Weilf24e9982009-10-06 11:31:10 -07001021/*
Sage Weil81b024e2009-10-09 10:29:18 -07001022 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -07001023 */
1024static void osd_reset(struct ceph_connection *con)
1025{
1026 struct ceph_osd *osd = con->private;
1027 struct ceph_osd_client *osdc;
1028
1029 if (!osd)
1030 return;
1031 dout("osd_reset osd%d\n", osd->o_osd);
1032 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001033 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001034 mutex_lock(&osdc->request_mutex);
1035 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001036 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001037 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001038 up_read(&osdc->map_sem);
1039}
1040
1041/*
1042 * Track open sessions with osds.
1043 */
Alex Eldere10006f2012-05-26 23:26:43 -05001044static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001045{
1046 struct ceph_osd *osd;
1047
1048 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1049 if (!osd)
1050 return NULL;
1051
1052 atomic_set(&osd->o_ref, 1);
1053 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001054 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001055 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001056 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001057 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001058 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001059 osd->o_incarnation = 1;
1060
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001061 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001062
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001063 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001064 return osd;
1065}
1066
1067static struct ceph_osd *get_osd(struct ceph_osd *osd)
1068{
1069 if (atomic_inc_not_zero(&osd->o_ref)) {
1070 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1071 atomic_read(&osd->o_ref));
1072 return osd;
1073 } else {
1074 dout("get_osd %p FAIL\n", osd);
1075 return NULL;
1076 }
1077}
1078
1079static void put_osd(struct ceph_osd *osd)
1080{
1081 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1082 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001083 if (atomic_dec_and_test(&osd->o_ref)) {
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001084 if (osd->o_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02001085 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001086 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001087 }
Sage Weilf24e9982009-10-06 11:31:10 -07001088}
1089
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001090DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1091
Sage Weilf24e9982009-10-06 11:31:10 -07001092/*
1093 * remove an osd from our map
1094 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001095static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001096{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001097 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001098 WARN_ON(!list_empty(&osd->o_requests));
1099 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001100
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001101 list_del_init(&osd->o_osd_lru);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001102 erase_osd(&osdc->osds, osd);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001103}
1104
1105static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1106{
1107 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1108
1109 if (!RB_EMPTY_NODE(&osd->o_node)) {
1110 ceph_con_close(&osd->o_con);
1111 __remove_osd(osdc, osd);
1112 put_osd(osd);
1113 }
Sage Weilf24e9982009-10-06 11:31:10 -07001114}
1115
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001116static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1117 struct ceph_osd *osd)
1118{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001119 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001120 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001121
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001122 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001123 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001124}
1125
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001126static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1127 struct ceph_osd *osd)
1128{
1129 dout("%s %p\n", __func__, osd);
1130
1131 if (list_empty(&osd->o_requests) &&
1132 list_empty(&osd->o_linger_requests))
1133 __move_osd_to_lru(osdc, osd);
1134}
1135
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001136static void __remove_osd_from_lru(struct ceph_osd *osd)
1137{
1138 dout("__remove_osd_from_lru %p\n", osd);
1139 if (!list_empty(&osd->o_osd_lru))
1140 list_del_init(&osd->o_osd_lru);
1141}
1142
Sage Weilf24e9982009-10-06 11:31:10 -07001143/*
1144 * reset osd connect
1145 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001146static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001147{
Alex Elderc3acb182012-12-07 09:57:58 -06001148 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001149
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001150 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001151 if (list_empty(&osd->o_requests) &&
1152 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001153 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001154 return -ENODEV;
1155 }
1156
1157 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1158 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1159 !ceph_con_opened(&osd->o_con)) {
1160 struct ceph_osd_request *req;
1161
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001162 dout("osd addr hasn't changed and connection never opened, "
1163 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001164 /* touch each r_stamp for handle_timeout()'s benfit */
1165 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1166 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001167
1168 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001169 }
Alex Elderc3acb182012-12-07 09:57:58 -06001170
1171 ceph_con_close(&osd->o_con);
1172 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1173 osd->o_incarnation++;
1174
1175 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001176}
1177
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001178static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1179{
1180 schedule_delayed_work(&osdc->timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03001181 osdc->client->options->osd_keepalive_timeout);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001182}
1183
1184static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1185{
1186 cancel_delayed_work(&osdc->timeout_work);
1187}
Sage Weilf24e9982009-10-06 11:31:10 -07001188
1189/*
1190 * Register request, assign tid. If this is the first request, set up
1191 * the timeout event.
1192 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001193static void __register_request(struct ceph_osd_client *osdc,
1194 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001195{
Sage Weilf24e9982009-10-06 11:31:10 -07001196 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001197 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001198 dout("__register_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001199 insert_request(&osdc->requests, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001200 ceph_osdc_get_request(req);
1201 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001202 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001203 dout(" first request, scheduling timeout\n");
1204 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001205 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001206}
1207
Sage Weilf24e9982009-10-06 11:31:10 -07001208/*
1209 * called under osdc->request_mutex
1210 */
1211static void __unregister_request(struct ceph_osd_client *osdc,
1212 struct ceph_osd_request *req)
1213{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001214 if (RB_EMPTY_NODE(&req->r_node)) {
1215 dout("__unregister_request %p tid %lld not registered\n",
1216 req, req->r_tid);
1217 return;
1218 }
1219
Sage Weilf24e9982009-10-06 11:31:10 -07001220 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001221 erase_request(&osdc->requests, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001222 osdc->num_requests--;
1223
Sage Weil0ba64782009-10-08 16:57:16 -07001224 if (req->r_osd) {
1225 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001226 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001227
1228 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001229 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001230 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001231 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001232 }
Sage Weilf24e9982009-10-06 11:31:10 -07001233
Alex Elder7d5f2482012-11-29 08:37:03 -06001234 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001235 ceph_osdc_put_request(req);
1236
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001237 if (osdc->num_requests == 0) {
1238 dout(" no requests, canceling timeout\n");
1239 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001240 }
1241}
1242
1243/*
1244 * Cancel a previously queued request message
1245 */
1246static void __cancel_request(struct ceph_osd_request *req)
1247{
Sage Weil6bc18872010-09-27 10:18:52 -07001248 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001249 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001250 req->r_sent = 0;
1251 }
1252}
1253
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001254static void __register_linger_request(struct ceph_osd_client *osdc,
1255 struct ceph_osd_request *req)
1256{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001257 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1258 WARN_ON(!req->r_linger);
1259
Alex Elder96e4dac2013-05-22 20:54:25 -05001260 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001261 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001262 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001263 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001264 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001265}
1266
1267static void __unregister_linger_request(struct ceph_osd_client *osdc,
1268 struct ceph_osd_request *req)
1269{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001270 WARN_ON(!req->r_linger);
1271
1272 if (list_empty(&req->r_linger_item)) {
1273 dout("%s %p tid %llu not registered\n", __func__, req,
1274 req->r_tid);
1275 return;
1276 }
1277
1278 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001279 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001280
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001281 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001282 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001283 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001284 if (list_empty(&req->r_osd_item))
1285 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001286 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001287 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001288}
1289
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001290void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1291 struct ceph_osd_request *req)
1292{
1293 if (!req->r_linger) {
1294 dout("set_request_linger %p\n", req);
1295 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001296 }
1297}
1298EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1299
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001300static bool __pool_full(struct ceph_pg_pool_info *pi)
1301{
1302 return pi->flags & CEPH_POOL_FLAG_FULL;
1303}
1304
Sage Weilf24e9982009-10-06 11:31:10 -07001305/*
Josh Durgind29adb32013-12-02 19:11:48 -08001306 * Returns whether a request should be blocked from being sent
1307 * based on the current osdmap and osd_client settings.
1308 *
1309 * Caller should hold map_sem for read.
1310 */
1311static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1312 struct ceph_osd_request *req)
1313{
1314 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1315 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1316 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1317 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1318 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1319}
1320
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001321static bool target_should_be_paused(struct ceph_osd_client *osdc,
1322 const struct ceph_osd_request_target *t,
1323 struct ceph_pg_pool_info *pi)
1324{
1325 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1326 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1327 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
1328 __pool_full(pi);
1329
1330 WARN_ON(pi->id != t->base_oloc.pool);
1331 return (t->flags & CEPH_OSD_FLAG_READ && pauserd) ||
1332 (t->flags & CEPH_OSD_FLAG_WRITE && pausewr);
1333}
1334
Josh Durgind29adb32013-12-02 19:11:48 -08001335/*
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001336 * Calculate mapping of a request to a PG. Takes tiering into account.
1337 */
1338static int __calc_request_pg(struct ceph_osdmap *osdmap,
1339 struct ceph_osd_request *req,
1340 struct ceph_pg *pg_out)
1341{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001342 bool need_check_tiering;
1343
1344 need_check_tiering = false;
1345 if (req->r_target_oloc.pool == -1) {
1346 req->r_target_oloc = req->r_base_oloc; /* struct */
1347 need_check_tiering = true;
1348 }
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001349 if (ceph_oid_empty(&req->r_target_oid)) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001350 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1351 need_check_tiering = true;
1352 }
1353
1354 if (need_check_tiering &&
1355 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001356 struct ceph_pg_pool_info *pi;
1357
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001358 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001359 if (pi) {
1360 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1361 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001362 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001363 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1364 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001365 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001366 }
1367 /* !pi is caught in ceph_oloc_oid_to_pg() */
1368 }
1369
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001370 return ceph_object_locator_to_pg(osdmap, &req->r_target_oid,
1371 &req->r_target_oloc, pg_out);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001372}
1373
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001374enum calc_target_result {
1375 CALC_TARGET_NO_ACTION = 0,
1376 CALC_TARGET_NEED_RESEND,
1377 CALC_TARGET_POOL_DNE,
1378};
1379
1380static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1381 struct ceph_osd_request_target *t,
1382 u32 *last_force_resend,
1383 bool any_change)
1384{
1385 struct ceph_pg_pool_info *pi;
1386 struct ceph_pg pgid, last_pgid;
1387 struct ceph_osds up, acting;
1388 bool force_resend = false;
1389 bool need_check_tiering = false;
1390 bool need_resend = false;
1391 bool sort_bitwise = ceph_osdmap_flag(osdc->osdmap,
1392 CEPH_OSDMAP_SORTBITWISE);
1393 enum calc_target_result ct_res;
1394 int ret;
1395
1396 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1397 if (!pi) {
1398 t->osd = CEPH_HOMELESS_OSD;
1399 ct_res = CALC_TARGET_POOL_DNE;
1400 goto out;
1401 }
1402
1403 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1404 if (last_force_resend &&
1405 *last_force_resend < pi->last_force_request_resend) {
1406 *last_force_resend = pi->last_force_request_resend;
1407 force_resend = true;
1408 } else if (!last_force_resend) {
1409 force_resend = true;
1410 }
1411 }
1412 if (ceph_oid_empty(&t->target_oid) || force_resend) {
1413 ceph_oid_copy(&t->target_oid, &t->base_oid);
1414 need_check_tiering = true;
1415 }
1416 if (ceph_oloc_empty(&t->target_oloc) || force_resend) {
1417 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1418 need_check_tiering = true;
1419 }
1420
1421 if (need_check_tiering &&
1422 (t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1423 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1424 t->target_oloc.pool = pi->read_tier;
1425 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1426 t->target_oloc.pool = pi->write_tier;
1427 }
1428
1429 ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1430 &t->target_oloc, &pgid);
1431 if (ret) {
1432 WARN_ON(ret != -ENOENT);
1433 t->osd = CEPH_HOMELESS_OSD;
1434 ct_res = CALC_TARGET_POOL_DNE;
1435 goto out;
1436 }
1437 last_pgid.pool = pgid.pool;
1438 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1439
1440 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1441 if (any_change &&
1442 ceph_is_new_interval(&t->acting,
1443 &acting,
1444 &t->up,
1445 &up,
1446 t->size,
1447 pi->size,
1448 t->min_size,
1449 pi->min_size,
1450 t->pg_num,
1451 pi->pg_num,
1452 t->sort_bitwise,
1453 sort_bitwise,
1454 &last_pgid))
1455 force_resend = true;
1456
1457 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1458 t->paused = false;
1459 need_resend = true;
1460 }
1461
1462 if (ceph_pg_compare(&t->pgid, &pgid) ||
1463 ceph_osds_changed(&t->acting, &acting, any_change) ||
1464 force_resend) {
1465 t->pgid = pgid; /* struct */
1466 ceph_osds_copy(&t->acting, &acting);
1467 ceph_osds_copy(&t->up, &up);
1468 t->size = pi->size;
1469 t->min_size = pi->min_size;
1470 t->pg_num = pi->pg_num;
1471 t->pg_num_mask = pi->pg_num_mask;
1472 t->sort_bitwise = sort_bitwise;
1473
1474 t->osd = acting.primary;
1475 need_resend = true;
1476 }
1477
1478 ct_res = need_resend ? CALC_TARGET_NEED_RESEND : CALC_TARGET_NO_ACTION;
1479out:
1480 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1481 return ct_res;
1482}
1483
Ilya Dryomovf671b582014-09-02 13:40:33 +04001484static void __enqueue_request(struct ceph_osd_request *req)
1485{
1486 struct ceph_osd_client *osdc = req->r_osdc;
1487
1488 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1489 req->r_osd ? req->r_osd->o_osd : -1);
1490
1491 if (req->r_osd) {
1492 __remove_osd_from_lru(req->r_osd);
1493 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1494 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1495 } else {
1496 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1497 }
1498}
1499
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001500/*
Sage Weilf24e9982009-10-06 11:31:10 -07001501 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1502 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001503 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001504 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001505 *
1506 * Return 0 if unchanged, 1 if changed, or negative on error.
1507 *
1508 * Caller should hold map_sem for read and request_mutex.
1509 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001510static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001511 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001512{
Sage Weil5b191d92013-02-23 10:38:16 -08001513 struct ceph_pg pgid;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001514 struct ceph_osds up, acting;
Sage Weilf24e9982009-10-06 11:31:10 -07001515 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001516 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001517
Sage Weil6f6c7002011-01-17 20:34:08 -08001518 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001519
1520 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001521 if (err) {
1522 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001523 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001524 }
Sage Weil7740a422010-01-08 15:58:25 -08001525 req->r_pgid = pgid;
1526
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001527 ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
Sage Weilf24e9982009-10-06 11:31:10 -07001528
Josh Durgind29adb32013-12-02 19:11:48 -08001529 was_paused = req->r_paused;
1530 req->r_paused = __req_should_be_paused(osdc, req);
1531 if (was_paused && !req->r_paused)
1532 force_resend = 1;
1533
Sage Weil38d64532011-10-14 13:33:55 -07001534 if ((!force_resend &&
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001535 req->r_osd && req->r_osd->o_osd == acting.primary &&
Sage Weild85b7052010-05-10 10:24:48 -07001536 req->r_sent >= req->r_osd->o_incarnation &&
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001537 req->r_num_pg_osds == acting.size &&
1538 memcmp(req->r_pg_osds, acting.osds,
1539 acting.size * sizeof(acting.osds[0])) == 0) ||
1540 (req->r_osd == NULL && acting.primary == -1) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001541 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001542 return 0; /* no change */
1543
Sage Weil5b191d92013-02-23 10:38:16 -08001544 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001545 req->r_tid, pgid.pool, pgid.seed, acting.primary,
Sage Weilf24e9982009-10-06 11:31:10 -07001546 req->r_osd ? req->r_osd->o_osd : -1);
1547
Sage Weild85b7052010-05-10 10:24:48 -07001548 /* record full pg acting set */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001549 memcpy(req->r_pg_osds, acting.osds,
1550 acting.size * sizeof(acting.osds[0]));
1551 req->r_num_pg_osds = acting.size;
Sage Weild85b7052010-05-10 10:24:48 -07001552
Sage Weilf24e9982009-10-06 11:31:10 -07001553 if (req->r_osd) {
1554 __cancel_request(req);
1555 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001556 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001557 req->r_osd = NULL;
1558 }
1559
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001560 req->r_osd = lookup_osd(&osdc->osds, acting.primary);
1561 if (!req->r_osd && acting.primary >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001562 err = -ENOMEM;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001563 req->r_osd = create_osd(osdc, acting.primary);
Sage Weil6f6c7002011-01-17 20:34:08 -08001564 if (!req->r_osd) {
1565 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001566 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001567 }
Sage Weilf24e9982009-10-06 11:31:10 -07001568
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001569 dout("map_request osd %p is osd%d\n", req->r_osd,
1570 acting.primary);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001571 insert_osd(&osdc->osds, req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001572
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001573 ceph_con_open(&req->r_osd->o_con,
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001574 CEPH_ENTITY_TYPE_OSD, acting.primary,
1575 &osdc->osdmap->osd_addr[acting.primary]);
Sage Weilf24e9982009-10-06 11:31:10 -07001576 }
1577
Ilya Dryomovf671b582014-09-02 13:40:33 +04001578 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001579 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001580
1581out:
Sage Weilf24e9982009-10-06 11:31:10 -07001582 return err;
1583}
1584
1585/*
1586 * caller should hold map_sem (for read) and request_mutex
1587 */
Sage Weil56e925b2012-01-03 12:34:34 -08001588static void __send_request(struct ceph_osd_client *osdc,
1589 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001590{
Sage Weil1b83bef2013-02-25 16:11:12 -08001591 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001592
Sage Weil1b83bef2013-02-25 16:11:12 -08001593 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1594 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1595 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001596
Sage Weil1b83bef2013-02-25 16:11:12 -08001597 /* fill in message content that changes each time we send it */
1598 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1599 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001600 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001601 p = req->r_request_pgid;
1602 ceph_encode_64(&p, req->r_pgid.pool);
1603 ceph_encode_32(&p, req->r_pgid.seed);
1604 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1605 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1606 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001607
Sage Weil3dd72fc2010-03-22 14:42:30 -07001608 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001609 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001610
1611 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001612
Sage Weilf24e9982009-10-06 11:31:10 -07001613 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001614
1615 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001616}
1617
1618/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001619 * Send any requests in the queue (req_unsent).
1620 */
Alex Elderf9d25192013-02-15 11:42:29 -06001621static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001622{
1623 struct ceph_osd_request *req, *tmp;
1624
Alex Elderf9d25192013-02-15 11:42:29 -06001625 dout("__send_queued\n");
1626 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001627 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001628}
1629
1630/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001631 * Caller should hold map_sem for read and request_mutex.
1632 */
1633static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1634 struct ceph_osd_request *req,
1635 bool nofail)
1636{
1637 int rc;
1638
1639 __register_request(osdc, req);
1640 req->r_sent = 0;
1641 req->r_got_reply = 0;
1642 rc = __map_request(osdc, req, 0);
1643 if (rc < 0) {
1644 if (nofail) {
1645 dout("osdc_start_request failed map, "
1646 " will retry %lld\n", req->r_tid);
1647 rc = 0;
1648 } else {
1649 __unregister_request(osdc, req);
1650 }
1651 return rc;
1652 }
1653
1654 if (req->r_osd == NULL) {
1655 dout("send_request %p no up osds in pg\n", req);
1656 ceph_monc_request_next_osdmap(&osdc->client->monc);
1657 } else {
1658 __send_queued(osdc);
1659 }
1660
1661 return 0;
1662}
1663
1664/*
Sage Weilf24e9982009-10-06 11:31:10 -07001665 * Timeout callback, called every N seconds when 1 or more osd
1666 * requests has been active for more than N seconds. When this
1667 * happens, we ping all OSDs with requests who have timed out to
1668 * ensure any communications channel reset is detected. Reset the
1669 * request timeouts another N seconds in the future as we go.
1670 * Reschedule the timeout event another N seconds in future (unless
1671 * there are no open requests).
1672 */
1673static void handle_timeout(struct work_struct *work)
1674{
1675 struct ceph_osd_client *osdc =
1676 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001677 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001678 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001679 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001680 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001681 dout("timeout\n");
1682 down_read(&osdc->map_sem);
1683
1684 ceph_monc_request_next_osdmap(&osdc->client->monc);
1685
1686 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001687
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001688 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001689 * ping osds that are a bit slow. this ensures that if there
1690 * is a break in the TCP connection we will notice, and reopen
1691 * a connection with that osd (from the fault callback).
1692 */
1693 INIT_LIST_HEAD(&slow_osds);
1694 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001695 if (time_before(jiffies,
1696 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001697 break;
1698
1699 osd = req->r_osd;
1700 BUG_ON(!osd);
1701 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001702 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001703 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1704 }
1705 while (!list_empty(&slow_osds)) {
1706 osd = list_entry(slow_osds.next, struct ceph_osd,
1707 o_keepalive_item);
1708 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001709 ceph_con_keepalive(&osd->o_con);
1710 }
1711
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001712 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001713 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001714 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001715 up_read(&osdc->map_sem);
1716}
1717
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001718static void handle_osds_timeout(struct work_struct *work)
1719{
1720 struct ceph_osd_client *osdc =
1721 container_of(work, struct ceph_osd_client,
1722 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001723 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001724 struct ceph_osd *osd, *nosd;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001725
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001726 dout("%s osdc %p\n", __func__, osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001727 down_read(&osdc->map_sem);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001728 mutex_lock(&osdc->request_mutex);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001729
Ilya Dryomov42a2c092016-04-28 16:07:22 +02001730 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1731 if (time_before(jiffies, osd->lru_ttl))
1732 break;
1733
1734 remove_osd(osdc, osd);
1735 }
1736
1737 mutex_unlock(&osdc->request_mutex);
1738 up_read(&osdc->map_sem);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001739 schedule_delayed_work(&osdc->osds_timeout_work,
1740 round_jiffies_relative(delay));
1741}
1742
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001743static int ceph_oloc_decode(void **p, void *end,
1744 struct ceph_object_locator *oloc)
1745{
1746 u8 struct_v, struct_cv;
1747 u32 len;
1748 void *struct_end;
1749 int ret = 0;
1750
1751 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1752 struct_v = ceph_decode_8(p);
1753 struct_cv = ceph_decode_8(p);
1754 if (struct_v < 3) {
1755 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1756 struct_v, struct_cv);
1757 goto e_inval;
1758 }
1759 if (struct_cv > 6) {
1760 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1761 struct_v, struct_cv);
1762 goto e_inval;
1763 }
1764 len = ceph_decode_32(p);
1765 ceph_decode_need(p, end, len, e_inval);
1766 struct_end = *p + len;
1767
1768 oloc->pool = ceph_decode_64(p);
1769 *p += 4; /* skip preferred */
1770
1771 len = ceph_decode_32(p);
1772 if (len > 0) {
1773 pr_warn("ceph_object_locator::key is set\n");
1774 goto e_inval;
1775 }
1776
1777 if (struct_v >= 5) {
1778 len = ceph_decode_32(p);
1779 if (len > 0) {
1780 pr_warn("ceph_object_locator::nspace is set\n");
1781 goto e_inval;
1782 }
1783 }
1784
1785 if (struct_v >= 6) {
1786 s64 hash = ceph_decode_64(p);
1787 if (hash != -1) {
1788 pr_warn("ceph_object_locator::hash is set\n");
1789 goto e_inval;
1790 }
1791 }
1792
1793 /* skip the rest */
1794 *p = struct_end;
1795out:
1796 return ret;
1797
1798e_inval:
1799 ret = -EINVAL;
1800 goto out;
1801}
1802
1803static int ceph_redirect_decode(void **p, void *end,
1804 struct ceph_request_redirect *redir)
1805{
1806 u8 struct_v, struct_cv;
1807 u32 len;
1808 void *struct_end;
1809 int ret;
1810
1811 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1812 struct_v = ceph_decode_8(p);
1813 struct_cv = ceph_decode_8(p);
1814 if (struct_cv > 1) {
1815 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1816 struct_v, struct_cv);
1817 goto e_inval;
1818 }
1819 len = ceph_decode_32(p);
1820 ceph_decode_need(p, end, len, e_inval);
1821 struct_end = *p + len;
1822
1823 ret = ceph_oloc_decode(p, end, &redir->oloc);
1824 if (ret)
1825 goto out;
1826
1827 len = ceph_decode_32(p);
1828 if (len > 0) {
1829 pr_warn("ceph_request_redirect::object_name is set\n");
1830 goto e_inval;
1831 }
1832
1833 len = ceph_decode_32(p);
1834 *p += len; /* skip osd_instructions */
1835
1836 /* skip the rest */
1837 *p = struct_end;
1838out:
1839 return ret;
1840
1841e_inval:
1842 ret = -EINVAL;
1843 goto out;
1844}
1845
Sage Weil25845472011-06-03 09:37:09 -07001846static void complete_request(struct ceph_osd_request *req)
1847{
Sage Weil25845472011-06-03 09:37:09 -07001848 complete_all(&req->r_safe_completion); /* fsync waiter */
1849}
1850
Sage Weilf24e9982009-10-06 11:31:10 -07001851/*
1852 * handle osd op reply. either call the callback if it is specified,
1853 * or do the completion to wake up the waiting thread.
1854 */
Shraddha Barke70cf0522015-10-18 13:55:38 +05301855static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001856{
Sage Weil1b83bef2013-02-25 16:11:12 -08001857 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001858 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001859 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001860 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001861 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001862 unsigned int numops;
1863 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001864 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001865 s32 retry_attempt;
1866 struct ceph_pg pg;
1867 int err;
1868 u32 reassert_epoch;
1869 u64 reassert_version;
1870 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001871 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001872 u32 bytes;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001873 u8 decode_redir;
Alex Elder79528732013-04-03 21:32:51 -05001874 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001875
Sage Weil6df058c2009-12-22 11:24:33 -08001876 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001877 dout("handle_reply %p tid %llu\n", msg, tid);
1878
1879 p = msg->front.iov_base;
1880 end = p + msg->front.iov_len;
1881
1882 ceph_decode_need(&p, end, 4, bad);
1883 object_len = ceph_decode_32(&p);
1884 ceph_decode_need(&p, end, object_len, bad);
1885 p += object_len;
1886
Alex Elderef4859d2013-04-01 18:58:26 -05001887 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001888 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001889 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001890
1891 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1892 flags = ceph_decode_64(&p);
1893 result = ceph_decode_32(&p);
1894 reassert_epoch = ceph_decode_32(&p);
1895 reassert_version = ceph_decode_64(&p);
1896 osdmap_epoch = ceph_decode_32(&p);
1897
Sage Weilf24e9982009-10-06 11:31:10 -07001898 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001899 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001900 mutex_lock(&osdc->request_mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001901 req = lookup_request(&osdc->requests, tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001902 if (req == NULL) {
1903 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001904 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001905 }
1906 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001907
1908 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1909 req, result);
1910
Dan Carpenter18741192013-08-15 08:51:58 +03001911 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001912 numops = ceph_decode_32(&p);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01001913 if (numops > CEPH_OSD_MAX_OPS)
Sage Weil1b83bef2013-02-25 16:11:12 -08001914 goto bad_put;
1915 if (numops != req->r_num_ops)
1916 goto bad_put;
1917 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001918 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001919 for (i = 0; i < numops; i++) {
1920 struct ceph_osd_op *op = p;
1921 int len;
1922
1923 len = le32_to_cpu(op->payload_len);
Yan, Zheng7665d852016-01-07 16:48:57 +08001924 req->r_ops[i].outdata_len = len;
Sage Weil1b83bef2013-02-25 16:11:12 -08001925 dout(" op %d has %d bytes\n", i, len);
1926 payload_len += len;
1927 p += sizeof(*op);
1928 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001929 bytes = le32_to_cpu(msg->hdr.data_len);
1930 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001931 pr_warn("sum of op payload lens %d != data_len %d\n",
1932 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001933 goto bad_put;
1934 }
1935
Dan Carpenter18741192013-08-15 08:51:58 +03001936 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001937 retry_attempt = ceph_decode_32(&p);
1938 for (i = 0; i < numops; i++)
Yan, Zheng7665d852016-01-07 16:48:57 +08001939 req->r_ops[i].rval = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001940
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001941 if (le16_to_cpu(msg->hdr.version) >= 6) {
1942 p += 8 + 4; /* skip replay_version */
1943 p += 8; /* skip user_version */
1944
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001945 if (le16_to_cpu(msg->hdr.version) >= 7)
1946 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1947 else
1948 decode_redir = 1;
1949 } else {
1950 decode_redir = 0;
1951 }
1952
1953 if (decode_redir) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001954 err = ceph_redirect_decode(&p, end, &redir);
1955 if (err)
1956 goto bad_put;
1957 } else {
1958 redir.oloc.pool = -1;
1959 }
1960
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001961 if (!ceph_oloc_empty(&redir.oloc)) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001962 dout("redirect pool %lld\n", redir.oloc.pool);
1963
1964 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001965
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001966 ceph_oloc_copy(&req->r_target_oloc, &redir.oloc);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001967
1968 /*
1969 * Start redirect requests with nofail=true. If
1970 * mapping fails, request will end up on the notarget
1971 * list, waiting for the new osdmap (which can take
1972 * a while), even though the original request mapped
1973 * successfully. In the future we might want to follow
1974 * original request's nofail setting here.
1975 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001976 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001977 BUG_ON(err);
1978
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001979 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001980 }
1981
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001982 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001983 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001984 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001985 dout("handle_reply result %d bytes %d\n", req->r_result,
1986 bytes);
1987 if (req->r_result == 0)
1988 req->r_result = bytes;
1989
1990 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001991 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1992 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001993
1994 req->r_got_reply = 1;
1995 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1996 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001997 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001998 }
1999
2000 dout("handle_reply tid %llu flags %d\n", tid, flags);
2001
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002002 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
2003 __register_linger_request(osdc, req);
2004
Sage Weilf24e9982009-10-06 11:31:10 -07002005 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07002006 if (result < 0 ||
2007 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07002008 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
2009 __unregister_request(osdc, req);
2010
2011 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02002012 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002013
Yan, Zhengeb845ff2013-05-31 15:54:44 +08002014 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08002015 if (req->r_unsafe_callback &&
2016 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
2017 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08002018 if (req->r_callback)
2019 req->r_callback(req, msg);
2020 else
2021 complete_all(&req->r_completion);
2022 }
Sage Weilf24e9982009-10-06 11:31:10 -07002023
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08002024 if (flags & CEPH_OSD_FLAG_ONDISK) {
2025 if (req->r_unsafe_callback && already_completed)
2026 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07002027 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08002028 }
Sage Weilf24e9982009-10-06 11:31:10 -07002029
Ilya Dryomovff513ac2014-02-03 13:56:33 +02002030out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002031 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002032 ceph_osdc_put_request(req);
2033 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02002034out_unlock:
2035 mutex_unlock(&osdc->request_mutex);
2036 up_read(&osdc->map_sem);
2037 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002038
Sage Weil1b83bef2013-02-25 16:11:12 -08002039bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08002040 req->r_result = -EIO;
2041 __unregister_request(osdc, req);
2042 if (req->r_callback)
2043 req->r_callback(req, msg);
2044 else
2045 complete_all(&req->r_completion);
2046 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08002047 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05002048bad_mutex:
2049 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02002050 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002051bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08002052 pr_err("corrupt osd_op_reply got %d %d\n",
2053 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08002054 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002055}
2056
Sage Weil6f6c7002011-01-17 20:34:08 -08002057static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07002058{
Sage Weilf24e9982009-10-06 11:31:10 -07002059 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07002060
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03002061 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08002062 for (p = rb_first(&osdc->osds); p; p = n) {
2063 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07002064
Sage Weil6f6c7002011-01-17 20:34:08 -08002065 n = rb_next(p);
2066 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
2067 memcmp(&osd->o_con.peer_addr,
2068 ceph_osd_addr(osdc->osdmap,
2069 osd->o_osd),
2070 sizeof(struct ceph_entity_addr)) != 0)
2071 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07002072 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002073}
2074
2075/*
Sage Weil6f6c7002011-01-17 20:34:08 -08002076 * Requeue requests whose mapping to an OSD has changed. If requests map to
2077 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002078 *
Alex Eldere6d50f62012-12-26 14:31:40 -06002079 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002080 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002081static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
2082 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002083{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002084 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08002085 struct rb_node *p;
2086 int needmap = 0;
2087 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002088 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002089
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002090 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2091 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002092 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07002093 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08002094 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07002095 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06002096
2097 /*
2098 * For linger requests that have not yet been
2099 * registered, move them to the linger list; they'll
2100 * be sent to the osd in the loop below. Unregister
2101 * the request before re-registering it as a linger
2102 * request to ensure the __map_request() below
2103 * will decide it needs to be sent.
2104 */
2105 if (req->r_linger && list_empty(&req->r_linger_item)) {
2106 dout("%p tid %llu restart on osd%d\n",
2107 req, req->r_tid,
2108 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05002109 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002110 __unregister_request(osdc, req);
2111 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002112 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002113 continue;
2114 }
2115
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002116 force_resend_req = force_resend ||
2117 (force_resend_writes &&
2118 req->r_flags & CEPH_OSD_FLAG_WRITE);
2119 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002120 if (err < 0)
2121 continue; /* error */
2122 if (req->r_osd == NULL) {
2123 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2124 needmap++; /* request a newer map */
2125 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002126 if (!req->r_linger) {
2127 dout("%p tid %llu requeued on osd%d\n", req,
2128 req->r_tid,
2129 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002130 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002131 }
2132 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002133 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002134
2135 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2136 r_linger_item) {
2137 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2138
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002139 err = __map_request(osdc, req,
2140 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002141 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002142 if (err < 0)
2143 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002144 if (req->r_osd == NULL || err > 0) {
2145 if (req->r_osd == NULL) {
2146 dout("lingering %p tid %llu maps to no osd\n",
2147 req, req->r_tid);
2148 /*
2149 * A homeless lingering request makes
2150 * no sense, as it's job is to keep
2151 * a particular OSD connection open.
2152 * Request a newer map and kick the
2153 * request, knowing that it won't be
2154 * resent until we actually get a map
2155 * that can tell us where to send it.
2156 */
2157 needmap++;
2158 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002159
Ilya Dryomovb0494532015-05-11 17:53:10 +03002160 dout("kicking lingering %p tid %llu osd%d\n", req,
2161 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2162 __register_request(osdc, req);
2163 __unregister_linger_request(osdc, req);
2164 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002165 }
Alex Elder14d2f382013-05-15 16:28:33 -05002166 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002167 mutex_unlock(&osdc->request_mutex);
2168
2169 if (needmap) {
2170 dout("%d requests for down osds, need new map\n", needmap);
2171 ceph_monc_request_next_osdmap(&osdc->client->monc);
2172 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002173}
Sage Weil6f6c7002011-01-17 20:34:08 -08002174
2175
Sage Weilf24e9982009-10-06 11:31:10 -07002176/*
2177 * Process updated osd map.
2178 *
2179 * The message contains any number of incremental and full maps, normally
2180 * indicating some sort of topology change in the cluster. Kick requests
2181 * off to different OSDs as needed.
2182 */
2183void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2184{
2185 void *p, *end, *next;
2186 u32 nr_maps, maplen;
2187 u32 epoch;
2188 struct ceph_osdmap *newmap = NULL, *oldmap;
2189 int err;
2190 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002191 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002192
2193 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2194 p = msg->front.iov_base;
2195 end = p + msg->front.iov_len;
2196
2197 /* verify fsid */
2198 ceph_decode_need(&p, end, sizeof(fsid), bad);
2199 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002200 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2201 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002202
2203 down_write(&osdc->map_sem);
2204
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002205 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2206
Sage Weilf24e9982009-10-06 11:31:10 -07002207 /* incremental maps */
2208 ceph_decode_32_safe(&p, end, nr_maps, bad);
2209 dout(" %d inc maps\n", nr_maps);
2210 while (nr_maps > 0) {
2211 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002212 epoch = ceph_decode_32(&p);
2213 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002214 ceph_decode_need(&p, end, maplen, bad);
2215 next = p + maplen;
2216 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2217 dout("applying incremental map %u len %d\n",
2218 epoch, maplen);
2219 newmap = osdmap_apply_incremental(&p, next,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02002220 osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002221 if (IS_ERR(newmap)) {
2222 err = PTR_ERR(newmap);
2223 goto bad;
2224 }
Sage Weil30dc6382009-12-21 14:49:37 -08002225 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002226 if (newmap != osdc->osdmap) {
2227 ceph_osdmap_destroy(osdc->osdmap);
2228 osdc->osdmap = newmap;
2229 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002230 was_full = was_full ||
2231 ceph_osdmap_flag(osdc->osdmap,
2232 CEPH_OSDMAP_FULL);
2233 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002234 } else {
2235 dout("ignoring incremental map %u len %d\n",
2236 epoch, maplen);
2237 }
2238 p = next;
2239 nr_maps--;
2240 }
2241 if (newmap)
2242 goto done;
2243
2244 /* full maps */
2245 ceph_decode_32_safe(&p, end, nr_maps, bad);
2246 dout(" %d full maps\n", nr_maps);
2247 while (nr_maps) {
2248 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002249 epoch = ceph_decode_32(&p);
2250 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002251 ceph_decode_need(&p, end, maplen, bad);
2252 if (nr_maps > 1) {
2253 dout("skipping non-latest full map %u len %d\n",
2254 epoch, maplen);
2255 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2256 dout("skipping full map %u len %d, "
2257 "older than our %u\n", epoch, maplen,
2258 osdc->osdmap->epoch);
2259 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002260 int skipped_map = 0;
2261
Sage Weilf24e9982009-10-06 11:31:10 -07002262 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002263 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002264 if (IS_ERR(newmap)) {
2265 err = PTR_ERR(newmap);
2266 goto bad;
2267 }
Sage Weil30dc6382009-12-21 14:49:37 -08002268 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002269 oldmap = osdc->osdmap;
2270 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002271 if (oldmap) {
2272 if (oldmap->epoch + 1 < newmap->epoch)
2273 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002274 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002275 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002276 was_full = was_full ||
2277 ceph_osdmap_flag(osdc->osdmap,
2278 CEPH_OSDMAP_FULL);
2279 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002280 }
2281 p += maplen;
2282 nr_maps--;
2283 }
2284
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002285 if (!osdc->osdmap)
2286 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002287done:
2288 downgrade_write(&osdc->map_sem);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01002289 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2290 osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002291
2292 /*
2293 * subscribe to subsequent osdmap updates if full to ensure
2294 * we find out when we are no longer full and stop returning
2295 * ENOSPC.
2296 */
Josh Durgind29adb32013-12-02 19:11:48 -08002297 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2298 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2299 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002300 ceph_monc_request_next_osdmap(&osdc->client->monc);
2301
Alex Elderf9d25192013-02-15 11:42:29 -06002302 mutex_lock(&osdc->request_mutex);
2303 __send_queued(osdc);
2304 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002305 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002306 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002307 return;
2308
2309bad:
2310 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002311 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002312 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002313}
2314
Sage Weilf24e9982009-10-06 11:31:10 -07002315/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002316 * watch/notify callback event infrastructure
2317 *
2318 * These callbacks are used both for watch and notify operations.
2319 */
2320static void __release_event(struct kref *kref)
2321{
2322 struct ceph_osd_event *event =
2323 container_of(kref, struct ceph_osd_event, kref);
2324
2325 dout("__release_event %p\n", event);
2326 kfree(event);
2327}
2328
2329static void get_event(struct ceph_osd_event *event)
2330{
2331 kref_get(&event->kref);
2332}
2333
2334void ceph_osdc_put_event(struct ceph_osd_event *event)
2335{
2336 kref_put(&event->kref, __release_event);
2337}
2338EXPORT_SYMBOL(ceph_osdc_put_event);
2339
2340static void __insert_event(struct ceph_osd_client *osdc,
2341 struct ceph_osd_event *new)
2342{
2343 struct rb_node **p = &osdc->event_tree.rb_node;
2344 struct rb_node *parent = NULL;
2345 struct ceph_osd_event *event = NULL;
2346
2347 while (*p) {
2348 parent = *p;
2349 event = rb_entry(parent, struct ceph_osd_event, node);
2350 if (new->cookie < event->cookie)
2351 p = &(*p)->rb_left;
2352 else if (new->cookie > event->cookie)
2353 p = &(*p)->rb_right;
2354 else
2355 BUG();
2356 }
2357
2358 rb_link_node(&new->node, parent, p);
2359 rb_insert_color(&new->node, &osdc->event_tree);
2360}
2361
2362static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2363 u64 cookie)
2364{
2365 struct rb_node **p = &osdc->event_tree.rb_node;
2366 struct rb_node *parent = NULL;
2367 struct ceph_osd_event *event = NULL;
2368
2369 while (*p) {
2370 parent = *p;
2371 event = rb_entry(parent, struct ceph_osd_event, node);
2372 if (cookie < event->cookie)
2373 p = &(*p)->rb_left;
2374 else if (cookie > event->cookie)
2375 p = &(*p)->rb_right;
2376 else
2377 return event;
2378 }
2379 return NULL;
2380}
2381
2382static void __remove_event(struct ceph_osd_event *event)
2383{
2384 struct ceph_osd_client *osdc = event->osdc;
2385
2386 if (!RB_EMPTY_NODE(&event->node)) {
2387 dout("__remove_event removed %p\n", event);
2388 rb_erase(&event->node, &osdc->event_tree);
2389 ceph_osdc_put_event(event);
2390 } else {
2391 dout("__remove_event didn't remove %p\n", event);
2392 }
2393}
2394
2395int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2396 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002397 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002398{
2399 struct ceph_osd_event *event;
2400
2401 event = kmalloc(sizeof(*event), GFP_NOIO);
2402 if (!event)
2403 return -ENOMEM;
2404
2405 dout("create_event %p\n", event);
2406 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002407 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002408 event->data = data;
2409 event->osdc = osdc;
2410 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002411 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002412 kref_init(&event->kref); /* one ref for us */
2413 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002414
2415 spin_lock(&osdc->event_lock);
2416 event->cookie = ++osdc->event_count;
2417 __insert_event(osdc, event);
2418 spin_unlock(&osdc->event_lock);
2419
2420 *pevent = event;
2421 return 0;
2422}
2423EXPORT_SYMBOL(ceph_osdc_create_event);
2424
2425void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2426{
2427 struct ceph_osd_client *osdc = event->osdc;
2428
2429 dout("cancel_event %p\n", event);
2430 spin_lock(&osdc->event_lock);
2431 __remove_event(event);
2432 spin_unlock(&osdc->event_lock);
2433 ceph_osdc_put_event(event); /* caller's */
2434}
2435EXPORT_SYMBOL(ceph_osdc_cancel_event);
2436
2437
2438static void do_event_work(struct work_struct *work)
2439{
2440 struct ceph_osd_event_work *event_work =
2441 container_of(work, struct ceph_osd_event_work, work);
2442 struct ceph_osd_event *event = event_work->event;
2443 u64 ver = event_work->ver;
2444 u64 notify_id = event_work->notify_id;
2445 u8 opcode = event_work->opcode;
2446
2447 dout("do_event_work completing %p\n", event);
2448 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002449 dout("do_event_work completed %p\n", event);
2450 ceph_osdc_put_event(event);
2451 kfree(event_work);
2452}
2453
2454
2455/*
2456 * Process osd watch notifications
2457 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002458static void handle_watch_notify(struct ceph_osd_client *osdc,
2459 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002460{
2461 void *p, *end;
2462 u8 proto_ver;
2463 u64 cookie, ver, notify_id;
2464 u8 opcode;
2465 struct ceph_osd_event *event;
2466 struct ceph_osd_event_work *event_work;
2467
2468 p = msg->front.iov_base;
2469 end = p + msg->front.iov_len;
2470
2471 ceph_decode_8_safe(&p, end, proto_ver, bad);
2472 ceph_decode_8_safe(&p, end, opcode, bad);
2473 ceph_decode_64_safe(&p, end, cookie, bad);
2474 ceph_decode_64_safe(&p, end, ver, bad);
2475 ceph_decode_64_safe(&p, end, notify_id, bad);
2476
2477 spin_lock(&osdc->event_lock);
2478 event = __find_event(osdc, cookie);
2479 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002480 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002481 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002482 }
2483 spin_unlock(&osdc->event_lock);
2484 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2485 cookie, ver, event);
2486 if (event) {
2487 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002488 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002489 pr_err("couldn't allocate event_work\n");
2490 ceph_osdc_put_event(event);
2491 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002492 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002493 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002494 event_work->event = event;
2495 event_work->ver = ver;
2496 event_work->notify_id = notify_id;
2497 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002498
2499 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002500 }
2501
2502 return;
2503
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002504bad:
2505 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002506}
2507
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002508/*
Alex Eldere65550f2013-04-05 01:27:12 -05002509 * build new request AND message
2510 *
2511 */
2512void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2513 struct ceph_snap_context *snapc, u64 snap_id,
2514 struct timespec *mtime)
2515{
2516 struct ceph_msg *msg = req->r_request;
2517 void *p;
2518 size_t msg_size;
2519 int flags = req->r_flags;
2520 u64 data_len;
2521 unsigned int i;
2522
2523 req->r_snapid = snap_id;
Ilya Dryomov84127282016-04-26 15:39:47 +02002524 WARN_ON(snapc != req->r_snapc);
Alex Eldere65550f2013-04-05 01:27:12 -05002525
2526 /* encode request */
2527 msg->hdr.version = cpu_to_le16(4);
2528
2529 p = msg->front.iov_base;
2530 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2531 req->r_request_osdmap_epoch = p;
2532 p += 4;
2533 req->r_request_flags = p;
2534 p += 4;
2535 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2536 ceph_encode_timespec(p, mtime);
2537 p += sizeof(struct ceph_timespec);
2538 req->r_request_reassert_version = p;
2539 p += sizeof(struct ceph_eversion); /* will get filled in */
2540
2541 /* oloc */
2542 ceph_encode_8(&p, 4);
2543 ceph_encode_8(&p, 4);
2544 ceph_encode_32(&p, 8 + 4 + 4);
2545 req->r_request_pool = p;
2546 p += 8;
2547 ceph_encode_32(&p, -1); /* preferred */
2548 ceph_encode_32(&p, 0); /* key len */
2549
2550 ceph_encode_8(&p, 1);
2551 req->r_request_pgid = p;
2552 p += 8 + 4;
2553 ceph_encode_32(&p, -1); /* preferred */
2554
2555 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002556 ceph_encode_32(&p, req->r_base_oid.name_len);
2557 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002558 dout("oid %*pE len %d\n", req->r_base_oid.name_len,
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002559 req->r_base_oid.name, req->r_base_oid.name_len);
2560 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002561
2562 /* ops--can imply data */
2563 ceph_encode_16(&p, (u16)req->r_num_ops);
2564 data_len = 0;
2565 for (i = 0; i < req->r_num_ops; i++) {
2566 data_len += osd_req_encode_op(req, p, i);
2567 p += sizeof(struct ceph_osd_op);
2568 }
2569
2570 /* snaps */
2571 ceph_encode_64(&p, req->r_snapid);
2572 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2573 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2574 if (req->r_snapc) {
Ilya Dryomov84127282016-04-26 15:39:47 +02002575 for (i = 0; i < req->r_snapc->num_snaps; i++) {
Alex Eldere65550f2013-04-05 01:27:12 -05002576 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2577 }
2578 }
2579
2580 req->r_request_attempts = p;
2581 p += 4;
2582
2583 /* data */
2584 if (flags & CEPH_OSD_FLAG_WRITE) {
2585 u16 data_off;
2586
2587 /*
2588 * The header "data_off" is a hint to the receiver
2589 * allowing it to align received data into its
2590 * buffers such that there's no need to re-copy
2591 * it before writing it to disk (direct I/O).
2592 */
2593 data_off = (u16) (off & 0xffff);
2594 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2595 }
2596 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2597
2598 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2599 msg_size = p - msg->front.iov_base;
2600 msg->front.iov_len = msg_size;
2601 msg->hdr.front_len = cpu_to_le32(msg_size);
2602
2603 dout("build_request msg_size was %d\n", (int)msg_size);
2604}
2605EXPORT_SYMBOL(ceph_osdc_build_request);
2606
2607/*
Sage Weilf24e9982009-10-06 11:31:10 -07002608 * Register request, send initial attempt.
2609 */
2610int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2611 struct ceph_osd_request *req,
2612 bool nofail)
2613{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002614 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002615
Sage Weilf24e9982009-10-06 11:31:10 -07002616 down_read(&osdc->map_sem);
2617 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002618
2619 rc = __ceph_osdc_start_request(osdc, req, nofail);
2620
Sage Weilf24e9982009-10-06 11:31:10 -07002621 mutex_unlock(&osdc->request_mutex);
2622 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002623
Sage Weilf24e9982009-10-06 11:31:10 -07002624 return rc;
2625}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002626EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002627
2628/*
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002629 * Unregister a registered request. The request is not completed (i.e.
2630 * no callbacks or wakeups) - higher layers are supposed to know what
2631 * they are canceling.
2632 */
2633void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2634{
2635 struct ceph_osd_client *osdc = req->r_osdc;
2636
2637 mutex_lock(&osdc->request_mutex);
2638 if (req->r_linger)
2639 __unregister_linger_request(osdc, req);
2640 __unregister_request(osdc, req);
2641 mutex_unlock(&osdc->request_mutex);
2642
2643 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2644}
2645EXPORT_SYMBOL(ceph_osdc_cancel_request);
2646
2647/*
Sage Weilf24e9982009-10-06 11:31:10 -07002648 * wait for a request to complete
2649 */
2650int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2651 struct ceph_osd_request *req)
2652{
2653 int rc;
2654
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002655 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2656
Sage Weilf24e9982009-10-06 11:31:10 -07002657 rc = wait_for_completion_interruptible(&req->r_completion);
2658 if (rc < 0) {
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002659 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2660 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002661 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002662 return rc;
2663 }
2664
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002665 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2666 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002667 return req->r_result;
2668}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002669EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002670
2671/*
2672 * sync - wait for all in-flight requests to flush. avoid starvation.
2673 */
2674void ceph_osdc_sync(struct ceph_osd_client *osdc)
2675{
2676 struct ceph_osd_request *req;
2677 u64 last_tid, next_tid = 0;
2678
2679 mutex_lock(&osdc->request_mutex);
2680 last_tid = osdc->last_tid;
2681 while (1) {
2682 req = __lookup_request_ge(osdc, next_tid);
2683 if (!req)
2684 break;
2685 if (req->r_tid > last_tid)
2686 break;
2687
2688 next_tid = req->r_tid + 1;
2689 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2690 continue;
2691
2692 ceph_osdc_get_request(req);
2693 mutex_unlock(&osdc->request_mutex);
2694 dout("sync waiting on tid %llu (last is %llu)\n",
2695 req->r_tid, last_tid);
2696 wait_for_completion(&req->r_safe_completion);
2697 mutex_lock(&osdc->request_mutex);
2698 ceph_osdc_put_request(req);
2699 }
2700 mutex_unlock(&osdc->request_mutex);
2701 dout("sync done (thru tid %llu)\n", last_tid);
2702}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002703EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002704
2705/*
Josh Durgindd935f42013-08-28 21:43:09 -07002706 * Call all pending notify callbacks - for use after a watch is
2707 * unregistered, to make sure no more callbacks for it will be invoked
2708 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002709void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002710{
2711 flush_workqueue(osdc->notify_wq);
2712}
2713EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2714
2715
2716/*
Sage Weilf24e9982009-10-06 11:31:10 -07002717 * init, shutdown
2718 */
2719int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2720{
2721 int err;
2722
2723 dout("init\n");
2724 osdc->client = client;
2725 osdc->osdmap = NULL;
2726 init_rwsem(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002727 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002728 osdc->last_tid = 0;
2729 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002730 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002731 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002732 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002733 INIT_LIST_HEAD(&osdc->req_unsent);
2734 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002735 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002736 osdc->num_requests = 0;
2737 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002738 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002739 spin_lock_init(&osdc->event_lock);
2740 osdc->event_tree = RB_ROOT;
2741 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002742
2743 schedule_delayed_work(&osdc->osds_timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03002744 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
Sage Weilf24e9982009-10-06 11:31:10 -07002745
Sage Weil5f44f142009-11-18 14:52:18 -08002746 err = -ENOMEM;
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01002747 osdc->req_mempool = mempool_create_slab_pool(10,
2748 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07002749 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002750 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002751
Sage Weild50b4092012-07-09 14:22:34 -07002752 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02002753 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002754 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002755 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002756 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02002757 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002758 if (err < 0)
2759 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002760
Dan Carpenterdbcae082013-08-15 08:58:59 +03002761 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002762 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002763 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002764 goto out_msgpool_reply;
2765
Sage Weilf24e9982009-10-06 11:31:10 -07002766 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002767
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002768out_msgpool_reply:
2769 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002770out_msgpool:
2771 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002772out_mempool:
2773 mempool_destroy(osdc->req_mempool);
2774out:
2775 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002776}
2777
2778void ceph_osdc_stop(struct ceph_osd_client *osdc)
2779{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002780 flush_workqueue(osdc->notify_wq);
2781 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002782 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002783 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Ilya Dryomov42a2c092016-04-28 16:07:22 +02002784
2785 mutex_lock(&osdc->request_mutex);
2786 while (!RB_EMPTY_ROOT(&osdc->osds)) {
2787 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
2788 struct ceph_osd, o_node);
2789 remove_osd(osdc, osd);
2790 }
2791 mutex_unlock(&osdc->request_mutex);
2792
Sage Weilf24e9982009-10-06 11:31:10 -07002793 if (osdc->osdmap) {
2794 ceph_osdmap_destroy(osdc->osdmap);
2795 osdc->osdmap = NULL;
2796 }
2797 mempool_destroy(osdc->req_mempool);
2798 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002799 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002800}
2801
2802/*
2803 * Read some contiguous pages. If we cross a stripe boundary, shorten
2804 * *plen. Return number of bytes read, or error.
2805 */
2806int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2807 struct ceph_vino vino, struct ceph_file_layout *layout,
2808 u64 off, u64 *plen,
2809 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002810 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002811{
2812 struct ceph_osd_request *req;
2813 int rc = 0;
2814
2815 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2816 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002817 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002818 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002819 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002820 false);
Sage Weil68162822012-09-24 21:01:02 -07002821 if (IS_ERR(req))
2822 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002823
2824 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002825
Alex Elder406e2c92013-04-15 14:50:36 -05002826 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002827 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002828
Alex Eldere0c59482013-03-07 15:38:25 -06002829 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002830 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002831
Alex Elder79528732013-04-03 21:32:51 -05002832 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002833
Sage Weilf24e9982009-10-06 11:31:10 -07002834 rc = ceph_osdc_start_request(osdc, req, false);
2835 if (!rc)
2836 rc = ceph_osdc_wait_request(osdc, req);
2837
2838 ceph_osdc_put_request(req);
2839 dout("readpages result %d\n", rc);
2840 return rc;
2841}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002842EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002843
2844/*
2845 * do a synchronous write on N pages
2846 */
2847int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2848 struct ceph_file_layout *layout,
2849 struct ceph_snap_context *snapc,
2850 u64 off, u64 len,
2851 u32 truncate_seq, u64 truncate_size,
2852 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002853 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002854{
2855 struct ceph_osd_request *req;
2856 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002857 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002858
Alex Elderacead002013-03-14 14:09:05 -05002859 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002860 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002861 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002862 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002863 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002864 true);
Sage Weil68162822012-09-24 21:01:02 -07002865 if (IS_ERR(req))
2866 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002867
2868 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002869 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002870 false, false);
2871 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002872
Alex Elder79528732013-04-03 21:32:51 -05002873 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002874
Alex Elder87f979d2013-02-15 11:42:29 -06002875 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002876 if (!rc)
2877 rc = ceph_osdc_wait_request(osdc, req);
2878
2879 ceph_osdc_put_request(req);
2880 if (rc == 0)
2881 rc = len;
2882 dout("writepages result %d\n", rc);
2883 return rc;
2884}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002885EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002886
Alex Elder5522ae02013-05-01 12:43:04 -05002887int ceph_osdc_setup(void)
2888{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002889 size_t size = sizeof(struct ceph_osd_request) +
2890 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2891
Alex Elder5522ae02013-05-01 12:43:04 -05002892 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002893 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2894 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05002895
2896 return ceph_osd_request_cache ? 0 : -ENOMEM;
2897}
2898EXPORT_SYMBOL(ceph_osdc_setup);
2899
2900void ceph_osdc_cleanup(void)
2901{
2902 BUG_ON(!ceph_osd_request_cache);
2903 kmem_cache_destroy(ceph_osd_request_cache);
2904 ceph_osd_request_cache = NULL;
2905}
2906EXPORT_SYMBOL(ceph_osdc_cleanup);
2907
Sage Weilf24e9982009-10-06 11:31:10 -07002908/*
2909 * handle incoming message
2910 */
2911static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2912{
2913 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002914 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002915 int type = le16_to_cpu(msg->hdr.type);
2916
2917 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002918 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002919 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002920
2921 switch (type) {
2922 case CEPH_MSG_OSD_MAP:
2923 ceph_osdc_handle_map(osdc, msg);
2924 break;
2925 case CEPH_MSG_OSD_OPREPLY:
Shraddha Barke70cf0522015-10-18 13:55:38 +05302926 handle_reply(osdc, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002927 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002928 case CEPH_MSG_WATCH_NOTIFY:
2929 handle_watch_notify(osdc, msg);
2930 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002931
2932 default:
2933 pr_err("received unknown message type %d %s\n", type,
2934 ceph_msg_type_name(type));
2935 }
Sage Weil4a32f932010-06-13 10:27:53 -07002936out:
Sage Weilf24e9982009-10-06 11:31:10 -07002937 ceph_msg_put(msg);
2938}
2939
Sage Weil5b3a4db2010-02-19 21:43:23 -08002940/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002941 * Lookup and return message for incoming reply. Don't try to do
2942 * anything about a larger than preallocated data portion of the
2943 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002944 */
2945static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002946 struct ceph_msg_header *hdr,
2947 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002948{
2949 struct ceph_osd *osd = con->private;
2950 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002951 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002952 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002953 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002954 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002955 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002956
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002957 tid = le64_to_cpu(hdr->tid);
2958 mutex_lock(&osdc->request_mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002959 req = lookup_request(&osdc->requests, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002960 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01002961 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2962 osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002963 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002964 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002965 goto out;
2966 }
Sage Weilc16e7862010-03-01 13:02:00 -08002967
Alex Elderace6d3a2013-04-01 16:12:14 -05002968 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002969
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002970 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002971 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2972 __func__, osd->o_osd, req->r_tid, front_len,
2973 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002974 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2975 false);
Sage Weila79832f2010-04-01 16:06:19 -07002976 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002977 goto out;
2978 ceph_msg_put(req->r_reply);
2979 req->r_reply = m;
2980 }
Sage Weilc16e7862010-03-01 13:02:00 -08002981
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002982 if (data_len > req->r_reply->data_length) {
2983 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2984 __func__, osd->o_osd, req->r_tid, data_len,
2985 req->r_reply->data_length);
2986 m = NULL;
2987 *skip = 1;
2988 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002989 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002990
2991 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002992 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002993
2994out:
2995 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002996 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002997}
2998
2999static struct ceph_msg *alloc_msg(struct ceph_connection *con,
3000 struct ceph_msg_header *hdr,
3001 int *skip)
3002{
3003 struct ceph_osd *osd = con->private;
3004 int type = le16_to_cpu(hdr->type);
3005 int front = le32_to_cpu(hdr->front_len);
3006
Alex Elder1c20f2d2012-06-04 14:43:32 -05003007 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08003008 switch (type) {
3009 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07003010 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07003011 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08003012 case CEPH_MSG_OSD_OPREPLY:
3013 return get_reply(con, hdr, skip);
3014 default:
3015 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
3016 osd->o_osd);
3017 *skip = 1;
3018 return NULL;
3019 }
Sage Weilf24e9982009-10-06 11:31:10 -07003020}
3021
3022/*
3023 * Wrappers to refcount containing ceph_osd struct
3024 */
3025static struct ceph_connection *get_osd_con(struct ceph_connection *con)
3026{
3027 struct ceph_osd *osd = con->private;
3028 if (get_osd(osd))
3029 return con;
3030 return NULL;
3031}
3032
3033static void put_osd_con(struct ceph_connection *con)
3034{
3035 struct ceph_osd *osd = con->private;
3036 put_osd(osd);
3037}
3038
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003039/*
3040 * authentication
3041 */
Alex Eldera3530df2012-05-16 15:16:39 -05003042/*
3043 * Note: returned pointer is the address of a structure that's
3044 * managed separately. Caller must *not* attempt to free it.
3045 */
3046static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05003047 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003048{
3049 struct ceph_osd *o = con->private;
3050 struct ceph_osd_client *osdc = o->o_osdc;
3051 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05003052 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003053
Alex Elder74f18692012-05-16 15:16:39 -05003054 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02003055 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05003056 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003057 }
Sage Weil27859f92013-03-25 10:26:14 -07003058 if (!auth->authorizer) {
3059 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
3060 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003061 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05003062 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07003063 } else {
3064 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07003065 auth);
3066 if (ret)
3067 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003068 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003069 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05003070
Alex Eldera3530df2012-05-16 15:16:39 -05003071 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003072}
3073
3074
3075static int verify_authorizer_reply(struct ceph_connection *con, int len)
3076{
3077 struct ceph_osd *o = con->private;
3078 struct ceph_osd_client *osdc = o->o_osdc;
3079 struct ceph_auth_client *ac = osdc->client->monc.auth;
3080
Sage Weil27859f92013-03-25 10:26:14 -07003081 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003082}
3083
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003084static int invalidate_authorizer(struct ceph_connection *con)
3085{
3086 struct ceph_osd *o = con->private;
3087 struct ceph_osd_client *osdc = o->o_osdc;
3088 struct ceph_auth_client *ac = osdc->client->monc.auth;
3089
Sage Weil27859f92013-03-25 10:26:14 -07003090 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003091 return ceph_monc_validate_auth(&osdc->client->monc);
3092}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003093
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003094static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003095{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003096 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003097 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003098
Yan, Zheng33d07332014-11-04 16:33:37 +08003099 return ceph_auth_sign_message(auth, msg);
3100}
3101
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003102static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003103{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003104 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003105 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003106
Yan, Zheng33d07332014-11-04 16:33:37 +08003107 return ceph_auth_check_message_signature(auth, msg);
3108}
3109
Tobias Klauser9e327892010-05-20 10:40:19 +02003110static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07003111 .get = get_osd_con,
3112 .put = put_osd_con,
3113 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003114 .get_authorizer = get_authorizer,
3115 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003116 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003117 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003118 .sign_message = osd_sign_message,
3119 .check_message_signature = osd_check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003120 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003121};