blob: 32ba09be6ee66be5a84d1efe8b46a7c6bccd5a7c [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/*
302 * requests
303 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400304static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700305{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400306 struct ceph_osd_request *req = container_of(kref,
307 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500308 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800309
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400310 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
311 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400312 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
313 WARN_ON(!list_empty(&req->r_req_lru_item));
314 WARN_ON(!list_empty(&req->r_osd_item));
315 WARN_ON(!list_empty(&req->r_linger_item));
316 WARN_ON(!list_empty(&req->r_linger_osd_item));
317 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400318
Sage Weil415e49a2009-12-07 13:37:03 -0800319 if (req->r_request)
320 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500321 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500322 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500323 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500324 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600325
Alex Elder54764922013-04-05 01:27:12 -0500326 for (which = 0; which < req->r_num_ops; which++)
327 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600328
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200329 ceph_oid_destroy(&req->r_base_oid);
330 ceph_oid_destroy(&req->r_target_oid);
Sage Weil415e49a2009-12-07 13:37:03 -0800331 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200332
Sage Weil415e49a2009-12-07 13:37:03 -0800333 if (req->r_mempool)
334 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100335 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500336 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100337 else
338 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700339}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400340
341void ceph_osdc_get_request(struct ceph_osd_request *req)
342{
343 dout("%s %p (was %d)\n", __func__, req,
344 atomic_read(&req->r_kref.refcount));
345 kref_get(&req->r_kref);
346}
347EXPORT_SYMBOL(ceph_osdc_get_request);
348
349void ceph_osdc_put_request(struct ceph_osd_request *req)
350{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200351 if (req) {
352 dout("%s %p (was %d)\n", __func__, req,
353 atomic_read(&req->r_kref.refcount));
354 kref_put(&req->r_kref, ceph_osdc_release_request);
355 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400356}
357EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700358
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700359struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700360 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800361 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700362 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600363 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700364{
365 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700366
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700367 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100368 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700369 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100370 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
371 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700372 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100373 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
374 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
375 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700376 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100377 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700378 return NULL;
379
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100380 /* req only, each op is zeroed in _osd_req_op_init() */
381 memset(req, 0, sizeof(*req));
382
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700383 req->r_osdc = osdc;
384 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500385 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200386 req->r_snapid = CEPH_NOSNAP;
387 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700388
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700389 kref_init(&req->r_kref);
390 init_completion(&req->r_completion);
391 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600392 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700393 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700394 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400395 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700396 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700397 INIT_LIST_HEAD(&req->r_osd_item);
398
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200399 ceph_oid_init(&req->r_base_oid);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200400 req->r_base_oloc.pool = -1;
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200401 ceph_oid_init(&req->r_target_oid);
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200402 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200403
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200404 dout("%s req %p\n", __func__, req);
405 return req;
406}
407EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100408
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200409int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
410{
411 struct ceph_osd_client *osdc = req->r_osdc;
412 struct ceph_msg *msg;
413 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700414
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200415 WARN_ON(ceph_oid_empty(&req->r_base_oid));
416
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200417 /* create request message */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100418 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
419 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
420 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
421 msg_size += 1 + 8 + 4 + 4; /* pgid */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200422 msg_size += 4 + req->r_base_oid.name_len; /* oid */
423 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100424 msg_size += 8; /* snapid */
425 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200426 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100427 msg_size += 4; /* retry_attempt */
428
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200429 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700430 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
431 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200432 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
433 if (!msg)
434 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700435
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700436 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700437 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700438
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200439 /* create reply message */
440 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200441 msg_size += req->r_base_oid.name_len;
442 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200443
444 if (req->r_mempool)
445 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
446 else
447 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
448 if (!msg)
449 return -ENOMEM;
450
451 req->r_reply = msg;
452
453 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700454}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200455EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700456
Alex Eldera8dd0a32013-03-13 20:50:00 -0500457static bool osd_req_opcode_valid(u16 opcode)
458{
459 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400460#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
461__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
462#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500463 default:
464 return false;
465 }
466}
467
Alex Elder33803f32013-03-13 20:50:00 -0500468/*
469 * This is an osd op init function for opcodes that have no data or
470 * other information associated with them. It also serves as a
471 * common init routine for all the other init functions, below.
472 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500473static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600474_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800475 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500476{
Alex Elderc99d2d42013-04-05 01:27:11 -0500477 struct ceph_osd_req_op *op;
478
479 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500480 BUG_ON(!osd_req_opcode_valid(opcode));
481
Alex Elderc99d2d42013-04-05 01:27:11 -0500482 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500483 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500484 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800485 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500486
487 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500488}
489
Alex Elder49719772013-02-11 12:33:24 -0600490void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800491 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600492{
Yan, Zheng144cba12015-04-27 11:09:54 +0800493 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600494}
495EXPORT_SYMBOL(osd_req_op_init);
496
Alex Elderc99d2d42013-04-05 01:27:11 -0500497void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
498 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500499 u64 offset, u64 length,
500 u64 truncate_size, u32 truncate_seq)
501{
Yan, Zheng144cba12015-04-27 11:09:54 +0800502 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
503 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500504 size_t payload_len = 0;
505
Li Wangad7a60d2013-08-15 11:51:44 +0800506 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200507 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
508 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500509
Alex Elder33803f32013-03-13 20:50:00 -0500510 op->extent.offset = offset;
511 op->extent.length = length;
512 op->extent.truncate_size = truncate_size;
513 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200514 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500515 payload_len += length;
516
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100517 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500518}
519EXPORT_SYMBOL(osd_req_op_extent_init);
520
Alex Elderc99d2d42013-04-05 01:27:11 -0500521void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
522 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500523{
Alex Elderc99d2d42013-04-05 01:27:11 -0500524 struct ceph_osd_req_op *op;
525 u64 previous;
526
527 BUG_ON(which >= osd_req->r_num_ops);
528 op = &osd_req->r_ops[which];
529 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500530
531 if (length == previous)
532 return; /* Nothing to do */
533 BUG_ON(length > previous);
534
535 op->extent.length = length;
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100536 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500537}
538EXPORT_SYMBOL(osd_req_op_extent_update);
539
Yan, Zheng2c63f492016-01-07 17:32:54 +0800540void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
541 unsigned int which, u64 offset_inc)
542{
543 struct ceph_osd_req_op *op, *prev_op;
544
545 BUG_ON(which + 1 >= osd_req->r_num_ops);
546
547 prev_op = &osd_req->r_ops[which];
548 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
549 /* dup previous one */
550 op->indata_len = prev_op->indata_len;
551 op->outdata_len = prev_op->outdata_len;
552 op->extent = prev_op->extent;
553 /* adjust offset */
554 op->extent.offset += offset_inc;
555 op->extent.length -= offset_inc;
556
557 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
558 op->indata_len -= offset_inc;
559}
560EXPORT_SYMBOL(osd_req_op_extent_dup_last);
561
Alex Elderc99d2d42013-04-05 01:27:11 -0500562void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500563 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500564{
Yan, Zheng144cba12015-04-27 11:09:54 +0800565 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
566 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500567 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500568 size_t payload_len = 0;
569 size_t size;
570
571 BUG_ON(opcode != CEPH_OSD_OP_CALL);
572
Alex Elder5f562df2013-04-05 01:27:12 -0500573 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
574 BUG_ON(!pagelist);
575 ceph_pagelist_init(pagelist);
576
Alex Elder33803f32013-03-13 20:50:00 -0500577 op->cls.class_name = class;
578 size = strlen(class);
579 BUG_ON(size > (size_t) U8_MAX);
580 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500581 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500582 payload_len += size;
583
584 op->cls.method_name = method;
585 size = strlen(method);
586 BUG_ON(size > (size_t) U8_MAX);
587 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500588 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500589 payload_len += size;
590
Alex Eldera4ce40a2013-04-05 01:27:12 -0500591 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500592
Alex Elder33803f32013-03-13 20:50:00 -0500593 op->cls.argc = 0; /* currently unused */
594
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100595 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500596}
597EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500598
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800599int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
600 u16 opcode, const char *name, const void *value,
601 size_t size, u8 cmp_op, u8 cmp_mode)
602{
Yan, Zheng144cba12015-04-27 11:09:54 +0800603 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
604 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800605 struct ceph_pagelist *pagelist;
606 size_t payload_len;
607
608 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
609
610 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
611 if (!pagelist)
612 return -ENOMEM;
613
614 ceph_pagelist_init(pagelist);
615
616 payload_len = strlen(name);
617 op->xattr.name_len = payload_len;
618 ceph_pagelist_append(pagelist, name, payload_len);
619
620 op->xattr.value_len = size;
621 ceph_pagelist_append(pagelist, value, size);
622 payload_len += size;
623
624 op->xattr.cmp_op = cmp_op;
625 op->xattr.cmp_mode = cmp_mode;
626
627 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100628 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800629 return 0;
630}
631EXPORT_SYMBOL(osd_req_op_xattr_init);
632
Alex Elderc99d2d42013-04-05 01:27:11 -0500633void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
634 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500635 u64 cookie, u64 version, int flag)
636{
Yan, Zheng144cba12015-04-27 11:09:54 +0800637 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
638 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500639
Alex Elderc99d2d42013-04-05 01:27:11 -0500640 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500641
642 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500643 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500644 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500645 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500646}
647EXPORT_SYMBOL(osd_req_op_watch_init);
648
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200649void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
650 unsigned int which,
651 u64 expected_object_size,
652 u64 expected_write_size)
653{
654 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800655 CEPH_OSD_OP_SETALLOCHINT,
656 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200657
658 op->alloc_hint.expected_object_size = expected_object_size;
659 op->alloc_hint.expected_write_size = expected_write_size;
660
661 /*
662 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
663 * not worth a feature bit. Set FAILOK per-op flag to make
664 * sure older osds don't trip over an unsupported opcode.
665 */
666 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
667}
668EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
669
Alex Elder90af3602013-04-05 14:46:01 -0500670static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500671 struct ceph_osd_data *osd_data)
672{
673 u64 length = ceph_osd_data_length(osd_data);
674
675 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
676 BUG_ON(length > (u64) SIZE_MAX);
677 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500678 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500679 length, osd_data->alignment);
680 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
681 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500682 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500683#ifdef CONFIG_BLOCK
684 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500685 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500686#endif
687 } else {
688 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
689 }
690}
691
Alex Elder175face2013-03-08 13:35:36 -0600692static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500693 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700694{
Alex Elder79528732013-04-03 21:32:51 -0500695 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500696 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500697 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500698 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600699
Alex Elder79528732013-04-03 21:32:51 -0500700 BUG_ON(which >= req->r_num_ops);
701 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500702 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
703 pr_err("unrecognized osd opcode %d\n", src->op);
704
705 return 0;
706 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700707
Alex Elder065a68f2012-04-20 15:49:43 -0500708 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600709 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600710 osd_data = &src->raw_data_in;
711 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600712 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700713 case CEPH_OSD_OP_READ:
714 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200715 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800716 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800717 case CEPH_OSD_OP_TRUNCATE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200718 if (src->op == CEPH_OSD_OP_WRITE ||
719 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder54d50642013-04-03 01:28:58 -0500720 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600721 dst->extent.offset = cpu_to_le64(src->extent.offset);
722 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700723 dst->extent.truncate_size =
724 cpu_to_le64(src->extent.truncate_size);
725 dst->extent.truncate_seq =
726 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500727 osd_data = &src->extent.osd_data;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200728 if (src->op == CEPH_OSD_OP_WRITE ||
729 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder04017e22013-04-05 14:46:02 -0500730 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500731 else
Alex Elder04017e22013-04-05 14:46:02 -0500732 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700733 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700734 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700735 dst->cls.class_len = src->cls.class_len;
736 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500737 osd_data = &src->cls.request_info;
738 ceph_osdc_msg_data_add(req->r_request, osd_data);
739 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
740 request_data_len = osd_data->pagelist->length;
741
742 osd_data = &src->cls.request_data;
743 data_length = ceph_osd_data_length(osd_data);
744 if (data_length) {
745 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
746 dst->cls.indata_len = cpu_to_le32(data_length);
747 ceph_osdc_msg_data_add(req->r_request, osd_data);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100748 src->indata_len += data_length;
Alex Elder04017e22013-04-05 14:46:02 -0500749 request_data_len += data_length;
750 }
751 osd_data = &src->cls.response_data;
752 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700753 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700754 case CEPH_OSD_OP_STARTSYNC:
755 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700756 case CEPH_OSD_OP_NOTIFY_ACK:
757 case CEPH_OSD_OP_WATCH:
758 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
759 dst->watch.ver = cpu_to_le64(src->watch.ver);
760 dst->watch.flag = src->watch.flag;
761 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200762 case CEPH_OSD_OP_SETALLOCHINT:
763 dst->alloc_hint.expected_object_size =
764 cpu_to_le64(src->alloc_hint.expected_object_size);
765 dst->alloc_hint.expected_write_size =
766 cpu_to_le64(src->alloc_hint.expected_write_size);
767 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800768 case CEPH_OSD_OP_SETXATTR:
769 case CEPH_OSD_OP_CMPXATTR:
770 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
771 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
772 dst->xattr.cmp_op = src->xattr.cmp_op;
773 dst->xattr.cmp_mode = src->xattr.cmp_mode;
774 osd_data = &src->xattr.osd_data;
775 ceph_osdc_msg_data_add(req->r_request, osd_data);
776 request_data_len = osd_data->pagelist->length;
777 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800778 case CEPH_OSD_OP_CREATE:
779 case CEPH_OSD_OP_DELETE:
780 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700781 default:
Alex Elder4c464592013-02-15 11:42:30 -0600782 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600783 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600784 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500785
786 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700787 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200788
Alex Eldera8dd0a32013-03-13 20:50:00 -0500789 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200790 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100791 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600792
Alex Elder54d50642013-04-03 01:28:58 -0500793 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700794}
795
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700796/*
Sage Weilf24e9982009-10-06 11:31:10 -0700797 * build new request AND message, calculate layout, and adjust file
798 * extent as needed.
799 *
800 * if the file was recently truncated, we include information about its
801 * old and new size so that the object can be updated appropriately. (we
802 * avoid synchronously deleting truncated objects because it's slow.)
803 *
804 * if @do_sync, include a 'startsync' command so that the osd will flush
805 * data quickly.
806 */
807struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
808 struct ceph_file_layout *layout,
809 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800810 u64 off, u64 *plen,
811 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700812 int opcode, int flags,
813 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700814 u32 truncate_seq,
815 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600816 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700817{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700818 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500819 u64 objnum = 0;
820 u64 objoff = 0;
821 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700822 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700823
Li Wangad7a60d2013-08-15 11:51:44 +0800824 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800825 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
826 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700827
Alex Elderacead002013-03-14 14:09:05 -0500828 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600829 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200830 if (!req) {
831 r = -ENOMEM;
832 goto fail;
833 }
Alex Elder79528732013-04-03 21:32:51 -0500834
Alex Elderd178a9e2012-11-13 21:11:15 -0600835 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700836
837 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500838 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200839 if (r)
840 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -0500841
Yan, Zheng864e9192014-11-13 10:47:25 +0800842 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800843 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800844 } else {
845 u32 object_size = le32_to_cpu(layout->fl_object_size);
846 u32 object_base = off - objoff;
847 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
848 if (truncate_size <= object_base) {
849 truncate_size = 0;
850 } else {
851 truncate_size -= object_base;
852 if (truncate_size > object_size)
853 truncate_size = object_size;
854 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800855 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800856 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800857 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500858 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500859
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200860 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200861 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600862
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200863 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
864 if (r)
865 goto fail;
866
Sage Weilf24e9982009-10-06 11:31:10 -0700867 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200868
869fail:
870 ceph_osdc_put_request(req);
871 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700872}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700873EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700874
875/*
876 * We keep osd requests in an rbtree, sorted by ->r_tid.
877 */
878static void __insert_request(struct ceph_osd_client *osdc,
879 struct ceph_osd_request *new)
880{
881 struct rb_node **p = &osdc->requests.rb_node;
882 struct rb_node *parent = NULL;
883 struct ceph_osd_request *req = NULL;
884
885 while (*p) {
886 parent = *p;
887 req = rb_entry(parent, struct ceph_osd_request, r_node);
888 if (new->r_tid < req->r_tid)
889 p = &(*p)->rb_left;
890 else if (new->r_tid > req->r_tid)
891 p = &(*p)->rb_right;
892 else
893 BUG();
894 }
895
896 rb_link_node(&new->r_node, parent, p);
897 rb_insert_color(&new->r_node, &osdc->requests);
898}
899
900static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
901 u64 tid)
902{
903 struct ceph_osd_request *req;
904 struct rb_node *n = osdc->requests.rb_node;
905
906 while (n) {
907 req = rb_entry(n, struct ceph_osd_request, r_node);
908 if (tid < req->r_tid)
909 n = n->rb_left;
910 else if (tid > req->r_tid)
911 n = n->rb_right;
912 else
913 return req;
914 }
915 return NULL;
916}
917
918static struct ceph_osd_request *
919__lookup_request_ge(struct ceph_osd_client *osdc,
920 u64 tid)
921{
922 struct ceph_osd_request *req;
923 struct rb_node *n = osdc->requests.rb_node;
924
925 while (n) {
926 req = rb_entry(n, struct ceph_osd_request, r_node);
927 if (tid < req->r_tid) {
928 if (!n->rb_left)
929 return req;
930 n = n->rb_left;
931 } else if (tid > req->r_tid) {
932 n = n->rb_right;
933 } else {
934 return req;
935 }
936 }
937 return NULL;
938}
939
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400940static void __kick_linger_request(struct ceph_osd_request *req)
941{
942 struct ceph_osd_client *osdc = req->r_osdc;
943 struct ceph_osd *osd = req->r_osd;
944
945 /*
946 * Linger requests need to be resent with a new tid to avoid
947 * the dup op detection logic on the OSDs. Achieve this with
948 * a re-register dance instead of open-coding.
949 */
950 ceph_osdc_get_request(req);
951 if (!list_empty(&req->r_linger_item))
952 __unregister_linger_request(osdc, req);
953 else
954 __unregister_request(osdc, req);
955 __register_request(osdc, req);
956 ceph_osdc_put_request(req);
957
958 /*
959 * Unless request has been registered as both normal and
960 * lingering, __unregister{,_linger}_request clears r_osd.
961 * However, here we need to preserve r_osd to make sure we
962 * requeue on the same OSD.
963 */
964 WARN_ON(req->r_osd || !osd);
965 req->r_osd = osd;
966
967 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
968 __enqueue_request(req);
969}
970
Sage Weil6f6c7002011-01-17 20:34:08 -0800971/*
972 * Resubmit requests pending on the given osd.
973 */
974static void __kick_osd_requests(struct ceph_osd_client *osdc,
975 struct ceph_osd *osd)
976{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700977 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500978 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400979 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800980 int err;
981
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400982 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800983 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600984 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800985 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400986
Alex Eldere02493c2013-03-25 18:16:11 -0500987 /*
988 * Build up a list of requests to resend by traversing the
989 * osd's list of requests. Requests for a given object are
990 * sent in tid order, and that is also the order they're
991 * kept on this list. Therefore all requests that are in
992 * flight will be found first, followed by all requests that
993 * have not yet been sent. And to resend requests while
994 * preserving this order we will want to put any sent
995 * requests back on the front of the osd client's unsent
996 * list.
997 *
998 * So we build a separate ordered list of already-sent
999 * requests for the affected osd and splice it onto the
1000 * front of the osd client's unsent list. Once we've seen a
1001 * request that has not yet been sent we're done. Those
1002 * requests are already sitting right where they belong.
1003 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001004 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -05001005 if (!req->r_sent)
1006 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001007
1008 if (!req->r_linger) {
1009 dout("%s requeueing %p tid %llu\n", __func__, req,
1010 req->r_tid);
1011 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001012 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001013 } else {
1014 list_move_tail(&req->r_req_lru_item, &resend_linger);
1015 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001016 }
Alex Eldere02493c2013-03-25 18:16:11 -05001017 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001018
Alex Eldere02493c2013-03-25 18:16:11 -05001019 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001020 * Both registered and not yet registered linger requests are
1021 * enqueued with a new tid on the same OSD. We add/move them
1022 * to req_unsent/o_requests at the end to keep things in tid
1023 * order.
Alex Eldere02493c2013-03-25 18:16:11 -05001024 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001025 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001026 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001027 WARN_ON(!list_empty(&req->r_req_lru_item));
1028 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001029 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001030
1031 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
1032 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001033}
1034
Sage Weilf24e9982009-10-06 11:31:10 -07001035/*
Sage Weil81b024e2009-10-09 10:29:18 -07001036 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -07001037 */
1038static void osd_reset(struct ceph_connection *con)
1039{
1040 struct ceph_osd *osd = con->private;
1041 struct ceph_osd_client *osdc;
1042
1043 if (!osd)
1044 return;
1045 dout("osd_reset osd%d\n", osd->o_osd);
1046 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001047 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001048 mutex_lock(&osdc->request_mutex);
1049 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001050 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001051 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001052 up_read(&osdc->map_sem);
1053}
1054
1055/*
1056 * Track open sessions with osds.
1057 */
Alex Eldere10006f2012-05-26 23:26:43 -05001058static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001059{
1060 struct ceph_osd *osd;
1061
1062 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1063 if (!osd)
1064 return NULL;
1065
1066 atomic_set(&osd->o_ref, 1);
1067 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001068 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001069 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001070 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001071 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001072 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001073 osd->o_incarnation = 1;
1074
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001075 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001076
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001077 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001078 return osd;
1079}
1080
1081static struct ceph_osd *get_osd(struct ceph_osd *osd)
1082{
1083 if (atomic_inc_not_zero(&osd->o_ref)) {
1084 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1085 atomic_read(&osd->o_ref));
1086 return osd;
1087 } else {
1088 dout("get_osd %p FAIL\n", osd);
1089 return NULL;
1090 }
1091}
1092
1093static void put_osd(struct ceph_osd *osd)
1094{
1095 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1096 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001097 if (atomic_dec_and_test(&osd->o_ref)) {
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001098 if (osd->o_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02001099 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001100 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001101 }
Sage Weilf24e9982009-10-06 11:31:10 -07001102}
1103
1104/*
1105 * remove an osd from our map
1106 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001107static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001108{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001109 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001110 WARN_ON(!list_empty(&osd->o_requests));
1111 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001112
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001113 list_del_init(&osd->o_osd_lru);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001114 rb_erase(&osd->o_node, &osdc->osds);
1115 RB_CLEAR_NODE(&osd->o_node);
1116}
1117
1118static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1119{
1120 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1121
1122 if (!RB_EMPTY_NODE(&osd->o_node)) {
1123 ceph_con_close(&osd->o_con);
1124 __remove_osd(osdc, osd);
1125 put_osd(osd);
1126 }
Sage Weilf24e9982009-10-06 11:31:10 -07001127}
1128
Sage Weilaca420b2011-08-31 14:45:53 -07001129static void remove_all_osds(struct ceph_osd_client *osdc)
1130{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001131 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001132 mutex_lock(&osdc->request_mutex);
1133 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1134 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1135 struct ceph_osd, o_node);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001136 remove_osd(osdc, osd);
Sage Weilaca420b2011-08-31 14:45:53 -07001137 }
1138 mutex_unlock(&osdc->request_mutex);
1139}
1140
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001141static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1142 struct ceph_osd *osd)
1143{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001144 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001145 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001146
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001147 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001148 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001149}
1150
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001151static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1152 struct ceph_osd *osd)
1153{
1154 dout("%s %p\n", __func__, osd);
1155
1156 if (list_empty(&osd->o_requests) &&
1157 list_empty(&osd->o_linger_requests))
1158 __move_osd_to_lru(osdc, osd);
1159}
1160
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001161static void __remove_osd_from_lru(struct ceph_osd *osd)
1162{
1163 dout("__remove_osd_from_lru %p\n", osd);
1164 if (!list_empty(&osd->o_osd_lru))
1165 list_del_init(&osd->o_osd_lru);
1166}
1167
Sage Weilaca420b2011-08-31 14:45:53 -07001168static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001169{
1170 struct ceph_osd *osd, *nosd;
1171
1172 dout("__remove_old_osds %p\n", osdc);
1173 mutex_lock(&osdc->request_mutex);
1174 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001175 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001176 break;
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001177 remove_osd(osdc, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001178 }
1179 mutex_unlock(&osdc->request_mutex);
1180}
1181
Sage Weilf24e9982009-10-06 11:31:10 -07001182/*
1183 * reset osd connect
1184 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001185static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001186{
Alex Elderc3acb182012-12-07 09:57:58 -06001187 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001188
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001189 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001190 if (list_empty(&osd->o_requests) &&
1191 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001192 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001193 return -ENODEV;
1194 }
1195
1196 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1197 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1198 !ceph_con_opened(&osd->o_con)) {
1199 struct ceph_osd_request *req;
1200
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001201 dout("osd addr hasn't changed and connection never opened, "
1202 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001203 /* touch each r_stamp for handle_timeout()'s benfit */
1204 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1205 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001206
1207 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001208 }
Alex Elderc3acb182012-12-07 09:57:58 -06001209
1210 ceph_con_close(&osd->o_con);
1211 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1212 osd->o_incarnation++;
1213
1214 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001215}
1216
1217static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1218{
1219 struct rb_node **p = &osdc->osds.rb_node;
1220 struct rb_node *parent = NULL;
1221 struct ceph_osd *osd = NULL;
1222
Sage Weilaca420b2011-08-31 14:45:53 -07001223 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001224 while (*p) {
1225 parent = *p;
1226 osd = rb_entry(parent, struct ceph_osd, o_node);
1227 if (new->o_osd < osd->o_osd)
1228 p = &(*p)->rb_left;
1229 else if (new->o_osd > osd->o_osd)
1230 p = &(*p)->rb_right;
1231 else
1232 BUG();
1233 }
1234
1235 rb_link_node(&new->o_node, parent, p);
1236 rb_insert_color(&new->o_node, &osdc->osds);
1237}
1238
1239static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1240{
1241 struct ceph_osd *osd;
1242 struct rb_node *n = osdc->osds.rb_node;
1243
1244 while (n) {
1245 osd = rb_entry(n, struct ceph_osd, o_node);
1246 if (o < osd->o_osd)
1247 n = n->rb_left;
1248 else if (o > osd->o_osd)
1249 n = n->rb_right;
1250 else
1251 return osd;
1252 }
1253 return NULL;
1254}
1255
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001256static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1257{
1258 schedule_delayed_work(&osdc->timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03001259 osdc->client->options->osd_keepalive_timeout);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001260}
1261
1262static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1263{
1264 cancel_delayed_work(&osdc->timeout_work);
1265}
Sage Weilf24e9982009-10-06 11:31:10 -07001266
1267/*
1268 * Register request, assign tid. If this is the first request, set up
1269 * the timeout event.
1270 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001271static void __register_request(struct ceph_osd_client *osdc,
1272 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001273{
Sage Weilf24e9982009-10-06 11:31:10 -07001274 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001275 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001276 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001277 __insert_request(osdc, req);
1278 ceph_osdc_get_request(req);
1279 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001280 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001281 dout(" first request, scheduling timeout\n");
1282 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001283 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001284}
1285
Sage Weilf24e9982009-10-06 11:31:10 -07001286/*
1287 * called under osdc->request_mutex
1288 */
1289static void __unregister_request(struct ceph_osd_client *osdc,
1290 struct ceph_osd_request *req)
1291{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001292 if (RB_EMPTY_NODE(&req->r_node)) {
1293 dout("__unregister_request %p tid %lld not registered\n",
1294 req, req->r_tid);
1295 return;
1296 }
1297
Sage Weilf24e9982009-10-06 11:31:10 -07001298 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1299 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001300 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001301 osdc->num_requests--;
1302
Sage Weil0ba64782009-10-08 16:57:16 -07001303 if (req->r_osd) {
1304 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001305 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001306
1307 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001308 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001309 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001310 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001311 }
Sage Weilf24e9982009-10-06 11:31:10 -07001312
Alex Elder7d5f2482012-11-29 08:37:03 -06001313 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001314 ceph_osdc_put_request(req);
1315
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001316 if (osdc->num_requests == 0) {
1317 dout(" no requests, canceling timeout\n");
1318 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001319 }
1320}
1321
1322/*
1323 * Cancel a previously queued request message
1324 */
1325static void __cancel_request(struct ceph_osd_request *req)
1326{
Sage Weil6bc18872010-09-27 10:18:52 -07001327 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001328 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001329 req->r_sent = 0;
1330 }
1331}
1332
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001333static void __register_linger_request(struct ceph_osd_client *osdc,
1334 struct ceph_osd_request *req)
1335{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001336 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1337 WARN_ON(!req->r_linger);
1338
Alex Elder96e4dac2013-05-22 20:54:25 -05001339 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001340 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001341 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001342 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001343 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001344}
1345
1346static void __unregister_linger_request(struct ceph_osd_client *osdc,
1347 struct ceph_osd_request *req)
1348{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001349 WARN_ON(!req->r_linger);
1350
1351 if (list_empty(&req->r_linger_item)) {
1352 dout("%s %p tid %llu not registered\n", __func__, req,
1353 req->r_tid);
1354 return;
1355 }
1356
1357 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001358 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001359
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001360 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001361 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001362 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001363 if (list_empty(&req->r_osd_item))
1364 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001365 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001366 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001367}
1368
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001369void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1370 struct ceph_osd_request *req)
1371{
1372 if (!req->r_linger) {
1373 dout("set_request_linger %p\n", req);
1374 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001375 }
1376}
1377EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1378
Sage Weilf24e9982009-10-06 11:31:10 -07001379/*
Josh Durgind29adb32013-12-02 19:11:48 -08001380 * Returns whether a request should be blocked from being sent
1381 * based on the current osdmap and osd_client settings.
1382 *
1383 * Caller should hold map_sem for read.
1384 */
1385static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1386 struct ceph_osd_request *req)
1387{
1388 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1389 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1390 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1391 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1392 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1393}
1394
1395/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001396 * Calculate mapping of a request to a PG. Takes tiering into account.
1397 */
1398static int __calc_request_pg(struct ceph_osdmap *osdmap,
1399 struct ceph_osd_request *req,
1400 struct ceph_pg *pg_out)
1401{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001402 bool need_check_tiering;
1403
1404 need_check_tiering = false;
1405 if (req->r_target_oloc.pool == -1) {
1406 req->r_target_oloc = req->r_base_oloc; /* struct */
1407 need_check_tiering = true;
1408 }
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001409 if (ceph_oid_empty(&req->r_target_oid)) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001410 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1411 need_check_tiering = true;
1412 }
1413
1414 if (need_check_tiering &&
1415 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001416 struct ceph_pg_pool_info *pi;
1417
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001418 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001419 if (pi) {
1420 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1421 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001422 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001423 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1424 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001425 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001426 }
1427 /* !pi is caught in ceph_oloc_oid_to_pg() */
1428 }
1429
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001430 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1431 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001432}
1433
Ilya Dryomovf671b582014-09-02 13:40:33 +04001434static void __enqueue_request(struct ceph_osd_request *req)
1435{
1436 struct ceph_osd_client *osdc = req->r_osdc;
1437
1438 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1439 req->r_osd ? req->r_osd->o_osd : -1);
1440
1441 if (req->r_osd) {
1442 __remove_osd_from_lru(req->r_osd);
1443 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1444 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1445 } else {
1446 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1447 }
1448}
1449
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001450/*
Sage Weilf24e9982009-10-06 11:31:10 -07001451 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1452 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001453 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001454 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001455 *
1456 * Return 0 if unchanged, 1 if changed, or negative on error.
1457 *
1458 * Caller should hold map_sem for read and request_mutex.
1459 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001460static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001461 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001462{
Sage Weil5b191d92013-02-23 10:38:16 -08001463 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001464 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001465 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001466 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001467 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001468
Sage Weil6f6c7002011-01-17 20:34:08 -08001469 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001470
1471 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001472 if (err) {
1473 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001474 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001475 }
Sage Weil7740a422010-01-08 15:58:25 -08001476 req->r_pgid = pgid;
1477
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001478 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1479 if (num < 0)
1480 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001481
Josh Durgind29adb32013-12-02 19:11:48 -08001482 was_paused = req->r_paused;
1483 req->r_paused = __req_should_be_paused(osdc, req);
1484 if (was_paused && !req->r_paused)
1485 force_resend = 1;
1486
Sage Weil38d64532011-10-14 13:33:55 -07001487 if ((!force_resend &&
1488 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001489 req->r_sent >= req->r_osd->o_incarnation &&
1490 req->r_num_pg_osds == num &&
1491 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001492 (req->r_osd == NULL && o == -1) ||
1493 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001494 return 0; /* no change */
1495
Sage Weil5b191d92013-02-23 10:38:16 -08001496 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1497 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001498 req->r_osd ? req->r_osd->o_osd : -1);
1499
Sage Weild85b7052010-05-10 10:24:48 -07001500 /* record full pg acting set */
1501 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1502 req->r_num_pg_osds = num;
1503
Sage Weilf24e9982009-10-06 11:31:10 -07001504 if (req->r_osd) {
1505 __cancel_request(req);
1506 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001507 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001508 req->r_osd = NULL;
1509 }
1510
1511 req->r_osd = __lookup_osd(osdc, o);
1512 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001513 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001514 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001515 if (!req->r_osd) {
1516 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001517 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001518 }
Sage Weilf24e9982009-10-06 11:31:10 -07001519
Sage Weil6f6c7002011-01-17 20:34:08 -08001520 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001521 __insert_osd(osdc, req->r_osd);
1522
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001523 ceph_con_open(&req->r_osd->o_con,
1524 CEPH_ENTITY_TYPE_OSD, o,
1525 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001526 }
1527
Ilya Dryomovf671b582014-09-02 13:40:33 +04001528 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001529 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001530
1531out:
Sage Weilf24e9982009-10-06 11:31:10 -07001532 return err;
1533}
1534
1535/*
1536 * caller should hold map_sem (for read) and request_mutex
1537 */
Sage Weil56e925b2012-01-03 12:34:34 -08001538static void __send_request(struct ceph_osd_client *osdc,
1539 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001540{
Sage Weil1b83bef2013-02-25 16:11:12 -08001541 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001542
Sage Weil1b83bef2013-02-25 16:11:12 -08001543 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1544 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1545 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001546
Sage Weil1b83bef2013-02-25 16:11:12 -08001547 /* fill in message content that changes each time we send it */
1548 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1549 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001550 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001551 p = req->r_request_pgid;
1552 ceph_encode_64(&p, req->r_pgid.pool);
1553 ceph_encode_32(&p, req->r_pgid.seed);
1554 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1555 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1556 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001557
Sage Weil3dd72fc2010-03-22 14:42:30 -07001558 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001559 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001560
1561 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001562
Sage Weilf24e9982009-10-06 11:31:10 -07001563 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001564
1565 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001566}
1567
1568/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001569 * Send any requests in the queue (req_unsent).
1570 */
Alex Elderf9d25192013-02-15 11:42:29 -06001571static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001572{
1573 struct ceph_osd_request *req, *tmp;
1574
Alex Elderf9d25192013-02-15 11:42:29 -06001575 dout("__send_queued\n");
1576 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001577 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001578}
1579
1580/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001581 * Caller should hold map_sem for read and request_mutex.
1582 */
1583static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1584 struct ceph_osd_request *req,
1585 bool nofail)
1586{
1587 int rc;
1588
1589 __register_request(osdc, req);
1590 req->r_sent = 0;
1591 req->r_got_reply = 0;
1592 rc = __map_request(osdc, req, 0);
1593 if (rc < 0) {
1594 if (nofail) {
1595 dout("osdc_start_request failed map, "
1596 " will retry %lld\n", req->r_tid);
1597 rc = 0;
1598 } else {
1599 __unregister_request(osdc, req);
1600 }
1601 return rc;
1602 }
1603
1604 if (req->r_osd == NULL) {
1605 dout("send_request %p no up osds in pg\n", req);
1606 ceph_monc_request_next_osdmap(&osdc->client->monc);
1607 } else {
1608 __send_queued(osdc);
1609 }
1610
1611 return 0;
1612}
1613
1614/*
Sage Weilf24e9982009-10-06 11:31:10 -07001615 * Timeout callback, called every N seconds when 1 or more osd
1616 * requests has been active for more than N seconds. When this
1617 * happens, we ping all OSDs with requests who have timed out to
1618 * ensure any communications channel reset is detected. Reset the
1619 * request timeouts another N seconds in the future as we go.
1620 * Reschedule the timeout event another N seconds in future (unless
1621 * there are no open requests).
1622 */
1623static void handle_timeout(struct work_struct *work)
1624{
1625 struct ceph_osd_client *osdc =
1626 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001627 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001628 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001629 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001630 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001631 dout("timeout\n");
1632 down_read(&osdc->map_sem);
1633
1634 ceph_monc_request_next_osdmap(&osdc->client->monc);
1635
1636 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001637
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001638 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001639 * ping osds that are a bit slow. this ensures that if there
1640 * is a break in the TCP connection we will notice, and reopen
1641 * a connection with that osd (from the fault callback).
1642 */
1643 INIT_LIST_HEAD(&slow_osds);
1644 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001645 if (time_before(jiffies,
1646 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001647 break;
1648
1649 osd = req->r_osd;
1650 BUG_ON(!osd);
1651 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001652 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001653 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1654 }
1655 while (!list_empty(&slow_osds)) {
1656 osd = list_entry(slow_osds.next, struct ceph_osd,
1657 o_keepalive_item);
1658 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001659 ceph_con_keepalive(&osd->o_con);
1660 }
1661
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001662 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001663 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001664 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001665 up_read(&osdc->map_sem);
1666}
1667
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001668static void handle_osds_timeout(struct work_struct *work)
1669{
1670 struct ceph_osd_client *osdc =
1671 container_of(work, struct ceph_osd_client,
1672 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001673 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001674
1675 dout("osds timeout\n");
1676 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001677 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001678 up_read(&osdc->map_sem);
1679
1680 schedule_delayed_work(&osdc->osds_timeout_work,
1681 round_jiffies_relative(delay));
1682}
1683
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001684static int ceph_oloc_decode(void **p, void *end,
1685 struct ceph_object_locator *oloc)
1686{
1687 u8 struct_v, struct_cv;
1688 u32 len;
1689 void *struct_end;
1690 int ret = 0;
1691
1692 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1693 struct_v = ceph_decode_8(p);
1694 struct_cv = ceph_decode_8(p);
1695 if (struct_v < 3) {
1696 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1697 struct_v, struct_cv);
1698 goto e_inval;
1699 }
1700 if (struct_cv > 6) {
1701 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1702 struct_v, struct_cv);
1703 goto e_inval;
1704 }
1705 len = ceph_decode_32(p);
1706 ceph_decode_need(p, end, len, e_inval);
1707 struct_end = *p + len;
1708
1709 oloc->pool = ceph_decode_64(p);
1710 *p += 4; /* skip preferred */
1711
1712 len = ceph_decode_32(p);
1713 if (len > 0) {
1714 pr_warn("ceph_object_locator::key is set\n");
1715 goto e_inval;
1716 }
1717
1718 if (struct_v >= 5) {
1719 len = ceph_decode_32(p);
1720 if (len > 0) {
1721 pr_warn("ceph_object_locator::nspace is set\n");
1722 goto e_inval;
1723 }
1724 }
1725
1726 if (struct_v >= 6) {
1727 s64 hash = ceph_decode_64(p);
1728 if (hash != -1) {
1729 pr_warn("ceph_object_locator::hash is set\n");
1730 goto e_inval;
1731 }
1732 }
1733
1734 /* skip the rest */
1735 *p = struct_end;
1736out:
1737 return ret;
1738
1739e_inval:
1740 ret = -EINVAL;
1741 goto out;
1742}
1743
1744static int ceph_redirect_decode(void **p, void *end,
1745 struct ceph_request_redirect *redir)
1746{
1747 u8 struct_v, struct_cv;
1748 u32 len;
1749 void *struct_end;
1750 int ret;
1751
1752 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1753 struct_v = ceph_decode_8(p);
1754 struct_cv = ceph_decode_8(p);
1755 if (struct_cv > 1) {
1756 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1757 struct_v, struct_cv);
1758 goto e_inval;
1759 }
1760 len = ceph_decode_32(p);
1761 ceph_decode_need(p, end, len, e_inval);
1762 struct_end = *p + len;
1763
1764 ret = ceph_oloc_decode(p, end, &redir->oloc);
1765 if (ret)
1766 goto out;
1767
1768 len = ceph_decode_32(p);
1769 if (len > 0) {
1770 pr_warn("ceph_request_redirect::object_name is set\n");
1771 goto e_inval;
1772 }
1773
1774 len = ceph_decode_32(p);
1775 *p += len; /* skip osd_instructions */
1776
1777 /* skip the rest */
1778 *p = struct_end;
1779out:
1780 return ret;
1781
1782e_inval:
1783 ret = -EINVAL;
1784 goto out;
1785}
1786
Sage Weil25845472011-06-03 09:37:09 -07001787static void complete_request(struct ceph_osd_request *req)
1788{
Sage Weil25845472011-06-03 09:37:09 -07001789 complete_all(&req->r_safe_completion); /* fsync waiter */
1790}
1791
Sage Weilf24e9982009-10-06 11:31:10 -07001792/*
1793 * handle osd op reply. either call the callback if it is specified,
1794 * or do the completion to wake up the waiting thread.
1795 */
Shraddha Barke70cf0522015-10-18 13:55:38 +05301796static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001797{
Sage Weil1b83bef2013-02-25 16:11:12 -08001798 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001799 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001800 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001801 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001802 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001803 unsigned int numops;
1804 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001805 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001806 s32 retry_attempt;
1807 struct ceph_pg pg;
1808 int err;
1809 u32 reassert_epoch;
1810 u64 reassert_version;
1811 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001812 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001813 u32 bytes;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001814 u8 decode_redir;
Alex Elder79528732013-04-03 21:32:51 -05001815 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001816
Sage Weil6df058c2009-12-22 11:24:33 -08001817 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001818 dout("handle_reply %p tid %llu\n", msg, tid);
1819
1820 p = msg->front.iov_base;
1821 end = p + msg->front.iov_len;
1822
1823 ceph_decode_need(&p, end, 4, bad);
1824 object_len = ceph_decode_32(&p);
1825 ceph_decode_need(&p, end, object_len, bad);
1826 p += object_len;
1827
Alex Elderef4859d2013-04-01 18:58:26 -05001828 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001829 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001830 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001831
1832 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1833 flags = ceph_decode_64(&p);
1834 result = ceph_decode_32(&p);
1835 reassert_epoch = ceph_decode_32(&p);
1836 reassert_version = ceph_decode_64(&p);
1837 osdmap_epoch = ceph_decode_32(&p);
1838
Sage Weilf24e9982009-10-06 11:31:10 -07001839 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001840 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001841 mutex_lock(&osdc->request_mutex);
1842 req = __lookup_request(osdc, tid);
1843 if (req == NULL) {
1844 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001845 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001846 }
1847 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001848
1849 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1850 req, result);
1851
Dan Carpenter18741192013-08-15 08:51:58 +03001852 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001853 numops = ceph_decode_32(&p);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01001854 if (numops > CEPH_OSD_MAX_OPS)
Sage Weil1b83bef2013-02-25 16:11:12 -08001855 goto bad_put;
1856 if (numops != req->r_num_ops)
1857 goto bad_put;
1858 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001859 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001860 for (i = 0; i < numops; i++) {
1861 struct ceph_osd_op *op = p;
1862 int len;
1863
1864 len = le32_to_cpu(op->payload_len);
Yan, Zheng7665d852016-01-07 16:48:57 +08001865 req->r_ops[i].outdata_len = len;
Sage Weil1b83bef2013-02-25 16:11:12 -08001866 dout(" op %d has %d bytes\n", i, len);
1867 payload_len += len;
1868 p += sizeof(*op);
1869 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001870 bytes = le32_to_cpu(msg->hdr.data_len);
1871 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001872 pr_warn("sum of op payload lens %d != data_len %d\n",
1873 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001874 goto bad_put;
1875 }
1876
Dan Carpenter18741192013-08-15 08:51:58 +03001877 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001878 retry_attempt = ceph_decode_32(&p);
1879 for (i = 0; i < numops; i++)
Yan, Zheng7665d852016-01-07 16:48:57 +08001880 req->r_ops[i].rval = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001881
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001882 if (le16_to_cpu(msg->hdr.version) >= 6) {
1883 p += 8 + 4; /* skip replay_version */
1884 p += 8; /* skip user_version */
1885
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001886 if (le16_to_cpu(msg->hdr.version) >= 7)
1887 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1888 else
1889 decode_redir = 1;
1890 } else {
1891 decode_redir = 0;
1892 }
1893
1894 if (decode_redir) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001895 err = ceph_redirect_decode(&p, end, &redir);
1896 if (err)
1897 goto bad_put;
1898 } else {
1899 redir.oloc.pool = -1;
1900 }
1901
1902 if (redir.oloc.pool != -1) {
1903 dout("redirect pool %lld\n", redir.oloc.pool);
1904
1905 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001906
1907 req->r_target_oloc = redir.oloc; /* struct */
1908
1909 /*
1910 * Start redirect requests with nofail=true. If
1911 * mapping fails, request will end up on the notarget
1912 * list, waiting for the new osdmap (which can take
1913 * a while), even though the original request mapped
1914 * successfully. In the future we might want to follow
1915 * original request's nofail setting here.
1916 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001917 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001918 BUG_ON(err);
1919
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001920 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001921 }
1922
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001923 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001924 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001925 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001926 dout("handle_reply result %d bytes %d\n", req->r_result,
1927 bytes);
1928 if (req->r_result == 0)
1929 req->r_result = bytes;
1930
1931 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001932 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1933 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001934
1935 req->r_got_reply = 1;
1936 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1937 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001938 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001939 }
1940
1941 dout("handle_reply tid %llu flags %d\n", tid, flags);
1942
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001943 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1944 __register_linger_request(osdc, req);
1945
Sage Weilf24e9982009-10-06 11:31:10 -07001946 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001947 if (result < 0 ||
1948 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001949 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1950 __unregister_request(osdc, req);
1951
1952 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001953 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001954
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001955 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001956 if (req->r_unsafe_callback &&
1957 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1958 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001959 if (req->r_callback)
1960 req->r_callback(req, msg);
1961 else
1962 complete_all(&req->r_completion);
1963 }
Sage Weilf24e9982009-10-06 11:31:10 -07001964
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001965 if (flags & CEPH_OSD_FLAG_ONDISK) {
1966 if (req->r_unsafe_callback && already_completed)
1967 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001968 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001969 }
Sage Weilf24e9982009-10-06 11:31:10 -07001970
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001971out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001972 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001973 ceph_osdc_put_request(req);
1974 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001975out_unlock:
1976 mutex_unlock(&osdc->request_mutex);
1977 up_read(&osdc->map_sem);
1978 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001979
Sage Weil1b83bef2013-02-25 16:11:12 -08001980bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001981 req->r_result = -EIO;
1982 __unregister_request(osdc, req);
1983 if (req->r_callback)
1984 req->r_callback(req, msg);
1985 else
1986 complete_all(&req->r_completion);
1987 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001988 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001989bad_mutex:
1990 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001991 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001992bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001993 pr_err("corrupt osd_op_reply got %d %d\n",
1994 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001995 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001996}
1997
Sage Weil6f6c7002011-01-17 20:34:08 -08001998static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001999{
Sage Weilf24e9982009-10-06 11:31:10 -07002000 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07002001
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03002002 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08002003 for (p = rb_first(&osdc->osds); p; p = n) {
2004 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07002005
Sage Weil6f6c7002011-01-17 20:34:08 -08002006 n = rb_next(p);
2007 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
2008 memcmp(&osd->o_con.peer_addr,
2009 ceph_osd_addr(osdc->osdmap,
2010 osd->o_osd),
2011 sizeof(struct ceph_entity_addr)) != 0)
2012 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07002013 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002014}
2015
2016/*
Sage Weil6f6c7002011-01-17 20:34:08 -08002017 * Requeue requests whose mapping to an OSD has changed. If requests map to
2018 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002019 *
Alex Eldere6d50f62012-12-26 14:31:40 -06002020 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002021 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002022static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
2023 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002024{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002025 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08002026 struct rb_node *p;
2027 int needmap = 0;
2028 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002029 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002030
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002031 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2032 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002033 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07002034 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08002035 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07002036 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06002037
2038 /*
2039 * For linger requests that have not yet been
2040 * registered, move them to the linger list; they'll
2041 * be sent to the osd in the loop below. Unregister
2042 * the request before re-registering it as a linger
2043 * request to ensure the __map_request() below
2044 * will decide it needs to be sent.
2045 */
2046 if (req->r_linger && list_empty(&req->r_linger_item)) {
2047 dout("%p tid %llu restart on osd%d\n",
2048 req, req->r_tid,
2049 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05002050 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002051 __unregister_request(osdc, req);
2052 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002053 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002054 continue;
2055 }
2056
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002057 force_resend_req = force_resend ||
2058 (force_resend_writes &&
2059 req->r_flags & CEPH_OSD_FLAG_WRITE);
2060 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002061 if (err < 0)
2062 continue; /* error */
2063 if (req->r_osd == NULL) {
2064 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2065 needmap++; /* request a newer map */
2066 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002067 if (!req->r_linger) {
2068 dout("%p tid %llu requeued on osd%d\n", req,
2069 req->r_tid,
2070 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002071 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002072 }
2073 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002074 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002075
2076 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2077 r_linger_item) {
2078 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2079
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002080 err = __map_request(osdc, req,
2081 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002082 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002083 if (err < 0)
2084 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002085 if (req->r_osd == NULL || err > 0) {
2086 if (req->r_osd == NULL) {
2087 dout("lingering %p tid %llu maps to no osd\n",
2088 req, req->r_tid);
2089 /*
2090 * A homeless lingering request makes
2091 * no sense, as it's job is to keep
2092 * a particular OSD connection open.
2093 * Request a newer map and kick the
2094 * request, knowing that it won't be
2095 * resent until we actually get a map
2096 * that can tell us where to send it.
2097 */
2098 needmap++;
2099 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002100
Ilya Dryomovb0494532015-05-11 17:53:10 +03002101 dout("kicking lingering %p tid %llu osd%d\n", req,
2102 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2103 __register_request(osdc, req);
2104 __unregister_linger_request(osdc, req);
2105 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002106 }
Alex Elder14d2f382013-05-15 16:28:33 -05002107 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002108 mutex_unlock(&osdc->request_mutex);
2109
2110 if (needmap) {
2111 dout("%d requests for down osds, need new map\n", needmap);
2112 ceph_monc_request_next_osdmap(&osdc->client->monc);
2113 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002114}
Sage Weil6f6c7002011-01-17 20:34:08 -08002115
2116
Sage Weilf24e9982009-10-06 11:31:10 -07002117/*
2118 * Process updated osd map.
2119 *
2120 * The message contains any number of incremental and full maps, normally
2121 * indicating some sort of topology change in the cluster. Kick requests
2122 * off to different OSDs as needed.
2123 */
2124void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2125{
2126 void *p, *end, *next;
2127 u32 nr_maps, maplen;
2128 u32 epoch;
2129 struct ceph_osdmap *newmap = NULL, *oldmap;
2130 int err;
2131 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002132 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002133
2134 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2135 p = msg->front.iov_base;
2136 end = p + msg->front.iov_len;
2137
2138 /* verify fsid */
2139 ceph_decode_need(&p, end, sizeof(fsid), bad);
2140 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002141 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2142 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002143
2144 down_write(&osdc->map_sem);
2145
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002146 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2147
Sage Weilf24e9982009-10-06 11:31:10 -07002148 /* incremental maps */
2149 ceph_decode_32_safe(&p, end, nr_maps, bad);
2150 dout(" %d inc maps\n", nr_maps);
2151 while (nr_maps > 0) {
2152 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002153 epoch = ceph_decode_32(&p);
2154 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002155 ceph_decode_need(&p, end, maplen, bad);
2156 next = p + maplen;
2157 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2158 dout("applying incremental map %u len %d\n",
2159 epoch, maplen);
2160 newmap = osdmap_apply_incremental(&p, next,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02002161 osdc->osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002162 if (IS_ERR(newmap)) {
2163 err = PTR_ERR(newmap);
2164 goto bad;
2165 }
Sage Weil30dc6382009-12-21 14:49:37 -08002166 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002167 if (newmap != osdc->osdmap) {
2168 ceph_osdmap_destroy(osdc->osdmap);
2169 osdc->osdmap = newmap;
2170 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002171 was_full = was_full ||
2172 ceph_osdmap_flag(osdc->osdmap,
2173 CEPH_OSDMAP_FULL);
2174 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002175 } else {
2176 dout("ignoring incremental map %u len %d\n",
2177 epoch, maplen);
2178 }
2179 p = next;
2180 nr_maps--;
2181 }
2182 if (newmap)
2183 goto done;
2184
2185 /* full maps */
2186 ceph_decode_32_safe(&p, end, nr_maps, bad);
2187 dout(" %d full maps\n", nr_maps);
2188 while (nr_maps) {
2189 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002190 epoch = ceph_decode_32(&p);
2191 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002192 ceph_decode_need(&p, end, maplen, bad);
2193 if (nr_maps > 1) {
2194 dout("skipping non-latest full map %u len %d\n",
2195 epoch, maplen);
2196 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2197 dout("skipping full map %u len %d, "
2198 "older than our %u\n", epoch, maplen,
2199 osdc->osdmap->epoch);
2200 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002201 int skipped_map = 0;
2202
Sage Weilf24e9982009-10-06 11:31:10 -07002203 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002204 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002205 if (IS_ERR(newmap)) {
2206 err = PTR_ERR(newmap);
2207 goto bad;
2208 }
Sage Weil30dc6382009-12-21 14:49:37 -08002209 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002210 oldmap = osdc->osdmap;
2211 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002212 if (oldmap) {
2213 if (oldmap->epoch + 1 < newmap->epoch)
2214 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002215 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002216 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002217 was_full = was_full ||
2218 ceph_osdmap_flag(osdc->osdmap,
2219 CEPH_OSDMAP_FULL);
2220 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002221 }
2222 p += maplen;
2223 nr_maps--;
2224 }
2225
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002226 if (!osdc->osdmap)
2227 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002228done:
2229 downgrade_write(&osdc->map_sem);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01002230 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2231 osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002232
2233 /*
2234 * subscribe to subsequent osdmap updates if full to ensure
2235 * we find out when we are no longer full and stop returning
2236 * ENOSPC.
2237 */
Josh Durgind29adb32013-12-02 19:11:48 -08002238 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2239 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2240 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002241 ceph_monc_request_next_osdmap(&osdc->client->monc);
2242
Alex Elderf9d25192013-02-15 11:42:29 -06002243 mutex_lock(&osdc->request_mutex);
2244 __send_queued(osdc);
2245 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002246 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002247 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002248 return;
2249
2250bad:
2251 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002252 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002253 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002254}
2255
Sage Weilf24e9982009-10-06 11:31:10 -07002256/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002257 * watch/notify callback event infrastructure
2258 *
2259 * These callbacks are used both for watch and notify operations.
2260 */
2261static void __release_event(struct kref *kref)
2262{
2263 struct ceph_osd_event *event =
2264 container_of(kref, struct ceph_osd_event, kref);
2265
2266 dout("__release_event %p\n", event);
2267 kfree(event);
2268}
2269
2270static void get_event(struct ceph_osd_event *event)
2271{
2272 kref_get(&event->kref);
2273}
2274
2275void ceph_osdc_put_event(struct ceph_osd_event *event)
2276{
2277 kref_put(&event->kref, __release_event);
2278}
2279EXPORT_SYMBOL(ceph_osdc_put_event);
2280
2281static void __insert_event(struct ceph_osd_client *osdc,
2282 struct ceph_osd_event *new)
2283{
2284 struct rb_node **p = &osdc->event_tree.rb_node;
2285 struct rb_node *parent = NULL;
2286 struct ceph_osd_event *event = NULL;
2287
2288 while (*p) {
2289 parent = *p;
2290 event = rb_entry(parent, struct ceph_osd_event, node);
2291 if (new->cookie < event->cookie)
2292 p = &(*p)->rb_left;
2293 else if (new->cookie > event->cookie)
2294 p = &(*p)->rb_right;
2295 else
2296 BUG();
2297 }
2298
2299 rb_link_node(&new->node, parent, p);
2300 rb_insert_color(&new->node, &osdc->event_tree);
2301}
2302
2303static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2304 u64 cookie)
2305{
2306 struct rb_node **p = &osdc->event_tree.rb_node;
2307 struct rb_node *parent = NULL;
2308 struct ceph_osd_event *event = NULL;
2309
2310 while (*p) {
2311 parent = *p;
2312 event = rb_entry(parent, struct ceph_osd_event, node);
2313 if (cookie < event->cookie)
2314 p = &(*p)->rb_left;
2315 else if (cookie > event->cookie)
2316 p = &(*p)->rb_right;
2317 else
2318 return event;
2319 }
2320 return NULL;
2321}
2322
2323static void __remove_event(struct ceph_osd_event *event)
2324{
2325 struct ceph_osd_client *osdc = event->osdc;
2326
2327 if (!RB_EMPTY_NODE(&event->node)) {
2328 dout("__remove_event removed %p\n", event);
2329 rb_erase(&event->node, &osdc->event_tree);
2330 ceph_osdc_put_event(event);
2331 } else {
2332 dout("__remove_event didn't remove %p\n", event);
2333 }
2334}
2335
2336int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2337 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002338 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002339{
2340 struct ceph_osd_event *event;
2341
2342 event = kmalloc(sizeof(*event), GFP_NOIO);
2343 if (!event)
2344 return -ENOMEM;
2345
2346 dout("create_event %p\n", event);
2347 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002348 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002349 event->data = data;
2350 event->osdc = osdc;
2351 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002352 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002353 kref_init(&event->kref); /* one ref for us */
2354 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002355
2356 spin_lock(&osdc->event_lock);
2357 event->cookie = ++osdc->event_count;
2358 __insert_event(osdc, event);
2359 spin_unlock(&osdc->event_lock);
2360
2361 *pevent = event;
2362 return 0;
2363}
2364EXPORT_SYMBOL(ceph_osdc_create_event);
2365
2366void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2367{
2368 struct ceph_osd_client *osdc = event->osdc;
2369
2370 dout("cancel_event %p\n", event);
2371 spin_lock(&osdc->event_lock);
2372 __remove_event(event);
2373 spin_unlock(&osdc->event_lock);
2374 ceph_osdc_put_event(event); /* caller's */
2375}
2376EXPORT_SYMBOL(ceph_osdc_cancel_event);
2377
2378
2379static void do_event_work(struct work_struct *work)
2380{
2381 struct ceph_osd_event_work *event_work =
2382 container_of(work, struct ceph_osd_event_work, work);
2383 struct ceph_osd_event *event = event_work->event;
2384 u64 ver = event_work->ver;
2385 u64 notify_id = event_work->notify_id;
2386 u8 opcode = event_work->opcode;
2387
2388 dout("do_event_work completing %p\n", event);
2389 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002390 dout("do_event_work completed %p\n", event);
2391 ceph_osdc_put_event(event);
2392 kfree(event_work);
2393}
2394
2395
2396/*
2397 * Process osd watch notifications
2398 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002399static void handle_watch_notify(struct ceph_osd_client *osdc,
2400 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002401{
2402 void *p, *end;
2403 u8 proto_ver;
2404 u64 cookie, ver, notify_id;
2405 u8 opcode;
2406 struct ceph_osd_event *event;
2407 struct ceph_osd_event_work *event_work;
2408
2409 p = msg->front.iov_base;
2410 end = p + msg->front.iov_len;
2411
2412 ceph_decode_8_safe(&p, end, proto_ver, bad);
2413 ceph_decode_8_safe(&p, end, opcode, bad);
2414 ceph_decode_64_safe(&p, end, cookie, bad);
2415 ceph_decode_64_safe(&p, end, ver, bad);
2416 ceph_decode_64_safe(&p, end, notify_id, bad);
2417
2418 spin_lock(&osdc->event_lock);
2419 event = __find_event(osdc, cookie);
2420 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002421 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002422 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002423 }
2424 spin_unlock(&osdc->event_lock);
2425 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2426 cookie, ver, event);
2427 if (event) {
2428 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002429 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002430 pr_err("couldn't allocate event_work\n");
2431 ceph_osdc_put_event(event);
2432 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002433 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002434 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002435 event_work->event = event;
2436 event_work->ver = ver;
2437 event_work->notify_id = notify_id;
2438 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002439
2440 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002441 }
2442
2443 return;
2444
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002445bad:
2446 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002447}
2448
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002449/*
Alex Eldere65550f2013-04-05 01:27:12 -05002450 * build new request AND message
2451 *
2452 */
2453void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2454 struct ceph_snap_context *snapc, u64 snap_id,
2455 struct timespec *mtime)
2456{
2457 struct ceph_msg *msg = req->r_request;
2458 void *p;
2459 size_t msg_size;
2460 int flags = req->r_flags;
2461 u64 data_len;
2462 unsigned int i;
2463
2464 req->r_snapid = snap_id;
Ilya Dryomov84127282016-04-26 15:39:47 +02002465 WARN_ON(snapc != req->r_snapc);
Alex Eldere65550f2013-04-05 01:27:12 -05002466
2467 /* encode request */
2468 msg->hdr.version = cpu_to_le16(4);
2469
2470 p = msg->front.iov_base;
2471 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2472 req->r_request_osdmap_epoch = p;
2473 p += 4;
2474 req->r_request_flags = p;
2475 p += 4;
2476 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2477 ceph_encode_timespec(p, mtime);
2478 p += sizeof(struct ceph_timespec);
2479 req->r_request_reassert_version = p;
2480 p += sizeof(struct ceph_eversion); /* will get filled in */
2481
2482 /* oloc */
2483 ceph_encode_8(&p, 4);
2484 ceph_encode_8(&p, 4);
2485 ceph_encode_32(&p, 8 + 4 + 4);
2486 req->r_request_pool = p;
2487 p += 8;
2488 ceph_encode_32(&p, -1); /* preferred */
2489 ceph_encode_32(&p, 0); /* key len */
2490
2491 ceph_encode_8(&p, 1);
2492 req->r_request_pgid = p;
2493 p += 8 + 4;
2494 ceph_encode_32(&p, -1); /* preferred */
2495
2496 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002497 ceph_encode_32(&p, req->r_base_oid.name_len);
2498 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002499 dout("oid %*pE len %d\n", req->r_base_oid.name_len,
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002500 req->r_base_oid.name, req->r_base_oid.name_len);
2501 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002502
2503 /* ops--can imply data */
2504 ceph_encode_16(&p, (u16)req->r_num_ops);
2505 data_len = 0;
2506 for (i = 0; i < req->r_num_ops; i++) {
2507 data_len += osd_req_encode_op(req, p, i);
2508 p += sizeof(struct ceph_osd_op);
2509 }
2510
2511 /* snaps */
2512 ceph_encode_64(&p, req->r_snapid);
2513 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2514 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2515 if (req->r_snapc) {
Ilya Dryomov84127282016-04-26 15:39:47 +02002516 for (i = 0; i < req->r_snapc->num_snaps; i++) {
Alex Eldere65550f2013-04-05 01:27:12 -05002517 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2518 }
2519 }
2520
2521 req->r_request_attempts = p;
2522 p += 4;
2523
2524 /* data */
2525 if (flags & CEPH_OSD_FLAG_WRITE) {
2526 u16 data_off;
2527
2528 /*
2529 * The header "data_off" is a hint to the receiver
2530 * allowing it to align received data into its
2531 * buffers such that there's no need to re-copy
2532 * it before writing it to disk (direct I/O).
2533 */
2534 data_off = (u16) (off & 0xffff);
2535 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2536 }
2537 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2538
2539 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2540 msg_size = p - msg->front.iov_base;
2541 msg->front.iov_len = msg_size;
2542 msg->hdr.front_len = cpu_to_le32(msg_size);
2543
2544 dout("build_request msg_size was %d\n", (int)msg_size);
2545}
2546EXPORT_SYMBOL(ceph_osdc_build_request);
2547
2548/*
Sage Weilf24e9982009-10-06 11:31:10 -07002549 * Register request, send initial attempt.
2550 */
2551int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2552 struct ceph_osd_request *req,
2553 bool nofail)
2554{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002555 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002556
Sage Weilf24e9982009-10-06 11:31:10 -07002557 down_read(&osdc->map_sem);
2558 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002559
2560 rc = __ceph_osdc_start_request(osdc, req, nofail);
2561
Sage Weilf24e9982009-10-06 11:31:10 -07002562 mutex_unlock(&osdc->request_mutex);
2563 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002564
Sage Weilf24e9982009-10-06 11:31:10 -07002565 return rc;
2566}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002567EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002568
2569/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002570 * Unregister a registered request. The request is not completed (i.e.
2571 * no callbacks or wakeups) - higher layers are supposed to know what
2572 * they are canceling.
2573 */
2574void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2575{
2576 struct ceph_osd_client *osdc = req->r_osdc;
2577
2578 mutex_lock(&osdc->request_mutex);
2579 if (req->r_linger)
2580 __unregister_linger_request(osdc, req);
2581 __unregister_request(osdc, req);
2582 mutex_unlock(&osdc->request_mutex);
2583
2584 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2585}
2586EXPORT_SYMBOL(ceph_osdc_cancel_request);
2587
2588/*
Sage Weilf24e9982009-10-06 11:31:10 -07002589 * wait for a request to complete
2590 */
2591int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2592 struct ceph_osd_request *req)
2593{
2594 int rc;
2595
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002596 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2597
Sage Weilf24e9982009-10-06 11:31:10 -07002598 rc = wait_for_completion_interruptible(&req->r_completion);
2599 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002600 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2601 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002602 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002603 return rc;
2604 }
2605
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002606 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2607 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002608 return req->r_result;
2609}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002610EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002611
2612/*
2613 * sync - wait for all in-flight requests to flush. avoid starvation.
2614 */
2615void ceph_osdc_sync(struct ceph_osd_client *osdc)
2616{
2617 struct ceph_osd_request *req;
2618 u64 last_tid, next_tid = 0;
2619
2620 mutex_lock(&osdc->request_mutex);
2621 last_tid = osdc->last_tid;
2622 while (1) {
2623 req = __lookup_request_ge(osdc, next_tid);
2624 if (!req)
2625 break;
2626 if (req->r_tid > last_tid)
2627 break;
2628
2629 next_tid = req->r_tid + 1;
2630 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2631 continue;
2632
2633 ceph_osdc_get_request(req);
2634 mutex_unlock(&osdc->request_mutex);
2635 dout("sync waiting on tid %llu (last is %llu)\n",
2636 req->r_tid, last_tid);
2637 wait_for_completion(&req->r_safe_completion);
2638 mutex_lock(&osdc->request_mutex);
2639 ceph_osdc_put_request(req);
2640 }
2641 mutex_unlock(&osdc->request_mutex);
2642 dout("sync done (thru tid %llu)\n", last_tid);
2643}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002644EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002645
2646/*
Josh Durgindd935f42013-08-28 21:43:09 -07002647 * Call all pending notify callbacks - for use after a watch is
2648 * unregistered, to make sure no more callbacks for it will be invoked
2649 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002650void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002651{
2652 flush_workqueue(osdc->notify_wq);
2653}
2654EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2655
2656
2657/*
Sage Weilf24e9982009-10-06 11:31:10 -07002658 * init, shutdown
2659 */
2660int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2661{
2662 int err;
2663
2664 dout("init\n");
2665 osdc->client = client;
2666 osdc->osdmap = NULL;
2667 init_rwsem(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002668 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002669 osdc->last_tid = 0;
2670 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002671 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002672 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002673 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002674 INIT_LIST_HEAD(&osdc->req_unsent);
2675 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002676 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002677 osdc->num_requests = 0;
2678 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002679 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002680 spin_lock_init(&osdc->event_lock);
2681 osdc->event_tree = RB_ROOT;
2682 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002683
2684 schedule_delayed_work(&osdc->osds_timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03002685 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
Sage Weilf24e9982009-10-06 11:31:10 -07002686
Sage Weil5f44f142009-11-18 14:52:18 -08002687 err = -ENOMEM;
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01002688 osdc->req_mempool = mempool_create_slab_pool(10,
2689 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07002690 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002691 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002692
Sage Weild50b4092012-07-09 14:22:34 -07002693 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02002694 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002695 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002696 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002697 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02002698 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002699 if (err < 0)
2700 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002701
Dan Carpenterdbcae082013-08-15 08:58:59 +03002702 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002703 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002704 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002705 goto out_msgpool_reply;
2706
Sage Weilf24e9982009-10-06 11:31:10 -07002707 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002708
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002709out_msgpool_reply:
2710 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002711out_msgpool:
2712 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002713out_mempool:
2714 mempool_destroy(osdc->req_mempool);
2715out:
2716 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002717}
2718
2719void ceph_osdc_stop(struct ceph_osd_client *osdc)
2720{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002721 flush_workqueue(osdc->notify_wq);
2722 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002723 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002724 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002725 if (osdc->osdmap) {
2726 ceph_osdmap_destroy(osdc->osdmap);
2727 osdc->osdmap = NULL;
2728 }
Sage Weilaca420b2011-08-31 14:45:53 -07002729 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002730 mempool_destroy(osdc->req_mempool);
2731 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002732 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002733}
2734
2735/*
2736 * Read some contiguous pages. If we cross a stripe boundary, shorten
2737 * *plen. Return number of bytes read, or error.
2738 */
2739int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2740 struct ceph_vino vino, struct ceph_file_layout *layout,
2741 u64 off, u64 *plen,
2742 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002743 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002744{
2745 struct ceph_osd_request *req;
2746 int rc = 0;
2747
2748 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2749 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002750 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002751 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002752 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002753 false);
Sage Weil68162822012-09-24 21:01:02 -07002754 if (IS_ERR(req))
2755 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002756
2757 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002758
Alex Elder406e2c92013-04-15 14:50:36 -05002759 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002760 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002761
Alex Eldere0c59482013-03-07 15:38:25 -06002762 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002763 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002764
Alex Elder79528732013-04-03 21:32:51 -05002765 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002766
Sage Weilf24e9982009-10-06 11:31:10 -07002767 rc = ceph_osdc_start_request(osdc, req, false);
2768 if (!rc)
2769 rc = ceph_osdc_wait_request(osdc, req);
2770
2771 ceph_osdc_put_request(req);
2772 dout("readpages result %d\n", rc);
2773 return rc;
2774}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002775EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002776
2777/*
2778 * do a synchronous write on N pages
2779 */
2780int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2781 struct ceph_file_layout *layout,
2782 struct ceph_snap_context *snapc,
2783 u64 off, u64 len,
2784 u32 truncate_seq, u64 truncate_size,
2785 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002786 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002787{
2788 struct ceph_osd_request *req;
2789 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002790 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002791
Alex Elderacead002013-03-14 14:09:05 -05002792 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002793 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002794 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002795 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002796 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002797 true);
Sage Weil68162822012-09-24 21:01:02 -07002798 if (IS_ERR(req))
2799 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002800
2801 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002802 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002803 false, false);
2804 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002805
Alex Elder79528732013-04-03 21:32:51 -05002806 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002807
Alex Elder87f979d2013-02-15 11:42:29 -06002808 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002809 if (!rc)
2810 rc = ceph_osdc_wait_request(osdc, req);
2811
2812 ceph_osdc_put_request(req);
2813 if (rc == 0)
2814 rc = len;
2815 dout("writepages result %d\n", rc);
2816 return rc;
2817}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002818EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002819
Alex Elder5522ae02013-05-01 12:43:04 -05002820int ceph_osdc_setup(void)
2821{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002822 size_t size = sizeof(struct ceph_osd_request) +
2823 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2824
Alex Elder5522ae02013-05-01 12:43:04 -05002825 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002826 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2827 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05002828
2829 return ceph_osd_request_cache ? 0 : -ENOMEM;
2830}
2831EXPORT_SYMBOL(ceph_osdc_setup);
2832
2833void ceph_osdc_cleanup(void)
2834{
2835 BUG_ON(!ceph_osd_request_cache);
2836 kmem_cache_destroy(ceph_osd_request_cache);
2837 ceph_osd_request_cache = NULL;
2838}
2839EXPORT_SYMBOL(ceph_osdc_cleanup);
2840
Sage Weilf24e9982009-10-06 11:31:10 -07002841/*
2842 * handle incoming message
2843 */
2844static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2845{
2846 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002847 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002848 int type = le16_to_cpu(msg->hdr.type);
2849
2850 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002851 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002852 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002853
2854 switch (type) {
2855 case CEPH_MSG_OSD_MAP:
2856 ceph_osdc_handle_map(osdc, msg);
2857 break;
2858 case CEPH_MSG_OSD_OPREPLY:
Shraddha Barke70cf0522015-10-18 13:55:38 +05302859 handle_reply(osdc, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002860 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002861 case CEPH_MSG_WATCH_NOTIFY:
2862 handle_watch_notify(osdc, msg);
2863 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002864
2865 default:
2866 pr_err("received unknown message type %d %s\n", type,
2867 ceph_msg_type_name(type));
2868 }
Sage Weil4a32f932010-06-13 10:27:53 -07002869out:
Sage Weilf24e9982009-10-06 11:31:10 -07002870 ceph_msg_put(msg);
2871}
2872
Sage Weil5b3a4db2010-02-19 21:43:23 -08002873/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002874 * Lookup and return message for incoming reply. Don't try to do
2875 * anything about a larger than preallocated data portion of the
2876 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002877 */
2878static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002879 struct ceph_msg_header *hdr,
2880 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002881{
2882 struct ceph_osd *osd = con->private;
2883 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002884 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002885 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002886 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002887 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002888 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002889
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002890 tid = le64_to_cpu(hdr->tid);
2891 mutex_lock(&osdc->request_mutex);
2892 req = __lookup_request(osdc, tid);
2893 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01002894 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2895 osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002896 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002897 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002898 goto out;
2899 }
Sage Weilc16e7862010-03-01 13:02:00 -08002900
Alex Elderace6d3a2013-04-01 16:12:14 -05002901 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002902
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002903 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002904 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2905 __func__, osd->o_osd, req->r_tid, front_len,
2906 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002907 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2908 false);
Sage Weila79832f2010-04-01 16:06:19 -07002909 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002910 goto out;
2911 ceph_msg_put(req->r_reply);
2912 req->r_reply = m;
2913 }
Sage Weilc16e7862010-03-01 13:02:00 -08002914
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002915 if (data_len > req->r_reply->data_length) {
2916 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2917 __func__, osd->o_osd, req->r_tid, data_len,
2918 req->r_reply->data_length);
2919 m = NULL;
2920 *skip = 1;
2921 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002922 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002923
2924 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002925 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002926
2927out:
2928 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002929 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002930}
2931
2932static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2933 struct ceph_msg_header *hdr,
2934 int *skip)
2935{
2936 struct ceph_osd *osd = con->private;
2937 int type = le16_to_cpu(hdr->type);
2938 int front = le32_to_cpu(hdr->front_len);
2939
Alex Elder1c20f2d2012-06-04 14:43:32 -05002940 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002941 switch (type) {
2942 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002943 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002944 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002945 case CEPH_MSG_OSD_OPREPLY:
2946 return get_reply(con, hdr, skip);
2947 default:
2948 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2949 osd->o_osd);
2950 *skip = 1;
2951 return NULL;
2952 }
Sage Weilf24e9982009-10-06 11:31:10 -07002953}
2954
2955/*
2956 * Wrappers to refcount containing ceph_osd struct
2957 */
2958static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2959{
2960 struct ceph_osd *osd = con->private;
2961 if (get_osd(osd))
2962 return con;
2963 return NULL;
2964}
2965
2966static void put_osd_con(struct ceph_connection *con)
2967{
2968 struct ceph_osd *osd = con->private;
2969 put_osd(osd);
2970}
2971
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002972/*
2973 * authentication
2974 */
Alex Eldera3530df2012-05-16 15:16:39 -05002975/*
2976 * Note: returned pointer is the address of a structure that's
2977 * managed separately. Caller must *not* attempt to free it.
2978 */
2979static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002980 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002981{
2982 struct ceph_osd *o = con->private;
2983 struct ceph_osd_client *osdc = o->o_osdc;
2984 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002985 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002986
Alex Elder74f18692012-05-16 15:16:39 -05002987 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02002988 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002989 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002990 }
Sage Weil27859f92013-03-25 10:26:14 -07002991 if (!auth->authorizer) {
2992 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2993 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002994 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002995 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002996 } else {
2997 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002998 auth);
2999 if (ret)
3000 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003001 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003002 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05003003
Alex Eldera3530df2012-05-16 15:16:39 -05003004 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003005}
3006
3007
3008static int verify_authorizer_reply(struct ceph_connection *con, int len)
3009{
3010 struct ceph_osd *o = con->private;
3011 struct ceph_osd_client *osdc = o->o_osdc;
3012 struct ceph_auth_client *ac = osdc->client->monc.auth;
3013
Sage Weil27859f92013-03-25 10:26:14 -07003014 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003015}
3016
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003017static int invalidate_authorizer(struct ceph_connection *con)
3018{
3019 struct ceph_osd *o = con->private;
3020 struct ceph_osd_client *osdc = o->o_osdc;
3021 struct ceph_auth_client *ac = osdc->client->monc.auth;
3022
Sage Weil27859f92013-03-25 10:26:14 -07003023 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003024 return ceph_monc_validate_auth(&osdc->client->monc);
3025}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003026
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003027static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003028{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003029 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003030 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003031
Yan, Zheng33d07332014-11-04 16:33:37 +08003032 return ceph_auth_sign_message(auth, msg);
3033}
3034
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003035static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003036{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003037 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003038 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003039
Yan, Zheng33d07332014-11-04 16:33:37 +08003040 return ceph_auth_check_message_signature(auth, msg);
3041}
3042
Tobias Klauser9e327892010-05-20 10:40:19 +02003043static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07003044 .get = get_osd_con,
3045 .put = put_osd_con,
3046 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003047 .get_authorizer = get_authorizer,
3048 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003049 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003050 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003051 .sign_message = osd_sign_message,
3052 .check_message_signature = osd_check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003053 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003054};