blob: 95910aed8e2e60930aba1b3ef3b3b54233662db5 [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
146struct ceph_osd_data *
Alex Eldera4ce40a2013-04-05 01:27:12 -0500147osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
148 unsigned int which)
149{
Alex Elder863c7eb2013-04-15 14:50:36 -0500150 return osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500151}
152EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
153
Alex Elder49719772013-02-11 12:33:24 -0600154void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
155 unsigned int which, struct page **pages,
156 u64 length, u32 alignment,
157 bool pages_from_pool, bool own_pages)
158{
159 struct ceph_osd_data *osd_data;
160
161 osd_data = osd_req_op_raw_data_in(osd_req, which);
162 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
163 pages_from_pool, own_pages);
164}
165EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
166
Alex Eldera4ce40a2013-04-05 01:27:12 -0500167void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500168 unsigned int which, struct page **pages,
169 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500170 bool pages_from_pool, bool own_pages)
171{
172 struct ceph_osd_data *osd_data;
173
Alex Elder863c7eb2013-04-15 14:50:36 -0500174 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500175 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
176 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500177}
178EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
179
180void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500181 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500182{
183 struct ceph_osd_data *osd_data;
184
Alex Elder863c7eb2013-04-15 14:50:36 -0500185 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500186 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500187}
188EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
189
190#ifdef CONFIG_BLOCK
191void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500192 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500193{
194 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500195
196 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500197 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500198}
199EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
200#endif /* CONFIG_BLOCK */
201
202static void osd_req_op_cls_request_info_pagelist(
203 struct ceph_osd_request *osd_req,
204 unsigned int which, struct ceph_pagelist *pagelist)
205{
206 struct ceph_osd_data *osd_data;
207
Alex Elder863c7eb2013-04-15 14:50:36 -0500208 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500209 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500210}
211
Alex Elder04017e22013-04-05 14:46:02 -0500212void osd_req_op_cls_request_data_pagelist(
213 struct ceph_osd_request *osd_req,
214 unsigned int which, struct ceph_pagelist *pagelist)
215{
216 struct ceph_osd_data *osd_data;
217
Alex Elder863c7eb2013-04-15 14:50:36 -0500218 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500219 ceph_osd_data_pagelist_init(osd_data, pagelist);
220}
221EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
222
Alex Elder6c57b552013-04-19 15:34:49 -0500223void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
224 unsigned int which, struct page **pages, u64 length,
225 u32 alignment, bool pages_from_pool, bool own_pages)
226{
227 struct ceph_osd_data *osd_data;
228
229 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
230 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
231 pages_from_pool, own_pages);
232}
233EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
234
Alex Eldera4ce40a2013-04-05 01:27:12 -0500235void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
236 unsigned int which, struct page **pages, u64 length,
237 u32 alignment, bool pages_from_pool, bool own_pages)
238{
239 struct ceph_osd_data *osd_data;
240
Alex Elder863c7eb2013-04-15 14:50:36 -0500241 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500242 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
243 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500244}
245EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
246
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500247static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
248{
249 switch (osd_data->type) {
250 case CEPH_OSD_DATA_TYPE_NONE:
251 return 0;
252 case CEPH_OSD_DATA_TYPE_PAGES:
253 return osd_data->length;
254 case CEPH_OSD_DATA_TYPE_PAGELIST:
255 return (u64)osd_data->pagelist->length;
256#ifdef CONFIG_BLOCK
257 case CEPH_OSD_DATA_TYPE_BIO:
258 return (u64)osd_data->bio_length;
259#endif /* CONFIG_BLOCK */
260 default:
261 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
262 return 0;
263 }
264}
265
Alex Elderc54d47b2013-04-03 01:28:57 -0500266static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
267{
Alex Elder54764922013-04-05 01:27:12 -0500268 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500269 int num_pages;
270
271 num_pages = calc_pages_for((u64)osd_data->alignment,
272 (u64)osd_data->length);
273 ceph_release_page_vector(osd_data->pages, num_pages);
274 }
Alex Elder54764922013-04-05 01:27:12 -0500275 ceph_osd_data_init(osd_data);
276}
277
278static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
279 unsigned int which)
280{
281 struct ceph_osd_req_op *op;
282
283 BUG_ON(which >= osd_req->r_num_ops);
284 op = &osd_req->r_ops[which];
285
286 switch (op->op) {
287 case CEPH_OSD_OP_READ:
288 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200289 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500290 ceph_osd_data_release(&op->extent.osd_data);
291 break;
292 case CEPH_OSD_OP_CALL:
293 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500294 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500295 ceph_osd_data_release(&op->cls.response_data);
296 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800297 case CEPH_OSD_OP_SETXATTR:
298 case CEPH_OSD_OP_CMPXATTR:
299 ceph_osd_data_release(&op->xattr.osd_data);
300 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800301 case CEPH_OSD_OP_STAT:
302 ceph_osd_data_release(&op->raw_data_in);
303 break;
Alex Elder54764922013-04-05 01:27:12 -0500304 default:
305 break;
306 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500307}
308
Sage Weilf24e9982009-10-06 11:31:10 -0700309/*
310 * requests
311 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400312static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700313{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400314 struct ceph_osd_request *req = container_of(kref,
315 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500316 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800317
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400318 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
319 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400320 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
321 WARN_ON(!list_empty(&req->r_req_lru_item));
322 WARN_ON(!list_empty(&req->r_osd_item));
323 WARN_ON(!list_empty(&req->r_linger_item));
324 WARN_ON(!list_empty(&req->r_linger_osd_item));
325 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400326
Sage Weil415e49a2009-12-07 13:37:03 -0800327 if (req->r_request)
328 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500329 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500330 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500331 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500332 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600333
Alex Elder54764922013-04-05 01:27:12 -0500334 for (which = 0; which < req->r_num_ops; which++)
335 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600336
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200337 ceph_oid_destroy(&req->r_base_oid);
338 ceph_oid_destroy(&req->r_target_oid);
Sage Weil415e49a2009-12-07 13:37:03 -0800339 ceph_put_snap_context(req->r_snapc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200340
Sage Weil415e49a2009-12-07 13:37:03 -0800341 if (req->r_mempool)
342 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100343 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500344 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100345 else
346 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700347}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400348
349void ceph_osdc_get_request(struct ceph_osd_request *req)
350{
351 dout("%s %p (was %d)\n", __func__, req,
352 atomic_read(&req->r_kref.refcount));
353 kref_get(&req->r_kref);
354}
355EXPORT_SYMBOL(ceph_osdc_get_request);
356
357void ceph_osdc_put_request(struct ceph_osd_request *req)
358{
Ilya Dryomov3ed97d62016-04-26 15:05:29 +0200359 if (req) {
360 dout("%s %p (was %d)\n", __func__, req,
361 atomic_read(&req->r_kref.refcount));
362 kref_put(&req->r_kref, ceph_osdc_release_request);
363 }
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400364}
365EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700366
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700367struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700368 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800369 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700370 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600371 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700372{
373 struct ceph_osd_request *req;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700375 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100376 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700377 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100378 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
379 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700380 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100381 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
382 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
383 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700384 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100385 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700386 return NULL;
387
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100388 /* req only, each op is zeroed in _osd_req_op_init() */
389 memset(req, 0, sizeof(*req));
390
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700391 req->r_osdc = osdc;
392 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500393 req->r_num_ops = num_ops;
Ilya Dryomov84127282016-04-26 15:39:47 +0200394 req->r_snapid = CEPH_NOSNAP;
395 req->r_snapc = ceph_get_snap_context(snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700396
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700397 kref_init(&req->r_kref);
398 init_completion(&req->r_completion);
399 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600400 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700401 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700402 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400403 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700404 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700405 INIT_LIST_HEAD(&req->r_osd_item);
406
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200407 ceph_oid_init(&req->r_base_oid);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200408 req->r_base_oloc.pool = -1;
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200409 ceph_oid_init(&req->r_target_oid);
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200410 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200411
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200412 dout("%s req %p\n", __func__, req);
413 return req;
414}
415EXPORT_SYMBOL(ceph_osdc_alloc_request);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100416
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200417int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
418{
419 struct ceph_osd_client *osdc = req->r_osdc;
420 struct ceph_msg *msg;
421 int msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700422
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200423 WARN_ON(ceph_oid_empty(&req->r_base_oid));
424
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200425 /* create request message */
Ilya Dryomovae458f52016-02-11 13:09:15 +0100426 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
427 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
428 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
429 msg_size += 1 + 8 + 4 + 4; /* pgid */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200430 msg_size += 4 + req->r_base_oid.name_len; /* oid */
431 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100432 msg_size += 8; /* snapid */
433 msg_size += 8; /* snap_seq */
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200434 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
Ilya Dryomovae458f52016-02-11 13:09:15 +0100435 msg_size += 4; /* retry_attempt */
436
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200437 if (req->r_mempool)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700438 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
439 else
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200440 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
441 if (!msg)
442 return -ENOMEM;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700443
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700444 memset(msg->front.iov_base, 0, msg->front.iov_len);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700445 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700446
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200447 /* create reply message */
448 msg_size = OSD_OPREPLY_FRONT_LEN;
Ilya Dryomov711da552016-04-27 18:32:56 +0200449 msg_size += req->r_base_oid.name_len;
450 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200451
452 if (req->r_mempool)
453 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
454 else
455 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
456 if (!msg)
457 return -ENOMEM;
458
459 req->r_reply = msg;
460
461 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700462}
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200463EXPORT_SYMBOL(ceph_osdc_alloc_messages);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700464
Alex Eldera8dd0a32013-03-13 20:50:00 -0500465static bool osd_req_opcode_valid(u16 opcode)
466{
467 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400468#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
469__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
470#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500471 default:
472 return false;
473 }
474}
475
Alex Elder33803f32013-03-13 20:50:00 -0500476/*
477 * This is an osd op init function for opcodes that have no data or
478 * other information associated with them. It also serves as a
479 * common init routine for all the other init functions, below.
480 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500481static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600482_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800483 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500484{
Alex Elderc99d2d42013-04-05 01:27:11 -0500485 struct ceph_osd_req_op *op;
486
487 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500488 BUG_ON(!osd_req_opcode_valid(opcode));
489
Alex Elderc99d2d42013-04-05 01:27:11 -0500490 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500491 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500492 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800493 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500494
495 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500496}
497
Alex Elder49719772013-02-11 12:33:24 -0600498void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800499 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600500{
Yan, Zheng144cba12015-04-27 11:09:54 +0800501 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600502}
503EXPORT_SYMBOL(osd_req_op_init);
504
Alex Elderc99d2d42013-04-05 01:27:11 -0500505void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
506 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500507 u64 offset, u64 length,
508 u64 truncate_size, u32 truncate_seq)
509{
Yan, Zheng144cba12015-04-27 11:09:54 +0800510 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
511 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500512 size_t payload_len = 0;
513
Li Wangad7a60d2013-08-15 11:51:44 +0800514 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200515 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
516 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500517
Alex Elder33803f32013-03-13 20:50:00 -0500518 op->extent.offset = offset;
519 op->extent.length = length;
520 op->extent.truncate_size = truncate_size;
521 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200522 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500523 payload_len += length;
524
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100525 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500526}
527EXPORT_SYMBOL(osd_req_op_extent_init);
528
Alex Elderc99d2d42013-04-05 01:27:11 -0500529void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
530 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500531{
Alex Elderc99d2d42013-04-05 01:27:11 -0500532 struct ceph_osd_req_op *op;
533 u64 previous;
534
535 BUG_ON(which >= osd_req->r_num_ops);
536 op = &osd_req->r_ops[which];
537 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500538
539 if (length == previous)
540 return; /* Nothing to do */
541 BUG_ON(length > previous);
542
543 op->extent.length = length;
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100544 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500545}
546EXPORT_SYMBOL(osd_req_op_extent_update);
547
Yan, Zheng2c63f492016-01-07 17:32:54 +0800548void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
549 unsigned int which, u64 offset_inc)
550{
551 struct ceph_osd_req_op *op, *prev_op;
552
553 BUG_ON(which + 1 >= osd_req->r_num_ops);
554
555 prev_op = &osd_req->r_ops[which];
556 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
557 /* dup previous one */
558 op->indata_len = prev_op->indata_len;
559 op->outdata_len = prev_op->outdata_len;
560 op->extent = prev_op->extent;
561 /* adjust offset */
562 op->extent.offset += offset_inc;
563 op->extent.length -= offset_inc;
564
565 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
566 op->indata_len -= offset_inc;
567}
568EXPORT_SYMBOL(osd_req_op_extent_dup_last);
569
Alex Elderc99d2d42013-04-05 01:27:11 -0500570void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500571 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500572{
Yan, Zheng144cba12015-04-27 11:09:54 +0800573 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
574 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500575 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500576 size_t payload_len = 0;
577 size_t size;
578
579 BUG_ON(opcode != CEPH_OSD_OP_CALL);
580
Alex Elder5f562df2013-04-05 01:27:12 -0500581 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
582 BUG_ON(!pagelist);
583 ceph_pagelist_init(pagelist);
584
Alex Elder33803f32013-03-13 20:50:00 -0500585 op->cls.class_name = class;
586 size = strlen(class);
587 BUG_ON(size > (size_t) U8_MAX);
588 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500589 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500590 payload_len += size;
591
592 op->cls.method_name = method;
593 size = strlen(method);
594 BUG_ON(size > (size_t) U8_MAX);
595 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500596 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500597 payload_len += size;
598
Alex Eldera4ce40a2013-04-05 01:27:12 -0500599 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500600
Alex Elder33803f32013-03-13 20:50:00 -0500601 op->cls.argc = 0; /* currently unused */
602
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100603 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500604}
605EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500606
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800607int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
608 u16 opcode, const char *name, const void *value,
609 size_t size, u8 cmp_op, u8 cmp_mode)
610{
Yan, Zheng144cba12015-04-27 11:09:54 +0800611 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
612 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800613 struct ceph_pagelist *pagelist;
614 size_t payload_len;
615
616 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
617
618 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
619 if (!pagelist)
620 return -ENOMEM;
621
622 ceph_pagelist_init(pagelist);
623
624 payload_len = strlen(name);
625 op->xattr.name_len = payload_len;
626 ceph_pagelist_append(pagelist, name, payload_len);
627
628 op->xattr.value_len = size;
629 ceph_pagelist_append(pagelist, value, size);
630 payload_len += size;
631
632 op->xattr.cmp_op = cmp_op;
633 op->xattr.cmp_mode = cmp_mode;
634
635 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100636 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800637 return 0;
638}
639EXPORT_SYMBOL(osd_req_op_xattr_init);
640
Alex Elderc99d2d42013-04-05 01:27:11 -0500641void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
642 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500643 u64 cookie, u64 version, int flag)
644{
Yan, Zheng144cba12015-04-27 11:09:54 +0800645 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
646 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500647
Alex Elderc99d2d42013-04-05 01:27:11 -0500648 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500649
650 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500651 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500652 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500653 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500654}
655EXPORT_SYMBOL(osd_req_op_watch_init);
656
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200657void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
658 unsigned int which,
659 u64 expected_object_size,
660 u64 expected_write_size)
661{
662 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800663 CEPH_OSD_OP_SETALLOCHINT,
664 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200665
666 op->alloc_hint.expected_object_size = expected_object_size;
667 op->alloc_hint.expected_write_size = expected_write_size;
668
669 /*
670 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
671 * not worth a feature bit. Set FAILOK per-op flag to make
672 * sure older osds don't trip over an unsupported opcode.
673 */
674 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
675}
676EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
677
Alex Elder90af3602013-04-05 14:46:01 -0500678static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500679 struct ceph_osd_data *osd_data)
680{
681 u64 length = ceph_osd_data_length(osd_data);
682
683 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
684 BUG_ON(length > (u64) SIZE_MAX);
685 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500686 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500687 length, osd_data->alignment);
688 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
689 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500690 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500691#ifdef CONFIG_BLOCK
692 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500693 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500694#endif
695 } else {
696 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
697 }
698}
699
Alex Elder175face2013-03-08 13:35:36 -0600700static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500701 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700702{
Alex Elder79528732013-04-03 21:32:51 -0500703 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500704 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500705 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500706 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600707
Alex Elder79528732013-04-03 21:32:51 -0500708 BUG_ON(which >= req->r_num_ops);
709 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500710 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
711 pr_err("unrecognized osd opcode %d\n", src->op);
712
713 return 0;
714 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700715
Alex Elder065a68f2012-04-20 15:49:43 -0500716 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600717 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600718 osd_data = &src->raw_data_in;
719 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600720 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700721 case CEPH_OSD_OP_READ:
722 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200723 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800724 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800725 case CEPH_OSD_OP_TRUNCATE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200726 if (src->op == CEPH_OSD_OP_WRITE ||
727 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder54d50642013-04-03 01:28:58 -0500728 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600729 dst->extent.offset = cpu_to_le64(src->extent.offset);
730 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700731 dst->extent.truncate_size =
732 cpu_to_le64(src->extent.truncate_size);
733 dst->extent.truncate_seq =
734 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500735 osd_data = &src->extent.osd_data;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200736 if (src->op == CEPH_OSD_OP_WRITE ||
737 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder04017e22013-04-05 14:46:02 -0500738 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500739 else
Alex Elder04017e22013-04-05 14:46:02 -0500740 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700741 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700742 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700743 dst->cls.class_len = src->cls.class_len;
744 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500745 osd_data = &src->cls.request_info;
746 ceph_osdc_msg_data_add(req->r_request, osd_data);
747 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
748 request_data_len = osd_data->pagelist->length;
749
750 osd_data = &src->cls.request_data;
751 data_length = ceph_osd_data_length(osd_data);
752 if (data_length) {
753 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
754 dst->cls.indata_len = cpu_to_le32(data_length);
755 ceph_osdc_msg_data_add(req->r_request, osd_data);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100756 src->indata_len += data_length;
Alex Elder04017e22013-04-05 14:46:02 -0500757 request_data_len += data_length;
758 }
759 osd_data = &src->cls.response_data;
760 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700761 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700762 case CEPH_OSD_OP_STARTSYNC:
763 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700764 case CEPH_OSD_OP_NOTIFY_ACK:
765 case CEPH_OSD_OP_WATCH:
766 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
767 dst->watch.ver = cpu_to_le64(src->watch.ver);
768 dst->watch.flag = src->watch.flag;
769 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200770 case CEPH_OSD_OP_SETALLOCHINT:
771 dst->alloc_hint.expected_object_size =
772 cpu_to_le64(src->alloc_hint.expected_object_size);
773 dst->alloc_hint.expected_write_size =
774 cpu_to_le64(src->alloc_hint.expected_write_size);
775 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800776 case CEPH_OSD_OP_SETXATTR:
777 case CEPH_OSD_OP_CMPXATTR:
778 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
779 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
780 dst->xattr.cmp_op = src->xattr.cmp_op;
781 dst->xattr.cmp_mode = src->xattr.cmp_mode;
782 osd_data = &src->xattr.osd_data;
783 ceph_osdc_msg_data_add(req->r_request, osd_data);
784 request_data_len = osd_data->pagelist->length;
785 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800786 case CEPH_OSD_OP_CREATE:
787 case CEPH_OSD_OP_DELETE:
788 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700789 default:
Alex Elder4c464592013-02-15 11:42:30 -0600790 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600791 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600792 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500793
794 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700795 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200796
Alex Eldera8dd0a32013-03-13 20:50:00 -0500797 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200798 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100799 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600800
Alex Elder54d50642013-04-03 01:28:58 -0500801 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700802}
803
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700804/*
Sage Weilf24e9982009-10-06 11:31:10 -0700805 * build new request AND message, calculate layout, and adjust file
806 * extent as needed.
807 *
808 * if the file was recently truncated, we include information about its
809 * old and new size so that the object can be updated appropriately. (we
810 * avoid synchronously deleting truncated objects because it's slow.)
811 *
812 * if @do_sync, include a 'startsync' command so that the osd will flush
813 * data quickly.
814 */
815struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
816 struct ceph_file_layout *layout,
817 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800818 u64 off, u64 *plen,
819 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700820 int opcode, int flags,
821 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700822 u32 truncate_seq,
823 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600824 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700825{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700826 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500827 u64 objnum = 0;
828 u64 objoff = 0;
829 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700830 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700831
Li Wangad7a60d2013-08-15 11:51:44 +0800832 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800833 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
834 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700835
Alex Elderacead002013-03-14 14:09:05 -0500836 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600837 GFP_NOFS);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200838 if (!req) {
839 r = -ENOMEM;
840 goto fail;
841 }
Alex Elder79528732013-04-03 21:32:51 -0500842
Alex Elderd178a9e2012-11-13 21:11:15 -0600843 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700844
845 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500846 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200847 if (r)
848 goto fail;
Alex Eldera19dadf2013-03-13 20:50:01 -0500849
Yan, Zheng864e9192014-11-13 10:47:25 +0800850 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800851 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800852 } else {
853 u32 object_size = le32_to_cpu(layout->fl_object_size);
854 u32 object_base = off - objoff;
855 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
856 if (truncate_size <= object_base) {
857 truncate_size = 0;
858 } else {
859 truncate_size -= object_base;
860 if (truncate_size > object_size)
861 truncate_size = object_size;
862 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800863 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800864 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800865 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500866 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500867
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200868 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Ilya Dryomovd30291b2016-04-29 19:54:20 +0200869 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600870
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200871 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
872 if (r)
873 goto fail;
874
Sage Weilf24e9982009-10-06 11:31:10 -0700875 return req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +0200876
877fail:
878 ceph_osdc_put_request(req);
879 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700880}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700881EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700882
883/*
884 * We keep osd requests in an rbtree, sorted by ->r_tid.
885 */
886static void __insert_request(struct ceph_osd_client *osdc,
887 struct ceph_osd_request *new)
888{
889 struct rb_node **p = &osdc->requests.rb_node;
890 struct rb_node *parent = NULL;
891 struct ceph_osd_request *req = NULL;
892
893 while (*p) {
894 parent = *p;
895 req = rb_entry(parent, struct ceph_osd_request, r_node);
896 if (new->r_tid < req->r_tid)
897 p = &(*p)->rb_left;
898 else if (new->r_tid > req->r_tid)
899 p = &(*p)->rb_right;
900 else
901 BUG();
902 }
903
904 rb_link_node(&new->r_node, parent, p);
905 rb_insert_color(&new->r_node, &osdc->requests);
906}
907
908static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
909 u64 tid)
910{
911 struct ceph_osd_request *req;
912 struct rb_node *n = osdc->requests.rb_node;
913
914 while (n) {
915 req = rb_entry(n, struct ceph_osd_request, r_node);
916 if (tid < req->r_tid)
917 n = n->rb_left;
918 else if (tid > req->r_tid)
919 n = n->rb_right;
920 else
921 return req;
922 }
923 return NULL;
924}
925
926static struct ceph_osd_request *
927__lookup_request_ge(struct ceph_osd_client *osdc,
928 u64 tid)
929{
930 struct ceph_osd_request *req;
931 struct rb_node *n = osdc->requests.rb_node;
932
933 while (n) {
934 req = rb_entry(n, struct ceph_osd_request, r_node);
935 if (tid < req->r_tid) {
936 if (!n->rb_left)
937 return req;
938 n = n->rb_left;
939 } else if (tid > req->r_tid) {
940 n = n->rb_right;
941 } else {
942 return req;
943 }
944 }
945 return NULL;
946}
947
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400948static void __kick_linger_request(struct ceph_osd_request *req)
949{
950 struct ceph_osd_client *osdc = req->r_osdc;
951 struct ceph_osd *osd = req->r_osd;
952
953 /*
954 * Linger requests need to be resent with a new tid to avoid
955 * the dup op detection logic on the OSDs. Achieve this with
956 * a re-register dance instead of open-coding.
957 */
958 ceph_osdc_get_request(req);
959 if (!list_empty(&req->r_linger_item))
960 __unregister_linger_request(osdc, req);
961 else
962 __unregister_request(osdc, req);
963 __register_request(osdc, req);
964 ceph_osdc_put_request(req);
965
966 /*
967 * Unless request has been registered as both normal and
968 * lingering, __unregister{,_linger}_request clears r_osd.
969 * However, here we need to preserve r_osd to make sure we
970 * requeue on the same OSD.
971 */
972 WARN_ON(req->r_osd || !osd);
973 req->r_osd = osd;
974
975 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
976 __enqueue_request(req);
977}
978
Sage Weil6f6c7002011-01-17 20:34:08 -0800979/*
980 * Resubmit requests pending on the given osd.
981 */
982static void __kick_osd_requests(struct ceph_osd_client *osdc,
983 struct ceph_osd *osd)
984{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700985 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500986 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400987 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800988 int err;
989
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400990 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800991 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600992 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800993 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400994
Alex Eldere02493c2013-03-25 18:16:11 -0500995 /*
996 * Build up a list of requests to resend by traversing the
997 * osd's list of requests. Requests for a given object are
998 * sent in tid order, and that is also the order they're
999 * kept on this list. Therefore all requests that are in
1000 * flight will be found first, followed by all requests that
1001 * have not yet been sent. And to resend requests while
1002 * preserving this order we will want to put any sent
1003 * requests back on the front of the osd client's unsent
1004 * list.
1005 *
1006 * So we build a separate ordered list of already-sent
1007 * requests for the affected osd and splice it onto the
1008 * front of the osd client's unsent list. Once we've seen a
1009 * request that has not yet been sent we're done. Those
1010 * requests are already sitting right where they belong.
1011 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001012 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -05001013 if (!req->r_sent)
1014 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001015
1016 if (!req->r_linger) {
1017 dout("%s requeueing %p tid %llu\n", __func__, req,
1018 req->r_tid);
1019 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001020 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001021 } else {
1022 list_move_tail(&req->r_req_lru_item, &resend_linger);
1023 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001024 }
Alex Eldere02493c2013-03-25 18:16:11 -05001025 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001026
Alex Eldere02493c2013-03-25 18:16:11 -05001027 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001028 * Both registered and not yet registered linger requests are
1029 * enqueued with a new tid on the same OSD. We add/move them
1030 * to req_unsent/o_requests at the end to keep things in tid
1031 * order.
Alex Eldere02493c2013-03-25 18:16:11 -05001032 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001033 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001034 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001035 WARN_ON(!list_empty(&req->r_req_lru_item));
1036 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001037 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001038
1039 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
1040 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001041}
1042
Sage Weilf24e9982009-10-06 11:31:10 -07001043/*
Sage Weil81b024e2009-10-09 10:29:18 -07001044 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -07001045 */
1046static void osd_reset(struct ceph_connection *con)
1047{
1048 struct ceph_osd *osd = con->private;
1049 struct ceph_osd_client *osdc;
1050
1051 if (!osd)
1052 return;
1053 dout("osd_reset osd%d\n", osd->o_osd);
1054 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001055 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001056 mutex_lock(&osdc->request_mutex);
1057 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001058 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001059 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001060 up_read(&osdc->map_sem);
1061}
1062
1063/*
1064 * Track open sessions with osds.
1065 */
Alex Eldere10006f2012-05-26 23:26:43 -05001066static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001067{
1068 struct ceph_osd *osd;
1069
1070 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1071 if (!osd)
1072 return NULL;
1073
1074 atomic_set(&osd->o_ref, 1);
1075 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001076 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001077 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001078 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001079 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001080 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001081 osd->o_incarnation = 1;
1082
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001083 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001084
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001085 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001086 return osd;
1087}
1088
1089static struct ceph_osd *get_osd(struct ceph_osd *osd)
1090{
1091 if (atomic_inc_not_zero(&osd->o_ref)) {
1092 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1093 atomic_read(&osd->o_ref));
1094 return osd;
1095 } else {
1096 dout("get_osd %p FAIL\n", osd);
1097 return NULL;
1098 }
1099}
1100
1101static void put_osd(struct ceph_osd *osd)
1102{
1103 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1104 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001105 if (atomic_dec_and_test(&osd->o_ref)) {
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001106 if (osd->o_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02001107 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001108 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001109 }
Sage Weilf24e9982009-10-06 11:31:10 -07001110}
1111
1112/*
1113 * remove an osd from our map
1114 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001115static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001116{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001117 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001118 WARN_ON(!list_empty(&osd->o_requests));
1119 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001120
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001121 list_del_init(&osd->o_osd_lru);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001122 rb_erase(&osd->o_node, &osdc->osds);
1123 RB_CLEAR_NODE(&osd->o_node);
1124}
1125
1126static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1127{
1128 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1129
1130 if (!RB_EMPTY_NODE(&osd->o_node)) {
1131 ceph_con_close(&osd->o_con);
1132 __remove_osd(osdc, osd);
1133 put_osd(osd);
1134 }
Sage Weilf24e9982009-10-06 11:31:10 -07001135}
1136
Sage Weilaca420b2011-08-31 14:45:53 -07001137static void remove_all_osds(struct ceph_osd_client *osdc)
1138{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001139 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001140 mutex_lock(&osdc->request_mutex);
1141 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1142 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1143 struct ceph_osd, o_node);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001144 remove_osd(osdc, osd);
Sage Weilaca420b2011-08-31 14:45:53 -07001145 }
1146 mutex_unlock(&osdc->request_mutex);
1147}
1148
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001149static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1150 struct ceph_osd *osd)
1151{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001152 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001153 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001154
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001155 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001156 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001157}
1158
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001159static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1160 struct ceph_osd *osd)
1161{
1162 dout("%s %p\n", __func__, osd);
1163
1164 if (list_empty(&osd->o_requests) &&
1165 list_empty(&osd->o_linger_requests))
1166 __move_osd_to_lru(osdc, osd);
1167}
1168
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001169static void __remove_osd_from_lru(struct ceph_osd *osd)
1170{
1171 dout("__remove_osd_from_lru %p\n", osd);
1172 if (!list_empty(&osd->o_osd_lru))
1173 list_del_init(&osd->o_osd_lru);
1174}
1175
Sage Weilaca420b2011-08-31 14:45:53 -07001176static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001177{
1178 struct ceph_osd *osd, *nosd;
1179
1180 dout("__remove_old_osds %p\n", osdc);
1181 mutex_lock(&osdc->request_mutex);
1182 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001183 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001184 break;
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001185 remove_osd(osdc, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001186 }
1187 mutex_unlock(&osdc->request_mutex);
1188}
1189
Sage Weilf24e9982009-10-06 11:31:10 -07001190/*
1191 * reset osd connect
1192 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001193static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001194{
Alex Elderc3acb182012-12-07 09:57:58 -06001195 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001196
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001197 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001198 if (list_empty(&osd->o_requests) &&
1199 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001200 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001201 return -ENODEV;
1202 }
1203
1204 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1205 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1206 !ceph_con_opened(&osd->o_con)) {
1207 struct ceph_osd_request *req;
1208
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001209 dout("osd addr hasn't changed and connection never opened, "
1210 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001211 /* touch each r_stamp for handle_timeout()'s benfit */
1212 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1213 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001214
1215 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001216 }
Alex Elderc3acb182012-12-07 09:57:58 -06001217
1218 ceph_con_close(&osd->o_con);
1219 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1220 osd->o_incarnation++;
1221
1222 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001223}
1224
1225static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1226{
1227 struct rb_node **p = &osdc->osds.rb_node;
1228 struct rb_node *parent = NULL;
1229 struct ceph_osd *osd = NULL;
1230
Sage Weilaca420b2011-08-31 14:45:53 -07001231 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001232 while (*p) {
1233 parent = *p;
1234 osd = rb_entry(parent, struct ceph_osd, o_node);
1235 if (new->o_osd < osd->o_osd)
1236 p = &(*p)->rb_left;
1237 else if (new->o_osd > osd->o_osd)
1238 p = &(*p)->rb_right;
1239 else
1240 BUG();
1241 }
1242
1243 rb_link_node(&new->o_node, parent, p);
1244 rb_insert_color(&new->o_node, &osdc->osds);
1245}
1246
1247static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1248{
1249 struct ceph_osd *osd;
1250 struct rb_node *n = osdc->osds.rb_node;
1251
1252 while (n) {
1253 osd = rb_entry(n, struct ceph_osd, o_node);
1254 if (o < osd->o_osd)
1255 n = n->rb_left;
1256 else if (o > osd->o_osd)
1257 n = n->rb_right;
1258 else
1259 return osd;
1260 }
1261 return NULL;
1262}
1263
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001264static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1265{
1266 schedule_delayed_work(&osdc->timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03001267 osdc->client->options->osd_keepalive_timeout);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001268}
1269
1270static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1271{
1272 cancel_delayed_work(&osdc->timeout_work);
1273}
Sage Weilf24e9982009-10-06 11:31:10 -07001274
1275/*
1276 * Register request, assign tid. If this is the first request, set up
1277 * the timeout event.
1278 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001279static void __register_request(struct ceph_osd_client *osdc,
1280 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001281{
Sage Weilf24e9982009-10-06 11:31:10 -07001282 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001283 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001284 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001285 __insert_request(osdc, req);
1286 ceph_osdc_get_request(req);
1287 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001288 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001289 dout(" first request, scheduling timeout\n");
1290 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001291 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001292}
1293
Sage Weilf24e9982009-10-06 11:31:10 -07001294/*
1295 * called under osdc->request_mutex
1296 */
1297static void __unregister_request(struct ceph_osd_client *osdc,
1298 struct ceph_osd_request *req)
1299{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001300 if (RB_EMPTY_NODE(&req->r_node)) {
1301 dout("__unregister_request %p tid %lld not registered\n",
1302 req, req->r_tid);
1303 return;
1304 }
1305
Sage Weilf24e9982009-10-06 11:31:10 -07001306 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1307 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001308 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001309 osdc->num_requests--;
1310
Sage Weil0ba64782009-10-08 16:57:16 -07001311 if (req->r_osd) {
1312 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001313 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001314
1315 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001316 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001317 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001318 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001319 }
Sage Weilf24e9982009-10-06 11:31:10 -07001320
Alex Elder7d5f2482012-11-29 08:37:03 -06001321 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001322 ceph_osdc_put_request(req);
1323
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001324 if (osdc->num_requests == 0) {
1325 dout(" no requests, canceling timeout\n");
1326 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001327 }
1328}
1329
1330/*
1331 * Cancel a previously queued request message
1332 */
1333static void __cancel_request(struct ceph_osd_request *req)
1334{
Sage Weil6bc18872010-09-27 10:18:52 -07001335 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001336 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001337 req->r_sent = 0;
1338 }
1339}
1340
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001341static void __register_linger_request(struct ceph_osd_client *osdc,
1342 struct ceph_osd_request *req)
1343{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001344 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1345 WARN_ON(!req->r_linger);
1346
Alex Elder96e4dac2013-05-22 20:54:25 -05001347 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001348 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001349 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001350 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001351 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001352}
1353
1354static void __unregister_linger_request(struct ceph_osd_client *osdc,
1355 struct ceph_osd_request *req)
1356{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001357 WARN_ON(!req->r_linger);
1358
1359 if (list_empty(&req->r_linger_item)) {
1360 dout("%s %p tid %llu not registered\n", __func__, req,
1361 req->r_tid);
1362 return;
1363 }
1364
1365 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001366 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001367
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001368 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001369 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001370 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001371 if (list_empty(&req->r_osd_item))
1372 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001373 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001374 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001375}
1376
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001377void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1378 struct ceph_osd_request *req)
1379{
1380 if (!req->r_linger) {
1381 dout("set_request_linger %p\n", req);
1382 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001383 }
1384}
1385EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1386
Sage Weilf24e9982009-10-06 11:31:10 -07001387/*
Josh Durgind29adb32013-12-02 19:11:48 -08001388 * Returns whether a request should be blocked from being sent
1389 * based on the current osdmap and osd_client settings.
1390 *
1391 * Caller should hold map_sem for read.
1392 */
1393static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1394 struct ceph_osd_request *req)
1395{
1396 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1397 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1398 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1399 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1400 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1401}
1402
1403/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001404 * Calculate mapping of a request to a PG. Takes tiering into account.
1405 */
1406static int __calc_request_pg(struct ceph_osdmap *osdmap,
1407 struct ceph_osd_request *req,
1408 struct ceph_pg *pg_out)
1409{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001410 bool need_check_tiering;
1411
1412 need_check_tiering = false;
1413 if (req->r_target_oloc.pool == -1) {
1414 req->r_target_oloc = req->r_base_oloc; /* struct */
1415 need_check_tiering = true;
1416 }
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001417 if (ceph_oid_empty(&req->r_target_oid)) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001418 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1419 need_check_tiering = true;
1420 }
1421
1422 if (need_check_tiering &&
1423 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001424 struct ceph_pg_pool_info *pi;
1425
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001426 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001427 if (pi) {
1428 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1429 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001430 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001431 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1432 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001433 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001434 }
1435 /* !pi is caught in ceph_oloc_oid_to_pg() */
1436 }
1437
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001438 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1439 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001440}
1441
Ilya Dryomovf671b582014-09-02 13:40:33 +04001442static void __enqueue_request(struct ceph_osd_request *req)
1443{
1444 struct ceph_osd_client *osdc = req->r_osdc;
1445
1446 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1447 req->r_osd ? req->r_osd->o_osd : -1);
1448
1449 if (req->r_osd) {
1450 __remove_osd_from_lru(req->r_osd);
1451 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1452 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1453 } else {
1454 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1455 }
1456}
1457
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001458/*
Sage Weilf24e9982009-10-06 11:31:10 -07001459 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1460 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001461 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001462 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001463 *
1464 * Return 0 if unchanged, 1 if changed, or negative on error.
1465 *
1466 * Caller should hold map_sem for read and request_mutex.
1467 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001468static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001469 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001470{
Sage Weil5b191d92013-02-23 10:38:16 -08001471 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001472 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001473 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001474 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001475 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001476
Sage Weil6f6c7002011-01-17 20:34:08 -08001477 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001478
1479 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001480 if (err) {
1481 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001482 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001483 }
Sage Weil7740a422010-01-08 15:58:25 -08001484 req->r_pgid = pgid;
1485
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001486 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1487 if (num < 0)
1488 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001489
Josh Durgind29adb32013-12-02 19:11:48 -08001490 was_paused = req->r_paused;
1491 req->r_paused = __req_should_be_paused(osdc, req);
1492 if (was_paused && !req->r_paused)
1493 force_resend = 1;
1494
Sage Weil38d64532011-10-14 13:33:55 -07001495 if ((!force_resend &&
1496 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001497 req->r_sent >= req->r_osd->o_incarnation &&
1498 req->r_num_pg_osds == num &&
1499 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001500 (req->r_osd == NULL && o == -1) ||
1501 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001502 return 0; /* no change */
1503
Sage Weil5b191d92013-02-23 10:38:16 -08001504 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1505 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001506 req->r_osd ? req->r_osd->o_osd : -1);
1507
Sage Weild85b7052010-05-10 10:24:48 -07001508 /* record full pg acting set */
1509 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1510 req->r_num_pg_osds = num;
1511
Sage Weilf24e9982009-10-06 11:31:10 -07001512 if (req->r_osd) {
1513 __cancel_request(req);
1514 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001515 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001516 req->r_osd = NULL;
1517 }
1518
1519 req->r_osd = __lookup_osd(osdc, o);
1520 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001521 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001522 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001523 if (!req->r_osd) {
1524 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001525 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001526 }
Sage Weilf24e9982009-10-06 11:31:10 -07001527
Sage Weil6f6c7002011-01-17 20:34:08 -08001528 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001529 __insert_osd(osdc, req->r_osd);
1530
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001531 ceph_con_open(&req->r_osd->o_con,
1532 CEPH_ENTITY_TYPE_OSD, o,
1533 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001534 }
1535
Ilya Dryomovf671b582014-09-02 13:40:33 +04001536 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001537 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001538
1539out:
Sage Weilf24e9982009-10-06 11:31:10 -07001540 return err;
1541}
1542
1543/*
1544 * caller should hold map_sem (for read) and request_mutex
1545 */
Sage Weil56e925b2012-01-03 12:34:34 -08001546static void __send_request(struct ceph_osd_client *osdc,
1547 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001548{
Sage Weil1b83bef2013-02-25 16:11:12 -08001549 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001550
Sage Weil1b83bef2013-02-25 16:11:12 -08001551 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1552 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1553 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001554
Sage Weil1b83bef2013-02-25 16:11:12 -08001555 /* fill in message content that changes each time we send it */
1556 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1557 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001558 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001559 p = req->r_request_pgid;
1560 ceph_encode_64(&p, req->r_pgid.pool);
1561 ceph_encode_32(&p, req->r_pgid.seed);
1562 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1563 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1564 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001565
Sage Weil3dd72fc2010-03-22 14:42:30 -07001566 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001567 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001568
1569 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001570
Sage Weilf24e9982009-10-06 11:31:10 -07001571 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001572
1573 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001574}
1575
1576/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001577 * Send any requests in the queue (req_unsent).
1578 */
Alex Elderf9d25192013-02-15 11:42:29 -06001579static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001580{
1581 struct ceph_osd_request *req, *tmp;
1582
Alex Elderf9d25192013-02-15 11:42:29 -06001583 dout("__send_queued\n");
1584 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001585 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001586}
1587
1588/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001589 * Caller should hold map_sem for read and request_mutex.
1590 */
1591static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1592 struct ceph_osd_request *req,
1593 bool nofail)
1594{
1595 int rc;
1596
1597 __register_request(osdc, req);
1598 req->r_sent = 0;
1599 req->r_got_reply = 0;
1600 rc = __map_request(osdc, req, 0);
1601 if (rc < 0) {
1602 if (nofail) {
1603 dout("osdc_start_request failed map, "
1604 " will retry %lld\n", req->r_tid);
1605 rc = 0;
1606 } else {
1607 __unregister_request(osdc, req);
1608 }
1609 return rc;
1610 }
1611
1612 if (req->r_osd == NULL) {
1613 dout("send_request %p no up osds in pg\n", req);
1614 ceph_monc_request_next_osdmap(&osdc->client->monc);
1615 } else {
1616 __send_queued(osdc);
1617 }
1618
1619 return 0;
1620}
1621
1622/*
Sage Weilf24e9982009-10-06 11:31:10 -07001623 * Timeout callback, called every N seconds when 1 or more osd
1624 * requests has been active for more than N seconds. When this
1625 * happens, we ping all OSDs with requests who have timed out to
1626 * ensure any communications channel reset is detected. Reset the
1627 * request timeouts another N seconds in the future as we go.
1628 * Reschedule the timeout event another N seconds in future (unless
1629 * there are no open requests).
1630 */
1631static void handle_timeout(struct work_struct *work)
1632{
1633 struct ceph_osd_client *osdc =
1634 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001635 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001636 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001637 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001638 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001639 dout("timeout\n");
1640 down_read(&osdc->map_sem);
1641
1642 ceph_monc_request_next_osdmap(&osdc->client->monc);
1643
1644 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001645
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001646 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001647 * ping osds that are a bit slow. this ensures that if there
1648 * is a break in the TCP connection we will notice, and reopen
1649 * a connection with that osd (from the fault callback).
1650 */
1651 INIT_LIST_HEAD(&slow_osds);
1652 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001653 if (time_before(jiffies,
1654 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001655 break;
1656
1657 osd = req->r_osd;
1658 BUG_ON(!osd);
1659 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001660 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001661 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1662 }
1663 while (!list_empty(&slow_osds)) {
1664 osd = list_entry(slow_osds.next, struct ceph_osd,
1665 o_keepalive_item);
1666 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001667 ceph_con_keepalive(&osd->o_con);
1668 }
1669
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001670 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001671 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001672 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001673 up_read(&osdc->map_sem);
1674}
1675
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001676static void handle_osds_timeout(struct work_struct *work)
1677{
1678 struct ceph_osd_client *osdc =
1679 container_of(work, struct ceph_osd_client,
1680 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001681 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001682
1683 dout("osds timeout\n");
1684 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001685 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001686 up_read(&osdc->map_sem);
1687
1688 schedule_delayed_work(&osdc->osds_timeout_work,
1689 round_jiffies_relative(delay));
1690}
1691
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001692static int ceph_oloc_decode(void **p, void *end,
1693 struct ceph_object_locator *oloc)
1694{
1695 u8 struct_v, struct_cv;
1696 u32 len;
1697 void *struct_end;
1698 int ret = 0;
1699
1700 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1701 struct_v = ceph_decode_8(p);
1702 struct_cv = ceph_decode_8(p);
1703 if (struct_v < 3) {
1704 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1705 struct_v, struct_cv);
1706 goto e_inval;
1707 }
1708 if (struct_cv > 6) {
1709 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1710 struct_v, struct_cv);
1711 goto e_inval;
1712 }
1713 len = ceph_decode_32(p);
1714 ceph_decode_need(p, end, len, e_inval);
1715 struct_end = *p + len;
1716
1717 oloc->pool = ceph_decode_64(p);
1718 *p += 4; /* skip preferred */
1719
1720 len = ceph_decode_32(p);
1721 if (len > 0) {
1722 pr_warn("ceph_object_locator::key is set\n");
1723 goto e_inval;
1724 }
1725
1726 if (struct_v >= 5) {
1727 len = ceph_decode_32(p);
1728 if (len > 0) {
1729 pr_warn("ceph_object_locator::nspace is set\n");
1730 goto e_inval;
1731 }
1732 }
1733
1734 if (struct_v >= 6) {
1735 s64 hash = ceph_decode_64(p);
1736 if (hash != -1) {
1737 pr_warn("ceph_object_locator::hash is set\n");
1738 goto e_inval;
1739 }
1740 }
1741
1742 /* skip the rest */
1743 *p = struct_end;
1744out:
1745 return ret;
1746
1747e_inval:
1748 ret = -EINVAL;
1749 goto out;
1750}
1751
1752static int ceph_redirect_decode(void **p, void *end,
1753 struct ceph_request_redirect *redir)
1754{
1755 u8 struct_v, struct_cv;
1756 u32 len;
1757 void *struct_end;
1758 int ret;
1759
1760 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1761 struct_v = ceph_decode_8(p);
1762 struct_cv = ceph_decode_8(p);
1763 if (struct_cv > 1) {
1764 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1765 struct_v, struct_cv);
1766 goto e_inval;
1767 }
1768 len = ceph_decode_32(p);
1769 ceph_decode_need(p, end, len, e_inval);
1770 struct_end = *p + len;
1771
1772 ret = ceph_oloc_decode(p, end, &redir->oloc);
1773 if (ret)
1774 goto out;
1775
1776 len = ceph_decode_32(p);
1777 if (len > 0) {
1778 pr_warn("ceph_request_redirect::object_name is set\n");
1779 goto e_inval;
1780 }
1781
1782 len = ceph_decode_32(p);
1783 *p += len; /* skip osd_instructions */
1784
1785 /* skip the rest */
1786 *p = struct_end;
1787out:
1788 return ret;
1789
1790e_inval:
1791 ret = -EINVAL;
1792 goto out;
1793}
1794
Sage Weil25845472011-06-03 09:37:09 -07001795static void complete_request(struct ceph_osd_request *req)
1796{
Sage Weil25845472011-06-03 09:37:09 -07001797 complete_all(&req->r_safe_completion); /* fsync waiter */
1798}
1799
Sage Weilf24e9982009-10-06 11:31:10 -07001800/*
1801 * handle osd op reply. either call the callback if it is specified,
1802 * or do the completion to wake up the waiting thread.
1803 */
Shraddha Barke70cf0522015-10-18 13:55:38 +05301804static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001805{
Sage Weil1b83bef2013-02-25 16:11:12 -08001806 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001807 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001808 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001809 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001810 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001811 unsigned int numops;
1812 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001813 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001814 s32 retry_attempt;
1815 struct ceph_pg pg;
1816 int err;
1817 u32 reassert_epoch;
1818 u64 reassert_version;
1819 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001820 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001821 u32 bytes;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001822 u8 decode_redir;
Alex Elder79528732013-04-03 21:32:51 -05001823 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001824
Sage Weil6df058c2009-12-22 11:24:33 -08001825 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001826 dout("handle_reply %p tid %llu\n", msg, tid);
1827
1828 p = msg->front.iov_base;
1829 end = p + msg->front.iov_len;
1830
1831 ceph_decode_need(&p, end, 4, bad);
1832 object_len = ceph_decode_32(&p);
1833 ceph_decode_need(&p, end, object_len, bad);
1834 p += object_len;
1835
Alex Elderef4859d2013-04-01 18:58:26 -05001836 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001837 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001838 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001839
1840 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1841 flags = ceph_decode_64(&p);
1842 result = ceph_decode_32(&p);
1843 reassert_epoch = ceph_decode_32(&p);
1844 reassert_version = ceph_decode_64(&p);
1845 osdmap_epoch = ceph_decode_32(&p);
1846
Sage Weilf24e9982009-10-06 11:31:10 -07001847 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001848 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001849 mutex_lock(&osdc->request_mutex);
1850 req = __lookup_request(osdc, tid);
1851 if (req == NULL) {
1852 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001853 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001854 }
1855 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001856
1857 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1858 req, result);
1859
Dan Carpenter18741192013-08-15 08:51:58 +03001860 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001861 numops = ceph_decode_32(&p);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01001862 if (numops > CEPH_OSD_MAX_OPS)
Sage Weil1b83bef2013-02-25 16:11:12 -08001863 goto bad_put;
1864 if (numops != req->r_num_ops)
1865 goto bad_put;
1866 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001867 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001868 for (i = 0; i < numops; i++) {
1869 struct ceph_osd_op *op = p;
1870 int len;
1871
1872 len = le32_to_cpu(op->payload_len);
Yan, Zheng7665d852016-01-07 16:48:57 +08001873 req->r_ops[i].outdata_len = len;
Sage Weil1b83bef2013-02-25 16:11:12 -08001874 dout(" op %d has %d bytes\n", i, len);
1875 payload_len += len;
1876 p += sizeof(*op);
1877 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001878 bytes = le32_to_cpu(msg->hdr.data_len);
1879 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001880 pr_warn("sum of op payload lens %d != data_len %d\n",
1881 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001882 goto bad_put;
1883 }
1884
Dan Carpenter18741192013-08-15 08:51:58 +03001885 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001886 retry_attempt = ceph_decode_32(&p);
1887 for (i = 0; i < numops; i++)
Yan, Zheng7665d852016-01-07 16:48:57 +08001888 req->r_ops[i].rval = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001889
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001890 if (le16_to_cpu(msg->hdr.version) >= 6) {
1891 p += 8 + 4; /* skip replay_version */
1892 p += 8; /* skip user_version */
1893
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001894 if (le16_to_cpu(msg->hdr.version) >= 7)
1895 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1896 else
1897 decode_redir = 1;
1898 } else {
1899 decode_redir = 0;
1900 }
1901
1902 if (decode_redir) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001903 err = ceph_redirect_decode(&p, end, &redir);
1904 if (err)
1905 goto bad_put;
1906 } else {
1907 redir.oloc.pool = -1;
1908 }
1909
1910 if (redir.oloc.pool != -1) {
1911 dout("redirect pool %lld\n", redir.oloc.pool);
1912
1913 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001914
1915 req->r_target_oloc = redir.oloc; /* struct */
1916
1917 /*
1918 * Start redirect requests with nofail=true. If
1919 * mapping fails, request will end up on the notarget
1920 * list, waiting for the new osdmap (which can take
1921 * a while), even though the original request mapped
1922 * successfully. In the future we might want to follow
1923 * original request's nofail setting here.
1924 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001925 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001926 BUG_ON(err);
1927
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001928 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001929 }
1930
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001931 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001932 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001933 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001934 dout("handle_reply result %d bytes %d\n", req->r_result,
1935 bytes);
1936 if (req->r_result == 0)
1937 req->r_result = bytes;
1938
1939 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001940 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1941 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001942
1943 req->r_got_reply = 1;
1944 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1945 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001946 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001947 }
1948
1949 dout("handle_reply tid %llu flags %d\n", tid, flags);
1950
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001951 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1952 __register_linger_request(osdc, req);
1953
Sage Weilf24e9982009-10-06 11:31:10 -07001954 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001955 if (result < 0 ||
1956 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001957 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1958 __unregister_request(osdc, req);
1959
1960 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001961 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001962
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001963 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001964 if (req->r_unsafe_callback &&
1965 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1966 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001967 if (req->r_callback)
1968 req->r_callback(req, msg);
1969 else
1970 complete_all(&req->r_completion);
1971 }
Sage Weilf24e9982009-10-06 11:31:10 -07001972
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001973 if (flags & CEPH_OSD_FLAG_ONDISK) {
1974 if (req->r_unsafe_callback && already_completed)
1975 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001976 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001977 }
Sage Weilf24e9982009-10-06 11:31:10 -07001978
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001979out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001980 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001981 ceph_osdc_put_request(req);
1982 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001983out_unlock:
1984 mutex_unlock(&osdc->request_mutex);
1985 up_read(&osdc->map_sem);
1986 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001987
Sage Weil1b83bef2013-02-25 16:11:12 -08001988bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001989 req->r_result = -EIO;
1990 __unregister_request(osdc, req);
1991 if (req->r_callback)
1992 req->r_callback(req, msg);
1993 else
1994 complete_all(&req->r_completion);
1995 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001996 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001997bad_mutex:
1998 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001999 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002000bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08002001 pr_err("corrupt osd_op_reply got %d %d\n",
2002 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08002003 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002004}
2005
Sage Weil6f6c7002011-01-17 20:34:08 -08002006static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07002007{
Sage Weilf24e9982009-10-06 11:31:10 -07002008 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07002009
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03002010 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08002011 for (p = rb_first(&osdc->osds); p; p = n) {
2012 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07002013
Sage Weil6f6c7002011-01-17 20:34:08 -08002014 n = rb_next(p);
2015 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
2016 memcmp(&osd->o_con.peer_addr,
2017 ceph_osd_addr(osdc->osdmap,
2018 osd->o_osd),
2019 sizeof(struct ceph_entity_addr)) != 0)
2020 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07002021 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002022}
2023
2024/*
Sage Weil6f6c7002011-01-17 20:34:08 -08002025 * Requeue requests whose mapping to an OSD has changed. If requests map to
2026 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002027 *
Alex Eldere6d50f62012-12-26 14:31:40 -06002028 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002029 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002030static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
2031 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002032{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002033 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08002034 struct rb_node *p;
2035 int needmap = 0;
2036 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002037 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002038
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002039 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2040 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002041 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07002042 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08002043 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07002044 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06002045
2046 /*
2047 * For linger requests that have not yet been
2048 * registered, move them to the linger list; they'll
2049 * be sent to the osd in the loop below. Unregister
2050 * the request before re-registering it as a linger
2051 * request to ensure the __map_request() below
2052 * will decide it needs to be sent.
2053 */
2054 if (req->r_linger && list_empty(&req->r_linger_item)) {
2055 dout("%p tid %llu restart on osd%d\n",
2056 req, req->r_tid,
2057 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05002058 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002059 __unregister_request(osdc, req);
2060 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002061 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002062 continue;
2063 }
2064
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002065 force_resend_req = force_resend ||
2066 (force_resend_writes &&
2067 req->r_flags & CEPH_OSD_FLAG_WRITE);
2068 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002069 if (err < 0)
2070 continue; /* error */
2071 if (req->r_osd == NULL) {
2072 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2073 needmap++; /* request a newer map */
2074 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002075 if (!req->r_linger) {
2076 dout("%p tid %llu requeued on osd%d\n", req,
2077 req->r_tid,
2078 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002079 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002080 }
2081 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002082 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002083
2084 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2085 r_linger_item) {
2086 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2087
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002088 err = __map_request(osdc, req,
2089 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002090 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002091 if (err < 0)
2092 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002093 if (req->r_osd == NULL || err > 0) {
2094 if (req->r_osd == NULL) {
2095 dout("lingering %p tid %llu maps to no osd\n",
2096 req, req->r_tid);
2097 /*
2098 * A homeless lingering request makes
2099 * no sense, as it's job is to keep
2100 * a particular OSD connection open.
2101 * Request a newer map and kick the
2102 * request, knowing that it won't be
2103 * resent until we actually get a map
2104 * that can tell us where to send it.
2105 */
2106 needmap++;
2107 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002108
Ilya Dryomovb0494532015-05-11 17:53:10 +03002109 dout("kicking lingering %p tid %llu osd%d\n", req,
2110 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2111 __register_request(osdc, req);
2112 __unregister_linger_request(osdc, req);
2113 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002114 }
Alex Elder14d2f382013-05-15 16:28:33 -05002115 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002116 mutex_unlock(&osdc->request_mutex);
2117
2118 if (needmap) {
2119 dout("%d requests for down osds, need new map\n", needmap);
2120 ceph_monc_request_next_osdmap(&osdc->client->monc);
2121 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002122}
Sage Weil6f6c7002011-01-17 20:34:08 -08002123
2124
Sage Weilf24e9982009-10-06 11:31:10 -07002125/*
2126 * Process updated osd map.
2127 *
2128 * The message contains any number of incremental and full maps, normally
2129 * indicating some sort of topology change in the cluster. Kick requests
2130 * off to different OSDs as needed.
2131 */
2132void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2133{
2134 void *p, *end, *next;
2135 u32 nr_maps, maplen;
2136 u32 epoch;
2137 struct ceph_osdmap *newmap = NULL, *oldmap;
2138 int err;
2139 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002140 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002141
2142 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2143 p = msg->front.iov_base;
2144 end = p + msg->front.iov_len;
2145
2146 /* verify fsid */
2147 ceph_decode_need(&p, end, sizeof(fsid), bad);
2148 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002149 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2150 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002151
2152 down_write(&osdc->map_sem);
2153
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002154 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2155
Sage Weilf24e9982009-10-06 11:31:10 -07002156 /* incremental maps */
2157 ceph_decode_32_safe(&p, end, nr_maps, bad);
2158 dout(" %d inc maps\n", nr_maps);
2159 while (nr_maps > 0) {
2160 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002161 epoch = ceph_decode_32(&p);
2162 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002163 ceph_decode_need(&p, end, maplen, bad);
2164 next = p + maplen;
2165 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2166 dout("applying incremental map %u len %d\n",
2167 epoch, maplen);
2168 newmap = osdmap_apply_incremental(&p, next,
2169 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002170 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002171 if (IS_ERR(newmap)) {
2172 err = PTR_ERR(newmap);
2173 goto bad;
2174 }
Sage Weil30dc6382009-12-21 14:49:37 -08002175 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002176 if (newmap != osdc->osdmap) {
2177 ceph_osdmap_destroy(osdc->osdmap);
2178 osdc->osdmap = newmap;
2179 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002180 was_full = was_full ||
2181 ceph_osdmap_flag(osdc->osdmap,
2182 CEPH_OSDMAP_FULL);
2183 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002184 } else {
2185 dout("ignoring incremental map %u len %d\n",
2186 epoch, maplen);
2187 }
2188 p = next;
2189 nr_maps--;
2190 }
2191 if (newmap)
2192 goto done;
2193
2194 /* full maps */
2195 ceph_decode_32_safe(&p, end, nr_maps, bad);
2196 dout(" %d full maps\n", nr_maps);
2197 while (nr_maps) {
2198 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002199 epoch = ceph_decode_32(&p);
2200 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002201 ceph_decode_need(&p, end, maplen, bad);
2202 if (nr_maps > 1) {
2203 dout("skipping non-latest full map %u len %d\n",
2204 epoch, maplen);
2205 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2206 dout("skipping full map %u len %d, "
2207 "older than our %u\n", epoch, maplen,
2208 osdc->osdmap->epoch);
2209 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002210 int skipped_map = 0;
2211
Sage Weilf24e9982009-10-06 11:31:10 -07002212 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002213 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002214 if (IS_ERR(newmap)) {
2215 err = PTR_ERR(newmap);
2216 goto bad;
2217 }
Sage Weil30dc6382009-12-21 14:49:37 -08002218 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002219 oldmap = osdc->osdmap;
2220 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002221 if (oldmap) {
2222 if (oldmap->epoch + 1 < newmap->epoch)
2223 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002224 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002225 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002226 was_full = was_full ||
2227 ceph_osdmap_flag(osdc->osdmap,
2228 CEPH_OSDMAP_FULL);
2229 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002230 }
2231 p += maplen;
2232 nr_maps--;
2233 }
2234
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002235 if (!osdc->osdmap)
2236 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002237done:
2238 downgrade_write(&osdc->map_sem);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01002239 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2240 osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002241
2242 /*
2243 * subscribe to subsequent osdmap updates if full to ensure
2244 * we find out when we are no longer full and stop returning
2245 * ENOSPC.
2246 */
Josh Durgind29adb32013-12-02 19:11:48 -08002247 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2248 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2249 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002250 ceph_monc_request_next_osdmap(&osdc->client->monc);
2251
Alex Elderf9d25192013-02-15 11:42:29 -06002252 mutex_lock(&osdc->request_mutex);
2253 __send_queued(osdc);
2254 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002255 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002256 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002257 return;
2258
2259bad:
2260 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002261 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002262 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002263}
2264
Sage Weilf24e9982009-10-06 11:31:10 -07002265/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002266 * watch/notify callback event infrastructure
2267 *
2268 * These callbacks are used both for watch and notify operations.
2269 */
2270static void __release_event(struct kref *kref)
2271{
2272 struct ceph_osd_event *event =
2273 container_of(kref, struct ceph_osd_event, kref);
2274
2275 dout("__release_event %p\n", event);
2276 kfree(event);
2277}
2278
2279static void get_event(struct ceph_osd_event *event)
2280{
2281 kref_get(&event->kref);
2282}
2283
2284void ceph_osdc_put_event(struct ceph_osd_event *event)
2285{
2286 kref_put(&event->kref, __release_event);
2287}
2288EXPORT_SYMBOL(ceph_osdc_put_event);
2289
2290static void __insert_event(struct ceph_osd_client *osdc,
2291 struct ceph_osd_event *new)
2292{
2293 struct rb_node **p = &osdc->event_tree.rb_node;
2294 struct rb_node *parent = NULL;
2295 struct ceph_osd_event *event = NULL;
2296
2297 while (*p) {
2298 parent = *p;
2299 event = rb_entry(parent, struct ceph_osd_event, node);
2300 if (new->cookie < event->cookie)
2301 p = &(*p)->rb_left;
2302 else if (new->cookie > event->cookie)
2303 p = &(*p)->rb_right;
2304 else
2305 BUG();
2306 }
2307
2308 rb_link_node(&new->node, parent, p);
2309 rb_insert_color(&new->node, &osdc->event_tree);
2310}
2311
2312static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2313 u64 cookie)
2314{
2315 struct rb_node **p = &osdc->event_tree.rb_node;
2316 struct rb_node *parent = NULL;
2317 struct ceph_osd_event *event = NULL;
2318
2319 while (*p) {
2320 parent = *p;
2321 event = rb_entry(parent, struct ceph_osd_event, node);
2322 if (cookie < event->cookie)
2323 p = &(*p)->rb_left;
2324 else if (cookie > event->cookie)
2325 p = &(*p)->rb_right;
2326 else
2327 return event;
2328 }
2329 return NULL;
2330}
2331
2332static void __remove_event(struct ceph_osd_event *event)
2333{
2334 struct ceph_osd_client *osdc = event->osdc;
2335
2336 if (!RB_EMPTY_NODE(&event->node)) {
2337 dout("__remove_event removed %p\n", event);
2338 rb_erase(&event->node, &osdc->event_tree);
2339 ceph_osdc_put_event(event);
2340 } else {
2341 dout("__remove_event didn't remove %p\n", event);
2342 }
2343}
2344
2345int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2346 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002347 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002348{
2349 struct ceph_osd_event *event;
2350
2351 event = kmalloc(sizeof(*event), GFP_NOIO);
2352 if (!event)
2353 return -ENOMEM;
2354
2355 dout("create_event %p\n", event);
2356 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002357 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002358 event->data = data;
2359 event->osdc = osdc;
2360 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002361 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002362 kref_init(&event->kref); /* one ref for us */
2363 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002364
2365 spin_lock(&osdc->event_lock);
2366 event->cookie = ++osdc->event_count;
2367 __insert_event(osdc, event);
2368 spin_unlock(&osdc->event_lock);
2369
2370 *pevent = event;
2371 return 0;
2372}
2373EXPORT_SYMBOL(ceph_osdc_create_event);
2374
2375void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2376{
2377 struct ceph_osd_client *osdc = event->osdc;
2378
2379 dout("cancel_event %p\n", event);
2380 spin_lock(&osdc->event_lock);
2381 __remove_event(event);
2382 spin_unlock(&osdc->event_lock);
2383 ceph_osdc_put_event(event); /* caller's */
2384}
2385EXPORT_SYMBOL(ceph_osdc_cancel_event);
2386
2387
2388static void do_event_work(struct work_struct *work)
2389{
2390 struct ceph_osd_event_work *event_work =
2391 container_of(work, struct ceph_osd_event_work, work);
2392 struct ceph_osd_event *event = event_work->event;
2393 u64 ver = event_work->ver;
2394 u64 notify_id = event_work->notify_id;
2395 u8 opcode = event_work->opcode;
2396
2397 dout("do_event_work completing %p\n", event);
2398 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002399 dout("do_event_work completed %p\n", event);
2400 ceph_osdc_put_event(event);
2401 kfree(event_work);
2402}
2403
2404
2405/*
2406 * Process osd watch notifications
2407 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002408static void handle_watch_notify(struct ceph_osd_client *osdc,
2409 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002410{
2411 void *p, *end;
2412 u8 proto_ver;
2413 u64 cookie, ver, notify_id;
2414 u8 opcode;
2415 struct ceph_osd_event *event;
2416 struct ceph_osd_event_work *event_work;
2417
2418 p = msg->front.iov_base;
2419 end = p + msg->front.iov_len;
2420
2421 ceph_decode_8_safe(&p, end, proto_ver, bad);
2422 ceph_decode_8_safe(&p, end, opcode, bad);
2423 ceph_decode_64_safe(&p, end, cookie, bad);
2424 ceph_decode_64_safe(&p, end, ver, bad);
2425 ceph_decode_64_safe(&p, end, notify_id, bad);
2426
2427 spin_lock(&osdc->event_lock);
2428 event = __find_event(osdc, cookie);
2429 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002430 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002431 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002432 }
2433 spin_unlock(&osdc->event_lock);
2434 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2435 cookie, ver, event);
2436 if (event) {
2437 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002438 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002439 pr_err("couldn't allocate event_work\n");
2440 ceph_osdc_put_event(event);
2441 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002442 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002443 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002444 event_work->event = event;
2445 event_work->ver = ver;
2446 event_work->notify_id = notify_id;
2447 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002448
2449 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002450 }
2451
2452 return;
2453
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002454bad:
2455 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002456}
2457
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002458/*
Alex Eldere65550f2013-04-05 01:27:12 -05002459 * build new request AND message
2460 *
2461 */
2462void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2463 struct ceph_snap_context *snapc, u64 snap_id,
2464 struct timespec *mtime)
2465{
2466 struct ceph_msg *msg = req->r_request;
2467 void *p;
2468 size_t msg_size;
2469 int flags = req->r_flags;
2470 u64 data_len;
2471 unsigned int i;
2472
2473 req->r_snapid = snap_id;
Ilya Dryomov84127282016-04-26 15:39:47 +02002474 WARN_ON(snapc != req->r_snapc);
Alex Eldere65550f2013-04-05 01:27:12 -05002475
2476 /* encode request */
2477 msg->hdr.version = cpu_to_le16(4);
2478
2479 p = msg->front.iov_base;
2480 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2481 req->r_request_osdmap_epoch = p;
2482 p += 4;
2483 req->r_request_flags = p;
2484 p += 4;
2485 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2486 ceph_encode_timespec(p, mtime);
2487 p += sizeof(struct ceph_timespec);
2488 req->r_request_reassert_version = p;
2489 p += sizeof(struct ceph_eversion); /* will get filled in */
2490
2491 /* oloc */
2492 ceph_encode_8(&p, 4);
2493 ceph_encode_8(&p, 4);
2494 ceph_encode_32(&p, 8 + 4 + 4);
2495 req->r_request_pool = p;
2496 p += 8;
2497 ceph_encode_32(&p, -1); /* preferred */
2498 ceph_encode_32(&p, 0); /* key len */
2499
2500 ceph_encode_8(&p, 1);
2501 req->r_request_pgid = p;
2502 p += 8 + 4;
2503 ceph_encode_32(&p, -1); /* preferred */
2504
2505 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002506 ceph_encode_32(&p, req->r_base_oid.name_len);
2507 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002508 dout("oid %*pE len %d\n", req->r_base_oid.name_len,
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002509 req->r_base_oid.name, req->r_base_oid.name_len);
2510 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002511
2512 /* ops--can imply data */
2513 ceph_encode_16(&p, (u16)req->r_num_ops);
2514 data_len = 0;
2515 for (i = 0; i < req->r_num_ops; i++) {
2516 data_len += osd_req_encode_op(req, p, i);
2517 p += sizeof(struct ceph_osd_op);
2518 }
2519
2520 /* snaps */
2521 ceph_encode_64(&p, req->r_snapid);
2522 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2523 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2524 if (req->r_snapc) {
Ilya Dryomov84127282016-04-26 15:39:47 +02002525 for (i = 0; i < req->r_snapc->num_snaps; i++) {
Alex Eldere65550f2013-04-05 01:27:12 -05002526 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2527 }
2528 }
2529
2530 req->r_request_attempts = p;
2531 p += 4;
2532
2533 /* data */
2534 if (flags & CEPH_OSD_FLAG_WRITE) {
2535 u16 data_off;
2536
2537 /*
2538 * The header "data_off" is a hint to the receiver
2539 * allowing it to align received data into its
2540 * buffers such that there's no need to re-copy
2541 * it before writing it to disk (direct I/O).
2542 */
2543 data_off = (u16) (off & 0xffff);
2544 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2545 }
2546 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2547
2548 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2549 msg_size = p - msg->front.iov_base;
2550 msg->front.iov_len = msg_size;
2551 msg->hdr.front_len = cpu_to_le32(msg_size);
2552
2553 dout("build_request msg_size was %d\n", (int)msg_size);
2554}
2555EXPORT_SYMBOL(ceph_osdc_build_request);
2556
2557/*
Sage Weilf24e9982009-10-06 11:31:10 -07002558 * Register request, send initial attempt.
2559 */
2560int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2561 struct ceph_osd_request *req,
2562 bool nofail)
2563{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002564 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002565
Sage Weilf24e9982009-10-06 11:31:10 -07002566 down_read(&osdc->map_sem);
2567 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002568
2569 rc = __ceph_osdc_start_request(osdc, req, nofail);
2570
Sage Weilf24e9982009-10-06 11:31:10 -07002571 mutex_unlock(&osdc->request_mutex);
2572 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002573
Sage Weilf24e9982009-10-06 11:31:10 -07002574 return rc;
2575}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002576EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002577
2578/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002579 * Unregister a registered request. The request is not completed (i.e.
2580 * no callbacks or wakeups) - higher layers are supposed to know what
2581 * they are canceling.
2582 */
2583void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2584{
2585 struct ceph_osd_client *osdc = req->r_osdc;
2586
2587 mutex_lock(&osdc->request_mutex);
2588 if (req->r_linger)
2589 __unregister_linger_request(osdc, req);
2590 __unregister_request(osdc, req);
2591 mutex_unlock(&osdc->request_mutex);
2592
2593 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2594}
2595EXPORT_SYMBOL(ceph_osdc_cancel_request);
2596
2597/*
Sage Weilf24e9982009-10-06 11:31:10 -07002598 * wait for a request to complete
2599 */
2600int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2601 struct ceph_osd_request *req)
2602{
2603 int rc;
2604
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002605 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2606
Sage Weilf24e9982009-10-06 11:31:10 -07002607 rc = wait_for_completion_interruptible(&req->r_completion);
2608 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002609 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2610 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002611 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002612 return rc;
2613 }
2614
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002615 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2616 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002617 return req->r_result;
2618}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002619EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002620
2621/*
2622 * sync - wait for all in-flight requests to flush. avoid starvation.
2623 */
2624void ceph_osdc_sync(struct ceph_osd_client *osdc)
2625{
2626 struct ceph_osd_request *req;
2627 u64 last_tid, next_tid = 0;
2628
2629 mutex_lock(&osdc->request_mutex);
2630 last_tid = osdc->last_tid;
2631 while (1) {
2632 req = __lookup_request_ge(osdc, next_tid);
2633 if (!req)
2634 break;
2635 if (req->r_tid > last_tid)
2636 break;
2637
2638 next_tid = req->r_tid + 1;
2639 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2640 continue;
2641
2642 ceph_osdc_get_request(req);
2643 mutex_unlock(&osdc->request_mutex);
2644 dout("sync waiting on tid %llu (last is %llu)\n",
2645 req->r_tid, last_tid);
2646 wait_for_completion(&req->r_safe_completion);
2647 mutex_lock(&osdc->request_mutex);
2648 ceph_osdc_put_request(req);
2649 }
2650 mutex_unlock(&osdc->request_mutex);
2651 dout("sync done (thru tid %llu)\n", last_tid);
2652}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002653EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002654
2655/*
Josh Durgindd935f42013-08-28 21:43:09 -07002656 * Call all pending notify callbacks - for use after a watch is
2657 * unregistered, to make sure no more callbacks for it will be invoked
2658 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002659void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002660{
2661 flush_workqueue(osdc->notify_wq);
2662}
2663EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2664
2665
2666/*
Sage Weilf24e9982009-10-06 11:31:10 -07002667 * init, shutdown
2668 */
2669int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2670{
2671 int err;
2672
2673 dout("init\n");
2674 osdc->client = client;
2675 osdc->osdmap = NULL;
2676 init_rwsem(&osdc->map_sem);
2677 init_completion(&osdc->map_waiters);
2678 osdc->last_requested_map = 0;
2679 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002680 osdc->last_tid = 0;
2681 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002682 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002683 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002684 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002685 INIT_LIST_HEAD(&osdc->req_unsent);
2686 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002687 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002688 osdc->num_requests = 0;
2689 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002690 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002691 spin_lock_init(&osdc->event_lock);
2692 osdc->event_tree = RB_ROOT;
2693 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002694
2695 schedule_delayed_work(&osdc->osds_timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03002696 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
Sage Weilf24e9982009-10-06 11:31:10 -07002697
Sage Weil5f44f142009-11-18 14:52:18 -08002698 err = -ENOMEM;
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01002699 osdc->req_mempool = mempool_create_slab_pool(10,
2700 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07002701 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002702 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002703
Sage Weild50b4092012-07-09 14:22:34 -07002704 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
Ilya Dryomov711da552016-04-27 18:32:56 +02002705 PAGE_SIZE, 10, true, "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002706 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002707 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002708 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Ilya Dryomov711da552016-04-27 18:32:56 +02002709 PAGE_SIZE, 10, true, "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002710 if (err < 0)
2711 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002712
Dan Carpenterdbcae082013-08-15 08:58:59 +03002713 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002714 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002715 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002716 goto out_msgpool_reply;
2717
Sage Weilf24e9982009-10-06 11:31:10 -07002718 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002719
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002720out_msgpool_reply:
2721 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002722out_msgpool:
2723 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002724out_mempool:
2725 mempool_destroy(osdc->req_mempool);
2726out:
2727 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002728}
2729
2730void ceph_osdc_stop(struct ceph_osd_client *osdc)
2731{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002732 flush_workqueue(osdc->notify_wq);
2733 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002734 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002735 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002736 if (osdc->osdmap) {
2737 ceph_osdmap_destroy(osdc->osdmap);
2738 osdc->osdmap = NULL;
2739 }
Sage Weilaca420b2011-08-31 14:45:53 -07002740 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002741 mempool_destroy(osdc->req_mempool);
2742 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002743 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002744}
2745
2746/*
2747 * Read some contiguous pages. If we cross a stripe boundary, shorten
2748 * *plen. Return number of bytes read, or error.
2749 */
2750int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2751 struct ceph_vino vino, struct ceph_file_layout *layout,
2752 u64 off, u64 *plen,
2753 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002754 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002755{
2756 struct ceph_osd_request *req;
2757 int rc = 0;
2758
2759 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2760 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002761 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002762 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002763 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002764 false);
Sage Weil68162822012-09-24 21:01:02 -07002765 if (IS_ERR(req))
2766 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002767
2768 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002769
Alex Elder406e2c92013-04-15 14:50:36 -05002770 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002771 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002772
Alex Eldere0c59482013-03-07 15:38:25 -06002773 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002774 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002775
Alex Elder79528732013-04-03 21:32:51 -05002776 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002777
Sage Weilf24e9982009-10-06 11:31:10 -07002778 rc = ceph_osdc_start_request(osdc, req, false);
2779 if (!rc)
2780 rc = ceph_osdc_wait_request(osdc, req);
2781
2782 ceph_osdc_put_request(req);
2783 dout("readpages result %d\n", rc);
2784 return rc;
2785}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002786EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002787
2788/*
2789 * do a synchronous write on N pages
2790 */
2791int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2792 struct ceph_file_layout *layout,
2793 struct ceph_snap_context *snapc,
2794 u64 off, u64 len,
2795 u32 truncate_seq, u64 truncate_size,
2796 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002797 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002798{
2799 struct ceph_osd_request *req;
2800 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002801 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002802
Alex Elderacead002013-03-14 14:09:05 -05002803 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002804 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002805 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002806 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002807 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002808 true);
Sage Weil68162822012-09-24 21:01:02 -07002809 if (IS_ERR(req))
2810 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002811
2812 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002813 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002814 false, false);
2815 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002816
Alex Elder79528732013-04-03 21:32:51 -05002817 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002818
Alex Elder87f979d2013-02-15 11:42:29 -06002819 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002820 if (!rc)
2821 rc = ceph_osdc_wait_request(osdc, req);
2822
2823 ceph_osdc_put_request(req);
2824 if (rc == 0)
2825 rc = len;
2826 dout("writepages result %d\n", rc);
2827 return rc;
2828}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002829EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002830
Alex Elder5522ae02013-05-01 12:43:04 -05002831int ceph_osdc_setup(void)
2832{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002833 size_t size = sizeof(struct ceph_osd_request) +
2834 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2835
Alex Elder5522ae02013-05-01 12:43:04 -05002836 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002837 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2838 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05002839
2840 return ceph_osd_request_cache ? 0 : -ENOMEM;
2841}
2842EXPORT_SYMBOL(ceph_osdc_setup);
2843
2844void ceph_osdc_cleanup(void)
2845{
2846 BUG_ON(!ceph_osd_request_cache);
2847 kmem_cache_destroy(ceph_osd_request_cache);
2848 ceph_osd_request_cache = NULL;
2849}
2850EXPORT_SYMBOL(ceph_osdc_cleanup);
2851
Sage Weilf24e9982009-10-06 11:31:10 -07002852/*
2853 * handle incoming message
2854 */
2855static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2856{
2857 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002858 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002859 int type = le16_to_cpu(msg->hdr.type);
2860
2861 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002862 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002863 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002864
2865 switch (type) {
2866 case CEPH_MSG_OSD_MAP:
2867 ceph_osdc_handle_map(osdc, msg);
2868 break;
2869 case CEPH_MSG_OSD_OPREPLY:
Shraddha Barke70cf0522015-10-18 13:55:38 +05302870 handle_reply(osdc, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002871 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002872 case CEPH_MSG_WATCH_NOTIFY:
2873 handle_watch_notify(osdc, msg);
2874 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002875
2876 default:
2877 pr_err("received unknown message type %d %s\n", type,
2878 ceph_msg_type_name(type));
2879 }
Sage Weil4a32f932010-06-13 10:27:53 -07002880out:
Sage Weilf24e9982009-10-06 11:31:10 -07002881 ceph_msg_put(msg);
2882}
2883
Sage Weil5b3a4db2010-02-19 21:43:23 -08002884/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002885 * Lookup and return message for incoming reply. Don't try to do
2886 * anything about a larger than preallocated data portion of the
2887 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002888 */
2889static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002890 struct ceph_msg_header *hdr,
2891 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002892{
2893 struct ceph_osd *osd = con->private;
2894 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002895 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002896 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002897 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002898 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002899 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002900
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002901 tid = le64_to_cpu(hdr->tid);
2902 mutex_lock(&osdc->request_mutex);
2903 req = __lookup_request(osdc, tid);
2904 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01002905 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2906 osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002907 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002908 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002909 goto out;
2910 }
Sage Weilc16e7862010-03-01 13:02:00 -08002911
Alex Elderace6d3a2013-04-01 16:12:14 -05002912 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002913
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002914 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002915 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2916 __func__, osd->o_osd, req->r_tid, front_len,
2917 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002918 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2919 false);
Sage Weila79832f2010-04-01 16:06:19 -07002920 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002921 goto out;
2922 ceph_msg_put(req->r_reply);
2923 req->r_reply = m;
2924 }
Sage Weilc16e7862010-03-01 13:02:00 -08002925
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002926 if (data_len > req->r_reply->data_length) {
2927 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2928 __func__, osd->o_osd, req->r_tid, data_len,
2929 req->r_reply->data_length);
2930 m = NULL;
2931 *skip = 1;
2932 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002933 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002934
2935 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002936 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002937
2938out:
2939 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002940 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002941}
2942
2943static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2944 struct ceph_msg_header *hdr,
2945 int *skip)
2946{
2947 struct ceph_osd *osd = con->private;
2948 int type = le16_to_cpu(hdr->type);
2949 int front = le32_to_cpu(hdr->front_len);
2950
Alex Elder1c20f2d2012-06-04 14:43:32 -05002951 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002952 switch (type) {
2953 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002954 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002955 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002956 case CEPH_MSG_OSD_OPREPLY:
2957 return get_reply(con, hdr, skip);
2958 default:
2959 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2960 osd->o_osd);
2961 *skip = 1;
2962 return NULL;
2963 }
Sage Weilf24e9982009-10-06 11:31:10 -07002964}
2965
2966/*
2967 * Wrappers to refcount containing ceph_osd struct
2968 */
2969static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2970{
2971 struct ceph_osd *osd = con->private;
2972 if (get_osd(osd))
2973 return con;
2974 return NULL;
2975}
2976
2977static void put_osd_con(struct ceph_connection *con)
2978{
2979 struct ceph_osd *osd = con->private;
2980 put_osd(osd);
2981}
2982
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002983/*
2984 * authentication
2985 */
Alex Eldera3530df2012-05-16 15:16:39 -05002986/*
2987 * Note: returned pointer is the address of a structure that's
2988 * managed separately. Caller must *not* attempt to free it.
2989 */
2990static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002991 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002992{
2993 struct ceph_osd *o = con->private;
2994 struct ceph_osd_client *osdc = o->o_osdc;
2995 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002996 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002997
Alex Elder74f18692012-05-16 15:16:39 -05002998 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02002999 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05003000 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003001 }
Sage Weil27859f92013-03-25 10:26:14 -07003002 if (!auth->authorizer) {
3003 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
3004 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003005 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05003006 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07003007 } else {
3008 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07003009 auth);
3010 if (ret)
3011 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003012 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003013 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05003014
Alex Eldera3530df2012-05-16 15:16:39 -05003015 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003016}
3017
3018
3019static int verify_authorizer_reply(struct ceph_connection *con, int len)
3020{
3021 struct ceph_osd *o = con->private;
3022 struct ceph_osd_client *osdc = o->o_osdc;
3023 struct ceph_auth_client *ac = osdc->client->monc.auth;
3024
Sage Weil27859f92013-03-25 10:26:14 -07003025 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003026}
3027
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003028static int invalidate_authorizer(struct ceph_connection *con)
3029{
3030 struct ceph_osd *o = con->private;
3031 struct ceph_osd_client *osdc = o->o_osdc;
3032 struct ceph_auth_client *ac = osdc->client->monc.auth;
3033
Sage Weil27859f92013-03-25 10:26:14 -07003034 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003035 return ceph_monc_validate_auth(&osdc->client->monc);
3036}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003037
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003038static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003039{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003040 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003041 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003042
Yan, Zheng33d07332014-11-04 16:33:37 +08003043 return ceph_auth_sign_message(auth, msg);
3044}
3045
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003046static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003047{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003048 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003049 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003050
Yan, Zheng33d07332014-11-04 16:33:37 +08003051 return ceph_auth_check_message_signature(auth, msg);
3052}
3053
Tobias Klauser9e327892010-05-20 10:40:19 +02003054static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07003055 .get = get_osd_con,
3056 .put = put_osd_con,
3057 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003058 .get_authorizer = get_authorizer,
3059 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003060 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003061 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003062 .sign_message = osd_sign_message,
3063 .check_message_signature = osd_check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003064 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003065};