blob: ccd4e031fa3f20ea9200395f16b620e4357420a8 [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_OP_FRONT_LEN 4096
23#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080024
Alex Elder5522ae02013-05-01 12:43:04 -050025static struct kmem_cache *ceph_osd_request_cache;
26
Tobias Klauser9e327892010-05-20 10:40:19 +020027static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070028
Alex Elderf9d25192013-02-15 11:42:29 -060029static void __send_queued(struct ceph_osd_client *osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -080030static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070031static void __register_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040033static void __unregister_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070035static void __unregister_linger_request(struct ceph_osd_client *osdc,
36 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040037static void __enqueue_request(struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080038static void __send_request(struct ceph_osd_client *osdc,
39 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070040
41/*
42 * Implement client access to distributed object storage cluster.
43 *
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
48 * access to storage.
49 *
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
52 *
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
57 */
58
59/*
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
62 * object boundary.
63 *
64 * fill osd op in request message.
65 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060066static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050067 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070068{
Alex Elder60e56f12013-02-15 11:42:29 -060069 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070070 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070071
Alex Elder60e56f12013-02-15 11:42:29 -060072 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050073 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
74 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070075 if (r < 0)
76 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050077 if (*objlen < orig_len) {
78 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060079 dout(" skipping last %llu, final file extent %llu~%llu\n",
80 orig_len - *plen, off, *plen);
81 }
82
Alex Elder75d1c942013-03-13 20:50:00 -050083 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070084
Alex Elder3ff5f382013-02-15 22:10:17 -060085 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070086}
87
Alex Elderc54d47b2013-04-03 01:28:57 -050088static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
89{
90 memset(osd_data, 0, sizeof (*osd_data));
91 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
92}
93
Alex Eldera4ce40a2013-04-05 01:27:12 -050094static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -050095 struct page **pages, u64 length, u32 alignment,
96 bool pages_from_pool, bool own_pages)
97{
98 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
99 osd_data->pages = pages;
100 osd_data->length = length;
101 osd_data->alignment = alignment;
102 osd_data->pages_from_pool = pages_from_pool;
103 osd_data->own_pages = own_pages;
104}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500105
Alex Eldera4ce40a2013-04-05 01:27:12 -0500106static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500107 struct ceph_pagelist *pagelist)
108{
109 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
110 osd_data->pagelist = pagelist;
111}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500112
113#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500114static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500115 struct bio *bio, size_t bio_length)
116{
117 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
118 osd_data->bio = bio;
119 osd_data->bio_length = bio_length;
120}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500121#endif /* CONFIG_BLOCK */
122
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300123#define osd_req_op_data(oreq, whch, typ, fld) \
124({ \
125 struct ceph_osd_request *__oreq = (oreq); \
126 unsigned int __whch = (whch); \
127 BUG_ON(__whch >= __oreq->r_num_ops); \
128 &__oreq->r_ops[__whch].typ.fld; \
129})
Alex Elder863c7eb2013-04-15 14:50:36 -0500130
Alex Elder49719772013-02-11 12:33:24 -0600131static struct ceph_osd_data *
132osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
133{
134 BUG_ON(which >= osd_req->r_num_ops);
135
136 return &osd_req->r_ops[which].raw_data_in;
137}
138
Alex Eldera4ce40a2013-04-05 01:27:12 -0500139struct ceph_osd_data *
140osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500141 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500142{
Alex Elder863c7eb2013-04-15 14:50:36 -0500143 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500144}
145EXPORT_SYMBOL(osd_req_op_extent_osd_data);
146
147struct ceph_osd_data *
Alex Eldera4ce40a2013-04-05 01:27:12 -0500148osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
149 unsigned int which)
150{
Alex Elder863c7eb2013-04-15 14:50:36 -0500151 return osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500152}
153EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
154
Alex Elder49719772013-02-11 12:33:24 -0600155void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
156 unsigned int which, struct page **pages,
157 u64 length, u32 alignment,
158 bool pages_from_pool, bool own_pages)
159{
160 struct ceph_osd_data *osd_data;
161
162 osd_data = osd_req_op_raw_data_in(osd_req, which);
163 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
164 pages_from_pool, own_pages);
165}
166EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
167
Alex Eldera4ce40a2013-04-05 01:27:12 -0500168void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500169 unsigned int which, struct page **pages,
170 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500171 bool pages_from_pool, bool own_pages)
172{
173 struct ceph_osd_data *osd_data;
174
Alex Elder863c7eb2013-04-15 14:50:36 -0500175 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500176 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
177 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500178}
179EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
180
181void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500182 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500183{
184 struct ceph_osd_data *osd_data;
185
Alex Elder863c7eb2013-04-15 14:50:36 -0500186 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500187 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500188}
189EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
190
191#ifdef CONFIG_BLOCK
192void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500193 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500194{
195 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500196
197 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500198 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500199}
200EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
201#endif /* CONFIG_BLOCK */
202
203static void osd_req_op_cls_request_info_pagelist(
204 struct ceph_osd_request *osd_req,
205 unsigned int which, struct ceph_pagelist *pagelist)
206{
207 struct ceph_osd_data *osd_data;
208
Alex Elder863c7eb2013-04-15 14:50:36 -0500209 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500210 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500211}
212
Alex Elder04017e22013-04-05 14:46:02 -0500213void osd_req_op_cls_request_data_pagelist(
214 struct ceph_osd_request *osd_req,
215 unsigned int which, struct ceph_pagelist *pagelist)
216{
217 struct ceph_osd_data *osd_data;
218
Alex Elder863c7eb2013-04-15 14:50:36 -0500219 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500220 ceph_osd_data_pagelist_init(osd_data, pagelist);
221}
222EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
223
Alex Elder6c57b552013-04-19 15:34:49 -0500224void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
225 unsigned int which, struct page **pages, u64 length,
226 u32 alignment, bool pages_from_pool, bool own_pages)
227{
228 struct ceph_osd_data *osd_data;
229
230 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
231 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
232 pages_from_pool, own_pages);
233}
234EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
235
Alex Eldera4ce40a2013-04-05 01:27:12 -0500236void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
237 unsigned int which, struct page **pages, u64 length,
238 u32 alignment, bool pages_from_pool, bool own_pages)
239{
240 struct ceph_osd_data *osd_data;
241
Alex Elder863c7eb2013-04-15 14:50:36 -0500242 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500243 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
244 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500245}
246EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
247
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500248static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
249{
250 switch (osd_data->type) {
251 case CEPH_OSD_DATA_TYPE_NONE:
252 return 0;
253 case CEPH_OSD_DATA_TYPE_PAGES:
254 return osd_data->length;
255 case CEPH_OSD_DATA_TYPE_PAGELIST:
256 return (u64)osd_data->pagelist->length;
257#ifdef CONFIG_BLOCK
258 case CEPH_OSD_DATA_TYPE_BIO:
259 return (u64)osd_data->bio_length;
260#endif /* CONFIG_BLOCK */
261 default:
262 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
263 return 0;
264 }
265}
266
Alex Elderc54d47b2013-04-03 01:28:57 -0500267static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
268{
Alex Elder54764922013-04-05 01:27:12 -0500269 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500270 int num_pages;
271
272 num_pages = calc_pages_for((u64)osd_data->alignment,
273 (u64)osd_data->length);
274 ceph_release_page_vector(osd_data->pages, num_pages);
275 }
Alex Elder54764922013-04-05 01:27:12 -0500276 ceph_osd_data_init(osd_data);
277}
278
279static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
280 unsigned int which)
281{
282 struct ceph_osd_req_op *op;
283
284 BUG_ON(which >= osd_req->r_num_ops);
285 op = &osd_req->r_ops[which];
286
287 switch (op->op) {
288 case CEPH_OSD_OP_READ:
289 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200290 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500291 ceph_osd_data_release(&op->extent.osd_data);
292 break;
293 case CEPH_OSD_OP_CALL:
294 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500295 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500296 ceph_osd_data_release(&op->cls.response_data);
297 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800298 case CEPH_OSD_OP_SETXATTR:
299 case CEPH_OSD_OP_CMPXATTR:
300 ceph_osd_data_release(&op->xattr.osd_data);
301 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800302 case CEPH_OSD_OP_STAT:
303 ceph_osd_data_release(&op->raw_data_in);
304 break;
Alex Elder54764922013-04-05 01:27:12 -0500305 default:
306 break;
307 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500308}
309
Sage Weilf24e9982009-10-06 11:31:10 -0700310/*
311 * requests
312 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400313static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700314{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400315 struct ceph_osd_request *req = container_of(kref,
316 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500317 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800318
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400319 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
320 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400321 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
322 WARN_ON(!list_empty(&req->r_req_lru_item));
323 WARN_ON(!list_empty(&req->r_osd_item));
324 WARN_ON(!list_empty(&req->r_linger_item));
325 WARN_ON(!list_empty(&req->r_linger_osd_item));
326 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400327
Sage Weil415e49a2009-12-07 13:37:03 -0800328 if (req->r_request)
329 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500330 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500331 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500332 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500333 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600334
Alex Elder54764922013-04-05 01:27:12 -0500335 for (which = 0; which < req->r_num_ops; which++)
336 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600337
Sage Weil415e49a2009-12-07 13:37:03 -0800338 ceph_put_snap_context(req->r_snapc);
339 if (req->r_mempool)
340 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100341 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500342 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100343 else
344 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700345}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400346
347void ceph_osdc_get_request(struct ceph_osd_request *req)
348{
349 dout("%s %p (was %d)\n", __func__, req,
350 atomic_read(&req->r_kref.refcount));
351 kref_get(&req->r_kref);
352}
353EXPORT_SYMBOL(ceph_osdc_get_request);
354
355void ceph_osdc_put_request(struct ceph_osd_request *req)
356{
357 dout("%s %p (was %d)\n", __func__, req,
358 atomic_read(&req->r_kref.refcount));
359 kref_put(&req->r_kref, ceph_osdc_release_request);
360}
361EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700362
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700363struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700364 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800365 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700366 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600367 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700368{
369 struct ceph_osd_request *req;
370 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800371 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700372
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700373 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100374 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700375 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100376 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
377 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700378 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100379 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
380 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
381 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700382 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100383 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700384 return NULL;
385
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100386 /* req only, each op is zeroed in _osd_req_op_init() */
387 memset(req, 0, sizeof(*req));
388
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700389 req->r_osdc = osdc;
390 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500391 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700392
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700393 kref_init(&req->r_kref);
394 init_completion(&req->r_completion);
395 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600396 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700397 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700398 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400399 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700400 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700401 INIT_LIST_HEAD(&req->r_osd_item);
402
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200403 req->r_base_oloc.pool = -1;
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200404 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200405
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100406 msg_size = OSD_OPREPLY_FRONT_LEN;
407 if (num_ops > CEPH_OSD_SLAB_OPS) {
408 /* ceph_osd_op and rval */
409 msg_size += (num_ops - CEPH_OSD_SLAB_OPS) *
410 (sizeof(struct ceph_osd_op) + 4);
411 }
412
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700413 /* create reply message */
414 if (use_mempool)
415 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
416 else
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100417 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size,
418 gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700419 if (!msg) {
420 ceph_osdc_put_request(req);
421 return NULL;
422 }
423 req->r_reply = msg;
424
Ilya Dryomovae458f52016-02-11 13:09:15 +0100425 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
426 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
427 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
428 msg_size += 1 + 8 + 4 + 4; /* pgid */
429 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
430 msg_size += 2 + num_ops * sizeof(struct ceph_osd_op);
431 msg_size += 8; /* snapid */
432 msg_size += 8; /* snap_seq */
433 msg_size += 4 + 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
434 msg_size += 4; /* retry_attempt */
435
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700436 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700437 if (use_mempool)
438 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
439 else
Sage Weilb61c2762011-08-09 15:03:46 -0700440 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700441 if (!msg) {
442 ceph_osdc_put_request(req);
443 return NULL;
444 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700445
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700446 memset(msg->front.iov_base, 0, msg->front.iov_len);
447
448 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700449
450 return req;
451}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700452EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700453
Alex Eldera8dd0a32013-03-13 20:50:00 -0500454static bool osd_req_opcode_valid(u16 opcode)
455{
456 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400457#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
458__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
459#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500460 default:
461 return false;
462 }
463}
464
Alex Elder33803f32013-03-13 20:50:00 -0500465/*
466 * This is an osd op init function for opcodes that have no data or
467 * other information associated with them. It also serves as a
468 * common init routine for all the other init functions, below.
469 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500470static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600471_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800472 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500473{
Alex Elderc99d2d42013-04-05 01:27:11 -0500474 struct ceph_osd_req_op *op;
475
476 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500477 BUG_ON(!osd_req_opcode_valid(opcode));
478
Alex Elderc99d2d42013-04-05 01:27:11 -0500479 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500480 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500481 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800482 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500483
484 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500485}
486
Alex Elder49719772013-02-11 12:33:24 -0600487void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800488 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600489{
Yan, Zheng144cba12015-04-27 11:09:54 +0800490 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600491}
492EXPORT_SYMBOL(osd_req_op_init);
493
Alex Elderc99d2d42013-04-05 01:27:11 -0500494void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
495 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500496 u64 offset, u64 length,
497 u64 truncate_size, u32 truncate_seq)
498{
Yan, Zheng144cba12015-04-27 11:09:54 +0800499 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
500 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500501 size_t payload_len = 0;
502
Li Wangad7a60d2013-08-15 11:51:44 +0800503 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200504 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
505 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500506
Alex Elder33803f32013-03-13 20:50:00 -0500507 op->extent.offset = offset;
508 op->extent.length = length;
509 op->extent.truncate_size = truncate_size;
510 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200511 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500512 payload_len += length;
513
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100514 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500515}
516EXPORT_SYMBOL(osd_req_op_extent_init);
517
Alex Elderc99d2d42013-04-05 01:27:11 -0500518void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
519 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500520{
Alex Elderc99d2d42013-04-05 01:27:11 -0500521 struct ceph_osd_req_op *op;
522 u64 previous;
523
524 BUG_ON(which >= osd_req->r_num_ops);
525 op = &osd_req->r_ops[which];
526 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500527
528 if (length == previous)
529 return; /* Nothing to do */
530 BUG_ON(length > previous);
531
532 op->extent.length = length;
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100533 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500534}
535EXPORT_SYMBOL(osd_req_op_extent_update);
536
Alex Elderc99d2d42013-04-05 01:27:11 -0500537void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500538 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500539{
Yan, Zheng144cba12015-04-27 11:09:54 +0800540 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
541 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500542 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500543 size_t payload_len = 0;
544 size_t size;
545
546 BUG_ON(opcode != CEPH_OSD_OP_CALL);
547
Alex Elder5f562df2013-04-05 01:27:12 -0500548 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
549 BUG_ON(!pagelist);
550 ceph_pagelist_init(pagelist);
551
Alex Elder33803f32013-03-13 20:50:00 -0500552 op->cls.class_name = class;
553 size = strlen(class);
554 BUG_ON(size > (size_t) U8_MAX);
555 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500556 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500557 payload_len += size;
558
559 op->cls.method_name = method;
560 size = strlen(method);
561 BUG_ON(size > (size_t) U8_MAX);
562 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500563 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500564 payload_len += size;
565
Alex Eldera4ce40a2013-04-05 01:27:12 -0500566 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500567
Alex Elder33803f32013-03-13 20:50:00 -0500568 op->cls.argc = 0; /* currently unused */
569
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100570 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500571}
572EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500573
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800574int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
575 u16 opcode, const char *name, const void *value,
576 size_t size, u8 cmp_op, u8 cmp_mode)
577{
Yan, Zheng144cba12015-04-27 11:09:54 +0800578 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
579 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800580 struct ceph_pagelist *pagelist;
581 size_t payload_len;
582
583 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
584
585 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
586 if (!pagelist)
587 return -ENOMEM;
588
589 ceph_pagelist_init(pagelist);
590
591 payload_len = strlen(name);
592 op->xattr.name_len = payload_len;
593 ceph_pagelist_append(pagelist, name, payload_len);
594
595 op->xattr.value_len = size;
596 ceph_pagelist_append(pagelist, value, size);
597 payload_len += size;
598
599 op->xattr.cmp_op = cmp_op;
600 op->xattr.cmp_mode = cmp_mode;
601
602 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100603 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800604 return 0;
605}
606EXPORT_SYMBOL(osd_req_op_xattr_init);
607
Alex Elderc99d2d42013-04-05 01:27:11 -0500608void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
609 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500610 u64 cookie, u64 version, int flag)
611{
Yan, Zheng144cba12015-04-27 11:09:54 +0800612 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
613 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500614
Alex Elderc99d2d42013-04-05 01:27:11 -0500615 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500616
617 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500618 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500619 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500620 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500621}
622EXPORT_SYMBOL(osd_req_op_watch_init);
623
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200624void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
625 unsigned int which,
626 u64 expected_object_size,
627 u64 expected_write_size)
628{
629 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800630 CEPH_OSD_OP_SETALLOCHINT,
631 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200632
633 op->alloc_hint.expected_object_size = expected_object_size;
634 op->alloc_hint.expected_write_size = expected_write_size;
635
636 /*
637 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
638 * not worth a feature bit. Set FAILOK per-op flag to make
639 * sure older osds don't trip over an unsupported opcode.
640 */
641 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
642}
643EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
644
Alex Elder90af3602013-04-05 14:46:01 -0500645static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500646 struct ceph_osd_data *osd_data)
647{
648 u64 length = ceph_osd_data_length(osd_data);
649
650 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
651 BUG_ON(length > (u64) SIZE_MAX);
652 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500653 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500654 length, osd_data->alignment);
655 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
656 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500657 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500658#ifdef CONFIG_BLOCK
659 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500660 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500661#endif
662 } else {
663 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
664 }
665}
666
Alex Elder175face2013-03-08 13:35:36 -0600667static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500668 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700669{
Alex Elder79528732013-04-03 21:32:51 -0500670 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500671 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500672 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500673 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600674
Alex Elder79528732013-04-03 21:32:51 -0500675 BUG_ON(which >= req->r_num_ops);
676 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500677 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
678 pr_err("unrecognized osd opcode %d\n", src->op);
679
680 return 0;
681 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700682
Alex Elder065a68f2012-04-20 15:49:43 -0500683 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600684 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600685 osd_data = &src->raw_data_in;
686 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600687 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700688 case CEPH_OSD_OP_READ:
689 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200690 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800691 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800692 case CEPH_OSD_OP_TRUNCATE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200693 if (src->op == CEPH_OSD_OP_WRITE ||
694 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder54d50642013-04-03 01:28:58 -0500695 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600696 dst->extent.offset = cpu_to_le64(src->extent.offset);
697 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700698 dst->extent.truncate_size =
699 cpu_to_le64(src->extent.truncate_size);
700 dst->extent.truncate_seq =
701 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500702 osd_data = &src->extent.osd_data;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200703 if (src->op == CEPH_OSD_OP_WRITE ||
704 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder04017e22013-04-05 14:46:02 -0500705 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500706 else
Alex Elder04017e22013-04-05 14:46:02 -0500707 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700708 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700709 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700710 dst->cls.class_len = src->cls.class_len;
711 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500712 osd_data = &src->cls.request_info;
713 ceph_osdc_msg_data_add(req->r_request, osd_data);
714 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
715 request_data_len = osd_data->pagelist->length;
716
717 osd_data = &src->cls.request_data;
718 data_length = ceph_osd_data_length(osd_data);
719 if (data_length) {
720 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
721 dst->cls.indata_len = cpu_to_le32(data_length);
722 ceph_osdc_msg_data_add(req->r_request, osd_data);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100723 src->indata_len += data_length;
Alex Elder04017e22013-04-05 14:46:02 -0500724 request_data_len += data_length;
725 }
726 osd_data = &src->cls.response_data;
727 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700728 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700729 case CEPH_OSD_OP_STARTSYNC:
730 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700731 case CEPH_OSD_OP_NOTIFY_ACK:
732 case CEPH_OSD_OP_WATCH:
733 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
734 dst->watch.ver = cpu_to_le64(src->watch.ver);
735 dst->watch.flag = src->watch.flag;
736 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200737 case CEPH_OSD_OP_SETALLOCHINT:
738 dst->alloc_hint.expected_object_size =
739 cpu_to_le64(src->alloc_hint.expected_object_size);
740 dst->alloc_hint.expected_write_size =
741 cpu_to_le64(src->alloc_hint.expected_write_size);
742 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800743 case CEPH_OSD_OP_SETXATTR:
744 case CEPH_OSD_OP_CMPXATTR:
745 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
746 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
747 dst->xattr.cmp_op = src->xattr.cmp_op;
748 dst->xattr.cmp_mode = src->xattr.cmp_mode;
749 osd_data = &src->xattr.osd_data;
750 ceph_osdc_msg_data_add(req->r_request, osd_data);
751 request_data_len = osd_data->pagelist->length;
752 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800753 case CEPH_OSD_OP_CREATE:
754 case CEPH_OSD_OP_DELETE:
755 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700756 default:
Alex Elder4c464592013-02-15 11:42:30 -0600757 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600758 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600759 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500760
761 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700762 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200763
Alex Eldera8dd0a32013-03-13 20:50:00 -0500764 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200765 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100766 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600767
Alex Elder54d50642013-04-03 01:28:58 -0500768 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700769}
770
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700771/*
Sage Weilf24e9982009-10-06 11:31:10 -0700772 * build new request AND message, calculate layout, and adjust file
773 * extent as needed.
774 *
775 * if the file was recently truncated, we include information about its
776 * old and new size so that the object can be updated appropriately. (we
777 * avoid synchronously deleting truncated objects because it's slow.)
778 *
779 * if @do_sync, include a 'startsync' command so that the osd will flush
780 * data quickly.
781 */
782struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
783 struct ceph_file_layout *layout,
784 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800785 u64 off, u64 *plen,
786 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700787 int opcode, int flags,
788 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700789 u32 truncate_seq,
790 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600791 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700792{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700793 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500794 u64 objnum = 0;
795 u64 objoff = 0;
796 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700797 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700798
Li Wangad7a60d2013-08-15 11:51:44 +0800799 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800800 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
801 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700802
Alex Elderacead002013-03-14 14:09:05 -0500803 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600804 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700805 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700806 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500807
Alex Elderd178a9e2012-11-13 21:11:15 -0600808 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700809
810 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500811 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600812 if (r < 0) {
813 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700814 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600815 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500816
Yan, Zheng864e9192014-11-13 10:47:25 +0800817 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800818 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800819 } else {
820 u32 object_size = le32_to_cpu(layout->fl_object_size);
821 u32 object_base = off - objoff;
822 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
823 if (truncate_size <= object_base) {
824 truncate_size = 0;
825 } else {
826 truncate_size -= object_base;
827 if (truncate_size > object_size)
828 truncate_size = object_size;
829 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800830 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800831 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800832 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500833 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500834
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200835 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700836
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200837 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200838 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200839 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600840
Sage Weilf24e9982009-10-06 11:31:10 -0700841 return req;
842}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700843EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700844
845/*
846 * We keep osd requests in an rbtree, sorted by ->r_tid.
847 */
848static void __insert_request(struct ceph_osd_client *osdc,
849 struct ceph_osd_request *new)
850{
851 struct rb_node **p = &osdc->requests.rb_node;
852 struct rb_node *parent = NULL;
853 struct ceph_osd_request *req = NULL;
854
855 while (*p) {
856 parent = *p;
857 req = rb_entry(parent, struct ceph_osd_request, r_node);
858 if (new->r_tid < req->r_tid)
859 p = &(*p)->rb_left;
860 else if (new->r_tid > req->r_tid)
861 p = &(*p)->rb_right;
862 else
863 BUG();
864 }
865
866 rb_link_node(&new->r_node, parent, p);
867 rb_insert_color(&new->r_node, &osdc->requests);
868}
869
870static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
871 u64 tid)
872{
873 struct ceph_osd_request *req;
874 struct rb_node *n = osdc->requests.rb_node;
875
876 while (n) {
877 req = rb_entry(n, struct ceph_osd_request, r_node);
878 if (tid < req->r_tid)
879 n = n->rb_left;
880 else if (tid > req->r_tid)
881 n = n->rb_right;
882 else
883 return req;
884 }
885 return NULL;
886}
887
888static struct ceph_osd_request *
889__lookup_request_ge(struct ceph_osd_client *osdc,
890 u64 tid)
891{
892 struct ceph_osd_request *req;
893 struct rb_node *n = osdc->requests.rb_node;
894
895 while (n) {
896 req = rb_entry(n, struct ceph_osd_request, r_node);
897 if (tid < req->r_tid) {
898 if (!n->rb_left)
899 return req;
900 n = n->rb_left;
901 } else if (tid > req->r_tid) {
902 n = n->rb_right;
903 } else {
904 return req;
905 }
906 }
907 return NULL;
908}
909
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400910static void __kick_linger_request(struct ceph_osd_request *req)
911{
912 struct ceph_osd_client *osdc = req->r_osdc;
913 struct ceph_osd *osd = req->r_osd;
914
915 /*
916 * Linger requests need to be resent with a new tid to avoid
917 * the dup op detection logic on the OSDs. Achieve this with
918 * a re-register dance instead of open-coding.
919 */
920 ceph_osdc_get_request(req);
921 if (!list_empty(&req->r_linger_item))
922 __unregister_linger_request(osdc, req);
923 else
924 __unregister_request(osdc, req);
925 __register_request(osdc, req);
926 ceph_osdc_put_request(req);
927
928 /*
929 * Unless request has been registered as both normal and
930 * lingering, __unregister{,_linger}_request clears r_osd.
931 * However, here we need to preserve r_osd to make sure we
932 * requeue on the same OSD.
933 */
934 WARN_ON(req->r_osd || !osd);
935 req->r_osd = osd;
936
937 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
938 __enqueue_request(req);
939}
940
Sage Weil6f6c7002011-01-17 20:34:08 -0800941/*
942 * Resubmit requests pending on the given osd.
943 */
944static void __kick_osd_requests(struct ceph_osd_client *osdc,
945 struct ceph_osd *osd)
946{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700947 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500948 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400949 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800950 int err;
951
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400952 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800953 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600954 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800955 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400956
Alex Eldere02493c2013-03-25 18:16:11 -0500957 /*
958 * Build up a list of requests to resend by traversing the
959 * osd's list of requests. Requests for a given object are
960 * sent in tid order, and that is also the order they're
961 * kept on this list. Therefore all requests that are in
962 * flight will be found first, followed by all requests that
963 * have not yet been sent. And to resend requests while
964 * preserving this order we will want to put any sent
965 * requests back on the front of the osd client's unsent
966 * list.
967 *
968 * So we build a separate ordered list of already-sent
969 * requests for the affected osd and splice it onto the
970 * front of the osd client's unsent list. Once we've seen a
971 * request that has not yet been sent we're done. Those
972 * requests are already sitting right where they belong.
973 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800974 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500975 if (!req->r_sent)
976 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400977
978 if (!req->r_linger) {
979 dout("%s requeueing %p tid %llu\n", __func__, req,
980 req->r_tid);
981 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700982 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400983 } else {
984 list_move_tail(&req->r_req_lru_item, &resend_linger);
985 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700986 }
Alex Eldere02493c2013-03-25 18:16:11 -0500987 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700988
Alex Eldere02493c2013-03-25 18:16:11 -0500989 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400990 * Both registered and not yet registered linger requests are
991 * enqueued with a new tid on the same OSD. We add/move them
992 * to req_unsent/o_requests at the end to keep things in tid
993 * order.
Alex Eldere02493c2013-03-25 18:16:11 -0500994 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700995 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400996 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400997 WARN_ON(!list_empty(&req->r_req_lru_item));
998 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800999 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001000
1001 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
1002 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001003}
1004
Sage Weilf24e9982009-10-06 11:31:10 -07001005/*
Sage Weil81b024e2009-10-09 10:29:18 -07001006 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -07001007 */
1008static void osd_reset(struct ceph_connection *con)
1009{
1010 struct ceph_osd *osd = con->private;
1011 struct ceph_osd_client *osdc;
1012
1013 if (!osd)
1014 return;
1015 dout("osd_reset osd%d\n", osd->o_osd);
1016 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001017 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001018 mutex_lock(&osdc->request_mutex);
1019 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001020 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001021 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001022 up_read(&osdc->map_sem);
1023}
1024
1025/*
1026 * Track open sessions with osds.
1027 */
Alex Eldere10006f2012-05-26 23:26:43 -05001028static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001029{
1030 struct ceph_osd *osd;
1031
1032 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1033 if (!osd)
1034 return NULL;
1035
1036 atomic_set(&osd->o_ref, 1);
1037 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001038 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001039 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001040 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001041 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001042 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001043 osd->o_incarnation = 1;
1044
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001045 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001046
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001047 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001048 return osd;
1049}
1050
1051static struct ceph_osd *get_osd(struct ceph_osd *osd)
1052{
1053 if (atomic_inc_not_zero(&osd->o_ref)) {
1054 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1055 atomic_read(&osd->o_ref));
1056 return osd;
1057 } else {
1058 dout("get_osd %p FAIL\n", osd);
1059 return NULL;
1060 }
1061}
1062
1063static void put_osd(struct ceph_osd *osd)
1064{
1065 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1066 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001067 if (atomic_dec_and_test(&osd->o_ref)) {
Sage Weil79494d12010-05-27 14:15:49 -07001068 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1069
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001070 if (osd->o_auth.authorizer)
1071 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001072 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001073 }
Sage Weilf24e9982009-10-06 11:31:10 -07001074}
1075
1076/*
1077 * remove an osd from our map
1078 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001079static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001080{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001081 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001082 WARN_ON(!list_empty(&osd->o_requests));
1083 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001084
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001085 list_del_init(&osd->o_osd_lru);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001086 rb_erase(&osd->o_node, &osdc->osds);
1087 RB_CLEAR_NODE(&osd->o_node);
1088}
1089
1090static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1091{
1092 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1093
1094 if (!RB_EMPTY_NODE(&osd->o_node)) {
1095 ceph_con_close(&osd->o_con);
1096 __remove_osd(osdc, osd);
1097 put_osd(osd);
1098 }
Sage Weilf24e9982009-10-06 11:31:10 -07001099}
1100
Sage Weilaca420b2011-08-31 14:45:53 -07001101static void remove_all_osds(struct ceph_osd_client *osdc)
1102{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001103 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001104 mutex_lock(&osdc->request_mutex);
1105 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1106 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1107 struct ceph_osd, o_node);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001108 remove_osd(osdc, osd);
Sage Weilaca420b2011-08-31 14:45:53 -07001109 }
1110 mutex_unlock(&osdc->request_mutex);
1111}
1112
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001113static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1114 struct ceph_osd *osd)
1115{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001116 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001117 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001118
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001119 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001120 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001121}
1122
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001123static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1124 struct ceph_osd *osd)
1125{
1126 dout("%s %p\n", __func__, osd);
1127
1128 if (list_empty(&osd->o_requests) &&
1129 list_empty(&osd->o_linger_requests))
1130 __move_osd_to_lru(osdc, osd);
1131}
1132
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001133static void __remove_osd_from_lru(struct ceph_osd *osd)
1134{
1135 dout("__remove_osd_from_lru %p\n", osd);
1136 if (!list_empty(&osd->o_osd_lru))
1137 list_del_init(&osd->o_osd_lru);
1138}
1139
Sage Weilaca420b2011-08-31 14:45:53 -07001140static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001141{
1142 struct ceph_osd *osd, *nosd;
1143
1144 dout("__remove_old_osds %p\n", osdc);
1145 mutex_lock(&osdc->request_mutex);
1146 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001147 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001148 break;
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001149 remove_osd(osdc, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001150 }
1151 mutex_unlock(&osdc->request_mutex);
1152}
1153
Sage Weilf24e9982009-10-06 11:31:10 -07001154/*
1155 * reset osd connect
1156 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001157static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001158{
Alex Elderc3acb182012-12-07 09:57:58 -06001159 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001160
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001161 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001162 if (list_empty(&osd->o_requests) &&
1163 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001164 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001165 return -ENODEV;
1166 }
1167
1168 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1169 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1170 !ceph_con_opened(&osd->o_con)) {
1171 struct ceph_osd_request *req;
1172
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001173 dout("osd addr hasn't changed and connection never opened, "
1174 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001175 /* touch each r_stamp for handle_timeout()'s benfit */
1176 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1177 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001178
1179 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001180 }
Alex Elderc3acb182012-12-07 09:57:58 -06001181
1182 ceph_con_close(&osd->o_con);
1183 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1184 osd->o_incarnation++;
1185
1186 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001187}
1188
1189static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1190{
1191 struct rb_node **p = &osdc->osds.rb_node;
1192 struct rb_node *parent = NULL;
1193 struct ceph_osd *osd = NULL;
1194
Sage Weilaca420b2011-08-31 14:45:53 -07001195 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001196 while (*p) {
1197 parent = *p;
1198 osd = rb_entry(parent, struct ceph_osd, o_node);
1199 if (new->o_osd < osd->o_osd)
1200 p = &(*p)->rb_left;
1201 else if (new->o_osd > osd->o_osd)
1202 p = &(*p)->rb_right;
1203 else
1204 BUG();
1205 }
1206
1207 rb_link_node(&new->o_node, parent, p);
1208 rb_insert_color(&new->o_node, &osdc->osds);
1209}
1210
1211static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1212{
1213 struct ceph_osd *osd;
1214 struct rb_node *n = osdc->osds.rb_node;
1215
1216 while (n) {
1217 osd = rb_entry(n, struct ceph_osd, o_node);
1218 if (o < osd->o_osd)
1219 n = n->rb_left;
1220 else if (o > osd->o_osd)
1221 n = n->rb_right;
1222 else
1223 return osd;
1224 }
1225 return NULL;
1226}
1227
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001228static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1229{
1230 schedule_delayed_work(&osdc->timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03001231 osdc->client->options->osd_keepalive_timeout);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001232}
1233
1234static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1235{
1236 cancel_delayed_work(&osdc->timeout_work);
1237}
Sage Weilf24e9982009-10-06 11:31:10 -07001238
1239/*
1240 * Register request, assign tid. If this is the first request, set up
1241 * the timeout event.
1242 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001243static void __register_request(struct ceph_osd_client *osdc,
1244 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001245{
Sage Weilf24e9982009-10-06 11:31:10 -07001246 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001247 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001248 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001249 __insert_request(osdc, req);
1250 ceph_osdc_get_request(req);
1251 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001252 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001253 dout(" first request, scheduling timeout\n");
1254 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001255 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001256}
1257
Sage Weilf24e9982009-10-06 11:31:10 -07001258/*
1259 * called under osdc->request_mutex
1260 */
1261static void __unregister_request(struct ceph_osd_client *osdc,
1262 struct ceph_osd_request *req)
1263{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001264 if (RB_EMPTY_NODE(&req->r_node)) {
1265 dout("__unregister_request %p tid %lld not registered\n",
1266 req, req->r_tid);
1267 return;
1268 }
1269
Sage Weilf24e9982009-10-06 11:31:10 -07001270 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1271 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001272 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001273 osdc->num_requests--;
1274
Sage Weil0ba64782009-10-08 16:57:16 -07001275 if (req->r_osd) {
1276 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001277 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001278
1279 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001280 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001281 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001282 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001283 }
Sage Weilf24e9982009-10-06 11:31:10 -07001284
Alex Elder7d5f2482012-11-29 08:37:03 -06001285 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001286 ceph_osdc_put_request(req);
1287
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001288 if (osdc->num_requests == 0) {
1289 dout(" no requests, canceling timeout\n");
1290 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001291 }
1292}
1293
1294/*
1295 * Cancel a previously queued request message
1296 */
1297static void __cancel_request(struct ceph_osd_request *req)
1298{
Sage Weil6bc18872010-09-27 10:18:52 -07001299 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001300 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001301 req->r_sent = 0;
1302 }
1303}
1304
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001305static void __register_linger_request(struct ceph_osd_client *osdc,
1306 struct ceph_osd_request *req)
1307{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001308 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1309 WARN_ON(!req->r_linger);
1310
Alex Elder96e4dac2013-05-22 20:54:25 -05001311 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001312 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001313 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001314 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001315 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001316}
1317
1318static void __unregister_linger_request(struct ceph_osd_client *osdc,
1319 struct ceph_osd_request *req)
1320{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001321 WARN_ON(!req->r_linger);
1322
1323 if (list_empty(&req->r_linger_item)) {
1324 dout("%s %p tid %llu not registered\n", __func__, req,
1325 req->r_tid);
1326 return;
1327 }
1328
1329 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001330 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001331
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001332 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001333 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001334 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001335 if (list_empty(&req->r_osd_item))
1336 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001337 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001338 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001339}
1340
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001341void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1342 struct ceph_osd_request *req)
1343{
1344 if (!req->r_linger) {
1345 dout("set_request_linger %p\n", req);
1346 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001347 }
1348}
1349EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1350
Sage Weilf24e9982009-10-06 11:31:10 -07001351/*
Josh Durgind29adb32013-12-02 19:11:48 -08001352 * Returns whether a request should be blocked from being sent
1353 * based on the current osdmap and osd_client settings.
1354 *
1355 * Caller should hold map_sem for read.
1356 */
1357static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1358 struct ceph_osd_request *req)
1359{
1360 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1361 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1362 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1363 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1364 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1365}
1366
1367/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001368 * Calculate mapping of a request to a PG. Takes tiering into account.
1369 */
1370static int __calc_request_pg(struct ceph_osdmap *osdmap,
1371 struct ceph_osd_request *req,
1372 struct ceph_pg *pg_out)
1373{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001374 bool need_check_tiering;
1375
1376 need_check_tiering = false;
1377 if (req->r_target_oloc.pool == -1) {
1378 req->r_target_oloc = req->r_base_oloc; /* struct */
1379 need_check_tiering = true;
1380 }
1381 if (req->r_target_oid.name_len == 0) {
1382 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1383 need_check_tiering = true;
1384 }
1385
1386 if (need_check_tiering &&
1387 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001388 struct ceph_pg_pool_info *pi;
1389
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001390 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001391 if (pi) {
1392 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1393 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001394 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001395 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1396 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001397 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001398 }
1399 /* !pi is caught in ceph_oloc_oid_to_pg() */
1400 }
1401
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001402 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1403 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001404}
1405
Ilya Dryomovf671b582014-09-02 13:40:33 +04001406static void __enqueue_request(struct ceph_osd_request *req)
1407{
1408 struct ceph_osd_client *osdc = req->r_osdc;
1409
1410 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1411 req->r_osd ? req->r_osd->o_osd : -1);
1412
1413 if (req->r_osd) {
1414 __remove_osd_from_lru(req->r_osd);
1415 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1416 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1417 } else {
1418 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1419 }
1420}
1421
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001422/*
Sage Weilf24e9982009-10-06 11:31:10 -07001423 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1424 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001425 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001426 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001427 *
1428 * Return 0 if unchanged, 1 if changed, or negative on error.
1429 *
1430 * Caller should hold map_sem for read and request_mutex.
1431 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001432static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001433 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001434{
Sage Weil5b191d92013-02-23 10:38:16 -08001435 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001436 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001437 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001438 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001439 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001440
Sage Weil6f6c7002011-01-17 20:34:08 -08001441 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001442
1443 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001444 if (err) {
1445 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001446 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001447 }
Sage Weil7740a422010-01-08 15:58:25 -08001448 req->r_pgid = pgid;
1449
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001450 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1451 if (num < 0)
1452 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001453
Josh Durgind29adb32013-12-02 19:11:48 -08001454 was_paused = req->r_paused;
1455 req->r_paused = __req_should_be_paused(osdc, req);
1456 if (was_paused && !req->r_paused)
1457 force_resend = 1;
1458
Sage Weil38d64532011-10-14 13:33:55 -07001459 if ((!force_resend &&
1460 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001461 req->r_sent >= req->r_osd->o_incarnation &&
1462 req->r_num_pg_osds == num &&
1463 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001464 (req->r_osd == NULL && o == -1) ||
1465 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001466 return 0; /* no change */
1467
Sage Weil5b191d92013-02-23 10:38:16 -08001468 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1469 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001470 req->r_osd ? req->r_osd->o_osd : -1);
1471
Sage Weild85b7052010-05-10 10:24:48 -07001472 /* record full pg acting set */
1473 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1474 req->r_num_pg_osds = num;
1475
Sage Weilf24e9982009-10-06 11:31:10 -07001476 if (req->r_osd) {
1477 __cancel_request(req);
1478 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001479 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001480 req->r_osd = NULL;
1481 }
1482
1483 req->r_osd = __lookup_osd(osdc, o);
1484 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001485 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001486 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001487 if (!req->r_osd) {
1488 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001489 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001490 }
Sage Weilf24e9982009-10-06 11:31:10 -07001491
Sage Weil6f6c7002011-01-17 20:34:08 -08001492 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001493 __insert_osd(osdc, req->r_osd);
1494
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001495 ceph_con_open(&req->r_osd->o_con,
1496 CEPH_ENTITY_TYPE_OSD, o,
1497 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001498 }
1499
Ilya Dryomovf671b582014-09-02 13:40:33 +04001500 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001501 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001502
1503out:
Sage Weilf24e9982009-10-06 11:31:10 -07001504 return err;
1505}
1506
1507/*
1508 * caller should hold map_sem (for read) and request_mutex
1509 */
Sage Weil56e925b2012-01-03 12:34:34 -08001510static void __send_request(struct ceph_osd_client *osdc,
1511 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001512{
Sage Weil1b83bef2013-02-25 16:11:12 -08001513 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001514
Sage Weil1b83bef2013-02-25 16:11:12 -08001515 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1516 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1517 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001518
Sage Weil1b83bef2013-02-25 16:11:12 -08001519 /* fill in message content that changes each time we send it */
1520 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1521 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001522 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001523 p = req->r_request_pgid;
1524 ceph_encode_64(&p, req->r_pgid.pool);
1525 ceph_encode_32(&p, req->r_pgid.seed);
1526 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1527 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1528 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001529
Sage Weil3dd72fc2010-03-22 14:42:30 -07001530 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001531 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001532
1533 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001534
Sage Weilf24e9982009-10-06 11:31:10 -07001535 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001536
1537 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001538}
1539
1540/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001541 * Send any requests in the queue (req_unsent).
1542 */
Alex Elderf9d25192013-02-15 11:42:29 -06001543static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001544{
1545 struct ceph_osd_request *req, *tmp;
1546
Alex Elderf9d25192013-02-15 11:42:29 -06001547 dout("__send_queued\n");
1548 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001549 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001550}
1551
1552/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001553 * Caller should hold map_sem for read and request_mutex.
1554 */
1555static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1556 struct ceph_osd_request *req,
1557 bool nofail)
1558{
1559 int rc;
1560
1561 __register_request(osdc, req);
1562 req->r_sent = 0;
1563 req->r_got_reply = 0;
1564 rc = __map_request(osdc, req, 0);
1565 if (rc < 0) {
1566 if (nofail) {
1567 dout("osdc_start_request failed map, "
1568 " will retry %lld\n", req->r_tid);
1569 rc = 0;
1570 } else {
1571 __unregister_request(osdc, req);
1572 }
1573 return rc;
1574 }
1575
1576 if (req->r_osd == NULL) {
1577 dout("send_request %p no up osds in pg\n", req);
1578 ceph_monc_request_next_osdmap(&osdc->client->monc);
1579 } else {
1580 __send_queued(osdc);
1581 }
1582
1583 return 0;
1584}
1585
1586/*
Sage Weilf24e9982009-10-06 11:31:10 -07001587 * Timeout callback, called every N seconds when 1 or more osd
1588 * requests has been active for more than N seconds. When this
1589 * happens, we ping all OSDs with requests who have timed out to
1590 * ensure any communications channel reset is detected. Reset the
1591 * request timeouts another N seconds in the future as we go.
1592 * Reschedule the timeout event another N seconds in future (unless
1593 * there are no open requests).
1594 */
1595static void handle_timeout(struct work_struct *work)
1596{
1597 struct ceph_osd_client *osdc =
1598 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001599 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001600 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001601 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001602 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001603 dout("timeout\n");
1604 down_read(&osdc->map_sem);
1605
1606 ceph_monc_request_next_osdmap(&osdc->client->monc);
1607
1608 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001609
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001610 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001611 * ping osds that are a bit slow. this ensures that if there
1612 * is a break in the TCP connection we will notice, and reopen
1613 * a connection with that osd (from the fault callback).
1614 */
1615 INIT_LIST_HEAD(&slow_osds);
1616 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001617 if (time_before(jiffies,
1618 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001619 break;
1620
1621 osd = req->r_osd;
1622 BUG_ON(!osd);
1623 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001624 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001625 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1626 }
1627 while (!list_empty(&slow_osds)) {
1628 osd = list_entry(slow_osds.next, struct ceph_osd,
1629 o_keepalive_item);
1630 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001631 ceph_con_keepalive(&osd->o_con);
1632 }
1633
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001634 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001635 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001636 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001637 up_read(&osdc->map_sem);
1638}
1639
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001640static void handle_osds_timeout(struct work_struct *work)
1641{
1642 struct ceph_osd_client *osdc =
1643 container_of(work, struct ceph_osd_client,
1644 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001645 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001646
1647 dout("osds timeout\n");
1648 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001649 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001650 up_read(&osdc->map_sem);
1651
1652 schedule_delayed_work(&osdc->osds_timeout_work,
1653 round_jiffies_relative(delay));
1654}
1655
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001656static int ceph_oloc_decode(void **p, void *end,
1657 struct ceph_object_locator *oloc)
1658{
1659 u8 struct_v, struct_cv;
1660 u32 len;
1661 void *struct_end;
1662 int ret = 0;
1663
1664 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1665 struct_v = ceph_decode_8(p);
1666 struct_cv = ceph_decode_8(p);
1667 if (struct_v < 3) {
1668 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1669 struct_v, struct_cv);
1670 goto e_inval;
1671 }
1672 if (struct_cv > 6) {
1673 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1674 struct_v, struct_cv);
1675 goto e_inval;
1676 }
1677 len = ceph_decode_32(p);
1678 ceph_decode_need(p, end, len, e_inval);
1679 struct_end = *p + len;
1680
1681 oloc->pool = ceph_decode_64(p);
1682 *p += 4; /* skip preferred */
1683
1684 len = ceph_decode_32(p);
1685 if (len > 0) {
1686 pr_warn("ceph_object_locator::key is set\n");
1687 goto e_inval;
1688 }
1689
1690 if (struct_v >= 5) {
1691 len = ceph_decode_32(p);
1692 if (len > 0) {
1693 pr_warn("ceph_object_locator::nspace is set\n");
1694 goto e_inval;
1695 }
1696 }
1697
1698 if (struct_v >= 6) {
1699 s64 hash = ceph_decode_64(p);
1700 if (hash != -1) {
1701 pr_warn("ceph_object_locator::hash is set\n");
1702 goto e_inval;
1703 }
1704 }
1705
1706 /* skip the rest */
1707 *p = struct_end;
1708out:
1709 return ret;
1710
1711e_inval:
1712 ret = -EINVAL;
1713 goto out;
1714}
1715
1716static int ceph_redirect_decode(void **p, void *end,
1717 struct ceph_request_redirect *redir)
1718{
1719 u8 struct_v, struct_cv;
1720 u32 len;
1721 void *struct_end;
1722 int ret;
1723
1724 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1725 struct_v = ceph_decode_8(p);
1726 struct_cv = ceph_decode_8(p);
1727 if (struct_cv > 1) {
1728 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1729 struct_v, struct_cv);
1730 goto e_inval;
1731 }
1732 len = ceph_decode_32(p);
1733 ceph_decode_need(p, end, len, e_inval);
1734 struct_end = *p + len;
1735
1736 ret = ceph_oloc_decode(p, end, &redir->oloc);
1737 if (ret)
1738 goto out;
1739
1740 len = ceph_decode_32(p);
1741 if (len > 0) {
1742 pr_warn("ceph_request_redirect::object_name is set\n");
1743 goto e_inval;
1744 }
1745
1746 len = ceph_decode_32(p);
1747 *p += len; /* skip osd_instructions */
1748
1749 /* skip the rest */
1750 *p = struct_end;
1751out:
1752 return ret;
1753
1754e_inval:
1755 ret = -EINVAL;
1756 goto out;
1757}
1758
Sage Weil25845472011-06-03 09:37:09 -07001759static void complete_request(struct ceph_osd_request *req)
1760{
Sage Weil25845472011-06-03 09:37:09 -07001761 complete_all(&req->r_safe_completion); /* fsync waiter */
1762}
1763
Sage Weilf24e9982009-10-06 11:31:10 -07001764/*
1765 * handle osd op reply. either call the callback if it is specified,
1766 * or do the completion to wake up the waiting thread.
1767 */
Shraddha Barke70cf0522015-10-18 13:55:38 +05301768static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001769{
Sage Weil1b83bef2013-02-25 16:11:12 -08001770 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001771 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001772 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001773 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001774 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001775 unsigned int numops;
1776 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001777 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001778 s32 retry_attempt;
1779 struct ceph_pg pg;
1780 int err;
1781 u32 reassert_epoch;
1782 u64 reassert_version;
1783 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001784 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001785 u32 bytes;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001786 u8 decode_redir;
Alex Elder79528732013-04-03 21:32:51 -05001787 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001788
Sage Weil6df058c2009-12-22 11:24:33 -08001789 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001790 dout("handle_reply %p tid %llu\n", msg, tid);
1791
1792 p = msg->front.iov_base;
1793 end = p + msg->front.iov_len;
1794
1795 ceph_decode_need(&p, end, 4, bad);
1796 object_len = ceph_decode_32(&p);
1797 ceph_decode_need(&p, end, object_len, bad);
1798 p += object_len;
1799
Alex Elderef4859d2013-04-01 18:58:26 -05001800 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001801 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001802 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001803
1804 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1805 flags = ceph_decode_64(&p);
1806 result = ceph_decode_32(&p);
1807 reassert_epoch = ceph_decode_32(&p);
1808 reassert_version = ceph_decode_64(&p);
1809 osdmap_epoch = ceph_decode_32(&p);
1810
Sage Weilf24e9982009-10-06 11:31:10 -07001811 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001812 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001813 mutex_lock(&osdc->request_mutex);
1814 req = __lookup_request(osdc, tid);
1815 if (req == NULL) {
1816 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001817 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001818 }
1819 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001820
1821 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1822 req, result);
1823
Dan Carpenter18741192013-08-15 08:51:58 +03001824 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001825 numops = ceph_decode_32(&p);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01001826 if (numops > CEPH_OSD_MAX_OPS)
Sage Weil1b83bef2013-02-25 16:11:12 -08001827 goto bad_put;
1828 if (numops != req->r_num_ops)
1829 goto bad_put;
1830 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001831 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001832 for (i = 0; i < numops; i++) {
1833 struct ceph_osd_op *op = p;
1834 int len;
1835
1836 len = le32_to_cpu(op->payload_len);
Yan, Zheng7665d852016-01-07 16:48:57 +08001837 req->r_ops[i].outdata_len = len;
Sage Weil1b83bef2013-02-25 16:11:12 -08001838 dout(" op %d has %d bytes\n", i, len);
1839 payload_len += len;
1840 p += sizeof(*op);
1841 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001842 bytes = le32_to_cpu(msg->hdr.data_len);
1843 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001844 pr_warn("sum of op payload lens %d != data_len %d\n",
1845 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001846 goto bad_put;
1847 }
1848
Dan Carpenter18741192013-08-15 08:51:58 +03001849 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001850 retry_attempt = ceph_decode_32(&p);
1851 for (i = 0; i < numops; i++)
Yan, Zheng7665d852016-01-07 16:48:57 +08001852 req->r_ops[i].rval = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001853
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001854 if (le16_to_cpu(msg->hdr.version) >= 6) {
1855 p += 8 + 4; /* skip replay_version */
1856 p += 8; /* skip user_version */
1857
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001858 if (le16_to_cpu(msg->hdr.version) >= 7)
1859 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1860 else
1861 decode_redir = 1;
1862 } else {
1863 decode_redir = 0;
1864 }
1865
1866 if (decode_redir) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001867 err = ceph_redirect_decode(&p, end, &redir);
1868 if (err)
1869 goto bad_put;
1870 } else {
1871 redir.oloc.pool = -1;
1872 }
1873
1874 if (redir.oloc.pool != -1) {
1875 dout("redirect pool %lld\n", redir.oloc.pool);
1876
1877 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001878
1879 req->r_target_oloc = redir.oloc; /* struct */
1880
1881 /*
1882 * Start redirect requests with nofail=true. If
1883 * mapping fails, request will end up on the notarget
1884 * list, waiting for the new osdmap (which can take
1885 * a while), even though the original request mapped
1886 * successfully. In the future we might want to follow
1887 * original request's nofail setting here.
1888 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001889 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001890 BUG_ON(err);
1891
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001892 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001893 }
1894
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001895 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001896 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001897 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001898 dout("handle_reply result %d bytes %d\n", req->r_result,
1899 bytes);
1900 if (req->r_result == 0)
1901 req->r_result = bytes;
1902
1903 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001904 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1905 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001906
1907 req->r_got_reply = 1;
1908 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1909 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001910 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001911 }
1912
1913 dout("handle_reply tid %llu flags %d\n", tid, flags);
1914
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001915 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1916 __register_linger_request(osdc, req);
1917
Sage Weilf24e9982009-10-06 11:31:10 -07001918 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001919 if (result < 0 ||
1920 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001921 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1922 __unregister_request(osdc, req);
1923
1924 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001925 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001926
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001927 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001928 if (req->r_unsafe_callback &&
1929 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1930 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001931 if (req->r_callback)
1932 req->r_callback(req, msg);
1933 else
1934 complete_all(&req->r_completion);
1935 }
Sage Weilf24e9982009-10-06 11:31:10 -07001936
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001937 if (flags & CEPH_OSD_FLAG_ONDISK) {
1938 if (req->r_unsafe_callback && already_completed)
1939 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001940 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001941 }
Sage Weilf24e9982009-10-06 11:31:10 -07001942
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001943out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001944 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001945 ceph_osdc_put_request(req);
1946 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001947out_unlock:
1948 mutex_unlock(&osdc->request_mutex);
1949 up_read(&osdc->map_sem);
1950 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001951
Sage Weil1b83bef2013-02-25 16:11:12 -08001952bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001953 req->r_result = -EIO;
1954 __unregister_request(osdc, req);
1955 if (req->r_callback)
1956 req->r_callback(req, msg);
1957 else
1958 complete_all(&req->r_completion);
1959 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001960 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001961bad_mutex:
1962 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001963 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001964bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001965 pr_err("corrupt osd_op_reply got %d %d\n",
1966 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001967 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001968}
1969
Sage Weil6f6c7002011-01-17 20:34:08 -08001970static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001971{
Sage Weilf24e9982009-10-06 11:31:10 -07001972 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001973
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001974 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08001975 for (p = rb_first(&osdc->osds); p; p = n) {
1976 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001977
Sage Weil6f6c7002011-01-17 20:34:08 -08001978 n = rb_next(p);
1979 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1980 memcmp(&osd->o_con.peer_addr,
1981 ceph_osd_addr(osdc->osdmap,
1982 osd->o_osd),
1983 sizeof(struct ceph_entity_addr)) != 0)
1984 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001985 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001986}
1987
1988/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001989 * Requeue requests whose mapping to an OSD has changed. If requests map to
1990 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001991 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001992 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001993 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001994static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1995 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001996{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001997 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001998 struct rb_node *p;
1999 int needmap = 0;
2000 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002001 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002002
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002003 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2004 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002005 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07002006 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08002007 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07002008 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06002009
2010 /*
2011 * For linger requests that have not yet been
2012 * registered, move them to the linger list; they'll
2013 * be sent to the osd in the loop below. Unregister
2014 * the request before re-registering it as a linger
2015 * request to ensure the __map_request() below
2016 * will decide it needs to be sent.
2017 */
2018 if (req->r_linger && list_empty(&req->r_linger_item)) {
2019 dout("%p tid %llu restart on osd%d\n",
2020 req, req->r_tid,
2021 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05002022 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002023 __unregister_request(osdc, req);
2024 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002025 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002026 continue;
2027 }
2028
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002029 force_resend_req = force_resend ||
2030 (force_resend_writes &&
2031 req->r_flags & CEPH_OSD_FLAG_WRITE);
2032 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002033 if (err < 0)
2034 continue; /* error */
2035 if (req->r_osd == NULL) {
2036 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2037 needmap++; /* request a newer map */
2038 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002039 if (!req->r_linger) {
2040 dout("%p tid %llu requeued on osd%d\n", req,
2041 req->r_tid,
2042 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002043 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002044 }
2045 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002046 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002047
2048 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2049 r_linger_item) {
2050 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2051
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002052 err = __map_request(osdc, req,
2053 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002054 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002055 if (err < 0)
2056 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002057 if (req->r_osd == NULL || err > 0) {
2058 if (req->r_osd == NULL) {
2059 dout("lingering %p tid %llu maps to no osd\n",
2060 req, req->r_tid);
2061 /*
2062 * A homeless lingering request makes
2063 * no sense, as it's job is to keep
2064 * a particular OSD connection open.
2065 * Request a newer map and kick the
2066 * request, knowing that it won't be
2067 * resent until we actually get a map
2068 * that can tell us where to send it.
2069 */
2070 needmap++;
2071 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002072
Ilya Dryomovb0494532015-05-11 17:53:10 +03002073 dout("kicking lingering %p tid %llu osd%d\n", req,
2074 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2075 __register_request(osdc, req);
2076 __unregister_linger_request(osdc, req);
2077 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002078 }
Alex Elder14d2f382013-05-15 16:28:33 -05002079 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002080 mutex_unlock(&osdc->request_mutex);
2081
2082 if (needmap) {
2083 dout("%d requests for down osds, need new map\n", needmap);
2084 ceph_monc_request_next_osdmap(&osdc->client->monc);
2085 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002086}
Sage Weil6f6c7002011-01-17 20:34:08 -08002087
2088
Sage Weilf24e9982009-10-06 11:31:10 -07002089/*
2090 * Process updated osd map.
2091 *
2092 * The message contains any number of incremental and full maps, normally
2093 * indicating some sort of topology change in the cluster. Kick requests
2094 * off to different OSDs as needed.
2095 */
2096void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2097{
2098 void *p, *end, *next;
2099 u32 nr_maps, maplen;
2100 u32 epoch;
2101 struct ceph_osdmap *newmap = NULL, *oldmap;
2102 int err;
2103 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002104 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002105
2106 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2107 p = msg->front.iov_base;
2108 end = p + msg->front.iov_len;
2109
2110 /* verify fsid */
2111 ceph_decode_need(&p, end, sizeof(fsid), bad);
2112 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002113 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2114 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002115
2116 down_write(&osdc->map_sem);
2117
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002118 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2119
Sage Weilf24e9982009-10-06 11:31:10 -07002120 /* incremental maps */
2121 ceph_decode_32_safe(&p, end, nr_maps, bad);
2122 dout(" %d inc maps\n", nr_maps);
2123 while (nr_maps > 0) {
2124 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002125 epoch = ceph_decode_32(&p);
2126 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002127 ceph_decode_need(&p, end, maplen, bad);
2128 next = p + maplen;
2129 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2130 dout("applying incremental map %u len %d\n",
2131 epoch, maplen);
2132 newmap = osdmap_apply_incremental(&p, next,
2133 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002134 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002135 if (IS_ERR(newmap)) {
2136 err = PTR_ERR(newmap);
2137 goto bad;
2138 }
Sage Weil30dc6382009-12-21 14:49:37 -08002139 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002140 if (newmap != osdc->osdmap) {
2141 ceph_osdmap_destroy(osdc->osdmap);
2142 osdc->osdmap = newmap;
2143 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002144 was_full = was_full ||
2145 ceph_osdmap_flag(osdc->osdmap,
2146 CEPH_OSDMAP_FULL);
2147 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002148 } else {
2149 dout("ignoring incremental map %u len %d\n",
2150 epoch, maplen);
2151 }
2152 p = next;
2153 nr_maps--;
2154 }
2155 if (newmap)
2156 goto done;
2157
2158 /* full maps */
2159 ceph_decode_32_safe(&p, end, nr_maps, bad);
2160 dout(" %d full maps\n", nr_maps);
2161 while (nr_maps) {
2162 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002163 epoch = ceph_decode_32(&p);
2164 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002165 ceph_decode_need(&p, end, maplen, bad);
2166 if (nr_maps > 1) {
2167 dout("skipping non-latest full map %u len %d\n",
2168 epoch, maplen);
2169 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2170 dout("skipping full map %u len %d, "
2171 "older than our %u\n", epoch, maplen,
2172 osdc->osdmap->epoch);
2173 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002174 int skipped_map = 0;
2175
Sage Weilf24e9982009-10-06 11:31:10 -07002176 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002177 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002178 if (IS_ERR(newmap)) {
2179 err = PTR_ERR(newmap);
2180 goto bad;
2181 }
Sage Weil30dc6382009-12-21 14:49:37 -08002182 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002183 oldmap = osdc->osdmap;
2184 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002185 if (oldmap) {
2186 if (oldmap->epoch + 1 < newmap->epoch)
2187 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002188 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002189 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002190 was_full = was_full ||
2191 ceph_osdmap_flag(osdc->osdmap,
2192 CEPH_OSDMAP_FULL);
2193 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002194 }
2195 p += maplen;
2196 nr_maps--;
2197 }
2198
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002199 if (!osdc->osdmap)
2200 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002201done:
2202 downgrade_write(&osdc->map_sem);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01002203 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2204 osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002205
2206 /*
2207 * subscribe to subsequent osdmap updates if full to ensure
2208 * we find out when we are no longer full and stop returning
2209 * ENOSPC.
2210 */
Josh Durgind29adb32013-12-02 19:11:48 -08002211 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2212 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2213 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002214 ceph_monc_request_next_osdmap(&osdc->client->monc);
2215
Alex Elderf9d25192013-02-15 11:42:29 -06002216 mutex_lock(&osdc->request_mutex);
2217 __send_queued(osdc);
2218 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002219 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002220 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002221 return;
2222
2223bad:
2224 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002225 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002226 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002227}
2228
Sage Weilf24e9982009-10-06 11:31:10 -07002229/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002230 * watch/notify callback event infrastructure
2231 *
2232 * These callbacks are used both for watch and notify operations.
2233 */
2234static void __release_event(struct kref *kref)
2235{
2236 struct ceph_osd_event *event =
2237 container_of(kref, struct ceph_osd_event, kref);
2238
2239 dout("__release_event %p\n", event);
2240 kfree(event);
2241}
2242
2243static void get_event(struct ceph_osd_event *event)
2244{
2245 kref_get(&event->kref);
2246}
2247
2248void ceph_osdc_put_event(struct ceph_osd_event *event)
2249{
2250 kref_put(&event->kref, __release_event);
2251}
2252EXPORT_SYMBOL(ceph_osdc_put_event);
2253
2254static void __insert_event(struct ceph_osd_client *osdc,
2255 struct ceph_osd_event *new)
2256{
2257 struct rb_node **p = &osdc->event_tree.rb_node;
2258 struct rb_node *parent = NULL;
2259 struct ceph_osd_event *event = NULL;
2260
2261 while (*p) {
2262 parent = *p;
2263 event = rb_entry(parent, struct ceph_osd_event, node);
2264 if (new->cookie < event->cookie)
2265 p = &(*p)->rb_left;
2266 else if (new->cookie > event->cookie)
2267 p = &(*p)->rb_right;
2268 else
2269 BUG();
2270 }
2271
2272 rb_link_node(&new->node, parent, p);
2273 rb_insert_color(&new->node, &osdc->event_tree);
2274}
2275
2276static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2277 u64 cookie)
2278{
2279 struct rb_node **p = &osdc->event_tree.rb_node;
2280 struct rb_node *parent = NULL;
2281 struct ceph_osd_event *event = NULL;
2282
2283 while (*p) {
2284 parent = *p;
2285 event = rb_entry(parent, struct ceph_osd_event, node);
2286 if (cookie < event->cookie)
2287 p = &(*p)->rb_left;
2288 else if (cookie > event->cookie)
2289 p = &(*p)->rb_right;
2290 else
2291 return event;
2292 }
2293 return NULL;
2294}
2295
2296static void __remove_event(struct ceph_osd_event *event)
2297{
2298 struct ceph_osd_client *osdc = event->osdc;
2299
2300 if (!RB_EMPTY_NODE(&event->node)) {
2301 dout("__remove_event removed %p\n", event);
2302 rb_erase(&event->node, &osdc->event_tree);
2303 ceph_osdc_put_event(event);
2304 } else {
2305 dout("__remove_event didn't remove %p\n", event);
2306 }
2307}
2308
2309int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2310 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002311 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002312{
2313 struct ceph_osd_event *event;
2314
2315 event = kmalloc(sizeof(*event), GFP_NOIO);
2316 if (!event)
2317 return -ENOMEM;
2318
2319 dout("create_event %p\n", event);
2320 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002321 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002322 event->data = data;
2323 event->osdc = osdc;
2324 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002325 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002326 kref_init(&event->kref); /* one ref for us */
2327 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002328
2329 spin_lock(&osdc->event_lock);
2330 event->cookie = ++osdc->event_count;
2331 __insert_event(osdc, event);
2332 spin_unlock(&osdc->event_lock);
2333
2334 *pevent = event;
2335 return 0;
2336}
2337EXPORT_SYMBOL(ceph_osdc_create_event);
2338
2339void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2340{
2341 struct ceph_osd_client *osdc = event->osdc;
2342
2343 dout("cancel_event %p\n", event);
2344 spin_lock(&osdc->event_lock);
2345 __remove_event(event);
2346 spin_unlock(&osdc->event_lock);
2347 ceph_osdc_put_event(event); /* caller's */
2348}
2349EXPORT_SYMBOL(ceph_osdc_cancel_event);
2350
2351
2352static void do_event_work(struct work_struct *work)
2353{
2354 struct ceph_osd_event_work *event_work =
2355 container_of(work, struct ceph_osd_event_work, work);
2356 struct ceph_osd_event *event = event_work->event;
2357 u64 ver = event_work->ver;
2358 u64 notify_id = event_work->notify_id;
2359 u8 opcode = event_work->opcode;
2360
2361 dout("do_event_work completing %p\n", event);
2362 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002363 dout("do_event_work completed %p\n", event);
2364 ceph_osdc_put_event(event);
2365 kfree(event_work);
2366}
2367
2368
2369/*
2370 * Process osd watch notifications
2371 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002372static void handle_watch_notify(struct ceph_osd_client *osdc,
2373 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002374{
2375 void *p, *end;
2376 u8 proto_ver;
2377 u64 cookie, ver, notify_id;
2378 u8 opcode;
2379 struct ceph_osd_event *event;
2380 struct ceph_osd_event_work *event_work;
2381
2382 p = msg->front.iov_base;
2383 end = p + msg->front.iov_len;
2384
2385 ceph_decode_8_safe(&p, end, proto_ver, bad);
2386 ceph_decode_8_safe(&p, end, opcode, bad);
2387 ceph_decode_64_safe(&p, end, cookie, bad);
2388 ceph_decode_64_safe(&p, end, ver, bad);
2389 ceph_decode_64_safe(&p, end, notify_id, bad);
2390
2391 spin_lock(&osdc->event_lock);
2392 event = __find_event(osdc, cookie);
2393 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002394 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002395 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002396 }
2397 spin_unlock(&osdc->event_lock);
2398 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2399 cookie, ver, event);
2400 if (event) {
2401 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002402 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002403 pr_err("couldn't allocate event_work\n");
2404 ceph_osdc_put_event(event);
2405 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002406 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002407 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002408 event_work->event = event;
2409 event_work->ver = ver;
2410 event_work->notify_id = notify_id;
2411 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002412
2413 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002414 }
2415
2416 return;
2417
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002418bad:
2419 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002420}
2421
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002422/*
Alex Eldere65550f2013-04-05 01:27:12 -05002423 * build new request AND message
2424 *
2425 */
2426void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2427 struct ceph_snap_context *snapc, u64 snap_id,
2428 struct timespec *mtime)
2429{
2430 struct ceph_msg *msg = req->r_request;
2431 void *p;
2432 size_t msg_size;
2433 int flags = req->r_flags;
2434 u64 data_len;
2435 unsigned int i;
2436
2437 req->r_snapid = snap_id;
2438 req->r_snapc = ceph_get_snap_context(snapc);
2439
2440 /* encode request */
2441 msg->hdr.version = cpu_to_le16(4);
2442
2443 p = msg->front.iov_base;
2444 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2445 req->r_request_osdmap_epoch = p;
2446 p += 4;
2447 req->r_request_flags = p;
2448 p += 4;
2449 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2450 ceph_encode_timespec(p, mtime);
2451 p += sizeof(struct ceph_timespec);
2452 req->r_request_reassert_version = p;
2453 p += sizeof(struct ceph_eversion); /* will get filled in */
2454
2455 /* oloc */
2456 ceph_encode_8(&p, 4);
2457 ceph_encode_8(&p, 4);
2458 ceph_encode_32(&p, 8 + 4 + 4);
2459 req->r_request_pool = p;
2460 p += 8;
2461 ceph_encode_32(&p, -1); /* preferred */
2462 ceph_encode_32(&p, 0); /* key len */
2463
2464 ceph_encode_8(&p, 1);
2465 req->r_request_pgid = p;
2466 p += 8 + 4;
2467 ceph_encode_32(&p, -1); /* preferred */
2468
2469 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002470 ceph_encode_32(&p, req->r_base_oid.name_len);
2471 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2472 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2473 req->r_base_oid.name, req->r_base_oid.name_len);
2474 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002475
2476 /* ops--can imply data */
2477 ceph_encode_16(&p, (u16)req->r_num_ops);
2478 data_len = 0;
2479 for (i = 0; i < req->r_num_ops; i++) {
2480 data_len += osd_req_encode_op(req, p, i);
2481 p += sizeof(struct ceph_osd_op);
2482 }
2483
2484 /* snaps */
2485 ceph_encode_64(&p, req->r_snapid);
2486 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2487 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2488 if (req->r_snapc) {
2489 for (i = 0; i < snapc->num_snaps; i++) {
2490 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2491 }
2492 }
2493
2494 req->r_request_attempts = p;
2495 p += 4;
2496
2497 /* data */
2498 if (flags & CEPH_OSD_FLAG_WRITE) {
2499 u16 data_off;
2500
2501 /*
2502 * The header "data_off" is a hint to the receiver
2503 * allowing it to align received data into its
2504 * buffers such that there's no need to re-copy
2505 * it before writing it to disk (direct I/O).
2506 */
2507 data_off = (u16) (off & 0xffff);
2508 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2509 }
2510 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2511
2512 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2513 msg_size = p - msg->front.iov_base;
2514 msg->front.iov_len = msg_size;
2515 msg->hdr.front_len = cpu_to_le32(msg_size);
2516
2517 dout("build_request msg_size was %d\n", (int)msg_size);
2518}
2519EXPORT_SYMBOL(ceph_osdc_build_request);
2520
2521/*
Sage Weilf24e9982009-10-06 11:31:10 -07002522 * Register request, send initial attempt.
2523 */
2524int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2525 struct ceph_osd_request *req,
2526 bool nofail)
2527{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002528 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002529
Sage Weilf24e9982009-10-06 11:31:10 -07002530 down_read(&osdc->map_sem);
2531 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002532
2533 rc = __ceph_osdc_start_request(osdc, req, nofail);
2534
Sage Weilf24e9982009-10-06 11:31:10 -07002535 mutex_unlock(&osdc->request_mutex);
2536 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002537
Sage Weilf24e9982009-10-06 11:31:10 -07002538 return rc;
2539}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002540EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002541
2542/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002543 * Unregister a registered request. The request is not completed (i.e.
2544 * no callbacks or wakeups) - higher layers are supposed to know what
2545 * they are canceling.
2546 */
2547void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2548{
2549 struct ceph_osd_client *osdc = req->r_osdc;
2550
2551 mutex_lock(&osdc->request_mutex);
2552 if (req->r_linger)
2553 __unregister_linger_request(osdc, req);
2554 __unregister_request(osdc, req);
2555 mutex_unlock(&osdc->request_mutex);
2556
2557 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2558}
2559EXPORT_SYMBOL(ceph_osdc_cancel_request);
2560
2561/*
Sage Weilf24e9982009-10-06 11:31:10 -07002562 * wait for a request to complete
2563 */
2564int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2565 struct ceph_osd_request *req)
2566{
2567 int rc;
2568
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002569 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2570
Sage Weilf24e9982009-10-06 11:31:10 -07002571 rc = wait_for_completion_interruptible(&req->r_completion);
2572 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002573 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2574 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002575 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002576 return rc;
2577 }
2578
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002579 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2580 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002581 return req->r_result;
2582}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002583EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002584
2585/*
2586 * sync - wait for all in-flight requests to flush. avoid starvation.
2587 */
2588void ceph_osdc_sync(struct ceph_osd_client *osdc)
2589{
2590 struct ceph_osd_request *req;
2591 u64 last_tid, next_tid = 0;
2592
2593 mutex_lock(&osdc->request_mutex);
2594 last_tid = osdc->last_tid;
2595 while (1) {
2596 req = __lookup_request_ge(osdc, next_tid);
2597 if (!req)
2598 break;
2599 if (req->r_tid > last_tid)
2600 break;
2601
2602 next_tid = req->r_tid + 1;
2603 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2604 continue;
2605
2606 ceph_osdc_get_request(req);
2607 mutex_unlock(&osdc->request_mutex);
2608 dout("sync waiting on tid %llu (last is %llu)\n",
2609 req->r_tid, last_tid);
2610 wait_for_completion(&req->r_safe_completion);
2611 mutex_lock(&osdc->request_mutex);
2612 ceph_osdc_put_request(req);
2613 }
2614 mutex_unlock(&osdc->request_mutex);
2615 dout("sync done (thru tid %llu)\n", last_tid);
2616}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002617EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002618
2619/*
Josh Durgindd935f42013-08-28 21:43:09 -07002620 * Call all pending notify callbacks - for use after a watch is
2621 * unregistered, to make sure no more callbacks for it will be invoked
2622 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002623void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002624{
2625 flush_workqueue(osdc->notify_wq);
2626}
2627EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2628
2629
2630/*
Sage Weilf24e9982009-10-06 11:31:10 -07002631 * init, shutdown
2632 */
2633int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2634{
2635 int err;
2636
2637 dout("init\n");
2638 osdc->client = client;
2639 osdc->osdmap = NULL;
2640 init_rwsem(&osdc->map_sem);
2641 init_completion(&osdc->map_waiters);
2642 osdc->last_requested_map = 0;
2643 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002644 osdc->last_tid = 0;
2645 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002646 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002647 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002648 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002649 INIT_LIST_HEAD(&osdc->req_unsent);
2650 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002651 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002652 osdc->num_requests = 0;
2653 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002654 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002655 spin_lock_init(&osdc->event_lock);
2656 osdc->event_tree = RB_ROOT;
2657 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002658
2659 schedule_delayed_work(&osdc->osds_timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03002660 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
Sage Weilf24e9982009-10-06 11:31:10 -07002661
Sage Weil5f44f142009-11-18 14:52:18 -08002662 err = -ENOMEM;
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01002663 osdc->req_mempool = mempool_create_slab_pool(10,
2664 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07002665 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002666 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002667
Sage Weild50b4092012-07-09 14:22:34 -07002668 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2669 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002670 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002671 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002672 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002673 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002674 OSD_OPREPLY_FRONT_LEN, 10, true,
2675 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002676 if (err < 0)
2677 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002678
Dan Carpenterdbcae082013-08-15 08:58:59 +03002679 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002680 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002681 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002682 goto out_msgpool_reply;
2683
Sage Weilf24e9982009-10-06 11:31:10 -07002684 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002685
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002686out_msgpool_reply:
2687 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002688out_msgpool:
2689 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002690out_mempool:
2691 mempool_destroy(osdc->req_mempool);
2692out:
2693 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002694}
2695
2696void ceph_osdc_stop(struct ceph_osd_client *osdc)
2697{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002698 flush_workqueue(osdc->notify_wq);
2699 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002700 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002701 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002702 if (osdc->osdmap) {
2703 ceph_osdmap_destroy(osdc->osdmap);
2704 osdc->osdmap = NULL;
2705 }
Sage Weilaca420b2011-08-31 14:45:53 -07002706 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002707 mempool_destroy(osdc->req_mempool);
2708 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002709 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002710}
2711
2712/*
2713 * Read some contiguous pages. If we cross a stripe boundary, shorten
2714 * *plen. Return number of bytes read, or error.
2715 */
2716int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2717 struct ceph_vino vino, struct ceph_file_layout *layout,
2718 u64 off, u64 *plen,
2719 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002720 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002721{
2722 struct ceph_osd_request *req;
2723 int rc = 0;
2724
2725 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2726 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002727 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002728 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002729 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002730 false);
Sage Weil68162822012-09-24 21:01:02 -07002731 if (IS_ERR(req))
2732 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002733
2734 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002735
Alex Elder406e2c92013-04-15 14:50:36 -05002736 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002737 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002738
Alex Eldere0c59482013-03-07 15:38:25 -06002739 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002740 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002741
Alex Elder79528732013-04-03 21:32:51 -05002742 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002743
Sage Weilf24e9982009-10-06 11:31:10 -07002744 rc = ceph_osdc_start_request(osdc, req, false);
2745 if (!rc)
2746 rc = ceph_osdc_wait_request(osdc, req);
2747
2748 ceph_osdc_put_request(req);
2749 dout("readpages result %d\n", rc);
2750 return rc;
2751}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002752EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002753
2754/*
2755 * do a synchronous write on N pages
2756 */
2757int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2758 struct ceph_file_layout *layout,
2759 struct ceph_snap_context *snapc,
2760 u64 off, u64 len,
2761 u32 truncate_seq, u64 truncate_size,
2762 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002763 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002764{
2765 struct ceph_osd_request *req;
2766 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002767 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002768
Alex Elderacead002013-03-14 14:09:05 -05002769 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002770 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002771 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002772 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002773 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002774 true);
Sage Weil68162822012-09-24 21:01:02 -07002775 if (IS_ERR(req))
2776 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002777
2778 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002779 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002780 false, false);
2781 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002782
Alex Elder79528732013-04-03 21:32:51 -05002783 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002784
Alex Elder87f979d2013-02-15 11:42:29 -06002785 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002786 if (!rc)
2787 rc = ceph_osdc_wait_request(osdc, req);
2788
2789 ceph_osdc_put_request(req);
2790 if (rc == 0)
2791 rc = len;
2792 dout("writepages result %d\n", rc);
2793 return rc;
2794}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002795EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002796
Alex Elder5522ae02013-05-01 12:43:04 -05002797int ceph_osdc_setup(void)
2798{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002799 size_t size = sizeof(struct ceph_osd_request) +
2800 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2801
Alex Elder5522ae02013-05-01 12:43:04 -05002802 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002803 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2804 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05002805
2806 return ceph_osd_request_cache ? 0 : -ENOMEM;
2807}
2808EXPORT_SYMBOL(ceph_osdc_setup);
2809
2810void ceph_osdc_cleanup(void)
2811{
2812 BUG_ON(!ceph_osd_request_cache);
2813 kmem_cache_destroy(ceph_osd_request_cache);
2814 ceph_osd_request_cache = NULL;
2815}
2816EXPORT_SYMBOL(ceph_osdc_cleanup);
2817
Sage Weilf24e9982009-10-06 11:31:10 -07002818/*
2819 * handle incoming message
2820 */
2821static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2822{
2823 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002824 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002825 int type = le16_to_cpu(msg->hdr.type);
2826
2827 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002828 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002829 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002830
2831 switch (type) {
2832 case CEPH_MSG_OSD_MAP:
2833 ceph_osdc_handle_map(osdc, msg);
2834 break;
2835 case CEPH_MSG_OSD_OPREPLY:
Shraddha Barke70cf0522015-10-18 13:55:38 +05302836 handle_reply(osdc, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002837 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002838 case CEPH_MSG_WATCH_NOTIFY:
2839 handle_watch_notify(osdc, msg);
2840 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002841
2842 default:
2843 pr_err("received unknown message type %d %s\n", type,
2844 ceph_msg_type_name(type));
2845 }
Sage Weil4a32f932010-06-13 10:27:53 -07002846out:
Sage Weilf24e9982009-10-06 11:31:10 -07002847 ceph_msg_put(msg);
2848}
2849
Sage Weil5b3a4db2010-02-19 21:43:23 -08002850/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002851 * Lookup and return message for incoming reply. Don't try to do
2852 * anything about a larger than preallocated data portion of the
2853 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002854 */
2855static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002856 struct ceph_msg_header *hdr,
2857 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002858{
2859 struct ceph_osd *osd = con->private;
2860 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002861 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002862 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002863 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002864 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002865 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002866
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002867 tid = le64_to_cpu(hdr->tid);
2868 mutex_lock(&osdc->request_mutex);
2869 req = __lookup_request(osdc, tid);
2870 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01002871 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2872 osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002873 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002874 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002875 goto out;
2876 }
Sage Weilc16e7862010-03-01 13:02:00 -08002877
Alex Elderace6d3a2013-04-01 16:12:14 -05002878 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002879
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002880 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002881 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2882 __func__, osd->o_osd, req->r_tid, front_len,
2883 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002884 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2885 false);
Sage Weila79832f2010-04-01 16:06:19 -07002886 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002887 goto out;
2888 ceph_msg_put(req->r_reply);
2889 req->r_reply = m;
2890 }
Sage Weilc16e7862010-03-01 13:02:00 -08002891
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002892 if (data_len > req->r_reply->data_length) {
2893 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2894 __func__, osd->o_osd, req->r_tid, data_len,
2895 req->r_reply->data_length);
2896 m = NULL;
2897 *skip = 1;
2898 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002899 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002900
2901 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002902 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002903
2904out:
2905 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002906 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002907}
2908
2909static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2910 struct ceph_msg_header *hdr,
2911 int *skip)
2912{
2913 struct ceph_osd *osd = con->private;
2914 int type = le16_to_cpu(hdr->type);
2915 int front = le32_to_cpu(hdr->front_len);
2916
Alex Elder1c20f2d2012-06-04 14:43:32 -05002917 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002918 switch (type) {
2919 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002920 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002921 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002922 case CEPH_MSG_OSD_OPREPLY:
2923 return get_reply(con, hdr, skip);
2924 default:
2925 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2926 osd->o_osd);
2927 *skip = 1;
2928 return NULL;
2929 }
Sage Weilf24e9982009-10-06 11:31:10 -07002930}
2931
2932/*
2933 * Wrappers to refcount containing ceph_osd struct
2934 */
2935static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2936{
2937 struct ceph_osd *osd = con->private;
2938 if (get_osd(osd))
2939 return con;
2940 return NULL;
2941}
2942
2943static void put_osd_con(struct ceph_connection *con)
2944{
2945 struct ceph_osd *osd = con->private;
2946 put_osd(osd);
2947}
2948
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002949/*
2950 * authentication
2951 */
Alex Eldera3530df2012-05-16 15:16:39 -05002952/*
2953 * Note: returned pointer is the address of a structure that's
2954 * managed separately. Caller must *not* attempt to free it.
2955 */
2956static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002957 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002958{
2959 struct ceph_osd *o = con->private;
2960 struct ceph_osd_client *osdc = o->o_osdc;
2961 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002962 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002963
Alex Elder74f18692012-05-16 15:16:39 -05002964 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002965 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002966 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002967 }
Sage Weil27859f92013-03-25 10:26:14 -07002968 if (!auth->authorizer) {
2969 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2970 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002971 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002972 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002973 } else {
2974 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002975 auth);
2976 if (ret)
2977 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002978 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002979 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002980
Alex Eldera3530df2012-05-16 15:16:39 -05002981 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002982}
2983
2984
2985static int verify_authorizer_reply(struct ceph_connection *con, int len)
2986{
2987 struct ceph_osd *o = con->private;
2988 struct ceph_osd_client *osdc = o->o_osdc;
2989 struct ceph_auth_client *ac = osdc->client->monc.auth;
2990
Sage Weil27859f92013-03-25 10:26:14 -07002991 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002992}
2993
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002994static int invalidate_authorizer(struct ceph_connection *con)
2995{
2996 struct ceph_osd *o = con->private;
2997 struct ceph_osd_client *osdc = o->o_osdc;
2998 struct ceph_auth_client *ac = osdc->client->monc.auth;
2999
Sage Weil27859f92013-03-25 10:26:14 -07003000 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003001 return ceph_monc_validate_auth(&osdc->client->monc);
3002}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003003
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003004static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003005{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003006 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003007 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003008
Yan, Zheng33d07332014-11-04 16:33:37 +08003009 return ceph_auth_sign_message(auth, msg);
3010}
3011
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003012static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003013{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003014 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003015 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003016
Yan, Zheng33d07332014-11-04 16:33:37 +08003017 return ceph_auth_check_message_signature(auth, msg);
3018}
3019
Tobias Klauser9e327892010-05-20 10:40:19 +02003020static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07003021 .get = get_osd_con,
3022 .put = put_osd_con,
3023 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003024 .get_authorizer = get_authorizer,
3025 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003026 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003027 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003028 .sign_message = osd_sign_message,
3029 .check_message_signature = osd_check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003030 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003031};